xref: /illumos-gate/usr/src/cmd/zoneadmd/zcons.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Console support for zones requires a significant infrastructure.  The
31*7c478bd9Sstevel@tonic-gate  * core pieces are contained in this file, but other portions of note
32*7c478bd9Sstevel@tonic-gate  * are in the zlogin(1M) command, the zcons(7D) driver, and in the
33*7c478bd9Sstevel@tonic-gate  * devfsadm(1M) misc_link generator.
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * Care is taken to make the console behave in an "intuitive" fashion for
36*7c478bd9Sstevel@tonic-gate  * administrators.  Essentially, we try as much as possible to mimic the
37*7c478bd9Sstevel@tonic-gate  * experience of using a system via a tip line and system controller.
38*7c478bd9Sstevel@tonic-gate  *
39*7c478bd9Sstevel@tonic-gate  * The zone console architecture looks like this:
40*7c478bd9Sstevel@tonic-gate  *
41*7c478bd9Sstevel@tonic-gate  *                                      Global Zone | Non-Global Zone
42*7c478bd9Sstevel@tonic-gate  *                        .--------------.          |
43*7c478bd9Sstevel@tonic-gate  *        .-----------.   | zoneadmd -z  |          | .--------. .---------.
44*7c478bd9Sstevel@tonic-gate  *        | zlogin -C |   |     myzone   |          | | ttymon | | syslogd |
45*7c478bd9Sstevel@tonic-gate  *        `-----------'   `--------------'          | `--------' `---------'
46*7c478bd9Sstevel@tonic-gate  *                  |       |       | |             |      |       |
47*7c478bd9Sstevel@tonic-gate  *  User            |       |       | |             |      V       V
48*7c478bd9Sstevel@tonic-gate  * - - - - - - - - -|- - - -|- - - -|-|- - - - - - -|- - /dev/zconsole - - -
49*7c478bd9Sstevel@tonic-gate  *  Kernel          V       V       | |                        |
50*7c478bd9Sstevel@tonic-gate  *               [AF_UNIX Socket]   | `--------. .-------------'
51*7c478bd9Sstevel@tonic-gate  *                                  |          | |
52*7c478bd9Sstevel@tonic-gate  *                                  |          V V
53*7c478bd9Sstevel@tonic-gate  *                                  |     +-----------+
54*7c478bd9Sstevel@tonic-gate  *                                  |     |  ldterm,  |
55*7c478bd9Sstevel@tonic-gate  *                                  |     |   etc.    |
56*7c478bd9Sstevel@tonic-gate  *                                  |     +-----------+
57*7c478bd9Sstevel@tonic-gate  *                                  |     +-[Anchor]--+
58*7c478bd9Sstevel@tonic-gate  *                                  |     |   ptem    |
59*7c478bd9Sstevel@tonic-gate  *                                  V     +-----------+
60*7c478bd9Sstevel@tonic-gate  *                           +---master---+---slave---+
61*7c478bd9Sstevel@tonic-gate  *                           |                        |
62*7c478bd9Sstevel@tonic-gate  *                           |      zcons driver      |
63*7c478bd9Sstevel@tonic-gate  *                           |    zonename="myzone"   |
64*7c478bd9Sstevel@tonic-gate  *                           +------------------------+
65*7c478bd9Sstevel@tonic-gate  *
66*7c478bd9Sstevel@tonic-gate  * There are basically three major tasks which the console subsystem in
67*7c478bd9Sstevel@tonic-gate  * zoneadmd accomplishes:
68*7c478bd9Sstevel@tonic-gate  *
69*7c478bd9Sstevel@tonic-gate  * - Setup and teardown of zcons driver instances.  One zcons instance
70*7c478bd9Sstevel@tonic-gate  *   is maintained per zone; we take advantage of the libdevice APIs
71*7c478bd9Sstevel@tonic-gate  *   to online new instances of zcons as needed.  Care is taken to
72*7c478bd9Sstevel@tonic-gate  *   prune and manage these appropriately; see init_console_dev() and
73*7c478bd9Sstevel@tonic-gate  *   destroy_console_dev().  The end result is the creation of the
74*7c478bd9Sstevel@tonic-gate  *   zcons(7D) instance and an open file descriptor to the master side.
75*7c478bd9Sstevel@tonic-gate  *   zcons instances are associated with zones via their zonename device
76*7c478bd9Sstevel@tonic-gate  *   property.  This the console instance to persist across reboots,
77*7c478bd9Sstevel@tonic-gate  *   and while the zone is halted.
78*7c478bd9Sstevel@tonic-gate  *
79*7c478bd9Sstevel@tonic-gate  * - Initialization of the slave side of the console.  This is
80*7c478bd9Sstevel@tonic-gate  *   accomplished by pushing various STREAMS modules onto the console.
81*7c478bd9Sstevel@tonic-gate  *   The ptem(7M) module gets special treatment, and is anchored into
82*7c478bd9Sstevel@tonic-gate  *   place using the I_ANCHOR facility.  This is so that the zcons driver
83*7c478bd9Sstevel@tonic-gate  *   always has terminal semantics, as would a real hardware terminal.
84*7c478bd9Sstevel@tonic-gate  *   This means that ttymon(1M) works unmodified;  at boot time, ttymon
85*7c478bd9Sstevel@tonic-gate  *   will do its own plumbing of the console stream, and will even
86*7c478bd9Sstevel@tonic-gate  *   I_POP modules off.  Hence the anchor, which assures that ptem will
87*7c478bd9Sstevel@tonic-gate  *   never be I_POP'd.
88*7c478bd9Sstevel@tonic-gate  *
89*7c478bd9Sstevel@tonic-gate  * - Acting as a server for 'zlogin -C' instances.  When zlogin -C is
90*7c478bd9Sstevel@tonic-gate  *   run, zlogin connects to zoneadmd via unix domain socket.  zoneadmd
91*7c478bd9Sstevel@tonic-gate  *   functions as a two-way proxy for console I/O, relaying user input
92*7c478bd9Sstevel@tonic-gate  *   to the master side of the console, and relaying output from the
93*7c478bd9Sstevel@tonic-gate  *   zone to the user.
94*7c478bd9Sstevel@tonic-gate  */
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
97*7c478bd9Sstevel@tonic-gate #include <sys/socket.h>
98*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
99*7c478bd9Sstevel@tonic-gate #include <sys/termios.h>
100*7c478bd9Sstevel@tonic-gate #include <sys/zcons.h>
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate #include <assert.h>
103*7c478bd9Sstevel@tonic-gate #include <ctype.h>
104*7c478bd9Sstevel@tonic-gate #include <errno.h>
105*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
106*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
107*7c478bd9Sstevel@tonic-gate #include <stdio.h>
108*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
109*7c478bd9Sstevel@tonic-gate #include <strings.h>
110*7c478bd9Sstevel@tonic-gate #include <stropts.h>
111*7c478bd9Sstevel@tonic-gate #include <thread.h>
112*7c478bd9Sstevel@tonic-gate #include <ucred.h>
113*7c478bd9Sstevel@tonic-gate #include <unistd.h>
114*7c478bd9Sstevel@tonic-gate #include <zone.h>
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
117*7c478bd9Sstevel@tonic-gate #include <libdevice.h>
118*7c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate #include <syslog.h>
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate #include "zoneadmd.h"
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate #define	ZCONSNEX_DEVTREEPATH	"/pseudo/zconsnex@1"
125*7c478bd9Sstevel@tonic-gate #define	ZCONSNEX_FILEPATH	"/devices/pseudo/zconsnex@1"
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate #define	CONSOLE_SOCKPATH	ZONES_TMPDIR "/%s.console_sock"
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate static int	slavefd = -1;	/* slave side of console */
130*7c478bd9Sstevel@tonic-gate static int	serverfd = -1;	/* console server unix domain socket fd */
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate static struct termios base_termios = {	/* from init.c */
133*7c478bd9Sstevel@tonic-gate 	BRKINT|ICRNL|IXON|IMAXBEL,			/* iflag */
134*7c478bd9Sstevel@tonic-gate 	OPOST|ONLCR|TAB3,				/* oflag */
135*7c478bd9Sstevel@tonic-gate 	CS8|CREAD|B9600,				/* cflag */
136*7c478bd9Sstevel@tonic-gate 	ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN,	/* lflag */
137*7c478bd9Sstevel@tonic-gate 	CINTR, CQUIT, CERASE, CKILL, CEOF, 0, 0, 0,
138*7c478bd9Sstevel@tonic-gate 	0, 0, 0, 0, 0, 0, 0, 0,
139*7c478bd9Sstevel@tonic-gate 	0, 0, 0
140*7c478bd9Sstevel@tonic-gate };
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate /*
143*7c478bd9Sstevel@tonic-gate  * The eventstream is a simple one-directional flow of messages from the
144*7c478bd9Sstevel@tonic-gate  * door server to the console subsystem, implemented with a pipe.
145*7c478bd9Sstevel@tonic-gate  * It is used to wake up the console poller when it needs to take action,
146*7c478bd9Sstevel@tonic-gate  * message the user, die off, etc.
147*7c478bd9Sstevel@tonic-gate  */
148*7c478bd9Sstevel@tonic-gate static int eventstream[2];
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate int
151*7c478bd9Sstevel@tonic-gate eventstream_init()
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 	if (pipe(eventstream) == -1)
154*7c478bd9Sstevel@tonic-gate 		return (-1);
155*7c478bd9Sstevel@tonic-gate 	return (0);
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate void
159*7c478bd9Sstevel@tonic-gate eventstream_write(zone_evt_t evt)
160*7c478bd9Sstevel@tonic-gate {
161*7c478bd9Sstevel@tonic-gate 	(void) write(eventstream[0], &evt, sizeof (evt));
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate static zone_evt_t
165*7c478bd9Sstevel@tonic-gate eventstream_read(void)
166*7c478bd9Sstevel@tonic-gate {
167*7c478bd9Sstevel@tonic-gate 	zone_evt_t evt = Z_EVT_NULL;
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	(void) read(eventstream[1], &evt, sizeof (evt));
170*7c478bd9Sstevel@tonic-gate 	return (evt);
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate /*
174*7c478bd9Sstevel@tonic-gate  * count_console_devs() and its helper count_cb() do a walk of the
175*7c478bd9Sstevel@tonic-gate  * subtree of the device tree where zone console nodes are represented.
176*7c478bd9Sstevel@tonic-gate  * The goal is to count zone console instances already setup for a zone
177*7c478bd9Sstevel@tonic-gate  * with the given name.  More than 1 is anomolous, and our caller will
178*7c478bd9Sstevel@tonic-gate  * have to deal with that if we find that's the case.
179*7c478bd9Sstevel@tonic-gate  *
180*7c478bd9Sstevel@tonic-gate  * Note: this algorithm is a linear search of nodes in the zconsnex subtree
181*7c478bd9Sstevel@tonic-gate  * of the device tree, and could be a scalability problem, but I don't see
182*7c478bd9Sstevel@tonic-gate  * how to avoid it.
183*7c478bd9Sstevel@tonic-gate  */
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate /*
186*7c478bd9Sstevel@tonic-gate  * cb_data is shared by count_cb and destroy_cb for simplicity.
187*7c478bd9Sstevel@tonic-gate  */
188*7c478bd9Sstevel@tonic-gate struct cb_data {
189*7c478bd9Sstevel@tonic-gate 	zlog_t *zlogp;
190*7c478bd9Sstevel@tonic-gate 	int found;
191*7c478bd9Sstevel@tonic-gate 	int killed;
192*7c478bd9Sstevel@tonic-gate };
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate static int
195*7c478bd9Sstevel@tonic-gate count_cb(di_node_t node, void *arg)
196*7c478bd9Sstevel@tonic-gate {
197*7c478bd9Sstevel@tonic-gate 	struct cb_data *cb = (struct cb_data *)arg;
198*7c478bd9Sstevel@tonic-gate 	char *prop_data;
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "zonename",
201*7c478bd9Sstevel@tonic-gate 	    &prop_data) != -1) {
202*7c478bd9Sstevel@tonic-gate 		assert(prop_data != NULL);
203*7c478bd9Sstevel@tonic-gate 		if (strcmp(prop_data, zone_name) == 0) {
204*7c478bd9Sstevel@tonic-gate 			cb->found++;
205*7c478bd9Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
206*7c478bd9Sstevel@tonic-gate 		}
207*7c478bd9Sstevel@tonic-gate 	}
208*7c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate static int
212*7c478bd9Sstevel@tonic-gate count_console_devs(zlog_t *zlogp)
213*7c478bd9Sstevel@tonic-gate {
214*7c478bd9Sstevel@tonic-gate 	di_node_t root;
215*7c478bd9Sstevel@tonic-gate 	struct cb_data cb;
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 	bzero(&cb, sizeof (cb));
218*7c478bd9Sstevel@tonic-gate 	cb.zlogp = zlogp;
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	if ((root = di_init(ZCONSNEX_DEVTREEPATH, DINFOCPYALL)) ==
221*7c478bd9Sstevel@tonic-gate 	    DI_NODE_NIL) {
222*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "di_init");
223*7c478bd9Sstevel@tonic-gate 		return (-1);
224*7c478bd9Sstevel@tonic-gate 	}
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	(void) di_walk_node(root, DI_WALK_CLDFIRST, (void *)&cb, count_cb);
227*7c478bd9Sstevel@tonic-gate 	di_fini(root);
228*7c478bd9Sstevel@tonic-gate 	return (cb.found);
229*7c478bd9Sstevel@tonic-gate }
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate /*
232*7c478bd9Sstevel@tonic-gate  * destroy_console_devs() and its helper destroy_cb() tears down any console
233*7c478bd9Sstevel@tonic-gate  * instances associated with this zone.  If things went very wrong, we
234*7c478bd9Sstevel@tonic-gate  * might have more than one console instance hanging around.  This routine
235*7c478bd9Sstevel@tonic-gate  * hunts down and tries to remove all of them.  Of course, if the console
236*7c478bd9Sstevel@tonic-gate  * is open, the instance will not detach, which is a potential issue.
237*7c478bd9Sstevel@tonic-gate  */
238*7c478bd9Sstevel@tonic-gate static int
239*7c478bd9Sstevel@tonic-gate destroy_cb(di_node_t node, void *arg)
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate 	struct cb_data *cb = (struct cb_data *)arg;
242*7c478bd9Sstevel@tonic-gate 	char *prop_data;
243*7c478bd9Sstevel@tonic-gate 	char *tmp;
244*7c478bd9Sstevel@tonic-gate 	char devpath[MAXPATHLEN];
245*7c478bd9Sstevel@tonic-gate 	devctl_hdl_t hdl;
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	if (di_prop_lookup_strings(DDI_DEV_T_ANY, node, "zonename",
248*7c478bd9Sstevel@tonic-gate 	    &prop_data) == -1)
249*7c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	assert(prop_data != NULL);
252*7c478bd9Sstevel@tonic-gate 	if (strcmp(prop_data, zone_name) != 0) {
253*7c478bd9Sstevel@tonic-gate 		/* this is the console for a different zone */
254*7c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
255*7c478bd9Sstevel@tonic-gate 	}
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 	cb->found++;
258*7c478bd9Sstevel@tonic-gate 	tmp = di_devfs_path(node);
259*7c478bd9Sstevel@tonic-gate 	(void) snprintf(devpath, sizeof (devpath), "/devices/%s", tmp);
260*7c478bd9Sstevel@tonic-gate 	di_devfs_path_free(tmp);
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	if ((hdl = devctl_device_acquire(devpath, 0)) == NULL) {
263*7c478bd9Sstevel@tonic-gate 		zerror(cb->zlogp, B_TRUE, "WARNING: console %s found, "
264*7c478bd9Sstevel@tonic-gate 		    "but it could not be controlled.", devpath);
265*7c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
266*7c478bd9Sstevel@tonic-gate 	}
267*7c478bd9Sstevel@tonic-gate 	if (devctl_device_remove(hdl) == 0) {
268*7c478bd9Sstevel@tonic-gate 		cb->killed++;
269*7c478bd9Sstevel@tonic-gate 	} else {
270*7c478bd9Sstevel@tonic-gate 		zerror(cb->zlogp, B_TRUE, "WARNING: console %s found, "
271*7c478bd9Sstevel@tonic-gate 		    "but it could not be removed.", devpath);
272*7c478bd9Sstevel@tonic-gate 	}
273*7c478bd9Sstevel@tonic-gate 	devctl_release(hdl);
274*7c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
275*7c478bd9Sstevel@tonic-gate }
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate static int
278*7c478bd9Sstevel@tonic-gate destroy_console_devs(zlog_t *zlogp)
279*7c478bd9Sstevel@tonic-gate {
280*7c478bd9Sstevel@tonic-gate 	di_node_t root;
281*7c478bd9Sstevel@tonic-gate 	struct cb_data cb;
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	bzero(&cb, sizeof (cb));
284*7c478bd9Sstevel@tonic-gate 	cb.zlogp = zlogp;
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 	if ((root = di_init(ZCONSNEX_DEVTREEPATH, DINFOCPYALL)) ==
287*7c478bd9Sstevel@tonic-gate 	    DI_NODE_NIL) {
288*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "di_init");
289*7c478bd9Sstevel@tonic-gate 		return (-1);
290*7c478bd9Sstevel@tonic-gate 	}
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 	(void) di_walk_node(root, DI_WALK_CLDFIRST, (void *)&cb, destroy_cb);
293*7c478bd9Sstevel@tonic-gate 	if (cb.found > 1) {
294*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "WARNING: multiple zone console "
295*7c478bd9Sstevel@tonic-gate 		    "instances detected for zone '%s'; %d of %d "
296*7c478bd9Sstevel@tonic-gate 		    "successfully removed.",
297*7c478bd9Sstevel@tonic-gate 		    zone_name, cb.killed, cb.found);
298*7c478bd9Sstevel@tonic-gate 	}
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 	di_fini(root);
301*7c478bd9Sstevel@tonic-gate 	return (0);
302*7c478bd9Sstevel@tonic-gate }
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate /*
305*7c478bd9Sstevel@tonic-gate  * init_console_dev() drives the device-tree configuration of the zone
306*7c478bd9Sstevel@tonic-gate  * console device.  The general strategy is to use the libdevice (devctl)
307*7c478bd9Sstevel@tonic-gate  * interfaces to instantiate a new zone console node.  We do a lot of
308*7c478bd9Sstevel@tonic-gate  * sanity checking, and are careful to reuse a console if one exists.
309*7c478bd9Sstevel@tonic-gate  *
310*7c478bd9Sstevel@tonic-gate  * Once the device is in the device tree, we kick devfsadm via di_init_devs()
311*7c478bd9Sstevel@tonic-gate  * to ensure that the appropriate symlinks (to the master and slave console
312*7c478bd9Sstevel@tonic-gate  * devices) are placed in /dev in the global zone.
313*7c478bd9Sstevel@tonic-gate  */
314*7c478bd9Sstevel@tonic-gate static int
315*7c478bd9Sstevel@tonic-gate init_console_dev(zlog_t *zlogp)
316*7c478bd9Sstevel@tonic-gate {
317*7c478bd9Sstevel@tonic-gate 	devctl_hdl_t bus_hdl = NULL, dev_hdl = NULL;
318*7c478bd9Sstevel@tonic-gate 	devctl_ddef_t ddef_hdl = NULL;
319*7c478bd9Sstevel@tonic-gate 	di_devlink_handle_t dl = NULL;
320*7c478bd9Sstevel@tonic-gate 	int rv = -1, ndevs;
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate 	/*
323*7c478bd9Sstevel@tonic-gate 	 * Don't re-setup console if it is working and ready already; just
324*7c478bd9Sstevel@tonic-gate 	 * skip ahead to making devlinks, which we do for sanity's sake.
325*7c478bd9Sstevel@tonic-gate 	 */
326*7c478bd9Sstevel@tonic-gate 	ndevs = count_console_devs(zlogp);
327*7c478bd9Sstevel@tonic-gate 	if (ndevs == 1) {
328*7c478bd9Sstevel@tonic-gate 		goto devlinks;
329*7c478bd9Sstevel@tonic-gate 	} else if (ndevs > 1 || ndevs == -1) {
330*7c478bd9Sstevel@tonic-gate 		/*
331*7c478bd9Sstevel@tonic-gate 		 * For now, this seems like a reasonable but harsh punishment.
332*7c478bd9Sstevel@tonic-gate 		 * If needed, we could try to get clever and delete all but
333*7c478bd9Sstevel@tonic-gate 		 * the console which is pointed at by the current symlink.
334*7c478bd9Sstevel@tonic-gate 		 */
335*7c478bd9Sstevel@tonic-gate 		if (destroy_console_devs(zlogp) == -1) {
336*7c478bd9Sstevel@tonic-gate 			goto error;
337*7c478bd9Sstevel@tonic-gate 		}
338*7c478bd9Sstevel@tonic-gate 	}
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	/*
341*7c478bd9Sstevel@tonic-gate 	 * Time to make the consoles!
342*7c478bd9Sstevel@tonic-gate 	 */
343*7c478bd9Sstevel@tonic-gate 	if ((bus_hdl = devctl_bus_acquire(ZCONSNEX_FILEPATH, 0)) == NULL) {
344*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "%s failed", "devctl_bus_acquire");
345*7c478bd9Sstevel@tonic-gate 		goto error;
346*7c478bd9Sstevel@tonic-gate 	}
347*7c478bd9Sstevel@tonic-gate 	if ((ddef_hdl = devctl_ddef_alloc("zcons", 0)) == NULL) {
348*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to allocate ddef handle");
349*7c478bd9Sstevel@tonic-gate 		goto error;
350*7c478bd9Sstevel@tonic-gate 	}
351*7c478bd9Sstevel@tonic-gate 	/*
352*7c478bd9Sstevel@tonic-gate 	 * Set three properties on this node; the first is the name of the
353*7c478bd9Sstevel@tonic-gate 	 * zone; the second is a flag which lets pseudo know that it is
354*7c478bd9Sstevel@tonic-gate 	 * OK to automatically allocate an instance # for this device;
355*7c478bd9Sstevel@tonic-gate 	 * the third tells the device framework not to auto-detach this
356*7c478bd9Sstevel@tonic-gate 	 * node-- we need the node to still be there when we ask devfsadmd
357*7c478bd9Sstevel@tonic-gate 	 * to make links, and when we need to open it.
358*7c478bd9Sstevel@tonic-gate 	 */
359*7c478bd9Sstevel@tonic-gate 	if (devctl_ddef_string(ddef_hdl, "zonename", zone_name) == -1) {
360*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to create zonename property");
361*7c478bd9Sstevel@tonic-gate 		goto error;
362*7c478bd9Sstevel@tonic-gate 	}
363*7c478bd9Sstevel@tonic-gate 	if (devctl_ddef_int(ddef_hdl, "auto-assign-instance", 1) == -1) {
364*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to create auto-assign-instance "
365*7c478bd9Sstevel@tonic-gate 		    "property");
366*7c478bd9Sstevel@tonic-gate 		goto error;
367*7c478bd9Sstevel@tonic-gate 	}
368*7c478bd9Sstevel@tonic-gate 	if (devctl_ddef_int(ddef_hdl, "ddi-no-autodetach", 1) == -1) {
369*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to create ddi-no-auto-detach "
370*7c478bd9Sstevel@tonic-gate 		    "property");
371*7c478bd9Sstevel@tonic-gate 		goto error;
372*7c478bd9Sstevel@tonic-gate 	}
373*7c478bd9Sstevel@tonic-gate 	if (devctl_bus_dev_create(bus_hdl, ddef_hdl, 0, &dev_hdl) == -1) {
374*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to create console node");
375*7c478bd9Sstevel@tonic-gate 		goto error;
376*7c478bd9Sstevel@tonic-gate 	}
377*7c478bd9Sstevel@tonic-gate 
378*7c478bd9Sstevel@tonic-gate devlinks:
379*7c478bd9Sstevel@tonic-gate 	if ((dl = di_devlink_init("zcons", DI_MAKE_LINK)) != NULL) {
380*7c478bd9Sstevel@tonic-gate 		(void) di_devlink_fini(&dl);
381*7c478bd9Sstevel@tonic-gate 	} else {
382*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to create devlinks");
383*7c478bd9Sstevel@tonic-gate 		goto error;
384*7c478bd9Sstevel@tonic-gate 	}
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	rv = 0;
387*7c478bd9Sstevel@tonic-gate error:
388*7c478bd9Sstevel@tonic-gate 	if (ddef_hdl)
389*7c478bd9Sstevel@tonic-gate 		devctl_ddef_free(ddef_hdl);
390*7c478bd9Sstevel@tonic-gate 	if (bus_hdl)
391*7c478bd9Sstevel@tonic-gate 		devctl_release(bus_hdl);
392*7c478bd9Sstevel@tonic-gate 	if (dev_hdl)
393*7c478bd9Sstevel@tonic-gate 		devctl_release(dev_hdl);
394*7c478bd9Sstevel@tonic-gate 	return (rv);
395*7c478bd9Sstevel@tonic-gate }
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate /*
398*7c478bd9Sstevel@tonic-gate  * prep_console_slave() takes care of setting up the console slave device
399*7c478bd9Sstevel@tonic-gate  * (the side that the zone will eventually open). It is a helper for
400*7c478bd9Sstevel@tonic-gate  * init_console_slave().
401*7c478bd9Sstevel@tonic-gate  *
402*7c478bd9Sstevel@tonic-gate  * We have to mknod and setup the console device; then the slave side is
403*7c478bd9Sstevel@tonic-gate  * opened, and the appropriate STREAMS modules are pushed on.  A wrinkle is that
404*7c478bd9Sstevel@tonic-gate  * 'ptem' must be anchored in place (see streamio(7i) since we always want the
405*7c478bd9Sstevel@tonic-gate  * console to have terminal semantics.
406*7c478bd9Sstevel@tonic-gate  */
407*7c478bd9Sstevel@tonic-gate static int
408*7c478bd9Sstevel@tonic-gate prep_console_slave(zlog_t *zlogp, char *zonepath)
409*7c478bd9Sstevel@tonic-gate {
410*7c478bd9Sstevel@tonic-gate 	char slavename[MAXPATHLEN];
411*7c478bd9Sstevel@tonic-gate 	char zoneslavename[MAXPATHLEN];
412*7c478bd9Sstevel@tonic-gate 	struct stat st;
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 	assert(slavefd == -1);
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 	(void) snprintf(slavename, sizeof (slavename),
417*7c478bd9Sstevel@tonic-gate 	    "/dev/zcons/%s/%s", zone_name, ZCONS_SLAVE_NAME);
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 	(void) snprintf(zoneslavename, sizeof (zoneslavename),
420*7c478bd9Sstevel@tonic-gate 	    "%s/dev/zconsole", zonepath);
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate 	/*
423*7c478bd9Sstevel@tonic-gate 	 * We mknod the zone console in $zonepath/dev/; someday it would
424*7c478bd9Sstevel@tonic-gate 	 * be nice to not have to manually mknod this stuff-- if possible,
425*7c478bd9Sstevel@tonic-gate 	 * we could move this into devfsadm.
426*7c478bd9Sstevel@tonic-gate 	 */
427*7c478bd9Sstevel@tonic-gate 	if (stat(slavename, &st) == -1) {
428*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to stat %s", slavename);
429*7c478bd9Sstevel@tonic-gate 		goto error;
430*7c478bd9Sstevel@tonic-gate 	}
431*7c478bd9Sstevel@tonic-gate 	(void) unlink(zoneslavename);
432*7c478bd9Sstevel@tonic-gate 	if (mknod(zoneslavename, st.st_mode, st.st_rdev) == -1) {
433*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to mknod %s", zoneslavename);
434*7c478bd9Sstevel@tonic-gate 		goto error;
435*7c478bd9Sstevel@tonic-gate 	}
436*7c478bd9Sstevel@tonic-gate 	(void) chown(zoneslavename, st.st_uid, st.st_gid);
437*7c478bd9Sstevel@tonic-gate 	if ((slavefd = open(zoneslavename, O_RDWR | O_NOCTTY)) < 0) {
438*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to open %s", zoneslavename);
439*7c478bd9Sstevel@tonic-gate 		return (-1);
440*7c478bd9Sstevel@tonic-gate 	}
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate 	/*
443*7c478bd9Sstevel@tonic-gate 	 * Just to make sure the whole stream is pristine.
444*7c478bd9Sstevel@tonic-gate 	 */
445*7c478bd9Sstevel@tonic-gate 	(void) ioctl(slavefd, I_FLUSH, FLUSHRW);
446*7c478bd9Sstevel@tonic-gate 
447*7c478bd9Sstevel@tonic-gate 	/*
448*7c478bd9Sstevel@tonic-gate 	 * Push hardware emulation (ptem) module; we would rather not, but
449*7c478bd9Sstevel@tonic-gate 	 * are forced to push ldterm and ttcompat here.  Ultimately ttymon
450*7c478bd9Sstevel@tonic-gate 	 * will pop them off, so we ANCHOR only ptem.
451*7c478bd9Sstevel@tonic-gate 	 *
452*7c478bd9Sstevel@tonic-gate 	 * We need to use __I_PUSH_NOCTTY instead of I_PUSH here, otherwise
453*7c478bd9Sstevel@tonic-gate 	 * we'll end up having the slave device as *our* controling terminal.
454*7c478bd9Sstevel@tonic-gate 	 */
455*7c478bd9Sstevel@tonic-gate 	if (ioctl(slavefd, __I_PUSH_NOCTTY, "ptem") == -1) {
456*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to push ptem module");
457*7c478bd9Sstevel@tonic-gate 		goto error;
458*7c478bd9Sstevel@tonic-gate 	}
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 	/*
461*7c478bd9Sstevel@tonic-gate 	 * Anchor the stream to prevent malicious or accidental I_POP of ptem.
462*7c478bd9Sstevel@tonic-gate 	 */
463*7c478bd9Sstevel@tonic-gate 	if (ioctl(slavefd, I_ANCHOR) == -1) {
464*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to set stream anchor");
465*7c478bd9Sstevel@tonic-gate 		goto error;
466*7c478bd9Sstevel@tonic-gate 	}
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate 	if (ioctl(slavefd, __I_PUSH_NOCTTY, "ldterm") == -1) {
469*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to push ldterm module");
470*7c478bd9Sstevel@tonic-gate 		goto error;
471*7c478bd9Sstevel@tonic-gate 	}
472*7c478bd9Sstevel@tonic-gate 	if (ioctl(slavefd, __I_PUSH_NOCTTY, "ttcompat") == -1) {
473*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to push ttcompat module");
474*7c478bd9Sstevel@tonic-gate 		goto error;
475*7c478bd9Sstevel@tonic-gate 	}
476*7c478bd9Sstevel@tonic-gate 
477*7c478bd9Sstevel@tonic-gate 	/*
478*7c478bd9Sstevel@tonic-gate 	 * Setup default terminal settings
479*7c478bd9Sstevel@tonic-gate 	 */
480*7c478bd9Sstevel@tonic-gate 	if (tcsetattr(slavefd, TCSAFLUSH, &base_termios) == -1) {
481*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "failed to set base terminal settings");
482*7c478bd9Sstevel@tonic-gate 		goto error;
483*7c478bd9Sstevel@tonic-gate 	}
484*7c478bd9Sstevel@tonic-gate 
485*7c478bd9Sstevel@tonic-gate 	return (0);
486*7c478bd9Sstevel@tonic-gate error:
487*7c478bd9Sstevel@tonic-gate 	(void) close(slavefd);
488*7c478bd9Sstevel@tonic-gate 	slavefd = -1;
489*7c478bd9Sstevel@tonic-gate 	return (-1);
490*7c478bd9Sstevel@tonic-gate }
491*7c478bd9Sstevel@tonic-gate 
492*7c478bd9Sstevel@tonic-gate /*
493*7c478bd9Sstevel@tonic-gate  * init_console_slave() sets up the console slave device; the device node
494*7c478bd9Sstevel@tonic-gate  * itself has already been set up in the device tree; the primary job
495*7c478bd9Sstevel@tonic-gate  * here is to do some STREAMS plumbing (via prep_console_slave()) and then
496*7c478bd9Sstevel@tonic-gate  * to establish some symlinks.  Eventually we should move that functionality
497*7c478bd9Sstevel@tonic-gate  * into devfsadm.
498*7c478bd9Sstevel@tonic-gate  */
499*7c478bd9Sstevel@tonic-gate int
500*7c478bd9Sstevel@tonic-gate init_console_slave(zlog_t *zlogp)
501*7c478bd9Sstevel@tonic-gate {
502*7c478bd9Sstevel@tonic-gate 	char zonepath[MAXPATHLEN];
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate 	if (slavefd != -1)
505*7c478bd9Sstevel@tonic-gate 		return (0);
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zonepath, sizeof (zonepath)) != Z_OK) {
508*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "unable to determine zone root");
509*7c478bd9Sstevel@tonic-gate 		return (-1);
510*7c478bd9Sstevel@tonic-gate 	}
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate 	if (prep_console_slave(zlogp, zonepath) == -1) {
513*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE, "could not prep console slave");
514*7c478bd9Sstevel@tonic-gate 		return (-1);
515*7c478bd9Sstevel@tonic-gate 	}
516*7c478bd9Sstevel@tonic-gate 
517*7c478bd9Sstevel@tonic-gate 	return (0);
518*7c478bd9Sstevel@tonic-gate }
519*7c478bd9Sstevel@tonic-gate 
520*7c478bd9Sstevel@tonic-gate void
521*7c478bd9Sstevel@tonic-gate destroy_console_slave(void)
522*7c478bd9Sstevel@tonic-gate {
523*7c478bd9Sstevel@tonic-gate 	(void) close(slavefd);
524*7c478bd9Sstevel@tonic-gate 	slavefd = -1;
525*7c478bd9Sstevel@tonic-gate }
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate /*
528*7c478bd9Sstevel@tonic-gate  * Restore initial terminal attributes to the zone console.
529*7c478bd9Sstevel@tonic-gate  */
530*7c478bd9Sstevel@tonic-gate void
531*7c478bd9Sstevel@tonic-gate reset_slave_terminal(zlog_t *zlogp)
532*7c478bd9Sstevel@tonic-gate {
533*7c478bd9Sstevel@tonic-gate 	assert(slavefd != -1);
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate 	/* Probably not fatal, so we drive on if it fails */
536*7c478bd9Sstevel@tonic-gate 	if (tcsetattr(slavefd, TCSAFLUSH, &base_termios) == -1) {
537*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "WARNING: failed to set terminal "
538*7c478bd9Sstevel@tonic-gate 		    "settings.");
539*7c478bd9Sstevel@tonic-gate 	}
540*7c478bd9Sstevel@tonic-gate }
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate static int
543*7c478bd9Sstevel@tonic-gate init_console_sock(zlog_t *zlogp)
544*7c478bd9Sstevel@tonic-gate {
545*7c478bd9Sstevel@tonic-gate 	int servfd;
546*7c478bd9Sstevel@tonic-gate 	struct sockaddr_un servaddr;
547*7c478bd9Sstevel@tonic-gate 
548*7c478bd9Sstevel@tonic-gate 	bzero(&servaddr, sizeof (servaddr));
549*7c478bd9Sstevel@tonic-gate 	servaddr.sun_family = AF_UNIX;
550*7c478bd9Sstevel@tonic-gate 	(void) snprintf(servaddr.sun_path, sizeof (servaddr.sun_path),
551*7c478bd9Sstevel@tonic-gate 	    CONSOLE_SOCKPATH, zone_name);
552*7c478bd9Sstevel@tonic-gate 
553*7c478bd9Sstevel@tonic-gate 	if ((servfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
554*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE, "console setup: could not create socket");
555*7c478bd9Sstevel@tonic-gate 		return (-1);
556*7c478bd9Sstevel@tonic-gate 	}
557*7c478bd9Sstevel@tonic-gate 	(void) unlink(servaddr.sun_path);
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate 	if (bind(servfd, (struct sockaddr *)&servaddr,
560*7c478bd9Sstevel@tonic-gate 	    sizeof (servaddr)) == -1) {
561*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
562*7c478bd9Sstevel@tonic-gate 		    "console setup: could not bind to socket");
563*7c478bd9Sstevel@tonic-gate 		goto out;
564*7c478bd9Sstevel@tonic-gate 	}
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate 	if (listen(servfd, 4) == -1) {
567*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_TRUE,
568*7c478bd9Sstevel@tonic-gate 		    "console setup: could not listen on socket");
569*7c478bd9Sstevel@tonic-gate 		goto out;
570*7c478bd9Sstevel@tonic-gate 	}
571*7c478bd9Sstevel@tonic-gate 	return (servfd);
572*7c478bd9Sstevel@tonic-gate 
573*7c478bd9Sstevel@tonic-gate out:
574*7c478bd9Sstevel@tonic-gate 	(void) unlink(servaddr.sun_path);
575*7c478bd9Sstevel@tonic-gate 	(void) close(servfd);
576*7c478bd9Sstevel@tonic-gate 	return (-1);
577*7c478bd9Sstevel@tonic-gate }
578*7c478bd9Sstevel@tonic-gate 
579*7c478bd9Sstevel@tonic-gate static void
580*7c478bd9Sstevel@tonic-gate destroy_console_sock(int servfd)
581*7c478bd9Sstevel@tonic-gate {
582*7c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), CONSOLE_SOCKPATH, zone_name);
585*7c478bd9Sstevel@tonic-gate 	(void) unlink(path);
586*7c478bd9Sstevel@tonic-gate 	(void) shutdown(servfd, SHUT_RDWR);
587*7c478bd9Sstevel@tonic-gate 	(void) close(servfd);
588*7c478bd9Sstevel@tonic-gate }
589*7c478bd9Sstevel@tonic-gate 
590*7c478bd9Sstevel@tonic-gate /*
591*7c478bd9Sstevel@tonic-gate  * Read the "ident" string from the client's descriptor; this routine also
592*7c478bd9Sstevel@tonic-gate  * tolerates being called with pid=NULL, for times when you want to "eat"
593*7c478bd9Sstevel@tonic-gate  * the ident string from a client without saving it.
594*7c478bd9Sstevel@tonic-gate  */
595*7c478bd9Sstevel@tonic-gate static int
596*7c478bd9Sstevel@tonic-gate get_client_ident(int clifd, pid_t *pid, char *locale, size_t locale_len)
597*7c478bd9Sstevel@tonic-gate {
598*7c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ], *bufp;
599*7c478bd9Sstevel@tonic-gate 	size_t buflen = sizeof (buf);
600*7c478bd9Sstevel@tonic-gate 	char c = '\0';
601*7c478bd9Sstevel@tonic-gate 	int i = 0, r;
602*7c478bd9Sstevel@tonic-gate 
603*7c478bd9Sstevel@tonic-gate 	/* "eat up the ident string" case, for simplicity */
604*7c478bd9Sstevel@tonic-gate 	if (pid == NULL) {
605*7c478bd9Sstevel@tonic-gate 		assert(locale == NULL && locale_len == 0);
606*7c478bd9Sstevel@tonic-gate 		while (read(clifd, &c, 1) == 1) {
607*7c478bd9Sstevel@tonic-gate 			if (c == '\n')
608*7c478bd9Sstevel@tonic-gate 				return (0);
609*7c478bd9Sstevel@tonic-gate 		}
610*7c478bd9Sstevel@tonic-gate 	}
611*7c478bd9Sstevel@tonic-gate 
612*7c478bd9Sstevel@tonic-gate 	bzero(buf, sizeof (buf));
613*7c478bd9Sstevel@tonic-gate 	while ((buflen > 1) && (r = read(clifd, &c, 1)) == 1) {
614*7c478bd9Sstevel@tonic-gate 		buflen--;
615*7c478bd9Sstevel@tonic-gate 		if (c == '\n')
616*7c478bd9Sstevel@tonic-gate 			break;
617*7c478bd9Sstevel@tonic-gate 
618*7c478bd9Sstevel@tonic-gate 		buf[i] = c;
619*7c478bd9Sstevel@tonic-gate 		i++;
620*7c478bd9Sstevel@tonic-gate 	}
621*7c478bd9Sstevel@tonic-gate 	if (r == -1)
622*7c478bd9Sstevel@tonic-gate 		return (-1);
623*7c478bd9Sstevel@tonic-gate 
624*7c478bd9Sstevel@tonic-gate 	/*
625*7c478bd9Sstevel@tonic-gate 	 * We've filled the buffer, but still haven't seen \n.  Keep eating
626*7c478bd9Sstevel@tonic-gate 	 * until we find it; we don't expect this to happen, but this is
627*7c478bd9Sstevel@tonic-gate 	 * defensive.
628*7c478bd9Sstevel@tonic-gate 	 */
629*7c478bd9Sstevel@tonic-gate 	if (c != '\n') {
630*7c478bd9Sstevel@tonic-gate 		while ((r = read(clifd, &c, sizeof (c))) > 0)
631*7c478bd9Sstevel@tonic-gate 			if (c == '\n')
632*7c478bd9Sstevel@tonic-gate 				break;
633*7c478bd9Sstevel@tonic-gate 	}
634*7c478bd9Sstevel@tonic-gate 
635*7c478bd9Sstevel@tonic-gate 	/*
636*7c478bd9Sstevel@tonic-gate 	 * Parse buffer for message of the form: IDENT <pid> <locale>
637*7c478bd9Sstevel@tonic-gate 	 */
638*7c478bd9Sstevel@tonic-gate 	bufp = buf;
639*7c478bd9Sstevel@tonic-gate 	if (strncmp(bufp, "IDENT ", 6) != 0)
640*7c478bd9Sstevel@tonic-gate 		return (-1);
641*7c478bd9Sstevel@tonic-gate 	bufp += 6;
642*7c478bd9Sstevel@tonic-gate 	errno = 0;
643*7c478bd9Sstevel@tonic-gate 	*pid = strtoll(bufp, &bufp, 10);
644*7c478bd9Sstevel@tonic-gate 	if (errno != 0)
645*7c478bd9Sstevel@tonic-gate 		return (-1);
646*7c478bd9Sstevel@tonic-gate 
647*7c478bd9Sstevel@tonic-gate 	while (*bufp != '\0' && isspace(*bufp))
648*7c478bd9Sstevel@tonic-gate 		bufp++;
649*7c478bd9Sstevel@tonic-gate 	(void) strlcpy(locale, bufp, locale_len);
650*7c478bd9Sstevel@tonic-gate 
651*7c478bd9Sstevel@tonic-gate 	return (0);
652*7c478bd9Sstevel@tonic-gate }
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate static int
655*7c478bd9Sstevel@tonic-gate accept_client(int servfd, pid_t *pid, char *locale, size_t locale_len)
656*7c478bd9Sstevel@tonic-gate {
657*7c478bd9Sstevel@tonic-gate 	int connfd;
658*7c478bd9Sstevel@tonic-gate 	struct sockaddr_un cliaddr;
659*7c478bd9Sstevel@tonic-gate 	socklen_t clilen;
660*7c478bd9Sstevel@tonic-gate 
661*7c478bd9Sstevel@tonic-gate 	clilen = sizeof (cliaddr);
662*7c478bd9Sstevel@tonic-gate 	connfd = accept(servfd, (struct sockaddr *)&cliaddr, &clilen);
663*7c478bd9Sstevel@tonic-gate 	if (connfd == -1)
664*7c478bd9Sstevel@tonic-gate 		return (-1);
665*7c478bd9Sstevel@tonic-gate 	if (get_client_ident(connfd, pid, locale, locale_len) == -1) {
666*7c478bd9Sstevel@tonic-gate 		(void) shutdown(connfd, SHUT_RDWR);
667*7c478bd9Sstevel@tonic-gate 		(void) close(connfd);
668*7c478bd9Sstevel@tonic-gate 		return (-1);
669*7c478bd9Sstevel@tonic-gate 	}
670*7c478bd9Sstevel@tonic-gate 	(void) write(connfd, "OK\n", 3);
671*7c478bd9Sstevel@tonic-gate 	return (connfd);
672*7c478bd9Sstevel@tonic-gate }
673*7c478bd9Sstevel@tonic-gate 
674*7c478bd9Sstevel@tonic-gate static void
675*7c478bd9Sstevel@tonic-gate reject_client(int servfd, pid_t clientpid)
676*7c478bd9Sstevel@tonic-gate {
677*7c478bd9Sstevel@tonic-gate 	int connfd;
678*7c478bd9Sstevel@tonic-gate 	struct sockaddr_un cliaddr;
679*7c478bd9Sstevel@tonic-gate 	socklen_t clilen;
680*7c478bd9Sstevel@tonic-gate 	char nak[MAXPATHLEN];
681*7c478bd9Sstevel@tonic-gate 
682*7c478bd9Sstevel@tonic-gate 	clilen = sizeof (cliaddr);
683*7c478bd9Sstevel@tonic-gate 	connfd = accept(servfd, (struct sockaddr *)&cliaddr, &clilen);
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate 	/*
686*7c478bd9Sstevel@tonic-gate 	 * After hear its ident string, tell client to get lost.
687*7c478bd9Sstevel@tonic-gate 	 */
688*7c478bd9Sstevel@tonic-gate 	if (get_client_ident(connfd, NULL, NULL, 0) == 0) {
689*7c478bd9Sstevel@tonic-gate 		(void) snprintf(nak, sizeof (nak), "%lu\n",
690*7c478bd9Sstevel@tonic-gate 		    clientpid);
691*7c478bd9Sstevel@tonic-gate 		(void) write(connfd, nak, strlen(nak));
692*7c478bd9Sstevel@tonic-gate 	}
693*7c478bd9Sstevel@tonic-gate 	(void) shutdown(connfd, SHUT_RDWR);
694*7c478bd9Sstevel@tonic-gate 	(void) close(connfd);
695*7c478bd9Sstevel@tonic-gate }
696*7c478bd9Sstevel@tonic-gate 
697*7c478bd9Sstevel@tonic-gate static void
698*7c478bd9Sstevel@tonic-gate event_message(int clifd, char *clilocale, zone_evt_t evt)
699*7c478bd9Sstevel@tonic-gate {
700*7c478bd9Sstevel@tonic-gate 	char msg[BUFSIZ];
701*7c478bd9Sstevel@tonic-gate 	char *str;
702*7c478bd9Sstevel@tonic-gate 	const char *fmt;
703*7c478bd9Sstevel@tonic-gate 
704*7c478bd9Sstevel@tonic-gate 	if (clifd == -1)
705*7c478bd9Sstevel@tonic-gate 		return;
706*7c478bd9Sstevel@tonic-gate 
707*7c478bd9Sstevel@tonic-gate 	switch (evt) {
708*7c478bd9Sstevel@tonic-gate 	case Z_EVT_ZONE_BOOTING:
709*7c478bd9Sstevel@tonic-gate 		str = "Zone booting up";
710*7c478bd9Sstevel@tonic-gate 		break;
711*7c478bd9Sstevel@tonic-gate 	case Z_EVT_ZONE_READIED:
712*7c478bd9Sstevel@tonic-gate 		str = "Zone readied";
713*7c478bd9Sstevel@tonic-gate 		break;
714*7c478bd9Sstevel@tonic-gate 	case Z_EVT_ZONE_HALTED:
715*7c478bd9Sstevel@tonic-gate 		str = "Zone halted";
716*7c478bd9Sstevel@tonic-gate 		break;
717*7c478bd9Sstevel@tonic-gate 	case Z_EVT_ZONE_REBOOTING:
718*7c478bd9Sstevel@tonic-gate 		str = "Zone rebooting";
719*7c478bd9Sstevel@tonic-gate 		break;
720*7c478bd9Sstevel@tonic-gate 	case Z_EVT_ZONE_UNINSTALLING:
721*7c478bd9Sstevel@tonic-gate 		str = "Zone is being uninstalled.  Disconnecting...";
722*7c478bd9Sstevel@tonic-gate 		break;
723*7c478bd9Sstevel@tonic-gate 	default:
724*7c478bd9Sstevel@tonic-gate 		return;
725*7c478bd9Sstevel@tonic-gate 	}
726*7c478bd9Sstevel@tonic-gate 	str = localize_msg(clilocale, str);
727*7c478bd9Sstevel@tonic-gate 	fmt = localize_msg(clilocale, "\r\n[NOTICE: %s]\r\n");
728*7c478bd9Sstevel@tonic-gate 	(void) snprintf(msg, sizeof (msg), fmt, str);
729*7c478bd9Sstevel@tonic-gate 	(void) write(clifd, msg, strlen(msg));
730*7c478bd9Sstevel@tonic-gate }
731*7c478bd9Sstevel@tonic-gate 
732*7c478bd9Sstevel@tonic-gate /*
733*7c478bd9Sstevel@tonic-gate  * Check to see if the client at the other end of the socket is still
734*7c478bd9Sstevel@tonic-gate  * alive; we know it is not if it throws EPIPE at us when we try to write
735*7c478bd9Sstevel@tonic-gate  * an otherwise harmless 0-length message to it.
736*7c478bd9Sstevel@tonic-gate  */
737*7c478bd9Sstevel@tonic-gate static int
738*7c478bd9Sstevel@tonic-gate test_client(int clifd)
739*7c478bd9Sstevel@tonic-gate {
740*7c478bd9Sstevel@tonic-gate 	if ((write(clifd, "", 0) == -1) && errno == EPIPE)
741*7c478bd9Sstevel@tonic-gate 		return (-1);
742*7c478bd9Sstevel@tonic-gate 	return (0);
743*7c478bd9Sstevel@tonic-gate }
744*7c478bd9Sstevel@tonic-gate 
745*7c478bd9Sstevel@tonic-gate /*
746*7c478bd9Sstevel@tonic-gate  * This routine drives the console I/O loop.  It polls for input from the
747*7c478bd9Sstevel@tonic-gate  * master side of the console (output to the console), and from the client
748*7c478bd9Sstevel@tonic-gate  * (input from the console user).  Additionally, it polls on the server fd,
749*7c478bd9Sstevel@tonic-gate  * and disconnects any clients that might try to hook up with the zone while
750*7c478bd9Sstevel@tonic-gate  * the console is in use.
751*7c478bd9Sstevel@tonic-gate  *
752*7c478bd9Sstevel@tonic-gate  * When the client first calls us up, it is expected to send a line giving
753*7c478bd9Sstevel@tonic-gate  * its "identity"; this consists of the string 'IDENT <pid> <locale>'.
754*7c478bd9Sstevel@tonic-gate  * This is so that we can report that the console is busy along with
755*7c478bd9Sstevel@tonic-gate  * some diagnostics about who has it busy; the locale is used so that
756*7c478bd9Sstevel@tonic-gate  * asynchronous messages about zone state (like the NOTICE: zone halted
757*7c478bd9Sstevel@tonic-gate  * messages) can be output in the user's locale.
758*7c478bd9Sstevel@tonic-gate  */
759*7c478bd9Sstevel@tonic-gate static void
760*7c478bd9Sstevel@tonic-gate do_console_io(zlog_t *zlogp, int consfd, int servfd)
761*7c478bd9Sstevel@tonic-gate {
762*7c478bd9Sstevel@tonic-gate 	struct pollfd pollfds[4];
763*7c478bd9Sstevel@tonic-gate 	char ibuf[BUFSIZ];
764*7c478bd9Sstevel@tonic-gate 	int cc, ret;
765*7c478bd9Sstevel@tonic-gate 	int clifd = -1;
766*7c478bd9Sstevel@tonic-gate 	int pollerr = 0;
767*7c478bd9Sstevel@tonic-gate 	char clilocale[MAXPATHLEN];
768*7c478bd9Sstevel@tonic-gate 	pid_t clipid = 0;
769*7c478bd9Sstevel@tonic-gate 
770*7c478bd9Sstevel@tonic-gate 	/* console side, watch for read events */
771*7c478bd9Sstevel@tonic-gate 	pollfds[0].fd = consfd;
772*7c478bd9Sstevel@tonic-gate 	pollfds[0].events = POLLIN | POLLRDNORM | POLLRDBAND |
773*7c478bd9Sstevel@tonic-gate 	    POLLPRI | POLLERR | POLLHUP | POLLNVAL;
774*7c478bd9Sstevel@tonic-gate 
775*7c478bd9Sstevel@tonic-gate 	/* client side, watch for read events */
776*7c478bd9Sstevel@tonic-gate 	pollfds[1].fd = clifd;
777*7c478bd9Sstevel@tonic-gate 	pollfds[1].events = pollfds[0].events;
778*7c478bd9Sstevel@tonic-gate 
779*7c478bd9Sstevel@tonic-gate 	/* the server socket; watch for events (new connections) */
780*7c478bd9Sstevel@tonic-gate 	pollfds[2].fd = servfd;
781*7c478bd9Sstevel@tonic-gate 	pollfds[2].events = pollfds[0].events;
782*7c478bd9Sstevel@tonic-gate 
783*7c478bd9Sstevel@tonic-gate 	/* the eventstram; watch for events (e.g.: zone halted) */
784*7c478bd9Sstevel@tonic-gate 	pollfds[3].fd = eventstream[1];
785*7c478bd9Sstevel@tonic-gate 	pollfds[3].events = pollfds[0].events;
786*7c478bd9Sstevel@tonic-gate 
787*7c478bd9Sstevel@tonic-gate 	for (;;) {
788*7c478bd9Sstevel@tonic-gate 		pollfds[0].revents = pollfds[1].revents = 0;
789*7c478bd9Sstevel@tonic-gate 		pollfds[2].revents = pollfds[3].revents = 0;
790*7c478bd9Sstevel@tonic-gate 
791*7c478bd9Sstevel@tonic-gate 		ret = poll(pollfds,
792*7c478bd9Sstevel@tonic-gate 		    sizeof (pollfds) / sizeof (struct pollfd), -1);
793*7c478bd9Sstevel@tonic-gate 		if (ret == -1 && errno != EINTR) {
794*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "poll failed");
795*7c478bd9Sstevel@tonic-gate 			/* we are hosed, close connection */
796*7c478bd9Sstevel@tonic-gate 			break;
797*7c478bd9Sstevel@tonic-gate 		}
798*7c478bd9Sstevel@tonic-gate 
799*7c478bd9Sstevel@tonic-gate 		/* event from console side */
800*7c478bd9Sstevel@tonic-gate 		if (pollfds[0].revents) {
801*7c478bd9Sstevel@tonic-gate 			if (pollfds[0].revents &
802*7c478bd9Sstevel@tonic-gate 			    (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
803*7c478bd9Sstevel@tonic-gate 				errno = 0;
804*7c478bd9Sstevel@tonic-gate 				cc = read(consfd, ibuf, BUFSIZ);
805*7c478bd9Sstevel@tonic-gate 				if (cc <= 0 && (errno != EINTR) &&
806*7c478bd9Sstevel@tonic-gate 				    (errno != EAGAIN))
807*7c478bd9Sstevel@tonic-gate 					break;
808*7c478bd9Sstevel@tonic-gate 				/*
809*7c478bd9Sstevel@tonic-gate 				 * Lose I/O if no one is listening
810*7c478bd9Sstevel@tonic-gate 				 */
811*7c478bd9Sstevel@tonic-gate 				if (clifd != -1 && cc > 0)
812*7c478bd9Sstevel@tonic-gate 					(void) write(clifd, ibuf, cc);
813*7c478bd9Sstevel@tonic-gate 			} else {
814*7c478bd9Sstevel@tonic-gate 				pollerr = pollfds[0].revents;
815*7c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
816*7c478bd9Sstevel@tonic-gate 				    "closing connection with (console) "
817*7c478bd9Sstevel@tonic-gate 				    "pollerr %d\n", pollerr);
818*7c478bd9Sstevel@tonic-gate 				break;
819*7c478bd9Sstevel@tonic-gate 			}
820*7c478bd9Sstevel@tonic-gate 		}
821*7c478bd9Sstevel@tonic-gate 
822*7c478bd9Sstevel@tonic-gate 		/* event from client side */
823*7c478bd9Sstevel@tonic-gate 		if (pollfds[1].revents) {
824*7c478bd9Sstevel@tonic-gate 			if (pollfds[1].revents &
825*7c478bd9Sstevel@tonic-gate 			    (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) {
826*7c478bd9Sstevel@tonic-gate 				errno = 0;
827*7c478bd9Sstevel@tonic-gate 				cc = read(clifd, ibuf, BUFSIZ);
828*7c478bd9Sstevel@tonic-gate 				if (cc <= 0 && (errno != EINTR) &&
829*7c478bd9Sstevel@tonic-gate 				    (errno != EAGAIN))
830*7c478bd9Sstevel@tonic-gate 					break;
831*7c478bd9Sstevel@tonic-gate 				(void) write(consfd, ibuf, cc);
832*7c478bd9Sstevel@tonic-gate 			} else {
833*7c478bd9Sstevel@tonic-gate 				pollerr = pollfds[1].revents;
834*7c478bd9Sstevel@tonic-gate 				zerror(zlogp, B_FALSE,
835*7c478bd9Sstevel@tonic-gate 				    "closing connection with (client) "
836*7c478bd9Sstevel@tonic-gate 				    "pollerr %d\n", pollerr);
837*7c478bd9Sstevel@tonic-gate 				break;
838*7c478bd9Sstevel@tonic-gate 			}
839*7c478bd9Sstevel@tonic-gate 		}
840*7c478bd9Sstevel@tonic-gate 
841*7c478bd9Sstevel@tonic-gate 		/* event from server socket */
842*7c478bd9Sstevel@tonic-gate 		if (pollfds[2].revents &&
843*7c478bd9Sstevel@tonic-gate 		    (pollfds[2].revents & (POLLIN | POLLRDNORM))) {
844*7c478bd9Sstevel@tonic-gate 			if (clifd != -1) {
845*7c478bd9Sstevel@tonic-gate 				/*
846*7c478bd9Sstevel@tonic-gate 				 * Test the client to see if it is really
847*7c478bd9Sstevel@tonic-gate 				 * still alive.  If it has died but we
848*7c478bd9Sstevel@tonic-gate 				 * haven't yet detected that, we might
849*7c478bd9Sstevel@tonic-gate 				 * deny a legitimate connect attempt.  If it
850*7c478bd9Sstevel@tonic-gate 				 * is dead, we break out; once we tear down
851*7c478bd9Sstevel@tonic-gate 				 * the old connection, the new connection
852*7c478bd9Sstevel@tonic-gate 				 * will happen.
853*7c478bd9Sstevel@tonic-gate 				 */
854*7c478bd9Sstevel@tonic-gate 				if (test_client(clifd) == -1) {
855*7c478bd9Sstevel@tonic-gate 					break;
856*7c478bd9Sstevel@tonic-gate 				}
857*7c478bd9Sstevel@tonic-gate 				/* we're already handling a client */
858*7c478bd9Sstevel@tonic-gate 				reject_client(servfd, clipid);
859*7c478bd9Sstevel@tonic-gate 
860*7c478bd9Sstevel@tonic-gate 
861*7c478bd9Sstevel@tonic-gate 			} else if ((clifd = accept_client(servfd, &clipid,
862*7c478bd9Sstevel@tonic-gate 			    clilocale, sizeof (clilocale))) != -1) {
863*7c478bd9Sstevel@tonic-gate 				pollfds[1].fd = clifd;
864*7c478bd9Sstevel@tonic-gate 
865*7c478bd9Sstevel@tonic-gate 			} else {
866*7c478bd9Sstevel@tonic-gate 				break;
867*7c478bd9Sstevel@tonic-gate 			}
868*7c478bd9Sstevel@tonic-gate 		}
869*7c478bd9Sstevel@tonic-gate 
870*7c478bd9Sstevel@tonic-gate 		/*
871*7c478bd9Sstevel@tonic-gate 		 * Watch for events on the eventstream.  This is how we get
872*7c478bd9Sstevel@tonic-gate 		 * notified of the zone halting, etc.  It provides us a
873*7c478bd9Sstevel@tonic-gate 		 * "wakeup" from poll when important things happen, which
874*7c478bd9Sstevel@tonic-gate 		 * is good.
875*7c478bd9Sstevel@tonic-gate 		 */
876*7c478bd9Sstevel@tonic-gate 		if (pollfds[3].revents) {
877*7c478bd9Sstevel@tonic-gate 			int evt = eventstream_read();
878*7c478bd9Sstevel@tonic-gate 			/*
879*7c478bd9Sstevel@tonic-gate 			 * After we drain out the event, if we aren't servicing
880*7c478bd9Sstevel@tonic-gate 			 * a console client, we hop back out to our caller,
881*7c478bd9Sstevel@tonic-gate 			 * which will check to see if it is time to shutdown
882*7c478bd9Sstevel@tonic-gate 			 * the daemon, or if we should take another console
883*7c478bd9Sstevel@tonic-gate 			 * service lap.
884*7c478bd9Sstevel@tonic-gate 			 */
885*7c478bd9Sstevel@tonic-gate 			if (clifd == -1) {
886*7c478bd9Sstevel@tonic-gate 				break;
887*7c478bd9Sstevel@tonic-gate 			}
888*7c478bd9Sstevel@tonic-gate 			event_message(clifd, clilocale, evt);
889*7c478bd9Sstevel@tonic-gate 			/*
890*7c478bd9Sstevel@tonic-gate 			 * Special handling for the message that the zone is
891*7c478bd9Sstevel@tonic-gate 			 * uninstalling; we boot the client, then break out
892*7c478bd9Sstevel@tonic-gate 			 * of this function.  When we return to the
893*7c478bd9Sstevel@tonic-gate 			 * serve_console loop, we will see that the zone is
894*7c478bd9Sstevel@tonic-gate 			 * in a state < READY, and so zoneadmd will shutdown.
895*7c478bd9Sstevel@tonic-gate 			 */
896*7c478bd9Sstevel@tonic-gate 			if (evt == Z_EVT_ZONE_UNINSTALLING) {
897*7c478bd9Sstevel@tonic-gate 				break;
898*7c478bd9Sstevel@tonic-gate 			}
899*7c478bd9Sstevel@tonic-gate 		}
900*7c478bd9Sstevel@tonic-gate 
901*7c478bd9Sstevel@tonic-gate 	}
902*7c478bd9Sstevel@tonic-gate 
903*7c478bd9Sstevel@tonic-gate 	if (clifd != -1) {
904*7c478bd9Sstevel@tonic-gate 		(void) shutdown(clifd, SHUT_RDWR);
905*7c478bd9Sstevel@tonic-gate 		(void) close(clifd);
906*7c478bd9Sstevel@tonic-gate 	}
907*7c478bd9Sstevel@tonic-gate }
908*7c478bd9Sstevel@tonic-gate 
909*7c478bd9Sstevel@tonic-gate int
910*7c478bd9Sstevel@tonic-gate init_console(zlog_t *zlogp)
911*7c478bd9Sstevel@tonic-gate {
912*7c478bd9Sstevel@tonic-gate 	if (init_console_dev(zlogp) == -1) {
913*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
914*7c478bd9Sstevel@tonic-gate 		    "console setup: device initialization failed");
915*7c478bd9Sstevel@tonic-gate 		return (-1);
916*7c478bd9Sstevel@tonic-gate 	}
917*7c478bd9Sstevel@tonic-gate 
918*7c478bd9Sstevel@tonic-gate 	if ((serverfd = init_console_sock(zlogp)) == -1) {
919*7c478bd9Sstevel@tonic-gate 		zerror(zlogp, B_FALSE,
920*7c478bd9Sstevel@tonic-gate 		    "console setup: socket initialization failed");
921*7c478bd9Sstevel@tonic-gate 		return (-1);
922*7c478bd9Sstevel@tonic-gate 	}
923*7c478bd9Sstevel@tonic-gate 	return (0);
924*7c478bd9Sstevel@tonic-gate }
925*7c478bd9Sstevel@tonic-gate 
926*7c478bd9Sstevel@tonic-gate /*
927*7c478bd9Sstevel@tonic-gate  * serve_console() is the master loop for driving console I/O.  It is also the
928*7c478bd9Sstevel@tonic-gate  * routine which is ultimately responsible for "pulling the plug" on zoneadmd
929*7c478bd9Sstevel@tonic-gate  * when it realizes that the daemon should shut down.
930*7c478bd9Sstevel@tonic-gate  *
931*7c478bd9Sstevel@tonic-gate  * The rules for shutdown are: there must be no console client, and the zone
932*7c478bd9Sstevel@tonic-gate  * state must be < ready.  However, we need to give things a chance to actually
933*7c478bd9Sstevel@tonic-gate  * get going when the daemon starts up-- otherwise the daemon would immediately
934*7c478bd9Sstevel@tonic-gate  * exit on startup if the zone was in the installed state, so we first drop
935*7c478bd9Sstevel@tonic-gate  * into the do_console_io() loop in order to give *something* a chance to
936*7c478bd9Sstevel@tonic-gate  * happen.
937*7c478bd9Sstevel@tonic-gate  */
938*7c478bd9Sstevel@tonic-gate void
939*7c478bd9Sstevel@tonic-gate serve_console(zlog_t *zlogp)
940*7c478bd9Sstevel@tonic-gate {
941*7c478bd9Sstevel@tonic-gate 	int masterfd;
942*7c478bd9Sstevel@tonic-gate 	zone_state_t zstate;
943*7c478bd9Sstevel@tonic-gate 	char conspath[MAXPATHLEN];
944*7c478bd9Sstevel@tonic-gate 
945*7c478bd9Sstevel@tonic-gate 	(void) snprintf(conspath, sizeof (conspath),
946*7c478bd9Sstevel@tonic-gate 	    "/dev/zcons/%s/%s", zone_name, ZCONS_MASTER_NAME);
947*7c478bd9Sstevel@tonic-gate 
948*7c478bd9Sstevel@tonic-gate 	for (;;) {
949*7c478bd9Sstevel@tonic-gate 		masterfd = open(conspath, O_RDWR|O_NONBLOCK|O_NOCTTY);
950*7c478bd9Sstevel@tonic-gate 		if (masterfd == -1) {
951*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "failed to open console master");
952*7c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&lock);
953*7c478bd9Sstevel@tonic-gate 			goto death;
954*7c478bd9Sstevel@tonic-gate 		}
955*7c478bd9Sstevel@tonic-gate 
956*7c478bd9Sstevel@tonic-gate 		/*
957*7c478bd9Sstevel@tonic-gate 		 * Setting RPROTDIS on the stream means that the control
958*7c478bd9Sstevel@tonic-gate 		 * portion of messages received (which we don't care about)
959*7c478bd9Sstevel@tonic-gate 		 * will be discarded by the stream head.  If we allowed such
960*7c478bd9Sstevel@tonic-gate 		 * messages, we wouldn't be able to use read(2), as it fails
961*7c478bd9Sstevel@tonic-gate 		 * (EBADMSG) when a message with a control element is received.
962*7c478bd9Sstevel@tonic-gate 		 */
963*7c478bd9Sstevel@tonic-gate 		if (ioctl(masterfd, I_SRDOPT, RNORM|RPROTDIS) == -1) {
964*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_TRUE, "failed to set options on "
965*7c478bd9Sstevel@tonic-gate 			    "console master");
966*7c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&lock);
967*7c478bd9Sstevel@tonic-gate 			goto death;
968*7c478bd9Sstevel@tonic-gate 		}
969*7c478bd9Sstevel@tonic-gate 
970*7c478bd9Sstevel@tonic-gate 		do_console_io(zlogp, masterfd, serverfd);
971*7c478bd9Sstevel@tonic-gate 
972*7c478bd9Sstevel@tonic-gate 		/*
973*7c478bd9Sstevel@tonic-gate 		 * We would prefer not to do this, but hostile zone processes
974*7c478bd9Sstevel@tonic-gate 		 * can cause the stream to become tainted, and reads will
975*7c478bd9Sstevel@tonic-gate 		 * fail.  So, in case something has gone seriously ill,
976*7c478bd9Sstevel@tonic-gate 		 * we dismantle the stream and reopen the console when we
977*7c478bd9Sstevel@tonic-gate 		 * take another lap.
978*7c478bd9Sstevel@tonic-gate 		 */
979*7c478bd9Sstevel@tonic-gate 		(void) close(masterfd);
980*7c478bd9Sstevel@tonic-gate 
981*7c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&lock);
982*7c478bd9Sstevel@tonic-gate 		/*
983*7c478bd9Sstevel@tonic-gate 		 * We need to set death_throes (see below) atomically with
984*7c478bd9Sstevel@tonic-gate 		 * respect to noticing that (a) we have no console client and
985*7c478bd9Sstevel@tonic-gate 		 * (b) the zone is not installed.  Otherwise we could get a
986*7c478bd9Sstevel@tonic-gate 		 * request to boot during this time.  Once we set death_throes,
987*7c478bd9Sstevel@tonic-gate 		 * any incoming door stuff will be turned away.
988*7c478bd9Sstevel@tonic-gate 		 */
989*7c478bd9Sstevel@tonic-gate 		if (zone_get_state(zone_name, &zstate) == Z_OK) {
990*7c478bd9Sstevel@tonic-gate 			if (zstate < ZONE_STATE_READY)
991*7c478bd9Sstevel@tonic-gate 				goto death;
992*7c478bd9Sstevel@tonic-gate 		} else {
993*7c478bd9Sstevel@tonic-gate 			zerror(zlogp, B_FALSE,
994*7c478bd9Sstevel@tonic-gate 			    "unable to determine state of zone");
995*7c478bd9Sstevel@tonic-gate 			goto death;
996*7c478bd9Sstevel@tonic-gate 		}
997*7c478bd9Sstevel@tonic-gate 		/*
998*7c478bd9Sstevel@tonic-gate 		 * Even if zone_get_state() fails, stay conservative, and
999*7c478bd9Sstevel@tonic-gate 		 * take another lap.
1000*7c478bd9Sstevel@tonic-gate 		 */
1001*7c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&lock);
1002*7c478bd9Sstevel@tonic-gate 	}
1003*7c478bd9Sstevel@tonic-gate 
1004*7c478bd9Sstevel@tonic-gate death:
1005*7c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&lock));
1006*7c478bd9Sstevel@tonic-gate 	in_death_throes = B_TRUE;
1007*7c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&lock);
1008*7c478bd9Sstevel@tonic-gate 
1009*7c478bd9Sstevel@tonic-gate 	destroy_console_sock(serverfd);
1010*7c478bd9Sstevel@tonic-gate 	(void) destroy_console_devs(zlogp);
1011*7c478bd9Sstevel@tonic-gate }
1012