xref: /onnv-gate/usr/src/cmd/svc/startd/startd.c (revision 5777:e3276fcb93e7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51914Scasper  * Common Development and Distribution License (the "License").
61914Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
215040Swesolows 
220Sstevel@tonic-gate /*
23*5777Stw21770  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * startd.c - the master restarter
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * svc.startd comprises two halves.  The graph engine is based in graph.c and
330Sstevel@tonic-gate  * maintains the service dependency graph based on the information in the
340Sstevel@tonic-gate  * repository.  For each service it also tracks the current state and the
350Sstevel@tonic-gate  * restarter responsible for the service.  Based on the graph, events from the
360Sstevel@tonic-gate  * repository (mostly administrative requests from svcadm), and messages from
370Sstevel@tonic-gate  * the restarters, the graph engine makes decisions about how the services
380Sstevel@tonic-gate  * should be manipulated and sends commands to the appropriate restarters.
390Sstevel@tonic-gate  * Communication between the graph engine and the restarters is embodied in
400Sstevel@tonic-gate  * protocol.c.
410Sstevel@tonic-gate  *
420Sstevel@tonic-gate  * The second half of svc.startd is the restarter for services managed by
430Sstevel@tonic-gate  * svc.startd and is primarily contained in restarter.c.  It responds to graph
440Sstevel@tonic-gate  * engine commands by executing methods, updating the repository, and sending
450Sstevel@tonic-gate  * feedback (mostly state updates) to the graph engine.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * Error handling
480Sstevel@tonic-gate  *
490Sstevel@tonic-gate  * In general, when svc.startd runs out of memory it reattempts a few times,
500Sstevel@tonic-gate  * sleeping inbetween, before giving up and exiting (see startd_alloc_retry()).
510Sstevel@tonic-gate  * When a repository connection is broken (libscf calls fail with
520Sstevel@tonic-gate  * SCF_ERROR_CONNECTION_BROKEN, librestart and internal functions return
530Sstevel@tonic-gate  * ECONNABORTED), svc.startd calls libscf_rebind_handle(), which coordinates
540Sstevel@tonic-gate  * with the svc.configd-restarting thread, fork_configd_thread(), via
550Sstevel@tonic-gate  * st->st_configd_live_cv, and rebinds the repository handle.  Doing so resets
560Sstevel@tonic-gate  * all libscf state associated with that handle, so functions which do this
570Sstevel@tonic-gate  * should communicate the event to their callers (usually by returning
580Sstevel@tonic-gate  * ECONNRESET) so they may reset their state appropriately.
59*5777Stw21770  *
60*5777Stw21770  * External references
61*5777Stw21770  *
62*5777Stw21770  * svc.configd generates special security audit events for changes to some
63*5777Stw21770  * restarter related properties.  See the special_props_list array in
64*5777Stw21770  * usr/src/cmd/svc/configd/rc_node.c for the properties that cause these audit
65*5777Stw21770  * events.  If you change the semantics of these propereties within startd, you
66*5777Stw21770  * will probably need to update rc_node.c
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include <stdio.h>
701914Scasper #include <stdio_ext.h>
710Sstevel@tonic-gate #include <sys/mnttab.h>		/* uses FILE * without including stdio.h */
720Sstevel@tonic-gate #include <alloca.h>
730Sstevel@tonic-gate #include <sys/mount.h>
740Sstevel@tonic-gate #include <sys/stat.h>
750Sstevel@tonic-gate #include <sys/types.h>
760Sstevel@tonic-gate #include <sys/wait.h>
770Sstevel@tonic-gate #include <assert.h>
780Sstevel@tonic-gate #include <errno.h>
790Sstevel@tonic-gate #include <fcntl.h>
800Sstevel@tonic-gate #include <ftw.h>
810Sstevel@tonic-gate #include <libintl.h>
820Sstevel@tonic-gate #include <libscf.h>
830Sstevel@tonic-gate #include <libscf_priv.h>
840Sstevel@tonic-gate #include <libuutil.h>
850Sstevel@tonic-gate #include <locale.h>
860Sstevel@tonic-gate #include <poll.h>
870Sstevel@tonic-gate #include <pthread.h>
880Sstevel@tonic-gate #include <signal.h>
890Sstevel@tonic-gate #include <stdarg.h>
900Sstevel@tonic-gate #include <stdlib.h>
910Sstevel@tonic-gate #include <string.h>
920Sstevel@tonic-gate #include <strings.h>
930Sstevel@tonic-gate #include <unistd.h>
940Sstevel@tonic-gate 
950Sstevel@tonic-gate #include "startd.h"
960Sstevel@tonic-gate #include "protocol.h"
970Sstevel@tonic-gate 
980Sstevel@tonic-gate ssize_t max_scf_name_size;
990Sstevel@tonic-gate ssize_t max_scf_fmri_size;
1000Sstevel@tonic-gate ssize_t max_scf_value_size;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate mode_t fmask;
1030Sstevel@tonic-gate mode_t dmask;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate graph_update_t *gu;
1060Sstevel@tonic-gate restarter_update_t *ru;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate startd_state_t *st;
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate boolean_t booting_to_single_user = B_FALSE;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate const char * const admin_actions[] = {
1130Sstevel@tonic-gate     SCF_PROPERTY_DEGRADED,
1140Sstevel@tonic-gate     SCF_PROPERTY_MAINT_OFF,
1150Sstevel@tonic-gate     SCF_PROPERTY_MAINT_ON,
1160Sstevel@tonic-gate     SCF_PROPERTY_MAINT_ON_IMMEDIATE,
1170Sstevel@tonic-gate     SCF_PROPERTY_REFRESH,
1180Sstevel@tonic-gate     SCF_PROPERTY_RESTART
1190Sstevel@tonic-gate };
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate const int admin_events[NACTIONS] = {
1220Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_DEGRADED,
1230Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF,
1240Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON,
1250Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE,
1260Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_REFRESH,
1270Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_RESTART
1280Sstevel@tonic-gate };
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate const char * const instance_state_str[] = {
1310Sstevel@tonic-gate 	"none",
1320Sstevel@tonic-gate 	"uninitialized",
1330Sstevel@tonic-gate 	"maintenance",
1340Sstevel@tonic-gate 	"offline",
1350Sstevel@tonic-gate 	"disabled",
1360Sstevel@tonic-gate 	"online",
1370Sstevel@tonic-gate 	"degraded"
1380Sstevel@tonic-gate };
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate static int finished = 0;
1410Sstevel@tonic-gate static int opt_reconfig = 0;
1420Sstevel@tonic-gate static uint8_t prop_reconfig = 0;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate #define	INITIAL_REBIND_ATTEMPTS	5
1450Sstevel@tonic-gate #define	INITIAL_REBIND_DELAY	3
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate pthread_mutexattr_t mutex_attrs;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate const char *
1500Sstevel@tonic-gate _umem_debug_init(void)
1510Sstevel@tonic-gate {
1520Sstevel@tonic-gate 	return ("default,verbose");	/* UMEM_DEBUG setting */
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate const char *
1560Sstevel@tonic-gate _umem_logging_init(void)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate 	return ("fail,contents");	/* UMEM_LOGGING setting */
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate /*
1620Sstevel@tonic-gate  * startd_alloc_retry()
1630Sstevel@tonic-gate  *   Wrapper for allocation functions.  Retries with a decaying time
1640Sstevel@tonic-gate  *   value on failure to allocate, and aborts startd if failure is
1650Sstevel@tonic-gate  *   persistent.
1660Sstevel@tonic-gate  */
1670Sstevel@tonic-gate void *
1680Sstevel@tonic-gate startd_alloc_retry(void *f(size_t, int), size_t sz)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate 	void *p;
1710Sstevel@tonic-gate 	uint_t try, msecs;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	p = f(sz, UMEM_DEFAULT);
1740Sstevel@tonic-gate 	if (p != NULL || sz == 0)
1750Sstevel@tonic-gate 		return (p);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	for (try = 0; p == NULL && try < ALLOC_RETRY; ++try) {
1800Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
1810Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
1820Sstevel@tonic-gate 		p = f(sz, UMEM_DEFAULT);
1830Sstevel@tonic-gate 		if (p != NULL)
1840Sstevel@tonic-gate 			return (p);
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
1880Sstevel@tonic-gate 	/* NOTREACHED */
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate void *
1920Sstevel@tonic-gate safe_realloc(void *p, size_t sz)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	uint_t try, msecs;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	p = realloc(p, sz);
1970Sstevel@tonic-gate 	if (p != NULL || sz == 0)
1980Sstevel@tonic-gate 		return (p);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	for (try = 0; errno == EAGAIN && try < ALLOC_RETRY; ++try) {
2030Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
2040Sstevel@tonic-gate 		p = realloc(p, sz);
2050Sstevel@tonic-gate 		if (p != NULL)
2060Sstevel@tonic-gate 			return (p);
2070Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
2110Sstevel@tonic-gate 	/* NOTREACHED */
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate char *
2150Sstevel@tonic-gate safe_strdup(const char *s)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	uint_t try, msecs;
2180Sstevel@tonic-gate 	char *d;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	d = strdup(s);
2210Sstevel@tonic-gate 	if (d != NULL)
2220Sstevel@tonic-gate 		return (d);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	for (try = 0;
2270Sstevel@tonic-gate 	    (errno == EAGAIN || errno == ENOMEM) && try < ALLOC_RETRY;
2280Sstevel@tonic-gate 	    ++try) {
2290Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
2300Sstevel@tonic-gate 		d = strdup(s);
2310Sstevel@tonic-gate 		if (d != NULL)
2320Sstevel@tonic-gate 			return (d);
2330Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
2370Sstevel@tonic-gate 	/* NOTREACHED */
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate void
2420Sstevel@tonic-gate startd_free(void *p, size_t sz)
2430Sstevel@tonic-gate {
2440Sstevel@tonic-gate 	umem_free(p, sz);
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate  * Creates a uu_list_pool_t with the same retry policy as startd_alloc().
2490Sstevel@tonic-gate  * Only returns NULL for UU_ERROR_UNKNOWN_FLAG and UU_ERROR_NOT_SUPPORTED.
2500Sstevel@tonic-gate  */
2510Sstevel@tonic-gate uu_list_pool_t *
2520Sstevel@tonic-gate startd_list_pool_create(const char *name, size_t e, size_t o,
2530Sstevel@tonic-gate     uu_compare_fn_t *f, uint32_t flags)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate 	uu_list_pool_t *pool;
2560Sstevel@tonic-gate 	uint_t try, msecs;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	pool = uu_list_pool_create(name, e, o, f, flags);
2590Sstevel@tonic-gate 	if (pool != NULL)
2600Sstevel@tonic-gate 		return (pool);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	for (try = 0; uu_error() == UU_ERROR_NO_MEMORY && try < ALLOC_RETRY;
2650Sstevel@tonic-gate 	    ++try) {
2660Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
2670Sstevel@tonic-gate 		pool = uu_list_pool_create(name, e, o, f, flags);
2680Sstevel@tonic-gate 		if (pool != NULL)
2690Sstevel@tonic-gate 			return (pool);
2700Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	if (try < ALLOC_RETRY)
2740Sstevel@tonic-gate 		return (NULL);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
2770Sstevel@tonic-gate 	/* NOTREACHED */
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate  * Creates a uu_list_t with the same retry policy as startd_alloc().  Only
2820Sstevel@tonic-gate  * returns NULL for UU_ERROR_UNKNOWN_FLAG and UU_ERROR_NOT_SUPPORTED.
2830Sstevel@tonic-gate  */
2840Sstevel@tonic-gate uu_list_t *
2850Sstevel@tonic-gate startd_list_create(uu_list_pool_t *pool, void *parent, uint32_t flags)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate 	uu_list_t *list;
2880Sstevel@tonic-gate 	uint_t try, msecs;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	list = uu_list_create(pool, parent, flags);
2910Sstevel@tonic-gate 	if (list != NULL)
2920Sstevel@tonic-gate 		return (list);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	for (try = 0; uu_error() == UU_ERROR_NO_MEMORY && try < ALLOC_RETRY;
2970Sstevel@tonic-gate 	    ++try) {
2980Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
2990Sstevel@tonic-gate 		list = uu_list_create(pool, parent, flags);
3000Sstevel@tonic-gate 		if (list != NULL)
3010Sstevel@tonic-gate 			return (list);
3020Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
3030Sstevel@tonic-gate 	}
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	if (try < ALLOC_RETRY)
3060Sstevel@tonic-gate 		return (NULL);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
3090Sstevel@tonic-gate 	/* NOTREACHED */
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate pthread_t
3130Sstevel@tonic-gate startd_thread_create(void *(*func)(void *), void *ptr)
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate 	int err;
3160Sstevel@tonic-gate 	pthread_t tid;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	err = pthread_create(&tid, NULL, func, ptr);
3190Sstevel@tonic-gate 	if (err != 0) {
3200Sstevel@tonic-gate 		assert(err == EAGAIN);
3210Sstevel@tonic-gate 		uu_die("Could not create thread.\n");
3220Sstevel@tonic-gate 	}
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	err = pthread_detach(tid);
3250Sstevel@tonic-gate 	assert(err == 0);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	return (tid);
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate static int
3321958Slianep read_startd_config(void)
3330Sstevel@tonic-gate {
3340Sstevel@tonic-gate 	scf_handle_t *hndl;
3350Sstevel@tonic-gate 	scf_instance_t *inst;
3360Sstevel@tonic-gate 	scf_propertygroup_t *pg;
3370Sstevel@tonic-gate 	scf_property_t *prop;
3380Sstevel@tonic-gate 	scf_value_t *val;
3390Sstevel@tonic-gate 	scf_iter_t *iter, *piter;
3400Sstevel@tonic-gate 	instance_data_t idata;
3410Sstevel@tonic-gate 	char *buf, *vbuf;
3420Sstevel@tonic-gate 	char *startd_options_fmri = uu_msprintf("%s/:properties/options",
3430Sstevel@tonic-gate 	    SCF_SERVICE_STARTD);
3440Sstevel@tonic-gate 	char *startd_reconfigure_fmri = uu_msprintf(
3450Sstevel@tonic-gate 	    "%s/:properties/system/reconfigure", SCF_SERVICE_STARTD);
3460Sstevel@tonic-gate 	char *env_opts, *lasts, *cp;
3470Sstevel@tonic-gate 	int bind_fails = 0;
3480Sstevel@tonic-gate 	int ret = 0, r;
3490Sstevel@tonic-gate 	uint_t count = 0, msecs = ALLOC_DELAY;
3500Sstevel@tonic-gate 	size_t sz;
3510Sstevel@tonic-gate 	ctid_t ctid;
3520Sstevel@tonic-gate 	uint64_t uint64;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	buf = startd_alloc(max_scf_fmri_size);
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	if (startd_options_fmri == NULL || startd_reconfigure_fmri == NULL)
3570Sstevel@tonic-gate 		uu_die("Allocation failure\n");
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	st->st_log_prefix = LOG_PREFIX_EARLY;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	if ((st->st_log_file = getenv("STARTD_DEFAULT_LOG")) == NULL) {
3620Sstevel@tonic-gate 		st->st_log_file = startd_alloc(strlen(STARTD_DEFAULT_LOG) + 1);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 		(void) strcpy(st->st_log_file, STARTD_DEFAULT_LOG);
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	st->st_door_path = getenv("STARTD_ALT_DOOR");
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	/*
3700Sstevel@tonic-gate 	 * Read "options" property group.
3710Sstevel@tonic-gate 	 */
3720Sstevel@tonic-gate 	for (hndl = libscf_handle_create_bound(SCF_VERSION); hndl == NULL;
3730Sstevel@tonic-gate 	    hndl = libscf_handle_create_bound(SCF_VERSION), bind_fails++) {
3740Sstevel@tonic-gate 		(void) sleep(INITIAL_REBIND_DELAY);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 		if (bind_fails > INITIAL_REBIND_ATTEMPTS) {
3770Sstevel@tonic-gate 			/*
3780Sstevel@tonic-gate 			 * In the case that we can't bind to the repository
3790Sstevel@tonic-gate 			 * (which should have been started), we need to allow
3800Sstevel@tonic-gate 			 * the user into maintenance mode to determine what's
3810Sstevel@tonic-gate 			 * failed.
3820Sstevel@tonic-gate 			 */
3830Sstevel@tonic-gate 			log_framework(LOG_INFO, "Couldn't fetch "
3840Sstevel@tonic-gate 			    "default settings: %s\n",
3850Sstevel@tonic-gate 			    scf_strerror(scf_error()));
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 			ret = -1;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 			goto noscfout;
3900Sstevel@tonic-gate 		}
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	idata.i_fmri = SCF_SERVICE_STARTD;
3940Sstevel@tonic-gate 	idata.i_state = RESTARTER_STATE_NONE;
3950Sstevel@tonic-gate 	idata.i_next_state = RESTARTER_STATE_NONE;
3960Sstevel@tonic-gate timestamp:
3970Sstevel@tonic-gate 	switch (r = _restarter_commit_states(hndl, &idata,
3980Sstevel@tonic-gate 	    RESTARTER_STATE_ONLINE, RESTARTER_STATE_NONE, NULL)) {
3990Sstevel@tonic-gate 	case 0:
4000Sstevel@tonic-gate 		break;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	case ENOMEM:
4030Sstevel@tonic-gate 		++count;
4040Sstevel@tonic-gate 		if (count < ALLOC_RETRY) {
4050Sstevel@tonic-gate 			(void) poll(NULL, 0, msecs);
4060Sstevel@tonic-gate 			msecs *= ALLOC_DELAY_MULT;
4070Sstevel@tonic-gate 			goto timestamp;
4080Sstevel@tonic-gate 		}
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 		uu_die("Insufficient memory.\n");
4110Sstevel@tonic-gate 		/* NOTREACHED */
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	case ECONNABORTED:
4140Sstevel@tonic-gate 		libscf_handle_rebind(hndl);
4150Sstevel@tonic-gate 		goto timestamp;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	case ENOENT:
4180Sstevel@tonic-gate 	case EPERM:
4190Sstevel@tonic-gate 	case EACCES:
4200Sstevel@tonic-gate 	case EROFS:
4210Sstevel@tonic-gate 		log_error(LOG_INFO, "Could set state of %s: %s.\n",
4220Sstevel@tonic-gate 		    idata.i_fmri, strerror(r));
4230Sstevel@tonic-gate 		break;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	case EINVAL:
4260Sstevel@tonic-gate 	default:
4270Sstevel@tonic-gate 		bad_error("_restarter_commit_states", r);
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	pg = safe_scf_pg_create(hndl);
4310Sstevel@tonic-gate 	prop = safe_scf_property_create(hndl);
4320Sstevel@tonic-gate 	val = safe_scf_value_create(hndl);
4330Sstevel@tonic-gate 	inst = safe_scf_instance_create(hndl);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	/* set startd's restarter properties */
4360Sstevel@tonic-gate 	if (scf_handle_decode_fmri(hndl, SCF_SERVICE_STARTD, NULL, NULL, inst,
4370Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) == 0) {
4380Sstevel@tonic-gate 		(void) libscf_write_start_pid(inst, getpid());
4390Sstevel@tonic-gate 		ctid = proc_get_ctid();
4400Sstevel@tonic-gate 		if (ctid != -1) {
4410Sstevel@tonic-gate 			uint64 = (uint64_t)ctid;
4420Sstevel@tonic-gate 			(void) libscf_inst_set_count_prop(inst,
4430Sstevel@tonic-gate 			    SCF_PG_RESTARTER, SCF_PG_RESTARTER_TYPE,
4440Sstevel@tonic-gate 			    SCF_PG_RESTARTER_FLAGS, SCF_PROPERTY_CONTRACT,
4450Sstevel@tonic-gate 			    uint64);
4460Sstevel@tonic-gate 		}
4470Sstevel@tonic-gate 		(void) libscf_note_method_log(inst, LOG_PREFIX_EARLY,
4480Sstevel@tonic-gate 		    STARTD_DEFAULT_LOG);
4490Sstevel@tonic-gate 		(void) libscf_note_method_log(inst, LOG_PREFIX_NORMAL,
4500Sstevel@tonic-gate 		    STARTD_DEFAULT_LOG);
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	/* Read reconfigure property for recovery. */
4540Sstevel@tonic-gate 	if (scf_handle_decode_fmri(hndl, startd_reconfigure_fmri, NULL, NULL,
4550Sstevel@tonic-gate 	    NULL, NULL, prop, NULL) != -1 &&
4560Sstevel@tonic-gate 	    scf_property_get_value(prop, val) == 0)
4570Sstevel@tonic-gate 		(void) scf_value_get_boolean(val, &prop_reconfig);
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	if (scf_handle_decode_fmri(hndl, startd_options_fmri, NULL, NULL, NULL,
4600Sstevel@tonic-gate 	    pg, NULL, SCF_DECODE_FMRI_TRUNCATE) == -1) {
4610Sstevel@tonic-gate 		/*
4620Sstevel@tonic-gate 		 * No configuration options defined.
4630Sstevel@tonic-gate 		 */
4640Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
4650Sstevel@tonic-gate 			uu_warn("Couldn't read configuration from 'options' "
4660Sstevel@tonic-gate 			    "group: %s\n", scf_strerror(scf_error()));
4670Sstevel@tonic-gate 		goto scfout;
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/*
4710Sstevel@tonic-gate 	 * If there is no "options" group defined, then our defaults are fine.
4720Sstevel@tonic-gate 	 */
4730Sstevel@tonic-gate 	if (scf_pg_get_name(pg, NULL, 0) < 0)
4740Sstevel@tonic-gate 		goto scfout;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	/* Iterate through. */
4770Sstevel@tonic-gate 	iter = safe_scf_iter_create(hndl);
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	(void) scf_iter_pg_properties(iter, pg);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	piter = safe_scf_iter_create(hndl);
4820Sstevel@tonic-gate 	vbuf = startd_alloc(max_scf_value_size);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	while ((scf_iter_next_property(iter, prop) == 1)) {
4850Sstevel@tonic-gate 		scf_type_t ty;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 		if (scf_property_get_name(prop, buf, max_scf_fmri_size) < 0)
4880Sstevel@tonic-gate 			continue;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 		if (strcmp(buf, "logging") != 0 &&
4910Sstevel@tonic-gate 		    strcmp(buf, "boot_messages") != 0)
4920Sstevel@tonic-gate 			continue;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 		if (scf_property_type(prop, &ty) != 0) {
4950Sstevel@tonic-gate 			switch (scf_error()) {
4960Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
4970Sstevel@tonic-gate 			default:
4980Sstevel@tonic-gate 				libscf_handle_rebind(hndl);
4990Sstevel@tonic-gate 				continue;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
5020Sstevel@tonic-gate 				continue;
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
5050Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
5060Sstevel@tonic-gate 				bad_error("scf_property_type", scf_error());
5070Sstevel@tonic-gate 			}
5080Sstevel@tonic-gate 		}
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 		if (ty != SCF_TYPE_ASTRING) {
5110Sstevel@tonic-gate 			uu_warn("property \"options/%s\" is not of type "
5120Sstevel@tonic-gate 			    "astring; ignored.\n", buf);
5130Sstevel@tonic-gate 			continue;
5140Sstevel@tonic-gate 		}
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 		if (scf_property_get_value(prop, val) != 0) {
5170Sstevel@tonic-gate 			switch (scf_error()) {
5180Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
5190Sstevel@tonic-gate 			default:
5200Sstevel@tonic-gate 				return (ECONNABORTED);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
5230Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
5240Sstevel@tonic-gate 				return (0);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
5270Sstevel@tonic-gate 				uu_warn("property \"options/%s\" has multiple "
5280Sstevel@tonic-gate 				    "values; ignored.\n", buf);
5290Sstevel@tonic-gate 				continue;
5300Sstevel@tonic-gate 
5315040Swesolows 			case SCF_ERROR_PERMISSION_DENIED:
5325040Swesolows 				uu_warn("property \"options/%s\" cannot be "
5335040Swesolows 				    "read because startd has insufficient "
5345040Swesolows 				    "permission; ignored.\n", buf);
5355040Swesolows 				continue;
5365040Swesolows 
5370Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
5380Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
5390Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
5400Sstevel@tonic-gate 				bad_error("scf_property_get_value",
5410Sstevel@tonic-gate 				    scf_error());
5420Sstevel@tonic-gate 			}
5430Sstevel@tonic-gate 		}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 		if (scf_value_get_astring(val, vbuf, max_scf_value_size) < 0)
5460Sstevel@tonic-gate 			bad_error("scf_value_get_astring", scf_error());
5470Sstevel@tonic-gate 
5481958Slianep 		if (strcmp("logging", buf) == 0) {
5490Sstevel@tonic-gate 			if (strcmp("verbose", vbuf) == 0) {
5500Sstevel@tonic-gate 				st->st_boot_flags = STARTD_BOOT_VERBOSE;
5510Sstevel@tonic-gate 				st->st_log_level_min = LOG_INFO;
5520Sstevel@tonic-gate 			} else if (strcmp("debug", vbuf) == 0) {
5530Sstevel@tonic-gate 				st->st_boot_flags = STARTD_BOOT_VERBOSE;
5540Sstevel@tonic-gate 				st->st_log_level_min = LOG_DEBUG;
5550Sstevel@tonic-gate 			} else if (strcmp("quiet", vbuf) == 0) {
5560Sstevel@tonic-gate 				st->st_log_level_min = LOG_NOTICE;
5570Sstevel@tonic-gate 			} else {
5580Sstevel@tonic-gate 				uu_warn("unknown options/logging "
5590Sstevel@tonic-gate 				    "value '%s' ignored\n", vbuf);
5600Sstevel@tonic-gate 			}
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 		} else if (strcmp("boot_messages", buf) == 0) {
5630Sstevel@tonic-gate 			if (strcmp("quiet", vbuf) == 0) {
5640Sstevel@tonic-gate 				st->st_boot_flags = STARTD_BOOT_QUIET;
5650Sstevel@tonic-gate 			} else if (strcmp("verbose", vbuf) == 0) {
5660Sstevel@tonic-gate 				st->st_boot_flags = STARTD_BOOT_VERBOSE;
5670Sstevel@tonic-gate 			} else {
5680Sstevel@tonic-gate 				log_framework(LOG_NOTICE, "unknown "
5690Sstevel@tonic-gate 				    "options/boot_messages value '%s' "
5700Sstevel@tonic-gate 				    "ignored\n", vbuf);
5710Sstevel@tonic-gate 			}
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 		}
5740Sstevel@tonic-gate 	}
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	startd_free(vbuf, max_scf_value_size);
5770Sstevel@tonic-gate 	scf_iter_destroy(piter);
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	scf_iter_destroy(iter);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate scfout:
5820Sstevel@tonic-gate 	scf_value_destroy(val);
5830Sstevel@tonic-gate 	scf_pg_destroy(pg);
5840Sstevel@tonic-gate 	scf_property_destroy(prop);
5850Sstevel@tonic-gate 	scf_instance_destroy(inst);
5860Sstevel@tonic-gate 	(void) scf_handle_unbind(hndl);
5870Sstevel@tonic-gate 	scf_handle_destroy(hndl);
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate noscfout:
5900Sstevel@tonic-gate 	startd_free(buf, max_scf_fmri_size);
5910Sstevel@tonic-gate 	uu_free(startd_options_fmri);
5920Sstevel@tonic-gate 	uu_free(startd_reconfigure_fmri);
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	if (booting_to_single_user) {
5950Sstevel@tonic-gate 		st->st_subgraph = startd_alloc(max_scf_fmri_size);
5960Sstevel@tonic-gate 		sz = strlcpy(st->st_subgraph, "milestone/single-user:default",
5970Sstevel@tonic-gate 		    max_scf_fmri_size);
5980Sstevel@tonic-gate 		assert(sz < max_scf_fmri_size);
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	/*
6020Sstevel@tonic-gate 	 * Options passed in as boot arguments override repository defaults.
6030Sstevel@tonic-gate 	 */
6040Sstevel@tonic-gate 	env_opts = getenv("SMF_OPTIONS");
6050Sstevel@tonic-gate 	if (env_opts == NULL)
6060Sstevel@tonic-gate 		return (ret);
6070Sstevel@tonic-gate 
608195Sdstaff 	for (cp = strtok_r(env_opts, ",", &lasts); cp != NULL;
609195Sdstaff 	    cp = strtok_r(NULL, ",", &lasts)) {
6100Sstevel@tonic-gate 		if (strcmp(cp, "debug") == 0) {
6110Sstevel@tonic-gate 			st->st_boot_flags = STARTD_BOOT_VERBOSE;
6120Sstevel@tonic-gate 			st->st_log_level_min = LOG_DEBUG;
6131958Slianep 
6141958Slianep 			/* -m debug should send messages to console */
6151958Slianep 			st->st_log_flags =
6161958Slianep 			    st->st_log_flags | STARTD_LOG_TERMINAL;
6170Sstevel@tonic-gate 		} else if (strcmp(cp, "verbose") == 0) {
6180Sstevel@tonic-gate 			st->st_boot_flags = STARTD_BOOT_VERBOSE;
6190Sstevel@tonic-gate 			st->st_log_level_min = LOG_INFO;
6200Sstevel@tonic-gate 		} else if (strcmp(cp, "seed") == 0) {
6210Sstevel@tonic-gate 			uu_warn("SMF option \"%s\" unimplemented.\n", cp);
6220Sstevel@tonic-gate 		} else if (strcmp(cp, "quiet") == 0) {
6230Sstevel@tonic-gate 			st->st_log_level_min = LOG_NOTICE;
6240Sstevel@tonic-gate 		} else if (strncmp(cp, "milestone=",
6250Sstevel@tonic-gate 		    sizeof ("milestone=") - 1) == 0) {
6260Sstevel@tonic-gate 			char *mp = cp + sizeof ("milestone=") - 1;
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 			if (booting_to_single_user)
6290Sstevel@tonic-gate 				continue;
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 			if (st->st_subgraph == NULL) {
6320Sstevel@tonic-gate 				st->st_subgraph =
6330Sstevel@tonic-gate 				    startd_alloc(max_scf_fmri_size);
6340Sstevel@tonic-gate 				st->st_subgraph[0] = '\0';
6350Sstevel@tonic-gate 			}
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 			if (mp[0] == '\0' || strcmp(mp, "all") == 0) {
6380Sstevel@tonic-gate 				(void) strcpy(st->st_subgraph, "all");
6390Sstevel@tonic-gate 			} else if (strcmp(mp, "su") == 0 ||
6400Sstevel@tonic-gate 			    strcmp(mp, "single-user") == 0) {
6410Sstevel@tonic-gate 				(void) strcpy(st->st_subgraph,
6420Sstevel@tonic-gate 				    "milestone/single-user:default");
6430Sstevel@tonic-gate 			} else if (strcmp(mp, "mu") == 0 ||
6440Sstevel@tonic-gate 			    strcmp(mp, "multi-user") == 0) {
6450Sstevel@tonic-gate 				(void) strcpy(st->st_subgraph,
6460Sstevel@tonic-gate 				    "milestone/multi-user:default");
6470Sstevel@tonic-gate 			} else if (strcmp(mp, "mus") == 0 ||
6480Sstevel@tonic-gate 			    strcmp(mp, "multi-user-server") == 0) {
6490Sstevel@tonic-gate 				(void) strcpy(st->st_subgraph,
6500Sstevel@tonic-gate 				    "milestone/multi-user-server:default");
6510Sstevel@tonic-gate 			} else if (strcmp(mp, "none") == 0) {
6520Sstevel@tonic-gate 				(void) strcpy(st->st_subgraph, "none");
6530Sstevel@tonic-gate 			} else {
6540Sstevel@tonic-gate 				log_framework(LOG_NOTICE,
6550Sstevel@tonic-gate 				    "invalid milestone option value "
6560Sstevel@tonic-gate 				    "'%s' ignored\n", mp);
6570Sstevel@tonic-gate 			}
6580Sstevel@tonic-gate 		} else {
6590Sstevel@tonic-gate 			uu_warn("Unknown SMF option \"%s\".\n", cp);
6600Sstevel@tonic-gate 		}
6610Sstevel@tonic-gate 	}
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	return (ret);
6640Sstevel@tonic-gate }
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate /*
6670Sstevel@tonic-gate  * void set_boot_env()
6680Sstevel@tonic-gate  *
6690Sstevel@tonic-gate  * If -r was passed or /reconfigure exists, this is a reconfig
6700Sstevel@tonic-gate  * reboot.  We need to make sure that this information is given
6710Sstevel@tonic-gate  * to the appropriate services the first time they're started
6720Sstevel@tonic-gate  * by setting the system/reconfigure repository property,
6730Sstevel@tonic-gate  * as well as pass the _INIT_RECONFIG variable on to the rcS
6740Sstevel@tonic-gate  * start method so that legacy services can continue to use it.
6750Sstevel@tonic-gate  *
6760Sstevel@tonic-gate  * This function must never be called before contract_init(), as
6770Sstevel@tonic-gate  * it sets st_initial.  get_startd_config() sets prop_reconfig from
6780Sstevel@tonic-gate  * pre-existing repository state.
6790Sstevel@tonic-gate  */
6800Sstevel@tonic-gate static void
6810Sstevel@tonic-gate set_boot_env()
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate 	struct stat sb;
6840Sstevel@tonic-gate 	int r;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	/*
6870Sstevel@tonic-gate 	 * Check if property still is set -- indicates we didn't get
6880Sstevel@tonic-gate 	 * far enough previously to unset it.  Otherwise, if this isn't
6890Sstevel@tonic-gate 	 * the first startup, don't re-process /reconfigure or the
6900Sstevel@tonic-gate 	 * boot flag.
6910Sstevel@tonic-gate 	 */
6920Sstevel@tonic-gate 	if (prop_reconfig != 1 && st->st_initial != 1)
6930Sstevel@tonic-gate 		return;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	/* If /reconfigure exists, also set opt_reconfig. */
6960Sstevel@tonic-gate 	if (stat("/reconfigure", &sb) != -1)
6970Sstevel@tonic-gate 		opt_reconfig = 1;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	/* Nothing to do.  Just return. */
7000Sstevel@tonic-gate 	if (opt_reconfig == 0 && prop_reconfig == 0)
7010Sstevel@tonic-gate 		return;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	/*
7040Sstevel@tonic-gate 	 * Set startd's reconfigure property.  This property is
7050Sstevel@tonic-gate 	 * then cleared by successful completion of the single-user
7060Sstevel@tonic-gate 	 * milestone.
7070Sstevel@tonic-gate 	 */
7080Sstevel@tonic-gate 	if (prop_reconfig != 1) {
7090Sstevel@tonic-gate 		r = libscf_set_reconfig(1);
7100Sstevel@tonic-gate 		switch (r) {
7110Sstevel@tonic-gate 		case 0:
7120Sstevel@tonic-gate 			break;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 		case ENOENT:
7150Sstevel@tonic-gate 		case EPERM:
7160Sstevel@tonic-gate 		case EACCES:
7170Sstevel@tonic-gate 		case EROFS:
7180Sstevel@tonic-gate 			log_error(LOG_WARNING, "Could not set reconfiguration "
7190Sstevel@tonic-gate 			    "property: %s\n", strerror(r));
7200Sstevel@tonic-gate 			break;
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 		default:
7230Sstevel@tonic-gate 			bad_error("libscf_set_reconfig", r);
7240Sstevel@tonic-gate 		}
7250Sstevel@tonic-gate 	}
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate static void
7291958Slianep startup(void)
7300Sstevel@tonic-gate {
7310Sstevel@tonic-gate 	ctid_t configd_ctid;
7320Sstevel@tonic-gate 	int err;
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	/*
7350Sstevel@tonic-gate 	 * Initialize data structures.
7360Sstevel@tonic-gate 	 */
7370Sstevel@tonic-gate 	gu = startd_zalloc(sizeof (graph_update_t));
7380Sstevel@tonic-gate 	ru = startd_zalloc(sizeof (restarter_update_t));
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	(void) pthread_cond_init(&st->st_load_cv, NULL);
7410Sstevel@tonic-gate 	(void) pthread_cond_init(&st->st_configd_live_cv, NULL);
7420Sstevel@tonic-gate 	(void) pthread_cond_init(&gu->gu_cv, NULL);
7430Sstevel@tonic-gate 	(void) pthread_cond_init(&gu->gu_freeze_cv, NULL);
7440Sstevel@tonic-gate 	(void) pthread_cond_init(&ru->restarter_update_cv, NULL);
7450Sstevel@tonic-gate 	(void) pthread_mutex_init(&st->st_load_lock, &mutex_attrs);
7460Sstevel@tonic-gate 	(void) pthread_mutex_init(&st->st_configd_live_lock, &mutex_attrs);
7470Sstevel@tonic-gate 	(void) pthread_mutex_init(&gu->gu_lock, &mutex_attrs);
7480Sstevel@tonic-gate 	(void) pthread_mutex_init(&gu->gu_freeze_lock, &mutex_attrs);
7490Sstevel@tonic-gate 	(void) pthread_mutex_init(&ru->restarter_update_lock, &mutex_attrs);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	configd_ctid = contract_init();
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	if (configd_ctid != -1)
7540Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Existing configd contract %ld; not "
7550Sstevel@tonic-gate 		    "starting svc.configd\n", configd_ctid);
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	(void) startd_thread_create(fork_configd_thread, (void *)configd_ctid);
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	/*
7600Sstevel@tonic-gate 	 * Await, if necessary, configd's initial arrival.
7610Sstevel@tonic-gate 	 */
7620Sstevel@tonic-gate 	MUTEX_LOCK(&st->st_configd_live_lock);
7630Sstevel@tonic-gate 	while (!st->st_configd_lives) {
7640Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Awaiting cv signal on "
7650Sstevel@tonic-gate 		    "configd_live_cv\n");
7660Sstevel@tonic-gate 		err = pthread_cond_wait(&st->st_configd_live_cv,
7670Sstevel@tonic-gate 		    &st->st_configd_live_lock);
7680Sstevel@tonic-gate 		assert(err == 0);
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 	MUTEX_UNLOCK(&st->st_configd_live_lock);
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	utmpx_init();
7730Sstevel@tonic-gate 	wait_init();
7740Sstevel@tonic-gate 
7751958Slianep 	if (read_startd_config())
7760Sstevel@tonic-gate 		log_framework(LOG_INFO, "svc.configd unable to provide startd "
7770Sstevel@tonic-gate 		    "optional settings\n");
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	log_init();
7800Sstevel@tonic-gate 	dict_init();
7810Sstevel@tonic-gate 	timeout_init();
7820Sstevel@tonic-gate 	restarter_protocol_init();
7830Sstevel@tonic-gate 	restarter_init();
7840Sstevel@tonic-gate 	graph_protocol_init();
7850Sstevel@tonic-gate 	graph_init();
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	init_env();
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate 	set_boot_env();
7900Sstevel@tonic-gate 	restarter_start();
7910Sstevel@tonic-gate 	graph_engine_start();
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate static void
7950Sstevel@tonic-gate usage(const char *name)
7960Sstevel@tonic-gate {
7972258Srm88369 	uu_warn(gettext("usage: %s [-n]\n"), name);
7980Sstevel@tonic-gate 	exit(UU_EXIT_USAGE);
7990Sstevel@tonic-gate }
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate static int
8020Sstevel@tonic-gate daemonize_start(void)
8030Sstevel@tonic-gate {
8040Sstevel@tonic-gate 	pid_t pid;
8050Sstevel@tonic-gate 	int fd;
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	if ((pid = fork1()) < 0)
8080Sstevel@tonic-gate 		return (-1);
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	if (pid != 0)
8110Sstevel@tonic-gate 		exit(0);
8120Sstevel@tonic-gate 
8135617Sacruz 	(void) close(STDIN_FILENO);
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	if ((fd = open("/dev/null", O_RDONLY)) == -1) {
8160Sstevel@tonic-gate 		uu_warn(gettext("can't connect stdin to /dev/null"));
8175617Sacruz 	} else if (fd != STDIN_FILENO) {
8185617Sacruz 		(void) dup2(fd, STDIN_FILENO);
8190Sstevel@tonic-gate 		startd_close(fd);
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	closefrom(3);
8235617Sacruz 	(void) dup2(STDERR_FILENO, STDOUT_FILENO);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	(void) setsid();
8260Sstevel@tonic-gate 	(void) chdir("/");
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	/* Use default umask that init handed us, but 022 to create files. */
8290Sstevel@tonic-gate 	dmask = umask(022);
8300Sstevel@tonic-gate 	fmask = umask(dmask);
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	return (0);
8330Sstevel@tonic-gate }
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate /*ARGSUSED*/
8360Sstevel@tonic-gate static void
8370Sstevel@tonic-gate die_handler(int sig, siginfo_t *info, void *data)
8380Sstevel@tonic-gate {
8390Sstevel@tonic-gate 	finished = 1;
8400Sstevel@tonic-gate }
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate int
8430Sstevel@tonic-gate main(int argc, char *argv[])
8440Sstevel@tonic-gate {
8450Sstevel@tonic-gate 	int opt;
8460Sstevel@tonic-gate 	int daemonize = 1;
8470Sstevel@tonic-gate 	struct sigaction act;
8480Sstevel@tonic-gate 	sigset_t nullset;
8490Sstevel@tonic-gate 	struct stat sb;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	(void) uu_setpname(argv[0]);
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	st = startd_zalloc(sizeof (startd_state_t));
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	(void) pthread_mutexattr_init(&mutex_attrs);
8560Sstevel@tonic-gate #ifndef	NDEBUG
8570Sstevel@tonic-gate 	(void) pthread_mutexattr_settype(&mutex_attrs,
8580Sstevel@tonic-gate 	    PTHREAD_MUTEX_ERRORCHECK);
8590Sstevel@tonic-gate #endif
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	max_scf_name_size = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH);
8620Sstevel@tonic-gate 	max_scf_value_size = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
8630Sstevel@tonic-gate 	max_scf_fmri_size = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	if (max_scf_name_size == -1 || max_scf_value_size == -1 ||
8660Sstevel@tonic-gate 	    max_scf_value_size == -1)
8670Sstevel@tonic-gate 		uu_die("Can't determine repository maximum lengths.\n");
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	max_scf_name_size++;
8700Sstevel@tonic-gate 	max_scf_value_size++;
8710Sstevel@tonic-gate 	max_scf_fmri_size++;
8720Sstevel@tonic-gate 
8731958Slianep 	st->st_log_flags = STARTD_LOG_FILE | STARTD_LOG_SYSLOG;
8741958Slianep 	st->st_log_level_min = LOG_NOTICE;
8750Sstevel@tonic-gate 
8761958Slianep 	while ((opt = getopt(argc, argv, "nrs")) != EOF) {
8770Sstevel@tonic-gate 		switch (opt) {
8780Sstevel@tonic-gate 		case 'n':
8790Sstevel@tonic-gate 			daemonize = 0;
8800Sstevel@tonic-gate 			break;
8810Sstevel@tonic-gate 		case 'r':			/* reconfiguration boot */
8820Sstevel@tonic-gate 			opt_reconfig = 1;
8830Sstevel@tonic-gate 			break;
8840Sstevel@tonic-gate 		case 's':			/* single-user mode */
8850Sstevel@tonic-gate 			booting_to_single_user = B_TRUE;
8860Sstevel@tonic-gate 			break;
8870Sstevel@tonic-gate 		default:
8880Sstevel@tonic-gate 			usage(argv[0]);		/* exits */
8890Sstevel@tonic-gate 		}
8900Sstevel@tonic-gate 	}
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	if (optind != argc)
8930Sstevel@tonic-gate 		usage(argv[0]);
8940Sstevel@tonic-gate 
8951914Scasper 	(void) enable_extended_FILE_stdio(-1, -1);
8961914Scasper 
8970Sstevel@tonic-gate 	if (daemonize)
8980Sstevel@tonic-gate 		if (daemonize_start() < 0)
8990Sstevel@tonic-gate 			uu_die("Can't daemonize\n");
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	log_init();
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	if (stat("/etc/svc/volatile/resetting", &sb) != -1) {
9040Sstevel@tonic-gate 		log_framework(LOG_NOTICE, "Restarter quiesced.\n");
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 		for (;;)
9070Sstevel@tonic-gate 			(void) pause();
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	act.sa_sigaction = &die_handler;
9110Sstevel@tonic-gate 	(void) sigfillset(&act.sa_mask);
9120Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
9130Sstevel@tonic-gate 	(void) sigaction(SIGINT, &act, NULL);
9140Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &act, NULL);
9150Sstevel@tonic-gate 
9161958Slianep 	startup();
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	(void) sigemptyset(&nullset);
9190Sstevel@tonic-gate 	while (!finished) {
9200Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Main thread paused\n");
9210Sstevel@tonic-gate 		(void) sigsuspend(&nullset);
9220Sstevel@tonic-gate 	}
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	(void) log_framework(LOG_DEBUG, "Restarter exiting.\n");
9250Sstevel@tonic-gate 	return (0);
9260Sstevel@tonic-gate }
927