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