1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * graph.c - master restarter graph engine
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  *   The graph engine keeps a dependency graph of all service instances on the
33*0Sstevel@tonic-gate  *   system, as recorded in the repository.  It decides when services should
34*0Sstevel@tonic-gate  *   be brought up or down based on service states and dependencies and sends
35*0Sstevel@tonic-gate  *   commands to restarters to effect any changes.  It also executes
36*0Sstevel@tonic-gate  *   administrator commands sent by svcadm via the repository.
37*0Sstevel@tonic-gate  *
38*0Sstevel@tonic-gate  *   The graph is stored in uu_list_t *dgraph and its vertices are
39*0Sstevel@tonic-gate  *   graph_vertex_t's, each of which has a name and an integer id unique to
40*0Sstevel@tonic-gate  *   its name (see dict.c).  A vertex's type attribute designates the type
41*0Sstevel@tonic-gate  *   of object it represents: GVT_INST for service instances, GVT_SVC for
42*0Sstevel@tonic-gate  *   service objects (since service instances may depend on another service,
43*0Sstevel@tonic-gate  *   rather than service instance), GVT_FILE for files (which services may
44*0Sstevel@tonic-gate  *   depend on), and GVT_GROUP for dependencies on multiple objects.  GVT_GROUP
45*0Sstevel@tonic-gate  *   vertices are necessary because dependency lists may have particular
46*0Sstevel@tonic-gate  *   grouping types (require any, require all, optional, or exclude) and
47*0Sstevel@tonic-gate  *   event-propagation characteristics.
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  *   The initial graph is built by libscf_populate_graph() invoking
50*0Sstevel@tonic-gate  *   dgraph_add_instance() for each instance in the repository.  The function
51*0Sstevel@tonic-gate  *   adds a GVT_SVC vertex for the service if one does not already exist, adds
52*0Sstevel@tonic-gate  *   a GVT_INST vertex named by the FMRI of the instance, and sets up the edges.
53*0Sstevel@tonic-gate  *   The resulting web of vertices & edges associated with an instance's vertex
54*0Sstevel@tonic-gate  *   includes
55*0Sstevel@tonic-gate  *
56*0Sstevel@tonic-gate  *     - an edge from the GVT_SVC vertex for the instance's service
57*0Sstevel@tonic-gate  *
58*0Sstevel@tonic-gate  *     - an edge to the GVT_INST vertex of the instance's resarter, if its
59*0Sstevel@tonic-gate  *       restarter is not svc.startd
60*0Sstevel@tonic-gate  *
61*0Sstevel@tonic-gate  *     - edges from other GVT_INST vertices if the instance is a restarter
62*0Sstevel@tonic-gate  *
63*0Sstevel@tonic-gate  *     - for each dependency property group in the instance's "running"
64*0Sstevel@tonic-gate  *       snapshot, an edge to a GVT_GROUP vertex named by the FMRI of the
65*0Sstevel@tonic-gate  *       instance and the name of the property group
66*0Sstevel@tonic-gate  *
67*0Sstevel@tonic-gate  *     - for each value of the "entities" property in each dependency property
68*0Sstevel@tonic-gate  *       group, an edge from the corresponding GVT_GROUP vertex to a
69*0Sstevel@tonic-gate  *       GVT_INST, GVT_SVC, or GVT_FILE vertex
70*0Sstevel@tonic-gate  *
71*0Sstevel@tonic-gate  *     - edges from GVT_GROUP vertices for each dependent instance
72*0Sstevel@tonic-gate  *
73*0Sstevel@tonic-gate  *   After the edges are set up the vertex's GV_CONFIGURED flag is set.  If
74*0Sstevel@tonic-gate  *   there are problems, or if a service is mentioned in a dependency but does
75*0Sstevel@tonic-gate  *   not exist in the repository, the GV_CONFIGURED flag will be clear.
76*0Sstevel@tonic-gate  *
77*0Sstevel@tonic-gate  *   The graph and all of its vertices are protected by the dgraph_lock mutex.
78*0Sstevel@tonic-gate  *   See restarter.c for more information.
79*0Sstevel@tonic-gate  *
80*0Sstevel@tonic-gate  *   The properties of an instance fall into two classes: immediate and
81*0Sstevel@tonic-gate  *   snapshotted.  Immediate properties should have an immediate effect when
82*0Sstevel@tonic-gate  *   changed.  Snapshotted properties should be read from a snapshot, so they
83*0Sstevel@tonic-gate  *   only change when the snapshot changes.  The immediate properties used by
84*0Sstevel@tonic-gate  *   the graph engine are general/enabled, general/restarter, and the properties
85*0Sstevel@tonic-gate  *   in the restarter_actions property group.  Since they are immediate, they
86*0Sstevel@tonic-gate  *   are not read out of a snapshot.  The snapshotted properties used by the
87*0Sstevel@tonic-gate  *   graph engine are those in the property groups with type "dependency" and
88*0Sstevel@tonic-gate  *   are read out of the "running" snapshot.  The "running" snapshot is created
89*0Sstevel@tonic-gate  *   by the the graph engine as soon as possible, and it is updated, along with
90*0Sstevel@tonic-gate  *   in-core copies of the data (dependency information for the graph engine) on
91*0Sstevel@tonic-gate  *   receipt of the refresh command from svcadm.  In addition, the graph engine
92*0Sstevel@tonic-gate  *   updates the "start" snapshot from the "running" snapshot whenever a service
93*0Sstevel@tonic-gate  *   comes online.
94*0Sstevel@tonic-gate  */
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate #include <sys/uadmin.h>
97*0Sstevel@tonic-gate #include <sys/wait.h>
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate #include <assert.h>
100*0Sstevel@tonic-gate #include <errno.h>
101*0Sstevel@tonic-gate #include <fcntl.h>
102*0Sstevel@tonic-gate #include <libscf.h>
103*0Sstevel@tonic-gate #include <libscf_priv.h>
104*0Sstevel@tonic-gate #include <libuutil.h>
105*0Sstevel@tonic-gate #include <locale.h>
106*0Sstevel@tonic-gate #include <poll.h>
107*0Sstevel@tonic-gate #include <pthread.h>
108*0Sstevel@tonic-gate #include <signal.h>
109*0Sstevel@tonic-gate #include <stddef.h>
110*0Sstevel@tonic-gate #include <stdio.h>
111*0Sstevel@tonic-gate #include <stdlib.h>
112*0Sstevel@tonic-gate #include <string.h>
113*0Sstevel@tonic-gate #include <strings.h>
114*0Sstevel@tonic-gate #include <sys/statvfs.h>
115*0Sstevel@tonic-gate #include <sys/uadmin.h>
116*0Sstevel@tonic-gate #include <zone.h>
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate #include "startd.h"
119*0Sstevel@tonic-gate #include "protocol.h"
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate #define	MILESTONE_NONE	((graph_vertex_t *)1)
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate #define	CONSOLE_LOGIN_FMRI	"svc:/system/console-login:default"
125*0Sstevel@tonic-gate #define	FS_MINIMAL_FMRI		"svc:/system/filesystem/minimal:default"
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate static uu_list_pool_t *graph_edge_pool, *graph_vertex_pool;
128*0Sstevel@tonic-gate static uu_list_t *dgraph;
129*0Sstevel@tonic-gate static pthread_mutex_t dgraph_lock;
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate /*
132*0Sstevel@tonic-gate  * milestone indicates the current subgraph.  When NULL, it is the entire
133*0Sstevel@tonic-gate  * graph.  When MILESTONE_NONE, it is the empty graph.  Otherwise, it is all
134*0Sstevel@tonic-gate  * services on which the target vertex depends.
135*0Sstevel@tonic-gate  */
136*0Sstevel@tonic-gate static graph_vertex_t *milestone = NULL;
137*0Sstevel@tonic-gate static boolean_t initial_milestone_set = B_FALSE;
138*0Sstevel@tonic-gate static pthread_cond_t initial_milestone_cv = PTHREAD_COND_INITIALIZER;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate /* protected by dgraph_lock */
141*0Sstevel@tonic-gate static boolean_t sulogin_thread_running = B_FALSE;
142*0Sstevel@tonic-gate static boolean_t sulogin_running = B_FALSE;
143*0Sstevel@tonic-gate static boolean_t console_login_ready = B_FALSE;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate /* Number of services to come down to complete milestone transition. */
146*0Sstevel@tonic-gate static uint_t non_subgraph_svcs;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate /*
149*0Sstevel@tonic-gate  * These variables indicate what should be done when we reach the milestone
150*0Sstevel@tonic-gate  * target milestone, i.e., when non_subgraph_svcs == 0.  They are acted upon in
151*0Sstevel@tonic-gate  * dgraph_set_instance_state().
152*0Sstevel@tonic-gate  */
153*0Sstevel@tonic-gate static int halting = -1;
154*0Sstevel@tonic-gate static boolean_t go_single_user_mode = B_FALSE;
155*0Sstevel@tonic-gate static boolean_t go_to_level1 = B_FALSE;
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate  * This tracks the legacy runlevel to ensure we signal init and manage
159*0Sstevel@tonic-gate  * utmpx entries correctly.
160*0Sstevel@tonic-gate  */
161*0Sstevel@tonic-gate static char current_runlevel = '\0';
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate /* Number of single user threads currently running */
164*0Sstevel@tonic-gate static pthread_mutex_t single_user_thread_lock;
165*0Sstevel@tonic-gate static int single_user_thread_count = 0;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate /* Statistics for dependency cycle-checking */
168*0Sstevel@tonic-gate static u_longlong_t dep_inserts = 0;
169*0Sstevel@tonic-gate static u_longlong_t dep_cycle_ns = 0;
170*0Sstevel@tonic-gate static u_longlong_t dep_insert_ns = 0;
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate static const char * const emsg_invalid_restarter =
174*0Sstevel@tonic-gate 	"Restarter FMRI for %s is invalid.  Transitioning to maintenance.\n";
175*0Sstevel@tonic-gate static const char * const console_login_fmri = CONSOLE_LOGIN_FMRI;
176*0Sstevel@tonic-gate static const char * const single_user_fmri = SCF_MILESTONE_SINGLE_USER;
177*0Sstevel@tonic-gate static const char * const multi_user_fmri = SCF_MILESTONE_MULTI_USER;
178*0Sstevel@tonic-gate static const char * const multi_user_svr_fmri = SCF_MILESTONE_MULTI_USER_SERVER;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate /*
182*0Sstevel@tonic-gate  * These services define the system being "up".  If none of them can come
183*0Sstevel@tonic-gate  * online, then we will run sulogin on the console.  Note that the install ones
184*0Sstevel@tonic-gate  * are for the miniroot and when installing CDs after the first.  can_come_up()
185*0Sstevel@tonic-gate  * does the decision making, and an sulogin_thread() runs sulogin, which can be
186*0Sstevel@tonic-gate  * started by dgraph_set_instance_state() or single_user_thread().
187*0Sstevel@tonic-gate  *
188*0Sstevel@tonic-gate  * NOTE: can_come_up() relies on SCF_MILESTONE_SINGLE_USER being the first
189*0Sstevel@tonic-gate  * entry, which is only used when booting_to_single_user (boot -s) is set.
190*0Sstevel@tonic-gate  * This is because when doing a "boot -s", sulogin is started from specials.c
191*0Sstevel@tonic-gate  * after milestone/single-user comes online, for backwards compatibility.
192*0Sstevel@tonic-gate  * In this case, SCF_MILESTONE_SINGLE_USER needs to be part of up_svcs
193*0Sstevel@tonic-gate  * to ensure sulogin will be spawned if milestone/single-user cannot be reached.
194*0Sstevel@tonic-gate  */
195*0Sstevel@tonic-gate static const char * const up_svcs[] = {
196*0Sstevel@tonic-gate 	SCF_MILESTONE_SINGLE_USER,
197*0Sstevel@tonic-gate 	CONSOLE_LOGIN_FMRI,
198*0Sstevel@tonic-gate 	"svc:/system/install-setup:default",
199*0Sstevel@tonic-gate 	"svc:/system/install:default",
200*0Sstevel@tonic-gate 	NULL
201*0Sstevel@tonic-gate };
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate /* This array must have an element for each non-NULL element of up_svcs[]. */
204*0Sstevel@tonic-gate static graph_vertex_t *up_svcs_p[] = { NULL, NULL, NULL, NULL };
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate /* These are for seed repository magic.  See can_come_up(). */
207*0Sstevel@tonic-gate static const char * const manifest_import =
208*0Sstevel@tonic-gate 	"svc:/system/manifest-import:default";
209*0Sstevel@tonic-gate static graph_vertex_t *manifest_import_p = NULL;
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate static char target_milestone_as_runlevel(void);
213*0Sstevel@tonic-gate static void graph_runlevel_changed(char rl, int online);
214*0Sstevel@tonic-gate static int dgraph_set_milestone(const char *, scf_handle_t *, boolean_t);
215*0Sstevel@tonic-gate static void vertex_send_event(graph_vertex_t *v, restarter_event_type_t e);
216*0Sstevel@tonic-gate static boolean_t should_be_in_subgraph(graph_vertex_t *v);
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate /*
219*0Sstevel@tonic-gate  * graph_vertex_compare()
220*0Sstevel@tonic-gate  *	This function can compare either int *id or * graph_vertex_t *gv
221*0Sstevel@tonic-gate  *	values, as the vertex id is always the first element of a
222*0Sstevel@tonic-gate  *	graph_vertex structure.
223*0Sstevel@tonic-gate  */
224*0Sstevel@tonic-gate /* ARGSUSED */
225*0Sstevel@tonic-gate static int
226*0Sstevel@tonic-gate graph_vertex_compare(const void *lc_arg, const void *rc_arg, void *private)
227*0Sstevel@tonic-gate {
228*0Sstevel@tonic-gate 	int lc_id = ((const graph_vertex_t *)lc_arg)->gv_id;
229*0Sstevel@tonic-gate 	int rc_id = *(int *)rc_arg;
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	if (lc_id > rc_id)
232*0Sstevel@tonic-gate 		return (1);
233*0Sstevel@tonic-gate 	if (lc_id < rc_id)
234*0Sstevel@tonic-gate 		return (-1);
235*0Sstevel@tonic-gate 	return (0);
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate void
239*0Sstevel@tonic-gate graph_init()
240*0Sstevel@tonic-gate {
241*0Sstevel@tonic-gate 	graph_edge_pool = startd_list_pool_create("graph_edges",
242*0Sstevel@tonic-gate 	    sizeof (graph_edge_t), offsetof(graph_edge_t, ge_link), NULL,
243*0Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG);
244*0Sstevel@tonic-gate 	assert(graph_edge_pool != NULL);
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	graph_vertex_pool = startd_list_pool_create("graph_vertices",
247*0Sstevel@tonic-gate 	    sizeof (graph_vertex_t), offsetof(graph_vertex_t, gv_link),
248*0Sstevel@tonic-gate 	    graph_vertex_compare, UU_LIST_POOL_DEBUG);
249*0Sstevel@tonic-gate 	assert(graph_vertex_pool != NULL);
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	(void) pthread_mutex_init(&dgraph_lock, &mutex_attrs);
252*0Sstevel@tonic-gate 	(void) pthread_mutex_init(&single_user_thread_lock, &mutex_attrs);
253*0Sstevel@tonic-gate 	dgraph = startd_list_create(graph_vertex_pool, NULL, UU_LIST_SORTED);
254*0Sstevel@tonic-gate 	assert(dgraph != NULL);
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	if (!st->st_initial)
257*0Sstevel@tonic-gate 		current_runlevel = utmpx_get_runlevel();
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Initialized graph\n");
260*0Sstevel@tonic-gate }
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate static graph_vertex_t *
263*0Sstevel@tonic-gate vertex_get_by_name(const char *name)
264*0Sstevel@tonic-gate {
265*0Sstevel@tonic-gate 	int id;
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	id = dict_lookup_byname(name);
270*0Sstevel@tonic-gate 	if (id == -1)
271*0Sstevel@tonic-gate 		return (NULL);
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	return (uu_list_find(dgraph, &id, NULL, NULL));
274*0Sstevel@tonic-gate }
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate static graph_vertex_t *
277*0Sstevel@tonic-gate vertex_get_by_id(int id)
278*0Sstevel@tonic-gate {
279*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	if (id == -1)
282*0Sstevel@tonic-gate 		return (NULL);
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	return (uu_list_find(dgraph, &id, NULL, NULL));
285*0Sstevel@tonic-gate }
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate /*
288*0Sstevel@tonic-gate  * Creates a new vertex with the given name, adds it to the graph, and returns
289*0Sstevel@tonic-gate  * a pointer to it.  The graph lock must be held by this thread on entry.
290*0Sstevel@tonic-gate  */
291*0Sstevel@tonic-gate static graph_vertex_t *
292*0Sstevel@tonic-gate graph_add_vertex(const char *name)
293*0Sstevel@tonic-gate {
294*0Sstevel@tonic-gate 	int id;
295*0Sstevel@tonic-gate 	graph_vertex_t *v;
296*0Sstevel@tonic-gate 	void *p;
297*0Sstevel@tonic-gate 	uu_list_index_t idx;
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	id = dict_insert(name);
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	v = startd_zalloc(sizeof (*v));
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 	v->gv_id = id;
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	v->gv_name = startd_alloc(strlen(name) + 1);
308*0Sstevel@tonic-gate 	(void) strcpy(v->gv_name, name);
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 	v->gv_dependencies = startd_list_create(graph_edge_pool, v, 0);
311*0Sstevel@tonic-gate 	v->gv_dependents = startd_list_create(graph_edge_pool, v, 0);
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 	p = uu_list_find(dgraph, &id, NULL, &idx);
314*0Sstevel@tonic-gate 	assert(p == NULL);
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 	uu_list_node_init(v, &v->gv_link, graph_vertex_pool);
317*0Sstevel@tonic-gate 	uu_list_insert(dgraph, v, idx);
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	return (v);
320*0Sstevel@tonic-gate }
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate /*
323*0Sstevel@tonic-gate  * Removes v from the graph and frees it.  The graph should be locked by this
324*0Sstevel@tonic-gate  * thread, and v should have no edges associated with it.
325*0Sstevel@tonic-gate  */
326*0Sstevel@tonic-gate static void
327*0Sstevel@tonic-gate graph_remove_vertex(graph_vertex_t *v)
328*0Sstevel@tonic-gate {
329*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependencies) == 0);
332*0Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependents) == 0);
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 	startd_free(v->gv_name, strlen(v->gv_name) + 1);
335*0Sstevel@tonic-gate 	uu_list_destroy(v->gv_dependencies);
336*0Sstevel@tonic-gate 	uu_list_destroy(v->gv_dependents);
337*0Sstevel@tonic-gate 	uu_list_remove(dgraph, v);
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 	startd_free(v, sizeof (graph_vertex_t));
340*0Sstevel@tonic-gate }
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate static void
343*0Sstevel@tonic-gate graph_add_edge(graph_vertex_t *fv, graph_vertex_t *tv)
344*0Sstevel@tonic-gate {
345*0Sstevel@tonic-gate 	graph_edge_t *e, *re;
346*0Sstevel@tonic-gate 	int r;
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 	e = startd_alloc(sizeof (graph_edge_t));
351*0Sstevel@tonic-gate 	re = startd_alloc(sizeof (graph_edge_t));
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate 	e->ge_parent = fv;
354*0Sstevel@tonic-gate 	e->ge_vertex = tv;
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 	re->ge_parent = tv;
357*0Sstevel@tonic-gate 	re->ge_vertex = fv;
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	uu_list_node_init(e, &e->ge_link, graph_edge_pool);
360*0Sstevel@tonic-gate 	r = uu_list_insert_before(fv->gv_dependencies, NULL, e);
361*0Sstevel@tonic-gate 	assert(r == 0);
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 	uu_list_node_init(re, &re->ge_link, graph_edge_pool);
364*0Sstevel@tonic-gate 	r = uu_list_insert_before(tv->gv_dependents, NULL, re);
365*0Sstevel@tonic-gate 	assert(r == 0);
366*0Sstevel@tonic-gate }
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate static void
369*0Sstevel@tonic-gate graph_remove_edge(graph_vertex_t *v, graph_vertex_t *dv)
370*0Sstevel@tonic-gate {
371*0Sstevel@tonic-gate 	graph_edge_t *e;
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
374*0Sstevel@tonic-gate 	    e != NULL;
375*0Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
376*0Sstevel@tonic-gate 		if (e->ge_vertex == dv) {
377*0Sstevel@tonic-gate 			uu_list_remove(v->gv_dependencies, e);
378*0Sstevel@tonic-gate 			startd_free(e, sizeof (graph_edge_t));
379*0Sstevel@tonic-gate 			break;
380*0Sstevel@tonic-gate 		}
381*0Sstevel@tonic-gate 	}
382*0Sstevel@tonic-gate 
383*0Sstevel@tonic-gate 	for (e = uu_list_first(dv->gv_dependents);
384*0Sstevel@tonic-gate 	    e != NULL;
385*0Sstevel@tonic-gate 	    e = uu_list_next(dv->gv_dependents, e)) {
386*0Sstevel@tonic-gate 		if (e->ge_vertex == v) {
387*0Sstevel@tonic-gate 			uu_list_remove(dv->gv_dependents, e);
388*0Sstevel@tonic-gate 			startd_free(e, sizeof (graph_edge_t));
389*0Sstevel@tonic-gate 			break;
390*0Sstevel@tonic-gate 		}
391*0Sstevel@tonic-gate 	}
392*0Sstevel@tonic-gate }
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate static void
395*0Sstevel@tonic-gate graph_walk_dependents(graph_vertex_t *v, void (*func)(graph_vertex_t *, void *),
396*0Sstevel@tonic-gate     void *arg)
397*0Sstevel@tonic-gate {
398*0Sstevel@tonic-gate 	graph_edge_t *e;
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
401*0Sstevel@tonic-gate 	    e != NULL;
402*0Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e))
403*0Sstevel@tonic-gate 		func(e->ge_vertex, arg);
404*0Sstevel@tonic-gate }
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate static void
407*0Sstevel@tonic-gate graph_walk_dependencies(graph_vertex_t *v, void (*func)(graph_vertex_t *,
408*0Sstevel@tonic-gate 	void *), void *arg)
409*0Sstevel@tonic-gate {
410*0Sstevel@tonic-gate 	graph_edge_t *e;
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
415*0Sstevel@tonic-gate 	    e != NULL;
416*0Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
417*0Sstevel@tonic-gate 
418*0Sstevel@tonic-gate 		func(e->ge_vertex, arg);
419*0Sstevel@tonic-gate 	}
420*0Sstevel@tonic-gate }
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate /*
423*0Sstevel@tonic-gate  * Generic graph walking function.
424*0Sstevel@tonic-gate  *
425*0Sstevel@tonic-gate  * Given a vertex, this function will walk either dependencies
426*0Sstevel@tonic-gate  * (WALK_DEPENDENCIES) or dependents (WALK_DEPENDENTS) of a vertex recursively
427*0Sstevel@tonic-gate  * for the entire graph.  It will avoid cycles and never visit the same vertex
428*0Sstevel@tonic-gate  * twice.
429*0Sstevel@tonic-gate  *
430*0Sstevel@tonic-gate  * We avoid traversing exclusion dependencies, because they are allowed to
431*0Sstevel@tonic-gate  * create cycles in the graph.  When propagating satisfiability, there is no
432*0Sstevel@tonic-gate  * need to walk exclusion dependencies because exclude_all_satisfied() doesn't
433*0Sstevel@tonic-gate  * test for satisfiability.
434*0Sstevel@tonic-gate  *
435*0Sstevel@tonic-gate  * The walker takes two callbacks.  The first is called before examining the
436*0Sstevel@tonic-gate  * dependents of each vertex.  The second is called on each vertex after
437*0Sstevel@tonic-gate  * examining its dependents.  This allows is_path_to() to construct a path only
438*0Sstevel@tonic-gate  * after the target vertex has been found.
439*0Sstevel@tonic-gate  */
440*0Sstevel@tonic-gate typedef enum {
441*0Sstevel@tonic-gate 	WALK_DEPENDENTS,
442*0Sstevel@tonic-gate 	WALK_DEPENDENCIES
443*0Sstevel@tonic-gate } graph_walk_dir_t;
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate typedef int (*graph_walk_cb_t)(graph_vertex_t *, void *);
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate typedef struct graph_walk_info {
448*0Sstevel@tonic-gate 	graph_walk_dir_t 	gi_dir;
449*0Sstevel@tonic-gate 	uchar_t			*gi_visited;	/* vertex bitmap */
450*0Sstevel@tonic-gate 	int			(*gi_pre)(graph_vertex_t *, void *);
451*0Sstevel@tonic-gate 	void			(*gi_post)(graph_vertex_t *, void *);
452*0Sstevel@tonic-gate 	void			*gi_arg;	/* callback arg */
453*0Sstevel@tonic-gate 	int			gi_ret;		/* return value */
454*0Sstevel@tonic-gate } graph_walk_info_t;
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate static int
457*0Sstevel@tonic-gate graph_walk_recurse(graph_edge_t *e, graph_walk_info_t *gip)
458*0Sstevel@tonic-gate {
459*0Sstevel@tonic-gate 	uu_list_t *list;
460*0Sstevel@tonic-gate 	int r;
461*0Sstevel@tonic-gate 	graph_vertex_t *v = e->ge_vertex;
462*0Sstevel@tonic-gate 	int i;
463*0Sstevel@tonic-gate 	uint_t b;
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 	i = v->gv_id / 8;
466*0Sstevel@tonic-gate 	b = 1 << (v->gv_id % 8);
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate 	/*
469*0Sstevel@tonic-gate 	 * Check to see if we've visited this vertex already.
470*0Sstevel@tonic-gate 	 */
471*0Sstevel@tonic-gate 	if (gip->gi_visited[i] & b)
472*0Sstevel@tonic-gate 		return (UU_WALK_NEXT);
473*0Sstevel@tonic-gate 
474*0Sstevel@tonic-gate 	gip->gi_visited[i] |= b;
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 	/*
477*0Sstevel@tonic-gate 	 * Don't follow exclusions.
478*0Sstevel@tonic-gate 	 */
479*0Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
480*0Sstevel@tonic-gate 		return (UU_WALK_NEXT);
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate 	/*
483*0Sstevel@tonic-gate 	 * Call pre-visit callback.  If this doesn't terminate the walk,
484*0Sstevel@tonic-gate 	 * continue search.
485*0Sstevel@tonic-gate 	 */
486*0Sstevel@tonic-gate 	if ((gip->gi_ret = gip->gi_pre(v, gip->gi_arg)) == UU_WALK_NEXT) {
487*0Sstevel@tonic-gate 		/*
488*0Sstevel@tonic-gate 		 * Recurse using appropriate list.
489*0Sstevel@tonic-gate 		 */
490*0Sstevel@tonic-gate 		if (gip->gi_dir == WALK_DEPENDENTS)
491*0Sstevel@tonic-gate 			list = v->gv_dependents;
492*0Sstevel@tonic-gate 		else
493*0Sstevel@tonic-gate 			list = v->gv_dependencies;
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 		r = uu_list_walk(list, (uu_walk_fn_t *)graph_walk_recurse,
496*0Sstevel@tonic-gate 		    gip, 0);
497*0Sstevel@tonic-gate 		assert(r == 0);
498*0Sstevel@tonic-gate 	}
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 	/*
501*0Sstevel@tonic-gate 	 * Callbacks must return either UU_WALK_NEXT or UU_WALK_DONE.
502*0Sstevel@tonic-gate 	 */
503*0Sstevel@tonic-gate 	assert(gip->gi_ret == UU_WALK_NEXT || gip->gi_ret == UU_WALK_DONE);
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 	/*
506*0Sstevel@tonic-gate 	 * If given a post-callback, call the function for every vertex.
507*0Sstevel@tonic-gate 	 */
508*0Sstevel@tonic-gate 	if (gip->gi_post != NULL)
509*0Sstevel@tonic-gate 		(void) gip->gi_post(v, gip->gi_arg);
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate 	/*
512*0Sstevel@tonic-gate 	 * Preserve the callback's return value.  If the callback returns
513*0Sstevel@tonic-gate 	 * UU_WALK_DONE, then we propagate that to the caller in order to
514*0Sstevel@tonic-gate 	 * terminate the walk.
515*0Sstevel@tonic-gate 	 */
516*0Sstevel@tonic-gate 	return (gip->gi_ret);
517*0Sstevel@tonic-gate }
518*0Sstevel@tonic-gate 
519*0Sstevel@tonic-gate static void
520*0Sstevel@tonic-gate graph_walk(graph_vertex_t *v, graph_walk_dir_t dir,
521*0Sstevel@tonic-gate     int (*pre)(graph_vertex_t *, void *),
522*0Sstevel@tonic-gate     void (*post)(graph_vertex_t *, void *), void *arg)
523*0Sstevel@tonic-gate {
524*0Sstevel@tonic-gate 	graph_walk_info_t gi;
525*0Sstevel@tonic-gate 	graph_edge_t fake;
526*0Sstevel@tonic-gate 	size_t sz = dictionary->dict_new_id / 8 + 1;
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 	gi.gi_visited = startd_zalloc(sz);
529*0Sstevel@tonic-gate 	gi.gi_pre = pre;
530*0Sstevel@tonic-gate 	gi.gi_post = post;
531*0Sstevel@tonic-gate 	gi.gi_arg = arg;
532*0Sstevel@tonic-gate 	gi.gi_dir = dir;
533*0Sstevel@tonic-gate 	gi.gi_ret = 0;
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate 	/*
536*0Sstevel@tonic-gate 	 * Fake up an edge for the first iteration
537*0Sstevel@tonic-gate 	 */
538*0Sstevel@tonic-gate 	fake.ge_vertex = v;
539*0Sstevel@tonic-gate 	(void) graph_walk_recurse(&fake, &gi);
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 	startd_free(gi.gi_visited, sz);
542*0Sstevel@tonic-gate }
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate typedef struct child_search {
545*0Sstevel@tonic-gate 	int	id;		/* id of vertex to look for */
546*0Sstevel@tonic-gate 	uint_t	depth;		/* recursion depth */
547*0Sstevel@tonic-gate 	/*
548*0Sstevel@tonic-gate 	 * While the vertex is not found, path is NULL.  After the search, if
549*0Sstevel@tonic-gate 	 * the vertex was found then path should point to a -1-terminated
550*0Sstevel@tonic-gate 	 * array of vertex id's which constitute the path to the vertex.
551*0Sstevel@tonic-gate 	 */
552*0Sstevel@tonic-gate 	int	*path;
553*0Sstevel@tonic-gate } child_search_t;
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate static int
556*0Sstevel@tonic-gate child_pre(graph_vertex_t *v, void *arg)
557*0Sstevel@tonic-gate {
558*0Sstevel@tonic-gate 	child_search_t *cs = arg;
559*0Sstevel@tonic-gate 
560*0Sstevel@tonic-gate 	cs->depth++;
561*0Sstevel@tonic-gate 
562*0Sstevel@tonic-gate 	if (v->gv_id == cs->id) {
563*0Sstevel@tonic-gate 		cs->path = startd_alloc((cs->depth + 1) * sizeof (int));
564*0Sstevel@tonic-gate 		cs->path[cs->depth] = -1;
565*0Sstevel@tonic-gate 		return (UU_WALK_DONE);
566*0Sstevel@tonic-gate 	}
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 	return (UU_WALK_NEXT);
569*0Sstevel@tonic-gate }
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate static void
572*0Sstevel@tonic-gate child_post(graph_vertex_t *v, void *arg)
573*0Sstevel@tonic-gate {
574*0Sstevel@tonic-gate 	child_search_t *cs = arg;
575*0Sstevel@tonic-gate 
576*0Sstevel@tonic-gate 	cs->depth--;
577*0Sstevel@tonic-gate 
578*0Sstevel@tonic-gate 	if (cs->path != NULL)
579*0Sstevel@tonic-gate 		cs->path[cs->depth] = v->gv_id;
580*0Sstevel@tonic-gate }
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate /*
583*0Sstevel@tonic-gate  * Look for a path from from to to.  If one exists, returns a pointer to
584*0Sstevel@tonic-gate  * a NULL-terminated array of pointers to the vertices along the path.  If
585*0Sstevel@tonic-gate  * there is no path, returns NULL.
586*0Sstevel@tonic-gate  */
587*0Sstevel@tonic-gate static int *
588*0Sstevel@tonic-gate is_path_to(graph_vertex_t *from, graph_vertex_t *to)
589*0Sstevel@tonic-gate {
590*0Sstevel@tonic-gate 	child_search_t cs;
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate 	cs.id = to->gv_id;
593*0Sstevel@tonic-gate 	cs.depth = 0;
594*0Sstevel@tonic-gate 	cs.path = NULL;
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 	graph_walk(from, WALK_DEPENDENCIES, child_pre, child_post, &cs);
597*0Sstevel@tonic-gate 
598*0Sstevel@tonic-gate 	return (cs.path);
599*0Sstevel@tonic-gate }
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate /*
602*0Sstevel@tonic-gate  * Given an array of int's as returned by is_path_to, allocates a string of
603*0Sstevel@tonic-gate  * their names joined by newlines.  Returns the size of the allocated buffer
604*0Sstevel@tonic-gate  * in *sz and frees path.
605*0Sstevel@tonic-gate  */
606*0Sstevel@tonic-gate static void
607*0Sstevel@tonic-gate path_to_str(int *path, char **cpp, size_t *sz)
608*0Sstevel@tonic-gate {
609*0Sstevel@tonic-gate 	int i;
610*0Sstevel@tonic-gate 	graph_vertex_t *v;
611*0Sstevel@tonic-gate 	size_t allocd, new_allocd;
612*0Sstevel@tonic-gate 	char *new, *name;
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
615*0Sstevel@tonic-gate 	assert(path[0] != -1);
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 	allocd = 1;
618*0Sstevel@tonic-gate 	*cpp = startd_alloc(1);
619*0Sstevel@tonic-gate 	(*cpp)[0] = '\0';
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 	for (i = 0; path[i] != -1; ++i) {
622*0Sstevel@tonic-gate 		name = NULL;
623*0Sstevel@tonic-gate 
624*0Sstevel@tonic-gate 		v = vertex_get_by_id(path[i]);
625*0Sstevel@tonic-gate 
626*0Sstevel@tonic-gate 		if (v == NULL)
627*0Sstevel@tonic-gate 			name = "<deleted>";
628*0Sstevel@tonic-gate 		else if (v->gv_type == GVT_INST || v->gv_type == GVT_SVC)
629*0Sstevel@tonic-gate 			name = v->gv_name;
630*0Sstevel@tonic-gate 
631*0Sstevel@tonic-gate 		if (name != NULL) {
632*0Sstevel@tonic-gate 			new_allocd = allocd + strlen(name) + 1;
633*0Sstevel@tonic-gate 			new = startd_alloc(new_allocd);
634*0Sstevel@tonic-gate 			(void) strcpy(new, *cpp);
635*0Sstevel@tonic-gate 			(void) strcat(new, name);
636*0Sstevel@tonic-gate 			(void) strcat(new, "\n");
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate 			startd_free(*cpp, allocd);
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate 			*cpp = new;
641*0Sstevel@tonic-gate 			allocd = new_allocd;
642*0Sstevel@tonic-gate 		}
643*0Sstevel@tonic-gate 	}
644*0Sstevel@tonic-gate 
645*0Sstevel@tonic-gate 	startd_free(path, sizeof (int) * (i + 1));
646*0Sstevel@tonic-gate 
647*0Sstevel@tonic-gate 	*sz = allocd;
648*0Sstevel@tonic-gate }
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate 
651*0Sstevel@tonic-gate /*
652*0Sstevel@tonic-gate  * This function along with run_sulogin() implements an exclusion relationship
653*0Sstevel@tonic-gate  * between system/console-login and sulogin.  run_sulogin() will fail if
654*0Sstevel@tonic-gate  * system/console-login is online, and the graph engine should call
655*0Sstevel@tonic-gate  * graph_clogin_start() to bring system/console-login online, which defers the
656*0Sstevel@tonic-gate  * start if sulogin is running.
657*0Sstevel@tonic-gate  */
658*0Sstevel@tonic-gate static void
659*0Sstevel@tonic-gate graph_clogin_start(graph_vertex_t *v)
660*0Sstevel@tonic-gate {
661*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
662*0Sstevel@tonic-gate 
663*0Sstevel@tonic-gate 	if (sulogin_running)
664*0Sstevel@tonic-gate 		console_login_ready = B_TRUE;
665*0Sstevel@tonic-gate 	else
666*0Sstevel@tonic-gate 		vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
667*0Sstevel@tonic-gate }
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate static void
670*0Sstevel@tonic-gate graph_su_start(graph_vertex_t *v)
671*0Sstevel@tonic-gate {
672*0Sstevel@tonic-gate 	/*
673*0Sstevel@tonic-gate 	 * /etc/inittab used to have the initial /sbin/rcS as a 'sysinit'
674*0Sstevel@tonic-gate 	 * entry with a runlevel of 'S', before jumping to the final
675*0Sstevel@tonic-gate 	 * target runlevel (as set in initdefault).  We mimic that legacy
676*0Sstevel@tonic-gate 	 * behavior here.
677*0Sstevel@tonic-gate 	 */
678*0Sstevel@tonic-gate 	utmpx_set_runlevel('S', '0', B_FALSE);
679*0Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
680*0Sstevel@tonic-gate }
681*0Sstevel@tonic-gate 
682*0Sstevel@tonic-gate static void
683*0Sstevel@tonic-gate graph_post_su_online(void)
684*0Sstevel@tonic-gate {
685*0Sstevel@tonic-gate 	graph_runlevel_changed('S', 1);
686*0Sstevel@tonic-gate }
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate static void
689*0Sstevel@tonic-gate graph_post_su_disable(void)
690*0Sstevel@tonic-gate {
691*0Sstevel@tonic-gate 	graph_runlevel_changed('S', 0);
692*0Sstevel@tonic-gate }
693*0Sstevel@tonic-gate 
694*0Sstevel@tonic-gate static void
695*0Sstevel@tonic-gate graph_post_mu_online(void)
696*0Sstevel@tonic-gate {
697*0Sstevel@tonic-gate 	graph_runlevel_changed('2', 1);
698*0Sstevel@tonic-gate }
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate static void
701*0Sstevel@tonic-gate graph_post_mu_disable(void)
702*0Sstevel@tonic-gate {
703*0Sstevel@tonic-gate 	graph_runlevel_changed('2', 0);
704*0Sstevel@tonic-gate }
705*0Sstevel@tonic-gate 
706*0Sstevel@tonic-gate static void
707*0Sstevel@tonic-gate graph_post_mus_online(void)
708*0Sstevel@tonic-gate {
709*0Sstevel@tonic-gate 	graph_runlevel_changed('3', 1);
710*0Sstevel@tonic-gate }
711*0Sstevel@tonic-gate 
712*0Sstevel@tonic-gate static void
713*0Sstevel@tonic-gate graph_post_mus_disable(void)
714*0Sstevel@tonic-gate {
715*0Sstevel@tonic-gate 	graph_runlevel_changed('3', 0);
716*0Sstevel@tonic-gate }
717*0Sstevel@tonic-gate 
718*0Sstevel@tonic-gate static struct special_vertex_info {
719*0Sstevel@tonic-gate 	const char	*name;
720*0Sstevel@tonic-gate 	void		(*start_f)(graph_vertex_t *);
721*0Sstevel@tonic-gate 	void		(*post_online_f)(void);
722*0Sstevel@tonic-gate 	void		(*post_disable_f)(void);
723*0Sstevel@tonic-gate } special_vertices[] = {
724*0Sstevel@tonic-gate 	{ CONSOLE_LOGIN_FMRI, graph_clogin_start, NULL, NULL },
725*0Sstevel@tonic-gate 	{ SCF_MILESTONE_SINGLE_USER, graph_su_start,
726*0Sstevel@tonic-gate 	    graph_post_su_online, graph_post_su_disable },
727*0Sstevel@tonic-gate 	{ SCF_MILESTONE_MULTI_USER, NULL,
728*0Sstevel@tonic-gate 	    graph_post_mu_online, graph_post_mu_disable },
729*0Sstevel@tonic-gate 	{ SCF_MILESTONE_MULTI_USER_SERVER, NULL,
730*0Sstevel@tonic-gate 	    graph_post_mus_online, graph_post_mus_disable },
731*0Sstevel@tonic-gate 	{ NULL },
732*0Sstevel@tonic-gate };
733*0Sstevel@tonic-gate 
734*0Sstevel@tonic-gate 
735*0Sstevel@tonic-gate void
736*0Sstevel@tonic-gate vertex_send_event(graph_vertex_t *v, restarter_event_type_t e)
737*0Sstevel@tonic-gate {
738*0Sstevel@tonic-gate 	switch (e) {
739*0Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADD_INSTANCE:
740*0Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_UNINIT);
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 		MUTEX_LOCK(&st->st_load_lock);
743*0Sstevel@tonic-gate 		st->st_load_instances++;
744*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&st->st_load_lock);
745*0Sstevel@tonic-gate 		break;
746*0Sstevel@tonic-gate 
747*0Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ENABLE:
748*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Enabling %s.\n", v->gv_name);
749*0Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_UNINIT ||
750*0Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_DISABLED ||
751*0Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_MAINT);
752*0Sstevel@tonic-gate 		break;
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_DISABLE:
755*0Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
756*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Disabling %s.\n", v->gv_name);
757*0Sstevel@tonic-gate 		assert(v->gv_state != RESTARTER_STATE_DISABLED);
758*0Sstevel@tonic-gate 		break;
759*0Sstevel@tonic-gate 
760*0Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_STOP:
761*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Stopping %s.\n", v->gv_name);
762*0Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_DEGRADED ||
763*0Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_ONLINE);
764*0Sstevel@tonic-gate 		break;
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_START:
767*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Starting %s.\n", v->gv_name);
768*0Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_OFFLINE);
769*0Sstevel@tonic-gate 		break;
770*0Sstevel@tonic-gate 
771*0Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE:
772*0Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_DEGRADED:
773*0Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_REFRESH:
774*0Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_RESTART:
775*0Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF:
776*0Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
777*0Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE:
778*0Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
779*0Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
780*0Sstevel@tonic-gate 		break;
781*0Sstevel@tonic-gate 
782*0Sstevel@tonic-gate 	default:
783*0Sstevel@tonic-gate #ifndef NDEBUG
784*0Sstevel@tonic-gate 		uu_warn("%s:%d: Bad event %d.\n", __FILE__, __LINE__, e);
785*0Sstevel@tonic-gate #endif
786*0Sstevel@tonic-gate 		abort();
787*0Sstevel@tonic-gate 	}
788*0Sstevel@tonic-gate 
789*0Sstevel@tonic-gate 	restarter_protocol_send_event(v->gv_name, v->gv_restarter_channel, e);
790*0Sstevel@tonic-gate }
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate static void
793*0Sstevel@tonic-gate graph_unset_restarter(graph_vertex_t *v)
794*0Sstevel@tonic-gate {
795*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
796*0Sstevel@tonic-gate 	assert(v->gv_flags & GV_CONFIGURED);
797*0Sstevel@tonic-gate 
798*0Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_REMOVE_INSTANCE);
799*0Sstevel@tonic-gate 
800*0Sstevel@tonic-gate 	if (v->gv_restarter_id != -1) {
801*0Sstevel@tonic-gate 		graph_vertex_t *rv;
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate 		rv = vertex_get_by_id(v->gv_restarter_id);
804*0Sstevel@tonic-gate 		graph_remove_edge(v, rv);
805*0Sstevel@tonic-gate 	}
806*0Sstevel@tonic-gate 
807*0Sstevel@tonic-gate 	v->gv_restarter_id = -1;
808*0Sstevel@tonic-gate 	v->gv_restarter_channel = NULL;
809*0Sstevel@tonic-gate }
810*0Sstevel@tonic-gate 
811*0Sstevel@tonic-gate static void
812*0Sstevel@tonic-gate delete_depgroup(graph_vertex_t *v)
813*0Sstevel@tonic-gate {
814*0Sstevel@tonic-gate 	graph_edge_t *e;
815*0Sstevel@tonic-gate 	graph_vertex_t *dv;
816*0Sstevel@tonic-gate 
817*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
818*0Sstevel@tonic-gate 	assert(v->gv_type == GVT_GROUP);
819*0Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependents) == 0);
820*0Sstevel@tonic-gate 
821*0Sstevel@tonic-gate 	while ((e = uu_list_first(v->gv_dependencies)) != NULL) {
822*0Sstevel@tonic-gate 		dv = e->ge_vertex;
823*0Sstevel@tonic-gate 
824*0Sstevel@tonic-gate 		graph_remove_edge(v, dv);
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate 		switch (dv->gv_type) {
827*0Sstevel@tonic-gate 		case GVT_INST:		/* instance dependency */
828*0Sstevel@tonic-gate 			break;
829*0Sstevel@tonic-gate 
830*0Sstevel@tonic-gate 		case GVT_SVC:		/* service dependency */
831*0Sstevel@tonic-gate 			if (uu_list_numnodes(dv->gv_dependents) == 0 &&
832*0Sstevel@tonic-gate 			    uu_list_numnodes(dv->gv_dependencies) == 0)
833*0Sstevel@tonic-gate 				graph_remove_vertex(dv);
834*0Sstevel@tonic-gate 			break;
835*0Sstevel@tonic-gate 
836*0Sstevel@tonic-gate 		case GVT_FILE:		/* file dependency */
837*0Sstevel@tonic-gate 			assert(uu_list_numnodes(dv->gv_dependencies) == 0);
838*0Sstevel@tonic-gate 			if (uu_list_numnodes(dv->gv_dependents) == 0)
839*0Sstevel@tonic-gate 				graph_remove_vertex(dv);
840*0Sstevel@tonic-gate 			break;
841*0Sstevel@tonic-gate 
842*0Sstevel@tonic-gate 		default:
843*0Sstevel@tonic-gate #ifndef NDEBUG
844*0Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected node type %d", __FILE__,
845*0Sstevel@tonic-gate 			    __LINE__, dv->gv_type);
846*0Sstevel@tonic-gate #endif
847*0Sstevel@tonic-gate 			abort();
848*0Sstevel@tonic-gate 		}
849*0Sstevel@tonic-gate 	}
850*0Sstevel@tonic-gate 
851*0Sstevel@tonic-gate 	graph_remove_vertex(v);
852*0Sstevel@tonic-gate }
853*0Sstevel@tonic-gate 
854*0Sstevel@tonic-gate static int
855*0Sstevel@tonic-gate delete_instance_deps_cb(graph_edge_t *e, void **ptrs)
856*0Sstevel@tonic-gate {
857*0Sstevel@tonic-gate 	graph_vertex_t *v = ptrs[0];
858*0Sstevel@tonic-gate 	boolean_t delete_restarter_dep = (boolean_t)ptrs[1];
859*0Sstevel@tonic-gate 	graph_vertex_t *dv;
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate 	dv = e->ge_vertex;
862*0Sstevel@tonic-gate 
863*0Sstevel@tonic-gate 	/*
864*0Sstevel@tonic-gate 	 * We have four possibilities here:
865*0Sstevel@tonic-gate 	 *   - GVT_INST: restarter
866*0Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_INST: instance dependency
867*0Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_SVC - GV_INST: service dependency
868*0Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_FILE: file dependency
869*0Sstevel@tonic-gate 	 */
870*0Sstevel@tonic-gate 	switch (dv->gv_type) {
871*0Sstevel@tonic-gate 	case GVT_INST:	/* restarter */
872*0Sstevel@tonic-gate 		assert(dv->gv_id == v->gv_restarter_id);
873*0Sstevel@tonic-gate 		if (delete_restarter_dep)
874*0Sstevel@tonic-gate 			graph_remove_edge(v, dv);
875*0Sstevel@tonic-gate 		break;
876*0Sstevel@tonic-gate 
877*0Sstevel@tonic-gate 	case GVT_GROUP:	/* pg dependency */
878*0Sstevel@tonic-gate 		graph_remove_edge(v, dv);
879*0Sstevel@tonic-gate 		delete_depgroup(dv);
880*0Sstevel@tonic-gate 		break;
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate 	case GVT_FILE:
883*0Sstevel@tonic-gate 		/* These are currently not direct dependencies */
884*0Sstevel@tonic-gate 
885*0Sstevel@tonic-gate 	default:
886*0Sstevel@tonic-gate #ifndef NDEBUG
887*0Sstevel@tonic-gate 		uu_warn("%s:%d: Bad vertex type %d.\n", __FILE__, __LINE__,
888*0Sstevel@tonic-gate 		    dv->gv_type);
889*0Sstevel@tonic-gate #endif
890*0Sstevel@tonic-gate 		abort();
891*0Sstevel@tonic-gate 	}
892*0Sstevel@tonic-gate 
893*0Sstevel@tonic-gate 	return (UU_WALK_NEXT);
894*0Sstevel@tonic-gate }
895*0Sstevel@tonic-gate 
896*0Sstevel@tonic-gate static void
897*0Sstevel@tonic-gate delete_instance_dependencies(graph_vertex_t *v, boolean_t delete_restarter_dep)
898*0Sstevel@tonic-gate {
899*0Sstevel@tonic-gate 	void *ptrs[2];
900*0Sstevel@tonic-gate 	int r;
901*0Sstevel@tonic-gate 
902*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
903*0Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
904*0Sstevel@tonic-gate 
905*0Sstevel@tonic-gate 	ptrs[0] = v;
906*0Sstevel@tonic-gate 	ptrs[1] = (void *)delete_restarter_dep;
907*0Sstevel@tonic-gate 
908*0Sstevel@tonic-gate 	r = uu_list_walk(v->gv_dependencies,
909*0Sstevel@tonic-gate 	    (uu_walk_fn_t *)delete_instance_deps_cb, &ptrs, UU_WALK_ROBUST);
910*0Sstevel@tonic-gate 	assert(r == 0);
911*0Sstevel@tonic-gate }
912*0Sstevel@tonic-gate 
913*0Sstevel@tonic-gate /*
914*0Sstevel@tonic-gate  * int graph_insert_vertex_unconfigured()
915*0Sstevel@tonic-gate  *   Insert a vertex without sending any restarter events. If the vertex
916*0Sstevel@tonic-gate  *   already exists or creation is successful, return a pointer to it in *vp.
917*0Sstevel@tonic-gate  *
918*0Sstevel@tonic-gate  *   If type is not GVT_GROUP, dt can remain unset.
919*0Sstevel@tonic-gate  *
920*0Sstevel@tonic-gate  *   Returns 0, EEXIST, or EINVAL if the arguments are invalid (i.e., fmri
921*0Sstevel@tonic-gate  *   doesn't agree with type, or type doesn't agree with dt).
922*0Sstevel@tonic-gate  */
923*0Sstevel@tonic-gate static int
924*0Sstevel@tonic-gate graph_insert_vertex_unconfigured(const char *fmri, gv_type_t type,
925*0Sstevel@tonic-gate     depgroup_type_t dt, restarter_error_t rt, graph_vertex_t **vp)
926*0Sstevel@tonic-gate {
927*0Sstevel@tonic-gate 	int r;
928*0Sstevel@tonic-gate 	int i;
929*0Sstevel@tonic-gate 
930*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
931*0Sstevel@tonic-gate 
932*0Sstevel@tonic-gate 	switch (type) {
933*0Sstevel@tonic-gate 	case GVT_SVC:
934*0Sstevel@tonic-gate 	case GVT_INST:
935*0Sstevel@tonic-gate 		if (strncmp(fmri, "svc:", sizeof ("svc:") - 1) != 0)
936*0Sstevel@tonic-gate 			return (EINVAL);
937*0Sstevel@tonic-gate 		break;
938*0Sstevel@tonic-gate 
939*0Sstevel@tonic-gate 	case GVT_FILE:
940*0Sstevel@tonic-gate 		if (strncmp(fmri, "file:", sizeof ("file:") - 1) != 0)
941*0Sstevel@tonic-gate 			return (EINVAL);
942*0Sstevel@tonic-gate 		break;
943*0Sstevel@tonic-gate 
944*0Sstevel@tonic-gate 	case GVT_GROUP:
945*0Sstevel@tonic-gate 		if (dt <= 0 || rt < 0)
946*0Sstevel@tonic-gate 			return (EINVAL);
947*0Sstevel@tonic-gate 		break;
948*0Sstevel@tonic-gate 
949*0Sstevel@tonic-gate 	default:
950*0Sstevel@tonic-gate #ifndef NDEBUG
951*0Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown type %d.\n", __FILE__, __LINE__, type);
952*0Sstevel@tonic-gate #endif
953*0Sstevel@tonic-gate 		abort();
954*0Sstevel@tonic-gate 	}
955*0Sstevel@tonic-gate 
956*0Sstevel@tonic-gate 	*vp = vertex_get_by_name(fmri);
957*0Sstevel@tonic-gate 	if (*vp != NULL)
958*0Sstevel@tonic-gate 		return (EEXIST);
959*0Sstevel@tonic-gate 
960*0Sstevel@tonic-gate 	*vp = graph_add_vertex(fmri);
961*0Sstevel@tonic-gate 
962*0Sstevel@tonic-gate 	(*vp)->gv_type = type;
963*0Sstevel@tonic-gate 	(*vp)->gv_depgroup = dt;
964*0Sstevel@tonic-gate 	(*vp)->gv_restart = rt;
965*0Sstevel@tonic-gate 
966*0Sstevel@tonic-gate 	(*vp)->gv_flags = 0;
967*0Sstevel@tonic-gate 	(*vp)->gv_state = RESTARTER_STATE_NONE;
968*0Sstevel@tonic-gate 
969*0Sstevel@tonic-gate 	for (i = 0; special_vertices[i].name != NULL; ++i) {
970*0Sstevel@tonic-gate 		if (strcmp(fmri, special_vertices[i].name) == 0) {
971*0Sstevel@tonic-gate 			(*vp)->gv_start_f = special_vertices[i].start_f;
972*0Sstevel@tonic-gate 			(*vp)->gv_post_online_f =
973*0Sstevel@tonic-gate 			    special_vertices[i].post_online_f;
974*0Sstevel@tonic-gate 			(*vp)->gv_post_disable_f =
975*0Sstevel@tonic-gate 			    special_vertices[i].post_disable_f;
976*0Sstevel@tonic-gate 			break;
977*0Sstevel@tonic-gate 		}
978*0Sstevel@tonic-gate 	}
979*0Sstevel@tonic-gate 
980*0Sstevel@tonic-gate 	(*vp)->gv_restarter_id = -1;
981*0Sstevel@tonic-gate 	(*vp)->gv_restarter_channel = 0;
982*0Sstevel@tonic-gate 
983*0Sstevel@tonic-gate 	if (type == GVT_INST) {
984*0Sstevel@tonic-gate 		char *sfmri;
985*0Sstevel@tonic-gate 		graph_vertex_t *sv;
986*0Sstevel@tonic-gate 
987*0Sstevel@tonic-gate 		sfmri = inst_fmri_to_svc_fmri(fmri);
988*0Sstevel@tonic-gate 		sv = vertex_get_by_name(sfmri);
989*0Sstevel@tonic-gate 		if (sv == NULL) {
990*0Sstevel@tonic-gate 			r = graph_insert_vertex_unconfigured(sfmri, GVT_SVC, 0,
991*0Sstevel@tonic-gate 			    0, &sv);
992*0Sstevel@tonic-gate 			assert(r == 0);
993*0Sstevel@tonic-gate 		}
994*0Sstevel@tonic-gate 		startd_free(sfmri, max_scf_fmri_size);
995*0Sstevel@tonic-gate 
996*0Sstevel@tonic-gate 		graph_add_edge(sv, *vp);
997*0Sstevel@tonic-gate 	}
998*0Sstevel@tonic-gate 
999*0Sstevel@tonic-gate 	/*
1000*0Sstevel@tonic-gate 	 * If this vertex is in the subgraph, mark it as so, for both
1001*0Sstevel@tonic-gate 	 * GVT_INST and GVT_SERVICE verteces.
1002*0Sstevel@tonic-gate 	 * A GVT_SERVICE vertex can only be in the subgraph if another instance
1003*0Sstevel@tonic-gate 	 * depends on it, in which case it's already been added to the graph
1004*0Sstevel@tonic-gate 	 * and marked as in the subgraph (by refresh_vertex()).  If a
1005*0Sstevel@tonic-gate 	 * GVT_SERVICE vertex was freshly added (by the code above), it means
1006*0Sstevel@tonic-gate 	 * that it has no dependents, and cannot be in the subgraph.
1007*0Sstevel@tonic-gate 	 * Regardless of this, we still check that gv_flags includes
1008*0Sstevel@tonic-gate 	 * GV_INSUBGRAPH in the event that future behavior causes the above
1009*0Sstevel@tonic-gate 	 * code to add a GVT_SERVICE vertex which should be in the subgraph.
1010*0Sstevel@tonic-gate 	 */
1011*0Sstevel@tonic-gate 
1012*0Sstevel@tonic-gate 	(*vp)->gv_flags |= (should_be_in_subgraph(*vp)? GV_INSUBGRAPH : 0);
1013*0Sstevel@tonic-gate 
1014*0Sstevel@tonic-gate 	return (0);
1015*0Sstevel@tonic-gate }
1016*0Sstevel@tonic-gate 
1017*0Sstevel@tonic-gate /*
1018*0Sstevel@tonic-gate  * Returns 0 on success or ELOOP if the dependency would create a cycle.
1019*0Sstevel@tonic-gate  */
1020*0Sstevel@tonic-gate static int
1021*0Sstevel@tonic-gate graph_insert_dependency(graph_vertex_t *fv, graph_vertex_t *tv, int **pathp)
1022*0Sstevel@tonic-gate {
1023*0Sstevel@tonic-gate 	hrtime_t now;
1024*0Sstevel@tonic-gate 
1025*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
1026*0Sstevel@tonic-gate 
1027*0Sstevel@tonic-gate 	/* cycle detection */
1028*0Sstevel@tonic-gate 	now = gethrtime();
1029*0Sstevel@tonic-gate 
1030*0Sstevel@tonic-gate 	/* Don't follow exclusions. */
1031*0Sstevel@tonic-gate 	if (!(fv->gv_type == GVT_GROUP &&
1032*0Sstevel@tonic-gate 	    fv->gv_depgroup == DEPGRP_EXCLUDE_ALL)) {
1033*0Sstevel@tonic-gate 		*pathp = is_path_to(tv, fv);
1034*0Sstevel@tonic-gate 		if (*pathp)
1035*0Sstevel@tonic-gate 			return (ELOOP);
1036*0Sstevel@tonic-gate 	}
1037*0Sstevel@tonic-gate 
1038*0Sstevel@tonic-gate 	dep_cycle_ns += gethrtime() - now;
1039*0Sstevel@tonic-gate 	++dep_inserts;
1040*0Sstevel@tonic-gate 	now = gethrtime();
1041*0Sstevel@tonic-gate 
1042*0Sstevel@tonic-gate 	graph_add_edge(fv, tv);
1043*0Sstevel@tonic-gate 
1044*0Sstevel@tonic-gate 	dep_insert_ns += gethrtime() - now;
1045*0Sstevel@tonic-gate 
1046*0Sstevel@tonic-gate 	/* Check if the dependency adds the "to" vertex to the subgraph */
1047*0Sstevel@tonic-gate 	tv->gv_flags |= (should_be_in_subgraph(tv) ? GV_INSUBGRAPH : 0);
1048*0Sstevel@tonic-gate 
1049*0Sstevel@tonic-gate 	return (0);
1050*0Sstevel@tonic-gate }
1051*0Sstevel@tonic-gate 
1052*0Sstevel@tonic-gate static int
1053*0Sstevel@tonic-gate inst_running(graph_vertex_t *v)
1054*0Sstevel@tonic-gate {
1055*0Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
1056*0Sstevel@tonic-gate 
1057*0Sstevel@tonic-gate 	if (v->gv_state == RESTARTER_STATE_ONLINE ||
1058*0Sstevel@tonic-gate 	    v->gv_state == RESTARTER_STATE_DEGRADED)
1059*0Sstevel@tonic-gate 		return (1);
1060*0Sstevel@tonic-gate 
1061*0Sstevel@tonic-gate 	return (0);
1062*0Sstevel@tonic-gate }
1063*0Sstevel@tonic-gate 
1064*0Sstevel@tonic-gate /*
1065*0Sstevel@tonic-gate  * The dependency evaluation functions return
1066*0Sstevel@tonic-gate  *   1 - dependency satisfied
1067*0Sstevel@tonic-gate  *   0 - dependency unsatisfied
1068*0Sstevel@tonic-gate  *   -1 - dependency unsatisfiable (without administrator intervention)
1069*0Sstevel@tonic-gate  *
1070*0Sstevel@tonic-gate  * The functions also take a boolean satbility argument.  When true, the
1071*0Sstevel@tonic-gate  * functions may recurse in order to determine satisfiability.
1072*0Sstevel@tonic-gate  */
1073*0Sstevel@tonic-gate static int require_any_satisfied(graph_vertex_t *, boolean_t);
1074*0Sstevel@tonic-gate static int dependency_satisfied(graph_vertex_t *, boolean_t);
1075*0Sstevel@tonic-gate 
1076*0Sstevel@tonic-gate /*
1077*0Sstevel@tonic-gate  * A require_all dependency is unsatisfied if any elements are unsatisfied.  It
1078*0Sstevel@tonic-gate  * is unsatisfiable if any elements are unsatisfiable.
1079*0Sstevel@tonic-gate  */
1080*0Sstevel@tonic-gate static int
1081*0Sstevel@tonic-gate require_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
1082*0Sstevel@tonic-gate {
1083*0Sstevel@tonic-gate 	graph_edge_t *edge;
1084*0Sstevel@tonic-gate 	int i;
1085*0Sstevel@tonic-gate 	boolean_t any_unsatisfied;
1086*0Sstevel@tonic-gate 
1087*0Sstevel@tonic-gate 	if (uu_list_numnodes(groupv->gv_dependencies) == 0)
1088*0Sstevel@tonic-gate 		return (1);
1089*0Sstevel@tonic-gate 
1090*0Sstevel@tonic-gate 	any_unsatisfied = B_FALSE;
1091*0Sstevel@tonic-gate 
1092*0Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
1093*0Sstevel@tonic-gate 	    edge != NULL;
1094*0Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
1095*0Sstevel@tonic-gate 		i = dependency_satisfied(edge->ge_vertex, satbility);
1096*0Sstevel@tonic-gate 		if (i == 1)
1097*0Sstevel@tonic-gate 			continue;
1098*0Sstevel@tonic-gate 
1099*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
1100*0Sstevel@tonic-gate 		    "require_all(%s): %s is unsatisfi%s.\n", groupv->gv_name,
1101*0Sstevel@tonic-gate 		    edge->ge_vertex->gv_name, i == 0 ? "ed" : "able");
1102*0Sstevel@tonic-gate 
1103*0Sstevel@tonic-gate 		if (!satbility)
1104*0Sstevel@tonic-gate 			return (0);
1105*0Sstevel@tonic-gate 
1106*0Sstevel@tonic-gate 		if (i == -1)
1107*0Sstevel@tonic-gate 			return (-1);
1108*0Sstevel@tonic-gate 
1109*0Sstevel@tonic-gate 		any_unsatisfied = B_TRUE;
1110*0Sstevel@tonic-gate 	}
1111*0Sstevel@tonic-gate 
1112*0Sstevel@tonic-gate 	return (any_unsatisfied ? 0 : 1);
1113*0Sstevel@tonic-gate }
1114*0Sstevel@tonic-gate 
1115*0Sstevel@tonic-gate /*
1116*0Sstevel@tonic-gate  * A require_any dependency is satisfied if any element is satisfied.  It is
1117*0Sstevel@tonic-gate  * satisfiable if any element is satisfiable.
1118*0Sstevel@tonic-gate  */
1119*0Sstevel@tonic-gate static int
1120*0Sstevel@tonic-gate require_any_satisfied(graph_vertex_t *groupv, boolean_t satbility)
1121*0Sstevel@tonic-gate {
1122*0Sstevel@tonic-gate 	graph_edge_t *edge;
1123*0Sstevel@tonic-gate 	int s;
1124*0Sstevel@tonic-gate 	boolean_t satisfiable;
1125*0Sstevel@tonic-gate 
1126*0Sstevel@tonic-gate 	if (uu_list_numnodes(groupv->gv_dependencies) == 0)
1127*0Sstevel@tonic-gate 		return (1);
1128*0Sstevel@tonic-gate 
1129*0Sstevel@tonic-gate 	satisfiable = B_FALSE;
1130*0Sstevel@tonic-gate 
1131*0Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
1132*0Sstevel@tonic-gate 	    edge != NULL;
1133*0Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
1134*0Sstevel@tonic-gate 		s = dependency_satisfied(edge->ge_vertex, satbility);
1135*0Sstevel@tonic-gate 
1136*0Sstevel@tonic-gate 		if (s == 1)
1137*0Sstevel@tonic-gate 			return (1);
1138*0Sstevel@tonic-gate 
1139*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
1140*0Sstevel@tonic-gate 		    "require_any(%s): %s is unsatisfi%s.\n",
1141*0Sstevel@tonic-gate 		    groupv->gv_name, edge->ge_vertex->gv_name,
1142*0Sstevel@tonic-gate 		    s == 0 ? "ed" : "able");
1143*0Sstevel@tonic-gate 
1144*0Sstevel@tonic-gate 		if (satbility && s == 0)
1145*0Sstevel@tonic-gate 			satisfiable = B_TRUE;
1146*0Sstevel@tonic-gate 	}
1147*0Sstevel@tonic-gate 
1148*0Sstevel@tonic-gate 	return (!satbility || satisfiable ? 0 : -1);
1149*0Sstevel@tonic-gate }
1150*0Sstevel@tonic-gate 
1151*0Sstevel@tonic-gate /*
1152*0Sstevel@tonic-gate  * An optional_all dependency only considers elements which are configured,
1153*0Sstevel@tonic-gate  * enabled, and not in maintenance.  If any are unsatisfied, then the dependency
1154*0Sstevel@tonic-gate  * is unsatisfied.
1155*0Sstevel@tonic-gate  *
1156*0Sstevel@tonic-gate  * Offline dependencies which are waiting for a dependency to come online are
1157*0Sstevel@tonic-gate  * unsatisfied.  Offline dependences which cannot possibly come online
1158*0Sstevel@tonic-gate  * (unsatisfiable) are always considered satisfied.
1159*0Sstevel@tonic-gate  */
1160*0Sstevel@tonic-gate static int
1161*0Sstevel@tonic-gate optional_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
1162*0Sstevel@tonic-gate {
1163*0Sstevel@tonic-gate 	graph_edge_t *edge;
1164*0Sstevel@tonic-gate 	graph_vertex_t *v;
1165*0Sstevel@tonic-gate 	boolean_t any_qualified;
1166*0Sstevel@tonic-gate 	boolean_t any_unsatisfied;
1167*0Sstevel@tonic-gate 	int i;
1168*0Sstevel@tonic-gate 
1169*0Sstevel@tonic-gate 	any_qualified = B_FALSE;
1170*0Sstevel@tonic-gate 	any_unsatisfied = B_FALSE;
1171*0Sstevel@tonic-gate 
1172*0Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
1173*0Sstevel@tonic-gate 	    edge != NULL;
1174*0Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
1175*0Sstevel@tonic-gate 		v = edge->ge_vertex;
1176*0Sstevel@tonic-gate 
1177*0Sstevel@tonic-gate 		switch (v->gv_type) {
1178*0Sstevel@tonic-gate 		case GVT_INST:
1179*0Sstevel@tonic-gate 			/* Skip missing or disabled instances */
1180*0Sstevel@tonic-gate 			if ((v->gv_flags & (GV_CONFIGURED | GV_ENABLED)) !=
1181*0Sstevel@tonic-gate 			    (GV_CONFIGURED | GV_ENABLED))
1182*0Sstevel@tonic-gate 				continue;
1183*0Sstevel@tonic-gate 
1184*0Sstevel@tonic-gate 			if (v->gv_state == RESTARTER_STATE_MAINT)
1185*0Sstevel@tonic-gate 				continue;
1186*0Sstevel@tonic-gate 
1187*0Sstevel@tonic-gate 			any_qualified = B_TRUE;
1188*0Sstevel@tonic-gate 			if (v->gv_state == RESTARTER_STATE_OFFLINE) {
1189*0Sstevel@tonic-gate 				/*
1190*0Sstevel@tonic-gate 				 * For offline dependencies, treat unsatisfiable
1191*0Sstevel@tonic-gate 				 * as satisfied.
1192*0Sstevel@tonic-gate 				 */
1193*0Sstevel@tonic-gate 				i = dependency_satisfied(v, B_TRUE);
1194*0Sstevel@tonic-gate 				if (i == -1)
1195*0Sstevel@tonic-gate 					i = 1;
1196*0Sstevel@tonic-gate 			} else if (v->gv_state == RESTARTER_STATE_DISABLED) {
1197*0Sstevel@tonic-gate 				/*
1198*0Sstevel@tonic-gate 				 * The service is enabled, but hasn't
1199*0Sstevel@tonic-gate 				 * transitioned out of disabled yet.  Treat it
1200*0Sstevel@tonic-gate 				 * as unsatisfied (not unsatisfiable).
1201*0Sstevel@tonic-gate 				 */
1202*0Sstevel@tonic-gate 				i = 0;
1203*0Sstevel@tonic-gate 			} else {
1204*0Sstevel@tonic-gate 				i = dependency_satisfied(v, satbility);
1205*0Sstevel@tonic-gate 			}
1206*0Sstevel@tonic-gate 			break;
1207*0Sstevel@tonic-gate 
1208*0Sstevel@tonic-gate 		case GVT_FILE:
1209*0Sstevel@tonic-gate 			any_qualified = B_TRUE;
1210*0Sstevel@tonic-gate 			i = dependency_satisfied(v, satbility);
1211*0Sstevel@tonic-gate 
1212*0Sstevel@tonic-gate 			break;
1213*0Sstevel@tonic-gate 
1214*0Sstevel@tonic-gate 		case GVT_SVC: {
1215*0Sstevel@tonic-gate 			boolean_t svc_any_qualified;
1216*0Sstevel@tonic-gate 			boolean_t svc_satisfied;
1217*0Sstevel@tonic-gate 			boolean_t svc_satisfiable;
1218*0Sstevel@tonic-gate 			graph_vertex_t *v2;
1219*0Sstevel@tonic-gate 			graph_edge_t *e2;
1220*0Sstevel@tonic-gate 
1221*0Sstevel@tonic-gate 			svc_any_qualified = B_FALSE;
1222*0Sstevel@tonic-gate 			svc_satisfied = B_FALSE;
1223*0Sstevel@tonic-gate 			svc_satisfiable = B_FALSE;
1224*0Sstevel@tonic-gate 
1225*0Sstevel@tonic-gate 			for (e2 = uu_list_first(v->gv_dependencies);
1226*0Sstevel@tonic-gate 			    e2 != NULL;
1227*0Sstevel@tonic-gate 			    e2 = uu_list_next(v->gv_dependencies, e2)) {
1228*0Sstevel@tonic-gate 				v2 = e2->ge_vertex;
1229*0Sstevel@tonic-gate 				assert(v2->gv_type == GVT_INST);
1230*0Sstevel@tonic-gate 
1231*0Sstevel@tonic-gate 				if ((v2->gv_flags &
1232*0Sstevel@tonic-gate 				    (GV_CONFIGURED | GV_ENABLED)) !=
1233*0Sstevel@tonic-gate 				    (GV_CONFIGURED | GV_ENABLED))
1234*0Sstevel@tonic-gate 					continue;
1235*0Sstevel@tonic-gate 
1236*0Sstevel@tonic-gate 				if (v2->gv_state == RESTARTER_STATE_MAINT)
1237*0Sstevel@tonic-gate 					continue;
1238*0Sstevel@tonic-gate 
1239*0Sstevel@tonic-gate 				svc_any_qualified = B_TRUE;
1240*0Sstevel@tonic-gate 
1241*0Sstevel@tonic-gate 				if (v2->gv_state == RESTARTER_STATE_OFFLINE) {
1242*0Sstevel@tonic-gate 					/*
1243*0Sstevel@tonic-gate 					 * For offline dependencies, treat
1244*0Sstevel@tonic-gate 					 * unsatisfiable as satisfied.
1245*0Sstevel@tonic-gate 					 */
1246*0Sstevel@tonic-gate 					i = dependency_satisfied(v2, B_TRUE);
1247*0Sstevel@tonic-gate 					if (i == -1)
1248*0Sstevel@tonic-gate 						i = 1;
1249*0Sstevel@tonic-gate 				} else if (v2->gv_state ==
1250*0Sstevel@tonic-gate 				    RESTARTER_STATE_DISABLED) {
1251*0Sstevel@tonic-gate 					i = 0;
1252*0Sstevel@tonic-gate 				} else {
1253*0Sstevel@tonic-gate 					i = dependency_satisfied(v2, satbility);
1254*0Sstevel@tonic-gate 				}
1255*0Sstevel@tonic-gate 
1256*0Sstevel@tonic-gate 				if (i == 1) {
1257*0Sstevel@tonic-gate 					svc_satisfied = B_TRUE;
1258*0Sstevel@tonic-gate 					break;
1259*0Sstevel@tonic-gate 				}
1260*0Sstevel@tonic-gate 				if (i == 0)
1261*0Sstevel@tonic-gate 					svc_satisfiable = B_TRUE;
1262*0Sstevel@tonic-gate 			}
1263*0Sstevel@tonic-gate 
1264*0Sstevel@tonic-gate 			if (!svc_any_qualified)
1265*0Sstevel@tonic-gate 				continue;
1266*0Sstevel@tonic-gate 			any_qualified = B_TRUE;
1267*0Sstevel@tonic-gate 			if (svc_satisfied) {
1268*0Sstevel@tonic-gate 				i = 1;
1269*0Sstevel@tonic-gate 			} else if (svc_satisfiable) {
1270*0Sstevel@tonic-gate 				i = 0;
1271*0Sstevel@tonic-gate 			} else {
1272*0Sstevel@tonic-gate 				i = -1;
1273*0Sstevel@tonic-gate 			}
1274*0Sstevel@tonic-gate 			break;
1275*0Sstevel@tonic-gate 		}
1276*0Sstevel@tonic-gate 
1277*0Sstevel@tonic-gate 		case GVT_GROUP:
1278*0Sstevel@tonic-gate 		default:
1279*0Sstevel@tonic-gate #ifndef NDEBUG
1280*0Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
1281*0Sstevel@tonic-gate 			    __LINE__, v->gv_type);
1282*0Sstevel@tonic-gate #endif
1283*0Sstevel@tonic-gate 			abort();
1284*0Sstevel@tonic-gate 		}
1285*0Sstevel@tonic-gate 
1286*0Sstevel@tonic-gate 		if (i == 1)
1287*0Sstevel@tonic-gate 			continue;
1288*0Sstevel@tonic-gate 
1289*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
1290*0Sstevel@tonic-gate 		    "optional_all(%s): %s is unsatisfi%s.\n", groupv->gv_name,
1291*0Sstevel@tonic-gate 		    v->gv_name, i == 0 ? "ed" : "able");
1292*0Sstevel@tonic-gate 
1293*0Sstevel@tonic-gate 		if (!satbility)
1294*0Sstevel@tonic-gate 			return (0);
1295*0Sstevel@tonic-gate 		if (i == -1)
1296*0Sstevel@tonic-gate 			return (-1);
1297*0Sstevel@tonic-gate 		any_unsatisfied = B_TRUE;
1298*0Sstevel@tonic-gate 	}
1299*0Sstevel@tonic-gate 
1300*0Sstevel@tonic-gate 	if (!any_qualified)
1301*0Sstevel@tonic-gate 		return (1);
1302*0Sstevel@tonic-gate 
1303*0Sstevel@tonic-gate 	return (any_unsatisfied ? 0 : 1);
1304*0Sstevel@tonic-gate }
1305*0Sstevel@tonic-gate 
1306*0Sstevel@tonic-gate /*
1307*0Sstevel@tonic-gate  * An exclude_all dependency is unsatisfied if any non-service element is
1308*0Sstevel@tonic-gate  * satisfied or any service instance which is configured, enabled, and not in
1309*0Sstevel@tonic-gate  * maintenance is satisfied.  Usually when unsatisfied, it is also
1310*0Sstevel@tonic-gate  * unsatisfiable.
1311*0Sstevel@tonic-gate  */
1312*0Sstevel@tonic-gate #define	LOG_EXCLUDE(u, v)						\
1313*0Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "exclude_all(%s): %s is satisfied.\n",	\
1314*0Sstevel@tonic-gate 	    (u)->gv_name, (v)->gv_name)
1315*0Sstevel@tonic-gate 
1316*0Sstevel@tonic-gate /* ARGSUSED */
1317*0Sstevel@tonic-gate static int
1318*0Sstevel@tonic-gate exclude_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
1319*0Sstevel@tonic-gate {
1320*0Sstevel@tonic-gate 	graph_edge_t *edge, *e2;
1321*0Sstevel@tonic-gate 	graph_vertex_t *v, *v2;
1322*0Sstevel@tonic-gate 
1323*0Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
1324*0Sstevel@tonic-gate 	    edge != NULL;
1325*0Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
1326*0Sstevel@tonic-gate 		v = edge->ge_vertex;
1327*0Sstevel@tonic-gate 
1328*0Sstevel@tonic-gate 		switch (v->gv_type) {
1329*0Sstevel@tonic-gate 		case GVT_INST:
1330*0Sstevel@tonic-gate 			if ((v->gv_flags & GV_CONFIGURED) == 0)
1331*0Sstevel@tonic-gate 				continue;
1332*0Sstevel@tonic-gate 
1333*0Sstevel@tonic-gate 			switch (v->gv_state) {
1334*0Sstevel@tonic-gate 			case RESTARTER_STATE_ONLINE:
1335*0Sstevel@tonic-gate 			case RESTARTER_STATE_DEGRADED:
1336*0Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v);
1337*0Sstevel@tonic-gate 				return (v->gv_flags & GV_ENABLED ? -1 : 0);
1338*0Sstevel@tonic-gate 
1339*0Sstevel@tonic-gate 			case RESTARTER_STATE_OFFLINE:
1340*0Sstevel@tonic-gate 			case RESTARTER_STATE_UNINIT:
1341*0Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v);
1342*0Sstevel@tonic-gate 				return (0);
1343*0Sstevel@tonic-gate 
1344*0Sstevel@tonic-gate 			case RESTARTER_STATE_DISABLED:
1345*0Sstevel@tonic-gate 			case RESTARTER_STATE_MAINT:
1346*0Sstevel@tonic-gate 				continue;
1347*0Sstevel@tonic-gate 
1348*0Sstevel@tonic-gate 			default:
1349*0Sstevel@tonic-gate #ifndef NDEBUG
1350*0Sstevel@tonic-gate 				uu_warn("%s:%d: Unexpected vertex state %d.\n",
1351*0Sstevel@tonic-gate 				    __FILE__, __LINE__, v->gv_state);
1352*0Sstevel@tonic-gate #endif
1353*0Sstevel@tonic-gate 				abort();
1354*0Sstevel@tonic-gate 			}
1355*0Sstevel@tonic-gate 			/* NOTREACHED */
1356*0Sstevel@tonic-gate 
1357*0Sstevel@tonic-gate 		case GVT_SVC:
1358*0Sstevel@tonic-gate 			break;
1359*0Sstevel@tonic-gate 
1360*0Sstevel@tonic-gate 		case GVT_FILE:
1361*0Sstevel@tonic-gate 			if (!file_ready(v))
1362*0Sstevel@tonic-gate 				continue;
1363*0Sstevel@tonic-gate 			LOG_EXCLUDE(groupv, v);
1364*0Sstevel@tonic-gate 			return (-1);
1365*0Sstevel@tonic-gate 
1366*0Sstevel@tonic-gate 		case GVT_GROUP:
1367*0Sstevel@tonic-gate 		default:
1368*0Sstevel@tonic-gate #ifndef NDEBUG
1369*0Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
1370*0Sstevel@tonic-gate 			    __LINE__, v->gv_type);
1371*0Sstevel@tonic-gate #endif
1372*0Sstevel@tonic-gate 			abort();
1373*0Sstevel@tonic-gate 		}
1374*0Sstevel@tonic-gate 
1375*0Sstevel@tonic-gate 		/* v represents a service */
1376*0Sstevel@tonic-gate 		if (uu_list_numnodes(v->gv_dependencies) == 0)
1377*0Sstevel@tonic-gate 			continue;
1378*0Sstevel@tonic-gate 
1379*0Sstevel@tonic-gate 		for (e2 = uu_list_first(v->gv_dependencies);
1380*0Sstevel@tonic-gate 		    e2 != NULL;
1381*0Sstevel@tonic-gate 		    e2 = uu_list_next(v->gv_dependencies, e2)) {
1382*0Sstevel@tonic-gate 			v2 = e2->ge_vertex;
1383*0Sstevel@tonic-gate 			assert(v2->gv_type == GVT_INST);
1384*0Sstevel@tonic-gate 
1385*0Sstevel@tonic-gate 			if ((v2->gv_flags & GV_CONFIGURED) == 0)
1386*0Sstevel@tonic-gate 				continue;
1387*0Sstevel@tonic-gate 
1388*0Sstevel@tonic-gate 			switch (v2->gv_state) {
1389*0Sstevel@tonic-gate 			case RESTARTER_STATE_ONLINE:
1390*0Sstevel@tonic-gate 			case RESTARTER_STATE_DEGRADED:
1391*0Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v2);
1392*0Sstevel@tonic-gate 				return (v2->gv_flags & GV_ENABLED ? -1 : 0);
1393*0Sstevel@tonic-gate 
1394*0Sstevel@tonic-gate 			case RESTARTER_STATE_OFFLINE:
1395*0Sstevel@tonic-gate 			case RESTARTER_STATE_UNINIT:
1396*0Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v2);
1397*0Sstevel@tonic-gate 				return (0);
1398*0Sstevel@tonic-gate 
1399*0Sstevel@tonic-gate 			case RESTARTER_STATE_DISABLED:
1400*0Sstevel@tonic-gate 			case RESTARTER_STATE_MAINT:
1401*0Sstevel@tonic-gate 				continue;
1402*0Sstevel@tonic-gate 
1403*0Sstevel@tonic-gate 			default:
1404*0Sstevel@tonic-gate #ifndef NDEBUG
1405*0Sstevel@tonic-gate 				uu_warn("%s:%d: Unexpected vertex type %d.\n",
1406*0Sstevel@tonic-gate 				    __FILE__, __LINE__, v2->gv_type);
1407*0Sstevel@tonic-gate #endif
1408*0Sstevel@tonic-gate 				abort();
1409*0Sstevel@tonic-gate 			}
1410*0Sstevel@tonic-gate 		}
1411*0Sstevel@tonic-gate 	}
1412*0Sstevel@tonic-gate 
1413*0Sstevel@tonic-gate 	return (1);
1414*0Sstevel@tonic-gate }
1415*0Sstevel@tonic-gate 
1416*0Sstevel@tonic-gate /*
1417*0Sstevel@tonic-gate  * int instance_satisfied()
1418*0Sstevel@tonic-gate  *   Determine if all the dependencies are satisfied for the supplied instance
1419*0Sstevel@tonic-gate  *   vertex. Return 1 if they are, 0 if they aren't, and -1 if they won't be
1420*0Sstevel@tonic-gate  *   without administrator intervention.
1421*0Sstevel@tonic-gate  */
1422*0Sstevel@tonic-gate static int
1423*0Sstevel@tonic-gate instance_satisfied(graph_vertex_t *v, boolean_t satbility)
1424*0Sstevel@tonic-gate {
1425*0Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
1426*0Sstevel@tonic-gate 	assert(!inst_running(v));
1427*0Sstevel@tonic-gate 
1428*0Sstevel@tonic-gate 	return (require_all_satisfied(v, satbility));
1429*0Sstevel@tonic-gate }
1430*0Sstevel@tonic-gate 
1431*0Sstevel@tonic-gate /*
1432*0Sstevel@tonic-gate  * Decide whether v can satisfy a dependency.  v can either be a child of
1433*0Sstevel@tonic-gate  * a group vertex, or of an instance vertex.
1434*0Sstevel@tonic-gate  */
1435*0Sstevel@tonic-gate static int
1436*0Sstevel@tonic-gate dependency_satisfied(graph_vertex_t *v, boolean_t satbility)
1437*0Sstevel@tonic-gate {
1438*0Sstevel@tonic-gate 	switch (v->gv_type) {
1439*0Sstevel@tonic-gate 	case GVT_INST:
1440*0Sstevel@tonic-gate 		if ((v->gv_flags & GV_CONFIGURED) == 0)
1441*0Sstevel@tonic-gate 			return (-1);
1442*0Sstevel@tonic-gate 
1443*0Sstevel@tonic-gate 		switch (v->gv_state) {
1444*0Sstevel@tonic-gate 		case RESTARTER_STATE_ONLINE:
1445*0Sstevel@tonic-gate 		case RESTARTER_STATE_DEGRADED:
1446*0Sstevel@tonic-gate 			return (1);
1447*0Sstevel@tonic-gate 
1448*0Sstevel@tonic-gate 		case RESTARTER_STATE_OFFLINE:
1449*0Sstevel@tonic-gate 			if (!satbility)
1450*0Sstevel@tonic-gate 				return (0);
1451*0Sstevel@tonic-gate 			return (instance_satisfied(v, satbility) != -1 ?
1452*0Sstevel@tonic-gate 			    0 : -1);
1453*0Sstevel@tonic-gate 
1454*0Sstevel@tonic-gate 		case RESTARTER_STATE_DISABLED:
1455*0Sstevel@tonic-gate 		case RESTARTER_STATE_MAINT:
1456*0Sstevel@tonic-gate 			return (-1);
1457*0Sstevel@tonic-gate 
1458*0Sstevel@tonic-gate 		case RESTARTER_STATE_UNINIT:
1459*0Sstevel@tonic-gate 			return (0);
1460*0Sstevel@tonic-gate 
1461*0Sstevel@tonic-gate 		default:
1462*0Sstevel@tonic-gate #ifndef NDEBUG
1463*0Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex state %d.\n",
1464*0Sstevel@tonic-gate 			    __FILE__, __LINE__, v->gv_state);
1465*0Sstevel@tonic-gate #endif
1466*0Sstevel@tonic-gate 			abort();
1467*0Sstevel@tonic-gate 			/* NOTREACHED */
1468*0Sstevel@tonic-gate 		}
1469*0Sstevel@tonic-gate 
1470*0Sstevel@tonic-gate 	case GVT_SVC:
1471*0Sstevel@tonic-gate 		if (uu_list_numnodes(v->gv_dependencies) == 0)
1472*0Sstevel@tonic-gate 			return (-1);
1473*0Sstevel@tonic-gate 		return (require_any_satisfied(v, satbility));
1474*0Sstevel@tonic-gate 
1475*0Sstevel@tonic-gate 	case GVT_FILE:
1476*0Sstevel@tonic-gate 		/* i.e., we assume files will not be automatically generated */
1477*0Sstevel@tonic-gate 		return (file_ready(v) ? 1 : -1);
1478*0Sstevel@tonic-gate 
1479*0Sstevel@tonic-gate 	case GVT_GROUP:
1480*0Sstevel@tonic-gate 		break;
1481*0Sstevel@tonic-gate 
1482*0Sstevel@tonic-gate 	default:
1483*0Sstevel@tonic-gate #ifndef NDEBUG
1484*0Sstevel@tonic-gate 		uu_warn("%s:%d: Unexpected node type %d.\n", __FILE__, __LINE__,
1485*0Sstevel@tonic-gate 		    v->gv_type);
1486*0Sstevel@tonic-gate #endif
1487*0Sstevel@tonic-gate 		abort();
1488*0Sstevel@tonic-gate 		/* NOTREACHED */
1489*0Sstevel@tonic-gate 	}
1490*0Sstevel@tonic-gate 
1491*0Sstevel@tonic-gate 	switch (v->gv_depgroup) {
1492*0Sstevel@tonic-gate 	case DEPGRP_REQUIRE_ANY:
1493*0Sstevel@tonic-gate 		return (require_any_satisfied(v, satbility));
1494*0Sstevel@tonic-gate 
1495*0Sstevel@tonic-gate 	case DEPGRP_REQUIRE_ALL:
1496*0Sstevel@tonic-gate 		return (require_all_satisfied(v, satbility));
1497*0Sstevel@tonic-gate 
1498*0Sstevel@tonic-gate 	case DEPGRP_OPTIONAL_ALL:
1499*0Sstevel@tonic-gate 		return (optional_all_satisfied(v, satbility));
1500*0Sstevel@tonic-gate 
1501*0Sstevel@tonic-gate 	case DEPGRP_EXCLUDE_ALL:
1502*0Sstevel@tonic-gate 		return (exclude_all_satisfied(v, satbility));
1503*0Sstevel@tonic-gate 
1504*0Sstevel@tonic-gate 	default:
1505*0Sstevel@tonic-gate #ifndef NDEBUG
1506*0Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown dependency grouping %d.\n", __FILE__,
1507*0Sstevel@tonic-gate 		    __LINE__, v->gv_depgroup);
1508*0Sstevel@tonic-gate #endif
1509*0Sstevel@tonic-gate 		abort();
1510*0Sstevel@tonic-gate 	}
1511*0Sstevel@tonic-gate }
1512*0Sstevel@tonic-gate 
1513*0Sstevel@tonic-gate static void
1514*0Sstevel@tonic-gate start_if_satisfied(graph_vertex_t *v)
1515*0Sstevel@tonic-gate {
1516*0Sstevel@tonic-gate 	if (v->gv_state == RESTARTER_STATE_OFFLINE &&
1517*0Sstevel@tonic-gate 	    instance_satisfied(v, B_FALSE) == 1) {
1518*0Sstevel@tonic-gate 		if (v->gv_start_f == NULL)
1519*0Sstevel@tonic-gate 			vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
1520*0Sstevel@tonic-gate 		else
1521*0Sstevel@tonic-gate 			v->gv_start_f(v);
1522*0Sstevel@tonic-gate 	}
1523*0Sstevel@tonic-gate }
1524*0Sstevel@tonic-gate 
1525*0Sstevel@tonic-gate /*
1526*0Sstevel@tonic-gate  * propagate_satbility()
1527*0Sstevel@tonic-gate  *
1528*0Sstevel@tonic-gate  * This function is used when the given vertex changes state in such a way that
1529*0Sstevel@tonic-gate  * one of its dependents may become unsatisfiable.  This happens when an
1530*0Sstevel@tonic-gate  * instance transitions between offline -> online, or from !running ->
1531*0Sstevel@tonic-gate  * maintenance, as well as when an instance is removed from the graph.
1532*0Sstevel@tonic-gate  *
1533*0Sstevel@tonic-gate  * We have to walk the all dependents, since optional_all dependencies several
1534*0Sstevel@tonic-gate  * levels up could become (un)satisfied, instead of unsatisfiable.  For example,
1535*0Sstevel@tonic-gate  *
1536*0Sstevel@tonic-gate  *	+-----+  optional_all  +-----+  require_all  +-----+
1537*0Sstevel@tonic-gate  *	|  A  |--------------->|  B  |-------------->|  C  |
1538*0Sstevel@tonic-gate  *	+-----+                +-----+               +-----+
1539*0Sstevel@tonic-gate  *
1540*0Sstevel@tonic-gate  *	                                        offline -> maintenance
1541*0Sstevel@tonic-gate  *
1542*0Sstevel@tonic-gate  * If C goes into maintenance, it's not enough simply to check B.  Because A has
1543*0Sstevel@tonic-gate  * an optional dependency, what was previously an unsatisfiable situation is now
1544*0Sstevel@tonic-gate  * satisfied (B will never come online, even though its state hasn't changed).
1545*0Sstevel@tonic-gate  *
1546*0Sstevel@tonic-gate  * Note that it's not necessary to continue examining dependents after reaching
1547*0Sstevel@tonic-gate  * an optional_all dependency.  It's not possible for an optional_all dependency
1548*0Sstevel@tonic-gate  * to change satisfiability without also coming online, in which case we get a
1549*0Sstevel@tonic-gate  * start event and propagation continues naturally.  However, it does no harm to
1550*0Sstevel@tonic-gate  * continue propagating satisfiability (as it is a relatively rare event), and
1551*0Sstevel@tonic-gate  * keeps the walker code simple and generic.
1552*0Sstevel@tonic-gate  */
1553*0Sstevel@tonic-gate /*ARGSUSED*/
1554*0Sstevel@tonic-gate static int
1555*0Sstevel@tonic-gate satbility_cb(graph_vertex_t *v, void *arg)
1556*0Sstevel@tonic-gate {
1557*0Sstevel@tonic-gate 	if (v->gv_type == GVT_INST)
1558*0Sstevel@tonic-gate 		start_if_satisfied(v);
1559*0Sstevel@tonic-gate 
1560*0Sstevel@tonic-gate 	return (UU_WALK_NEXT);
1561*0Sstevel@tonic-gate }
1562*0Sstevel@tonic-gate 
1563*0Sstevel@tonic-gate static void
1564*0Sstevel@tonic-gate propagate_satbility(graph_vertex_t *v)
1565*0Sstevel@tonic-gate {
1566*0Sstevel@tonic-gate 	graph_walk(v, WALK_DEPENDENTS, satbility_cb, NULL, NULL);
1567*0Sstevel@tonic-gate }
1568*0Sstevel@tonic-gate 
1569*0Sstevel@tonic-gate static void propagate_stop(graph_vertex_t *, void *);
1570*0Sstevel@tonic-gate 
1571*0Sstevel@tonic-gate /* ARGSUSED */
1572*0Sstevel@tonic-gate static void
1573*0Sstevel@tonic-gate propagate_start(graph_vertex_t *v, void *arg)
1574*0Sstevel@tonic-gate {
1575*0Sstevel@tonic-gate 	switch (v->gv_type) {
1576*0Sstevel@tonic-gate 	case GVT_INST:
1577*0Sstevel@tonic-gate 		start_if_satisfied(v);
1578*0Sstevel@tonic-gate 		break;
1579*0Sstevel@tonic-gate 
1580*0Sstevel@tonic-gate 	case GVT_GROUP:
1581*0Sstevel@tonic-gate 		if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
1582*0Sstevel@tonic-gate 			graph_walk_dependents(v, propagate_stop,
1583*0Sstevel@tonic-gate 			    (void *)RERR_RESTART);
1584*0Sstevel@tonic-gate 			break;
1585*0Sstevel@tonic-gate 		}
1586*0Sstevel@tonic-gate 		/* FALLTHROUGH */
1587*0Sstevel@tonic-gate 
1588*0Sstevel@tonic-gate 	case GVT_SVC:
1589*0Sstevel@tonic-gate 		graph_walk_dependents(v, propagate_start, NULL);
1590*0Sstevel@tonic-gate 		break;
1591*0Sstevel@tonic-gate 
1592*0Sstevel@tonic-gate 	case GVT_FILE:
1593*0Sstevel@tonic-gate #ifndef NDEBUG
1594*0Sstevel@tonic-gate 		uu_warn("%s:%d: propagate_start() encountered GVT_FILE.\n",
1595*0Sstevel@tonic-gate 		    __FILE__, __LINE__);
1596*0Sstevel@tonic-gate #endif
1597*0Sstevel@tonic-gate 		abort();
1598*0Sstevel@tonic-gate 		/* NOTREACHED */
1599*0Sstevel@tonic-gate 
1600*0Sstevel@tonic-gate 	default:
1601*0Sstevel@tonic-gate #ifndef NDEBUG
1602*0Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__,
1603*0Sstevel@tonic-gate 		    v->gv_type);
1604*0Sstevel@tonic-gate #endif
1605*0Sstevel@tonic-gate 		abort();
1606*0Sstevel@tonic-gate 	}
1607*0Sstevel@tonic-gate }
1608*0Sstevel@tonic-gate 
1609*0Sstevel@tonic-gate static void
1610*0Sstevel@tonic-gate propagate_stop(graph_vertex_t *v, void *arg)
1611*0Sstevel@tonic-gate {
1612*0Sstevel@tonic-gate 	graph_edge_t *e;
1613*0Sstevel@tonic-gate 	graph_vertex_t *svc;
1614*0Sstevel@tonic-gate 	restarter_error_t err = (restarter_error_t)arg;
1615*0Sstevel@tonic-gate 
1616*0Sstevel@tonic-gate 	switch (v->gv_type) {
1617*0Sstevel@tonic-gate 	case GVT_INST:
1618*0Sstevel@tonic-gate 		/* Restarter */
1619*0Sstevel@tonic-gate 		if (err > RERR_NONE && inst_running(v))
1620*0Sstevel@tonic-gate 			vertex_send_event(v, RESTARTER_EVENT_TYPE_STOP);
1621*0Sstevel@tonic-gate 		break;
1622*0Sstevel@tonic-gate 
1623*0Sstevel@tonic-gate 	case GVT_SVC:
1624*0Sstevel@tonic-gate 		graph_walk_dependents(v, propagate_stop, arg);
1625*0Sstevel@tonic-gate 		break;
1626*0Sstevel@tonic-gate 
1627*0Sstevel@tonic-gate 	case GVT_FILE:
1628*0Sstevel@tonic-gate #ifndef NDEBUG
1629*0Sstevel@tonic-gate 		uu_warn("%s:%d: propagate_stop() encountered GVT_FILE.\n",
1630*0Sstevel@tonic-gate 		    __FILE__, __LINE__);
1631*0Sstevel@tonic-gate #endif
1632*0Sstevel@tonic-gate 		abort();
1633*0Sstevel@tonic-gate 		/* NOTREACHED */
1634*0Sstevel@tonic-gate 
1635*0Sstevel@tonic-gate 	case GVT_GROUP:
1636*0Sstevel@tonic-gate 		if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
1637*0Sstevel@tonic-gate 			graph_walk_dependents(v, propagate_start, NULL);
1638*0Sstevel@tonic-gate 			break;
1639*0Sstevel@tonic-gate 		}
1640*0Sstevel@tonic-gate 
1641*0Sstevel@tonic-gate 		if (err == RERR_NONE || err > v->gv_restart)
1642*0Sstevel@tonic-gate 			break;
1643*0Sstevel@tonic-gate 
1644*0Sstevel@tonic-gate 		assert(uu_list_numnodes(v->gv_dependents) == 1);
1645*0Sstevel@tonic-gate 		e = uu_list_first(v->gv_dependents);
1646*0Sstevel@tonic-gate 		svc = e->ge_vertex;
1647*0Sstevel@tonic-gate 
1648*0Sstevel@tonic-gate 		if (inst_running(svc))
1649*0Sstevel@tonic-gate 			vertex_send_event(svc, RESTARTER_EVENT_TYPE_STOP);
1650*0Sstevel@tonic-gate 		break;
1651*0Sstevel@tonic-gate 
1652*0Sstevel@tonic-gate 	default:
1653*0Sstevel@tonic-gate #ifndef NDEBUG
1654*0Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__,
1655*0Sstevel@tonic-gate 		    v->gv_type);
1656*0Sstevel@tonic-gate #endif
1657*0Sstevel@tonic-gate 		abort();
1658*0Sstevel@tonic-gate 	}
1659*0Sstevel@tonic-gate }
1660*0Sstevel@tonic-gate 
1661*0Sstevel@tonic-gate /*
1662*0Sstevel@tonic-gate  * void graph_enable_by_vertex()
1663*0Sstevel@tonic-gate  *   If admin is non-zero, this is an administrative request for change
1664*0Sstevel@tonic-gate  *   of the enabled property.  Thus, send the ADMIN_DISABLE rather than
1665*0Sstevel@tonic-gate  *   a plain DISABLE restarter event.
1666*0Sstevel@tonic-gate  */
1667*0Sstevel@tonic-gate static void
1668*0Sstevel@tonic-gate graph_enable_by_vertex(graph_vertex_t *vertex, int enable, int admin)
1669*0Sstevel@tonic-gate {
1670*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
1671*0Sstevel@tonic-gate 	assert((vertex->gv_flags & GV_CONFIGURED));
1672*0Sstevel@tonic-gate 
1673*0Sstevel@tonic-gate 	vertex->gv_flags = (vertex->gv_flags & ~GV_ENABLED) |
1674*0Sstevel@tonic-gate 	    (enable ? GV_ENABLED : 0);
1675*0Sstevel@tonic-gate 
1676*0Sstevel@tonic-gate 	if (enable) {
1677*0Sstevel@tonic-gate 		if (vertex->gv_state != RESTARTER_STATE_OFFLINE &&
1678*0Sstevel@tonic-gate 		    vertex->gv_state != RESTARTER_STATE_DEGRADED &&
1679*0Sstevel@tonic-gate 		    vertex->gv_state != RESTARTER_STATE_ONLINE)
1680*0Sstevel@tonic-gate 			vertex_send_event(vertex, RESTARTER_EVENT_TYPE_ENABLE);
1681*0Sstevel@tonic-gate 	} else {
1682*0Sstevel@tonic-gate 		if (vertex->gv_state != RESTARTER_STATE_DISABLED) {
1683*0Sstevel@tonic-gate 			if (admin)
1684*0Sstevel@tonic-gate 				vertex_send_event(vertex,
1685*0Sstevel@tonic-gate 				    RESTARTER_EVENT_TYPE_ADMIN_DISABLE);
1686*0Sstevel@tonic-gate 			else
1687*0Sstevel@tonic-gate 				vertex_send_event(vertex,
1688*0Sstevel@tonic-gate 				    RESTARTER_EVENT_TYPE_DISABLE);
1689*0Sstevel@tonic-gate 		}
1690*0Sstevel@tonic-gate 	}
1691*0Sstevel@tonic-gate 
1692*0Sstevel@tonic-gate 	/*
1693*0Sstevel@tonic-gate 	 * Wait for state update from restarter before sending _START or
1694*0Sstevel@tonic-gate 	 * _STOP.
1695*0Sstevel@tonic-gate 	 */
1696*0Sstevel@tonic-gate }
1697*0Sstevel@tonic-gate 
1698*0Sstevel@tonic-gate static int configure_vertex(graph_vertex_t *, scf_instance_t *);
1699*0Sstevel@tonic-gate 
1700*0Sstevel@tonic-gate /*
1701*0Sstevel@tonic-gate  * Set the restarter for v to fmri_arg.  That is, make sure a vertex for
1702*0Sstevel@tonic-gate  * fmri_arg exists, make v depend on it, and send _ADD_INSTANCE for v.  If
1703*0Sstevel@tonic-gate  * v is already configured and fmri_arg indicates the current restarter, do
1704*0Sstevel@tonic-gate  * nothing.  If v is configured and fmri_arg is a new restarter, delete v's
1705*0Sstevel@tonic-gate  * dependency on the restarter, send _REMOVE_INSTANCE for v, and set the new
1706*0Sstevel@tonic-gate  * restarter.  Returns 0 on success, EINVAL if the FMRI is invalid,
1707*0Sstevel@tonic-gate  * ECONNABORTED if the repository connection is broken, and ELOOP
1708*0Sstevel@tonic-gate  * if the dependency would create a cycle.  In the last case, *pathp will
1709*0Sstevel@tonic-gate  * point to a -1-terminated array of ids which compose the path from v to
1710*0Sstevel@tonic-gate  * restarter_fmri.
1711*0Sstevel@tonic-gate  */
1712*0Sstevel@tonic-gate int
1713*0Sstevel@tonic-gate graph_change_restarter(graph_vertex_t *v, const char *fmri_arg, scf_handle_t *h,
1714*0Sstevel@tonic-gate     int **pathp)
1715*0Sstevel@tonic-gate {
1716*0Sstevel@tonic-gate 	char *restarter_fmri = NULL;
1717*0Sstevel@tonic-gate 	graph_vertex_t *rv;
1718*0Sstevel@tonic-gate 	int err;
1719*0Sstevel@tonic-gate 	int id;
1720*0Sstevel@tonic-gate 
1721*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
1722*0Sstevel@tonic-gate 
1723*0Sstevel@tonic-gate 	if (fmri_arg[0] != '\0') {
1724*0Sstevel@tonic-gate 		err = fmri_canonify(fmri_arg, &restarter_fmri, B_TRUE);
1725*0Sstevel@tonic-gate 		if (err != 0) {
1726*0Sstevel@tonic-gate 			assert(err == EINVAL);
1727*0Sstevel@tonic-gate 			return (err);
1728*0Sstevel@tonic-gate 		}
1729*0Sstevel@tonic-gate 	}
1730*0Sstevel@tonic-gate 
1731*0Sstevel@tonic-gate 	if (restarter_fmri == NULL ||
1732*0Sstevel@tonic-gate 	    strcmp(restarter_fmri, SCF_SERVICE_STARTD) == 0) {
1733*0Sstevel@tonic-gate 		if (v->gv_flags & GV_CONFIGURED) {
1734*0Sstevel@tonic-gate 			if (v->gv_restarter_id == -1) {
1735*0Sstevel@tonic-gate 				if (restarter_fmri != NULL)
1736*0Sstevel@tonic-gate 					startd_free(restarter_fmri,
1737*0Sstevel@tonic-gate 					    max_scf_fmri_size);
1738*0Sstevel@tonic-gate 				return (0);
1739*0Sstevel@tonic-gate 			}
1740*0Sstevel@tonic-gate 
1741*0Sstevel@tonic-gate 			graph_unset_restarter(v);
1742*0Sstevel@tonic-gate 		}
1743*0Sstevel@tonic-gate 
1744*0Sstevel@tonic-gate 		/* Master restarter, nothing to do. */
1745*0Sstevel@tonic-gate 		v->gv_restarter_id = -1;
1746*0Sstevel@tonic-gate 		v->gv_restarter_channel = NULL;
1747*0Sstevel@tonic-gate 		vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE);
1748*0Sstevel@tonic-gate 		return (0);
1749*0Sstevel@tonic-gate 	}
1750*0Sstevel@tonic-gate 
1751*0Sstevel@tonic-gate 	if (v->gv_flags & GV_CONFIGURED) {
1752*0Sstevel@tonic-gate 		id = dict_lookup_byname(restarter_fmri);
1753*0Sstevel@tonic-gate 		if (id != -1 && v->gv_restarter_id == id) {
1754*0Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_fmri_size);
1755*0Sstevel@tonic-gate 			return (0);
1756*0Sstevel@tonic-gate 		}
1757*0Sstevel@tonic-gate 
1758*0Sstevel@tonic-gate 		graph_unset_restarter(v);
1759*0Sstevel@tonic-gate 	}
1760*0Sstevel@tonic-gate 
1761*0Sstevel@tonic-gate 	err = graph_insert_vertex_unconfigured(restarter_fmri, GVT_INST, 0,
1762*0Sstevel@tonic-gate 	    RERR_NONE, &rv);
1763*0Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_fmri_size);
1764*0Sstevel@tonic-gate 	assert(err == 0 || err == EEXIST);
1765*0Sstevel@tonic-gate 
1766*0Sstevel@tonic-gate 	if (rv->gv_delegate_initialized == 0) {
1767*0Sstevel@tonic-gate 		rv->gv_delegate_channel = restarter_protocol_init_delegate(
1768*0Sstevel@tonic-gate 		    rv->gv_name);
1769*0Sstevel@tonic-gate 		rv->gv_delegate_initialized = 1;
1770*0Sstevel@tonic-gate 	}
1771*0Sstevel@tonic-gate 	v->gv_restarter_id = rv->gv_id;
1772*0Sstevel@tonic-gate 	v->gv_restarter_channel = rv->gv_delegate_channel;
1773*0Sstevel@tonic-gate 
1774*0Sstevel@tonic-gate 	err = graph_insert_dependency(v, rv, pathp);
1775*0Sstevel@tonic-gate 	if (err != 0) {
1776*0Sstevel@tonic-gate 		assert(err == ELOOP);
1777*0Sstevel@tonic-gate 		return (ELOOP);
1778*0Sstevel@tonic-gate 	}
1779*0Sstevel@tonic-gate 
1780*0Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE);
1781*0Sstevel@tonic-gate 
1782*0Sstevel@tonic-gate 	if (!(rv->gv_flags & GV_CONFIGURED)) {
1783*0Sstevel@tonic-gate 		scf_instance_t *inst;
1784*0Sstevel@tonic-gate 
1785*0Sstevel@tonic-gate 		err = libscf_fmri_get_instance(h, rv->gv_name, &inst);
1786*0Sstevel@tonic-gate 		switch (err) {
1787*0Sstevel@tonic-gate 		case 0:
1788*0Sstevel@tonic-gate 			err = configure_vertex(rv, inst);
1789*0Sstevel@tonic-gate 			scf_instance_destroy(inst);
1790*0Sstevel@tonic-gate 			switch (err) {
1791*0Sstevel@tonic-gate 			case 0:
1792*0Sstevel@tonic-gate 			case ECANCELED:
1793*0Sstevel@tonic-gate 				break;
1794*0Sstevel@tonic-gate 
1795*0Sstevel@tonic-gate 			case ECONNABORTED:
1796*0Sstevel@tonic-gate 				return (ECONNABORTED);
1797*0Sstevel@tonic-gate 
1798*0Sstevel@tonic-gate 			default:
1799*0Sstevel@tonic-gate 				bad_error("configure_vertex", err);
1800*0Sstevel@tonic-gate 			}
1801*0Sstevel@tonic-gate 			break;
1802*0Sstevel@tonic-gate 
1803*0Sstevel@tonic-gate 		case ECONNABORTED:
1804*0Sstevel@tonic-gate 			return (ECONNABORTED);
1805*0Sstevel@tonic-gate 
1806*0Sstevel@tonic-gate 		case ENOENT:
1807*0Sstevel@tonic-gate 			break;
1808*0Sstevel@tonic-gate 
1809*0Sstevel@tonic-gate 		case ENOTSUP:
1810*0Sstevel@tonic-gate 			/*
1811*0Sstevel@tonic-gate 			 * The fmri doesn't specify an instance - translate
1812*0Sstevel@tonic-gate 			 * to EINVAL.
1813*0Sstevel@tonic-gate 			 */
1814*0Sstevel@tonic-gate 			return (EINVAL);
1815*0Sstevel@tonic-gate 
1816*0Sstevel@tonic-gate 		case EINVAL:
1817*0Sstevel@tonic-gate 		default:
1818*0Sstevel@tonic-gate 			bad_error("libscf_fmri_get_instance", err);
1819*0Sstevel@tonic-gate 		}
1820*0Sstevel@tonic-gate 	}
1821*0Sstevel@tonic-gate 
1822*0Sstevel@tonic-gate 	return (0);
1823*0Sstevel@tonic-gate }
1824*0Sstevel@tonic-gate 
1825*0Sstevel@tonic-gate 
1826*0Sstevel@tonic-gate /*
1827*0Sstevel@tonic-gate  * Add all of the instances of the service named by fmri to the graph.
1828*0Sstevel@tonic-gate  * Returns
1829*0Sstevel@tonic-gate  *   0 - success
1830*0Sstevel@tonic-gate  *   ENOENT - service indicated by fmri does not exist
1831*0Sstevel@tonic-gate  *
1832*0Sstevel@tonic-gate  * In both cases *reboundp will be B_TRUE if the handle was rebound, or B_FALSE
1833*0Sstevel@tonic-gate  * otherwise.
1834*0Sstevel@tonic-gate  */
1835*0Sstevel@tonic-gate static int
1836*0Sstevel@tonic-gate add_service(const char *fmri, scf_handle_t *h, boolean_t *reboundp)
1837*0Sstevel@tonic-gate {
1838*0Sstevel@tonic-gate 	scf_service_t *svc;
1839*0Sstevel@tonic-gate 	scf_instance_t *inst;
1840*0Sstevel@tonic-gate 	scf_iter_t *iter;
1841*0Sstevel@tonic-gate 	char *inst_fmri;
1842*0Sstevel@tonic-gate 	int ret, r;
1843*0Sstevel@tonic-gate 
1844*0Sstevel@tonic-gate 	*reboundp = B_FALSE;
1845*0Sstevel@tonic-gate 
1846*0Sstevel@tonic-gate 	svc = safe_scf_service_create(h);
1847*0Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
1848*0Sstevel@tonic-gate 	iter = safe_scf_iter_create(h);
1849*0Sstevel@tonic-gate 	inst_fmri = startd_alloc(max_scf_fmri_size);
1850*0Sstevel@tonic-gate 
1851*0Sstevel@tonic-gate rebound:
1852*0Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
1853*0Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) != 0) {
1854*0Sstevel@tonic-gate 		switch (scf_error()) {
1855*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
1856*0Sstevel@tonic-gate 		default:
1857*0Sstevel@tonic-gate 			libscf_handle_rebind(h);
1858*0Sstevel@tonic-gate 			*reboundp = B_TRUE;
1859*0Sstevel@tonic-gate 			goto rebound;
1860*0Sstevel@tonic-gate 
1861*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
1862*0Sstevel@tonic-gate 			ret = ENOENT;
1863*0Sstevel@tonic-gate 			goto out;
1864*0Sstevel@tonic-gate 
1865*0Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
1866*0Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
1867*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
1868*0Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
1869*0Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
1870*0Sstevel@tonic-gate 		}
1871*0Sstevel@tonic-gate 	}
1872*0Sstevel@tonic-gate 
1873*0Sstevel@tonic-gate 	if (scf_iter_service_instances(iter, svc) != 0) {
1874*0Sstevel@tonic-gate 		switch (scf_error()) {
1875*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
1876*0Sstevel@tonic-gate 		default:
1877*0Sstevel@tonic-gate 			libscf_handle_rebind(h);
1878*0Sstevel@tonic-gate 			*reboundp = B_TRUE;
1879*0Sstevel@tonic-gate 			goto rebound;
1880*0Sstevel@tonic-gate 
1881*0Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
1882*0Sstevel@tonic-gate 			ret = ENOENT;
1883*0Sstevel@tonic-gate 			goto out;
1884*0Sstevel@tonic-gate 
1885*0Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
1886*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
1887*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
1888*0Sstevel@tonic-gate 			bad_error("scf_iter_service_instances", scf_error())
1889*0Sstevel@tonic-gate 		}
1890*0Sstevel@tonic-gate 	}
1891*0Sstevel@tonic-gate 
1892*0Sstevel@tonic-gate 	for (;;) {
1893*0Sstevel@tonic-gate 		r = scf_iter_next_instance(iter, inst);
1894*0Sstevel@tonic-gate 		if (r == 0)
1895*0Sstevel@tonic-gate 			break;
1896*0Sstevel@tonic-gate 		if (r != 1) {
1897*0Sstevel@tonic-gate 			switch (scf_error()) {
1898*0Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
1899*0Sstevel@tonic-gate 			default:
1900*0Sstevel@tonic-gate 				libscf_handle_rebind(h);
1901*0Sstevel@tonic-gate 				*reboundp = B_TRUE;
1902*0Sstevel@tonic-gate 				goto rebound;
1903*0Sstevel@tonic-gate 
1904*0Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
1905*0Sstevel@tonic-gate 				ret = ENOENT;
1906*0Sstevel@tonic-gate 				goto out;
1907*0Sstevel@tonic-gate 
1908*0Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
1909*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
1910*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
1911*0Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
1912*0Sstevel@tonic-gate 				bad_error("scf_iter_next_instance",
1913*0Sstevel@tonic-gate 				    scf_error());
1914*0Sstevel@tonic-gate 			}
1915*0Sstevel@tonic-gate 		}
1916*0Sstevel@tonic-gate 
1917*0Sstevel@tonic-gate 		if (scf_instance_to_fmri(inst, inst_fmri, max_scf_fmri_size) <
1918*0Sstevel@tonic-gate 		    0) {
1919*0Sstevel@tonic-gate 			switch (scf_error()) {
1920*0Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
1921*0Sstevel@tonic-gate 				libscf_handle_rebind(h);
1922*0Sstevel@tonic-gate 				*reboundp = B_TRUE;
1923*0Sstevel@tonic-gate 				goto rebound;
1924*0Sstevel@tonic-gate 
1925*0Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
1926*0Sstevel@tonic-gate 				continue;
1927*0Sstevel@tonic-gate 
1928*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
1929*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
1930*0Sstevel@tonic-gate 				bad_error("scf_instance_to_fmri", scf_error());
1931*0Sstevel@tonic-gate 			}
1932*0Sstevel@tonic-gate 		}
1933*0Sstevel@tonic-gate 
1934*0Sstevel@tonic-gate 		r = dgraph_add_instance(inst_fmri, inst, B_FALSE);
1935*0Sstevel@tonic-gate 		switch (r) {
1936*0Sstevel@tonic-gate 		case 0:
1937*0Sstevel@tonic-gate 		case ECANCELED:
1938*0Sstevel@tonic-gate 			break;
1939*0Sstevel@tonic-gate 
1940*0Sstevel@tonic-gate 		case EEXIST:
1941*0Sstevel@tonic-gate 			continue;
1942*0Sstevel@tonic-gate 
1943*0Sstevel@tonic-gate 		case ECONNABORTED:
1944*0Sstevel@tonic-gate 			libscf_handle_rebind(h);
1945*0Sstevel@tonic-gate 			*reboundp = B_TRUE;
1946*0Sstevel@tonic-gate 			goto rebound;
1947*0Sstevel@tonic-gate 
1948*0Sstevel@tonic-gate 		case EINVAL:
1949*0Sstevel@tonic-gate 		default:
1950*0Sstevel@tonic-gate 			bad_error("dgraph_add_instance", r);
1951*0Sstevel@tonic-gate 		}
1952*0Sstevel@tonic-gate 	}
1953*0Sstevel@tonic-gate 
1954*0Sstevel@tonic-gate 	ret = 0;
1955*0Sstevel@tonic-gate 
1956*0Sstevel@tonic-gate out:
1957*0Sstevel@tonic-gate 	startd_free(inst_fmri, max_scf_fmri_size);
1958*0Sstevel@tonic-gate 	scf_iter_destroy(iter);
1959*0Sstevel@tonic-gate 	scf_instance_destroy(inst);
1960*0Sstevel@tonic-gate 	scf_service_destroy(svc);
1961*0Sstevel@tonic-gate 	return (ret);
1962*0Sstevel@tonic-gate }
1963*0Sstevel@tonic-gate 
1964*0Sstevel@tonic-gate struct depfmri_info {
1965*0Sstevel@tonic-gate 	graph_vertex_t	*v;		/* GVT_GROUP vertex */
1966*0Sstevel@tonic-gate 	gv_type_t	type;		/* type of dependency */
1967*0Sstevel@tonic-gate 	const char	*inst_fmri;	/* FMRI of parental GVT_INST vert. */
1968*0Sstevel@tonic-gate 	const char	*pg_name;	/* Name of dependency pg */
1969*0Sstevel@tonic-gate 	scf_handle_t	*h;
1970*0Sstevel@tonic-gate 	int		err;		/* return error code */
1971*0Sstevel@tonic-gate 	int		**pathp;	/* return circular dependency path */
1972*0Sstevel@tonic-gate };
1973*0Sstevel@tonic-gate 
1974*0Sstevel@tonic-gate /*
1975*0Sstevel@tonic-gate  * Find or create a vertex for fmri and make info->v depend on it.
1976*0Sstevel@tonic-gate  * Returns
1977*0Sstevel@tonic-gate  *   0 - success
1978*0Sstevel@tonic-gate  *   nonzero - failure
1979*0Sstevel@tonic-gate  *
1980*0Sstevel@tonic-gate  * On failure, sets info->err to
1981*0Sstevel@tonic-gate  *   EINVAL - fmri is invalid
1982*0Sstevel@tonic-gate  *	      fmri does not match info->type
1983*0Sstevel@tonic-gate  *   ELOOP - Adding the dependency creates a circular dependency.  *info->pathp
1984*0Sstevel@tonic-gate  *	     will point to an array of the ids of the members of the cycle.
1985*0Sstevel@tonic-gate  *   ECONNABORTED - repository connection was broken
1986*0Sstevel@tonic-gate  *   ECONNRESET - succeeded, but repository connection was reset
1987*0Sstevel@tonic-gate  */
1988*0Sstevel@tonic-gate static int
1989*0Sstevel@tonic-gate process_dependency_fmri(const char *fmri, struct depfmri_info *info)
1990*0Sstevel@tonic-gate {
1991*0Sstevel@tonic-gate 	int err;
1992*0Sstevel@tonic-gate 	graph_vertex_t *depgroup_v, *v;
1993*0Sstevel@tonic-gate 	char *fmri_copy, *cfmri;
1994*0Sstevel@tonic-gate 	size_t fmri_copy_sz;
1995*0Sstevel@tonic-gate 	const char *scope, *service, *instance, *pg;
1996*0Sstevel@tonic-gate 	scf_instance_t *inst;
1997*0Sstevel@tonic-gate 	boolean_t rebound;
1998*0Sstevel@tonic-gate 
1999*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
2000*0Sstevel@tonic-gate 
2001*0Sstevel@tonic-gate 	/* Get or create vertex for FMRI */
2002*0Sstevel@tonic-gate 	depgroup_v = info->v;
2003*0Sstevel@tonic-gate 
2004*0Sstevel@tonic-gate 	if (strncmp(fmri, "file:", sizeof ("file:") - 1) == 0) {
2005*0Sstevel@tonic-gate 		if (info->type != GVT_FILE) {
2006*0Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
2007*0Sstevel@tonic-gate 			    "FMRI \"%s\" is not allowed for the \"%s\" "
2008*0Sstevel@tonic-gate 			    "dependency's type of instance %s.\n", fmri,
2009*0Sstevel@tonic-gate 			    info->pg_name, info->inst_fmri);
2010*0Sstevel@tonic-gate 			return (info->err = EINVAL);
2011*0Sstevel@tonic-gate 		}
2012*0Sstevel@tonic-gate 
2013*0Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(fmri, info->type, 0,
2014*0Sstevel@tonic-gate 		    RERR_NONE, &v);
2015*0Sstevel@tonic-gate 		switch (err) {
2016*0Sstevel@tonic-gate 		case 0:
2017*0Sstevel@tonic-gate 			break;
2018*0Sstevel@tonic-gate 
2019*0Sstevel@tonic-gate 		case EEXIST:
2020*0Sstevel@tonic-gate 			assert(v->gv_type == GVT_FILE);
2021*0Sstevel@tonic-gate 			break;
2022*0Sstevel@tonic-gate 
2023*0Sstevel@tonic-gate 		case EINVAL:		/* prevented above */
2024*0Sstevel@tonic-gate 		default:
2025*0Sstevel@tonic-gate 			bad_error("graph_insert_vertex_unconfigured", err);
2026*0Sstevel@tonic-gate 		}
2027*0Sstevel@tonic-gate 	} else {
2028*0Sstevel@tonic-gate 		if (info->type != GVT_INST) {
2029*0Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
2030*0Sstevel@tonic-gate 			    "FMRI \"%s\" is not allowed for the \"%s\" "
2031*0Sstevel@tonic-gate 			    "dependency's type of instance %s.\n", fmri,
2032*0Sstevel@tonic-gate 			    info->pg_name, info->inst_fmri);
2033*0Sstevel@tonic-gate 			return (info->err = EINVAL);
2034*0Sstevel@tonic-gate 		}
2035*0Sstevel@tonic-gate 
2036*0Sstevel@tonic-gate 		/*
2037*0Sstevel@tonic-gate 		 * We must canonify fmri & add a vertex for it.
2038*0Sstevel@tonic-gate 		 */
2039*0Sstevel@tonic-gate 		fmri_copy_sz = strlen(fmri) + 1;
2040*0Sstevel@tonic-gate 		fmri_copy = startd_alloc(fmri_copy_sz);
2041*0Sstevel@tonic-gate 		(void) strcpy(fmri_copy, fmri);
2042*0Sstevel@tonic-gate 
2043*0Sstevel@tonic-gate 		/* Determine if the FMRI is a property group or instance */
2044*0Sstevel@tonic-gate 		if (scf_parse_svc_fmri(fmri_copy, &scope, &service,
2045*0Sstevel@tonic-gate 		    &instance, &pg, NULL) != 0) {
2046*0Sstevel@tonic-gate 			startd_free(fmri_copy, fmri_copy_sz);
2047*0Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
2048*0Sstevel@tonic-gate 			    "Dependency \"%s\" of %s has invalid FMRI "
2049*0Sstevel@tonic-gate 			    "\"%s\".\n", info->pg_name, info->inst_fmri,
2050*0Sstevel@tonic-gate 			    fmri);
2051*0Sstevel@tonic-gate 			return (info->err = EINVAL);
2052*0Sstevel@tonic-gate 		}
2053*0Sstevel@tonic-gate 
2054*0Sstevel@tonic-gate 		if (service == NULL || pg != NULL) {
2055*0Sstevel@tonic-gate 			startd_free(fmri_copy, fmri_copy_sz);
2056*0Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
2057*0Sstevel@tonic-gate 			    "Dependency \"%s\" of %s does not designate a "
2058*0Sstevel@tonic-gate 			    "service or instance.\n", info->pg_name,
2059*0Sstevel@tonic-gate 			    info->inst_fmri);
2060*0Sstevel@tonic-gate 			return (info->err = EINVAL);
2061*0Sstevel@tonic-gate 		}
2062*0Sstevel@tonic-gate 
2063*0Sstevel@tonic-gate 		if (scope == NULL || strcmp(scope, SCF_SCOPE_LOCAL) == 0) {
2064*0Sstevel@tonic-gate 			cfmri = uu_msprintf("svc:/%s%s%s",
2065*0Sstevel@tonic-gate 			    service, instance ? ":" : "", instance ? instance :
2066*0Sstevel@tonic-gate 			    "");
2067*0Sstevel@tonic-gate 		} else {
2068*0Sstevel@tonic-gate 			cfmri = uu_msprintf("svc://%s/%s%s%s",
2069*0Sstevel@tonic-gate 			    scope, service, instance ? ":" : "", instance ?
2070*0Sstevel@tonic-gate 			    instance : "");
2071*0Sstevel@tonic-gate 		}
2072*0Sstevel@tonic-gate 
2073*0Sstevel@tonic-gate 		startd_free(fmri_copy, fmri_copy_sz);
2074*0Sstevel@tonic-gate 
2075*0Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(cfmri, instance ?
2076*0Sstevel@tonic-gate 		    GVT_INST : GVT_SVC, instance ? 0 : DEPGRP_REQUIRE_ANY,
2077*0Sstevel@tonic-gate 		    RERR_NONE, &v);
2078*0Sstevel@tonic-gate 		uu_free(cfmri);
2079*0Sstevel@tonic-gate 		switch (err) {
2080*0Sstevel@tonic-gate 		case 0:
2081*0Sstevel@tonic-gate 			break;
2082*0Sstevel@tonic-gate 
2083*0Sstevel@tonic-gate 		case EEXIST:
2084*0Sstevel@tonic-gate 			/* Verify v. */
2085*0Sstevel@tonic-gate 			if (instance != NULL)
2086*0Sstevel@tonic-gate 				assert(v->gv_type == GVT_INST);
2087*0Sstevel@tonic-gate 			else
2088*0Sstevel@tonic-gate 				assert(v->gv_type == GVT_SVC);
2089*0Sstevel@tonic-gate 			break;
2090*0Sstevel@tonic-gate 
2091*0Sstevel@tonic-gate 		default:
2092*0Sstevel@tonic-gate 			bad_error("graph_insert_vertex_unconfigured", err);
2093*0Sstevel@tonic-gate 		}
2094*0Sstevel@tonic-gate 	}
2095*0Sstevel@tonic-gate 
2096*0Sstevel@tonic-gate 	/* Add dependency from depgroup_v to new vertex */
2097*0Sstevel@tonic-gate 	info->err = graph_insert_dependency(depgroup_v, v, info->pathp);
2098*0Sstevel@tonic-gate 	switch (info->err) {
2099*0Sstevel@tonic-gate 	case 0:
2100*0Sstevel@tonic-gate 		break;
2101*0Sstevel@tonic-gate 
2102*0Sstevel@tonic-gate 	case ELOOP:
2103*0Sstevel@tonic-gate 		return (ELOOP);
2104*0Sstevel@tonic-gate 
2105*0Sstevel@tonic-gate 	default:
2106*0Sstevel@tonic-gate 		bad_error("graph_insert_dependency", info->err);
2107*0Sstevel@tonic-gate 	}
2108*0Sstevel@tonic-gate 
2109*0Sstevel@tonic-gate 	/* This must be after we insert the dependency, to avoid looping. */
2110*0Sstevel@tonic-gate 	switch (v->gv_type) {
2111*0Sstevel@tonic-gate 	case GVT_INST:
2112*0Sstevel@tonic-gate 		if ((v->gv_flags & GV_CONFIGURED) != 0)
2113*0Sstevel@tonic-gate 			break;
2114*0Sstevel@tonic-gate 
2115*0Sstevel@tonic-gate 		inst = safe_scf_instance_create(info->h);
2116*0Sstevel@tonic-gate 
2117*0Sstevel@tonic-gate 		rebound = B_FALSE;
2118*0Sstevel@tonic-gate 
2119*0Sstevel@tonic-gate rebound:
2120*0Sstevel@tonic-gate 		err = libscf_lookup_instance(v->gv_name, inst);
2121*0Sstevel@tonic-gate 		switch (err) {
2122*0Sstevel@tonic-gate 		case 0:
2123*0Sstevel@tonic-gate 			err = configure_vertex(v, inst);
2124*0Sstevel@tonic-gate 			switch (err) {
2125*0Sstevel@tonic-gate 			case 0:
2126*0Sstevel@tonic-gate 			case ECANCELED:
2127*0Sstevel@tonic-gate 				break;
2128*0Sstevel@tonic-gate 
2129*0Sstevel@tonic-gate 			case ECONNABORTED:
2130*0Sstevel@tonic-gate 				libscf_handle_rebind(info->h);
2131*0Sstevel@tonic-gate 				rebound = B_TRUE;
2132*0Sstevel@tonic-gate 				goto rebound;
2133*0Sstevel@tonic-gate 
2134*0Sstevel@tonic-gate 			default:
2135*0Sstevel@tonic-gate 				bad_error("configure_vertex", err);
2136*0Sstevel@tonic-gate 			}
2137*0Sstevel@tonic-gate 			break;
2138*0Sstevel@tonic-gate 
2139*0Sstevel@tonic-gate 		case ENOENT:
2140*0Sstevel@tonic-gate 			break;
2141*0Sstevel@tonic-gate 
2142*0Sstevel@tonic-gate 		case ECONNABORTED:
2143*0Sstevel@tonic-gate 			libscf_handle_rebind(info->h);
2144*0Sstevel@tonic-gate 			rebound = B_TRUE;
2145*0Sstevel@tonic-gate 			goto rebound;
2146*0Sstevel@tonic-gate 
2147*0Sstevel@tonic-gate 		case EINVAL:
2148*0Sstevel@tonic-gate 		case ENOTSUP:
2149*0Sstevel@tonic-gate 		default:
2150*0Sstevel@tonic-gate 			bad_error("libscf_fmri_get_instance", err);
2151*0Sstevel@tonic-gate 		}
2152*0Sstevel@tonic-gate 
2153*0Sstevel@tonic-gate 		scf_instance_destroy(inst);
2154*0Sstevel@tonic-gate 
2155*0Sstevel@tonic-gate 		if (rebound)
2156*0Sstevel@tonic-gate 			return (info->err = ECONNRESET);
2157*0Sstevel@tonic-gate 		break;
2158*0Sstevel@tonic-gate 
2159*0Sstevel@tonic-gate 	case GVT_SVC:
2160*0Sstevel@tonic-gate 		(void) add_service(v->gv_name, info->h, &rebound);
2161*0Sstevel@tonic-gate 		if (rebound)
2162*0Sstevel@tonic-gate 			return (info->err = ECONNRESET);
2163*0Sstevel@tonic-gate 	}
2164*0Sstevel@tonic-gate 
2165*0Sstevel@tonic-gate 	return (0);
2166*0Sstevel@tonic-gate }
2167*0Sstevel@tonic-gate 
2168*0Sstevel@tonic-gate struct deppg_info {
2169*0Sstevel@tonic-gate 	graph_vertex_t	*v;		/* GVT_INST vertex */
2170*0Sstevel@tonic-gate 	int		err;		/* return error */
2171*0Sstevel@tonic-gate 	int		**pathp;	/* return circular dependency path */
2172*0Sstevel@tonic-gate };
2173*0Sstevel@tonic-gate 
2174*0Sstevel@tonic-gate /*
2175*0Sstevel@tonic-gate  * Make info->v depend on a new GVT_GROUP node for this property group,
2176*0Sstevel@tonic-gate  * and then call process_dependency_fmri() for the values of the entity
2177*0Sstevel@tonic-gate  * property.  Return 0 on success, or if something goes wrong return nonzero
2178*0Sstevel@tonic-gate  * and set info->err to ECONNABORTED, EINVAL, or the error code returned by
2179*0Sstevel@tonic-gate  * process_dependency_fmri().
2180*0Sstevel@tonic-gate  */
2181*0Sstevel@tonic-gate static int
2182*0Sstevel@tonic-gate process_dependency_pg(scf_propertygroup_t *pg, struct deppg_info *info)
2183*0Sstevel@tonic-gate {
2184*0Sstevel@tonic-gate 	scf_handle_t *h;
2185*0Sstevel@tonic-gate 	depgroup_type_t deptype;
2186*0Sstevel@tonic-gate 	struct depfmri_info linfo;
2187*0Sstevel@tonic-gate 	char *fmri, *pg_name;
2188*0Sstevel@tonic-gate 	size_t fmri_sz;
2189*0Sstevel@tonic-gate 	graph_vertex_t *depgrp;
2190*0Sstevel@tonic-gate 	scf_property_t *prop;
2191*0Sstevel@tonic-gate 	int err;
2192*0Sstevel@tonic-gate 	int empty;
2193*0Sstevel@tonic-gate 	scf_error_t scferr;
2194*0Sstevel@tonic-gate 	ssize_t len;
2195*0Sstevel@tonic-gate 
2196*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
2197*0Sstevel@tonic-gate 
2198*0Sstevel@tonic-gate 	h = scf_pg_handle(pg);
2199*0Sstevel@tonic-gate 
2200*0Sstevel@tonic-gate 	pg_name = startd_alloc(max_scf_name_size);
2201*0Sstevel@tonic-gate 
2202*0Sstevel@tonic-gate 	len = scf_pg_get_name(pg, pg_name, max_scf_name_size);
2203*0Sstevel@tonic-gate 	if (len < 0) {
2204*0Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
2205*0Sstevel@tonic-gate 		switch (scf_error()) {
2206*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
2207*0Sstevel@tonic-gate 		default:
2208*0Sstevel@tonic-gate 			return (info->err = ECONNABORTED);
2209*0Sstevel@tonic-gate 
2210*0Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
2211*0Sstevel@tonic-gate 			return (info->err = 0);
2212*0Sstevel@tonic-gate 
2213*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
2214*0Sstevel@tonic-gate 			bad_error("scf_pg_get_name", scf_error());
2215*0Sstevel@tonic-gate 		}
2216*0Sstevel@tonic-gate 	}
2217*0Sstevel@tonic-gate 
2218*0Sstevel@tonic-gate 	/*
2219*0Sstevel@tonic-gate 	 * Skip over empty dependency groups.  Since dependency property
2220*0Sstevel@tonic-gate 	 * groups are updated atomically, they are either empty or
2221*0Sstevel@tonic-gate 	 * fully populated.
2222*0Sstevel@tonic-gate 	 */
2223*0Sstevel@tonic-gate 	empty = depgroup_empty(h, pg);
2224*0Sstevel@tonic-gate 	if (empty < 0) {
2225*0Sstevel@tonic-gate 		log_error(LOG_INFO,
2226*0Sstevel@tonic-gate 		    "Error reading dependency group \"%s\" of %s: %s\n",
2227*0Sstevel@tonic-gate 		    pg_name, info->v->gv_name, scf_strerror(scf_error()));
2228*0Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
2229*0Sstevel@tonic-gate 		return (info->err = EINVAL);
2230*0Sstevel@tonic-gate 
2231*0Sstevel@tonic-gate 	} else if (empty == 1) {
2232*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
2233*0Sstevel@tonic-gate 		    "Ignoring empty dependency group \"%s\" of %s\n",
2234*0Sstevel@tonic-gate 		    pg_name, info->v->gv_name);
2235*0Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
2236*0Sstevel@tonic-gate 		return (info->err = 0);
2237*0Sstevel@tonic-gate 	}
2238*0Sstevel@tonic-gate 
2239*0Sstevel@tonic-gate 	fmri_sz = strlen(info->v->gv_name) + 1 + len + 1;
2240*0Sstevel@tonic-gate 	fmri = startd_alloc(fmri_sz);
2241*0Sstevel@tonic-gate 
2242*0Sstevel@tonic-gate 	(void) snprintf(fmri, max_scf_name_size, "%s>%s", info->v->gv_name,
2243*0Sstevel@tonic-gate 	    pg_name);
2244*0Sstevel@tonic-gate 
2245*0Sstevel@tonic-gate 	/* Validate the pg before modifying the graph */
2246*0Sstevel@tonic-gate 	deptype = depgroup_read_grouping(h, pg);
2247*0Sstevel@tonic-gate 	if (deptype == DEPGRP_UNSUPPORTED) {
2248*0Sstevel@tonic-gate 		log_error(LOG_INFO,
2249*0Sstevel@tonic-gate 		    "Dependency \"%s\" of %s has an unknown grouping value.\n",
2250*0Sstevel@tonic-gate 		    pg_name, info->v->gv_name);
2251*0Sstevel@tonic-gate 		startd_free(fmri, fmri_sz);
2252*0Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
2253*0Sstevel@tonic-gate 		return (info->err = EINVAL);
2254*0Sstevel@tonic-gate 	}
2255*0Sstevel@tonic-gate 
2256*0Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
2257*0Sstevel@tonic-gate 
2258*0Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0) {
2259*0Sstevel@tonic-gate 		scferr = scf_error();
2260*0Sstevel@tonic-gate 		scf_property_destroy(prop);
2261*0Sstevel@tonic-gate 		if (scferr == SCF_ERROR_DELETED) {
2262*0Sstevel@tonic-gate 			startd_free(fmri, fmri_sz);
2263*0Sstevel@tonic-gate 			startd_free(pg_name, max_scf_name_size);
2264*0Sstevel@tonic-gate 			return (info->err = 0);
2265*0Sstevel@tonic-gate 		} else if (scferr != SCF_ERROR_NOT_FOUND) {
2266*0Sstevel@tonic-gate 			startd_free(fmri, fmri_sz);
2267*0Sstevel@tonic-gate 			startd_free(pg_name, max_scf_name_size);
2268*0Sstevel@tonic-gate 			return (info->err = ECONNABORTED);
2269*0Sstevel@tonic-gate 		}
2270*0Sstevel@tonic-gate 
2271*0Sstevel@tonic-gate 		log_error(LOG_INFO,
2272*0Sstevel@tonic-gate 		    "Dependency \"%s\" of %s is missing a \"%s\" property.\n",
2273*0Sstevel@tonic-gate 		    pg_name, info->v->gv_name, SCF_PROPERTY_ENTITIES);
2274*0Sstevel@tonic-gate 
2275*0Sstevel@tonic-gate 		startd_free(fmri, fmri_sz);
2276*0Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
2277*0Sstevel@tonic-gate 
2278*0Sstevel@tonic-gate 		return (info->err = EINVAL);
2279*0Sstevel@tonic-gate 	}
2280*0Sstevel@tonic-gate 
2281*0Sstevel@tonic-gate 	/* Create depgroup vertex for pg */
2282*0Sstevel@tonic-gate 	err = graph_insert_vertex_unconfigured(fmri, GVT_GROUP, deptype,
2283*0Sstevel@tonic-gate 	    depgroup_read_restart(h, pg), &depgrp);
2284*0Sstevel@tonic-gate 	assert(err == 0);
2285*0Sstevel@tonic-gate 	startd_free(fmri, fmri_sz);
2286*0Sstevel@tonic-gate 
2287*0Sstevel@tonic-gate 	/* Add dependency from inst vertex to new vertex */
2288*0Sstevel@tonic-gate 	err = graph_insert_dependency(info->v, depgrp, info->pathp);
2289*0Sstevel@tonic-gate 	/* ELOOP can't happen because this should be a new vertex */
2290*0Sstevel@tonic-gate 	assert(err == 0);
2291*0Sstevel@tonic-gate 
2292*0Sstevel@tonic-gate 	linfo.v = depgrp;
2293*0Sstevel@tonic-gate 	linfo.type = depgroup_read_scheme(h, pg);
2294*0Sstevel@tonic-gate 	linfo.inst_fmri = info->v->gv_name;
2295*0Sstevel@tonic-gate 	linfo.pg_name = pg_name;
2296*0Sstevel@tonic-gate 	linfo.h = h;
2297*0Sstevel@tonic-gate 	linfo.err = 0;
2298*0Sstevel@tonic-gate 	linfo.pathp = info->pathp;
2299*0Sstevel@tonic-gate 	err = walk_property_astrings(prop, (callback_t)process_dependency_fmri,
2300*0Sstevel@tonic-gate 	    &linfo);
2301*0Sstevel@tonic-gate 
2302*0Sstevel@tonic-gate 	scf_property_destroy(prop);
2303*0Sstevel@tonic-gate 	startd_free(pg_name, max_scf_name_size);
2304*0Sstevel@tonic-gate 
2305*0Sstevel@tonic-gate 	switch (err) {
2306*0Sstevel@tonic-gate 	case 0:
2307*0Sstevel@tonic-gate 	case EINTR:
2308*0Sstevel@tonic-gate 		return (info->err = linfo.err);
2309*0Sstevel@tonic-gate 
2310*0Sstevel@tonic-gate 	case ECONNABORTED:
2311*0Sstevel@tonic-gate 	case EINVAL:
2312*0Sstevel@tonic-gate 		return (info->err = err);
2313*0Sstevel@tonic-gate 
2314*0Sstevel@tonic-gate 	case ECANCELED:
2315*0Sstevel@tonic-gate 		return (info->err = 0);
2316*0Sstevel@tonic-gate 
2317*0Sstevel@tonic-gate 	case ECONNRESET:
2318*0Sstevel@tonic-gate 		return (info->err = ECONNABORTED);
2319*0Sstevel@tonic-gate 
2320*0Sstevel@tonic-gate 	default:
2321*0Sstevel@tonic-gate 		bad_error("walk_property_astrings", err);
2322*0Sstevel@tonic-gate 		/* NOTREACHED */
2323*0Sstevel@tonic-gate 	}
2324*0Sstevel@tonic-gate }
2325*0Sstevel@tonic-gate 
2326*0Sstevel@tonic-gate /*
2327*0Sstevel@tonic-gate  * Build the dependency info for v from the repository.  Returns 0 on success,
2328*0Sstevel@tonic-gate  * ECONNABORTED on repository disconnection, EINVAL if the repository
2329*0Sstevel@tonic-gate  * configuration is invalid, and ELOOP if a dependency would cause a cycle.
2330*0Sstevel@tonic-gate  * In the last case, *pathp will point to a -1-terminated array of ids which
2331*0Sstevel@tonic-gate  * constitute the rest of the dependency cycle.
2332*0Sstevel@tonic-gate  */
2333*0Sstevel@tonic-gate static int
2334*0Sstevel@tonic-gate set_dependencies(graph_vertex_t *v, scf_instance_t *inst, int **pathp)
2335*0Sstevel@tonic-gate {
2336*0Sstevel@tonic-gate 	struct deppg_info info;
2337*0Sstevel@tonic-gate 	int err;
2338*0Sstevel@tonic-gate 	uint_t old_configured;
2339*0Sstevel@tonic-gate 
2340*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
2341*0Sstevel@tonic-gate 
2342*0Sstevel@tonic-gate 	/*
2343*0Sstevel@tonic-gate 	 * Mark the vertex as configured during dependency insertion to avoid
2344*0Sstevel@tonic-gate 	 * dependency cycles (which can appear in the graph if one of the
2345*0Sstevel@tonic-gate 	 * vertices is an exclusion-group).
2346*0Sstevel@tonic-gate 	 */
2347*0Sstevel@tonic-gate 	old_configured = v->gv_flags & GV_CONFIGURED;
2348*0Sstevel@tonic-gate 	v->gv_flags |= GV_CONFIGURED;
2349*0Sstevel@tonic-gate 
2350*0Sstevel@tonic-gate 	info.err = 0;
2351*0Sstevel@tonic-gate 	info.v = v;
2352*0Sstevel@tonic-gate 	info.pathp = pathp;
2353*0Sstevel@tonic-gate 
2354*0Sstevel@tonic-gate 	err = walk_dependency_pgs(inst, (callback_t)process_dependency_pg,
2355*0Sstevel@tonic-gate 	    &info);
2356*0Sstevel@tonic-gate 
2357*0Sstevel@tonic-gate 	if (!old_configured)
2358*0Sstevel@tonic-gate 		v->gv_flags &= ~GV_CONFIGURED;
2359*0Sstevel@tonic-gate 
2360*0Sstevel@tonic-gate 	switch (err) {
2361*0Sstevel@tonic-gate 	case 0:
2362*0Sstevel@tonic-gate 	case EINTR:
2363*0Sstevel@tonic-gate 		return (info.err);
2364*0Sstevel@tonic-gate 
2365*0Sstevel@tonic-gate 	case ECONNABORTED:
2366*0Sstevel@tonic-gate 		return (ECONNABORTED);
2367*0Sstevel@tonic-gate 
2368*0Sstevel@tonic-gate 	case ECANCELED:
2369*0Sstevel@tonic-gate 		/* Should get delete event, so return 0. */
2370*0Sstevel@tonic-gate 		return (0);
2371*0Sstevel@tonic-gate 
2372*0Sstevel@tonic-gate 	default:
2373*0Sstevel@tonic-gate 		bad_error("walk_dependency_pgs", err);
2374*0Sstevel@tonic-gate 		/* NOTREACHED */
2375*0Sstevel@tonic-gate 	}
2376*0Sstevel@tonic-gate }
2377*0Sstevel@tonic-gate 
2378*0Sstevel@tonic-gate 
2379*0Sstevel@tonic-gate static void
2380*0Sstevel@tonic-gate handle_cycle(const char *fmri, int *path)
2381*0Sstevel@tonic-gate {
2382*0Sstevel@tonic-gate 	const char *cp;
2383*0Sstevel@tonic-gate 	size_t sz;
2384*0Sstevel@tonic-gate 
2385*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
2386*0Sstevel@tonic-gate 
2387*0Sstevel@tonic-gate 	path_to_str(path, (char **)&cp, &sz);
2388*0Sstevel@tonic-gate 
2389*0Sstevel@tonic-gate 	log_error(LOG_ERR, "Putting service %s into maintenance "
2390*0Sstevel@tonic-gate 	    "because it completes a dependency cycle:\n%s", fmri ? fmri : "?",
2391*0Sstevel@tonic-gate 	    cp);
2392*0Sstevel@tonic-gate 
2393*0Sstevel@tonic-gate 	startd_free((void *)cp, sz);
2394*0Sstevel@tonic-gate }
2395*0Sstevel@tonic-gate 
2396*0Sstevel@tonic-gate /*
2397*0Sstevel@tonic-gate  * When run on the dependencies of a vertex, populates list with
2398*0Sstevel@tonic-gate  * graph_edge_t's which point to the instance vertices (no GVT_GROUP nodes)
2399*0Sstevel@tonic-gate  * on which the vertex depends.
2400*0Sstevel@tonic-gate  */
2401*0Sstevel@tonic-gate static int
2402*0Sstevel@tonic-gate append_insts(graph_edge_t *e, uu_list_t *list)
2403*0Sstevel@tonic-gate {
2404*0Sstevel@tonic-gate 	graph_vertex_t *v = e->ge_vertex;
2405*0Sstevel@tonic-gate 	graph_edge_t *new;
2406*0Sstevel@tonic-gate 	int r;
2407*0Sstevel@tonic-gate 
2408*0Sstevel@tonic-gate 	switch (v->gv_type) {
2409*0Sstevel@tonic-gate 	case GVT_INST:
2410*0Sstevel@tonic-gate 	case GVT_SVC:
2411*0Sstevel@tonic-gate 		break;
2412*0Sstevel@tonic-gate 
2413*0Sstevel@tonic-gate 	case GVT_GROUP:
2414*0Sstevel@tonic-gate 		r = uu_list_walk(v->gv_dependencies,
2415*0Sstevel@tonic-gate 		    (uu_walk_fn_t *)append_insts, list, 0);
2416*0Sstevel@tonic-gate 		assert(r == 0);
2417*0Sstevel@tonic-gate 		return (UU_WALK_NEXT);
2418*0Sstevel@tonic-gate 
2419*0Sstevel@tonic-gate 	case GVT_FILE:
2420*0Sstevel@tonic-gate 		return (UU_WALK_NEXT);
2421*0Sstevel@tonic-gate 
2422*0Sstevel@tonic-gate 	default:
2423*0Sstevel@tonic-gate #ifndef NDEBUG
2424*0Sstevel@tonic-gate 		uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
2425*0Sstevel@tonic-gate 		    __LINE__, v->gv_type);
2426*0Sstevel@tonic-gate #endif
2427*0Sstevel@tonic-gate 		abort();
2428*0Sstevel@tonic-gate 	}
2429*0Sstevel@tonic-gate 
2430*0Sstevel@tonic-gate 	new = startd_alloc(sizeof (*new));
2431*0Sstevel@tonic-gate 	new->ge_vertex = v;
2432*0Sstevel@tonic-gate 	uu_list_node_init(new, &new->ge_link, graph_edge_pool);
2433*0Sstevel@tonic-gate 	r = uu_list_insert_before(list, NULL, new);
2434*0Sstevel@tonic-gate 	assert(r == 0);
2435*0Sstevel@tonic-gate 	return (UU_WALK_NEXT);
2436*0Sstevel@tonic-gate }
2437*0Sstevel@tonic-gate 
2438*0Sstevel@tonic-gate static boolean_t
2439*0Sstevel@tonic-gate should_be_in_subgraph(graph_vertex_t *v)
2440*0Sstevel@tonic-gate {
2441*0Sstevel@tonic-gate 	graph_edge_t *e;
2442*0Sstevel@tonic-gate 
2443*0Sstevel@tonic-gate 	if (v == milestone)
2444*0Sstevel@tonic-gate 		return (B_TRUE);
2445*0Sstevel@tonic-gate 
2446*0Sstevel@tonic-gate 	/*
2447*0Sstevel@tonic-gate 	 * v is in the subgraph if any of its dependents are in the subgraph.
2448*0Sstevel@tonic-gate 	 * Except for EXCLUDE_ALL dependents.  And OPTIONAL dependents only
2449*0Sstevel@tonic-gate 	 * count if we're enabled.
2450*0Sstevel@tonic-gate 	 */
2451*0Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
2452*0Sstevel@tonic-gate 	    e != NULL;
2453*0Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e)) {
2454*0Sstevel@tonic-gate 		graph_vertex_t *dv = e->ge_vertex;
2455*0Sstevel@tonic-gate 
2456*0Sstevel@tonic-gate 		if (!(dv->gv_flags & GV_INSUBGRAPH))
2457*0Sstevel@tonic-gate 			continue;
2458*0Sstevel@tonic-gate 
2459*0Sstevel@tonic-gate 		/*
2460*0Sstevel@tonic-gate 		 * Don't include instances that are optional and disabled.
2461*0Sstevel@tonic-gate 		 */
2462*0Sstevel@tonic-gate 		if (v->gv_type == GVT_INST && dv->gv_type == GVT_SVC) {
2463*0Sstevel@tonic-gate 
2464*0Sstevel@tonic-gate 			int in = 0;
2465*0Sstevel@tonic-gate 			graph_edge_t *ee;
2466*0Sstevel@tonic-gate 
2467*0Sstevel@tonic-gate 			for (ee = uu_list_first(dv->gv_dependents);
2468*0Sstevel@tonic-gate 			    ee != NULL;
2469*0Sstevel@tonic-gate 			    ee = uu_list_next(dv->gv_dependents, ee)) {
2470*0Sstevel@tonic-gate 
2471*0Sstevel@tonic-gate 				graph_vertex_t *ddv = e->ge_vertex;
2472*0Sstevel@tonic-gate 
2473*0Sstevel@tonic-gate 				if (ddv->gv_type == GVT_GROUP &&
2474*0Sstevel@tonic-gate 				    ddv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
2475*0Sstevel@tonic-gate 					continue;
2476*0Sstevel@tonic-gate 
2477*0Sstevel@tonic-gate 				if (ddv->gv_type == GVT_GROUP &&
2478*0Sstevel@tonic-gate 				    ddv->gv_depgroup == DEPGRP_OPTIONAL_ALL &&
2479*0Sstevel@tonic-gate 				    !(v->gv_flags & GV_ENBLD_NOOVR))
2480*0Sstevel@tonic-gate 					continue;
2481*0Sstevel@tonic-gate 
2482*0Sstevel@tonic-gate 				in = 1;
2483*0Sstevel@tonic-gate 			}
2484*0Sstevel@tonic-gate 			if (!in)
2485*0Sstevel@tonic-gate 				continue;
2486*0Sstevel@tonic-gate 		}
2487*0Sstevel@tonic-gate 		if (v->gv_type == GVT_INST &&
2488*0Sstevel@tonic-gate 		    dv->gv_type == GVT_GROUP &&
2489*0Sstevel@tonic-gate 		    dv->gv_depgroup == DEPGRP_OPTIONAL_ALL &&
2490*0Sstevel@tonic-gate 		    !(v->gv_flags & GV_ENBLD_NOOVR))
2491*0Sstevel@tonic-gate 			continue;
2492*0Sstevel@tonic-gate 
2493*0Sstevel@tonic-gate 		/* Don't include excluded services and instances */
2494*0Sstevel@tonic-gate 		if (dv->gv_type == GVT_GROUP &&
2495*0Sstevel@tonic-gate 		    dv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
2496*0Sstevel@tonic-gate 			continue;
2497*0Sstevel@tonic-gate 
2498*0Sstevel@tonic-gate 		return (B_TRUE);
2499*0Sstevel@tonic-gate 	}
2500*0Sstevel@tonic-gate 
2501*0Sstevel@tonic-gate 	return (B_FALSE);
2502*0Sstevel@tonic-gate }
2503*0Sstevel@tonic-gate 
2504*0Sstevel@tonic-gate /*
2505*0Sstevel@tonic-gate  * Ensures that GV_INSUBGRAPH is set properly for v and its descendents.  If
2506*0Sstevel@tonic-gate  * any bits change, manipulate the repository appropriately.  Returns 0 or
2507*0Sstevel@tonic-gate  * ECONNABORTED.
2508*0Sstevel@tonic-gate  */
2509*0Sstevel@tonic-gate static int
2510*0Sstevel@tonic-gate eval_subgraph(graph_vertex_t *v, scf_handle_t *h)
2511*0Sstevel@tonic-gate {
2512*0Sstevel@tonic-gate 	boolean_t old = (v->gv_flags & GV_INSUBGRAPH) != 0;
2513*0Sstevel@tonic-gate 	boolean_t new;
2514*0Sstevel@tonic-gate 	graph_edge_t *e;
2515*0Sstevel@tonic-gate 	scf_instance_t *inst;
2516*0Sstevel@tonic-gate 	int ret = 0, r;
2517*0Sstevel@tonic-gate 
2518*0Sstevel@tonic-gate 	assert(milestone != NULL && milestone != MILESTONE_NONE);
2519*0Sstevel@tonic-gate 
2520*0Sstevel@tonic-gate 	new = should_be_in_subgraph(v);
2521*0Sstevel@tonic-gate 
2522*0Sstevel@tonic-gate 	if (new == old)
2523*0Sstevel@tonic-gate 		return (0);
2524*0Sstevel@tonic-gate 
2525*0Sstevel@tonic-gate 	log_framework(LOG_DEBUG, new ? "Adding %s to the subgraph.\n" :
2526*0Sstevel@tonic-gate 	    "Removing %s from the subgraph.\n", v->gv_name);
2527*0Sstevel@tonic-gate 
2528*0Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_INSUBGRAPH) |
2529*0Sstevel@tonic-gate 	    (new ? GV_INSUBGRAPH : 0);
2530*0Sstevel@tonic-gate 
2531*0Sstevel@tonic-gate 	if (v->gv_type == GVT_INST && (v->gv_flags & GV_CONFIGURED)) {
2532*0Sstevel@tonic-gate 		int err;
2533*0Sstevel@tonic-gate 
2534*0Sstevel@tonic-gate get_inst:
2535*0Sstevel@tonic-gate 		err = libscf_fmri_get_instance(h, v->gv_name, &inst);
2536*0Sstevel@tonic-gate 		if (err != 0) {
2537*0Sstevel@tonic-gate 			switch (err) {
2538*0Sstevel@tonic-gate 			case ECONNABORTED:
2539*0Sstevel@tonic-gate 				libscf_handle_rebind(h);
2540*0Sstevel@tonic-gate 				ret = ECONNABORTED;
2541*0Sstevel@tonic-gate 				goto get_inst;
2542*0Sstevel@tonic-gate 
2543*0Sstevel@tonic-gate 			case ENOENT:
2544*0Sstevel@tonic-gate 				break;
2545*0Sstevel@tonic-gate 
2546*0Sstevel@tonic-gate 			case EINVAL:
2547*0Sstevel@tonic-gate 			case ENOTSUP:
2548*0Sstevel@tonic-gate 			default:
2549*0Sstevel@tonic-gate 				bad_error("libscf_fmri_get_instance", err);
2550*0Sstevel@tonic-gate 			}
2551*0Sstevel@tonic-gate 		} else {
2552*0Sstevel@tonic-gate 			const char *f;
2553*0Sstevel@tonic-gate 
2554*0Sstevel@tonic-gate 			if (new) {
2555*0Sstevel@tonic-gate 				err = libscf_delete_enable_ovr(inst);
2556*0Sstevel@tonic-gate 				f = "libscf_delete_enable_ovr";
2557*0Sstevel@tonic-gate 			} else {
2558*0Sstevel@tonic-gate 				err = libscf_set_enable_ovr(inst, 0);
2559*0Sstevel@tonic-gate 				f = "libscf_set_enable_ovr";
2560*0Sstevel@tonic-gate 			}
2561*0Sstevel@tonic-gate 			scf_instance_destroy(inst);
2562*0Sstevel@tonic-gate 			switch (err) {
2563*0Sstevel@tonic-gate 			case 0:
2564*0Sstevel@tonic-gate 			case ECANCELED:
2565*0Sstevel@tonic-gate 				break;
2566*0Sstevel@tonic-gate 
2567*0Sstevel@tonic-gate 			case ECONNABORTED:
2568*0Sstevel@tonic-gate 				libscf_handle_rebind(h);
2569*0Sstevel@tonic-gate 				/*
2570*0Sstevel@tonic-gate 				 * We must continue so the graph is updated,
2571*0Sstevel@tonic-gate 				 * but we must return ECONNABORTED so any
2572*0Sstevel@tonic-gate 				 * libscf state held by any callers is reset.
2573*0Sstevel@tonic-gate 				 */
2574*0Sstevel@tonic-gate 				ret = ECONNABORTED;
2575*0Sstevel@tonic-gate 				goto get_inst;
2576*0Sstevel@tonic-gate 
2577*0Sstevel@tonic-gate 			case EROFS:
2578*0Sstevel@tonic-gate 			case EPERM:
2579*0Sstevel@tonic-gate 				log_error(LOG_WARNING,
2580*0Sstevel@tonic-gate 				    "Could not set %s/%s for %s: %s.\n",
2581*0Sstevel@tonic-gate 				    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
2582*0Sstevel@tonic-gate 				    v->gv_name, strerror(err));
2583*0Sstevel@tonic-gate 				break;
2584*0Sstevel@tonic-gate 
2585*0Sstevel@tonic-gate 			default:
2586*0Sstevel@tonic-gate 				bad_error(f, err);
2587*0Sstevel@tonic-gate 			}
2588*0Sstevel@tonic-gate 		}
2589*0Sstevel@tonic-gate 	}
2590*0Sstevel@tonic-gate 
2591*0Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
2592*0Sstevel@tonic-gate 	    e != NULL;
2593*0Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
2594*0Sstevel@tonic-gate 		r = eval_subgraph(e->ge_vertex, h);
2595*0Sstevel@tonic-gate 		if (r != 0) {
2596*0Sstevel@tonic-gate 			assert(r == ECONNABORTED);
2597*0Sstevel@tonic-gate 			ret = ECONNABORTED;
2598*0Sstevel@tonic-gate 		}
2599*0Sstevel@tonic-gate 	}
2600*0Sstevel@tonic-gate 
2601*0Sstevel@tonic-gate 	return (ret);
2602*0Sstevel@tonic-gate }
2603*0Sstevel@tonic-gate 
2604*0Sstevel@tonic-gate /*
2605*0Sstevel@tonic-gate  * Delete the (property group) dependencies of v & create new ones based on
2606*0Sstevel@tonic-gate  * inst.  If doing so would create a cycle, log a message and put the instance
2607*0Sstevel@tonic-gate  * into maintenance.  Update GV_INSUBGRAPH flags as necessary.  Returns 0 or
2608*0Sstevel@tonic-gate  * ECONNABORTED.
2609*0Sstevel@tonic-gate  */
2610*0Sstevel@tonic-gate static int
2611*0Sstevel@tonic-gate refresh_vertex(graph_vertex_t *v, scf_instance_t *inst)
2612*0Sstevel@tonic-gate {
2613*0Sstevel@tonic-gate 	int err;
2614*0Sstevel@tonic-gate 	int *path;
2615*0Sstevel@tonic-gate 	char *fmri;
2616*0Sstevel@tonic-gate 	int r;
2617*0Sstevel@tonic-gate 	scf_handle_t *h = scf_instance_handle(inst);
2618*0Sstevel@tonic-gate 	uu_list_t *old_deps;
2619*0Sstevel@tonic-gate 	int ret = 0;
2620*0Sstevel@tonic-gate 	graph_edge_t *e;
2621*0Sstevel@tonic-gate 
2622*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
2623*0Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
2624*0Sstevel@tonic-gate 
2625*0Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph engine: Refreshing %s.\n", v->gv_name);
2626*0Sstevel@tonic-gate 
2627*0Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
2628*0Sstevel@tonic-gate 		/*
2629*0Sstevel@tonic-gate 		 * In case some of v's dependencies are being deleted we must
2630*0Sstevel@tonic-gate 		 * make a list of them now for GV_INSUBGRAPH-flag evaluation
2631*0Sstevel@tonic-gate 		 * after the new dependencies are in place.
2632*0Sstevel@tonic-gate 		 */
2633*0Sstevel@tonic-gate 		old_deps = startd_list_create(graph_edge_pool, NULL, 0);
2634*0Sstevel@tonic-gate 
2635*0Sstevel@tonic-gate 		err = uu_list_walk(v->gv_dependencies,
2636*0Sstevel@tonic-gate 		    (uu_walk_fn_t *)append_insts, old_deps, 0);
2637*0Sstevel@tonic-gate 		assert(err == 0);
2638*0Sstevel@tonic-gate 	}
2639*0Sstevel@tonic-gate 
2640*0Sstevel@tonic-gate 	delete_instance_dependencies(v, B_FALSE);
2641*0Sstevel@tonic-gate 
2642*0Sstevel@tonic-gate 	err = set_dependencies(v, inst, &path);
2643*0Sstevel@tonic-gate 	switch (err) {
2644*0Sstevel@tonic-gate 	case 0:
2645*0Sstevel@tonic-gate 		break;
2646*0Sstevel@tonic-gate 
2647*0Sstevel@tonic-gate 	case ECONNABORTED:
2648*0Sstevel@tonic-gate 		ret = err;
2649*0Sstevel@tonic-gate 		goto out;
2650*0Sstevel@tonic-gate 
2651*0Sstevel@tonic-gate 	case EINVAL:
2652*0Sstevel@tonic-gate 	case ELOOP:
2653*0Sstevel@tonic-gate 		r = libscf_instance_get_fmri(inst, &fmri);
2654*0Sstevel@tonic-gate 		switch (r) {
2655*0Sstevel@tonic-gate 		case 0:
2656*0Sstevel@tonic-gate 			break;
2657*0Sstevel@tonic-gate 
2658*0Sstevel@tonic-gate 		case ECONNABORTED:
2659*0Sstevel@tonic-gate 			ret = ECONNABORTED;
2660*0Sstevel@tonic-gate 			goto out;
2661*0Sstevel@tonic-gate 
2662*0Sstevel@tonic-gate 		case ECANCELED:
2663*0Sstevel@tonic-gate 			ret = 0;
2664*0Sstevel@tonic-gate 			goto out;
2665*0Sstevel@tonic-gate 
2666*0Sstevel@tonic-gate 		default:
2667*0Sstevel@tonic-gate 			bad_error("libscf_instance_get_fmri", r);
2668*0Sstevel@tonic-gate 		}
2669*0Sstevel@tonic-gate 
2670*0Sstevel@tonic-gate 		if (err == EINVAL) {
2671*0Sstevel@tonic-gate 			log_error(LOG_ERR, "Transitioning %s "
2672*0Sstevel@tonic-gate 			    "to maintenance due to misconfiguration.\n",
2673*0Sstevel@tonic-gate 			    fmri ? fmri : "?");
2674*0Sstevel@tonic-gate 			vertex_send_event(v,
2675*0Sstevel@tonic-gate 			    RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY);
2676*0Sstevel@tonic-gate 		} else {
2677*0Sstevel@tonic-gate 			handle_cycle(fmri, path);
2678*0Sstevel@tonic-gate 			vertex_send_event(v,
2679*0Sstevel@tonic-gate 			    RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE);
2680*0Sstevel@tonic-gate 		}
2681*0Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
2682*0Sstevel@tonic-gate 		ret = 0;
2683*0Sstevel@tonic-gate 		goto out;
2684*0Sstevel@tonic-gate 
2685*0Sstevel@tonic-gate 	default:
2686*0Sstevel@tonic-gate 		bad_error("set_dependencies", err);
2687*0Sstevel@tonic-gate 	}
2688*0Sstevel@tonic-gate 
2689*0Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
2690*0Sstevel@tonic-gate 		boolean_t aborted = B_FALSE;
2691*0Sstevel@tonic-gate 
2692*0Sstevel@tonic-gate 		for (e = uu_list_first(old_deps);
2693*0Sstevel@tonic-gate 		    e != NULL;
2694*0Sstevel@tonic-gate 		    e = uu_list_next(old_deps, e)) {
2695*0Sstevel@tonic-gate 			if (eval_subgraph(e->ge_vertex, h) ==
2696*0Sstevel@tonic-gate 			    ECONNABORTED)
2697*0Sstevel@tonic-gate 				aborted = B_TRUE;
2698*0Sstevel@tonic-gate 		}
2699*0Sstevel@tonic-gate 
2700*0Sstevel@tonic-gate 		for (e = uu_list_first(v->gv_dependencies);
2701*0Sstevel@tonic-gate 		    e != NULL;
2702*0Sstevel@tonic-gate 		    e = uu_list_next(v->gv_dependencies, e)) {
2703*0Sstevel@tonic-gate 			if (eval_subgraph(e->ge_vertex, h) ==
2704*0Sstevel@tonic-gate 			    ECONNABORTED)
2705*0Sstevel@tonic-gate 				aborted = B_TRUE;
2706*0Sstevel@tonic-gate 		}
2707*0Sstevel@tonic-gate 
2708*0Sstevel@tonic-gate 		if (aborted) {
2709*0Sstevel@tonic-gate 			ret = ECONNABORTED;
2710*0Sstevel@tonic-gate 			goto out;
2711*0Sstevel@tonic-gate 		}
2712*0Sstevel@tonic-gate 	}
2713*0Sstevel@tonic-gate 
2714*0Sstevel@tonic-gate 	if (v->gv_state == RESTARTER_STATE_OFFLINE) {
2715*0Sstevel@tonic-gate 		if (instance_satisfied(v, B_FALSE) == 1) {
2716*0Sstevel@tonic-gate 			if (v->gv_start_f == NULL)
2717*0Sstevel@tonic-gate 				vertex_send_event(v,
2718*0Sstevel@tonic-gate 				    RESTARTER_EVENT_TYPE_START);
2719*0Sstevel@tonic-gate 			else
2720*0Sstevel@tonic-gate 				v->gv_start_f(v);
2721*0Sstevel@tonic-gate 		}
2722*0Sstevel@tonic-gate 	}
2723*0Sstevel@tonic-gate 
2724*0Sstevel@tonic-gate 	ret = 0;
2725*0Sstevel@tonic-gate 
2726*0Sstevel@tonic-gate out:
2727*0Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
2728*0Sstevel@tonic-gate 		void *cookie = NULL;
2729*0Sstevel@tonic-gate 
2730*0Sstevel@tonic-gate 		while ((e = uu_list_teardown(old_deps, &cookie)) != NULL)
2731*0Sstevel@tonic-gate 			startd_free(e, sizeof (*e));
2732*0Sstevel@tonic-gate 
2733*0Sstevel@tonic-gate 		uu_list_destroy(old_deps);
2734*0Sstevel@tonic-gate 	}
2735*0Sstevel@tonic-gate 
2736*0Sstevel@tonic-gate 	return (ret);
2737*0Sstevel@tonic-gate }
2738*0Sstevel@tonic-gate 
2739*0Sstevel@tonic-gate /*
2740*0Sstevel@tonic-gate  * Set up v according to inst.  That is, make sure it depends on its
2741*0Sstevel@tonic-gate  * restarter and set up its dependencies.  Send the ADD_INSTANCE command to
2742*0Sstevel@tonic-gate  * the restarter, and send ENABLE or DISABLE as appropriate.
2743*0Sstevel@tonic-gate  *
2744*0Sstevel@tonic-gate  * Returns 0 on success, ECONNABORTED on repository disconnection, or
2745*0Sstevel@tonic-gate  * ECANCELED if inst is deleted.
2746*0Sstevel@tonic-gate  */
2747*0Sstevel@tonic-gate static int
2748*0Sstevel@tonic-gate configure_vertex(graph_vertex_t *v, scf_instance_t *inst)
2749*0Sstevel@tonic-gate {
2750*0Sstevel@tonic-gate 	scf_handle_t *h;
2751*0Sstevel@tonic-gate 	scf_propertygroup_t *pg;
2752*0Sstevel@tonic-gate 	scf_snapshot_t *snap;
2753*0Sstevel@tonic-gate 	char *restarter_fmri = startd_alloc(max_scf_value_size);
2754*0Sstevel@tonic-gate 	int enabled, enabled_ovr;
2755*0Sstevel@tonic-gate 	int err;
2756*0Sstevel@tonic-gate 	int *path;
2757*0Sstevel@tonic-gate 
2758*0Sstevel@tonic-gate 	restarter_fmri[0] = '\0';
2759*0Sstevel@tonic-gate 
2760*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
2761*0Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
2762*0Sstevel@tonic-gate 	assert((v->gv_flags & GV_CONFIGURED) == 0);
2763*0Sstevel@tonic-gate 
2764*0Sstevel@tonic-gate 	/* GV_INSUBGRAPH should already be set properly. */
2765*0Sstevel@tonic-gate 	assert(should_be_in_subgraph(v) ==
2766*0Sstevel@tonic-gate 	    ((v->gv_flags & GV_INSUBGRAPH) != 0));
2767*0Sstevel@tonic-gate 
2768*0Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph adding %s.\n", v->gv_name);
2769*0Sstevel@tonic-gate 
2770*0Sstevel@tonic-gate 	h = scf_instance_handle(inst);
2771*0Sstevel@tonic-gate 
2772*0Sstevel@tonic-gate 	/*
2773*0Sstevel@tonic-gate 	 * If the instance does not have a restarter property group,
2774*0Sstevel@tonic-gate 	 * initialize its state to uninitialized/none, in case the restarter
2775*0Sstevel@tonic-gate 	 * is not enabled.
2776*0Sstevel@tonic-gate 	 */
2777*0Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
2778*0Sstevel@tonic-gate 
2779*0Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) != 0) {
2780*0Sstevel@tonic-gate 		instance_data_t idata;
2781*0Sstevel@tonic-gate 		uint_t count = 0, msecs = ALLOC_DELAY;
2782*0Sstevel@tonic-gate 
2783*0Sstevel@tonic-gate 		switch (scf_error()) {
2784*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
2785*0Sstevel@tonic-gate 			break;
2786*0Sstevel@tonic-gate 
2787*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
2788*0Sstevel@tonic-gate 		default:
2789*0Sstevel@tonic-gate 			scf_pg_destroy(pg);
2790*0Sstevel@tonic-gate 			return (ECONNABORTED);
2791*0Sstevel@tonic-gate 
2792*0Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
2793*0Sstevel@tonic-gate 			scf_pg_destroy(pg);
2794*0Sstevel@tonic-gate 			return (ECANCELED);
2795*0Sstevel@tonic-gate 
2796*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
2797*0Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
2798*0Sstevel@tonic-gate 		}
2799*0Sstevel@tonic-gate 
2800*0Sstevel@tonic-gate 		switch (err = libscf_instance_get_fmri(inst,
2801*0Sstevel@tonic-gate 		    (char **)&idata.i_fmri)) {
2802*0Sstevel@tonic-gate 		case 0:
2803*0Sstevel@tonic-gate 			break;
2804*0Sstevel@tonic-gate 
2805*0Sstevel@tonic-gate 		case ECONNABORTED:
2806*0Sstevel@tonic-gate 		case ECANCELED:
2807*0Sstevel@tonic-gate 			scf_pg_destroy(pg);
2808*0Sstevel@tonic-gate 			return (err);
2809*0Sstevel@tonic-gate 
2810*0Sstevel@tonic-gate 		default:
2811*0Sstevel@tonic-gate 			bad_error("libscf_instance_get_fmri", err);
2812*0Sstevel@tonic-gate 		}
2813*0Sstevel@tonic-gate 
2814*0Sstevel@tonic-gate 		idata.i_state = RESTARTER_STATE_NONE;
2815*0Sstevel@tonic-gate 		idata.i_next_state = RESTARTER_STATE_NONE;
2816*0Sstevel@tonic-gate 
2817*0Sstevel@tonic-gate init_state:
2818*0Sstevel@tonic-gate 		switch (err = _restarter_commit_states(h, &idata,
2819*0Sstevel@tonic-gate 		    RESTARTER_STATE_UNINIT, RESTARTER_STATE_NONE, NULL)) {
2820*0Sstevel@tonic-gate 		case 0:
2821*0Sstevel@tonic-gate 			break;
2822*0Sstevel@tonic-gate 
2823*0Sstevel@tonic-gate 		case ENOMEM:
2824*0Sstevel@tonic-gate 			++count;
2825*0Sstevel@tonic-gate 			if (count < ALLOC_RETRY) {
2826*0Sstevel@tonic-gate 				(void) poll(NULL, 0, msecs);
2827*0Sstevel@tonic-gate 				msecs *= ALLOC_DELAY_MULT;
2828*0Sstevel@tonic-gate 				goto init_state;
2829*0Sstevel@tonic-gate 			}
2830*0Sstevel@tonic-gate 
2831*0Sstevel@tonic-gate 			uu_die("Insufficient memory.\n");
2832*0Sstevel@tonic-gate 			/* NOTREACHED */
2833*0Sstevel@tonic-gate 
2834*0Sstevel@tonic-gate 		case ECONNABORTED:
2835*0Sstevel@tonic-gate 			scf_pg_destroy(pg);
2836*0Sstevel@tonic-gate 			return (ECONNABORTED);
2837*0Sstevel@tonic-gate 
2838*0Sstevel@tonic-gate 		case ENOENT:
2839*0Sstevel@tonic-gate 			scf_pg_destroy(pg);
2840*0Sstevel@tonic-gate 			return (ECANCELED);
2841*0Sstevel@tonic-gate 
2842*0Sstevel@tonic-gate 		case EPERM:
2843*0Sstevel@tonic-gate 		case EACCES:
2844*0Sstevel@tonic-gate 		case EROFS:
2845*0Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not initialize state for "
2846*0Sstevel@tonic-gate 			    "%s: %s.\n", idata.i_fmri, strerror(err));
2847*0Sstevel@tonic-gate 			break;
2848*0Sstevel@tonic-gate 
2849*0Sstevel@tonic-gate 		case EINVAL:
2850*0Sstevel@tonic-gate 		default:
2851*0Sstevel@tonic-gate 			bad_error("_restarter_commit_states", err);
2852*0Sstevel@tonic-gate 		}
2853*0Sstevel@tonic-gate 
2854*0Sstevel@tonic-gate 		startd_free((void *)idata.i_fmri, max_scf_fmri_size);
2855*0Sstevel@tonic-gate 	}
2856*0Sstevel@tonic-gate 
2857*0Sstevel@tonic-gate 	scf_pg_destroy(pg);
2858*0Sstevel@tonic-gate 
2859*0Sstevel@tonic-gate 	if (milestone != NULL) {
2860*0Sstevel@tonic-gate 		/*
2861*0Sstevel@tonic-gate 		 * Make sure the enable-override is set properly before we
2862*0Sstevel@tonic-gate 		 * read whether we should be enabled.
2863*0Sstevel@tonic-gate 		 */
2864*0Sstevel@tonic-gate 		if (milestone == MILESTONE_NONE ||
2865*0Sstevel@tonic-gate 		    !(v->gv_flags & GV_INSUBGRAPH)) {
2866*0Sstevel@tonic-gate 			switch (err = libscf_set_enable_ovr(inst, 0)) {
2867*0Sstevel@tonic-gate 			case 0:
2868*0Sstevel@tonic-gate 				break;
2869*0Sstevel@tonic-gate 
2870*0Sstevel@tonic-gate 			case ECONNABORTED:
2871*0Sstevel@tonic-gate 			case ECANCELED:
2872*0Sstevel@tonic-gate 				return (err);
2873*0Sstevel@tonic-gate 
2874*0Sstevel@tonic-gate 			case EROFS:
2875*0Sstevel@tonic-gate 				log_error(LOG_WARNING,
2876*0Sstevel@tonic-gate 				    "Could not set %s/%s for %s: %s.\n",
2877*0Sstevel@tonic-gate 				    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
2878*0Sstevel@tonic-gate 				    v->gv_name, strerror(err));
2879*0Sstevel@tonic-gate 				break;
2880*0Sstevel@tonic-gate 
2881*0Sstevel@tonic-gate 			case EPERM:
2882*0Sstevel@tonic-gate 				uu_die("Permission denied.\n");
2883*0Sstevel@tonic-gate 				/* NOTREACHED */
2884*0Sstevel@tonic-gate 
2885*0Sstevel@tonic-gate 			default:
2886*0Sstevel@tonic-gate 				bad_error("libscf_set_enable_ovr", err);
2887*0Sstevel@tonic-gate 			}
2888*0Sstevel@tonic-gate 		} else {
2889*0Sstevel@tonic-gate 			assert(v->gv_flags & GV_INSUBGRAPH);
2890*0Sstevel@tonic-gate 			switch (err = libscf_delete_enable_ovr(inst)) {
2891*0Sstevel@tonic-gate 			case 0:
2892*0Sstevel@tonic-gate 				break;
2893*0Sstevel@tonic-gate 
2894*0Sstevel@tonic-gate 			case ECONNABORTED:
2895*0Sstevel@tonic-gate 			case ECANCELED:
2896*0Sstevel@tonic-gate 				return (err);
2897*0Sstevel@tonic-gate 
2898*0Sstevel@tonic-gate 			case EPERM:
2899*0Sstevel@tonic-gate 				uu_die("Permission denied.\n");
2900*0Sstevel@tonic-gate 				/* NOTREACHED */
2901*0Sstevel@tonic-gate 
2902*0Sstevel@tonic-gate 			default:
2903*0Sstevel@tonic-gate 				bad_error("libscf_delete_enable_ovr", err);
2904*0Sstevel@tonic-gate 			}
2905*0Sstevel@tonic-gate 		}
2906*0Sstevel@tonic-gate 	}
2907*0Sstevel@tonic-gate 
2908*0Sstevel@tonic-gate 	err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled,
2909*0Sstevel@tonic-gate 	    &enabled_ovr, &restarter_fmri);
2910*0Sstevel@tonic-gate 	switch (err) {
2911*0Sstevel@tonic-gate 	case 0:
2912*0Sstevel@tonic-gate 		break;
2913*0Sstevel@tonic-gate 
2914*0Sstevel@tonic-gate 	case ECONNABORTED:
2915*0Sstevel@tonic-gate 	case ECANCELED:
2916*0Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
2917*0Sstevel@tonic-gate 		return (err);
2918*0Sstevel@tonic-gate 
2919*0Sstevel@tonic-gate 	case ENOENT:
2920*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
2921*0Sstevel@tonic-gate 		    "Ignoring %s because it has no general property group.\n",
2922*0Sstevel@tonic-gate 		    v->gv_name);
2923*0Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
2924*0Sstevel@tonic-gate 		return (0);
2925*0Sstevel@tonic-gate 
2926*0Sstevel@tonic-gate 	default:
2927*0Sstevel@tonic-gate 		bad_error("libscf_get_basic_instance_data", err);
2928*0Sstevel@tonic-gate 	}
2929*0Sstevel@tonic-gate 
2930*0Sstevel@tonic-gate 	if (enabled == -1) {
2931*0Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
2932*0Sstevel@tonic-gate 		return (0);
2933*0Sstevel@tonic-gate 	}
2934*0Sstevel@tonic-gate 
2935*0Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) |
2936*0Sstevel@tonic-gate 	    (enabled ? GV_ENBLD_NOOVR : 0);
2937*0Sstevel@tonic-gate 
2938*0Sstevel@tonic-gate 	if (enabled_ovr != -1)
2939*0Sstevel@tonic-gate 		enabled = enabled_ovr;
2940*0Sstevel@tonic-gate 
2941*0Sstevel@tonic-gate 	v->gv_state = RESTARTER_STATE_UNINIT;
2942*0Sstevel@tonic-gate 
2943*0Sstevel@tonic-gate 	snap = libscf_get_or_make_running_snapshot(inst, v->gv_name, B_TRUE);
2944*0Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
2945*0Sstevel@tonic-gate 
2946*0Sstevel@tonic-gate 	/* Set up the restarter. (Sends _ADD_INSTANCE on success.) */
2947*0Sstevel@tonic-gate 	err = graph_change_restarter(v, restarter_fmri, h, &path);
2948*0Sstevel@tonic-gate 	if (err != 0) {
2949*0Sstevel@tonic-gate 		instance_data_t idata;
2950*0Sstevel@tonic-gate 		uint_t count = 0, msecs = ALLOC_DELAY;
2951*0Sstevel@tonic-gate 		const char *reason;
2952*0Sstevel@tonic-gate 
2953*0Sstevel@tonic-gate 		if (err == ECONNABORTED) {
2954*0Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
2955*0Sstevel@tonic-gate 			return (err);
2956*0Sstevel@tonic-gate 		}
2957*0Sstevel@tonic-gate 
2958*0Sstevel@tonic-gate 		assert(err == EINVAL || err == ELOOP);
2959*0Sstevel@tonic-gate 
2960*0Sstevel@tonic-gate 		if (err == EINVAL) {
2961*0Sstevel@tonic-gate 			log_framework(LOG_WARNING, emsg_invalid_restarter,
2962*0Sstevel@tonic-gate 			    v->gv_name);
2963*0Sstevel@tonic-gate 			reason = "invalid_restarter";
2964*0Sstevel@tonic-gate 		} else {
2965*0Sstevel@tonic-gate 			handle_cycle(v->gv_name, path);
2966*0Sstevel@tonic-gate 			reason = "dependency_cycle";
2967*0Sstevel@tonic-gate 		}
2968*0Sstevel@tonic-gate 
2969*0Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
2970*0Sstevel@tonic-gate 
2971*0Sstevel@tonic-gate 		/*
2972*0Sstevel@tonic-gate 		 * We didn't register the instance with the restarter, so we
2973*0Sstevel@tonic-gate 		 * must set maintenance mode ourselves.
2974*0Sstevel@tonic-gate 		 */
2975*0Sstevel@tonic-gate 		err = libscf_instance_get_fmri(inst, (char **)&idata.i_fmri);
2976*0Sstevel@tonic-gate 		if (err != 0) {
2977*0Sstevel@tonic-gate 			assert(err == ECONNABORTED || err == ECANCELED);
2978*0Sstevel@tonic-gate 			return (err);
2979*0Sstevel@tonic-gate 		}
2980*0Sstevel@tonic-gate 
2981*0Sstevel@tonic-gate 		idata.i_state = RESTARTER_STATE_NONE;
2982*0Sstevel@tonic-gate 		idata.i_next_state = RESTARTER_STATE_NONE;
2983*0Sstevel@tonic-gate 
2984*0Sstevel@tonic-gate set_maint:
2985*0Sstevel@tonic-gate 		switch (err = _restarter_commit_states(h, &idata,
2986*0Sstevel@tonic-gate 		    RESTARTER_STATE_MAINT, RESTARTER_STATE_NONE, reason)) {
2987*0Sstevel@tonic-gate 		case 0:
2988*0Sstevel@tonic-gate 			break;
2989*0Sstevel@tonic-gate 
2990*0Sstevel@tonic-gate 		case ENOMEM:
2991*0Sstevel@tonic-gate 			++count;
2992*0Sstevel@tonic-gate 			if (count < ALLOC_RETRY) {
2993*0Sstevel@tonic-gate 				(void) poll(NULL, 0, msecs);
2994*0Sstevel@tonic-gate 				msecs *= ALLOC_DELAY_MULT;
2995*0Sstevel@tonic-gate 				goto set_maint;
2996*0Sstevel@tonic-gate 			}
2997*0Sstevel@tonic-gate 
2998*0Sstevel@tonic-gate 			uu_die("Insufficient memory.\n");
2999*0Sstevel@tonic-gate 			/* NOTREACHED */
3000*0Sstevel@tonic-gate 
3001*0Sstevel@tonic-gate 		case ECONNABORTED:
3002*0Sstevel@tonic-gate 			return (ECONNABORTED);
3003*0Sstevel@tonic-gate 
3004*0Sstevel@tonic-gate 		case ENOENT:
3005*0Sstevel@tonic-gate 			return (ECANCELED);
3006*0Sstevel@tonic-gate 
3007*0Sstevel@tonic-gate 		case EPERM:
3008*0Sstevel@tonic-gate 		case EACCES:
3009*0Sstevel@tonic-gate 		case EROFS:
3010*0Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not initialize state for "
3011*0Sstevel@tonic-gate 			    "%s: %s.\n", idata.i_fmri, strerror(err));
3012*0Sstevel@tonic-gate 			break;
3013*0Sstevel@tonic-gate 
3014*0Sstevel@tonic-gate 		case EINVAL:
3015*0Sstevel@tonic-gate 		default:
3016*0Sstevel@tonic-gate 			bad_error("_restarter_commit_states", err);
3017*0Sstevel@tonic-gate 		}
3018*0Sstevel@tonic-gate 
3019*0Sstevel@tonic-gate 		startd_free((void *)idata.i_fmri, max_scf_fmri_size);
3020*0Sstevel@tonic-gate 
3021*0Sstevel@tonic-gate 		v->gv_state = RESTARTER_STATE_MAINT;
3022*0Sstevel@tonic-gate 
3023*0Sstevel@tonic-gate 		goto out;
3024*0Sstevel@tonic-gate 	}
3025*0Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_value_size);
3026*0Sstevel@tonic-gate 
3027*0Sstevel@tonic-gate 	/* Add all the other dependencies. */
3028*0Sstevel@tonic-gate 	err = refresh_vertex(v, inst);
3029*0Sstevel@tonic-gate 	if (err != 0) {
3030*0Sstevel@tonic-gate 		assert(err == ECONNABORTED);
3031*0Sstevel@tonic-gate 		return (err);
3032*0Sstevel@tonic-gate 	}
3033*0Sstevel@tonic-gate 
3034*0Sstevel@tonic-gate out:
3035*0Sstevel@tonic-gate 	v->gv_flags |= GV_CONFIGURED;
3036*0Sstevel@tonic-gate 
3037*0Sstevel@tonic-gate 	graph_enable_by_vertex(v, enabled, 0);
3038*0Sstevel@tonic-gate 
3039*0Sstevel@tonic-gate 	return (0);
3040*0Sstevel@tonic-gate }
3041*0Sstevel@tonic-gate 
3042*0Sstevel@tonic-gate static void
3043*0Sstevel@tonic-gate do_uadmin(void)
3044*0Sstevel@tonic-gate {
3045*0Sstevel@tonic-gate 	int fd, left;
3046*0Sstevel@tonic-gate 	struct statvfs vfs;
3047*0Sstevel@tonic-gate 
3048*0Sstevel@tonic-gate 	const char * const resetting = "/etc/svc/volatile/resetting";
3049*0Sstevel@tonic-gate 
3050*0Sstevel@tonic-gate 	fd = creat(resetting, 0777);
3051*0Sstevel@tonic-gate 	if (fd >= 0)
3052*0Sstevel@tonic-gate 		startd_close(fd);
3053*0Sstevel@tonic-gate 	else
3054*0Sstevel@tonic-gate 		uu_warn("Could not create \"%s\"", resetting);
3055*0Sstevel@tonic-gate 
3056*0Sstevel@tonic-gate 	/* Kill dhcpagent if we're not using nfs for root */
3057*0Sstevel@tonic-gate 	if ((statvfs("/", &vfs) == 0) &&
3058*0Sstevel@tonic-gate 	    (strncmp(vfs.f_basetype, "nfs", sizeof ("nfs") - 1) != 0))
3059*0Sstevel@tonic-gate 		(void) system("/usr/bin/pkill -x -u 0 dhcpagent");
3060*0Sstevel@tonic-gate 
3061*0Sstevel@tonic-gate 	(void) system("/usr/sbin/killall");
3062*0Sstevel@tonic-gate 	left = 5;
3063*0Sstevel@tonic-gate 	while (left > 0)
3064*0Sstevel@tonic-gate 		left = sleep(left);
3065*0Sstevel@tonic-gate 
3066*0Sstevel@tonic-gate 	(void) system("/usr/sbin/killall 9");
3067*0Sstevel@tonic-gate 	left = 10;
3068*0Sstevel@tonic-gate 	while (left > 0)
3069*0Sstevel@tonic-gate 		left = sleep(left);
3070*0Sstevel@tonic-gate 
3071*0Sstevel@tonic-gate 	sync();
3072*0Sstevel@tonic-gate 	sync();
3073*0Sstevel@tonic-gate 	sync();
3074*0Sstevel@tonic-gate 
3075*0Sstevel@tonic-gate 	(void) system("/sbin/umountall");
3076*0Sstevel@tonic-gate 	(void) system("/sbin/umount /tmp >/dev/null 2>&1");
3077*0Sstevel@tonic-gate 	(void) system("/sbin/umount /var/adm >/dev/null 2>&1");
3078*0Sstevel@tonic-gate 	(void) system("/sbin/umount /var/run >/dev/null 2>&1");
3079*0Sstevel@tonic-gate 	(void) system("/sbin/umount /var >/dev/null 2>&1");
3080*0Sstevel@tonic-gate 	(void) system("/sbin/umount /usr >/dev/null 2>&1");
3081*0Sstevel@tonic-gate 
3082*0Sstevel@tonic-gate 	uu_warn("The system is down.\n");
3083*0Sstevel@tonic-gate 
3084*0Sstevel@tonic-gate 	(void) uadmin(A_SHUTDOWN, halting, NULL);
3085*0Sstevel@tonic-gate 	uu_warn("uadmin() failed");
3086*0Sstevel@tonic-gate 
3087*0Sstevel@tonic-gate 	if (remove(resetting) != 0 && errno != ENOENT)
3088*0Sstevel@tonic-gate 		uu_warn("Could not remove \"%s\"", resetting);
3089*0Sstevel@tonic-gate }
3090*0Sstevel@tonic-gate 
3091*0Sstevel@tonic-gate /*
3092*0Sstevel@tonic-gate  * If any of the up_svcs[] are online or satisfiable, return true.  If they are
3093*0Sstevel@tonic-gate  * all missing, disabled, in maintenance, or unsatisfiable, return false.
3094*0Sstevel@tonic-gate  */
3095*0Sstevel@tonic-gate boolean_t
3096*0Sstevel@tonic-gate can_come_up(void)
3097*0Sstevel@tonic-gate {
3098*0Sstevel@tonic-gate 	int i;
3099*0Sstevel@tonic-gate 
3100*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
3101*0Sstevel@tonic-gate 
3102*0Sstevel@tonic-gate 	/*
3103*0Sstevel@tonic-gate 	 * If we are booting to single user (boot -s),
3104*0Sstevel@tonic-gate 	 * SCF_MILESTONE_SINGLE_USER is needed to come up because startd
3105*0Sstevel@tonic-gate 	 * spawns sulogin after single-user is online (see specials.c).
3106*0Sstevel@tonic-gate 	 */
3107*0Sstevel@tonic-gate 	i = (booting_to_single_user ? 0 : 1);
3108*0Sstevel@tonic-gate 
3109*0Sstevel@tonic-gate 	for (; up_svcs[i] != NULL; ++i) {
3110*0Sstevel@tonic-gate 		if (up_svcs_p[i] == NULL) {
3111*0Sstevel@tonic-gate 			up_svcs_p[i] = vertex_get_by_name(up_svcs[i]);
3112*0Sstevel@tonic-gate 
3113*0Sstevel@tonic-gate 			if (up_svcs_p[i] == NULL)
3114*0Sstevel@tonic-gate 				continue;
3115*0Sstevel@tonic-gate 		}
3116*0Sstevel@tonic-gate 
3117*0Sstevel@tonic-gate 		/*
3118*0Sstevel@tonic-gate 		 * Ignore unconfigured services (the ones that have been
3119*0Sstevel@tonic-gate 		 * mentioned in a dependency from other services, but do
3120*0Sstevel@tonic-gate 		 * not exist in the repository).  Services which exist
3121*0Sstevel@tonic-gate 		 * in the repository but don't have general/enabled
3122*0Sstevel@tonic-gate 		 * property will be also ignored.
3123*0Sstevel@tonic-gate 		 */
3124*0Sstevel@tonic-gate 		if (!(up_svcs_p[i]->gv_flags & GV_CONFIGURED))
3125*0Sstevel@tonic-gate 			continue;
3126*0Sstevel@tonic-gate 
3127*0Sstevel@tonic-gate 		switch (up_svcs_p[i]->gv_state) {
3128*0Sstevel@tonic-gate 		case RESTARTER_STATE_ONLINE:
3129*0Sstevel@tonic-gate 		case RESTARTER_STATE_DEGRADED:
3130*0Sstevel@tonic-gate 			/*
3131*0Sstevel@tonic-gate 			 * Deactivate verbose boot once a login service has been
3132*0Sstevel@tonic-gate 			 * reached.
3133*0Sstevel@tonic-gate 			 */
3134*0Sstevel@tonic-gate 			st->st_log_login_reached = 1;
3135*0Sstevel@tonic-gate 			/*FALLTHROUGH*/
3136*0Sstevel@tonic-gate 		case RESTARTER_STATE_UNINIT:
3137*0Sstevel@tonic-gate 			return (B_TRUE);
3138*0Sstevel@tonic-gate 
3139*0Sstevel@tonic-gate 		case RESTARTER_STATE_OFFLINE:
3140*0Sstevel@tonic-gate 			if (instance_satisfied(up_svcs_p[i], B_TRUE) != -1)
3141*0Sstevel@tonic-gate 				return (B_TRUE);
3142*0Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
3143*0Sstevel@tonic-gate 			    "can_come_up(): %s is unsatisfiable.\n",
3144*0Sstevel@tonic-gate 			    up_svcs_p[i]->gv_name);
3145*0Sstevel@tonic-gate 			continue;
3146*0Sstevel@tonic-gate 
3147*0Sstevel@tonic-gate 		case RESTARTER_STATE_DISABLED:
3148*0Sstevel@tonic-gate 		case RESTARTER_STATE_MAINT:
3149*0Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
3150*0Sstevel@tonic-gate 			    "can_come_up(): %s is in state %s.\n",
3151*0Sstevel@tonic-gate 			    up_svcs_p[i]->gv_name,
3152*0Sstevel@tonic-gate 			    instance_state_str[up_svcs_p[i]->gv_state]);
3153*0Sstevel@tonic-gate 			continue;
3154*0Sstevel@tonic-gate 
3155*0Sstevel@tonic-gate 		default:
3156*0Sstevel@tonic-gate #ifndef NDEBUG
3157*0Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex state %d.\n",
3158*0Sstevel@tonic-gate 			    __FILE__, __LINE__, up_svcs_p[i]->gv_state);
3159*0Sstevel@tonic-gate #endif
3160*0Sstevel@tonic-gate 			abort();
3161*0Sstevel@tonic-gate 		}
3162*0Sstevel@tonic-gate 	}
3163*0Sstevel@tonic-gate 
3164*0Sstevel@tonic-gate 	/*
3165*0Sstevel@tonic-gate 	 * In the seed repository, console-login is unsatisfiable because
3166*0Sstevel@tonic-gate 	 * services are missing.  To behave correctly in that case we don't want
3167*0Sstevel@tonic-gate 	 * to return false until manifest-import is online.
3168*0Sstevel@tonic-gate 	 */
3169*0Sstevel@tonic-gate 
3170*0Sstevel@tonic-gate 	if (manifest_import_p == NULL) {
3171*0Sstevel@tonic-gate 		manifest_import_p = vertex_get_by_name(manifest_import);
3172*0Sstevel@tonic-gate 
3173*0Sstevel@tonic-gate 		if (manifest_import_p == NULL)
3174*0Sstevel@tonic-gate 			return (B_FALSE);
3175*0Sstevel@tonic-gate 	}
3176*0Sstevel@tonic-gate 
3177*0Sstevel@tonic-gate 	switch (manifest_import_p->gv_state) {
3178*0Sstevel@tonic-gate 	case RESTARTER_STATE_ONLINE:
3179*0Sstevel@tonic-gate 	case RESTARTER_STATE_DEGRADED:
3180*0Sstevel@tonic-gate 	case RESTARTER_STATE_DISABLED:
3181*0Sstevel@tonic-gate 	case RESTARTER_STATE_MAINT:
3182*0Sstevel@tonic-gate 		break;
3183*0Sstevel@tonic-gate 
3184*0Sstevel@tonic-gate 	case RESTARTER_STATE_OFFLINE:
3185*0Sstevel@tonic-gate 		if (instance_satisfied(manifest_import_p, B_TRUE) == -1)
3186*0Sstevel@tonic-gate 			break;
3187*0Sstevel@tonic-gate 		/* FALLTHROUGH */
3188*0Sstevel@tonic-gate 
3189*0Sstevel@tonic-gate 	case RESTARTER_STATE_UNINIT:
3190*0Sstevel@tonic-gate 		return (B_TRUE);
3191*0Sstevel@tonic-gate 	}
3192*0Sstevel@tonic-gate 
3193*0Sstevel@tonic-gate 	return (B_FALSE);
3194*0Sstevel@tonic-gate }
3195*0Sstevel@tonic-gate 
3196*0Sstevel@tonic-gate /*
3197*0Sstevel@tonic-gate  * Runs sulogin.  Returns
3198*0Sstevel@tonic-gate  *   0 - success
3199*0Sstevel@tonic-gate  *   EALREADY - sulogin is already running
3200*0Sstevel@tonic-gate  *   EBUSY - console-login is running
3201*0Sstevel@tonic-gate  */
3202*0Sstevel@tonic-gate static int
3203*0Sstevel@tonic-gate run_sulogin(const char *msg)
3204*0Sstevel@tonic-gate {
3205*0Sstevel@tonic-gate 	graph_vertex_t *v;
3206*0Sstevel@tonic-gate 
3207*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
3208*0Sstevel@tonic-gate 
3209*0Sstevel@tonic-gate 	if (sulogin_running)
3210*0Sstevel@tonic-gate 		return (EALREADY);
3211*0Sstevel@tonic-gate 
3212*0Sstevel@tonic-gate 	v = vertex_get_by_name(console_login_fmri);
3213*0Sstevel@tonic-gate 	if (v != NULL && inst_running(v))
3214*0Sstevel@tonic-gate 		return (EBUSY);
3215*0Sstevel@tonic-gate 
3216*0Sstevel@tonic-gate 	sulogin_running = B_TRUE;
3217*0Sstevel@tonic-gate 
3218*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
3219*0Sstevel@tonic-gate 
3220*0Sstevel@tonic-gate 	fork_sulogin(B_FALSE, msg);
3221*0Sstevel@tonic-gate 
3222*0Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
3223*0Sstevel@tonic-gate 
3224*0Sstevel@tonic-gate 	sulogin_running = B_FALSE;
3225*0Sstevel@tonic-gate 
3226*0Sstevel@tonic-gate 	if (console_login_ready) {
3227*0Sstevel@tonic-gate 		v = vertex_get_by_name(console_login_fmri);
3228*0Sstevel@tonic-gate 
3229*0Sstevel@tonic-gate 		if (v != NULL && v->gv_state == RESTARTER_STATE_OFFLINE &&
3230*0Sstevel@tonic-gate 		    !inst_running(v)) {
3231*0Sstevel@tonic-gate 			if (v->gv_start_f == NULL)
3232*0Sstevel@tonic-gate 				vertex_send_event(v,
3233*0Sstevel@tonic-gate 				    RESTARTER_EVENT_TYPE_START);
3234*0Sstevel@tonic-gate 			else
3235*0Sstevel@tonic-gate 				v->gv_start_f(v);
3236*0Sstevel@tonic-gate 		}
3237*0Sstevel@tonic-gate 
3238*0Sstevel@tonic-gate 		console_login_ready = B_FALSE;
3239*0Sstevel@tonic-gate 	}
3240*0Sstevel@tonic-gate 
3241*0Sstevel@tonic-gate 	return (0);
3242*0Sstevel@tonic-gate }
3243*0Sstevel@tonic-gate 
3244*0Sstevel@tonic-gate /*
3245*0Sstevel@tonic-gate  * The sulogin thread runs sulogin while can_come_up() is false.  run_sulogin()
3246*0Sstevel@tonic-gate  * keeps sulogin from stepping on console-login's toes.
3247*0Sstevel@tonic-gate  */
3248*0Sstevel@tonic-gate /* ARGSUSED */
3249*0Sstevel@tonic-gate static void *
3250*0Sstevel@tonic-gate sulogin_thread(void *unused)
3251*0Sstevel@tonic-gate {
3252*0Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
3253*0Sstevel@tonic-gate 
3254*0Sstevel@tonic-gate 	assert(sulogin_thread_running);
3255*0Sstevel@tonic-gate 
3256*0Sstevel@tonic-gate 	do
3257*0Sstevel@tonic-gate 		(void) run_sulogin("Console login service(s) cannot run\n");
3258*0Sstevel@tonic-gate 	while (!can_come_up());
3259*0Sstevel@tonic-gate 
3260*0Sstevel@tonic-gate 	sulogin_thread_running = B_FALSE;
3261*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
3262*0Sstevel@tonic-gate 
3263*0Sstevel@tonic-gate 	return (NULL);
3264*0Sstevel@tonic-gate }
3265*0Sstevel@tonic-gate 
3266*0Sstevel@tonic-gate /* ARGSUSED */
3267*0Sstevel@tonic-gate void *
3268*0Sstevel@tonic-gate single_user_thread(void *unused)
3269*0Sstevel@tonic-gate {
3270*0Sstevel@tonic-gate 	uint_t left;
3271*0Sstevel@tonic-gate 	scf_handle_t *h;
3272*0Sstevel@tonic-gate 	scf_instance_t *inst;
3273*0Sstevel@tonic-gate 	scf_property_t *prop;
3274*0Sstevel@tonic-gate 	scf_value_t *val;
3275*0Sstevel@tonic-gate 	const char *msg;
3276*0Sstevel@tonic-gate 	char *buf;
3277*0Sstevel@tonic-gate 	int r;
3278*0Sstevel@tonic-gate 
3279*0Sstevel@tonic-gate 	MUTEX_LOCK(&single_user_thread_lock);
3280*0Sstevel@tonic-gate 	single_user_thread_count++;
3281*0Sstevel@tonic-gate 
3282*0Sstevel@tonic-gate 	if (!booting_to_single_user) {
3283*0Sstevel@tonic-gate 		/*
3284*0Sstevel@tonic-gate 		 * From rcS.sh: Look for ttymon, in.telnetd, in.rlogind and
3285*0Sstevel@tonic-gate 		 * processes in their process groups so they can be terminated.
3286*0Sstevel@tonic-gate 		 */
3287*0Sstevel@tonic-gate 		(void) fputs("svc.startd: Killing user processes: ", stdout);
3288*0Sstevel@tonic-gate 		(void) system("/usr/sbin/killall");
3289*0Sstevel@tonic-gate 		(void) system("/usr/sbin/killall 9");
3290*0Sstevel@tonic-gate 		(void) system("/usr/bin/pkill -TERM -v -u 0,1");
3291*0Sstevel@tonic-gate 
3292*0Sstevel@tonic-gate 		left = 5;
3293*0Sstevel@tonic-gate 		while (left > 0)
3294*0Sstevel@tonic-gate 			left = sleep(left);
3295*0Sstevel@tonic-gate 
3296*0Sstevel@tonic-gate 		(void) system("/usr/bin/pkill -KILL -v -u 0,1");
3297*0Sstevel@tonic-gate 		(void) puts("done.");
3298*0Sstevel@tonic-gate 	}
3299*0Sstevel@tonic-gate 
3300*0Sstevel@tonic-gate 	if (go_single_user_mode || booting_to_single_user) {
3301*0Sstevel@tonic-gate 		msg = "SINGLE USER MODE\n";
3302*0Sstevel@tonic-gate 	} else {
3303*0Sstevel@tonic-gate 		assert(go_to_level1);
3304*0Sstevel@tonic-gate 
3305*0Sstevel@tonic-gate 		fork_rc_script('1', "start", B_TRUE);
3306*0Sstevel@tonic-gate 
3307*0Sstevel@tonic-gate 		uu_warn("The system is ready for administration.\n");
3308*0Sstevel@tonic-gate 
3309*0Sstevel@tonic-gate 		msg = "";
3310*0Sstevel@tonic-gate 	}
3311*0Sstevel@tonic-gate 
3312*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&single_user_thread_lock);
3313*0Sstevel@tonic-gate 
3314*0Sstevel@tonic-gate 	for (;;) {
3315*0Sstevel@tonic-gate 		MUTEX_LOCK(&dgraph_lock);
3316*0Sstevel@tonic-gate 		r = run_sulogin(msg);
3317*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
3318*0Sstevel@tonic-gate 		if (r == 0)
3319*0Sstevel@tonic-gate 			break;
3320*0Sstevel@tonic-gate 
3321*0Sstevel@tonic-gate 		assert(r == EALREADY || r == EBUSY);
3322*0Sstevel@tonic-gate 
3323*0Sstevel@tonic-gate 		left = 3;
3324*0Sstevel@tonic-gate 		while (left > 0)
3325*0Sstevel@tonic-gate 			left = sleep(left);
3326*0Sstevel@tonic-gate 	}
3327*0Sstevel@tonic-gate 
3328*0Sstevel@tonic-gate 	MUTEX_LOCK(&single_user_thread_lock);
3329*0Sstevel@tonic-gate 
3330*0Sstevel@tonic-gate 	/*
3331*0Sstevel@tonic-gate 	 * If another single user thread has started, let it finish changing
3332*0Sstevel@tonic-gate 	 * the run level.
3333*0Sstevel@tonic-gate 	 */
3334*0Sstevel@tonic-gate 	if (single_user_thread_count > 1) {
3335*0Sstevel@tonic-gate 		single_user_thread_count--;
3336*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&single_user_thread_lock);
3337*0Sstevel@tonic-gate 		return (NULL);
3338*0Sstevel@tonic-gate 	}
3339*0Sstevel@tonic-gate 
3340*0Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
3341*0Sstevel@tonic-gate 	inst = scf_instance_create(h);
3342*0Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
3343*0Sstevel@tonic-gate 	val = safe_scf_value_create(h);
3344*0Sstevel@tonic-gate 	buf = startd_alloc(max_scf_fmri_size);
3345*0Sstevel@tonic-gate 
3346*0Sstevel@tonic-gate lookup:
3347*0Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
3348*0Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
3349*0Sstevel@tonic-gate 		switch (scf_error()) {
3350*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
3351*0Sstevel@tonic-gate 			r = libscf_create_self(h);
3352*0Sstevel@tonic-gate 			if (r == 0)
3353*0Sstevel@tonic-gate 				goto lookup;
3354*0Sstevel@tonic-gate 			assert(r == ECONNABORTED);
3355*0Sstevel@tonic-gate 			/* FALLTHROUGH */
3356*0Sstevel@tonic-gate 
3357*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
3358*0Sstevel@tonic-gate 			libscf_handle_rebind(h);
3359*0Sstevel@tonic-gate 			goto lookup;
3360*0Sstevel@tonic-gate 
3361*0Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
3362*0Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
3363*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
3364*0Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
3365*0Sstevel@tonic-gate 		default:
3366*0Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
3367*0Sstevel@tonic-gate 		}
3368*0Sstevel@tonic-gate 	}
3369*0Sstevel@tonic-gate 
3370*0Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
3371*0Sstevel@tonic-gate 
3372*0Sstevel@tonic-gate 	r = libscf_inst_delete_prop(inst, SCF_PG_OPTIONS_OVR,
3373*0Sstevel@tonic-gate 	    SCF_PROPERTY_MILESTONE);
3374*0Sstevel@tonic-gate 	switch (r) {
3375*0Sstevel@tonic-gate 	case 0:
3376*0Sstevel@tonic-gate 	case ECANCELED:
3377*0Sstevel@tonic-gate 		break;
3378*0Sstevel@tonic-gate 
3379*0Sstevel@tonic-gate 	case ECONNABORTED:
3380*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
3381*0Sstevel@tonic-gate 		libscf_handle_rebind(h);
3382*0Sstevel@tonic-gate 		goto lookup;
3383*0Sstevel@tonic-gate 
3384*0Sstevel@tonic-gate 	case EPERM:
3385*0Sstevel@tonic-gate 	case EACCES:
3386*0Sstevel@tonic-gate 	case EROFS:
3387*0Sstevel@tonic-gate 		log_error(LOG_WARNING, "Could not clear temporary milestone: "
3388*0Sstevel@tonic-gate 		    "%s.\n", strerror(r));
3389*0Sstevel@tonic-gate 		break;
3390*0Sstevel@tonic-gate 
3391*0Sstevel@tonic-gate 	default:
3392*0Sstevel@tonic-gate 		bad_error("libscf_inst_delete_prop", r);
3393*0Sstevel@tonic-gate 	}
3394*0Sstevel@tonic-gate 
3395*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
3396*0Sstevel@tonic-gate 
3397*0Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, buf, max_scf_fmri_size);
3398*0Sstevel@tonic-gate 	switch (r) {
3399*0Sstevel@tonic-gate 	case ECANCELED:
3400*0Sstevel@tonic-gate 	case ENOENT:
3401*0Sstevel@tonic-gate 	case EINVAL:
3402*0Sstevel@tonic-gate 		(void) strcpy(buf, "all");
3403*0Sstevel@tonic-gate 		/* FALLTHROUGH */
3404*0Sstevel@tonic-gate 
3405*0Sstevel@tonic-gate 	case 0:
3406*0Sstevel@tonic-gate 		uu_warn("Returning to milestone %s.\n", buf);
3407*0Sstevel@tonic-gate 		break;
3408*0Sstevel@tonic-gate 
3409*0Sstevel@tonic-gate 	case ECONNABORTED:
3410*0Sstevel@tonic-gate 		libscf_handle_rebind(h);
3411*0Sstevel@tonic-gate 		goto lookup;
3412*0Sstevel@tonic-gate 
3413*0Sstevel@tonic-gate 	default:
3414*0Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
3415*0Sstevel@tonic-gate 	}
3416*0Sstevel@tonic-gate 
3417*0Sstevel@tonic-gate 	r = dgraph_set_milestone(buf, h, B_FALSE);
3418*0Sstevel@tonic-gate 	switch (r) {
3419*0Sstevel@tonic-gate 	case 0:
3420*0Sstevel@tonic-gate 	case ECONNRESET:
3421*0Sstevel@tonic-gate 	case EALREADY:
3422*0Sstevel@tonic-gate 	case EINVAL:
3423*0Sstevel@tonic-gate 	case ENOENT:
3424*0Sstevel@tonic-gate 		break;
3425*0Sstevel@tonic-gate 
3426*0Sstevel@tonic-gate 	default:
3427*0Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
3428*0Sstevel@tonic-gate 	}
3429*0Sstevel@tonic-gate 
3430*0Sstevel@tonic-gate 	/*
3431*0Sstevel@tonic-gate 	 * See graph_runlevel_changed().
3432*0Sstevel@tonic-gate 	 */
3433*0Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
3434*0Sstevel@tonic-gate 	utmpx_set_runlevel(target_milestone_as_runlevel(), 'S', B_TRUE);
3435*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
3436*0Sstevel@tonic-gate 
3437*0Sstevel@tonic-gate 	startd_free(buf, max_scf_fmri_size);
3438*0Sstevel@tonic-gate 	scf_value_destroy(val);
3439*0Sstevel@tonic-gate 	scf_property_destroy(prop);
3440*0Sstevel@tonic-gate 	scf_instance_destroy(inst);
3441*0Sstevel@tonic-gate 	scf_handle_destroy(h);
3442*0Sstevel@tonic-gate 
3443*0Sstevel@tonic-gate 	/*
3444*0Sstevel@tonic-gate 	 * We'll give ourselves 3 seconds to respond to all of the enablings
3445*0Sstevel@tonic-gate 	 * that setting the milestone should have created before checking
3446*0Sstevel@tonic-gate 	 * whether to run sulogin.
3447*0Sstevel@tonic-gate 	 */
3448*0Sstevel@tonic-gate 	left = 3;
3449*0Sstevel@tonic-gate 	while (left > 0)
3450*0Sstevel@tonic-gate 		left = sleep(left);
3451*0Sstevel@tonic-gate 
3452*0Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
3453*0Sstevel@tonic-gate 	/*
3454*0Sstevel@tonic-gate 	 * Clearing these variables will allow the sulogin thread to run.  We
3455*0Sstevel@tonic-gate 	 * check here in case there aren't any more state updates anytime soon.
3456*0Sstevel@tonic-gate 	 */
3457*0Sstevel@tonic-gate 	go_to_level1 = go_single_user_mode = booting_to_single_user = B_FALSE;
3458*0Sstevel@tonic-gate 	if (!sulogin_thread_running && !can_come_up()) {
3459*0Sstevel@tonic-gate 		(void) startd_thread_create(sulogin_thread, NULL);
3460*0Sstevel@tonic-gate 		sulogin_thread_running = B_TRUE;
3461*0Sstevel@tonic-gate 	}
3462*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
3463*0Sstevel@tonic-gate 	single_user_thread_count--;
3464*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&single_user_thread_lock);
3465*0Sstevel@tonic-gate 	return (NULL);
3466*0Sstevel@tonic-gate }
3467*0Sstevel@tonic-gate 
3468*0Sstevel@tonic-gate 
3469*0Sstevel@tonic-gate /*
3470*0Sstevel@tonic-gate  * Dependency graph operations API.  These are handle-independent thread-safe
3471*0Sstevel@tonic-gate  * graph manipulation functions which are the entry points for the event
3472*0Sstevel@tonic-gate  * threads below.
3473*0Sstevel@tonic-gate  */
3474*0Sstevel@tonic-gate 
3475*0Sstevel@tonic-gate /*
3476*0Sstevel@tonic-gate  * If a configured vertex exists for inst_fmri, return EEXIST.  If no vertex
3477*0Sstevel@tonic-gate  * exists for inst_fmri, add one.  Then fetch the restarter from inst, make
3478*0Sstevel@tonic-gate  * this vertex dependent on it, and send _ADD_INSTANCE to the restarter.
3479*0Sstevel@tonic-gate  * Fetch whether the instance should be enabled from inst and send _ENABLE or
3480*0Sstevel@tonic-gate  * _DISABLE as appropriate.  Finally rummage through inst's dependency
3481*0Sstevel@tonic-gate  * property groups and add vertices and edges as appropriate.  If anything
3482*0Sstevel@tonic-gate  * goes wrong after sending _ADD_INSTANCE, send _ADMIN_MAINT_ON to put the
3483*0Sstevel@tonic-gate  * instance in maintenance.  Don't send _START or _STOP until we get a state
3484*0Sstevel@tonic-gate  * update in case we're being restarted and the service is already running.
3485*0Sstevel@tonic-gate  *
3486*0Sstevel@tonic-gate  * To support booting to a milestone, we must also make sure all dependencies
3487*0Sstevel@tonic-gate  * encountered are configured, if they exist in the repository.
3488*0Sstevel@tonic-gate  *
3489*0Sstevel@tonic-gate  * Returns 0 on success, ECONNABORTED on repository disconnection, EINVAL if
3490*0Sstevel@tonic-gate  * inst_fmri is an invalid (or not canonical) FMRI, ECANCELED if inst is
3491*0Sstevel@tonic-gate  * deleted, or EEXIST if a configured vertex for inst_fmri already exists.
3492*0Sstevel@tonic-gate  */
3493*0Sstevel@tonic-gate int
3494*0Sstevel@tonic-gate dgraph_add_instance(const char *inst_fmri, scf_instance_t *inst,
3495*0Sstevel@tonic-gate     boolean_t lock_graph)
3496*0Sstevel@tonic-gate {
3497*0Sstevel@tonic-gate 	graph_vertex_t *v;
3498*0Sstevel@tonic-gate 	int err;
3499*0Sstevel@tonic-gate 
3500*0Sstevel@tonic-gate 	if (strcmp(inst_fmri, SCF_SERVICE_STARTD) == 0)
3501*0Sstevel@tonic-gate 		return (0);
3502*0Sstevel@tonic-gate 
3503*0Sstevel@tonic-gate 	/* Check for a vertex for inst_fmri. */
3504*0Sstevel@tonic-gate 	if (lock_graph) {
3505*0Sstevel@tonic-gate 		MUTEX_LOCK(&dgraph_lock);
3506*0Sstevel@tonic-gate 	} else {
3507*0Sstevel@tonic-gate 		assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
3508*0Sstevel@tonic-gate 	}
3509*0Sstevel@tonic-gate 
3510*0Sstevel@tonic-gate 	v = vertex_get_by_name(inst_fmri);
3511*0Sstevel@tonic-gate 
3512*0Sstevel@tonic-gate 	if (v != NULL) {
3513*0Sstevel@tonic-gate 		assert(v->gv_type == GVT_INST);
3514*0Sstevel@tonic-gate 
3515*0Sstevel@tonic-gate 		if (v->gv_flags & GV_CONFIGURED) {
3516*0Sstevel@tonic-gate 			if (lock_graph)
3517*0Sstevel@tonic-gate 				MUTEX_UNLOCK(&dgraph_lock);
3518*0Sstevel@tonic-gate 			return (EEXIST);
3519*0Sstevel@tonic-gate 		}
3520*0Sstevel@tonic-gate 	} else {
3521*0Sstevel@tonic-gate 		/* Add the vertex. */
3522*0Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(inst_fmri, GVT_INST, 0,
3523*0Sstevel@tonic-gate 		    RERR_NONE, &v);
3524*0Sstevel@tonic-gate 		if (err != 0) {
3525*0Sstevel@tonic-gate 			assert(err == EINVAL);
3526*0Sstevel@tonic-gate 			if (lock_graph)
3527*0Sstevel@tonic-gate 				MUTEX_UNLOCK(&dgraph_lock);
3528*0Sstevel@tonic-gate 			return (EINVAL);
3529*0Sstevel@tonic-gate 		}
3530*0Sstevel@tonic-gate 	}
3531*0Sstevel@tonic-gate 
3532*0Sstevel@tonic-gate 	err = configure_vertex(v, inst);
3533*0Sstevel@tonic-gate 
3534*0Sstevel@tonic-gate 	if (lock_graph)
3535*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
3536*0Sstevel@tonic-gate 
3537*0Sstevel@tonic-gate 	return (err);
3538*0Sstevel@tonic-gate }
3539*0Sstevel@tonic-gate 
3540*0Sstevel@tonic-gate /*
3541*0Sstevel@tonic-gate  * Locate the vertex for this property group's instance.  If it doesn't exist
3542*0Sstevel@tonic-gate  * or is unconfigured, call dgraph_add_instance() & return.  Otherwise fetch
3543*0Sstevel@tonic-gate  * the restarter for the instance, and if it has changed, send
3544*0Sstevel@tonic-gate  * _REMOVE_INSTANCE to the old restarter, remove the dependency, make sure the
3545*0Sstevel@tonic-gate  * new restarter has a vertex, add a new dependency, and send _ADD_INSTANCE to
3546*0Sstevel@tonic-gate  * the new restarter.  Then fetch whether the instance should be enabled, and
3547*0Sstevel@tonic-gate  * if it is different from what we had, or if we changed the restarter, send
3548*0Sstevel@tonic-gate  * the appropriate _ENABLE or _DISABLE command.
3549*0Sstevel@tonic-gate  *
3550*0Sstevel@tonic-gate  * Returns 0 on success, ENOTSUP if the pg's parent is not an instance,
3551*0Sstevel@tonic-gate  * ECONNABORTED on repository disconnection, ECANCELED if the instance is
3552*0Sstevel@tonic-gate  * deleted, or -1 if the instance's general property group is deleted or if
3553*0Sstevel@tonic-gate  * its enabled property is misconfigured.
3554*0Sstevel@tonic-gate  */
3555*0Sstevel@tonic-gate static int
3556*0Sstevel@tonic-gate dgraph_update_general(scf_propertygroup_t *pg)
3557*0Sstevel@tonic-gate {
3558*0Sstevel@tonic-gate 	scf_handle_t *h;
3559*0Sstevel@tonic-gate 	scf_instance_t *inst;
3560*0Sstevel@tonic-gate 	char *fmri;
3561*0Sstevel@tonic-gate 	char *restarter_fmri;
3562*0Sstevel@tonic-gate 	graph_vertex_t *v;
3563*0Sstevel@tonic-gate 	int err;
3564*0Sstevel@tonic-gate 	int enabled, enabled_ovr;
3565*0Sstevel@tonic-gate 	int oldflags;
3566*0Sstevel@tonic-gate 
3567*0Sstevel@tonic-gate 	/* Find the vertex for this service */
3568*0Sstevel@tonic-gate 	h = scf_pg_handle(pg);
3569*0Sstevel@tonic-gate 
3570*0Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
3571*0Sstevel@tonic-gate 
3572*0Sstevel@tonic-gate 	if (scf_pg_get_parent_instance(pg, inst) != 0) {
3573*0Sstevel@tonic-gate 		switch (scf_error()) {
3574*0Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
3575*0Sstevel@tonic-gate 			return (ENOTSUP);
3576*0Sstevel@tonic-gate 
3577*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
3578*0Sstevel@tonic-gate 		default:
3579*0Sstevel@tonic-gate 			return (ECONNABORTED);
3580*0Sstevel@tonic-gate 
3581*0Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
3582*0Sstevel@tonic-gate 			return (0);
3583*0Sstevel@tonic-gate 
3584*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
3585*0Sstevel@tonic-gate 			bad_error("scf_pg_get_parent_instance", scf_error());
3586*0Sstevel@tonic-gate 		}
3587*0Sstevel@tonic-gate 	}
3588*0Sstevel@tonic-gate 
3589*0Sstevel@tonic-gate 	err = libscf_instance_get_fmri(inst, &fmri);
3590*0Sstevel@tonic-gate 	switch (err) {
3591*0Sstevel@tonic-gate 	case 0:
3592*0Sstevel@tonic-gate 		break;
3593*0Sstevel@tonic-gate 
3594*0Sstevel@tonic-gate 	case ECONNABORTED:
3595*0Sstevel@tonic-gate 		scf_instance_destroy(inst);
3596*0Sstevel@tonic-gate 		return (ECONNABORTED);
3597*0Sstevel@tonic-gate 
3598*0Sstevel@tonic-gate 	case ECANCELED:
3599*0Sstevel@tonic-gate 		scf_instance_destroy(inst);
3600*0Sstevel@tonic-gate 		return (0);
3601*0Sstevel@tonic-gate 
3602*0Sstevel@tonic-gate 	default:
3603*0Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", err);
3604*0Sstevel@tonic-gate 	}
3605*0Sstevel@tonic-gate 
3606*0Sstevel@tonic-gate 	log_framework(LOG_DEBUG,
3607*0Sstevel@tonic-gate 	    "Graph engine: Reloading general properties for %s.\n", fmri);
3608*0Sstevel@tonic-gate 
3609*0Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
3610*0Sstevel@tonic-gate 
3611*0Sstevel@tonic-gate 	v = vertex_get_by_name(fmri);
3612*0Sstevel@tonic-gate 	if (v == NULL || !(v->gv_flags & GV_CONFIGURED)) {
3613*0Sstevel@tonic-gate 		/* Will get the up-to-date properties. */
3614*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
3615*0Sstevel@tonic-gate 		err = dgraph_add_instance(fmri, inst, B_TRUE);
3616*0Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
3617*0Sstevel@tonic-gate 		scf_instance_destroy(inst);
3618*0Sstevel@tonic-gate 		return (err == ECANCELED ? 0 : err);
3619*0Sstevel@tonic-gate 	}
3620*0Sstevel@tonic-gate 
3621*0Sstevel@tonic-gate 	/* Read enabled & restarter from repository. */
3622*0Sstevel@tonic-gate 	restarter_fmri = startd_alloc(max_scf_value_size);
3623*0Sstevel@tonic-gate 	err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled,
3624*0Sstevel@tonic-gate 	    &enabled_ovr, &restarter_fmri);
3625*0Sstevel@tonic-gate 	if (err != 0 || enabled == -1) {
3626*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
3627*0Sstevel@tonic-gate 		scf_instance_destroy(inst);
3628*0Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
3629*0Sstevel@tonic-gate 
3630*0Sstevel@tonic-gate 		switch (err) {
3631*0Sstevel@tonic-gate 		case ENOENT:
3632*0Sstevel@tonic-gate 		case 0:
3633*0Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
3634*0Sstevel@tonic-gate 			return (-1);
3635*0Sstevel@tonic-gate 
3636*0Sstevel@tonic-gate 		case ECONNABORTED:
3637*0Sstevel@tonic-gate 		case ECANCELED:
3638*0Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
3639*0Sstevel@tonic-gate 			return (err);
3640*0Sstevel@tonic-gate 
3641*0Sstevel@tonic-gate 		default:
3642*0Sstevel@tonic-gate 			bad_error("libscf_get_basic_instance_data", err);
3643*0Sstevel@tonic-gate 		}
3644*0Sstevel@tonic-gate 	}
3645*0Sstevel@tonic-gate 
3646*0Sstevel@tonic-gate 	oldflags = v->gv_flags;
3647*0Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) |
3648*0Sstevel@tonic-gate 	    (enabled ? GV_ENBLD_NOOVR : 0);
3649*0Sstevel@tonic-gate 
3650*0Sstevel@tonic-gate 	if (enabled_ovr != -1)
3651*0Sstevel@tonic-gate 		enabled = enabled_ovr;
3652*0Sstevel@tonic-gate 
3653*0Sstevel@tonic-gate 	/*
3654*0Sstevel@tonic-gate 	 * If GV_ENBLD_NOOVR has changed, then we need to re-evaluate the
3655*0Sstevel@tonic-gate 	 * subgraph.
3656*0Sstevel@tonic-gate 	 */
3657*0Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE && v->gv_flags != oldflags)
3658*0Sstevel@tonic-gate 		(void) eval_subgraph(v, h);
3659*0Sstevel@tonic-gate 
3660*0Sstevel@tonic-gate 	scf_instance_destroy(inst);
3661*0Sstevel@tonic-gate 
3662*0Sstevel@tonic-gate 	/* Ignore restarter change for now. */
3663*0Sstevel@tonic-gate 
3664*0Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_value_size);
3665*0Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
3666*0Sstevel@tonic-gate 
3667*0Sstevel@tonic-gate 	/*
3668*0Sstevel@tonic-gate 	 * Always send _ENABLE or _DISABLE.  We could avoid this if the
3669*0Sstevel@tonic-gate 	 * restarter didn't change and the enabled value didn't change, but
3670*0Sstevel@tonic-gate 	 * that's not easy to check and improbable anyway, so we'll just do
3671*0Sstevel@tonic-gate 	 * this.
3672*0Sstevel@tonic-gate 	 */
3673*0Sstevel@tonic-gate 	graph_enable_by_vertex(v, enabled, 1);
3674*0Sstevel@tonic-gate 
3675*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
3676*0Sstevel@tonic-gate 
3677*0Sstevel@tonic-gate 	return (0);
3678*0Sstevel@tonic-gate }
3679*0Sstevel@tonic-gate 
3680*0Sstevel@tonic-gate /*
3681*0Sstevel@tonic-gate  * Delete all of the property group dependencies of v, update inst's running
3682*0Sstevel@tonic-gate  * snapshot, and add the dependencies in the new snapshot.  If any of the new
3683*0Sstevel@tonic-gate  * dependencies would create a cycle, send _ADMIN_MAINT_ON.  Otherwise
3684*0Sstevel@tonic-gate  * reevaluate v's dependencies, send _START or _STOP as appropriate, and do
3685*0Sstevel@tonic-gate  * the same for v's dependents.
3686*0Sstevel@tonic-gate  *
3687*0Sstevel@tonic-gate  * Returns
3688*0Sstevel@tonic-gate  *   0 - success
3689*0Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
3690*0Sstevel@tonic-gate  *   ECANCELED - inst was deleted
3691*0Sstevel@tonic-gate  *   EINVAL - inst is invalid (e.g., missing general/enabled)
3692*0Sstevel@tonic-gate  *   -1 - libscf_snapshots_refresh() failed
3693*0Sstevel@tonic-gate  */
3694*0Sstevel@tonic-gate static int
3695*0Sstevel@tonic-gate dgraph_refresh_instance(graph_vertex_t *v, scf_instance_t *inst)
3696*0Sstevel@tonic-gate {
3697*0Sstevel@tonic-gate 	int r;
3698*0Sstevel@tonic-gate 	int enabled;
3699*0Sstevel@tonic-gate 
3700*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
3701*0Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
3702*0Sstevel@tonic-gate 
3703*0Sstevel@tonic-gate 	/* Only refresh services with valid general/enabled properties. */
3704*0Sstevel@tonic-gate 	r = libscf_get_basic_instance_data(scf_instance_handle(inst), inst,
3705*0Sstevel@tonic-gate 	    v->gv_name, &enabled, NULL, NULL);
3706*0Sstevel@tonic-gate 	switch (r) {
3707*0Sstevel@tonic-gate 	case 0:
3708*0Sstevel@tonic-gate 		break;
3709*0Sstevel@tonic-gate 
3710*0Sstevel@tonic-gate 	case ECONNABORTED:
3711*0Sstevel@tonic-gate 	case ECANCELED:
3712*0Sstevel@tonic-gate 		return (r);
3713*0Sstevel@tonic-gate 
3714*0Sstevel@tonic-gate 	case ENOENT:
3715*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
3716*0Sstevel@tonic-gate 		    "Ignoring %s because it has no general property group.\n",
3717*0Sstevel@tonic-gate 		    v->gv_name);
3718*0Sstevel@tonic-gate 		return (EINVAL);
3719*0Sstevel@tonic-gate 
3720*0Sstevel@tonic-gate 	default:
3721*0Sstevel@tonic-gate 		bad_error("libscf_get_basic_instance_data", r);
3722*0Sstevel@tonic-gate 	}
3723*0Sstevel@tonic-gate 
3724*0Sstevel@tonic-gate 	if (enabled == -1)
3725*0Sstevel@tonic-gate 		return (EINVAL);
3726*0Sstevel@tonic-gate 
3727*0Sstevel@tonic-gate 	r = libscf_snapshots_refresh(inst, v->gv_name);
3728*0Sstevel@tonic-gate 	if (r != 0) {
3729*0Sstevel@tonic-gate 		if (r != -1)
3730*0Sstevel@tonic-gate 			bad_error("libscf_snapshots_refresh", r);
3731*0Sstevel@tonic-gate 
3732*0Sstevel@tonic-gate 		/* error logged */
3733*0Sstevel@tonic-gate 		return (r);
3734*0Sstevel@tonic-gate 	}
3735*0Sstevel@tonic-gate 
3736*0Sstevel@tonic-gate 	r = refresh_vertex(v, inst);
3737*0Sstevel@tonic-gate 	if (r != 0 && r != ECONNABORTED)
3738*0Sstevel@tonic-gate 		bad_error("refresh_vertex", r);
3739*0Sstevel@tonic-gate 	return (r);
3740*0Sstevel@tonic-gate }
3741*0Sstevel@tonic-gate 
3742*0Sstevel@tonic-gate /*
3743*0Sstevel@tonic-gate  * Returns 1 if any instances which directly depend on the passed instance
3744*0Sstevel@tonic-gate  * (or it's service) are running.
3745*0Sstevel@tonic-gate  */
3746*0Sstevel@tonic-gate static int
3747*0Sstevel@tonic-gate has_running_nonsubgraph_dependents(graph_vertex_t *v)
3748*0Sstevel@tonic-gate {
3749*0Sstevel@tonic-gate 	graph_vertex_t *vv;
3750*0Sstevel@tonic-gate 	graph_edge_t *e;
3751*0Sstevel@tonic-gate 
3752*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
3753*0Sstevel@tonic-gate 
3754*0Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
3755*0Sstevel@tonic-gate 	    e != NULL;
3756*0Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e)) {
3757*0Sstevel@tonic-gate 
3758*0Sstevel@tonic-gate 		vv = e->ge_vertex;
3759*0Sstevel@tonic-gate 		if (vv->gv_type == GVT_INST) {
3760*0Sstevel@tonic-gate 			if (inst_running(vv) &&
3761*0Sstevel@tonic-gate 			    ((vv->gv_flags & GV_INSUBGRAPH) == 0))
3762*0Sstevel@tonic-gate 				return (1);
3763*0Sstevel@tonic-gate 		} else {
3764*0Sstevel@tonic-gate 			/*
3765*0Sstevel@tonic-gate 			 * For dependency group or service vertices, keep
3766*0Sstevel@tonic-gate 			 * traversing to see if instances are running.
3767*0Sstevel@tonic-gate 			 */
3768*0Sstevel@tonic-gate 			if (has_running_nonsubgraph_dependents(vv))
3769*0Sstevel@tonic-gate 				return (1);
3770*0Sstevel@tonic-gate 		}
3771*0Sstevel@tonic-gate 	}
3772*0Sstevel@tonic-gate 	return (0);
3773*0Sstevel@tonic-gate }
3774*0Sstevel@tonic-gate 
3775*0Sstevel@tonic-gate /*
3776*0Sstevel@tonic-gate  * For the dependency, disable the instance which makes up the dependency if
3777*0Sstevel@tonic-gate  * it is not in the subgraph and running.  If the dependency instance is in
3778*0Sstevel@tonic-gate  * the subgraph or it is not running, continue by disabling all of it's
3779*0Sstevel@tonic-gate  * non-subgraph dependencies.
3780*0Sstevel@tonic-gate  */
3781*0Sstevel@tonic-gate static void
3782*0Sstevel@tonic-gate disable_nonsubgraph_dependencies(graph_vertex_t *v, void *arg)
3783*0Sstevel@tonic-gate {
3784*0Sstevel@tonic-gate 	int r;
3785*0Sstevel@tonic-gate 	scf_handle_t *h = (scf_handle_t *)arg;
3786*0Sstevel@tonic-gate 	scf_instance_t *inst = NULL;
3787*0Sstevel@tonic-gate 
3788*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
3789*0Sstevel@tonic-gate 
3790*0Sstevel@tonic-gate 	/* Continue recursing non-inst nodes */
3791*0Sstevel@tonic-gate 	if (v->gv_type != GVT_INST)
3792*0Sstevel@tonic-gate 		goto recurse;
3793*0Sstevel@tonic-gate 
3794*0Sstevel@tonic-gate 	/*
3795*0Sstevel@tonic-gate 	 * For instances that are in the subgraph or already not running,
3796*0Sstevel@tonic-gate 	 * skip and attempt to disable their non-dependencies.
3797*0Sstevel@tonic-gate 	 */
3798*0Sstevel@tonic-gate 	if ((v->gv_flags & GV_INSUBGRAPH) || (!inst_running(v)))
3799*0Sstevel@tonic-gate 		goto recurse;
3800*0Sstevel@tonic-gate 
3801*0Sstevel@tonic-gate 	/*
3802*0Sstevel@tonic-gate 	 * If not all this instance's dependents have stopped
3803*0Sstevel@tonic-gate 	 * running, do not disable.
3804*0Sstevel@tonic-gate 	 */
3805*0Sstevel@tonic-gate 	if (has_running_nonsubgraph_dependents(v))
3806*0Sstevel@tonic-gate 		return;
3807*0Sstevel@tonic-gate 
3808*0Sstevel@tonic-gate 	inst = scf_instance_create(h);
3809*0Sstevel@tonic-gate 	if (inst == NULL) {
3810*0Sstevel@tonic-gate 		log_error(LOG_WARNING, "Unable to gracefully disable instance:"
3811*0Sstevel@tonic-gate 		    "  %s due to lack of resources\n", v->gv_name);
3812*0Sstevel@tonic-gate 		goto disable;
3813*0Sstevel@tonic-gate 	}
3814*0Sstevel@tonic-gate again:
3815*0Sstevel@tonic-gate 	r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst,
3816*0Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT);
3817*0Sstevel@tonic-gate 	if (r != 0) {
3818*0Sstevel@tonic-gate 		switch (scf_error()) {
3819*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
3820*0Sstevel@tonic-gate 			libscf_handle_rebind(h);
3821*0Sstevel@tonic-gate 			goto again;
3822*0Sstevel@tonic-gate 
3823*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
3824*0Sstevel@tonic-gate 			goto recurse;
3825*0Sstevel@tonic-gate 
3826*0Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
3827*0Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
3828*0Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
3829*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
3830*0Sstevel@tonic-gate 		default:
3831*0Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri",
3832*0Sstevel@tonic-gate 			    scf_error());
3833*0Sstevel@tonic-gate 		}
3834*0Sstevel@tonic-gate 	}
3835*0Sstevel@tonic-gate 	r = libscf_set_enable_ovr(inst, 0);
3836*0Sstevel@tonic-gate 	switch (r) {
3837*0Sstevel@tonic-gate 	case 0:
3838*0Sstevel@tonic-gate 		scf_instance_destroy(inst);
3839*0Sstevel@tonic-gate 		return;
3840*0Sstevel@tonic-gate 	case ECANCELED:
3841*0Sstevel@tonic-gate 		scf_instance_destroy(inst);
3842*0Sstevel@tonic-gate 		goto recurse;
3843*0Sstevel@tonic-gate 	case ECONNABORTED:
3844*0Sstevel@tonic-gate 		libscf_handle_rebind(h);
3845*0Sstevel@tonic-gate 		goto again;
3846*0Sstevel@tonic-gate 	case EPERM:
3847*0Sstevel@tonic-gate 	case EROFS:
3848*0Sstevel@tonic-gate 		log_error(LOG_WARNING,
3849*0Sstevel@tonic-gate 		    "Could not set %s/%s for %s: %s.\n",
3850*0Sstevel@tonic-gate 		    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
3851*0Sstevel@tonic-gate 		    v->gv_name, strerror(r));
3852*0Sstevel@tonic-gate 		goto disable;
3853*0Sstevel@tonic-gate 	default:
3854*0Sstevel@tonic-gate 		bad_error("libscf_set_enable_ovr", r);
3855*0Sstevel@tonic-gate 	}
3856*0Sstevel@tonic-gate disable:
3857*0Sstevel@tonic-gate 	graph_enable_by_vertex(v, 0, 0);
3858*0Sstevel@tonic-gate 	return;
3859*0Sstevel@tonic-gate recurse:
3860*0Sstevel@tonic-gate 	graph_walk_dependencies(v, disable_nonsubgraph_dependencies,
3861*0Sstevel@tonic-gate 	    arg);
3862*0Sstevel@tonic-gate }
3863*0Sstevel@tonic-gate 
3864*0Sstevel@tonic-gate /*
3865*0Sstevel@tonic-gate  * Find the vertex for inst_name.  If it doesn't exist, return ENOENT.
3866*0Sstevel@tonic-gate  * Otherwise set its state to state.  If the instance has entered a state
3867*0Sstevel@tonic-gate  * which requires automatic action, take it (Uninitialized: do
3868*0Sstevel@tonic-gate  * dgraph_refresh_instance() without the snapshot update.  Disabled: if the
3869*0Sstevel@tonic-gate  * instance should be enabled, send _ENABLE.  Offline: if the instance should
3870*0Sstevel@tonic-gate  * be disabled, send _DISABLE, and if its dependencies are satisfied, send
3871*0Sstevel@tonic-gate  * _START.  Online, Degraded: if the instance wasn't running, update its start
3872*0Sstevel@tonic-gate  * snapshot.  Maintenance: no action.)
3873*0Sstevel@tonic-gate  *
3874*0Sstevel@tonic-gate  * Also fails with ECONNABORTED, or EINVAL if state is invalid.
3875*0Sstevel@tonic-gate  */
3876*0Sstevel@tonic-gate static int
3877*0Sstevel@tonic-gate dgraph_set_instance_state(scf_handle_t *h, const char *inst_name,
3878*0Sstevel@tonic-gate     restarter_instance_state_t state, restarter_error_t serr)
3879*0Sstevel@tonic-gate {
3880*0Sstevel@tonic-gate 	graph_vertex_t *v;
3881*0Sstevel@tonic-gate 	int err = 0, r;
3882*0Sstevel@tonic-gate 	int was_running, up_or_down;
3883*0Sstevel@tonic-gate 	restarter_instance_state_t old_state;
3884*0Sstevel@tonic-gate 
3885*0Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
3886*0Sstevel@tonic-gate 
3887*0Sstevel@tonic-gate 	v = vertex_get_by_name(inst_name);
3888*0Sstevel@tonic-gate 	if (v == NULL) {
3889*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
3890*0Sstevel@tonic-gate 		return (ENOENT);
3891*0Sstevel@tonic-gate 	}
3892*0Sstevel@tonic-gate 
3893*0Sstevel@tonic-gate 	switch (state) {
3894*0Sstevel@tonic-gate 	case RESTARTER_STATE_UNINIT:
3895*0Sstevel@tonic-gate 	case RESTARTER_STATE_DISABLED:
3896*0Sstevel@tonic-gate 	case RESTARTER_STATE_OFFLINE:
3897*0Sstevel@tonic-gate 	case RESTARTER_STATE_ONLINE:
3898*0Sstevel@tonic-gate 	case RESTARTER_STATE_DEGRADED:
3899*0Sstevel@tonic-gate 	case RESTARTER_STATE_MAINT:
3900*0Sstevel@tonic-gate 		break;
3901*0Sstevel@tonic-gate 
3902*0Sstevel@tonic-gate 	default:
3903*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
3904*0Sstevel@tonic-gate 		return (EINVAL);
3905*0Sstevel@tonic-gate 	}
3906*0Sstevel@tonic-gate 
3907*0Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph noting %s %s -> %s.\n", v->gv_name,
3908*0Sstevel@tonic-gate 	    instance_state_str[v->gv_state], instance_state_str[state]);
3909*0Sstevel@tonic-gate 
3910*0Sstevel@tonic-gate 	old_state = v->gv_state;
3911*0Sstevel@tonic-gate 	was_running = inst_running(v);
3912*0Sstevel@tonic-gate 
3913*0Sstevel@tonic-gate 	v->gv_state = state;
3914*0Sstevel@tonic-gate 
3915*0Sstevel@tonic-gate 	up_or_down = was_running ^ inst_running(v);
3916*0Sstevel@tonic-gate 
3917*0Sstevel@tonic-gate 	if (up_or_down && milestone != NULL && !inst_running(v) &&
3918*0Sstevel@tonic-gate 	    ((v->gv_flags & GV_INSUBGRAPH) == 0 ||
3919*0Sstevel@tonic-gate 	    milestone == MILESTONE_NONE)) {
3920*0Sstevel@tonic-gate 		--non_subgraph_svcs;
3921*0Sstevel@tonic-gate 		if (non_subgraph_svcs == 0) {
3922*0Sstevel@tonic-gate 			if (halting != -1) {
3923*0Sstevel@tonic-gate 				do_uadmin();
3924*0Sstevel@tonic-gate 			} else if (go_single_user_mode || go_to_level1) {
3925*0Sstevel@tonic-gate 				(void) startd_thread_create(single_user_thread,
3926*0Sstevel@tonic-gate 				    NULL);
3927*0Sstevel@tonic-gate 			}
3928*0Sstevel@tonic-gate 		} else {
3929*0Sstevel@tonic-gate 			graph_walk_dependencies(v,
3930*0Sstevel@tonic-gate 			    disable_nonsubgraph_dependencies, (void *)h);
3931*0Sstevel@tonic-gate 		}
3932*0Sstevel@tonic-gate 	}
3933*0Sstevel@tonic-gate 
3934*0Sstevel@tonic-gate 	switch (state) {
3935*0Sstevel@tonic-gate 	case RESTARTER_STATE_UNINIT: {
3936*0Sstevel@tonic-gate 		scf_instance_t *inst;
3937*0Sstevel@tonic-gate 
3938*0Sstevel@tonic-gate 		/* Initialize instance by refreshing it. */
3939*0Sstevel@tonic-gate 
3940*0Sstevel@tonic-gate 		err = libscf_fmri_get_instance(h, v->gv_name, &inst);
3941*0Sstevel@tonic-gate 		switch (err) {
3942*0Sstevel@tonic-gate 		case 0:
3943*0Sstevel@tonic-gate 			break;
3944*0Sstevel@tonic-gate 
3945*0Sstevel@tonic-gate 		case ECONNABORTED:
3946*0Sstevel@tonic-gate 			MUTEX_UNLOCK(&dgraph_lock);
3947*0Sstevel@tonic-gate 			return (ECONNABORTED);
3948*0Sstevel@tonic-gate 
3949*0Sstevel@tonic-gate 		case ENOENT:
3950*0Sstevel@tonic-gate 			MUTEX_UNLOCK(&dgraph_lock);
3951*0Sstevel@tonic-gate 			return (0);
3952*0Sstevel@tonic-gate 
3953*0Sstevel@tonic-gate 		case EINVAL:
3954*0Sstevel@tonic-gate 		case ENOTSUP:
3955*0Sstevel@tonic-gate 		default:
3956*0Sstevel@tonic-gate 			bad_error("libscf_fmri_get_instance", err);
3957*0Sstevel@tonic-gate 		}
3958*0Sstevel@tonic-gate 
3959*0Sstevel@tonic-gate 		err = refresh_vertex(v, inst);
3960*0Sstevel@tonic-gate 		if (err == 0)
3961*0Sstevel@tonic-gate 			graph_enable_by_vertex(v, v->gv_flags & GV_ENABLED, 0);
3962*0Sstevel@tonic-gate 
3963*0Sstevel@tonic-gate 		scf_instance_destroy(inst);
3964*0Sstevel@tonic-gate 		break;
3965*0Sstevel@tonic-gate 	}
3966*0Sstevel@tonic-gate 
3967*0Sstevel@tonic-gate 	case RESTARTER_STATE_DISABLED:
3968*0Sstevel@tonic-gate 		/*
3969*0Sstevel@tonic-gate 		 * If the instance should be disabled, no problem.  Otherwise,
3970*0Sstevel@tonic-gate 		 * send an enable command, which should result in the instance
3971*0Sstevel@tonic-gate 		 * moving to OFFLINE.
3972*0Sstevel@tonic-gate 		 */
3973*0Sstevel@tonic-gate 		if (v->gv_flags & GV_ENABLED) {
3974*0Sstevel@tonic-gate 			vertex_send_event(v, RESTARTER_EVENT_TYPE_ENABLE);
3975*0Sstevel@tonic-gate 		} else if (was_running && v->gv_post_disable_f) {
3976*0Sstevel@tonic-gate 			v->gv_post_disable_f();
3977*0Sstevel@tonic-gate 		}
3978*0Sstevel@tonic-gate 		break;
3979*0Sstevel@tonic-gate 
3980*0Sstevel@tonic-gate 	case RESTARTER_STATE_OFFLINE:
3981*0Sstevel@tonic-gate 		/*
3982*0Sstevel@tonic-gate 		 * If the instance should be enabled, see if we can start it.
3983*0Sstevel@tonic-gate 		 * Otherwise send a disable command.
3984*0Sstevel@tonic-gate 		 */
3985*0Sstevel@tonic-gate 		if (v->gv_flags & GV_ENABLED) {
3986*0Sstevel@tonic-gate 			if (instance_satisfied(v, B_FALSE) == 1) {
3987*0Sstevel@tonic-gate 				if (v->gv_start_f == NULL) {
3988*0Sstevel@tonic-gate 					vertex_send_event(v,
3989*0Sstevel@tonic-gate 					    RESTARTER_EVENT_TYPE_START);
3990*0Sstevel@tonic-gate 				} else {
3991*0Sstevel@tonic-gate 					v->gv_start_f(v);
3992*0Sstevel@tonic-gate 				}
3993*0Sstevel@tonic-gate 			} else {
3994*0Sstevel@tonic-gate 				log_framework(LOG_DEBUG,
3995*0Sstevel@tonic-gate 				    "Dependencies of %s not satisfied, "
3996*0Sstevel@tonic-gate 				    "not starting.\n", v->gv_name);
3997*0Sstevel@tonic-gate 			}
3998*0Sstevel@tonic-gate 		} else {
3999*0Sstevel@tonic-gate 			if (was_running && v->gv_post_disable_f)
4000*0Sstevel@tonic-gate 				v->gv_post_disable_f();
4001*0Sstevel@tonic-gate 			vertex_send_event(v, RESTARTER_EVENT_TYPE_DISABLE);
4002*0Sstevel@tonic-gate 		}
4003*0Sstevel@tonic-gate 		break;
4004*0Sstevel@tonic-gate 
4005*0Sstevel@tonic-gate 	case RESTARTER_STATE_ONLINE:
4006*0Sstevel@tonic-gate 	case RESTARTER_STATE_DEGRADED:
4007*0Sstevel@tonic-gate 		/*
4008*0Sstevel@tonic-gate 		 * If the instance has just come up, update the start
4009*0Sstevel@tonic-gate 		 * snapshot.
4010*0Sstevel@tonic-gate 		 */
4011*0Sstevel@tonic-gate 		if (!was_running) {
4012*0Sstevel@tonic-gate 			/*
4013*0Sstevel@tonic-gate 			 * Don't fire if we're just recovering state
4014*0Sstevel@tonic-gate 			 * after a restart.
4015*0Sstevel@tonic-gate 			 */
4016*0Sstevel@tonic-gate 			if (old_state != RESTARTER_STATE_UNINIT &&
4017*0Sstevel@tonic-gate 			    v->gv_post_online_f)
4018*0Sstevel@tonic-gate 				v->gv_post_online_f();
4019*0Sstevel@tonic-gate 
4020*0Sstevel@tonic-gate 			r = libscf_snapshots_poststart(h, v->gv_name, B_TRUE);
4021*0Sstevel@tonic-gate 			switch (r) {
4022*0Sstevel@tonic-gate 			case 0:
4023*0Sstevel@tonic-gate 			case ENOENT:
4024*0Sstevel@tonic-gate 				/*
4025*0Sstevel@tonic-gate 				 * If ENOENT, the instance must have been
4026*0Sstevel@tonic-gate 				 * deleted.  Pretend we were successful since
4027*0Sstevel@tonic-gate 				 * we should get a delete event later.
4028*0Sstevel@tonic-gate 				 */
4029*0Sstevel@tonic-gate 				break;
4030*0Sstevel@tonic-gate 
4031*0Sstevel@tonic-gate 			case ECONNABORTED:
4032*0Sstevel@tonic-gate 				MUTEX_UNLOCK(&dgraph_lock);
4033*0Sstevel@tonic-gate 				return (ECONNABORTED);
4034*0Sstevel@tonic-gate 
4035*0Sstevel@tonic-gate 			case EACCES:
4036*0Sstevel@tonic-gate 			case ENOTSUP:
4037*0Sstevel@tonic-gate 			default:
4038*0Sstevel@tonic-gate 				bad_error("libscf_snapshots_poststart", r);
4039*0Sstevel@tonic-gate 			}
4040*0Sstevel@tonic-gate 		}
4041*0Sstevel@tonic-gate 		if (!(v->gv_flags & GV_ENABLED))
4042*0Sstevel@tonic-gate 			vertex_send_event(v, RESTARTER_EVENT_TYPE_DISABLE);
4043*0Sstevel@tonic-gate 		break;
4044*0Sstevel@tonic-gate 
4045*0Sstevel@tonic-gate 	case RESTARTER_STATE_MAINT:
4046*0Sstevel@tonic-gate 		/* No action. */
4047*0Sstevel@tonic-gate 		break;
4048*0Sstevel@tonic-gate 
4049*0Sstevel@tonic-gate 	default:
4050*0Sstevel@tonic-gate 		/* Should have been caught above. */
4051*0Sstevel@tonic-gate #ifndef NDEBUG
4052*0Sstevel@tonic-gate 		uu_warn("%s:%d: Uncaught case %d.\n", __FILE__, __LINE__,
4053*0Sstevel@tonic-gate 		    state);
4054*0Sstevel@tonic-gate #endif
4055*0Sstevel@tonic-gate 		abort();
4056*0Sstevel@tonic-gate 	}
4057*0Sstevel@tonic-gate 
4058*0Sstevel@tonic-gate 	/*
4059*0Sstevel@tonic-gate 	 * If the service came up or went down, propagate the event.  We must
4060*0Sstevel@tonic-gate 	 * treat offline -> disabled as a start since it can satisfy
4061*0Sstevel@tonic-gate 	 * optional_all dependencies.  And we must treat !running -> maintenance
4062*0Sstevel@tonic-gate 	 * as a start because maintenance satisfies optional and exclusion
4063*0Sstevel@tonic-gate 	 * dependencies.
4064*0Sstevel@tonic-gate 	 */
4065*0Sstevel@tonic-gate 	if (inst_running(v)) {
4066*0Sstevel@tonic-gate 		if (!was_running) {
4067*0Sstevel@tonic-gate 			log_framework(LOG_DEBUG, "Propagating start of %s.\n",
4068*0Sstevel@tonic-gate 			    v->gv_name);
4069*0Sstevel@tonic-gate 
4070*0Sstevel@tonic-gate 			graph_walk_dependents(v, propagate_start, NULL);
4071*0Sstevel@tonic-gate 		} else if (serr == RERR_REFRESH) {
4072*0Sstevel@tonic-gate 			/* For refresh we'll get a message sans state change */
4073*0Sstevel@tonic-gate 
4074*0Sstevel@tonic-gate 			log_framework(LOG_DEBUG, "Propagating refresh of %s.\n",
4075*0Sstevel@tonic-gate 			    v->gv_name);
4076*0Sstevel@tonic-gate 
4077*0Sstevel@tonic-gate 			graph_walk_dependents(v, propagate_stop, (void *)serr);
4078*0Sstevel@tonic-gate 		}
4079*0Sstevel@tonic-gate 	} else if (was_running) {
4080*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Propagating stop of %s.\n",
4081*0Sstevel@tonic-gate 			    v->gv_name);
4082*0Sstevel@tonic-gate 
4083*0Sstevel@tonic-gate 		graph_walk_dependents(v, propagate_stop, (void *)serr);
4084*0Sstevel@tonic-gate 	} else if (v->gv_state == RESTARTER_STATE_DISABLED) {
4085*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Propagating disable of %s.\n",
4086*0Sstevel@tonic-gate 		    v->gv_name);
4087*0Sstevel@tonic-gate 
4088*0Sstevel@tonic-gate 		graph_walk_dependents(v, propagate_start, NULL);
4089*0Sstevel@tonic-gate 		propagate_satbility(v);
4090*0Sstevel@tonic-gate 	} else if (v->gv_state == RESTARTER_STATE_MAINT) {
4091*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Propagating maintenance of %s.\n",
4092*0Sstevel@tonic-gate 		    v->gv_name);
4093*0Sstevel@tonic-gate 
4094*0Sstevel@tonic-gate 		graph_walk_dependents(v, propagate_start, NULL);
4095*0Sstevel@tonic-gate 		propagate_satbility(v);
4096*0Sstevel@tonic-gate 	}
4097*0Sstevel@tonic-gate 
4098*0Sstevel@tonic-gate 	if (state != old_state && st->st_load_complete &&
4099*0Sstevel@tonic-gate 	    !go_single_user_mode && !go_to_level1 &&
4100*0Sstevel@tonic-gate 	    halting == -1) {
4101*0Sstevel@tonic-gate 		if (!can_come_up() && !sulogin_thread_running) {
4102*0Sstevel@tonic-gate 			(void) startd_thread_create(sulogin_thread, NULL);
4103*0Sstevel@tonic-gate 			sulogin_thread_running = B_TRUE;
4104*0Sstevel@tonic-gate 		}
4105*0Sstevel@tonic-gate 	}
4106*0Sstevel@tonic-gate 
4107*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
4108*0Sstevel@tonic-gate 
4109*0Sstevel@tonic-gate 	return (err);
4110*0Sstevel@tonic-gate }
4111*0Sstevel@tonic-gate 
4112*0Sstevel@tonic-gate 
4113*0Sstevel@tonic-gate static void
4114*0Sstevel@tonic-gate remove_inst_vertex(graph_vertex_t *v)
4115*0Sstevel@tonic-gate {
4116*0Sstevel@tonic-gate 	graph_edge_t *e;
4117*0Sstevel@tonic-gate 	graph_vertex_t *sv;
4118*0Sstevel@tonic-gate 	int i;
4119*0Sstevel@tonic-gate 
4120*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
4121*0Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependents) == 1);
4122*0Sstevel@tonic-gate 
4123*0Sstevel@tonic-gate 	e = uu_list_first(v->gv_dependents);
4124*0Sstevel@tonic-gate 	sv = e->ge_vertex;
4125*0Sstevel@tonic-gate 	graph_remove_edge(sv, v);
4126*0Sstevel@tonic-gate 
4127*0Sstevel@tonic-gate 	for (i = 0; up_svcs[i] != NULL; ++i) {
4128*0Sstevel@tonic-gate 		if (up_svcs_p[i] == v)
4129*0Sstevel@tonic-gate 			up_svcs_p[i] = NULL;
4130*0Sstevel@tonic-gate 	}
4131*0Sstevel@tonic-gate 
4132*0Sstevel@tonic-gate 	if (manifest_import_p == v)
4133*0Sstevel@tonic-gate 		manifest_import_p = NULL;
4134*0Sstevel@tonic-gate 
4135*0Sstevel@tonic-gate 	graph_remove_vertex(v);
4136*0Sstevel@tonic-gate 
4137*0Sstevel@tonic-gate 	if (uu_list_numnodes(sv->gv_dependencies) == 0 &&
4138*0Sstevel@tonic-gate 	    uu_list_numnodes(sv->gv_dependents) == 0)
4139*0Sstevel@tonic-gate 		graph_remove_vertex(sv);
4140*0Sstevel@tonic-gate }
4141*0Sstevel@tonic-gate 
4142*0Sstevel@tonic-gate /*
4143*0Sstevel@tonic-gate  * If a vertex for fmri exists and it is enabled, send _DISABLE to the
4144*0Sstevel@tonic-gate  * restarter.  If it is running, send _STOP.  Send _REMOVE_INSTANCE.  Delete
4145*0Sstevel@tonic-gate  * all property group dependencies, and the dependency on the restarter,
4146*0Sstevel@tonic-gate  * disposing of vertices as appropriate.  If other vertices depend on this
4147*0Sstevel@tonic-gate  * one, mark it unconfigured and return.  Otherwise remove the vertex.  Always
4148*0Sstevel@tonic-gate  * returns 0.
4149*0Sstevel@tonic-gate  */
4150*0Sstevel@tonic-gate static int
4151*0Sstevel@tonic-gate dgraph_remove_instance(const char *fmri, scf_handle_t *h)
4152*0Sstevel@tonic-gate {
4153*0Sstevel@tonic-gate 	graph_vertex_t *v;
4154*0Sstevel@tonic-gate 	graph_edge_t *e;
4155*0Sstevel@tonic-gate 	uu_list_t *old_deps;
4156*0Sstevel@tonic-gate 	int err;
4157*0Sstevel@tonic-gate 
4158*0Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph engine: Removing %s.\n", fmri);
4159*0Sstevel@tonic-gate 
4160*0Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
4161*0Sstevel@tonic-gate 
4162*0Sstevel@tonic-gate 	v = vertex_get_by_name(fmri);
4163*0Sstevel@tonic-gate 	if (v == NULL) {
4164*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
4165*0Sstevel@tonic-gate 		return (0);
4166*0Sstevel@tonic-gate 	}
4167*0Sstevel@tonic-gate 
4168*0Sstevel@tonic-gate 	/* Send restarter delete event. */
4169*0Sstevel@tonic-gate 	if (v->gv_flags & GV_CONFIGURED)
4170*0Sstevel@tonic-gate 		graph_unset_restarter(v);
4171*0Sstevel@tonic-gate 
4172*0Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
4173*0Sstevel@tonic-gate 		/*
4174*0Sstevel@tonic-gate 		 * Make a list of v's current dependencies so we can
4175*0Sstevel@tonic-gate 		 * reevaluate their GV_INSUBGRAPH flags after the dependencies
4176*0Sstevel@tonic-gate 		 * are removed.
4177*0Sstevel@tonic-gate 		 */
4178*0Sstevel@tonic-gate 		old_deps = startd_list_create(graph_edge_pool, NULL, 0);
4179*0Sstevel@tonic-gate 
4180*0Sstevel@tonic-gate 		err = uu_list_walk(v->gv_dependencies,
4181*0Sstevel@tonic-gate 		    (uu_walk_fn_t *)append_insts, old_deps, 0);
4182*0Sstevel@tonic-gate 		assert(err == 0);
4183*0Sstevel@tonic-gate 	}
4184*0Sstevel@tonic-gate 
4185*0Sstevel@tonic-gate 	delete_instance_dependencies(v, B_TRUE);
4186*0Sstevel@tonic-gate 
4187*0Sstevel@tonic-gate 	/*
4188*0Sstevel@tonic-gate 	 * Deleting an instance can both satisfy and unsatisfy dependencies,
4189*0Sstevel@tonic-gate 	 * depending on their type.  First propagate the stop as a RERR_RESTART
4190*0Sstevel@tonic-gate 	 * event -- deletion isn't a fault, just a normal stop.  This gives
4191*0Sstevel@tonic-gate 	 * dependent services the chance to do a clean shutdown.  Then, mark
4192*0Sstevel@tonic-gate 	 * the service as unconfigured and propagate the start event for the
4193*0Sstevel@tonic-gate 	 * optional_all dependencies that might have become satisfied.
4194*0Sstevel@tonic-gate 	 */
4195*0Sstevel@tonic-gate 	graph_walk_dependents(v, propagate_stop, (void *)RERR_RESTART);
4196*0Sstevel@tonic-gate 
4197*0Sstevel@tonic-gate 	v->gv_flags &= ~GV_CONFIGURED;
4198*0Sstevel@tonic-gate 
4199*0Sstevel@tonic-gate 	graph_walk_dependents(v, propagate_start, NULL);
4200*0Sstevel@tonic-gate 	propagate_satbility(v);
4201*0Sstevel@tonic-gate 
4202*0Sstevel@tonic-gate 	/*
4203*0Sstevel@tonic-gate 	 * If there are no (non-service) dependents, the vertex can be
4204*0Sstevel@tonic-gate 	 * completely removed.
4205*0Sstevel@tonic-gate 	 */
4206*0Sstevel@tonic-gate 	if (v != milestone && uu_list_numnodes(v->gv_dependents) == 1)
4207*0Sstevel@tonic-gate 		remove_inst_vertex(v);
4208*0Sstevel@tonic-gate 
4209*0Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
4210*0Sstevel@tonic-gate 		void *cookie = NULL;
4211*0Sstevel@tonic-gate 
4212*0Sstevel@tonic-gate 		while ((e = uu_list_teardown(old_deps, &cookie)) != NULL) {
4213*0Sstevel@tonic-gate 			while (eval_subgraph(e->ge_vertex, h) == ECONNABORTED)
4214*0Sstevel@tonic-gate 				libscf_handle_rebind(h);
4215*0Sstevel@tonic-gate 
4216*0Sstevel@tonic-gate 			startd_free(e, sizeof (*e));
4217*0Sstevel@tonic-gate 		}
4218*0Sstevel@tonic-gate 
4219*0Sstevel@tonic-gate 		uu_list_destroy(old_deps);
4220*0Sstevel@tonic-gate 	}
4221*0Sstevel@tonic-gate 
4222*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
4223*0Sstevel@tonic-gate 
4224*0Sstevel@tonic-gate 	return (0);
4225*0Sstevel@tonic-gate }
4226*0Sstevel@tonic-gate 
4227*0Sstevel@tonic-gate /*
4228*0Sstevel@tonic-gate  * Return the eventual (maybe current) milestone in the form of a
4229*0Sstevel@tonic-gate  * legacy runlevel.
4230*0Sstevel@tonic-gate  */
4231*0Sstevel@tonic-gate static char
4232*0Sstevel@tonic-gate target_milestone_as_runlevel()
4233*0Sstevel@tonic-gate {
4234*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
4235*0Sstevel@tonic-gate 
4236*0Sstevel@tonic-gate 	if (milestone == NULL)
4237*0Sstevel@tonic-gate 		return ('3');
4238*0Sstevel@tonic-gate 	else if (milestone == MILESTONE_NONE)
4239*0Sstevel@tonic-gate 		return ('0');
4240*0Sstevel@tonic-gate 
4241*0Sstevel@tonic-gate 	if (strcmp(milestone->gv_name, multi_user_fmri) == 0)
4242*0Sstevel@tonic-gate 		return ('2');
4243*0Sstevel@tonic-gate 	else if (strcmp(milestone->gv_name, single_user_fmri) == 0)
4244*0Sstevel@tonic-gate 		return ('S');
4245*0Sstevel@tonic-gate 	else if (strcmp(milestone->gv_name, multi_user_svr_fmri) == 0)
4246*0Sstevel@tonic-gate 		return ('3');
4247*0Sstevel@tonic-gate 
4248*0Sstevel@tonic-gate #ifndef NDEBUG
4249*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:%d: Unknown milestone name \"%s\".\n",
4250*0Sstevel@tonic-gate 	    __FILE__, __LINE__, milestone->gv_name);
4251*0Sstevel@tonic-gate #endif
4252*0Sstevel@tonic-gate 	abort();
4253*0Sstevel@tonic-gate 	/* NOTREACHED */
4254*0Sstevel@tonic-gate }
4255*0Sstevel@tonic-gate 
4256*0Sstevel@tonic-gate static struct {
4257*0Sstevel@tonic-gate 	char	rl;
4258*0Sstevel@tonic-gate 	int	sig;
4259*0Sstevel@tonic-gate } init_sigs[] = {
4260*0Sstevel@tonic-gate 	{ 'S', SIGBUS },
4261*0Sstevel@tonic-gate 	{ '0', SIGINT },
4262*0Sstevel@tonic-gate 	{ '1', SIGQUIT },
4263*0Sstevel@tonic-gate 	{ '2', SIGILL },
4264*0Sstevel@tonic-gate 	{ '3', SIGTRAP },
4265*0Sstevel@tonic-gate 	{ '4', SIGIOT },
4266*0Sstevel@tonic-gate 	{ '5', SIGEMT },
4267*0Sstevel@tonic-gate 	{ '6', SIGFPE },
4268*0Sstevel@tonic-gate 	{ 0, 0 }
4269*0Sstevel@tonic-gate };
4270*0Sstevel@tonic-gate 
4271*0Sstevel@tonic-gate static void
4272*0Sstevel@tonic-gate signal_init(char rl)
4273*0Sstevel@tonic-gate {
4274*0Sstevel@tonic-gate 	pid_t init_pid;
4275*0Sstevel@tonic-gate 	int i;
4276*0Sstevel@tonic-gate 
4277*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
4278*0Sstevel@tonic-gate 
4279*0Sstevel@tonic-gate 	if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
4280*0Sstevel@tonic-gate 	    sizeof (init_pid)) != sizeof (init_pid)) {
4281*0Sstevel@tonic-gate 		log_error(LOG_NOTICE, "Could not get pid to signal init.\n");
4282*0Sstevel@tonic-gate 		return;
4283*0Sstevel@tonic-gate 	}
4284*0Sstevel@tonic-gate 
4285*0Sstevel@tonic-gate 	for (i = 0; init_sigs[i].rl != 0; ++i)
4286*0Sstevel@tonic-gate 		if (init_sigs[i].rl == rl)
4287*0Sstevel@tonic-gate 			break;
4288*0Sstevel@tonic-gate 
4289*0Sstevel@tonic-gate 	if (init_sigs[i].rl != 0) {
4290*0Sstevel@tonic-gate 		if (kill(init_pid, init_sigs[i].sig) != 0) {
4291*0Sstevel@tonic-gate 			switch (errno) {
4292*0Sstevel@tonic-gate 			case EPERM:
4293*0Sstevel@tonic-gate 			case ESRCH:
4294*0Sstevel@tonic-gate 				log_error(LOG_NOTICE, "Could not signal init: "
4295*0Sstevel@tonic-gate 				    "%s.\n", strerror(errno));
4296*0Sstevel@tonic-gate 				break;
4297*0Sstevel@tonic-gate 
4298*0Sstevel@tonic-gate 			case EINVAL:
4299*0Sstevel@tonic-gate 			default:
4300*0Sstevel@tonic-gate 				bad_error("kill", errno);
4301*0Sstevel@tonic-gate 			}
4302*0Sstevel@tonic-gate 		}
4303*0Sstevel@tonic-gate 	}
4304*0Sstevel@tonic-gate }
4305*0Sstevel@tonic-gate 
4306*0Sstevel@tonic-gate /*
4307*0Sstevel@tonic-gate  * This is called when one of the major milestones changes state, or when
4308*0Sstevel@tonic-gate  * init is signalled and tells us it was told to change runlevel.  We wait
4309*0Sstevel@tonic-gate  * to reach the milestone because this allows /etc/inittab entries to retain
4310*0Sstevel@tonic-gate  * some boot ordering: historically, entries could place themselves before/after
4311*0Sstevel@tonic-gate  * the running of /sbin/rcX scripts but we can no longer make the
4312*0Sstevel@tonic-gate  * distinction because the /sbin/rcX scripts no longer exist as punctuation
4313*0Sstevel@tonic-gate  * marks in /etc/inittab.
4314*0Sstevel@tonic-gate  *
4315*0Sstevel@tonic-gate  * Also, we only trigger an update when we reach the eventual target
4316*0Sstevel@tonic-gate  * milestone: without this, an /etc/inittab entry marked only for
4317*0Sstevel@tonic-gate  * runlevel 2 would be executed for runlevel 3, which is not how
4318*0Sstevel@tonic-gate  * /etc/inittab entries work.
4319*0Sstevel@tonic-gate  *
4320*0Sstevel@tonic-gate  * If we're single user coming online, then we set utmpx to the target
4321*0Sstevel@tonic-gate  * runlevel so that legacy scripts can work as expected.
4322*0Sstevel@tonic-gate  */
4323*0Sstevel@tonic-gate static void
4324*0Sstevel@tonic-gate graph_runlevel_changed(char rl, int online)
4325*0Sstevel@tonic-gate {
4326*0Sstevel@tonic-gate 	char trl;
4327*0Sstevel@tonic-gate 
4328*0Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
4329*0Sstevel@tonic-gate 
4330*0Sstevel@tonic-gate 	trl = target_milestone_as_runlevel();
4331*0Sstevel@tonic-gate 
4332*0Sstevel@tonic-gate 	if (online) {
4333*0Sstevel@tonic-gate 		if (rl == trl) {
4334*0Sstevel@tonic-gate 			signal_init(trl);
4335*0Sstevel@tonic-gate 			current_runlevel = rl;
4336*0Sstevel@tonic-gate 		} else if (rl == 'S') {
4337*0Sstevel@tonic-gate 			/*
4338*0Sstevel@tonic-gate 			 * At boot, set the entry early for the benefit of the
4339*0Sstevel@tonic-gate 			 * legacy init scripts.
4340*0Sstevel@tonic-gate 			 */
4341*0Sstevel@tonic-gate 			utmpx_set_runlevel(trl, 'S', B_FALSE);
4342*0Sstevel@tonic-gate 		}
4343*0Sstevel@tonic-gate 	} else {
4344*0Sstevel@tonic-gate 		if (rl == '3' && trl == '2') {
4345*0Sstevel@tonic-gate 			signal_init(trl);
4346*0Sstevel@tonic-gate 			current_runlevel = rl;
4347*0Sstevel@tonic-gate 		} else if (rl == '2' && trl == 'S') {
4348*0Sstevel@tonic-gate 			signal_init(trl);
4349*0Sstevel@tonic-gate 			current_runlevel = rl;
4350*0Sstevel@tonic-gate 		}
4351*0Sstevel@tonic-gate 	}
4352*0Sstevel@tonic-gate }
4353*0Sstevel@tonic-gate 
4354*0Sstevel@tonic-gate /*
4355*0Sstevel@tonic-gate  * Move to a backwards-compatible runlevel by executing the appropriate
4356*0Sstevel@tonic-gate  * /etc/rc?.d/K* scripts and/or setting the milestone.
4357*0Sstevel@tonic-gate  *
4358*0Sstevel@tonic-gate  * Returns
4359*0Sstevel@tonic-gate  *   0 - success
4360*0Sstevel@tonic-gate  *   ECONNRESET - success, but handle was reset
4361*0Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
4362*0Sstevel@tonic-gate  *   ECANCELED - pg was deleted
4363*0Sstevel@tonic-gate  */
4364*0Sstevel@tonic-gate static int
4365*0Sstevel@tonic-gate dgraph_set_runlevel(scf_propertygroup_t *pg, scf_property_t *prop)
4366*0Sstevel@tonic-gate {
4367*0Sstevel@tonic-gate 	char rl;
4368*0Sstevel@tonic-gate 	scf_handle_t *h;
4369*0Sstevel@tonic-gate 	int r;
4370*0Sstevel@tonic-gate 	const char *ms = NULL;	/* what to commit as options/milestone */
4371*0Sstevel@tonic-gate 	boolean_t rebound = B_FALSE;
4372*0Sstevel@tonic-gate 	int mark_rl = 0;
4373*0Sstevel@tonic-gate 
4374*0Sstevel@tonic-gate 	const char * const stop = "stop";
4375*0Sstevel@tonic-gate 
4376*0Sstevel@tonic-gate 	r = libscf_extract_runlevel(prop, &rl);
4377*0Sstevel@tonic-gate 	switch (r) {
4378*0Sstevel@tonic-gate 	case 0:
4379*0Sstevel@tonic-gate 		break;
4380*0Sstevel@tonic-gate 
4381*0Sstevel@tonic-gate 	case ECONNABORTED:
4382*0Sstevel@tonic-gate 	case ECANCELED:
4383*0Sstevel@tonic-gate 		return (r);
4384*0Sstevel@tonic-gate 
4385*0Sstevel@tonic-gate 	case EINVAL:
4386*0Sstevel@tonic-gate 	case ENOENT:
4387*0Sstevel@tonic-gate 		log_error(LOG_WARNING, "runlevel property is misconfigured; "
4388*0Sstevel@tonic-gate 		    "ignoring.\n");
4389*0Sstevel@tonic-gate 		/* delete the bad property */
4390*0Sstevel@tonic-gate 		goto nolock_out;
4391*0Sstevel@tonic-gate 
4392*0Sstevel@tonic-gate 	default:
4393*0Sstevel@tonic-gate 		bad_error("libscf_extract_runlevel", r);
4394*0Sstevel@tonic-gate 	}
4395*0Sstevel@tonic-gate 
4396*0Sstevel@tonic-gate 	switch (rl) {
4397*0Sstevel@tonic-gate 	case 's':
4398*0Sstevel@tonic-gate 		rl = 'S';
4399*0Sstevel@tonic-gate 		/* FALLTHROUGH */
4400*0Sstevel@tonic-gate 
4401*0Sstevel@tonic-gate 	case 'S':
4402*0Sstevel@tonic-gate 	case '2':
4403*0Sstevel@tonic-gate 	case '3':
4404*0Sstevel@tonic-gate 		/*
4405*0Sstevel@tonic-gate 		 * These cases cause a milestone change, so
4406*0Sstevel@tonic-gate 		 * graph_runlevel_changed() will eventually deal with
4407*0Sstevel@tonic-gate 		 * signalling init.
4408*0Sstevel@tonic-gate 		 */
4409*0Sstevel@tonic-gate 		break;
4410*0Sstevel@tonic-gate 
4411*0Sstevel@tonic-gate 	case '0':
4412*0Sstevel@tonic-gate 	case '1':
4413*0Sstevel@tonic-gate 	case '4':
4414*0Sstevel@tonic-gate 	case '5':
4415*0Sstevel@tonic-gate 	case '6':
4416*0Sstevel@tonic-gate 		mark_rl = 1;
4417*0Sstevel@tonic-gate 		break;
4418*0Sstevel@tonic-gate 
4419*0Sstevel@tonic-gate 	default:
4420*0Sstevel@tonic-gate 		log_framework(LOG_NOTICE, "Unknown runlevel '%c'.\n", rl);
4421*0Sstevel@tonic-gate 		ms = NULL;
4422*0Sstevel@tonic-gate 		goto nolock_out;
4423*0Sstevel@tonic-gate 	}
4424*0Sstevel@tonic-gate 
4425*0Sstevel@tonic-gate 	h = scf_pg_handle(pg);
4426*0Sstevel@tonic-gate 
4427*0Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
4428*0Sstevel@tonic-gate 
4429*0Sstevel@tonic-gate 	/*
4430*0Sstevel@tonic-gate 	 * Since this triggers no milestone changes, force it by hand.
4431*0Sstevel@tonic-gate 	 */
4432*0Sstevel@tonic-gate 	if (current_runlevel == '4' && rl == '3')
4433*0Sstevel@tonic-gate 		mark_rl = 1;
4434*0Sstevel@tonic-gate 
4435*0Sstevel@tonic-gate 	if (rl == current_runlevel) {
4436*0Sstevel@tonic-gate 		ms = NULL;
4437*0Sstevel@tonic-gate 		goto out;
4438*0Sstevel@tonic-gate 	}
4439*0Sstevel@tonic-gate 
4440*0Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Changing to runlevel '%c'.\n", rl);
4441*0Sstevel@tonic-gate 
4442*0Sstevel@tonic-gate 	/*
4443*0Sstevel@tonic-gate 	 * Make sure stop rc scripts see the new settings via who -r.
4444*0Sstevel@tonic-gate 	 */
4445*0Sstevel@tonic-gate 	utmpx_set_runlevel(rl, current_runlevel, B_TRUE);
4446*0Sstevel@tonic-gate 
4447*0Sstevel@tonic-gate 	/*
4448*0Sstevel@tonic-gate 	 * Some run levels don't have a direct correspondence to any
4449*0Sstevel@tonic-gate 	 * milestones, so we have to signal init directly.
4450*0Sstevel@tonic-gate 	 */
4451*0Sstevel@tonic-gate 	if (mark_rl) {
4452*0Sstevel@tonic-gate 		current_runlevel = rl;
4453*0Sstevel@tonic-gate 		signal_init(rl);
4454*0Sstevel@tonic-gate 	}
4455*0Sstevel@tonic-gate 
4456*0Sstevel@tonic-gate 	switch (rl) {
4457*0Sstevel@tonic-gate 	case 'S':
4458*0Sstevel@tonic-gate 		uu_warn("The system is coming down for administration.  "
4459*0Sstevel@tonic-gate 		    "Please wait.\n");
4460*0Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_FALSE);
4461*0Sstevel@tonic-gate 		ms = single_user_fmri;
4462*0Sstevel@tonic-gate 		go_single_user_mode = B_TRUE;
4463*0Sstevel@tonic-gate 		break;
4464*0Sstevel@tonic-gate 
4465*0Sstevel@tonic-gate 	case '0':
4466*0Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
4467*0Sstevel@tonic-gate 		halting = AD_HALT;
4468*0Sstevel@tonic-gate 		goto uadmin;
4469*0Sstevel@tonic-gate 
4470*0Sstevel@tonic-gate 	case '5':
4471*0Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
4472*0Sstevel@tonic-gate 		halting = AD_POWEROFF;
4473*0Sstevel@tonic-gate 		goto uadmin;
4474*0Sstevel@tonic-gate 
4475*0Sstevel@tonic-gate 	case '6':
4476*0Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
4477*0Sstevel@tonic-gate 		halting = AD_BOOT;
4478*0Sstevel@tonic-gate 		goto uadmin;
4479*0Sstevel@tonic-gate 
4480*0Sstevel@tonic-gate uadmin:
4481*0Sstevel@tonic-gate 		uu_warn("The system is coming down.  Please wait.\n");
4482*0Sstevel@tonic-gate 		ms = "none";
4483*0Sstevel@tonic-gate 
4484*0Sstevel@tonic-gate 		/*
4485*0Sstevel@tonic-gate 		 * We can't wait until all services are offline since this
4486*0Sstevel@tonic-gate 		 * thread is responsible for taking them offline.  Instead we
4487*0Sstevel@tonic-gate 		 * set halting to the second argument for uadmin() and call
4488*0Sstevel@tonic-gate 		 * do_uadmin() from dgraph_set_instance_state() when
4489*0Sstevel@tonic-gate 		 * appropriate.
4490*0Sstevel@tonic-gate 		 */
4491*0Sstevel@tonic-gate 		break;
4492*0Sstevel@tonic-gate 
4493*0Sstevel@tonic-gate 	case '1':
4494*0Sstevel@tonic-gate 		if (current_runlevel != 'S') {
4495*0Sstevel@tonic-gate 			uu_warn("Changing to state 1.\n");
4496*0Sstevel@tonic-gate 			fork_rc_script(rl, stop, B_FALSE);
4497*0Sstevel@tonic-gate 		} else {
4498*0Sstevel@tonic-gate 			uu_warn("The system is coming up for administration.  "
4499*0Sstevel@tonic-gate 			    "Please wait.\n");
4500*0Sstevel@tonic-gate 		}
4501*0Sstevel@tonic-gate 		ms = single_user_fmri;
4502*0Sstevel@tonic-gate 		go_to_level1 = B_TRUE;
4503*0Sstevel@tonic-gate 		break;
4504*0Sstevel@tonic-gate 
4505*0Sstevel@tonic-gate 	case '2':
4506*0Sstevel@tonic-gate 		if (current_runlevel == '3' || current_runlevel == '4')
4507*0Sstevel@tonic-gate 			fork_rc_script(rl, stop, B_FALSE);
4508*0Sstevel@tonic-gate 		ms = multi_user_fmri;
4509*0Sstevel@tonic-gate 		break;
4510*0Sstevel@tonic-gate 
4511*0Sstevel@tonic-gate 	case '3':
4512*0Sstevel@tonic-gate 	case '4':
4513*0Sstevel@tonic-gate 		ms = "all";
4514*0Sstevel@tonic-gate 		break;
4515*0Sstevel@tonic-gate 
4516*0Sstevel@tonic-gate 	default:
4517*0Sstevel@tonic-gate #ifndef NDEBUG
4518*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s:%d: Uncaught case %d ('%c').\n",
4519*0Sstevel@tonic-gate 		    __FILE__, __LINE__, rl, rl);
4520*0Sstevel@tonic-gate #endif
4521*0Sstevel@tonic-gate 		abort();
4522*0Sstevel@tonic-gate 	}
4523*0Sstevel@tonic-gate 
4524*0Sstevel@tonic-gate out:
4525*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
4526*0Sstevel@tonic-gate 
4527*0Sstevel@tonic-gate nolock_out:
4528*0Sstevel@tonic-gate 	switch (r = libscf_clear_runlevel(pg, ms)) {
4529*0Sstevel@tonic-gate 	case 0:
4530*0Sstevel@tonic-gate 		break;
4531*0Sstevel@tonic-gate 
4532*0Sstevel@tonic-gate 	case ECONNABORTED:
4533*0Sstevel@tonic-gate 		libscf_handle_rebind(h);
4534*0Sstevel@tonic-gate 		rebound = B_TRUE;
4535*0Sstevel@tonic-gate 		goto nolock_out;
4536*0Sstevel@tonic-gate 
4537*0Sstevel@tonic-gate 	case ECANCELED:
4538*0Sstevel@tonic-gate 		break;
4539*0Sstevel@tonic-gate 
4540*0Sstevel@tonic-gate 	case EPERM:
4541*0Sstevel@tonic-gate 	case EACCES:
4542*0Sstevel@tonic-gate 	case EROFS:
4543*0Sstevel@tonic-gate 		log_error(LOG_NOTICE, "Could not delete \"%s/%s\" property: "
4544*0Sstevel@tonic-gate 		    "%s.\n", SCF_PG_OPTIONS, "runlevel", strerror(r));
4545*0Sstevel@tonic-gate 		break;
4546*0Sstevel@tonic-gate 
4547*0Sstevel@tonic-gate 	default:
4548*0Sstevel@tonic-gate 		bad_error("libscf_clear_runlevel", r);
4549*0Sstevel@tonic-gate 	}
4550*0Sstevel@tonic-gate 
4551*0Sstevel@tonic-gate 	return (rebound ? ECONNRESET : 0);
4552*0Sstevel@tonic-gate }
4553*0Sstevel@tonic-gate 
4554*0Sstevel@tonic-gate static int
4555*0Sstevel@tonic-gate mark_subgraph(graph_edge_t *e, void *arg)
4556*0Sstevel@tonic-gate {
4557*0Sstevel@tonic-gate 	graph_vertex_t *v;
4558*0Sstevel@tonic-gate 	int r;
4559*0Sstevel@tonic-gate 	int optional = (int)arg;
4560*0Sstevel@tonic-gate 
4561*0Sstevel@tonic-gate 	v = e->ge_vertex;
4562*0Sstevel@tonic-gate 
4563*0Sstevel@tonic-gate 	/* If it's already in the subgraph, skip. */
4564*0Sstevel@tonic-gate 	if (v->gv_flags & GV_INSUBGRAPH)
4565*0Sstevel@tonic-gate 		return (UU_WALK_NEXT);
4566*0Sstevel@tonic-gate 
4567*0Sstevel@tonic-gate 	/*
4568*0Sstevel@tonic-gate 	 * Keep track if walk has entered an optional dependency group
4569*0Sstevel@tonic-gate 	 */
4570*0Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_OPTIONAL_ALL) {
4571*0Sstevel@tonic-gate 		optional = 1;
4572*0Sstevel@tonic-gate 	}
4573*0Sstevel@tonic-gate 	/*
4574*0Sstevel@tonic-gate 	 * Quit if we are in an optional dependency group and the instance
4575*0Sstevel@tonic-gate 	 * is disabled
4576*0Sstevel@tonic-gate 	 */
4577*0Sstevel@tonic-gate 	if (optional && (v->gv_type == GVT_INST) &&
4578*0Sstevel@tonic-gate 	    (!(v->gv_flags & GV_ENBLD_NOOVR)))
4579*0Sstevel@tonic-gate 		return (UU_WALK_NEXT);
4580*0Sstevel@tonic-gate 
4581*0Sstevel@tonic-gate 	v->gv_flags |= GV_INSUBGRAPH;
4582*0Sstevel@tonic-gate 
4583*0Sstevel@tonic-gate 	/* Skip all excluded dependencies. */
4584*0Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
4585*0Sstevel@tonic-gate 		return (UU_WALK_NEXT);
4586*0Sstevel@tonic-gate 
4587*0Sstevel@tonic-gate 	r = uu_list_walk(v->gv_dependencies, (uu_walk_fn_t *)mark_subgraph,
4588*0Sstevel@tonic-gate 	    (void *)optional, 0);
4589*0Sstevel@tonic-gate 	assert(r == 0);
4590*0Sstevel@tonic-gate 	return (UU_WALK_NEXT);
4591*0Sstevel@tonic-gate }
4592*0Sstevel@tonic-gate 
4593*0Sstevel@tonic-gate /*
4594*0Sstevel@tonic-gate  * "Restrict" the graph to dependencies of fmri.  We implement it by walking
4595*0Sstevel@tonic-gate  * all services, override-disabling those which are not descendents of the
4596*0Sstevel@tonic-gate  * instance, and removing any enable-override for the rest.  milestone is set
4597*0Sstevel@tonic-gate  * to the vertex which represents fmri so that the other graph operations may
4598*0Sstevel@tonic-gate  * act appropriately.
4599*0Sstevel@tonic-gate  *
4600*0Sstevel@tonic-gate  * If norepository is true, the function will not change the repository.
4601*0Sstevel@tonic-gate  *
4602*0Sstevel@tonic-gate  * Returns
4603*0Sstevel@tonic-gate  *   0 - success
4604*0Sstevel@tonic-gate  *   ECONNRESET - success, but handle was rebound
4605*0Sstevel@tonic-gate  *   EINVAL - fmri is invalid (error is logged)
4606*0Sstevel@tonic-gate  *   EALREADY - the milestone is already set to fmri
4607*0Sstevel@tonic-gate  *   ENOENT - a configured vertex does not exist for fmri (an error is logged)
4608*0Sstevel@tonic-gate  */
4609*0Sstevel@tonic-gate static int
4610*0Sstevel@tonic-gate dgraph_set_milestone(const char *fmri, scf_handle_t *h, boolean_t norepository)
4611*0Sstevel@tonic-gate {
4612*0Sstevel@tonic-gate 	const char *cfmri, *fs;
4613*0Sstevel@tonic-gate 	graph_vertex_t *nm, *v;
4614*0Sstevel@tonic-gate 	int ret = 0, r;
4615*0Sstevel@tonic-gate 	scf_instance_t *inst;
4616*0Sstevel@tonic-gate 	boolean_t isall, isnone, rebound = B_FALSE;
4617*0Sstevel@tonic-gate 
4618*0Sstevel@tonic-gate 	/* Validate fmri */
4619*0Sstevel@tonic-gate 	isall = (strcmp(fmri, "all") == 0);
4620*0Sstevel@tonic-gate 	isnone = (strcmp(fmri, "none") == 0);
4621*0Sstevel@tonic-gate 
4622*0Sstevel@tonic-gate 	if (!isall && !isnone) {
4623*0Sstevel@tonic-gate 		if (fmri_canonify(fmri, (char **)&cfmri, B_FALSE) == EINVAL)
4624*0Sstevel@tonic-gate 			goto reject;
4625*0Sstevel@tonic-gate 
4626*0Sstevel@tonic-gate 		if (strcmp(cfmri, single_user_fmri) != 0 &&
4627*0Sstevel@tonic-gate 		    strcmp(cfmri, multi_user_fmri) != 0 &&
4628*0Sstevel@tonic-gate 		    strcmp(cfmri, multi_user_svr_fmri) != 0) {
4629*0Sstevel@tonic-gate 			startd_free((void *)cfmri, max_scf_fmri_size);
4630*0Sstevel@tonic-gate reject:
4631*0Sstevel@tonic-gate 			log_framework(LOG_WARNING,
4632*0Sstevel@tonic-gate 			    "Rejecting request for invalid milestone \"%s\".\n",
4633*0Sstevel@tonic-gate 			    fmri);
4634*0Sstevel@tonic-gate 			return (EINVAL);
4635*0Sstevel@tonic-gate 		}
4636*0Sstevel@tonic-gate 	}
4637*0Sstevel@tonic-gate 
4638*0Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
4639*0Sstevel@tonic-gate 
4640*0Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
4641*0Sstevel@tonic-gate 
4642*0Sstevel@tonic-gate 	if (milestone == NULL) {
4643*0Sstevel@tonic-gate 		if (isall) {
4644*0Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
4645*0Sstevel@tonic-gate 			    "Milestone already set to all.\n");
4646*0Sstevel@tonic-gate 			ret = EALREADY;
4647*0Sstevel@tonic-gate 			goto out;
4648*0Sstevel@tonic-gate 		}
4649*0Sstevel@tonic-gate 	} else if (milestone == MILESTONE_NONE) {
4650*0Sstevel@tonic-gate 		if (isnone) {
4651*0Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
4652*0Sstevel@tonic-gate 			    "Milestone already set to none.\n");
4653*0Sstevel@tonic-gate 			ret = EALREADY;
4654*0Sstevel@tonic-gate 			goto out;
4655*0Sstevel@tonic-gate 		}
4656*0Sstevel@tonic-gate 	} else {
4657*0Sstevel@tonic-gate 		if (!isall && !isnone &&
4658*0Sstevel@tonic-gate 		    strcmp(cfmri, milestone->gv_name) == 0) {
4659*0Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
4660*0Sstevel@tonic-gate 			    "Milestone already set to %s.\n", cfmri);
4661*0Sstevel@tonic-gate 			ret = EALREADY;
4662*0Sstevel@tonic-gate 			goto out;
4663*0Sstevel@tonic-gate 		}
4664*0Sstevel@tonic-gate 	}
4665*0Sstevel@tonic-gate 
4666*0Sstevel@tonic-gate 	if (!isall && !isnone) {
4667*0Sstevel@tonic-gate 		nm = vertex_get_by_name(cfmri);
4668*0Sstevel@tonic-gate 		if (nm == NULL || !(nm->gv_flags & GV_CONFIGURED)) {
4669*0Sstevel@tonic-gate 			log_framework(LOG_WARNING, "Cannot set milestone to %s "
4670*0Sstevel@tonic-gate 			    "because no such service exists.\n", cfmri);
4671*0Sstevel@tonic-gate 			ret = ENOENT;
4672*0Sstevel@tonic-gate 			goto out;
4673*0Sstevel@tonic-gate 		}
4674*0Sstevel@tonic-gate 	}
4675*0Sstevel@tonic-gate 
4676*0Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Changing milestone to %s.\n", fmri);
4677*0Sstevel@tonic-gate 
4678*0Sstevel@tonic-gate 	/*
4679*0Sstevel@tonic-gate 	 * Set milestone, removing the old one if this was the last reference.
4680*0Sstevel@tonic-gate 	 */
4681*0Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE &&
4682*0Sstevel@tonic-gate 	    (milestone->gv_flags & GV_CONFIGURED) == 0)
4683*0Sstevel@tonic-gate 		remove_inst_vertex(milestone);
4684*0Sstevel@tonic-gate 
4685*0Sstevel@tonic-gate 	if (isall)
4686*0Sstevel@tonic-gate 		milestone = NULL;
4687*0Sstevel@tonic-gate 	else if (isnone)
4688*0Sstevel@tonic-gate 		milestone = MILESTONE_NONE;
4689*0Sstevel@tonic-gate 	else
4690*0Sstevel@tonic-gate 		milestone = nm;
4691*0Sstevel@tonic-gate 
4692*0Sstevel@tonic-gate 	/* Clear all GV_INSUBGRAPH bits. */
4693*0Sstevel@tonic-gate 	for (v = uu_list_first(dgraph); v != NULL; v = uu_list_next(dgraph, v))
4694*0Sstevel@tonic-gate 		v->gv_flags &= ~GV_INSUBGRAPH;
4695*0Sstevel@tonic-gate 
4696*0Sstevel@tonic-gate 	if (!isall && !isnone) {
4697*0Sstevel@tonic-gate 		/* Set GV_INSUBGRAPH for milestone & descendents. */
4698*0Sstevel@tonic-gate 		milestone->gv_flags |= GV_INSUBGRAPH;
4699*0Sstevel@tonic-gate 
4700*0Sstevel@tonic-gate 		r = uu_list_walk(milestone->gv_dependencies,
4701*0Sstevel@tonic-gate 		    (uu_walk_fn_t *)mark_subgraph, NULL, 0);
4702*0Sstevel@tonic-gate 		assert(r == 0);
4703*0Sstevel@tonic-gate 	}
4704*0Sstevel@tonic-gate 
4705*0Sstevel@tonic-gate 	/* Un-override services in the subgraph & override-disable the rest. */
4706*0Sstevel@tonic-gate 	if (norepository)
4707*0Sstevel@tonic-gate 		goto out;
4708*0Sstevel@tonic-gate 
4709*0Sstevel@tonic-gate 	non_subgraph_svcs = 0;
4710*0Sstevel@tonic-gate 	for (v = uu_list_first(dgraph);
4711*0Sstevel@tonic-gate 	    v != NULL;
4712*0Sstevel@tonic-gate 	    v = uu_list_next(dgraph, v)) {
4713*0Sstevel@tonic-gate 		if (v->gv_type != GVT_INST ||
4714*0Sstevel@tonic-gate 		    (v->gv_flags & GV_CONFIGURED) == 0)
4715*0Sstevel@tonic-gate 			continue;
4716*0Sstevel@tonic-gate 
4717*0Sstevel@tonic-gate again:
4718*0Sstevel@tonic-gate 		r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst,
4719*0Sstevel@tonic-gate 		    NULL, NULL, SCF_DECODE_FMRI_EXACT);
4720*0Sstevel@tonic-gate 		if (r != 0) {
4721*0Sstevel@tonic-gate 			switch (scf_error()) {
4722*0Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
4723*0Sstevel@tonic-gate 			default:
4724*0Sstevel@tonic-gate 				libscf_handle_rebind(h);
4725*0Sstevel@tonic-gate 				rebound = B_TRUE;
4726*0Sstevel@tonic-gate 				goto again;
4727*0Sstevel@tonic-gate 
4728*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
4729*0Sstevel@tonic-gate 				continue;
4730*0Sstevel@tonic-gate 
4731*0Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
4732*0Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
4733*0Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
4734*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
4735*0Sstevel@tonic-gate 				bad_error("scf_handle_decode_fmri",
4736*0Sstevel@tonic-gate 				    scf_error());
4737*0Sstevel@tonic-gate 			}
4738*0Sstevel@tonic-gate 		}
4739*0Sstevel@tonic-gate 
4740*0Sstevel@tonic-gate 		if (isall || (v->gv_flags & GV_INSUBGRAPH)) {
4741*0Sstevel@tonic-gate 			r = libscf_delete_enable_ovr(inst);
4742*0Sstevel@tonic-gate 			fs = "libscf_delete_enable_ovr";
4743*0Sstevel@tonic-gate 		} else {
4744*0Sstevel@tonic-gate 			assert(isnone || (v->gv_flags & GV_INSUBGRAPH) == 0);
4745*0Sstevel@tonic-gate 
4746*0Sstevel@tonic-gate 			if (inst_running(v))
4747*0Sstevel@tonic-gate 				++non_subgraph_svcs;
4748*0Sstevel@tonic-gate 
4749*0Sstevel@tonic-gate 			if (has_running_nonsubgraph_dependents(v))
4750*0Sstevel@tonic-gate 				continue;
4751*0Sstevel@tonic-gate 
4752*0Sstevel@tonic-gate 			r = libscf_set_enable_ovr(inst, 0);
4753*0Sstevel@tonic-gate 			fs = "libscf_set_enable_ovr";
4754*0Sstevel@tonic-gate 		}
4755*0Sstevel@tonic-gate 		switch (r) {
4756*0Sstevel@tonic-gate 		case 0:
4757*0Sstevel@tonic-gate 		case ECANCELED:
4758*0Sstevel@tonic-gate 			break;
4759*0Sstevel@tonic-gate 
4760*0Sstevel@tonic-gate 		case ECONNABORTED:
4761*0Sstevel@tonic-gate 			libscf_handle_rebind(h);
4762*0Sstevel@tonic-gate 			rebound = B_TRUE;
4763*0Sstevel@tonic-gate 			goto again;
4764*0Sstevel@tonic-gate 
4765*0Sstevel@tonic-gate 		case EPERM:
4766*0Sstevel@tonic-gate 		case EROFS:
4767*0Sstevel@tonic-gate 			log_error(LOG_WARNING,
4768*0Sstevel@tonic-gate 			    "Could not set %s/%s for %s: %s.\n",
4769*0Sstevel@tonic-gate 			    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
4770*0Sstevel@tonic-gate 			    v->gv_name, strerror(r));
4771*0Sstevel@tonic-gate 			break;
4772*0Sstevel@tonic-gate 
4773*0Sstevel@tonic-gate 		default:
4774*0Sstevel@tonic-gate 			bad_error(fs, r);
4775*0Sstevel@tonic-gate 		}
4776*0Sstevel@tonic-gate 	}
4777*0Sstevel@tonic-gate 
4778*0Sstevel@tonic-gate 	if (halting != -1) {
4779*0Sstevel@tonic-gate 		if (non_subgraph_svcs > 1)
4780*0Sstevel@tonic-gate 			uu_warn("%d system services are now being stopped.\n",
4781*0Sstevel@tonic-gate 			    non_subgraph_svcs);
4782*0Sstevel@tonic-gate 		else if (non_subgraph_svcs == 1)
4783*0Sstevel@tonic-gate 			uu_warn("One system service is now being stopped.\n");
4784*0Sstevel@tonic-gate 		else if (non_subgraph_svcs == 0)
4785*0Sstevel@tonic-gate 			do_uadmin();
4786*0Sstevel@tonic-gate 	}
4787*0Sstevel@tonic-gate 
4788*0Sstevel@tonic-gate 	ret = rebound ? ECONNRESET : 0;
4789*0Sstevel@tonic-gate 
4790*0Sstevel@tonic-gate out:
4791*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
4792*0Sstevel@tonic-gate 	if (!isall && !isnone)
4793*0Sstevel@tonic-gate 		startd_free((void *)cfmri, max_scf_fmri_size);
4794*0Sstevel@tonic-gate 	scf_instance_destroy(inst);
4795*0Sstevel@tonic-gate 	return (ret);
4796*0Sstevel@tonic-gate }
4797*0Sstevel@tonic-gate 
4798*0Sstevel@tonic-gate 
4799*0Sstevel@tonic-gate /*
4800*0Sstevel@tonic-gate  * Returns 0, ECONNABORTED, or EINVAL.
4801*0Sstevel@tonic-gate  */
4802*0Sstevel@tonic-gate static int
4803*0Sstevel@tonic-gate handle_graph_update_event(scf_handle_t *h, graph_protocol_event_t *e)
4804*0Sstevel@tonic-gate {
4805*0Sstevel@tonic-gate 	int r;
4806*0Sstevel@tonic-gate 
4807*0Sstevel@tonic-gate 	switch (e->gpe_type) {
4808*0Sstevel@tonic-gate 	case GRAPH_UPDATE_RELOAD_GRAPH:
4809*0Sstevel@tonic-gate 		log_error(LOG_WARNING,
4810*0Sstevel@tonic-gate 		    "graph_event: reload graph unimplemented\n");
4811*0Sstevel@tonic-gate 		break;
4812*0Sstevel@tonic-gate 
4813*0Sstevel@tonic-gate 	case GRAPH_UPDATE_STATE_CHANGE: {
4814*0Sstevel@tonic-gate 		protocol_states_t *states = e->gpe_data;
4815*0Sstevel@tonic-gate 
4816*0Sstevel@tonic-gate 		switch (r = dgraph_set_instance_state(h, e->gpe_inst,
4817*0Sstevel@tonic-gate 		    states->ps_state, states->ps_err)) {
4818*0Sstevel@tonic-gate 		case 0:
4819*0Sstevel@tonic-gate 		case ENOENT:
4820*0Sstevel@tonic-gate 			break;
4821*0Sstevel@tonic-gate 
4822*0Sstevel@tonic-gate 		case ECONNABORTED:
4823*0Sstevel@tonic-gate 			return (ECONNABORTED);
4824*0Sstevel@tonic-gate 
4825*0Sstevel@tonic-gate 		case EINVAL:
4826*0Sstevel@tonic-gate 		default:
4827*0Sstevel@tonic-gate #ifndef NDEBUG
4828*0Sstevel@tonic-gate 			(void) fprintf(stderr, "dgraph_set_instance_state() "
4829*0Sstevel@tonic-gate 			    "failed with unexpected error %d at %s:%d.\n", r,
4830*0Sstevel@tonic-gate 			    __FILE__, __LINE__);
4831*0Sstevel@tonic-gate #endif
4832*0Sstevel@tonic-gate 			abort();
4833*0Sstevel@tonic-gate 		}
4834*0Sstevel@tonic-gate 
4835*0Sstevel@tonic-gate 		startd_free(states, sizeof (protocol_states_t));
4836*0Sstevel@tonic-gate 		break;
4837*0Sstevel@tonic-gate 	}
4838*0Sstevel@tonic-gate 
4839*0Sstevel@tonic-gate 	default:
4840*0Sstevel@tonic-gate 		log_error(LOG_WARNING,
4841*0Sstevel@tonic-gate 		    "graph_event_loop received an unknown event: %d\n",
4842*0Sstevel@tonic-gate 		    e->gpe_type);
4843*0Sstevel@tonic-gate 		break;
4844*0Sstevel@tonic-gate 	}
4845*0Sstevel@tonic-gate 
4846*0Sstevel@tonic-gate 	return (0);
4847*0Sstevel@tonic-gate }
4848*0Sstevel@tonic-gate 
4849*0Sstevel@tonic-gate /*
4850*0Sstevel@tonic-gate  * graph_event_thread()
4851*0Sstevel@tonic-gate  *    Wait for state changes from the restarters.
4852*0Sstevel@tonic-gate  */
4853*0Sstevel@tonic-gate /*ARGSUSED*/
4854*0Sstevel@tonic-gate void *
4855*0Sstevel@tonic-gate graph_event_thread(void *unused)
4856*0Sstevel@tonic-gate {
4857*0Sstevel@tonic-gate 	scf_handle_t *h;
4858*0Sstevel@tonic-gate 	int err;
4859*0Sstevel@tonic-gate 
4860*0Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
4861*0Sstevel@tonic-gate 
4862*0Sstevel@tonic-gate 	/*CONSTCOND*/
4863*0Sstevel@tonic-gate 	while (1) {
4864*0Sstevel@tonic-gate 		graph_protocol_event_t *e;
4865*0Sstevel@tonic-gate 
4866*0Sstevel@tonic-gate 		MUTEX_LOCK(&gu->gu_lock);
4867*0Sstevel@tonic-gate 
4868*0Sstevel@tonic-gate 		while (gu->gu_wakeup == 0)
4869*0Sstevel@tonic-gate 			(void) pthread_cond_wait(&gu->gu_cv, &gu->gu_lock);
4870*0Sstevel@tonic-gate 
4871*0Sstevel@tonic-gate 		gu->gu_wakeup = 0;
4872*0Sstevel@tonic-gate 
4873*0Sstevel@tonic-gate 		while ((e = graph_event_dequeue()) != NULL) {
4874*0Sstevel@tonic-gate 			MUTEX_LOCK(&e->gpe_lock);
4875*0Sstevel@tonic-gate 			MUTEX_UNLOCK(&gu->gu_lock);
4876*0Sstevel@tonic-gate 
4877*0Sstevel@tonic-gate 			while ((err = handle_graph_update_event(h, e)) ==
4878*0Sstevel@tonic-gate 			    ECONNABORTED)
4879*0Sstevel@tonic-gate 				libscf_handle_rebind(h);
4880*0Sstevel@tonic-gate 
4881*0Sstevel@tonic-gate 			if (err == 0)
4882*0Sstevel@tonic-gate 				graph_event_release(e);
4883*0Sstevel@tonic-gate 			else
4884*0Sstevel@tonic-gate 				graph_event_requeue(e);
4885*0Sstevel@tonic-gate 
4886*0Sstevel@tonic-gate 			MUTEX_LOCK(&gu->gu_lock);
4887*0Sstevel@tonic-gate 		}
4888*0Sstevel@tonic-gate 
4889*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&gu->gu_lock);
4890*0Sstevel@tonic-gate 	}
4891*0Sstevel@tonic-gate 
4892*0Sstevel@tonic-gate 	/*
4893*0Sstevel@tonic-gate 	 * Unreachable for now -- there's currently no graceful cleanup
4894*0Sstevel@tonic-gate 	 * called on exit().
4895*0Sstevel@tonic-gate 	 */
4896*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&gu->gu_lock);
4897*0Sstevel@tonic-gate 	scf_handle_destroy(h);
4898*0Sstevel@tonic-gate 	return (NULL);
4899*0Sstevel@tonic-gate }
4900*0Sstevel@tonic-gate 
4901*0Sstevel@tonic-gate static void
4902*0Sstevel@tonic-gate set_initial_milestone(scf_handle_t *h)
4903*0Sstevel@tonic-gate {
4904*0Sstevel@tonic-gate 	scf_instance_t *inst;
4905*0Sstevel@tonic-gate 	char *fmri, *cfmri;
4906*0Sstevel@tonic-gate 	size_t sz;
4907*0Sstevel@tonic-gate 	int r;
4908*0Sstevel@tonic-gate 
4909*0Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
4910*0Sstevel@tonic-gate 	fmri = startd_alloc(max_scf_fmri_size);
4911*0Sstevel@tonic-gate 
4912*0Sstevel@tonic-gate 	/*
4913*0Sstevel@tonic-gate 	 * If -m milestone= was specified, we want to set options_ovr/milestone
4914*0Sstevel@tonic-gate 	 * to it.  Otherwise we want to read what the milestone should be set
4915*0Sstevel@tonic-gate 	 * to.  Either way we need our inst.
4916*0Sstevel@tonic-gate 	 */
4917*0Sstevel@tonic-gate get_self:
4918*0Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
4919*0Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
4920*0Sstevel@tonic-gate 		switch (scf_error()) {
4921*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
4922*0Sstevel@tonic-gate 			libscf_handle_rebind(h);
4923*0Sstevel@tonic-gate 			goto get_self;
4924*0Sstevel@tonic-gate 
4925*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
4926*0Sstevel@tonic-gate 			if (st->st_subgraph != NULL &&
4927*0Sstevel@tonic-gate 			    st->st_subgraph[0] != '\0') {
4928*0Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
4929*0Sstevel@tonic-gate 				    max_scf_fmri_size);
4930*0Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
4931*0Sstevel@tonic-gate 			} else {
4932*0Sstevel@tonic-gate 				fmri[0] = '\0';
4933*0Sstevel@tonic-gate 			}
4934*0Sstevel@tonic-gate 			break;
4935*0Sstevel@tonic-gate 
4936*0Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
4937*0Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
4938*0Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
4939*0Sstevel@tonic-gate 		default:
4940*0Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
4941*0Sstevel@tonic-gate 		}
4942*0Sstevel@tonic-gate 	} else {
4943*0Sstevel@tonic-gate 		if (st->st_subgraph != NULL && st->st_subgraph[0] != '\0') {
4944*0Sstevel@tonic-gate 			scf_propertygroup_t *pg;
4945*0Sstevel@tonic-gate 
4946*0Sstevel@tonic-gate 			pg = safe_scf_pg_create(h);
4947*0Sstevel@tonic-gate 
4948*0Sstevel@tonic-gate 			sz = strlcpy(fmri, st->st_subgraph, max_scf_fmri_size);
4949*0Sstevel@tonic-gate 			assert(sz < max_scf_fmri_size);
4950*0Sstevel@tonic-gate 
4951*0Sstevel@tonic-gate 			r = libscf_inst_get_or_add_pg(inst, SCF_PG_OPTIONS_OVR,
4952*0Sstevel@tonic-gate 			    SCF_PG_OPTIONS_OVR_TYPE, SCF_PG_OPTIONS_OVR_FLAGS,
4953*0Sstevel@tonic-gate 			    pg);
4954*0Sstevel@tonic-gate 			switch (r) {
4955*0Sstevel@tonic-gate 			case 0:
4956*0Sstevel@tonic-gate 				break;
4957*0Sstevel@tonic-gate 
4958*0Sstevel@tonic-gate 			case ECONNABORTED:
4959*0Sstevel@tonic-gate 				libscf_handle_rebind(h);
4960*0Sstevel@tonic-gate 				goto get_self;
4961*0Sstevel@tonic-gate 
4962*0Sstevel@tonic-gate 			case EPERM:
4963*0Sstevel@tonic-gate 			case EACCES:
4964*0Sstevel@tonic-gate 			case EROFS:
4965*0Sstevel@tonic-gate 				log_error(LOG_WARNING, "Could not set %s/%s: "
4966*0Sstevel@tonic-gate 				    "%s.\n", SCF_PG_OPTIONS_OVR,
4967*0Sstevel@tonic-gate 				    SCF_PROPERTY_MILESTONE, strerror(r));
4968*0Sstevel@tonic-gate 				/* FALLTHROUGH */
4969*0Sstevel@tonic-gate 
4970*0Sstevel@tonic-gate 			case ECANCELED:
4971*0Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
4972*0Sstevel@tonic-gate 				    max_scf_fmri_size);
4973*0Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
4974*0Sstevel@tonic-gate 				break;
4975*0Sstevel@tonic-gate 
4976*0Sstevel@tonic-gate 			default:
4977*0Sstevel@tonic-gate 				bad_error("libscf_inst_get_or_add_pg", r);
4978*0Sstevel@tonic-gate 			}
4979*0Sstevel@tonic-gate 
4980*0Sstevel@tonic-gate 			r = libscf_clear_runlevel(pg, fmri);
4981*0Sstevel@tonic-gate 			switch (r) {
4982*0Sstevel@tonic-gate 			case 0:
4983*0Sstevel@tonic-gate 				break;
4984*0Sstevel@tonic-gate 
4985*0Sstevel@tonic-gate 			case ECONNABORTED:
4986*0Sstevel@tonic-gate 				libscf_handle_rebind(h);
4987*0Sstevel@tonic-gate 				goto get_self;
4988*0Sstevel@tonic-gate 
4989*0Sstevel@tonic-gate 			case EPERM:
4990*0Sstevel@tonic-gate 			case EACCES:
4991*0Sstevel@tonic-gate 			case EROFS:
4992*0Sstevel@tonic-gate 				log_error(LOG_WARNING, "Could not set %s/%s: "
4993*0Sstevel@tonic-gate 				    "%s.\n", SCF_PG_OPTIONS_OVR,
4994*0Sstevel@tonic-gate 				    SCF_PROPERTY_MILESTONE, strerror(r));
4995*0Sstevel@tonic-gate 				/* FALLTHROUGH */
4996*0Sstevel@tonic-gate 
4997*0Sstevel@tonic-gate 			case ECANCELED:
4998*0Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
4999*0Sstevel@tonic-gate 				    max_scf_fmri_size);
5000*0Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
5001*0Sstevel@tonic-gate 				break;
5002*0Sstevel@tonic-gate 
5003*0Sstevel@tonic-gate 			default:
5004*0Sstevel@tonic-gate 				bad_error("libscf_clear_runlevel", r);
5005*0Sstevel@tonic-gate 			}
5006*0Sstevel@tonic-gate 
5007*0Sstevel@tonic-gate 			scf_pg_destroy(pg);
5008*0Sstevel@tonic-gate 		} else {
5009*0Sstevel@tonic-gate 			scf_property_t *prop;
5010*0Sstevel@tonic-gate 			scf_value_t *val;
5011*0Sstevel@tonic-gate 
5012*0Sstevel@tonic-gate 			prop = safe_scf_property_create(h);
5013*0Sstevel@tonic-gate 			val = safe_scf_value_create(h);
5014*0Sstevel@tonic-gate 
5015*0Sstevel@tonic-gate 			r = libscf_get_milestone(inst, prop, val, fmri,
5016*0Sstevel@tonic-gate 			    max_scf_fmri_size);
5017*0Sstevel@tonic-gate 			switch (r) {
5018*0Sstevel@tonic-gate 			case 0:
5019*0Sstevel@tonic-gate 				break;
5020*0Sstevel@tonic-gate 
5021*0Sstevel@tonic-gate 			case ECONNABORTED:
5022*0Sstevel@tonic-gate 				libscf_handle_rebind(h);
5023*0Sstevel@tonic-gate 				goto get_self;
5024*0Sstevel@tonic-gate 
5025*0Sstevel@tonic-gate 			case EINVAL:
5026*0Sstevel@tonic-gate 				log_error(LOG_WARNING, "Milestone property is "
5027*0Sstevel@tonic-gate 				    "misconfigured.  Defaulting to \"all\".\n");
5028*0Sstevel@tonic-gate 				/* FALLTHROUGH */
5029*0Sstevel@tonic-gate 
5030*0Sstevel@tonic-gate 			case ECANCELED:
5031*0Sstevel@tonic-gate 			case ENOENT:
5032*0Sstevel@tonic-gate 				fmri[0] = '\0';
5033*0Sstevel@tonic-gate 				break;
5034*0Sstevel@tonic-gate 
5035*0Sstevel@tonic-gate 			default:
5036*0Sstevel@tonic-gate 				bad_error("libscf_get_milestone", r);
5037*0Sstevel@tonic-gate 			}
5038*0Sstevel@tonic-gate 
5039*0Sstevel@tonic-gate 			scf_value_destroy(val);
5040*0Sstevel@tonic-gate 			scf_property_destroy(prop);
5041*0Sstevel@tonic-gate 		}
5042*0Sstevel@tonic-gate 	}
5043*0Sstevel@tonic-gate 
5044*0Sstevel@tonic-gate 	if (fmri[0] == '\0' || strcmp(fmri, "all") == 0)
5045*0Sstevel@tonic-gate 		goto out;
5046*0Sstevel@tonic-gate 
5047*0Sstevel@tonic-gate 	if (strcmp(fmri, "none") != 0) {
5048*0Sstevel@tonic-gate retry:
5049*0Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
5050*0Sstevel@tonic-gate 		    NULL, SCF_DECODE_FMRI_EXACT) != 0) {
5051*0Sstevel@tonic-gate 			switch (scf_error()) {
5052*0Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
5053*0Sstevel@tonic-gate 				log_error(LOG_WARNING,
5054*0Sstevel@tonic-gate 				    "Requested milestone \"%s\" is invalid.  "
5055*0Sstevel@tonic-gate 				    "Reverting to \"all\".\n", fmri);
5056*0Sstevel@tonic-gate 				goto out;
5057*0Sstevel@tonic-gate 
5058*0Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
5059*0Sstevel@tonic-gate 				log_error(LOG_WARNING, "Requested milestone "
5060*0Sstevel@tonic-gate 				    "\"%s\" does not specify an instance.  "
5061*0Sstevel@tonic-gate 				    "Reverting to \"all\".\n", fmri);
5062*0Sstevel@tonic-gate 				goto out;
5063*0Sstevel@tonic-gate 
5064*0Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
5065*0Sstevel@tonic-gate 				libscf_handle_rebind(h);
5066*0Sstevel@tonic-gate 				goto retry;
5067*0Sstevel@tonic-gate 
5068*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
5069*0Sstevel@tonic-gate 				log_error(LOG_WARNING, "Requested milestone "
5070*0Sstevel@tonic-gate 				    "\"%s\" not in repository.  Reverting to "
5071*0Sstevel@tonic-gate 				    "\"all\".\n", fmri);
5072*0Sstevel@tonic-gate 				goto out;
5073*0Sstevel@tonic-gate 
5074*0Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
5075*0Sstevel@tonic-gate 			default:
5076*0Sstevel@tonic-gate 				bad_error("scf_handle_decode_fmri",
5077*0Sstevel@tonic-gate 				    scf_error());
5078*0Sstevel@tonic-gate 			}
5079*0Sstevel@tonic-gate 		}
5080*0Sstevel@tonic-gate 
5081*0Sstevel@tonic-gate 		r = fmri_canonify(fmri, &cfmri, B_FALSE);
5082*0Sstevel@tonic-gate 		assert(r == 0);
5083*0Sstevel@tonic-gate 
5084*0Sstevel@tonic-gate 		r = dgraph_add_instance(cfmri, inst, B_TRUE);
5085*0Sstevel@tonic-gate 		startd_free(cfmri, max_scf_fmri_size);
5086*0Sstevel@tonic-gate 		switch (r) {
5087*0Sstevel@tonic-gate 		case 0:
5088*0Sstevel@tonic-gate 			break;
5089*0Sstevel@tonic-gate 
5090*0Sstevel@tonic-gate 		case ECONNABORTED:
5091*0Sstevel@tonic-gate 			goto retry;
5092*0Sstevel@tonic-gate 
5093*0Sstevel@tonic-gate 		case EINVAL:
5094*0Sstevel@tonic-gate 			log_error(LOG_WARNING,
5095*0Sstevel@tonic-gate 			    "Requested milestone \"%s\" is invalid.  "
5096*0Sstevel@tonic-gate 			    "Reverting to \"all\".\n", fmri);
5097*0Sstevel@tonic-gate 			goto out;
5098*0Sstevel@tonic-gate 
5099*0Sstevel@tonic-gate 		case ECANCELED:
5100*0Sstevel@tonic-gate 			log_error(LOG_WARNING,
5101*0Sstevel@tonic-gate 			    "Requested milestone \"%s\" not "
5102*0Sstevel@tonic-gate 			    "in repository.  Reverting to \"all\".\n",
5103*0Sstevel@tonic-gate 			    fmri);
5104*0Sstevel@tonic-gate 			goto out;
5105*0Sstevel@tonic-gate 
5106*0Sstevel@tonic-gate 		case EEXIST:
5107*0Sstevel@tonic-gate 		default:
5108*0Sstevel@tonic-gate 			bad_error("dgraph_add_instance", r);
5109*0Sstevel@tonic-gate 		}
5110*0Sstevel@tonic-gate 	}
5111*0Sstevel@tonic-gate 
5112*0Sstevel@tonic-gate 	log_console(LOG_INFO, "Booting to milestone \"%s\".\n", fmri);
5113*0Sstevel@tonic-gate 
5114*0Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_FALSE);
5115*0Sstevel@tonic-gate 	switch (r) {
5116*0Sstevel@tonic-gate 	case 0:
5117*0Sstevel@tonic-gate 	case ECONNRESET:
5118*0Sstevel@tonic-gate 	case EALREADY:
5119*0Sstevel@tonic-gate 		break;
5120*0Sstevel@tonic-gate 
5121*0Sstevel@tonic-gate 	case EINVAL:
5122*0Sstevel@tonic-gate 	case ENOENT:
5123*0Sstevel@tonic-gate 	default:
5124*0Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
5125*0Sstevel@tonic-gate 	}
5126*0Sstevel@tonic-gate 
5127*0Sstevel@tonic-gate out:
5128*0Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
5129*0Sstevel@tonic-gate 	scf_instance_destroy(inst);
5130*0Sstevel@tonic-gate }
5131*0Sstevel@tonic-gate 
5132*0Sstevel@tonic-gate void
5133*0Sstevel@tonic-gate set_restart_milestone(scf_handle_t *h)
5134*0Sstevel@tonic-gate {
5135*0Sstevel@tonic-gate 	scf_instance_t *inst;
5136*0Sstevel@tonic-gate 	scf_property_t *prop;
5137*0Sstevel@tonic-gate 	scf_value_t *val;
5138*0Sstevel@tonic-gate 	char *fmri;
5139*0Sstevel@tonic-gate 	int r;
5140*0Sstevel@tonic-gate 
5141*0Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
5142*0Sstevel@tonic-gate 
5143*0Sstevel@tonic-gate get_self:
5144*0Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL,
5145*0Sstevel@tonic-gate 	    inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
5146*0Sstevel@tonic-gate 		switch (scf_error()) {
5147*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
5148*0Sstevel@tonic-gate 			libscf_handle_rebind(h);
5149*0Sstevel@tonic-gate 			goto get_self;
5150*0Sstevel@tonic-gate 
5151*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
5152*0Sstevel@tonic-gate 			break;
5153*0Sstevel@tonic-gate 
5154*0Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
5155*0Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
5156*0Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
5157*0Sstevel@tonic-gate 		default:
5158*0Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
5159*0Sstevel@tonic-gate 		}
5160*0Sstevel@tonic-gate 
5161*0Sstevel@tonic-gate 		scf_instance_destroy(inst);
5162*0Sstevel@tonic-gate 		return;
5163*0Sstevel@tonic-gate 	}
5164*0Sstevel@tonic-gate 
5165*0Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
5166*0Sstevel@tonic-gate 	val = safe_scf_value_create(h);
5167*0Sstevel@tonic-gate 	fmri = startd_alloc(max_scf_fmri_size);
5168*0Sstevel@tonic-gate 
5169*0Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size);
5170*0Sstevel@tonic-gate 	switch (r) {
5171*0Sstevel@tonic-gate 	case 0:
5172*0Sstevel@tonic-gate 		break;
5173*0Sstevel@tonic-gate 
5174*0Sstevel@tonic-gate 	case ECONNABORTED:
5175*0Sstevel@tonic-gate 		libscf_handle_rebind(h);
5176*0Sstevel@tonic-gate 		goto get_self;
5177*0Sstevel@tonic-gate 
5178*0Sstevel@tonic-gate 	case ECANCELED:
5179*0Sstevel@tonic-gate 	case ENOENT:
5180*0Sstevel@tonic-gate 	case EINVAL:
5181*0Sstevel@tonic-gate 		goto out;
5182*0Sstevel@tonic-gate 
5183*0Sstevel@tonic-gate 	default:
5184*0Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
5185*0Sstevel@tonic-gate 	}
5186*0Sstevel@tonic-gate 
5187*0Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_TRUE);
5188*0Sstevel@tonic-gate 	switch (r) {
5189*0Sstevel@tonic-gate 	case 0:
5190*0Sstevel@tonic-gate 	case ECONNRESET:
5191*0Sstevel@tonic-gate 	case EALREADY:
5192*0Sstevel@tonic-gate 	case EINVAL:
5193*0Sstevel@tonic-gate 	case ENOENT:
5194*0Sstevel@tonic-gate 		break;
5195*0Sstevel@tonic-gate 
5196*0Sstevel@tonic-gate 	default:
5197*0Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
5198*0Sstevel@tonic-gate 	}
5199*0Sstevel@tonic-gate 
5200*0Sstevel@tonic-gate out:
5201*0Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
5202*0Sstevel@tonic-gate 	scf_value_destroy(val);
5203*0Sstevel@tonic-gate 	scf_property_destroy(prop);
5204*0Sstevel@tonic-gate 	scf_instance_destroy(inst);
5205*0Sstevel@tonic-gate }
5206*0Sstevel@tonic-gate 
5207*0Sstevel@tonic-gate /*
5208*0Sstevel@tonic-gate  * void *graph_thread(void *)
5209*0Sstevel@tonic-gate  *
5210*0Sstevel@tonic-gate  * Graph management thread.
5211*0Sstevel@tonic-gate  */
5212*0Sstevel@tonic-gate /*ARGSUSED*/
5213*0Sstevel@tonic-gate void *
5214*0Sstevel@tonic-gate graph_thread(void *arg)
5215*0Sstevel@tonic-gate {
5216*0Sstevel@tonic-gate 	scf_handle_t *h;
5217*0Sstevel@tonic-gate 	int err;
5218*0Sstevel@tonic-gate 
5219*0Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
5220*0Sstevel@tonic-gate 
5221*0Sstevel@tonic-gate 	if (st->st_initial)
5222*0Sstevel@tonic-gate 		set_initial_milestone(h);
5223*0Sstevel@tonic-gate 
5224*0Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
5225*0Sstevel@tonic-gate 	initial_milestone_set = B_TRUE;
5226*0Sstevel@tonic-gate 	err = pthread_cond_broadcast(&initial_milestone_cv);
5227*0Sstevel@tonic-gate 	assert(err == 0);
5228*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
5229*0Sstevel@tonic-gate 
5230*0Sstevel@tonic-gate 	libscf_populate_graph(h);
5231*0Sstevel@tonic-gate 
5232*0Sstevel@tonic-gate 	if (!st->st_initial)
5233*0Sstevel@tonic-gate 		set_restart_milestone(h);
5234*0Sstevel@tonic-gate 
5235*0Sstevel@tonic-gate 	MUTEX_LOCK(&st->st_load_lock);
5236*0Sstevel@tonic-gate 	st->st_load_complete = 1;
5237*0Sstevel@tonic-gate 	(void) pthread_cond_broadcast(&st->st_load_cv);
5238*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&st->st_load_lock);
5239*0Sstevel@tonic-gate 
5240*0Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
5241*0Sstevel@tonic-gate 	/*
5242*0Sstevel@tonic-gate 	 * Now that we've set st_load_complete we need to check can_come_up()
5243*0Sstevel@tonic-gate 	 * since if we booted to a milestone, then there won't be any more
5244*0Sstevel@tonic-gate 	 * state updates.
5245*0Sstevel@tonic-gate 	 */
5246*0Sstevel@tonic-gate 	if (!go_single_user_mode && !go_to_level1 &&
5247*0Sstevel@tonic-gate 	    halting == -1) {
5248*0Sstevel@tonic-gate 		if (!can_come_up() && !sulogin_thread_running) {
5249*0Sstevel@tonic-gate 			(void) startd_thread_create(sulogin_thread, NULL);
5250*0Sstevel@tonic-gate 			sulogin_thread_running = B_TRUE;
5251*0Sstevel@tonic-gate 		}
5252*0Sstevel@tonic-gate 	}
5253*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
5254*0Sstevel@tonic-gate 
5255*0Sstevel@tonic-gate 	(void) pthread_mutex_lock(&gu->gu_freeze_lock);
5256*0Sstevel@tonic-gate 
5257*0Sstevel@tonic-gate 	/*CONSTCOND*/
5258*0Sstevel@tonic-gate 	while (1) {
5259*0Sstevel@tonic-gate 		(void) pthread_cond_wait(&gu->gu_freeze_cv,
5260*0Sstevel@tonic-gate 		    &gu->gu_freeze_lock);
5261*0Sstevel@tonic-gate 	}
5262*0Sstevel@tonic-gate 
5263*0Sstevel@tonic-gate 	/*
5264*0Sstevel@tonic-gate 	 * Unreachable for now -- there's currently no graceful cleanup
5265*0Sstevel@tonic-gate 	 * called on exit().
5266*0Sstevel@tonic-gate 	 */
5267*0Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&gu->gu_freeze_lock);
5268*0Sstevel@tonic-gate 	scf_handle_destroy(h);
5269*0Sstevel@tonic-gate 
5270*0Sstevel@tonic-gate 	return (NULL);
5271*0Sstevel@tonic-gate }
5272*0Sstevel@tonic-gate 
5273*0Sstevel@tonic-gate 
5274*0Sstevel@tonic-gate /*
5275*0Sstevel@tonic-gate  * int next_action()
5276*0Sstevel@tonic-gate  *   Given an array of timestamps 'a' with 'num' elements, find the
5277*0Sstevel@tonic-gate  *   lowest non-zero timestamp and return its index. If there are no
5278*0Sstevel@tonic-gate  *   non-zero elements, return -1.
5279*0Sstevel@tonic-gate  */
5280*0Sstevel@tonic-gate static int
5281*0Sstevel@tonic-gate next_action(hrtime_t *a, int num)
5282*0Sstevel@tonic-gate {
5283*0Sstevel@tonic-gate 	hrtime_t t = 0;
5284*0Sstevel@tonic-gate 	int i = 0, smallest = -1;
5285*0Sstevel@tonic-gate 
5286*0Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
5287*0Sstevel@tonic-gate 		if (t == 0) {
5288*0Sstevel@tonic-gate 			t = a[i];
5289*0Sstevel@tonic-gate 			smallest = i;
5290*0Sstevel@tonic-gate 		} else if (a[i] != 0 && a[i] < t) {
5291*0Sstevel@tonic-gate 			t = a[i];
5292*0Sstevel@tonic-gate 			smallest = i;
5293*0Sstevel@tonic-gate 		}
5294*0Sstevel@tonic-gate 	}
5295*0Sstevel@tonic-gate 
5296*0Sstevel@tonic-gate 	if (t == 0)
5297*0Sstevel@tonic-gate 		return (-1);
5298*0Sstevel@tonic-gate 	else
5299*0Sstevel@tonic-gate 		return (smallest);
5300*0Sstevel@tonic-gate }
5301*0Sstevel@tonic-gate 
5302*0Sstevel@tonic-gate /*
5303*0Sstevel@tonic-gate  * void process_actions()
5304*0Sstevel@tonic-gate  *   Process actions requested by the administrator. Possibilities include:
5305*0Sstevel@tonic-gate  *   refresh, restart, maintenance mode off, maintenance mode on,
5306*0Sstevel@tonic-gate  *   maintenance mode immediate, and degraded.
5307*0Sstevel@tonic-gate  *
5308*0Sstevel@tonic-gate  *   The set of pending actions is represented in the repository as a
5309*0Sstevel@tonic-gate  *   per-instance property group, with each action being a single property
5310*0Sstevel@tonic-gate  *   in that group.  This property group is converted to an array, with each
5311*0Sstevel@tonic-gate  *   action type having an array slot.  The actions in the array at the
5312*0Sstevel@tonic-gate  *   time process_actions() is called are acted on in the order of the
5313*0Sstevel@tonic-gate  *   timestamp (which is the value stored in the slot).  A value of zero
5314*0Sstevel@tonic-gate  *   indicates that there is no pending action of the type associated with
5315*0Sstevel@tonic-gate  *   a particular slot.
5316*0Sstevel@tonic-gate  *
5317*0Sstevel@tonic-gate  *   Sending an action event multiple times before the restarter has a
5318*0Sstevel@tonic-gate  *   chance to process that action will force it to be run at the last
5319*0Sstevel@tonic-gate  *   timestamp where it appears in the ordering.
5320*0Sstevel@tonic-gate  *
5321*0Sstevel@tonic-gate  *   Turning maintenance mode on trumps all other actions.
5322*0Sstevel@tonic-gate  *
5323*0Sstevel@tonic-gate  *   Returns 0 or ECONNABORTED.
5324*0Sstevel@tonic-gate  */
5325*0Sstevel@tonic-gate static int
5326*0Sstevel@tonic-gate process_actions(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst)
5327*0Sstevel@tonic-gate {
5328*0Sstevel@tonic-gate 	scf_property_t *prop = NULL;
5329*0Sstevel@tonic-gate 	scf_value_t *val = NULL;
5330*0Sstevel@tonic-gate 	scf_type_t type;
5331*0Sstevel@tonic-gate 	graph_vertex_t *vertex;
5332*0Sstevel@tonic-gate 	admin_action_t a;
5333*0Sstevel@tonic-gate 	int i, ret = 0, r;
5334*0Sstevel@tonic-gate 	hrtime_t action_ts[NACTIONS];
5335*0Sstevel@tonic-gate 	char *inst_name;
5336*0Sstevel@tonic-gate 
5337*0Sstevel@tonic-gate 	r = libscf_instance_get_fmri(inst, &inst_name);
5338*0Sstevel@tonic-gate 	switch (r) {
5339*0Sstevel@tonic-gate 	case 0:
5340*0Sstevel@tonic-gate 		break;
5341*0Sstevel@tonic-gate 
5342*0Sstevel@tonic-gate 	case ECONNABORTED:
5343*0Sstevel@tonic-gate 		return (ECONNABORTED);
5344*0Sstevel@tonic-gate 
5345*0Sstevel@tonic-gate 	case ECANCELED:
5346*0Sstevel@tonic-gate 		return (0);
5347*0Sstevel@tonic-gate 
5348*0Sstevel@tonic-gate 	default:
5349*0Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", r);
5350*0Sstevel@tonic-gate 	}
5351*0Sstevel@tonic-gate 
5352*0Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
5353*0Sstevel@tonic-gate 
5354*0Sstevel@tonic-gate 	vertex = vertex_get_by_name(inst_name);
5355*0Sstevel@tonic-gate 	if (vertex == NULL) {
5356*0Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
5357*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "%s: Can't find graph vertex. "
5358*0Sstevel@tonic-gate 		    "The instance must have been removed.\n", inst_name);
5359*0Sstevel@tonic-gate 		return (0);
5360*0Sstevel@tonic-gate 	}
5361*0Sstevel@tonic-gate 
5362*0Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
5363*0Sstevel@tonic-gate 	val = safe_scf_value_create(h);
5364*0Sstevel@tonic-gate 
5365*0Sstevel@tonic-gate 	for (i = 0; i < NACTIONS; i++) {
5366*0Sstevel@tonic-gate 		if (scf_pg_get_property(pg, admin_actions[i], prop) != 0) {
5367*0Sstevel@tonic-gate 			switch (scf_error()) {
5368*0Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
5369*0Sstevel@tonic-gate 			default:
5370*0Sstevel@tonic-gate 				ret = ECONNABORTED;
5371*0Sstevel@tonic-gate 				goto out;
5372*0Sstevel@tonic-gate 
5373*0Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
5374*0Sstevel@tonic-gate 				goto out;
5375*0Sstevel@tonic-gate 
5376*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
5377*0Sstevel@tonic-gate 				action_ts[i] = 0;
5378*0Sstevel@tonic-gate 				continue;
5379*0Sstevel@tonic-gate 
5380*0Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
5381*0Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
5382*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
5383*0Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
5384*0Sstevel@tonic-gate 			}
5385*0Sstevel@tonic-gate 		}
5386*0Sstevel@tonic-gate 
5387*0Sstevel@tonic-gate 		if (scf_property_type(prop, &type) != 0) {
5388*0Sstevel@tonic-gate 			switch (scf_error()) {
5389*0Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
5390*0Sstevel@tonic-gate 			default:
5391*0Sstevel@tonic-gate 				ret = ECONNABORTED;
5392*0Sstevel@tonic-gate 				goto out;
5393*0Sstevel@tonic-gate 
5394*0Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
5395*0Sstevel@tonic-gate 				action_ts[i] = 0;
5396*0Sstevel@tonic-gate 				continue;
5397*0Sstevel@tonic-gate 
5398*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
5399*0Sstevel@tonic-gate 				bad_error("scf_property_type", scf_error());
5400*0Sstevel@tonic-gate 			}
5401*0Sstevel@tonic-gate 		}
5402*0Sstevel@tonic-gate 
5403*0Sstevel@tonic-gate 		if (type != SCF_TYPE_INTEGER) {
5404*0Sstevel@tonic-gate 			action_ts[i] = 0;
5405*0Sstevel@tonic-gate 			continue;
5406*0Sstevel@tonic-gate 		}
5407*0Sstevel@tonic-gate 
5408*0Sstevel@tonic-gate 		if (scf_property_get_value(prop, val) != 0) {
5409*0Sstevel@tonic-gate 			switch (scf_error()) {
5410*0Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
5411*0Sstevel@tonic-gate 			default:
5412*0Sstevel@tonic-gate 				ret = ECONNABORTED;
5413*0Sstevel@tonic-gate 				goto out;
5414*0Sstevel@tonic-gate 
5415*0Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
5416*0Sstevel@tonic-gate 				goto out;
5417*0Sstevel@tonic-gate 
5418*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
5419*0Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
5420*0Sstevel@tonic-gate 				action_ts[i] = 0;
5421*0Sstevel@tonic-gate 				continue;
5422*0Sstevel@tonic-gate 
5423*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
5424*0Sstevel@tonic-gate 				bad_error("scf_property_get_value",
5425*0Sstevel@tonic-gate 				    scf_error());
5426*0Sstevel@tonic-gate 			}
5427*0Sstevel@tonic-gate 		}
5428*0Sstevel@tonic-gate 
5429*0Sstevel@tonic-gate 		r = scf_value_get_integer(val, &action_ts[i]);
5430*0Sstevel@tonic-gate 		assert(r == 0);
5431*0Sstevel@tonic-gate 	}
5432*0Sstevel@tonic-gate 
5433*0Sstevel@tonic-gate 	a = ADMIN_EVENT_MAINT_ON_IMMEDIATE;
5434*0Sstevel@tonic-gate 	if (action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ||
5435*0Sstevel@tonic-gate 	    action_ts[ADMIN_EVENT_MAINT_ON]) {
5436*0Sstevel@tonic-gate 		a = action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ?
5437*0Sstevel@tonic-gate 		    ADMIN_EVENT_MAINT_ON_IMMEDIATE : ADMIN_EVENT_MAINT_ON;
5438*0Sstevel@tonic-gate 
5439*0Sstevel@tonic-gate 		vertex_send_event(vertex, admin_events[a]);
5440*0Sstevel@tonic-gate 		r = libscf_unset_action(h, pg, a, action_ts[a]);
5441*0Sstevel@tonic-gate 		switch (r) {
5442*0Sstevel@tonic-gate 		case 0:
5443*0Sstevel@tonic-gate 		case EACCES:
5444*0Sstevel@tonic-gate 			break;
5445*0Sstevel@tonic-gate 
5446*0Sstevel@tonic-gate 		case ECONNABORTED:
5447*0Sstevel@tonic-gate 			ret = ECONNABORTED;
5448*0Sstevel@tonic-gate 			goto out;
5449*0Sstevel@tonic-gate 
5450*0Sstevel@tonic-gate 		case EPERM:
5451*0Sstevel@tonic-gate 			uu_die("Insufficient privilege.\n");
5452*0Sstevel@tonic-gate 			/* NOTREACHED */
5453*0Sstevel@tonic-gate 
5454*0Sstevel@tonic-gate 		default:
5455*0Sstevel@tonic-gate 			bad_error("libscf_unset_action", r);
5456*0Sstevel@tonic-gate 		}
5457*0Sstevel@tonic-gate 	}
5458*0Sstevel@tonic-gate 
5459*0Sstevel@tonic-gate 	while ((a = next_action(action_ts, NACTIONS)) != -1) {
5460*0Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
5461*0Sstevel@tonic-gate 		    "Graph: processing %s action for %s.\n", admin_actions[a],
5462*0Sstevel@tonic-gate 		    inst_name);
5463*0Sstevel@tonic-gate 
5464*0Sstevel@tonic-gate 		if (a == ADMIN_EVENT_REFRESH) {
5465*0Sstevel@tonic-gate 			r = dgraph_refresh_instance(vertex, inst);
5466*0Sstevel@tonic-gate 			switch (r) {
5467*0Sstevel@tonic-gate 			case 0:
5468*0Sstevel@tonic-gate 			case ECANCELED:
5469*0Sstevel@tonic-gate 			case EINVAL:
5470*0Sstevel@tonic-gate 			case -1:
5471*0Sstevel@tonic-gate 				break;
5472*0Sstevel@tonic-gate 
5473*0Sstevel@tonic-gate 			case ECONNABORTED:
5474*0Sstevel@tonic-gate 				/* pg & inst are reset now, so just return. */
5475*0Sstevel@tonic-gate 				ret = ECONNABORTED;
5476*0Sstevel@tonic-gate 				goto out;
5477*0Sstevel@tonic-gate 
5478*0Sstevel@tonic-gate 			default:
5479*0Sstevel@tonic-gate 				bad_error("dgraph_refresh_instance", r);
5480*0Sstevel@tonic-gate 			}
5481*0Sstevel@tonic-gate 		}
5482*0Sstevel@tonic-gate 
5483*0Sstevel@tonic-gate 		vertex_send_event(vertex, admin_events[a]);
5484*0Sstevel@tonic-gate 
5485*0Sstevel@tonic-gate 		r = libscf_unset_action(h, pg, a, action_ts[a]);
5486*0Sstevel@tonic-gate 		switch (r) {
5487*0Sstevel@tonic-gate 		case 0:
5488*0Sstevel@tonic-gate 		case EACCES:
5489*0Sstevel@tonic-gate 			break;
5490*0Sstevel@tonic-gate 
5491*0Sstevel@tonic-gate 		case ECONNABORTED:
5492*0Sstevel@tonic-gate 			ret = ECONNABORTED;
5493*0Sstevel@tonic-gate 			goto out;
5494*0Sstevel@tonic-gate 
5495*0Sstevel@tonic-gate 		case EPERM:
5496*0Sstevel@tonic-gate 			uu_die("Insufficient privilege.\n");
5497*0Sstevel@tonic-gate 			/* NOTREACHED */
5498*0Sstevel@tonic-gate 
5499*0Sstevel@tonic-gate 		default:
5500*0Sstevel@tonic-gate 			bad_error("libscf_unset_action", r);
5501*0Sstevel@tonic-gate 		}
5502*0Sstevel@tonic-gate 
5503*0Sstevel@tonic-gate 		action_ts[a] = 0;
5504*0Sstevel@tonic-gate 	}
5505*0Sstevel@tonic-gate 
5506*0Sstevel@tonic-gate out:
5507*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
5508*0Sstevel@tonic-gate 
5509*0Sstevel@tonic-gate 	scf_property_destroy(prop);
5510*0Sstevel@tonic-gate 	scf_value_destroy(val);
5511*0Sstevel@tonic-gate 	startd_free(inst_name, max_scf_fmri_size);
5512*0Sstevel@tonic-gate 	return (ret);
5513*0Sstevel@tonic-gate }
5514*0Sstevel@tonic-gate 
5515*0Sstevel@tonic-gate /*
5516*0Sstevel@tonic-gate  * inst and pg_name are scratch space, and are unset on entry.
5517*0Sstevel@tonic-gate  * Returns
5518*0Sstevel@tonic-gate  *   0 - success
5519*0Sstevel@tonic-gate  *   ECONNRESET - success, but repository handle rebound
5520*0Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
5521*0Sstevel@tonic-gate  */
5522*0Sstevel@tonic-gate static int
5523*0Sstevel@tonic-gate process_pg_event(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst,
5524*0Sstevel@tonic-gate     char *pg_name)
5525*0Sstevel@tonic-gate {
5526*0Sstevel@tonic-gate 	int r;
5527*0Sstevel@tonic-gate 	scf_property_t *prop;
5528*0Sstevel@tonic-gate 	scf_value_t *val;
5529*0Sstevel@tonic-gate 	char *fmri;
5530*0Sstevel@tonic-gate 	boolean_t rebound = B_FALSE, rebind_inst = B_FALSE;
5531*0Sstevel@tonic-gate 
5532*0Sstevel@tonic-gate 	if (scf_pg_get_name(pg, pg_name, max_scf_value_size) < 0) {
5533*0Sstevel@tonic-gate 		switch (scf_error()) {
5534*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
5535*0Sstevel@tonic-gate 		default:
5536*0Sstevel@tonic-gate 			return (ECONNABORTED);
5537*0Sstevel@tonic-gate 
5538*0Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
5539*0Sstevel@tonic-gate 			return (0);
5540*0Sstevel@tonic-gate 
5541*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
5542*0Sstevel@tonic-gate 			bad_error("scf_pg_get_name", scf_error());
5543*0Sstevel@tonic-gate 		}
5544*0Sstevel@tonic-gate 	}
5545*0Sstevel@tonic-gate 
5546*0Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_GENERAL) == 0 ||
5547*0Sstevel@tonic-gate 	    strcmp(pg_name, SCF_PG_GENERAL_OVR) == 0) {
5548*0Sstevel@tonic-gate 		r = dgraph_update_general(pg);
5549*0Sstevel@tonic-gate 		switch (r) {
5550*0Sstevel@tonic-gate 		case 0:
5551*0Sstevel@tonic-gate 		case ENOTSUP:
5552*0Sstevel@tonic-gate 		case ECANCELED:
5553*0Sstevel@tonic-gate 			return (0);
5554*0Sstevel@tonic-gate 
5555*0Sstevel@tonic-gate 		case ECONNABORTED:
5556*0Sstevel@tonic-gate 			return (ECONNABORTED);
5557*0Sstevel@tonic-gate 
5558*0Sstevel@tonic-gate 		case -1:
5559*0Sstevel@tonic-gate 			/* Error should have been logged. */
5560*0Sstevel@tonic-gate 			return (0);
5561*0Sstevel@tonic-gate 
5562*0Sstevel@tonic-gate 		default:
5563*0Sstevel@tonic-gate 			bad_error("dgraph_update_general", r);
5564*0Sstevel@tonic-gate 		}
5565*0Sstevel@tonic-gate 	} else if (strcmp(pg_name, SCF_PG_RESTARTER_ACTIONS) == 0) {
5566*0Sstevel@tonic-gate 		if (scf_pg_get_parent_instance(pg, inst) != 0) {
5567*0Sstevel@tonic-gate 			switch (scf_error()) {
5568*0Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
5569*0Sstevel@tonic-gate 				return (ECONNABORTED);
5570*0Sstevel@tonic-gate 
5571*0Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
5572*0Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
5573*0Sstevel@tonic-gate 				/* Ignore commands on services. */
5574*0Sstevel@tonic-gate 				return (0);
5575*0Sstevel@tonic-gate 
5576*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
5577*0Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
5578*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
5579*0Sstevel@tonic-gate 			default:
5580*0Sstevel@tonic-gate 				bad_error("scf_pg_get_parent_instance",
5581*0Sstevel@tonic-gate 				    scf_error());
5582*0Sstevel@tonic-gate 			}
5583*0Sstevel@tonic-gate 		}
5584*0Sstevel@tonic-gate 
5585*0Sstevel@tonic-gate 		return (process_actions(h, pg, inst));
5586*0Sstevel@tonic-gate 	}
5587*0Sstevel@tonic-gate 
5588*0Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_OPTIONS) != 0 &&
5589*0Sstevel@tonic-gate 	    strcmp(pg_name, SCF_PG_OPTIONS_OVR) != 0)
5590*0Sstevel@tonic-gate 		return (0);
5591*0Sstevel@tonic-gate 
5592*0Sstevel@tonic-gate 	/*
5593*0Sstevel@tonic-gate 	 * We only care about the options[_ovr] property groups of our own
5594*0Sstevel@tonic-gate 	 * instance, so get the fmri and compare.  Plus, once we know it's
5595*0Sstevel@tonic-gate 	 * correct, if the repository connection is broken we know exactly what
5596*0Sstevel@tonic-gate 	 * property group we were operating on, and can look it up again.
5597*0Sstevel@tonic-gate 	 */
5598*0Sstevel@tonic-gate 	if (scf_pg_get_parent_instance(pg, inst) != 0) {
5599*0Sstevel@tonic-gate 		switch (scf_error()) {
5600*0Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
5601*0Sstevel@tonic-gate 			return (ECONNABORTED);
5602*0Sstevel@tonic-gate 
5603*0Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
5604*0Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
5605*0Sstevel@tonic-gate 			return (0);
5606*0Sstevel@tonic-gate 
5607*0Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
5608*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
5609*0Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
5610*0Sstevel@tonic-gate 		default:
5611*0Sstevel@tonic-gate 			bad_error("scf_pg_get_parent_instance",
5612*0Sstevel@tonic-gate 			    scf_error());
5613*0Sstevel@tonic-gate 		}
5614*0Sstevel@tonic-gate 	}
5615*0Sstevel@tonic-gate 
5616*0Sstevel@tonic-gate 	switch (r = libscf_instance_get_fmri(inst, &fmri)) {
5617*0Sstevel@tonic-gate 	case 0:
5618*0Sstevel@tonic-gate 		break;
5619*0Sstevel@tonic-gate 
5620*0Sstevel@tonic-gate 	case ECONNABORTED:
5621*0Sstevel@tonic-gate 		return (ECONNABORTED);
5622*0Sstevel@tonic-gate 
5623*0Sstevel@tonic-gate 	case ECANCELED:
5624*0Sstevel@tonic-gate 		return (0);
5625*0Sstevel@tonic-gate 
5626*0Sstevel@tonic-gate 	default:
5627*0Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", r);
5628*0Sstevel@tonic-gate 	}
5629*0Sstevel@tonic-gate 
5630*0Sstevel@tonic-gate 	if (strcmp(fmri, SCF_SERVICE_STARTD) != 0) {
5631*0Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
5632*0Sstevel@tonic-gate 		return (0);
5633*0Sstevel@tonic-gate 	}
5634*0Sstevel@tonic-gate 
5635*0Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
5636*0Sstevel@tonic-gate 	val = safe_scf_value_create(h);
5637*0Sstevel@tonic-gate 
5638*0Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_OPTIONS_OVR) == 0) {
5639*0Sstevel@tonic-gate 		/* See if we need to set the runlevel. */
5640*0Sstevel@tonic-gate 		/* CONSTCOND */
5641*0Sstevel@tonic-gate 		if (0) {
5642*0Sstevel@tonic-gate rebind_pg:
5643*0Sstevel@tonic-gate 			libscf_handle_rebind(h);
5644*0Sstevel@tonic-gate 			rebound = B_TRUE;
5645*0Sstevel@tonic-gate 
5646*0Sstevel@tonic-gate 			r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst);
5647*0Sstevel@tonic-gate 			switch (r) {
5648*0Sstevel@tonic-gate 			case 0:
5649*0Sstevel@tonic-gate 				break;
5650*0Sstevel@tonic-gate 
5651*0Sstevel@tonic-gate 			case ECONNABORTED:
5652*0Sstevel@tonic-gate 				goto rebind_pg;
5653*0Sstevel@tonic-gate 
5654*0Sstevel@tonic-gate 			case ENOENT:
5655*0Sstevel@tonic-gate 				goto out;
5656*0Sstevel@tonic-gate 
5657*0Sstevel@tonic-gate 			case EINVAL:
5658*0Sstevel@tonic-gate 			case ENOTSUP:
5659*0Sstevel@tonic-gate 				bad_error("libscf_lookup_instance", r);
5660*0Sstevel@tonic-gate 			}
5661*0Sstevel@tonic-gate 
5662*0Sstevel@tonic-gate 			if (scf_instance_get_pg(inst, pg_name, pg) != 0) {
5663*0Sstevel@tonic-gate 				switch (scf_error()) {
5664*0Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
5665*0Sstevel@tonic-gate 				case SCF_ERROR_NOT_FOUND:
5666*0Sstevel@tonic-gate 					goto out;
5667*0Sstevel@tonic-gate 
5668*0Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
5669*0Sstevel@tonic-gate 					goto rebind_pg;
5670*0Sstevel@tonic-gate 
5671*0Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
5672*0Sstevel@tonic-gate 				case SCF_ERROR_NOT_BOUND:
5673*0Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
5674*0Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
5675*0Sstevel@tonic-gate 				default:
5676*0Sstevel@tonic-gate 					bad_error("scf_instance_get_pg",
5677*0Sstevel@tonic-gate 					    scf_error());
5678*0Sstevel@tonic-gate 				}
5679*0Sstevel@tonic-gate 			}
5680*0Sstevel@tonic-gate 		}
5681*0Sstevel@tonic-gate 
5682*0Sstevel@tonic-gate 		if (scf_pg_get_property(pg, "runlevel", prop) == 0) {
5683*0Sstevel@tonic-gate 			r = dgraph_set_runlevel(pg, prop);
5684*0Sstevel@tonic-gate 			switch (r) {
5685*0Sstevel@tonic-gate 			case ECONNRESET:
5686*0Sstevel@tonic-gate 				rebound = B_TRUE;
5687*0Sstevel@tonic-gate 				rebind_inst = B_TRUE;
5688*0Sstevel@tonic-gate 				/* FALLTHROUGH */
5689*0Sstevel@tonic-gate 
5690*0Sstevel@tonic-gate 			case 0:
5691*0Sstevel@tonic-gate 				break;
5692*0Sstevel@tonic-gate 
5693*0Sstevel@tonic-gate 			case ECONNABORTED:
5694*0Sstevel@tonic-gate 				goto rebind_pg;
5695*0Sstevel@tonic-gate 
5696*0Sstevel@tonic-gate 			case ECANCELED:
5697*0Sstevel@tonic-gate 				goto out;
5698*0Sstevel@tonic-gate 
5699*0Sstevel@tonic-gate 			default:
5700*0Sstevel@tonic-gate 				bad_error("dgraph_set_runlevel", r);
5701*0Sstevel@tonic-gate 			}
5702*0Sstevel@tonic-gate 		} else {
5703*0Sstevel@tonic-gate 			switch (scf_error()) {
5704*0Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
5705*0Sstevel@tonic-gate 			default:
5706*0Sstevel@tonic-gate 				goto rebind_pg;
5707*0Sstevel@tonic-gate 
5708*0Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
5709*0Sstevel@tonic-gate 				goto out;
5710*0Sstevel@tonic-gate 
5711*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
5712*0Sstevel@tonic-gate 				break;
5713*0Sstevel@tonic-gate 
5714*0Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
5715*0Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
5716*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
5717*0Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
5718*0Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
5719*0Sstevel@tonic-gate 			}
5720*0Sstevel@tonic-gate 		}
5721*0Sstevel@tonic-gate 	}
5722*0Sstevel@tonic-gate 
5723*0Sstevel@tonic-gate 	if (rebind_inst) {
5724*0Sstevel@tonic-gate lookup_inst:
5725*0Sstevel@tonic-gate 		r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst);
5726*0Sstevel@tonic-gate 		switch (r) {
5727*0Sstevel@tonic-gate 		case 0:
5728*0Sstevel@tonic-gate 			break;
5729*0Sstevel@tonic-gate 
5730*0Sstevel@tonic-gate 		case ECONNABORTED:
5731*0Sstevel@tonic-gate 			libscf_handle_rebind(h);
5732*0Sstevel@tonic-gate 			rebound = B_TRUE;
5733*0Sstevel@tonic-gate 			goto lookup_inst;
5734*0Sstevel@tonic-gate 
5735*0Sstevel@tonic-gate 		case ENOENT:
5736*0Sstevel@tonic-gate 			goto out;
5737*0Sstevel@tonic-gate 
5738*0Sstevel@tonic-gate 		case EINVAL:
5739*0Sstevel@tonic-gate 		case ENOTSUP:
5740*0Sstevel@tonic-gate 			bad_error("libscf_lookup_instance", r);
5741*0Sstevel@tonic-gate 		}
5742*0Sstevel@tonic-gate 	}
5743*0Sstevel@tonic-gate 
5744*0Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size);
5745*0Sstevel@tonic-gate 	switch (r) {
5746*0Sstevel@tonic-gate 	case 0:
5747*0Sstevel@tonic-gate 		break;
5748*0Sstevel@tonic-gate 
5749*0Sstevel@tonic-gate 	case ECONNABORTED:
5750*0Sstevel@tonic-gate 		libscf_handle_rebind(h);
5751*0Sstevel@tonic-gate 		rebound = B_TRUE;
5752*0Sstevel@tonic-gate 		goto lookup_inst;
5753*0Sstevel@tonic-gate 
5754*0Sstevel@tonic-gate 	case EINVAL:
5755*0Sstevel@tonic-gate 		log_error(LOG_NOTICE,
5756*0Sstevel@tonic-gate 		    "%s/%s property of %s is misconfigured.\n", pg_name,
5757*0Sstevel@tonic-gate 		    SCF_PROPERTY_MILESTONE, SCF_SERVICE_STARTD);
5758*0Sstevel@tonic-gate 		/* FALLTHROUGH */
5759*0Sstevel@tonic-gate 
5760*0Sstevel@tonic-gate 	case ECANCELED:
5761*0Sstevel@tonic-gate 	case ENOENT:
5762*0Sstevel@tonic-gate 		(void) strcpy(fmri, "all");
5763*0Sstevel@tonic-gate 		break;
5764*0Sstevel@tonic-gate 
5765*0Sstevel@tonic-gate 	default:
5766*0Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
5767*0Sstevel@tonic-gate 	}
5768*0Sstevel@tonic-gate 
5769*0Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_FALSE);
5770*0Sstevel@tonic-gate 	switch (r) {
5771*0Sstevel@tonic-gate 	case 0:
5772*0Sstevel@tonic-gate 	case ECONNRESET:
5773*0Sstevel@tonic-gate 	case EALREADY:
5774*0Sstevel@tonic-gate 		break;
5775*0Sstevel@tonic-gate 
5776*0Sstevel@tonic-gate 	case EINVAL:
5777*0Sstevel@tonic-gate 		log_error(LOG_WARNING, "Milestone %s is invalid.\n", fmri);
5778*0Sstevel@tonic-gate 		break;
5779*0Sstevel@tonic-gate 
5780*0Sstevel@tonic-gate 	case ENOENT:
5781*0Sstevel@tonic-gate 		log_error(LOG_WARNING, "Milestone %s does not exist.\n", fmri);
5782*0Sstevel@tonic-gate 		break;
5783*0Sstevel@tonic-gate 
5784*0Sstevel@tonic-gate 	default:
5785*0Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
5786*0Sstevel@tonic-gate 	}
5787*0Sstevel@tonic-gate 
5788*0Sstevel@tonic-gate out:
5789*0Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
5790*0Sstevel@tonic-gate 	scf_value_destroy(val);
5791*0Sstevel@tonic-gate 	scf_property_destroy(prop);
5792*0Sstevel@tonic-gate 
5793*0Sstevel@tonic-gate 	return (rebound ? ECONNRESET : 0);
5794*0Sstevel@tonic-gate }
5795*0Sstevel@tonic-gate 
5796*0Sstevel@tonic-gate static void
5797*0Sstevel@tonic-gate process_delete(char *fmri, scf_handle_t *h)
5798*0Sstevel@tonic-gate {
5799*0Sstevel@tonic-gate 	char *lfmri;
5800*0Sstevel@tonic-gate 	const char *inst_name, *pg_name;
5801*0Sstevel@tonic-gate 
5802*0Sstevel@tonic-gate 	lfmri = safe_strdup(fmri);
5803*0Sstevel@tonic-gate 
5804*0Sstevel@tonic-gate 	/* Determine if the FMRI is a property group or instance */
5805*0Sstevel@tonic-gate 	if (scf_parse_svc_fmri(lfmri, NULL, NULL, &inst_name, &pg_name,
5806*0Sstevel@tonic-gate 	    NULL) != SCF_SUCCESS) {
5807*0Sstevel@tonic-gate 		log_error(LOG_WARNING,
5808*0Sstevel@tonic-gate 		    "Received invalid FMRI \"%s\" from repository server.\n",
5809*0Sstevel@tonic-gate 		    fmri);
5810*0Sstevel@tonic-gate 	} else if (inst_name != NULL && pg_name == NULL) {
5811*0Sstevel@tonic-gate 		(void) dgraph_remove_instance(fmri, h);
5812*0Sstevel@tonic-gate 	}
5813*0Sstevel@tonic-gate 
5814*0Sstevel@tonic-gate 	free(lfmri);
5815*0Sstevel@tonic-gate }
5816*0Sstevel@tonic-gate 
5817*0Sstevel@tonic-gate /*ARGSUSED*/
5818*0Sstevel@tonic-gate void *
5819*0Sstevel@tonic-gate repository_event_thread(void *unused)
5820*0Sstevel@tonic-gate {
5821*0Sstevel@tonic-gate 	scf_handle_t *h;
5822*0Sstevel@tonic-gate 	scf_propertygroup_t *pg;
5823*0Sstevel@tonic-gate 	scf_instance_t *inst;
5824*0Sstevel@tonic-gate 	char *fmri = startd_alloc(max_scf_fmri_size);
5825*0Sstevel@tonic-gate 	char *pg_name = startd_alloc(max_scf_value_size);
5826*0Sstevel@tonic-gate 	int r;
5827*0Sstevel@tonic-gate 
5828*0Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
5829*0Sstevel@tonic-gate 
5830*0Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
5831*0Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
5832*0Sstevel@tonic-gate 
5833*0Sstevel@tonic-gate retry:
5834*0Sstevel@tonic-gate 	if (_scf_notify_add_pgtype(h, SCF_GROUP_FRAMEWORK) != SCF_SUCCESS) {
5835*0Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN) {
5836*0Sstevel@tonic-gate 			libscf_handle_rebind(h);
5837*0Sstevel@tonic-gate 		} else {
5838*0Sstevel@tonic-gate 			log_error(LOG_WARNING,
5839*0Sstevel@tonic-gate 			    "Couldn't set up repository notification "
5840*0Sstevel@tonic-gate 			    "for property group type %s: %s\n",
5841*0Sstevel@tonic-gate 			    SCF_GROUP_FRAMEWORK, scf_strerror(scf_error()));
5842*0Sstevel@tonic-gate 
5843*0Sstevel@tonic-gate 			(void) sleep(1);
5844*0Sstevel@tonic-gate 		}
5845*0Sstevel@tonic-gate 
5846*0Sstevel@tonic-gate 		goto retry;
5847*0Sstevel@tonic-gate 	}
5848*0Sstevel@tonic-gate 
5849*0Sstevel@tonic-gate 	/*CONSTCOND*/
5850*0Sstevel@tonic-gate 	while (1) {
5851*0Sstevel@tonic-gate 		ssize_t res;
5852*0Sstevel@tonic-gate 
5853*0Sstevel@tonic-gate 		/* Note: fmri is only set on delete events. */
5854*0Sstevel@tonic-gate 		res = _scf_notify_wait(pg, fmri, max_scf_fmri_size);
5855*0Sstevel@tonic-gate 		if (res < 0) {
5856*0Sstevel@tonic-gate 			libscf_handle_rebind(h);
5857*0Sstevel@tonic-gate 			goto retry;
5858*0Sstevel@tonic-gate 		} else if (res == 0) {
5859*0Sstevel@tonic-gate 			/*
5860*0Sstevel@tonic-gate 			 * property group modified.  inst and pg_name are
5861*0Sstevel@tonic-gate 			 * pre-allocated scratch space.
5862*0Sstevel@tonic-gate 			 */
5863*0Sstevel@tonic-gate 			if (scf_pg_update(pg) < 0) {
5864*0Sstevel@tonic-gate 				switch (scf_error()) {
5865*0Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
5866*0Sstevel@tonic-gate 					continue;
5867*0Sstevel@tonic-gate 
5868*0Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
5869*0Sstevel@tonic-gate 					log_error(LOG_WARNING,
5870*0Sstevel@tonic-gate 					    "Lost repository event due to "
5871*0Sstevel@tonic-gate 					    "disconnection.\n");
5872*0Sstevel@tonic-gate 					libscf_handle_rebind(h);
5873*0Sstevel@tonic-gate 					goto retry;
5874*0Sstevel@tonic-gate 
5875*0Sstevel@tonic-gate 				case SCF_ERROR_NOT_BOUND:
5876*0Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
5877*0Sstevel@tonic-gate 				default:
5878*0Sstevel@tonic-gate 					bad_error("scf_pg_update", scf_error());
5879*0Sstevel@tonic-gate 				}
5880*0Sstevel@tonic-gate 			}
5881*0Sstevel@tonic-gate 
5882*0Sstevel@tonic-gate 			r = process_pg_event(h, pg, inst, pg_name);
5883*0Sstevel@tonic-gate 			switch (r) {
5884*0Sstevel@tonic-gate 			case 0:
5885*0Sstevel@tonic-gate 				break;
5886*0Sstevel@tonic-gate 
5887*0Sstevel@tonic-gate 			case ECONNABORTED:
5888*0Sstevel@tonic-gate 				log_error(LOG_WARNING, "Lost repository event "
5889*0Sstevel@tonic-gate 				    "due to disconnection.\n");
5890*0Sstevel@tonic-gate 				libscf_handle_rebind(h);
5891*0Sstevel@tonic-gate 				/* FALLTHROUGH */
5892*0Sstevel@tonic-gate 
5893*0Sstevel@tonic-gate 			case ECONNRESET:
5894*0Sstevel@tonic-gate 				goto retry;
5895*0Sstevel@tonic-gate 
5896*0Sstevel@tonic-gate 			default:
5897*0Sstevel@tonic-gate 				bad_error("process_pg_event", r);
5898*0Sstevel@tonic-gate 			}
5899*0Sstevel@tonic-gate 		} else {
5900*0Sstevel@tonic-gate 			/* service, instance, or pg deleted. */
5901*0Sstevel@tonic-gate 			process_delete(fmri, h);
5902*0Sstevel@tonic-gate 		}
5903*0Sstevel@tonic-gate 	}
5904*0Sstevel@tonic-gate 
5905*0Sstevel@tonic-gate 	/*NOTREACHED*/
5906*0Sstevel@tonic-gate 	return (NULL);
5907*0Sstevel@tonic-gate }
5908*0Sstevel@tonic-gate 
5909*0Sstevel@tonic-gate void
5910*0Sstevel@tonic-gate graph_engine_start()
5911*0Sstevel@tonic-gate {
5912*0Sstevel@tonic-gate 	int err;
5913*0Sstevel@tonic-gate 
5914*0Sstevel@tonic-gate 	(void) startd_thread_create(graph_thread, NULL);
5915*0Sstevel@tonic-gate 
5916*0Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
5917*0Sstevel@tonic-gate 	while (!initial_milestone_set) {
5918*0Sstevel@tonic-gate 		err = pthread_cond_wait(&initial_milestone_cv, &dgraph_lock);
5919*0Sstevel@tonic-gate 		assert(err == 0);
5920*0Sstevel@tonic-gate 	}
5921*0Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
5922*0Sstevel@tonic-gate 
5923*0Sstevel@tonic-gate 	(void) startd_thread_create(repository_event_thread, NULL);
5924*0Sstevel@tonic-gate 	(void) startd_thread_create(graph_event_thread, NULL);
5925*0Sstevel@tonic-gate }
5926