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