xref: /titanic_51/usr/src/cmd/svc/startd/graph.c (revision 70cbfe41f2338b77c15f79c6625eca6e70c412f3)
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  */
217c478bd9Sstevel@tonic-gate /*
22ffbfbf47Srm88369  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * graph.c - master restarter graph engine
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  *   The graph engine keeps a dependency graph of all service instances on the
307c478bd9Sstevel@tonic-gate  *   system, as recorded in the repository.  It decides when services should
317c478bd9Sstevel@tonic-gate  *   be brought up or down based on service states and dependencies and sends
327c478bd9Sstevel@tonic-gate  *   commands to restarters to effect any changes.  It also executes
337c478bd9Sstevel@tonic-gate  *   administrator commands sent by svcadm via the repository.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  *   The graph is stored in uu_list_t *dgraph and its vertices are
367c478bd9Sstevel@tonic-gate  *   graph_vertex_t's, each of which has a name and an integer id unique to
377c478bd9Sstevel@tonic-gate  *   its name (see dict.c).  A vertex's type attribute designates the type
387c478bd9Sstevel@tonic-gate  *   of object it represents: GVT_INST for service instances, GVT_SVC for
397c478bd9Sstevel@tonic-gate  *   service objects (since service instances may depend on another service,
407c478bd9Sstevel@tonic-gate  *   rather than service instance), GVT_FILE for files (which services may
417c478bd9Sstevel@tonic-gate  *   depend on), and GVT_GROUP for dependencies on multiple objects.  GVT_GROUP
427c478bd9Sstevel@tonic-gate  *   vertices are necessary because dependency lists may have particular
437c478bd9Sstevel@tonic-gate  *   grouping types (require any, require all, optional, or exclude) and
447c478bd9Sstevel@tonic-gate  *   event-propagation characteristics.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  *   The initial graph is built by libscf_populate_graph() invoking
477c478bd9Sstevel@tonic-gate  *   dgraph_add_instance() for each instance in the repository.  The function
487c478bd9Sstevel@tonic-gate  *   adds a GVT_SVC vertex for the service if one does not already exist, adds
497c478bd9Sstevel@tonic-gate  *   a GVT_INST vertex named by the FMRI of the instance, and sets up the edges.
507c478bd9Sstevel@tonic-gate  *   The resulting web of vertices & edges associated with an instance's vertex
517c478bd9Sstevel@tonic-gate  *   includes
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  *     - an edge from the GVT_SVC vertex for the instance's service
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  *     - an edge to the GVT_INST vertex of the instance's resarter, if its
567c478bd9Sstevel@tonic-gate  *       restarter is not svc.startd
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  *     - edges from other GVT_INST vertices if the instance is a restarter
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  *     - for each dependency property group in the instance's "running"
617c478bd9Sstevel@tonic-gate  *       snapshot, an edge to a GVT_GROUP vertex named by the FMRI of the
627c478bd9Sstevel@tonic-gate  *       instance and the name of the property group
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  *     - for each value of the "entities" property in each dependency property
657c478bd9Sstevel@tonic-gate  *       group, an edge from the corresponding GVT_GROUP vertex to a
667c478bd9Sstevel@tonic-gate  *       GVT_INST, GVT_SVC, or GVT_FILE vertex
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  *     - edges from GVT_GROUP vertices for each dependent instance
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  *   After the edges are set up the vertex's GV_CONFIGURED flag is set.  If
717c478bd9Sstevel@tonic-gate  *   there are problems, or if a service is mentioned in a dependency but does
727c478bd9Sstevel@tonic-gate  *   not exist in the repository, the GV_CONFIGURED flag will be clear.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  *   The graph and all of its vertices are protected by the dgraph_lock mutex.
757c478bd9Sstevel@tonic-gate  *   See restarter.c for more information.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  *   The properties of an instance fall into two classes: immediate and
787c478bd9Sstevel@tonic-gate  *   snapshotted.  Immediate properties should have an immediate effect when
797c478bd9Sstevel@tonic-gate  *   changed.  Snapshotted properties should be read from a snapshot, so they
807c478bd9Sstevel@tonic-gate  *   only change when the snapshot changes.  The immediate properties used by
817c478bd9Sstevel@tonic-gate  *   the graph engine are general/enabled, general/restarter, and the properties
827c478bd9Sstevel@tonic-gate  *   in the restarter_actions property group.  Since they are immediate, they
837c478bd9Sstevel@tonic-gate  *   are not read out of a snapshot.  The snapshotted properties used by the
847c478bd9Sstevel@tonic-gate  *   graph engine are those in the property groups with type "dependency" and
857c478bd9Sstevel@tonic-gate  *   are read out of the "running" snapshot.  The "running" snapshot is created
867c478bd9Sstevel@tonic-gate  *   by the the graph engine as soon as possible, and it is updated, along with
877c478bd9Sstevel@tonic-gate  *   in-core copies of the data (dependency information for the graph engine) on
887c478bd9Sstevel@tonic-gate  *   receipt of the refresh command from svcadm.  In addition, the graph engine
897c478bd9Sstevel@tonic-gate  *   updates the "start" snapshot from the "running" snapshot whenever a service
907c478bd9Sstevel@tonic-gate  *   comes online.
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
947c478bd9Sstevel@tonic-gate #include <sys/wait.h>
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #include <assert.h>
977c478bd9Sstevel@tonic-gate #include <errno.h>
987c478bd9Sstevel@tonic-gate #include <fcntl.h>
997c478bd9Sstevel@tonic-gate #include <libscf.h>
1007c478bd9Sstevel@tonic-gate #include <libscf_priv.h>
1017c478bd9Sstevel@tonic-gate #include <libuutil.h>
1027c478bd9Sstevel@tonic-gate #include <locale.h>
1037c478bd9Sstevel@tonic-gate #include <poll.h>
1047c478bd9Sstevel@tonic-gate #include <pthread.h>
1057c478bd9Sstevel@tonic-gate #include <signal.h>
1067c478bd9Sstevel@tonic-gate #include <stddef.h>
1077c478bd9Sstevel@tonic-gate #include <stdio.h>
1087c478bd9Sstevel@tonic-gate #include <stdlib.h>
1097c478bd9Sstevel@tonic-gate #include <string.h>
1107c478bd9Sstevel@tonic-gate #include <strings.h>
1117c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
1127c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
1137c478bd9Sstevel@tonic-gate #include <zone.h>
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #include "startd.h"
1167c478bd9Sstevel@tonic-gate #include "protocol.h"
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate #define	MILESTONE_NONE	((graph_vertex_t *)1)
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #define	CONSOLE_LOGIN_FMRI	"svc:/system/console-login:default"
1227c478bd9Sstevel@tonic-gate #define	FS_MINIMAL_FMRI		"svc:/system/filesystem/minimal:default"
1237c478bd9Sstevel@tonic-gate 
1243ad28c1eSrm88369 #define	VERTEX_REMOVED	0	/* vertex has been freed  */
1253ad28c1eSrm88369 #define	VERTEX_INUSE	1	/* vertex is still in use */
1263ad28c1eSrm88369 
12756e23938Sbustos /*
12856e23938Sbustos  * Services in these states are not considered 'down' by the
12956e23938Sbustos  * milestone/shutdown code.
13056e23938Sbustos  */
13156e23938Sbustos #define	up_state(state)	((state) == RESTARTER_STATE_ONLINE || \
13256e23938Sbustos 	(state) == RESTARTER_STATE_DEGRADED || \
13356e23938Sbustos 	(state) == RESTARTER_STATE_OFFLINE)
13456e23938Sbustos 
1357c478bd9Sstevel@tonic-gate static uu_list_pool_t *graph_edge_pool, *graph_vertex_pool;
1367c478bd9Sstevel@tonic-gate static uu_list_t *dgraph;
1377c478bd9Sstevel@tonic-gate static pthread_mutex_t dgraph_lock;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * milestone indicates the current subgraph.  When NULL, it is the entire
1417c478bd9Sstevel@tonic-gate  * graph.  When MILESTONE_NONE, it is the empty graph.  Otherwise, it is all
1427c478bd9Sstevel@tonic-gate  * services on which the target vertex depends.
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate static graph_vertex_t *milestone = NULL;
1457c478bd9Sstevel@tonic-gate static boolean_t initial_milestone_set = B_FALSE;
1467c478bd9Sstevel@tonic-gate static pthread_cond_t initial_milestone_cv = PTHREAD_COND_INITIALIZER;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate /* protected by dgraph_lock */
1497c478bd9Sstevel@tonic-gate static boolean_t sulogin_thread_running = B_FALSE;
1507c478bd9Sstevel@tonic-gate static boolean_t sulogin_running = B_FALSE;
1517c478bd9Sstevel@tonic-gate static boolean_t console_login_ready = B_FALSE;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /* Number of services to come down to complete milestone transition. */
1547c478bd9Sstevel@tonic-gate static uint_t non_subgraph_svcs;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * These variables indicate what should be done when we reach the milestone
1587c478bd9Sstevel@tonic-gate  * target milestone, i.e., when non_subgraph_svcs == 0.  They are acted upon in
1597c478bd9Sstevel@tonic-gate  * dgraph_set_instance_state().
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate static int halting = -1;
1627c478bd9Sstevel@tonic-gate static boolean_t go_single_user_mode = B_FALSE;
1637c478bd9Sstevel@tonic-gate static boolean_t go_to_level1 = B_FALSE;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * This tracks the legacy runlevel to ensure we signal init and manage
1677c478bd9Sstevel@tonic-gate  * utmpx entries correctly.
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate static char current_runlevel = '\0';
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /* Number of single user threads currently running */
1727c478bd9Sstevel@tonic-gate static pthread_mutex_t single_user_thread_lock;
1737c478bd9Sstevel@tonic-gate static int single_user_thread_count = 0;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /* Statistics for dependency cycle-checking */
1767c478bd9Sstevel@tonic-gate static u_longlong_t dep_inserts = 0;
1777c478bd9Sstevel@tonic-gate static u_longlong_t dep_cycle_ns = 0;
1787c478bd9Sstevel@tonic-gate static u_longlong_t dep_insert_ns = 0;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static const char * const emsg_invalid_restarter =
18299b44c3bSlianep 	"Transitioning %s to maintenance, restarter FMRI %s is invalid "
18399b44c3bSlianep 	"(see 'svcs -xv' for details).\n";
1847c478bd9Sstevel@tonic-gate static const char * const console_login_fmri = CONSOLE_LOGIN_FMRI;
1857c478bd9Sstevel@tonic-gate static const char * const single_user_fmri = SCF_MILESTONE_SINGLE_USER;
1867c478bd9Sstevel@tonic-gate static const char * const multi_user_fmri = SCF_MILESTONE_MULTI_USER;
1877c478bd9Sstevel@tonic-gate static const char * const multi_user_svr_fmri = SCF_MILESTONE_MULTI_USER_SERVER;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * These services define the system being "up".  If none of them can come
1927c478bd9Sstevel@tonic-gate  * online, then we will run sulogin on the console.  Note that the install ones
1937c478bd9Sstevel@tonic-gate  * are for the miniroot and when installing CDs after the first.  can_come_up()
1947c478bd9Sstevel@tonic-gate  * does the decision making, and an sulogin_thread() runs sulogin, which can be
1957c478bd9Sstevel@tonic-gate  * started by dgraph_set_instance_state() or single_user_thread().
1967c478bd9Sstevel@tonic-gate  *
1977c478bd9Sstevel@tonic-gate  * NOTE: can_come_up() relies on SCF_MILESTONE_SINGLE_USER being the first
1987c478bd9Sstevel@tonic-gate  * entry, which is only used when booting_to_single_user (boot -s) is set.
1997c478bd9Sstevel@tonic-gate  * This is because when doing a "boot -s", sulogin is started from specials.c
2007c478bd9Sstevel@tonic-gate  * after milestone/single-user comes online, for backwards compatibility.
2017c478bd9Sstevel@tonic-gate  * In this case, SCF_MILESTONE_SINGLE_USER needs to be part of up_svcs
2027c478bd9Sstevel@tonic-gate  * to ensure sulogin will be spawned if milestone/single-user cannot be reached.
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate static const char * const up_svcs[] = {
2057c478bd9Sstevel@tonic-gate 	SCF_MILESTONE_SINGLE_USER,
2067c478bd9Sstevel@tonic-gate 	CONSOLE_LOGIN_FMRI,
2077c478bd9Sstevel@tonic-gate 	"svc:/system/install-setup:default",
2087c478bd9Sstevel@tonic-gate 	"svc:/system/install:default",
2097c478bd9Sstevel@tonic-gate 	NULL
2107c478bd9Sstevel@tonic-gate };
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /* This array must have an element for each non-NULL element of up_svcs[]. */
2137c478bd9Sstevel@tonic-gate static graph_vertex_t *up_svcs_p[] = { NULL, NULL, NULL, NULL };
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /* These are for seed repository magic.  See can_come_up(). */
2167c478bd9Sstevel@tonic-gate static const char * const manifest_import =
2177c478bd9Sstevel@tonic-gate 	"svc:/system/manifest-import:default";
2187c478bd9Sstevel@tonic-gate static graph_vertex_t *manifest_import_p = NULL;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate static char target_milestone_as_runlevel(void);
2227c478bd9Sstevel@tonic-gate static void graph_runlevel_changed(char rl, int online);
2237c478bd9Sstevel@tonic-gate static int dgraph_set_milestone(const char *, scf_handle_t *, boolean_t);
2247c478bd9Sstevel@tonic-gate static boolean_t should_be_in_subgraph(graph_vertex_t *v);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate  * graph_vertex_compare()
2287c478bd9Sstevel@tonic-gate  *	This function can compare either int *id or * graph_vertex_t *gv
2297c478bd9Sstevel@tonic-gate  *	values, as the vertex id is always the first element of a
2307c478bd9Sstevel@tonic-gate  *	graph_vertex structure.
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate /* ARGSUSED */
2337c478bd9Sstevel@tonic-gate static int
2347c478bd9Sstevel@tonic-gate graph_vertex_compare(const void *lc_arg, const void *rc_arg, void *private)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	int lc_id = ((const graph_vertex_t *)lc_arg)->gv_id;
2377c478bd9Sstevel@tonic-gate 	int rc_id = *(int *)rc_arg;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	if (lc_id > rc_id)
2407c478bd9Sstevel@tonic-gate 		return (1);
2417c478bd9Sstevel@tonic-gate 	if (lc_id < rc_id)
2427c478bd9Sstevel@tonic-gate 		return (-1);
2437c478bd9Sstevel@tonic-gate 	return (0);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate void
2477c478bd9Sstevel@tonic-gate graph_init()
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	graph_edge_pool = startd_list_pool_create("graph_edges",
2507c478bd9Sstevel@tonic-gate 	    sizeof (graph_edge_t), offsetof(graph_edge_t, ge_link), NULL,
2517c478bd9Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG);
2527c478bd9Sstevel@tonic-gate 	assert(graph_edge_pool != NULL);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	graph_vertex_pool = startd_list_pool_create("graph_vertices",
2557c478bd9Sstevel@tonic-gate 	    sizeof (graph_vertex_t), offsetof(graph_vertex_t, gv_link),
2567c478bd9Sstevel@tonic-gate 	    graph_vertex_compare, UU_LIST_POOL_DEBUG);
2577c478bd9Sstevel@tonic-gate 	assert(graph_vertex_pool != NULL);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&dgraph_lock, &mutex_attrs);
2607c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&single_user_thread_lock, &mutex_attrs);
2617c478bd9Sstevel@tonic-gate 	dgraph = startd_list_create(graph_vertex_pool, NULL, UU_LIST_SORTED);
2627c478bd9Sstevel@tonic-gate 	assert(dgraph != NULL);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (!st->st_initial)
2657c478bd9Sstevel@tonic-gate 		current_runlevel = utmpx_get_runlevel();
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Initialized graph\n");
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate static graph_vertex_t *
2717c478bd9Sstevel@tonic-gate vertex_get_by_name(const char *name)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	int id;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	id = dict_lookup_byname(name);
2787c478bd9Sstevel@tonic-gate 	if (id == -1)
2797c478bd9Sstevel@tonic-gate 		return (NULL);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	return (uu_list_find(dgraph, &id, NULL, NULL));
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate static graph_vertex_t *
2857c478bd9Sstevel@tonic-gate vertex_get_by_id(int id)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	if (id == -1)
2907c478bd9Sstevel@tonic-gate 		return (NULL);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	return (uu_list_find(dgraph, &id, NULL, NULL));
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  * Creates a new vertex with the given name, adds it to the graph, and returns
2977c478bd9Sstevel@tonic-gate  * a pointer to it.  The graph lock must be held by this thread on entry.
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate static graph_vertex_t *
3007c478bd9Sstevel@tonic-gate graph_add_vertex(const char *name)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 	int id;
3037c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
3047c478bd9Sstevel@tonic-gate 	void *p;
3057c478bd9Sstevel@tonic-gate 	uu_list_index_t idx;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	id = dict_insert(name);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	v = startd_zalloc(sizeof (*v));
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	v->gv_id = id;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	v->gv_name = startd_alloc(strlen(name) + 1);
3167c478bd9Sstevel@tonic-gate 	(void) strcpy(v->gv_name, name);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	v->gv_dependencies = startd_list_create(graph_edge_pool, v, 0);
3197c478bd9Sstevel@tonic-gate 	v->gv_dependents = startd_list_create(graph_edge_pool, v, 0);
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	p = uu_list_find(dgraph, &id, NULL, &idx);
3227c478bd9Sstevel@tonic-gate 	assert(p == NULL);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	uu_list_node_init(v, &v->gv_link, graph_vertex_pool);
3257c478bd9Sstevel@tonic-gate 	uu_list_insert(dgraph, v, idx);
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	return (v);
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate  * Removes v from the graph and frees it.  The graph should be locked by this
3327c478bd9Sstevel@tonic-gate  * thread, and v should have no edges associated with it.
3337c478bd9Sstevel@tonic-gate  */
3347c478bd9Sstevel@tonic-gate static void
3357c478bd9Sstevel@tonic-gate graph_remove_vertex(graph_vertex_t *v)
3367c478bd9Sstevel@tonic-gate {
3377c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependencies) == 0);
3407c478bd9Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependents) == 0);
3413ad28c1eSrm88369 	assert(v->gv_refs == 0);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	startd_free(v->gv_name, strlen(v->gv_name) + 1);
3447c478bd9Sstevel@tonic-gate 	uu_list_destroy(v->gv_dependencies);
3457c478bd9Sstevel@tonic-gate 	uu_list_destroy(v->gv_dependents);
3467c478bd9Sstevel@tonic-gate 	uu_list_remove(dgraph, v);
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	startd_free(v, sizeof (graph_vertex_t));
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate static void
3527c478bd9Sstevel@tonic-gate graph_add_edge(graph_vertex_t *fv, graph_vertex_t *tv)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	graph_edge_t *e, *re;
3557c478bd9Sstevel@tonic-gate 	int r;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	e = startd_alloc(sizeof (graph_edge_t));
3607c478bd9Sstevel@tonic-gate 	re = startd_alloc(sizeof (graph_edge_t));
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	e->ge_parent = fv;
3637c478bd9Sstevel@tonic-gate 	e->ge_vertex = tv;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	re->ge_parent = tv;
3667c478bd9Sstevel@tonic-gate 	re->ge_vertex = fv;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	uu_list_node_init(e, &e->ge_link, graph_edge_pool);
3697c478bd9Sstevel@tonic-gate 	r = uu_list_insert_before(fv->gv_dependencies, NULL, e);
3707c478bd9Sstevel@tonic-gate 	assert(r == 0);
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	uu_list_node_init(re, &re->ge_link, graph_edge_pool);
3737c478bd9Sstevel@tonic-gate 	r = uu_list_insert_before(tv->gv_dependents, NULL, re);
3747c478bd9Sstevel@tonic-gate 	assert(r == 0);
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate static void
3787c478bd9Sstevel@tonic-gate graph_remove_edge(graph_vertex_t *v, graph_vertex_t *dv)
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
3837c478bd9Sstevel@tonic-gate 	    e != NULL;
3847c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
3857c478bd9Sstevel@tonic-gate 		if (e->ge_vertex == dv) {
3867c478bd9Sstevel@tonic-gate 			uu_list_remove(v->gv_dependencies, e);
3877c478bd9Sstevel@tonic-gate 			startd_free(e, sizeof (graph_edge_t));
3887c478bd9Sstevel@tonic-gate 			break;
3897c478bd9Sstevel@tonic-gate 		}
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(dv->gv_dependents);
3937c478bd9Sstevel@tonic-gate 	    e != NULL;
3947c478bd9Sstevel@tonic-gate 	    e = uu_list_next(dv->gv_dependents, e)) {
3957c478bd9Sstevel@tonic-gate 		if (e->ge_vertex == v) {
3967c478bd9Sstevel@tonic-gate 			uu_list_remove(dv->gv_dependents, e);
3977c478bd9Sstevel@tonic-gate 			startd_free(e, sizeof (graph_edge_t));
3987c478bd9Sstevel@tonic-gate 			break;
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate static void
4043ad28c1eSrm88369 remove_inst_vertex(graph_vertex_t *v)
4053ad28c1eSrm88369 {
4063ad28c1eSrm88369 	graph_edge_t *e;
4073ad28c1eSrm88369 	graph_vertex_t *sv;
4083ad28c1eSrm88369 	int i;
4093ad28c1eSrm88369 
4103ad28c1eSrm88369 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
4113ad28c1eSrm88369 	assert(uu_list_numnodes(v->gv_dependents) == 1);
4123ad28c1eSrm88369 	assert(uu_list_numnodes(v->gv_dependencies) == 0);
4133ad28c1eSrm88369 	assert(v->gv_refs == 0);
4143ad28c1eSrm88369 	assert((v->gv_flags & GV_CONFIGURED) == 0);
4153ad28c1eSrm88369 
4163ad28c1eSrm88369 	e = uu_list_first(v->gv_dependents);
4173ad28c1eSrm88369 	sv = e->ge_vertex;
4183ad28c1eSrm88369 	graph_remove_edge(sv, v);
4193ad28c1eSrm88369 
4203ad28c1eSrm88369 	for (i = 0; up_svcs[i] != NULL; ++i) {
4213ad28c1eSrm88369 		if (up_svcs_p[i] == v)
4223ad28c1eSrm88369 			up_svcs_p[i] = NULL;
4233ad28c1eSrm88369 	}
4243ad28c1eSrm88369 
4253ad28c1eSrm88369 	if (manifest_import_p == v)
4263ad28c1eSrm88369 		manifest_import_p = NULL;
4273ad28c1eSrm88369 
4283ad28c1eSrm88369 	graph_remove_vertex(v);
4293ad28c1eSrm88369 
4303ad28c1eSrm88369 	if (uu_list_numnodes(sv->gv_dependencies) == 0 &&
4313ad28c1eSrm88369 	    uu_list_numnodes(sv->gv_dependents) == 0 &&
4323ad28c1eSrm88369 	    sv->gv_refs == 0)
4333ad28c1eSrm88369 		graph_remove_vertex(sv);
4343ad28c1eSrm88369 }
4353ad28c1eSrm88369 
4363ad28c1eSrm88369 static void
4377c478bd9Sstevel@tonic-gate graph_walk_dependents(graph_vertex_t *v, void (*func)(graph_vertex_t *, void *),
4387c478bd9Sstevel@tonic-gate     void *arg)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
4437c478bd9Sstevel@tonic-gate 	    e != NULL;
4447c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e))
4457c478bd9Sstevel@tonic-gate 		func(e->ge_vertex, arg);
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate static void
4497c478bd9Sstevel@tonic-gate graph_walk_dependencies(graph_vertex_t *v, void (*func)(graph_vertex_t *,
4507c478bd9Sstevel@tonic-gate 	void *), void *arg)
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
4577c478bd9Sstevel@tonic-gate 	    e != NULL;
4587c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		func(e->ge_vertex, arg);
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate /*
4657c478bd9Sstevel@tonic-gate  * Generic graph walking function.
4667c478bd9Sstevel@tonic-gate  *
4677c478bd9Sstevel@tonic-gate  * Given a vertex, this function will walk either dependencies
4687c478bd9Sstevel@tonic-gate  * (WALK_DEPENDENCIES) or dependents (WALK_DEPENDENTS) of a vertex recursively
4697c478bd9Sstevel@tonic-gate  * for the entire graph.  It will avoid cycles and never visit the same vertex
4707c478bd9Sstevel@tonic-gate  * twice.
4717c478bd9Sstevel@tonic-gate  *
4727c478bd9Sstevel@tonic-gate  * We avoid traversing exclusion dependencies, because they are allowed to
4737c478bd9Sstevel@tonic-gate  * create cycles in the graph.  When propagating satisfiability, there is no
4747c478bd9Sstevel@tonic-gate  * need to walk exclusion dependencies because exclude_all_satisfied() doesn't
4757c478bd9Sstevel@tonic-gate  * test for satisfiability.
4767c478bd9Sstevel@tonic-gate  *
4777c478bd9Sstevel@tonic-gate  * The walker takes two callbacks.  The first is called before examining the
4787c478bd9Sstevel@tonic-gate  * dependents of each vertex.  The second is called on each vertex after
4797c478bd9Sstevel@tonic-gate  * examining its dependents.  This allows is_path_to() to construct a path only
4807c478bd9Sstevel@tonic-gate  * after the target vertex has been found.
4817c478bd9Sstevel@tonic-gate  */
4827c478bd9Sstevel@tonic-gate typedef enum {
4837c478bd9Sstevel@tonic-gate 	WALK_DEPENDENTS,
4847c478bd9Sstevel@tonic-gate 	WALK_DEPENDENCIES
4857c478bd9Sstevel@tonic-gate } graph_walk_dir_t;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate typedef int (*graph_walk_cb_t)(graph_vertex_t *, void *);
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate typedef struct graph_walk_info {
4907c478bd9Sstevel@tonic-gate 	graph_walk_dir_t 	gi_dir;
4917c478bd9Sstevel@tonic-gate 	uchar_t			*gi_visited;	/* vertex bitmap */
4927c478bd9Sstevel@tonic-gate 	int			(*gi_pre)(graph_vertex_t *, void *);
4937c478bd9Sstevel@tonic-gate 	void			(*gi_post)(graph_vertex_t *, void *);
4947c478bd9Sstevel@tonic-gate 	void			*gi_arg;	/* callback arg */
4957c478bd9Sstevel@tonic-gate 	int			gi_ret;		/* return value */
4967c478bd9Sstevel@tonic-gate } graph_walk_info_t;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate static int
4997c478bd9Sstevel@tonic-gate graph_walk_recurse(graph_edge_t *e, graph_walk_info_t *gip)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	uu_list_t *list;
5027c478bd9Sstevel@tonic-gate 	int r;
5037c478bd9Sstevel@tonic-gate 	graph_vertex_t *v = e->ge_vertex;
5047c478bd9Sstevel@tonic-gate 	int i;
5057c478bd9Sstevel@tonic-gate 	uint_t b;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	i = v->gv_id / 8;
5087c478bd9Sstevel@tonic-gate 	b = 1 << (v->gv_id % 8);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	/*
5117c478bd9Sstevel@tonic-gate 	 * Check to see if we've visited this vertex already.
5127c478bd9Sstevel@tonic-gate 	 */
5137c478bd9Sstevel@tonic-gate 	if (gip->gi_visited[i] & b)
5147c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	gip->gi_visited[i] |= b;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	/*
5197c478bd9Sstevel@tonic-gate 	 * Don't follow exclusions.
5207c478bd9Sstevel@tonic-gate 	 */
5217c478bd9Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
5227c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/*
5257c478bd9Sstevel@tonic-gate 	 * Call pre-visit callback.  If this doesn't terminate the walk,
5267c478bd9Sstevel@tonic-gate 	 * continue search.
5277c478bd9Sstevel@tonic-gate 	 */
5287c478bd9Sstevel@tonic-gate 	if ((gip->gi_ret = gip->gi_pre(v, gip->gi_arg)) == UU_WALK_NEXT) {
5297c478bd9Sstevel@tonic-gate 		/*
5307c478bd9Sstevel@tonic-gate 		 * Recurse using appropriate list.
5317c478bd9Sstevel@tonic-gate 		 */
5327c478bd9Sstevel@tonic-gate 		if (gip->gi_dir == WALK_DEPENDENTS)
5337c478bd9Sstevel@tonic-gate 			list = v->gv_dependents;
5347c478bd9Sstevel@tonic-gate 		else
5357c478bd9Sstevel@tonic-gate 			list = v->gv_dependencies;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 		r = uu_list_walk(list, (uu_walk_fn_t *)graph_walk_recurse,
5387c478bd9Sstevel@tonic-gate 		    gip, 0);
5397c478bd9Sstevel@tonic-gate 		assert(r == 0);
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/*
5437c478bd9Sstevel@tonic-gate 	 * Callbacks must return either UU_WALK_NEXT or UU_WALK_DONE.
5447c478bd9Sstevel@tonic-gate 	 */
5457c478bd9Sstevel@tonic-gate 	assert(gip->gi_ret == UU_WALK_NEXT || gip->gi_ret == UU_WALK_DONE);
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/*
5487c478bd9Sstevel@tonic-gate 	 * If given a post-callback, call the function for every vertex.
5497c478bd9Sstevel@tonic-gate 	 */
5507c478bd9Sstevel@tonic-gate 	if (gip->gi_post != NULL)
5517c478bd9Sstevel@tonic-gate 		(void) gip->gi_post(v, gip->gi_arg);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	/*
5547c478bd9Sstevel@tonic-gate 	 * Preserve the callback's return value.  If the callback returns
5557c478bd9Sstevel@tonic-gate 	 * UU_WALK_DONE, then we propagate that to the caller in order to
5567c478bd9Sstevel@tonic-gate 	 * terminate the walk.
5577c478bd9Sstevel@tonic-gate 	 */
5587c478bd9Sstevel@tonic-gate 	return (gip->gi_ret);
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate static void
5627c478bd9Sstevel@tonic-gate graph_walk(graph_vertex_t *v, graph_walk_dir_t dir,
5637c478bd9Sstevel@tonic-gate     int (*pre)(graph_vertex_t *, void *),
5647c478bd9Sstevel@tonic-gate     void (*post)(graph_vertex_t *, void *), void *arg)
5657c478bd9Sstevel@tonic-gate {
5667c478bd9Sstevel@tonic-gate 	graph_walk_info_t gi;
5677c478bd9Sstevel@tonic-gate 	graph_edge_t fake;
5687c478bd9Sstevel@tonic-gate 	size_t sz = dictionary->dict_new_id / 8 + 1;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	gi.gi_visited = startd_zalloc(sz);
5717c478bd9Sstevel@tonic-gate 	gi.gi_pre = pre;
5727c478bd9Sstevel@tonic-gate 	gi.gi_post = post;
5737c478bd9Sstevel@tonic-gate 	gi.gi_arg = arg;
5747c478bd9Sstevel@tonic-gate 	gi.gi_dir = dir;
5757c478bd9Sstevel@tonic-gate 	gi.gi_ret = 0;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	/*
5787c478bd9Sstevel@tonic-gate 	 * Fake up an edge for the first iteration
5797c478bd9Sstevel@tonic-gate 	 */
5807c478bd9Sstevel@tonic-gate 	fake.ge_vertex = v;
5817c478bd9Sstevel@tonic-gate 	(void) graph_walk_recurse(&fake, &gi);
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	startd_free(gi.gi_visited, sz);
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate typedef struct child_search {
5877c478bd9Sstevel@tonic-gate 	int	id;		/* id of vertex to look for */
5887c478bd9Sstevel@tonic-gate 	uint_t	depth;		/* recursion depth */
5897c478bd9Sstevel@tonic-gate 	/*
5907c478bd9Sstevel@tonic-gate 	 * While the vertex is not found, path is NULL.  After the search, if
5917c478bd9Sstevel@tonic-gate 	 * the vertex was found then path should point to a -1-terminated
5927c478bd9Sstevel@tonic-gate 	 * array of vertex id's which constitute the path to the vertex.
5937c478bd9Sstevel@tonic-gate 	 */
5947c478bd9Sstevel@tonic-gate 	int	*path;
5957c478bd9Sstevel@tonic-gate } child_search_t;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate static int
5987c478bd9Sstevel@tonic-gate child_pre(graph_vertex_t *v, void *arg)
5997c478bd9Sstevel@tonic-gate {
6007c478bd9Sstevel@tonic-gate 	child_search_t *cs = arg;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	cs->depth++;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	if (v->gv_id == cs->id) {
6057c478bd9Sstevel@tonic-gate 		cs->path = startd_alloc((cs->depth + 1) * sizeof (int));
6067c478bd9Sstevel@tonic-gate 		cs->path[cs->depth] = -1;
6077c478bd9Sstevel@tonic-gate 		return (UU_WALK_DONE);
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate static void
6147c478bd9Sstevel@tonic-gate child_post(graph_vertex_t *v, void *arg)
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	child_search_t *cs = arg;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	cs->depth--;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	if (cs->path != NULL)
6217c478bd9Sstevel@tonic-gate 		cs->path[cs->depth] = v->gv_id;
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate  * Look for a path from from to to.  If one exists, returns a pointer to
6267c478bd9Sstevel@tonic-gate  * a NULL-terminated array of pointers to the vertices along the path.  If
6277c478bd9Sstevel@tonic-gate  * there is no path, returns NULL.
6287c478bd9Sstevel@tonic-gate  */
6297c478bd9Sstevel@tonic-gate static int *
6307c478bd9Sstevel@tonic-gate is_path_to(graph_vertex_t *from, graph_vertex_t *to)
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate 	child_search_t cs;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	cs.id = to->gv_id;
6357c478bd9Sstevel@tonic-gate 	cs.depth = 0;
6367c478bd9Sstevel@tonic-gate 	cs.path = NULL;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	graph_walk(from, WALK_DEPENDENCIES, child_pre, child_post, &cs);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	return (cs.path);
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate /*
6447c478bd9Sstevel@tonic-gate  * Given an array of int's as returned by is_path_to, allocates a string of
6457c478bd9Sstevel@tonic-gate  * their names joined by newlines.  Returns the size of the allocated buffer
6467c478bd9Sstevel@tonic-gate  * in *sz and frees path.
6477c478bd9Sstevel@tonic-gate  */
6487c478bd9Sstevel@tonic-gate static void
6497c478bd9Sstevel@tonic-gate path_to_str(int *path, char **cpp, size_t *sz)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate 	int i;
6527c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
6537c478bd9Sstevel@tonic-gate 	size_t allocd, new_allocd;
6547c478bd9Sstevel@tonic-gate 	char *new, *name;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
6577c478bd9Sstevel@tonic-gate 	assert(path[0] != -1);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	allocd = 1;
6607c478bd9Sstevel@tonic-gate 	*cpp = startd_alloc(1);
6617c478bd9Sstevel@tonic-gate 	(*cpp)[0] = '\0';
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	for (i = 0; path[i] != -1; ++i) {
6647c478bd9Sstevel@tonic-gate 		name = NULL;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 		v = vertex_get_by_id(path[i]);
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 		if (v == NULL)
6697c478bd9Sstevel@tonic-gate 			name = "<deleted>";
6707c478bd9Sstevel@tonic-gate 		else if (v->gv_type == GVT_INST || v->gv_type == GVT_SVC)
6717c478bd9Sstevel@tonic-gate 			name = v->gv_name;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 		if (name != NULL) {
6747c478bd9Sstevel@tonic-gate 			new_allocd = allocd + strlen(name) + 1;
6757c478bd9Sstevel@tonic-gate 			new = startd_alloc(new_allocd);
6767c478bd9Sstevel@tonic-gate 			(void) strcpy(new, *cpp);
6777c478bd9Sstevel@tonic-gate 			(void) strcat(new, name);
6787c478bd9Sstevel@tonic-gate 			(void) strcat(new, "\n");
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 			startd_free(*cpp, allocd);
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 			*cpp = new;
6837c478bd9Sstevel@tonic-gate 			allocd = new_allocd;
6847c478bd9Sstevel@tonic-gate 		}
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	startd_free(path, sizeof (int) * (i + 1));
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	*sz = allocd;
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate /*
6947c478bd9Sstevel@tonic-gate  * This function along with run_sulogin() implements an exclusion relationship
6957c478bd9Sstevel@tonic-gate  * between system/console-login and sulogin.  run_sulogin() will fail if
6967c478bd9Sstevel@tonic-gate  * system/console-login is online, and the graph engine should call
6977c478bd9Sstevel@tonic-gate  * graph_clogin_start() to bring system/console-login online, which defers the
6987c478bd9Sstevel@tonic-gate  * start if sulogin is running.
6997c478bd9Sstevel@tonic-gate  */
7007c478bd9Sstevel@tonic-gate static void
7017c478bd9Sstevel@tonic-gate graph_clogin_start(graph_vertex_t *v)
7027c478bd9Sstevel@tonic-gate {
7037c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	if (sulogin_running)
7067c478bd9Sstevel@tonic-gate 		console_login_ready = B_TRUE;
7077c478bd9Sstevel@tonic-gate 	else
7087c478bd9Sstevel@tonic-gate 		vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
7097c478bd9Sstevel@tonic-gate }
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate static void
7127c478bd9Sstevel@tonic-gate graph_su_start(graph_vertex_t *v)
7137c478bd9Sstevel@tonic-gate {
7147c478bd9Sstevel@tonic-gate 	/*
7157c478bd9Sstevel@tonic-gate 	 * /etc/inittab used to have the initial /sbin/rcS as a 'sysinit'
7167c478bd9Sstevel@tonic-gate 	 * entry with a runlevel of 'S', before jumping to the final
7177c478bd9Sstevel@tonic-gate 	 * target runlevel (as set in initdefault).  We mimic that legacy
7187c478bd9Sstevel@tonic-gate 	 * behavior here.
7197c478bd9Sstevel@tonic-gate 	 */
7207c478bd9Sstevel@tonic-gate 	utmpx_set_runlevel('S', '0', B_FALSE);
7217c478bd9Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate static void
7257c478bd9Sstevel@tonic-gate graph_post_su_online(void)
7267c478bd9Sstevel@tonic-gate {
7277c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('S', 1);
7287c478bd9Sstevel@tonic-gate }
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate static void
7317c478bd9Sstevel@tonic-gate graph_post_su_disable(void)
7327c478bd9Sstevel@tonic-gate {
7337c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('S', 0);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate static void
7377c478bd9Sstevel@tonic-gate graph_post_mu_online(void)
7387c478bd9Sstevel@tonic-gate {
7397c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('2', 1);
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate static void
7437c478bd9Sstevel@tonic-gate graph_post_mu_disable(void)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('2', 0);
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate static void
7497c478bd9Sstevel@tonic-gate graph_post_mus_online(void)
7507c478bd9Sstevel@tonic-gate {
7517c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('3', 1);
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate static void
7557c478bd9Sstevel@tonic-gate graph_post_mus_disable(void)
7567c478bd9Sstevel@tonic-gate {
7577c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('3', 0);
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate static struct special_vertex_info {
7617c478bd9Sstevel@tonic-gate 	const char	*name;
7627c478bd9Sstevel@tonic-gate 	void		(*start_f)(graph_vertex_t *);
7637c478bd9Sstevel@tonic-gate 	void		(*post_online_f)(void);
7647c478bd9Sstevel@tonic-gate 	void		(*post_disable_f)(void);
7657c478bd9Sstevel@tonic-gate } special_vertices[] = {
7667c478bd9Sstevel@tonic-gate 	{ CONSOLE_LOGIN_FMRI, graph_clogin_start, NULL, NULL },
7677c478bd9Sstevel@tonic-gate 	{ SCF_MILESTONE_SINGLE_USER, graph_su_start,
7687c478bd9Sstevel@tonic-gate 	    graph_post_su_online, graph_post_su_disable },
7697c478bd9Sstevel@tonic-gate 	{ SCF_MILESTONE_MULTI_USER, NULL,
7707c478bd9Sstevel@tonic-gate 	    graph_post_mu_online, graph_post_mu_disable },
7717c478bd9Sstevel@tonic-gate 	{ SCF_MILESTONE_MULTI_USER_SERVER, NULL,
7727c478bd9Sstevel@tonic-gate 	    graph_post_mus_online, graph_post_mus_disable },
7737c478bd9Sstevel@tonic-gate 	{ NULL },
7747c478bd9Sstevel@tonic-gate };
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate void
7787c478bd9Sstevel@tonic-gate vertex_send_event(graph_vertex_t *v, restarter_event_type_t e)
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate 	switch (e) {
7817c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADD_INSTANCE:
7827c478bd9Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_UNINIT);
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&st->st_load_lock);
7857c478bd9Sstevel@tonic-gate 		st->st_load_instances++;
7867c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&st->st_load_lock);
7877c478bd9Sstevel@tonic-gate 		break;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ENABLE:
7907c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Enabling %s.\n", v->gv_name);
7917c478bd9Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_UNINIT ||
7927c478bd9Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_DISABLED ||
7937c478bd9Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_MAINT);
7947c478bd9Sstevel@tonic-gate 		break;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_DISABLE:
7977c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
7987c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Disabling %s.\n", v->gv_name);
7997c478bd9Sstevel@tonic-gate 		assert(v->gv_state != RESTARTER_STATE_DISABLED);
8007c478bd9Sstevel@tonic-gate 		break;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_STOP:
8037c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Stopping %s.\n", v->gv_name);
8047c478bd9Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_DEGRADED ||
8057c478bd9Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_ONLINE);
8067c478bd9Sstevel@tonic-gate 		break;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_START:
8097c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Starting %s.\n", v->gv_name);
8107c478bd9Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_OFFLINE);
8117c478bd9Sstevel@tonic-gate 		break;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE:
8147c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_DEGRADED:
8157c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_REFRESH:
8167c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_RESTART:
8177c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF:
8187c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
8197c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE:
8207c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
8217c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
8227c478bd9Sstevel@tonic-gate 		break;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	default:
8257c478bd9Sstevel@tonic-gate #ifndef NDEBUG
8267c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Bad event %d.\n", __FILE__, __LINE__, e);
8277c478bd9Sstevel@tonic-gate #endif
8287c478bd9Sstevel@tonic-gate 		abort();
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	restarter_protocol_send_event(v->gv_name, v->gv_restarter_channel, e);
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate static void
8357c478bd9Sstevel@tonic-gate graph_unset_restarter(graph_vertex_t *v)
8367c478bd9Sstevel@tonic-gate {
8377c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
8387c478bd9Sstevel@tonic-gate 	assert(v->gv_flags & GV_CONFIGURED);
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_REMOVE_INSTANCE);
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	if (v->gv_restarter_id != -1) {
8437c478bd9Sstevel@tonic-gate 		graph_vertex_t *rv;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 		rv = vertex_get_by_id(v->gv_restarter_id);
8467c478bd9Sstevel@tonic-gate 		graph_remove_edge(v, rv);
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	v->gv_restarter_id = -1;
8507c478bd9Sstevel@tonic-gate 	v->gv_restarter_channel = NULL;
8517c478bd9Sstevel@tonic-gate }
8527c478bd9Sstevel@tonic-gate 
8533ad28c1eSrm88369 /*
8543ad28c1eSrm88369  * Return VERTEX_REMOVED when the vertex passed in argument is deleted from the
8553ad28c1eSrm88369  * dgraph otherwise return VERTEX_INUSE.
8563ad28c1eSrm88369  */
8573ad28c1eSrm88369 static int
8583ad28c1eSrm88369 free_if_unrefed(graph_vertex_t *v)
8593ad28c1eSrm88369 {
8603ad28c1eSrm88369 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
8613ad28c1eSrm88369 
8623ad28c1eSrm88369 	if (v->gv_refs > 0)
8633ad28c1eSrm88369 		return (VERTEX_INUSE);
8643ad28c1eSrm88369 
8653ad28c1eSrm88369 	if (v->gv_type == GVT_SVC &&
8663ad28c1eSrm88369 	    uu_list_numnodes(v->gv_dependents) == 0 &&
8673ad28c1eSrm88369 	    uu_list_numnodes(v->gv_dependencies) == 0) {
8683ad28c1eSrm88369 		graph_remove_vertex(v);
8693ad28c1eSrm88369 		return (VERTEX_REMOVED);
8703ad28c1eSrm88369 	} else if (v->gv_type == GVT_INST &&
8713ad28c1eSrm88369 	    (v->gv_flags & GV_CONFIGURED) == 0 &&
8723ad28c1eSrm88369 	    uu_list_numnodes(v->gv_dependents) == 1 &&
8733ad28c1eSrm88369 	    uu_list_numnodes(v->gv_dependencies) == 0) {
8743ad28c1eSrm88369 		remove_inst_vertex(v);
8753ad28c1eSrm88369 		return (VERTEX_REMOVED);
8763ad28c1eSrm88369 	}
8773ad28c1eSrm88369 
8783ad28c1eSrm88369 	return (VERTEX_INUSE);
8793ad28c1eSrm88369 }
8803ad28c1eSrm88369 
8817c478bd9Sstevel@tonic-gate static void
8827c478bd9Sstevel@tonic-gate delete_depgroup(graph_vertex_t *v)
8837c478bd9Sstevel@tonic-gate {
8847c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
8857c478bd9Sstevel@tonic-gate 	graph_vertex_t *dv;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
8887c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_GROUP);
8897c478bd9Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependents) == 0);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	while ((e = uu_list_first(v->gv_dependencies)) != NULL) {
8927c478bd9Sstevel@tonic-gate 		dv = e->ge_vertex;
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 		graph_remove_edge(v, dv);
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 		switch (dv->gv_type) {
8977c478bd9Sstevel@tonic-gate 		case GVT_INST:		/* instance dependency */
8987c478bd9Sstevel@tonic-gate 		case GVT_SVC:		/* service dependency */
8993ad28c1eSrm88369 			(void) free_if_unrefed(dv);
9007c478bd9Sstevel@tonic-gate 			break;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 		case GVT_FILE:		/* file dependency */
9037c478bd9Sstevel@tonic-gate 			assert(uu_list_numnodes(dv->gv_dependencies) == 0);
9047c478bd9Sstevel@tonic-gate 			if (uu_list_numnodes(dv->gv_dependents) == 0)
9057c478bd9Sstevel@tonic-gate 				graph_remove_vertex(dv);
9067c478bd9Sstevel@tonic-gate 			break;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 		default:
9097c478bd9Sstevel@tonic-gate #ifndef NDEBUG
9107c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected node type %d", __FILE__,
9117c478bd9Sstevel@tonic-gate 			    __LINE__, dv->gv_type);
9127c478bd9Sstevel@tonic-gate #endif
9137c478bd9Sstevel@tonic-gate 			abort();
9147c478bd9Sstevel@tonic-gate 		}
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	graph_remove_vertex(v);
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate static int
9217c478bd9Sstevel@tonic-gate delete_instance_deps_cb(graph_edge_t *e, void **ptrs)
9227c478bd9Sstevel@tonic-gate {
9237c478bd9Sstevel@tonic-gate 	graph_vertex_t *v = ptrs[0];
9247c478bd9Sstevel@tonic-gate 	boolean_t delete_restarter_dep = (boolean_t)ptrs[1];
9257c478bd9Sstevel@tonic-gate 	graph_vertex_t *dv;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	dv = e->ge_vertex;
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	/*
9307c478bd9Sstevel@tonic-gate 	 * We have four possibilities here:
9317c478bd9Sstevel@tonic-gate 	 *   - GVT_INST: restarter
9327c478bd9Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_INST: instance dependency
9337c478bd9Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_SVC - GV_INST: service dependency
9347c478bd9Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_FILE: file dependency
9357c478bd9Sstevel@tonic-gate 	 */
9367c478bd9Sstevel@tonic-gate 	switch (dv->gv_type) {
9377c478bd9Sstevel@tonic-gate 	case GVT_INST:	/* restarter */
9387c478bd9Sstevel@tonic-gate 		assert(dv->gv_id == v->gv_restarter_id);
9397c478bd9Sstevel@tonic-gate 		if (delete_restarter_dep)
9407c478bd9Sstevel@tonic-gate 			graph_remove_edge(v, dv);
9417c478bd9Sstevel@tonic-gate 		break;
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	case GVT_GROUP:	/* pg dependency */
9447c478bd9Sstevel@tonic-gate 		graph_remove_edge(v, dv);
9457c478bd9Sstevel@tonic-gate 		delete_depgroup(dv);
9467c478bd9Sstevel@tonic-gate 		break;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	case GVT_FILE:
9497c478bd9Sstevel@tonic-gate 		/* These are currently not direct dependencies */
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	default:
9527c478bd9Sstevel@tonic-gate #ifndef NDEBUG
9537c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Bad vertex type %d.\n", __FILE__, __LINE__,
9547c478bd9Sstevel@tonic-gate 		    dv->gv_type);
9557c478bd9Sstevel@tonic-gate #endif
9567c478bd9Sstevel@tonic-gate 		abort();
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
9607c478bd9Sstevel@tonic-gate }
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate static void
9637c478bd9Sstevel@tonic-gate delete_instance_dependencies(graph_vertex_t *v, boolean_t delete_restarter_dep)
9647c478bd9Sstevel@tonic-gate {
9657c478bd9Sstevel@tonic-gate 	void *ptrs[2];
9667c478bd9Sstevel@tonic-gate 	int r;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
9697c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	ptrs[0] = v;
9727c478bd9Sstevel@tonic-gate 	ptrs[1] = (void *)delete_restarter_dep;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	r = uu_list_walk(v->gv_dependencies,
9757c478bd9Sstevel@tonic-gate 	    (uu_walk_fn_t *)delete_instance_deps_cb, &ptrs, UU_WALK_ROBUST);
9767c478bd9Sstevel@tonic-gate 	assert(r == 0);
9777c478bd9Sstevel@tonic-gate }
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate /*
9807c478bd9Sstevel@tonic-gate  * int graph_insert_vertex_unconfigured()
9817c478bd9Sstevel@tonic-gate  *   Insert a vertex without sending any restarter events. If the vertex
9827c478bd9Sstevel@tonic-gate  *   already exists or creation is successful, return a pointer to it in *vp.
9837c478bd9Sstevel@tonic-gate  *
9847c478bd9Sstevel@tonic-gate  *   If type is not GVT_GROUP, dt can remain unset.
9857c478bd9Sstevel@tonic-gate  *
9867c478bd9Sstevel@tonic-gate  *   Returns 0, EEXIST, or EINVAL if the arguments are invalid (i.e., fmri
9877c478bd9Sstevel@tonic-gate  *   doesn't agree with type, or type doesn't agree with dt).
9887c478bd9Sstevel@tonic-gate  */
9897c478bd9Sstevel@tonic-gate static int
9907c478bd9Sstevel@tonic-gate graph_insert_vertex_unconfigured(const char *fmri, gv_type_t type,
9917c478bd9Sstevel@tonic-gate     depgroup_type_t dt, restarter_error_t rt, graph_vertex_t **vp)
9927c478bd9Sstevel@tonic-gate {
9937c478bd9Sstevel@tonic-gate 	int r;
9947c478bd9Sstevel@tonic-gate 	int i;
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	switch (type) {
9997c478bd9Sstevel@tonic-gate 	case GVT_SVC:
10007c478bd9Sstevel@tonic-gate 	case GVT_INST:
10017c478bd9Sstevel@tonic-gate 		if (strncmp(fmri, "svc:", sizeof ("svc:") - 1) != 0)
10027c478bd9Sstevel@tonic-gate 			return (EINVAL);
10037c478bd9Sstevel@tonic-gate 		break;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	case GVT_FILE:
10067c478bd9Sstevel@tonic-gate 		if (strncmp(fmri, "file:", sizeof ("file:") - 1) != 0)
10077c478bd9Sstevel@tonic-gate 			return (EINVAL);
10087c478bd9Sstevel@tonic-gate 		break;
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	case GVT_GROUP:
10117c478bd9Sstevel@tonic-gate 		if (dt <= 0 || rt < 0)
10127c478bd9Sstevel@tonic-gate 			return (EINVAL);
10137c478bd9Sstevel@tonic-gate 		break;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	default:
10167c478bd9Sstevel@tonic-gate #ifndef NDEBUG
10177c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown type %d.\n", __FILE__, __LINE__, type);
10187c478bd9Sstevel@tonic-gate #endif
10197c478bd9Sstevel@tonic-gate 		abort();
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	*vp = vertex_get_by_name(fmri);
10237c478bd9Sstevel@tonic-gate 	if (*vp != NULL)
10247c478bd9Sstevel@tonic-gate 		return (EEXIST);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	*vp = graph_add_vertex(fmri);
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	(*vp)->gv_type = type;
10297c478bd9Sstevel@tonic-gate 	(*vp)->gv_depgroup = dt;
10307c478bd9Sstevel@tonic-gate 	(*vp)->gv_restart = rt;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	(*vp)->gv_flags = 0;
10337c478bd9Sstevel@tonic-gate 	(*vp)->gv_state = RESTARTER_STATE_NONE;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	for (i = 0; special_vertices[i].name != NULL; ++i) {
10367c478bd9Sstevel@tonic-gate 		if (strcmp(fmri, special_vertices[i].name) == 0) {
10377c478bd9Sstevel@tonic-gate 			(*vp)->gv_start_f = special_vertices[i].start_f;
10387c478bd9Sstevel@tonic-gate 			(*vp)->gv_post_online_f =
10397c478bd9Sstevel@tonic-gate 			    special_vertices[i].post_online_f;
10407c478bd9Sstevel@tonic-gate 			(*vp)->gv_post_disable_f =
10417c478bd9Sstevel@tonic-gate 			    special_vertices[i].post_disable_f;
10427c478bd9Sstevel@tonic-gate 			break;
10437c478bd9Sstevel@tonic-gate 		}
10447c478bd9Sstevel@tonic-gate 	}
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate 	(*vp)->gv_restarter_id = -1;
10477c478bd9Sstevel@tonic-gate 	(*vp)->gv_restarter_channel = 0;
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	if (type == GVT_INST) {
10507c478bd9Sstevel@tonic-gate 		char *sfmri;
10517c478bd9Sstevel@tonic-gate 		graph_vertex_t *sv;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 		sfmri = inst_fmri_to_svc_fmri(fmri);
10547c478bd9Sstevel@tonic-gate 		sv = vertex_get_by_name(sfmri);
10557c478bd9Sstevel@tonic-gate 		if (sv == NULL) {
10567c478bd9Sstevel@tonic-gate 			r = graph_insert_vertex_unconfigured(sfmri, GVT_SVC, 0,
10577c478bd9Sstevel@tonic-gate 			    0, &sv);
10587c478bd9Sstevel@tonic-gate 			assert(r == 0);
10597c478bd9Sstevel@tonic-gate 		}
10607c478bd9Sstevel@tonic-gate 		startd_free(sfmri, max_scf_fmri_size);
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 		graph_add_edge(sv, *vp);
10637c478bd9Sstevel@tonic-gate 	}
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	/*
10667c478bd9Sstevel@tonic-gate 	 * If this vertex is in the subgraph, mark it as so, for both
10677c478bd9Sstevel@tonic-gate 	 * GVT_INST and GVT_SERVICE verteces.
10687c478bd9Sstevel@tonic-gate 	 * A GVT_SERVICE vertex can only be in the subgraph if another instance
10697c478bd9Sstevel@tonic-gate 	 * depends on it, in which case it's already been added to the graph
10707c478bd9Sstevel@tonic-gate 	 * and marked as in the subgraph (by refresh_vertex()).  If a
10717c478bd9Sstevel@tonic-gate 	 * GVT_SERVICE vertex was freshly added (by the code above), it means
10727c478bd9Sstevel@tonic-gate 	 * that it has no dependents, and cannot be in the subgraph.
10737c478bd9Sstevel@tonic-gate 	 * Regardless of this, we still check that gv_flags includes
10747c478bd9Sstevel@tonic-gate 	 * GV_INSUBGRAPH in the event that future behavior causes the above
10757c478bd9Sstevel@tonic-gate 	 * code to add a GVT_SERVICE vertex which should be in the subgraph.
10767c478bd9Sstevel@tonic-gate 	 */
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	(*vp)->gv_flags |= (should_be_in_subgraph(*vp)? GV_INSUBGRAPH : 0);
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	return (0);
10817c478bd9Sstevel@tonic-gate }
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate /*
10847c478bd9Sstevel@tonic-gate  * Returns 0 on success or ELOOP if the dependency would create a cycle.
10857c478bd9Sstevel@tonic-gate  */
10867c478bd9Sstevel@tonic-gate static int
10877c478bd9Sstevel@tonic-gate graph_insert_dependency(graph_vertex_t *fv, graph_vertex_t *tv, int **pathp)
10887c478bd9Sstevel@tonic-gate {
10897c478bd9Sstevel@tonic-gate 	hrtime_t now;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	/* cycle detection */
10947c478bd9Sstevel@tonic-gate 	now = gethrtime();
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	/* Don't follow exclusions. */
10977c478bd9Sstevel@tonic-gate 	if (!(fv->gv_type == GVT_GROUP &&
10987c478bd9Sstevel@tonic-gate 	    fv->gv_depgroup == DEPGRP_EXCLUDE_ALL)) {
10997c478bd9Sstevel@tonic-gate 		*pathp = is_path_to(tv, fv);
11007c478bd9Sstevel@tonic-gate 		if (*pathp)
11017c478bd9Sstevel@tonic-gate 			return (ELOOP);
11027c478bd9Sstevel@tonic-gate 	}
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	dep_cycle_ns += gethrtime() - now;
11057c478bd9Sstevel@tonic-gate 	++dep_inserts;
11067c478bd9Sstevel@tonic-gate 	now = gethrtime();
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	graph_add_edge(fv, tv);
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	dep_insert_ns += gethrtime() - now;
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	/* Check if the dependency adds the "to" vertex to the subgraph */
11137c478bd9Sstevel@tonic-gate 	tv->gv_flags |= (should_be_in_subgraph(tv) ? GV_INSUBGRAPH : 0);
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	return (0);
11167c478bd9Sstevel@tonic-gate }
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate static int
11197c478bd9Sstevel@tonic-gate inst_running(graph_vertex_t *v)
11207c478bd9Sstevel@tonic-gate {
11217c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	if (v->gv_state == RESTARTER_STATE_ONLINE ||
11247c478bd9Sstevel@tonic-gate 	    v->gv_state == RESTARTER_STATE_DEGRADED)
11257c478bd9Sstevel@tonic-gate 		return (1);
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	return (0);
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate /*
11317c478bd9Sstevel@tonic-gate  * The dependency evaluation functions return
11327c478bd9Sstevel@tonic-gate  *   1 - dependency satisfied
11337c478bd9Sstevel@tonic-gate  *   0 - dependency unsatisfied
11347c478bd9Sstevel@tonic-gate  *   -1 - dependency unsatisfiable (without administrator intervention)
11357c478bd9Sstevel@tonic-gate  *
11367c478bd9Sstevel@tonic-gate  * The functions also take a boolean satbility argument.  When true, the
11377c478bd9Sstevel@tonic-gate  * functions may recurse in order to determine satisfiability.
11387c478bd9Sstevel@tonic-gate  */
11397c478bd9Sstevel@tonic-gate static int require_any_satisfied(graph_vertex_t *, boolean_t);
11407c478bd9Sstevel@tonic-gate static int dependency_satisfied(graph_vertex_t *, boolean_t);
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate /*
11437c478bd9Sstevel@tonic-gate  * A require_all dependency is unsatisfied if any elements are unsatisfied.  It
11447c478bd9Sstevel@tonic-gate  * is unsatisfiable if any elements are unsatisfiable.
11457c478bd9Sstevel@tonic-gate  */
11467c478bd9Sstevel@tonic-gate static int
11477c478bd9Sstevel@tonic-gate require_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
11487c478bd9Sstevel@tonic-gate {
11497c478bd9Sstevel@tonic-gate 	graph_edge_t *edge;
11507c478bd9Sstevel@tonic-gate 	int i;
11517c478bd9Sstevel@tonic-gate 	boolean_t any_unsatisfied;
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	if (uu_list_numnodes(groupv->gv_dependencies) == 0)
11547c478bd9Sstevel@tonic-gate 		return (1);
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 	any_unsatisfied = B_FALSE;
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
11597c478bd9Sstevel@tonic-gate 	    edge != NULL;
11607c478bd9Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
11617c478bd9Sstevel@tonic-gate 		i = dependency_satisfied(edge->ge_vertex, satbility);
11627c478bd9Sstevel@tonic-gate 		if (i == 1)
11637c478bd9Sstevel@tonic-gate 			continue;
11647c478bd9Sstevel@tonic-gate 
11653efb42ecSrm88369 		log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
11667c478bd9Sstevel@tonic-gate 		    "require_all(%s): %s is unsatisfi%s.\n", groupv->gv_name,
11677c478bd9Sstevel@tonic-gate 		    edge->ge_vertex->gv_name, i == 0 ? "ed" : "able");
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 		if (!satbility)
11707c478bd9Sstevel@tonic-gate 			return (0);
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 		if (i == -1)
11737c478bd9Sstevel@tonic-gate 			return (-1);
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 		any_unsatisfied = B_TRUE;
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	return (any_unsatisfied ? 0 : 1);
11797c478bd9Sstevel@tonic-gate }
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate /*
11827c478bd9Sstevel@tonic-gate  * A require_any dependency is satisfied if any element is satisfied.  It is
11837c478bd9Sstevel@tonic-gate  * satisfiable if any element is satisfiable.
11847c478bd9Sstevel@tonic-gate  */
11857c478bd9Sstevel@tonic-gate static int
11867c478bd9Sstevel@tonic-gate require_any_satisfied(graph_vertex_t *groupv, boolean_t satbility)
11877c478bd9Sstevel@tonic-gate {
11887c478bd9Sstevel@tonic-gate 	graph_edge_t *edge;
11897c478bd9Sstevel@tonic-gate 	int s;
11907c478bd9Sstevel@tonic-gate 	boolean_t satisfiable;
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	if (uu_list_numnodes(groupv->gv_dependencies) == 0)
11937c478bd9Sstevel@tonic-gate 		return (1);
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	satisfiable = B_FALSE;
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
11987c478bd9Sstevel@tonic-gate 	    edge != NULL;
11997c478bd9Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
12007c478bd9Sstevel@tonic-gate 		s = dependency_satisfied(edge->ge_vertex, satbility);
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 		if (s == 1)
12037c478bd9Sstevel@tonic-gate 			return (1);
12047c478bd9Sstevel@tonic-gate 
12053efb42ecSrm88369 		log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
12067c478bd9Sstevel@tonic-gate 		    "require_any(%s): %s is unsatisfi%s.\n",
12077c478bd9Sstevel@tonic-gate 		    groupv->gv_name, edge->ge_vertex->gv_name,
12087c478bd9Sstevel@tonic-gate 		    s == 0 ? "ed" : "able");
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 		if (satbility && s == 0)
12117c478bd9Sstevel@tonic-gate 			satisfiable = B_TRUE;
12127c478bd9Sstevel@tonic-gate 	}
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	return (!satbility || satisfiable ? 0 : -1);
12157c478bd9Sstevel@tonic-gate }
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate /*
12187c478bd9Sstevel@tonic-gate  * An optional_all dependency only considers elements which are configured,
12197c478bd9Sstevel@tonic-gate  * enabled, and not in maintenance.  If any are unsatisfied, then the dependency
12207c478bd9Sstevel@tonic-gate  * is unsatisfied.
12217c478bd9Sstevel@tonic-gate  *
12227c478bd9Sstevel@tonic-gate  * Offline dependencies which are waiting for a dependency to come online are
12237c478bd9Sstevel@tonic-gate  * unsatisfied.  Offline dependences which cannot possibly come online
12247c478bd9Sstevel@tonic-gate  * (unsatisfiable) are always considered satisfied.
12257c478bd9Sstevel@tonic-gate  */
12267c478bd9Sstevel@tonic-gate static int
12277c478bd9Sstevel@tonic-gate optional_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
12287c478bd9Sstevel@tonic-gate {
12297c478bd9Sstevel@tonic-gate 	graph_edge_t *edge;
12307c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
12317c478bd9Sstevel@tonic-gate 	boolean_t any_qualified;
12327c478bd9Sstevel@tonic-gate 	boolean_t any_unsatisfied;
12337c478bd9Sstevel@tonic-gate 	int i;
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	any_qualified = B_FALSE;
12367c478bd9Sstevel@tonic-gate 	any_unsatisfied = B_FALSE;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
12397c478bd9Sstevel@tonic-gate 	    edge != NULL;
12407c478bd9Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
12417c478bd9Sstevel@tonic-gate 		v = edge->ge_vertex;
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 		switch (v->gv_type) {
12447c478bd9Sstevel@tonic-gate 		case GVT_INST:
12457c478bd9Sstevel@tonic-gate 			/* Skip missing or disabled instances */
12467c478bd9Sstevel@tonic-gate 			if ((v->gv_flags & (GV_CONFIGURED | GV_ENABLED)) !=
12477c478bd9Sstevel@tonic-gate 			    (GV_CONFIGURED | GV_ENABLED))
12487c478bd9Sstevel@tonic-gate 				continue;
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 			if (v->gv_state == RESTARTER_STATE_MAINT)
12517c478bd9Sstevel@tonic-gate 				continue;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 			any_qualified = B_TRUE;
12547c478bd9Sstevel@tonic-gate 			if (v->gv_state == RESTARTER_STATE_OFFLINE) {
12557c478bd9Sstevel@tonic-gate 				/*
12567c478bd9Sstevel@tonic-gate 				 * For offline dependencies, treat unsatisfiable
12577c478bd9Sstevel@tonic-gate 				 * as satisfied.
12587c478bd9Sstevel@tonic-gate 				 */
12597c478bd9Sstevel@tonic-gate 				i = dependency_satisfied(v, B_TRUE);
12607c478bd9Sstevel@tonic-gate 				if (i == -1)
12617c478bd9Sstevel@tonic-gate 					i = 1;
12627c478bd9Sstevel@tonic-gate 			} else if (v->gv_state == RESTARTER_STATE_DISABLED) {
12637c478bd9Sstevel@tonic-gate 				/*
12647c478bd9Sstevel@tonic-gate 				 * The service is enabled, but hasn't
12657c478bd9Sstevel@tonic-gate 				 * transitioned out of disabled yet.  Treat it
12667c478bd9Sstevel@tonic-gate 				 * as unsatisfied (not unsatisfiable).
12677c478bd9Sstevel@tonic-gate 				 */
12687c478bd9Sstevel@tonic-gate 				i = 0;
12697c478bd9Sstevel@tonic-gate 			} else {
12707c478bd9Sstevel@tonic-gate 				i = dependency_satisfied(v, satbility);
12717c478bd9Sstevel@tonic-gate 			}
12727c478bd9Sstevel@tonic-gate 			break;
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 		case GVT_FILE:
12757c478bd9Sstevel@tonic-gate 			any_qualified = B_TRUE;
12767c478bd9Sstevel@tonic-gate 			i = dependency_satisfied(v, satbility);
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 			break;
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 		case GVT_SVC: {
12817c478bd9Sstevel@tonic-gate 			boolean_t svc_any_qualified;
12827c478bd9Sstevel@tonic-gate 			boolean_t svc_satisfied;
12837c478bd9Sstevel@tonic-gate 			boolean_t svc_satisfiable;
12847c478bd9Sstevel@tonic-gate 			graph_vertex_t *v2;
12857c478bd9Sstevel@tonic-gate 			graph_edge_t *e2;
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 			svc_any_qualified = B_FALSE;
12887c478bd9Sstevel@tonic-gate 			svc_satisfied = B_FALSE;
12897c478bd9Sstevel@tonic-gate 			svc_satisfiable = B_FALSE;
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 			for (e2 = uu_list_first(v->gv_dependencies);
12927c478bd9Sstevel@tonic-gate 			    e2 != NULL;
12937c478bd9Sstevel@tonic-gate 			    e2 = uu_list_next(v->gv_dependencies, e2)) {
12947c478bd9Sstevel@tonic-gate 				v2 = e2->ge_vertex;
12957c478bd9Sstevel@tonic-gate 				assert(v2->gv_type == GVT_INST);
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 				if ((v2->gv_flags &
12987c478bd9Sstevel@tonic-gate 				    (GV_CONFIGURED | GV_ENABLED)) !=
12997c478bd9Sstevel@tonic-gate 				    (GV_CONFIGURED | GV_ENABLED))
13007c478bd9Sstevel@tonic-gate 					continue;
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 				if (v2->gv_state == RESTARTER_STATE_MAINT)
13037c478bd9Sstevel@tonic-gate 					continue;
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 				svc_any_qualified = B_TRUE;
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 				if (v2->gv_state == RESTARTER_STATE_OFFLINE) {
13087c478bd9Sstevel@tonic-gate 					/*
13097c478bd9Sstevel@tonic-gate 					 * For offline dependencies, treat
13107c478bd9Sstevel@tonic-gate 					 * unsatisfiable as satisfied.
13117c478bd9Sstevel@tonic-gate 					 */
13127c478bd9Sstevel@tonic-gate 					i = dependency_satisfied(v2, B_TRUE);
13137c478bd9Sstevel@tonic-gate 					if (i == -1)
13147c478bd9Sstevel@tonic-gate 						i = 1;
13157c478bd9Sstevel@tonic-gate 				} else if (v2->gv_state ==
13167c478bd9Sstevel@tonic-gate 				    RESTARTER_STATE_DISABLED) {
13177c478bd9Sstevel@tonic-gate 					i = 0;
13187c478bd9Sstevel@tonic-gate 				} else {
13197c478bd9Sstevel@tonic-gate 					i = dependency_satisfied(v2, satbility);
13207c478bd9Sstevel@tonic-gate 				}
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 				if (i == 1) {
13237c478bd9Sstevel@tonic-gate 					svc_satisfied = B_TRUE;
13247c478bd9Sstevel@tonic-gate 					break;
13257c478bd9Sstevel@tonic-gate 				}
13267c478bd9Sstevel@tonic-gate 				if (i == 0)
13277c478bd9Sstevel@tonic-gate 					svc_satisfiable = B_TRUE;
13287c478bd9Sstevel@tonic-gate 			}
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 			if (!svc_any_qualified)
13317c478bd9Sstevel@tonic-gate 				continue;
13327c478bd9Sstevel@tonic-gate 			any_qualified = B_TRUE;
13337c478bd9Sstevel@tonic-gate 			if (svc_satisfied) {
13347c478bd9Sstevel@tonic-gate 				i = 1;
13357c478bd9Sstevel@tonic-gate 			} else if (svc_satisfiable) {
13367c478bd9Sstevel@tonic-gate 				i = 0;
13377c478bd9Sstevel@tonic-gate 			} else {
13387c478bd9Sstevel@tonic-gate 				i = -1;
13397c478bd9Sstevel@tonic-gate 			}
13407c478bd9Sstevel@tonic-gate 			break;
13417c478bd9Sstevel@tonic-gate 		}
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 		case GVT_GROUP:
13447c478bd9Sstevel@tonic-gate 		default:
13457c478bd9Sstevel@tonic-gate #ifndef NDEBUG
13467c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
13477c478bd9Sstevel@tonic-gate 			    __LINE__, v->gv_type);
13487c478bd9Sstevel@tonic-gate #endif
13497c478bd9Sstevel@tonic-gate 			abort();
13507c478bd9Sstevel@tonic-gate 		}
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 		if (i == 1)
13537c478bd9Sstevel@tonic-gate 			continue;
13547c478bd9Sstevel@tonic-gate 
13553efb42ecSrm88369 		log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
13567c478bd9Sstevel@tonic-gate 		    "optional_all(%s): %s is unsatisfi%s.\n", groupv->gv_name,
13577c478bd9Sstevel@tonic-gate 		    v->gv_name, i == 0 ? "ed" : "able");
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 		if (!satbility)
13607c478bd9Sstevel@tonic-gate 			return (0);
13617c478bd9Sstevel@tonic-gate 		if (i == -1)
13627c478bd9Sstevel@tonic-gate 			return (-1);
13637c478bd9Sstevel@tonic-gate 		any_unsatisfied = B_TRUE;
13647c478bd9Sstevel@tonic-gate 	}
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	if (!any_qualified)
13677c478bd9Sstevel@tonic-gate 		return (1);
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 	return (any_unsatisfied ? 0 : 1);
13707c478bd9Sstevel@tonic-gate }
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate /*
13737c478bd9Sstevel@tonic-gate  * An exclude_all dependency is unsatisfied if any non-service element is
13747c478bd9Sstevel@tonic-gate  * satisfied or any service instance which is configured, enabled, and not in
13757c478bd9Sstevel@tonic-gate  * maintenance is satisfied.  Usually when unsatisfied, it is also
13767c478bd9Sstevel@tonic-gate  * unsatisfiable.
13777c478bd9Sstevel@tonic-gate  */
13787c478bd9Sstevel@tonic-gate #define	LOG_EXCLUDE(u, v)						\
13793efb42ecSrm88369 	log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,			\
13803efb42ecSrm88369 	    "exclude_all(%s): %s is satisfied.\n",			\
13817c478bd9Sstevel@tonic-gate 	    (u)->gv_name, (v)->gv_name)
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate /* ARGSUSED */
13847c478bd9Sstevel@tonic-gate static int
13857c478bd9Sstevel@tonic-gate exclude_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
13867c478bd9Sstevel@tonic-gate {
13877c478bd9Sstevel@tonic-gate 	graph_edge_t *edge, *e2;
13887c478bd9Sstevel@tonic-gate 	graph_vertex_t *v, *v2;
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
13917c478bd9Sstevel@tonic-gate 	    edge != NULL;
13927c478bd9Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
13937c478bd9Sstevel@tonic-gate 		v = edge->ge_vertex;
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 		switch (v->gv_type) {
13967c478bd9Sstevel@tonic-gate 		case GVT_INST:
13977c478bd9Sstevel@tonic-gate 			if ((v->gv_flags & GV_CONFIGURED) == 0)
13987c478bd9Sstevel@tonic-gate 				continue;
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 			switch (v->gv_state) {
14017c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_ONLINE:
14027c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_DEGRADED:
14037c478bd9Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v);
14047c478bd9Sstevel@tonic-gate 				return (v->gv_flags & GV_ENABLED ? -1 : 0);
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_OFFLINE:
14077c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_UNINIT:
14087c478bd9Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v);
14097c478bd9Sstevel@tonic-gate 				return (0);
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_DISABLED:
14127c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_MAINT:
14137c478bd9Sstevel@tonic-gate 				continue;
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 			default:
14167c478bd9Sstevel@tonic-gate #ifndef NDEBUG
14177c478bd9Sstevel@tonic-gate 				uu_warn("%s:%d: Unexpected vertex state %d.\n",
14187c478bd9Sstevel@tonic-gate 				    __FILE__, __LINE__, v->gv_state);
14197c478bd9Sstevel@tonic-gate #endif
14207c478bd9Sstevel@tonic-gate 				abort();
14217c478bd9Sstevel@tonic-gate 			}
14227c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 		case GVT_SVC:
14257c478bd9Sstevel@tonic-gate 			break;
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 		case GVT_FILE:
14287c478bd9Sstevel@tonic-gate 			if (!file_ready(v))
14297c478bd9Sstevel@tonic-gate 				continue;
14307c478bd9Sstevel@tonic-gate 			LOG_EXCLUDE(groupv, v);
14317c478bd9Sstevel@tonic-gate 			return (-1);
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 		case GVT_GROUP:
14347c478bd9Sstevel@tonic-gate 		default:
14357c478bd9Sstevel@tonic-gate #ifndef NDEBUG
14367c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
14377c478bd9Sstevel@tonic-gate 			    __LINE__, v->gv_type);
14387c478bd9Sstevel@tonic-gate #endif
14397c478bd9Sstevel@tonic-gate 			abort();
14407c478bd9Sstevel@tonic-gate 		}
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 		/* v represents a service */
14437c478bd9Sstevel@tonic-gate 		if (uu_list_numnodes(v->gv_dependencies) == 0)
14447c478bd9Sstevel@tonic-gate 			continue;
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 		for (e2 = uu_list_first(v->gv_dependencies);
14477c478bd9Sstevel@tonic-gate 		    e2 != NULL;
14487c478bd9Sstevel@tonic-gate 		    e2 = uu_list_next(v->gv_dependencies, e2)) {
14497c478bd9Sstevel@tonic-gate 			v2 = e2->ge_vertex;
14507c478bd9Sstevel@tonic-gate 			assert(v2->gv_type == GVT_INST);
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 			if ((v2->gv_flags & GV_CONFIGURED) == 0)
14537c478bd9Sstevel@tonic-gate 				continue;
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 			switch (v2->gv_state) {
14567c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_ONLINE:
14577c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_DEGRADED:
14587c478bd9Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v2);
14597c478bd9Sstevel@tonic-gate 				return (v2->gv_flags & GV_ENABLED ? -1 : 0);
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_OFFLINE:
14627c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_UNINIT:
14637c478bd9Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v2);
14647c478bd9Sstevel@tonic-gate 				return (0);
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_DISABLED:
14677c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_MAINT:
14687c478bd9Sstevel@tonic-gate 				continue;
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 			default:
14717c478bd9Sstevel@tonic-gate #ifndef NDEBUG
14727c478bd9Sstevel@tonic-gate 				uu_warn("%s:%d: Unexpected vertex type %d.\n",
14737c478bd9Sstevel@tonic-gate 				    __FILE__, __LINE__, v2->gv_type);
14747c478bd9Sstevel@tonic-gate #endif
14757c478bd9Sstevel@tonic-gate 				abort();
14767c478bd9Sstevel@tonic-gate 			}
14777c478bd9Sstevel@tonic-gate 		}
14787c478bd9Sstevel@tonic-gate 	}
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	return (1);
14817c478bd9Sstevel@tonic-gate }
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate /*
14847c478bd9Sstevel@tonic-gate  * int instance_satisfied()
14857c478bd9Sstevel@tonic-gate  *   Determine if all the dependencies are satisfied for the supplied instance
14867c478bd9Sstevel@tonic-gate  *   vertex. Return 1 if they are, 0 if they aren't, and -1 if they won't be
14877c478bd9Sstevel@tonic-gate  *   without administrator intervention.
14887c478bd9Sstevel@tonic-gate  */
14897c478bd9Sstevel@tonic-gate static int
14907c478bd9Sstevel@tonic-gate instance_satisfied(graph_vertex_t *v, boolean_t satbility)
14917c478bd9Sstevel@tonic-gate {
14927c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
14937c478bd9Sstevel@tonic-gate 	assert(!inst_running(v));
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	return (require_all_satisfied(v, satbility));
14967c478bd9Sstevel@tonic-gate }
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate /*
14997c478bd9Sstevel@tonic-gate  * Decide whether v can satisfy a dependency.  v can either be a child of
15007c478bd9Sstevel@tonic-gate  * a group vertex, or of an instance vertex.
15017c478bd9Sstevel@tonic-gate  */
15027c478bd9Sstevel@tonic-gate static int
15037c478bd9Sstevel@tonic-gate dependency_satisfied(graph_vertex_t *v, boolean_t satbility)
15047c478bd9Sstevel@tonic-gate {
15057c478bd9Sstevel@tonic-gate 	switch (v->gv_type) {
15067c478bd9Sstevel@tonic-gate 	case GVT_INST:
1507*70cbfe41SPhilippe Jung 		if ((v->gv_flags & GV_CONFIGURED) == 0) {
1508*70cbfe41SPhilippe Jung 			if (v->gv_flags & GV_DEATHROW) {
1509*70cbfe41SPhilippe Jung 				/*
1510*70cbfe41SPhilippe Jung 				 * A dependency on an instance with GV_DEATHROW
1511*70cbfe41SPhilippe Jung 				 * flag is always considered as satisfied.
1512*70cbfe41SPhilippe Jung 				 */
1513*70cbfe41SPhilippe Jung 				return (1);
1514*70cbfe41SPhilippe Jung 			}
15157c478bd9Sstevel@tonic-gate 			return (-1);
1516*70cbfe41SPhilippe Jung 		}
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 		switch (v->gv_state) {
15197c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_ONLINE:
15207c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_DEGRADED:
15217c478bd9Sstevel@tonic-gate 			return (1);
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_OFFLINE:
15247c478bd9Sstevel@tonic-gate 			if (!satbility)
15257c478bd9Sstevel@tonic-gate 				return (0);
15267c478bd9Sstevel@tonic-gate 			return (instance_satisfied(v, satbility) != -1 ?
15277c478bd9Sstevel@tonic-gate 			    0 : -1);
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_DISABLED:
15307c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_MAINT:
15317c478bd9Sstevel@tonic-gate 			return (-1);
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_UNINIT:
15347c478bd9Sstevel@tonic-gate 			return (0);
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 		default:
15377c478bd9Sstevel@tonic-gate #ifndef NDEBUG
15387c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex state %d.\n",
15397c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__, v->gv_state);
15407c478bd9Sstevel@tonic-gate #endif
15417c478bd9Sstevel@tonic-gate 			abort();
15427c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
15437c478bd9Sstevel@tonic-gate 		}
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	case GVT_SVC:
15467c478bd9Sstevel@tonic-gate 		if (uu_list_numnodes(v->gv_dependencies) == 0)
15477c478bd9Sstevel@tonic-gate 			return (-1);
15487c478bd9Sstevel@tonic-gate 		return (require_any_satisfied(v, satbility));
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 	case GVT_FILE:
15517c478bd9Sstevel@tonic-gate 		/* i.e., we assume files will not be automatically generated */
15527c478bd9Sstevel@tonic-gate 		return (file_ready(v) ? 1 : -1);
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	case GVT_GROUP:
15557c478bd9Sstevel@tonic-gate 		break;
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	default:
15587c478bd9Sstevel@tonic-gate #ifndef NDEBUG
15597c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unexpected node type %d.\n", __FILE__, __LINE__,
15607c478bd9Sstevel@tonic-gate 		    v->gv_type);
15617c478bd9Sstevel@tonic-gate #endif
15627c478bd9Sstevel@tonic-gate 		abort();
15637c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
15647c478bd9Sstevel@tonic-gate 	}
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	switch (v->gv_depgroup) {
15677c478bd9Sstevel@tonic-gate 	case DEPGRP_REQUIRE_ANY:
15687c478bd9Sstevel@tonic-gate 		return (require_any_satisfied(v, satbility));
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate 	case DEPGRP_REQUIRE_ALL:
15717c478bd9Sstevel@tonic-gate 		return (require_all_satisfied(v, satbility));
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	case DEPGRP_OPTIONAL_ALL:
15747c478bd9Sstevel@tonic-gate 		return (optional_all_satisfied(v, satbility));
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 	case DEPGRP_EXCLUDE_ALL:
15777c478bd9Sstevel@tonic-gate 		return (exclude_all_satisfied(v, satbility));
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	default:
15807c478bd9Sstevel@tonic-gate #ifndef NDEBUG
15817c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown dependency grouping %d.\n", __FILE__,
15827c478bd9Sstevel@tonic-gate 		    __LINE__, v->gv_depgroup);
15837c478bd9Sstevel@tonic-gate #endif
15847c478bd9Sstevel@tonic-gate 		abort();
15857c478bd9Sstevel@tonic-gate 	}
15867c478bd9Sstevel@tonic-gate }
15877c478bd9Sstevel@tonic-gate 
158899b44c3bSlianep void
158999b44c3bSlianep graph_start_if_satisfied(graph_vertex_t *v)
15907c478bd9Sstevel@tonic-gate {
15917c478bd9Sstevel@tonic-gate 	if (v->gv_state == RESTARTER_STATE_OFFLINE &&
15927c478bd9Sstevel@tonic-gate 	    instance_satisfied(v, B_FALSE) == 1) {
15937c478bd9Sstevel@tonic-gate 		if (v->gv_start_f == NULL)
15947c478bd9Sstevel@tonic-gate 			vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
15957c478bd9Sstevel@tonic-gate 		else
15967c478bd9Sstevel@tonic-gate 			v->gv_start_f(v);
15977c478bd9Sstevel@tonic-gate 	}
15987c478bd9Sstevel@tonic-gate }
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate /*
16017c478bd9Sstevel@tonic-gate  * propagate_satbility()
16027c478bd9Sstevel@tonic-gate  *
16037c478bd9Sstevel@tonic-gate  * This function is used when the given vertex changes state in such a way that
16047c478bd9Sstevel@tonic-gate  * one of its dependents may become unsatisfiable.  This happens when an
16057c478bd9Sstevel@tonic-gate  * instance transitions between offline -> online, or from !running ->
16067c478bd9Sstevel@tonic-gate  * maintenance, as well as when an instance is removed from the graph.
16077c478bd9Sstevel@tonic-gate  *
1608cd3bce3eSlianep  * We have to walk all the dependents, since optional_all dependencies several
16097c478bd9Sstevel@tonic-gate  * levels up could become (un)satisfied, instead of unsatisfiable.  For example,
16107c478bd9Sstevel@tonic-gate  *
16117c478bd9Sstevel@tonic-gate  *	+-----+  optional_all  +-----+  require_all  +-----+
16127c478bd9Sstevel@tonic-gate  *	|  A  |--------------->|  B  |-------------->|  C  |
16137c478bd9Sstevel@tonic-gate  *	+-----+                +-----+               +-----+
16147c478bd9Sstevel@tonic-gate  *
16157c478bd9Sstevel@tonic-gate  *	                                        offline -> maintenance
16167c478bd9Sstevel@tonic-gate  *
16177c478bd9Sstevel@tonic-gate  * If C goes into maintenance, it's not enough simply to check B.  Because A has
16187c478bd9Sstevel@tonic-gate  * an optional dependency, what was previously an unsatisfiable situation is now
16197c478bd9Sstevel@tonic-gate  * satisfied (B will never come online, even though its state hasn't changed).
16207c478bd9Sstevel@tonic-gate  *
16217c478bd9Sstevel@tonic-gate  * Note that it's not necessary to continue examining dependents after reaching
16227c478bd9Sstevel@tonic-gate  * an optional_all dependency.  It's not possible for an optional_all dependency
16237c478bd9Sstevel@tonic-gate  * to change satisfiability without also coming online, in which case we get a
16247c478bd9Sstevel@tonic-gate  * start event and propagation continues naturally.  However, it does no harm to
16257c478bd9Sstevel@tonic-gate  * continue propagating satisfiability (as it is a relatively rare event), and
16267c478bd9Sstevel@tonic-gate  * keeps the walker code simple and generic.
16277c478bd9Sstevel@tonic-gate  */
16287c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16297c478bd9Sstevel@tonic-gate static int
16307c478bd9Sstevel@tonic-gate satbility_cb(graph_vertex_t *v, void *arg)
16317c478bd9Sstevel@tonic-gate {
16327c478bd9Sstevel@tonic-gate 	if (v->gv_type == GVT_INST)
163399b44c3bSlianep 		graph_start_if_satisfied(v);
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
16367c478bd9Sstevel@tonic-gate }
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate static void
16397c478bd9Sstevel@tonic-gate propagate_satbility(graph_vertex_t *v)
16407c478bd9Sstevel@tonic-gate {
16417c478bd9Sstevel@tonic-gate 	graph_walk(v, WALK_DEPENDENTS, satbility_cb, NULL, NULL);
16427c478bd9Sstevel@tonic-gate }
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate static void propagate_stop(graph_vertex_t *, void *);
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate /* ARGSUSED */
16477c478bd9Sstevel@tonic-gate static void
16487c478bd9Sstevel@tonic-gate propagate_start(graph_vertex_t *v, void *arg)
16497c478bd9Sstevel@tonic-gate {
16507c478bd9Sstevel@tonic-gate 	switch (v->gv_type) {
16517c478bd9Sstevel@tonic-gate 	case GVT_INST:
165299b44c3bSlianep 		graph_start_if_satisfied(v);
16537c478bd9Sstevel@tonic-gate 		break;
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	case GVT_GROUP:
16567c478bd9Sstevel@tonic-gate 		if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
16577c478bd9Sstevel@tonic-gate 			graph_walk_dependents(v, propagate_stop,
16587c478bd9Sstevel@tonic-gate 			    (void *)RERR_RESTART);
16597c478bd9Sstevel@tonic-gate 			break;
16607c478bd9Sstevel@tonic-gate 		}
16617c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	case GVT_SVC:
16647c478bd9Sstevel@tonic-gate 		graph_walk_dependents(v, propagate_start, NULL);
16657c478bd9Sstevel@tonic-gate 		break;
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	case GVT_FILE:
16687c478bd9Sstevel@tonic-gate #ifndef NDEBUG
16697c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: propagate_start() encountered GVT_FILE.\n",
16707c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__);
16717c478bd9Sstevel@tonic-gate #endif
16727c478bd9Sstevel@tonic-gate 		abort();
16737c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 	default:
16767c478bd9Sstevel@tonic-gate #ifndef NDEBUG
16777c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__,
16787c478bd9Sstevel@tonic-gate 		    v->gv_type);
16797c478bd9Sstevel@tonic-gate #endif
16807c478bd9Sstevel@tonic-gate 		abort();
16817c478bd9Sstevel@tonic-gate 	}
16827c478bd9Sstevel@tonic-gate }
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate static void
16857c478bd9Sstevel@tonic-gate propagate_stop(graph_vertex_t *v, void *arg)
16867c478bd9Sstevel@tonic-gate {
16877c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
16887c478bd9Sstevel@tonic-gate 	graph_vertex_t *svc;
16897c478bd9Sstevel@tonic-gate 	restarter_error_t err = (restarter_error_t)arg;
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 	switch (v->gv_type) {
16927c478bd9Sstevel@tonic-gate 	case GVT_INST:
16937c478bd9Sstevel@tonic-gate 		/* Restarter */
16947c478bd9Sstevel@tonic-gate 		if (err > RERR_NONE && inst_running(v))
16957c478bd9Sstevel@tonic-gate 			vertex_send_event(v, RESTARTER_EVENT_TYPE_STOP);
16967c478bd9Sstevel@tonic-gate 		break;
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	case GVT_SVC:
16997c478bd9Sstevel@tonic-gate 		graph_walk_dependents(v, propagate_stop, arg);
17007c478bd9Sstevel@tonic-gate 		break;
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	case GVT_FILE:
17037c478bd9Sstevel@tonic-gate #ifndef NDEBUG
17047c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: propagate_stop() encountered GVT_FILE.\n",
17057c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__);
17067c478bd9Sstevel@tonic-gate #endif
17077c478bd9Sstevel@tonic-gate 		abort();
17087c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	case GVT_GROUP:
17117c478bd9Sstevel@tonic-gate 		if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
17127c478bd9Sstevel@tonic-gate 			graph_walk_dependents(v, propagate_start, NULL);
17137c478bd9Sstevel@tonic-gate 			break;
17147c478bd9Sstevel@tonic-gate 		}
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 		if (err == RERR_NONE || err > v->gv_restart)
17177c478bd9Sstevel@tonic-gate 			break;
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 		assert(uu_list_numnodes(v->gv_dependents) == 1);
17207c478bd9Sstevel@tonic-gate 		e = uu_list_first(v->gv_dependents);
17217c478bd9Sstevel@tonic-gate 		svc = e->ge_vertex;
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 		if (inst_running(svc))
17247c478bd9Sstevel@tonic-gate 			vertex_send_event(svc, RESTARTER_EVENT_TYPE_STOP);
17257c478bd9Sstevel@tonic-gate 		break;
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	default:
17287c478bd9Sstevel@tonic-gate #ifndef NDEBUG
17297c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__,
17307c478bd9Sstevel@tonic-gate 		    v->gv_type);
17317c478bd9Sstevel@tonic-gate #endif
17327c478bd9Sstevel@tonic-gate 		abort();
17337c478bd9Sstevel@tonic-gate 	}
17347c478bd9Sstevel@tonic-gate }
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate /*
17377c478bd9Sstevel@tonic-gate  * void graph_enable_by_vertex()
17387c478bd9Sstevel@tonic-gate  *   If admin is non-zero, this is an administrative request for change
17397c478bd9Sstevel@tonic-gate  *   of the enabled property.  Thus, send the ADMIN_DISABLE rather than
17407c478bd9Sstevel@tonic-gate  *   a plain DISABLE restarter event.
17417c478bd9Sstevel@tonic-gate  */
174299b44c3bSlianep void
17437c478bd9Sstevel@tonic-gate graph_enable_by_vertex(graph_vertex_t *vertex, int enable, int admin)
17447c478bd9Sstevel@tonic-gate {
17457c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
17467c478bd9Sstevel@tonic-gate 	assert((vertex->gv_flags & GV_CONFIGURED));
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	vertex->gv_flags = (vertex->gv_flags & ~GV_ENABLED) |
17497c478bd9Sstevel@tonic-gate 	    (enable ? GV_ENABLED : 0);
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	if (enable) {
17527c478bd9Sstevel@tonic-gate 		if (vertex->gv_state != RESTARTER_STATE_OFFLINE &&
17537c478bd9Sstevel@tonic-gate 		    vertex->gv_state != RESTARTER_STATE_DEGRADED &&
17547c478bd9Sstevel@tonic-gate 		    vertex->gv_state != RESTARTER_STATE_ONLINE)
17557c478bd9Sstevel@tonic-gate 			vertex_send_event(vertex, RESTARTER_EVENT_TYPE_ENABLE);
17567c478bd9Sstevel@tonic-gate 	} else {
17577c478bd9Sstevel@tonic-gate 		if (vertex->gv_state != RESTARTER_STATE_DISABLED) {
17587c478bd9Sstevel@tonic-gate 			if (admin)
17597c478bd9Sstevel@tonic-gate 				vertex_send_event(vertex,
17607c478bd9Sstevel@tonic-gate 				    RESTARTER_EVENT_TYPE_ADMIN_DISABLE);
17617c478bd9Sstevel@tonic-gate 			else
17627c478bd9Sstevel@tonic-gate 				vertex_send_event(vertex,
17637c478bd9Sstevel@tonic-gate 				    RESTARTER_EVENT_TYPE_DISABLE);
17647c478bd9Sstevel@tonic-gate 		}
17657c478bd9Sstevel@tonic-gate 	}
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	/*
17687c478bd9Sstevel@tonic-gate 	 * Wait for state update from restarter before sending _START or
17697c478bd9Sstevel@tonic-gate 	 * _STOP.
17707c478bd9Sstevel@tonic-gate 	 */
17717c478bd9Sstevel@tonic-gate }
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate static int configure_vertex(graph_vertex_t *, scf_instance_t *);
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate /*
17767c478bd9Sstevel@tonic-gate  * Set the restarter for v to fmri_arg.  That is, make sure a vertex for
17777c478bd9Sstevel@tonic-gate  * fmri_arg exists, make v depend on it, and send _ADD_INSTANCE for v.  If
17787c478bd9Sstevel@tonic-gate  * v is already configured and fmri_arg indicates the current restarter, do
17797c478bd9Sstevel@tonic-gate  * nothing.  If v is configured and fmri_arg is a new restarter, delete v's
17807c478bd9Sstevel@tonic-gate  * dependency on the restarter, send _REMOVE_INSTANCE for v, and set the new
17817c478bd9Sstevel@tonic-gate  * restarter.  Returns 0 on success, EINVAL if the FMRI is invalid,
17827c478bd9Sstevel@tonic-gate  * ECONNABORTED if the repository connection is broken, and ELOOP
17837c478bd9Sstevel@tonic-gate  * if the dependency would create a cycle.  In the last case, *pathp will
17847c478bd9Sstevel@tonic-gate  * point to a -1-terminated array of ids which compose the path from v to
17857c478bd9Sstevel@tonic-gate  * restarter_fmri.
17867c478bd9Sstevel@tonic-gate  */
17877c478bd9Sstevel@tonic-gate int
17887c478bd9Sstevel@tonic-gate graph_change_restarter(graph_vertex_t *v, const char *fmri_arg, scf_handle_t *h,
17897c478bd9Sstevel@tonic-gate     int **pathp)
17907c478bd9Sstevel@tonic-gate {
17917c478bd9Sstevel@tonic-gate 	char *restarter_fmri = NULL;
17927c478bd9Sstevel@tonic-gate 	graph_vertex_t *rv;
17937c478bd9Sstevel@tonic-gate 	int err;
17947c478bd9Sstevel@tonic-gate 	int id;
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 	if (fmri_arg[0] != '\0') {
17997c478bd9Sstevel@tonic-gate 		err = fmri_canonify(fmri_arg, &restarter_fmri, B_TRUE);
18007c478bd9Sstevel@tonic-gate 		if (err != 0) {
18017c478bd9Sstevel@tonic-gate 			assert(err == EINVAL);
18027c478bd9Sstevel@tonic-gate 			return (err);
18037c478bd9Sstevel@tonic-gate 		}
18047c478bd9Sstevel@tonic-gate 	}
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	if (restarter_fmri == NULL ||
18077c478bd9Sstevel@tonic-gate 	    strcmp(restarter_fmri, SCF_SERVICE_STARTD) == 0) {
18087c478bd9Sstevel@tonic-gate 		if (v->gv_flags & GV_CONFIGURED) {
18097c478bd9Sstevel@tonic-gate 			if (v->gv_restarter_id == -1) {
18107c478bd9Sstevel@tonic-gate 				if (restarter_fmri != NULL)
18117c478bd9Sstevel@tonic-gate 					startd_free(restarter_fmri,
18127c478bd9Sstevel@tonic-gate 					    max_scf_fmri_size);
18137c478bd9Sstevel@tonic-gate 				return (0);
18147c478bd9Sstevel@tonic-gate 			}
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 			graph_unset_restarter(v);
18177c478bd9Sstevel@tonic-gate 		}
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 		/* Master restarter, nothing to do. */
18207c478bd9Sstevel@tonic-gate 		v->gv_restarter_id = -1;
18217c478bd9Sstevel@tonic-gate 		v->gv_restarter_channel = NULL;
18227c478bd9Sstevel@tonic-gate 		vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE);
18237c478bd9Sstevel@tonic-gate 		return (0);
18247c478bd9Sstevel@tonic-gate 	}
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	if (v->gv_flags & GV_CONFIGURED) {
18277c478bd9Sstevel@tonic-gate 		id = dict_lookup_byname(restarter_fmri);
18287c478bd9Sstevel@tonic-gate 		if (id != -1 && v->gv_restarter_id == id) {
18297c478bd9Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_fmri_size);
18307c478bd9Sstevel@tonic-gate 			return (0);
18317c478bd9Sstevel@tonic-gate 		}
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 		graph_unset_restarter(v);
18347c478bd9Sstevel@tonic-gate 	}
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 	err = graph_insert_vertex_unconfigured(restarter_fmri, GVT_INST, 0,
18377c478bd9Sstevel@tonic-gate 	    RERR_NONE, &rv);
18387c478bd9Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_fmri_size);
18397c478bd9Sstevel@tonic-gate 	assert(err == 0 || err == EEXIST);
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	if (rv->gv_delegate_initialized == 0) {
18427c478bd9Sstevel@tonic-gate 		rv->gv_delegate_channel = restarter_protocol_init_delegate(
18437c478bd9Sstevel@tonic-gate 		    rv->gv_name);
18447c478bd9Sstevel@tonic-gate 		rv->gv_delegate_initialized = 1;
18457c478bd9Sstevel@tonic-gate 	}
18467c478bd9Sstevel@tonic-gate 	v->gv_restarter_id = rv->gv_id;
18477c478bd9Sstevel@tonic-gate 	v->gv_restarter_channel = rv->gv_delegate_channel;
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	err = graph_insert_dependency(v, rv, pathp);
18507c478bd9Sstevel@tonic-gate 	if (err != 0) {
18517c478bd9Sstevel@tonic-gate 		assert(err == ELOOP);
18527c478bd9Sstevel@tonic-gate 		return (ELOOP);
18537c478bd9Sstevel@tonic-gate 	}
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE);
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	if (!(rv->gv_flags & GV_CONFIGURED)) {
18587c478bd9Sstevel@tonic-gate 		scf_instance_t *inst;
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 		err = libscf_fmri_get_instance(h, rv->gv_name, &inst);
18617c478bd9Sstevel@tonic-gate 		switch (err) {
18627c478bd9Sstevel@tonic-gate 		case 0:
18637c478bd9Sstevel@tonic-gate 			err = configure_vertex(rv, inst);
18647c478bd9Sstevel@tonic-gate 			scf_instance_destroy(inst);
18657c478bd9Sstevel@tonic-gate 			switch (err) {
18667c478bd9Sstevel@tonic-gate 			case 0:
18677c478bd9Sstevel@tonic-gate 			case ECANCELED:
18687c478bd9Sstevel@tonic-gate 				break;
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
18717c478bd9Sstevel@tonic-gate 				return (ECONNABORTED);
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 			default:
18747c478bd9Sstevel@tonic-gate 				bad_error("configure_vertex", err);
18757c478bd9Sstevel@tonic-gate 			}
18767c478bd9Sstevel@tonic-gate 			break;
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
18797c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 		case ENOENT:
18827c478bd9Sstevel@tonic-gate 			break;
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 		case ENOTSUP:
18857c478bd9Sstevel@tonic-gate 			/*
18867c478bd9Sstevel@tonic-gate 			 * The fmri doesn't specify an instance - translate
18877c478bd9Sstevel@tonic-gate 			 * to EINVAL.
18887c478bd9Sstevel@tonic-gate 			 */
18897c478bd9Sstevel@tonic-gate 			return (EINVAL);
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 		case EINVAL:
18927c478bd9Sstevel@tonic-gate 		default:
18937c478bd9Sstevel@tonic-gate 			bad_error("libscf_fmri_get_instance", err);
18947c478bd9Sstevel@tonic-gate 		}
18957c478bd9Sstevel@tonic-gate 	}
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	return (0);
18987c478bd9Sstevel@tonic-gate }
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 
19017c478bd9Sstevel@tonic-gate /*
19027c478bd9Sstevel@tonic-gate  * Add all of the instances of the service named by fmri to the graph.
19037c478bd9Sstevel@tonic-gate  * Returns
19047c478bd9Sstevel@tonic-gate  *   0 - success
19057c478bd9Sstevel@tonic-gate  *   ENOENT - service indicated by fmri does not exist
19067c478bd9Sstevel@tonic-gate  *
19077c478bd9Sstevel@tonic-gate  * In both cases *reboundp will be B_TRUE if the handle was rebound, or B_FALSE
19087c478bd9Sstevel@tonic-gate  * otherwise.
19097c478bd9Sstevel@tonic-gate  */
19107c478bd9Sstevel@tonic-gate static int
19117c478bd9Sstevel@tonic-gate add_service(const char *fmri, scf_handle_t *h, boolean_t *reboundp)
19127c478bd9Sstevel@tonic-gate {
19137c478bd9Sstevel@tonic-gate 	scf_service_t *svc;
19147c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
19157c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
19167c478bd9Sstevel@tonic-gate 	char *inst_fmri;
19177c478bd9Sstevel@tonic-gate 	int ret, r;
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 	*reboundp = B_FALSE;
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	svc = safe_scf_service_create(h);
19227c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
19237c478bd9Sstevel@tonic-gate 	iter = safe_scf_iter_create(h);
19247c478bd9Sstevel@tonic-gate 	inst_fmri = startd_alloc(max_scf_fmri_size);
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate rebound:
19277c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
19287c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) != 0) {
19297c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
19307c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
19317c478bd9Sstevel@tonic-gate 		default:
19327c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
19337c478bd9Sstevel@tonic-gate 			*reboundp = B_TRUE;
19347c478bd9Sstevel@tonic-gate 			goto rebound;
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
19377c478bd9Sstevel@tonic-gate 			ret = ENOENT;
19387c478bd9Sstevel@tonic-gate 			goto out;
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
19417c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
19427c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
19437c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
19447c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
19457c478bd9Sstevel@tonic-gate 		}
19467c478bd9Sstevel@tonic-gate 	}
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 	if (scf_iter_service_instances(iter, svc) != 0) {
19497c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
19507c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
19517c478bd9Sstevel@tonic-gate 		default:
19527c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
19537c478bd9Sstevel@tonic-gate 			*reboundp = B_TRUE;
19547c478bd9Sstevel@tonic-gate 			goto rebound;
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
19577c478bd9Sstevel@tonic-gate 			ret = ENOENT;
19587c478bd9Sstevel@tonic-gate 			goto out;
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
19617c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
19627c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
19633eae19d9Swesolows 			bad_error("scf_iter_service_instances", scf_error());
19647c478bd9Sstevel@tonic-gate 		}
19657c478bd9Sstevel@tonic-gate 	}
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 	for (;;) {
19687c478bd9Sstevel@tonic-gate 		r = scf_iter_next_instance(iter, inst);
19697c478bd9Sstevel@tonic-gate 		if (r == 0)
19707c478bd9Sstevel@tonic-gate 			break;
19717c478bd9Sstevel@tonic-gate 		if (r != 1) {
19727c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
19737c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
19747c478bd9Sstevel@tonic-gate 			default:
19757c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
19767c478bd9Sstevel@tonic-gate 				*reboundp = B_TRUE;
19777c478bd9Sstevel@tonic-gate 				goto rebound;
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
19807c478bd9Sstevel@tonic-gate 				ret = ENOENT;
19817c478bd9Sstevel@tonic-gate 				goto out;
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
19847c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
19857c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
19867c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
19877c478bd9Sstevel@tonic-gate 				bad_error("scf_iter_next_instance",
19887c478bd9Sstevel@tonic-gate 				    scf_error());
19897c478bd9Sstevel@tonic-gate 			}
19907c478bd9Sstevel@tonic-gate 		}
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 		if (scf_instance_to_fmri(inst, inst_fmri, max_scf_fmri_size) <
19937c478bd9Sstevel@tonic-gate 		    0) {
19947c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
19957c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
19967c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
19977c478bd9Sstevel@tonic-gate 				*reboundp = B_TRUE;
19987c478bd9Sstevel@tonic-gate 				goto rebound;
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
20017c478bd9Sstevel@tonic-gate 				continue;
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
20047c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
20057c478bd9Sstevel@tonic-gate 				bad_error("scf_instance_to_fmri", scf_error());
20067c478bd9Sstevel@tonic-gate 			}
20077c478bd9Sstevel@tonic-gate 		}
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 		r = dgraph_add_instance(inst_fmri, inst, B_FALSE);
20107c478bd9Sstevel@tonic-gate 		switch (r) {
20117c478bd9Sstevel@tonic-gate 		case 0:
20127c478bd9Sstevel@tonic-gate 		case ECANCELED:
20137c478bd9Sstevel@tonic-gate 			break;
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 		case EEXIST:
20167c478bd9Sstevel@tonic-gate 			continue;
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
20197c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
20207c478bd9Sstevel@tonic-gate 			*reboundp = B_TRUE;
20217c478bd9Sstevel@tonic-gate 			goto rebound;
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 		case EINVAL:
20247c478bd9Sstevel@tonic-gate 		default:
20257c478bd9Sstevel@tonic-gate 			bad_error("dgraph_add_instance", r);
20267c478bd9Sstevel@tonic-gate 		}
20277c478bd9Sstevel@tonic-gate 	}
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 	ret = 0;
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate out:
20327c478bd9Sstevel@tonic-gate 	startd_free(inst_fmri, max_scf_fmri_size);
20337c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
20347c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
20357c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
20367c478bd9Sstevel@tonic-gate 	return (ret);
20377c478bd9Sstevel@tonic-gate }
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate struct depfmri_info {
20407c478bd9Sstevel@tonic-gate 	graph_vertex_t	*v;		/* GVT_GROUP vertex */
20417c478bd9Sstevel@tonic-gate 	gv_type_t	type;		/* type of dependency */
20427c478bd9Sstevel@tonic-gate 	const char	*inst_fmri;	/* FMRI of parental GVT_INST vert. */
20437c478bd9Sstevel@tonic-gate 	const char	*pg_name;	/* Name of dependency pg */
20447c478bd9Sstevel@tonic-gate 	scf_handle_t	*h;
20457c478bd9Sstevel@tonic-gate 	int		err;		/* return error code */
20467c478bd9Sstevel@tonic-gate 	int		**pathp;	/* return circular dependency path */
20477c478bd9Sstevel@tonic-gate };
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate /*
20507c478bd9Sstevel@tonic-gate  * Find or create a vertex for fmri and make info->v depend on it.
20517c478bd9Sstevel@tonic-gate  * Returns
20527c478bd9Sstevel@tonic-gate  *   0 - success
20537c478bd9Sstevel@tonic-gate  *   nonzero - failure
20547c478bd9Sstevel@tonic-gate  *
20557c478bd9Sstevel@tonic-gate  * On failure, sets info->err to
20567c478bd9Sstevel@tonic-gate  *   EINVAL - fmri is invalid
20577c478bd9Sstevel@tonic-gate  *	      fmri does not match info->type
20587c478bd9Sstevel@tonic-gate  *   ELOOP - Adding the dependency creates a circular dependency.  *info->pathp
20597c478bd9Sstevel@tonic-gate  *	     will point to an array of the ids of the members of the cycle.
20607c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection was broken
20617c478bd9Sstevel@tonic-gate  *   ECONNRESET - succeeded, but repository connection was reset
20627c478bd9Sstevel@tonic-gate  */
20637c478bd9Sstevel@tonic-gate static int
20647c478bd9Sstevel@tonic-gate process_dependency_fmri(const char *fmri, struct depfmri_info *info)
20657c478bd9Sstevel@tonic-gate {
20667c478bd9Sstevel@tonic-gate 	int err;
20677c478bd9Sstevel@tonic-gate 	graph_vertex_t *depgroup_v, *v;
20687c478bd9Sstevel@tonic-gate 	char *fmri_copy, *cfmri;
20697c478bd9Sstevel@tonic-gate 	size_t fmri_copy_sz;
20707c478bd9Sstevel@tonic-gate 	const char *scope, *service, *instance, *pg;
20717c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
20727c478bd9Sstevel@tonic-gate 	boolean_t rebound;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate 	/* Get or create vertex for FMRI */
20777c478bd9Sstevel@tonic-gate 	depgroup_v = info->v;
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 	if (strncmp(fmri, "file:", sizeof ("file:") - 1) == 0) {
20807c478bd9Sstevel@tonic-gate 		if (info->type != GVT_FILE) {
20817c478bd9Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
20827c478bd9Sstevel@tonic-gate 			    "FMRI \"%s\" is not allowed for the \"%s\" "
20837c478bd9Sstevel@tonic-gate 			    "dependency's type of instance %s.\n", fmri,
20847c478bd9Sstevel@tonic-gate 			    info->pg_name, info->inst_fmri);
20857c478bd9Sstevel@tonic-gate 			return (info->err = EINVAL);
20867c478bd9Sstevel@tonic-gate 		}
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(fmri, info->type, 0,
20897c478bd9Sstevel@tonic-gate 		    RERR_NONE, &v);
20907c478bd9Sstevel@tonic-gate 		switch (err) {
20917c478bd9Sstevel@tonic-gate 		case 0:
20927c478bd9Sstevel@tonic-gate 			break;
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 		case EEXIST:
20957c478bd9Sstevel@tonic-gate 			assert(v->gv_type == GVT_FILE);
20967c478bd9Sstevel@tonic-gate 			break;
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 		case EINVAL:		/* prevented above */
20997c478bd9Sstevel@tonic-gate 		default:
21007c478bd9Sstevel@tonic-gate 			bad_error("graph_insert_vertex_unconfigured", err);
21017c478bd9Sstevel@tonic-gate 		}
21027c478bd9Sstevel@tonic-gate 	} else {
21037c478bd9Sstevel@tonic-gate 		if (info->type != GVT_INST) {
21047c478bd9Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
21057c478bd9Sstevel@tonic-gate 			    "FMRI \"%s\" is not allowed for the \"%s\" "
21067c478bd9Sstevel@tonic-gate 			    "dependency's type of instance %s.\n", fmri,
21077c478bd9Sstevel@tonic-gate 			    info->pg_name, info->inst_fmri);
21087c478bd9Sstevel@tonic-gate 			return (info->err = EINVAL);
21097c478bd9Sstevel@tonic-gate 		}
21107c478bd9Sstevel@tonic-gate 
21117c478bd9Sstevel@tonic-gate 		/*
21127c478bd9Sstevel@tonic-gate 		 * We must canonify fmri & add a vertex for it.
21137c478bd9Sstevel@tonic-gate 		 */
21147c478bd9Sstevel@tonic-gate 		fmri_copy_sz = strlen(fmri) + 1;
21157c478bd9Sstevel@tonic-gate 		fmri_copy = startd_alloc(fmri_copy_sz);
21167c478bd9Sstevel@tonic-gate 		(void) strcpy(fmri_copy, fmri);
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 		/* Determine if the FMRI is a property group or instance */
21197c478bd9Sstevel@tonic-gate 		if (scf_parse_svc_fmri(fmri_copy, &scope, &service,
21207c478bd9Sstevel@tonic-gate 		    &instance, &pg, NULL) != 0) {
21217c478bd9Sstevel@tonic-gate 			startd_free(fmri_copy, fmri_copy_sz);
21227c478bd9Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
21237c478bd9Sstevel@tonic-gate 			    "Dependency \"%s\" of %s has invalid FMRI "
21247c478bd9Sstevel@tonic-gate 			    "\"%s\".\n", info->pg_name, info->inst_fmri,
21257c478bd9Sstevel@tonic-gate 			    fmri);
21267c478bd9Sstevel@tonic-gate 			return (info->err = EINVAL);
21277c478bd9Sstevel@tonic-gate 		}
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 		if (service == NULL || pg != NULL) {
21307c478bd9Sstevel@tonic-gate 			startd_free(fmri_copy, fmri_copy_sz);
21317c478bd9Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
21327c478bd9Sstevel@tonic-gate 			    "Dependency \"%s\" of %s does not designate a "
21337c478bd9Sstevel@tonic-gate 			    "service or instance.\n", info->pg_name,
21347c478bd9Sstevel@tonic-gate 			    info->inst_fmri);
21357c478bd9Sstevel@tonic-gate 			return (info->err = EINVAL);
21367c478bd9Sstevel@tonic-gate 		}
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 		if (scope == NULL || strcmp(scope, SCF_SCOPE_LOCAL) == 0) {
21397c478bd9Sstevel@tonic-gate 			cfmri = uu_msprintf("svc:/%s%s%s",
21407c478bd9Sstevel@tonic-gate 			    service, instance ? ":" : "", instance ? instance :
21417c478bd9Sstevel@tonic-gate 			    "");
21427c478bd9Sstevel@tonic-gate 		} else {
21437c478bd9Sstevel@tonic-gate 			cfmri = uu_msprintf("svc://%s/%s%s%s",
21447c478bd9Sstevel@tonic-gate 			    scope, service, instance ? ":" : "", instance ?
21457c478bd9Sstevel@tonic-gate 			    instance : "");
21467c478bd9Sstevel@tonic-gate 		}
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 		startd_free(fmri_copy, fmri_copy_sz);
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(cfmri, instance ?
21517c478bd9Sstevel@tonic-gate 		    GVT_INST : GVT_SVC, instance ? 0 : DEPGRP_REQUIRE_ANY,
21527c478bd9Sstevel@tonic-gate 		    RERR_NONE, &v);
21537c478bd9Sstevel@tonic-gate 		uu_free(cfmri);
21547c478bd9Sstevel@tonic-gate 		switch (err) {
21557c478bd9Sstevel@tonic-gate 		case 0:
21567c478bd9Sstevel@tonic-gate 			break;
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 		case EEXIST:
21597c478bd9Sstevel@tonic-gate 			/* Verify v. */
21607c478bd9Sstevel@tonic-gate 			if (instance != NULL)
21617c478bd9Sstevel@tonic-gate 				assert(v->gv_type == GVT_INST);
21627c478bd9Sstevel@tonic-gate 			else
21637c478bd9Sstevel@tonic-gate 				assert(v->gv_type == GVT_SVC);
21647c478bd9Sstevel@tonic-gate 			break;
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 		default:
21677c478bd9Sstevel@tonic-gate 			bad_error("graph_insert_vertex_unconfigured", err);
21687c478bd9Sstevel@tonic-gate 		}
21697c478bd9Sstevel@tonic-gate 	}
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate 	/* Add dependency from depgroup_v to new vertex */
21727c478bd9Sstevel@tonic-gate 	info->err = graph_insert_dependency(depgroup_v, v, info->pathp);
21737c478bd9Sstevel@tonic-gate 	switch (info->err) {
21747c478bd9Sstevel@tonic-gate 	case 0:
21757c478bd9Sstevel@tonic-gate 		break;
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	case ELOOP:
21787c478bd9Sstevel@tonic-gate 		return (ELOOP);
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 	default:
21817c478bd9Sstevel@tonic-gate 		bad_error("graph_insert_dependency", info->err);
21827c478bd9Sstevel@tonic-gate 	}
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 	/* This must be after we insert the dependency, to avoid looping. */
21857c478bd9Sstevel@tonic-gate 	switch (v->gv_type) {
21867c478bd9Sstevel@tonic-gate 	case GVT_INST:
21877c478bd9Sstevel@tonic-gate 		if ((v->gv_flags & GV_CONFIGURED) != 0)
21887c478bd9Sstevel@tonic-gate 			break;
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 		inst = safe_scf_instance_create(info->h);
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 		rebound = B_FALSE;
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate rebound:
21957c478bd9Sstevel@tonic-gate 		err = libscf_lookup_instance(v->gv_name, inst);
21967c478bd9Sstevel@tonic-gate 		switch (err) {
21977c478bd9Sstevel@tonic-gate 		case 0:
21987c478bd9Sstevel@tonic-gate 			err = configure_vertex(v, inst);
21997c478bd9Sstevel@tonic-gate 			switch (err) {
22007c478bd9Sstevel@tonic-gate 			case 0:
22017c478bd9Sstevel@tonic-gate 			case ECANCELED:
22027c478bd9Sstevel@tonic-gate 				break;
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
22057c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(info->h);
22067c478bd9Sstevel@tonic-gate 				rebound = B_TRUE;
22077c478bd9Sstevel@tonic-gate 				goto rebound;
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 			default:
22107c478bd9Sstevel@tonic-gate 				bad_error("configure_vertex", err);
22117c478bd9Sstevel@tonic-gate 			}
22127c478bd9Sstevel@tonic-gate 			break;
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate 		case ENOENT:
22157c478bd9Sstevel@tonic-gate 			break;
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
22187c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(info->h);
22197c478bd9Sstevel@tonic-gate 			rebound = B_TRUE;
22207c478bd9Sstevel@tonic-gate 			goto rebound;
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 		case EINVAL:
22237c478bd9Sstevel@tonic-gate 		case ENOTSUP:
22247c478bd9Sstevel@tonic-gate 		default:
22257c478bd9Sstevel@tonic-gate 			bad_error("libscf_fmri_get_instance", err);
22267c478bd9Sstevel@tonic-gate 		}
22277c478bd9Sstevel@tonic-gate 
22287c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate 		if (rebound)
22317c478bd9Sstevel@tonic-gate 			return (info->err = ECONNRESET);
22327c478bd9Sstevel@tonic-gate 		break;
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate 	case GVT_SVC:
22357c478bd9Sstevel@tonic-gate 		(void) add_service(v->gv_name, info->h, &rebound);
22367c478bd9Sstevel@tonic-gate 		if (rebound)
22377c478bd9Sstevel@tonic-gate 			return (info->err = ECONNRESET);
22387c478bd9Sstevel@tonic-gate 	}
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate 	return (0);
22417c478bd9Sstevel@tonic-gate }
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate struct deppg_info {
22447c478bd9Sstevel@tonic-gate 	graph_vertex_t	*v;		/* GVT_INST vertex */
22457c478bd9Sstevel@tonic-gate 	int		err;		/* return error */
22467c478bd9Sstevel@tonic-gate 	int		**pathp;	/* return circular dependency path */
22477c478bd9Sstevel@tonic-gate };
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate /*
22507c478bd9Sstevel@tonic-gate  * Make info->v depend on a new GVT_GROUP node for this property group,
22517c478bd9Sstevel@tonic-gate  * and then call process_dependency_fmri() for the values of the entity
22527c478bd9Sstevel@tonic-gate  * property.  Return 0 on success, or if something goes wrong return nonzero
22537c478bd9Sstevel@tonic-gate  * and set info->err to ECONNABORTED, EINVAL, or the error code returned by
22547c478bd9Sstevel@tonic-gate  * process_dependency_fmri().
22557c478bd9Sstevel@tonic-gate  */
22567c478bd9Sstevel@tonic-gate static int
22577c478bd9Sstevel@tonic-gate process_dependency_pg(scf_propertygroup_t *pg, struct deppg_info *info)
22587c478bd9Sstevel@tonic-gate {
22597c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
22607c478bd9Sstevel@tonic-gate 	depgroup_type_t deptype;
2261af3bcd91Srm88369 	restarter_error_t rerr;
22627c478bd9Sstevel@tonic-gate 	struct depfmri_info linfo;
22637c478bd9Sstevel@tonic-gate 	char *fmri, *pg_name;
22647c478bd9Sstevel@tonic-gate 	size_t fmri_sz;
22657c478bd9Sstevel@tonic-gate 	graph_vertex_t *depgrp;
22667c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
22677c478bd9Sstevel@tonic-gate 	int err;
22687c478bd9Sstevel@tonic-gate 	int empty;
22697c478bd9Sstevel@tonic-gate 	scf_error_t scferr;
22707c478bd9Sstevel@tonic-gate 	ssize_t len;
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 	h = scf_pg_handle(pg);
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 	pg_name = startd_alloc(max_scf_name_size);
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 	len = scf_pg_get_name(pg, pg_name, max_scf_name_size);
22797c478bd9Sstevel@tonic-gate 	if (len < 0) {
22807c478bd9Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
22817c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
22827c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
22837c478bd9Sstevel@tonic-gate 		default:
22847c478bd9Sstevel@tonic-gate 			return (info->err = ECONNABORTED);
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
22877c478bd9Sstevel@tonic-gate 			return (info->err = 0);
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
22907c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_name", scf_error());
22917c478bd9Sstevel@tonic-gate 		}
22927c478bd9Sstevel@tonic-gate 	}
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	/*
22957c478bd9Sstevel@tonic-gate 	 * Skip over empty dependency groups.  Since dependency property
22967c478bd9Sstevel@tonic-gate 	 * groups are updated atomically, they are either empty or
22977c478bd9Sstevel@tonic-gate 	 * fully populated.
22987c478bd9Sstevel@tonic-gate 	 */
22997c478bd9Sstevel@tonic-gate 	empty = depgroup_empty(h, pg);
23007c478bd9Sstevel@tonic-gate 	if (empty < 0) {
23017c478bd9Sstevel@tonic-gate 		log_error(LOG_INFO,
23027c478bd9Sstevel@tonic-gate 		    "Error reading dependency group \"%s\" of %s: %s\n",
23037c478bd9Sstevel@tonic-gate 		    pg_name, info->v->gv_name, scf_strerror(scf_error()));
23047c478bd9Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
23057c478bd9Sstevel@tonic-gate 		return (info->err = EINVAL);
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 	} else if (empty == 1) {
23087c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
23097c478bd9Sstevel@tonic-gate 		    "Ignoring empty dependency group \"%s\" of %s\n",
23107c478bd9Sstevel@tonic-gate 		    pg_name, info->v->gv_name);
23117c478bd9Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
23127c478bd9Sstevel@tonic-gate 		return (info->err = 0);
23137c478bd9Sstevel@tonic-gate 	}
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 	fmri_sz = strlen(info->v->gv_name) + 1 + len + 1;
23167c478bd9Sstevel@tonic-gate 	fmri = startd_alloc(fmri_sz);
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate 	(void) snprintf(fmri, max_scf_name_size, "%s>%s", info->v->gv_name,
23197c478bd9Sstevel@tonic-gate 	    pg_name);
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate 	/* Validate the pg before modifying the graph */
23227c478bd9Sstevel@tonic-gate 	deptype = depgroup_read_grouping(h, pg);
23237c478bd9Sstevel@tonic-gate 	if (deptype == DEPGRP_UNSUPPORTED) {
23247c478bd9Sstevel@tonic-gate 		log_error(LOG_INFO,
23257c478bd9Sstevel@tonic-gate 		    "Dependency \"%s\" of %s has an unknown grouping value.\n",
23267c478bd9Sstevel@tonic-gate 		    pg_name, info->v->gv_name);
23277c478bd9Sstevel@tonic-gate 		startd_free(fmri, fmri_sz);
23287c478bd9Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
23297c478bd9Sstevel@tonic-gate 		return (info->err = EINVAL);
23307c478bd9Sstevel@tonic-gate 	}
23317c478bd9Sstevel@tonic-gate 
2332af3bcd91Srm88369 	rerr = depgroup_read_restart(h, pg);
2333af3bcd91Srm88369 	if (rerr == RERR_UNSUPPORTED) {
2334af3bcd91Srm88369 		log_error(LOG_INFO,
2335af3bcd91Srm88369 		    "Dependency \"%s\" of %s has an unknown restart_on value."
2336af3bcd91Srm88369 		    "\n", pg_name, info->v->gv_name);
2337af3bcd91Srm88369 		startd_free(fmri, fmri_sz);
2338af3bcd91Srm88369 		startd_free(pg_name, max_scf_name_size);
2339af3bcd91Srm88369 		return (info->err = EINVAL);
2340af3bcd91Srm88369 	}
2341af3bcd91Srm88369 
23427c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0) {
23457c478bd9Sstevel@tonic-gate 		scferr = scf_error();
23467c478bd9Sstevel@tonic-gate 		scf_property_destroy(prop);
23477c478bd9Sstevel@tonic-gate 		if (scferr == SCF_ERROR_DELETED) {
23487c478bd9Sstevel@tonic-gate 			startd_free(fmri, fmri_sz);
23497c478bd9Sstevel@tonic-gate 			startd_free(pg_name, max_scf_name_size);
23507c478bd9Sstevel@tonic-gate 			return (info->err = 0);
23517c478bd9Sstevel@tonic-gate 		} else if (scferr != SCF_ERROR_NOT_FOUND) {
23527c478bd9Sstevel@tonic-gate 			startd_free(fmri, fmri_sz);
23537c478bd9Sstevel@tonic-gate 			startd_free(pg_name, max_scf_name_size);
23547c478bd9Sstevel@tonic-gate 			return (info->err = ECONNABORTED);
23557c478bd9Sstevel@tonic-gate 		}
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 		log_error(LOG_INFO,
23587c478bd9Sstevel@tonic-gate 		    "Dependency \"%s\" of %s is missing a \"%s\" property.\n",
23597c478bd9Sstevel@tonic-gate 		    pg_name, info->v->gv_name, SCF_PROPERTY_ENTITIES);
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate 		startd_free(fmri, fmri_sz);
23627c478bd9Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 		return (info->err = EINVAL);
23657c478bd9Sstevel@tonic-gate 	}
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	/* Create depgroup vertex for pg */
23687c478bd9Sstevel@tonic-gate 	err = graph_insert_vertex_unconfigured(fmri, GVT_GROUP, deptype,
2369af3bcd91Srm88369 	    rerr, &depgrp);
23707c478bd9Sstevel@tonic-gate 	assert(err == 0);
23717c478bd9Sstevel@tonic-gate 	startd_free(fmri, fmri_sz);
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 	/* Add dependency from inst vertex to new vertex */
23747c478bd9Sstevel@tonic-gate 	err = graph_insert_dependency(info->v, depgrp, info->pathp);
23757c478bd9Sstevel@tonic-gate 	/* ELOOP can't happen because this should be a new vertex */
23767c478bd9Sstevel@tonic-gate 	assert(err == 0);
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 	linfo.v = depgrp;
23797c478bd9Sstevel@tonic-gate 	linfo.type = depgroup_read_scheme(h, pg);
23807c478bd9Sstevel@tonic-gate 	linfo.inst_fmri = info->v->gv_name;
23817c478bd9Sstevel@tonic-gate 	linfo.pg_name = pg_name;
23827c478bd9Sstevel@tonic-gate 	linfo.h = h;
23837c478bd9Sstevel@tonic-gate 	linfo.err = 0;
23847c478bd9Sstevel@tonic-gate 	linfo.pathp = info->pathp;
23857c478bd9Sstevel@tonic-gate 	err = walk_property_astrings(prop, (callback_t)process_dependency_fmri,
23867c478bd9Sstevel@tonic-gate 	    &linfo);
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
23897c478bd9Sstevel@tonic-gate 	startd_free(pg_name, max_scf_name_size);
23907c478bd9Sstevel@tonic-gate 
23917c478bd9Sstevel@tonic-gate 	switch (err) {
23927c478bd9Sstevel@tonic-gate 	case 0:
23937c478bd9Sstevel@tonic-gate 	case EINTR:
23947c478bd9Sstevel@tonic-gate 		return (info->err = linfo.err);
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
23977c478bd9Sstevel@tonic-gate 	case EINVAL:
23987c478bd9Sstevel@tonic-gate 		return (info->err = err);
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 	case ECANCELED:
24017c478bd9Sstevel@tonic-gate 		return (info->err = 0);
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	case ECONNRESET:
24047c478bd9Sstevel@tonic-gate 		return (info->err = ECONNABORTED);
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	default:
24077c478bd9Sstevel@tonic-gate 		bad_error("walk_property_astrings", err);
24087c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
24097c478bd9Sstevel@tonic-gate 	}
24107c478bd9Sstevel@tonic-gate }
24117c478bd9Sstevel@tonic-gate 
24127c478bd9Sstevel@tonic-gate /*
24137c478bd9Sstevel@tonic-gate  * Build the dependency info for v from the repository.  Returns 0 on success,
24147c478bd9Sstevel@tonic-gate  * ECONNABORTED on repository disconnection, EINVAL if the repository
24157c478bd9Sstevel@tonic-gate  * configuration is invalid, and ELOOP if a dependency would cause a cycle.
24167c478bd9Sstevel@tonic-gate  * In the last case, *pathp will point to a -1-terminated array of ids which
24177c478bd9Sstevel@tonic-gate  * constitute the rest of the dependency cycle.
24187c478bd9Sstevel@tonic-gate  */
24197c478bd9Sstevel@tonic-gate static int
24207c478bd9Sstevel@tonic-gate set_dependencies(graph_vertex_t *v, scf_instance_t *inst, int **pathp)
24217c478bd9Sstevel@tonic-gate {
24227c478bd9Sstevel@tonic-gate 	struct deppg_info info;
24237c478bd9Sstevel@tonic-gate 	int err;
24247c478bd9Sstevel@tonic-gate 	uint_t old_configured;
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate 	/*
24297c478bd9Sstevel@tonic-gate 	 * Mark the vertex as configured during dependency insertion to avoid
24307c478bd9Sstevel@tonic-gate 	 * dependency cycles (which can appear in the graph if one of the
24317c478bd9Sstevel@tonic-gate 	 * vertices is an exclusion-group).
24327c478bd9Sstevel@tonic-gate 	 */
24337c478bd9Sstevel@tonic-gate 	old_configured = v->gv_flags & GV_CONFIGURED;
24347c478bd9Sstevel@tonic-gate 	v->gv_flags |= GV_CONFIGURED;
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 	info.err = 0;
24377c478bd9Sstevel@tonic-gate 	info.v = v;
24387c478bd9Sstevel@tonic-gate 	info.pathp = pathp;
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate 	err = walk_dependency_pgs(inst, (callback_t)process_dependency_pg,
24417c478bd9Sstevel@tonic-gate 	    &info);
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate 	if (!old_configured)
24447c478bd9Sstevel@tonic-gate 		v->gv_flags &= ~GV_CONFIGURED;
24457c478bd9Sstevel@tonic-gate 
24467c478bd9Sstevel@tonic-gate 	switch (err) {
24477c478bd9Sstevel@tonic-gate 	case 0:
24487c478bd9Sstevel@tonic-gate 	case EINTR:
24497c478bd9Sstevel@tonic-gate 		return (info.err);
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
24527c478bd9Sstevel@tonic-gate 		return (ECONNABORTED);
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate 	case ECANCELED:
24557c478bd9Sstevel@tonic-gate 		/* Should get delete event, so return 0. */
24567c478bd9Sstevel@tonic-gate 		return (0);
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate 	default:
24597c478bd9Sstevel@tonic-gate 		bad_error("walk_dependency_pgs", err);
24607c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
24617c478bd9Sstevel@tonic-gate 	}
24627c478bd9Sstevel@tonic-gate }
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate static void
24667c478bd9Sstevel@tonic-gate handle_cycle(const char *fmri, int *path)
24677c478bd9Sstevel@tonic-gate {
24687c478bd9Sstevel@tonic-gate 	const char *cp;
24697c478bd9Sstevel@tonic-gate 	size_t sz;
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 	path_to_str(path, (char **)&cp, &sz);
24747c478bd9Sstevel@tonic-gate 
247599b44c3bSlianep 	log_error(LOG_ERR, "Transitioning %s to maintenance "
247699b44c3bSlianep 	    "because it completes a dependency cycle (see svcs -xv for "
247799b44c3bSlianep 	    "details):\n%s", fmri ? fmri : "?", cp);
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 	startd_free((void *)cp, sz);
24807c478bd9Sstevel@tonic-gate }
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate /*
24833ad28c1eSrm88369  * Increment the vertex's reference count to prevent the vertex removal
24843ad28c1eSrm88369  * from the dgraph.
24853ad28c1eSrm88369  */
24863ad28c1eSrm88369 static void
24873ad28c1eSrm88369 vertex_ref(graph_vertex_t *v)
24883ad28c1eSrm88369 {
24893ad28c1eSrm88369 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
24903ad28c1eSrm88369 
24913ad28c1eSrm88369 	v->gv_refs++;
24923ad28c1eSrm88369 }
24933ad28c1eSrm88369 
24943ad28c1eSrm88369 /*
24953ad28c1eSrm88369  * Decrement the vertex's reference count and remove the vertex from
24963ad28c1eSrm88369  * the dgraph when possible.
24973ad28c1eSrm88369  *
24983ad28c1eSrm88369  * Return VERTEX_REMOVED when the vertex has been removed otherwise
24993ad28c1eSrm88369  * return VERTEX_INUSE.
25007c478bd9Sstevel@tonic-gate  */
25017c478bd9Sstevel@tonic-gate static int
25023ad28c1eSrm88369 vertex_unref(graph_vertex_t *v)
25033ad28c1eSrm88369 {
25043ad28c1eSrm88369 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
25053ad28c1eSrm88369 	assert(v->gv_refs > 0);
25063ad28c1eSrm88369 
25073ad28c1eSrm88369 	v->gv_refs--;
25083ad28c1eSrm88369 
25093ad28c1eSrm88369 	return (free_if_unrefed(v));
25103ad28c1eSrm88369 }
25113ad28c1eSrm88369 
25123ad28c1eSrm88369 /*
25133ad28c1eSrm88369  * When run on the dependencies of a vertex, populates list with
25143ad28c1eSrm88369  * graph_edge_t's which point to the service vertices or the instance
25153ad28c1eSrm88369  * vertices (no GVT_GROUP nodes) on which the vertex depends.
25163ad28c1eSrm88369  *
25173ad28c1eSrm88369  * Increment the vertex's reference count once the vertex is inserted
25183ad28c1eSrm88369  * in the list. The vertex won't be able to be deleted from the dgraph
25193ad28c1eSrm88369  * while it is referenced.
25203ad28c1eSrm88369  */
25213ad28c1eSrm88369 static int
25223ad28c1eSrm88369 append_svcs_or_insts(graph_edge_t *e, uu_list_t *list)
25237c478bd9Sstevel@tonic-gate {
25247c478bd9Sstevel@tonic-gate 	graph_vertex_t *v = e->ge_vertex;
25257c478bd9Sstevel@tonic-gate 	graph_edge_t *new;
25267c478bd9Sstevel@tonic-gate 	int r;
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 	switch (v->gv_type) {
25297c478bd9Sstevel@tonic-gate 	case GVT_INST:
25307c478bd9Sstevel@tonic-gate 	case GVT_SVC:
25317c478bd9Sstevel@tonic-gate 		break;
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 	case GVT_GROUP:
25347c478bd9Sstevel@tonic-gate 		r = uu_list_walk(v->gv_dependencies,
25353ad28c1eSrm88369 		    (uu_walk_fn_t *)append_svcs_or_insts, list, 0);
25367c478bd9Sstevel@tonic-gate 		assert(r == 0);
25377c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 	case GVT_FILE:
25407c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 	default:
25437c478bd9Sstevel@tonic-gate #ifndef NDEBUG
25447c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
25457c478bd9Sstevel@tonic-gate 		    __LINE__, v->gv_type);
25467c478bd9Sstevel@tonic-gate #endif
25477c478bd9Sstevel@tonic-gate 		abort();
25487c478bd9Sstevel@tonic-gate 	}
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate 	new = startd_alloc(sizeof (*new));
25517c478bd9Sstevel@tonic-gate 	new->ge_vertex = v;
25527c478bd9Sstevel@tonic-gate 	uu_list_node_init(new, &new->ge_link, graph_edge_pool);
25537c478bd9Sstevel@tonic-gate 	r = uu_list_insert_before(list, NULL, new);
25547c478bd9Sstevel@tonic-gate 	assert(r == 0);
25553ad28c1eSrm88369 
25563ad28c1eSrm88369 	/*
25573ad28c1eSrm88369 	 * Because we are inserting the vertex in a list, we don't want
25583ad28c1eSrm88369 	 * the vertex to be freed while the list is in use. In order to
25593ad28c1eSrm88369 	 * achieve that, increment the vertex's reference count.
25603ad28c1eSrm88369 	 */
25613ad28c1eSrm88369 	vertex_ref(v);
25623ad28c1eSrm88369 
25637c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
25647c478bd9Sstevel@tonic-gate }
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate static boolean_t
25677c478bd9Sstevel@tonic-gate should_be_in_subgraph(graph_vertex_t *v)
25687c478bd9Sstevel@tonic-gate {
25697c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
25707c478bd9Sstevel@tonic-gate 
25717c478bd9Sstevel@tonic-gate 	if (v == milestone)
25727c478bd9Sstevel@tonic-gate 		return (B_TRUE);
25737c478bd9Sstevel@tonic-gate 
25747c478bd9Sstevel@tonic-gate 	/*
25757c478bd9Sstevel@tonic-gate 	 * v is in the subgraph if any of its dependents are in the subgraph.
25767c478bd9Sstevel@tonic-gate 	 * Except for EXCLUDE_ALL dependents.  And OPTIONAL dependents only
25777c478bd9Sstevel@tonic-gate 	 * count if we're enabled.
25787c478bd9Sstevel@tonic-gate 	 */
25797c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
25807c478bd9Sstevel@tonic-gate 	    e != NULL;
25817c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e)) {
25827c478bd9Sstevel@tonic-gate 		graph_vertex_t *dv = e->ge_vertex;
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate 		if (!(dv->gv_flags & GV_INSUBGRAPH))
25857c478bd9Sstevel@tonic-gate 			continue;
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate 		/*
25887c478bd9Sstevel@tonic-gate 		 * Don't include instances that are optional and disabled.
25897c478bd9Sstevel@tonic-gate 		 */
25907c478bd9Sstevel@tonic-gate 		if (v->gv_type == GVT_INST && dv->gv_type == GVT_SVC) {
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 			int in = 0;
25937c478bd9Sstevel@tonic-gate 			graph_edge_t *ee;
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate 			for (ee = uu_list_first(dv->gv_dependents);
25967c478bd9Sstevel@tonic-gate 			    ee != NULL;
25977c478bd9Sstevel@tonic-gate 			    ee = uu_list_next(dv->gv_dependents, ee)) {
25987c478bd9Sstevel@tonic-gate 
25997c478bd9Sstevel@tonic-gate 				graph_vertex_t *ddv = e->ge_vertex;
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate 				if (ddv->gv_type == GVT_GROUP &&
26027c478bd9Sstevel@tonic-gate 				    ddv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
26037c478bd9Sstevel@tonic-gate 					continue;
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 				if (ddv->gv_type == GVT_GROUP &&
26067c478bd9Sstevel@tonic-gate 				    ddv->gv_depgroup == DEPGRP_OPTIONAL_ALL &&
26077c478bd9Sstevel@tonic-gate 				    !(v->gv_flags & GV_ENBLD_NOOVR))
26087c478bd9Sstevel@tonic-gate 					continue;
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate 				in = 1;
26117c478bd9Sstevel@tonic-gate 			}
26127c478bd9Sstevel@tonic-gate 			if (!in)
26137c478bd9Sstevel@tonic-gate 				continue;
26147c478bd9Sstevel@tonic-gate 		}
26157c478bd9Sstevel@tonic-gate 		if (v->gv_type == GVT_INST &&
26167c478bd9Sstevel@tonic-gate 		    dv->gv_type == GVT_GROUP &&
26177c478bd9Sstevel@tonic-gate 		    dv->gv_depgroup == DEPGRP_OPTIONAL_ALL &&
26187c478bd9Sstevel@tonic-gate 		    !(v->gv_flags & GV_ENBLD_NOOVR))
26197c478bd9Sstevel@tonic-gate 			continue;
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate 		/* Don't include excluded services and instances */
26227c478bd9Sstevel@tonic-gate 		if (dv->gv_type == GVT_GROUP &&
26237c478bd9Sstevel@tonic-gate 		    dv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
26247c478bd9Sstevel@tonic-gate 			continue;
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate 		return (B_TRUE);
26277c478bd9Sstevel@tonic-gate 	}
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 	return (B_FALSE);
26307c478bd9Sstevel@tonic-gate }
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate /*
26337c478bd9Sstevel@tonic-gate  * Ensures that GV_INSUBGRAPH is set properly for v and its descendents.  If
26347c478bd9Sstevel@tonic-gate  * any bits change, manipulate the repository appropriately.  Returns 0 or
26357c478bd9Sstevel@tonic-gate  * ECONNABORTED.
26367c478bd9Sstevel@tonic-gate  */
26377c478bd9Sstevel@tonic-gate static int
26387c478bd9Sstevel@tonic-gate eval_subgraph(graph_vertex_t *v, scf_handle_t *h)
26397c478bd9Sstevel@tonic-gate {
26407c478bd9Sstevel@tonic-gate 	boolean_t old = (v->gv_flags & GV_INSUBGRAPH) != 0;
26417c478bd9Sstevel@tonic-gate 	boolean_t new;
26427c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
26437c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
26447c478bd9Sstevel@tonic-gate 	int ret = 0, r;
26457c478bd9Sstevel@tonic-gate 
26467c478bd9Sstevel@tonic-gate 	assert(milestone != NULL && milestone != MILESTONE_NONE);
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate 	new = should_be_in_subgraph(v);
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate 	if (new == old)
26517c478bd9Sstevel@tonic-gate 		return (0);
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, new ? "Adding %s to the subgraph.\n" :
26547c478bd9Sstevel@tonic-gate 	    "Removing %s from the subgraph.\n", v->gv_name);
26557c478bd9Sstevel@tonic-gate 
26567c478bd9Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_INSUBGRAPH) |
26577c478bd9Sstevel@tonic-gate 	    (new ? GV_INSUBGRAPH : 0);
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 	if (v->gv_type == GVT_INST && (v->gv_flags & GV_CONFIGURED)) {
26607c478bd9Sstevel@tonic-gate 		int err;
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate get_inst:
26637c478bd9Sstevel@tonic-gate 		err = libscf_fmri_get_instance(h, v->gv_name, &inst);
26647c478bd9Sstevel@tonic-gate 		if (err != 0) {
26657c478bd9Sstevel@tonic-gate 			switch (err) {
26667c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
26677c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
26687c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
26697c478bd9Sstevel@tonic-gate 				goto get_inst;
26707c478bd9Sstevel@tonic-gate 
26717c478bd9Sstevel@tonic-gate 			case ENOENT:
26727c478bd9Sstevel@tonic-gate 				break;
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 			case EINVAL:
26757c478bd9Sstevel@tonic-gate 			case ENOTSUP:
26767c478bd9Sstevel@tonic-gate 			default:
26777c478bd9Sstevel@tonic-gate 				bad_error("libscf_fmri_get_instance", err);
26787c478bd9Sstevel@tonic-gate 			}
26797c478bd9Sstevel@tonic-gate 		} else {
26807c478bd9Sstevel@tonic-gate 			const char *f;
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate 			if (new) {
26837c478bd9Sstevel@tonic-gate 				err = libscf_delete_enable_ovr(inst);
26847c478bd9Sstevel@tonic-gate 				f = "libscf_delete_enable_ovr";
26857c478bd9Sstevel@tonic-gate 			} else {
26867c478bd9Sstevel@tonic-gate 				err = libscf_set_enable_ovr(inst, 0);
26877c478bd9Sstevel@tonic-gate 				f = "libscf_set_enable_ovr";
26887c478bd9Sstevel@tonic-gate 			}
26897c478bd9Sstevel@tonic-gate 			scf_instance_destroy(inst);
26907c478bd9Sstevel@tonic-gate 			switch (err) {
26917c478bd9Sstevel@tonic-gate 			case 0:
26927c478bd9Sstevel@tonic-gate 			case ECANCELED:
26937c478bd9Sstevel@tonic-gate 				break;
26947c478bd9Sstevel@tonic-gate 
26957c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
26967c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
26977c478bd9Sstevel@tonic-gate 				/*
26987c478bd9Sstevel@tonic-gate 				 * We must continue so the graph is updated,
26997c478bd9Sstevel@tonic-gate 				 * but we must return ECONNABORTED so any
27007c478bd9Sstevel@tonic-gate 				 * libscf state held by any callers is reset.
27017c478bd9Sstevel@tonic-gate 				 */
27027c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
27037c478bd9Sstevel@tonic-gate 				goto get_inst;
27047c478bd9Sstevel@tonic-gate 
27057c478bd9Sstevel@tonic-gate 			case EROFS:
27067c478bd9Sstevel@tonic-gate 			case EPERM:
27077c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING,
27087c478bd9Sstevel@tonic-gate 				    "Could not set %s/%s for %s: %s.\n",
27097c478bd9Sstevel@tonic-gate 				    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
27107c478bd9Sstevel@tonic-gate 				    v->gv_name, strerror(err));
27117c478bd9Sstevel@tonic-gate 				break;
27127c478bd9Sstevel@tonic-gate 
27137c478bd9Sstevel@tonic-gate 			default:
27147c478bd9Sstevel@tonic-gate 				bad_error(f, err);
27157c478bd9Sstevel@tonic-gate 			}
27167c478bd9Sstevel@tonic-gate 		}
27177c478bd9Sstevel@tonic-gate 	}
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
27207c478bd9Sstevel@tonic-gate 	    e != NULL;
27217c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
27227c478bd9Sstevel@tonic-gate 		r = eval_subgraph(e->ge_vertex, h);
27237c478bd9Sstevel@tonic-gate 		if (r != 0) {
27247c478bd9Sstevel@tonic-gate 			assert(r == ECONNABORTED);
27257c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
27267c478bd9Sstevel@tonic-gate 		}
27277c478bd9Sstevel@tonic-gate 	}
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate 	return (ret);
27307c478bd9Sstevel@tonic-gate }
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate /*
27337c478bd9Sstevel@tonic-gate  * Delete the (property group) dependencies of v & create new ones based on
27347c478bd9Sstevel@tonic-gate  * inst.  If doing so would create a cycle, log a message and put the instance
27357c478bd9Sstevel@tonic-gate  * into maintenance.  Update GV_INSUBGRAPH flags as necessary.  Returns 0 or
27367c478bd9Sstevel@tonic-gate  * ECONNABORTED.
27377c478bd9Sstevel@tonic-gate  */
273899b44c3bSlianep int
27397c478bd9Sstevel@tonic-gate refresh_vertex(graph_vertex_t *v, scf_instance_t *inst)
27407c478bd9Sstevel@tonic-gate {
27417c478bd9Sstevel@tonic-gate 	int err;
27427c478bd9Sstevel@tonic-gate 	int *path;
27437c478bd9Sstevel@tonic-gate 	char *fmri;
27447c478bd9Sstevel@tonic-gate 	int r;
27457c478bd9Sstevel@tonic-gate 	scf_handle_t *h = scf_instance_handle(inst);
27467c478bd9Sstevel@tonic-gate 	uu_list_t *old_deps;
27477c478bd9Sstevel@tonic-gate 	int ret = 0;
27487c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
27493ad28c1eSrm88369 	graph_vertex_t *vv;
27507c478bd9Sstevel@tonic-gate 
27517c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
27527c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph engine: Refreshing %s.\n", v->gv_name);
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
27577c478bd9Sstevel@tonic-gate 		/*
27587c478bd9Sstevel@tonic-gate 		 * In case some of v's dependencies are being deleted we must
27597c478bd9Sstevel@tonic-gate 		 * make a list of them now for GV_INSUBGRAPH-flag evaluation
27607c478bd9Sstevel@tonic-gate 		 * after the new dependencies are in place.
27617c478bd9Sstevel@tonic-gate 		 */
27627c478bd9Sstevel@tonic-gate 		old_deps = startd_list_create(graph_edge_pool, NULL, 0);
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate 		err = uu_list_walk(v->gv_dependencies,
27653ad28c1eSrm88369 		    (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0);
27667c478bd9Sstevel@tonic-gate 		assert(err == 0);
27677c478bd9Sstevel@tonic-gate 	}
27687c478bd9Sstevel@tonic-gate 
27697c478bd9Sstevel@tonic-gate 	delete_instance_dependencies(v, B_FALSE);
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 	err = set_dependencies(v, inst, &path);
27727c478bd9Sstevel@tonic-gate 	switch (err) {
27737c478bd9Sstevel@tonic-gate 	case 0:
27747c478bd9Sstevel@tonic-gate 		break;
27757c478bd9Sstevel@tonic-gate 
27767c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
27777c478bd9Sstevel@tonic-gate 		ret = err;
27787c478bd9Sstevel@tonic-gate 		goto out;
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	case EINVAL:
27817c478bd9Sstevel@tonic-gate 	case ELOOP:
27827c478bd9Sstevel@tonic-gate 		r = libscf_instance_get_fmri(inst, &fmri);
27837c478bd9Sstevel@tonic-gate 		switch (r) {
27847c478bd9Sstevel@tonic-gate 		case 0:
27857c478bd9Sstevel@tonic-gate 			break;
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
27887c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
27897c478bd9Sstevel@tonic-gate 			goto out;
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 		case ECANCELED:
27927c478bd9Sstevel@tonic-gate 			ret = 0;
27937c478bd9Sstevel@tonic-gate 			goto out;
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 		default:
27967c478bd9Sstevel@tonic-gate 			bad_error("libscf_instance_get_fmri", r);
27977c478bd9Sstevel@tonic-gate 		}
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 		if (err == EINVAL) {
28007c478bd9Sstevel@tonic-gate 			log_error(LOG_ERR, "Transitioning %s "
28017c478bd9Sstevel@tonic-gate 			    "to maintenance due to misconfiguration.\n",
28027c478bd9Sstevel@tonic-gate 			    fmri ? fmri : "?");
28037c478bd9Sstevel@tonic-gate 			vertex_send_event(v,
28047c478bd9Sstevel@tonic-gate 			    RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY);
28057c478bd9Sstevel@tonic-gate 		} else {
28067c478bd9Sstevel@tonic-gate 			handle_cycle(fmri, path);
28077c478bd9Sstevel@tonic-gate 			vertex_send_event(v,
28087c478bd9Sstevel@tonic-gate 			    RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE);
28097c478bd9Sstevel@tonic-gate 		}
28107c478bd9Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
28117c478bd9Sstevel@tonic-gate 		ret = 0;
28127c478bd9Sstevel@tonic-gate 		goto out;
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate 	default:
28157c478bd9Sstevel@tonic-gate 		bad_error("set_dependencies", err);
28167c478bd9Sstevel@tonic-gate 	}
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
28197c478bd9Sstevel@tonic-gate 		boolean_t aborted = B_FALSE;
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 		for (e = uu_list_first(old_deps);
28227c478bd9Sstevel@tonic-gate 		    e != NULL;
28237c478bd9Sstevel@tonic-gate 		    e = uu_list_next(old_deps, e)) {
28243ad28c1eSrm88369 			vv = e->ge_vertex;
28253ad28c1eSrm88369 
28263ad28c1eSrm88369 			if (vertex_unref(vv) == VERTEX_INUSE &&
28273ad28c1eSrm88369 			    eval_subgraph(vv, h) == ECONNABORTED)
28287c478bd9Sstevel@tonic-gate 				aborted = B_TRUE;
28297c478bd9Sstevel@tonic-gate 		}
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 		for (e = uu_list_first(v->gv_dependencies);
28327c478bd9Sstevel@tonic-gate 		    e != NULL;
28337c478bd9Sstevel@tonic-gate 		    e = uu_list_next(v->gv_dependencies, e)) {
28347c478bd9Sstevel@tonic-gate 			if (eval_subgraph(e->ge_vertex, h) ==
28357c478bd9Sstevel@tonic-gate 			    ECONNABORTED)
28367c478bd9Sstevel@tonic-gate 				aborted = B_TRUE;
28377c478bd9Sstevel@tonic-gate 		}
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate 		if (aborted) {
28407c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
28417c478bd9Sstevel@tonic-gate 			goto out;
28427c478bd9Sstevel@tonic-gate 		}
28437c478bd9Sstevel@tonic-gate 	}
28447c478bd9Sstevel@tonic-gate 
284599b44c3bSlianep 	graph_start_if_satisfied(v);
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	ret = 0;
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate out:
28507c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
28517c478bd9Sstevel@tonic-gate 		void *cookie = NULL;
28527c478bd9Sstevel@tonic-gate 
28537c478bd9Sstevel@tonic-gate 		while ((e = uu_list_teardown(old_deps, &cookie)) != NULL)
28547c478bd9Sstevel@tonic-gate 			startd_free(e, sizeof (*e));
28557c478bd9Sstevel@tonic-gate 
28567c478bd9Sstevel@tonic-gate 		uu_list_destroy(old_deps);
28577c478bd9Sstevel@tonic-gate 	}
28587c478bd9Sstevel@tonic-gate 
28597c478bd9Sstevel@tonic-gate 	return (ret);
28607c478bd9Sstevel@tonic-gate }
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate /*
28637c478bd9Sstevel@tonic-gate  * Set up v according to inst.  That is, make sure it depends on its
28647c478bd9Sstevel@tonic-gate  * restarter and set up its dependencies.  Send the ADD_INSTANCE command to
28657c478bd9Sstevel@tonic-gate  * the restarter, and send ENABLE or DISABLE as appropriate.
28667c478bd9Sstevel@tonic-gate  *
28677c478bd9Sstevel@tonic-gate  * Returns 0 on success, ECONNABORTED on repository disconnection, or
28687c478bd9Sstevel@tonic-gate  * ECANCELED if inst is deleted.
28697c478bd9Sstevel@tonic-gate  */
28707c478bd9Sstevel@tonic-gate static int
28717c478bd9Sstevel@tonic-gate configure_vertex(graph_vertex_t *v, scf_instance_t *inst)
28727c478bd9Sstevel@tonic-gate {
28737c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
28747c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
28757c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
28767c478bd9Sstevel@tonic-gate 	char *restarter_fmri = startd_alloc(max_scf_value_size);
28777c478bd9Sstevel@tonic-gate 	int enabled, enabled_ovr;
28787c478bd9Sstevel@tonic-gate 	int err;
28797c478bd9Sstevel@tonic-gate 	int *path;
2880*70cbfe41SPhilippe Jung 	int deathrow;
28817c478bd9Sstevel@tonic-gate 
28827c478bd9Sstevel@tonic-gate 	restarter_fmri[0] = '\0';
28837c478bd9Sstevel@tonic-gate 
28847c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
28857c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
28867c478bd9Sstevel@tonic-gate 	assert((v->gv_flags & GV_CONFIGURED) == 0);
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate 	/* GV_INSUBGRAPH should already be set properly. */
28897c478bd9Sstevel@tonic-gate 	assert(should_be_in_subgraph(v) ==
28907c478bd9Sstevel@tonic-gate 	    ((v->gv_flags & GV_INSUBGRAPH) != 0));
28917c478bd9Sstevel@tonic-gate 
2892*70cbfe41SPhilippe Jung 	/*
2893*70cbfe41SPhilippe Jung 	 * If the instance fmri is in the deathrow list then set the
2894*70cbfe41SPhilippe Jung 	 * GV_DEATHROW flag on the vertex and create and set to true the
2895*70cbfe41SPhilippe Jung 	 * SCF_PROPERTY_DEATHROW boolean property in the non-persistent
2896*70cbfe41SPhilippe Jung 	 * repository for this instance fmri.
2897*70cbfe41SPhilippe Jung 	 */
2898*70cbfe41SPhilippe Jung 	if ((v->gv_flags & GV_DEATHROW) ||
2899*70cbfe41SPhilippe Jung 	    (is_fmri_in_deathrow(v->gv_name) == B_TRUE)) {
2900*70cbfe41SPhilippe Jung 		if ((v->gv_flags & GV_DEATHROW) == 0) {
2901*70cbfe41SPhilippe Jung 			/*
2902*70cbfe41SPhilippe Jung 			 * Set flag GV_DEATHROW, create and set to true
2903*70cbfe41SPhilippe Jung 			 * the SCF_PROPERTY_DEATHROW property in the
2904*70cbfe41SPhilippe Jung 			 * non-persistent repository for this instance fmri.
2905*70cbfe41SPhilippe Jung 			 */
2906*70cbfe41SPhilippe Jung 			v->gv_flags |= GV_DEATHROW;
2907*70cbfe41SPhilippe Jung 
2908*70cbfe41SPhilippe Jung 			switch (err = libscf_set_deathrow(inst, 1)) {
2909*70cbfe41SPhilippe Jung 			case 0:
2910*70cbfe41SPhilippe Jung 				break;
2911*70cbfe41SPhilippe Jung 
2912*70cbfe41SPhilippe Jung 			case ECONNABORTED:
2913*70cbfe41SPhilippe Jung 			case ECANCELED:
2914*70cbfe41SPhilippe Jung 				startd_free(restarter_fmri, max_scf_value_size);
2915*70cbfe41SPhilippe Jung 				return (err);
2916*70cbfe41SPhilippe Jung 
2917*70cbfe41SPhilippe Jung 			case EROFS:
2918*70cbfe41SPhilippe Jung 				log_error(LOG_WARNING, "Could not set %s/%s "
2919*70cbfe41SPhilippe Jung 				    "for deathrow %s: %s.\n",
2920*70cbfe41SPhilippe Jung 				    SCF_PG_DEATHROW, SCF_PROPERTY_DEATHROW,
2921*70cbfe41SPhilippe Jung 				    v->gv_name, strerror(err));
2922*70cbfe41SPhilippe Jung 				break;
2923*70cbfe41SPhilippe Jung 
2924*70cbfe41SPhilippe Jung 			case EPERM:
2925*70cbfe41SPhilippe Jung 				uu_die("Permission denied.\n");
2926*70cbfe41SPhilippe Jung 				/* NOTREACHED */
2927*70cbfe41SPhilippe Jung 
2928*70cbfe41SPhilippe Jung 			default:
2929*70cbfe41SPhilippe Jung 				bad_error("libscf_set_deathrow", err);
2930*70cbfe41SPhilippe Jung 			}
2931*70cbfe41SPhilippe Jung 			log_framework(LOG_DEBUG, "Deathrow, graph set %s.\n",
2932*70cbfe41SPhilippe Jung 			    v->gv_name);
2933*70cbfe41SPhilippe Jung 		}
2934*70cbfe41SPhilippe Jung 		startd_free(restarter_fmri, max_scf_value_size);
2935*70cbfe41SPhilippe Jung 		return (0);
2936*70cbfe41SPhilippe Jung 	}
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(inst);
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate 	/*
2941*70cbfe41SPhilippe Jung 	 * Using a temporary deathrow boolean property, set through
2942*70cbfe41SPhilippe Jung 	 * libscf_set_deathrow(), only for fmris on deathrow, is necessary
2943*70cbfe41SPhilippe Jung 	 * because deathrow_fini() may already have been called, and in case
2944*70cbfe41SPhilippe Jung 	 * of a refresh, GV_DEATHROW may need to be set again.
2945*70cbfe41SPhilippe Jung 	 * libscf_get_deathrow() sets deathrow to 1 only if this instance
2946*70cbfe41SPhilippe Jung 	 * has a temporary boolean property named 'deathrow' valued true
2947*70cbfe41SPhilippe Jung 	 * in a property group 'deathrow', -1 or 0 in all other cases.
2948*70cbfe41SPhilippe Jung 	 */
2949*70cbfe41SPhilippe Jung 	err = libscf_get_deathrow(h, inst, &deathrow);
2950*70cbfe41SPhilippe Jung 	switch (err) {
2951*70cbfe41SPhilippe Jung 	case 0:
2952*70cbfe41SPhilippe Jung 		break;
2953*70cbfe41SPhilippe Jung 
2954*70cbfe41SPhilippe Jung 	case ECONNABORTED:
2955*70cbfe41SPhilippe Jung 	case ECANCELED:
2956*70cbfe41SPhilippe Jung 		startd_free(restarter_fmri, max_scf_value_size);
2957*70cbfe41SPhilippe Jung 		return (err);
2958*70cbfe41SPhilippe Jung 
2959*70cbfe41SPhilippe Jung 	default:
2960*70cbfe41SPhilippe Jung 		bad_error("libscf_get_deathrow", err);
2961*70cbfe41SPhilippe Jung 	}
2962*70cbfe41SPhilippe Jung 
2963*70cbfe41SPhilippe Jung 	if (deathrow == 1) {
2964*70cbfe41SPhilippe Jung 		v->gv_flags |= GV_DEATHROW;
2965*70cbfe41SPhilippe Jung 		startd_free(restarter_fmri, max_scf_value_size);
2966*70cbfe41SPhilippe Jung 		return (0);
2967*70cbfe41SPhilippe Jung 	}
2968*70cbfe41SPhilippe Jung 
2969*70cbfe41SPhilippe Jung 	log_framework(LOG_DEBUG, "Graph adding %s.\n", v->gv_name);
2970*70cbfe41SPhilippe Jung 
2971*70cbfe41SPhilippe Jung 	/*
29727c478bd9Sstevel@tonic-gate 	 * If the instance does not have a restarter property group,
29737c478bd9Sstevel@tonic-gate 	 * initialize its state to uninitialized/none, in case the restarter
29747c478bd9Sstevel@tonic-gate 	 * is not enabled.
29757c478bd9Sstevel@tonic-gate 	 */
29767c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) != 0) {
29797c478bd9Sstevel@tonic-gate 		instance_data_t idata;
29807c478bd9Sstevel@tonic-gate 		uint_t count = 0, msecs = ALLOC_DELAY;
29817c478bd9Sstevel@tonic-gate 
29827c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
29837c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
29847c478bd9Sstevel@tonic-gate 			break;
29857c478bd9Sstevel@tonic-gate 
29867c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
29877c478bd9Sstevel@tonic-gate 		default:
29887c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
29897c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
29927c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
29937c478bd9Sstevel@tonic-gate 			return (ECANCELED);
29947c478bd9Sstevel@tonic-gate 
29957c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
29967c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
29977c478bd9Sstevel@tonic-gate 		}
29987c478bd9Sstevel@tonic-gate 
29997c478bd9Sstevel@tonic-gate 		switch (err = libscf_instance_get_fmri(inst,
30007c478bd9Sstevel@tonic-gate 		    (char **)&idata.i_fmri)) {
30017c478bd9Sstevel@tonic-gate 		case 0:
30027c478bd9Sstevel@tonic-gate 			break;
30037c478bd9Sstevel@tonic-gate 
30047c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
30057c478bd9Sstevel@tonic-gate 		case ECANCELED:
30067c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
30077c478bd9Sstevel@tonic-gate 			return (err);
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate 		default:
30107c478bd9Sstevel@tonic-gate 			bad_error("libscf_instance_get_fmri", err);
30117c478bd9Sstevel@tonic-gate 		}
30127c478bd9Sstevel@tonic-gate 
30137c478bd9Sstevel@tonic-gate 		idata.i_state = RESTARTER_STATE_NONE;
30147c478bd9Sstevel@tonic-gate 		idata.i_next_state = RESTARTER_STATE_NONE;
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate init_state:
30177c478bd9Sstevel@tonic-gate 		switch (err = _restarter_commit_states(h, &idata,
30187c478bd9Sstevel@tonic-gate 		    RESTARTER_STATE_UNINIT, RESTARTER_STATE_NONE, NULL)) {
30197c478bd9Sstevel@tonic-gate 		case 0:
30207c478bd9Sstevel@tonic-gate 			break;
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate 		case ENOMEM:
30237c478bd9Sstevel@tonic-gate 			++count;
30247c478bd9Sstevel@tonic-gate 			if (count < ALLOC_RETRY) {
30257c478bd9Sstevel@tonic-gate 				(void) poll(NULL, 0, msecs);
30267c478bd9Sstevel@tonic-gate 				msecs *= ALLOC_DELAY_MULT;
30277c478bd9Sstevel@tonic-gate 				goto init_state;
30287c478bd9Sstevel@tonic-gate 			}
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate 			uu_die("Insufficient memory.\n");
30317c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
30327c478bd9Sstevel@tonic-gate 
30337c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
30345b6c5443Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
30357c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
30367c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
30377c478bd9Sstevel@tonic-gate 
30387c478bd9Sstevel@tonic-gate 		case ENOENT:
30395b6c5443Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
30407c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
30417c478bd9Sstevel@tonic-gate 			return (ECANCELED);
30427c478bd9Sstevel@tonic-gate 
30437c478bd9Sstevel@tonic-gate 		case EPERM:
30447c478bd9Sstevel@tonic-gate 		case EACCES:
30457c478bd9Sstevel@tonic-gate 		case EROFS:
30467c478bd9Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not initialize state for "
30477c478bd9Sstevel@tonic-gate 			    "%s: %s.\n", idata.i_fmri, strerror(err));
30487c478bd9Sstevel@tonic-gate 			break;
30497c478bd9Sstevel@tonic-gate 
30507c478bd9Sstevel@tonic-gate 		case EINVAL:
30517c478bd9Sstevel@tonic-gate 		default:
30527c478bd9Sstevel@tonic-gate 			bad_error("_restarter_commit_states", err);
30537c478bd9Sstevel@tonic-gate 		}
30547c478bd9Sstevel@tonic-gate 
30557c478bd9Sstevel@tonic-gate 		startd_free((void *)idata.i_fmri, max_scf_fmri_size);
30567c478bd9Sstevel@tonic-gate 	}
30577c478bd9Sstevel@tonic-gate 
30587c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
30597c478bd9Sstevel@tonic-gate 
30607c478bd9Sstevel@tonic-gate 	if (milestone != NULL) {
30617c478bd9Sstevel@tonic-gate 		/*
30627c478bd9Sstevel@tonic-gate 		 * Make sure the enable-override is set properly before we
30637c478bd9Sstevel@tonic-gate 		 * read whether we should be enabled.
30647c478bd9Sstevel@tonic-gate 		 */
30657c478bd9Sstevel@tonic-gate 		if (milestone == MILESTONE_NONE ||
30667c478bd9Sstevel@tonic-gate 		    !(v->gv_flags & GV_INSUBGRAPH)) {
306756e23938Sbustos 			/*
306856e23938Sbustos 			 * This might seem unjustified after the milestone
306956e23938Sbustos 			 * transition has completed (non_subgraph_svcs == 0),
307056e23938Sbustos 			 * but it's important because when we boot to
307156e23938Sbustos 			 * a milestone, we set the milestone before populating
307256e23938Sbustos 			 * the graph, and all of the new non-subgraph services
307356e23938Sbustos 			 * need to be disabled here.
307456e23938Sbustos 			 */
30757c478bd9Sstevel@tonic-gate 			switch (err = libscf_set_enable_ovr(inst, 0)) {
30767c478bd9Sstevel@tonic-gate 			case 0:
30777c478bd9Sstevel@tonic-gate 				break;
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
30807c478bd9Sstevel@tonic-gate 			case ECANCELED:
30817c478bd9Sstevel@tonic-gate 				return (err);
30827c478bd9Sstevel@tonic-gate 
30837c478bd9Sstevel@tonic-gate 			case EROFS:
30847c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING,
30857c478bd9Sstevel@tonic-gate 				    "Could not set %s/%s for %s: %s.\n",
30867c478bd9Sstevel@tonic-gate 				    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
30877c478bd9Sstevel@tonic-gate 				    v->gv_name, strerror(err));
30887c478bd9Sstevel@tonic-gate 				break;
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate 			case EPERM:
30917c478bd9Sstevel@tonic-gate 				uu_die("Permission denied.\n");
30927c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
30937c478bd9Sstevel@tonic-gate 
30947c478bd9Sstevel@tonic-gate 			default:
30957c478bd9Sstevel@tonic-gate 				bad_error("libscf_set_enable_ovr", err);
30967c478bd9Sstevel@tonic-gate 			}
30977c478bd9Sstevel@tonic-gate 		} else {
30987c478bd9Sstevel@tonic-gate 			assert(v->gv_flags & GV_INSUBGRAPH);
30997c478bd9Sstevel@tonic-gate 			switch (err = libscf_delete_enable_ovr(inst)) {
31007c478bd9Sstevel@tonic-gate 			case 0:
31017c478bd9Sstevel@tonic-gate 				break;
31027c478bd9Sstevel@tonic-gate 
31037c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
31047c478bd9Sstevel@tonic-gate 			case ECANCELED:
31057c478bd9Sstevel@tonic-gate 				return (err);
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate 			case EPERM:
31087c478bd9Sstevel@tonic-gate 				uu_die("Permission denied.\n");
31097c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
31107c478bd9Sstevel@tonic-gate 
31117c478bd9Sstevel@tonic-gate 			default:
31127c478bd9Sstevel@tonic-gate 				bad_error("libscf_delete_enable_ovr", err);
31137c478bd9Sstevel@tonic-gate 			}
31147c478bd9Sstevel@tonic-gate 		}
31157c478bd9Sstevel@tonic-gate 	}
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate 	err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled,
31187c478bd9Sstevel@tonic-gate 	    &enabled_ovr, &restarter_fmri);
31197c478bd9Sstevel@tonic-gate 	switch (err) {
31207c478bd9Sstevel@tonic-gate 	case 0:
31217c478bd9Sstevel@tonic-gate 		break;
31227c478bd9Sstevel@tonic-gate 
31237c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
31247c478bd9Sstevel@tonic-gate 	case ECANCELED:
31257c478bd9Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
31267c478bd9Sstevel@tonic-gate 		return (err);
31277c478bd9Sstevel@tonic-gate 
31287c478bd9Sstevel@tonic-gate 	case ENOENT:
31297c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
31307c478bd9Sstevel@tonic-gate 		    "Ignoring %s because it has no general property group.\n",
31317c478bd9Sstevel@tonic-gate 		    v->gv_name);
31327c478bd9Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
31337c478bd9Sstevel@tonic-gate 		return (0);
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate 	default:
31367c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_basic_instance_data", err);
31377c478bd9Sstevel@tonic-gate 	}
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 	if (enabled == -1) {
31407c478bd9Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
31417c478bd9Sstevel@tonic-gate 		return (0);
31427c478bd9Sstevel@tonic-gate 	}
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) |
31457c478bd9Sstevel@tonic-gate 	    (enabled ? GV_ENBLD_NOOVR : 0);
31467c478bd9Sstevel@tonic-gate 
31477c478bd9Sstevel@tonic-gate 	if (enabled_ovr != -1)
31487c478bd9Sstevel@tonic-gate 		enabled = enabled_ovr;
31497c478bd9Sstevel@tonic-gate 
31507c478bd9Sstevel@tonic-gate 	v->gv_state = RESTARTER_STATE_UNINIT;
31517c478bd9Sstevel@tonic-gate 
31527c478bd9Sstevel@tonic-gate 	snap = libscf_get_or_make_running_snapshot(inst, v->gv_name, B_TRUE);
31537c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate 	/* Set up the restarter. (Sends _ADD_INSTANCE on success.) */
31567c478bd9Sstevel@tonic-gate 	err = graph_change_restarter(v, restarter_fmri, h, &path);
31577c478bd9Sstevel@tonic-gate 	if (err != 0) {
31587c478bd9Sstevel@tonic-gate 		instance_data_t idata;
31597c478bd9Sstevel@tonic-gate 		uint_t count = 0, msecs = ALLOC_DELAY;
31607c478bd9Sstevel@tonic-gate 		const char *reason;
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 		if (err == ECONNABORTED) {
31637c478bd9Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
31647c478bd9Sstevel@tonic-gate 			return (err);
31657c478bd9Sstevel@tonic-gate 		}
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 		assert(err == EINVAL || err == ELOOP);
31687c478bd9Sstevel@tonic-gate 
31697c478bd9Sstevel@tonic-gate 		if (err == EINVAL) {
317099b44c3bSlianep 			log_framework(LOG_ERR, emsg_invalid_restarter,
31717c478bd9Sstevel@tonic-gate 			    v->gv_name);
31727c478bd9Sstevel@tonic-gate 			reason = "invalid_restarter";
31737c478bd9Sstevel@tonic-gate 		} else {
31747c478bd9Sstevel@tonic-gate 			handle_cycle(v->gv_name, path);
31757c478bd9Sstevel@tonic-gate 			reason = "dependency_cycle";
31767c478bd9Sstevel@tonic-gate 		}
31777c478bd9Sstevel@tonic-gate 
31787c478bd9Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate 		/*
31817c478bd9Sstevel@tonic-gate 		 * We didn't register the instance with the restarter, so we
31827c478bd9Sstevel@tonic-gate 		 * must set maintenance mode ourselves.
31837c478bd9Sstevel@tonic-gate 		 */
31847c478bd9Sstevel@tonic-gate 		err = libscf_instance_get_fmri(inst, (char **)&idata.i_fmri);
31857c478bd9Sstevel@tonic-gate 		if (err != 0) {
31867c478bd9Sstevel@tonic-gate 			assert(err == ECONNABORTED || err == ECANCELED);
31877c478bd9Sstevel@tonic-gate 			return (err);
31887c478bd9Sstevel@tonic-gate 		}
31897c478bd9Sstevel@tonic-gate 
31907c478bd9Sstevel@tonic-gate 		idata.i_state = RESTARTER_STATE_NONE;
31917c478bd9Sstevel@tonic-gate 		idata.i_next_state = RESTARTER_STATE_NONE;
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate set_maint:
31947c478bd9Sstevel@tonic-gate 		switch (err = _restarter_commit_states(h, &idata,
31957c478bd9Sstevel@tonic-gate 		    RESTARTER_STATE_MAINT, RESTARTER_STATE_NONE, reason)) {
31967c478bd9Sstevel@tonic-gate 		case 0:
31977c478bd9Sstevel@tonic-gate 			break;
31987c478bd9Sstevel@tonic-gate 
31997c478bd9Sstevel@tonic-gate 		case ENOMEM:
32007c478bd9Sstevel@tonic-gate 			++count;
32017c478bd9Sstevel@tonic-gate 			if (count < ALLOC_RETRY) {
32027c478bd9Sstevel@tonic-gate 				(void) poll(NULL, 0, msecs);
32037c478bd9Sstevel@tonic-gate 				msecs *= ALLOC_DELAY_MULT;
32047c478bd9Sstevel@tonic-gate 				goto set_maint;
32057c478bd9Sstevel@tonic-gate 			}
32067c478bd9Sstevel@tonic-gate 
32077c478bd9Sstevel@tonic-gate 			uu_die("Insufficient memory.\n");
32087c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
32115b6c5443Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
32127c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
32137c478bd9Sstevel@tonic-gate 
32147c478bd9Sstevel@tonic-gate 		case ENOENT:
32155b6c5443Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
32167c478bd9Sstevel@tonic-gate 			return (ECANCELED);
32177c478bd9Sstevel@tonic-gate 
32187c478bd9Sstevel@tonic-gate 		case EPERM:
32197c478bd9Sstevel@tonic-gate 		case EACCES:
32207c478bd9Sstevel@tonic-gate 		case EROFS:
32217c478bd9Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not initialize state for "
32227c478bd9Sstevel@tonic-gate 			    "%s: %s.\n", idata.i_fmri, strerror(err));
32237c478bd9Sstevel@tonic-gate 			break;
32247c478bd9Sstevel@tonic-gate 
32257c478bd9Sstevel@tonic-gate 		case EINVAL:
32267c478bd9Sstevel@tonic-gate 		default:
32277c478bd9Sstevel@tonic-gate 			bad_error("_restarter_commit_states", err);
32287c478bd9Sstevel@tonic-gate 		}
32297c478bd9Sstevel@tonic-gate 
32307c478bd9Sstevel@tonic-gate 		startd_free((void *)idata.i_fmri, max_scf_fmri_size);
32317c478bd9Sstevel@tonic-gate 
32327c478bd9Sstevel@tonic-gate 		v->gv_state = RESTARTER_STATE_MAINT;
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 		goto out;
32357c478bd9Sstevel@tonic-gate 	}
32367c478bd9Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_value_size);
32377c478bd9Sstevel@tonic-gate 
32387c478bd9Sstevel@tonic-gate 	/* Add all the other dependencies. */
32397c478bd9Sstevel@tonic-gate 	err = refresh_vertex(v, inst);
32407c478bd9Sstevel@tonic-gate 	if (err != 0) {
32417c478bd9Sstevel@tonic-gate 		assert(err == ECONNABORTED);
32427c478bd9Sstevel@tonic-gate 		return (err);
32437c478bd9Sstevel@tonic-gate 	}
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate out:
32467c478bd9Sstevel@tonic-gate 	v->gv_flags |= GV_CONFIGURED;
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate 	graph_enable_by_vertex(v, enabled, 0);
32497c478bd9Sstevel@tonic-gate 
32507c478bd9Sstevel@tonic-gate 	return (0);
32517c478bd9Sstevel@tonic-gate }
32527c478bd9Sstevel@tonic-gate 
32537c478bd9Sstevel@tonic-gate static void
32547c478bd9Sstevel@tonic-gate do_uadmin(void)
32557c478bd9Sstevel@tonic-gate {
32567c478bd9Sstevel@tonic-gate 	int fd, left;
32577c478bd9Sstevel@tonic-gate 	struct statvfs vfs;
32587c478bd9Sstevel@tonic-gate 
32597c478bd9Sstevel@tonic-gate 	const char * const resetting = "/etc/svc/volatile/resetting";
32607c478bd9Sstevel@tonic-gate 
32617c478bd9Sstevel@tonic-gate 	fd = creat(resetting, 0777);
32627c478bd9Sstevel@tonic-gate 	if (fd >= 0)
32637c478bd9Sstevel@tonic-gate 		startd_close(fd);
32647c478bd9Sstevel@tonic-gate 	else
32657c478bd9Sstevel@tonic-gate 		uu_warn("Could not create \"%s\"", resetting);
32667c478bd9Sstevel@tonic-gate 
32677c478bd9Sstevel@tonic-gate 	/* Kill dhcpagent if we're not using nfs for root */
32687c478bd9Sstevel@tonic-gate 	if ((statvfs("/", &vfs) == 0) &&
32697c478bd9Sstevel@tonic-gate 	    (strncmp(vfs.f_basetype, "nfs", sizeof ("nfs") - 1) != 0))
32707c478bd9Sstevel@tonic-gate 		(void) system("/usr/bin/pkill -x -u 0 dhcpagent");
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate 	(void) system("/usr/sbin/killall");
32737c478bd9Sstevel@tonic-gate 	left = 5;
32747c478bd9Sstevel@tonic-gate 	while (left > 0)
32757c478bd9Sstevel@tonic-gate 		left = sleep(left);
32767c478bd9Sstevel@tonic-gate 
32777c478bd9Sstevel@tonic-gate 	(void) system("/usr/sbin/killall 9");
32787c478bd9Sstevel@tonic-gate 	left = 10;
32797c478bd9Sstevel@tonic-gate 	while (left > 0)
32807c478bd9Sstevel@tonic-gate 		left = sleep(left);
32817c478bd9Sstevel@tonic-gate 
32827c478bd9Sstevel@tonic-gate 	sync();
32837c478bd9Sstevel@tonic-gate 	sync();
32847c478bd9Sstevel@tonic-gate 	sync();
32857c478bd9Sstevel@tonic-gate 
3286505b14d1SPavel Filipensky 	(void) system("/sbin/umountall -l");
32877c478bd9Sstevel@tonic-gate 	(void) system("/sbin/umount /tmp >/dev/null 2>&1");
32887c478bd9Sstevel@tonic-gate 	(void) system("/sbin/umount /var/adm >/dev/null 2>&1");
32897c478bd9Sstevel@tonic-gate 	(void) system("/sbin/umount /var/run >/dev/null 2>&1");
32907c478bd9Sstevel@tonic-gate 	(void) system("/sbin/umount /var >/dev/null 2>&1");
32917c478bd9Sstevel@tonic-gate 	(void) system("/sbin/umount /usr >/dev/null 2>&1");
32927c478bd9Sstevel@tonic-gate 
32937c478bd9Sstevel@tonic-gate 	uu_warn("The system is down.\n");
32947c478bd9Sstevel@tonic-gate 
32957c478bd9Sstevel@tonic-gate 	(void) uadmin(A_SHUTDOWN, halting, NULL);
32967c478bd9Sstevel@tonic-gate 	uu_warn("uadmin() failed");
32977c478bd9Sstevel@tonic-gate 
32987c478bd9Sstevel@tonic-gate 	if (remove(resetting) != 0 && errno != ENOENT)
32997c478bd9Sstevel@tonic-gate 		uu_warn("Could not remove \"%s\"", resetting);
33007c478bd9Sstevel@tonic-gate }
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate /*
33037c478bd9Sstevel@tonic-gate  * If any of the up_svcs[] are online or satisfiable, return true.  If they are
33047c478bd9Sstevel@tonic-gate  * all missing, disabled, in maintenance, or unsatisfiable, return false.
33057c478bd9Sstevel@tonic-gate  */
33067c478bd9Sstevel@tonic-gate boolean_t
33077c478bd9Sstevel@tonic-gate can_come_up(void)
33087c478bd9Sstevel@tonic-gate {
33097c478bd9Sstevel@tonic-gate 	int i;
33107c478bd9Sstevel@tonic-gate 
33117c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate 	/*
33147c478bd9Sstevel@tonic-gate 	 * If we are booting to single user (boot -s),
33157c478bd9Sstevel@tonic-gate 	 * SCF_MILESTONE_SINGLE_USER is needed to come up because startd
33167c478bd9Sstevel@tonic-gate 	 * spawns sulogin after single-user is online (see specials.c).
33177c478bd9Sstevel@tonic-gate 	 */
33187c478bd9Sstevel@tonic-gate 	i = (booting_to_single_user ? 0 : 1);
33197c478bd9Sstevel@tonic-gate 
33207c478bd9Sstevel@tonic-gate 	for (; up_svcs[i] != NULL; ++i) {
33217c478bd9Sstevel@tonic-gate 		if (up_svcs_p[i] == NULL) {
33227c478bd9Sstevel@tonic-gate 			up_svcs_p[i] = vertex_get_by_name(up_svcs[i]);
33237c478bd9Sstevel@tonic-gate 
33247c478bd9Sstevel@tonic-gate 			if (up_svcs_p[i] == NULL)
33257c478bd9Sstevel@tonic-gate 				continue;
33267c478bd9Sstevel@tonic-gate 		}
33277c478bd9Sstevel@tonic-gate 
33287c478bd9Sstevel@tonic-gate 		/*
33297c478bd9Sstevel@tonic-gate 		 * Ignore unconfigured services (the ones that have been
33307c478bd9Sstevel@tonic-gate 		 * mentioned in a dependency from other services, but do
33317c478bd9Sstevel@tonic-gate 		 * not exist in the repository).  Services which exist
33327c478bd9Sstevel@tonic-gate 		 * in the repository but don't have general/enabled
33337c478bd9Sstevel@tonic-gate 		 * property will be also ignored.
33347c478bd9Sstevel@tonic-gate 		 */
33357c478bd9Sstevel@tonic-gate 		if (!(up_svcs_p[i]->gv_flags & GV_CONFIGURED))
33367c478bd9Sstevel@tonic-gate 			continue;
33377c478bd9Sstevel@tonic-gate 
33387c478bd9Sstevel@tonic-gate 		switch (up_svcs_p[i]->gv_state) {
33397c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_ONLINE:
33407c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_DEGRADED:
33417c478bd9Sstevel@tonic-gate 			/*
33427c478bd9Sstevel@tonic-gate 			 * Deactivate verbose boot once a login service has been
33437c478bd9Sstevel@tonic-gate 			 * reached.
33447c478bd9Sstevel@tonic-gate 			 */
33457c478bd9Sstevel@tonic-gate 			st->st_log_login_reached = 1;
33467c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
33477c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_UNINIT:
33487c478bd9Sstevel@tonic-gate 			return (B_TRUE);
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_OFFLINE:
33517c478bd9Sstevel@tonic-gate 			if (instance_satisfied(up_svcs_p[i], B_TRUE) != -1)
33527c478bd9Sstevel@tonic-gate 				return (B_TRUE);
33537c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
33547c478bd9Sstevel@tonic-gate 			    "can_come_up(): %s is unsatisfiable.\n",
33557c478bd9Sstevel@tonic-gate 			    up_svcs_p[i]->gv_name);
33567c478bd9Sstevel@tonic-gate 			continue;
33577c478bd9Sstevel@tonic-gate 
33587c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_DISABLED:
33597c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_MAINT:
33607c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
33617c478bd9Sstevel@tonic-gate 			    "can_come_up(): %s is in state %s.\n",
33627c478bd9Sstevel@tonic-gate 			    up_svcs_p[i]->gv_name,
33637c478bd9Sstevel@tonic-gate 			    instance_state_str[up_svcs_p[i]->gv_state]);
33647c478bd9Sstevel@tonic-gate 			continue;
33657c478bd9Sstevel@tonic-gate 
33667c478bd9Sstevel@tonic-gate 		default:
33677c478bd9Sstevel@tonic-gate #ifndef NDEBUG
33687c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex state %d.\n",
33697c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__, up_svcs_p[i]->gv_state);
33707c478bd9Sstevel@tonic-gate #endif
33717c478bd9Sstevel@tonic-gate 			abort();
33727c478bd9Sstevel@tonic-gate 		}
33737c478bd9Sstevel@tonic-gate 	}
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate 	/*
33767c478bd9Sstevel@tonic-gate 	 * In the seed repository, console-login is unsatisfiable because
33777c478bd9Sstevel@tonic-gate 	 * services are missing.  To behave correctly in that case we don't want
33787c478bd9Sstevel@tonic-gate 	 * to return false until manifest-import is online.
33797c478bd9Sstevel@tonic-gate 	 */
33807c478bd9Sstevel@tonic-gate 
33817c478bd9Sstevel@tonic-gate 	if (manifest_import_p == NULL) {
33827c478bd9Sstevel@tonic-gate 		manifest_import_p = vertex_get_by_name(manifest_import);
33837c478bd9Sstevel@tonic-gate 
33847c478bd9Sstevel@tonic-gate 		if (manifest_import_p == NULL)
33857c478bd9Sstevel@tonic-gate 			return (B_FALSE);
33867c478bd9Sstevel@tonic-gate 	}
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate 	switch (manifest_import_p->gv_state) {
33897c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_ONLINE:
33907c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_DEGRADED:
33917c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_DISABLED:
33927c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_MAINT:
33937c478bd9Sstevel@tonic-gate 		break;
33947c478bd9Sstevel@tonic-gate 
33957c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_OFFLINE:
33967c478bd9Sstevel@tonic-gate 		if (instance_satisfied(manifest_import_p, B_TRUE) == -1)
33977c478bd9Sstevel@tonic-gate 			break;
33987c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_UNINIT:
34017c478bd9Sstevel@tonic-gate 		return (B_TRUE);
34027c478bd9Sstevel@tonic-gate 	}
34037c478bd9Sstevel@tonic-gate 
34047c478bd9Sstevel@tonic-gate 	return (B_FALSE);
34057c478bd9Sstevel@tonic-gate }
34067c478bd9Sstevel@tonic-gate 
34077c478bd9Sstevel@tonic-gate /*
34087c478bd9Sstevel@tonic-gate  * Runs sulogin.  Returns
34097c478bd9Sstevel@tonic-gate  *   0 - success
34107c478bd9Sstevel@tonic-gate  *   EALREADY - sulogin is already running
34117c478bd9Sstevel@tonic-gate  *   EBUSY - console-login is running
34127c478bd9Sstevel@tonic-gate  */
34137c478bd9Sstevel@tonic-gate static int
34147c478bd9Sstevel@tonic-gate run_sulogin(const char *msg)
34157c478bd9Sstevel@tonic-gate {
34167c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
34177c478bd9Sstevel@tonic-gate 
34187c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate 	if (sulogin_running)
34217c478bd9Sstevel@tonic-gate 		return (EALREADY);
34227c478bd9Sstevel@tonic-gate 
34237c478bd9Sstevel@tonic-gate 	v = vertex_get_by_name(console_login_fmri);
34247c478bd9Sstevel@tonic-gate 	if (v != NULL && inst_running(v))
34257c478bd9Sstevel@tonic-gate 		return (EBUSY);
34267c478bd9Sstevel@tonic-gate 
34277c478bd9Sstevel@tonic-gate 	sulogin_running = B_TRUE;
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
34307c478bd9Sstevel@tonic-gate 
34317c478bd9Sstevel@tonic-gate 	fork_sulogin(B_FALSE, msg);
34327c478bd9Sstevel@tonic-gate 
34337c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
34347c478bd9Sstevel@tonic-gate 
34357c478bd9Sstevel@tonic-gate 	sulogin_running = B_FALSE;
34367c478bd9Sstevel@tonic-gate 
34377c478bd9Sstevel@tonic-gate 	if (console_login_ready) {
34387c478bd9Sstevel@tonic-gate 		v = vertex_get_by_name(console_login_fmri);
34397c478bd9Sstevel@tonic-gate 
34407c478bd9Sstevel@tonic-gate 		if (v != NULL && v->gv_state == RESTARTER_STATE_OFFLINE &&
34417c478bd9Sstevel@tonic-gate 		    !inst_running(v)) {
34427c478bd9Sstevel@tonic-gate 			if (v->gv_start_f == NULL)
34437c478bd9Sstevel@tonic-gate 				vertex_send_event(v,
34447c478bd9Sstevel@tonic-gate 				    RESTARTER_EVENT_TYPE_START);
34457c478bd9Sstevel@tonic-gate 			else
34467c478bd9Sstevel@tonic-gate 				v->gv_start_f(v);
34477c478bd9Sstevel@tonic-gate 		}
34487c478bd9Sstevel@tonic-gate 
34497c478bd9Sstevel@tonic-gate 		console_login_ready = B_FALSE;
34507c478bd9Sstevel@tonic-gate 	}
34517c478bd9Sstevel@tonic-gate 
34527c478bd9Sstevel@tonic-gate 	return (0);
34537c478bd9Sstevel@tonic-gate }
34547c478bd9Sstevel@tonic-gate 
34557c478bd9Sstevel@tonic-gate /*
34567c478bd9Sstevel@tonic-gate  * The sulogin thread runs sulogin while can_come_up() is false.  run_sulogin()
34577c478bd9Sstevel@tonic-gate  * keeps sulogin from stepping on console-login's toes.
34587c478bd9Sstevel@tonic-gate  */
34597c478bd9Sstevel@tonic-gate /* ARGSUSED */
34607c478bd9Sstevel@tonic-gate static void *
34617c478bd9Sstevel@tonic-gate sulogin_thread(void *unused)
34627c478bd9Sstevel@tonic-gate {
34637c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
34647c478bd9Sstevel@tonic-gate 
34657c478bd9Sstevel@tonic-gate 	assert(sulogin_thread_running);
34667c478bd9Sstevel@tonic-gate 
34673eae19d9Swesolows 	do {
34687c478bd9Sstevel@tonic-gate 		(void) run_sulogin("Console login service(s) cannot run\n");
34693eae19d9Swesolows 	} while (!can_come_up());
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate 	sulogin_thread_running = B_FALSE;
34727c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate 	return (NULL);
34757c478bd9Sstevel@tonic-gate }
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate /* ARGSUSED */
34787c478bd9Sstevel@tonic-gate void *
34797c478bd9Sstevel@tonic-gate single_user_thread(void *unused)
34807c478bd9Sstevel@tonic-gate {
34817c478bd9Sstevel@tonic-gate 	uint_t left;
34827c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
34837c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
34847c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
34857c478bd9Sstevel@tonic-gate 	scf_value_t *val;
34867c478bd9Sstevel@tonic-gate 	const char *msg;
34877c478bd9Sstevel@tonic-gate 	char *buf;
34887c478bd9Sstevel@tonic-gate 	int r;
34897c478bd9Sstevel@tonic-gate 
34907c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&single_user_thread_lock);
34917c478bd9Sstevel@tonic-gate 	single_user_thread_count++;
34927c478bd9Sstevel@tonic-gate 
34937c478bd9Sstevel@tonic-gate 	if (!booting_to_single_user) {
34947c478bd9Sstevel@tonic-gate 		/*
34957c478bd9Sstevel@tonic-gate 		 * From rcS.sh: Look for ttymon, in.telnetd, in.rlogind and
34967c478bd9Sstevel@tonic-gate 		 * processes in their process groups so they can be terminated.
34977c478bd9Sstevel@tonic-gate 		 */
34987c478bd9Sstevel@tonic-gate 		(void) fputs("svc.startd: Killing user processes: ", stdout);
34997c478bd9Sstevel@tonic-gate 		(void) system("/usr/sbin/killall");
35007c478bd9Sstevel@tonic-gate 		(void) system("/usr/sbin/killall 9");
35017c478bd9Sstevel@tonic-gate 		(void) system("/usr/bin/pkill -TERM -v -u 0,1");
35027c478bd9Sstevel@tonic-gate 
35037c478bd9Sstevel@tonic-gate 		left = 5;
35047c478bd9Sstevel@tonic-gate 		while (left > 0)
35057c478bd9Sstevel@tonic-gate 			left = sleep(left);
35067c478bd9Sstevel@tonic-gate 
35077c478bd9Sstevel@tonic-gate 		(void) system("/usr/bin/pkill -KILL -v -u 0,1");
35087c478bd9Sstevel@tonic-gate 		(void) puts("done.");
35097c478bd9Sstevel@tonic-gate 	}
35107c478bd9Sstevel@tonic-gate 
35117c478bd9Sstevel@tonic-gate 	if (go_single_user_mode || booting_to_single_user) {
35127c478bd9Sstevel@tonic-gate 		msg = "SINGLE USER MODE\n";
35137c478bd9Sstevel@tonic-gate 	} else {
35147c478bd9Sstevel@tonic-gate 		assert(go_to_level1);
35157c478bd9Sstevel@tonic-gate 
35167c478bd9Sstevel@tonic-gate 		fork_rc_script('1', "start", B_TRUE);
35177c478bd9Sstevel@tonic-gate 
35187c478bd9Sstevel@tonic-gate 		uu_warn("The system is ready for administration.\n");
35197c478bd9Sstevel@tonic-gate 
35207c478bd9Sstevel@tonic-gate 		msg = "";
35217c478bd9Sstevel@tonic-gate 	}
35227c478bd9Sstevel@tonic-gate 
35237c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&single_user_thread_lock);
35247c478bd9Sstevel@tonic-gate 
35257c478bd9Sstevel@tonic-gate 	for (;;) {
35267c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&dgraph_lock);
35277c478bd9Sstevel@tonic-gate 		r = run_sulogin(msg);
35287c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
35297c478bd9Sstevel@tonic-gate 		if (r == 0)
35307c478bd9Sstevel@tonic-gate 			break;
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate 		assert(r == EALREADY || r == EBUSY);
35337c478bd9Sstevel@tonic-gate 
35347c478bd9Sstevel@tonic-gate 		left = 3;
35357c478bd9Sstevel@tonic-gate 		while (left > 0)
35367c478bd9Sstevel@tonic-gate 			left = sleep(left);
35377c478bd9Sstevel@tonic-gate 	}
35387c478bd9Sstevel@tonic-gate 
35397c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&single_user_thread_lock);
35407c478bd9Sstevel@tonic-gate 
35417c478bd9Sstevel@tonic-gate 	/*
35427c478bd9Sstevel@tonic-gate 	 * If another single user thread has started, let it finish changing
35437c478bd9Sstevel@tonic-gate 	 * the run level.
35447c478bd9Sstevel@tonic-gate 	 */
35457c478bd9Sstevel@tonic-gate 	if (single_user_thread_count > 1) {
35467c478bd9Sstevel@tonic-gate 		single_user_thread_count--;
35477c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&single_user_thread_lock);
35487c478bd9Sstevel@tonic-gate 		return (NULL);
35497c478bd9Sstevel@tonic-gate 	}
35507c478bd9Sstevel@tonic-gate 
35517c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
35527c478bd9Sstevel@tonic-gate 	inst = scf_instance_create(h);
35537c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
35547c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
35557c478bd9Sstevel@tonic-gate 	buf = startd_alloc(max_scf_fmri_size);
35567c478bd9Sstevel@tonic-gate 
35577c478bd9Sstevel@tonic-gate lookup:
35587c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
35597c478bd9Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
35607c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
35617c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
35627c478bd9Sstevel@tonic-gate 			r = libscf_create_self(h);
35637c478bd9Sstevel@tonic-gate 			if (r == 0)
35647c478bd9Sstevel@tonic-gate 				goto lookup;
35657c478bd9Sstevel@tonic-gate 			assert(r == ECONNABORTED);
35667c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
35677c478bd9Sstevel@tonic-gate 
35687c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
35697c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
35707c478bd9Sstevel@tonic-gate 			goto lookup;
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
35737c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
35747c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
35757c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
35767c478bd9Sstevel@tonic-gate 		default:
35777c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
35787c478bd9Sstevel@tonic-gate 		}
35797c478bd9Sstevel@tonic-gate 	}
35807c478bd9Sstevel@tonic-gate 
35817c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
35827c478bd9Sstevel@tonic-gate 
35837c478bd9Sstevel@tonic-gate 	r = libscf_inst_delete_prop(inst, SCF_PG_OPTIONS_OVR,
35847c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_MILESTONE);
35857c478bd9Sstevel@tonic-gate 	switch (r) {
35867c478bd9Sstevel@tonic-gate 	case 0:
35877c478bd9Sstevel@tonic-gate 	case ECANCELED:
35887c478bd9Sstevel@tonic-gate 		break;
35897c478bd9Sstevel@tonic-gate 
35907c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
35917c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
35927c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
35937c478bd9Sstevel@tonic-gate 		goto lookup;
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate 	case EPERM:
35967c478bd9Sstevel@tonic-gate 	case EACCES:
35977c478bd9Sstevel@tonic-gate 	case EROFS:
35987c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "Could not clear temporary milestone: "
35997c478bd9Sstevel@tonic-gate 		    "%s.\n", strerror(r));
36007c478bd9Sstevel@tonic-gate 		break;
36017c478bd9Sstevel@tonic-gate 
36027c478bd9Sstevel@tonic-gate 	default:
36037c478bd9Sstevel@tonic-gate 		bad_error("libscf_inst_delete_prop", r);
36047c478bd9Sstevel@tonic-gate 	}
36057c478bd9Sstevel@tonic-gate 
36067c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
36077c478bd9Sstevel@tonic-gate 
36087c478bd9Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, buf, max_scf_fmri_size);
36097c478bd9Sstevel@tonic-gate 	switch (r) {
36107c478bd9Sstevel@tonic-gate 	case ECANCELED:
36117c478bd9Sstevel@tonic-gate 	case ENOENT:
36127c478bd9Sstevel@tonic-gate 	case EINVAL:
36137c478bd9Sstevel@tonic-gate 		(void) strcpy(buf, "all");
36147c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
36157c478bd9Sstevel@tonic-gate 
36167c478bd9Sstevel@tonic-gate 	case 0:
36177c478bd9Sstevel@tonic-gate 		uu_warn("Returning to milestone %s.\n", buf);
36187c478bd9Sstevel@tonic-gate 		break;
36197c478bd9Sstevel@tonic-gate 
36207c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
36217c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
36227c478bd9Sstevel@tonic-gate 		goto lookup;
36237c478bd9Sstevel@tonic-gate 
36247c478bd9Sstevel@tonic-gate 	default:
36257c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
36267c478bd9Sstevel@tonic-gate 	}
36277c478bd9Sstevel@tonic-gate 
36287c478bd9Sstevel@tonic-gate 	r = dgraph_set_milestone(buf, h, B_FALSE);
36297c478bd9Sstevel@tonic-gate 	switch (r) {
36307c478bd9Sstevel@tonic-gate 	case 0:
36317c478bd9Sstevel@tonic-gate 	case ECONNRESET:
36327c478bd9Sstevel@tonic-gate 	case EALREADY:
36337c478bd9Sstevel@tonic-gate 	case EINVAL:
36347c478bd9Sstevel@tonic-gate 	case ENOENT:
36357c478bd9Sstevel@tonic-gate 		break;
36367c478bd9Sstevel@tonic-gate 
36377c478bd9Sstevel@tonic-gate 	default:
36387c478bd9Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
36397c478bd9Sstevel@tonic-gate 	}
36407c478bd9Sstevel@tonic-gate 
36417c478bd9Sstevel@tonic-gate 	/*
36427c478bd9Sstevel@tonic-gate 	 * See graph_runlevel_changed().
36437c478bd9Sstevel@tonic-gate 	 */
36447c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
36457c478bd9Sstevel@tonic-gate 	utmpx_set_runlevel(target_milestone_as_runlevel(), 'S', B_TRUE);
36467c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
36477c478bd9Sstevel@tonic-gate 
36487c478bd9Sstevel@tonic-gate 	startd_free(buf, max_scf_fmri_size);
36497c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
36507c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
36517c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
36527c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
36537c478bd9Sstevel@tonic-gate 
36547c478bd9Sstevel@tonic-gate 	/*
36557c478bd9Sstevel@tonic-gate 	 * We'll give ourselves 3 seconds to respond to all of the enablings
36567c478bd9Sstevel@tonic-gate 	 * that setting the milestone should have created before checking
36577c478bd9Sstevel@tonic-gate 	 * whether to run sulogin.
36587c478bd9Sstevel@tonic-gate 	 */
36597c478bd9Sstevel@tonic-gate 	left = 3;
36607c478bd9Sstevel@tonic-gate 	while (left > 0)
36617c478bd9Sstevel@tonic-gate 		left = sleep(left);
36627c478bd9Sstevel@tonic-gate 
36637c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
36647c478bd9Sstevel@tonic-gate 	/*
36657c478bd9Sstevel@tonic-gate 	 * Clearing these variables will allow the sulogin thread to run.  We
36667c478bd9Sstevel@tonic-gate 	 * check here in case there aren't any more state updates anytime soon.
36677c478bd9Sstevel@tonic-gate 	 */
36687c478bd9Sstevel@tonic-gate 	go_to_level1 = go_single_user_mode = booting_to_single_user = B_FALSE;
36697c478bd9Sstevel@tonic-gate 	if (!sulogin_thread_running && !can_come_up()) {
36707c478bd9Sstevel@tonic-gate 		(void) startd_thread_create(sulogin_thread, NULL);
36717c478bd9Sstevel@tonic-gate 		sulogin_thread_running = B_TRUE;
36727c478bd9Sstevel@tonic-gate 	}
36737c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
36747c478bd9Sstevel@tonic-gate 	single_user_thread_count--;
36757c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&single_user_thread_lock);
36767c478bd9Sstevel@tonic-gate 	return (NULL);
36777c478bd9Sstevel@tonic-gate }
36787c478bd9Sstevel@tonic-gate 
36797c478bd9Sstevel@tonic-gate 
36807c478bd9Sstevel@tonic-gate /*
36817c478bd9Sstevel@tonic-gate  * Dependency graph operations API.  These are handle-independent thread-safe
36827c478bd9Sstevel@tonic-gate  * graph manipulation functions which are the entry points for the event
36837c478bd9Sstevel@tonic-gate  * threads below.
36847c478bd9Sstevel@tonic-gate  */
36857c478bd9Sstevel@tonic-gate 
36867c478bd9Sstevel@tonic-gate /*
36877c478bd9Sstevel@tonic-gate  * If a configured vertex exists for inst_fmri, return EEXIST.  If no vertex
36887c478bd9Sstevel@tonic-gate  * exists for inst_fmri, add one.  Then fetch the restarter from inst, make
36897c478bd9Sstevel@tonic-gate  * this vertex dependent on it, and send _ADD_INSTANCE to the restarter.
36907c478bd9Sstevel@tonic-gate  * Fetch whether the instance should be enabled from inst and send _ENABLE or
36917c478bd9Sstevel@tonic-gate  * _DISABLE as appropriate.  Finally rummage through inst's dependency
36927c478bd9Sstevel@tonic-gate  * property groups and add vertices and edges as appropriate.  If anything
36937c478bd9Sstevel@tonic-gate  * goes wrong after sending _ADD_INSTANCE, send _ADMIN_MAINT_ON to put the
36947c478bd9Sstevel@tonic-gate  * instance in maintenance.  Don't send _START or _STOP until we get a state
36957c478bd9Sstevel@tonic-gate  * update in case we're being restarted and the service is already running.
36967c478bd9Sstevel@tonic-gate  *
36977c478bd9Sstevel@tonic-gate  * To support booting to a milestone, we must also make sure all dependencies
36987c478bd9Sstevel@tonic-gate  * encountered are configured, if they exist in the repository.
36997c478bd9Sstevel@tonic-gate  *
37007c478bd9Sstevel@tonic-gate  * Returns 0 on success, ECONNABORTED on repository disconnection, EINVAL if
37017c478bd9Sstevel@tonic-gate  * inst_fmri is an invalid (or not canonical) FMRI, ECANCELED if inst is
37027c478bd9Sstevel@tonic-gate  * deleted, or EEXIST if a configured vertex for inst_fmri already exists.
37037c478bd9Sstevel@tonic-gate  */
37047c478bd9Sstevel@tonic-gate int
37057c478bd9Sstevel@tonic-gate dgraph_add_instance(const char *inst_fmri, scf_instance_t *inst,
37067c478bd9Sstevel@tonic-gate     boolean_t lock_graph)
37077c478bd9Sstevel@tonic-gate {
37087c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
37097c478bd9Sstevel@tonic-gate 	int err;
37107c478bd9Sstevel@tonic-gate 
37117c478bd9Sstevel@tonic-gate 	if (strcmp(inst_fmri, SCF_SERVICE_STARTD) == 0)
37127c478bd9Sstevel@tonic-gate 		return (0);
37137c478bd9Sstevel@tonic-gate 
37147c478bd9Sstevel@tonic-gate 	/* Check for a vertex for inst_fmri. */
37157c478bd9Sstevel@tonic-gate 	if (lock_graph) {
37167c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&dgraph_lock);
37177c478bd9Sstevel@tonic-gate 	} else {
37187c478bd9Sstevel@tonic-gate 		assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
37197c478bd9Sstevel@tonic-gate 	}
37207c478bd9Sstevel@tonic-gate 
37217c478bd9Sstevel@tonic-gate 	v = vertex_get_by_name(inst_fmri);
37227c478bd9Sstevel@tonic-gate 
37237c478bd9Sstevel@tonic-gate 	if (v != NULL) {
37247c478bd9Sstevel@tonic-gate 		assert(v->gv_type == GVT_INST);
37257c478bd9Sstevel@tonic-gate 
37267c478bd9Sstevel@tonic-gate 		if (v->gv_flags & GV_CONFIGURED) {
37277c478bd9Sstevel@tonic-gate 			if (lock_graph)
37287c478bd9Sstevel@tonic-gate 				MUTEX_UNLOCK(&dgraph_lock);
37297c478bd9Sstevel@tonic-gate 			return (EEXIST);
37307c478bd9Sstevel@tonic-gate 		}
37317c478bd9Sstevel@tonic-gate 	} else {
37327c478bd9Sstevel@tonic-gate 		/* Add the vertex. */
37337c478bd9Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(inst_fmri, GVT_INST, 0,
37347c478bd9Sstevel@tonic-gate 		    RERR_NONE, &v);
37357c478bd9Sstevel@tonic-gate 		if (err != 0) {
37367c478bd9Sstevel@tonic-gate 			assert(err == EINVAL);
37377c478bd9Sstevel@tonic-gate 			if (lock_graph)
37387c478bd9Sstevel@tonic-gate 				MUTEX_UNLOCK(&dgraph_lock);
37397c478bd9Sstevel@tonic-gate 			return (EINVAL);
37407c478bd9Sstevel@tonic-gate 		}
37417c478bd9Sstevel@tonic-gate 	}
37427c478bd9Sstevel@tonic-gate 
37437c478bd9Sstevel@tonic-gate 	err = configure_vertex(v, inst);
37447c478bd9Sstevel@tonic-gate 
37457c478bd9Sstevel@tonic-gate 	if (lock_graph)
37467c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
37477c478bd9Sstevel@tonic-gate 
37487c478bd9Sstevel@tonic-gate 	return (err);
37497c478bd9Sstevel@tonic-gate }
37507c478bd9Sstevel@tonic-gate 
37517c478bd9Sstevel@tonic-gate /*
37527c478bd9Sstevel@tonic-gate  * Locate the vertex for this property group's instance.  If it doesn't exist
37537c478bd9Sstevel@tonic-gate  * or is unconfigured, call dgraph_add_instance() & return.  Otherwise fetch
37547c478bd9Sstevel@tonic-gate  * the restarter for the instance, and if it has changed, send
37557c478bd9Sstevel@tonic-gate  * _REMOVE_INSTANCE to the old restarter, remove the dependency, make sure the
37567c478bd9Sstevel@tonic-gate  * new restarter has a vertex, add a new dependency, and send _ADD_INSTANCE to
37577c478bd9Sstevel@tonic-gate  * the new restarter.  Then fetch whether the instance should be enabled, and
37587c478bd9Sstevel@tonic-gate  * if it is different from what we had, or if we changed the restarter, send
37597c478bd9Sstevel@tonic-gate  * the appropriate _ENABLE or _DISABLE command.
37607c478bd9Sstevel@tonic-gate  *
37617c478bd9Sstevel@tonic-gate  * Returns 0 on success, ENOTSUP if the pg's parent is not an instance,
37627c478bd9Sstevel@tonic-gate  * ECONNABORTED on repository disconnection, ECANCELED if the instance is
37637c478bd9Sstevel@tonic-gate  * deleted, or -1 if the instance's general property group is deleted or if
37647c478bd9Sstevel@tonic-gate  * its enabled property is misconfigured.
37657c478bd9Sstevel@tonic-gate  */
37667c478bd9Sstevel@tonic-gate static int
37677c478bd9Sstevel@tonic-gate dgraph_update_general(scf_propertygroup_t *pg)
37687c478bd9Sstevel@tonic-gate {
37697c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
37707c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
37717c478bd9Sstevel@tonic-gate 	char *fmri;
37727c478bd9Sstevel@tonic-gate 	char *restarter_fmri;
37737c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
37747c478bd9Sstevel@tonic-gate 	int err;
37757c478bd9Sstevel@tonic-gate 	int enabled, enabled_ovr;
37767c478bd9Sstevel@tonic-gate 	int oldflags;
37777c478bd9Sstevel@tonic-gate 
37787c478bd9Sstevel@tonic-gate 	/* Find the vertex for this service */
37797c478bd9Sstevel@tonic-gate 	h = scf_pg_handle(pg);
37807c478bd9Sstevel@tonic-gate 
37817c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
37827c478bd9Sstevel@tonic-gate 
37837c478bd9Sstevel@tonic-gate 	if (scf_pg_get_parent_instance(pg, inst) != 0) {
37847c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
37857c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
37867c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
37877c478bd9Sstevel@tonic-gate 
37887c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
37897c478bd9Sstevel@tonic-gate 		default:
37907c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
37917c478bd9Sstevel@tonic-gate 
37927c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
37937c478bd9Sstevel@tonic-gate 			return (0);
37947c478bd9Sstevel@tonic-gate 
37957c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
37967c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_parent_instance", scf_error());
37977c478bd9Sstevel@tonic-gate 		}
37987c478bd9Sstevel@tonic-gate 	}
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate 	err = libscf_instance_get_fmri(inst, &fmri);
38017c478bd9Sstevel@tonic-gate 	switch (err) {
38027c478bd9Sstevel@tonic-gate 	case 0:
38037c478bd9Sstevel@tonic-gate 		break;
38047c478bd9Sstevel@tonic-gate 
38057c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
38067c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
38077c478bd9Sstevel@tonic-gate 		return (ECONNABORTED);
38087c478bd9Sstevel@tonic-gate 
38097c478bd9Sstevel@tonic-gate 	case ECANCELED:
38107c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
38117c478bd9Sstevel@tonic-gate 		return (0);
38127c478bd9Sstevel@tonic-gate 
38137c478bd9Sstevel@tonic-gate 	default:
38147c478bd9Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", err);
38157c478bd9Sstevel@tonic-gate 	}
38167c478bd9Sstevel@tonic-gate 
38177c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG,
38187c478bd9Sstevel@tonic-gate 	    "Graph engine: Reloading general properties for %s.\n", fmri);
38197c478bd9Sstevel@tonic-gate 
38207c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
38217c478bd9Sstevel@tonic-gate 
38227c478bd9Sstevel@tonic-gate 	v = vertex_get_by_name(fmri);
38237c478bd9Sstevel@tonic-gate 	if (v == NULL || !(v->gv_flags & GV_CONFIGURED)) {
38247c478bd9Sstevel@tonic-gate 		/* Will get the up-to-date properties. */
38257c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
38267c478bd9Sstevel@tonic-gate 		err = dgraph_add_instance(fmri, inst, B_TRUE);
38277c478bd9Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
38287c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
38297c478bd9Sstevel@tonic-gate 		return (err == ECANCELED ? 0 : err);
38307c478bd9Sstevel@tonic-gate 	}
38317c478bd9Sstevel@tonic-gate 
38327c478bd9Sstevel@tonic-gate 	/* Read enabled & restarter from repository. */
38337c478bd9Sstevel@tonic-gate 	restarter_fmri = startd_alloc(max_scf_value_size);
38347c478bd9Sstevel@tonic-gate 	err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled,
38357c478bd9Sstevel@tonic-gate 	    &enabled_ovr, &restarter_fmri);
38367c478bd9Sstevel@tonic-gate 	if (err != 0 || enabled == -1) {
38377c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
38387c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
38397c478bd9Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
38407c478bd9Sstevel@tonic-gate 
38417c478bd9Sstevel@tonic-gate 		switch (err) {
38427c478bd9Sstevel@tonic-gate 		case ENOENT:
38437c478bd9Sstevel@tonic-gate 		case 0:
38447c478bd9Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
38457c478bd9Sstevel@tonic-gate 			return (-1);
38467c478bd9Sstevel@tonic-gate 
38477c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
38487c478bd9Sstevel@tonic-gate 		case ECANCELED:
38497c478bd9Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
38507c478bd9Sstevel@tonic-gate 			return (err);
38517c478bd9Sstevel@tonic-gate 
38527c478bd9Sstevel@tonic-gate 		default:
38537c478bd9Sstevel@tonic-gate 			bad_error("libscf_get_basic_instance_data", err);
38547c478bd9Sstevel@tonic-gate 		}
38557c478bd9Sstevel@tonic-gate 	}
38567c478bd9Sstevel@tonic-gate 
38577c478bd9Sstevel@tonic-gate 	oldflags = v->gv_flags;
38587c478bd9Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) |
38597c478bd9Sstevel@tonic-gate 	    (enabled ? GV_ENBLD_NOOVR : 0);
38607c478bd9Sstevel@tonic-gate 
38617c478bd9Sstevel@tonic-gate 	if (enabled_ovr != -1)
38627c478bd9Sstevel@tonic-gate 		enabled = enabled_ovr;
38637c478bd9Sstevel@tonic-gate 
38647c478bd9Sstevel@tonic-gate 	/*
38657c478bd9Sstevel@tonic-gate 	 * If GV_ENBLD_NOOVR has changed, then we need to re-evaluate the
38667c478bd9Sstevel@tonic-gate 	 * subgraph.
38677c478bd9Sstevel@tonic-gate 	 */
38687c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE && v->gv_flags != oldflags)
38697c478bd9Sstevel@tonic-gate 		(void) eval_subgraph(v, h);
38707c478bd9Sstevel@tonic-gate 
38717c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
38727c478bd9Sstevel@tonic-gate 
38737c478bd9Sstevel@tonic-gate 	/* Ignore restarter change for now. */
38747c478bd9Sstevel@tonic-gate 
38757c478bd9Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_value_size);
38767c478bd9Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
38777c478bd9Sstevel@tonic-gate 
38787c478bd9Sstevel@tonic-gate 	/*
38797c478bd9Sstevel@tonic-gate 	 * Always send _ENABLE or _DISABLE.  We could avoid this if the
38807c478bd9Sstevel@tonic-gate 	 * restarter didn't change and the enabled value didn't change, but
38817c478bd9Sstevel@tonic-gate 	 * that's not easy to check and improbable anyway, so we'll just do
38827c478bd9Sstevel@tonic-gate 	 * this.
38837c478bd9Sstevel@tonic-gate 	 */
38847c478bd9Sstevel@tonic-gate 	graph_enable_by_vertex(v, enabled, 1);
38857c478bd9Sstevel@tonic-gate 
38867c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
38877c478bd9Sstevel@tonic-gate 
38887c478bd9Sstevel@tonic-gate 	return (0);
38897c478bd9Sstevel@tonic-gate }
38907c478bd9Sstevel@tonic-gate 
38917c478bd9Sstevel@tonic-gate /*
38927c478bd9Sstevel@tonic-gate  * Delete all of the property group dependencies of v, update inst's running
38937c478bd9Sstevel@tonic-gate  * snapshot, and add the dependencies in the new snapshot.  If any of the new
38947c478bd9Sstevel@tonic-gate  * dependencies would create a cycle, send _ADMIN_MAINT_ON.  Otherwise
38957c478bd9Sstevel@tonic-gate  * reevaluate v's dependencies, send _START or _STOP as appropriate, and do
38967c478bd9Sstevel@tonic-gate  * the same for v's dependents.
38977c478bd9Sstevel@tonic-gate  *
38987c478bd9Sstevel@tonic-gate  * Returns
38997c478bd9Sstevel@tonic-gate  *   0 - success
39007c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
39017c478bd9Sstevel@tonic-gate  *   ECANCELED - inst was deleted
39027c478bd9Sstevel@tonic-gate  *   EINVAL - inst is invalid (e.g., missing general/enabled)
39037c478bd9Sstevel@tonic-gate  *   -1 - libscf_snapshots_refresh() failed
39047c478bd9Sstevel@tonic-gate  */
39057c478bd9Sstevel@tonic-gate static int
39067c478bd9Sstevel@tonic-gate dgraph_refresh_instance(graph_vertex_t *v, scf_instance_t *inst)
39077c478bd9Sstevel@tonic-gate {
39087c478bd9Sstevel@tonic-gate 	int r;
39097c478bd9Sstevel@tonic-gate 	int enabled;
39107c478bd9Sstevel@tonic-gate 
39117c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
39127c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
39137c478bd9Sstevel@tonic-gate 
39147c478bd9Sstevel@tonic-gate 	/* Only refresh services with valid general/enabled properties. */
39157c478bd9Sstevel@tonic-gate 	r = libscf_get_basic_instance_data(scf_instance_handle(inst), inst,
39167c478bd9Sstevel@tonic-gate 	    v->gv_name, &enabled, NULL, NULL);
39177c478bd9Sstevel@tonic-gate 	switch (r) {
39187c478bd9Sstevel@tonic-gate 	case 0:
39197c478bd9Sstevel@tonic-gate 		break;
39207c478bd9Sstevel@tonic-gate 
39217c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
39227c478bd9Sstevel@tonic-gate 	case ECANCELED:
39237c478bd9Sstevel@tonic-gate 		return (r);
39247c478bd9Sstevel@tonic-gate 
39257c478bd9Sstevel@tonic-gate 	case ENOENT:
39267c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
39277c478bd9Sstevel@tonic-gate 		    "Ignoring %s because it has no general property group.\n",
39287c478bd9Sstevel@tonic-gate 		    v->gv_name);
39297c478bd9Sstevel@tonic-gate 		return (EINVAL);
39307c478bd9Sstevel@tonic-gate 
39317c478bd9Sstevel@tonic-gate 	default:
39327c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_basic_instance_data", r);
39337c478bd9Sstevel@tonic-gate 	}
39347c478bd9Sstevel@tonic-gate 
39357c478bd9Sstevel@tonic-gate 	if (enabled == -1)
39367c478bd9Sstevel@tonic-gate 		return (EINVAL);
39377c478bd9Sstevel@tonic-gate 
39387c478bd9Sstevel@tonic-gate 	r = libscf_snapshots_refresh(inst, v->gv_name);
39397c478bd9Sstevel@tonic-gate 	if (r != 0) {
39407c478bd9Sstevel@tonic-gate 		if (r != -1)
39417c478bd9Sstevel@tonic-gate 			bad_error("libscf_snapshots_refresh", r);
39427c478bd9Sstevel@tonic-gate 
39437c478bd9Sstevel@tonic-gate 		/* error logged */
39447c478bd9Sstevel@tonic-gate 		return (r);
39457c478bd9Sstevel@tonic-gate 	}
39467c478bd9Sstevel@tonic-gate 
39477c478bd9Sstevel@tonic-gate 	r = refresh_vertex(v, inst);
39487c478bd9Sstevel@tonic-gate 	if (r != 0 && r != ECONNABORTED)
39497c478bd9Sstevel@tonic-gate 		bad_error("refresh_vertex", r);
39507c478bd9Sstevel@tonic-gate 	return (r);
39517c478bd9Sstevel@tonic-gate }
39527c478bd9Sstevel@tonic-gate 
39537c478bd9Sstevel@tonic-gate /*
395456e23938Sbustos  * Returns true only if none of this service's dependents are 'up' -- online,
395556e23938Sbustos  * degraded, or offline.
39567c478bd9Sstevel@tonic-gate  */
39577c478bd9Sstevel@tonic-gate static int
395856e23938Sbustos is_nonsubgraph_leaf(graph_vertex_t *v)
39597c478bd9Sstevel@tonic-gate {
39607c478bd9Sstevel@tonic-gate 	graph_vertex_t *vv;
39617c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
39627c478bd9Sstevel@tonic-gate 
39637c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
39647c478bd9Sstevel@tonic-gate 
39657c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
39667c478bd9Sstevel@tonic-gate 	    e != NULL;
39677c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e)) {
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate 		vv = e->ge_vertex;
39707c478bd9Sstevel@tonic-gate 		if (vv->gv_type == GVT_INST) {
397156e23938Sbustos 			if ((vv->gv_flags & GV_CONFIGURED) == 0)
397256e23938Sbustos 				continue;
397356e23938Sbustos 
397456e23938Sbustos 			if (vv->gv_flags & GV_INSUBGRAPH)
397556e23938Sbustos 				continue;
397656e23938Sbustos 
397756e23938Sbustos 			if (up_state(vv->gv_state))
397856e23938Sbustos 				return (0);
39797c478bd9Sstevel@tonic-gate 		} else {
39807c478bd9Sstevel@tonic-gate 			/*
39817c478bd9Sstevel@tonic-gate 			 * For dependency group or service vertices, keep
39827c478bd9Sstevel@tonic-gate 			 * traversing to see if instances are running.
39837c478bd9Sstevel@tonic-gate 			 */
398456e23938Sbustos 			if (!is_nonsubgraph_leaf(vv))
39857c478bd9Sstevel@tonic-gate 				return (0);
39867c478bd9Sstevel@tonic-gate 		}
398756e23938Sbustos 	}
398856e23938Sbustos 
398956e23938Sbustos 	return (1);
399056e23938Sbustos }
39917c478bd9Sstevel@tonic-gate 
39927c478bd9Sstevel@tonic-gate /*
399356e23938Sbustos  * Disable v temporarily.  Attempt to do this by setting its enabled override
399456e23938Sbustos  * property in the repository.  If that fails, send a _DISABLE command.
399556e23938Sbustos  * Returns 0 on success and ECONNABORTED if the repository connection is
399656e23938Sbustos  * broken.
39977c478bd9Sstevel@tonic-gate  */
399856e23938Sbustos static int
399956e23938Sbustos disable_service_temporarily(graph_vertex_t *v, scf_handle_t *h)
40007c478bd9Sstevel@tonic-gate {
400156e23938Sbustos 	const char * const emsg = "Could not temporarily disable %s because "
400256e23938Sbustos 	    "%s.  Will stop service anyways.  Repository status for the "
400356e23938Sbustos 	    "service may be inaccurate.\n";
400456e23938Sbustos 	const char * const emsg_cbroken =
400556e23938Sbustos 	    "the repository connection was broken";
400656e23938Sbustos 
400756e23938Sbustos 	scf_instance_t *inst;
40087c478bd9Sstevel@tonic-gate 	int r;
40097c478bd9Sstevel@tonic-gate 
40107c478bd9Sstevel@tonic-gate 	inst = scf_instance_create(h);
40117c478bd9Sstevel@tonic-gate 	if (inst == NULL) {
401256e23938Sbustos 		char buf[100];
401356e23938Sbustos 
401456e23938Sbustos 		(void) snprintf(buf, sizeof (buf),
401556e23938Sbustos 		    "scf_instance_create() failed (%s)",
401656e23938Sbustos 		    scf_strerror(scf_error()));
401756e23938Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name, buf);
401856e23938Sbustos 
401956e23938Sbustos 		graph_enable_by_vertex(v, 0, 0);
402056e23938Sbustos 		return (0);
40217c478bd9Sstevel@tonic-gate 	}
402256e23938Sbustos 
40237c478bd9Sstevel@tonic-gate 	r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst,
40247c478bd9Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT);
40257c478bd9Sstevel@tonic-gate 	if (r != 0) {
40267c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
40277c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
402856e23938Sbustos 			log_error(LOG_WARNING, emsg, v->gv_name, emsg_cbroken);
402956e23938Sbustos 			graph_enable_by_vertex(v, 0, 0);
403056e23938Sbustos 			return (ECONNABORTED);
40317c478bd9Sstevel@tonic-gate 
40327c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
403356e23938Sbustos 			return (0);
40347c478bd9Sstevel@tonic-gate 
40357c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
40367c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
40377c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
40387c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
40397c478bd9Sstevel@tonic-gate 		default:
40407c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri",
40417c478bd9Sstevel@tonic-gate 			    scf_error());
40427c478bd9Sstevel@tonic-gate 		}
40437c478bd9Sstevel@tonic-gate 	}
404456e23938Sbustos 
40457c478bd9Sstevel@tonic-gate 	r = libscf_set_enable_ovr(inst, 0);
40467c478bd9Sstevel@tonic-gate 	switch (r) {
40477c478bd9Sstevel@tonic-gate 	case 0:
40487c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
404956e23938Sbustos 		return (0);
405056e23938Sbustos 
40517c478bd9Sstevel@tonic-gate 	case ECANCELED:
40527c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
405356e23938Sbustos 		return (0);
405456e23938Sbustos 
40557c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
405656e23938Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name, emsg_cbroken);
405756e23938Sbustos 		graph_enable_by_vertex(v, 0, 0);
405856e23938Sbustos 		return (ECONNABORTED);
405956e23938Sbustos 
40607c478bd9Sstevel@tonic-gate 	case EPERM:
406156e23938Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name,
406256e23938Sbustos 		    "the repository denied permission");
406356e23938Sbustos 		graph_enable_by_vertex(v, 0, 0);
406456e23938Sbustos 		return (0);
406556e23938Sbustos 
40667c478bd9Sstevel@tonic-gate 	case EROFS:
406756e23938Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name,
406856e23938Sbustos 		    "the repository is read-only");
406956e23938Sbustos 		graph_enable_by_vertex(v, 0, 0);
407056e23938Sbustos 		return (0);
407156e23938Sbustos 
40727c478bd9Sstevel@tonic-gate 	default:
40737c478bd9Sstevel@tonic-gate 		bad_error("libscf_set_enable_ovr", r);
407456e23938Sbustos 		/* NOTREACHED */
40757c478bd9Sstevel@tonic-gate 	}
407656e23938Sbustos }
407756e23938Sbustos 
407856e23938Sbustos /*
407956e23938Sbustos  * Of the transitive instance dependencies of v, disable those which are not
408056e23938Sbustos  * in the subgraph and which are leaves (i.e., have no dependents which are
408156e23938Sbustos  * "up").
408256e23938Sbustos  */
408356e23938Sbustos static void
408456e23938Sbustos disable_nonsubgraph_leaves(graph_vertex_t *v, void *arg)
408556e23938Sbustos {
408656e23938Sbustos 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
408756e23938Sbustos 
4088b54561f4Snakanon 	/*
4089b54561f4Snakanon 	 * We must skip exclusion dependencies because they are allowed to
4090b54561f4Snakanon 	 * complete dependency cycles.  This is correct because A's exclusion
4091b54561f4Snakanon 	 * dependency on B doesn't bear on the order in which they should be
4092b54561f4Snakanon 	 * stopped.  Indeed, the exclusion dependency should guarantee that
4093b54561f4Snakanon 	 * they are never online at the same time.
4094b54561f4Snakanon 	 */
4095b54561f4Snakanon 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
4096b54561f4Snakanon 		return;
4097b54561f4Snakanon 
409856e23938Sbustos 	/* If v isn't an instance, recurse on its dependencies. */
409956e23938Sbustos 	if (v->gv_type != GVT_INST)
410056e23938Sbustos 		goto recurse;
410156e23938Sbustos 
410256e23938Sbustos 	if ((v->gv_flags & GV_CONFIGURED) == 0)
410356e23938Sbustos 		/*
410456e23938Sbustos 		 * Unconfigured instances should have no dependencies, but in
410556e23938Sbustos 		 * case they ever get them,
410656e23938Sbustos 		 */
410756e23938Sbustos 		goto recurse;
410856e23938Sbustos 
410956e23938Sbustos 	/*
411056e23938Sbustos 	 * If v is in the subgraph, so should all of its dependencies, so do
411156e23938Sbustos 	 * nothing.
411256e23938Sbustos 	 */
411356e23938Sbustos 	if (v->gv_flags & GV_INSUBGRAPH)
41147c478bd9Sstevel@tonic-gate 		return;
411556e23938Sbustos 
411656e23938Sbustos 	/* If v isn't a leaf because it's already down, recurse. */
411756e23938Sbustos 	if (!up_state(v->gv_state))
411856e23938Sbustos 		goto recurse;
411956e23938Sbustos 
412056e23938Sbustos 	/* If v is disabled but not down yet, be patient. */
412156e23938Sbustos 	if ((v->gv_flags & GV_ENABLED) == 0)
412256e23938Sbustos 		return;
412356e23938Sbustos 
412456e23938Sbustos 	/* If v is a leaf, disable it. */
412556e23938Sbustos 	if (is_nonsubgraph_leaf(v))
412656e23938Sbustos 		(void) disable_service_temporarily(v, (scf_handle_t *)arg);
412756e23938Sbustos 
412856e23938Sbustos 	return;
412956e23938Sbustos 
41307c478bd9Sstevel@tonic-gate recurse:
413156e23938Sbustos 	graph_walk_dependencies(v, disable_nonsubgraph_leaves, arg);
41327c478bd9Sstevel@tonic-gate }
41337c478bd9Sstevel@tonic-gate 
41347c478bd9Sstevel@tonic-gate /*
41357c478bd9Sstevel@tonic-gate  * Find the vertex for inst_name.  If it doesn't exist, return ENOENT.
41367c478bd9Sstevel@tonic-gate  * Otherwise set its state to state.  If the instance has entered a state
41377c478bd9Sstevel@tonic-gate  * which requires automatic action, take it (Uninitialized: do
41387c478bd9Sstevel@tonic-gate  * dgraph_refresh_instance() without the snapshot update.  Disabled: if the
41397c478bd9Sstevel@tonic-gate  * instance should be enabled, send _ENABLE.  Offline: if the instance should
41407c478bd9Sstevel@tonic-gate  * be disabled, send _DISABLE, and if its dependencies are satisfied, send
41417c478bd9Sstevel@tonic-gate  * _START.  Online, Degraded: if the instance wasn't running, update its start
41427c478bd9Sstevel@tonic-gate  * snapshot.  Maintenance: no action.)
41437c478bd9Sstevel@tonic-gate  *
41447c478bd9Sstevel@tonic-gate  * Also fails with ECONNABORTED, or EINVAL if state is invalid.
41457c478bd9Sstevel@tonic-gate  */
41467c478bd9Sstevel@tonic-gate static int
41477c478bd9Sstevel@tonic-gate dgraph_set_instance_state(scf_handle_t *h, const char *inst_name,
41487c478bd9Sstevel@tonic-gate     restarter_instance_state_t state, restarter_error_t serr)
41497c478bd9Sstevel@tonic-gate {
41507c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
415199b44c3bSlianep 	int err = 0;
41527c478bd9Sstevel@tonic-gate 	restarter_instance_state_t old_state;
41537c478bd9Sstevel@tonic-gate 
41547c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
41557c478bd9Sstevel@tonic-gate 
41567c478bd9Sstevel@tonic-gate 	v = vertex_get_by_name(inst_name);
41577c478bd9Sstevel@tonic-gate 	if (v == NULL) {
41587c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
41597c478bd9Sstevel@tonic-gate 		return (ENOENT);
41607c478bd9Sstevel@tonic-gate 	}
41617c478bd9Sstevel@tonic-gate 
416256e23938Sbustos 	assert(v->gv_type == GVT_INST);
416356e23938Sbustos 
41647c478bd9Sstevel@tonic-gate 	switch (state) {
41657c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_UNINIT:
41667c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_DISABLED:
41677c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_OFFLINE:
41687c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_ONLINE:
41697c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_DEGRADED:
41707c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_MAINT:
41717c478bd9Sstevel@tonic-gate 		break;
41727c478bd9Sstevel@tonic-gate 
41737c478bd9Sstevel@tonic-gate 	default:
41747c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
41757c478bd9Sstevel@tonic-gate 		return (EINVAL);
41767c478bd9Sstevel@tonic-gate 	}
41777c478bd9Sstevel@tonic-gate 
41787c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph noting %s %s -> %s.\n", v->gv_name,
41797c478bd9Sstevel@tonic-gate 	    instance_state_str[v->gv_state], instance_state_str[state]);
41807c478bd9Sstevel@tonic-gate 
41817c478bd9Sstevel@tonic-gate 	old_state = v->gv_state;
41827c478bd9Sstevel@tonic-gate 	v->gv_state = state;
41837c478bd9Sstevel@tonic-gate 
4184cd3bce3eSlianep 	err = gt_transition(h, v, serr, old_state);
418599b44c3bSlianep 
418699b44c3bSlianep 	MUTEX_UNLOCK(&dgraph_lock);
418799b44c3bSlianep 	return (err);
418899b44c3bSlianep }
418999b44c3bSlianep 
419099b44c3bSlianep /*
419156e23938Sbustos  * Handle state changes during milestone shutdown.  See
419256e23938Sbustos  * dgraph_set_milestone().  If the repository connection is broken,
419356e23938Sbustos  * ECONNABORTED will be returned, though a _DISABLE command will be sent for
419456e23938Sbustos  * the vertex anyway.
419599b44c3bSlianep  */
419656e23938Sbustos int
419799b44c3bSlianep vertex_subgraph_dependencies_shutdown(scf_handle_t *h, graph_vertex_t *v,
419856e23938Sbustos     restarter_instance_state_t old_state)
419999b44c3bSlianep {
420056e23938Sbustos 	int was_up, now_up;
420156e23938Sbustos 	int ret = 0;
420299b44c3bSlianep 
420356e23938Sbustos 	assert(v->gv_type == GVT_INST);
42047c478bd9Sstevel@tonic-gate 
420556e23938Sbustos 	/* Don't care if we're not going to a milestone. */
420656e23938Sbustos 	if (milestone == NULL)
420756e23938Sbustos 		return (0);
420856e23938Sbustos 
420956e23938Sbustos 	/* Don't care if we already finished coming down. */
421056e23938Sbustos 	if (non_subgraph_svcs == 0)
421156e23938Sbustos 		return (0);
421256e23938Sbustos 
421356e23938Sbustos 	/* Don't care if the service is in the subgraph. */
421456e23938Sbustos 	if (v->gv_flags & GV_INSUBGRAPH)
421556e23938Sbustos 		return (0);
421656e23938Sbustos 
421756e23938Sbustos 	/*
421856e23938Sbustos 	 * Update non_subgraph_svcs.  It is the number of non-subgraph
421956e23938Sbustos 	 * services which are in online, degraded, or offline.
422056e23938Sbustos 	 */
422156e23938Sbustos 
422256e23938Sbustos 	was_up = up_state(old_state);
422356e23938Sbustos 	now_up = up_state(v->gv_state);
422456e23938Sbustos 
422556e23938Sbustos 	if (!was_up && now_up) {
422656e23938Sbustos 		++non_subgraph_svcs;
422756e23938Sbustos 	} else if (was_up && !now_up) {
42287c478bd9Sstevel@tonic-gate 		--non_subgraph_svcs;
422956e23938Sbustos 
42307c478bd9Sstevel@tonic-gate 		if (non_subgraph_svcs == 0) {
42317c478bd9Sstevel@tonic-gate 			if (halting != -1) {
42327c478bd9Sstevel@tonic-gate 				do_uadmin();
42337c478bd9Sstevel@tonic-gate 			} else if (go_single_user_mode || go_to_level1) {
42347c478bd9Sstevel@tonic-gate 				(void) startd_thread_create(single_user_thread,
42357c478bd9Sstevel@tonic-gate 				    NULL);
42367c478bd9Sstevel@tonic-gate 			}
423756e23938Sbustos 			return (0);
42387c478bd9Sstevel@tonic-gate 		}
42397c478bd9Sstevel@tonic-gate 	}
424056e23938Sbustos 
424156e23938Sbustos 	/* If this service is a leaf, it should be disabled. */
424256e23938Sbustos 	if ((v->gv_flags & GV_ENABLED) && is_nonsubgraph_leaf(v)) {
424356e23938Sbustos 		int r;
424456e23938Sbustos 
424556e23938Sbustos 		r = disable_service_temporarily(v, h);
424656e23938Sbustos 		switch (r) {
424756e23938Sbustos 		case 0:
424856e23938Sbustos 			break;
424956e23938Sbustos 
425056e23938Sbustos 		case ECONNABORTED:
425156e23938Sbustos 			ret = ECONNABORTED;
425256e23938Sbustos 			break;
425356e23938Sbustos 
425456e23938Sbustos 		default:
425556e23938Sbustos 			bad_error("disable_service_temporarily", r);
425656e23938Sbustos 		}
425756e23938Sbustos 	}
425856e23938Sbustos 
425956e23938Sbustos 	/*
426056e23938Sbustos 	 * If the service just came down, propagate the disable to the newly
426156e23938Sbustos 	 * exposed leaves.
426256e23938Sbustos 	 */
426356e23938Sbustos 	if (was_up && !now_up)
426456e23938Sbustos 		graph_walk_dependencies(v, disable_nonsubgraph_leaves,
426556e23938Sbustos 		    (void *)h);
426656e23938Sbustos 
426756e23938Sbustos 	return (ret);
42687c478bd9Sstevel@tonic-gate }
42697c478bd9Sstevel@tonic-gate 
42707c478bd9Sstevel@tonic-gate /*
427199b44c3bSlianep  * Decide whether to start up an sulogin thread after a service is
427299b44c3bSlianep  * finished changing state.  Only need to do the full can_come_up()
427399b44c3bSlianep  * evaluation if an instance is changing state, we're not halfway through
427499b44c3bSlianep  * loading the thread, and we aren't shutting down or going to the single
427599b44c3bSlianep  * user milestone.
42767c478bd9Sstevel@tonic-gate  */
427799b44c3bSlianep void
427899b44c3bSlianep graph_transition_sulogin(restarter_instance_state_t state,
427999b44c3bSlianep     restarter_instance_state_t old_state)
428099b44c3bSlianep {
428199b44c3bSlianep 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
42827c478bd9Sstevel@tonic-gate 
42837c478bd9Sstevel@tonic-gate 	if (state != old_state && st->st_load_complete &&
42847c478bd9Sstevel@tonic-gate 	    !go_single_user_mode && !go_to_level1 &&
42857c478bd9Sstevel@tonic-gate 	    halting == -1) {
428673b709eaSrm88369 		if (!sulogin_thread_running && !can_come_up()) {
42877c478bd9Sstevel@tonic-gate 			(void) startd_thread_create(sulogin_thread, NULL);
42887c478bd9Sstevel@tonic-gate 			sulogin_thread_running = B_TRUE;
42897c478bd9Sstevel@tonic-gate 		}
42907c478bd9Sstevel@tonic-gate 	}
429199b44c3bSlianep }
42927c478bd9Sstevel@tonic-gate 
429399b44c3bSlianep /*
4294cd3bce3eSlianep  * Propagate a start, stop event, or a satisfiability event.
429599b44c3bSlianep  *
4296cd3bce3eSlianep  * PROPAGATE_START and PROPAGATE_STOP simply propagate the transition event
4297cd3bce3eSlianep  * to direct dependents.  PROPAGATE_SAT propagates a start then walks the
4298cd3bce3eSlianep  * full dependent graph to check for newly satisfied nodes.  This is
4299cd3bce3eSlianep  * necessary for cases when non-direct dependents may be effected but direct
4300cd3bce3eSlianep  * dependents may not (e.g. for optional_all evaluations, see the
4301cd3bce3eSlianep  * propagate_satbility() comments).
4302cd3bce3eSlianep  *
4303cd3bce3eSlianep  * PROPAGATE_SAT should be used whenever a non-running service moves into
4304cd3bce3eSlianep  * a state which can satisfy optional dependencies, like disabled or
4305cd3bce3eSlianep  * maintenance.
430699b44c3bSlianep  */
430799b44c3bSlianep void
4308cd3bce3eSlianep graph_transition_propagate(graph_vertex_t *v, propagate_event_t type,
430999b44c3bSlianep     restarter_error_t rerr)
431099b44c3bSlianep {
4311cd3bce3eSlianep 	if (type == PROPAGATE_STOP) {
431299b44c3bSlianep 		graph_walk_dependents(v, propagate_stop, (void *)rerr);
4313cd3bce3eSlianep 	} else if (type == PROPAGATE_START || type == PROPAGATE_SAT) {
431499b44c3bSlianep 		graph_walk_dependents(v, propagate_start, NULL);
43157c478bd9Sstevel@tonic-gate 
4316cd3bce3eSlianep 		if (type == PROPAGATE_SAT)
431799b44c3bSlianep 			propagate_satbility(v);
431899b44c3bSlianep 	} else {
431999b44c3bSlianep #ifndef NDEBUG
4320cd3bce3eSlianep 		uu_warn("%s:%d: Unexpected type value %d.\n",  __FILE__,
4321cd3bce3eSlianep 		    __LINE__, type);
432299b44c3bSlianep #endif
432399b44c3bSlianep 		abort();
432499b44c3bSlianep 	}
43257c478bd9Sstevel@tonic-gate }
43267c478bd9Sstevel@tonic-gate 
43277c478bd9Sstevel@tonic-gate /*
43287c478bd9Sstevel@tonic-gate  * If a vertex for fmri exists and it is enabled, send _DISABLE to the
43297c478bd9Sstevel@tonic-gate  * restarter.  If it is running, send _STOP.  Send _REMOVE_INSTANCE.  Delete
43307c478bd9Sstevel@tonic-gate  * all property group dependencies, and the dependency on the restarter,
43317c478bd9Sstevel@tonic-gate  * disposing of vertices as appropriate.  If other vertices depend on this
43327c478bd9Sstevel@tonic-gate  * one, mark it unconfigured and return.  Otherwise remove the vertex.  Always
43337c478bd9Sstevel@tonic-gate  * returns 0.
43347c478bd9Sstevel@tonic-gate  */
43357c478bd9Sstevel@tonic-gate static int
43367c478bd9Sstevel@tonic-gate dgraph_remove_instance(const char *fmri, scf_handle_t *h)
43377c478bd9Sstevel@tonic-gate {
43387c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
43397c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
43407c478bd9Sstevel@tonic-gate 	uu_list_t *old_deps;
43417c478bd9Sstevel@tonic-gate 	int err;
43427c478bd9Sstevel@tonic-gate 
43437c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph engine: Removing %s.\n", fmri);
43447c478bd9Sstevel@tonic-gate 
43457c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
43467c478bd9Sstevel@tonic-gate 
43477c478bd9Sstevel@tonic-gate 	v = vertex_get_by_name(fmri);
43487c478bd9Sstevel@tonic-gate 	if (v == NULL) {
43497c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
43507c478bd9Sstevel@tonic-gate 		return (0);
43517c478bd9Sstevel@tonic-gate 	}
43527c478bd9Sstevel@tonic-gate 
43537c478bd9Sstevel@tonic-gate 	/* Send restarter delete event. */
43547c478bd9Sstevel@tonic-gate 	if (v->gv_flags & GV_CONFIGURED)
43557c478bd9Sstevel@tonic-gate 		graph_unset_restarter(v);
43567c478bd9Sstevel@tonic-gate 
43577c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
43587c478bd9Sstevel@tonic-gate 		/*
43597c478bd9Sstevel@tonic-gate 		 * Make a list of v's current dependencies so we can
43607c478bd9Sstevel@tonic-gate 		 * reevaluate their GV_INSUBGRAPH flags after the dependencies
43617c478bd9Sstevel@tonic-gate 		 * are removed.
43627c478bd9Sstevel@tonic-gate 		 */
43637c478bd9Sstevel@tonic-gate 		old_deps = startd_list_create(graph_edge_pool, NULL, 0);
43647c478bd9Sstevel@tonic-gate 
43657c478bd9Sstevel@tonic-gate 		err = uu_list_walk(v->gv_dependencies,
43663ad28c1eSrm88369 		    (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0);
43677c478bd9Sstevel@tonic-gate 		assert(err == 0);
43687c478bd9Sstevel@tonic-gate 	}
43697c478bd9Sstevel@tonic-gate 
43707c478bd9Sstevel@tonic-gate 	delete_instance_dependencies(v, B_TRUE);
43717c478bd9Sstevel@tonic-gate 
43727c478bd9Sstevel@tonic-gate 	/*
43737c478bd9Sstevel@tonic-gate 	 * Deleting an instance can both satisfy and unsatisfy dependencies,
43747c478bd9Sstevel@tonic-gate 	 * depending on their type.  First propagate the stop as a RERR_RESTART
43757c478bd9Sstevel@tonic-gate 	 * event -- deletion isn't a fault, just a normal stop.  This gives
43767c478bd9Sstevel@tonic-gate 	 * dependent services the chance to do a clean shutdown.  Then, mark
43777c478bd9Sstevel@tonic-gate 	 * the service as unconfigured and propagate the start event for the
43787c478bd9Sstevel@tonic-gate 	 * optional_all dependencies that might have become satisfied.
43797c478bd9Sstevel@tonic-gate 	 */
43807c478bd9Sstevel@tonic-gate 	graph_walk_dependents(v, propagate_stop, (void *)RERR_RESTART);
43817c478bd9Sstevel@tonic-gate 
43827c478bd9Sstevel@tonic-gate 	v->gv_flags &= ~GV_CONFIGURED;
4383*70cbfe41SPhilippe Jung 	v->gv_flags &= ~GV_DEATHROW;
43847c478bd9Sstevel@tonic-gate 
43857c478bd9Sstevel@tonic-gate 	graph_walk_dependents(v, propagate_start, NULL);
43867c478bd9Sstevel@tonic-gate 	propagate_satbility(v);
43877c478bd9Sstevel@tonic-gate 
43887c478bd9Sstevel@tonic-gate 	/*
43897c478bd9Sstevel@tonic-gate 	 * If there are no (non-service) dependents, the vertex can be
43907c478bd9Sstevel@tonic-gate 	 * completely removed.
43917c478bd9Sstevel@tonic-gate 	 */
43923ad28c1eSrm88369 	if (v != milestone && v->gv_refs == 0 &&
43933ad28c1eSrm88369 	    uu_list_numnodes(v->gv_dependents) == 1)
43947c478bd9Sstevel@tonic-gate 		remove_inst_vertex(v);
43957c478bd9Sstevel@tonic-gate 
43967c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
43977c478bd9Sstevel@tonic-gate 		void *cookie = NULL;
43987c478bd9Sstevel@tonic-gate 
43997c478bd9Sstevel@tonic-gate 		while ((e = uu_list_teardown(old_deps, &cookie)) != NULL) {
44003ad28c1eSrm88369 			v = e->ge_vertex;
44013ad28c1eSrm88369 
44023ad28c1eSrm88369 			if (vertex_unref(v) == VERTEX_INUSE)
44033ad28c1eSrm88369 				while (eval_subgraph(v, h) == ECONNABORTED)
44047c478bd9Sstevel@tonic-gate 					libscf_handle_rebind(h);
44057c478bd9Sstevel@tonic-gate 
44067c478bd9Sstevel@tonic-gate 			startd_free(e, sizeof (*e));
44077c478bd9Sstevel@tonic-gate 		}
44087c478bd9Sstevel@tonic-gate 
44097c478bd9Sstevel@tonic-gate 		uu_list_destroy(old_deps);
44107c478bd9Sstevel@tonic-gate 	}
44117c478bd9Sstevel@tonic-gate 
44127c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
44137c478bd9Sstevel@tonic-gate 
44147c478bd9Sstevel@tonic-gate 	return (0);
44157c478bd9Sstevel@tonic-gate }
44167c478bd9Sstevel@tonic-gate 
44177c478bd9Sstevel@tonic-gate /*
44187c478bd9Sstevel@tonic-gate  * Return the eventual (maybe current) milestone in the form of a
44197c478bd9Sstevel@tonic-gate  * legacy runlevel.
44207c478bd9Sstevel@tonic-gate  */
44217c478bd9Sstevel@tonic-gate static char
44227c478bd9Sstevel@tonic-gate target_milestone_as_runlevel()
44237c478bd9Sstevel@tonic-gate {
44247c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
44257c478bd9Sstevel@tonic-gate 
44267c478bd9Sstevel@tonic-gate 	if (milestone == NULL)
44277c478bd9Sstevel@tonic-gate 		return ('3');
44287c478bd9Sstevel@tonic-gate 	else if (milestone == MILESTONE_NONE)
44297c478bd9Sstevel@tonic-gate 		return ('0');
44307c478bd9Sstevel@tonic-gate 
44317c478bd9Sstevel@tonic-gate 	if (strcmp(milestone->gv_name, multi_user_fmri) == 0)
44327c478bd9Sstevel@tonic-gate 		return ('2');
44337c478bd9Sstevel@tonic-gate 	else if (strcmp(milestone->gv_name, single_user_fmri) == 0)
44347c478bd9Sstevel@tonic-gate 		return ('S');
44357c478bd9Sstevel@tonic-gate 	else if (strcmp(milestone->gv_name, multi_user_svr_fmri) == 0)
44367c478bd9Sstevel@tonic-gate 		return ('3');
44377c478bd9Sstevel@tonic-gate 
44387c478bd9Sstevel@tonic-gate #ifndef NDEBUG
44397c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:%d: Unknown milestone name \"%s\".\n",
44407c478bd9Sstevel@tonic-gate 	    __FILE__, __LINE__, milestone->gv_name);
44417c478bd9Sstevel@tonic-gate #endif
44427c478bd9Sstevel@tonic-gate 	abort();
44437c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
44447c478bd9Sstevel@tonic-gate }
44457c478bd9Sstevel@tonic-gate 
44467c478bd9Sstevel@tonic-gate static struct {
44477c478bd9Sstevel@tonic-gate 	char	rl;
44487c478bd9Sstevel@tonic-gate 	int	sig;
44497c478bd9Sstevel@tonic-gate } init_sigs[] = {
44507c478bd9Sstevel@tonic-gate 	{ 'S', SIGBUS },
44517c478bd9Sstevel@tonic-gate 	{ '0', SIGINT },
44527c478bd9Sstevel@tonic-gate 	{ '1', SIGQUIT },
44537c478bd9Sstevel@tonic-gate 	{ '2', SIGILL },
44547c478bd9Sstevel@tonic-gate 	{ '3', SIGTRAP },
44557c478bd9Sstevel@tonic-gate 	{ '4', SIGIOT },
44567c478bd9Sstevel@tonic-gate 	{ '5', SIGEMT },
44577c478bd9Sstevel@tonic-gate 	{ '6', SIGFPE },
44587c478bd9Sstevel@tonic-gate 	{ 0, 0 }
44597c478bd9Sstevel@tonic-gate };
44607c478bd9Sstevel@tonic-gate 
44617c478bd9Sstevel@tonic-gate static void
44627c478bd9Sstevel@tonic-gate signal_init(char rl)
44637c478bd9Sstevel@tonic-gate {
44647c478bd9Sstevel@tonic-gate 	pid_t init_pid;
44657c478bd9Sstevel@tonic-gate 	int i;
44667c478bd9Sstevel@tonic-gate 
44677c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
44687c478bd9Sstevel@tonic-gate 
44697c478bd9Sstevel@tonic-gate 	if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
44707c478bd9Sstevel@tonic-gate 	    sizeof (init_pid)) != sizeof (init_pid)) {
44717c478bd9Sstevel@tonic-gate 		log_error(LOG_NOTICE, "Could not get pid to signal init.\n");
44727c478bd9Sstevel@tonic-gate 		return;
44737c478bd9Sstevel@tonic-gate 	}
44747c478bd9Sstevel@tonic-gate 
44757c478bd9Sstevel@tonic-gate 	for (i = 0; init_sigs[i].rl != 0; ++i)
44767c478bd9Sstevel@tonic-gate 		if (init_sigs[i].rl == rl)
44777c478bd9Sstevel@tonic-gate 			break;
44787c478bd9Sstevel@tonic-gate 
44797c478bd9Sstevel@tonic-gate 	if (init_sigs[i].rl != 0) {
44807c478bd9Sstevel@tonic-gate 		if (kill(init_pid, init_sigs[i].sig) != 0) {
44817c478bd9Sstevel@tonic-gate 			switch (errno) {
44827c478bd9Sstevel@tonic-gate 			case EPERM:
44837c478bd9Sstevel@tonic-gate 			case ESRCH:
44847c478bd9Sstevel@tonic-gate 				log_error(LOG_NOTICE, "Could not signal init: "
44857c478bd9Sstevel@tonic-gate 				    "%s.\n", strerror(errno));
44867c478bd9Sstevel@tonic-gate 				break;
44877c478bd9Sstevel@tonic-gate 
44887c478bd9Sstevel@tonic-gate 			case EINVAL:
44897c478bd9Sstevel@tonic-gate 			default:
44907c478bd9Sstevel@tonic-gate 				bad_error("kill", errno);
44917c478bd9Sstevel@tonic-gate 			}
44927c478bd9Sstevel@tonic-gate 		}
44937c478bd9Sstevel@tonic-gate 	}
44947c478bd9Sstevel@tonic-gate }
44957c478bd9Sstevel@tonic-gate 
44967c478bd9Sstevel@tonic-gate /*
44977c478bd9Sstevel@tonic-gate  * This is called when one of the major milestones changes state, or when
44987c478bd9Sstevel@tonic-gate  * init is signalled and tells us it was told to change runlevel.  We wait
44997c478bd9Sstevel@tonic-gate  * to reach the milestone because this allows /etc/inittab entries to retain
45007c478bd9Sstevel@tonic-gate  * some boot ordering: historically, entries could place themselves before/after
45017c478bd9Sstevel@tonic-gate  * the running of /sbin/rcX scripts but we can no longer make the
45027c478bd9Sstevel@tonic-gate  * distinction because the /sbin/rcX scripts no longer exist as punctuation
45037c478bd9Sstevel@tonic-gate  * marks in /etc/inittab.
45047c478bd9Sstevel@tonic-gate  *
45057c478bd9Sstevel@tonic-gate  * Also, we only trigger an update when we reach the eventual target
45067c478bd9Sstevel@tonic-gate  * milestone: without this, an /etc/inittab entry marked only for
45077c478bd9Sstevel@tonic-gate  * runlevel 2 would be executed for runlevel 3, which is not how
45087c478bd9Sstevel@tonic-gate  * /etc/inittab entries work.
45097c478bd9Sstevel@tonic-gate  *
45107c478bd9Sstevel@tonic-gate  * If we're single user coming online, then we set utmpx to the target
45117c478bd9Sstevel@tonic-gate  * runlevel so that legacy scripts can work as expected.
45127c478bd9Sstevel@tonic-gate  */
45137c478bd9Sstevel@tonic-gate static void
45147c478bd9Sstevel@tonic-gate graph_runlevel_changed(char rl, int online)
45157c478bd9Sstevel@tonic-gate {
45167c478bd9Sstevel@tonic-gate 	char trl;
45177c478bd9Sstevel@tonic-gate 
45187c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&dgraph_lock));
45197c478bd9Sstevel@tonic-gate 
45207c478bd9Sstevel@tonic-gate 	trl = target_milestone_as_runlevel();
45217c478bd9Sstevel@tonic-gate 
45227c478bd9Sstevel@tonic-gate 	if (online) {
45237c478bd9Sstevel@tonic-gate 		if (rl == trl) {
4524965e507bSrm88369 			current_runlevel = trl;
45257c478bd9Sstevel@tonic-gate 			signal_init(trl);
45267c478bd9Sstevel@tonic-gate 		} else if (rl == 'S') {
45277c478bd9Sstevel@tonic-gate 			/*
45287c478bd9Sstevel@tonic-gate 			 * At boot, set the entry early for the benefit of the
45297c478bd9Sstevel@tonic-gate 			 * legacy init scripts.
45307c478bd9Sstevel@tonic-gate 			 */
45317c478bd9Sstevel@tonic-gate 			utmpx_set_runlevel(trl, 'S', B_FALSE);
45327c478bd9Sstevel@tonic-gate 		}
45337c478bd9Sstevel@tonic-gate 	} else {
45347c478bd9Sstevel@tonic-gate 		if (rl == '3' && trl == '2') {
4535965e507bSrm88369 			current_runlevel = trl;
45367c478bd9Sstevel@tonic-gate 			signal_init(trl);
45377c478bd9Sstevel@tonic-gate 		} else if (rl == '2' && trl == 'S') {
4538965e507bSrm88369 			current_runlevel = trl;
45397c478bd9Sstevel@tonic-gate 			signal_init(trl);
45407c478bd9Sstevel@tonic-gate 		}
45417c478bd9Sstevel@tonic-gate 	}
45427c478bd9Sstevel@tonic-gate }
45437c478bd9Sstevel@tonic-gate 
45447c478bd9Sstevel@tonic-gate /*
45457c478bd9Sstevel@tonic-gate  * Move to a backwards-compatible runlevel by executing the appropriate
45467c478bd9Sstevel@tonic-gate  * /etc/rc?.d/K* scripts and/or setting the milestone.
45477c478bd9Sstevel@tonic-gate  *
45487c478bd9Sstevel@tonic-gate  * Returns
45497c478bd9Sstevel@tonic-gate  *   0 - success
45507c478bd9Sstevel@tonic-gate  *   ECONNRESET - success, but handle was reset
45517c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
45527c478bd9Sstevel@tonic-gate  *   ECANCELED - pg was deleted
45537c478bd9Sstevel@tonic-gate  */
45547c478bd9Sstevel@tonic-gate static int
45557c478bd9Sstevel@tonic-gate dgraph_set_runlevel(scf_propertygroup_t *pg, scf_property_t *prop)
45567c478bd9Sstevel@tonic-gate {
45577c478bd9Sstevel@tonic-gate 	char rl;
45587c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
45597c478bd9Sstevel@tonic-gate 	int r;
45607c478bd9Sstevel@tonic-gate 	const char *ms = NULL;	/* what to commit as options/milestone */
45617c478bd9Sstevel@tonic-gate 	boolean_t rebound = B_FALSE;
45627c478bd9Sstevel@tonic-gate 	int mark_rl = 0;
45637c478bd9Sstevel@tonic-gate 
45647c478bd9Sstevel@tonic-gate 	const char * const stop = "stop";
45657c478bd9Sstevel@tonic-gate 
45667c478bd9Sstevel@tonic-gate 	r = libscf_extract_runlevel(prop, &rl);
45677c478bd9Sstevel@tonic-gate 	switch (r) {
45687c478bd9Sstevel@tonic-gate 	case 0:
45697c478bd9Sstevel@tonic-gate 		break;
45707c478bd9Sstevel@tonic-gate 
45717c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
45727c478bd9Sstevel@tonic-gate 	case ECANCELED:
45737c478bd9Sstevel@tonic-gate 		return (r);
45747c478bd9Sstevel@tonic-gate 
45757c478bd9Sstevel@tonic-gate 	case EINVAL:
45767c478bd9Sstevel@tonic-gate 	case ENOENT:
45777c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "runlevel property is misconfigured; "
45787c478bd9Sstevel@tonic-gate 		    "ignoring.\n");
45797c478bd9Sstevel@tonic-gate 		/* delete the bad property */
45807c478bd9Sstevel@tonic-gate 		goto nolock_out;
45817c478bd9Sstevel@tonic-gate 
45827c478bd9Sstevel@tonic-gate 	default:
45837c478bd9Sstevel@tonic-gate 		bad_error("libscf_extract_runlevel", r);
45847c478bd9Sstevel@tonic-gate 	}
45857c478bd9Sstevel@tonic-gate 
45867c478bd9Sstevel@tonic-gate 	switch (rl) {
45877c478bd9Sstevel@tonic-gate 	case 's':
45887c478bd9Sstevel@tonic-gate 		rl = 'S';
45897c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
45907c478bd9Sstevel@tonic-gate 
45917c478bd9Sstevel@tonic-gate 	case 'S':
45927c478bd9Sstevel@tonic-gate 	case '2':
45937c478bd9Sstevel@tonic-gate 	case '3':
45947c478bd9Sstevel@tonic-gate 		/*
45957c478bd9Sstevel@tonic-gate 		 * These cases cause a milestone change, so
45967c478bd9Sstevel@tonic-gate 		 * graph_runlevel_changed() will eventually deal with
45977c478bd9Sstevel@tonic-gate 		 * signalling init.
45987c478bd9Sstevel@tonic-gate 		 */
45997c478bd9Sstevel@tonic-gate 		break;
46007c478bd9Sstevel@tonic-gate 
46017c478bd9Sstevel@tonic-gate 	case '0':
46027c478bd9Sstevel@tonic-gate 	case '1':
46037c478bd9Sstevel@tonic-gate 	case '4':
46047c478bd9Sstevel@tonic-gate 	case '5':
46057c478bd9Sstevel@tonic-gate 	case '6':
46067c478bd9Sstevel@tonic-gate 		mark_rl = 1;
46077c478bd9Sstevel@tonic-gate 		break;
46087c478bd9Sstevel@tonic-gate 
46097c478bd9Sstevel@tonic-gate 	default:
46107c478bd9Sstevel@tonic-gate 		log_framework(LOG_NOTICE, "Unknown runlevel '%c'.\n", rl);
46117c478bd9Sstevel@tonic-gate 		ms = NULL;
46127c478bd9Sstevel@tonic-gate 		goto nolock_out;
46137c478bd9Sstevel@tonic-gate 	}
46147c478bd9Sstevel@tonic-gate 
46157c478bd9Sstevel@tonic-gate 	h = scf_pg_handle(pg);
46167c478bd9Sstevel@tonic-gate 
46177c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
46187c478bd9Sstevel@tonic-gate 
46197c478bd9Sstevel@tonic-gate 	/*
46207c478bd9Sstevel@tonic-gate 	 * Since this triggers no milestone changes, force it by hand.
46217c478bd9Sstevel@tonic-gate 	 */
46227c478bd9Sstevel@tonic-gate 	if (current_runlevel == '4' && rl == '3')
46237c478bd9Sstevel@tonic-gate 		mark_rl = 1;
46247c478bd9Sstevel@tonic-gate 
4625965e507bSrm88369 	/*
4626965e507bSrm88369 	 * 1. If we are here after an "init X":
4627965e507bSrm88369 	 *
4628965e507bSrm88369 	 * init X
4629965e507bSrm88369 	 *	init/lscf_set_runlevel()
4630965e507bSrm88369 	 *		process_pg_event()
4631965e507bSrm88369 	 *		dgraph_set_runlevel()
4632965e507bSrm88369 	 *
4633965e507bSrm88369 	 * then we haven't passed through graph_runlevel_changed() yet,
4634965e507bSrm88369 	 * therefore 'current_runlevel' has not changed for sure but 'rl' has.
4635965e507bSrm88369 	 * In consequence, if 'rl' is lower than 'current_runlevel', we change
4636965e507bSrm88369 	 * the system runlevel and execute the appropriate /etc/rc?.d/K* scripts
4637965e507bSrm88369 	 * past this test.
4638965e507bSrm88369 	 *
4639965e507bSrm88369 	 * 2. On the other hand, if we are here after a "svcadm milestone":
4640965e507bSrm88369 	 *
4641965e507bSrm88369 	 * svcadm milestone X
4642965e507bSrm88369 	 *	dgraph_set_milestone()
4643965e507bSrm88369 	 *		handle_graph_update_event()
4644965e507bSrm88369 	 *		dgraph_set_instance_state()
4645965e507bSrm88369 	 *		graph_post_X_[online|offline]()
4646965e507bSrm88369 	 *		graph_runlevel_changed()
4647965e507bSrm88369 	 *		signal_init()
4648965e507bSrm88369 	 *			init/lscf_set_runlevel()
4649965e507bSrm88369 	 *				process_pg_event()
4650965e507bSrm88369 	 *				dgraph_set_runlevel()
4651965e507bSrm88369 	 *
4652965e507bSrm88369 	 * then we already passed through graph_runlevel_changed() (by the way
4653965e507bSrm88369 	 * of dgraph_set_milestone()) and 'current_runlevel' may have changed
4654965e507bSrm88369 	 * and already be equal to 'rl' so we are going to return immediately
4655965e507bSrm88369 	 * from dgraph_set_runlevel() without changing the system runlevel and
4656965e507bSrm88369 	 * without executing the /etc/rc?.d/K* scripts.
4657965e507bSrm88369 	 */
46587c478bd9Sstevel@tonic-gate 	if (rl == current_runlevel) {
46597c478bd9Sstevel@tonic-gate 		ms = NULL;
46607c478bd9Sstevel@tonic-gate 		goto out;
46617c478bd9Sstevel@tonic-gate 	}
46627c478bd9Sstevel@tonic-gate 
46637c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Changing to runlevel '%c'.\n", rl);
46647c478bd9Sstevel@tonic-gate 
46657c478bd9Sstevel@tonic-gate 	/*
46667c478bd9Sstevel@tonic-gate 	 * Make sure stop rc scripts see the new settings via who -r.
46677c478bd9Sstevel@tonic-gate 	 */
46687c478bd9Sstevel@tonic-gate 	utmpx_set_runlevel(rl, current_runlevel, B_TRUE);
46697c478bd9Sstevel@tonic-gate 
46707c478bd9Sstevel@tonic-gate 	/*
46717c478bd9Sstevel@tonic-gate 	 * Some run levels don't have a direct correspondence to any
46727c478bd9Sstevel@tonic-gate 	 * milestones, so we have to signal init directly.
46737c478bd9Sstevel@tonic-gate 	 */
46747c478bd9Sstevel@tonic-gate 	if (mark_rl) {
46757c478bd9Sstevel@tonic-gate 		current_runlevel = rl;
46767c478bd9Sstevel@tonic-gate 		signal_init(rl);
46777c478bd9Sstevel@tonic-gate 	}
46787c478bd9Sstevel@tonic-gate 
46797c478bd9Sstevel@tonic-gate 	switch (rl) {
46807c478bd9Sstevel@tonic-gate 	case 'S':
46817c478bd9Sstevel@tonic-gate 		uu_warn("The system is coming down for administration.  "
46827c478bd9Sstevel@tonic-gate 		    "Please wait.\n");
46837c478bd9Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_FALSE);
46847c478bd9Sstevel@tonic-gate 		ms = single_user_fmri;
46857c478bd9Sstevel@tonic-gate 		go_single_user_mode = B_TRUE;
46867c478bd9Sstevel@tonic-gate 		break;
46877c478bd9Sstevel@tonic-gate 
46887c478bd9Sstevel@tonic-gate 	case '0':
46897c478bd9Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
46907c478bd9Sstevel@tonic-gate 		halting = AD_HALT;
46917c478bd9Sstevel@tonic-gate 		goto uadmin;
46927c478bd9Sstevel@tonic-gate 
46937c478bd9Sstevel@tonic-gate 	case '5':
46947c478bd9Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
46957c478bd9Sstevel@tonic-gate 		halting = AD_POWEROFF;
46967c478bd9Sstevel@tonic-gate 		goto uadmin;
46977c478bd9Sstevel@tonic-gate 
46987c478bd9Sstevel@tonic-gate 	case '6':
46997c478bd9Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
47007c478bd9Sstevel@tonic-gate 		halting = AD_BOOT;
47017c478bd9Sstevel@tonic-gate 		goto uadmin;
47027c478bd9Sstevel@tonic-gate 
47037c478bd9Sstevel@tonic-gate uadmin:
47047c478bd9Sstevel@tonic-gate 		uu_warn("The system is coming down.  Please wait.\n");
47057c478bd9Sstevel@tonic-gate 		ms = "none";
47067c478bd9Sstevel@tonic-gate 
47077c478bd9Sstevel@tonic-gate 		/*
47087c478bd9Sstevel@tonic-gate 		 * We can't wait until all services are offline since this
47097c478bd9Sstevel@tonic-gate 		 * thread is responsible for taking them offline.  Instead we
47107c478bd9Sstevel@tonic-gate 		 * set halting to the second argument for uadmin() and call
47117c478bd9Sstevel@tonic-gate 		 * do_uadmin() from dgraph_set_instance_state() when
47127c478bd9Sstevel@tonic-gate 		 * appropriate.
47137c478bd9Sstevel@tonic-gate 		 */
47147c478bd9Sstevel@tonic-gate 		break;
47157c478bd9Sstevel@tonic-gate 
47167c478bd9Sstevel@tonic-gate 	case '1':
47177c478bd9Sstevel@tonic-gate 		if (current_runlevel != 'S') {
47187c478bd9Sstevel@tonic-gate 			uu_warn("Changing to state 1.\n");
47197c478bd9Sstevel@tonic-gate 			fork_rc_script(rl, stop, B_FALSE);
47207c478bd9Sstevel@tonic-gate 		} else {
47217c478bd9Sstevel@tonic-gate 			uu_warn("The system is coming up for administration.  "
47227c478bd9Sstevel@tonic-gate 			    "Please wait.\n");
47237c478bd9Sstevel@tonic-gate 		}
47247c478bd9Sstevel@tonic-gate 		ms = single_user_fmri;
47257c478bd9Sstevel@tonic-gate 		go_to_level1 = B_TRUE;
47267c478bd9Sstevel@tonic-gate 		break;
47277c478bd9Sstevel@tonic-gate 
47287c478bd9Sstevel@tonic-gate 	case '2':
47297c478bd9Sstevel@tonic-gate 		if (current_runlevel == '3' || current_runlevel == '4')
47307c478bd9Sstevel@tonic-gate 			fork_rc_script(rl, stop, B_FALSE);
47317c478bd9Sstevel@tonic-gate 		ms = multi_user_fmri;
47327c478bd9Sstevel@tonic-gate 		break;
47337c478bd9Sstevel@tonic-gate 
47347c478bd9Sstevel@tonic-gate 	case '3':
47357c478bd9Sstevel@tonic-gate 	case '4':
47367c478bd9Sstevel@tonic-gate 		ms = "all";
47377c478bd9Sstevel@tonic-gate 		break;
47387c478bd9Sstevel@tonic-gate 
47397c478bd9Sstevel@tonic-gate 	default:
47407c478bd9Sstevel@tonic-gate #ifndef NDEBUG
47417c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s:%d: Uncaught case %d ('%c').\n",
47427c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__, rl, rl);
47437c478bd9Sstevel@tonic-gate #endif
47447c478bd9Sstevel@tonic-gate 		abort();
47457c478bd9Sstevel@tonic-gate 	}
47467c478bd9Sstevel@tonic-gate 
47477c478bd9Sstevel@tonic-gate out:
47487c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
47497c478bd9Sstevel@tonic-gate 
47507c478bd9Sstevel@tonic-gate nolock_out:
47517c478bd9Sstevel@tonic-gate 	switch (r = libscf_clear_runlevel(pg, ms)) {
47527c478bd9Sstevel@tonic-gate 	case 0:
47537c478bd9Sstevel@tonic-gate 		break;
47547c478bd9Sstevel@tonic-gate 
47557c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
47567c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
47577c478bd9Sstevel@tonic-gate 		rebound = B_TRUE;
47587c478bd9Sstevel@tonic-gate 		goto nolock_out;
47597c478bd9Sstevel@tonic-gate 
47607c478bd9Sstevel@tonic-gate 	case ECANCELED:
47617c478bd9Sstevel@tonic-gate 		break;
47627c478bd9Sstevel@tonic-gate 
47637c478bd9Sstevel@tonic-gate 	case EPERM:
47647c478bd9Sstevel@tonic-gate 	case EACCES:
47657c478bd9Sstevel@tonic-gate 	case EROFS:
47667c478bd9Sstevel@tonic-gate 		log_error(LOG_NOTICE, "Could not delete \"%s/%s\" property: "
47677c478bd9Sstevel@tonic-gate 		    "%s.\n", SCF_PG_OPTIONS, "runlevel", strerror(r));
47687c478bd9Sstevel@tonic-gate 		break;
47697c478bd9Sstevel@tonic-gate 
47707c478bd9Sstevel@tonic-gate 	default:
47717c478bd9Sstevel@tonic-gate 		bad_error("libscf_clear_runlevel", r);
47727c478bd9Sstevel@tonic-gate 	}
47737c478bd9Sstevel@tonic-gate 
47747c478bd9Sstevel@tonic-gate 	return (rebound ? ECONNRESET : 0);
47757c478bd9Sstevel@tonic-gate }
47767c478bd9Sstevel@tonic-gate 
47777c478bd9Sstevel@tonic-gate static int
47787c478bd9Sstevel@tonic-gate mark_subgraph(graph_edge_t *e, void *arg)
47797c478bd9Sstevel@tonic-gate {
47807c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
47817c478bd9Sstevel@tonic-gate 	int r;
47827c478bd9Sstevel@tonic-gate 	int optional = (int)arg;
47837c478bd9Sstevel@tonic-gate 
47847c478bd9Sstevel@tonic-gate 	v = e->ge_vertex;
47857c478bd9Sstevel@tonic-gate 
47867c478bd9Sstevel@tonic-gate 	/* If it's already in the subgraph, skip. */
47877c478bd9Sstevel@tonic-gate 	if (v->gv_flags & GV_INSUBGRAPH)
47887c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
47897c478bd9Sstevel@tonic-gate 
47907c478bd9Sstevel@tonic-gate 	/*
47917c478bd9Sstevel@tonic-gate 	 * Keep track if walk has entered an optional dependency group
47927c478bd9Sstevel@tonic-gate 	 */
47937c478bd9Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_OPTIONAL_ALL) {
47947c478bd9Sstevel@tonic-gate 		optional = 1;
47957c478bd9Sstevel@tonic-gate 	}
47967c478bd9Sstevel@tonic-gate 	/*
47977c478bd9Sstevel@tonic-gate 	 * Quit if we are in an optional dependency group and the instance
47987c478bd9Sstevel@tonic-gate 	 * is disabled
47997c478bd9Sstevel@tonic-gate 	 */
48007c478bd9Sstevel@tonic-gate 	if (optional && (v->gv_type == GVT_INST) &&
48017c478bd9Sstevel@tonic-gate 	    (!(v->gv_flags & GV_ENBLD_NOOVR)))
48027c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
48037c478bd9Sstevel@tonic-gate 
48047c478bd9Sstevel@tonic-gate 	v->gv_flags |= GV_INSUBGRAPH;
48057c478bd9Sstevel@tonic-gate 
48067c478bd9Sstevel@tonic-gate 	/* Skip all excluded dependencies. */
48077c478bd9Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
48087c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
48097c478bd9Sstevel@tonic-gate 
48107c478bd9Sstevel@tonic-gate 	r = uu_list_walk(v->gv_dependencies, (uu_walk_fn_t *)mark_subgraph,
48117c478bd9Sstevel@tonic-gate 	    (void *)optional, 0);
48127c478bd9Sstevel@tonic-gate 	assert(r == 0);
48137c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
48147c478bd9Sstevel@tonic-gate }
48157c478bd9Sstevel@tonic-gate 
48167c478bd9Sstevel@tonic-gate /*
481756e23938Sbustos  * Bring down all services which are not dependencies of fmri.  The
481856e23938Sbustos  * dependencies of fmri (direct & indirect) will constitute the "subgraph",
481956e23938Sbustos  * and will have the GV_INSUBGRAPH flag set.  The rest must be brought down,
482056e23938Sbustos  * which means the state is "disabled", "maintenance", or "uninitialized".  We
482156e23938Sbustos  * could consider "offline" to be down, and refrain from sending start
482256e23938Sbustos  * commands for such services, but that's not strictly necessary, so we'll
482356e23938Sbustos  * decline to intrude on the state machine.  It would probably confuse users
482456e23938Sbustos  * anyway.
482556e23938Sbustos  *
482656e23938Sbustos  * The services should be brought down in reverse-dependency order, so we
482756e23938Sbustos  * can't do it all at once here.  We initiate by override-disabling the leaves
482856e23938Sbustos  * of the dependency tree -- those services which are up but have no
482956e23938Sbustos  * dependents which are up.  When they come down,
483056e23938Sbustos  * vertex_subgraph_dependencies_shutdown() will override-disable the newly
483156e23938Sbustos  * exposed leaves.  Perseverance will ensure completion.
483256e23938Sbustos  *
483356e23938Sbustos  * Sometimes we need to take action when the transition is complete, like
483456e23938Sbustos  * start sulogin or halt the system.  To tell when we're done, we initialize
483556e23938Sbustos  * non_subgraph_svcs here to be the number of services which need to come
483656e23938Sbustos  * down.  As each does, we decrement the counter.  When it hits zero, we take
483756e23938Sbustos  * the appropriate action.  See vertex_subgraph_dependencies_shutdown().
483856e23938Sbustos  *
483956e23938Sbustos  * In case we're coming up, we also remove any enable-overrides for the
484056e23938Sbustos  * services which are dependencies of fmri.
48417c478bd9Sstevel@tonic-gate  *
48427c478bd9Sstevel@tonic-gate  * If norepository is true, the function will not change the repository.
48437c478bd9Sstevel@tonic-gate  *
4844965e507bSrm88369  * The decision to change the system run level in accordance with the milestone
4845965e507bSrm88369  * is taken in dgraph_set_runlevel().
4846965e507bSrm88369  *
48477c478bd9Sstevel@tonic-gate  * Returns
48487c478bd9Sstevel@tonic-gate  *   0 - success
48497c478bd9Sstevel@tonic-gate  *   ECONNRESET - success, but handle was rebound
48507c478bd9Sstevel@tonic-gate  *   EINVAL - fmri is invalid (error is logged)
48517c478bd9Sstevel@tonic-gate  *   EALREADY - the milestone is already set to fmri
48527c478bd9Sstevel@tonic-gate  *   ENOENT - a configured vertex does not exist for fmri (an error is logged)
48537c478bd9Sstevel@tonic-gate  */
48547c478bd9Sstevel@tonic-gate static int
48557c478bd9Sstevel@tonic-gate dgraph_set_milestone(const char *fmri, scf_handle_t *h, boolean_t norepository)
48567c478bd9Sstevel@tonic-gate {
48577c478bd9Sstevel@tonic-gate 	const char *cfmri, *fs;
48587c478bd9Sstevel@tonic-gate 	graph_vertex_t *nm, *v;
48597c478bd9Sstevel@tonic-gate 	int ret = 0, r;
48607c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
48617c478bd9Sstevel@tonic-gate 	boolean_t isall, isnone, rebound = B_FALSE;
48627c478bd9Sstevel@tonic-gate 
48637c478bd9Sstevel@tonic-gate 	/* Validate fmri */
48647c478bd9Sstevel@tonic-gate 	isall = (strcmp(fmri, "all") == 0);
48657c478bd9Sstevel@tonic-gate 	isnone = (strcmp(fmri, "none") == 0);
48667c478bd9Sstevel@tonic-gate 
48677c478bd9Sstevel@tonic-gate 	if (!isall && !isnone) {
48687c478bd9Sstevel@tonic-gate 		if (fmri_canonify(fmri, (char **)&cfmri, B_FALSE) == EINVAL)
48697c478bd9Sstevel@tonic-gate 			goto reject;
48707c478bd9Sstevel@tonic-gate 
48717c478bd9Sstevel@tonic-gate 		if (strcmp(cfmri, single_user_fmri) != 0 &&
48727c478bd9Sstevel@tonic-gate 		    strcmp(cfmri, multi_user_fmri) != 0 &&
48737c478bd9Sstevel@tonic-gate 		    strcmp(cfmri, multi_user_svr_fmri) != 0) {
48747c478bd9Sstevel@tonic-gate 			startd_free((void *)cfmri, max_scf_fmri_size);
48757c478bd9Sstevel@tonic-gate reject:
48767c478bd9Sstevel@tonic-gate 			log_framework(LOG_WARNING,
48777c478bd9Sstevel@tonic-gate 			    "Rejecting request for invalid milestone \"%s\".\n",
48787c478bd9Sstevel@tonic-gate 			    fmri);
48797c478bd9Sstevel@tonic-gate 			return (EINVAL);
48807c478bd9Sstevel@tonic-gate 		}
48817c478bd9Sstevel@tonic-gate 	}
48827c478bd9Sstevel@tonic-gate 
48837c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
48847c478bd9Sstevel@tonic-gate 
48857c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
48867c478bd9Sstevel@tonic-gate 
48877c478bd9Sstevel@tonic-gate 	if (milestone == NULL) {
48887c478bd9Sstevel@tonic-gate 		if (isall) {
48897c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
48907c478bd9Sstevel@tonic-gate 			    "Milestone already set to all.\n");
48917c478bd9Sstevel@tonic-gate 			ret = EALREADY;
48927c478bd9Sstevel@tonic-gate 			goto out;
48937c478bd9Sstevel@tonic-gate 		}
48947c478bd9Sstevel@tonic-gate 	} else if (milestone == MILESTONE_NONE) {
48957c478bd9Sstevel@tonic-gate 		if (isnone) {
48967c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
48977c478bd9Sstevel@tonic-gate 			    "Milestone already set to none.\n");
48987c478bd9Sstevel@tonic-gate 			ret = EALREADY;
48997c478bd9Sstevel@tonic-gate 			goto out;
49007c478bd9Sstevel@tonic-gate 		}
49017c478bd9Sstevel@tonic-gate 	} else {
49027c478bd9Sstevel@tonic-gate 		if (!isall && !isnone &&
49037c478bd9Sstevel@tonic-gate 		    strcmp(cfmri, milestone->gv_name) == 0) {
49047c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
49057c478bd9Sstevel@tonic-gate 			    "Milestone already set to %s.\n", cfmri);
49067c478bd9Sstevel@tonic-gate 			ret = EALREADY;
49077c478bd9Sstevel@tonic-gate 			goto out;
49087c478bd9Sstevel@tonic-gate 		}
49097c478bd9Sstevel@tonic-gate 	}
49107c478bd9Sstevel@tonic-gate 
49117c478bd9Sstevel@tonic-gate 	if (!isall && !isnone) {
49127c478bd9Sstevel@tonic-gate 		nm = vertex_get_by_name(cfmri);
49137c478bd9Sstevel@tonic-gate 		if (nm == NULL || !(nm->gv_flags & GV_CONFIGURED)) {
49147c478bd9Sstevel@tonic-gate 			log_framework(LOG_WARNING, "Cannot set milestone to %s "
49157c478bd9Sstevel@tonic-gate 			    "because no such service exists.\n", cfmri);
49167c478bd9Sstevel@tonic-gate 			ret = ENOENT;
49177c478bd9Sstevel@tonic-gate 			goto out;
49187c478bd9Sstevel@tonic-gate 		}
49197c478bd9Sstevel@tonic-gate 	}
49207c478bd9Sstevel@tonic-gate 
49217c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Changing milestone to %s.\n", fmri);
49227c478bd9Sstevel@tonic-gate 
49237c478bd9Sstevel@tonic-gate 	/*
49247c478bd9Sstevel@tonic-gate 	 * Set milestone, removing the old one if this was the last reference.
49257c478bd9Sstevel@tonic-gate 	 */
49263ad28c1eSrm88369 	if (milestone > MILESTONE_NONE)
49273ad28c1eSrm88369 		(void) vertex_unref(milestone);
49287c478bd9Sstevel@tonic-gate 
49297c478bd9Sstevel@tonic-gate 	if (isall)
49307c478bd9Sstevel@tonic-gate 		milestone = NULL;
49317c478bd9Sstevel@tonic-gate 	else if (isnone)
49327c478bd9Sstevel@tonic-gate 		milestone = MILESTONE_NONE;
49333ad28c1eSrm88369 	else {
49347c478bd9Sstevel@tonic-gate 		milestone = nm;
49353ad28c1eSrm88369 		/* milestone should count as a reference */
49363ad28c1eSrm88369 		vertex_ref(milestone);
49373ad28c1eSrm88369 	}
49387c478bd9Sstevel@tonic-gate 
49397c478bd9Sstevel@tonic-gate 	/* Clear all GV_INSUBGRAPH bits. */
49407c478bd9Sstevel@tonic-gate 	for (v = uu_list_first(dgraph); v != NULL; v = uu_list_next(dgraph, v))
49417c478bd9Sstevel@tonic-gate 		v->gv_flags &= ~GV_INSUBGRAPH;
49427c478bd9Sstevel@tonic-gate 
49437c478bd9Sstevel@tonic-gate 	if (!isall && !isnone) {
49447c478bd9Sstevel@tonic-gate 		/* Set GV_INSUBGRAPH for milestone & descendents. */
49457c478bd9Sstevel@tonic-gate 		milestone->gv_flags |= GV_INSUBGRAPH;
49467c478bd9Sstevel@tonic-gate 
49477c478bd9Sstevel@tonic-gate 		r = uu_list_walk(milestone->gv_dependencies,
49487c478bd9Sstevel@tonic-gate 		    (uu_walk_fn_t *)mark_subgraph, NULL, 0);
49497c478bd9Sstevel@tonic-gate 		assert(r == 0);
49507c478bd9Sstevel@tonic-gate 	}
49517c478bd9Sstevel@tonic-gate 
49527c478bd9Sstevel@tonic-gate 	/* Un-override services in the subgraph & override-disable the rest. */
49537c478bd9Sstevel@tonic-gate 	if (norepository)
49547c478bd9Sstevel@tonic-gate 		goto out;
49557c478bd9Sstevel@tonic-gate 
49567c478bd9Sstevel@tonic-gate 	non_subgraph_svcs = 0;
49577c478bd9Sstevel@tonic-gate 	for (v = uu_list_first(dgraph);
49587c478bd9Sstevel@tonic-gate 	    v != NULL;
49597c478bd9Sstevel@tonic-gate 	    v = uu_list_next(dgraph, v)) {
49607c478bd9Sstevel@tonic-gate 		if (v->gv_type != GVT_INST ||
49617c478bd9Sstevel@tonic-gate 		    (v->gv_flags & GV_CONFIGURED) == 0)
49627c478bd9Sstevel@tonic-gate 			continue;
49637c478bd9Sstevel@tonic-gate 
49647c478bd9Sstevel@tonic-gate again:
49657c478bd9Sstevel@tonic-gate 		r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst,
49667c478bd9Sstevel@tonic-gate 		    NULL, NULL, SCF_DECODE_FMRI_EXACT);
49677c478bd9Sstevel@tonic-gate 		if (r != 0) {
49687c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
49697c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
49707c478bd9Sstevel@tonic-gate 			default:
49717c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
49727c478bd9Sstevel@tonic-gate 				rebound = B_TRUE;
49737c478bd9Sstevel@tonic-gate 				goto again;
49747c478bd9Sstevel@tonic-gate 
49757c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
49767c478bd9Sstevel@tonic-gate 				continue;
49777c478bd9Sstevel@tonic-gate 
49787c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
49797c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
49807c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
49817c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
49827c478bd9Sstevel@tonic-gate 				bad_error("scf_handle_decode_fmri",
49837c478bd9Sstevel@tonic-gate 				    scf_error());
49847c478bd9Sstevel@tonic-gate 			}
49857c478bd9Sstevel@tonic-gate 		}
49867c478bd9Sstevel@tonic-gate 
49877c478bd9Sstevel@tonic-gate 		if (isall || (v->gv_flags & GV_INSUBGRAPH)) {
49887c478bd9Sstevel@tonic-gate 			r = libscf_delete_enable_ovr(inst);
49897c478bd9Sstevel@tonic-gate 			fs = "libscf_delete_enable_ovr";
49907c478bd9Sstevel@tonic-gate 		} else {
49917c478bd9Sstevel@tonic-gate 			assert(isnone || (v->gv_flags & GV_INSUBGRAPH) == 0);
49927c478bd9Sstevel@tonic-gate 
499356e23938Sbustos 			/*
499456e23938Sbustos 			 * Services which are up need to come down before
499556e23938Sbustos 			 * we're done, but we can only disable the leaves
499656e23938Sbustos 			 * here.
499756e23938Sbustos 			 */
499856e23938Sbustos 
499956e23938Sbustos 			if (up_state(v->gv_state))
50007c478bd9Sstevel@tonic-gate 				++non_subgraph_svcs;
50017c478bd9Sstevel@tonic-gate 
500256e23938Sbustos 			/* If it's already disabled, don't bother. */
500356e23938Sbustos 			if ((v->gv_flags & GV_ENABLED) == 0)
500456e23938Sbustos 				continue;
500556e23938Sbustos 
500656e23938Sbustos 			if (!is_nonsubgraph_leaf(v))
50077c478bd9Sstevel@tonic-gate 				continue;
50087c478bd9Sstevel@tonic-gate 
50097c478bd9Sstevel@tonic-gate 			r = libscf_set_enable_ovr(inst, 0);
50107c478bd9Sstevel@tonic-gate 			fs = "libscf_set_enable_ovr";
50117c478bd9Sstevel@tonic-gate 		}
50127c478bd9Sstevel@tonic-gate 		switch (r) {
50137c478bd9Sstevel@tonic-gate 		case 0:
50147c478bd9Sstevel@tonic-gate 		case ECANCELED:
50157c478bd9Sstevel@tonic-gate 			break;
50167c478bd9Sstevel@tonic-gate 
50177c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
50187c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
50197c478bd9Sstevel@tonic-gate 			rebound = B_TRUE;
50207c478bd9Sstevel@tonic-gate 			goto again;
50217c478bd9Sstevel@tonic-gate 
50227c478bd9Sstevel@tonic-gate 		case EPERM:
50237c478bd9Sstevel@tonic-gate 		case EROFS:
50247c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
50257c478bd9Sstevel@tonic-gate 			    "Could not set %s/%s for %s: %s.\n",
50267c478bd9Sstevel@tonic-gate 			    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
50277c478bd9Sstevel@tonic-gate 			    v->gv_name, strerror(r));
50287c478bd9Sstevel@tonic-gate 			break;
50297c478bd9Sstevel@tonic-gate 
50307c478bd9Sstevel@tonic-gate 		default:
50317c478bd9Sstevel@tonic-gate 			bad_error(fs, r);
50327c478bd9Sstevel@tonic-gate 		}
50337c478bd9Sstevel@tonic-gate 	}
50347c478bd9Sstevel@tonic-gate 
50357c478bd9Sstevel@tonic-gate 	if (halting != -1) {
50367c478bd9Sstevel@tonic-gate 		if (non_subgraph_svcs > 1)
50377c478bd9Sstevel@tonic-gate 			uu_warn("%d system services are now being stopped.\n",
50387c478bd9Sstevel@tonic-gate 			    non_subgraph_svcs);
50397c478bd9Sstevel@tonic-gate 		else if (non_subgraph_svcs == 1)
50407c478bd9Sstevel@tonic-gate 			uu_warn("One system service is now being stopped.\n");
50417c478bd9Sstevel@tonic-gate 		else if (non_subgraph_svcs == 0)
50427c478bd9Sstevel@tonic-gate 			do_uadmin();
50437c478bd9Sstevel@tonic-gate 	}
50447c478bd9Sstevel@tonic-gate 
50457c478bd9Sstevel@tonic-gate 	ret = rebound ? ECONNRESET : 0;
50467c478bd9Sstevel@tonic-gate 
50477c478bd9Sstevel@tonic-gate out:
50487c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
50497c478bd9Sstevel@tonic-gate 	if (!isall && !isnone)
50507c478bd9Sstevel@tonic-gate 		startd_free((void *)cfmri, max_scf_fmri_size);
50517c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
50527c478bd9Sstevel@tonic-gate 	return (ret);
50537c478bd9Sstevel@tonic-gate }
50547c478bd9Sstevel@tonic-gate 
50557c478bd9Sstevel@tonic-gate 
50567c478bd9Sstevel@tonic-gate /*
50577c478bd9Sstevel@tonic-gate  * Returns 0, ECONNABORTED, or EINVAL.
50587c478bd9Sstevel@tonic-gate  */
50597c478bd9Sstevel@tonic-gate static int
50607c478bd9Sstevel@tonic-gate handle_graph_update_event(scf_handle_t *h, graph_protocol_event_t *e)
50617c478bd9Sstevel@tonic-gate {
50627c478bd9Sstevel@tonic-gate 	int r;
50637c478bd9Sstevel@tonic-gate 
50647c478bd9Sstevel@tonic-gate 	switch (e->gpe_type) {
50657c478bd9Sstevel@tonic-gate 	case GRAPH_UPDATE_RELOAD_GRAPH:
50667c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
50677c478bd9Sstevel@tonic-gate 		    "graph_event: reload graph unimplemented\n");
50687c478bd9Sstevel@tonic-gate 		break;
50697c478bd9Sstevel@tonic-gate 
50707c478bd9Sstevel@tonic-gate 	case GRAPH_UPDATE_STATE_CHANGE: {
50717c478bd9Sstevel@tonic-gate 		protocol_states_t *states = e->gpe_data;
50727c478bd9Sstevel@tonic-gate 
50737c478bd9Sstevel@tonic-gate 		switch (r = dgraph_set_instance_state(h, e->gpe_inst,
50747c478bd9Sstevel@tonic-gate 		    states->ps_state, states->ps_err)) {
50757c478bd9Sstevel@tonic-gate 		case 0:
50767c478bd9Sstevel@tonic-gate 		case ENOENT:
50777c478bd9Sstevel@tonic-gate 			break;
50787c478bd9Sstevel@tonic-gate 
50797c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
50807c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
50817c478bd9Sstevel@tonic-gate 
50827c478bd9Sstevel@tonic-gate 		case EINVAL:
50837c478bd9Sstevel@tonic-gate 		default:
50847c478bd9Sstevel@tonic-gate #ifndef NDEBUG
50857c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "dgraph_set_instance_state() "
50867c478bd9Sstevel@tonic-gate 			    "failed with unexpected error %d at %s:%d.\n", r,
50877c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__);
50887c478bd9Sstevel@tonic-gate #endif
50897c478bd9Sstevel@tonic-gate 			abort();
50907c478bd9Sstevel@tonic-gate 		}
50917c478bd9Sstevel@tonic-gate 
50927c478bd9Sstevel@tonic-gate 		startd_free(states, sizeof (protocol_states_t));
50937c478bd9Sstevel@tonic-gate 		break;
50947c478bd9Sstevel@tonic-gate 	}
50957c478bd9Sstevel@tonic-gate 
50967c478bd9Sstevel@tonic-gate 	default:
50977c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
50987c478bd9Sstevel@tonic-gate 		    "graph_event_loop received an unknown event: %d\n",
50997c478bd9Sstevel@tonic-gate 		    e->gpe_type);
51007c478bd9Sstevel@tonic-gate 		break;
51017c478bd9Sstevel@tonic-gate 	}
51027c478bd9Sstevel@tonic-gate 
51037c478bd9Sstevel@tonic-gate 	return (0);
51047c478bd9Sstevel@tonic-gate }
51057c478bd9Sstevel@tonic-gate 
51067c478bd9Sstevel@tonic-gate /*
51077c478bd9Sstevel@tonic-gate  * graph_event_thread()
51087c478bd9Sstevel@tonic-gate  *    Wait for state changes from the restarters.
51097c478bd9Sstevel@tonic-gate  */
51107c478bd9Sstevel@tonic-gate /*ARGSUSED*/
51117c478bd9Sstevel@tonic-gate void *
51127c478bd9Sstevel@tonic-gate graph_event_thread(void *unused)
51137c478bd9Sstevel@tonic-gate {
51147c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
51157c478bd9Sstevel@tonic-gate 	int err;
51167c478bd9Sstevel@tonic-gate 
51177c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
51187c478bd9Sstevel@tonic-gate 
51197c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
51207c478bd9Sstevel@tonic-gate 	while (1) {
51217c478bd9Sstevel@tonic-gate 		graph_protocol_event_t *e;
51227c478bd9Sstevel@tonic-gate 
51237c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&gu->gu_lock);
51247c478bd9Sstevel@tonic-gate 
51257c478bd9Sstevel@tonic-gate 		while (gu->gu_wakeup == 0)
51267c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&gu->gu_cv, &gu->gu_lock);
51277c478bd9Sstevel@tonic-gate 
51287c478bd9Sstevel@tonic-gate 		gu->gu_wakeup = 0;
51297c478bd9Sstevel@tonic-gate 
51307c478bd9Sstevel@tonic-gate 		while ((e = graph_event_dequeue()) != NULL) {
51317c478bd9Sstevel@tonic-gate 			MUTEX_LOCK(&e->gpe_lock);
51327c478bd9Sstevel@tonic-gate 			MUTEX_UNLOCK(&gu->gu_lock);
51337c478bd9Sstevel@tonic-gate 
51347c478bd9Sstevel@tonic-gate 			while ((err = handle_graph_update_event(h, e)) ==
51357c478bd9Sstevel@tonic-gate 			    ECONNABORTED)
51367c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
51377c478bd9Sstevel@tonic-gate 
51387c478bd9Sstevel@tonic-gate 			if (err == 0)
51397c478bd9Sstevel@tonic-gate 				graph_event_release(e);
51407c478bd9Sstevel@tonic-gate 			else
51417c478bd9Sstevel@tonic-gate 				graph_event_requeue(e);
51427c478bd9Sstevel@tonic-gate 
51437c478bd9Sstevel@tonic-gate 			MUTEX_LOCK(&gu->gu_lock);
51447c478bd9Sstevel@tonic-gate 		}
51457c478bd9Sstevel@tonic-gate 
51467c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&gu->gu_lock);
51477c478bd9Sstevel@tonic-gate 	}
51487c478bd9Sstevel@tonic-gate 
51497c478bd9Sstevel@tonic-gate 	/*
51507c478bd9Sstevel@tonic-gate 	 * Unreachable for now -- there's currently no graceful cleanup
51517c478bd9Sstevel@tonic-gate 	 * called on exit().
51527c478bd9Sstevel@tonic-gate 	 */
51537c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&gu->gu_lock);
51547c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
51557c478bd9Sstevel@tonic-gate 	return (NULL);
51567c478bd9Sstevel@tonic-gate }
51577c478bd9Sstevel@tonic-gate 
51587c478bd9Sstevel@tonic-gate static void
51597c478bd9Sstevel@tonic-gate set_initial_milestone(scf_handle_t *h)
51607c478bd9Sstevel@tonic-gate {
51617c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
51627c478bd9Sstevel@tonic-gate 	char *fmri, *cfmri;
51637c478bd9Sstevel@tonic-gate 	size_t sz;
51647c478bd9Sstevel@tonic-gate 	int r;
51657c478bd9Sstevel@tonic-gate 
51667c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
51677c478bd9Sstevel@tonic-gate 	fmri = startd_alloc(max_scf_fmri_size);
51687c478bd9Sstevel@tonic-gate 
51697c478bd9Sstevel@tonic-gate 	/*
51707c478bd9Sstevel@tonic-gate 	 * If -m milestone= was specified, we want to set options_ovr/milestone
51717c478bd9Sstevel@tonic-gate 	 * to it.  Otherwise we want to read what the milestone should be set
51727c478bd9Sstevel@tonic-gate 	 * to.  Either way we need our inst.
51737c478bd9Sstevel@tonic-gate 	 */
51747c478bd9Sstevel@tonic-gate get_self:
51757c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
51767c478bd9Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
51777c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
51787c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
51797c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
51807c478bd9Sstevel@tonic-gate 			goto get_self;
51817c478bd9Sstevel@tonic-gate 
51827c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
51837c478bd9Sstevel@tonic-gate 			if (st->st_subgraph != NULL &&
51847c478bd9Sstevel@tonic-gate 			    st->st_subgraph[0] != '\0') {
51857c478bd9Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
51867c478bd9Sstevel@tonic-gate 				    max_scf_fmri_size);
51877c478bd9Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
51887c478bd9Sstevel@tonic-gate 			} else {
51897c478bd9Sstevel@tonic-gate 				fmri[0] = '\0';
51907c478bd9Sstevel@tonic-gate 			}
51917c478bd9Sstevel@tonic-gate 			break;
51927c478bd9Sstevel@tonic-gate 
51937c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
51947c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
51957c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
51967c478bd9Sstevel@tonic-gate 		default:
51977c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
51987c478bd9Sstevel@tonic-gate 		}
51997c478bd9Sstevel@tonic-gate 	} else {
52007c478bd9Sstevel@tonic-gate 		if (st->st_subgraph != NULL && st->st_subgraph[0] != '\0') {
52017c478bd9Sstevel@tonic-gate 			scf_propertygroup_t *pg;
52027c478bd9Sstevel@tonic-gate 
52037c478bd9Sstevel@tonic-gate 			pg = safe_scf_pg_create(h);
52047c478bd9Sstevel@tonic-gate 
52057c478bd9Sstevel@tonic-gate 			sz = strlcpy(fmri, st->st_subgraph, max_scf_fmri_size);
52067c478bd9Sstevel@tonic-gate 			assert(sz < max_scf_fmri_size);
52077c478bd9Sstevel@tonic-gate 
52087c478bd9Sstevel@tonic-gate 			r = libscf_inst_get_or_add_pg(inst, SCF_PG_OPTIONS_OVR,
52097c478bd9Sstevel@tonic-gate 			    SCF_PG_OPTIONS_OVR_TYPE, SCF_PG_OPTIONS_OVR_FLAGS,
52107c478bd9Sstevel@tonic-gate 			    pg);
52117c478bd9Sstevel@tonic-gate 			switch (r) {
52127c478bd9Sstevel@tonic-gate 			case 0:
52137c478bd9Sstevel@tonic-gate 				break;
52147c478bd9Sstevel@tonic-gate 
52157c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
52167c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
52177c478bd9Sstevel@tonic-gate 				goto get_self;
52187c478bd9Sstevel@tonic-gate 
52197c478bd9Sstevel@tonic-gate 			case EPERM:
52207c478bd9Sstevel@tonic-gate 			case EACCES:
52217c478bd9Sstevel@tonic-gate 			case EROFS:
52227c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Could not set %s/%s: "
52237c478bd9Sstevel@tonic-gate 				    "%s.\n", SCF_PG_OPTIONS_OVR,
52247c478bd9Sstevel@tonic-gate 				    SCF_PROPERTY_MILESTONE, strerror(r));
52257c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
52267c478bd9Sstevel@tonic-gate 
52277c478bd9Sstevel@tonic-gate 			case ECANCELED:
52287c478bd9Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
52297c478bd9Sstevel@tonic-gate 				    max_scf_fmri_size);
52307c478bd9Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
52317c478bd9Sstevel@tonic-gate 				break;
52327c478bd9Sstevel@tonic-gate 
52337c478bd9Sstevel@tonic-gate 			default:
52347c478bd9Sstevel@tonic-gate 				bad_error("libscf_inst_get_or_add_pg", r);
52357c478bd9Sstevel@tonic-gate 			}
52367c478bd9Sstevel@tonic-gate 
52377c478bd9Sstevel@tonic-gate 			r = libscf_clear_runlevel(pg, fmri);
52387c478bd9Sstevel@tonic-gate 			switch (r) {
52397c478bd9Sstevel@tonic-gate 			case 0:
52407c478bd9Sstevel@tonic-gate 				break;
52417c478bd9Sstevel@tonic-gate 
52427c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
52437c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
52447c478bd9Sstevel@tonic-gate 				goto get_self;
52457c478bd9Sstevel@tonic-gate 
52467c478bd9Sstevel@tonic-gate 			case EPERM:
52477c478bd9Sstevel@tonic-gate 			case EACCES:
52487c478bd9Sstevel@tonic-gate 			case EROFS:
52497c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Could not set %s/%s: "
52507c478bd9Sstevel@tonic-gate 				    "%s.\n", SCF_PG_OPTIONS_OVR,
52517c478bd9Sstevel@tonic-gate 				    SCF_PROPERTY_MILESTONE, strerror(r));
52527c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
52537c478bd9Sstevel@tonic-gate 
52547c478bd9Sstevel@tonic-gate 			case ECANCELED:
52557c478bd9Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
52567c478bd9Sstevel@tonic-gate 				    max_scf_fmri_size);
52577c478bd9Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
52587c478bd9Sstevel@tonic-gate 				break;
52597c478bd9Sstevel@tonic-gate 
52607c478bd9Sstevel@tonic-gate 			default:
52617c478bd9Sstevel@tonic-gate 				bad_error("libscf_clear_runlevel", r);
52627c478bd9Sstevel@tonic-gate 			}
52637c478bd9Sstevel@tonic-gate 
52647c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
52657c478bd9Sstevel@tonic-gate 		} else {
52667c478bd9Sstevel@tonic-gate 			scf_property_t *prop;
52677c478bd9Sstevel@tonic-gate 			scf_value_t *val;
52687c478bd9Sstevel@tonic-gate 
52697c478bd9Sstevel@tonic-gate 			prop = safe_scf_property_create(h);
52707c478bd9Sstevel@tonic-gate 			val = safe_scf_value_create(h);
52717c478bd9Sstevel@tonic-gate 
52727c478bd9Sstevel@tonic-gate 			r = libscf_get_milestone(inst, prop, val, fmri,
52737c478bd9Sstevel@tonic-gate 			    max_scf_fmri_size);
52747c478bd9Sstevel@tonic-gate 			switch (r) {
52757c478bd9Sstevel@tonic-gate 			case 0:
52767c478bd9Sstevel@tonic-gate 				break;
52777c478bd9Sstevel@tonic-gate 
52787c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
52797c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
52807c478bd9Sstevel@tonic-gate 				goto get_self;
52817c478bd9Sstevel@tonic-gate 
52827c478bd9Sstevel@tonic-gate 			case EINVAL:
52837c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Milestone property is "
52847c478bd9Sstevel@tonic-gate 				    "misconfigured.  Defaulting to \"all\".\n");
52857c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
52867c478bd9Sstevel@tonic-gate 
52877c478bd9Sstevel@tonic-gate 			case ECANCELED:
52887c478bd9Sstevel@tonic-gate 			case ENOENT:
52897c478bd9Sstevel@tonic-gate 				fmri[0] = '\0';
52907c478bd9Sstevel@tonic-gate 				break;
52917c478bd9Sstevel@tonic-gate 
52927c478bd9Sstevel@tonic-gate 			default:
52937c478bd9Sstevel@tonic-gate 				bad_error("libscf_get_milestone", r);
52947c478bd9Sstevel@tonic-gate 			}
52957c478bd9Sstevel@tonic-gate 
52967c478bd9Sstevel@tonic-gate 			scf_value_destroy(val);
52977c478bd9Sstevel@tonic-gate 			scf_property_destroy(prop);
52987c478bd9Sstevel@tonic-gate 		}
52997c478bd9Sstevel@tonic-gate 	}
53007c478bd9Sstevel@tonic-gate 
53017c478bd9Sstevel@tonic-gate 	if (fmri[0] == '\0' || strcmp(fmri, "all") == 0)
53027c478bd9Sstevel@tonic-gate 		goto out;
53037c478bd9Sstevel@tonic-gate 
53047c478bd9Sstevel@tonic-gate 	if (strcmp(fmri, "none") != 0) {
53057c478bd9Sstevel@tonic-gate retry:
53067c478bd9Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
53077c478bd9Sstevel@tonic-gate 		    NULL, SCF_DECODE_FMRI_EXACT) != 0) {
53087c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
53097c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
53107c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING,
53117c478bd9Sstevel@tonic-gate 				    "Requested milestone \"%s\" is invalid.  "
53127c478bd9Sstevel@tonic-gate 				    "Reverting to \"all\".\n", fmri);
53137c478bd9Sstevel@tonic-gate 				goto out;
53147c478bd9Sstevel@tonic-gate 
53157c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
53167c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Requested milestone "
53177c478bd9Sstevel@tonic-gate 				    "\"%s\" does not specify an instance.  "
53187c478bd9Sstevel@tonic-gate 				    "Reverting to \"all\".\n", fmri);
53197c478bd9Sstevel@tonic-gate 				goto out;
53207c478bd9Sstevel@tonic-gate 
53217c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
53227c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
53237c478bd9Sstevel@tonic-gate 				goto retry;
53247c478bd9Sstevel@tonic-gate 
53257c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
53267c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Requested milestone "
53277c478bd9Sstevel@tonic-gate 				    "\"%s\" not in repository.  Reverting to "
53287c478bd9Sstevel@tonic-gate 				    "\"all\".\n", fmri);
53297c478bd9Sstevel@tonic-gate 				goto out;
53307c478bd9Sstevel@tonic-gate 
53317c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
53327c478bd9Sstevel@tonic-gate 			default:
53337c478bd9Sstevel@tonic-gate 				bad_error("scf_handle_decode_fmri",
53347c478bd9Sstevel@tonic-gate 				    scf_error());
53357c478bd9Sstevel@tonic-gate 			}
53367c478bd9Sstevel@tonic-gate 		}
53377c478bd9Sstevel@tonic-gate 
53387c478bd9Sstevel@tonic-gate 		r = fmri_canonify(fmri, &cfmri, B_FALSE);
53397c478bd9Sstevel@tonic-gate 		assert(r == 0);
53407c478bd9Sstevel@tonic-gate 
53417c478bd9Sstevel@tonic-gate 		r = dgraph_add_instance(cfmri, inst, B_TRUE);
53427c478bd9Sstevel@tonic-gate 		startd_free(cfmri, max_scf_fmri_size);
53437c478bd9Sstevel@tonic-gate 		switch (r) {
53447c478bd9Sstevel@tonic-gate 		case 0:
53457c478bd9Sstevel@tonic-gate 			break;
53467c478bd9Sstevel@tonic-gate 
53477c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
53487c478bd9Sstevel@tonic-gate 			goto retry;
53497c478bd9Sstevel@tonic-gate 
53507c478bd9Sstevel@tonic-gate 		case EINVAL:
53517c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
53527c478bd9Sstevel@tonic-gate 			    "Requested milestone \"%s\" is invalid.  "
53537c478bd9Sstevel@tonic-gate 			    "Reverting to \"all\".\n", fmri);
53547c478bd9Sstevel@tonic-gate 			goto out;
53557c478bd9Sstevel@tonic-gate 
53567c478bd9Sstevel@tonic-gate 		case ECANCELED:
53577c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
53587c478bd9Sstevel@tonic-gate 			    "Requested milestone \"%s\" not "
53597c478bd9Sstevel@tonic-gate 			    "in repository.  Reverting to \"all\".\n",
53607c478bd9Sstevel@tonic-gate 			    fmri);
53617c478bd9Sstevel@tonic-gate 			goto out;
53627c478bd9Sstevel@tonic-gate 
53637c478bd9Sstevel@tonic-gate 		case EEXIST:
53647c478bd9Sstevel@tonic-gate 		default:
53657c478bd9Sstevel@tonic-gate 			bad_error("dgraph_add_instance", r);
53667c478bd9Sstevel@tonic-gate 		}
53677c478bd9Sstevel@tonic-gate 	}
53687c478bd9Sstevel@tonic-gate 
53697c478bd9Sstevel@tonic-gate 	log_console(LOG_INFO, "Booting to milestone \"%s\".\n", fmri);
53707c478bd9Sstevel@tonic-gate 
53717c478bd9Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_FALSE);
53727c478bd9Sstevel@tonic-gate 	switch (r) {
53737c478bd9Sstevel@tonic-gate 	case 0:
53747c478bd9Sstevel@tonic-gate 	case ECONNRESET:
53757c478bd9Sstevel@tonic-gate 	case EALREADY:
53767c478bd9Sstevel@tonic-gate 		break;
53777c478bd9Sstevel@tonic-gate 
53787c478bd9Sstevel@tonic-gate 	case EINVAL:
53797c478bd9Sstevel@tonic-gate 	case ENOENT:
53807c478bd9Sstevel@tonic-gate 	default:
53817c478bd9Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
53827c478bd9Sstevel@tonic-gate 	}
53837c478bd9Sstevel@tonic-gate 
53847c478bd9Sstevel@tonic-gate out:
53857c478bd9Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
53867c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
53877c478bd9Sstevel@tonic-gate }
53887c478bd9Sstevel@tonic-gate 
53897c478bd9Sstevel@tonic-gate void
53907c478bd9Sstevel@tonic-gate set_restart_milestone(scf_handle_t *h)
53917c478bd9Sstevel@tonic-gate {
53927c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
53937c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
53947c478bd9Sstevel@tonic-gate 	scf_value_t *val;
53957c478bd9Sstevel@tonic-gate 	char *fmri;
53967c478bd9Sstevel@tonic-gate 	int r;
53977c478bd9Sstevel@tonic-gate 
53987c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
53997c478bd9Sstevel@tonic-gate 
54007c478bd9Sstevel@tonic-gate get_self:
54017c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL,
54027c478bd9Sstevel@tonic-gate 	    inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
54037c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
54047c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
54057c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
54067c478bd9Sstevel@tonic-gate 			goto get_self;
54077c478bd9Sstevel@tonic-gate 
54087c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
54097c478bd9Sstevel@tonic-gate 			break;
54107c478bd9Sstevel@tonic-gate 
54117c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
54127c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
54137c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
54147c478bd9Sstevel@tonic-gate 		default:
54157c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
54167c478bd9Sstevel@tonic-gate 		}
54177c478bd9Sstevel@tonic-gate 
54187c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
54197c478bd9Sstevel@tonic-gate 		return;
54207c478bd9Sstevel@tonic-gate 	}
54217c478bd9Sstevel@tonic-gate 
54227c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
54237c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
54247c478bd9Sstevel@tonic-gate 	fmri = startd_alloc(max_scf_fmri_size);
54257c478bd9Sstevel@tonic-gate 
54267c478bd9Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size);
54277c478bd9Sstevel@tonic-gate 	switch (r) {
54287c478bd9Sstevel@tonic-gate 	case 0:
54297c478bd9Sstevel@tonic-gate 		break;
54307c478bd9Sstevel@tonic-gate 
54317c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
54327c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
54337c478bd9Sstevel@tonic-gate 		goto get_self;
54347c478bd9Sstevel@tonic-gate 
54357c478bd9Sstevel@tonic-gate 	case ECANCELED:
54367c478bd9Sstevel@tonic-gate 	case ENOENT:
54377c478bd9Sstevel@tonic-gate 	case EINVAL:
54387c478bd9Sstevel@tonic-gate 		goto out;
54397c478bd9Sstevel@tonic-gate 
54407c478bd9Sstevel@tonic-gate 	default:
54417c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
54427c478bd9Sstevel@tonic-gate 	}
54437c478bd9Sstevel@tonic-gate 
54447c478bd9Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_TRUE);
54457c478bd9Sstevel@tonic-gate 	switch (r) {
54467c478bd9Sstevel@tonic-gate 	case 0:
54477c478bd9Sstevel@tonic-gate 	case ECONNRESET:
54487c478bd9Sstevel@tonic-gate 	case EALREADY:
54497c478bd9Sstevel@tonic-gate 	case EINVAL:
54507c478bd9Sstevel@tonic-gate 	case ENOENT:
54517c478bd9Sstevel@tonic-gate 		break;
54527c478bd9Sstevel@tonic-gate 
54537c478bd9Sstevel@tonic-gate 	default:
54547c478bd9Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
54557c478bd9Sstevel@tonic-gate 	}
54567c478bd9Sstevel@tonic-gate 
54577c478bd9Sstevel@tonic-gate out:
54587c478bd9Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
54597c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
54607c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
54617c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
54627c478bd9Sstevel@tonic-gate }
54637c478bd9Sstevel@tonic-gate 
54647c478bd9Sstevel@tonic-gate /*
54657c478bd9Sstevel@tonic-gate  * void *graph_thread(void *)
54667c478bd9Sstevel@tonic-gate  *
54677c478bd9Sstevel@tonic-gate  * Graph management thread.
54687c478bd9Sstevel@tonic-gate  */
54697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
54707c478bd9Sstevel@tonic-gate void *
54717c478bd9Sstevel@tonic-gate graph_thread(void *arg)
54727c478bd9Sstevel@tonic-gate {
54737c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
54747c478bd9Sstevel@tonic-gate 	int err;
54757c478bd9Sstevel@tonic-gate 
54767c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
54777c478bd9Sstevel@tonic-gate 
54787c478bd9Sstevel@tonic-gate 	if (st->st_initial)
54797c478bd9Sstevel@tonic-gate 		set_initial_milestone(h);
54807c478bd9Sstevel@tonic-gate 
54817c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
54827c478bd9Sstevel@tonic-gate 	initial_milestone_set = B_TRUE;
54837c478bd9Sstevel@tonic-gate 	err = pthread_cond_broadcast(&initial_milestone_cv);
54847c478bd9Sstevel@tonic-gate 	assert(err == 0);
54857c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
54867c478bd9Sstevel@tonic-gate 
54877c478bd9Sstevel@tonic-gate 	libscf_populate_graph(h);
54887c478bd9Sstevel@tonic-gate 
54897c478bd9Sstevel@tonic-gate 	if (!st->st_initial)
54907c478bd9Sstevel@tonic-gate 		set_restart_milestone(h);
54917c478bd9Sstevel@tonic-gate 
54927c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&st->st_load_lock);
54937c478bd9Sstevel@tonic-gate 	st->st_load_complete = 1;
54947c478bd9Sstevel@tonic-gate 	(void) pthread_cond_broadcast(&st->st_load_cv);
54957c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&st->st_load_lock);
54967c478bd9Sstevel@tonic-gate 
54977c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
54987c478bd9Sstevel@tonic-gate 	/*
54997c478bd9Sstevel@tonic-gate 	 * Now that we've set st_load_complete we need to check can_come_up()
55007c478bd9Sstevel@tonic-gate 	 * since if we booted to a milestone, then there won't be any more
55017c478bd9Sstevel@tonic-gate 	 * state updates.
55027c478bd9Sstevel@tonic-gate 	 */
55037c478bd9Sstevel@tonic-gate 	if (!go_single_user_mode && !go_to_level1 &&
55047c478bd9Sstevel@tonic-gate 	    halting == -1) {
550573b709eaSrm88369 		if (!sulogin_thread_running && !can_come_up()) {
55067c478bd9Sstevel@tonic-gate 			(void) startd_thread_create(sulogin_thread, NULL);
55077c478bd9Sstevel@tonic-gate 			sulogin_thread_running = B_TRUE;
55087c478bd9Sstevel@tonic-gate 		}
55097c478bd9Sstevel@tonic-gate 	}
55107c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
55117c478bd9Sstevel@tonic-gate 
55127c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&gu->gu_freeze_lock);
55137c478bd9Sstevel@tonic-gate 
55147c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
55157c478bd9Sstevel@tonic-gate 	while (1) {
55167c478bd9Sstevel@tonic-gate 		(void) pthread_cond_wait(&gu->gu_freeze_cv,
55177c478bd9Sstevel@tonic-gate 		    &gu->gu_freeze_lock);
55187c478bd9Sstevel@tonic-gate 	}
55197c478bd9Sstevel@tonic-gate 
55207c478bd9Sstevel@tonic-gate 	/*
55217c478bd9Sstevel@tonic-gate 	 * Unreachable for now -- there's currently no graceful cleanup
55227c478bd9Sstevel@tonic-gate 	 * called on exit().
55237c478bd9Sstevel@tonic-gate 	 */
55247c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&gu->gu_freeze_lock);
55257c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
55267c478bd9Sstevel@tonic-gate 
55277c478bd9Sstevel@tonic-gate 	return (NULL);
55287c478bd9Sstevel@tonic-gate }
55297c478bd9Sstevel@tonic-gate 
55307c478bd9Sstevel@tonic-gate 
55317c478bd9Sstevel@tonic-gate /*
55327c478bd9Sstevel@tonic-gate  * int next_action()
55337c478bd9Sstevel@tonic-gate  *   Given an array of timestamps 'a' with 'num' elements, find the
55347c478bd9Sstevel@tonic-gate  *   lowest non-zero timestamp and return its index. If there are no
55357c478bd9Sstevel@tonic-gate  *   non-zero elements, return -1.
55367c478bd9Sstevel@tonic-gate  */
55377c478bd9Sstevel@tonic-gate static int
55387c478bd9Sstevel@tonic-gate next_action(hrtime_t *a, int num)
55397c478bd9Sstevel@tonic-gate {
55407c478bd9Sstevel@tonic-gate 	hrtime_t t = 0;
55417c478bd9Sstevel@tonic-gate 	int i = 0, smallest = -1;
55427c478bd9Sstevel@tonic-gate 
55437c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
55447c478bd9Sstevel@tonic-gate 		if (t == 0) {
55457c478bd9Sstevel@tonic-gate 			t = a[i];
55467c478bd9Sstevel@tonic-gate 			smallest = i;
55477c478bd9Sstevel@tonic-gate 		} else if (a[i] != 0 && a[i] < t) {
55487c478bd9Sstevel@tonic-gate 			t = a[i];
55497c478bd9Sstevel@tonic-gate 			smallest = i;
55507c478bd9Sstevel@tonic-gate 		}
55517c478bd9Sstevel@tonic-gate 	}
55527c478bd9Sstevel@tonic-gate 
55537c478bd9Sstevel@tonic-gate 	if (t == 0)
55547c478bd9Sstevel@tonic-gate 		return (-1);
55557c478bd9Sstevel@tonic-gate 	else
55567c478bd9Sstevel@tonic-gate 		return (smallest);
55577c478bd9Sstevel@tonic-gate }
55587c478bd9Sstevel@tonic-gate 
55597c478bd9Sstevel@tonic-gate /*
55607c478bd9Sstevel@tonic-gate  * void process_actions()
55617c478bd9Sstevel@tonic-gate  *   Process actions requested by the administrator. Possibilities include:
55627c478bd9Sstevel@tonic-gate  *   refresh, restart, maintenance mode off, maintenance mode on,
55637c478bd9Sstevel@tonic-gate  *   maintenance mode immediate, and degraded.
55647c478bd9Sstevel@tonic-gate  *
55657c478bd9Sstevel@tonic-gate  *   The set of pending actions is represented in the repository as a
55667c478bd9Sstevel@tonic-gate  *   per-instance property group, with each action being a single property
55677c478bd9Sstevel@tonic-gate  *   in that group.  This property group is converted to an array, with each
55687c478bd9Sstevel@tonic-gate  *   action type having an array slot.  The actions in the array at the
55697c478bd9Sstevel@tonic-gate  *   time process_actions() is called are acted on in the order of the
55707c478bd9Sstevel@tonic-gate  *   timestamp (which is the value stored in the slot).  A value of zero
55717c478bd9Sstevel@tonic-gate  *   indicates that there is no pending action of the type associated with
55727c478bd9Sstevel@tonic-gate  *   a particular slot.
55737c478bd9Sstevel@tonic-gate  *
55747c478bd9Sstevel@tonic-gate  *   Sending an action event multiple times before the restarter has a
55757c478bd9Sstevel@tonic-gate  *   chance to process that action will force it to be run at the last
55767c478bd9Sstevel@tonic-gate  *   timestamp where it appears in the ordering.
55777c478bd9Sstevel@tonic-gate  *
55787c478bd9Sstevel@tonic-gate  *   Turning maintenance mode on trumps all other actions.
55797c478bd9Sstevel@tonic-gate  *
55807c478bd9Sstevel@tonic-gate  *   Returns 0 or ECONNABORTED.
55817c478bd9Sstevel@tonic-gate  */
55827c478bd9Sstevel@tonic-gate static int
55837c478bd9Sstevel@tonic-gate process_actions(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst)
55847c478bd9Sstevel@tonic-gate {
55857c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
55867c478bd9Sstevel@tonic-gate 	scf_value_t *val = NULL;
55877c478bd9Sstevel@tonic-gate 	scf_type_t type;
55887c478bd9Sstevel@tonic-gate 	graph_vertex_t *vertex;
55897c478bd9Sstevel@tonic-gate 	admin_action_t a;
55907c478bd9Sstevel@tonic-gate 	int i, ret = 0, r;
55917c478bd9Sstevel@tonic-gate 	hrtime_t action_ts[NACTIONS];
55927c478bd9Sstevel@tonic-gate 	char *inst_name;
55937c478bd9Sstevel@tonic-gate 
55947c478bd9Sstevel@tonic-gate 	r = libscf_instance_get_fmri(inst, &inst_name);
55957c478bd9Sstevel@tonic-gate 	switch (r) {
55967c478bd9Sstevel@tonic-gate 	case 0:
55977c478bd9Sstevel@tonic-gate 		break;
55987c478bd9Sstevel@tonic-gate 
55997c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
56007c478bd9Sstevel@tonic-gate 		return (ECONNABORTED);
56017c478bd9Sstevel@tonic-gate 
56027c478bd9Sstevel@tonic-gate 	case ECANCELED:
56037c478bd9Sstevel@tonic-gate 		return (0);
56047c478bd9Sstevel@tonic-gate 
56057c478bd9Sstevel@tonic-gate 	default:
56067c478bd9Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", r);
56077c478bd9Sstevel@tonic-gate 	}
56087c478bd9Sstevel@tonic-gate 
56097c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
56107c478bd9Sstevel@tonic-gate 
56117c478bd9Sstevel@tonic-gate 	vertex = vertex_get_by_name(inst_name);
56127c478bd9Sstevel@tonic-gate 	if (vertex == NULL) {
56137c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
56145b6c5443Srm88369 		startd_free(inst_name, max_scf_fmri_size);
56157c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "%s: Can't find graph vertex. "
56167c478bd9Sstevel@tonic-gate 		    "The instance must have been removed.\n", inst_name);
56177c478bd9Sstevel@tonic-gate 		return (0);
56187c478bd9Sstevel@tonic-gate 	}
56197c478bd9Sstevel@tonic-gate 
56207c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
56217c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
56227c478bd9Sstevel@tonic-gate 
56237c478bd9Sstevel@tonic-gate 	for (i = 0; i < NACTIONS; i++) {
56247c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(pg, admin_actions[i], prop) != 0) {
56257c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
56267c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
56277c478bd9Sstevel@tonic-gate 			default:
56287c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
56297c478bd9Sstevel@tonic-gate 				goto out;
56307c478bd9Sstevel@tonic-gate 
56317c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
56327c478bd9Sstevel@tonic-gate 				goto out;
56337c478bd9Sstevel@tonic-gate 
56347c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
56357c478bd9Sstevel@tonic-gate 				action_ts[i] = 0;
56367c478bd9Sstevel@tonic-gate 				continue;
56377c478bd9Sstevel@tonic-gate 
56387c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
56397c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
56407c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
56417c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
56427c478bd9Sstevel@tonic-gate 			}
56437c478bd9Sstevel@tonic-gate 		}
56447c478bd9Sstevel@tonic-gate 
56457c478bd9Sstevel@tonic-gate 		if (scf_property_type(prop, &type) != 0) {
56467c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
56477c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
56487c478bd9Sstevel@tonic-gate 			default:
56497c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
56507c478bd9Sstevel@tonic-gate 				goto out;
56517c478bd9Sstevel@tonic-gate 
56527c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
56537c478bd9Sstevel@tonic-gate 				action_ts[i] = 0;
56547c478bd9Sstevel@tonic-gate 				continue;
56557c478bd9Sstevel@tonic-gate 
56567c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
56577c478bd9Sstevel@tonic-gate 				bad_error("scf_property_type", scf_error());
56587c478bd9Sstevel@tonic-gate 			}
56597c478bd9Sstevel@tonic-gate 		}
56607c478bd9Sstevel@tonic-gate 
56617c478bd9Sstevel@tonic-gate 		if (type != SCF_TYPE_INTEGER) {
56627c478bd9Sstevel@tonic-gate 			action_ts[i] = 0;
56637c478bd9Sstevel@tonic-gate 			continue;
56647c478bd9Sstevel@tonic-gate 		}
56657c478bd9Sstevel@tonic-gate 
56667c478bd9Sstevel@tonic-gate 		if (scf_property_get_value(prop, val) != 0) {
56677c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
56687c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
56697c478bd9Sstevel@tonic-gate 			default:
56707c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
56717c478bd9Sstevel@tonic-gate 				goto out;
56727c478bd9Sstevel@tonic-gate 
56737c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
56747c478bd9Sstevel@tonic-gate 				goto out;
56757c478bd9Sstevel@tonic-gate 
56767c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
56777c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
56787c478bd9Sstevel@tonic-gate 				action_ts[i] = 0;
56797c478bd9Sstevel@tonic-gate 				continue;
56807c478bd9Sstevel@tonic-gate 
56817c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
56823eae19d9Swesolows 			case SCF_ERROR_PERMISSION_DENIED:
56837c478bd9Sstevel@tonic-gate 				bad_error("scf_property_get_value",
56847c478bd9Sstevel@tonic-gate 				    scf_error());
56857c478bd9Sstevel@tonic-gate 			}
56867c478bd9Sstevel@tonic-gate 		}
56877c478bd9Sstevel@tonic-gate 
56887c478bd9Sstevel@tonic-gate 		r = scf_value_get_integer(val, &action_ts[i]);
56897c478bd9Sstevel@tonic-gate 		assert(r == 0);
56907c478bd9Sstevel@tonic-gate 	}
56917c478bd9Sstevel@tonic-gate 
56927c478bd9Sstevel@tonic-gate 	a = ADMIN_EVENT_MAINT_ON_IMMEDIATE;
56937c478bd9Sstevel@tonic-gate 	if (action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ||
56947c478bd9Sstevel@tonic-gate 	    action_ts[ADMIN_EVENT_MAINT_ON]) {
56957c478bd9Sstevel@tonic-gate 		a = action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ?
56967c478bd9Sstevel@tonic-gate 		    ADMIN_EVENT_MAINT_ON_IMMEDIATE : ADMIN_EVENT_MAINT_ON;
56977c478bd9Sstevel@tonic-gate 
56987c478bd9Sstevel@tonic-gate 		vertex_send_event(vertex, admin_events[a]);
56997c478bd9Sstevel@tonic-gate 		r = libscf_unset_action(h, pg, a, action_ts[a]);
57007c478bd9Sstevel@tonic-gate 		switch (r) {
57017c478bd9Sstevel@tonic-gate 		case 0:
57027c478bd9Sstevel@tonic-gate 		case EACCES:
57037c478bd9Sstevel@tonic-gate 			break;
57047c478bd9Sstevel@tonic-gate 
57057c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
57067c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
57077c478bd9Sstevel@tonic-gate 			goto out;
57087c478bd9Sstevel@tonic-gate 
57097c478bd9Sstevel@tonic-gate 		case EPERM:
57107c478bd9Sstevel@tonic-gate 			uu_die("Insufficient privilege.\n");
57117c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
57127c478bd9Sstevel@tonic-gate 
57137c478bd9Sstevel@tonic-gate 		default:
57147c478bd9Sstevel@tonic-gate 			bad_error("libscf_unset_action", r);
57157c478bd9Sstevel@tonic-gate 		}
57167c478bd9Sstevel@tonic-gate 	}
57177c478bd9Sstevel@tonic-gate 
57187c478bd9Sstevel@tonic-gate 	while ((a = next_action(action_ts, NACTIONS)) != -1) {
57197c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
57207c478bd9Sstevel@tonic-gate 		    "Graph: processing %s action for %s.\n", admin_actions[a],
57217c478bd9Sstevel@tonic-gate 		    inst_name);
57227c478bd9Sstevel@tonic-gate 
57237c478bd9Sstevel@tonic-gate 		if (a == ADMIN_EVENT_REFRESH) {
57247c478bd9Sstevel@tonic-gate 			r = dgraph_refresh_instance(vertex, inst);
57257c478bd9Sstevel@tonic-gate 			switch (r) {
57267c478bd9Sstevel@tonic-gate 			case 0:
57277c478bd9Sstevel@tonic-gate 			case ECANCELED:
57287c478bd9Sstevel@tonic-gate 			case EINVAL:
57297c478bd9Sstevel@tonic-gate 			case -1:
57307c478bd9Sstevel@tonic-gate 				break;
57317c478bd9Sstevel@tonic-gate 
57327c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
57337c478bd9Sstevel@tonic-gate 				/* pg & inst are reset now, so just return. */
57347c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
57357c478bd9Sstevel@tonic-gate 				goto out;
57367c478bd9Sstevel@tonic-gate 
57377c478bd9Sstevel@tonic-gate 			default:
57387c478bd9Sstevel@tonic-gate 				bad_error("dgraph_refresh_instance", r);
57397c478bd9Sstevel@tonic-gate 			}
57407c478bd9Sstevel@tonic-gate 		}
57417c478bd9Sstevel@tonic-gate 
57427c478bd9Sstevel@tonic-gate 		vertex_send_event(vertex, admin_events[a]);
57437c478bd9Sstevel@tonic-gate 
57447c478bd9Sstevel@tonic-gate 		r = libscf_unset_action(h, pg, a, action_ts[a]);
57457c478bd9Sstevel@tonic-gate 		switch (r) {
57467c478bd9Sstevel@tonic-gate 		case 0:
57477c478bd9Sstevel@tonic-gate 		case EACCES:
57487c478bd9Sstevel@tonic-gate 			break;
57497c478bd9Sstevel@tonic-gate 
57507c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
57517c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
57527c478bd9Sstevel@tonic-gate 			goto out;
57537c478bd9Sstevel@tonic-gate 
57547c478bd9Sstevel@tonic-gate 		case EPERM:
57557c478bd9Sstevel@tonic-gate 			uu_die("Insufficient privilege.\n");
57567c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
57577c478bd9Sstevel@tonic-gate 
57587c478bd9Sstevel@tonic-gate 		default:
57597c478bd9Sstevel@tonic-gate 			bad_error("libscf_unset_action", r);
57607c478bd9Sstevel@tonic-gate 		}
57617c478bd9Sstevel@tonic-gate 
57627c478bd9Sstevel@tonic-gate 		action_ts[a] = 0;
57637c478bd9Sstevel@tonic-gate 	}
57647c478bd9Sstevel@tonic-gate 
57657c478bd9Sstevel@tonic-gate out:
57667c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
57677c478bd9Sstevel@tonic-gate 
57687c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
57697c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
57707c478bd9Sstevel@tonic-gate 	startd_free(inst_name, max_scf_fmri_size);
57717c478bd9Sstevel@tonic-gate 	return (ret);
57727c478bd9Sstevel@tonic-gate }
57737c478bd9Sstevel@tonic-gate 
57747c478bd9Sstevel@tonic-gate /*
57757c478bd9Sstevel@tonic-gate  * inst and pg_name are scratch space, and are unset on entry.
57767c478bd9Sstevel@tonic-gate  * Returns
57777c478bd9Sstevel@tonic-gate  *   0 - success
57787c478bd9Sstevel@tonic-gate  *   ECONNRESET - success, but repository handle rebound
57797c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
57807c478bd9Sstevel@tonic-gate  */
57817c478bd9Sstevel@tonic-gate static int
57827c478bd9Sstevel@tonic-gate process_pg_event(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst,
57837c478bd9Sstevel@tonic-gate     char *pg_name)
57847c478bd9Sstevel@tonic-gate {
57857c478bd9Sstevel@tonic-gate 	int r;
57867c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
57877c478bd9Sstevel@tonic-gate 	scf_value_t *val;
57887c478bd9Sstevel@tonic-gate 	char *fmri;
57897c478bd9Sstevel@tonic-gate 	boolean_t rebound = B_FALSE, rebind_inst = B_FALSE;
57907c478bd9Sstevel@tonic-gate 
57917c478bd9Sstevel@tonic-gate 	if (scf_pg_get_name(pg, pg_name, max_scf_value_size) < 0) {
57927c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
57937c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
57947c478bd9Sstevel@tonic-gate 		default:
57957c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
57967c478bd9Sstevel@tonic-gate 
57977c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
57987c478bd9Sstevel@tonic-gate 			return (0);
57997c478bd9Sstevel@tonic-gate 
58007c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
58017c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_name", scf_error());
58027c478bd9Sstevel@tonic-gate 		}
58037c478bd9Sstevel@tonic-gate 	}
58047c478bd9Sstevel@tonic-gate 
58057c478bd9Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_GENERAL) == 0 ||
58067c478bd9Sstevel@tonic-gate 	    strcmp(pg_name, SCF_PG_GENERAL_OVR) == 0) {
58077c478bd9Sstevel@tonic-gate 		r = dgraph_update_general(pg);
58087c478bd9Sstevel@tonic-gate 		switch (r) {
58097c478bd9Sstevel@tonic-gate 		case 0:
58107c478bd9Sstevel@tonic-gate 		case ENOTSUP:
58117c478bd9Sstevel@tonic-gate 		case ECANCELED:
58127c478bd9Sstevel@tonic-gate 			return (0);
58137c478bd9Sstevel@tonic-gate 
58147c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
58157c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
58167c478bd9Sstevel@tonic-gate 
58177c478bd9Sstevel@tonic-gate 		case -1:
58187c478bd9Sstevel@tonic-gate 			/* Error should have been logged. */
58197c478bd9Sstevel@tonic-gate 			return (0);
58207c478bd9Sstevel@tonic-gate 
58217c478bd9Sstevel@tonic-gate 		default:
58227c478bd9Sstevel@tonic-gate 			bad_error("dgraph_update_general", r);
58237c478bd9Sstevel@tonic-gate 		}
58247c478bd9Sstevel@tonic-gate 	} else if (strcmp(pg_name, SCF_PG_RESTARTER_ACTIONS) == 0) {
58257c478bd9Sstevel@tonic-gate 		if (scf_pg_get_parent_instance(pg, inst) != 0) {
58267c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
58277c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
58287c478bd9Sstevel@tonic-gate 				return (ECONNABORTED);
58297c478bd9Sstevel@tonic-gate 
58307c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
58317c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
58327c478bd9Sstevel@tonic-gate 				/* Ignore commands on services. */
58337c478bd9Sstevel@tonic-gate 				return (0);
58347c478bd9Sstevel@tonic-gate 
58357c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
58367c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
58377c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
58387c478bd9Sstevel@tonic-gate 			default:
58397c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_get_parent_instance",
58407c478bd9Sstevel@tonic-gate 				    scf_error());
58417c478bd9Sstevel@tonic-gate 			}
58427c478bd9Sstevel@tonic-gate 		}
58437c478bd9Sstevel@tonic-gate 
58447c478bd9Sstevel@tonic-gate 		return (process_actions(h, pg, inst));
58457c478bd9Sstevel@tonic-gate 	}
58467c478bd9Sstevel@tonic-gate 
58477c478bd9Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_OPTIONS) != 0 &&
58487c478bd9Sstevel@tonic-gate 	    strcmp(pg_name, SCF_PG_OPTIONS_OVR) != 0)
58497c478bd9Sstevel@tonic-gate 		return (0);
58507c478bd9Sstevel@tonic-gate 
58517c478bd9Sstevel@tonic-gate 	/*
58527c478bd9Sstevel@tonic-gate 	 * We only care about the options[_ovr] property groups of our own
58537c478bd9Sstevel@tonic-gate 	 * instance, so get the fmri and compare.  Plus, once we know it's
58547c478bd9Sstevel@tonic-gate 	 * correct, if the repository connection is broken we know exactly what
58557c478bd9Sstevel@tonic-gate 	 * property group we were operating on, and can look it up again.
58567c478bd9Sstevel@tonic-gate 	 */
58577c478bd9Sstevel@tonic-gate 	if (scf_pg_get_parent_instance(pg, inst) != 0) {
58587c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
58597c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
58607c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
58617c478bd9Sstevel@tonic-gate 
58627c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
58637c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
58647c478bd9Sstevel@tonic-gate 			return (0);
58657c478bd9Sstevel@tonic-gate 
58667c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
58677c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
58687c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
58697c478bd9Sstevel@tonic-gate 		default:
58707c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_parent_instance",
58717c478bd9Sstevel@tonic-gate 			    scf_error());
58727c478bd9Sstevel@tonic-gate 		}
58737c478bd9Sstevel@tonic-gate 	}
58747c478bd9Sstevel@tonic-gate 
58757c478bd9Sstevel@tonic-gate 	switch (r = libscf_instance_get_fmri(inst, &fmri)) {
58767c478bd9Sstevel@tonic-gate 	case 0:
58777c478bd9Sstevel@tonic-gate 		break;
58787c478bd9Sstevel@tonic-gate 
58797c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
58807c478bd9Sstevel@tonic-gate 		return (ECONNABORTED);
58817c478bd9Sstevel@tonic-gate 
58827c478bd9Sstevel@tonic-gate 	case ECANCELED:
58837c478bd9Sstevel@tonic-gate 		return (0);
58847c478bd9Sstevel@tonic-gate 
58857c478bd9Sstevel@tonic-gate 	default:
58867c478bd9Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", r);
58877c478bd9Sstevel@tonic-gate 	}
58887c478bd9Sstevel@tonic-gate 
58897c478bd9Sstevel@tonic-gate 	if (strcmp(fmri, SCF_SERVICE_STARTD) != 0) {
58907c478bd9Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
58917c478bd9Sstevel@tonic-gate 		return (0);
58927c478bd9Sstevel@tonic-gate 	}
58937c478bd9Sstevel@tonic-gate 
58947c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
58957c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
58967c478bd9Sstevel@tonic-gate 
58977c478bd9Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_OPTIONS_OVR) == 0) {
58987c478bd9Sstevel@tonic-gate 		/* See if we need to set the runlevel. */
58997c478bd9Sstevel@tonic-gate 		/* CONSTCOND */
59007c478bd9Sstevel@tonic-gate 		if (0) {
59017c478bd9Sstevel@tonic-gate rebind_pg:
59027c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
59037c478bd9Sstevel@tonic-gate 			rebound = B_TRUE;
59047c478bd9Sstevel@tonic-gate 
59057c478bd9Sstevel@tonic-gate 			r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst);
59067c478bd9Sstevel@tonic-gate 			switch (r) {
59077c478bd9Sstevel@tonic-gate 			case 0:
59087c478bd9Sstevel@tonic-gate 				break;
59097c478bd9Sstevel@tonic-gate 
59107c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
59117c478bd9Sstevel@tonic-gate 				goto rebind_pg;
59127c478bd9Sstevel@tonic-gate 
59137c478bd9Sstevel@tonic-gate 			case ENOENT:
59147c478bd9Sstevel@tonic-gate 				goto out;
59157c478bd9Sstevel@tonic-gate 
59167c478bd9Sstevel@tonic-gate 			case EINVAL:
59177c478bd9Sstevel@tonic-gate 			case ENOTSUP:
59187c478bd9Sstevel@tonic-gate 				bad_error("libscf_lookup_instance", r);
59197c478bd9Sstevel@tonic-gate 			}
59207c478bd9Sstevel@tonic-gate 
59217c478bd9Sstevel@tonic-gate 			if (scf_instance_get_pg(inst, pg_name, pg) != 0) {
59227c478bd9Sstevel@tonic-gate 				switch (scf_error()) {
59237c478bd9Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
59247c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_FOUND:
59257c478bd9Sstevel@tonic-gate 					goto out;
59267c478bd9Sstevel@tonic-gate 
59277c478bd9Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
59287c478bd9Sstevel@tonic-gate 					goto rebind_pg;
59297c478bd9Sstevel@tonic-gate 
59307c478bd9Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
59317c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_BOUND:
59327c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
59337c478bd9Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
59347c478bd9Sstevel@tonic-gate 				default:
59357c478bd9Sstevel@tonic-gate 					bad_error("scf_instance_get_pg",
59367c478bd9Sstevel@tonic-gate 					    scf_error());
59377c478bd9Sstevel@tonic-gate 				}
59387c478bd9Sstevel@tonic-gate 			}
59397c478bd9Sstevel@tonic-gate 		}
59407c478bd9Sstevel@tonic-gate 
59417c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(pg, "runlevel", prop) == 0) {
59427c478bd9Sstevel@tonic-gate 			r = dgraph_set_runlevel(pg, prop);
59437c478bd9Sstevel@tonic-gate 			switch (r) {
59447c478bd9Sstevel@tonic-gate 			case ECONNRESET:
59457c478bd9Sstevel@tonic-gate 				rebound = B_TRUE;
59467c478bd9Sstevel@tonic-gate 				rebind_inst = B_TRUE;
59477c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
59487c478bd9Sstevel@tonic-gate 
59497c478bd9Sstevel@tonic-gate 			case 0:
59507c478bd9Sstevel@tonic-gate 				break;
59517c478bd9Sstevel@tonic-gate 
59527c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
59537c478bd9Sstevel@tonic-gate 				goto rebind_pg;
59547c478bd9Sstevel@tonic-gate 
59557c478bd9Sstevel@tonic-gate 			case ECANCELED:
59567c478bd9Sstevel@tonic-gate 				goto out;
59577c478bd9Sstevel@tonic-gate 
59587c478bd9Sstevel@tonic-gate 			default:
59597c478bd9Sstevel@tonic-gate 				bad_error("dgraph_set_runlevel", r);
59607c478bd9Sstevel@tonic-gate 			}
59617c478bd9Sstevel@tonic-gate 		} else {
59627c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
59637c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
59647c478bd9Sstevel@tonic-gate 			default:
59657c478bd9Sstevel@tonic-gate 				goto rebind_pg;
59667c478bd9Sstevel@tonic-gate 
59677c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
59687c478bd9Sstevel@tonic-gate 				goto out;
59697c478bd9Sstevel@tonic-gate 
59707c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
59717c478bd9Sstevel@tonic-gate 				break;
59727c478bd9Sstevel@tonic-gate 
59737c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
59747c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
59757c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
59767c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
59777c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
59787c478bd9Sstevel@tonic-gate 			}
59797c478bd9Sstevel@tonic-gate 		}
59807c478bd9Sstevel@tonic-gate 	}
59817c478bd9Sstevel@tonic-gate 
59827c478bd9Sstevel@tonic-gate 	if (rebind_inst) {
59837c478bd9Sstevel@tonic-gate lookup_inst:
59847c478bd9Sstevel@tonic-gate 		r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst);
59857c478bd9Sstevel@tonic-gate 		switch (r) {
59867c478bd9Sstevel@tonic-gate 		case 0:
59877c478bd9Sstevel@tonic-gate 			break;
59887c478bd9Sstevel@tonic-gate 
59897c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
59907c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
59917c478bd9Sstevel@tonic-gate 			rebound = B_TRUE;
59927c478bd9Sstevel@tonic-gate 			goto lookup_inst;
59937c478bd9Sstevel@tonic-gate 
59947c478bd9Sstevel@tonic-gate 		case ENOENT:
59957c478bd9Sstevel@tonic-gate 			goto out;
59967c478bd9Sstevel@tonic-gate 
59977c478bd9Sstevel@tonic-gate 		case EINVAL:
59987c478bd9Sstevel@tonic-gate 		case ENOTSUP:
59997c478bd9Sstevel@tonic-gate 			bad_error("libscf_lookup_instance", r);
60007c478bd9Sstevel@tonic-gate 		}
60017c478bd9Sstevel@tonic-gate 	}
60027c478bd9Sstevel@tonic-gate 
60037c478bd9Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size);
60047c478bd9Sstevel@tonic-gate 	switch (r) {
60057c478bd9Sstevel@tonic-gate 	case 0:
60067c478bd9Sstevel@tonic-gate 		break;
60077c478bd9Sstevel@tonic-gate 
60087c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
60097c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
60107c478bd9Sstevel@tonic-gate 		rebound = B_TRUE;
60117c478bd9Sstevel@tonic-gate 		goto lookup_inst;
60127c478bd9Sstevel@tonic-gate 
60137c478bd9Sstevel@tonic-gate 	case EINVAL:
60147c478bd9Sstevel@tonic-gate 		log_error(LOG_NOTICE,
60157c478bd9Sstevel@tonic-gate 		    "%s/%s property of %s is misconfigured.\n", pg_name,
60167c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MILESTONE, SCF_SERVICE_STARTD);
60177c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
60187c478bd9Sstevel@tonic-gate 
60197c478bd9Sstevel@tonic-gate 	case ECANCELED:
60207c478bd9Sstevel@tonic-gate 	case ENOENT:
60217c478bd9Sstevel@tonic-gate 		(void) strcpy(fmri, "all");
60227c478bd9Sstevel@tonic-gate 		break;
60237c478bd9Sstevel@tonic-gate 
60247c478bd9Sstevel@tonic-gate 	default:
60257c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
60267c478bd9Sstevel@tonic-gate 	}
60277c478bd9Sstevel@tonic-gate 
60287c478bd9Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_FALSE);
60297c478bd9Sstevel@tonic-gate 	switch (r) {
60307c478bd9Sstevel@tonic-gate 	case 0:
60317c478bd9Sstevel@tonic-gate 	case ECONNRESET:
60327c478bd9Sstevel@tonic-gate 	case EALREADY:
60337c478bd9Sstevel@tonic-gate 		break;
60347c478bd9Sstevel@tonic-gate 
60357c478bd9Sstevel@tonic-gate 	case EINVAL:
60367c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "Milestone %s is invalid.\n", fmri);
60377c478bd9Sstevel@tonic-gate 		break;
60387c478bd9Sstevel@tonic-gate 
60397c478bd9Sstevel@tonic-gate 	case ENOENT:
60407c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "Milestone %s does not exist.\n", fmri);
60417c478bd9Sstevel@tonic-gate 		break;
60427c478bd9Sstevel@tonic-gate 
60437c478bd9Sstevel@tonic-gate 	default:
60447c478bd9Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
60457c478bd9Sstevel@tonic-gate 	}
60467c478bd9Sstevel@tonic-gate 
60477c478bd9Sstevel@tonic-gate out:
60487c478bd9Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
60497c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
60507c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
60517c478bd9Sstevel@tonic-gate 
60527c478bd9Sstevel@tonic-gate 	return (rebound ? ECONNRESET : 0);
60537c478bd9Sstevel@tonic-gate }
60547c478bd9Sstevel@tonic-gate 
6055ffbfbf47Srm88369 /*
6056ffbfbf47Srm88369  * process_delete() deletes an instance from the dgraph if 'fmri' is an
6057ffbfbf47Srm88369  * instance fmri or if 'fmri' matches the 'general' property group of an
6058ffbfbf47Srm88369  * instance (or the 'general/enabled' property).
6059ffbfbf47Srm88369  *
6060ffbfbf47Srm88369  * 'fmri' may be overwritten and cannot be trusted on return by the caller.
6061ffbfbf47Srm88369  */
60627c478bd9Sstevel@tonic-gate static void
60637c478bd9Sstevel@tonic-gate process_delete(char *fmri, scf_handle_t *h)
60647c478bd9Sstevel@tonic-gate {
6065ffbfbf47Srm88369 	char *lfmri, *end_inst_fmri;
6066ffbfbf47Srm88369 	const char *inst_name = NULL;
6067ffbfbf47Srm88369 	const char *pg_name = NULL;
6068ffbfbf47Srm88369 	const char *prop_name = NULL;
60697c478bd9Sstevel@tonic-gate 
60707c478bd9Sstevel@tonic-gate 	lfmri = safe_strdup(fmri);
60717c478bd9Sstevel@tonic-gate 
60727c478bd9Sstevel@tonic-gate 	/* Determine if the FMRI is a property group or instance */
60737c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(lfmri, NULL, NULL, &inst_name, &pg_name,
6074ffbfbf47Srm88369 	    &prop_name) != SCF_SUCCESS) {
60757c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
60767c478bd9Sstevel@tonic-gate 		    "Received invalid FMRI \"%s\" from repository server.\n",
60777c478bd9Sstevel@tonic-gate 		    fmri);
60787c478bd9Sstevel@tonic-gate 	} else if (inst_name != NULL && pg_name == NULL) {
60797c478bd9Sstevel@tonic-gate 		(void) dgraph_remove_instance(fmri, h);
6080ffbfbf47Srm88369 	} else if (inst_name != NULL && pg_name != NULL) {
6081ffbfbf47Srm88369 		/*
6082ffbfbf47Srm88369 		 * If we're deleting the 'general' property group or
6083ffbfbf47Srm88369 		 * 'general/enabled' property then the whole instance
6084ffbfbf47Srm88369 		 * must be removed from the dgraph.
6085ffbfbf47Srm88369 		 */
6086ffbfbf47Srm88369 		if (strcmp(pg_name, SCF_PG_GENERAL) != 0) {
6087ffbfbf47Srm88369 			free(lfmri);
6088ffbfbf47Srm88369 			return;
6089ffbfbf47Srm88369 		}
6090ffbfbf47Srm88369 
6091ffbfbf47Srm88369 		if (prop_name != NULL &&
6092ffbfbf47Srm88369 		    strcmp(prop_name, SCF_PROPERTY_ENABLED) != 0) {
6093ffbfbf47Srm88369 			free(lfmri);
6094ffbfbf47Srm88369 			return;
6095ffbfbf47Srm88369 		}
6096ffbfbf47Srm88369 
6097ffbfbf47Srm88369 		/*
6098ffbfbf47Srm88369 		 * Because the instance has already been deleted from the
6099ffbfbf47Srm88369 		 * repository, we cannot use any scf_ functions to retrieve
6100ffbfbf47Srm88369 		 * the instance FMRI however we can easily reconstruct it
6101ffbfbf47Srm88369 		 * manually.
6102ffbfbf47Srm88369 		 */
6103ffbfbf47Srm88369 		end_inst_fmri = strstr(fmri, SCF_FMRI_PROPERTYGRP_PREFIX);
6104ffbfbf47Srm88369 		if (end_inst_fmri == NULL)
6105ffbfbf47Srm88369 			bad_error("process_delete", 0);
6106ffbfbf47Srm88369 
6107ffbfbf47Srm88369 		end_inst_fmri[0] = '\0';
6108ffbfbf47Srm88369 
6109ffbfbf47Srm88369 		(void) dgraph_remove_instance(fmri, h);
61107c478bd9Sstevel@tonic-gate 	}
61117c478bd9Sstevel@tonic-gate 
61127c478bd9Sstevel@tonic-gate 	free(lfmri);
61137c478bd9Sstevel@tonic-gate }
61147c478bd9Sstevel@tonic-gate 
61157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
61167c478bd9Sstevel@tonic-gate void *
61177c478bd9Sstevel@tonic-gate repository_event_thread(void *unused)
61187c478bd9Sstevel@tonic-gate {
61197c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
61207c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
61217c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
61227c478bd9Sstevel@tonic-gate 	char *fmri = startd_alloc(max_scf_fmri_size);
61237c478bd9Sstevel@tonic-gate 	char *pg_name = startd_alloc(max_scf_value_size);
61247c478bd9Sstevel@tonic-gate 	int r;
61257c478bd9Sstevel@tonic-gate 
61267c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
61277c478bd9Sstevel@tonic-gate 
61287c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
61297c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
61307c478bd9Sstevel@tonic-gate 
61317c478bd9Sstevel@tonic-gate retry:
61327c478bd9Sstevel@tonic-gate 	if (_scf_notify_add_pgtype(h, SCF_GROUP_FRAMEWORK) != SCF_SUCCESS) {
61337c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN) {
61347c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
61357c478bd9Sstevel@tonic-gate 		} else {
61367c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
61377c478bd9Sstevel@tonic-gate 			    "Couldn't set up repository notification "
61387c478bd9Sstevel@tonic-gate 			    "for property group type %s: %s\n",
61397c478bd9Sstevel@tonic-gate 			    SCF_GROUP_FRAMEWORK, scf_strerror(scf_error()));
61407c478bd9Sstevel@tonic-gate 
61417c478bd9Sstevel@tonic-gate 			(void) sleep(1);
61427c478bd9Sstevel@tonic-gate 		}
61437c478bd9Sstevel@tonic-gate 
61447c478bd9Sstevel@tonic-gate 		goto retry;
61457c478bd9Sstevel@tonic-gate 	}
61467c478bd9Sstevel@tonic-gate 
61477c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
61487c478bd9Sstevel@tonic-gate 	while (1) {
61497c478bd9Sstevel@tonic-gate 		ssize_t res;
61507c478bd9Sstevel@tonic-gate 
61517c478bd9Sstevel@tonic-gate 		/* Note: fmri is only set on delete events. */
61527c478bd9Sstevel@tonic-gate 		res = _scf_notify_wait(pg, fmri, max_scf_fmri_size);
61537c478bd9Sstevel@tonic-gate 		if (res < 0) {
61547c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
61557c478bd9Sstevel@tonic-gate 			goto retry;
61567c478bd9Sstevel@tonic-gate 		} else if (res == 0) {
61577c478bd9Sstevel@tonic-gate 			/*
61587c478bd9Sstevel@tonic-gate 			 * property group modified.  inst and pg_name are
61597c478bd9Sstevel@tonic-gate 			 * pre-allocated scratch space.
61607c478bd9Sstevel@tonic-gate 			 */
61617c478bd9Sstevel@tonic-gate 			if (scf_pg_update(pg) < 0) {
61627c478bd9Sstevel@tonic-gate 				switch (scf_error()) {
61637c478bd9Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
61647c478bd9Sstevel@tonic-gate 					continue;
61657c478bd9Sstevel@tonic-gate 
61667c478bd9Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
61677c478bd9Sstevel@tonic-gate 					log_error(LOG_WARNING,
61687c478bd9Sstevel@tonic-gate 					    "Lost repository event due to "
61697c478bd9Sstevel@tonic-gate 					    "disconnection.\n");
61707c478bd9Sstevel@tonic-gate 					libscf_handle_rebind(h);
61717c478bd9Sstevel@tonic-gate 					goto retry;
61727c478bd9Sstevel@tonic-gate 
61737c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_BOUND:
61747c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
61757c478bd9Sstevel@tonic-gate 				default:
61767c478bd9Sstevel@tonic-gate 					bad_error("scf_pg_update", scf_error());
61777c478bd9Sstevel@tonic-gate 				}
61787c478bd9Sstevel@tonic-gate 			}
61797c478bd9Sstevel@tonic-gate 
61807c478bd9Sstevel@tonic-gate 			r = process_pg_event(h, pg, inst, pg_name);
61817c478bd9Sstevel@tonic-gate 			switch (r) {
61827c478bd9Sstevel@tonic-gate 			case 0:
61837c478bd9Sstevel@tonic-gate 				break;
61847c478bd9Sstevel@tonic-gate 
61857c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
61867c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Lost repository event "
61877c478bd9Sstevel@tonic-gate 				    "due to disconnection.\n");
61887c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
61897c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
61907c478bd9Sstevel@tonic-gate 
61917c478bd9Sstevel@tonic-gate 			case ECONNRESET:
61927c478bd9Sstevel@tonic-gate 				goto retry;
61937c478bd9Sstevel@tonic-gate 
61947c478bd9Sstevel@tonic-gate 			default:
61957c478bd9Sstevel@tonic-gate 				bad_error("process_pg_event", r);
61967c478bd9Sstevel@tonic-gate 			}
61977c478bd9Sstevel@tonic-gate 		} else {
6198ffbfbf47Srm88369 			/*
6199ffbfbf47Srm88369 			 * Service, instance, or pg deleted.
6200ffbfbf47Srm88369 			 * Don't trust fmri on return.
6201ffbfbf47Srm88369 			 */
62027c478bd9Sstevel@tonic-gate 			process_delete(fmri, h);
62037c478bd9Sstevel@tonic-gate 		}
62047c478bd9Sstevel@tonic-gate 	}
62057c478bd9Sstevel@tonic-gate 
62067c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
62077c478bd9Sstevel@tonic-gate 	return (NULL);
62087c478bd9Sstevel@tonic-gate }
62097c478bd9Sstevel@tonic-gate 
62107c478bd9Sstevel@tonic-gate void
62117c478bd9Sstevel@tonic-gate graph_engine_start()
62127c478bd9Sstevel@tonic-gate {
62137c478bd9Sstevel@tonic-gate 	int err;
62147c478bd9Sstevel@tonic-gate 
62157c478bd9Sstevel@tonic-gate 	(void) startd_thread_create(graph_thread, NULL);
62167c478bd9Sstevel@tonic-gate 
62177c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
62187c478bd9Sstevel@tonic-gate 	while (!initial_milestone_set) {
62197c478bd9Sstevel@tonic-gate 		err = pthread_cond_wait(&initial_milestone_cv, &dgraph_lock);
62207c478bd9Sstevel@tonic-gate 		assert(err == 0);
62217c478bd9Sstevel@tonic-gate 	}
62227c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
62237c478bd9Sstevel@tonic-gate 
62247c478bd9Sstevel@tonic-gate 	(void) startd_thread_create(repository_event_thread, NULL);
62257c478bd9Sstevel@tonic-gate 	(void) startd_thread_create(graph_event_thread, NULL);
62267c478bd9Sstevel@tonic-gate }
6227