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