/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2014 Nexenta Systems, Inc. All rights reserved.
*/
/*
* zoneadmd manages zones; one zoneadmd process is launched for each
* non-global zone on the system. This daemon juggles four jobs:
*
* - Implement setup and teardown of the zone "virtual platform": mount and
* unmount filesystems; create and destroy network interfaces; communicate
* with devfsadmd to lay out devices for the zone; instantiate the zone
* console device; configure process runtime attributes such as resource
* controls, pool bindings, fine-grained privileges.
*
* - Launch the zone's init(1M) process.
*
* - Implement a door server; clients (like zoneadm) connect to the door
* server and request zone state changes. The kernel is also a client of
* this door server. A request to halt or reboot the zone which originates
* *inside* the zone results in a door upcall from the kernel into zoneadmd.
*
* One minor problem is that messages emitted by zoneadmd need to be passed
* back to the zoneadm process making the request. These messages need to
* be rendered in the client's locale; so, this is passed in as part of the
* request. The exception is the kernel upcall to zoneadmd, in which case
* messages are syslog'd.
*
* To make all of this work, the Makefile adds -a to xgettext to extract *all*
* strings, and an exclusion file (zoneadmd.xcl) is used to exclude those
* strings which do not need to be translated.
*
* - Act as a console server for zlogin -C processes; see comments in zcons.c
* for more information about the zone console architecture.
*
* DESIGN NOTES
*
* Restart:
* A chief design constraint of zoneadmd is that it should be restartable in
* the case that the administrator kills it off, or it suffers a fatal error,
* without the running zone being impacted; this is akin to being able to
* reboot the service processor of a server without affecting the OS instance.
*/
#include <sys/sysmacros.h>
#include <bsm/adt_event.h>
#include <alloca.h>
#include <assert.h>
#include <errno.h>
#include <door.h>
#include <fcntl.h>
#include <locale.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <synch.h>
#include <syslog.h>
#include <thread.h>
#include <unistd.h>
#include <wait.h>
#include <limits.h>
#include <zone.h>
#include <libbrand.h>
#include <libcontract.h>
#include <libcontract_priv.h>
#include <libdladm.h>
#include <sys/dls_mgmt.h>
#include <libscf.h>
#include <libzonecfg.h>
#include <zonestat_impl.h>
#include "zoneadmd.h"
static char *progname;
#if !defined(TEXT_DOMAIN) /* should be defined by cc -D */
#endif
static const char *
{
static const char *zcmdstr[] = {
"ready", "boot", "forceboot", "reboot", "halt",
"note_uninstalling", "mount", "forcemount", "unmount",
"shutdown"
};
return ("unknown");
else
}
static char *
{
/* guard against '/' at end of command invocation */
for (;;) {
if (last_slash == NULL) {
break;
} else {
if (*execbasename == '\0') {
*last_slash = '\0';
continue;
}
break;
}
}
return (execbasename);
}
static void
usage(void)
{
exit(2);
}
/* ARGSUSED */
static void
{
}
char *
{
char *out;
(void) mutex_lock(&msglock);
(void) mutex_unlock(&msglock);
return (out);
}
/* PRINTFLIKE3 */
void
{
char *bp;
return;
else
buf[0] = '\0';
/*
* In theory, the locale pointer should be set to either "C" or a
* char array, so it should never be NULL
*/
/* Locale is per process, but we are multi-threaded... */
if (use_strerror)
} else {
}
}
/*
* Emit a warning for any boot arguments which are unrecognized. Since
* Solaris boot arguments are getopt(3c) compatible (see kernel(1m)), we
* put the arguments into an argv style array, use getopt to process them,
* and put the resultant argument string back into outargs.
*
* During the filtering, we pull out any arguments which are truly "boot"
* arguments, leaving only those which are to be passed intact to the
* progenitor process. The one we support at the moment is -i, which
* indicates to the kernel which program should be launched as 'init'.
*
* A return of Z_INVAL indicates specifically that the arguments are
* not valid; this is a non-fatal error. Except for Z_OK, all other return
* values are treated as fatal.
*/
static int
{
int i;
int err;
char c;
/*
* If the user didn't specify transient boot arguments, check
* to see if there were any specified in the zone configuration,
* and use them if applicable.
*/
"getting zone configuration handle");
return (Z_BAD_HANDLE);
}
"invalid configuration snapshot");
return (Z_BAD_HANDLE);
}
sizeof (zonecfg_args));
}
return (Z_INVAL);
}
sargs = scratchargs;
argc++;
}
return (Z_NOMEM);
}
sargs = scratchargs;
i = 0;
goto done;
}
i++;
}
/*
* We preserve compatibility with the Solaris system boot behavior,
* which allows:
*
*
* boot. We don't want reboot in a zone to be gratuitously different,
* so we silently ignore the boot file, if necessary.
*/
goto done;
argc--;
}
optind = 0;
opterr = 0;
switch (c) {
case 'i':
/*
* -i is handled by the runtime and is not passed
* along to userland
*/
break;
case 'f':
/* This has already been processed by zoneadm */
break;
case 'm':
case 's':
/* These pass through unmolested */
break;
case '?':
/*
* We warn about unknown arguments but pass them
* along anyway-- if someone wants to develop their
* own init replacement, they can pass it whatever
* args they want.
*/
break;
}
}
/*
* For Solaris Zones we warn about and discard non-option arguments.
* Hence 'boot foo bar baz gub' --> 'boot'. However, to be similar
* to the kernel, we concat up all the other remaining boot args.
* and warn on them as a group.
*/
optind++;
}
"arguments `%s'.", badarg);
}
done:
for (i = 0; i < argc_save; i++) {
}
return (err);
}
static int
{
/*
* We must create and lock everyone but root out of ZONES_TMPDIR
* since anyone can open any UNIX domain socket, regardless of
* its file system permissions. Sigh...
*/
return (-1);
}
/* paranoia */
return (-1);
}
return (0);
}
/*
* Run the brand's pre-state change callback, if it exists.
*/
static int
{
const char *altroot;
if (pre_statechg_hook[0] == '\0')
return (0);
altroot = zonecfg_get_root();
return (-1);
return (-1);
return (0);
}
/*
* Run the brand's post-state change callback, if it exists.
*/
static int
{
const char *altroot;
if (post_statechg_hook[0] == '\0')
return (0);
altroot = zonecfg_get_root();
return (-1);
return (-1);
return (0);
}
/*
* Notify zonestatd of the new zone. If zonestatd is not running, this
* will do nothing.
*/
static void
{
int fd;
if (fd < 0)
return;
cmd[0] = ZSD_CMD_NEW_ZONE;
}
/*
* Bring a zone up to the pre-boot "ready" stage. The mount_cmd argument is
* 'true' if this is being invoked as part of the processing for the "mount"
* subcommand.
*/
static int
{
int err;
return (-1);
goto bad;
}
goto bad;
}
goto bad;
}
goto bad;
return (0);
bad:
/*
* If something goes wrong, we up the zones's state to the target
* state, READY, and then invoke the hook as if we're halting.
*/
return (-1);
}
int
init_template(void)
{
int fd;
int err = 0;
if (fd == -1)
return (-1);
/*
* For now, zoneadmd doesn't do anything with the contract.
* Deliver no events, don't inherit, and allow it to be orphaned.
*/
return (-1);
}
return (fd);
}
typedef struct fs_callback {
static int
{
int child_status;
int tmpl_fd;
int rv;
/* determine the zone rootpath */
if (mount_cmd) {
return (-1);
}
} else {
return (-1);
}
}
return (-1);
} else if (rv > 0) {
/* The mount point path doesn't exist, create it now. */
DEFAULT_DIR_GROUP) != 0) {
return (-1);
}
/*
* Now this might seem weird, but we need to invoke
* valid_mount_path() again. Why? Because it checks
* to make sure that the mount point path is canonical,
* which it can only do if the path exists, so now that
* we've created the path we have to verify it again.
*/
fstype)) < 0) {
return (-1);
}
}
return (-1);
}
(void) ct_tmpl_clear(tmpl_fd);
return (-1);
} else if (child == 0) { /* child */
int optlen = 0;
(void) ct_tmpl_clear(tmpl_fd);
/*
* Even though there are no procs running in the zone, we
* do this for paranoia's sake.
*/
(void) closefrom(0);
}
/*
* The mount() system call is incredibly annoying.
* If options are specified, we need to copy them
* into a temporary buffer since the mount() system
* call will overwrite the options string. It will
* also fail if the new option string it wants to
* write is bigger than the one we passed in, so
* you must pass in a buffer of the maximum possible
* option string length. sigh.
*/
}
_exit(0);
}
/* parent */
ct = -1;
(void) ct_tmpl_clear(tmpl_fd);
/* unexpected: we must have been signalled */
(void) contract_abandon_id(ct);
return (-1);
}
(void) contract_abandon_id(ct);
if (WEXITSTATUS(child_status) != 0) {
return (-1);
}
return (0);
}
/*
* If retstr is not NULL, the output of the subproc is returned in the str,
* otherwise it is output using zerror(). Any memory allocated for retstr
* should be freed by the caller.
*/
int
{
char *inbuf;
int status;
int rd_cnt;
return (-1);
}
rd_cnt = 0;
} else {
}
return (-1);
}
} else {
char *p;
return (-1);
}
*retstr = p;
}
}
if (WIFSIGNALED(status)) {
return (-1);
}
return (-1);
}
return (WEXITSTATUS(status));
}
static int
{
int err;
return (-1);
goto bad;
}
/* Get a handle to the brand info for this zone */
goto bad;
}
/*
* Get the list of filesystems to mount from the brand
* configuration. These mounts are done via a thread that will
* enter the zone, so they are done from within the context of the
* zone.
*/
goto bad;
}
/*
* Get the brand's boot callback if it exists.
*/
goto bad;
}
"unable to determine branded zone's boot callback");
goto bad;
}
/* Get the path for this zone's init(1M) (or equivalent) process. */
"unable to determine zone's init(1M) location");
goto bad;
}
/* See if this zone's brand should restart init if it dies. */
goto bad;
/* Try to anticipate possible problems: Make sure init is executable. */
goto bad;
}
goto bad;
}
goto bad;
}
/*
* Exclusive stack zones interact with the dlmgmtd running in the
* global zone. dladm_zone_boot() tells dlmgmtd that this zone is
* booting, and loads its datalinks from the zone's datalink
* configuration file.
*/
if (status != DLADM_STATUS_OK) {
goto bad;
}
}
/*
* If there is a brand 'boot' callback, execute it now to give the
* brand one last chance to do any additional setup before the zone
* is booted.
*/
goto bad;
}
goto bad;
}
goto bad;
}
NULL, 0) == -1) {
goto bad;
}
/*
* Inform zonestatd of a new zone so that it can install a door for
* the zone to contact it.
*/
goto bad;
}
goto bad;
return (0);
bad:
/*
* If something goes wrong, we up the zones's state to the target
* state, RUNNING, and then invoke the hook as if we're halting.
*/
if (links_loaded)
return (-1);
}
static int
{
int err;
return (-1);
if (!bringup_failure_recovery)
return (-1);
}
return (-1);
return (0);
}
static int
{
int tmpl_fd;
int child_status;
if (shutdown_in_progress) {
return (-1);
}
return (-1);
}
/* Get a handle to the brand info for this zone */
return (-1);
}
return (-1);
}
/*
* If there is a brand 'shutdown' callback, execute it now to give the
* brand a chance to cleanup any custom configuration.
*/
}
return (-1);
}
(void) ct_tmpl_clear(tmpl_fd);
return (-1);
} else if (child == 0) {
(void) ct_tmpl_clear(tmpl_fd);
}
}
ct = -1;
(void) ct_tmpl_clear(tmpl_fd);
/* unexpected: we must have been signalled */
(void) contract_abandon_id(ct);
return (-1);
}
(void) contract_abandon_id(ct);
if (WEXITSTATUS(child_status) != 0) {
return (-1);
}
return (0);
}
static int
{
int timeout;
int tries;
/* Get default stop timeout from SMF framework */
SCF_PROPERTY_TIMEOUT)) != NULL) {
if (tm != 0)
}
}
/* allow time for zone to shutdown cleanly */
(void) sleep(1);
zstate == ZONE_STATE_INSTALLED) {
rc = 0;
break;
}
}
if (rc != 0)
return (rc);
}
/*
* Generate AUE_zone_state for a command that boots a zone.
*/
static void
char *new_state)
{
if (!adt_audit_enabled())
return;
if (return_val == 0) {
} else {
}
return;
}
(void) adt_end_session(ah);
return;
}
(void) adt_end_session(ah);
return;
}
(void) adt_end_session(ah);
}
/*
* The main routine for the door server that deals with zone state transitions.
*/
/* ARGSUSED */
static void
{
/* LINTED E_BAD_PTR_CAST_ALIGN */
/*
* When we get the door unref message, we've fdetach'd the door, and
* it is time for us to shut down zoneadmd.
*/
if (zargp == DOOR_UNREF_DATA) {
/*
* See comment at end of main() for info on the last rites.
*/
exit(0);
}
(void) door_return(NULL, 0, 0, 0);
}
/* defer initialization of zlog.locale until after credential check */
if (alen != sizeof (zone_cmd_arg_t)) {
/*
* This really shouldn't be happening.
*/
"unexpected (expected %d bytes)", alen,
sizeof (zone_cmd_arg_t));
goto out;
}
if (door_ucred(&uc) != 0) {
goto out;
}
ucred_geteuid(uc) != 0)) {
goto out;
}
/*
* This is safe because we only use a zlog_t throughout the
* duration of a door call; i.e., by the time the pointer
* might become invalid, the door call would be over.
*/
(void) mutex_lock(&lock);
/*
* Once we start to really die off, we don't want more connections.
*/
if (in_death_throes) {
(void) mutex_unlock(&lock);
ucred_free(uc);
(void) door_return(NULL, 0, 0, 0);
}
/*
* Check for validity of command.
*/
goto out;
}
/*
* Can't happen
*/
cmd);
goto out;
}
/*
* We ignore the possibility of someone calling zone_create(2)
* explicitly; all requests must come through zoneadmd.
*/
/*
* Something terribly wrong happened
*/
goto out;
}
if (kernelcall) {
/*
* Kernel-initiated requests may lose their validity if the
* zone_t the kernel was referring to has gone away.
*/
/*
* We're not talking about the same zone. The request
* must have arrived too late. Return error.
*/
rval = -1;
goto out;
}
}
/*
* If we are being asked to forcibly mount or boot a zone, we
* pretend that an INCOMPLETE zone is actually INSTALLED.
*/
if (zstate == ZONE_STATE_INCOMPLETE &&
switch (zstate) {
case ZONE_STATE_CONFIGURED:
case ZONE_STATE_INCOMPLETE:
/*
* Not our area of expertise; we just print a nice message
* and die off.
*/
"%s operation is invalid for zones in state '%s'",
break;
case ZONE_STATE_INSTALLED:
switch (cmd) {
case Z_READY:
if (rval == 0)
break;
case Z_BOOT:
case Z_FORCEBOOT:
== 0) {
zstate);
}
if (rval != 0) {
zstate);
}
break;
case Z_SHUTDOWN:
case Z_HALT:
if (kernelcall) /* Invalid; can't happen */
abort();
/*
* We could have two clients racing to halt this
* zone; the second client loses, but his request
* doesn't fail, since the zone is now in the desired
* state.
*/
rval = 0;
break;
case Z_REBOOT:
if (kernelcall) /* Invalid; can't happen */
abort();
rval = -1;
break;
case Z_NOTE_UNINSTALLING:
if (kernelcall) /* Invalid; can't happen */
abort();
/*
* Tell the console to print out a message about this.
* Once it does, we will be in_death_throes.
*/
break;
case Z_MOUNT:
case Z_FORCEMOUNT:
if (kernelcall) /* Invalid; can't happen */
abort();
if (!zone_isnative && !zone_iscluster &&
!zone_islabeled) {
/*
* -U mounts the zone without lofs mounting
* zone file systems back into the scratch
* zone. This is required when mounting
* non-native branded zones.
*/
}
if (rval != 0)
break;
/*
* Get a handle to the default brand info.
* We must always use the default brand file system
* list when mounting the zone.
*/
rval = -1;
break;
}
/*
* Get the list of filesystems to mount from
* the brand configuration. These mounts are done
* via a thread that will enter the zone, so they
* are done from within the context of the zone.
*/
mount_early_fs, &cb);
/*
* by svc:/system/filesystem/usr:default, but since
* we're not booting the zone, we need to do this
* manually.
*/
if (rval == 0)
break;
case Z_UNMOUNT:
if (kernelcall) /* Invalid; can't happen */
abort();
rval = 0;
break;
}
break;
case ZONE_STATE_READY:
switch (cmd) {
case Z_READY:
/*
* We could have two clients racing to ready this
* zone; the second client loses, but his request
* doesn't fail, since the zone is now in the desired
* state.
*/
rval = 0;
break;
case Z_BOOT:
sizeof (boot_args));
if (rval != 0) {
zstate);
}
boot_args[0] = '\0';
break;
case Z_HALT:
if (kernelcall) /* Invalid; can't happen */
abort();
!= 0)
break;
break;
case Z_SHUTDOWN:
case Z_REBOOT:
case Z_NOTE_UNINSTALLING:
case Z_MOUNT:
case Z_UNMOUNT:
if (kernelcall) /* Invalid; can't happen */
abort();
rval = -1;
break;
}
break;
case ZONE_STATE_MOUNTED:
switch (cmd) {
case Z_UNMOUNT:
if (kernelcall) /* Invalid; can't happen */
abort();
if (rval == 0) {
(void) sema_post(&scratch_sem);
}
break;
default:
if (kernelcall) /* Invalid; can't happen */
abort();
rval = -1;
break;
}
break;
case ZONE_STATE_RUNNING:
case ZONE_STATE_SHUTTING_DOWN:
case ZONE_STATE_DOWN:
switch (cmd) {
case Z_READY:
!= 0)
break;
else
break;
case Z_BOOT:
/*
* We could have two clients racing to boot this
* zone; the second client loses, but his request
* doesn't fail, since the zone is now in the desired
* state.
*/
rval = 0;
break;
case Z_HALT:
!= 0)
break;
break;
case Z_REBOOT:
sizeof (boot_args));
!= 0) {
boot_args[0] = '\0';
break;
}
!= 0) {
boot_args[0] = '\0';
break;
}
if (rval != 0) {
zstate);
}
boot_args[0] = '\0';
break;
case Z_SHUTDOWN:
}
break;
case Z_NOTE_UNINSTALLING:
case Z_MOUNT:
case Z_UNMOUNT:
rval = -1;
break;
}
break;
default:
abort();
}
/*
* Because the state of the zone may have changed, we make sure
* to wake the console poller, which is in charge of initiating
* the shutdown procedure as necessary.
*/
out:
(void) mutex_unlock(&lock);
/* Wait for the Z_SHUTDOWN commands to complete */
if (wait_shut)
if (kernelcall) {
rlen = 0;
} else {
}
ucred_free(uc);
}
static int
{
return (-1);
}
(void) fdetach(zone_door_path);
(void) door_revoke(zone_door);
(void) fdetach(zone_door_path);
zone_door = -1;
return (-1);
}
return (0);
}
/*
* zoneadm(1m) will start zoneadmd if it thinks it isn't running; this
* is where zoneadmd itself will check to see that another instance of
* zoneadmd isn't already controlling this zone.
*
* The idea here is that we want to open the path to which we will
* attach our door, lock it, and then make sure that no-one has beat us
* to fattach(3c)ing onto it.
*
* fattach(3c) is really a mount, so there are actually two possible
* vnodes we could be dealing with. Our strategy is as follows:
*
* - If the file we opened is a regular file (common case):
* There is no fattach(3c)ed door, so we have a chance of becoming
* the managing zoneadmd. We attempt to lock the file: if it is
* already locked, that means someone else raced us here, so we
* lose and give up. zoneadm(1m) will try to contact the zoneadmd
* that beat us to it.
*
* - If the file we opened is a namefs file:
* This means there is already an established door fattach(3c)'ed
* to the rendezvous path. We've lost the race, so we give up.
* Note that in this case we also try to grab the file lock, and
* will succeed in acquiring it since the vnode locked by the
* "winning" zoneadmd was a regular one, and the one we locked was
* the fattach(3c)'ed door node. At any rate, no harm is done, and
* we just return to zoneadm(1m) which knows to retry.
*/
static int
{
top:
goto out;
}
goto out;
}
goto out;
}
/*
* Lock the file to synchronize with other zoneadmd
*/
/*
* Someone else raced us here and grabbed the lock file
* first. A warning here is inappropriate since nothing
* went wrong.
*/
goto out;
}
/*
* There is already something fattach()'ed to this file.
* Lets see what the door is up to.
*/
/*
* Another zoneadmd process seems to be in
* control of the situation and we don't need to
* be here. A warning here is inappropriate
* since nothing went wrong.
*
* If the door has been revoked, the zoneadmd
* process currently managing the zone is going
* away. We'll return control to zoneadm(1m)
* which will try again (by which time zoneadmd
* will hopefully have exited).
*/
goto out;
}
/*
* If we got this far, there's a fattach(3c)'ed door
* that belongs to a process that has exited, which can
* happen if the previous zoneadmd died unexpectedly.
*
* Let user know that something is amiss, but that we can
* recover; if the zone is in the installed state, then don't
* message, since having a running zoneadmd isn't really
* limited to times when zoneadmd is picking back up from a
* zoneadmd that died while the zone was in some non-trivial
* state.
*/
if (zstate > ZONE_STATE_INSTALLED) {
"zone '%s': WARNING: zone is in state '%s', but "
"zoneadmd does not appear to be available; "
"restarted zoneadmd to recover.",
}
(void) fdetach(zone_door_path);
goto top;
}
ret = 0;
out:
return (ret);
}
/*
* Setup the brand's pre and post state change callbacks, as well as the
* query callback, if any of these exist.
*/
static int
{
return (-1);
sizeof (pre_statechg_hook));
sizeof (pre_statechg_hook) - EXEC_LEN) != 0)
return (-1);
pre_statechg_hook[0] = '\0';
sizeof (post_statechg_hook));
sizeof (post_statechg_hook) - EXEC_LEN) != 0)
return (-1);
post_statechg_hook[0] = '\0';
sizeof (query_hook));
sizeof (query_hook) - EXEC_LEN) != 0)
return (-1);
query_hook[0] = '\0';
return (0);
}
int
{
int opt;
int err;
struct {
int status;
} *shstate;
int ctfd;
/*
* Make sure stderr is unbuffered
*/
/*
* Get out of the way of mounted filesystems, since we will daemonize
* soon.
*/
(void) chdir("/");
/*
* Use the default system umask per PSARC 1998/110 rather than
* anything that may have been set by the caller.
*/
/*
* Initially we want to use our parent's locale.
*/
(void) textdomain(TEXT_DOMAIN);
sizeof (parents_locale));
/*
* This zlog_t is used for writing to stderr
*/
/*
* We start off writing to stderr until we're ready to daemonize.
*/
/*
* Process options.
*/
switch (opt) {
case 'R':
break;
case 'z':
break;
default:
usage();
}
}
usage();
/*
* Because usage() prints directly to stderr, it has gettext()
* wrapping, which depends on the locale. But since zerror() calls
* localize() which tweaks the locale, it is not safe to call zerror()
* until after the last call to usage(). Fortunately, the last call
* to usage() is just above and the first call to zerror() is just
* below. Don't mess this up.
*/
return (1);
}
return (1);
}
return (1);
}
if (zstate < ZONE_STATE_INCOMPLETE) {
"cannot manage a zone which is in state '%s'",
return (1);
}
sizeof (default_brand)) != Z_OK) {
return (1);
}
/* Get a handle to the brand info for this zone */
!= Z_OK) {
return (1);
}
/*
* In the alternate root environment, the only supported
* operations are mount and unmount. In this case, just treat
* the zone as native if it is cluster. Cluster zones can be
* native for the purpose of LU or upgrade, and the cluster
* brand may not exist in the miniroot (such as in net install
* upgrade).
*/
if (zonecfg_in_alt_root()) {
sizeof (brand_name));
}
} else {
}
return (1);
}
/* Get state change brand hooks. */
"failed to initialize brand state change hooks");
return (1);
}
/*
* Check that we have all privileges. It would be nice to pare
* this down, but this is at least a first cut.
*/
return (1);
}
return (1);
}
"run this command (all privs required)");
return (1);
}
return (1);
/*
* Pre-fork: setup shared state
*/
MAP_FAILED) {
return (1);
}
return (1);
}
/*
* We need a SIGCHLD handler so the sema_wait() below will wake
* up if the child dies without doing a sema_post().
*/
/*
* We must mask SIGCHLD until after we've coped with the fork
* sufficiently to deal with it; otherwise we can race and
* receive the signal before pid has been initialized
* (yes, this really happens).
*/
(void) sigemptyset(&block_cld);
/*
* The parent only needs stderr after the fork, so close other fd's
* that we inherited from zoneadm so that the parent doesn't have those
* open while waiting. The child will close the rest after the fork.
*/
closefrom(3);
return (1);
}
/*
* Do not let another thread localize a message while we are forking.
*/
(void) mutex_lock(&msglock);
(void) mutex_unlock(&msglock);
/*
* In all cases (parent, child, and in the event of an error) we
* don't want to cause creation of contracts on subsequent fork()s.
*/
(void) ct_tmpl_clear(ctfd);
if (pid == -1) {
return (1);
} else if (pid > 0) { /* parent */
/*
* This marks a window of vulnerability in which we receive
* the SIGCLD before falling into sema_wait (normally we would
* get woken up from sema_wait with EINTR upon receipt of
* SIGCLD). So we may need to use some other scheme like
* sema_posting in the sigcld handler.
* blech
*/
/*
* It's ok if we die with SIGPIPE. It's not like we could have
* done anything about it.
*/
}
/*
* The child charges on.
*/
/*
* SIGPIPE can be delivered if we write to a socket for which the
* peer endpoint is gone. That can lead to too-early termination
* of zoneadmd, and that's not good eats.
*/
/*
* Stop using stderr
*/
/*
*/
closefrom(0);
/*
* Initialize the syslog zlog_t. This needs to be done after
* the call to closefrom().
*/
/*
* The eventstream is used to publish state changes in the zone
* from the door threads to the console I/O poller.
*/
if (eventstream_init() == -1) {
goto child_out;
}
/*
* See if another zoneadmd is running for this zone. If not, then we
* can now modify system state.
*/
goto child_out;
/*
* the console from now on so we don't end up being the session leader
* for the terminal we're going to be handing out.
*/
(void) setsid();
/*
* This thread shouldn't be receiving any signals; in particular,
* SIGCHLD should be received by the thread doing the fork().
*/
(void) sigfillset(&blockset);
/*
* Setup the console device and get ready to serve the console;
* once this has completed, we're ready to let console clients
* make an attempt to connect (they will block until
* serve_console_sock() below gets called, and any pending
* connection is accept()ed).
*/
goto child_out;
/*
* Take the lock now, so that when the door server gets going, we
* are guaranteed that it won't take a request until we are sure
* that everything is completely set up. See the child_out: label
* below to see why this matters.
*/
(void) mutex_lock(&lock);
/* Init semaphore for scratch zones. */
"failed to initialize semaphore for scratch zone");
goto child_out;
}
/* open the dladm handle */
goto child_out;
}
/*
* Note: door setup must occur *after* the console is setup.
* This is so that as zlogin tests the door to see if zoneadmd
* is ready yet, we know that the console will get serviced
* once door_info() indicates that the door is "up".
*/
goto child_out;
/*
* Things seem OK so far; tell the parent process that we're done
* with setup tasks. This will cause the parent to exit, signalling
* to zoneadm, zlogin, or whatever forked it that we are ready to
* service requests.
*/
(void) mutex_unlock(&lock);
/*
* zlogp is now invalid, so reset it to the syslog logger.
*/
/*
* Now that we are free of any parents, switch to the default locale.
*/
/*
* At this point the setup portion of main() is basically done, so
* we reuse this thread to manage the zone console. When
* serve_console() has returned, we are past the point of no return
* in the life of this zoneadmd.
*/
if (zonecfg_in_alt_root()) {
/*
* This is just awful, but mounted scratch zones don't (and
* can't) have consoles. We just wait for unmount instead.
*/
;
} else {
}
/*
* This is the next-to-last part of the exit interlock. Upon calling
* fdetach(), the door will go unreferenced; once any
* outstanding requests (like the door thread doing Z_HALT) are
* done, the door will get an UNREF notification; when it handles
* the UNREF, the door server will cause the exit. It's possible
* that fdetach() can fail because the file is in use, in which
* case we'll retry the operation.
*/
for (;;) {
break;
yield();
}
for (;;)
(void) pause();
}
/*
* This might trigger an unref notification, but if so,
* we are still holding the lock, so our call to exit will
* ultimately win the race and will publish the right exit
* code.
*/
if (zone_door != -1) {
(void) door_revoke(zone_door);
(void) fdetach(zone_door_path);
}
if (dld_handle != NULL)
return (1); /* return from main() forcibly exits an MT process */
}