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