xref: /titanic_50/usr/src/cmd/svc/startd/startd.c (revision 7b966b001ffaa737db1048e7248351f8cefe9587)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*7b966b00Sdstaff  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * startd.c - the master restarter
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * svc.startd comprises two halves.  The graph engine is based in graph.c and
337c478bd9Sstevel@tonic-gate  * maintains the service dependency graph based on the information in the
347c478bd9Sstevel@tonic-gate  * repository.  For each service it also tracks the current state and the
357c478bd9Sstevel@tonic-gate  * restarter responsible for the service.  Based on the graph, events from the
367c478bd9Sstevel@tonic-gate  * repository (mostly administrative requests from svcadm), and messages from
377c478bd9Sstevel@tonic-gate  * the restarters, the graph engine makes decisions about how the services
387c478bd9Sstevel@tonic-gate  * should be manipulated and sends commands to the appropriate restarters.
397c478bd9Sstevel@tonic-gate  * Communication between the graph engine and the restarters is embodied in
407c478bd9Sstevel@tonic-gate  * protocol.c.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * The second half of svc.startd is the restarter for services managed by
437c478bd9Sstevel@tonic-gate  * svc.startd and is primarily contained in restarter.c.  It responds to graph
447c478bd9Sstevel@tonic-gate  * engine commands by executing methods, updating the repository, and sending
457c478bd9Sstevel@tonic-gate  * feedback (mostly state updates) to the graph engine.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * Error handling
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * In general, when svc.startd runs out of memory it reattempts a few times,
507c478bd9Sstevel@tonic-gate  * sleeping inbetween, before giving up and exiting (see startd_alloc_retry()).
517c478bd9Sstevel@tonic-gate  * When a repository connection is broken (libscf calls fail with
527c478bd9Sstevel@tonic-gate  * SCF_ERROR_CONNECTION_BROKEN, librestart and internal functions return
537c478bd9Sstevel@tonic-gate  * ECONNABORTED), svc.startd calls libscf_rebind_handle(), which coordinates
547c478bd9Sstevel@tonic-gate  * with the svc.configd-restarting thread, fork_configd_thread(), via
557c478bd9Sstevel@tonic-gate  * st->st_configd_live_cv, and rebinds the repository handle.  Doing so resets
567c478bd9Sstevel@tonic-gate  * all libscf state associated with that handle, so functions which do this
577c478bd9Sstevel@tonic-gate  * should communicate the event to their callers (usually by returning
587c478bd9Sstevel@tonic-gate  * ECONNRESET) so they may reset their state appropriately.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include <stdio.h>
627c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>		/* uses FILE * without including stdio.h */
637c478bd9Sstevel@tonic-gate #include <alloca.h>
647c478bd9Sstevel@tonic-gate #include <sys/mount.h>
657c478bd9Sstevel@tonic-gate #include <sys/stat.h>
667c478bd9Sstevel@tonic-gate #include <sys/types.h>
677c478bd9Sstevel@tonic-gate #include <sys/wait.h>
687c478bd9Sstevel@tonic-gate #include <assert.h>
697c478bd9Sstevel@tonic-gate #include <errno.h>
707c478bd9Sstevel@tonic-gate #include <fcntl.h>
717c478bd9Sstevel@tonic-gate #include <ftw.h>
727c478bd9Sstevel@tonic-gate #include <libintl.h>
737c478bd9Sstevel@tonic-gate #include <libscf.h>
747c478bd9Sstevel@tonic-gate #include <libscf_priv.h>
757c478bd9Sstevel@tonic-gate #include <libuutil.h>
767c478bd9Sstevel@tonic-gate #include <locale.h>
777c478bd9Sstevel@tonic-gate #include <poll.h>
787c478bd9Sstevel@tonic-gate #include <pthread.h>
797c478bd9Sstevel@tonic-gate #include <signal.h>
807c478bd9Sstevel@tonic-gate #include <stdarg.h>
817c478bd9Sstevel@tonic-gate #include <stdlib.h>
827c478bd9Sstevel@tonic-gate #include <string.h>
837c478bd9Sstevel@tonic-gate #include <strings.h>
847c478bd9Sstevel@tonic-gate #include <unistd.h>
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #include "startd.h"
877c478bd9Sstevel@tonic-gate #include "protocol.h"
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate ssize_t max_scf_name_size;
907c478bd9Sstevel@tonic-gate ssize_t max_scf_fmri_size;
917c478bd9Sstevel@tonic-gate ssize_t max_scf_value_size;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate mode_t fmask;
947c478bd9Sstevel@tonic-gate mode_t dmask;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate graph_update_t *gu;
977c478bd9Sstevel@tonic-gate restarter_update_t *ru;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate startd_state_t *st;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate boolean_t booting_to_single_user = B_FALSE;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate const char * const admin_actions[] = {
1047c478bd9Sstevel@tonic-gate     SCF_PROPERTY_DEGRADED,
1057c478bd9Sstevel@tonic-gate     SCF_PROPERTY_MAINT_OFF,
1067c478bd9Sstevel@tonic-gate     SCF_PROPERTY_MAINT_ON,
1077c478bd9Sstevel@tonic-gate     SCF_PROPERTY_MAINT_ON_IMMEDIATE,
1087c478bd9Sstevel@tonic-gate     SCF_PROPERTY_REFRESH,
1097c478bd9Sstevel@tonic-gate     SCF_PROPERTY_RESTART
1107c478bd9Sstevel@tonic-gate };
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate const int admin_events[NACTIONS] = {
1137c478bd9Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_DEGRADED,
1147c478bd9Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF,
1157c478bd9Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON,
1167c478bd9Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE,
1177c478bd9Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_REFRESH,
1187c478bd9Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_RESTART
1197c478bd9Sstevel@tonic-gate };
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate const char * const instance_state_str[] = {
1227c478bd9Sstevel@tonic-gate 	"none",
1237c478bd9Sstevel@tonic-gate 	"uninitialized",
1247c478bd9Sstevel@tonic-gate 	"maintenance",
1257c478bd9Sstevel@tonic-gate 	"offline",
1267c478bd9Sstevel@tonic-gate 	"disabled",
1277c478bd9Sstevel@tonic-gate 	"online",
1287c478bd9Sstevel@tonic-gate 	"degraded"
1297c478bd9Sstevel@tonic-gate };
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static int finished = 0;
1327c478bd9Sstevel@tonic-gate static int opt_reconfig = 0;
1337c478bd9Sstevel@tonic-gate static uint8_t prop_reconfig = 0;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #define	INITIAL_REBIND_ATTEMPTS	5
1367c478bd9Sstevel@tonic-gate #define	INITIAL_REBIND_DELAY	3
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate pthread_mutexattr_t mutex_attrs;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate const char *
1417c478bd9Sstevel@tonic-gate _umem_debug_init(void)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	return ("default,verbose");	/* UMEM_DEBUG setting */
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate const char *
1477c478bd9Sstevel@tonic-gate _umem_logging_init(void)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	return ("fail,contents");	/* UMEM_LOGGING setting */
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * startd_alloc_retry()
1547c478bd9Sstevel@tonic-gate  *   Wrapper for allocation functions.  Retries with a decaying time
1557c478bd9Sstevel@tonic-gate  *   value on failure to allocate, and aborts startd if failure is
1567c478bd9Sstevel@tonic-gate  *   persistent.
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate void *
1597c478bd9Sstevel@tonic-gate startd_alloc_retry(void *f(size_t, int), size_t sz)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	void *p;
1627c478bd9Sstevel@tonic-gate 	uint_t try, msecs;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	p = f(sz, UMEM_DEFAULT);
1657c478bd9Sstevel@tonic-gate 	if (p != NULL || sz == 0)
1667c478bd9Sstevel@tonic-gate 		return (p);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	for (try = 0; p == NULL && try < ALLOC_RETRY; ++try) {
1717c478bd9Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
1727c478bd9Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
1737c478bd9Sstevel@tonic-gate 		p = f(sz, UMEM_DEFAULT);
1747c478bd9Sstevel@tonic-gate 		if (p != NULL)
1757c478bd9Sstevel@tonic-gate 			return (p);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
1797c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate void *
1837c478bd9Sstevel@tonic-gate safe_realloc(void *p, size_t sz)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	uint_t try, msecs;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	p = realloc(p, sz);
1887c478bd9Sstevel@tonic-gate 	if (p != NULL || sz == 0)
1897c478bd9Sstevel@tonic-gate 		return (p);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	for (try = 0; errno == EAGAIN && try < ALLOC_RETRY; ++try) {
1947c478bd9Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
1957c478bd9Sstevel@tonic-gate 		p = realloc(p, sz);
1967c478bd9Sstevel@tonic-gate 		if (p != NULL)
1977c478bd9Sstevel@tonic-gate 			return (p);
1987c478bd9Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
2027c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate char *
2067c478bd9Sstevel@tonic-gate safe_strdup(const char *s)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	uint_t try, msecs;
2097c478bd9Sstevel@tonic-gate 	char *d;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	d = strdup(s);
2127c478bd9Sstevel@tonic-gate 	if (d != NULL)
2137c478bd9Sstevel@tonic-gate 		return (d);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	for (try = 0;
2187c478bd9Sstevel@tonic-gate 	    (errno == EAGAIN || errno == ENOMEM) && try < ALLOC_RETRY;
2197c478bd9Sstevel@tonic-gate 	    ++try) {
2207c478bd9Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
2217c478bd9Sstevel@tonic-gate 		d = strdup(s);
2227c478bd9Sstevel@tonic-gate 		if (d != NULL)
2237c478bd9Sstevel@tonic-gate 			return (d);
2247c478bd9Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
2287c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate void
2337c478bd9Sstevel@tonic-gate startd_free(void *p, size_t sz)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	umem_free(p, sz);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * Creates a uu_list_pool_t with the same retry policy as startd_alloc().
2407c478bd9Sstevel@tonic-gate  * Only returns NULL for UU_ERROR_UNKNOWN_FLAG and UU_ERROR_NOT_SUPPORTED.
2417c478bd9Sstevel@tonic-gate  */
2427c478bd9Sstevel@tonic-gate uu_list_pool_t *
2437c478bd9Sstevel@tonic-gate startd_list_pool_create(const char *name, size_t e, size_t o,
2447c478bd9Sstevel@tonic-gate     uu_compare_fn_t *f, uint32_t flags)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	uu_list_pool_t *pool;
2477c478bd9Sstevel@tonic-gate 	uint_t try, msecs;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	pool = uu_list_pool_create(name, e, o, f, flags);
2507c478bd9Sstevel@tonic-gate 	if (pool != NULL)
2517c478bd9Sstevel@tonic-gate 		return (pool);
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	for (try = 0; uu_error() == UU_ERROR_NO_MEMORY && try < ALLOC_RETRY;
2567c478bd9Sstevel@tonic-gate 	    ++try) {
2577c478bd9Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
2587c478bd9Sstevel@tonic-gate 		pool = uu_list_pool_create(name, e, o, f, flags);
2597c478bd9Sstevel@tonic-gate 		if (pool != NULL)
2607c478bd9Sstevel@tonic-gate 			return (pool);
2617c478bd9Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (try < ALLOC_RETRY)
2657c478bd9Sstevel@tonic-gate 		return (NULL);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
2687c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate /*
2727c478bd9Sstevel@tonic-gate  * Creates a uu_list_t with the same retry policy as startd_alloc().  Only
2737c478bd9Sstevel@tonic-gate  * returns NULL for UU_ERROR_UNKNOWN_FLAG and UU_ERROR_NOT_SUPPORTED.
2747c478bd9Sstevel@tonic-gate  */
2757c478bd9Sstevel@tonic-gate uu_list_t *
2767c478bd9Sstevel@tonic-gate startd_list_create(uu_list_pool_t *pool, void *parent, uint32_t flags)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	uu_list_t *list;
2797c478bd9Sstevel@tonic-gate 	uint_t try, msecs;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	list = uu_list_create(pool, parent, flags);
2827c478bd9Sstevel@tonic-gate 	if (list != NULL)
2837c478bd9Sstevel@tonic-gate 		return (list);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	for (try = 0; uu_error() == UU_ERROR_NO_MEMORY && try < ALLOC_RETRY;
2887c478bd9Sstevel@tonic-gate 	    ++try) {
2897c478bd9Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
2907c478bd9Sstevel@tonic-gate 		list = uu_list_create(pool, parent, flags);
2917c478bd9Sstevel@tonic-gate 		if (list != NULL)
2927c478bd9Sstevel@tonic-gate 			return (list);
2937c478bd9Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	if (try < ALLOC_RETRY)
2977c478bd9Sstevel@tonic-gate 		return (NULL);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
3007c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate pthread_t
3047c478bd9Sstevel@tonic-gate startd_thread_create(void *(*func)(void *), void *ptr)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 	int err;
3077c478bd9Sstevel@tonic-gate 	pthread_t tid;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	err = pthread_create(&tid, NULL, func, ptr);
3107c478bd9Sstevel@tonic-gate 	if (err != 0) {
3117c478bd9Sstevel@tonic-gate 		assert(err == EAGAIN);
3127c478bd9Sstevel@tonic-gate 		uu_die("Could not create thread.\n");
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	err = pthread_detach(tid);
3167c478bd9Sstevel@tonic-gate 	assert(err == 0);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	return (tid);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate static int
3237c478bd9Sstevel@tonic-gate read_startd_config(int log_args)
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	scf_handle_t *hndl;
3267c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
3277c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
3287c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
3297c478bd9Sstevel@tonic-gate 	scf_value_t *val;
3307c478bd9Sstevel@tonic-gate 	scf_iter_t *iter, *piter;
3317c478bd9Sstevel@tonic-gate 	instance_data_t idata;
3327c478bd9Sstevel@tonic-gate 	char *buf, *vbuf;
3337c478bd9Sstevel@tonic-gate 	char *startd_options_fmri = uu_msprintf("%s/:properties/options",
3347c478bd9Sstevel@tonic-gate 	    SCF_SERVICE_STARTD);
3357c478bd9Sstevel@tonic-gate 	char *startd_reconfigure_fmri = uu_msprintf(
3367c478bd9Sstevel@tonic-gate 	    "%s/:properties/system/reconfigure", SCF_SERVICE_STARTD);
3377c478bd9Sstevel@tonic-gate 	char *env_opts, *lasts, *cp;
3387c478bd9Sstevel@tonic-gate 	int bind_fails = 0;
3397c478bd9Sstevel@tonic-gate 	int ret = 0, r;
3407c478bd9Sstevel@tonic-gate 	uint_t count = 0, msecs = ALLOC_DELAY;
3417c478bd9Sstevel@tonic-gate 	size_t sz;
3427c478bd9Sstevel@tonic-gate 	ctid_t ctid;
3437c478bd9Sstevel@tonic-gate 	uint64_t uint64;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	buf = startd_alloc(max_scf_fmri_size);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (startd_options_fmri == NULL || startd_reconfigure_fmri == NULL)
3487c478bd9Sstevel@tonic-gate 		uu_die("Allocation failure\n");
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	st->st_log_prefix = LOG_PREFIX_EARLY;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	if ((st->st_log_file = getenv("STARTD_DEFAULT_LOG")) == NULL) {
3537c478bd9Sstevel@tonic-gate 		st->st_log_file = startd_alloc(strlen(STARTD_DEFAULT_LOG) + 1);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 		(void) strcpy(st->st_log_file, STARTD_DEFAULT_LOG);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	st->st_door_path = getenv("STARTD_ALT_DOOR");
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	/*
3617c478bd9Sstevel@tonic-gate 	 * Read "options" property group.
3627c478bd9Sstevel@tonic-gate 	 */
3637c478bd9Sstevel@tonic-gate 	for (hndl = libscf_handle_create_bound(SCF_VERSION); hndl == NULL;
3647c478bd9Sstevel@tonic-gate 	    hndl = libscf_handle_create_bound(SCF_VERSION), bind_fails++) {
3657c478bd9Sstevel@tonic-gate 		(void) sleep(INITIAL_REBIND_DELAY);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 		if (bind_fails > INITIAL_REBIND_ATTEMPTS) {
3687c478bd9Sstevel@tonic-gate 			/*
3697c478bd9Sstevel@tonic-gate 			 * In the case that we can't bind to the repository
3707c478bd9Sstevel@tonic-gate 			 * (which should have been started), we need to allow
3717c478bd9Sstevel@tonic-gate 			 * the user into maintenance mode to determine what's
3727c478bd9Sstevel@tonic-gate 			 * failed.
3737c478bd9Sstevel@tonic-gate 			 */
3747c478bd9Sstevel@tonic-gate 			log_framework(LOG_INFO, "Couldn't fetch "
3757c478bd9Sstevel@tonic-gate 			    "default settings: %s\n",
3767c478bd9Sstevel@tonic-gate 			    scf_strerror(scf_error()));
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 			ret = -1;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 			goto noscfout;
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	idata.i_fmri = SCF_SERVICE_STARTD;
3857c478bd9Sstevel@tonic-gate 	idata.i_state = RESTARTER_STATE_NONE;
3867c478bd9Sstevel@tonic-gate 	idata.i_next_state = RESTARTER_STATE_NONE;
3877c478bd9Sstevel@tonic-gate timestamp:
3887c478bd9Sstevel@tonic-gate 	switch (r = _restarter_commit_states(hndl, &idata,
3897c478bd9Sstevel@tonic-gate 	    RESTARTER_STATE_ONLINE, RESTARTER_STATE_NONE, NULL)) {
3907c478bd9Sstevel@tonic-gate 	case 0:
3917c478bd9Sstevel@tonic-gate 		break;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	case ENOMEM:
3947c478bd9Sstevel@tonic-gate 		++count;
3957c478bd9Sstevel@tonic-gate 		if (count < ALLOC_RETRY) {
3967c478bd9Sstevel@tonic-gate 			(void) poll(NULL, 0, msecs);
3977c478bd9Sstevel@tonic-gate 			msecs *= ALLOC_DELAY_MULT;
3987c478bd9Sstevel@tonic-gate 			goto timestamp;
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 		uu_die("Insufficient memory.\n");
4027c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
4057c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(hndl);
4067c478bd9Sstevel@tonic-gate 		goto timestamp;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	case ENOENT:
4097c478bd9Sstevel@tonic-gate 	case EPERM:
4107c478bd9Sstevel@tonic-gate 	case EACCES:
4117c478bd9Sstevel@tonic-gate 	case EROFS:
4127c478bd9Sstevel@tonic-gate 		log_error(LOG_INFO, "Could set state of %s: %s.\n",
4137c478bd9Sstevel@tonic-gate 		    idata.i_fmri, strerror(r));
4147c478bd9Sstevel@tonic-gate 		break;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	case EINVAL:
4177c478bd9Sstevel@tonic-gate 	default:
4187c478bd9Sstevel@tonic-gate 		bad_error("_restarter_commit_states", r);
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(hndl);
4227c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(hndl);
4237c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(hndl);
4247c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(hndl);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/* set startd's restarter properties */
4277c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(hndl, SCF_SERVICE_STARTD, NULL, NULL, inst,
4287c478bd9Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) == 0) {
4297c478bd9Sstevel@tonic-gate 		(void) libscf_write_start_pid(inst, getpid());
4307c478bd9Sstevel@tonic-gate 		ctid = proc_get_ctid();
4317c478bd9Sstevel@tonic-gate 		if (ctid != -1) {
4327c478bd9Sstevel@tonic-gate 			uint64 = (uint64_t)ctid;
4337c478bd9Sstevel@tonic-gate 			(void) libscf_inst_set_count_prop(inst,
4347c478bd9Sstevel@tonic-gate 			    SCF_PG_RESTARTER, SCF_PG_RESTARTER_TYPE,
4357c478bd9Sstevel@tonic-gate 			    SCF_PG_RESTARTER_FLAGS, SCF_PROPERTY_CONTRACT,
4367c478bd9Sstevel@tonic-gate 			    uint64);
4377c478bd9Sstevel@tonic-gate 		}
4387c478bd9Sstevel@tonic-gate 		(void) libscf_note_method_log(inst, LOG_PREFIX_EARLY,
4397c478bd9Sstevel@tonic-gate 		    STARTD_DEFAULT_LOG);
4407c478bd9Sstevel@tonic-gate 		(void) libscf_note_method_log(inst, LOG_PREFIX_NORMAL,
4417c478bd9Sstevel@tonic-gate 		    STARTD_DEFAULT_LOG);
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	/* Read reconfigure property for recovery. */
4457c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(hndl, startd_reconfigure_fmri, NULL, NULL,
4467c478bd9Sstevel@tonic-gate 	    NULL, NULL, prop, NULL) != -1 &&
4477c478bd9Sstevel@tonic-gate 	    scf_property_get_value(prop, val) == 0)
4487c478bd9Sstevel@tonic-gate 		(void) scf_value_get_boolean(val, &prop_reconfig);
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(hndl, startd_options_fmri, NULL, NULL, NULL,
4517c478bd9Sstevel@tonic-gate 	    pg, NULL, SCF_DECODE_FMRI_TRUNCATE) == -1) {
4527c478bd9Sstevel@tonic-gate 		/*
4537c478bd9Sstevel@tonic-gate 		 * No configuration options defined.
4547c478bd9Sstevel@tonic-gate 		 */
4557c478bd9Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
4567c478bd9Sstevel@tonic-gate 			uu_warn("Couldn't read configuration from 'options' "
4577c478bd9Sstevel@tonic-gate 			    "group: %s\n", scf_strerror(scf_error()));
4587c478bd9Sstevel@tonic-gate 		goto scfout;
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	/*
4627c478bd9Sstevel@tonic-gate 	 * If there is no "options" group defined, then our defaults are fine.
4637c478bd9Sstevel@tonic-gate 	 */
4647c478bd9Sstevel@tonic-gate 	if (scf_pg_get_name(pg, NULL, 0) < 0)
4657c478bd9Sstevel@tonic-gate 		goto scfout;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	/* Iterate through. */
4687c478bd9Sstevel@tonic-gate 	iter = safe_scf_iter_create(hndl);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	(void) scf_iter_pg_properties(iter, pg);
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	piter = safe_scf_iter_create(hndl);
4737c478bd9Sstevel@tonic-gate 	vbuf = startd_alloc(max_scf_value_size);
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	while ((scf_iter_next_property(iter, prop) == 1)) {
4767c478bd9Sstevel@tonic-gate 		scf_type_t ty;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 		if (scf_property_get_name(prop, buf, max_scf_fmri_size) < 0)
4797c478bd9Sstevel@tonic-gate 			continue;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 		if (strcmp(buf, "logging") != 0 &&
4827c478bd9Sstevel@tonic-gate 		    strcmp(buf, "boot_messages") != 0)
4837c478bd9Sstevel@tonic-gate 			continue;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 		if (scf_property_type(prop, &ty) != 0) {
4867c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
4877c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
4887c478bd9Sstevel@tonic-gate 			default:
4897c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(hndl);
4907c478bd9Sstevel@tonic-gate 				continue;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
4937c478bd9Sstevel@tonic-gate 				continue;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
4967c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
4977c478bd9Sstevel@tonic-gate 				bad_error("scf_property_type", scf_error());
4987c478bd9Sstevel@tonic-gate 			}
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 		if (ty != SCF_TYPE_ASTRING) {
5027c478bd9Sstevel@tonic-gate 			uu_warn("property \"options/%s\" is not of type "
5037c478bd9Sstevel@tonic-gate 			    "astring; ignored.\n", buf);
5047c478bd9Sstevel@tonic-gate 			continue;
5057c478bd9Sstevel@tonic-gate 		}
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		if (scf_property_get_value(prop, val) != 0) {
5087c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
5097c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
5107c478bd9Sstevel@tonic-gate 			default:
5117c478bd9Sstevel@tonic-gate 				return (ECONNABORTED);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
5147c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
5157c478bd9Sstevel@tonic-gate 				return (0);
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
5187c478bd9Sstevel@tonic-gate 				uu_warn("property \"options/%s\" has multiple "
5197c478bd9Sstevel@tonic-gate 				    "values; ignored.\n", buf);
5207c478bd9Sstevel@tonic-gate 				continue;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
5237c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
5247c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
5257c478bd9Sstevel@tonic-gate 				bad_error("scf_property_get_value",
5267c478bd9Sstevel@tonic-gate 				    scf_error());
5277c478bd9Sstevel@tonic-gate 			}
5287c478bd9Sstevel@tonic-gate 		}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 		if (scf_value_get_astring(val, vbuf, max_scf_value_size) < 0)
5317c478bd9Sstevel@tonic-gate 			bad_error("scf_value_get_astring", scf_error());
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 		if (!log_args && strcmp("logging", buf) == 0) {
5347c478bd9Sstevel@tonic-gate 			if (strcmp("verbose", vbuf) == 0) {
5357c478bd9Sstevel@tonic-gate 				st->st_boot_flags = STARTD_BOOT_VERBOSE;
5367c478bd9Sstevel@tonic-gate 				st->st_log_flags = STARTD_LOG_VERBOSE;
5377c478bd9Sstevel@tonic-gate 				st->st_log_level_min = LOG_INFO;
5387c478bd9Sstevel@tonic-gate 			} else if (strcmp("debug", vbuf) == 0) {
5397c478bd9Sstevel@tonic-gate 				st->st_boot_flags = STARTD_BOOT_VERBOSE;
5407c478bd9Sstevel@tonic-gate 				st->st_log_flags = STARTD_LOG_DEBUG;
5417c478bd9Sstevel@tonic-gate 				st->st_log_level_min = LOG_DEBUG;
5427c478bd9Sstevel@tonic-gate 			} else if (strcmp("quiet", vbuf) == 0) {
5437c478bd9Sstevel@tonic-gate 				st->st_log_flags = STARTD_LOG_QUIET;
5447c478bd9Sstevel@tonic-gate 				st->st_log_level_min = LOG_NOTICE;
5457c478bd9Sstevel@tonic-gate 			} else {
5467c478bd9Sstevel@tonic-gate 				uu_warn("unknown options/logging "
5477c478bd9Sstevel@tonic-gate 				    "value '%s' ignored\n", vbuf);
5487c478bd9Sstevel@tonic-gate 			}
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 		} else if (strcmp("boot_messages", buf) == 0) {
5517c478bd9Sstevel@tonic-gate 			if (strcmp("quiet", vbuf) == 0) {
5527c478bd9Sstevel@tonic-gate 				st->st_boot_flags = STARTD_BOOT_QUIET;
5537c478bd9Sstevel@tonic-gate 			} else if (strcmp("verbose", vbuf) == 0) {
5547c478bd9Sstevel@tonic-gate 				st->st_boot_flags = STARTD_BOOT_VERBOSE;
5557c478bd9Sstevel@tonic-gate 			} else {
5567c478bd9Sstevel@tonic-gate 				log_framework(LOG_NOTICE, "unknown "
5577c478bd9Sstevel@tonic-gate 				    "options/boot_messages value '%s' "
5587c478bd9Sstevel@tonic-gate 				    "ignored\n", vbuf);
5597c478bd9Sstevel@tonic-gate 			}
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 		}
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	startd_free(vbuf, max_scf_value_size);
5657c478bd9Sstevel@tonic-gate 	scf_iter_destroy(piter);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate scfout:
5707c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
5717c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
5727c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
5737c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
5747c478bd9Sstevel@tonic-gate 	(void) scf_handle_unbind(hndl);
5757c478bd9Sstevel@tonic-gate 	scf_handle_destroy(hndl);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate noscfout:
5787c478bd9Sstevel@tonic-gate 	startd_free(buf, max_scf_fmri_size);
5797c478bd9Sstevel@tonic-gate 	uu_free(startd_options_fmri);
5807c478bd9Sstevel@tonic-gate 	uu_free(startd_reconfigure_fmri);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	if (booting_to_single_user) {
5837c478bd9Sstevel@tonic-gate 		st->st_subgraph = startd_alloc(max_scf_fmri_size);
5847c478bd9Sstevel@tonic-gate 		sz = strlcpy(st->st_subgraph, "milestone/single-user:default",
5857c478bd9Sstevel@tonic-gate 		    max_scf_fmri_size);
5867c478bd9Sstevel@tonic-gate 		assert(sz < max_scf_fmri_size);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	/*
5907c478bd9Sstevel@tonic-gate 	 * Options passed in as boot arguments override repository defaults.
5917c478bd9Sstevel@tonic-gate 	 */
5927c478bd9Sstevel@tonic-gate 	env_opts = getenv("SMF_OPTIONS");
5937c478bd9Sstevel@tonic-gate 	if (env_opts == NULL)
5947c478bd9Sstevel@tonic-gate 		return (ret);
5957c478bd9Sstevel@tonic-gate 
596*7b966b00Sdstaff 	for (cp = strtok_r(env_opts, ",", &lasts); cp != NULL;
597*7b966b00Sdstaff 	    cp = strtok_r(NULL, ",", &lasts)) {
5987c478bd9Sstevel@tonic-gate 		if (strcmp(cp, "debug") == 0) {
5997c478bd9Sstevel@tonic-gate 			st->st_boot_flags = STARTD_BOOT_VERBOSE;
6007c478bd9Sstevel@tonic-gate 			st->st_log_flags = STARTD_LOG_DEBUG;
6017c478bd9Sstevel@tonic-gate 			st->st_log_level_min = LOG_DEBUG;
6027c478bd9Sstevel@tonic-gate 		} else if (strcmp(cp, "verbose") == 0) {
6037c478bd9Sstevel@tonic-gate 			st->st_boot_flags = STARTD_BOOT_VERBOSE;
6047c478bd9Sstevel@tonic-gate 			st->st_log_flags = STARTD_LOG_VERBOSE;
6057c478bd9Sstevel@tonic-gate 			st->st_log_level_min = LOG_INFO;
6067c478bd9Sstevel@tonic-gate 		} else if (strcmp(cp, "seed") == 0) {
6077c478bd9Sstevel@tonic-gate 			uu_warn("SMF option \"%s\" unimplemented.\n", cp);
6087c478bd9Sstevel@tonic-gate 		} else if (strcmp(cp, "quiet") == 0) {
6097c478bd9Sstevel@tonic-gate 			st->st_log_flags = STARTD_LOG_QUIET;
6107c478bd9Sstevel@tonic-gate 			st->st_log_level_min = LOG_NOTICE;
6117c478bd9Sstevel@tonic-gate 		} else if (strncmp(cp, "milestone=",
6127c478bd9Sstevel@tonic-gate 		    sizeof ("milestone=") - 1) == 0) {
6137c478bd9Sstevel@tonic-gate 			char *mp = cp + sizeof ("milestone=") - 1;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 			if (booting_to_single_user)
6167c478bd9Sstevel@tonic-gate 				continue;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 			if (st->st_subgraph == NULL) {
6197c478bd9Sstevel@tonic-gate 				st->st_subgraph =
6207c478bd9Sstevel@tonic-gate 				    startd_alloc(max_scf_fmri_size);
6217c478bd9Sstevel@tonic-gate 				st->st_subgraph[0] = '\0';
6227c478bd9Sstevel@tonic-gate 			}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 			if (mp[0] == '\0' || strcmp(mp, "all") == 0) {
6257c478bd9Sstevel@tonic-gate 				(void) strcpy(st->st_subgraph, "all");
6267c478bd9Sstevel@tonic-gate 			} else if (strcmp(mp, "su") == 0 ||
6277c478bd9Sstevel@tonic-gate 			    strcmp(mp, "single-user") == 0) {
6287c478bd9Sstevel@tonic-gate 				(void) strcpy(st->st_subgraph,
6297c478bd9Sstevel@tonic-gate 				    "milestone/single-user:default");
6307c478bd9Sstevel@tonic-gate 			} else if (strcmp(mp, "mu") == 0 ||
6317c478bd9Sstevel@tonic-gate 			    strcmp(mp, "multi-user") == 0) {
6327c478bd9Sstevel@tonic-gate 				(void) strcpy(st->st_subgraph,
6337c478bd9Sstevel@tonic-gate 				    "milestone/multi-user:default");
6347c478bd9Sstevel@tonic-gate 			} else if (strcmp(mp, "mus") == 0 ||
6357c478bd9Sstevel@tonic-gate 			    strcmp(mp, "multi-user-server") == 0) {
6367c478bd9Sstevel@tonic-gate 				(void) strcpy(st->st_subgraph,
6377c478bd9Sstevel@tonic-gate 				    "milestone/multi-user-server:default");
6387c478bd9Sstevel@tonic-gate 			} else if (strcmp(mp, "none") == 0) {
6397c478bd9Sstevel@tonic-gate 				(void) strcpy(st->st_subgraph, "none");
6407c478bd9Sstevel@tonic-gate 			} else {
6417c478bd9Sstevel@tonic-gate 				log_framework(LOG_NOTICE,
6427c478bd9Sstevel@tonic-gate 				    "invalid milestone option value "
6437c478bd9Sstevel@tonic-gate 				    "'%s' ignored\n", mp);
6447c478bd9Sstevel@tonic-gate 			}
6457c478bd9Sstevel@tonic-gate 		} else {
6467c478bd9Sstevel@tonic-gate 			uu_warn("Unknown SMF option \"%s\".\n", cp);
6477c478bd9Sstevel@tonic-gate 		}
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	return (ret);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate /*
6547c478bd9Sstevel@tonic-gate  * void set_boot_env()
6557c478bd9Sstevel@tonic-gate  *
6567c478bd9Sstevel@tonic-gate  * If -r was passed or /reconfigure exists, this is a reconfig
6577c478bd9Sstevel@tonic-gate  * reboot.  We need to make sure that this information is given
6587c478bd9Sstevel@tonic-gate  * to the appropriate services the first time they're started
6597c478bd9Sstevel@tonic-gate  * by setting the system/reconfigure repository property,
6607c478bd9Sstevel@tonic-gate  * as well as pass the _INIT_RECONFIG variable on to the rcS
6617c478bd9Sstevel@tonic-gate  * start method so that legacy services can continue to use it.
6627c478bd9Sstevel@tonic-gate  *
6637c478bd9Sstevel@tonic-gate  * This function must never be called before contract_init(), as
6647c478bd9Sstevel@tonic-gate  * it sets st_initial.  get_startd_config() sets prop_reconfig from
6657c478bd9Sstevel@tonic-gate  * pre-existing repository state.
6667c478bd9Sstevel@tonic-gate  */
6677c478bd9Sstevel@tonic-gate static void
6687c478bd9Sstevel@tonic-gate set_boot_env()
6697c478bd9Sstevel@tonic-gate {
6707c478bd9Sstevel@tonic-gate 	struct stat sb;
6717c478bd9Sstevel@tonic-gate 	int r;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	/*
6747c478bd9Sstevel@tonic-gate 	 * Check if property still is set -- indicates we didn't get
6757c478bd9Sstevel@tonic-gate 	 * far enough previously to unset it.  Otherwise, if this isn't
6767c478bd9Sstevel@tonic-gate 	 * the first startup, don't re-process /reconfigure or the
6777c478bd9Sstevel@tonic-gate 	 * boot flag.
6787c478bd9Sstevel@tonic-gate 	 */
6797c478bd9Sstevel@tonic-gate 	if (prop_reconfig != 1 && st->st_initial != 1)
6807c478bd9Sstevel@tonic-gate 		return;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	/* If /reconfigure exists, also set opt_reconfig. */
6837c478bd9Sstevel@tonic-gate 	if (stat("/reconfigure", &sb) != -1)
6847c478bd9Sstevel@tonic-gate 		opt_reconfig = 1;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/* Nothing to do.  Just return. */
6877c478bd9Sstevel@tonic-gate 	if (opt_reconfig == 0 && prop_reconfig == 0)
6887c478bd9Sstevel@tonic-gate 		return;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	/*
6917c478bd9Sstevel@tonic-gate 	 * Set startd's reconfigure property.  This property is
6927c478bd9Sstevel@tonic-gate 	 * then cleared by successful completion of the single-user
6937c478bd9Sstevel@tonic-gate 	 * milestone.
6947c478bd9Sstevel@tonic-gate 	 */
6957c478bd9Sstevel@tonic-gate 	if (prop_reconfig != 1) {
6967c478bd9Sstevel@tonic-gate 		r = libscf_set_reconfig(1);
6977c478bd9Sstevel@tonic-gate 		switch (r) {
6987c478bd9Sstevel@tonic-gate 		case 0:
6997c478bd9Sstevel@tonic-gate 			break;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		case ENOENT:
7027c478bd9Sstevel@tonic-gate 		case EPERM:
7037c478bd9Sstevel@tonic-gate 		case EACCES:
7047c478bd9Sstevel@tonic-gate 		case EROFS:
7057c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING, "Could not set reconfiguration "
7067c478bd9Sstevel@tonic-gate 			    "property: %s\n", strerror(r));
7077c478bd9Sstevel@tonic-gate 			break;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		default:
7107c478bd9Sstevel@tonic-gate 			bad_error("libscf_set_reconfig", r);
7117c478bd9Sstevel@tonic-gate 		}
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate static void
7167c478bd9Sstevel@tonic-gate startup(int log_args)
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate 	ctid_t configd_ctid;
7197c478bd9Sstevel@tonic-gate 	int err;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	/*
7227c478bd9Sstevel@tonic-gate 	 * Initialize data structures.
7237c478bd9Sstevel@tonic-gate 	 */
7247c478bd9Sstevel@tonic-gate 	gu = startd_zalloc(sizeof (graph_update_t));
7257c478bd9Sstevel@tonic-gate 	ru = startd_zalloc(sizeof (restarter_update_t));
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	(void) pthread_cond_init(&st->st_load_cv, NULL);
7287c478bd9Sstevel@tonic-gate 	(void) pthread_cond_init(&st->st_configd_live_cv, NULL);
7297c478bd9Sstevel@tonic-gate 	(void) pthread_cond_init(&gu->gu_cv, NULL);
7307c478bd9Sstevel@tonic-gate 	(void) pthread_cond_init(&gu->gu_freeze_cv, NULL);
7317c478bd9Sstevel@tonic-gate 	(void) pthread_cond_init(&ru->restarter_update_cv, NULL);
7327c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&st->st_load_lock, &mutex_attrs);
7337c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&st->st_configd_live_lock, &mutex_attrs);
7347c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&gu->gu_lock, &mutex_attrs);
7357c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&gu->gu_freeze_lock, &mutex_attrs);
7367c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&ru->restarter_update_lock, &mutex_attrs);
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	configd_ctid = contract_init();
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	if (configd_ctid != -1)
7417c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Existing configd contract %ld; not "
7427c478bd9Sstevel@tonic-gate 		    "starting svc.configd\n", configd_ctid);
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	(void) startd_thread_create(fork_configd_thread, (void *)configd_ctid);
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	/*
7477c478bd9Sstevel@tonic-gate 	 * Await, if necessary, configd's initial arrival.
7487c478bd9Sstevel@tonic-gate 	 */
7497c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&st->st_configd_live_lock);
7507c478bd9Sstevel@tonic-gate 	while (!st->st_configd_lives) {
7517c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Awaiting cv signal on "
7527c478bd9Sstevel@tonic-gate 		    "configd_live_cv\n");
7537c478bd9Sstevel@tonic-gate 		err = pthread_cond_wait(&st->st_configd_live_cv,
7547c478bd9Sstevel@tonic-gate 		    &st->st_configd_live_lock);
7557c478bd9Sstevel@tonic-gate 		assert(err == 0);
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&st->st_configd_live_lock);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	utmpx_init();
7607c478bd9Sstevel@tonic-gate 	wait_init();
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	if (read_startd_config(log_args))
7637c478bd9Sstevel@tonic-gate 		log_framework(LOG_INFO, "svc.configd unable to provide startd "
7647c478bd9Sstevel@tonic-gate 		    "optional settings\n");
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	log_init();
7677c478bd9Sstevel@tonic-gate 	dict_init();
7687c478bd9Sstevel@tonic-gate 	timeout_init();
7697c478bd9Sstevel@tonic-gate 	restarter_protocol_init();
7707c478bd9Sstevel@tonic-gate 	restarter_init();
7717c478bd9Sstevel@tonic-gate 	graph_protocol_init();
7727c478bd9Sstevel@tonic-gate 	graph_init();
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	init_env();
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	set_boot_env();
7777c478bd9Sstevel@tonic-gate 	restarter_start();
7787c478bd9Sstevel@tonic-gate 	graph_engine_start();
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate static void
7827c478bd9Sstevel@tonic-gate usage(const char *name)
7837c478bd9Sstevel@tonic-gate {
7847c478bd9Sstevel@tonic-gate 	uu_warn(gettext("usage: %s [-dnq]\n"), name);
7857c478bd9Sstevel@tonic-gate 	exit(UU_EXIT_USAGE);
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate static int
7897c478bd9Sstevel@tonic-gate daemonize_start(void)
7907c478bd9Sstevel@tonic-gate {
7917c478bd9Sstevel@tonic-gate 	pid_t pid;
7927c478bd9Sstevel@tonic-gate 	int fd;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	if ((pid = fork1()) < 0)
7957c478bd9Sstevel@tonic-gate 		return (-1);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	if (pid != 0)
7987c478bd9Sstevel@tonic-gate 		exit(0);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	(void) close(0);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	if ((fd = open("/dev/null", O_RDONLY)) == -1) {
8037c478bd9Sstevel@tonic-gate 		uu_warn(gettext("can't connect stdin to /dev/null"));
8047c478bd9Sstevel@tonic-gate 	} else if (fd != 0) {
8057c478bd9Sstevel@tonic-gate 		(void) dup2(fd, 0);
8067c478bd9Sstevel@tonic-gate 		startd_close(fd);
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	closefrom(3);
8107c478bd9Sstevel@tonic-gate 	(void) dup2(2, 1);
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	(void) setsid();
8137c478bd9Sstevel@tonic-gate 	(void) chdir("/");
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	/* Use default umask that init handed us, but 022 to create files. */
8167c478bd9Sstevel@tonic-gate 	dmask = umask(022);
8177c478bd9Sstevel@tonic-gate 	fmask = umask(dmask);
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	return (0);
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8237c478bd9Sstevel@tonic-gate static void
8247c478bd9Sstevel@tonic-gate die_handler(int sig, siginfo_t *info, void *data)
8257c478bd9Sstevel@tonic-gate {
8267c478bd9Sstevel@tonic-gate 	finished = 1;
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate int
8307c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
8317c478bd9Sstevel@tonic-gate {
8327c478bd9Sstevel@tonic-gate 	int opt;
8337c478bd9Sstevel@tonic-gate 	int daemonize = 1;
8347c478bd9Sstevel@tonic-gate 	int log_args = 0;
8357c478bd9Sstevel@tonic-gate 	struct sigaction act;
8367c478bd9Sstevel@tonic-gate 	sigset_t nullset;
8377c478bd9Sstevel@tonic-gate 	struct stat sb;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	(void) uu_setpname(argv[0]);
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	st = startd_zalloc(sizeof (startd_state_t));
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	(void) pthread_mutexattr_init(&mutex_attrs);
8447c478bd9Sstevel@tonic-gate #ifndef	NDEBUG
8457c478bd9Sstevel@tonic-gate 	(void) pthread_mutexattr_settype(&mutex_attrs,
8467c478bd9Sstevel@tonic-gate 	    PTHREAD_MUTEX_ERRORCHECK);
8477c478bd9Sstevel@tonic-gate #endif
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	max_scf_name_size = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH);
8507c478bd9Sstevel@tonic-gate 	max_scf_value_size = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
8517c478bd9Sstevel@tonic-gate 	max_scf_fmri_size = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	if (max_scf_name_size == -1 || max_scf_value_size == -1 ||
8547c478bd9Sstevel@tonic-gate 	    max_scf_value_size == -1)
8557c478bd9Sstevel@tonic-gate 		uu_die("Can't determine repository maximum lengths.\n");
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	max_scf_name_size++;
8587c478bd9Sstevel@tonic-gate 	max_scf_value_size++;
8597c478bd9Sstevel@tonic-gate 	max_scf_fmri_size++;
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	st->st_log_flags = STARTD_LOG_FILE;
8627c478bd9Sstevel@tonic-gate 	st->st_log_level_min = LOG_INFO;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "dnqrs")) != EOF) {
8657c478bd9Sstevel@tonic-gate 		switch (opt) {
8667c478bd9Sstevel@tonic-gate 		case 'd':
8677c478bd9Sstevel@tonic-gate 			st->st_log_flags =
8687c478bd9Sstevel@tonic-gate 			    STARTD_LOG_FILE | STARTD_LOG_TERMINAL;
8697c478bd9Sstevel@tonic-gate 			st->st_log_level_min = LOG_DEBUG;
8707c478bd9Sstevel@tonic-gate 			log_args = 1;
8717c478bd9Sstevel@tonic-gate 			break;
8727c478bd9Sstevel@tonic-gate 		case 'n':
8737c478bd9Sstevel@tonic-gate 			daemonize = 0;
8747c478bd9Sstevel@tonic-gate 			break;
8757c478bd9Sstevel@tonic-gate 		case 'q':
8767c478bd9Sstevel@tonic-gate 			st->st_log_flags = 0;
8777c478bd9Sstevel@tonic-gate 			st->st_log_level_min = LOG_NOTICE;
8787c478bd9Sstevel@tonic-gate 			log_args = 1;
8797c478bd9Sstevel@tonic-gate 			break;
8807c478bd9Sstevel@tonic-gate 		case 'r':			/* reconfiguration boot */
8817c478bd9Sstevel@tonic-gate 			opt_reconfig = 1;
8827c478bd9Sstevel@tonic-gate 			break;
8837c478bd9Sstevel@tonic-gate 		case 's':			/* single-user mode */
8847c478bd9Sstevel@tonic-gate 			booting_to_single_user = B_TRUE;
8857c478bd9Sstevel@tonic-gate 			break;
8867c478bd9Sstevel@tonic-gate 		default:
8877c478bd9Sstevel@tonic-gate 			usage(argv[0]);		/* exits */
8887c478bd9Sstevel@tonic-gate 		}
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	if (optind != argc)
8927c478bd9Sstevel@tonic-gate 		usage(argv[0]);
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	if (daemonize)
8957c478bd9Sstevel@tonic-gate 		if (daemonize_start() < 0)
8967c478bd9Sstevel@tonic-gate 			uu_die("Can't daemonize\n");
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	log_init();
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	if (stat("/etc/svc/volatile/resetting", &sb) != -1) {
9017c478bd9Sstevel@tonic-gate 		log_framework(LOG_NOTICE, "Restarter quiesced.\n");
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 		for (;;)
9047c478bd9Sstevel@tonic-gate 			(void) pause();
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	act.sa_sigaction = &die_handler;
9087c478bd9Sstevel@tonic-gate 	(void) sigfillset(&act.sa_mask);
9097c478bd9Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
9107c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGINT, &act, NULL);
9117c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &act, NULL);
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	startup(log_args);
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&nullset);
9167c478bd9Sstevel@tonic-gate 	while (!finished) {
9177c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Main thread paused\n");
9187c478bd9Sstevel@tonic-gate 		(void) sigsuspend(&nullset);
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	(void) log_framework(LOG_DEBUG, "Restarter exiting.\n");
9227c478bd9Sstevel@tonic-gate 	return (0);
9237c478bd9Sstevel@tonic-gate }
924