xref: /onnv-gate/usr/src/cmd/svc/startd/startd.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * startd.c - the master restarter
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * svc.startd comprises two halves.  The graph engine is based in graph.c and
33*0Sstevel@tonic-gate  * maintains the service dependency graph based on the information in the
34*0Sstevel@tonic-gate  * repository.  For each service it also tracks the current state and the
35*0Sstevel@tonic-gate  * restarter responsible for the service.  Based on the graph, events from the
36*0Sstevel@tonic-gate  * repository (mostly administrative requests from svcadm), and messages from
37*0Sstevel@tonic-gate  * the restarters, the graph engine makes decisions about how the services
38*0Sstevel@tonic-gate  * should be manipulated and sends commands to the appropriate restarters.
39*0Sstevel@tonic-gate  * Communication between the graph engine and the restarters is embodied in
40*0Sstevel@tonic-gate  * protocol.c.
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  * The second half of svc.startd is the restarter for services managed by
43*0Sstevel@tonic-gate  * svc.startd and is primarily contained in restarter.c.  It responds to graph
44*0Sstevel@tonic-gate  * engine commands by executing methods, updating the repository, and sending
45*0Sstevel@tonic-gate  * feedback (mostly state updates) to the graph engine.
46*0Sstevel@tonic-gate  *
47*0Sstevel@tonic-gate  * Error handling
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  * In general, when svc.startd runs out of memory it reattempts a few times,
50*0Sstevel@tonic-gate  * sleeping inbetween, before giving up and exiting (see startd_alloc_retry()).
51*0Sstevel@tonic-gate  * When a repository connection is broken (libscf calls fail with
52*0Sstevel@tonic-gate  * SCF_ERROR_CONNECTION_BROKEN, librestart and internal functions return
53*0Sstevel@tonic-gate  * ECONNABORTED), svc.startd calls libscf_rebind_handle(), which coordinates
54*0Sstevel@tonic-gate  * with the svc.configd-restarting thread, fork_configd_thread(), via
55*0Sstevel@tonic-gate  * st->st_configd_live_cv, and rebinds the repository handle.  Doing so resets
56*0Sstevel@tonic-gate  * all libscf state associated with that handle, so functions which do this
57*0Sstevel@tonic-gate  * should communicate the event to their callers (usually by returning
58*0Sstevel@tonic-gate  * ECONNRESET) so they may reset their state appropriately.
59*0Sstevel@tonic-gate  */
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate #include <stdio.h>
62*0Sstevel@tonic-gate #include <sys/mnttab.h>		/* uses FILE * without including stdio.h */
63*0Sstevel@tonic-gate #include <alloca.h>
64*0Sstevel@tonic-gate #include <sys/mount.h>
65*0Sstevel@tonic-gate #include <sys/stat.h>
66*0Sstevel@tonic-gate #include <sys/types.h>
67*0Sstevel@tonic-gate #include <sys/wait.h>
68*0Sstevel@tonic-gate #include <assert.h>
69*0Sstevel@tonic-gate #include <errno.h>
70*0Sstevel@tonic-gate #include <fcntl.h>
71*0Sstevel@tonic-gate #include <ftw.h>
72*0Sstevel@tonic-gate #include <libintl.h>
73*0Sstevel@tonic-gate #include <libscf.h>
74*0Sstevel@tonic-gate #include <libscf_priv.h>
75*0Sstevel@tonic-gate #include <libuutil.h>
76*0Sstevel@tonic-gate #include <locale.h>
77*0Sstevel@tonic-gate #include <poll.h>
78*0Sstevel@tonic-gate #include <pthread.h>
79*0Sstevel@tonic-gate #include <signal.h>
80*0Sstevel@tonic-gate #include <stdarg.h>
81*0Sstevel@tonic-gate #include <stdlib.h>
82*0Sstevel@tonic-gate #include <string.h>
83*0Sstevel@tonic-gate #include <strings.h>
84*0Sstevel@tonic-gate #include <unistd.h>
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate #include "startd.h"
87*0Sstevel@tonic-gate #include "protocol.h"
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate ssize_t max_scf_name_size;
90*0Sstevel@tonic-gate ssize_t max_scf_fmri_size;
91*0Sstevel@tonic-gate ssize_t max_scf_value_size;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate mode_t fmask;
94*0Sstevel@tonic-gate mode_t dmask;
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate graph_update_t *gu;
97*0Sstevel@tonic-gate restarter_update_t *ru;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate startd_state_t *st;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate boolean_t booting_to_single_user = B_FALSE;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate const char * const admin_actions[] = {
104*0Sstevel@tonic-gate     SCF_PROPERTY_DEGRADED,
105*0Sstevel@tonic-gate     SCF_PROPERTY_MAINT_OFF,
106*0Sstevel@tonic-gate     SCF_PROPERTY_MAINT_ON,
107*0Sstevel@tonic-gate     SCF_PROPERTY_MAINT_ON_IMMEDIATE,
108*0Sstevel@tonic-gate     SCF_PROPERTY_REFRESH,
109*0Sstevel@tonic-gate     SCF_PROPERTY_RESTART
110*0Sstevel@tonic-gate };
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate const int admin_events[NACTIONS] = {
113*0Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_DEGRADED,
114*0Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF,
115*0Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON,
116*0Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE,
117*0Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_REFRESH,
118*0Sstevel@tonic-gate     RESTARTER_EVENT_TYPE_ADMIN_RESTART
119*0Sstevel@tonic-gate };
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate const char * const instance_state_str[] = {
122*0Sstevel@tonic-gate 	"none",
123*0Sstevel@tonic-gate 	"uninitialized",
124*0Sstevel@tonic-gate 	"maintenance",
125*0Sstevel@tonic-gate 	"offline",
126*0Sstevel@tonic-gate 	"disabled",
127*0Sstevel@tonic-gate 	"online",
128*0Sstevel@tonic-gate 	"degraded"
129*0Sstevel@tonic-gate };
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate static int finished = 0;
132*0Sstevel@tonic-gate static int opt_reconfig = 0;
133*0Sstevel@tonic-gate static uint8_t prop_reconfig = 0;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate #define	INITIAL_REBIND_ATTEMPTS	5
136*0Sstevel@tonic-gate #define	INITIAL_REBIND_DELAY	3
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate pthread_mutexattr_t mutex_attrs;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate const char *
141*0Sstevel@tonic-gate _umem_debug_init(void)
142*0Sstevel@tonic-gate {
143*0Sstevel@tonic-gate 	return ("default,verbose");	/* UMEM_DEBUG setting */
144*0Sstevel@tonic-gate }
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate const char *
147*0Sstevel@tonic-gate _umem_logging_init(void)
148*0Sstevel@tonic-gate {
149*0Sstevel@tonic-gate 	return ("fail,contents");	/* UMEM_LOGGING setting */
150*0Sstevel@tonic-gate }
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate /*
153*0Sstevel@tonic-gate  * startd_alloc_retry()
154*0Sstevel@tonic-gate  *   Wrapper for allocation functions.  Retries with a decaying time
155*0Sstevel@tonic-gate  *   value on failure to allocate, and aborts startd if failure is
156*0Sstevel@tonic-gate  *   persistent.
157*0Sstevel@tonic-gate  */
158*0Sstevel@tonic-gate void *
159*0Sstevel@tonic-gate startd_alloc_retry(void *f(size_t, int), size_t sz)
160*0Sstevel@tonic-gate {
161*0Sstevel@tonic-gate 	void *p;
162*0Sstevel@tonic-gate 	uint_t try, msecs;
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	p = f(sz, UMEM_DEFAULT);
165*0Sstevel@tonic-gate 	if (p != NULL || sz == 0)
166*0Sstevel@tonic-gate 		return (p);
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	for (try = 0; p == NULL && try < ALLOC_RETRY; ++try) {
171*0Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
172*0Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
173*0Sstevel@tonic-gate 		p = f(sz, UMEM_DEFAULT);
174*0Sstevel@tonic-gate 		if (p != NULL)
175*0Sstevel@tonic-gate 			return (p);
176*0Sstevel@tonic-gate 	}
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
179*0Sstevel@tonic-gate 	/* NOTREACHED */
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate void *
183*0Sstevel@tonic-gate safe_realloc(void *p, size_t sz)
184*0Sstevel@tonic-gate {
185*0Sstevel@tonic-gate 	uint_t try, msecs;
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	p = realloc(p, sz);
188*0Sstevel@tonic-gate 	if (p != NULL || sz == 0)
189*0Sstevel@tonic-gate 		return (p);
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	for (try = 0; errno == EAGAIN && try < ALLOC_RETRY; ++try) {
194*0Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
195*0Sstevel@tonic-gate 		p = realloc(p, sz);
196*0Sstevel@tonic-gate 		if (p != NULL)
197*0Sstevel@tonic-gate 			return (p);
198*0Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
199*0Sstevel@tonic-gate 	}
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
202*0Sstevel@tonic-gate 	/* NOTREACHED */
203*0Sstevel@tonic-gate }
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate char *
206*0Sstevel@tonic-gate safe_strdup(const char *s)
207*0Sstevel@tonic-gate {
208*0Sstevel@tonic-gate 	uint_t try, msecs;
209*0Sstevel@tonic-gate 	char *d;
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	d = strdup(s);
212*0Sstevel@tonic-gate 	if (d != NULL)
213*0Sstevel@tonic-gate 		return (d);
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	for (try = 0;
218*0Sstevel@tonic-gate 	    (errno == EAGAIN || errno == ENOMEM) && try < ALLOC_RETRY;
219*0Sstevel@tonic-gate 	    ++try) {
220*0Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
221*0Sstevel@tonic-gate 		d = strdup(s);
222*0Sstevel@tonic-gate 		if (d != NULL)
223*0Sstevel@tonic-gate 			return (d);
224*0Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
225*0Sstevel@tonic-gate 	}
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
228*0Sstevel@tonic-gate 	/* NOTREACHED */
229*0Sstevel@tonic-gate }
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate void
233*0Sstevel@tonic-gate startd_free(void *p, size_t sz)
234*0Sstevel@tonic-gate {
235*0Sstevel@tonic-gate 	umem_free(p, sz);
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate /*
239*0Sstevel@tonic-gate  * Creates a uu_list_pool_t with the same retry policy as startd_alloc().
240*0Sstevel@tonic-gate  * Only returns NULL for UU_ERROR_UNKNOWN_FLAG and UU_ERROR_NOT_SUPPORTED.
241*0Sstevel@tonic-gate  */
242*0Sstevel@tonic-gate uu_list_pool_t *
243*0Sstevel@tonic-gate startd_list_pool_create(const char *name, size_t e, size_t o,
244*0Sstevel@tonic-gate     uu_compare_fn_t *f, uint32_t flags)
245*0Sstevel@tonic-gate {
246*0Sstevel@tonic-gate 	uu_list_pool_t *pool;
247*0Sstevel@tonic-gate 	uint_t try, msecs;
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	pool = uu_list_pool_create(name, e, o, f, flags);
250*0Sstevel@tonic-gate 	if (pool != NULL)
251*0Sstevel@tonic-gate 		return (pool);
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	for (try = 0; uu_error() == UU_ERROR_NO_MEMORY && try < ALLOC_RETRY;
256*0Sstevel@tonic-gate 	    ++try) {
257*0Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
258*0Sstevel@tonic-gate 		pool = uu_list_pool_create(name, e, o, f, flags);
259*0Sstevel@tonic-gate 		if (pool != NULL)
260*0Sstevel@tonic-gate 			return (pool);
261*0Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
262*0Sstevel@tonic-gate 	}
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	if (try < ALLOC_RETRY)
265*0Sstevel@tonic-gate 		return (NULL);
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
268*0Sstevel@tonic-gate 	/* NOTREACHED */
269*0Sstevel@tonic-gate }
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate /*
272*0Sstevel@tonic-gate  * Creates a uu_list_t with the same retry policy as startd_alloc().  Only
273*0Sstevel@tonic-gate  * returns NULL for UU_ERROR_UNKNOWN_FLAG and UU_ERROR_NOT_SUPPORTED.
274*0Sstevel@tonic-gate  */
275*0Sstevel@tonic-gate uu_list_t *
276*0Sstevel@tonic-gate startd_list_create(uu_list_pool_t *pool, void *parent, uint32_t flags)
277*0Sstevel@tonic-gate {
278*0Sstevel@tonic-gate 	uu_list_t *list;
279*0Sstevel@tonic-gate 	uint_t try, msecs;
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	list = uu_list_create(pool, parent, flags);
282*0Sstevel@tonic-gate 	if (list != NULL)
283*0Sstevel@tonic-gate 		return (list);
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	msecs = ALLOC_DELAY;
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	for (try = 0; uu_error() == UU_ERROR_NO_MEMORY && try < ALLOC_RETRY;
288*0Sstevel@tonic-gate 	    ++try) {
289*0Sstevel@tonic-gate 		(void) poll(NULL, 0, msecs);
290*0Sstevel@tonic-gate 		list = uu_list_create(pool, parent, flags);
291*0Sstevel@tonic-gate 		if (list != NULL)
292*0Sstevel@tonic-gate 			return (list);
293*0Sstevel@tonic-gate 		msecs *= ALLOC_DELAY_MULT;
294*0Sstevel@tonic-gate 	}
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	if (try < ALLOC_RETRY)
297*0Sstevel@tonic-gate 		return (NULL);
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	uu_die("Insufficient memory.\n");
300*0Sstevel@tonic-gate 	/* NOTREACHED */
301*0Sstevel@tonic-gate }
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate pthread_t
304*0Sstevel@tonic-gate startd_thread_create(void *(*func)(void *), void *ptr)
305*0Sstevel@tonic-gate {
306*0Sstevel@tonic-gate 	int err;
307*0Sstevel@tonic-gate 	pthread_t tid;
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	err = pthread_create(&tid, NULL, func, ptr);
310*0Sstevel@tonic-gate 	if (err != 0) {
311*0Sstevel@tonic-gate 		assert(err == EAGAIN);
312*0Sstevel@tonic-gate 		uu_die("Could not create thread.\n");
313*0Sstevel@tonic-gate 	}
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	err = pthread_detach(tid);
316*0Sstevel@tonic-gate 	assert(err == 0);
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 	return (tid);
319*0Sstevel@tonic-gate }
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate static int
323*0Sstevel@tonic-gate read_startd_config(int log_args)
324*0Sstevel@tonic-gate {
325*0Sstevel@tonic-gate 	scf_handle_t *hndl;
326*0Sstevel@tonic-gate 	scf_instance_t *inst;
327*0Sstevel@tonic-gate 	scf_propertygroup_t *pg;
328*0Sstevel@tonic-gate 	scf_property_t *prop;
329*0Sstevel@tonic-gate 	scf_value_t *val;
330*0Sstevel@tonic-gate 	scf_iter_t *iter, *piter;
331*0Sstevel@tonic-gate 	instance_data_t idata;
332*0Sstevel@tonic-gate 	char *buf, *vbuf;
333*0Sstevel@tonic-gate 	char *startd_options_fmri = uu_msprintf("%s/:properties/options",
334*0Sstevel@tonic-gate 	    SCF_SERVICE_STARTD);
335*0Sstevel@tonic-gate 	char *startd_reconfigure_fmri = uu_msprintf(
336*0Sstevel@tonic-gate 	    "%s/:properties/system/reconfigure", SCF_SERVICE_STARTD);
337*0Sstevel@tonic-gate 	char *env_opts, *lasts, *cp;
338*0Sstevel@tonic-gate 	int bind_fails = 0;
339*0Sstevel@tonic-gate 	int ret = 0, r;
340*0Sstevel@tonic-gate 	uint_t count = 0, msecs = ALLOC_DELAY;
341*0Sstevel@tonic-gate 	size_t sz;
342*0Sstevel@tonic-gate 	ctid_t ctid;
343*0Sstevel@tonic-gate 	uint64_t uint64;
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	buf = startd_alloc(max_scf_fmri_size);
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	if (startd_options_fmri == NULL || startd_reconfigure_fmri == NULL)
348*0Sstevel@tonic-gate 		uu_die("Allocation failure\n");
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	st->st_log_prefix = LOG_PREFIX_EARLY;
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	if ((st->st_log_file = getenv("STARTD_DEFAULT_LOG")) == NULL) {
353*0Sstevel@tonic-gate 		st->st_log_file = startd_alloc(strlen(STARTD_DEFAULT_LOG) + 1);
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate 		(void) strcpy(st->st_log_file, STARTD_DEFAULT_LOG);
356*0Sstevel@tonic-gate 	}
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 	st->st_door_path = getenv("STARTD_ALT_DOOR");
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate 	/*
361*0Sstevel@tonic-gate 	 * Read "options" property group.
362*0Sstevel@tonic-gate 	 */
363*0Sstevel@tonic-gate 	for (hndl = libscf_handle_create_bound(SCF_VERSION); hndl == NULL;
364*0Sstevel@tonic-gate 	    hndl = libscf_handle_create_bound(SCF_VERSION), bind_fails++) {
365*0Sstevel@tonic-gate 		(void) sleep(INITIAL_REBIND_DELAY);
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 		if (bind_fails > INITIAL_REBIND_ATTEMPTS) {
368*0Sstevel@tonic-gate 			/*
369*0Sstevel@tonic-gate 			 * In the case that we can't bind to the repository
370*0Sstevel@tonic-gate 			 * (which should have been started), we need to allow
371*0Sstevel@tonic-gate 			 * the user into maintenance mode to determine what's
372*0Sstevel@tonic-gate 			 * failed.
373*0Sstevel@tonic-gate 			 */
374*0Sstevel@tonic-gate 			log_framework(LOG_INFO, "Couldn't fetch "
375*0Sstevel@tonic-gate 			    "default settings: %s\n",
376*0Sstevel@tonic-gate 			    scf_strerror(scf_error()));
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 			ret = -1;
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 			goto noscfout;
381*0Sstevel@tonic-gate 		}
382*0Sstevel@tonic-gate 	}
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate 	idata.i_fmri = SCF_SERVICE_STARTD;
385*0Sstevel@tonic-gate 	idata.i_state = RESTARTER_STATE_NONE;
386*0Sstevel@tonic-gate 	idata.i_next_state = RESTARTER_STATE_NONE;
387*0Sstevel@tonic-gate timestamp:
388*0Sstevel@tonic-gate 	switch (r = _restarter_commit_states(hndl, &idata,
389*0Sstevel@tonic-gate 	    RESTARTER_STATE_ONLINE, RESTARTER_STATE_NONE, NULL)) {
390*0Sstevel@tonic-gate 	case 0:
391*0Sstevel@tonic-gate 		break;
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 	case ENOMEM:
394*0Sstevel@tonic-gate 		++count;
395*0Sstevel@tonic-gate 		if (count < ALLOC_RETRY) {
396*0Sstevel@tonic-gate 			(void) poll(NULL, 0, msecs);
397*0Sstevel@tonic-gate 			msecs *= ALLOC_DELAY_MULT;
398*0Sstevel@tonic-gate 			goto timestamp;
399*0Sstevel@tonic-gate 		}
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 		uu_die("Insufficient memory.\n");
402*0Sstevel@tonic-gate 		/* NOTREACHED */
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate 	case ECONNABORTED:
405*0Sstevel@tonic-gate 		libscf_handle_rebind(hndl);
406*0Sstevel@tonic-gate 		goto timestamp;
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	case ENOENT:
409*0Sstevel@tonic-gate 	case EPERM:
410*0Sstevel@tonic-gate 	case EACCES:
411*0Sstevel@tonic-gate 	case EROFS:
412*0Sstevel@tonic-gate 		log_error(LOG_INFO, "Could set state of %s: %s.\n",
413*0Sstevel@tonic-gate 		    idata.i_fmri, strerror(r));
414*0Sstevel@tonic-gate 		break;
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 	case EINVAL:
417*0Sstevel@tonic-gate 	default:
418*0Sstevel@tonic-gate 		bad_error("_restarter_commit_states", r);
419*0Sstevel@tonic-gate 	}
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 	pg = safe_scf_pg_create(hndl);
422*0Sstevel@tonic-gate 	prop = safe_scf_property_create(hndl);
423*0Sstevel@tonic-gate 	val = safe_scf_value_create(hndl);
424*0Sstevel@tonic-gate 	inst = safe_scf_instance_create(hndl);
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 	/* set startd's restarter properties */
427*0Sstevel@tonic-gate 	if (scf_handle_decode_fmri(hndl, SCF_SERVICE_STARTD, NULL, NULL, inst,
428*0Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) == 0) {
429*0Sstevel@tonic-gate 		(void) libscf_write_start_pid(inst, getpid());
430*0Sstevel@tonic-gate 		ctid = proc_get_ctid();
431*0Sstevel@tonic-gate 		if (ctid != -1) {
432*0Sstevel@tonic-gate 			uint64 = (uint64_t)ctid;
433*0Sstevel@tonic-gate 			(void) libscf_inst_set_count_prop(inst,
434*0Sstevel@tonic-gate 			    SCF_PG_RESTARTER, SCF_PG_RESTARTER_TYPE,
435*0Sstevel@tonic-gate 			    SCF_PG_RESTARTER_FLAGS, SCF_PROPERTY_CONTRACT,
436*0Sstevel@tonic-gate 			    uint64);
437*0Sstevel@tonic-gate 		}
438*0Sstevel@tonic-gate 		(void) libscf_note_method_log(inst, LOG_PREFIX_EARLY,
439*0Sstevel@tonic-gate 		    STARTD_DEFAULT_LOG);
440*0Sstevel@tonic-gate 		(void) libscf_note_method_log(inst, LOG_PREFIX_NORMAL,
441*0Sstevel@tonic-gate 		    STARTD_DEFAULT_LOG);
442*0Sstevel@tonic-gate 	}
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 	/* Read reconfigure property for recovery. */
445*0Sstevel@tonic-gate 	if (scf_handle_decode_fmri(hndl, startd_reconfigure_fmri, NULL, NULL,
446*0Sstevel@tonic-gate 	    NULL, NULL, prop, NULL) != -1 &&
447*0Sstevel@tonic-gate 	    scf_property_get_value(prop, val) == 0)
448*0Sstevel@tonic-gate 		(void) scf_value_get_boolean(val, &prop_reconfig);
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 	if (scf_handle_decode_fmri(hndl, startd_options_fmri, NULL, NULL, NULL,
451*0Sstevel@tonic-gate 	    pg, NULL, SCF_DECODE_FMRI_TRUNCATE) == -1) {
452*0Sstevel@tonic-gate 		/*
453*0Sstevel@tonic-gate 		 * No configuration options defined.
454*0Sstevel@tonic-gate 		 */
455*0Sstevel@tonic-gate 		if (scf_error() != SCF_ERROR_NOT_FOUND)
456*0Sstevel@tonic-gate 			uu_warn("Couldn't read configuration from 'options' "
457*0Sstevel@tonic-gate 			    "group: %s\n", scf_strerror(scf_error()));
458*0Sstevel@tonic-gate 		goto scfout;
459*0Sstevel@tonic-gate 	}
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 	/*
462*0Sstevel@tonic-gate 	 * If there is no "options" group defined, then our defaults are fine.
463*0Sstevel@tonic-gate 	 */
464*0Sstevel@tonic-gate 	if (scf_pg_get_name(pg, NULL, 0) < 0)
465*0Sstevel@tonic-gate 		goto scfout;
466*0Sstevel@tonic-gate 
467*0Sstevel@tonic-gate 	/* Iterate through. */
468*0Sstevel@tonic-gate 	iter = safe_scf_iter_create(hndl);
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 	(void) scf_iter_pg_properties(iter, pg);
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate 	piter = safe_scf_iter_create(hndl);
473*0Sstevel@tonic-gate 	vbuf = startd_alloc(max_scf_value_size);
474*0Sstevel@tonic-gate 
475*0Sstevel@tonic-gate 	while ((scf_iter_next_property(iter, prop) == 1)) {
476*0Sstevel@tonic-gate 		scf_type_t ty;
477*0Sstevel@tonic-gate 
478*0Sstevel@tonic-gate 		if (scf_property_get_name(prop, buf, max_scf_fmri_size) < 0)
479*0Sstevel@tonic-gate 			continue;
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 		if (strcmp(buf, "logging") != 0 &&
482*0Sstevel@tonic-gate 		    strcmp(buf, "boot_messages") != 0)
483*0Sstevel@tonic-gate 			continue;
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 		if (scf_property_type(prop, &ty) != 0) {
486*0Sstevel@tonic-gate 			switch (scf_error()) {
487*0Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
488*0Sstevel@tonic-gate 			default:
489*0Sstevel@tonic-gate 				libscf_handle_rebind(hndl);
490*0Sstevel@tonic-gate 				continue;
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
493*0Sstevel@tonic-gate 				continue;
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
496*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
497*0Sstevel@tonic-gate 				bad_error("scf_property_type", scf_error());
498*0Sstevel@tonic-gate 			}
499*0Sstevel@tonic-gate 		}
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 		if (ty != SCF_TYPE_ASTRING) {
502*0Sstevel@tonic-gate 			uu_warn("property \"options/%s\" is not of type "
503*0Sstevel@tonic-gate 			    "astring; ignored.\n", buf);
504*0Sstevel@tonic-gate 			continue;
505*0Sstevel@tonic-gate 		}
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 		if (scf_property_get_value(prop, val) != 0) {
508*0Sstevel@tonic-gate 			switch (scf_error()) {
509*0Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
510*0Sstevel@tonic-gate 			default:
511*0Sstevel@tonic-gate 				return (ECONNABORTED);
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
514*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
515*0Sstevel@tonic-gate 				return (0);
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
518*0Sstevel@tonic-gate 				uu_warn("property \"options/%s\" has multiple "
519*0Sstevel@tonic-gate 				    "values; ignored.\n", buf);
520*0Sstevel@tonic-gate 				continue;
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
523*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
524*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
525*0Sstevel@tonic-gate 				bad_error("scf_property_get_value",
526*0Sstevel@tonic-gate 				    scf_error());
527*0Sstevel@tonic-gate 			}
528*0Sstevel@tonic-gate 		}
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate 		if (scf_value_get_astring(val, vbuf, max_scf_value_size) < 0)
531*0Sstevel@tonic-gate 			bad_error("scf_value_get_astring", scf_error());
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 		if (!log_args && strcmp("logging", buf) == 0) {
534*0Sstevel@tonic-gate 			if (strcmp("verbose", vbuf) == 0) {
535*0Sstevel@tonic-gate 				st->st_boot_flags = STARTD_BOOT_VERBOSE;
536*0Sstevel@tonic-gate 				st->st_log_flags = STARTD_LOG_VERBOSE;
537*0Sstevel@tonic-gate 				st->st_log_level_min = LOG_INFO;
538*0Sstevel@tonic-gate 			} else if (strcmp("debug", vbuf) == 0) {
539*0Sstevel@tonic-gate 				st->st_boot_flags = STARTD_BOOT_VERBOSE;
540*0Sstevel@tonic-gate 				st->st_log_flags = STARTD_LOG_DEBUG;
541*0Sstevel@tonic-gate 				st->st_log_level_min = LOG_DEBUG;
542*0Sstevel@tonic-gate 			} else if (strcmp("quiet", vbuf) == 0) {
543*0Sstevel@tonic-gate 				st->st_log_flags = STARTD_LOG_QUIET;
544*0Sstevel@tonic-gate 				st->st_log_level_min = LOG_NOTICE;
545*0Sstevel@tonic-gate 			} else {
546*0Sstevel@tonic-gate 				uu_warn("unknown options/logging "
547*0Sstevel@tonic-gate 				    "value '%s' ignored\n", vbuf);
548*0Sstevel@tonic-gate 			}
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 		} else if (strcmp("boot_messages", buf) == 0) {
551*0Sstevel@tonic-gate 			if (strcmp("quiet", vbuf) == 0) {
552*0Sstevel@tonic-gate 				st->st_boot_flags = STARTD_BOOT_QUIET;
553*0Sstevel@tonic-gate 			} else if (strcmp("verbose", vbuf) == 0) {
554*0Sstevel@tonic-gate 				st->st_boot_flags = STARTD_BOOT_VERBOSE;
555*0Sstevel@tonic-gate 			} else {
556*0Sstevel@tonic-gate 				log_framework(LOG_NOTICE, "unknown "
557*0Sstevel@tonic-gate 				    "options/boot_messages value '%s' "
558*0Sstevel@tonic-gate 				    "ignored\n", vbuf);
559*0Sstevel@tonic-gate 			}
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 		}
562*0Sstevel@tonic-gate 	}
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 	startd_free(vbuf, max_scf_value_size);
565*0Sstevel@tonic-gate 	scf_iter_destroy(piter);
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate 	scf_iter_destroy(iter);
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate scfout:
570*0Sstevel@tonic-gate 	scf_value_destroy(val);
571*0Sstevel@tonic-gate 	scf_pg_destroy(pg);
572*0Sstevel@tonic-gate 	scf_property_destroy(prop);
573*0Sstevel@tonic-gate 	scf_instance_destroy(inst);
574*0Sstevel@tonic-gate 	(void) scf_handle_unbind(hndl);
575*0Sstevel@tonic-gate 	scf_handle_destroy(hndl);
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate noscfout:
578*0Sstevel@tonic-gate 	startd_free(buf, max_scf_fmri_size);
579*0Sstevel@tonic-gate 	uu_free(startd_options_fmri);
580*0Sstevel@tonic-gate 	uu_free(startd_reconfigure_fmri);
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 	if (booting_to_single_user) {
583*0Sstevel@tonic-gate 		st->st_subgraph = startd_alloc(max_scf_fmri_size);
584*0Sstevel@tonic-gate 		sz = strlcpy(st->st_subgraph, "milestone/single-user:default",
585*0Sstevel@tonic-gate 		    max_scf_fmri_size);
586*0Sstevel@tonic-gate 		assert(sz < max_scf_fmri_size);
587*0Sstevel@tonic-gate 	}
588*0Sstevel@tonic-gate 
589*0Sstevel@tonic-gate 	/*
590*0Sstevel@tonic-gate 	 * Options passed in as boot arguments override repository defaults.
591*0Sstevel@tonic-gate 	 */
592*0Sstevel@tonic-gate 	env_opts = getenv("SMF_OPTIONS");
593*0Sstevel@tonic-gate 	if (env_opts == NULL)
594*0Sstevel@tonic-gate 		return (ret);
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 	cp = strtok_r(env_opts, ",", &lasts);
597*0Sstevel@tonic-gate 	while (cp != NULL) {
598*0Sstevel@tonic-gate 		if (strcmp(cp, "debug") == 0) {
599*0Sstevel@tonic-gate 			st->st_boot_flags = STARTD_BOOT_VERBOSE;
600*0Sstevel@tonic-gate 			st->st_log_flags = STARTD_LOG_DEBUG;
601*0Sstevel@tonic-gate 			st->st_log_level_min = LOG_DEBUG;
602*0Sstevel@tonic-gate 		} else if (strcmp(cp, "verbose") == 0) {
603*0Sstevel@tonic-gate 			st->st_boot_flags = STARTD_BOOT_VERBOSE;
604*0Sstevel@tonic-gate 			st->st_log_flags = STARTD_LOG_VERBOSE;
605*0Sstevel@tonic-gate 			st->st_log_level_min = LOG_INFO;
606*0Sstevel@tonic-gate 		} else if (strcmp(cp, "seed") == 0) {
607*0Sstevel@tonic-gate 			uu_warn("SMF option \"%s\" unimplemented.\n", cp);
608*0Sstevel@tonic-gate 		} else if (strcmp(cp, "quiet") == 0) {
609*0Sstevel@tonic-gate 			st->st_log_flags = STARTD_LOG_QUIET;
610*0Sstevel@tonic-gate 			st->st_log_level_min = LOG_NOTICE;
611*0Sstevel@tonic-gate 		} else if (strncmp(cp, "milestone=",
612*0Sstevel@tonic-gate 		    sizeof ("milestone=") - 1) == 0) {
613*0Sstevel@tonic-gate 			char *mp = cp + sizeof ("milestone=") - 1;
614*0Sstevel@tonic-gate 
615*0Sstevel@tonic-gate 			if (booting_to_single_user)
616*0Sstevel@tonic-gate 				continue;
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate 			if (st->st_subgraph == NULL) {
619*0Sstevel@tonic-gate 				st->st_subgraph =
620*0Sstevel@tonic-gate 				    startd_alloc(max_scf_fmri_size);
621*0Sstevel@tonic-gate 				st->st_subgraph[0] = '\0';
622*0Sstevel@tonic-gate 			}
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate 			if (mp[0] == '\0' || strcmp(mp, "all") == 0) {
625*0Sstevel@tonic-gate 				(void) strcpy(st->st_subgraph, "all");
626*0Sstevel@tonic-gate 			} else if (strcmp(mp, "su") == 0 ||
627*0Sstevel@tonic-gate 			    strcmp(mp, "single-user") == 0) {
628*0Sstevel@tonic-gate 				(void) strcpy(st->st_subgraph,
629*0Sstevel@tonic-gate 				    "milestone/single-user:default");
630*0Sstevel@tonic-gate 			} else if (strcmp(mp, "mu") == 0 ||
631*0Sstevel@tonic-gate 			    strcmp(mp, "multi-user") == 0) {
632*0Sstevel@tonic-gate 				(void) strcpy(st->st_subgraph,
633*0Sstevel@tonic-gate 				    "milestone/multi-user:default");
634*0Sstevel@tonic-gate 			} else if (strcmp(mp, "mus") == 0 ||
635*0Sstevel@tonic-gate 			    strcmp(mp, "multi-user-server") == 0) {
636*0Sstevel@tonic-gate 				(void) strcpy(st->st_subgraph,
637*0Sstevel@tonic-gate 				    "milestone/multi-user-server:default");
638*0Sstevel@tonic-gate 			} else if (strcmp(mp, "none") == 0) {
639*0Sstevel@tonic-gate 				(void) strcpy(st->st_subgraph, "none");
640*0Sstevel@tonic-gate 			} else {
641*0Sstevel@tonic-gate 				log_framework(LOG_NOTICE,
642*0Sstevel@tonic-gate 				    "invalid milestone option value "
643*0Sstevel@tonic-gate 				    "'%s' ignored\n", mp);
644*0Sstevel@tonic-gate 			}
645*0Sstevel@tonic-gate 		} else {
646*0Sstevel@tonic-gate 			uu_warn("Unknown SMF option \"%s\".\n", cp);
647*0Sstevel@tonic-gate 		}
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate 		cp = strtok_r(NULL, ",", &lasts);
650*0Sstevel@tonic-gate 	}
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate 	return (ret);
653*0Sstevel@tonic-gate }
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate /*
656*0Sstevel@tonic-gate  * void set_boot_env()
657*0Sstevel@tonic-gate  *
658*0Sstevel@tonic-gate  * If -r was passed or /reconfigure exists, this is a reconfig
659*0Sstevel@tonic-gate  * reboot.  We need to make sure that this information is given
660*0Sstevel@tonic-gate  * to the appropriate services the first time they're started
661*0Sstevel@tonic-gate  * by setting the system/reconfigure repository property,
662*0Sstevel@tonic-gate  * as well as pass the _INIT_RECONFIG variable on to the rcS
663*0Sstevel@tonic-gate  * start method so that legacy services can continue to use it.
664*0Sstevel@tonic-gate  *
665*0Sstevel@tonic-gate  * This function must never be called before contract_init(), as
666*0Sstevel@tonic-gate  * it sets st_initial.  get_startd_config() sets prop_reconfig from
667*0Sstevel@tonic-gate  * pre-existing repository state.
668*0Sstevel@tonic-gate  */
669*0Sstevel@tonic-gate static void
670*0Sstevel@tonic-gate set_boot_env()
671*0Sstevel@tonic-gate {
672*0Sstevel@tonic-gate 	struct stat sb;
673*0Sstevel@tonic-gate 	int r;
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate 	/*
676*0Sstevel@tonic-gate 	 * Check if property still is set -- indicates we didn't get
677*0Sstevel@tonic-gate 	 * far enough previously to unset it.  Otherwise, if this isn't
678*0Sstevel@tonic-gate 	 * the first startup, don't re-process /reconfigure or the
679*0Sstevel@tonic-gate 	 * boot flag.
680*0Sstevel@tonic-gate 	 */
681*0Sstevel@tonic-gate 	if (prop_reconfig != 1 && st->st_initial != 1)
682*0Sstevel@tonic-gate 		return;
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 	/* If /reconfigure exists, also set opt_reconfig. */
685*0Sstevel@tonic-gate 	if (stat("/reconfigure", &sb) != -1)
686*0Sstevel@tonic-gate 		opt_reconfig = 1;
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate 	/* Nothing to do.  Just return. */
689*0Sstevel@tonic-gate 	if (opt_reconfig == 0 && prop_reconfig == 0)
690*0Sstevel@tonic-gate 		return;
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 	/*
693*0Sstevel@tonic-gate 	 * Set startd's reconfigure property.  This property is
694*0Sstevel@tonic-gate 	 * then cleared by successful completion of the single-user
695*0Sstevel@tonic-gate 	 * milestone.
696*0Sstevel@tonic-gate 	 */
697*0Sstevel@tonic-gate 	if (prop_reconfig != 1) {
698*0Sstevel@tonic-gate 		r = libscf_set_reconfig(1);
699*0Sstevel@tonic-gate 		switch (r) {
700*0Sstevel@tonic-gate 		case 0:
701*0Sstevel@tonic-gate 			break;
702*0Sstevel@tonic-gate 
703*0Sstevel@tonic-gate 		case ENOENT:
704*0Sstevel@tonic-gate 		case EPERM:
705*0Sstevel@tonic-gate 		case EACCES:
706*0Sstevel@tonic-gate 		case EROFS:
707*0Sstevel@tonic-gate 			log_error(LOG_WARNING, "Could not set reconfiguration "
708*0Sstevel@tonic-gate 			    "property: %s\n", strerror(r));
709*0Sstevel@tonic-gate 			break;
710*0Sstevel@tonic-gate 
711*0Sstevel@tonic-gate 		default:
712*0Sstevel@tonic-gate 			bad_error("libscf_set_reconfig", r);
713*0Sstevel@tonic-gate 		}
714*0Sstevel@tonic-gate 	}
715*0Sstevel@tonic-gate }
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate static void
718*0Sstevel@tonic-gate startup(int log_args)
719*0Sstevel@tonic-gate {
720*0Sstevel@tonic-gate 	ctid_t configd_ctid;
721*0Sstevel@tonic-gate 	int err;
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate 	/*
724*0Sstevel@tonic-gate 	 * Initialize data structures.
725*0Sstevel@tonic-gate 	 */
726*0Sstevel@tonic-gate 	gu = startd_zalloc(sizeof (graph_update_t));
727*0Sstevel@tonic-gate 	ru = startd_zalloc(sizeof (restarter_update_t));
728*0Sstevel@tonic-gate 
729*0Sstevel@tonic-gate 	(void) pthread_cond_init(&st->st_load_cv, NULL);
730*0Sstevel@tonic-gate 	(void) pthread_cond_init(&st->st_configd_live_cv, NULL);
731*0Sstevel@tonic-gate 	(void) pthread_cond_init(&gu->gu_cv, NULL);
732*0Sstevel@tonic-gate 	(void) pthread_cond_init(&gu->gu_freeze_cv, NULL);
733*0Sstevel@tonic-gate 	(void) pthread_cond_init(&ru->restarter_update_cv, NULL);
734*0Sstevel@tonic-gate 	(void) pthread_mutex_init(&st->st_load_lock, &mutex_attrs);
735*0Sstevel@tonic-gate 	(void) pthread_mutex_init(&st->st_configd_live_lock, &mutex_attrs);
736*0Sstevel@tonic-gate 	(void) pthread_mutex_init(&gu->gu_lock, &mutex_attrs);
737*0Sstevel@tonic-gate 	(void) pthread_mutex_init(&gu->gu_freeze_lock, &mutex_attrs);
738*0Sstevel@tonic-gate 	(void) pthread_mutex_init(&ru->restarter_update_lock, &mutex_attrs);
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate 	configd_ctid = contract_init();
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 	if (configd_ctid != -1)
743*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Existing configd contract %ld; not "
744*0Sstevel@tonic-gate 		    "starting svc.configd\n", configd_ctid);
745*0Sstevel@tonic-gate 
746*0Sstevel@tonic-gate 	(void) startd_thread_create(fork_configd_thread, (void *)configd_ctid);
747*0Sstevel@tonic-gate 
748*0Sstevel@tonic-gate 	/*
749*0Sstevel@tonic-gate 	 * Await, if necessary, configd's initial arrival.
750*0Sstevel@tonic-gate 	 */
751*0Sstevel@tonic-gate 	MUTEX_LOCK(&st->st_configd_live_lock);
752*0Sstevel@tonic-gate 	while (!st->st_configd_lives) {
753*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Awaiting cv signal on "
754*0Sstevel@tonic-gate 		    "configd_live_cv\n");
755*0Sstevel@tonic-gate 		err = pthread_cond_wait(&st->st_configd_live_cv,
756*0Sstevel@tonic-gate 		    &st->st_configd_live_lock);
757*0Sstevel@tonic-gate 		assert(err == 0);
758*0Sstevel@tonic-gate 	}
759*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&st->st_configd_live_lock);
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate 	utmpx_init();
762*0Sstevel@tonic-gate 	wait_init();
763*0Sstevel@tonic-gate 
764*0Sstevel@tonic-gate 	if (read_startd_config(log_args))
765*0Sstevel@tonic-gate 		log_framework(LOG_INFO, "svc.configd unable to provide startd "
766*0Sstevel@tonic-gate 		    "optional settings\n");
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate 	log_init();
769*0Sstevel@tonic-gate 	dict_init();
770*0Sstevel@tonic-gate 	timeout_init();
771*0Sstevel@tonic-gate 	restarter_protocol_init();
772*0Sstevel@tonic-gate 	restarter_init();
773*0Sstevel@tonic-gate 	graph_protocol_init();
774*0Sstevel@tonic-gate 	graph_init();
775*0Sstevel@tonic-gate 
776*0Sstevel@tonic-gate 	init_env();
777*0Sstevel@tonic-gate 
778*0Sstevel@tonic-gate 	set_boot_env();
779*0Sstevel@tonic-gate 	restarter_start();
780*0Sstevel@tonic-gate 	graph_engine_start();
781*0Sstevel@tonic-gate }
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate static void
784*0Sstevel@tonic-gate usage(const char *name)
785*0Sstevel@tonic-gate {
786*0Sstevel@tonic-gate 	uu_warn(gettext("usage: %s [-dnq]\n"), name);
787*0Sstevel@tonic-gate 	exit(UU_EXIT_USAGE);
788*0Sstevel@tonic-gate }
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate static int
791*0Sstevel@tonic-gate daemonize_start(void)
792*0Sstevel@tonic-gate {
793*0Sstevel@tonic-gate 	pid_t pid;
794*0Sstevel@tonic-gate 	int fd;
795*0Sstevel@tonic-gate 
796*0Sstevel@tonic-gate 	if ((pid = fork1()) < 0)
797*0Sstevel@tonic-gate 		return (-1);
798*0Sstevel@tonic-gate 
799*0Sstevel@tonic-gate 	if (pid != 0)
800*0Sstevel@tonic-gate 		exit(0);
801*0Sstevel@tonic-gate 
802*0Sstevel@tonic-gate 	(void) close(0);
803*0Sstevel@tonic-gate 
804*0Sstevel@tonic-gate 	if ((fd = open("/dev/null", O_RDONLY)) == -1) {
805*0Sstevel@tonic-gate 		uu_warn(gettext("can't connect stdin to /dev/null"));
806*0Sstevel@tonic-gate 	} else if (fd != 0) {
807*0Sstevel@tonic-gate 		(void) dup2(fd, 0);
808*0Sstevel@tonic-gate 		startd_close(fd);
809*0Sstevel@tonic-gate 	}
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate 	closefrom(3);
812*0Sstevel@tonic-gate 	(void) dup2(2, 1);
813*0Sstevel@tonic-gate 
814*0Sstevel@tonic-gate 	(void) setsid();
815*0Sstevel@tonic-gate 	(void) chdir("/");
816*0Sstevel@tonic-gate 
817*0Sstevel@tonic-gate 	/* Use default umask that init handed us, but 022 to create files. */
818*0Sstevel@tonic-gate 	dmask = umask(022);
819*0Sstevel@tonic-gate 	fmask = umask(dmask);
820*0Sstevel@tonic-gate 
821*0Sstevel@tonic-gate 	return (0);
822*0Sstevel@tonic-gate }
823*0Sstevel@tonic-gate 
824*0Sstevel@tonic-gate /*ARGSUSED*/
825*0Sstevel@tonic-gate static void
826*0Sstevel@tonic-gate die_handler(int sig, siginfo_t *info, void *data)
827*0Sstevel@tonic-gate {
828*0Sstevel@tonic-gate 	finished = 1;
829*0Sstevel@tonic-gate }
830*0Sstevel@tonic-gate 
831*0Sstevel@tonic-gate int
832*0Sstevel@tonic-gate main(int argc, char *argv[])
833*0Sstevel@tonic-gate {
834*0Sstevel@tonic-gate 	int opt;
835*0Sstevel@tonic-gate 	int daemonize = 1;
836*0Sstevel@tonic-gate 	int log_args = 0;
837*0Sstevel@tonic-gate 	struct sigaction act;
838*0Sstevel@tonic-gate 	sigset_t nullset;
839*0Sstevel@tonic-gate 	struct stat sb;
840*0Sstevel@tonic-gate 
841*0Sstevel@tonic-gate 	(void) uu_setpname(argv[0]);
842*0Sstevel@tonic-gate 
843*0Sstevel@tonic-gate 	st = startd_zalloc(sizeof (startd_state_t));
844*0Sstevel@tonic-gate 
845*0Sstevel@tonic-gate 	(void) pthread_mutexattr_init(&mutex_attrs);
846*0Sstevel@tonic-gate #ifndef	NDEBUG
847*0Sstevel@tonic-gate 	(void) pthread_mutexattr_settype(&mutex_attrs,
848*0Sstevel@tonic-gate 	    PTHREAD_MUTEX_ERRORCHECK);
849*0Sstevel@tonic-gate #endif
850*0Sstevel@tonic-gate 
851*0Sstevel@tonic-gate 	max_scf_name_size = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH);
852*0Sstevel@tonic-gate 	max_scf_value_size = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
853*0Sstevel@tonic-gate 	max_scf_fmri_size = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
854*0Sstevel@tonic-gate 
855*0Sstevel@tonic-gate 	if (max_scf_name_size == -1 || max_scf_value_size == -1 ||
856*0Sstevel@tonic-gate 	    max_scf_value_size == -1)
857*0Sstevel@tonic-gate 		uu_die("Can't determine repository maximum lengths.\n");
858*0Sstevel@tonic-gate 
859*0Sstevel@tonic-gate 	max_scf_name_size++;
860*0Sstevel@tonic-gate 	max_scf_value_size++;
861*0Sstevel@tonic-gate 	max_scf_fmri_size++;
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate 	st->st_log_flags = STARTD_LOG_FILE;
864*0Sstevel@tonic-gate 	st->st_log_level_min = LOG_INFO;
865*0Sstevel@tonic-gate 
866*0Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "dnqrs")) != EOF) {
867*0Sstevel@tonic-gate 		switch (opt) {
868*0Sstevel@tonic-gate 		case 'd':
869*0Sstevel@tonic-gate 			st->st_log_flags =
870*0Sstevel@tonic-gate 			    STARTD_LOG_FILE | STARTD_LOG_TERMINAL;
871*0Sstevel@tonic-gate 			st->st_log_level_min = LOG_DEBUG;
872*0Sstevel@tonic-gate 			log_args = 1;
873*0Sstevel@tonic-gate 			break;
874*0Sstevel@tonic-gate 		case 'n':
875*0Sstevel@tonic-gate 			daemonize = 0;
876*0Sstevel@tonic-gate 			break;
877*0Sstevel@tonic-gate 		case 'q':
878*0Sstevel@tonic-gate 			st->st_log_flags = 0;
879*0Sstevel@tonic-gate 			st->st_log_level_min = LOG_NOTICE;
880*0Sstevel@tonic-gate 			log_args = 1;
881*0Sstevel@tonic-gate 			break;
882*0Sstevel@tonic-gate 		case 'r':			/* reconfiguration boot */
883*0Sstevel@tonic-gate 			opt_reconfig = 1;
884*0Sstevel@tonic-gate 			break;
885*0Sstevel@tonic-gate 		case 's':			/* single-user mode */
886*0Sstevel@tonic-gate 			booting_to_single_user = B_TRUE;
887*0Sstevel@tonic-gate 			break;
888*0Sstevel@tonic-gate 		default:
889*0Sstevel@tonic-gate 			usage(argv[0]);		/* exits */
890*0Sstevel@tonic-gate 		}
891*0Sstevel@tonic-gate 	}
892*0Sstevel@tonic-gate 
893*0Sstevel@tonic-gate 	if (optind != argc)
894*0Sstevel@tonic-gate 		usage(argv[0]);
895*0Sstevel@tonic-gate 
896*0Sstevel@tonic-gate 	if (daemonize)
897*0Sstevel@tonic-gate 		if (daemonize_start() < 0)
898*0Sstevel@tonic-gate 			uu_die("Can't daemonize\n");
899*0Sstevel@tonic-gate 
900*0Sstevel@tonic-gate 	log_init();
901*0Sstevel@tonic-gate 
902*0Sstevel@tonic-gate 	if (stat("/etc/svc/volatile/resetting", &sb) != -1) {
903*0Sstevel@tonic-gate 		log_framework(LOG_NOTICE, "Restarter quiesced.\n");
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate 		for (;;)
906*0Sstevel@tonic-gate 			(void) pause();
907*0Sstevel@tonic-gate 	}
908*0Sstevel@tonic-gate 
909*0Sstevel@tonic-gate 	act.sa_sigaction = &die_handler;
910*0Sstevel@tonic-gate 	(void) sigfillset(&act.sa_mask);
911*0Sstevel@tonic-gate 	act.sa_flags = SA_SIGINFO;
912*0Sstevel@tonic-gate 	(void) sigaction(SIGINT, &act, NULL);
913*0Sstevel@tonic-gate 	(void) sigaction(SIGTERM, &act, NULL);
914*0Sstevel@tonic-gate 
915*0Sstevel@tonic-gate 	startup(log_args);
916*0Sstevel@tonic-gate 
917*0Sstevel@tonic-gate 	(void) sigemptyset(&nullset);
918*0Sstevel@tonic-gate 	while (!finished) {
919*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Main thread paused\n");
920*0Sstevel@tonic-gate 		(void) sigsuspend(&nullset);
921*0Sstevel@tonic-gate 	}
922*0Sstevel@tonic-gate 
923*0Sstevel@tonic-gate 	(void) log_framework(LOG_DEBUG, "Restarter exiting.\n");
924*0Sstevel@tonic-gate 	return (0);
925*0Sstevel@tonic-gate }
926