xref: /onnv-gate/usr/src/cmd/svc/startd/graph.c (revision 11996:91b62f7b8186)
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  */
2111466SRoger.Faulkner@Sun.COM 
220Sstevel@tonic-gate /*
2311466SRoger.Faulkner@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * graph.c - master restarter graph engine
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  *   The graph engine keeps a dependency graph of all service instances on the
310Sstevel@tonic-gate  *   system, as recorded in the repository.  It decides when services should
320Sstevel@tonic-gate  *   be brought up or down based on service states and dependencies and sends
330Sstevel@tonic-gate  *   commands to restarters to effect any changes.  It also executes
340Sstevel@tonic-gate  *   administrator commands sent by svcadm via the repository.
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  *   The graph is stored in uu_list_t *dgraph and its vertices are
370Sstevel@tonic-gate  *   graph_vertex_t's, each of which has a name and an integer id unique to
380Sstevel@tonic-gate  *   its name (see dict.c).  A vertex's type attribute designates the type
390Sstevel@tonic-gate  *   of object it represents: GVT_INST for service instances, GVT_SVC for
400Sstevel@tonic-gate  *   service objects (since service instances may depend on another service,
410Sstevel@tonic-gate  *   rather than service instance), GVT_FILE for files (which services may
420Sstevel@tonic-gate  *   depend on), and GVT_GROUP for dependencies on multiple objects.  GVT_GROUP
430Sstevel@tonic-gate  *   vertices are necessary because dependency lists may have particular
440Sstevel@tonic-gate  *   grouping types (require any, require all, optional, or exclude) and
450Sstevel@tonic-gate  *   event-propagation characteristics.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  *   The initial graph is built by libscf_populate_graph() invoking
480Sstevel@tonic-gate  *   dgraph_add_instance() for each instance in the repository.  The function
490Sstevel@tonic-gate  *   adds a GVT_SVC vertex for the service if one does not already exist, adds
500Sstevel@tonic-gate  *   a GVT_INST vertex named by the FMRI of the instance, and sets up the edges.
510Sstevel@tonic-gate  *   The resulting web of vertices & edges associated with an instance's vertex
520Sstevel@tonic-gate  *   includes
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  *     - an edge from the GVT_SVC vertex for the instance's service
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  *     - an edge to the GVT_INST vertex of the instance's resarter, if its
570Sstevel@tonic-gate  *       restarter is not svc.startd
580Sstevel@tonic-gate  *
590Sstevel@tonic-gate  *     - edges from other GVT_INST vertices if the instance is a restarter
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  *     - for each dependency property group in the instance's "running"
620Sstevel@tonic-gate  *       snapshot, an edge to a GVT_GROUP vertex named by the FMRI of the
630Sstevel@tonic-gate  *       instance and the name of the property group
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  *     - for each value of the "entities" property in each dependency property
660Sstevel@tonic-gate  *       group, an edge from the corresponding GVT_GROUP vertex to a
670Sstevel@tonic-gate  *       GVT_INST, GVT_SVC, or GVT_FILE vertex
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  *     - edges from GVT_GROUP vertices for each dependent instance
700Sstevel@tonic-gate  *
710Sstevel@tonic-gate  *   After the edges are set up the vertex's GV_CONFIGURED flag is set.  If
720Sstevel@tonic-gate  *   there are problems, or if a service is mentioned in a dependency but does
730Sstevel@tonic-gate  *   not exist in the repository, the GV_CONFIGURED flag will be clear.
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  *   The graph and all of its vertices are protected by the dgraph_lock mutex.
760Sstevel@tonic-gate  *   See restarter.c for more information.
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  *   The properties of an instance fall into two classes: immediate and
790Sstevel@tonic-gate  *   snapshotted.  Immediate properties should have an immediate effect when
800Sstevel@tonic-gate  *   changed.  Snapshotted properties should be read from a snapshot, so they
810Sstevel@tonic-gate  *   only change when the snapshot changes.  The immediate properties used by
820Sstevel@tonic-gate  *   the graph engine are general/enabled, general/restarter, and the properties
830Sstevel@tonic-gate  *   in the restarter_actions property group.  Since they are immediate, they
840Sstevel@tonic-gate  *   are not read out of a snapshot.  The snapshotted properties used by the
850Sstevel@tonic-gate  *   graph engine are those in the property groups with type "dependency" and
860Sstevel@tonic-gate  *   are read out of the "running" snapshot.  The "running" snapshot is created
870Sstevel@tonic-gate  *   by the the graph engine as soon as possible, and it is updated, along with
880Sstevel@tonic-gate  *   in-core copies of the data (dependency information for the graph engine) on
890Sstevel@tonic-gate  *   receipt of the refresh command from svcadm.  In addition, the graph engine
900Sstevel@tonic-gate  *   updates the "start" snapshot from the "running" snapshot whenever a service
910Sstevel@tonic-gate  *   comes online.
927630SRenaud.Manus@Sun.COM  *
937630SRenaud.Manus@Sun.COM  *   When a DISABLE event is requested by the administrator, svc.startd shutdown
947630SRenaud.Manus@Sun.COM  *   the dependents first before shutting down the requested service.
957630SRenaud.Manus@Sun.COM  *   In graph_enable_by_vertex, we create a subtree that contains the dependent
967630SRenaud.Manus@Sun.COM  *   vertices by marking those vertices with the GV_TOOFFLINE flag. And we mark
977630SRenaud.Manus@Sun.COM  *   the vertex to disable with the GV_TODISABLE flag. Once the tree is created,
987630SRenaud.Manus@Sun.COM  *   we send the _ADMIN_DISABLE event to the leaves. The leaves will then
997630SRenaud.Manus@Sun.COM  *   transition from STATE_ONLINE/STATE_DEGRADED to STATE_OFFLINE/STATE_MAINT.
1007630SRenaud.Manus@Sun.COM  *   In gt_enter_offline and gt_enter_maint if the vertex was in a subtree then
1017630SRenaud.Manus@Sun.COM  *   we clear the GV_TOOFFLINE flag and walk the dependencies to offline the new
1027630SRenaud.Manus@Sun.COM  *   exposed leaves. We do the same until we reach the last leaf (the one with
1037630SRenaud.Manus@Sun.COM  *   the GV_TODISABLE flag). If the vertex to disable is also part of a larger
1047630SRenaud.Manus@Sun.COM  *   subtree (eg. multiple DISABLE events on vertices in the same subtree) then
1057630SRenaud.Manus@Sun.COM  *   once the first vertex is disabled (GV_TODISABLE flag is removed), we
1067630SRenaud.Manus@Sun.COM  *   continue to propagate the offline event to the vertex's dependencies.
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate #include <sys/uadmin.h>
1100Sstevel@tonic-gate #include <sys/wait.h>
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate #include <assert.h>
1130Sstevel@tonic-gate #include <errno.h>
1140Sstevel@tonic-gate #include <fcntl.h>
1150Sstevel@tonic-gate #include <libscf.h>
1160Sstevel@tonic-gate #include <libscf_priv.h>
1170Sstevel@tonic-gate #include <libuutil.h>
1180Sstevel@tonic-gate #include <locale.h>
1190Sstevel@tonic-gate #include <poll.h>
1200Sstevel@tonic-gate #include <pthread.h>
1210Sstevel@tonic-gate #include <signal.h>
1220Sstevel@tonic-gate #include <stddef.h>
1230Sstevel@tonic-gate #include <stdio.h>
1240Sstevel@tonic-gate #include <stdlib.h>
1250Sstevel@tonic-gate #include <string.h>
1260Sstevel@tonic-gate #include <strings.h>
1270Sstevel@tonic-gate #include <sys/statvfs.h>
1280Sstevel@tonic-gate #include <sys/uadmin.h>
1290Sstevel@tonic-gate #include <zone.h>
1309160SSherry.Moore@Sun.COM #if defined(__i386)
1319160SSherry.Moore@Sun.COM #include <libgrubmgmt.h>
1329160SSherry.Moore@Sun.COM #endif	/* __i386 */
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate #include "startd.h"
1350Sstevel@tonic-gate #include "protocol.h"
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate #define	MILESTONE_NONE	((graph_vertex_t *)1)
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate #define	CONSOLE_LOGIN_FMRI	"svc:/system/console-login:default"
1410Sstevel@tonic-gate #define	FS_MINIMAL_FMRI		"svc:/system/filesystem/minimal:default"
1420Sstevel@tonic-gate 
1431712Srm88369 #define	VERTEX_REMOVED	0	/* vertex has been freed  */
1441712Srm88369 #define	VERTEX_INUSE	1	/* vertex is still in use */
1451712Srm88369 
1462747Sbustos /*
1472747Sbustos  * Services in these states are not considered 'down' by the
1482747Sbustos  * milestone/shutdown code.
1492747Sbustos  */
1502747Sbustos #define	up_state(state)	((state) == RESTARTER_STATE_ONLINE || \
1512747Sbustos 	(state) == RESTARTER_STATE_DEGRADED || \
1522747Sbustos 	(state) == RESTARTER_STATE_OFFLINE)
1532747Sbustos 
1540Sstevel@tonic-gate static uu_list_pool_t *graph_edge_pool, *graph_vertex_pool;
1550Sstevel@tonic-gate static uu_list_t *dgraph;
1560Sstevel@tonic-gate static pthread_mutex_t dgraph_lock;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate  * milestone indicates the current subgraph.  When NULL, it is the entire
1600Sstevel@tonic-gate  * graph.  When MILESTONE_NONE, it is the empty graph.  Otherwise, it is all
1610Sstevel@tonic-gate  * services on which the target vertex depends.
1620Sstevel@tonic-gate  */
1630Sstevel@tonic-gate static graph_vertex_t *milestone = NULL;
1640Sstevel@tonic-gate static boolean_t initial_milestone_set = B_FALSE;
1650Sstevel@tonic-gate static pthread_cond_t initial_milestone_cv = PTHREAD_COND_INITIALIZER;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /* protected by dgraph_lock */
1680Sstevel@tonic-gate static boolean_t sulogin_thread_running = B_FALSE;
1690Sstevel@tonic-gate static boolean_t sulogin_running = B_FALSE;
1700Sstevel@tonic-gate static boolean_t console_login_ready = B_FALSE;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate /* Number of services to come down to complete milestone transition. */
1730Sstevel@tonic-gate static uint_t non_subgraph_svcs;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate /*
1760Sstevel@tonic-gate  * These variables indicate what should be done when we reach the milestone
1770Sstevel@tonic-gate  * target milestone, i.e., when non_subgraph_svcs == 0.  They are acted upon in
1780Sstevel@tonic-gate  * dgraph_set_instance_state().
1790Sstevel@tonic-gate  */
1800Sstevel@tonic-gate static int halting = -1;
1810Sstevel@tonic-gate static boolean_t go_single_user_mode = B_FALSE;
1820Sstevel@tonic-gate static boolean_t go_to_level1 = B_FALSE;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate /*
1858944Sdp@eng.sun.com  * Tracks when we started halting.
1868944Sdp@eng.sun.com  */
1878944Sdp@eng.sun.com static time_t halting_time = 0;
1888944Sdp@eng.sun.com 
1898944Sdp@eng.sun.com /*
1900Sstevel@tonic-gate  * This tracks the legacy runlevel to ensure we signal init and manage
1910Sstevel@tonic-gate  * utmpx entries correctly.
1920Sstevel@tonic-gate  */
1930Sstevel@tonic-gate static char current_runlevel = '\0';
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate /* Number of single user threads currently running */
1960Sstevel@tonic-gate static pthread_mutex_t single_user_thread_lock;
1970Sstevel@tonic-gate static int single_user_thread_count = 0;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /* Statistics for dependency cycle-checking */
2000Sstevel@tonic-gate static u_longlong_t dep_inserts = 0;
2010Sstevel@tonic-gate static u_longlong_t dep_cycle_ns = 0;
2020Sstevel@tonic-gate static u_longlong_t dep_insert_ns = 0;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate static const char * const emsg_invalid_restarter =
2061958Slianep 	"Transitioning %s to maintenance, restarter FMRI %s is invalid "
2071958Slianep 	"(see 'svcs -xv' for details).\n";
2080Sstevel@tonic-gate static const char * const console_login_fmri = CONSOLE_LOGIN_FMRI;
2090Sstevel@tonic-gate static const char * const single_user_fmri = SCF_MILESTONE_SINGLE_USER;
2100Sstevel@tonic-gate static const char * const multi_user_fmri = SCF_MILESTONE_MULTI_USER;
2110Sstevel@tonic-gate static const char * const multi_user_svr_fmri = SCF_MILESTONE_MULTI_USER_SERVER;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * These services define the system being "up".  If none of them can come
2160Sstevel@tonic-gate  * online, then we will run sulogin on the console.  Note that the install ones
2170Sstevel@tonic-gate  * are for the miniroot and when installing CDs after the first.  can_come_up()
2180Sstevel@tonic-gate  * does the decision making, and an sulogin_thread() runs sulogin, which can be
2190Sstevel@tonic-gate  * started by dgraph_set_instance_state() or single_user_thread().
2200Sstevel@tonic-gate  *
2210Sstevel@tonic-gate  * NOTE: can_come_up() relies on SCF_MILESTONE_SINGLE_USER being the first
2220Sstevel@tonic-gate  * entry, which is only used when booting_to_single_user (boot -s) is set.
2230Sstevel@tonic-gate  * This is because when doing a "boot -s", sulogin is started from specials.c
2240Sstevel@tonic-gate  * after milestone/single-user comes online, for backwards compatibility.
2250Sstevel@tonic-gate  * In this case, SCF_MILESTONE_SINGLE_USER needs to be part of up_svcs
2260Sstevel@tonic-gate  * to ensure sulogin will be spawned if milestone/single-user cannot be reached.
2270Sstevel@tonic-gate  */
2280Sstevel@tonic-gate static const char * const up_svcs[] = {
2290Sstevel@tonic-gate 	SCF_MILESTONE_SINGLE_USER,
2300Sstevel@tonic-gate 	CONSOLE_LOGIN_FMRI,
2310Sstevel@tonic-gate 	"svc:/system/install-setup:default",
2320Sstevel@tonic-gate 	"svc:/system/install:default",
2330Sstevel@tonic-gate 	NULL
2340Sstevel@tonic-gate };
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate /* This array must have an element for each non-NULL element of up_svcs[]. */
2370Sstevel@tonic-gate static graph_vertex_t *up_svcs_p[] = { NULL, NULL, NULL, NULL };
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate /* These are for seed repository magic.  See can_come_up(). */
240*11996SThomas.Whitten@Sun.COM static const char * const manifest_import = SCF_INSTANCE_MI;
2410Sstevel@tonic-gate static graph_vertex_t *manifest_import_p = NULL;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate static char target_milestone_as_runlevel(void);
2450Sstevel@tonic-gate static void graph_runlevel_changed(char rl, int online);
2460Sstevel@tonic-gate static int dgraph_set_milestone(const char *, scf_handle_t *, boolean_t);
2470Sstevel@tonic-gate static boolean_t should_be_in_subgraph(graph_vertex_t *v);
2487630SRenaud.Manus@Sun.COM static int mark_subtree(graph_edge_t *, void *);
2497630SRenaud.Manus@Sun.COM static boolean_t insubtree_dependents_down(graph_vertex_t *);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate /*
2520Sstevel@tonic-gate  * graph_vertex_compare()
2530Sstevel@tonic-gate  *	This function can compare either int *id or * graph_vertex_t *gv
2540Sstevel@tonic-gate  *	values, as the vertex id is always the first element of a
2550Sstevel@tonic-gate  *	graph_vertex structure.
2560Sstevel@tonic-gate  */
2570Sstevel@tonic-gate /* ARGSUSED */
2580Sstevel@tonic-gate static int
2590Sstevel@tonic-gate graph_vertex_compare(const void *lc_arg, const void *rc_arg, void *private)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	int lc_id = ((const graph_vertex_t *)lc_arg)->gv_id;
2620Sstevel@tonic-gate 	int rc_id = *(int *)rc_arg;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (lc_id > rc_id)
2650Sstevel@tonic-gate 		return (1);
2660Sstevel@tonic-gate 	if (lc_id < rc_id)
2670Sstevel@tonic-gate 		return (-1);
2680Sstevel@tonic-gate 	return (0);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate void
2720Sstevel@tonic-gate graph_init()
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate 	graph_edge_pool = startd_list_pool_create("graph_edges",
2750Sstevel@tonic-gate 	    sizeof (graph_edge_t), offsetof(graph_edge_t, ge_link), NULL,
2760Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG);
2770Sstevel@tonic-gate 	assert(graph_edge_pool != NULL);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	graph_vertex_pool = startd_list_pool_create("graph_vertices",
2800Sstevel@tonic-gate 	    sizeof (graph_vertex_t), offsetof(graph_vertex_t, gv_link),
2810Sstevel@tonic-gate 	    graph_vertex_compare, UU_LIST_POOL_DEBUG);
2820Sstevel@tonic-gate 	assert(graph_vertex_pool != NULL);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	(void) pthread_mutex_init(&dgraph_lock, &mutex_attrs);
2850Sstevel@tonic-gate 	(void) pthread_mutex_init(&single_user_thread_lock, &mutex_attrs);
2860Sstevel@tonic-gate 	dgraph = startd_list_create(graph_vertex_pool, NULL, UU_LIST_SORTED);
2870Sstevel@tonic-gate 	assert(dgraph != NULL);
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	if (!st->st_initial)
2900Sstevel@tonic-gate 		current_runlevel = utmpx_get_runlevel();
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Initialized graph\n");
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate static graph_vertex_t *
2960Sstevel@tonic-gate vertex_get_by_name(const char *name)
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate 	int id;
2990Sstevel@tonic-gate 
30011466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	id = dict_lookup_byname(name);
3030Sstevel@tonic-gate 	if (id == -1)
3040Sstevel@tonic-gate 		return (NULL);
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	return (uu_list_find(dgraph, &id, NULL, NULL));
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate static graph_vertex_t *
3100Sstevel@tonic-gate vertex_get_by_id(int id)
3110Sstevel@tonic-gate {
31211466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	if (id == -1)
3150Sstevel@tonic-gate 		return (NULL);
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	return (uu_list_find(dgraph, &id, NULL, NULL));
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate  * Creates a new vertex with the given name, adds it to the graph, and returns
3220Sstevel@tonic-gate  * a pointer to it.  The graph lock must be held by this thread on entry.
3230Sstevel@tonic-gate  */
3240Sstevel@tonic-gate static graph_vertex_t *
3250Sstevel@tonic-gate graph_add_vertex(const char *name)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate 	int id;
3280Sstevel@tonic-gate 	graph_vertex_t *v;
3290Sstevel@tonic-gate 	void *p;
3300Sstevel@tonic-gate 	uu_list_index_t idx;
3310Sstevel@tonic-gate 
33211466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	id = dict_insert(name);
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	v = startd_zalloc(sizeof (*v));
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	v->gv_id = id;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	v->gv_name = startd_alloc(strlen(name) + 1);
3410Sstevel@tonic-gate 	(void) strcpy(v->gv_name, name);
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	v->gv_dependencies = startd_list_create(graph_edge_pool, v, 0);
3440Sstevel@tonic-gate 	v->gv_dependents = startd_list_create(graph_edge_pool, v, 0);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	p = uu_list_find(dgraph, &id, NULL, &idx);
3470Sstevel@tonic-gate 	assert(p == NULL);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	uu_list_node_init(v, &v->gv_link, graph_vertex_pool);
3500Sstevel@tonic-gate 	uu_list_insert(dgraph, v, idx);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	return (v);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate /*
3560Sstevel@tonic-gate  * Removes v from the graph and frees it.  The graph should be locked by this
3570Sstevel@tonic-gate  * thread, and v should have no edges associated with it.
3580Sstevel@tonic-gate  */
3590Sstevel@tonic-gate static void
3600Sstevel@tonic-gate graph_remove_vertex(graph_vertex_t *v)
3610Sstevel@tonic-gate {
36211466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependencies) == 0);
3650Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependents) == 0);
3661712Srm88369 	assert(v->gv_refs == 0);
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	startd_free(v->gv_name, strlen(v->gv_name) + 1);
3690Sstevel@tonic-gate 	uu_list_destroy(v->gv_dependencies);
3700Sstevel@tonic-gate 	uu_list_destroy(v->gv_dependents);
3710Sstevel@tonic-gate 	uu_list_remove(dgraph, v);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	startd_free(v, sizeof (graph_vertex_t));
3740Sstevel@tonic-gate }
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate static void
3770Sstevel@tonic-gate graph_add_edge(graph_vertex_t *fv, graph_vertex_t *tv)
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate 	graph_edge_t *e, *re;
3800Sstevel@tonic-gate 	int r;
3810Sstevel@tonic-gate 
38211466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	e = startd_alloc(sizeof (graph_edge_t));
3850Sstevel@tonic-gate 	re = startd_alloc(sizeof (graph_edge_t));
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	e->ge_parent = fv;
3880Sstevel@tonic-gate 	e->ge_vertex = tv;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	re->ge_parent = tv;
3910Sstevel@tonic-gate 	re->ge_vertex = fv;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	uu_list_node_init(e, &e->ge_link, graph_edge_pool);
3940Sstevel@tonic-gate 	r = uu_list_insert_before(fv->gv_dependencies, NULL, e);
3950Sstevel@tonic-gate 	assert(r == 0);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	uu_list_node_init(re, &re->ge_link, graph_edge_pool);
3980Sstevel@tonic-gate 	r = uu_list_insert_before(tv->gv_dependents, NULL, re);
3990Sstevel@tonic-gate 	assert(r == 0);
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate static void
4030Sstevel@tonic-gate graph_remove_edge(graph_vertex_t *v, graph_vertex_t *dv)
4040Sstevel@tonic-gate {
4050Sstevel@tonic-gate 	graph_edge_t *e;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
4080Sstevel@tonic-gate 	    e != NULL;
4090Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
4100Sstevel@tonic-gate 		if (e->ge_vertex == dv) {
4110Sstevel@tonic-gate 			uu_list_remove(v->gv_dependencies, e);
4120Sstevel@tonic-gate 			startd_free(e, sizeof (graph_edge_t));
4130Sstevel@tonic-gate 			break;
4140Sstevel@tonic-gate 		}
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	for (e = uu_list_first(dv->gv_dependents);
4180Sstevel@tonic-gate 	    e != NULL;
4190Sstevel@tonic-gate 	    e = uu_list_next(dv->gv_dependents, e)) {
4200Sstevel@tonic-gate 		if (e->ge_vertex == v) {
4210Sstevel@tonic-gate 			uu_list_remove(dv->gv_dependents, e);
4220Sstevel@tonic-gate 			startd_free(e, sizeof (graph_edge_t));
4230Sstevel@tonic-gate 			break;
4240Sstevel@tonic-gate 		}
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate static void
4291712Srm88369 remove_inst_vertex(graph_vertex_t *v)
4301712Srm88369 {
4311712Srm88369 	graph_edge_t *e;
4321712Srm88369 	graph_vertex_t *sv;
4331712Srm88369 	int i;
4341712Srm88369 
43511466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
4361712Srm88369 	assert(uu_list_numnodes(v->gv_dependents) == 1);
4371712Srm88369 	assert(uu_list_numnodes(v->gv_dependencies) == 0);
4381712Srm88369 	assert(v->gv_refs == 0);
4391712Srm88369 	assert((v->gv_flags & GV_CONFIGURED) == 0);
4401712Srm88369 
4411712Srm88369 	e = uu_list_first(v->gv_dependents);
4421712Srm88369 	sv = e->ge_vertex;
4431712Srm88369 	graph_remove_edge(sv, v);
4441712Srm88369 
4451712Srm88369 	for (i = 0; up_svcs[i] != NULL; ++i) {
4461712Srm88369 		if (up_svcs_p[i] == v)
4471712Srm88369 			up_svcs_p[i] = NULL;
4481712Srm88369 	}
4491712Srm88369 
4501712Srm88369 	if (manifest_import_p == v)
4511712Srm88369 		manifest_import_p = NULL;
4521712Srm88369 
4531712Srm88369 	graph_remove_vertex(v);
4541712Srm88369 
4551712Srm88369 	if (uu_list_numnodes(sv->gv_dependencies) == 0 &&
4561712Srm88369 	    uu_list_numnodes(sv->gv_dependents) == 0 &&
4571712Srm88369 	    sv->gv_refs == 0)
4581712Srm88369 		graph_remove_vertex(sv);
4591712Srm88369 }
4601712Srm88369 
4611712Srm88369 static void
4620Sstevel@tonic-gate graph_walk_dependents(graph_vertex_t *v, void (*func)(graph_vertex_t *, void *),
4630Sstevel@tonic-gate     void *arg)
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate 	graph_edge_t *e;
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
4680Sstevel@tonic-gate 	    e != NULL;
4690Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e))
4700Sstevel@tonic-gate 		func(e->ge_vertex, arg);
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate static void
4740Sstevel@tonic-gate graph_walk_dependencies(graph_vertex_t *v, void (*func)(graph_vertex_t *,
4750Sstevel@tonic-gate 	void *), void *arg)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate 	graph_edge_t *e;
4780Sstevel@tonic-gate 
47911466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
4820Sstevel@tonic-gate 	    e != NULL;
4830Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 		func(e->ge_vertex, arg);
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate /*
4900Sstevel@tonic-gate  * Generic graph walking function.
4910Sstevel@tonic-gate  *
4920Sstevel@tonic-gate  * Given a vertex, this function will walk either dependencies
4930Sstevel@tonic-gate  * (WALK_DEPENDENCIES) or dependents (WALK_DEPENDENTS) of a vertex recursively
4940Sstevel@tonic-gate  * for the entire graph.  It will avoid cycles and never visit the same vertex
4950Sstevel@tonic-gate  * twice.
4960Sstevel@tonic-gate  *
4970Sstevel@tonic-gate  * We avoid traversing exclusion dependencies, because they are allowed to
4980Sstevel@tonic-gate  * create cycles in the graph.  When propagating satisfiability, there is no
4990Sstevel@tonic-gate  * need to walk exclusion dependencies because exclude_all_satisfied() doesn't
5000Sstevel@tonic-gate  * test for satisfiability.
5010Sstevel@tonic-gate  *
5020Sstevel@tonic-gate  * The walker takes two callbacks.  The first is called before examining the
5030Sstevel@tonic-gate  * dependents of each vertex.  The second is called on each vertex after
5040Sstevel@tonic-gate  * examining its dependents.  This allows is_path_to() to construct a path only
5050Sstevel@tonic-gate  * after the target vertex has been found.
5060Sstevel@tonic-gate  */
5070Sstevel@tonic-gate typedef enum {
5080Sstevel@tonic-gate 	WALK_DEPENDENTS,
5090Sstevel@tonic-gate 	WALK_DEPENDENCIES
5100Sstevel@tonic-gate } graph_walk_dir_t;
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate typedef int (*graph_walk_cb_t)(graph_vertex_t *, void *);
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate typedef struct graph_walk_info {
5150Sstevel@tonic-gate 	graph_walk_dir_t 	gi_dir;
5160Sstevel@tonic-gate 	uchar_t			*gi_visited;	/* vertex bitmap */
5170Sstevel@tonic-gate 	int			(*gi_pre)(graph_vertex_t *, void *);
5180Sstevel@tonic-gate 	void			(*gi_post)(graph_vertex_t *, void *);
5190Sstevel@tonic-gate 	void			*gi_arg;	/* callback arg */
5200Sstevel@tonic-gate 	int			gi_ret;		/* return value */
5210Sstevel@tonic-gate } graph_walk_info_t;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate static int
5240Sstevel@tonic-gate graph_walk_recurse(graph_edge_t *e, graph_walk_info_t *gip)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate 	uu_list_t *list;
5270Sstevel@tonic-gate 	int r;
5280Sstevel@tonic-gate 	graph_vertex_t *v = e->ge_vertex;
5290Sstevel@tonic-gate 	int i;
5300Sstevel@tonic-gate 	uint_t b;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	i = v->gv_id / 8;
5330Sstevel@tonic-gate 	b = 1 << (v->gv_id % 8);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	/*
5360Sstevel@tonic-gate 	 * Check to see if we've visited this vertex already.
5370Sstevel@tonic-gate 	 */
5380Sstevel@tonic-gate 	if (gip->gi_visited[i] & b)
5390Sstevel@tonic-gate 		return (UU_WALK_NEXT);
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	gip->gi_visited[i] |= b;
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	/*
5440Sstevel@tonic-gate 	 * Don't follow exclusions.
5450Sstevel@tonic-gate 	 */
5460Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
5470Sstevel@tonic-gate 		return (UU_WALK_NEXT);
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	/*
5500Sstevel@tonic-gate 	 * Call pre-visit callback.  If this doesn't terminate the walk,
5510Sstevel@tonic-gate 	 * continue search.
5520Sstevel@tonic-gate 	 */
5530Sstevel@tonic-gate 	if ((gip->gi_ret = gip->gi_pre(v, gip->gi_arg)) == UU_WALK_NEXT) {
5540Sstevel@tonic-gate 		/*
5550Sstevel@tonic-gate 		 * Recurse using appropriate list.
5560Sstevel@tonic-gate 		 */
5570Sstevel@tonic-gate 		if (gip->gi_dir == WALK_DEPENDENTS)
5580Sstevel@tonic-gate 			list = v->gv_dependents;
5590Sstevel@tonic-gate 		else
5600Sstevel@tonic-gate 			list = v->gv_dependencies;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 		r = uu_list_walk(list, (uu_walk_fn_t *)graph_walk_recurse,
5630Sstevel@tonic-gate 		    gip, 0);
5640Sstevel@tonic-gate 		assert(r == 0);
5650Sstevel@tonic-gate 	}
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	/*
5680Sstevel@tonic-gate 	 * Callbacks must return either UU_WALK_NEXT or UU_WALK_DONE.
5690Sstevel@tonic-gate 	 */
5700Sstevel@tonic-gate 	assert(gip->gi_ret == UU_WALK_NEXT || gip->gi_ret == UU_WALK_DONE);
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	/*
5730Sstevel@tonic-gate 	 * If given a post-callback, call the function for every vertex.
5740Sstevel@tonic-gate 	 */
5750Sstevel@tonic-gate 	if (gip->gi_post != NULL)
5760Sstevel@tonic-gate 		(void) gip->gi_post(v, gip->gi_arg);
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	/*
5790Sstevel@tonic-gate 	 * Preserve the callback's return value.  If the callback returns
5800Sstevel@tonic-gate 	 * UU_WALK_DONE, then we propagate that to the caller in order to
5810Sstevel@tonic-gate 	 * terminate the walk.
5820Sstevel@tonic-gate 	 */
5830Sstevel@tonic-gate 	return (gip->gi_ret);
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate static void
5870Sstevel@tonic-gate graph_walk(graph_vertex_t *v, graph_walk_dir_t dir,
5880Sstevel@tonic-gate     int (*pre)(graph_vertex_t *, void *),
5890Sstevel@tonic-gate     void (*post)(graph_vertex_t *, void *), void *arg)
5900Sstevel@tonic-gate {
5910Sstevel@tonic-gate 	graph_walk_info_t gi;
5920Sstevel@tonic-gate 	graph_edge_t fake;
5930Sstevel@tonic-gate 	size_t sz = dictionary->dict_new_id / 8 + 1;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	gi.gi_visited = startd_zalloc(sz);
5960Sstevel@tonic-gate 	gi.gi_pre = pre;
5970Sstevel@tonic-gate 	gi.gi_post = post;
5980Sstevel@tonic-gate 	gi.gi_arg = arg;
5990Sstevel@tonic-gate 	gi.gi_dir = dir;
6000Sstevel@tonic-gate 	gi.gi_ret = 0;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	/*
6030Sstevel@tonic-gate 	 * Fake up an edge for the first iteration
6040Sstevel@tonic-gate 	 */
6050Sstevel@tonic-gate 	fake.ge_vertex = v;
6060Sstevel@tonic-gate 	(void) graph_walk_recurse(&fake, &gi);
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	startd_free(gi.gi_visited, sz);
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate typedef struct child_search {
6120Sstevel@tonic-gate 	int	id;		/* id of vertex to look for */
6130Sstevel@tonic-gate 	uint_t	depth;		/* recursion depth */
6140Sstevel@tonic-gate 	/*
6150Sstevel@tonic-gate 	 * While the vertex is not found, path is NULL.  After the search, if
6160Sstevel@tonic-gate 	 * the vertex was found then path should point to a -1-terminated
6170Sstevel@tonic-gate 	 * array of vertex id's which constitute the path to the vertex.
6180Sstevel@tonic-gate 	 */
6190Sstevel@tonic-gate 	int	*path;
6200Sstevel@tonic-gate } child_search_t;
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate static int
6230Sstevel@tonic-gate child_pre(graph_vertex_t *v, void *arg)
6240Sstevel@tonic-gate {
6250Sstevel@tonic-gate 	child_search_t *cs = arg;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	cs->depth++;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	if (v->gv_id == cs->id) {
6300Sstevel@tonic-gate 		cs->path = startd_alloc((cs->depth + 1) * sizeof (int));
6310Sstevel@tonic-gate 		cs->path[cs->depth] = -1;
6320Sstevel@tonic-gate 		return (UU_WALK_DONE);
6330Sstevel@tonic-gate 	}
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	return (UU_WALK_NEXT);
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate static void
6390Sstevel@tonic-gate child_post(graph_vertex_t *v, void *arg)
6400Sstevel@tonic-gate {
6410Sstevel@tonic-gate 	child_search_t *cs = arg;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	cs->depth--;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	if (cs->path != NULL)
6460Sstevel@tonic-gate 		cs->path[cs->depth] = v->gv_id;
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate /*
6500Sstevel@tonic-gate  * Look for a path from from to to.  If one exists, returns a pointer to
6510Sstevel@tonic-gate  * a NULL-terminated array of pointers to the vertices along the path.  If
6520Sstevel@tonic-gate  * there is no path, returns NULL.
6530Sstevel@tonic-gate  */
6540Sstevel@tonic-gate static int *
6550Sstevel@tonic-gate is_path_to(graph_vertex_t *from, graph_vertex_t *to)
6560Sstevel@tonic-gate {
6570Sstevel@tonic-gate 	child_search_t cs;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	cs.id = to->gv_id;
6600Sstevel@tonic-gate 	cs.depth = 0;
6610Sstevel@tonic-gate 	cs.path = NULL;
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 	graph_walk(from, WALK_DEPENDENCIES, child_pre, child_post, &cs);
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	return (cs.path);
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate /*
6690Sstevel@tonic-gate  * Given an array of int's as returned by is_path_to, allocates a string of
6700Sstevel@tonic-gate  * their names joined by newlines.  Returns the size of the allocated buffer
6710Sstevel@tonic-gate  * in *sz and frees path.
6720Sstevel@tonic-gate  */
6730Sstevel@tonic-gate static void
6740Sstevel@tonic-gate path_to_str(int *path, char **cpp, size_t *sz)
6750Sstevel@tonic-gate {
6760Sstevel@tonic-gate 	int i;
6770Sstevel@tonic-gate 	graph_vertex_t *v;
6780Sstevel@tonic-gate 	size_t allocd, new_allocd;
6790Sstevel@tonic-gate 	char *new, *name;
6800Sstevel@tonic-gate 
68111466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
6820Sstevel@tonic-gate 	assert(path[0] != -1);
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	allocd = 1;
6850Sstevel@tonic-gate 	*cpp = startd_alloc(1);
6860Sstevel@tonic-gate 	(*cpp)[0] = '\0';
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	for (i = 0; path[i] != -1; ++i) {
6890Sstevel@tonic-gate 		name = NULL;
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		v = vertex_get_by_id(path[i]);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 		if (v == NULL)
6940Sstevel@tonic-gate 			name = "<deleted>";
6950Sstevel@tonic-gate 		else if (v->gv_type == GVT_INST || v->gv_type == GVT_SVC)
6960Sstevel@tonic-gate 			name = v->gv_name;
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 		if (name != NULL) {
6990Sstevel@tonic-gate 			new_allocd = allocd + strlen(name) + 1;
7000Sstevel@tonic-gate 			new = startd_alloc(new_allocd);
7010Sstevel@tonic-gate 			(void) strcpy(new, *cpp);
7020Sstevel@tonic-gate 			(void) strcat(new, name);
7030Sstevel@tonic-gate 			(void) strcat(new, "\n");
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 			startd_free(*cpp, allocd);
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 			*cpp = new;
7080Sstevel@tonic-gate 			allocd = new_allocd;
7090Sstevel@tonic-gate 		}
7100Sstevel@tonic-gate 	}
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	startd_free(path, sizeof (int) * (i + 1));
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	*sz = allocd;
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate /*
7190Sstevel@tonic-gate  * This function along with run_sulogin() implements an exclusion relationship
7200Sstevel@tonic-gate  * between system/console-login and sulogin.  run_sulogin() will fail if
7210Sstevel@tonic-gate  * system/console-login is online, and the graph engine should call
7220Sstevel@tonic-gate  * graph_clogin_start() to bring system/console-login online, which defers the
7230Sstevel@tonic-gate  * start if sulogin is running.
7240Sstevel@tonic-gate  */
7250Sstevel@tonic-gate static void
7260Sstevel@tonic-gate graph_clogin_start(graph_vertex_t *v)
7270Sstevel@tonic-gate {
72811466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	if (sulogin_running)
7310Sstevel@tonic-gate 		console_login_ready = B_TRUE;
7320Sstevel@tonic-gate 	else
7330Sstevel@tonic-gate 		vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate static void
7370Sstevel@tonic-gate graph_su_start(graph_vertex_t *v)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate 	/*
7400Sstevel@tonic-gate 	 * /etc/inittab used to have the initial /sbin/rcS as a 'sysinit'
7410Sstevel@tonic-gate 	 * entry with a runlevel of 'S', before jumping to the final
7420Sstevel@tonic-gate 	 * target runlevel (as set in initdefault).  We mimic that legacy
7430Sstevel@tonic-gate 	 * behavior here.
7440Sstevel@tonic-gate 	 */
7450Sstevel@tonic-gate 	utmpx_set_runlevel('S', '0', B_FALSE);
7460Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate static void
7500Sstevel@tonic-gate graph_post_su_online(void)
7510Sstevel@tonic-gate {
7520Sstevel@tonic-gate 	graph_runlevel_changed('S', 1);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate static void
7560Sstevel@tonic-gate graph_post_su_disable(void)
7570Sstevel@tonic-gate {
7580Sstevel@tonic-gate 	graph_runlevel_changed('S', 0);
7590Sstevel@tonic-gate }
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate static void
7620Sstevel@tonic-gate graph_post_mu_online(void)
7630Sstevel@tonic-gate {
7640Sstevel@tonic-gate 	graph_runlevel_changed('2', 1);
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate static void
7680Sstevel@tonic-gate graph_post_mu_disable(void)
7690Sstevel@tonic-gate {
7700Sstevel@tonic-gate 	graph_runlevel_changed('2', 0);
7710Sstevel@tonic-gate }
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate static void
7740Sstevel@tonic-gate graph_post_mus_online(void)
7750Sstevel@tonic-gate {
7760Sstevel@tonic-gate 	graph_runlevel_changed('3', 1);
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate static void
7800Sstevel@tonic-gate graph_post_mus_disable(void)
7810Sstevel@tonic-gate {
7820Sstevel@tonic-gate 	graph_runlevel_changed('3', 0);
7830Sstevel@tonic-gate }
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate static struct special_vertex_info {
7860Sstevel@tonic-gate 	const char	*name;
7870Sstevel@tonic-gate 	void		(*start_f)(graph_vertex_t *);
7880Sstevel@tonic-gate 	void		(*post_online_f)(void);
7890Sstevel@tonic-gate 	void		(*post_disable_f)(void);
7900Sstevel@tonic-gate } special_vertices[] = {
7910Sstevel@tonic-gate 	{ CONSOLE_LOGIN_FMRI, graph_clogin_start, NULL, NULL },
7920Sstevel@tonic-gate 	{ SCF_MILESTONE_SINGLE_USER, graph_su_start,
7930Sstevel@tonic-gate 	    graph_post_su_online, graph_post_su_disable },
7940Sstevel@tonic-gate 	{ SCF_MILESTONE_MULTI_USER, NULL,
7950Sstevel@tonic-gate 	    graph_post_mu_online, graph_post_mu_disable },
7960Sstevel@tonic-gate 	{ SCF_MILESTONE_MULTI_USER_SERVER, NULL,
7970Sstevel@tonic-gate 	    graph_post_mus_online, graph_post_mus_disable },
7980Sstevel@tonic-gate 	{ NULL },
7990Sstevel@tonic-gate };
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate void
8030Sstevel@tonic-gate vertex_send_event(graph_vertex_t *v, restarter_event_type_t e)
8040Sstevel@tonic-gate {
8050Sstevel@tonic-gate 	switch (e) {
8060Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADD_INSTANCE:
8070Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_UNINIT);
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 		MUTEX_LOCK(&st->st_load_lock);
8100Sstevel@tonic-gate 		st->st_load_instances++;
8110Sstevel@tonic-gate 		MUTEX_UNLOCK(&st->st_load_lock);
8120Sstevel@tonic-gate 		break;
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ENABLE:
8150Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Enabling %s.\n", v->gv_name);
8160Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_UNINIT ||
8170Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_DISABLED ||
8180Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_MAINT);
8190Sstevel@tonic-gate 		break;
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_DISABLE:
8220Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
8230Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Disabling %s.\n", v->gv_name);
8240Sstevel@tonic-gate 		assert(v->gv_state != RESTARTER_STATE_DISABLED);
8250Sstevel@tonic-gate 		break;
8260Sstevel@tonic-gate 
82711482SSean.Wilcox@Sun.COM 	case RESTARTER_EVENT_TYPE_STOP_RESET:
8280Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_STOP:
8290Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Stopping %s.\n", v->gv_name);
8300Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_DEGRADED ||
8310Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_ONLINE);
8320Sstevel@tonic-gate 		break;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_START:
8350Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Starting %s.\n", v->gv_name);
8360Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_OFFLINE);
8370Sstevel@tonic-gate 		break;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE:
8400Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_DEGRADED:
8410Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_REFRESH:
8420Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_RESTART:
8430Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF:
8440Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
8450Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE:
8460Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
8470Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
8480Sstevel@tonic-gate 		break;
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	default:
8510Sstevel@tonic-gate #ifndef NDEBUG
8520Sstevel@tonic-gate 		uu_warn("%s:%d: Bad event %d.\n", __FILE__, __LINE__, e);
8530Sstevel@tonic-gate #endif
8540Sstevel@tonic-gate 		abort();
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	restarter_protocol_send_event(v->gv_name, v->gv_restarter_channel, e);
8580Sstevel@tonic-gate }
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate static void
8610Sstevel@tonic-gate graph_unset_restarter(graph_vertex_t *v)
8620Sstevel@tonic-gate {
86311466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
8640Sstevel@tonic-gate 	assert(v->gv_flags & GV_CONFIGURED);
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_REMOVE_INSTANCE);
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	if (v->gv_restarter_id != -1) {
8690Sstevel@tonic-gate 		graph_vertex_t *rv;
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 		rv = vertex_get_by_id(v->gv_restarter_id);
8720Sstevel@tonic-gate 		graph_remove_edge(v, rv);
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	v->gv_restarter_id = -1;
8760Sstevel@tonic-gate 	v->gv_restarter_channel = NULL;
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate 
8791712Srm88369 /*
8801712Srm88369  * Return VERTEX_REMOVED when the vertex passed in argument is deleted from the
8811712Srm88369  * dgraph otherwise return VERTEX_INUSE.
8821712Srm88369  */
8831712Srm88369 static int
8841712Srm88369 free_if_unrefed(graph_vertex_t *v)
8851712Srm88369 {
88611466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
8871712Srm88369 
8881712Srm88369 	if (v->gv_refs > 0)
8891712Srm88369 		return (VERTEX_INUSE);
8901712Srm88369 
8911712Srm88369 	if (v->gv_type == GVT_SVC &&
8921712Srm88369 	    uu_list_numnodes(v->gv_dependents) == 0 &&
8931712Srm88369 	    uu_list_numnodes(v->gv_dependencies) == 0) {
8941712Srm88369 		graph_remove_vertex(v);
8951712Srm88369 		return (VERTEX_REMOVED);
8961712Srm88369 	} else if (v->gv_type == GVT_INST &&
8971712Srm88369 	    (v->gv_flags & GV_CONFIGURED) == 0 &&
8981712Srm88369 	    uu_list_numnodes(v->gv_dependents) == 1 &&
8991712Srm88369 	    uu_list_numnodes(v->gv_dependencies) == 0) {
9001712Srm88369 		remove_inst_vertex(v);
9011712Srm88369 		return (VERTEX_REMOVED);
9021712Srm88369 	}
9031712Srm88369 
9041712Srm88369 	return (VERTEX_INUSE);
9051712Srm88369 }
9061712Srm88369 
9070Sstevel@tonic-gate static void
9080Sstevel@tonic-gate delete_depgroup(graph_vertex_t *v)
9090Sstevel@tonic-gate {
9100Sstevel@tonic-gate 	graph_edge_t *e;
9110Sstevel@tonic-gate 	graph_vertex_t *dv;
9120Sstevel@tonic-gate 
91311466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
9140Sstevel@tonic-gate 	assert(v->gv_type == GVT_GROUP);
9150Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependents) == 0);
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	while ((e = uu_list_first(v->gv_dependencies)) != NULL) {
9180Sstevel@tonic-gate 		dv = e->ge_vertex;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 		graph_remove_edge(v, dv);
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 		switch (dv->gv_type) {
9230Sstevel@tonic-gate 		case GVT_INST:		/* instance dependency */
9240Sstevel@tonic-gate 		case GVT_SVC:		/* service dependency */
9251712Srm88369 			(void) free_if_unrefed(dv);
9260Sstevel@tonic-gate 			break;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 		case GVT_FILE:		/* file dependency */
9290Sstevel@tonic-gate 			assert(uu_list_numnodes(dv->gv_dependencies) == 0);
9300Sstevel@tonic-gate 			if (uu_list_numnodes(dv->gv_dependents) == 0)
9310Sstevel@tonic-gate 				graph_remove_vertex(dv);
9320Sstevel@tonic-gate 			break;
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 		default:
9350Sstevel@tonic-gate #ifndef NDEBUG
9360Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected node type %d", __FILE__,
9370Sstevel@tonic-gate 			    __LINE__, dv->gv_type);
9380Sstevel@tonic-gate #endif
9390Sstevel@tonic-gate 			abort();
9400Sstevel@tonic-gate 		}
9410Sstevel@tonic-gate 	}
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 	graph_remove_vertex(v);
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate static int
9470Sstevel@tonic-gate delete_instance_deps_cb(graph_edge_t *e, void **ptrs)
9480Sstevel@tonic-gate {
9490Sstevel@tonic-gate 	graph_vertex_t *v = ptrs[0];
9500Sstevel@tonic-gate 	boolean_t delete_restarter_dep = (boolean_t)ptrs[1];
9510Sstevel@tonic-gate 	graph_vertex_t *dv;
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	dv = e->ge_vertex;
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	/*
9560Sstevel@tonic-gate 	 * We have four possibilities here:
9570Sstevel@tonic-gate 	 *   - GVT_INST: restarter
9580Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_INST: instance dependency
9590Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_SVC - GV_INST: service dependency
9600Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_FILE: file dependency
9610Sstevel@tonic-gate 	 */
9620Sstevel@tonic-gate 	switch (dv->gv_type) {
9630Sstevel@tonic-gate 	case GVT_INST:	/* restarter */
9640Sstevel@tonic-gate 		assert(dv->gv_id == v->gv_restarter_id);
9650Sstevel@tonic-gate 		if (delete_restarter_dep)
9660Sstevel@tonic-gate 			graph_remove_edge(v, dv);
9670Sstevel@tonic-gate 		break;
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	case GVT_GROUP:	/* pg dependency */
9700Sstevel@tonic-gate 		graph_remove_edge(v, dv);
9710Sstevel@tonic-gate 		delete_depgroup(dv);
9720Sstevel@tonic-gate 		break;
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	case GVT_FILE:
9750Sstevel@tonic-gate 		/* These are currently not direct dependencies */
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	default:
9780Sstevel@tonic-gate #ifndef NDEBUG
9790Sstevel@tonic-gate 		uu_warn("%s:%d: Bad vertex type %d.\n", __FILE__, __LINE__,
9800Sstevel@tonic-gate 		    dv->gv_type);
9810Sstevel@tonic-gate #endif
9820Sstevel@tonic-gate 		abort();
9830Sstevel@tonic-gate 	}
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	return (UU_WALK_NEXT);
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate static void
9890Sstevel@tonic-gate delete_instance_dependencies(graph_vertex_t *v, boolean_t delete_restarter_dep)
9900Sstevel@tonic-gate {
9910Sstevel@tonic-gate 	void *ptrs[2];
9920Sstevel@tonic-gate 	int r;
9930Sstevel@tonic-gate 
99411466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
9950Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	ptrs[0] = v;
9980Sstevel@tonic-gate 	ptrs[1] = (void *)delete_restarter_dep;
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	r = uu_list_walk(v->gv_dependencies,
10010Sstevel@tonic-gate 	    (uu_walk_fn_t *)delete_instance_deps_cb, &ptrs, UU_WALK_ROBUST);
10020Sstevel@tonic-gate 	assert(r == 0);
10030Sstevel@tonic-gate }
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate /*
10060Sstevel@tonic-gate  * int graph_insert_vertex_unconfigured()
10070Sstevel@tonic-gate  *   Insert a vertex without sending any restarter events. If the vertex
10080Sstevel@tonic-gate  *   already exists or creation is successful, return a pointer to it in *vp.
10090Sstevel@tonic-gate  *
10100Sstevel@tonic-gate  *   If type is not GVT_GROUP, dt can remain unset.
10110Sstevel@tonic-gate  *
10120Sstevel@tonic-gate  *   Returns 0, EEXIST, or EINVAL if the arguments are invalid (i.e., fmri
10130Sstevel@tonic-gate  *   doesn't agree with type, or type doesn't agree with dt).
10140Sstevel@tonic-gate  */
10150Sstevel@tonic-gate static int
10160Sstevel@tonic-gate graph_insert_vertex_unconfigured(const char *fmri, gv_type_t type,
10170Sstevel@tonic-gate     depgroup_type_t dt, restarter_error_t rt, graph_vertex_t **vp)
10180Sstevel@tonic-gate {
10190Sstevel@tonic-gate 	int r;
10200Sstevel@tonic-gate 	int i;
10210Sstevel@tonic-gate 
102211466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 	switch (type) {
10250Sstevel@tonic-gate 	case GVT_SVC:
10260Sstevel@tonic-gate 	case GVT_INST:
10270Sstevel@tonic-gate 		if (strncmp(fmri, "svc:", sizeof ("svc:") - 1) != 0)
10280Sstevel@tonic-gate 			return (EINVAL);
10290Sstevel@tonic-gate 		break;
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	case GVT_FILE:
10320Sstevel@tonic-gate 		if (strncmp(fmri, "file:", sizeof ("file:") - 1) != 0)
10330Sstevel@tonic-gate 			return (EINVAL);
10340Sstevel@tonic-gate 		break;
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	case GVT_GROUP:
10370Sstevel@tonic-gate 		if (dt <= 0 || rt < 0)
10380Sstevel@tonic-gate 			return (EINVAL);
10390Sstevel@tonic-gate 		break;
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	default:
10420Sstevel@tonic-gate #ifndef NDEBUG
10430Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown type %d.\n", __FILE__, __LINE__, type);
10440Sstevel@tonic-gate #endif
10450Sstevel@tonic-gate 		abort();
10460Sstevel@tonic-gate 	}
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 	*vp = vertex_get_by_name(fmri);
10490Sstevel@tonic-gate 	if (*vp != NULL)
10500Sstevel@tonic-gate 		return (EEXIST);
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	*vp = graph_add_vertex(fmri);
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	(*vp)->gv_type = type;
10550Sstevel@tonic-gate 	(*vp)->gv_depgroup = dt;
10560Sstevel@tonic-gate 	(*vp)->gv_restart = rt;
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	(*vp)->gv_flags = 0;
10590Sstevel@tonic-gate 	(*vp)->gv_state = RESTARTER_STATE_NONE;
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	for (i = 0; special_vertices[i].name != NULL; ++i) {
10620Sstevel@tonic-gate 		if (strcmp(fmri, special_vertices[i].name) == 0) {
10630Sstevel@tonic-gate 			(*vp)->gv_start_f = special_vertices[i].start_f;
10640Sstevel@tonic-gate 			(*vp)->gv_post_online_f =
10650Sstevel@tonic-gate 			    special_vertices[i].post_online_f;
10660Sstevel@tonic-gate 			(*vp)->gv_post_disable_f =
10670Sstevel@tonic-gate 			    special_vertices[i].post_disable_f;
10680Sstevel@tonic-gate 			break;
10690Sstevel@tonic-gate 		}
10700Sstevel@tonic-gate 	}
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate 	(*vp)->gv_restarter_id = -1;
10730Sstevel@tonic-gate 	(*vp)->gv_restarter_channel = 0;
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	if (type == GVT_INST) {
10760Sstevel@tonic-gate 		char *sfmri;
10770Sstevel@tonic-gate 		graph_vertex_t *sv;
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 		sfmri = inst_fmri_to_svc_fmri(fmri);
10800Sstevel@tonic-gate 		sv = vertex_get_by_name(sfmri);
10810Sstevel@tonic-gate 		if (sv == NULL) {
10820Sstevel@tonic-gate 			r = graph_insert_vertex_unconfigured(sfmri, GVT_SVC, 0,
10830Sstevel@tonic-gate 			    0, &sv);
10840Sstevel@tonic-gate 			assert(r == 0);
10850Sstevel@tonic-gate 		}
10860Sstevel@tonic-gate 		startd_free(sfmri, max_scf_fmri_size);
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 		graph_add_edge(sv, *vp);
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	/*
10920Sstevel@tonic-gate 	 * If this vertex is in the subgraph, mark it as so, for both
10930Sstevel@tonic-gate 	 * GVT_INST and GVT_SERVICE verteces.
10940Sstevel@tonic-gate 	 * A GVT_SERVICE vertex can only be in the subgraph if another instance
10950Sstevel@tonic-gate 	 * depends on it, in which case it's already been added to the graph
10960Sstevel@tonic-gate 	 * and marked as in the subgraph (by refresh_vertex()).  If a
10970Sstevel@tonic-gate 	 * GVT_SERVICE vertex was freshly added (by the code above), it means
10980Sstevel@tonic-gate 	 * that it has no dependents, and cannot be in the subgraph.
10990Sstevel@tonic-gate 	 * Regardless of this, we still check that gv_flags includes
11000Sstevel@tonic-gate 	 * GV_INSUBGRAPH in the event that future behavior causes the above
11010Sstevel@tonic-gate 	 * code to add a GVT_SERVICE vertex which should be in the subgraph.
11020Sstevel@tonic-gate 	 */
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 	(*vp)->gv_flags |= (should_be_in_subgraph(*vp)? GV_INSUBGRAPH : 0);
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	return (0);
11070Sstevel@tonic-gate }
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate /*
11100Sstevel@tonic-gate  * Returns 0 on success or ELOOP if the dependency would create a cycle.
11110Sstevel@tonic-gate  */
11120Sstevel@tonic-gate static int
11130Sstevel@tonic-gate graph_insert_dependency(graph_vertex_t *fv, graph_vertex_t *tv, int **pathp)
11140Sstevel@tonic-gate {
11150Sstevel@tonic-gate 	hrtime_t now;
11160Sstevel@tonic-gate 
111711466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	/* cycle detection */
11200Sstevel@tonic-gate 	now = gethrtime();
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	/* Don't follow exclusions. */
11230Sstevel@tonic-gate 	if (!(fv->gv_type == GVT_GROUP &&
11240Sstevel@tonic-gate 	    fv->gv_depgroup == DEPGRP_EXCLUDE_ALL)) {
11250Sstevel@tonic-gate 		*pathp = is_path_to(tv, fv);
11260Sstevel@tonic-gate 		if (*pathp)
11270Sstevel@tonic-gate 			return (ELOOP);
11280Sstevel@tonic-gate 	}
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 	dep_cycle_ns += gethrtime() - now;
11310Sstevel@tonic-gate 	++dep_inserts;
11320Sstevel@tonic-gate 	now = gethrtime();
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	graph_add_edge(fv, tv);
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	dep_insert_ns += gethrtime() - now;
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	/* Check if the dependency adds the "to" vertex to the subgraph */
11390Sstevel@tonic-gate 	tv->gv_flags |= (should_be_in_subgraph(tv) ? GV_INSUBGRAPH : 0);
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	return (0);
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate static int
11450Sstevel@tonic-gate inst_running(graph_vertex_t *v)
11460Sstevel@tonic-gate {
11470Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 	if (v->gv_state == RESTARTER_STATE_ONLINE ||
11500Sstevel@tonic-gate 	    v->gv_state == RESTARTER_STATE_DEGRADED)
11510Sstevel@tonic-gate 		return (1);
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	return (0);
11540Sstevel@tonic-gate }
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate /*
11570Sstevel@tonic-gate  * The dependency evaluation functions return
11580Sstevel@tonic-gate  *   1 - dependency satisfied
11590Sstevel@tonic-gate  *   0 - dependency unsatisfied
11600Sstevel@tonic-gate  *   -1 - dependency unsatisfiable (without administrator intervention)
11610Sstevel@tonic-gate  *
11620Sstevel@tonic-gate  * The functions also take a boolean satbility argument.  When true, the
11630Sstevel@tonic-gate  * functions may recurse in order to determine satisfiability.
11640Sstevel@tonic-gate  */
11650Sstevel@tonic-gate static int require_any_satisfied(graph_vertex_t *, boolean_t);
11660Sstevel@tonic-gate static int dependency_satisfied(graph_vertex_t *, boolean_t);
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate /*
11690Sstevel@tonic-gate  * A require_all dependency is unsatisfied if any elements are unsatisfied.  It
11700Sstevel@tonic-gate  * is unsatisfiable if any elements are unsatisfiable.
11710Sstevel@tonic-gate  */
11720Sstevel@tonic-gate static int
11730Sstevel@tonic-gate require_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
11740Sstevel@tonic-gate {
11750Sstevel@tonic-gate 	graph_edge_t *edge;
11760Sstevel@tonic-gate 	int i;
11770Sstevel@tonic-gate 	boolean_t any_unsatisfied;
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	if (uu_list_numnodes(groupv->gv_dependencies) == 0)
11800Sstevel@tonic-gate 		return (1);
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	any_unsatisfied = B_FALSE;
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
11850Sstevel@tonic-gate 	    edge != NULL;
11860Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
11870Sstevel@tonic-gate 		i = dependency_satisfied(edge->ge_vertex, satbility);
11880Sstevel@tonic-gate 		if (i == 1)
11890Sstevel@tonic-gate 			continue;
11900Sstevel@tonic-gate 
11915680Srm88369 		log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
11920Sstevel@tonic-gate 		    "require_all(%s): %s is unsatisfi%s.\n", groupv->gv_name,
11930Sstevel@tonic-gate 		    edge->ge_vertex->gv_name, i == 0 ? "ed" : "able");
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 		if (!satbility)
11960Sstevel@tonic-gate 			return (0);
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 		if (i == -1)
11990Sstevel@tonic-gate 			return (-1);
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 		any_unsatisfied = B_TRUE;
12020Sstevel@tonic-gate 	}
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	return (any_unsatisfied ? 0 : 1);
12050Sstevel@tonic-gate }
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate /*
12080Sstevel@tonic-gate  * A require_any dependency is satisfied if any element is satisfied.  It is
12090Sstevel@tonic-gate  * satisfiable if any element is satisfiable.
12100Sstevel@tonic-gate  */
12110Sstevel@tonic-gate static int
12120Sstevel@tonic-gate require_any_satisfied(graph_vertex_t *groupv, boolean_t satbility)
12130Sstevel@tonic-gate {
12140Sstevel@tonic-gate 	graph_edge_t *edge;
12150Sstevel@tonic-gate 	int s;
12160Sstevel@tonic-gate 	boolean_t satisfiable;
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	if (uu_list_numnodes(groupv->gv_dependencies) == 0)
12190Sstevel@tonic-gate 		return (1);
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	satisfiable = B_FALSE;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
12240Sstevel@tonic-gate 	    edge != NULL;
12250Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
12260Sstevel@tonic-gate 		s = dependency_satisfied(edge->ge_vertex, satbility);
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 		if (s == 1)
12290Sstevel@tonic-gate 			return (1);
12300Sstevel@tonic-gate 
12315680Srm88369 		log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
12320Sstevel@tonic-gate 		    "require_any(%s): %s is unsatisfi%s.\n",
12330Sstevel@tonic-gate 		    groupv->gv_name, edge->ge_vertex->gv_name,
12340Sstevel@tonic-gate 		    s == 0 ? "ed" : "able");
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 		if (satbility && s == 0)
12370Sstevel@tonic-gate 			satisfiable = B_TRUE;
12380Sstevel@tonic-gate 	}
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	return (!satbility || satisfiable ? 0 : -1);
12410Sstevel@tonic-gate }
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate /*
12440Sstevel@tonic-gate  * An optional_all dependency only considers elements which are configured,
12450Sstevel@tonic-gate  * enabled, and not in maintenance.  If any are unsatisfied, then the dependency
12460Sstevel@tonic-gate  * is unsatisfied.
12470Sstevel@tonic-gate  *
12480Sstevel@tonic-gate  * Offline dependencies which are waiting for a dependency to come online are
12490Sstevel@tonic-gate  * unsatisfied.  Offline dependences which cannot possibly come online
12500Sstevel@tonic-gate  * (unsatisfiable) are always considered satisfied.
12510Sstevel@tonic-gate  */
12520Sstevel@tonic-gate static int
12530Sstevel@tonic-gate optional_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
12540Sstevel@tonic-gate {
12550Sstevel@tonic-gate 	graph_edge_t *edge;
12560Sstevel@tonic-gate 	graph_vertex_t *v;
12570Sstevel@tonic-gate 	boolean_t any_qualified;
12580Sstevel@tonic-gate 	boolean_t any_unsatisfied;
12590Sstevel@tonic-gate 	int i;
12600Sstevel@tonic-gate 
12610Sstevel@tonic-gate 	any_qualified = B_FALSE;
12620Sstevel@tonic-gate 	any_unsatisfied = B_FALSE;
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
12650Sstevel@tonic-gate 	    edge != NULL;
12660Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
12670Sstevel@tonic-gate 		v = edge->ge_vertex;
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 		switch (v->gv_type) {
12700Sstevel@tonic-gate 		case GVT_INST:
12710Sstevel@tonic-gate 			/* Skip missing or disabled instances */
12720Sstevel@tonic-gate 			if ((v->gv_flags & (GV_CONFIGURED | GV_ENABLED)) !=
12730Sstevel@tonic-gate 			    (GV_CONFIGURED | GV_ENABLED))
12740Sstevel@tonic-gate 				continue;
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 			if (v->gv_state == RESTARTER_STATE_MAINT)
12770Sstevel@tonic-gate 				continue;
12780Sstevel@tonic-gate 
12798354SRenaud.Manus@Sun.COM 			if (v->gv_flags & GV_TOOFFLINE)
12808354SRenaud.Manus@Sun.COM 				continue;
12818354SRenaud.Manus@Sun.COM 
12820Sstevel@tonic-gate 			any_qualified = B_TRUE;
12830Sstevel@tonic-gate 			if (v->gv_state == RESTARTER_STATE_OFFLINE) {
12840Sstevel@tonic-gate 				/*
12850Sstevel@tonic-gate 				 * For offline dependencies, treat unsatisfiable
12860Sstevel@tonic-gate 				 * as satisfied.
12870Sstevel@tonic-gate 				 */
12880Sstevel@tonic-gate 				i = dependency_satisfied(v, B_TRUE);
12890Sstevel@tonic-gate 				if (i == -1)
12900Sstevel@tonic-gate 					i = 1;
12910Sstevel@tonic-gate 			} else if (v->gv_state == RESTARTER_STATE_DISABLED) {
12920Sstevel@tonic-gate 				/*
12930Sstevel@tonic-gate 				 * The service is enabled, but hasn't
12940Sstevel@tonic-gate 				 * transitioned out of disabled yet.  Treat it
12950Sstevel@tonic-gate 				 * as unsatisfied (not unsatisfiable).
12960Sstevel@tonic-gate 				 */
12970Sstevel@tonic-gate 				i = 0;
12980Sstevel@tonic-gate 			} else {
12990Sstevel@tonic-gate 				i = dependency_satisfied(v, satbility);
13000Sstevel@tonic-gate 			}
13010Sstevel@tonic-gate 			break;
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 		case GVT_FILE:
13040Sstevel@tonic-gate 			any_qualified = B_TRUE;
13050Sstevel@tonic-gate 			i = dependency_satisfied(v, satbility);
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 			break;
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 		case GVT_SVC: {
13100Sstevel@tonic-gate 			boolean_t svc_any_qualified;
13110Sstevel@tonic-gate 			boolean_t svc_satisfied;
13120Sstevel@tonic-gate 			boolean_t svc_satisfiable;
13130Sstevel@tonic-gate 			graph_vertex_t *v2;
13140Sstevel@tonic-gate 			graph_edge_t *e2;
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 			svc_any_qualified = B_FALSE;
13170Sstevel@tonic-gate 			svc_satisfied = B_FALSE;
13180Sstevel@tonic-gate 			svc_satisfiable = B_FALSE;
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 			for (e2 = uu_list_first(v->gv_dependencies);
13210Sstevel@tonic-gate 			    e2 != NULL;
13220Sstevel@tonic-gate 			    e2 = uu_list_next(v->gv_dependencies, e2)) {
13230Sstevel@tonic-gate 				v2 = e2->ge_vertex;
13240Sstevel@tonic-gate 				assert(v2->gv_type == GVT_INST);
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 				if ((v2->gv_flags &
13270Sstevel@tonic-gate 				    (GV_CONFIGURED | GV_ENABLED)) !=
13280Sstevel@tonic-gate 				    (GV_CONFIGURED | GV_ENABLED))
13290Sstevel@tonic-gate 					continue;
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 				if (v2->gv_state == RESTARTER_STATE_MAINT)
13320Sstevel@tonic-gate 					continue;
13330Sstevel@tonic-gate 
13348354SRenaud.Manus@Sun.COM 				if (v2->gv_flags & GV_TOOFFLINE)
13358354SRenaud.Manus@Sun.COM 					continue;
13368354SRenaud.Manus@Sun.COM 
13370Sstevel@tonic-gate 				svc_any_qualified = B_TRUE;
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 				if (v2->gv_state == RESTARTER_STATE_OFFLINE) {
13400Sstevel@tonic-gate 					/*
13410Sstevel@tonic-gate 					 * For offline dependencies, treat
13420Sstevel@tonic-gate 					 * unsatisfiable as satisfied.
13430Sstevel@tonic-gate 					 */
13440Sstevel@tonic-gate 					i = dependency_satisfied(v2, B_TRUE);
13450Sstevel@tonic-gate 					if (i == -1)
13460Sstevel@tonic-gate 						i = 1;
13470Sstevel@tonic-gate 				} else if (v2->gv_state ==
13480Sstevel@tonic-gate 				    RESTARTER_STATE_DISABLED) {
13490Sstevel@tonic-gate 					i = 0;
13500Sstevel@tonic-gate 				} else {
13510Sstevel@tonic-gate 					i = dependency_satisfied(v2, satbility);
13520Sstevel@tonic-gate 				}
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 				if (i == 1) {
13550Sstevel@tonic-gate 					svc_satisfied = B_TRUE;
13560Sstevel@tonic-gate 					break;
13570Sstevel@tonic-gate 				}
13580Sstevel@tonic-gate 				if (i == 0)
13590Sstevel@tonic-gate 					svc_satisfiable = B_TRUE;
13600Sstevel@tonic-gate 			}
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 			if (!svc_any_qualified)
13630Sstevel@tonic-gate 				continue;
13640Sstevel@tonic-gate 			any_qualified = B_TRUE;
13650Sstevel@tonic-gate 			if (svc_satisfied) {
13660Sstevel@tonic-gate 				i = 1;
13670Sstevel@tonic-gate 			} else if (svc_satisfiable) {
13680Sstevel@tonic-gate 				i = 0;
13690Sstevel@tonic-gate 			} else {
13700Sstevel@tonic-gate 				i = -1;
13710Sstevel@tonic-gate 			}
13720Sstevel@tonic-gate 			break;
13730Sstevel@tonic-gate 		}
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 		case GVT_GROUP:
13760Sstevel@tonic-gate 		default:
13770Sstevel@tonic-gate #ifndef NDEBUG
13780Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
13790Sstevel@tonic-gate 			    __LINE__, v->gv_type);
13800Sstevel@tonic-gate #endif
13810Sstevel@tonic-gate 			abort();
13820Sstevel@tonic-gate 		}
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 		if (i == 1)
13850Sstevel@tonic-gate 			continue;
13860Sstevel@tonic-gate 
13875680Srm88369 		log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
13880Sstevel@tonic-gate 		    "optional_all(%s): %s is unsatisfi%s.\n", groupv->gv_name,
13890Sstevel@tonic-gate 		    v->gv_name, i == 0 ? "ed" : "able");
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 		if (!satbility)
13920Sstevel@tonic-gate 			return (0);
13930Sstevel@tonic-gate 		if (i == -1)
13940Sstevel@tonic-gate 			return (-1);
13950Sstevel@tonic-gate 		any_unsatisfied = B_TRUE;
13960Sstevel@tonic-gate 	}
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	if (!any_qualified)
13990Sstevel@tonic-gate 		return (1);
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	return (any_unsatisfied ? 0 : 1);
14020Sstevel@tonic-gate }
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate /*
14050Sstevel@tonic-gate  * An exclude_all dependency is unsatisfied if any non-service element is
14060Sstevel@tonic-gate  * satisfied or any service instance which is configured, enabled, and not in
14070Sstevel@tonic-gate  * maintenance is satisfied.  Usually when unsatisfied, it is also
14080Sstevel@tonic-gate  * unsatisfiable.
14090Sstevel@tonic-gate  */
14100Sstevel@tonic-gate #define	LOG_EXCLUDE(u, v)						\
14115680Srm88369 	log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,			\
14125680Srm88369 	    "exclude_all(%s): %s is satisfied.\n",			\
14130Sstevel@tonic-gate 	    (u)->gv_name, (v)->gv_name)
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate /* ARGSUSED */
14160Sstevel@tonic-gate static int
14170Sstevel@tonic-gate exclude_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
14180Sstevel@tonic-gate {
14190Sstevel@tonic-gate 	graph_edge_t *edge, *e2;
14200Sstevel@tonic-gate 	graph_vertex_t *v, *v2;
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
14230Sstevel@tonic-gate 	    edge != NULL;
14240Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
14250Sstevel@tonic-gate 		v = edge->ge_vertex;
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 		switch (v->gv_type) {
14280Sstevel@tonic-gate 		case GVT_INST:
14290Sstevel@tonic-gate 			if ((v->gv_flags & GV_CONFIGURED) == 0)
14300Sstevel@tonic-gate 				continue;
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 			switch (v->gv_state) {
14330Sstevel@tonic-gate 			case RESTARTER_STATE_ONLINE:
14340Sstevel@tonic-gate 			case RESTARTER_STATE_DEGRADED:
14350Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v);
14360Sstevel@tonic-gate 				return (v->gv_flags & GV_ENABLED ? -1 : 0);
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 			case RESTARTER_STATE_OFFLINE:
14390Sstevel@tonic-gate 			case RESTARTER_STATE_UNINIT:
14400Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v);
14410Sstevel@tonic-gate 				return (0);
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 			case RESTARTER_STATE_DISABLED:
14440Sstevel@tonic-gate 			case RESTARTER_STATE_MAINT:
14450Sstevel@tonic-gate 				continue;
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 			default:
14480Sstevel@tonic-gate #ifndef NDEBUG
14490Sstevel@tonic-gate 				uu_warn("%s:%d: Unexpected vertex state %d.\n",
14500Sstevel@tonic-gate 				    __FILE__, __LINE__, v->gv_state);
14510Sstevel@tonic-gate #endif
14520Sstevel@tonic-gate 				abort();
14530Sstevel@tonic-gate 			}
14540Sstevel@tonic-gate 			/* NOTREACHED */
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 		case GVT_SVC:
14570Sstevel@tonic-gate 			break;
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 		case GVT_FILE:
14600Sstevel@tonic-gate 			if (!file_ready(v))
14610Sstevel@tonic-gate 				continue;
14620Sstevel@tonic-gate 			LOG_EXCLUDE(groupv, v);
14630Sstevel@tonic-gate 			return (-1);
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 		case GVT_GROUP:
14660Sstevel@tonic-gate 		default:
14670Sstevel@tonic-gate #ifndef NDEBUG
14680Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
14690Sstevel@tonic-gate 			    __LINE__, v->gv_type);
14700Sstevel@tonic-gate #endif
14710Sstevel@tonic-gate 			abort();
14720Sstevel@tonic-gate 		}
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 		/* v represents a service */
14750Sstevel@tonic-gate 		if (uu_list_numnodes(v->gv_dependencies) == 0)
14760Sstevel@tonic-gate 			continue;
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 		for (e2 = uu_list_first(v->gv_dependencies);
14790Sstevel@tonic-gate 		    e2 != NULL;
14800Sstevel@tonic-gate 		    e2 = uu_list_next(v->gv_dependencies, e2)) {
14810Sstevel@tonic-gate 			v2 = e2->ge_vertex;
14820Sstevel@tonic-gate 			assert(v2->gv_type == GVT_INST);
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 			if ((v2->gv_flags & GV_CONFIGURED) == 0)
14850Sstevel@tonic-gate 				continue;
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 			switch (v2->gv_state) {
14880Sstevel@tonic-gate 			case RESTARTER_STATE_ONLINE:
14890Sstevel@tonic-gate 			case RESTARTER_STATE_DEGRADED:
14900Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v2);
14910Sstevel@tonic-gate 				return (v2->gv_flags & GV_ENABLED ? -1 : 0);
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 			case RESTARTER_STATE_OFFLINE:
14940Sstevel@tonic-gate 			case RESTARTER_STATE_UNINIT:
14950Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v2);
14960Sstevel@tonic-gate 				return (0);
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 			case RESTARTER_STATE_DISABLED:
14990Sstevel@tonic-gate 			case RESTARTER_STATE_MAINT:
15000Sstevel@tonic-gate 				continue;
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 			default:
15030Sstevel@tonic-gate #ifndef NDEBUG
15040Sstevel@tonic-gate 				uu_warn("%s:%d: Unexpected vertex type %d.\n",
15050Sstevel@tonic-gate 				    __FILE__, __LINE__, v2->gv_type);
15060Sstevel@tonic-gate #endif
15070Sstevel@tonic-gate 				abort();
15080Sstevel@tonic-gate 			}
15090Sstevel@tonic-gate 		}
15100Sstevel@tonic-gate 	}
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	return (1);
15130Sstevel@tonic-gate }
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate /*
15160Sstevel@tonic-gate  * int instance_satisfied()
15170Sstevel@tonic-gate  *   Determine if all the dependencies are satisfied for the supplied instance
15180Sstevel@tonic-gate  *   vertex. Return 1 if they are, 0 if they aren't, and -1 if they won't be
15190Sstevel@tonic-gate  *   without administrator intervention.
15200Sstevel@tonic-gate  */
15210Sstevel@tonic-gate static int
15220Sstevel@tonic-gate instance_satisfied(graph_vertex_t *v, boolean_t satbility)
15230Sstevel@tonic-gate {
15240Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
15250Sstevel@tonic-gate 	assert(!inst_running(v));
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	return (require_all_satisfied(v, satbility));
15280Sstevel@tonic-gate }
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate /*
15310Sstevel@tonic-gate  * Decide whether v can satisfy a dependency.  v can either be a child of
15320Sstevel@tonic-gate  * a group vertex, or of an instance vertex.
15330Sstevel@tonic-gate  */
15340Sstevel@tonic-gate static int
15350Sstevel@tonic-gate dependency_satisfied(graph_vertex_t *v, boolean_t satbility)
15360Sstevel@tonic-gate {
15370Sstevel@tonic-gate 	switch (v->gv_type) {
15380Sstevel@tonic-gate 	case GVT_INST:
15397475SPhilippe.Jung@Sun.COM 		if ((v->gv_flags & GV_CONFIGURED) == 0) {
15407475SPhilippe.Jung@Sun.COM 			if (v->gv_flags & GV_DEATHROW) {
15417475SPhilippe.Jung@Sun.COM 				/*
15427475SPhilippe.Jung@Sun.COM 				 * A dependency on an instance with GV_DEATHROW
15437475SPhilippe.Jung@Sun.COM 				 * flag is always considered as satisfied.
15447475SPhilippe.Jung@Sun.COM 				 */
15457475SPhilippe.Jung@Sun.COM 				return (1);
15467475SPhilippe.Jung@Sun.COM 			}
15470Sstevel@tonic-gate 			return (-1);
15487475SPhilippe.Jung@Sun.COM 		}
15490Sstevel@tonic-gate 
15508354SRenaud.Manus@Sun.COM 		/*
15518354SRenaud.Manus@Sun.COM 		 * Any vertex with the GV_TOOFFLINE flag set is guaranteed
15528354SRenaud.Manus@Sun.COM 		 * to have its dependencies unsatisfiable.
15538354SRenaud.Manus@Sun.COM 		 */
15548354SRenaud.Manus@Sun.COM 		if (v->gv_flags & GV_TOOFFLINE)
15558354SRenaud.Manus@Sun.COM 			return (-1);
15568354SRenaud.Manus@Sun.COM 
15570Sstevel@tonic-gate 		switch (v->gv_state) {
15580Sstevel@tonic-gate 		case RESTARTER_STATE_ONLINE:
15590Sstevel@tonic-gate 		case RESTARTER_STATE_DEGRADED:
15600Sstevel@tonic-gate 			return (1);
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 		case RESTARTER_STATE_OFFLINE:
15630Sstevel@tonic-gate 			if (!satbility)
15640Sstevel@tonic-gate 				return (0);
15650Sstevel@tonic-gate 			return (instance_satisfied(v, satbility) != -1 ?
15660Sstevel@tonic-gate 			    0 : -1);
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 		case RESTARTER_STATE_DISABLED:
15690Sstevel@tonic-gate 		case RESTARTER_STATE_MAINT:
15700Sstevel@tonic-gate 			return (-1);
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 		case RESTARTER_STATE_UNINIT:
15730Sstevel@tonic-gate 			return (0);
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 		default:
15760Sstevel@tonic-gate #ifndef NDEBUG
15770Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex state %d.\n",
15780Sstevel@tonic-gate 			    __FILE__, __LINE__, v->gv_state);
15790Sstevel@tonic-gate #endif
15800Sstevel@tonic-gate 			abort();
15810Sstevel@tonic-gate 			/* NOTREACHED */
15820Sstevel@tonic-gate 		}
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	case GVT_SVC:
15850Sstevel@tonic-gate 		if (uu_list_numnodes(v->gv_dependencies) == 0)
15860Sstevel@tonic-gate 			return (-1);
15870Sstevel@tonic-gate 		return (require_any_satisfied(v, satbility));
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	case GVT_FILE:
15900Sstevel@tonic-gate 		/* i.e., we assume files will not be automatically generated */
15910Sstevel@tonic-gate 		return (file_ready(v) ? 1 : -1);
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 	case GVT_GROUP:
15940Sstevel@tonic-gate 		break;
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	default:
15970Sstevel@tonic-gate #ifndef NDEBUG
15980Sstevel@tonic-gate 		uu_warn("%s:%d: Unexpected node type %d.\n", __FILE__, __LINE__,
15990Sstevel@tonic-gate 		    v->gv_type);
16000Sstevel@tonic-gate #endif
16010Sstevel@tonic-gate 		abort();
16020Sstevel@tonic-gate 		/* NOTREACHED */
16030Sstevel@tonic-gate 	}
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 	switch (v->gv_depgroup) {
16060Sstevel@tonic-gate 	case DEPGRP_REQUIRE_ANY:
16070Sstevel@tonic-gate 		return (require_any_satisfied(v, satbility));
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	case DEPGRP_REQUIRE_ALL:
16100Sstevel@tonic-gate 		return (require_all_satisfied(v, satbility));
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	case DEPGRP_OPTIONAL_ALL:
16130Sstevel@tonic-gate 		return (optional_all_satisfied(v, satbility));
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	case DEPGRP_EXCLUDE_ALL:
16160Sstevel@tonic-gate 		return (exclude_all_satisfied(v, satbility));
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	default:
16190Sstevel@tonic-gate #ifndef NDEBUG
16200Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown dependency grouping %d.\n", __FILE__,
16210Sstevel@tonic-gate 		    __LINE__, v->gv_depgroup);
16220Sstevel@tonic-gate #endif
16230Sstevel@tonic-gate 		abort();
16240Sstevel@tonic-gate 	}
16250Sstevel@tonic-gate }
16260Sstevel@tonic-gate 
16271958Slianep void
16281958Slianep graph_start_if_satisfied(graph_vertex_t *v)
16290Sstevel@tonic-gate {
16300Sstevel@tonic-gate 	if (v->gv_state == RESTARTER_STATE_OFFLINE &&
16310Sstevel@tonic-gate 	    instance_satisfied(v, B_FALSE) == 1) {
16320Sstevel@tonic-gate 		if (v->gv_start_f == NULL)
16330Sstevel@tonic-gate 			vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
16340Sstevel@tonic-gate 		else
16350Sstevel@tonic-gate 			v->gv_start_f(v);
16360Sstevel@tonic-gate 	}
16370Sstevel@tonic-gate }
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate /*
16400Sstevel@tonic-gate  * propagate_satbility()
16410Sstevel@tonic-gate  *
16420Sstevel@tonic-gate  * This function is used when the given vertex changes state in such a way that
16430Sstevel@tonic-gate  * one of its dependents may become unsatisfiable.  This happens when an
16440Sstevel@tonic-gate  * instance transitions between offline -> online, or from !running ->
16450Sstevel@tonic-gate  * maintenance, as well as when an instance is removed from the graph.
16460Sstevel@tonic-gate  *
16472339Slianep  * We have to walk all the dependents, since optional_all dependencies several
16480Sstevel@tonic-gate  * levels up could become (un)satisfied, instead of unsatisfiable.  For example,
16490Sstevel@tonic-gate  *
16500Sstevel@tonic-gate  *	+-----+  optional_all  +-----+  require_all  +-----+
16510Sstevel@tonic-gate  *	|  A  |--------------->|  B  |-------------->|  C  |
16520Sstevel@tonic-gate  *	+-----+                +-----+               +-----+
16530Sstevel@tonic-gate  *
16540Sstevel@tonic-gate  *	                                        offline -> maintenance
16550Sstevel@tonic-gate  *
16560Sstevel@tonic-gate  * If C goes into maintenance, it's not enough simply to check B.  Because A has
16570Sstevel@tonic-gate  * an optional dependency, what was previously an unsatisfiable situation is now
16580Sstevel@tonic-gate  * satisfied (B will never come online, even though its state hasn't changed).
16590Sstevel@tonic-gate  *
16600Sstevel@tonic-gate  * Note that it's not necessary to continue examining dependents after reaching
16610Sstevel@tonic-gate  * an optional_all dependency.  It's not possible for an optional_all dependency
16620Sstevel@tonic-gate  * to change satisfiability without also coming online, in which case we get a
16630Sstevel@tonic-gate  * start event and propagation continues naturally.  However, it does no harm to
16640Sstevel@tonic-gate  * continue propagating satisfiability (as it is a relatively rare event), and
16650Sstevel@tonic-gate  * keeps the walker code simple and generic.
16660Sstevel@tonic-gate  */
16670Sstevel@tonic-gate /*ARGSUSED*/
16680Sstevel@tonic-gate static int
16690Sstevel@tonic-gate satbility_cb(graph_vertex_t *v, void *arg)
16700Sstevel@tonic-gate {
16710Sstevel@tonic-gate 	if (v->gv_type == GVT_INST)
16721958Slianep 		graph_start_if_satisfied(v);
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	return (UU_WALK_NEXT);
16750Sstevel@tonic-gate }
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate static void
16780Sstevel@tonic-gate propagate_satbility(graph_vertex_t *v)
16790Sstevel@tonic-gate {
16800Sstevel@tonic-gate 	graph_walk(v, WALK_DEPENDENTS, satbility_cb, NULL, NULL);
16810Sstevel@tonic-gate }
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate static void propagate_stop(graph_vertex_t *, void *);
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate /* ARGSUSED */
16860Sstevel@tonic-gate static void
16870Sstevel@tonic-gate propagate_start(graph_vertex_t *v, void *arg)
16880Sstevel@tonic-gate {
16890Sstevel@tonic-gate 	switch (v->gv_type) {
16900Sstevel@tonic-gate 	case GVT_INST:
16911958Slianep 		graph_start_if_satisfied(v);
16920Sstevel@tonic-gate 		break;
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 	case GVT_GROUP:
16950Sstevel@tonic-gate 		if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
16960Sstevel@tonic-gate 			graph_walk_dependents(v, propagate_stop,
16970Sstevel@tonic-gate 			    (void *)RERR_RESTART);
16980Sstevel@tonic-gate 			break;
16990Sstevel@tonic-gate 		}
17000Sstevel@tonic-gate 		/* FALLTHROUGH */
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	case GVT_SVC:
17030Sstevel@tonic-gate 		graph_walk_dependents(v, propagate_start, NULL);
17040Sstevel@tonic-gate 		break;
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 	case GVT_FILE:
17070Sstevel@tonic-gate #ifndef NDEBUG
17080Sstevel@tonic-gate 		uu_warn("%s:%d: propagate_start() encountered GVT_FILE.\n",
17090Sstevel@tonic-gate 		    __FILE__, __LINE__);
17100Sstevel@tonic-gate #endif
17110Sstevel@tonic-gate 		abort();
17120Sstevel@tonic-gate 		/* NOTREACHED */
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	default:
17150Sstevel@tonic-gate #ifndef NDEBUG
17160Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__,
17170Sstevel@tonic-gate 		    v->gv_type);
17180Sstevel@tonic-gate #endif
17190Sstevel@tonic-gate 		abort();
17200Sstevel@tonic-gate 	}
17210Sstevel@tonic-gate }
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate static void
17240Sstevel@tonic-gate propagate_stop(graph_vertex_t *v, void *arg)
17250Sstevel@tonic-gate {
17260Sstevel@tonic-gate 	graph_edge_t *e;
17270Sstevel@tonic-gate 	graph_vertex_t *svc;
17280Sstevel@tonic-gate 	restarter_error_t err = (restarter_error_t)arg;
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate 	switch (v->gv_type) {
17310Sstevel@tonic-gate 	case GVT_INST:
17320Sstevel@tonic-gate 		/* Restarter */
173311482SSean.Wilcox@Sun.COM 		if (err > RERR_NONE && inst_running(v)) {
173411623SSean.Wilcox@Sun.COM 			if (err == RERR_RESTART || err == RERR_REFRESH) {
173511482SSean.Wilcox@Sun.COM 				vertex_send_event(v,
173611482SSean.Wilcox@Sun.COM 				    RESTARTER_EVENT_TYPE_STOP_RESET);
173711482SSean.Wilcox@Sun.COM 			} else {
173811482SSean.Wilcox@Sun.COM 				vertex_send_event(v, RESTARTER_EVENT_TYPE_STOP);
173911482SSean.Wilcox@Sun.COM 			}
174011482SSean.Wilcox@Sun.COM 		}
17410Sstevel@tonic-gate 		break;
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 	case GVT_SVC:
17440Sstevel@tonic-gate 		graph_walk_dependents(v, propagate_stop, arg);
17450Sstevel@tonic-gate 		break;
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 	case GVT_FILE:
17480Sstevel@tonic-gate #ifndef NDEBUG
17490Sstevel@tonic-gate 		uu_warn("%s:%d: propagate_stop() encountered GVT_FILE.\n",
17500Sstevel@tonic-gate 		    __FILE__, __LINE__);
17510Sstevel@tonic-gate #endif
17520Sstevel@tonic-gate 		abort();
17530Sstevel@tonic-gate 		/* NOTREACHED */
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	case GVT_GROUP:
17560Sstevel@tonic-gate 		if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
17570Sstevel@tonic-gate 			graph_walk_dependents(v, propagate_start, NULL);
17580Sstevel@tonic-gate 			break;
17590Sstevel@tonic-gate 		}
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 		if (err == RERR_NONE || err > v->gv_restart)
17620Sstevel@tonic-gate 			break;
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate 		assert(uu_list_numnodes(v->gv_dependents) == 1);
17650Sstevel@tonic-gate 		e = uu_list_first(v->gv_dependents);
17660Sstevel@tonic-gate 		svc = e->ge_vertex;
17670Sstevel@tonic-gate 
176811482SSean.Wilcox@Sun.COM 		if (inst_running(svc)) {
176911623SSean.Wilcox@Sun.COM 			if (err == RERR_RESTART || err == RERR_REFRESH) {
177011482SSean.Wilcox@Sun.COM 				vertex_send_event(svc,
177111482SSean.Wilcox@Sun.COM 				    RESTARTER_EVENT_TYPE_STOP_RESET);
177211482SSean.Wilcox@Sun.COM 			} else {
177311482SSean.Wilcox@Sun.COM 				vertex_send_event(svc,
177411482SSean.Wilcox@Sun.COM 				    RESTARTER_EVENT_TYPE_STOP);
177511482SSean.Wilcox@Sun.COM 			}
177611482SSean.Wilcox@Sun.COM 		}
17770Sstevel@tonic-gate 		break;
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 	default:
17800Sstevel@tonic-gate #ifndef NDEBUG
17810Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__,
17820Sstevel@tonic-gate 		    v->gv_type);
17830Sstevel@tonic-gate #endif
17840Sstevel@tonic-gate 		abort();
17850Sstevel@tonic-gate 	}
17860Sstevel@tonic-gate }
17870Sstevel@tonic-gate 
17889333SRenaud.Manus@Sun.COM void
17897630SRenaud.Manus@Sun.COM offline_vertex(graph_vertex_t *v)
17907630SRenaud.Manus@Sun.COM {
17917630SRenaud.Manus@Sun.COM 	scf_handle_t *h = libscf_handle_create_bound_loop();
17927630SRenaud.Manus@Sun.COM 	scf_instance_t *scf_inst = safe_scf_instance_create(h);
17937630SRenaud.Manus@Sun.COM 	scf_propertygroup_t *pg = safe_scf_pg_create(h);
17947630SRenaud.Manus@Sun.COM 	restarter_instance_state_t state, next_state;
17957630SRenaud.Manus@Sun.COM 	int r;
17967630SRenaud.Manus@Sun.COM 
17977630SRenaud.Manus@Sun.COM 	assert(v->gv_type == GVT_INST);
17987630SRenaud.Manus@Sun.COM 
17997630SRenaud.Manus@Sun.COM 	if (scf_inst == NULL)
18007630SRenaud.Manus@Sun.COM 		bad_error("safe_scf_instance_create", scf_error());
18017630SRenaud.Manus@Sun.COM 	if (pg == NULL)
18027630SRenaud.Manus@Sun.COM 		bad_error("safe_scf_pg_create", scf_error());
18037630SRenaud.Manus@Sun.COM 
18047630SRenaud.Manus@Sun.COM 	/* if the vertex is already going offline, return */
18057630SRenaud.Manus@Sun.COM rep_retry:
18067630SRenaud.Manus@Sun.COM 	if (scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, scf_inst, NULL,
18077630SRenaud.Manus@Sun.COM 	    NULL, SCF_DECODE_FMRI_EXACT) != 0) {
18087630SRenaud.Manus@Sun.COM 		switch (scf_error()) {
18097630SRenaud.Manus@Sun.COM 		case SCF_ERROR_CONNECTION_BROKEN:
18107630SRenaud.Manus@Sun.COM 			libscf_handle_rebind(h);
18117630SRenaud.Manus@Sun.COM 			goto rep_retry;
18127630SRenaud.Manus@Sun.COM 
18137630SRenaud.Manus@Sun.COM 		case SCF_ERROR_NOT_FOUND:
18147630SRenaud.Manus@Sun.COM 			scf_pg_destroy(pg);
18157630SRenaud.Manus@Sun.COM 			scf_instance_destroy(scf_inst);
18167630SRenaud.Manus@Sun.COM 			(void) scf_handle_unbind(h);
18177630SRenaud.Manus@Sun.COM 			scf_handle_destroy(h);
18187630SRenaud.Manus@Sun.COM 			return;
18197630SRenaud.Manus@Sun.COM 		}
18207630SRenaud.Manus@Sun.COM 		uu_die("Can't decode FMRI %s: %s\n", v->gv_name,
18217630SRenaud.Manus@Sun.COM 		    scf_strerror(scf_error()));
18227630SRenaud.Manus@Sun.COM 	}
18237630SRenaud.Manus@Sun.COM 
18247630SRenaud.Manus@Sun.COM 	r = scf_instance_get_pg(scf_inst, SCF_PG_RESTARTER, pg);
18257630SRenaud.Manus@Sun.COM 	if (r != 0) {
18267630SRenaud.Manus@Sun.COM 		switch (scf_error()) {
18277630SRenaud.Manus@Sun.COM 		case SCF_ERROR_CONNECTION_BROKEN:
18287630SRenaud.Manus@Sun.COM 			libscf_handle_rebind(h);
18297630SRenaud.Manus@Sun.COM 			goto rep_retry;
18307630SRenaud.Manus@Sun.COM 
18317630SRenaud.Manus@Sun.COM 		case SCF_ERROR_NOT_SET:
18327630SRenaud.Manus@Sun.COM 		case SCF_ERROR_NOT_FOUND:
18337630SRenaud.Manus@Sun.COM 			scf_pg_destroy(pg);
18347630SRenaud.Manus@Sun.COM 			scf_instance_destroy(scf_inst);
18357630SRenaud.Manus@Sun.COM 			(void) scf_handle_unbind(h);
18367630SRenaud.Manus@Sun.COM 			scf_handle_destroy(h);
18377630SRenaud.Manus@Sun.COM 			return;
18387630SRenaud.Manus@Sun.COM 
18397630SRenaud.Manus@Sun.COM 		default:
18407630SRenaud.Manus@Sun.COM 			bad_error("scf_instance_get_pg", scf_error());
18417630SRenaud.Manus@Sun.COM 		}
18427630SRenaud.Manus@Sun.COM 	} else {
18437630SRenaud.Manus@Sun.COM 		r = libscf_read_states(pg, &state, &next_state);
18447630SRenaud.Manus@Sun.COM 		if (r == 0 && (next_state == RESTARTER_STATE_OFFLINE ||
18457630SRenaud.Manus@Sun.COM 		    next_state == RESTARTER_STATE_DISABLED)) {
18467630SRenaud.Manus@Sun.COM 			log_framework(LOG_DEBUG,
18477630SRenaud.Manus@Sun.COM 			    "%s: instance is already going down.\n",
18487630SRenaud.Manus@Sun.COM 			    v->gv_name);
18497630SRenaud.Manus@Sun.COM 			scf_pg_destroy(pg);
18507630SRenaud.Manus@Sun.COM 			scf_instance_destroy(scf_inst);
18517630SRenaud.Manus@Sun.COM 			(void) scf_handle_unbind(h);
18527630SRenaud.Manus@Sun.COM 			scf_handle_destroy(h);
18537630SRenaud.Manus@Sun.COM 			return;
18547630SRenaud.Manus@Sun.COM 		}
18557630SRenaud.Manus@Sun.COM 	}
18567630SRenaud.Manus@Sun.COM 
18577630SRenaud.Manus@Sun.COM 	scf_pg_destroy(pg);
18587630SRenaud.Manus@Sun.COM 	scf_instance_destroy(scf_inst);
18597630SRenaud.Manus@Sun.COM 	(void) scf_handle_unbind(h);
18607630SRenaud.Manus@Sun.COM 	scf_handle_destroy(h);
18617815SRenaud.Manus@Sun.COM 
186211482SSean.Wilcox@Sun.COM 	vertex_send_event(v, RESTARTER_EVENT_TYPE_STOP_RESET);
18637630SRenaud.Manus@Sun.COM }
18647630SRenaud.Manus@Sun.COM 
18650Sstevel@tonic-gate /*
18660Sstevel@tonic-gate  * void graph_enable_by_vertex()
18670Sstevel@tonic-gate  *   If admin is non-zero, this is an administrative request for change
18680Sstevel@tonic-gate  *   of the enabled property.  Thus, send the ADMIN_DISABLE rather than
18690Sstevel@tonic-gate  *   a plain DISABLE restarter event.
18700Sstevel@tonic-gate  */
18711958Slianep void
18720Sstevel@tonic-gate graph_enable_by_vertex(graph_vertex_t *vertex, int enable, int admin)
18730Sstevel@tonic-gate {
18747630SRenaud.Manus@Sun.COM 	graph_vertex_t *v;
18757630SRenaud.Manus@Sun.COM 	int r;
18767630SRenaud.Manus@Sun.COM 
187711466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
18780Sstevel@tonic-gate 	assert((vertex->gv_flags & GV_CONFIGURED));
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 	vertex->gv_flags = (vertex->gv_flags & ~GV_ENABLED) |
18810Sstevel@tonic-gate 	    (enable ? GV_ENABLED : 0);
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 	if (enable) {
18840Sstevel@tonic-gate 		if (vertex->gv_state != RESTARTER_STATE_OFFLINE &&
18850Sstevel@tonic-gate 		    vertex->gv_state != RESTARTER_STATE_DEGRADED &&
18867630SRenaud.Manus@Sun.COM 		    vertex->gv_state != RESTARTER_STATE_ONLINE) {
18877630SRenaud.Manus@Sun.COM 			/*
18887630SRenaud.Manus@Sun.COM 			 * In case the vertex was notified to go down,
18897630SRenaud.Manus@Sun.COM 			 * but now can return online, clear the _TOOFFLINE
18907630SRenaud.Manus@Sun.COM 			 * and _TODISABLE flags.
18917630SRenaud.Manus@Sun.COM 			 */
18927630SRenaud.Manus@Sun.COM 			vertex->gv_flags &= ~GV_TOOFFLINE;
18937630SRenaud.Manus@Sun.COM 			vertex->gv_flags &= ~GV_TODISABLE;
18947630SRenaud.Manus@Sun.COM 
18950Sstevel@tonic-gate 			vertex_send_event(vertex, RESTARTER_EVENT_TYPE_ENABLE);
18960Sstevel@tonic-gate 		}
18977630SRenaud.Manus@Sun.COM 
18987630SRenaud.Manus@Sun.COM 		/*
18997630SRenaud.Manus@Sun.COM 		 * Wait for state update from restarter before sending _START or
19007630SRenaud.Manus@Sun.COM 		 * _STOP.
19017630SRenaud.Manus@Sun.COM 		 */
19027630SRenaud.Manus@Sun.COM 
19037630SRenaud.Manus@Sun.COM 		return;
19047630SRenaud.Manus@Sun.COM 	}
19057630SRenaud.Manus@Sun.COM 
19067630SRenaud.Manus@Sun.COM 	if (vertex->gv_state == RESTARTER_STATE_DISABLED)
19077630SRenaud.Manus@Sun.COM 		return;
19087630SRenaud.Manus@Sun.COM 
19097630SRenaud.Manus@Sun.COM 	if (!admin) {
19107630SRenaud.Manus@Sun.COM 		vertex_send_event(vertex, RESTARTER_EVENT_TYPE_DISABLE);
19117630SRenaud.Manus@Sun.COM 
19127630SRenaud.Manus@Sun.COM 		/*
19137630SRenaud.Manus@Sun.COM 		 * Wait for state update from restarter before sending _START or
19147630SRenaud.Manus@Sun.COM 		 * _STOP.
19157630SRenaud.Manus@Sun.COM 		 */
19167630SRenaud.Manus@Sun.COM 
19177630SRenaud.Manus@Sun.COM 		return;
19180Sstevel@tonic-gate 	}
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 	/*
19217630SRenaud.Manus@Sun.COM 	 * If it is a DISABLE event requested by the administrator then we are
19227630SRenaud.Manus@Sun.COM 	 * offlining the dependents first.
19237630SRenaud.Manus@Sun.COM 	 */
19247630SRenaud.Manus@Sun.COM 
19257630SRenaud.Manus@Sun.COM 	/*
19267630SRenaud.Manus@Sun.COM 	 * Set GV_TOOFFLINE for the services we are offlining. We cannot
19277630SRenaud.Manus@Sun.COM 	 * clear the GV_TOOFFLINE bits from all the services because
19287630SRenaud.Manus@Sun.COM 	 * other DISABLE events might be handled at the same time.
19290Sstevel@tonic-gate 	 */
19307630SRenaud.Manus@Sun.COM 	vertex->gv_flags |= GV_TOOFFLINE;
19317630SRenaud.Manus@Sun.COM 
19327630SRenaud.Manus@Sun.COM 	/* remember which vertex to disable... */
19337630SRenaud.Manus@Sun.COM 	vertex->gv_flags |= GV_TODISABLE;
19347630SRenaud.Manus@Sun.COM 
19358354SRenaud.Manus@Sun.COM 	log_framework(LOG_DEBUG, "Marking in-subtree vertices before "
19368354SRenaud.Manus@Sun.COM 	    "disabling %s.\n", vertex->gv_name);
19378354SRenaud.Manus@Sun.COM 
19387630SRenaud.Manus@Sun.COM 	/* set GV_TOOFFLINE for its dependents */
19397630SRenaud.Manus@Sun.COM 	r = uu_list_walk(vertex->gv_dependents, (uu_walk_fn_t *)mark_subtree,
19407630SRenaud.Manus@Sun.COM 	    NULL, 0);
19417630SRenaud.Manus@Sun.COM 	assert(r == 0);
19427630SRenaud.Manus@Sun.COM 
19437630SRenaud.Manus@Sun.COM 	/* disable the instance now if there is nothing else to offline */
19447630SRenaud.Manus@Sun.COM 	if (insubtree_dependents_down(vertex) == B_TRUE) {
19457630SRenaud.Manus@Sun.COM 		vertex_send_event(vertex, RESTARTER_EVENT_TYPE_ADMIN_DISABLE);
19467630SRenaud.Manus@Sun.COM 		return;
19477630SRenaud.Manus@Sun.COM 	}
19487630SRenaud.Manus@Sun.COM 
19497630SRenaud.Manus@Sun.COM 	/*
19507630SRenaud.Manus@Sun.COM 	 * This loop is similar to the one used for the graph reversal shutdown
19517630SRenaud.Manus@Sun.COM 	 * and could be improved in term of performance for the subtree reversal
19527630SRenaud.Manus@Sun.COM 	 * disable case.
19537630SRenaud.Manus@Sun.COM 	 */
19547630SRenaud.Manus@Sun.COM 	for (v = uu_list_first(dgraph); v != NULL;
19557630SRenaud.Manus@Sun.COM 	    v = uu_list_next(dgraph, v)) {
19567630SRenaud.Manus@Sun.COM 		/* skip the vertex we are disabling for now */
19577630SRenaud.Manus@Sun.COM 		if (v == vertex)
19587630SRenaud.Manus@Sun.COM 			continue;
19597630SRenaud.Manus@Sun.COM 
19607630SRenaud.Manus@Sun.COM 		if (v->gv_type != GVT_INST ||
19617630SRenaud.Manus@Sun.COM 		    (v->gv_flags & GV_CONFIGURED) == 0 ||
19627630SRenaud.Manus@Sun.COM 		    (v->gv_flags & GV_ENABLED) == 0 ||
19637630SRenaud.Manus@Sun.COM 		    (v->gv_flags & GV_TOOFFLINE) == 0)
19647630SRenaud.Manus@Sun.COM 			continue;
19657630SRenaud.Manus@Sun.COM 
19667630SRenaud.Manus@Sun.COM 		if ((v->gv_state != RESTARTER_STATE_ONLINE) &&
19677630SRenaud.Manus@Sun.COM 		    (v->gv_state != RESTARTER_STATE_DEGRADED)) {
19687630SRenaud.Manus@Sun.COM 			/* continue if there is nothing to offline */
19697630SRenaud.Manus@Sun.COM 			continue;
19707630SRenaud.Manus@Sun.COM 		}
19717630SRenaud.Manus@Sun.COM 
19727630SRenaud.Manus@Sun.COM 		/*
19737630SRenaud.Manus@Sun.COM 		 * Instances which are up need to come down before we're
19747630SRenaud.Manus@Sun.COM 		 * done, but we can only offline the leaves here. An
19757630SRenaud.Manus@Sun.COM 		 * instance is a leaf when all its dependents are down.
19767630SRenaud.Manus@Sun.COM 		 */
19778354SRenaud.Manus@Sun.COM 		if (insubtree_dependents_down(v) == B_TRUE) {
19788354SRenaud.Manus@Sun.COM 			log_framework(LOG_DEBUG, "Offlining in-subtree "
19798354SRenaud.Manus@Sun.COM 			    "instance %s for %s.\n",
19808354SRenaud.Manus@Sun.COM 			    v->gv_name, vertex->gv_name);
19817630SRenaud.Manus@Sun.COM 			offline_vertex(v);
19828354SRenaud.Manus@Sun.COM 		}
19837630SRenaud.Manus@Sun.COM 	}
19840Sstevel@tonic-gate }
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate static int configure_vertex(graph_vertex_t *, scf_instance_t *);
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate /*
19890Sstevel@tonic-gate  * Set the restarter for v to fmri_arg.  That is, make sure a vertex for
19900Sstevel@tonic-gate  * fmri_arg exists, make v depend on it, and send _ADD_INSTANCE for v.  If
19910Sstevel@tonic-gate  * v is already configured and fmri_arg indicates the current restarter, do
19920Sstevel@tonic-gate  * nothing.  If v is configured and fmri_arg is a new restarter, delete v's
19930Sstevel@tonic-gate  * dependency on the restarter, send _REMOVE_INSTANCE for v, and set the new
19940Sstevel@tonic-gate  * restarter.  Returns 0 on success, EINVAL if the FMRI is invalid,
19950Sstevel@tonic-gate  * ECONNABORTED if the repository connection is broken, and ELOOP
19960Sstevel@tonic-gate  * if the dependency would create a cycle.  In the last case, *pathp will
19970Sstevel@tonic-gate  * point to a -1-terminated array of ids which compose the path from v to
19980Sstevel@tonic-gate  * restarter_fmri.
19990Sstevel@tonic-gate  */
20000Sstevel@tonic-gate int
20010Sstevel@tonic-gate graph_change_restarter(graph_vertex_t *v, const char *fmri_arg, scf_handle_t *h,
20020Sstevel@tonic-gate     int **pathp)
20030Sstevel@tonic-gate {
20040Sstevel@tonic-gate 	char *restarter_fmri = NULL;
20050Sstevel@tonic-gate 	graph_vertex_t *rv;
20060Sstevel@tonic-gate 	int err;
20070Sstevel@tonic-gate 	int id;
20080Sstevel@tonic-gate 
200911466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 	if (fmri_arg[0] != '\0') {
20120Sstevel@tonic-gate 		err = fmri_canonify(fmri_arg, &restarter_fmri, B_TRUE);
20130Sstevel@tonic-gate 		if (err != 0) {
20140Sstevel@tonic-gate 			assert(err == EINVAL);
20150Sstevel@tonic-gate 			return (err);
20160Sstevel@tonic-gate 		}
20170Sstevel@tonic-gate 	}
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 	if (restarter_fmri == NULL ||
20200Sstevel@tonic-gate 	    strcmp(restarter_fmri, SCF_SERVICE_STARTD) == 0) {
20210Sstevel@tonic-gate 		if (v->gv_flags & GV_CONFIGURED) {
20220Sstevel@tonic-gate 			if (v->gv_restarter_id == -1) {
20230Sstevel@tonic-gate 				if (restarter_fmri != NULL)
20240Sstevel@tonic-gate 					startd_free(restarter_fmri,
20250Sstevel@tonic-gate 					    max_scf_fmri_size);
20260Sstevel@tonic-gate 				return (0);
20270Sstevel@tonic-gate 			}
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 			graph_unset_restarter(v);
20300Sstevel@tonic-gate 		}
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate 		/* Master restarter, nothing to do. */
20330Sstevel@tonic-gate 		v->gv_restarter_id = -1;
20340Sstevel@tonic-gate 		v->gv_restarter_channel = NULL;
20350Sstevel@tonic-gate 		vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE);
20360Sstevel@tonic-gate 		return (0);
20370Sstevel@tonic-gate 	}
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 	if (v->gv_flags & GV_CONFIGURED) {
20400Sstevel@tonic-gate 		id = dict_lookup_byname(restarter_fmri);
20410Sstevel@tonic-gate 		if (id != -1 && v->gv_restarter_id == id) {
20420Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_fmri_size);
20430Sstevel@tonic-gate 			return (0);
20440Sstevel@tonic-gate 		}
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate 		graph_unset_restarter(v);
20470Sstevel@tonic-gate 	}
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate 	err = graph_insert_vertex_unconfigured(restarter_fmri, GVT_INST, 0,
20500Sstevel@tonic-gate 	    RERR_NONE, &rv);
20510Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_fmri_size);
20520Sstevel@tonic-gate 	assert(err == 0 || err == EEXIST);
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate 	if (rv->gv_delegate_initialized == 0) {
20559263SSean.Wilcox@Sun.COM 		if ((rv->gv_delegate_channel = restarter_protocol_init_delegate(
20569263SSean.Wilcox@Sun.COM 		    rv->gv_name)) == NULL)
20579263SSean.Wilcox@Sun.COM 			return (EINVAL);
20580Sstevel@tonic-gate 		rv->gv_delegate_initialized = 1;
20590Sstevel@tonic-gate 	}
20600Sstevel@tonic-gate 	v->gv_restarter_id = rv->gv_id;
20610Sstevel@tonic-gate 	v->gv_restarter_channel = rv->gv_delegate_channel;
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 	err = graph_insert_dependency(v, rv, pathp);
20640Sstevel@tonic-gate 	if (err != 0) {
20650Sstevel@tonic-gate 		assert(err == ELOOP);
20660Sstevel@tonic-gate 		return (ELOOP);
20670Sstevel@tonic-gate 	}
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE);
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	if (!(rv->gv_flags & GV_CONFIGURED)) {
20720Sstevel@tonic-gate 		scf_instance_t *inst;
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 		err = libscf_fmri_get_instance(h, rv->gv_name, &inst);
20750Sstevel@tonic-gate 		switch (err) {
20760Sstevel@tonic-gate 		case 0:
20770Sstevel@tonic-gate 			err = configure_vertex(rv, inst);
20780Sstevel@tonic-gate 			scf_instance_destroy(inst);
20790Sstevel@tonic-gate 			switch (err) {
20800Sstevel@tonic-gate 			case 0:
20810Sstevel@tonic-gate 			case ECANCELED:
20820Sstevel@tonic-gate 				break;
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 			case ECONNABORTED:
20850Sstevel@tonic-gate 				return (ECONNABORTED);
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 			default:
20880Sstevel@tonic-gate 				bad_error("configure_vertex", err);
20890Sstevel@tonic-gate 			}
20900Sstevel@tonic-gate 			break;
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 		case ECONNABORTED:
20930Sstevel@tonic-gate 			return (ECONNABORTED);
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate 		case ENOENT:
20960Sstevel@tonic-gate 			break;
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 		case ENOTSUP:
20990Sstevel@tonic-gate 			/*
21000Sstevel@tonic-gate 			 * The fmri doesn't specify an instance - translate
21010Sstevel@tonic-gate 			 * to EINVAL.
21020Sstevel@tonic-gate 			 */
21030Sstevel@tonic-gate 			return (EINVAL);
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 		case EINVAL:
21060Sstevel@tonic-gate 		default:
21070Sstevel@tonic-gate 			bad_error("libscf_fmri_get_instance", err);
21080Sstevel@tonic-gate 		}
21090Sstevel@tonic-gate 	}
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate 	return (0);
21120Sstevel@tonic-gate }
21130Sstevel@tonic-gate 
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate /*
21160Sstevel@tonic-gate  * Add all of the instances of the service named by fmri to the graph.
21170Sstevel@tonic-gate  * Returns
21180Sstevel@tonic-gate  *   0 - success
21190Sstevel@tonic-gate  *   ENOENT - service indicated by fmri does not exist
21200Sstevel@tonic-gate  *
21210Sstevel@tonic-gate  * In both cases *reboundp will be B_TRUE if the handle was rebound, or B_FALSE
21220Sstevel@tonic-gate  * otherwise.
21230Sstevel@tonic-gate  */
21240Sstevel@tonic-gate static int
21250Sstevel@tonic-gate add_service(const char *fmri, scf_handle_t *h, boolean_t *reboundp)
21260Sstevel@tonic-gate {
21270Sstevel@tonic-gate 	scf_service_t *svc;
21280Sstevel@tonic-gate 	scf_instance_t *inst;
21290Sstevel@tonic-gate 	scf_iter_t *iter;
21300Sstevel@tonic-gate 	char *inst_fmri;
21310Sstevel@tonic-gate 	int ret, r;
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 	*reboundp = B_FALSE;
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 	svc = safe_scf_service_create(h);
21360Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
21370Sstevel@tonic-gate 	iter = safe_scf_iter_create(h);
21380Sstevel@tonic-gate 	inst_fmri = startd_alloc(max_scf_fmri_size);
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate rebound:
21410Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
21420Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) != 0) {
21430Sstevel@tonic-gate 		switch (scf_error()) {
21440Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
21450Sstevel@tonic-gate 		default:
21460Sstevel@tonic-gate 			libscf_handle_rebind(h);
21470Sstevel@tonic-gate 			*reboundp = B_TRUE;
21480Sstevel@tonic-gate 			goto rebound;
21490Sstevel@tonic-gate 
21500Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
21510Sstevel@tonic-gate 			ret = ENOENT;
21520Sstevel@tonic-gate 			goto out;
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
21550Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
21560Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
21570Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
21580Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
21590Sstevel@tonic-gate 		}
21600Sstevel@tonic-gate 	}
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate 	if (scf_iter_service_instances(iter, svc) != 0) {
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:
21775040Swesolows 			bad_error("scf_iter_service_instances", scf_error());
21780Sstevel@tonic-gate 		}
21790Sstevel@tonic-gate 	}
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 	for (;;) {
21820Sstevel@tonic-gate 		r = scf_iter_next_instance(iter, inst);
21830Sstevel@tonic-gate 		if (r == 0)
21840Sstevel@tonic-gate 			break;
21850Sstevel@tonic-gate 		if (r != 1) {
21860Sstevel@tonic-gate 			switch (scf_error()) {
21870Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
21880Sstevel@tonic-gate 			default:
21890Sstevel@tonic-gate 				libscf_handle_rebind(h);
21900Sstevel@tonic-gate 				*reboundp = B_TRUE;
21910Sstevel@tonic-gate 				goto rebound;
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
21940Sstevel@tonic-gate 				ret = ENOENT;
21950Sstevel@tonic-gate 				goto out;
21960Sstevel@tonic-gate 
21970Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
21980Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
21990Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
22000Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
22010Sstevel@tonic-gate 				bad_error("scf_iter_next_instance",
22020Sstevel@tonic-gate 				    scf_error());
22030Sstevel@tonic-gate 			}
22040Sstevel@tonic-gate 		}
22050Sstevel@tonic-gate 
22060Sstevel@tonic-gate 		if (scf_instance_to_fmri(inst, inst_fmri, max_scf_fmri_size) <
22070Sstevel@tonic-gate 		    0) {
22080Sstevel@tonic-gate 			switch (scf_error()) {
22090Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
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 SCF_ERROR_DELETED:
22150Sstevel@tonic-gate 				continue;
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
22180Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
22190Sstevel@tonic-gate 				bad_error("scf_instance_to_fmri", scf_error());
22200Sstevel@tonic-gate 			}
22210Sstevel@tonic-gate 		}
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 		r = dgraph_add_instance(inst_fmri, inst, B_FALSE);
22240Sstevel@tonic-gate 		switch (r) {
22250Sstevel@tonic-gate 		case 0:
22260Sstevel@tonic-gate 		case ECANCELED:
22270Sstevel@tonic-gate 			break;
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate 		case EEXIST:
22300Sstevel@tonic-gate 			continue;
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 		case ECONNABORTED:
22330Sstevel@tonic-gate 			libscf_handle_rebind(h);
22340Sstevel@tonic-gate 			*reboundp = B_TRUE;
22350Sstevel@tonic-gate 			goto rebound;
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 		case EINVAL:
22380Sstevel@tonic-gate 		default:
22390Sstevel@tonic-gate 			bad_error("dgraph_add_instance", r);
22400Sstevel@tonic-gate 		}
22410Sstevel@tonic-gate 	}
22420Sstevel@tonic-gate 
22430Sstevel@tonic-gate 	ret = 0;
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate out:
22460Sstevel@tonic-gate 	startd_free(inst_fmri, max_scf_fmri_size);
22470Sstevel@tonic-gate 	scf_iter_destroy(iter);
22480Sstevel@tonic-gate 	scf_instance_destroy(inst);
22490Sstevel@tonic-gate 	scf_service_destroy(svc);
22500Sstevel@tonic-gate 	return (ret);
22510Sstevel@tonic-gate }
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate struct depfmri_info {
22540Sstevel@tonic-gate 	graph_vertex_t	*v;		/* GVT_GROUP vertex */
22550Sstevel@tonic-gate 	gv_type_t	type;		/* type of dependency */
22560Sstevel@tonic-gate 	const char	*inst_fmri;	/* FMRI of parental GVT_INST vert. */
22570Sstevel@tonic-gate 	const char	*pg_name;	/* Name of dependency pg */
22580Sstevel@tonic-gate 	scf_handle_t	*h;
22590Sstevel@tonic-gate 	int		err;		/* return error code */
22600Sstevel@tonic-gate 	int		**pathp;	/* return circular dependency path */
22610Sstevel@tonic-gate };
22620Sstevel@tonic-gate 
22630Sstevel@tonic-gate /*
22640Sstevel@tonic-gate  * Find or create a vertex for fmri and make info->v depend on it.
22650Sstevel@tonic-gate  * Returns
22660Sstevel@tonic-gate  *   0 - success
22670Sstevel@tonic-gate  *   nonzero - failure
22680Sstevel@tonic-gate  *
22690Sstevel@tonic-gate  * On failure, sets info->err to
22700Sstevel@tonic-gate  *   EINVAL - fmri is invalid
22710Sstevel@tonic-gate  *	      fmri does not match info->type
22720Sstevel@tonic-gate  *   ELOOP - Adding the dependency creates a circular dependency.  *info->pathp
22730Sstevel@tonic-gate  *	     will point to an array of the ids of the members of the cycle.
22740Sstevel@tonic-gate  *   ECONNABORTED - repository connection was broken
22750Sstevel@tonic-gate  *   ECONNRESET - succeeded, but repository connection was reset
22760Sstevel@tonic-gate  */
22770Sstevel@tonic-gate static int
22780Sstevel@tonic-gate process_dependency_fmri(const char *fmri, struct depfmri_info *info)
22790Sstevel@tonic-gate {
22800Sstevel@tonic-gate 	int err;
22810Sstevel@tonic-gate 	graph_vertex_t *depgroup_v, *v;
22820Sstevel@tonic-gate 	char *fmri_copy, *cfmri;
22830Sstevel@tonic-gate 	size_t fmri_copy_sz;
22840Sstevel@tonic-gate 	const char *scope, *service, *instance, *pg;
22850Sstevel@tonic-gate 	scf_instance_t *inst;
22860Sstevel@tonic-gate 	boolean_t rebound;
22870Sstevel@tonic-gate 
228811466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 	/* Get or create vertex for FMRI */
22910Sstevel@tonic-gate 	depgroup_v = info->v;
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate 	if (strncmp(fmri, "file:", sizeof ("file:") - 1) == 0) {
22940Sstevel@tonic-gate 		if (info->type != GVT_FILE) {
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 		err = graph_insert_vertex_unconfigured(fmri, info->type, 0,
23030Sstevel@tonic-gate 		    RERR_NONE, &v);
23040Sstevel@tonic-gate 		switch (err) {
23050Sstevel@tonic-gate 		case 0:
23060Sstevel@tonic-gate 			break;
23070Sstevel@tonic-gate 
23080Sstevel@tonic-gate 		case EEXIST:
23090Sstevel@tonic-gate 			assert(v->gv_type == GVT_FILE);
23100Sstevel@tonic-gate 			break;
23110Sstevel@tonic-gate 
23120Sstevel@tonic-gate 		case EINVAL:		/* prevented above */
23130Sstevel@tonic-gate 		default:
23140Sstevel@tonic-gate 			bad_error("graph_insert_vertex_unconfigured", err);
23150Sstevel@tonic-gate 		}
23160Sstevel@tonic-gate 	} else {
23170Sstevel@tonic-gate 		if (info->type != GVT_INST) {
23180Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
23190Sstevel@tonic-gate 			    "FMRI \"%s\" is not allowed for the \"%s\" "
23200Sstevel@tonic-gate 			    "dependency's type of instance %s.\n", fmri,
23210Sstevel@tonic-gate 			    info->pg_name, info->inst_fmri);
23220Sstevel@tonic-gate 			return (info->err = EINVAL);
23230Sstevel@tonic-gate 		}
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate 		/*
23260Sstevel@tonic-gate 		 * We must canonify fmri & add a vertex for it.
23270Sstevel@tonic-gate 		 */
23280Sstevel@tonic-gate 		fmri_copy_sz = strlen(fmri) + 1;
23290Sstevel@tonic-gate 		fmri_copy = startd_alloc(fmri_copy_sz);
23300Sstevel@tonic-gate 		(void) strcpy(fmri_copy, fmri);
23310Sstevel@tonic-gate 
23320Sstevel@tonic-gate 		/* Determine if the FMRI is a property group or instance */
23330Sstevel@tonic-gate 		if (scf_parse_svc_fmri(fmri_copy, &scope, &service,
23340Sstevel@tonic-gate 		    &instance, &pg, NULL) != 0) {
23350Sstevel@tonic-gate 			startd_free(fmri_copy, fmri_copy_sz);
23360Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
23370Sstevel@tonic-gate 			    "Dependency \"%s\" of %s has invalid FMRI "
23380Sstevel@tonic-gate 			    "\"%s\".\n", info->pg_name, info->inst_fmri,
23390Sstevel@tonic-gate 			    fmri);
23400Sstevel@tonic-gate 			return (info->err = EINVAL);
23410Sstevel@tonic-gate 		}
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 		if (service == NULL || pg != NULL) {
23440Sstevel@tonic-gate 			startd_free(fmri_copy, fmri_copy_sz);
23450Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
23460Sstevel@tonic-gate 			    "Dependency \"%s\" of %s does not designate a "
23470Sstevel@tonic-gate 			    "service or instance.\n", info->pg_name,
23480Sstevel@tonic-gate 			    info->inst_fmri);
23490Sstevel@tonic-gate 			return (info->err = EINVAL);
23500Sstevel@tonic-gate 		}
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate 		if (scope == NULL || strcmp(scope, SCF_SCOPE_LOCAL) == 0) {
23530Sstevel@tonic-gate 			cfmri = uu_msprintf("svc:/%s%s%s",
23540Sstevel@tonic-gate 			    service, instance ? ":" : "", instance ? instance :
23550Sstevel@tonic-gate 			    "");
23560Sstevel@tonic-gate 		} else {
23570Sstevel@tonic-gate 			cfmri = uu_msprintf("svc://%s/%s%s%s",
23580Sstevel@tonic-gate 			    scope, service, instance ? ":" : "", instance ?
23590Sstevel@tonic-gate 			    instance : "");
23600Sstevel@tonic-gate 		}
23610Sstevel@tonic-gate 
23620Sstevel@tonic-gate 		startd_free(fmri_copy, fmri_copy_sz);
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(cfmri, instance ?
23650Sstevel@tonic-gate 		    GVT_INST : GVT_SVC, instance ? 0 : DEPGRP_REQUIRE_ANY,
23660Sstevel@tonic-gate 		    RERR_NONE, &v);
23670Sstevel@tonic-gate 		uu_free(cfmri);
23680Sstevel@tonic-gate 		switch (err) {
23690Sstevel@tonic-gate 		case 0:
23700Sstevel@tonic-gate 			break;
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate 		case EEXIST:
23730Sstevel@tonic-gate 			/* Verify v. */
23740Sstevel@tonic-gate 			if (instance != NULL)
23750Sstevel@tonic-gate 				assert(v->gv_type == GVT_INST);
23760Sstevel@tonic-gate 			else
23770Sstevel@tonic-gate 				assert(v->gv_type == GVT_SVC);
23780Sstevel@tonic-gate 			break;
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 		default:
23810Sstevel@tonic-gate 			bad_error("graph_insert_vertex_unconfigured", err);
23820Sstevel@tonic-gate 		}
23830Sstevel@tonic-gate 	}
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate 	/* Add dependency from depgroup_v to new vertex */
23860Sstevel@tonic-gate 	info->err = graph_insert_dependency(depgroup_v, v, info->pathp);
23870Sstevel@tonic-gate 	switch (info->err) {
23880Sstevel@tonic-gate 	case 0:
23890Sstevel@tonic-gate 		break;
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 	case ELOOP:
23920Sstevel@tonic-gate 		return (ELOOP);
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	default:
23950Sstevel@tonic-gate 		bad_error("graph_insert_dependency", info->err);
23960Sstevel@tonic-gate 	}
23970Sstevel@tonic-gate 
23980Sstevel@tonic-gate 	/* This must be after we insert the dependency, to avoid looping. */
23990Sstevel@tonic-gate 	switch (v->gv_type) {
24000Sstevel@tonic-gate 	case GVT_INST:
24010Sstevel@tonic-gate 		if ((v->gv_flags & GV_CONFIGURED) != 0)
24020Sstevel@tonic-gate 			break;
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate 		inst = safe_scf_instance_create(info->h);
24050Sstevel@tonic-gate 
24060Sstevel@tonic-gate 		rebound = B_FALSE;
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate rebound:
24090Sstevel@tonic-gate 		err = libscf_lookup_instance(v->gv_name, inst);
24100Sstevel@tonic-gate 		switch (err) {
24110Sstevel@tonic-gate 		case 0:
24120Sstevel@tonic-gate 			err = configure_vertex(v, inst);
24130Sstevel@tonic-gate 			switch (err) {
24140Sstevel@tonic-gate 			case 0:
24150Sstevel@tonic-gate 			case ECANCELED:
24160Sstevel@tonic-gate 				break;
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate 			case ECONNABORTED:
24190Sstevel@tonic-gate 				libscf_handle_rebind(info->h);
24200Sstevel@tonic-gate 				rebound = B_TRUE;
24210Sstevel@tonic-gate 				goto rebound;
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 			default:
24240Sstevel@tonic-gate 				bad_error("configure_vertex", err);
24250Sstevel@tonic-gate 			}
24260Sstevel@tonic-gate 			break;
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 		case ENOENT:
24290Sstevel@tonic-gate 			break;
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate 		case ECONNABORTED:
24320Sstevel@tonic-gate 			libscf_handle_rebind(info->h);
24330Sstevel@tonic-gate 			rebound = B_TRUE;
24340Sstevel@tonic-gate 			goto rebound;
24350Sstevel@tonic-gate 
24360Sstevel@tonic-gate 		case EINVAL:
24370Sstevel@tonic-gate 		case ENOTSUP:
24380Sstevel@tonic-gate 		default:
24390Sstevel@tonic-gate 			bad_error("libscf_fmri_get_instance", err);
24400Sstevel@tonic-gate 		}
24410Sstevel@tonic-gate 
24420Sstevel@tonic-gate 		scf_instance_destroy(inst);
24430Sstevel@tonic-gate 
24440Sstevel@tonic-gate 		if (rebound)
24450Sstevel@tonic-gate 			return (info->err = ECONNRESET);
24460Sstevel@tonic-gate 		break;
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate 	case GVT_SVC:
24490Sstevel@tonic-gate 		(void) add_service(v->gv_name, info->h, &rebound);
24500Sstevel@tonic-gate 		if (rebound)
24510Sstevel@tonic-gate 			return (info->err = ECONNRESET);
24520Sstevel@tonic-gate 	}
24530Sstevel@tonic-gate 
24540Sstevel@tonic-gate 	return (0);
24550Sstevel@tonic-gate }
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate struct deppg_info {
24580Sstevel@tonic-gate 	graph_vertex_t	*v;		/* GVT_INST vertex */
24590Sstevel@tonic-gate 	int		err;		/* return error */
24600Sstevel@tonic-gate 	int		**pathp;	/* return circular dependency path */
24610Sstevel@tonic-gate };
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate /*
24640Sstevel@tonic-gate  * Make info->v depend on a new GVT_GROUP node for this property group,
24650Sstevel@tonic-gate  * and then call process_dependency_fmri() for the values of the entity
24660Sstevel@tonic-gate  * property.  Return 0 on success, or if something goes wrong return nonzero
24670Sstevel@tonic-gate  * and set info->err to ECONNABORTED, EINVAL, or the error code returned by
24680Sstevel@tonic-gate  * process_dependency_fmri().
24690Sstevel@tonic-gate  */
24700Sstevel@tonic-gate static int
24710Sstevel@tonic-gate process_dependency_pg(scf_propertygroup_t *pg, struct deppg_info *info)
24720Sstevel@tonic-gate {
24730Sstevel@tonic-gate 	scf_handle_t *h;
24740Sstevel@tonic-gate 	depgroup_type_t deptype;
24752704Srm88369 	restarter_error_t rerr;
24760Sstevel@tonic-gate 	struct depfmri_info linfo;
24770Sstevel@tonic-gate 	char *fmri, *pg_name;
24780Sstevel@tonic-gate 	size_t fmri_sz;
24790Sstevel@tonic-gate 	graph_vertex_t *depgrp;
24800Sstevel@tonic-gate 	scf_property_t *prop;
24810Sstevel@tonic-gate 	int err;
24820Sstevel@tonic-gate 	int empty;
24830Sstevel@tonic-gate 	scf_error_t scferr;
24840Sstevel@tonic-gate 	ssize_t len;
24850Sstevel@tonic-gate 
248611466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
24870Sstevel@tonic-gate 
24880Sstevel@tonic-gate 	h = scf_pg_handle(pg);
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 	pg_name = startd_alloc(max_scf_name_size);
24910Sstevel@tonic-gate 
24920Sstevel@tonic-gate 	len = scf_pg_get_name(pg, pg_name, max_scf_name_size);
24930Sstevel@tonic-gate 	if (len < 0) {
24940Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
24950Sstevel@tonic-gate 		switch (scf_error()) {
24960Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
24970Sstevel@tonic-gate 		default:
24980Sstevel@tonic-gate 			return (info->err = ECONNABORTED);
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
25010Sstevel@tonic-gate 			return (info->err = 0);
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
25040Sstevel@tonic-gate 			bad_error("scf_pg_get_name", scf_error());
25050Sstevel@tonic-gate 		}
25060Sstevel@tonic-gate 	}
25070Sstevel@tonic-gate 
25080Sstevel@tonic-gate 	/*
25090Sstevel@tonic-gate 	 * Skip over empty dependency groups.  Since dependency property
25100Sstevel@tonic-gate 	 * groups are updated atomically, they are either empty or
25110Sstevel@tonic-gate 	 * fully populated.
25120Sstevel@tonic-gate 	 */
25130Sstevel@tonic-gate 	empty = depgroup_empty(h, pg);
25140Sstevel@tonic-gate 	if (empty < 0) {
25150Sstevel@tonic-gate 		log_error(LOG_INFO,
25160Sstevel@tonic-gate 		    "Error reading dependency group \"%s\" of %s: %s\n",
25170Sstevel@tonic-gate 		    pg_name, info->v->gv_name, scf_strerror(scf_error()));
25180Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
25190Sstevel@tonic-gate 		return (info->err = EINVAL);
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate 	} else if (empty == 1) {
25220Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
25230Sstevel@tonic-gate 		    "Ignoring empty dependency group \"%s\" of %s\n",
25240Sstevel@tonic-gate 		    pg_name, info->v->gv_name);
25250Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
25260Sstevel@tonic-gate 		return (info->err = 0);
25270Sstevel@tonic-gate 	}
25280Sstevel@tonic-gate 
25290Sstevel@tonic-gate 	fmri_sz = strlen(info->v->gv_name) + 1 + len + 1;
25300Sstevel@tonic-gate 	fmri = startd_alloc(fmri_sz);
25310Sstevel@tonic-gate 
25320Sstevel@tonic-gate 	(void) snprintf(fmri, max_scf_name_size, "%s>%s", info->v->gv_name,
25330Sstevel@tonic-gate 	    pg_name);
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 	/* Validate the pg before modifying the graph */
25360Sstevel@tonic-gate 	deptype = depgroup_read_grouping(h, pg);
25370Sstevel@tonic-gate 	if (deptype == DEPGRP_UNSUPPORTED) {
25380Sstevel@tonic-gate 		log_error(LOG_INFO,
25390Sstevel@tonic-gate 		    "Dependency \"%s\" of %s has an unknown grouping value.\n",
25400Sstevel@tonic-gate 		    pg_name, info->v->gv_name);
25410Sstevel@tonic-gate 		startd_free(fmri, fmri_sz);
25420Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
25430Sstevel@tonic-gate 		return (info->err = EINVAL);
25440Sstevel@tonic-gate 	}
25450Sstevel@tonic-gate 
25462704Srm88369 	rerr = depgroup_read_restart(h, pg);
25472704Srm88369 	if (rerr == RERR_UNSUPPORTED) {
25482704Srm88369 		log_error(LOG_INFO,
25492704Srm88369 		    "Dependency \"%s\" of %s has an unknown restart_on value."
25502704Srm88369 		    "\n", pg_name, info->v->gv_name);
25512704Srm88369 		startd_free(fmri, fmri_sz);
25522704Srm88369 		startd_free(pg_name, max_scf_name_size);
25532704Srm88369 		return (info->err = EINVAL);
25542704Srm88369 	}
25552704Srm88369 
25560Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
25570Sstevel@tonic-gate 
25580Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0) {
25590Sstevel@tonic-gate 		scferr = scf_error();
25600Sstevel@tonic-gate 		scf_property_destroy(prop);
25610Sstevel@tonic-gate 		if (scferr == SCF_ERROR_DELETED) {
25620Sstevel@tonic-gate 			startd_free(fmri, fmri_sz);
25630Sstevel@tonic-gate 			startd_free(pg_name, max_scf_name_size);
25640Sstevel@tonic-gate 			return (info->err = 0);
25650Sstevel@tonic-gate 		} else if (scferr != SCF_ERROR_NOT_FOUND) {
25660Sstevel@tonic-gate 			startd_free(fmri, fmri_sz);
25670Sstevel@tonic-gate 			startd_free(pg_name, max_scf_name_size);
25680Sstevel@tonic-gate 			return (info->err = ECONNABORTED);
25690Sstevel@tonic-gate 		}
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate 		log_error(LOG_INFO,
25720Sstevel@tonic-gate 		    "Dependency \"%s\" of %s is missing a \"%s\" property.\n",
25730Sstevel@tonic-gate 		    pg_name, info->v->gv_name, SCF_PROPERTY_ENTITIES);
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate 		startd_free(fmri, fmri_sz);
25760Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate 		return (info->err = EINVAL);
25790Sstevel@tonic-gate 	}
25800Sstevel@tonic-gate 
25810Sstevel@tonic-gate 	/* Create depgroup vertex for pg */
25820Sstevel@tonic-gate 	err = graph_insert_vertex_unconfigured(fmri, GVT_GROUP, deptype,
25832704Srm88369 	    rerr, &depgrp);
25840Sstevel@tonic-gate 	assert(err == 0);
25850Sstevel@tonic-gate 	startd_free(fmri, fmri_sz);
25860Sstevel@tonic-gate 
25870Sstevel@tonic-gate 	/* Add dependency from inst vertex to new vertex */
25880Sstevel@tonic-gate 	err = graph_insert_dependency(info->v, depgrp, info->pathp);
25890Sstevel@tonic-gate 	/* ELOOP can't happen because this should be a new vertex */
25900Sstevel@tonic-gate 	assert(err == 0);
25910Sstevel@tonic-gate 
25920Sstevel@tonic-gate 	linfo.v = depgrp;
25930Sstevel@tonic-gate 	linfo.type = depgroup_read_scheme(h, pg);
25940Sstevel@tonic-gate 	linfo.inst_fmri = info->v->gv_name;
25950Sstevel@tonic-gate 	linfo.pg_name = pg_name;
25960Sstevel@tonic-gate 	linfo.h = h;
25970Sstevel@tonic-gate 	linfo.err = 0;
25980Sstevel@tonic-gate 	linfo.pathp = info->pathp;
25990Sstevel@tonic-gate 	err = walk_property_astrings(prop, (callback_t)process_dependency_fmri,
26000Sstevel@tonic-gate 	    &linfo);
26010Sstevel@tonic-gate 
26020Sstevel@tonic-gate 	scf_property_destroy(prop);
26030Sstevel@tonic-gate 	startd_free(pg_name, max_scf_name_size);
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate 	switch (err) {
26060Sstevel@tonic-gate 	case 0:
26070Sstevel@tonic-gate 	case EINTR:
26080Sstevel@tonic-gate 		return (info->err = linfo.err);
26090Sstevel@tonic-gate 
26100Sstevel@tonic-gate 	case ECONNABORTED:
26110Sstevel@tonic-gate 	case EINVAL:
26120Sstevel@tonic-gate 		return (info->err = err);
26130Sstevel@tonic-gate 
26140Sstevel@tonic-gate 	case ECANCELED:
26150Sstevel@tonic-gate 		return (info->err = 0);
26160Sstevel@tonic-gate 
26170Sstevel@tonic-gate 	case ECONNRESET:
26180Sstevel@tonic-gate 		return (info->err = ECONNABORTED);
26190Sstevel@tonic-gate 
26200Sstevel@tonic-gate 	default:
26210Sstevel@tonic-gate 		bad_error("walk_property_astrings", err);
26220Sstevel@tonic-gate 		/* NOTREACHED */
26230Sstevel@tonic-gate 	}
26240Sstevel@tonic-gate }
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate /*
26270Sstevel@tonic-gate  * Build the dependency info for v from the repository.  Returns 0 on success,
26280Sstevel@tonic-gate  * ECONNABORTED on repository disconnection, EINVAL if the repository
26290Sstevel@tonic-gate  * configuration is invalid, and ELOOP if a dependency would cause a cycle.
26300Sstevel@tonic-gate  * In the last case, *pathp will point to a -1-terminated array of ids which
26310Sstevel@tonic-gate  * constitute the rest of the dependency cycle.
26320Sstevel@tonic-gate  */
26330Sstevel@tonic-gate static int
26340Sstevel@tonic-gate set_dependencies(graph_vertex_t *v, scf_instance_t *inst, int **pathp)
26350Sstevel@tonic-gate {
26360Sstevel@tonic-gate 	struct deppg_info info;
26370Sstevel@tonic-gate 	int err;
26380Sstevel@tonic-gate 	uint_t old_configured;
26390Sstevel@tonic-gate 
264011466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	/*
26430Sstevel@tonic-gate 	 * Mark the vertex as configured during dependency insertion to avoid
26440Sstevel@tonic-gate 	 * dependency cycles (which can appear in the graph if one of the
26450Sstevel@tonic-gate 	 * vertices is an exclusion-group).
26460Sstevel@tonic-gate 	 */
26470Sstevel@tonic-gate 	old_configured = v->gv_flags & GV_CONFIGURED;
26480Sstevel@tonic-gate 	v->gv_flags |= GV_CONFIGURED;
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate 	info.err = 0;
26510Sstevel@tonic-gate 	info.v = v;
26520Sstevel@tonic-gate 	info.pathp = pathp;
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate 	err = walk_dependency_pgs(inst, (callback_t)process_dependency_pg,
26550Sstevel@tonic-gate 	    &info);
26560Sstevel@tonic-gate 
26570Sstevel@tonic-gate 	if (!old_configured)
26580Sstevel@tonic-gate 		v->gv_flags &= ~GV_CONFIGURED;
26590Sstevel@tonic-gate 
26600Sstevel@tonic-gate 	switch (err) {
26610Sstevel@tonic-gate 	case 0:
26620Sstevel@tonic-gate 	case EINTR:
26630Sstevel@tonic-gate 		return (info.err);
26640Sstevel@tonic-gate 
26650Sstevel@tonic-gate 	case ECONNABORTED:
26660Sstevel@tonic-gate 		return (ECONNABORTED);
26670Sstevel@tonic-gate 
26680Sstevel@tonic-gate 	case ECANCELED:
26690Sstevel@tonic-gate 		/* Should get delete event, so return 0. */
26700Sstevel@tonic-gate 		return (0);
26710Sstevel@tonic-gate 
26720Sstevel@tonic-gate 	default:
26730Sstevel@tonic-gate 		bad_error("walk_dependency_pgs", err);
26740Sstevel@tonic-gate 		/* NOTREACHED */
26750Sstevel@tonic-gate 	}
26760Sstevel@tonic-gate }
26770Sstevel@tonic-gate 
26780Sstevel@tonic-gate 
26790Sstevel@tonic-gate static void
26800Sstevel@tonic-gate handle_cycle(const char *fmri, int *path)
26810Sstevel@tonic-gate {
26820Sstevel@tonic-gate 	const char *cp;
26830Sstevel@tonic-gate 	size_t sz;
26840Sstevel@tonic-gate 
268511466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate 	path_to_str(path, (char **)&cp, &sz);
26880Sstevel@tonic-gate 
26891958Slianep 	log_error(LOG_ERR, "Transitioning %s to maintenance "
26901958Slianep 	    "because it completes a dependency cycle (see svcs -xv for "
26911958Slianep 	    "details):\n%s", fmri ? fmri : "?", cp);
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate 	startd_free((void *)cp, sz);
26940Sstevel@tonic-gate }
26950Sstevel@tonic-gate 
26960Sstevel@tonic-gate /*
26971712Srm88369  * Increment the vertex's reference count to prevent the vertex removal
26981712Srm88369  * from the dgraph.
26991712Srm88369  */
27001712Srm88369 static void
27011712Srm88369 vertex_ref(graph_vertex_t *v)
27021712Srm88369 {
270311466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
27041712Srm88369 
27051712Srm88369 	v->gv_refs++;
27061712Srm88369 }
27071712Srm88369 
27081712Srm88369 /*
27091712Srm88369  * Decrement the vertex's reference count and remove the vertex from
27101712Srm88369  * the dgraph when possible.
27111712Srm88369  *
27121712Srm88369  * Return VERTEX_REMOVED when the vertex has been removed otherwise
27131712Srm88369  * return VERTEX_INUSE.
27140Sstevel@tonic-gate  */
27150Sstevel@tonic-gate static int
27161712Srm88369 vertex_unref(graph_vertex_t *v)
27171712Srm88369 {
271811466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
27191712Srm88369 	assert(v->gv_refs > 0);
27201712Srm88369 
27211712Srm88369 	v->gv_refs--;
27221712Srm88369 
27231712Srm88369 	return (free_if_unrefed(v));
27241712Srm88369 }
27251712Srm88369 
27261712Srm88369 /*
27271712Srm88369  * When run on the dependencies of a vertex, populates list with
27281712Srm88369  * graph_edge_t's which point to the service vertices or the instance
27291712Srm88369  * vertices (no GVT_GROUP nodes) on which the vertex depends.
27301712Srm88369  *
27311712Srm88369  * Increment the vertex's reference count once the vertex is inserted
27321712Srm88369  * in the list. The vertex won't be able to be deleted from the dgraph
27331712Srm88369  * while it is referenced.
27341712Srm88369  */
27351712Srm88369 static int
27361712Srm88369 append_svcs_or_insts(graph_edge_t *e, uu_list_t *list)
27370Sstevel@tonic-gate {
27380Sstevel@tonic-gate 	graph_vertex_t *v = e->ge_vertex;
27390Sstevel@tonic-gate 	graph_edge_t *new;
27400Sstevel@tonic-gate 	int r;
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate 	switch (v->gv_type) {
27430Sstevel@tonic-gate 	case GVT_INST:
27440Sstevel@tonic-gate 	case GVT_SVC:
27450Sstevel@tonic-gate 		break;
27460Sstevel@tonic-gate 
27470Sstevel@tonic-gate 	case GVT_GROUP:
27480Sstevel@tonic-gate 		r = uu_list_walk(v->gv_dependencies,
27491712Srm88369 		    (uu_walk_fn_t *)append_svcs_or_insts, list, 0);
27500Sstevel@tonic-gate 		assert(r == 0);
27510Sstevel@tonic-gate 		return (UU_WALK_NEXT);
27520Sstevel@tonic-gate 
27530Sstevel@tonic-gate 	case GVT_FILE:
27540Sstevel@tonic-gate 		return (UU_WALK_NEXT);
27550Sstevel@tonic-gate 
27560Sstevel@tonic-gate 	default:
27570Sstevel@tonic-gate #ifndef NDEBUG
27580Sstevel@tonic-gate 		uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
27590Sstevel@tonic-gate 		    __LINE__, v->gv_type);
27600Sstevel@tonic-gate #endif
27610Sstevel@tonic-gate 		abort();
27620Sstevel@tonic-gate 	}
27630Sstevel@tonic-gate 
27640Sstevel@tonic-gate 	new = startd_alloc(sizeof (*new));
27650Sstevel@tonic-gate 	new->ge_vertex = v;
27660Sstevel@tonic-gate 	uu_list_node_init(new, &new->ge_link, graph_edge_pool);
27670Sstevel@tonic-gate 	r = uu_list_insert_before(list, NULL, new);
27680Sstevel@tonic-gate 	assert(r == 0);
27691712Srm88369 
27701712Srm88369 	/*
27711712Srm88369 	 * Because we are inserting the vertex in a list, we don't want
27721712Srm88369 	 * the vertex to be freed while the list is in use. In order to
27731712Srm88369 	 * achieve that, increment the vertex's reference count.
27741712Srm88369 	 */
27751712Srm88369 	vertex_ref(v);
27761712Srm88369 
27770Sstevel@tonic-gate 	return (UU_WALK_NEXT);
27780Sstevel@tonic-gate }
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate static boolean_t
27810Sstevel@tonic-gate should_be_in_subgraph(graph_vertex_t *v)
27820Sstevel@tonic-gate {
27830Sstevel@tonic-gate 	graph_edge_t *e;
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate 	if (v == milestone)
27860Sstevel@tonic-gate 		return (B_TRUE);
27870Sstevel@tonic-gate 
27880Sstevel@tonic-gate 	/*
27890Sstevel@tonic-gate 	 * v is in the subgraph if any of its dependents are in the subgraph.
27900Sstevel@tonic-gate 	 * Except for EXCLUDE_ALL dependents.  And OPTIONAL dependents only
27910Sstevel@tonic-gate 	 * count if we're enabled.
27920Sstevel@tonic-gate 	 */
27930Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
27940Sstevel@tonic-gate 	    e != NULL;
27950Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e)) {
27960Sstevel@tonic-gate 		graph_vertex_t *dv = e->ge_vertex;
27970Sstevel@tonic-gate 
27980Sstevel@tonic-gate 		if (!(dv->gv_flags & GV_INSUBGRAPH))
27990Sstevel@tonic-gate 			continue;
28000Sstevel@tonic-gate 
28010Sstevel@tonic-gate 		/*
28020Sstevel@tonic-gate 		 * Don't include instances that are optional and disabled.
28030Sstevel@tonic-gate 		 */
28040Sstevel@tonic-gate 		if (v->gv_type == GVT_INST && dv->gv_type == GVT_SVC) {
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate 			int in = 0;
28070Sstevel@tonic-gate 			graph_edge_t *ee;
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate 			for (ee = uu_list_first(dv->gv_dependents);
28100Sstevel@tonic-gate 			    ee != NULL;
28110Sstevel@tonic-gate 			    ee = uu_list_next(dv->gv_dependents, ee)) {
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 				graph_vertex_t *ddv = e->ge_vertex;
28140Sstevel@tonic-gate 
28150Sstevel@tonic-gate 				if (ddv->gv_type == GVT_GROUP &&
28160Sstevel@tonic-gate 				    ddv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
28170Sstevel@tonic-gate 					continue;
28180Sstevel@tonic-gate 
28190Sstevel@tonic-gate 				if (ddv->gv_type == GVT_GROUP &&
28200Sstevel@tonic-gate 				    ddv->gv_depgroup == DEPGRP_OPTIONAL_ALL &&
28210Sstevel@tonic-gate 				    !(v->gv_flags & GV_ENBLD_NOOVR))
28220Sstevel@tonic-gate 					continue;
28230Sstevel@tonic-gate 
28240Sstevel@tonic-gate 				in = 1;
28250Sstevel@tonic-gate 			}
28260Sstevel@tonic-gate 			if (!in)
28270Sstevel@tonic-gate 				continue;
28280Sstevel@tonic-gate 		}
28290Sstevel@tonic-gate 		if (v->gv_type == GVT_INST &&
28300Sstevel@tonic-gate 		    dv->gv_type == GVT_GROUP &&
28310Sstevel@tonic-gate 		    dv->gv_depgroup == DEPGRP_OPTIONAL_ALL &&
28320Sstevel@tonic-gate 		    !(v->gv_flags & GV_ENBLD_NOOVR))
28330Sstevel@tonic-gate 			continue;
28340Sstevel@tonic-gate 
28350Sstevel@tonic-gate 		/* Don't include excluded services and instances */
28360Sstevel@tonic-gate 		if (dv->gv_type == GVT_GROUP &&
28370Sstevel@tonic-gate 		    dv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
28380Sstevel@tonic-gate 			continue;
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 		return (B_TRUE);
28410Sstevel@tonic-gate 	}
28420Sstevel@tonic-gate 
28430Sstevel@tonic-gate 	return (B_FALSE);
28440Sstevel@tonic-gate }
28450Sstevel@tonic-gate 
28460Sstevel@tonic-gate /*
28470Sstevel@tonic-gate  * Ensures that GV_INSUBGRAPH is set properly for v and its descendents.  If
28480Sstevel@tonic-gate  * any bits change, manipulate the repository appropriately.  Returns 0 or
28490Sstevel@tonic-gate  * ECONNABORTED.
28500Sstevel@tonic-gate  */
28510Sstevel@tonic-gate static int
28520Sstevel@tonic-gate eval_subgraph(graph_vertex_t *v, scf_handle_t *h)
28530Sstevel@tonic-gate {
28540Sstevel@tonic-gate 	boolean_t old = (v->gv_flags & GV_INSUBGRAPH) != 0;
28550Sstevel@tonic-gate 	boolean_t new;
28560Sstevel@tonic-gate 	graph_edge_t *e;
28570Sstevel@tonic-gate 	scf_instance_t *inst;
28580Sstevel@tonic-gate 	int ret = 0, r;
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate 	assert(milestone != NULL && milestone != MILESTONE_NONE);
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 	new = should_be_in_subgraph(v);
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate 	if (new == old)
28650Sstevel@tonic-gate 		return (0);
28660Sstevel@tonic-gate 
28670Sstevel@tonic-gate 	log_framework(LOG_DEBUG, new ? "Adding %s to the subgraph.\n" :
28680Sstevel@tonic-gate 	    "Removing %s from the subgraph.\n", v->gv_name);
28690Sstevel@tonic-gate 
28700Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_INSUBGRAPH) |
28710Sstevel@tonic-gate 	    (new ? GV_INSUBGRAPH : 0);
28720Sstevel@tonic-gate 
28730Sstevel@tonic-gate 	if (v->gv_type == GVT_INST && (v->gv_flags & GV_CONFIGURED)) {
28740Sstevel@tonic-gate 		int err;
28750Sstevel@tonic-gate 
28760Sstevel@tonic-gate get_inst:
28770Sstevel@tonic-gate 		err = libscf_fmri_get_instance(h, v->gv_name, &inst);
28780Sstevel@tonic-gate 		if (err != 0) {
28790Sstevel@tonic-gate 			switch (err) {
28800Sstevel@tonic-gate 			case ECONNABORTED:
28810Sstevel@tonic-gate 				libscf_handle_rebind(h);
28820Sstevel@tonic-gate 				ret = ECONNABORTED;
28830Sstevel@tonic-gate 				goto get_inst;
28840Sstevel@tonic-gate 
28850Sstevel@tonic-gate 			case ENOENT:
28860Sstevel@tonic-gate 				break;
28870Sstevel@tonic-gate 
28880Sstevel@tonic-gate 			case EINVAL:
28890Sstevel@tonic-gate 			case ENOTSUP:
28900Sstevel@tonic-gate 			default:
28910Sstevel@tonic-gate 				bad_error("libscf_fmri_get_instance", err);
28920Sstevel@tonic-gate 			}
28930Sstevel@tonic-gate 		} else {
28940Sstevel@tonic-gate 			const char *f;
28950Sstevel@tonic-gate 
28960Sstevel@tonic-gate 			if (new) {
28970Sstevel@tonic-gate 				err = libscf_delete_enable_ovr(inst);
28980Sstevel@tonic-gate 				f = "libscf_delete_enable_ovr";
28990Sstevel@tonic-gate 			} else {
29000Sstevel@tonic-gate 				err = libscf_set_enable_ovr(inst, 0);
29010Sstevel@tonic-gate 				f = "libscf_set_enable_ovr";
29020Sstevel@tonic-gate 			}
29030Sstevel@tonic-gate 			scf_instance_destroy(inst);
29040Sstevel@tonic-gate 			switch (err) {
29050Sstevel@tonic-gate 			case 0:
29060Sstevel@tonic-gate 			case ECANCELED:
29070Sstevel@tonic-gate 				break;
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate 			case ECONNABORTED:
29100Sstevel@tonic-gate 				libscf_handle_rebind(h);
29110Sstevel@tonic-gate 				/*
29120Sstevel@tonic-gate 				 * We must continue so the graph is updated,
29130Sstevel@tonic-gate 				 * but we must return ECONNABORTED so any
29140Sstevel@tonic-gate 				 * libscf state held by any callers is reset.
29150Sstevel@tonic-gate 				 */
29160Sstevel@tonic-gate 				ret = ECONNABORTED;
29170Sstevel@tonic-gate 				goto get_inst;
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate 			case EROFS:
29200Sstevel@tonic-gate 			case EPERM:
29210Sstevel@tonic-gate 				log_error(LOG_WARNING,
29220Sstevel@tonic-gate 				    "Could not set %s/%s for %s: %s.\n",
29230Sstevel@tonic-gate 				    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
29240Sstevel@tonic-gate 				    v->gv_name, strerror(err));
29250Sstevel@tonic-gate 				break;
29260Sstevel@tonic-gate 
29270Sstevel@tonic-gate 			default:
29280Sstevel@tonic-gate 				bad_error(f, err);
29290Sstevel@tonic-gate 			}
29300Sstevel@tonic-gate 		}
29310Sstevel@tonic-gate 	}
29320Sstevel@tonic-gate 
29330Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
29340Sstevel@tonic-gate 	    e != NULL;
29350Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
29360Sstevel@tonic-gate 		r = eval_subgraph(e->ge_vertex, h);
29370Sstevel@tonic-gate 		if (r != 0) {
29380Sstevel@tonic-gate 			assert(r == ECONNABORTED);
29390Sstevel@tonic-gate 			ret = ECONNABORTED;
29400Sstevel@tonic-gate 		}
29410Sstevel@tonic-gate 	}
29420Sstevel@tonic-gate 
29430Sstevel@tonic-gate 	return (ret);
29440Sstevel@tonic-gate }
29450Sstevel@tonic-gate 
29460Sstevel@tonic-gate /*
29470Sstevel@tonic-gate  * Delete the (property group) dependencies of v & create new ones based on
29480Sstevel@tonic-gate  * inst.  If doing so would create a cycle, log a message and put the instance
29490Sstevel@tonic-gate  * into maintenance.  Update GV_INSUBGRAPH flags as necessary.  Returns 0 or
29500Sstevel@tonic-gate  * ECONNABORTED.
29510Sstevel@tonic-gate  */
29521958Slianep int
29530Sstevel@tonic-gate refresh_vertex(graph_vertex_t *v, scf_instance_t *inst)
29540Sstevel@tonic-gate {
29550Sstevel@tonic-gate 	int err;
29560Sstevel@tonic-gate 	int *path;
29570Sstevel@tonic-gate 	char *fmri;
29580Sstevel@tonic-gate 	int r;
29590Sstevel@tonic-gate 	scf_handle_t *h = scf_instance_handle(inst);
29600Sstevel@tonic-gate 	uu_list_t *old_deps;
29610Sstevel@tonic-gate 	int ret = 0;
29620Sstevel@tonic-gate 	graph_edge_t *e;
29631712Srm88369 	graph_vertex_t *vv;
29640Sstevel@tonic-gate 
296511466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
29660Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
29670Sstevel@tonic-gate 
29680Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph engine: Refreshing %s.\n", v->gv_name);
29690Sstevel@tonic-gate 
29700Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
29710Sstevel@tonic-gate 		/*
29720Sstevel@tonic-gate 		 * In case some of v's dependencies are being deleted we must
29730Sstevel@tonic-gate 		 * make a list of them now for GV_INSUBGRAPH-flag evaluation
29740Sstevel@tonic-gate 		 * after the new dependencies are in place.
29750Sstevel@tonic-gate 		 */
29760Sstevel@tonic-gate 		old_deps = startd_list_create(graph_edge_pool, NULL, 0);
29770Sstevel@tonic-gate 
29780Sstevel@tonic-gate 		err = uu_list_walk(v->gv_dependencies,
29791712Srm88369 		    (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0);
29800Sstevel@tonic-gate 		assert(err == 0);
29810Sstevel@tonic-gate 	}
29820Sstevel@tonic-gate 
29830Sstevel@tonic-gate 	delete_instance_dependencies(v, B_FALSE);
29840Sstevel@tonic-gate 
29850Sstevel@tonic-gate 	err = set_dependencies(v, inst, &path);
29860Sstevel@tonic-gate 	switch (err) {
29870Sstevel@tonic-gate 	case 0:
29880Sstevel@tonic-gate 		break;
29890Sstevel@tonic-gate 
29900Sstevel@tonic-gate 	case ECONNABORTED:
29910Sstevel@tonic-gate 		ret = err;
29920Sstevel@tonic-gate 		goto out;
29930Sstevel@tonic-gate 
29940Sstevel@tonic-gate 	case EINVAL:
29950Sstevel@tonic-gate 	case ELOOP:
29960Sstevel@tonic-gate 		r = libscf_instance_get_fmri(inst, &fmri);
29970Sstevel@tonic-gate 		switch (r) {
29980Sstevel@tonic-gate 		case 0:
29990Sstevel@tonic-gate 			break;
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate 		case ECONNABORTED:
30020Sstevel@tonic-gate 			ret = ECONNABORTED;
30030Sstevel@tonic-gate 			goto out;
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate 		case ECANCELED:
30060Sstevel@tonic-gate 			ret = 0;
30070Sstevel@tonic-gate 			goto out;
30080Sstevel@tonic-gate 
30090Sstevel@tonic-gate 		default:
30100Sstevel@tonic-gate 			bad_error("libscf_instance_get_fmri", r);
30110Sstevel@tonic-gate 		}
30120Sstevel@tonic-gate 
30130Sstevel@tonic-gate 		if (err == EINVAL) {
30140Sstevel@tonic-gate 			log_error(LOG_ERR, "Transitioning %s "
30150Sstevel@tonic-gate 			    "to maintenance due to misconfiguration.\n",
30160Sstevel@tonic-gate 			    fmri ? fmri : "?");
30170Sstevel@tonic-gate 			vertex_send_event(v,
30180Sstevel@tonic-gate 			    RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY);
30190Sstevel@tonic-gate 		} else {
30200Sstevel@tonic-gate 			handle_cycle(fmri, path);
30210Sstevel@tonic-gate 			vertex_send_event(v,
30220Sstevel@tonic-gate 			    RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE);
30230Sstevel@tonic-gate 		}
30240Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
30250Sstevel@tonic-gate 		ret = 0;
30260Sstevel@tonic-gate 		goto out;
30270Sstevel@tonic-gate 
30280Sstevel@tonic-gate 	default:
30290Sstevel@tonic-gate 		bad_error("set_dependencies", err);
30300Sstevel@tonic-gate 	}
30310Sstevel@tonic-gate 
30320Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
30330Sstevel@tonic-gate 		boolean_t aborted = B_FALSE;
30340Sstevel@tonic-gate 
30350Sstevel@tonic-gate 		for (e = uu_list_first(old_deps);
30360Sstevel@tonic-gate 		    e != NULL;
30370Sstevel@tonic-gate 		    e = uu_list_next(old_deps, e)) {
30381712Srm88369 			vv = e->ge_vertex;
30391712Srm88369 
30401712Srm88369 			if (vertex_unref(vv) == VERTEX_INUSE &&
30411712Srm88369 			    eval_subgraph(vv, h) == ECONNABORTED)
30420Sstevel@tonic-gate 				aborted = B_TRUE;
30430Sstevel@tonic-gate 		}
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate 		for (e = uu_list_first(v->gv_dependencies);
30460Sstevel@tonic-gate 		    e != NULL;
30470Sstevel@tonic-gate 		    e = uu_list_next(v->gv_dependencies, e)) {
30480Sstevel@tonic-gate 			if (eval_subgraph(e->ge_vertex, h) ==
30490Sstevel@tonic-gate 			    ECONNABORTED)
30500Sstevel@tonic-gate 				aborted = B_TRUE;
30510Sstevel@tonic-gate 		}
30520Sstevel@tonic-gate 
30530Sstevel@tonic-gate 		if (aborted) {
30540Sstevel@tonic-gate 			ret = ECONNABORTED;
30550Sstevel@tonic-gate 			goto out;
30560Sstevel@tonic-gate 		}
30570Sstevel@tonic-gate 	}
30580Sstevel@tonic-gate 
30591958Slianep 	graph_start_if_satisfied(v);
30600Sstevel@tonic-gate 
30610Sstevel@tonic-gate 	ret = 0;
30620Sstevel@tonic-gate 
30630Sstevel@tonic-gate out:
30640Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
30650Sstevel@tonic-gate 		void *cookie = NULL;
30660Sstevel@tonic-gate 
30670Sstevel@tonic-gate 		while ((e = uu_list_teardown(old_deps, &cookie)) != NULL)
30680Sstevel@tonic-gate 			startd_free(e, sizeof (*e));
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate 		uu_list_destroy(old_deps);
30710Sstevel@tonic-gate 	}
30720Sstevel@tonic-gate 
30730Sstevel@tonic-gate 	return (ret);
30740Sstevel@tonic-gate }
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate /*
30770Sstevel@tonic-gate  * Set up v according to inst.  That is, make sure it depends on its
30780Sstevel@tonic-gate  * restarter and set up its dependencies.  Send the ADD_INSTANCE command to
30790Sstevel@tonic-gate  * the restarter, and send ENABLE or DISABLE as appropriate.
30800Sstevel@tonic-gate  *
30810Sstevel@tonic-gate  * Returns 0 on success, ECONNABORTED on repository disconnection, or
30820Sstevel@tonic-gate  * ECANCELED if inst is deleted.
30830Sstevel@tonic-gate  */
30840Sstevel@tonic-gate static int
30850Sstevel@tonic-gate configure_vertex(graph_vertex_t *v, scf_instance_t *inst)
30860Sstevel@tonic-gate {
30870Sstevel@tonic-gate 	scf_handle_t *h;
30880Sstevel@tonic-gate 	scf_propertygroup_t *pg;
30890Sstevel@tonic-gate 	scf_snapshot_t *snap;
30900Sstevel@tonic-gate 	char *restarter_fmri = startd_alloc(max_scf_value_size);
30910Sstevel@tonic-gate 	int enabled, enabled_ovr;
30920Sstevel@tonic-gate 	int err;
30930Sstevel@tonic-gate 	int *path;
30947475SPhilippe.Jung@Sun.COM 	int deathrow;
30950Sstevel@tonic-gate 
30960Sstevel@tonic-gate 	restarter_fmri[0] = '\0';
30970Sstevel@tonic-gate 
309811466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
30990Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
31000Sstevel@tonic-gate 	assert((v->gv_flags & GV_CONFIGURED) == 0);
31010Sstevel@tonic-gate 
31020Sstevel@tonic-gate 	/* GV_INSUBGRAPH should already be set properly. */
31030Sstevel@tonic-gate 	assert(should_be_in_subgraph(v) ==
31040Sstevel@tonic-gate 	    ((v->gv_flags & GV_INSUBGRAPH) != 0));
31050Sstevel@tonic-gate 
31067475SPhilippe.Jung@Sun.COM 	/*
31077475SPhilippe.Jung@Sun.COM 	 * If the instance fmri is in the deathrow list then set the
31087475SPhilippe.Jung@Sun.COM 	 * GV_DEATHROW flag on the vertex and create and set to true the
31097475SPhilippe.Jung@Sun.COM 	 * SCF_PROPERTY_DEATHROW boolean property in the non-persistent
31107475SPhilippe.Jung@Sun.COM 	 * repository for this instance fmri.
31117475SPhilippe.Jung@Sun.COM 	 */
31127475SPhilippe.Jung@Sun.COM 	if ((v->gv_flags & GV_DEATHROW) ||
31137475SPhilippe.Jung@Sun.COM 	    (is_fmri_in_deathrow(v->gv_name) == B_TRUE)) {
31147475SPhilippe.Jung@Sun.COM 		if ((v->gv_flags & GV_DEATHROW) == 0) {
31157475SPhilippe.Jung@Sun.COM 			/*
31167475SPhilippe.Jung@Sun.COM 			 * Set flag GV_DEATHROW, create and set to true
31177475SPhilippe.Jung@Sun.COM 			 * the SCF_PROPERTY_DEATHROW property in the
31187475SPhilippe.Jung@Sun.COM 			 * non-persistent repository for this instance fmri.
31197475SPhilippe.Jung@Sun.COM 			 */
31207475SPhilippe.Jung@Sun.COM 			v->gv_flags |= GV_DEATHROW;
31217475SPhilippe.Jung@Sun.COM 
31227475SPhilippe.Jung@Sun.COM 			switch (err = libscf_set_deathrow(inst, 1)) {
31237475SPhilippe.Jung@Sun.COM 			case 0:
31247475SPhilippe.Jung@Sun.COM 				break;
31257475SPhilippe.Jung@Sun.COM 
31267475SPhilippe.Jung@Sun.COM 			case ECONNABORTED:
31277475SPhilippe.Jung@Sun.COM 			case ECANCELED:
31287475SPhilippe.Jung@Sun.COM 				startd_free(restarter_fmri, max_scf_value_size);
31297475SPhilippe.Jung@Sun.COM 				return (err);
31307475SPhilippe.Jung@Sun.COM 
31317475SPhilippe.Jung@Sun.COM 			case EROFS:
31327475SPhilippe.Jung@Sun.COM 				log_error(LOG_WARNING, "Could not set %s/%s "
31337475SPhilippe.Jung@Sun.COM 				    "for deathrow %s: %s.\n",
31347475SPhilippe.Jung@Sun.COM 				    SCF_PG_DEATHROW, SCF_PROPERTY_DEATHROW,
31357475SPhilippe.Jung@Sun.COM 				    v->gv_name, strerror(err));
31367475SPhilippe.Jung@Sun.COM 				break;
31377475SPhilippe.Jung@Sun.COM 
31387475SPhilippe.Jung@Sun.COM 			case EPERM:
31397475SPhilippe.Jung@Sun.COM 				uu_die("Permission denied.\n");
31407475SPhilippe.Jung@Sun.COM 				/* NOTREACHED */
31417475SPhilippe.Jung@Sun.COM 
31427475SPhilippe.Jung@Sun.COM 			default:
31437475SPhilippe.Jung@Sun.COM 				bad_error("libscf_set_deathrow", err);
31447475SPhilippe.Jung@Sun.COM 			}
31457475SPhilippe.Jung@Sun.COM 			log_framework(LOG_DEBUG, "Deathrow, graph set %s.\n",
31467475SPhilippe.Jung@Sun.COM 			    v->gv_name);
31477475SPhilippe.Jung@Sun.COM 		}
31487475SPhilippe.Jung@Sun.COM 		startd_free(restarter_fmri, max_scf_value_size);
31497475SPhilippe.Jung@Sun.COM 		return (0);
31507475SPhilippe.Jung@Sun.COM 	}
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate 	h = scf_instance_handle(inst);
31530Sstevel@tonic-gate 
31540Sstevel@tonic-gate 	/*
31557475SPhilippe.Jung@Sun.COM 	 * Using a temporary deathrow boolean property, set through
31567475SPhilippe.Jung@Sun.COM 	 * libscf_set_deathrow(), only for fmris on deathrow, is necessary
31577475SPhilippe.Jung@Sun.COM 	 * because deathrow_fini() may already have been called, and in case
31587475SPhilippe.Jung@Sun.COM 	 * of a refresh, GV_DEATHROW may need to be set again.
31597475SPhilippe.Jung@Sun.COM 	 * libscf_get_deathrow() sets deathrow to 1 only if this instance
31607475SPhilippe.Jung@Sun.COM 	 * has a temporary boolean property named 'deathrow' valued true
31617475SPhilippe.Jung@Sun.COM 	 * in a property group 'deathrow', -1 or 0 in all other cases.
31627475SPhilippe.Jung@Sun.COM 	 */
31637475SPhilippe.Jung@Sun.COM 	err = libscf_get_deathrow(h, inst, &deathrow);
31647475SPhilippe.Jung@Sun.COM 	switch (err) {
31657475SPhilippe.Jung@Sun.COM 	case 0:
31667475SPhilippe.Jung@Sun.COM 		break;
31677475SPhilippe.Jung@Sun.COM 
31687475SPhilippe.Jung@Sun.COM 	case ECONNABORTED:
31697475SPhilippe.Jung@Sun.COM 	case ECANCELED:
31707475SPhilippe.Jung@Sun.COM 		startd_free(restarter_fmri, max_scf_value_size);
31717475SPhilippe.Jung@Sun.COM 		return (err);
31727475SPhilippe.Jung@Sun.COM 
31737475SPhilippe.Jung@Sun.COM 	default:
31747475SPhilippe.Jung@Sun.COM 		bad_error("libscf_get_deathrow", err);
31757475SPhilippe.Jung@Sun.COM 	}
31767475SPhilippe.Jung@Sun.COM 
31777475SPhilippe.Jung@Sun.COM 	if (deathrow == 1) {
31787475SPhilippe.Jung@Sun.COM 		v->gv_flags |= GV_DEATHROW;
31797475SPhilippe.Jung@Sun.COM 		startd_free(restarter_fmri, max_scf_value_size);
31807475SPhilippe.Jung@Sun.COM 		return (0);
31817475SPhilippe.Jung@Sun.COM 	}
31827475SPhilippe.Jung@Sun.COM 
31837475SPhilippe.Jung@Sun.COM 	log_framework(LOG_DEBUG, "Graph adding %s.\n", v->gv_name);
31847475SPhilippe.Jung@Sun.COM 
31857475SPhilippe.Jung@Sun.COM 	/*
31860Sstevel@tonic-gate 	 * If the instance does not have a restarter property group,
31870Sstevel@tonic-gate 	 * initialize its state to uninitialized/none, in case the restarter
31880Sstevel@tonic-gate 	 * is not enabled.
31890Sstevel@tonic-gate 	 */
31900Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
31910Sstevel@tonic-gate 
31920Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) != 0) {
31930Sstevel@tonic-gate 		instance_data_t idata;
31940Sstevel@tonic-gate 		uint_t count = 0, msecs = ALLOC_DELAY;
31950Sstevel@tonic-gate 
31960Sstevel@tonic-gate 		switch (scf_error()) {
31970Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
31980Sstevel@tonic-gate 			break;
31990Sstevel@tonic-gate 
32000Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
32010Sstevel@tonic-gate 		default:
32020Sstevel@tonic-gate 			scf_pg_destroy(pg);
32039333SRenaud.Manus@Sun.COM 			startd_free(restarter_fmri, max_scf_value_size);
32040Sstevel@tonic-gate 			return (ECONNABORTED);
32050Sstevel@tonic-gate 
32060Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
32070Sstevel@tonic-gate 			scf_pg_destroy(pg);
32089333SRenaud.Manus@Sun.COM 			startd_free(restarter_fmri, max_scf_value_size);
32090Sstevel@tonic-gate 			return (ECANCELED);
32100Sstevel@tonic-gate 
32110Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
32120Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
32130Sstevel@tonic-gate 		}
32140Sstevel@tonic-gate 
32150Sstevel@tonic-gate 		switch (err = libscf_instance_get_fmri(inst,
32160Sstevel@tonic-gate 		    (char **)&idata.i_fmri)) {
32170Sstevel@tonic-gate 		case 0:
32180Sstevel@tonic-gate 			break;
32190Sstevel@tonic-gate 
32200Sstevel@tonic-gate 		case ECONNABORTED:
32210Sstevel@tonic-gate 		case ECANCELED:
32220Sstevel@tonic-gate 			scf_pg_destroy(pg);
32239333SRenaud.Manus@Sun.COM 			startd_free(restarter_fmri, max_scf_value_size);
32240Sstevel@tonic-gate 			return (err);
32250Sstevel@tonic-gate 
32260Sstevel@tonic-gate 		default:
32270Sstevel@tonic-gate 			bad_error("libscf_instance_get_fmri", err);
32280Sstevel@tonic-gate 		}
32290Sstevel@tonic-gate 
32300Sstevel@tonic-gate 		idata.i_state = RESTARTER_STATE_NONE;
32310Sstevel@tonic-gate 		idata.i_next_state = RESTARTER_STATE_NONE;
32320Sstevel@tonic-gate 
32330Sstevel@tonic-gate init_state:
32340Sstevel@tonic-gate 		switch (err = _restarter_commit_states(h, &idata,
32350Sstevel@tonic-gate 		    RESTARTER_STATE_UNINIT, RESTARTER_STATE_NONE, NULL)) {
32360Sstevel@tonic-gate 		case 0:
32370Sstevel@tonic-gate 			break;
32380Sstevel@tonic-gate 
32390Sstevel@tonic-gate 		case ENOMEM:
32400Sstevel@tonic-gate 			++count;
32410Sstevel@tonic-gate 			if (count < ALLOC_RETRY) {
32420Sstevel@tonic-gate 				(void) poll(NULL, 0, msecs);
32430Sstevel@tonic-gate 				msecs *= ALLOC_DELAY_MULT;
32440Sstevel@tonic-gate 				goto init_state;
32450Sstevel@tonic-gate 			}
32460Sstevel@tonic-gate 
32470Sstevel@tonic-gate 			uu_die("Insufficient memory.\n");
32480Sstevel@tonic-gate 			/* NOTREACHED */
32490Sstevel@tonic-gate 
32500Sstevel@tonic-gate 		case ECONNABORTED:
32512682Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
32520Sstevel@tonic-gate 			scf_pg_destroy(pg);
32539333SRenaud.Manus@Sun.COM 			startd_free(restarter_fmri, max_scf_value_size);
32540Sstevel@tonic-gate 			return (ECONNABORTED);
32550Sstevel@tonic-gate 
32560Sstevel@tonic-gate 		case ENOENT:
32572682Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
32580Sstevel@tonic-gate 			scf_pg_destroy(pg);
32599333SRenaud.Manus@Sun.COM 			startd_free(restarter_fmri, max_scf_value_size);
32600Sstevel@tonic-gate 			return (ECANCELED);
32610Sstevel@tonic-gate 
32620Sstevel@tonic-gate 		case EPERM:
32630Sstevel@tonic-gate 		case EACCES:
32640Sstevel@tonic-gate 		case EROFS:
32650Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not initialize state for "
32660Sstevel@tonic-gate 			    "%s: %s.\n", idata.i_fmri, strerror(err));
32670Sstevel@tonic-gate 			break;
32680Sstevel@tonic-gate 
32690Sstevel@tonic-gate 		case EINVAL:
32700Sstevel@tonic-gate 		default:
32710Sstevel@tonic-gate 			bad_error("_restarter_commit_states", err);
32720Sstevel@tonic-gate 		}
32730Sstevel@tonic-gate 
32740Sstevel@tonic-gate 		startd_free((void *)idata.i_fmri, max_scf_fmri_size);
32750Sstevel@tonic-gate 	}
32760Sstevel@tonic-gate 
32770Sstevel@tonic-gate 	scf_pg_destroy(pg);
32780Sstevel@tonic-gate 
32790Sstevel@tonic-gate 	if (milestone != NULL) {
32800Sstevel@tonic-gate 		/*
32810Sstevel@tonic-gate 		 * Make sure the enable-override is set properly before we
32820Sstevel@tonic-gate 		 * read whether we should be enabled.
32830Sstevel@tonic-gate 		 */
32840Sstevel@tonic-gate 		if (milestone == MILESTONE_NONE ||
32850Sstevel@tonic-gate 		    !(v->gv_flags & GV_INSUBGRAPH)) {
32862747Sbustos 			/*
32872747Sbustos 			 * This might seem unjustified after the milestone
32882747Sbustos 			 * transition has completed (non_subgraph_svcs == 0),
32892747Sbustos 			 * but it's important because when we boot to
32902747Sbustos 			 * a milestone, we set the milestone before populating
32912747Sbustos 			 * the graph, and all of the new non-subgraph services
32922747Sbustos 			 * need to be disabled here.
32932747Sbustos 			 */
32940Sstevel@tonic-gate 			switch (err = libscf_set_enable_ovr(inst, 0)) {
32950Sstevel@tonic-gate 			case 0:
32960Sstevel@tonic-gate 				break;
32970Sstevel@tonic-gate 
32980Sstevel@tonic-gate 			case ECONNABORTED:
32990Sstevel@tonic-gate 			case ECANCELED:
33009333SRenaud.Manus@Sun.COM 				startd_free(restarter_fmri, max_scf_value_size);
33010Sstevel@tonic-gate 				return (err);
33020Sstevel@tonic-gate 
33030Sstevel@tonic-gate 			case EROFS:
33040Sstevel@tonic-gate 				log_error(LOG_WARNING,
33050Sstevel@tonic-gate 				    "Could not set %s/%s for %s: %s.\n",
33060Sstevel@tonic-gate 				    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
33070Sstevel@tonic-gate 				    v->gv_name, strerror(err));
33080Sstevel@tonic-gate 				break;
33090Sstevel@tonic-gate 
33100Sstevel@tonic-gate 			case EPERM:
33110Sstevel@tonic-gate 				uu_die("Permission denied.\n");
33120Sstevel@tonic-gate 				/* NOTREACHED */
33130Sstevel@tonic-gate 
33140Sstevel@tonic-gate 			default:
33150Sstevel@tonic-gate 				bad_error("libscf_set_enable_ovr", err);
33160Sstevel@tonic-gate 			}
33170Sstevel@tonic-gate 		} else {
33180Sstevel@tonic-gate 			assert(v->gv_flags & GV_INSUBGRAPH);
33190Sstevel@tonic-gate 			switch (err = libscf_delete_enable_ovr(inst)) {
33200Sstevel@tonic-gate 			case 0:
33210Sstevel@tonic-gate 				break;
33220Sstevel@tonic-gate 
33230Sstevel@tonic-gate 			case ECONNABORTED:
33240Sstevel@tonic-gate 			case ECANCELED:
33259333SRenaud.Manus@Sun.COM 				startd_free(restarter_fmri, max_scf_value_size);
33260Sstevel@tonic-gate 				return (err);
33270Sstevel@tonic-gate 
33280Sstevel@tonic-gate 			case EPERM:
33290Sstevel@tonic-gate 				uu_die("Permission denied.\n");
33300Sstevel@tonic-gate 				/* NOTREACHED */
33310Sstevel@tonic-gate 
33320Sstevel@tonic-gate 			default:
33330Sstevel@tonic-gate 				bad_error("libscf_delete_enable_ovr", err);
33340Sstevel@tonic-gate 			}
33350Sstevel@tonic-gate 		}
33360Sstevel@tonic-gate 	}
33370Sstevel@tonic-gate 
33380Sstevel@tonic-gate 	err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled,
33390Sstevel@tonic-gate 	    &enabled_ovr, &restarter_fmri);
33400Sstevel@tonic-gate 	switch (err) {
33410Sstevel@tonic-gate 	case 0:
33420Sstevel@tonic-gate 		break;
33430Sstevel@tonic-gate 
33440Sstevel@tonic-gate 	case ECONNABORTED:
33450Sstevel@tonic-gate 	case ECANCELED:
33460Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
33470Sstevel@tonic-gate 		return (err);
33480Sstevel@tonic-gate 
33490Sstevel@tonic-gate 	case ENOENT:
33500Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
33510Sstevel@tonic-gate 		    "Ignoring %s because it has no general property group.\n",
33520Sstevel@tonic-gate 		    v->gv_name);
33530Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
33540Sstevel@tonic-gate 		return (0);
33550Sstevel@tonic-gate 
33560Sstevel@tonic-gate 	default:
33570Sstevel@tonic-gate 		bad_error("libscf_get_basic_instance_data", err);
33580Sstevel@tonic-gate 	}
33590Sstevel@tonic-gate 
33600Sstevel@tonic-gate 	if (enabled == -1) {
33610Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
33620Sstevel@tonic-gate 		return (0);
33630Sstevel@tonic-gate 	}
33640Sstevel@tonic-gate 
33650Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) |
33660Sstevel@tonic-gate 	    (enabled ? GV_ENBLD_NOOVR : 0);
33670Sstevel@tonic-gate 
33680Sstevel@tonic-gate 	if (enabled_ovr != -1)
33690Sstevel@tonic-gate 		enabled = enabled_ovr;
33700Sstevel@tonic-gate 
33710Sstevel@tonic-gate 	v->gv_state = RESTARTER_STATE_UNINIT;
33720Sstevel@tonic-gate 
33730Sstevel@tonic-gate 	snap = libscf_get_or_make_running_snapshot(inst, v->gv_name, B_TRUE);
33740Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
33750Sstevel@tonic-gate 
33760Sstevel@tonic-gate 	/* Set up the restarter. (Sends _ADD_INSTANCE on success.) */
33770Sstevel@tonic-gate 	err = graph_change_restarter(v, restarter_fmri, h, &path);
33780Sstevel@tonic-gate 	if (err != 0) {
33790Sstevel@tonic-gate 		instance_data_t idata;
33800Sstevel@tonic-gate 		uint_t count = 0, msecs = ALLOC_DELAY;
33810Sstevel@tonic-gate 		const char *reason;
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate 		if (err == ECONNABORTED) {
33840Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
33850Sstevel@tonic-gate 			return (err);
33860Sstevel@tonic-gate 		}
33870Sstevel@tonic-gate 
33880Sstevel@tonic-gate 		assert(err == EINVAL || err == ELOOP);
33890Sstevel@tonic-gate 
33900Sstevel@tonic-gate 		if (err == EINVAL) {
33911958Slianep 			log_framework(LOG_ERR, emsg_invalid_restarter,
33929263SSean.Wilcox@Sun.COM 			    v->gv_name, restarter_fmri);
33930Sstevel@tonic-gate 			reason = "invalid_restarter";
33940Sstevel@tonic-gate 		} else {
33950Sstevel@tonic-gate 			handle_cycle(v->gv_name, path);
33960Sstevel@tonic-gate 			reason = "dependency_cycle";
33970Sstevel@tonic-gate 		}
33980Sstevel@tonic-gate 
33990Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
34000Sstevel@tonic-gate 
34010Sstevel@tonic-gate 		/*
34020Sstevel@tonic-gate 		 * We didn't register the instance with the restarter, so we
34030Sstevel@tonic-gate 		 * must set maintenance mode ourselves.
34040Sstevel@tonic-gate 		 */
34050Sstevel@tonic-gate 		err = libscf_instance_get_fmri(inst, (char **)&idata.i_fmri);
34060Sstevel@tonic-gate 		if (err != 0) {
34070Sstevel@tonic-gate 			assert(err == ECONNABORTED || err == ECANCELED);
34080Sstevel@tonic-gate 			return (err);
34090Sstevel@tonic-gate 		}
34100Sstevel@tonic-gate 
34110Sstevel@tonic-gate 		idata.i_state = RESTARTER_STATE_NONE;
34120Sstevel@tonic-gate 		idata.i_next_state = RESTARTER_STATE_NONE;
34130Sstevel@tonic-gate 
34140Sstevel@tonic-gate set_maint:
34150Sstevel@tonic-gate 		switch (err = _restarter_commit_states(h, &idata,
34160Sstevel@tonic-gate 		    RESTARTER_STATE_MAINT, RESTARTER_STATE_NONE, reason)) {
34170Sstevel@tonic-gate 		case 0:
34180Sstevel@tonic-gate 			break;
34190Sstevel@tonic-gate 
34200Sstevel@tonic-gate 		case ENOMEM:
34210Sstevel@tonic-gate 			++count;
34220Sstevel@tonic-gate 			if (count < ALLOC_RETRY) {
34230Sstevel@tonic-gate 				(void) poll(NULL, 0, msecs);
34240Sstevel@tonic-gate 				msecs *= ALLOC_DELAY_MULT;
34250Sstevel@tonic-gate 				goto set_maint;
34260Sstevel@tonic-gate 			}
34270Sstevel@tonic-gate 
34280Sstevel@tonic-gate 			uu_die("Insufficient memory.\n");
34290Sstevel@tonic-gate 			/* NOTREACHED */
34300Sstevel@tonic-gate 
34310Sstevel@tonic-gate 		case ECONNABORTED:
34322682Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
34330Sstevel@tonic-gate 			return (ECONNABORTED);
34340Sstevel@tonic-gate 
34350Sstevel@tonic-gate 		case ENOENT:
34362682Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
34370Sstevel@tonic-gate 			return (ECANCELED);
34380Sstevel@tonic-gate 
34390Sstevel@tonic-gate 		case EPERM:
34400Sstevel@tonic-gate 		case EACCES:
34410Sstevel@tonic-gate 		case EROFS:
34420Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not initialize state for "
34430Sstevel@tonic-gate 			    "%s: %s.\n", idata.i_fmri, strerror(err));
34440Sstevel@tonic-gate 			break;
34450Sstevel@tonic-gate 
34460Sstevel@tonic-gate 		case EINVAL:
34470Sstevel@tonic-gate 		default:
34480Sstevel@tonic-gate 			bad_error("_restarter_commit_states", err);
34490Sstevel@tonic-gate 		}
34500Sstevel@tonic-gate 
34510Sstevel@tonic-gate 		startd_free((void *)idata.i_fmri, max_scf_fmri_size);
34520Sstevel@tonic-gate 
34530Sstevel@tonic-gate 		v->gv_state = RESTARTER_STATE_MAINT;
34540Sstevel@tonic-gate 
34550Sstevel@tonic-gate 		goto out;
34560Sstevel@tonic-gate 	}
34570Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_value_size);
34580Sstevel@tonic-gate 
34590Sstevel@tonic-gate 	/* Add all the other dependencies. */
34600Sstevel@tonic-gate 	err = refresh_vertex(v, inst);
34610Sstevel@tonic-gate 	if (err != 0) {
34620Sstevel@tonic-gate 		assert(err == ECONNABORTED);
34630Sstevel@tonic-gate 		return (err);
34640Sstevel@tonic-gate 	}
34650Sstevel@tonic-gate 
34660Sstevel@tonic-gate out:
34670Sstevel@tonic-gate 	v->gv_flags |= GV_CONFIGURED;
34680Sstevel@tonic-gate 
34690Sstevel@tonic-gate 	graph_enable_by_vertex(v, enabled, 0);
34700Sstevel@tonic-gate 
34710Sstevel@tonic-gate 	return (0);
34720Sstevel@tonic-gate }
34730Sstevel@tonic-gate 
34748944Sdp@eng.sun.com 
34758944Sdp@eng.sun.com static void
34768944Sdp@eng.sun.com kill_user_procs(void)
34778944Sdp@eng.sun.com {
34788944Sdp@eng.sun.com 	(void) fputs("svc.startd: Killing user processes.\n", stdout);
34798944Sdp@eng.sun.com 
34808944Sdp@eng.sun.com 	/*
34818944Sdp@eng.sun.com 	 * Despite its name, killall's role is to get select user processes--
34828944Sdp@eng.sun.com 	 * basically those representing terminal-based logins-- to die.  Victims
34838944Sdp@eng.sun.com 	 * are located by killall in the utmp database.  Since these are most
34848944Sdp@eng.sun.com 	 * often shell based logins, and many shells mask SIGTERM (but are
34858944Sdp@eng.sun.com 	 * responsive to SIGHUP) we first HUP and then shortly thereafter
34868944Sdp@eng.sun.com 	 * kill -9.
34878944Sdp@eng.sun.com 	 */
34888944Sdp@eng.sun.com 	(void) fork_with_timeout("/usr/sbin/killall HUP", 1, 5);
34898944Sdp@eng.sun.com 	(void) fork_with_timeout("/usr/sbin/killall KILL", 1, 5);
34908944Sdp@eng.sun.com 
34918944Sdp@eng.sun.com 	/*
34928944Sdp@eng.sun.com 	 * Note the selection of user id's 0, 1 and 15, subsequently
34938944Sdp@eng.sun.com 	 * inverted by -v.  15 is reserved for dladmd.  Yes, this is a
34948944Sdp@eng.sun.com 	 * kludge-- a better policy is needed.
34958944Sdp@eng.sun.com 	 *
34968944Sdp@eng.sun.com 	 * Note that fork_with_timeout will only wait out the 1 second
34978944Sdp@eng.sun.com 	 * "grace time" if pkill actually returns 0.  So if there are
34988944Sdp@eng.sun.com 	 * no matches, this will run to completion much more quickly.
34998944Sdp@eng.sun.com 	 */
35008944Sdp@eng.sun.com 	(void) fork_with_timeout("/usr/bin/pkill -TERM -v -u 0,1,15", 1, 5);
35018944Sdp@eng.sun.com 	(void) fork_with_timeout("/usr/bin/pkill -KILL -v -u 0,1,15", 1, 5);
35028944Sdp@eng.sun.com }
35038944Sdp@eng.sun.com 
35040Sstevel@tonic-gate static void
35050Sstevel@tonic-gate do_uadmin(void)
35060Sstevel@tonic-gate {
35079160SSherry.Moore@Sun.COM 	const char * const resetting = "/etc/svc/volatile/resetting";
35088944Sdp@eng.sun.com 	int fd;
35090Sstevel@tonic-gate 	struct statvfs vfs;
35108944Sdp@eng.sun.com 	time_t now;
35118944Sdp@eng.sun.com 	struct tm nowtm;
35128944Sdp@eng.sun.com 	char down_buf[256], time_buf[256];
35139160SSherry.Moore@Sun.COM 	uintptr_t mdep;
35149160SSherry.Moore@Sun.COM #if defined(__i386)
35159160SSherry.Moore@Sun.COM 	grub_boot_args_t fbarg;
35169160SSherry.Moore@Sun.COM #endif	/* __i386 */
35179160SSherry.Moore@Sun.COM 
35189160SSherry.Moore@Sun.COM 	mdep = NULL;
35190Sstevel@tonic-gate 	fd = creat(resetting, 0777);
35200Sstevel@tonic-gate 	if (fd >= 0)
35210Sstevel@tonic-gate 		startd_close(fd);
35220Sstevel@tonic-gate 	else
35230Sstevel@tonic-gate 		uu_warn("Could not create \"%s\"", resetting);
35240Sstevel@tonic-gate 
35250Sstevel@tonic-gate 	/* Kill dhcpagent if we're not using nfs for root */
35260Sstevel@tonic-gate 	if ((statvfs("/", &vfs) == 0) &&
35270Sstevel@tonic-gate 	    (strncmp(vfs.f_basetype, "nfs", sizeof ("nfs") - 1) != 0))
35288944Sdp@eng.sun.com 		fork_with_timeout("/usr/bin/pkill -x -u 0 dhcpagent", 0, 5);
35298944Sdp@eng.sun.com 
35308944Sdp@eng.sun.com 	/*
35318944Sdp@eng.sun.com 	 * Call sync(2) now, before we kill off user processes.  This takes
35328944Sdp@eng.sun.com 	 * advantage of the several seconds of pause we have before the
35338944Sdp@eng.sun.com 	 * killalls are done.  Time we can make good use of to get pages
35348944Sdp@eng.sun.com 	 * moving out to disk.
35358944Sdp@eng.sun.com 	 *
35368944Sdp@eng.sun.com 	 * Inside non-global zones, we don't bother, and it's better not to
35378944Sdp@eng.sun.com 	 * anyway, since sync(2) can have system-wide impact.
35388944Sdp@eng.sun.com 	 */
35398944Sdp@eng.sun.com 	if (getzoneid() == 0)
35408944Sdp@eng.sun.com 		sync();
35418944Sdp@eng.sun.com 
35428944Sdp@eng.sun.com 	kill_user_procs();
35438944Sdp@eng.sun.com 
35448944Sdp@eng.sun.com 	/*
35458944Sdp@eng.sun.com 	 * Note that this must come after the killing of user procs, since
35468944Sdp@eng.sun.com 	 * killall relies on utmpx, and this command affects the contents of
35478944Sdp@eng.sun.com 	 * said file.
35488944Sdp@eng.sun.com 	 */
35498944Sdp@eng.sun.com 	if (access("/usr/lib/acct/closewtmp", X_OK) == 0)
35508944Sdp@eng.sun.com 		fork_with_timeout("/usr/lib/acct/closewtmp", 0, 5);
35518944Sdp@eng.sun.com 
35528944Sdp@eng.sun.com 	/*
35538944Sdp@eng.sun.com 	 * For patches which may be installed as the system is shutting
35548944Sdp@eng.sun.com 	 * down, we need to ensure, one more time, that the boot archive
35558944Sdp@eng.sun.com 	 * really is up to date.
35568944Sdp@eng.sun.com 	 */
35578944Sdp@eng.sun.com 	if (getzoneid() == 0 && access("/usr/sbin/bootadm", X_OK) == 0)
35588944Sdp@eng.sun.com 		fork_with_timeout("/usr/sbin/bootadm -ea update_all", 0, 3600);
35598944Sdp@eng.sun.com 
35609479SSherry.Moore@Sun.COM 	/*
35619479SSherry.Moore@Sun.COM 	 * Right now, fast reboot is supported only on i386.
35629479SSherry.Moore@Sun.COM 	 * scf_is_fastboot_default() should take care of it.
35639479SSherry.Moore@Sun.COM 	 * If somehow we got there on unsupported platform -
35649479SSherry.Moore@Sun.COM 	 * print warning and fall back to regular reboot.
35659479SSherry.Moore@Sun.COM 	 */
35669479SSherry.Moore@Sun.COM 	if (halting == AD_FASTREBOOT) {
35679479SSherry.Moore@Sun.COM #if defined(__i386)
35689479SSherry.Moore@Sun.COM 		int rc;
35699479SSherry.Moore@Sun.COM 
35709479SSherry.Moore@Sun.COM 		if ((rc = grub_get_boot_args(&fbarg, NULL,
35719479SSherry.Moore@Sun.COM 		    GRUB_ENTRY_DEFAULT)) == 0) {
35729479SSherry.Moore@Sun.COM 			mdep = (uintptr_t)&fbarg.gba_bootargs;
35739479SSherry.Moore@Sun.COM 		} else {
35749479SSherry.Moore@Sun.COM 			/*
35759479SSherry.Moore@Sun.COM 			 * Failed to read GRUB menu, fall back to normal reboot
35769479SSherry.Moore@Sun.COM 			 */
35779479SSherry.Moore@Sun.COM 			halting = AD_BOOT;
35789479SSherry.Moore@Sun.COM 			uu_warn("Failed to process GRUB menu entry "
35799479SSherry.Moore@Sun.COM 			    "for fast reboot.\n\t%s\n"
35809479SSherry.Moore@Sun.COM 			    "Falling back to regular reboot.\n",
35819479SSherry.Moore@Sun.COM 			    grub_strerror(rc));
35829479SSherry.Moore@Sun.COM 		}
35839479SSherry.Moore@Sun.COM #else	/* __i386 */
35849479SSherry.Moore@Sun.COM 		halting = AD_BOOT;
35859479SSherry.Moore@Sun.COM 		uu_warn("Fast reboot configured, but not supported by "
35869479SSherry.Moore@Sun.COM 		    "this ISA\n");
35879479SSherry.Moore@Sun.COM #endif	/* __i386 */
35889479SSherry.Moore@Sun.COM 	}
35899479SSherry.Moore@Sun.COM 
35908944Sdp@eng.sun.com 	fork_with_timeout("/sbin/umountall -l", 0, 5);
35918944Sdp@eng.sun.com 	fork_with_timeout("/sbin/umount /tmp /var/adm /var/run /var "
35928944Sdp@eng.sun.com 	    ">/dev/null 2>&1", 0, 5);
35938944Sdp@eng.sun.com 
35948944Sdp@eng.sun.com 	/*
35958944Sdp@eng.sun.com 	 * Try to get to consistency for whatever UFS filesystems are left.
35968944Sdp@eng.sun.com 	 * This is pretty expensive, so we save it for the end in the hopes of
35978944Sdp@eng.sun.com 	 * minimizing what it must do.  The other option would be to start in
35988944Sdp@eng.sun.com 	 * parallel with the killall's, but lockfs tends to throw out much more
35998944Sdp@eng.sun.com 	 * than is needed, and so subsequent commands (like umountall) take a
36008944Sdp@eng.sun.com 	 * long time to get going again.
36018944Sdp@eng.sun.com 	 *
36028944Sdp@eng.sun.com 	 * Inside of zones, we don't bother, since we're not about to terminate
36038944Sdp@eng.sun.com 	 * the whole OS instance.
36048944Sdp@eng.sun.com 	 *
36058944Sdp@eng.sun.com 	 * On systems using only ZFS, this call to lockfs -fa is a no-op.
36068944Sdp@eng.sun.com 	 */
36078944Sdp@eng.sun.com 	if (getzoneid() == 0) {
36088944Sdp@eng.sun.com 		if (access("/usr/sbin/lockfs", X_OK) == 0)
36098944Sdp@eng.sun.com 			fork_with_timeout("/usr/sbin/lockfs -fa", 0, 30);
36108944Sdp@eng.sun.com 
36118944Sdp@eng.sun.com 		sync();	/* once more, with feeling */
36128944Sdp@eng.sun.com 	}
36138944Sdp@eng.sun.com 
36148944Sdp@eng.sun.com 	fork_with_timeout("/sbin/umount /usr >/dev/null 2>&1", 0, 5);
36158944Sdp@eng.sun.com 
36168944Sdp@eng.sun.com 	/*
36178944Sdp@eng.sun.com 	 * Construct and emit the last words from userland:
36188944Sdp@eng.sun.com 	 * "<timestamp> The system is down.  Shutdown took <N> seconds."
36198944Sdp@eng.sun.com 	 *
36208944Sdp@eng.sun.com 	 * Normally we'd use syslog, but with /var and other things
36218944Sdp@eng.sun.com 	 * potentially gone, try to minimize the external dependencies.
36228944Sdp@eng.sun.com 	 */
36238944Sdp@eng.sun.com 	now = time(NULL);
36248944Sdp@eng.sun.com 	(void) localtime_r(&now, &nowtm);
36258944Sdp@eng.sun.com 
36268944Sdp@eng.sun.com 	if (strftime(down_buf, sizeof (down_buf),
36278944Sdp@eng.sun.com 	    "%b %e %T The system is down.", &nowtm) == 0) {
36288944Sdp@eng.sun.com 		(void) strlcpy(down_buf, "The system is down.",
36298944Sdp@eng.sun.com 		    sizeof (down_buf));
36308944Sdp@eng.sun.com 	}
36318944Sdp@eng.sun.com 
36328944Sdp@eng.sun.com 	if (halting_time != 0 && halting_time <= now) {
36338944Sdp@eng.sun.com 		(void) snprintf(time_buf, sizeof (time_buf),
36348944Sdp@eng.sun.com 		    "  Shutdown took %lu seconds.", now - halting_time);
36358944Sdp@eng.sun.com 	} else {
36368944Sdp@eng.sun.com 		time_buf[0] = '\0';
36378944Sdp@eng.sun.com 	}
36388944Sdp@eng.sun.com 	(void) printf("%s%s\n", down_buf, time_buf);
36390Sstevel@tonic-gate 
36409160SSherry.Moore@Sun.COM 	(void) uadmin(A_SHUTDOWN, halting, mdep);
36410Sstevel@tonic-gate 	uu_warn("uadmin() failed");
36420Sstevel@tonic-gate 
36439160SSherry.Moore@Sun.COM #if defined(__i386)
36449160SSherry.Moore@Sun.COM 	/* uadmin fail, cleanup grub_boot_args */
36459160SSherry.Moore@Sun.COM 	if (halting == AD_FASTREBOOT)
36469160SSherry.Moore@Sun.COM 		grub_cleanup_boot_args(&fbarg);
36479160SSherry.Moore@Sun.COM #endif	/* __i386 */
36489160SSherry.Moore@Sun.COM 
36490Sstevel@tonic-gate 	if (remove(resetting) != 0 && errno != ENOENT)
36500Sstevel@tonic-gate 		uu_warn("Could not remove \"%s\"", resetting);
36510Sstevel@tonic-gate }
36520Sstevel@tonic-gate 
36530Sstevel@tonic-gate /*
36540Sstevel@tonic-gate  * If any of the up_svcs[] are online or satisfiable, return true.  If they are
36550Sstevel@tonic-gate  * all missing, disabled, in maintenance, or unsatisfiable, return false.
36560Sstevel@tonic-gate  */
36570Sstevel@tonic-gate boolean_t
36580Sstevel@tonic-gate can_come_up(void)
36590Sstevel@tonic-gate {
36600Sstevel@tonic-gate 	int i;
36610Sstevel@tonic-gate 
366211466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
36630Sstevel@tonic-gate 
36640Sstevel@tonic-gate 	/*
36650Sstevel@tonic-gate 	 * If we are booting to single user (boot -s),
36660Sstevel@tonic-gate 	 * SCF_MILESTONE_SINGLE_USER is needed to come up because startd
36670Sstevel@tonic-gate 	 * spawns sulogin after single-user is online (see specials.c).
36680Sstevel@tonic-gate 	 */
36690Sstevel@tonic-gate 	i = (booting_to_single_user ? 0 : 1);
36700Sstevel@tonic-gate 
36710Sstevel@tonic-gate 	for (; up_svcs[i] != NULL; ++i) {
36720Sstevel@tonic-gate 		if (up_svcs_p[i] == NULL) {
36730Sstevel@tonic-gate 			up_svcs_p[i] = vertex_get_by_name(up_svcs[i]);
36740Sstevel@tonic-gate 
36750Sstevel@tonic-gate 			if (up_svcs_p[i] == NULL)
36760Sstevel@tonic-gate 				continue;
36770Sstevel@tonic-gate 		}
36780Sstevel@tonic-gate 
36790Sstevel@tonic-gate 		/*
36800Sstevel@tonic-gate 		 * Ignore unconfigured services (the ones that have been
36810Sstevel@tonic-gate 		 * mentioned in a dependency from other services, but do
36820Sstevel@tonic-gate 		 * not exist in the repository).  Services which exist
36830Sstevel@tonic-gate 		 * in the repository but don't have general/enabled
36840Sstevel@tonic-gate 		 * property will be also ignored.
36850Sstevel@tonic-gate 		 */
36860Sstevel@tonic-gate 		if (!(up_svcs_p[i]->gv_flags & GV_CONFIGURED))
36870Sstevel@tonic-gate 			continue;
36880Sstevel@tonic-gate 
36890Sstevel@tonic-gate 		switch (up_svcs_p[i]->gv_state) {
36900Sstevel@tonic-gate 		case RESTARTER_STATE_ONLINE:
36910Sstevel@tonic-gate 		case RESTARTER_STATE_DEGRADED:
36920Sstevel@tonic-gate 			/*
36930Sstevel@tonic-gate 			 * Deactivate verbose boot once a login service has been
36940Sstevel@tonic-gate 			 * reached.
36950Sstevel@tonic-gate 			 */
36960Sstevel@tonic-gate 			st->st_log_login_reached = 1;
36970Sstevel@tonic-gate 			/*FALLTHROUGH*/
36980Sstevel@tonic-gate 		case RESTARTER_STATE_UNINIT:
36990Sstevel@tonic-gate 			return (B_TRUE);
37000Sstevel@tonic-gate 
37010Sstevel@tonic-gate 		case RESTARTER_STATE_OFFLINE:
37020Sstevel@tonic-gate 			if (instance_satisfied(up_svcs_p[i], B_TRUE) != -1)
37030Sstevel@tonic-gate 				return (B_TRUE);
37040Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
37050Sstevel@tonic-gate 			    "can_come_up(): %s is unsatisfiable.\n",
37060Sstevel@tonic-gate 			    up_svcs_p[i]->gv_name);
37070Sstevel@tonic-gate 			continue;
37080Sstevel@tonic-gate 
37090Sstevel@tonic-gate 		case RESTARTER_STATE_DISABLED:
37100Sstevel@tonic-gate 		case RESTARTER_STATE_MAINT:
37110Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
37120Sstevel@tonic-gate 			    "can_come_up(): %s is in state %s.\n",
37130Sstevel@tonic-gate 			    up_svcs_p[i]->gv_name,
37140Sstevel@tonic-gate 			    instance_state_str[up_svcs_p[i]->gv_state]);
37150Sstevel@tonic-gate 			continue;
37160Sstevel@tonic-gate 
37170Sstevel@tonic-gate 		default:
37180Sstevel@tonic-gate #ifndef NDEBUG
37190Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex state %d.\n",
37200Sstevel@tonic-gate 			    __FILE__, __LINE__, up_svcs_p[i]->gv_state);
37210Sstevel@tonic-gate #endif
37220Sstevel@tonic-gate 			abort();
37230Sstevel@tonic-gate 		}
37240Sstevel@tonic-gate 	}
37250Sstevel@tonic-gate 
37260Sstevel@tonic-gate 	/*
37270Sstevel@tonic-gate 	 * In the seed repository, console-login is unsatisfiable because
37280Sstevel@tonic-gate 	 * services are missing.  To behave correctly in that case we don't want
37290Sstevel@tonic-gate 	 * to return false until manifest-import is online.
37300Sstevel@tonic-gate 	 */
37310Sstevel@tonic-gate 
37320Sstevel@tonic-gate 	if (manifest_import_p == NULL) {
37330Sstevel@tonic-gate 		manifest_import_p = vertex_get_by_name(manifest_import);
37340Sstevel@tonic-gate 
37350Sstevel@tonic-gate 		if (manifest_import_p == NULL)
37360Sstevel@tonic-gate 			return (B_FALSE);
37370Sstevel@tonic-gate 	}
37380Sstevel@tonic-gate 
37390Sstevel@tonic-gate 	switch (manifest_import_p->gv_state) {
37400Sstevel@tonic-gate 	case RESTARTER_STATE_ONLINE:
37410Sstevel@tonic-gate 	case RESTARTER_STATE_DEGRADED:
37420Sstevel@tonic-gate 	case RESTARTER_STATE_DISABLED:
37430Sstevel@tonic-gate 	case RESTARTER_STATE_MAINT:
37440Sstevel@tonic-gate 		break;
37450Sstevel@tonic-gate 
37460Sstevel@tonic-gate 	case RESTARTER_STATE_OFFLINE:
37470Sstevel@tonic-gate 		if (instance_satisfied(manifest_import_p, B_TRUE) == -1)
37480Sstevel@tonic-gate 			break;
37490Sstevel@tonic-gate 		/* FALLTHROUGH */
37500Sstevel@tonic-gate 
37510Sstevel@tonic-gate 	case RESTARTER_STATE_UNINIT:
37520Sstevel@tonic-gate 		return (B_TRUE);
37530Sstevel@tonic-gate 	}
37540Sstevel@tonic-gate 
37550Sstevel@tonic-gate 	return (B_FALSE);
37560Sstevel@tonic-gate }
37570Sstevel@tonic-gate 
37580Sstevel@tonic-gate /*
37590Sstevel@tonic-gate  * Runs sulogin.  Returns
37600Sstevel@tonic-gate  *   0 - success
37610Sstevel@tonic-gate  *   EALREADY - sulogin is already running
37620Sstevel@tonic-gate  *   EBUSY - console-login is running
37630Sstevel@tonic-gate  */
37640Sstevel@tonic-gate static int
37650Sstevel@tonic-gate run_sulogin(const char *msg)
37660Sstevel@tonic-gate {
37670Sstevel@tonic-gate 	graph_vertex_t *v;
37680Sstevel@tonic-gate 
376911466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
37700Sstevel@tonic-gate 
37710Sstevel@tonic-gate 	if (sulogin_running)
37720Sstevel@tonic-gate 		return (EALREADY);
37730Sstevel@tonic-gate 
37740Sstevel@tonic-gate 	v = vertex_get_by_name(console_login_fmri);
37750Sstevel@tonic-gate 	if (v != NULL && inst_running(v))
37760Sstevel@tonic-gate 		return (EBUSY);
37770Sstevel@tonic-gate 
37780Sstevel@tonic-gate 	sulogin_running = B_TRUE;
37790Sstevel@tonic-gate 
37800Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
37810Sstevel@tonic-gate 
37820Sstevel@tonic-gate 	fork_sulogin(B_FALSE, msg);
37830Sstevel@tonic-gate 
37840Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
37850Sstevel@tonic-gate 
37860Sstevel@tonic-gate 	sulogin_running = B_FALSE;
37870Sstevel@tonic-gate 
37880Sstevel@tonic-gate 	if (console_login_ready) {
37890Sstevel@tonic-gate 		v = vertex_get_by_name(console_login_fmri);
37900Sstevel@tonic-gate 
37919333SRenaud.Manus@Sun.COM 		if (v != NULL && v->gv_state == RESTARTER_STATE_OFFLINE) {
37920Sstevel@tonic-gate 			if (v->gv_start_f == NULL)
37930Sstevel@tonic-gate 				vertex_send_event(v,
37940Sstevel@tonic-gate 				    RESTARTER_EVENT_TYPE_START);
37950Sstevel@tonic-gate 			else
37960Sstevel@tonic-gate 				v->gv_start_f(v);
37970Sstevel@tonic-gate 		}
37980Sstevel@tonic-gate 
37990Sstevel@tonic-gate 		console_login_ready = B_FALSE;
38000Sstevel@tonic-gate 	}
38010Sstevel@tonic-gate 
38020Sstevel@tonic-gate 	return (0);
38030Sstevel@tonic-gate }
38040Sstevel@tonic-gate 
38050Sstevel@tonic-gate /*
38060Sstevel@tonic-gate  * The sulogin thread runs sulogin while can_come_up() is false.  run_sulogin()
38070Sstevel@tonic-gate  * keeps sulogin from stepping on console-login's toes.
38080Sstevel@tonic-gate  */
38090Sstevel@tonic-gate /* ARGSUSED */
38100Sstevel@tonic-gate static void *
38110Sstevel@tonic-gate sulogin_thread(void *unused)
38120Sstevel@tonic-gate {
38130Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
38140Sstevel@tonic-gate 
38150Sstevel@tonic-gate 	assert(sulogin_thread_running);
38160Sstevel@tonic-gate 
38175040Swesolows 	do {
38180Sstevel@tonic-gate 		(void) run_sulogin("Console login service(s) cannot run\n");
38195040Swesolows 	} while (!can_come_up());
38200Sstevel@tonic-gate 
38210Sstevel@tonic-gate 	sulogin_thread_running = B_FALSE;
38220Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
38230Sstevel@tonic-gate 
38240Sstevel@tonic-gate 	return (NULL);
38250Sstevel@tonic-gate }
38260Sstevel@tonic-gate 
38270Sstevel@tonic-gate /* ARGSUSED */
38280Sstevel@tonic-gate void *
38290Sstevel@tonic-gate single_user_thread(void *unused)
38300Sstevel@tonic-gate {
38310Sstevel@tonic-gate 	uint_t left;
38320Sstevel@tonic-gate 	scf_handle_t *h;
38330Sstevel@tonic-gate 	scf_instance_t *inst;
38340Sstevel@tonic-gate 	scf_property_t *prop;
38350Sstevel@tonic-gate 	scf_value_t *val;
38360Sstevel@tonic-gate 	const char *msg;
38370Sstevel@tonic-gate 	char *buf;
38380Sstevel@tonic-gate 	int r;
38390Sstevel@tonic-gate 
38400Sstevel@tonic-gate 	MUTEX_LOCK(&single_user_thread_lock);
38410Sstevel@tonic-gate 	single_user_thread_count++;
38420Sstevel@tonic-gate 
38438944Sdp@eng.sun.com 	if (!booting_to_single_user)
38448944Sdp@eng.sun.com 		kill_user_procs();
38450Sstevel@tonic-gate 
38460Sstevel@tonic-gate 	if (go_single_user_mode || booting_to_single_user) {
38470Sstevel@tonic-gate 		msg = "SINGLE USER MODE\n";
38480Sstevel@tonic-gate 	} else {
38490Sstevel@tonic-gate 		assert(go_to_level1);
38500Sstevel@tonic-gate 
38510Sstevel@tonic-gate 		fork_rc_script('1', "start", B_TRUE);
38520Sstevel@tonic-gate 
38530Sstevel@tonic-gate 		uu_warn("The system is ready for administration.\n");
38540Sstevel@tonic-gate 
38550Sstevel@tonic-gate 		msg = "";
38560Sstevel@tonic-gate 	}
38570Sstevel@tonic-gate 
38580Sstevel@tonic-gate 	MUTEX_UNLOCK(&single_user_thread_lock);
38590Sstevel@tonic-gate 
38600Sstevel@tonic-gate 	for (;;) {
38610Sstevel@tonic-gate 		MUTEX_LOCK(&dgraph_lock);
38620Sstevel@tonic-gate 		r = run_sulogin(msg);
38630Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
38640Sstevel@tonic-gate 		if (r == 0)
38650Sstevel@tonic-gate 			break;
38660Sstevel@tonic-gate 
38670Sstevel@tonic-gate 		assert(r == EALREADY || r == EBUSY);
38680Sstevel@tonic-gate 
38690Sstevel@tonic-gate 		left = 3;
38700Sstevel@tonic-gate 		while (left > 0)
38710Sstevel@tonic-gate 			left = sleep(left);
38720Sstevel@tonic-gate 	}
38730Sstevel@tonic-gate 
38740Sstevel@tonic-gate 	MUTEX_LOCK(&single_user_thread_lock);
38750Sstevel@tonic-gate 
38760Sstevel@tonic-gate 	/*
38770Sstevel@tonic-gate 	 * If another single user thread has started, let it finish changing
38780Sstevel@tonic-gate 	 * the run level.
38790Sstevel@tonic-gate 	 */
38800Sstevel@tonic-gate 	if (single_user_thread_count > 1) {
38810Sstevel@tonic-gate 		single_user_thread_count--;
38820Sstevel@tonic-gate 		MUTEX_UNLOCK(&single_user_thread_lock);
38830Sstevel@tonic-gate 		return (NULL);
38840Sstevel@tonic-gate 	}
38850Sstevel@tonic-gate 
38860Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
38870Sstevel@tonic-gate 	inst = scf_instance_create(h);
38880Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
38890Sstevel@tonic-gate 	val = safe_scf_value_create(h);
38900Sstevel@tonic-gate 	buf = startd_alloc(max_scf_fmri_size);
38910Sstevel@tonic-gate 
38920Sstevel@tonic-gate lookup:
38930Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
38940Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
38950Sstevel@tonic-gate 		switch (scf_error()) {
38960Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
38970Sstevel@tonic-gate 			r = libscf_create_self(h);
38980Sstevel@tonic-gate 			if (r == 0)
38990Sstevel@tonic-gate 				goto lookup;
39000Sstevel@tonic-gate 			assert(r == ECONNABORTED);
39010Sstevel@tonic-gate 			/* FALLTHROUGH */
39020Sstevel@tonic-gate 
39030Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
39040Sstevel@tonic-gate 			libscf_handle_rebind(h);
39050Sstevel@tonic-gate 			goto lookup;
39060Sstevel@tonic-gate 
39070Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
39080Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
39090Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
39100Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
39110Sstevel@tonic-gate 		default:
39120Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
39130Sstevel@tonic-gate 		}
39140Sstevel@tonic-gate 	}
39150Sstevel@tonic-gate 
39160Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
39170Sstevel@tonic-gate 
39188823STruong.Q.Nguyen@Sun.COM 	r = scf_instance_delete_prop(inst, SCF_PG_OPTIONS_OVR,
39190Sstevel@tonic-gate 	    SCF_PROPERTY_MILESTONE);
39200Sstevel@tonic-gate 	switch (r) {
39210Sstevel@tonic-gate 	case 0:
39220Sstevel@tonic-gate 	case ECANCELED:
39230Sstevel@tonic-gate 		break;
39240Sstevel@tonic-gate 
39250Sstevel@tonic-gate 	case ECONNABORTED:
39260Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
39270Sstevel@tonic-gate 		libscf_handle_rebind(h);
39280Sstevel@tonic-gate 		goto lookup;
39290Sstevel@tonic-gate 
39300Sstevel@tonic-gate 	case EPERM:
39310Sstevel@tonic-gate 	case EACCES:
39320Sstevel@tonic-gate 	case EROFS:
39330Sstevel@tonic-gate 		log_error(LOG_WARNING, "Could not clear temporary milestone: "
39340Sstevel@tonic-gate 		    "%s.\n", strerror(r));
39350Sstevel@tonic-gate 		break;
39360Sstevel@tonic-gate 
39370Sstevel@tonic-gate 	default:
39388823STruong.Q.Nguyen@Sun.COM 		bad_error("scf_instance_delete_prop", r);
39390Sstevel@tonic-gate 	}
39400Sstevel@tonic-gate 
39410Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
39420Sstevel@tonic-gate 
39430Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, buf, max_scf_fmri_size);
39440Sstevel@tonic-gate 	switch (r) {
39450Sstevel@tonic-gate 	case ECANCELED:
39460Sstevel@tonic-gate 	case ENOENT:
39470Sstevel@tonic-gate 	case EINVAL:
39480Sstevel@tonic-gate 		(void) strcpy(buf, "all");
39490Sstevel@tonic-gate 		/* FALLTHROUGH */
39500Sstevel@tonic-gate 
39510Sstevel@tonic-gate 	case 0:
39520Sstevel@tonic-gate 		uu_warn("Returning to milestone %s.\n", buf);
39530Sstevel@tonic-gate 		break;
39540Sstevel@tonic-gate 
39550Sstevel@tonic-gate 	case ECONNABORTED:
39560Sstevel@tonic-gate 		libscf_handle_rebind(h);
39570Sstevel@tonic-gate 		goto lookup;
39580Sstevel@tonic-gate 
39590Sstevel@tonic-gate 	default:
39600Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
39610Sstevel@tonic-gate 	}
39620Sstevel@tonic-gate 
39630Sstevel@tonic-gate 	r = dgraph_set_milestone(buf, h, B_FALSE);
39640Sstevel@tonic-gate 	switch (r) {
39650Sstevel@tonic-gate 	case 0:
39660Sstevel@tonic-gate 	case ECONNRESET:
39670Sstevel@tonic-gate 	case EALREADY:
39680Sstevel@tonic-gate 	case EINVAL:
39690Sstevel@tonic-gate 	case ENOENT:
39700Sstevel@tonic-gate 		break;
39710Sstevel@tonic-gate 
39720Sstevel@tonic-gate 	default:
39730Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
39740Sstevel@tonic-gate 	}
39750Sstevel@tonic-gate 
39760Sstevel@tonic-gate 	/*
39770Sstevel@tonic-gate 	 * See graph_runlevel_changed().
39780Sstevel@tonic-gate 	 */
39790Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
39800Sstevel@tonic-gate 	utmpx_set_runlevel(target_milestone_as_runlevel(), 'S', B_TRUE);
39810Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
39820Sstevel@tonic-gate 
39830Sstevel@tonic-gate 	startd_free(buf, max_scf_fmri_size);
39840Sstevel@tonic-gate 	scf_value_destroy(val);
39850Sstevel@tonic-gate 	scf_property_destroy(prop);
39860Sstevel@tonic-gate 	scf_instance_destroy(inst);
39870Sstevel@tonic-gate 	scf_handle_destroy(h);
39880Sstevel@tonic-gate 
39890Sstevel@tonic-gate 	/*
39900Sstevel@tonic-gate 	 * We'll give ourselves 3 seconds to respond to all of the enablings
39910Sstevel@tonic-gate 	 * that setting the milestone should have created before checking
39920Sstevel@tonic-gate 	 * whether to run sulogin.
39930Sstevel@tonic-gate 	 */
39940Sstevel@tonic-gate 	left = 3;
39950Sstevel@tonic-gate 	while (left > 0)
39960Sstevel@tonic-gate 		left = sleep(left);
39970Sstevel@tonic-gate 
39980Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
39990Sstevel@tonic-gate 	/*
40000Sstevel@tonic-gate 	 * Clearing these variables will allow the sulogin thread to run.  We
40010Sstevel@tonic-gate 	 * check here in case there aren't any more state updates anytime soon.
40020Sstevel@tonic-gate 	 */
40030Sstevel@tonic-gate 	go_to_level1 = go_single_user_mode = booting_to_single_user = B_FALSE;
40040Sstevel@tonic-gate 	if (!sulogin_thread_running && !can_come_up()) {
40050Sstevel@tonic-gate 		(void) startd_thread_create(sulogin_thread, NULL);
40060Sstevel@tonic-gate 		sulogin_thread_running = B_TRUE;
40070Sstevel@tonic-gate 	}
40080Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
40090Sstevel@tonic-gate 	single_user_thread_count--;
40100Sstevel@tonic-gate 	MUTEX_UNLOCK(&single_user_thread_lock);
40110Sstevel@tonic-gate 	return (NULL);
40120Sstevel@tonic-gate }
40130Sstevel@tonic-gate 
40140Sstevel@tonic-gate 
40150Sstevel@tonic-gate /*
40160Sstevel@tonic-gate  * Dependency graph operations API.  These are handle-independent thread-safe
40170Sstevel@tonic-gate  * graph manipulation functions which are the entry points for the event
40180Sstevel@tonic-gate  * threads below.
40190Sstevel@tonic-gate  */
40200Sstevel@tonic-gate 
40210Sstevel@tonic-gate /*
40220Sstevel@tonic-gate  * If a configured vertex exists for inst_fmri, return EEXIST.  If no vertex
40230Sstevel@tonic-gate  * exists for inst_fmri, add one.  Then fetch the restarter from inst, make
40240Sstevel@tonic-gate  * this vertex dependent on it, and send _ADD_INSTANCE to the restarter.
40250Sstevel@tonic-gate  * Fetch whether the instance should be enabled from inst and send _ENABLE or
40260Sstevel@tonic-gate  * _DISABLE as appropriate.  Finally rummage through inst's dependency
40270Sstevel@tonic-gate  * property groups and add vertices and edges as appropriate.  If anything
40280Sstevel@tonic-gate  * goes wrong after sending _ADD_INSTANCE, send _ADMIN_MAINT_ON to put the
40290Sstevel@tonic-gate  * instance in maintenance.  Don't send _START or _STOP until we get a state
40300Sstevel@tonic-gate  * update in case we're being restarted and the service is already running.
40310Sstevel@tonic-gate  *
40320Sstevel@tonic-gate  * To support booting to a milestone, we must also make sure all dependencies
40330Sstevel@tonic-gate  * encountered are configured, if they exist in the repository.
40340Sstevel@tonic-gate  *
40350Sstevel@tonic-gate  * Returns 0 on success, ECONNABORTED on repository disconnection, EINVAL if
40360Sstevel@tonic-gate  * inst_fmri is an invalid (or not canonical) FMRI, ECANCELED if inst is
40370Sstevel@tonic-gate  * deleted, or EEXIST if a configured vertex for inst_fmri already exists.
40380Sstevel@tonic-gate  */
40390Sstevel@tonic-gate int
40400Sstevel@tonic-gate dgraph_add_instance(const char *inst_fmri, scf_instance_t *inst,
40410Sstevel@tonic-gate     boolean_t lock_graph)
40420Sstevel@tonic-gate {
40430Sstevel@tonic-gate 	graph_vertex_t *v;
40440Sstevel@tonic-gate 	int err;
40450Sstevel@tonic-gate 
40460Sstevel@tonic-gate 	if (strcmp(inst_fmri, SCF_SERVICE_STARTD) == 0)
40470Sstevel@tonic-gate 		return (0);
40480Sstevel@tonic-gate 
40490Sstevel@tonic-gate 	/* Check for a vertex for inst_fmri. */
40500Sstevel@tonic-gate 	if (lock_graph) {
40510Sstevel@tonic-gate 		MUTEX_LOCK(&dgraph_lock);
40520Sstevel@tonic-gate 	} else {
405311466SRoger.Faulkner@Sun.COM 		assert(MUTEX_HELD(&dgraph_lock));
40540Sstevel@tonic-gate 	}
40550Sstevel@tonic-gate 
40560Sstevel@tonic-gate 	v = vertex_get_by_name(inst_fmri);
40570Sstevel@tonic-gate 
40580Sstevel@tonic-gate 	if (v != NULL) {
40590Sstevel@tonic-gate 		assert(v->gv_type == GVT_INST);
40600Sstevel@tonic-gate 
40610Sstevel@tonic-gate 		if (v->gv_flags & GV_CONFIGURED) {
40620Sstevel@tonic-gate 			if (lock_graph)
40630Sstevel@tonic-gate 				MUTEX_UNLOCK(&dgraph_lock);
40640Sstevel@tonic-gate 			return (EEXIST);
40650Sstevel@tonic-gate 		}
40660Sstevel@tonic-gate 	} else {
40670Sstevel@tonic-gate 		/* Add the vertex. */
40680Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(inst_fmri, GVT_INST, 0,
40690Sstevel@tonic-gate 		    RERR_NONE, &v);
40700Sstevel@tonic-gate 		if (err != 0) {
40710Sstevel@tonic-gate 			assert(err == EINVAL);
40720Sstevel@tonic-gate 			if (lock_graph)
40730Sstevel@tonic-gate 				MUTEX_UNLOCK(&dgraph_lock);
40740Sstevel@tonic-gate 			return (EINVAL);
40750Sstevel@tonic-gate 		}
40760Sstevel@tonic-gate 	}
40770Sstevel@tonic-gate 
40780Sstevel@tonic-gate 	err = configure_vertex(v, inst);
40790Sstevel@tonic-gate 
40800Sstevel@tonic-gate 	if (lock_graph)
40810Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
40820Sstevel@tonic-gate 
40830Sstevel@tonic-gate 	return (err);
40840Sstevel@tonic-gate }
40850Sstevel@tonic-gate 
40860Sstevel@tonic-gate /*
40870Sstevel@tonic-gate  * Locate the vertex for this property group's instance.  If it doesn't exist
40880Sstevel@tonic-gate  * or is unconfigured, call dgraph_add_instance() & return.  Otherwise fetch
40890Sstevel@tonic-gate  * the restarter for the instance, and if it has changed, send
40900Sstevel@tonic-gate  * _REMOVE_INSTANCE to the old restarter, remove the dependency, make sure the
40910Sstevel@tonic-gate  * new restarter has a vertex, add a new dependency, and send _ADD_INSTANCE to
40920Sstevel@tonic-gate  * the new restarter.  Then fetch whether the instance should be enabled, and
40930Sstevel@tonic-gate  * if it is different from what we had, or if we changed the restarter, send
40940Sstevel@tonic-gate  * the appropriate _ENABLE or _DISABLE command.
40950Sstevel@tonic-gate  *
40960Sstevel@tonic-gate  * Returns 0 on success, ENOTSUP if the pg's parent is not an instance,
40970Sstevel@tonic-gate  * ECONNABORTED on repository disconnection, ECANCELED if the instance is
40980Sstevel@tonic-gate  * deleted, or -1 if the instance's general property group is deleted or if
40990Sstevel@tonic-gate  * its enabled property is misconfigured.
41000Sstevel@tonic-gate  */
41010Sstevel@tonic-gate static int
41020Sstevel@tonic-gate dgraph_update_general(scf_propertygroup_t *pg)
41030Sstevel@tonic-gate {
41040Sstevel@tonic-gate 	scf_handle_t *h;
41050Sstevel@tonic-gate 	scf_instance_t *inst;
41060Sstevel@tonic-gate 	char *fmri;
41070Sstevel@tonic-gate 	char *restarter_fmri;
41080Sstevel@tonic-gate 	graph_vertex_t *v;
41090Sstevel@tonic-gate 	int err;
41100Sstevel@tonic-gate 	int enabled, enabled_ovr;
41110Sstevel@tonic-gate 	int oldflags;
41120Sstevel@tonic-gate 
41130Sstevel@tonic-gate 	/* Find the vertex for this service */
41140Sstevel@tonic-gate 	h = scf_pg_handle(pg);
41150Sstevel@tonic-gate 
41160Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
41170Sstevel@tonic-gate 
41180Sstevel@tonic-gate 	if (scf_pg_get_parent_instance(pg, inst) != 0) {
41190Sstevel@tonic-gate 		switch (scf_error()) {
41200Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
41210Sstevel@tonic-gate 			return (ENOTSUP);
41220Sstevel@tonic-gate 
41230Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
41240Sstevel@tonic-gate 		default:
41250Sstevel@tonic-gate 			return (ECONNABORTED);
41260Sstevel@tonic-gate 
41270Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
41280Sstevel@tonic-gate 			return (0);
41290Sstevel@tonic-gate 
41300Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
41310Sstevel@tonic-gate 			bad_error("scf_pg_get_parent_instance", scf_error());
41320Sstevel@tonic-gate 		}
41330Sstevel@tonic-gate 	}
41340Sstevel@tonic-gate 
41350Sstevel@tonic-gate 	err = libscf_instance_get_fmri(inst, &fmri);
41360Sstevel@tonic-gate 	switch (err) {
41370Sstevel@tonic-gate 	case 0:
41380Sstevel@tonic-gate 		break;
41390Sstevel@tonic-gate 
41400Sstevel@tonic-gate 	case ECONNABORTED:
41410Sstevel@tonic-gate 		scf_instance_destroy(inst);
41420Sstevel@tonic-gate 		return (ECONNABORTED);
41430Sstevel@tonic-gate 
41440Sstevel@tonic-gate 	case ECANCELED:
41450Sstevel@tonic-gate 		scf_instance_destroy(inst);
41460Sstevel@tonic-gate 		return (0);
41470Sstevel@tonic-gate 
41480Sstevel@tonic-gate 	default:
41490Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", err);
41500Sstevel@tonic-gate 	}
41510Sstevel@tonic-gate 
41520Sstevel@tonic-gate 	log_framework(LOG_DEBUG,
41530Sstevel@tonic-gate 	    "Graph engine: Reloading general properties for %s.\n", fmri);
41540Sstevel@tonic-gate 
41550Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
41560Sstevel@tonic-gate 
41570Sstevel@tonic-gate 	v = vertex_get_by_name(fmri);
41580Sstevel@tonic-gate 	if (v == NULL || !(v->gv_flags & GV_CONFIGURED)) {
41590Sstevel@tonic-gate 		/* Will get the up-to-date properties. */
41600Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
41610Sstevel@tonic-gate 		err = dgraph_add_instance(fmri, inst, B_TRUE);
41620Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
41630Sstevel@tonic-gate 		scf_instance_destroy(inst);
41640Sstevel@tonic-gate 		return (err == ECANCELED ? 0 : err);
41650Sstevel@tonic-gate 	}
41660Sstevel@tonic-gate 
41670Sstevel@tonic-gate 	/* Read enabled & restarter from repository. */
41680Sstevel@tonic-gate 	restarter_fmri = startd_alloc(max_scf_value_size);
41690Sstevel@tonic-gate 	err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled,
41700Sstevel@tonic-gate 	    &enabled_ovr, &restarter_fmri);
41710Sstevel@tonic-gate 	if (err != 0 || enabled == -1) {
41720Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
41730Sstevel@tonic-gate 		scf_instance_destroy(inst);
41740Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
41750Sstevel@tonic-gate 
41760Sstevel@tonic-gate 		switch (err) {
41770Sstevel@tonic-gate 		case ENOENT:
41780Sstevel@tonic-gate 		case 0:
41790Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
41800Sstevel@tonic-gate 			return (-1);
41810Sstevel@tonic-gate 
41820Sstevel@tonic-gate 		case ECONNABORTED:
41830Sstevel@tonic-gate 		case ECANCELED:
41840Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
41850Sstevel@tonic-gate 			return (err);
41860Sstevel@tonic-gate 
41870Sstevel@tonic-gate 		default:
41880Sstevel@tonic-gate 			bad_error("libscf_get_basic_instance_data", err);
41890Sstevel@tonic-gate 		}
41900Sstevel@tonic-gate 	}
41910Sstevel@tonic-gate 
41920Sstevel@tonic-gate 	oldflags = v->gv_flags;
41930Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) |
41940Sstevel@tonic-gate 	    (enabled ? GV_ENBLD_NOOVR : 0);
41950Sstevel@tonic-gate 
41960Sstevel@tonic-gate 	if (enabled_ovr != -1)
41970Sstevel@tonic-gate 		enabled = enabled_ovr;
41980Sstevel@tonic-gate 
41990Sstevel@tonic-gate 	/*
42000Sstevel@tonic-gate 	 * If GV_ENBLD_NOOVR has changed, then we need to re-evaluate the
42010Sstevel@tonic-gate 	 * subgraph.
42020Sstevel@tonic-gate 	 */
42030Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE && v->gv_flags != oldflags)
42040Sstevel@tonic-gate 		(void) eval_subgraph(v, h);
42050Sstevel@tonic-gate 
42060Sstevel@tonic-gate 	scf_instance_destroy(inst);
42070Sstevel@tonic-gate 
42080Sstevel@tonic-gate 	/* Ignore restarter change for now. */
42090Sstevel@tonic-gate 
42100Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_value_size);
42110Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
42120Sstevel@tonic-gate 
42130Sstevel@tonic-gate 	/*
42140Sstevel@tonic-gate 	 * Always send _ENABLE or _DISABLE.  We could avoid this if the
42150Sstevel@tonic-gate 	 * restarter didn't change and the enabled value didn't change, but
42160Sstevel@tonic-gate 	 * that's not easy to check and improbable anyway, so we'll just do
42170Sstevel@tonic-gate 	 * this.
42180Sstevel@tonic-gate 	 */
42190Sstevel@tonic-gate 	graph_enable_by_vertex(v, enabled, 1);
42200Sstevel@tonic-gate 
42210Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
42220Sstevel@tonic-gate 
42230Sstevel@tonic-gate 	return (0);
42240Sstevel@tonic-gate }
42250Sstevel@tonic-gate 
42260Sstevel@tonic-gate /*
42270Sstevel@tonic-gate  * Delete all of the property group dependencies of v, update inst's running
42280Sstevel@tonic-gate  * snapshot, and add the dependencies in the new snapshot.  If any of the new
42290Sstevel@tonic-gate  * dependencies would create a cycle, send _ADMIN_MAINT_ON.  Otherwise
42300Sstevel@tonic-gate  * reevaluate v's dependencies, send _START or _STOP as appropriate, and do
42310Sstevel@tonic-gate  * the same for v's dependents.
42320Sstevel@tonic-gate  *
42330Sstevel@tonic-gate  * Returns
42340Sstevel@tonic-gate  *   0 - success
42350Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
42360Sstevel@tonic-gate  *   ECANCELED - inst was deleted
42370Sstevel@tonic-gate  *   EINVAL - inst is invalid (e.g., missing general/enabled)
42380Sstevel@tonic-gate  *   -1 - libscf_snapshots_refresh() failed
42390Sstevel@tonic-gate  */
42400Sstevel@tonic-gate static int
42410Sstevel@tonic-gate dgraph_refresh_instance(graph_vertex_t *v, scf_instance_t *inst)
42420Sstevel@tonic-gate {
42430Sstevel@tonic-gate 	int r;
42440Sstevel@tonic-gate 	int enabled;
42450Sstevel@tonic-gate 
424611466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
42470Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
42480Sstevel@tonic-gate 
42490Sstevel@tonic-gate 	/* Only refresh services with valid general/enabled properties. */
42500Sstevel@tonic-gate 	r = libscf_get_basic_instance_data(scf_instance_handle(inst), inst,
42510Sstevel@tonic-gate 	    v->gv_name, &enabled, NULL, NULL);
42520Sstevel@tonic-gate 	switch (r) {
42530Sstevel@tonic-gate 	case 0:
42540Sstevel@tonic-gate 		break;
42550Sstevel@tonic-gate 
42560Sstevel@tonic-gate 	case ECONNABORTED:
42570Sstevel@tonic-gate 	case ECANCELED:
42580Sstevel@tonic-gate 		return (r);
42590Sstevel@tonic-gate 
42600Sstevel@tonic-gate 	case ENOENT:
42610Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
42620Sstevel@tonic-gate 		    "Ignoring %s because it has no general property group.\n",
42630Sstevel@tonic-gate 		    v->gv_name);
42640Sstevel@tonic-gate 		return (EINVAL);
42650Sstevel@tonic-gate 
42660Sstevel@tonic-gate 	default:
42670Sstevel@tonic-gate 		bad_error("libscf_get_basic_instance_data", r);
42680Sstevel@tonic-gate 	}
42690Sstevel@tonic-gate 
42700Sstevel@tonic-gate 	if (enabled == -1)
42710Sstevel@tonic-gate 		return (EINVAL);
42720Sstevel@tonic-gate 
42730Sstevel@tonic-gate 	r = libscf_snapshots_refresh(inst, v->gv_name);
42740Sstevel@tonic-gate 	if (r != 0) {
42750Sstevel@tonic-gate 		if (r != -1)
42760Sstevel@tonic-gate 			bad_error("libscf_snapshots_refresh", r);
42770Sstevel@tonic-gate 
42780Sstevel@tonic-gate 		/* error logged */
42790Sstevel@tonic-gate 		return (r);
42800Sstevel@tonic-gate 	}
42810Sstevel@tonic-gate 
42820Sstevel@tonic-gate 	r = refresh_vertex(v, inst);
42830Sstevel@tonic-gate 	if (r != 0 && r != ECONNABORTED)
42840Sstevel@tonic-gate 		bad_error("refresh_vertex", r);
42850Sstevel@tonic-gate 	return (r);
42860Sstevel@tonic-gate }
42870Sstevel@tonic-gate 
42880Sstevel@tonic-gate /*
42897630SRenaud.Manus@Sun.COM  * Returns true only if none of this service's dependents are 'up' -- online
42907630SRenaud.Manus@Sun.COM  * or degraded (offline is considered down in this situation). This function
42917630SRenaud.Manus@Sun.COM  * is somehow similar to is_nonsubgraph_leaf() but works on subtrees.
42927630SRenaud.Manus@Sun.COM  */
42937630SRenaud.Manus@Sun.COM static boolean_t
42947630SRenaud.Manus@Sun.COM insubtree_dependents_down(graph_vertex_t *v)
42957630SRenaud.Manus@Sun.COM {
42967630SRenaud.Manus@Sun.COM 	graph_vertex_t *vv;
42977630SRenaud.Manus@Sun.COM 	graph_edge_t *e;
42987630SRenaud.Manus@Sun.COM 
429911466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
43007630SRenaud.Manus@Sun.COM 
43017630SRenaud.Manus@Sun.COM 	for (e = uu_list_first(v->gv_dependents); e != NULL;
43027630SRenaud.Manus@Sun.COM 	    e = uu_list_next(v->gv_dependents, e)) {
43037630SRenaud.Manus@Sun.COM 		vv = e->ge_vertex;
43047630SRenaud.Manus@Sun.COM 		if (vv->gv_type == GVT_INST) {
43057630SRenaud.Manus@Sun.COM 			if ((vv->gv_flags & GV_CONFIGURED) == 0)
43067630SRenaud.Manus@Sun.COM 				continue;
43077630SRenaud.Manus@Sun.COM 
43087630SRenaud.Manus@Sun.COM 			if ((vv->gv_flags & GV_TOOFFLINE) == 0)
43097630SRenaud.Manus@Sun.COM 				continue;
43107630SRenaud.Manus@Sun.COM 
43117630SRenaud.Manus@Sun.COM 			if ((vv->gv_state == RESTARTER_STATE_ONLINE) ||
43127630SRenaud.Manus@Sun.COM 			    (vv->gv_state == RESTARTER_STATE_DEGRADED))
43137630SRenaud.Manus@Sun.COM 				return (B_FALSE);
43147630SRenaud.Manus@Sun.COM 		} else {
43157630SRenaud.Manus@Sun.COM 			/*
43167630SRenaud.Manus@Sun.COM 			 * For dependency groups or service vertices, keep
43177630SRenaud.Manus@Sun.COM 			 * traversing to see if instances are running.
43187630SRenaud.Manus@Sun.COM 			 */
43197630SRenaud.Manus@Sun.COM 			if (insubtree_dependents_down(vv) == B_FALSE)
43207630SRenaud.Manus@Sun.COM 				return (B_FALSE);
43217630SRenaud.Manus@Sun.COM 		}
43227630SRenaud.Manus@Sun.COM 	}
43237630SRenaud.Manus@Sun.COM 
43247630SRenaud.Manus@Sun.COM 	return (B_TRUE);
43257630SRenaud.Manus@Sun.COM }
43267630SRenaud.Manus@Sun.COM 
43277630SRenaud.Manus@Sun.COM /*
43282747Sbustos  * Returns true only if none of this service's dependents are 'up' -- online,
43292747Sbustos  * degraded, or offline.
43300Sstevel@tonic-gate  */
43310Sstevel@tonic-gate static int
43322747Sbustos is_nonsubgraph_leaf(graph_vertex_t *v)
43330Sstevel@tonic-gate {
43340Sstevel@tonic-gate 	graph_vertex_t *vv;
43350Sstevel@tonic-gate 	graph_edge_t *e;
43360Sstevel@tonic-gate 
433711466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
43380Sstevel@tonic-gate 
43390Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
43400Sstevel@tonic-gate 	    e != NULL;
43410Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e)) {
43420Sstevel@tonic-gate 
43430Sstevel@tonic-gate 		vv = e->ge_vertex;
43440Sstevel@tonic-gate 		if (vv->gv_type == GVT_INST) {
43452747Sbustos 			if ((vv->gv_flags & GV_CONFIGURED) == 0)
43462747Sbustos 				continue;
43472747Sbustos 
43482747Sbustos 			if (vv->gv_flags & GV_INSUBGRAPH)
43492747Sbustos 				continue;
43502747Sbustos 
43512747Sbustos 			if (up_state(vv->gv_state))
43522747Sbustos 				return (0);
43530Sstevel@tonic-gate 		} else {
43540Sstevel@tonic-gate 			/*
43550Sstevel@tonic-gate 			 * For dependency group or service vertices, keep
43560Sstevel@tonic-gate 			 * traversing to see if instances are running.
43577630SRenaud.Manus@Sun.COM 			 *
43587630SRenaud.Manus@Sun.COM 			 * We should skip exclude_all dependencies otherwise
43597630SRenaud.Manus@Sun.COM 			 * the vertex will never be considered as a leaf
43607630SRenaud.Manus@Sun.COM 			 * if the dependent is offline. The main reason for
43617630SRenaud.Manus@Sun.COM 			 * this is that disable_nonsubgraph_leaves() skips
43627630SRenaud.Manus@Sun.COM 			 * exclusion dependencies.
43630Sstevel@tonic-gate 			 */
43647630SRenaud.Manus@Sun.COM 			if (vv->gv_type == GVT_GROUP &&
43657630SRenaud.Manus@Sun.COM 			    vv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
43667630SRenaud.Manus@Sun.COM 				continue;
43677630SRenaud.Manus@Sun.COM 
43682747Sbustos 			if (!is_nonsubgraph_leaf(vv))
43692747Sbustos 				return (0);
43700Sstevel@tonic-gate 		}
43710Sstevel@tonic-gate 	}
43722747Sbustos 
43732747Sbustos 	return (1);
43740Sstevel@tonic-gate }
43750Sstevel@tonic-gate 
43760Sstevel@tonic-gate /*
43772747Sbustos  * Disable v temporarily.  Attempt to do this by setting its enabled override
43782747Sbustos  * property in the repository.  If that fails, send a _DISABLE command.
43792747Sbustos  * Returns 0 on success and ECONNABORTED if the repository connection is
43802747Sbustos  * broken.
43810Sstevel@tonic-gate  */
43822747Sbustos static int
43832747Sbustos disable_service_temporarily(graph_vertex_t *v, scf_handle_t *h)
43840Sstevel@tonic-gate {
43852747Sbustos 	const char * const emsg = "Could not temporarily disable %s because "
43862747Sbustos 	    "%s.  Will stop service anyways.  Repository status for the "
43872747Sbustos 	    "service may be inaccurate.\n";
43882747Sbustos 	const char * const emsg_cbroken =
43892747Sbustos 	    "the repository connection was broken";
43902747Sbustos 
43912747Sbustos 	scf_instance_t *inst;
43920Sstevel@tonic-gate 	int r;
43930Sstevel@tonic-gate 
43940Sstevel@tonic-gate 	inst = scf_instance_create(h);
43950Sstevel@tonic-gate 	if (inst == NULL) {
43962747Sbustos 		char buf[100];
43972747Sbustos 
43982747Sbustos 		(void) snprintf(buf, sizeof (buf),
43992747Sbustos 		    "scf_instance_create() failed (%s)",
44002747Sbustos 		    scf_strerror(scf_error()));
44012747Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name, buf);
44022747Sbustos 
44032747Sbustos 		graph_enable_by_vertex(v, 0, 0);
44042747Sbustos 		return (0);
44050Sstevel@tonic-gate 	}
44062747Sbustos 
44070Sstevel@tonic-gate 	r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst,
44080Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT);
44090Sstevel@tonic-gate 	if (r != 0) {
44100Sstevel@tonic-gate 		switch (scf_error()) {
44110Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
44122747Sbustos 			log_error(LOG_WARNING, emsg, v->gv_name, emsg_cbroken);
44132747Sbustos 			graph_enable_by_vertex(v, 0, 0);
44142747Sbustos 			return (ECONNABORTED);
44150Sstevel@tonic-gate 
44160Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
44172747Sbustos 			return (0);
44180Sstevel@tonic-gate 
44190Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
44200Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
44210Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
44220Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
44230Sstevel@tonic-gate 		default:
44240Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri",
44250Sstevel@tonic-gate 			    scf_error());
44260Sstevel@tonic-gate 		}
44270Sstevel@tonic-gate 	}
44282747Sbustos 
44290Sstevel@tonic-gate 	r = libscf_set_enable_ovr(inst, 0);
44300Sstevel@tonic-gate 	switch (r) {
44310Sstevel@tonic-gate 	case 0:
44320Sstevel@tonic-gate 		scf_instance_destroy(inst);
44332747Sbustos 		return (0);
44342747Sbustos 
44350Sstevel@tonic-gate 	case ECANCELED:
44360Sstevel@tonic-gate 		scf_instance_destroy(inst);
44372747Sbustos 		return (0);
44382747Sbustos 
44390Sstevel@tonic-gate 	case ECONNABORTED:
44402747Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name, emsg_cbroken);
44412747Sbustos 		graph_enable_by_vertex(v, 0, 0);
44422747Sbustos 		return (ECONNABORTED);
44432747Sbustos 
44440Sstevel@tonic-gate 	case EPERM:
44452747Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name,
44462747Sbustos 		    "the repository denied permission");
44472747Sbustos 		graph_enable_by_vertex(v, 0, 0);
44482747Sbustos 		return (0);
44492747Sbustos 
44500Sstevel@tonic-gate 	case EROFS:
44512747Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name,
44522747Sbustos 		    "the repository is read-only");
44532747Sbustos 		graph_enable_by_vertex(v, 0, 0);
44542747Sbustos 		return (0);
44552747Sbustos 
44560Sstevel@tonic-gate 	default:
44570Sstevel@tonic-gate 		bad_error("libscf_set_enable_ovr", r);
44582747Sbustos 		/* NOTREACHED */
44590Sstevel@tonic-gate 	}
44602747Sbustos }
44612747Sbustos 
44622747Sbustos /*
44637630SRenaud.Manus@Sun.COM  * Of the transitive instance dependencies of v, offline those which are
44647630SRenaud.Manus@Sun.COM  * in the subtree and which are leaves (i.e., have no dependents which are
44657630SRenaud.Manus@Sun.COM  * "up").
44667630SRenaud.Manus@Sun.COM  */
44677630SRenaud.Manus@Sun.COM void
44687630SRenaud.Manus@Sun.COM offline_subtree_leaves(graph_vertex_t *v, void *arg)
44697630SRenaud.Manus@Sun.COM {
447011466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
44717630SRenaud.Manus@Sun.COM 
44727630SRenaud.Manus@Sun.COM 	/* If v isn't an instance, recurse on its dependencies. */
44737630SRenaud.Manus@Sun.COM 	if (v->gv_type != GVT_INST) {
44747630SRenaud.Manus@Sun.COM 		graph_walk_dependencies(v, offline_subtree_leaves, arg);
44757630SRenaud.Manus@Sun.COM 		return;
44767630SRenaud.Manus@Sun.COM 	}
44777630SRenaud.Manus@Sun.COM 
44787630SRenaud.Manus@Sun.COM 	/*
44797630SRenaud.Manus@Sun.COM 	 * If v is not in the subtree, so should all of its dependencies,
44807630SRenaud.Manus@Sun.COM 	 * so do nothing.
44817630SRenaud.Manus@Sun.COM 	 */
44827630SRenaud.Manus@Sun.COM 	if ((v->gv_flags & GV_TOOFFLINE) == 0)
44837630SRenaud.Manus@Sun.COM 		return;
44847630SRenaud.Manus@Sun.COM 
44857630SRenaud.Manus@Sun.COM 	/* If v isn't a leaf because it's already down, recurse. */
44867630SRenaud.Manus@Sun.COM 	if (!up_state(v->gv_state)) {
44877630SRenaud.Manus@Sun.COM 		graph_walk_dependencies(v, offline_subtree_leaves, arg);
44887630SRenaud.Manus@Sun.COM 		return;
44897630SRenaud.Manus@Sun.COM 	}
44907630SRenaud.Manus@Sun.COM 
44917630SRenaud.Manus@Sun.COM 	/* if v is a leaf, offline it or disable it if it's the last one */
44927630SRenaud.Manus@Sun.COM 	if (insubtree_dependents_down(v) == B_TRUE) {
44937630SRenaud.Manus@Sun.COM 		if (v->gv_flags & GV_TODISABLE)
44947630SRenaud.Manus@Sun.COM 			vertex_send_event(v,
44957630SRenaud.Manus@Sun.COM 			    RESTARTER_EVENT_TYPE_ADMIN_DISABLE);
44967630SRenaud.Manus@Sun.COM 		else
44977630SRenaud.Manus@Sun.COM 			offline_vertex(v);
44987630SRenaud.Manus@Sun.COM 	}
44997630SRenaud.Manus@Sun.COM }
45007630SRenaud.Manus@Sun.COM 
45017630SRenaud.Manus@Sun.COM void
45027630SRenaud.Manus@Sun.COM graph_offline_subtree_leaves(graph_vertex_t *v, void *h)
45037630SRenaud.Manus@Sun.COM {
45047630SRenaud.Manus@Sun.COM 	graph_walk_dependencies(v, offline_subtree_leaves, (void *)h);
45057630SRenaud.Manus@Sun.COM }
45067630SRenaud.Manus@Sun.COM 
45077630SRenaud.Manus@Sun.COM 
45087630SRenaud.Manus@Sun.COM /*
45092747Sbustos  * Of the transitive instance dependencies of v, disable those which are not
45102747Sbustos  * in the subgraph and which are leaves (i.e., have no dependents which are
45112747Sbustos  * "up").
45122747Sbustos  */
45132747Sbustos static void
45142747Sbustos disable_nonsubgraph_leaves(graph_vertex_t *v, void *arg)
45152747Sbustos {
451611466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
45172747Sbustos 
45184762Snakanon 	/*
45194762Snakanon 	 * We must skip exclusion dependencies because they are allowed to
45204762Snakanon 	 * complete dependency cycles.  This is correct because A's exclusion
45214762Snakanon 	 * dependency on B doesn't bear on the order in which they should be
45224762Snakanon 	 * stopped.  Indeed, the exclusion dependency should guarantee that
45234762Snakanon 	 * they are never online at the same time.
45244762Snakanon 	 */
45254762Snakanon 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
45264762Snakanon 		return;
45274762Snakanon 
45282747Sbustos 	/* If v isn't an instance, recurse on its dependencies. */
45292747Sbustos 	if (v->gv_type != GVT_INST)
45302747Sbustos 		goto recurse;
45312747Sbustos 
45322747Sbustos 	if ((v->gv_flags & GV_CONFIGURED) == 0)
45332747Sbustos 		/*
45342747Sbustos 		 * Unconfigured instances should have no dependencies, but in
45352747Sbustos 		 * case they ever get them,
45362747Sbustos 		 */
45372747Sbustos 		goto recurse;
45382747Sbustos 
45392747Sbustos 	/*
45402747Sbustos 	 * If v is in the subgraph, so should all of its dependencies, so do
45412747Sbustos 	 * nothing.
45422747Sbustos 	 */
45432747Sbustos 	if (v->gv_flags & GV_INSUBGRAPH)
45442747Sbustos 		return;
45452747Sbustos 
45462747Sbustos 	/* If v isn't a leaf because it's already down, recurse. */
45472747Sbustos 	if (!up_state(v->gv_state))
45482747Sbustos 		goto recurse;
45492747Sbustos 
45502747Sbustos 	/* If v is disabled but not down yet, be patient. */
45512747Sbustos 	if ((v->gv_flags & GV_ENABLED) == 0)
45522747Sbustos 		return;
45532747Sbustos 
45542747Sbustos 	/* If v is a leaf, disable it. */
45552747Sbustos 	if (is_nonsubgraph_leaf(v))
45562747Sbustos 		(void) disable_service_temporarily(v, (scf_handle_t *)arg);
45572747Sbustos 
45580Sstevel@tonic-gate 	return;
45592747Sbustos 
45600Sstevel@tonic-gate recurse:
45612747Sbustos 	graph_walk_dependencies(v, disable_nonsubgraph_leaves, arg);
45620Sstevel@tonic-gate }
45630Sstevel@tonic-gate 
45640Sstevel@tonic-gate /*
45650Sstevel@tonic-gate  * Find the vertex for inst_name.  If it doesn't exist, return ENOENT.
45660Sstevel@tonic-gate  * Otherwise set its state to state.  If the instance has entered a state
45670Sstevel@tonic-gate  * which requires automatic action, take it (Uninitialized: do
45680Sstevel@tonic-gate  * dgraph_refresh_instance() without the snapshot update.  Disabled: if the
45690Sstevel@tonic-gate  * instance should be enabled, send _ENABLE.  Offline: if the instance should
45700Sstevel@tonic-gate  * be disabled, send _DISABLE, and if its dependencies are satisfied, send
45710Sstevel@tonic-gate  * _START.  Online, Degraded: if the instance wasn't running, update its start
45720Sstevel@tonic-gate  * snapshot.  Maintenance: no action.)
45730Sstevel@tonic-gate  *
45740Sstevel@tonic-gate  * Also fails with ECONNABORTED, or EINVAL if state is invalid.
45750Sstevel@tonic-gate  */
45760Sstevel@tonic-gate static int
45770Sstevel@tonic-gate dgraph_set_instance_state(scf_handle_t *h, const char *inst_name,
45780Sstevel@tonic-gate     restarter_instance_state_t state, restarter_error_t serr)
45790Sstevel@tonic-gate {
45800Sstevel@tonic-gate 	graph_vertex_t *v;
45811958Slianep 	int err = 0;
45820Sstevel@tonic-gate 	restarter_instance_state_t old_state;
45830Sstevel@tonic-gate 
45840Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
45850Sstevel@tonic-gate 
45860Sstevel@tonic-gate 	v = vertex_get_by_name(inst_name);
45870Sstevel@tonic-gate 	if (v == NULL) {
45880Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
45890Sstevel@tonic-gate 		return (ENOENT);
45900Sstevel@tonic-gate 	}
45910Sstevel@tonic-gate 
45922747Sbustos 	assert(v->gv_type == GVT_INST);
45932747Sbustos 
45940Sstevel@tonic-gate 	switch (state) {
45950Sstevel@tonic-gate 	case RESTARTER_STATE_UNINIT:
45960Sstevel@tonic-gate 	case RESTARTER_STATE_DISABLED:
45970Sstevel@tonic-gate 	case RESTARTER_STATE_OFFLINE:
45980Sstevel@tonic-gate 	case RESTARTER_STATE_ONLINE:
45990Sstevel@tonic-gate 	case RESTARTER_STATE_DEGRADED:
46000Sstevel@tonic-gate 	case RESTARTER_STATE_MAINT:
46010Sstevel@tonic-gate 		break;
46020Sstevel@tonic-gate 
46030Sstevel@tonic-gate 	default:
46040Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
46050Sstevel@tonic-gate 		return (EINVAL);
46060Sstevel@tonic-gate 	}
46070Sstevel@tonic-gate 
46080Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph noting %s %s -> %s.\n", v->gv_name,
46090Sstevel@tonic-gate 	    instance_state_str[v->gv_state], instance_state_str[state]);
46100Sstevel@tonic-gate 
46110Sstevel@tonic-gate 	old_state = v->gv_state;
46120Sstevel@tonic-gate 	v->gv_state = state;
46130Sstevel@tonic-gate 
46142339Slianep 	err = gt_transition(h, v, serr, old_state);
46151958Slianep 
46161958Slianep 	MUTEX_UNLOCK(&dgraph_lock);
46171958Slianep 	return (err);
46181958Slianep }
46191958Slianep 
46201958Slianep /*
46212747Sbustos  * Handle state changes during milestone shutdown.  See
46222747Sbustos  * dgraph_set_milestone().  If the repository connection is broken,
46232747Sbustos  * ECONNABORTED will be returned, though a _DISABLE command will be sent for
46242747Sbustos  * the vertex anyway.
46251958Slianep  */
46262747Sbustos int
46271958Slianep vertex_subgraph_dependencies_shutdown(scf_handle_t *h, graph_vertex_t *v,
46282747Sbustos     restarter_instance_state_t old_state)
46291958Slianep {
46302747Sbustos 	int was_up, now_up;
46312747Sbustos 	int ret = 0;
46322747Sbustos 
46332747Sbustos 	assert(v->gv_type == GVT_INST);
46342747Sbustos 
46352747Sbustos 	/* Don't care if we're not going to a milestone. */
46362747Sbustos 	if (milestone == NULL)
46372747Sbustos 		return (0);
46382747Sbustos 
46392747Sbustos 	/* Don't care if we already finished coming down. */
46402747Sbustos 	if (non_subgraph_svcs == 0)
46412747Sbustos 		return (0);
46422747Sbustos 
46432747Sbustos 	/* Don't care if the service is in the subgraph. */
46442747Sbustos 	if (v->gv_flags & GV_INSUBGRAPH)
46452747Sbustos 		return (0);
46462747Sbustos 
46472747Sbustos 	/*
46482747Sbustos 	 * Update non_subgraph_svcs.  It is the number of non-subgraph
46492747Sbustos 	 * services which are in online, degraded, or offline.
46502747Sbustos 	 */
46512747Sbustos 
46522747Sbustos 	was_up = up_state(old_state);
46532747Sbustos 	now_up = up_state(v->gv_state);
46542747Sbustos 
46552747Sbustos 	if (!was_up && now_up) {
46562747Sbustos 		++non_subgraph_svcs;
46572747Sbustos 	} else if (was_up && !now_up) {
46580Sstevel@tonic-gate 		--non_subgraph_svcs;
46592747Sbustos 
46600Sstevel@tonic-gate 		if (non_subgraph_svcs == 0) {
46610Sstevel@tonic-gate 			if (halting != -1) {
46620Sstevel@tonic-gate 				do_uadmin();
46630Sstevel@tonic-gate 			} else if (go_single_user_mode || go_to_level1) {
46640Sstevel@tonic-gate 				(void) startd_thread_create(single_user_thread,
46650Sstevel@tonic-gate 				    NULL);
46660Sstevel@tonic-gate 			}
46672747Sbustos 			return (0);
46680Sstevel@tonic-gate 		}
46690Sstevel@tonic-gate 	}
46702747Sbustos 
46712747Sbustos 	/* If this service is a leaf, it should be disabled. */
46722747Sbustos 	if ((v->gv_flags & GV_ENABLED) && is_nonsubgraph_leaf(v)) {
46732747Sbustos 		int r;
46742747Sbustos 
46752747Sbustos 		r = disable_service_temporarily(v, h);
46762747Sbustos 		switch (r) {
46772747Sbustos 		case 0:
46782747Sbustos 			break;
46792747Sbustos 
46802747Sbustos 		case ECONNABORTED:
46812747Sbustos 			ret = ECONNABORTED;
46822747Sbustos 			break;
46832747Sbustos 
46842747Sbustos 		default:
46852747Sbustos 			bad_error("disable_service_temporarily", r);
46862747Sbustos 		}
46872747Sbustos 	}
46882747Sbustos 
46892747Sbustos 	/*
46902747Sbustos 	 * If the service just came down, propagate the disable to the newly
46912747Sbustos 	 * exposed leaves.
46922747Sbustos 	 */
46932747Sbustos 	if (was_up && !now_up)
46942747Sbustos 		graph_walk_dependencies(v, disable_nonsubgraph_leaves,
46952747Sbustos 		    (void *)h);
46962747Sbustos 
46972747Sbustos 	return (ret);
46981958Slianep }
46991958Slianep 
47001958Slianep /*
47011958Slianep  * Decide whether to start up an sulogin thread after a service is
47021958Slianep  * finished changing state.  Only need to do the full can_come_up()
47031958Slianep  * evaluation if an instance is changing state, we're not halfway through
47041958Slianep  * loading the thread, and we aren't shutting down or going to the single
47051958Slianep  * user milestone.
47061958Slianep  */
47071958Slianep void
47081958Slianep graph_transition_sulogin(restarter_instance_state_t state,
47091958Slianep     restarter_instance_state_t old_state)
47101958Slianep {
471111466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
47120Sstevel@tonic-gate 
47130Sstevel@tonic-gate 	if (state != old_state && st->st_load_complete &&
47140Sstevel@tonic-gate 	    !go_single_user_mode && !go_to_level1 &&
47150Sstevel@tonic-gate 	    halting == -1) {
47163639Srm88369 		if (!sulogin_thread_running && !can_come_up()) {
47170Sstevel@tonic-gate 			(void) startd_thread_create(sulogin_thread, NULL);
47180Sstevel@tonic-gate 			sulogin_thread_running = B_TRUE;
47190Sstevel@tonic-gate 		}
47200Sstevel@tonic-gate 	}
47211958Slianep }
47221958Slianep 
47231958Slianep /*
47242339Slianep  * Propagate a start, stop event, or a satisfiability event.
47251958Slianep  *
47262339Slianep  * PROPAGATE_START and PROPAGATE_STOP simply propagate the transition event
47272339Slianep  * to direct dependents.  PROPAGATE_SAT propagates a start then walks the
47282339Slianep  * full dependent graph to check for newly satisfied nodes.  This is
47292339Slianep  * necessary for cases when non-direct dependents may be effected but direct
47302339Slianep  * dependents may not (e.g. for optional_all evaluations, see the
47312339Slianep  * propagate_satbility() comments).
47322339Slianep  *
47332339Slianep  * PROPAGATE_SAT should be used whenever a non-running service moves into
47342339Slianep  * a state which can satisfy optional dependencies, like disabled or
47352339Slianep  * maintenance.
47361958Slianep  */
47371958Slianep void
47382339Slianep graph_transition_propagate(graph_vertex_t *v, propagate_event_t type,
47391958Slianep     restarter_error_t rerr)
47401958Slianep {
47412339Slianep 	if (type == PROPAGATE_STOP) {
47421958Slianep 		graph_walk_dependents(v, propagate_stop, (void *)rerr);
47432339Slianep 	} else if (type == PROPAGATE_START || type == PROPAGATE_SAT) {
47441958Slianep 		graph_walk_dependents(v, propagate_start, NULL);
47451958Slianep 
47462339Slianep 		if (type == PROPAGATE_SAT)
47471958Slianep 			propagate_satbility(v);
47481958Slianep 	} else {
47491958Slianep #ifndef NDEBUG
47502339Slianep 		uu_warn("%s:%d: Unexpected type value %d.\n",  __FILE__,
47512339Slianep 		    __LINE__, type);
47521958Slianep #endif
47531958Slianep 		abort();
47541958Slianep 	}
47550Sstevel@tonic-gate }
47560Sstevel@tonic-gate 
47570Sstevel@tonic-gate /*
47580Sstevel@tonic-gate  * If a vertex for fmri exists and it is enabled, send _DISABLE to the
47590Sstevel@tonic-gate  * restarter.  If it is running, send _STOP.  Send _REMOVE_INSTANCE.  Delete
47600Sstevel@tonic-gate  * all property group dependencies, and the dependency on the restarter,
47610Sstevel@tonic-gate  * disposing of vertices as appropriate.  If other vertices depend on this
47620Sstevel@tonic-gate  * one, mark it unconfigured and return.  Otherwise remove the vertex.  Always
47630Sstevel@tonic-gate  * returns 0.
47640Sstevel@tonic-gate  */
47650Sstevel@tonic-gate static int
47660Sstevel@tonic-gate dgraph_remove_instance(const char *fmri, scf_handle_t *h)
47670Sstevel@tonic-gate {
47680Sstevel@tonic-gate 	graph_vertex_t *v;
47690Sstevel@tonic-gate 	graph_edge_t *e;
47700Sstevel@tonic-gate 	uu_list_t *old_deps;
47710Sstevel@tonic-gate 	int err;
47720Sstevel@tonic-gate 
47730Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph engine: Removing %s.\n", fmri);
47740Sstevel@tonic-gate 
47750Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
47760Sstevel@tonic-gate 
47770Sstevel@tonic-gate 	v = vertex_get_by_name(fmri);
47780Sstevel@tonic-gate 	if (v == NULL) {
47790Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
47800Sstevel@tonic-gate 		return (0);
47810Sstevel@tonic-gate 	}
47820Sstevel@tonic-gate 
47830Sstevel@tonic-gate 	/* Send restarter delete event. */
47840Sstevel@tonic-gate 	if (v->gv_flags & GV_CONFIGURED)
47850Sstevel@tonic-gate 		graph_unset_restarter(v);
47860Sstevel@tonic-gate 
47870Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
47880Sstevel@tonic-gate 		/*
47890Sstevel@tonic-gate 		 * Make a list of v's current dependencies so we can
47900Sstevel@tonic-gate 		 * reevaluate their GV_INSUBGRAPH flags after the dependencies
47910Sstevel@tonic-gate 		 * are removed.
47920Sstevel@tonic-gate 		 */
47930Sstevel@tonic-gate 		old_deps = startd_list_create(graph_edge_pool, NULL, 0);
47940Sstevel@tonic-gate 
47950Sstevel@tonic-gate 		err = uu_list_walk(v->gv_dependencies,
47961712Srm88369 		    (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0);
47970Sstevel@tonic-gate 		assert(err == 0);
47980Sstevel@tonic-gate 	}
47990Sstevel@tonic-gate 
48000Sstevel@tonic-gate 	delete_instance_dependencies(v, B_TRUE);
48010Sstevel@tonic-gate 
48020Sstevel@tonic-gate 	/*
48030Sstevel@tonic-gate 	 * Deleting an instance can both satisfy and unsatisfy dependencies,
48040Sstevel@tonic-gate 	 * depending on their type.  First propagate the stop as a RERR_RESTART
48050Sstevel@tonic-gate 	 * event -- deletion isn't a fault, just a normal stop.  This gives
48060Sstevel@tonic-gate 	 * dependent services the chance to do a clean shutdown.  Then, mark
48070Sstevel@tonic-gate 	 * the service as unconfigured and propagate the start event for the
48080Sstevel@tonic-gate 	 * optional_all dependencies that might have become satisfied.
48090Sstevel@tonic-gate 	 */
48100Sstevel@tonic-gate 	graph_walk_dependents(v, propagate_stop, (void *)RERR_RESTART);
48110Sstevel@tonic-gate 
48120Sstevel@tonic-gate 	v->gv_flags &= ~GV_CONFIGURED;
48137475SPhilippe.Jung@Sun.COM 	v->gv_flags &= ~GV_DEATHROW;
48140Sstevel@tonic-gate 
48150Sstevel@tonic-gate 	graph_walk_dependents(v, propagate_start, NULL);
48160Sstevel@tonic-gate 	propagate_satbility(v);
48170Sstevel@tonic-gate 
48180Sstevel@tonic-gate 	/*
48190Sstevel@tonic-gate 	 * If there are no (non-service) dependents, the vertex can be
48200Sstevel@tonic-gate 	 * completely removed.
48210Sstevel@tonic-gate 	 */
48221712Srm88369 	if (v != milestone && v->gv_refs == 0 &&
48231712Srm88369 	    uu_list_numnodes(v->gv_dependents) == 1)
48240Sstevel@tonic-gate 		remove_inst_vertex(v);
48250Sstevel@tonic-gate 
48260Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
48270Sstevel@tonic-gate 		void *cookie = NULL;
48280Sstevel@tonic-gate 
48290Sstevel@tonic-gate 		while ((e = uu_list_teardown(old_deps, &cookie)) != NULL) {
48301712Srm88369 			v = e->ge_vertex;
48311712Srm88369 
48321712Srm88369 			if (vertex_unref(v) == VERTEX_INUSE)
48331712Srm88369 				while (eval_subgraph(v, h) == ECONNABORTED)
48341712Srm88369 					libscf_handle_rebind(h);
48350Sstevel@tonic-gate 
48360Sstevel@tonic-gate 			startd_free(e, sizeof (*e));
48370Sstevel@tonic-gate 		}
48380Sstevel@tonic-gate 
48390Sstevel@tonic-gate 		uu_list_destroy(old_deps);
48400Sstevel@tonic-gate 	}
48410Sstevel@tonic-gate 
48420Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
48430Sstevel@tonic-gate 
48440Sstevel@tonic-gate 	return (0);
48450Sstevel@tonic-gate }
48460Sstevel@tonic-gate 
48470Sstevel@tonic-gate /*
48480Sstevel@tonic-gate  * Return the eventual (maybe current) milestone in the form of a
48490Sstevel@tonic-gate  * legacy runlevel.
48500Sstevel@tonic-gate  */
48510Sstevel@tonic-gate static char
48520Sstevel@tonic-gate target_milestone_as_runlevel()
48530Sstevel@tonic-gate {
485411466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
48550Sstevel@tonic-gate 
48560Sstevel@tonic-gate 	if (milestone == NULL)
48570Sstevel@tonic-gate 		return ('3');
48580Sstevel@tonic-gate 	else if (milestone == MILESTONE_NONE)
48590Sstevel@tonic-gate 		return ('0');
48600Sstevel@tonic-gate 
48610Sstevel@tonic-gate 	if (strcmp(milestone->gv_name, multi_user_fmri) == 0)
48620Sstevel@tonic-gate 		return ('2');
48630Sstevel@tonic-gate 	else if (strcmp(milestone->gv_name, single_user_fmri) == 0)
48640Sstevel@tonic-gate 		return ('S');
48650Sstevel@tonic-gate 	else if (strcmp(milestone->gv_name, multi_user_svr_fmri) == 0)
48660Sstevel@tonic-gate 		return ('3');
48670Sstevel@tonic-gate 
48680Sstevel@tonic-gate #ifndef NDEBUG
48690Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:%d: Unknown milestone name \"%s\".\n",
48700Sstevel@tonic-gate 	    __FILE__, __LINE__, milestone->gv_name);
48710Sstevel@tonic-gate #endif
48720Sstevel@tonic-gate 	abort();
48730Sstevel@tonic-gate 	/* NOTREACHED */
48740Sstevel@tonic-gate }
48750Sstevel@tonic-gate 
48760Sstevel@tonic-gate static struct {
48770Sstevel@tonic-gate 	char	rl;
48780Sstevel@tonic-gate 	int	sig;
48790Sstevel@tonic-gate } init_sigs[] = {
48800Sstevel@tonic-gate 	{ 'S', SIGBUS },
48810Sstevel@tonic-gate 	{ '0', SIGINT },
48820Sstevel@tonic-gate 	{ '1', SIGQUIT },
48830Sstevel@tonic-gate 	{ '2', SIGILL },
48840Sstevel@tonic-gate 	{ '3', SIGTRAP },
48850Sstevel@tonic-gate 	{ '4', SIGIOT },
48860Sstevel@tonic-gate 	{ '5', SIGEMT },
48870Sstevel@tonic-gate 	{ '6', SIGFPE },
48880Sstevel@tonic-gate 	{ 0, 0 }
48890Sstevel@tonic-gate };
48900Sstevel@tonic-gate 
48910Sstevel@tonic-gate static void
48920Sstevel@tonic-gate signal_init(char rl)
48930Sstevel@tonic-gate {
48940Sstevel@tonic-gate 	pid_t init_pid;
48950Sstevel@tonic-gate 	int i;
48960Sstevel@tonic-gate 
489711466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
48980Sstevel@tonic-gate 
48990Sstevel@tonic-gate 	if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
49000Sstevel@tonic-gate 	    sizeof (init_pid)) != sizeof (init_pid)) {
49010Sstevel@tonic-gate 		log_error(LOG_NOTICE, "Could not get pid to signal init.\n");
49020Sstevel@tonic-gate 		return;
49030Sstevel@tonic-gate 	}
49040Sstevel@tonic-gate 
49050Sstevel@tonic-gate 	for (i = 0; init_sigs[i].rl != 0; ++i)
49060Sstevel@tonic-gate 		if (init_sigs[i].rl == rl)
49070Sstevel@tonic-gate 			break;
49080Sstevel@tonic-gate 
49090Sstevel@tonic-gate 	if (init_sigs[i].rl != 0) {
49100Sstevel@tonic-gate 		if (kill(init_pid, init_sigs[i].sig) != 0) {
49110Sstevel@tonic-gate 			switch (errno) {
49120Sstevel@tonic-gate 			case EPERM:
49130Sstevel@tonic-gate 			case ESRCH:
49140Sstevel@tonic-gate 				log_error(LOG_NOTICE, "Could not signal init: "
49150Sstevel@tonic-gate 				    "%s.\n", strerror(errno));
49160Sstevel@tonic-gate 				break;
49170Sstevel@tonic-gate 
49180Sstevel@tonic-gate 			case EINVAL:
49190Sstevel@tonic-gate 			default:
49200Sstevel@tonic-gate 				bad_error("kill", errno);
49210Sstevel@tonic-gate 			}
49220Sstevel@tonic-gate 		}
49230Sstevel@tonic-gate 	}
49240Sstevel@tonic-gate }
49250Sstevel@tonic-gate 
49260Sstevel@tonic-gate /*
49270Sstevel@tonic-gate  * This is called when one of the major milestones changes state, or when
49280Sstevel@tonic-gate  * init is signalled and tells us it was told to change runlevel.  We wait
49290Sstevel@tonic-gate  * to reach the milestone because this allows /etc/inittab entries to retain
49300Sstevel@tonic-gate  * some boot ordering: historically, entries could place themselves before/after
49310Sstevel@tonic-gate  * the running of /sbin/rcX scripts but we can no longer make the
49320Sstevel@tonic-gate  * distinction because the /sbin/rcX scripts no longer exist as punctuation
49330Sstevel@tonic-gate  * marks in /etc/inittab.
49340Sstevel@tonic-gate  *
49350Sstevel@tonic-gate  * Also, we only trigger an update when we reach the eventual target
49360Sstevel@tonic-gate  * milestone: without this, an /etc/inittab entry marked only for
49370Sstevel@tonic-gate  * runlevel 2 would be executed for runlevel 3, which is not how
49380Sstevel@tonic-gate  * /etc/inittab entries work.
49390Sstevel@tonic-gate  *
49400Sstevel@tonic-gate  * If we're single user coming online, then we set utmpx to the target
49410Sstevel@tonic-gate  * runlevel so that legacy scripts can work as expected.
49420Sstevel@tonic-gate  */
49430Sstevel@tonic-gate static void
49440Sstevel@tonic-gate graph_runlevel_changed(char rl, int online)
49450Sstevel@tonic-gate {
49460Sstevel@tonic-gate 	char trl;
49470Sstevel@tonic-gate 
494811466SRoger.Faulkner@Sun.COM 	assert(MUTEX_HELD(&dgraph_lock));
49490Sstevel@tonic-gate 
49500Sstevel@tonic-gate 	trl = target_milestone_as_runlevel();
49510Sstevel@tonic-gate 
49520Sstevel@tonic-gate 	if (online) {
49530Sstevel@tonic-gate 		if (rl == trl) {
49541514Srm88369 			current_runlevel = trl;
49550Sstevel@tonic-gate 			signal_init(trl);
49560Sstevel@tonic-gate 		} else if (rl == 'S') {
49570Sstevel@tonic-gate 			/*
49580Sstevel@tonic-gate 			 * At boot, set the entry early for the benefit of the
49590Sstevel@tonic-gate 			 * legacy init scripts.
49600Sstevel@tonic-gate 			 */
49610Sstevel@tonic-gate 			utmpx_set_runlevel(trl, 'S', B_FALSE);
49620Sstevel@tonic-gate 		}
49630Sstevel@tonic-gate 	} else {
49640Sstevel@tonic-gate 		if (rl == '3' && trl == '2') {
49651514Srm88369 			current_runlevel = trl;
49660Sstevel@tonic-gate 			signal_init(trl);
49670Sstevel@tonic-gate 		} else if (rl == '2' && trl == 'S') {
49681514Srm88369 			current_runlevel = trl;
49690Sstevel@tonic-gate 			signal_init(trl);
49700Sstevel@tonic-gate 		}
49710Sstevel@tonic-gate 	}
49720Sstevel@tonic-gate }
49730Sstevel@tonic-gate 
49740Sstevel@tonic-gate /*
49750Sstevel@tonic-gate  * Move to a backwards-compatible runlevel by executing the appropriate
49760Sstevel@tonic-gate  * /etc/rc?.d/K* scripts and/or setting the milestone.
49770Sstevel@tonic-gate  *
49780Sstevel@tonic-gate  * Returns
49790Sstevel@tonic-gate  *   0 - success
49800Sstevel@tonic-gate  *   ECONNRESET - success, but handle was reset
49810Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
49820Sstevel@tonic-gate  *   ECANCELED - pg was deleted
49830Sstevel@tonic-gate  */
49840Sstevel@tonic-gate static int
49850Sstevel@tonic-gate dgraph_set_runlevel(scf_propertygroup_t *pg, scf_property_t *prop)
49860Sstevel@tonic-gate {
49870Sstevel@tonic-gate 	char rl;
49880Sstevel@tonic-gate 	scf_handle_t *h;
49890Sstevel@tonic-gate 	int r;
49900Sstevel@tonic-gate 	const char *ms = NULL;	/* what to commit as options/milestone */
49910Sstevel@tonic-gate 	boolean_t rebound = B_FALSE;
49920Sstevel@tonic-gate 	int mark_rl = 0;
49930Sstevel@tonic-gate 
49940Sstevel@tonic-gate 	const char * const stop = "stop";
49950Sstevel@tonic-gate 
49960Sstevel@tonic-gate 	r = libscf_extract_runlevel(prop, &rl);
49970Sstevel@tonic-gate 	switch (r) {
49980Sstevel@tonic-gate 	case 0:
49990Sstevel@tonic-gate 		break;
50000Sstevel@tonic-gate 
50010Sstevel@tonic-gate 	case ECONNABORTED:
50020Sstevel@tonic-gate 	case ECANCELED:
50030Sstevel@tonic-gate 		return (r);
50040Sstevel@tonic-gate 
50050Sstevel@tonic-gate 	case EINVAL:
50060Sstevel@tonic-gate 	case ENOENT:
50070Sstevel@tonic-gate 		log_error(LOG_WARNING, "runlevel property is misconfigured; "
50080Sstevel@tonic-gate 		    "ignoring.\n");
50090Sstevel@tonic-gate 		/* delete the bad property */
50100Sstevel@tonic-gate 		goto nolock_out;
50110Sstevel@tonic-gate 
50120Sstevel@tonic-gate 	default:
50130Sstevel@tonic-gate 		bad_error("libscf_extract_runlevel", r);
50140Sstevel@tonic-gate 	}
50150Sstevel@tonic-gate 
50160Sstevel@tonic-gate 	switch (rl) {
50170Sstevel@tonic-gate 	case 's':
50180Sstevel@tonic-gate 		rl = 'S';
50190Sstevel@tonic-gate 		/* FALLTHROUGH */
50200Sstevel@tonic-gate 
50210Sstevel@tonic-gate 	case 'S':
50220Sstevel@tonic-gate 	case '2':
50230Sstevel@tonic-gate 	case '3':
50240Sstevel@tonic-gate 		/*
50250Sstevel@tonic-gate 		 * These cases cause a milestone change, so
50260Sstevel@tonic-gate 		 * graph_runlevel_changed() will eventually deal with
50270Sstevel@tonic-gate 		 * signalling init.
50280Sstevel@tonic-gate 		 */
50290Sstevel@tonic-gate 		break;
50300Sstevel@tonic-gate 
50310Sstevel@tonic-gate 	case '0':
50320Sstevel@tonic-gate 	case '1':
50330Sstevel@tonic-gate 	case '4':
50340Sstevel@tonic-gate 	case '5':
50350Sstevel@tonic-gate 	case '6':
50360Sstevel@tonic-gate 		mark_rl = 1;
50370Sstevel@tonic-gate 		break;
50380Sstevel@tonic-gate 
50390Sstevel@tonic-gate 	default:
50400Sstevel@tonic-gate 		log_framework(LOG_NOTICE, "Unknown runlevel '%c'.\n", rl);
50410Sstevel@tonic-gate 		ms = NULL;
50420Sstevel@tonic-gate 		goto nolock_out;
50430Sstevel@tonic-gate 	}
50440Sstevel@tonic-gate 
50450Sstevel@tonic-gate 	h = scf_pg_handle(pg);
50460Sstevel@tonic-gate 
50470Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
50480Sstevel@tonic-gate 
50490Sstevel@tonic-gate 	/*
50500Sstevel@tonic-gate 	 * Since this triggers no milestone changes, force it by hand.
50510Sstevel@tonic-gate 	 */
50520Sstevel@tonic-gate 	if (current_runlevel == '4' && rl == '3')
50530Sstevel@tonic-gate 		mark_rl = 1;
50540Sstevel@tonic-gate 
50551514Srm88369 	/*
50561514Srm88369 	 * 1. If we are here after an "init X":
50571514Srm88369 	 *
50581514Srm88369 	 * init X
50591514Srm88369 	 *	init/lscf_set_runlevel()
50601514Srm88369 	 *		process_pg_event()
50611514Srm88369 	 *		dgraph_set_runlevel()
50621514Srm88369 	 *
50631514Srm88369 	 * then we haven't passed through graph_runlevel_changed() yet,
50641514Srm88369 	 * therefore 'current_runlevel' has not changed for sure but 'rl' has.
50651514Srm88369 	 * In consequence, if 'rl' is lower than 'current_runlevel', we change
50661514Srm88369 	 * the system runlevel and execute the appropriate /etc/rc?.d/K* scripts
50671514Srm88369 	 * past this test.
50681514Srm88369 	 *
50691514Srm88369 	 * 2. On the other hand, if we are here after a "svcadm milestone":
50701514Srm88369 	 *
50711514Srm88369 	 * svcadm milestone X
50721514Srm88369 	 *	dgraph_set_milestone()
50731514Srm88369 	 *		handle_graph_update_event()
50741514Srm88369 	 *		dgraph_set_instance_state()
50751514Srm88369 	 *		graph_post_X_[online|offline]()
50761514Srm88369 	 *		graph_runlevel_changed()
50771514Srm88369 	 *		signal_init()
50781514Srm88369 	 *			init/lscf_set_runlevel()
50791514Srm88369 	 *				process_pg_event()
50801514Srm88369 	 *				dgraph_set_runlevel()
50811514Srm88369 	 *
50821514Srm88369 	 * then we already passed through graph_runlevel_changed() (by the way
50831514Srm88369 	 * of dgraph_set_milestone()) and 'current_runlevel' may have changed
50841514Srm88369 	 * and already be equal to 'rl' so we are going to return immediately
50851514Srm88369 	 * from dgraph_set_runlevel() without changing the system runlevel and
50861514Srm88369 	 * without executing the /etc/rc?.d/K* scripts.
50871514Srm88369 	 */
50880Sstevel@tonic-gate 	if (rl == current_runlevel) {
50890Sstevel@tonic-gate 		ms = NULL;
50900Sstevel@tonic-gate 		goto out;
50910Sstevel@tonic-gate 	}
50920Sstevel@tonic-gate 
50930Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Changing to runlevel '%c'.\n", rl);
50940Sstevel@tonic-gate 
50950Sstevel@tonic-gate 	/*
50960Sstevel@tonic-gate 	 * Make sure stop rc scripts see the new settings via who -r.
50970Sstevel@tonic-gate 	 */
50980Sstevel@tonic-gate 	utmpx_set_runlevel(rl, current_runlevel, B_TRUE);
50990Sstevel@tonic-gate 
51000Sstevel@tonic-gate 	/*
51010Sstevel@tonic-gate 	 * Some run levels don't have a direct correspondence to any
51020Sstevel@tonic-gate 	 * milestones, so we have to signal init directly.
51030Sstevel@tonic-gate 	 */
51040Sstevel@tonic-gate 	if (mark_rl) {
51050Sstevel@tonic-gate 		current_runlevel = rl;
51060Sstevel@tonic-gate 		signal_init(rl);
51070Sstevel@tonic-gate 	}
51080Sstevel@tonic-gate 
51090Sstevel@tonic-gate 	switch (rl) {
51100Sstevel@tonic-gate 	case 'S':
51110Sstevel@tonic-gate 		uu_warn("The system is coming down for administration.  "
51120Sstevel@tonic-gate 		    "Please wait.\n");
51130Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_FALSE);
51140Sstevel@tonic-gate 		ms = single_user_fmri;
51150Sstevel@tonic-gate 		go_single_user_mode = B_TRUE;
51160Sstevel@tonic-gate 		break;
51170Sstevel@tonic-gate 
51180Sstevel@tonic-gate 	case '0':
51198944Sdp@eng.sun.com 		halting_time = time(NULL);
51200Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
51210Sstevel@tonic-gate 		halting = AD_HALT;
51220Sstevel@tonic-gate 		goto uadmin;
51230Sstevel@tonic-gate 
51240Sstevel@tonic-gate 	case '5':
51258944Sdp@eng.sun.com 		halting_time = time(NULL);
51260Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
51270Sstevel@tonic-gate 		halting = AD_POWEROFF;
51280Sstevel@tonic-gate 		goto uadmin;
51290Sstevel@tonic-gate 
51300Sstevel@tonic-gate 	case '6':
51318944Sdp@eng.sun.com 		halting_time = time(NULL);
51320Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
51339160SSherry.Moore@Sun.COM 		if (scf_is_fastboot_default() && getzoneid() == GLOBAL_ZONEID)
51349160SSherry.Moore@Sun.COM 			halting = AD_FASTREBOOT;
51359160SSherry.Moore@Sun.COM 		else
51369160SSherry.Moore@Sun.COM 			halting = AD_BOOT;
51370Sstevel@tonic-gate 
51380Sstevel@tonic-gate uadmin:
51390Sstevel@tonic-gate 		uu_warn("The system is coming down.  Please wait.\n");
51400Sstevel@tonic-gate 		ms = "none";
51410Sstevel@tonic-gate 
51420Sstevel@tonic-gate 		/*
51430Sstevel@tonic-gate 		 * We can't wait until all services are offline since this
51440Sstevel@tonic-gate 		 * thread is responsible for taking them offline.  Instead we
51450Sstevel@tonic-gate 		 * set halting to the second argument for uadmin() and call
51460Sstevel@tonic-gate 		 * do_uadmin() from dgraph_set_instance_state() when
51470Sstevel@tonic-gate 		 * appropriate.
51480Sstevel@tonic-gate 		 */
51490Sstevel@tonic-gate 		break;
51500Sstevel@tonic-gate 
51510Sstevel@tonic-gate 	case '1':
51520Sstevel@tonic-gate 		if (current_runlevel != 'S') {
51530Sstevel@tonic-gate 			uu_warn("Changing to state 1.\n");
51540Sstevel@tonic-gate 			fork_rc_script(rl, stop, B_FALSE);
51550Sstevel@tonic-gate 		} else {
51560Sstevel@tonic-gate 			uu_warn("The system is coming up for administration.  "
51570Sstevel@tonic-gate 			    "Please wait.\n");
51580Sstevel@tonic-gate 		}
51590Sstevel@tonic-gate 		ms = single_user_fmri;
51600Sstevel@tonic-gate 		go_to_level1 = B_TRUE;
51610Sstevel@tonic-gate 		break;
51620Sstevel@tonic-gate 
51630Sstevel@tonic-gate 	case '2':
51640Sstevel@tonic-gate 		if (current_runlevel == '3' || current_runlevel == '4')
51650Sstevel@tonic-gate 			fork_rc_script(rl, stop, B_FALSE);
51660Sstevel@tonic-gate 		ms = multi_user_fmri;
51670Sstevel@tonic-gate 		break;
51680Sstevel@tonic-gate 
51690Sstevel@tonic-gate 	case '3':
51700Sstevel@tonic-gate 	case '4':
51710Sstevel@tonic-gate 		ms = "all";
51720Sstevel@tonic-gate 		break;
51730Sstevel@tonic-gate 
51740Sstevel@tonic-gate 	default:
51750Sstevel@tonic-gate #ifndef NDEBUG
51760Sstevel@tonic-gate 		(void) fprintf(stderr, "%s:%d: Uncaught case %d ('%c').\n",
51770Sstevel@tonic-gate 		    __FILE__, __LINE__, rl, rl);
51780Sstevel@tonic-gate #endif
51790Sstevel@tonic-gate 		abort();
51800Sstevel@tonic-gate 	}
51810Sstevel@tonic-gate 
51820Sstevel@tonic-gate out:
51830Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
51840Sstevel@tonic-gate 
51850Sstevel@tonic-gate nolock_out:
51860Sstevel@tonic-gate 	switch (r = libscf_clear_runlevel(pg, ms)) {
51870Sstevel@tonic-gate 	case 0:
51880Sstevel@tonic-gate 		break;
51890Sstevel@tonic-gate 
51900Sstevel@tonic-gate 	case ECONNABORTED:
51910Sstevel@tonic-gate 		libscf_handle_rebind(h);
51920Sstevel@tonic-gate 		rebound = B_TRUE;
51930Sstevel@tonic-gate 		goto nolock_out;
51940Sstevel@tonic-gate 
51950Sstevel@tonic-gate 	case ECANCELED:
51960Sstevel@tonic-gate 		break;
51970Sstevel@tonic-gate 
51980Sstevel@tonic-gate 	case EPERM:
51990Sstevel@tonic-gate 	case EACCES:
52000Sstevel@tonic-gate 	case EROFS:
52010Sstevel@tonic-gate 		log_error(LOG_NOTICE, "Could not delete \"%s/%s\" property: "
52020Sstevel@tonic-gate 		    "%s.\n", SCF_PG_OPTIONS, "runlevel", strerror(r));
52030Sstevel@tonic-gate 		break;
52040Sstevel@tonic-gate 
52050Sstevel@tonic-gate 	default:
52060Sstevel@tonic-gate 		bad_error("libscf_clear_runlevel", r);
52070Sstevel@tonic-gate 	}
52080Sstevel@tonic-gate 
52090Sstevel@tonic-gate 	return (rebound ? ECONNRESET : 0);
52100Sstevel@tonic-gate }
52110Sstevel@tonic-gate 
52127630SRenaud.Manus@Sun.COM /*
52137630SRenaud.Manus@Sun.COM  * mark_subtree walks the dependents and add the GV_TOOFFLINE flag
52147630SRenaud.Manus@Sun.COM  * to the instances that are supposed to go offline during an
52157630SRenaud.Manus@Sun.COM  * administrative disable operation.
52167630SRenaud.Manus@Sun.COM  */
52177630SRenaud.Manus@Sun.COM static int
52187630SRenaud.Manus@Sun.COM mark_subtree(graph_edge_t *e, void *arg)
52197630SRenaud.Manus@Sun.COM {
52207630SRenaud.Manus@Sun.COM 	graph_vertex_t *v;
52217630SRenaud.Manus@Sun.COM 	int r;
52227630SRenaud.Manus@Sun.COM 
52237630SRenaud.Manus@Sun.COM 	v = e->ge_vertex;
52247630SRenaud.Manus@Sun.COM 
52257630SRenaud.Manus@Sun.COM 	/* If it's already in the subgraph, skip. */
52267630SRenaud.Manus@Sun.COM 	if (v->gv_flags & GV_TOOFFLINE)
52277630SRenaud.Manus@Sun.COM 		return (UU_WALK_NEXT);
52287630SRenaud.Manus@Sun.COM 
52297630SRenaud.Manus@Sun.COM 	switch (v->gv_type) {
52307630SRenaud.Manus@Sun.COM 	case GVT_INST:
52317630SRenaud.Manus@Sun.COM 		/* If the instance is already disabled, skip it. */
52327630SRenaud.Manus@Sun.COM 		if (!(v->gv_flags & GV_ENABLED))
52337630SRenaud.Manus@Sun.COM 			return (UU_WALK_NEXT);
52347630SRenaud.Manus@Sun.COM 
52357630SRenaud.Manus@Sun.COM 		v->gv_flags |= GV_TOOFFLINE;
52367630SRenaud.Manus@Sun.COM 		log_framework(LOG_DEBUG, "%s added to subtree\n", v->gv_name);
52377630SRenaud.Manus@Sun.COM 		break;
52387630SRenaud.Manus@Sun.COM 	case GVT_GROUP:
52397630SRenaud.Manus@Sun.COM 		/*
52408354SRenaud.Manus@Sun.COM 		 * Skip all excluded and optional_all dependencies and decide
52418354SRenaud.Manus@Sun.COM 		 * whether to offline the service based on restart_on attribute.
52427630SRenaud.Manus@Sun.COM 		 */
52437630SRenaud.Manus@Sun.COM 		if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL ||
52448354SRenaud.Manus@Sun.COM 		    v->gv_depgroup == DEPGRP_OPTIONAL_ALL ||
52457630SRenaud.Manus@Sun.COM 		    v->gv_restart < RERR_RESTART)
52467630SRenaud.Manus@Sun.COM 			return (UU_WALK_NEXT);
52477630SRenaud.Manus@Sun.COM 		break;
52487630SRenaud.Manus@Sun.COM 	}
52497630SRenaud.Manus@Sun.COM 
52507630SRenaud.Manus@Sun.COM 	r = uu_list_walk(v->gv_dependents, (uu_walk_fn_t *)mark_subtree, arg,
52517630SRenaud.Manus@Sun.COM 	    0);
52527630SRenaud.Manus@Sun.COM 	assert(r == 0);
52537630SRenaud.Manus@Sun.COM 	return (UU_WALK_NEXT);
52547630SRenaud.Manus@Sun.COM }
52557630SRenaud.Manus@Sun.COM 
52560Sstevel@tonic-gate static int
52570Sstevel@tonic-gate mark_subgraph(graph_edge_t *e, void *arg)
52580Sstevel@tonic-gate {
52590Sstevel@tonic-gate 	graph_vertex_t *v;
52600Sstevel@tonic-gate 	int r;
52610Sstevel@tonic-gate 	int optional = (int)arg;
52620Sstevel@tonic-gate 
52630Sstevel@tonic-gate 	v = e->ge_vertex;
52640Sstevel@tonic-gate 
52650Sstevel@tonic-gate 	/* If it's already in the subgraph, skip. */
52660Sstevel@tonic-gate 	if (v->gv_flags & GV_INSUBGRAPH)
52670Sstevel@tonic-gate 		return (UU_WALK_NEXT);
52680Sstevel@tonic-gate 
52690Sstevel@tonic-gate 	/*
52700Sstevel@tonic-gate 	 * Keep track if walk has entered an optional dependency group
52710Sstevel@tonic-gate 	 */
52720Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_OPTIONAL_ALL) {
52730Sstevel@tonic-gate 		optional = 1;
52740Sstevel@tonic-gate 	}
52750Sstevel@tonic-gate 	/*
52760Sstevel@tonic-gate 	 * Quit if we are in an optional dependency group and the instance
52770Sstevel@tonic-gate 	 * is disabled
52780Sstevel@tonic-gate 	 */
52790Sstevel@tonic-gate 	if (optional && (v->gv_type == GVT_INST) &&
52800Sstevel@tonic-gate 	    (!(v->gv_flags & GV_ENBLD_NOOVR)))
52810Sstevel@tonic-gate 		return (UU_WALK_NEXT);
52820Sstevel@tonic-gate 
52830Sstevel@tonic-gate 	v->gv_flags |= GV_INSUBGRAPH;
52840Sstevel@tonic-gate 
52850Sstevel@tonic-gate 	/* Skip all excluded dependencies. */
52860Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
52870Sstevel@tonic-gate 		return (UU_WALK_NEXT);
52880Sstevel@tonic-gate 
52890Sstevel@tonic-gate 	r = uu_list_walk(v->gv_dependencies, (uu_walk_fn_t *)mark_subgraph,
52900Sstevel@tonic-gate 	    (void *)optional, 0);
52910Sstevel@tonic-gate 	assert(r == 0);
52920Sstevel@tonic-gate 	return (UU_WALK_NEXT);
52930Sstevel@tonic-gate }
52940Sstevel@tonic-gate 
52950Sstevel@tonic-gate /*
52962747Sbustos  * Bring down all services which are not dependencies of fmri.  The
52972747Sbustos  * dependencies of fmri (direct & indirect) will constitute the "subgraph",
52982747Sbustos  * and will have the GV_INSUBGRAPH flag set.  The rest must be brought down,
52992747Sbustos  * which means the state is "disabled", "maintenance", or "uninitialized".  We
53002747Sbustos  * could consider "offline" to be down, and refrain from sending start
53012747Sbustos  * commands for such services, but that's not strictly necessary, so we'll
53022747Sbustos  * decline to intrude on the state machine.  It would probably confuse users
53032747Sbustos  * anyway.
53042747Sbustos  *
53052747Sbustos  * The services should be brought down in reverse-dependency order, so we
53062747Sbustos  * can't do it all at once here.  We initiate by override-disabling the leaves
53072747Sbustos  * of the dependency tree -- those services which are up but have no
53082747Sbustos  * dependents which are up.  When they come down,
53092747Sbustos  * vertex_subgraph_dependencies_shutdown() will override-disable the newly
53102747Sbustos  * exposed leaves.  Perseverance will ensure completion.
53112747Sbustos  *
53122747Sbustos  * Sometimes we need to take action when the transition is complete, like
53132747Sbustos  * start sulogin or halt the system.  To tell when we're done, we initialize
53142747Sbustos  * non_subgraph_svcs here to be the number of services which need to come
53152747Sbustos  * down.  As each does, we decrement the counter.  When it hits zero, we take
53162747Sbustos  * the appropriate action.  See vertex_subgraph_dependencies_shutdown().
53172747Sbustos  *
53182747Sbustos  * In case we're coming up, we also remove any enable-overrides for the
53192747Sbustos  * services which are dependencies of fmri.
53200Sstevel@tonic-gate  *
53210Sstevel@tonic-gate  * If norepository is true, the function will not change the repository.
53220Sstevel@tonic-gate  *
53231514Srm88369  * The decision to change the system run level in accordance with the milestone
53241514Srm88369  * is taken in dgraph_set_runlevel().
53251514Srm88369  *
53260Sstevel@tonic-gate  * Returns
53270Sstevel@tonic-gate  *   0 - success
53280Sstevel@tonic-gate  *   ECONNRESET - success, but handle was rebound
53290Sstevel@tonic-gate  *   EINVAL - fmri is invalid (error is logged)
53300Sstevel@tonic-gate  *   EALREADY - the milestone is already set to fmri
53310Sstevel@tonic-gate  *   ENOENT - a configured vertex does not exist for fmri (an error is logged)
53320Sstevel@tonic-gate  */
53330Sstevel@tonic-gate static int
53340Sstevel@tonic-gate dgraph_set_milestone(const char *fmri, scf_handle_t *h, boolean_t norepository)
53350Sstevel@tonic-gate {
53360Sstevel@tonic-gate 	const char *cfmri, *fs;
53370Sstevel@tonic-gate 	graph_vertex_t *nm, *v;
53380Sstevel@tonic-gate 	int ret = 0, r;
53390Sstevel@tonic-gate 	scf_instance_t *inst;
53400Sstevel@tonic-gate 	boolean_t isall, isnone, rebound = B_FALSE;
53410Sstevel@tonic-gate 
53420Sstevel@tonic-gate 	/* Validate fmri */
53430Sstevel@tonic-gate 	isall = (strcmp(fmri, "all") == 0);
53440Sstevel@tonic-gate 	isnone = (strcmp(fmri, "none") == 0);
53450Sstevel@tonic-gate 
53460Sstevel@tonic-gate 	if (!isall && !isnone) {
53470Sstevel@tonic-gate 		if (fmri_canonify(fmri, (char **)&cfmri, B_FALSE) == EINVAL)
53480Sstevel@tonic-gate 			goto reject;
53490Sstevel@tonic-gate 
53500Sstevel@tonic-gate 		if (strcmp(cfmri, single_user_fmri) != 0 &&
53510Sstevel@tonic-gate 		    strcmp(cfmri, multi_user_fmri) != 0 &&
53520Sstevel@tonic-gate 		    strcmp(cfmri, multi_user_svr_fmri) != 0) {
53530Sstevel@tonic-gate 			startd_free((void *)cfmri, max_scf_fmri_size);
53540Sstevel@tonic-gate reject:
53550Sstevel@tonic-gate 			log_framework(LOG_WARNING,
53560Sstevel@tonic-gate 			    "Rejecting request for invalid milestone \"%s\".\n",
53570Sstevel@tonic-gate 			    fmri);
53580Sstevel@tonic-gate 			return (EINVAL);
53590Sstevel@tonic-gate 		}
53600Sstevel@tonic-gate 	}
53610Sstevel@tonic-gate 
53620Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
53630Sstevel@tonic-gate 
53640Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
53650Sstevel@tonic-gate 
53660Sstevel@tonic-gate 	if (milestone == NULL) {
53670Sstevel@tonic-gate 		if (isall) {
53680Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
53690Sstevel@tonic-gate 			    "Milestone already set to all.\n");
53700Sstevel@tonic-gate 			ret = EALREADY;
53710Sstevel@tonic-gate 			goto out;
53720Sstevel@tonic-gate 		}
53730Sstevel@tonic-gate 	} else if (milestone == MILESTONE_NONE) {
53740Sstevel@tonic-gate 		if (isnone) {
53750Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
53760Sstevel@tonic-gate 			    "Milestone already set to none.\n");
53770Sstevel@tonic-gate 			ret = EALREADY;
53780Sstevel@tonic-gate 			goto out;
53790Sstevel@tonic-gate 		}
53800Sstevel@tonic-gate 	} else {
53810Sstevel@tonic-gate 		if (!isall && !isnone &&
53820Sstevel@tonic-gate 		    strcmp(cfmri, milestone->gv_name) == 0) {
53830Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
53840Sstevel@tonic-gate 			    "Milestone already set to %s.\n", cfmri);
53850Sstevel@tonic-gate 			ret = EALREADY;
53860Sstevel@tonic-gate 			goto out;
53870Sstevel@tonic-gate 		}
53880Sstevel@tonic-gate 	}
53890Sstevel@tonic-gate 
53900Sstevel@tonic-gate 	if (!isall && !isnone) {
53910Sstevel@tonic-gate 		nm = vertex_get_by_name(cfmri);
53920Sstevel@tonic-gate 		if (nm == NULL || !(nm->gv_flags & GV_CONFIGURED)) {
53930Sstevel@tonic-gate 			log_framework(LOG_WARNING, "Cannot set milestone to %s "
53940Sstevel@tonic-gate 			    "because no such service exists.\n", cfmri);
53950Sstevel@tonic-gate 			ret = ENOENT;
53960Sstevel@tonic-gate 			goto out;
53970Sstevel@tonic-gate 		}
53980Sstevel@tonic-gate 	}
53990Sstevel@tonic-gate 
54000Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Changing milestone to %s.\n", fmri);
54010Sstevel@tonic-gate 
54020Sstevel@tonic-gate 	/*
54030Sstevel@tonic-gate 	 * Set milestone, removing the old one if this was the last reference.
54040Sstevel@tonic-gate 	 */
54051712Srm88369 	if (milestone > MILESTONE_NONE)
54061712Srm88369 		(void) vertex_unref(milestone);
54070Sstevel@tonic-gate 
54080Sstevel@tonic-gate 	if (isall)
54090Sstevel@tonic-gate 		milestone = NULL;
54100Sstevel@tonic-gate 	else if (isnone)
54110Sstevel@tonic-gate 		milestone = MILESTONE_NONE;
54121712Srm88369 	else {
54130Sstevel@tonic-gate 		milestone = nm;
54141712Srm88369 		/* milestone should count as a reference */
54151712Srm88369 		vertex_ref(milestone);
54161712Srm88369 	}
54170Sstevel@tonic-gate 
54180Sstevel@tonic-gate 	/* Clear all GV_INSUBGRAPH bits. */
54190Sstevel@tonic-gate 	for (v = uu_list_first(dgraph); v != NULL; v = uu_list_next(dgraph, v))
54200Sstevel@tonic-gate 		v->gv_flags &= ~GV_INSUBGRAPH;
54210Sstevel@tonic-gate 
54220Sstevel@tonic-gate 	if (!isall && !isnone) {
54230Sstevel@tonic-gate 		/* Set GV_INSUBGRAPH for milestone & descendents. */
54240Sstevel@tonic-gate 		milestone->gv_flags |= GV_INSUBGRAPH;
54250Sstevel@tonic-gate 
54260Sstevel@tonic-gate 		r = uu_list_walk(milestone->gv_dependencies,
54270Sstevel@tonic-gate 		    (uu_walk_fn_t *)mark_subgraph, NULL, 0);
54280Sstevel@tonic-gate 		assert(r == 0);
54290Sstevel@tonic-gate 	}
54300Sstevel@tonic-gate 
54310Sstevel@tonic-gate 	/* Un-override services in the subgraph & override-disable the rest. */
54320Sstevel@tonic-gate 	if (norepository)
54330Sstevel@tonic-gate 		goto out;
54340Sstevel@tonic-gate 
54350Sstevel@tonic-gate 	non_subgraph_svcs = 0;
54360Sstevel@tonic-gate 	for (v = uu_list_first(dgraph);
54370Sstevel@tonic-gate 	    v != NULL;
54380Sstevel@tonic-gate 	    v = uu_list_next(dgraph, v)) {
54390Sstevel@tonic-gate 		if (v->gv_type != GVT_INST ||
54400Sstevel@tonic-gate 		    (v->gv_flags & GV_CONFIGURED) == 0)
54410Sstevel@tonic-gate 			continue;
54420Sstevel@tonic-gate 
54430Sstevel@tonic-gate again:
54440Sstevel@tonic-gate 		r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst,
54450Sstevel@tonic-gate 		    NULL, NULL, SCF_DECODE_FMRI_EXACT);
54460Sstevel@tonic-gate 		if (r != 0) {
54470Sstevel@tonic-gate 			switch (scf_error()) {
54480Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
54490Sstevel@tonic-gate 			default:
54500Sstevel@tonic-gate 				libscf_handle_rebind(h);
54510Sstevel@tonic-gate 				rebound = B_TRUE;
54520Sstevel@tonic-gate 				goto again;
54530Sstevel@tonic-gate 
54540Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
54550Sstevel@tonic-gate 				continue;
54560Sstevel@tonic-gate 
54570Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
54580Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
54590Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
54600Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
54610Sstevel@tonic-gate 				bad_error("scf_handle_decode_fmri",
54620Sstevel@tonic-gate 				    scf_error());
54630Sstevel@tonic-gate 			}
54640Sstevel@tonic-gate 		}
54650Sstevel@tonic-gate 
54660Sstevel@tonic-gate 		if (isall || (v->gv_flags & GV_INSUBGRAPH)) {
54670Sstevel@tonic-gate 			r = libscf_delete_enable_ovr(inst);
54680Sstevel@tonic-gate 			fs = "libscf_delete_enable_ovr";
54690Sstevel@tonic-gate 		} else {
54700Sstevel@tonic-gate 			assert(isnone || (v->gv_flags & GV_INSUBGRAPH) == 0);
54710Sstevel@tonic-gate 
54722747Sbustos 			/*
54732747Sbustos 			 * Services which are up need to come down before
54742747Sbustos 			 * we're done, but we can only disable the leaves
54752747Sbustos 			 * here.
54762747Sbustos 			 */
54772747Sbustos 
54782747Sbustos 			if (up_state(v->gv_state))
54790Sstevel@tonic-gate 				++non_subgraph_svcs;
54800Sstevel@tonic-gate 
54812747Sbustos 			/* If it's already disabled, don't bother. */
54822747Sbustos 			if ((v->gv_flags & GV_ENABLED) == 0)
54832747Sbustos 				continue;
54842747Sbustos 
54852747Sbustos 			if (!is_nonsubgraph_leaf(v))
54860Sstevel@tonic-gate 				continue;
54870Sstevel@tonic-gate 
54880Sstevel@tonic-gate 			r = libscf_set_enable_ovr(inst, 0);
54890Sstevel@tonic-gate 			fs = "libscf_set_enable_ovr";
54900Sstevel@tonic-gate 		}
54910Sstevel@tonic-gate 		switch (r) {
54920Sstevel@tonic-gate 		case 0:
54930Sstevel@tonic-gate 		case ECANCELED:
54940Sstevel@tonic-gate 			break;
54950Sstevel@tonic-gate 
54960Sstevel@tonic-gate 		case ECONNABORTED:
54970Sstevel@tonic-gate 			libscf_handle_rebind(h);
54980Sstevel@tonic-gate 			rebound = B_TRUE;
54990Sstevel@tonic-gate 			goto again;
55000Sstevel@tonic-gate 
55010Sstevel@tonic-gate 		case EPERM:
55020Sstevel@tonic-gate 		case EROFS:
55030Sstevel@tonic-gate 			log_error(LOG_WARNING,
55040Sstevel@tonic-gate 			    "Could not set %s/%s for %s: %s.\n",
55050Sstevel@tonic-gate 			    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
55060Sstevel@tonic-gate 			    v->gv_name, strerror(r));
55070Sstevel@tonic-gate 			break;
55080Sstevel@tonic-gate 
55090Sstevel@tonic-gate 		default:
55100Sstevel@tonic-gate 			bad_error(fs, r);
55110Sstevel@tonic-gate 		}
55120Sstevel@tonic-gate 	}
55130Sstevel@tonic-gate 
55140Sstevel@tonic-gate 	if (halting != -1) {
55150Sstevel@tonic-gate 		if (non_subgraph_svcs > 1)
55160Sstevel@tonic-gate 			uu_warn("%d system services are now being stopped.\n",
55170Sstevel@tonic-gate 			    non_subgraph_svcs);
55180Sstevel@tonic-gate 		else if (non_subgraph_svcs == 1)
55190Sstevel@tonic-gate 			uu_warn("One system service is now being stopped.\n");
55200Sstevel@tonic-gate 		else if (non_subgraph_svcs == 0)
55210Sstevel@tonic-gate 			do_uadmin();
55220Sstevel@tonic-gate 	}
55230Sstevel@tonic-gate 
55240Sstevel@tonic-gate 	ret = rebound ? ECONNRESET : 0;
55250Sstevel@tonic-gate 
55260Sstevel@tonic-gate out:
55270Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
55280Sstevel@tonic-gate 	if (!isall && !isnone)
55290Sstevel@tonic-gate 		startd_free((void *)cfmri, max_scf_fmri_size);
55300Sstevel@tonic-gate 	scf_instance_destroy(inst);
55310Sstevel@tonic-gate 	return (ret);
55320Sstevel@tonic-gate }
55330Sstevel@tonic-gate 
55340Sstevel@tonic-gate 
55350Sstevel@tonic-gate /*
55360Sstevel@tonic-gate  * Returns 0, ECONNABORTED, or EINVAL.
55370Sstevel@tonic-gate  */
55380Sstevel@tonic-gate static int
55390Sstevel@tonic-gate handle_graph_update_event(scf_handle_t *h, graph_protocol_event_t *e)
55400Sstevel@tonic-gate {
55410Sstevel@tonic-gate 	int r;
55420Sstevel@tonic-gate 
55430Sstevel@tonic-gate 	switch (e->gpe_type) {
55440Sstevel@tonic-gate 	case GRAPH_UPDATE_RELOAD_GRAPH:
55450Sstevel@tonic-gate 		log_error(LOG_WARNING,
55460Sstevel@tonic-gate 		    "graph_event: reload graph unimplemented\n");
55470Sstevel@tonic-gate 		break;
55480Sstevel@tonic-gate 
55490Sstevel@tonic-gate 	case GRAPH_UPDATE_STATE_CHANGE: {
55500Sstevel@tonic-gate 		protocol_states_t *states = e->gpe_data;
55510Sstevel@tonic-gate 
55520Sstevel@tonic-gate 		switch (r = dgraph_set_instance_state(h, e->gpe_inst,
55530Sstevel@tonic-gate 		    states->ps_state, states->ps_err)) {
55540Sstevel@tonic-gate 		case 0:
55550Sstevel@tonic-gate 		case ENOENT:
55560Sstevel@tonic-gate 			break;
55570Sstevel@tonic-gate 
55580Sstevel@tonic-gate 		case ECONNABORTED:
55590Sstevel@tonic-gate 			return (ECONNABORTED);
55600Sstevel@tonic-gate 
55610Sstevel@tonic-gate 		case EINVAL:
55620Sstevel@tonic-gate 		default:
55630Sstevel@tonic-gate #ifndef NDEBUG
55640Sstevel@tonic-gate 			(void) fprintf(stderr, "dgraph_set_instance_state() "
55650Sstevel@tonic-gate 			    "failed with unexpected error %d at %s:%d.\n", r,
55660Sstevel@tonic-gate 			    __FILE__, __LINE__);
55670Sstevel@tonic-gate #endif
55680Sstevel@tonic-gate 			abort();
55690Sstevel@tonic-gate 		}
55700Sstevel@tonic-gate 
55710Sstevel@tonic-gate 		startd_free(states, sizeof (protocol_states_t));
55720Sstevel@tonic-gate 		break;
55730Sstevel@tonic-gate 	}
55740Sstevel@tonic-gate 
55750Sstevel@tonic-gate 	default:
55760Sstevel@tonic-gate 		log_error(LOG_WARNING,
55770Sstevel@tonic-gate 		    "graph_event_loop received an unknown event: %d\n",
55780Sstevel@tonic-gate 		    e->gpe_type);
55790Sstevel@tonic-gate 		break;
55800Sstevel@tonic-gate 	}
55810Sstevel@tonic-gate 
55820Sstevel@tonic-gate 	return (0);
55830Sstevel@tonic-gate }
55840Sstevel@tonic-gate 
55850Sstevel@tonic-gate /*
55860Sstevel@tonic-gate  * graph_event_thread()
55870Sstevel@tonic-gate  *    Wait for state changes from the restarters.
55880Sstevel@tonic-gate  */
55890Sstevel@tonic-gate /*ARGSUSED*/
55900Sstevel@tonic-gate void *
55910Sstevel@tonic-gate graph_event_thread(void *unused)
55920Sstevel@tonic-gate {
55930Sstevel@tonic-gate 	scf_handle_t *h;
55940Sstevel@tonic-gate 	int err;
55950Sstevel@tonic-gate 
55960Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
55970Sstevel@tonic-gate 
55980Sstevel@tonic-gate 	/*CONSTCOND*/
55990Sstevel@tonic-gate 	while (1) {
56000Sstevel@tonic-gate 		graph_protocol_event_t *e;
56010Sstevel@tonic-gate 
56020Sstevel@tonic-gate 		MUTEX_LOCK(&gu->gu_lock);
56030Sstevel@tonic-gate 
56040Sstevel@tonic-gate 		while (gu->gu_wakeup == 0)
56050Sstevel@tonic-gate 			(void) pthread_cond_wait(&gu->gu_cv, &gu->gu_lock);
56060Sstevel@tonic-gate 
56070Sstevel@tonic-gate 		gu->gu_wakeup = 0;
56080Sstevel@tonic-gate 
56090Sstevel@tonic-gate 		while ((e = graph_event_dequeue()) != NULL) {
56100Sstevel@tonic-gate 			MUTEX_LOCK(&e->gpe_lock);
56110Sstevel@tonic-gate 			MUTEX_UNLOCK(&gu->gu_lock);
56120Sstevel@tonic-gate 
56130Sstevel@tonic-gate 			while ((err = handle_graph_update_event(h, e)) ==
56140Sstevel@tonic-gate 			    ECONNABORTED)
56150Sstevel@tonic-gate 				libscf_handle_rebind(h);
56160Sstevel@tonic-gate 
56170Sstevel@tonic-gate 			if (err == 0)
56180Sstevel@tonic-gate 				graph_event_release(e);
56190Sstevel@tonic-gate 			else
56200Sstevel@tonic-gate 				graph_event_requeue(e);
56210Sstevel@tonic-gate 
56220Sstevel@tonic-gate 			MUTEX_LOCK(&gu->gu_lock);
56230Sstevel@tonic-gate 		}
56240Sstevel@tonic-gate 
56250Sstevel@tonic-gate 		MUTEX_UNLOCK(&gu->gu_lock);
56260Sstevel@tonic-gate 	}
56270Sstevel@tonic-gate 
56280Sstevel@tonic-gate 	/*
56290Sstevel@tonic-gate 	 * Unreachable for now -- there's currently no graceful cleanup
56300Sstevel@tonic-gate 	 * called on exit().
56310Sstevel@tonic-gate 	 */
56320Sstevel@tonic-gate 	MUTEX_UNLOCK(&gu->gu_lock);
56330Sstevel@tonic-gate 	scf_handle_destroy(h);
56340Sstevel@tonic-gate 	return (NULL);
56350Sstevel@tonic-gate }
56360Sstevel@tonic-gate 
56370Sstevel@tonic-gate static void
56380Sstevel@tonic-gate set_initial_milestone(scf_handle_t *h)
56390Sstevel@tonic-gate {
56400Sstevel@tonic-gate 	scf_instance_t *inst;
56410Sstevel@tonic-gate 	char *fmri, *cfmri;
56420Sstevel@tonic-gate 	size_t sz;
56430Sstevel@tonic-gate 	int r;
56440Sstevel@tonic-gate 
56450Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
56460Sstevel@tonic-gate 	fmri = startd_alloc(max_scf_fmri_size);
56470Sstevel@tonic-gate 
56480Sstevel@tonic-gate 	/*
56490Sstevel@tonic-gate 	 * If -m milestone= was specified, we want to set options_ovr/milestone
56500Sstevel@tonic-gate 	 * to it.  Otherwise we want to read what the milestone should be set
56510Sstevel@tonic-gate 	 * to.  Either way we need our inst.
56520Sstevel@tonic-gate 	 */
56530Sstevel@tonic-gate get_self:
56540Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
56550Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
56560Sstevel@tonic-gate 		switch (scf_error()) {
56570Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
56580Sstevel@tonic-gate 			libscf_handle_rebind(h);
56590Sstevel@tonic-gate 			goto get_self;
56600Sstevel@tonic-gate 
56610Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
56620Sstevel@tonic-gate 			if (st->st_subgraph != NULL &&
56630Sstevel@tonic-gate 			    st->st_subgraph[0] != '\0') {
56640Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
56650Sstevel@tonic-gate 				    max_scf_fmri_size);
56660Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
56670Sstevel@tonic-gate 			} else {
56680Sstevel@tonic-gate 				fmri[0] = '\0';
56690Sstevel@tonic-gate 			}
56700Sstevel@tonic-gate 			break;
56710Sstevel@tonic-gate 
56720Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
56730Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
56740Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
56750Sstevel@tonic-gate 		default:
56760Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
56770Sstevel@tonic-gate 		}
56780Sstevel@tonic-gate 	} else {
56790Sstevel@tonic-gate 		if (st->st_subgraph != NULL && st->st_subgraph[0] != '\0') {
56800Sstevel@tonic-gate 			scf_propertygroup_t *pg;
56810Sstevel@tonic-gate 
56820Sstevel@tonic-gate 			pg = safe_scf_pg_create(h);
56830Sstevel@tonic-gate 
56840Sstevel@tonic-gate 			sz = strlcpy(fmri, st->st_subgraph, max_scf_fmri_size);
56850Sstevel@tonic-gate 			assert(sz < max_scf_fmri_size);
56860Sstevel@tonic-gate 
56870Sstevel@tonic-gate 			r = libscf_inst_get_or_add_pg(inst, SCF_PG_OPTIONS_OVR,
56880Sstevel@tonic-gate 			    SCF_PG_OPTIONS_OVR_TYPE, SCF_PG_OPTIONS_OVR_FLAGS,
56890Sstevel@tonic-gate 			    pg);
56900Sstevel@tonic-gate 			switch (r) {
56910Sstevel@tonic-gate 			case 0:
56920Sstevel@tonic-gate 				break;
56930Sstevel@tonic-gate 
56940Sstevel@tonic-gate 			case ECONNABORTED:
56950Sstevel@tonic-gate 				libscf_handle_rebind(h);
56960Sstevel@tonic-gate 				goto get_self;
56970Sstevel@tonic-gate 
56980Sstevel@tonic-gate 			case EPERM:
56990Sstevel@tonic-gate 			case EACCES:
57000Sstevel@tonic-gate 			case EROFS:
57010Sstevel@tonic-gate 				log_error(LOG_WARNING, "Could not set %s/%s: "
57020Sstevel@tonic-gate 				    "%s.\n", SCF_PG_OPTIONS_OVR,
57030Sstevel@tonic-gate 				    SCF_PROPERTY_MILESTONE, strerror(r));
57040Sstevel@tonic-gate 				/* FALLTHROUGH */
57050Sstevel@tonic-gate 
57060Sstevel@tonic-gate 			case ECANCELED:
57070Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
57080Sstevel@tonic-gate 				    max_scf_fmri_size);
57090Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
57100Sstevel@tonic-gate 				break;
57110Sstevel@tonic-gate 
57120Sstevel@tonic-gate 			default:
57130Sstevel@tonic-gate 				bad_error("libscf_inst_get_or_add_pg", r);
57140Sstevel@tonic-gate 			}
57150Sstevel@tonic-gate 
57160Sstevel@tonic-gate 			r = libscf_clear_runlevel(pg, fmri);
57170Sstevel@tonic-gate 			switch (r) {
57180Sstevel@tonic-gate 			case 0:
57190Sstevel@tonic-gate 				break;
57200Sstevel@tonic-gate 
57210Sstevel@tonic-gate 			case ECONNABORTED:
57220Sstevel@tonic-gate 				libscf_handle_rebind(h);
57230Sstevel@tonic-gate 				goto get_self;
57240Sstevel@tonic-gate 
57250Sstevel@tonic-gate 			case EPERM:
57260Sstevel@tonic-gate 			case EACCES:
57270Sstevel@tonic-gate 			case EROFS:
57280Sstevel@tonic-gate 				log_error(LOG_WARNING, "Could not set %s/%s: "
57290Sstevel@tonic-gate 				    "%s.\n", SCF_PG_OPTIONS_OVR,
57300Sstevel@tonic-gate 				    SCF_PROPERTY_MILESTONE, strerror(r));
57310Sstevel@tonic-gate 				/* FALLTHROUGH */
57320Sstevel@tonic-gate 
57330Sstevel@tonic-gate 			case ECANCELED:
57340Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
57350Sstevel@tonic-gate 				    max_scf_fmri_size);
57360Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
57370Sstevel@tonic-gate 				break;
57380Sstevel@tonic-gate 
57390Sstevel@tonic-gate 			default:
57400Sstevel@tonic-gate 				bad_error("libscf_clear_runlevel", r);
57410Sstevel@tonic-gate 			}
57420Sstevel@tonic-gate 
57430Sstevel@tonic-gate 			scf_pg_destroy(pg);
57440Sstevel@tonic-gate 		} else {
57450Sstevel@tonic-gate 			scf_property_t *prop;
57460Sstevel@tonic-gate 			scf_value_t *val;
57470Sstevel@tonic-gate 
57480Sstevel@tonic-gate 			prop = safe_scf_property_create(h);
57490Sstevel@tonic-gate 			val = safe_scf_value_create(h);
57500Sstevel@tonic-gate 
57510Sstevel@tonic-gate 			r = libscf_get_milestone(inst, prop, val, fmri,
57520Sstevel@tonic-gate 			    max_scf_fmri_size);
57530Sstevel@tonic-gate 			switch (r) {
57540Sstevel@tonic-gate 			case 0:
57550Sstevel@tonic-gate 				break;
57560Sstevel@tonic-gate 
57570Sstevel@tonic-gate 			case ECONNABORTED:
57580Sstevel@tonic-gate 				libscf_handle_rebind(h);
57590Sstevel@tonic-gate 				goto get_self;
57600Sstevel@tonic-gate 
57610Sstevel@tonic-gate 			case EINVAL:
57620Sstevel@tonic-gate 				log_error(LOG_WARNING, "Milestone property is "
57630Sstevel@tonic-gate 				    "misconfigured.  Defaulting to \"all\".\n");
57640Sstevel@tonic-gate 				/* FALLTHROUGH */
57650Sstevel@tonic-gate 
57660Sstevel@tonic-gate 			case ECANCELED:
57670Sstevel@tonic-gate 			case ENOENT:
57680Sstevel@tonic-gate 				fmri[0] = '\0';
57690Sstevel@tonic-gate 				break;
57700Sstevel@tonic-gate 
57710Sstevel@tonic-gate 			default:
57720Sstevel@tonic-gate 				bad_error("libscf_get_milestone", r);
57730Sstevel@tonic-gate 			}
57740Sstevel@tonic-gate 
57750Sstevel@tonic-gate 			scf_value_destroy(val);
57760Sstevel@tonic-gate 			scf_property_destroy(prop);
57770Sstevel@tonic-gate 		}
57780Sstevel@tonic-gate 	}
57790Sstevel@tonic-gate 
57800Sstevel@tonic-gate 	if (fmri[0] == '\0' || strcmp(fmri, "all") == 0)
57810Sstevel@tonic-gate 		goto out;
57820Sstevel@tonic-gate 
57830Sstevel@tonic-gate 	if (strcmp(fmri, "none") != 0) {
57840Sstevel@tonic-gate retry:
57850Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
57860Sstevel@tonic-gate 		    NULL, SCF_DECODE_FMRI_EXACT) != 0) {
57870Sstevel@tonic-gate 			switch (scf_error()) {
57880Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
57890Sstevel@tonic-gate 				log_error(LOG_WARNING,
57900Sstevel@tonic-gate 				    "Requested milestone \"%s\" is invalid.  "
57910Sstevel@tonic-gate 				    "Reverting to \"all\".\n", fmri);
57920Sstevel@tonic-gate 				goto out;
57930Sstevel@tonic-gate 
57940Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
57950Sstevel@tonic-gate 				log_error(LOG_WARNING, "Requested milestone "
57960Sstevel@tonic-gate 				    "\"%s\" does not specify an instance.  "
57970Sstevel@tonic-gate 				    "Reverting to \"all\".\n", fmri);
57980Sstevel@tonic-gate 				goto out;
57990Sstevel@tonic-gate 
58000Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
58010Sstevel@tonic-gate 				libscf_handle_rebind(h);
58020Sstevel@tonic-gate 				goto retry;
58030Sstevel@tonic-gate 
58040Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
58050Sstevel@tonic-gate 				log_error(LOG_WARNING, "Requested milestone "
58060Sstevel@tonic-gate 				    "\"%s\" not in repository.  Reverting to "
58070Sstevel@tonic-gate 				    "\"all\".\n", fmri);
58080Sstevel@tonic-gate 				goto out;
58090Sstevel@tonic-gate 
58100Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
58110Sstevel@tonic-gate 			default:
58120Sstevel@tonic-gate 				bad_error("scf_handle_decode_fmri",
58130Sstevel@tonic-gate 				    scf_error());
58140Sstevel@tonic-gate 			}
58150Sstevel@tonic-gate 		}
58160Sstevel@tonic-gate 
58170Sstevel@tonic-gate 		r = fmri_canonify(fmri, &cfmri, B_FALSE);
58180Sstevel@tonic-gate 		assert(r == 0);
58190Sstevel@tonic-gate 
58200Sstevel@tonic-gate 		r = dgraph_add_instance(cfmri, inst, B_TRUE);
58210Sstevel@tonic-gate 		startd_free(cfmri, max_scf_fmri_size);
58220Sstevel@tonic-gate 		switch (r) {
58230Sstevel@tonic-gate 		case 0:
58240Sstevel@tonic-gate 			break;
58250Sstevel@tonic-gate 
58260Sstevel@tonic-gate 		case ECONNABORTED:
58270Sstevel@tonic-gate 			goto retry;
58280Sstevel@tonic-gate 
58290Sstevel@tonic-gate 		case EINVAL:
58300Sstevel@tonic-gate 			log_error(LOG_WARNING,
58310Sstevel@tonic-gate 			    "Requested milestone \"%s\" is invalid.  "
58320Sstevel@tonic-gate 			    "Reverting to \"all\".\n", fmri);
58330Sstevel@tonic-gate 			goto out;
58340Sstevel@tonic-gate 
58350Sstevel@tonic-gate 		case ECANCELED:
58360Sstevel@tonic-gate 			log_error(LOG_WARNING,
58370Sstevel@tonic-gate 			    "Requested milestone \"%s\" not "
58380Sstevel@tonic-gate 			    "in repository.  Reverting to \"all\".\n",
58390Sstevel@tonic-gate 			    fmri);
58400Sstevel@tonic-gate 			goto out;
58410Sstevel@tonic-gate 
58420Sstevel@tonic-gate 		case EEXIST:
58430Sstevel@tonic-gate 		default:
58440Sstevel@tonic-gate 			bad_error("dgraph_add_instance", r);
58450Sstevel@tonic-gate 		}
58460Sstevel@tonic-gate 	}
58470Sstevel@tonic-gate 
58480Sstevel@tonic-gate 	log_console(LOG_INFO, "Booting to milestone \"%s\".\n", fmri);
58490Sstevel@tonic-gate 
58500Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_FALSE);
58510Sstevel@tonic-gate 	switch (r) {
58520Sstevel@tonic-gate 	case 0:
58530Sstevel@tonic-gate 	case ECONNRESET:
58540Sstevel@tonic-gate 	case EALREADY:
58550Sstevel@tonic-gate 		break;
58560Sstevel@tonic-gate 
58570Sstevel@tonic-gate 	case EINVAL:
58580Sstevel@tonic-gate 	case ENOENT:
58590Sstevel@tonic-gate 	default:
58600Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
58610Sstevel@tonic-gate 	}
58620Sstevel@tonic-gate 
58630Sstevel@tonic-gate out:
58640Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
58650Sstevel@tonic-gate 	scf_instance_destroy(inst);
58660Sstevel@tonic-gate }
58670Sstevel@tonic-gate 
58680Sstevel@tonic-gate void
58690Sstevel@tonic-gate set_restart_milestone(scf_handle_t *h)
58700Sstevel@tonic-gate {
58710Sstevel@tonic-gate 	scf_instance_t *inst;
58720Sstevel@tonic-gate 	scf_property_t *prop;
58730Sstevel@tonic-gate 	scf_value_t *val;
58740Sstevel@tonic-gate 	char *fmri;
58750Sstevel@tonic-gate 	int r;
58760Sstevel@tonic-gate 
58770Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
58780Sstevel@tonic-gate 
58790Sstevel@tonic-gate get_self:
58800Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL,
58810Sstevel@tonic-gate 	    inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
58820Sstevel@tonic-gate 		switch (scf_error()) {
58830Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
58840Sstevel@tonic-gate 			libscf_handle_rebind(h);
58850Sstevel@tonic-gate 			goto get_self;
58860Sstevel@tonic-gate 
58870Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
58880Sstevel@tonic-gate 			break;
58890Sstevel@tonic-gate 
58900Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
58910Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
58920Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
58930Sstevel@tonic-gate 		default:
58940Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
58950Sstevel@tonic-gate 		}
58960Sstevel@tonic-gate 
58970Sstevel@tonic-gate 		scf_instance_destroy(inst);
58980Sstevel@tonic-gate 		return;
58990Sstevel@tonic-gate 	}
59000Sstevel@tonic-gate 
59010Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
59020Sstevel@tonic-gate 	val = safe_scf_value_create(h);
59030Sstevel@tonic-gate 	fmri = startd_alloc(max_scf_fmri_size);
59040Sstevel@tonic-gate 
59050Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size);
59060Sstevel@tonic-gate 	switch (r) {
59070Sstevel@tonic-gate 	case 0:
59080Sstevel@tonic-gate 		break;
59090Sstevel@tonic-gate 
59100Sstevel@tonic-gate 	case ECONNABORTED:
59110Sstevel@tonic-gate 		libscf_handle_rebind(h);
59120Sstevel@tonic-gate 		goto get_self;
59130Sstevel@tonic-gate 
59140Sstevel@tonic-gate 	case ECANCELED:
59150Sstevel@tonic-gate 	case ENOENT:
59160Sstevel@tonic-gate 	case EINVAL:
59170Sstevel@tonic-gate 		goto out;
59180Sstevel@tonic-gate 
59190Sstevel@tonic-gate 	default:
59200Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
59210Sstevel@tonic-gate 	}
59220Sstevel@tonic-gate 
59230Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_TRUE);
59240Sstevel@tonic-gate 	switch (r) {
59250Sstevel@tonic-gate 	case 0:
59260Sstevel@tonic-gate 	case ECONNRESET:
59270Sstevel@tonic-gate 	case EALREADY:
59280Sstevel@tonic-gate 	case EINVAL:
59290Sstevel@tonic-gate 	case ENOENT:
59300Sstevel@tonic-gate 		break;
59310Sstevel@tonic-gate 
59320Sstevel@tonic-gate 	default:
59330Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
59340Sstevel@tonic-gate 	}
59350Sstevel@tonic-gate 
59360Sstevel@tonic-gate out:
59370Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
59380Sstevel@tonic-gate 	scf_value_destroy(val);
59390Sstevel@tonic-gate 	scf_property_destroy(prop);
59400Sstevel@tonic-gate 	scf_instance_destroy(inst);
59410Sstevel@tonic-gate }
59420Sstevel@tonic-gate 
59430Sstevel@tonic-gate /*
59440Sstevel@tonic-gate  * void *graph_thread(void *)
59450Sstevel@tonic-gate  *
59460Sstevel@tonic-gate  * Graph management thread.
59470Sstevel@tonic-gate  */
59480Sstevel@tonic-gate /*ARGSUSED*/
59490Sstevel@tonic-gate void *
59500Sstevel@tonic-gate graph_thread(void *arg)
59510Sstevel@tonic-gate {
59520Sstevel@tonic-gate 	scf_handle_t *h;
59530Sstevel@tonic-gate 	int err;
59540Sstevel@tonic-gate 
59550Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
59560Sstevel@tonic-gate 
59570Sstevel@tonic-gate 	if (st->st_initial)
59580Sstevel@tonic-gate 		set_initial_milestone(h);
59590Sstevel@tonic-gate 
59600Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
59610Sstevel@tonic-gate 	initial_milestone_set = B_TRUE;
59620Sstevel@tonic-gate 	err = pthread_cond_broadcast(&initial_milestone_cv);
59630Sstevel@tonic-gate 	assert(err == 0);
59640Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
59650Sstevel@tonic-gate 
59660Sstevel@tonic-gate 	libscf_populate_graph(h);
59670Sstevel@tonic-gate 
59680Sstevel@tonic-gate 	if (!st->st_initial)
59690Sstevel@tonic-gate 		set_restart_milestone(h);
59700Sstevel@tonic-gate 
59710Sstevel@tonic-gate 	MUTEX_LOCK(&st->st_load_lock);
59720Sstevel@tonic-gate 	st->st_load_complete = 1;
59730Sstevel@tonic-gate 	(void) pthread_cond_broadcast(&st->st_load_cv);
59740Sstevel@tonic-gate 	MUTEX_UNLOCK(&st->st_load_lock);
59750Sstevel@tonic-gate 
59760Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
59770Sstevel@tonic-gate 	/*
59780Sstevel@tonic-gate 	 * Now that we've set st_load_complete we need to check can_come_up()
59790Sstevel@tonic-gate 	 * since if we booted to a milestone, then there won't be any more
59800Sstevel@tonic-gate 	 * state updates.
59810Sstevel@tonic-gate 	 */
59820Sstevel@tonic-gate 	if (!go_single_user_mode && !go_to_level1 &&
59830Sstevel@tonic-gate 	    halting == -1) {
59843639Srm88369 		if (!sulogin_thread_running && !can_come_up()) {
59850Sstevel@tonic-gate 			(void) startd_thread_create(sulogin_thread, NULL);
59860Sstevel@tonic-gate 			sulogin_thread_running = B_TRUE;
59870Sstevel@tonic-gate 		}
59880Sstevel@tonic-gate 	}
59890Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
59900Sstevel@tonic-gate 
59910Sstevel@tonic-gate 	(void) pthread_mutex_lock(&gu->gu_freeze_lock);
59920Sstevel@tonic-gate 
59930Sstevel@tonic-gate 	/*CONSTCOND*/
59940Sstevel@tonic-gate 	while (1) {
59950Sstevel@tonic-gate 		(void) pthread_cond_wait(&gu->gu_freeze_cv,
59960Sstevel@tonic-gate 		    &gu->gu_freeze_lock);
59970Sstevel@tonic-gate 	}
59980Sstevel@tonic-gate 
59990Sstevel@tonic-gate 	/*
60000Sstevel@tonic-gate 	 * Unreachable for now -- there's currently no graceful cleanup
60010Sstevel@tonic-gate 	 * called on exit().
60020Sstevel@tonic-gate 	 */
60030Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&gu->gu_freeze_lock);
60040Sstevel@tonic-gate 	scf_handle_destroy(h);
60050Sstevel@tonic-gate 
60060Sstevel@tonic-gate 	return (NULL);
60070Sstevel@tonic-gate }
60080Sstevel@tonic-gate 
60090Sstevel@tonic-gate 
60100Sstevel@tonic-gate /*
60110Sstevel@tonic-gate  * int next_action()
60120Sstevel@tonic-gate  *   Given an array of timestamps 'a' with 'num' elements, find the
60130Sstevel@tonic-gate  *   lowest non-zero timestamp and return its index. If there are no
60140Sstevel@tonic-gate  *   non-zero elements, return -1.
60150Sstevel@tonic-gate  */
60160Sstevel@tonic-gate static int
60170Sstevel@tonic-gate next_action(hrtime_t *a, int num)
60180Sstevel@tonic-gate {
60190Sstevel@tonic-gate 	hrtime_t t = 0;
60200Sstevel@tonic-gate 	int i = 0, smallest = -1;
60210Sstevel@tonic-gate 
60220Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
60230Sstevel@tonic-gate 		if (t == 0) {
60240Sstevel@tonic-gate 			t = a[i];
60250Sstevel@tonic-gate 			smallest = i;
60260Sstevel@tonic-gate 		} else if (a[i] != 0 && a[i] < t) {
60270Sstevel@tonic-gate 			t = a[i];
60280Sstevel@tonic-gate 			smallest = i;
60290Sstevel@tonic-gate 		}
60300Sstevel@tonic-gate 	}
60310Sstevel@tonic-gate 
60320Sstevel@tonic-gate 	if (t == 0)
60330Sstevel@tonic-gate 		return (-1);
60340Sstevel@tonic-gate 	else
60350Sstevel@tonic-gate 		return (smallest);
60360Sstevel@tonic-gate }
60370Sstevel@tonic-gate 
60380Sstevel@tonic-gate /*
60390Sstevel@tonic-gate  * void process_actions()
60400Sstevel@tonic-gate  *   Process actions requested by the administrator. Possibilities include:
60410Sstevel@tonic-gate  *   refresh, restart, maintenance mode off, maintenance mode on,
60420Sstevel@tonic-gate  *   maintenance mode immediate, and degraded.
60430Sstevel@tonic-gate  *
60440Sstevel@tonic-gate  *   The set of pending actions is represented in the repository as a
60450Sstevel@tonic-gate  *   per-instance property group, with each action being a single property
60460Sstevel@tonic-gate  *   in that group.  This property group is converted to an array, with each
60470Sstevel@tonic-gate  *   action type having an array slot.  The actions in the array at the
60480Sstevel@tonic-gate  *   time process_actions() is called are acted on in the order of the
60490Sstevel@tonic-gate  *   timestamp (which is the value stored in the slot).  A value of zero
60500Sstevel@tonic-gate  *   indicates that there is no pending action of the type associated with
60510Sstevel@tonic-gate  *   a particular slot.
60520Sstevel@tonic-gate  *
60530Sstevel@tonic-gate  *   Sending an action event multiple times before the restarter has a
60540Sstevel@tonic-gate  *   chance to process that action will force it to be run at the last
60550Sstevel@tonic-gate  *   timestamp where it appears in the ordering.
60560Sstevel@tonic-gate  *
60570Sstevel@tonic-gate  *   Turning maintenance mode on trumps all other actions.
60580Sstevel@tonic-gate  *
60590Sstevel@tonic-gate  *   Returns 0 or ECONNABORTED.
60600Sstevel@tonic-gate  */
60610Sstevel@tonic-gate static int
60620Sstevel@tonic-gate process_actions(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst)
60630Sstevel@tonic-gate {
60640Sstevel@tonic-gate 	scf_property_t *prop = NULL;
60650Sstevel@tonic-gate 	scf_value_t *val = NULL;
60660Sstevel@tonic-gate 	scf_type_t type;
60670Sstevel@tonic-gate 	graph_vertex_t *vertex;
60680Sstevel@tonic-gate 	admin_action_t a;
60690Sstevel@tonic-gate 	int i, ret = 0, r;
60700Sstevel@tonic-gate 	hrtime_t action_ts[NACTIONS];
60710Sstevel@tonic-gate 	char *inst_name;
60720Sstevel@tonic-gate 
60730Sstevel@tonic-gate 	r = libscf_instance_get_fmri(inst, &inst_name);
60740Sstevel@tonic-gate 	switch (r) {
60750Sstevel@tonic-gate 	case 0:
60760Sstevel@tonic-gate 		break;
60770Sstevel@tonic-gate 
60780Sstevel@tonic-gate 	case ECONNABORTED:
60790Sstevel@tonic-gate 		return (ECONNABORTED);
60800Sstevel@tonic-gate 
60810Sstevel@tonic-gate 	case ECANCELED:
60820Sstevel@tonic-gate 		return (0);
60830Sstevel@tonic-gate 
60840Sstevel@tonic-gate 	default:
60850Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", r);
60860Sstevel@tonic-gate 	}
60870Sstevel@tonic-gate 
60880Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
60890Sstevel@tonic-gate 
60900Sstevel@tonic-gate 	vertex = vertex_get_by_name(inst_name);
60910Sstevel@tonic-gate 	if (vertex == NULL) {
60920Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
60930Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "%s: Can't find graph vertex. "
60940Sstevel@tonic-gate 		    "The instance must have been removed.\n", inst_name);
609510297SSeth.Goldberg@Sun.COM 		startd_free(inst_name, max_scf_fmri_size);
60960Sstevel@tonic-gate 		return (0);
60970Sstevel@tonic-gate 	}
60980Sstevel@tonic-gate 
60990Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
61000Sstevel@tonic-gate 	val = safe_scf_value_create(h);
61010Sstevel@tonic-gate 
61020Sstevel@tonic-gate 	for (i = 0; i < NACTIONS; i++) {
61030Sstevel@tonic-gate 		if (scf_pg_get_property(pg, admin_actions[i], prop) != 0) {
61040Sstevel@tonic-gate 			switch (scf_error()) {
61050Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
61060Sstevel@tonic-gate 			default:
61070Sstevel@tonic-gate 				ret = ECONNABORTED;
61080Sstevel@tonic-gate 				goto out;
61090Sstevel@tonic-gate 
61100Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
61110Sstevel@tonic-gate 				goto out;
61120Sstevel@tonic-gate 
61130Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
61140Sstevel@tonic-gate 				action_ts[i] = 0;
61150Sstevel@tonic-gate 				continue;
61160Sstevel@tonic-gate 
61170Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
61180Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
61190Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
61200Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
61210Sstevel@tonic-gate 			}
61220Sstevel@tonic-gate 		}
61230Sstevel@tonic-gate 
61240Sstevel@tonic-gate 		if (scf_property_type(prop, &type) != 0) {
61250Sstevel@tonic-gate 			switch (scf_error()) {
61260Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
61270Sstevel@tonic-gate 			default:
61280Sstevel@tonic-gate 				ret = ECONNABORTED;
61290Sstevel@tonic-gate 				goto out;
61300Sstevel@tonic-gate 
61310Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
61320Sstevel@tonic-gate 				action_ts[i] = 0;
61330Sstevel@tonic-gate 				continue;
61340Sstevel@tonic-gate 
61350Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
61360Sstevel@tonic-gate 				bad_error("scf_property_type", scf_error());
61370Sstevel@tonic-gate 			}
61380Sstevel@tonic-gate 		}
61390Sstevel@tonic-gate 
61400Sstevel@tonic-gate 		if (type != SCF_TYPE_INTEGER) {
61410Sstevel@tonic-gate 			action_ts[i] = 0;
61420Sstevel@tonic-gate 			continue;
61430Sstevel@tonic-gate 		}
61440Sstevel@tonic-gate 
61450Sstevel@tonic-gate 		if (scf_property_get_value(prop, val) != 0) {
61460Sstevel@tonic-gate 			switch (scf_error()) {
61470Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
61480Sstevel@tonic-gate 			default:
61490Sstevel@tonic-gate 				ret = ECONNABORTED;
61500Sstevel@tonic-gate 				goto out;
61510Sstevel@tonic-gate 
61520Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
61530Sstevel@tonic-gate 				goto out;
61540Sstevel@tonic-gate 
61550Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
61560Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
61570Sstevel@tonic-gate 				action_ts[i] = 0;
61580Sstevel@tonic-gate 				continue;
61590Sstevel@tonic-gate 
61600Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
61615040Swesolows 			case SCF_ERROR_PERMISSION_DENIED:
61620Sstevel@tonic-gate 				bad_error("scf_property_get_value",
61630Sstevel@tonic-gate 				    scf_error());
61640Sstevel@tonic-gate 			}
61650Sstevel@tonic-gate 		}
61660Sstevel@tonic-gate 
61670Sstevel@tonic-gate 		r = scf_value_get_integer(val, &action_ts[i]);
61680Sstevel@tonic-gate 		assert(r == 0);
61690Sstevel@tonic-gate 	}
61700Sstevel@tonic-gate 
61710Sstevel@tonic-gate 	a = ADMIN_EVENT_MAINT_ON_IMMEDIATE;
61720Sstevel@tonic-gate 	if (action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ||
61730Sstevel@tonic-gate 	    action_ts[ADMIN_EVENT_MAINT_ON]) {
61740Sstevel@tonic-gate 		a = action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ?
61750Sstevel@tonic-gate 		    ADMIN_EVENT_MAINT_ON_IMMEDIATE : ADMIN_EVENT_MAINT_ON;
61760Sstevel@tonic-gate 
61770Sstevel@tonic-gate 		vertex_send_event(vertex, admin_events[a]);
61780Sstevel@tonic-gate 		r = libscf_unset_action(h, pg, a, action_ts[a]);
61790Sstevel@tonic-gate 		switch (r) {
61800Sstevel@tonic-gate 		case 0:
61810Sstevel@tonic-gate 		case EACCES:
61820Sstevel@tonic-gate 			break;
61830Sstevel@tonic-gate 
61840Sstevel@tonic-gate 		case ECONNABORTED:
61850Sstevel@tonic-gate 			ret = ECONNABORTED;
61860Sstevel@tonic-gate 			goto out;
61870Sstevel@tonic-gate 
61880Sstevel@tonic-gate 		case EPERM:
61890Sstevel@tonic-gate 			uu_die("Insufficient privilege.\n");
61900Sstevel@tonic-gate 			/* NOTREACHED */
61910Sstevel@tonic-gate 
61920Sstevel@tonic-gate 		default:
61930Sstevel@tonic-gate 			bad_error("libscf_unset_action", r);
61940Sstevel@tonic-gate 		}
61950Sstevel@tonic-gate 	}
61960Sstevel@tonic-gate 
61970Sstevel@tonic-gate 	while ((a = next_action(action_ts, NACTIONS)) != -1) {
61980Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
61990Sstevel@tonic-gate 		    "Graph: processing %s action for %s.\n", admin_actions[a],
62000Sstevel@tonic-gate 		    inst_name);
62010Sstevel@tonic-gate 
62020Sstevel@tonic-gate 		if (a == ADMIN_EVENT_REFRESH) {
62030Sstevel@tonic-gate 			r = dgraph_refresh_instance(vertex, inst);
62040Sstevel@tonic-gate 			switch (r) {
62050Sstevel@tonic-gate 			case 0:
62060Sstevel@tonic-gate 			case ECANCELED:
62070Sstevel@tonic-gate 			case EINVAL:
62080Sstevel@tonic-gate 			case -1:
62090Sstevel@tonic-gate 				break;
62100Sstevel@tonic-gate 
62110Sstevel@tonic-gate 			case ECONNABORTED:
62120Sstevel@tonic-gate 				/* pg & inst are reset now, so just return. */
62130Sstevel@tonic-gate 				ret = ECONNABORTED;
62140Sstevel@tonic-gate 				goto out;
62150Sstevel@tonic-gate 
62160Sstevel@tonic-gate 			default:
62170Sstevel@tonic-gate 				bad_error("dgraph_refresh_instance", r);
62180Sstevel@tonic-gate 			}
62190Sstevel@tonic-gate 		}
62200Sstevel@tonic-gate 
62210Sstevel@tonic-gate 		vertex_send_event(vertex, admin_events[a]);
62220Sstevel@tonic-gate 
62230Sstevel@tonic-gate 		r = libscf_unset_action(h, pg, a, action_ts[a]);
62240Sstevel@tonic-gate 		switch (r) {
62250Sstevel@tonic-gate 		case 0:
62260Sstevel@tonic-gate 		case EACCES:
62270Sstevel@tonic-gate 			break;
62280Sstevel@tonic-gate 
62290Sstevel@tonic-gate 		case ECONNABORTED:
62300Sstevel@tonic-gate 			ret = ECONNABORTED;
62310Sstevel@tonic-gate 			goto out;
62320Sstevel@tonic-gate 
62330Sstevel@tonic-gate 		case EPERM:
62340Sstevel@tonic-gate 			uu_die("Insufficient privilege.\n");
62350Sstevel@tonic-gate 			/* NOTREACHED */
62360Sstevel@tonic-gate 
62370Sstevel@tonic-gate 		default:
62380Sstevel@tonic-gate 			bad_error("libscf_unset_action", r);
62390Sstevel@tonic-gate 		}
62400Sstevel@tonic-gate 
62410Sstevel@tonic-gate 		action_ts[a] = 0;
62420Sstevel@tonic-gate 	}
62430Sstevel@tonic-gate 
62440Sstevel@tonic-gate out:
62450Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
62460Sstevel@tonic-gate 
62470Sstevel@tonic-gate 	scf_property_destroy(prop);
62480Sstevel@tonic-gate 	scf_value_destroy(val);
62490Sstevel@tonic-gate 	startd_free(inst_name, max_scf_fmri_size);
62500Sstevel@tonic-gate 	return (ret);
62510Sstevel@tonic-gate }
62520Sstevel@tonic-gate 
62530Sstevel@tonic-gate /*
62540Sstevel@tonic-gate  * inst and pg_name are scratch space, and are unset on entry.
62550Sstevel@tonic-gate  * Returns
62560Sstevel@tonic-gate  *   0 - success
62570Sstevel@tonic-gate  *   ECONNRESET - success, but repository handle rebound
62580Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
62590Sstevel@tonic-gate  */
62600Sstevel@tonic-gate static int
62610Sstevel@tonic-gate process_pg_event(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst,
62620Sstevel@tonic-gate     char *pg_name)
62630Sstevel@tonic-gate {
62640Sstevel@tonic-gate 	int r;
62650Sstevel@tonic-gate 	scf_property_t *prop;
62660Sstevel@tonic-gate 	scf_value_t *val;
62670Sstevel@tonic-gate 	char *fmri;
62680Sstevel@tonic-gate 	boolean_t rebound = B_FALSE, rebind_inst = B_FALSE;
62690Sstevel@tonic-gate 
62700Sstevel@tonic-gate 	if (scf_pg_get_name(pg, pg_name, max_scf_value_size) < 0) {
62710Sstevel@tonic-gate 		switch (scf_error()) {
62720Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
62730Sstevel@tonic-gate 		default:
62740Sstevel@tonic-gate 			return (ECONNABORTED);
62750Sstevel@tonic-gate 
62760Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
62770Sstevel@tonic-gate 			return (0);
62780Sstevel@tonic-gate 
62790Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
62800Sstevel@tonic-gate 			bad_error("scf_pg_get_name", scf_error());
62810Sstevel@tonic-gate 		}
62820Sstevel@tonic-gate 	}
62830Sstevel@tonic-gate 
62840Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_GENERAL) == 0 ||
62850Sstevel@tonic-gate 	    strcmp(pg_name, SCF_PG_GENERAL_OVR) == 0) {
62860Sstevel@tonic-gate 		r = dgraph_update_general(pg);
62870Sstevel@tonic-gate 		switch (r) {
62880Sstevel@tonic-gate 		case 0:
62890Sstevel@tonic-gate 		case ENOTSUP:
62900Sstevel@tonic-gate 		case ECANCELED:
62910Sstevel@tonic-gate 			return (0);
62920Sstevel@tonic-gate 
62930Sstevel@tonic-gate 		case ECONNABORTED:
62940Sstevel@tonic-gate 			return (ECONNABORTED);
62950Sstevel@tonic-gate 
62960Sstevel@tonic-gate 		case -1:
62970Sstevel@tonic-gate 			/* Error should have been logged. */
62980Sstevel@tonic-gate 			return (0);
62990Sstevel@tonic-gate 
63000Sstevel@tonic-gate 		default:
63010Sstevel@tonic-gate 			bad_error("dgraph_update_general", r);
63020Sstevel@tonic-gate 		}
63030Sstevel@tonic-gate 	} else if (strcmp(pg_name, SCF_PG_RESTARTER_ACTIONS) == 0) {
63040Sstevel@tonic-gate 		if (scf_pg_get_parent_instance(pg, inst) != 0) {
63050Sstevel@tonic-gate 			switch (scf_error()) {
63060Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
63070Sstevel@tonic-gate 				return (ECONNABORTED);
63080Sstevel@tonic-gate 
63090Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
63100Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
63110Sstevel@tonic-gate 				/* Ignore commands on services. */
63120Sstevel@tonic-gate 				return (0);
63130Sstevel@tonic-gate 
63140Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
63150Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
63160Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
63170Sstevel@tonic-gate 			default:
63180Sstevel@tonic-gate 				bad_error("scf_pg_get_parent_instance",
63190Sstevel@tonic-gate 				    scf_error());
63200Sstevel@tonic-gate 			}
63210Sstevel@tonic-gate 		}
63220Sstevel@tonic-gate 
63230Sstevel@tonic-gate 		return (process_actions(h, pg, inst));
63240Sstevel@tonic-gate 	}
63250Sstevel@tonic-gate 
63260Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_OPTIONS) != 0 &&
63270Sstevel@tonic-gate 	    strcmp(pg_name, SCF_PG_OPTIONS_OVR) != 0)
63280Sstevel@tonic-gate 		return (0);
63290Sstevel@tonic-gate 
63300Sstevel@tonic-gate 	/*
63310Sstevel@tonic-gate 	 * We only care about the options[_ovr] property groups of our own
63320Sstevel@tonic-gate 	 * instance, so get the fmri and compare.  Plus, once we know it's
63330Sstevel@tonic-gate 	 * correct, if the repository connection is broken we know exactly what
63340Sstevel@tonic-gate 	 * property group we were operating on, and can look it up again.
63350Sstevel@tonic-gate 	 */
63360Sstevel@tonic-gate 	if (scf_pg_get_parent_instance(pg, inst) != 0) {
63370Sstevel@tonic-gate 		switch (scf_error()) {
63380Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
63390Sstevel@tonic-gate 			return (ECONNABORTED);
63400Sstevel@tonic-gate 
63410Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
63420Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
63430Sstevel@tonic-gate 			return (0);
63440Sstevel@tonic-gate 
63450Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
63460Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
63470Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
63480Sstevel@tonic-gate 		default:
63490Sstevel@tonic-gate 			bad_error("scf_pg_get_parent_instance",
63500Sstevel@tonic-gate 			    scf_error());
63510Sstevel@tonic-gate 		}
63520Sstevel@tonic-gate 	}
63530Sstevel@tonic-gate 
63540Sstevel@tonic-gate 	switch (r = libscf_instance_get_fmri(inst, &fmri)) {
63550Sstevel@tonic-gate 	case 0:
63560Sstevel@tonic-gate 		break;
63570Sstevel@tonic-gate 
63580Sstevel@tonic-gate 	case ECONNABORTED:
63590Sstevel@tonic-gate 		return (ECONNABORTED);
63600Sstevel@tonic-gate 
63610Sstevel@tonic-gate 	case ECANCELED:
63620Sstevel@tonic-gate 		return (0);
63630Sstevel@tonic-gate 
63640Sstevel@tonic-gate 	default:
63650Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", r);
63660Sstevel@tonic-gate 	}
63670Sstevel@tonic-gate 
63680Sstevel@tonic-gate 	if (strcmp(fmri, SCF_SERVICE_STARTD) != 0) {
63690Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
63700Sstevel@tonic-gate 		return (0);
63710Sstevel@tonic-gate 	}
63720Sstevel@tonic-gate 
63730Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
63740Sstevel@tonic-gate 	val = safe_scf_value_create(h);
63750Sstevel@tonic-gate 
63760Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_OPTIONS_OVR) == 0) {
63770Sstevel@tonic-gate 		/* See if we need to set the runlevel. */
63780Sstevel@tonic-gate 		/* CONSTCOND */
63790Sstevel@tonic-gate 		if (0) {
63800Sstevel@tonic-gate rebind_pg:
63810Sstevel@tonic-gate 			libscf_handle_rebind(h);
63820Sstevel@tonic-gate 			rebound = B_TRUE;
63830Sstevel@tonic-gate 
63840Sstevel@tonic-gate 			r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst);
63850Sstevel@tonic-gate 			switch (r) {
63860Sstevel@tonic-gate 			case 0:
63870Sstevel@tonic-gate 				break;
63880Sstevel@tonic-gate 
63890Sstevel@tonic-gate 			case ECONNABORTED:
63900Sstevel@tonic-gate 				goto rebind_pg;
63910Sstevel@tonic-gate 
63920Sstevel@tonic-gate 			case ENOENT:
63930Sstevel@tonic-gate 				goto out;
63940Sstevel@tonic-gate 
63950Sstevel@tonic-gate 			case EINVAL:
63960Sstevel@tonic-gate 			case ENOTSUP:
63970Sstevel@tonic-gate 				bad_error("libscf_lookup_instance", r);
63980Sstevel@tonic-gate 			}
63990Sstevel@tonic-gate 
64000Sstevel@tonic-gate 			if (scf_instance_get_pg(inst, pg_name, pg) != 0) {
64010Sstevel@tonic-gate 				switch (scf_error()) {
64020Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
64030Sstevel@tonic-gate 				case SCF_ERROR_NOT_FOUND:
64040Sstevel@tonic-gate 					goto out;
64050Sstevel@tonic-gate 
64060Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
64070Sstevel@tonic-gate 					goto rebind_pg;
64080Sstevel@tonic-gate 
64090Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
64100Sstevel@tonic-gate 				case SCF_ERROR_NOT_BOUND:
64110Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
64120Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
64130Sstevel@tonic-gate 				default:
64140Sstevel@tonic-gate 					bad_error("scf_instance_get_pg",
64150Sstevel@tonic-gate 					    scf_error());
64160Sstevel@tonic-gate 				}
64170Sstevel@tonic-gate 			}
64180Sstevel@tonic-gate 		}
64190Sstevel@tonic-gate 
64200Sstevel@tonic-gate 		if (scf_pg_get_property(pg, "runlevel", prop) == 0) {
64210Sstevel@tonic-gate 			r = dgraph_set_runlevel(pg, prop);
64220Sstevel@tonic-gate 			switch (r) {
64230Sstevel@tonic-gate 			case ECONNRESET:
64240Sstevel@tonic-gate 				rebound = B_TRUE;
64250Sstevel@tonic-gate 				rebind_inst = B_TRUE;
64260Sstevel@tonic-gate 				/* FALLTHROUGH */
64270Sstevel@tonic-gate 
64280Sstevel@tonic-gate 			case 0:
64290Sstevel@tonic-gate 				break;
64300Sstevel@tonic-gate 
64310Sstevel@tonic-gate 			case ECONNABORTED:
64320Sstevel@tonic-gate 				goto rebind_pg;
64330Sstevel@tonic-gate 
64340Sstevel@tonic-gate 			case ECANCELED:
64350Sstevel@tonic-gate 				goto out;
64360Sstevel@tonic-gate 
64370Sstevel@tonic-gate 			default:
64380Sstevel@tonic-gate 				bad_error("dgraph_set_runlevel", r);
64390Sstevel@tonic-gate 			}
64400Sstevel@tonic-gate 		} else {
64410Sstevel@tonic-gate 			switch (scf_error()) {
64420Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
64430Sstevel@tonic-gate 			default:
64440Sstevel@tonic-gate 				goto rebind_pg;
64450Sstevel@tonic-gate 
64460Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
64470Sstevel@tonic-gate 				goto out;
64480Sstevel@tonic-gate 
64490Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
64500Sstevel@tonic-gate 				break;
64510Sstevel@tonic-gate 
64520Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
64530Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
64540Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
64550Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
64560Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
64570Sstevel@tonic-gate 			}
64580Sstevel@tonic-gate 		}
64590Sstevel@tonic-gate 	}
64600Sstevel@tonic-gate 
64610Sstevel@tonic-gate 	if (rebind_inst) {
64620Sstevel@tonic-gate lookup_inst:
64630Sstevel@tonic-gate 		r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst);
64640Sstevel@tonic-gate 		switch (r) {
64650Sstevel@tonic-gate 		case 0:
64660Sstevel@tonic-gate 			break;
64670Sstevel@tonic-gate 
64680Sstevel@tonic-gate 		case ECONNABORTED:
64690Sstevel@tonic-gate 			libscf_handle_rebind(h);
64700Sstevel@tonic-gate 			rebound = B_TRUE;
64710Sstevel@tonic-gate 			goto lookup_inst;
64720Sstevel@tonic-gate 
64730Sstevel@tonic-gate 		case ENOENT:
64740Sstevel@tonic-gate 			goto out;
64750Sstevel@tonic-gate 
64760Sstevel@tonic-gate 		case EINVAL:
64770Sstevel@tonic-gate 		case ENOTSUP:
64780Sstevel@tonic-gate 			bad_error("libscf_lookup_instance", r);
64790Sstevel@tonic-gate 		}
64800Sstevel@tonic-gate 	}
64810Sstevel@tonic-gate 
64820Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size);
64830Sstevel@tonic-gate 	switch (r) {
64840Sstevel@tonic-gate 	case 0:
64850Sstevel@tonic-gate 		break;
64860Sstevel@tonic-gate 
64870Sstevel@tonic-gate 	case ECONNABORTED:
64880Sstevel@tonic-gate 		libscf_handle_rebind(h);
64890Sstevel@tonic-gate 		rebound = B_TRUE;
64900Sstevel@tonic-gate 		goto lookup_inst;
64910Sstevel@tonic-gate 
64920Sstevel@tonic-gate 	case EINVAL:
64930Sstevel@tonic-gate 		log_error(LOG_NOTICE,
64940Sstevel@tonic-gate 		    "%s/%s property of %s is misconfigured.\n", pg_name,
64950Sstevel@tonic-gate 		    SCF_PROPERTY_MILESTONE, SCF_SERVICE_STARTD);
64960Sstevel@tonic-gate 		/* FALLTHROUGH */
64970Sstevel@tonic-gate 
64980Sstevel@tonic-gate 	case ECANCELED:
64990Sstevel@tonic-gate 	case ENOENT:
65000Sstevel@tonic-gate 		(void) strcpy(fmri, "all");
65010Sstevel@tonic-gate 		break;
65020Sstevel@tonic-gate 
65030Sstevel@tonic-gate 	default:
65040Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
65050Sstevel@tonic-gate 	}
65060Sstevel@tonic-gate 
65070Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_FALSE);
65080Sstevel@tonic-gate 	switch (r) {
65090Sstevel@tonic-gate 	case 0:
65100Sstevel@tonic-gate 	case ECONNRESET:
65110Sstevel@tonic-gate 	case EALREADY:
65120Sstevel@tonic-gate 		break;
65130Sstevel@tonic-gate 
65140Sstevel@tonic-gate 	case EINVAL:
65150Sstevel@tonic-gate 		log_error(LOG_WARNING, "Milestone %s is invalid.\n", fmri);
65160Sstevel@tonic-gate 		break;
65170Sstevel@tonic-gate 
65180Sstevel@tonic-gate 	case ENOENT:
65190Sstevel@tonic-gate 		log_error(LOG_WARNING, "Milestone %s does not exist.\n", fmri);
65200Sstevel@tonic-gate 		break;
65210Sstevel@tonic-gate 
65220Sstevel@tonic-gate 	default:
65230Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
65240Sstevel@tonic-gate 	}
65250Sstevel@tonic-gate 
65260Sstevel@tonic-gate out:
65270Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
65280Sstevel@tonic-gate 	scf_value_destroy(val);
65290Sstevel@tonic-gate 	scf_property_destroy(prop);
65300Sstevel@tonic-gate 
65310Sstevel@tonic-gate 	return (rebound ? ECONNRESET : 0);
65320Sstevel@tonic-gate }
65330Sstevel@tonic-gate 
65346055Srm88369 /*
65356055Srm88369  * process_delete() deletes an instance from the dgraph if 'fmri' is an
65366055Srm88369  * instance fmri or if 'fmri' matches the 'general' property group of an
65376055Srm88369  * instance (or the 'general/enabled' property).
65386055Srm88369  *
65396055Srm88369  * 'fmri' may be overwritten and cannot be trusted on return by the caller.
65406055Srm88369  */
65410Sstevel@tonic-gate static void
65420Sstevel@tonic-gate process_delete(char *fmri, scf_handle_t *h)
65430Sstevel@tonic-gate {
65446055Srm88369 	char *lfmri, *end_inst_fmri;
65456055Srm88369 	const char *inst_name = NULL;
65466055Srm88369 	const char *pg_name = NULL;
65476055Srm88369 	const char *prop_name = NULL;
65480Sstevel@tonic-gate 
65490Sstevel@tonic-gate 	lfmri = safe_strdup(fmri);
65500Sstevel@tonic-gate 
65510Sstevel@tonic-gate 	/* Determine if the FMRI is a property group or instance */
65520Sstevel@tonic-gate 	if (scf_parse_svc_fmri(lfmri, NULL, NULL, &inst_name, &pg_name,
65536055Srm88369 	    &prop_name) != SCF_SUCCESS) {
65540Sstevel@tonic-gate 		log_error(LOG_WARNING,
65550Sstevel@tonic-gate 		    "Received invalid FMRI \"%s\" from repository server.\n",
65560Sstevel@tonic-gate 		    fmri);
65570Sstevel@tonic-gate 	} else if (inst_name != NULL && pg_name == NULL) {
65580Sstevel@tonic-gate 		(void) dgraph_remove_instance(fmri, h);
65596055Srm88369 	} else if (inst_name != NULL && pg_name != NULL) {
65606055Srm88369 		/*
65616055Srm88369 		 * If we're deleting the 'general' property group or
65626055Srm88369 		 * 'general/enabled' property then the whole instance
65636055Srm88369 		 * must be removed from the dgraph.
65646055Srm88369 		 */
65656055Srm88369 		if (strcmp(pg_name, SCF_PG_GENERAL) != 0) {
65666055Srm88369 			free(lfmri);
65676055Srm88369 			return;
65686055Srm88369 		}
65696055Srm88369 
65706055Srm88369 		if (prop_name != NULL &&
65716055Srm88369 		    strcmp(prop_name, SCF_PROPERTY_ENABLED) != 0) {
65726055Srm88369 			free(lfmri);
65736055Srm88369 			return;
65746055Srm88369 		}
65756055Srm88369 
65766055Srm88369 		/*
65776055Srm88369 		 * Because the instance has already been deleted from the
65786055Srm88369 		 * repository, we cannot use any scf_ functions to retrieve
65796055Srm88369 		 * the instance FMRI however we can easily reconstruct it
65806055Srm88369 		 * manually.
65816055Srm88369 		 */
65826055Srm88369 		end_inst_fmri = strstr(fmri, SCF_FMRI_PROPERTYGRP_PREFIX);
65836055Srm88369 		if (end_inst_fmri == NULL)
65846055Srm88369 			bad_error("process_delete", 0);
65856055Srm88369 
65866055Srm88369 		end_inst_fmri[0] = '\0';
65876055Srm88369 
65886055Srm88369 		(void) dgraph_remove_instance(fmri, h);
65890Sstevel@tonic-gate 	}
65900Sstevel@tonic-gate 
65910Sstevel@tonic-gate 	free(lfmri);
65920Sstevel@tonic-gate }
65930Sstevel@tonic-gate 
65940Sstevel@tonic-gate /*ARGSUSED*/
65950Sstevel@tonic-gate void *
65960Sstevel@tonic-gate repository_event_thread(void *unused)
65970Sstevel@tonic-gate {
65980Sstevel@tonic-gate 	scf_handle_t *h;
65990Sstevel@tonic-gate 	scf_propertygroup_t *pg;
66000Sstevel@tonic-gate 	scf_instance_t *inst;
66010Sstevel@tonic-gate 	char *fmri = startd_alloc(max_scf_fmri_size);
66020Sstevel@tonic-gate 	char *pg_name = startd_alloc(max_scf_value_size);
66030Sstevel@tonic-gate 	int r;
66040Sstevel@tonic-gate 
66050Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
66060Sstevel@tonic-gate 
66070Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
66080Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
66090Sstevel@tonic-gate 
66100Sstevel@tonic-gate retry:
66110Sstevel@tonic-gate 	if (_scf_notify_add_pgtype(h, SCF_GROUP_FRAMEWORK) != SCF_SUCCESS) {
66120Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN) {
66130Sstevel@tonic-gate 			libscf_handle_rebind(h);
66140Sstevel@tonic-gate 		} else {
66150Sstevel@tonic-gate 			log_error(LOG_WARNING,
66160Sstevel@tonic-gate 			    "Couldn't set up repository notification "
66170Sstevel@tonic-gate 			    "for property group type %s: %s\n",
66180Sstevel@tonic-gate 			    SCF_GROUP_FRAMEWORK, scf_strerror(scf_error()));
66190Sstevel@tonic-gate 
66200Sstevel@tonic-gate 			(void) sleep(1);
66210Sstevel@tonic-gate 		}
66220Sstevel@tonic-gate 
66230Sstevel@tonic-gate 		goto retry;
66240Sstevel@tonic-gate 	}
66250Sstevel@tonic-gate 
66260Sstevel@tonic-gate 	/*CONSTCOND*/
66270Sstevel@tonic-gate 	while (1) {
66280Sstevel@tonic-gate 		ssize_t res;
66290Sstevel@tonic-gate 
66300Sstevel@tonic-gate 		/* Note: fmri is only set on delete events. */
66310Sstevel@tonic-gate 		res = _scf_notify_wait(pg, fmri, max_scf_fmri_size);
66320Sstevel@tonic-gate 		if (res < 0) {
66330Sstevel@tonic-gate 			libscf_handle_rebind(h);
66340Sstevel@tonic-gate 			goto retry;
66350Sstevel@tonic-gate 		} else if (res == 0) {
66360Sstevel@tonic-gate 			/*
66370Sstevel@tonic-gate 			 * property group modified.  inst and pg_name are
66380Sstevel@tonic-gate 			 * pre-allocated scratch space.
66390Sstevel@tonic-gate 			 */
66400Sstevel@tonic-gate 			if (scf_pg_update(pg) < 0) {
66410Sstevel@tonic-gate 				switch (scf_error()) {
66420Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
66430Sstevel@tonic-gate 					continue;
66440Sstevel@tonic-gate 
66450Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
66460Sstevel@tonic-gate 					log_error(LOG_WARNING,
66470Sstevel@tonic-gate 					    "Lost repository event due to "
66480Sstevel@tonic-gate 					    "disconnection.\n");
66490Sstevel@tonic-gate 					libscf_handle_rebind(h);
66500Sstevel@tonic-gate 					goto retry;
66510Sstevel@tonic-gate 
66520Sstevel@tonic-gate 				case SCF_ERROR_NOT_BOUND:
66530Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
66540Sstevel@tonic-gate 				default:
66550Sstevel@tonic-gate 					bad_error("scf_pg_update", scf_error());
66560Sstevel@tonic-gate 				}
66570Sstevel@tonic-gate 			}
66580Sstevel@tonic-gate 
66590Sstevel@tonic-gate 			r = process_pg_event(h, pg, inst, pg_name);
66600Sstevel@tonic-gate 			switch (r) {
66610Sstevel@tonic-gate 			case 0:
66620Sstevel@tonic-gate 				break;
66630Sstevel@tonic-gate 
66640Sstevel@tonic-gate 			case ECONNABORTED:
66650Sstevel@tonic-gate 				log_error(LOG_WARNING, "Lost repository event "
66660Sstevel@tonic-gate 				    "due to disconnection.\n");
66670Sstevel@tonic-gate 				libscf_handle_rebind(h);
66680Sstevel@tonic-gate 				/* FALLTHROUGH */
66690Sstevel@tonic-gate 
66700Sstevel@tonic-gate 			case ECONNRESET:
66710Sstevel@tonic-gate 				goto retry;
66720Sstevel@tonic-gate 
66730Sstevel@tonic-gate 			default:
66740Sstevel@tonic-gate 				bad_error("process_pg_event", r);
66750Sstevel@tonic-gate 			}
66760Sstevel@tonic-gate 		} else {
66776055Srm88369 			/*
66786055Srm88369 			 * Service, instance, or pg deleted.
66796055Srm88369 			 * Don't trust fmri on return.
66806055Srm88369 			 */
66810Sstevel@tonic-gate 			process_delete(fmri, h);
66820Sstevel@tonic-gate 		}
66830Sstevel@tonic-gate 	}
66840Sstevel@tonic-gate 
66850Sstevel@tonic-gate 	/*NOTREACHED*/
66860Sstevel@tonic-gate 	return (NULL);
66870Sstevel@tonic-gate }
66880Sstevel@tonic-gate 
66890Sstevel@tonic-gate void
66900Sstevel@tonic-gate graph_engine_start()
66910Sstevel@tonic-gate {
66920Sstevel@tonic-gate 	int err;
66930Sstevel@tonic-gate 
66940Sstevel@tonic-gate 	(void) startd_thread_create(graph_thread, NULL);
66950Sstevel@tonic-gate 
66960Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
66970Sstevel@tonic-gate 	while (!initial_milestone_set) {
66980Sstevel@tonic-gate 		err = pthread_cond_wait(&initial_milestone_cv, &dgraph_lock);
66990Sstevel@tonic-gate 		assert(err == 0);
67000Sstevel@tonic-gate 	}
67010Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
67020Sstevel@tonic-gate 
67030Sstevel@tonic-gate 	(void) startd_thread_create(repository_event_thread, NULL);
67040Sstevel@tonic-gate 	(void) startd_thread_create(graph_event_thread, NULL);
67050Sstevel@tonic-gate }
6706