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