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