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