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