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