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