xref: /onnv-gate/usr/src/cmd/svc/startd/graph.c (revision 8354:05e1ad9f9a99)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51514Srm88369  * Common Development and Distribution License (the "License").
61514Srm88369  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
226055Srm88369  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * graph.c - master restarter graph engine
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  *   The graph engine keeps a dependency graph of all service instances on the
300Sstevel@tonic-gate  *   system, as recorded in the repository.  It decides when services should
310Sstevel@tonic-gate  *   be brought up or down based on service states and dependencies and sends
320Sstevel@tonic-gate  *   commands to restarters to effect any changes.  It also executes
330Sstevel@tonic-gate  *   administrator commands sent by svcadm via the repository.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  *   The graph is stored in uu_list_t *dgraph and its vertices are
360Sstevel@tonic-gate  *   graph_vertex_t's, each of which has a name and an integer id unique to
370Sstevel@tonic-gate  *   its name (see dict.c).  A vertex's type attribute designates the type
380Sstevel@tonic-gate  *   of object it represents: GVT_INST for service instances, GVT_SVC for
390Sstevel@tonic-gate  *   service objects (since service instances may depend on another service,
400Sstevel@tonic-gate  *   rather than service instance), GVT_FILE for files (which services may
410Sstevel@tonic-gate  *   depend on), and GVT_GROUP for dependencies on multiple objects.  GVT_GROUP
420Sstevel@tonic-gate  *   vertices are necessary because dependency lists may have particular
430Sstevel@tonic-gate  *   grouping types (require any, require all, optional, or exclude) and
440Sstevel@tonic-gate  *   event-propagation characteristics.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  *   The initial graph is built by libscf_populate_graph() invoking
470Sstevel@tonic-gate  *   dgraph_add_instance() for each instance in the repository.  The function
480Sstevel@tonic-gate  *   adds a GVT_SVC vertex for the service if one does not already exist, adds
490Sstevel@tonic-gate  *   a GVT_INST vertex named by the FMRI of the instance, and sets up the edges.
500Sstevel@tonic-gate  *   The resulting web of vertices & edges associated with an instance's vertex
510Sstevel@tonic-gate  *   includes
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  *     - an edge from the GVT_SVC vertex for the instance's service
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  *     - an edge to the GVT_INST vertex of the instance's resarter, if its
560Sstevel@tonic-gate  *       restarter is not svc.startd
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  *     - edges from other GVT_INST vertices if the instance is a restarter
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  *     - for each dependency property group in the instance's "running"
610Sstevel@tonic-gate  *       snapshot, an edge to a GVT_GROUP vertex named by the FMRI of the
620Sstevel@tonic-gate  *       instance and the name of the property group
630Sstevel@tonic-gate  *
640Sstevel@tonic-gate  *     - for each value of the "entities" property in each dependency property
650Sstevel@tonic-gate  *       group, an edge from the corresponding GVT_GROUP vertex to a
660Sstevel@tonic-gate  *       GVT_INST, GVT_SVC, or GVT_FILE vertex
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  *     - edges from GVT_GROUP vertices for each dependent instance
690Sstevel@tonic-gate  *
700Sstevel@tonic-gate  *   After the edges are set up the vertex's GV_CONFIGURED flag is set.  If
710Sstevel@tonic-gate  *   there are problems, or if a service is mentioned in a dependency but does
720Sstevel@tonic-gate  *   not exist in the repository, the GV_CONFIGURED flag will be clear.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  *   The graph and all of its vertices are protected by the dgraph_lock mutex.
750Sstevel@tonic-gate  *   See restarter.c for more information.
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  *   The properties of an instance fall into two classes: immediate and
780Sstevel@tonic-gate  *   snapshotted.  Immediate properties should have an immediate effect when
790Sstevel@tonic-gate  *   changed.  Snapshotted properties should be read from a snapshot, so they
800Sstevel@tonic-gate  *   only change when the snapshot changes.  The immediate properties used by
810Sstevel@tonic-gate  *   the graph engine are general/enabled, general/restarter, and the properties
820Sstevel@tonic-gate  *   in the restarter_actions property group.  Since they are immediate, they
830Sstevel@tonic-gate  *   are not read out of a snapshot.  The snapshotted properties used by the
840Sstevel@tonic-gate  *   graph engine are those in the property groups with type "dependency" and
850Sstevel@tonic-gate  *   are read out of the "running" snapshot.  The "running" snapshot is created
860Sstevel@tonic-gate  *   by the the graph engine as soon as possible, and it is updated, along with
870Sstevel@tonic-gate  *   in-core copies of the data (dependency information for the graph engine) on
880Sstevel@tonic-gate  *   receipt of the refresh command from svcadm.  In addition, the graph engine
890Sstevel@tonic-gate  *   updates the "start" snapshot from the "running" snapshot whenever a service
900Sstevel@tonic-gate  *   comes online.
917630SRenaud.Manus@Sun.COM  *
927630SRenaud.Manus@Sun.COM  *   When a DISABLE event is requested by the administrator, svc.startd shutdown
937630SRenaud.Manus@Sun.COM  *   the dependents first before shutting down the requested service.
947630SRenaud.Manus@Sun.COM  *   In graph_enable_by_vertex, we create a subtree that contains the dependent
957630SRenaud.Manus@Sun.COM  *   vertices by marking those vertices with the GV_TOOFFLINE flag. And we mark
967630SRenaud.Manus@Sun.COM  *   the vertex to disable with the GV_TODISABLE flag. Once the tree is created,
977630SRenaud.Manus@Sun.COM  *   we send the _ADMIN_DISABLE event to the leaves. The leaves will then
987630SRenaud.Manus@Sun.COM  *   transition from STATE_ONLINE/STATE_DEGRADED to STATE_OFFLINE/STATE_MAINT.
997630SRenaud.Manus@Sun.COM  *   In gt_enter_offline and gt_enter_maint if the vertex was in a subtree then
1007630SRenaud.Manus@Sun.COM  *   we clear the GV_TOOFFLINE flag and walk the dependencies to offline the new
1017630SRenaud.Manus@Sun.COM  *   exposed leaves. We do the same until we reach the last leaf (the one with
1027630SRenaud.Manus@Sun.COM  *   the GV_TODISABLE flag). If the vertex to disable is also part of a larger
1037630SRenaud.Manus@Sun.COM  *   subtree (eg. multiple DISABLE events on vertices in the same subtree) then
1047630SRenaud.Manus@Sun.COM  *   once the first vertex is disabled (GV_TODISABLE flag is removed), we
1057630SRenaud.Manus@Sun.COM  *   continue to propagate the offline event to the vertex's dependencies.
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate #include <sys/uadmin.h>
1090Sstevel@tonic-gate #include <sys/wait.h>
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate #include <assert.h>
1120Sstevel@tonic-gate #include <errno.h>
1130Sstevel@tonic-gate #include <fcntl.h>
1140Sstevel@tonic-gate #include <libscf.h>
1150Sstevel@tonic-gate #include <libscf_priv.h>
1160Sstevel@tonic-gate #include <libuutil.h>
1170Sstevel@tonic-gate #include <locale.h>
1180Sstevel@tonic-gate #include <poll.h>
1190Sstevel@tonic-gate #include <pthread.h>
1200Sstevel@tonic-gate #include <signal.h>
1210Sstevel@tonic-gate #include <stddef.h>
1220Sstevel@tonic-gate #include <stdio.h>
1230Sstevel@tonic-gate #include <stdlib.h>
1240Sstevel@tonic-gate #include <string.h>
1250Sstevel@tonic-gate #include <strings.h>
1260Sstevel@tonic-gate #include <sys/statvfs.h>
1270Sstevel@tonic-gate #include <sys/uadmin.h>
1280Sstevel@tonic-gate #include <zone.h>
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate #include "startd.h"
1310Sstevel@tonic-gate #include "protocol.h"
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate #define	MILESTONE_NONE	((graph_vertex_t *)1)
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate #define	CONSOLE_LOGIN_FMRI	"svc:/system/console-login:default"
1370Sstevel@tonic-gate #define	FS_MINIMAL_FMRI		"svc:/system/filesystem/minimal:default"
1380Sstevel@tonic-gate 
1391712Srm88369 #define	VERTEX_REMOVED	0	/* vertex has been freed  */
1401712Srm88369 #define	VERTEX_INUSE	1	/* vertex is still in use */
1411712Srm88369 
1422747Sbustos /*
1432747Sbustos  * Services in these states are not considered 'down' by the
1442747Sbustos  * milestone/shutdown code.
1452747Sbustos  */
1462747Sbustos #define	up_state(state)	((state) == RESTARTER_STATE_ONLINE || \
1472747Sbustos 	(state) == RESTARTER_STATE_DEGRADED || \
1482747Sbustos 	(state) == RESTARTER_STATE_OFFLINE)
1492747Sbustos 
1500Sstevel@tonic-gate static uu_list_pool_t *graph_edge_pool, *graph_vertex_pool;
1510Sstevel@tonic-gate static uu_list_t *dgraph;
1520Sstevel@tonic-gate static pthread_mutex_t dgraph_lock;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate /*
1550Sstevel@tonic-gate  * milestone indicates the current subgraph.  When NULL, it is the entire
1560Sstevel@tonic-gate  * graph.  When MILESTONE_NONE, it is the empty graph.  Otherwise, it is all
1570Sstevel@tonic-gate  * services on which the target vertex depends.
1580Sstevel@tonic-gate  */
1590Sstevel@tonic-gate static graph_vertex_t *milestone = NULL;
1600Sstevel@tonic-gate static boolean_t initial_milestone_set = B_FALSE;
1610Sstevel@tonic-gate static pthread_cond_t initial_milestone_cv = PTHREAD_COND_INITIALIZER;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /* protected by dgraph_lock */
1640Sstevel@tonic-gate static boolean_t sulogin_thread_running = B_FALSE;
1650Sstevel@tonic-gate static boolean_t sulogin_running = B_FALSE;
1660Sstevel@tonic-gate static boolean_t console_login_ready = B_FALSE;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /* Number of services to come down to complete milestone transition. */
1690Sstevel@tonic-gate static uint_t non_subgraph_svcs;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * These variables indicate what should be done when we reach the milestone
1730Sstevel@tonic-gate  * target milestone, i.e., when non_subgraph_svcs == 0.  They are acted upon in
1740Sstevel@tonic-gate  * dgraph_set_instance_state().
1750Sstevel@tonic-gate  */
1760Sstevel@tonic-gate static int halting = -1;
1770Sstevel@tonic-gate static boolean_t go_single_user_mode = B_FALSE;
1780Sstevel@tonic-gate static boolean_t go_to_level1 = B_FALSE;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate  * This tracks the legacy runlevel to ensure we signal init and manage
1820Sstevel@tonic-gate  * utmpx entries correctly.
1830Sstevel@tonic-gate  */
1840Sstevel@tonic-gate static char current_runlevel = '\0';
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate /* Number of single user threads currently running */
1870Sstevel@tonic-gate static pthread_mutex_t single_user_thread_lock;
1880Sstevel@tonic-gate static int single_user_thread_count = 0;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate /* Statistics for dependency cycle-checking */
1910Sstevel@tonic-gate static u_longlong_t dep_inserts = 0;
1920Sstevel@tonic-gate static u_longlong_t dep_cycle_ns = 0;
1930Sstevel@tonic-gate static u_longlong_t dep_insert_ns = 0;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate static const char * const emsg_invalid_restarter =
1971958Slianep 	"Transitioning %s to maintenance, restarter FMRI %s is invalid "
1981958Slianep 	"(see 'svcs -xv' for details).\n";
1990Sstevel@tonic-gate static const char * const console_login_fmri = CONSOLE_LOGIN_FMRI;
2000Sstevel@tonic-gate static const char * const single_user_fmri = SCF_MILESTONE_SINGLE_USER;
2010Sstevel@tonic-gate static const char * const multi_user_fmri = SCF_MILESTONE_MULTI_USER;
2020Sstevel@tonic-gate static const char * const multi_user_svr_fmri = SCF_MILESTONE_MULTI_USER_SERVER;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate  * These services define the system being "up".  If none of them can come
2070Sstevel@tonic-gate  * online, then we will run sulogin on the console.  Note that the install ones
2080Sstevel@tonic-gate  * are for the miniroot and when installing CDs after the first.  can_come_up()
2090Sstevel@tonic-gate  * does the decision making, and an sulogin_thread() runs sulogin, which can be
2100Sstevel@tonic-gate  * started by dgraph_set_instance_state() or single_user_thread().
2110Sstevel@tonic-gate  *
2120Sstevel@tonic-gate  * NOTE: can_come_up() relies on SCF_MILESTONE_SINGLE_USER being the first
2130Sstevel@tonic-gate  * entry, which is only used when booting_to_single_user (boot -s) is set.
2140Sstevel@tonic-gate  * This is because when doing a "boot -s", sulogin is started from specials.c
2150Sstevel@tonic-gate  * after milestone/single-user comes online, for backwards compatibility.
2160Sstevel@tonic-gate  * In this case, SCF_MILESTONE_SINGLE_USER needs to be part of up_svcs
2170Sstevel@tonic-gate  * to ensure sulogin will be spawned if milestone/single-user cannot be reached.
2180Sstevel@tonic-gate  */
2190Sstevel@tonic-gate static const char * const up_svcs[] = {
2200Sstevel@tonic-gate 	SCF_MILESTONE_SINGLE_USER,
2210Sstevel@tonic-gate 	CONSOLE_LOGIN_FMRI,
2220Sstevel@tonic-gate 	"svc:/system/install-setup:default",
2230Sstevel@tonic-gate 	"svc:/system/install:default",
2240Sstevel@tonic-gate 	NULL
2250Sstevel@tonic-gate };
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate /* This array must have an element for each non-NULL element of up_svcs[]. */
2280Sstevel@tonic-gate static graph_vertex_t *up_svcs_p[] = { NULL, NULL, NULL, NULL };
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /* These are for seed repository magic.  See can_come_up(). */
2310Sstevel@tonic-gate static const char * const manifest_import =
2320Sstevel@tonic-gate 	"svc:/system/manifest-import:default";
2330Sstevel@tonic-gate static graph_vertex_t *manifest_import_p = NULL;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate static char target_milestone_as_runlevel(void);
2370Sstevel@tonic-gate static void graph_runlevel_changed(char rl, int online);
2380Sstevel@tonic-gate static int dgraph_set_milestone(const char *, scf_handle_t *, boolean_t);
2390Sstevel@tonic-gate static boolean_t should_be_in_subgraph(graph_vertex_t *v);
2407630SRenaud.Manus@Sun.COM static int mark_subtree(graph_edge_t *, void *);
2417630SRenaud.Manus@Sun.COM static boolean_t insubtree_dependents_down(graph_vertex_t *);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate  * graph_vertex_compare()
2450Sstevel@tonic-gate  *	This function can compare either int *id or * graph_vertex_t *gv
2460Sstevel@tonic-gate  *	values, as the vertex id is always the first element of a
2470Sstevel@tonic-gate  *	graph_vertex structure.
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate /* ARGSUSED */
2500Sstevel@tonic-gate static int
2510Sstevel@tonic-gate graph_vertex_compare(const void *lc_arg, const void *rc_arg, void *private)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate 	int lc_id = ((const graph_vertex_t *)lc_arg)->gv_id;
2540Sstevel@tonic-gate 	int rc_id = *(int *)rc_arg;
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	if (lc_id > rc_id)
2570Sstevel@tonic-gate 		return (1);
2580Sstevel@tonic-gate 	if (lc_id < rc_id)
2590Sstevel@tonic-gate 		return (-1);
2600Sstevel@tonic-gate 	return (0);
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate void
2640Sstevel@tonic-gate graph_init()
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate 	graph_edge_pool = startd_list_pool_create("graph_edges",
2670Sstevel@tonic-gate 	    sizeof (graph_edge_t), offsetof(graph_edge_t, ge_link), NULL,
2680Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG);
2690Sstevel@tonic-gate 	assert(graph_edge_pool != NULL);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	graph_vertex_pool = startd_list_pool_create("graph_vertices",
2720Sstevel@tonic-gate 	    sizeof (graph_vertex_t), offsetof(graph_vertex_t, gv_link),
2730Sstevel@tonic-gate 	    graph_vertex_compare, UU_LIST_POOL_DEBUG);
2740Sstevel@tonic-gate 	assert(graph_vertex_pool != NULL);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	(void) pthread_mutex_init(&dgraph_lock, &mutex_attrs);
2770Sstevel@tonic-gate 	(void) pthread_mutex_init(&single_user_thread_lock, &mutex_attrs);
2780Sstevel@tonic-gate 	dgraph = startd_list_create(graph_vertex_pool, NULL, UU_LIST_SORTED);
2790Sstevel@tonic-gate 	assert(dgraph != NULL);
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	if (!st->st_initial)
2820Sstevel@tonic-gate 		current_runlevel = utmpx_get_runlevel();
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Initialized graph\n");
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate static graph_vertex_t *
2880Sstevel@tonic-gate vertex_get_by_name(const char *name)
2890Sstevel@tonic-gate {
2900Sstevel@tonic-gate 	int id;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	id = dict_lookup_byname(name);
2950Sstevel@tonic-gate 	if (id == -1)
2960Sstevel@tonic-gate 		return (NULL);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	return (uu_list_find(dgraph, &id, NULL, NULL));
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate static graph_vertex_t *
3020Sstevel@tonic-gate vertex_get_by_id(int id)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	if (id == -1)
3070Sstevel@tonic-gate 		return (NULL);
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	return (uu_list_find(dgraph, &id, NULL, NULL));
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate  * Creates a new vertex with the given name, adds it to the graph, and returns
3140Sstevel@tonic-gate  * a pointer to it.  The graph lock must be held by this thread on entry.
3150Sstevel@tonic-gate  */
3160Sstevel@tonic-gate static graph_vertex_t *
3170Sstevel@tonic-gate graph_add_vertex(const char *name)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	int id;
3200Sstevel@tonic-gate 	graph_vertex_t *v;
3210Sstevel@tonic-gate 	void *p;
3220Sstevel@tonic-gate 	uu_list_index_t idx;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	id = dict_insert(name);
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	v = startd_zalloc(sizeof (*v));
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	v->gv_id = id;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	v->gv_name = startd_alloc(strlen(name) + 1);
3330Sstevel@tonic-gate 	(void) strcpy(v->gv_name, name);
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	v->gv_dependencies = startd_list_create(graph_edge_pool, v, 0);
3360Sstevel@tonic-gate 	v->gv_dependents = startd_list_create(graph_edge_pool, v, 0);
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	p = uu_list_find(dgraph, &id, NULL, &idx);
3390Sstevel@tonic-gate 	assert(p == NULL);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	uu_list_node_init(v, &v->gv_link, graph_vertex_pool);
3420Sstevel@tonic-gate 	uu_list_insert(dgraph, v, idx);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	return (v);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate /*
3480Sstevel@tonic-gate  * Removes v from the graph and frees it.  The graph should be locked by this
3490Sstevel@tonic-gate  * thread, and v should have no edges associated with it.
3500Sstevel@tonic-gate  */
3510Sstevel@tonic-gate static void
3520Sstevel@tonic-gate graph_remove_vertex(graph_vertex_t *v)
3530Sstevel@tonic-gate {
3540Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependencies) == 0);
3570Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependents) == 0);
3581712Srm88369 	assert(v->gv_refs == 0);
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	startd_free(v->gv_name, strlen(v->gv_name) + 1);
3610Sstevel@tonic-gate 	uu_list_destroy(v->gv_dependencies);
3620Sstevel@tonic-gate 	uu_list_destroy(v->gv_dependents);
3630Sstevel@tonic-gate 	uu_list_remove(dgraph, v);
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	startd_free(v, sizeof (graph_vertex_t));
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate static void
3690Sstevel@tonic-gate graph_add_edge(graph_vertex_t *fv, graph_vertex_t *tv)
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate 	graph_edge_t *e, *re;
3720Sstevel@tonic-gate 	int r;
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	e = startd_alloc(sizeof (graph_edge_t));
3770Sstevel@tonic-gate 	re = startd_alloc(sizeof (graph_edge_t));
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	e->ge_parent = fv;
3800Sstevel@tonic-gate 	e->ge_vertex = tv;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	re->ge_parent = tv;
3830Sstevel@tonic-gate 	re->ge_vertex = fv;
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	uu_list_node_init(e, &e->ge_link, graph_edge_pool);
3860Sstevel@tonic-gate 	r = uu_list_insert_before(fv->gv_dependencies, NULL, e);
3870Sstevel@tonic-gate 	assert(r == 0);
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	uu_list_node_init(re, &re->ge_link, graph_edge_pool);
3900Sstevel@tonic-gate 	r = uu_list_insert_before(tv->gv_dependents, NULL, re);
3910Sstevel@tonic-gate 	assert(r == 0);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate static void
3950Sstevel@tonic-gate graph_remove_edge(graph_vertex_t *v, graph_vertex_t *dv)
3960Sstevel@tonic-gate {
3970Sstevel@tonic-gate 	graph_edge_t *e;
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
4000Sstevel@tonic-gate 	    e != NULL;
4010Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
4020Sstevel@tonic-gate 		if (e->ge_vertex == dv) {
4030Sstevel@tonic-gate 			uu_list_remove(v->gv_dependencies, e);
4040Sstevel@tonic-gate 			startd_free(e, sizeof (graph_edge_t));
4050Sstevel@tonic-gate 			break;
4060Sstevel@tonic-gate 		}
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	for (e = uu_list_first(dv->gv_dependents);
4100Sstevel@tonic-gate 	    e != NULL;
4110Sstevel@tonic-gate 	    e = uu_list_next(dv->gv_dependents, e)) {
4120Sstevel@tonic-gate 		if (e->ge_vertex == v) {
4130Sstevel@tonic-gate 			uu_list_remove(dv->gv_dependents, e);
4140Sstevel@tonic-gate 			startd_free(e, sizeof (graph_edge_t));
4150Sstevel@tonic-gate 			break;
4160Sstevel@tonic-gate 		}
4170Sstevel@tonic-gate 	}
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate static void
4211712Srm88369 remove_inst_vertex(graph_vertex_t *v)
4221712Srm88369 {
4231712Srm88369 	graph_edge_t *e;
4241712Srm88369 	graph_vertex_t *sv;
4251712Srm88369 	int i;
4261712Srm88369 
4271712Srm88369 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
4281712Srm88369 	assert(uu_list_numnodes(v->gv_dependents) == 1);
4291712Srm88369 	assert(uu_list_numnodes(v->gv_dependencies) == 0);
4301712Srm88369 	assert(v->gv_refs == 0);
4311712Srm88369 	assert((v->gv_flags & GV_CONFIGURED) == 0);
4321712Srm88369 
4331712Srm88369 	e = uu_list_first(v->gv_dependents);
4341712Srm88369 	sv = e->ge_vertex;
4351712Srm88369 	graph_remove_edge(sv, v);
4361712Srm88369 
4371712Srm88369 	for (i = 0; up_svcs[i] != NULL; ++i) {
4381712Srm88369 		if (up_svcs_p[i] == v)
4391712Srm88369 			up_svcs_p[i] = NULL;
4401712Srm88369 	}
4411712Srm88369 
4421712Srm88369 	if (manifest_import_p == v)
4431712Srm88369 		manifest_import_p = NULL;
4441712Srm88369 
4451712Srm88369 	graph_remove_vertex(v);
4461712Srm88369 
4471712Srm88369 	if (uu_list_numnodes(sv->gv_dependencies) == 0 &&
4481712Srm88369 	    uu_list_numnodes(sv->gv_dependents) == 0 &&
4491712Srm88369 	    sv->gv_refs == 0)
4501712Srm88369 		graph_remove_vertex(sv);
4511712Srm88369 }
4521712Srm88369 
4531712Srm88369 static void
4540Sstevel@tonic-gate graph_walk_dependents(graph_vertex_t *v, void (*func)(graph_vertex_t *, void *),
4550Sstevel@tonic-gate     void *arg)
4560Sstevel@tonic-gate {
4570Sstevel@tonic-gate 	graph_edge_t *e;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
4600Sstevel@tonic-gate 	    e != NULL;
4610Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e))
4620Sstevel@tonic-gate 		func(e->ge_vertex, arg);
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate static void
4660Sstevel@tonic-gate graph_walk_dependencies(graph_vertex_t *v, void (*func)(graph_vertex_t *,
4670Sstevel@tonic-gate 	void *), void *arg)
4680Sstevel@tonic-gate {
4690Sstevel@tonic-gate 	graph_edge_t *e;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
4740Sstevel@tonic-gate 	    e != NULL;
4750Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 		func(e->ge_vertex, arg);
4780Sstevel@tonic-gate 	}
4790Sstevel@tonic-gate }
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate /*
4820Sstevel@tonic-gate  * Generic graph walking function.
4830Sstevel@tonic-gate  *
4840Sstevel@tonic-gate  * Given a vertex, this function will walk either dependencies
4850Sstevel@tonic-gate  * (WALK_DEPENDENCIES) or dependents (WALK_DEPENDENTS) of a vertex recursively
4860Sstevel@tonic-gate  * for the entire graph.  It will avoid cycles and never visit the same vertex
4870Sstevel@tonic-gate  * twice.
4880Sstevel@tonic-gate  *
4890Sstevel@tonic-gate  * We avoid traversing exclusion dependencies, because they are allowed to
4900Sstevel@tonic-gate  * create cycles in the graph.  When propagating satisfiability, there is no
4910Sstevel@tonic-gate  * need to walk exclusion dependencies because exclude_all_satisfied() doesn't
4920Sstevel@tonic-gate  * test for satisfiability.
4930Sstevel@tonic-gate  *
4940Sstevel@tonic-gate  * The walker takes two callbacks.  The first is called before examining the
4950Sstevel@tonic-gate  * dependents of each vertex.  The second is called on each vertex after
4960Sstevel@tonic-gate  * examining its dependents.  This allows is_path_to() to construct a path only
4970Sstevel@tonic-gate  * after the target vertex has been found.
4980Sstevel@tonic-gate  */
4990Sstevel@tonic-gate typedef enum {
5000Sstevel@tonic-gate 	WALK_DEPENDENTS,
5010Sstevel@tonic-gate 	WALK_DEPENDENCIES
5020Sstevel@tonic-gate } graph_walk_dir_t;
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate typedef int (*graph_walk_cb_t)(graph_vertex_t *, void *);
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate typedef struct graph_walk_info {
5070Sstevel@tonic-gate 	graph_walk_dir_t 	gi_dir;
5080Sstevel@tonic-gate 	uchar_t			*gi_visited;	/* vertex bitmap */
5090Sstevel@tonic-gate 	int			(*gi_pre)(graph_vertex_t *, void *);
5100Sstevel@tonic-gate 	void			(*gi_post)(graph_vertex_t *, void *);
5110Sstevel@tonic-gate 	void			*gi_arg;	/* callback arg */
5120Sstevel@tonic-gate 	int			gi_ret;		/* return value */
5130Sstevel@tonic-gate } graph_walk_info_t;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate static int
5160Sstevel@tonic-gate graph_walk_recurse(graph_edge_t *e, graph_walk_info_t *gip)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	uu_list_t *list;
5190Sstevel@tonic-gate 	int r;
5200Sstevel@tonic-gate 	graph_vertex_t *v = e->ge_vertex;
5210Sstevel@tonic-gate 	int i;
5220Sstevel@tonic-gate 	uint_t b;
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	i = v->gv_id / 8;
5250Sstevel@tonic-gate 	b = 1 << (v->gv_id % 8);
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	/*
5280Sstevel@tonic-gate 	 * Check to see if we've visited this vertex already.
5290Sstevel@tonic-gate 	 */
5300Sstevel@tonic-gate 	if (gip->gi_visited[i] & b)
5310Sstevel@tonic-gate 		return (UU_WALK_NEXT);
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	gip->gi_visited[i] |= b;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	/*
5360Sstevel@tonic-gate 	 * Don't follow exclusions.
5370Sstevel@tonic-gate 	 */
5380Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
5390Sstevel@tonic-gate 		return (UU_WALK_NEXT);
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	/*
5420Sstevel@tonic-gate 	 * Call pre-visit callback.  If this doesn't terminate the walk,
5430Sstevel@tonic-gate 	 * continue search.
5440Sstevel@tonic-gate 	 */
5450Sstevel@tonic-gate 	if ((gip->gi_ret = gip->gi_pre(v, gip->gi_arg)) == UU_WALK_NEXT) {
5460Sstevel@tonic-gate 		/*
5470Sstevel@tonic-gate 		 * Recurse using appropriate list.
5480Sstevel@tonic-gate 		 */
5490Sstevel@tonic-gate 		if (gip->gi_dir == WALK_DEPENDENTS)
5500Sstevel@tonic-gate 			list = v->gv_dependents;
5510Sstevel@tonic-gate 		else
5520Sstevel@tonic-gate 			list = v->gv_dependencies;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 		r = uu_list_walk(list, (uu_walk_fn_t *)graph_walk_recurse,
5550Sstevel@tonic-gate 		    gip, 0);
5560Sstevel@tonic-gate 		assert(r == 0);
5570Sstevel@tonic-gate 	}
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	/*
5600Sstevel@tonic-gate 	 * Callbacks must return either UU_WALK_NEXT or UU_WALK_DONE.
5610Sstevel@tonic-gate 	 */
5620Sstevel@tonic-gate 	assert(gip->gi_ret == UU_WALK_NEXT || gip->gi_ret == UU_WALK_DONE);
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	/*
5650Sstevel@tonic-gate 	 * If given a post-callback, call the function for every vertex.
5660Sstevel@tonic-gate 	 */
5670Sstevel@tonic-gate 	if (gip->gi_post != NULL)
5680Sstevel@tonic-gate 		(void) gip->gi_post(v, gip->gi_arg);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	/*
5710Sstevel@tonic-gate 	 * Preserve the callback's return value.  If the callback returns
5720Sstevel@tonic-gate 	 * UU_WALK_DONE, then we propagate that to the caller in order to
5730Sstevel@tonic-gate 	 * terminate the walk.
5740Sstevel@tonic-gate 	 */
5750Sstevel@tonic-gate 	return (gip->gi_ret);
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate static void
5790Sstevel@tonic-gate graph_walk(graph_vertex_t *v, graph_walk_dir_t dir,
5800Sstevel@tonic-gate     int (*pre)(graph_vertex_t *, void *),
5810Sstevel@tonic-gate     void (*post)(graph_vertex_t *, void *), void *arg)
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate 	graph_walk_info_t gi;
5840Sstevel@tonic-gate 	graph_edge_t fake;
5850Sstevel@tonic-gate 	size_t sz = dictionary->dict_new_id / 8 + 1;
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	gi.gi_visited = startd_zalloc(sz);
5880Sstevel@tonic-gate 	gi.gi_pre = pre;
5890Sstevel@tonic-gate 	gi.gi_post = post;
5900Sstevel@tonic-gate 	gi.gi_arg = arg;
5910Sstevel@tonic-gate 	gi.gi_dir = dir;
5920Sstevel@tonic-gate 	gi.gi_ret = 0;
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	/*
5950Sstevel@tonic-gate 	 * Fake up an edge for the first iteration
5960Sstevel@tonic-gate 	 */
5970Sstevel@tonic-gate 	fake.ge_vertex = v;
5980Sstevel@tonic-gate 	(void) graph_walk_recurse(&fake, &gi);
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	startd_free(gi.gi_visited, sz);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate typedef struct child_search {
6040Sstevel@tonic-gate 	int	id;		/* id of vertex to look for */
6050Sstevel@tonic-gate 	uint_t	depth;		/* recursion depth */
6060Sstevel@tonic-gate 	/*
6070Sstevel@tonic-gate 	 * While the vertex is not found, path is NULL.  After the search, if
6080Sstevel@tonic-gate 	 * the vertex was found then path should point to a -1-terminated
6090Sstevel@tonic-gate 	 * array of vertex id's which constitute the path to the vertex.
6100Sstevel@tonic-gate 	 */
6110Sstevel@tonic-gate 	int	*path;
6120Sstevel@tonic-gate } child_search_t;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate static int
6150Sstevel@tonic-gate child_pre(graph_vertex_t *v, void *arg)
6160Sstevel@tonic-gate {
6170Sstevel@tonic-gate 	child_search_t *cs = arg;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	cs->depth++;
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	if (v->gv_id == cs->id) {
6220Sstevel@tonic-gate 		cs->path = startd_alloc((cs->depth + 1) * sizeof (int));
6230Sstevel@tonic-gate 		cs->path[cs->depth] = -1;
6240Sstevel@tonic-gate 		return (UU_WALK_DONE);
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	return (UU_WALK_NEXT);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate static void
6310Sstevel@tonic-gate child_post(graph_vertex_t *v, void *arg)
6320Sstevel@tonic-gate {
6330Sstevel@tonic-gate 	child_search_t *cs = arg;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	cs->depth--;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	if (cs->path != NULL)
6380Sstevel@tonic-gate 		cs->path[cs->depth] = v->gv_id;
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate /*
6420Sstevel@tonic-gate  * Look for a path from from to to.  If one exists, returns a pointer to
6430Sstevel@tonic-gate  * a NULL-terminated array of pointers to the vertices along the path.  If
6440Sstevel@tonic-gate  * there is no path, returns NULL.
6450Sstevel@tonic-gate  */
6460Sstevel@tonic-gate static int *
6470Sstevel@tonic-gate is_path_to(graph_vertex_t *from, graph_vertex_t *to)
6480Sstevel@tonic-gate {
6490Sstevel@tonic-gate 	child_search_t cs;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	cs.id = to->gv_id;
6520Sstevel@tonic-gate 	cs.depth = 0;
6530Sstevel@tonic-gate 	cs.path = NULL;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	graph_walk(from, WALK_DEPENDENCIES, child_pre, child_post, &cs);
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	return (cs.path);
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate /*
6610Sstevel@tonic-gate  * Given an array of int's as returned by is_path_to, allocates a string of
6620Sstevel@tonic-gate  * their names joined by newlines.  Returns the size of the allocated buffer
6630Sstevel@tonic-gate  * in *sz and frees path.
6640Sstevel@tonic-gate  */
6650Sstevel@tonic-gate static void
6660Sstevel@tonic-gate path_to_str(int *path, char **cpp, size_t *sz)
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate 	int i;
6690Sstevel@tonic-gate 	graph_vertex_t *v;
6700Sstevel@tonic-gate 	size_t allocd, new_allocd;
6710Sstevel@tonic-gate 	char *new, *name;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
6740Sstevel@tonic-gate 	assert(path[0] != -1);
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	allocd = 1;
6770Sstevel@tonic-gate 	*cpp = startd_alloc(1);
6780Sstevel@tonic-gate 	(*cpp)[0] = '\0';
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	for (i = 0; path[i] != -1; ++i) {
6810Sstevel@tonic-gate 		name = NULL;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 		v = vertex_get_by_id(path[i]);
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 		if (v == NULL)
6860Sstevel@tonic-gate 			name = "<deleted>";
6870Sstevel@tonic-gate 		else if (v->gv_type == GVT_INST || v->gv_type == GVT_SVC)
6880Sstevel@tonic-gate 			name = v->gv_name;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 		if (name != NULL) {
6910Sstevel@tonic-gate 			new_allocd = allocd + strlen(name) + 1;
6920Sstevel@tonic-gate 			new = startd_alloc(new_allocd);
6930Sstevel@tonic-gate 			(void) strcpy(new, *cpp);
6940Sstevel@tonic-gate 			(void) strcat(new, name);
6950Sstevel@tonic-gate 			(void) strcat(new, "\n");
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 			startd_free(*cpp, allocd);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 			*cpp = new;
7000Sstevel@tonic-gate 			allocd = new_allocd;
7010Sstevel@tonic-gate 		}
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	startd_free(path, sizeof (int) * (i + 1));
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	*sz = allocd;
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate /*
7110Sstevel@tonic-gate  * This function along with run_sulogin() implements an exclusion relationship
7120Sstevel@tonic-gate  * between system/console-login and sulogin.  run_sulogin() will fail if
7130Sstevel@tonic-gate  * system/console-login is online, and the graph engine should call
7140Sstevel@tonic-gate  * graph_clogin_start() to bring system/console-login online, which defers the
7150Sstevel@tonic-gate  * start if sulogin is running.
7160Sstevel@tonic-gate  */
7170Sstevel@tonic-gate static void
7180Sstevel@tonic-gate graph_clogin_start(graph_vertex_t *v)
7190Sstevel@tonic-gate {
7200Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	if (sulogin_running)
7230Sstevel@tonic-gate 		console_login_ready = B_TRUE;
7240Sstevel@tonic-gate 	else
7250Sstevel@tonic-gate 		vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate static void
7290Sstevel@tonic-gate graph_su_start(graph_vertex_t *v)
7300Sstevel@tonic-gate {
7310Sstevel@tonic-gate 	/*
7320Sstevel@tonic-gate 	 * /etc/inittab used to have the initial /sbin/rcS as a 'sysinit'
7330Sstevel@tonic-gate 	 * entry with a runlevel of 'S', before jumping to the final
7340Sstevel@tonic-gate 	 * target runlevel (as set in initdefault).  We mimic that legacy
7350Sstevel@tonic-gate 	 * behavior here.
7360Sstevel@tonic-gate 	 */
7370Sstevel@tonic-gate 	utmpx_set_runlevel('S', '0', B_FALSE);
7380Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
7390Sstevel@tonic-gate }
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate static void
7420Sstevel@tonic-gate graph_post_su_online(void)
7430Sstevel@tonic-gate {
7440Sstevel@tonic-gate 	graph_runlevel_changed('S', 1);
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate static void
7480Sstevel@tonic-gate graph_post_su_disable(void)
7490Sstevel@tonic-gate {
7500Sstevel@tonic-gate 	graph_runlevel_changed('S', 0);
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate static void
7540Sstevel@tonic-gate graph_post_mu_online(void)
7550Sstevel@tonic-gate {
7560Sstevel@tonic-gate 	graph_runlevel_changed('2', 1);
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate static void
7600Sstevel@tonic-gate graph_post_mu_disable(void)
7610Sstevel@tonic-gate {
7620Sstevel@tonic-gate 	graph_runlevel_changed('2', 0);
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate static void
7660Sstevel@tonic-gate graph_post_mus_online(void)
7670Sstevel@tonic-gate {
7680Sstevel@tonic-gate 	graph_runlevel_changed('3', 1);
7690Sstevel@tonic-gate }
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate static void
7720Sstevel@tonic-gate graph_post_mus_disable(void)
7730Sstevel@tonic-gate {
7740Sstevel@tonic-gate 	graph_runlevel_changed('3', 0);
7750Sstevel@tonic-gate }
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate static struct special_vertex_info {
7780Sstevel@tonic-gate 	const char	*name;
7790Sstevel@tonic-gate 	void		(*start_f)(graph_vertex_t *);
7800Sstevel@tonic-gate 	void		(*post_online_f)(void);
7810Sstevel@tonic-gate 	void		(*post_disable_f)(void);
7820Sstevel@tonic-gate } special_vertices[] = {
7830Sstevel@tonic-gate 	{ CONSOLE_LOGIN_FMRI, graph_clogin_start, NULL, NULL },
7840Sstevel@tonic-gate 	{ SCF_MILESTONE_SINGLE_USER, graph_su_start,
7850Sstevel@tonic-gate 	    graph_post_su_online, graph_post_su_disable },
7860Sstevel@tonic-gate 	{ SCF_MILESTONE_MULTI_USER, NULL,
7870Sstevel@tonic-gate 	    graph_post_mu_online, graph_post_mu_disable },
7880Sstevel@tonic-gate 	{ SCF_MILESTONE_MULTI_USER_SERVER, NULL,
7890Sstevel@tonic-gate 	    graph_post_mus_online, graph_post_mus_disable },
7900Sstevel@tonic-gate 	{ NULL },
7910Sstevel@tonic-gate };
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate void
7950Sstevel@tonic-gate vertex_send_event(graph_vertex_t *v, restarter_event_type_t e)
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate 	switch (e) {
7980Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADD_INSTANCE:
7990Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_UNINIT);
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 		MUTEX_LOCK(&st->st_load_lock);
8020Sstevel@tonic-gate 		st->st_load_instances++;
8030Sstevel@tonic-gate 		MUTEX_UNLOCK(&st->st_load_lock);
8040Sstevel@tonic-gate 		break;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ENABLE:
8070Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Enabling %s.\n", v->gv_name);
8080Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_UNINIT ||
8090Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_DISABLED ||
8100Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_MAINT);
8110Sstevel@tonic-gate 		break;
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_DISABLE:
8140Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
8150Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Disabling %s.\n", v->gv_name);
8160Sstevel@tonic-gate 		assert(v->gv_state != RESTARTER_STATE_DISABLED);
8170Sstevel@tonic-gate 		break;
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_STOP:
8200Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Stopping %s.\n", v->gv_name);
8210Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_DEGRADED ||
8220Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_ONLINE);
8230Sstevel@tonic-gate 		break;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_START:
8260Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Starting %s.\n", v->gv_name);
8270Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_OFFLINE);
8280Sstevel@tonic-gate 		break;
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE:
8310Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_DEGRADED:
8320Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_REFRESH:
8330Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_RESTART:
8340Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF:
8350Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
8360Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE:
8370Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
8380Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
8390Sstevel@tonic-gate 		break;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	default:
8420Sstevel@tonic-gate #ifndef NDEBUG
8430Sstevel@tonic-gate 		uu_warn("%s:%d: Bad event %d.\n", __FILE__, __LINE__, e);
8440Sstevel@tonic-gate #endif
8450Sstevel@tonic-gate 		abort();
8460Sstevel@tonic-gate 	}
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	restarter_protocol_send_event(v->gv_name, v->gv_restarter_channel, e);
8490Sstevel@tonic-gate }
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate static void
8520Sstevel@tonic-gate graph_unset_restarter(graph_vertex_t *v)
8530Sstevel@tonic-gate {
8540Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
8550Sstevel@tonic-gate 	assert(v->gv_flags & GV_CONFIGURED);
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_REMOVE_INSTANCE);
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	if (v->gv_restarter_id != -1) {
8600Sstevel@tonic-gate 		graph_vertex_t *rv;
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 		rv = vertex_get_by_id(v->gv_restarter_id);
8630Sstevel@tonic-gate 		graph_remove_edge(v, rv);
8640Sstevel@tonic-gate 	}
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	v->gv_restarter_id = -1;
8670Sstevel@tonic-gate 	v->gv_restarter_channel = NULL;
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate 
8701712Srm88369 /*
8711712Srm88369  * Return VERTEX_REMOVED when the vertex passed in argument is deleted from the
8721712Srm88369  * dgraph otherwise return VERTEX_INUSE.
8731712Srm88369  */
8741712Srm88369 static int
8751712Srm88369 free_if_unrefed(graph_vertex_t *v)
8761712Srm88369 {
8771712Srm88369 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
8781712Srm88369 
8791712Srm88369 	if (v->gv_refs > 0)
8801712Srm88369 		return (VERTEX_INUSE);
8811712Srm88369 
8821712Srm88369 	if (v->gv_type == GVT_SVC &&
8831712Srm88369 	    uu_list_numnodes(v->gv_dependents) == 0 &&
8841712Srm88369 	    uu_list_numnodes(v->gv_dependencies) == 0) {
8851712Srm88369 		graph_remove_vertex(v);
8861712Srm88369 		return (VERTEX_REMOVED);
8871712Srm88369 	} else if (v->gv_type == GVT_INST &&
8881712Srm88369 	    (v->gv_flags & GV_CONFIGURED) == 0 &&
8891712Srm88369 	    uu_list_numnodes(v->gv_dependents) == 1 &&
8901712Srm88369 	    uu_list_numnodes(v->gv_dependencies) == 0) {
8911712Srm88369 		remove_inst_vertex(v);
8921712Srm88369 		return (VERTEX_REMOVED);
8931712Srm88369 	}
8941712Srm88369 
8951712Srm88369 	return (VERTEX_INUSE);
8961712Srm88369 }
8971712Srm88369 
8980Sstevel@tonic-gate static void
8990Sstevel@tonic-gate delete_depgroup(graph_vertex_t *v)
9000Sstevel@tonic-gate {
9010Sstevel@tonic-gate 	graph_edge_t *e;
9020Sstevel@tonic-gate 	graph_vertex_t *dv;
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
9050Sstevel@tonic-gate 	assert(v->gv_type == GVT_GROUP);
9060Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependents) == 0);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	while ((e = uu_list_first(v->gv_dependencies)) != NULL) {
9090Sstevel@tonic-gate 		dv = e->ge_vertex;
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 		graph_remove_edge(v, dv);
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 		switch (dv->gv_type) {
9140Sstevel@tonic-gate 		case GVT_INST:		/* instance dependency */
9150Sstevel@tonic-gate 		case GVT_SVC:		/* service dependency */
9161712Srm88369 			(void) free_if_unrefed(dv);
9170Sstevel@tonic-gate 			break;
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 		case GVT_FILE:		/* file dependency */
9200Sstevel@tonic-gate 			assert(uu_list_numnodes(dv->gv_dependencies) == 0);
9210Sstevel@tonic-gate 			if (uu_list_numnodes(dv->gv_dependents) == 0)
9220Sstevel@tonic-gate 				graph_remove_vertex(dv);
9230Sstevel@tonic-gate 			break;
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 		default:
9260Sstevel@tonic-gate #ifndef NDEBUG
9270Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected node type %d", __FILE__,
9280Sstevel@tonic-gate 			    __LINE__, dv->gv_type);
9290Sstevel@tonic-gate #endif
9300Sstevel@tonic-gate 			abort();
9310Sstevel@tonic-gate 		}
9320Sstevel@tonic-gate 	}
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	graph_remove_vertex(v);
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate static int
9380Sstevel@tonic-gate delete_instance_deps_cb(graph_edge_t *e, void **ptrs)
9390Sstevel@tonic-gate {
9400Sstevel@tonic-gate 	graph_vertex_t *v = ptrs[0];
9410Sstevel@tonic-gate 	boolean_t delete_restarter_dep = (boolean_t)ptrs[1];
9420Sstevel@tonic-gate 	graph_vertex_t *dv;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	dv = e->ge_vertex;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	/*
9470Sstevel@tonic-gate 	 * We have four possibilities here:
9480Sstevel@tonic-gate 	 *   - GVT_INST: restarter
9490Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_INST: instance dependency
9500Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_SVC - GV_INST: service dependency
9510Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_FILE: file dependency
9520Sstevel@tonic-gate 	 */
9530Sstevel@tonic-gate 	switch (dv->gv_type) {
9540Sstevel@tonic-gate 	case GVT_INST:	/* restarter */
9550Sstevel@tonic-gate 		assert(dv->gv_id == v->gv_restarter_id);
9560Sstevel@tonic-gate 		if (delete_restarter_dep)
9570Sstevel@tonic-gate 			graph_remove_edge(v, dv);
9580Sstevel@tonic-gate 		break;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	case GVT_GROUP:	/* pg dependency */
9610Sstevel@tonic-gate 		graph_remove_edge(v, dv);
9620Sstevel@tonic-gate 		delete_depgroup(dv);
9630Sstevel@tonic-gate 		break;
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	case GVT_FILE:
9660Sstevel@tonic-gate 		/* These are currently not direct dependencies */
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	default:
9690Sstevel@tonic-gate #ifndef NDEBUG
9700Sstevel@tonic-gate 		uu_warn("%s:%d: Bad vertex type %d.\n", __FILE__, __LINE__,
9710Sstevel@tonic-gate 		    dv->gv_type);
9720Sstevel@tonic-gate #endif
9730Sstevel@tonic-gate 		abort();
9740Sstevel@tonic-gate 	}
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	return (UU_WALK_NEXT);
9770Sstevel@tonic-gate }
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate static void
9800Sstevel@tonic-gate delete_instance_dependencies(graph_vertex_t *v, boolean_t delete_restarter_dep)
9810Sstevel@tonic-gate {
9820Sstevel@tonic-gate 	void *ptrs[2];
9830Sstevel@tonic-gate 	int r;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
9860Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	ptrs[0] = v;
9890Sstevel@tonic-gate 	ptrs[1] = (void *)delete_restarter_dep;
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate 	r = uu_list_walk(v->gv_dependencies,
9920Sstevel@tonic-gate 	    (uu_walk_fn_t *)delete_instance_deps_cb, &ptrs, UU_WALK_ROBUST);
9930Sstevel@tonic-gate 	assert(r == 0);
9940Sstevel@tonic-gate }
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate /*
9970Sstevel@tonic-gate  * int graph_insert_vertex_unconfigured()
9980Sstevel@tonic-gate  *   Insert a vertex without sending any restarter events. If the vertex
9990Sstevel@tonic-gate  *   already exists or creation is successful, return a pointer to it in *vp.
10000Sstevel@tonic-gate  *
10010Sstevel@tonic-gate  *   If type is not GVT_GROUP, dt can remain unset.
10020Sstevel@tonic-gate  *
10030Sstevel@tonic-gate  *   Returns 0, EEXIST, or EINVAL if the arguments are invalid (i.e., fmri
10040Sstevel@tonic-gate  *   doesn't agree with type, or type doesn't agree with dt).
10050Sstevel@tonic-gate  */
10060Sstevel@tonic-gate static int
10070Sstevel@tonic-gate graph_insert_vertex_unconfigured(const char *fmri, gv_type_t type,
10080Sstevel@tonic-gate     depgroup_type_t dt, restarter_error_t rt, graph_vertex_t **vp)
10090Sstevel@tonic-gate {
10100Sstevel@tonic-gate 	int r;
10110Sstevel@tonic-gate 	int i;
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	switch (type) {
10160Sstevel@tonic-gate 	case GVT_SVC:
10170Sstevel@tonic-gate 	case GVT_INST:
10180Sstevel@tonic-gate 		if (strncmp(fmri, "svc:", sizeof ("svc:") - 1) != 0)
10190Sstevel@tonic-gate 			return (EINVAL);
10200Sstevel@tonic-gate 		break;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	case GVT_FILE:
10230Sstevel@tonic-gate 		if (strncmp(fmri, "file:", sizeof ("file:") - 1) != 0)
10240Sstevel@tonic-gate 			return (EINVAL);
10250Sstevel@tonic-gate 		break;
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	case GVT_GROUP:
10280Sstevel@tonic-gate 		if (dt <= 0 || rt < 0)
10290Sstevel@tonic-gate 			return (EINVAL);
10300Sstevel@tonic-gate 		break;
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	default:
10330Sstevel@tonic-gate #ifndef NDEBUG
10340Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown type %d.\n", __FILE__, __LINE__, type);
10350Sstevel@tonic-gate #endif
10360Sstevel@tonic-gate 		abort();
10370Sstevel@tonic-gate 	}
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	*vp = vertex_get_by_name(fmri);
10400Sstevel@tonic-gate 	if (*vp != NULL)
10410Sstevel@tonic-gate 		return (EEXIST);
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	*vp = graph_add_vertex(fmri);
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	(*vp)->gv_type = type;
10460Sstevel@tonic-gate 	(*vp)->gv_depgroup = dt;
10470Sstevel@tonic-gate 	(*vp)->gv_restart = rt;
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	(*vp)->gv_flags = 0;
10500Sstevel@tonic-gate 	(*vp)->gv_state = RESTARTER_STATE_NONE;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	for (i = 0; special_vertices[i].name != NULL; ++i) {
10530Sstevel@tonic-gate 		if (strcmp(fmri, special_vertices[i].name) == 0) {
10540Sstevel@tonic-gate 			(*vp)->gv_start_f = special_vertices[i].start_f;
10550Sstevel@tonic-gate 			(*vp)->gv_post_online_f =
10560Sstevel@tonic-gate 			    special_vertices[i].post_online_f;
10570Sstevel@tonic-gate 			(*vp)->gv_post_disable_f =
10580Sstevel@tonic-gate 			    special_vertices[i].post_disable_f;
10590Sstevel@tonic-gate 			break;
10600Sstevel@tonic-gate 		}
10610Sstevel@tonic-gate 	}
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	(*vp)->gv_restarter_id = -1;
10640Sstevel@tonic-gate 	(*vp)->gv_restarter_channel = 0;
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	if (type == GVT_INST) {
10670Sstevel@tonic-gate 		char *sfmri;
10680Sstevel@tonic-gate 		graph_vertex_t *sv;
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 		sfmri = inst_fmri_to_svc_fmri(fmri);
10710Sstevel@tonic-gate 		sv = vertex_get_by_name(sfmri);
10720Sstevel@tonic-gate 		if (sv == NULL) {
10730Sstevel@tonic-gate 			r = graph_insert_vertex_unconfigured(sfmri, GVT_SVC, 0,
10740Sstevel@tonic-gate 			    0, &sv);
10750Sstevel@tonic-gate 			assert(r == 0);
10760Sstevel@tonic-gate 		}
10770Sstevel@tonic-gate 		startd_free(sfmri, max_scf_fmri_size);
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 		graph_add_edge(sv, *vp);
10800Sstevel@tonic-gate 	}
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	/*
10830Sstevel@tonic-gate 	 * If this vertex is in the subgraph, mark it as so, for both
10840Sstevel@tonic-gate 	 * GVT_INST and GVT_SERVICE verteces.
10850Sstevel@tonic-gate 	 * A GVT_SERVICE vertex can only be in the subgraph if another instance
10860Sstevel@tonic-gate 	 * depends on it, in which case it's already been added to the graph
10870Sstevel@tonic-gate 	 * and marked as in the subgraph (by refresh_vertex()).  If a
10880Sstevel@tonic-gate 	 * GVT_SERVICE vertex was freshly added (by the code above), it means
10890Sstevel@tonic-gate 	 * that it has no dependents, and cannot be in the subgraph.
10900Sstevel@tonic-gate 	 * Regardless of this, we still check that gv_flags includes
10910Sstevel@tonic-gate 	 * GV_INSUBGRAPH in the event that future behavior causes the above
10920Sstevel@tonic-gate 	 * code to add a GVT_SERVICE vertex which should be in the subgraph.
10930Sstevel@tonic-gate 	 */
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 	(*vp)->gv_flags |= (should_be_in_subgraph(*vp)? GV_INSUBGRAPH : 0);
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	return (0);
10980Sstevel@tonic-gate }
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate /*
11010Sstevel@tonic-gate  * Returns 0 on success or ELOOP if the dependency would create a cycle.
11020Sstevel@tonic-gate  */
11030Sstevel@tonic-gate static int
11040Sstevel@tonic-gate graph_insert_dependency(graph_vertex_t *fv, graph_vertex_t *tv, int **pathp)
11050Sstevel@tonic-gate {
11060Sstevel@tonic-gate 	hrtime_t now;
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	/* cycle detection */
11110Sstevel@tonic-gate 	now = gethrtime();
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	/* Don't follow exclusions. */
11140Sstevel@tonic-gate 	if (!(fv->gv_type == GVT_GROUP &&
11150Sstevel@tonic-gate 	    fv->gv_depgroup == DEPGRP_EXCLUDE_ALL)) {
11160Sstevel@tonic-gate 		*pathp = is_path_to(tv, fv);
11170Sstevel@tonic-gate 		if (*pathp)
11180Sstevel@tonic-gate 			return (ELOOP);
11190Sstevel@tonic-gate 	}
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	dep_cycle_ns += gethrtime() - now;
11220Sstevel@tonic-gate 	++dep_inserts;
11230Sstevel@tonic-gate 	now = gethrtime();
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	graph_add_edge(fv, tv);
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	dep_insert_ns += gethrtime() - now;
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	/* Check if the dependency adds the "to" vertex to the subgraph */
11300Sstevel@tonic-gate 	tv->gv_flags |= (should_be_in_subgraph(tv) ? GV_INSUBGRAPH : 0);
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	return (0);
11330Sstevel@tonic-gate }
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate static int
11360Sstevel@tonic-gate inst_running(graph_vertex_t *v)
11370Sstevel@tonic-gate {
11380Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	if (v->gv_state == RESTARTER_STATE_ONLINE ||
11410Sstevel@tonic-gate 	    v->gv_state == RESTARTER_STATE_DEGRADED)
11420Sstevel@tonic-gate 		return (1);
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	return (0);
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate /*
11480Sstevel@tonic-gate  * The dependency evaluation functions return
11490Sstevel@tonic-gate  *   1 - dependency satisfied
11500Sstevel@tonic-gate  *   0 - dependency unsatisfied
11510Sstevel@tonic-gate  *   -1 - dependency unsatisfiable (without administrator intervention)
11520Sstevel@tonic-gate  *
11530Sstevel@tonic-gate  * The functions also take a boolean satbility argument.  When true, the
11540Sstevel@tonic-gate  * functions may recurse in order to determine satisfiability.
11550Sstevel@tonic-gate  */
11560Sstevel@tonic-gate static int require_any_satisfied(graph_vertex_t *, boolean_t);
11570Sstevel@tonic-gate static int dependency_satisfied(graph_vertex_t *, boolean_t);
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate /*
11600Sstevel@tonic-gate  * A require_all dependency is unsatisfied if any elements are unsatisfied.  It
11610Sstevel@tonic-gate  * is unsatisfiable if any elements are unsatisfiable.
11620Sstevel@tonic-gate  */
11630Sstevel@tonic-gate static int
11640Sstevel@tonic-gate require_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
11650Sstevel@tonic-gate {
11660Sstevel@tonic-gate 	graph_edge_t *edge;
11670Sstevel@tonic-gate 	int i;
11680Sstevel@tonic-gate 	boolean_t any_unsatisfied;
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	if (uu_list_numnodes(groupv->gv_dependencies) == 0)
11710Sstevel@tonic-gate 		return (1);
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	any_unsatisfied = B_FALSE;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
11760Sstevel@tonic-gate 	    edge != NULL;
11770Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
11780Sstevel@tonic-gate 		i = dependency_satisfied(edge->ge_vertex, satbility);
11790Sstevel@tonic-gate 		if (i == 1)
11800Sstevel@tonic-gate 			continue;
11810Sstevel@tonic-gate 
11825680Srm88369 		log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
11830Sstevel@tonic-gate 		    "require_all(%s): %s is unsatisfi%s.\n", groupv->gv_name,
11840Sstevel@tonic-gate 		    edge->ge_vertex->gv_name, i == 0 ? "ed" : "able");
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 		if (!satbility)
11870Sstevel@tonic-gate 			return (0);
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 		if (i == -1)
11900Sstevel@tonic-gate 			return (-1);
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 		any_unsatisfied = B_TRUE;
11930Sstevel@tonic-gate 	}
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	return (any_unsatisfied ? 0 : 1);
11960Sstevel@tonic-gate }
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate /*
11990Sstevel@tonic-gate  * A require_any dependency is satisfied if any element is satisfied.  It is
12000Sstevel@tonic-gate  * satisfiable if any element is satisfiable.
12010Sstevel@tonic-gate  */
12020Sstevel@tonic-gate static int
12030Sstevel@tonic-gate require_any_satisfied(graph_vertex_t *groupv, boolean_t satbility)
12040Sstevel@tonic-gate {
12050Sstevel@tonic-gate 	graph_edge_t *edge;
12060Sstevel@tonic-gate 	int s;
12070Sstevel@tonic-gate 	boolean_t satisfiable;
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	if (uu_list_numnodes(groupv->gv_dependencies) == 0)
12100Sstevel@tonic-gate 		return (1);
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	satisfiable = B_FALSE;
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
12150Sstevel@tonic-gate 	    edge != NULL;
12160Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
12170Sstevel@tonic-gate 		s = dependency_satisfied(edge->ge_vertex, satbility);
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 		if (s == 1)
12200Sstevel@tonic-gate 			return (1);
12210Sstevel@tonic-gate 
12225680Srm88369 		log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
12230Sstevel@tonic-gate 		    "require_any(%s): %s is unsatisfi%s.\n",
12240Sstevel@tonic-gate 		    groupv->gv_name, edge->ge_vertex->gv_name,
12250Sstevel@tonic-gate 		    s == 0 ? "ed" : "able");
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 		if (satbility && s == 0)
12280Sstevel@tonic-gate 			satisfiable = B_TRUE;
12290Sstevel@tonic-gate 	}
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	return (!satbility || satisfiable ? 0 : -1);
12320Sstevel@tonic-gate }
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate /*
12350Sstevel@tonic-gate  * An optional_all dependency only considers elements which are configured,
12360Sstevel@tonic-gate  * enabled, and not in maintenance.  If any are unsatisfied, then the dependency
12370Sstevel@tonic-gate  * is unsatisfied.
12380Sstevel@tonic-gate  *
12390Sstevel@tonic-gate  * Offline dependencies which are waiting for a dependency to come online are
12400Sstevel@tonic-gate  * unsatisfied.  Offline dependences which cannot possibly come online
12410Sstevel@tonic-gate  * (unsatisfiable) are always considered satisfied.
12420Sstevel@tonic-gate  */
12430Sstevel@tonic-gate static int
12440Sstevel@tonic-gate optional_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
12450Sstevel@tonic-gate {
12460Sstevel@tonic-gate 	graph_edge_t *edge;
12470Sstevel@tonic-gate 	graph_vertex_t *v;
12480Sstevel@tonic-gate 	boolean_t any_qualified;
12490Sstevel@tonic-gate 	boolean_t any_unsatisfied;
12500Sstevel@tonic-gate 	int i;
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	any_qualified = B_FALSE;
12530Sstevel@tonic-gate 	any_unsatisfied = B_FALSE;
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
12560Sstevel@tonic-gate 	    edge != NULL;
12570Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
12580Sstevel@tonic-gate 		v = edge->ge_vertex;
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 		switch (v->gv_type) {
12610Sstevel@tonic-gate 		case GVT_INST:
12620Sstevel@tonic-gate 			/* Skip missing or disabled instances */
12630Sstevel@tonic-gate 			if ((v->gv_flags & (GV_CONFIGURED | GV_ENABLED)) !=
12640Sstevel@tonic-gate 			    (GV_CONFIGURED | GV_ENABLED))
12650Sstevel@tonic-gate 				continue;
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 			if (v->gv_state == RESTARTER_STATE_MAINT)
12680Sstevel@tonic-gate 				continue;
12690Sstevel@tonic-gate 
1270*8354SRenaud.Manus@Sun.COM 			if (v->gv_flags & GV_TOOFFLINE)
1271*8354SRenaud.Manus@Sun.COM 				continue;
1272*8354SRenaud.Manus@Sun.COM 
12730Sstevel@tonic-gate 			any_qualified = B_TRUE;
12740Sstevel@tonic-gate 			if (v->gv_state == RESTARTER_STATE_OFFLINE) {
12750Sstevel@tonic-gate 				/*
12760Sstevel@tonic-gate 				 * For offline dependencies, treat unsatisfiable
12770Sstevel@tonic-gate 				 * as satisfied.
12780Sstevel@tonic-gate 				 */
12790Sstevel@tonic-gate 				i = dependency_satisfied(v, B_TRUE);
12800Sstevel@tonic-gate 				if (i == -1)
12810Sstevel@tonic-gate 					i = 1;
12820Sstevel@tonic-gate 			} else if (v->gv_state == RESTARTER_STATE_DISABLED) {
12830Sstevel@tonic-gate 				/*
12840Sstevel@tonic-gate 				 * The service is enabled, but hasn't
12850Sstevel@tonic-gate 				 * transitioned out of disabled yet.  Treat it
12860Sstevel@tonic-gate 				 * as unsatisfied (not unsatisfiable).
12870Sstevel@tonic-gate 				 */
12880Sstevel@tonic-gate 				i = 0;
12890Sstevel@tonic-gate 			} else {
12900Sstevel@tonic-gate 				i = dependency_satisfied(v, satbility);
12910Sstevel@tonic-gate 			}
12920Sstevel@tonic-gate 			break;
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate 		case GVT_FILE:
12950Sstevel@tonic-gate 			any_qualified = B_TRUE;
12960Sstevel@tonic-gate 			i = dependency_satisfied(v, satbility);
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 			break;
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate 		case GVT_SVC: {
13010Sstevel@tonic-gate 			boolean_t svc_any_qualified;
13020Sstevel@tonic-gate 			boolean_t svc_satisfied;
13030Sstevel@tonic-gate 			boolean_t svc_satisfiable;
13040Sstevel@tonic-gate 			graph_vertex_t *v2;
13050Sstevel@tonic-gate 			graph_edge_t *e2;
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 			svc_any_qualified = B_FALSE;
13080Sstevel@tonic-gate 			svc_satisfied = B_FALSE;
13090Sstevel@tonic-gate 			svc_satisfiable = B_FALSE;
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 			for (e2 = uu_list_first(v->gv_dependencies);
13120Sstevel@tonic-gate 			    e2 != NULL;
13130Sstevel@tonic-gate 			    e2 = uu_list_next(v->gv_dependencies, e2)) {
13140Sstevel@tonic-gate 				v2 = e2->ge_vertex;
13150Sstevel@tonic-gate 				assert(v2->gv_type == GVT_INST);
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 				if ((v2->gv_flags &
13180Sstevel@tonic-gate 				    (GV_CONFIGURED | GV_ENABLED)) !=
13190Sstevel@tonic-gate 				    (GV_CONFIGURED | GV_ENABLED))
13200Sstevel@tonic-gate 					continue;
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 				if (v2->gv_state == RESTARTER_STATE_MAINT)
13230Sstevel@tonic-gate 					continue;
13240Sstevel@tonic-gate 
1325*8354SRenaud.Manus@Sun.COM 				if (v2->gv_flags & GV_TOOFFLINE)
1326*8354SRenaud.Manus@Sun.COM 					continue;
1327*8354SRenaud.Manus@Sun.COM 
13280Sstevel@tonic-gate 				svc_any_qualified = B_TRUE;
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 				if (v2->gv_state == RESTARTER_STATE_OFFLINE) {
13310Sstevel@tonic-gate 					/*
13320Sstevel@tonic-gate 					 * For offline dependencies, treat
13330Sstevel@tonic-gate 					 * unsatisfiable as satisfied.
13340Sstevel@tonic-gate 					 */
13350Sstevel@tonic-gate 					i = dependency_satisfied(v2, B_TRUE);
13360Sstevel@tonic-gate 					if (i == -1)
13370Sstevel@tonic-gate 						i = 1;
13380Sstevel@tonic-gate 				} else if (v2->gv_state ==
13390Sstevel@tonic-gate 				    RESTARTER_STATE_DISABLED) {
13400Sstevel@tonic-gate 					i = 0;
13410Sstevel@tonic-gate 				} else {
13420Sstevel@tonic-gate 					i = dependency_satisfied(v2, satbility);
13430Sstevel@tonic-gate 				}
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 				if (i == 1) {
13460Sstevel@tonic-gate 					svc_satisfied = B_TRUE;
13470Sstevel@tonic-gate 					break;
13480Sstevel@tonic-gate 				}
13490Sstevel@tonic-gate 				if (i == 0)
13500Sstevel@tonic-gate 					svc_satisfiable = B_TRUE;
13510Sstevel@tonic-gate 			}
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 			if (!svc_any_qualified)
13540Sstevel@tonic-gate 				continue;
13550Sstevel@tonic-gate 			any_qualified = B_TRUE;
13560Sstevel@tonic-gate 			if (svc_satisfied) {
13570Sstevel@tonic-gate 				i = 1;
13580Sstevel@tonic-gate 			} else if (svc_satisfiable) {
13590Sstevel@tonic-gate 				i = 0;
13600Sstevel@tonic-gate 			} else {
13610Sstevel@tonic-gate 				i = -1;
13620Sstevel@tonic-gate 			}
13630Sstevel@tonic-gate 			break;
13640Sstevel@tonic-gate 		}
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 		case GVT_GROUP:
13670Sstevel@tonic-gate 		default:
13680Sstevel@tonic-gate #ifndef NDEBUG
13690Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
13700Sstevel@tonic-gate 			    __LINE__, v->gv_type);
13710Sstevel@tonic-gate #endif
13720Sstevel@tonic-gate 			abort();
13730Sstevel@tonic-gate 		}
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 		if (i == 1)
13760Sstevel@tonic-gate 			continue;
13770Sstevel@tonic-gate 
13785680Srm88369 		log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
13790Sstevel@tonic-gate 		    "optional_all(%s): %s is unsatisfi%s.\n", groupv->gv_name,
13800Sstevel@tonic-gate 		    v->gv_name, i == 0 ? "ed" : "able");
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 		if (!satbility)
13830Sstevel@tonic-gate 			return (0);
13840Sstevel@tonic-gate 		if (i == -1)
13850Sstevel@tonic-gate 			return (-1);
13860Sstevel@tonic-gate 		any_unsatisfied = B_TRUE;
13870Sstevel@tonic-gate 	}
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	if (!any_qualified)
13900Sstevel@tonic-gate 		return (1);
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	return (any_unsatisfied ? 0 : 1);
13930Sstevel@tonic-gate }
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate /*
13960Sstevel@tonic-gate  * An exclude_all dependency is unsatisfied if any non-service element is
13970Sstevel@tonic-gate  * satisfied or any service instance which is configured, enabled, and not in
13980Sstevel@tonic-gate  * maintenance is satisfied.  Usually when unsatisfied, it is also
13990Sstevel@tonic-gate  * unsatisfiable.
14000Sstevel@tonic-gate  */
14010Sstevel@tonic-gate #define	LOG_EXCLUDE(u, v)						\
14025680Srm88369 	log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,			\
14035680Srm88369 	    "exclude_all(%s): %s is satisfied.\n",			\
14040Sstevel@tonic-gate 	    (u)->gv_name, (v)->gv_name)
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate /* ARGSUSED */
14070Sstevel@tonic-gate static int
14080Sstevel@tonic-gate exclude_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
14090Sstevel@tonic-gate {
14100Sstevel@tonic-gate 	graph_edge_t *edge, *e2;
14110Sstevel@tonic-gate 	graph_vertex_t *v, *v2;
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
14140Sstevel@tonic-gate 	    edge != NULL;
14150Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
14160Sstevel@tonic-gate 		v = edge->ge_vertex;
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 		switch (v->gv_type) {
14190Sstevel@tonic-gate 		case GVT_INST:
14200Sstevel@tonic-gate 			if ((v->gv_flags & GV_CONFIGURED) == 0)
14210Sstevel@tonic-gate 				continue;
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 			switch (v->gv_state) {
14240Sstevel@tonic-gate 			case RESTARTER_STATE_ONLINE:
14250Sstevel@tonic-gate 			case RESTARTER_STATE_DEGRADED:
14260Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v);
14270Sstevel@tonic-gate 				return (v->gv_flags & GV_ENABLED ? -1 : 0);
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 			case RESTARTER_STATE_OFFLINE:
14300Sstevel@tonic-gate 			case RESTARTER_STATE_UNINIT:
14310Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v);
14320Sstevel@tonic-gate 				return (0);
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 			case RESTARTER_STATE_DISABLED:
14350Sstevel@tonic-gate 			case RESTARTER_STATE_MAINT:
14360Sstevel@tonic-gate 				continue;
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 			default:
14390Sstevel@tonic-gate #ifndef NDEBUG
14400Sstevel@tonic-gate 				uu_warn("%s:%d: Unexpected vertex state %d.\n",
14410Sstevel@tonic-gate 				    __FILE__, __LINE__, v->gv_state);
14420Sstevel@tonic-gate #endif
14430Sstevel@tonic-gate 				abort();
14440Sstevel@tonic-gate 			}
14450Sstevel@tonic-gate 			/* NOTREACHED */
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 		case GVT_SVC:
14480Sstevel@tonic-gate 			break;
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 		case GVT_FILE:
14510Sstevel@tonic-gate 			if (!file_ready(v))
14520Sstevel@tonic-gate 				continue;
14530Sstevel@tonic-gate 			LOG_EXCLUDE(groupv, v);
14540Sstevel@tonic-gate 			return (-1);
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 		case GVT_GROUP:
14570Sstevel@tonic-gate 		default:
14580Sstevel@tonic-gate #ifndef NDEBUG
14590Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
14600Sstevel@tonic-gate 			    __LINE__, v->gv_type);
14610Sstevel@tonic-gate #endif
14620Sstevel@tonic-gate 			abort();
14630Sstevel@tonic-gate 		}
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 		/* v represents a service */
14660Sstevel@tonic-gate 		if (uu_list_numnodes(v->gv_dependencies) == 0)
14670Sstevel@tonic-gate 			continue;
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 		for (e2 = uu_list_first(v->gv_dependencies);
14700Sstevel@tonic-gate 		    e2 != NULL;
14710Sstevel@tonic-gate 		    e2 = uu_list_next(v->gv_dependencies, e2)) {
14720Sstevel@tonic-gate 			v2 = e2->ge_vertex;
14730Sstevel@tonic-gate 			assert(v2->gv_type == GVT_INST);
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 			if ((v2->gv_flags & GV_CONFIGURED) == 0)
14760Sstevel@tonic-gate 				continue;
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 			switch (v2->gv_state) {
14790Sstevel@tonic-gate 			case RESTARTER_STATE_ONLINE:
14800Sstevel@tonic-gate 			case RESTARTER_STATE_DEGRADED:
14810Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v2);
14820Sstevel@tonic-gate 				return (v2->gv_flags & GV_ENABLED ? -1 : 0);
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 			case RESTARTER_STATE_OFFLINE:
14850Sstevel@tonic-gate 			case RESTARTER_STATE_UNINIT:
14860Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v2);
14870Sstevel@tonic-gate 				return (0);
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 			case RESTARTER_STATE_DISABLED:
14900Sstevel@tonic-gate 			case RESTARTER_STATE_MAINT:
14910Sstevel@tonic-gate 				continue;
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 			default:
14940Sstevel@tonic-gate #ifndef NDEBUG
14950Sstevel@tonic-gate 				uu_warn("%s:%d: Unexpected vertex type %d.\n",
14960Sstevel@tonic-gate 				    __FILE__, __LINE__, v2->gv_type);
14970Sstevel@tonic-gate #endif
14980Sstevel@tonic-gate 				abort();
14990Sstevel@tonic-gate 			}
15000Sstevel@tonic-gate 		}
15010Sstevel@tonic-gate 	}
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 	return (1);
15040Sstevel@tonic-gate }
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate /*
15070Sstevel@tonic-gate  * int instance_satisfied()
15080Sstevel@tonic-gate  *   Determine if all the dependencies are satisfied for the supplied instance
15090Sstevel@tonic-gate  *   vertex. Return 1 if they are, 0 if they aren't, and -1 if they won't be
15100Sstevel@tonic-gate  *   without administrator intervention.
15110Sstevel@tonic-gate  */
15120Sstevel@tonic-gate static int
15130Sstevel@tonic-gate instance_satisfied(graph_vertex_t *v, boolean_t satbility)
15140Sstevel@tonic-gate {
15150Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
15160Sstevel@tonic-gate 	assert(!inst_running(v));
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 	return (require_all_satisfied(v, satbility));
15190Sstevel@tonic-gate }
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate /*
15220Sstevel@tonic-gate  * Decide whether v can satisfy a dependency.  v can either be a child of
15230Sstevel@tonic-gate  * a group vertex, or of an instance vertex.
15240Sstevel@tonic-gate  */
15250Sstevel@tonic-gate static int
15260Sstevel@tonic-gate dependency_satisfied(graph_vertex_t *v, boolean_t satbility)
15270Sstevel@tonic-gate {
15280Sstevel@tonic-gate 	switch (v->gv_type) {
15290Sstevel@tonic-gate 	case GVT_INST:
15307475SPhilippe.Jung@Sun.COM 		if ((v->gv_flags & GV_CONFIGURED) == 0) {
15317475SPhilippe.Jung@Sun.COM 			if (v->gv_flags & GV_DEATHROW) {
15327475SPhilippe.Jung@Sun.COM 				/*
15337475SPhilippe.Jung@Sun.COM 				 * A dependency on an instance with GV_DEATHROW
15347475SPhilippe.Jung@Sun.COM 				 * flag is always considered as satisfied.
15357475SPhilippe.Jung@Sun.COM 				 */
15367475SPhilippe.Jung@Sun.COM 				return (1);
15377475SPhilippe.Jung@Sun.COM 			}
15380Sstevel@tonic-gate 			return (-1);
15397475SPhilippe.Jung@Sun.COM 		}
15400Sstevel@tonic-gate 
1541*8354SRenaud.Manus@Sun.COM 		/*
1542*8354SRenaud.Manus@Sun.COM 		 * Any vertex with the GV_TOOFFLINE flag set is guaranteed
1543*8354SRenaud.Manus@Sun.COM 		 * to have its dependencies unsatisfiable.
1544*8354SRenaud.Manus@Sun.COM 		 */
1545*8354SRenaud.Manus@Sun.COM 		if (v->gv_flags & GV_TOOFFLINE)
1546*8354SRenaud.Manus@Sun.COM 			return (-1);
1547*8354SRenaud.Manus@Sun.COM 
15480Sstevel@tonic-gate 		switch (v->gv_state) {
15490Sstevel@tonic-gate 		case RESTARTER_STATE_ONLINE:
15500Sstevel@tonic-gate 		case RESTARTER_STATE_DEGRADED:
15510Sstevel@tonic-gate 			return (1);
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 		case RESTARTER_STATE_OFFLINE:
15540Sstevel@tonic-gate 			if (!satbility)
15550Sstevel@tonic-gate 				return (0);
15560Sstevel@tonic-gate 			return (instance_satisfied(v, satbility) != -1 ?
15570Sstevel@tonic-gate 			    0 : -1);
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 		case RESTARTER_STATE_DISABLED:
15600Sstevel@tonic-gate 		case RESTARTER_STATE_MAINT:
15610Sstevel@tonic-gate 			return (-1);
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 		case RESTARTER_STATE_UNINIT:
15640Sstevel@tonic-gate 			return (0);
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 		default:
15670Sstevel@tonic-gate #ifndef NDEBUG
15680Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex state %d.\n",
15690Sstevel@tonic-gate 			    __FILE__, __LINE__, v->gv_state);
15700Sstevel@tonic-gate #endif
15710Sstevel@tonic-gate 			abort();
15720Sstevel@tonic-gate 			/* NOTREACHED */
15730Sstevel@tonic-gate 		}
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 	case GVT_SVC:
15760Sstevel@tonic-gate 		if (uu_list_numnodes(v->gv_dependencies) == 0)
15770Sstevel@tonic-gate 			return (-1);
15780Sstevel@tonic-gate 		return (require_any_satisfied(v, satbility));
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	case GVT_FILE:
15810Sstevel@tonic-gate 		/* i.e., we assume files will not be automatically generated */
15820Sstevel@tonic-gate 		return (file_ready(v) ? 1 : -1);
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	case GVT_GROUP:
15850Sstevel@tonic-gate 		break;
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	default:
15880Sstevel@tonic-gate #ifndef NDEBUG
15890Sstevel@tonic-gate 		uu_warn("%s:%d: Unexpected node type %d.\n", __FILE__, __LINE__,
15900Sstevel@tonic-gate 		    v->gv_type);
15910Sstevel@tonic-gate #endif
15920Sstevel@tonic-gate 		abort();
15930Sstevel@tonic-gate 		/* NOTREACHED */
15940Sstevel@tonic-gate 	}
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	switch (v->gv_depgroup) {
15970Sstevel@tonic-gate 	case DEPGRP_REQUIRE_ANY:
15980Sstevel@tonic-gate 		return (require_any_satisfied(v, satbility));
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	case DEPGRP_REQUIRE_ALL:
16010Sstevel@tonic-gate 		return (require_all_satisfied(v, satbility));
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 	case DEPGRP_OPTIONAL_ALL:
16040Sstevel@tonic-gate 		return (optional_all_satisfied(v, satbility));
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate 	case DEPGRP_EXCLUDE_ALL:
16070Sstevel@tonic-gate 		return (exclude_all_satisfied(v, satbility));
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	default:
16100Sstevel@tonic-gate #ifndef NDEBUG
16110Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown dependency grouping %d.\n", __FILE__,
16120Sstevel@tonic-gate 		    __LINE__, v->gv_depgroup);
16130Sstevel@tonic-gate #endif
16140Sstevel@tonic-gate 		abort();
16150Sstevel@tonic-gate 	}
16160Sstevel@tonic-gate }
16170Sstevel@tonic-gate 
16181958Slianep void
16191958Slianep graph_start_if_satisfied(graph_vertex_t *v)
16200Sstevel@tonic-gate {
16210Sstevel@tonic-gate 	if (v->gv_state == RESTARTER_STATE_OFFLINE &&
16220Sstevel@tonic-gate 	    instance_satisfied(v, B_FALSE) == 1) {
16230Sstevel@tonic-gate 		if (v->gv_start_f == NULL)
16240Sstevel@tonic-gate 			vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
16250Sstevel@tonic-gate 		else
16260Sstevel@tonic-gate 			v->gv_start_f(v);
16270Sstevel@tonic-gate 	}
16280Sstevel@tonic-gate }
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate /*
16310Sstevel@tonic-gate  * propagate_satbility()
16320Sstevel@tonic-gate  *
16330Sstevel@tonic-gate  * This function is used when the given vertex changes state in such a way that
16340Sstevel@tonic-gate  * one of its dependents may become unsatisfiable.  This happens when an
16350Sstevel@tonic-gate  * instance transitions between offline -> online, or from !running ->
16360Sstevel@tonic-gate  * maintenance, as well as when an instance is removed from the graph.
16370Sstevel@tonic-gate  *
16382339Slianep  * We have to walk all the dependents, since optional_all dependencies several
16390Sstevel@tonic-gate  * levels up could become (un)satisfied, instead of unsatisfiable.  For example,
16400Sstevel@tonic-gate  *
16410Sstevel@tonic-gate  *	+-----+  optional_all  +-----+  require_all  +-----+
16420Sstevel@tonic-gate  *	|  A  |--------------->|  B  |-------------->|  C  |
16430Sstevel@tonic-gate  *	+-----+                +-----+               +-----+
16440Sstevel@tonic-gate  *
16450Sstevel@tonic-gate  *	                                        offline -> maintenance
16460Sstevel@tonic-gate  *
16470Sstevel@tonic-gate  * If C goes into maintenance, it's not enough simply to check B.  Because A has
16480Sstevel@tonic-gate  * an optional dependency, what was previously an unsatisfiable situation is now
16490Sstevel@tonic-gate  * satisfied (B will never come online, even though its state hasn't changed).
16500Sstevel@tonic-gate  *
16510Sstevel@tonic-gate  * Note that it's not necessary to continue examining dependents after reaching
16520Sstevel@tonic-gate  * an optional_all dependency.  It's not possible for an optional_all dependency
16530Sstevel@tonic-gate  * to change satisfiability without also coming online, in which case we get a
16540Sstevel@tonic-gate  * start event and propagation continues naturally.  However, it does no harm to
16550Sstevel@tonic-gate  * continue propagating satisfiability (as it is a relatively rare event), and
16560Sstevel@tonic-gate  * keeps the walker code simple and generic.
16570Sstevel@tonic-gate  */
16580Sstevel@tonic-gate /*ARGSUSED*/
16590Sstevel@tonic-gate static int
16600Sstevel@tonic-gate satbility_cb(graph_vertex_t *v, void *arg)
16610Sstevel@tonic-gate {
16620Sstevel@tonic-gate 	if (v->gv_type == GVT_INST)
16631958Slianep 		graph_start_if_satisfied(v);
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 	return (UU_WALK_NEXT);
16660Sstevel@tonic-gate }
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate static void
16690Sstevel@tonic-gate propagate_satbility(graph_vertex_t *v)
16700Sstevel@tonic-gate {
16710Sstevel@tonic-gate 	graph_walk(v, WALK_DEPENDENTS, satbility_cb, NULL, NULL);
16720Sstevel@tonic-gate }
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate static void propagate_stop(graph_vertex_t *, void *);
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate /* ARGSUSED */
16770Sstevel@tonic-gate static void
16780Sstevel@tonic-gate propagate_start(graph_vertex_t *v, void *arg)
16790Sstevel@tonic-gate {
16800Sstevel@tonic-gate 	switch (v->gv_type) {
16810Sstevel@tonic-gate 	case GVT_INST:
16821958Slianep 		graph_start_if_satisfied(v);
16830Sstevel@tonic-gate 		break;
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 	case GVT_GROUP:
16860Sstevel@tonic-gate 		if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
16870Sstevel@tonic-gate 			graph_walk_dependents(v, propagate_stop,
16880Sstevel@tonic-gate 			    (void *)RERR_RESTART);
16890Sstevel@tonic-gate 			break;
16900Sstevel@tonic-gate 		}
16910Sstevel@tonic-gate 		/* FALLTHROUGH */
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	case GVT_SVC:
16940Sstevel@tonic-gate 		graph_walk_dependents(v, propagate_start, NULL);
16950Sstevel@tonic-gate 		break;
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	case GVT_FILE:
16980Sstevel@tonic-gate #ifndef NDEBUG
16990Sstevel@tonic-gate 		uu_warn("%s:%d: propagate_start() encountered GVT_FILE.\n",
17000Sstevel@tonic-gate 		    __FILE__, __LINE__);
17010Sstevel@tonic-gate #endif
17020Sstevel@tonic-gate 		abort();
17030Sstevel@tonic-gate 		/* NOTREACHED */
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	default:
17060Sstevel@tonic-gate #ifndef NDEBUG
17070Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__,
17080Sstevel@tonic-gate 		    v->gv_type);
17090Sstevel@tonic-gate #endif
17100Sstevel@tonic-gate 		abort();
17110Sstevel@tonic-gate 	}
17120Sstevel@tonic-gate }
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate static void
17150Sstevel@tonic-gate propagate_stop(graph_vertex_t *v, void *arg)
17160Sstevel@tonic-gate {
17170Sstevel@tonic-gate 	graph_edge_t *e;
17180Sstevel@tonic-gate 	graph_vertex_t *svc;
17190Sstevel@tonic-gate 	restarter_error_t err = (restarter_error_t)arg;
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	switch (v->gv_type) {
17220Sstevel@tonic-gate 	case GVT_INST:
17230Sstevel@tonic-gate 		/* Restarter */
17240Sstevel@tonic-gate 		if (err > RERR_NONE && inst_running(v))
17250Sstevel@tonic-gate 			vertex_send_event(v, RESTARTER_EVENT_TYPE_STOP);
17260Sstevel@tonic-gate 		break;
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	case GVT_SVC:
17290Sstevel@tonic-gate 		graph_walk_dependents(v, propagate_stop, arg);
17300Sstevel@tonic-gate 		break;
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 	case GVT_FILE:
17330Sstevel@tonic-gate #ifndef NDEBUG
17340Sstevel@tonic-gate 		uu_warn("%s:%d: propagate_stop() encountered GVT_FILE.\n",
17350Sstevel@tonic-gate 		    __FILE__, __LINE__);
17360Sstevel@tonic-gate #endif
17370Sstevel@tonic-gate 		abort();
17380Sstevel@tonic-gate 		/* NOTREACHED */
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 	case GVT_GROUP:
17410Sstevel@tonic-gate 		if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
17420Sstevel@tonic-gate 			graph_walk_dependents(v, propagate_start, NULL);
17430Sstevel@tonic-gate 			break;
17440Sstevel@tonic-gate 		}
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 		if (err == RERR_NONE || err > v->gv_restart)
17470Sstevel@tonic-gate 			break;
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 		assert(uu_list_numnodes(v->gv_dependents) == 1);
17500Sstevel@tonic-gate 		e = uu_list_first(v->gv_dependents);
17510Sstevel@tonic-gate 		svc = e->ge_vertex;
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 		if (inst_running(svc))
17540Sstevel@tonic-gate 			vertex_send_event(svc, RESTARTER_EVENT_TYPE_STOP);
17550Sstevel@tonic-gate 		break;
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 	default:
17580Sstevel@tonic-gate #ifndef NDEBUG
17590Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__,
17600Sstevel@tonic-gate 		    v->gv_type);
17610Sstevel@tonic-gate #endif
17620Sstevel@tonic-gate 		abort();
17630Sstevel@tonic-gate 	}
17640Sstevel@tonic-gate }
17650Sstevel@tonic-gate 
17667630SRenaud.Manus@Sun.COM static void
17677630SRenaud.Manus@Sun.COM offline_vertex(graph_vertex_t *v)
17687630SRenaud.Manus@Sun.COM {
17697630SRenaud.Manus@Sun.COM 	scf_handle_t *h = libscf_handle_create_bound_loop();
17707630SRenaud.Manus@Sun.COM 	scf_instance_t *scf_inst = safe_scf_instance_create(h);
17717630SRenaud.Manus@Sun.COM 	scf_propertygroup_t *pg = safe_scf_pg_create(h);
17727630SRenaud.Manus@Sun.COM 	restarter_instance_state_t state, next_state;
17737630SRenaud.Manus@Sun.COM 	int r;
17747630SRenaud.Manus@Sun.COM 
17757630SRenaud.Manus@Sun.COM 	assert(v->gv_type == GVT_INST);
17767630SRenaud.Manus@Sun.COM 
17777630SRenaud.Manus@Sun.COM 	if (scf_inst == NULL)
17787630SRenaud.Manus@Sun.COM 		bad_error("safe_scf_instance_create", scf_error());
17797630SRenaud.Manus@Sun.COM 	if (pg == NULL)
17807630SRenaud.Manus@Sun.COM 		bad_error("safe_scf_pg_create", scf_error());
17817630SRenaud.Manus@Sun.COM 
17827630SRenaud.Manus@Sun.COM 	/* if the vertex is already going offline, return */
17837630SRenaud.Manus@Sun.COM rep_retry:
17847630SRenaud.Manus@Sun.COM 	if (scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, scf_inst, NULL,
17857630SRenaud.Manus@Sun.COM 	    NULL, SCF_DECODE_FMRI_EXACT) != 0) {
17867630SRenaud.Manus@Sun.COM 		switch (scf_error()) {
17877630SRenaud.Manus@Sun.COM 		case SCF_ERROR_CONNECTION_BROKEN:
17887630SRenaud.Manus@Sun.COM 			libscf_handle_rebind(h);
17897630SRenaud.Manus@Sun.COM 			goto rep_retry;
17907630SRenaud.Manus@Sun.COM 
17917630SRenaud.Manus@Sun.COM 		case SCF_ERROR_NOT_FOUND:
17927630SRenaud.Manus@Sun.COM 			scf_pg_destroy(pg);
17937630SRenaud.Manus@Sun.COM 			scf_instance_destroy(scf_inst);
17947630SRenaud.Manus@Sun.COM 			(void) scf_handle_unbind(h);
17957630SRenaud.Manus@Sun.COM 			scf_handle_destroy(h);
17967630SRenaud.Manus@Sun.COM 			return;
17977630SRenaud.Manus@Sun.COM 		}
17987630SRenaud.Manus@Sun.COM 		uu_die("Can't decode FMRI %s: %s\n", v->gv_name,
17997630SRenaud.Manus@Sun.COM 		    scf_strerror(scf_error()));
18007630SRenaud.Manus@Sun.COM 	}
18017630SRenaud.Manus@Sun.COM 
18027630SRenaud.Manus@Sun.COM 	r = scf_instance_get_pg(scf_inst, SCF_PG_RESTARTER, pg);
18037630SRenaud.Manus@Sun.COM 	if (r != 0) {
18047630SRenaud.Manus@Sun.COM 		switch (scf_error()) {
18057630SRenaud.Manus@Sun.COM 		case SCF_ERROR_CONNECTION_BROKEN:
18067630SRenaud.Manus@Sun.COM 			libscf_handle_rebind(h);
18077630SRenaud.Manus@Sun.COM 			goto rep_retry;
18087630SRenaud.Manus@Sun.COM 
18097630SRenaud.Manus@Sun.COM 		case SCF_ERROR_NOT_SET:
18107630SRenaud.Manus@Sun.COM 		case SCF_ERROR_NOT_FOUND:
18117630SRenaud.Manus@Sun.COM 			scf_pg_destroy(pg);
18127630SRenaud.Manus@Sun.COM 			scf_instance_destroy(scf_inst);
18137630SRenaud.Manus@Sun.COM 			(void) scf_handle_unbind(h);
18147630SRenaud.Manus@Sun.COM 			scf_handle_destroy(h);
18157630SRenaud.Manus@Sun.COM 			return;
18167630SRenaud.Manus@Sun.COM 
18177630SRenaud.Manus@Sun.COM 		default:
18187630SRenaud.Manus@Sun.COM 			bad_error("scf_instance_get_pg", scf_error());
18197630SRenaud.Manus@Sun.COM 		}
18207630SRenaud.Manus@Sun.COM 	} else {
18217630SRenaud.Manus@Sun.COM 		r = libscf_read_states(pg, &state, &next_state);
18227630SRenaud.Manus@Sun.COM 		if (r == 0 && (next_state == RESTARTER_STATE_OFFLINE ||
18237630SRenaud.Manus@Sun.COM 		    next_state == RESTARTER_STATE_DISABLED)) {
18247630SRenaud.Manus@Sun.COM 			log_framework(LOG_DEBUG,
18257630SRenaud.Manus@Sun.COM 			    "%s: instance is already going down.\n",
18267630SRenaud.Manus@Sun.COM 			    v->gv_name);
18277630SRenaud.Manus@Sun.COM 			scf_pg_destroy(pg);
18287630SRenaud.Manus@Sun.COM 			scf_instance_destroy(scf_inst);
18297630SRenaud.Manus@Sun.COM 			(void) scf_handle_unbind(h);
18307630SRenaud.Manus@Sun.COM 			scf_handle_destroy(h);
18317630SRenaud.Manus@Sun.COM 			return;
18327630SRenaud.Manus@Sun.COM 		}
18337630SRenaud.Manus@Sun.COM 	}
18347630SRenaud.Manus@Sun.COM 
18357630SRenaud.Manus@Sun.COM 	scf_pg_destroy(pg);
18367630SRenaud.Manus@Sun.COM 	scf_instance_destroy(scf_inst);
18377630SRenaud.Manus@Sun.COM 	(void) scf_handle_unbind(h);
18387630SRenaud.Manus@Sun.COM 	scf_handle_destroy(h);
18397815SRenaud.Manus@Sun.COM 
18407815SRenaud.Manus@Sun.COM 	vertex_send_event(v, RESTARTER_EVENT_TYPE_STOP);
18417630SRenaud.Manus@Sun.COM }
18427630SRenaud.Manus@Sun.COM 
18430Sstevel@tonic-gate /*
18440Sstevel@tonic-gate  * void graph_enable_by_vertex()
18450Sstevel@tonic-gate  *   If admin is non-zero, this is an administrative request for change
18460Sstevel@tonic-gate  *   of the enabled property.  Thus, send the ADMIN_DISABLE rather than
18470Sstevel@tonic-gate  *   a plain DISABLE restarter event.
18480Sstevel@tonic-gate  */
18491958Slianep void
18500Sstevel@tonic-gate graph_enable_by_vertex(graph_vertex_t *vertex, int enable, int admin)
18510Sstevel@tonic-gate {
18527630SRenaud.Manus@Sun.COM 	graph_vertex_t *v;
18537630SRenaud.Manus@Sun.COM 	int r;
18547630SRenaud.Manus@Sun.COM 
18550Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
18560Sstevel@tonic-gate 	assert((vertex->gv_flags & GV_CONFIGURED));
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 	vertex->gv_flags = (vertex->gv_flags & ~GV_ENABLED) |
18590Sstevel@tonic-gate 	    (enable ? GV_ENABLED : 0);
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate 	if (enable) {
18620Sstevel@tonic-gate 		if (vertex->gv_state != RESTARTER_STATE_OFFLINE &&
18630Sstevel@tonic-gate 		    vertex->gv_state != RESTARTER_STATE_DEGRADED &&
18647630SRenaud.Manus@Sun.COM 		    vertex->gv_state != RESTARTER_STATE_ONLINE) {
18657630SRenaud.Manus@Sun.COM 			/*
18667630SRenaud.Manus@Sun.COM 			 * In case the vertex was notified to go down,
18677630SRenaud.Manus@Sun.COM 			 * but now can return online, clear the _TOOFFLINE
18687630SRenaud.Manus@Sun.COM 			 * and _TODISABLE flags.
18697630SRenaud.Manus@Sun.COM 			 */
18707630SRenaud.Manus@Sun.COM 			vertex->gv_flags &= ~GV_TOOFFLINE;
18717630SRenaud.Manus@Sun.COM 			vertex->gv_flags &= ~GV_TODISABLE;
18727630SRenaud.Manus@Sun.COM 
18730Sstevel@tonic-gate 			vertex_send_event(vertex, RESTARTER_EVENT_TYPE_ENABLE);
18740Sstevel@tonic-gate 		}
18757630SRenaud.Manus@Sun.COM 
18767630SRenaud.Manus@Sun.COM 		/*
18777630SRenaud.Manus@Sun.COM 		 * Wait for state update from restarter before sending _START or
18787630SRenaud.Manus@Sun.COM 		 * _STOP.
18797630SRenaud.Manus@Sun.COM 		 */
18807630SRenaud.Manus@Sun.COM 
18817630SRenaud.Manus@Sun.COM 		return;
18827630SRenaud.Manus@Sun.COM 	}
18837630SRenaud.Manus@Sun.COM 
18847630SRenaud.Manus@Sun.COM 	if (vertex->gv_state == RESTARTER_STATE_DISABLED)
18857630SRenaud.Manus@Sun.COM 		return;
18867630SRenaud.Manus@Sun.COM 
18877630SRenaud.Manus@Sun.COM 	if (!admin) {
18887630SRenaud.Manus@Sun.COM 		vertex_send_event(vertex, RESTARTER_EVENT_TYPE_DISABLE);
18897630SRenaud.Manus@Sun.COM 
18907630SRenaud.Manus@Sun.COM 		/*
18917630SRenaud.Manus@Sun.COM 		 * Wait for state update from restarter before sending _START or
18927630SRenaud.Manus@Sun.COM 		 * _STOP.
18937630SRenaud.Manus@Sun.COM 		 */
18947630SRenaud.Manus@Sun.COM 
18957630SRenaud.Manus@Sun.COM 		return;
18960Sstevel@tonic-gate 	}
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	/*
18997630SRenaud.Manus@Sun.COM 	 * If it is a DISABLE event requested by the administrator then we are
19007630SRenaud.Manus@Sun.COM 	 * offlining the dependents first.
19017630SRenaud.Manus@Sun.COM 	 */
19027630SRenaud.Manus@Sun.COM 
19037630SRenaud.Manus@Sun.COM 	/*
19047630SRenaud.Manus@Sun.COM 	 * Set GV_TOOFFLINE for the services we are offlining. We cannot
19057630SRenaud.Manus@Sun.COM 	 * clear the GV_TOOFFLINE bits from all the services because
19067630SRenaud.Manus@Sun.COM 	 * other DISABLE events might be handled at the same time.
19070Sstevel@tonic-gate 	 */
19087630SRenaud.Manus@Sun.COM 	vertex->gv_flags |= GV_TOOFFLINE;
19097630SRenaud.Manus@Sun.COM 
19107630SRenaud.Manus@Sun.COM 	/* remember which vertex to disable... */
19117630SRenaud.Manus@Sun.COM 	vertex->gv_flags |= GV_TODISABLE;
19127630SRenaud.Manus@Sun.COM 
1913*8354SRenaud.Manus@Sun.COM 	log_framework(LOG_DEBUG, "Marking in-subtree vertices before "
1914*8354SRenaud.Manus@Sun.COM 	    "disabling %s.\n", vertex->gv_name);
1915*8354SRenaud.Manus@Sun.COM 
19167630SRenaud.Manus@Sun.COM 	/* set GV_TOOFFLINE for its dependents */
19177630SRenaud.Manus@Sun.COM 	r = uu_list_walk(vertex->gv_dependents, (uu_walk_fn_t *)mark_subtree,
19187630SRenaud.Manus@Sun.COM 	    NULL, 0);
19197630SRenaud.Manus@Sun.COM 	assert(r == 0);
19207630SRenaud.Manus@Sun.COM 
19217630SRenaud.Manus@Sun.COM 	/* disable the instance now if there is nothing else to offline */
19227630SRenaud.Manus@Sun.COM 	if (insubtree_dependents_down(vertex) == B_TRUE) {
19237630SRenaud.Manus@Sun.COM 		vertex_send_event(vertex, RESTARTER_EVENT_TYPE_ADMIN_DISABLE);
19247630SRenaud.Manus@Sun.COM 		return;
19257630SRenaud.Manus@Sun.COM 	}
19267630SRenaud.Manus@Sun.COM 
19277630SRenaud.Manus@Sun.COM 	/*
19287630SRenaud.Manus@Sun.COM 	 * This loop is similar to the one used for the graph reversal shutdown
19297630SRenaud.Manus@Sun.COM 	 * and could be improved in term of performance for the subtree reversal
19307630SRenaud.Manus@Sun.COM 	 * disable case.
19317630SRenaud.Manus@Sun.COM 	 */
19327630SRenaud.Manus@Sun.COM 	for (v = uu_list_first(dgraph); v != NULL;
19337630SRenaud.Manus@Sun.COM 	    v = uu_list_next(dgraph, v)) {
19347630SRenaud.Manus@Sun.COM 		/* skip the vertex we are disabling for now */
19357630SRenaud.Manus@Sun.COM 		if (v == vertex)
19367630SRenaud.Manus@Sun.COM 			continue;
19377630SRenaud.Manus@Sun.COM 
19387630SRenaud.Manus@Sun.COM 		if (v->gv_type != GVT_INST ||
19397630SRenaud.Manus@Sun.COM 		    (v->gv_flags & GV_CONFIGURED) == 0 ||
19407630SRenaud.Manus@Sun.COM 		    (v->gv_flags & GV_ENABLED) == 0 ||
19417630SRenaud.Manus@Sun.COM 		    (v->gv_flags & GV_TOOFFLINE) == 0)
19427630SRenaud.Manus@Sun.COM 			continue;
19437630SRenaud.Manus@Sun.COM 
19447630SRenaud.Manus@Sun.COM 		if ((v->gv_state != RESTARTER_STATE_ONLINE) &&
19457630SRenaud.Manus@Sun.COM 		    (v->gv_state != RESTARTER_STATE_DEGRADED)) {
19467630SRenaud.Manus@Sun.COM 			/* continue if there is nothing to offline */
19477630SRenaud.Manus@Sun.COM 			continue;
19487630SRenaud.Manus@Sun.COM 		}
19497630SRenaud.Manus@Sun.COM 
19507630SRenaud.Manus@Sun.COM 		/*
19517630SRenaud.Manus@Sun.COM 		 * Instances which are up need to come down before we're
19527630SRenaud.Manus@Sun.COM 		 * done, but we can only offline the leaves here. An
19537630SRenaud.Manus@Sun.COM 		 * instance is a leaf when all its dependents are down.
19547630SRenaud.Manus@Sun.COM 		 */
1955*8354SRenaud.Manus@Sun.COM 		if (insubtree_dependents_down(v) == B_TRUE) {
1956*8354SRenaud.Manus@Sun.COM 			log_framework(LOG_DEBUG, "Offlining in-subtree "
1957*8354SRenaud.Manus@Sun.COM 			    "instance %s for %s.\n",
1958*8354SRenaud.Manus@Sun.COM 			    v->gv_name, vertex->gv_name);
19597630SRenaud.Manus@Sun.COM 			offline_vertex(v);
1960*8354SRenaud.Manus@Sun.COM 		}
19617630SRenaud.Manus@Sun.COM 	}
19620Sstevel@tonic-gate }
19630Sstevel@tonic-gate 
19640Sstevel@tonic-gate static int configure_vertex(graph_vertex_t *, scf_instance_t *);
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate /*
19670Sstevel@tonic-gate  * Set the restarter for v to fmri_arg.  That is, make sure a vertex for
19680Sstevel@tonic-gate  * fmri_arg exists, make v depend on it, and send _ADD_INSTANCE for v.  If
19690Sstevel@tonic-gate  * v is already configured and fmri_arg indicates the current restarter, do
19700Sstevel@tonic-gate  * nothing.  If v is configured and fmri_arg is a new restarter, delete v's
19710Sstevel@tonic-gate  * dependency on the restarter, send _REMOVE_INSTANCE for v, and set the new
19720Sstevel@tonic-gate  * restarter.  Returns 0 on success, EINVAL if the FMRI is invalid,
19730Sstevel@tonic-gate  * ECONNABORTED if the repository connection is broken, and ELOOP
19740Sstevel@tonic-gate  * if the dependency would create a cycle.  In the last case, *pathp will
19750Sstevel@tonic-gate  * point to a -1-terminated array of ids which compose the path from v to
19760Sstevel@tonic-gate  * restarter_fmri.
19770Sstevel@tonic-gate  */
19780Sstevel@tonic-gate int
19790Sstevel@tonic-gate graph_change_restarter(graph_vertex_t *v, const char *fmri_arg, scf_handle_t *h,
19800Sstevel@tonic-gate     int **pathp)
19810Sstevel@tonic-gate {
19820Sstevel@tonic-gate 	char *restarter_fmri = NULL;
19830Sstevel@tonic-gate 	graph_vertex_t *rv;
19840Sstevel@tonic-gate 	int err;
19850Sstevel@tonic-gate 	int id;
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
19880Sstevel@tonic-gate 
19890Sstevel@tonic-gate 	if (fmri_arg[0] != '\0') {
19900Sstevel@tonic-gate 		err = fmri_canonify(fmri_arg, &restarter_fmri, B_TRUE);
19910Sstevel@tonic-gate 		if (err != 0) {
19920Sstevel@tonic-gate 			assert(err == EINVAL);
19930Sstevel@tonic-gate 			return (err);
19940Sstevel@tonic-gate 		}
19950Sstevel@tonic-gate 	}
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 	if (restarter_fmri == NULL ||
19980Sstevel@tonic-gate 	    strcmp(restarter_fmri, SCF_SERVICE_STARTD) == 0) {
19990Sstevel@tonic-gate 		if (v->gv_flags & GV_CONFIGURED) {
20000Sstevel@tonic-gate 			if (v->gv_restarter_id == -1) {
20010Sstevel@tonic-gate 				if (restarter_fmri != NULL)
20020Sstevel@tonic-gate 					startd_free(restarter_fmri,
20030Sstevel@tonic-gate 					    max_scf_fmri_size);
20040Sstevel@tonic-gate 				return (0);
20050Sstevel@tonic-gate 			}
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 			graph_unset_restarter(v);
20080Sstevel@tonic-gate 		}
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 		/* Master restarter, nothing to do. */
20110Sstevel@tonic-gate 		v->gv_restarter_id = -1;
20120Sstevel@tonic-gate 		v->gv_restarter_channel = NULL;
20130Sstevel@tonic-gate 		vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE);
20140Sstevel@tonic-gate 		return (0);
20150Sstevel@tonic-gate 	}
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	if (v->gv_flags & GV_CONFIGURED) {
20180Sstevel@tonic-gate 		id = dict_lookup_byname(restarter_fmri);
20190Sstevel@tonic-gate 		if (id != -1 && v->gv_restarter_id == id) {
20200Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_fmri_size);
20210Sstevel@tonic-gate 			return (0);
20220Sstevel@tonic-gate 		}
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 		graph_unset_restarter(v);
20250Sstevel@tonic-gate 	}
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 	err = graph_insert_vertex_unconfigured(restarter_fmri, GVT_INST, 0,
20280Sstevel@tonic-gate 	    RERR_NONE, &rv);
20290Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_fmri_size);
20300Sstevel@tonic-gate 	assert(err == 0 || err == EEXIST);
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate 	if (rv->gv_delegate_initialized == 0) {
20330Sstevel@tonic-gate 		rv->gv_delegate_channel = restarter_protocol_init_delegate(
20340Sstevel@tonic-gate 		    rv->gv_name);
20350Sstevel@tonic-gate 		rv->gv_delegate_initialized = 1;
20360Sstevel@tonic-gate 	}
20370Sstevel@tonic-gate 	v->gv_restarter_id = rv->gv_id;
20380Sstevel@tonic-gate 	v->gv_restarter_channel = rv->gv_delegate_channel;
20390Sstevel@tonic-gate 
20400Sstevel@tonic-gate 	err = graph_insert_dependency(v, rv, pathp);
20410Sstevel@tonic-gate 	if (err != 0) {
20420Sstevel@tonic-gate 		assert(err == ELOOP);
20430Sstevel@tonic-gate 		return (ELOOP);
20440Sstevel@tonic-gate 	}
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE);
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 	if (!(rv->gv_flags & GV_CONFIGURED)) {
20490Sstevel@tonic-gate 		scf_instance_t *inst;
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 		err = libscf_fmri_get_instance(h, rv->gv_name, &inst);
20520Sstevel@tonic-gate 		switch (err) {
20530Sstevel@tonic-gate 		case 0:
20540Sstevel@tonic-gate 			err = configure_vertex(rv, inst);
20550Sstevel@tonic-gate 			scf_instance_destroy(inst);
20560Sstevel@tonic-gate 			switch (err) {
20570Sstevel@tonic-gate 			case 0:
20580Sstevel@tonic-gate 			case ECANCELED:
20590Sstevel@tonic-gate 				break;
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 			case ECONNABORTED:
20620Sstevel@tonic-gate 				return (ECONNABORTED);
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate 			default:
20650Sstevel@tonic-gate 				bad_error("configure_vertex", err);
20660Sstevel@tonic-gate 			}
20670Sstevel@tonic-gate 			break;
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate 		case ECONNABORTED:
20700Sstevel@tonic-gate 			return (ECONNABORTED);
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 		case ENOENT:
20730Sstevel@tonic-gate 			break;
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 		case ENOTSUP:
20760Sstevel@tonic-gate 			/*
20770Sstevel@tonic-gate 			 * The fmri doesn't specify an instance - translate
20780Sstevel@tonic-gate 			 * to EINVAL.
20790Sstevel@tonic-gate 			 */
20800Sstevel@tonic-gate 			return (EINVAL);
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 		case EINVAL:
20830Sstevel@tonic-gate 		default:
20840Sstevel@tonic-gate 			bad_error("libscf_fmri_get_instance", err);
20850Sstevel@tonic-gate 		}
20860Sstevel@tonic-gate 	}
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 	return (0);
20890Sstevel@tonic-gate }
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate /*
20930Sstevel@tonic-gate  * Add all of the instances of the service named by fmri to the graph.
20940Sstevel@tonic-gate  * Returns
20950Sstevel@tonic-gate  *   0 - success
20960Sstevel@tonic-gate  *   ENOENT - service indicated by fmri does not exist
20970Sstevel@tonic-gate  *
20980Sstevel@tonic-gate  * In both cases *reboundp will be B_TRUE if the handle was rebound, or B_FALSE
20990Sstevel@tonic-gate  * otherwise.
21000Sstevel@tonic-gate  */
21010Sstevel@tonic-gate static int
21020Sstevel@tonic-gate add_service(const char *fmri, scf_handle_t *h, boolean_t *reboundp)
21030Sstevel@tonic-gate {
21040Sstevel@tonic-gate 	scf_service_t *svc;
21050Sstevel@tonic-gate 	scf_instance_t *inst;
21060Sstevel@tonic-gate 	scf_iter_t *iter;
21070Sstevel@tonic-gate 	char *inst_fmri;
21080Sstevel@tonic-gate 	int ret, r;
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 	*reboundp = B_FALSE;
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 	svc = safe_scf_service_create(h);
21130Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
21140Sstevel@tonic-gate 	iter = safe_scf_iter_create(h);
21150Sstevel@tonic-gate 	inst_fmri = startd_alloc(max_scf_fmri_size);
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate rebound:
21180Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
21190Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) != 0) {
21200Sstevel@tonic-gate 		switch (scf_error()) {
21210Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
21220Sstevel@tonic-gate 		default:
21230Sstevel@tonic-gate 			libscf_handle_rebind(h);
21240Sstevel@tonic-gate 			*reboundp = B_TRUE;
21250Sstevel@tonic-gate 			goto rebound;
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
21280Sstevel@tonic-gate 			ret = ENOENT;
21290Sstevel@tonic-gate 			goto out;
21300Sstevel@tonic-gate 
21310Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
21320Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
21330Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
21340Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
21350Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
21360Sstevel@tonic-gate 		}
21370Sstevel@tonic-gate 	}
21380Sstevel@tonic-gate 
21390Sstevel@tonic-gate 	if (scf_iter_service_instances(iter, svc) != 0) {
21400Sstevel@tonic-gate 		switch (scf_error()) {
21410Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
21420Sstevel@tonic-gate 		default:
21430Sstevel@tonic-gate 			libscf_handle_rebind(h);
21440Sstevel@tonic-gate 			*reboundp = B_TRUE;
21450Sstevel@tonic-gate 			goto rebound;
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
21480Sstevel@tonic-gate 			ret = ENOENT;
21490Sstevel@tonic-gate 			goto out;
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
21520Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
21530Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
21545040Swesolows 			bad_error("scf_iter_service_instances", scf_error());
21550Sstevel@tonic-gate 		}
21560Sstevel@tonic-gate 	}
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 	for (;;) {
21590Sstevel@tonic-gate 		r = scf_iter_next_instance(iter, inst);
21600Sstevel@tonic-gate 		if (r == 0)
21610Sstevel@tonic-gate 			break;
21620Sstevel@tonic-gate 		if (r != 1) {
21630Sstevel@tonic-gate 			switch (scf_error()) {
21640Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
21650Sstevel@tonic-gate 			default:
21660Sstevel@tonic-gate 				libscf_handle_rebind(h);
21670Sstevel@tonic-gate 				*reboundp = B_TRUE;
21680Sstevel@tonic-gate 				goto rebound;
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
21710Sstevel@tonic-gate 				ret = ENOENT;
21720Sstevel@tonic-gate 				goto out;
21730Sstevel@tonic-gate 
21740Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
21750Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
21760Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
21770Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
21780Sstevel@tonic-gate 				bad_error("scf_iter_next_instance",
21790Sstevel@tonic-gate 				    scf_error());
21800Sstevel@tonic-gate 			}
21810Sstevel@tonic-gate 		}
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 		if (scf_instance_to_fmri(inst, inst_fmri, max_scf_fmri_size) <
21840Sstevel@tonic-gate 		    0) {
21850Sstevel@tonic-gate 			switch (scf_error()) {
21860Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
21870Sstevel@tonic-gate 				libscf_handle_rebind(h);
21880Sstevel@tonic-gate 				*reboundp = B_TRUE;
21890Sstevel@tonic-gate 				goto rebound;
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
21920Sstevel@tonic-gate 				continue;
21930Sstevel@tonic-gate 
21940Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
21950Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
21960Sstevel@tonic-gate 				bad_error("scf_instance_to_fmri", scf_error());
21970Sstevel@tonic-gate 			}
21980Sstevel@tonic-gate 		}
21990Sstevel@tonic-gate 
22000Sstevel@tonic-gate 		r = dgraph_add_instance(inst_fmri, inst, B_FALSE);
22010Sstevel@tonic-gate 		switch (r) {
22020Sstevel@tonic-gate 		case 0:
22030Sstevel@tonic-gate 		case ECANCELED:
22040Sstevel@tonic-gate 			break;
22050Sstevel@tonic-gate 
22060Sstevel@tonic-gate 		case EEXIST:
22070Sstevel@tonic-gate 			continue;
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 		case ECONNABORTED:
22100Sstevel@tonic-gate 			libscf_handle_rebind(h);
22110Sstevel@tonic-gate 			*reboundp = B_TRUE;
22120Sstevel@tonic-gate 			goto rebound;
22130Sstevel@tonic-gate 
22140Sstevel@tonic-gate 		case EINVAL:
22150Sstevel@tonic-gate 		default:
22160Sstevel@tonic-gate 			bad_error("dgraph_add_instance", r);
22170Sstevel@tonic-gate 		}
22180Sstevel@tonic-gate 	}
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 	ret = 0;
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate out:
22230Sstevel@tonic-gate 	startd_free(inst_fmri, max_scf_fmri_size);
22240Sstevel@tonic-gate 	scf_iter_destroy(iter);
22250Sstevel@tonic-gate 	scf_instance_destroy(inst);
22260Sstevel@tonic-gate 	scf_service_destroy(svc);
22270Sstevel@tonic-gate 	return (ret);
22280Sstevel@tonic-gate }
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate struct depfmri_info {
22310Sstevel@tonic-gate 	graph_vertex_t	*v;		/* GVT_GROUP vertex */
22320Sstevel@tonic-gate 	gv_type_t	type;		/* type of dependency */
22330Sstevel@tonic-gate 	const char	*inst_fmri;	/* FMRI of parental GVT_INST vert. */
22340Sstevel@tonic-gate 	const char	*pg_name;	/* Name of dependency pg */
22350Sstevel@tonic-gate 	scf_handle_t	*h;
22360Sstevel@tonic-gate 	int		err;		/* return error code */
22370Sstevel@tonic-gate 	int		**pathp;	/* return circular dependency path */
22380Sstevel@tonic-gate };
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate /*
22410Sstevel@tonic-gate  * Find or create a vertex for fmri and make info->v depend on it.
22420Sstevel@tonic-gate  * Returns
22430Sstevel@tonic-gate  *   0 - success
22440Sstevel@tonic-gate  *   nonzero - failure
22450Sstevel@tonic-gate  *
22460Sstevel@tonic-gate  * On failure, sets info->err to
22470Sstevel@tonic-gate  *   EINVAL - fmri is invalid
22480Sstevel@tonic-gate  *	      fmri does not match info->type
22490Sstevel@tonic-gate  *   ELOOP - Adding the dependency creates a circular dependency.  *info->pathp
22500Sstevel@tonic-gate  *	     will point to an array of the ids of the members of the cycle.
22510Sstevel@tonic-gate  *   ECONNABORTED - repository connection was broken
22520Sstevel@tonic-gate  *   ECONNRESET - succeeded, but repository connection was reset
22530Sstevel@tonic-gate  */
22540Sstevel@tonic-gate static int
22550Sstevel@tonic-gate process_dependency_fmri(const char *fmri, struct depfmri_info *info)
22560Sstevel@tonic-gate {
22570Sstevel@tonic-gate 	int err;
22580Sstevel@tonic-gate 	graph_vertex_t *depgroup_v, *v;
22590Sstevel@tonic-gate 	char *fmri_copy, *cfmri;
22600Sstevel@tonic-gate 	size_t fmri_copy_sz;
22610Sstevel@tonic-gate 	const char *scope, *service, *instance, *pg;
22620Sstevel@tonic-gate 	scf_instance_t *inst;
22630Sstevel@tonic-gate 	boolean_t rebound;
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 	/* Get or create vertex for FMRI */
22680Sstevel@tonic-gate 	depgroup_v = info->v;
22690Sstevel@tonic-gate 
22700Sstevel@tonic-gate 	if (strncmp(fmri, "file:", sizeof ("file:") - 1) == 0) {
22710Sstevel@tonic-gate 		if (info->type != GVT_FILE) {
22720Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
22730Sstevel@tonic-gate 			    "FMRI \"%s\" is not allowed for the \"%s\" "
22740Sstevel@tonic-gate 			    "dependency's type of instance %s.\n", fmri,
22750Sstevel@tonic-gate 			    info->pg_name, info->inst_fmri);
22760Sstevel@tonic-gate 			return (info->err = EINVAL);
22770Sstevel@tonic-gate 		}
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(fmri, info->type, 0,
22800Sstevel@tonic-gate 		    RERR_NONE, &v);
22810Sstevel@tonic-gate 		switch (err) {
22820Sstevel@tonic-gate 		case 0:
22830Sstevel@tonic-gate 			break;
22840Sstevel@tonic-gate 
22850Sstevel@tonic-gate 		case EEXIST:
22860Sstevel@tonic-gate 			assert(v->gv_type == GVT_FILE);
22870Sstevel@tonic-gate 			break;
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 		case EINVAL:		/* prevented above */
22900Sstevel@tonic-gate 		default:
22910Sstevel@tonic-gate 			bad_error("graph_insert_vertex_unconfigured", err);
22920Sstevel@tonic-gate 		}
22930Sstevel@tonic-gate 	} else {
22940Sstevel@tonic-gate 		if (info->type != GVT_INST) {
22950Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
22960Sstevel@tonic-gate 			    "FMRI \"%s\" is not allowed for the \"%s\" "
22970Sstevel@tonic-gate 			    "dependency's type of instance %s.\n", fmri,
22980Sstevel@tonic-gate 			    info->pg_name, info->inst_fmri);
22990Sstevel@tonic-gate 			return (info->err = EINVAL);
23000Sstevel@tonic-gate 		}
23010Sstevel@tonic-gate 
23020Sstevel@tonic-gate 		/*
23030Sstevel@tonic-gate 		 * We must canonify fmri & add a vertex for it.
23040Sstevel@tonic-gate 		 */
23050Sstevel@tonic-gate 		fmri_copy_sz = strlen(fmri) + 1;
23060Sstevel@tonic-gate 		fmri_copy = startd_alloc(fmri_copy_sz);
23070Sstevel@tonic-gate 		(void) strcpy(fmri_copy, fmri);
23080Sstevel@tonic-gate 
23090Sstevel@tonic-gate 		/* Determine if the FMRI is a property group or instance */
23100Sstevel@tonic-gate 		if (scf_parse_svc_fmri(fmri_copy, &scope, &service,
23110Sstevel@tonic-gate 		    &instance, &pg, NULL) != 0) {
23120Sstevel@tonic-gate 			startd_free(fmri_copy, fmri_copy_sz);
23130Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
23140Sstevel@tonic-gate 			    "Dependency \"%s\" of %s has invalid FMRI "
23150Sstevel@tonic-gate 			    "\"%s\".\n", info->pg_name, info->inst_fmri,
23160Sstevel@tonic-gate 			    fmri);
23170Sstevel@tonic-gate 			return (info->err = EINVAL);
23180Sstevel@tonic-gate 		}
23190Sstevel@tonic-gate 
23200Sstevel@tonic-gate 		if (service == NULL || pg != NULL) {
23210Sstevel@tonic-gate 			startd_free(fmri_copy, fmri_copy_sz);
23220Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
23230Sstevel@tonic-gate 			    "Dependency \"%s\" of %s does not designate a "
23240Sstevel@tonic-gate 			    "service or instance.\n", info->pg_name,
23250Sstevel@tonic-gate 			    info->inst_fmri);
23260Sstevel@tonic-gate 			return (info->err = EINVAL);
23270Sstevel@tonic-gate 		}
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 		if (scope == NULL || strcmp(scope, SCF_SCOPE_LOCAL) == 0) {
23300Sstevel@tonic-gate 			cfmri = uu_msprintf("svc:/%s%s%s",
23310Sstevel@tonic-gate 			    service, instance ? ":" : "", instance ? instance :
23320Sstevel@tonic-gate 			    "");
23330Sstevel@tonic-gate 		} else {
23340Sstevel@tonic-gate 			cfmri = uu_msprintf("svc://%s/%s%s%s",
23350Sstevel@tonic-gate 			    scope, service, instance ? ":" : "", instance ?
23360Sstevel@tonic-gate 			    instance : "");
23370Sstevel@tonic-gate 		}
23380Sstevel@tonic-gate 
23390Sstevel@tonic-gate 		startd_free(fmri_copy, fmri_copy_sz);
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(cfmri, instance ?
23420Sstevel@tonic-gate 		    GVT_INST : GVT_SVC, instance ? 0 : DEPGRP_REQUIRE_ANY,
23430Sstevel@tonic-gate 		    RERR_NONE, &v);
23440Sstevel@tonic-gate 		uu_free(cfmri);
23450Sstevel@tonic-gate 		switch (err) {
23460Sstevel@tonic-gate 		case 0:
23470Sstevel@tonic-gate 			break;
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate 		case EEXIST:
23500Sstevel@tonic-gate 			/* Verify v. */
23510Sstevel@tonic-gate 			if (instance != NULL)
23520Sstevel@tonic-gate 				assert(v->gv_type == GVT_INST);
23530Sstevel@tonic-gate 			else
23540Sstevel@tonic-gate 				assert(v->gv_type == GVT_SVC);
23550Sstevel@tonic-gate 			break;
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate 		default:
23580Sstevel@tonic-gate 			bad_error("graph_insert_vertex_unconfigured", err);
23590Sstevel@tonic-gate 		}
23600Sstevel@tonic-gate 	}
23610Sstevel@tonic-gate 
23620Sstevel@tonic-gate 	/* Add dependency from depgroup_v to new vertex */
23630Sstevel@tonic-gate 	info->err = graph_insert_dependency(depgroup_v, v, info->pathp);
23640Sstevel@tonic-gate 	switch (info->err) {
23650Sstevel@tonic-gate 	case 0:
23660Sstevel@tonic-gate 		break;
23670Sstevel@tonic-gate 
23680Sstevel@tonic-gate 	case ELOOP:
23690Sstevel@tonic-gate 		return (ELOOP);
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate 	default:
23720Sstevel@tonic-gate 		bad_error("graph_insert_dependency", info->err);
23730Sstevel@tonic-gate 	}
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate 	/* This must be after we insert the dependency, to avoid looping. */
23760Sstevel@tonic-gate 	switch (v->gv_type) {
23770Sstevel@tonic-gate 	case GVT_INST:
23780Sstevel@tonic-gate 		if ((v->gv_flags & GV_CONFIGURED) != 0)
23790Sstevel@tonic-gate 			break;
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 		inst = safe_scf_instance_create(info->h);
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate 		rebound = B_FALSE;
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate rebound:
23860Sstevel@tonic-gate 		err = libscf_lookup_instance(v->gv_name, inst);
23870Sstevel@tonic-gate 		switch (err) {
23880Sstevel@tonic-gate 		case 0:
23890Sstevel@tonic-gate 			err = configure_vertex(v, inst);
23900Sstevel@tonic-gate 			switch (err) {
23910Sstevel@tonic-gate 			case 0:
23920Sstevel@tonic-gate 			case ECANCELED:
23930Sstevel@tonic-gate 				break;
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate 			case ECONNABORTED:
23960Sstevel@tonic-gate 				libscf_handle_rebind(info->h);
23970Sstevel@tonic-gate 				rebound = B_TRUE;
23980Sstevel@tonic-gate 				goto rebound;
23990Sstevel@tonic-gate 
24000Sstevel@tonic-gate 			default:
24010Sstevel@tonic-gate 				bad_error("configure_vertex", err);
24020Sstevel@tonic-gate 			}
24030Sstevel@tonic-gate 			break;
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate 		case ENOENT:
24060Sstevel@tonic-gate 			break;
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 		case ECONNABORTED:
24090Sstevel@tonic-gate 			libscf_handle_rebind(info->h);
24100Sstevel@tonic-gate 			rebound = B_TRUE;
24110Sstevel@tonic-gate 			goto rebound;
24120Sstevel@tonic-gate 
24130Sstevel@tonic-gate 		case EINVAL:
24140Sstevel@tonic-gate 		case ENOTSUP:
24150Sstevel@tonic-gate 		default:
24160Sstevel@tonic-gate 			bad_error("libscf_fmri_get_instance", err);
24170Sstevel@tonic-gate 		}
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate 		scf_instance_destroy(inst);
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 		if (rebound)
24220Sstevel@tonic-gate 			return (info->err = ECONNRESET);
24230Sstevel@tonic-gate 		break;
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 	case GVT_SVC:
24260Sstevel@tonic-gate 		(void) add_service(v->gv_name, info->h, &rebound);
24270Sstevel@tonic-gate 		if (rebound)
24280Sstevel@tonic-gate 			return (info->err = ECONNRESET);
24290Sstevel@tonic-gate 	}
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate 	return (0);
24320Sstevel@tonic-gate }
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate struct deppg_info {
24350Sstevel@tonic-gate 	graph_vertex_t	*v;		/* GVT_INST vertex */
24360Sstevel@tonic-gate 	int		err;		/* return error */
24370Sstevel@tonic-gate 	int		**pathp;	/* return circular dependency path */
24380Sstevel@tonic-gate };
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate /*
24410Sstevel@tonic-gate  * Make info->v depend on a new GVT_GROUP node for this property group,
24420Sstevel@tonic-gate  * and then call process_dependency_fmri() for the values of the entity
24430Sstevel@tonic-gate  * property.  Return 0 on success, or if something goes wrong return nonzero
24440Sstevel@tonic-gate  * and set info->err to ECONNABORTED, EINVAL, or the error code returned by
24450Sstevel@tonic-gate  * process_dependency_fmri().
24460Sstevel@tonic-gate  */
24470Sstevel@tonic-gate static int
24480Sstevel@tonic-gate process_dependency_pg(scf_propertygroup_t *pg, struct deppg_info *info)
24490Sstevel@tonic-gate {
24500Sstevel@tonic-gate 	scf_handle_t *h;
24510Sstevel@tonic-gate 	depgroup_type_t deptype;
24522704Srm88369 	restarter_error_t rerr;
24530Sstevel@tonic-gate 	struct depfmri_info linfo;
24540Sstevel@tonic-gate 	char *fmri, *pg_name;
24550Sstevel@tonic-gate 	size_t fmri_sz;
24560Sstevel@tonic-gate 	graph_vertex_t *depgrp;
24570Sstevel@tonic-gate 	scf_property_t *prop;
24580Sstevel@tonic-gate 	int err;
24590Sstevel@tonic-gate 	int empty;
24600Sstevel@tonic-gate 	scf_error_t scferr;
24610Sstevel@tonic-gate 	ssize_t len;
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate 	h = scf_pg_handle(pg);
24660Sstevel@tonic-gate 
24670Sstevel@tonic-gate 	pg_name = startd_alloc(max_scf_name_size);
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 	len = scf_pg_get_name(pg, pg_name, max_scf_name_size);
24700Sstevel@tonic-gate 	if (len < 0) {
24710Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
24720Sstevel@tonic-gate 		switch (scf_error()) {
24730Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
24740Sstevel@tonic-gate 		default:
24750Sstevel@tonic-gate 			return (info->err = ECONNABORTED);
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
24780Sstevel@tonic-gate 			return (info->err = 0);
24790Sstevel@tonic-gate 
24800Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
24810Sstevel@tonic-gate 			bad_error("scf_pg_get_name", scf_error());
24820Sstevel@tonic-gate 		}
24830Sstevel@tonic-gate 	}
24840Sstevel@tonic-gate 
24850Sstevel@tonic-gate 	/*
24860Sstevel@tonic-gate 	 * Skip over empty dependency groups.  Since dependency property
24870Sstevel@tonic-gate 	 * groups are updated atomically, they are either empty or
24880Sstevel@tonic-gate 	 * fully populated.
24890Sstevel@tonic-gate 	 */
24900Sstevel@tonic-gate 	empty = depgroup_empty(h, pg);
24910Sstevel@tonic-gate 	if (empty < 0) {
24920Sstevel@tonic-gate 		log_error(LOG_INFO,
24930Sstevel@tonic-gate 		    "Error reading dependency group \"%s\" of %s: %s\n",
24940Sstevel@tonic-gate 		    pg_name, info->v->gv_name, scf_strerror(scf_error()));
24950Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
24960Sstevel@tonic-gate 		return (info->err = EINVAL);
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 	} else if (empty == 1) {
24990Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
25000Sstevel@tonic-gate 		    "Ignoring empty dependency group \"%s\" of %s\n",
25010Sstevel@tonic-gate 		    pg_name, info->v->gv_name);
25020Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
25030Sstevel@tonic-gate 		return (info->err = 0);
25040Sstevel@tonic-gate 	}
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 	fmri_sz = strlen(info->v->gv_name) + 1 + len + 1;
25070Sstevel@tonic-gate 	fmri = startd_alloc(fmri_sz);
25080Sstevel@tonic-gate 
25090Sstevel@tonic-gate 	(void) snprintf(fmri, max_scf_name_size, "%s>%s", info->v->gv_name,
25100Sstevel@tonic-gate 	    pg_name);
25110Sstevel@tonic-gate 
25120Sstevel@tonic-gate 	/* Validate the pg before modifying the graph */
25130Sstevel@tonic-gate 	deptype = depgroup_read_grouping(h, pg);
25140Sstevel@tonic-gate 	if (deptype == DEPGRP_UNSUPPORTED) {
25150Sstevel@tonic-gate 		log_error(LOG_INFO,
25160Sstevel@tonic-gate 		    "Dependency \"%s\" of %s has an unknown grouping value.\n",
25170Sstevel@tonic-gate 		    pg_name, info->v->gv_name);
25180Sstevel@tonic-gate 		startd_free(fmri, fmri_sz);
25190Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
25200Sstevel@tonic-gate 		return (info->err = EINVAL);
25210Sstevel@tonic-gate 	}
25220Sstevel@tonic-gate 
25232704Srm88369 	rerr = depgroup_read_restart(h, pg);
25242704Srm88369 	if (rerr == RERR_UNSUPPORTED) {
25252704Srm88369 		log_error(LOG_INFO,
25262704Srm88369 		    "Dependency \"%s\" of %s has an unknown restart_on value."
25272704Srm88369 		    "\n", pg_name, info->v->gv_name);
25282704Srm88369 		startd_free(fmri, fmri_sz);
25292704Srm88369 		startd_free(pg_name, max_scf_name_size);
25302704Srm88369 		return (info->err = EINVAL);
25312704Srm88369 	}
25322704Srm88369 
25330Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0) {
25360Sstevel@tonic-gate 		scferr = scf_error();
25370Sstevel@tonic-gate 		scf_property_destroy(prop);
25380Sstevel@tonic-gate 		if (scferr == SCF_ERROR_DELETED) {
25390Sstevel@tonic-gate 			startd_free(fmri, fmri_sz);
25400Sstevel@tonic-gate 			startd_free(pg_name, max_scf_name_size);
25410Sstevel@tonic-gate 			return (info->err = 0);
25420Sstevel@tonic-gate 		} else if (scferr != SCF_ERROR_NOT_FOUND) {
25430Sstevel@tonic-gate 			startd_free(fmri, fmri_sz);
25440Sstevel@tonic-gate 			startd_free(pg_name, max_scf_name_size);
25450Sstevel@tonic-gate 			return (info->err = ECONNABORTED);
25460Sstevel@tonic-gate 		}
25470Sstevel@tonic-gate 
25480Sstevel@tonic-gate 		log_error(LOG_INFO,
25490Sstevel@tonic-gate 		    "Dependency \"%s\" of %s is missing a \"%s\" property.\n",
25500Sstevel@tonic-gate 		    pg_name, info->v->gv_name, SCF_PROPERTY_ENTITIES);
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate 		startd_free(fmri, fmri_sz);
25530Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
25540Sstevel@tonic-gate 
25550Sstevel@tonic-gate 		return (info->err = EINVAL);
25560Sstevel@tonic-gate 	}
25570Sstevel@tonic-gate 
25580Sstevel@tonic-gate 	/* Create depgroup vertex for pg */
25590Sstevel@tonic-gate 	err = graph_insert_vertex_unconfigured(fmri, GVT_GROUP, deptype,
25602704Srm88369 	    rerr, &depgrp);
25610Sstevel@tonic-gate 	assert(err == 0);
25620Sstevel@tonic-gate 	startd_free(fmri, fmri_sz);
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 	/* Add dependency from inst vertex to new vertex */
25650Sstevel@tonic-gate 	err = graph_insert_dependency(info->v, depgrp, info->pathp);
25660Sstevel@tonic-gate 	/* ELOOP can't happen because this should be a new vertex */
25670Sstevel@tonic-gate 	assert(err == 0);
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 	linfo.v = depgrp;
25700Sstevel@tonic-gate 	linfo.type = depgroup_read_scheme(h, pg);
25710Sstevel@tonic-gate 	linfo.inst_fmri = info->v->gv_name;
25720Sstevel@tonic-gate 	linfo.pg_name = pg_name;
25730Sstevel@tonic-gate 	linfo.h = h;
25740Sstevel@tonic-gate 	linfo.err = 0;
25750Sstevel@tonic-gate 	linfo.pathp = info->pathp;
25760Sstevel@tonic-gate 	err = walk_property_astrings(prop, (callback_t)process_dependency_fmri,
25770Sstevel@tonic-gate 	    &linfo);
25780Sstevel@tonic-gate 
25790Sstevel@tonic-gate 	scf_property_destroy(prop);
25800Sstevel@tonic-gate 	startd_free(pg_name, max_scf_name_size);
25810Sstevel@tonic-gate 
25820Sstevel@tonic-gate 	switch (err) {
25830Sstevel@tonic-gate 	case 0:
25840Sstevel@tonic-gate 	case EINTR:
25850Sstevel@tonic-gate 		return (info->err = linfo.err);
25860Sstevel@tonic-gate 
25870Sstevel@tonic-gate 	case ECONNABORTED:
25880Sstevel@tonic-gate 	case EINVAL:
25890Sstevel@tonic-gate 		return (info->err = err);
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate 	case ECANCELED:
25920Sstevel@tonic-gate 		return (info->err = 0);
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate 	case ECONNRESET:
25950Sstevel@tonic-gate 		return (info->err = ECONNABORTED);
25960Sstevel@tonic-gate 
25970Sstevel@tonic-gate 	default:
25980Sstevel@tonic-gate 		bad_error("walk_property_astrings", err);
25990Sstevel@tonic-gate 		/* NOTREACHED */
26000Sstevel@tonic-gate 	}
26010Sstevel@tonic-gate }
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate /*
26040Sstevel@tonic-gate  * Build the dependency info for v from the repository.  Returns 0 on success,
26050Sstevel@tonic-gate  * ECONNABORTED on repository disconnection, EINVAL if the repository
26060Sstevel@tonic-gate  * configuration is invalid, and ELOOP if a dependency would cause a cycle.
26070Sstevel@tonic-gate  * In the last case, *pathp will point to a -1-terminated array of ids which
26080Sstevel@tonic-gate  * constitute the rest of the dependency cycle.
26090Sstevel@tonic-gate  */
26100Sstevel@tonic-gate static int
26110Sstevel@tonic-gate set_dependencies(graph_vertex_t *v, scf_instance_t *inst, int **pathp)
26120Sstevel@tonic-gate {
26130Sstevel@tonic-gate 	struct deppg_info info;
26140Sstevel@tonic-gate 	int err;
26150Sstevel@tonic-gate 	uint_t old_configured;
26160Sstevel@tonic-gate 
26170Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate 	/*
26200Sstevel@tonic-gate 	 * Mark the vertex as configured during dependency insertion to avoid
26210Sstevel@tonic-gate 	 * dependency cycles (which can appear in the graph if one of the
26220Sstevel@tonic-gate 	 * vertices is an exclusion-group).
26230Sstevel@tonic-gate 	 */
26240Sstevel@tonic-gate 	old_configured = v->gv_flags & GV_CONFIGURED;
26250Sstevel@tonic-gate 	v->gv_flags |= GV_CONFIGURED;
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate 	info.err = 0;
26280Sstevel@tonic-gate 	info.v = v;
26290Sstevel@tonic-gate 	info.pathp = pathp;
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate 	err = walk_dependency_pgs(inst, (callback_t)process_dependency_pg,
26320Sstevel@tonic-gate 	    &info);
26330Sstevel@tonic-gate 
26340Sstevel@tonic-gate 	if (!old_configured)
26350Sstevel@tonic-gate 		v->gv_flags &= ~GV_CONFIGURED;
26360Sstevel@tonic-gate 
26370Sstevel@tonic-gate 	switch (err) {
26380Sstevel@tonic-gate 	case 0:
26390Sstevel@tonic-gate 	case EINTR:
26400Sstevel@tonic-gate 		return (info.err);
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	case ECONNABORTED:
26430Sstevel@tonic-gate 		return (ECONNABORTED);
26440Sstevel@tonic-gate 
26450Sstevel@tonic-gate 	case ECANCELED:
26460Sstevel@tonic-gate 		/* Should get delete event, so return 0. */
26470Sstevel@tonic-gate 		return (0);
26480Sstevel@tonic-gate 
26490Sstevel@tonic-gate 	default:
26500Sstevel@tonic-gate 		bad_error("walk_dependency_pgs", err);
26510Sstevel@tonic-gate 		/* NOTREACHED */
26520Sstevel@tonic-gate 	}
26530Sstevel@tonic-gate }
26540Sstevel@tonic-gate 
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate static void
26570Sstevel@tonic-gate handle_cycle(const char *fmri, int *path)
26580Sstevel@tonic-gate {
26590Sstevel@tonic-gate 	const char *cp;
26600Sstevel@tonic-gate 	size_t sz;
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
26630Sstevel@tonic-gate 
26640Sstevel@tonic-gate 	path_to_str(path, (char **)&cp, &sz);
26650Sstevel@tonic-gate 
26661958Slianep 	log_error(LOG_ERR, "Transitioning %s to maintenance "
26671958Slianep 	    "because it completes a dependency cycle (see svcs -xv for "
26681958Slianep 	    "details):\n%s", fmri ? fmri : "?", cp);
26690Sstevel@tonic-gate 
26700Sstevel@tonic-gate 	startd_free((void *)cp, sz);
26710Sstevel@tonic-gate }
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate /*
26741712Srm88369  * Increment the vertex's reference count to prevent the vertex removal
26751712Srm88369  * from the dgraph.
26761712Srm88369  */
26771712Srm88369 static void
26781712Srm88369 vertex_ref(graph_vertex_t *v)
26791712Srm88369 {
26801712Srm88369 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
26811712Srm88369 
26821712Srm88369 	v->gv_refs++;
26831712Srm88369 }
26841712Srm88369 
26851712Srm88369 /*
26861712Srm88369  * Decrement the vertex's reference count and remove the vertex from
26871712Srm88369  * the dgraph when possible.
26881712Srm88369  *
26891712Srm88369  * Return VERTEX_REMOVED when the vertex has been removed otherwise
26901712Srm88369  * return VERTEX_INUSE.
26910Sstevel@tonic-gate  */
26920Sstevel@tonic-gate static int
26931712Srm88369 vertex_unref(graph_vertex_t *v)
26941712Srm88369 {
26951712Srm88369 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
26961712Srm88369 	assert(v->gv_refs > 0);
26971712Srm88369 
26981712Srm88369 	v->gv_refs--;
26991712Srm88369 
27001712Srm88369 	return (free_if_unrefed(v));
27011712Srm88369 }
27021712Srm88369 
27031712Srm88369 /*
27041712Srm88369  * When run on the dependencies of a vertex, populates list with
27051712Srm88369  * graph_edge_t's which point to the service vertices or the instance
27061712Srm88369  * vertices (no GVT_GROUP nodes) on which the vertex depends.
27071712Srm88369  *
27081712Srm88369  * Increment the vertex's reference count once the vertex is inserted
27091712Srm88369  * in the list. The vertex won't be able to be deleted from the dgraph
27101712Srm88369  * while it is referenced.
27111712Srm88369  */
27121712Srm88369 static int
27131712Srm88369 append_svcs_or_insts(graph_edge_t *e, uu_list_t *list)
27140Sstevel@tonic-gate {
27150Sstevel@tonic-gate 	graph_vertex_t *v = e->ge_vertex;
27160Sstevel@tonic-gate 	graph_edge_t *new;
27170Sstevel@tonic-gate 	int r;
27180Sstevel@tonic-gate 
27190Sstevel@tonic-gate 	switch (v->gv_type) {
27200Sstevel@tonic-gate 	case GVT_INST:
27210Sstevel@tonic-gate 	case GVT_SVC:
27220Sstevel@tonic-gate 		break;
27230Sstevel@tonic-gate 
27240Sstevel@tonic-gate 	case GVT_GROUP:
27250Sstevel@tonic-gate 		r = uu_list_walk(v->gv_dependencies,
27261712Srm88369 		    (uu_walk_fn_t *)append_svcs_or_insts, list, 0);
27270Sstevel@tonic-gate 		assert(r == 0);
27280Sstevel@tonic-gate 		return (UU_WALK_NEXT);
27290Sstevel@tonic-gate 
27300Sstevel@tonic-gate 	case GVT_FILE:
27310Sstevel@tonic-gate 		return (UU_WALK_NEXT);
27320Sstevel@tonic-gate 
27330Sstevel@tonic-gate 	default:
27340Sstevel@tonic-gate #ifndef NDEBUG
27350Sstevel@tonic-gate 		uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
27360Sstevel@tonic-gate 		    __LINE__, v->gv_type);
27370Sstevel@tonic-gate #endif
27380Sstevel@tonic-gate 		abort();
27390Sstevel@tonic-gate 	}
27400Sstevel@tonic-gate 
27410Sstevel@tonic-gate 	new = startd_alloc(sizeof (*new));
27420Sstevel@tonic-gate 	new->ge_vertex = v;
27430Sstevel@tonic-gate 	uu_list_node_init(new, &new->ge_link, graph_edge_pool);
27440Sstevel@tonic-gate 	r = uu_list_insert_before(list, NULL, new);
27450Sstevel@tonic-gate 	assert(r == 0);
27461712Srm88369 
27471712Srm88369 	/*
27481712Srm88369 	 * Because we are inserting the vertex in a list, we don't want
27491712Srm88369 	 * the vertex to be freed while the list is in use. In order to
27501712Srm88369 	 * achieve that, increment the vertex's reference count.
27511712Srm88369 	 */
27521712Srm88369 	vertex_ref(v);
27531712Srm88369 
27540Sstevel@tonic-gate 	return (UU_WALK_NEXT);
27550Sstevel@tonic-gate }
27560Sstevel@tonic-gate 
27570Sstevel@tonic-gate static boolean_t
27580Sstevel@tonic-gate should_be_in_subgraph(graph_vertex_t *v)
27590Sstevel@tonic-gate {
27600Sstevel@tonic-gate 	graph_edge_t *e;
27610Sstevel@tonic-gate 
27620Sstevel@tonic-gate 	if (v == milestone)
27630Sstevel@tonic-gate 		return (B_TRUE);
27640Sstevel@tonic-gate 
27650Sstevel@tonic-gate 	/*
27660Sstevel@tonic-gate 	 * v is in the subgraph if any of its dependents are in the subgraph.
27670Sstevel@tonic-gate 	 * Except for EXCLUDE_ALL dependents.  And OPTIONAL dependents only
27680Sstevel@tonic-gate 	 * count if we're enabled.
27690Sstevel@tonic-gate 	 */
27700Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
27710Sstevel@tonic-gate 	    e != NULL;
27720Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e)) {
27730Sstevel@tonic-gate 		graph_vertex_t *dv = e->ge_vertex;
27740Sstevel@tonic-gate 
27750Sstevel@tonic-gate 		if (!(dv->gv_flags & GV_INSUBGRAPH))
27760Sstevel@tonic-gate 			continue;
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 		/*
27790Sstevel@tonic-gate 		 * Don't include instances that are optional and disabled.
27800Sstevel@tonic-gate 		 */
27810Sstevel@tonic-gate 		if (v->gv_type == GVT_INST && dv->gv_type == GVT_SVC) {
27820Sstevel@tonic-gate 
27830Sstevel@tonic-gate 			int in = 0;
27840Sstevel@tonic-gate 			graph_edge_t *ee;
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate 			for (ee = uu_list_first(dv->gv_dependents);
27870Sstevel@tonic-gate 			    ee != NULL;
27880Sstevel@tonic-gate 			    ee = uu_list_next(dv->gv_dependents, ee)) {
27890Sstevel@tonic-gate 
27900Sstevel@tonic-gate 				graph_vertex_t *ddv = e->ge_vertex;
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate 				if (ddv->gv_type == GVT_GROUP &&
27930Sstevel@tonic-gate 				    ddv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
27940Sstevel@tonic-gate 					continue;
27950Sstevel@tonic-gate 
27960Sstevel@tonic-gate 				if (ddv->gv_type == GVT_GROUP &&
27970Sstevel@tonic-gate 				    ddv->gv_depgroup == DEPGRP_OPTIONAL_ALL &&
27980Sstevel@tonic-gate 				    !(v->gv_flags & GV_ENBLD_NOOVR))
27990Sstevel@tonic-gate 					continue;
28000Sstevel@tonic-gate 
28010Sstevel@tonic-gate 				in = 1;
28020Sstevel@tonic-gate 			}
28030Sstevel@tonic-gate 			if (!in)
28040Sstevel@tonic-gate 				continue;
28050Sstevel@tonic-gate 		}
28060Sstevel@tonic-gate 		if (v->gv_type == GVT_INST &&
28070Sstevel@tonic-gate 		    dv->gv_type == GVT_GROUP &&
28080Sstevel@tonic-gate 		    dv->gv_depgroup == DEPGRP_OPTIONAL_ALL &&
28090Sstevel@tonic-gate 		    !(v->gv_flags & GV_ENBLD_NOOVR))
28100Sstevel@tonic-gate 			continue;
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate 		/* Don't include excluded services and instances */
28130Sstevel@tonic-gate 		if (dv->gv_type == GVT_GROUP &&
28140Sstevel@tonic-gate 		    dv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
28150Sstevel@tonic-gate 			continue;
28160Sstevel@tonic-gate 
28170Sstevel@tonic-gate 		return (B_TRUE);
28180Sstevel@tonic-gate 	}
28190Sstevel@tonic-gate 
28200Sstevel@tonic-gate 	return (B_FALSE);
28210Sstevel@tonic-gate }
28220Sstevel@tonic-gate 
28230Sstevel@tonic-gate /*
28240Sstevel@tonic-gate  * Ensures that GV_INSUBGRAPH is set properly for v and its descendents.  If
28250Sstevel@tonic-gate  * any bits change, manipulate the repository appropriately.  Returns 0 or
28260Sstevel@tonic-gate  * ECONNABORTED.
28270Sstevel@tonic-gate  */
28280Sstevel@tonic-gate static int
28290Sstevel@tonic-gate eval_subgraph(graph_vertex_t *v, scf_handle_t *h)
28300Sstevel@tonic-gate {
28310Sstevel@tonic-gate 	boolean_t old = (v->gv_flags & GV_INSUBGRAPH) != 0;
28320Sstevel@tonic-gate 	boolean_t new;
28330Sstevel@tonic-gate 	graph_edge_t *e;
28340Sstevel@tonic-gate 	scf_instance_t *inst;
28350Sstevel@tonic-gate 	int ret = 0, r;
28360Sstevel@tonic-gate 
28370Sstevel@tonic-gate 	assert(milestone != NULL && milestone != MILESTONE_NONE);
28380Sstevel@tonic-gate 
28390Sstevel@tonic-gate 	new = should_be_in_subgraph(v);
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate 	if (new == old)
28420Sstevel@tonic-gate 		return (0);
28430Sstevel@tonic-gate 
28440Sstevel@tonic-gate 	log_framework(LOG_DEBUG, new ? "Adding %s to the subgraph.\n" :
28450Sstevel@tonic-gate 	    "Removing %s from the subgraph.\n", v->gv_name);
28460Sstevel@tonic-gate 
28470Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_INSUBGRAPH) |
28480Sstevel@tonic-gate 	    (new ? GV_INSUBGRAPH : 0);
28490Sstevel@tonic-gate 
28500Sstevel@tonic-gate 	if (v->gv_type == GVT_INST && (v->gv_flags & GV_CONFIGURED)) {
28510Sstevel@tonic-gate 		int err;
28520Sstevel@tonic-gate 
28530Sstevel@tonic-gate get_inst:
28540Sstevel@tonic-gate 		err = libscf_fmri_get_instance(h, v->gv_name, &inst);
28550Sstevel@tonic-gate 		if (err != 0) {
28560Sstevel@tonic-gate 			switch (err) {
28570Sstevel@tonic-gate 			case ECONNABORTED:
28580Sstevel@tonic-gate 				libscf_handle_rebind(h);
28590Sstevel@tonic-gate 				ret = ECONNABORTED;
28600Sstevel@tonic-gate 				goto get_inst;
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 			case ENOENT:
28630Sstevel@tonic-gate 				break;
28640Sstevel@tonic-gate 
28650Sstevel@tonic-gate 			case EINVAL:
28660Sstevel@tonic-gate 			case ENOTSUP:
28670Sstevel@tonic-gate 			default:
28680Sstevel@tonic-gate 				bad_error("libscf_fmri_get_instance", err);
28690Sstevel@tonic-gate 			}
28700Sstevel@tonic-gate 		} else {
28710Sstevel@tonic-gate 			const char *f;
28720Sstevel@tonic-gate 
28730Sstevel@tonic-gate 			if (new) {
28740Sstevel@tonic-gate 				err = libscf_delete_enable_ovr(inst);
28750Sstevel@tonic-gate 				f = "libscf_delete_enable_ovr";
28760Sstevel@tonic-gate 			} else {
28770Sstevel@tonic-gate 				err = libscf_set_enable_ovr(inst, 0);
28780Sstevel@tonic-gate 				f = "libscf_set_enable_ovr";
28790Sstevel@tonic-gate 			}
28800Sstevel@tonic-gate 			scf_instance_destroy(inst);
28810Sstevel@tonic-gate 			switch (err) {
28820Sstevel@tonic-gate 			case 0:
28830Sstevel@tonic-gate 			case ECANCELED:
28840Sstevel@tonic-gate 				break;
28850Sstevel@tonic-gate 
28860Sstevel@tonic-gate 			case ECONNABORTED:
28870Sstevel@tonic-gate 				libscf_handle_rebind(h);
28880Sstevel@tonic-gate 				/*
28890Sstevel@tonic-gate 				 * We must continue so the graph is updated,
28900Sstevel@tonic-gate 				 * but we must return ECONNABORTED so any
28910Sstevel@tonic-gate 				 * libscf state held by any callers is reset.
28920Sstevel@tonic-gate 				 */
28930Sstevel@tonic-gate 				ret = ECONNABORTED;
28940Sstevel@tonic-gate 				goto get_inst;
28950Sstevel@tonic-gate 
28960Sstevel@tonic-gate 			case EROFS:
28970Sstevel@tonic-gate 			case EPERM:
28980Sstevel@tonic-gate 				log_error(LOG_WARNING,
28990Sstevel@tonic-gate 				    "Could not set %s/%s for %s: %s.\n",
29000Sstevel@tonic-gate 				    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
29010Sstevel@tonic-gate 				    v->gv_name, strerror(err));
29020Sstevel@tonic-gate 				break;
29030Sstevel@tonic-gate 
29040Sstevel@tonic-gate 			default:
29050Sstevel@tonic-gate 				bad_error(f, err);
29060Sstevel@tonic-gate 			}
29070Sstevel@tonic-gate 		}
29080Sstevel@tonic-gate 	}
29090Sstevel@tonic-gate 
29100Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
29110Sstevel@tonic-gate 	    e != NULL;
29120Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
29130Sstevel@tonic-gate 		r = eval_subgraph(e->ge_vertex, h);
29140Sstevel@tonic-gate 		if (r != 0) {
29150Sstevel@tonic-gate 			assert(r == ECONNABORTED);
29160Sstevel@tonic-gate 			ret = ECONNABORTED;
29170Sstevel@tonic-gate 		}
29180Sstevel@tonic-gate 	}
29190Sstevel@tonic-gate 
29200Sstevel@tonic-gate 	return (ret);
29210Sstevel@tonic-gate }
29220Sstevel@tonic-gate 
29230Sstevel@tonic-gate /*
29240Sstevel@tonic-gate  * Delete the (property group) dependencies of v & create new ones based on
29250Sstevel@tonic-gate  * inst.  If doing so would create a cycle, log a message and put the instance
29260Sstevel@tonic-gate  * into maintenance.  Update GV_INSUBGRAPH flags as necessary.  Returns 0 or
29270Sstevel@tonic-gate  * ECONNABORTED.
29280Sstevel@tonic-gate  */
29291958Slianep int
29300Sstevel@tonic-gate refresh_vertex(graph_vertex_t *v, scf_instance_t *inst)
29310Sstevel@tonic-gate {
29320Sstevel@tonic-gate 	int err;
29330Sstevel@tonic-gate 	int *path;
29340Sstevel@tonic-gate 	char *fmri;
29350Sstevel@tonic-gate 	int r;
29360Sstevel@tonic-gate 	scf_handle_t *h = scf_instance_handle(inst);
29370Sstevel@tonic-gate 	uu_list_t *old_deps;
29380Sstevel@tonic-gate 	int ret = 0;
29390Sstevel@tonic-gate 	graph_edge_t *e;
29401712Srm88369 	graph_vertex_t *vv;
29410Sstevel@tonic-gate 
29420Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
29430Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
29440Sstevel@tonic-gate 
29450Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph engine: Refreshing %s.\n", v->gv_name);
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
29480Sstevel@tonic-gate 		/*
29490Sstevel@tonic-gate 		 * In case some of v's dependencies are being deleted we must
29500Sstevel@tonic-gate 		 * make a list of them now for GV_INSUBGRAPH-flag evaluation
29510Sstevel@tonic-gate 		 * after the new dependencies are in place.
29520Sstevel@tonic-gate 		 */
29530Sstevel@tonic-gate 		old_deps = startd_list_create(graph_edge_pool, NULL, 0);
29540Sstevel@tonic-gate 
29550Sstevel@tonic-gate 		err = uu_list_walk(v->gv_dependencies,
29561712Srm88369 		    (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0);
29570Sstevel@tonic-gate 		assert(err == 0);
29580Sstevel@tonic-gate 	}
29590Sstevel@tonic-gate 
29600Sstevel@tonic-gate 	delete_instance_dependencies(v, B_FALSE);
29610Sstevel@tonic-gate 
29620Sstevel@tonic-gate 	err = set_dependencies(v, inst, &path);
29630Sstevel@tonic-gate 	switch (err) {
29640Sstevel@tonic-gate 	case 0:
29650Sstevel@tonic-gate 		break;
29660Sstevel@tonic-gate 
29670Sstevel@tonic-gate 	case ECONNABORTED:
29680Sstevel@tonic-gate 		ret = err;
29690Sstevel@tonic-gate 		goto out;
29700Sstevel@tonic-gate 
29710Sstevel@tonic-gate 	case EINVAL:
29720Sstevel@tonic-gate 	case ELOOP:
29730Sstevel@tonic-gate 		r = libscf_instance_get_fmri(inst, &fmri);
29740Sstevel@tonic-gate 		switch (r) {
29750Sstevel@tonic-gate 		case 0:
29760Sstevel@tonic-gate 			break;
29770Sstevel@tonic-gate 
29780Sstevel@tonic-gate 		case ECONNABORTED:
29790Sstevel@tonic-gate 			ret = ECONNABORTED;
29800Sstevel@tonic-gate 			goto out;
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 		case ECANCELED:
29830Sstevel@tonic-gate 			ret = 0;
29840Sstevel@tonic-gate 			goto out;
29850Sstevel@tonic-gate 
29860Sstevel@tonic-gate 		default:
29870Sstevel@tonic-gate 			bad_error("libscf_instance_get_fmri", r);
29880Sstevel@tonic-gate 		}
29890Sstevel@tonic-gate 
29900Sstevel@tonic-gate 		if (err == EINVAL) {
29910Sstevel@tonic-gate 			log_error(LOG_ERR, "Transitioning %s "
29920Sstevel@tonic-gate 			    "to maintenance due to misconfiguration.\n",
29930Sstevel@tonic-gate 			    fmri ? fmri : "?");
29940Sstevel@tonic-gate 			vertex_send_event(v,
29950Sstevel@tonic-gate 			    RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY);
29960Sstevel@tonic-gate 		} else {
29970Sstevel@tonic-gate 			handle_cycle(fmri, path);
29980Sstevel@tonic-gate 			vertex_send_event(v,
29990Sstevel@tonic-gate 			    RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE);
30000Sstevel@tonic-gate 		}
30010Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
30020Sstevel@tonic-gate 		ret = 0;
30030Sstevel@tonic-gate 		goto out;
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate 	default:
30060Sstevel@tonic-gate 		bad_error("set_dependencies", err);
30070Sstevel@tonic-gate 	}
30080Sstevel@tonic-gate 
30090Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
30100Sstevel@tonic-gate 		boolean_t aborted = B_FALSE;
30110Sstevel@tonic-gate 
30120Sstevel@tonic-gate 		for (e = uu_list_first(old_deps);
30130Sstevel@tonic-gate 		    e != NULL;
30140Sstevel@tonic-gate 		    e = uu_list_next(old_deps, e)) {
30151712Srm88369 			vv = e->ge_vertex;
30161712Srm88369 
30171712Srm88369 			if (vertex_unref(vv) == VERTEX_INUSE &&
30181712Srm88369 			    eval_subgraph(vv, h) == ECONNABORTED)
30190Sstevel@tonic-gate 				aborted = B_TRUE;
30200Sstevel@tonic-gate 		}
30210Sstevel@tonic-gate 
30220Sstevel@tonic-gate 		for (e = uu_list_first(v->gv_dependencies);
30230Sstevel@tonic-gate 		    e != NULL;
30240Sstevel@tonic-gate 		    e = uu_list_next(v->gv_dependencies, e)) {
30250Sstevel@tonic-gate 			if (eval_subgraph(e->ge_vertex, h) ==
30260Sstevel@tonic-gate 			    ECONNABORTED)
30270Sstevel@tonic-gate 				aborted = B_TRUE;
30280Sstevel@tonic-gate 		}
30290Sstevel@tonic-gate 
30300Sstevel@tonic-gate 		if (aborted) {
30310Sstevel@tonic-gate 			ret = ECONNABORTED;
30320Sstevel@tonic-gate 			goto out;
30330Sstevel@tonic-gate 		}
30340Sstevel@tonic-gate 	}
30350Sstevel@tonic-gate 
30361958Slianep 	graph_start_if_satisfied(v);
30370Sstevel@tonic-gate 
30380Sstevel@tonic-gate 	ret = 0;
30390Sstevel@tonic-gate 
30400Sstevel@tonic-gate out:
30410Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
30420Sstevel@tonic-gate 		void *cookie = NULL;
30430Sstevel@tonic-gate 
30440Sstevel@tonic-gate 		while ((e = uu_list_teardown(old_deps, &cookie)) != NULL)
30450Sstevel@tonic-gate 			startd_free(e, sizeof (*e));
30460Sstevel@tonic-gate 
30470Sstevel@tonic-gate 		uu_list_destroy(old_deps);
30480Sstevel@tonic-gate 	}
30490Sstevel@tonic-gate 
30500Sstevel@tonic-gate 	return (ret);
30510Sstevel@tonic-gate }
30520Sstevel@tonic-gate 
30530Sstevel@tonic-gate /*
30540Sstevel@tonic-gate  * Set up v according to inst.  That is, make sure it depends on its
30550Sstevel@tonic-gate  * restarter and set up its dependencies.  Send the ADD_INSTANCE command to
30560Sstevel@tonic-gate  * the restarter, and send ENABLE or DISABLE as appropriate.
30570Sstevel@tonic-gate  *
30580Sstevel@tonic-gate  * Returns 0 on success, ECONNABORTED on repository disconnection, or
30590Sstevel@tonic-gate  * ECANCELED if inst is deleted.
30600Sstevel@tonic-gate  */
30610Sstevel@tonic-gate static int
30620Sstevel@tonic-gate configure_vertex(graph_vertex_t *v, scf_instance_t *inst)
30630Sstevel@tonic-gate {
30640Sstevel@tonic-gate 	scf_handle_t *h;
30650Sstevel@tonic-gate 	scf_propertygroup_t *pg;
30660Sstevel@tonic-gate 	scf_snapshot_t *snap;
30670Sstevel@tonic-gate 	char *restarter_fmri = startd_alloc(max_scf_value_size);
30680Sstevel@tonic-gate 	int enabled, enabled_ovr;
30690Sstevel@tonic-gate 	int err;
30700Sstevel@tonic-gate 	int *path;
30717475SPhilippe.Jung@Sun.COM 	int deathrow;
30720Sstevel@tonic-gate 
30730Sstevel@tonic-gate 	restarter_fmri[0] = '\0';
30740Sstevel@tonic-gate 
30750Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
30760Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
30770Sstevel@tonic-gate 	assert((v->gv_flags & GV_CONFIGURED) == 0);
30780Sstevel@tonic-gate 
30790Sstevel@tonic-gate 	/* GV_INSUBGRAPH should already be set properly. */
30800Sstevel@tonic-gate 	assert(should_be_in_subgraph(v) ==
30810Sstevel@tonic-gate 	    ((v->gv_flags & GV_INSUBGRAPH) != 0));
30820Sstevel@tonic-gate 
30837475SPhilippe.Jung@Sun.COM 	/*
30847475SPhilippe.Jung@Sun.COM 	 * If the instance fmri is in the deathrow list then set the
30857475SPhilippe.Jung@Sun.COM 	 * GV_DEATHROW flag on the vertex and create and set to true the
30867475SPhilippe.Jung@Sun.COM 	 * SCF_PROPERTY_DEATHROW boolean property in the non-persistent
30877475SPhilippe.Jung@Sun.COM 	 * repository for this instance fmri.
30887475SPhilippe.Jung@Sun.COM 	 */
30897475SPhilippe.Jung@Sun.COM 	if ((v->gv_flags & GV_DEATHROW) ||
30907475SPhilippe.Jung@Sun.COM 	    (is_fmri_in_deathrow(v->gv_name) == B_TRUE)) {
30917475SPhilippe.Jung@Sun.COM 		if ((v->gv_flags & GV_DEATHROW) == 0) {
30927475SPhilippe.Jung@Sun.COM 			/*
30937475SPhilippe.Jung@Sun.COM 			 * Set flag GV_DEATHROW, create and set to true
30947475SPhilippe.Jung@Sun.COM 			 * the SCF_PROPERTY_DEATHROW property in the
30957475SPhilippe.Jung@Sun.COM 			 * non-persistent repository for this instance fmri.
30967475SPhilippe.Jung@Sun.COM 			 */
30977475SPhilippe.Jung@Sun.COM 			v->gv_flags |= GV_DEATHROW;
30987475SPhilippe.Jung@Sun.COM 
30997475SPhilippe.Jung@Sun.COM 			switch (err = libscf_set_deathrow(inst, 1)) {
31007475SPhilippe.Jung@Sun.COM 			case 0:
31017475SPhilippe.Jung@Sun.COM 				break;
31027475SPhilippe.Jung@Sun.COM 
31037475SPhilippe.Jung@Sun.COM 			case ECONNABORTED:
31047475SPhilippe.Jung@Sun.COM 			case ECANCELED:
31057475SPhilippe.Jung@Sun.COM 				startd_free(restarter_fmri, max_scf_value_size);
31067475SPhilippe.Jung@Sun.COM 				return (err);
31077475SPhilippe.Jung@Sun.COM 
31087475SPhilippe.Jung@Sun.COM 			case EROFS:
31097475SPhilippe.Jung@Sun.COM 				log_error(LOG_WARNING, "Could not set %s/%s "
31107475SPhilippe.Jung@Sun.COM 				    "for deathrow %s: %s.\n",
31117475SPhilippe.Jung@Sun.COM 				    SCF_PG_DEATHROW, SCF_PROPERTY_DEATHROW,
31127475SPhilippe.Jung@Sun.COM 				    v->gv_name, strerror(err));
31137475SPhilippe.Jung@Sun.COM 				break;
31147475SPhilippe.Jung@Sun.COM 
31157475SPhilippe.Jung@Sun.COM 			case EPERM:
31167475SPhilippe.Jung@Sun.COM 				uu_die("Permission denied.\n");
31177475SPhilippe.Jung@Sun.COM 				/* NOTREACHED */
31187475SPhilippe.Jung@Sun.COM 
31197475SPhilippe.Jung@Sun.COM 			default:
31207475SPhilippe.Jung@Sun.COM 				bad_error("libscf_set_deathrow", err);
31217475SPhilippe.Jung@Sun.COM 			}
31227475SPhilippe.Jung@Sun.COM 			log_framework(LOG_DEBUG, "Deathrow, graph set %s.\n",
31237475SPhilippe.Jung@Sun.COM 			    v->gv_name);
31247475SPhilippe.Jung@Sun.COM 		}
31257475SPhilippe.Jung@Sun.COM 		startd_free(restarter_fmri, max_scf_value_size);
31267475SPhilippe.Jung@Sun.COM 		return (0);
31277475SPhilippe.Jung@Sun.COM 	}
31280Sstevel@tonic-gate 
31290Sstevel@tonic-gate 	h = scf_instance_handle(inst);
31300Sstevel@tonic-gate 
31310Sstevel@tonic-gate 	/*
31327475SPhilippe.Jung@Sun.COM 	 * Using a temporary deathrow boolean property, set through
31337475SPhilippe.Jung@Sun.COM 	 * libscf_set_deathrow(), only for fmris on deathrow, is necessary
31347475SPhilippe.Jung@Sun.COM 	 * because deathrow_fini() may already have been called, and in case
31357475SPhilippe.Jung@Sun.COM 	 * of a refresh, GV_DEATHROW may need to be set again.
31367475SPhilippe.Jung@Sun.COM 	 * libscf_get_deathrow() sets deathrow to 1 only if this instance
31377475SPhilippe.Jung@Sun.COM 	 * has a temporary boolean property named 'deathrow' valued true
31387475SPhilippe.Jung@Sun.COM 	 * in a property group 'deathrow', -1 or 0 in all other cases.
31397475SPhilippe.Jung@Sun.COM 	 */
31407475SPhilippe.Jung@Sun.COM 	err = libscf_get_deathrow(h, inst, &deathrow);
31417475SPhilippe.Jung@Sun.COM 	switch (err) {
31427475SPhilippe.Jung@Sun.COM 	case 0:
31437475SPhilippe.Jung@Sun.COM 		break;
31447475SPhilippe.Jung@Sun.COM 
31457475SPhilippe.Jung@Sun.COM 	case ECONNABORTED:
31467475SPhilippe.Jung@Sun.COM 	case ECANCELED:
31477475SPhilippe.Jung@Sun.COM 		startd_free(restarter_fmri, max_scf_value_size);
31487475SPhilippe.Jung@Sun.COM 		return (err);
31497475SPhilippe.Jung@Sun.COM 
31507475SPhilippe.Jung@Sun.COM 	default:
31517475SPhilippe.Jung@Sun.COM 		bad_error("libscf_get_deathrow", err);
31527475SPhilippe.Jung@Sun.COM 	}
31537475SPhilippe.Jung@Sun.COM 
31547475SPhilippe.Jung@Sun.COM 	if (deathrow == 1) {
31557475SPhilippe.Jung@Sun.COM 		v->gv_flags |= GV_DEATHROW;
31567475SPhilippe.Jung@Sun.COM 		startd_free(restarter_fmri, max_scf_value_size);
31577475SPhilippe.Jung@Sun.COM 		return (0);
31587475SPhilippe.Jung@Sun.COM 	}
31597475SPhilippe.Jung@Sun.COM 
31607475SPhilippe.Jung@Sun.COM 	log_framework(LOG_DEBUG, "Graph adding %s.\n", v->gv_name);
31617475SPhilippe.Jung@Sun.COM 
31627475SPhilippe.Jung@Sun.COM 	/*
31630Sstevel@tonic-gate 	 * If the instance does not have a restarter property group,
31640Sstevel@tonic-gate 	 * initialize its state to uninitialized/none, in case the restarter
31650Sstevel@tonic-gate 	 * is not enabled.
31660Sstevel@tonic-gate 	 */
31670Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
31680Sstevel@tonic-gate 
31690Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) != 0) {
31700Sstevel@tonic-gate 		instance_data_t idata;
31710Sstevel@tonic-gate 		uint_t count = 0, msecs = ALLOC_DELAY;
31720Sstevel@tonic-gate 
31730Sstevel@tonic-gate 		switch (scf_error()) {
31740Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
31750Sstevel@tonic-gate 			break;
31760Sstevel@tonic-gate 
31770Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
31780Sstevel@tonic-gate 		default:
31790Sstevel@tonic-gate 			scf_pg_destroy(pg);
31800Sstevel@tonic-gate 			return (ECONNABORTED);
31810Sstevel@tonic-gate 
31820Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
31830Sstevel@tonic-gate 			scf_pg_destroy(pg);
31840Sstevel@tonic-gate 			return (ECANCELED);
31850Sstevel@tonic-gate 
31860Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
31870Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
31880Sstevel@tonic-gate 		}
31890Sstevel@tonic-gate 
31900Sstevel@tonic-gate 		switch (err = libscf_instance_get_fmri(inst,
31910Sstevel@tonic-gate 		    (char **)&idata.i_fmri)) {
31920Sstevel@tonic-gate 		case 0:
31930Sstevel@tonic-gate 			break;
31940Sstevel@tonic-gate 
31950Sstevel@tonic-gate 		case ECONNABORTED:
31960Sstevel@tonic-gate 		case ECANCELED:
31970Sstevel@tonic-gate 			scf_pg_destroy(pg);
31980Sstevel@tonic-gate 			return (err);
31990Sstevel@tonic-gate 
32000Sstevel@tonic-gate 		default:
32010Sstevel@tonic-gate 			bad_error("libscf_instance_get_fmri", err);
32020Sstevel@tonic-gate 		}
32030Sstevel@tonic-gate 
32040Sstevel@tonic-gate 		idata.i_state = RESTARTER_STATE_NONE;
32050Sstevel@tonic-gate 		idata.i_next_state = RESTARTER_STATE_NONE;
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate init_state:
32080Sstevel@tonic-gate 		switch (err = _restarter_commit_states(h, &idata,
32090Sstevel@tonic-gate 		    RESTARTER_STATE_UNINIT, RESTARTER_STATE_NONE, NULL)) {
32100Sstevel@tonic-gate 		case 0:
32110Sstevel@tonic-gate 			break;
32120Sstevel@tonic-gate 
32130Sstevel@tonic-gate 		case ENOMEM:
32140Sstevel@tonic-gate 			++count;
32150Sstevel@tonic-gate 			if (count < ALLOC_RETRY) {
32160Sstevel@tonic-gate 				(void) poll(NULL, 0, msecs);
32170Sstevel@tonic-gate 				msecs *= ALLOC_DELAY_MULT;
32180Sstevel@tonic-gate 				goto init_state;
32190Sstevel@tonic-gate 			}
32200Sstevel@tonic-gate 
32210Sstevel@tonic-gate 			uu_die("Insufficient memory.\n");
32220Sstevel@tonic-gate 			/* NOTREACHED */
32230Sstevel@tonic-gate 
32240Sstevel@tonic-gate 		case ECONNABORTED:
32252682Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
32260Sstevel@tonic-gate 			scf_pg_destroy(pg);
32270Sstevel@tonic-gate 			return (ECONNABORTED);
32280Sstevel@tonic-gate 
32290Sstevel@tonic-gate 		case ENOENT:
32302682Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
32310Sstevel@tonic-gate 			scf_pg_destroy(pg);
32320Sstevel@tonic-gate 			return (ECANCELED);
32330Sstevel@tonic-gate 
32340Sstevel@tonic-gate 		case EPERM:
32350Sstevel@tonic-gate 		case EACCES:
32360Sstevel@tonic-gate 		case EROFS:
32370Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not initialize state for "
32380Sstevel@tonic-gate 			    "%s: %s.\n", idata.i_fmri, strerror(err));
32390Sstevel@tonic-gate 			break;
32400Sstevel@tonic-gate 
32410Sstevel@tonic-gate 		case EINVAL:
32420Sstevel@tonic-gate 		default:
32430Sstevel@tonic-gate 			bad_error("_restarter_commit_states", err);
32440Sstevel@tonic-gate 		}
32450Sstevel@tonic-gate 
32460Sstevel@tonic-gate 		startd_free((void *)idata.i_fmri, max_scf_fmri_size);
32470Sstevel@tonic-gate 	}
32480Sstevel@tonic-gate 
32490Sstevel@tonic-gate 	scf_pg_destroy(pg);
32500Sstevel@tonic-gate 
32510Sstevel@tonic-gate 	if (milestone != NULL) {
32520Sstevel@tonic-gate 		/*
32530Sstevel@tonic-gate 		 * Make sure the enable-override is set properly before we
32540Sstevel@tonic-gate 		 * read whether we should be enabled.
32550Sstevel@tonic-gate 		 */
32560Sstevel@tonic-gate 		if (milestone == MILESTONE_NONE ||
32570Sstevel@tonic-gate 		    !(v->gv_flags & GV_INSUBGRAPH)) {
32582747Sbustos 			/*
32592747Sbustos 			 * This might seem unjustified after the milestone
32602747Sbustos 			 * transition has completed (non_subgraph_svcs == 0),
32612747Sbustos 			 * but it's important because when we boot to
32622747Sbustos 			 * a milestone, we set the milestone before populating
32632747Sbustos 			 * the graph, and all of the new non-subgraph services
32642747Sbustos 			 * need to be disabled here.
32652747Sbustos 			 */
32660Sstevel@tonic-gate 			switch (err = libscf_set_enable_ovr(inst, 0)) {
32670Sstevel@tonic-gate 			case 0:
32680Sstevel@tonic-gate 				break;
32690Sstevel@tonic-gate 
32700Sstevel@tonic-gate 			case ECONNABORTED:
32710Sstevel@tonic-gate 			case ECANCELED:
32720Sstevel@tonic-gate 				return (err);
32730Sstevel@tonic-gate 
32740Sstevel@tonic-gate 			case EROFS:
32750Sstevel@tonic-gate 				log_error(LOG_WARNING,
32760Sstevel@tonic-gate 				    "Could not set %s/%s for %s: %s.\n",
32770Sstevel@tonic-gate 				    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
32780Sstevel@tonic-gate 				    v->gv_name, strerror(err));
32790Sstevel@tonic-gate 				break;
32800Sstevel@tonic-gate 
32810Sstevel@tonic-gate 			case EPERM:
32820Sstevel@tonic-gate 				uu_die("Permission denied.\n");
32830Sstevel@tonic-gate 				/* NOTREACHED */
32840Sstevel@tonic-gate 
32850Sstevel@tonic-gate 			default:
32860Sstevel@tonic-gate 				bad_error("libscf_set_enable_ovr", err);
32870Sstevel@tonic-gate 			}
32880Sstevel@tonic-gate 		} else {
32890Sstevel@tonic-gate 			assert(v->gv_flags & GV_INSUBGRAPH);
32900Sstevel@tonic-gate 			switch (err = libscf_delete_enable_ovr(inst)) {
32910Sstevel@tonic-gate 			case 0:
32920Sstevel@tonic-gate 				break;
32930Sstevel@tonic-gate 
32940Sstevel@tonic-gate 			case ECONNABORTED:
32950Sstevel@tonic-gate 			case ECANCELED:
32960Sstevel@tonic-gate 				return (err);
32970Sstevel@tonic-gate 
32980Sstevel@tonic-gate 			case EPERM:
32990Sstevel@tonic-gate 				uu_die("Permission denied.\n");
33000Sstevel@tonic-gate 				/* NOTREACHED */
33010Sstevel@tonic-gate 
33020Sstevel@tonic-gate 			default:
33030Sstevel@tonic-gate 				bad_error("libscf_delete_enable_ovr", err);
33040Sstevel@tonic-gate 			}
33050Sstevel@tonic-gate 		}
33060Sstevel@tonic-gate 	}
33070Sstevel@tonic-gate 
33080Sstevel@tonic-gate 	err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled,
33090Sstevel@tonic-gate 	    &enabled_ovr, &restarter_fmri);
33100Sstevel@tonic-gate 	switch (err) {
33110Sstevel@tonic-gate 	case 0:
33120Sstevel@tonic-gate 		break;
33130Sstevel@tonic-gate 
33140Sstevel@tonic-gate 	case ECONNABORTED:
33150Sstevel@tonic-gate 	case ECANCELED:
33160Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
33170Sstevel@tonic-gate 		return (err);
33180Sstevel@tonic-gate 
33190Sstevel@tonic-gate 	case ENOENT:
33200Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
33210Sstevel@tonic-gate 		    "Ignoring %s because it has no general property group.\n",
33220Sstevel@tonic-gate 		    v->gv_name);
33230Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
33240Sstevel@tonic-gate 		return (0);
33250Sstevel@tonic-gate 
33260Sstevel@tonic-gate 	default:
33270Sstevel@tonic-gate 		bad_error("libscf_get_basic_instance_data", err);
33280Sstevel@tonic-gate 	}
33290Sstevel@tonic-gate 
33300Sstevel@tonic-gate 	if (enabled == -1) {
33310Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
33320Sstevel@tonic-gate 		return (0);
33330Sstevel@tonic-gate 	}
33340Sstevel@tonic-gate 
33350Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) |
33360Sstevel@tonic-gate 	    (enabled ? GV_ENBLD_NOOVR : 0);
33370Sstevel@tonic-gate 
33380Sstevel@tonic-gate 	if (enabled_ovr != -1)
33390Sstevel@tonic-gate 		enabled = enabled_ovr;
33400Sstevel@tonic-gate 
33410Sstevel@tonic-gate 	v->gv_state = RESTARTER_STATE_UNINIT;
33420Sstevel@tonic-gate 
33430Sstevel@tonic-gate 	snap = libscf_get_or_make_running_snapshot(inst, v->gv_name, B_TRUE);
33440Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
33450Sstevel@tonic-gate 
33460Sstevel@tonic-gate 	/* Set up the restarter. (Sends _ADD_INSTANCE on success.) */
33470Sstevel@tonic-gate 	err = graph_change_restarter(v, restarter_fmri, h, &path);
33480Sstevel@tonic-gate 	if (err != 0) {
33490Sstevel@tonic-gate 		instance_data_t idata;
33500Sstevel@tonic-gate 		uint_t count = 0, msecs = ALLOC_DELAY;
33510Sstevel@tonic-gate 		const char *reason;
33520Sstevel@tonic-gate 
33530Sstevel@tonic-gate 		if (err == ECONNABORTED) {
33540Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
33550Sstevel@tonic-gate 			return (err);
33560Sstevel@tonic-gate 		}
33570Sstevel@tonic-gate 
33580Sstevel@tonic-gate 		assert(err == EINVAL || err == ELOOP);
33590Sstevel@tonic-gate 
33600Sstevel@tonic-gate 		if (err == EINVAL) {
33611958Slianep 			log_framework(LOG_ERR, emsg_invalid_restarter,
33620Sstevel@tonic-gate 			    v->gv_name);
33630Sstevel@tonic-gate 			reason = "invalid_restarter";
33640Sstevel@tonic-gate 		} else {
33650Sstevel@tonic-gate 			handle_cycle(v->gv_name, path);
33660Sstevel@tonic-gate 			reason = "dependency_cycle";
33670Sstevel@tonic-gate 		}
33680Sstevel@tonic-gate 
33690Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
33700Sstevel@tonic-gate 
33710Sstevel@tonic-gate 		/*
33720Sstevel@tonic-gate 		 * We didn't register the instance with the restarter, so we
33730Sstevel@tonic-gate 		 * must set maintenance mode ourselves.
33740Sstevel@tonic-gate 		 */
33750Sstevel@tonic-gate 		err = libscf_instance_get_fmri(inst, (char **)&idata.i_fmri);
33760Sstevel@tonic-gate 		if (err != 0) {
33770Sstevel@tonic-gate 			assert(err == ECONNABORTED || err == ECANCELED);
33780Sstevel@tonic-gate 			return (err);
33790Sstevel@tonic-gate 		}
33800Sstevel@tonic-gate 
33810Sstevel@tonic-gate 		idata.i_state = RESTARTER_STATE_NONE;
33820Sstevel@tonic-gate 		idata.i_next_state = RESTARTER_STATE_NONE;
33830Sstevel@tonic-gate 
33840Sstevel@tonic-gate set_maint:
33850Sstevel@tonic-gate 		switch (err = _restarter_commit_states(h, &idata,
33860Sstevel@tonic-gate 		    RESTARTER_STATE_MAINT, RESTARTER_STATE_NONE, reason)) {
33870Sstevel@tonic-gate 		case 0:
33880Sstevel@tonic-gate 			break;
33890Sstevel@tonic-gate 
33900Sstevel@tonic-gate 		case ENOMEM:
33910Sstevel@tonic-gate 			++count;
33920Sstevel@tonic-gate 			if (count < ALLOC_RETRY) {
33930Sstevel@tonic-gate 				(void) poll(NULL, 0, msecs);
33940Sstevel@tonic-gate 				msecs *= ALLOC_DELAY_MULT;
33950Sstevel@tonic-gate 				goto set_maint;
33960Sstevel@tonic-gate 			}
33970Sstevel@tonic-gate 
33980Sstevel@tonic-gate 			uu_die("Insufficient memory.\n");
33990Sstevel@tonic-gate 			/* NOTREACHED */
34000Sstevel@tonic-gate 
34010Sstevel@tonic-gate 		case ECONNABORTED:
34022682Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
34030Sstevel@tonic-gate 			return (ECONNABORTED);
34040Sstevel@tonic-gate 
34050Sstevel@tonic-gate 		case ENOENT:
34062682Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
34070Sstevel@tonic-gate 			return (ECANCELED);
34080Sstevel@tonic-gate 
34090Sstevel@tonic-gate 		case EPERM:
34100Sstevel@tonic-gate 		case EACCES:
34110Sstevel@tonic-gate 		case EROFS:
34120Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not initialize state for "
34130Sstevel@tonic-gate 			    "%s: %s.\n", idata.i_fmri, strerror(err));
34140Sstevel@tonic-gate 			break;
34150Sstevel@tonic-gate 
34160Sstevel@tonic-gate 		case EINVAL:
34170Sstevel@tonic-gate 		default:
34180Sstevel@tonic-gate 			bad_error("_restarter_commit_states", err);
34190Sstevel@tonic-gate 		}
34200Sstevel@tonic-gate 
34210Sstevel@tonic-gate 		startd_free((void *)idata.i_fmri, max_scf_fmri_size);
34220Sstevel@tonic-gate 
34230Sstevel@tonic-gate 		v->gv_state = RESTARTER_STATE_MAINT;
34240Sstevel@tonic-gate 
34250Sstevel@tonic-gate 		goto out;
34260Sstevel@tonic-gate 	}
34270Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_value_size);
34280Sstevel@tonic-gate 
34290Sstevel@tonic-gate 	/* Add all the other dependencies. */
34300Sstevel@tonic-gate 	err = refresh_vertex(v, inst);
34310Sstevel@tonic-gate 	if (err != 0) {
34320Sstevel@tonic-gate 		assert(err == ECONNABORTED);
34330Sstevel@tonic-gate 		return (err);
34340Sstevel@tonic-gate 	}
34350Sstevel@tonic-gate 
34360Sstevel@tonic-gate out:
34370Sstevel@tonic-gate 	v->gv_flags |= GV_CONFIGURED;
34380Sstevel@tonic-gate 
34390Sstevel@tonic-gate 	graph_enable_by_vertex(v, enabled, 0);
34400Sstevel@tonic-gate 
34410Sstevel@tonic-gate 	return (0);
34420Sstevel@tonic-gate }
34430Sstevel@tonic-gate 
34440Sstevel@tonic-gate static void
34450Sstevel@tonic-gate do_uadmin(void)
34460Sstevel@tonic-gate {
34470Sstevel@tonic-gate 	int fd, left;
34480Sstevel@tonic-gate 	struct statvfs vfs;
34490Sstevel@tonic-gate 
34500Sstevel@tonic-gate 	const char * const resetting = "/etc/svc/volatile/resetting";
34510Sstevel@tonic-gate 
34520Sstevel@tonic-gate 	fd = creat(resetting, 0777);
34530Sstevel@tonic-gate 	if (fd >= 0)
34540Sstevel@tonic-gate 		startd_close(fd);
34550Sstevel@tonic-gate 	else
34560Sstevel@tonic-gate 		uu_warn("Could not create \"%s\"", resetting);
34570Sstevel@tonic-gate 
34580Sstevel@tonic-gate 	/* Kill dhcpagent if we're not using nfs for root */
34590Sstevel@tonic-gate 	if ((statvfs("/", &vfs) == 0) &&
34600Sstevel@tonic-gate 	    (strncmp(vfs.f_basetype, "nfs", sizeof ("nfs") - 1) != 0))
34610Sstevel@tonic-gate 		(void) system("/usr/bin/pkill -x -u 0 dhcpagent");
34620Sstevel@tonic-gate 
34630Sstevel@tonic-gate 	(void) system("/usr/sbin/killall");
34640Sstevel@tonic-gate 	left = 5;
34650Sstevel@tonic-gate 	while (left > 0)
34660Sstevel@tonic-gate 		left = sleep(left);
34670Sstevel@tonic-gate 
34680Sstevel@tonic-gate 	(void) system("/usr/sbin/killall 9");
34690Sstevel@tonic-gate 	left = 10;
34700Sstevel@tonic-gate 	while (left > 0)
34710Sstevel@tonic-gate 		left = sleep(left);
34720Sstevel@tonic-gate 
34730Sstevel@tonic-gate 	sync();
34740Sstevel@tonic-gate 	sync();
34750Sstevel@tonic-gate 	sync();
34760Sstevel@tonic-gate 
34777375SPavel.Filipensky@Sun.COM 	(void) system("/sbin/umountall -l");
34780Sstevel@tonic-gate 	(void) system("/sbin/umount /tmp >/dev/null 2>&1");
34790Sstevel@tonic-gate 	(void) system("/sbin/umount /var/adm >/dev/null 2>&1");
34800Sstevel@tonic-gate 	(void) system("/sbin/umount /var/run >/dev/null 2>&1");
34810Sstevel@tonic-gate 	(void) system("/sbin/umount /var >/dev/null 2>&1");
34820Sstevel@tonic-gate 	(void) system("/sbin/umount /usr >/dev/null 2>&1");
34830Sstevel@tonic-gate 
34840Sstevel@tonic-gate 	uu_warn("The system is down.\n");
34850Sstevel@tonic-gate 
34860Sstevel@tonic-gate 	(void) uadmin(A_SHUTDOWN, halting, NULL);
34870Sstevel@tonic-gate 	uu_warn("uadmin() failed");
34880Sstevel@tonic-gate 
34890Sstevel@tonic-gate 	if (remove(resetting) != 0 && errno != ENOENT)
34900Sstevel@tonic-gate 		uu_warn("Could not remove \"%s\"", resetting);
34910Sstevel@tonic-gate }
34920Sstevel@tonic-gate 
34930Sstevel@tonic-gate /*
34940Sstevel@tonic-gate  * If any of the up_svcs[] are online or satisfiable, return true.  If they are
34950Sstevel@tonic-gate  * all missing, disabled, in maintenance, or unsatisfiable, return false.
34960Sstevel@tonic-gate  */
34970Sstevel@tonic-gate boolean_t
34980Sstevel@tonic-gate can_come_up(void)
34990Sstevel@tonic-gate {
35000Sstevel@tonic-gate 	int i;
35010Sstevel@tonic-gate 
35020Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
35030Sstevel@tonic-gate 
35040Sstevel@tonic-gate 	/*
35050Sstevel@tonic-gate 	 * If we are booting to single user (boot -s),
35060Sstevel@tonic-gate 	 * SCF_MILESTONE_SINGLE_USER is needed to come up because startd
35070Sstevel@tonic-gate 	 * spawns sulogin after single-user is online (see specials.c).
35080Sstevel@tonic-gate 	 */
35090Sstevel@tonic-gate 	i = (booting_to_single_user ? 0 : 1);
35100Sstevel@tonic-gate 
35110Sstevel@tonic-gate 	for (; up_svcs[i] != NULL; ++i) {
35120Sstevel@tonic-gate 		if (up_svcs_p[i] == NULL) {
35130Sstevel@tonic-gate 			up_svcs_p[i] = vertex_get_by_name(up_svcs[i]);
35140Sstevel@tonic-gate 
35150Sstevel@tonic-gate 			if (up_svcs_p[i] == NULL)
35160Sstevel@tonic-gate 				continue;
35170Sstevel@tonic-gate 		}
35180Sstevel@tonic-gate 
35190Sstevel@tonic-gate 		/*
35200Sstevel@tonic-gate 		 * Ignore unconfigured services (the ones that have been
35210Sstevel@tonic-gate 		 * mentioned in a dependency from other services, but do
35220Sstevel@tonic-gate 		 * not exist in the repository).  Services which exist
35230Sstevel@tonic-gate 		 * in the repository but don't have general/enabled
35240Sstevel@tonic-gate 		 * property will be also ignored.
35250Sstevel@tonic-gate 		 */
35260Sstevel@tonic-gate 		if (!(up_svcs_p[i]->gv_flags & GV_CONFIGURED))
35270Sstevel@tonic-gate 			continue;
35280Sstevel@tonic-gate 
35290Sstevel@tonic-gate 		switch (up_svcs_p[i]->gv_state) {
35300Sstevel@tonic-gate 		case RESTARTER_STATE_ONLINE:
35310Sstevel@tonic-gate 		case RESTARTER_STATE_DEGRADED:
35320Sstevel@tonic-gate 			/*
35330Sstevel@tonic-gate 			 * Deactivate verbose boot once a login service has been
35340Sstevel@tonic-gate 			 * reached.
35350Sstevel@tonic-gate 			 */
35360Sstevel@tonic-gate 			st->st_log_login_reached = 1;
35370Sstevel@tonic-gate 			/*FALLTHROUGH*/
35380Sstevel@tonic-gate 		case RESTARTER_STATE_UNINIT:
35390Sstevel@tonic-gate 			return (B_TRUE);
35400Sstevel@tonic-gate 
35410Sstevel@tonic-gate 		case RESTARTER_STATE_OFFLINE:
35420Sstevel@tonic-gate 			if (instance_satisfied(up_svcs_p[i], B_TRUE) != -1)
35430Sstevel@tonic-gate 				return (B_TRUE);
35440Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
35450Sstevel@tonic-gate 			    "can_come_up(): %s is unsatisfiable.\n",
35460Sstevel@tonic-gate 			    up_svcs_p[i]->gv_name);
35470Sstevel@tonic-gate 			continue;
35480Sstevel@tonic-gate 
35490Sstevel@tonic-gate 		case RESTARTER_STATE_DISABLED:
35500Sstevel@tonic-gate 		case RESTARTER_STATE_MAINT:
35510Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
35520Sstevel@tonic-gate 			    "can_come_up(): %s is in state %s.\n",
35530Sstevel@tonic-gate 			    up_svcs_p[i]->gv_name,
35540Sstevel@tonic-gate 			    instance_state_str[up_svcs_p[i]->gv_state]);
35550Sstevel@tonic-gate 			continue;
35560Sstevel@tonic-gate 
35570Sstevel@tonic-gate 		default:
35580Sstevel@tonic-gate #ifndef NDEBUG
35590Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex state %d.\n",
35600Sstevel@tonic-gate 			    __FILE__, __LINE__, up_svcs_p[i]->gv_state);
35610Sstevel@tonic-gate #endif
35620Sstevel@tonic-gate 			abort();
35630Sstevel@tonic-gate 		}
35640Sstevel@tonic-gate 	}
35650Sstevel@tonic-gate 
35660Sstevel@tonic-gate 	/*
35670Sstevel@tonic-gate 	 * In the seed repository, console-login is unsatisfiable because
35680Sstevel@tonic-gate 	 * services are missing.  To behave correctly in that case we don't want
35690Sstevel@tonic-gate 	 * to return false until manifest-import is online.
35700Sstevel@tonic-gate 	 */
35710Sstevel@tonic-gate 
35720Sstevel@tonic-gate 	if (manifest_import_p == NULL) {
35730Sstevel@tonic-gate 		manifest_import_p = vertex_get_by_name(manifest_import);
35740Sstevel@tonic-gate 
35750Sstevel@tonic-gate 		if (manifest_import_p == NULL)
35760Sstevel@tonic-gate 			return (B_FALSE);
35770Sstevel@tonic-gate 	}
35780Sstevel@tonic-gate 
35790Sstevel@tonic-gate 	switch (manifest_import_p->gv_state) {
35800Sstevel@tonic-gate 	case RESTARTER_STATE_ONLINE:
35810Sstevel@tonic-gate 	case RESTARTER_STATE_DEGRADED:
35820Sstevel@tonic-gate 	case RESTARTER_STATE_DISABLED:
35830Sstevel@tonic-gate 	case RESTARTER_STATE_MAINT:
35840Sstevel@tonic-gate 		break;
35850Sstevel@tonic-gate 
35860Sstevel@tonic-gate 	case RESTARTER_STATE_OFFLINE:
35870Sstevel@tonic-gate 		if (instance_satisfied(manifest_import_p, B_TRUE) == -1)
35880Sstevel@tonic-gate 			break;
35890Sstevel@tonic-gate 		/* FALLTHROUGH */
35900Sstevel@tonic-gate 
35910Sstevel@tonic-gate 	case RESTARTER_STATE_UNINIT:
35920Sstevel@tonic-gate 		return (B_TRUE);
35930Sstevel@tonic-gate 	}
35940Sstevel@tonic-gate 
35950Sstevel@tonic-gate 	return (B_FALSE);
35960Sstevel@tonic-gate }
35970Sstevel@tonic-gate 
35980Sstevel@tonic-gate /*
35990Sstevel@tonic-gate  * Runs sulogin.  Returns
36000Sstevel@tonic-gate  *   0 - success
36010Sstevel@tonic-gate  *   EALREADY - sulogin is already running
36020Sstevel@tonic-gate  *   EBUSY - console-login is running
36030Sstevel@tonic-gate  */
36040Sstevel@tonic-gate static int
36050Sstevel@tonic-gate run_sulogin(const char *msg)
36060Sstevel@tonic-gate {
36070Sstevel@tonic-gate 	graph_vertex_t *v;
36080Sstevel@tonic-gate 
36090Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
36100Sstevel@tonic-gate 
36110Sstevel@tonic-gate 	if (sulogin_running)
36120Sstevel@tonic-gate 		return (EALREADY);
36130Sstevel@tonic-gate 
36140Sstevel@tonic-gate 	v = vertex_get_by_name(console_login_fmri);
36150Sstevel@tonic-gate 	if (v != NULL && inst_running(v))
36160Sstevel@tonic-gate 		return (EBUSY);
36170Sstevel@tonic-gate 
36180Sstevel@tonic-gate 	sulogin_running = B_TRUE;
36190Sstevel@tonic-gate 
36200Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
36210Sstevel@tonic-gate 
36220Sstevel@tonic-gate 	fork_sulogin(B_FALSE, msg);
36230Sstevel@tonic-gate 
36240Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
36250Sstevel@tonic-gate 
36260Sstevel@tonic-gate 	sulogin_running = B_FALSE;
36270Sstevel@tonic-gate 
36280Sstevel@tonic-gate 	if (console_login_ready) {
36290Sstevel@tonic-gate 		v = vertex_get_by_name(console_login_fmri);
36300Sstevel@tonic-gate 
36310Sstevel@tonic-gate 		if (v != NULL && v->gv_state == RESTARTER_STATE_OFFLINE &&
36320Sstevel@tonic-gate 		    !inst_running(v)) {
36330Sstevel@tonic-gate 			if (v->gv_start_f == NULL)
36340Sstevel@tonic-gate 				vertex_send_event(v,
36350Sstevel@tonic-gate 				    RESTARTER_EVENT_TYPE_START);
36360Sstevel@tonic-gate 			else
36370Sstevel@tonic-gate 				v->gv_start_f(v);
36380Sstevel@tonic-gate 		}
36390Sstevel@tonic-gate 
36400Sstevel@tonic-gate 		console_login_ready = B_FALSE;
36410Sstevel@tonic-gate 	}
36420Sstevel@tonic-gate 
36430Sstevel@tonic-gate 	return (0);
36440Sstevel@tonic-gate }
36450Sstevel@tonic-gate 
36460Sstevel@tonic-gate /*
36470Sstevel@tonic-gate  * The sulogin thread runs sulogin while can_come_up() is false.  run_sulogin()
36480Sstevel@tonic-gate  * keeps sulogin from stepping on console-login's toes.
36490Sstevel@tonic-gate  */
36500Sstevel@tonic-gate /* ARGSUSED */
36510Sstevel@tonic-gate static void *
36520Sstevel@tonic-gate sulogin_thread(void *unused)
36530Sstevel@tonic-gate {
36540Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
36550Sstevel@tonic-gate 
36560Sstevel@tonic-gate 	assert(sulogin_thread_running);
36570Sstevel@tonic-gate 
36585040Swesolows 	do {
36590Sstevel@tonic-gate 		(void) run_sulogin("Console login service(s) cannot run\n");
36605040Swesolows 	} while (!can_come_up());
36610Sstevel@tonic-gate 
36620Sstevel@tonic-gate 	sulogin_thread_running = B_FALSE;
36630Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
36640Sstevel@tonic-gate 
36650Sstevel@tonic-gate 	return (NULL);
36660Sstevel@tonic-gate }
36670Sstevel@tonic-gate 
36680Sstevel@tonic-gate /* ARGSUSED */
36690Sstevel@tonic-gate void *
36700Sstevel@tonic-gate single_user_thread(void *unused)
36710Sstevel@tonic-gate {
36720Sstevel@tonic-gate 	uint_t left;
36730Sstevel@tonic-gate 	scf_handle_t *h;
36740Sstevel@tonic-gate 	scf_instance_t *inst;
36750Sstevel@tonic-gate 	scf_property_t *prop;
36760Sstevel@tonic-gate 	scf_value_t *val;
36770Sstevel@tonic-gate 	const char *msg;
36780Sstevel@tonic-gate 	char *buf;
36790Sstevel@tonic-gate 	int r;
36800Sstevel@tonic-gate 
36810Sstevel@tonic-gate 	MUTEX_LOCK(&single_user_thread_lock);
36820Sstevel@tonic-gate 	single_user_thread_count++;
36830Sstevel@tonic-gate 
36840Sstevel@tonic-gate 	if (!booting_to_single_user) {
36850Sstevel@tonic-gate 		/*
36860Sstevel@tonic-gate 		 * From rcS.sh: Look for ttymon, in.telnetd, in.rlogind and
36870Sstevel@tonic-gate 		 * processes in their process groups so they can be terminated.
36880Sstevel@tonic-gate 		 */
36890Sstevel@tonic-gate 		(void) fputs("svc.startd: Killing user processes: ", stdout);
36900Sstevel@tonic-gate 		(void) system("/usr/sbin/killall");
36910Sstevel@tonic-gate 		(void) system("/usr/sbin/killall 9");
36920Sstevel@tonic-gate 		(void) system("/usr/bin/pkill -TERM -v -u 0,1");
36930Sstevel@tonic-gate 
36940Sstevel@tonic-gate 		left = 5;
36950Sstevel@tonic-gate 		while (left > 0)
36960Sstevel@tonic-gate 			left = sleep(left);
36970Sstevel@tonic-gate 
36980Sstevel@tonic-gate 		(void) system("/usr/bin/pkill -KILL -v -u 0,1");
36990Sstevel@tonic-gate 		(void) puts("done.");
37000Sstevel@tonic-gate 	}
37010Sstevel@tonic-gate 
37020Sstevel@tonic-gate 	if (go_single_user_mode || booting_to_single_user) {
37030Sstevel@tonic-gate 		msg = "SINGLE USER MODE\n";
37040Sstevel@tonic-gate 	} else {
37050Sstevel@tonic-gate 		assert(go_to_level1);
37060Sstevel@tonic-gate 
37070Sstevel@tonic-gate 		fork_rc_script('1', "start", B_TRUE);
37080Sstevel@tonic-gate 
37090Sstevel@tonic-gate 		uu_warn("The system is ready for administration.\n");
37100Sstevel@tonic-gate 
37110Sstevel@tonic-gate 		msg = "";
37120Sstevel@tonic-gate 	}
37130Sstevel@tonic-gate 
37140Sstevel@tonic-gate 	MUTEX_UNLOCK(&single_user_thread_lock);
37150Sstevel@tonic-gate 
37160Sstevel@tonic-gate 	for (;;) {
37170Sstevel@tonic-gate 		MUTEX_LOCK(&dgraph_lock);
37180Sstevel@tonic-gate 		r = run_sulogin(msg);
37190Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
37200Sstevel@tonic-gate 		if (r == 0)
37210Sstevel@tonic-gate 			break;
37220Sstevel@tonic-gate 
37230Sstevel@tonic-gate 		assert(r == EALREADY || r == EBUSY);
37240Sstevel@tonic-gate 
37250Sstevel@tonic-gate 		left = 3;
37260Sstevel@tonic-gate 		while (left > 0)
37270Sstevel@tonic-gate 			left = sleep(left);
37280Sstevel@tonic-gate 	}
37290Sstevel@tonic-gate 
37300Sstevel@tonic-gate 	MUTEX_LOCK(&single_user_thread_lock);
37310Sstevel@tonic-gate 
37320Sstevel@tonic-gate 	/*
37330Sstevel@tonic-gate 	 * If another single user thread has started, let it finish changing
37340Sstevel@tonic-gate 	 * the run level.
37350Sstevel@tonic-gate 	 */
37360Sstevel@tonic-gate 	if (single_user_thread_count > 1) {
37370Sstevel@tonic-gate 		single_user_thread_count--;
37380Sstevel@tonic-gate 		MUTEX_UNLOCK(&single_user_thread_lock);
37390Sstevel@tonic-gate 		return (NULL);
37400Sstevel@tonic-gate 	}
37410Sstevel@tonic-gate 
37420Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
37430Sstevel@tonic-gate 	inst = scf_instance_create(h);
37440Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
37450Sstevel@tonic-gate 	val = safe_scf_value_create(h);
37460Sstevel@tonic-gate 	buf = startd_alloc(max_scf_fmri_size);
37470Sstevel@tonic-gate 
37480Sstevel@tonic-gate lookup:
37490Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
37500Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
37510Sstevel@tonic-gate 		switch (scf_error()) {
37520Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
37530Sstevel@tonic-gate 			r = libscf_create_self(h);
37540Sstevel@tonic-gate 			if (r == 0)
37550Sstevel@tonic-gate 				goto lookup;
37560Sstevel@tonic-gate 			assert(r == ECONNABORTED);
37570Sstevel@tonic-gate 			/* FALLTHROUGH */
37580Sstevel@tonic-gate 
37590Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
37600Sstevel@tonic-gate 			libscf_handle_rebind(h);
37610Sstevel@tonic-gate 			goto lookup;
37620Sstevel@tonic-gate 
37630Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
37640Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
37650Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
37660Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
37670Sstevel@tonic-gate 		default:
37680Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
37690Sstevel@tonic-gate 		}
37700Sstevel@tonic-gate 	}
37710Sstevel@tonic-gate 
37720Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
37730Sstevel@tonic-gate 
37740Sstevel@tonic-gate 	r = libscf_inst_delete_prop(inst, SCF_PG_OPTIONS_OVR,
37750Sstevel@tonic-gate 	    SCF_PROPERTY_MILESTONE);
37760Sstevel@tonic-gate 	switch (r) {
37770Sstevel@tonic-gate 	case 0:
37780Sstevel@tonic-gate 	case ECANCELED:
37790Sstevel@tonic-gate 		break;
37800Sstevel@tonic-gate 
37810Sstevel@tonic-gate 	case ECONNABORTED:
37820Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
37830Sstevel@tonic-gate 		libscf_handle_rebind(h);
37840Sstevel@tonic-gate 		goto lookup;
37850Sstevel@tonic-gate 
37860Sstevel@tonic-gate 	case EPERM:
37870Sstevel@tonic-gate 	case EACCES:
37880Sstevel@tonic-gate 	case EROFS:
37890Sstevel@tonic-gate 		log_error(LOG_WARNING, "Could not clear temporary milestone: "
37900Sstevel@tonic-gate 		    "%s.\n", strerror(r));
37910Sstevel@tonic-gate 		break;
37920Sstevel@tonic-gate 
37930Sstevel@tonic-gate 	default:
37940Sstevel@tonic-gate 		bad_error("libscf_inst_delete_prop", r);
37950Sstevel@tonic-gate 	}
37960Sstevel@tonic-gate 
37970Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
37980Sstevel@tonic-gate 
37990Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, buf, max_scf_fmri_size);
38000Sstevel@tonic-gate 	switch (r) {
38010Sstevel@tonic-gate 	case ECANCELED:
38020Sstevel@tonic-gate 	case ENOENT:
38030Sstevel@tonic-gate 	case EINVAL:
38040Sstevel@tonic-gate 		(void) strcpy(buf, "all");
38050Sstevel@tonic-gate 		/* FALLTHROUGH */
38060Sstevel@tonic-gate 
38070Sstevel@tonic-gate 	case 0:
38080Sstevel@tonic-gate 		uu_warn("Returning to milestone %s.\n", buf);
38090Sstevel@tonic-gate 		break;
38100Sstevel@tonic-gate 
38110Sstevel@tonic-gate 	case ECONNABORTED:
38120Sstevel@tonic-gate 		libscf_handle_rebind(h);
38130Sstevel@tonic-gate 		goto lookup;
38140Sstevel@tonic-gate 
38150Sstevel@tonic-gate 	default:
38160Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
38170Sstevel@tonic-gate 	}
38180Sstevel@tonic-gate 
38190Sstevel@tonic-gate 	r = dgraph_set_milestone(buf, h, B_FALSE);
38200Sstevel@tonic-gate 	switch (r) {
38210Sstevel@tonic-gate 	case 0:
38220Sstevel@tonic-gate 	case ECONNRESET:
38230Sstevel@tonic-gate 	case EALREADY:
38240Sstevel@tonic-gate 	case EINVAL:
38250Sstevel@tonic-gate 	case ENOENT:
38260Sstevel@tonic-gate 		break;
38270Sstevel@tonic-gate 
38280Sstevel@tonic-gate 	default:
38290Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
38300Sstevel@tonic-gate 	}
38310Sstevel@tonic-gate 
38320Sstevel@tonic-gate 	/*
38330Sstevel@tonic-gate 	 * See graph_runlevel_changed().
38340Sstevel@tonic-gate 	 */
38350Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
38360Sstevel@tonic-gate 	utmpx_set_runlevel(target_milestone_as_runlevel(), 'S', B_TRUE);
38370Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
38380Sstevel@tonic-gate 
38390Sstevel@tonic-gate 	startd_free(buf, max_scf_fmri_size);
38400Sstevel@tonic-gate 	scf_value_destroy(val);
38410Sstevel@tonic-gate 	scf_property_destroy(prop);
38420Sstevel@tonic-gate 	scf_instance_destroy(inst);
38430Sstevel@tonic-gate 	scf_handle_destroy(h);
38440Sstevel@tonic-gate 
38450Sstevel@tonic-gate 	/*
38460Sstevel@tonic-gate 	 * We'll give ourselves 3 seconds to respond to all of the enablings
38470Sstevel@tonic-gate 	 * that setting the milestone should have created before checking
38480Sstevel@tonic-gate 	 * whether to run sulogin.
38490Sstevel@tonic-gate 	 */
38500Sstevel@tonic-gate 	left = 3;
38510Sstevel@tonic-gate 	while (left > 0)
38520Sstevel@tonic-gate 		left = sleep(left);
38530Sstevel@tonic-gate 
38540Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
38550Sstevel@tonic-gate 	/*
38560Sstevel@tonic-gate 	 * Clearing these variables will allow the sulogin thread to run.  We
38570Sstevel@tonic-gate 	 * check here in case there aren't any more state updates anytime soon.
38580Sstevel@tonic-gate 	 */
38590Sstevel@tonic-gate 	go_to_level1 = go_single_user_mode = booting_to_single_user = B_FALSE;
38600Sstevel@tonic-gate 	if (!sulogin_thread_running && !can_come_up()) {
38610Sstevel@tonic-gate 		(void) startd_thread_create(sulogin_thread, NULL);
38620Sstevel@tonic-gate 		sulogin_thread_running = B_TRUE;
38630Sstevel@tonic-gate 	}
38640Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
38650Sstevel@tonic-gate 	single_user_thread_count--;
38660Sstevel@tonic-gate 	MUTEX_UNLOCK(&single_user_thread_lock);
38670Sstevel@tonic-gate 	return (NULL);
38680Sstevel@tonic-gate }
38690Sstevel@tonic-gate 
38700Sstevel@tonic-gate 
38710Sstevel@tonic-gate /*
38720Sstevel@tonic-gate  * Dependency graph operations API.  These are handle-independent thread-safe
38730Sstevel@tonic-gate  * graph manipulation functions which are the entry points for the event
38740Sstevel@tonic-gate  * threads below.
38750Sstevel@tonic-gate  */
38760Sstevel@tonic-gate 
38770Sstevel@tonic-gate /*
38780Sstevel@tonic-gate  * If a configured vertex exists for inst_fmri, return EEXIST.  If no vertex
38790Sstevel@tonic-gate  * exists for inst_fmri, add one.  Then fetch the restarter from inst, make
38800Sstevel@tonic-gate  * this vertex dependent on it, and send _ADD_INSTANCE to the restarter.
38810Sstevel@tonic-gate  * Fetch whether the instance should be enabled from inst and send _ENABLE or
38820Sstevel@tonic-gate  * _DISABLE as appropriate.  Finally rummage through inst's dependency
38830Sstevel@tonic-gate  * property groups and add vertices and edges as appropriate.  If anything
38840Sstevel@tonic-gate  * goes wrong after sending _ADD_INSTANCE, send _ADMIN_MAINT_ON to put the
38850Sstevel@tonic-gate  * instance in maintenance.  Don't send _START or _STOP until we get a state
38860Sstevel@tonic-gate  * update in case we're being restarted and the service is already running.
38870Sstevel@tonic-gate  *
38880Sstevel@tonic-gate  * To support booting to a milestone, we must also make sure all dependencies
38890Sstevel@tonic-gate  * encountered are configured, if they exist in the repository.
38900Sstevel@tonic-gate  *
38910Sstevel@tonic-gate  * Returns 0 on success, ECONNABORTED on repository disconnection, EINVAL if
38920Sstevel@tonic-gate  * inst_fmri is an invalid (or not canonical) FMRI, ECANCELED if inst is
38930Sstevel@tonic-gate  * deleted, or EEXIST if a configured vertex for inst_fmri already exists.
38940Sstevel@tonic-gate  */
38950Sstevel@tonic-gate int
38960Sstevel@tonic-gate dgraph_add_instance(const char *inst_fmri, scf_instance_t *inst,
38970Sstevel@tonic-gate     boolean_t lock_graph)
38980Sstevel@tonic-gate {
38990Sstevel@tonic-gate 	graph_vertex_t *v;
39000Sstevel@tonic-gate 	int err;
39010Sstevel@tonic-gate 
39020Sstevel@tonic-gate 	if (strcmp(inst_fmri, SCF_SERVICE_STARTD) == 0)
39030Sstevel@tonic-gate 		return (0);
39040Sstevel@tonic-gate 
39050Sstevel@tonic-gate 	/* Check for a vertex for inst_fmri. */
39060Sstevel@tonic-gate 	if (lock_graph) {
39070Sstevel@tonic-gate 		MUTEX_LOCK(&dgraph_lock);
39080Sstevel@tonic-gate 	} else {
39090Sstevel@tonic-gate 		assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
39100Sstevel@tonic-gate 	}
39110Sstevel@tonic-gate 
39120Sstevel@tonic-gate 	v = vertex_get_by_name(inst_fmri);
39130Sstevel@tonic-gate 
39140Sstevel@tonic-gate 	if (v != NULL) {
39150Sstevel@tonic-gate 		assert(v->gv_type == GVT_INST);
39160Sstevel@tonic-gate 
39170Sstevel@tonic-gate 		if (v->gv_flags & GV_CONFIGURED) {
39180Sstevel@tonic-gate 			if (lock_graph)
39190Sstevel@tonic-gate 				MUTEX_UNLOCK(&dgraph_lock);
39200Sstevel@tonic-gate 			return (EEXIST);
39210Sstevel@tonic-gate 		}
39220Sstevel@tonic-gate 	} else {
39230Sstevel@tonic-gate 		/* Add the vertex. */
39240Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(inst_fmri, GVT_INST, 0,
39250Sstevel@tonic-gate 		    RERR_NONE, &v);
39260Sstevel@tonic-gate 		if (err != 0) {
39270Sstevel@tonic-gate 			assert(err == EINVAL);
39280Sstevel@tonic-gate 			if (lock_graph)
39290Sstevel@tonic-gate 				MUTEX_UNLOCK(&dgraph_lock);
39300Sstevel@tonic-gate 			return (EINVAL);
39310Sstevel@tonic-gate 		}
39320Sstevel@tonic-gate 	}
39330Sstevel@tonic-gate 
39340Sstevel@tonic-gate 	err = configure_vertex(v, inst);
39350Sstevel@tonic-gate 
39360Sstevel@tonic-gate 	if (lock_graph)
39370Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
39380Sstevel@tonic-gate 
39390Sstevel@tonic-gate 	return (err);
39400Sstevel@tonic-gate }
39410Sstevel@tonic-gate 
39420Sstevel@tonic-gate /*
39430Sstevel@tonic-gate  * Locate the vertex for this property group's instance.  If it doesn't exist
39440Sstevel@tonic-gate  * or is unconfigured, call dgraph_add_instance() & return.  Otherwise fetch
39450Sstevel@tonic-gate  * the restarter for the instance, and if it has changed, send
39460Sstevel@tonic-gate  * _REMOVE_INSTANCE to the old restarter, remove the dependency, make sure the
39470Sstevel@tonic-gate  * new restarter has a vertex, add a new dependency, and send _ADD_INSTANCE to
39480Sstevel@tonic-gate  * the new restarter.  Then fetch whether the instance should be enabled, and
39490Sstevel@tonic-gate  * if it is different from what we had, or if we changed the restarter, send
39500Sstevel@tonic-gate  * the appropriate _ENABLE or _DISABLE command.
39510Sstevel@tonic-gate  *
39520Sstevel@tonic-gate  * Returns 0 on success, ENOTSUP if the pg's parent is not an instance,
39530Sstevel@tonic-gate  * ECONNABORTED on repository disconnection, ECANCELED if the instance is
39540Sstevel@tonic-gate  * deleted, or -1 if the instance's general property group is deleted or if
39550Sstevel@tonic-gate  * its enabled property is misconfigured.
39560Sstevel@tonic-gate  */
39570Sstevel@tonic-gate static int
39580Sstevel@tonic-gate dgraph_update_general(scf_propertygroup_t *pg)
39590Sstevel@tonic-gate {
39600Sstevel@tonic-gate 	scf_handle_t *h;
39610Sstevel@tonic-gate 	scf_instance_t *inst;
39620Sstevel@tonic-gate 	char *fmri;
39630Sstevel@tonic-gate 	char *restarter_fmri;
39640Sstevel@tonic-gate 	graph_vertex_t *v;
39650Sstevel@tonic-gate 	int err;
39660Sstevel@tonic-gate 	int enabled, enabled_ovr;
39670Sstevel@tonic-gate 	int oldflags;
39680Sstevel@tonic-gate 
39690Sstevel@tonic-gate 	/* Find the vertex for this service */
39700Sstevel@tonic-gate 	h = scf_pg_handle(pg);
39710Sstevel@tonic-gate 
39720Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
39730Sstevel@tonic-gate 
39740Sstevel@tonic-gate 	if (scf_pg_get_parent_instance(pg, inst) != 0) {
39750Sstevel@tonic-gate 		switch (scf_error()) {
39760Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
39770Sstevel@tonic-gate 			return (ENOTSUP);
39780Sstevel@tonic-gate 
39790Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
39800Sstevel@tonic-gate 		default:
39810Sstevel@tonic-gate 			return (ECONNABORTED);
39820Sstevel@tonic-gate 
39830Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
39840Sstevel@tonic-gate 			return (0);
39850Sstevel@tonic-gate 
39860Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
39870Sstevel@tonic-gate 			bad_error("scf_pg_get_parent_instance", scf_error());
39880Sstevel@tonic-gate 		}
39890Sstevel@tonic-gate 	}
39900Sstevel@tonic-gate 
39910Sstevel@tonic-gate 	err = libscf_instance_get_fmri(inst, &fmri);
39920Sstevel@tonic-gate 	switch (err) {
39930Sstevel@tonic-gate 	case 0:
39940Sstevel@tonic-gate 		break;
39950Sstevel@tonic-gate 
39960Sstevel@tonic-gate 	case ECONNABORTED:
39970Sstevel@tonic-gate 		scf_instance_destroy(inst);
39980Sstevel@tonic-gate 		return (ECONNABORTED);
39990Sstevel@tonic-gate 
40000Sstevel@tonic-gate 	case ECANCELED:
40010Sstevel@tonic-gate 		scf_instance_destroy(inst);
40020Sstevel@tonic-gate 		return (0);
40030Sstevel@tonic-gate 
40040Sstevel@tonic-gate 	default:
40050Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", err);
40060Sstevel@tonic-gate 	}
40070Sstevel@tonic-gate 
40080Sstevel@tonic-gate 	log_framework(LOG_DEBUG,
40090Sstevel@tonic-gate 	    "Graph engine: Reloading general properties for %s.\n", fmri);
40100Sstevel@tonic-gate 
40110Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
40120Sstevel@tonic-gate 
40130Sstevel@tonic-gate 	v = vertex_get_by_name(fmri);
40140Sstevel@tonic-gate 	if (v == NULL || !(v->gv_flags & GV_CONFIGURED)) {
40150Sstevel@tonic-gate 		/* Will get the up-to-date properties. */
40160Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
40170Sstevel@tonic-gate 		err = dgraph_add_instance(fmri, inst, B_TRUE);
40180Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
40190Sstevel@tonic-gate 		scf_instance_destroy(inst);
40200Sstevel@tonic-gate 		return (err == ECANCELED ? 0 : err);
40210Sstevel@tonic-gate 	}
40220Sstevel@tonic-gate 
40230Sstevel@tonic-gate 	/* Read enabled & restarter from repository. */
40240Sstevel@tonic-gate 	restarter_fmri = startd_alloc(max_scf_value_size);
40250Sstevel@tonic-gate 	err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled,
40260Sstevel@tonic-gate 	    &enabled_ovr, &restarter_fmri);
40270Sstevel@tonic-gate 	if (err != 0 || enabled == -1) {
40280Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
40290Sstevel@tonic-gate 		scf_instance_destroy(inst);
40300Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
40310Sstevel@tonic-gate 
40320Sstevel@tonic-gate 		switch (err) {
40330Sstevel@tonic-gate 		case ENOENT:
40340Sstevel@tonic-gate 		case 0:
40350Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
40360Sstevel@tonic-gate 			return (-1);
40370Sstevel@tonic-gate 
40380Sstevel@tonic-gate 		case ECONNABORTED:
40390Sstevel@tonic-gate 		case ECANCELED:
40400Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
40410Sstevel@tonic-gate 			return (err);
40420Sstevel@tonic-gate 
40430Sstevel@tonic-gate 		default:
40440Sstevel@tonic-gate 			bad_error("libscf_get_basic_instance_data", err);
40450Sstevel@tonic-gate 		}
40460Sstevel@tonic-gate 	}
40470Sstevel@tonic-gate 
40480Sstevel@tonic-gate 	oldflags = v->gv_flags;
40490Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) |
40500Sstevel@tonic-gate 	    (enabled ? GV_ENBLD_NOOVR : 0);
40510Sstevel@tonic-gate 
40520Sstevel@tonic-gate 	if (enabled_ovr != -1)
40530Sstevel@tonic-gate 		enabled = enabled_ovr;
40540Sstevel@tonic-gate 
40550Sstevel@tonic-gate 	/*
40560Sstevel@tonic-gate 	 * If GV_ENBLD_NOOVR has changed, then we need to re-evaluate the
40570Sstevel@tonic-gate 	 * subgraph.
40580Sstevel@tonic-gate 	 */
40590Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE && v->gv_flags != oldflags)
40600Sstevel@tonic-gate 		(void) eval_subgraph(v, h);
40610Sstevel@tonic-gate 
40620Sstevel@tonic-gate 	scf_instance_destroy(inst);
40630Sstevel@tonic-gate 
40640Sstevel@tonic-gate 	/* Ignore restarter change for now. */
40650Sstevel@tonic-gate 
40660Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_value_size);
40670Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
40680Sstevel@tonic-gate 
40690Sstevel@tonic-gate 	/*
40700Sstevel@tonic-gate 	 * Always send _ENABLE or _DISABLE.  We could avoid this if the
40710Sstevel@tonic-gate 	 * restarter didn't change and the enabled value didn't change, but
40720Sstevel@tonic-gate 	 * that's not easy to check and improbable anyway, so we'll just do
40730Sstevel@tonic-gate 	 * this.
40740Sstevel@tonic-gate 	 */
40750Sstevel@tonic-gate 	graph_enable_by_vertex(v, enabled, 1);
40760Sstevel@tonic-gate 
40770Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
40780Sstevel@tonic-gate 
40790Sstevel@tonic-gate 	return (0);
40800Sstevel@tonic-gate }
40810Sstevel@tonic-gate 
40820Sstevel@tonic-gate /*
40830Sstevel@tonic-gate  * Delete all of the property group dependencies of v, update inst's running
40840Sstevel@tonic-gate  * snapshot, and add the dependencies in the new snapshot.  If any of the new
40850Sstevel@tonic-gate  * dependencies would create a cycle, send _ADMIN_MAINT_ON.  Otherwise
40860Sstevel@tonic-gate  * reevaluate v's dependencies, send _START or _STOP as appropriate, and do
40870Sstevel@tonic-gate  * the same for v's dependents.
40880Sstevel@tonic-gate  *
40890Sstevel@tonic-gate  * Returns
40900Sstevel@tonic-gate  *   0 - success
40910Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
40920Sstevel@tonic-gate  *   ECANCELED - inst was deleted
40930Sstevel@tonic-gate  *   EINVAL - inst is invalid (e.g., missing general/enabled)
40940Sstevel@tonic-gate  *   -1 - libscf_snapshots_refresh() failed
40950Sstevel@tonic-gate  */
40960Sstevel@tonic-gate static int
40970Sstevel@tonic-gate dgraph_refresh_instance(graph_vertex_t *v, scf_instance_t *inst)
40980Sstevel@tonic-gate {
40990Sstevel@tonic-gate 	int r;
41000Sstevel@tonic-gate 	int enabled;
41010Sstevel@tonic-gate 
41020Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
41030Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
41040Sstevel@tonic-gate 
41050Sstevel@tonic-gate 	/* Only refresh services with valid general/enabled properties. */
41060Sstevel@tonic-gate 	r = libscf_get_basic_instance_data(scf_instance_handle(inst), inst,
41070Sstevel@tonic-gate 	    v->gv_name, &enabled, NULL, NULL);
41080Sstevel@tonic-gate 	switch (r) {
41090Sstevel@tonic-gate 	case 0:
41100Sstevel@tonic-gate 		break;
41110Sstevel@tonic-gate 
41120Sstevel@tonic-gate 	case ECONNABORTED:
41130Sstevel@tonic-gate 	case ECANCELED:
41140Sstevel@tonic-gate 		return (r);
41150Sstevel@tonic-gate 
41160Sstevel@tonic-gate 	case ENOENT:
41170Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
41180Sstevel@tonic-gate 		    "Ignoring %s because it has no general property group.\n",
41190Sstevel@tonic-gate 		    v->gv_name);
41200Sstevel@tonic-gate 		return (EINVAL);
41210Sstevel@tonic-gate 
41220Sstevel@tonic-gate 	default:
41230Sstevel@tonic-gate 		bad_error("libscf_get_basic_instance_data", r);
41240Sstevel@tonic-gate 	}
41250Sstevel@tonic-gate 
41260Sstevel@tonic-gate 	if (enabled == -1)
41270Sstevel@tonic-gate 		return (EINVAL);
41280Sstevel@tonic-gate 
41290Sstevel@tonic-gate 	r = libscf_snapshots_refresh(inst, v->gv_name);
41300Sstevel@tonic-gate 	if (r != 0) {
41310Sstevel@tonic-gate 		if (r != -1)
41320Sstevel@tonic-gate 			bad_error("libscf_snapshots_refresh", r);
41330Sstevel@tonic-gate 
41340Sstevel@tonic-gate 		/* error logged */
41350Sstevel@tonic-gate 		return (r);
41360Sstevel@tonic-gate 	}
41370Sstevel@tonic-gate 
41380Sstevel@tonic-gate 	r = refresh_vertex(v, inst);
41390Sstevel@tonic-gate 	if (r != 0 && r != ECONNABORTED)
41400Sstevel@tonic-gate 		bad_error("refresh_vertex", r);
41410Sstevel@tonic-gate 	return (r);
41420Sstevel@tonic-gate }
41430Sstevel@tonic-gate 
41440Sstevel@tonic-gate /*
41457630SRenaud.Manus@Sun.COM  * Returns true only if none of this service's dependents are 'up' -- online
41467630SRenaud.Manus@Sun.COM  * or degraded (offline is considered down in this situation). This function
41477630SRenaud.Manus@Sun.COM  * is somehow similar to is_nonsubgraph_leaf() but works on subtrees.
41487630SRenaud.Manus@Sun.COM  */
41497630SRenaud.Manus@Sun.COM static boolean_t
41507630SRenaud.Manus@Sun.COM insubtree_dependents_down(graph_vertex_t *v)
41517630SRenaud.Manus@Sun.COM {
41527630SRenaud.Manus@Sun.COM 	graph_vertex_t *vv;
41537630SRenaud.Manus@Sun.COM 	graph_edge_t *e;
41547630SRenaud.Manus@Sun.COM 
41557630SRenaud.Manus@Sun.COM 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
41567630SRenaud.Manus@Sun.COM 
41577630SRenaud.Manus@Sun.COM 	for (e = uu_list_first(v->gv_dependents); e != NULL;
41587630SRenaud.Manus@Sun.COM 	    e = uu_list_next(v->gv_dependents, e)) {
41597630SRenaud.Manus@Sun.COM 		vv = e->ge_vertex;
41607630SRenaud.Manus@Sun.COM 		if (vv->gv_type == GVT_INST) {
41617630SRenaud.Manus@Sun.COM 			if ((vv->gv_flags & GV_CONFIGURED) == 0)
41627630SRenaud.Manus@Sun.COM 				continue;
41637630SRenaud.Manus@Sun.COM 
41647630SRenaud.Manus@Sun.COM 			if ((vv->gv_flags & GV_TOOFFLINE) == 0)
41657630SRenaud.Manus@Sun.COM 				continue;
41667630SRenaud.Manus@Sun.COM 
41677630SRenaud.Manus@Sun.COM 			if ((vv->gv_state == RESTARTER_STATE_ONLINE) ||
41687630SRenaud.Manus@Sun.COM 			    (vv->gv_state == RESTARTER_STATE_DEGRADED))
41697630SRenaud.Manus@Sun.COM 				return (B_FALSE);
41707630SRenaud.Manus@Sun.COM 		} else {
41717630SRenaud.Manus@Sun.COM 			/*
41727630SRenaud.Manus@Sun.COM 			 * For dependency groups or service vertices, keep
41737630SRenaud.Manus@Sun.COM 			 * traversing to see if instances are running.
41747630SRenaud.Manus@Sun.COM 			 */
41757630SRenaud.Manus@Sun.COM 			if (insubtree_dependents_down(vv) == B_FALSE)
41767630SRenaud.Manus@Sun.COM 				return (B_FALSE);
41777630SRenaud.Manus@Sun.COM 		}
41787630SRenaud.Manus@Sun.COM 	}
41797630SRenaud.Manus@Sun.COM 
41807630SRenaud.Manus@Sun.COM 	return (B_TRUE);
41817630SRenaud.Manus@Sun.COM }
41827630SRenaud.Manus@Sun.COM 
41837630SRenaud.Manus@Sun.COM /*
41842747Sbustos  * Returns true only if none of this service's dependents are 'up' -- online,
41852747Sbustos  * degraded, or offline.
41860Sstevel@tonic-gate  */
41870Sstevel@tonic-gate static int
41882747Sbustos is_nonsubgraph_leaf(graph_vertex_t *v)
41890Sstevel@tonic-gate {
41900Sstevel@tonic-gate 	graph_vertex_t *vv;
41910Sstevel@tonic-gate 	graph_edge_t *e;
41920Sstevel@tonic-gate 
41930Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
41940Sstevel@tonic-gate 
41950Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
41960Sstevel@tonic-gate 	    e != NULL;
41970Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e)) {
41980Sstevel@tonic-gate 
41990Sstevel@tonic-gate 		vv = e->ge_vertex;
42000Sstevel@tonic-gate 		if (vv->gv_type == GVT_INST) {
42012747Sbustos 			if ((vv->gv_flags & GV_CONFIGURED) == 0)
42022747Sbustos 				continue;
42032747Sbustos 
42042747Sbustos 			if (vv->gv_flags & GV_INSUBGRAPH)
42052747Sbustos 				continue;
42062747Sbustos 
42072747Sbustos 			if (up_state(vv->gv_state))
42082747Sbustos 				return (0);
42090Sstevel@tonic-gate 		} else {
42100Sstevel@tonic-gate 			/*
42110Sstevel@tonic-gate 			 * For dependency group or service vertices, keep
42120Sstevel@tonic-gate 			 * traversing to see if instances are running.
42137630SRenaud.Manus@Sun.COM 			 *
42147630SRenaud.Manus@Sun.COM 			 * We should skip exclude_all dependencies otherwise
42157630SRenaud.Manus@Sun.COM 			 * the vertex will never be considered as a leaf
42167630SRenaud.Manus@Sun.COM 			 * if the dependent is offline. The main reason for
42177630SRenaud.Manus@Sun.COM 			 * this is that disable_nonsubgraph_leaves() skips
42187630SRenaud.Manus@Sun.COM 			 * exclusion dependencies.
42190Sstevel@tonic-gate 			 */
42207630SRenaud.Manus@Sun.COM 			if (vv->gv_type == GVT_GROUP &&
42217630SRenaud.Manus@Sun.COM 			    vv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
42227630SRenaud.Manus@Sun.COM 				continue;
42237630SRenaud.Manus@Sun.COM 
42242747Sbustos 			if (!is_nonsubgraph_leaf(vv))
42252747Sbustos 				return (0);
42260Sstevel@tonic-gate 		}
42270Sstevel@tonic-gate 	}
42282747Sbustos 
42292747Sbustos 	return (1);
42300Sstevel@tonic-gate }
42310Sstevel@tonic-gate 
42320Sstevel@tonic-gate /*
42332747Sbustos  * Disable v temporarily.  Attempt to do this by setting its enabled override
42342747Sbustos  * property in the repository.  If that fails, send a _DISABLE command.
42352747Sbustos  * Returns 0 on success and ECONNABORTED if the repository connection is
42362747Sbustos  * broken.
42370Sstevel@tonic-gate  */
42382747Sbustos static int
42392747Sbustos disable_service_temporarily(graph_vertex_t *v, scf_handle_t *h)
42400Sstevel@tonic-gate {
42412747Sbustos 	const char * const emsg = "Could not temporarily disable %s because "
42422747Sbustos 	    "%s.  Will stop service anyways.  Repository status for the "
42432747Sbustos 	    "service may be inaccurate.\n";
42442747Sbustos 	const char * const emsg_cbroken =
42452747Sbustos 	    "the repository connection was broken";
42462747Sbustos 
42472747Sbustos 	scf_instance_t *inst;
42480Sstevel@tonic-gate 	int r;
42490Sstevel@tonic-gate 
42500Sstevel@tonic-gate 	inst = scf_instance_create(h);
42510Sstevel@tonic-gate 	if (inst == NULL) {
42522747Sbustos 		char buf[100];
42532747Sbustos 
42542747Sbustos 		(void) snprintf(buf, sizeof (buf),
42552747Sbustos 		    "scf_instance_create() failed (%s)",
42562747Sbustos 		    scf_strerror(scf_error()));
42572747Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name, buf);
42582747Sbustos 
42592747Sbustos 		graph_enable_by_vertex(v, 0, 0);
42602747Sbustos 		return (0);
42610Sstevel@tonic-gate 	}
42622747Sbustos 
42630Sstevel@tonic-gate 	r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst,
42640Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT);
42650Sstevel@tonic-gate 	if (r != 0) {
42660Sstevel@tonic-gate 		switch (scf_error()) {
42670Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
42682747Sbustos 			log_error(LOG_WARNING, emsg, v->gv_name, emsg_cbroken);
42692747Sbustos 			graph_enable_by_vertex(v, 0, 0);
42702747Sbustos 			return (ECONNABORTED);
42710Sstevel@tonic-gate 
42720Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
42732747Sbustos 			return (0);
42740Sstevel@tonic-gate 
42750Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
42760Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
42770Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
42780Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
42790Sstevel@tonic-gate 		default:
42800Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri",
42810Sstevel@tonic-gate 			    scf_error());
42820Sstevel@tonic-gate 		}
42830Sstevel@tonic-gate 	}
42842747Sbustos 
42850Sstevel@tonic-gate 	r = libscf_set_enable_ovr(inst, 0);
42860Sstevel@tonic-gate 	switch (r) {
42870Sstevel@tonic-gate 	case 0:
42880Sstevel@tonic-gate 		scf_instance_destroy(inst);
42892747Sbustos 		return (0);
42902747Sbustos 
42910Sstevel@tonic-gate 	case ECANCELED:
42920Sstevel@tonic-gate 		scf_instance_destroy(inst);
42932747Sbustos 		return (0);
42942747Sbustos 
42950Sstevel@tonic-gate 	case ECONNABORTED:
42962747Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name, emsg_cbroken);
42972747Sbustos 		graph_enable_by_vertex(v, 0, 0);
42982747Sbustos 		return (ECONNABORTED);
42992747Sbustos 
43000Sstevel@tonic-gate 	case EPERM:
43012747Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name,
43022747Sbustos 		    "the repository denied permission");
43032747Sbustos 		graph_enable_by_vertex(v, 0, 0);
43042747Sbustos 		return (0);
43052747Sbustos 
43060Sstevel@tonic-gate 	case EROFS:
43072747Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name,
43082747Sbustos 		    "the repository is read-only");
43092747Sbustos 		graph_enable_by_vertex(v, 0, 0);
43102747Sbustos 		return (0);
43112747Sbustos 
43120Sstevel@tonic-gate 	default:
43130Sstevel@tonic-gate 		bad_error("libscf_set_enable_ovr", r);
43142747Sbustos 		/* NOTREACHED */
43150Sstevel@tonic-gate 	}
43162747Sbustos }
43172747Sbustos 
43182747Sbustos /*
43197630SRenaud.Manus@Sun.COM  * Of the transitive instance dependencies of v, offline those which are
43207630SRenaud.Manus@Sun.COM  * in the subtree and which are leaves (i.e., have no dependents which are
43217630SRenaud.Manus@Sun.COM  * "up").
43227630SRenaud.Manus@Sun.COM  */
43237630SRenaud.Manus@Sun.COM void
43247630SRenaud.Manus@Sun.COM offline_subtree_leaves(graph_vertex_t *v, void *arg)
43257630SRenaud.Manus@Sun.COM {
43267630SRenaud.Manus@Sun.COM 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
43277630SRenaud.Manus@Sun.COM 
43287630SRenaud.Manus@Sun.COM 	/* If v isn't an instance, recurse on its dependencies. */
43297630SRenaud.Manus@Sun.COM 	if (v->gv_type != GVT_INST) {
43307630SRenaud.Manus@Sun.COM 		graph_walk_dependencies(v, offline_subtree_leaves, arg);
43317630SRenaud.Manus@Sun.COM 		return;
43327630SRenaud.Manus@Sun.COM 	}
43337630SRenaud.Manus@Sun.COM 
43347630SRenaud.Manus@Sun.COM 	/*
43357630SRenaud.Manus@Sun.COM 	 * If v is not in the subtree, so should all of its dependencies,
43367630SRenaud.Manus@Sun.COM 	 * so do nothing.
43377630SRenaud.Manus@Sun.COM 	 */
43387630SRenaud.Manus@Sun.COM 	if ((v->gv_flags & GV_TOOFFLINE) == 0)
43397630SRenaud.Manus@Sun.COM 		return;
43407630SRenaud.Manus@Sun.COM 
43417630SRenaud.Manus@Sun.COM 	/* If v isn't a leaf because it's already down, recurse. */
43427630SRenaud.Manus@Sun.COM 	if (!up_state(v->gv_state)) {
43437630SRenaud.Manus@Sun.COM 		graph_walk_dependencies(v, offline_subtree_leaves, arg);
43447630SRenaud.Manus@Sun.COM 		return;
43457630SRenaud.Manus@Sun.COM 	}
43467630SRenaud.Manus@Sun.COM 
43477630SRenaud.Manus@Sun.COM 	/* if v is a leaf, offline it or disable it if it's the last one */
43487630SRenaud.Manus@Sun.COM 	if (insubtree_dependents_down(v) == B_TRUE) {
43497630SRenaud.Manus@Sun.COM 		if (v->gv_flags & GV_TODISABLE)
43507630SRenaud.Manus@Sun.COM 			vertex_send_event(v,
43517630SRenaud.Manus@Sun.COM 			    RESTARTER_EVENT_TYPE_ADMIN_DISABLE);
43527630SRenaud.Manus@Sun.COM 		else
43537630SRenaud.Manus@Sun.COM 			offline_vertex(v);
43547630SRenaud.Manus@Sun.COM 	}
43557630SRenaud.Manus@Sun.COM }
43567630SRenaud.Manus@Sun.COM 
43577630SRenaud.Manus@Sun.COM void
43587630SRenaud.Manus@Sun.COM graph_offline_subtree_leaves(graph_vertex_t *v, void *h)
43597630SRenaud.Manus@Sun.COM {
43607630SRenaud.Manus@Sun.COM 	graph_walk_dependencies(v, offline_subtree_leaves, (void *)h);
43617630SRenaud.Manus@Sun.COM }
43627630SRenaud.Manus@Sun.COM 
43637630SRenaud.Manus@Sun.COM 
43647630SRenaud.Manus@Sun.COM /*
43652747Sbustos  * Of the transitive instance dependencies of v, disable those which are not
43662747Sbustos  * in the subgraph and which are leaves (i.e., have no dependents which are
43672747Sbustos  * "up").
43682747Sbustos  */
43692747Sbustos static void
43702747Sbustos disable_nonsubgraph_leaves(graph_vertex_t *v, void *arg)
43712747Sbustos {
43722747Sbustos 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
43732747Sbustos 
43744762Snakanon 	/*
43754762Snakanon 	 * We must skip exclusion dependencies because they are allowed to
43764762Snakanon 	 * complete dependency cycles.  This is correct because A's exclusion
43774762Snakanon 	 * dependency on B doesn't bear on the order in which they should be
43784762Snakanon 	 * stopped.  Indeed, the exclusion dependency should guarantee that
43794762Snakanon 	 * they are never online at the same time.
43804762Snakanon 	 */
43814762Snakanon 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
43824762Snakanon 		return;
43834762Snakanon 
43842747Sbustos 	/* If v isn't an instance, recurse on its dependencies. */
43852747Sbustos 	if (v->gv_type != GVT_INST)
43862747Sbustos 		goto recurse;
43872747Sbustos 
43882747Sbustos 	if ((v->gv_flags & GV_CONFIGURED) == 0)
43892747Sbustos 		/*
43902747Sbustos 		 * Unconfigured instances should have no dependencies, but in
43912747Sbustos 		 * case they ever get them,
43922747Sbustos 		 */
43932747Sbustos 		goto recurse;
43942747Sbustos 
43952747Sbustos 	/*
43962747Sbustos 	 * If v is in the subgraph, so should all of its dependencies, so do
43972747Sbustos 	 * nothing.
43982747Sbustos 	 */
43992747Sbustos 	if (v->gv_flags & GV_INSUBGRAPH)
44002747Sbustos 		return;
44012747Sbustos 
44022747Sbustos 	/* If v isn't a leaf because it's already down, recurse. */
44032747Sbustos 	if (!up_state(v->gv_state))
44042747Sbustos 		goto recurse;
44052747Sbustos 
44062747Sbustos 	/* If v is disabled but not down yet, be patient. */
44072747Sbustos 	if ((v->gv_flags & GV_ENABLED) == 0)
44082747Sbustos 		return;
44092747Sbustos 
44102747Sbustos 	/* If v is a leaf, disable it. */
44112747Sbustos 	if (is_nonsubgraph_leaf(v))
44122747Sbustos 		(void) disable_service_temporarily(v, (scf_handle_t *)arg);
44132747Sbustos 
44140Sstevel@tonic-gate 	return;
44152747Sbustos 
44160Sstevel@tonic-gate recurse:
44172747Sbustos 	graph_walk_dependencies(v, disable_nonsubgraph_leaves, arg);
44180Sstevel@tonic-gate }
44190Sstevel@tonic-gate 
44200Sstevel@tonic-gate /*
44210Sstevel@tonic-gate  * Find the vertex for inst_name.  If it doesn't exist, return ENOENT.
44220Sstevel@tonic-gate  * Otherwise set its state to state.  If the instance has entered a state
44230Sstevel@tonic-gate  * which requires automatic action, take it (Uninitialized: do
44240Sstevel@tonic-gate  * dgraph_refresh_instance() without the snapshot update.  Disabled: if the
44250Sstevel@tonic-gate  * instance should be enabled, send _ENABLE.  Offline: if the instance should
44260Sstevel@tonic-gate  * be disabled, send _DISABLE, and if its dependencies are satisfied, send
44270Sstevel@tonic-gate  * _START.  Online, Degraded: if the instance wasn't running, update its start
44280Sstevel@tonic-gate  * snapshot.  Maintenance: no action.)
44290Sstevel@tonic-gate  *
44300Sstevel@tonic-gate  * Also fails with ECONNABORTED, or EINVAL if state is invalid.
44310Sstevel@tonic-gate  */
44320Sstevel@tonic-gate static int
44330Sstevel@tonic-gate dgraph_set_instance_state(scf_handle_t *h, const char *inst_name,
44340Sstevel@tonic-gate     restarter_instance_state_t state, restarter_error_t serr)
44350Sstevel@tonic-gate {
44360Sstevel@tonic-gate 	graph_vertex_t *v;
44371958Slianep 	int err = 0;
44380Sstevel@tonic-gate 	restarter_instance_state_t old_state;
44390Sstevel@tonic-gate 
44400Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
44410Sstevel@tonic-gate 
44420Sstevel@tonic-gate 	v = vertex_get_by_name(inst_name);
44430Sstevel@tonic-gate 	if (v == NULL) {
44440Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
44450Sstevel@tonic-gate 		return (ENOENT);
44460Sstevel@tonic-gate 	}
44470Sstevel@tonic-gate 
44482747Sbustos 	assert(v->gv_type == GVT_INST);
44492747Sbustos 
44500Sstevel@tonic-gate 	switch (state) {
44510Sstevel@tonic-gate 	case RESTARTER_STATE_UNINIT:
44520Sstevel@tonic-gate 	case RESTARTER_STATE_DISABLED:
44530Sstevel@tonic-gate 	case RESTARTER_STATE_OFFLINE:
44540Sstevel@tonic-gate 	case RESTARTER_STATE_ONLINE:
44550Sstevel@tonic-gate 	case RESTARTER_STATE_DEGRADED:
44560Sstevel@tonic-gate 	case RESTARTER_STATE_MAINT:
44570Sstevel@tonic-gate 		break;
44580Sstevel@tonic-gate 
44590Sstevel@tonic-gate 	default:
44600Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
44610Sstevel@tonic-gate 		return (EINVAL);
44620Sstevel@tonic-gate 	}
44630Sstevel@tonic-gate 
44640Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph noting %s %s -> %s.\n", v->gv_name,
44650Sstevel@tonic-gate 	    instance_state_str[v->gv_state], instance_state_str[state]);
44660Sstevel@tonic-gate 
44670Sstevel@tonic-gate 	old_state = v->gv_state;
44680Sstevel@tonic-gate 	v->gv_state = state;
44690Sstevel@tonic-gate 
44702339Slianep 	err = gt_transition(h, v, serr, old_state);
44711958Slianep 
44721958Slianep 	MUTEX_UNLOCK(&dgraph_lock);
44731958Slianep 	return (err);
44741958Slianep }
44751958Slianep 
44761958Slianep /*
44772747Sbustos  * Handle state changes during milestone shutdown.  See
44782747Sbustos  * dgraph_set_milestone().  If the repository connection is broken,
44792747Sbustos  * ECONNABORTED will be returned, though a _DISABLE command will be sent for
44802747Sbustos  * the vertex anyway.
44811958Slianep  */
44822747Sbustos int
44831958Slianep vertex_subgraph_dependencies_shutdown(scf_handle_t *h, graph_vertex_t *v,
44842747Sbustos     restarter_instance_state_t old_state)
44851958Slianep {
44862747Sbustos 	int was_up, now_up;
44872747Sbustos 	int ret = 0;
44882747Sbustos 
44892747Sbustos 	assert(v->gv_type == GVT_INST);
44902747Sbustos 
44912747Sbustos 	/* Don't care if we're not going to a milestone. */
44922747Sbustos 	if (milestone == NULL)
44932747Sbustos 		return (0);
44942747Sbustos 
44952747Sbustos 	/* Don't care if we already finished coming down. */
44962747Sbustos 	if (non_subgraph_svcs == 0)
44972747Sbustos 		return (0);
44982747Sbustos 
44992747Sbustos 	/* Don't care if the service is in the subgraph. */
45002747Sbustos 	if (v->gv_flags & GV_INSUBGRAPH)
45012747Sbustos 		return (0);
45022747Sbustos 
45032747Sbustos 	/*
45042747Sbustos 	 * Update non_subgraph_svcs.  It is the number of non-subgraph
45052747Sbustos 	 * services which are in online, degraded, or offline.
45062747Sbustos 	 */
45072747Sbustos 
45082747Sbustos 	was_up = up_state(old_state);
45092747Sbustos 	now_up = up_state(v->gv_state);
45102747Sbustos 
45112747Sbustos 	if (!was_up && now_up) {
45122747Sbustos 		++non_subgraph_svcs;
45132747Sbustos 	} else if (was_up && !now_up) {
45140Sstevel@tonic-gate 		--non_subgraph_svcs;
45152747Sbustos 
45160Sstevel@tonic-gate 		if (non_subgraph_svcs == 0) {
45170Sstevel@tonic-gate 			if (halting != -1) {
45180Sstevel@tonic-gate 				do_uadmin();
45190Sstevel@tonic-gate 			} else if (go_single_user_mode || go_to_level1) {
45200Sstevel@tonic-gate 				(void) startd_thread_create(single_user_thread,
45210Sstevel@tonic-gate 				    NULL);
45220Sstevel@tonic-gate 			}
45232747Sbustos 			return (0);
45240Sstevel@tonic-gate 		}
45250Sstevel@tonic-gate 	}
45262747Sbustos 
45272747Sbustos 	/* If this service is a leaf, it should be disabled. */
45282747Sbustos 	if ((v->gv_flags & GV_ENABLED) && is_nonsubgraph_leaf(v)) {
45292747Sbustos 		int r;
45302747Sbustos 
45312747Sbustos 		r = disable_service_temporarily(v, h);
45322747Sbustos 		switch (r) {
45332747Sbustos 		case 0:
45342747Sbustos 			break;
45352747Sbustos 
45362747Sbustos 		case ECONNABORTED:
45372747Sbustos 			ret = ECONNABORTED;
45382747Sbustos 			break;
45392747Sbustos 
45402747Sbustos 		default:
45412747Sbustos 			bad_error("disable_service_temporarily", r);
45422747Sbustos 		}
45432747Sbustos 	}
45442747Sbustos 
45452747Sbustos 	/*
45462747Sbustos 	 * If the service just came down, propagate the disable to the newly
45472747Sbustos 	 * exposed leaves.
45482747Sbustos 	 */
45492747Sbustos 	if (was_up && !now_up)
45502747Sbustos 		graph_walk_dependencies(v, disable_nonsubgraph_leaves,
45512747Sbustos 		    (void *)h);
45522747Sbustos 
45532747Sbustos 	return (ret);
45541958Slianep }
45551958Slianep 
45561958Slianep /*
45571958Slianep  * Decide whether to start up an sulogin thread after a service is
45581958Slianep  * finished changing state.  Only need to do the full can_come_up()
45591958Slianep  * evaluation if an instance is changing state, we're not halfway through
45601958Slianep  * loading the thread, and we aren't shutting down or going to the single
45611958Slianep  * user milestone.
45621958Slianep  */
45631958Slianep void
45641958Slianep graph_transition_sulogin(restarter_instance_state_t state,
45651958Slianep     restarter_instance_state_t old_state)
45661958Slianep {
45671958Slianep 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
45680Sstevel@tonic-gate 
45690Sstevel@tonic-gate 	if (state != old_state && st->st_load_complete &&
45700Sstevel@tonic-gate 	    !go_single_user_mode && !go_to_level1 &&
45710Sstevel@tonic-gate 	    halting == -1) {
45723639Srm88369 		if (!sulogin_thread_running && !can_come_up()) {
45730Sstevel@tonic-gate 			(void) startd_thread_create(sulogin_thread, NULL);
45740Sstevel@tonic-gate 			sulogin_thread_running = B_TRUE;
45750Sstevel@tonic-gate 		}
45760Sstevel@tonic-gate 	}
45771958Slianep }
45781958Slianep 
45791958Slianep /*
45802339Slianep  * Propagate a start, stop event, or a satisfiability event.
45811958Slianep  *
45822339Slianep  * PROPAGATE_START and PROPAGATE_STOP simply propagate the transition event
45832339Slianep  * to direct dependents.  PROPAGATE_SAT propagates a start then walks the
45842339Slianep  * full dependent graph to check for newly satisfied nodes.  This is
45852339Slianep  * necessary for cases when non-direct dependents may be effected but direct
45862339Slianep  * dependents may not (e.g. for optional_all evaluations, see the
45872339Slianep  * propagate_satbility() comments).
45882339Slianep  *
45892339Slianep  * PROPAGATE_SAT should be used whenever a non-running service moves into
45902339Slianep  * a state which can satisfy optional dependencies, like disabled or
45912339Slianep  * maintenance.
45921958Slianep  */
45931958Slianep void
45942339Slianep graph_transition_propagate(graph_vertex_t *v, propagate_event_t type,
45951958Slianep     restarter_error_t rerr)
45961958Slianep {
45972339Slianep 	if (type == PROPAGATE_STOP) {
45981958Slianep 		graph_walk_dependents(v, propagate_stop, (void *)rerr);
45992339Slianep 	} else if (type == PROPAGATE_START || type == PROPAGATE_SAT) {
46001958Slianep 		graph_walk_dependents(v, propagate_start, NULL);
46011958Slianep 
46022339Slianep 		if (type == PROPAGATE_SAT)
46031958Slianep 			propagate_satbility(v);
46041958Slianep 	} else {
46051958Slianep #ifndef NDEBUG
46062339Slianep 		uu_warn("%s:%d: Unexpected type value %d.\n",  __FILE__,
46072339Slianep 		    __LINE__, type);
46081958Slianep #endif
46091958Slianep 		abort();
46101958Slianep 	}
46110Sstevel@tonic-gate }
46120Sstevel@tonic-gate 
46130Sstevel@tonic-gate /*
46140Sstevel@tonic-gate  * If a vertex for fmri exists and it is enabled, send _DISABLE to the
46150Sstevel@tonic-gate  * restarter.  If it is running, send _STOP.  Send _REMOVE_INSTANCE.  Delete
46160Sstevel@tonic-gate  * all property group dependencies, and the dependency on the restarter,
46170Sstevel@tonic-gate  * disposing of vertices as appropriate.  If other vertices depend on this
46180Sstevel@tonic-gate  * one, mark it unconfigured and return.  Otherwise remove the vertex.  Always
46190Sstevel@tonic-gate  * returns 0.
46200Sstevel@tonic-gate  */
46210Sstevel@tonic-gate static int
46220Sstevel@tonic-gate dgraph_remove_instance(const char *fmri, scf_handle_t *h)
46230Sstevel@tonic-gate {
46240Sstevel@tonic-gate 	graph_vertex_t *v;
46250Sstevel@tonic-gate 	graph_edge_t *e;
46260Sstevel@tonic-gate 	uu_list_t *old_deps;
46270Sstevel@tonic-gate 	int err;
46280Sstevel@tonic-gate 
46290Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph engine: Removing %s.\n", fmri);
46300Sstevel@tonic-gate 
46310Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
46320Sstevel@tonic-gate 
46330Sstevel@tonic-gate 	v = vertex_get_by_name(fmri);
46340Sstevel@tonic-gate 	if (v == NULL) {
46350Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
46360Sstevel@tonic-gate 		return (0);
46370Sstevel@tonic-gate 	}
46380Sstevel@tonic-gate 
46390Sstevel@tonic-gate 	/* Send restarter delete event. */
46400Sstevel@tonic-gate 	if (v->gv_flags & GV_CONFIGURED)
46410Sstevel@tonic-gate 		graph_unset_restarter(v);
46420Sstevel@tonic-gate 
46430Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
46440Sstevel@tonic-gate 		/*
46450Sstevel@tonic-gate 		 * Make a list of v's current dependencies so we can
46460Sstevel@tonic-gate 		 * reevaluate their GV_INSUBGRAPH flags after the dependencies
46470Sstevel@tonic-gate 		 * are removed.
46480Sstevel@tonic-gate 		 */
46490Sstevel@tonic-gate 		old_deps = startd_list_create(graph_edge_pool, NULL, 0);
46500Sstevel@tonic-gate 
46510Sstevel@tonic-gate 		err = uu_list_walk(v->gv_dependencies,
46521712Srm88369 		    (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0);
46530Sstevel@tonic-gate 		assert(err == 0);
46540Sstevel@tonic-gate 	}
46550Sstevel@tonic-gate 
46560Sstevel@tonic-gate 	delete_instance_dependencies(v, B_TRUE);
46570Sstevel@tonic-gate 
46580Sstevel@tonic-gate 	/*
46590Sstevel@tonic-gate 	 * Deleting an instance can both satisfy and unsatisfy dependencies,
46600Sstevel@tonic-gate 	 * depending on their type.  First propagate the stop as a RERR_RESTART
46610Sstevel@tonic-gate 	 * event -- deletion isn't a fault, just a normal stop.  This gives
46620Sstevel@tonic-gate 	 * dependent services the chance to do a clean shutdown.  Then, mark
46630Sstevel@tonic-gate 	 * the service as unconfigured and propagate the start event for the
46640Sstevel@tonic-gate 	 * optional_all dependencies that might have become satisfied.
46650Sstevel@tonic-gate 	 */
46660Sstevel@tonic-gate 	graph_walk_dependents(v, propagate_stop, (void *)RERR_RESTART);
46670Sstevel@tonic-gate 
46680Sstevel@tonic-gate 	v->gv_flags &= ~GV_CONFIGURED;
46697475SPhilippe.Jung@Sun.COM 	v->gv_flags &= ~GV_DEATHROW;
46700Sstevel@tonic-gate 
46710Sstevel@tonic-gate 	graph_walk_dependents(v, propagate_start, NULL);
46720Sstevel@tonic-gate 	propagate_satbility(v);
46730Sstevel@tonic-gate 
46740Sstevel@tonic-gate 	/*
46750Sstevel@tonic-gate 	 * If there are no (non-service) dependents, the vertex can be
46760Sstevel@tonic-gate 	 * completely removed.
46770Sstevel@tonic-gate 	 */
46781712Srm88369 	if (v != milestone && v->gv_refs == 0 &&
46791712Srm88369 	    uu_list_numnodes(v->gv_dependents) == 1)
46800Sstevel@tonic-gate 		remove_inst_vertex(v);
46810Sstevel@tonic-gate 
46820Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
46830Sstevel@tonic-gate 		void *cookie = NULL;
46840Sstevel@tonic-gate 
46850Sstevel@tonic-gate 		while ((e = uu_list_teardown(old_deps, &cookie)) != NULL) {
46861712Srm88369 			v = e->ge_vertex;
46871712Srm88369 
46881712Srm88369 			if (vertex_unref(v) == VERTEX_INUSE)
46891712Srm88369 				while (eval_subgraph(v, h) == ECONNABORTED)
46901712Srm88369 					libscf_handle_rebind(h);
46910Sstevel@tonic-gate 
46920Sstevel@tonic-gate 			startd_free(e, sizeof (*e));
46930Sstevel@tonic-gate 		}
46940Sstevel@tonic-gate 
46950Sstevel@tonic-gate 		uu_list_destroy(old_deps);
46960Sstevel@tonic-gate 	}
46970Sstevel@tonic-gate 
46980Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
46990Sstevel@tonic-gate 
47000Sstevel@tonic-gate 	return (0);
47010Sstevel@tonic-gate }
47020Sstevel@tonic-gate 
47030Sstevel@tonic-gate /*
47040Sstevel@tonic-gate  * Return the eventual (maybe current) milestone in the form of a
47050Sstevel@tonic-gate  * legacy runlevel.
47060Sstevel@tonic-gate  */
47070Sstevel@tonic-gate static char
47080Sstevel@tonic-gate target_milestone_as_runlevel()
47090Sstevel@tonic-gate {
47100Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
47110Sstevel@tonic-gate 
47120Sstevel@tonic-gate 	if (milestone == NULL)
47130Sstevel@tonic-gate 		return ('3');
47140Sstevel@tonic-gate 	else if (milestone == MILESTONE_NONE)
47150Sstevel@tonic-gate 		return ('0');
47160Sstevel@tonic-gate 
47170Sstevel@tonic-gate 	if (strcmp(milestone->gv_name, multi_user_fmri) == 0)
47180Sstevel@tonic-gate 		return ('2');
47190Sstevel@tonic-gate 	else if (strcmp(milestone->gv_name, single_user_fmri) == 0)
47200Sstevel@tonic-gate 		return ('S');
47210Sstevel@tonic-gate 	else if (strcmp(milestone->gv_name, multi_user_svr_fmri) == 0)
47220Sstevel@tonic-gate 		return ('3');
47230Sstevel@tonic-gate 
47240Sstevel@tonic-gate #ifndef NDEBUG
47250Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:%d: Unknown milestone name \"%s\".\n",
47260Sstevel@tonic-gate 	    __FILE__, __LINE__, milestone->gv_name);
47270Sstevel@tonic-gate #endif
47280Sstevel@tonic-gate 	abort();
47290Sstevel@tonic-gate 	/* NOTREACHED */
47300Sstevel@tonic-gate }
47310Sstevel@tonic-gate 
47320Sstevel@tonic-gate static struct {
47330Sstevel@tonic-gate 	char	rl;
47340Sstevel@tonic-gate 	int	sig;
47350Sstevel@tonic-gate } init_sigs[] = {
47360Sstevel@tonic-gate 	{ 'S', SIGBUS },
47370Sstevel@tonic-gate 	{ '0', SIGINT },
47380Sstevel@tonic-gate 	{ '1', SIGQUIT },
47390Sstevel@tonic-gate 	{ '2', SIGILL },
47400Sstevel@tonic-gate 	{ '3', SIGTRAP },
47410Sstevel@tonic-gate 	{ '4', SIGIOT },
47420Sstevel@tonic-gate 	{ '5', SIGEMT },
47430Sstevel@tonic-gate 	{ '6', SIGFPE },
47440Sstevel@tonic-gate 	{ 0, 0 }
47450Sstevel@tonic-gate };
47460Sstevel@tonic-gate 
47470Sstevel@tonic-gate static void
47480Sstevel@tonic-gate signal_init(char rl)
47490Sstevel@tonic-gate {
47500Sstevel@tonic-gate 	pid_t init_pid;
47510Sstevel@tonic-gate 	int i;
47520Sstevel@tonic-gate 
47530Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
47540Sstevel@tonic-gate 
47550Sstevel@tonic-gate 	if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
47560Sstevel@tonic-gate 	    sizeof (init_pid)) != sizeof (init_pid)) {
47570Sstevel@tonic-gate 		log_error(LOG_NOTICE, "Could not get pid to signal init.\n");
47580Sstevel@tonic-gate 		return;
47590Sstevel@tonic-gate 	}
47600Sstevel@tonic-gate 
47610Sstevel@tonic-gate 	for (i = 0; init_sigs[i].rl != 0; ++i)
47620Sstevel@tonic-gate 		if (init_sigs[i].rl == rl)
47630Sstevel@tonic-gate 			break;
47640Sstevel@tonic-gate 
47650Sstevel@tonic-gate 	if (init_sigs[i].rl != 0) {
47660Sstevel@tonic-gate 		if (kill(init_pid, init_sigs[i].sig) != 0) {
47670Sstevel@tonic-gate 			switch (errno) {
47680Sstevel@tonic-gate 			case EPERM:
47690Sstevel@tonic-gate 			case ESRCH:
47700Sstevel@tonic-gate 				log_error(LOG_NOTICE, "Could not signal init: "
47710Sstevel@tonic-gate 				    "%s.\n", strerror(errno));
47720Sstevel@tonic-gate 				break;
47730Sstevel@tonic-gate 
47740Sstevel@tonic-gate 			case EINVAL:
47750Sstevel@tonic-gate 			default:
47760Sstevel@tonic-gate 				bad_error("kill", errno);
47770Sstevel@tonic-gate 			}
47780Sstevel@tonic-gate 		}
47790Sstevel@tonic-gate 	}
47800Sstevel@tonic-gate }
47810Sstevel@tonic-gate 
47820Sstevel@tonic-gate /*
47830Sstevel@tonic-gate  * This is called when one of the major milestones changes state, or when
47840Sstevel@tonic-gate  * init is signalled and tells us it was told to change runlevel.  We wait
47850Sstevel@tonic-gate  * to reach the milestone because this allows /etc/inittab entries to retain
47860Sstevel@tonic-gate  * some boot ordering: historically, entries could place themselves before/after
47870Sstevel@tonic-gate  * the running of /sbin/rcX scripts but we can no longer make the
47880Sstevel@tonic-gate  * distinction because the /sbin/rcX scripts no longer exist as punctuation
47890Sstevel@tonic-gate  * marks in /etc/inittab.
47900Sstevel@tonic-gate  *
47910Sstevel@tonic-gate  * Also, we only trigger an update when we reach the eventual target
47920Sstevel@tonic-gate  * milestone: without this, an /etc/inittab entry marked only for
47930Sstevel@tonic-gate  * runlevel 2 would be executed for runlevel 3, which is not how
47940Sstevel@tonic-gate  * /etc/inittab entries work.
47950Sstevel@tonic-gate  *
47960Sstevel@tonic-gate  * If we're single user coming online, then we set utmpx to the target
47970Sstevel@tonic-gate  * runlevel so that legacy scripts can work as expected.
47980Sstevel@tonic-gate  */
47990Sstevel@tonic-gate static void
48000Sstevel@tonic-gate graph_runlevel_changed(char rl, int online)
48010Sstevel@tonic-gate {
48020Sstevel@tonic-gate 	char trl;
48030Sstevel@tonic-gate 
48040Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
48050Sstevel@tonic-gate 
48060Sstevel@tonic-gate 	trl = target_milestone_as_runlevel();
48070Sstevel@tonic-gate 
48080Sstevel@tonic-gate 	if (online) {
48090Sstevel@tonic-gate 		if (rl == trl) {
48101514Srm88369 			current_runlevel = trl;
48110Sstevel@tonic-gate 			signal_init(trl);
48120Sstevel@tonic-gate 		} else if (rl == 'S') {
48130Sstevel@tonic-gate 			/*
48140Sstevel@tonic-gate 			 * At boot, set the entry early for the benefit of the
48150Sstevel@tonic-gate 			 * legacy init scripts.
48160Sstevel@tonic-gate 			 */
48170Sstevel@tonic-gate 			utmpx_set_runlevel(trl, 'S', B_FALSE);
48180Sstevel@tonic-gate 		}
48190Sstevel@tonic-gate 	} else {
48200Sstevel@tonic-gate 		if (rl == '3' && trl == '2') {
48211514Srm88369 			current_runlevel = trl;
48220Sstevel@tonic-gate 			signal_init(trl);
48230Sstevel@tonic-gate 		} else if (rl == '2' && trl == 'S') {
48241514Srm88369 			current_runlevel = trl;
48250Sstevel@tonic-gate 			signal_init(trl);
48260Sstevel@tonic-gate 		}
48270Sstevel@tonic-gate 	}
48280Sstevel@tonic-gate }
48290Sstevel@tonic-gate 
48300Sstevel@tonic-gate /*
48310Sstevel@tonic-gate  * Move to a backwards-compatible runlevel by executing the appropriate
48320Sstevel@tonic-gate  * /etc/rc?.d/K* scripts and/or setting the milestone.
48330Sstevel@tonic-gate  *
48340Sstevel@tonic-gate  * Returns
48350Sstevel@tonic-gate  *   0 - success
48360Sstevel@tonic-gate  *   ECONNRESET - success, but handle was reset
48370Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
48380Sstevel@tonic-gate  *   ECANCELED - pg was deleted
48390Sstevel@tonic-gate  */
48400Sstevel@tonic-gate static int
48410Sstevel@tonic-gate dgraph_set_runlevel(scf_propertygroup_t *pg, scf_property_t *prop)
48420Sstevel@tonic-gate {
48430Sstevel@tonic-gate 	char rl;
48440Sstevel@tonic-gate 	scf_handle_t *h;
48450Sstevel@tonic-gate 	int r;
48460Sstevel@tonic-gate 	const char *ms = NULL;	/* what to commit as options/milestone */
48470Sstevel@tonic-gate 	boolean_t rebound = B_FALSE;
48480Sstevel@tonic-gate 	int mark_rl = 0;
48490Sstevel@tonic-gate 
48500Sstevel@tonic-gate 	const char * const stop = "stop";
48510Sstevel@tonic-gate 
48520Sstevel@tonic-gate 	r = libscf_extract_runlevel(prop, &rl);
48530Sstevel@tonic-gate 	switch (r) {
48540Sstevel@tonic-gate 	case 0:
48550Sstevel@tonic-gate 		break;
48560Sstevel@tonic-gate 
48570Sstevel@tonic-gate 	case ECONNABORTED:
48580Sstevel@tonic-gate 	case ECANCELED:
48590Sstevel@tonic-gate 		return (r);
48600Sstevel@tonic-gate 
48610Sstevel@tonic-gate 	case EINVAL:
48620Sstevel@tonic-gate 	case ENOENT:
48630Sstevel@tonic-gate 		log_error(LOG_WARNING, "runlevel property is misconfigured; "
48640Sstevel@tonic-gate 		    "ignoring.\n");
48650Sstevel@tonic-gate 		/* delete the bad property */
48660Sstevel@tonic-gate 		goto nolock_out;
48670Sstevel@tonic-gate 
48680Sstevel@tonic-gate 	default:
48690Sstevel@tonic-gate 		bad_error("libscf_extract_runlevel", r);
48700Sstevel@tonic-gate 	}
48710Sstevel@tonic-gate 
48720Sstevel@tonic-gate 	switch (rl) {
48730Sstevel@tonic-gate 	case 's':
48740Sstevel@tonic-gate 		rl = 'S';
48750Sstevel@tonic-gate 		/* FALLTHROUGH */
48760Sstevel@tonic-gate 
48770Sstevel@tonic-gate 	case 'S':
48780Sstevel@tonic-gate 	case '2':
48790Sstevel@tonic-gate 	case '3':
48800Sstevel@tonic-gate 		/*
48810Sstevel@tonic-gate 		 * These cases cause a milestone change, so
48820Sstevel@tonic-gate 		 * graph_runlevel_changed() will eventually deal with
48830Sstevel@tonic-gate 		 * signalling init.
48840Sstevel@tonic-gate 		 */
48850Sstevel@tonic-gate 		break;
48860Sstevel@tonic-gate 
48870Sstevel@tonic-gate 	case '0':
48880Sstevel@tonic-gate 	case '1':
48890Sstevel@tonic-gate 	case '4':
48900Sstevel@tonic-gate 	case '5':
48910Sstevel@tonic-gate 	case '6':
48920Sstevel@tonic-gate 		mark_rl = 1;
48930Sstevel@tonic-gate 		break;
48940Sstevel@tonic-gate 
48950Sstevel@tonic-gate 	default:
48960Sstevel@tonic-gate 		log_framework(LOG_NOTICE, "Unknown runlevel '%c'.\n", rl);
48970Sstevel@tonic-gate 		ms = NULL;
48980Sstevel@tonic-gate 		goto nolock_out;
48990Sstevel@tonic-gate 	}
49000Sstevel@tonic-gate 
49010Sstevel@tonic-gate 	h = scf_pg_handle(pg);
49020Sstevel@tonic-gate 
49030Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
49040Sstevel@tonic-gate 
49050Sstevel@tonic-gate 	/*
49060Sstevel@tonic-gate 	 * Since this triggers no milestone changes, force it by hand.
49070Sstevel@tonic-gate 	 */
49080Sstevel@tonic-gate 	if (current_runlevel == '4' && rl == '3')
49090Sstevel@tonic-gate 		mark_rl = 1;
49100Sstevel@tonic-gate 
49111514Srm88369 	/*
49121514Srm88369 	 * 1. If we are here after an "init X":
49131514Srm88369 	 *
49141514Srm88369 	 * init X
49151514Srm88369 	 *	init/lscf_set_runlevel()
49161514Srm88369 	 *		process_pg_event()
49171514Srm88369 	 *		dgraph_set_runlevel()
49181514Srm88369 	 *
49191514Srm88369 	 * then we haven't passed through graph_runlevel_changed() yet,
49201514Srm88369 	 * therefore 'current_runlevel' has not changed for sure but 'rl' has.
49211514Srm88369 	 * In consequence, if 'rl' is lower than 'current_runlevel', we change
49221514Srm88369 	 * the system runlevel and execute the appropriate /etc/rc?.d/K* scripts
49231514Srm88369 	 * past this test.
49241514Srm88369 	 *
49251514Srm88369 	 * 2. On the other hand, if we are here after a "svcadm milestone":
49261514Srm88369 	 *
49271514Srm88369 	 * svcadm milestone X
49281514Srm88369 	 *	dgraph_set_milestone()
49291514Srm88369 	 *		handle_graph_update_event()
49301514Srm88369 	 *		dgraph_set_instance_state()
49311514Srm88369 	 *		graph_post_X_[online|offline]()
49321514Srm88369 	 *		graph_runlevel_changed()
49331514Srm88369 	 *		signal_init()
49341514Srm88369 	 *			init/lscf_set_runlevel()
49351514Srm88369 	 *				process_pg_event()
49361514Srm88369 	 *				dgraph_set_runlevel()
49371514Srm88369 	 *
49381514Srm88369 	 * then we already passed through graph_runlevel_changed() (by the way
49391514Srm88369 	 * of dgraph_set_milestone()) and 'current_runlevel' may have changed
49401514Srm88369 	 * and already be equal to 'rl' so we are going to return immediately
49411514Srm88369 	 * from dgraph_set_runlevel() without changing the system runlevel and
49421514Srm88369 	 * without executing the /etc/rc?.d/K* scripts.
49431514Srm88369 	 */
49440Sstevel@tonic-gate 	if (rl == current_runlevel) {
49450Sstevel@tonic-gate 		ms = NULL;
49460Sstevel@tonic-gate 		goto out;
49470Sstevel@tonic-gate 	}
49480Sstevel@tonic-gate 
49490Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Changing to runlevel '%c'.\n", rl);
49500Sstevel@tonic-gate 
49510Sstevel@tonic-gate 	/*
49520Sstevel@tonic-gate 	 * Make sure stop rc scripts see the new settings via who -r.
49530Sstevel@tonic-gate 	 */
49540Sstevel@tonic-gate 	utmpx_set_runlevel(rl, current_runlevel, B_TRUE);
49550Sstevel@tonic-gate 
49560Sstevel@tonic-gate 	/*
49570Sstevel@tonic-gate 	 * Some run levels don't have a direct correspondence to any
49580Sstevel@tonic-gate 	 * milestones, so we have to signal init directly.
49590Sstevel@tonic-gate 	 */
49600Sstevel@tonic-gate 	if (mark_rl) {
49610Sstevel@tonic-gate 		current_runlevel = rl;
49620Sstevel@tonic-gate 		signal_init(rl);
49630Sstevel@tonic-gate 	}
49640Sstevel@tonic-gate 
49650Sstevel@tonic-gate 	switch (rl) {
49660Sstevel@tonic-gate 	case 'S':
49670Sstevel@tonic-gate 		uu_warn("The system is coming down for administration.  "
49680Sstevel@tonic-gate 		    "Please wait.\n");
49690Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_FALSE);
49700Sstevel@tonic-gate 		ms = single_user_fmri;
49710Sstevel@tonic-gate 		go_single_user_mode = B_TRUE;
49720Sstevel@tonic-gate 		break;
49730Sstevel@tonic-gate 
49740Sstevel@tonic-gate 	case '0':
49750Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
49760Sstevel@tonic-gate 		halting = AD_HALT;
49770Sstevel@tonic-gate 		goto uadmin;
49780Sstevel@tonic-gate 
49790Sstevel@tonic-gate 	case '5':
49800Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
49810Sstevel@tonic-gate 		halting = AD_POWEROFF;
49820Sstevel@tonic-gate 		goto uadmin;
49830Sstevel@tonic-gate 
49840Sstevel@tonic-gate 	case '6':
49850Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
49860Sstevel@tonic-gate 		halting = AD_BOOT;
49870Sstevel@tonic-gate 		goto uadmin;
49880Sstevel@tonic-gate 
49890Sstevel@tonic-gate uadmin:
49900Sstevel@tonic-gate 		uu_warn("The system is coming down.  Please wait.\n");
49910Sstevel@tonic-gate 		ms = "none";
49920Sstevel@tonic-gate 
49930Sstevel@tonic-gate 		/*
49940Sstevel@tonic-gate 		 * We can't wait until all services are offline since this
49950Sstevel@tonic-gate 		 * thread is responsible for taking them offline.  Instead we
49960Sstevel@tonic-gate 		 * set halting to the second argument for uadmin() and call
49970Sstevel@tonic-gate 		 * do_uadmin() from dgraph_set_instance_state() when
49980Sstevel@tonic-gate 		 * appropriate.
49990Sstevel@tonic-gate 		 */
50000Sstevel@tonic-gate 		break;
50010Sstevel@tonic-gate 
50020Sstevel@tonic-gate 	case '1':
50030Sstevel@tonic-gate 		if (current_runlevel != 'S') {
50040Sstevel@tonic-gate 			uu_warn("Changing to state 1.\n");
50050Sstevel@tonic-gate 			fork_rc_script(rl, stop, B_FALSE);
50060Sstevel@tonic-gate 		} else {
50070Sstevel@tonic-gate 			uu_warn("The system is coming up for administration.  "
50080Sstevel@tonic-gate 			    "Please wait.\n");
50090Sstevel@tonic-gate 		}
50100Sstevel@tonic-gate 		ms = single_user_fmri;
50110Sstevel@tonic-gate 		go_to_level1 = B_TRUE;
50120Sstevel@tonic-gate 		break;
50130Sstevel@tonic-gate 
50140Sstevel@tonic-gate 	case '2':
50150Sstevel@tonic-gate 		if (current_runlevel == '3' || current_runlevel == '4')
50160Sstevel@tonic-gate 			fork_rc_script(rl, stop, B_FALSE);
50170Sstevel@tonic-gate 		ms = multi_user_fmri;
50180Sstevel@tonic-gate 		break;
50190Sstevel@tonic-gate 
50200Sstevel@tonic-gate 	case '3':
50210Sstevel@tonic-gate 	case '4':
50220Sstevel@tonic-gate 		ms = "all";
50230Sstevel@tonic-gate 		break;
50240Sstevel@tonic-gate 
50250Sstevel@tonic-gate 	default:
50260Sstevel@tonic-gate #ifndef NDEBUG
50270Sstevel@tonic-gate 		(void) fprintf(stderr, "%s:%d: Uncaught case %d ('%c').\n",
50280Sstevel@tonic-gate 		    __FILE__, __LINE__, rl, rl);
50290Sstevel@tonic-gate #endif
50300Sstevel@tonic-gate 		abort();
50310Sstevel@tonic-gate 	}
50320Sstevel@tonic-gate 
50330Sstevel@tonic-gate out:
50340Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
50350Sstevel@tonic-gate 
50360Sstevel@tonic-gate nolock_out:
50370Sstevel@tonic-gate 	switch (r = libscf_clear_runlevel(pg, ms)) {
50380Sstevel@tonic-gate 	case 0:
50390Sstevel@tonic-gate 		break;
50400Sstevel@tonic-gate 
50410Sstevel@tonic-gate 	case ECONNABORTED:
50420Sstevel@tonic-gate 		libscf_handle_rebind(h);
50430Sstevel@tonic-gate 		rebound = B_TRUE;
50440Sstevel@tonic-gate 		goto nolock_out;
50450Sstevel@tonic-gate 
50460Sstevel@tonic-gate 	case ECANCELED:
50470Sstevel@tonic-gate 		break;
50480Sstevel@tonic-gate 
50490Sstevel@tonic-gate 	case EPERM:
50500Sstevel@tonic-gate 	case EACCES:
50510Sstevel@tonic-gate 	case EROFS:
50520Sstevel@tonic-gate 		log_error(LOG_NOTICE, "Could not delete \"%s/%s\" property: "
50530Sstevel@tonic-gate 		    "%s.\n", SCF_PG_OPTIONS, "runlevel", strerror(r));
50540Sstevel@tonic-gate 		break;
50550Sstevel@tonic-gate 
50560Sstevel@tonic-gate 	default:
50570Sstevel@tonic-gate 		bad_error("libscf_clear_runlevel", r);
50580Sstevel@tonic-gate 	}
50590Sstevel@tonic-gate 
50600Sstevel@tonic-gate 	return (rebound ? ECONNRESET : 0);
50610Sstevel@tonic-gate }
50620Sstevel@tonic-gate 
50637630SRenaud.Manus@Sun.COM /*
50647630SRenaud.Manus@Sun.COM  * mark_subtree walks the dependents and add the GV_TOOFFLINE flag
50657630SRenaud.Manus@Sun.COM  * to the instances that are supposed to go offline during an
50667630SRenaud.Manus@Sun.COM  * administrative disable operation.
50677630SRenaud.Manus@Sun.COM  */
50687630SRenaud.Manus@Sun.COM static int
50697630SRenaud.Manus@Sun.COM mark_subtree(graph_edge_t *e, void *arg)
50707630SRenaud.Manus@Sun.COM {
50717630SRenaud.Manus@Sun.COM 	graph_vertex_t *v;
50727630SRenaud.Manus@Sun.COM 	int r;
50737630SRenaud.Manus@Sun.COM 
50747630SRenaud.Manus@Sun.COM 	v = e->ge_vertex;
50757630SRenaud.Manus@Sun.COM 
50767630SRenaud.Manus@Sun.COM 	/* If it's already in the subgraph, skip. */
50777630SRenaud.Manus@Sun.COM 	if (v->gv_flags & GV_TOOFFLINE)
50787630SRenaud.Manus@Sun.COM 		return (UU_WALK_NEXT);
50797630SRenaud.Manus@Sun.COM 
50807630SRenaud.Manus@Sun.COM 	switch (v->gv_type) {
50817630SRenaud.Manus@Sun.COM 	case GVT_INST:
50827630SRenaud.Manus@Sun.COM 		/* If the instance is already disabled, skip it. */
50837630SRenaud.Manus@Sun.COM 		if (!(v->gv_flags & GV_ENABLED))
50847630SRenaud.Manus@Sun.COM 			return (UU_WALK_NEXT);
50857630SRenaud.Manus@Sun.COM 
50867630SRenaud.Manus@Sun.COM 		v->gv_flags |= GV_TOOFFLINE;
50877630SRenaud.Manus@Sun.COM 		log_framework(LOG_DEBUG, "%s added to subtree\n", v->gv_name);
50887630SRenaud.Manus@Sun.COM 		break;
50897630SRenaud.Manus@Sun.COM 	case GVT_GROUP:
50907630SRenaud.Manus@Sun.COM 		/*
5091*8354SRenaud.Manus@Sun.COM 		 * Skip all excluded and optional_all dependencies and decide
5092*8354SRenaud.Manus@Sun.COM 		 * whether to offline the service based on restart_on attribute.
50937630SRenaud.Manus@Sun.COM 		 */
50947630SRenaud.Manus@Sun.COM 		if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL ||
5095*8354SRenaud.Manus@Sun.COM 		    v->gv_depgroup == DEPGRP_OPTIONAL_ALL ||
50967630SRenaud.Manus@Sun.COM 		    v->gv_restart < RERR_RESTART)
50977630SRenaud.Manus@Sun.COM 			return (UU_WALK_NEXT);
50987630SRenaud.Manus@Sun.COM 		break;
50997630SRenaud.Manus@Sun.COM 	}
51007630SRenaud.Manus@Sun.COM 
51017630SRenaud.Manus@Sun.COM 	r = uu_list_walk(v->gv_dependents, (uu_walk_fn_t *)mark_subtree, arg,
51027630SRenaud.Manus@Sun.COM 	    0);
51037630SRenaud.Manus@Sun.COM 	assert(r == 0);
51047630SRenaud.Manus@Sun.COM 	return (UU_WALK_NEXT);
51057630SRenaud.Manus@Sun.COM }
51067630SRenaud.Manus@Sun.COM 
51070Sstevel@tonic-gate static int
51080Sstevel@tonic-gate mark_subgraph(graph_edge_t *e, void *arg)
51090Sstevel@tonic-gate {
51100Sstevel@tonic-gate 	graph_vertex_t *v;
51110Sstevel@tonic-gate 	int r;
51120Sstevel@tonic-gate 	int optional = (int)arg;
51130Sstevel@tonic-gate 
51140Sstevel@tonic-gate 	v = e->ge_vertex;
51150Sstevel@tonic-gate 
51160Sstevel@tonic-gate 	/* If it's already in the subgraph, skip. */
51170Sstevel@tonic-gate 	if (v->gv_flags & GV_INSUBGRAPH)
51180Sstevel@tonic-gate 		return (UU_WALK_NEXT);
51190Sstevel@tonic-gate 
51200Sstevel@tonic-gate 	/*
51210Sstevel@tonic-gate 	 * Keep track if walk has entered an optional dependency group
51220Sstevel@tonic-gate 	 */
51230Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_OPTIONAL_ALL) {
51240Sstevel@tonic-gate 		optional = 1;
51250Sstevel@tonic-gate 	}
51260Sstevel@tonic-gate 	/*
51270Sstevel@tonic-gate 	 * Quit if we are in an optional dependency group and the instance
51280Sstevel@tonic-gate 	 * is disabled
51290Sstevel@tonic-gate 	 */
51300Sstevel@tonic-gate 	if (optional && (v->gv_type == GVT_INST) &&
51310Sstevel@tonic-gate 	    (!(v->gv_flags & GV_ENBLD_NOOVR)))
51320Sstevel@tonic-gate 		return (UU_WALK_NEXT);
51330Sstevel@tonic-gate 
51340Sstevel@tonic-gate 	v->gv_flags |= GV_INSUBGRAPH;
51350Sstevel@tonic-gate 
51360Sstevel@tonic-gate 	/* Skip all excluded dependencies. */
51370Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
51380Sstevel@tonic-gate 		return (UU_WALK_NEXT);
51390Sstevel@tonic-gate 
51400Sstevel@tonic-gate 	r = uu_list_walk(v->gv_dependencies, (uu_walk_fn_t *)mark_subgraph,
51410Sstevel@tonic-gate 	    (void *)optional, 0);
51420Sstevel@tonic-gate 	assert(r == 0);
51430Sstevel@tonic-gate 	return (UU_WALK_NEXT);
51440Sstevel@tonic-gate }
51450Sstevel@tonic-gate 
51460Sstevel@tonic-gate /*
51472747Sbustos  * Bring down all services which are not dependencies of fmri.  The
51482747Sbustos  * dependencies of fmri (direct & indirect) will constitute the "subgraph",
51492747Sbustos  * and will have the GV_INSUBGRAPH flag set.  The rest must be brought down,
51502747Sbustos  * which means the state is "disabled", "maintenance", or "uninitialized".  We
51512747Sbustos  * could consider "offline" to be down, and refrain from sending start
51522747Sbustos  * commands for such services, but that's not strictly necessary, so we'll
51532747Sbustos  * decline to intrude on the state machine.  It would probably confuse users
51542747Sbustos  * anyway.
51552747Sbustos  *
51562747Sbustos  * The services should be brought down in reverse-dependency order, so we
51572747Sbustos  * can't do it all at once here.  We initiate by override-disabling the leaves
51582747Sbustos  * of the dependency tree -- those services which are up but have no
51592747Sbustos  * dependents which are up.  When they come down,
51602747Sbustos  * vertex_subgraph_dependencies_shutdown() will override-disable the newly
51612747Sbustos  * exposed leaves.  Perseverance will ensure completion.
51622747Sbustos  *
51632747Sbustos  * Sometimes we need to take action when the transition is complete, like
51642747Sbustos  * start sulogin or halt the system.  To tell when we're done, we initialize
51652747Sbustos  * non_subgraph_svcs here to be the number of services which need to come
51662747Sbustos  * down.  As each does, we decrement the counter.  When it hits zero, we take
51672747Sbustos  * the appropriate action.  See vertex_subgraph_dependencies_shutdown().
51682747Sbustos  *
51692747Sbustos  * In case we're coming up, we also remove any enable-overrides for the
51702747Sbustos  * services which are dependencies of fmri.
51710Sstevel@tonic-gate  *
51720Sstevel@tonic-gate  * If norepository is true, the function will not change the repository.
51730Sstevel@tonic-gate  *
51741514Srm88369  * The decision to change the system run level in accordance with the milestone
51751514Srm88369  * is taken in dgraph_set_runlevel().
51761514Srm88369  *
51770Sstevel@tonic-gate  * Returns
51780Sstevel@tonic-gate  *   0 - success
51790Sstevel@tonic-gate  *   ECONNRESET - success, but handle was rebound
51800Sstevel@tonic-gate  *   EINVAL - fmri is invalid (error is logged)
51810Sstevel@tonic-gate  *   EALREADY - the milestone is already set to fmri
51820Sstevel@tonic-gate  *   ENOENT - a configured vertex does not exist for fmri (an error is logged)
51830Sstevel@tonic-gate  */
51840Sstevel@tonic-gate static int
51850Sstevel@tonic-gate dgraph_set_milestone(const char *fmri, scf_handle_t *h, boolean_t norepository)
51860Sstevel@tonic-gate {
51870Sstevel@tonic-gate 	const char *cfmri, *fs;
51880Sstevel@tonic-gate 	graph_vertex_t *nm, *v;
51890Sstevel@tonic-gate 	int ret = 0, r;
51900Sstevel@tonic-gate 	scf_instance_t *inst;
51910Sstevel@tonic-gate 	boolean_t isall, isnone, rebound = B_FALSE;
51920Sstevel@tonic-gate 
51930Sstevel@tonic-gate 	/* Validate fmri */
51940Sstevel@tonic-gate 	isall = (strcmp(fmri, "all") == 0);
51950Sstevel@tonic-gate 	isnone = (strcmp(fmri, "none") == 0);
51960Sstevel@tonic-gate 
51970Sstevel@tonic-gate 	if (!isall && !isnone) {
51980Sstevel@tonic-gate 		if (fmri_canonify(fmri, (char **)&cfmri, B_FALSE) == EINVAL)
51990Sstevel@tonic-gate 			goto reject;
52000Sstevel@tonic-gate 
52010Sstevel@tonic-gate 		if (strcmp(cfmri, single_user_fmri) != 0 &&
52020Sstevel@tonic-gate 		    strcmp(cfmri, multi_user_fmri) != 0 &&
52030Sstevel@tonic-gate 		    strcmp(cfmri, multi_user_svr_fmri) != 0) {
52040Sstevel@tonic-gate 			startd_free((void *)cfmri, max_scf_fmri_size);
52050Sstevel@tonic-gate reject:
52060Sstevel@tonic-gate 			log_framework(LOG_WARNING,
52070Sstevel@tonic-gate 			    "Rejecting request for invalid milestone \"%s\".\n",
52080Sstevel@tonic-gate 			    fmri);
52090Sstevel@tonic-gate 			return (EINVAL);
52100Sstevel@tonic-gate 		}
52110Sstevel@tonic-gate 	}
52120Sstevel@tonic-gate 
52130Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
52140Sstevel@tonic-gate 
52150Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
52160Sstevel@tonic-gate 
52170Sstevel@tonic-gate 	if (milestone == NULL) {
52180Sstevel@tonic-gate 		if (isall) {
52190Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
52200Sstevel@tonic-gate 			    "Milestone already set to all.\n");
52210Sstevel@tonic-gate 			ret = EALREADY;
52220Sstevel@tonic-gate 			goto out;
52230Sstevel@tonic-gate 		}
52240Sstevel@tonic-gate 	} else if (milestone == MILESTONE_NONE) {
52250Sstevel@tonic-gate 		if (isnone) {
52260Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
52270Sstevel@tonic-gate 			    "Milestone already set to none.\n");
52280Sstevel@tonic-gate 			ret = EALREADY;
52290Sstevel@tonic-gate 			goto out;
52300Sstevel@tonic-gate 		}
52310Sstevel@tonic-gate 	} else {
52320Sstevel@tonic-gate 		if (!isall && !isnone &&
52330Sstevel@tonic-gate 		    strcmp(cfmri, milestone->gv_name) == 0) {
52340Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
52350Sstevel@tonic-gate 			    "Milestone already set to %s.\n", cfmri);
52360Sstevel@tonic-gate 			ret = EALREADY;
52370Sstevel@tonic-gate 			goto out;
52380Sstevel@tonic-gate 		}
52390Sstevel@tonic-gate 	}
52400Sstevel@tonic-gate 
52410Sstevel@tonic-gate 	if (!isall && !isnone) {
52420Sstevel@tonic-gate 		nm = vertex_get_by_name(cfmri);
52430Sstevel@tonic-gate 		if (nm == NULL || !(nm->gv_flags & GV_CONFIGURED)) {
52440Sstevel@tonic-gate 			log_framework(LOG_WARNING, "Cannot set milestone to %s "
52450Sstevel@tonic-gate 			    "because no such service exists.\n", cfmri);
52460Sstevel@tonic-gate 			ret = ENOENT;
52470Sstevel@tonic-gate 			goto out;
52480Sstevel@tonic-gate 		}
52490Sstevel@tonic-gate 	}
52500Sstevel@tonic-gate 
52510Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Changing milestone to %s.\n", fmri);
52520Sstevel@tonic-gate 
52530Sstevel@tonic-gate 	/*
52540Sstevel@tonic-gate 	 * Set milestone, removing the old one if this was the last reference.
52550Sstevel@tonic-gate 	 */
52561712Srm88369 	if (milestone > MILESTONE_NONE)
52571712Srm88369 		(void) vertex_unref(milestone);
52580Sstevel@tonic-gate 
52590Sstevel@tonic-gate 	if (isall)
52600Sstevel@tonic-gate 		milestone = NULL;
52610Sstevel@tonic-gate 	else if (isnone)
52620Sstevel@tonic-gate 		milestone = MILESTONE_NONE;
52631712Srm88369 	else {
52640Sstevel@tonic-gate 		milestone = nm;
52651712Srm88369 		/* milestone should count as a reference */
52661712Srm88369 		vertex_ref(milestone);
52671712Srm88369 	}
52680Sstevel@tonic-gate 
52690Sstevel@tonic-gate 	/* Clear all GV_INSUBGRAPH bits. */
52700Sstevel@tonic-gate 	for (v = uu_list_first(dgraph); v != NULL; v = uu_list_next(dgraph, v))
52710Sstevel@tonic-gate 		v->gv_flags &= ~GV_INSUBGRAPH;
52720Sstevel@tonic-gate 
52730Sstevel@tonic-gate 	if (!isall && !isnone) {
52740Sstevel@tonic-gate 		/* Set GV_INSUBGRAPH for milestone & descendents. */
52750Sstevel@tonic-gate 		milestone->gv_flags |= GV_INSUBGRAPH;
52760Sstevel@tonic-gate 
52770Sstevel@tonic-gate 		r = uu_list_walk(milestone->gv_dependencies,
52780Sstevel@tonic-gate 		    (uu_walk_fn_t *)mark_subgraph, NULL, 0);
52790Sstevel@tonic-gate 		assert(r == 0);
52800Sstevel@tonic-gate 	}
52810Sstevel@tonic-gate 
52820Sstevel@tonic-gate 	/* Un-override services in the subgraph & override-disable the rest. */
52830Sstevel@tonic-gate 	if (norepository)
52840Sstevel@tonic-gate 		goto out;
52850Sstevel@tonic-gate 
52860Sstevel@tonic-gate 	non_subgraph_svcs = 0;
52870Sstevel@tonic-gate 	for (v = uu_list_first(dgraph);
52880Sstevel@tonic-gate 	    v != NULL;
52890Sstevel@tonic-gate 	    v = uu_list_next(dgraph, v)) {
52900Sstevel@tonic-gate 		if (v->gv_type != GVT_INST ||
52910Sstevel@tonic-gate 		    (v->gv_flags & GV_CONFIGURED) == 0)
52920Sstevel@tonic-gate 			continue;
52930Sstevel@tonic-gate 
52940Sstevel@tonic-gate again:
52950Sstevel@tonic-gate 		r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst,
52960Sstevel@tonic-gate 		    NULL, NULL, SCF_DECODE_FMRI_EXACT);
52970Sstevel@tonic-gate 		if (r != 0) {
52980Sstevel@tonic-gate 			switch (scf_error()) {
52990Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
53000Sstevel@tonic-gate 			default:
53010Sstevel@tonic-gate 				libscf_handle_rebind(h);
53020Sstevel@tonic-gate 				rebound = B_TRUE;
53030Sstevel@tonic-gate 				goto again;
53040Sstevel@tonic-gate 
53050Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
53060Sstevel@tonic-gate 				continue;
53070Sstevel@tonic-gate 
53080Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
53090Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
53100Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
53110Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
53120Sstevel@tonic-gate 				bad_error("scf_handle_decode_fmri",
53130Sstevel@tonic-gate 				    scf_error());
53140Sstevel@tonic-gate 			}
53150Sstevel@tonic-gate 		}
53160Sstevel@tonic-gate 
53170Sstevel@tonic-gate 		if (isall || (v->gv_flags & GV_INSUBGRAPH)) {
53180Sstevel@tonic-gate 			r = libscf_delete_enable_ovr(inst);
53190Sstevel@tonic-gate 			fs = "libscf_delete_enable_ovr";
53200Sstevel@tonic-gate 		} else {
53210Sstevel@tonic-gate 			assert(isnone || (v->gv_flags & GV_INSUBGRAPH) == 0);
53220Sstevel@tonic-gate 
53232747Sbustos 			/*
53242747Sbustos 			 * Services which are up need to come down before
53252747Sbustos 			 * we're done, but we can only disable the leaves
53262747Sbustos 			 * here.
53272747Sbustos 			 */
53282747Sbustos 
53292747Sbustos 			if (up_state(v->gv_state))
53300Sstevel@tonic-gate 				++non_subgraph_svcs;
53310Sstevel@tonic-gate 
53322747Sbustos 			/* If it's already disabled, don't bother. */
53332747Sbustos 			if ((v->gv_flags & GV_ENABLED) == 0)
53342747Sbustos 				continue;
53352747Sbustos 
53362747Sbustos 			if (!is_nonsubgraph_leaf(v))
53370Sstevel@tonic-gate 				continue;
53380Sstevel@tonic-gate 
53390Sstevel@tonic-gate 			r = libscf_set_enable_ovr(inst, 0);
53400Sstevel@tonic-gate 			fs = "libscf_set_enable_ovr";
53410Sstevel@tonic-gate 		}
53420Sstevel@tonic-gate 		switch (r) {
53430Sstevel@tonic-gate 		case 0:
53440Sstevel@tonic-gate 		case ECANCELED:
53450Sstevel@tonic-gate 			break;
53460Sstevel@tonic-gate 
53470Sstevel@tonic-gate 		case ECONNABORTED:
53480Sstevel@tonic-gate 			libscf_handle_rebind(h);
53490Sstevel@tonic-gate 			rebound = B_TRUE;
53500Sstevel@tonic-gate 			goto again;
53510Sstevel@tonic-gate 
53520Sstevel@tonic-gate 		case EPERM:
53530Sstevel@tonic-gate 		case EROFS:
53540Sstevel@tonic-gate 			log_error(LOG_WARNING,
53550Sstevel@tonic-gate 			    "Could not set %s/%s for %s: %s.\n",
53560Sstevel@tonic-gate 			    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
53570Sstevel@tonic-gate 			    v->gv_name, strerror(r));
53580Sstevel@tonic-gate 			break;
53590Sstevel@tonic-gate 
53600Sstevel@tonic-gate 		default:
53610Sstevel@tonic-gate 			bad_error(fs, r);
53620Sstevel@tonic-gate 		}
53630Sstevel@tonic-gate 	}
53640Sstevel@tonic-gate 
53650Sstevel@tonic-gate 	if (halting != -1) {
53660Sstevel@tonic-gate 		if (non_subgraph_svcs > 1)
53670Sstevel@tonic-gate 			uu_warn("%d system services are now being stopped.\n",
53680Sstevel@tonic-gate 			    non_subgraph_svcs);
53690Sstevel@tonic-gate 		else if (non_subgraph_svcs == 1)
53700Sstevel@tonic-gate 			uu_warn("One system service is now being stopped.\n");
53710Sstevel@tonic-gate 		else if (non_subgraph_svcs == 0)
53720Sstevel@tonic-gate 			do_uadmin();
53730Sstevel@tonic-gate 	}
53740Sstevel@tonic-gate 
53750Sstevel@tonic-gate 	ret = rebound ? ECONNRESET : 0;
53760Sstevel@tonic-gate 
53770Sstevel@tonic-gate out:
53780Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
53790Sstevel@tonic-gate 	if (!isall && !isnone)
53800Sstevel@tonic-gate 		startd_free((void *)cfmri, max_scf_fmri_size);
53810Sstevel@tonic-gate 	scf_instance_destroy(inst);
53820Sstevel@tonic-gate 	return (ret);
53830Sstevel@tonic-gate }
53840Sstevel@tonic-gate 
53850Sstevel@tonic-gate 
53860Sstevel@tonic-gate /*
53870Sstevel@tonic-gate  * Returns 0, ECONNABORTED, or EINVAL.
53880Sstevel@tonic-gate  */
53890Sstevel@tonic-gate static int
53900Sstevel@tonic-gate handle_graph_update_event(scf_handle_t *h, graph_protocol_event_t *e)
53910Sstevel@tonic-gate {
53920Sstevel@tonic-gate 	int r;
53930Sstevel@tonic-gate 
53940Sstevel@tonic-gate 	switch (e->gpe_type) {
53950Sstevel@tonic-gate 	case GRAPH_UPDATE_RELOAD_GRAPH:
53960Sstevel@tonic-gate 		log_error(LOG_WARNING,
53970Sstevel@tonic-gate 		    "graph_event: reload graph unimplemented\n");
53980Sstevel@tonic-gate 		break;
53990Sstevel@tonic-gate 
54000Sstevel@tonic-gate 	case GRAPH_UPDATE_STATE_CHANGE: {
54010Sstevel@tonic-gate 		protocol_states_t *states = e->gpe_data;
54020Sstevel@tonic-gate 
54030Sstevel@tonic-gate 		switch (r = dgraph_set_instance_state(h, e->gpe_inst,
54040Sstevel@tonic-gate 		    states->ps_state, states->ps_err)) {
54050Sstevel@tonic-gate 		case 0:
54060Sstevel@tonic-gate 		case ENOENT:
54070Sstevel@tonic-gate 			break;
54080Sstevel@tonic-gate 
54090Sstevel@tonic-gate 		case ECONNABORTED:
54100Sstevel@tonic-gate 			return (ECONNABORTED);
54110Sstevel@tonic-gate 
54120Sstevel@tonic-gate 		case EINVAL:
54130Sstevel@tonic-gate 		default:
54140Sstevel@tonic-gate #ifndef NDEBUG
54150Sstevel@tonic-gate 			(void) fprintf(stderr, "dgraph_set_instance_state() "
54160Sstevel@tonic-gate 			    "failed with unexpected error %d at %s:%d.\n", r,
54170Sstevel@tonic-gate 			    __FILE__, __LINE__);
54180Sstevel@tonic-gate #endif
54190Sstevel@tonic-gate 			abort();
54200Sstevel@tonic-gate 		}
54210Sstevel@tonic-gate 
54220Sstevel@tonic-gate 		startd_free(states, sizeof (protocol_states_t));
54230Sstevel@tonic-gate 		break;
54240Sstevel@tonic-gate 	}
54250Sstevel@tonic-gate 
54260Sstevel@tonic-gate 	default:
54270Sstevel@tonic-gate 		log_error(LOG_WARNING,
54280Sstevel@tonic-gate 		    "graph_event_loop received an unknown event: %d\n",
54290Sstevel@tonic-gate 		    e->gpe_type);
54300Sstevel@tonic-gate 		break;
54310Sstevel@tonic-gate 	}
54320Sstevel@tonic-gate 
54330Sstevel@tonic-gate 	return (0);
54340Sstevel@tonic-gate }
54350Sstevel@tonic-gate 
54360Sstevel@tonic-gate /*
54370Sstevel@tonic-gate  * graph_event_thread()
54380Sstevel@tonic-gate  *    Wait for state changes from the restarters.
54390Sstevel@tonic-gate  */
54400Sstevel@tonic-gate /*ARGSUSED*/
54410Sstevel@tonic-gate void *
54420Sstevel@tonic-gate graph_event_thread(void *unused)
54430Sstevel@tonic-gate {
54440Sstevel@tonic-gate 	scf_handle_t *h;
54450Sstevel@tonic-gate 	int err;
54460Sstevel@tonic-gate 
54470Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
54480Sstevel@tonic-gate 
54490Sstevel@tonic-gate 	/*CONSTCOND*/
54500Sstevel@tonic-gate 	while (1) {
54510Sstevel@tonic-gate 		graph_protocol_event_t *e;
54520Sstevel@tonic-gate 
54530Sstevel@tonic-gate 		MUTEX_LOCK(&gu->gu_lock);
54540Sstevel@tonic-gate 
54550Sstevel@tonic-gate 		while (gu->gu_wakeup == 0)
54560Sstevel@tonic-gate 			(void) pthread_cond_wait(&gu->gu_cv, &gu->gu_lock);
54570Sstevel@tonic-gate 
54580Sstevel@tonic-gate 		gu->gu_wakeup = 0;
54590Sstevel@tonic-gate 
54600Sstevel@tonic-gate 		while ((e = graph_event_dequeue()) != NULL) {
54610Sstevel@tonic-gate 			MUTEX_LOCK(&e->gpe_lock);
54620Sstevel@tonic-gate 			MUTEX_UNLOCK(&gu->gu_lock);
54630Sstevel@tonic-gate 
54640Sstevel@tonic-gate 			while ((err = handle_graph_update_event(h, e)) ==
54650Sstevel@tonic-gate 			    ECONNABORTED)
54660Sstevel@tonic-gate 				libscf_handle_rebind(h);
54670Sstevel@tonic-gate 
54680Sstevel@tonic-gate 			if (err == 0)
54690Sstevel@tonic-gate 				graph_event_release(e);
54700Sstevel@tonic-gate 			else
54710Sstevel@tonic-gate 				graph_event_requeue(e);
54720Sstevel@tonic-gate 
54730Sstevel@tonic-gate 			MUTEX_LOCK(&gu->gu_lock);
54740Sstevel@tonic-gate 		}
54750Sstevel@tonic-gate 
54760Sstevel@tonic-gate 		MUTEX_UNLOCK(&gu->gu_lock);
54770Sstevel@tonic-gate 	}
54780Sstevel@tonic-gate 
54790Sstevel@tonic-gate 	/*
54800Sstevel@tonic-gate 	 * Unreachable for now -- there's currently no graceful cleanup
54810Sstevel@tonic-gate 	 * called on exit().
54820Sstevel@tonic-gate 	 */
54830Sstevel@tonic-gate 	MUTEX_UNLOCK(&gu->gu_lock);
54840Sstevel@tonic-gate 	scf_handle_destroy(h);
54850Sstevel@tonic-gate 	return (NULL);
54860Sstevel@tonic-gate }
54870Sstevel@tonic-gate 
54880Sstevel@tonic-gate static void
54890Sstevel@tonic-gate set_initial_milestone(scf_handle_t *h)
54900Sstevel@tonic-gate {
54910Sstevel@tonic-gate 	scf_instance_t *inst;
54920Sstevel@tonic-gate 	char *fmri, *cfmri;
54930Sstevel@tonic-gate 	size_t sz;
54940Sstevel@tonic-gate 	int r;
54950Sstevel@tonic-gate 
54960Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
54970Sstevel@tonic-gate 	fmri = startd_alloc(max_scf_fmri_size);
54980Sstevel@tonic-gate 
54990Sstevel@tonic-gate 	/*
55000Sstevel@tonic-gate 	 * If -m milestone= was specified, we want to set options_ovr/milestone
55010Sstevel@tonic-gate 	 * to it.  Otherwise we want to read what the milestone should be set
55020Sstevel@tonic-gate 	 * to.  Either way we need our inst.
55030Sstevel@tonic-gate 	 */
55040Sstevel@tonic-gate get_self:
55050Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
55060Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
55070Sstevel@tonic-gate 		switch (scf_error()) {
55080Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
55090Sstevel@tonic-gate 			libscf_handle_rebind(h);
55100Sstevel@tonic-gate 			goto get_self;
55110Sstevel@tonic-gate 
55120Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
55130Sstevel@tonic-gate 			if (st->st_subgraph != NULL &&
55140Sstevel@tonic-gate 			    st->st_subgraph[0] != '\0') {
55150Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
55160Sstevel@tonic-gate 				    max_scf_fmri_size);
55170Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
55180Sstevel@tonic-gate 			} else {
55190Sstevel@tonic-gate 				fmri[0] = '\0';
55200Sstevel@tonic-gate 			}
55210Sstevel@tonic-gate 			break;
55220Sstevel@tonic-gate 
55230Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
55240Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
55250Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
55260Sstevel@tonic-gate 		default:
55270Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
55280Sstevel@tonic-gate 		}
55290Sstevel@tonic-gate 	} else {
55300Sstevel@tonic-gate 		if (st->st_subgraph != NULL && st->st_subgraph[0] != '\0') {
55310Sstevel@tonic-gate 			scf_propertygroup_t *pg;
55320Sstevel@tonic-gate 
55330Sstevel@tonic-gate 			pg = safe_scf_pg_create(h);
55340Sstevel@tonic-gate 
55350Sstevel@tonic-gate 			sz = strlcpy(fmri, st->st_subgraph, max_scf_fmri_size);
55360Sstevel@tonic-gate 			assert(sz < max_scf_fmri_size);
55370Sstevel@tonic-gate 
55380Sstevel@tonic-gate 			r = libscf_inst_get_or_add_pg(inst, SCF_PG_OPTIONS_OVR,
55390Sstevel@tonic-gate 			    SCF_PG_OPTIONS_OVR_TYPE, SCF_PG_OPTIONS_OVR_FLAGS,
55400Sstevel@tonic-gate 			    pg);
55410Sstevel@tonic-gate 			switch (r) {
55420Sstevel@tonic-gate 			case 0:
55430Sstevel@tonic-gate 				break;
55440Sstevel@tonic-gate 
55450Sstevel@tonic-gate 			case ECONNABORTED:
55460Sstevel@tonic-gate 				libscf_handle_rebind(h);
55470Sstevel@tonic-gate 				goto get_self;
55480Sstevel@tonic-gate 
55490Sstevel@tonic-gate 			case EPERM:
55500Sstevel@tonic-gate 			case EACCES:
55510Sstevel@tonic-gate 			case EROFS:
55520Sstevel@tonic-gate 				log_error(LOG_WARNING, "Could not set %s/%s: "
55530Sstevel@tonic-gate 				    "%s.\n", SCF_PG_OPTIONS_OVR,
55540Sstevel@tonic-gate 				    SCF_PROPERTY_MILESTONE, strerror(r));
55550Sstevel@tonic-gate 				/* FALLTHROUGH */
55560Sstevel@tonic-gate 
55570Sstevel@tonic-gate 			case ECANCELED:
55580Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
55590Sstevel@tonic-gate 				    max_scf_fmri_size);
55600Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
55610Sstevel@tonic-gate 				break;
55620Sstevel@tonic-gate 
55630Sstevel@tonic-gate 			default:
55640Sstevel@tonic-gate 				bad_error("libscf_inst_get_or_add_pg", r);
55650Sstevel@tonic-gate 			}
55660Sstevel@tonic-gate 
55670Sstevel@tonic-gate 			r = libscf_clear_runlevel(pg, fmri);
55680Sstevel@tonic-gate 			switch (r) {
55690Sstevel@tonic-gate 			case 0:
55700Sstevel@tonic-gate 				break;
55710Sstevel@tonic-gate 
55720Sstevel@tonic-gate 			case ECONNABORTED:
55730Sstevel@tonic-gate 				libscf_handle_rebind(h);
55740Sstevel@tonic-gate 				goto get_self;
55750Sstevel@tonic-gate 
55760Sstevel@tonic-gate 			case EPERM:
55770Sstevel@tonic-gate 			case EACCES:
55780Sstevel@tonic-gate 			case EROFS:
55790Sstevel@tonic-gate 				log_error(LOG_WARNING, "Could not set %s/%s: "
55800Sstevel@tonic-gate 				    "%s.\n", SCF_PG_OPTIONS_OVR,
55810Sstevel@tonic-gate 				    SCF_PROPERTY_MILESTONE, strerror(r));
55820Sstevel@tonic-gate 				/* FALLTHROUGH */
55830Sstevel@tonic-gate 
55840Sstevel@tonic-gate 			case ECANCELED:
55850Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
55860Sstevel@tonic-gate 				    max_scf_fmri_size);
55870Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
55880Sstevel@tonic-gate 				break;
55890Sstevel@tonic-gate 
55900Sstevel@tonic-gate 			default:
55910Sstevel@tonic-gate 				bad_error("libscf_clear_runlevel", r);
55920Sstevel@tonic-gate 			}
55930Sstevel@tonic-gate 
55940Sstevel@tonic-gate 			scf_pg_destroy(pg);
55950Sstevel@tonic-gate 		} else {
55960Sstevel@tonic-gate 			scf_property_t *prop;
55970Sstevel@tonic-gate 			scf_value_t *val;
55980Sstevel@tonic-gate 
55990Sstevel@tonic-gate 			prop = safe_scf_property_create(h);
56000Sstevel@tonic-gate 			val = safe_scf_value_create(h);
56010Sstevel@tonic-gate 
56020Sstevel@tonic-gate 			r = libscf_get_milestone(inst, prop, val, fmri,
56030Sstevel@tonic-gate 			    max_scf_fmri_size);
56040Sstevel@tonic-gate 			switch (r) {
56050Sstevel@tonic-gate 			case 0:
56060Sstevel@tonic-gate 				break;
56070Sstevel@tonic-gate 
56080Sstevel@tonic-gate 			case ECONNABORTED:
56090Sstevel@tonic-gate 				libscf_handle_rebind(h);
56100Sstevel@tonic-gate 				goto get_self;
56110Sstevel@tonic-gate 
56120Sstevel@tonic-gate 			case EINVAL:
56130Sstevel@tonic-gate 				log_error(LOG_WARNING, "Milestone property is "
56140Sstevel@tonic-gate 				    "misconfigured.  Defaulting to \"all\".\n");
56150Sstevel@tonic-gate 				/* FALLTHROUGH */
56160Sstevel@tonic-gate 
56170Sstevel@tonic-gate 			case ECANCELED:
56180Sstevel@tonic-gate 			case ENOENT:
56190Sstevel@tonic-gate 				fmri[0] = '\0';
56200Sstevel@tonic-gate 				break;
56210Sstevel@tonic-gate 
56220Sstevel@tonic-gate 			default:
56230Sstevel@tonic-gate 				bad_error("libscf_get_milestone", r);
56240Sstevel@tonic-gate 			}
56250Sstevel@tonic-gate 
56260Sstevel@tonic-gate 			scf_value_destroy(val);
56270Sstevel@tonic-gate 			scf_property_destroy(prop);
56280Sstevel@tonic-gate 		}
56290Sstevel@tonic-gate 	}
56300Sstevel@tonic-gate 
56310Sstevel@tonic-gate 	if (fmri[0] == '\0' || strcmp(fmri, "all") == 0)
56320Sstevel@tonic-gate 		goto out;
56330Sstevel@tonic-gate 
56340Sstevel@tonic-gate 	if (strcmp(fmri, "none") != 0) {
56350Sstevel@tonic-gate retry:
56360Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
56370Sstevel@tonic-gate 		    NULL, SCF_DECODE_FMRI_EXACT) != 0) {
56380Sstevel@tonic-gate 			switch (scf_error()) {
56390Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
56400Sstevel@tonic-gate 				log_error(LOG_WARNING,
56410Sstevel@tonic-gate 				    "Requested milestone \"%s\" is invalid.  "
56420Sstevel@tonic-gate 				    "Reverting to \"all\".\n", fmri);
56430Sstevel@tonic-gate 				goto out;
56440Sstevel@tonic-gate 
56450Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
56460Sstevel@tonic-gate 				log_error(LOG_WARNING, "Requested milestone "
56470Sstevel@tonic-gate 				    "\"%s\" does not specify an instance.  "
56480Sstevel@tonic-gate 				    "Reverting to \"all\".\n", fmri);
56490Sstevel@tonic-gate 				goto out;
56500Sstevel@tonic-gate 
56510Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
56520Sstevel@tonic-gate 				libscf_handle_rebind(h);
56530Sstevel@tonic-gate 				goto retry;
56540Sstevel@tonic-gate 
56550Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
56560Sstevel@tonic-gate 				log_error(LOG_WARNING, "Requested milestone "
56570Sstevel@tonic-gate 				    "\"%s\" not in repository.  Reverting to "
56580Sstevel@tonic-gate 				    "\"all\".\n", fmri);
56590Sstevel@tonic-gate 				goto out;
56600Sstevel@tonic-gate 
56610Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
56620Sstevel@tonic-gate 			default:
56630Sstevel@tonic-gate 				bad_error("scf_handle_decode_fmri",
56640Sstevel@tonic-gate 				    scf_error());
56650Sstevel@tonic-gate 			}
56660Sstevel@tonic-gate 		}
56670Sstevel@tonic-gate 
56680Sstevel@tonic-gate 		r = fmri_canonify(fmri, &cfmri, B_FALSE);
56690Sstevel@tonic-gate 		assert(r == 0);
56700Sstevel@tonic-gate 
56710Sstevel@tonic-gate 		r = dgraph_add_instance(cfmri, inst, B_TRUE);
56720Sstevel@tonic-gate 		startd_free(cfmri, max_scf_fmri_size);
56730Sstevel@tonic-gate 		switch (r) {
56740Sstevel@tonic-gate 		case 0:
56750Sstevel@tonic-gate 			break;
56760Sstevel@tonic-gate 
56770Sstevel@tonic-gate 		case ECONNABORTED:
56780Sstevel@tonic-gate 			goto retry;
56790Sstevel@tonic-gate 
56800Sstevel@tonic-gate 		case EINVAL:
56810Sstevel@tonic-gate 			log_error(LOG_WARNING,
56820Sstevel@tonic-gate 			    "Requested milestone \"%s\" is invalid.  "
56830Sstevel@tonic-gate 			    "Reverting to \"all\".\n", fmri);
56840Sstevel@tonic-gate 			goto out;
56850Sstevel@tonic-gate 
56860Sstevel@tonic-gate 		case ECANCELED:
56870Sstevel@tonic-gate 			log_error(LOG_WARNING,
56880Sstevel@tonic-gate 			    "Requested milestone \"%s\" not "
56890Sstevel@tonic-gate 			    "in repository.  Reverting to \"all\".\n",
56900Sstevel@tonic-gate 			    fmri);
56910Sstevel@tonic-gate 			goto out;
56920Sstevel@tonic-gate 
56930Sstevel@tonic-gate 		case EEXIST:
56940Sstevel@tonic-gate 		default:
56950Sstevel@tonic-gate 			bad_error("dgraph_add_instance", r);
56960Sstevel@tonic-gate 		}
56970Sstevel@tonic-gate 	}
56980Sstevel@tonic-gate 
56990Sstevel@tonic-gate 	log_console(LOG_INFO, "Booting to milestone \"%s\".\n", fmri);
57000Sstevel@tonic-gate 
57010Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_FALSE);
57020Sstevel@tonic-gate 	switch (r) {
57030Sstevel@tonic-gate 	case 0:
57040Sstevel@tonic-gate 	case ECONNRESET:
57050Sstevel@tonic-gate 	case EALREADY:
57060Sstevel@tonic-gate 		break;
57070Sstevel@tonic-gate 
57080Sstevel@tonic-gate 	case EINVAL:
57090Sstevel@tonic-gate 	case ENOENT:
57100Sstevel@tonic-gate 	default:
57110Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
57120Sstevel@tonic-gate 	}
57130Sstevel@tonic-gate 
57140Sstevel@tonic-gate out:
57150Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
57160Sstevel@tonic-gate 	scf_instance_destroy(inst);
57170Sstevel@tonic-gate }
57180Sstevel@tonic-gate 
57190Sstevel@tonic-gate void
57200Sstevel@tonic-gate set_restart_milestone(scf_handle_t *h)
57210Sstevel@tonic-gate {
57220Sstevel@tonic-gate 	scf_instance_t *inst;
57230Sstevel@tonic-gate 	scf_property_t *prop;
57240Sstevel@tonic-gate 	scf_value_t *val;
57250Sstevel@tonic-gate 	char *fmri;
57260Sstevel@tonic-gate 	int r;
57270Sstevel@tonic-gate 
57280Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
57290Sstevel@tonic-gate 
57300Sstevel@tonic-gate get_self:
57310Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL,
57320Sstevel@tonic-gate 	    inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
57330Sstevel@tonic-gate 		switch (scf_error()) {
57340Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
57350Sstevel@tonic-gate 			libscf_handle_rebind(h);
57360Sstevel@tonic-gate 			goto get_self;
57370Sstevel@tonic-gate 
57380Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
57390Sstevel@tonic-gate 			break;
57400Sstevel@tonic-gate 
57410Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
57420Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
57430Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
57440Sstevel@tonic-gate 		default:
57450Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
57460Sstevel@tonic-gate 		}
57470Sstevel@tonic-gate 
57480Sstevel@tonic-gate 		scf_instance_destroy(inst);
57490Sstevel@tonic-gate 		return;
57500Sstevel@tonic-gate 	}
57510Sstevel@tonic-gate 
57520Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
57530Sstevel@tonic-gate 	val = safe_scf_value_create(h);
57540Sstevel@tonic-gate 	fmri = startd_alloc(max_scf_fmri_size);
57550Sstevel@tonic-gate 
57560Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size);
57570Sstevel@tonic-gate 	switch (r) {
57580Sstevel@tonic-gate 	case 0:
57590Sstevel@tonic-gate 		break;
57600Sstevel@tonic-gate 
57610Sstevel@tonic-gate 	case ECONNABORTED:
57620Sstevel@tonic-gate 		libscf_handle_rebind(h);
57630Sstevel@tonic-gate 		goto get_self;
57640Sstevel@tonic-gate 
57650Sstevel@tonic-gate 	case ECANCELED:
57660Sstevel@tonic-gate 	case ENOENT:
57670Sstevel@tonic-gate 	case EINVAL:
57680Sstevel@tonic-gate 		goto out;
57690Sstevel@tonic-gate 
57700Sstevel@tonic-gate 	default:
57710Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
57720Sstevel@tonic-gate 	}
57730Sstevel@tonic-gate 
57740Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_TRUE);
57750Sstevel@tonic-gate 	switch (r) {
57760Sstevel@tonic-gate 	case 0:
57770Sstevel@tonic-gate 	case ECONNRESET:
57780Sstevel@tonic-gate 	case EALREADY:
57790Sstevel@tonic-gate 	case EINVAL:
57800Sstevel@tonic-gate 	case ENOENT:
57810Sstevel@tonic-gate 		break;
57820Sstevel@tonic-gate 
57830Sstevel@tonic-gate 	default:
57840Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
57850Sstevel@tonic-gate 	}
57860Sstevel@tonic-gate 
57870Sstevel@tonic-gate out:
57880Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
57890Sstevel@tonic-gate 	scf_value_destroy(val);
57900Sstevel@tonic-gate 	scf_property_destroy(prop);
57910Sstevel@tonic-gate 	scf_instance_destroy(inst);
57920Sstevel@tonic-gate }
57930Sstevel@tonic-gate 
57940Sstevel@tonic-gate /*
57950Sstevel@tonic-gate  * void *graph_thread(void *)
57960Sstevel@tonic-gate  *
57970Sstevel@tonic-gate  * Graph management thread.
57980Sstevel@tonic-gate  */
57990Sstevel@tonic-gate /*ARGSUSED*/
58000Sstevel@tonic-gate void *
58010Sstevel@tonic-gate graph_thread(void *arg)
58020Sstevel@tonic-gate {
58030Sstevel@tonic-gate 	scf_handle_t *h;
58040Sstevel@tonic-gate 	int err;
58050Sstevel@tonic-gate 
58060Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
58070Sstevel@tonic-gate 
58080Sstevel@tonic-gate 	if (st->st_initial)
58090Sstevel@tonic-gate 		set_initial_milestone(h);
58100Sstevel@tonic-gate 
58110Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
58120Sstevel@tonic-gate 	initial_milestone_set = B_TRUE;
58130Sstevel@tonic-gate 	err = pthread_cond_broadcast(&initial_milestone_cv);
58140Sstevel@tonic-gate 	assert(err == 0);
58150Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
58160Sstevel@tonic-gate 
58170Sstevel@tonic-gate 	libscf_populate_graph(h);
58180Sstevel@tonic-gate 
58190Sstevel@tonic-gate 	if (!st->st_initial)
58200Sstevel@tonic-gate 		set_restart_milestone(h);
58210Sstevel@tonic-gate 
58220Sstevel@tonic-gate 	MUTEX_LOCK(&st->st_load_lock);
58230Sstevel@tonic-gate 	st->st_load_complete = 1;
58240Sstevel@tonic-gate 	(void) pthread_cond_broadcast(&st->st_load_cv);
58250Sstevel@tonic-gate 	MUTEX_UNLOCK(&st->st_load_lock);
58260Sstevel@tonic-gate 
58270Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
58280Sstevel@tonic-gate 	/*
58290Sstevel@tonic-gate 	 * Now that we've set st_load_complete we need to check can_come_up()
58300Sstevel@tonic-gate 	 * since if we booted to a milestone, then there won't be any more
58310Sstevel@tonic-gate 	 * state updates.
58320Sstevel@tonic-gate 	 */
58330Sstevel@tonic-gate 	if (!go_single_user_mode && !go_to_level1 &&
58340Sstevel@tonic-gate 	    halting == -1) {
58353639Srm88369 		if (!sulogin_thread_running && !can_come_up()) {
58360Sstevel@tonic-gate 			(void) startd_thread_create(sulogin_thread, NULL);
58370Sstevel@tonic-gate 			sulogin_thread_running = B_TRUE;
58380Sstevel@tonic-gate 		}
58390Sstevel@tonic-gate 	}
58400Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
58410Sstevel@tonic-gate 
58420Sstevel@tonic-gate 	(void) pthread_mutex_lock(&gu->gu_freeze_lock);
58430Sstevel@tonic-gate 
58440Sstevel@tonic-gate 	/*CONSTCOND*/
58450Sstevel@tonic-gate 	while (1) {
58460Sstevel@tonic-gate 		(void) pthread_cond_wait(&gu->gu_freeze_cv,
58470Sstevel@tonic-gate 		    &gu->gu_freeze_lock);
58480Sstevel@tonic-gate 	}
58490Sstevel@tonic-gate 
58500Sstevel@tonic-gate 	/*
58510Sstevel@tonic-gate 	 * Unreachable for now -- there's currently no graceful cleanup
58520Sstevel@tonic-gate 	 * called on exit().
58530Sstevel@tonic-gate 	 */
58540Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&gu->gu_freeze_lock);
58550Sstevel@tonic-gate 	scf_handle_destroy(h);
58560Sstevel@tonic-gate 
58570Sstevel@tonic-gate 	return (NULL);
58580Sstevel@tonic-gate }
58590Sstevel@tonic-gate 
58600Sstevel@tonic-gate 
58610Sstevel@tonic-gate /*
58620Sstevel@tonic-gate  * int next_action()
58630Sstevel@tonic-gate  *   Given an array of timestamps 'a' with 'num' elements, find the
58640Sstevel@tonic-gate  *   lowest non-zero timestamp and return its index. If there are no
58650Sstevel@tonic-gate  *   non-zero elements, return -1.
58660Sstevel@tonic-gate  */
58670Sstevel@tonic-gate static int
58680Sstevel@tonic-gate next_action(hrtime_t *a, int num)
58690Sstevel@tonic-gate {
58700Sstevel@tonic-gate 	hrtime_t t = 0;
58710Sstevel@tonic-gate 	int i = 0, smallest = -1;
58720Sstevel@tonic-gate 
58730Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
58740Sstevel@tonic-gate 		if (t == 0) {
58750Sstevel@tonic-gate 			t = a[i];
58760Sstevel@tonic-gate 			smallest = i;
58770Sstevel@tonic-gate 		} else if (a[i] != 0 && a[i] < t) {
58780Sstevel@tonic-gate 			t = a[i];
58790Sstevel@tonic-gate 			smallest = i;
58800Sstevel@tonic-gate 		}
58810Sstevel@tonic-gate 	}
58820Sstevel@tonic-gate 
58830Sstevel@tonic-gate 	if (t == 0)
58840Sstevel@tonic-gate 		return (-1);
58850Sstevel@tonic-gate 	else
58860Sstevel@tonic-gate 		return (smallest);
58870Sstevel@tonic-gate }
58880Sstevel@tonic-gate 
58890Sstevel@tonic-gate /*
58900Sstevel@tonic-gate  * void process_actions()
58910Sstevel@tonic-gate  *   Process actions requested by the administrator. Possibilities include:
58920Sstevel@tonic-gate  *   refresh, restart, maintenance mode off, maintenance mode on,
58930Sstevel@tonic-gate  *   maintenance mode immediate, and degraded.
58940Sstevel@tonic-gate  *
58950Sstevel@tonic-gate  *   The set of pending actions is represented in the repository as a
58960Sstevel@tonic-gate  *   per-instance property group, with each action being a single property
58970Sstevel@tonic-gate  *   in that group.  This property group is converted to an array, with each
58980Sstevel@tonic-gate  *   action type having an array slot.  The actions in the array at the
58990Sstevel@tonic-gate  *   time process_actions() is called are acted on in the order of the
59000Sstevel@tonic-gate  *   timestamp (which is the value stored in the slot).  A value of zero
59010Sstevel@tonic-gate  *   indicates that there is no pending action of the type associated with
59020Sstevel@tonic-gate  *   a particular slot.
59030Sstevel@tonic-gate  *
59040Sstevel@tonic-gate  *   Sending an action event multiple times before the restarter has a
59050Sstevel@tonic-gate  *   chance to process that action will force it to be run at the last
59060Sstevel@tonic-gate  *   timestamp where it appears in the ordering.
59070Sstevel@tonic-gate  *
59080Sstevel@tonic-gate  *   Turning maintenance mode on trumps all other actions.
59090Sstevel@tonic-gate  *
59100Sstevel@tonic-gate  *   Returns 0 or ECONNABORTED.
59110Sstevel@tonic-gate  */
59120Sstevel@tonic-gate static int
59130Sstevel@tonic-gate process_actions(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst)
59140Sstevel@tonic-gate {
59150Sstevel@tonic-gate 	scf_property_t *prop = NULL;
59160Sstevel@tonic-gate 	scf_value_t *val = NULL;
59170Sstevel@tonic-gate 	scf_type_t type;
59180Sstevel@tonic-gate 	graph_vertex_t *vertex;
59190Sstevel@tonic-gate 	admin_action_t a;
59200Sstevel@tonic-gate 	int i, ret = 0, r;
59210Sstevel@tonic-gate 	hrtime_t action_ts[NACTIONS];
59220Sstevel@tonic-gate 	char *inst_name;
59230Sstevel@tonic-gate 
59240Sstevel@tonic-gate 	r = libscf_instance_get_fmri(inst, &inst_name);
59250Sstevel@tonic-gate 	switch (r) {
59260Sstevel@tonic-gate 	case 0:
59270Sstevel@tonic-gate 		break;
59280Sstevel@tonic-gate 
59290Sstevel@tonic-gate 	case ECONNABORTED:
59300Sstevel@tonic-gate 		return (ECONNABORTED);
59310Sstevel@tonic-gate 
59320Sstevel@tonic-gate 	case ECANCELED:
59330Sstevel@tonic-gate 		return (0);
59340Sstevel@tonic-gate 
59350Sstevel@tonic-gate 	default:
59360Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", r);
59370Sstevel@tonic-gate 	}
59380Sstevel@tonic-gate 
59390Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
59400Sstevel@tonic-gate 
59410Sstevel@tonic-gate 	vertex = vertex_get_by_name(inst_name);
59420Sstevel@tonic-gate 	if (vertex == NULL) {
59430Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
59442682Srm88369 		startd_free(inst_name, max_scf_fmri_size);
59450Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "%s: Can't find graph vertex. "
59460Sstevel@tonic-gate 		    "The instance must have been removed.\n", inst_name);
59470Sstevel@tonic-gate 		return (0);
59480Sstevel@tonic-gate 	}
59490Sstevel@tonic-gate 
59500Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
59510Sstevel@tonic-gate 	val = safe_scf_value_create(h);
59520Sstevel@tonic-gate 
59530Sstevel@tonic-gate 	for (i = 0; i < NACTIONS; i++) {
59540Sstevel@tonic-gate 		if (scf_pg_get_property(pg, admin_actions[i], prop) != 0) {
59550Sstevel@tonic-gate 			switch (scf_error()) {
59560Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
59570Sstevel@tonic-gate 			default:
59580Sstevel@tonic-gate 				ret = ECONNABORTED;
59590Sstevel@tonic-gate 				goto out;
59600Sstevel@tonic-gate 
59610Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
59620Sstevel@tonic-gate 				goto out;
59630Sstevel@tonic-gate 
59640Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
59650Sstevel@tonic-gate 				action_ts[i] = 0;
59660Sstevel@tonic-gate 				continue;
59670Sstevel@tonic-gate 
59680Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
59690Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
59700Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
59710Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
59720Sstevel@tonic-gate 			}
59730Sstevel@tonic-gate 		}
59740Sstevel@tonic-gate 
59750Sstevel@tonic-gate 		if (scf_property_type(prop, &type) != 0) {
59760Sstevel@tonic-gate 			switch (scf_error()) {
59770Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
59780Sstevel@tonic-gate 			default:
59790Sstevel@tonic-gate 				ret = ECONNABORTED;
59800Sstevel@tonic-gate 				goto out;
59810Sstevel@tonic-gate 
59820Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
59830Sstevel@tonic-gate 				action_ts[i] = 0;
59840Sstevel@tonic-gate 				continue;
59850Sstevel@tonic-gate 
59860Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
59870Sstevel@tonic-gate 				bad_error("scf_property_type", scf_error());
59880Sstevel@tonic-gate 			}
59890Sstevel@tonic-gate 		}
59900Sstevel@tonic-gate 
59910Sstevel@tonic-gate 		if (type != SCF_TYPE_INTEGER) {
59920Sstevel@tonic-gate 			action_ts[i] = 0;
59930Sstevel@tonic-gate 			continue;
59940Sstevel@tonic-gate 		}
59950Sstevel@tonic-gate 
59960Sstevel@tonic-gate 		if (scf_property_get_value(prop, val) != 0) {
59970Sstevel@tonic-gate 			switch (scf_error()) {
59980Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
59990Sstevel@tonic-gate 			default:
60000Sstevel@tonic-gate 				ret = ECONNABORTED;
60010Sstevel@tonic-gate 				goto out;
60020Sstevel@tonic-gate 
60030Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
60040Sstevel@tonic-gate 				goto out;
60050Sstevel@tonic-gate 
60060Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
60070Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
60080Sstevel@tonic-gate 				action_ts[i] = 0;
60090Sstevel@tonic-gate 				continue;
60100Sstevel@tonic-gate 
60110Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
60125040Swesolows 			case SCF_ERROR_PERMISSION_DENIED:
60130Sstevel@tonic-gate 				bad_error("scf_property_get_value",
60140Sstevel@tonic-gate 				    scf_error());
60150Sstevel@tonic-gate 			}
60160Sstevel@tonic-gate 		}
60170Sstevel@tonic-gate 
60180Sstevel@tonic-gate 		r = scf_value_get_integer(val, &action_ts[i]);
60190Sstevel@tonic-gate 		assert(r == 0);
60200Sstevel@tonic-gate 	}
60210Sstevel@tonic-gate 
60220Sstevel@tonic-gate 	a = ADMIN_EVENT_MAINT_ON_IMMEDIATE;
60230Sstevel@tonic-gate 	if (action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ||
60240Sstevel@tonic-gate 	    action_ts[ADMIN_EVENT_MAINT_ON]) {
60250Sstevel@tonic-gate 		a = action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ?
60260Sstevel@tonic-gate 		    ADMIN_EVENT_MAINT_ON_IMMEDIATE : ADMIN_EVENT_MAINT_ON;
60270Sstevel@tonic-gate 
60280Sstevel@tonic-gate 		vertex_send_event(vertex, admin_events[a]);
60290Sstevel@tonic-gate 		r = libscf_unset_action(h, pg, a, action_ts[a]);
60300Sstevel@tonic-gate 		switch (r) {
60310Sstevel@tonic-gate 		case 0:
60320Sstevel@tonic-gate 		case EACCES:
60330Sstevel@tonic-gate 			break;
60340Sstevel@tonic-gate 
60350Sstevel@tonic-gate 		case ECONNABORTED:
60360Sstevel@tonic-gate 			ret = ECONNABORTED;
60370Sstevel@tonic-gate 			goto out;
60380Sstevel@tonic-gate 
60390Sstevel@tonic-gate 		case EPERM:
60400Sstevel@tonic-gate 			uu_die("Insufficient privilege.\n");
60410Sstevel@tonic-gate 			/* NOTREACHED */
60420Sstevel@tonic-gate 
60430Sstevel@tonic-gate 		default:
60440Sstevel@tonic-gate 			bad_error("libscf_unset_action", r);
60450Sstevel@tonic-gate 		}
60460Sstevel@tonic-gate 	}
60470Sstevel@tonic-gate 
60480Sstevel@tonic-gate 	while ((a = next_action(action_ts, NACTIONS)) != -1) {
60490Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
60500Sstevel@tonic-gate 		    "Graph: processing %s action for %s.\n", admin_actions[a],
60510Sstevel@tonic-gate 		    inst_name);
60520Sstevel@tonic-gate 
60530Sstevel@tonic-gate 		if (a == ADMIN_EVENT_REFRESH) {
60540Sstevel@tonic-gate 			r = dgraph_refresh_instance(vertex, inst);
60550Sstevel@tonic-gate 			switch (r) {
60560Sstevel@tonic-gate 			case 0:
60570Sstevel@tonic-gate 			case ECANCELED:
60580Sstevel@tonic-gate 			case EINVAL:
60590Sstevel@tonic-gate 			case -1:
60600Sstevel@tonic-gate 				break;
60610Sstevel@tonic-gate 
60620Sstevel@tonic-gate 			case ECONNABORTED:
60630Sstevel@tonic-gate 				/* pg & inst are reset now, so just return. */
60640Sstevel@tonic-gate 				ret = ECONNABORTED;
60650Sstevel@tonic-gate 				goto out;
60660Sstevel@tonic-gate 
60670Sstevel@tonic-gate 			default:
60680Sstevel@tonic-gate 				bad_error("dgraph_refresh_instance", r);
60690Sstevel@tonic-gate 			}
60700Sstevel@tonic-gate 		}
60710Sstevel@tonic-gate 
60720Sstevel@tonic-gate 		vertex_send_event(vertex, admin_events[a]);
60730Sstevel@tonic-gate 
60740Sstevel@tonic-gate 		r = libscf_unset_action(h, pg, a, action_ts[a]);
60750Sstevel@tonic-gate 		switch (r) {
60760Sstevel@tonic-gate 		case 0:
60770Sstevel@tonic-gate 		case EACCES:
60780Sstevel@tonic-gate 			break;
60790Sstevel@tonic-gate 
60800Sstevel@tonic-gate 		case ECONNABORTED:
60810Sstevel@tonic-gate 			ret = ECONNABORTED;
60820Sstevel@tonic-gate 			goto out;
60830Sstevel@tonic-gate 
60840Sstevel@tonic-gate 		case EPERM:
60850Sstevel@tonic-gate 			uu_die("Insufficient privilege.\n");
60860Sstevel@tonic-gate 			/* NOTREACHED */
60870Sstevel@tonic-gate 
60880Sstevel@tonic-gate 		default:
60890Sstevel@tonic-gate 			bad_error("libscf_unset_action", r);
60900Sstevel@tonic-gate 		}
60910Sstevel@tonic-gate 
60920Sstevel@tonic-gate 		action_ts[a] = 0;
60930Sstevel@tonic-gate 	}
60940Sstevel@tonic-gate 
60950Sstevel@tonic-gate out:
60960Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
60970Sstevel@tonic-gate 
60980Sstevel@tonic-gate 	scf_property_destroy(prop);
60990Sstevel@tonic-gate 	scf_value_destroy(val);
61000Sstevel@tonic-gate 	startd_free(inst_name, max_scf_fmri_size);
61010Sstevel@tonic-gate 	return (ret);
61020Sstevel@tonic-gate }
61030Sstevel@tonic-gate 
61040Sstevel@tonic-gate /*
61050Sstevel@tonic-gate  * inst and pg_name are scratch space, and are unset on entry.
61060Sstevel@tonic-gate  * Returns
61070Sstevel@tonic-gate  *   0 - success
61080Sstevel@tonic-gate  *   ECONNRESET - success, but repository handle rebound
61090Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
61100Sstevel@tonic-gate  */
61110Sstevel@tonic-gate static int
61120Sstevel@tonic-gate process_pg_event(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst,
61130Sstevel@tonic-gate     char *pg_name)
61140Sstevel@tonic-gate {
61150Sstevel@tonic-gate 	int r;
61160Sstevel@tonic-gate 	scf_property_t *prop;
61170Sstevel@tonic-gate 	scf_value_t *val;
61180Sstevel@tonic-gate 	char *fmri;
61190Sstevel@tonic-gate 	boolean_t rebound = B_FALSE, rebind_inst = B_FALSE;
61200Sstevel@tonic-gate 
61210Sstevel@tonic-gate 	if (scf_pg_get_name(pg, pg_name, max_scf_value_size) < 0) {
61220Sstevel@tonic-gate 		switch (scf_error()) {
61230Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
61240Sstevel@tonic-gate 		default:
61250Sstevel@tonic-gate 			return (ECONNABORTED);
61260Sstevel@tonic-gate 
61270Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
61280Sstevel@tonic-gate 			return (0);
61290Sstevel@tonic-gate 
61300Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
61310Sstevel@tonic-gate 			bad_error("scf_pg_get_name", scf_error());
61320Sstevel@tonic-gate 		}
61330Sstevel@tonic-gate 	}
61340Sstevel@tonic-gate 
61350Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_GENERAL) == 0 ||
61360Sstevel@tonic-gate 	    strcmp(pg_name, SCF_PG_GENERAL_OVR) == 0) {
61370Sstevel@tonic-gate 		r = dgraph_update_general(pg);
61380Sstevel@tonic-gate 		switch (r) {
61390Sstevel@tonic-gate 		case 0:
61400Sstevel@tonic-gate 		case ENOTSUP:
61410Sstevel@tonic-gate 		case ECANCELED:
61420Sstevel@tonic-gate 			return (0);
61430Sstevel@tonic-gate 
61440Sstevel@tonic-gate 		case ECONNABORTED:
61450Sstevel@tonic-gate 			return (ECONNABORTED);
61460Sstevel@tonic-gate 
61470Sstevel@tonic-gate 		case -1:
61480Sstevel@tonic-gate 			/* Error should have been logged. */
61490Sstevel@tonic-gate 			return (0);
61500Sstevel@tonic-gate 
61510Sstevel@tonic-gate 		default:
61520Sstevel@tonic-gate 			bad_error("dgraph_update_general", r);
61530Sstevel@tonic-gate 		}
61540Sstevel@tonic-gate 	} else if (strcmp(pg_name, SCF_PG_RESTARTER_ACTIONS) == 0) {
61550Sstevel@tonic-gate 		if (scf_pg_get_parent_instance(pg, inst) != 0) {
61560Sstevel@tonic-gate 			switch (scf_error()) {
61570Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
61580Sstevel@tonic-gate 				return (ECONNABORTED);
61590Sstevel@tonic-gate 
61600Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
61610Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
61620Sstevel@tonic-gate 				/* Ignore commands on services. */
61630Sstevel@tonic-gate 				return (0);
61640Sstevel@tonic-gate 
61650Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
61660Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
61670Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
61680Sstevel@tonic-gate 			default:
61690Sstevel@tonic-gate 				bad_error("scf_pg_get_parent_instance",
61700Sstevel@tonic-gate 				    scf_error());
61710Sstevel@tonic-gate 			}
61720Sstevel@tonic-gate 		}
61730Sstevel@tonic-gate 
61740Sstevel@tonic-gate 		return (process_actions(h, pg, inst));
61750Sstevel@tonic-gate 	}
61760Sstevel@tonic-gate 
61770Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_OPTIONS) != 0 &&
61780Sstevel@tonic-gate 	    strcmp(pg_name, SCF_PG_OPTIONS_OVR) != 0)
61790Sstevel@tonic-gate 		return (0);
61800Sstevel@tonic-gate 
61810Sstevel@tonic-gate 	/*
61820Sstevel@tonic-gate 	 * We only care about the options[_ovr] property groups of our own
61830Sstevel@tonic-gate 	 * instance, so get the fmri and compare.  Plus, once we know it's
61840Sstevel@tonic-gate 	 * correct, if the repository connection is broken we know exactly what
61850Sstevel@tonic-gate 	 * property group we were operating on, and can look it up again.
61860Sstevel@tonic-gate 	 */
61870Sstevel@tonic-gate 	if (scf_pg_get_parent_instance(pg, inst) != 0) {
61880Sstevel@tonic-gate 		switch (scf_error()) {
61890Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
61900Sstevel@tonic-gate 			return (ECONNABORTED);
61910Sstevel@tonic-gate 
61920Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
61930Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
61940Sstevel@tonic-gate 			return (0);
61950Sstevel@tonic-gate 
61960Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
61970Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
61980Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
61990Sstevel@tonic-gate 		default:
62000Sstevel@tonic-gate 			bad_error("scf_pg_get_parent_instance",
62010Sstevel@tonic-gate 			    scf_error());
62020Sstevel@tonic-gate 		}
62030Sstevel@tonic-gate 	}
62040Sstevel@tonic-gate 
62050Sstevel@tonic-gate 	switch (r = libscf_instance_get_fmri(inst, &fmri)) {
62060Sstevel@tonic-gate 	case 0:
62070Sstevel@tonic-gate 		break;
62080Sstevel@tonic-gate 
62090Sstevel@tonic-gate 	case ECONNABORTED:
62100Sstevel@tonic-gate 		return (ECONNABORTED);
62110Sstevel@tonic-gate 
62120Sstevel@tonic-gate 	case ECANCELED:
62130Sstevel@tonic-gate 		return (0);
62140Sstevel@tonic-gate 
62150Sstevel@tonic-gate 	default:
62160Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", r);
62170Sstevel@tonic-gate 	}
62180Sstevel@tonic-gate 
62190Sstevel@tonic-gate 	if (strcmp(fmri, SCF_SERVICE_STARTD) != 0) {
62200Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
62210Sstevel@tonic-gate 		return (0);
62220Sstevel@tonic-gate 	}
62230Sstevel@tonic-gate 
62240Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
62250Sstevel@tonic-gate 	val = safe_scf_value_create(h);
62260Sstevel@tonic-gate 
62270Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_OPTIONS_OVR) == 0) {
62280Sstevel@tonic-gate 		/* See if we need to set the runlevel. */
62290Sstevel@tonic-gate 		/* CONSTCOND */
62300Sstevel@tonic-gate 		if (0) {
62310Sstevel@tonic-gate rebind_pg:
62320Sstevel@tonic-gate 			libscf_handle_rebind(h);
62330Sstevel@tonic-gate 			rebound = B_TRUE;
62340Sstevel@tonic-gate 
62350Sstevel@tonic-gate 			r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst);
62360Sstevel@tonic-gate 			switch (r) {
62370Sstevel@tonic-gate 			case 0:
62380Sstevel@tonic-gate 				break;
62390Sstevel@tonic-gate 
62400Sstevel@tonic-gate 			case ECONNABORTED:
62410Sstevel@tonic-gate 				goto rebind_pg;
62420Sstevel@tonic-gate 
62430Sstevel@tonic-gate 			case ENOENT:
62440Sstevel@tonic-gate 				goto out;
62450Sstevel@tonic-gate 
62460Sstevel@tonic-gate 			case EINVAL:
62470Sstevel@tonic-gate 			case ENOTSUP:
62480Sstevel@tonic-gate 				bad_error("libscf_lookup_instance", r);
62490Sstevel@tonic-gate 			}
62500Sstevel@tonic-gate 
62510Sstevel@tonic-gate 			if (scf_instance_get_pg(inst, pg_name, pg) != 0) {
62520Sstevel@tonic-gate 				switch (scf_error()) {
62530Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
62540Sstevel@tonic-gate 				case SCF_ERROR_NOT_FOUND:
62550Sstevel@tonic-gate 					goto out;
62560Sstevel@tonic-gate 
62570Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
62580Sstevel@tonic-gate 					goto rebind_pg;
62590Sstevel@tonic-gate 
62600Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
62610Sstevel@tonic-gate 				case SCF_ERROR_NOT_BOUND:
62620Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
62630Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
62640Sstevel@tonic-gate 				default:
62650Sstevel@tonic-gate 					bad_error("scf_instance_get_pg",
62660Sstevel@tonic-gate 					    scf_error());
62670Sstevel@tonic-gate 				}
62680Sstevel@tonic-gate 			}
62690Sstevel@tonic-gate 		}
62700Sstevel@tonic-gate 
62710Sstevel@tonic-gate 		if (scf_pg_get_property(pg, "runlevel", prop) == 0) {
62720Sstevel@tonic-gate 			r = dgraph_set_runlevel(pg, prop);
62730Sstevel@tonic-gate 			switch (r) {
62740Sstevel@tonic-gate 			case ECONNRESET:
62750Sstevel@tonic-gate 				rebound = B_TRUE;
62760Sstevel@tonic-gate 				rebind_inst = B_TRUE;
62770Sstevel@tonic-gate 				/* FALLTHROUGH */
62780Sstevel@tonic-gate 
62790Sstevel@tonic-gate 			case 0:
62800Sstevel@tonic-gate 				break;
62810Sstevel@tonic-gate 
62820Sstevel@tonic-gate 			case ECONNABORTED:
62830Sstevel@tonic-gate 				goto rebind_pg;
62840Sstevel@tonic-gate 
62850Sstevel@tonic-gate 			case ECANCELED:
62860Sstevel@tonic-gate 				goto out;
62870Sstevel@tonic-gate 
62880Sstevel@tonic-gate 			default:
62890Sstevel@tonic-gate 				bad_error("dgraph_set_runlevel", r);
62900Sstevel@tonic-gate 			}
62910Sstevel@tonic-gate 		} else {
62920Sstevel@tonic-gate 			switch (scf_error()) {
62930Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
62940Sstevel@tonic-gate 			default:
62950Sstevel@tonic-gate 				goto rebind_pg;
62960Sstevel@tonic-gate 
62970Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
62980Sstevel@tonic-gate 				goto out;
62990Sstevel@tonic-gate 
63000Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
63010Sstevel@tonic-gate 				break;
63020Sstevel@tonic-gate 
63030Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
63040Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
63050Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
63060Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
63070Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
63080Sstevel@tonic-gate 			}
63090Sstevel@tonic-gate 		}
63100Sstevel@tonic-gate 	}
63110Sstevel@tonic-gate 
63120Sstevel@tonic-gate 	if (rebind_inst) {
63130Sstevel@tonic-gate lookup_inst:
63140Sstevel@tonic-gate 		r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst);
63150Sstevel@tonic-gate 		switch (r) {
63160Sstevel@tonic-gate 		case 0:
63170Sstevel@tonic-gate 			break;
63180Sstevel@tonic-gate 
63190Sstevel@tonic-gate 		case ECONNABORTED:
63200Sstevel@tonic-gate 			libscf_handle_rebind(h);
63210Sstevel@tonic-gate 			rebound = B_TRUE;
63220Sstevel@tonic-gate 			goto lookup_inst;
63230Sstevel@tonic-gate 
63240Sstevel@tonic-gate 		case ENOENT:
63250Sstevel@tonic-gate 			goto out;
63260Sstevel@tonic-gate 
63270Sstevel@tonic-gate 		case EINVAL:
63280Sstevel@tonic-gate 		case ENOTSUP:
63290Sstevel@tonic-gate 			bad_error("libscf_lookup_instance", r);
63300Sstevel@tonic-gate 		}
63310Sstevel@tonic-gate 	}
63320Sstevel@tonic-gate 
63330Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size);
63340Sstevel@tonic-gate 	switch (r) {
63350Sstevel@tonic-gate 	case 0:
63360Sstevel@tonic-gate 		break;
63370Sstevel@tonic-gate 
63380Sstevel@tonic-gate 	case ECONNABORTED:
63390Sstevel@tonic-gate 		libscf_handle_rebind(h);
63400Sstevel@tonic-gate 		rebound = B_TRUE;
63410Sstevel@tonic-gate 		goto lookup_inst;
63420Sstevel@tonic-gate 
63430Sstevel@tonic-gate 	case EINVAL:
63440Sstevel@tonic-gate 		log_error(LOG_NOTICE,
63450Sstevel@tonic-gate 		    "%s/%s property of %s is misconfigured.\n", pg_name,
63460Sstevel@tonic-gate 		    SCF_PROPERTY_MILESTONE, SCF_SERVICE_STARTD);
63470Sstevel@tonic-gate 		/* FALLTHROUGH */
63480Sstevel@tonic-gate 
63490Sstevel@tonic-gate 	case ECANCELED:
63500Sstevel@tonic-gate 	case ENOENT:
63510Sstevel@tonic-gate 		(void) strcpy(fmri, "all");
63520Sstevel@tonic-gate 		break;
63530Sstevel@tonic-gate 
63540Sstevel@tonic-gate 	default:
63550Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
63560Sstevel@tonic-gate 	}
63570Sstevel@tonic-gate 
63580Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_FALSE);
63590Sstevel@tonic-gate 	switch (r) {
63600Sstevel@tonic-gate 	case 0:
63610Sstevel@tonic-gate 	case ECONNRESET:
63620Sstevel@tonic-gate 	case EALREADY:
63630Sstevel@tonic-gate 		break;
63640Sstevel@tonic-gate 
63650Sstevel@tonic-gate 	case EINVAL:
63660Sstevel@tonic-gate 		log_error(LOG_WARNING, "Milestone %s is invalid.\n", fmri);
63670Sstevel@tonic-gate 		break;
63680Sstevel@tonic-gate 
63690Sstevel@tonic-gate 	case ENOENT:
63700Sstevel@tonic-gate 		log_error(LOG_WARNING, "Milestone %s does not exist.\n", fmri);
63710Sstevel@tonic-gate 		break;
63720Sstevel@tonic-gate 
63730Sstevel@tonic-gate 	default:
63740Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
63750Sstevel@tonic-gate 	}
63760Sstevel@tonic-gate 
63770Sstevel@tonic-gate out:
63780Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
63790Sstevel@tonic-gate 	scf_value_destroy(val);
63800Sstevel@tonic-gate 	scf_property_destroy(prop);
63810Sstevel@tonic-gate 
63820Sstevel@tonic-gate 	return (rebound ? ECONNRESET : 0);
63830Sstevel@tonic-gate }
63840Sstevel@tonic-gate 
63856055Srm88369 /*
63866055Srm88369  * process_delete() deletes an instance from the dgraph if 'fmri' is an
63876055Srm88369  * instance fmri or if 'fmri' matches the 'general' property group of an
63886055Srm88369  * instance (or the 'general/enabled' property).
63896055Srm88369  *
63906055Srm88369  * 'fmri' may be overwritten and cannot be trusted on return by the caller.
63916055Srm88369  */
63920Sstevel@tonic-gate static void
63930Sstevel@tonic-gate process_delete(char *fmri, scf_handle_t *h)
63940Sstevel@tonic-gate {
63956055Srm88369 	char *lfmri, *end_inst_fmri;
63966055Srm88369 	const char *inst_name = NULL;
63976055Srm88369 	const char *pg_name = NULL;
63986055Srm88369 	const char *prop_name = NULL;
63990Sstevel@tonic-gate 
64000Sstevel@tonic-gate 	lfmri = safe_strdup(fmri);
64010Sstevel@tonic-gate 
64020Sstevel@tonic-gate 	/* Determine if the FMRI is a property group or instance */
64030Sstevel@tonic-gate 	if (scf_parse_svc_fmri(lfmri, NULL, NULL, &inst_name, &pg_name,
64046055Srm88369 	    &prop_name) != SCF_SUCCESS) {
64050Sstevel@tonic-gate 		log_error(LOG_WARNING,
64060Sstevel@tonic-gate 		    "Received invalid FMRI \"%s\" from repository server.\n",
64070Sstevel@tonic-gate 		    fmri);
64080Sstevel@tonic-gate 	} else if (inst_name != NULL && pg_name == NULL) {
64090Sstevel@tonic-gate 		(void) dgraph_remove_instance(fmri, h);
64106055Srm88369 	} else if (inst_name != NULL && pg_name != NULL) {
64116055Srm88369 		/*
64126055Srm88369 		 * If we're deleting the 'general' property group or
64136055Srm88369 		 * 'general/enabled' property then the whole instance
64146055Srm88369 		 * must be removed from the dgraph.
64156055Srm88369 		 */
64166055Srm88369 		if (strcmp(pg_name, SCF_PG_GENERAL) != 0) {
64176055Srm88369 			free(lfmri);
64186055Srm88369 			return;
64196055Srm88369 		}
64206055Srm88369 
64216055Srm88369 		if (prop_name != NULL &&
64226055Srm88369 		    strcmp(prop_name, SCF_PROPERTY_ENABLED) != 0) {
64236055Srm88369 			free(lfmri);
64246055Srm88369 			return;
64256055Srm88369 		}
64266055Srm88369 
64276055Srm88369 		/*
64286055Srm88369 		 * Because the instance has already been deleted from the
64296055Srm88369 		 * repository, we cannot use any scf_ functions to retrieve
64306055Srm88369 		 * the instance FMRI however we can easily reconstruct it
64316055Srm88369 		 * manually.
64326055Srm88369 		 */
64336055Srm88369 		end_inst_fmri = strstr(fmri, SCF_FMRI_PROPERTYGRP_PREFIX);
64346055Srm88369 		if (end_inst_fmri == NULL)
64356055Srm88369 			bad_error("process_delete", 0);
64366055Srm88369 
64376055Srm88369 		end_inst_fmri[0] = '\0';
64386055Srm88369 
64396055Srm88369 		(void) dgraph_remove_instance(fmri, h);
64400Sstevel@tonic-gate 	}
64410Sstevel@tonic-gate 
64420Sstevel@tonic-gate 	free(lfmri);
64430Sstevel@tonic-gate }
64440Sstevel@tonic-gate 
64450Sstevel@tonic-gate /*ARGSUSED*/
64460Sstevel@tonic-gate void *
64470Sstevel@tonic-gate repository_event_thread(void *unused)
64480Sstevel@tonic-gate {
64490Sstevel@tonic-gate 	scf_handle_t *h;
64500Sstevel@tonic-gate 	scf_propertygroup_t *pg;
64510Sstevel@tonic-gate 	scf_instance_t *inst;
64520Sstevel@tonic-gate 	char *fmri = startd_alloc(max_scf_fmri_size);
64530Sstevel@tonic-gate 	char *pg_name = startd_alloc(max_scf_value_size);
64540Sstevel@tonic-gate 	int r;
64550Sstevel@tonic-gate 
64560Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
64570Sstevel@tonic-gate 
64580Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
64590Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
64600Sstevel@tonic-gate 
64610Sstevel@tonic-gate retry:
64620Sstevel@tonic-gate 	if (_scf_notify_add_pgtype(h, SCF_GROUP_FRAMEWORK) != SCF_SUCCESS) {
64630Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN) {
64640Sstevel@tonic-gate 			libscf_handle_rebind(h);
64650Sstevel@tonic-gate 		} else {
64660Sstevel@tonic-gate 			log_error(LOG_WARNING,
64670Sstevel@tonic-gate 			    "Couldn't set up repository notification "
64680Sstevel@tonic-gate 			    "for property group type %s: %s\n",
64690Sstevel@tonic-gate 			    SCF_GROUP_FRAMEWORK, scf_strerror(scf_error()));
64700Sstevel@tonic-gate 
64710Sstevel@tonic-gate 			(void) sleep(1);
64720Sstevel@tonic-gate 		}
64730Sstevel@tonic-gate 
64740Sstevel@tonic-gate 		goto retry;
64750Sstevel@tonic-gate 	}
64760Sstevel@tonic-gate 
64770Sstevel@tonic-gate 	/*CONSTCOND*/
64780Sstevel@tonic-gate 	while (1) {
64790Sstevel@tonic-gate 		ssize_t res;
64800Sstevel@tonic-gate 
64810Sstevel@tonic-gate 		/* Note: fmri is only set on delete events. */
64820Sstevel@tonic-gate 		res = _scf_notify_wait(pg, fmri, max_scf_fmri_size);
64830Sstevel@tonic-gate 		if (res < 0) {
64840Sstevel@tonic-gate 			libscf_handle_rebind(h);
64850Sstevel@tonic-gate 			goto retry;
64860Sstevel@tonic-gate 		} else if (res == 0) {
64870Sstevel@tonic-gate 			/*
64880Sstevel@tonic-gate 			 * property group modified.  inst and pg_name are
64890Sstevel@tonic-gate 			 * pre-allocated scratch space.
64900Sstevel@tonic-gate 			 */
64910Sstevel@tonic-gate 			if (scf_pg_update(pg) < 0) {
64920Sstevel@tonic-gate 				switch (scf_error()) {
64930Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
64940Sstevel@tonic-gate 					continue;
64950Sstevel@tonic-gate 
64960Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
64970Sstevel@tonic-gate 					log_error(LOG_WARNING,
64980Sstevel@tonic-gate 					    "Lost repository event due to "
64990Sstevel@tonic-gate 					    "disconnection.\n");
65000Sstevel@tonic-gate 					libscf_handle_rebind(h);
65010Sstevel@tonic-gate 					goto retry;
65020Sstevel@tonic-gate 
65030Sstevel@tonic-gate 				case SCF_ERROR_NOT_BOUND:
65040Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
65050Sstevel@tonic-gate 				default:
65060Sstevel@tonic-gate 					bad_error("scf_pg_update", scf_error());
65070Sstevel@tonic-gate 				}
65080Sstevel@tonic-gate 			}
65090Sstevel@tonic-gate 
65100Sstevel@tonic-gate 			r = process_pg_event(h, pg, inst, pg_name);
65110Sstevel@tonic-gate 			switch (r) {
65120Sstevel@tonic-gate 			case 0:
65130Sstevel@tonic-gate 				break;
65140Sstevel@tonic-gate 
65150Sstevel@tonic-gate 			case ECONNABORTED:
65160Sstevel@tonic-gate 				log_error(LOG_WARNING, "Lost repository event "
65170Sstevel@tonic-gate 				    "due to disconnection.\n");
65180Sstevel@tonic-gate 				libscf_handle_rebind(h);
65190Sstevel@tonic-gate 				/* FALLTHROUGH */
65200Sstevel@tonic-gate 
65210Sstevel@tonic-gate 			case ECONNRESET:
65220Sstevel@tonic-gate 				goto retry;
65230Sstevel@tonic-gate 
65240Sstevel@tonic-gate 			default:
65250Sstevel@tonic-gate 				bad_error("process_pg_event", r);
65260Sstevel@tonic-gate 			}
65270Sstevel@tonic-gate 		} else {
65286055Srm88369 			/*
65296055Srm88369 			 * Service, instance, or pg deleted.
65306055Srm88369 			 * Don't trust fmri on return.
65316055Srm88369 			 */
65320Sstevel@tonic-gate 			process_delete(fmri, h);
65330Sstevel@tonic-gate 		}
65340Sstevel@tonic-gate 	}
65350Sstevel@tonic-gate 
65360Sstevel@tonic-gate 	/*NOTREACHED*/
65370Sstevel@tonic-gate 	return (NULL);
65380Sstevel@tonic-gate }
65390Sstevel@tonic-gate 
65400Sstevel@tonic-gate void
65410Sstevel@tonic-gate graph_engine_start()
65420Sstevel@tonic-gate {
65430Sstevel@tonic-gate 	int err;
65440Sstevel@tonic-gate 
65450Sstevel@tonic-gate 	(void) startd_thread_create(graph_thread, NULL);
65460Sstevel@tonic-gate 
65470Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
65480Sstevel@tonic-gate 	while (!initial_milestone_set) {
65490Sstevel@tonic-gate 		err = pthread_cond_wait(&initial_milestone_cv, &dgraph_lock);
65500Sstevel@tonic-gate 		assert(err == 0);
65510Sstevel@tonic-gate 	}
65520Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
65530Sstevel@tonic-gate 
65540Sstevel@tonic-gate 	(void) startd_thread_create(repository_event_thread, NULL);
65550Sstevel@tonic-gate 	(void) startd_thread_create(graph_event_thread, NULL);
65560Sstevel@tonic-gate }
6557