xref: /titanic_51/usr/src/cmd/svc/startd/graph.c (revision 53f3aea0943e36e5fed2615ad5f9fd1f17de51d2)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5965e507bSrm88369  * Common Development and Distribution License (the "License").
6965e507bSrm88369  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*53f3aea0SRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
23*53f3aea0SRoger A. Faulkner  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * graph.c - master restarter graph engine
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  *   The graph engine keeps a dependency graph of all service instances on the
317c478bd9Sstevel@tonic-gate  *   system, as recorded in the repository.  It decides when services should
327c478bd9Sstevel@tonic-gate  *   be brought up or down based on service states and dependencies and sends
337c478bd9Sstevel@tonic-gate  *   commands to restarters to effect any changes.  It also executes
347c478bd9Sstevel@tonic-gate  *   administrator commands sent by svcadm via the repository.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  *   The graph is stored in uu_list_t *dgraph and its vertices are
377c478bd9Sstevel@tonic-gate  *   graph_vertex_t's, each of which has a name and an integer id unique to
387c478bd9Sstevel@tonic-gate  *   its name (see dict.c).  A vertex's type attribute designates the type
397c478bd9Sstevel@tonic-gate  *   of object it represents: GVT_INST for service instances, GVT_SVC for
407c478bd9Sstevel@tonic-gate  *   service objects (since service instances may depend on another service,
417c478bd9Sstevel@tonic-gate  *   rather than service instance), GVT_FILE for files (which services may
427c478bd9Sstevel@tonic-gate  *   depend on), and GVT_GROUP for dependencies on multiple objects.  GVT_GROUP
437c478bd9Sstevel@tonic-gate  *   vertices are necessary because dependency lists may have particular
447c478bd9Sstevel@tonic-gate  *   grouping types (require any, require all, optional, or exclude) and
457c478bd9Sstevel@tonic-gate  *   event-propagation characteristics.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  *   The initial graph is built by libscf_populate_graph() invoking
487c478bd9Sstevel@tonic-gate  *   dgraph_add_instance() for each instance in the repository.  The function
497c478bd9Sstevel@tonic-gate  *   adds a GVT_SVC vertex for the service if one does not already exist, adds
507c478bd9Sstevel@tonic-gate  *   a GVT_INST vertex named by the FMRI of the instance, and sets up the edges.
517c478bd9Sstevel@tonic-gate  *   The resulting web of vertices & edges associated with an instance's vertex
527c478bd9Sstevel@tonic-gate  *   includes
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  *     - an edge from the GVT_SVC vertex for the instance's service
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  *     - an edge to the GVT_INST vertex of the instance's resarter, if its
577c478bd9Sstevel@tonic-gate  *       restarter is not svc.startd
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  *     - edges from other GVT_INST vertices if the instance is a restarter
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  *     - for each dependency property group in the instance's "running"
627c478bd9Sstevel@tonic-gate  *       snapshot, an edge to a GVT_GROUP vertex named by the FMRI of the
637c478bd9Sstevel@tonic-gate  *       instance and the name of the property group
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  *     - for each value of the "entities" property in each dependency property
667c478bd9Sstevel@tonic-gate  *       group, an edge from the corresponding GVT_GROUP vertex to a
677c478bd9Sstevel@tonic-gate  *       GVT_INST, GVT_SVC, or GVT_FILE vertex
687c478bd9Sstevel@tonic-gate  *
697c478bd9Sstevel@tonic-gate  *     - edges from GVT_GROUP vertices for each dependent instance
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  *   After the edges are set up the vertex's GV_CONFIGURED flag is set.  If
727c478bd9Sstevel@tonic-gate  *   there are problems, or if a service is mentioned in a dependency but does
737c478bd9Sstevel@tonic-gate  *   not exist in the repository, the GV_CONFIGURED flag will be clear.
747c478bd9Sstevel@tonic-gate  *
757c478bd9Sstevel@tonic-gate  *   The graph and all of its vertices are protected by the dgraph_lock mutex.
767c478bd9Sstevel@tonic-gate  *   See restarter.c for more information.
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  *   The properties of an instance fall into two classes: immediate and
797c478bd9Sstevel@tonic-gate  *   snapshotted.  Immediate properties should have an immediate effect when
807c478bd9Sstevel@tonic-gate  *   changed.  Snapshotted properties should be read from a snapshot, so they
817c478bd9Sstevel@tonic-gate  *   only change when the snapshot changes.  The immediate properties used by
827c478bd9Sstevel@tonic-gate  *   the graph engine are general/enabled, general/restarter, and the properties
837c478bd9Sstevel@tonic-gate  *   in the restarter_actions property group.  Since they are immediate, they
847c478bd9Sstevel@tonic-gate  *   are not read out of a snapshot.  The snapshotted properties used by the
857c478bd9Sstevel@tonic-gate  *   graph engine are those in the property groups with type "dependency" and
867c478bd9Sstevel@tonic-gate  *   are read out of the "running" snapshot.  The "running" snapshot is created
877c478bd9Sstevel@tonic-gate  *   by the the graph engine as soon as possible, and it is updated, along with
887c478bd9Sstevel@tonic-gate  *   in-core copies of the data (dependency information for the graph engine) on
897c478bd9Sstevel@tonic-gate  *   receipt of the refresh command from svcadm.  In addition, the graph engine
907c478bd9Sstevel@tonic-gate  *   updates the "start" snapshot from the "running" snapshot whenever a service
917c478bd9Sstevel@tonic-gate  *   comes online.
92aca380d7SRenaud Manus  *
93aca380d7SRenaud Manus  *   When a DISABLE event is requested by the administrator, svc.startd shutdown
94aca380d7SRenaud Manus  *   the dependents first before shutting down the requested service.
95aca380d7SRenaud Manus  *   In graph_enable_by_vertex, we create a subtree that contains the dependent
96aca380d7SRenaud Manus  *   vertices by marking those vertices with the GV_TOOFFLINE flag. And we mark
97aca380d7SRenaud Manus  *   the vertex to disable with the GV_TODISABLE flag. Once the tree is created,
98aca380d7SRenaud Manus  *   we send the _ADMIN_DISABLE event to the leaves. The leaves will then
99aca380d7SRenaud Manus  *   transition from STATE_ONLINE/STATE_DEGRADED to STATE_OFFLINE/STATE_MAINT.
100aca380d7SRenaud Manus  *   In gt_enter_offline and gt_enter_maint if the vertex was in a subtree then
101aca380d7SRenaud Manus  *   we clear the GV_TOOFFLINE flag and walk the dependencies to offline the new
102aca380d7SRenaud Manus  *   exposed leaves. We do the same until we reach the last leaf (the one with
103aca380d7SRenaud Manus  *   the GV_TODISABLE flag). If the vertex to disable is also part of a larger
104aca380d7SRenaud Manus  *   subtree (eg. multiple DISABLE events on vertices in the same subtree) then
105aca380d7SRenaud Manus  *   once the first vertex is disabled (GV_TODISABLE flag is removed), we
106aca380d7SRenaud Manus  *   continue to propagate the offline event to the vertex's dependencies.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
1107c478bd9Sstevel@tonic-gate #include <sys/wait.h>
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate #include <assert.h>
1137c478bd9Sstevel@tonic-gate #include <errno.h>
1147c478bd9Sstevel@tonic-gate #include <fcntl.h>
1157c478bd9Sstevel@tonic-gate #include <libscf.h>
1167c478bd9Sstevel@tonic-gate #include <libscf_priv.h>
1177c478bd9Sstevel@tonic-gate #include <libuutil.h>
1187c478bd9Sstevel@tonic-gate #include <locale.h>
1197c478bd9Sstevel@tonic-gate #include <poll.h>
1207c478bd9Sstevel@tonic-gate #include <pthread.h>
1217c478bd9Sstevel@tonic-gate #include <signal.h>
1227c478bd9Sstevel@tonic-gate #include <stddef.h>
1237c478bd9Sstevel@tonic-gate #include <stdio.h>
1247c478bd9Sstevel@tonic-gate #include <stdlib.h>
1257c478bd9Sstevel@tonic-gate #include <string.h>
1267c478bd9Sstevel@tonic-gate #include <strings.h>
1277c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
1287c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
1297c478bd9Sstevel@tonic-gate #include <zone.h>
130753a6d45SSherry Moore #if defined(__i386)
131753a6d45SSherry Moore #include <libgrubmgmt.h>
132753a6d45SSherry Moore #endif	/* __i386 */
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate #include "startd.h"
1357c478bd9Sstevel@tonic-gate #include "protocol.h"
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate #define	MILESTONE_NONE	((graph_vertex_t *)1)
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #define	CONSOLE_LOGIN_FMRI	"svc:/system/console-login:default"
1417c478bd9Sstevel@tonic-gate #define	FS_MINIMAL_FMRI		"svc:/system/filesystem/minimal:default"
1427c478bd9Sstevel@tonic-gate 
1433ad28c1eSrm88369 #define	VERTEX_REMOVED	0	/* vertex has been freed  */
1443ad28c1eSrm88369 #define	VERTEX_INUSE	1	/* vertex is still in use */
1453ad28c1eSrm88369 
14656e23938Sbustos /*
14756e23938Sbustos  * Services in these states are not considered 'down' by the
14856e23938Sbustos  * milestone/shutdown code.
14956e23938Sbustos  */
15056e23938Sbustos #define	up_state(state)	((state) == RESTARTER_STATE_ONLINE || \
15156e23938Sbustos 	(state) == RESTARTER_STATE_DEGRADED || \
15256e23938Sbustos 	(state) == RESTARTER_STATE_OFFLINE)
15356e23938Sbustos 
1547c478bd9Sstevel@tonic-gate static uu_list_pool_t *graph_edge_pool, *graph_vertex_pool;
1557c478bd9Sstevel@tonic-gate static uu_list_t *dgraph;
1567c478bd9Sstevel@tonic-gate static pthread_mutex_t dgraph_lock;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate  * milestone indicates the current subgraph.  When NULL, it is the entire
1607c478bd9Sstevel@tonic-gate  * graph.  When MILESTONE_NONE, it is the empty graph.  Otherwise, it is all
1617c478bd9Sstevel@tonic-gate  * services on which the target vertex depends.
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate static graph_vertex_t *milestone = NULL;
1647c478bd9Sstevel@tonic-gate static boolean_t initial_milestone_set = B_FALSE;
1657c478bd9Sstevel@tonic-gate static pthread_cond_t initial_milestone_cv = PTHREAD_COND_INITIALIZER;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /* protected by dgraph_lock */
1687c478bd9Sstevel@tonic-gate static boolean_t sulogin_thread_running = B_FALSE;
1697c478bd9Sstevel@tonic-gate static boolean_t sulogin_running = B_FALSE;
1707c478bd9Sstevel@tonic-gate static boolean_t console_login_ready = B_FALSE;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /* Number of services to come down to complete milestone transition. */
1737c478bd9Sstevel@tonic-gate static uint_t non_subgraph_svcs;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * These variables indicate what should be done when we reach the milestone
1777c478bd9Sstevel@tonic-gate  * target milestone, i.e., when non_subgraph_svcs == 0.  They are acted upon in
1787c478bd9Sstevel@tonic-gate  * dgraph_set_instance_state().
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate static int halting = -1;
1817c478bd9Sstevel@tonic-gate static boolean_t go_single_user_mode = B_FALSE;
1827c478bd9Sstevel@tonic-gate static boolean_t go_to_level1 = B_FALSE;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1854d53c7adSDan Price  * Tracks when we started halting.
1864d53c7adSDan Price  */
1874d53c7adSDan Price static time_t halting_time = 0;
1884d53c7adSDan Price 
1894d53c7adSDan Price /*
1907c478bd9Sstevel@tonic-gate  * This tracks the legacy runlevel to ensure we signal init and manage
1917c478bd9Sstevel@tonic-gate  * utmpx entries correctly.
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate static char current_runlevel = '\0';
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /* Number of single user threads currently running */
1967c478bd9Sstevel@tonic-gate static pthread_mutex_t single_user_thread_lock;
1977c478bd9Sstevel@tonic-gate static int single_user_thread_count = 0;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /* Statistics for dependency cycle-checking */
2007c478bd9Sstevel@tonic-gate static u_longlong_t dep_inserts = 0;
2017c478bd9Sstevel@tonic-gate static u_longlong_t dep_cycle_ns = 0;
2027c478bd9Sstevel@tonic-gate static u_longlong_t dep_insert_ns = 0;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate static const char * const emsg_invalid_restarter =
20699b44c3bSlianep 	"Transitioning %s to maintenance, restarter FMRI %s is invalid "
20799b44c3bSlianep 	"(see 'svcs -xv' for details).\n";
2087c478bd9Sstevel@tonic-gate static const char * const console_login_fmri = CONSOLE_LOGIN_FMRI;
2097c478bd9Sstevel@tonic-gate static const char * const single_user_fmri = SCF_MILESTONE_SINGLE_USER;
2107c478bd9Sstevel@tonic-gate static const char * const multi_user_fmri = SCF_MILESTONE_MULTI_USER;
2117c478bd9Sstevel@tonic-gate static const char * const multi_user_svr_fmri = SCF_MILESTONE_MULTI_USER_SERVER;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate  * These services define the system being "up".  If none of them can come
2167c478bd9Sstevel@tonic-gate  * online, then we will run sulogin on the console.  Note that the install ones
2177c478bd9Sstevel@tonic-gate  * are for the miniroot and when installing CDs after the first.  can_come_up()
2187c478bd9Sstevel@tonic-gate  * does the decision making, and an sulogin_thread() runs sulogin, which can be
2197c478bd9Sstevel@tonic-gate  * started by dgraph_set_instance_state() or single_user_thread().
2207c478bd9Sstevel@tonic-gate  *
2217c478bd9Sstevel@tonic-gate  * NOTE: can_come_up() relies on SCF_MILESTONE_SINGLE_USER being the first
2227c478bd9Sstevel@tonic-gate  * entry, which is only used when booting_to_single_user (boot -s) is set.
2237c478bd9Sstevel@tonic-gate  * This is because when doing a "boot -s", sulogin is started from specials.c
2247c478bd9Sstevel@tonic-gate  * after milestone/single-user comes online, for backwards compatibility.
2257c478bd9Sstevel@tonic-gate  * In this case, SCF_MILESTONE_SINGLE_USER needs to be part of up_svcs
2267c478bd9Sstevel@tonic-gate  * to ensure sulogin will be spawned if milestone/single-user cannot be reached.
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate static const char * const up_svcs[] = {
2297c478bd9Sstevel@tonic-gate 	SCF_MILESTONE_SINGLE_USER,
2307c478bd9Sstevel@tonic-gate 	CONSOLE_LOGIN_FMRI,
2317c478bd9Sstevel@tonic-gate 	"svc:/system/install-setup:default",
2327c478bd9Sstevel@tonic-gate 	"svc:/system/install:default",
2337c478bd9Sstevel@tonic-gate 	NULL
2347c478bd9Sstevel@tonic-gate };
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /* This array must have an element for each non-NULL element of up_svcs[]. */
2377c478bd9Sstevel@tonic-gate static graph_vertex_t *up_svcs_p[] = { NULL, NULL, NULL, NULL };
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /* These are for seed repository magic.  See can_come_up(). */
2407c478bd9Sstevel@tonic-gate static const char * const manifest_import =
2417c478bd9Sstevel@tonic-gate 	"svc:/system/manifest-import:default";
2427c478bd9Sstevel@tonic-gate static graph_vertex_t *manifest_import_p = NULL;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate static char target_milestone_as_runlevel(void);
2467c478bd9Sstevel@tonic-gate static void graph_runlevel_changed(char rl, int online);
2477c478bd9Sstevel@tonic-gate static int dgraph_set_milestone(const char *, scf_handle_t *, boolean_t);
2487c478bd9Sstevel@tonic-gate static boolean_t should_be_in_subgraph(graph_vertex_t *v);
249aca380d7SRenaud Manus static int mark_subtree(graph_edge_t *, void *);
250aca380d7SRenaud Manus static boolean_t insubtree_dependents_down(graph_vertex_t *);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate  * graph_vertex_compare()
2547c478bd9Sstevel@tonic-gate  *	This function can compare either int *id or * graph_vertex_t *gv
2557c478bd9Sstevel@tonic-gate  *	values, as the vertex id is always the first element of a
2567c478bd9Sstevel@tonic-gate  *	graph_vertex structure.
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate /* ARGSUSED */
2597c478bd9Sstevel@tonic-gate static int
2607c478bd9Sstevel@tonic-gate graph_vertex_compare(const void *lc_arg, const void *rc_arg, void *private)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	int lc_id = ((const graph_vertex_t *)lc_arg)->gv_id;
2637c478bd9Sstevel@tonic-gate 	int rc_id = *(int *)rc_arg;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (lc_id > rc_id)
2667c478bd9Sstevel@tonic-gate 		return (1);
2677c478bd9Sstevel@tonic-gate 	if (lc_id < rc_id)
2687c478bd9Sstevel@tonic-gate 		return (-1);
2697c478bd9Sstevel@tonic-gate 	return (0);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate void
2737c478bd9Sstevel@tonic-gate graph_init()
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	graph_edge_pool = startd_list_pool_create("graph_edges",
2767c478bd9Sstevel@tonic-gate 	    sizeof (graph_edge_t), offsetof(graph_edge_t, ge_link), NULL,
2777c478bd9Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG);
2787c478bd9Sstevel@tonic-gate 	assert(graph_edge_pool != NULL);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	graph_vertex_pool = startd_list_pool_create("graph_vertices",
2817c478bd9Sstevel@tonic-gate 	    sizeof (graph_vertex_t), offsetof(graph_vertex_t, gv_link),
2827c478bd9Sstevel@tonic-gate 	    graph_vertex_compare, UU_LIST_POOL_DEBUG);
2837c478bd9Sstevel@tonic-gate 	assert(graph_vertex_pool != NULL);
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&dgraph_lock, &mutex_attrs);
2867c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&single_user_thread_lock, &mutex_attrs);
2877c478bd9Sstevel@tonic-gate 	dgraph = startd_list_create(graph_vertex_pool, NULL, UU_LIST_SORTED);
2887c478bd9Sstevel@tonic-gate 	assert(dgraph != NULL);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if (!st->st_initial)
2917c478bd9Sstevel@tonic-gate 		current_runlevel = utmpx_get_runlevel();
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Initialized graph\n");
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate static graph_vertex_t *
2977c478bd9Sstevel@tonic-gate vertex_get_by_name(const char *name)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	int id;
3007c478bd9Sstevel@tonic-gate 
301*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	id = dict_lookup_byname(name);
3047c478bd9Sstevel@tonic-gate 	if (id == -1)
3057c478bd9Sstevel@tonic-gate 		return (NULL);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	return (uu_list_find(dgraph, &id, NULL, NULL));
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate static graph_vertex_t *
3117c478bd9Sstevel@tonic-gate vertex_get_by_id(int id)
3127c478bd9Sstevel@tonic-gate {
313*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if (id == -1)
3167c478bd9Sstevel@tonic-gate 		return (NULL);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	return (uu_list_find(dgraph, &id, NULL, NULL));
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate /*
3227c478bd9Sstevel@tonic-gate  * Creates a new vertex with the given name, adds it to the graph, and returns
3237c478bd9Sstevel@tonic-gate  * a pointer to it.  The graph lock must be held by this thread on entry.
3247c478bd9Sstevel@tonic-gate  */
3257c478bd9Sstevel@tonic-gate static graph_vertex_t *
3267c478bd9Sstevel@tonic-gate graph_add_vertex(const char *name)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 	int id;
3297c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
3307c478bd9Sstevel@tonic-gate 	void *p;
3317c478bd9Sstevel@tonic-gate 	uu_list_index_t idx;
3327c478bd9Sstevel@tonic-gate 
333*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	id = dict_insert(name);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	v = startd_zalloc(sizeof (*v));
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	v->gv_id = id;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	v->gv_name = startd_alloc(strlen(name) + 1);
3427c478bd9Sstevel@tonic-gate 	(void) strcpy(v->gv_name, name);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	v->gv_dependencies = startd_list_create(graph_edge_pool, v, 0);
3457c478bd9Sstevel@tonic-gate 	v->gv_dependents = startd_list_create(graph_edge_pool, v, 0);
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	p = uu_list_find(dgraph, &id, NULL, &idx);
3487c478bd9Sstevel@tonic-gate 	assert(p == NULL);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	uu_list_node_init(v, &v->gv_link, graph_vertex_pool);
3517c478bd9Sstevel@tonic-gate 	uu_list_insert(dgraph, v, idx);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	return (v);
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate /*
3577c478bd9Sstevel@tonic-gate  * Removes v from the graph and frees it.  The graph should be locked by this
3587c478bd9Sstevel@tonic-gate  * thread, and v should have no edges associated with it.
3597c478bd9Sstevel@tonic-gate  */
3607c478bd9Sstevel@tonic-gate static void
3617c478bd9Sstevel@tonic-gate graph_remove_vertex(graph_vertex_t *v)
3627c478bd9Sstevel@tonic-gate {
363*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependencies) == 0);
3667c478bd9Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependents) == 0);
3673ad28c1eSrm88369 	assert(v->gv_refs == 0);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	startd_free(v->gv_name, strlen(v->gv_name) + 1);
3707c478bd9Sstevel@tonic-gate 	uu_list_destroy(v->gv_dependencies);
3717c478bd9Sstevel@tonic-gate 	uu_list_destroy(v->gv_dependents);
3727c478bd9Sstevel@tonic-gate 	uu_list_remove(dgraph, v);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	startd_free(v, sizeof (graph_vertex_t));
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate static void
3787c478bd9Sstevel@tonic-gate graph_add_edge(graph_vertex_t *fv, graph_vertex_t *tv)
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate 	graph_edge_t *e, *re;
3817c478bd9Sstevel@tonic-gate 	int r;
3827c478bd9Sstevel@tonic-gate 
383*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	e = startd_alloc(sizeof (graph_edge_t));
3867c478bd9Sstevel@tonic-gate 	re = startd_alloc(sizeof (graph_edge_t));
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	e->ge_parent = fv;
3897c478bd9Sstevel@tonic-gate 	e->ge_vertex = tv;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	re->ge_parent = tv;
3927c478bd9Sstevel@tonic-gate 	re->ge_vertex = fv;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	uu_list_node_init(e, &e->ge_link, graph_edge_pool);
3957c478bd9Sstevel@tonic-gate 	r = uu_list_insert_before(fv->gv_dependencies, NULL, e);
3967c478bd9Sstevel@tonic-gate 	assert(r == 0);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	uu_list_node_init(re, &re->ge_link, graph_edge_pool);
3997c478bd9Sstevel@tonic-gate 	r = uu_list_insert_before(tv->gv_dependents, NULL, re);
4007c478bd9Sstevel@tonic-gate 	assert(r == 0);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate static void
4047c478bd9Sstevel@tonic-gate graph_remove_edge(graph_vertex_t *v, graph_vertex_t *dv)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
4097c478bd9Sstevel@tonic-gate 	    e != NULL;
4107c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
4117c478bd9Sstevel@tonic-gate 		if (e->ge_vertex == dv) {
4127c478bd9Sstevel@tonic-gate 			uu_list_remove(v->gv_dependencies, e);
4137c478bd9Sstevel@tonic-gate 			startd_free(e, sizeof (graph_edge_t));
4147c478bd9Sstevel@tonic-gate 			break;
4157c478bd9Sstevel@tonic-gate 		}
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(dv->gv_dependents);
4197c478bd9Sstevel@tonic-gate 	    e != NULL;
4207c478bd9Sstevel@tonic-gate 	    e = uu_list_next(dv->gv_dependents, e)) {
4217c478bd9Sstevel@tonic-gate 		if (e->ge_vertex == v) {
4227c478bd9Sstevel@tonic-gate 			uu_list_remove(dv->gv_dependents, e);
4237c478bd9Sstevel@tonic-gate 			startd_free(e, sizeof (graph_edge_t));
4247c478bd9Sstevel@tonic-gate 			break;
4257c478bd9Sstevel@tonic-gate 		}
4267c478bd9Sstevel@tonic-gate 	}
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate static void
4303ad28c1eSrm88369 remove_inst_vertex(graph_vertex_t *v)
4313ad28c1eSrm88369 {
4323ad28c1eSrm88369 	graph_edge_t *e;
4333ad28c1eSrm88369 	graph_vertex_t *sv;
4343ad28c1eSrm88369 	int i;
4353ad28c1eSrm88369 
436*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
4373ad28c1eSrm88369 	assert(uu_list_numnodes(v->gv_dependents) == 1);
4383ad28c1eSrm88369 	assert(uu_list_numnodes(v->gv_dependencies) == 0);
4393ad28c1eSrm88369 	assert(v->gv_refs == 0);
4403ad28c1eSrm88369 	assert((v->gv_flags & GV_CONFIGURED) == 0);
4413ad28c1eSrm88369 
4423ad28c1eSrm88369 	e = uu_list_first(v->gv_dependents);
4433ad28c1eSrm88369 	sv = e->ge_vertex;
4443ad28c1eSrm88369 	graph_remove_edge(sv, v);
4453ad28c1eSrm88369 
4463ad28c1eSrm88369 	for (i = 0; up_svcs[i] != NULL; ++i) {
4473ad28c1eSrm88369 		if (up_svcs_p[i] == v)
4483ad28c1eSrm88369 			up_svcs_p[i] = NULL;
4493ad28c1eSrm88369 	}
4503ad28c1eSrm88369 
4513ad28c1eSrm88369 	if (manifest_import_p == v)
4523ad28c1eSrm88369 		manifest_import_p = NULL;
4533ad28c1eSrm88369 
4543ad28c1eSrm88369 	graph_remove_vertex(v);
4553ad28c1eSrm88369 
4563ad28c1eSrm88369 	if (uu_list_numnodes(sv->gv_dependencies) == 0 &&
4573ad28c1eSrm88369 	    uu_list_numnodes(sv->gv_dependents) == 0 &&
4583ad28c1eSrm88369 	    sv->gv_refs == 0)
4593ad28c1eSrm88369 		graph_remove_vertex(sv);
4603ad28c1eSrm88369 }
4613ad28c1eSrm88369 
4623ad28c1eSrm88369 static void
4637c478bd9Sstevel@tonic-gate graph_walk_dependents(graph_vertex_t *v, void (*func)(graph_vertex_t *, void *),
4647c478bd9Sstevel@tonic-gate     void *arg)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
4697c478bd9Sstevel@tonic-gate 	    e != NULL;
4707c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e))
4717c478bd9Sstevel@tonic-gate 		func(e->ge_vertex, arg);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate static void
4757c478bd9Sstevel@tonic-gate graph_walk_dependencies(graph_vertex_t *v, void (*func)(graph_vertex_t *,
4767c478bd9Sstevel@tonic-gate 	void *), void *arg)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
4797c478bd9Sstevel@tonic-gate 
480*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
4837c478bd9Sstevel@tonic-gate 	    e != NULL;
4847c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 		func(e->ge_vertex, arg);
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate /*
4917c478bd9Sstevel@tonic-gate  * Generic graph walking function.
4927c478bd9Sstevel@tonic-gate  *
4937c478bd9Sstevel@tonic-gate  * Given a vertex, this function will walk either dependencies
4947c478bd9Sstevel@tonic-gate  * (WALK_DEPENDENCIES) or dependents (WALK_DEPENDENTS) of a vertex recursively
4957c478bd9Sstevel@tonic-gate  * for the entire graph.  It will avoid cycles and never visit the same vertex
4967c478bd9Sstevel@tonic-gate  * twice.
4977c478bd9Sstevel@tonic-gate  *
4987c478bd9Sstevel@tonic-gate  * We avoid traversing exclusion dependencies, because they are allowed to
4997c478bd9Sstevel@tonic-gate  * create cycles in the graph.  When propagating satisfiability, there is no
5007c478bd9Sstevel@tonic-gate  * need to walk exclusion dependencies because exclude_all_satisfied() doesn't
5017c478bd9Sstevel@tonic-gate  * test for satisfiability.
5027c478bd9Sstevel@tonic-gate  *
5037c478bd9Sstevel@tonic-gate  * The walker takes two callbacks.  The first is called before examining the
5047c478bd9Sstevel@tonic-gate  * dependents of each vertex.  The second is called on each vertex after
5057c478bd9Sstevel@tonic-gate  * examining its dependents.  This allows is_path_to() to construct a path only
5067c478bd9Sstevel@tonic-gate  * after the target vertex has been found.
5077c478bd9Sstevel@tonic-gate  */
5087c478bd9Sstevel@tonic-gate typedef enum {
5097c478bd9Sstevel@tonic-gate 	WALK_DEPENDENTS,
5107c478bd9Sstevel@tonic-gate 	WALK_DEPENDENCIES
5117c478bd9Sstevel@tonic-gate } graph_walk_dir_t;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate typedef int (*graph_walk_cb_t)(graph_vertex_t *, void *);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate typedef struct graph_walk_info {
5167c478bd9Sstevel@tonic-gate 	graph_walk_dir_t 	gi_dir;
5177c478bd9Sstevel@tonic-gate 	uchar_t			*gi_visited;	/* vertex bitmap */
5187c478bd9Sstevel@tonic-gate 	int			(*gi_pre)(graph_vertex_t *, void *);
5197c478bd9Sstevel@tonic-gate 	void			(*gi_post)(graph_vertex_t *, void *);
5207c478bd9Sstevel@tonic-gate 	void			*gi_arg;	/* callback arg */
5217c478bd9Sstevel@tonic-gate 	int			gi_ret;		/* return value */
5227c478bd9Sstevel@tonic-gate } graph_walk_info_t;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate static int
5257c478bd9Sstevel@tonic-gate graph_walk_recurse(graph_edge_t *e, graph_walk_info_t *gip)
5267c478bd9Sstevel@tonic-gate {
5277c478bd9Sstevel@tonic-gate 	uu_list_t *list;
5287c478bd9Sstevel@tonic-gate 	int r;
5297c478bd9Sstevel@tonic-gate 	graph_vertex_t *v = e->ge_vertex;
5307c478bd9Sstevel@tonic-gate 	int i;
5317c478bd9Sstevel@tonic-gate 	uint_t b;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	i = v->gv_id / 8;
5347c478bd9Sstevel@tonic-gate 	b = 1 << (v->gv_id % 8);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	/*
5377c478bd9Sstevel@tonic-gate 	 * Check to see if we've visited this vertex already.
5387c478bd9Sstevel@tonic-gate 	 */
5397c478bd9Sstevel@tonic-gate 	if (gip->gi_visited[i] & b)
5407c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	gip->gi_visited[i] |= b;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	/*
5457c478bd9Sstevel@tonic-gate 	 * Don't follow exclusions.
5467c478bd9Sstevel@tonic-gate 	 */
5477c478bd9Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
5487c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	/*
5517c478bd9Sstevel@tonic-gate 	 * Call pre-visit callback.  If this doesn't terminate the walk,
5527c478bd9Sstevel@tonic-gate 	 * continue search.
5537c478bd9Sstevel@tonic-gate 	 */
5547c478bd9Sstevel@tonic-gate 	if ((gip->gi_ret = gip->gi_pre(v, gip->gi_arg)) == UU_WALK_NEXT) {
5557c478bd9Sstevel@tonic-gate 		/*
5567c478bd9Sstevel@tonic-gate 		 * Recurse using appropriate list.
5577c478bd9Sstevel@tonic-gate 		 */
5587c478bd9Sstevel@tonic-gate 		if (gip->gi_dir == WALK_DEPENDENTS)
5597c478bd9Sstevel@tonic-gate 			list = v->gv_dependents;
5607c478bd9Sstevel@tonic-gate 		else
5617c478bd9Sstevel@tonic-gate 			list = v->gv_dependencies;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 		r = uu_list_walk(list, (uu_walk_fn_t *)graph_walk_recurse,
5647c478bd9Sstevel@tonic-gate 		    gip, 0);
5657c478bd9Sstevel@tonic-gate 		assert(r == 0);
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	/*
5697c478bd9Sstevel@tonic-gate 	 * Callbacks must return either UU_WALK_NEXT or UU_WALK_DONE.
5707c478bd9Sstevel@tonic-gate 	 */
5717c478bd9Sstevel@tonic-gate 	assert(gip->gi_ret == UU_WALK_NEXT || gip->gi_ret == UU_WALK_DONE);
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	/*
5747c478bd9Sstevel@tonic-gate 	 * If given a post-callback, call the function for every vertex.
5757c478bd9Sstevel@tonic-gate 	 */
5767c478bd9Sstevel@tonic-gate 	if (gip->gi_post != NULL)
5777c478bd9Sstevel@tonic-gate 		(void) gip->gi_post(v, gip->gi_arg);
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	/*
5807c478bd9Sstevel@tonic-gate 	 * Preserve the callback's return value.  If the callback returns
5817c478bd9Sstevel@tonic-gate 	 * UU_WALK_DONE, then we propagate that to the caller in order to
5827c478bd9Sstevel@tonic-gate 	 * terminate the walk.
5837c478bd9Sstevel@tonic-gate 	 */
5847c478bd9Sstevel@tonic-gate 	return (gip->gi_ret);
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate static void
5887c478bd9Sstevel@tonic-gate graph_walk(graph_vertex_t *v, graph_walk_dir_t dir,
5897c478bd9Sstevel@tonic-gate     int (*pre)(graph_vertex_t *, void *),
5907c478bd9Sstevel@tonic-gate     void (*post)(graph_vertex_t *, void *), void *arg)
5917c478bd9Sstevel@tonic-gate {
5927c478bd9Sstevel@tonic-gate 	graph_walk_info_t gi;
5937c478bd9Sstevel@tonic-gate 	graph_edge_t fake;
5947c478bd9Sstevel@tonic-gate 	size_t sz = dictionary->dict_new_id / 8 + 1;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	gi.gi_visited = startd_zalloc(sz);
5977c478bd9Sstevel@tonic-gate 	gi.gi_pre = pre;
5987c478bd9Sstevel@tonic-gate 	gi.gi_post = post;
5997c478bd9Sstevel@tonic-gate 	gi.gi_arg = arg;
6007c478bd9Sstevel@tonic-gate 	gi.gi_dir = dir;
6017c478bd9Sstevel@tonic-gate 	gi.gi_ret = 0;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	/*
6047c478bd9Sstevel@tonic-gate 	 * Fake up an edge for the first iteration
6057c478bd9Sstevel@tonic-gate 	 */
6067c478bd9Sstevel@tonic-gate 	fake.ge_vertex = v;
6077c478bd9Sstevel@tonic-gate 	(void) graph_walk_recurse(&fake, &gi);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	startd_free(gi.gi_visited, sz);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate typedef struct child_search {
6137c478bd9Sstevel@tonic-gate 	int	id;		/* id of vertex to look for */
6147c478bd9Sstevel@tonic-gate 	uint_t	depth;		/* recursion depth */
6157c478bd9Sstevel@tonic-gate 	/*
6167c478bd9Sstevel@tonic-gate 	 * While the vertex is not found, path is NULL.  After the search, if
6177c478bd9Sstevel@tonic-gate 	 * the vertex was found then path should point to a -1-terminated
6187c478bd9Sstevel@tonic-gate 	 * array of vertex id's which constitute the path to the vertex.
6197c478bd9Sstevel@tonic-gate 	 */
6207c478bd9Sstevel@tonic-gate 	int	*path;
6217c478bd9Sstevel@tonic-gate } child_search_t;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate static int
6247c478bd9Sstevel@tonic-gate child_pre(graph_vertex_t *v, void *arg)
6257c478bd9Sstevel@tonic-gate {
6267c478bd9Sstevel@tonic-gate 	child_search_t *cs = arg;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	cs->depth++;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	if (v->gv_id == cs->id) {
6317c478bd9Sstevel@tonic-gate 		cs->path = startd_alloc((cs->depth + 1) * sizeof (int));
6327c478bd9Sstevel@tonic-gate 		cs->path[cs->depth] = -1;
6337c478bd9Sstevel@tonic-gate 		return (UU_WALK_DONE);
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate static void
6407c478bd9Sstevel@tonic-gate child_post(graph_vertex_t *v, void *arg)
6417c478bd9Sstevel@tonic-gate {
6427c478bd9Sstevel@tonic-gate 	child_search_t *cs = arg;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	cs->depth--;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	if (cs->path != NULL)
6477c478bd9Sstevel@tonic-gate 		cs->path[cs->depth] = v->gv_id;
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate /*
6517c478bd9Sstevel@tonic-gate  * Look for a path from from to to.  If one exists, returns a pointer to
6527c478bd9Sstevel@tonic-gate  * a NULL-terminated array of pointers to the vertices along the path.  If
6537c478bd9Sstevel@tonic-gate  * there is no path, returns NULL.
6547c478bd9Sstevel@tonic-gate  */
6557c478bd9Sstevel@tonic-gate static int *
6567c478bd9Sstevel@tonic-gate is_path_to(graph_vertex_t *from, graph_vertex_t *to)
6577c478bd9Sstevel@tonic-gate {
6587c478bd9Sstevel@tonic-gate 	child_search_t cs;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	cs.id = to->gv_id;
6617c478bd9Sstevel@tonic-gate 	cs.depth = 0;
6627c478bd9Sstevel@tonic-gate 	cs.path = NULL;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	graph_walk(from, WALK_DEPENDENCIES, child_pre, child_post, &cs);
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	return (cs.path);
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate /*
6707c478bd9Sstevel@tonic-gate  * Given an array of int's as returned by is_path_to, allocates a string of
6717c478bd9Sstevel@tonic-gate  * their names joined by newlines.  Returns the size of the allocated buffer
6727c478bd9Sstevel@tonic-gate  * in *sz and frees path.
6737c478bd9Sstevel@tonic-gate  */
6747c478bd9Sstevel@tonic-gate static void
6757c478bd9Sstevel@tonic-gate path_to_str(int *path, char **cpp, size_t *sz)
6767c478bd9Sstevel@tonic-gate {
6777c478bd9Sstevel@tonic-gate 	int i;
6787c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
6797c478bd9Sstevel@tonic-gate 	size_t allocd, new_allocd;
6807c478bd9Sstevel@tonic-gate 	char *new, *name;
6817c478bd9Sstevel@tonic-gate 
682*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
6837c478bd9Sstevel@tonic-gate 	assert(path[0] != -1);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	allocd = 1;
6867c478bd9Sstevel@tonic-gate 	*cpp = startd_alloc(1);
6877c478bd9Sstevel@tonic-gate 	(*cpp)[0] = '\0';
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	for (i = 0; path[i] != -1; ++i) {
6907c478bd9Sstevel@tonic-gate 		name = NULL;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 		v = vertex_get_by_id(path[i]);
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 		if (v == NULL)
6957c478bd9Sstevel@tonic-gate 			name = "<deleted>";
6967c478bd9Sstevel@tonic-gate 		else if (v->gv_type == GVT_INST || v->gv_type == GVT_SVC)
6977c478bd9Sstevel@tonic-gate 			name = v->gv_name;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 		if (name != NULL) {
7007c478bd9Sstevel@tonic-gate 			new_allocd = allocd + strlen(name) + 1;
7017c478bd9Sstevel@tonic-gate 			new = startd_alloc(new_allocd);
7027c478bd9Sstevel@tonic-gate 			(void) strcpy(new, *cpp);
7037c478bd9Sstevel@tonic-gate 			(void) strcat(new, name);
7047c478bd9Sstevel@tonic-gate 			(void) strcat(new, "\n");
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 			startd_free(*cpp, allocd);
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 			*cpp = new;
7097c478bd9Sstevel@tonic-gate 			allocd = new_allocd;
7107c478bd9Sstevel@tonic-gate 		}
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	startd_free(path, sizeof (int) * (i + 1));
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	*sz = allocd;
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate /*
7207c478bd9Sstevel@tonic-gate  * This function along with run_sulogin() implements an exclusion relationship
7217c478bd9Sstevel@tonic-gate  * between system/console-login and sulogin.  run_sulogin() will fail if
7227c478bd9Sstevel@tonic-gate  * system/console-login is online, and the graph engine should call
7237c478bd9Sstevel@tonic-gate  * graph_clogin_start() to bring system/console-login online, which defers the
7247c478bd9Sstevel@tonic-gate  * start if sulogin is running.
7257c478bd9Sstevel@tonic-gate  */
7267c478bd9Sstevel@tonic-gate static void
7277c478bd9Sstevel@tonic-gate graph_clogin_start(graph_vertex_t *v)
7287c478bd9Sstevel@tonic-gate {
729*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	if (sulogin_running)
7327c478bd9Sstevel@tonic-gate 		console_login_ready = B_TRUE;
7337c478bd9Sstevel@tonic-gate 	else
7347c478bd9Sstevel@tonic-gate 		vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate static void
7387c478bd9Sstevel@tonic-gate graph_su_start(graph_vertex_t *v)
7397c478bd9Sstevel@tonic-gate {
7407c478bd9Sstevel@tonic-gate 	/*
7417c478bd9Sstevel@tonic-gate 	 * /etc/inittab used to have the initial /sbin/rcS as a 'sysinit'
7427c478bd9Sstevel@tonic-gate 	 * entry with a runlevel of 'S', before jumping to the final
7437c478bd9Sstevel@tonic-gate 	 * target runlevel (as set in initdefault).  We mimic that legacy
7447c478bd9Sstevel@tonic-gate 	 * behavior here.
7457c478bd9Sstevel@tonic-gate 	 */
7467c478bd9Sstevel@tonic-gate 	utmpx_set_runlevel('S', '0', B_FALSE);
7477c478bd9Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate static void
7517c478bd9Sstevel@tonic-gate graph_post_su_online(void)
7527c478bd9Sstevel@tonic-gate {
7537c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('S', 1);
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate static void
7577c478bd9Sstevel@tonic-gate graph_post_su_disable(void)
7587c478bd9Sstevel@tonic-gate {
7597c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('S', 0);
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate static void
7637c478bd9Sstevel@tonic-gate graph_post_mu_online(void)
7647c478bd9Sstevel@tonic-gate {
7657c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('2', 1);
7667c478bd9Sstevel@tonic-gate }
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate static void
7697c478bd9Sstevel@tonic-gate graph_post_mu_disable(void)
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('2', 0);
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate static void
7757c478bd9Sstevel@tonic-gate graph_post_mus_online(void)
7767c478bd9Sstevel@tonic-gate {
7777c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('3', 1);
7787c478bd9Sstevel@tonic-gate }
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate static void
7817c478bd9Sstevel@tonic-gate graph_post_mus_disable(void)
7827c478bd9Sstevel@tonic-gate {
7837c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('3', 0);
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate static struct special_vertex_info {
7877c478bd9Sstevel@tonic-gate 	const char	*name;
7887c478bd9Sstevel@tonic-gate 	void		(*start_f)(graph_vertex_t *);
7897c478bd9Sstevel@tonic-gate 	void		(*post_online_f)(void);
7907c478bd9Sstevel@tonic-gate 	void		(*post_disable_f)(void);
7917c478bd9Sstevel@tonic-gate } special_vertices[] = {
7927c478bd9Sstevel@tonic-gate 	{ CONSOLE_LOGIN_FMRI, graph_clogin_start, NULL, NULL },
7937c478bd9Sstevel@tonic-gate 	{ SCF_MILESTONE_SINGLE_USER, graph_su_start,
7947c478bd9Sstevel@tonic-gate 	    graph_post_su_online, graph_post_su_disable },
7957c478bd9Sstevel@tonic-gate 	{ SCF_MILESTONE_MULTI_USER, NULL,
7967c478bd9Sstevel@tonic-gate 	    graph_post_mu_online, graph_post_mu_disable },
7977c478bd9Sstevel@tonic-gate 	{ SCF_MILESTONE_MULTI_USER_SERVER, NULL,
7987c478bd9Sstevel@tonic-gate 	    graph_post_mus_online, graph_post_mus_disable },
7997c478bd9Sstevel@tonic-gate 	{ NULL },
8007c478bd9Sstevel@tonic-gate };
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate void
8047c478bd9Sstevel@tonic-gate vertex_send_event(graph_vertex_t *v, restarter_event_type_t e)
8057c478bd9Sstevel@tonic-gate {
8067c478bd9Sstevel@tonic-gate 	switch (e) {
8077c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADD_INSTANCE:
8087c478bd9Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_UNINIT);
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&st->st_load_lock);
8117c478bd9Sstevel@tonic-gate 		st->st_load_instances++;
8127c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&st->st_load_lock);
8137c478bd9Sstevel@tonic-gate 		break;
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ENABLE:
8167c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Enabling %s.\n", v->gv_name);
8177c478bd9Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_UNINIT ||
8187c478bd9Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_DISABLED ||
8197c478bd9Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_MAINT);
8207c478bd9Sstevel@tonic-gate 		break;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_DISABLE:
8237c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
8247c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Disabling %s.\n", v->gv_name);
8257c478bd9Sstevel@tonic-gate 		assert(v->gv_state != RESTARTER_STATE_DISABLED);
8267c478bd9Sstevel@tonic-gate 		break;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_STOP:
8297c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Stopping %s.\n", v->gv_name);
8307c478bd9Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_DEGRADED ||
8317c478bd9Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_ONLINE);
8327c478bd9Sstevel@tonic-gate 		break;
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_START:
8357c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Starting %s.\n", v->gv_name);
8367c478bd9Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_OFFLINE);
8377c478bd9Sstevel@tonic-gate 		break;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE:
8407c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_DEGRADED:
8417c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_REFRESH:
8427c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_RESTART:
8437c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF:
8447c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
8457c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE:
8467c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
8477c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
8487c478bd9Sstevel@tonic-gate 		break;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	default:
8517c478bd9Sstevel@tonic-gate #ifndef NDEBUG
8527c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Bad event %d.\n", __FILE__, __LINE__, e);
8537c478bd9Sstevel@tonic-gate #endif
8547c478bd9Sstevel@tonic-gate 		abort();
8557c478bd9Sstevel@tonic-gate 	}
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	restarter_protocol_send_event(v->gv_name, v->gv_restarter_channel, e);
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate static void
8617c478bd9Sstevel@tonic-gate graph_unset_restarter(graph_vertex_t *v)
8627c478bd9Sstevel@tonic-gate {
863*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
8647c478bd9Sstevel@tonic-gate 	assert(v->gv_flags & GV_CONFIGURED);
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_REMOVE_INSTANCE);
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	if (v->gv_restarter_id != -1) {
8697c478bd9Sstevel@tonic-gate 		graph_vertex_t *rv;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 		rv = vertex_get_by_id(v->gv_restarter_id);
8727c478bd9Sstevel@tonic-gate 		graph_remove_edge(v, rv);
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	v->gv_restarter_id = -1;
8767c478bd9Sstevel@tonic-gate 	v->gv_restarter_channel = NULL;
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate 
8793ad28c1eSrm88369 /*
8803ad28c1eSrm88369  * Return VERTEX_REMOVED when the vertex passed in argument is deleted from the
8813ad28c1eSrm88369  * dgraph otherwise return VERTEX_INUSE.
8823ad28c1eSrm88369  */
8833ad28c1eSrm88369 static int
8843ad28c1eSrm88369 free_if_unrefed(graph_vertex_t *v)
8853ad28c1eSrm88369 {
886*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
8873ad28c1eSrm88369 
8883ad28c1eSrm88369 	if (v->gv_refs > 0)
8893ad28c1eSrm88369 		return (VERTEX_INUSE);
8903ad28c1eSrm88369 
8913ad28c1eSrm88369 	if (v->gv_type == GVT_SVC &&
8923ad28c1eSrm88369 	    uu_list_numnodes(v->gv_dependents) == 0 &&
8933ad28c1eSrm88369 	    uu_list_numnodes(v->gv_dependencies) == 0) {
8943ad28c1eSrm88369 		graph_remove_vertex(v);
8953ad28c1eSrm88369 		return (VERTEX_REMOVED);
8963ad28c1eSrm88369 	} else if (v->gv_type == GVT_INST &&
8973ad28c1eSrm88369 	    (v->gv_flags & GV_CONFIGURED) == 0 &&
8983ad28c1eSrm88369 	    uu_list_numnodes(v->gv_dependents) == 1 &&
8993ad28c1eSrm88369 	    uu_list_numnodes(v->gv_dependencies) == 0) {
9003ad28c1eSrm88369 		remove_inst_vertex(v);
9013ad28c1eSrm88369 		return (VERTEX_REMOVED);
9023ad28c1eSrm88369 	}
9033ad28c1eSrm88369 
9043ad28c1eSrm88369 	return (VERTEX_INUSE);
9053ad28c1eSrm88369 }
9063ad28c1eSrm88369 
9077c478bd9Sstevel@tonic-gate static void
9087c478bd9Sstevel@tonic-gate delete_depgroup(graph_vertex_t *v)
9097c478bd9Sstevel@tonic-gate {
9107c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
9117c478bd9Sstevel@tonic-gate 	graph_vertex_t *dv;
9127c478bd9Sstevel@tonic-gate 
913*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
9147c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_GROUP);
9157c478bd9Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependents) == 0);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	while ((e = uu_list_first(v->gv_dependencies)) != NULL) {
9187c478bd9Sstevel@tonic-gate 		dv = e->ge_vertex;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 		graph_remove_edge(v, dv);
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 		switch (dv->gv_type) {
9237c478bd9Sstevel@tonic-gate 		case GVT_INST:		/* instance dependency */
9247c478bd9Sstevel@tonic-gate 		case GVT_SVC:		/* service dependency */
9253ad28c1eSrm88369 			(void) free_if_unrefed(dv);
9267c478bd9Sstevel@tonic-gate 			break;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 		case GVT_FILE:		/* file dependency */
9297c478bd9Sstevel@tonic-gate 			assert(uu_list_numnodes(dv->gv_dependencies) == 0);
9307c478bd9Sstevel@tonic-gate 			if (uu_list_numnodes(dv->gv_dependents) == 0)
9317c478bd9Sstevel@tonic-gate 				graph_remove_vertex(dv);
9327c478bd9Sstevel@tonic-gate 			break;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 		default:
9357c478bd9Sstevel@tonic-gate #ifndef NDEBUG
9367c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected node type %d", __FILE__,
9377c478bd9Sstevel@tonic-gate 			    __LINE__, dv->gv_type);
9387c478bd9Sstevel@tonic-gate #endif
9397c478bd9Sstevel@tonic-gate 			abort();
9407c478bd9Sstevel@tonic-gate 		}
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	graph_remove_vertex(v);
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate static int
9477c478bd9Sstevel@tonic-gate delete_instance_deps_cb(graph_edge_t *e, void **ptrs)
9487c478bd9Sstevel@tonic-gate {
9497c478bd9Sstevel@tonic-gate 	graph_vertex_t *v = ptrs[0];
9507c478bd9Sstevel@tonic-gate 	boolean_t delete_restarter_dep = (boolean_t)ptrs[1];
9517c478bd9Sstevel@tonic-gate 	graph_vertex_t *dv;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	dv = e->ge_vertex;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	/*
9567c478bd9Sstevel@tonic-gate 	 * We have four possibilities here:
9577c478bd9Sstevel@tonic-gate 	 *   - GVT_INST: restarter
9587c478bd9Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_INST: instance dependency
9597c478bd9Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_SVC - GV_INST: service dependency
9607c478bd9Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_FILE: file dependency
9617c478bd9Sstevel@tonic-gate 	 */
9627c478bd9Sstevel@tonic-gate 	switch (dv->gv_type) {
9637c478bd9Sstevel@tonic-gate 	case GVT_INST:	/* restarter */
9647c478bd9Sstevel@tonic-gate 		assert(dv->gv_id == v->gv_restarter_id);
9657c478bd9Sstevel@tonic-gate 		if (delete_restarter_dep)
9667c478bd9Sstevel@tonic-gate 			graph_remove_edge(v, dv);
9677c478bd9Sstevel@tonic-gate 		break;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	case GVT_GROUP:	/* pg dependency */
9707c478bd9Sstevel@tonic-gate 		graph_remove_edge(v, dv);
9717c478bd9Sstevel@tonic-gate 		delete_depgroup(dv);
9727c478bd9Sstevel@tonic-gate 		break;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	case GVT_FILE:
9757c478bd9Sstevel@tonic-gate 		/* These are currently not direct dependencies */
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	default:
9787c478bd9Sstevel@tonic-gate #ifndef NDEBUG
9797c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Bad vertex type %d.\n", __FILE__, __LINE__,
9807c478bd9Sstevel@tonic-gate 		    dv->gv_type);
9817c478bd9Sstevel@tonic-gate #endif
9827c478bd9Sstevel@tonic-gate 		abort();
9837c478bd9Sstevel@tonic-gate 	}
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate static void
9897c478bd9Sstevel@tonic-gate delete_instance_dependencies(graph_vertex_t *v, boolean_t delete_restarter_dep)
9907c478bd9Sstevel@tonic-gate {
9917c478bd9Sstevel@tonic-gate 	void *ptrs[2];
9927c478bd9Sstevel@tonic-gate 	int r;
9937c478bd9Sstevel@tonic-gate 
994*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
9957c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	ptrs[0] = v;
9987c478bd9Sstevel@tonic-gate 	ptrs[1] = (void *)delete_restarter_dep;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate 	r = uu_list_walk(v->gv_dependencies,
10017c478bd9Sstevel@tonic-gate 	    (uu_walk_fn_t *)delete_instance_deps_cb, &ptrs, UU_WALK_ROBUST);
10027c478bd9Sstevel@tonic-gate 	assert(r == 0);
10037c478bd9Sstevel@tonic-gate }
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate /*
10067c478bd9Sstevel@tonic-gate  * int graph_insert_vertex_unconfigured()
10077c478bd9Sstevel@tonic-gate  *   Insert a vertex without sending any restarter events. If the vertex
10087c478bd9Sstevel@tonic-gate  *   already exists or creation is successful, return a pointer to it in *vp.
10097c478bd9Sstevel@tonic-gate  *
10107c478bd9Sstevel@tonic-gate  *   If type is not GVT_GROUP, dt can remain unset.
10117c478bd9Sstevel@tonic-gate  *
10127c478bd9Sstevel@tonic-gate  *   Returns 0, EEXIST, or EINVAL if the arguments are invalid (i.e., fmri
10137c478bd9Sstevel@tonic-gate  *   doesn't agree with type, or type doesn't agree with dt).
10147c478bd9Sstevel@tonic-gate  */
10157c478bd9Sstevel@tonic-gate static int
10167c478bd9Sstevel@tonic-gate graph_insert_vertex_unconfigured(const char *fmri, gv_type_t type,
10177c478bd9Sstevel@tonic-gate     depgroup_type_t dt, restarter_error_t rt, graph_vertex_t **vp)
10187c478bd9Sstevel@tonic-gate {
10197c478bd9Sstevel@tonic-gate 	int r;
10207c478bd9Sstevel@tonic-gate 	int i;
10217c478bd9Sstevel@tonic-gate 
1022*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	switch (type) {
10257c478bd9Sstevel@tonic-gate 	case GVT_SVC:
10267c478bd9Sstevel@tonic-gate 	case GVT_INST:
10277c478bd9Sstevel@tonic-gate 		if (strncmp(fmri, "svc:", sizeof ("svc:") - 1) != 0)
10287c478bd9Sstevel@tonic-gate 			return (EINVAL);
10297c478bd9Sstevel@tonic-gate 		break;
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	case GVT_FILE:
10327c478bd9Sstevel@tonic-gate 		if (strncmp(fmri, "file:", sizeof ("file:") - 1) != 0)
10337c478bd9Sstevel@tonic-gate 			return (EINVAL);
10347c478bd9Sstevel@tonic-gate 		break;
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	case GVT_GROUP:
10377c478bd9Sstevel@tonic-gate 		if (dt <= 0 || rt < 0)
10387c478bd9Sstevel@tonic-gate 			return (EINVAL);
10397c478bd9Sstevel@tonic-gate 		break;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	default:
10427c478bd9Sstevel@tonic-gate #ifndef NDEBUG
10437c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown type %d.\n", __FILE__, __LINE__, type);
10447c478bd9Sstevel@tonic-gate #endif
10457c478bd9Sstevel@tonic-gate 		abort();
10467c478bd9Sstevel@tonic-gate 	}
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	*vp = vertex_get_by_name(fmri);
10497c478bd9Sstevel@tonic-gate 	if (*vp != NULL)
10507c478bd9Sstevel@tonic-gate 		return (EEXIST);
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	*vp = graph_add_vertex(fmri);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	(*vp)->gv_type = type;
10557c478bd9Sstevel@tonic-gate 	(*vp)->gv_depgroup = dt;
10567c478bd9Sstevel@tonic-gate 	(*vp)->gv_restart = rt;
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 	(*vp)->gv_flags = 0;
10597c478bd9Sstevel@tonic-gate 	(*vp)->gv_state = RESTARTER_STATE_NONE;
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	for (i = 0; special_vertices[i].name != NULL; ++i) {
10627c478bd9Sstevel@tonic-gate 		if (strcmp(fmri, special_vertices[i].name) == 0) {
10637c478bd9Sstevel@tonic-gate 			(*vp)->gv_start_f = special_vertices[i].start_f;
10647c478bd9Sstevel@tonic-gate 			(*vp)->gv_post_online_f =
10657c478bd9Sstevel@tonic-gate 			    special_vertices[i].post_online_f;
10667c478bd9Sstevel@tonic-gate 			(*vp)->gv_post_disable_f =
10677c478bd9Sstevel@tonic-gate 			    special_vertices[i].post_disable_f;
10687c478bd9Sstevel@tonic-gate 			break;
10697c478bd9Sstevel@tonic-gate 		}
10707c478bd9Sstevel@tonic-gate 	}
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	(*vp)->gv_restarter_id = -1;
10737c478bd9Sstevel@tonic-gate 	(*vp)->gv_restarter_channel = 0;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	if (type == GVT_INST) {
10767c478bd9Sstevel@tonic-gate 		char *sfmri;
10777c478bd9Sstevel@tonic-gate 		graph_vertex_t *sv;
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 		sfmri = inst_fmri_to_svc_fmri(fmri);
10807c478bd9Sstevel@tonic-gate 		sv = vertex_get_by_name(sfmri);
10817c478bd9Sstevel@tonic-gate 		if (sv == NULL) {
10827c478bd9Sstevel@tonic-gate 			r = graph_insert_vertex_unconfigured(sfmri, GVT_SVC, 0,
10837c478bd9Sstevel@tonic-gate 			    0, &sv);
10847c478bd9Sstevel@tonic-gate 			assert(r == 0);
10857c478bd9Sstevel@tonic-gate 		}
10867c478bd9Sstevel@tonic-gate 		startd_free(sfmri, max_scf_fmri_size);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 		graph_add_edge(sv, *vp);
10897c478bd9Sstevel@tonic-gate 	}
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	/*
10927c478bd9Sstevel@tonic-gate 	 * If this vertex is in the subgraph, mark it as so, for both
10937c478bd9Sstevel@tonic-gate 	 * GVT_INST and GVT_SERVICE verteces.
10947c478bd9Sstevel@tonic-gate 	 * A GVT_SERVICE vertex can only be in the subgraph if another instance
10957c478bd9Sstevel@tonic-gate 	 * depends on it, in which case it's already been added to the graph
10967c478bd9Sstevel@tonic-gate 	 * and marked as in the subgraph (by refresh_vertex()).  If a
10977c478bd9Sstevel@tonic-gate 	 * GVT_SERVICE vertex was freshly added (by the code above), it means
10987c478bd9Sstevel@tonic-gate 	 * that it has no dependents, and cannot be in the subgraph.
10997c478bd9Sstevel@tonic-gate 	 * Regardless of this, we still check that gv_flags includes
11007c478bd9Sstevel@tonic-gate 	 * GV_INSUBGRAPH in the event that future behavior causes the above
11017c478bd9Sstevel@tonic-gate 	 * code to add a GVT_SERVICE vertex which should be in the subgraph.
11027c478bd9Sstevel@tonic-gate 	 */
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	(*vp)->gv_flags |= (should_be_in_subgraph(*vp)? GV_INSUBGRAPH : 0);
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	return (0);
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate /*
11107c478bd9Sstevel@tonic-gate  * Returns 0 on success or ELOOP if the dependency would create a cycle.
11117c478bd9Sstevel@tonic-gate  */
11127c478bd9Sstevel@tonic-gate static int
11137c478bd9Sstevel@tonic-gate graph_insert_dependency(graph_vertex_t *fv, graph_vertex_t *tv, int **pathp)
11147c478bd9Sstevel@tonic-gate {
11157c478bd9Sstevel@tonic-gate 	hrtime_t now;
11167c478bd9Sstevel@tonic-gate 
1117*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	/* cycle detection */
11207c478bd9Sstevel@tonic-gate 	now = gethrtime();
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	/* Don't follow exclusions. */
11237c478bd9Sstevel@tonic-gate 	if (!(fv->gv_type == GVT_GROUP &&
11247c478bd9Sstevel@tonic-gate 	    fv->gv_depgroup == DEPGRP_EXCLUDE_ALL)) {
11257c478bd9Sstevel@tonic-gate 		*pathp = is_path_to(tv, fv);
11267c478bd9Sstevel@tonic-gate 		if (*pathp)
11277c478bd9Sstevel@tonic-gate 			return (ELOOP);
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	dep_cycle_ns += gethrtime() - now;
11317c478bd9Sstevel@tonic-gate 	++dep_inserts;
11327c478bd9Sstevel@tonic-gate 	now = gethrtime();
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	graph_add_edge(fv, tv);
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	dep_insert_ns += gethrtime() - now;
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	/* Check if the dependency adds the "to" vertex to the subgraph */
11397c478bd9Sstevel@tonic-gate 	tv->gv_flags |= (should_be_in_subgraph(tv) ? GV_INSUBGRAPH : 0);
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 	return (0);
11427c478bd9Sstevel@tonic-gate }
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate static int
11457c478bd9Sstevel@tonic-gate inst_running(graph_vertex_t *v)
11467c478bd9Sstevel@tonic-gate {
11477c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	if (v->gv_state == RESTARTER_STATE_ONLINE ||
11507c478bd9Sstevel@tonic-gate 	    v->gv_state == RESTARTER_STATE_DEGRADED)
11517c478bd9Sstevel@tonic-gate 		return (1);
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	return (0);
11547c478bd9Sstevel@tonic-gate }
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate /*
11577c478bd9Sstevel@tonic-gate  * The dependency evaluation functions return
11587c478bd9Sstevel@tonic-gate  *   1 - dependency satisfied
11597c478bd9Sstevel@tonic-gate  *   0 - dependency unsatisfied
11607c478bd9Sstevel@tonic-gate  *   -1 - dependency unsatisfiable (without administrator intervention)
11617c478bd9Sstevel@tonic-gate  *
11627c478bd9Sstevel@tonic-gate  * The functions also take a boolean satbility argument.  When true, the
11637c478bd9Sstevel@tonic-gate  * functions may recurse in order to determine satisfiability.
11647c478bd9Sstevel@tonic-gate  */
11657c478bd9Sstevel@tonic-gate static int require_any_satisfied(graph_vertex_t *, boolean_t);
11667c478bd9Sstevel@tonic-gate static int dependency_satisfied(graph_vertex_t *, boolean_t);
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate /*
11697c478bd9Sstevel@tonic-gate  * A require_all dependency is unsatisfied if any elements are unsatisfied.  It
11707c478bd9Sstevel@tonic-gate  * is unsatisfiable if any elements are unsatisfiable.
11717c478bd9Sstevel@tonic-gate  */
11727c478bd9Sstevel@tonic-gate static int
11737c478bd9Sstevel@tonic-gate require_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
11747c478bd9Sstevel@tonic-gate {
11757c478bd9Sstevel@tonic-gate 	graph_edge_t *edge;
11767c478bd9Sstevel@tonic-gate 	int i;
11777c478bd9Sstevel@tonic-gate 	boolean_t any_unsatisfied;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	if (uu_list_numnodes(groupv->gv_dependencies) == 0)
11807c478bd9Sstevel@tonic-gate 		return (1);
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	any_unsatisfied = B_FALSE;
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
11857c478bd9Sstevel@tonic-gate 	    edge != NULL;
11867c478bd9Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
11877c478bd9Sstevel@tonic-gate 		i = dependency_satisfied(edge->ge_vertex, satbility);
11887c478bd9Sstevel@tonic-gate 		if (i == 1)
11897c478bd9Sstevel@tonic-gate 			continue;
11907c478bd9Sstevel@tonic-gate 
11913efb42ecSrm88369 		log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
11927c478bd9Sstevel@tonic-gate 		    "require_all(%s): %s is unsatisfi%s.\n", groupv->gv_name,
11937c478bd9Sstevel@tonic-gate 		    edge->ge_vertex->gv_name, i == 0 ? "ed" : "able");
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 		if (!satbility)
11967c478bd9Sstevel@tonic-gate 			return (0);
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 		if (i == -1)
11997c478bd9Sstevel@tonic-gate 			return (-1);
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 		any_unsatisfied = B_TRUE;
12027c478bd9Sstevel@tonic-gate 	}
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	return (any_unsatisfied ? 0 : 1);
12057c478bd9Sstevel@tonic-gate }
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate /*
12087c478bd9Sstevel@tonic-gate  * A require_any dependency is satisfied if any element is satisfied.  It is
12097c478bd9Sstevel@tonic-gate  * satisfiable if any element is satisfiable.
12107c478bd9Sstevel@tonic-gate  */
12117c478bd9Sstevel@tonic-gate static int
12127c478bd9Sstevel@tonic-gate require_any_satisfied(graph_vertex_t *groupv, boolean_t satbility)
12137c478bd9Sstevel@tonic-gate {
12147c478bd9Sstevel@tonic-gate 	graph_edge_t *edge;
12157c478bd9Sstevel@tonic-gate 	int s;
12167c478bd9Sstevel@tonic-gate 	boolean_t satisfiable;
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	if (uu_list_numnodes(groupv->gv_dependencies) == 0)
12197c478bd9Sstevel@tonic-gate 		return (1);
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	satisfiable = B_FALSE;
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
12247c478bd9Sstevel@tonic-gate 	    edge != NULL;
12257c478bd9Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
12267c478bd9Sstevel@tonic-gate 		s = dependency_satisfied(edge->ge_vertex, satbility);
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 		if (s == 1)
12297c478bd9Sstevel@tonic-gate 			return (1);
12307c478bd9Sstevel@tonic-gate 
12313efb42ecSrm88369 		log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
12327c478bd9Sstevel@tonic-gate 		    "require_any(%s): %s is unsatisfi%s.\n",
12337c478bd9Sstevel@tonic-gate 		    groupv->gv_name, edge->ge_vertex->gv_name,
12347c478bd9Sstevel@tonic-gate 		    s == 0 ? "ed" : "able");
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 		if (satbility && s == 0)
12377c478bd9Sstevel@tonic-gate 			satisfiable = B_TRUE;
12387c478bd9Sstevel@tonic-gate 	}
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	return (!satbility || satisfiable ? 0 : -1);
12417c478bd9Sstevel@tonic-gate }
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate /*
12447c478bd9Sstevel@tonic-gate  * An optional_all dependency only considers elements which are configured,
12457c478bd9Sstevel@tonic-gate  * enabled, and not in maintenance.  If any are unsatisfied, then the dependency
12467c478bd9Sstevel@tonic-gate  * is unsatisfied.
12477c478bd9Sstevel@tonic-gate  *
12487c478bd9Sstevel@tonic-gate  * Offline dependencies which are waiting for a dependency to come online are
12497c478bd9Sstevel@tonic-gate  * unsatisfied.  Offline dependences which cannot possibly come online
12507c478bd9Sstevel@tonic-gate  * (unsatisfiable) are always considered satisfied.
12517c478bd9Sstevel@tonic-gate  */
12527c478bd9Sstevel@tonic-gate static int
12537c478bd9Sstevel@tonic-gate optional_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
12547c478bd9Sstevel@tonic-gate {
12557c478bd9Sstevel@tonic-gate 	graph_edge_t *edge;
12567c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
12577c478bd9Sstevel@tonic-gate 	boolean_t any_qualified;
12587c478bd9Sstevel@tonic-gate 	boolean_t any_unsatisfied;
12597c478bd9Sstevel@tonic-gate 	int i;
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	any_qualified = B_FALSE;
12627c478bd9Sstevel@tonic-gate 	any_unsatisfied = B_FALSE;
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
12657c478bd9Sstevel@tonic-gate 	    edge != NULL;
12667c478bd9Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
12677c478bd9Sstevel@tonic-gate 		v = edge->ge_vertex;
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 		switch (v->gv_type) {
12707c478bd9Sstevel@tonic-gate 		case GVT_INST:
12717c478bd9Sstevel@tonic-gate 			/* Skip missing or disabled instances */
12727c478bd9Sstevel@tonic-gate 			if ((v->gv_flags & (GV_CONFIGURED | GV_ENABLED)) !=
12737c478bd9Sstevel@tonic-gate 			    (GV_CONFIGURED | GV_ENABLED))
12747c478bd9Sstevel@tonic-gate 				continue;
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 			if (v->gv_state == RESTARTER_STATE_MAINT)
12777c478bd9Sstevel@tonic-gate 				continue;
12787c478bd9Sstevel@tonic-gate 
1279207246e9SRenaud Manus 			if (v->gv_flags & GV_TOOFFLINE)
1280207246e9SRenaud Manus 				continue;
1281207246e9SRenaud Manus 
12827c478bd9Sstevel@tonic-gate 			any_qualified = B_TRUE;
12837c478bd9Sstevel@tonic-gate 			if (v->gv_state == RESTARTER_STATE_OFFLINE) {
12847c478bd9Sstevel@tonic-gate 				/*
12857c478bd9Sstevel@tonic-gate 				 * For offline dependencies, treat unsatisfiable
12867c478bd9Sstevel@tonic-gate 				 * as satisfied.
12877c478bd9Sstevel@tonic-gate 				 */
12887c478bd9Sstevel@tonic-gate 				i = dependency_satisfied(v, B_TRUE);
12897c478bd9Sstevel@tonic-gate 				if (i == -1)
12907c478bd9Sstevel@tonic-gate 					i = 1;
12917c478bd9Sstevel@tonic-gate 			} else if (v->gv_state == RESTARTER_STATE_DISABLED) {
12927c478bd9Sstevel@tonic-gate 				/*
12937c478bd9Sstevel@tonic-gate 				 * The service is enabled, but hasn't
12947c478bd9Sstevel@tonic-gate 				 * transitioned out of disabled yet.  Treat it
12957c478bd9Sstevel@tonic-gate 				 * as unsatisfied (not unsatisfiable).
12967c478bd9Sstevel@tonic-gate 				 */
12977c478bd9Sstevel@tonic-gate 				i = 0;
12987c478bd9Sstevel@tonic-gate 			} else {
12997c478bd9Sstevel@tonic-gate 				i = dependency_satisfied(v, satbility);
13007c478bd9Sstevel@tonic-gate 			}
13017c478bd9Sstevel@tonic-gate 			break;
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 		case GVT_FILE:
13047c478bd9Sstevel@tonic-gate 			any_qualified = B_TRUE;
13057c478bd9Sstevel@tonic-gate 			i = dependency_satisfied(v, satbility);
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 			break;
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 		case GVT_SVC: {
13107c478bd9Sstevel@tonic-gate 			boolean_t svc_any_qualified;
13117c478bd9Sstevel@tonic-gate 			boolean_t svc_satisfied;
13127c478bd9Sstevel@tonic-gate 			boolean_t svc_satisfiable;
13137c478bd9Sstevel@tonic-gate 			graph_vertex_t *v2;
13147c478bd9Sstevel@tonic-gate 			graph_edge_t *e2;
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 			svc_any_qualified = B_FALSE;
13177c478bd9Sstevel@tonic-gate 			svc_satisfied = B_FALSE;
13187c478bd9Sstevel@tonic-gate 			svc_satisfiable = B_FALSE;
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 			for (e2 = uu_list_first(v->gv_dependencies);
13217c478bd9Sstevel@tonic-gate 			    e2 != NULL;
13227c478bd9Sstevel@tonic-gate 			    e2 = uu_list_next(v->gv_dependencies, e2)) {
13237c478bd9Sstevel@tonic-gate 				v2 = e2->ge_vertex;
13247c478bd9Sstevel@tonic-gate 				assert(v2->gv_type == GVT_INST);
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 				if ((v2->gv_flags &
13277c478bd9Sstevel@tonic-gate 				    (GV_CONFIGURED | GV_ENABLED)) !=
13287c478bd9Sstevel@tonic-gate 				    (GV_CONFIGURED | GV_ENABLED))
13297c478bd9Sstevel@tonic-gate 					continue;
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 				if (v2->gv_state == RESTARTER_STATE_MAINT)
13327c478bd9Sstevel@tonic-gate 					continue;
13337c478bd9Sstevel@tonic-gate 
1334207246e9SRenaud Manus 				if (v2->gv_flags & GV_TOOFFLINE)
1335207246e9SRenaud Manus 					continue;
1336207246e9SRenaud Manus 
13377c478bd9Sstevel@tonic-gate 				svc_any_qualified = B_TRUE;
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 				if (v2->gv_state == RESTARTER_STATE_OFFLINE) {
13407c478bd9Sstevel@tonic-gate 					/*
13417c478bd9Sstevel@tonic-gate 					 * For offline dependencies, treat
13427c478bd9Sstevel@tonic-gate 					 * unsatisfiable as satisfied.
13437c478bd9Sstevel@tonic-gate 					 */
13447c478bd9Sstevel@tonic-gate 					i = dependency_satisfied(v2, B_TRUE);
13457c478bd9Sstevel@tonic-gate 					if (i == -1)
13467c478bd9Sstevel@tonic-gate 						i = 1;
13477c478bd9Sstevel@tonic-gate 				} else if (v2->gv_state ==
13487c478bd9Sstevel@tonic-gate 				    RESTARTER_STATE_DISABLED) {
13497c478bd9Sstevel@tonic-gate 					i = 0;
13507c478bd9Sstevel@tonic-gate 				} else {
13517c478bd9Sstevel@tonic-gate 					i = dependency_satisfied(v2, satbility);
13527c478bd9Sstevel@tonic-gate 				}
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 				if (i == 1) {
13557c478bd9Sstevel@tonic-gate 					svc_satisfied = B_TRUE;
13567c478bd9Sstevel@tonic-gate 					break;
13577c478bd9Sstevel@tonic-gate 				}
13587c478bd9Sstevel@tonic-gate 				if (i == 0)
13597c478bd9Sstevel@tonic-gate 					svc_satisfiable = B_TRUE;
13607c478bd9Sstevel@tonic-gate 			}
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 			if (!svc_any_qualified)
13637c478bd9Sstevel@tonic-gate 				continue;
13647c478bd9Sstevel@tonic-gate 			any_qualified = B_TRUE;
13657c478bd9Sstevel@tonic-gate 			if (svc_satisfied) {
13667c478bd9Sstevel@tonic-gate 				i = 1;
13677c478bd9Sstevel@tonic-gate 			} else if (svc_satisfiable) {
13687c478bd9Sstevel@tonic-gate 				i = 0;
13697c478bd9Sstevel@tonic-gate 			} else {
13707c478bd9Sstevel@tonic-gate 				i = -1;
13717c478bd9Sstevel@tonic-gate 			}
13727c478bd9Sstevel@tonic-gate 			break;
13737c478bd9Sstevel@tonic-gate 		}
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 		case GVT_GROUP:
13767c478bd9Sstevel@tonic-gate 		default:
13777c478bd9Sstevel@tonic-gate #ifndef NDEBUG
13787c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
13797c478bd9Sstevel@tonic-gate 			    __LINE__, v->gv_type);
13807c478bd9Sstevel@tonic-gate #endif
13817c478bd9Sstevel@tonic-gate 			abort();
13827c478bd9Sstevel@tonic-gate 		}
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 		if (i == 1)
13857c478bd9Sstevel@tonic-gate 			continue;
13867c478bd9Sstevel@tonic-gate 
13873efb42ecSrm88369 		log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
13887c478bd9Sstevel@tonic-gate 		    "optional_all(%s): %s is unsatisfi%s.\n", groupv->gv_name,
13897c478bd9Sstevel@tonic-gate 		    v->gv_name, i == 0 ? "ed" : "able");
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 		if (!satbility)
13927c478bd9Sstevel@tonic-gate 			return (0);
13937c478bd9Sstevel@tonic-gate 		if (i == -1)
13947c478bd9Sstevel@tonic-gate 			return (-1);
13957c478bd9Sstevel@tonic-gate 		any_unsatisfied = B_TRUE;
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	if (!any_qualified)
13997c478bd9Sstevel@tonic-gate 		return (1);
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	return (any_unsatisfied ? 0 : 1);
14027c478bd9Sstevel@tonic-gate }
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate /*
14057c478bd9Sstevel@tonic-gate  * An exclude_all dependency is unsatisfied if any non-service element is
14067c478bd9Sstevel@tonic-gate  * satisfied or any service instance which is configured, enabled, and not in
14077c478bd9Sstevel@tonic-gate  * maintenance is satisfied.  Usually when unsatisfied, it is also
14087c478bd9Sstevel@tonic-gate  * unsatisfiable.
14097c478bd9Sstevel@tonic-gate  */
14107c478bd9Sstevel@tonic-gate #define	LOG_EXCLUDE(u, v)						\
14113efb42ecSrm88369 	log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,			\
14123efb42ecSrm88369 	    "exclude_all(%s): %s is satisfied.\n",			\
14137c478bd9Sstevel@tonic-gate 	    (u)->gv_name, (v)->gv_name)
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate /* ARGSUSED */
14167c478bd9Sstevel@tonic-gate static int
14177c478bd9Sstevel@tonic-gate exclude_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
14187c478bd9Sstevel@tonic-gate {
14197c478bd9Sstevel@tonic-gate 	graph_edge_t *edge, *e2;
14207c478bd9Sstevel@tonic-gate 	graph_vertex_t *v, *v2;
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
14237c478bd9Sstevel@tonic-gate 	    edge != NULL;
14247c478bd9Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
14257c478bd9Sstevel@tonic-gate 		v = edge->ge_vertex;
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 		switch (v->gv_type) {
14287c478bd9Sstevel@tonic-gate 		case GVT_INST:
14297c478bd9Sstevel@tonic-gate 			if ((v->gv_flags & GV_CONFIGURED) == 0)
14307c478bd9Sstevel@tonic-gate 				continue;
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 			switch (v->gv_state) {
14337c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_ONLINE:
14347c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_DEGRADED:
14357c478bd9Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v);
14367c478bd9Sstevel@tonic-gate 				return (v->gv_flags & GV_ENABLED ? -1 : 0);
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_OFFLINE:
14397c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_UNINIT:
14407c478bd9Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v);
14417c478bd9Sstevel@tonic-gate 				return (0);
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_DISABLED:
14447c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_MAINT:
14457c478bd9Sstevel@tonic-gate 				continue;
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 			default:
14487c478bd9Sstevel@tonic-gate #ifndef NDEBUG
14497c478bd9Sstevel@tonic-gate 				uu_warn("%s:%d: Unexpected vertex state %d.\n",
14507c478bd9Sstevel@tonic-gate 				    __FILE__, __LINE__, v->gv_state);
14517c478bd9Sstevel@tonic-gate #endif
14527c478bd9Sstevel@tonic-gate 				abort();
14537c478bd9Sstevel@tonic-gate 			}
14547c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 		case GVT_SVC:
14577c478bd9Sstevel@tonic-gate 			break;
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 		case GVT_FILE:
14607c478bd9Sstevel@tonic-gate 			if (!file_ready(v))
14617c478bd9Sstevel@tonic-gate 				continue;
14627c478bd9Sstevel@tonic-gate 			LOG_EXCLUDE(groupv, v);
14637c478bd9Sstevel@tonic-gate 			return (-1);
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 		case GVT_GROUP:
14667c478bd9Sstevel@tonic-gate 		default:
14677c478bd9Sstevel@tonic-gate #ifndef NDEBUG
14687c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
14697c478bd9Sstevel@tonic-gate 			    __LINE__, v->gv_type);
14707c478bd9Sstevel@tonic-gate #endif
14717c478bd9Sstevel@tonic-gate 			abort();
14727c478bd9Sstevel@tonic-gate 		}
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 		/* v represents a service */
14757c478bd9Sstevel@tonic-gate 		if (uu_list_numnodes(v->gv_dependencies) == 0)
14767c478bd9Sstevel@tonic-gate 			continue;
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 		for (e2 = uu_list_first(v->gv_dependencies);
14797c478bd9Sstevel@tonic-gate 		    e2 != NULL;
14807c478bd9Sstevel@tonic-gate 		    e2 = uu_list_next(v->gv_dependencies, e2)) {
14817c478bd9Sstevel@tonic-gate 			v2 = e2->ge_vertex;
14827c478bd9Sstevel@tonic-gate 			assert(v2->gv_type == GVT_INST);
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 			if ((v2->gv_flags & GV_CONFIGURED) == 0)
14857c478bd9Sstevel@tonic-gate 				continue;
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 			switch (v2->gv_state) {
14887c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_ONLINE:
14897c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_DEGRADED:
14907c478bd9Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v2);
14917c478bd9Sstevel@tonic-gate 				return (v2->gv_flags & GV_ENABLED ? -1 : 0);
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_OFFLINE:
14947c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_UNINIT:
14957c478bd9Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v2);
14967c478bd9Sstevel@tonic-gate 				return (0);
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_DISABLED:
14997c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_MAINT:
15007c478bd9Sstevel@tonic-gate 				continue;
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 			default:
15037c478bd9Sstevel@tonic-gate #ifndef NDEBUG
15047c478bd9Sstevel@tonic-gate 				uu_warn("%s:%d: Unexpected vertex type %d.\n",
15057c478bd9Sstevel@tonic-gate 				    __FILE__, __LINE__, v2->gv_type);
15067c478bd9Sstevel@tonic-gate #endif
15077c478bd9Sstevel@tonic-gate 				abort();
15087c478bd9Sstevel@tonic-gate 			}
15097c478bd9Sstevel@tonic-gate 		}
15107c478bd9Sstevel@tonic-gate 	}
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	return (1);
15137c478bd9Sstevel@tonic-gate }
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate /*
15167c478bd9Sstevel@tonic-gate  * int instance_satisfied()
15177c478bd9Sstevel@tonic-gate  *   Determine if all the dependencies are satisfied for the supplied instance
15187c478bd9Sstevel@tonic-gate  *   vertex. Return 1 if they are, 0 if they aren't, and -1 if they won't be
15197c478bd9Sstevel@tonic-gate  *   without administrator intervention.
15207c478bd9Sstevel@tonic-gate  */
15217c478bd9Sstevel@tonic-gate static int
15227c478bd9Sstevel@tonic-gate instance_satisfied(graph_vertex_t *v, boolean_t satbility)
15237c478bd9Sstevel@tonic-gate {
15247c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
15257c478bd9Sstevel@tonic-gate 	assert(!inst_running(v));
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	return (require_all_satisfied(v, satbility));
15287c478bd9Sstevel@tonic-gate }
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate /*
15317c478bd9Sstevel@tonic-gate  * Decide whether v can satisfy a dependency.  v can either be a child of
15327c478bd9Sstevel@tonic-gate  * a group vertex, or of an instance vertex.
15337c478bd9Sstevel@tonic-gate  */
15347c478bd9Sstevel@tonic-gate static int
15357c478bd9Sstevel@tonic-gate dependency_satisfied(graph_vertex_t *v, boolean_t satbility)
15367c478bd9Sstevel@tonic-gate {
15377c478bd9Sstevel@tonic-gate 	switch (v->gv_type) {
15387c478bd9Sstevel@tonic-gate 	case GVT_INST:
153970cbfe41SPhilippe Jung 		if ((v->gv_flags & GV_CONFIGURED) == 0) {
154070cbfe41SPhilippe Jung 			if (v->gv_flags & GV_DEATHROW) {
154170cbfe41SPhilippe Jung 				/*
154270cbfe41SPhilippe Jung 				 * A dependency on an instance with GV_DEATHROW
154370cbfe41SPhilippe Jung 				 * flag is always considered as satisfied.
154470cbfe41SPhilippe Jung 				 */
154570cbfe41SPhilippe Jung 				return (1);
154670cbfe41SPhilippe Jung 			}
15477c478bd9Sstevel@tonic-gate 			return (-1);
154870cbfe41SPhilippe Jung 		}
15497c478bd9Sstevel@tonic-gate 
1550207246e9SRenaud Manus 		/*
1551207246e9SRenaud Manus 		 * Any vertex with the GV_TOOFFLINE flag set is guaranteed
1552207246e9SRenaud Manus 		 * to have its dependencies unsatisfiable.
1553207246e9SRenaud Manus 		 */
1554207246e9SRenaud Manus 		if (v->gv_flags & GV_TOOFFLINE)
1555207246e9SRenaud Manus 			return (-1);
1556207246e9SRenaud Manus 
15577c478bd9Sstevel@tonic-gate 		switch (v->gv_state) {
15587c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_ONLINE:
15597c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_DEGRADED:
15607c478bd9Sstevel@tonic-gate 			return (1);
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_OFFLINE:
15637c478bd9Sstevel@tonic-gate 			if (!satbility)
15647c478bd9Sstevel@tonic-gate 				return (0);
15657c478bd9Sstevel@tonic-gate 			return (instance_satisfied(v, satbility) != -1 ?
15667c478bd9Sstevel@tonic-gate 			    0 : -1);
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_DISABLED:
15697c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_MAINT:
15707c478bd9Sstevel@tonic-gate 			return (-1);
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_UNINIT:
15737c478bd9Sstevel@tonic-gate 			return (0);
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate 		default:
15767c478bd9Sstevel@tonic-gate #ifndef NDEBUG
15777c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex state %d.\n",
15787c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__, v->gv_state);
15797c478bd9Sstevel@tonic-gate #endif
15807c478bd9Sstevel@tonic-gate 			abort();
15817c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
15827c478bd9Sstevel@tonic-gate 		}
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	case GVT_SVC:
15857c478bd9Sstevel@tonic-gate 		if (uu_list_numnodes(v->gv_dependencies) == 0)
15867c478bd9Sstevel@tonic-gate 			return (-1);
15877c478bd9Sstevel@tonic-gate 		return (require_any_satisfied(v, satbility));
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	case GVT_FILE:
15907c478bd9Sstevel@tonic-gate 		/* i.e., we assume files will not be automatically generated */
15917c478bd9Sstevel@tonic-gate 		return (file_ready(v) ? 1 : -1);
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	case GVT_GROUP:
15947c478bd9Sstevel@tonic-gate 		break;
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	default:
15977c478bd9Sstevel@tonic-gate #ifndef NDEBUG
15987c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unexpected node type %d.\n", __FILE__, __LINE__,
15997c478bd9Sstevel@tonic-gate 		    v->gv_type);
16007c478bd9Sstevel@tonic-gate #endif
16017c478bd9Sstevel@tonic-gate 		abort();
16027c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
16037c478bd9Sstevel@tonic-gate 	}
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	switch (v->gv_depgroup) {
16067c478bd9Sstevel@tonic-gate 	case DEPGRP_REQUIRE_ANY:
16077c478bd9Sstevel@tonic-gate 		return (require_any_satisfied(v, satbility));
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	case DEPGRP_REQUIRE_ALL:
16107c478bd9Sstevel@tonic-gate 		return (require_all_satisfied(v, satbility));
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	case DEPGRP_OPTIONAL_ALL:
16137c478bd9Sstevel@tonic-gate 		return (optional_all_satisfied(v, satbility));
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	case DEPGRP_EXCLUDE_ALL:
16167c478bd9Sstevel@tonic-gate 		return (exclude_all_satisfied(v, satbility));
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	default:
16197c478bd9Sstevel@tonic-gate #ifndef NDEBUG
16207c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown dependency grouping %d.\n", __FILE__,
16217c478bd9Sstevel@tonic-gate 		    __LINE__, v->gv_depgroup);
16227c478bd9Sstevel@tonic-gate #endif
16237c478bd9Sstevel@tonic-gate 		abort();
16247c478bd9Sstevel@tonic-gate 	}
16257c478bd9Sstevel@tonic-gate }
16267c478bd9Sstevel@tonic-gate 
162799b44c3bSlianep void
162899b44c3bSlianep graph_start_if_satisfied(graph_vertex_t *v)
16297c478bd9Sstevel@tonic-gate {
16307c478bd9Sstevel@tonic-gate 	if (v->gv_state == RESTARTER_STATE_OFFLINE &&
16317c478bd9Sstevel@tonic-gate 	    instance_satisfied(v, B_FALSE) == 1) {
16327c478bd9Sstevel@tonic-gate 		if (v->gv_start_f == NULL)
16337c478bd9Sstevel@tonic-gate 			vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
16347c478bd9Sstevel@tonic-gate 		else
16357c478bd9Sstevel@tonic-gate 			v->gv_start_f(v);
16367c478bd9Sstevel@tonic-gate 	}
16377c478bd9Sstevel@tonic-gate }
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate /*
16407c478bd9Sstevel@tonic-gate  * propagate_satbility()
16417c478bd9Sstevel@tonic-gate  *
16427c478bd9Sstevel@tonic-gate  * This function is used when the given vertex changes state in such a way that
16437c478bd9Sstevel@tonic-gate  * one of its dependents may become unsatisfiable.  This happens when an
16447c478bd9Sstevel@tonic-gate  * instance transitions between offline -> online, or from !running ->
16457c478bd9Sstevel@tonic-gate  * maintenance, as well as when an instance is removed from the graph.
16467c478bd9Sstevel@tonic-gate  *
1647cd3bce3eSlianep  * We have to walk all the dependents, since optional_all dependencies several
16487c478bd9Sstevel@tonic-gate  * levels up could become (un)satisfied, instead of unsatisfiable.  For example,
16497c478bd9Sstevel@tonic-gate  *
16507c478bd9Sstevel@tonic-gate  *	+-----+  optional_all  +-----+  require_all  +-----+
16517c478bd9Sstevel@tonic-gate  *	|  A  |--------------->|  B  |-------------->|  C  |
16527c478bd9Sstevel@tonic-gate  *	+-----+                +-----+               +-----+
16537c478bd9Sstevel@tonic-gate  *
16547c478bd9Sstevel@tonic-gate  *	                                        offline -> maintenance
16557c478bd9Sstevel@tonic-gate  *
16567c478bd9Sstevel@tonic-gate  * If C goes into maintenance, it's not enough simply to check B.  Because A has
16577c478bd9Sstevel@tonic-gate  * an optional dependency, what was previously an unsatisfiable situation is now
16587c478bd9Sstevel@tonic-gate  * satisfied (B will never come online, even though its state hasn't changed).
16597c478bd9Sstevel@tonic-gate  *
16607c478bd9Sstevel@tonic-gate  * Note that it's not necessary to continue examining dependents after reaching
16617c478bd9Sstevel@tonic-gate  * an optional_all dependency.  It's not possible for an optional_all dependency
16627c478bd9Sstevel@tonic-gate  * to change satisfiability without also coming online, in which case we get a
16637c478bd9Sstevel@tonic-gate  * start event and propagation continues naturally.  However, it does no harm to
16647c478bd9Sstevel@tonic-gate  * continue propagating satisfiability (as it is a relatively rare event), and
16657c478bd9Sstevel@tonic-gate  * keeps the walker code simple and generic.
16667c478bd9Sstevel@tonic-gate  */
16677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16687c478bd9Sstevel@tonic-gate static int
16697c478bd9Sstevel@tonic-gate satbility_cb(graph_vertex_t *v, void *arg)
16707c478bd9Sstevel@tonic-gate {
16717c478bd9Sstevel@tonic-gate 	if (v->gv_type == GVT_INST)
167299b44c3bSlianep 		graph_start_if_satisfied(v);
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
16757c478bd9Sstevel@tonic-gate }
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate static void
16787c478bd9Sstevel@tonic-gate propagate_satbility(graph_vertex_t *v)
16797c478bd9Sstevel@tonic-gate {
16807c478bd9Sstevel@tonic-gate 	graph_walk(v, WALK_DEPENDENTS, satbility_cb, NULL, NULL);
16817c478bd9Sstevel@tonic-gate }
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate static void propagate_stop(graph_vertex_t *, void *);
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate /* ARGSUSED */
16867c478bd9Sstevel@tonic-gate static void
16877c478bd9Sstevel@tonic-gate propagate_start(graph_vertex_t *v, void *arg)
16887c478bd9Sstevel@tonic-gate {
16897c478bd9Sstevel@tonic-gate 	switch (v->gv_type) {
16907c478bd9Sstevel@tonic-gate 	case GVT_INST:
169199b44c3bSlianep 		graph_start_if_satisfied(v);
16927c478bd9Sstevel@tonic-gate 		break;
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	case GVT_GROUP:
16957c478bd9Sstevel@tonic-gate 		if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
16967c478bd9Sstevel@tonic-gate 			graph_walk_dependents(v, propagate_stop,
16977c478bd9Sstevel@tonic-gate 			    (void *)RERR_RESTART);
16987c478bd9Sstevel@tonic-gate 			break;
16997c478bd9Sstevel@tonic-gate 		}
17007c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	case GVT_SVC:
17037c478bd9Sstevel@tonic-gate 		graph_walk_dependents(v, propagate_start, NULL);
17047c478bd9Sstevel@tonic-gate 		break;
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	case GVT_FILE:
17077c478bd9Sstevel@tonic-gate #ifndef NDEBUG
17087c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: propagate_start() encountered GVT_FILE.\n",
17097c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__);
17107c478bd9Sstevel@tonic-gate #endif
17117c478bd9Sstevel@tonic-gate 		abort();
17127c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	default:
17157c478bd9Sstevel@tonic-gate #ifndef NDEBUG
17167c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__,
17177c478bd9Sstevel@tonic-gate 		    v->gv_type);
17187c478bd9Sstevel@tonic-gate #endif
17197c478bd9Sstevel@tonic-gate 		abort();
17207c478bd9Sstevel@tonic-gate 	}
17217c478bd9Sstevel@tonic-gate }
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate static void
17247c478bd9Sstevel@tonic-gate propagate_stop(graph_vertex_t *v, void *arg)
17257c478bd9Sstevel@tonic-gate {
17267c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
17277c478bd9Sstevel@tonic-gate 	graph_vertex_t *svc;
17287c478bd9Sstevel@tonic-gate 	restarter_error_t err = (restarter_error_t)arg;
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	switch (v->gv_type) {
17317c478bd9Sstevel@tonic-gate 	case GVT_INST:
17327c478bd9Sstevel@tonic-gate 		/* Restarter */
17337c478bd9Sstevel@tonic-gate 		if (err > RERR_NONE && inst_running(v))
17347c478bd9Sstevel@tonic-gate 			vertex_send_event(v, RESTARTER_EVENT_TYPE_STOP);
17357c478bd9Sstevel@tonic-gate 		break;
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 	case GVT_SVC:
17387c478bd9Sstevel@tonic-gate 		graph_walk_dependents(v, propagate_stop, arg);
17397c478bd9Sstevel@tonic-gate 		break;
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 	case GVT_FILE:
17427c478bd9Sstevel@tonic-gate #ifndef NDEBUG
17437c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: propagate_stop() encountered GVT_FILE.\n",
17447c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__);
17457c478bd9Sstevel@tonic-gate #endif
17467c478bd9Sstevel@tonic-gate 		abort();
17477c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	case GVT_GROUP:
17507c478bd9Sstevel@tonic-gate 		if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
17517c478bd9Sstevel@tonic-gate 			graph_walk_dependents(v, propagate_start, NULL);
17527c478bd9Sstevel@tonic-gate 			break;
17537c478bd9Sstevel@tonic-gate 		}
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 		if (err == RERR_NONE || err > v->gv_restart)
17567c478bd9Sstevel@tonic-gate 			break;
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 		assert(uu_list_numnodes(v->gv_dependents) == 1);
17597c478bd9Sstevel@tonic-gate 		e = uu_list_first(v->gv_dependents);
17607c478bd9Sstevel@tonic-gate 		svc = e->ge_vertex;
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 		if (inst_running(svc))
17637c478bd9Sstevel@tonic-gate 			vertex_send_event(svc, RESTARTER_EVENT_TYPE_STOP);
17647c478bd9Sstevel@tonic-gate 		break;
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	default:
17677c478bd9Sstevel@tonic-gate #ifndef NDEBUG
17687c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__,
17697c478bd9Sstevel@tonic-gate 		    v->gv_type);
17707c478bd9Sstevel@tonic-gate #endif
17717c478bd9Sstevel@tonic-gate 		abort();
17727c478bd9Sstevel@tonic-gate 	}
17737c478bd9Sstevel@tonic-gate }
17747c478bd9Sstevel@tonic-gate 
1775845e9415SRenaud Manus void
1776aca380d7SRenaud Manus offline_vertex(graph_vertex_t *v)
1777aca380d7SRenaud Manus {
1778aca380d7SRenaud Manus 	scf_handle_t *h = libscf_handle_create_bound_loop();
1779aca380d7SRenaud Manus 	scf_instance_t *scf_inst = safe_scf_instance_create(h);
1780aca380d7SRenaud Manus 	scf_propertygroup_t *pg = safe_scf_pg_create(h);
1781aca380d7SRenaud Manus 	restarter_instance_state_t state, next_state;
1782aca380d7SRenaud Manus 	int r;
1783aca380d7SRenaud Manus 
1784aca380d7SRenaud Manus 	assert(v->gv_type == GVT_INST);
1785aca380d7SRenaud Manus 
1786aca380d7SRenaud Manus 	if (scf_inst == NULL)
1787aca380d7SRenaud Manus 		bad_error("safe_scf_instance_create", scf_error());
1788aca380d7SRenaud Manus 	if (pg == NULL)
1789aca380d7SRenaud Manus 		bad_error("safe_scf_pg_create", scf_error());
1790aca380d7SRenaud Manus 
1791aca380d7SRenaud Manus 	/* if the vertex is already going offline, return */
1792aca380d7SRenaud Manus rep_retry:
1793aca380d7SRenaud Manus 	if (scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, scf_inst, NULL,
1794aca380d7SRenaud Manus 	    NULL, SCF_DECODE_FMRI_EXACT) != 0) {
1795aca380d7SRenaud Manus 		switch (scf_error()) {
1796aca380d7SRenaud Manus 		case SCF_ERROR_CONNECTION_BROKEN:
1797aca380d7SRenaud Manus 			libscf_handle_rebind(h);
1798aca380d7SRenaud Manus 			goto rep_retry;
1799aca380d7SRenaud Manus 
1800aca380d7SRenaud Manus 		case SCF_ERROR_NOT_FOUND:
1801aca380d7SRenaud Manus 			scf_pg_destroy(pg);
1802aca380d7SRenaud Manus 			scf_instance_destroy(scf_inst);
1803aca380d7SRenaud Manus 			(void) scf_handle_unbind(h);
1804aca380d7SRenaud Manus 			scf_handle_destroy(h);
1805aca380d7SRenaud Manus 			return;
1806aca380d7SRenaud Manus 		}
1807aca380d7SRenaud Manus 		uu_die("Can't decode FMRI %s: %s\n", v->gv_name,
1808aca380d7SRenaud Manus 		    scf_strerror(scf_error()));
1809aca380d7SRenaud Manus 	}
1810aca380d7SRenaud Manus 
1811aca380d7SRenaud Manus 	r = scf_instance_get_pg(scf_inst, SCF_PG_RESTARTER, pg);
1812aca380d7SRenaud Manus 	if (r != 0) {
1813aca380d7SRenaud Manus 		switch (scf_error()) {
1814aca380d7SRenaud Manus 		case SCF_ERROR_CONNECTION_BROKEN:
1815aca380d7SRenaud Manus 			libscf_handle_rebind(h);
1816aca380d7SRenaud Manus 			goto rep_retry;
1817aca380d7SRenaud Manus 
1818aca380d7SRenaud Manus 		case SCF_ERROR_NOT_SET:
1819aca380d7SRenaud Manus 		case SCF_ERROR_NOT_FOUND:
1820aca380d7SRenaud Manus 			scf_pg_destroy(pg);
1821aca380d7SRenaud Manus 			scf_instance_destroy(scf_inst);
1822aca380d7SRenaud Manus 			(void) scf_handle_unbind(h);
1823aca380d7SRenaud Manus 			scf_handle_destroy(h);
1824aca380d7SRenaud Manus 			return;
1825aca380d7SRenaud Manus 
1826aca380d7SRenaud Manus 		default:
1827aca380d7SRenaud Manus 			bad_error("scf_instance_get_pg", scf_error());
1828aca380d7SRenaud Manus 		}
1829aca380d7SRenaud Manus 	} else {
1830aca380d7SRenaud Manus 		r = libscf_read_states(pg, &state, &next_state);
1831aca380d7SRenaud Manus 		if (r == 0 && (next_state == RESTARTER_STATE_OFFLINE ||
1832aca380d7SRenaud Manus 		    next_state == RESTARTER_STATE_DISABLED)) {
1833aca380d7SRenaud Manus 			log_framework(LOG_DEBUG,
1834aca380d7SRenaud Manus 			    "%s: instance is already going down.\n",
1835aca380d7SRenaud Manus 			    v->gv_name);
1836aca380d7SRenaud Manus 			scf_pg_destroy(pg);
1837aca380d7SRenaud Manus 			scf_instance_destroy(scf_inst);
1838aca380d7SRenaud Manus 			(void) scf_handle_unbind(h);
1839aca380d7SRenaud Manus 			scf_handle_destroy(h);
1840aca380d7SRenaud Manus 			return;
1841aca380d7SRenaud Manus 		}
1842aca380d7SRenaud Manus 	}
1843aca380d7SRenaud Manus 
1844aca380d7SRenaud Manus 	scf_pg_destroy(pg);
1845aca380d7SRenaud Manus 	scf_instance_destroy(scf_inst);
1846aca380d7SRenaud Manus 	(void) scf_handle_unbind(h);
1847aca380d7SRenaud Manus 	scf_handle_destroy(h);
1848accdf946SRenaud Manus 
1849accdf946SRenaud Manus 	vertex_send_event(v, RESTARTER_EVENT_TYPE_STOP);
1850aca380d7SRenaud Manus }
1851aca380d7SRenaud Manus 
18527c478bd9Sstevel@tonic-gate /*
18537c478bd9Sstevel@tonic-gate  * void graph_enable_by_vertex()
18547c478bd9Sstevel@tonic-gate  *   If admin is non-zero, this is an administrative request for change
18557c478bd9Sstevel@tonic-gate  *   of the enabled property.  Thus, send the ADMIN_DISABLE rather than
18567c478bd9Sstevel@tonic-gate  *   a plain DISABLE restarter event.
18577c478bd9Sstevel@tonic-gate  */
185899b44c3bSlianep void
18597c478bd9Sstevel@tonic-gate graph_enable_by_vertex(graph_vertex_t *vertex, int enable, int admin)
18607c478bd9Sstevel@tonic-gate {
1861aca380d7SRenaud Manus 	graph_vertex_t *v;
1862aca380d7SRenaud Manus 	int r;
1863aca380d7SRenaud Manus 
1864*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
18657c478bd9Sstevel@tonic-gate 	assert((vertex->gv_flags & GV_CONFIGURED));
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	vertex->gv_flags = (vertex->gv_flags & ~GV_ENABLED) |
18687c478bd9Sstevel@tonic-gate 	    (enable ? GV_ENABLED : 0);
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	if (enable) {
18717c478bd9Sstevel@tonic-gate 		if (vertex->gv_state != RESTARTER_STATE_OFFLINE &&
18727c478bd9Sstevel@tonic-gate 		    vertex->gv_state != RESTARTER_STATE_DEGRADED &&
1873aca380d7SRenaud Manus 		    vertex->gv_state != RESTARTER_STATE_ONLINE) {
1874aca380d7SRenaud Manus 			/*
1875aca380d7SRenaud Manus 			 * In case the vertex was notified to go down,
1876aca380d7SRenaud Manus 			 * but now can return online, clear the _TOOFFLINE
1877aca380d7SRenaud Manus 			 * and _TODISABLE flags.
1878aca380d7SRenaud Manus 			 */
1879aca380d7SRenaud Manus 			vertex->gv_flags &= ~GV_TOOFFLINE;
1880aca380d7SRenaud Manus 			vertex->gv_flags &= ~GV_TODISABLE;
1881aca380d7SRenaud Manus 
18827c478bd9Sstevel@tonic-gate 			vertex_send_event(vertex, RESTARTER_EVENT_TYPE_ENABLE);
18837c478bd9Sstevel@tonic-gate 		}
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 		/*
18867c478bd9Sstevel@tonic-gate 		 * Wait for state update from restarter before sending _START or
18877c478bd9Sstevel@tonic-gate 		 * _STOP.
18887c478bd9Sstevel@tonic-gate 		 */
1889aca380d7SRenaud Manus 
1890aca380d7SRenaud Manus 		return;
1891aca380d7SRenaud Manus 	}
1892aca380d7SRenaud Manus 
1893aca380d7SRenaud Manus 	if (vertex->gv_state == RESTARTER_STATE_DISABLED)
1894aca380d7SRenaud Manus 		return;
1895aca380d7SRenaud Manus 
1896aca380d7SRenaud Manus 	if (!admin) {
1897aca380d7SRenaud Manus 		vertex_send_event(vertex, RESTARTER_EVENT_TYPE_DISABLE);
1898aca380d7SRenaud Manus 
1899aca380d7SRenaud Manus 		/*
1900aca380d7SRenaud Manus 		 * Wait for state update from restarter before sending _START or
1901aca380d7SRenaud Manus 		 * _STOP.
1902aca380d7SRenaud Manus 		 */
1903aca380d7SRenaud Manus 
1904aca380d7SRenaud Manus 		return;
1905aca380d7SRenaud Manus 	}
1906aca380d7SRenaud Manus 
1907aca380d7SRenaud Manus 	/*
1908aca380d7SRenaud Manus 	 * If it is a DISABLE event requested by the administrator then we are
1909aca380d7SRenaud Manus 	 * offlining the dependents first.
1910aca380d7SRenaud Manus 	 */
1911aca380d7SRenaud Manus 
1912aca380d7SRenaud Manus 	/*
1913aca380d7SRenaud Manus 	 * Set GV_TOOFFLINE for the services we are offlining. We cannot
1914aca380d7SRenaud Manus 	 * clear the GV_TOOFFLINE bits from all the services because
1915aca380d7SRenaud Manus 	 * other DISABLE events might be handled at the same time.
1916aca380d7SRenaud Manus 	 */
1917aca380d7SRenaud Manus 	vertex->gv_flags |= GV_TOOFFLINE;
1918aca380d7SRenaud Manus 
1919aca380d7SRenaud Manus 	/* remember which vertex to disable... */
1920aca380d7SRenaud Manus 	vertex->gv_flags |= GV_TODISABLE;
1921aca380d7SRenaud Manus 
1922207246e9SRenaud Manus 	log_framework(LOG_DEBUG, "Marking in-subtree vertices before "
1923207246e9SRenaud Manus 	    "disabling %s.\n", vertex->gv_name);
1924207246e9SRenaud Manus 
1925aca380d7SRenaud Manus 	/* set GV_TOOFFLINE for its dependents */
1926aca380d7SRenaud Manus 	r = uu_list_walk(vertex->gv_dependents, (uu_walk_fn_t *)mark_subtree,
1927aca380d7SRenaud Manus 	    NULL, 0);
1928aca380d7SRenaud Manus 	assert(r == 0);
1929aca380d7SRenaud Manus 
1930aca380d7SRenaud Manus 	/* disable the instance now if there is nothing else to offline */
1931aca380d7SRenaud Manus 	if (insubtree_dependents_down(vertex) == B_TRUE) {
1932aca380d7SRenaud Manus 		vertex_send_event(vertex, RESTARTER_EVENT_TYPE_ADMIN_DISABLE);
1933aca380d7SRenaud Manus 		return;
1934aca380d7SRenaud Manus 	}
1935aca380d7SRenaud Manus 
1936aca380d7SRenaud Manus 	/*
1937aca380d7SRenaud Manus 	 * This loop is similar to the one used for the graph reversal shutdown
1938aca380d7SRenaud Manus 	 * and could be improved in term of performance for the subtree reversal
1939aca380d7SRenaud Manus 	 * disable case.
1940aca380d7SRenaud Manus 	 */
1941aca380d7SRenaud Manus 	for (v = uu_list_first(dgraph); v != NULL;
1942aca380d7SRenaud Manus 	    v = uu_list_next(dgraph, v)) {
1943aca380d7SRenaud Manus 		/* skip the vertex we are disabling for now */
1944aca380d7SRenaud Manus 		if (v == vertex)
1945aca380d7SRenaud Manus 			continue;
1946aca380d7SRenaud Manus 
1947aca380d7SRenaud Manus 		if (v->gv_type != GVT_INST ||
1948aca380d7SRenaud Manus 		    (v->gv_flags & GV_CONFIGURED) == 0 ||
1949aca380d7SRenaud Manus 		    (v->gv_flags & GV_ENABLED) == 0 ||
1950aca380d7SRenaud Manus 		    (v->gv_flags & GV_TOOFFLINE) == 0)
1951aca380d7SRenaud Manus 			continue;
1952aca380d7SRenaud Manus 
1953aca380d7SRenaud Manus 		if ((v->gv_state != RESTARTER_STATE_ONLINE) &&
1954aca380d7SRenaud Manus 		    (v->gv_state != RESTARTER_STATE_DEGRADED)) {
1955aca380d7SRenaud Manus 			/* continue if there is nothing to offline */
1956aca380d7SRenaud Manus 			continue;
1957aca380d7SRenaud Manus 		}
1958aca380d7SRenaud Manus 
1959aca380d7SRenaud Manus 		/*
1960aca380d7SRenaud Manus 		 * Instances which are up need to come down before we're
1961aca380d7SRenaud Manus 		 * done, but we can only offline the leaves here. An
1962aca380d7SRenaud Manus 		 * instance is a leaf when all its dependents are down.
1963aca380d7SRenaud Manus 		 */
1964207246e9SRenaud Manus 		if (insubtree_dependents_down(v) == B_TRUE) {
1965207246e9SRenaud Manus 			log_framework(LOG_DEBUG, "Offlining in-subtree "
1966207246e9SRenaud Manus 			    "instance %s for %s.\n",
1967207246e9SRenaud Manus 			    v->gv_name, vertex->gv_name);
1968aca380d7SRenaud Manus 			offline_vertex(v);
1969aca380d7SRenaud Manus 		}
19707c478bd9Sstevel@tonic-gate 	}
1971207246e9SRenaud Manus }
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate static int configure_vertex(graph_vertex_t *, scf_instance_t *);
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate /*
19767c478bd9Sstevel@tonic-gate  * Set the restarter for v to fmri_arg.  That is, make sure a vertex for
19777c478bd9Sstevel@tonic-gate  * fmri_arg exists, make v depend on it, and send _ADD_INSTANCE for v.  If
19787c478bd9Sstevel@tonic-gate  * v is already configured and fmri_arg indicates the current restarter, do
19797c478bd9Sstevel@tonic-gate  * nothing.  If v is configured and fmri_arg is a new restarter, delete v's
19807c478bd9Sstevel@tonic-gate  * dependency on the restarter, send _REMOVE_INSTANCE for v, and set the new
19817c478bd9Sstevel@tonic-gate  * restarter.  Returns 0 on success, EINVAL if the FMRI is invalid,
19827c478bd9Sstevel@tonic-gate  * ECONNABORTED if the repository connection is broken, and ELOOP
19837c478bd9Sstevel@tonic-gate  * if the dependency would create a cycle.  In the last case, *pathp will
19847c478bd9Sstevel@tonic-gate  * point to a -1-terminated array of ids which compose the path from v to
19857c478bd9Sstevel@tonic-gate  * restarter_fmri.
19867c478bd9Sstevel@tonic-gate  */
19877c478bd9Sstevel@tonic-gate int
19887c478bd9Sstevel@tonic-gate graph_change_restarter(graph_vertex_t *v, const char *fmri_arg, scf_handle_t *h,
19897c478bd9Sstevel@tonic-gate     int **pathp)
19907c478bd9Sstevel@tonic-gate {
19917c478bd9Sstevel@tonic-gate 	char *restarter_fmri = NULL;
19927c478bd9Sstevel@tonic-gate 	graph_vertex_t *rv;
19937c478bd9Sstevel@tonic-gate 	int err;
19947c478bd9Sstevel@tonic-gate 	int id;
19957c478bd9Sstevel@tonic-gate 
1996*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 	if (fmri_arg[0] != '\0') {
19997c478bd9Sstevel@tonic-gate 		err = fmri_canonify(fmri_arg, &restarter_fmri, B_TRUE);
20007c478bd9Sstevel@tonic-gate 		if (err != 0) {
20017c478bd9Sstevel@tonic-gate 			assert(err == EINVAL);
20027c478bd9Sstevel@tonic-gate 			return (err);
20037c478bd9Sstevel@tonic-gate 		}
20047c478bd9Sstevel@tonic-gate 	}
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	if (restarter_fmri == NULL ||
20077c478bd9Sstevel@tonic-gate 	    strcmp(restarter_fmri, SCF_SERVICE_STARTD) == 0) {
20087c478bd9Sstevel@tonic-gate 		if (v->gv_flags & GV_CONFIGURED) {
20097c478bd9Sstevel@tonic-gate 			if (v->gv_restarter_id == -1) {
20107c478bd9Sstevel@tonic-gate 				if (restarter_fmri != NULL)
20117c478bd9Sstevel@tonic-gate 					startd_free(restarter_fmri,
20127c478bd9Sstevel@tonic-gate 					    max_scf_fmri_size);
20137c478bd9Sstevel@tonic-gate 				return (0);
20147c478bd9Sstevel@tonic-gate 			}
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 			graph_unset_restarter(v);
20177c478bd9Sstevel@tonic-gate 		}
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 		/* Master restarter, nothing to do. */
20207c478bd9Sstevel@tonic-gate 		v->gv_restarter_id = -1;
20217c478bd9Sstevel@tonic-gate 		v->gv_restarter_channel = NULL;
20227c478bd9Sstevel@tonic-gate 		vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE);
20237c478bd9Sstevel@tonic-gate 		return (0);
20247c478bd9Sstevel@tonic-gate 	}
20257c478bd9Sstevel@tonic-gate 
20267c478bd9Sstevel@tonic-gate 	if (v->gv_flags & GV_CONFIGURED) {
20277c478bd9Sstevel@tonic-gate 		id = dict_lookup_byname(restarter_fmri);
20287c478bd9Sstevel@tonic-gate 		if (id != -1 && v->gv_restarter_id == id) {
20297c478bd9Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_fmri_size);
20307c478bd9Sstevel@tonic-gate 			return (0);
20317c478bd9Sstevel@tonic-gate 		}
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 		graph_unset_restarter(v);
20347c478bd9Sstevel@tonic-gate 	}
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	err = graph_insert_vertex_unconfigured(restarter_fmri, GVT_INST, 0,
20377c478bd9Sstevel@tonic-gate 	    RERR_NONE, &rv);
20387c478bd9Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_fmri_size);
20397c478bd9Sstevel@tonic-gate 	assert(err == 0 || err == EEXIST);
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	if (rv->gv_delegate_initialized == 0) {
204213d8aaa1SSean Wilcox 		if ((rv->gv_delegate_channel = restarter_protocol_init_delegate(
204313d8aaa1SSean Wilcox 		    rv->gv_name)) == NULL)
204413d8aaa1SSean Wilcox 			return (EINVAL);
20457c478bd9Sstevel@tonic-gate 		rv->gv_delegate_initialized = 1;
20467c478bd9Sstevel@tonic-gate 	}
20477c478bd9Sstevel@tonic-gate 	v->gv_restarter_id = rv->gv_id;
20487c478bd9Sstevel@tonic-gate 	v->gv_restarter_channel = rv->gv_delegate_channel;
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 	err = graph_insert_dependency(v, rv, pathp);
20517c478bd9Sstevel@tonic-gate 	if (err != 0) {
20527c478bd9Sstevel@tonic-gate 		assert(err == ELOOP);
20537c478bd9Sstevel@tonic-gate 		return (ELOOP);
20547c478bd9Sstevel@tonic-gate 	}
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE);
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	if (!(rv->gv_flags & GV_CONFIGURED)) {
20597c478bd9Sstevel@tonic-gate 		scf_instance_t *inst;
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 		err = libscf_fmri_get_instance(h, rv->gv_name, &inst);
20627c478bd9Sstevel@tonic-gate 		switch (err) {
20637c478bd9Sstevel@tonic-gate 		case 0:
20647c478bd9Sstevel@tonic-gate 			err = configure_vertex(rv, inst);
20657c478bd9Sstevel@tonic-gate 			scf_instance_destroy(inst);
20667c478bd9Sstevel@tonic-gate 			switch (err) {
20677c478bd9Sstevel@tonic-gate 			case 0:
20687c478bd9Sstevel@tonic-gate 			case ECANCELED:
20697c478bd9Sstevel@tonic-gate 				break;
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
20727c478bd9Sstevel@tonic-gate 				return (ECONNABORTED);
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 			default:
20757c478bd9Sstevel@tonic-gate 				bad_error("configure_vertex", err);
20767c478bd9Sstevel@tonic-gate 			}
20777c478bd9Sstevel@tonic-gate 			break;
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
20807c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 		case ENOENT:
20837c478bd9Sstevel@tonic-gate 			break;
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 		case ENOTSUP:
20867c478bd9Sstevel@tonic-gate 			/*
20877c478bd9Sstevel@tonic-gate 			 * The fmri doesn't specify an instance - translate
20887c478bd9Sstevel@tonic-gate 			 * to EINVAL.
20897c478bd9Sstevel@tonic-gate 			 */
20907c478bd9Sstevel@tonic-gate 			return (EINVAL);
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 		case EINVAL:
20937c478bd9Sstevel@tonic-gate 		default:
20947c478bd9Sstevel@tonic-gate 			bad_error("libscf_fmri_get_instance", err);
20957c478bd9Sstevel@tonic-gate 		}
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	return (0);
20997c478bd9Sstevel@tonic-gate }
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate /*
21037c478bd9Sstevel@tonic-gate  * Add all of the instances of the service named by fmri to the graph.
21047c478bd9Sstevel@tonic-gate  * Returns
21057c478bd9Sstevel@tonic-gate  *   0 - success
21067c478bd9Sstevel@tonic-gate  *   ENOENT - service indicated by fmri does not exist
21077c478bd9Sstevel@tonic-gate  *
21087c478bd9Sstevel@tonic-gate  * In both cases *reboundp will be B_TRUE if the handle was rebound, or B_FALSE
21097c478bd9Sstevel@tonic-gate  * otherwise.
21107c478bd9Sstevel@tonic-gate  */
21117c478bd9Sstevel@tonic-gate static int
21127c478bd9Sstevel@tonic-gate add_service(const char *fmri, scf_handle_t *h, boolean_t *reboundp)
21137c478bd9Sstevel@tonic-gate {
21147c478bd9Sstevel@tonic-gate 	scf_service_t *svc;
21157c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
21167c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
21177c478bd9Sstevel@tonic-gate 	char *inst_fmri;
21187c478bd9Sstevel@tonic-gate 	int ret, r;
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 	*reboundp = B_FALSE;
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 	svc = safe_scf_service_create(h);
21237c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
21247c478bd9Sstevel@tonic-gate 	iter = safe_scf_iter_create(h);
21257c478bd9Sstevel@tonic-gate 	inst_fmri = startd_alloc(max_scf_fmri_size);
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate rebound:
21287c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
21297c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) != 0) {
21307c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
21317c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
21327c478bd9Sstevel@tonic-gate 		default:
21337c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
21347c478bd9Sstevel@tonic-gate 			*reboundp = B_TRUE;
21357c478bd9Sstevel@tonic-gate 			goto rebound;
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
21387c478bd9Sstevel@tonic-gate 			ret = ENOENT;
21397c478bd9Sstevel@tonic-gate 			goto out;
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
21427c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
21437c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
21447c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
21457c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
21467c478bd9Sstevel@tonic-gate 		}
21477c478bd9Sstevel@tonic-gate 	}
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 	if (scf_iter_service_instances(iter, svc) != 0) {
21507c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
21517c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
21527c478bd9Sstevel@tonic-gate 		default:
21537c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
21547c478bd9Sstevel@tonic-gate 			*reboundp = B_TRUE;
21557c478bd9Sstevel@tonic-gate 			goto rebound;
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
21587c478bd9Sstevel@tonic-gate 			ret = ENOENT;
21597c478bd9Sstevel@tonic-gate 			goto out;
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
21627c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
21637c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
21643eae19d9Swesolows 			bad_error("scf_iter_service_instances", scf_error());
21657c478bd9Sstevel@tonic-gate 		}
21667c478bd9Sstevel@tonic-gate 	}
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate 	for (;;) {
21697c478bd9Sstevel@tonic-gate 		r = scf_iter_next_instance(iter, inst);
21707c478bd9Sstevel@tonic-gate 		if (r == 0)
21717c478bd9Sstevel@tonic-gate 			break;
21727c478bd9Sstevel@tonic-gate 		if (r != 1) {
21737c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
21747c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
21757c478bd9Sstevel@tonic-gate 			default:
21767c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
21777c478bd9Sstevel@tonic-gate 				*reboundp = B_TRUE;
21787c478bd9Sstevel@tonic-gate 				goto rebound;
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
21817c478bd9Sstevel@tonic-gate 				ret = ENOENT;
21827c478bd9Sstevel@tonic-gate 				goto out;
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
21857c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
21867c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
21877c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
21887c478bd9Sstevel@tonic-gate 				bad_error("scf_iter_next_instance",
21897c478bd9Sstevel@tonic-gate 				    scf_error());
21907c478bd9Sstevel@tonic-gate 			}
21917c478bd9Sstevel@tonic-gate 		}
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate 		if (scf_instance_to_fmri(inst, inst_fmri, max_scf_fmri_size) <
21947c478bd9Sstevel@tonic-gate 		    0) {
21957c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
21967c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
21977c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
21987c478bd9Sstevel@tonic-gate 				*reboundp = B_TRUE;
21997c478bd9Sstevel@tonic-gate 				goto rebound;
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
22027c478bd9Sstevel@tonic-gate 				continue;
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
22057c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
22067c478bd9Sstevel@tonic-gate 				bad_error("scf_instance_to_fmri", scf_error());
22077c478bd9Sstevel@tonic-gate 			}
22087c478bd9Sstevel@tonic-gate 		}
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 		r = dgraph_add_instance(inst_fmri, inst, B_FALSE);
22117c478bd9Sstevel@tonic-gate 		switch (r) {
22127c478bd9Sstevel@tonic-gate 		case 0:
22137c478bd9Sstevel@tonic-gate 		case ECANCELED:
22147c478bd9Sstevel@tonic-gate 			break;
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 		case EEXIST:
22177c478bd9Sstevel@tonic-gate 			continue;
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
22207c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
22217c478bd9Sstevel@tonic-gate 			*reboundp = B_TRUE;
22227c478bd9Sstevel@tonic-gate 			goto rebound;
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 		case EINVAL:
22257c478bd9Sstevel@tonic-gate 		default:
22267c478bd9Sstevel@tonic-gate 			bad_error("dgraph_add_instance", r);
22277c478bd9Sstevel@tonic-gate 		}
22287c478bd9Sstevel@tonic-gate 	}
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 	ret = 0;
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate out:
22337c478bd9Sstevel@tonic-gate 	startd_free(inst_fmri, max_scf_fmri_size);
22347c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
22357c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
22367c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
22377c478bd9Sstevel@tonic-gate 	return (ret);
22387c478bd9Sstevel@tonic-gate }
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate struct depfmri_info {
22417c478bd9Sstevel@tonic-gate 	graph_vertex_t	*v;		/* GVT_GROUP vertex */
22427c478bd9Sstevel@tonic-gate 	gv_type_t	type;		/* type of dependency */
22437c478bd9Sstevel@tonic-gate 	const char	*inst_fmri;	/* FMRI of parental GVT_INST vert. */
22447c478bd9Sstevel@tonic-gate 	const char	*pg_name;	/* Name of dependency pg */
22457c478bd9Sstevel@tonic-gate 	scf_handle_t	*h;
22467c478bd9Sstevel@tonic-gate 	int		err;		/* return error code */
22477c478bd9Sstevel@tonic-gate 	int		**pathp;	/* return circular dependency path */
22487c478bd9Sstevel@tonic-gate };
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate /*
22517c478bd9Sstevel@tonic-gate  * Find or create a vertex for fmri and make info->v depend on it.
22527c478bd9Sstevel@tonic-gate  * Returns
22537c478bd9Sstevel@tonic-gate  *   0 - success
22547c478bd9Sstevel@tonic-gate  *   nonzero - failure
22557c478bd9Sstevel@tonic-gate  *
22567c478bd9Sstevel@tonic-gate  * On failure, sets info->err to
22577c478bd9Sstevel@tonic-gate  *   EINVAL - fmri is invalid
22587c478bd9Sstevel@tonic-gate  *	      fmri does not match info->type
22597c478bd9Sstevel@tonic-gate  *   ELOOP - Adding the dependency creates a circular dependency.  *info->pathp
22607c478bd9Sstevel@tonic-gate  *	     will point to an array of the ids of the members of the cycle.
22617c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection was broken
22627c478bd9Sstevel@tonic-gate  *   ECONNRESET - succeeded, but repository connection was reset
22637c478bd9Sstevel@tonic-gate  */
22647c478bd9Sstevel@tonic-gate static int
22657c478bd9Sstevel@tonic-gate process_dependency_fmri(const char *fmri, struct depfmri_info *info)
22667c478bd9Sstevel@tonic-gate {
22677c478bd9Sstevel@tonic-gate 	int err;
22687c478bd9Sstevel@tonic-gate 	graph_vertex_t *depgroup_v, *v;
22697c478bd9Sstevel@tonic-gate 	char *fmri_copy, *cfmri;
22707c478bd9Sstevel@tonic-gate 	size_t fmri_copy_sz;
22717c478bd9Sstevel@tonic-gate 	const char *scope, *service, *instance, *pg;
22727c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
22737c478bd9Sstevel@tonic-gate 	boolean_t rebound;
22747c478bd9Sstevel@tonic-gate 
2275*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 	/* Get or create vertex for FMRI */
22787c478bd9Sstevel@tonic-gate 	depgroup_v = info->v;
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	if (strncmp(fmri, "file:", sizeof ("file:") - 1) == 0) {
22817c478bd9Sstevel@tonic-gate 		if (info->type != GVT_FILE) {
22827c478bd9Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
22837c478bd9Sstevel@tonic-gate 			    "FMRI \"%s\" is not allowed for the \"%s\" "
22847c478bd9Sstevel@tonic-gate 			    "dependency's type of instance %s.\n", fmri,
22857c478bd9Sstevel@tonic-gate 			    info->pg_name, info->inst_fmri);
22867c478bd9Sstevel@tonic-gate 			return (info->err = EINVAL);
22877c478bd9Sstevel@tonic-gate 		}
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(fmri, info->type, 0,
22907c478bd9Sstevel@tonic-gate 		    RERR_NONE, &v);
22917c478bd9Sstevel@tonic-gate 		switch (err) {
22927c478bd9Sstevel@tonic-gate 		case 0:
22937c478bd9Sstevel@tonic-gate 			break;
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 		case EEXIST:
22967c478bd9Sstevel@tonic-gate 			assert(v->gv_type == GVT_FILE);
22977c478bd9Sstevel@tonic-gate 			break;
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate 		case EINVAL:		/* prevented above */
23007c478bd9Sstevel@tonic-gate 		default:
23017c478bd9Sstevel@tonic-gate 			bad_error("graph_insert_vertex_unconfigured", err);
23027c478bd9Sstevel@tonic-gate 		}
23037c478bd9Sstevel@tonic-gate 	} else {
23047c478bd9Sstevel@tonic-gate 		if (info->type != GVT_INST) {
23057c478bd9Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
23067c478bd9Sstevel@tonic-gate 			    "FMRI \"%s\" is not allowed for the \"%s\" "
23077c478bd9Sstevel@tonic-gate 			    "dependency's type of instance %s.\n", fmri,
23087c478bd9Sstevel@tonic-gate 			    info->pg_name, info->inst_fmri);
23097c478bd9Sstevel@tonic-gate 			return (info->err = EINVAL);
23107c478bd9Sstevel@tonic-gate 		}
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 		/*
23137c478bd9Sstevel@tonic-gate 		 * We must canonify fmri & add a vertex for it.
23147c478bd9Sstevel@tonic-gate 		 */
23157c478bd9Sstevel@tonic-gate 		fmri_copy_sz = strlen(fmri) + 1;
23167c478bd9Sstevel@tonic-gate 		fmri_copy = startd_alloc(fmri_copy_sz);
23177c478bd9Sstevel@tonic-gate 		(void) strcpy(fmri_copy, fmri);
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 		/* Determine if the FMRI is a property group or instance */
23207c478bd9Sstevel@tonic-gate 		if (scf_parse_svc_fmri(fmri_copy, &scope, &service,
23217c478bd9Sstevel@tonic-gate 		    &instance, &pg, NULL) != 0) {
23227c478bd9Sstevel@tonic-gate 			startd_free(fmri_copy, fmri_copy_sz);
23237c478bd9Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
23247c478bd9Sstevel@tonic-gate 			    "Dependency \"%s\" of %s has invalid FMRI "
23257c478bd9Sstevel@tonic-gate 			    "\"%s\".\n", info->pg_name, info->inst_fmri,
23267c478bd9Sstevel@tonic-gate 			    fmri);
23277c478bd9Sstevel@tonic-gate 			return (info->err = EINVAL);
23287c478bd9Sstevel@tonic-gate 		}
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 		if (service == NULL || pg != NULL) {
23317c478bd9Sstevel@tonic-gate 			startd_free(fmri_copy, fmri_copy_sz);
23327c478bd9Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
23337c478bd9Sstevel@tonic-gate 			    "Dependency \"%s\" of %s does not designate a "
23347c478bd9Sstevel@tonic-gate 			    "service or instance.\n", info->pg_name,
23357c478bd9Sstevel@tonic-gate 			    info->inst_fmri);
23367c478bd9Sstevel@tonic-gate 			return (info->err = EINVAL);
23377c478bd9Sstevel@tonic-gate 		}
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 		if (scope == NULL || strcmp(scope, SCF_SCOPE_LOCAL) == 0) {
23407c478bd9Sstevel@tonic-gate 			cfmri = uu_msprintf("svc:/%s%s%s",
23417c478bd9Sstevel@tonic-gate 			    service, instance ? ":" : "", instance ? instance :
23427c478bd9Sstevel@tonic-gate 			    "");
23437c478bd9Sstevel@tonic-gate 		} else {
23447c478bd9Sstevel@tonic-gate 			cfmri = uu_msprintf("svc://%s/%s%s%s",
23457c478bd9Sstevel@tonic-gate 			    scope, service, instance ? ":" : "", instance ?
23467c478bd9Sstevel@tonic-gate 			    instance : "");
23477c478bd9Sstevel@tonic-gate 		}
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 		startd_free(fmri_copy, fmri_copy_sz);
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(cfmri, instance ?
23527c478bd9Sstevel@tonic-gate 		    GVT_INST : GVT_SVC, instance ? 0 : DEPGRP_REQUIRE_ANY,
23537c478bd9Sstevel@tonic-gate 		    RERR_NONE, &v);
23547c478bd9Sstevel@tonic-gate 		uu_free(cfmri);
23557c478bd9Sstevel@tonic-gate 		switch (err) {
23567c478bd9Sstevel@tonic-gate 		case 0:
23577c478bd9Sstevel@tonic-gate 			break;
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 		case EEXIST:
23607c478bd9Sstevel@tonic-gate 			/* Verify v. */
23617c478bd9Sstevel@tonic-gate 			if (instance != NULL)
23627c478bd9Sstevel@tonic-gate 				assert(v->gv_type == GVT_INST);
23637c478bd9Sstevel@tonic-gate 			else
23647c478bd9Sstevel@tonic-gate 				assert(v->gv_type == GVT_SVC);
23657c478bd9Sstevel@tonic-gate 			break;
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 		default:
23687c478bd9Sstevel@tonic-gate 			bad_error("graph_insert_vertex_unconfigured", err);
23697c478bd9Sstevel@tonic-gate 		}
23707c478bd9Sstevel@tonic-gate 	}
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate 	/* Add dependency from depgroup_v to new vertex */
23737c478bd9Sstevel@tonic-gate 	info->err = graph_insert_dependency(depgroup_v, v, info->pathp);
23747c478bd9Sstevel@tonic-gate 	switch (info->err) {
23757c478bd9Sstevel@tonic-gate 	case 0:
23767c478bd9Sstevel@tonic-gate 		break;
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 	case ELOOP:
23797c478bd9Sstevel@tonic-gate 		return (ELOOP);
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 	default:
23827c478bd9Sstevel@tonic-gate 		bad_error("graph_insert_dependency", info->err);
23837c478bd9Sstevel@tonic-gate 	}
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 	/* This must be after we insert the dependency, to avoid looping. */
23867c478bd9Sstevel@tonic-gate 	switch (v->gv_type) {
23877c478bd9Sstevel@tonic-gate 	case GVT_INST:
23887c478bd9Sstevel@tonic-gate 		if ((v->gv_flags & GV_CONFIGURED) != 0)
23897c478bd9Sstevel@tonic-gate 			break;
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 		inst = safe_scf_instance_create(info->h);
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate 		rebound = B_FALSE;
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate rebound:
23967c478bd9Sstevel@tonic-gate 		err = libscf_lookup_instance(v->gv_name, inst);
23977c478bd9Sstevel@tonic-gate 		switch (err) {
23987c478bd9Sstevel@tonic-gate 		case 0:
23997c478bd9Sstevel@tonic-gate 			err = configure_vertex(v, inst);
24007c478bd9Sstevel@tonic-gate 			switch (err) {
24017c478bd9Sstevel@tonic-gate 			case 0:
24027c478bd9Sstevel@tonic-gate 			case ECANCELED:
24037c478bd9Sstevel@tonic-gate 				break;
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
24067c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(info->h);
24077c478bd9Sstevel@tonic-gate 				rebound = B_TRUE;
24087c478bd9Sstevel@tonic-gate 				goto rebound;
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 			default:
24117c478bd9Sstevel@tonic-gate 				bad_error("configure_vertex", err);
24127c478bd9Sstevel@tonic-gate 			}
24137c478bd9Sstevel@tonic-gate 			break;
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 		case ENOENT:
24167c478bd9Sstevel@tonic-gate 			break;
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
24197c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(info->h);
24207c478bd9Sstevel@tonic-gate 			rebound = B_TRUE;
24217c478bd9Sstevel@tonic-gate 			goto rebound;
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 		case EINVAL:
24247c478bd9Sstevel@tonic-gate 		case ENOTSUP:
24257c478bd9Sstevel@tonic-gate 		default:
24267c478bd9Sstevel@tonic-gate 			bad_error("libscf_fmri_get_instance", err);
24277c478bd9Sstevel@tonic-gate 		}
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 		if (rebound)
24327c478bd9Sstevel@tonic-gate 			return (info->err = ECONNRESET);
24337c478bd9Sstevel@tonic-gate 		break;
24347c478bd9Sstevel@tonic-gate 
24357c478bd9Sstevel@tonic-gate 	case GVT_SVC:
24367c478bd9Sstevel@tonic-gate 		(void) add_service(v->gv_name, info->h, &rebound);
24377c478bd9Sstevel@tonic-gate 		if (rebound)
24387c478bd9Sstevel@tonic-gate 			return (info->err = ECONNRESET);
24397c478bd9Sstevel@tonic-gate 	}
24407c478bd9Sstevel@tonic-gate 
24417c478bd9Sstevel@tonic-gate 	return (0);
24427c478bd9Sstevel@tonic-gate }
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate struct deppg_info {
24457c478bd9Sstevel@tonic-gate 	graph_vertex_t	*v;		/* GVT_INST vertex */
24467c478bd9Sstevel@tonic-gate 	int		err;		/* return error */
24477c478bd9Sstevel@tonic-gate 	int		**pathp;	/* return circular dependency path */
24487c478bd9Sstevel@tonic-gate };
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate /*
24517c478bd9Sstevel@tonic-gate  * Make info->v depend on a new GVT_GROUP node for this property group,
24527c478bd9Sstevel@tonic-gate  * and then call process_dependency_fmri() for the values of the entity
24537c478bd9Sstevel@tonic-gate  * property.  Return 0 on success, or if something goes wrong return nonzero
24547c478bd9Sstevel@tonic-gate  * and set info->err to ECONNABORTED, EINVAL, or the error code returned by
24557c478bd9Sstevel@tonic-gate  * process_dependency_fmri().
24567c478bd9Sstevel@tonic-gate  */
24577c478bd9Sstevel@tonic-gate static int
24587c478bd9Sstevel@tonic-gate process_dependency_pg(scf_propertygroup_t *pg, struct deppg_info *info)
24597c478bd9Sstevel@tonic-gate {
24607c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
24617c478bd9Sstevel@tonic-gate 	depgroup_type_t deptype;
2462af3bcd91Srm88369 	restarter_error_t rerr;
24637c478bd9Sstevel@tonic-gate 	struct depfmri_info linfo;
24647c478bd9Sstevel@tonic-gate 	char *fmri, *pg_name;
24657c478bd9Sstevel@tonic-gate 	size_t fmri_sz;
24667c478bd9Sstevel@tonic-gate 	graph_vertex_t *depgrp;
24677c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
24687c478bd9Sstevel@tonic-gate 	int err;
24697c478bd9Sstevel@tonic-gate 	int empty;
24707c478bd9Sstevel@tonic-gate 	scf_error_t scferr;
24717c478bd9Sstevel@tonic-gate 	ssize_t len;
24727c478bd9Sstevel@tonic-gate 
2473*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 	h = scf_pg_handle(pg);
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 	pg_name = startd_alloc(max_scf_name_size);
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 	len = scf_pg_get_name(pg, pg_name, max_scf_name_size);
24807c478bd9Sstevel@tonic-gate 	if (len < 0) {
24817c478bd9Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
24827c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
24837c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
24847c478bd9Sstevel@tonic-gate 		default:
24857c478bd9Sstevel@tonic-gate 			return (info->err = ECONNABORTED);
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
24887c478bd9Sstevel@tonic-gate 			return (info->err = 0);
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
24917c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_name", scf_error());
24927c478bd9Sstevel@tonic-gate 		}
24937c478bd9Sstevel@tonic-gate 	}
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 	/*
24967c478bd9Sstevel@tonic-gate 	 * Skip over empty dependency groups.  Since dependency property
24977c478bd9Sstevel@tonic-gate 	 * groups are updated atomically, they are either empty or
24987c478bd9Sstevel@tonic-gate 	 * fully populated.
24997c478bd9Sstevel@tonic-gate 	 */
25007c478bd9Sstevel@tonic-gate 	empty = depgroup_empty(h, pg);
25017c478bd9Sstevel@tonic-gate 	if (empty < 0) {
25027c478bd9Sstevel@tonic-gate 		log_error(LOG_INFO,
25037c478bd9Sstevel@tonic-gate 		    "Error reading dependency group \"%s\" of %s: %s\n",
25047c478bd9Sstevel@tonic-gate 		    pg_name, info->v->gv_name, scf_strerror(scf_error()));
25057c478bd9Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
25067c478bd9Sstevel@tonic-gate 		return (info->err = EINVAL);
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 	} else if (empty == 1) {
25097c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
25107c478bd9Sstevel@tonic-gate 		    "Ignoring empty dependency group \"%s\" of %s\n",
25117c478bd9Sstevel@tonic-gate 		    pg_name, info->v->gv_name);
25127c478bd9Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
25137c478bd9Sstevel@tonic-gate 		return (info->err = 0);
25147c478bd9Sstevel@tonic-gate 	}
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate 	fmri_sz = strlen(info->v->gv_name) + 1 + len + 1;
25177c478bd9Sstevel@tonic-gate 	fmri = startd_alloc(fmri_sz);
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 	(void) snprintf(fmri, max_scf_name_size, "%s>%s", info->v->gv_name,
25207c478bd9Sstevel@tonic-gate 	    pg_name);
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 	/* Validate the pg before modifying the graph */
25237c478bd9Sstevel@tonic-gate 	deptype = depgroup_read_grouping(h, pg);
25247c478bd9Sstevel@tonic-gate 	if (deptype == DEPGRP_UNSUPPORTED) {
25257c478bd9Sstevel@tonic-gate 		log_error(LOG_INFO,
25267c478bd9Sstevel@tonic-gate 		    "Dependency \"%s\" of %s has an unknown grouping value.\n",
25277c478bd9Sstevel@tonic-gate 		    pg_name, info->v->gv_name);
25287c478bd9Sstevel@tonic-gate 		startd_free(fmri, fmri_sz);
25297c478bd9Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
25307c478bd9Sstevel@tonic-gate 		return (info->err = EINVAL);
25317c478bd9Sstevel@tonic-gate 	}
25327c478bd9Sstevel@tonic-gate 
2533af3bcd91Srm88369 	rerr = depgroup_read_restart(h, pg);
2534af3bcd91Srm88369 	if (rerr == RERR_UNSUPPORTED) {
2535af3bcd91Srm88369 		log_error(LOG_INFO,
2536af3bcd91Srm88369 		    "Dependency \"%s\" of %s has an unknown restart_on value."
2537af3bcd91Srm88369 		    "\n", pg_name, info->v->gv_name);
2538af3bcd91Srm88369 		startd_free(fmri, fmri_sz);
2539af3bcd91Srm88369 		startd_free(pg_name, max_scf_name_size);
2540af3bcd91Srm88369 		return (info->err = EINVAL);
2541af3bcd91Srm88369 	}
2542af3bcd91Srm88369 
25437c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
25447c478bd9Sstevel@tonic-gate 
25457c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0) {
25467c478bd9Sstevel@tonic-gate 		scferr = scf_error();
25477c478bd9Sstevel@tonic-gate 		scf_property_destroy(prop);
25487c478bd9Sstevel@tonic-gate 		if (scferr == SCF_ERROR_DELETED) {
25497c478bd9Sstevel@tonic-gate 			startd_free(fmri, fmri_sz);
25507c478bd9Sstevel@tonic-gate 			startd_free(pg_name, max_scf_name_size);
25517c478bd9Sstevel@tonic-gate 			return (info->err = 0);
25527c478bd9Sstevel@tonic-gate 		} else if (scferr != SCF_ERROR_NOT_FOUND) {
25537c478bd9Sstevel@tonic-gate 			startd_free(fmri, fmri_sz);
25547c478bd9Sstevel@tonic-gate 			startd_free(pg_name, max_scf_name_size);
25557c478bd9Sstevel@tonic-gate 			return (info->err = ECONNABORTED);
25567c478bd9Sstevel@tonic-gate 		}
25577c478bd9Sstevel@tonic-gate 
25587c478bd9Sstevel@tonic-gate 		log_error(LOG_INFO,
25597c478bd9Sstevel@tonic-gate 		    "Dependency \"%s\" of %s is missing a \"%s\" property.\n",
25607c478bd9Sstevel@tonic-gate 		    pg_name, info->v->gv_name, SCF_PROPERTY_ENTITIES);
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 		startd_free(fmri, fmri_sz);
25637c478bd9Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 		return (info->err = EINVAL);
25667c478bd9Sstevel@tonic-gate 	}
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 	/* Create depgroup vertex for pg */
25697c478bd9Sstevel@tonic-gate 	err = graph_insert_vertex_unconfigured(fmri, GVT_GROUP, deptype,
2570af3bcd91Srm88369 	    rerr, &depgrp);
25717c478bd9Sstevel@tonic-gate 	assert(err == 0);
25727c478bd9Sstevel@tonic-gate 	startd_free(fmri, fmri_sz);
25737c478bd9Sstevel@tonic-gate 
25747c478bd9Sstevel@tonic-gate 	/* Add dependency from inst vertex to new vertex */
25757c478bd9Sstevel@tonic-gate 	err = graph_insert_dependency(info->v, depgrp, info->pathp);
25767c478bd9Sstevel@tonic-gate 	/* ELOOP can't happen because this should be a new vertex */
25777c478bd9Sstevel@tonic-gate 	assert(err == 0);
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 	linfo.v = depgrp;
25807c478bd9Sstevel@tonic-gate 	linfo.type = depgroup_read_scheme(h, pg);
25817c478bd9Sstevel@tonic-gate 	linfo.inst_fmri = info->v->gv_name;
25827c478bd9Sstevel@tonic-gate 	linfo.pg_name = pg_name;
25837c478bd9Sstevel@tonic-gate 	linfo.h = h;
25847c478bd9Sstevel@tonic-gate 	linfo.err = 0;
25857c478bd9Sstevel@tonic-gate 	linfo.pathp = info->pathp;
25867c478bd9Sstevel@tonic-gate 	err = walk_property_astrings(prop, (callback_t)process_dependency_fmri,
25877c478bd9Sstevel@tonic-gate 	    &linfo);
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
25907c478bd9Sstevel@tonic-gate 	startd_free(pg_name, max_scf_name_size);
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 	switch (err) {
25937c478bd9Sstevel@tonic-gate 	case 0:
25947c478bd9Sstevel@tonic-gate 	case EINTR:
25957c478bd9Sstevel@tonic-gate 		return (info->err = linfo.err);
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
25987c478bd9Sstevel@tonic-gate 	case EINVAL:
25997c478bd9Sstevel@tonic-gate 		return (info->err = err);
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate 	case ECANCELED:
26027c478bd9Sstevel@tonic-gate 		return (info->err = 0);
26037c478bd9Sstevel@tonic-gate 
26047c478bd9Sstevel@tonic-gate 	case ECONNRESET:
26057c478bd9Sstevel@tonic-gate 		return (info->err = ECONNABORTED);
26067c478bd9Sstevel@tonic-gate 
26077c478bd9Sstevel@tonic-gate 	default:
26087c478bd9Sstevel@tonic-gate 		bad_error("walk_property_astrings", err);
26097c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
26107c478bd9Sstevel@tonic-gate 	}
26117c478bd9Sstevel@tonic-gate }
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate /*
26147c478bd9Sstevel@tonic-gate  * Build the dependency info for v from the repository.  Returns 0 on success,
26157c478bd9Sstevel@tonic-gate  * ECONNABORTED on repository disconnection, EINVAL if the repository
26167c478bd9Sstevel@tonic-gate  * configuration is invalid, and ELOOP if a dependency would cause a cycle.
26177c478bd9Sstevel@tonic-gate  * In the last case, *pathp will point to a -1-terminated array of ids which
26187c478bd9Sstevel@tonic-gate  * constitute the rest of the dependency cycle.
26197c478bd9Sstevel@tonic-gate  */
26207c478bd9Sstevel@tonic-gate static int
26217c478bd9Sstevel@tonic-gate set_dependencies(graph_vertex_t *v, scf_instance_t *inst, int **pathp)
26227c478bd9Sstevel@tonic-gate {
26237c478bd9Sstevel@tonic-gate 	struct deppg_info info;
26247c478bd9Sstevel@tonic-gate 	int err;
26257c478bd9Sstevel@tonic-gate 	uint_t old_configured;
26267c478bd9Sstevel@tonic-gate 
2627*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 	/*
26307c478bd9Sstevel@tonic-gate 	 * Mark the vertex as configured during dependency insertion to avoid
26317c478bd9Sstevel@tonic-gate 	 * dependency cycles (which can appear in the graph if one of the
26327c478bd9Sstevel@tonic-gate 	 * vertices is an exclusion-group).
26337c478bd9Sstevel@tonic-gate 	 */
26347c478bd9Sstevel@tonic-gate 	old_configured = v->gv_flags & GV_CONFIGURED;
26357c478bd9Sstevel@tonic-gate 	v->gv_flags |= GV_CONFIGURED;
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate 	info.err = 0;
26387c478bd9Sstevel@tonic-gate 	info.v = v;
26397c478bd9Sstevel@tonic-gate 	info.pathp = pathp;
26407c478bd9Sstevel@tonic-gate 
26417c478bd9Sstevel@tonic-gate 	err = walk_dependency_pgs(inst, (callback_t)process_dependency_pg,
26427c478bd9Sstevel@tonic-gate 	    &info);
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate 	if (!old_configured)
26457c478bd9Sstevel@tonic-gate 		v->gv_flags &= ~GV_CONFIGURED;
26467c478bd9Sstevel@tonic-gate 
26477c478bd9Sstevel@tonic-gate 	switch (err) {
26487c478bd9Sstevel@tonic-gate 	case 0:
26497c478bd9Sstevel@tonic-gate 	case EINTR:
26507c478bd9Sstevel@tonic-gate 		return (info.err);
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
26537c478bd9Sstevel@tonic-gate 		return (ECONNABORTED);
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 	case ECANCELED:
26567c478bd9Sstevel@tonic-gate 		/* Should get delete event, so return 0. */
26577c478bd9Sstevel@tonic-gate 		return (0);
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 	default:
26607c478bd9Sstevel@tonic-gate 		bad_error("walk_dependency_pgs", err);
26617c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
26627c478bd9Sstevel@tonic-gate 	}
26637c478bd9Sstevel@tonic-gate }
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate 
26667c478bd9Sstevel@tonic-gate static void
26677c478bd9Sstevel@tonic-gate handle_cycle(const char *fmri, int *path)
26687c478bd9Sstevel@tonic-gate {
26697c478bd9Sstevel@tonic-gate 	const char *cp;
26707c478bd9Sstevel@tonic-gate 	size_t sz;
26717c478bd9Sstevel@tonic-gate 
2672*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 	path_to_str(path, (char **)&cp, &sz);
26757c478bd9Sstevel@tonic-gate 
267699b44c3bSlianep 	log_error(LOG_ERR, "Transitioning %s to maintenance "
267799b44c3bSlianep 	    "because it completes a dependency cycle (see svcs -xv for "
267899b44c3bSlianep 	    "details):\n%s", fmri ? fmri : "?", cp);
26797c478bd9Sstevel@tonic-gate 
26807c478bd9Sstevel@tonic-gate 	startd_free((void *)cp, sz);
26817c478bd9Sstevel@tonic-gate }
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate /*
26843ad28c1eSrm88369  * Increment the vertex's reference count to prevent the vertex removal
26853ad28c1eSrm88369  * from the dgraph.
26863ad28c1eSrm88369  */
26873ad28c1eSrm88369 static void
26883ad28c1eSrm88369 vertex_ref(graph_vertex_t *v)
26893ad28c1eSrm88369 {
2690*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
26913ad28c1eSrm88369 
26923ad28c1eSrm88369 	v->gv_refs++;
26933ad28c1eSrm88369 }
26943ad28c1eSrm88369 
26953ad28c1eSrm88369 /*
26963ad28c1eSrm88369  * Decrement the vertex's reference count and remove the vertex from
26973ad28c1eSrm88369  * the dgraph when possible.
26983ad28c1eSrm88369  *
26993ad28c1eSrm88369  * Return VERTEX_REMOVED when the vertex has been removed otherwise
27003ad28c1eSrm88369  * return VERTEX_INUSE.
27017c478bd9Sstevel@tonic-gate  */
27027c478bd9Sstevel@tonic-gate static int
27033ad28c1eSrm88369 vertex_unref(graph_vertex_t *v)
27043ad28c1eSrm88369 {
2705*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
27063ad28c1eSrm88369 	assert(v->gv_refs > 0);
27073ad28c1eSrm88369 
27083ad28c1eSrm88369 	v->gv_refs--;
27093ad28c1eSrm88369 
27103ad28c1eSrm88369 	return (free_if_unrefed(v));
27113ad28c1eSrm88369 }
27123ad28c1eSrm88369 
27133ad28c1eSrm88369 /*
27143ad28c1eSrm88369  * When run on the dependencies of a vertex, populates list with
27153ad28c1eSrm88369  * graph_edge_t's which point to the service vertices or the instance
27163ad28c1eSrm88369  * vertices (no GVT_GROUP nodes) on which the vertex depends.
27173ad28c1eSrm88369  *
27183ad28c1eSrm88369  * Increment the vertex's reference count once the vertex is inserted
27193ad28c1eSrm88369  * in the list. The vertex won't be able to be deleted from the dgraph
27203ad28c1eSrm88369  * while it is referenced.
27213ad28c1eSrm88369  */
27223ad28c1eSrm88369 static int
27233ad28c1eSrm88369 append_svcs_or_insts(graph_edge_t *e, uu_list_t *list)
27247c478bd9Sstevel@tonic-gate {
27257c478bd9Sstevel@tonic-gate 	graph_vertex_t *v = e->ge_vertex;
27267c478bd9Sstevel@tonic-gate 	graph_edge_t *new;
27277c478bd9Sstevel@tonic-gate 	int r;
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate 	switch (v->gv_type) {
27307c478bd9Sstevel@tonic-gate 	case GVT_INST:
27317c478bd9Sstevel@tonic-gate 	case GVT_SVC:
27327c478bd9Sstevel@tonic-gate 		break;
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 	case GVT_GROUP:
27357c478bd9Sstevel@tonic-gate 		r = uu_list_walk(v->gv_dependencies,
27363ad28c1eSrm88369 		    (uu_walk_fn_t *)append_svcs_or_insts, list, 0);
27377c478bd9Sstevel@tonic-gate 		assert(r == 0);
27387c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
27397c478bd9Sstevel@tonic-gate 
27407c478bd9Sstevel@tonic-gate 	case GVT_FILE:
27417c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate 	default:
27447c478bd9Sstevel@tonic-gate #ifndef NDEBUG
27457c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
27467c478bd9Sstevel@tonic-gate 		    __LINE__, v->gv_type);
27477c478bd9Sstevel@tonic-gate #endif
27487c478bd9Sstevel@tonic-gate 		abort();
27497c478bd9Sstevel@tonic-gate 	}
27507c478bd9Sstevel@tonic-gate 
27517c478bd9Sstevel@tonic-gate 	new = startd_alloc(sizeof (*new));
27527c478bd9Sstevel@tonic-gate 	new->ge_vertex = v;
27537c478bd9Sstevel@tonic-gate 	uu_list_node_init(new, &new->ge_link, graph_edge_pool);
27547c478bd9Sstevel@tonic-gate 	r = uu_list_insert_before(list, NULL, new);
27557c478bd9Sstevel@tonic-gate 	assert(r == 0);
27563ad28c1eSrm88369 
27573ad28c1eSrm88369 	/*
27583ad28c1eSrm88369 	 * Because we are inserting the vertex in a list, we don't want
27593ad28c1eSrm88369 	 * the vertex to be freed while the list is in use. In order to
27603ad28c1eSrm88369 	 * achieve that, increment the vertex's reference count.
27613ad28c1eSrm88369 	 */
27623ad28c1eSrm88369 	vertex_ref(v);
27633ad28c1eSrm88369 
27647c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
27657c478bd9Sstevel@tonic-gate }
27667c478bd9Sstevel@tonic-gate 
27677c478bd9Sstevel@tonic-gate static boolean_t
27687c478bd9Sstevel@tonic-gate should_be_in_subgraph(graph_vertex_t *v)
27697c478bd9Sstevel@tonic-gate {
27707c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
27717c478bd9Sstevel@tonic-gate 
27727c478bd9Sstevel@tonic-gate 	if (v == milestone)
27737c478bd9Sstevel@tonic-gate 		return (B_TRUE);
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 	/*
27767c478bd9Sstevel@tonic-gate 	 * v is in the subgraph if any of its dependents are in the subgraph.
27777c478bd9Sstevel@tonic-gate 	 * Except for EXCLUDE_ALL dependents.  And OPTIONAL dependents only
27787c478bd9Sstevel@tonic-gate 	 * count if we're enabled.
27797c478bd9Sstevel@tonic-gate 	 */
27807c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
27817c478bd9Sstevel@tonic-gate 	    e != NULL;
27827c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e)) {
27837c478bd9Sstevel@tonic-gate 		graph_vertex_t *dv = e->ge_vertex;
27847c478bd9Sstevel@tonic-gate 
27857c478bd9Sstevel@tonic-gate 		if (!(dv->gv_flags & GV_INSUBGRAPH))
27867c478bd9Sstevel@tonic-gate 			continue;
27877c478bd9Sstevel@tonic-gate 
27887c478bd9Sstevel@tonic-gate 		/*
27897c478bd9Sstevel@tonic-gate 		 * Don't include instances that are optional and disabled.
27907c478bd9Sstevel@tonic-gate 		 */
27917c478bd9Sstevel@tonic-gate 		if (v->gv_type == GVT_INST && dv->gv_type == GVT_SVC) {
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 			int in = 0;
27947c478bd9Sstevel@tonic-gate 			graph_edge_t *ee;
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate 			for (ee = uu_list_first(dv->gv_dependents);
27977c478bd9Sstevel@tonic-gate 			    ee != NULL;
27987c478bd9Sstevel@tonic-gate 			    ee = uu_list_next(dv->gv_dependents, ee)) {
27997c478bd9Sstevel@tonic-gate 
28007c478bd9Sstevel@tonic-gate 				graph_vertex_t *ddv = e->ge_vertex;
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 				if (ddv->gv_type == GVT_GROUP &&
28037c478bd9Sstevel@tonic-gate 				    ddv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
28047c478bd9Sstevel@tonic-gate 					continue;
28057c478bd9Sstevel@tonic-gate 
28067c478bd9Sstevel@tonic-gate 				if (ddv->gv_type == GVT_GROUP &&
28077c478bd9Sstevel@tonic-gate 				    ddv->gv_depgroup == DEPGRP_OPTIONAL_ALL &&
28087c478bd9Sstevel@tonic-gate 				    !(v->gv_flags & GV_ENBLD_NOOVR))
28097c478bd9Sstevel@tonic-gate 					continue;
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate 				in = 1;
28127c478bd9Sstevel@tonic-gate 			}
28137c478bd9Sstevel@tonic-gate 			if (!in)
28147c478bd9Sstevel@tonic-gate 				continue;
28157c478bd9Sstevel@tonic-gate 		}
28167c478bd9Sstevel@tonic-gate 		if (v->gv_type == GVT_INST &&
28177c478bd9Sstevel@tonic-gate 		    dv->gv_type == GVT_GROUP &&
28187c478bd9Sstevel@tonic-gate 		    dv->gv_depgroup == DEPGRP_OPTIONAL_ALL &&
28197c478bd9Sstevel@tonic-gate 		    !(v->gv_flags & GV_ENBLD_NOOVR))
28207c478bd9Sstevel@tonic-gate 			continue;
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 		/* Don't include excluded services and instances */
28237c478bd9Sstevel@tonic-gate 		if (dv->gv_type == GVT_GROUP &&
28247c478bd9Sstevel@tonic-gate 		    dv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
28257c478bd9Sstevel@tonic-gate 			continue;
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate 		return (B_TRUE);
28287c478bd9Sstevel@tonic-gate 	}
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate 	return (B_FALSE);
28317c478bd9Sstevel@tonic-gate }
28327c478bd9Sstevel@tonic-gate 
28337c478bd9Sstevel@tonic-gate /*
28347c478bd9Sstevel@tonic-gate  * Ensures that GV_INSUBGRAPH is set properly for v and its descendents.  If
28357c478bd9Sstevel@tonic-gate  * any bits change, manipulate the repository appropriately.  Returns 0 or
28367c478bd9Sstevel@tonic-gate  * ECONNABORTED.
28377c478bd9Sstevel@tonic-gate  */
28387c478bd9Sstevel@tonic-gate static int
28397c478bd9Sstevel@tonic-gate eval_subgraph(graph_vertex_t *v, scf_handle_t *h)
28407c478bd9Sstevel@tonic-gate {
28417c478bd9Sstevel@tonic-gate 	boolean_t old = (v->gv_flags & GV_INSUBGRAPH) != 0;
28427c478bd9Sstevel@tonic-gate 	boolean_t new;
28437c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
28447c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
28457c478bd9Sstevel@tonic-gate 	int ret = 0, r;
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	assert(milestone != NULL && milestone != MILESTONE_NONE);
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 	new = should_be_in_subgraph(v);
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 	if (new == old)
28527c478bd9Sstevel@tonic-gate 		return (0);
28537c478bd9Sstevel@tonic-gate 
28547c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, new ? "Adding %s to the subgraph.\n" :
28557c478bd9Sstevel@tonic-gate 	    "Removing %s from the subgraph.\n", v->gv_name);
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_INSUBGRAPH) |
28587c478bd9Sstevel@tonic-gate 	    (new ? GV_INSUBGRAPH : 0);
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate 	if (v->gv_type == GVT_INST && (v->gv_flags & GV_CONFIGURED)) {
28617c478bd9Sstevel@tonic-gate 		int err;
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate get_inst:
28647c478bd9Sstevel@tonic-gate 		err = libscf_fmri_get_instance(h, v->gv_name, &inst);
28657c478bd9Sstevel@tonic-gate 		if (err != 0) {
28667c478bd9Sstevel@tonic-gate 			switch (err) {
28677c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
28687c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
28697c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
28707c478bd9Sstevel@tonic-gate 				goto get_inst;
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 			case ENOENT:
28737c478bd9Sstevel@tonic-gate 				break;
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate 			case EINVAL:
28767c478bd9Sstevel@tonic-gate 			case ENOTSUP:
28777c478bd9Sstevel@tonic-gate 			default:
28787c478bd9Sstevel@tonic-gate 				bad_error("libscf_fmri_get_instance", err);
28797c478bd9Sstevel@tonic-gate 			}
28807c478bd9Sstevel@tonic-gate 		} else {
28817c478bd9Sstevel@tonic-gate 			const char *f;
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate 			if (new) {
28847c478bd9Sstevel@tonic-gate 				err = libscf_delete_enable_ovr(inst);
28857c478bd9Sstevel@tonic-gate 				f = "libscf_delete_enable_ovr";
28867c478bd9Sstevel@tonic-gate 			} else {
28877c478bd9Sstevel@tonic-gate 				err = libscf_set_enable_ovr(inst, 0);
28887c478bd9Sstevel@tonic-gate 				f = "libscf_set_enable_ovr";
28897c478bd9Sstevel@tonic-gate 			}
28907c478bd9Sstevel@tonic-gate 			scf_instance_destroy(inst);
28917c478bd9Sstevel@tonic-gate 			switch (err) {
28927c478bd9Sstevel@tonic-gate 			case 0:
28937c478bd9Sstevel@tonic-gate 			case ECANCELED:
28947c478bd9Sstevel@tonic-gate 				break;
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
28977c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
28987c478bd9Sstevel@tonic-gate 				/*
28997c478bd9Sstevel@tonic-gate 				 * We must continue so the graph is updated,
29007c478bd9Sstevel@tonic-gate 				 * but we must return ECONNABORTED so any
29017c478bd9Sstevel@tonic-gate 				 * libscf state held by any callers is reset.
29027c478bd9Sstevel@tonic-gate 				 */
29037c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
29047c478bd9Sstevel@tonic-gate 				goto get_inst;
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate 			case EROFS:
29077c478bd9Sstevel@tonic-gate 			case EPERM:
29087c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING,
29097c478bd9Sstevel@tonic-gate 				    "Could not set %s/%s for %s: %s.\n",
29107c478bd9Sstevel@tonic-gate 				    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
29117c478bd9Sstevel@tonic-gate 				    v->gv_name, strerror(err));
29127c478bd9Sstevel@tonic-gate 				break;
29137c478bd9Sstevel@tonic-gate 
29147c478bd9Sstevel@tonic-gate 			default:
29157c478bd9Sstevel@tonic-gate 				bad_error(f, err);
29167c478bd9Sstevel@tonic-gate 			}
29177c478bd9Sstevel@tonic-gate 		}
29187c478bd9Sstevel@tonic-gate 	}
29197c478bd9Sstevel@tonic-gate 
29207c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
29217c478bd9Sstevel@tonic-gate 	    e != NULL;
29227c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
29237c478bd9Sstevel@tonic-gate 		r = eval_subgraph(e->ge_vertex, h);
29247c478bd9Sstevel@tonic-gate 		if (r != 0) {
29257c478bd9Sstevel@tonic-gate 			assert(r == ECONNABORTED);
29267c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
29277c478bd9Sstevel@tonic-gate 		}
29287c478bd9Sstevel@tonic-gate 	}
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate 	return (ret);
29317c478bd9Sstevel@tonic-gate }
29327c478bd9Sstevel@tonic-gate 
29337c478bd9Sstevel@tonic-gate /*
29347c478bd9Sstevel@tonic-gate  * Delete the (property group) dependencies of v & create new ones based on
29357c478bd9Sstevel@tonic-gate  * inst.  If doing so would create a cycle, log a message and put the instance
29367c478bd9Sstevel@tonic-gate  * into maintenance.  Update GV_INSUBGRAPH flags as necessary.  Returns 0 or
29377c478bd9Sstevel@tonic-gate  * ECONNABORTED.
29387c478bd9Sstevel@tonic-gate  */
293999b44c3bSlianep int
29407c478bd9Sstevel@tonic-gate refresh_vertex(graph_vertex_t *v, scf_instance_t *inst)
29417c478bd9Sstevel@tonic-gate {
29427c478bd9Sstevel@tonic-gate 	int err;
29437c478bd9Sstevel@tonic-gate 	int *path;
29447c478bd9Sstevel@tonic-gate 	char *fmri;
29457c478bd9Sstevel@tonic-gate 	int r;
29467c478bd9Sstevel@tonic-gate 	scf_handle_t *h = scf_instance_handle(inst);
29477c478bd9Sstevel@tonic-gate 	uu_list_t *old_deps;
29487c478bd9Sstevel@tonic-gate 	int ret = 0;
29497c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
29503ad28c1eSrm88369 	graph_vertex_t *vv;
29517c478bd9Sstevel@tonic-gate 
2952*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
29537c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph engine: Refreshing %s.\n", v->gv_name);
29567c478bd9Sstevel@tonic-gate 
29577c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
29587c478bd9Sstevel@tonic-gate 		/*
29597c478bd9Sstevel@tonic-gate 		 * In case some of v's dependencies are being deleted we must
29607c478bd9Sstevel@tonic-gate 		 * make a list of them now for GV_INSUBGRAPH-flag evaluation
29617c478bd9Sstevel@tonic-gate 		 * after the new dependencies are in place.
29627c478bd9Sstevel@tonic-gate 		 */
29637c478bd9Sstevel@tonic-gate 		old_deps = startd_list_create(graph_edge_pool, NULL, 0);
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate 		err = uu_list_walk(v->gv_dependencies,
29663ad28c1eSrm88369 		    (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0);
29677c478bd9Sstevel@tonic-gate 		assert(err == 0);
29687c478bd9Sstevel@tonic-gate 	}
29697c478bd9Sstevel@tonic-gate 
29707c478bd9Sstevel@tonic-gate 	delete_instance_dependencies(v, B_FALSE);
29717c478bd9Sstevel@tonic-gate 
29727c478bd9Sstevel@tonic-gate 	err = set_dependencies(v, inst, &path);
29737c478bd9Sstevel@tonic-gate 	switch (err) {
29747c478bd9Sstevel@tonic-gate 	case 0:
29757c478bd9Sstevel@tonic-gate 		break;
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
29787c478bd9Sstevel@tonic-gate 		ret = err;
29797c478bd9Sstevel@tonic-gate 		goto out;
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 	case EINVAL:
29827c478bd9Sstevel@tonic-gate 	case ELOOP:
29837c478bd9Sstevel@tonic-gate 		r = libscf_instance_get_fmri(inst, &fmri);
29847c478bd9Sstevel@tonic-gate 		switch (r) {
29857c478bd9Sstevel@tonic-gate 		case 0:
29867c478bd9Sstevel@tonic-gate 			break;
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
29897c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
29907c478bd9Sstevel@tonic-gate 			goto out;
29917c478bd9Sstevel@tonic-gate 
29927c478bd9Sstevel@tonic-gate 		case ECANCELED:
29937c478bd9Sstevel@tonic-gate 			ret = 0;
29947c478bd9Sstevel@tonic-gate 			goto out;
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 		default:
29977c478bd9Sstevel@tonic-gate 			bad_error("libscf_instance_get_fmri", r);
29987c478bd9Sstevel@tonic-gate 		}
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 		if (err == EINVAL) {
30017c478bd9Sstevel@tonic-gate 			log_error(LOG_ERR, "Transitioning %s "
30027c478bd9Sstevel@tonic-gate 			    "to maintenance due to misconfiguration.\n",
30037c478bd9Sstevel@tonic-gate 			    fmri ? fmri : "?");
30047c478bd9Sstevel@tonic-gate 			vertex_send_event(v,
30057c478bd9Sstevel@tonic-gate 			    RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY);
30067c478bd9Sstevel@tonic-gate 		} else {
30077c478bd9Sstevel@tonic-gate 			handle_cycle(fmri, path);
30087c478bd9Sstevel@tonic-gate 			vertex_send_event(v,
30097c478bd9Sstevel@tonic-gate 			    RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE);
30107c478bd9Sstevel@tonic-gate 		}
30117c478bd9Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
30127c478bd9Sstevel@tonic-gate 		ret = 0;
30137c478bd9Sstevel@tonic-gate 		goto out;
30147c478bd9Sstevel@tonic-gate 
30157c478bd9Sstevel@tonic-gate 	default:
30167c478bd9Sstevel@tonic-gate 		bad_error("set_dependencies", err);
30177c478bd9Sstevel@tonic-gate 	}
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
30207c478bd9Sstevel@tonic-gate 		boolean_t aborted = B_FALSE;
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate 		for (e = uu_list_first(old_deps);
30237c478bd9Sstevel@tonic-gate 		    e != NULL;
30247c478bd9Sstevel@tonic-gate 		    e = uu_list_next(old_deps, e)) {
30253ad28c1eSrm88369 			vv = e->ge_vertex;
30263ad28c1eSrm88369 
30273ad28c1eSrm88369 			if (vertex_unref(vv) == VERTEX_INUSE &&
30283ad28c1eSrm88369 			    eval_subgraph(vv, h) == ECONNABORTED)
30297c478bd9Sstevel@tonic-gate 				aborted = B_TRUE;
30307c478bd9Sstevel@tonic-gate 		}
30317c478bd9Sstevel@tonic-gate 
30327c478bd9Sstevel@tonic-gate 		for (e = uu_list_first(v->gv_dependencies);
30337c478bd9Sstevel@tonic-gate 		    e != NULL;
30347c478bd9Sstevel@tonic-gate 		    e = uu_list_next(v->gv_dependencies, e)) {
30357c478bd9Sstevel@tonic-gate 			if (eval_subgraph(e->ge_vertex, h) ==
30367c478bd9Sstevel@tonic-gate 			    ECONNABORTED)
30377c478bd9Sstevel@tonic-gate 				aborted = B_TRUE;
30387c478bd9Sstevel@tonic-gate 		}
30397c478bd9Sstevel@tonic-gate 
30407c478bd9Sstevel@tonic-gate 		if (aborted) {
30417c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
30427c478bd9Sstevel@tonic-gate 			goto out;
30437c478bd9Sstevel@tonic-gate 		}
30447c478bd9Sstevel@tonic-gate 	}
30457c478bd9Sstevel@tonic-gate 
304699b44c3bSlianep 	graph_start_if_satisfied(v);
30477c478bd9Sstevel@tonic-gate 
30487c478bd9Sstevel@tonic-gate 	ret = 0;
30497c478bd9Sstevel@tonic-gate 
30507c478bd9Sstevel@tonic-gate out:
30517c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
30527c478bd9Sstevel@tonic-gate 		void *cookie = NULL;
30537c478bd9Sstevel@tonic-gate 
30547c478bd9Sstevel@tonic-gate 		while ((e = uu_list_teardown(old_deps, &cookie)) != NULL)
30557c478bd9Sstevel@tonic-gate 			startd_free(e, sizeof (*e));
30567c478bd9Sstevel@tonic-gate 
30577c478bd9Sstevel@tonic-gate 		uu_list_destroy(old_deps);
30587c478bd9Sstevel@tonic-gate 	}
30597c478bd9Sstevel@tonic-gate 
30607c478bd9Sstevel@tonic-gate 	return (ret);
30617c478bd9Sstevel@tonic-gate }
30627c478bd9Sstevel@tonic-gate 
30637c478bd9Sstevel@tonic-gate /*
30647c478bd9Sstevel@tonic-gate  * Set up v according to inst.  That is, make sure it depends on its
30657c478bd9Sstevel@tonic-gate  * restarter and set up its dependencies.  Send the ADD_INSTANCE command to
30667c478bd9Sstevel@tonic-gate  * the restarter, and send ENABLE or DISABLE as appropriate.
30677c478bd9Sstevel@tonic-gate  *
30687c478bd9Sstevel@tonic-gate  * Returns 0 on success, ECONNABORTED on repository disconnection, or
30697c478bd9Sstevel@tonic-gate  * ECANCELED if inst is deleted.
30707c478bd9Sstevel@tonic-gate  */
30717c478bd9Sstevel@tonic-gate static int
30727c478bd9Sstevel@tonic-gate configure_vertex(graph_vertex_t *v, scf_instance_t *inst)
30737c478bd9Sstevel@tonic-gate {
30747c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
30757c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
30767c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
30777c478bd9Sstevel@tonic-gate 	char *restarter_fmri = startd_alloc(max_scf_value_size);
30787c478bd9Sstevel@tonic-gate 	int enabled, enabled_ovr;
30797c478bd9Sstevel@tonic-gate 	int err;
30807c478bd9Sstevel@tonic-gate 	int *path;
308170cbfe41SPhilippe Jung 	int deathrow;
30827c478bd9Sstevel@tonic-gate 
30837c478bd9Sstevel@tonic-gate 	restarter_fmri[0] = '\0';
30847c478bd9Sstevel@tonic-gate 
3085*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
30867c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
30877c478bd9Sstevel@tonic-gate 	assert((v->gv_flags & GV_CONFIGURED) == 0);
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate 	/* GV_INSUBGRAPH should already be set properly. */
30907c478bd9Sstevel@tonic-gate 	assert(should_be_in_subgraph(v) ==
30917c478bd9Sstevel@tonic-gate 	    ((v->gv_flags & GV_INSUBGRAPH) != 0));
30927c478bd9Sstevel@tonic-gate 
309370cbfe41SPhilippe Jung 	/*
309470cbfe41SPhilippe Jung 	 * If the instance fmri is in the deathrow list then set the
309570cbfe41SPhilippe Jung 	 * GV_DEATHROW flag on the vertex and create and set to true the
309670cbfe41SPhilippe Jung 	 * SCF_PROPERTY_DEATHROW boolean property in the non-persistent
309770cbfe41SPhilippe Jung 	 * repository for this instance fmri.
309870cbfe41SPhilippe Jung 	 */
309970cbfe41SPhilippe Jung 	if ((v->gv_flags & GV_DEATHROW) ||
310070cbfe41SPhilippe Jung 	    (is_fmri_in_deathrow(v->gv_name) == B_TRUE)) {
310170cbfe41SPhilippe Jung 		if ((v->gv_flags & GV_DEATHROW) == 0) {
310270cbfe41SPhilippe Jung 			/*
310370cbfe41SPhilippe Jung 			 * Set flag GV_DEATHROW, create and set to true
310470cbfe41SPhilippe Jung 			 * the SCF_PROPERTY_DEATHROW property in the
310570cbfe41SPhilippe Jung 			 * non-persistent repository for this instance fmri.
310670cbfe41SPhilippe Jung 			 */
310770cbfe41SPhilippe Jung 			v->gv_flags |= GV_DEATHROW;
310870cbfe41SPhilippe Jung 
310970cbfe41SPhilippe Jung 			switch (err = libscf_set_deathrow(inst, 1)) {
311070cbfe41SPhilippe Jung 			case 0:
311170cbfe41SPhilippe Jung 				break;
311270cbfe41SPhilippe Jung 
311370cbfe41SPhilippe Jung 			case ECONNABORTED:
311470cbfe41SPhilippe Jung 			case ECANCELED:
311570cbfe41SPhilippe Jung 				startd_free(restarter_fmri, max_scf_value_size);
311670cbfe41SPhilippe Jung 				return (err);
311770cbfe41SPhilippe Jung 
311870cbfe41SPhilippe Jung 			case EROFS:
311970cbfe41SPhilippe Jung 				log_error(LOG_WARNING, "Could not set %s/%s "
312070cbfe41SPhilippe Jung 				    "for deathrow %s: %s.\n",
312170cbfe41SPhilippe Jung 				    SCF_PG_DEATHROW, SCF_PROPERTY_DEATHROW,
312270cbfe41SPhilippe Jung 				    v->gv_name, strerror(err));
312370cbfe41SPhilippe Jung 				break;
312470cbfe41SPhilippe Jung 
312570cbfe41SPhilippe Jung 			case EPERM:
312670cbfe41SPhilippe Jung 				uu_die("Permission denied.\n");
312770cbfe41SPhilippe Jung 				/* NOTREACHED */
312870cbfe41SPhilippe Jung 
312970cbfe41SPhilippe Jung 			default:
313070cbfe41SPhilippe Jung 				bad_error("libscf_set_deathrow", err);
313170cbfe41SPhilippe Jung 			}
313270cbfe41SPhilippe Jung 			log_framework(LOG_DEBUG, "Deathrow, graph set %s.\n",
313370cbfe41SPhilippe Jung 			    v->gv_name);
313470cbfe41SPhilippe Jung 		}
313570cbfe41SPhilippe Jung 		startd_free(restarter_fmri, max_scf_value_size);
313670cbfe41SPhilippe Jung 		return (0);
313770cbfe41SPhilippe Jung 	}
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(inst);
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate 	/*
314270cbfe41SPhilippe Jung 	 * Using a temporary deathrow boolean property, set through
314370cbfe41SPhilippe Jung 	 * libscf_set_deathrow(), only for fmris on deathrow, is necessary
314470cbfe41SPhilippe Jung 	 * because deathrow_fini() may already have been called, and in case
314570cbfe41SPhilippe Jung 	 * of a refresh, GV_DEATHROW may need to be set again.
314670cbfe41SPhilippe Jung 	 * libscf_get_deathrow() sets deathrow to 1 only if this instance
314770cbfe41SPhilippe Jung 	 * has a temporary boolean property named 'deathrow' valued true
314870cbfe41SPhilippe Jung 	 * in a property group 'deathrow', -1 or 0 in all other cases.
314970cbfe41SPhilippe Jung 	 */
315070cbfe41SPhilippe Jung 	err = libscf_get_deathrow(h, inst, &deathrow);
315170cbfe41SPhilippe Jung 	switch (err) {
315270cbfe41SPhilippe Jung 	case 0:
315370cbfe41SPhilippe Jung 		break;
315470cbfe41SPhilippe Jung 
315570cbfe41SPhilippe Jung 	case ECONNABORTED:
315670cbfe41SPhilippe Jung 	case ECANCELED:
315770cbfe41SPhilippe Jung 		startd_free(restarter_fmri, max_scf_value_size);
315870cbfe41SPhilippe Jung 		return (err);
315970cbfe41SPhilippe Jung 
316070cbfe41SPhilippe Jung 	default:
316170cbfe41SPhilippe Jung 		bad_error("libscf_get_deathrow", err);
316270cbfe41SPhilippe Jung 	}
316370cbfe41SPhilippe Jung 
316470cbfe41SPhilippe Jung 	if (deathrow == 1) {
316570cbfe41SPhilippe Jung 		v->gv_flags |= GV_DEATHROW;
316670cbfe41SPhilippe Jung 		startd_free(restarter_fmri, max_scf_value_size);
316770cbfe41SPhilippe Jung 		return (0);
316870cbfe41SPhilippe Jung 	}
316970cbfe41SPhilippe Jung 
317070cbfe41SPhilippe Jung 	log_framework(LOG_DEBUG, "Graph adding %s.\n", v->gv_name);
317170cbfe41SPhilippe Jung 
317270cbfe41SPhilippe Jung 	/*
31737c478bd9Sstevel@tonic-gate 	 * If the instance does not have a restarter property group,
31747c478bd9Sstevel@tonic-gate 	 * initialize its state to uninitialized/none, in case the restarter
31757c478bd9Sstevel@tonic-gate 	 * is not enabled.
31767c478bd9Sstevel@tonic-gate 	 */
31777c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
31787c478bd9Sstevel@tonic-gate 
31797c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) != 0) {
31807c478bd9Sstevel@tonic-gate 		instance_data_t idata;
31817c478bd9Sstevel@tonic-gate 		uint_t count = 0, msecs = ALLOC_DELAY;
31827c478bd9Sstevel@tonic-gate 
31837c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
31847c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
31857c478bd9Sstevel@tonic-gate 			break;
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
31887c478bd9Sstevel@tonic-gate 		default:
31897c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
3190845e9415SRenaud Manus 			startd_free(restarter_fmri, max_scf_value_size);
31917c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
31947c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
3195845e9415SRenaud Manus 			startd_free(restarter_fmri, max_scf_value_size);
31967c478bd9Sstevel@tonic-gate 			return (ECANCELED);
31977c478bd9Sstevel@tonic-gate 
31987c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
31997c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
32007c478bd9Sstevel@tonic-gate 		}
32017c478bd9Sstevel@tonic-gate 
32027c478bd9Sstevel@tonic-gate 		switch (err = libscf_instance_get_fmri(inst,
32037c478bd9Sstevel@tonic-gate 		    (char **)&idata.i_fmri)) {
32047c478bd9Sstevel@tonic-gate 		case 0:
32057c478bd9Sstevel@tonic-gate 			break;
32067c478bd9Sstevel@tonic-gate 
32077c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
32087c478bd9Sstevel@tonic-gate 		case ECANCELED:
32097c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
3210845e9415SRenaud Manus 			startd_free(restarter_fmri, max_scf_value_size);
32117c478bd9Sstevel@tonic-gate 			return (err);
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 		default:
32147c478bd9Sstevel@tonic-gate 			bad_error("libscf_instance_get_fmri", err);
32157c478bd9Sstevel@tonic-gate 		}
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate 		idata.i_state = RESTARTER_STATE_NONE;
32187c478bd9Sstevel@tonic-gate 		idata.i_next_state = RESTARTER_STATE_NONE;
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate init_state:
32217c478bd9Sstevel@tonic-gate 		switch (err = _restarter_commit_states(h, &idata,
32227c478bd9Sstevel@tonic-gate 		    RESTARTER_STATE_UNINIT, RESTARTER_STATE_NONE, NULL)) {
32237c478bd9Sstevel@tonic-gate 		case 0:
32247c478bd9Sstevel@tonic-gate 			break;
32257c478bd9Sstevel@tonic-gate 
32267c478bd9Sstevel@tonic-gate 		case ENOMEM:
32277c478bd9Sstevel@tonic-gate 			++count;
32287c478bd9Sstevel@tonic-gate 			if (count < ALLOC_RETRY) {
32297c478bd9Sstevel@tonic-gate 				(void) poll(NULL, 0, msecs);
32307c478bd9Sstevel@tonic-gate 				msecs *= ALLOC_DELAY_MULT;
32317c478bd9Sstevel@tonic-gate 				goto init_state;
32327c478bd9Sstevel@tonic-gate 			}
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 			uu_die("Insufficient memory.\n");
32357c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
32385b6c5443Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
32397c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
3240845e9415SRenaud Manus 			startd_free(restarter_fmri, max_scf_value_size);
32417c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate 		case ENOENT:
32445b6c5443Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
32457c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
3246845e9415SRenaud Manus 			startd_free(restarter_fmri, max_scf_value_size);
32477c478bd9Sstevel@tonic-gate 			return (ECANCELED);
32487c478bd9Sstevel@tonic-gate 
32497c478bd9Sstevel@tonic-gate 		case EPERM:
32507c478bd9Sstevel@tonic-gate 		case EACCES:
32517c478bd9Sstevel@tonic-gate 		case EROFS:
32527c478bd9Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not initialize state for "
32537c478bd9Sstevel@tonic-gate 			    "%s: %s.\n", idata.i_fmri, strerror(err));
32547c478bd9Sstevel@tonic-gate 			break;
32557c478bd9Sstevel@tonic-gate 
32567c478bd9Sstevel@tonic-gate 		case EINVAL:
32577c478bd9Sstevel@tonic-gate 		default:
32587c478bd9Sstevel@tonic-gate 			bad_error("_restarter_commit_states", err);
32597c478bd9Sstevel@tonic-gate 		}
32607c478bd9Sstevel@tonic-gate 
32617c478bd9Sstevel@tonic-gate 		startd_free((void *)idata.i_fmri, max_scf_fmri_size);
32627c478bd9Sstevel@tonic-gate 	}
32637c478bd9Sstevel@tonic-gate 
32647c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
32657c478bd9Sstevel@tonic-gate 
32667c478bd9Sstevel@tonic-gate 	if (milestone != NULL) {
32677c478bd9Sstevel@tonic-gate 		/*
32687c478bd9Sstevel@tonic-gate 		 * Make sure the enable-override is set properly before we
32697c478bd9Sstevel@tonic-gate 		 * read whether we should be enabled.
32707c478bd9Sstevel@tonic-gate 		 */
32717c478bd9Sstevel@tonic-gate 		if (milestone == MILESTONE_NONE ||
32727c478bd9Sstevel@tonic-gate 		    !(v->gv_flags & GV_INSUBGRAPH)) {
327356e23938Sbustos 			/*
327456e23938Sbustos 			 * This might seem unjustified after the milestone
327556e23938Sbustos 			 * transition has completed (non_subgraph_svcs == 0),
327656e23938Sbustos 			 * but it's important because when we boot to
327756e23938Sbustos 			 * a milestone, we set the milestone before populating
327856e23938Sbustos 			 * the graph, and all of the new non-subgraph services
327956e23938Sbustos 			 * need to be disabled here.
328056e23938Sbustos 			 */
32817c478bd9Sstevel@tonic-gate 			switch (err = libscf_set_enable_ovr(inst, 0)) {
32827c478bd9Sstevel@tonic-gate 			case 0:
32837c478bd9Sstevel@tonic-gate 				break;
32847c478bd9Sstevel@tonic-gate 
32857c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
32867c478bd9Sstevel@tonic-gate 			case ECANCELED:
3287845e9415SRenaud Manus 				startd_free(restarter_fmri, max_scf_value_size);
32887c478bd9Sstevel@tonic-gate 				return (err);
32897c478bd9Sstevel@tonic-gate 
32907c478bd9Sstevel@tonic-gate 			case EROFS:
32917c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING,
32927c478bd9Sstevel@tonic-gate 				    "Could not set %s/%s for %s: %s.\n",
32937c478bd9Sstevel@tonic-gate 				    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
32947c478bd9Sstevel@tonic-gate 				    v->gv_name, strerror(err));
32957c478bd9Sstevel@tonic-gate 				break;
32967c478bd9Sstevel@tonic-gate 
32977c478bd9Sstevel@tonic-gate 			case EPERM:
32987c478bd9Sstevel@tonic-gate 				uu_die("Permission denied.\n");
32997c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
33007c478bd9Sstevel@tonic-gate 
33017c478bd9Sstevel@tonic-gate 			default:
33027c478bd9Sstevel@tonic-gate 				bad_error("libscf_set_enable_ovr", err);
33037c478bd9Sstevel@tonic-gate 			}
33047c478bd9Sstevel@tonic-gate 		} else {
33057c478bd9Sstevel@tonic-gate 			assert(v->gv_flags & GV_INSUBGRAPH);
33067c478bd9Sstevel@tonic-gate 			switch (err = libscf_delete_enable_ovr(inst)) {
33077c478bd9Sstevel@tonic-gate 			case 0:
33087c478bd9Sstevel@tonic-gate 				break;
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
33117c478bd9Sstevel@tonic-gate 			case ECANCELED:
3312845e9415SRenaud Manus 				startd_free(restarter_fmri, max_scf_value_size);
33137c478bd9Sstevel@tonic-gate 				return (err);
33147c478bd9Sstevel@tonic-gate 
33157c478bd9Sstevel@tonic-gate 			case EPERM:
33167c478bd9Sstevel@tonic-gate 				uu_die("Permission denied.\n");
33177c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
33187c478bd9Sstevel@tonic-gate 
33197c478bd9Sstevel@tonic-gate 			default:
33207c478bd9Sstevel@tonic-gate 				bad_error("libscf_delete_enable_ovr", err);
33217c478bd9Sstevel@tonic-gate 			}
33227c478bd9Sstevel@tonic-gate 		}
33237c478bd9Sstevel@tonic-gate 	}
33247c478bd9Sstevel@tonic-gate 
33257c478bd9Sstevel@tonic-gate 	err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled,
33267c478bd9Sstevel@tonic-gate 	    &enabled_ovr, &restarter_fmri);
33277c478bd9Sstevel@tonic-gate 	switch (err) {
33287c478bd9Sstevel@tonic-gate 	case 0:
33297c478bd9Sstevel@tonic-gate 		break;
33307c478bd9Sstevel@tonic-gate 
33317c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
33327c478bd9Sstevel@tonic-gate 	case ECANCELED:
33337c478bd9Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
33347c478bd9Sstevel@tonic-gate 		return (err);
33357c478bd9Sstevel@tonic-gate 
33367c478bd9Sstevel@tonic-gate 	case ENOENT:
33377c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
33387c478bd9Sstevel@tonic-gate 		    "Ignoring %s because it has no general property group.\n",
33397c478bd9Sstevel@tonic-gate 		    v->gv_name);
33407c478bd9Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
33417c478bd9Sstevel@tonic-gate 		return (0);
33427c478bd9Sstevel@tonic-gate 
33437c478bd9Sstevel@tonic-gate 	default:
33447c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_basic_instance_data", err);
33457c478bd9Sstevel@tonic-gate 	}
33467c478bd9Sstevel@tonic-gate 
33477c478bd9Sstevel@tonic-gate 	if (enabled == -1) {
33487c478bd9Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
33497c478bd9Sstevel@tonic-gate 		return (0);
33507c478bd9Sstevel@tonic-gate 	}
33517c478bd9Sstevel@tonic-gate 
33527c478bd9Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) |
33537c478bd9Sstevel@tonic-gate 	    (enabled ? GV_ENBLD_NOOVR : 0);
33547c478bd9Sstevel@tonic-gate 
33557c478bd9Sstevel@tonic-gate 	if (enabled_ovr != -1)
33567c478bd9Sstevel@tonic-gate 		enabled = enabled_ovr;
33577c478bd9Sstevel@tonic-gate 
33587c478bd9Sstevel@tonic-gate 	v->gv_state = RESTARTER_STATE_UNINIT;
33597c478bd9Sstevel@tonic-gate 
33607c478bd9Sstevel@tonic-gate 	snap = libscf_get_or_make_running_snapshot(inst, v->gv_name, B_TRUE);
33617c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
33627c478bd9Sstevel@tonic-gate 
33637c478bd9Sstevel@tonic-gate 	/* Set up the restarter. (Sends _ADD_INSTANCE on success.) */
33647c478bd9Sstevel@tonic-gate 	err = graph_change_restarter(v, restarter_fmri, h, &path);
33657c478bd9Sstevel@tonic-gate 	if (err != 0) {
33667c478bd9Sstevel@tonic-gate 		instance_data_t idata;
33677c478bd9Sstevel@tonic-gate 		uint_t count = 0, msecs = ALLOC_DELAY;
33687c478bd9Sstevel@tonic-gate 		const char *reason;
33697c478bd9Sstevel@tonic-gate 
33707c478bd9Sstevel@tonic-gate 		if (err == ECONNABORTED) {
33717c478bd9Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
33727c478bd9Sstevel@tonic-gate 			return (err);
33737c478bd9Sstevel@tonic-gate 		}
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate 		assert(err == EINVAL || err == ELOOP);
33767c478bd9Sstevel@tonic-gate 
33777c478bd9Sstevel@tonic-gate 		if (err == EINVAL) {
337899b44c3bSlianep 			log_framework(LOG_ERR, emsg_invalid_restarter,
337913d8aaa1SSean Wilcox 			    v->gv_name, restarter_fmri);
33807c478bd9Sstevel@tonic-gate 			reason = "invalid_restarter";
33817c478bd9Sstevel@tonic-gate 		} else {
33827c478bd9Sstevel@tonic-gate 			handle_cycle(v->gv_name, path);
33837c478bd9Sstevel@tonic-gate 			reason = "dependency_cycle";
33847c478bd9Sstevel@tonic-gate 		}
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate 		/*
33897c478bd9Sstevel@tonic-gate 		 * We didn't register the instance with the restarter, so we
33907c478bd9Sstevel@tonic-gate 		 * must set maintenance mode ourselves.
33917c478bd9Sstevel@tonic-gate 		 */
33927c478bd9Sstevel@tonic-gate 		err = libscf_instance_get_fmri(inst, (char **)&idata.i_fmri);
33937c478bd9Sstevel@tonic-gate 		if (err != 0) {
33947c478bd9Sstevel@tonic-gate 			assert(err == ECONNABORTED || err == ECANCELED);
33957c478bd9Sstevel@tonic-gate 			return (err);
33967c478bd9Sstevel@tonic-gate 		}
33977c478bd9Sstevel@tonic-gate 
33987c478bd9Sstevel@tonic-gate 		idata.i_state = RESTARTER_STATE_NONE;
33997c478bd9Sstevel@tonic-gate 		idata.i_next_state = RESTARTER_STATE_NONE;
34007c478bd9Sstevel@tonic-gate 
34017c478bd9Sstevel@tonic-gate set_maint:
34027c478bd9Sstevel@tonic-gate 		switch (err = _restarter_commit_states(h, &idata,
34037c478bd9Sstevel@tonic-gate 		    RESTARTER_STATE_MAINT, RESTARTER_STATE_NONE, reason)) {
34047c478bd9Sstevel@tonic-gate 		case 0:
34057c478bd9Sstevel@tonic-gate 			break;
34067c478bd9Sstevel@tonic-gate 
34077c478bd9Sstevel@tonic-gate 		case ENOMEM:
34087c478bd9Sstevel@tonic-gate 			++count;
34097c478bd9Sstevel@tonic-gate 			if (count < ALLOC_RETRY) {
34107c478bd9Sstevel@tonic-gate 				(void) poll(NULL, 0, msecs);
34117c478bd9Sstevel@tonic-gate 				msecs *= ALLOC_DELAY_MULT;
34127c478bd9Sstevel@tonic-gate 				goto set_maint;
34137c478bd9Sstevel@tonic-gate 			}
34147c478bd9Sstevel@tonic-gate 
34157c478bd9Sstevel@tonic-gate 			uu_die("Insufficient memory.\n");
34167c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
34177c478bd9Sstevel@tonic-gate 
34187c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
34195b6c5443Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
34207c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
34217c478bd9Sstevel@tonic-gate 
34227c478bd9Sstevel@tonic-gate 		case ENOENT:
34235b6c5443Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
34247c478bd9Sstevel@tonic-gate 			return (ECANCELED);
34257c478bd9Sstevel@tonic-gate 
34267c478bd9Sstevel@tonic-gate 		case EPERM:
34277c478bd9Sstevel@tonic-gate 		case EACCES:
34287c478bd9Sstevel@tonic-gate 		case EROFS:
34297c478bd9Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not initialize state for "
34307c478bd9Sstevel@tonic-gate 			    "%s: %s.\n", idata.i_fmri, strerror(err));
34317c478bd9Sstevel@tonic-gate 			break;
34327c478bd9Sstevel@tonic-gate 
34337c478bd9Sstevel@tonic-gate 		case EINVAL:
34347c478bd9Sstevel@tonic-gate 		default:
34357c478bd9Sstevel@tonic-gate 			bad_error("_restarter_commit_states", err);
34367c478bd9Sstevel@tonic-gate 		}
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate 		startd_free((void *)idata.i_fmri, max_scf_fmri_size);
34397c478bd9Sstevel@tonic-gate 
34407c478bd9Sstevel@tonic-gate 		v->gv_state = RESTARTER_STATE_MAINT;
34417c478bd9Sstevel@tonic-gate 
34427c478bd9Sstevel@tonic-gate 		goto out;
34437c478bd9Sstevel@tonic-gate 	}
34447c478bd9Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_value_size);
34457c478bd9Sstevel@tonic-gate 
34467c478bd9Sstevel@tonic-gate 	/* Add all the other dependencies. */
34477c478bd9Sstevel@tonic-gate 	err = refresh_vertex(v, inst);
34487c478bd9Sstevel@tonic-gate 	if (err != 0) {
34497c478bd9Sstevel@tonic-gate 		assert(err == ECONNABORTED);
34507c478bd9Sstevel@tonic-gate 		return (err);
34517c478bd9Sstevel@tonic-gate 	}
34527c478bd9Sstevel@tonic-gate 
34537c478bd9Sstevel@tonic-gate out:
34547c478bd9Sstevel@tonic-gate 	v->gv_flags |= GV_CONFIGURED;
34557c478bd9Sstevel@tonic-gate 
34567c478bd9Sstevel@tonic-gate 	graph_enable_by_vertex(v, enabled, 0);
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate 	return (0);
34597c478bd9Sstevel@tonic-gate }
34607c478bd9Sstevel@tonic-gate 
34614d53c7adSDan Price 
34624d53c7adSDan Price static void
34634d53c7adSDan Price kill_user_procs(void)
34644d53c7adSDan Price {
34654d53c7adSDan Price 	(void) fputs("svc.startd: Killing user processes.\n", stdout);
34664d53c7adSDan Price 
34674d53c7adSDan Price 	/*
34684d53c7adSDan Price 	 * Despite its name, killall's role is to get select user processes--
34694d53c7adSDan Price 	 * basically those representing terminal-based logins-- to die.  Victims
34704d53c7adSDan Price 	 * are located by killall in the utmp database.  Since these are most
34714d53c7adSDan Price 	 * often shell based logins, and many shells mask SIGTERM (but are
34724d53c7adSDan Price 	 * responsive to SIGHUP) we first HUP and then shortly thereafter
34734d53c7adSDan Price 	 * kill -9.
34744d53c7adSDan Price 	 */
34754d53c7adSDan Price 	(void) fork_with_timeout("/usr/sbin/killall HUP", 1, 5);
34764d53c7adSDan Price 	(void) fork_with_timeout("/usr/sbin/killall KILL", 1, 5);
34774d53c7adSDan Price 
34784d53c7adSDan Price 	/*
34794d53c7adSDan Price 	 * Note the selection of user id's 0, 1 and 15, subsequently
34804d53c7adSDan Price 	 * inverted by -v.  15 is reserved for dladmd.  Yes, this is a
34814d53c7adSDan Price 	 * kludge-- a better policy is needed.
34824d53c7adSDan Price 	 *
34834d53c7adSDan Price 	 * Note that fork_with_timeout will only wait out the 1 second
34844d53c7adSDan Price 	 * "grace time" if pkill actually returns 0.  So if there are
34854d53c7adSDan Price 	 * no matches, this will run to completion much more quickly.
34864d53c7adSDan Price 	 */
34874d53c7adSDan Price 	(void) fork_with_timeout("/usr/bin/pkill -TERM -v -u 0,1,15", 1, 5);
34884d53c7adSDan Price 	(void) fork_with_timeout("/usr/bin/pkill -KILL -v -u 0,1,15", 1, 5);
34894d53c7adSDan Price }
34904d53c7adSDan Price 
34917c478bd9Sstevel@tonic-gate static void
34927c478bd9Sstevel@tonic-gate do_uadmin(void)
34937c478bd9Sstevel@tonic-gate {
3494753a6d45SSherry Moore 	const char * const resetting = "/etc/svc/volatile/resetting";
34954d53c7adSDan Price 	int fd;
34967c478bd9Sstevel@tonic-gate 	struct statvfs vfs;
34974d53c7adSDan Price 	time_t now;
34984d53c7adSDan Price 	struct tm nowtm;
34994d53c7adSDan Price 	char down_buf[256], time_buf[256];
3500753a6d45SSherry Moore 	uintptr_t mdep;
3501753a6d45SSherry Moore #if defined(__i386)
3502753a6d45SSherry Moore 	grub_boot_args_t fbarg;
3503753a6d45SSherry Moore #endif	/* __i386 */
35047c478bd9Sstevel@tonic-gate 
3505753a6d45SSherry Moore 	mdep = NULL;
35067c478bd9Sstevel@tonic-gate 	fd = creat(resetting, 0777);
35077c478bd9Sstevel@tonic-gate 	if (fd >= 0)
35087c478bd9Sstevel@tonic-gate 		startd_close(fd);
35097c478bd9Sstevel@tonic-gate 	else
35107c478bd9Sstevel@tonic-gate 		uu_warn("Could not create \"%s\"", resetting);
35117c478bd9Sstevel@tonic-gate 
35127c478bd9Sstevel@tonic-gate 	/* Kill dhcpagent if we're not using nfs for root */
35137c478bd9Sstevel@tonic-gate 	if ((statvfs("/", &vfs) == 0) &&
35147c478bd9Sstevel@tonic-gate 	    (strncmp(vfs.f_basetype, "nfs", sizeof ("nfs") - 1) != 0))
35154d53c7adSDan Price 		fork_with_timeout("/usr/bin/pkill -x -u 0 dhcpagent", 0, 5);
35167c478bd9Sstevel@tonic-gate 
35174d53c7adSDan Price 	/*
35184d53c7adSDan Price 	 * Call sync(2) now, before we kill off user processes.  This takes
35194d53c7adSDan Price 	 * advantage of the several seconds of pause we have before the
35204d53c7adSDan Price 	 * killalls are done.  Time we can make good use of to get pages
35214d53c7adSDan Price 	 * moving out to disk.
35224d53c7adSDan Price 	 *
35234d53c7adSDan Price 	 * Inside non-global zones, we don't bother, and it's better not to
35244d53c7adSDan Price 	 * anyway, since sync(2) can have system-wide impact.
35254d53c7adSDan Price 	 */
35264d53c7adSDan Price 	if (getzoneid() == 0)
35277c478bd9Sstevel@tonic-gate 		sync();
35287c478bd9Sstevel@tonic-gate 
35294d53c7adSDan Price 	kill_user_procs();
35307c478bd9Sstevel@tonic-gate 
35314d53c7adSDan Price 	/*
35324d53c7adSDan Price 	 * Note that this must come after the killing of user procs, since
35334d53c7adSDan Price 	 * killall relies on utmpx, and this command affects the contents of
35344d53c7adSDan Price 	 * said file.
35354d53c7adSDan Price 	 */
35364d53c7adSDan Price 	if (access("/usr/lib/acct/closewtmp", X_OK) == 0)
35374d53c7adSDan Price 		fork_with_timeout("/usr/lib/acct/closewtmp", 0, 5);
35384d53c7adSDan Price 
35394d53c7adSDan Price 	/*
35404d53c7adSDan Price 	 * For patches which may be installed as the system is shutting
35414d53c7adSDan Price 	 * down, we need to ensure, one more time, that the boot archive
35424d53c7adSDan Price 	 * really is up to date.
35434d53c7adSDan Price 	 */
35444d53c7adSDan Price 	if (getzoneid() == 0 && access("/usr/sbin/bootadm", X_OK) == 0)
35454d53c7adSDan Price 		fork_with_timeout("/usr/sbin/bootadm -ea update_all", 0, 3600);
35464d53c7adSDan Price 
35477f84ffd0SSherry Moore 	/*
35487f84ffd0SSherry Moore 	 * Right now, fast reboot is supported only on i386.
35497f84ffd0SSherry Moore 	 * scf_is_fastboot_default() should take care of it.
35507f84ffd0SSherry Moore 	 * If somehow we got there on unsupported platform -
35517f84ffd0SSherry Moore 	 * print warning and fall back to regular reboot.
35527f84ffd0SSherry Moore 	 */
35537f84ffd0SSherry Moore 	if (halting == AD_FASTREBOOT) {
35547f84ffd0SSherry Moore #if defined(__i386)
35557f84ffd0SSherry Moore 		int rc;
35567f84ffd0SSherry Moore 
35577f84ffd0SSherry Moore 		if ((rc = grub_get_boot_args(&fbarg, NULL,
35587f84ffd0SSherry Moore 		    GRUB_ENTRY_DEFAULT)) == 0) {
35597f84ffd0SSherry Moore 			mdep = (uintptr_t)&fbarg.gba_bootargs;
35607f84ffd0SSherry Moore 		} else {
35617f84ffd0SSherry Moore 			/*
35627f84ffd0SSherry Moore 			 * Failed to read GRUB menu, fall back to normal reboot
35637f84ffd0SSherry Moore 			 */
35647f84ffd0SSherry Moore 			halting = AD_BOOT;
35657f84ffd0SSherry Moore 			uu_warn("Failed to process GRUB menu entry "
35667f84ffd0SSherry Moore 			    "for fast reboot.\n\t%s\n"
35677f84ffd0SSherry Moore 			    "Falling back to regular reboot.\n",
35687f84ffd0SSherry Moore 			    grub_strerror(rc));
35697f84ffd0SSherry Moore 		}
35707f84ffd0SSherry Moore #else	/* __i386 */
35717f84ffd0SSherry Moore 		halting = AD_BOOT;
35727f84ffd0SSherry Moore 		uu_warn("Fast reboot configured, but not supported by "
35737f84ffd0SSherry Moore 		    "this ISA\n");
35747f84ffd0SSherry Moore #endif	/* __i386 */
35757f84ffd0SSherry Moore 	}
35767f84ffd0SSherry Moore 
35774d53c7adSDan Price 	fork_with_timeout("/sbin/umountall -l", 0, 5);
35784d53c7adSDan Price 	fork_with_timeout("/sbin/umount /tmp /var/adm /var/run /var "
35794d53c7adSDan Price 	    ">/dev/null 2>&1", 0, 5);
35804d53c7adSDan Price 
35814d53c7adSDan Price 	/*
35824d53c7adSDan Price 	 * Try to get to consistency for whatever UFS filesystems are left.
35834d53c7adSDan Price 	 * This is pretty expensive, so we save it for the end in the hopes of
35844d53c7adSDan Price 	 * minimizing what it must do.  The other option would be to start in
35854d53c7adSDan Price 	 * parallel with the killall's, but lockfs tends to throw out much more
35864d53c7adSDan Price 	 * than is needed, and so subsequent commands (like umountall) take a
35874d53c7adSDan Price 	 * long time to get going again.
35884d53c7adSDan Price 	 *
35894d53c7adSDan Price 	 * Inside of zones, we don't bother, since we're not about to terminate
35904d53c7adSDan Price 	 * the whole OS instance.
35914d53c7adSDan Price 	 *
35924d53c7adSDan Price 	 * On systems using only ZFS, this call to lockfs -fa is a no-op.
35934d53c7adSDan Price 	 */
35944d53c7adSDan Price 	if (getzoneid() == 0) {
35954d53c7adSDan Price 		if (access("/usr/sbin/lockfs", X_OK) == 0)
35964d53c7adSDan Price 			fork_with_timeout("/usr/sbin/lockfs -fa", 0, 30);
35974d53c7adSDan Price 
35984d53c7adSDan Price 		sync();	/* once more, with feeling */
35994d53c7adSDan Price 	}
36004d53c7adSDan Price 
36014d53c7adSDan Price 	fork_with_timeout("/sbin/umount /usr >/dev/null 2>&1", 0, 5);
36024d53c7adSDan Price 
36034d53c7adSDan Price 	/*
36044d53c7adSDan Price 	 * Construct and emit the last words from userland:
36054d53c7adSDan Price 	 * "<timestamp> The system is down.  Shutdown took <N> seconds."
36064d53c7adSDan Price 	 *
36074d53c7adSDan Price 	 * Normally we'd use syslog, but with /var and other things
36084d53c7adSDan Price 	 * potentially gone, try to minimize the external dependencies.
36094d53c7adSDan Price 	 */
36104d53c7adSDan Price 	now = time(NULL);
36114d53c7adSDan Price 	(void) localtime_r(&now, &nowtm);
36124d53c7adSDan Price 
36134d53c7adSDan Price 	if (strftime(down_buf, sizeof (down_buf),
36144d53c7adSDan Price 	    "%b %e %T The system is down.", &nowtm) == 0) {
36154d53c7adSDan Price 		(void) strlcpy(down_buf, "The system is down.",
36164d53c7adSDan Price 		    sizeof (down_buf));
36174d53c7adSDan Price 	}
36184d53c7adSDan Price 
36194d53c7adSDan Price 	if (halting_time != 0 && halting_time <= now) {
36204d53c7adSDan Price 		(void) snprintf(time_buf, sizeof (time_buf),
36214d53c7adSDan Price 		    "  Shutdown took %lu seconds.", now - halting_time);
36224d53c7adSDan Price 	} else {
36234d53c7adSDan Price 		time_buf[0] = '\0';
36244d53c7adSDan Price 	}
36254d53c7adSDan Price 	(void) printf("%s%s\n", down_buf, time_buf);
36267c478bd9Sstevel@tonic-gate 
3627753a6d45SSherry Moore 	(void) uadmin(A_SHUTDOWN, halting, mdep);
36287c478bd9Sstevel@tonic-gate 	uu_warn("uadmin() failed");
36297c478bd9Sstevel@tonic-gate 
3630753a6d45SSherry Moore #if defined(__i386)
3631753a6d45SSherry Moore 	/* uadmin fail, cleanup grub_boot_args */
3632753a6d45SSherry Moore 	if (halting == AD_FASTREBOOT)
3633753a6d45SSherry Moore 		grub_cleanup_boot_args(&fbarg);
3634753a6d45SSherry Moore #endif	/* __i386 */
3635753a6d45SSherry Moore 
36367c478bd9Sstevel@tonic-gate 	if (remove(resetting) != 0 && errno != ENOENT)
36377c478bd9Sstevel@tonic-gate 		uu_warn("Could not remove \"%s\"", resetting);
36387c478bd9Sstevel@tonic-gate }
36397c478bd9Sstevel@tonic-gate 
36407c478bd9Sstevel@tonic-gate /*
36417c478bd9Sstevel@tonic-gate  * If any of the up_svcs[] are online or satisfiable, return true.  If they are
36427c478bd9Sstevel@tonic-gate  * all missing, disabled, in maintenance, or unsatisfiable, return false.
36437c478bd9Sstevel@tonic-gate  */
36447c478bd9Sstevel@tonic-gate boolean_t
36457c478bd9Sstevel@tonic-gate can_come_up(void)
36467c478bd9Sstevel@tonic-gate {
36477c478bd9Sstevel@tonic-gate 	int i;
36487c478bd9Sstevel@tonic-gate 
3649*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
36507c478bd9Sstevel@tonic-gate 
36517c478bd9Sstevel@tonic-gate 	/*
36527c478bd9Sstevel@tonic-gate 	 * If we are booting to single user (boot -s),
36537c478bd9Sstevel@tonic-gate 	 * SCF_MILESTONE_SINGLE_USER is needed to come up because startd
36547c478bd9Sstevel@tonic-gate 	 * spawns sulogin after single-user is online (see specials.c).
36557c478bd9Sstevel@tonic-gate 	 */
36567c478bd9Sstevel@tonic-gate 	i = (booting_to_single_user ? 0 : 1);
36577c478bd9Sstevel@tonic-gate 
36587c478bd9Sstevel@tonic-gate 	for (; up_svcs[i] != NULL; ++i) {
36597c478bd9Sstevel@tonic-gate 		if (up_svcs_p[i] == NULL) {
36607c478bd9Sstevel@tonic-gate 			up_svcs_p[i] = vertex_get_by_name(up_svcs[i]);
36617c478bd9Sstevel@tonic-gate 
36627c478bd9Sstevel@tonic-gate 			if (up_svcs_p[i] == NULL)
36637c478bd9Sstevel@tonic-gate 				continue;
36647c478bd9Sstevel@tonic-gate 		}
36657c478bd9Sstevel@tonic-gate 
36667c478bd9Sstevel@tonic-gate 		/*
36677c478bd9Sstevel@tonic-gate 		 * Ignore unconfigured services (the ones that have been
36687c478bd9Sstevel@tonic-gate 		 * mentioned in a dependency from other services, but do
36697c478bd9Sstevel@tonic-gate 		 * not exist in the repository).  Services which exist
36707c478bd9Sstevel@tonic-gate 		 * in the repository but don't have general/enabled
36717c478bd9Sstevel@tonic-gate 		 * property will be also ignored.
36727c478bd9Sstevel@tonic-gate 		 */
36737c478bd9Sstevel@tonic-gate 		if (!(up_svcs_p[i]->gv_flags & GV_CONFIGURED))
36747c478bd9Sstevel@tonic-gate 			continue;
36757c478bd9Sstevel@tonic-gate 
36767c478bd9Sstevel@tonic-gate 		switch (up_svcs_p[i]->gv_state) {
36777c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_ONLINE:
36787c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_DEGRADED:
36797c478bd9Sstevel@tonic-gate 			/*
36807c478bd9Sstevel@tonic-gate 			 * Deactivate verbose boot once a login service has been
36817c478bd9Sstevel@tonic-gate 			 * reached.
36827c478bd9Sstevel@tonic-gate 			 */
36837c478bd9Sstevel@tonic-gate 			st->st_log_login_reached = 1;
36847c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
36857c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_UNINIT:
36867c478bd9Sstevel@tonic-gate 			return (B_TRUE);
36877c478bd9Sstevel@tonic-gate 
36887c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_OFFLINE:
36897c478bd9Sstevel@tonic-gate 			if (instance_satisfied(up_svcs_p[i], B_TRUE) != -1)
36907c478bd9Sstevel@tonic-gate 				return (B_TRUE);
36917c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
36927c478bd9Sstevel@tonic-gate 			    "can_come_up(): %s is unsatisfiable.\n",
36937c478bd9Sstevel@tonic-gate 			    up_svcs_p[i]->gv_name);
36947c478bd9Sstevel@tonic-gate 			continue;
36957c478bd9Sstevel@tonic-gate 
36967c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_DISABLED:
36977c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_MAINT:
36987c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
36997c478bd9Sstevel@tonic-gate 			    "can_come_up(): %s is in state %s.\n",
37007c478bd9Sstevel@tonic-gate 			    up_svcs_p[i]->gv_name,
37017c478bd9Sstevel@tonic-gate 			    instance_state_str[up_svcs_p[i]->gv_state]);
37027c478bd9Sstevel@tonic-gate 			continue;
37037c478bd9Sstevel@tonic-gate 
37047c478bd9Sstevel@tonic-gate 		default:
37057c478bd9Sstevel@tonic-gate #ifndef NDEBUG
37067c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex state %d.\n",
37077c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__, up_svcs_p[i]->gv_state);
37087c478bd9Sstevel@tonic-gate #endif
37097c478bd9Sstevel@tonic-gate 			abort();
37107c478bd9Sstevel@tonic-gate 		}
37117c478bd9Sstevel@tonic-gate 	}
37127c478bd9Sstevel@tonic-gate 
37137c478bd9Sstevel@tonic-gate 	/*
37147c478bd9Sstevel@tonic-gate 	 * In the seed repository, console-login is unsatisfiable because
37157c478bd9Sstevel@tonic-gate 	 * services are missing.  To behave correctly in that case we don't want
37167c478bd9Sstevel@tonic-gate 	 * to return false until manifest-import is online.
37177c478bd9Sstevel@tonic-gate 	 */
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	if (manifest_import_p == NULL) {
37207c478bd9Sstevel@tonic-gate 		manifest_import_p = vertex_get_by_name(manifest_import);
37217c478bd9Sstevel@tonic-gate 
37227c478bd9Sstevel@tonic-gate 		if (manifest_import_p == NULL)
37237c478bd9Sstevel@tonic-gate 			return (B_FALSE);
37247c478bd9Sstevel@tonic-gate 	}
37257c478bd9Sstevel@tonic-gate 
37267c478bd9Sstevel@tonic-gate 	switch (manifest_import_p->gv_state) {
37277c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_ONLINE:
37287c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_DEGRADED:
37297c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_DISABLED:
37307c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_MAINT:
37317c478bd9Sstevel@tonic-gate 		break;
37327c478bd9Sstevel@tonic-gate 
37337c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_OFFLINE:
37347c478bd9Sstevel@tonic-gate 		if (instance_satisfied(manifest_import_p, B_TRUE) == -1)
37357c478bd9Sstevel@tonic-gate 			break;
37367c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
37377c478bd9Sstevel@tonic-gate 
37387c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_UNINIT:
37397c478bd9Sstevel@tonic-gate 		return (B_TRUE);
37407c478bd9Sstevel@tonic-gate 	}
37417c478bd9Sstevel@tonic-gate 
37427c478bd9Sstevel@tonic-gate 	return (B_FALSE);
37437c478bd9Sstevel@tonic-gate }
37447c478bd9Sstevel@tonic-gate 
37457c478bd9Sstevel@tonic-gate /*
37467c478bd9Sstevel@tonic-gate  * Runs sulogin.  Returns
37477c478bd9Sstevel@tonic-gate  *   0 - success
37487c478bd9Sstevel@tonic-gate  *   EALREADY - sulogin is already running
37497c478bd9Sstevel@tonic-gate  *   EBUSY - console-login is running
37507c478bd9Sstevel@tonic-gate  */
37517c478bd9Sstevel@tonic-gate static int
37527c478bd9Sstevel@tonic-gate run_sulogin(const char *msg)
37537c478bd9Sstevel@tonic-gate {
37547c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
37557c478bd9Sstevel@tonic-gate 
3756*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
37577c478bd9Sstevel@tonic-gate 
37587c478bd9Sstevel@tonic-gate 	if (sulogin_running)
37597c478bd9Sstevel@tonic-gate 		return (EALREADY);
37607c478bd9Sstevel@tonic-gate 
37617c478bd9Sstevel@tonic-gate 	v = vertex_get_by_name(console_login_fmri);
37627c478bd9Sstevel@tonic-gate 	if (v != NULL && inst_running(v))
37637c478bd9Sstevel@tonic-gate 		return (EBUSY);
37647c478bd9Sstevel@tonic-gate 
37657c478bd9Sstevel@tonic-gate 	sulogin_running = B_TRUE;
37667c478bd9Sstevel@tonic-gate 
37677c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
37687c478bd9Sstevel@tonic-gate 
37697c478bd9Sstevel@tonic-gate 	fork_sulogin(B_FALSE, msg);
37707c478bd9Sstevel@tonic-gate 
37717c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
37727c478bd9Sstevel@tonic-gate 
37737c478bd9Sstevel@tonic-gate 	sulogin_running = B_FALSE;
37747c478bd9Sstevel@tonic-gate 
37757c478bd9Sstevel@tonic-gate 	if (console_login_ready) {
37767c478bd9Sstevel@tonic-gate 		v = vertex_get_by_name(console_login_fmri);
37777c478bd9Sstevel@tonic-gate 
3778845e9415SRenaud Manus 		if (v != NULL && v->gv_state == RESTARTER_STATE_OFFLINE) {
37797c478bd9Sstevel@tonic-gate 			if (v->gv_start_f == NULL)
37807c478bd9Sstevel@tonic-gate 				vertex_send_event(v,
37817c478bd9Sstevel@tonic-gate 				    RESTARTER_EVENT_TYPE_START);
37827c478bd9Sstevel@tonic-gate 			else
37837c478bd9Sstevel@tonic-gate 				v->gv_start_f(v);
37847c478bd9Sstevel@tonic-gate 		}
37857c478bd9Sstevel@tonic-gate 
37867c478bd9Sstevel@tonic-gate 		console_login_ready = B_FALSE;
37877c478bd9Sstevel@tonic-gate 	}
37887c478bd9Sstevel@tonic-gate 
37897c478bd9Sstevel@tonic-gate 	return (0);
37907c478bd9Sstevel@tonic-gate }
37917c478bd9Sstevel@tonic-gate 
37927c478bd9Sstevel@tonic-gate /*
37937c478bd9Sstevel@tonic-gate  * The sulogin thread runs sulogin while can_come_up() is false.  run_sulogin()
37947c478bd9Sstevel@tonic-gate  * keeps sulogin from stepping on console-login's toes.
37957c478bd9Sstevel@tonic-gate  */
37967c478bd9Sstevel@tonic-gate /* ARGSUSED */
37977c478bd9Sstevel@tonic-gate static void *
37987c478bd9Sstevel@tonic-gate sulogin_thread(void *unused)
37997c478bd9Sstevel@tonic-gate {
38007c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
38017c478bd9Sstevel@tonic-gate 
38027c478bd9Sstevel@tonic-gate 	assert(sulogin_thread_running);
38037c478bd9Sstevel@tonic-gate 
38043eae19d9Swesolows 	do {
38057c478bd9Sstevel@tonic-gate 		(void) run_sulogin("Console login service(s) cannot run\n");
38063eae19d9Swesolows 	} while (!can_come_up());
38077c478bd9Sstevel@tonic-gate 
38087c478bd9Sstevel@tonic-gate 	sulogin_thread_running = B_FALSE;
38097c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
38107c478bd9Sstevel@tonic-gate 
38117c478bd9Sstevel@tonic-gate 	return (NULL);
38127c478bd9Sstevel@tonic-gate }
38137c478bd9Sstevel@tonic-gate 
38147c478bd9Sstevel@tonic-gate /* ARGSUSED */
38157c478bd9Sstevel@tonic-gate void *
38167c478bd9Sstevel@tonic-gate single_user_thread(void *unused)
38177c478bd9Sstevel@tonic-gate {
38187c478bd9Sstevel@tonic-gate 	uint_t left;
38197c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
38207c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
38217c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
38227c478bd9Sstevel@tonic-gate 	scf_value_t *val;
38237c478bd9Sstevel@tonic-gate 	const char *msg;
38247c478bd9Sstevel@tonic-gate 	char *buf;
38257c478bd9Sstevel@tonic-gate 	int r;
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&single_user_thread_lock);
38287c478bd9Sstevel@tonic-gate 	single_user_thread_count++;
38297c478bd9Sstevel@tonic-gate 
38304d53c7adSDan Price 	if (!booting_to_single_user)
38314d53c7adSDan Price 		kill_user_procs();
38327c478bd9Sstevel@tonic-gate 
38337c478bd9Sstevel@tonic-gate 	if (go_single_user_mode || booting_to_single_user) {
38347c478bd9Sstevel@tonic-gate 		msg = "SINGLE USER MODE\n";
38357c478bd9Sstevel@tonic-gate 	} else {
38367c478bd9Sstevel@tonic-gate 		assert(go_to_level1);
38377c478bd9Sstevel@tonic-gate 
38387c478bd9Sstevel@tonic-gate 		fork_rc_script('1', "start", B_TRUE);
38397c478bd9Sstevel@tonic-gate 
38407c478bd9Sstevel@tonic-gate 		uu_warn("The system is ready for administration.\n");
38417c478bd9Sstevel@tonic-gate 
38427c478bd9Sstevel@tonic-gate 		msg = "";
38437c478bd9Sstevel@tonic-gate 	}
38447c478bd9Sstevel@tonic-gate 
38457c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&single_user_thread_lock);
38467c478bd9Sstevel@tonic-gate 
38477c478bd9Sstevel@tonic-gate 	for (;;) {
38487c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&dgraph_lock);
38497c478bd9Sstevel@tonic-gate 		r = run_sulogin(msg);
38507c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
38517c478bd9Sstevel@tonic-gate 		if (r == 0)
38527c478bd9Sstevel@tonic-gate 			break;
38537c478bd9Sstevel@tonic-gate 
38547c478bd9Sstevel@tonic-gate 		assert(r == EALREADY || r == EBUSY);
38557c478bd9Sstevel@tonic-gate 
38567c478bd9Sstevel@tonic-gate 		left = 3;
38577c478bd9Sstevel@tonic-gate 		while (left > 0)
38587c478bd9Sstevel@tonic-gate 			left = sleep(left);
38597c478bd9Sstevel@tonic-gate 	}
38607c478bd9Sstevel@tonic-gate 
38617c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&single_user_thread_lock);
38627c478bd9Sstevel@tonic-gate 
38637c478bd9Sstevel@tonic-gate 	/*
38647c478bd9Sstevel@tonic-gate 	 * If another single user thread has started, let it finish changing
38657c478bd9Sstevel@tonic-gate 	 * the run level.
38667c478bd9Sstevel@tonic-gate 	 */
38677c478bd9Sstevel@tonic-gate 	if (single_user_thread_count > 1) {
38687c478bd9Sstevel@tonic-gate 		single_user_thread_count--;
38697c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&single_user_thread_lock);
38707c478bd9Sstevel@tonic-gate 		return (NULL);
38717c478bd9Sstevel@tonic-gate 	}
38727c478bd9Sstevel@tonic-gate 
38737c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
38747c478bd9Sstevel@tonic-gate 	inst = scf_instance_create(h);
38757c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
38767c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
38777c478bd9Sstevel@tonic-gate 	buf = startd_alloc(max_scf_fmri_size);
38787c478bd9Sstevel@tonic-gate 
38797c478bd9Sstevel@tonic-gate lookup:
38807c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
38817c478bd9Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
38827c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
38837c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
38847c478bd9Sstevel@tonic-gate 			r = libscf_create_self(h);
38857c478bd9Sstevel@tonic-gate 			if (r == 0)
38867c478bd9Sstevel@tonic-gate 				goto lookup;
38877c478bd9Sstevel@tonic-gate 			assert(r == ECONNABORTED);
38887c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
38897c478bd9Sstevel@tonic-gate 
38907c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
38917c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
38927c478bd9Sstevel@tonic-gate 			goto lookup;
38937c478bd9Sstevel@tonic-gate 
38947c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
38957c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
38967c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
38977c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
38987c478bd9Sstevel@tonic-gate 		default:
38997c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
39007c478bd9Sstevel@tonic-gate 		}
39017c478bd9Sstevel@tonic-gate 	}
39027c478bd9Sstevel@tonic-gate 
39037c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
39047c478bd9Sstevel@tonic-gate 
3905eb1a3463STruong Nguyen 	r = scf_instance_delete_prop(inst, SCF_PG_OPTIONS_OVR,
39067c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_MILESTONE);
39077c478bd9Sstevel@tonic-gate 	switch (r) {
39087c478bd9Sstevel@tonic-gate 	case 0:
39097c478bd9Sstevel@tonic-gate 	case ECANCELED:
39107c478bd9Sstevel@tonic-gate 		break;
39117c478bd9Sstevel@tonic-gate 
39127c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
39137c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
39147c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
39157c478bd9Sstevel@tonic-gate 		goto lookup;
39167c478bd9Sstevel@tonic-gate 
39177c478bd9Sstevel@tonic-gate 	case EPERM:
39187c478bd9Sstevel@tonic-gate 	case EACCES:
39197c478bd9Sstevel@tonic-gate 	case EROFS:
39207c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "Could not clear temporary milestone: "
39217c478bd9Sstevel@tonic-gate 		    "%s.\n", strerror(r));
39227c478bd9Sstevel@tonic-gate 		break;
39237c478bd9Sstevel@tonic-gate 
39247c478bd9Sstevel@tonic-gate 	default:
3925eb1a3463STruong Nguyen 		bad_error("scf_instance_delete_prop", r);
39267c478bd9Sstevel@tonic-gate 	}
39277c478bd9Sstevel@tonic-gate 
39287c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
39297c478bd9Sstevel@tonic-gate 
39307c478bd9Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, buf, max_scf_fmri_size);
39317c478bd9Sstevel@tonic-gate 	switch (r) {
39327c478bd9Sstevel@tonic-gate 	case ECANCELED:
39337c478bd9Sstevel@tonic-gate 	case ENOENT:
39347c478bd9Sstevel@tonic-gate 	case EINVAL:
39357c478bd9Sstevel@tonic-gate 		(void) strcpy(buf, "all");
39367c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
39377c478bd9Sstevel@tonic-gate 
39387c478bd9Sstevel@tonic-gate 	case 0:
39397c478bd9Sstevel@tonic-gate 		uu_warn("Returning to milestone %s.\n", buf);
39407c478bd9Sstevel@tonic-gate 		break;
39417c478bd9Sstevel@tonic-gate 
39427c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
39437c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
39447c478bd9Sstevel@tonic-gate 		goto lookup;
39457c478bd9Sstevel@tonic-gate 
39467c478bd9Sstevel@tonic-gate 	default:
39477c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
39487c478bd9Sstevel@tonic-gate 	}
39497c478bd9Sstevel@tonic-gate 
39507c478bd9Sstevel@tonic-gate 	r = dgraph_set_milestone(buf, h, B_FALSE);
39517c478bd9Sstevel@tonic-gate 	switch (r) {
39527c478bd9Sstevel@tonic-gate 	case 0:
39537c478bd9Sstevel@tonic-gate 	case ECONNRESET:
39547c478bd9Sstevel@tonic-gate 	case EALREADY:
39557c478bd9Sstevel@tonic-gate 	case EINVAL:
39567c478bd9Sstevel@tonic-gate 	case ENOENT:
39577c478bd9Sstevel@tonic-gate 		break;
39587c478bd9Sstevel@tonic-gate 
39597c478bd9Sstevel@tonic-gate 	default:
39607c478bd9Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
39617c478bd9Sstevel@tonic-gate 	}
39627c478bd9Sstevel@tonic-gate 
39637c478bd9Sstevel@tonic-gate 	/*
39647c478bd9Sstevel@tonic-gate 	 * See graph_runlevel_changed().
39657c478bd9Sstevel@tonic-gate 	 */
39667c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
39677c478bd9Sstevel@tonic-gate 	utmpx_set_runlevel(target_milestone_as_runlevel(), 'S', B_TRUE);
39687c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
39697c478bd9Sstevel@tonic-gate 
39707c478bd9Sstevel@tonic-gate 	startd_free(buf, max_scf_fmri_size);
39717c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
39727c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
39737c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
39747c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
39757c478bd9Sstevel@tonic-gate 
39767c478bd9Sstevel@tonic-gate 	/*
39777c478bd9Sstevel@tonic-gate 	 * We'll give ourselves 3 seconds to respond to all of the enablings
39787c478bd9Sstevel@tonic-gate 	 * that setting the milestone should have created before checking
39797c478bd9Sstevel@tonic-gate 	 * whether to run sulogin.
39807c478bd9Sstevel@tonic-gate 	 */
39817c478bd9Sstevel@tonic-gate 	left = 3;
39827c478bd9Sstevel@tonic-gate 	while (left > 0)
39837c478bd9Sstevel@tonic-gate 		left = sleep(left);
39847c478bd9Sstevel@tonic-gate 
39857c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
39867c478bd9Sstevel@tonic-gate 	/*
39877c478bd9Sstevel@tonic-gate 	 * Clearing these variables will allow the sulogin thread to run.  We
39887c478bd9Sstevel@tonic-gate 	 * check here in case there aren't any more state updates anytime soon.
39897c478bd9Sstevel@tonic-gate 	 */
39907c478bd9Sstevel@tonic-gate 	go_to_level1 = go_single_user_mode = booting_to_single_user = B_FALSE;
39917c478bd9Sstevel@tonic-gate 	if (!sulogin_thread_running && !can_come_up()) {
39927c478bd9Sstevel@tonic-gate 		(void) startd_thread_create(sulogin_thread, NULL);
39937c478bd9Sstevel@tonic-gate 		sulogin_thread_running = B_TRUE;
39947c478bd9Sstevel@tonic-gate 	}
39957c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
39967c478bd9Sstevel@tonic-gate 	single_user_thread_count--;
39977c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&single_user_thread_lock);
39987c478bd9Sstevel@tonic-gate 	return (NULL);
39997c478bd9Sstevel@tonic-gate }
40007c478bd9Sstevel@tonic-gate 
40017c478bd9Sstevel@tonic-gate 
40027c478bd9Sstevel@tonic-gate /*
40037c478bd9Sstevel@tonic-gate  * Dependency graph operations API.  These are handle-independent thread-safe
40047c478bd9Sstevel@tonic-gate  * graph manipulation functions which are the entry points for the event
40057c478bd9Sstevel@tonic-gate  * threads below.
40067c478bd9Sstevel@tonic-gate  */
40077c478bd9Sstevel@tonic-gate 
40087c478bd9Sstevel@tonic-gate /*
40097c478bd9Sstevel@tonic-gate  * If a configured vertex exists for inst_fmri, return EEXIST.  If no vertex
40107c478bd9Sstevel@tonic-gate  * exists for inst_fmri, add one.  Then fetch the restarter from inst, make
40117c478bd9Sstevel@tonic-gate  * this vertex dependent on it, and send _ADD_INSTANCE to the restarter.
40127c478bd9Sstevel@tonic-gate  * Fetch whether the instance should be enabled from inst and send _ENABLE or
40137c478bd9Sstevel@tonic-gate  * _DISABLE as appropriate.  Finally rummage through inst's dependency
40147c478bd9Sstevel@tonic-gate  * property groups and add vertices and edges as appropriate.  If anything
40157c478bd9Sstevel@tonic-gate  * goes wrong after sending _ADD_INSTANCE, send _ADMIN_MAINT_ON to put the
40167c478bd9Sstevel@tonic-gate  * instance in maintenance.  Don't send _START or _STOP until we get a state
40177c478bd9Sstevel@tonic-gate  * update in case we're being restarted and the service is already running.
40187c478bd9Sstevel@tonic-gate  *
40197c478bd9Sstevel@tonic-gate  * To support booting to a milestone, we must also make sure all dependencies
40207c478bd9Sstevel@tonic-gate  * encountered are configured, if they exist in the repository.
40217c478bd9Sstevel@tonic-gate  *
40227c478bd9Sstevel@tonic-gate  * Returns 0 on success, ECONNABORTED on repository disconnection, EINVAL if
40237c478bd9Sstevel@tonic-gate  * inst_fmri is an invalid (or not canonical) FMRI, ECANCELED if inst is
40247c478bd9Sstevel@tonic-gate  * deleted, or EEXIST if a configured vertex for inst_fmri already exists.
40257c478bd9Sstevel@tonic-gate  */
40267c478bd9Sstevel@tonic-gate int
40277c478bd9Sstevel@tonic-gate dgraph_add_instance(const char *inst_fmri, scf_instance_t *inst,
40287c478bd9Sstevel@tonic-gate     boolean_t lock_graph)
40297c478bd9Sstevel@tonic-gate {
40307c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
40317c478bd9Sstevel@tonic-gate 	int err;
40327c478bd9Sstevel@tonic-gate 
40337c478bd9Sstevel@tonic-gate 	if (strcmp(inst_fmri, SCF_SERVICE_STARTD) == 0)
40347c478bd9Sstevel@tonic-gate 		return (0);
40357c478bd9Sstevel@tonic-gate 
40367c478bd9Sstevel@tonic-gate 	/* Check for a vertex for inst_fmri. */
40377c478bd9Sstevel@tonic-gate 	if (lock_graph) {
40387c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&dgraph_lock);
40397c478bd9Sstevel@tonic-gate 	} else {
4040*53f3aea0SRoger A. Faulkner 		assert(MUTEX_HELD(&dgraph_lock));
40417c478bd9Sstevel@tonic-gate 	}
40427c478bd9Sstevel@tonic-gate 
40437c478bd9Sstevel@tonic-gate 	v = vertex_get_by_name(inst_fmri);
40447c478bd9Sstevel@tonic-gate 
40457c478bd9Sstevel@tonic-gate 	if (v != NULL) {
40467c478bd9Sstevel@tonic-gate 		assert(v->gv_type == GVT_INST);
40477c478bd9Sstevel@tonic-gate 
40487c478bd9Sstevel@tonic-gate 		if (v->gv_flags & GV_CONFIGURED) {
40497c478bd9Sstevel@tonic-gate 			if (lock_graph)
40507c478bd9Sstevel@tonic-gate 				MUTEX_UNLOCK(&dgraph_lock);
40517c478bd9Sstevel@tonic-gate 			return (EEXIST);
40527c478bd9Sstevel@tonic-gate 		}
40537c478bd9Sstevel@tonic-gate 	} else {
40547c478bd9Sstevel@tonic-gate 		/* Add the vertex. */
40557c478bd9Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(inst_fmri, GVT_INST, 0,
40567c478bd9Sstevel@tonic-gate 		    RERR_NONE, &v);
40577c478bd9Sstevel@tonic-gate 		if (err != 0) {
40587c478bd9Sstevel@tonic-gate 			assert(err == EINVAL);
40597c478bd9Sstevel@tonic-gate 			if (lock_graph)
40607c478bd9Sstevel@tonic-gate 				MUTEX_UNLOCK(&dgraph_lock);
40617c478bd9Sstevel@tonic-gate 			return (EINVAL);
40627c478bd9Sstevel@tonic-gate 		}
40637c478bd9Sstevel@tonic-gate 	}
40647c478bd9Sstevel@tonic-gate 
40657c478bd9Sstevel@tonic-gate 	err = configure_vertex(v, inst);
40667c478bd9Sstevel@tonic-gate 
40677c478bd9Sstevel@tonic-gate 	if (lock_graph)
40687c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
40697c478bd9Sstevel@tonic-gate 
40707c478bd9Sstevel@tonic-gate 	return (err);
40717c478bd9Sstevel@tonic-gate }
40727c478bd9Sstevel@tonic-gate 
40737c478bd9Sstevel@tonic-gate /*
40747c478bd9Sstevel@tonic-gate  * Locate the vertex for this property group's instance.  If it doesn't exist
40757c478bd9Sstevel@tonic-gate  * or is unconfigured, call dgraph_add_instance() & return.  Otherwise fetch
40767c478bd9Sstevel@tonic-gate  * the restarter for the instance, and if it has changed, send
40777c478bd9Sstevel@tonic-gate  * _REMOVE_INSTANCE to the old restarter, remove the dependency, make sure the
40787c478bd9Sstevel@tonic-gate  * new restarter has a vertex, add a new dependency, and send _ADD_INSTANCE to
40797c478bd9Sstevel@tonic-gate  * the new restarter.  Then fetch whether the instance should be enabled, and
40807c478bd9Sstevel@tonic-gate  * if it is different from what we had, or if we changed the restarter, send
40817c478bd9Sstevel@tonic-gate  * the appropriate _ENABLE or _DISABLE command.
40827c478bd9Sstevel@tonic-gate  *
40837c478bd9Sstevel@tonic-gate  * Returns 0 on success, ENOTSUP if the pg's parent is not an instance,
40847c478bd9Sstevel@tonic-gate  * ECONNABORTED on repository disconnection, ECANCELED if the instance is
40857c478bd9Sstevel@tonic-gate  * deleted, or -1 if the instance's general property group is deleted or if
40867c478bd9Sstevel@tonic-gate  * its enabled property is misconfigured.
40877c478bd9Sstevel@tonic-gate  */
40887c478bd9Sstevel@tonic-gate static int
40897c478bd9Sstevel@tonic-gate dgraph_update_general(scf_propertygroup_t *pg)
40907c478bd9Sstevel@tonic-gate {
40917c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
40927c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
40937c478bd9Sstevel@tonic-gate 	char *fmri;
40947c478bd9Sstevel@tonic-gate 	char *restarter_fmri;
40957c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
40967c478bd9Sstevel@tonic-gate 	int err;
40977c478bd9Sstevel@tonic-gate 	int enabled, enabled_ovr;
40987c478bd9Sstevel@tonic-gate 	int oldflags;
40997c478bd9Sstevel@tonic-gate 
41007c478bd9Sstevel@tonic-gate 	/* Find the vertex for this service */
41017c478bd9Sstevel@tonic-gate 	h = scf_pg_handle(pg);
41027c478bd9Sstevel@tonic-gate 
41037c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
41047c478bd9Sstevel@tonic-gate 
41057c478bd9Sstevel@tonic-gate 	if (scf_pg_get_parent_instance(pg, inst) != 0) {
41067c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
41077c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
41087c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
41097c478bd9Sstevel@tonic-gate 
41107c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
41117c478bd9Sstevel@tonic-gate 		default:
41127c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
41137c478bd9Sstevel@tonic-gate 
41147c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
41157c478bd9Sstevel@tonic-gate 			return (0);
41167c478bd9Sstevel@tonic-gate 
41177c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
41187c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_parent_instance", scf_error());
41197c478bd9Sstevel@tonic-gate 		}
41207c478bd9Sstevel@tonic-gate 	}
41217c478bd9Sstevel@tonic-gate 
41227c478bd9Sstevel@tonic-gate 	err = libscf_instance_get_fmri(inst, &fmri);
41237c478bd9Sstevel@tonic-gate 	switch (err) {
41247c478bd9Sstevel@tonic-gate 	case 0:
41257c478bd9Sstevel@tonic-gate 		break;
41267c478bd9Sstevel@tonic-gate 
41277c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
41287c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
41297c478bd9Sstevel@tonic-gate 		return (ECONNABORTED);
41307c478bd9Sstevel@tonic-gate 
41317c478bd9Sstevel@tonic-gate 	case ECANCELED:
41327c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
41337c478bd9Sstevel@tonic-gate 		return (0);
41347c478bd9Sstevel@tonic-gate 
41357c478bd9Sstevel@tonic-gate 	default:
41367c478bd9Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", err);
41377c478bd9Sstevel@tonic-gate 	}
41387c478bd9Sstevel@tonic-gate 
41397c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG,
41407c478bd9Sstevel@tonic-gate 	    "Graph engine: Reloading general properties for %s.\n", fmri);
41417c478bd9Sstevel@tonic-gate 
41427c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
41437c478bd9Sstevel@tonic-gate 
41447c478bd9Sstevel@tonic-gate 	v = vertex_get_by_name(fmri);
41457c478bd9Sstevel@tonic-gate 	if (v == NULL || !(v->gv_flags & GV_CONFIGURED)) {
41467c478bd9Sstevel@tonic-gate 		/* Will get the up-to-date properties. */
41477c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
41487c478bd9Sstevel@tonic-gate 		err = dgraph_add_instance(fmri, inst, B_TRUE);
41497c478bd9Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
41507c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
41517c478bd9Sstevel@tonic-gate 		return (err == ECANCELED ? 0 : err);
41527c478bd9Sstevel@tonic-gate 	}
41537c478bd9Sstevel@tonic-gate 
41547c478bd9Sstevel@tonic-gate 	/* Read enabled & restarter from repository. */
41557c478bd9Sstevel@tonic-gate 	restarter_fmri = startd_alloc(max_scf_value_size);
41567c478bd9Sstevel@tonic-gate 	err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled,
41577c478bd9Sstevel@tonic-gate 	    &enabled_ovr, &restarter_fmri);
41587c478bd9Sstevel@tonic-gate 	if (err != 0 || enabled == -1) {
41597c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
41607c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
41617c478bd9Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
41627c478bd9Sstevel@tonic-gate 
41637c478bd9Sstevel@tonic-gate 		switch (err) {
41647c478bd9Sstevel@tonic-gate 		case ENOENT:
41657c478bd9Sstevel@tonic-gate 		case 0:
41667c478bd9Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
41677c478bd9Sstevel@tonic-gate 			return (-1);
41687c478bd9Sstevel@tonic-gate 
41697c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
41707c478bd9Sstevel@tonic-gate 		case ECANCELED:
41717c478bd9Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
41727c478bd9Sstevel@tonic-gate 			return (err);
41737c478bd9Sstevel@tonic-gate 
41747c478bd9Sstevel@tonic-gate 		default:
41757c478bd9Sstevel@tonic-gate 			bad_error("libscf_get_basic_instance_data", err);
41767c478bd9Sstevel@tonic-gate 		}
41777c478bd9Sstevel@tonic-gate 	}
41787c478bd9Sstevel@tonic-gate 
41797c478bd9Sstevel@tonic-gate 	oldflags = v->gv_flags;
41807c478bd9Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) |
41817c478bd9Sstevel@tonic-gate 	    (enabled ? GV_ENBLD_NOOVR : 0);
41827c478bd9Sstevel@tonic-gate 
41837c478bd9Sstevel@tonic-gate 	if (enabled_ovr != -1)
41847c478bd9Sstevel@tonic-gate 		enabled = enabled_ovr;
41857c478bd9Sstevel@tonic-gate 
41867c478bd9Sstevel@tonic-gate 	/*
41877c478bd9Sstevel@tonic-gate 	 * If GV_ENBLD_NOOVR has changed, then we need to re-evaluate the
41887c478bd9Sstevel@tonic-gate 	 * subgraph.
41897c478bd9Sstevel@tonic-gate 	 */
41907c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE && v->gv_flags != oldflags)
41917c478bd9Sstevel@tonic-gate 		(void) eval_subgraph(v, h);
41927c478bd9Sstevel@tonic-gate 
41937c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
41947c478bd9Sstevel@tonic-gate 
41957c478bd9Sstevel@tonic-gate 	/* Ignore restarter change for now. */
41967c478bd9Sstevel@tonic-gate 
41977c478bd9Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_value_size);
41987c478bd9Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
41997c478bd9Sstevel@tonic-gate 
42007c478bd9Sstevel@tonic-gate 	/*
42017c478bd9Sstevel@tonic-gate 	 * Always send _ENABLE or _DISABLE.  We could avoid this if the
42027c478bd9Sstevel@tonic-gate 	 * restarter didn't change and the enabled value didn't change, but
42037c478bd9Sstevel@tonic-gate 	 * that's not easy to check and improbable anyway, so we'll just do
42047c478bd9Sstevel@tonic-gate 	 * this.
42057c478bd9Sstevel@tonic-gate 	 */
42067c478bd9Sstevel@tonic-gate 	graph_enable_by_vertex(v, enabled, 1);
42077c478bd9Sstevel@tonic-gate 
42087c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
42097c478bd9Sstevel@tonic-gate 
42107c478bd9Sstevel@tonic-gate 	return (0);
42117c478bd9Sstevel@tonic-gate }
42127c478bd9Sstevel@tonic-gate 
42137c478bd9Sstevel@tonic-gate /*
42147c478bd9Sstevel@tonic-gate  * Delete all of the property group dependencies of v, update inst's running
42157c478bd9Sstevel@tonic-gate  * snapshot, and add the dependencies in the new snapshot.  If any of the new
42167c478bd9Sstevel@tonic-gate  * dependencies would create a cycle, send _ADMIN_MAINT_ON.  Otherwise
42177c478bd9Sstevel@tonic-gate  * reevaluate v's dependencies, send _START or _STOP as appropriate, and do
42187c478bd9Sstevel@tonic-gate  * the same for v's dependents.
42197c478bd9Sstevel@tonic-gate  *
42207c478bd9Sstevel@tonic-gate  * Returns
42217c478bd9Sstevel@tonic-gate  *   0 - success
42227c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
42237c478bd9Sstevel@tonic-gate  *   ECANCELED - inst was deleted
42247c478bd9Sstevel@tonic-gate  *   EINVAL - inst is invalid (e.g., missing general/enabled)
42257c478bd9Sstevel@tonic-gate  *   -1 - libscf_snapshots_refresh() failed
42267c478bd9Sstevel@tonic-gate  */
42277c478bd9Sstevel@tonic-gate static int
42287c478bd9Sstevel@tonic-gate dgraph_refresh_instance(graph_vertex_t *v, scf_instance_t *inst)
42297c478bd9Sstevel@tonic-gate {
42307c478bd9Sstevel@tonic-gate 	int r;
42317c478bd9Sstevel@tonic-gate 	int enabled;
42327c478bd9Sstevel@tonic-gate 
4233*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
42347c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
42357c478bd9Sstevel@tonic-gate 
42367c478bd9Sstevel@tonic-gate 	/* Only refresh services with valid general/enabled properties. */
42377c478bd9Sstevel@tonic-gate 	r = libscf_get_basic_instance_data(scf_instance_handle(inst), inst,
42387c478bd9Sstevel@tonic-gate 	    v->gv_name, &enabled, NULL, NULL);
42397c478bd9Sstevel@tonic-gate 	switch (r) {
42407c478bd9Sstevel@tonic-gate 	case 0:
42417c478bd9Sstevel@tonic-gate 		break;
42427c478bd9Sstevel@tonic-gate 
42437c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
42447c478bd9Sstevel@tonic-gate 	case ECANCELED:
42457c478bd9Sstevel@tonic-gate 		return (r);
42467c478bd9Sstevel@tonic-gate 
42477c478bd9Sstevel@tonic-gate 	case ENOENT:
42487c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
42497c478bd9Sstevel@tonic-gate 		    "Ignoring %s because it has no general property group.\n",
42507c478bd9Sstevel@tonic-gate 		    v->gv_name);
42517c478bd9Sstevel@tonic-gate 		return (EINVAL);
42527c478bd9Sstevel@tonic-gate 
42537c478bd9Sstevel@tonic-gate 	default:
42547c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_basic_instance_data", r);
42557c478bd9Sstevel@tonic-gate 	}
42567c478bd9Sstevel@tonic-gate 
42577c478bd9Sstevel@tonic-gate 	if (enabled == -1)
42587c478bd9Sstevel@tonic-gate 		return (EINVAL);
42597c478bd9Sstevel@tonic-gate 
42607c478bd9Sstevel@tonic-gate 	r = libscf_snapshots_refresh(inst, v->gv_name);
42617c478bd9Sstevel@tonic-gate 	if (r != 0) {
42627c478bd9Sstevel@tonic-gate 		if (r != -1)
42637c478bd9Sstevel@tonic-gate 			bad_error("libscf_snapshots_refresh", r);
42647c478bd9Sstevel@tonic-gate 
42657c478bd9Sstevel@tonic-gate 		/* error logged */
42667c478bd9Sstevel@tonic-gate 		return (r);
42677c478bd9Sstevel@tonic-gate 	}
42687c478bd9Sstevel@tonic-gate 
42697c478bd9Sstevel@tonic-gate 	r = refresh_vertex(v, inst);
42707c478bd9Sstevel@tonic-gate 	if (r != 0 && r != ECONNABORTED)
42717c478bd9Sstevel@tonic-gate 		bad_error("refresh_vertex", r);
42727c478bd9Sstevel@tonic-gate 	return (r);
42737c478bd9Sstevel@tonic-gate }
42747c478bd9Sstevel@tonic-gate 
42757c478bd9Sstevel@tonic-gate /*
4276aca380d7SRenaud Manus  * Returns true only if none of this service's dependents are 'up' -- online
4277aca380d7SRenaud Manus  * or degraded (offline is considered down in this situation). This function
4278aca380d7SRenaud Manus  * is somehow similar to is_nonsubgraph_leaf() but works on subtrees.
4279aca380d7SRenaud Manus  */
4280aca380d7SRenaud Manus static boolean_t
4281aca380d7SRenaud Manus insubtree_dependents_down(graph_vertex_t *v)
4282aca380d7SRenaud Manus {
4283aca380d7SRenaud Manus 	graph_vertex_t *vv;
4284aca380d7SRenaud Manus 	graph_edge_t *e;
4285aca380d7SRenaud Manus 
4286*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
4287aca380d7SRenaud Manus 
4288aca380d7SRenaud Manus 	for (e = uu_list_first(v->gv_dependents); e != NULL;
4289aca380d7SRenaud Manus 	    e = uu_list_next(v->gv_dependents, e)) {
4290aca380d7SRenaud Manus 		vv = e->ge_vertex;
4291aca380d7SRenaud Manus 		if (vv->gv_type == GVT_INST) {
4292aca380d7SRenaud Manus 			if ((vv->gv_flags & GV_CONFIGURED) == 0)
4293aca380d7SRenaud Manus 				continue;
4294aca380d7SRenaud Manus 
4295aca380d7SRenaud Manus 			if ((vv->gv_flags & GV_TOOFFLINE) == 0)
4296aca380d7SRenaud Manus 				continue;
4297aca380d7SRenaud Manus 
4298aca380d7SRenaud Manus 			if ((vv->gv_state == RESTARTER_STATE_ONLINE) ||
4299aca380d7SRenaud Manus 			    (vv->gv_state == RESTARTER_STATE_DEGRADED))
4300aca380d7SRenaud Manus 				return (B_FALSE);
4301aca380d7SRenaud Manus 		} else {
4302aca380d7SRenaud Manus 			/*
4303aca380d7SRenaud Manus 			 * For dependency groups or service vertices, keep
4304aca380d7SRenaud Manus 			 * traversing to see if instances are running.
4305aca380d7SRenaud Manus 			 */
4306aca380d7SRenaud Manus 			if (insubtree_dependents_down(vv) == B_FALSE)
4307aca380d7SRenaud Manus 				return (B_FALSE);
4308aca380d7SRenaud Manus 		}
4309aca380d7SRenaud Manus 	}
4310aca380d7SRenaud Manus 
4311aca380d7SRenaud Manus 	return (B_TRUE);
4312aca380d7SRenaud Manus }
4313aca380d7SRenaud Manus 
4314aca380d7SRenaud Manus /*
431556e23938Sbustos  * Returns true only if none of this service's dependents are 'up' -- online,
431656e23938Sbustos  * degraded, or offline.
43177c478bd9Sstevel@tonic-gate  */
43187c478bd9Sstevel@tonic-gate static int
431956e23938Sbustos is_nonsubgraph_leaf(graph_vertex_t *v)
43207c478bd9Sstevel@tonic-gate {
43217c478bd9Sstevel@tonic-gate 	graph_vertex_t *vv;
43227c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
43237c478bd9Sstevel@tonic-gate 
4324*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
43257c478bd9Sstevel@tonic-gate 
43267c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
43277c478bd9Sstevel@tonic-gate 	    e != NULL;
43287c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e)) {
43297c478bd9Sstevel@tonic-gate 
43307c478bd9Sstevel@tonic-gate 		vv = e->ge_vertex;
43317c478bd9Sstevel@tonic-gate 		if (vv->gv_type == GVT_INST) {
433256e23938Sbustos 			if ((vv->gv_flags & GV_CONFIGURED) == 0)
433356e23938Sbustos 				continue;
433456e23938Sbustos 
433556e23938Sbustos 			if (vv->gv_flags & GV_INSUBGRAPH)
433656e23938Sbustos 				continue;
433756e23938Sbustos 
433856e23938Sbustos 			if (up_state(vv->gv_state))
433956e23938Sbustos 				return (0);
43407c478bd9Sstevel@tonic-gate 		} else {
43417c478bd9Sstevel@tonic-gate 			/*
43427c478bd9Sstevel@tonic-gate 			 * For dependency group or service vertices, keep
43437c478bd9Sstevel@tonic-gate 			 * traversing to see if instances are running.
4344aca380d7SRenaud Manus 			 *
4345aca380d7SRenaud Manus 			 * We should skip exclude_all dependencies otherwise
4346aca380d7SRenaud Manus 			 * the vertex will never be considered as a leaf
4347aca380d7SRenaud Manus 			 * if the dependent is offline. The main reason for
4348aca380d7SRenaud Manus 			 * this is that disable_nonsubgraph_leaves() skips
4349aca380d7SRenaud Manus 			 * exclusion dependencies.
43507c478bd9Sstevel@tonic-gate 			 */
4351aca380d7SRenaud Manus 			if (vv->gv_type == GVT_GROUP &&
4352aca380d7SRenaud Manus 			    vv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
4353aca380d7SRenaud Manus 				continue;
4354aca380d7SRenaud Manus 
435556e23938Sbustos 			if (!is_nonsubgraph_leaf(vv))
43567c478bd9Sstevel@tonic-gate 				return (0);
43577c478bd9Sstevel@tonic-gate 		}
435856e23938Sbustos 	}
435956e23938Sbustos 
436056e23938Sbustos 	return (1);
436156e23938Sbustos }
43627c478bd9Sstevel@tonic-gate 
43637c478bd9Sstevel@tonic-gate /*
436456e23938Sbustos  * Disable v temporarily.  Attempt to do this by setting its enabled override
436556e23938Sbustos  * property in the repository.  If that fails, send a _DISABLE command.
436656e23938Sbustos  * Returns 0 on success and ECONNABORTED if the repository connection is
436756e23938Sbustos  * broken.
43687c478bd9Sstevel@tonic-gate  */
436956e23938Sbustos static int
437056e23938Sbustos disable_service_temporarily(graph_vertex_t *v, scf_handle_t *h)
43717c478bd9Sstevel@tonic-gate {
437256e23938Sbustos 	const char * const emsg = "Could not temporarily disable %s because "
437356e23938Sbustos 	    "%s.  Will stop service anyways.  Repository status for the "
437456e23938Sbustos 	    "service may be inaccurate.\n";
437556e23938Sbustos 	const char * const emsg_cbroken =
437656e23938Sbustos 	    "the repository connection was broken";
437756e23938Sbustos 
437856e23938Sbustos 	scf_instance_t *inst;
43797c478bd9Sstevel@tonic-gate 	int r;
43807c478bd9Sstevel@tonic-gate 
43817c478bd9Sstevel@tonic-gate 	inst = scf_instance_create(h);
43827c478bd9Sstevel@tonic-gate 	if (inst == NULL) {
438356e23938Sbustos 		char buf[100];
438456e23938Sbustos 
438556e23938Sbustos 		(void) snprintf(buf, sizeof (buf),
438656e23938Sbustos 		    "scf_instance_create() failed (%s)",
438756e23938Sbustos 		    scf_strerror(scf_error()));
438856e23938Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name, buf);
438956e23938Sbustos 
439056e23938Sbustos 		graph_enable_by_vertex(v, 0, 0);
439156e23938Sbustos 		return (0);
43927c478bd9Sstevel@tonic-gate 	}
439356e23938Sbustos 
43947c478bd9Sstevel@tonic-gate 	r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst,
43957c478bd9Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT);
43967c478bd9Sstevel@tonic-gate 	if (r != 0) {
43977c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
43987c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
439956e23938Sbustos 			log_error(LOG_WARNING, emsg, v->gv_name, emsg_cbroken);
440056e23938Sbustos 			graph_enable_by_vertex(v, 0, 0);
440156e23938Sbustos 			return (ECONNABORTED);
44027c478bd9Sstevel@tonic-gate 
44037c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
440456e23938Sbustos 			return (0);
44057c478bd9Sstevel@tonic-gate 
44067c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
44077c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
44087c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
44097c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
44107c478bd9Sstevel@tonic-gate 		default:
44117c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri",
44127c478bd9Sstevel@tonic-gate 			    scf_error());
44137c478bd9Sstevel@tonic-gate 		}
44147c478bd9Sstevel@tonic-gate 	}
441556e23938Sbustos 
44167c478bd9Sstevel@tonic-gate 	r = libscf_set_enable_ovr(inst, 0);
44177c478bd9Sstevel@tonic-gate 	switch (r) {
44187c478bd9Sstevel@tonic-gate 	case 0:
44197c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
442056e23938Sbustos 		return (0);
442156e23938Sbustos 
44227c478bd9Sstevel@tonic-gate 	case ECANCELED:
44237c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
442456e23938Sbustos 		return (0);
442556e23938Sbustos 
44267c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
442756e23938Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name, emsg_cbroken);
442856e23938Sbustos 		graph_enable_by_vertex(v, 0, 0);
442956e23938Sbustos 		return (ECONNABORTED);
443056e23938Sbustos 
44317c478bd9Sstevel@tonic-gate 	case EPERM:
443256e23938Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name,
443356e23938Sbustos 		    "the repository denied permission");
443456e23938Sbustos 		graph_enable_by_vertex(v, 0, 0);
443556e23938Sbustos 		return (0);
443656e23938Sbustos 
44377c478bd9Sstevel@tonic-gate 	case EROFS:
443856e23938Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name,
443956e23938Sbustos 		    "the repository is read-only");
444056e23938Sbustos 		graph_enable_by_vertex(v, 0, 0);
444156e23938Sbustos 		return (0);
444256e23938Sbustos 
44437c478bd9Sstevel@tonic-gate 	default:
44447c478bd9Sstevel@tonic-gate 		bad_error("libscf_set_enable_ovr", r);
444556e23938Sbustos 		/* NOTREACHED */
44467c478bd9Sstevel@tonic-gate 	}
444756e23938Sbustos }
444856e23938Sbustos 
444956e23938Sbustos /*
4450aca380d7SRenaud Manus  * Of the transitive instance dependencies of v, offline those which are
4451aca380d7SRenaud Manus  * in the subtree and which are leaves (i.e., have no dependents which are
4452aca380d7SRenaud Manus  * "up").
4453aca380d7SRenaud Manus  */
4454aca380d7SRenaud Manus void
4455aca380d7SRenaud Manus offline_subtree_leaves(graph_vertex_t *v, void *arg)
4456aca380d7SRenaud Manus {
4457*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
4458aca380d7SRenaud Manus 
4459aca380d7SRenaud Manus 	/* If v isn't an instance, recurse on its dependencies. */
4460aca380d7SRenaud Manus 	if (v->gv_type != GVT_INST) {
4461aca380d7SRenaud Manus 		graph_walk_dependencies(v, offline_subtree_leaves, arg);
4462aca380d7SRenaud Manus 		return;
4463aca380d7SRenaud Manus 	}
4464aca380d7SRenaud Manus 
4465aca380d7SRenaud Manus 	/*
4466aca380d7SRenaud Manus 	 * If v is not in the subtree, so should all of its dependencies,
4467aca380d7SRenaud Manus 	 * so do nothing.
4468aca380d7SRenaud Manus 	 */
4469aca380d7SRenaud Manus 	if ((v->gv_flags & GV_TOOFFLINE) == 0)
4470aca380d7SRenaud Manus 		return;
4471aca380d7SRenaud Manus 
4472aca380d7SRenaud Manus 	/* If v isn't a leaf because it's already down, recurse. */
4473aca380d7SRenaud Manus 	if (!up_state(v->gv_state)) {
4474aca380d7SRenaud Manus 		graph_walk_dependencies(v, offline_subtree_leaves, arg);
4475aca380d7SRenaud Manus 		return;
4476aca380d7SRenaud Manus 	}
4477aca380d7SRenaud Manus 
4478aca380d7SRenaud Manus 	/* if v is a leaf, offline it or disable it if it's the last one */
4479aca380d7SRenaud Manus 	if (insubtree_dependents_down(v) == B_TRUE) {
4480aca380d7SRenaud Manus 		if (v->gv_flags & GV_TODISABLE)
4481aca380d7SRenaud Manus 			vertex_send_event(v,
4482aca380d7SRenaud Manus 			    RESTARTER_EVENT_TYPE_ADMIN_DISABLE);
4483aca380d7SRenaud Manus 		else
4484aca380d7SRenaud Manus 			offline_vertex(v);
4485aca380d7SRenaud Manus 	}
4486aca380d7SRenaud Manus }
4487aca380d7SRenaud Manus 
4488aca380d7SRenaud Manus void
4489aca380d7SRenaud Manus graph_offline_subtree_leaves(graph_vertex_t *v, void *h)
4490aca380d7SRenaud Manus {
4491aca380d7SRenaud Manus 	graph_walk_dependencies(v, offline_subtree_leaves, (void *)h);
4492aca380d7SRenaud Manus }
4493aca380d7SRenaud Manus 
4494aca380d7SRenaud Manus 
4495aca380d7SRenaud Manus /*
449656e23938Sbustos  * Of the transitive instance dependencies of v, disable those which are not
449756e23938Sbustos  * in the subgraph and which are leaves (i.e., have no dependents which are
449856e23938Sbustos  * "up").
449956e23938Sbustos  */
450056e23938Sbustos static void
450156e23938Sbustos disable_nonsubgraph_leaves(graph_vertex_t *v, void *arg)
450256e23938Sbustos {
4503*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
450456e23938Sbustos 
4505b54561f4Snakanon 	/*
4506b54561f4Snakanon 	 * We must skip exclusion dependencies because they are allowed to
4507b54561f4Snakanon 	 * complete dependency cycles.  This is correct because A's exclusion
4508b54561f4Snakanon 	 * dependency on B doesn't bear on the order in which they should be
4509b54561f4Snakanon 	 * stopped.  Indeed, the exclusion dependency should guarantee that
4510b54561f4Snakanon 	 * they are never online at the same time.
4511b54561f4Snakanon 	 */
4512b54561f4Snakanon 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
4513b54561f4Snakanon 		return;
4514b54561f4Snakanon 
451556e23938Sbustos 	/* If v isn't an instance, recurse on its dependencies. */
451656e23938Sbustos 	if (v->gv_type != GVT_INST)
451756e23938Sbustos 		goto recurse;
451856e23938Sbustos 
451956e23938Sbustos 	if ((v->gv_flags & GV_CONFIGURED) == 0)
452056e23938Sbustos 		/*
452156e23938Sbustos 		 * Unconfigured instances should have no dependencies, but in
452256e23938Sbustos 		 * case they ever get them,
452356e23938Sbustos 		 */
452456e23938Sbustos 		goto recurse;
452556e23938Sbustos 
452656e23938Sbustos 	/*
452756e23938Sbustos 	 * If v is in the subgraph, so should all of its dependencies, so do
452856e23938Sbustos 	 * nothing.
452956e23938Sbustos 	 */
453056e23938Sbustos 	if (v->gv_flags & GV_INSUBGRAPH)
45317c478bd9Sstevel@tonic-gate 		return;
453256e23938Sbustos 
453356e23938Sbustos 	/* If v isn't a leaf because it's already down, recurse. */
453456e23938Sbustos 	if (!up_state(v->gv_state))
453556e23938Sbustos 		goto recurse;
453656e23938Sbustos 
453756e23938Sbustos 	/* If v is disabled but not down yet, be patient. */
453856e23938Sbustos 	if ((v->gv_flags & GV_ENABLED) == 0)
453956e23938Sbustos 		return;
454056e23938Sbustos 
454156e23938Sbustos 	/* If v is a leaf, disable it. */
454256e23938Sbustos 	if (is_nonsubgraph_leaf(v))
454356e23938Sbustos 		(void) disable_service_temporarily(v, (scf_handle_t *)arg);
454456e23938Sbustos 
454556e23938Sbustos 	return;
454656e23938Sbustos 
45477c478bd9Sstevel@tonic-gate recurse:
454856e23938Sbustos 	graph_walk_dependencies(v, disable_nonsubgraph_leaves, arg);
45497c478bd9Sstevel@tonic-gate }
45507c478bd9Sstevel@tonic-gate 
45517c478bd9Sstevel@tonic-gate /*
45527c478bd9Sstevel@tonic-gate  * Find the vertex for inst_name.  If it doesn't exist, return ENOENT.
45537c478bd9Sstevel@tonic-gate  * Otherwise set its state to state.  If the instance has entered a state
45547c478bd9Sstevel@tonic-gate  * which requires automatic action, take it (Uninitialized: do
45557c478bd9Sstevel@tonic-gate  * dgraph_refresh_instance() without the snapshot update.  Disabled: if the
45567c478bd9Sstevel@tonic-gate  * instance should be enabled, send _ENABLE.  Offline: if the instance should
45577c478bd9Sstevel@tonic-gate  * be disabled, send _DISABLE, and if its dependencies are satisfied, send
45587c478bd9Sstevel@tonic-gate  * _START.  Online, Degraded: if the instance wasn't running, update its start
45597c478bd9Sstevel@tonic-gate  * snapshot.  Maintenance: no action.)
45607c478bd9Sstevel@tonic-gate  *
45617c478bd9Sstevel@tonic-gate  * Also fails with ECONNABORTED, or EINVAL if state is invalid.
45627c478bd9Sstevel@tonic-gate  */
45637c478bd9Sstevel@tonic-gate static int
45647c478bd9Sstevel@tonic-gate dgraph_set_instance_state(scf_handle_t *h, const char *inst_name,
45657c478bd9Sstevel@tonic-gate     restarter_instance_state_t state, restarter_error_t serr)
45667c478bd9Sstevel@tonic-gate {
45677c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
456899b44c3bSlianep 	int err = 0;
45697c478bd9Sstevel@tonic-gate 	restarter_instance_state_t old_state;
45707c478bd9Sstevel@tonic-gate 
45717c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
45727c478bd9Sstevel@tonic-gate 
45737c478bd9Sstevel@tonic-gate 	v = vertex_get_by_name(inst_name);
45747c478bd9Sstevel@tonic-gate 	if (v == NULL) {
45757c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
45767c478bd9Sstevel@tonic-gate 		return (ENOENT);
45777c478bd9Sstevel@tonic-gate 	}
45787c478bd9Sstevel@tonic-gate 
457956e23938Sbustos 	assert(v->gv_type == GVT_INST);
458056e23938Sbustos 
45817c478bd9Sstevel@tonic-gate 	switch (state) {
45827c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_UNINIT:
45837c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_DISABLED:
45847c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_OFFLINE:
45857c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_ONLINE:
45867c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_DEGRADED:
45877c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_MAINT:
45887c478bd9Sstevel@tonic-gate 		break;
45897c478bd9Sstevel@tonic-gate 
45907c478bd9Sstevel@tonic-gate 	default:
45917c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
45927c478bd9Sstevel@tonic-gate 		return (EINVAL);
45937c478bd9Sstevel@tonic-gate 	}
45947c478bd9Sstevel@tonic-gate 
45957c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph noting %s %s -> %s.\n", v->gv_name,
45967c478bd9Sstevel@tonic-gate 	    instance_state_str[v->gv_state], instance_state_str[state]);
45977c478bd9Sstevel@tonic-gate 
45987c478bd9Sstevel@tonic-gate 	old_state = v->gv_state;
45997c478bd9Sstevel@tonic-gate 	v->gv_state = state;
46007c478bd9Sstevel@tonic-gate 
4601cd3bce3eSlianep 	err = gt_transition(h, v, serr, old_state);
460299b44c3bSlianep 
460399b44c3bSlianep 	MUTEX_UNLOCK(&dgraph_lock);
460499b44c3bSlianep 	return (err);
460599b44c3bSlianep }
460699b44c3bSlianep 
460799b44c3bSlianep /*
460856e23938Sbustos  * Handle state changes during milestone shutdown.  See
460956e23938Sbustos  * dgraph_set_milestone().  If the repository connection is broken,
461056e23938Sbustos  * ECONNABORTED will be returned, though a _DISABLE command will be sent for
461156e23938Sbustos  * the vertex anyway.
461299b44c3bSlianep  */
461356e23938Sbustos int
461499b44c3bSlianep vertex_subgraph_dependencies_shutdown(scf_handle_t *h, graph_vertex_t *v,
461556e23938Sbustos     restarter_instance_state_t old_state)
461699b44c3bSlianep {
461756e23938Sbustos 	int was_up, now_up;
461856e23938Sbustos 	int ret = 0;
461999b44c3bSlianep 
462056e23938Sbustos 	assert(v->gv_type == GVT_INST);
46217c478bd9Sstevel@tonic-gate 
462256e23938Sbustos 	/* Don't care if we're not going to a milestone. */
462356e23938Sbustos 	if (milestone == NULL)
462456e23938Sbustos 		return (0);
462556e23938Sbustos 
462656e23938Sbustos 	/* Don't care if we already finished coming down. */
462756e23938Sbustos 	if (non_subgraph_svcs == 0)
462856e23938Sbustos 		return (0);
462956e23938Sbustos 
463056e23938Sbustos 	/* Don't care if the service is in the subgraph. */
463156e23938Sbustos 	if (v->gv_flags & GV_INSUBGRAPH)
463256e23938Sbustos 		return (0);
463356e23938Sbustos 
463456e23938Sbustos 	/*
463556e23938Sbustos 	 * Update non_subgraph_svcs.  It is the number of non-subgraph
463656e23938Sbustos 	 * services which are in online, degraded, or offline.
463756e23938Sbustos 	 */
463856e23938Sbustos 
463956e23938Sbustos 	was_up = up_state(old_state);
464056e23938Sbustos 	now_up = up_state(v->gv_state);
464156e23938Sbustos 
464256e23938Sbustos 	if (!was_up && now_up) {
464356e23938Sbustos 		++non_subgraph_svcs;
464456e23938Sbustos 	} else if (was_up && !now_up) {
46457c478bd9Sstevel@tonic-gate 		--non_subgraph_svcs;
464656e23938Sbustos 
46477c478bd9Sstevel@tonic-gate 		if (non_subgraph_svcs == 0) {
46487c478bd9Sstevel@tonic-gate 			if (halting != -1) {
46497c478bd9Sstevel@tonic-gate 				do_uadmin();
46507c478bd9Sstevel@tonic-gate 			} else if (go_single_user_mode || go_to_level1) {
46517c478bd9Sstevel@tonic-gate 				(void) startd_thread_create(single_user_thread,
46527c478bd9Sstevel@tonic-gate 				    NULL);
46537c478bd9Sstevel@tonic-gate 			}
465456e23938Sbustos 			return (0);
46557c478bd9Sstevel@tonic-gate 		}
46567c478bd9Sstevel@tonic-gate 	}
465756e23938Sbustos 
465856e23938Sbustos 	/* If this service is a leaf, it should be disabled. */
465956e23938Sbustos 	if ((v->gv_flags & GV_ENABLED) && is_nonsubgraph_leaf(v)) {
466056e23938Sbustos 		int r;
466156e23938Sbustos 
466256e23938Sbustos 		r = disable_service_temporarily(v, h);
466356e23938Sbustos 		switch (r) {
466456e23938Sbustos 		case 0:
466556e23938Sbustos 			break;
466656e23938Sbustos 
466756e23938Sbustos 		case ECONNABORTED:
466856e23938Sbustos 			ret = ECONNABORTED;
466956e23938Sbustos 			break;
467056e23938Sbustos 
467156e23938Sbustos 		default:
467256e23938Sbustos 			bad_error("disable_service_temporarily", r);
467356e23938Sbustos 		}
467456e23938Sbustos 	}
467556e23938Sbustos 
467656e23938Sbustos 	/*
467756e23938Sbustos 	 * If the service just came down, propagate the disable to the newly
467856e23938Sbustos 	 * exposed leaves.
467956e23938Sbustos 	 */
468056e23938Sbustos 	if (was_up && !now_up)
468156e23938Sbustos 		graph_walk_dependencies(v, disable_nonsubgraph_leaves,
468256e23938Sbustos 		    (void *)h);
468356e23938Sbustos 
468456e23938Sbustos 	return (ret);
46857c478bd9Sstevel@tonic-gate }
46867c478bd9Sstevel@tonic-gate 
46877c478bd9Sstevel@tonic-gate /*
468899b44c3bSlianep  * Decide whether to start up an sulogin thread after a service is
468999b44c3bSlianep  * finished changing state.  Only need to do the full can_come_up()
469099b44c3bSlianep  * evaluation if an instance is changing state, we're not halfway through
469199b44c3bSlianep  * loading the thread, and we aren't shutting down or going to the single
469299b44c3bSlianep  * user milestone.
46937c478bd9Sstevel@tonic-gate  */
469499b44c3bSlianep void
469599b44c3bSlianep graph_transition_sulogin(restarter_instance_state_t state,
469699b44c3bSlianep     restarter_instance_state_t old_state)
469799b44c3bSlianep {
4698*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
46997c478bd9Sstevel@tonic-gate 
47007c478bd9Sstevel@tonic-gate 	if (state != old_state && st->st_load_complete &&
47017c478bd9Sstevel@tonic-gate 	    !go_single_user_mode && !go_to_level1 &&
47027c478bd9Sstevel@tonic-gate 	    halting == -1) {
470373b709eaSrm88369 		if (!sulogin_thread_running && !can_come_up()) {
47047c478bd9Sstevel@tonic-gate 			(void) startd_thread_create(sulogin_thread, NULL);
47057c478bd9Sstevel@tonic-gate 			sulogin_thread_running = B_TRUE;
47067c478bd9Sstevel@tonic-gate 		}
47077c478bd9Sstevel@tonic-gate 	}
470899b44c3bSlianep }
47097c478bd9Sstevel@tonic-gate 
471099b44c3bSlianep /*
4711cd3bce3eSlianep  * Propagate a start, stop event, or a satisfiability event.
471299b44c3bSlianep  *
4713cd3bce3eSlianep  * PROPAGATE_START and PROPAGATE_STOP simply propagate the transition event
4714cd3bce3eSlianep  * to direct dependents.  PROPAGATE_SAT propagates a start then walks the
4715cd3bce3eSlianep  * full dependent graph to check for newly satisfied nodes.  This is
4716cd3bce3eSlianep  * necessary for cases when non-direct dependents may be effected but direct
4717cd3bce3eSlianep  * dependents may not (e.g. for optional_all evaluations, see the
4718cd3bce3eSlianep  * propagate_satbility() comments).
4719cd3bce3eSlianep  *
4720cd3bce3eSlianep  * PROPAGATE_SAT should be used whenever a non-running service moves into
4721cd3bce3eSlianep  * a state which can satisfy optional dependencies, like disabled or
4722cd3bce3eSlianep  * maintenance.
472399b44c3bSlianep  */
472499b44c3bSlianep void
4725cd3bce3eSlianep graph_transition_propagate(graph_vertex_t *v, propagate_event_t type,
472699b44c3bSlianep     restarter_error_t rerr)
472799b44c3bSlianep {
4728cd3bce3eSlianep 	if (type == PROPAGATE_STOP) {
472999b44c3bSlianep 		graph_walk_dependents(v, propagate_stop, (void *)rerr);
4730cd3bce3eSlianep 	} else if (type == PROPAGATE_START || type == PROPAGATE_SAT) {
473199b44c3bSlianep 		graph_walk_dependents(v, propagate_start, NULL);
47327c478bd9Sstevel@tonic-gate 
4733cd3bce3eSlianep 		if (type == PROPAGATE_SAT)
473499b44c3bSlianep 			propagate_satbility(v);
473599b44c3bSlianep 	} else {
473699b44c3bSlianep #ifndef NDEBUG
4737cd3bce3eSlianep 		uu_warn("%s:%d: Unexpected type value %d.\n",  __FILE__,
4738cd3bce3eSlianep 		    __LINE__, type);
473999b44c3bSlianep #endif
474099b44c3bSlianep 		abort();
474199b44c3bSlianep 	}
47427c478bd9Sstevel@tonic-gate }
47437c478bd9Sstevel@tonic-gate 
47447c478bd9Sstevel@tonic-gate /*
47457c478bd9Sstevel@tonic-gate  * If a vertex for fmri exists and it is enabled, send _DISABLE to the
47467c478bd9Sstevel@tonic-gate  * restarter.  If it is running, send _STOP.  Send _REMOVE_INSTANCE.  Delete
47477c478bd9Sstevel@tonic-gate  * all property group dependencies, and the dependency on the restarter,
47487c478bd9Sstevel@tonic-gate  * disposing of vertices as appropriate.  If other vertices depend on this
47497c478bd9Sstevel@tonic-gate  * one, mark it unconfigured and return.  Otherwise remove the vertex.  Always
47507c478bd9Sstevel@tonic-gate  * returns 0.
47517c478bd9Sstevel@tonic-gate  */
47527c478bd9Sstevel@tonic-gate static int
47537c478bd9Sstevel@tonic-gate dgraph_remove_instance(const char *fmri, scf_handle_t *h)
47547c478bd9Sstevel@tonic-gate {
47557c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
47567c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
47577c478bd9Sstevel@tonic-gate 	uu_list_t *old_deps;
47587c478bd9Sstevel@tonic-gate 	int err;
47597c478bd9Sstevel@tonic-gate 
47607c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph engine: Removing %s.\n", fmri);
47617c478bd9Sstevel@tonic-gate 
47627c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
47637c478bd9Sstevel@tonic-gate 
47647c478bd9Sstevel@tonic-gate 	v = vertex_get_by_name(fmri);
47657c478bd9Sstevel@tonic-gate 	if (v == NULL) {
47667c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
47677c478bd9Sstevel@tonic-gate 		return (0);
47687c478bd9Sstevel@tonic-gate 	}
47697c478bd9Sstevel@tonic-gate 
47707c478bd9Sstevel@tonic-gate 	/* Send restarter delete event. */
47717c478bd9Sstevel@tonic-gate 	if (v->gv_flags & GV_CONFIGURED)
47727c478bd9Sstevel@tonic-gate 		graph_unset_restarter(v);
47737c478bd9Sstevel@tonic-gate 
47747c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
47757c478bd9Sstevel@tonic-gate 		/*
47767c478bd9Sstevel@tonic-gate 		 * Make a list of v's current dependencies so we can
47777c478bd9Sstevel@tonic-gate 		 * reevaluate their GV_INSUBGRAPH flags after the dependencies
47787c478bd9Sstevel@tonic-gate 		 * are removed.
47797c478bd9Sstevel@tonic-gate 		 */
47807c478bd9Sstevel@tonic-gate 		old_deps = startd_list_create(graph_edge_pool, NULL, 0);
47817c478bd9Sstevel@tonic-gate 
47827c478bd9Sstevel@tonic-gate 		err = uu_list_walk(v->gv_dependencies,
47833ad28c1eSrm88369 		    (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0);
47847c478bd9Sstevel@tonic-gate 		assert(err == 0);
47857c478bd9Sstevel@tonic-gate 	}
47867c478bd9Sstevel@tonic-gate 
47877c478bd9Sstevel@tonic-gate 	delete_instance_dependencies(v, B_TRUE);
47887c478bd9Sstevel@tonic-gate 
47897c478bd9Sstevel@tonic-gate 	/*
47907c478bd9Sstevel@tonic-gate 	 * Deleting an instance can both satisfy and unsatisfy dependencies,
47917c478bd9Sstevel@tonic-gate 	 * depending on their type.  First propagate the stop as a RERR_RESTART
47927c478bd9Sstevel@tonic-gate 	 * event -- deletion isn't a fault, just a normal stop.  This gives
47937c478bd9Sstevel@tonic-gate 	 * dependent services the chance to do a clean shutdown.  Then, mark
47947c478bd9Sstevel@tonic-gate 	 * the service as unconfigured and propagate the start event for the
47957c478bd9Sstevel@tonic-gate 	 * optional_all dependencies that might have become satisfied.
47967c478bd9Sstevel@tonic-gate 	 */
47977c478bd9Sstevel@tonic-gate 	graph_walk_dependents(v, propagate_stop, (void *)RERR_RESTART);
47987c478bd9Sstevel@tonic-gate 
47997c478bd9Sstevel@tonic-gate 	v->gv_flags &= ~GV_CONFIGURED;
480070cbfe41SPhilippe Jung 	v->gv_flags &= ~GV_DEATHROW;
48017c478bd9Sstevel@tonic-gate 
48027c478bd9Sstevel@tonic-gate 	graph_walk_dependents(v, propagate_start, NULL);
48037c478bd9Sstevel@tonic-gate 	propagate_satbility(v);
48047c478bd9Sstevel@tonic-gate 
48057c478bd9Sstevel@tonic-gate 	/*
48067c478bd9Sstevel@tonic-gate 	 * If there are no (non-service) dependents, the vertex can be
48077c478bd9Sstevel@tonic-gate 	 * completely removed.
48087c478bd9Sstevel@tonic-gate 	 */
48093ad28c1eSrm88369 	if (v != milestone && v->gv_refs == 0 &&
48103ad28c1eSrm88369 	    uu_list_numnodes(v->gv_dependents) == 1)
48117c478bd9Sstevel@tonic-gate 		remove_inst_vertex(v);
48127c478bd9Sstevel@tonic-gate 
48137c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
48147c478bd9Sstevel@tonic-gate 		void *cookie = NULL;
48157c478bd9Sstevel@tonic-gate 
48167c478bd9Sstevel@tonic-gate 		while ((e = uu_list_teardown(old_deps, &cookie)) != NULL) {
48173ad28c1eSrm88369 			v = e->ge_vertex;
48183ad28c1eSrm88369 
48193ad28c1eSrm88369 			if (vertex_unref(v) == VERTEX_INUSE)
48203ad28c1eSrm88369 				while (eval_subgraph(v, h) == ECONNABORTED)
48217c478bd9Sstevel@tonic-gate 					libscf_handle_rebind(h);
48227c478bd9Sstevel@tonic-gate 
48237c478bd9Sstevel@tonic-gate 			startd_free(e, sizeof (*e));
48247c478bd9Sstevel@tonic-gate 		}
48257c478bd9Sstevel@tonic-gate 
48267c478bd9Sstevel@tonic-gate 		uu_list_destroy(old_deps);
48277c478bd9Sstevel@tonic-gate 	}
48287c478bd9Sstevel@tonic-gate 
48297c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
48307c478bd9Sstevel@tonic-gate 
48317c478bd9Sstevel@tonic-gate 	return (0);
48327c478bd9Sstevel@tonic-gate }
48337c478bd9Sstevel@tonic-gate 
48347c478bd9Sstevel@tonic-gate /*
48357c478bd9Sstevel@tonic-gate  * Return the eventual (maybe current) milestone in the form of a
48367c478bd9Sstevel@tonic-gate  * legacy runlevel.
48377c478bd9Sstevel@tonic-gate  */
48387c478bd9Sstevel@tonic-gate static char
48397c478bd9Sstevel@tonic-gate target_milestone_as_runlevel()
48407c478bd9Sstevel@tonic-gate {
4841*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
48427c478bd9Sstevel@tonic-gate 
48437c478bd9Sstevel@tonic-gate 	if (milestone == NULL)
48447c478bd9Sstevel@tonic-gate 		return ('3');
48457c478bd9Sstevel@tonic-gate 	else if (milestone == MILESTONE_NONE)
48467c478bd9Sstevel@tonic-gate 		return ('0');
48477c478bd9Sstevel@tonic-gate 
48487c478bd9Sstevel@tonic-gate 	if (strcmp(milestone->gv_name, multi_user_fmri) == 0)
48497c478bd9Sstevel@tonic-gate 		return ('2');
48507c478bd9Sstevel@tonic-gate 	else if (strcmp(milestone->gv_name, single_user_fmri) == 0)
48517c478bd9Sstevel@tonic-gate 		return ('S');
48527c478bd9Sstevel@tonic-gate 	else if (strcmp(milestone->gv_name, multi_user_svr_fmri) == 0)
48537c478bd9Sstevel@tonic-gate 		return ('3');
48547c478bd9Sstevel@tonic-gate 
48557c478bd9Sstevel@tonic-gate #ifndef NDEBUG
48567c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:%d: Unknown milestone name \"%s\".\n",
48577c478bd9Sstevel@tonic-gate 	    __FILE__, __LINE__, milestone->gv_name);
48587c478bd9Sstevel@tonic-gate #endif
48597c478bd9Sstevel@tonic-gate 	abort();
48607c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
48617c478bd9Sstevel@tonic-gate }
48627c478bd9Sstevel@tonic-gate 
48637c478bd9Sstevel@tonic-gate static struct {
48647c478bd9Sstevel@tonic-gate 	char	rl;
48657c478bd9Sstevel@tonic-gate 	int	sig;
48667c478bd9Sstevel@tonic-gate } init_sigs[] = {
48677c478bd9Sstevel@tonic-gate 	{ 'S', SIGBUS },
48687c478bd9Sstevel@tonic-gate 	{ '0', SIGINT },
48697c478bd9Sstevel@tonic-gate 	{ '1', SIGQUIT },
48707c478bd9Sstevel@tonic-gate 	{ '2', SIGILL },
48717c478bd9Sstevel@tonic-gate 	{ '3', SIGTRAP },
48727c478bd9Sstevel@tonic-gate 	{ '4', SIGIOT },
48737c478bd9Sstevel@tonic-gate 	{ '5', SIGEMT },
48747c478bd9Sstevel@tonic-gate 	{ '6', SIGFPE },
48757c478bd9Sstevel@tonic-gate 	{ 0, 0 }
48767c478bd9Sstevel@tonic-gate };
48777c478bd9Sstevel@tonic-gate 
48787c478bd9Sstevel@tonic-gate static void
48797c478bd9Sstevel@tonic-gate signal_init(char rl)
48807c478bd9Sstevel@tonic-gate {
48817c478bd9Sstevel@tonic-gate 	pid_t init_pid;
48827c478bd9Sstevel@tonic-gate 	int i;
48837c478bd9Sstevel@tonic-gate 
4884*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
48857c478bd9Sstevel@tonic-gate 
48867c478bd9Sstevel@tonic-gate 	if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
48877c478bd9Sstevel@tonic-gate 	    sizeof (init_pid)) != sizeof (init_pid)) {
48887c478bd9Sstevel@tonic-gate 		log_error(LOG_NOTICE, "Could not get pid to signal init.\n");
48897c478bd9Sstevel@tonic-gate 		return;
48907c478bd9Sstevel@tonic-gate 	}
48917c478bd9Sstevel@tonic-gate 
48927c478bd9Sstevel@tonic-gate 	for (i = 0; init_sigs[i].rl != 0; ++i)
48937c478bd9Sstevel@tonic-gate 		if (init_sigs[i].rl == rl)
48947c478bd9Sstevel@tonic-gate 			break;
48957c478bd9Sstevel@tonic-gate 
48967c478bd9Sstevel@tonic-gate 	if (init_sigs[i].rl != 0) {
48977c478bd9Sstevel@tonic-gate 		if (kill(init_pid, init_sigs[i].sig) != 0) {
48987c478bd9Sstevel@tonic-gate 			switch (errno) {
48997c478bd9Sstevel@tonic-gate 			case EPERM:
49007c478bd9Sstevel@tonic-gate 			case ESRCH:
49017c478bd9Sstevel@tonic-gate 				log_error(LOG_NOTICE, "Could not signal init: "
49027c478bd9Sstevel@tonic-gate 				    "%s.\n", strerror(errno));
49037c478bd9Sstevel@tonic-gate 				break;
49047c478bd9Sstevel@tonic-gate 
49057c478bd9Sstevel@tonic-gate 			case EINVAL:
49067c478bd9Sstevel@tonic-gate 			default:
49077c478bd9Sstevel@tonic-gate 				bad_error("kill", errno);
49087c478bd9Sstevel@tonic-gate 			}
49097c478bd9Sstevel@tonic-gate 		}
49107c478bd9Sstevel@tonic-gate 	}
49117c478bd9Sstevel@tonic-gate }
49127c478bd9Sstevel@tonic-gate 
49137c478bd9Sstevel@tonic-gate /*
49147c478bd9Sstevel@tonic-gate  * This is called when one of the major milestones changes state, or when
49157c478bd9Sstevel@tonic-gate  * init is signalled and tells us it was told to change runlevel.  We wait
49167c478bd9Sstevel@tonic-gate  * to reach the milestone because this allows /etc/inittab entries to retain
49177c478bd9Sstevel@tonic-gate  * some boot ordering: historically, entries could place themselves before/after
49187c478bd9Sstevel@tonic-gate  * the running of /sbin/rcX scripts but we can no longer make the
49197c478bd9Sstevel@tonic-gate  * distinction because the /sbin/rcX scripts no longer exist as punctuation
49207c478bd9Sstevel@tonic-gate  * marks in /etc/inittab.
49217c478bd9Sstevel@tonic-gate  *
49227c478bd9Sstevel@tonic-gate  * Also, we only trigger an update when we reach the eventual target
49237c478bd9Sstevel@tonic-gate  * milestone: without this, an /etc/inittab entry marked only for
49247c478bd9Sstevel@tonic-gate  * runlevel 2 would be executed for runlevel 3, which is not how
49257c478bd9Sstevel@tonic-gate  * /etc/inittab entries work.
49267c478bd9Sstevel@tonic-gate  *
49277c478bd9Sstevel@tonic-gate  * If we're single user coming online, then we set utmpx to the target
49287c478bd9Sstevel@tonic-gate  * runlevel so that legacy scripts can work as expected.
49297c478bd9Sstevel@tonic-gate  */
49307c478bd9Sstevel@tonic-gate static void
49317c478bd9Sstevel@tonic-gate graph_runlevel_changed(char rl, int online)
49327c478bd9Sstevel@tonic-gate {
49337c478bd9Sstevel@tonic-gate 	char trl;
49347c478bd9Sstevel@tonic-gate 
4935*53f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
49367c478bd9Sstevel@tonic-gate 
49377c478bd9Sstevel@tonic-gate 	trl = target_milestone_as_runlevel();
49387c478bd9Sstevel@tonic-gate 
49397c478bd9Sstevel@tonic-gate 	if (online) {
49407c478bd9Sstevel@tonic-gate 		if (rl == trl) {
4941965e507bSrm88369 			current_runlevel = trl;
49427c478bd9Sstevel@tonic-gate 			signal_init(trl);
49437c478bd9Sstevel@tonic-gate 		} else if (rl == 'S') {
49447c478bd9Sstevel@tonic-gate 			/*
49457c478bd9Sstevel@tonic-gate 			 * At boot, set the entry early for the benefit of the
49467c478bd9Sstevel@tonic-gate 			 * legacy init scripts.
49477c478bd9Sstevel@tonic-gate 			 */
49487c478bd9Sstevel@tonic-gate 			utmpx_set_runlevel(trl, 'S', B_FALSE);
49497c478bd9Sstevel@tonic-gate 		}
49507c478bd9Sstevel@tonic-gate 	} else {
49517c478bd9Sstevel@tonic-gate 		if (rl == '3' && trl == '2') {
4952965e507bSrm88369 			current_runlevel = trl;
49537c478bd9Sstevel@tonic-gate 			signal_init(trl);
49547c478bd9Sstevel@tonic-gate 		} else if (rl == '2' && trl == 'S') {
4955965e507bSrm88369 			current_runlevel = trl;
49567c478bd9Sstevel@tonic-gate 			signal_init(trl);
49577c478bd9Sstevel@tonic-gate 		}
49587c478bd9Sstevel@tonic-gate 	}
49597c478bd9Sstevel@tonic-gate }
49607c478bd9Sstevel@tonic-gate 
49617c478bd9Sstevel@tonic-gate /*
49627c478bd9Sstevel@tonic-gate  * Move to a backwards-compatible runlevel by executing the appropriate
49637c478bd9Sstevel@tonic-gate  * /etc/rc?.d/K* scripts and/or setting the milestone.
49647c478bd9Sstevel@tonic-gate  *
49657c478bd9Sstevel@tonic-gate  * Returns
49667c478bd9Sstevel@tonic-gate  *   0 - success
49677c478bd9Sstevel@tonic-gate  *   ECONNRESET - success, but handle was reset
49687c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
49697c478bd9Sstevel@tonic-gate  *   ECANCELED - pg was deleted
49707c478bd9Sstevel@tonic-gate  */
49717c478bd9Sstevel@tonic-gate static int
49727c478bd9Sstevel@tonic-gate dgraph_set_runlevel(scf_propertygroup_t *pg, scf_property_t *prop)
49737c478bd9Sstevel@tonic-gate {
49747c478bd9Sstevel@tonic-gate 	char rl;
49757c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
49767c478bd9Sstevel@tonic-gate 	int r;
49777c478bd9Sstevel@tonic-gate 	const char *ms = NULL;	/* what to commit as options/milestone */
49787c478bd9Sstevel@tonic-gate 	boolean_t rebound = B_FALSE;
49797c478bd9Sstevel@tonic-gate 	int mark_rl = 0;
49807c478bd9Sstevel@tonic-gate 
49817c478bd9Sstevel@tonic-gate 	const char * const stop = "stop";
49827c478bd9Sstevel@tonic-gate 
49837c478bd9Sstevel@tonic-gate 	r = libscf_extract_runlevel(prop, &rl);
49847c478bd9Sstevel@tonic-gate 	switch (r) {
49857c478bd9Sstevel@tonic-gate 	case 0:
49867c478bd9Sstevel@tonic-gate 		break;
49877c478bd9Sstevel@tonic-gate 
49887c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
49897c478bd9Sstevel@tonic-gate 	case ECANCELED:
49907c478bd9Sstevel@tonic-gate 		return (r);
49917c478bd9Sstevel@tonic-gate 
49927c478bd9Sstevel@tonic-gate 	case EINVAL:
49937c478bd9Sstevel@tonic-gate 	case ENOENT:
49947c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "runlevel property is misconfigured; "
49957c478bd9Sstevel@tonic-gate 		    "ignoring.\n");
49967c478bd9Sstevel@tonic-gate 		/* delete the bad property */
49977c478bd9Sstevel@tonic-gate 		goto nolock_out;
49987c478bd9Sstevel@tonic-gate 
49997c478bd9Sstevel@tonic-gate 	default:
50007c478bd9Sstevel@tonic-gate 		bad_error("libscf_extract_runlevel", r);
50017c478bd9Sstevel@tonic-gate 	}
50027c478bd9Sstevel@tonic-gate 
50037c478bd9Sstevel@tonic-gate 	switch (rl) {
50047c478bd9Sstevel@tonic-gate 	case 's':
50057c478bd9Sstevel@tonic-gate 		rl = 'S';
50067c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
50077c478bd9Sstevel@tonic-gate 
50087c478bd9Sstevel@tonic-gate 	case 'S':
50097c478bd9Sstevel@tonic-gate 	case '2':
50107c478bd9Sstevel@tonic-gate 	case '3':
50117c478bd9Sstevel@tonic-gate 		/*
50127c478bd9Sstevel@tonic-gate 		 * These cases cause a milestone change, so
50137c478bd9Sstevel@tonic-gate 		 * graph_runlevel_changed() will eventually deal with
50147c478bd9Sstevel@tonic-gate 		 * signalling init.
50157c478bd9Sstevel@tonic-gate 		 */
50167c478bd9Sstevel@tonic-gate 		break;
50177c478bd9Sstevel@tonic-gate 
50187c478bd9Sstevel@tonic-gate 	case '0':
50197c478bd9Sstevel@tonic-gate 	case '1':
50207c478bd9Sstevel@tonic-gate 	case '4':
50217c478bd9Sstevel@tonic-gate 	case '5':
50227c478bd9Sstevel@tonic-gate 	case '6':
50237c478bd9Sstevel@tonic-gate 		mark_rl = 1;
50247c478bd9Sstevel@tonic-gate 		break;
50257c478bd9Sstevel@tonic-gate 
50267c478bd9Sstevel@tonic-gate 	default:
50277c478bd9Sstevel@tonic-gate 		log_framework(LOG_NOTICE, "Unknown runlevel '%c'.\n", rl);
50287c478bd9Sstevel@tonic-gate 		ms = NULL;
50297c478bd9Sstevel@tonic-gate 		goto nolock_out;
50307c478bd9Sstevel@tonic-gate 	}
50317c478bd9Sstevel@tonic-gate 
50327c478bd9Sstevel@tonic-gate 	h = scf_pg_handle(pg);
50337c478bd9Sstevel@tonic-gate 
50347c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
50357c478bd9Sstevel@tonic-gate 
50367c478bd9Sstevel@tonic-gate 	/*
50377c478bd9Sstevel@tonic-gate 	 * Since this triggers no milestone changes, force it by hand.
50387c478bd9Sstevel@tonic-gate 	 */
50397c478bd9Sstevel@tonic-gate 	if (current_runlevel == '4' && rl == '3')
50407c478bd9Sstevel@tonic-gate 		mark_rl = 1;
50417c478bd9Sstevel@tonic-gate 
5042965e507bSrm88369 	/*
5043965e507bSrm88369 	 * 1. If we are here after an "init X":
5044965e507bSrm88369 	 *
5045965e507bSrm88369 	 * init X
5046965e507bSrm88369 	 *	init/lscf_set_runlevel()
5047965e507bSrm88369 	 *		process_pg_event()
5048965e507bSrm88369 	 *		dgraph_set_runlevel()
5049965e507bSrm88369 	 *
5050965e507bSrm88369 	 * then we haven't passed through graph_runlevel_changed() yet,
5051965e507bSrm88369 	 * therefore 'current_runlevel' has not changed for sure but 'rl' has.
5052965e507bSrm88369 	 * In consequence, if 'rl' is lower than 'current_runlevel', we change
5053965e507bSrm88369 	 * the system runlevel and execute the appropriate /etc/rc?.d/K* scripts
5054965e507bSrm88369 	 * past this test.
5055965e507bSrm88369 	 *
5056965e507bSrm88369 	 * 2. On the other hand, if we are here after a "svcadm milestone":
5057965e507bSrm88369 	 *
5058965e507bSrm88369 	 * svcadm milestone X
5059965e507bSrm88369 	 *	dgraph_set_milestone()
5060965e507bSrm88369 	 *		handle_graph_update_event()
5061965e507bSrm88369 	 *		dgraph_set_instance_state()
5062965e507bSrm88369 	 *		graph_post_X_[online|offline]()
5063965e507bSrm88369 	 *		graph_runlevel_changed()
5064965e507bSrm88369 	 *		signal_init()
5065965e507bSrm88369 	 *			init/lscf_set_runlevel()
5066965e507bSrm88369 	 *				process_pg_event()
5067965e507bSrm88369 	 *				dgraph_set_runlevel()
5068965e507bSrm88369 	 *
5069965e507bSrm88369 	 * then we already passed through graph_runlevel_changed() (by the way
5070965e507bSrm88369 	 * of dgraph_set_milestone()) and 'current_runlevel' may have changed
5071965e507bSrm88369 	 * and already be equal to 'rl' so we are going to return immediately
5072965e507bSrm88369 	 * from dgraph_set_runlevel() without changing the system runlevel and
5073965e507bSrm88369 	 * without executing the /etc/rc?.d/K* scripts.
5074965e507bSrm88369 	 */
50757c478bd9Sstevel@tonic-gate 	if (rl == current_runlevel) {
50767c478bd9Sstevel@tonic-gate 		ms = NULL;
50777c478bd9Sstevel@tonic-gate 		goto out;
50787c478bd9Sstevel@tonic-gate 	}
50797c478bd9Sstevel@tonic-gate 
50807c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Changing to runlevel '%c'.\n", rl);
50817c478bd9Sstevel@tonic-gate 
50827c478bd9Sstevel@tonic-gate 	/*
50837c478bd9Sstevel@tonic-gate 	 * Make sure stop rc scripts see the new settings via who -r.
50847c478bd9Sstevel@tonic-gate 	 */
50857c478bd9Sstevel@tonic-gate 	utmpx_set_runlevel(rl, current_runlevel, B_TRUE);
50867c478bd9Sstevel@tonic-gate 
50877c478bd9Sstevel@tonic-gate 	/*
50887c478bd9Sstevel@tonic-gate 	 * Some run levels don't have a direct correspondence to any
50897c478bd9Sstevel@tonic-gate 	 * milestones, so we have to signal init directly.
50907c478bd9Sstevel@tonic-gate 	 */
50917c478bd9Sstevel@tonic-gate 	if (mark_rl) {
50927c478bd9Sstevel@tonic-gate 		current_runlevel = rl;
50937c478bd9Sstevel@tonic-gate 		signal_init(rl);
50947c478bd9Sstevel@tonic-gate 	}
50957c478bd9Sstevel@tonic-gate 
50967c478bd9Sstevel@tonic-gate 	switch (rl) {
50977c478bd9Sstevel@tonic-gate 	case 'S':
50987c478bd9Sstevel@tonic-gate 		uu_warn("The system is coming down for administration.  "
50997c478bd9Sstevel@tonic-gate 		    "Please wait.\n");
51007c478bd9Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_FALSE);
51017c478bd9Sstevel@tonic-gate 		ms = single_user_fmri;
51027c478bd9Sstevel@tonic-gate 		go_single_user_mode = B_TRUE;
51037c478bd9Sstevel@tonic-gate 		break;
51047c478bd9Sstevel@tonic-gate 
51057c478bd9Sstevel@tonic-gate 	case '0':
51064d53c7adSDan Price 		halting_time = time(NULL);
51077c478bd9Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
51087c478bd9Sstevel@tonic-gate 		halting = AD_HALT;
51097c478bd9Sstevel@tonic-gate 		goto uadmin;
51107c478bd9Sstevel@tonic-gate 
51117c478bd9Sstevel@tonic-gate 	case '5':
51124d53c7adSDan Price 		halting_time = time(NULL);
51137c478bd9Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
51147c478bd9Sstevel@tonic-gate 		halting = AD_POWEROFF;
51157c478bd9Sstevel@tonic-gate 		goto uadmin;
51167c478bd9Sstevel@tonic-gate 
51177c478bd9Sstevel@tonic-gate 	case '6':
51184d53c7adSDan Price 		halting_time = time(NULL);
51197c478bd9Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
5120753a6d45SSherry Moore 		if (scf_is_fastboot_default() && getzoneid() == GLOBAL_ZONEID)
5121753a6d45SSherry Moore 			halting = AD_FASTREBOOT;
5122753a6d45SSherry Moore 		else
51237c478bd9Sstevel@tonic-gate 			halting = AD_BOOT;
51247c478bd9Sstevel@tonic-gate 
51257c478bd9Sstevel@tonic-gate uadmin:
51267c478bd9Sstevel@tonic-gate 		uu_warn("The system is coming down.  Please wait.\n");
51277c478bd9Sstevel@tonic-gate 		ms = "none";
51287c478bd9Sstevel@tonic-gate 
51297c478bd9Sstevel@tonic-gate 		/*
51307c478bd9Sstevel@tonic-gate 		 * We can't wait until all services are offline since this
51317c478bd9Sstevel@tonic-gate 		 * thread is responsible for taking them offline.  Instead we
51327c478bd9Sstevel@tonic-gate 		 * set halting to the second argument for uadmin() and call
51337c478bd9Sstevel@tonic-gate 		 * do_uadmin() from dgraph_set_instance_state() when
51347c478bd9Sstevel@tonic-gate 		 * appropriate.
51357c478bd9Sstevel@tonic-gate 		 */
51367c478bd9Sstevel@tonic-gate 		break;
51377c478bd9Sstevel@tonic-gate 
51387c478bd9Sstevel@tonic-gate 	case '1':
51397c478bd9Sstevel@tonic-gate 		if (current_runlevel != 'S') {
51407c478bd9Sstevel@tonic-gate 			uu_warn("Changing to state 1.\n");
51417c478bd9Sstevel@tonic-gate 			fork_rc_script(rl, stop, B_FALSE);
51427c478bd9Sstevel@tonic-gate 		} else {
51437c478bd9Sstevel@tonic-gate 			uu_warn("The system is coming up for administration.  "
51447c478bd9Sstevel@tonic-gate 			    "Please wait.\n");
51457c478bd9Sstevel@tonic-gate 		}
51467c478bd9Sstevel@tonic-gate 		ms = single_user_fmri;
51477c478bd9Sstevel@tonic-gate 		go_to_level1 = B_TRUE;
51487c478bd9Sstevel@tonic-gate 		break;
51497c478bd9Sstevel@tonic-gate 
51507c478bd9Sstevel@tonic-gate 	case '2':
51517c478bd9Sstevel@tonic-gate 		if (current_runlevel == '3' || current_runlevel == '4')
51527c478bd9Sstevel@tonic-gate 			fork_rc_script(rl, stop, B_FALSE);
51537c478bd9Sstevel@tonic-gate 		ms = multi_user_fmri;
51547c478bd9Sstevel@tonic-gate 		break;
51557c478bd9Sstevel@tonic-gate 
51567c478bd9Sstevel@tonic-gate 	case '3':
51577c478bd9Sstevel@tonic-gate 	case '4':
51587c478bd9Sstevel@tonic-gate 		ms = "all";
51597c478bd9Sstevel@tonic-gate 		break;
51607c478bd9Sstevel@tonic-gate 
51617c478bd9Sstevel@tonic-gate 	default:
51627c478bd9Sstevel@tonic-gate #ifndef NDEBUG
51637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s:%d: Uncaught case %d ('%c').\n",
51647c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__, rl, rl);
51657c478bd9Sstevel@tonic-gate #endif
51667c478bd9Sstevel@tonic-gate 		abort();
51677c478bd9Sstevel@tonic-gate 	}
51687c478bd9Sstevel@tonic-gate 
51697c478bd9Sstevel@tonic-gate out:
51707c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
51717c478bd9Sstevel@tonic-gate 
51727c478bd9Sstevel@tonic-gate nolock_out:
51737c478bd9Sstevel@tonic-gate 	switch (r = libscf_clear_runlevel(pg, ms)) {
51747c478bd9Sstevel@tonic-gate 	case 0:
51757c478bd9Sstevel@tonic-gate 		break;
51767c478bd9Sstevel@tonic-gate 
51777c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
51787c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
51797c478bd9Sstevel@tonic-gate 		rebound = B_TRUE;
51807c478bd9Sstevel@tonic-gate 		goto nolock_out;
51817c478bd9Sstevel@tonic-gate 
51827c478bd9Sstevel@tonic-gate 	case ECANCELED:
51837c478bd9Sstevel@tonic-gate 		break;
51847c478bd9Sstevel@tonic-gate 
51857c478bd9Sstevel@tonic-gate 	case EPERM:
51867c478bd9Sstevel@tonic-gate 	case EACCES:
51877c478bd9Sstevel@tonic-gate 	case EROFS:
51887c478bd9Sstevel@tonic-gate 		log_error(LOG_NOTICE, "Could not delete \"%s/%s\" property: "
51897c478bd9Sstevel@tonic-gate 		    "%s.\n", SCF_PG_OPTIONS, "runlevel", strerror(r));
51907c478bd9Sstevel@tonic-gate 		break;
51917c478bd9Sstevel@tonic-gate 
51927c478bd9Sstevel@tonic-gate 	default:
51937c478bd9Sstevel@tonic-gate 		bad_error("libscf_clear_runlevel", r);
51947c478bd9Sstevel@tonic-gate 	}
51957c478bd9Sstevel@tonic-gate 
51967c478bd9Sstevel@tonic-gate 	return (rebound ? ECONNRESET : 0);
51977c478bd9Sstevel@tonic-gate }
51987c478bd9Sstevel@tonic-gate 
5199aca380d7SRenaud Manus /*
5200aca380d7SRenaud Manus  * mark_subtree walks the dependents and add the GV_TOOFFLINE flag
5201aca380d7SRenaud Manus  * to the instances that are supposed to go offline during an
5202aca380d7SRenaud Manus  * administrative disable operation.
5203aca380d7SRenaud Manus  */
5204aca380d7SRenaud Manus static int
5205aca380d7SRenaud Manus mark_subtree(graph_edge_t *e, void *arg)
5206aca380d7SRenaud Manus {
5207aca380d7SRenaud Manus 	graph_vertex_t *v;
5208aca380d7SRenaud Manus 	int r;
5209aca380d7SRenaud Manus 
5210aca380d7SRenaud Manus 	v = e->ge_vertex;
5211aca380d7SRenaud Manus 
5212aca380d7SRenaud Manus 	/* If it's already in the subgraph, skip. */
5213aca380d7SRenaud Manus 	if (v->gv_flags & GV_TOOFFLINE)
5214aca380d7SRenaud Manus 		return (UU_WALK_NEXT);
5215aca380d7SRenaud Manus 
5216aca380d7SRenaud Manus 	switch (v->gv_type) {
5217aca380d7SRenaud Manus 	case GVT_INST:
5218aca380d7SRenaud Manus 		/* If the instance is already disabled, skip it. */
5219aca380d7SRenaud Manus 		if (!(v->gv_flags & GV_ENABLED))
5220aca380d7SRenaud Manus 			return (UU_WALK_NEXT);
5221aca380d7SRenaud Manus 
5222aca380d7SRenaud Manus 		v->gv_flags |= GV_TOOFFLINE;
5223aca380d7SRenaud Manus 		log_framework(LOG_DEBUG, "%s added to subtree\n", v->gv_name);
5224aca380d7SRenaud Manus 		break;
5225aca380d7SRenaud Manus 	case GVT_GROUP:
5226aca380d7SRenaud Manus 		/*
5227207246e9SRenaud Manus 		 * Skip all excluded and optional_all dependencies and decide
5228207246e9SRenaud Manus 		 * whether to offline the service based on restart_on attribute.
5229aca380d7SRenaud Manus 		 */
5230aca380d7SRenaud Manus 		if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL ||
5231207246e9SRenaud Manus 		    v->gv_depgroup == DEPGRP_OPTIONAL_ALL ||
5232aca380d7SRenaud Manus 		    v->gv_restart < RERR_RESTART)
5233aca380d7SRenaud Manus 			return (UU_WALK_NEXT);
5234aca380d7SRenaud Manus 		break;
5235aca380d7SRenaud Manus 	}
5236aca380d7SRenaud Manus 
5237aca380d7SRenaud Manus 	r = uu_list_walk(v->gv_dependents, (uu_walk_fn_t *)mark_subtree, arg,
5238aca380d7SRenaud Manus 	    0);
5239aca380d7SRenaud Manus 	assert(r == 0);
5240aca380d7SRenaud Manus 	return (UU_WALK_NEXT);
5241aca380d7SRenaud Manus }
5242aca380d7SRenaud Manus 
52437c478bd9Sstevel@tonic-gate static int
52447c478bd9Sstevel@tonic-gate mark_subgraph(graph_edge_t *e, void *arg)
52457c478bd9Sstevel@tonic-gate {
52467c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
52477c478bd9Sstevel@tonic-gate 	int r;
52487c478bd9Sstevel@tonic-gate 	int optional = (int)arg;
52497c478bd9Sstevel@tonic-gate 
52507c478bd9Sstevel@tonic-gate 	v = e->ge_vertex;
52517c478bd9Sstevel@tonic-gate 
52527c478bd9Sstevel@tonic-gate 	/* If it's already in the subgraph, skip. */
52537c478bd9Sstevel@tonic-gate 	if (v->gv_flags & GV_INSUBGRAPH)
52547c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
52557c478bd9Sstevel@tonic-gate 
52567c478bd9Sstevel@tonic-gate 	/*
52577c478bd9Sstevel@tonic-gate 	 * Keep track if walk has entered an optional dependency group
52587c478bd9Sstevel@tonic-gate 	 */
52597c478bd9Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_OPTIONAL_ALL) {
52607c478bd9Sstevel@tonic-gate 		optional = 1;
52617c478bd9Sstevel@tonic-gate 	}
52627c478bd9Sstevel@tonic-gate 	/*
52637c478bd9Sstevel@tonic-gate 	 * Quit if we are in an optional dependency group and the instance
52647c478bd9Sstevel@tonic-gate 	 * is disabled
52657c478bd9Sstevel@tonic-gate 	 */
52667c478bd9Sstevel@tonic-gate 	if (optional && (v->gv_type == GVT_INST) &&
52677c478bd9Sstevel@tonic-gate 	    (!(v->gv_flags & GV_ENBLD_NOOVR)))
52687c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
52697c478bd9Sstevel@tonic-gate 
52707c478bd9Sstevel@tonic-gate 	v->gv_flags |= GV_INSUBGRAPH;
52717c478bd9Sstevel@tonic-gate 
52727c478bd9Sstevel@tonic-gate 	/* Skip all excluded dependencies. */
52737c478bd9Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
52747c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
52757c478bd9Sstevel@tonic-gate 
52767c478bd9Sstevel@tonic-gate 	r = uu_list_walk(v->gv_dependencies, (uu_walk_fn_t *)mark_subgraph,
52777c478bd9Sstevel@tonic-gate 	    (void *)optional, 0);
52787c478bd9Sstevel@tonic-gate 	assert(r == 0);
52797c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
52807c478bd9Sstevel@tonic-gate }
52817c478bd9Sstevel@tonic-gate 
52827c478bd9Sstevel@tonic-gate /*
528356e23938Sbustos  * Bring down all services which are not dependencies of fmri.  The
528456e23938Sbustos  * dependencies of fmri (direct & indirect) will constitute the "subgraph",
528556e23938Sbustos  * and will have the GV_INSUBGRAPH flag set.  The rest must be brought down,
528656e23938Sbustos  * which means the state is "disabled", "maintenance", or "uninitialized".  We
528756e23938Sbustos  * could consider "offline" to be down, and refrain from sending start
528856e23938Sbustos  * commands for such services, but that's not strictly necessary, so we'll
528956e23938Sbustos  * decline to intrude on the state machine.  It would probably confuse users
529056e23938Sbustos  * anyway.
529156e23938Sbustos  *
529256e23938Sbustos  * The services should be brought down in reverse-dependency order, so we
529356e23938Sbustos  * can't do it all at once here.  We initiate by override-disabling the leaves
529456e23938Sbustos  * of the dependency tree -- those services which are up but have no
529556e23938Sbustos  * dependents which are up.  When they come down,
529656e23938Sbustos  * vertex_subgraph_dependencies_shutdown() will override-disable the newly
529756e23938Sbustos  * exposed leaves.  Perseverance will ensure completion.
529856e23938Sbustos  *
529956e23938Sbustos  * Sometimes we need to take action when the transition is complete, like
530056e23938Sbustos  * start sulogin or halt the system.  To tell when we're done, we initialize
530156e23938Sbustos  * non_subgraph_svcs here to be the number of services which need to come
530256e23938Sbustos  * down.  As each does, we decrement the counter.  When it hits zero, we take
530356e23938Sbustos  * the appropriate action.  See vertex_subgraph_dependencies_shutdown().
530456e23938Sbustos  *
530556e23938Sbustos  * In case we're coming up, we also remove any enable-overrides for the
530656e23938Sbustos  * services which are dependencies of fmri.
53077c478bd9Sstevel@tonic-gate  *
53087c478bd9Sstevel@tonic-gate  * If norepository is true, the function will not change the repository.
53097c478bd9Sstevel@tonic-gate  *
5310965e507bSrm88369  * The decision to change the system run level in accordance with the milestone
5311965e507bSrm88369  * is taken in dgraph_set_runlevel().
5312965e507bSrm88369  *
53137c478bd9Sstevel@tonic-gate  * Returns
53147c478bd9Sstevel@tonic-gate  *   0 - success
53157c478bd9Sstevel@tonic-gate  *   ECONNRESET - success, but handle was rebound
53167c478bd9Sstevel@tonic-gate  *   EINVAL - fmri is invalid (error is logged)
53177c478bd9Sstevel@tonic-gate  *   EALREADY - the milestone is already set to fmri
53187c478bd9Sstevel@tonic-gate  *   ENOENT - a configured vertex does not exist for fmri (an error is logged)
53197c478bd9Sstevel@tonic-gate  */
53207c478bd9Sstevel@tonic-gate static int
53217c478bd9Sstevel@tonic-gate dgraph_set_milestone(const char *fmri, scf_handle_t *h, boolean_t norepository)
53227c478bd9Sstevel@tonic-gate {
53237c478bd9Sstevel@tonic-gate 	const char *cfmri, *fs;
53247c478bd9Sstevel@tonic-gate 	graph_vertex_t *nm, *v;
53257c478bd9Sstevel@tonic-gate 	int ret = 0, r;
53267c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
53277c478bd9Sstevel@tonic-gate 	boolean_t isall, isnone, rebound = B_FALSE;
53287c478bd9Sstevel@tonic-gate 
53297c478bd9Sstevel@tonic-gate 	/* Validate fmri */
53307c478bd9Sstevel@tonic-gate 	isall = (strcmp(fmri, "all") == 0);
53317c478bd9Sstevel@tonic-gate 	isnone = (strcmp(fmri, "none") == 0);
53327c478bd9Sstevel@tonic-gate 
53337c478bd9Sstevel@tonic-gate 	if (!isall && !isnone) {
53347c478bd9Sstevel@tonic-gate 		if (fmri_canonify(fmri, (char **)&cfmri, B_FALSE) == EINVAL)
53357c478bd9Sstevel@tonic-gate 			goto reject;
53367c478bd9Sstevel@tonic-gate 
53377c478bd9Sstevel@tonic-gate 		if (strcmp(cfmri, single_user_fmri) != 0 &&
53387c478bd9Sstevel@tonic-gate 		    strcmp(cfmri, multi_user_fmri) != 0 &&
53397c478bd9Sstevel@tonic-gate 		    strcmp(cfmri, multi_user_svr_fmri) != 0) {
53407c478bd9Sstevel@tonic-gate 			startd_free((void *)cfmri, max_scf_fmri_size);
53417c478bd9Sstevel@tonic-gate reject:
53427c478bd9Sstevel@tonic-gate 			log_framework(LOG_WARNING,
53437c478bd9Sstevel@tonic-gate 			    "Rejecting request for invalid milestone \"%s\".\n",
53447c478bd9Sstevel@tonic-gate 			    fmri);
53457c478bd9Sstevel@tonic-gate 			return (EINVAL);
53467c478bd9Sstevel@tonic-gate 		}
53477c478bd9Sstevel@tonic-gate 	}
53487c478bd9Sstevel@tonic-gate 
53497c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
53507c478bd9Sstevel@tonic-gate 
53517c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
53527c478bd9Sstevel@tonic-gate 
53537c478bd9Sstevel@tonic-gate 	if (milestone == NULL) {
53547c478bd9Sstevel@tonic-gate 		if (isall) {
53557c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
53567c478bd9Sstevel@tonic-gate 			    "Milestone already set to all.\n");
53577c478bd9Sstevel@tonic-gate 			ret = EALREADY;
53587c478bd9Sstevel@tonic-gate 			goto out;
53597c478bd9Sstevel@tonic-gate 		}
53607c478bd9Sstevel@tonic-gate 	} else if (milestone == MILESTONE_NONE) {
53617c478bd9Sstevel@tonic-gate 		if (isnone) {
53627c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
53637c478bd9Sstevel@tonic-gate 			    "Milestone already set to none.\n");
53647c478bd9Sstevel@tonic-gate 			ret = EALREADY;
53657c478bd9Sstevel@tonic-gate 			goto out;
53667c478bd9Sstevel@tonic-gate 		}
53677c478bd9Sstevel@tonic-gate 	} else {
53687c478bd9Sstevel@tonic-gate 		if (!isall && !isnone &&
53697c478bd9Sstevel@tonic-gate 		    strcmp(cfmri, milestone->gv_name) == 0) {
53707c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
53717c478bd9Sstevel@tonic-gate 			    "Milestone already set to %s.\n", cfmri);
53727c478bd9Sstevel@tonic-gate 			ret = EALREADY;
53737c478bd9Sstevel@tonic-gate 			goto out;
53747c478bd9Sstevel@tonic-gate 		}
53757c478bd9Sstevel@tonic-gate 	}
53767c478bd9Sstevel@tonic-gate 
53777c478bd9Sstevel@tonic-gate 	if (!isall && !isnone) {
53787c478bd9Sstevel@tonic-gate 		nm = vertex_get_by_name(cfmri);
53797c478bd9Sstevel@tonic-gate 		if (nm == NULL || !(nm->gv_flags & GV_CONFIGURED)) {
53807c478bd9Sstevel@tonic-gate 			log_framework(LOG_WARNING, "Cannot set milestone to %s "
53817c478bd9Sstevel@tonic-gate 			    "because no such service exists.\n", cfmri);
53827c478bd9Sstevel@tonic-gate 			ret = ENOENT;
53837c478bd9Sstevel@tonic-gate 			goto out;
53847c478bd9Sstevel@tonic-gate 		}
53857c478bd9Sstevel@tonic-gate 	}
53867c478bd9Sstevel@tonic-gate 
53877c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Changing milestone to %s.\n", fmri);
53887c478bd9Sstevel@tonic-gate 
53897c478bd9Sstevel@tonic-gate 	/*
53907c478bd9Sstevel@tonic-gate 	 * Set milestone, removing the old one if this was the last reference.
53917c478bd9Sstevel@tonic-gate 	 */
53923ad28c1eSrm88369 	if (milestone > MILESTONE_NONE)
53933ad28c1eSrm88369 		(void) vertex_unref(milestone);
53947c478bd9Sstevel@tonic-gate 
53957c478bd9Sstevel@tonic-gate 	if (isall)
53967c478bd9Sstevel@tonic-gate 		milestone = NULL;
53977c478bd9Sstevel@tonic-gate 	else if (isnone)
53987c478bd9Sstevel@tonic-gate 		milestone = MILESTONE_NONE;
53993ad28c1eSrm88369 	else {
54007c478bd9Sstevel@tonic-gate 		milestone = nm;
54013ad28c1eSrm88369 		/* milestone should count as a reference */
54023ad28c1eSrm88369 		vertex_ref(milestone);
54033ad28c1eSrm88369 	}
54047c478bd9Sstevel@tonic-gate 
54057c478bd9Sstevel@tonic-gate 	/* Clear all GV_INSUBGRAPH bits. */
54067c478bd9Sstevel@tonic-gate 	for (v = uu_list_first(dgraph); v != NULL; v = uu_list_next(dgraph, v))
54077c478bd9Sstevel@tonic-gate 		v->gv_flags &= ~GV_INSUBGRAPH;
54087c478bd9Sstevel@tonic-gate 
54097c478bd9Sstevel@tonic-gate 	if (!isall && !isnone) {
54107c478bd9Sstevel@tonic-gate 		/* Set GV_INSUBGRAPH for milestone & descendents. */
54117c478bd9Sstevel@tonic-gate 		milestone->gv_flags |= GV_INSUBGRAPH;
54127c478bd9Sstevel@tonic-gate 
54137c478bd9Sstevel@tonic-gate 		r = uu_list_walk(milestone->gv_dependencies,
54147c478bd9Sstevel@tonic-gate 		    (uu_walk_fn_t *)mark_subgraph, NULL, 0);
54157c478bd9Sstevel@tonic-gate 		assert(r == 0);
54167c478bd9Sstevel@tonic-gate 	}
54177c478bd9Sstevel@tonic-gate 
54187c478bd9Sstevel@tonic-gate 	/* Un-override services in the subgraph & override-disable the rest. */
54197c478bd9Sstevel@tonic-gate 	if (norepository)
54207c478bd9Sstevel@tonic-gate 		goto out;
54217c478bd9Sstevel@tonic-gate 
54227c478bd9Sstevel@tonic-gate 	non_subgraph_svcs = 0;
54237c478bd9Sstevel@tonic-gate 	for (v = uu_list_first(dgraph);
54247c478bd9Sstevel@tonic-gate 	    v != NULL;
54257c478bd9Sstevel@tonic-gate 	    v = uu_list_next(dgraph, v)) {
54267c478bd9Sstevel@tonic-gate 		if (v->gv_type != GVT_INST ||
54277c478bd9Sstevel@tonic-gate 		    (v->gv_flags & GV_CONFIGURED) == 0)
54287c478bd9Sstevel@tonic-gate 			continue;
54297c478bd9Sstevel@tonic-gate 
54307c478bd9Sstevel@tonic-gate again:
54317c478bd9Sstevel@tonic-gate 		r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst,
54327c478bd9Sstevel@tonic-gate 		    NULL, NULL, SCF_DECODE_FMRI_EXACT);
54337c478bd9Sstevel@tonic-gate 		if (r != 0) {
54347c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
54357c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
54367c478bd9Sstevel@tonic-gate 			default:
54377c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
54387c478bd9Sstevel@tonic-gate 				rebound = B_TRUE;
54397c478bd9Sstevel@tonic-gate 				goto again;
54407c478bd9Sstevel@tonic-gate 
54417c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
54427c478bd9Sstevel@tonic-gate 				continue;
54437c478bd9Sstevel@tonic-gate 
54447c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
54457c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
54467c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
54477c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
54487c478bd9Sstevel@tonic-gate 				bad_error("scf_handle_decode_fmri",
54497c478bd9Sstevel@tonic-gate 				    scf_error());
54507c478bd9Sstevel@tonic-gate 			}
54517c478bd9Sstevel@tonic-gate 		}
54527c478bd9Sstevel@tonic-gate 
54537c478bd9Sstevel@tonic-gate 		if (isall || (v->gv_flags & GV_INSUBGRAPH)) {
54547c478bd9Sstevel@tonic-gate 			r = libscf_delete_enable_ovr(inst);
54557c478bd9Sstevel@tonic-gate 			fs = "libscf_delete_enable_ovr";
54567c478bd9Sstevel@tonic-gate 		} else {
54577c478bd9Sstevel@tonic-gate 			assert(isnone || (v->gv_flags & GV_INSUBGRAPH) == 0);
54587c478bd9Sstevel@tonic-gate 
545956e23938Sbustos 			/*
546056e23938Sbustos 			 * Services which are up need to come down before
546156e23938Sbustos 			 * we're done, but we can only disable the leaves
546256e23938Sbustos 			 * here.
546356e23938Sbustos 			 */
546456e23938Sbustos 
546556e23938Sbustos 			if (up_state(v->gv_state))
54667c478bd9Sstevel@tonic-gate 				++non_subgraph_svcs;
54677c478bd9Sstevel@tonic-gate 
546856e23938Sbustos 			/* If it's already disabled, don't bother. */
546956e23938Sbustos 			if ((v->gv_flags & GV_ENABLED) == 0)
547056e23938Sbustos 				continue;
547156e23938Sbustos 
547256e23938Sbustos 			if (!is_nonsubgraph_leaf(v))
54737c478bd9Sstevel@tonic-gate 				continue;
54747c478bd9Sstevel@tonic-gate 
54757c478bd9Sstevel@tonic-gate 			r = libscf_set_enable_ovr(inst, 0);
54767c478bd9Sstevel@tonic-gate 			fs = "libscf_set_enable_ovr";
54777c478bd9Sstevel@tonic-gate 		}
54787c478bd9Sstevel@tonic-gate 		switch (r) {
54797c478bd9Sstevel@tonic-gate 		case 0:
54807c478bd9Sstevel@tonic-gate 		case ECANCELED:
54817c478bd9Sstevel@tonic-gate 			break;
54827c478bd9Sstevel@tonic-gate 
54837c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
54847c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
54857c478bd9Sstevel@tonic-gate 			rebound = B_TRUE;
54867c478bd9Sstevel@tonic-gate 			goto again;
54877c478bd9Sstevel@tonic-gate 
54887c478bd9Sstevel@tonic-gate 		case EPERM:
54897c478bd9Sstevel@tonic-gate 		case EROFS:
54907c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
54917c478bd9Sstevel@tonic-gate 			    "Could not set %s/%s for %s: %s.\n",
54927c478bd9Sstevel@tonic-gate 			    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
54937c478bd9Sstevel@tonic-gate 			    v->gv_name, strerror(r));
54947c478bd9Sstevel@tonic-gate 			break;
54957c478bd9Sstevel@tonic-gate 
54967c478bd9Sstevel@tonic-gate 		default:
54977c478bd9Sstevel@tonic-gate 			bad_error(fs, r);
54987c478bd9Sstevel@tonic-gate 		}
54997c478bd9Sstevel@tonic-gate 	}
55007c478bd9Sstevel@tonic-gate 
55017c478bd9Sstevel@tonic-gate 	if (halting != -1) {
55027c478bd9Sstevel@tonic-gate 		if (non_subgraph_svcs > 1)
55037c478bd9Sstevel@tonic-gate 			uu_warn("%d system services are now being stopped.\n",
55047c478bd9Sstevel@tonic-gate 			    non_subgraph_svcs);
55057c478bd9Sstevel@tonic-gate 		else if (non_subgraph_svcs == 1)
55067c478bd9Sstevel@tonic-gate 			uu_warn("One system service is now being stopped.\n");
55077c478bd9Sstevel@tonic-gate 		else if (non_subgraph_svcs == 0)
55087c478bd9Sstevel@tonic-gate 			do_uadmin();
55097c478bd9Sstevel@tonic-gate 	}
55107c478bd9Sstevel@tonic-gate 
55117c478bd9Sstevel@tonic-gate 	ret = rebound ? ECONNRESET : 0;
55127c478bd9Sstevel@tonic-gate 
55137c478bd9Sstevel@tonic-gate out:
55147c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
55157c478bd9Sstevel@tonic-gate 	if (!isall && !isnone)
55167c478bd9Sstevel@tonic-gate 		startd_free((void *)cfmri, max_scf_fmri_size);
55177c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
55187c478bd9Sstevel@tonic-gate 	return (ret);
55197c478bd9Sstevel@tonic-gate }
55207c478bd9Sstevel@tonic-gate 
55217c478bd9Sstevel@tonic-gate 
55227c478bd9Sstevel@tonic-gate /*
55237c478bd9Sstevel@tonic-gate  * Returns 0, ECONNABORTED, or EINVAL.
55247c478bd9Sstevel@tonic-gate  */
55257c478bd9Sstevel@tonic-gate static int
55267c478bd9Sstevel@tonic-gate handle_graph_update_event(scf_handle_t *h, graph_protocol_event_t *e)
55277c478bd9Sstevel@tonic-gate {
55287c478bd9Sstevel@tonic-gate 	int r;
55297c478bd9Sstevel@tonic-gate 
55307c478bd9Sstevel@tonic-gate 	switch (e->gpe_type) {
55317c478bd9Sstevel@tonic-gate 	case GRAPH_UPDATE_RELOAD_GRAPH:
55327c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
55337c478bd9Sstevel@tonic-gate 		    "graph_event: reload graph unimplemented\n");
55347c478bd9Sstevel@tonic-gate 		break;
55357c478bd9Sstevel@tonic-gate 
55367c478bd9Sstevel@tonic-gate 	case GRAPH_UPDATE_STATE_CHANGE: {
55377c478bd9Sstevel@tonic-gate 		protocol_states_t *states = e->gpe_data;
55387c478bd9Sstevel@tonic-gate 
55397c478bd9Sstevel@tonic-gate 		switch (r = dgraph_set_instance_state(h, e->gpe_inst,
55407c478bd9Sstevel@tonic-gate 		    states->ps_state, states->ps_err)) {
55417c478bd9Sstevel@tonic-gate 		case 0:
55427c478bd9Sstevel@tonic-gate 		case ENOENT:
55437c478bd9Sstevel@tonic-gate 			break;
55447c478bd9Sstevel@tonic-gate 
55457c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
55467c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
55477c478bd9Sstevel@tonic-gate 
55487c478bd9Sstevel@tonic-gate 		case EINVAL:
55497c478bd9Sstevel@tonic-gate 		default:
55507c478bd9Sstevel@tonic-gate #ifndef NDEBUG
55517c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "dgraph_set_instance_state() "
55527c478bd9Sstevel@tonic-gate 			    "failed with unexpected error %d at %s:%d.\n", r,
55537c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__);
55547c478bd9Sstevel@tonic-gate #endif
55557c478bd9Sstevel@tonic-gate 			abort();
55567c478bd9Sstevel@tonic-gate 		}
55577c478bd9Sstevel@tonic-gate 
55587c478bd9Sstevel@tonic-gate 		startd_free(states, sizeof (protocol_states_t));
55597c478bd9Sstevel@tonic-gate 		break;
55607c478bd9Sstevel@tonic-gate 	}
55617c478bd9Sstevel@tonic-gate 
55627c478bd9Sstevel@tonic-gate 	default:
55637c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
55647c478bd9Sstevel@tonic-gate 		    "graph_event_loop received an unknown event: %d\n",
55657c478bd9Sstevel@tonic-gate 		    e->gpe_type);
55667c478bd9Sstevel@tonic-gate 		break;
55677c478bd9Sstevel@tonic-gate 	}
55687c478bd9Sstevel@tonic-gate 
55697c478bd9Sstevel@tonic-gate 	return (0);
55707c478bd9Sstevel@tonic-gate }
55717c478bd9Sstevel@tonic-gate 
55727c478bd9Sstevel@tonic-gate /*
55737c478bd9Sstevel@tonic-gate  * graph_event_thread()
55747c478bd9Sstevel@tonic-gate  *    Wait for state changes from the restarters.
55757c478bd9Sstevel@tonic-gate  */
55767c478bd9Sstevel@tonic-gate /*ARGSUSED*/
55777c478bd9Sstevel@tonic-gate void *
55787c478bd9Sstevel@tonic-gate graph_event_thread(void *unused)
55797c478bd9Sstevel@tonic-gate {
55807c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
55817c478bd9Sstevel@tonic-gate 	int err;
55827c478bd9Sstevel@tonic-gate 
55837c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
55847c478bd9Sstevel@tonic-gate 
55857c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
55867c478bd9Sstevel@tonic-gate 	while (1) {
55877c478bd9Sstevel@tonic-gate 		graph_protocol_event_t *e;
55887c478bd9Sstevel@tonic-gate 
55897c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&gu->gu_lock);
55907c478bd9Sstevel@tonic-gate 
55917c478bd9Sstevel@tonic-gate 		while (gu->gu_wakeup == 0)
55927c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&gu->gu_cv, &gu->gu_lock);
55937c478bd9Sstevel@tonic-gate 
55947c478bd9Sstevel@tonic-gate 		gu->gu_wakeup = 0;
55957c478bd9Sstevel@tonic-gate 
55967c478bd9Sstevel@tonic-gate 		while ((e = graph_event_dequeue()) != NULL) {
55977c478bd9Sstevel@tonic-gate 			MUTEX_LOCK(&e->gpe_lock);
55987c478bd9Sstevel@tonic-gate 			MUTEX_UNLOCK(&gu->gu_lock);
55997c478bd9Sstevel@tonic-gate 
56007c478bd9Sstevel@tonic-gate 			while ((err = handle_graph_update_event(h, e)) ==
56017c478bd9Sstevel@tonic-gate 			    ECONNABORTED)
56027c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
56037c478bd9Sstevel@tonic-gate 
56047c478bd9Sstevel@tonic-gate 			if (err == 0)
56057c478bd9Sstevel@tonic-gate 				graph_event_release(e);
56067c478bd9Sstevel@tonic-gate 			else
56077c478bd9Sstevel@tonic-gate 				graph_event_requeue(e);
56087c478bd9Sstevel@tonic-gate 
56097c478bd9Sstevel@tonic-gate 			MUTEX_LOCK(&gu->gu_lock);
56107c478bd9Sstevel@tonic-gate 		}
56117c478bd9Sstevel@tonic-gate 
56127c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&gu->gu_lock);
56137c478bd9Sstevel@tonic-gate 	}
56147c478bd9Sstevel@tonic-gate 
56157c478bd9Sstevel@tonic-gate 	/*
56167c478bd9Sstevel@tonic-gate 	 * Unreachable for now -- there's currently no graceful cleanup
56177c478bd9Sstevel@tonic-gate 	 * called on exit().
56187c478bd9Sstevel@tonic-gate 	 */
56197c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&gu->gu_lock);
56207c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
56217c478bd9Sstevel@tonic-gate 	return (NULL);
56227c478bd9Sstevel@tonic-gate }
56237c478bd9Sstevel@tonic-gate 
56247c478bd9Sstevel@tonic-gate static void
56257c478bd9Sstevel@tonic-gate set_initial_milestone(scf_handle_t *h)
56267c478bd9Sstevel@tonic-gate {
56277c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
56287c478bd9Sstevel@tonic-gate 	char *fmri, *cfmri;
56297c478bd9Sstevel@tonic-gate 	size_t sz;
56307c478bd9Sstevel@tonic-gate 	int r;
56317c478bd9Sstevel@tonic-gate 
56327c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
56337c478bd9Sstevel@tonic-gate 	fmri = startd_alloc(max_scf_fmri_size);
56347c478bd9Sstevel@tonic-gate 
56357c478bd9Sstevel@tonic-gate 	/*
56367c478bd9Sstevel@tonic-gate 	 * If -m milestone= was specified, we want to set options_ovr/milestone
56377c478bd9Sstevel@tonic-gate 	 * to it.  Otherwise we want to read what the milestone should be set
56387c478bd9Sstevel@tonic-gate 	 * to.  Either way we need our inst.
56397c478bd9Sstevel@tonic-gate 	 */
56407c478bd9Sstevel@tonic-gate get_self:
56417c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
56427c478bd9Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
56437c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
56447c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
56457c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
56467c478bd9Sstevel@tonic-gate 			goto get_self;
56477c478bd9Sstevel@tonic-gate 
56487c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
56497c478bd9Sstevel@tonic-gate 			if (st->st_subgraph != NULL &&
56507c478bd9Sstevel@tonic-gate 			    st->st_subgraph[0] != '\0') {
56517c478bd9Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
56527c478bd9Sstevel@tonic-gate 				    max_scf_fmri_size);
56537c478bd9Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
56547c478bd9Sstevel@tonic-gate 			} else {
56557c478bd9Sstevel@tonic-gate 				fmri[0] = '\0';
56567c478bd9Sstevel@tonic-gate 			}
56577c478bd9Sstevel@tonic-gate 			break;
56587c478bd9Sstevel@tonic-gate 
56597c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
56607c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
56617c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
56627c478bd9Sstevel@tonic-gate 		default:
56637c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
56647c478bd9Sstevel@tonic-gate 		}
56657c478bd9Sstevel@tonic-gate 	} else {
56667c478bd9Sstevel@tonic-gate 		if (st->st_subgraph != NULL && st->st_subgraph[0] != '\0') {
56677c478bd9Sstevel@tonic-gate 			scf_propertygroup_t *pg;
56687c478bd9Sstevel@tonic-gate 
56697c478bd9Sstevel@tonic-gate 			pg = safe_scf_pg_create(h);
56707c478bd9Sstevel@tonic-gate 
56717c478bd9Sstevel@tonic-gate 			sz = strlcpy(fmri, st->st_subgraph, max_scf_fmri_size);
56727c478bd9Sstevel@tonic-gate 			assert(sz < max_scf_fmri_size);
56737c478bd9Sstevel@tonic-gate 
56747c478bd9Sstevel@tonic-gate 			r = libscf_inst_get_or_add_pg(inst, SCF_PG_OPTIONS_OVR,
56757c478bd9Sstevel@tonic-gate 			    SCF_PG_OPTIONS_OVR_TYPE, SCF_PG_OPTIONS_OVR_FLAGS,
56767c478bd9Sstevel@tonic-gate 			    pg);
56777c478bd9Sstevel@tonic-gate 			switch (r) {
56787c478bd9Sstevel@tonic-gate 			case 0:
56797c478bd9Sstevel@tonic-gate 				break;
56807c478bd9Sstevel@tonic-gate 
56817c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
56827c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
56837c478bd9Sstevel@tonic-gate 				goto get_self;
56847c478bd9Sstevel@tonic-gate 
56857c478bd9Sstevel@tonic-gate 			case EPERM:
56867c478bd9Sstevel@tonic-gate 			case EACCES:
56877c478bd9Sstevel@tonic-gate 			case EROFS:
56887c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Could not set %s/%s: "
56897c478bd9Sstevel@tonic-gate 				    "%s.\n", SCF_PG_OPTIONS_OVR,
56907c478bd9Sstevel@tonic-gate 				    SCF_PROPERTY_MILESTONE, strerror(r));
56917c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
56927c478bd9Sstevel@tonic-gate 
56937c478bd9Sstevel@tonic-gate 			case ECANCELED:
56947c478bd9Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
56957c478bd9Sstevel@tonic-gate 				    max_scf_fmri_size);
56967c478bd9Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
56977c478bd9Sstevel@tonic-gate 				break;
56987c478bd9Sstevel@tonic-gate 
56997c478bd9Sstevel@tonic-gate 			default:
57007c478bd9Sstevel@tonic-gate 				bad_error("libscf_inst_get_or_add_pg", r);
57017c478bd9Sstevel@tonic-gate 			}
57027c478bd9Sstevel@tonic-gate 
57037c478bd9Sstevel@tonic-gate 			r = libscf_clear_runlevel(pg, fmri);
57047c478bd9Sstevel@tonic-gate 			switch (r) {
57057c478bd9Sstevel@tonic-gate 			case 0:
57067c478bd9Sstevel@tonic-gate 				break;
57077c478bd9Sstevel@tonic-gate 
57087c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
57097c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
57107c478bd9Sstevel@tonic-gate 				goto get_self;
57117c478bd9Sstevel@tonic-gate 
57127c478bd9Sstevel@tonic-gate 			case EPERM:
57137c478bd9Sstevel@tonic-gate 			case EACCES:
57147c478bd9Sstevel@tonic-gate 			case EROFS:
57157c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Could not set %s/%s: "
57167c478bd9Sstevel@tonic-gate 				    "%s.\n", SCF_PG_OPTIONS_OVR,
57177c478bd9Sstevel@tonic-gate 				    SCF_PROPERTY_MILESTONE, strerror(r));
57187c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
57197c478bd9Sstevel@tonic-gate 
57207c478bd9Sstevel@tonic-gate 			case ECANCELED:
57217c478bd9Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
57227c478bd9Sstevel@tonic-gate 				    max_scf_fmri_size);
57237c478bd9Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
57247c478bd9Sstevel@tonic-gate 				break;
57257c478bd9Sstevel@tonic-gate 
57267c478bd9Sstevel@tonic-gate 			default:
57277c478bd9Sstevel@tonic-gate 				bad_error("libscf_clear_runlevel", r);
57287c478bd9Sstevel@tonic-gate 			}
57297c478bd9Sstevel@tonic-gate 
57307c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
57317c478bd9Sstevel@tonic-gate 		} else {
57327c478bd9Sstevel@tonic-gate 			scf_property_t *prop;
57337c478bd9Sstevel@tonic-gate 			scf_value_t *val;
57347c478bd9Sstevel@tonic-gate 
57357c478bd9Sstevel@tonic-gate 			prop = safe_scf_property_create(h);
57367c478bd9Sstevel@tonic-gate 			val = safe_scf_value_create(h);
57377c478bd9Sstevel@tonic-gate 
57387c478bd9Sstevel@tonic-gate 			r = libscf_get_milestone(inst, prop, val, fmri,
57397c478bd9Sstevel@tonic-gate 			    max_scf_fmri_size);
57407c478bd9Sstevel@tonic-gate 			switch (r) {
57417c478bd9Sstevel@tonic-gate 			case 0:
57427c478bd9Sstevel@tonic-gate 				break;
57437c478bd9Sstevel@tonic-gate 
57447c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
57457c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
57467c478bd9Sstevel@tonic-gate 				goto get_self;
57477c478bd9Sstevel@tonic-gate 
57487c478bd9Sstevel@tonic-gate 			case EINVAL:
57497c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Milestone property is "
57507c478bd9Sstevel@tonic-gate 				    "misconfigured.  Defaulting to \"all\".\n");
57517c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
57527c478bd9Sstevel@tonic-gate 
57537c478bd9Sstevel@tonic-gate 			case ECANCELED:
57547c478bd9Sstevel@tonic-gate 			case ENOENT:
57557c478bd9Sstevel@tonic-gate 				fmri[0] = '\0';
57567c478bd9Sstevel@tonic-gate 				break;
57577c478bd9Sstevel@tonic-gate 
57587c478bd9Sstevel@tonic-gate 			default:
57597c478bd9Sstevel@tonic-gate 				bad_error("libscf_get_milestone", r);
57607c478bd9Sstevel@tonic-gate 			}
57617c478bd9Sstevel@tonic-gate 
57627c478bd9Sstevel@tonic-gate 			scf_value_destroy(val);
57637c478bd9Sstevel@tonic-gate 			scf_property_destroy(prop);
57647c478bd9Sstevel@tonic-gate 		}
57657c478bd9Sstevel@tonic-gate 	}
57667c478bd9Sstevel@tonic-gate 
57677c478bd9Sstevel@tonic-gate 	if (fmri[0] == '\0' || strcmp(fmri, "all") == 0)
57687c478bd9Sstevel@tonic-gate 		goto out;
57697c478bd9Sstevel@tonic-gate 
57707c478bd9Sstevel@tonic-gate 	if (strcmp(fmri, "none") != 0) {
57717c478bd9Sstevel@tonic-gate retry:
57727c478bd9Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
57737c478bd9Sstevel@tonic-gate 		    NULL, SCF_DECODE_FMRI_EXACT) != 0) {
57747c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
57757c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
57767c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING,
57777c478bd9Sstevel@tonic-gate 				    "Requested milestone \"%s\" is invalid.  "
57787c478bd9Sstevel@tonic-gate 				    "Reverting to \"all\".\n", fmri);
57797c478bd9Sstevel@tonic-gate 				goto out;
57807c478bd9Sstevel@tonic-gate 
57817c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
57827c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Requested milestone "
57837c478bd9Sstevel@tonic-gate 				    "\"%s\" does not specify an instance.  "
57847c478bd9Sstevel@tonic-gate 				    "Reverting to \"all\".\n", fmri);
57857c478bd9Sstevel@tonic-gate 				goto out;
57867c478bd9Sstevel@tonic-gate 
57877c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
57887c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
57897c478bd9Sstevel@tonic-gate 				goto retry;
57907c478bd9Sstevel@tonic-gate 
57917c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
57927c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Requested milestone "
57937c478bd9Sstevel@tonic-gate 				    "\"%s\" not in repository.  Reverting to "
57947c478bd9Sstevel@tonic-gate 				    "\"all\".\n", fmri);
57957c478bd9Sstevel@tonic-gate 				goto out;
57967c478bd9Sstevel@tonic-gate 
57977c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
57987c478bd9Sstevel@tonic-gate 			default:
57997c478bd9Sstevel@tonic-gate 				bad_error("scf_handle_decode_fmri",
58007c478bd9Sstevel@tonic-gate 				    scf_error());
58017c478bd9Sstevel@tonic-gate 			}
58027c478bd9Sstevel@tonic-gate 		}
58037c478bd9Sstevel@tonic-gate 
58047c478bd9Sstevel@tonic-gate 		r = fmri_canonify(fmri, &cfmri, B_FALSE);
58057c478bd9Sstevel@tonic-gate 		assert(r == 0);
58067c478bd9Sstevel@tonic-gate 
58077c478bd9Sstevel@tonic-gate 		r = dgraph_add_instance(cfmri, inst, B_TRUE);
58087c478bd9Sstevel@tonic-gate 		startd_free(cfmri, max_scf_fmri_size);
58097c478bd9Sstevel@tonic-gate 		switch (r) {
58107c478bd9Sstevel@tonic-gate 		case 0:
58117c478bd9Sstevel@tonic-gate 			break;
58127c478bd9Sstevel@tonic-gate 
58137c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
58147c478bd9Sstevel@tonic-gate 			goto retry;
58157c478bd9Sstevel@tonic-gate 
58167c478bd9Sstevel@tonic-gate 		case EINVAL:
58177c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
58187c478bd9Sstevel@tonic-gate 			    "Requested milestone \"%s\" is invalid.  "
58197c478bd9Sstevel@tonic-gate 			    "Reverting to \"all\".\n", fmri);
58207c478bd9Sstevel@tonic-gate 			goto out;
58217c478bd9Sstevel@tonic-gate 
58227c478bd9Sstevel@tonic-gate 		case ECANCELED:
58237c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
58247c478bd9Sstevel@tonic-gate 			    "Requested milestone \"%s\" not "
58257c478bd9Sstevel@tonic-gate 			    "in repository.  Reverting to \"all\".\n",
58267c478bd9Sstevel@tonic-gate 			    fmri);
58277c478bd9Sstevel@tonic-gate 			goto out;
58287c478bd9Sstevel@tonic-gate 
58297c478bd9Sstevel@tonic-gate 		case EEXIST:
58307c478bd9Sstevel@tonic-gate 		default:
58317c478bd9Sstevel@tonic-gate 			bad_error("dgraph_add_instance", r);
58327c478bd9Sstevel@tonic-gate 		}
58337c478bd9Sstevel@tonic-gate 	}
58347c478bd9Sstevel@tonic-gate 
58357c478bd9Sstevel@tonic-gate 	log_console(LOG_INFO, "Booting to milestone \"%s\".\n", fmri);
58367c478bd9Sstevel@tonic-gate 
58377c478bd9Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_FALSE);
58387c478bd9Sstevel@tonic-gate 	switch (r) {
58397c478bd9Sstevel@tonic-gate 	case 0:
58407c478bd9Sstevel@tonic-gate 	case ECONNRESET:
58417c478bd9Sstevel@tonic-gate 	case EALREADY:
58427c478bd9Sstevel@tonic-gate 		break;
58437c478bd9Sstevel@tonic-gate 
58447c478bd9Sstevel@tonic-gate 	case EINVAL:
58457c478bd9Sstevel@tonic-gate 	case ENOENT:
58467c478bd9Sstevel@tonic-gate 	default:
58477c478bd9Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
58487c478bd9Sstevel@tonic-gate 	}
58497c478bd9Sstevel@tonic-gate 
58507c478bd9Sstevel@tonic-gate out:
58517c478bd9Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
58527c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
58537c478bd9Sstevel@tonic-gate }
58547c478bd9Sstevel@tonic-gate 
58557c478bd9Sstevel@tonic-gate void
58567c478bd9Sstevel@tonic-gate set_restart_milestone(scf_handle_t *h)
58577c478bd9Sstevel@tonic-gate {
58587c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
58597c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
58607c478bd9Sstevel@tonic-gate 	scf_value_t *val;
58617c478bd9Sstevel@tonic-gate 	char *fmri;
58627c478bd9Sstevel@tonic-gate 	int r;
58637c478bd9Sstevel@tonic-gate 
58647c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
58657c478bd9Sstevel@tonic-gate 
58667c478bd9Sstevel@tonic-gate get_self:
58677c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL,
58687c478bd9Sstevel@tonic-gate 	    inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
58697c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
58707c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
58717c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
58727c478bd9Sstevel@tonic-gate 			goto get_self;
58737c478bd9Sstevel@tonic-gate 
58747c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
58757c478bd9Sstevel@tonic-gate 			break;
58767c478bd9Sstevel@tonic-gate 
58777c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
58787c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
58797c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
58807c478bd9Sstevel@tonic-gate 		default:
58817c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
58827c478bd9Sstevel@tonic-gate 		}
58837c478bd9Sstevel@tonic-gate 
58847c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
58857c478bd9Sstevel@tonic-gate 		return;
58867c478bd9Sstevel@tonic-gate 	}
58877c478bd9Sstevel@tonic-gate 
58887c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
58897c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
58907c478bd9Sstevel@tonic-gate 	fmri = startd_alloc(max_scf_fmri_size);
58917c478bd9Sstevel@tonic-gate 
58927c478bd9Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size);
58937c478bd9Sstevel@tonic-gate 	switch (r) {
58947c478bd9Sstevel@tonic-gate 	case 0:
58957c478bd9Sstevel@tonic-gate 		break;
58967c478bd9Sstevel@tonic-gate 
58977c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
58987c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
58997c478bd9Sstevel@tonic-gate 		goto get_self;
59007c478bd9Sstevel@tonic-gate 
59017c478bd9Sstevel@tonic-gate 	case ECANCELED:
59027c478bd9Sstevel@tonic-gate 	case ENOENT:
59037c478bd9Sstevel@tonic-gate 	case EINVAL:
59047c478bd9Sstevel@tonic-gate 		goto out;
59057c478bd9Sstevel@tonic-gate 
59067c478bd9Sstevel@tonic-gate 	default:
59077c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
59087c478bd9Sstevel@tonic-gate 	}
59097c478bd9Sstevel@tonic-gate 
59107c478bd9Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_TRUE);
59117c478bd9Sstevel@tonic-gate 	switch (r) {
59127c478bd9Sstevel@tonic-gate 	case 0:
59137c478bd9Sstevel@tonic-gate 	case ECONNRESET:
59147c478bd9Sstevel@tonic-gate 	case EALREADY:
59157c478bd9Sstevel@tonic-gate 	case EINVAL:
59167c478bd9Sstevel@tonic-gate 	case ENOENT:
59177c478bd9Sstevel@tonic-gate 		break;
59187c478bd9Sstevel@tonic-gate 
59197c478bd9Sstevel@tonic-gate 	default:
59207c478bd9Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
59217c478bd9Sstevel@tonic-gate 	}
59227c478bd9Sstevel@tonic-gate 
59237c478bd9Sstevel@tonic-gate out:
59247c478bd9Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
59257c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
59267c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
59277c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
59287c478bd9Sstevel@tonic-gate }
59297c478bd9Sstevel@tonic-gate 
59307c478bd9Sstevel@tonic-gate /*
59317c478bd9Sstevel@tonic-gate  * void *graph_thread(void *)
59327c478bd9Sstevel@tonic-gate  *
59337c478bd9Sstevel@tonic-gate  * Graph management thread.
59347c478bd9Sstevel@tonic-gate  */
59357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
59367c478bd9Sstevel@tonic-gate void *
59377c478bd9Sstevel@tonic-gate graph_thread(void *arg)
59387c478bd9Sstevel@tonic-gate {
59397c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
59407c478bd9Sstevel@tonic-gate 	int err;
59417c478bd9Sstevel@tonic-gate 
59427c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
59437c478bd9Sstevel@tonic-gate 
59447c478bd9Sstevel@tonic-gate 	if (st->st_initial)
59457c478bd9Sstevel@tonic-gate 		set_initial_milestone(h);
59467c478bd9Sstevel@tonic-gate 
59477c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
59487c478bd9Sstevel@tonic-gate 	initial_milestone_set = B_TRUE;
59497c478bd9Sstevel@tonic-gate 	err = pthread_cond_broadcast(&initial_milestone_cv);
59507c478bd9Sstevel@tonic-gate 	assert(err == 0);
59517c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
59527c478bd9Sstevel@tonic-gate 
59537c478bd9Sstevel@tonic-gate 	libscf_populate_graph(h);
59547c478bd9Sstevel@tonic-gate 
59557c478bd9Sstevel@tonic-gate 	if (!st->st_initial)
59567c478bd9Sstevel@tonic-gate 		set_restart_milestone(h);
59577c478bd9Sstevel@tonic-gate 
59587c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&st->st_load_lock);
59597c478bd9Sstevel@tonic-gate 	st->st_load_complete = 1;
59607c478bd9Sstevel@tonic-gate 	(void) pthread_cond_broadcast(&st->st_load_cv);
59617c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&st->st_load_lock);
59627c478bd9Sstevel@tonic-gate 
59637c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
59647c478bd9Sstevel@tonic-gate 	/*
59657c478bd9Sstevel@tonic-gate 	 * Now that we've set st_load_complete we need to check can_come_up()
59667c478bd9Sstevel@tonic-gate 	 * since if we booted to a milestone, then there won't be any more
59677c478bd9Sstevel@tonic-gate 	 * state updates.
59687c478bd9Sstevel@tonic-gate 	 */
59697c478bd9Sstevel@tonic-gate 	if (!go_single_user_mode && !go_to_level1 &&
59707c478bd9Sstevel@tonic-gate 	    halting == -1) {
597173b709eaSrm88369 		if (!sulogin_thread_running && !can_come_up()) {
59727c478bd9Sstevel@tonic-gate 			(void) startd_thread_create(sulogin_thread, NULL);
59737c478bd9Sstevel@tonic-gate 			sulogin_thread_running = B_TRUE;
59747c478bd9Sstevel@tonic-gate 		}
59757c478bd9Sstevel@tonic-gate 	}
59767c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
59777c478bd9Sstevel@tonic-gate 
59787c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&gu->gu_freeze_lock);
59797c478bd9Sstevel@tonic-gate 
59807c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
59817c478bd9Sstevel@tonic-gate 	while (1) {
59827c478bd9Sstevel@tonic-gate 		(void) pthread_cond_wait(&gu->gu_freeze_cv,
59837c478bd9Sstevel@tonic-gate 		    &gu->gu_freeze_lock);
59847c478bd9Sstevel@tonic-gate 	}
59857c478bd9Sstevel@tonic-gate 
59867c478bd9Sstevel@tonic-gate 	/*
59877c478bd9Sstevel@tonic-gate 	 * Unreachable for now -- there's currently no graceful cleanup
59887c478bd9Sstevel@tonic-gate 	 * called on exit().
59897c478bd9Sstevel@tonic-gate 	 */
59907c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&gu->gu_freeze_lock);
59917c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
59927c478bd9Sstevel@tonic-gate 
59937c478bd9Sstevel@tonic-gate 	return (NULL);
59947c478bd9Sstevel@tonic-gate }
59957c478bd9Sstevel@tonic-gate 
59967c478bd9Sstevel@tonic-gate 
59977c478bd9Sstevel@tonic-gate /*
59987c478bd9Sstevel@tonic-gate  * int next_action()
59997c478bd9Sstevel@tonic-gate  *   Given an array of timestamps 'a' with 'num' elements, find the
60007c478bd9Sstevel@tonic-gate  *   lowest non-zero timestamp and return its index. If there are no
60017c478bd9Sstevel@tonic-gate  *   non-zero elements, return -1.
60027c478bd9Sstevel@tonic-gate  */
60037c478bd9Sstevel@tonic-gate static int
60047c478bd9Sstevel@tonic-gate next_action(hrtime_t *a, int num)
60057c478bd9Sstevel@tonic-gate {
60067c478bd9Sstevel@tonic-gate 	hrtime_t t = 0;
60077c478bd9Sstevel@tonic-gate 	int i = 0, smallest = -1;
60087c478bd9Sstevel@tonic-gate 
60097c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
60107c478bd9Sstevel@tonic-gate 		if (t == 0) {
60117c478bd9Sstevel@tonic-gate 			t = a[i];
60127c478bd9Sstevel@tonic-gate 			smallest = i;
60137c478bd9Sstevel@tonic-gate 		} else if (a[i] != 0 && a[i] < t) {
60147c478bd9Sstevel@tonic-gate 			t = a[i];
60157c478bd9Sstevel@tonic-gate 			smallest = i;
60167c478bd9Sstevel@tonic-gate 		}
60177c478bd9Sstevel@tonic-gate 	}
60187c478bd9Sstevel@tonic-gate 
60197c478bd9Sstevel@tonic-gate 	if (t == 0)
60207c478bd9Sstevel@tonic-gate 		return (-1);
60217c478bd9Sstevel@tonic-gate 	else
60227c478bd9Sstevel@tonic-gate 		return (smallest);
60237c478bd9Sstevel@tonic-gate }
60247c478bd9Sstevel@tonic-gate 
60257c478bd9Sstevel@tonic-gate /*
60267c478bd9Sstevel@tonic-gate  * void process_actions()
60277c478bd9Sstevel@tonic-gate  *   Process actions requested by the administrator. Possibilities include:
60287c478bd9Sstevel@tonic-gate  *   refresh, restart, maintenance mode off, maintenance mode on,
60297c478bd9Sstevel@tonic-gate  *   maintenance mode immediate, and degraded.
60307c478bd9Sstevel@tonic-gate  *
60317c478bd9Sstevel@tonic-gate  *   The set of pending actions is represented in the repository as a
60327c478bd9Sstevel@tonic-gate  *   per-instance property group, with each action being a single property
60337c478bd9Sstevel@tonic-gate  *   in that group.  This property group is converted to an array, with each
60347c478bd9Sstevel@tonic-gate  *   action type having an array slot.  The actions in the array at the
60357c478bd9Sstevel@tonic-gate  *   time process_actions() is called are acted on in the order of the
60367c478bd9Sstevel@tonic-gate  *   timestamp (which is the value stored in the slot).  A value of zero
60377c478bd9Sstevel@tonic-gate  *   indicates that there is no pending action of the type associated with
60387c478bd9Sstevel@tonic-gate  *   a particular slot.
60397c478bd9Sstevel@tonic-gate  *
60407c478bd9Sstevel@tonic-gate  *   Sending an action event multiple times before the restarter has a
60417c478bd9Sstevel@tonic-gate  *   chance to process that action will force it to be run at the last
60427c478bd9Sstevel@tonic-gate  *   timestamp where it appears in the ordering.
60437c478bd9Sstevel@tonic-gate  *
60447c478bd9Sstevel@tonic-gate  *   Turning maintenance mode on trumps all other actions.
60457c478bd9Sstevel@tonic-gate  *
60467c478bd9Sstevel@tonic-gate  *   Returns 0 or ECONNABORTED.
60477c478bd9Sstevel@tonic-gate  */
60487c478bd9Sstevel@tonic-gate static int
60497c478bd9Sstevel@tonic-gate process_actions(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst)
60507c478bd9Sstevel@tonic-gate {
60517c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
60527c478bd9Sstevel@tonic-gate 	scf_value_t *val = NULL;
60537c478bd9Sstevel@tonic-gate 	scf_type_t type;
60547c478bd9Sstevel@tonic-gate 	graph_vertex_t *vertex;
60557c478bd9Sstevel@tonic-gate 	admin_action_t a;
60567c478bd9Sstevel@tonic-gate 	int i, ret = 0, r;
60577c478bd9Sstevel@tonic-gate 	hrtime_t action_ts[NACTIONS];
60587c478bd9Sstevel@tonic-gate 	char *inst_name;
60597c478bd9Sstevel@tonic-gate 
60607c478bd9Sstevel@tonic-gate 	r = libscf_instance_get_fmri(inst, &inst_name);
60617c478bd9Sstevel@tonic-gate 	switch (r) {
60627c478bd9Sstevel@tonic-gate 	case 0:
60637c478bd9Sstevel@tonic-gate 		break;
60647c478bd9Sstevel@tonic-gate 
60657c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
60667c478bd9Sstevel@tonic-gate 		return (ECONNABORTED);
60677c478bd9Sstevel@tonic-gate 
60687c478bd9Sstevel@tonic-gate 	case ECANCELED:
60697c478bd9Sstevel@tonic-gate 		return (0);
60707c478bd9Sstevel@tonic-gate 
60717c478bd9Sstevel@tonic-gate 	default:
60727c478bd9Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", r);
60737c478bd9Sstevel@tonic-gate 	}
60747c478bd9Sstevel@tonic-gate 
60757c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
60767c478bd9Sstevel@tonic-gate 
60777c478bd9Sstevel@tonic-gate 	vertex = vertex_get_by_name(inst_name);
60787c478bd9Sstevel@tonic-gate 	if (vertex == NULL) {
60797c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
60807c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "%s: Can't find graph vertex. "
60817c478bd9Sstevel@tonic-gate 		    "The instance must have been removed.\n", inst_name);
6082a2c43da6SSeth Goldberg 		startd_free(inst_name, max_scf_fmri_size);
60837c478bd9Sstevel@tonic-gate 		return (0);
60847c478bd9Sstevel@tonic-gate 	}
60857c478bd9Sstevel@tonic-gate 
60867c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
60877c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
60887c478bd9Sstevel@tonic-gate 
60897c478bd9Sstevel@tonic-gate 	for (i = 0; i < NACTIONS; i++) {
60907c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(pg, admin_actions[i], prop) != 0) {
60917c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
60927c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
60937c478bd9Sstevel@tonic-gate 			default:
60947c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
60957c478bd9Sstevel@tonic-gate 				goto out;
60967c478bd9Sstevel@tonic-gate 
60977c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
60987c478bd9Sstevel@tonic-gate 				goto out;
60997c478bd9Sstevel@tonic-gate 
61007c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
61017c478bd9Sstevel@tonic-gate 				action_ts[i] = 0;
61027c478bd9Sstevel@tonic-gate 				continue;
61037c478bd9Sstevel@tonic-gate 
61047c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
61057c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
61067c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
61077c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
61087c478bd9Sstevel@tonic-gate 			}
61097c478bd9Sstevel@tonic-gate 		}
61107c478bd9Sstevel@tonic-gate 
61117c478bd9Sstevel@tonic-gate 		if (scf_property_type(prop, &type) != 0) {
61127c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
61137c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
61147c478bd9Sstevel@tonic-gate 			default:
61157c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
61167c478bd9Sstevel@tonic-gate 				goto out;
61177c478bd9Sstevel@tonic-gate 
61187c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
61197c478bd9Sstevel@tonic-gate 				action_ts[i] = 0;
61207c478bd9Sstevel@tonic-gate 				continue;
61217c478bd9Sstevel@tonic-gate 
61227c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
61237c478bd9Sstevel@tonic-gate 				bad_error("scf_property_type", scf_error());
61247c478bd9Sstevel@tonic-gate 			}
61257c478bd9Sstevel@tonic-gate 		}
61267c478bd9Sstevel@tonic-gate 
61277c478bd9Sstevel@tonic-gate 		if (type != SCF_TYPE_INTEGER) {
61287c478bd9Sstevel@tonic-gate 			action_ts[i] = 0;
61297c478bd9Sstevel@tonic-gate 			continue;
61307c478bd9Sstevel@tonic-gate 		}
61317c478bd9Sstevel@tonic-gate 
61327c478bd9Sstevel@tonic-gate 		if (scf_property_get_value(prop, val) != 0) {
61337c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
61347c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
61357c478bd9Sstevel@tonic-gate 			default:
61367c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
61377c478bd9Sstevel@tonic-gate 				goto out;
61387c478bd9Sstevel@tonic-gate 
61397c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
61407c478bd9Sstevel@tonic-gate 				goto out;
61417c478bd9Sstevel@tonic-gate 
61427c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
61437c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
61447c478bd9Sstevel@tonic-gate 				action_ts[i] = 0;
61457c478bd9Sstevel@tonic-gate 				continue;
61467c478bd9Sstevel@tonic-gate 
61477c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
61483eae19d9Swesolows 			case SCF_ERROR_PERMISSION_DENIED:
61497c478bd9Sstevel@tonic-gate 				bad_error("scf_property_get_value",
61507c478bd9Sstevel@tonic-gate 				    scf_error());
61517c478bd9Sstevel@tonic-gate 			}
61527c478bd9Sstevel@tonic-gate 		}
61537c478bd9Sstevel@tonic-gate 
61547c478bd9Sstevel@tonic-gate 		r = scf_value_get_integer(val, &action_ts[i]);
61557c478bd9Sstevel@tonic-gate 		assert(r == 0);
61567c478bd9Sstevel@tonic-gate 	}
61577c478bd9Sstevel@tonic-gate 
61587c478bd9Sstevel@tonic-gate 	a = ADMIN_EVENT_MAINT_ON_IMMEDIATE;
61597c478bd9Sstevel@tonic-gate 	if (action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ||
61607c478bd9Sstevel@tonic-gate 	    action_ts[ADMIN_EVENT_MAINT_ON]) {
61617c478bd9Sstevel@tonic-gate 		a = action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ?
61627c478bd9Sstevel@tonic-gate 		    ADMIN_EVENT_MAINT_ON_IMMEDIATE : ADMIN_EVENT_MAINT_ON;
61637c478bd9Sstevel@tonic-gate 
61647c478bd9Sstevel@tonic-gate 		vertex_send_event(vertex, admin_events[a]);
61657c478bd9Sstevel@tonic-gate 		r = libscf_unset_action(h, pg, a, action_ts[a]);
61667c478bd9Sstevel@tonic-gate 		switch (r) {
61677c478bd9Sstevel@tonic-gate 		case 0:
61687c478bd9Sstevel@tonic-gate 		case EACCES:
61697c478bd9Sstevel@tonic-gate 			break;
61707c478bd9Sstevel@tonic-gate 
61717c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
61727c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
61737c478bd9Sstevel@tonic-gate 			goto out;
61747c478bd9Sstevel@tonic-gate 
61757c478bd9Sstevel@tonic-gate 		case EPERM:
61767c478bd9Sstevel@tonic-gate 			uu_die("Insufficient privilege.\n");
61777c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
61787c478bd9Sstevel@tonic-gate 
61797c478bd9Sstevel@tonic-gate 		default:
61807c478bd9Sstevel@tonic-gate 			bad_error("libscf_unset_action", r);
61817c478bd9Sstevel@tonic-gate 		}
61827c478bd9Sstevel@tonic-gate 	}
61837c478bd9Sstevel@tonic-gate 
61847c478bd9Sstevel@tonic-gate 	while ((a = next_action(action_ts, NACTIONS)) != -1) {
61857c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
61867c478bd9Sstevel@tonic-gate 		    "Graph: processing %s action for %s.\n", admin_actions[a],
61877c478bd9Sstevel@tonic-gate 		    inst_name);
61887c478bd9Sstevel@tonic-gate 
61897c478bd9Sstevel@tonic-gate 		if (a == ADMIN_EVENT_REFRESH) {
61907c478bd9Sstevel@tonic-gate 			r = dgraph_refresh_instance(vertex, inst);
61917c478bd9Sstevel@tonic-gate 			switch (r) {
61927c478bd9Sstevel@tonic-gate 			case 0:
61937c478bd9Sstevel@tonic-gate 			case ECANCELED:
61947c478bd9Sstevel@tonic-gate 			case EINVAL:
61957c478bd9Sstevel@tonic-gate 			case -1:
61967c478bd9Sstevel@tonic-gate 				break;
61977c478bd9Sstevel@tonic-gate 
61987c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
61997c478bd9Sstevel@tonic-gate 				/* pg & inst are reset now, so just return. */
62007c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
62017c478bd9Sstevel@tonic-gate 				goto out;
62027c478bd9Sstevel@tonic-gate 
62037c478bd9Sstevel@tonic-gate 			default:
62047c478bd9Sstevel@tonic-gate 				bad_error("dgraph_refresh_instance", r);
62057c478bd9Sstevel@tonic-gate 			}
62067c478bd9Sstevel@tonic-gate 		}
62077c478bd9Sstevel@tonic-gate 
62087c478bd9Sstevel@tonic-gate 		vertex_send_event(vertex, admin_events[a]);
62097c478bd9Sstevel@tonic-gate 
62107c478bd9Sstevel@tonic-gate 		r = libscf_unset_action(h, pg, a, action_ts[a]);
62117c478bd9Sstevel@tonic-gate 		switch (r) {
62127c478bd9Sstevel@tonic-gate 		case 0:
62137c478bd9Sstevel@tonic-gate 		case EACCES:
62147c478bd9Sstevel@tonic-gate 			break;
62157c478bd9Sstevel@tonic-gate 
62167c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
62177c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
62187c478bd9Sstevel@tonic-gate 			goto out;
62197c478bd9Sstevel@tonic-gate 
62207c478bd9Sstevel@tonic-gate 		case EPERM:
62217c478bd9Sstevel@tonic-gate 			uu_die("Insufficient privilege.\n");
62227c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
62237c478bd9Sstevel@tonic-gate 
62247c478bd9Sstevel@tonic-gate 		default:
62257c478bd9Sstevel@tonic-gate 			bad_error("libscf_unset_action", r);
62267c478bd9Sstevel@tonic-gate 		}
62277c478bd9Sstevel@tonic-gate 
62287c478bd9Sstevel@tonic-gate 		action_ts[a] = 0;
62297c478bd9Sstevel@tonic-gate 	}
62307c478bd9Sstevel@tonic-gate 
62317c478bd9Sstevel@tonic-gate out:
62327c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
62337c478bd9Sstevel@tonic-gate 
62347c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
62357c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
62367c478bd9Sstevel@tonic-gate 	startd_free(inst_name, max_scf_fmri_size);
62377c478bd9Sstevel@tonic-gate 	return (ret);
62387c478bd9Sstevel@tonic-gate }
62397c478bd9Sstevel@tonic-gate 
62407c478bd9Sstevel@tonic-gate /*
62417c478bd9Sstevel@tonic-gate  * inst and pg_name are scratch space, and are unset on entry.
62427c478bd9Sstevel@tonic-gate  * Returns
62437c478bd9Sstevel@tonic-gate  *   0 - success
62447c478bd9Sstevel@tonic-gate  *   ECONNRESET - success, but repository handle rebound
62457c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
62467c478bd9Sstevel@tonic-gate  */
62477c478bd9Sstevel@tonic-gate static int
62487c478bd9Sstevel@tonic-gate process_pg_event(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst,
62497c478bd9Sstevel@tonic-gate     char *pg_name)
62507c478bd9Sstevel@tonic-gate {
62517c478bd9Sstevel@tonic-gate 	int r;
62527c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
62537c478bd9Sstevel@tonic-gate 	scf_value_t *val;
62547c478bd9Sstevel@tonic-gate 	char *fmri;
62557c478bd9Sstevel@tonic-gate 	boolean_t rebound = B_FALSE, rebind_inst = B_FALSE;
62567c478bd9Sstevel@tonic-gate 
62577c478bd9Sstevel@tonic-gate 	if (scf_pg_get_name(pg, pg_name, max_scf_value_size) < 0) {
62587c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
62597c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
62607c478bd9Sstevel@tonic-gate 		default:
62617c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
62627c478bd9Sstevel@tonic-gate 
62637c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
62647c478bd9Sstevel@tonic-gate 			return (0);
62657c478bd9Sstevel@tonic-gate 
62667c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
62677c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_name", scf_error());
62687c478bd9Sstevel@tonic-gate 		}
62697c478bd9Sstevel@tonic-gate 	}
62707c478bd9Sstevel@tonic-gate 
62717c478bd9Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_GENERAL) == 0 ||
62727c478bd9Sstevel@tonic-gate 	    strcmp(pg_name, SCF_PG_GENERAL_OVR) == 0) {
62737c478bd9Sstevel@tonic-gate 		r = dgraph_update_general(pg);
62747c478bd9Sstevel@tonic-gate 		switch (r) {
62757c478bd9Sstevel@tonic-gate 		case 0:
62767c478bd9Sstevel@tonic-gate 		case ENOTSUP:
62777c478bd9Sstevel@tonic-gate 		case ECANCELED:
62787c478bd9Sstevel@tonic-gate 			return (0);
62797c478bd9Sstevel@tonic-gate 
62807c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
62817c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
62827c478bd9Sstevel@tonic-gate 
62837c478bd9Sstevel@tonic-gate 		case -1:
62847c478bd9Sstevel@tonic-gate 			/* Error should have been logged. */
62857c478bd9Sstevel@tonic-gate 			return (0);
62867c478bd9Sstevel@tonic-gate 
62877c478bd9Sstevel@tonic-gate 		default:
62887c478bd9Sstevel@tonic-gate 			bad_error("dgraph_update_general", r);
62897c478bd9Sstevel@tonic-gate 		}
62907c478bd9Sstevel@tonic-gate 	} else if (strcmp(pg_name, SCF_PG_RESTARTER_ACTIONS) == 0) {
62917c478bd9Sstevel@tonic-gate 		if (scf_pg_get_parent_instance(pg, inst) != 0) {
62927c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
62937c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
62947c478bd9Sstevel@tonic-gate 				return (ECONNABORTED);
62957c478bd9Sstevel@tonic-gate 
62967c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
62977c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
62987c478bd9Sstevel@tonic-gate 				/* Ignore commands on services. */
62997c478bd9Sstevel@tonic-gate 				return (0);
63007c478bd9Sstevel@tonic-gate 
63017c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
63027c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
63037c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
63047c478bd9Sstevel@tonic-gate 			default:
63057c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_get_parent_instance",
63067c478bd9Sstevel@tonic-gate 				    scf_error());
63077c478bd9Sstevel@tonic-gate 			}
63087c478bd9Sstevel@tonic-gate 		}
63097c478bd9Sstevel@tonic-gate 
63107c478bd9Sstevel@tonic-gate 		return (process_actions(h, pg, inst));
63117c478bd9Sstevel@tonic-gate 	}
63127c478bd9Sstevel@tonic-gate 
63137c478bd9Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_OPTIONS) != 0 &&
63147c478bd9Sstevel@tonic-gate 	    strcmp(pg_name, SCF_PG_OPTIONS_OVR) != 0)
63157c478bd9Sstevel@tonic-gate 		return (0);
63167c478bd9Sstevel@tonic-gate 
63177c478bd9Sstevel@tonic-gate 	/*
63187c478bd9Sstevel@tonic-gate 	 * We only care about the options[_ovr] property groups of our own
63197c478bd9Sstevel@tonic-gate 	 * instance, so get the fmri and compare.  Plus, once we know it's
63207c478bd9Sstevel@tonic-gate 	 * correct, if the repository connection is broken we know exactly what
63217c478bd9Sstevel@tonic-gate 	 * property group we were operating on, and can look it up again.
63227c478bd9Sstevel@tonic-gate 	 */
63237c478bd9Sstevel@tonic-gate 	if (scf_pg_get_parent_instance(pg, inst) != 0) {
63247c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
63257c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
63267c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
63277c478bd9Sstevel@tonic-gate 
63287c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
63297c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
63307c478bd9Sstevel@tonic-gate 			return (0);
63317c478bd9Sstevel@tonic-gate 
63327c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
63337c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
63347c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
63357c478bd9Sstevel@tonic-gate 		default:
63367c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_parent_instance",
63377c478bd9Sstevel@tonic-gate 			    scf_error());
63387c478bd9Sstevel@tonic-gate 		}
63397c478bd9Sstevel@tonic-gate 	}
63407c478bd9Sstevel@tonic-gate 
63417c478bd9Sstevel@tonic-gate 	switch (r = libscf_instance_get_fmri(inst, &fmri)) {
63427c478bd9Sstevel@tonic-gate 	case 0:
63437c478bd9Sstevel@tonic-gate 		break;
63447c478bd9Sstevel@tonic-gate 
63457c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
63467c478bd9Sstevel@tonic-gate 		return (ECONNABORTED);
63477c478bd9Sstevel@tonic-gate 
63487c478bd9Sstevel@tonic-gate 	case ECANCELED:
63497c478bd9Sstevel@tonic-gate 		return (0);
63507c478bd9Sstevel@tonic-gate 
63517c478bd9Sstevel@tonic-gate 	default:
63527c478bd9Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", r);
63537c478bd9Sstevel@tonic-gate 	}
63547c478bd9Sstevel@tonic-gate 
63557c478bd9Sstevel@tonic-gate 	if (strcmp(fmri, SCF_SERVICE_STARTD) != 0) {
63567c478bd9Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
63577c478bd9Sstevel@tonic-gate 		return (0);
63587c478bd9Sstevel@tonic-gate 	}
63597c478bd9Sstevel@tonic-gate 
63607c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
63617c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
63627c478bd9Sstevel@tonic-gate 
63637c478bd9Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_OPTIONS_OVR) == 0) {
63647c478bd9Sstevel@tonic-gate 		/* See if we need to set the runlevel. */
63657c478bd9Sstevel@tonic-gate 		/* CONSTCOND */
63667c478bd9Sstevel@tonic-gate 		if (0) {
63677c478bd9Sstevel@tonic-gate rebind_pg:
63687c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
63697c478bd9Sstevel@tonic-gate 			rebound = B_TRUE;
63707c478bd9Sstevel@tonic-gate 
63717c478bd9Sstevel@tonic-gate 			r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst);
63727c478bd9Sstevel@tonic-gate 			switch (r) {
63737c478bd9Sstevel@tonic-gate 			case 0:
63747c478bd9Sstevel@tonic-gate 				break;
63757c478bd9Sstevel@tonic-gate 
63767c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
63777c478bd9Sstevel@tonic-gate 				goto rebind_pg;
63787c478bd9Sstevel@tonic-gate 
63797c478bd9Sstevel@tonic-gate 			case ENOENT:
63807c478bd9Sstevel@tonic-gate 				goto out;
63817c478bd9Sstevel@tonic-gate 
63827c478bd9Sstevel@tonic-gate 			case EINVAL:
63837c478bd9Sstevel@tonic-gate 			case ENOTSUP:
63847c478bd9Sstevel@tonic-gate 				bad_error("libscf_lookup_instance", r);
63857c478bd9Sstevel@tonic-gate 			}
63867c478bd9Sstevel@tonic-gate 
63877c478bd9Sstevel@tonic-gate 			if (scf_instance_get_pg(inst, pg_name, pg) != 0) {
63887c478bd9Sstevel@tonic-gate 				switch (scf_error()) {
63897c478bd9Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
63907c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_FOUND:
63917c478bd9Sstevel@tonic-gate 					goto out;
63927c478bd9Sstevel@tonic-gate 
63937c478bd9Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
63947c478bd9Sstevel@tonic-gate 					goto rebind_pg;
63957c478bd9Sstevel@tonic-gate 
63967c478bd9Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
63977c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_BOUND:
63987c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
63997c478bd9Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
64007c478bd9Sstevel@tonic-gate 				default:
64017c478bd9Sstevel@tonic-gate 					bad_error("scf_instance_get_pg",
64027c478bd9Sstevel@tonic-gate 					    scf_error());
64037c478bd9Sstevel@tonic-gate 				}
64047c478bd9Sstevel@tonic-gate 			}
64057c478bd9Sstevel@tonic-gate 		}
64067c478bd9Sstevel@tonic-gate 
64077c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(pg, "runlevel", prop) == 0) {
64087c478bd9Sstevel@tonic-gate 			r = dgraph_set_runlevel(pg, prop);
64097c478bd9Sstevel@tonic-gate 			switch (r) {
64107c478bd9Sstevel@tonic-gate 			case ECONNRESET:
64117c478bd9Sstevel@tonic-gate 				rebound = B_TRUE;
64127c478bd9Sstevel@tonic-gate 				rebind_inst = B_TRUE;
64137c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
64147c478bd9Sstevel@tonic-gate 
64157c478bd9Sstevel@tonic-gate 			case 0:
64167c478bd9Sstevel@tonic-gate 				break;
64177c478bd9Sstevel@tonic-gate 
64187c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
64197c478bd9Sstevel@tonic-gate 				goto rebind_pg;
64207c478bd9Sstevel@tonic-gate 
64217c478bd9Sstevel@tonic-gate 			case ECANCELED:
64227c478bd9Sstevel@tonic-gate 				goto out;
64237c478bd9Sstevel@tonic-gate 
64247c478bd9Sstevel@tonic-gate 			default:
64257c478bd9Sstevel@tonic-gate 				bad_error("dgraph_set_runlevel", r);
64267c478bd9Sstevel@tonic-gate 			}
64277c478bd9Sstevel@tonic-gate 		} else {
64287c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
64297c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
64307c478bd9Sstevel@tonic-gate 			default:
64317c478bd9Sstevel@tonic-gate 				goto rebind_pg;
64327c478bd9Sstevel@tonic-gate 
64337c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
64347c478bd9Sstevel@tonic-gate 				goto out;
64357c478bd9Sstevel@tonic-gate 
64367c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
64377c478bd9Sstevel@tonic-gate 				break;
64387c478bd9Sstevel@tonic-gate 
64397c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
64407c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
64417c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
64427c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
64437c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
64447c478bd9Sstevel@tonic-gate 			}
64457c478bd9Sstevel@tonic-gate 		}
64467c478bd9Sstevel@tonic-gate 	}
64477c478bd9Sstevel@tonic-gate 
64487c478bd9Sstevel@tonic-gate 	if (rebind_inst) {
64497c478bd9Sstevel@tonic-gate lookup_inst:
64507c478bd9Sstevel@tonic-gate 		r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst);
64517c478bd9Sstevel@tonic-gate 		switch (r) {
64527c478bd9Sstevel@tonic-gate 		case 0:
64537c478bd9Sstevel@tonic-gate 			break;
64547c478bd9Sstevel@tonic-gate 
64557c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
64567c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
64577c478bd9Sstevel@tonic-gate 			rebound = B_TRUE;
64587c478bd9Sstevel@tonic-gate 			goto lookup_inst;
64597c478bd9Sstevel@tonic-gate 
64607c478bd9Sstevel@tonic-gate 		case ENOENT:
64617c478bd9Sstevel@tonic-gate 			goto out;
64627c478bd9Sstevel@tonic-gate 
64637c478bd9Sstevel@tonic-gate 		case EINVAL:
64647c478bd9Sstevel@tonic-gate 		case ENOTSUP:
64657c478bd9Sstevel@tonic-gate 			bad_error("libscf_lookup_instance", r);
64667c478bd9Sstevel@tonic-gate 		}
64677c478bd9Sstevel@tonic-gate 	}
64687c478bd9Sstevel@tonic-gate 
64697c478bd9Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size);
64707c478bd9Sstevel@tonic-gate 	switch (r) {
64717c478bd9Sstevel@tonic-gate 	case 0:
64727c478bd9Sstevel@tonic-gate 		break;
64737c478bd9Sstevel@tonic-gate 
64747c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
64757c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
64767c478bd9Sstevel@tonic-gate 		rebound = B_TRUE;
64777c478bd9Sstevel@tonic-gate 		goto lookup_inst;
64787c478bd9Sstevel@tonic-gate 
64797c478bd9Sstevel@tonic-gate 	case EINVAL:
64807c478bd9Sstevel@tonic-gate 		log_error(LOG_NOTICE,
64817c478bd9Sstevel@tonic-gate 		    "%s/%s property of %s is misconfigured.\n", pg_name,
64827c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MILESTONE, SCF_SERVICE_STARTD);
64837c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
64847c478bd9Sstevel@tonic-gate 
64857c478bd9Sstevel@tonic-gate 	case ECANCELED:
64867c478bd9Sstevel@tonic-gate 	case ENOENT:
64877c478bd9Sstevel@tonic-gate 		(void) strcpy(fmri, "all");
64887c478bd9Sstevel@tonic-gate 		break;
64897c478bd9Sstevel@tonic-gate 
64907c478bd9Sstevel@tonic-gate 	default:
64917c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
64927c478bd9Sstevel@tonic-gate 	}
64937c478bd9Sstevel@tonic-gate 
64947c478bd9Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_FALSE);
64957c478bd9Sstevel@tonic-gate 	switch (r) {
64967c478bd9Sstevel@tonic-gate 	case 0:
64977c478bd9Sstevel@tonic-gate 	case ECONNRESET:
64987c478bd9Sstevel@tonic-gate 	case EALREADY:
64997c478bd9Sstevel@tonic-gate 		break;
65007c478bd9Sstevel@tonic-gate 
65017c478bd9Sstevel@tonic-gate 	case EINVAL:
65027c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "Milestone %s is invalid.\n", fmri);
65037c478bd9Sstevel@tonic-gate 		break;
65047c478bd9Sstevel@tonic-gate 
65057c478bd9Sstevel@tonic-gate 	case ENOENT:
65067c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "Milestone %s does not exist.\n", fmri);
65077c478bd9Sstevel@tonic-gate 		break;
65087c478bd9Sstevel@tonic-gate 
65097c478bd9Sstevel@tonic-gate 	default:
65107c478bd9Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
65117c478bd9Sstevel@tonic-gate 	}
65127c478bd9Sstevel@tonic-gate 
65137c478bd9Sstevel@tonic-gate out:
65147c478bd9Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
65157c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
65167c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
65177c478bd9Sstevel@tonic-gate 
65187c478bd9Sstevel@tonic-gate 	return (rebound ? ECONNRESET : 0);
65197c478bd9Sstevel@tonic-gate }
65207c478bd9Sstevel@tonic-gate 
6521ffbfbf47Srm88369 /*
6522ffbfbf47Srm88369  * process_delete() deletes an instance from the dgraph if 'fmri' is an
6523ffbfbf47Srm88369  * instance fmri or if 'fmri' matches the 'general' property group of an
6524ffbfbf47Srm88369  * instance (or the 'general/enabled' property).
6525ffbfbf47Srm88369  *
6526ffbfbf47Srm88369  * 'fmri' may be overwritten and cannot be trusted on return by the caller.
6527ffbfbf47Srm88369  */
65287c478bd9Sstevel@tonic-gate static void
65297c478bd9Sstevel@tonic-gate process_delete(char *fmri, scf_handle_t *h)
65307c478bd9Sstevel@tonic-gate {
6531ffbfbf47Srm88369 	char *lfmri, *end_inst_fmri;
6532ffbfbf47Srm88369 	const char *inst_name = NULL;
6533ffbfbf47Srm88369 	const char *pg_name = NULL;
6534ffbfbf47Srm88369 	const char *prop_name = NULL;
65357c478bd9Sstevel@tonic-gate 
65367c478bd9Sstevel@tonic-gate 	lfmri = safe_strdup(fmri);
65377c478bd9Sstevel@tonic-gate 
65387c478bd9Sstevel@tonic-gate 	/* Determine if the FMRI is a property group or instance */
65397c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(lfmri, NULL, NULL, &inst_name, &pg_name,
6540ffbfbf47Srm88369 	    &prop_name) != SCF_SUCCESS) {
65417c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
65427c478bd9Sstevel@tonic-gate 		    "Received invalid FMRI \"%s\" from repository server.\n",
65437c478bd9Sstevel@tonic-gate 		    fmri);
65447c478bd9Sstevel@tonic-gate 	} else if (inst_name != NULL && pg_name == NULL) {
65457c478bd9Sstevel@tonic-gate 		(void) dgraph_remove_instance(fmri, h);
6546ffbfbf47Srm88369 	} else if (inst_name != NULL && pg_name != NULL) {
6547ffbfbf47Srm88369 		/*
6548ffbfbf47Srm88369 		 * If we're deleting the 'general' property group or
6549ffbfbf47Srm88369 		 * 'general/enabled' property then the whole instance
6550ffbfbf47Srm88369 		 * must be removed from the dgraph.
6551ffbfbf47Srm88369 		 */
6552ffbfbf47Srm88369 		if (strcmp(pg_name, SCF_PG_GENERAL) != 0) {
6553ffbfbf47Srm88369 			free(lfmri);
6554ffbfbf47Srm88369 			return;
6555ffbfbf47Srm88369 		}
6556ffbfbf47Srm88369 
6557ffbfbf47Srm88369 		if (prop_name != NULL &&
6558ffbfbf47Srm88369 		    strcmp(prop_name, SCF_PROPERTY_ENABLED) != 0) {
6559ffbfbf47Srm88369 			free(lfmri);
6560ffbfbf47Srm88369 			return;
6561ffbfbf47Srm88369 		}
6562ffbfbf47Srm88369 
6563ffbfbf47Srm88369 		/*
6564ffbfbf47Srm88369 		 * Because the instance has already been deleted from the
6565ffbfbf47Srm88369 		 * repository, we cannot use any scf_ functions to retrieve
6566ffbfbf47Srm88369 		 * the instance FMRI however we can easily reconstruct it
6567ffbfbf47Srm88369 		 * manually.
6568ffbfbf47Srm88369 		 */
6569ffbfbf47Srm88369 		end_inst_fmri = strstr(fmri, SCF_FMRI_PROPERTYGRP_PREFIX);
6570ffbfbf47Srm88369 		if (end_inst_fmri == NULL)
6571ffbfbf47Srm88369 			bad_error("process_delete", 0);
6572ffbfbf47Srm88369 
6573ffbfbf47Srm88369 		end_inst_fmri[0] = '\0';
6574ffbfbf47Srm88369 
6575ffbfbf47Srm88369 		(void) dgraph_remove_instance(fmri, h);
65767c478bd9Sstevel@tonic-gate 	}
65777c478bd9Sstevel@tonic-gate 
65787c478bd9Sstevel@tonic-gate 	free(lfmri);
65797c478bd9Sstevel@tonic-gate }
65807c478bd9Sstevel@tonic-gate 
65817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
65827c478bd9Sstevel@tonic-gate void *
65837c478bd9Sstevel@tonic-gate repository_event_thread(void *unused)
65847c478bd9Sstevel@tonic-gate {
65857c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
65867c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
65877c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
65887c478bd9Sstevel@tonic-gate 	char *fmri = startd_alloc(max_scf_fmri_size);
65897c478bd9Sstevel@tonic-gate 	char *pg_name = startd_alloc(max_scf_value_size);
65907c478bd9Sstevel@tonic-gate 	int r;
65917c478bd9Sstevel@tonic-gate 
65927c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
65937c478bd9Sstevel@tonic-gate 
65947c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
65957c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
65967c478bd9Sstevel@tonic-gate 
65977c478bd9Sstevel@tonic-gate retry:
65987c478bd9Sstevel@tonic-gate 	if (_scf_notify_add_pgtype(h, SCF_GROUP_FRAMEWORK) != SCF_SUCCESS) {
65997c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN) {
66007c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
66017c478bd9Sstevel@tonic-gate 		} else {
66027c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
66037c478bd9Sstevel@tonic-gate 			    "Couldn't set up repository notification "
66047c478bd9Sstevel@tonic-gate 			    "for property group type %s: %s\n",
66057c478bd9Sstevel@tonic-gate 			    SCF_GROUP_FRAMEWORK, scf_strerror(scf_error()));
66067c478bd9Sstevel@tonic-gate 
66077c478bd9Sstevel@tonic-gate 			(void) sleep(1);
66087c478bd9Sstevel@tonic-gate 		}
66097c478bd9Sstevel@tonic-gate 
66107c478bd9Sstevel@tonic-gate 		goto retry;
66117c478bd9Sstevel@tonic-gate 	}
66127c478bd9Sstevel@tonic-gate 
66137c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
66147c478bd9Sstevel@tonic-gate 	while (1) {
66157c478bd9Sstevel@tonic-gate 		ssize_t res;
66167c478bd9Sstevel@tonic-gate 
66177c478bd9Sstevel@tonic-gate 		/* Note: fmri is only set on delete events. */
66187c478bd9Sstevel@tonic-gate 		res = _scf_notify_wait(pg, fmri, max_scf_fmri_size);
66197c478bd9Sstevel@tonic-gate 		if (res < 0) {
66207c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
66217c478bd9Sstevel@tonic-gate 			goto retry;
66227c478bd9Sstevel@tonic-gate 		} else if (res == 0) {
66237c478bd9Sstevel@tonic-gate 			/*
66247c478bd9Sstevel@tonic-gate 			 * property group modified.  inst and pg_name are
66257c478bd9Sstevel@tonic-gate 			 * pre-allocated scratch space.
66267c478bd9Sstevel@tonic-gate 			 */
66277c478bd9Sstevel@tonic-gate 			if (scf_pg_update(pg) < 0) {
66287c478bd9Sstevel@tonic-gate 				switch (scf_error()) {
66297c478bd9Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
66307c478bd9Sstevel@tonic-gate 					continue;
66317c478bd9Sstevel@tonic-gate 
66327c478bd9Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
66337c478bd9Sstevel@tonic-gate 					log_error(LOG_WARNING,
66347c478bd9Sstevel@tonic-gate 					    "Lost repository event due to "
66357c478bd9Sstevel@tonic-gate 					    "disconnection.\n");
66367c478bd9Sstevel@tonic-gate 					libscf_handle_rebind(h);
66377c478bd9Sstevel@tonic-gate 					goto retry;
66387c478bd9Sstevel@tonic-gate 
66397c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_BOUND:
66407c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
66417c478bd9Sstevel@tonic-gate 				default:
66427c478bd9Sstevel@tonic-gate 					bad_error("scf_pg_update", scf_error());
66437c478bd9Sstevel@tonic-gate 				}
66447c478bd9Sstevel@tonic-gate 			}
66457c478bd9Sstevel@tonic-gate 
66467c478bd9Sstevel@tonic-gate 			r = process_pg_event(h, pg, inst, pg_name);
66477c478bd9Sstevel@tonic-gate 			switch (r) {
66487c478bd9Sstevel@tonic-gate 			case 0:
66497c478bd9Sstevel@tonic-gate 				break;
66507c478bd9Sstevel@tonic-gate 
66517c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
66527c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Lost repository event "
66537c478bd9Sstevel@tonic-gate 				    "due to disconnection.\n");
66547c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
66557c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
66567c478bd9Sstevel@tonic-gate 
66577c478bd9Sstevel@tonic-gate 			case ECONNRESET:
66587c478bd9Sstevel@tonic-gate 				goto retry;
66597c478bd9Sstevel@tonic-gate 
66607c478bd9Sstevel@tonic-gate 			default:
66617c478bd9Sstevel@tonic-gate 				bad_error("process_pg_event", r);
66627c478bd9Sstevel@tonic-gate 			}
66637c478bd9Sstevel@tonic-gate 		} else {
6664ffbfbf47Srm88369 			/*
6665ffbfbf47Srm88369 			 * Service, instance, or pg deleted.
6666ffbfbf47Srm88369 			 * Don't trust fmri on return.
6667ffbfbf47Srm88369 			 */
66687c478bd9Sstevel@tonic-gate 			process_delete(fmri, h);
66697c478bd9Sstevel@tonic-gate 		}
66707c478bd9Sstevel@tonic-gate 	}
66717c478bd9Sstevel@tonic-gate 
66727c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
66737c478bd9Sstevel@tonic-gate 	return (NULL);
66747c478bd9Sstevel@tonic-gate }
66757c478bd9Sstevel@tonic-gate 
66767c478bd9Sstevel@tonic-gate void
66777c478bd9Sstevel@tonic-gate graph_engine_start()
66787c478bd9Sstevel@tonic-gate {
66797c478bd9Sstevel@tonic-gate 	int err;
66807c478bd9Sstevel@tonic-gate 
66817c478bd9Sstevel@tonic-gate 	(void) startd_thread_create(graph_thread, NULL);
66827c478bd9Sstevel@tonic-gate 
66837c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
66847c478bd9Sstevel@tonic-gate 	while (!initial_milestone_set) {
66857c478bd9Sstevel@tonic-gate 		err = pthread_cond_wait(&initial_milestone_cv, &dgraph_lock);
66867c478bd9Sstevel@tonic-gate 		assert(err == 0);
66877c478bd9Sstevel@tonic-gate 	}
66887c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
66897c478bd9Sstevel@tonic-gate 
66907c478bd9Sstevel@tonic-gate 	(void) startd_thread_create(repository_event_thread, NULL);
66917c478bd9Sstevel@tonic-gate 	(void) startd_thread_create(graph_event_thread, NULL);
66927c478bd9Sstevel@tonic-gate }
6693