xref: /titanic_44/usr/src/cmd/zoneadmd/zoneadmd.c (revision 8142c2b2a11acff39ec7e0576c566a751eb30ba6)
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 2007 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  * zoneadmd manages zones; one zoneadmd process is launched for each
31  * non-global zone on the system.  This daemon juggles four jobs:
32  *
33  * - Implement setup and teardown of the zone "virtual platform": mount and
34  *   unmount filesystems; create and destroy network interfaces; communicate
35  *   with devfsadmd to lay out devices for the zone; instantiate the zone
36  *   console device; configure process runtime attributes such as resource
37  *   controls, pool bindings, fine-grained privileges.
38  *
39  * - Launch the zone's init(1M) process.
40  *
41  * - Implement a door server; clients (like zoneadm) connect to the door
42  *   server and request zone state changes.  The kernel is also a client of
43  *   this door server.  A request to halt or reboot the zone which originates
44  *   *inside* the zone results in a door upcall from the kernel into zoneadmd.
45  *
46  *   One minor problem is that messages emitted by zoneadmd need to be passed
47  *   back to the zoneadm process making the request.  These messages need to
48  *   be rendered in the client's locale; so, this is passed in as part of the
49  *   request.  The exception is the kernel upcall to zoneadmd, in which case
50  *   messages are syslog'd.
51  *
52  *   To make all of this work, the Makefile adds -a to xgettext to extract *all*
53  *   strings, and an exclusion file (zoneadmd.xcl) is used to exclude those
54  *   strings which do not need to be translated.
55  *
56  * - Act as a console server for zlogin -C processes; see comments in zcons.c
57  *   for more information about the zone console architecture.
58  *
59  * DESIGN NOTES
60  *
61  * Restart:
62  *   A chief design constraint of zoneadmd is that it should be restartable in
63  *   the case that the administrator kills it off, or it suffers a fatal error,
64  *   without the running zone being impacted; this is akin to being able to
65  *   reboot the service processor of a server without affecting the OS instance.
66  */
67 
68 #include <sys/param.h>
69 #include <sys/mman.h>
70 #include <sys/types.h>
71 #include <sys/stat.h>
72 #include <sys/sysmacros.h>
73 
74 #include <bsm/adt.h>
75 #include <bsm/adt_event.h>
76 
77 #include <alloca.h>
78 #include <assert.h>
79 #include <errno.h>
80 #include <door.h>
81 #include <fcntl.h>
82 #include <locale.h>
83 #include <signal.h>
84 #include <stdarg.h>
85 #include <stdio.h>
86 #include <stdlib.h>
87 #include <string.h>
88 #include <strings.h>
89 #include <synch.h>
90 #include <syslog.h>
91 #include <thread.h>
92 #include <unistd.h>
93 #include <wait.h>
94 #include <limits.h>
95 #include <zone.h>
96 #include <libbrand.h>
97 #include <libcontract.h>
98 #include <libcontract_priv.h>
99 #include <sys/contract/process.h>
100 #include <sys/ctfs.h>
101 
102 #include <libzonecfg.h>
103 #include "zoneadmd.h"
104 
105 static char *progname;
106 char *zone_name;	/* zone which we are managing */
107 char brand_name[MAXNAMELEN];
108 boolean_t zone_isnative;
109 boolean_t zone_iscluster;
110 static zoneid_t zone_id;
111 
112 zlog_t logsys;
113 
114 mutex_t	lock = DEFAULTMUTEX;	/* to serialize stuff */
115 mutex_t	msglock = DEFAULTMUTEX;	/* for calling setlocale() */
116 
117 static sema_t scratch_sem;	/* for scratch zones */
118 
119 static char	zone_door_path[MAXPATHLEN];
120 static int	zone_door = -1;
121 
122 boolean_t in_death_throes = B_FALSE;	/* daemon is dying */
123 boolean_t bringup_failure_recovery = B_FALSE; /* ignore certain failures */
124 
125 #if !defined(TEXT_DOMAIN)		/* should be defined by cc -D */
126 #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
127 #endif
128 
129 #define	DEFAULT_LOCALE	"C"
130 
131 static const char *
132 z_cmd_name(zone_cmd_t zcmd)
133 {
134 	/* This list needs to match the enum in sys/zone.h */
135 	static const char *zcmdstr[] = {
136 		"ready", "boot", "forceboot", "reboot", "halt",
137 		"note_uninstalling", "mount", "forcemount", "unmount"
138 	};
139 
140 	if (zcmd >= sizeof (zcmdstr) / sizeof (*zcmdstr))
141 		return ("unknown");
142 	else
143 		return (zcmdstr[(int)zcmd]);
144 }
145 
146 static char *
147 get_execbasename(char *execfullname)
148 {
149 	char *last_slash, *execbasename;
150 
151 	/* guard against '/' at end of command invocation */
152 	for (;;) {
153 		last_slash = strrchr(execfullname, '/');
154 		if (last_slash == NULL) {
155 			execbasename = execfullname;
156 			break;
157 		} else {
158 			execbasename = last_slash + 1;
159 			if (*execbasename == '\0') {
160 				*last_slash = '\0';
161 				continue;
162 			}
163 			break;
164 		}
165 	}
166 	return (execbasename);
167 }
168 
169 static void
170 usage(void)
171 {
172 	(void) fprintf(stderr, gettext("Usage: %s -z zonename\n"), progname);
173 	(void) fprintf(stderr,
174 	    gettext("\tNote: %s should not be run directly.\n"), progname);
175 	exit(2);
176 }
177 
178 /* ARGSUSED */
179 static void
180 sigchld(int sig)
181 {
182 }
183 
184 char *
185 localize_msg(char *locale, const char *msg)
186 {
187 	char *out;
188 
189 	(void) mutex_lock(&msglock);
190 	(void) setlocale(LC_MESSAGES, locale);
191 	out = gettext(msg);
192 	(void) setlocale(LC_MESSAGES, DEFAULT_LOCALE);
193 	(void) mutex_unlock(&msglock);
194 	return (out);
195 }
196 
197 /* PRINTFLIKE3 */
198 void
199 zerror(zlog_t *zlogp, boolean_t use_strerror, const char *fmt, ...)
200 {
201 	va_list alist;
202 	char buf[MAXPATHLEN * 2]; /* enough space for err msg with a path */
203 	char *bp;
204 	int saved_errno = errno;
205 
206 	if (zlogp == NULL)
207 		return;
208 	if (zlogp == &logsys)
209 		(void) snprintf(buf, sizeof (buf), "[zone '%s'] ",
210 		    zone_name);
211 	else
212 		buf[0] = '\0';
213 	bp = &(buf[strlen(buf)]);
214 
215 	/*
216 	 * In theory, the locale pointer should be set to either "C" or a
217 	 * char array, so it should never be NULL
218 	 */
219 	assert(zlogp->locale != NULL);
220 	/* Locale is per process, but we are multi-threaded... */
221 	fmt = localize_msg(zlogp->locale, fmt);
222 
223 	va_start(alist, fmt);
224 	(void) vsnprintf(bp, sizeof (buf) - (bp - buf), fmt, alist);
225 	va_end(alist);
226 	bp = &(buf[strlen(buf)]);
227 	if (use_strerror)
228 		(void) snprintf(bp, sizeof (buf) - (bp - buf), ": %s",
229 		    strerror(saved_errno));
230 	if (zlogp == &logsys) {
231 		(void) syslog(LOG_ERR, "%s", buf);
232 	} else if (zlogp->logfile != NULL) {
233 		(void) fprintf(zlogp->logfile, "%s\n", buf);
234 	} else {
235 		size_t buflen;
236 		size_t copylen;
237 
238 		buflen = snprintf(zlogp->log, zlogp->loglen, "%s\n", buf);
239 		copylen = MIN(buflen, zlogp->loglen);
240 		zlogp->log += copylen;
241 		zlogp->loglen -= copylen;
242 	}
243 }
244 
245 /*
246  * Emit a warning for any boot arguments which are unrecognized.  Since
247  * Solaris boot arguments are getopt(3c) compatible (see kernel(1m)), we
248  * put the arguments into an argv style array, use getopt to process them,
249  * and put the resultant argument string back into outargs.
250  *
251  * During the filtering, we pull out any arguments which are truly "boot"
252  * arguments, leaving only those which are to be passed intact to the
253  * progenitor process.  The one we support at the moment is -i, which
254  * indicates to the kernel which program should be launched as 'init'.
255  *
256  * A return of Z_INVAL indicates specifically that the arguments are
257  * not valid; this is a non-fatal error.  Except for Z_OK, all other return
258  * values are treated as fatal.
259  */
260 static int
261 filter_bootargs(zlog_t *zlogp, const char *inargs, char *outargs,
262     char *init_file, char *badarg)
263 {
264 	int argc = 0, argc_save;
265 	int i;
266 	int err;
267 	char *arg, *lasts, **argv = NULL, **argv_save;
268 	char zonecfg_args[BOOTARGS_MAX];
269 	char scratchargs[BOOTARGS_MAX], *sargs;
270 	char c;
271 
272 	bzero(outargs, BOOTARGS_MAX);
273 	bzero(badarg, BOOTARGS_MAX);
274 
275 	/*
276 	 * If the user didn't specify transient boot arguments, check
277 	 * to see if there were any specified in the zone configuration,
278 	 * and use them if applicable.
279 	 */
280 	if (inargs == NULL || inargs[0] == '\0')  {
281 		zone_dochandle_t handle;
282 		if ((handle = zonecfg_init_handle()) == NULL) {
283 			zerror(zlogp, B_TRUE,
284 			    "getting zone configuration handle");
285 			return (Z_BAD_HANDLE);
286 		}
287 		err = zonecfg_get_snapshot_handle(zone_name, handle);
288 		if (err != Z_OK) {
289 			zerror(zlogp, B_FALSE,
290 			    "invalid configuration snapshot");
291 			zonecfg_fini_handle(handle);
292 			return (Z_BAD_HANDLE);
293 		}
294 
295 		bzero(zonecfg_args, sizeof (zonecfg_args));
296 		(void) zonecfg_get_bootargs(handle, zonecfg_args,
297 		    sizeof (zonecfg_args));
298 		inargs = zonecfg_args;
299 		zonecfg_fini_handle(handle);
300 	}
301 
302 	if (strlen(inargs) >= BOOTARGS_MAX) {
303 		zerror(zlogp, B_FALSE, "boot argument string too long");
304 		return (Z_INVAL);
305 	}
306 
307 	(void) strlcpy(scratchargs, inargs, sizeof (scratchargs));
308 	sargs = scratchargs;
309 	while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) {
310 		sargs = NULL;
311 		argc++;
312 	}
313 
314 	if ((argv = calloc(argc + 1, sizeof (char *))) == NULL) {
315 		zerror(zlogp, B_FALSE, "memory allocation failed");
316 		return (Z_NOMEM);
317 	}
318 
319 	argv_save = argv;
320 	argc_save = argc;
321 
322 	(void) strlcpy(scratchargs, inargs, sizeof (scratchargs));
323 	sargs = scratchargs;
324 	i = 0;
325 	while ((arg = strtok_r(sargs, " \t", &lasts)) != NULL) {
326 		sargs = NULL;
327 		if ((argv[i] = strdup(arg)) == NULL) {
328 			err = Z_NOMEM;
329 			zerror(zlogp, B_FALSE, "memory allocation failed");
330 			goto done;
331 		}
332 		i++;
333 	}
334 
335 	/*
336 	 * We preserve compatibility with the Solaris system boot behavior,
337 	 * which allows:
338 	 *
339 	 * 	# reboot kernel/unix -s -m verbose
340 	 *
341 	 * In this example, kernel/unix tells the booter what file to
342 	 * boot.  We don't want reboot in a zone to be gratuitously different,
343 	 * so we silently ignore the boot file, if necessary.
344 	 */
345 	if (argv[0] == NULL)
346 		goto done;
347 
348 	assert(argv[0][0] != ' ');
349 	assert(argv[0][0] != '\t');
350 
351 	if (argv[0][0] != '-' && argv[0][0] != '\0') {
352 		argv = &argv[1];
353 		argc--;
354 	}
355 
356 	optind = 0;
357 	opterr = 0;
358 	err = Z_OK;
359 	while ((c = getopt(argc, argv, "fi:m:s")) != -1) {
360 		switch (c) {
361 		case 'i':
362 			/*
363 			 * -i is handled by the runtime and is not passed
364 			 * along to userland
365 			 */
366 			(void) strlcpy(init_file, optarg, MAXPATHLEN);
367 			break;
368 		case 'f':
369 			/* This has already been processed by zoneadm */
370 			break;
371 		case 'm':
372 		case 's':
373 			/* These pass through unmolested */
374 			(void) snprintf(outargs, BOOTARGS_MAX,
375 			    "%s -%c %s ", outargs, c, optarg ? optarg : "");
376 			break;
377 		case '?':
378 			/*
379 			 * We warn about unknown arguments but pass them
380 			 * along anyway-- if someone wants to develop their
381 			 * own init replacement, they can pass it whatever
382 			 * args they want.
383 			 */
384 			err = Z_INVAL;
385 			(void) snprintf(outargs, BOOTARGS_MAX,
386 			    "%s -%c", outargs, optopt);
387 			(void) snprintf(badarg, BOOTARGS_MAX,
388 			    "%s -%c", badarg, optopt);
389 			break;
390 		}
391 	}
392 
393 	/*
394 	 * For Solaris Zones we warn about and discard non-option arguments.
395 	 * Hence 'boot foo bar baz gub' --> 'boot'.  However, to be similar
396 	 * to the kernel, we concat up all the other remaining boot args.
397 	 * and warn on them as a group.
398 	 */
399 	if (optind < argc) {
400 		err = Z_INVAL;
401 		while (optind < argc) {
402 			(void) snprintf(badarg, BOOTARGS_MAX, "%s%s%s",
403 			    badarg, strlen(badarg) > 0 ? " " : "",
404 			    argv[optind]);
405 			optind++;
406 		}
407 		zerror(zlogp, B_FALSE, "WARNING: Unused or invalid boot "
408 		    "arguments `%s'.", badarg);
409 	}
410 
411 done:
412 	for (i = 0; i < argc_save; i++) {
413 		if (argv_save[i] != NULL)
414 			free(argv_save[i]);
415 	}
416 	free(argv_save);
417 	return (err);
418 }
419 
420 
421 static int
422 mkzonedir(zlog_t *zlogp)
423 {
424 	struct stat st;
425 	/*
426 	 * We must create and lock everyone but root out of ZONES_TMPDIR
427 	 * since anyone can open any UNIX domain socket, regardless of
428 	 * its file system permissions.  Sigh...
429 	 */
430 	if (mkdir(ZONES_TMPDIR, S_IRWXU) < 0 && errno != EEXIST) {
431 		zerror(zlogp, B_TRUE, "could not mkdir '%s'", ZONES_TMPDIR);
432 		return (-1);
433 	}
434 	/* paranoia */
435 	if ((stat(ZONES_TMPDIR, &st) < 0) || !S_ISDIR(st.st_mode)) {
436 		zerror(zlogp, B_TRUE, "'%s' is not a directory", ZONES_TMPDIR);
437 		return (-1);
438 	}
439 	(void) chmod(ZONES_TMPDIR, S_IRWXU);
440 	return (0);
441 }
442 
443 /*
444  * Bring a zone up to the pre-boot "ready" stage.  The mount_cmd argument is
445  * 'true' if this is being invoked as part of the processing for the "mount"
446  * subcommand.
447  */
448 static int
449 zone_ready(zlog_t *zlogp, boolean_t mount_cmd)
450 {
451 	int err;
452 
453 	if ((err = zonecfg_create_snapshot(zone_name)) != Z_OK) {
454 		zerror(zlogp, B_FALSE, "unable to create snapshot: %s",
455 		    zonecfg_strerror(err));
456 		return (-1);
457 	}
458 
459 	if ((zone_id = vplat_create(zlogp, mount_cmd)) == -1) {
460 		if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
461 			zerror(zlogp, B_FALSE, "destroying snapshot: %s",
462 			    zonecfg_strerror(err));
463 		return (-1);
464 	}
465 	if (vplat_bringup(zlogp, mount_cmd, zone_id) != 0) {
466 		bringup_failure_recovery = B_TRUE;
467 		(void) vplat_teardown(NULL, mount_cmd, B_FALSE);
468 		if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
469 			zerror(zlogp, B_FALSE, "destroying snapshot: %s",
470 			    zonecfg_strerror(err));
471 		return (-1);
472 	}
473 
474 	return (0);
475 }
476 
477 int
478 init_template(void)
479 {
480 	int fd;
481 	int err = 0;
482 
483 	fd = open64(CTFS_ROOT "/process/template", O_RDWR);
484 	if (fd == -1)
485 		return (-1);
486 
487 	/*
488 	 * For now, zoneadmd doesn't do anything with the contract.
489 	 * Deliver no events, don't inherit, and allow it to be orphaned.
490 	 */
491 	err |= ct_tmpl_set_critical(fd, 0);
492 	err |= ct_tmpl_set_informative(fd, 0);
493 	err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR);
494 	err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT);
495 	if (err || ct_tmpl_activate(fd)) {
496 		(void) close(fd);
497 		return (-1);
498 	}
499 
500 	return (fd);
501 }
502 
503 typedef struct fs_callback {
504 	zlog_t		*zlogp;
505 	zoneid_t	zoneid;
506 } fs_callback_t;
507 
508 static int
509 mount_early_fs(void *data, const char *spec, const char *dir,
510     const char *fstype, const char *opt)
511 {
512 	zlog_t *zlogp = ((fs_callback_t *)data)->zlogp;
513 	zoneid_t zoneid = ((fs_callback_t *)data)->zoneid;
514 	char rootpath[MAXPATHLEN];
515 	pid_t child;
516 	int child_status;
517 	int tmpl_fd;
518 	int rv;
519 	ctid_t ct;
520 
521 	if (zone_get_rootpath(zone_name, rootpath, sizeof (rootpath)) != Z_OK) {
522 		zerror(zlogp, B_FALSE, "unable to determine zone root");
523 		return (-1);
524 	}
525 
526 	if ((rv = valid_mount_path(zlogp, rootpath, spec, dir, fstype)) < 0) {
527 		zerror(zlogp, B_FALSE, "%s%s is not a valid mount point",
528 		    rootpath, dir);
529 		return (-1);
530 	} else if (rv > 0) {
531 		/* The mount point path doesn't exist, create it now. */
532 		if (make_one_dir(zlogp, rootpath, dir,
533 		    DEFAULT_DIR_MODE, DEFAULT_DIR_USER,
534 		    DEFAULT_DIR_GROUP) != 0) {
535 			zerror(zlogp, B_FALSE, "failed to create mount point");
536 			return (-1);
537 		}
538 
539 		/*
540 		 * Now this might seem weird, but we need to invoke
541 		 * valid_mount_path() again.  Why?  Because it checks
542 		 * to make sure that the mount point path is canonical,
543 		 * which it can only do if the path exists, so now that
544 		 * we've created the path we have to verify it again.
545 		 */
546 		if ((rv = valid_mount_path(zlogp, rootpath, spec, dir,
547 		    fstype)) < 0) {
548 			zerror(zlogp, B_FALSE,
549 			    "%s%s is not a valid mount point", rootpath, dir);
550 			return (-1);
551 		}
552 	}
553 
554 	if ((tmpl_fd = init_template()) == -1) {
555 		zerror(zlogp, B_TRUE, "failed to create contract");
556 		return (-1);
557 	}
558 
559 	if ((child = fork()) == -1) {
560 		(void) ct_tmpl_clear(tmpl_fd);
561 		(void) close(tmpl_fd);
562 		zerror(zlogp, B_TRUE, "failed to fork");
563 		return (-1);
564 
565 	} else if (child == 0) {	/* child */
566 		char opt_buf[MAX_MNTOPT_STR];
567 		int optlen = 0;
568 		int mflag = MS_DATA;
569 
570 		(void) ct_tmpl_clear(tmpl_fd);
571 		/*
572 		 * Even though there are no procs running in the zone, we
573 		 * do this for paranoia's sake.
574 		 */
575 		(void) closefrom(0);
576 
577 		if (zone_enter(zoneid) == -1) {
578 			_exit(errno);
579 		}
580 		if (opt != NULL) {
581 			/*
582 			 * The mount() system call is incredibly annoying.
583 			 * If options are specified, we need to copy them
584 			 * into a temporary buffer since the mount() system
585 			 * call will overwrite the options string.  It will
586 			 * also fail if the new option string it wants to
587 			 * write is bigger than the one we passed in, so
588 			 * you must pass in a buffer of the maximum possible
589 			 * option string length.  sigh.
590 			 */
591 			(void) strlcpy(opt_buf, opt, sizeof (opt_buf));
592 			opt = opt_buf;
593 			optlen = MAX_MNTOPT_STR;
594 			mflag = MS_OPTIONSTR;
595 		}
596 		if (mount(spec, dir, mflag, fstype, NULL, 0, opt, optlen) != 0)
597 			_exit(errno);
598 		_exit(0);
599 	}
600 
601 	/* parent */
602 	if (contract_latest(&ct) == -1)
603 		ct = -1;
604 	(void) ct_tmpl_clear(tmpl_fd);
605 	(void) close(tmpl_fd);
606 	if (waitpid(child, &child_status, 0) != child) {
607 		/* unexpected: we must have been signalled */
608 		(void) contract_abandon_id(ct);
609 		return (-1);
610 	}
611 	(void) contract_abandon_id(ct);
612 	if (WEXITSTATUS(child_status) != 0) {
613 		errno = WEXITSTATUS(child_status);
614 		zerror(zlogp, B_TRUE, "mount of %s failed", dir);
615 		return (-1);
616 	}
617 
618 	return (0);
619 }
620 
621 int
622 do_subproc(zlog_t *zlogp, char *cmdbuf)
623 {
624 	char inbuf[1024];	/* arbitrary large amount */
625 	FILE *file;
626 	int status;
627 
628 	file = popen(cmdbuf, "r");
629 	if (file == NULL) {
630 		zerror(zlogp, B_TRUE, "could not launch: %s", cmdbuf);
631 		return (-1);
632 	}
633 
634 	while (fgets(inbuf, sizeof (inbuf), file) != NULL)
635 		if (zlogp != &logsys)
636 			zerror(zlogp, B_FALSE, "%s", inbuf);
637 	status = pclose(file);
638 
639 	if (WIFSIGNALED(status)) {
640 		zerror(zlogp, B_FALSE, "%s unexpectedly terminated due to "
641 		    "signal %d", cmdbuf, WTERMSIG(status));
642 		return (-1);
643 	}
644 	assert(WIFEXITED(status));
645 	if (WEXITSTATUS(status) == ZEXIT_EXEC) {
646 		zerror(zlogp, B_FALSE, "failed to exec %s", cmdbuf);
647 		return (-1);
648 	}
649 	return (WEXITSTATUS(status));
650 }
651 
652 static int
653 zone_bootup(zlog_t *zlogp, const char *bootargs)
654 {
655 	zoneid_t zoneid;
656 	struct stat st;
657 	char zroot[MAXPATHLEN], initpath[MAXPATHLEN], init_file[MAXPATHLEN];
658 	char nbootargs[BOOTARGS_MAX];
659 	char cmdbuf[MAXPATHLEN];
660 	fs_callback_t cb;
661 	brand_handle_t bh;
662 	int err;
663 
664 	if (init_console_slave(zlogp) != 0)
665 		return (-1);
666 	reset_slave_terminal(zlogp);
667 
668 	if ((zoneid = getzoneidbyname(zone_name)) == -1) {
669 		zerror(zlogp, B_TRUE, "unable to get zoneid");
670 		return (-1);
671 	}
672 
673 	cb.zlogp = zlogp;
674 	cb.zoneid = zoneid;
675 
676 	/* Get a handle to the brand info for this zone */
677 	if ((bh = brand_open(brand_name)) == NULL) {
678 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
679 		return (-1);
680 	}
681 
682 	/*
683 	 * Get the list of filesystems to mount from the brand
684 	 * configuration.  These mounts are done via a thread that will
685 	 * enter the zone, so they are done from within the context of the
686 	 * zone.
687 	 */
688 	if (brand_platform_iter_mounts(bh, mount_early_fs, &cb) != 0) {
689 		zerror(zlogp, B_FALSE, "unable to mount filesystems");
690 		brand_close(bh);
691 		return (-1);
692 	}
693 
694 	/*
695 	 * Get the brand's boot callback if it exists.
696 	 */
697 	if (zone_get_zonepath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
698 		zerror(zlogp, B_FALSE, "unable to determine zone root");
699 		brand_close(bh);
700 		return (-1);
701 	}
702 	(void) strcpy(cmdbuf, EXEC_PREFIX);
703 	if (brand_get_boot(bh, zone_name, zroot, cmdbuf + EXEC_LEN,
704 	    sizeof (cmdbuf) - EXEC_LEN, 0, NULL) != 0) {
705 		zerror(zlogp, B_FALSE,
706 		    "unable to determine branded zone's boot callback");
707 		brand_close(bh);
708 		return (-1);
709 	}
710 
711 	/* Get the path for this zone's init(1M) (or equivalent) process.  */
712 	if (brand_get_initname(bh, init_file, MAXPATHLEN) != 0) {
713 		zerror(zlogp, B_FALSE,
714 		    "unable to determine zone's init(1M) location");
715 		brand_close(bh);
716 		return (-1);
717 	}
718 
719 	brand_close(bh);
720 
721 	err = filter_bootargs(zlogp, bootargs, nbootargs, init_file,
722 	    bad_boot_arg);
723 	if (err == Z_INVAL)
724 		eventstream_write(Z_EVT_ZONE_BADARGS);
725 	else if (err != Z_OK)
726 		return (-1);
727 
728 	assert(init_file[0] != '\0');
729 
730 	/* Try to anticipate possible problems: Make sure init is executable. */
731 	if (zone_get_rootpath(zone_name, zroot, sizeof (zroot)) != Z_OK) {
732 		zerror(zlogp, B_FALSE, "unable to determine zone root");
733 		return (-1);
734 	}
735 
736 	(void) snprintf(initpath, sizeof (initpath), "%s%s", zroot, init_file);
737 
738 	if (stat(initpath, &st) == -1) {
739 		zerror(zlogp, B_TRUE, "could not stat %s", initpath);
740 		return (-1);
741 	}
742 
743 	if ((st.st_mode & S_IXUSR) == 0) {
744 		zerror(zlogp, B_FALSE, "%s is not executable", initpath);
745 		return (-1);
746 	}
747 
748 	/*
749 	 * If there is a brand 'boot' callback, execute it now to give the
750 	 * brand one last chance to do any additional setup before the zone
751 	 * is booted.
752 	 */
753 	if ((strlen(cmdbuf) > EXEC_LEN) &&
754 	    (do_subproc(zlogp, cmdbuf) != Z_OK)) {
755 		zerror(zlogp, B_FALSE, "%s failed", cmdbuf);
756 		return (-1);
757 	}
758 
759 	if (zone_setattr(zoneid, ZONE_ATTR_INITNAME, init_file, 0) == -1) {
760 		zerror(zlogp, B_TRUE, "could not set zone boot file");
761 		return (-1);
762 	}
763 
764 	if (zone_setattr(zoneid, ZONE_ATTR_BOOTARGS, nbootargs, 0) == -1) {
765 		zerror(zlogp, B_TRUE, "could not set zone boot arguments");
766 		return (-1);
767 	}
768 
769 	if (zone_boot(zoneid) == -1) {
770 		zerror(zlogp, B_TRUE, "unable to boot zone");
771 		return (-1);
772 	}
773 
774 	return (0);
775 }
776 
777 static int
778 zone_halt(zlog_t *zlogp, boolean_t unmount_cmd, boolean_t rebooting)
779 {
780 	int err;
781 
782 	if (vplat_teardown(zlogp, unmount_cmd, rebooting) != 0) {
783 		if (!bringup_failure_recovery)
784 			zerror(zlogp, B_FALSE, "unable to destroy zone");
785 		return (-1);
786 	}
787 
788 	if ((err = zonecfg_destroy_snapshot(zone_name)) != Z_OK)
789 		zerror(zlogp, B_FALSE, "destroying snapshot: %s",
790 		    zonecfg_strerror(err));
791 
792 	return (0);
793 }
794 
795 /*
796  * Generate AUE_zone_state for a command that boots a zone.
797  */
798 static void
799 audit_put_record(zlog_t *zlogp, ucred_t *uc, int return_val,
800     char *new_state)
801 {
802 	adt_session_data_t	*ah;
803 	adt_event_data_t	*event;
804 	int			pass_fail, fail_reason;
805 
806 	if (!adt_audit_enabled())
807 		return;
808 
809 	if (return_val == 0) {
810 		pass_fail = ADT_SUCCESS;
811 		fail_reason = ADT_SUCCESS;
812 	} else {
813 		pass_fail = ADT_FAILURE;
814 		fail_reason = ADT_FAIL_VALUE_PROGRAM;
815 	}
816 
817 	if (adt_start_session(&ah, NULL, 0)) {
818 		zerror(zlogp, B_TRUE, gettext("audit failure."));
819 		return;
820 	}
821 	if (adt_set_from_ucred(ah, uc, ADT_NEW)) {
822 		zerror(zlogp, B_TRUE, gettext("audit failure."));
823 		(void) adt_end_session(ah);
824 		return;
825 	}
826 
827 	event = adt_alloc_event(ah, ADT_zone_state);
828 	if (event == NULL) {
829 		zerror(zlogp, B_TRUE, gettext("audit failure."));
830 		(void) adt_end_session(ah);
831 		return;
832 	}
833 	event->adt_zone_state.zonename = zone_name;
834 	event->adt_zone_state.new_state = new_state;
835 
836 	if (adt_put_event(event, pass_fail, fail_reason))
837 		zerror(zlogp, B_TRUE, gettext("audit failure."));
838 
839 	adt_free_event(event);
840 
841 	(void) adt_end_session(ah);
842 }
843 
844 /*
845  * The main routine for the door server that deals with zone state transitions.
846  */
847 /* ARGSUSED */
848 static void
849 server(void *cookie, char *args, size_t alen, door_desc_t *dp,
850     uint_t n_desc)
851 {
852 	ucred_t *uc = NULL;
853 	const priv_set_t *eset;
854 
855 	zone_state_t zstate;
856 	zone_cmd_t cmd;
857 	zone_cmd_arg_t *zargp;
858 
859 	boolean_t kernelcall;
860 
861 	int rval = -1;
862 	uint64_t uniqid;
863 	zoneid_t zoneid = -1;
864 	zlog_t zlog;
865 	zlog_t *zlogp;
866 	zone_cmd_rval_t *rvalp;
867 	size_t rlen = getpagesize(); /* conservative */
868 	fs_callback_t cb;
869 	brand_handle_t bh;
870 
871 	/* LINTED E_BAD_PTR_CAST_ALIGN */
872 	zargp = (zone_cmd_arg_t *)args;
873 
874 	/*
875 	 * When we get the door unref message, we've fdetach'd the door, and
876 	 * it is time for us to shut down zoneadmd.
877 	 */
878 	if (zargp == DOOR_UNREF_DATA) {
879 		/*
880 		 * See comment at end of main() for info on the last rites.
881 		 */
882 		exit(0);
883 	}
884 
885 	if (zargp == NULL) {
886 		(void) door_return(NULL, 0, 0, 0);
887 	}
888 
889 	rvalp = alloca(rlen);
890 	bzero(rvalp, rlen);
891 	zlog.logfile = NULL;
892 	zlog.buflen = zlog.loglen = rlen - sizeof (zone_cmd_rval_t) + 1;
893 	zlog.buf = rvalp->errbuf;
894 	zlog.log = zlog.buf;
895 	/* defer initialization of zlog.locale until after credential check */
896 	zlogp = &zlog;
897 
898 	if (alen != sizeof (zone_cmd_arg_t)) {
899 		/*
900 		 * This really shouldn't be happening.
901 		 */
902 		zerror(&logsys, B_FALSE, "argument size (%d bytes) "
903 		    "unexpected (expected %d bytes)", alen,
904 		    sizeof (zone_cmd_arg_t));
905 		goto out;
906 	}
907 	cmd = zargp->cmd;
908 
909 	if (door_ucred(&uc) != 0) {
910 		zerror(&logsys, B_TRUE, "door_ucred");
911 		goto out;
912 	}
913 	eset = ucred_getprivset(uc, PRIV_EFFECTIVE);
914 	if (ucred_getzoneid(uc) != GLOBAL_ZONEID ||
915 	    (eset != NULL ? !priv_ismember(eset, PRIV_SYS_CONFIG) :
916 	    ucred_geteuid(uc) != 0)) {
917 		zerror(&logsys, B_FALSE, "insufficient privileges");
918 		goto out;
919 	}
920 
921 	kernelcall = ucred_getpid(uc) == 0;
922 
923 	/*
924 	 * This is safe because we only use a zlog_t throughout the
925 	 * duration of a door call; i.e., by the time the pointer
926 	 * might become invalid, the door call would be over.
927 	 */
928 	zlog.locale = kernelcall ? DEFAULT_LOCALE : zargp->locale;
929 
930 	(void) mutex_lock(&lock);
931 
932 	/*
933 	 * Once we start to really die off, we don't want more connections.
934 	 */
935 	if (in_death_throes) {
936 		(void) mutex_unlock(&lock);
937 		ucred_free(uc);
938 		(void) door_return(NULL, 0, 0, 0);
939 		thr_exit(NULL);
940 	}
941 
942 	/*
943 	 * Check for validity of command.
944 	 */
945 	if (cmd != Z_READY && cmd != Z_BOOT && cmd != Z_FORCEBOOT &&
946 	    cmd != Z_REBOOT && cmd != Z_HALT && cmd != Z_NOTE_UNINSTALLING &&
947 	    cmd != Z_MOUNT && cmd != Z_FORCEMOUNT && cmd != Z_UNMOUNT) {
948 		zerror(&logsys, B_FALSE, "invalid command %d", (int)cmd);
949 		goto out;
950 	}
951 
952 	if (kernelcall && (cmd != Z_HALT && cmd != Z_REBOOT)) {
953 		/*
954 		 * Can't happen
955 		 */
956 		zerror(&logsys, B_FALSE, "received unexpected kernel upcall %d",
957 		    cmd);
958 		goto out;
959 	}
960 	/*
961 	 * We ignore the possibility of someone calling zone_create(2)
962 	 * explicitly; all requests must come through zoneadmd.
963 	 */
964 	if (zone_get_state(zone_name, &zstate) != Z_OK) {
965 		/*
966 		 * Something terribly wrong happened
967 		 */
968 		zerror(&logsys, B_FALSE, "unable to determine state of zone");
969 		goto out;
970 	}
971 
972 	if (kernelcall) {
973 		/*
974 		 * Kernel-initiated requests may lose their validity if the
975 		 * zone_t the kernel was referring to has gone away.
976 		 */
977 		if ((zoneid = getzoneidbyname(zone_name)) == -1 ||
978 		    zone_getattr(zoneid, ZONE_ATTR_UNIQID, &uniqid,
979 		    sizeof (uniqid)) == -1 || uniqid != zargp->uniqid) {
980 			/*
981 			 * We're not talking about the same zone. The request
982 			 * must have arrived too late.  Return error.
983 			 */
984 			rval = -1;
985 			goto out;
986 		}
987 		zlogp = &logsys;	/* Log errors to syslog */
988 	}
989 
990 	/*
991 	 * If we are being asked to forcibly mount or boot a zone, we
992 	 * pretend that an INCOMPLETE zone is actually INSTALLED.
993 	 */
994 	if (zstate == ZONE_STATE_INCOMPLETE &&
995 	    (cmd == Z_FORCEBOOT || cmd == Z_FORCEMOUNT))
996 		zstate = ZONE_STATE_INSTALLED;
997 
998 	switch (zstate) {
999 	case ZONE_STATE_CONFIGURED:
1000 	case ZONE_STATE_INCOMPLETE:
1001 		/*
1002 		 * Not our area of expertise; we just print a nice message
1003 		 * and die off.
1004 		 */
1005 		zerror(zlogp, B_FALSE,
1006 		    "%s operation is invalid for zones in state '%s'",
1007 		    z_cmd_name(cmd), zone_state_str(zstate));
1008 		break;
1009 
1010 	case ZONE_STATE_INSTALLED:
1011 		switch (cmd) {
1012 		case Z_READY:
1013 			rval = zone_ready(zlogp, B_FALSE);
1014 			if (rval == 0)
1015 				eventstream_write(Z_EVT_ZONE_READIED);
1016 			break;
1017 		case Z_BOOT:
1018 		case Z_FORCEBOOT:
1019 			eventstream_write(Z_EVT_ZONE_BOOTING);
1020 			if ((rval = zone_ready(zlogp, B_FALSE)) == 0)
1021 				rval = zone_bootup(zlogp, zargp->bootbuf);
1022 			audit_put_record(zlogp, uc, rval, "boot");
1023 			if (rval != 0) {
1024 				bringup_failure_recovery = B_TRUE;
1025 				(void) zone_halt(zlogp, B_FALSE, B_FALSE);
1026 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1027 			}
1028 			break;
1029 		case Z_HALT:
1030 			if (kernelcall)	/* Invalid; can't happen */
1031 				abort();
1032 			/*
1033 			 * We could have two clients racing to halt this
1034 			 * zone; the second client loses, but his request
1035 			 * doesn't fail, since the zone is now in the desired
1036 			 * state.
1037 			 */
1038 			zerror(zlogp, B_FALSE, "zone is already halted");
1039 			rval = 0;
1040 			break;
1041 		case Z_REBOOT:
1042 			if (kernelcall)	/* Invalid; can't happen */
1043 				abort();
1044 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1045 			    "for zones in state '%s'", z_cmd_name(cmd),
1046 			    zone_state_str(zstate));
1047 			rval = -1;
1048 			break;
1049 		case Z_NOTE_UNINSTALLING:
1050 			if (kernelcall)	/* Invalid; can't happen */
1051 				abort();
1052 			/*
1053 			 * Tell the console to print out a message about this.
1054 			 * Once it does, we will be in_death_throes.
1055 			 */
1056 			eventstream_write(Z_EVT_ZONE_UNINSTALLING);
1057 			break;
1058 		case Z_MOUNT:
1059 		case Z_FORCEMOUNT:
1060 			if (kernelcall)	/* Invalid; can't happen */
1061 				abort();
1062 			if (!zone_isnative && !zone_iscluster) {
1063 				zerror(zlogp, B_FALSE,
1064 				    "%s operation is invalid for branded "
1065 				    "zones", z_cmd_name(cmd));
1066 				rval = -1;
1067 				break;
1068 			}
1069 
1070 			rval = zone_ready(zlogp, B_TRUE);
1071 			if (rval != 0)
1072 				break;
1073 
1074 			eventstream_write(Z_EVT_ZONE_READIED);
1075 
1076 			/* Get a handle to the brand info for this zone */
1077 			if ((bh = brand_open(brand_name)) == NULL) {
1078 				rval = -1;
1079 				break;
1080 			}
1081 
1082 			/*
1083 			 * Get the list of filesystems to mount from
1084 			 * the brand configuration.  These mounts are done
1085 			 * via a thread that will enter the zone, so they
1086 			 * are done from within the context of the zone.
1087 			 */
1088 			cb.zlogp = zlogp;
1089 			cb.zoneid = zone_id;
1090 			rval = brand_platform_iter_mounts(bh,
1091 			    mount_early_fs, &cb);
1092 
1093 			brand_close(bh);
1094 
1095 			/*
1096 			 * Ordinarily, /dev/fd would be mounted inside the zone
1097 			 * by svc:/system/filesystem/usr:default, but since
1098 			 * we're not booting the zone, we need to do this
1099 			 * manually.
1100 			 */
1101 			if (rval == 0)
1102 				rval = mount_early_fs(&cb,
1103 				    "fd", "/dev/fd", "fd", NULL);
1104 			break;
1105 		case Z_UNMOUNT:
1106 			if (kernelcall)	/* Invalid; can't happen */
1107 				abort();
1108 			zerror(zlogp, B_FALSE, "zone is already unmounted");
1109 			rval = 0;
1110 			break;
1111 		}
1112 		break;
1113 
1114 	case ZONE_STATE_READY:
1115 		switch (cmd) {
1116 		case Z_READY:
1117 			/*
1118 			 * We could have two clients racing to ready this
1119 			 * zone; the second client loses, but his request
1120 			 * doesn't fail, since the zone is now in the desired
1121 			 * state.
1122 			 */
1123 			zerror(zlogp, B_FALSE, "zone is already ready");
1124 			rval = 0;
1125 			break;
1126 		case Z_BOOT:
1127 			(void) strlcpy(boot_args, zargp->bootbuf,
1128 			    sizeof (boot_args));
1129 			eventstream_write(Z_EVT_ZONE_BOOTING);
1130 			rval = zone_bootup(zlogp, zargp->bootbuf);
1131 			audit_put_record(zlogp, uc, rval, "boot");
1132 			if (rval != 0) {
1133 				bringup_failure_recovery = B_TRUE;
1134 				(void) zone_halt(zlogp, B_FALSE, B_TRUE);
1135 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1136 			}
1137 			boot_args[0] = '\0';
1138 			break;
1139 		case Z_HALT:
1140 			if (kernelcall)	/* Invalid; can't happen */
1141 				abort();
1142 			if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE)) != 0)
1143 				break;
1144 			eventstream_write(Z_EVT_ZONE_HALTED);
1145 			break;
1146 		case Z_REBOOT:
1147 		case Z_NOTE_UNINSTALLING:
1148 		case Z_MOUNT:
1149 		case Z_UNMOUNT:
1150 			if (kernelcall)	/* Invalid; can't happen */
1151 				abort();
1152 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1153 			    "for zones in state '%s'", z_cmd_name(cmd),
1154 			    zone_state_str(zstate));
1155 			rval = -1;
1156 			break;
1157 		}
1158 		break;
1159 
1160 	case ZONE_STATE_MOUNTED:
1161 		switch (cmd) {
1162 		case Z_UNMOUNT:
1163 			if (kernelcall)	/* Invalid; can't happen */
1164 				abort();
1165 			rval = zone_halt(zlogp, B_TRUE, B_FALSE);
1166 			if (rval == 0) {
1167 				eventstream_write(Z_EVT_ZONE_HALTED);
1168 				(void) sema_post(&scratch_sem);
1169 			}
1170 			break;
1171 		default:
1172 			if (kernelcall)	/* Invalid; can't happen */
1173 				abort();
1174 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1175 			    "for zones in state '%s'", z_cmd_name(cmd),
1176 			    zone_state_str(zstate));
1177 			rval = -1;
1178 			break;
1179 		}
1180 		break;
1181 
1182 	case ZONE_STATE_RUNNING:
1183 	case ZONE_STATE_SHUTTING_DOWN:
1184 	case ZONE_STATE_DOWN:
1185 		switch (cmd) {
1186 		case Z_READY:
1187 			if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE)) != 0)
1188 				break;
1189 			if ((rval = zone_ready(zlogp, B_FALSE)) == 0)
1190 				eventstream_write(Z_EVT_ZONE_READIED);
1191 			else
1192 				eventstream_write(Z_EVT_ZONE_HALTED);
1193 			break;
1194 		case Z_BOOT:
1195 			/*
1196 			 * We could have two clients racing to boot this
1197 			 * zone; the second client loses, but his request
1198 			 * doesn't fail, since the zone is now in the desired
1199 			 * state.
1200 			 */
1201 			zerror(zlogp, B_FALSE, "zone is already booted");
1202 			rval = 0;
1203 			break;
1204 		case Z_HALT:
1205 			if ((rval = zone_halt(zlogp, B_FALSE, B_FALSE)) != 0)
1206 				break;
1207 			eventstream_write(Z_EVT_ZONE_HALTED);
1208 			break;
1209 		case Z_REBOOT:
1210 			(void) strlcpy(boot_args, zargp->bootbuf,
1211 			    sizeof (boot_args));
1212 			eventstream_write(Z_EVT_ZONE_REBOOTING);
1213 			if ((rval = zone_halt(zlogp, B_FALSE, B_TRUE)) != 0) {
1214 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1215 				boot_args[0] = '\0';
1216 				break;
1217 			}
1218 			if ((rval = zone_ready(zlogp, B_FALSE)) != 0) {
1219 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1220 				boot_args[0] = '\0';
1221 				break;
1222 			}
1223 			rval = zone_bootup(zlogp, zargp->bootbuf);
1224 			audit_put_record(zlogp, uc, rval, "reboot");
1225 			if (rval != 0) {
1226 				(void) zone_halt(zlogp, B_FALSE, B_TRUE);
1227 				eventstream_write(Z_EVT_ZONE_BOOTFAILED);
1228 			}
1229 			boot_args[0] = '\0';
1230 			break;
1231 		case Z_NOTE_UNINSTALLING:
1232 		case Z_MOUNT:
1233 		case Z_UNMOUNT:
1234 			zerror(zlogp, B_FALSE, "%s operation is invalid "
1235 			    "for zones in state '%s'", z_cmd_name(cmd),
1236 			    zone_state_str(zstate));
1237 			rval = -1;
1238 			break;
1239 		}
1240 		break;
1241 	default:
1242 		abort();
1243 	}
1244 
1245 	/*
1246 	 * Because the state of the zone may have changed, we make sure
1247 	 * to wake the console poller, which is in charge of initiating
1248 	 * the shutdown procedure as necessary.
1249 	 */
1250 	eventstream_write(Z_EVT_NULL);
1251 
1252 out:
1253 	(void) mutex_unlock(&lock);
1254 	if (kernelcall) {
1255 		rvalp = NULL;
1256 		rlen = 0;
1257 	} else {
1258 		rvalp->rval = rval;
1259 	}
1260 	if (uc != NULL)
1261 		ucred_free(uc);
1262 	(void) door_return((char *)rvalp, rlen, NULL, 0);
1263 	thr_exit(NULL);
1264 }
1265 
1266 static int
1267 setup_door(zlog_t *zlogp)
1268 {
1269 	if ((zone_door = door_create(server, NULL,
1270 	    DOOR_UNREF | DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) < 0) {
1271 		zerror(zlogp, B_TRUE, "%s failed", "door_create");
1272 		return (-1);
1273 	}
1274 	(void) fdetach(zone_door_path);
1275 
1276 	if (fattach(zone_door, zone_door_path) != 0) {
1277 		zerror(zlogp, B_TRUE, "fattach to %s failed", zone_door_path);
1278 		(void) door_revoke(zone_door);
1279 		(void) fdetach(zone_door_path);
1280 		zone_door = -1;
1281 		return (-1);
1282 	}
1283 	return (0);
1284 }
1285 
1286 /*
1287  * zoneadm(1m) will start zoneadmd if it thinks it isn't running; this
1288  * is where zoneadmd itself will check to see that another instance of
1289  * zoneadmd isn't already controlling this zone.
1290  *
1291  * The idea here is that we want to open the path to which we will
1292  * attach our door, lock it, and then make sure that no-one has beat us
1293  * to fattach(3c)ing onto it.
1294  *
1295  * fattach(3c) is really a mount, so there are actually two possible
1296  * vnodes we could be dealing with.  Our strategy is as follows:
1297  *
1298  * - If the file we opened is a regular file (common case):
1299  * 	There is no fattach(3c)ed door, so we have a chance of becoming
1300  * 	the managing zoneadmd. We attempt to lock the file: if it is
1301  * 	already locked, that means someone else raced us here, so we
1302  * 	lose and give up.  zoneadm(1m) will try to contact the zoneadmd
1303  * 	that beat us to it.
1304  *
1305  * - If the file we opened is a namefs file:
1306  * 	This means there is already an established door fattach(3c)'ed
1307  * 	to the rendezvous path.  We've lost the race, so we give up.
1308  * 	Note that in this case we also try to grab the file lock, and
1309  * 	will succeed in acquiring it since the vnode locked by the
1310  * 	"winning" zoneadmd was a regular one, and the one we locked was
1311  * 	the fattach(3c)'ed door node.  At any rate, no harm is done, and
1312  * 	we just return to zoneadm(1m) which knows to retry.
1313  */
1314 static int
1315 make_daemon_exclusive(zlog_t *zlogp)
1316 {
1317 	int doorfd = -1;
1318 	int err, ret = -1;
1319 	struct stat st;
1320 	struct flock flock;
1321 	zone_state_t zstate;
1322 
1323 top:
1324 	if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
1325 		zerror(zlogp, B_FALSE, "failed to get zone state: %s",
1326 		    zonecfg_strerror(err));
1327 		goto out;
1328 	}
1329 	if ((doorfd = open(zone_door_path, O_CREAT|O_RDWR,
1330 	    S_IREAD|S_IWRITE)) < 0) {
1331 		zerror(zlogp, B_TRUE, "failed to open %s", zone_door_path);
1332 		goto out;
1333 	}
1334 	if (fstat(doorfd, &st) < 0) {
1335 		zerror(zlogp, B_TRUE, "failed to stat %s", zone_door_path);
1336 		goto out;
1337 	}
1338 	/*
1339 	 * Lock the file to synchronize with other zoneadmd
1340 	 */
1341 	flock.l_type = F_WRLCK;
1342 	flock.l_whence = SEEK_SET;
1343 	flock.l_start = (off_t)0;
1344 	flock.l_len = (off_t)0;
1345 	if (fcntl(doorfd, F_SETLK, &flock) < 0) {
1346 		/*
1347 		 * Someone else raced us here and grabbed the lock file
1348 		 * first.  A warning here is inappropriate since nothing
1349 		 * went wrong.
1350 		 */
1351 		goto out;
1352 	}
1353 
1354 	if (strcmp(st.st_fstype, "namefs") == 0) {
1355 		struct door_info info;
1356 
1357 		/*
1358 		 * There is already something fattach()'ed to this file.
1359 		 * Lets see what the door is up to.
1360 		 */
1361 		if (door_info(doorfd, &info) == 0 && info.di_target != -1) {
1362 			/*
1363 			 * Another zoneadmd process seems to be in
1364 			 * control of the situation and we don't need to
1365 			 * be here.  A warning here is inappropriate
1366 			 * since nothing went wrong.
1367 			 *
1368 			 * If the door has been revoked, the zoneadmd
1369 			 * process currently managing the zone is going
1370 			 * away.  We'll return control to zoneadm(1m)
1371 			 * which will try again (by which time zoneadmd
1372 			 * will hopefully have exited).
1373 			 */
1374 			goto out;
1375 		}
1376 
1377 		/*
1378 		 * If we got this far, there's a fattach(3c)'ed door
1379 		 * that belongs to a process that has exited, which can
1380 		 * happen if the previous zoneadmd died unexpectedly.
1381 		 *
1382 		 * Let user know that something is amiss, but that we can
1383 		 * recover; if the zone is in the installed state, then don't
1384 		 * message, since having a running zoneadmd isn't really
1385 		 * expected/needed.  We want to keep occurences of this message
1386 		 * limited to times when zoneadmd is picking back up from a
1387 		 * zoneadmd that died while the zone was in some non-trivial
1388 		 * state.
1389 		 */
1390 		if (zstate > ZONE_STATE_INSTALLED) {
1391 			zerror(zlogp, B_FALSE,
1392 			    "zone '%s': WARNING: zone is in state '%s', but "
1393 			    "zoneadmd does not appear to be available; "
1394 			    "restarted zoneadmd to recover.",
1395 			    zone_name, zone_state_str(zstate));
1396 		}
1397 
1398 		(void) fdetach(zone_door_path);
1399 		(void) close(doorfd);
1400 		goto top;
1401 	}
1402 	ret = 0;
1403 out:
1404 	(void) close(doorfd);
1405 	return (ret);
1406 }
1407 
1408 int
1409 main(int argc, char *argv[])
1410 {
1411 	int opt;
1412 	zoneid_t zid;
1413 	priv_set_t *privset;
1414 	zone_state_t zstate;
1415 	char parents_locale[MAXPATHLEN];
1416 	brand_handle_t bh;
1417 	int err;
1418 
1419 	pid_t pid;
1420 	sigset_t blockset;
1421 	sigset_t block_cld;
1422 
1423 	struct {
1424 		sema_t sem;
1425 		int status;
1426 		zlog_t log;
1427 	} *shstate;
1428 	size_t shstatelen = getpagesize();
1429 
1430 	zlog_t errlog;
1431 	zlog_t *zlogp;
1432 
1433 	int ctfd;
1434 
1435 	progname = get_execbasename(argv[0]);
1436 
1437 	/*
1438 	 * Make sure stderr is unbuffered
1439 	 */
1440 	(void) setbuffer(stderr, NULL, 0);
1441 
1442 	/*
1443 	 * Get out of the way of mounted filesystems, since we will daemonize
1444 	 * soon.
1445 	 */
1446 	(void) chdir("/");
1447 
1448 	/*
1449 	 * Use the default system umask per PSARC 1998/110 rather than
1450 	 * anything that may have been set by the caller.
1451 	 */
1452 	(void) umask(CMASK);
1453 
1454 	/*
1455 	 * Initially we want to use our parent's locale.
1456 	 */
1457 	(void) setlocale(LC_ALL, "");
1458 	(void) textdomain(TEXT_DOMAIN);
1459 	(void) strlcpy(parents_locale, setlocale(LC_MESSAGES, NULL),
1460 	    sizeof (parents_locale));
1461 
1462 	/*
1463 	 * This zlog_t is used for writing to stderr
1464 	 */
1465 	errlog.logfile = stderr;
1466 	errlog.buflen = errlog.loglen = 0;
1467 	errlog.buf = errlog.log = NULL;
1468 	errlog.locale = parents_locale;
1469 
1470 	/*
1471 	 * We start off writing to stderr until we're ready to daemonize.
1472 	 */
1473 	zlogp = &errlog;
1474 
1475 	/*
1476 	 * Process options.
1477 	 */
1478 	while ((opt = getopt(argc, argv, "R:z:")) != EOF) {
1479 		switch (opt) {
1480 		case 'R':
1481 			zonecfg_set_root(optarg);
1482 			break;
1483 		case 'z':
1484 			zone_name = optarg;
1485 			break;
1486 		default:
1487 			usage();
1488 		}
1489 	}
1490 
1491 	if (zone_name == NULL)
1492 		usage();
1493 
1494 	/*
1495 	 * Because usage() prints directly to stderr, it has gettext()
1496 	 * wrapping, which depends on the locale.  But since zerror() calls
1497 	 * localize() which tweaks the locale, it is not safe to call zerror()
1498 	 * until after the last call to usage().  Fortunately, the last call
1499 	 * to usage() is just above and the first call to zerror() is just
1500 	 * below.  Don't mess this up.
1501 	 */
1502 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
1503 		zerror(zlogp, B_FALSE, "cannot manage the %s zone",
1504 		    GLOBAL_ZONENAME);
1505 		return (1);
1506 	}
1507 
1508 	if (zone_get_id(zone_name, &zid) != 0) {
1509 		zerror(zlogp, B_FALSE, "could not manage %s: %s", zone_name,
1510 		    zonecfg_strerror(Z_NO_ZONE));
1511 		return (1);
1512 	}
1513 
1514 	if ((err = zone_get_state(zone_name, &zstate)) != Z_OK) {
1515 		zerror(zlogp, B_FALSE, "failed to get zone state: %s",
1516 		    zonecfg_strerror(err));
1517 		return (1);
1518 	}
1519 	if (zstate < ZONE_STATE_INCOMPLETE) {
1520 		zerror(zlogp, B_FALSE,
1521 		    "cannot manage a zone which is in state '%s'",
1522 		    zone_state_str(zstate));
1523 		return (1);
1524 	}
1525 
1526 	/* Get a handle to the brand info for this zone */
1527 	if ((zone_get_brand(zone_name, brand_name, sizeof (brand_name))
1528 	    != Z_OK) || (bh = brand_open(brand_name)) == NULL) {
1529 		zerror(zlogp, B_FALSE, "unable to determine zone brand");
1530 		return (1);
1531 	}
1532 	zone_isnative = brand_is_native(bh);
1533 	zone_iscluster = (strcmp(brand_name, CLUSTER_BRAND_NAME) == 0);
1534 	brand_close(bh);
1535 
1536 	/*
1537 	 * Check that we have all privileges.  It would be nice to pare
1538 	 * this down, but this is at least a first cut.
1539 	 */
1540 	if ((privset = priv_allocset()) == NULL) {
1541 		zerror(zlogp, B_TRUE, "%s failed", "priv_allocset");
1542 		return (1);
1543 	}
1544 
1545 	if (getppriv(PRIV_EFFECTIVE, privset) != 0) {
1546 		zerror(zlogp, B_TRUE, "%s failed", "getppriv");
1547 		priv_freeset(privset);
1548 		return (1);
1549 	}
1550 
1551 	if (priv_isfullset(privset) == B_FALSE) {
1552 		zerror(zlogp, B_FALSE, "You lack sufficient privilege to "
1553 		    "run this command (all privs required)");
1554 		priv_freeset(privset);
1555 		return (1);
1556 	}
1557 	priv_freeset(privset);
1558 
1559 	if (mkzonedir(zlogp) != 0)
1560 		return (1);
1561 
1562 	/*
1563 	 * Pre-fork: setup shared state
1564 	 */
1565 	if ((shstate = (void *)mmap(NULL, shstatelen,
1566 	    PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, (off_t)0)) ==
1567 	    MAP_FAILED) {
1568 		zerror(zlogp, B_TRUE, "%s failed", "mmap");
1569 		return (1);
1570 	}
1571 	if (sema_init(&shstate->sem, 0, USYNC_PROCESS, NULL) != 0) {
1572 		zerror(zlogp, B_TRUE, "%s failed", "sema_init()");
1573 		(void) munmap((char *)shstate, shstatelen);
1574 		return (1);
1575 	}
1576 	shstate->log.logfile = NULL;
1577 	shstate->log.buflen = shstatelen - sizeof (*shstate);
1578 	shstate->log.loglen = shstate->log.buflen;
1579 	shstate->log.buf = (char *)shstate + sizeof (*shstate);
1580 	shstate->log.log = shstate->log.buf;
1581 	shstate->log.locale = parents_locale;
1582 	shstate->status = -1;
1583 
1584 	/*
1585 	 * We need a SIGCHLD handler so the sema_wait() below will wake
1586 	 * up if the child dies without doing a sema_post().
1587 	 */
1588 	(void) sigset(SIGCHLD, sigchld);
1589 	/*
1590 	 * We must mask SIGCHLD until after we've coped with the fork
1591 	 * sufficiently to deal with it; otherwise we can race and
1592 	 * receive the signal before pid has been initialized
1593 	 * (yes, this really happens).
1594 	 */
1595 	(void) sigemptyset(&block_cld);
1596 	(void) sigaddset(&block_cld, SIGCHLD);
1597 	(void) sigprocmask(SIG_BLOCK, &block_cld, NULL);
1598 
1599 	if ((ctfd = init_template()) == -1) {
1600 		zerror(zlogp, B_TRUE, "failed to create contract");
1601 		return (1);
1602 	}
1603 
1604 	/*
1605 	 * Do not let another thread localize a message while we are forking.
1606 	 */
1607 	(void) mutex_lock(&msglock);
1608 	pid = fork();
1609 	(void) mutex_unlock(&msglock);
1610 
1611 	/*
1612 	 * In all cases (parent, child, and in the event of an error) we
1613 	 * don't want to cause creation of contracts on subsequent fork()s.
1614 	 */
1615 	(void) ct_tmpl_clear(ctfd);
1616 	(void) close(ctfd);
1617 
1618 	if (pid == -1) {
1619 		zerror(zlogp, B_TRUE, "could not fork");
1620 		return (1);
1621 
1622 	} else if (pid > 0) { /* parent */
1623 		(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
1624 		/*
1625 		 * This marks a window of vulnerability in which we receive
1626 		 * the SIGCLD before falling into sema_wait (normally we would
1627 		 * get woken up from sema_wait with EINTR upon receipt of
1628 		 * SIGCLD).  So we may need to use some other scheme like
1629 		 * sema_posting in the sigcld handler.
1630 		 * blech
1631 		 */
1632 		(void) sema_wait(&shstate->sem);
1633 		(void) sema_destroy(&shstate->sem);
1634 		if (shstate->status != 0)
1635 			(void) waitpid(pid, NULL, WNOHANG);
1636 		/*
1637 		 * It's ok if we die with SIGPIPE.  It's not like we could have
1638 		 * done anything about it.
1639 		 */
1640 		(void) fprintf(stderr, "%s", shstate->log.buf);
1641 		_exit(shstate->status == 0 ? 0 : 1);
1642 	}
1643 
1644 	/*
1645 	 * The child charges on.
1646 	 */
1647 	(void) sigset(SIGCHLD, SIG_DFL);
1648 	(void) sigprocmask(SIG_UNBLOCK, &block_cld, NULL);
1649 
1650 	/*
1651 	 * SIGPIPE can be delivered if we write to a socket for which the
1652 	 * peer endpoint is gone.  That can lead to too-early termination
1653 	 * of zoneadmd, and that's not good eats.
1654 	 */
1655 	(void) sigset(SIGPIPE, SIG_IGN);
1656 	/*
1657 	 * Stop using stderr
1658 	 */
1659 	zlogp = &shstate->log;
1660 
1661 	/*
1662 	 * We don't need stdout/stderr from now on.
1663 	 */
1664 	closefrom(0);
1665 
1666 	/*
1667 	 * Initialize the syslog zlog_t.  This needs to be done after
1668 	 * the call to closefrom().
1669 	 */
1670 	logsys.buf = logsys.log = NULL;
1671 	logsys.buflen = logsys.loglen = 0;
1672 	logsys.logfile = NULL;
1673 	logsys.locale = DEFAULT_LOCALE;
1674 
1675 	openlog("zoneadmd", LOG_PID, LOG_DAEMON);
1676 
1677 	/*
1678 	 * The eventstream is used to publish state changes in the zone
1679 	 * from the door threads to the console I/O poller.
1680 	 */
1681 	if (eventstream_init() == -1) {
1682 		zerror(zlogp, B_TRUE, "unable to create eventstream");
1683 		goto child_out;
1684 	}
1685 
1686 	(void) snprintf(zone_door_path, sizeof (zone_door_path),
1687 	    "%s" ZONE_DOOR_PATH, zonecfg_get_root(), zone_name);
1688 
1689 	/*
1690 	 * See if another zoneadmd is running for this zone.  If not, then we
1691 	 * can now modify system state.
1692 	 */
1693 	if (make_daemon_exclusive(zlogp) == -1)
1694 		goto child_out;
1695 
1696 
1697 	/*
1698 	 * Create/join a new session; we need to be careful of what we do with
1699 	 * the console from now on so we don't end up being the session leader
1700 	 * for the terminal we're going to be handing out.
1701 	 */
1702 	(void) setsid();
1703 
1704 	/*
1705 	 * This thread shouldn't be receiving any signals; in particular,
1706 	 * SIGCHLD should be received by the thread doing the fork().
1707 	 */
1708 	(void) sigfillset(&blockset);
1709 	(void) thr_sigsetmask(SIG_BLOCK, &blockset, NULL);
1710 
1711 	/*
1712 	 * Setup the console device and get ready to serve the console;
1713 	 * once this has completed, we're ready to let console clients
1714 	 * make an attempt to connect (they will block until
1715 	 * serve_console_sock() below gets called, and any pending
1716 	 * connection is accept()ed).
1717 	 */
1718 	if (!zonecfg_in_alt_root() && init_console(zlogp) == -1)
1719 		goto child_out;
1720 
1721 	/*
1722 	 * Take the lock now, so that when the door server gets going, we
1723 	 * are guaranteed that it won't take a request until we are sure
1724 	 * that everything is completely set up.  See the child_out: label
1725 	 * below to see why this matters.
1726 	 */
1727 	(void) mutex_lock(&lock);
1728 
1729 	/* Init semaphore for scratch zones. */
1730 	if (sema_init(&scratch_sem, 0, USYNC_THREAD, NULL) == -1) {
1731 		zerror(zlogp, B_TRUE,
1732 		    "failed to initialize semaphore for scratch zone");
1733 		goto child_out;
1734 	}
1735 
1736 	/*
1737 	 * Note: door setup must occur *after* the console is setup.
1738 	 * This is so that as zlogin tests the door to see if zoneadmd
1739 	 * is ready yet, we know that the console will get serviced
1740 	 * once door_info() indicates that the door is "up".
1741 	 */
1742 	if (setup_door(zlogp) == -1)
1743 		goto child_out;
1744 
1745 	/*
1746 	 * Things seem OK so far; tell the parent process that we're done
1747 	 * with setup tasks.  This will cause the parent to exit, signalling
1748 	 * to zoneadm, zlogin, or whatever forked it that we are ready to
1749 	 * service requests.
1750 	 */
1751 	shstate->status = 0;
1752 	(void) sema_post(&shstate->sem);
1753 	(void) munmap((char *)shstate, shstatelen);
1754 	shstate = NULL;
1755 
1756 	(void) mutex_unlock(&lock);
1757 
1758 	/*
1759 	 * zlogp is now invalid, so reset it to the syslog logger.
1760 	 */
1761 	zlogp = &logsys;
1762 
1763 	/*
1764 	 * Now that we are free of any parents, switch to the default locale.
1765 	 */
1766 	(void) setlocale(LC_ALL, DEFAULT_LOCALE);
1767 
1768 	/*
1769 	 * At this point the setup portion of main() is basically done, so
1770 	 * we reuse this thread to manage the zone console.  When
1771 	 * serve_console() has returned, we are past the point of no return
1772 	 * in the life of this zoneadmd.
1773 	 */
1774 	if (zonecfg_in_alt_root()) {
1775 		/*
1776 		 * This is just awful, but mounted scratch zones don't (and
1777 		 * can't) have consoles.  We just wait for unmount instead.
1778 		 */
1779 		while (sema_wait(&scratch_sem) == EINTR)
1780 			;
1781 	} else {
1782 		serve_console(zlogp);
1783 		assert(in_death_throes);
1784 	}
1785 
1786 	/*
1787 	 * This is the next-to-last part of the exit interlock.  Upon calling
1788 	 * fdetach(), the door will go unreferenced; once any
1789 	 * outstanding requests (like the door thread doing Z_HALT) are
1790 	 * done, the door will get an UNREF notification; when it handles
1791 	 * the UNREF, the door server will cause the exit.
1792 	 */
1793 	assert(!MUTEX_HELD(&lock));
1794 	(void) fdetach(zone_door_path);
1795 	for (;;)
1796 		(void) pause();
1797 
1798 child_out:
1799 	assert(pid == 0);
1800 	if (shstate != NULL) {
1801 		shstate->status = -1;
1802 		(void) sema_post(&shstate->sem);
1803 		(void) munmap((char *)shstate, shstatelen);
1804 	}
1805 
1806 	/*
1807 	 * This might trigger an unref notification, but if so,
1808 	 * we are still holding the lock, so our call to exit will
1809 	 * ultimately win the race and will publish the right exit
1810 	 * code.
1811 	 */
1812 	if (zone_door != -1) {
1813 		assert(MUTEX_HELD(&lock));
1814 		(void) door_revoke(zone_door);
1815 		(void) fdetach(zone_door_path);
1816 	}
1817 	return (1); /* return from main() forcibly exits an MT process */
1818 }
1819