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 /*
23f6e214c7SGavin Maltby * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24*2ba6d2b9SRobert Mustacchi * Copyright 2015, Joyent, Inc.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate * startd.c - the master restarter
297c478bd9Sstevel@tonic-gate *
307c478bd9Sstevel@tonic-gate * svc.startd comprises two halves. The graph engine is based in graph.c and
317c478bd9Sstevel@tonic-gate * maintains the service dependency graph based on the information in the
327c478bd9Sstevel@tonic-gate * repository. For each service it also tracks the current state and the
337c478bd9Sstevel@tonic-gate * restarter responsible for the service. Based on the graph, events from the
347c478bd9Sstevel@tonic-gate * repository (mostly administrative requests from svcadm), and messages from
357c478bd9Sstevel@tonic-gate * the restarters, the graph engine makes decisions about how the services
367c478bd9Sstevel@tonic-gate * should be manipulated and sends commands to the appropriate restarters.
377c478bd9Sstevel@tonic-gate * Communication between the graph engine and the restarters is embodied in
387c478bd9Sstevel@tonic-gate * protocol.c.
397c478bd9Sstevel@tonic-gate *
407c478bd9Sstevel@tonic-gate * The second half of svc.startd is the restarter for services managed by
417c478bd9Sstevel@tonic-gate * svc.startd and is primarily contained in restarter.c. It responds to graph
427c478bd9Sstevel@tonic-gate * engine commands by executing methods, updating the repository, and sending
437c478bd9Sstevel@tonic-gate * feedback (mostly state updates) to the graph engine.
447c478bd9Sstevel@tonic-gate *
45*2ba6d2b9SRobert Mustacchi * Overview of the SMF Architecture
46*2ba6d2b9SRobert Mustacchi *
47*2ba6d2b9SRobert Mustacchi * There are a few different components that make up SMF and are responsible
48*2ba6d2b9SRobert Mustacchi * for different pieces of functionality that are used:
49*2ba6d2b9SRobert Mustacchi *
50*2ba6d2b9SRobert Mustacchi * svc.startd(1M): A daemon that is in charge of starting, stopping, and
51*2ba6d2b9SRobert Mustacchi * restarting services and instances.
52*2ba6d2b9SRobert Mustacchi * svc.configd(1M): A daemon that manages the repository that stores
53*2ba6d2b9SRobert Mustacchi * information, property groups, and state of the different services and
54*2ba6d2b9SRobert Mustacchi * instances.
55*2ba6d2b9SRobert Mustacchi * libscf(3LIB): A C library that provides the glue for communicating,
56*2ba6d2b9SRobert Mustacchi * accessing, and updating information about services and instances.
57*2ba6d2b9SRobert Mustacchi * svccfg(1M): A utility to add and remove services as well as change the
58*2ba6d2b9SRobert Mustacchi * properties associated with different services and instances.
59*2ba6d2b9SRobert Mustacchi * svcadm(1M): A utility to control the different instance of a service. You
60*2ba6d2b9SRobert Mustacchi * can use this to enable and disable them among some other useful things.
61*2ba6d2b9SRobert Mustacchi * svcs(1): A utility that reports on the status of various services on the
62*2ba6d2b9SRobert Mustacchi * system.
63*2ba6d2b9SRobert Mustacchi *
64*2ba6d2b9SRobert Mustacchi * The following block diagram explains how these components communicate:
65*2ba6d2b9SRobert Mustacchi *
66*2ba6d2b9SRobert Mustacchi * The SMF Block Diagram
67*2ba6d2b9SRobert Mustacchi * Repository
68*2ba6d2b9SRobert Mustacchi * This attempts to show +---------+ +--------+
69*2ba6d2b9SRobert Mustacchi * the relations between | | SQL | |
70*2ba6d2b9SRobert Mustacchi * the different pieces | configd |<----------->| SQLite |
71*2ba6d2b9SRobert Mustacchi * that make SMF work and | | Transaction | |
72*2ba6d2b9SRobert Mustacchi * users/administrators +---------+ +--------+
73*2ba6d2b9SRobert Mustacchi * call into. ^ ^
74*2ba6d2b9SRobert Mustacchi * | |
75*2ba6d2b9SRobert Mustacchi * door_call(3C)| | door_call(3C)
76*2ba6d2b9SRobert Mustacchi * | |
77*2ba6d2b9SRobert Mustacchi * v v
78*2ba6d2b9SRobert Mustacchi * +----------+ +--------+ +--------+ +----------+
79*2ba6d2b9SRobert Mustacchi * | | | | | | | svccfg |
80*2ba6d2b9SRobert Mustacchi * | startd |<--->| libscf | | libscf |<---->| svcadm |
81*2ba6d2b9SRobert Mustacchi * | | | (3LIB) | | (3LIB) | | svcs |
82*2ba6d2b9SRobert Mustacchi * +----------+ +--------+ +--------+ +----------+
83*2ba6d2b9SRobert Mustacchi * ^ ^
84*2ba6d2b9SRobert Mustacchi * | | fork(2)/exec(2)
85*2ba6d2b9SRobert Mustacchi * | | libcontract(3LIB)
86*2ba6d2b9SRobert Mustacchi * v v Various System/User services
87*2ba6d2b9SRobert Mustacchi * +-------------------------------------------------------------------+
88*2ba6d2b9SRobert Mustacchi * | system/filesystem/local:default system/coreadm:default |
89*2ba6d2b9SRobert Mustacchi * | network/loopback:default system/zones:default |
90*2ba6d2b9SRobert Mustacchi * | milestone/multi-user:default system/cron:default |
91*2ba6d2b9SRobert Mustacchi * | system/console-login:default network/ssh:default |
92*2ba6d2b9SRobert Mustacchi * | system/pfexec:default system/svc/restarter:default |
93*2ba6d2b9SRobert Mustacchi * +-------------------------------------------------------------------+
94*2ba6d2b9SRobert Mustacchi *
95*2ba6d2b9SRobert Mustacchi * Chatting with Configd and Sharing Repository Information
96*2ba6d2b9SRobert Mustacchi *
97*2ba6d2b9SRobert Mustacchi * As you run commands with svcs, svccfg, and svcadm, they are all creating a
98*2ba6d2b9SRobert Mustacchi * libscf handle to communicate with configd. As calls are made via libscf they
99*2ba6d2b9SRobert Mustacchi * ultimately go and talk to configd to get information. However, how we
100*2ba6d2b9SRobert Mustacchi * actually are talking to configd is not as straightforward as it appears.
101*2ba6d2b9SRobert Mustacchi *
102*2ba6d2b9SRobert Mustacchi * When configd starts up it creates a door located at
103*2ba6d2b9SRobert Mustacchi * /etc/svc/volatile/repository_door. This door runs the routine called
104*2ba6d2b9SRobert Mustacchi * main_switcher() from usr/src/cmd/svc/configd/maindoor.c. When you first
105*2ba6d2b9SRobert Mustacchi * invoke svc(cfg|s|adm), one of the first things that occurs is creating a
106*2ba6d2b9SRobert Mustacchi * scf_handle_t and binding it to configd by calling scf_handle_bind(). This
107*2ba6d2b9SRobert Mustacchi * function makes a door call to configd and gets returned a new file
108*2ba6d2b9SRobert Mustacchi * descriptor. This file descriptor is itself another door which calls into
109*2ba6d2b9SRobert Mustacchi * configd's client_switcher(). This is the door that is actually used when
110*2ba6d2b9SRobert Mustacchi * getting and fetching properties, and many other useful things.
111*2ba6d2b9SRobert Mustacchi *
112*2ba6d2b9SRobert Mustacchi * svc.startd needs a way to notice the changes that occur to the repository.
113*2ba6d2b9SRobert Mustacchi * For example, if you enabled a service that was not previously running, it's
114*2ba6d2b9SRobert Mustacchi * up to startd to notice that this has happened, check dependencies, and
115*2ba6d2b9SRobert Mustacchi * eventually start up the service. The way it gets these notifications is via
116*2ba6d2b9SRobert Mustacchi * a thread who's sole purpose in life is to call _scf_notify_wait(). This
117*2ba6d2b9SRobert Mustacchi * function acts like poll(2) but for changes that occur in the repository.
118*2ba6d2b9SRobert Mustacchi * Once this thread gets the event, it dispatches the event appropriately.
119*2ba6d2b9SRobert Mustacchi *
120*2ba6d2b9SRobert Mustacchi * The Events of svc.startd
121*2ba6d2b9SRobert Mustacchi *
122*2ba6d2b9SRobert Mustacchi * svc.startd has to handle a lot of complexity. Understanding how you go from
123*2ba6d2b9SRobert Mustacchi * getting the notification that a service was enabled to actually enabling it
124*2ba6d2b9SRobert Mustacchi * is not obvious from a cursory glance. The first thing to keep in mind is
125*2ba6d2b9SRobert Mustacchi * that startd maintains a graph of all the related services and instances so
126*2ba6d2b9SRobert Mustacchi * it can keep track of what is enabled, what dependencies exist, etc. all so
127*2ba6d2b9SRobert Mustacchi * that it can answer the question of what is affected by a change. Internally
128*2ba6d2b9SRobert Mustacchi * there are a lot of different queues for events, threads to process these
129*2ba6d2b9SRobert Mustacchi * queues, and different paths to have events enter these queues. What follows
130*2ba6d2b9SRobert Mustacchi * is a diagram that attempts to explain some of those paths, though it's
131*2ba6d2b9SRobert Mustacchi * important to note that for some of these pieces, such as the graph and
132*2ba6d2b9SRobert Mustacchi * vertex events, there are many additional ways and code paths these threads
133*2ba6d2b9SRobert Mustacchi * and functions can take. And yes, restarter_event_enqueue() is not the same
134*2ba6d2b9SRobert Mustacchi * thing as restarter_queue_event().
135*2ba6d2b9SRobert Mustacchi *
136*2ba6d2b9SRobert Mustacchi * Threads/Functions Queues Threads/Functions
137*2ba6d2b9SRobert Mustacchi *
138*2ba6d2b9SRobert Mustacchi * called by various
139*2ba6d2b9SRobert Mustacchi * +----------------+ +-------+ +-------------+
140*2ba6d2b9SRobert Mustacchi * --->| graph_protocol | graph_event | graph | graph_event_ | graph_event |
141*2ba6d2b9SRobert Mustacchi * --->| _send_event() |------------>| event |----------------->| _thread |
142*2ba6d2b9SRobert Mustacchi * +----------------+ _enqueue() | queue | dequeue() +-------------+
143*2ba6d2b9SRobert Mustacchi * +-------+ |
144*2ba6d2b9SRobert Mustacchi * _scf_notify_wait() vertex_send_event()|
145*2ba6d2b9SRobert Mustacchi * | v
146*2ba6d2b9SRobert Mustacchi * | +------------------+ +--------------------+
147*2ba6d2b9SRobert Mustacchi * +->| repository_event | vertex_send_event() | restarter_protocol |
148*2ba6d2b9SRobert Mustacchi * | _thread |----------------------------->| _send_event() |
149*2ba6d2b9SRobert Mustacchi * +------------------+ +--------------------+
150*2ba6d2b9SRobert Mustacchi * | | out to other
151*2ba6d2b9SRobert Mustacchi * restarter_ restarter_ | | restarters
152*2ba6d2b9SRobert Mustacchi * event_dequeue() +-----------+ event_ | | not startd
153*2ba6d2b9SRobert Mustacchi * +----------------| restarter |<------------+ +------------->
154*2ba6d2b9SRobert Mustacchi * v | event | enqueue()
155*2ba6d2b9SRobert Mustacchi * +-----------------+ | queue | +------------------>
156*2ba6d2b9SRobert Mustacchi * | restarter_event | +-----------+ |+----------------->
157*2ba6d2b9SRobert Mustacchi * | _thread | ||+---------------->
158*2ba6d2b9SRobert Mustacchi * +-----------------+ ||| start/stop inst
159*2ba6d2b9SRobert Mustacchi * | +--------------+ +--------------------+
160*2ba6d2b9SRobert Mustacchi * | | instance | | restarter_process_ |
161*2ba6d2b9SRobert Mustacchi * +-------------->| event |------>| events |
162*2ba6d2b9SRobert Mustacchi * restarter_ | queue | | per-instance lwp |
163*2ba6d2b9SRobert Mustacchi * queue_event() +--------------+ +--------------------+
164*2ba6d2b9SRobert Mustacchi * ||| various funcs
165*2ba6d2b9SRobert Mustacchi * ||| controlling
166*2ba6d2b9SRobert Mustacchi * ||| instance state
167*2ba6d2b9SRobert Mustacchi * ||+--------------->
168*2ba6d2b9SRobert Mustacchi * |+---------------->
169*2ba6d2b9SRobert Mustacchi * +----------------->
170*2ba6d2b9SRobert Mustacchi *
171*2ba6d2b9SRobert Mustacchi * What's important to take away is that there is a queue for each instance on
172*2ba6d2b9SRobert Mustacchi * the system that handles events related to dealing directly with that
173*2ba6d2b9SRobert Mustacchi * instance and that events can be added to it because of changes to properties
174*2ba6d2b9SRobert Mustacchi * that are made to configd and acted upon asynchronously by startd.
175*2ba6d2b9SRobert Mustacchi *
1767c478bd9Sstevel@tonic-gate * Error handling
1777c478bd9Sstevel@tonic-gate *
1787c478bd9Sstevel@tonic-gate * In general, when svc.startd runs out of memory it reattempts a few times,
1797c478bd9Sstevel@tonic-gate * sleeping inbetween, before giving up and exiting (see startd_alloc_retry()).
1807c478bd9Sstevel@tonic-gate * When a repository connection is broken (libscf calls fail with
1817c478bd9Sstevel@tonic-gate * SCF_ERROR_CONNECTION_BROKEN, librestart and internal functions return
1827c478bd9Sstevel@tonic-gate * ECONNABORTED), svc.startd calls libscf_rebind_handle(), which coordinates
1837c478bd9Sstevel@tonic-gate * with the svc.configd-restarting thread, fork_configd_thread(), via
1847c478bd9Sstevel@tonic-gate * st->st_configd_live_cv, and rebinds the repository handle. Doing so resets
1857c478bd9Sstevel@tonic-gate * all libscf state associated with that handle, so functions which do this
1867c478bd9Sstevel@tonic-gate * should communicate the event to their callers (usually by returning
1877c478bd9Sstevel@tonic-gate * ECONNRESET) so they may reset their state appropriately.
1885b7f77adStw21770 *
1895b7f77adStw21770 * External references
1905b7f77adStw21770 *
1915b7f77adStw21770 * svc.configd generates special security audit events for changes to some
1925b7f77adStw21770 * restarter related properties. See the special_props_list array in
1935b7f77adStw21770 * usr/src/cmd/svc/configd/rc_node.c for the properties that cause these audit
1945b7f77adStw21770 * events. If you change the semantics of these propereties within startd, you
1955b7f77adStw21770 * will probably need to update rc_node.c
1967c478bd9Sstevel@tonic-gate */
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate #include <stdio.h>
199004388ebScasper #include <stdio_ext.h>
2007c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> /* uses FILE * without including stdio.h */
2017c478bd9Sstevel@tonic-gate #include <alloca.h>
2027c478bd9Sstevel@tonic-gate #include <sys/mount.h>
2037c478bd9Sstevel@tonic-gate #include <sys/stat.h>
2047c478bd9Sstevel@tonic-gate #include <sys/types.h>
2057c478bd9Sstevel@tonic-gate #include <sys/wait.h>
2067c478bd9Sstevel@tonic-gate #include <assert.h>
2077c478bd9Sstevel@tonic-gate #include <errno.h>
2087c478bd9Sstevel@tonic-gate #include <fcntl.h>
2097c478bd9Sstevel@tonic-gate #include <ftw.h>
2107c478bd9Sstevel@tonic-gate #include <libintl.h>
2117c478bd9Sstevel@tonic-gate #include <libscf.h>
2127c478bd9Sstevel@tonic-gate #include <libscf_priv.h>
2137c478bd9Sstevel@tonic-gate #include <libuutil.h>
2147c478bd9Sstevel@tonic-gate #include <locale.h>
2157c478bd9Sstevel@tonic-gate #include <poll.h>
2167c478bd9Sstevel@tonic-gate #include <pthread.h>
2177c478bd9Sstevel@tonic-gate #include <signal.h>
2187c478bd9Sstevel@tonic-gate #include <stdarg.h>
2197c478bd9Sstevel@tonic-gate #include <stdlib.h>
2207c478bd9Sstevel@tonic-gate #include <string.h>
2217c478bd9Sstevel@tonic-gate #include <strings.h>
2227c478bd9Sstevel@tonic-gate #include <unistd.h>
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate #include "startd.h"
2257c478bd9Sstevel@tonic-gate #include "protocol.h"
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate ssize_t max_scf_name_size;
2287c478bd9Sstevel@tonic-gate ssize_t max_scf_fmri_size;
2297c478bd9Sstevel@tonic-gate ssize_t max_scf_value_size;
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate mode_t fmask;
2327c478bd9Sstevel@tonic-gate mode_t dmask;
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate graph_update_t *gu;
2357c478bd9Sstevel@tonic-gate restarter_update_t *ru;
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate startd_state_t *st;
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate boolean_t booting_to_single_user = B_FALSE;
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate const char * const admin_actions[] = {
2427c478bd9Sstevel@tonic-gate SCF_PROPERTY_DEGRADED,
2437c478bd9Sstevel@tonic-gate SCF_PROPERTY_MAINT_OFF,
2447c478bd9Sstevel@tonic-gate SCF_PROPERTY_MAINT_ON,
2457c478bd9Sstevel@tonic-gate SCF_PROPERTY_MAINT_ON_IMMEDIATE,
2467c478bd9Sstevel@tonic-gate SCF_PROPERTY_REFRESH,
2477c478bd9Sstevel@tonic-gate SCF_PROPERTY_RESTART
2487c478bd9Sstevel@tonic-gate };
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate const int admin_events[NACTIONS] = {
2517c478bd9Sstevel@tonic-gate RESTARTER_EVENT_TYPE_ADMIN_DEGRADED,
2527c478bd9Sstevel@tonic-gate RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF,
2537c478bd9Sstevel@tonic-gate RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON,
2547c478bd9Sstevel@tonic-gate RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE,
2557c478bd9Sstevel@tonic-gate RESTARTER_EVENT_TYPE_ADMIN_REFRESH,
2567c478bd9Sstevel@tonic-gate RESTARTER_EVENT_TYPE_ADMIN_RESTART
2577c478bd9Sstevel@tonic-gate };
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate const char * const instance_state_str[] = {
2607c478bd9Sstevel@tonic-gate "none",
2617c478bd9Sstevel@tonic-gate "uninitialized",
2627c478bd9Sstevel@tonic-gate "maintenance",
2637c478bd9Sstevel@tonic-gate "offline",
2647c478bd9Sstevel@tonic-gate "disabled",
2657c478bd9Sstevel@tonic-gate "online",
2667c478bd9Sstevel@tonic-gate "degraded"
2677c478bd9Sstevel@tonic-gate };
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate static int finished = 0;
2707c478bd9Sstevel@tonic-gate static int opt_reconfig = 0;
2717c478bd9Sstevel@tonic-gate static uint8_t prop_reconfig = 0;
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate #define INITIAL_REBIND_ATTEMPTS 5
2747c478bd9Sstevel@tonic-gate #define INITIAL_REBIND_DELAY 3
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate pthread_mutexattr_t mutex_attrs;
2777c478bd9Sstevel@tonic-gate
2783dd94f79SBryan Cantrill #ifdef DEBUG
2797c478bd9Sstevel@tonic-gate const char *
_umem_debug_init(void)2807c478bd9Sstevel@tonic-gate _umem_debug_init(void)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate return ("default,verbose"); /* UMEM_DEBUG setting */
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate const char *
_umem_logging_init(void)2867c478bd9Sstevel@tonic-gate _umem_logging_init(void)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate return ("fail,contents"); /* UMEM_LOGGING setting */
2897c478bd9Sstevel@tonic-gate }
2903dd94f79SBryan Cantrill #endif
2913dd94f79SBryan Cantrill
2923dd94f79SBryan Cantrill const char *
_umem_options_init(void)2933dd94f79SBryan Cantrill _umem_options_init(void)
2943dd94f79SBryan Cantrill {
2953dd94f79SBryan Cantrill /*
2963dd94f79SBryan Cantrill * To reduce our memory footprint, we set our UMEM_OPTIONS to indicate
2973dd94f79SBryan Cantrill * that we do not wish to have per-CPU magazines -- if svc.startd is so
2983dd94f79SBryan Cantrill * hot on CPU such that this becomes a scalability problem, there are
2993dd94f79SBryan Cantrill * likely deeper things amiss...
3003dd94f79SBryan Cantrill */
3013dd94f79SBryan Cantrill return ("nomagazines"); /* UMEM_OPTIONS setting */
3023dd94f79SBryan Cantrill }
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate /*
3057c478bd9Sstevel@tonic-gate * startd_alloc_retry()
3067c478bd9Sstevel@tonic-gate * Wrapper for allocation functions. Retries with a decaying time
3077c478bd9Sstevel@tonic-gate * value on failure to allocate, and aborts startd if failure is
3087c478bd9Sstevel@tonic-gate * persistent.
3097c478bd9Sstevel@tonic-gate */
3107c478bd9Sstevel@tonic-gate void *
startd_alloc_retry(void * f (size_t,int),size_t sz)3117c478bd9Sstevel@tonic-gate startd_alloc_retry(void *f(size_t, int), size_t sz)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate void *p;
3147c478bd9Sstevel@tonic-gate uint_t try, msecs;
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate p = f(sz, UMEM_DEFAULT);
3177c478bd9Sstevel@tonic-gate if (p != NULL || sz == 0)
3187c478bd9Sstevel@tonic-gate return (p);
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate msecs = ALLOC_DELAY;
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate for (try = 0; p == NULL && try < ALLOC_RETRY; ++try) {
3237c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, msecs);
3247c478bd9Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT;
3257c478bd9Sstevel@tonic-gate p = f(sz, UMEM_DEFAULT);
3267c478bd9Sstevel@tonic-gate if (p != NULL)
3277c478bd9Sstevel@tonic-gate return (p);
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate
3307c478bd9Sstevel@tonic-gate uu_die("Insufficient memory.\n");
3317c478bd9Sstevel@tonic-gate /* NOTREACHED */
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate
3347c478bd9Sstevel@tonic-gate void *
safe_realloc(void * p,size_t sz)3357c478bd9Sstevel@tonic-gate safe_realloc(void *p, size_t sz)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate uint_t try, msecs;
3387c478bd9Sstevel@tonic-gate
3397c478bd9Sstevel@tonic-gate p = realloc(p, sz);
3407c478bd9Sstevel@tonic-gate if (p != NULL || sz == 0)
3417c478bd9Sstevel@tonic-gate return (p);
3427c478bd9Sstevel@tonic-gate
3437c478bd9Sstevel@tonic-gate msecs = ALLOC_DELAY;
3447c478bd9Sstevel@tonic-gate
3457c478bd9Sstevel@tonic-gate for (try = 0; errno == EAGAIN && try < ALLOC_RETRY; ++try) {
3467c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, msecs);
3477c478bd9Sstevel@tonic-gate p = realloc(p, sz);
3487c478bd9Sstevel@tonic-gate if (p != NULL)
3497c478bd9Sstevel@tonic-gate return (p);
3507c478bd9Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT;
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate uu_die("Insufficient memory.\n");
3547c478bd9Sstevel@tonic-gate /* NOTREACHED */
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate char *
safe_strdup(const char * s)3587c478bd9Sstevel@tonic-gate safe_strdup(const char *s)
3597c478bd9Sstevel@tonic-gate {
3607c478bd9Sstevel@tonic-gate uint_t try, msecs;
3617c478bd9Sstevel@tonic-gate char *d;
3627c478bd9Sstevel@tonic-gate
3637c478bd9Sstevel@tonic-gate d = strdup(s);
3647c478bd9Sstevel@tonic-gate if (d != NULL)
3657c478bd9Sstevel@tonic-gate return (d);
3667c478bd9Sstevel@tonic-gate
3677c478bd9Sstevel@tonic-gate msecs = ALLOC_DELAY;
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate for (try = 0;
3707c478bd9Sstevel@tonic-gate (errno == EAGAIN || errno == ENOMEM) && try < ALLOC_RETRY;
3717c478bd9Sstevel@tonic-gate ++try) {
3727c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, msecs);
3737c478bd9Sstevel@tonic-gate d = strdup(s);
3747c478bd9Sstevel@tonic-gate if (d != NULL)
3757c478bd9Sstevel@tonic-gate return (d);
3767c478bd9Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT;
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate uu_die("Insufficient memory.\n");
3807c478bd9Sstevel@tonic-gate /* NOTREACHED */
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate
3847c478bd9Sstevel@tonic-gate void
startd_free(void * p,size_t sz)3857c478bd9Sstevel@tonic-gate startd_free(void *p, size_t sz)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate umem_free(p, sz);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate
3907c478bd9Sstevel@tonic-gate /*
3917c478bd9Sstevel@tonic-gate * Creates a uu_list_pool_t with the same retry policy as startd_alloc().
3927c478bd9Sstevel@tonic-gate * Only returns NULL for UU_ERROR_UNKNOWN_FLAG and UU_ERROR_NOT_SUPPORTED.
3937c478bd9Sstevel@tonic-gate */
3947c478bd9Sstevel@tonic-gate uu_list_pool_t *
startd_list_pool_create(const char * name,size_t e,size_t o,uu_compare_fn_t * f,uint32_t flags)3957c478bd9Sstevel@tonic-gate startd_list_pool_create(const char *name, size_t e, size_t o,
3967c478bd9Sstevel@tonic-gate uu_compare_fn_t *f, uint32_t flags)
3977c478bd9Sstevel@tonic-gate {
3987c478bd9Sstevel@tonic-gate uu_list_pool_t *pool;
3997c478bd9Sstevel@tonic-gate uint_t try, msecs;
4007c478bd9Sstevel@tonic-gate
4017c478bd9Sstevel@tonic-gate pool = uu_list_pool_create(name, e, o, f, flags);
4027c478bd9Sstevel@tonic-gate if (pool != NULL)
4037c478bd9Sstevel@tonic-gate return (pool);
4047c478bd9Sstevel@tonic-gate
4057c478bd9Sstevel@tonic-gate msecs = ALLOC_DELAY;
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate for (try = 0; uu_error() == UU_ERROR_NO_MEMORY && try < ALLOC_RETRY;
4087c478bd9Sstevel@tonic-gate ++try) {
4097c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, msecs);
4107c478bd9Sstevel@tonic-gate pool = uu_list_pool_create(name, e, o, f, flags);
4117c478bd9Sstevel@tonic-gate if (pool != NULL)
4127c478bd9Sstevel@tonic-gate return (pool);
4137c478bd9Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT;
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate if (try < ALLOC_RETRY)
4177c478bd9Sstevel@tonic-gate return (NULL);
4187c478bd9Sstevel@tonic-gate
4197c478bd9Sstevel@tonic-gate uu_die("Insufficient memory.\n");
4207c478bd9Sstevel@tonic-gate /* NOTREACHED */
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate
4237c478bd9Sstevel@tonic-gate /*
4247c478bd9Sstevel@tonic-gate * Creates a uu_list_t with the same retry policy as startd_alloc(). Only
4257c478bd9Sstevel@tonic-gate * returns NULL for UU_ERROR_UNKNOWN_FLAG and UU_ERROR_NOT_SUPPORTED.
4267c478bd9Sstevel@tonic-gate */
4277c478bd9Sstevel@tonic-gate uu_list_t *
startd_list_create(uu_list_pool_t * pool,void * parent,uint32_t flags)4287c478bd9Sstevel@tonic-gate startd_list_create(uu_list_pool_t *pool, void *parent, uint32_t flags)
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate uu_list_t *list;
4317c478bd9Sstevel@tonic-gate uint_t try, msecs;
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate list = uu_list_create(pool, parent, flags);
4347c478bd9Sstevel@tonic-gate if (list != NULL)
4357c478bd9Sstevel@tonic-gate return (list);
4367c478bd9Sstevel@tonic-gate
4377c478bd9Sstevel@tonic-gate msecs = ALLOC_DELAY;
4387c478bd9Sstevel@tonic-gate
4397c478bd9Sstevel@tonic-gate for (try = 0; uu_error() == UU_ERROR_NO_MEMORY && try < ALLOC_RETRY;
4407c478bd9Sstevel@tonic-gate ++try) {
4417c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, msecs);
4427c478bd9Sstevel@tonic-gate list = uu_list_create(pool, parent, flags);
4437c478bd9Sstevel@tonic-gate if (list != NULL)
4447c478bd9Sstevel@tonic-gate return (list);
4457c478bd9Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT;
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate
4487c478bd9Sstevel@tonic-gate if (try < ALLOC_RETRY)
4497c478bd9Sstevel@tonic-gate return (NULL);
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate uu_die("Insufficient memory.\n");
4527c478bd9Sstevel@tonic-gate /* NOTREACHED */
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate pthread_t
startd_thread_create(void * (* func)(void *),void * ptr)4567c478bd9Sstevel@tonic-gate startd_thread_create(void *(*func)(void *), void *ptr)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate int err;
4597c478bd9Sstevel@tonic-gate pthread_t tid;
4607c478bd9Sstevel@tonic-gate
4617c478bd9Sstevel@tonic-gate err = pthread_create(&tid, NULL, func, ptr);
4627c478bd9Sstevel@tonic-gate if (err != 0) {
4637c478bd9Sstevel@tonic-gate assert(err == EAGAIN);
4647c478bd9Sstevel@tonic-gate uu_die("Could not create thread.\n");
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate
4677c478bd9Sstevel@tonic-gate err = pthread_detach(tid);
4687c478bd9Sstevel@tonic-gate assert(err == 0);
4697c478bd9Sstevel@tonic-gate
4707c478bd9Sstevel@tonic-gate return (tid);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate
473f6e214c7SGavin Maltby extern int info_events_all;
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate static int
read_startd_config(void)47699b44c3bSlianep read_startd_config(void)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate scf_handle_t *hndl;
4797c478bd9Sstevel@tonic-gate scf_instance_t *inst;
4807c478bd9Sstevel@tonic-gate scf_propertygroup_t *pg;
4817c478bd9Sstevel@tonic-gate scf_property_t *prop;
4827c478bd9Sstevel@tonic-gate scf_value_t *val;
4837c478bd9Sstevel@tonic-gate scf_iter_t *iter, *piter;
4847c478bd9Sstevel@tonic-gate instance_data_t idata;
4857c478bd9Sstevel@tonic-gate char *buf, *vbuf;
4867c478bd9Sstevel@tonic-gate char *startd_options_fmri = uu_msprintf("%s/:properties/options",
4877c478bd9Sstevel@tonic-gate SCF_SERVICE_STARTD);
4887c478bd9Sstevel@tonic-gate char *startd_reconfigure_fmri = uu_msprintf(
4897c478bd9Sstevel@tonic-gate "%s/:properties/system/reconfigure", SCF_SERVICE_STARTD);
4907c478bd9Sstevel@tonic-gate char *env_opts, *lasts, *cp;
4917c478bd9Sstevel@tonic-gate int bind_fails = 0;
4927c478bd9Sstevel@tonic-gate int ret = 0, r;
4937c478bd9Sstevel@tonic-gate uint_t count = 0, msecs = ALLOC_DELAY;
4947c478bd9Sstevel@tonic-gate size_t sz;
4957c478bd9Sstevel@tonic-gate ctid_t ctid;
4967c478bd9Sstevel@tonic-gate uint64_t uint64;
4977c478bd9Sstevel@tonic-gate
4987c478bd9Sstevel@tonic-gate buf = startd_alloc(max_scf_fmri_size);
4997c478bd9Sstevel@tonic-gate
5007c478bd9Sstevel@tonic-gate if (startd_options_fmri == NULL || startd_reconfigure_fmri == NULL)
5017c478bd9Sstevel@tonic-gate uu_die("Allocation failure\n");
5027c478bd9Sstevel@tonic-gate
5037c478bd9Sstevel@tonic-gate st->st_log_prefix = LOG_PREFIX_EARLY;
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate if ((st->st_log_file = getenv("STARTD_DEFAULT_LOG")) == NULL) {
5067c478bd9Sstevel@tonic-gate st->st_log_file = startd_alloc(strlen(STARTD_DEFAULT_LOG) + 1);
5077c478bd9Sstevel@tonic-gate
5087c478bd9Sstevel@tonic-gate (void) strcpy(st->st_log_file, STARTD_DEFAULT_LOG);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate st->st_door_path = getenv("STARTD_ALT_DOOR");
5127c478bd9Sstevel@tonic-gate
5137c478bd9Sstevel@tonic-gate /*
5147c478bd9Sstevel@tonic-gate * Read "options" property group.
5157c478bd9Sstevel@tonic-gate */
5167c478bd9Sstevel@tonic-gate for (hndl = libscf_handle_create_bound(SCF_VERSION); hndl == NULL;
5177c478bd9Sstevel@tonic-gate hndl = libscf_handle_create_bound(SCF_VERSION), bind_fails++) {
5187c478bd9Sstevel@tonic-gate (void) sleep(INITIAL_REBIND_DELAY);
5197c478bd9Sstevel@tonic-gate
5207c478bd9Sstevel@tonic-gate if (bind_fails > INITIAL_REBIND_ATTEMPTS) {
5217c478bd9Sstevel@tonic-gate /*
5227c478bd9Sstevel@tonic-gate * In the case that we can't bind to the repository
5237c478bd9Sstevel@tonic-gate * (which should have been started), we need to allow
5247c478bd9Sstevel@tonic-gate * the user into maintenance mode to determine what's
5257c478bd9Sstevel@tonic-gate * failed.
5267c478bd9Sstevel@tonic-gate */
5277c478bd9Sstevel@tonic-gate log_framework(LOG_INFO, "Couldn't fetch "
5287c478bd9Sstevel@tonic-gate "default settings: %s\n",
5297c478bd9Sstevel@tonic-gate scf_strerror(scf_error()));
5307c478bd9Sstevel@tonic-gate
5317c478bd9Sstevel@tonic-gate ret = -1;
5327c478bd9Sstevel@tonic-gate
5337c478bd9Sstevel@tonic-gate goto noscfout;
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate
5377c478bd9Sstevel@tonic-gate idata.i_fmri = SCF_SERVICE_STARTD;
5387c478bd9Sstevel@tonic-gate idata.i_state = RESTARTER_STATE_NONE;
5397c478bd9Sstevel@tonic-gate idata.i_next_state = RESTARTER_STATE_NONE;
5407c478bd9Sstevel@tonic-gate timestamp:
5417c478bd9Sstevel@tonic-gate switch (r = _restarter_commit_states(hndl, &idata,
542f6e214c7SGavin Maltby RESTARTER_STATE_ONLINE, RESTARTER_STATE_NONE,
543f6e214c7SGavin Maltby restarter_get_str_short(restarter_str_insert_in_graph))) {
5447c478bd9Sstevel@tonic-gate case 0:
5457c478bd9Sstevel@tonic-gate break;
5467c478bd9Sstevel@tonic-gate
5477c478bd9Sstevel@tonic-gate case ENOMEM:
5487c478bd9Sstevel@tonic-gate ++count;
5497c478bd9Sstevel@tonic-gate if (count < ALLOC_RETRY) {
5507c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, msecs);
5517c478bd9Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT;
5527c478bd9Sstevel@tonic-gate goto timestamp;
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate
5557c478bd9Sstevel@tonic-gate uu_die("Insufficient memory.\n");
5567c478bd9Sstevel@tonic-gate /* NOTREACHED */
5577c478bd9Sstevel@tonic-gate
5587c478bd9Sstevel@tonic-gate case ECONNABORTED:
5597c478bd9Sstevel@tonic-gate libscf_handle_rebind(hndl);
5607c478bd9Sstevel@tonic-gate goto timestamp;
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate case ENOENT:
5637c478bd9Sstevel@tonic-gate case EPERM:
5647c478bd9Sstevel@tonic-gate case EACCES:
5657c478bd9Sstevel@tonic-gate case EROFS:
5667c478bd9Sstevel@tonic-gate log_error(LOG_INFO, "Could set state of %s: %s.\n",
5677c478bd9Sstevel@tonic-gate idata.i_fmri, strerror(r));
5687c478bd9Sstevel@tonic-gate break;
5697c478bd9Sstevel@tonic-gate
5707c478bd9Sstevel@tonic-gate case EINVAL:
5717c478bd9Sstevel@tonic-gate default:
5727c478bd9Sstevel@tonic-gate bad_error("_restarter_commit_states", r);
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate
5757c478bd9Sstevel@tonic-gate pg = safe_scf_pg_create(hndl);
5767c478bd9Sstevel@tonic-gate prop = safe_scf_property_create(hndl);
5777c478bd9Sstevel@tonic-gate val = safe_scf_value_create(hndl);
5787c478bd9Sstevel@tonic-gate inst = safe_scf_instance_create(hndl);
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate /* set startd's restarter properties */
5817c478bd9Sstevel@tonic-gate if (scf_handle_decode_fmri(hndl, SCF_SERVICE_STARTD, NULL, NULL, inst,
5827c478bd9Sstevel@tonic-gate NULL, NULL, SCF_DECODE_FMRI_EXACT) == 0) {
5837c478bd9Sstevel@tonic-gate (void) libscf_write_start_pid(inst, getpid());
5847c478bd9Sstevel@tonic-gate ctid = proc_get_ctid();
5857c478bd9Sstevel@tonic-gate if (ctid != -1) {
5867c478bd9Sstevel@tonic-gate uint64 = (uint64_t)ctid;
5877c478bd9Sstevel@tonic-gate (void) libscf_inst_set_count_prop(inst,
5887c478bd9Sstevel@tonic-gate SCF_PG_RESTARTER, SCF_PG_RESTARTER_TYPE,
5897c478bd9Sstevel@tonic-gate SCF_PG_RESTARTER_FLAGS, SCF_PROPERTY_CONTRACT,
5907c478bd9Sstevel@tonic-gate uint64);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate (void) libscf_note_method_log(inst, LOG_PREFIX_EARLY,
5937c478bd9Sstevel@tonic-gate STARTD_DEFAULT_LOG);
5947c478bd9Sstevel@tonic-gate (void) libscf_note_method_log(inst, LOG_PREFIX_NORMAL,
5957c478bd9Sstevel@tonic-gate STARTD_DEFAULT_LOG);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate
5987c478bd9Sstevel@tonic-gate /* Read reconfigure property for recovery. */
5997c478bd9Sstevel@tonic-gate if (scf_handle_decode_fmri(hndl, startd_reconfigure_fmri, NULL, NULL,
6007c478bd9Sstevel@tonic-gate NULL, NULL, prop, NULL) != -1 &&
6017c478bd9Sstevel@tonic-gate scf_property_get_value(prop, val) == 0)
6027c478bd9Sstevel@tonic-gate (void) scf_value_get_boolean(val, &prop_reconfig);
6037c478bd9Sstevel@tonic-gate
6047c478bd9Sstevel@tonic-gate if (scf_handle_decode_fmri(hndl, startd_options_fmri, NULL, NULL, NULL,
6057c478bd9Sstevel@tonic-gate pg, NULL, SCF_DECODE_FMRI_TRUNCATE) == -1) {
6067c478bd9Sstevel@tonic-gate /*
6077c478bd9Sstevel@tonic-gate * No configuration options defined.
6087c478bd9Sstevel@tonic-gate */
6097c478bd9Sstevel@tonic-gate if (scf_error() != SCF_ERROR_NOT_FOUND)
6107c478bd9Sstevel@tonic-gate uu_warn("Couldn't read configuration from 'options' "
6117c478bd9Sstevel@tonic-gate "group: %s\n", scf_strerror(scf_error()));
6127c478bd9Sstevel@tonic-gate goto scfout;
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate
6157c478bd9Sstevel@tonic-gate /*
6167c478bd9Sstevel@tonic-gate * If there is no "options" group defined, then our defaults are fine.
6177c478bd9Sstevel@tonic-gate */
6187c478bd9Sstevel@tonic-gate if (scf_pg_get_name(pg, NULL, 0) < 0)
6197c478bd9Sstevel@tonic-gate goto scfout;
6207c478bd9Sstevel@tonic-gate
621f6e214c7SGavin Maltby /* get info_events_all */
622f6e214c7SGavin Maltby info_events_all = libscf_get_info_events_all(pg);
623f6e214c7SGavin Maltby
6247c478bd9Sstevel@tonic-gate /* Iterate through. */
6257c478bd9Sstevel@tonic-gate iter = safe_scf_iter_create(hndl);
6267c478bd9Sstevel@tonic-gate
6277c478bd9Sstevel@tonic-gate (void) scf_iter_pg_properties(iter, pg);
6287c478bd9Sstevel@tonic-gate
6297c478bd9Sstevel@tonic-gate piter = safe_scf_iter_create(hndl);
6307c478bd9Sstevel@tonic-gate vbuf = startd_alloc(max_scf_value_size);
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate while ((scf_iter_next_property(iter, prop) == 1)) {
6337c478bd9Sstevel@tonic-gate scf_type_t ty;
6347c478bd9Sstevel@tonic-gate
6357c478bd9Sstevel@tonic-gate if (scf_property_get_name(prop, buf, max_scf_fmri_size) < 0)
6367c478bd9Sstevel@tonic-gate continue;
6377c478bd9Sstevel@tonic-gate
6387c478bd9Sstevel@tonic-gate if (strcmp(buf, "logging") != 0 &&
6397c478bd9Sstevel@tonic-gate strcmp(buf, "boot_messages") != 0)
6407c478bd9Sstevel@tonic-gate continue;
6417c478bd9Sstevel@tonic-gate
6427c478bd9Sstevel@tonic-gate if (scf_property_type(prop, &ty) != 0) {
6437c478bd9Sstevel@tonic-gate switch (scf_error()) {
6447c478bd9Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
6457c478bd9Sstevel@tonic-gate default:
6467c478bd9Sstevel@tonic-gate libscf_handle_rebind(hndl);
6477c478bd9Sstevel@tonic-gate continue;
6487c478bd9Sstevel@tonic-gate
6497c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
6507c478bd9Sstevel@tonic-gate continue;
6517c478bd9Sstevel@tonic-gate
6527c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
6537c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
6547c478bd9Sstevel@tonic-gate bad_error("scf_property_type", scf_error());
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate if (ty != SCF_TYPE_ASTRING) {
6597c478bd9Sstevel@tonic-gate uu_warn("property \"options/%s\" is not of type "
6607c478bd9Sstevel@tonic-gate "astring; ignored.\n", buf);
6617c478bd9Sstevel@tonic-gate continue;
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate
6647c478bd9Sstevel@tonic-gate if (scf_property_get_value(prop, val) != 0) {
6657c478bd9Sstevel@tonic-gate switch (scf_error()) {
6667c478bd9Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
6677c478bd9Sstevel@tonic-gate default:
6687c478bd9Sstevel@tonic-gate return (ECONNABORTED);
6697c478bd9Sstevel@tonic-gate
6707c478bd9Sstevel@tonic-gate case SCF_ERROR_DELETED:
6717c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
6727c478bd9Sstevel@tonic-gate return (0);
6737c478bd9Sstevel@tonic-gate
6747c478bd9Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
6757c478bd9Sstevel@tonic-gate uu_warn("property \"options/%s\" has multiple "
6767c478bd9Sstevel@tonic-gate "values; ignored.\n", buf);
6777c478bd9Sstevel@tonic-gate continue;
6787c478bd9Sstevel@tonic-gate
6793eae19d9Swesolows case SCF_ERROR_PERMISSION_DENIED:
6803eae19d9Swesolows uu_warn("property \"options/%s\" cannot be "
6813eae19d9Swesolows "read because startd has insufficient "
6823eae19d9Swesolows "permission; ignored.\n", buf);
6833eae19d9Swesolows continue;
6843eae19d9Swesolows
6857c478bd9Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
6867c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
6877c478bd9Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
6887c478bd9Sstevel@tonic-gate bad_error("scf_property_get_value",
6897c478bd9Sstevel@tonic-gate scf_error());
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate
6937c478bd9Sstevel@tonic-gate if (scf_value_get_astring(val, vbuf, max_scf_value_size) < 0)
6947c478bd9Sstevel@tonic-gate bad_error("scf_value_get_astring", scf_error());
6957c478bd9Sstevel@tonic-gate
69699b44c3bSlianep if (strcmp("logging", buf) == 0) {
6977c478bd9Sstevel@tonic-gate if (strcmp("verbose", vbuf) == 0) {
6987c478bd9Sstevel@tonic-gate st->st_boot_flags = STARTD_BOOT_VERBOSE;
6997c478bd9Sstevel@tonic-gate st->st_log_level_min = LOG_INFO;
7007c478bd9Sstevel@tonic-gate } else if (strcmp("debug", vbuf) == 0) {
7017c478bd9Sstevel@tonic-gate st->st_boot_flags = STARTD_BOOT_VERBOSE;
7027c478bd9Sstevel@tonic-gate st->st_log_level_min = LOG_DEBUG;
7037c478bd9Sstevel@tonic-gate } else if (strcmp("quiet", vbuf) == 0) {
7047c478bd9Sstevel@tonic-gate st->st_log_level_min = LOG_NOTICE;
7057c478bd9Sstevel@tonic-gate } else {
7067c478bd9Sstevel@tonic-gate uu_warn("unknown options/logging "
7077c478bd9Sstevel@tonic-gate "value '%s' ignored\n", vbuf);
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate
7107c478bd9Sstevel@tonic-gate } else if (strcmp("boot_messages", buf) == 0) {
7117c478bd9Sstevel@tonic-gate if (strcmp("quiet", vbuf) == 0) {
7127c478bd9Sstevel@tonic-gate st->st_boot_flags = STARTD_BOOT_QUIET;
7137c478bd9Sstevel@tonic-gate } else if (strcmp("verbose", vbuf) == 0) {
7147c478bd9Sstevel@tonic-gate st->st_boot_flags = STARTD_BOOT_VERBOSE;
7157c478bd9Sstevel@tonic-gate } else {
7167c478bd9Sstevel@tonic-gate log_framework(LOG_NOTICE, "unknown "
7177c478bd9Sstevel@tonic-gate "options/boot_messages value '%s' "
7187c478bd9Sstevel@tonic-gate "ignored\n", vbuf);
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate
7247c478bd9Sstevel@tonic-gate startd_free(vbuf, max_scf_value_size);
7257c478bd9Sstevel@tonic-gate scf_iter_destroy(piter);
7267c478bd9Sstevel@tonic-gate
7277c478bd9Sstevel@tonic-gate scf_iter_destroy(iter);
7287c478bd9Sstevel@tonic-gate
7297c478bd9Sstevel@tonic-gate scfout:
7307c478bd9Sstevel@tonic-gate scf_value_destroy(val);
7317c478bd9Sstevel@tonic-gate scf_pg_destroy(pg);
7327c478bd9Sstevel@tonic-gate scf_property_destroy(prop);
7337c478bd9Sstevel@tonic-gate scf_instance_destroy(inst);
7347c478bd9Sstevel@tonic-gate (void) scf_handle_unbind(hndl);
7357c478bd9Sstevel@tonic-gate scf_handle_destroy(hndl);
7367c478bd9Sstevel@tonic-gate
7377c478bd9Sstevel@tonic-gate noscfout:
7387c478bd9Sstevel@tonic-gate startd_free(buf, max_scf_fmri_size);
7397c478bd9Sstevel@tonic-gate uu_free(startd_options_fmri);
7407c478bd9Sstevel@tonic-gate uu_free(startd_reconfigure_fmri);
7417c478bd9Sstevel@tonic-gate
7427c478bd9Sstevel@tonic-gate if (booting_to_single_user) {
7437c478bd9Sstevel@tonic-gate st->st_subgraph = startd_alloc(max_scf_fmri_size);
7447c478bd9Sstevel@tonic-gate sz = strlcpy(st->st_subgraph, "milestone/single-user:default",
7457c478bd9Sstevel@tonic-gate max_scf_fmri_size);
7467c478bd9Sstevel@tonic-gate assert(sz < max_scf_fmri_size);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate
7497c478bd9Sstevel@tonic-gate /*
7507c478bd9Sstevel@tonic-gate * Options passed in as boot arguments override repository defaults.
7517c478bd9Sstevel@tonic-gate */
7527c478bd9Sstevel@tonic-gate env_opts = getenv("SMF_OPTIONS");
7537c478bd9Sstevel@tonic-gate if (env_opts == NULL)
7547c478bd9Sstevel@tonic-gate return (ret);
7557c478bd9Sstevel@tonic-gate
7567b966b00Sdstaff for (cp = strtok_r(env_opts, ",", &lasts); cp != NULL;
7577b966b00Sdstaff cp = strtok_r(NULL, ",", &lasts)) {
7587c478bd9Sstevel@tonic-gate if (strcmp(cp, "debug") == 0) {
7597c478bd9Sstevel@tonic-gate st->st_boot_flags = STARTD_BOOT_VERBOSE;
7607c478bd9Sstevel@tonic-gate st->st_log_level_min = LOG_DEBUG;
76199b44c3bSlianep
76299b44c3bSlianep /* -m debug should send messages to console */
76399b44c3bSlianep st->st_log_flags =
76499b44c3bSlianep st->st_log_flags | STARTD_LOG_TERMINAL;
7657c478bd9Sstevel@tonic-gate } else if (strcmp(cp, "verbose") == 0) {
7667c478bd9Sstevel@tonic-gate st->st_boot_flags = STARTD_BOOT_VERBOSE;
7677c478bd9Sstevel@tonic-gate st->st_log_level_min = LOG_INFO;
7687c478bd9Sstevel@tonic-gate } else if (strcmp(cp, "seed") == 0) {
7697c478bd9Sstevel@tonic-gate uu_warn("SMF option \"%s\" unimplemented.\n", cp);
7707c478bd9Sstevel@tonic-gate } else if (strcmp(cp, "quiet") == 0) {
7717c478bd9Sstevel@tonic-gate st->st_log_level_min = LOG_NOTICE;
7727c478bd9Sstevel@tonic-gate } else if (strncmp(cp, "milestone=",
7737c478bd9Sstevel@tonic-gate sizeof ("milestone=") - 1) == 0) {
7747c478bd9Sstevel@tonic-gate char *mp = cp + sizeof ("milestone=") - 1;
7757c478bd9Sstevel@tonic-gate
7767c478bd9Sstevel@tonic-gate if (booting_to_single_user)
7777c478bd9Sstevel@tonic-gate continue;
7787c478bd9Sstevel@tonic-gate
7797c478bd9Sstevel@tonic-gate if (st->st_subgraph == NULL) {
7807c478bd9Sstevel@tonic-gate st->st_subgraph =
7817c478bd9Sstevel@tonic-gate startd_alloc(max_scf_fmri_size);
7827c478bd9Sstevel@tonic-gate st->st_subgraph[0] = '\0';
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate
7857c478bd9Sstevel@tonic-gate if (mp[0] == '\0' || strcmp(mp, "all") == 0) {
7867c478bd9Sstevel@tonic-gate (void) strcpy(st->st_subgraph, "all");
7877c478bd9Sstevel@tonic-gate } else if (strcmp(mp, "su") == 0 ||
7887c478bd9Sstevel@tonic-gate strcmp(mp, "single-user") == 0) {
7897c478bd9Sstevel@tonic-gate (void) strcpy(st->st_subgraph,
7907c478bd9Sstevel@tonic-gate "milestone/single-user:default");
7917c478bd9Sstevel@tonic-gate } else if (strcmp(mp, "mu") == 0 ||
7927c478bd9Sstevel@tonic-gate strcmp(mp, "multi-user") == 0) {
7937c478bd9Sstevel@tonic-gate (void) strcpy(st->st_subgraph,
7947c478bd9Sstevel@tonic-gate "milestone/multi-user:default");
7957c478bd9Sstevel@tonic-gate } else if (strcmp(mp, "mus") == 0 ||
7967c478bd9Sstevel@tonic-gate strcmp(mp, "multi-user-server") == 0) {
7977c478bd9Sstevel@tonic-gate (void) strcpy(st->st_subgraph,
7987c478bd9Sstevel@tonic-gate "milestone/multi-user-server:default");
7997c478bd9Sstevel@tonic-gate } else if (strcmp(mp, "none") == 0) {
8007c478bd9Sstevel@tonic-gate (void) strcpy(st->st_subgraph, "none");
8017c478bd9Sstevel@tonic-gate } else {
8027c478bd9Sstevel@tonic-gate log_framework(LOG_NOTICE,
8037c478bd9Sstevel@tonic-gate "invalid milestone option value "
8047c478bd9Sstevel@tonic-gate "'%s' ignored\n", mp);
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate } else {
8077c478bd9Sstevel@tonic-gate uu_warn("Unknown SMF option \"%s\".\n", cp);
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate
8117c478bd9Sstevel@tonic-gate return (ret);
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate
8147c478bd9Sstevel@tonic-gate /*
8157c478bd9Sstevel@tonic-gate * void set_boot_env()
8167c478bd9Sstevel@tonic-gate *
8177c478bd9Sstevel@tonic-gate * If -r was passed or /reconfigure exists, this is a reconfig
8187c478bd9Sstevel@tonic-gate * reboot. We need to make sure that this information is given
8197c478bd9Sstevel@tonic-gate * to the appropriate services the first time they're started
8207c478bd9Sstevel@tonic-gate * by setting the system/reconfigure repository property,
8217c478bd9Sstevel@tonic-gate * as well as pass the _INIT_RECONFIG variable on to the rcS
8227c478bd9Sstevel@tonic-gate * start method so that legacy services can continue to use it.
8237c478bd9Sstevel@tonic-gate *
8247c478bd9Sstevel@tonic-gate * This function must never be called before contract_init(), as
8257c478bd9Sstevel@tonic-gate * it sets st_initial. get_startd_config() sets prop_reconfig from
8267c478bd9Sstevel@tonic-gate * pre-existing repository state.
8277c478bd9Sstevel@tonic-gate */
8287c478bd9Sstevel@tonic-gate static void
set_boot_env()8297c478bd9Sstevel@tonic-gate set_boot_env()
8307c478bd9Sstevel@tonic-gate {
8317c478bd9Sstevel@tonic-gate struct stat sb;
8327c478bd9Sstevel@tonic-gate int r;
8337c478bd9Sstevel@tonic-gate
8347c478bd9Sstevel@tonic-gate /*
8357c478bd9Sstevel@tonic-gate * Check if property still is set -- indicates we didn't get
8367c478bd9Sstevel@tonic-gate * far enough previously to unset it. Otherwise, if this isn't
8377c478bd9Sstevel@tonic-gate * the first startup, don't re-process /reconfigure or the
8387c478bd9Sstevel@tonic-gate * boot flag.
8397c478bd9Sstevel@tonic-gate */
8407c478bd9Sstevel@tonic-gate if (prop_reconfig != 1 && st->st_initial != 1)
8417c478bd9Sstevel@tonic-gate return;
8427c478bd9Sstevel@tonic-gate
8437c478bd9Sstevel@tonic-gate /* If /reconfigure exists, also set opt_reconfig. */
8447c478bd9Sstevel@tonic-gate if (stat("/reconfigure", &sb) != -1)
8457c478bd9Sstevel@tonic-gate opt_reconfig = 1;
8467c478bd9Sstevel@tonic-gate
8477c478bd9Sstevel@tonic-gate /* Nothing to do. Just return. */
8487c478bd9Sstevel@tonic-gate if (opt_reconfig == 0 && prop_reconfig == 0)
8497c478bd9Sstevel@tonic-gate return;
8507c478bd9Sstevel@tonic-gate
8517c478bd9Sstevel@tonic-gate /*
8527c478bd9Sstevel@tonic-gate * Set startd's reconfigure property. This property is
8537c478bd9Sstevel@tonic-gate * then cleared by successful completion of the single-user
8547c478bd9Sstevel@tonic-gate * milestone.
8557c478bd9Sstevel@tonic-gate */
8567c478bd9Sstevel@tonic-gate if (prop_reconfig != 1) {
8577c478bd9Sstevel@tonic-gate r = libscf_set_reconfig(1);
8587c478bd9Sstevel@tonic-gate switch (r) {
8597c478bd9Sstevel@tonic-gate case 0:
8607c478bd9Sstevel@tonic-gate break;
8617c478bd9Sstevel@tonic-gate
8627c478bd9Sstevel@tonic-gate case ENOENT:
8637c478bd9Sstevel@tonic-gate case EPERM:
8647c478bd9Sstevel@tonic-gate case EACCES:
8657c478bd9Sstevel@tonic-gate case EROFS:
8667c478bd9Sstevel@tonic-gate log_error(LOG_WARNING, "Could not set reconfiguration "
8677c478bd9Sstevel@tonic-gate "property: %s\n", strerror(r));
8687c478bd9Sstevel@tonic-gate break;
8697c478bd9Sstevel@tonic-gate
8707c478bd9Sstevel@tonic-gate default:
8717c478bd9Sstevel@tonic-gate bad_error("libscf_set_reconfig", r);
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate
8767c478bd9Sstevel@tonic-gate static void
startup(void)87799b44c3bSlianep startup(void)
8787c478bd9Sstevel@tonic-gate {
8797c478bd9Sstevel@tonic-gate ctid_t configd_ctid;
8807c478bd9Sstevel@tonic-gate int err;
8817c478bd9Sstevel@tonic-gate
8827c478bd9Sstevel@tonic-gate /*
8837c478bd9Sstevel@tonic-gate * Initialize data structures.
8847c478bd9Sstevel@tonic-gate */
8857c478bd9Sstevel@tonic-gate gu = startd_zalloc(sizeof (graph_update_t));
8867c478bd9Sstevel@tonic-gate ru = startd_zalloc(sizeof (restarter_update_t));
8877c478bd9Sstevel@tonic-gate
8887c478bd9Sstevel@tonic-gate (void) pthread_cond_init(&st->st_load_cv, NULL);
8897c478bd9Sstevel@tonic-gate (void) pthread_cond_init(&st->st_configd_live_cv, NULL);
8907c478bd9Sstevel@tonic-gate (void) pthread_cond_init(&gu->gu_cv, NULL);
8917c478bd9Sstevel@tonic-gate (void) pthread_cond_init(&gu->gu_freeze_cv, NULL);
8927c478bd9Sstevel@tonic-gate (void) pthread_cond_init(&ru->restarter_update_cv, NULL);
8937c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&st->st_load_lock, &mutex_attrs);
8947c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&st->st_configd_live_lock, &mutex_attrs);
8957c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&gu->gu_lock, &mutex_attrs);
8967c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&gu->gu_freeze_lock, &mutex_attrs);
8977c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&ru->restarter_update_lock, &mutex_attrs);
8987c478bd9Sstevel@tonic-gate
8997c478bd9Sstevel@tonic-gate configd_ctid = contract_init();
9007c478bd9Sstevel@tonic-gate
9017c478bd9Sstevel@tonic-gate if (configd_ctid != -1)
9027c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "Existing configd contract %ld; not "
9037c478bd9Sstevel@tonic-gate "starting svc.configd\n", configd_ctid);
9047c478bd9Sstevel@tonic-gate
9050d421f66SBryan Cantrill /*
9060d421f66SBryan Cantrill * Call utmpx_init() before creating the fork_configd() thread.
9070d421f66SBryan Cantrill */
9080d421f66SBryan Cantrill utmpx_init();
9090d421f66SBryan Cantrill
9107c478bd9Sstevel@tonic-gate (void) startd_thread_create(fork_configd_thread, (void *)configd_ctid);
9117c478bd9Sstevel@tonic-gate
9127c478bd9Sstevel@tonic-gate /*
9137c478bd9Sstevel@tonic-gate * Await, if necessary, configd's initial arrival.
9147c478bd9Sstevel@tonic-gate */
9157c478bd9Sstevel@tonic-gate MUTEX_LOCK(&st->st_configd_live_lock);
9167c478bd9Sstevel@tonic-gate while (!st->st_configd_lives) {
9177c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "Awaiting cv signal on "
9187c478bd9Sstevel@tonic-gate "configd_live_cv\n");
9197c478bd9Sstevel@tonic-gate err = pthread_cond_wait(&st->st_configd_live_cv,
9207c478bd9Sstevel@tonic-gate &st->st_configd_live_lock);
9217c478bd9Sstevel@tonic-gate assert(err == 0);
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate MUTEX_UNLOCK(&st->st_configd_live_lock);
9247c478bd9Sstevel@tonic-gate
9257c478bd9Sstevel@tonic-gate wait_init();
9267c478bd9Sstevel@tonic-gate
92799b44c3bSlianep if (read_startd_config())
9287c478bd9Sstevel@tonic-gate log_framework(LOG_INFO, "svc.configd unable to provide startd "
9297c478bd9Sstevel@tonic-gate "optional settings\n");
9307c478bd9Sstevel@tonic-gate
9317c478bd9Sstevel@tonic-gate log_init();
9327c478bd9Sstevel@tonic-gate dict_init();
9337c478bd9Sstevel@tonic-gate timeout_init();
9347c478bd9Sstevel@tonic-gate restarter_protocol_init();
9357c478bd9Sstevel@tonic-gate restarter_init();
9369444c26fSTom Whitten
9379444c26fSTom Whitten /*
9389444c26fSTom Whitten * svc.configd is started by fork_configd_thread so repository access is
9399444c26fSTom Whitten * available, run early manifest import before continuing with starting
9409444c26fSTom Whitten * graph engine and the rest of startd.
9419444c26fSTom Whitten */
9429444c26fSTom Whitten log_framework(LOG_DEBUG, "Calling fork_emi...\n");
9439444c26fSTom Whitten fork_emi();
9449444c26fSTom Whitten
9457c478bd9Sstevel@tonic-gate graph_protocol_init();
9467c478bd9Sstevel@tonic-gate graph_init();
9477c478bd9Sstevel@tonic-gate
9487c478bd9Sstevel@tonic-gate init_env();
9497c478bd9Sstevel@tonic-gate
9507c478bd9Sstevel@tonic-gate set_boot_env();
9517c478bd9Sstevel@tonic-gate restarter_start();
9527c478bd9Sstevel@tonic-gate graph_engine_start();
9537c478bd9Sstevel@tonic-gate }
9547c478bd9Sstevel@tonic-gate
9557c478bd9Sstevel@tonic-gate static void
usage(const char * name)9567c478bd9Sstevel@tonic-gate usage(const char *name)
9577c478bd9Sstevel@tonic-gate {
95854d6518cSrm88369 uu_warn(gettext("usage: %s [-n]\n"), name);
9597c478bd9Sstevel@tonic-gate exit(UU_EXIT_USAGE);
9607c478bd9Sstevel@tonic-gate }
9617c478bd9Sstevel@tonic-gate
9627c478bd9Sstevel@tonic-gate static int
daemonize_start(void)9637c478bd9Sstevel@tonic-gate daemonize_start(void)
9647c478bd9Sstevel@tonic-gate {
9657c478bd9Sstevel@tonic-gate pid_t pid;
9667c478bd9Sstevel@tonic-gate int fd;
9677c478bd9Sstevel@tonic-gate
9687c478bd9Sstevel@tonic-gate if ((pid = fork1()) < 0)
9697c478bd9Sstevel@tonic-gate return (-1);
9707c478bd9Sstevel@tonic-gate
9717c478bd9Sstevel@tonic-gate if (pid != 0)
9727c478bd9Sstevel@tonic-gate exit(0);
9737c478bd9Sstevel@tonic-gate
9742f1b831aSacruz (void) close(STDIN_FILENO);
9757c478bd9Sstevel@tonic-gate
9767c478bd9Sstevel@tonic-gate if ((fd = open("/dev/null", O_RDONLY)) == -1) {
9777c478bd9Sstevel@tonic-gate uu_warn(gettext("can't connect stdin to /dev/null"));
9782f1b831aSacruz } else if (fd != STDIN_FILENO) {
9792f1b831aSacruz (void) dup2(fd, STDIN_FILENO);
9807c478bd9Sstevel@tonic-gate startd_close(fd);
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate
9837c478bd9Sstevel@tonic-gate closefrom(3);
9842f1b831aSacruz (void) dup2(STDERR_FILENO, STDOUT_FILENO);
9857c478bd9Sstevel@tonic-gate
9867c478bd9Sstevel@tonic-gate (void) setsid();
9877c478bd9Sstevel@tonic-gate (void) chdir("/");
9887c478bd9Sstevel@tonic-gate
9897c478bd9Sstevel@tonic-gate /* Use default umask that init handed us, but 022 to create files. */
9907c478bd9Sstevel@tonic-gate dmask = umask(022);
9917c478bd9Sstevel@tonic-gate fmask = umask(dmask);
9927c478bd9Sstevel@tonic-gate
9937c478bd9Sstevel@tonic-gate return (0);
9947c478bd9Sstevel@tonic-gate }
9957c478bd9Sstevel@tonic-gate
9967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9977c478bd9Sstevel@tonic-gate static void
die_handler(int sig,siginfo_t * info,void * data)9987c478bd9Sstevel@tonic-gate die_handler(int sig, siginfo_t *info, void *data)
9997c478bd9Sstevel@tonic-gate {
10007c478bd9Sstevel@tonic-gate finished = 1;
10017c478bd9Sstevel@tonic-gate }
10027c478bd9Sstevel@tonic-gate
10037c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])10047c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
10057c478bd9Sstevel@tonic-gate {
10067c478bd9Sstevel@tonic-gate int opt;
10077c478bd9Sstevel@tonic-gate int daemonize = 1;
10087c478bd9Sstevel@tonic-gate struct sigaction act;
10097c478bd9Sstevel@tonic-gate sigset_t nullset;
10107c478bd9Sstevel@tonic-gate struct stat sb;
10117c478bd9Sstevel@tonic-gate
10127c478bd9Sstevel@tonic-gate (void) uu_setpname(argv[0]);
10137c478bd9Sstevel@tonic-gate
10147c478bd9Sstevel@tonic-gate st = startd_zalloc(sizeof (startd_state_t));
10157c478bd9Sstevel@tonic-gate
10167c478bd9Sstevel@tonic-gate (void) pthread_mutexattr_init(&mutex_attrs);
10177c478bd9Sstevel@tonic-gate #ifndef NDEBUG
10187c478bd9Sstevel@tonic-gate (void) pthread_mutexattr_settype(&mutex_attrs,
10197c478bd9Sstevel@tonic-gate PTHREAD_MUTEX_ERRORCHECK);
10207c478bd9Sstevel@tonic-gate #endif
10217c478bd9Sstevel@tonic-gate
10227c478bd9Sstevel@tonic-gate max_scf_name_size = scf_limit(SCF_LIMIT_MAX_NAME_LENGTH);
10237c478bd9Sstevel@tonic-gate max_scf_value_size = scf_limit(SCF_LIMIT_MAX_VALUE_LENGTH);
10247c478bd9Sstevel@tonic-gate max_scf_fmri_size = scf_limit(SCF_LIMIT_MAX_FMRI_LENGTH);
10257c478bd9Sstevel@tonic-gate
10267c478bd9Sstevel@tonic-gate if (max_scf_name_size == -1 || max_scf_value_size == -1 ||
10277c478bd9Sstevel@tonic-gate max_scf_value_size == -1)
10287c478bd9Sstevel@tonic-gate uu_die("Can't determine repository maximum lengths.\n");
10297c478bd9Sstevel@tonic-gate
10307c478bd9Sstevel@tonic-gate max_scf_name_size++;
10317c478bd9Sstevel@tonic-gate max_scf_value_size++;
10327c478bd9Sstevel@tonic-gate max_scf_fmri_size++;
10337c478bd9Sstevel@tonic-gate
103499b44c3bSlianep st->st_log_flags = STARTD_LOG_FILE | STARTD_LOG_SYSLOG;
103599b44c3bSlianep st->st_log_level_min = LOG_NOTICE;
10367c478bd9Sstevel@tonic-gate
103799b44c3bSlianep while ((opt = getopt(argc, argv, "nrs")) != EOF) {
10387c478bd9Sstevel@tonic-gate switch (opt) {
10397c478bd9Sstevel@tonic-gate case 'n':
10407c478bd9Sstevel@tonic-gate daemonize = 0;
10417c478bd9Sstevel@tonic-gate break;
10427c478bd9Sstevel@tonic-gate case 'r': /* reconfiguration boot */
10437c478bd9Sstevel@tonic-gate opt_reconfig = 1;
10447c478bd9Sstevel@tonic-gate break;
10457c478bd9Sstevel@tonic-gate case 's': /* single-user mode */
10467c478bd9Sstevel@tonic-gate booting_to_single_user = B_TRUE;
10477c478bd9Sstevel@tonic-gate break;
10487c478bd9Sstevel@tonic-gate default:
10497c478bd9Sstevel@tonic-gate usage(argv[0]); /* exits */
10507c478bd9Sstevel@tonic-gate }
10517c478bd9Sstevel@tonic-gate }
10527c478bd9Sstevel@tonic-gate
10537c478bd9Sstevel@tonic-gate if (optind != argc)
10547c478bd9Sstevel@tonic-gate usage(argv[0]);
10557c478bd9Sstevel@tonic-gate
1056004388ebScasper (void) enable_extended_FILE_stdio(-1, -1);
1057004388ebScasper
10587c478bd9Sstevel@tonic-gate if (daemonize)
10597c478bd9Sstevel@tonic-gate if (daemonize_start() < 0)
10607c478bd9Sstevel@tonic-gate uu_die("Can't daemonize\n");
10617c478bd9Sstevel@tonic-gate
10627c478bd9Sstevel@tonic-gate log_init();
10637c478bd9Sstevel@tonic-gate
10647c478bd9Sstevel@tonic-gate if (stat("/etc/svc/volatile/resetting", &sb) != -1) {
10657c478bd9Sstevel@tonic-gate log_framework(LOG_NOTICE, "Restarter quiesced.\n");
10667c478bd9Sstevel@tonic-gate
10677c478bd9Sstevel@tonic-gate for (;;)
10687c478bd9Sstevel@tonic-gate (void) pause();
10697c478bd9Sstevel@tonic-gate }
10707c478bd9Sstevel@tonic-gate
10717c478bd9Sstevel@tonic-gate act.sa_sigaction = &die_handler;
10727c478bd9Sstevel@tonic-gate (void) sigfillset(&act.sa_mask);
10737c478bd9Sstevel@tonic-gate act.sa_flags = SA_SIGINFO;
10747c478bd9Sstevel@tonic-gate (void) sigaction(SIGINT, &act, NULL);
10757c478bd9Sstevel@tonic-gate (void) sigaction(SIGTERM, &act, NULL);
10767c478bd9Sstevel@tonic-gate
107799b44c3bSlianep startup();
10787c478bd9Sstevel@tonic-gate
10797c478bd9Sstevel@tonic-gate (void) sigemptyset(&nullset);
10807c478bd9Sstevel@tonic-gate while (!finished) {
10817c478bd9Sstevel@tonic-gate log_framework(LOG_DEBUG, "Main thread paused\n");
10827c478bd9Sstevel@tonic-gate (void) sigsuspend(&nullset);
10837c478bd9Sstevel@tonic-gate }
10847c478bd9Sstevel@tonic-gate
10857c478bd9Sstevel@tonic-gate (void) log_framework(LOG_DEBUG, "Restarter exiting.\n");
10867c478bd9Sstevel@tonic-gate return (0);
10877c478bd9Sstevel@tonic-gate }
1088