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