17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5004388ebScasper * Common Development and Distribution License (the "License"). 6004388ebScasper * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 213eae19d9Swesolows 227c478bd9Sstevel@tonic-gate /* 23*5b7f77adStw21770 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * startd.c - the master restarter 317c478bd9Sstevel@tonic-gate * 327c478bd9Sstevel@tonic-gate * svc.startd comprises two halves. The graph engine is based in graph.c and 337c478bd9Sstevel@tonic-gate * maintains the service dependency graph based on the information in the 347c478bd9Sstevel@tonic-gate * repository. For each service it also tracks the current state and the 357c478bd9Sstevel@tonic-gate * restarter responsible for the service. Based on the graph, events from the 367c478bd9Sstevel@tonic-gate * repository (mostly administrative requests from svcadm), and messages from 377c478bd9Sstevel@tonic-gate * the restarters, the graph engine makes decisions about how the services 387c478bd9Sstevel@tonic-gate * should be manipulated and sends commands to the appropriate restarters. 397c478bd9Sstevel@tonic-gate * Communication between the graph engine and the restarters is embodied in 407c478bd9Sstevel@tonic-gate * protocol.c. 417c478bd9Sstevel@tonic-gate * 427c478bd9Sstevel@tonic-gate * The second half of svc.startd is the restarter for services managed by 437c478bd9Sstevel@tonic-gate * svc.startd and is primarily contained in restarter.c. It responds to graph 447c478bd9Sstevel@tonic-gate * engine commands by executing methods, updating the repository, and sending 457c478bd9Sstevel@tonic-gate * feedback (mostly state updates) to the graph engine. 467c478bd9Sstevel@tonic-gate * 477c478bd9Sstevel@tonic-gate * Error handling 487c478bd9Sstevel@tonic-gate * 497c478bd9Sstevel@tonic-gate * In general, when svc.startd runs out of memory it reattempts a few times, 507c478bd9Sstevel@tonic-gate * sleeping inbetween, before giving up and exiting (see startd_alloc_retry()). 517c478bd9Sstevel@tonic-gate * When a repository connection is broken (libscf calls fail with 527c478bd9Sstevel@tonic-gate * SCF_ERROR_CONNECTION_BROKEN, librestart and internal functions return 537c478bd9Sstevel@tonic-gate * ECONNABORTED), svc.startd calls libscf_rebind_handle(), which coordinates 547c478bd9Sstevel@tonic-gate * with the svc.configd-restarting thread, fork_configd_thread(), via 557c478bd9Sstevel@tonic-gate * st->st_configd_live_cv, and rebinds the repository handle. Doing so resets 567c478bd9Sstevel@tonic-gate * all libscf state associated with that handle, so functions which do this 577c478bd9Sstevel@tonic-gate * should communicate the event to their callers (usually by returning 587c478bd9Sstevel@tonic-gate * ECONNRESET) so they may reset their state appropriately. 59*5b7f77adStw21770 * 60*5b7f77adStw21770 * External references 61*5b7f77adStw21770 * 62*5b7f77adStw21770 * svc.configd generates special security audit events for changes to some 63*5b7f77adStw21770 * restarter related properties. See the special_props_list array in 64*5b7f77adStw21770 * usr/src/cmd/svc/configd/rc_node.c for the properties that cause these audit 65*5b7f77adStw21770 * events. If you change the semantics of these propereties within startd, you 66*5b7f77adStw21770 * will probably need to update rc_node.c 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate #include <stdio.h> 70004388ebScasper #include <stdio_ext.h> 717c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> /* uses FILE * without including stdio.h */ 727c478bd9Sstevel@tonic-gate #include <alloca.h> 737c478bd9Sstevel@tonic-gate #include <sys/mount.h> 747c478bd9Sstevel@tonic-gate #include <sys/stat.h> 757c478bd9Sstevel@tonic-gate #include <sys/types.h> 767c478bd9Sstevel@tonic-gate #include <sys/wait.h> 777c478bd9Sstevel@tonic-gate #include <assert.h> 787c478bd9Sstevel@tonic-gate #include <errno.h> 797c478bd9Sstevel@tonic-gate #include <fcntl.h> 807c478bd9Sstevel@tonic-gate #include <ftw.h> 817c478bd9Sstevel@tonic-gate #include <libintl.h> 827c478bd9Sstevel@tonic-gate #include <libscf.h> 837c478bd9Sstevel@tonic-gate #include <libscf_priv.h> 847c478bd9Sstevel@tonic-gate #include <libuutil.h> 857c478bd9Sstevel@tonic-gate #include <locale.h> 867c478bd9Sstevel@tonic-gate #include <poll.h> 877c478bd9Sstevel@tonic-gate #include <pthread.h> 887c478bd9Sstevel@tonic-gate #include <signal.h> 897c478bd9Sstevel@tonic-gate #include <stdarg.h> 907c478bd9Sstevel@tonic-gate #include <stdlib.h> 917c478bd9Sstevel@tonic-gate #include <string.h> 927c478bd9Sstevel@tonic-gate #include <strings.h> 937c478bd9Sstevel@tonic-gate #include <unistd.h> 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate #include "startd.h" 967c478bd9Sstevel@tonic-gate #include "protocol.h" 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate ssize_t max_scf_name_size; 997c478bd9Sstevel@tonic-gate ssize_t max_scf_fmri_size; 1007c478bd9Sstevel@tonic-gate ssize_t max_scf_value_size; 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate mode_t fmask; 1037c478bd9Sstevel@tonic-gate mode_t dmask; 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate graph_update_t *gu; 1067c478bd9Sstevel@tonic-gate restarter_update_t *ru; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate startd_state_t *st; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate boolean_t booting_to_single_user = B_FALSE; 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate const char * const admin_actions[] = { 1137c478bd9Sstevel@tonic-gate SCF_PROPERTY_DEGRADED, 1147c478bd9Sstevel@tonic-gate SCF_PROPERTY_MAINT_OFF, 1157c478bd9Sstevel@tonic-gate SCF_PROPERTY_MAINT_ON, 1167c478bd9Sstevel@tonic-gate SCF_PROPERTY_MAINT_ON_IMMEDIATE, 1177c478bd9Sstevel@tonic-gate SCF_PROPERTY_REFRESH, 1187c478bd9Sstevel@tonic-gate SCF_PROPERTY_RESTART 1197c478bd9Sstevel@tonic-gate }; 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate const int admin_events[NACTIONS] = { 1227c478bd9Sstevel@tonic-gate RESTARTER_EVENT_TYPE_ADMIN_DEGRADED, 1237c478bd9Sstevel@tonic-gate RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF, 1247c478bd9Sstevel@tonic-gate RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON, 1257c478bd9Sstevel@tonic-gate RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE, 1267c478bd9Sstevel@tonic-gate RESTARTER_EVENT_TYPE_ADMIN_REFRESH, 1277c478bd9Sstevel@tonic-gate RESTARTER_EVENT_TYPE_ADMIN_RESTART 1287c478bd9Sstevel@tonic-gate }; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate const char * const instance_state_str[] = { 1317c478bd9Sstevel@tonic-gate "none", 1327c478bd9Sstevel@tonic-gate "uninitialized", 1337c478bd9Sstevel@tonic-gate "maintenance", 1347c478bd9Sstevel@tonic-gate "offline", 1357c478bd9Sstevel@tonic-gate "disabled", 1367c478bd9Sstevel@tonic-gate "online", 1377c478bd9Sstevel@tonic-gate "degraded" 1387c478bd9Sstevel@tonic-gate }; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate static int finished = 0; 1417c478bd9Sstevel@tonic-gate static int opt_reconfig = 0; 1427c478bd9Sstevel@tonic-gate static uint8_t prop_reconfig = 0; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate #define INITIAL_REBIND_ATTEMPTS 5 1457c478bd9Sstevel@tonic-gate #define INITIAL_REBIND_DELAY 3 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate pthread_mutexattr_t mutex_attrs; 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate const char * 1507c478bd9Sstevel@tonic-gate _umem_debug_init(void) 1517c478bd9Sstevel@tonic-gate { 1527c478bd9Sstevel@tonic-gate return ("default,verbose"); /* UMEM_DEBUG setting */ 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate const char * 1567c478bd9Sstevel@tonic-gate _umem_logging_init(void) 1577c478bd9Sstevel@tonic-gate { 1587c478bd9Sstevel@tonic-gate return ("fail,contents"); /* UMEM_LOGGING setting */ 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate /* 1627c478bd9Sstevel@tonic-gate * startd_alloc_retry() 1637c478bd9Sstevel@tonic-gate * Wrapper for allocation functions. Retries with a decaying time 1647c478bd9Sstevel@tonic-gate * value on failure to allocate, and aborts startd if failure is 1657c478bd9Sstevel@tonic-gate * persistent. 1667c478bd9Sstevel@tonic-gate */ 1677c478bd9Sstevel@tonic-gate void * 1687c478bd9Sstevel@tonic-gate startd_alloc_retry(void *f(size_t, int), size_t sz) 1697c478bd9Sstevel@tonic-gate { 1707c478bd9Sstevel@tonic-gate void *p; 1717c478bd9Sstevel@tonic-gate uint_t try, msecs; 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate p = f(sz, UMEM_DEFAULT); 1747c478bd9Sstevel@tonic-gate if (p != NULL || sz == 0) 1757c478bd9Sstevel@tonic-gate return (p); 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate msecs = ALLOC_DELAY; 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate for (try = 0; p == NULL && try < ALLOC_RETRY; ++try) { 1807c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, msecs); 1817c478bd9Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT; 1827c478bd9Sstevel@tonic-gate p = f(sz, UMEM_DEFAULT); 1837c478bd9Sstevel@tonic-gate if (p != NULL) 1847c478bd9Sstevel@tonic-gate return (p); 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate uu_die("Insufficient memory.\n"); 1887c478bd9Sstevel@tonic-gate /* NOTREACHED */ 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate void * 1927c478bd9Sstevel@tonic-gate safe_realloc(void *p, size_t sz) 1937c478bd9Sstevel@tonic-gate { 1947c478bd9Sstevel@tonic-gate uint_t try, msecs; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate p = realloc(p, sz); 1977c478bd9Sstevel@tonic-gate if (p != NULL || sz == 0) 1987c478bd9Sstevel@tonic-gate return (p); 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate msecs = ALLOC_DELAY; 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate for (try = 0; errno == EAGAIN && try < ALLOC_RETRY; ++try) { 2037c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, msecs); 2047c478bd9Sstevel@tonic-gate p = realloc(p, sz); 2057c478bd9Sstevel@tonic-gate if (p != NULL) 2067c478bd9Sstevel@tonic-gate return (p); 2077c478bd9Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT; 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate uu_die("Insufficient memory.\n"); 2117c478bd9Sstevel@tonic-gate /* NOTREACHED */ 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate char * 2157c478bd9Sstevel@tonic-gate safe_strdup(const char *s) 2167c478bd9Sstevel@tonic-gate { 2177c478bd9Sstevel@tonic-gate uint_t try, msecs; 2187c478bd9Sstevel@tonic-gate char *d; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate d = strdup(s); 2217c478bd9Sstevel@tonic-gate if (d != NULL) 2227c478bd9Sstevel@tonic-gate return (d); 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate msecs = ALLOC_DELAY; 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate for (try = 0; 2277c478bd9Sstevel@tonic-gate (errno == EAGAIN || errno == ENOMEM) && try < ALLOC_RETRY; 2287c478bd9Sstevel@tonic-gate ++try) { 2297c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, msecs); 2307c478bd9Sstevel@tonic-gate d = strdup(s); 2317c478bd9Sstevel@tonic-gate if (d != NULL) 2327c478bd9Sstevel@tonic-gate return (d); 2337c478bd9Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT; 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate uu_die("Insufficient memory.\n"); 2377c478bd9Sstevel@tonic-gate /* NOTREACHED */ 2387c478bd9Sstevel@tonic-gate } 2397c478bd9Sstevel@tonic-gate 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate void 2427c478bd9Sstevel@tonic-gate startd_free(void *p, size_t sz) 2437c478bd9Sstevel@tonic-gate { 2447c478bd9Sstevel@tonic-gate umem_free(p, sz); 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate /* 2487c478bd9Sstevel@tonic-gate * Creates a uu_list_pool_t with the same retry policy as startd_alloc(). 2497c478bd9Sstevel@tonic-gate * Only returns NULL for UU_ERROR_UNKNOWN_FLAG and UU_ERROR_NOT_SUPPORTED. 2507c478bd9Sstevel@tonic-gate */ 2517c478bd9Sstevel@tonic-gate uu_list_pool_t * 2527c478bd9Sstevel@tonic-gate startd_list_pool_create(const char *name, size_t e, size_t o, 2537c478bd9Sstevel@tonic-gate uu_compare_fn_t *f, uint32_t flags) 2547c478bd9Sstevel@tonic-gate { 2557c478bd9Sstevel@tonic-gate uu_list_pool_t *pool; 2567c478bd9Sstevel@tonic-gate uint_t try, msecs; 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate pool = uu_list_pool_create(name, e, o, f, flags); 2597c478bd9Sstevel@tonic-gate if (pool != NULL) 2607c478bd9Sstevel@tonic-gate return (pool); 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate msecs = ALLOC_DELAY; 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate for (try = 0; uu_error() == UU_ERROR_NO_MEMORY && try < ALLOC_RETRY; 2657c478bd9Sstevel@tonic-gate ++try) { 2667c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, msecs); 2677c478bd9Sstevel@tonic-gate pool = uu_list_pool_create(name, e, o, f, flags); 2687c478bd9Sstevel@tonic-gate if (pool != NULL) 2697c478bd9Sstevel@tonic-gate return (pool); 2707c478bd9Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT; 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate if (try < ALLOC_RETRY) 2747c478bd9Sstevel@tonic-gate return (NULL); 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate uu_die("Insufficient memory.\n"); 2777c478bd9Sstevel@tonic-gate /* NOTREACHED */ 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate /* 2817c478bd9Sstevel@tonic-gate * Creates a uu_list_t with the same retry policy as startd_alloc(). Only 2827c478bd9Sstevel@tonic-gate * returns NULL for UU_ERROR_UNKNOWN_FLAG and UU_ERROR_NOT_SUPPORTED. 2837c478bd9Sstevel@tonic-gate */ 2847c478bd9Sstevel@tonic-gate uu_list_t * 2857c478bd9Sstevel@tonic-gate startd_list_create(uu_list_pool_t *pool, void *parent, uint32_t flags) 2867c478bd9Sstevel@tonic-gate { 2877c478bd9Sstevel@tonic-gate uu_list_t *list; 2887c478bd9Sstevel@tonic-gate uint_t try, msecs; 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate list = uu_list_create(pool, parent, flags); 2917c478bd9Sstevel@tonic-gate if (list != NULL) 2927c478bd9Sstevel@tonic-gate return (list); 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate msecs = ALLOC_DELAY; 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate for (try = 0; uu_error() == UU_ERROR_NO_MEMORY && try < ALLOC_RETRY; 2977c478bd9Sstevel@tonic-gate ++try) { 2987c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, msecs); 2997c478bd9Sstevel@tonic-gate list = uu_list_create(pool, parent, flags); 3007c478bd9Sstevel@tonic-gate if (list != NULL) 3017c478bd9Sstevel@tonic-gate return (list); 3027c478bd9Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT; 3037c478bd9Sstevel@tonic-gate } 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate if (try < ALLOC_RETRY) 3067c478bd9Sstevel@tonic-gate return (NULL); 3077c478bd9Sstevel@tonic-gate 3087c478bd9Sstevel@tonic-gate uu_die("Insufficient memory.\n"); 3097c478bd9Sstevel@tonic-gate /* NOTREACHED */ 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate pthread_t 3137c478bd9Sstevel@tonic-gate startd_thread_create(void *(*func)(void *), void *ptr) 3147c478bd9Sstevel@tonic-gate { 3157c478bd9Sstevel@tonic-gate int err; 3167c478bd9Sstevel@tonic-gate pthread_t tid; 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate err = pthread_create(&tid, NULL, func, ptr); 3197c478bd9Sstevel@tonic-gate if (err != 0) { 3207c478bd9Sstevel@tonic-gate assert(err == EAGAIN); 3217c478bd9Sstevel@tonic-gate uu_die("Could not create thread.\n"); 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate err = pthread_detach(tid); 3257c478bd9Sstevel@tonic-gate assert(err == 0); 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate return (tid); 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate static int 33299b44c3bSlianep read_startd_config(void) 3337c478bd9Sstevel@tonic-gate { 3347c478bd9Sstevel@tonic-gate scf_handle_t *hndl; 3357c478bd9Sstevel@tonic-gate scf_instance_t *inst; 3367c478bd9Sstevel@tonic-gate scf_propertygroup_t *pg; 3377c478bd9Sstevel@tonic-gate scf_property_t *prop; 3387c478bd9Sstevel@tonic-gate scf_value_t *val; 3397c478bd9Sstevel@tonic-gate scf_iter_t *iter, *piter; 3407c478bd9Sstevel@tonic-gate instance_data_t idata; 3417c478bd9Sstevel@tonic-gate char *buf, *vbuf; 3427c478bd9Sstevel@tonic-gate char *startd_options_fmri = uu_msprintf("%s/:properties/options", 3437c478bd9Sstevel@tonic-gate SCF_SERVICE_STARTD); 3447c478bd9Sstevel@tonic-gate char *startd_reconfigure_fmri = uu_msprintf( 3457c478bd9Sstevel@tonic-gate "%s/:properties/system/reconfigure", SCF_SERVICE_STARTD); 3467c478bd9Sstevel@tonic-gate char *env_opts, *lasts, *cp; 3477c478bd9Sstevel@tonic-gate int bind_fails = 0; 3487c478bd9Sstevel@tonic-gate int ret = 0, r; 3497c478bd9Sstevel@tonic-gate uint_t count = 0, msecs = ALLOC_DELAY; 3507c478bd9Sstevel@tonic-gate size_t sz; 3517c478bd9Sstevel@tonic-gate ctid_t ctid; 3527c478bd9Sstevel@tonic-gate uint64_t uint64; 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate buf = startd_alloc(max_scf_fmri_size); 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate if (startd_options_fmri == NULL || startd_reconfigure_fmri == NULL) 3577c478bd9Sstevel@tonic-gate uu_die("Allocation failure\n"); 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate st->st_log_prefix = LOG_PREFIX_EARLY; 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate if ((st->st_log_file = getenv("STARTD_DEFAULT_LOG")) == NULL) { 3627c478bd9Sstevel@tonic-gate st->st_log_file = startd_alloc(strlen(STARTD_DEFAULT_LOG) + 1); 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate (void) strcpy(st->st_log_file, STARTD_DEFAULT_LOG); 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate st->st_door_path = getenv("STARTD_ALT_DOOR"); 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate /* 3707c478bd9Sstevel@tonic-gate * Read "options" property group. 3717c478bd9Sstevel@tonic-gate */ 3727c478bd9Sstevel@tonic-gate for (hndl = libscf_handle_create_bound(SCF_VERSION); hndl == NULL; 3737c478bd9Sstevel@tonic-gate hndl = libscf_handle_create_bound(SCF_VERSION), bind_fails++) { 3747c478bd9Sstevel@tonic-gate (void) sleep(INITIAL_REBIND_DELAY); 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate if (bind_fails > INITIAL_REBIND_ATTEMPTS) { 3777c478bd9Sstevel@tonic-gate /* 3787c478bd9Sstevel@tonic-gate * In the case that we can't bind to the repository 3797c478bd9Sstevel@tonic-gate * (which should have been started), we need to allow 3807c478bd9Sstevel@tonic-gate * the user into maintenance mode to determine what's 3817c478bd9Sstevel@tonic-gate * failed. 3827c478bd9Sstevel@tonic-gate */ 3837c478bd9Sstevel@tonic-gate log_framework(LOG_INFO, "Couldn't fetch " 3847c478bd9Sstevel@tonic-gate "default settings: %s\n", 3857c478bd9Sstevel@tonic-gate scf_strerror(scf_error())); 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate ret = -1; 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate goto noscfout; 3907c478bd9Sstevel@tonic-gate } 3917c478bd9Sstevel@tonic-gate } 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate idata.i_fmri = SCF_SERVICE_STARTD; 3947c478bd9Sstevel@tonic-gate idata.i_state = RESTARTER_STATE_NONE; 3957c478bd9Sstevel@tonic-gate idata.i_next_state = RESTARTER_STATE_NONE; 3967c478bd9Sstevel@tonic-gate timestamp: 3977c478bd9Sstevel@tonic-gate switch (r = _restarter_commit_states(hndl, &idata, 3987c478bd9Sstevel@tonic-gate RESTARTER_STATE_ONLINE, RESTARTER_STATE_NONE, NULL)) { 3997c478bd9Sstevel@tonic-gate case 0: 4007c478bd9Sstevel@tonic-gate break; 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate case ENOMEM: 4037c478bd9Sstevel@tonic-gate ++count; 4047c478bd9Sstevel@tonic-gate if (count < ALLOC_RETRY) { 4057c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, msecs); 4067c478bd9Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT; 4077c478bd9Sstevel@tonic-gate goto timestamp; 4087c478bd9Sstevel@tonic-gate } 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate uu_die("Insufficient memory.\n"); 4117c478bd9Sstevel@tonic-gate /* NOTREACHED */ 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate case ECONNABORTED: 4147c478bd9Sstevel@tonic-gate libscf_handle_rebind(hndl); 4157c478bd9Sstevel@tonic-gate goto timestamp; 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate case ENOENT: 4187c478bd9Sstevel@tonic-gate case EPERM: 4197c478bd9Sstevel@tonic-gate case EACCES: 4207c478bd9Sstevel@tonic-gate case EROFS: 4217c478bd9Sstevel@tonic-gate log_error(LOG_INFO, "Could set state of %s: %s.\n", 4227c478bd9Sstevel@tonic-gate idata.i_fmri, strerror(r)); 4237c478bd9Sstevel@tonic-gate break; 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate case EINVAL: 4267c478bd9Sstevel@tonic-gate default: 4277c478bd9Sstevel@tonic-gate bad_error("_restarter_commit_states", r); 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate pg = safe_scf_pg_create(hndl); 4317c478bd9Sstevel@tonic-gate prop = safe_scf_property_create(hndl); 4327c478bd9Sstevel@tonic-gate val = safe_scf_value_create(hndl); 4337c478bd9Sstevel@tonic-gate inst = safe_scf_instance_create(hndl); 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate /* set startd's restarter properties */ 4367c478bd9Sstevel@tonic-gate if (scf_handle_decode_fmri(hndl, SCF_SERVICE_STARTD, NULL, NULL, inst, 4377c478bd9Sstevel@tonic-gate NULL, NULL, SCF_DECODE_FMRI_EXACT) == 0) { 4387c478bd9Sstevel@tonic-gate (void) libscf_write_start_pid(inst, getpid()); 4397c478bd9Sstevel@tonic-gate ctid = proc_get_ctid(); 4407c478bd9Sstevel@tonic-gate if (ctid != -1) { 4417c478bd9Sstevel@tonic-gate uint64 = (uint64_t)ctid; 4427c478bd9Sstevel@tonic-gate (void) libscf_inst_set_count_prop(inst, 4437c478bd9Sstevel@tonic-gate SCF_PG_RESTARTER, SCF_PG_RESTARTER_TYPE, 4447c478bd9Sstevel@tonic-gate SCF_PG_RESTARTER_FLAGS, SCF_PROPERTY_CONTRACT, 4457c478bd9Sstevel@tonic-gate uint64); 4467c478bd9Sstevel@tonic-gate } 4477c478bd9Sstevel@tonic-gate (void) libscf_note_method_log(inst, LOG_PREFIX_EARLY, 4487c478bd9Sstevel@tonic-gate STARTD_DEFAULT_LOG); 4497c478bd9Sstevel@tonic-gate (void) libscf_note_method_log(inst, LOG_PREFIX_NORMAL, 4507c478bd9Sstevel@tonic-gate STARTD_DEFAULT_LOG); 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate 4537c478bd9Sstevel@tonic-gate /* Read reconfigure property for recovery. */ 4547c478bd9Sstevel@tonic-gate if (scf_handle_decode_fmri(hndl, startd_reconfigure_fmri, NULL, NULL, 4557c478bd9Sstevel@tonic-gate NULL, NULL, prop, NULL) != -1 && 4567c478bd9Sstevel@tonic-gate scf_property_get_value(prop, val) == 0) 4577c478bd9Sstevel@tonic-gate (void) scf_value_get_boolean(val, &prop_reconfig); 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate if (scf_handle_decode_fmri(hndl, startd_options_fmri, NULL, NULL, NULL, 4607c478bd9Sstevel@tonic-gate pg, NULL, SCF_DECODE_FMRI_TRUNCATE) == -1) { 4617c478bd9Sstevel@tonic-gate /* 4627c478bd9Sstevel@tonic-gate * No configuration options defined. 4637c478bd9Sstevel@tonic-gate */ 4647c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND) 4657c478bd9Sstevel@tonic-gate uu_warn("Couldn't read configuration from 'options' " 4667c478bd9Sstevel@tonic-gate "group: %s\n", scf_strerror(scf_error())); 4677c478bd9Sstevel@tonic-gate goto scfout; 4687c478bd9Sstevel@tonic-gate } 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate /* 4717c478bd9Sstevel@tonic-gate * If there is no "options" group defined, then our defaults are fine. 4727c478bd9Sstevel@tonic-gate */ 4737c478bd9Sstevel@tonic-gate if (scf_pg_get_name(pg, NULL, 0) < 0) 4747c478bd9Sstevel@tonic-gate goto scfout; 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate /* Iterate through. */ 4777c478bd9Sstevel@tonic-gate iter = safe_scf_iter_create(hndl); 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate (void) scf_iter_pg_properties(iter, pg); 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate piter = safe_scf_iter_create(hndl); 4827c478bd9Sstevel@tonic-gate vbuf = startd_alloc(max_scf_value_size); 4837c478bd9Sstevel@tonic-gate 4847c478bd9Sstevel@tonic-gate while ((scf_iter_next_property(iter, prop) == 1)) { 4857c478bd9Sstevel@tonic-gate scf_type_t ty; 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate if (scf_property_get_name(prop, buf, max_scf_fmri_size) < 0) 4887c478bd9Sstevel@tonic-gate continue; 4897c478bd9Sstevel@tonic-gate 4907c478bd9Sstevel@tonic-gate if (strcmp(buf, "logging") != 0 && 4917c478bd9Sstevel@tonic-gate strcmp(buf, "boot_messages") != 0) 4927c478bd9Sstevel@tonic-gate continue; 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate if (scf_property_type(prop, &ty) != 0) { 4957c478bd9Sstevel@tonic-gate switch (scf_error()) { 4967c478bd9Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 4977c478bd9Sstevel@tonic-gate default: 4987c478bd9Sstevel@tonic-gate libscf_handle_rebind(hndl); 4997c478bd9Sstevel@tonic-gate continue; 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED: 5027c478bd9Sstevel@tonic-gate continue; 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 5057c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 5067c478bd9Sstevel@tonic-gate bad_error("scf_property_type", scf_error()); 5077c478bd9Sstevel@tonic-gate } 5087c478bd9Sstevel@tonic-gate } 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate if (ty != SCF_TYPE_ASTRING) { 5117c478bd9Sstevel@tonic-gate uu_warn("property \"options/%s\" is not of type " 5127c478bd9Sstevel@tonic-gate "astring; ignored.\n", buf); 5137c478bd9Sstevel@tonic-gate continue; 5147c478bd9Sstevel@tonic-gate } 5157c478bd9Sstevel@tonic-gate 5167c478bd9Sstevel@tonic-gate if (scf_property_get_value(prop, val) != 0) { 5177c478bd9Sstevel@tonic-gate switch (scf_error()) { 5187c478bd9Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 5197c478bd9Sstevel@tonic-gate default: 5207c478bd9Sstevel@tonic-gate return (ECONNABORTED); 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED: 5237c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 5247c478bd9Sstevel@tonic-gate return (0); 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED: 5277c478bd9Sstevel@tonic-gate uu_warn("property \"options/%s\" has multiple " 5287c478bd9Sstevel@tonic-gate "values; ignored.\n", buf); 5297c478bd9Sstevel@tonic-gate continue; 5307c478bd9Sstevel@tonic-gate 5313eae19d9Swesolows case SCF_ERROR_PERMISSION_DENIED: 5323eae19d9Swesolows uu_warn("property \"options/%s\" cannot be " 5333eae19d9Swesolows "read because startd has insufficient " 5343eae19d9Swesolows "permission; ignored.\n", buf); 5353eae19d9Swesolows continue; 5363eae19d9Swesolows 5377c478bd9Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 5387c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 5397c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 5407c478bd9Sstevel@tonic-gate bad_error("scf_property_get_value", 5417c478bd9Sstevel@tonic-gate scf_error()); 5427c478bd9Sstevel@tonic-gate } 5437c478bd9Sstevel@tonic-gate } 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate if (scf_value_get_astring(val, vbuf, max_scf_value_size) < 0) 5467c478bd9Sstevel@tonic-gate bad_error("scf_value_get_astring", scf_error()); 5477c478bd9Sstevel@tonic-gate 54899b44c3bSlianep if (strcmp("logging", buf) == 0) { 5497c478bd9Sstevel@tonic-gate if (strcmp("verbose", vbuf) == 0) { 5507c478bd9Sstevel@tonic-gate st->st_boot_flags = STARTD_BOOT_VERBOSE; 5517c478bd9Sstevel@tonic-gate st->st_log_level_min = LOG_INFO; 5527c478bd9Sstevel@tonic-gate } else if (strcmp("debug", vbuf) == 0) { 5537c478bd9Sstevel@tonic-gate st->st_boot_flags = STARTD_BOOT_VERBOSE; 5547c478bd9Sstevel@tonic-gate st->st_log_level_min = LOG_DEBUG; 5557c478bd9Sstevel@tonic-gate } else if (strcmp("quiet", vbuf) == 0) { 5567c478bd9Sstevel@tonic-gate st->st_log_level_min = LOG_NOTICE; 5577c478bd9Sstevel@tonic-gate } else { 5587c478bd9Sstevel@tonic-gate uu_warn("unknown options/logging " 5597c478bd9Sstevel@tonic-gate "value '%s' ignored\n", vbuf); 5607c478bd9Sstevel@tonic-gate } 5617c478bd9Sstevel@tonic-gate 5627c478bd9Sstevel@tonic-gate } else if (strcmp("boot_messages", buf) == 0) { 5637c478bd9Sstevel@tonic-gate if (strcmp("quiet", vbuf) == 0) { 5647c478bd9Sstevel@tonic-gate st->st_boot_flags = STARTD_BOOT_QUIET; 5657c478bd9Sstevel@tonic-gate } else if (strcmp("verbose", vbuf) == 0) { 5667c478bd9Sstevel@tonic-gate st->st_boot_flags = STARTD_BOOT_VERBOSE; 5677c478bd9Sstevel@tonic-gate } else { 5687c478bd9Sstevel@tonic-gate log_framework(LOG_NOTICE, "unknown " 5697c478bd9Sstevel@tonic-gate "options/boot_messages value '%s' " 5707c478bd9Sstevel@tonic-gate "ignored\n", vbuf); 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate } 5747c478bd9Sstevel@tonic-gate } 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate startd_free(vbuf, max_scf_value_size); 5777c478bd9Sstevel@tonic-gate scf_iter_destroy(piter); 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate scf_iter_destroy(iter); 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate scfout: 5827c478bd9Sstevel@tonic-gate scf_value_destroy(val); 5837c478bd9Sstevel@tonic-gate scf_pg_destroy(pg); 5847c478bd9Sstevel@tonic-gate scf_property_destroy(prop); 5857c478bd9Sstevel@tonic-gate scf_instance_destroy(inst); 5867c478bd9Sstevel@tonic-gate (void) scf_handle_unbind(hndl); 5877c478bd9Sstevel@tonic-gate scf_handle_destroy(hndl); 5887c478bd9Sstevel@tonic-gate 5897c478bd9Sstevel@tonic-gate noscfout: 5907c478bd9Sstevel@tonic-gate startd_free(buf, max_scf_fmri_size); 5917c478bd9Sstevel@tonic-gate uu_free(startd_options_fmri); 5927c478bd9Sstevel@tonic-gate uu_free(startd_reconfigure_fmri); 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate if (booting_to_single_user) { 5957c478bd9Sstevel@tonic-gate st->st_subgraph = startd_alloc(max_scf_fmri_size); 5967c478bd9Sstevel@tonic-gate sz = strlcpy(st->st_subgraph, "milestone/single-user:default", 5977c478bd9Sstevel@tonic-gate max_scf_fmri_size); 5987c478bd9Sstevel@tonic-gate assert(sz < max_scf_fmri_size); 5997c478bd9Sstevel@tonic-gate } 6007c478bd9Sstevel@tonic-gate 6017c478bd9Sstevel@tonic-gate /* 6027c478bd9Sstevel@tonic-gate * Options passed in as boot arguments override repository defaults. 6037c478bd9Sstevel@tonic-gate */ 6047c478bd9Sstevel@tonic-gate env_opts = getenv("SMF_OPTIONS"); 6057c478bd9Sstevel@tonic-gate if (env_opts == NULL) 6067c478bd9Sstevel@tonic-gate return (ret); 6077c478bd9Sstevel@tonic-gate 6087b966b00Sdstaff for (cp = strtok_r(env_opts, ",", &lasts); cp != NULL; 6097b966b00Sdstaff cp = strtok_r(NULL, ",", &lasts)) { 6107c478bd9Sstevel@tonic-gate if (strcmp(cp, "debug") == 0) { 6117c478bd9Sstevel@tonic-gate st->st_boot_flags = STARTD_BOOT_VERBOSE; 6127c478bd9Sstevel@tonic-gate st->st_log_level_min = LOG_DEBUG; 61399b44c3bSlianep 61499b44c3bSlianep /* -m debug should send messages to console */ 61599b44c3bSlianep st->st_log_flags = 61699b44c3bSlianep st->st_log_flags | STARTD_LOG_TERMINAL; 6177c478bd9Sstevel@tonic-gate } else if (strcmp(cp, "verbose") == 0) { 6187c478bd9Sstevel@tonic-gate st->st_boot_flags = STARTD_BOOT_VERBOSE; 6197c478bd9Sstevel@tonic-gate st->st_log_level_min = LOG_INFO; 6207c478bd9Sstevel@tonic-gate } else if (strcmp(cp, "seed") == 0) { 6217c478bd9Sstevel@tonic-gate uu_warn("SMF option \"%s\" unimplemented.\n", cp); 6227c478bd9Sstevel@tonic-gate } else if (strcmp(cp, "quiet") == 0) { 6237c478bd9Sstevel@tonic-gate st->st_log_level_min = LOG_NOTICE; 6247c478bd9Sstevel@tonic-gate } else if (strncmp(cp, "milestone=", 6257c478bd9Sstevel@tonic-gate sizeof ("milestone=") - 1) == 0) { 6267c478bd9Sstevel@tonic-gate char *mp = cp + sizeof ("milestone=") - 1; 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate if (booting_to_single_user) 6297c478bd9Sstevel@tonic-gate continue; 6307c478bd9Sstevel@tonic-gate 6317c478bd9Sstevel@tonic-gate if (st->st_subgraph == NULL) { 6327c478bd9Sstevel@tonic-gate st->st_subgraph = 6337c478bd9Sstevel@tonic-gate startd_alloc(max_scf_fmri_size); 6347c478bd9Sstevel@tonic-gate st->st_subgraph[0] = '\0'; 6357c478bd9Sstevel@tonic-gate } 6367c478bd9Sstevel@tonic-gate 6377c478bd9Sstevel@tonic-gate if (mp[0] == '\0' || strcmp(mp, "all") == 0) { 6387c478bd9Sstevel@tonic-gate (void) strcpy(st->st_subgraph, "all"); 6397c478bd9Sstevel@tonic-gate } else if (strcmp(mp, "su") == 0 || 6407c478bd9Sstevel@tonic-gate strcmp(mp, "single-user") == 0) { 6417c478bd9Sstevel@tonic-gate (void) strcpy(st->st_subgraph, 6427c478bd9Sstevel@tonic-gate "milestone/single-user:default"); 6437c478bd9Sstevel@tonic-gate } else if (strcmp(mp, "mu") == 0 || 6447c478bd9Sstevel@tonic-gate strcmp(mp, "multi-user") == 0) { 6457c478bd9Sstevel@tonic-gate (void) strcpy(st->st_subgraph, 6467c478bd9Sstevel@tonic-gate "milestone/multi-user:default"); 6477c478bd9Sstevel@tonic-gate } else if (strcmp(mp, "mus") == 0 || 6487c478bd9Sstevel@tonic-gate strcmp(mp, "multi-user-server") == 0) { 6497c478bd9Sstevel@tonic-gate (void) strcpy(st->st_subgraph, 6507c478bd9Sstevel@tonic-gate "milestone/multi-user-server:default"); 6517c478bd9Sstevel@tonic-gate } else if (strcmp(mp, "none") == 0) { 6527c478bd9Sstevel@tonic-gate (void) strcpy(st->st_subgraph, "none"); 6537c478bd9Sstevel@tonic-gate } else { 6547c478bd9Sstevel@tonic-gate log_framework(LOG_NOTICE, 6557c478bd9Sstevel@tonic-gate "invalid milestone option value " 6567c478bd9Sstevel@tonic-gate "'%s' ignored\n", mp); 6577c478bd9Sstevel@tonic-gate } 6587c478bd9Sstevel@tonic-gate } else { 6597c478bd9Sstevel@tonic-gate uu_warn("Unknown SMF option \"%s\".\n", cp); 6607c478bd9Sstevel@tonic-gate } 6617c478bd9Sstevel@tonic-gate } 6627c478bd9Sstevel@tonic-gate 6637c478bd9Sstevel@tonic-gate return (ret); 6647c478bd9Sstevel@tonic-gate } 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate /* 6677c478bd9Sstevel@tonic-gate * void set_boot_env() 6687c478bd9Sstevel@tonic-gate * 6697c478bd9Sstevel@tonic-gate * If -r was passed or /reconfigure exists, this is a reconfig 6707c478bd9Sstevel@tonic-gate * reboot. We need to make sure that this information is given 6717c478bd9Sstevel@tonic-gate * to the appropriate services the first time they're started 6727c478bd9Sstevel@tonic-gate * by setting the system/reconfigure repository property, 6737c478bd9Sstevel@tonic-gate * as well as pass the _INIT_RECONFIG variable on to the rcS 6747c478bd9Sstevel@tonic-gate * start method so that legacy services can continue to use it. 6757c478bd9Sstevel@tonic-gate * 6767c478bd9Sstevel@tonic-gate * This function must never be called before contract_init(), as 6777c478bd9Sstevel@tonic-gate * it sets st_initial. get_startd_config() sets prop_reconfig from 6787c478bd9Sstevel@tonic-gate * pre-existing repository state. 6797c478bd9Sstevel@tonic-gate */ 6807c478bd9Sstevel@tonic-gate static void 6817c478bd9Sstevel@tonic-gate set_boot_env() 6827c478bd9Sstevel@tonic-gate { 6837c478bd9Sstevel@tonic-gate struct stat sb; 6847c478bd9Sstevel@tonic-gate int r; 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate /* 6877c478bd9Sstevel@tonic-gate * Check if property still is set -- indicates we didn't get 6887c478bd9Sstevel@tonic-gate * far enough previously to unset it. Otherwise, if this isn't 6897c478bd9Sstevel@tonic-gate * the first startup, don't re-process /reconfigure or the 6907c478bd9Sstevel@tonic-gate * boot flag. 6917c478bd9Sstevel@tonic-gate */ 6927c478bd9Sstevel@tonic-gate if (prop_reconfig != 1 && st->st_initial != 1) 6937c478bd9Sstevel@tonic-gate return; 6947c478bd9Sstevel@tonic-gate 6957c478bd9Sstevel@tonic-gate /* If /reconfigure exists, also set opt_reconfig. */ 6967c478bd9Sstevel@tonic-gate if (stat("/reconfigure", &sb) != -1) 6977c478bd9Sstevel@tonic-gate opt_reconfig = 1; 6987c478bd9Sstevel@tonic-gate 6997c478bd9Sstevel@tonic-gate /* Nothing to do. Just return. */ 7007c478bd9Sstevel@tonic-gate if (opt_reconfig == 0 && prop_reconfig == 0) 7017c478bd9Sstevel@tonic-gate return; 7027c478bd9Sstevel@tonic-gate 7037c478bd9Sstevel@tonic-gate /* 7047c478bd9Sstevel@tonic-gate * Set startd's reconfigure property. This property is 7057c478bd9Sstevel@tonic-gate * then cleared by successful completion of the single-user 7067c478bd9Sstevel@tonic-gate * milestone. 7077c478bd9Sstevel@tonic-gate */ 7087c478bd9Sstevel@tonic-gate if (prop_reconfig != 1) { 7097c478bd9Sstevel@tonic-gate r = libscf_set_reconfig(1); 7107c478bd9Sstevel@tonic-gate switch (r) { 7117c478bd9Sstevel@tonic-gate case 0: 7127c478bd9Sstevel@tonic-gate break; 7137c478bd9Sstevel@tonic-gate 7147c478bd9Sstevel@tonic-gate case ENOENT: 7157c478bd9Sstevel@tonic-gate case EPERM: 7167c478bd9Sstevel@tonic-gate case EACCES: 7177c478bd9Sstevel@tonic-gate case EROFS: 7187c478bd9Sstevel@tonic-gate log_error(LOG_WARNING, "Could not set reconfiguration " 7197c478bd9Sstevel@tonic-gate "property: %s\n", strerror(r)); 7207c478bd9Sstevel@tonic-gate break; 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate default: 7237c478bd9Sstevel@tonic-gate bad_error("libscf_set_reconfig", r); 7247c478bd9Sstevel@tonic-gate } 7257c478bd9Sstevel@tonic-gate } 7267c478bd9Sstevel@tonic-gate } 7277c478bd9Sstevel@tonic-gate 7287c478bd9Sstevel@tonic-gate static void 72999b44c3bSlianep startup(void) 7307c478bd9Sstevel@tonic-gate { 7317c478bd9Sstevel@tonic-gate ctid_t configd_ctid; 7327c478bd9Sstevel@tonic-gate int err; 7337c478bd9Sstevel@tonic-gate 7347c478bd9Sstevel@tonic-gate /* 7357c478bd9Sstevel@tonic-gate * Initialize data structures. 7367c478bd9Sstevel@tonic-gate */ 7377c478bd9Sstevel@tonic-gate gu = startd_zalloc(sizeof (graph_update_t)); 7387c478bd9Sstevel@tonic-gate ru = startd_zalloc(sizeof (restarter_update_t)); 7397c478bd9Sstevel@tonic-gate 7407c478bd9Sstevel@tonic-gate (void) pthread_cond_init(&st->st_load_cv, NULL); 7417c478bd9Sstevel@tonic-gate (void) pthread_cond_init(&st->st_configd_live_cv, NULL); 7427c478bd9Sstevel@tonic-gate (void) pthread_cond_init(&gu->gu_cv, NULL); 7437c478bd9Sstevel@tonic-gate (void) pthread_cond_init(&gu->gu_freeze_cv, NULL); 7447c478bd9Sstevel@tonic-gate (void) pthread_cond_init(&ru->restarter_update_cv, NULL); 7457c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&st->st_load_lock, &mutex_attrs); 7467c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&st->st_configd_live_lock, &mutex_attrs); 7477c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&gu->gu_lock, &mutex_attrs); 7487c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&gu->gu_freeze_lock, &mutex_attrs); 7497c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&ru->restarter_update_lock, &mutex_attrs); 7507c478bd9Sstevel@tonic-gate 7517c478bd9Sstevel@tonic-gate configd_ctid = contract_init(); 7527c478bd9Sstevel@tonic-gate 7537c478bd9Sstevel@tonic-gate if (configd_ctid != -1) 7547c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "Existing configd contract %ld; not " 7557c478bd9Sstevel@tonic-gate "starting svc.configd\n", configd_ctid); 7567c478bd9Sstevel@tonic-gate 7577c478bd9Sstevel@tonic-gate (void) startd_thread_create(fork_configd_thread, (void *)configd_ctid); 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate /* 7607c478bd9Sstevel@tonic-gate * Await, if necessary, configd's initial arrival. 7617c478bd9Sstevel@tonic-gate */ 7627c478bd9Sstevel@tonic-gate MUTEX_LOCK(&st->st_configd_live_lock); 7637c478bd9Sstevel@tonic-gate while (!st->st_configd_lives) { 7647c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "Awaiting cv signal on " 7657c478bd9Sstevel@tonic-gate "configd_live_cv\n"); 7667c478bd9Sstevel@tonic-gate err = pthread_cond_wait(&st->st_configd_live_cv, 7677c478bd9Sstevel@tonic-gate &st->st_configd_live_lock); 7687c478bd9Sstevel@tonic-gate assert(err == 0); 7697c478bd9Sstevel@tonic-gate } 7707c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&st->st_configd_live_lock); 7717c478bd9Sstevel@tonic-gate 7727c478bd9Sstevel@tonic-gate utmpx_init(); 7737c478bd9Sstevel@tonic-gate wait_init(); 7747c478bd9Sstevel@tonic-gate 77599b44c3bSlianep if (read_startd_config()) 7767c478bd9Sstevel@tonic-gate log_framework(LOG_INFO, "svc.configd unable to provide startd " 7777c478bd9Sstevel@tonic-gate "optional settings\n"); 7787c478bd9Sstevel@tonic-gate 7797c478bd9Sstevel@tonic-gate log_init(); 7807c478bd9Sstevel@tonic-gate dict_init(); 7817c478bd9Sstevel@tonic-gate timeout_init(); 7827c478bd9Sstevel@tonic-gate restarter_protocol_init(); 7837c478bd9Sstevel@tonic-gate restarter_init(); 7847c478bd9Sstevel@tonic-gate graph_protocol_init(); 7857c478bd9Sstevel@tonic-gate graph_init(); 7867c478bd9Sstevel@tonic-gate 7877c478bd9Sstevel@tonic-gate init_env(); 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate set_boot_env(); 7907c478bd9Sstevel@tonic-gate restarter_start(); 7917c478bd9Sstevel@tonic-gate graph_engine_start(); 7927c478bd9Sstevel@tonic-gate } 7937c478bd9Sstevel@tonic-gate 7947c478bd9Sstevel@tonic-gate static void 7957c478bd9Sstevel@tonic-gate usage(const char *name) 7967c478bd9Sstevel@tonic-gate { 79754d6518cSrm88369 uu_warn(gettext("usage: %s [-n]\n"), name); 7987c478bd9Sstevel@tonic-gate exit(UU_EXIT_USAGE); 7997c478bd9Sstevel@tonic-gate } 8007c478bd9Sstevel@tonic-gate 8017c478bd9Sstevel@tonic-gate static int 8027c478bd9Sstevel@tonic-gate daemonize_start(void) 8037c478bd9Sstevel@tonic-gate { 8047c478bd9Sstevel@tonic-gate pid_t pid; 8057c478bd9Sstevel@tonic-gate int fd; 8067c478bd9Sstevel@tonic-gate 8077c478bd9Sstevel@tonic-gate if ((pid = fork1()) < 0) 8087c478bd9Sstevel@tonic-gate return (-1); 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate if (pid != 0) 8117c478bd9Sstevel@tonic-gate exit(0); 8127c478bd9Sstevel@tonic-gate 8132f1b831aSacruz (void) close(STDIN_FILENO); 8147c478bd9Sstevel@tonic-gate 8157c478bd9Sstevel@tonic-gate if ((fd = open("/dev/null", O_RDONLY)) == -1) { 8167c478bd9Sstevel@tonic-gate uu_warn(gettext("can't connect stdin to /dev/null")); 8172f1b831aSacruz } else if (fd != STDIN_FILENO) { 8182f1b831aSacruz (void) dup2(fd, STDIN_FILENO); 8197c478bd9Sstevel@tonic-gate startd_close(fd); 8207c478bd9Sstevel@tonic-gate } 8217c478bd9Sstevel@tonic-gate 8227c478bd9Sstevel@tonic-gate closefrom(3); 8232f1b831aSacruz (void) dup2(STDERR_FILENO, STDOUT_FILENO); 8247c478bd9Sstevel@tonic-gate 8257c478bd9Sstevel@tonic-gate (void) setsid(); 8267c478bd9Sstevel@tonic-gate (void) chdir("/"); 8277c478bd9Sstevel@tonic-gate 8287c478bd9Sstevel@tonic-gate /* Use default umask that init handed us, but 022 to create files. */ 8297c478bd9Sstevel@tonic-gate dmask = umask(022); 8307c478bd9Sstevel@tonic-gate fmask = umask(dmask); 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate return (0); 8337c478bd9Sstevel@tonic-gate } 8347c478bd9Sstevel@tonic-gate 8357c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 8367c478bd9Sstevel@tonic-gate static void 8377c478bd9Sstevel@tonic-gate die_handler(int sig, siginfo_t *info, void *data) 8387c478bd9Sstevel@tonic-gate { 8397c478bd9Sstevel@tonic-gate finished = 1; 8407c478bd9Sstevel@tonic-gate } 8417c478bd9Sstevel@tonic-gate 8427c478bd9Sstevel@tonic-gate int 8437c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 8447c478bd9Sstevel@tonic-gate { 8457c478bd9Sstevel@tonic-gate int opt; 8467c478bd9Sstevel@tonic-gate int daemonize = 1; 8477c478bd9Sstevel@tonic-gate struct sigaction act; 8487c478bd9Sstevel@tonic-gate sigset_t nullset; 8497c478bd9Sstevel@tonic-gate struct stat sb; 8507c478bd9Sstevel@tonic-gate 8517c478bd9Sstevel@tonic-gate (void) uu_setpname(argv[0]); 8527c478bd9Sstevel@tonic-gate 8537c478bd9Sstevel@tonic-gate st = startd_zalloc(sizeof (startd_state_t)); 8547c478bd9Sstevel@tonic-gate 8557c478bd9Sstevel@tonic-gate (void) pthread_mutexattr_init(&mutex_attrs); 8567c478bd9Sstevel@tonic-gate #ifndef NDEBUG 8577c478bd9Sstevel@tonic-gate (void) pthread_mutexattr_settype(&mutex_attrs, 8587c478bd9Sstevel@tonic-gate PTHREAD_MUTEX_ERRORCHECK); 8597c478bd9Sstevel@tonic-gate #endif 8607c478bd9Sstevel@tonic-gate 8617c478bd9Sstevel@tonic-gate max_scf_name_size = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH); 8627c478bd9Sstevel@tonic-gate max_scf_value_size = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH); 8637c478bd9Sstevel@tonic-gate max_scf_fmri_size = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH); 8647c478bd9Sstevel@tonic-gate 8657c478bd9Sstevel@tonic-gate if (max_scf_name_size == -1 || max_scf_value_size == -1 || 8667c478bd9Sstevel@tonic-gate max_scf_value_size == -1) 8677c478bd9Sstevel@tonic-gate uu_die("Can't determine repository maximum lengths.\n"); 8687c478bd9Sstevel@tonic-gate 8697c478bd9Sstevel@tonic-gate max_scf_name_size++; 8707c478bd9Sstevel@tonic-gate max_scf_value_size++; 8717c478bd9Sstevel@tonic-gate max_scf_fmri_size++; 8727c478bd9Sstevel@tonic-gate 87399b44c3bSlianep st->st_log_flags = STARTD_LOG_FILE | STARTD_LOG_SYSLOG; 87499b44c3bSlianep st->st_log_level_min = LOG_NOTICE; 8757c478bd9Sstevel@tonic-gate 87699b44c3bSlianep while ((opt = getopt(argc, argv, "nrs")) != EOF) { 8777c478bd9Sstevel@tonic-gate switch (opt) { 8787c478bd9Sstevel@tonic-gate case 'n': 8797c478bd9Sstevel@tonic-gate daemonize = 0; 8807c478bd9Sstevel@tonic-gate break; 8817c478bd9Sstevel@tonic-gate case 'r': /* reconfiguration boot */ 8827c478bd9Sstevel@tonic-gate opt_reconfig = 1; 8837c478bd9Sstevel@tonic-gate break; 8847c478bd9Sstevel@tonic-gate case 's': /* single-user mode */ 8857c478bd9Sstevel@tonic-gate booting_to_single_user = B_TRUE; 8867c478bd9Sstevel@tonic-gate break; 8877c478bd9Sstevel@tonic-gate default: 8887c478bd9Sstevel@tonic-gate usage(argv[0]); /* exits */ 8897c478bd9Sstevel@tonic-gate } 8907c478bd9Sstevel@tonic-gate } 8917c478bd9Sstevel@tonic-gate 8927c478bd9Sstevel@tonic-gate if (optind != argc) 8937c478bd9Sstevel@tonic-gate usage(argv[0]); 8947c478bd9Sstevel@tonic-gate 895004388ebScasper (void) enable_extended_FILE_stdio(-1, -1); 896004388ebScasper 8977c478bd9Sstevel@tonic-gate if (daemonize) 8987c478bd9Sstevel@tonic-gate if (daemonize_start() < 0) 8997c478bd9Sstevel@tonic-gate uu_die("Can't daemonize\n"); 9007c478bd9Sstevel@tonic-gate 9017c478bd9Sstevel@tonic-gate log_init(); 9027c478bd9Sstevel@tonic-gate 9037c478bd9Sstevel@tonic-gate if (stat("/etc/svc/volatile/resetting", &sb) != -1) { 9047c478bd9Sstevel@tonic-gate log_framework(LOG_NOTICE, "Restarter quiesced.\n"); 9057c478bd9Sstevel@tonic-gate 9067c478bd9Sstevel@tonic-gate for (;;) 9077c478bd9Sstevel@tonic-gate (void) pause(); 9087c478bd9Sstevel@tonic-gate } 9097c478bd9Sstevel@tonic-gate 9107c478bd9Sstevel@tonic-gate act.sa_sigaction = &die_handler; 9117c478bd9Sstevel@tonic-gate (void) sigfillset(&act.sa_mask); 9127c478bd9Sstevel@tonic-gate act.sa_flags = SA_SIGINFO; 9137c478bd9Sstevel@tonic-gate (void) sigaction(SIGINT, &act, NULL); 9147c478bd9Sstevel@tonic-gate (void) sigaction(SIGTERM, &act, NULL); 9157c478bd9Sstevel@tonic-gate 91699b44c3bSlianep startup(); 9177c478bd9Sstevel@tonic-gate 9187c478bd9Sstevel@tonic-gate (void) sigemptyset(&nullset); 9197c478bd9Sstevel@tonic-gate while (!finished) { 9207c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "Main thread paused\n"); 9217c478bd9Sstevel@tonic-gate (void) sigsuspend(&nullset); 9227c478bd9Sstevel@tonic-gate } 9237c478bd9Sstevel@tonic-gate 9247c478bd9Sstevel@tonic-gate (void) log_framework(LOG_DEBUG, "Restarter exiting.\n"); 9257c478bd9Sstevel@tonic-gate return (0); 9267c478bd9Sstevel@tonic-gate } 927