xref: /illumos-gate/usr/src/cmd/svc/startd/graph.c (revision 56998286d4d9e755bec6e2ec4c104309b57bcbaa)
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  */
2153f3aea0SRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
23653e7459Sgowtham thommandra - Sun Microsystems - Bangalore India  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
2444bf619dSJohn Levon  * Copyright 2019 Joyent, Inc.
2509f79f7cSDan Vatca  * Copyright (c) 2015, Syneto S.R.L. All rights reserved.
26a6424c75SToomas Soome  * Copyright 2016 Toomas Soome <tsoome@me.com>
27*56998286SAndrew Stormont  * Copyright 2017 RackTop Systems.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * graph.c - master restarter graph engine
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  *   The graph engine keeps a dependency graph of all service instances on the
347c478bd9Sstevel@tonic-gate  *   system, as recorded in the repository.  It decides when services should
357c478bd9Sstevel@tonic-gate  *   be brought up or down based on service states and dependencies and sends
367c478bd9Sstevel@tonic-gate  *   commands to restarters to effect any changes.  It also executes
377c478bd9Sstevel@tonic-gate  *   administrator commands sent by svcadm via the repository.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  *   The graph is stored in uu_list_t *dgraph and its vertices are
407c478bd9Sstevel@tonic-gate  *   graph_vertex_t's, each of which has a name and an integer id unique to
417c478bd9Sstevel@tonic-gate  *   its name (see dict.c).  A vertex's type attribute designates the type
427c478bd9Sstevel@tonic-gate  *   of object it represents: GVT_INST for service instances, GVT_SVC for
437c478bd9Sstevel@tonic-gate  *   service objects (since service instances may depend on another service,
447c478bd9Sstevel@tonic-gate  *   rather than service instance), GVT_FILE for files (which services may
457c478bd9Sstevel@tonic-gate  *   depend on), and GVT_GROUP for dependencies on multiple objects.  GVT_GROUP
467c478bd9Sstevel@tonic-gate  *   vertices are necessary because dependency lists may have particular
477c478bd9Sstevel@tonic-gate  *   grouping types (require any, require all, optional, or exclude) and
487c478bd9Sstevel@tonic-gate  *   event-propagation characteristics.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  *   The initial graph is built by libscf_populate_graph() invoking
517c478bd9Sstevel@tonic-gate  *   dgraph_add_instance() for each instance in the repository.  The function
527c478bd9Sstevel@tonic-gate  *   adds a GVT_SVC vertex for the service if one does not already exist, adds
537c478bd9Sstevel@tonic-gate  *   a GVT_INST vertex named by the FMRI of the instance, and sets up the edges.
547c478bd9Sstevel@tonic-gate  *   The resulting web of vertices & edges associated with an instance's vertex
557c478bd9Sstevel@tonic-gate  *   includes
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  *     - an edge from the GVT_SVC vertex for the instance's service
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  *     - an edge to the GVT_INST vertex of the instance's resarter, if its
607c478bd9Sstevel@tonic-gate  *       restarter is not svc.startd
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  *     - edges from other GVT_INST vertices if the instance is a restarter
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  *     - for each dependency property group in the instance's "running"
657c478bd9Sstevel@tonic-gate  *       snapshot, an edge to a GVT_GROUP vertex named by the FMRI of the
667c478bd9Sstevel@tonic-gate  *       instance and the name of the property group
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  *     - for each value of the "entities" property in each dependency property
697c478bd9Sstevel@tonic-gate  *       group, an edge from the corresponding GVT_GROUP vertex to a
707c478bd9Sstevel@tonic-gate  *       GVT_INST, GVT_SVC, or GVT_FILE vertex
717c478bd9Sstevel@tonic-gate  *
727c478bd9Sstevel@tonic-gate  *     - edges from GVT_GROUP vertices for each dependent instance
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  *   After the edges are set up the vertex's GV_CONFIGURED flag is set.  If
757c478bd9Sstevel@tonic-gate  *   there are problems, or if a service is mentioned in a dependency but does
767c478bd9Sstevel@tonic-gate  *   not exist in the repository, the GV_CONFIGURED flag will be clear.
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  *   The graph and all of its vertices are protected by the dgraph_lock mutex.
797c478bd9Sstevel@tonic-gate  *   See restarter.c for more information.
807c478bd9Sstevel@tonic-gate  *
817c478bd9Sstevel@tonic-gate  *   The properties of an instance fall into two classes: immediate and
827c478bd9Sstevel@tonic-gate  *   snapshotted.  Immediate properties should have an immediate effect when
837c478bd9Sstevel@tonic-gate  *   changed.  Snapshotted properties should be read from a snapshot, so they
847c478bd9Sstevel@tonic-gate  *   only change when the snapshot changes.  The immediate properties used by
857c478bd9Sstevel@tonic-gate  *   the graph engine are general/enabled, general/restarter, and the properties
867c478bd9Sstevel@tonic-gate  *   in the restarter_actions property group.  Since they are immediate, they
877c478bd9Sstevel@tonic-gate  *   are not read out of a snapshot.  The snapshotted properties used by the
887c478bd9Sstevel@tonic-gate  *   graph engine are those in the property groups with type "dependency" and
897c478bd9Sstevel@tonic-gate  *   are read out of the "running" snapshot.  The "running" snapshot is created
907c478bd9Sstevel@tonic-gate  *   by the the graph engine as soon as possible, and it is updated, along with
917c478bd9Sstevel@tonic-gate  *   in-core copies of the data (dependency information for the graph engine) on
927c478bd9Sstevel@tonic-gate  *   receipt of the refresh command from svcadm.  In addition, the graph engine
937c478bd9Sstevel@tonic-gate  *   updates the "start" snapshot from the "running" snapshot whenever a service
947c478bd9Sstevel@tonic-gate  *   comes online.
95aca380d7SRenaud Manus  *
96aca380d7SRenaud Manus  *   When a DISABLE event is requested by the administrator, svc.startd shutdown
97aca380d7SRenaud Manus  *   the dependents first before shutting down the requested service.
98aca380d7SRenaud Manus  *   In graph_enable_by_vertex, we create a subtree that contains the dependent
99aca380d7SRenaud Manus  *   vertices by marking those vertices with the GV_TOOFFLINE flag. And we mark
100aca380d7SRenaud Manus  *   the vertex to disable with the GV_TODISABLE flag. Once the tree is created,
101aca380d7SRenaud Manus  *   we send the _ADMIN_DISABLE event to the leaves. The leaves will then
102aca380d7SRenaud Manus  *   transition from STATE_ONLINE/STATE_DEGRADED to STATE_OFFLINE/STATE_MAINT.
103aca380d7SRenaud Manus  *   In gt_enter_offline and gt_enter_maint if the vertex was in a subtree then
104aca380d7SRenaud Manus  *   we clear the GV_TOOFFLINE flag and walk the dependencies to offline the new
105aca380d7SRenaud Manus  *   exposed leaves. We do the same until we reach the last leaf (the one with
106aca380d7SRenaud Manus  *   the GV_TODISABLE flag). If the vertex to disable is also part of a larger
107aca380d7SRenaud Manus  *   subtree (eg. multiple DISABLE events on vertices in the same subtree) then
108aca380d7SRenaud Manus  *   once the first vertex is disabled (GV_TODISABLE flag is removed), we
109aca380d7SRenaud Manus  *   continue to propagate the offline event to the vertex's dependencies.
110f6e214c7SGavin Maltby  *
111f6e214c7SGavin Maltby  *
112f6e214c7SGavin Maltby  * SMF state transition notifications
113f6e214c7SGavin Maltby  *
114f6e214c7SGavin Maltby  *   When an instance of a service managed by SMF changes state, svc.startd may
115f6e214c7SGavin Maltby  *   publish a GPEC sysevent. All transitions to or from maintenance, a
116f6e214c7SGavin Maltby  *   transition cause by a hardware error will generate an event.
117f6e214c7SGavin Maltby  *   Other transitions will generate an event if there exist notification
118f6e214c7SGavin Maltby  *   parameter for that transition. Notification parameters are stored in the
119f6e214c7SGavin Maltby  *   SMF repository for the service/instance they refer to. System-wide
120f6e214c7SGavin Maltby  *   notification parameters are stored in the global instance.
121f6e214c7SGavin Maltby  *   svc.startd can be told to send events for all SMF state transitions despite
122f6e214c7SGavin Maltby  *   of notification parameters by setting options/info_events_all to true in
123f6e214c7SGavin Maltby  *   restarter:default
124f6e214c7SGavin Maltby  *
125f6e214c7SGavin Maltby  *   The set of transitions that generate events is cached in the
126f6e214c7SGavin Maltby  *   dgraph_vertex_t gv_stn_tset for service/instance and in the global
127f6e214c7SGavin Maltby  *   stn_global for the system-wide set. They are re-read when instances are
128f6e214c7SGavin Maltby  *   refreshed.
129f6e214c7SGavin Maltby  *
130bbf21555SRichard Lowe  *   The GPEC events published by svc.startd are consumed by fmd(8). After
131bbf21555SRichard Lowe  *   processing these events, fmd(8) publishes the processed events to
132f6e214c7SGavin Maltby  *   notification agents. The notification agents read the notification
133f6e214c7SGavin Maltby  *   parameters from the SMF repository through libscf(3LIB) interfaces and send
134f6e214c7SGavin Maltby  *   the notification, or not, based on those parameters.
135f6e214c7SGavin Maltby  *
136f6e214c7SGavin Maltby  *   Subscription and publishing to the GPEC channels is done with the
137f6e214c7SGavin Maltby  *   libfmevent(3LIB) wrappers fmev_[r]publish_*() and
138f6e214c7SGavin Maltby  *   fmev_shdl_(un)subscribe().
139f6e214c7SGavin Maltby  *
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
1437c478bd9Sstevel@tonic-gate #include <sys/wait.h>
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate #include <assert.h>
1467c478bd9Sstevel@tonic-gate #include <errno.h>
1477c478bd9Sstevel@tonic-gate #include <fcntl.h>
148f6e214c7SGavin Maltby #include <fm/libfmevent.h>
1497c478bd9Sstevel@tonic-gate #include <libscf.h>
1507c478bd9Sstevel@tonic-gate #include <libscf_priv.h>
151f6e214c7SGavin Maltby #include <librestart.h>
1527c478bd9Sstevel@tonic-gate #include <libuutil.h>
1537c478bd9Sstevel@tonic-gate #include <locale.h>
1547c478bd9Sstevel@tonic-gate #include <poll.h>
1557c478bd9Sstevel@tonic-gate #include <pthread.h>
1567c478bd9Sstevel@tonic-gate #include <signal.h>
1577c478bd9Sstevel@tonic-gate #include <stddef.h>
1587c478bd9Sstevel@tonic-gate #include <stdio.h>
1597c478bd9Sstevel@tonic-gate #include <stdlib.h>
1607c478bd9Sstevel@tonic-gate #include <string.h>
1617c478bd9Sstevel@tonic-gate #include <strings.h>
1627c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
1637c478bd9Sstevel@tonic-gate #include <sys/uadmin.h>
1647c478bd9Sstevel@tonic-gate #include <zone.h>
165a6424c75SToomas Soome #if defined(__x86)
166a6424c75SToomas Soome #include <libbe.h>
167a6424c75SToomas Soome #endif	/* __x86 */
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate #include "startd.h"
1707c478bd9Sstevel@tonic-gate #include "protocol.h"
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate #define	MILESTONE_NONE	((graph_vertex_t *)1)
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate #define	CONSOLE_LOGIN_FMRI	"svc:/system/console-login:default"
1767c478bd9Sstevel@tonic-gate #define	FS_MINIMAL_FMRI		"svc:/system/filesystem/minimal:default"
1777c478bd9Sstevel@tonic-gate 
1783ad28c1eSrm88369 #define	VERTEX_REMOVED	0	/* vertex has been freed  */
1793ad28c1eSrm88369 #define	VERTEX_INUSE	1	/* vertex is still in use */
1803ad28c1eSrm88369 
181f6e214c7SGavin Maltby #define	IS_ENABLED(v) ((v)->gv_flags & (GV_ENABLED | GV_ENBLD_NOOVR))
182f6e214c7SGavin Maltby 
183f6e214c7SGavin Maltby /*
184f6e214c7SGavin Maltby  * stn_global holds the tset for the system wide notification parameters.
185f6e214c7SGavin Maltby  * It is updated on refresh of svc:/system/svc/global:default
186f6e214c7SGavin Maltby  *
187f6e214c7SGavin Maltby  * There are two assumptions that relax the need for a mutex:
188f6e214c7SGavin Maltby  *     1. 32-bit value assignments are atomic
189f6e214c7SGavin Maltby  *     2. Its value is consumed only in one point at
190f6e214c7SGavin Maltby  *     dgraph_state_transition_notify(). There are no test and set races.
191f6e214c7SGavin Maltby  *
192f6e214c7SGavin Maltby  *     If either assumption is broken, we'll need a mutex to synchronize
193f6e214c7SGavin Maltby  *     access to stn_global
194f6e214c7SGavin Maltby  */
195f6e214c7SGavin Maltby int32_t stn_global;
196f6e214c7SGavin Maltby /*
197f6e214c7SGavin Maltby  * info_events_all holds a flag to override notification parameters and send
198f6e214c7SGavin Maltby  * Information events for all state transitions.
199f6e214c7SGavin Maltby  * same about the need of a mutex here.
200f6e214c7SGavin Maltby  */
201f6e214c7SGavin Maltby int info_events_all;
202f6e214c7SGavin Maltby 
20356e23938Sbustos /*
20456e23938Sbustos  * Services in these states are not considered 'down' by the
20556e23938Sbustos  * milestone/shutdown code.
20656e23938Sbustos  */
20756e23938Sbustos #define	up_state(state)	((state) == RESTARTER_STATE_ONLINE || \
20856e23938Sbustos 	(state) == RESTARTER_STATE_DEGRADED || \
20956e23938Sbustos 	(state) == RESTARTER_STATE_OFFLINE)
21056e23938Sbustos 
211653e7459Sgowtham thommandra - Sun Microsystems - Bangalore India #define	is_depgrp_bypassed(v) ((v->gv_type == GVT_GROUP) && \
212653e7459Sgowtham thommandra - Sun Microsystems - Bangalore India 	((v->gv_depgroup == DEPGRP_EXCLUDE_ALL) || \
213653e7459Sgowtham thommandra - Sun Microsystems - Bangalore India 	(v->gv_restart < RERR_RESTART)))
214653e7459Sgowtham thommandra - Sun Microsystems - Bangalore India 
2153e207980SAndrew Stormont #define	is_inst_bypassed(v) ((v->gv_type == GVT_INST) && \
2163e207980SAndrew Stormont 	((v->gv_flags & GV_TODISABLE) || \
2173e207980SAndrew Stormont 	(v->gv_flags & GV_TOOFFLINE)))
2183e207980SAndrew Stormont 
2197c478bd9Sstevel@tonic-gate static uu_list_pool_t *graph_edge_pool, *graph_vertex_pool;
2207c478bd9Sstevel@tonic-gate static uu_list_t *dgraph;
2217c478bd9Sstevel@tonic-gate static pthread_mutex_t dgraph_lock;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * milestone indicates the current subgraph.  When NULL, it is the entire
2257c478bd9Sstevel@tonic-gate  * graph.  When MILESTONE_NONE, it is the empty graph.  Otherwise, it is all
2267c478bd9Sstevel@tonic-gate  * services on which the target vertex depends.
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate static graph_vertex_t *milestone = NULL;
2297c478bd9Sstevel@tonic-gate static boolean_t initial_milestone_set = B_FALSE;
2307c478bd9Sstevel@tonic-gate static pthread_cond_t initial_milestone_cv = PTHREAD_COND_INITIALIZER;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /* protected by dgraph_lock */
2337c478bd9Sstevel@tonic-gate static boolean_t sulogin_thread_running = B_FALSE;
2347c478bd9Sstevel@tonic-gate static boolean_t sulogin_running = B_FALSE;
2357c478bd9Sstevel@tonic-gate static boolean_t console_login_ready = B_FALSE;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate /* Number of services to come down to complete milestone transition. */
2387c478bd9Sstevel@tonic-gate static uint_t non_subgraph_svcs;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate  * These variables indicate what should be done when we reach the milestone
2427c478bd9Sstevel@tonic-gate  * target milestone, i.e., when non_subgraph_svcs == 0.  They are acted upon in
2437c478bd9Sstevel@tonic-gate  * dgraph_set_instance_state().
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate static int halting = -1;
2467c478bd9Sstevel@tonic-gate static boolean_t go_single_user_mode = B_FALSE;
2477c478bd9Sstevel@tonic-gate static boolean_t go_to_level1 = B_FALSE;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2504d53c7adSDan Price  * Tracks when we started halting.
2514d53c7adSDan Price  */
2524d53c7adSDan Price static time_t halting_time = 0;
2534d53c7adSDan Price 
2544d53c7adSDan Price /*
2557c478bd9Sstevel@tonic-gate  * This tracks the legacy runlevel to ensure we signal init and manage
2567c478bd9Sstevel@tonic-gate  * utmpx entries correctly.
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate static char current_runlevel = '\0';
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /* Number of single user threads currently running */
2617c478bd9Sstevel@tonic-gate static pthread_mutex_t single_user_thread_lock;
2627c478bd9Sstevel@tonic-gate static int single_user_thread_count = 0;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate /* Statistics for dependency cycle-checking */
2657c478bd9Sstevel@tonic-gate static u_longlong_t dep_inserts = 0;
2667c478bd9Sstevel@tonic-gate static u_longlong_t dep_cycle_ns = 0;
2677c478bd9Sstevel@tonic-gate static u_longlong_t dep_insert_ns = 0;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate static const char * const emsg_invalid_restarter =
27199b44c3bSlianep 	"Transitioning %s to maintenance, restarter FMRI %s is invalid "
27299b44c3bSlianep 	"(see 'svcs -xv' for details).\n";
2737c478bd9Sstevel@tonic-gate static const char * const console_login_fmri = CONSOLE_LOGIN_FMRI;
2747c478bd9Sstevel@tonic-gate static const char * const single_user_fmri = SCF_MILESTONE_SINGLE_USER;
2757c478bd9Sstevel@tonic-gate static const char * const multi_user_fmri = SCF_MILESTONE_MULTI_USER;
2767c478bd9Sstevel@tonic-gate static const char * const multi_user_svr_fmri = SCF_MILESTONE_MULTI_USER_SERVER;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * These services define the system being "up".  If none of them can come
2817c478bd9Sstevel@tonic-gate  * online, then we will run sulogin on the console.  Note that the install ones
2827c478bd9Sstevel@tonic-gate  * are for the miniroot and when installing CDs after the first.  can_come_up()
2837c478bd9Sstevel@tonic-gate  * does the decision making, and an sulogin_thread() runs sulogin, which can be
2847c478bd9Sstevel@tonic-gate  * started by dgraph_set_instance_state() or single_user_thread().
2857c478bd9Sstevel@tonic-gate  *
2867c478bd9Sstevel@tonic-gate  * NOTE: can_come_up() relies on SCF_MILESTONE_SINGLE_USER being the first
2877c478bd9Sstevel@tonic-gate  * entry, which is only used when booting_to_single_user (boot -s) is set.
2887c478bd9Sstevel@tonic-gate  * This is because when doing a "boot -s", sulogin is started from specials.c
2897c478bd9Sstevel@tonic-gate  * after milestone/single-user comes online, for backwards compatibility.
2907c478bd9Sstevel@tonic-gate  * In this case, SCF_MILESTONE_SINGLE_USER needs to be part of up_svcs
2917c478bd9Sstevel@tonic-gate  * to ensure sulogin will be spawned if milestone/single-user cannot be reached.
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate static const char * const up_svcs[] = {
2947c478bd9Sstevel@tonic-gate 	SCF_MILESTONE_SINGLE_USER,
2957c478bd9Sstevel@tonic-gate 	CONSOLE_LOGIN_FMRI,
2967c478bd9Sstevel@tonic-gate 	"svc:/system/install-setup:default",
2977c478bd9Sstevel@tonic-gate 	"svc:/system/install:default",
2987c478bd9Sstevel@tonic-gate 	NULL
2997c478bd9Sstevel@tonic-gate };
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate /* This array must have an element for each non-NULL element of up_svcs[]. */
3027c478bd9Sstevel@tonic-gate static graph_vertex_t *up_svcs_p[] = { NULL, NULL, NULL, NULL };
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate /* These are for seed repository magic.  See can_come_up(). */
3059444c26fSTom Whitten static const char * const manifest_import = SCF_INSTANCE_MI;
3067c478bd9Sstevel@tonic-gate static graph_vertex_t *manifest_import_p = NULL;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate static char target_milestone_as_runlevel(void);
3107c478bd9Sstevel@tonic-gate static void graph_runlevel_changed(char rl, int online);
3117c478bd9Sstevel@tonic-gate static int dgraph_set_milestone(const char *, scf_handle_t *, boolean_t);
3127c478bd9Sstevel@tonic-gate static boolean_t should_be_in_subgraph(graph_vertex_t *v);
313aca380d7SRenaud Manus static int mark_subtree(graph_edge_t *, void *);
314aca380d7SRenaud Manus static boolean_t insubtree_dependents_down(graph_vertex_t *);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /*
3177c478bd9Sstevel@tonic-gate  * graph_vertex_compare()
3187c478bd9Sstevel@tonic-gate  *	This function can compare either int *id or * graph_vertex_t *gv
3197c478bd9Sstevel@tonic-gate  *	values, as the vertex id is always the first element of a
3207c478bd9Sstevel@tonic-gate  *	graph_vertex structure.
3217c478bd9Sstevel@tonic-gate  */
3227c478bd9Sstevel@tonic-gate /* ARGSUSED */
3237c478bd9Sstevel@tonic-gate static int
graph_vertex_compare(const void * lc_arg,const void * rc_arg,void * private)3247c478bd9Sstevel@tonic-gate graph_vertex_compare(const void *lc_arg, const void *rc_arg, void *private)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	int lc_id = ((const graph_vertex_t *)lc_arg)->gv_id;
3277c478bd9Sstevel@tonic-gate 	int rc_id = *(int *)rc_arg;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	if (lc_id > rc_id)
3307c478bd9Sstevel@tonic-gate 		return (1);
3317c478bd9Sstevel@tonic-gate 	if (lc_id < rc_id)
3327c478bd9Sstevel@tonic-gate 		return (-1);
3337c478bd9Sstevel@tonic-gate 	return (0);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate void
graph_init()3377c478bd9Sstevel@tonic-gate graph_init()
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	graph_edge_pool = startd_list_pool_create("graph_edges",
3407c478bd9Sstevel@tonic-gate 	    sizeof (graph_edge_t), offsetof(graph_edge_t, ge_link), NULL,
3417c478bd9Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG);
3427c478bd9Sstevel@tonic-gate 	assert(graph_edge_pool != NULL);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	graph_vertex_pool = startd_list_pool_create("graph_vertices",
3457c478bd9Sstevel@tonic-gate 	    sizeof (graph_vertex_t), offsetof(graph_vertex_t, gv_link),
3467c478bd9Sstevel@tonic-gate 	    graph_vertex_compare, UU_LIST_POOL_DEBUG);
3477c478bd9Sstevel@tonic-gate 	assert(graph_vertex_pool != NULL);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&dgraph_lock, &mutex_attrs);
3507c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&single_user_thread_lock, &mutex_attrs);
3517c478bd9Sstevel@tonic-gate 	dgraph = startd_list_create(graph_vertex_pool, NULL, UU_LIST_SORTED);
3527c478bd9Sstevel@tonic-gate 	assert(dgraph != NULL);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (!st->st_initial)
3557c478bd9Sstevel@tonic-gate 		current_runlevel = utmpx_get_runlevel();
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Initialized graph\n");
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate static graph_vertex_t *
vertex_get_by_name(const char * name)3617c478bd9Sstevel@tonic-gate vertex_get_by_name(const char *name)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	int id;
3647c478bd9Sstevel@tonic-gate 
36553f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	id = dict_lookup_byname(name);
3687c478bd9Sstevel@tonic-gate 	if (id == -1)
3697c478bd9Sstevel@tonic-gate 		return (NULL);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	return (uu_list_find(dgraph, &id, NULL, NULL));
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate static graph_vertex_t *
vertex_get_by_id(int id)3757c478bd9Sstevel@tonic-gate vertex_get_by_id(int id)
3767c478bd9Sstevel@tonic-gate {
37753f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if (id == -1)
3807c478bd9Sstevel@tonic-gate 		return (NULL);
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	return (uu_list_find(dgraph, &id, NULL, NULL));
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate /*
3867c478bd9Sstevel@tonic-gate  * Creates a new vertex with the given name, adds it to the graph, and returns
3877c478bd9Sstevel@tonic-gate  * a pointer to it.  The graph lock must be held by this thread on entry.
3887c478bd9Sstevel@tonic-gate  */
3897c478bd9Sstevel@tonic-gate static graph_vertex_t *
graph_add_vertex(const char * name)3907c478bd9Sstevel@tonic-gate graph_add_vertex(const char *name)
3917c478bd9Sstevel@tonic-gate {
3927c478bd9Sstevel@tonic-gate 	int id;
3937c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
3947c478bd9Sstevel@tonic-gate 	void *p;
3957c478bd9Sstevel@tonic-gate 	uu_list_index_t idx;
3967c478bd9Sstevel@tonic-gate 
39753f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	id = dict_insert(name);
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	v = startd_zalloc(sizeof (*v));
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	v->gv_id = id;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	v->gv_name = startd_alloc(strlen(name) + 1);
4067c478bd9Sstevel@tonic-gate 	(void) strcpy(v->gv_name, name);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	v->gv_dependencies = startd_list_create(graph_edge_pool, v, 0);
4097c478bd9Sstevel@tonic-gate 	v->gv_dependents = startd_list_create(graph_edge_pool, v, 0);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	p = uu_list_find(dgraph, &id, NULL, &idx);
4127c478bd9Sstevel@tonic-gate 	assert(p == NULL);
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	uu_list_node_init(v, &v->gv_link, graph_vertex_pool);
4157c478bd9Sstevel@tonic-gate 	uu_list_insert(dgraph, v, idx);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	return (v);
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate  * Removes v from the graph and frees it.  The graph should be locked by this
4227c478bd9Sstevel@tonic-gate  * thread, and v should have no edges associated with it.
4237c478bd9Sstevel@tonic-gate  */
4247c478bd9Sstevel@tonic-gate static void
graph_remove_vertex(graph_vertex_t * v)4257c478bd9Sstevel@tonic-gate graph_remove_vertex(graph_vertex_t *v)
4267c478bd9Sstevel@tonic-gate {
42753f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependencies) == 0);
4307c478bd9Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependents) == 0);
4313ad28c1eSrm88369 	assert(v->gv_refs == 0);
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	startd_free(v->gv_name, strlen(v->gv_name) + 1);
4347c478bd9Sstevel@tonic-gate 	uu_list_destroy(v->gv_dependencies);
4357c478bd9Sstevel@tonic-gate 	uu_list_destroy(v->gv_dependents);
4367c478bd9Sstevel@tonic-gate 	uu_list_remove(dgraph, v);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	startd_free(v, sizeof (graph_vertex_t));
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate static void
graph_add_edge(graph_vertex_t * fv,graph_vertex_t * tv)4427c478bd9Sstevel@tonic-gate graph_add_edge(graph_vertex_t *fv, graph_vertex_t *tv)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	graph_edge_t *e, *re;
4457c478bd9Sstevel@tonic-gate 	int r;
4467c478bd9Sstevel@tonic-gate 
44753f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	e = startd_alloc(sizeof (graph_edge_t));
4507c478bd9Sstevel@tonic-gate 	re = startd_alloc(sizeof (graph_edge_t));
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	e->ge_parent = fv;
4537c478bd9Sstevel@tonic-gate 	e->ge_vertex = tv;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	re->ge_parent = tv;
4567c478bd9Sstevel@tonic-gate 	re->ge_vertex = fv;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	uu_list_node_init(e, &e->ge_link, graph_edge_pool);
4597c478bd9Sstevel@tonic-gate 	r = uu_list_insert_before(fv->gv_dependencies, NULL, e);
4607c478bd9Sstevel@tonic-gate 	assert(r == 0);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	uu_list_node_init(re, &re->ge_link, graph_edge_pool);
4637c478bd9Sstevel@tonic-gate 	r = uu_list_insert_before(tv->gv_dependents, NULL, re);
4647c478bd9Sstevel@tonic-gate 	assert(r == 0);
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate static void
graph_remove_edge(graph_vertex_t * v,graph_vertex_t * dv)4687c478bd9Sstevel@tonic-gate graph_remove_edge(graph_vertex_t *v, graph_vertex_t *dv)
4697c478bd9Sstevel@tonic-gate {
4707c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
4737c478bd9Sstevel@tonic-gate 	    e != NULL;
4747c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
4757c478bd9Sstevel@tonic-gate 		if (e->ge_vertex == dv) {
4767c478bd9Sstevel@tonic-gate 			uu_list_remove(v->gv_dependencies, e);
4777c478bd9Sstevel@tonic-gate 			startd_free(e, sizeof (graph_edge_t));
4787c478bd9Sstevel@tonic-gate 			break;
4797c478bd9Sstevel@tonic-gate 		}
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(dv->gv_dependents);
4837c478bd9Sstevel@tonic-gate 	    e != NULL;
4847c478bd9Sstevel@tonic-gate 	    e = uu_list_next(dv->gv_dependents, e)) {
4857c478bd9Sstevel@tonic-gate 		if (e->ge_vertex == v) {
4867c478bd9Sstevel@tonic-gate 			uu_list_remove(dv->gv_dependents, e);
4877c478bd9Sstevel@tonic-gate 			startd_free(e, sizeof (graph_edge_t));
4887c478bd9Sstevel@tonic-gate 			break;
4897c478bd9Sstevel@tonic-gate 		}
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate static void
remove_inst_vertex(graph_vertex_t * v)4943ad28c1eSrm88369 remove_inst_vertex(graph_vertex_t *v)
4953ad28c1eSrm88369 {
4963ad28c1eSrm88369 	graph_edge_t *e;
4973ad28c1eSrm88369 	graph_vertex_t *sv;
4983ad28c1eSrm88369 	int i;
4993ad28c1eSrm88369 
50053f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
5013ad28c1eSrm88369 	assert(uu_list_numnodes(v->gv_dependents) == 1);
5023ad28c1eSrm88369 	assert(uu_list_numnodes(v->gv_dependencies) == 0);
5033ad28c1eSrm88369 	assert(v->gv_refs == 0);
5043ad28c1eSrm88369 	assert((v->gv_flags & GV_CONFIGURED) == 0);
5053ad28c1eSrm88369 
5063ad28c1eSrm88369 	e = uu_list_first(v->gv_dependents);
5073ad28c1eSrm88369 	sv = e->ge_vertex;
5083ad28c1eSrm88369 	graph_remove_edge(sv, v);
5093ad28c1eSrm88369 
5103ad28c1eSrm88369 	for (i = 0; up_svcs[i] != NULL; ++i) {
5113ad28c1eSrm88369 		if (up_svcs_p[i] == v)
5123ad28c1eSrm88369 			up_svcs_p[i] = NULL;
5133ad28c1eSrm88369 	}
5143ad28c1eSrm88369 
5153ad28c1eSrm88369 	if (manifest_import_p == v)
5163ad28c1eSrm88369 		manifest_import_p = NULL;
5173ad28c1eSrm88369 
5183ad28c1eSrm88369 	graph_remove_vertex(v);
5193ad28c1eSrm88369 
5203ad28c1eSrm88369 	if (uu_list_numnodes(sv->gv_dependencies) == 0 &&
5213ad28c1eSrm88369 	    uu_list_numnodes(sv->gv_dependents) == 0 &&
5223ad28c1eSrm88369 	    sv->gv_refs == 0)
5233ad28c1eSrm88369 		graph_remove_vertex(sv);
5243ad28c1eSrm88369 }
5253ad28c1eSrm88369 
5263ad28c1eSrm88369 static void
graph_walk_dependents(graph_vertex_t * v,void (* func)(graph_vertex_t *,void *),void * arg)5277c478bd9Sstevel@tonic-gate graph_walk_dependents(graph_vertex_t *v, void (*func)(graph_vertex_t *, void *),
5287c478bd9Sstevel@tonic-gate     void *arg)
5297c478bd9Sstevel@tonic-gate {
5307c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
5337c478bd9Sstevel@tonic-gate 	    e != NULL;
5347c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e))
5357c478bd9Sstevel@tonic-gate 		func(e->ge_vertex, arg);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate static void
graph_walk_dependencies(graph_vertex_t * v,void (* func)(graph_vertex_t *,void *),void * arg)539a6424c75SToomas Soome graph_walk_dependencies(graph_vertex_t *v,
540a6424c75SToomas Soome     void (*func)(graph_vertex_t *, void *), void *arg)
5417c478bd9Sstevel@tonic-gate {
5427c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
5437c478bd9Sstevel@tonic-gate 
54453f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
5477c478bd9Sstevel@tonic-gate 	    e != NULL;
5487c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 		func(e->ge_vertex, arg);
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate  * Generic graph walking function.
5567c478bd9Sstevel@tonic-gate  *
5577c478bd9Sstevel@tonic-gate  * Given a vertex, this function will walk either dependencies
5587c478bd9Sstevel@tonic-gate  * (WALK_DEPENDENCIES) or dependents (WALK_DEPENDENTS) of a vertex recursively
5597c478bd9Sstevel@tonic-gate  * for the entire graph.  It will avoid cycles and never visit the same vertex
5607c478bd9Sstevel@tonic-gate  * twice.
5617c478bd9Sstevel@tonic-gate  *
5627c478bd9Sstevel@tonic-gate  * We avoid traversing exclusion dependencies, because they are allowed to
5637c478bd9Sstevel@tonic-gate  * create cycles in the graph.  When propagating satisfiability, there is no
5647c478bd9Sstevel@tonic-gate  * need to walk exclusion dependencies because exclude_all_satisfied() doesn't
5657c478bd9Sstevel@tonic-gate  * test for satisfiability.
5667c478bd9Sstevel@tonic-gate  *
5677c478bd9Sstevel@tonic-gate  * The walker takes two callbacks.  The first is called before examining the
5687c478bd9Sstevel@tonic-gate  * dependents of each vertex.  The second is called on each vertex after
5697c478bd9Sstevel@tonic-gate  * examining its dependents.  This allows is_path_to() to construct a path only
5707c478bd9Sstevel@tonic-gate  * after the target vertex has been found.
5717c478bd9Sstevel@tonic-gate  */
5727c478bd9Sstevel@tonic-gate typedef enum {
5737c478bd9Sstevel@tonic-gate 	WALK_DEPENDENTS,
5747c478bd9Sstevel@tonic-gate 	WALK_DEPENDENCIES
5757c478bd9Sstevel@tonic-gate } graph_walk_dir_t;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate typedef int (*graph_walk_cb_t)(graph_vertex_t *, void *);
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate typedef struct graph_walk_info {
5807c478bd9Sstevel@tonic-gate 	graph_walk_dir_t	gi_dir;
5817c478bd9Sstevel@tonic-gate 	uchar_t			*gi_visited;	/* vertex bitmap */
5827c478bd9Sstevel@tonic-gate 	int			(*gi_pre)(graph_vertex_t *, void *);
5837c478bd9Sstevel@tonic-gate 	void			(*gi_post)(graph_vertex_t *, void *);
5847c478bd9Sstevel@tonic-gate 	void			*gi_arg;	/* callback arg */
5857c478bd9Sstevel@tonic-gate 	int			gi_ret;		/* return value */
5867c478bd9Sstevel@tonic-gate } graph_walk_info_t;
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate static int
graph_walk_recurse(graph_edge_t * e,graph_walk_info_t * gip)5897c478bd9Sstevel@tonic-gate graph_walk_recurse(graph_edge_t *e, graph_walk_info_t *gip)
5907c478bd9Sstevel@tonic-gate {
5917c478bd9Sstevel@tonic-gate 	uu_list_t *list;
5927c478bd9Sstevel@tonic-gate 	int r;
5937c478bd9Sstevel@tonic-gate 	graph_vertex_t *v = e->ge_vertex;
5947c478bd9Sstevel@tonic-gate 	int i;
5957c478bd9Sstevel@tonic-gate 	uint_t b;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	i = v->gv_id / 8;
5987c478bd9Sstevel@tonic-gate 	b = 1 << (v->gv_id % 8);
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/*
6017c478bd9Sstevel@tonic-gate 	 * Check to see if we've visited this vertex already.
6027c478bd9Sstevel@tonic-gate 	 */
6037c478bd9Sstevel@tonic-gate 	if (gip->gi_visited[i] & b)
6047c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	gip->gi_visited[i] |= b;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/*
6097c478bd9Sstevel@tonic-gate 	 * Don't follow exclusions.
6107c478bd9Sstevel@tonic-gate 	 */
6117c478bd9Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
6127c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	/*
6157c478bd9Sstevel@tonic-gate 	 * Call pre-visit callback.  If this doesn't terminate the walk,
6167c478bd9Sstevel@tonic-gate 	 * continue search.
6177c478bd9Sstevel@tonic-gate 	 */
6187c478bd9Sstevel@tonic-gate 	if ((gip->gi_ret = gip->gi_pre(v, gip->gi_arg)) == UU_WALK_NEXT) {
6197c478bd9Sstevel@tonic-gate 		/*
6207c478bd9Sstevel@tonic-gate 		 * Recurse using appropriate list.
6217c478bd9Sstevel@tonic-gate 		 */
6227c478bd9Sstevel@tonic-gate 		if (gip->gi_dir == WALK_DEPENDENTS)
6237c478bd9Sstevel@tonic-gate 			list = v->gv_dependents;
6247c478bd9Sstevel@tonic-gate 		else
6257c478bd9Sstevel@tonic-gate 			list = v->gv_dependencies;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 		r = uu_list_walk(list, (uu_walk_fn_t *)graph_walk_recurse,
6287c478bd9Sstevel@tonic-gate 		    gip, 0);
6297c478bd9Sstevel@tonic-gate 		assert(r == 0);
6307c478bd9Sstevel@tonic-gate 	}
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	/*
6337c478bd9Sstevel@tonic-gate 	 * Callbacks must return either UU_WALK_NEXT or UU_WALK_DONE.
6347c478bd9Sstevel@tonic-gate 	 */
6357c478bd9Sstevel@tonic-gate 	assert(gip->gi_ret == UU_WALK_NEXT || gip->gi_ret == UU_WALK_DONE);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	/*
6387c478bd9Sstevel@tonic-gate 	 * If given a post-callback, call the function for every vertex.
6397c478bd9Sstevel@tonic-gate 	 */
6407c478bd9Sstevel@tonic-gate 	if (gip->gi_post != NULL)
6417c478bd9Sstevel@tonic-gate 		(void) gip->gi_post(v, gip->gi_arg);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	/*
6447c478bd9Sstevel@tonic-gate 	 * Preserve the callback's return value.  If the callback returns
6457c478bd9Sstevel@tonic-gate 	 * UU_WALK_DONE, then we propagate that to the caller in order to
6467c478bd9Sstevel@tonic-gate 	 * terminate the walk.
6477c478bd9Sstevel@tonic-gate 	 */
6487c478bd9Sstevel@tonic-gate 	return (gip->gi_ret);
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate static void
graph_walk(graph_vertex_t * v,graph_walk_dir_t dir,int (* pre)(graph_vertex_t *,void *),void (* post)(graph_vertex_t *,void *),void * arg)6527c478bd9Sstevel@tonic-gate graph_walk(graph_vertex_t *v, graph_walk_dir_t dir,
6537c478bd9Sstevel@tonic-gate     int (*pre)(graph_vertex_t *, void *),
6547c478bd9Sstevel@tonic-gate     void (*post)(graph_vertex_t *, void *), void *arg)
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate 	graph_walk_info_t gi;
6577c478bd9Sstevel@tonic-gate 	graph_edge_t fake;
6587c478bd9Sstevel@tonic-gate 	size_t sz = dictionary->dict_new_id / 8 + 1;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	gi.gi_visited = startd_zalloc(sz);
6617c478bd9Sstevel@tonic-gate 	gi.gi_pre = pre;
6627c478bd9Sstevel@tonic-gate 	gi.gi_post = post;
6637c478bd9Sstevel@tonic-gate 	gi.gi_arg = arg;
6647c478bd9Sstevel@tonic-gate 	gi.gi_dir = dir;
6657c478bd9Sstevel@tonic-gate 	gi.gi_ret = 0;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	/*
6687c478bd9Sstevel@tonic-gate 	 * Fake up an edge for the first iteration
6697c478bd9Sstevel@tonic-gate 	 */
6707c478bd9Sstevel@tonic-gate 	fake.ge_vertex = v;
6717c478bd9Sstevel@tonic-gate 	(void) graph_walk_recurse(&fake, &gi);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	startd_free(gi.gi_visited, sz);
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate typedef struct child_search {
6777c478bd9Sstevel@tonic-gate 	int	id;		/* id of vertex to look for */
6787c478bd9Sstevel@tonic-gate 	uint_t	depth;		/* recursion depth */
6797c478bd9Sstevel@tonic-gate 	/*
6807c478bd9Sstevel@tonic-gate 	 * While the vertex is not found, path is NULL.  After the search, if
6817c478bd9Sstevel@tonic-gate 	 * the vertex was found then path should point to a -1-terminated
6827c478bd9Sstevel@tonic-gate 	 * array of vertex id's which constitute the path to the vertex.
6837c478bd9Sstevel@tonic-gate 	 */
6847c478bd9Sstevel@tonic-gate 	int	*path;
6857c478bd9Sstevel@tonic-gate } child_search_t;
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate static int
child_pre(graph_vertex_t * v,void * arg)6887c478bd9Sstevel@tonic-gate child_pre(graph_vertex_t *v, void *arg)
6897c478bd9Sstevel@tonic-gate {
6907c478bd9Sstevel@tonic-gate 	child_search_t *cs = arg;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	cs->depth++;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	if (v->gv_id == cs->id) {
6957c478bd9Sstevel@tonic-gate 		cs->path = startd_alloc((cs->depth + 1) * sizeof (int));
6967c478bd9Sstevel@tonic-gate 		cs->path[cs->depth] = -1;
6977c478bd9Sstevel@tonic-gate 		return (UU_WALK_DONE);
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate static void
child_post(graph_vertex_t * v,void * arg)7047c478bd9Sstevel@tonic-gate child_post(graph_vertex_t *v, void *arg)
7057c478bd9Sstevel@tonic-gate {
7067c478bd9Sstevel@tonic-gate 	child_search_t *cs = arg;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	cs->depth--;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	if (cs->path != NULL)
7117c478bd9Sstevel@tonic-gate 		cs->path[cs->depth] = v->gv_id;
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate /*
7157c478bd9Sstevel@tonic-gate  * Look for a path from from to to.  If one exists, returns a pointer to
7167c478bd9Sstevel@tonic-gate  * a NULL-terminated array of pointers to the vertices along the path.  If
7177c478bd9Sstevel@tonic-gate  * there is no path, returns NULL.
7187c478bd9Sstevel@tonic-gate  */
7197c478bd9Sstevel@tonic-gate static int *
is_path_to(graph_vertex_t * from,graph_vertex_t * to)7207c478bd9Sstevel@tonic-gate is_path_to(graph_vertex_t *from, graph_vertex_t *to)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	child_search_t cs;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	cs.id = to->gv_id;
7257c478bd9Sstevel@tonic-gate 	cs.depth = 0;
7267c478bd9Sstevel@tonic-gate 	cs.path = NULL;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	graph_walk(from, WALK_DEPENDENCIES, child_pre, child_post, &cs);
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	return (cs.path);
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate /*
7347c478bd9Sstevel@tonic-gate  * Given an array of int's as returned by is_path_to, allocates a string of
7357c478bd9Sstevel@tonic-gate  * their names joined by newlines.  Returns the size of the allocated buffer
7367c478bd9Sstevel@tonic-gate  * in *sz and frees path.
7377c478bd9Sstevel@tonic-gate  */
7387c478bd9Sstevel@tonic-gate static void
path_to_str(int * path,char ** cpp,size_t * sz)7397c478bd9Sstevel@tonic-gate path_to_str(int *path, char **cpp, size_t *sz)
7407c478bd9Sstevel@tonic-gate {
7417c478bd9Sstevel@tonic-gate 	int i;
7427c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
7437c478bd9Sstevel@tonic-gate 	size_t allocd, new_allocd;
7447c478bd9Sstevel@tonic-gate 	char *new, *name;
7457c478bd9Sstevel@tonic-gate 
74653f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
7477c478bd9Sstevel@tonic-gate 	assert(path[0] != -1);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	allocd = 1;
7507c478bd9Sstevel@tonic-gate 	*cpp = startd_alloc(1);
7517c478bd9Sstevel@tonic-gate 	(*cpp)[0] = '\0';
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	for (i = 0; path[i] != -1; ++i) {
7547c478bd9Sstevel@tonic-gate 		name = NULL;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 		v = vertex_get_by_id(path[i]);
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 		if (v == NULL)
7597c478bd9Sstevel@tonic-gate 			name = "<deleted>";
7607c478bd9Sstevel@tonic-gate 		else if (v->gv_type == GVT_INST || v->gv_type == GVT_SVC)
7617c478bd9Sstevel@tonic-gate 			name = v->gv_name;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 		if (name != NULL) {
7647c478bd9Sstevel@tonic-gate 			new_allocd = allocd + strlen(name) + 1;
7657c478bd9Sstevel@tonic-gate 			new = startd_alloc(new_allocd);
7667c478bd9Sstevel@tonic-gate 			(void) strcpy(new, *cpp);
7677c478bd9Sstevel@tonic-gate 			(void) strcat(new, name);
7687c478bd9Sstevel@tonic-gate 			(void) strcat(new, "\n");
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 			startd_free(*cpp, allocd);
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 			*cpp = new;
7737c478bd9Sstevel@tonic-gate 			allocd = new_allocd;
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	startd_free(path, sizeof (int) * (i + 1));
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	*sz = allocd;
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate /*
7847c478bd9Sstevel@tonic-gate  * This function along with run_sulogin() implements an exclusion relationship
7857c478bd9Sstevel@tonic-gate  * between system/console-login and sulogin.  run_sulogin() will fail if
7867c478bd9Sstevel@tonic-gate  * system/console-login is online, and the graph engine should call
7877c478bd9Sstevel@tonic-gate  * graph_clogin_start() to bring system/console-login online, which defers the
7887c478bd9Sstevel@tonic-gate  * start if sulogin is running.
7897c478bd9Sstevel@tonic-gate  */
7907c478bd9Sstevel@tonic-gate static void
graph_clogin_start(graph_vertex_t * v)7917c478bd9Sstevel@tonic-gate graph_clogin_start(graph_vertex_t *v)
7927c478bd9Sstevel@tonic-gate {
79353f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	if (sulogin_running)
7967c478bd9Sstevel@tonic-gate 		console_login_ready = B_TRUE;
7977c478bd9Sstevel@tonic-gate 	else
7987c478bd9Sstevel@tonic-gate 		vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate static void
graph_su_start(graph_vertex_t * v)8027c478bd9Sstevel@tonic-gate graph_su_start(graph_vertex_t *v)
8037c478bd9Sstevel@tonic-gate {
8047c478bd9Sstevel@tonic-gate 	/*
8057c478bd9Sstevel@tonic-gate 	 * /etc/inittab used to have the initial /sbin/rcS as a 'sysinit'
8067c478bd9Sstevel@tonic-gate 	 * entry with a runlevel of 'S', before jumping to the final
8077c478bd9Sstevel@tonic-gate 	 * target runlevel (as set in initdefault).  We mimic that legacy
8087c478bd9Sstevel@tonic-gate 	 * behavior here.
8097c478bd9Sstevel@tonic-gate 	 */
8107c478bd9Sstevel@tonic-gate 	utmpx_set_runlevel('S', '0', B_FALSE);
8117c478bd9Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate static void
graph_post_su_online(void)8157c478bd9Sstevel@tonic-gate graph_post_su_online(void)
8167c478bd9Sstevel@tonic-gate {
8177c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('S', 1);
8187c478bd9Sstevel@tonic-gate }
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate static void
graph_post_su_disable(void)8217c478bd9Sstevel@tonic-gate graph_post_su_disable(void)
8227c478bd9Sstevel@tonic-gate {
8237c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('S', 0);
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate static void
graph_post_mu_online(void)8277c478bd9Sstevel@tonic-gate graph_post_mu_online(void)
8287c478bd9Sstevel@tonic-gate {
8297c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('2', 1);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate static void
graph_post_mu_disable(void)8337c478bd9Sstevel@tonic-gate graph_post_mu_disable(void)
8347c478bd9Sstevel@tonic-gate {
8357c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('2', 0);
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate static void
graph_post_mus_online(void)8397c478bd9Sstevel@tonic-gate graph_post_mus_online(void)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('3', 1);
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate static void
graph_post_mus_disable(void)8457c478bd9Sstevel@tonic-gate graph_post_mus_disable(void)
8467c478bd9Sstevel@tonic-gate {
8477c478bd9Sstevel@tonic-gate 	graph_runlevel_changed('3', 0);
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate static struct special_vertex_info {
8517c478bd9Sstevel@tonic-gate 	const char	*name;
8527c478bd9Sstevel@tonic-gate 	void		(*start_f)(graph_vertex_t *);
8537c478bd9Sstevel@tonic-gate 	void		(*post_online_f)(void);
8547c478bd9Sstevel@tonic-gate 	void		(*post_disable_f)(void);
8557c478bd9Sstevel@tonic-gate } special_vertices[] = {
8567c478bd9Sstevel@tonic-gate 	{ CONSOLE_LOGIN_FMRI, graph_clogin_start, NULL, NULL },
8577c478bd9Sstevel@tonic-gate 	{ SCF_MILESTONE_SINGLE_USER, graph_su_start,
8587c478bd9Sstevel@tonic-gate 	    graph_post_su_online, graph_post_su_disable },
8597c478bd9Sstevel@tonic-gate 	{ SCF_MILESTONE_MULTI_USER, NULL,
8607c478bd9Sstevel@tonic-gate 	    graph_post_mu_online, graph_post_mu_disable },
8617c478bd9Sstevel@tonic-gate 	{ SCF_MILESTONE_MULTI_USER_SERVER, NULL,
8627c478bd9Sstevel@tonic-gate 	    graph_post_mus_online, graph_post_mus_disable },
8637c478bd9Sstevel@tonic-gate 	{ NULL },
8647c478bd9Sstevel@tonic-gate };
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate void
vertex_send_event(graph_vertex_t * v,restarter_event_type_t e)8687c478bd9Sstevel@tonic-gate vertex_send_event(graph_vertex_t *v, restarter_event_type_t e)
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate 	switch (e) {
8717c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADD_INSTANCE:
8727c478bd9Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_UNINIT);
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&st->st_load_lock);
8757c478bd9Sstevel@tonic-gate 		st->st_load_instances++;
8767c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&st->st_load_lock);
8777c478bd9Sstevel@tonic-gate 		break;
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ENABLE:
8807c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Enabling %s.\n", v->gv_name);
8817c478bd9Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_UNINIT ||
8827c478bd9Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_DISABLED ||
8837c478bd9Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_MAINT);
8847c478bd9Sstevel@tonic-gate 		break;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_DISABLE:
8877c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
8887c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Disabling %s.\n", v->gv_name);
8897c478bd9Sstevel@tonic-gate 		assert(v->gv_state != RESTARTER_STATE_DISABLED);
8907c478bd9Sstevel@tonic-gate 		break;
8917c478bd9Sstevel@tonic-gate 
89216ba0facSSean Wilcox 	case RESTARTER_EVENT_TYPE_STOP_RESET:
8937c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_STOP:
8947c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Stopping %s.\n", v->gv_name);
8957c478bd9Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_DEGRADED ||
8967c478bd9Sstevel@tonic-gate 		    v->gv_state == RESTARTER_STATE_ONLINE);
8977c478bd9Sstevel@tonic-gate 		break;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_START:
9007c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Starting %s.\n", v->gv_name);
9017c478bd9Sstevel@tonic-gate 		assert(v->gv_state == RESTARTER_STATE_OFFLINE);
9027c478bd9Sstevel@tonic-gate 		break;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE:
905*56998286SAndrew Stormont 	case RESTARTER_EVENT_TYPE_ADMIN_RESTORE:
9067c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_DEGRADED:
907*56998286SAndrew Stormont 	case RESTARTER_EVENT_TYPE_ADMIN_DEGRADE_IMMEDIATE:
9087c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_REFRESH:
9097c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_RESTART:
9107c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF:
9117c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
9127c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE:
9137c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
9147c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
9157c478bd9Sstevel@tonic-gate 		break;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	default:
9187c478bd9Sstevel@tonic-gate #ifndef NDEBUG
9197c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Bad event %d.\n", __FILE__, __LINE__, e);
9207c478bd9Sstevel@tonic-gate #endif
9217c478bd9Sstevel@tonic-gate 		abort();
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 
924f6e214c7SGavin Maltby 	restarter_protocol_send_event(v->gv_name, v->gv_restarter_channel, e,
925f6e214c7SGavin Maltby 	    v->gv_reason);
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate static void
graph_unset_restarter(graph_vertex_t * v)9297c478bd9Sstevel@tonic-gate graph_unset_restarter(graph_vertex_t *v)
9307c478bd9Sstevel@tonic-gate {
93153f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
9327c478bd9Sstevel@tonic-gate 	assert(v->gv_flags & GV_CONFIGURED);
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_REMOVE_INSTANCE);
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	if (v->gv_restarter_id != -1) {
9377c478bd9Sstevel@tonic-gate 		graph_vertex_t *rv;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 		rv = vertex_get_by_id(v->gv_restarter_id);
9407c478bd9Sstevel@tonic-gate 		graph_remove_edge(v, rv);
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	v->gv_restarter_id = -1;
9447c478bd9Sstevel@tonic-gate 	v->gv_restarter_channel = NULL;
9457c478bd9Sstevel@tonic-gate }
9467c478bd9Sstevel@tonic-gate 
9473ad28c1eSrm88369 /*
9483ad28c1eSrm88369  * Return VERTEX_REMOVED when the vertex passed in argument is deleted from the
9493ad28c1eSrm88369  * dgraph otherwise return VERTEX_INUSE.
9503ad28c1eSrm88369  */
9513ad28c1eSrm88369 static int
free_if_unrefed(graph_vertex_t * v)9523ad28c1eSrm88369 free_if_unrefed(graph_vertex_t *v)
9533ad28c1eSrm88369 {
95453f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
9553ad28c1eSrm88369 
9563ad28c1eSrm88369 	if (v->gv_refs > 0)
9573ad28c1eSrm88369 		return (VERTEX_INUSE);
9583ad28c1eSrm88369 
9593ad28c1eSrm88369 	if (v->gv_type == GVT_SVC &&
9603ad28c1eSrm88369 	    uu_list_numnodes(v->gv_dependents) == 0 &&
9613ad28c1eSrm88369 	    uu_list_numnodes(v->gv_dependencies) == 0) {
9623ad28c1eSrm88369 		graph_remove_vertex(v);
9633ad28c1eSrm88369 		return (VERTEX_REMOVED);
9643ad28c1eSrm88369 	} else if (v->gv_type == GVT_INST &&
9653ad28c1eSrm88369 	    (v->gv_flags & GV_CONFIGURED) == 0 &&
9663ad28c1eSrm88369 	    uu_list_numnodes(v->gv_dependents) == 1 &&
9673ad28c1eSrm88369 	    uu_list_numnodes(v->gv_dependencies) == 0) {
9683ad28c1eSrm88369 		remove_inst_vertex(v);
9693ad28c1eSrm88369 		return (VERTEX_REMOVED);
9703ad28c1eSrm88369 	}
9713ad28c1eSrm88369 
9723ad28c1eSrm88369 	return (VERTEX_INUSE);
9733ad28c1eSrm88369 }
9743ad28c1eSrm88369 
9757c478bd9Sstevel@tonic-gate static void
delete_depgroup(graph_vertex_t * v)9767c478bd9Sstevel@tonic-gate delete_depgroup(graph_vertex_t *v)
9777c478bd9Sstevel@tonic-gate {
9787c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
9797c478bd9Sstevel@tonic-gate 	graph_vertex_t *dv;
9807c478bd9Sstevel@tonic-gate 
98153f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
9827c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_GROUP);
9837c478bd9Sstevel@tonic-gate 	assert(uu_list_numnodes(v->gv_dependents) == 0);
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	while ((e = uu_list_first(v->gv_dependencies)) != NULL) {
9867c478bd9Sstevel@tonic-gate 		dv = e->ge_vertex;
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 		graph_remove_edge(v, dv);
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 		switch (dv->gv_type) {
9917c478bd9Sstevel@tonic-gate 		case GVT_INST:		/* instance dependency */
9927c478bd9Sstevel@tonic-gate 		case GVT_SVC:		/* service dependency */
9933ad28c1eSrm88369 			(void) free_if_unrefed(dv);
9947c478bd9Sstevel@tonic-gate 			break;
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 		case GVT_FILE:		/* file dependency */
9977c478bd9Sstevel@tonic-gate 			assert(uu_list_numnodes(dv->gv_dependencies) == 0);
9987c478bd9Sstevel@tonic-gate 			if (uu_list_numnodes(dv->gv_dependents) == 0)
9997c478bd9Sstevel@tonic-gate 				graph_remove_vertex(dv);
10007c478bd9Sstevel@tonic-gate 			break;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 		default:
10037c478bd9Sstevel@tonic-gate #ifndef NDEBUG
10047c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected node type %d", __FILE__,
10057c478bd9Sstevel@tonic-gate 			    __LINE__, dv->gv_type);
10067c478bd9Sstevel@tonic-gate #endif
10077c478bd9Sstevel@tonic-gate 			abort();
10087c478bd9Sstevel@tonic-gate 		}
10097c478bd9Sstevel@tonic-gate 	}
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	graph_remove_vertex(v);
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate static int
delete_instance_deps_cb(graph_edge_t * e,void ** ptrs)10157c478bd9Sstevel@tonic-gate delete_instance_deps_cb(graph_edge_t *e, void **ptrs)
10167c478bd9Sstevel@tonic-gate {
10177c478bd9Sstevel@tonic-gate 	graph_vertex_t *v = ptrs[0];
10187c478bd9Sstevel@tonic-gate 	boolean_t delete_restarter_dep = (boolean_t)ptrs[1];
10197c478bd9Sstevel@tonic-gate 	graph_vertex_t *dv;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	dv = e->ge_vertex;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	/*
10247c478bd9Sstevel@tonic-gate 	 * We have four possibilities here:
10257c478bd9Sstevel@tonic-gate 	 *   - GVT_INST: restarter
10267c478bd9Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_INST: instance dependency
10277c478bd9Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_SVC - GV_INST: service dependency
10287c478bd9Sstevel@tonic-gate 	 *   - GVT_GROUP - GVT_FILE: file dependency
10297c478bd9Sstevel@tonic-gate 	 */
10307c478bd9Sstevel@tonic-gate 	switch (dv->gv_type) {
10317c478bd9Sstevel@tonic-gate 	case GVT_INST:	/* restarter */
10327c478bd9Sstevel@tonic-gate 		assert(dv->gv_id == v->gv_restarter_id);
10337c478bd9Sstevel@tonic-gate 		if (delete_restarter_dep)
10347c478bd9Sstevel@tonic-gate 			graph_remove_edge(v, dv);
10357c478bd9Sstevel@tonic-gate 		break;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	case GVT_GROUP:	/* pg dependency */
10387c478bd9Sstevel@tonic-gate 		graph_remove_edge(v, dv);
10397c478bd9Sstevel@tonic-gate 		delete_depgroup(dv);
10407c478bd9Sstevel@tonic-gate 		break;
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	case GVT_FILE:
10437c478bd9Sstevel@tonic-gate 		/* These are currently not direct dependencies */
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	default:
10467c478bd9Sstevel@tonic-gate #ifndef NDEBUG
10477c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Bad vertex type %d.\n", __FILE__, __LINE__,
10487c478bd9Sstevel@tonic-gate 		    dv->gv_type);
10497c478bd9Sstevel@tonic-gate #endif
10507c478bd9Sstevel@tonic-gate 		abort();
10517c478bd9Sstevel@tonic-gate 	}
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate static void
delete_instance_dependencies(graph_vertex_t * v,boolean_t delete_restarter_dep)10577c478bd9Sstevel@tonic-gate delete_instance_dependencies(graph_vertex_t *v, boolean_t delete_restarter_dep)
10587c478bd9Sstevel@tonic-gate {
10597c478bd9Sstevel@tonic-gate 	void *ptrs[2];
10607c478bd9Sstevel@tonic-gate 	int r;
10617c478bd9Sstevel@tonic-gate 
106253f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
10637c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	ptrs[0] = v;
10667c478bd9Sstevel@tonic-gate 	ptrs[1] = (void *)delete_restarter_dep;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	r = uu_list_walk(v->gv_dependencies,
10697c478bd9Sstevel@tonic-gate 	    (uu_walk_fn_t *)delete_instance_deps_cb, &ptrs, UU_WALK_ROBUST);
10707c478bd9Sstevel@tonic-gate 	assert(r == 0);
10717c478bd9Sstevel@tonic-gate }
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate /*
10747c478bd9Sstevel@tonic-gate  * int graph_insert_vertex_unconfigured()
10757c478bd9Sstevel@tonic-gate  *   Insert a vertex without sending any restarter events. If the vertex
10767c478bd9Sstevel@tonic-gate  *   already exists or creation is successful, return a pointer to it in *vp.
10777c478bd9Sstevel@tonic-gate  *
10787c478bd9Sstevel@tonic-gate  *   If type is not GVT_GROUP, dt can remain unset.
10797c478bd9Sstevel@tonic-gate  *
10807c478bd9Sstevel@tonic-gate  *   Returns 0, EEXIST, or EINVAL if the arguments are invalid (i.e., fmri
10817c478bd9Sstevel@tonic-gate  *   doesn't agree with type, or type doesn't agree with dt).
10827c478bd9Sstevel@tonic-gate  */
10837c478bd9Sstevel@tonic-gate static int
graph_insert_vertex_unconfigured(const char * fmri,gv_type_t type,depgroup_type_t dt,restarter_error_t rt,graph_vertex_t ** vp)10847c478bd9Sstevel@tonic-gate graph_insert_vertex_unconfigured(const char *fmri, gv_type_t type,
10857c478bd9Sstevel@tonic-gate     depgroup_type_t dt, restarter_error_t rt, graph_vertex_t **vp)
10867c478bd9Sstevel@tonic-gate {
10877c478bd9Sstevel@tonic-gate 	int r;
10887c478bd9Sstevel@tonic-gate 	int i;
10897c478bd9Sstevel@tonic-gate 
109053f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	switch (type) {
10937c478bd9Sstevel@tonic-gate 	case GVT_SVC:
10947c478bd9Sstevel@tonic-gate 	case GVT_INST:
10957c478bd9Sstevel@tonic-gate 		if (strncmp(fmri, "svc:", sizeof ("svc:") - 1) != 0)
10967c478bd9Sstevel@tonic-gate 			return (EINVAL);
10977c478bd9Sstevel@tonic-gate 		break;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	case GVT_FILE:
11007c478bd9Sstevel@tonic-gate 		if (strncmp(fmri, "file:", sizeof ("file:") - 1) != 0)
11017c478bd9Sstevel@tonic-gate 			return (EINVAL);
11027c478bd9Sstevel@tonic-gate 		break;
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	case GVT_GROUP:
11057c478bd9Sstevel@tonic-gate 		if (dt <= 0 || rt < 0)
11067c478bd9Sstevel@tonic-gate 			return (EINVAL);
11077c478bd9Sstevel@tonic-gate 		break;
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	default:
11107c478bd9Sstevel@tonic-gate #ifndef NDEBUG
11117c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown type %d.\n", __FILE__, __LINE__, type);
11127c478bd9Sstevel@tonic-gate #endif
11137c478bd9Sstevel@tonic-gate 		abort();
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	*vp = vertex_get_by_name(fmri);
11177c478bd9Sstevel@tonic-gate 	if (*vp != NULL)
11187c478bd9Sstevel@tonic-gate 		return (EEXIST);
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	*vp = graph_add_vertex(fmri);
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	(*vp)->gv_type = type;
11237c478bd9Sstevel@tonic-gate 	(*vp)->gv_depgroup = dt;
11247c478bd9Sstevel@tonic-gate 	(*vp)->gv_restart = rt;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	(*vp)->gv_flags = 0;
11277c478bd9Sstevel@tonic-gate 	(*vp)->gv_state = RESTARTER_STATE_NONE;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	for (i = 0; special_vertices[i].name != NULL; ++i) {
11307c478bd9Sstevel@tonic-gate 		if (strcmp(fmri, special_vertices[i].name) == 0) {
11317c478bd9Sstevel@tonic-gate 			(*vp)->gv_start_f = special_vertices[i].start_f;
11327c478bd9Sstevel@tonic-gate 			(*vp)->gv_post_online_f =
11337c478bd9Sstevel@tonic-gate 			    special_vertices[i].post_online_f;
11347c478bd9Sstevel@tonic-gate 			(*vp)->gv_post_disable_f =
11357c478bd9Sstevel@tonic-gate 			    special_vertices[i].post_disable_f;
11367c478bd9Sstevel@tonic-gate 			break;
11377c478bd9Sstevel@tonic-gate 		}
11387c478bd9Sstevel@tonic-gate 	}
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	(*vp)->gv_restarter_id = -1;
11417c478bd9Sstevel@tonic-gate 	(*vp)->gv_restarter_channel = 0;
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	if (type == GVT_INST) {
11447c478bd9Sstevel@tonic-gate 		char *sfmri;
11457c478bd9Sstevel@tonic-gate 		graph_vertex_t *sv;
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 		sfmri = inst_fmri_to_svc_fmri(fmri);
11487c478bd9Sstevel@tonic-gate 		sv = vertex_get_by_name(sfmri);
11497c478bd9Sstevel@tonic-gate 		if (sv == NULL) {
11507c478bd9Sstevel@tonic-gate 			r = graph_insert_vertex_unconfigured(sfmri, GVT_SVC, 0,
11517c478bd9Sstevel@tonic-gate 			    0, &sv);
11527c478bd9Sstevel@tonic-gate 			assert(r == 0);
11537c478bd9Sstevel@tonic-gate 		}
11547c478bd9Sstevel@tonic-gate 		startd_free(sfmri, max_scf_fmri_size);
11557c478bd9Sstevel@tonic-gate 
11567c478bd9Sstevel@tonic-gate 		graph_add_edge(sv, *vp);
11577c478bd9Sstevel@tonic-gate 	}
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	/*
11607c478bd9Sstevel@tonic-gate 	 * If this vertex is in the subgraph, mark it as so, for both
11617c478bd9Sstevel@tonic-gate 	 * GVT_INST and GVT_SERVICE verteces.
11627c478bd9Sstevel@tonic-gate 	 * A GVT_SERVICE vertex can only be in the subgraph if another instance
11637c478bd9Sstevel@tonic-gate 	 * depends on it, in which case it's already been added to the graph
11647c478bd9Sstevel@tonic-gate 	 * and marked as in the subgraph (by refresh_vertex()).  If a
11657c478bd9Sstevel@tonic-gate 	 * GVT_SERVICE vertex was freshly added (by the code above), it means
11667c478bd9Sstevel@tonic-gate 	 * that it has no dependents, and cannot be in the subgraph.
11677c478bd9Sstevel@tonic-gate 	 * Regardless of this, we still check that gv_flags includes
11687c478bd9Sstevel@tonic-gate 	 * GV_INSUBGRAPH in the event that future behavior causes the above
11697c478bd9Sstevel@tonic-gate 	 * code to add a GVT_SERVICE vertex which should be in the subgraph.
11707c478bd9Sstevel@tonic-gate 	 */
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	(*vp)->gv_flags |= (should_be_in_subgraph(*vp)? GV_INSUBGRAPH : 0);
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	return (0);
11757c478bd9Sstevel@tonic-gate }
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate /*
11787c478bd9Sstevel@tonic-gate  * Returns 0 on success or ELOOP if the dependency would create a cycle.
11797c478bd9Sstevel@tonic-gate  */
11807c478bd9Sstevel@tonic-gate static int
graph_insert_dependency(graph_vertex_t * fv,graph_vertex_t * tv,int ** pathp)11817c478bd9Sstevel@tonic-gate graph_insert_dependency(graph_vertex_t *fv, graph_vertex_t *tv, int **pathp)
11827c478bd9Sstevel@tonic-gate {
11837c478bd9Sstevel@tonic-gate 	hrtime_t now;
11847c478bd9Sstevel@tonic-gate 
118553f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	/* cycle detection */
11887c478bd9Sstevel@tonic-gate 	now = gethrtime();
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	/* Don't follow exclusions. */
11917c478bd9Sstevel@tonic-gate 	if (!(fv->gv_type == GVT_GROUP &&
11927c478bd9Sstevel@tonic-gate 	    fv->gv_depgroup == DEPGRP_EXCLUDE_ALL)) {
11937c478bd9Sstevel@tonic-gate 		*pathp = is_path_to(tv, fv);
11947c478bd9Sstevel@tonic-gate 		if (*pathp)
11957c478bd9Sstevel@tonic-gate 			return (ELOOP);
11967c478bd9Sstevel@tonic-gate 	}
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	dep_cycle_ns += gethrtime() - now;
11997c478bd9Sstevel@tonic-gate 	++dep_inserts;
12007c478bd9Sstevel@tonic-gate 	now = gethrtime();
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	graph_add_edge(fv, tv);
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	dep_insert_ns += gethrtime() - now;
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	/* Check if the dependency adds the "to" vertex to the subgraph */
12077c478bd9Sstevel@tonic-gate 	tv->gv_flags |= (should_be_in_subgraph(tv) ? GV_INSUBGRAPH : 0);
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 	return (0);
12107c478bd9Sstevel@tonic-gate }
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate static int
inst_running(graph_vertex_t * v)12137c478bd9Sstevel@tonic-gate inst_running(graph_vertex_t *v)
12147c478bd9Sstevel@tonic-gate {
12157c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	if (v->gv_state == RESTARTER_STATE_ONLINE ||
12187c478bd9Sstevel@tonic-gate 	    v->gv_state == RESTARTER_STATE_DEGRADED)
12197c478bd9Sstevel@tonic-gate 		return (1);
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	return (0);
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate /*
12257c478bd9Sstevel@tonic-gate  * The dependency evaluation functions return
12267c478bd9Sstevel@tonic-gate  *   1 - dependency satisfied
12277c478bd9Sstevel@tonic-gate  *   0 - dependency unsatisfied
12287c478bd9Sstevel@tonic-gate  *   -1 - dependency unsatisfiable (without administrator intervention)
12297c478bd9Sstevel@tonic-gate  *
12307c478bd9Sstevel@tonic-gate  * The functions also take a boolean satbility argument.  When true, the
12317c478bd9Sstevel@tonic-gate  * functions may recurse in order to determine satisfiability.
12327c478bd9Sstevel@tonic-gate  */
12337c478bd9Sstevel@tonic-gate static int require_any_satisfied(graph_vertex_t *, boolean_t);
12347c478bd9Sstevel@tonic-gate static int dependency_satisfied(graph_vertex_t *, boolean_t);
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate /*
12377c478bd9Sstevel@tonic-gate  * A require_all dependency is unsatisfied if any elements are unsatisfied.  It
12387c478bd9Sstevel@tonic-gate  * is unsatisfiable if any elements are unsatisfiable.
12397c478bd9Sstevel@tonic-gate  */
12407c478bd9Sstevel@tonic-gate static int
require_all_satisfied(graph_vertex_t * groupv,boolean_t satbility)12417c478bd9Sstevel@tonic-gate require_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
12427c478bd9Sstevel@tonic-gate {
12437c478bd9Sstevel@tonic-gate 	graph_edge_t *edge;
12447c478bd9Sstevel@tonic-gate 	int i;
12457c478bd9Sstevel@tonic-gate 	boolean_t any_unsatisfied;
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	if (uu_list_numnodes(groupv->gv_dependencies) == 0)
12487c478bd9Sstevel@tonic-gate 		return (1);
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	any_unsatisfied = B_FALSE;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
12537c478bd9Sstevel@tonic-gate 	    edge != NULL;
12547c478bd9Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
12557c478bd9Sstevel@tonic-gate 		i = dependency_satisfied(edge->ge_vertex, satbility);
12567c478bd9Sstevel@tonic-gate 		if (i == 1)
12577c478bd9Sstevel@tonic-gate 			continue;
12587c478bd9Sstevel@tonic-gate 
12593efb42ecSrm88369 		log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
12607c478bd9Sstevel@tonic-gate 		    "require_all(%s): %s is unsatisfi%s.\n", groupv->gv_name,
12617c478bd9Sstevel@tonic-gate 		    edge->ge_vertex->gv_name, i == 0 ? "ed" : "able");
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 		if (!satbility)
12647c478bd9Sstevel@tonic-gate 			return (0);
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 		if (i == -1)
12677c478bd9Sstevel@tonic-gate 			return (-1);
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 		any_unsatisfied = B_TRUE;
12707c478bd9Sstevel@tonic-gate 	}
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	return (any_unsatisfied ? 0 : 1);
12737c478bd9Sstevel@tonic-gate }
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate /*
12767c478bd9Sstevel@tonic-gate  * A require_any dependency is satisfied if any element is satisfied.  It is
12777c478bd9Sstevel@tonic-gate  * satisfiable if any element is satisfiable.
12787c478bd9Sstevel@tonic-gate  */
12797c478bd9Sstevel@tonic-gate static int
require_any_satisfied(graph_vertex_t * groupv,boolean_t satbility)12807c478bd9Sstevel@tonic-gate require_any_satisfied(graph_vertex_t *groupv, boolean_t satbility)
12817c478bd9Sstevel@tonic-gate {
12827c478bd9Sstevel@tonic-gate 	graph_edge_t *edge;
12837c478bd9Sstevel@tonic-gate 	int s;
12847c478bd9Sstevel@tonic-gate 	boolean_t satisfiable;
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	if (uu_list_numnodes(groupv->gv_dependencies) == 0)
12877c478bd9Sstevel@tonic-gate 		return (1);
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	satisfiable = B_FALSE;
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
12927c478bd9Sstevel@tonic-gate 	    edge != NULL;
12937c478bd9Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
12947c478bd9Sstevel@tonic-gate 		s = dependency_satisfied(edge->ge_vertex, satbility);
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 		if (s == 1)
12977c478bd9Sstevel@tonic-gate 			return (1);
12987c478bd9Sstevel@tonic-gate 
12993efb42ecSrm88369 		log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
13007c478bd9Sstevel@tonic-gate 		    "require_any(%s): %s is unsatisfi%s.\n",
13017c478bd9Sstevel@tonic-gate 		    groupv->gv_name, edge->ge_vertex->gv_name,
13027c478bd9Sstevel@tonic-gate 		    s == 0 ? "ed" : "able");
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 		if (satbility && s == 0)
13057c478bd9Sstevel@tonic-gate 			satisfiable = B_TRUE;
13067c478bd9Sstevel@tonic-gate 	}
13077c478bd9Sstevel@tonic-gate 
13083e207980SAndrew Stormont 	return ((!satbility || satisfiable) ? 0 : -1);
13097c478bd9Sstevel@tonic-gate }
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate /*
13127c478bd9Sstevel@tonic-gate  * An optional_all dependency only considers elements which are configured,
13137c478bd9Sstevel@tonic-gate  * enabled, and not in maintenance.  If any are unsatisfied, then the dependency
13147c478bd9Sstevel@tonic-gate  * is unsatisfied.
13157c478bd9Sstevel@tonic-gate  *
13167c478bd9Sstevel@tonic-gate  * Offline dependencies which are waiting for a dependency to come online are
13177c478bd9Sstevel@tonic-gate  * unsatisfied.  Offline dependences which cannot possibly come online
13187c478bd9Sstevel@tonic-gate  * (unsatisfiable) are always considered satisfied.
13197c478bd9Sstevel@tonic-gate  */
13207c478bd9Sstevel@tonic-gate static int
optional_all_satisfied(graph_vertex_t * groupv,boolean_t satbility)13217c478bd9Sstevel@tonic-gate optional_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
13227c478bd9Sstevel@tonic-gate {
13237c478bd9Sstevel@tonic-gate 	graph_edge_t *edge;
13247c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
13257c478bd9Sstevel@tonic-gate 	boolean_t any_qualified;
13267c478bd9Sstevel@tonic-gate 	boolean_t any_unsatisfied;
13277c478bd9Sstevel@tonic-gate 	int i;
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	any_qualified = B_FALSE;
13307c478bd9Sstevel@tonic-gate 	any_unsatisfied = B_FALSE;
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
13337c478bd9Sstevel@tonic-gate 	    edge != NULL;
13347c478bd9Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
13357c478bd9Sstevel@tonic-gate 		v = edge->ge_vertex;
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 		switch (v->gv_type) {
13387c478bd9Sstevel@tonic-gate 		case GVT_INST:
13393e207980SAndrew Stormont 			/* Skip missing instances */
13403e207980SAndrew Stormont 			if ((v->gv_flags & GV_CONFIGURED) == 0)
13417c478bd9Sstevel@tonic-gate 				continue;
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 			if (v->gv_state == RESTARTER_STATE_MAINT)
13447c478bd9Sstevel@tonic-gate 				continue;
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 			any_qualified = B_TRUE;
13473e207980SAndrew Stormont 			if (v->gv_state == RESTARTER_STATE_OFFLINE ||
13483e207980SAndrew Stormont 			    v->gv_state == RESTARTER_STATE_DISABLED) {
13497c478bd9Sstevel@tonic-gate 				/*
13503e207980SAndrew Stormont 				 * For offline/disabled dependencies,
13513e207980SAndrew Stormont 				 * treat unsatisfiable as satisfied.
13527c478bd9Sstevel@tonic-gate 				 */
13537c478bd9Sstevel@tonic-gate 				i = dependency_satisfied(v, B_TRUE);
13547c478bd9Sstevel@tonic-gate 				if (i == -1)
13557c478bd9Sstevel@tonic-gate 					i = 1;
13567c478bd9Sstevel@tonic-gate 			} else {
13577c478bd9Sstevel@tonic-gate 				i = dependency_satisfied(v, satbility);
13587c478bd9Sstevel@tonic-gate 			}
13597c478bd9Sstevel@tonic-gate 			break;
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 		case GVT_FILE:
13627c478bd9Sstevel@tonic-gate 			any_qualified = B_TRUE;
13637c478bd9Sstevel@tonic-gate 			i = dependency_satisfied(v, satbility);
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 			break;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 		case GVT_SVC: {
13684e567863SRobert Mustacchi 			any_qualified = B_TRUE;
13693e207980SAndrew Stormont 			i = optional_all_satisfied(v, satbility);
13703e207980SAndrew Stormont 
13717c478bd9Sstevel@tonic-gate 			break;
13727c478bd9Sstevel@tonic-gate 		}
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 		case GVT_GROUP:
13757c478bd9Sstevel@tonic-gate 		default:
13767c478bd9Sstevel@tonic-gate #ifndef NDEBUG
13777c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
13787c478bd9Sstevel@tonic-gate 			    __LINE__, v->gv_type);
13797c478bd9Sstevel@tonic-gate #endif
13807c478bd9Sstevel@tonic-gate 			abort();
13817c478bd9Sstevel@tonic-gate 		}
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 		if (i == 1)
13847c478bd9Sstevel@tonic-gate 			continue;
13857c478bd9Sstevel@tonic-gate 
13863efb42ecSrm88369 		log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
13877c478bd9Sstevel@tonic-gate 		    "optional_all(%s): %s is unsatisfi%s.\n", groupv->gv_name,
13887c478bd9Sstevel@tonic-gate 		    v->gv_name, i == 0 ? "ed" : "able");
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 		if (!satbility)
13917c478bd9Sstevel@tonic-gate 			return (0);
13927c478bd9Sstevel@tonic-gate 		if (i == -1)
13937c478bd9Sstevel@tonic-gate 			return (-1);
13947c478bd9Sstevel@tonic-gate 		any_unsatisfied = B_TRUE;
13957c478bd9Sstevel@tonic-gate 	}
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	if (!any_qualified)
13987c478bd9Sstevel@tonic-gate 		return (1);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	return (any_unsatisfied ? 0 : 1);
14017c478bd9Sstevel@tonic-gate }
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate /*
14047c478bd9Sstevel@tonic-gate  * An exclude_all dependency is unsatisfied if any non-service element is
14057c478bd9Sstevel@tonic-gate  * satisfied or any service instance which is configured, enabled, and not in
14067c478bd9Sstevel@tonic-gate  * maintenance is satisfied.  Usually when unsatisfied, it is also
14077c478bd9Sstevel@tonic-gate  * unsatisfiable.
14087c478bd9Sstevel@tonic-gate  */
14097c478bd9Sstevel@tonic-gate #define	LOG_EXCLUDE(u, v)						\
14103efb42ecSrm88369 	log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,			\
14113efb42ecSrm88369 	    "exclude_all(%s): %s is satisfied.\n",			\
14127c478bd9Sstevel@tonic-gate 	    (u)->gv_name, (v)->gv_name)
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate /* ARGSUSED */
14157c478bd9Sstevel@tonic-gate static int
exclude_all_satisfied(graph_vertex_t * groupv,boolean_t satbility)14167c478bd9Sstevel@tonic-gate exclude_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
14177c478bd9Sstevel@tonic-gate {
14187c478bd9Sstevel@tonic-gate 	graph_edge_t *edge, *e2;
14197c478bd9Sstevel@tonic-gate 	graph_vertex_t *v, *v2;
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	for (edge = uu_list_first(groupv->gv_dependencies);
14227c478bd9Sstevel@tonic-gate 	    edge != NULL;
14237c478bd9Sstevel@tonic-gate 	    edge = uu_list_next(groupv->gv_dependencies, edge)) {
14247c478bd9Sstevel@tonic-gate 		v = edge->ge_vertex;
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 		switch (v->gv_type) {
14277c478bd9Sstevel@tonic-gate 		case GVT_INST:
14287c478bd9Sstevel@tonic-gate 			if ((v->gv_flags & GV_CONFIGURED) == 0)
14297c478bd9Sstevel@tonic-gate 				continue;
14307c478bd9Sstevel@tonic-gate 
14317c478bd9Sstevel@tonic-gate 			switch (v->gv_state) {
14327c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_ONLINE:
14337c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_DEGRADED:
14347c478bd9Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v);
14357c478bd9Sstevel@tonic-gate 				return (v->gv_flags & GV_ENABLED ? -1 : 0);
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_OFFLINE:
14387c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_UNINIT:
14397c478bd9Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v);
14407c478bd9Sstevel@tonic-gate 				return (0);
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_DISABLED:
14437c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_MAINT:
14447c478bd9Sstevel@tonic-gate 				continue;
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 			default:
14477c478bd9Sstevel@tonic-gate #ifndef NDEBUG
14487c478bd9Sstevel@tonic-gate 				uu_warn("%s:%d: Unexpected vertex state %d.\n",
14497c478bd9Sstevel@tonic-gate 				    __FILE__, __LINE__, v->gv_state);
14507c478bd9Sstevel@tonic-gate #endif
14517c478bd9Sstevel@tonic-gate 				abort();
14527c478bd9Sstevel@tonic-gate 			}
14537c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 		case GVT_SVC:
14567c478bd9Sstevel@tonic-gate 			break;
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 		case GVT_FILE:
14597c478bd9Sstevel@tonic-gate 			if (!file_ready(v))
14607c478bd9Sstevel@tonic-gate 				continue;
14617c478bd9Sstevel@tonic-gate 			LOG_EXCLUDE(groupv, v);
14627c478bd9Sstevel@tonic-gate 			return (-1);
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 		case GVT_GROUP:
14657c478bd9Sstevel@tonic-gate 		default:
14667c478bd9Sstevel@tonic-gate #ifndef NDEBUG
14677c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
14687c478bd9Sstevel@tonic-gate 			    __LINE__, v->gv_type);
14697c478bd9Sstevel@tonic-gate #endif
14707c478bd9Sstevel@tonic-gate 			abort();
14717c478bd9Sstevel@tonic-gate 		}
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 		/* v represents a service */
14747c478bd9Sstevel@tonic-gate 		if (uu_list_numnodes(v->gv_dependencies) == 0)
14757c478bd9Sstevel@tonic-gate 			continue;
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 		for (e2 = uu_list_first(v->gv_dependencies);
14787c478bd9Sstevel@tonic-gate 		    e2 != NULL;
14797c478bd9Sstevel@tonic-gate 		    e2 = uu_list_next(v->gv_dependencies, e2)) {
14807c478bd9Sstevel@tonic-gate 			v2 = e2->ge_vertex;
14817c478bd9Sstevel@tonic-gate 			assert(v2->gv_type == GVT_INST);
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 			if ((v2->gv_flags & GV_CONFIGURED) == 0)
14847c478bd9Sstevel@tonic-gate 				continue;
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 			switch (v2->gv_state) {
14877c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_ONLINE:
14887c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_DEGRADED:
14897c478bd9Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v2);
14907c478bd9Sstevel@tonic-gate 				return (v2->gv_flags & GV_ENABLED ? -1 : 0);
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_OFFLINE:
14937c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_UNINIT:
14947c478bd9Sstevel@tonic-gate 				LOG_EXCLUDE(groupv, v2);
14957c478bd9Sstevel@tonic-gate 				return (0);
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_DISABLED:
14987c478bd9Sstevel@tonic-gate 			case RESTARTER_STATE_MAINT:
14997c478bd9Sstevel@tonic-gate 				continue;
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 			default:
15027c478bd9Sstevel@tonic-gate #ifndef NDEBUG
15037c478bd9Sstevel@tonic-gate 				uu_warn("%s:%d: Unexpected vertex type %d.\n",
15047c478bd9Sstevel@tonic-gate 				    __FILE__, __LINE__, v2->gv_type);
15057c478bd9Sstevel@tonic-gate #endif
15067c478bd9Sstevel@tonic-gate 				abort();
15077c478bd9Sstevel@tonic-gate 			}
15087c478bd9Sstevel@tonic-gate 		}
15097c478bd9Sstevel@tonic-gate 	}
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	return (1);
15127c478bd9Sstevel@tonic-gate }
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate /*
15157c478bd9Sstevel@tonic-gate  * int instance_satisfied()
15167c478bd9Sstevel@tonic-gate  *   Determine if all the dependencies are satisfied for the supplied instance
15177c478bd9Sstevel@tonic-gate  *   vertex. Return 1 if they are, 0 if they aren't, and -1 if they won't be
15187c478bd9Sstevel@tonic-gate  *   without administrator intervention.
15197c478bd9Sstevel@tonic-gate  */
15207c478bd9Sstevel@tonic-gate static int
instance_satisfied(graph_vertex_t * v,boolean_t satbility)15217c478bd9Sstevel@tonic-gate instance_satisfied(graph_vertex_t *v, boolean_t satbility)
15227c478bd9Sstevel@tonic-gate {
15237c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
15247c478bd9Sstevel@tonic-gate 	assert(!inst_running(v));
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	return (require_all_satisfied(v, satbility));
15277c478bd9Sstevel@tonic-gate }
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate /*
15307c478bd9Sstevel@tonic-gate  * Decide whether v can satisfy a dependency.  v can either be a child of
15317c478bd9Sstevel@tonic-gate  * a group vertex, or of an instance vertex.
15327c478bd9Sstevel@tonic-gate  */
15337c478bd9Sstevel@tonic-gate static int
dependency_satisfied(graph_vertex_t * v,boolean_t satbility)15347c478bd9Sstevel@tonic-gate dependency_satisfied(graph_vertex_t *v, boolean_t satbility)
15357c478bd9Sstevel@tonic-gate {
15367c478bd9Sstevel@tonic-gate 	switch (v->gv_type) {
15377c478bd9Sstevel@tonic-gate 	case GVT_INST:
153870cbfe41SPhilippe Jung 		if ((v->gv_flags & GV_CONFIGURED) == 0) {
153970cbfe41SPhilippe Jung 			if (v->gv_flags & GV_DEATHROW) {
154070cbfe41SPhilippe Jung 				/*
154170cbfe41SPhilippe Jung 				 * A dependency on an instance with GV_DEATHROW
154270cbfe41SPhilippe Jung 				 * flag is always considered as satisfied.
154370cbfe41SPhilippe Jung 				 */
154470cbfe41SPhilippe Jung 				return (1);
154570cbfe41SPhilippe Jung 			}
15467c478bd9Sstevel@tonic-gate 			return (-1);
154770cbfe41SPhilippe Jung 		}
15487c478bd9Sstevel@tonic-gate 
1549207246e9SRenaud Manus 		/*
15503e207980SAndrew Stormont 		 * Vertices may be transitioning so we try to figure out if
15513e207980SAndrew Stormont 		 * the end state is likely to satisfy the dependency instead
15523e207980SAndrew Stormont 		 * of assuming the dependency is unsatisfied/unsatisfiable.
15533e207980SAndrew Stormont 		 *
15543e207980SAndrew Stormont 		 * Support for optional_all dependencies depends on us getting
15553e207980SAndrew Stormont 		 * this right because unsatisfiable dependencies are treated
15563e207980SAndrew Stormont 		 * as being satisfied.
1557207246e9SRenaud Manus 		 */
15587c478bd9Sstevel@tonic-gate 		switch (v->gv_state) {
15597c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_ONLINE:
15607c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_DEGRADED:
15613e207980SAndrew Stormont 			if (v->gv_flags & GV_TODISABLE)
15623e207980SAndrew Stormont 				return (-1);
15633e207980SAndrew Stormont 			if (v->gv_flags & GV_TOOFFLINE)
15643e207980SAndrew Stormont 				return (0);
15657c478bd9Sstevel@tonic-gate 			return (1);
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_OFFLINE:
15683e207980SAndrew Stormont 			if (!satbility || v->gv_flags & GV_TODISABLE)
15693e207980SAndrew Stormont 				return (satbility ? -1 : 0);
15707c478bd9Sstevel@tonic-gate 			return (instance_satisfied(v, satbility) != -1 ?
15717c478bd9Sstevel@tonic-gate 			    0 : -1);
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_DISABLED:
15743e207980SAndrew Stormont 			if (!satbility || !(v->gv_flags & GV_ENABLED))
15753e207980SAndrew Stormont 				return (satbility ? -1 : 0);
15763e207980SAndrew Stormont 			return (instance_satisfied(v, satbility) != -1 ?
15773e207980SAndrew Stormont 			    0 : -1);
15783e207980SAndrew Stormont 
15797c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_MAINT:
15807c478bd9Sstevel@tonic-gate 			return (-1);
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_UNINIT:
15837c478bd9Sstevel@tonic-gate 			return (0);
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 		default:
15867c478bd9Sstevel@tonic-gate #ifndef NDEBUG
15877c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex state %d.\n",
15887c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__, v->gv_state);
15897c478bd9Sstevel@tonic-gate #endif
15907c478bd9Sstevel@tonic-gate 			abort();
15917c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
15927c478bd9Sstevel@tonic-gate 		}
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	case GVT_SVC:
15957c478bd9Sstevel@tonic-gate 		if (uu_list_numnodes(v->gv_dependencies) == 0)
15967c478bd9Sstevel@tonic-gate 			return (-1);
15977c478bd9Sstevel@tonic-gate 		return (require_any_satisfied(v, satbility));
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 	case GVT_FILE:
16007c478bd9Sstevel@tonic-gate 		/* i.e., we assume files will not be automatically generated */
16017c478bd9Sstevel@tonic-gate 		return (file_ready(v) ? 1 : -1);
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	case GVT_GROUP:
16047c478bd9Sstevel@tonic-gate 		break;
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	default:
16077c478bd9Sstevel@tonic-gate #ifndef NDEBUG
16087c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unexpected node type %d.\n", __FILE__, __LINE__,
16097c478bd9Sstevel@tonic-gate 		    v->gv_type);
16107c478bd9Sstevel@tonic-gate #endif
16117c478bd9Sstevel@tonic-gate 		abort();
16127c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
16137c478bd9Sstevel@tonic-gate 	}
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	switch (v->gv_depgroup) {
16167c478bd9Sstevel@tonic-gate 	case DEPGRP_REQUIRE_ANY:
16177c478bd9Sstevel@tonic-gate 		return (require_any_satisfied(v, satbility));
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	case DEPGRP_REQUIRE_ALL:
16207c478bd9Sstevel@tonic-gate 		return (require_all_satisfied(v, satbility));
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	case DEPGRP_OPTIONAL_ALL:
16237c478bd9Sstevel@tonic-gate 		return (optional_all_satisfied(v, satbility));
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 	case DEPGRP_EXCLUDE_ALL:
16267c478bd9Sstevel@tonic-gate 		return (exclude_all_satisfied(v, satbility));
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 	default:
16297c478bd9Sstevel@tonic-gate #ifndef NDEBUG
16307c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown dependency grouping %d.\n", __FILE__,
16317c478bd9Sstevel@tonic-gate 		    __LINE__, v->gv_depgroup);
16327c478bd9Sstevel@tonic-gate #endif
16337c478bd9Sstevel@tonic-gate 		abort();
16347c478bd9Sstevel@tonic-gate 	}
16357c478bd9Sstevel@tonic-gate }
16367c478bd9Sstevel@tonic-gate 
163799b44c3bSlianep void
graph_start_if_satisfied(graph_vertex_t * v)163899b44c3bSlianep graph_start_if_satisfied(graph_vertex_t *v)
16397c478bd9Sstevel@tonic-gate {
16407c478bd9Sstevel@tonic-gate 	if (v->gv_state == RESTARTER_STATE_OFFLINE &&
16417c478bd9Sstevel@tonic-gate 	    instance_satisfied(v, B_FALSE) == 1) {
16427c478bd9Sstevel@tonic-gate 		if (v->gv_start_f == NULL)
16437c478bd9Sstevel@tonic-gate 			vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
16447c478bd9Sstevel@tonic-gate 		else
16457c478bd9Sstevel@tonic-gate 			v->gv_start_f(v);
16467c478bd9Sstevel@tonic-gate 	}
16477c478bd9Sstevel@tonic-gate }
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate /*
16507c478bd9Sstevel@tonic-gate  * propagate_satbility()
16517c478bd9Sstevel@tonic-gate  *
16527c478bd9Sstevel@tonic-gate  * This function is used when the given vertex changes state in such a way that
16537c478bd9Sstevel@tonic-gate  * one of its dependents may become unsatisfiable.  This happens when an
16547c478bd9Sstevel@tonic-gate  * instance transitions between offline -> online, or from !running ->
16557c478bd9Sstevel@tonic-gate  * maintenance, as well as when an instance is removed from the graph.
16567c478bd9Sstevel@tonic-gate  *
1657cd3bce3eSlianep  * We have to walk all the dependents, since optional_all dependencies several
16587c478bd9Sstevel@tonic-gate  * levels up could become (un)satisfied, instead of unsatisfiable.  For example,
16597c478bd9Sstevel@tonic-gate  *
16607c478bd9Sstevel@tonic-gate  *	+-----+  optional_all  +-----+  require_all  +-----+
16617c478bd9Sstevel@tonic-gate  *	|  A  |--------------->|  B  |-------------->|  C  |
16627c478bd9Sstevel@tonic-gate  *	+-----+                +-----+               +-----+
16637c478bd9Sstevel@tonic-gate  *
16647c478bd9Sstevel@tonic-gate  *	                                        offline -> maintenance
16657c478bd9Sstevel@tonic-gate  *
16667c478bd9Sstevel@tonic-gate  * If C goes into maintenance, it's not enough simply to check B.  Because A has
16677c478bd9Sstevel@tonic-gate  * an optional dependency, what was previously an unsatisfiable situation is now
16687c478bd9Sstevel@tonic-gate  * satisfied (B will never come online, even though its state hasn't changed).
16697c478bd9Sstevel@tonic-gate  *
16707c478bd9Sstevel@tonic-gate  * Note that it's not necessary to continue examining dependents after reaching
16717c478bd9Sstevel@tonic-gate  * an optional_all dependency.  It's not possible for an optional_all dependency
16727c478bd9Sstevel@tonic-gate  * to change satisfiability without also coming online, in which case we get a
16737c478bd9Sstevel@tonic-gate  * start event and propagation continues naturally.  However, it does no harm to
16747c478bd9Sstevel@tonic-gate  * continue propagating satisfiability (as it is a relatively rare event), and
16757c478bd9Sstevel@tonic-gate  * keeps the walker code simple and generic.
16767c478bd9Sstevel@tonic-gate  */
16777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16787c478bd9Sstevel@tonic-gate static int
satbility_cb(graph_vertex_t * v,void * arg)16797c478bd9Sstevel@tonic-gate satbility_cb(graph_vertex_t *v, void *arg)
16807c478bd9Sstevel@tonic-gate {
16813e207980SAndrew Stormont 	if (is_inst_bypassed(v))
16823e207980SAndrew Stormont 		return (UU_WALK_NEXT);
16833e207980SAndrew Stormont 
16847c478bd9Sstevel@tonic-gate 	if (v->gv_type == GVT_INST)
168599b44c3bSlianep 		graph_start_if_satisfied(v);
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
16887c478bd9Sstevel@tonic-gate }
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate static void
propagate_satbility(graph_vertex_t * v)16917c478bd9Sstevel@tonic-gate propagate_satbility(graph_vertex_t *v)
16927c478bd9Sstevel@tonic-gate {
16937c478bd9Sstevel@tonic-gate 	graph_walk(v, WALK_DEPENDENTS, satbility_cb, NULL, NULL);
16947c478bd9Sstevel@tonic-gate }
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate static void propagate_stop(graph_vertex_t *, void *);
16977c478bd9Sstevel@tonic-gate 
16983e207980SAndrew Stormont /*
16993e207980SAndrew Stormont  * propagate_start()
17003e207980SAndrew Stormont  *
17013e207980SAndrew Stormont  * This function is used to propagate a start event to the dependents of the
17023e207980SAndrew Stormont  * given vertex.  Any dependents that are offline but have their dependencies
17033e207980SAndrew Stormont  * satisfied are started.  Any dependents that are online and have restart_on
17043e207980SAndrew Stormont  * set to "restart" or "refresh" are restarted because their dependencies have
17053e207980SAndrew Stormont  * just changed.  This only happens with optional_all dependencies.
17063e207980SAndrew Stormont  */
17077c478bd9Sstevel@tonic-gate static void
propagate_start(graph_vertex_t * v,void * arg)17087c478bd9Sstevel@tonic-gate propagate_start(graph_vertex_t *v, void *arg)
17097c478bd9Sstevel@tonic-gate {
17103e207980SAndrew Stormont 	restarter_error_t err = (restarter_error_t)arg;
17113e207980SAndrew Stormont 
17123e207980SAndrew Stormont 	if (is_inst_bypassed(v))
17133e207980SAndrew Stormont 		return;
17143e207980SAndrew Stormont 
17157c478bd9Sstevel@tonic-gate 	switch (v->gv_type) {
17167c478bd9Sstevel@tonic-gate 	case GVT_INST:
17173e207980SAndrew Stormont 		/* Restarter */
17183e207980SAndrew Stormont 		if (inst_running(v)) {
17193e207980SAndrew Stormont 			if (err == RERR_RESTART || err == RERR_REFRESH) {
17203e207980SAndrew Stormont 				vertex_send_event(v,
17213e207980SAndrew Stormont 				    RESTARTER_EVENT_TYPE_STOP_RESET);
17223e207980SAndrew Stormont 			}
17233e207980SAndrew Stormont 		} else {
172499b44c3bSlianep 			graph_start_if_satisfied(v);
17253e207980SAndrew Stormont 		}
17267c478bd9Sstevel@tonic-gate 		break;
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	case GVT_GROUP:
17297c478bd9Sstevel@tonic-gate 		if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
17307c478bd9Sstevel@tonic-gate 			graph_walk_dependents(v, propagate_stop,
17317c478bd9Sstevel@tonic-gate 			    (void *)RERR_RESTART);
17327c478bd9Sstevel@tonic-gate 			break;
17337c478bd9Sstevel@tonic-gate 		}
17343e207980SAndrew Stormont 		err = v->gv_restart;
17357c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 	case GVT_SVC:
17383e207980SAndrew Stormont 		graph_walk_dependents(v, propagate_start, (void *)err);
17397c478bd9Sstevel@tonic-gate 		break;
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 	case GVT_FILE:
17427c478bd9Sstevel@tonic-gate #ifndef NDEBUG
17437c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: propagate_start() encountered GVT_FILE.\n",
17447c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__);
17457c478bd9Sstevel@tonic-gate #endif
17467c478bd9Sstevel@tonic-gate 		abort();
17477c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	default:
17507c478bd9Sstevel@tonic-gate #ifndef NDEBUG
17517c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__,
17527c478bd9Sstevel@tonic-gate 		    v->gv_type);
17537c478bd9Sstevel@tonic-gate #endif
17547c478bd9Sstevel@tonic-gate 		abort();
17557c478bd9Sstevel@tonic-gate 	}
17567c478bd9Sstevel@tonic-gate }
17577c478bd9Sstevel@tonic-gate 
17583e207980SAndrew Stormont /*
17593e207980SAndrew Stormont  * propagate_stop()
17603e207980SAndrew Stormont  *
17613e207980SAndrew Stormont  * This function is used to propagate a stop event to the dependents of the
17623e207980SAndrew Stormont  * given vertex.  Any dependents that are online (or in degraded state) with
17633e207980SAndrew Stormont  * the restart_on property set to "restart" or "refresh" will be stopped as
17643e207980SAndrew Stormont  * their dependencies have just changed, propagate_start() will start them
17653e207980SAndrew Stormont  * again once their dependencies have been re-satisfied.
17663e207980SAndrew Stormont  */
17677c478bd9Sstevel@tonic-gate static void
propagate_stop(graph_vertex_t * v,void * arg)17687c478bd9Sstevel@tonic-gate propagate_stop(graph_vertex_t *v, void *arg)
17697c478bd9Sstevel@tonic-gate {
17707c478bd9Sstevel@tonic-gate 	restarter_error_t err = (restarter_error_t)arg;
17717c478bd9Sstevel@tonic-gate 
17723e207980SAndrew Stormont 	if (is_inst_bypassed(v))
17733e207980SAndrew Stormont 		return;
17743e207980SAndrew Stormont 
17757c478bd9Sstevel@tonic-gate 	switch (v->gv_type) {
17767c478bd9Sstevel@tonic-gate 	case GVT_INST:
17777c478bd9Sstevel@tonic-gate 		/* Restarter */
177816ba0facSSean Wilcox 		if (err > RERR_NONE && inst_running(v)) {
1779c238c833SSean Wilcox 			if (err == RERR_RESTART || err == RERR_REFRESH) {
178016ba0facSSean Wilcox 				vertex_send_event(v,
178116ba0facSSean Wilcox 				    RESTARTER_EVENT_TYPE_STOP_RESET);
178216ba0facSSean Wilcox 			} else {
17837c478bd9Sstevel@tonic-gate 				vertex_send_event(v, RESTARTER_EVENT_TYPE_STOP);
178416ba0facSSean Wilcox 			}
178516ba0facSSean Wilcox 		}
17867c478bd9Sstevel@tonic-gate 		break;
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	case GVT_SVC:
17897c478bd9Sstevel@tonic-gate 		graph_walk_dependents(v, propagate_stop, arg);
17907c478bd9Sstevel@tonic-gate 		break;
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	case GVT_FILE:
17937c478bd9Sstevel@tonic-gate #ifndef NDEBUG
17947c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: propagate_stop() encountered GVT_FILE.\n",
17957c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__);
17967c478bd9Sstevel@tonic-gate #endif
17977c478bd9Sstevel@tonic-gate 		abort();
17987c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	case GVT_GROUP:
18017c478bd9Sstevel@tonic-gate 		if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
18023e207980SAndrew Stormont 			graph_walk_dependents(v, propagate_start,
18033e207980SAndrew Stormont 			    (void *)RERR_NONE);
18047c478bd9Sstevel@tonic-gate 			break;
18057c478bd9Sstevel@tonic-gate 		}
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate 		if (err == RERR_NONE || err > v->gv_restart)
18087c478bd9Sstevel@tonic-gate 			break;
18097c478bd9Sstevel@tonic-gate 
18103e207980SAndrew Stormont 		graph_walk_dependents(v, propagate_stop, arg);
18117c478bd9Sstevel@tonic-gate 		break;
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 	default:
18147c478bd9Sstevel@tonic-gate #ifndef NDEBUG
18157c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__,
18167c478bd9Sstevel@tonic-gate 		    v->gv_type);
18177c478bd9Sstevel@tonic-gate #endif
18187c478bd9Sstevel@tonic-gate 		abort();
18197c478bd9Sstevel@tonic-gate 	}
18207c478bd9Sstevel@tonic-gate }
18217c478bd9Sstevel@tonic-gate 
1822845e9415SRenaud Manus void
offline_vertex(graph_vertex_t * v)1823aca380d7SRenaud Manus offline_vertex(graph_vertex_t *v)
1824aca380d7SRenaud Manus {
1825aca380d7SRenaud Manus 	scf_handle_t *h = libscf_handle_create_bound_loop();
1826aca380d7SRenaud Manus 	scf_instance_t *scf_inst = safe_scf_instance_create(h);
1827aca380d7SRenaud Manus 	scf_propertygroup_t *pg = safe_scf_pg_create(h);
1828aca380d7SRenaud Manus 	restarter_instance_state_t state, next_state;
1829aca380d7SRenaud Manus 	int r;
1830aca380d7SRenaud Manus 
1831aca380d7SRenaud Manus 	assert(v->gv_type == GVT_INST);
1832aca380d7SRenaud Manus 
1833aca380d7SRenaud Manus 	if (scf_inst == NULL)
1834aca380d7SRenaud Manus 		bad_error("safe_scf_instance_create", scf_error());
1835aca380d7SRenaud Manus 	if (pg == NULL)
1836aca380d7SRenaud Manus 		bad_error("safe_scf_pg_create", scf_error());
1837aca380d7SRenaud Manus 
1838aca380d7SRenaud Manus 	/* if the vertex is already going offline, return */
1839aca380d7SRenaud Manus rep_retry:
1840aca380d7SRenaud Manus 	if (scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, scf_inst, NULL,
1841aca380d7SRenaud Manus 	    NULL, SCF_DECODE_FMRI_EXACT) != 0) {
1842aca380d7SRenaud Manus 		switch (scf_error()) {
1843aca380d7SRenaud Manus 		case SCF_ERROR_CONNECTION_BROKEN:
1844aca380d7SRenaud Manus 			libscf_handle_rebind(h);
1845aca380d7SRenaud Manus 			goto rep_retry;
1846aca380d7SRenaud Manus 
1847aca380d7SRenaud Manus 		case SCF_ERROR_NOT_FOUND:
1848aca380d7SRenaud Manus 			scf_pg_destroy(pg);
1849aca380d7SRenaud Manus 			scf_instance_destroy(scf_inst);
1850aca380d7SRenaud Manus 			(void) scf_handle_unbind(h);
1851aca380d7SRenaud Manus 			scf_handle_destroy(h);
1852aca380d7SRenaud Manus 			return;
1853aca380d7SRenaud Manus 		}
1854aca380d7SRenaud Manus 		uu_die("Can't decode FMRI %s: %s\n", v->gv_name,
1855aca380d7SRenaud Manus 		    scf_strerror(scf_error()));
1856aca380d7SRenaud Manus 	}
1857aca380d7SRenaud Manus 
1858aca380d7SRenaud Manus 	r = scf_instance_get_pg(scf_inst, SCF_PG_RESTARTER, pg);
1859aca380d7SRenaud Manus 	if (r != 0) {
1860aca380d7SRenaud Manus 		switch (scf_error()) {
1861aca380d7SRenaud Manus 		case SCF_ERROR_CONNECTION_BROKEN:
1862aca380d7SRenaud Manus 			libscf_handle_rebind(h);
1863aca380d7SRenaud Manus 			goto rep_retry;
1864aca380d7SRenaud Manus 
1865aca380d7SRenaud Manus 		case SCF_ERROR_NOT_SET:
1866aca380d7SRenaud Manus 		case SCF_ERROR_NOT_FOUND:
1867aca380d7SRenaud Manus 			scf_pg_destroy(pg);
1868aca380d7SRenaud Manus 			scf_instance_destroy(scf_inst);
1869aca380d7SRenaud Manus 			(void) scf_handle_unbind(h);
1870aca380d7SRenaud Manus 			scf_handle_destroy(h);
1871aca380d7SRenaud Manus 			return;
1872aca380d7SRenaud Manus 
1873aca380d7SRenaud Manus 		default:
1874aca380d7SRenaud Manus 			bad_error("scf_instance_get_pg", scf_error());
1875aca380d7SRenaud Manus 		}
1876aca380d7SRenaud Manus 	} else {
1877aca380d7SRenaud Manus 		r = libscf_read_states(pg, &state, &next_state);
1878aca380d7SRenaud Manus 		if (r == 0 && (next_state == RESTARTER_STATE_OFFLINE ||
1879aca380d7SRenaud Manus 		    next_state == RESTARTER_STATE_DISABLED)) {
1880aca380d7SRenaud Manus 			log_framework(LOG_DEBUG,
1881aca380d7SRenaud Manus 			    "%s: instance is already going down.\n",
1882aca380d7SRenaud Manus 			    v->gv_name);
1883aca380d7SRenaud Manus 			scf_pg_destroy(pg);
1884aca380d7SRenaud Manus 			scf_instance_destroy(scf_inst);
1885aca380d7SRenaud Manus 			(void) scf_handle_unbind(h);
1886aca380d7SRenaud Manus 			scf_handle_destroy(h);
1887aca380d7SRenaud Manus 			return;
1888aca380d7SRenaud Manus 		}
1889aca380d7SRenaud Manus 	}
1890aca380d7SRenaud Manus 
1891aca380d7SRenaud Manus 	scf_pg_destroy(pg);
1892aca380d7SRenaud Manus 	scf_instance_destroy(scf_inst);
1893aca380d7SRenaud Manus 	(void) scf_handle_unbind(h);
1894aca380d7SRenaud Manus 	scf_handle_destroy(h);
1895accdf946SRenaud Manus 
189616ba0facSSean Wilcox 	vertex_send_event(v, RESTARTER_EVENT_TYPE_STOP_RESET);
1897aca380d7SRenaud Manus }
1898aca380d7SRenaud Manus 
18997c478bd9Sstevel@tonic-gate /*
19007c478bd9Sstevel@tonic-gate  * void graph_enable_by_vertex()
19017c478bd9Sstevel@tonic-gate  *   If admin is non-zero, this is an administrative request for change
19027c478bd9Sstevel@tonic-gate  *   of the enabled property.  Thus, send the ADMIN_DISABLE rather than
19037c478bd9Sstevel@tonic-gate  *   a plain DISABLE restarter event.
19047c478bd9Sstevel@tonic-gate  */
190599b44c3bSlianep void
graph_enable_by_vertex(graph_vertex_t * vertex,int enable,int admin)19067c478bd9Sstevel@tonic-gate graph_enable_by_vertex(graph_vertex_t *vertex, int enable, int admin)
19077c478bd9Sstevel@tonic-gate {
1908aca380d7SRenaud Manus 	graph_vertex_t *v;
1909aca380d7SRenaud Manus 	int r;
1910aca380d7SRenaud Manus 
191153f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
19127c478bd9Sstevel@tonic-gate 	assert((vertex->gv_flags & GV_CONFIGURED));
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	vertex->gv_flags = (vertex->gv_flags & ~GV_ENABLED) |
19157c478bd9Sstevel@tonic-gate 	    (enable ? GV_ENABLED : 0);
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	if (enable) {
19187c478bd9Sstevel@tonic-gate 		if (vertex->gv_state != RESTARTER_STATE_OFFLINE &&
19197c478bd9Sstevel@tonic-gate 		    vertex->gv_state != RESTARTER_STATE_DEGRADED &&
1920aca380d7SRenaud Manus 		    vertex->gv_state != RESTARTER_STATE_ONLINE) {
1921aca380d7SRenaud Manus 			/*
1922aca380d7SRenaud Manus 			 * In case the vertex was notified to go down,
1923aca380d7SRenaud Manus 			 * but now can return online, clear the _TOOFFLINE
1924aca380d7SRenaud Manus 			 * and _TODISABLE flags.
1925aca380d7SRenaud Manus 			 */
1926aca380d7SRenaud Manus 			vertex->gv_flags &= ~GV_TOOFFLINE;
1927aca380d7SRenaud Manus 			vertex->gv_flags &= ~GV_TODISABLE;
1928aca380d7SRenaud Manus 
19297c478bd9Sstevel@tonic-gate 			vertex_send_event(vertex, RESTARTER_EVENT_TYPE_ENABLE);
19307c478bd9Sstevel@tonic-gate 		}
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 		/*
19337c478bd9Sstevel@tonic-gate 		 * Wait for state update from restarter before sending _START or
19347c478bd9Sstevel@tonic-gate 		 * _STOP.
19357c478bd9Sstevel@tonic-gate 		 */
1936aca380d7SRenaud Manus 
1937aca380d7SRenaud Manus 		return;
1938aca380d7SRenaud Manus 	}
1939aca380d7SRenaud Manus 
1940aca380d7SRenaud Manus 	if (vertex->gv_state == RESTARTER_STATE_DISABLED)
1941aca380d7SRenaud Manus 		return;
1942aca380d7SRenaud Manus 
1943aca380d7SRenaud Manus 	if (!admin) {
1944aca380d7SRenaud Manus 		vertex_send_event(vertex, RESTARTER_EVENT_TYPE_DISABLE);
1945aca380d7SRenaud Manus 
1946aca380d7SRenaud Manus 		/*
1947aca380d7SRenaud Manus 		 * Wait for state update from restarter before sending _START or
1948aca380d7SRenaud Manus 		 * _STOP.
1949aca380d7SRenaud Manus 		 */
1950aca380d7SRenaud Manus 
1951aca380d7SRenaud Manus 		return;
1952aca380d7SRenaud Manus 	}
1953aca380d7SRenaud Manus 
1954aca380d7SRenaud Manus 	/*
1955aca380d7SRenaud Manus 	 * If it is a DISABLE event requested by the administrator then we are
1956aca380d7SRenaud Manus 	 * offlining the dependents first.
1957aca380d7SRenaud Manus 	 */
1958aca380d7SRenaud Manus 
1959aca380d7SRenaud Manus 	/*
1960aca380d7SRenaud Manus 	 * Set GV_TOOFFLINE for the services we are offlining. We cannot
1961aca380d7SRenaud Manus 	 * clear the GV_TOOFFLINE bits from all the services because
1962aca380d7SRenaud Manus 	 * other DISABLE events might be handled at the same time.
1963aca380d7SRenaud Manus 	 */
1964aca380d7SRenaud Manus 	vertex->gv_flags |= GV_TOOFFLINE;
1965aca380d7SRenaud Manus 
1966aca380d7SRenaud Manus 	/* remember which vertex to disable... */
1967aca380d7SRenaud Manus 	vertex->gv_flags |= GV_TODISABLE;
1968aca380d7SRenaud Manus 
1969207246e9SRenaud Manus 	log_framework(LOG_DEBUG, "Marking in-subtree vertices before "
1970207246e9SRenaud Manus 	    "disabling %s.\n", vertex->gv_name);
1971207246e9SRenaud Manus 
1972aca380d7SRenaud Manus 	/* set GV_TOOFFLINE for its dependents */
1973aca380d7SRenaud Manus 	r = uu_list_walk(vertex->gv_dependents, (uu_walk_fn_t *)mark_subtree,
1974aca380d7SRenaud Manus 	    NULL, 0);
1975aca380d7SRenaud Manus 	assert(r == 0);
1976aca380d7SRenaud Manus 
1977aca380d7SRenaud Manus 	/* disable the instance now if there is nothing else to offline */
1978aca380d7SRenaud Manus 	if (insubtree_dependents_down(vertex) == B_TRUE) {
1979aca380d7SRenaud Manus 		vertex_send_event(vertex, RESTARTER_EVENT_TYPE_ADMIN_DISABLE);
1980aca380d7SRenaud Manus 		return;
1981aca380d7SRenaud Manus 	}
1982aca380d7SRenaud Manus 
1983aca380d7SRenaud Manus 	/*
1984aca380d7SRenaud Manus 	 * This loop is similar to the one used for the graph reversal shutdown
1985aca380d7SRenaud Manus 	 * and could be improved in term of performance for the subtree reversal
1986aca380d7SRenaud Manus 	 * disable case.
1987aca380d7SRenaud Manus 	 */
1988aca380d7SRenaud Manus 	for (v = uu_list_first(dgraph); v != NULL;
1989aca380d7SRenaud Manus 	    v = uu_list_next(dgraph, v)) {
1990aca380d7SRenaud Manus 		/* skip the vertex we are disabling for now */
1991aca380d7SRenaud Manus 		if (v == vertex)
1992aca380d7SRenaud Manus 			continue;
1993aca380d7SRenaud Manus 
1994aca380d7SRenaud Manus 		if (v->gv_type != GVT_INST ||
1995aca380d7SRenaud Manus 		    (v->gv_flags & GV_CONFIGURED) == 0 ||
1996aca380d7SRenaud Manus 		    (v->gv_flags & GV_ENABLED) == 0 ||
1997aca380d7SRenaud Manus 		    (v->gv_flags & GV_TOOFFLINE) == 0)
1998aca380d7SRenaud Manus 			continue;
1999aca380d7SRenaud Manus 
2000aca380d7SRenaud Manus 		if ((v->gv_state != RESTARTER_STATE_ONLINE) &&
2001aca380d7SRenaud Manus 		    (v->gv_state != RESTARTER_STATE_DEGRADED)) {
2002aca380d7SRenaud Manus 			/* continue if there is nothing to offline */
2003aca380d7SRenaud Manus 			continue;
2004aca380d7SRenaud Manus 		}
2005aca380d7SRenaud Manus 
2006aca380d7SRenaud Manus 		/*
2007aca380d7SRenaud Manus 		 * Instances which are up need to come down before we're
2008aca380d7SRenaud Manus 		 * done, but we can only offline the leaves here. An
2009aca380d7SRenaud Manus 		 * instance is a leaf when all its dependents are down.
2010aca380d7SRenaud Manus 		 */
2011207246e9SRenaud Manus 		if (insubtree_dependents_down(v) == B_TRUE) {
2012207246e9SRenaud Manus 			log_framework(LOG_DEBUG, "Offlining in-subtree "
2013207246e9SRenaud Manus 			    "instance %s for %s.\n",
2014207246e9SRenaud Manus 			    v->gv_name, vertex->gv_name);
2015aca380d7SRenaud Manus 			offline_vertex(v);
2016aca380d7SRenaud Manus 		}
20177c478bd9Sstevel@tonic-gate 	}
2018207246e9SRenaud Manus }
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate static int configure_vertex(graph_vertex_t *, scf_instance_t *);
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate /*
20237c478bd9Sstevel@tonic-gate  * Set the restarter for v to fmri_arg.  That is, make sure a vertex for
20247c478bd9Sstevel@tonic-gate  * fmri_arg exists, make v depend on it, and send _ADD_INSTANCE for v.  If
20257c478bd9Sstevel@tonic-gate  * v is already configured and fmri_arg indicates the current restarter, do
20267c478bd9Sstevel@tonic-gate  * nothing.  If v is configured and fmri_arg is a new restarter, delete v's
20277c478bd9Sstevel@tonic-gate  * dependency on the restarter, send _REMOVE_INSTANCE for v, and set the new
20287c478bd9Sstevel@tonic-gate  * restarter.  Returns 0 on success, EINVAL if the FMRI is invalid,
20297c478bd9Sstevel@tonic-gate  * ECONNABORTED if the repository connection is broken, and ELOOP
20307c478bd9Sstevel@tonic-gate  * if the dependency would create a cycle.  In the last case, *pathp will
20317c478bd9Sstevel@tonic-gate  * point to a -1-terminated array of ids which compose the path from v to
20327c478bd9Sstevel@tonic-gate  * restarter_fmri.
20337c478bd9Sstevel@tonic-gate  */
20347c478bd9Sstevel@tonic-gate int
graph_change_restarter(graph_vertex_t * v,const char * fmri_arg,scf_handle_t * h,int ** pathp)20357c478bd9Sstevel@tonic-gate graph_change_restarter(graph_vertex_t *v, const char *fmri_arg, scf_handle_t *h,
20367c478bd9Sstevel@tonic-gate     int **pathp)
20377c478bd9Sstevel@tonic-gate {
20387c478bd9Sstevel@tonic-gate 	char *restarter_fmri = NULL;
20397c478bd9Sstevel@tonic-gate 	graph_vertex_t *rv;
20407c478bd9Sstevel@tonic-gate 	int err;
20417c478bd9Sstevel@tonic-gate 	int id;
20427c478bd9Sstevel@tonic-gate 
204353f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	if (fmri_arg[0] != '\0') {
20467c478bd9Sstevel@tonic-gate 		err = fmri_canonify(fmri_arg, &restarter_fmri, B_TRUE);
20477c478bd9Sstevel@tonic-gate 		if (err != 0) {
20487c478bd9Sstevel@tonic-gate 			assert(err == EINVAL);
20497c478bd9Sstevel@tonic-gate 			return (err);
20507c478bd9Sstevel@tonic-gate 		}
20517c478bd9Sstevel@tonic-gate 	}
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	if (restarter_fmri == NULL ||
20547c478bd9Sstevel@tonic-gate 	    strcmp(restarter_fmri, SCF_SERVICE_STARTD) == 0) {
20557c478bd9Sstevel@tonic-gate 		if (v->gv_flags & GV_CONFIGURED) {
20567c478bd9Sstevel@tonic-gate 			if (v->gv_restarter_id == -1) {
20577c478bd9Sstevel@tonic-gate 				if (restarter_fmri != NULL)
20587c478bd9Sstevel@tonic-gate 					startd_free(restarter_fmri,
20597c478bd9Sstevel@tonic-gate 					    max_scf_fmri_size);
20607c478bd9Sstevel@tonic-gate 				return (0);
20617c478bd9Sstevel@tonic-gate 			}
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 			graph_unset_restarter(v);
20647c478bd9Sstevel@tonic-gate 		}
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 		/* Master restarter, nothing to do. */
20677c478bd9Sstevel@tonic-gate 		v->gv_restarter_id = -1;
20687c478bd9Sstevel@tonic-gate 		v->gv_restarter_channel = NULL;
20697c478bd9Sstevel@tonic-gate 		vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE);
20707c478bd9Sstevel@tonic-gate 		return (0);
20717c478bd9Sstevel@tonic-gate 	}
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate 	if (v->gv_flags & GV_CONFIGURED) {
20747c478bd9Sstevel@tonic-gate 		id = dict_lookup_byname(restarter_fmri);
20757c478bd9Sstevel@tonic-gate 		if (id != -1 && v->gv_restarter_id == id) {
20767c478bd9Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_fmri_size);
20777c478bd9Sstevel@tonic-gate 			return (0);
20787c478bd9Sstevel@tonic-gate 		}
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 		graph_unset_restarter(v);
20817c478bd9Sstevel@tonic-gate 	}
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	err = graph_insert_vertex_unconfigured(restarter_fmri, GVT_INST, 0,
20847c478bd9Sstevel@tonic-gate 	    RERR_NONE, &rv);
20857c478bd9Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_fmri_size);
20867c478bd9Sstevel@tonic-gate 	assert(err == 0 || err == EEXIST);
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 	if (rv->gv_delegate_initialized == 0) {
208913d8aaa1SSean Wilcox 		if ((rv->gv_delegate_channel = restarter_protocol_init_delegate(
209013d8aaa1SSean Wilcox 		    rv->gv_name)) == NULL)
209113d8aaa1SSean Wilcox 			return (EINVAL);
20927c478bd9Sstevel@tonic-gate 		rv->gv_delegate_initialized = 1;
20937c478bd9Sstevel@tonic-gate 	}
20947c478bd9Sstevel@tonic-gate 	v->gv_restarter_id = rv->gv_id;
20957c478bd9Sstevel@tonic-gate 	v->gv_restarter_channel = rv->gv_delegate_channel;
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	err = graph_insert_dependency(v, rv, pathp);
20987c478bd9Sstevel@tonic-gate 	if (err != 0) {
20997c478bd9Sstevel@tonic-gate 		assert(err == ELOOP);
21007c478bd9Sstevel@tonic-gate 		return (ELOOP);
21017c478bd9Sstevel@tonic-gate 	}
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE);
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	if (!(rv->gv_flags & GV_CONFIGURED)) {
21067c478bd9Sstevel@tonic-gate 		scf_instance_t *inst;
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 		err = libscf_fmri_get_instance(h, rv->gv_name, &inst);
21097c478bd9Sstevel@tonic-gate 		switch (err) {
21107c478bd9Sstevel@tonic-gate 		case 0:
21117c478bd9Sstevel@tonic-gate 			err = configure_vertex(rv, inst);
21127c478bd9Sstevel@tonic-gate 			scf_instance_destroy(inst);
21137c478bd9Sstevel@tonic-gate 			switch (err) {
21147c478bd9Sstevel@tonic-gate 			case 0:
21157c478bd9Sstevel@tonic-gate 			case ECANCELED:
21167c478bd9Sstevel@tonic-gate 				break;
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
21197c478bd9Sstevel@tonic-gate 				return (ECONNABORTED);
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 			default:
21227c478bd9Sstevel@tonic-gate 				bad_error("configure_vertex", err);
21237c478bd9Sstevel@tonic-gate 			}
21247c478bd9Sstevel@tonic-gate 			break;
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
21277c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 		case ENOENT:
21307c478bd9Sstevel@tonic-gate 			break;
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 		case ENOTSUP:
21337c478bd9Sstevel@tonic-gate 			/*
21347c478bd9Sstevel@tonic-gate 			 * The fmri doesn't specify an instance - translate
21357c478bd9Sstevel@tonic-gate 			 * to EINVAL.
21367c478bd9Sstevel@tonic-gate 			 */
21377c478bd9Sstevel@tonic-gate 			return (EINVAL);
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 		case EINVAL:
21407c478bd9Sstevel@tonic-gate 		default:
21417c478bd9Sstevel@tonic-gate 			bad_error("libscf_fmri_get_instance", err);
21427c478bd9Sstevel@tonic-gate 		}
21437c478bd9Sstevel@tonic-gate 	}
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate 	return (0);
21467c478bd9Sstevel@tonic-gate }
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate /*
21507c478bd9Sstevel@tonic-gate  * Add all of the instances of the service named by fmri to the graph.
21517c478bd9Sstevel@tonic-gate  * Returns
21527c478bd9Sstevel@tonic-gate  *   0 - success
21537c478bd9Sstevel@tonic-gate  *   ENOENT - service indicated by fmri does not exist
21547c478bd9Sstevel@tonic-gate  *
21557c478bd9Sstevel@tonic-gate  * In both cases *reboundp will be B_TRUE if the handle was rebound, or B_FALSE
21567c478bd9Sstevel@tonic-gate  * otherwise.
21577c478bd9Sstevel@tonic-gate  */
21587c478bd9Sstevel@tonic-gate static int
add_service(const char * fmri,scf_handle_t * h,boolean_t * reboundp)21597c478bd9Sstevel@tonic-gate add_service(const char *fmri, scf_handle_t *h, boolean_t *reboundp)
21607c478bd9Sstevel@tonic-gate {
21617c478bd9Sstevel@tonic-gate 	scf_service_t *svc;
21627c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
21637c478bd9Sstevel@tonic-gate 	scf_iter_t *iter;
21647c478bd9Sstevel@tonic-gate 	char *inst_fmri;
21657c478bd9Sstevel@tonic-gate 	int ret, r;
21667c478bd9Sstevel@tonic-gate 
21677c478bd9Sstevel@tonic-gate 	*reboundp = B_FALSE;
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 	svc = safe_scf_service_create(h);
21707c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
21717c478bd9Sstevel@tonic-gate 	iter = safe_scf_iter_create(h);
21727c478bd9Sstevel@tonic-gate 	inst_fmri = startd_alloc(max_scf_fmri_size);
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate rebound:
21757c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
21767c478bd9Sstevel@tonic-gate 	    SCF_DECODE_FMRI_EXACT) != 0) {
21777c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
21787c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
21797c478bd9Sstevel@tonic-gate 		default:
21807c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
21817c478bd9Sstevel@tonic-gate 			*reboundp = B_TRUE;
21827c478bd9Sstevel@tonic-gate 			goto rebound;
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
21857c478bd9Sstevel@tonic-gate 			ret = ENOENT;
21867c478bd9Sstevel@tonic-gate 			goto out;
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
21897c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
21907c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
21917c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
21927c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
21937c478bd9Sstevel@tonic-gate 		}
21947c478bd9Sstevel@tonic-gate 	}
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 	if (scf_iter_service_instances(iter, svc) != 0) {
21977c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
21987c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
21997c478bd9Sstevel@tonic-gate 		default:
22007c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
22017c478bd9Sstevel@tonic-gate 			*reboundp = B_TRUE;
22027c478bd9Sstevel@tonic-gate 			goto rebound;
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
22057c478bd9Sstevel@tonic-gate 			ret = ENOENT;
22067c478bd9Sstevel@tonic-gate 			goto out;
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
22097c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
22107c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
22113eae19d9Swesolows 			bad_error("scf_iter_service_instances", scf_error());
22127c478bd9Sstevel@tonic-gate 		}
22137c478bd9Sstevel@tonic-gate 	}
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 	for (;;) {
22167c478bd9Sstevel@tonic-gate 		r = scf_iter_next_instance(iter, inst);
22177c478bd9Sstevel@tonic-gate 		if (r == 0)
22187c478bd9Sstevel@tonic-gate 			break;
22197c478bd9Sstevel@tonic-gate 		if (r != 1) {
22207c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
22217c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
22227c478bd9Sstevel@tonic-gate 			default:
22237c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
22247c478bd9Sstevel@tonic-gate 				*reboundp = B_TRUE;
22257c478bd9Sstevel@tonic-gate 				goto rebound;
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
22287c478bd9Sstevel@tonic-gate 				ret = ENOENT;
22297c478bd9Sstevel@tonic-gate 				goto out;
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
22327c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
22337c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
22347c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
22357c478bd9Sstevel@tonic-gate 				bad_error("scf_iter_next_instance",
22367c478bd9Sstevel@tonic-gate 				    scf_error());
22377c478bd9Sstevel@tonic-gate 			}
22387c478bd9Sstevel@tonic-gate 		}
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate 		if (scf_instance_to_fmri(inst, inst_fmri, max_scf_fmri_size) <
22417c478bd9Sstevel@tonic-gate 		    0) {
22427c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
22437c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
22447c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
22457c478bd9Sstevel@tonic-gate 				*reboundp = B_TRUE;
22467c478bd9Sstevel@tonic-gate 				goto rebound;
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
22497c478bd9Sstevel@tonic-gate 				continue;
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
22527c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
22537c478bd9Sstevel@tonic-gate 				bad_error("scf_instance_to_fmri", scf_error());
22547c478bd9Sstevel@tonic-gate 			}
22557c478bd9Sstevel@tonic-gate 		}
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 		r = dgraph_add_instance(inst_fmri, inst, B_FALSE);
22587c478bd9Sstevel@tonic-gate 		switch (r) {
22597c478bd9Sstevel@tonic-gate 		case 0:
22607c478bd9Sstevel@tonic-gate 		case ECANCELED:
22617c478bd9Sstevel@tonic-gate 			break;
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 		case EEXIST:
22647c478bd9Sstevel@tonic-gate 			continue;
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
22677c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
22687c478bd9Sstevel@tonic-gate 			*reboundp = B_TRUE;
22697c478bd9Sstevel@tonic-gate 			goto rebound;
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 		case EINVAL:
22727c478bd9Sstevel@tonic-gate 		default:
22737c478bd9Sstevel@tonic-gate 			bad_error("dgraph_add_instance", r);
22747c478bd9Sstevel@tonic-gate 		}
22757c478bd9Sstevel@tonic-gate 	}
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 	ret = 0;
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate out:
22807c478bd9Sstevel@tonic-gate 	startd_free(inst_fmri, max_scf_fmri_size);
22817c478bd9Sstevel@tonic-gate 	scf_iter_destroy(iter);
22827c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
22837c478bd9Sstevel@tonic-gate 	scf_service_destroy(svc);
22847c478bd9Sstevel@tonic-gate 	return (ret);
22857c478bd9Sstevel@tonic-gate }
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate struct depfmri_info {
22887c478bd9Sstevel@tonic-gate 	graph_vertex_t	*v;		/* GVT_GROUP vertex */
22897c478bd9Sstevel@tonic-gate 	gv_type_t	type;		/* type of dependency */
22907c478bd9Sstevel@tonic-gate 	const char	*inst_fmri;	/* FMRI of parental GVT_INST vert. */
22917c478bd9Sstevel@tonic-gate 	const char	*pg_name;	/* Name of dependency pg */
22927c478bd9Sstevel@tonic-gate 	scf_handle_t	*h;
22937c478bd9Sstevel@tonic-gate 	int		err;		/* return error code */
22947c478bd9Sstevel@tonic-gate 	int		**pathp;	/* return circular dependency path */
22957c478bd9Sstevel@tonic-gate };
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate /*
22987c478bd9Sstevel@tonic-gate  * Find or create a vertex for fmri and make info->v depend on it.
22997c478bd9Sstevel@tonic-gate  * Returns
23007c478bd9Sstevel@tonic-gate  *   0 - success
23017c478bd9Sstevel@tonic-gate  *   nonzero - failure
23027c478bd9Sstevel@tonic-gate  *
23037c478bd9Sstevel@tonic-gate  * On failure, sets info->err to
23047c478bd9Sstevel@tonic-gate  *   EINVAL - fmri is invalid
23057c478bd9Sstevel@tonic-gate  *	      fmri does not match info->type
23067c478bd9Sstevel@tonic-gate  *   ELOOP - Adding the dependency creates a circular dependency.  *info->pathp
23077c478bd9Sstevel@tonic-gate  *	     will point to an array of the ids of the members of the cycle.
23087c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection was broken
23097c478bd9Sstevel@tonic-gate  *   ECONNRESET - succeeded, but repository connection was reset
23107c478bd9Sstevel@tonic-gate  */
23117c478bd9Sstevel@tonic-gate static int
process_dependency_fmri(const char * fmri,struct depfmri_info * info)23127c478bd9Sstevel@tonic-gate process_dependency_fmri(const char *fmri, struct depfmri_info *info)
23137c478bd9Sstevel@tonic-gate {
23147c478bd9Sstevel@tonic-gate 	int err;
23157c478bd9Sstevel@tonic-gate 	graph_vertex_t *depgroup_v, *v;
23167c478bd9Sstevel@tonic-gate 	char *fmri_copy, *cfmri;
23177c478bd9Sstevel@tonic-gate 	size_t fmri_copy_sz;
23187c478bd9Sstevel@tonic-gate 	const char *scope, *service, *instance, *pg;
23197c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
23207c478bd9Sstevel@tonic-gate 	boolean_t rebound;
23217c478bd9Sstevel@tonic-gate 
232253f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	/* Get or create vertex for FMRI */
23257c478bd9Sstevel@tonic-gate 	depgroup_v = info->v;
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 	if (strncmp(fmri, "file:", sizeof ("file:") - 1) == 0) {
23287c478bd9Sstevel@tonic-gate 		if (info->type != GVT_FILE) {
23297c478bd9Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
23307c478bd9Sstevel@tonic-gate 			    "FMRI \"%s\" is not allowed for the \"%s\" "
23317c478bd9Sstevel@tonic-gate 			    "dependency's type of instance %s.\n", fmri,
23327c478bd9Sstevel@tonic-gate 			    info->pg_name, info->inst_fmri);
23337c478bd9Sstevel@tonic-gate 			return (info->err = EINVAL);
23347c478bd9Sstevel@tonic-gate 		}
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(fmri, info->type, 0,
23377c478bd9Sstevel@tonic-gate 		    RERR_NONE, &v);
23387c478bd9Sstevel@tonic-gate 		switch (err) {
23397c478bd9Sstevel@tonic-gate 		case 0:
23407c478bd9Sstevel@tonic-gate 			break;
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 		case EEXIST:
23437c478bd9Sstevel@tonic-gate 			assert(v->gv_type == GVT_FILE);
23447c478bd9Sstevel@tonic-gate 			break;
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 		case EINVAL:		/* prevented above */
23477c478bd9Sstevel@tonic-gate 		default:
23487c478bd9Sstevel@tonic-gate 			bad_error("graph_insert_vertex_unconfigured", err);
23497c478bd9Sstevel@tonic-gate 		}
23507c478bd9Sstevel@tonic-gate 	} else {
23517c478bd9Sstevel@tonic-gate 		if (info->type != GVT_INST) {
23527c478bd9Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
23537c478bd9Sstevel@tonic-gate 			    "FMRI \"%s\" is not allowed for the \"%s\" "
23547c478bd9Sstevel@tonic-gate 			    "dependency's type of instance %s.\n", fmri,
23557c478bd9Sstevel@tonic-gate 			    info->pg_name, info->inst_fmri);
23567c478bd9Sstevel@tonic-gate 			return (info->err = EINVAL);
23577c478bd9Sstevel@tonic-gate 		}
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 		/*
23607c478bd9Sstevel@tonic-gate 		 * We must canonify fmri & add a vertex for it.
23617c478bd9Sstevel@tonic-gate 		 */
23627c478bd9Sstevel@tonic-gate 		fmri_copy_sz = strlen(fmri) + 1;
23637c478bd9Sstevel@tonic-gate 		fmri_copy = startd_alloc(fmri_copy_sz);
23647c478bd9Sstevel@tonic-gate 		(void) strcpy(fmri_copy, fmri);
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate 		/* Determine if the FMRI is a property group or instance */
23677c478bd9Sstevel@tonic-gate 		if (scf_parse_svc_fmri(fmri_copy, &scope, &service,
23687c478bd9Sstevel@tonic-gate 		    &instance, &pg, NULL) != 0) {
23697c478bd9Sstevel@tonic-gate 			startd_free(fmri_copy, fmri_copy_sz);
23707c478bd9Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
23717c478bd9Sstevel@tonic-gate 			    "Dependency \"%s\" of %s has invalid FMRI "
23727c478bd9Sstevel@tonic-gate 			    "\"%s\".\n", info->pg_name, info->inst_fmri,
23737c478bd9Sstevel@tonic-gate 			    fmri);
23747c478bd9Sstevel@tonic-gate 			return (info->err = EINVAL);
23757c478bd9Sstevel@tonic-gate 		}
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate 		if (service == NULL || pg != NULL) {
23787c478bd9Sstevel@tonic-gate 			startd_free(fmri_copy, fmri_copy_sz);
23797c478bd9Sstevel@tonic-gate 			log_framework(LOG_NOTICE,
23807c478bd9Sstevel@tonic-gate 			    "Dependency \"%s\" of %s does not designate a "
23817c478bd9Sstevel@tonic-gate 			    "service or instance.\n", info->pg_name,
23827c478bd9Sstevel@tonic-gate 			    info->inst_fmri);
23837c478bd9Sstevel@tonic-gate 			return (info->err = EINVAL);
23847c478bd9Sstevel@tonic-gate 		}
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 		if (scope == NULL || strcmp(scope, SCF_SCOPE_LOCAL) == 0) {
23877c478bd9Sstevel@tonic-gate 			cfmri = uu_msprintf("svc:/%s%s%s",
23887c478bd9Sstevel@tonic-gate 			    service, instance ? ":" : "", instance ? instance :
23897c478bd9Sstevel@tonic-gate 			    "");
23907c478bd9Sstevel@tonic-gate 		} else {
23917c478bd9Sstevel@tonic-gate 			cfmri = uu_msprintf("svc://%s/%s%s%s",
23927c478bd9Sstevel@tonic-gate 			    scope, service, instance ? ":" : "", instance ?
23937c478bd9Sstevel@tonic-gate 			    instance : "");
23947c478bd9Sstevel@tonic-gate 		}
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 		startd_free(fmri_copy, fmri_copy_sz);
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(cfmri, instance ?
23997c478bd9Sstevel@tonic-gate 		    GVT_INST : GVT_SVC, instance ? 0 : DEPGRP_REQUIRE_ANY,
24007c478bd9Sstevel@tonic-gate 		    RERR_NONE, &v);
24017c478bd9Sstevel@tonic-gate 		uu_free(cfmri);
24027c478bd9Sstevel@tonic-gate 		switch (err) {
24037c478bd9Sstevel@tonic-gate 		case 0:
24047c478bd9Sstevel@tonic-gate 			break;
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 		case EEXIST:
24077c478bd9Sstevel@tonic-gate 			/* Verify v. */
24087c478bd9Sstevel@tonic-gate 			if (instance != NULL)
24097c478bd9Sstevel@tonic-gate 				assert(v->gv_type == GVT_INST);
24107c478bd9Sstevel@tonic-gate 			else
24117c478bd9Sstevel@tonic-gate 				assert(v->gv_type == GVT_SVC);
24127c478bd9Sstevel@tonic-gate 			break;
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 		default:
24157c478bd9Sstevel@tonic-gate 			bad_error("graph_insert_vertex_unconfigured", err);
24167c478bd9Sstevel@tonic-gate 		}
24177c478bd9Sstevel@tonic-gate 	}
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 	/* Add dependency from depgroup_v to new vertex */
24207c478bd9Sstevel@tonic-gate 	info->err = graph_insert_dependency(depgroup_v, v, info->pathp);
24217c478bd9Sstevel@tonic-gate 	switch (info->err) {
24227c478bd9Sstevel@tonic-gate 	case 0:
24237c478bd9Sstevel@tonic-gate 		break;
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 	case ELOOP:
24267c478bd9Sstevel@tonic-gate 		return (ELOOP);
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate 	default:
24297c478bd9Sstevel@tonic-gate 		bad_error("graph_insert_dependency", info->err);
24307c478bd9Sstevel@tonic-gate 	}
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate 	/* This must be after we insert the dependency, to avoid looping. */
24337c478bd9Sstevel@tonic-gate 	switch (v->gv_type) {
24347c478bd9Sstevel@tonic-gate 	case GVT_INST:
24357c478bd9Sstevel@tonic-gate 		if ((v->gv_flags & GV_CONFIGURED) != 0)
24367c478bd9Sstevel@tonic-gate 			break;
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 		inst = safe_scf_instance_create(info->h);
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate 		rebound = B_FALSE;
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate rebound:
24437c478bd9Sstevel@tonic-gate 		err = libscf_lookup_instance(v->gv_name, inst);
24447c478bd9Sstevel@tonic-gate 		switch (err) {
24457c478bd9Sstevel@tonic-gate 		case 0:
24467c478bd9Sstevel@tonic-gate 			err = configure_vertex(v, inst);
24477c478bd9Sstevel@tonic-gate 			switch (err) {
24487c478bd9Sstevel@tonic-gate 			case 0:
24497c478bd9Sstevel@tonic-gate 			case ECANCELED:
24507c478bd9Sstevel@tonic-gate 				break;
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
24537c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(info->h);
24547c478bd9Sstevel@tonic-gate 				rebound = B_TRUE;
24557c478bd9Sstevel@tonic-gate 				goto rebound;
24567c478bd9Sstevel@tonic-gate 
24577c478bd9Sstevel@tonic-gate 			default:
24587c478bd9Sstevel@tonic-gate 				bad_error("configure_vertex", err);
24597c478bd9Sstevel@tonic-gate 			}
24607c478bd9Sstevel@tonic-gate 			break;
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 		case ENOENT:
24637c478bd9Sstevel@tonic-gate 			break;
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
24667c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(info->h);
24677c478bd9Sstevel@tonic-gate 			rebound = B_TRUE;
24687c478bd9Sstevel@tonic-gate 			goto rebound;
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate 		case EINVAL:
24717c478bd9Sstevel@tonic-gate 		case ENOTSUP:
24727c478bd9Sstevel@tonic-gate 		default:
24737c478bd9Sstevel@tonic-gate 			bad_error("libscf_fmri_get_instance", err);
24747c478bd9Sstevel@tonic-gate 		}
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate 		if (rebound)
24797c478bd9Sstevel@tonic-gate 			return (info->err = ECONNRESET);
24807c478bd9Sstevel@tonic-gate 		break;
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 	case GVT_SVC:
24837c478bd9Sstevel@tonic-gate 		(void) add_service(v->gv_name, info->h, &rebound);
24847c478bd9Sstevel@tonic-gate 		if (rebound)
24857c478bd9Sstevel@tonic-gate 			return (info->err = ECONNRESET);
24867c478bd9Sstevel@tonic-gate 	}
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate 	return (0);
24897c478bd9Sstevel@tonic-gate }
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate struct deppg_info {
24927c478bd9Sstevel@tonic-gate 	graph_vertex_t	*v;		/* GVT_INST vertex */
24937c478bd9Sstevel@tonic-gate 	int		err;		/* return error */
24947c478bd9Sstevel@tonic-gate 	int		**pathp;	/* return circular dependency path */
24957c478bd9Sstevel@tonic-gate };
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate /*
24987c478bd9Sstevel@tonic-gate  * Make info->v depend on a new GVT_GROUP node for this property group,
24997c478bd9Sstevel@tonic-gate  * and then call process_dependency_fmri() for the values of the entity
25007c478bd9Sstevel@tonic-gate  * property.  Return 0 on success, or if something goes wrong return nonzero
25017c478bd9Sstevel@tonic-gate  * and set info->err to ECONNABORTED, EINVAL, or the error code returned by
25027c478bd9Sstevel@tonic-gate  * process_dependency_fmri().
25037c478bd9Sstevel@tonic-gate  */
25047c478bd9Sstevel@tonic-gate static int
process_dependency_pg(scf_propertygroup_t * pg,struct deppg_info * info)25057c478bd9Sstevel@tonic-gate process_dependency_pg(scf_propertygroup_t *pg, struct deppg_info *info)
25067c478bd9Sstevel@tonic-gate {
25077c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
25087c478bd9Sstevel@tonic-gate 	depgroup_type_t deptype;
2509af3bcd91Srm88369 	restarter_error_t rerr;
25107c478bd9Sstevel@tonic-gate 	struct depfmri_info linfo;
25117c478bd9Sstevel@tonic-gate 	char *fmri, *pg_name;
25127c478bd9Sstevel@tonic-gate 	size_t fmri_sz;
25137c478bd9Sstevel@tonic-gate 	graph_vertex_t *depgrp;
25147c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
25157c478bd9Sstevel@tonic-gate 	int err;
25167c478bd9Sstevel@tonic-gate 	int empty;
25177c478bd9Sstevel@tonic-gate 	scf_error_t scferr;
25187c478bd9Sstevel@tonic-gate 	ssize_t len;
25197c478bd9Sstevel@tonic-gate 
252053f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 	h = scf_pg_handle(pg);
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate 	pg_name = startd_alloc(max_scf_name_size);
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 	len = scf_pg_get_name(pg, pg_name, max_scf_name_size);
25277c478bd9Sstevel@tonic-gate 	if (len < 0) {
25287c478bd9Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
25297c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
25307c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
25317c478bd9Sstevel@tonic-gate 		default:
25327c478bd9Sstevel@tonic-gate 			return (info->err = ECONNABORTED);
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
25357c478bd9Sstevel@tonic-gate 			return (info->err = 0);
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
25387c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_name", scf_error());
25397c478bd9Sstevel@tonic-gate 		}
25407c478bd9Sstevel@tonic-gate 	}
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 	/*
25437c478bd9Sstevel@tonic-gate 	 * Skip over empty dependency groups.  Since dependency property
25447c478bd9Sstevel@tonic-gate 	 * groups are updated atomically, they are either empty or
25457c478bd9Sstevel@tonic-gate 	 * fully populated.
25467c478bd9Sstevel@tonic-gate 	 */
25477c478bd9Sstevel@tonic-gate 	empty = depgroup_empty(h, pg);
25487c478bd9Sstevel@tonic-gate 	if (empty < 0) {
25497c478bd9Sstevel@tonic-gate 		log_error(LOG_INFO,
25507c478bd9Sstevel@tonic-gate 		    "Error reading dependency group \"%s\" of %s: %s\n",
25517c478bd9Sstevel@tonic-gate 		    pg_name, info->v->gv_name, scf_strerror(scf_error()));
25527c478bd9Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
25537c478bd9Sstevel@tonic-gate 		return (info->err = EINVAL);
25547c478bd9Sstevel@tonic-gate 
25557c478bd9Sstevel@tonic-gate 	} else if (empty == 1) {
25567c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
25577c478bd9Sstevel@tonic-gate 		    "Ignoring empty dependency group \"%s\" of %s\n",
25587c478bd9Sstevel@tonic-gate 		    pg_name, info->v->gv_name);
25597c478bd9Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
25607c478bd9Sstevel@tonic-gate 		return (info->err = 0);
25617c478bd9Sstevel@tonic-gate 	}
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	fmri_sz = strlen(info->v->gv_name) + 1 + len + 1;
25647c478bd9Sstevel@tonic-gate 	fmri = startd_alloc(fmri_sz);
25657c478bd9Sstevel@tonic-gate 
256609f79f7cSDan Vatca 	(void) snprintf(fmri, fmri_sz, "%s>%s", info->v->gv_name,
25677c478bd9Sstevel@tonic-gate 	    pg_name);
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate 	/* Validate the pg before modifying the graph */
25707c478bd9Sstevel@tonic-gate 	deptype = depgroup_read_grouping(h, pg);
25717c478bd9Sstevel@tonic-gate 	if (deptype == DEPGRP_UNSUPPORTED) {
25727c478bd9Sstevel@tonic-gate 		log_error(LOG_INFO,
25737c478bd9Sstevel@tonic-gate 		    "Dependency \"%s\" of %s has an unknown grouping value.\n",
25747c478bd9Sstevel@tonic-gate 		    pg_name, info->v->gv_name);
25757c478bd9Sstevel@tonic-gate 		startd_free(fmri, fmri_sz);
25767c478bd9Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
25777c478bd9Sstevel@tonic-gate 		return (info->err = EINVAL);
25787c478bd9Sstevel@tonic-gate 	}
25797c478bd9Sstevel@tonic-gate 
2580af3bcd91Srm88369 	rerr = depgroup_read_restart(h, pg);
2581af3bcd91Srm88369 	if (rerr == RERR_UNSUPPORTED) {
2582af3bcd91Srm88369 		log_error(LOG_INFO,
2583af3bcd91Srm88369 		    "Dependency \"%s\" of %s has an unknown restart_on value."
2584af3bcd91Srm88369 		    "\n", pg_name, info->v->gv_name);
2585af3bcd91Srm88369 		startd_free(fmri, fmri_sz);
2586af3bcd91Srm88369 		startd_free(pg_name, max_scf_name_size);
2587af3bcd91Srm88369 		return (info->err = EINVAL);
2588af3bcd91Srm88369 	}
2589af3bcd91Srm88369 
25907c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 	if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0) {
25937c478bd9Sstevel@tonic-gate 		scferr = scf_error();
25947c478bd9Sstevel@tonic-gate 		scf_property_destroy(prop);
25957c478bd9Sstevel@tonic-gate 		if (scferr == SCF_ERROR_DELETED) {
25967c478bd9Sstevel@tonic-gate 			startd_free(fmri, fmri_sz);
25977c478bd9Sstevel@tonic-gate 			startd_free(pg_name, max_scf_name_size);
25987c478bd9Sstevel@tonic-gate 			return (info->err = 0);
25997c478bd9Sstevel@tonic-gate 		} else if (scferr != SCF_ERROR_NOT_FOUND) {
26007c478bd9Sstevel@tonic-gate 			startd_free(fmri, fmri_sz);
26017c478bd9Sstevel@tonic-gate 			startd_free(pg_name, max_scf_name_size);
26027c478bd9Sstevel@tonic-gate 			return (info->err = ECONNABORTED);
26037c478bd9Sstevel@tonic-gate 		}
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 		log_error(LOG_INFO,
26067c478bd9Sstevel@tonic-gate 		    "Dependency \"%s\" of %s is missing a \"%s\" property.\n",
26077c478bd9Sstevel@tonic-gate 		    pg_name, info->v->gv_name, SCF_PROPERTY_ENTITIES);
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 		startd_free(fmri, fmri_sz);
26107c478bd9Sstevel@tonic-gate 		startd_free(pg_name, max_scf_name_size);
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 		return (info->err = EINVAL);
26137c478bd9Sstevel@tonic-gate 	}
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate 	/* Create depgroup vertex for pg */
26167c478bd9Sstevel@tonic-gate 	err = graph_insert_vertex_unconfigured(fmri, GVT_GROUP, deptype,
2617af3bcd91Srm88369 	    rerr, &depgrp);
26187c478bd9Sstevel@tonic-gate 	assert(err == 0);
26197c478bd9Sstevel@tonic-gate 	startd_free(fmri, fmri_sz);
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate 	/* Add dependency from inst vertex to new vertex */
26227c478bd9Sstevel@tonic-gate 	err = graph_insert_dependency(info->v, depgrp, info->pathp);
26237c478bd9Sstevel@tonic-gate 	/* ELOOP can't happen because this should be a new vertex */
26247c478bd9Sstevel@tonic-gate 	assert(err == 0);
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate 	linfo.v = depgrp;
26277c478bd9Sstevel@tonic-gate 	linfo.type = depgroup_read_scheme(h, pg);
26287c478bd9Sstevel@tonic-gate 	linfo.inst_fmri = info->v->gv_name;
26297c478bd9Sstevel@tonic-gate 	linfo.pg_name = pg_name;
26307c478bd9Sstevel@tonic-gate 	linfo.h = h;
26317c478bd9Sstevel@tonic-gate 	linfo.err = 0;
26327c478bd9Sstevel@tonic-gate 	linfo.pathp = info->pathp;
26337c478bd9Sstevel@tonic-gate 	err = walk_property_astrings(prop, (callback_t)process_dependency_fmri,
26347c478bd9Sstevel@tonic-gate 	    &linfo);
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
26377c478bd9Sstevel@tonic-gate 	startd_free(pg_name, max_scf_name_size);
26387c478bd9Sstevel@tonic-gate 
26397c478bd9Sstevel@tonic-gate 	switch (err) {
26407c478bd9Sstevel@tonic-gate 	case 0:
26417c478bd9Sstevel@tonic-gate 	case EINTR:
26427c478bd9Sstevel@tonic-gate 		return (info->err = linfo.err);
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
26457c478bd9Sstevel@tonic-gate 	case EINVAL:
26467c478bd9Sstevel@tonic-gate 		return (info->err = err);
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate 	case ECANCELED:
26497c478bd9Sstevel@tonic-gate 		return (info->err = 0);
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 	case ECONNRESET:
26527c478bd9Sstevel@tonic-gate 		return (info->err = ECONNABORTED);
26537c478bd9Sstevel@tonic-gate 
26547c478bd9Sstevel@tonic-gate 	default:
26557c478bd9Sstevel@tonic-gate 		bad_error("walk_property_astrings", err);
26567c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
26577c478bd9Sstevel@tonic-gate 	}
26587c478bd9Sstevel@tonic-gate }
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate /*
26617c478bd9Sstevel@tonic-gate  * Build the dependency info for v from the repository.  Returns 0 on success,
26627c478bd9Sstevel@tonic-gate  * ECONNABORTED on repository disconnection, EINVAL if the repository
26637c478bd9Sstevel@tonic-gate  * configuration is invalid, and ELOOP if a dependency would cause a cycle.
26647c478bd9Sstevel@tonic-gate  * In the last case, *pathp will point to a -1-terminated array of ids which
26657c478bd9Sstevel@tonic-gate  * constitute the rest of the dependency cycle.
26667c478bd9Sstevel@tonic-gate  */
26677c478bd9Sstevel@tonic-gate static int
set_dependencies(graph_vertex_t * v,scf_instance_t * inst,int ** pathp)26687c478bd9Sstevel@tonic-gate set_dependencies(graph_vertex_t *v, scf_instance_t *inst, int **pathp)
26697c478bd9Sstevel@tonic-gate {
26707c478bd9Sstevel@tonic-gate 	struct deppg_info info;
26717c478bd9Sstevel@tonic-gate 	int err;
26727c478bd9Sstevel@tonic-gate 	uint_t old_configured;
26737c478bd9Sstevel@tonic-gate 
267453f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 	/*
26777c478bd9Sstevel@tonic-gate 	 * Mark the vertex as configured during dependency insertion to avoid
26787c478bd9Sstevel@tonic-gate 	 * dependency cycles (which can appear in the graph if one of the
26797c478bd9Sstevel@tonic-gate 	 * vertices is an exclusion-group).
26807c478bd9Sstevel@tonic-gate 	 */
26817c478bd9Sstevel@tonic-gate 	old_configured = v->gv_flags & GV_CONFIGURED;
26827c478bd9Sstevel@tonic-gate 	v->gv_flags |= GV_CONFIGURED;
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	info.err = 0;
26857c478bd9Sstevel@tonic-gate 	info.v = v;
26867c478bd9Sstevel@tonic-gate 	info.pathp = pathp;
26877c478bd9Sstevel@tonic-gate 
26887c478bd9Sstevel@tonic-gate 	err = walk_dependency_pgs(inst, (callback_t)process_dependency_pg,
26897c478bd9Sstevel@tonic-gate 	    &info);
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate 	if (!old_configured)
26927c478bd9Sstevel@tonic-gate 		v->gv_flags &= ~GV_CONFIGURED;
26937c478bd9Sstevel@tonic-gate 
26947c478bd9Sstevel@tonic-gate 	switch (err) {
26957c478bd9Sstevel@tonic-gate 	case 0:
26967c478bd9Sstevel@tonic-gate 	case EINTR:
26977c478bd9Sstevel@tonic-gate 		return (info.err);
26987c478bd9Sstevel@tonic-gate 
26997c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
27007c478bd9Sstevel@tonic-gate 		return (ECONNABORTED);
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 	case ECANCELED:
27037c478bd9Sstevel@tonic-gate 		/* Should get delete event, so return 0. */
27047c478bd9Sstevel@tonic-gate 		return (0);
27057c478bd9Sstevel@tonic-gate 
27067c478bd9Sstevel@tonic-gate 	default:
27077c478bd9Sstevel@tonic-gate 		bad_error("walk_dependency_pgs", err);
27087c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
27097c478bd9Sstevel@tonic-gate 	}
27107c478bd9Sstevel@tonic-gate }
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate 
27137c478bd9Sstevel@tonic-gate static void
handle_cycle(const char * fmri,int * path)27147c478bd9Sstevel@tonic-gate handle_cycle(const char *fmri, int *path)
27157c478bd9Sstevel@tonic-gate {
27167c478bd9Sstevel@tonic-gate 	const char *cp;
27177c478bd9Sstevel@tonic-gate 	size_t sz;
27187c478bd9Sstevel@tonic-gate 
271953f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
27207c478bd9Sstevel@tonic-gate 
27217c478bd9Sstevel@tonic-gate 	path_to_str(path, (char **)&cp, &sz);
27227c478bd9Sstevel@tonic-gate 
272399b44c3bSlianep 	log_error(LOG_ERR, "Transitioning %s to maintenance "
272499b44c3bSlianep 	    "because it completes a dependency cycle (see svcs -xv for "
272599b44c3bSlianep 	    "details):\n%s", fmri ? fmri : "?", cp);
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate 	startd_free((void *)cp, sz);
27287c478bd9Sstevel@tonic-gate }
27297c478bd9Sstevel@tonic-gate 
27307c478bd9Sstevel@tonic-gate /*
27313ad28c1eSrm88369  * Increment the vertex's reference count to prevent the vertex removal
27323ad28c1eSrm88369  * from the dgraph.
27333ad28c1eSrm88369  */
27343ad28c1eSrm88369 static void
vertex_ref(graph_vertex_t * v)27353ad28c1eSrm88369 vertex_ref(graph_vertex_t *v)
27363ad28c1eSrm88369 {
273753f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
27383ad28c1eSrm88369 
27393ad28c1eSrm88369 	v->gv_refs++;
27403ad28c1eSrm88369 }
27413ad28c1eSrm88369 
27423ad28c1eSrm88369 /*
27433ad28c1eSrm88369  * Decrement the vertex's reference count and remove the vertex from
27443ad28c1eSrm88369  * the dgraph when possible.
27453ad28c1eSrm88369  *
27463ad28c1eSrm88369  * Return VERTEX_REMOVED when the vertex has been removed otherwise
27473ad28c1eSrm88369  * return VERTEX_INUSE.
27487c478bd9Sstevel@tonic-gate  */
27497c478bd9Sstevel@tonic-gate static int
vertex_unref(graph_vertex_t * v)27503ad28c1eSrm88369 vertex_unref(graph_vertex_t *v)
27513ad28c1eSrm88369 {
275253f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
27533ad28c1eSrm88369 	assert(v->gv_refs > 0);
27543ad28c1eSrm88369 
27553ad28c1eSrm88369 	v->gv_refs--;
27563ad28c1eSrm88369 
27573ad28c1eSrm88369 	return (free_if_unrefed(v));
27583ad28c1eSrm88369 }
27593ad28c1eSrm88369 
27603ad28c1eSrm88369 /*
27613ad28c1eSrm88369  * When run on the dependencies of a vertex, populates list with
27623ad28c1eSrm88369  * graph_edge_t's which point to the service vertices or the instance
27633ad28c1eSrm88369  * vertices (no GVT_GROUP nodes) on which the vertex depends.
27643ad28c1eSrm88369  *
27653ad28c1eSrm88369  * Increment the vertex's reference count once the vertex is inserted
27663ad28c1eSrm88369  * in the list. The vertex won't be able to be deleted from the dgraph
27673ad28c1eSrm88369  * while it is referenced.
27683ad28c1eSrm88369  */
27693ad28c1eSrm88369 static int
append_svcs_or_insts(graph_edge_t * e,uu_list_t * list)27703ad28c1eSrm88369 append_svcs_or_insts(graph_edge_t *e, uu_list_t *list)
27717c478bd9Sstevel@tonic-gate {
27727c478bd9Sstevel@tonic-gate 	graph_vertex_t *v = e->ge_vertex;
27737c478bd9Sstevel@tonic-gate 	graph_edge_t *new;
27747c478bd9Sstevel@tonic-gate 	int r;
27757c478bd9Sstevel@tonic-gate 
27767c478bd9Sstevel@tonic-gate 	switch (v->gv_type) {
27777c478bd9Sstevel@tonic-gate 	case GVT_INST:
27787c478bd9Sstevel@tonic-gate 	case GVT_SVC:
27797c478bd9Sstevel@tonic-gate 		break;
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate 	case GVT_GROUP:
27827c478bd9Sstevel@tonic-gate 		r = uu_list_walk(v->gv_dependencies,
27833ad28c1eSrm88369 		    (uu_walk_fn_t *)append_svcs_or_insts, list, 0);
27847c478bd9Sstevel@tonic-gate 		assert(r == 0);
27857c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 	case GVT_FILE:
27887c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 	default:
27917c478bd9Sstevel@tonic-gate #ifndef NDEBUG
27927c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
27937c478bd9Sstevel@tonic-gate 		    __LINE__, v->gv_type);
27947c478bd9Sstevel@tonic-gate #endif
27957c478bd9Sstevel@tonic-gate 		abort();
27967c478bd9Sstevel@tonic-gate 	}
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 	new = startd_alloc(sizeof (*new));
27997c478bd9Sstevel@tonic-gate 	new->ge_vertex = v;
28007c478bd9Sstevel@tonic-gate 	uu_list_node_init(new, &new->ge_link, graph_edge_pool);
28017c478bd9Sstevel@tonic-gate 	r = uu_list_insert_before(list, NULL, new);
28027c478bd9Sstevel@tonic-gate 	assert(r == 0);
28033ad28c1eSrm88369 
28043ad28c1eSrm88369 	/*
28053ad28c1eSrm88369 	 * Because we are inserting the vertex in a list, we don't want
28063ad28c1eSrm88369 	 * the vertex to be freed while the list is in use. In order to
28073ad28c1eSrm88369 	 * achieve that, increment the vertex's reference count.
28083ad28c1eSrm88369 	 */
28093ad28c1eSrm88369 	vertex_ref(v);
28103ad28c1eSrm88369 
28117c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
28127c478bd9Sstevel@tonic-gate }
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate static boolean_t
should_be_in_subgraph(graph_vertex_t * v)28157c478bd9Sstevel@tonic-gate should_be_in_subgraph(graph_vertex_t *v)
28167c478bd9Sstevel@tonic-gate {
28177c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate 	if (v == milestone)
28207c478bd9Sstevel@tonic-gate 		return (B_TRUE);
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 	/*
28237c478bd9Sstevel@tonic-gate 	 * v is in the subgraph if any of its dependents are in the subgraph.
28247c478bd9Sstevel@tonic-gate 	 * Except for EXCLUDE_ALL dependents.  And OPTIONAL dependents only
28257c478bd9Sstevel@tonic-gate 	 * count if we're enabled.
28267c478bd9Sstevel@tonic-gate 	 */
28277c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
28287c478bd9Sstevel@tonic-gate 	    e != NULL;
28297c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e)) {
28307c478bd9Sstevel@tonic-gate 		graph_vertex_t *dv = e->ge_vertex;
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate 		if (!(dv->gv_flags & GV_INSUBGRAPH))
28337c478bd9Sstevel@tonic-gate 			continue;
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 		/*
28367c478bd9Sstevel@tonic-gate 		 * Don't include instances that are optional and disabled.
28377c478bd9Sstevel@tonic-gate 		 */
28387c478bd9Sstevel@tonic-gate 		if (v->gv_type == GVT_INST && dv->gv_type == GVT_SVC) {
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate 			int in = 0;
28417c478bd9Sstevel@tonic-gate 			graph_edge_t *ee;
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 			for (ee = uu_list_first(dv->gv_dependents);
28447c478bd9Sstevel@tonic-gate 			    ee != NULL;
28457c478bd9Sstevel@tonic-gate 			    ee = uu_list_next(dv->gv_dependents, ee)) {
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 				graph_vertex_t *ddv = e->ge_vertex;
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 				if (ddv->gv_type == GVT_GROUP &&
28507c478bd9Sstevel@tonic-gate 				    ddv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
28517c478bd9Sstevel@tonic-gate 					continue;
28527c478bd9Sstevel@tonic-gate 
28537c478bd9Sstevel@tonic-gate 				if (ddv->gv_type == GVT_GROUP &&
28547c478bd9Sstevel@tonic-gate 				    ddv->gv_depgroup == DEPGRP_OPTIONAL_ALL &&
28557c478bd9Sstevel@tonic-gate 				    !(v->gv_flags & GV_ENBLD_NOOVR))
28567c478bd9Sstevel@tonic-gate 					continue;
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 				in = 1;
28597c478bd9Sstevel@tonic-gate 			}
28607c478bd9Sstevel@tonic-gate 			if (!in)
28617c478bd9Sstevel@tonic-gate 				continue;
28627c478bd9Sstevel@tonic-gate 		}
28637c478bd9Sstevel@tonic-gate 		if (v->gv_type == GVT_INST &&
28647c478bd9Sstevel@tonic-gate 		    dv->gv_type == GVT_GROUP &&
28657c478bd9Sstevel@tonic-gate 		    dv->gv_depgroup == DEPGRP_OPTIONAL_ALL &&
28667c478bd9Sstevel@tonic-gate 		    !(v->gv_flags & GV_ENBLD_NOOVR))
28677c478bd9Sstevel@tonic-gate 			continue;
28687c478bd9Sstevel@tonic-gate 
28697c478bd9Sstevel@tonic-gate 		/* Don't include excluded services and instances */
28707c478bd9Sstevel@tonic-gate 		if (dv->gv_type == GVT_GROUP &&
28717c478bd9Sstevel@tonic-gate 		    dv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
28727c478bd9Sstevel@tonic-gate 			continue;
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 		return (B_TRUE);
28757c478bd9Sstevel@tonic-gate 	}
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate 	return (B_FALSE);
28787c478bd9Sstevel@tonic-gate }
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate /*
28817c478bd9Sstevel@tonic-gate  * Ensures that GV_INSUBGRAPH is set properly for v and its descendents.  If
28827c478bd9Sstevel@tonic-gate  * any bits change, manipulate the repository appropriately.  Returns 0 or
28837c478bd9Sstevel@tonic-gate  * ECONNABORTED.
28847c478bd9Sstevel@tonic-gate  */
28857c478bd9Sstevel@tonic-gate static int
eval_subgraph(graph_vertex_t * v,scf_handle_t * h)28867c478bd9Sstevel@tonic-gate eval_subgraph(graph_vertex_t *v, scf_handle_t *h)
28877c478bd9Sstevel@tonic-gate {
28887c478bd9Sstevel@tonic-gate 	boolean_t old = (v->gv_flags & GV_INSUBGRAPH) != 0;
28897c478bd9Sstevel@tonic-gate 	boolean_t new;
28907c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
28917c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
28927c478bd9Sstevel@tonic-gate 	int ret = 0, r;
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 	assert(milestone != NULL && milestone != MILESTONE_NONE);
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 	new = should_be_in_subgraph(v);
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 	if (new == old)
28997c478bd9Sstevel@tonic-gate 		return (0);
29007c478bd9Sstevel@tonic-gate 
29017c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, new ? "Adding %s to the subgraph.\n" :
29027c478bd9Sstevel@tonic-gate 	    "Removing %s from the subgraph.\n", v->gv_name);
29037c478bd9Sstevel@tonic-gate 
29047c478bd9Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_INSUBGRAPH) |
29057c478bd9Sstevel@tonic-gate 	    (new ? GV_INSUBGRAPH : 0);
29067c478bd9Sstevel@tonic-gate 
29077c478bd9Sstevel@tonic-gate 	if (v->gv_type == GVT_INST && (v->gv_flags & GV_CONFIGURED)) {
29087c478bd9Sstevel@tonic-gate 		int err;
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate get_inst:
29117c478bd9Sstevel@tonic-gate 		err = libscf_fmri_get_instance(h, v->gv_name, &inst);
29127c478bd9Sstevel@tonic-gate 		if (err != 0) {
29137c478bd9Sstevel@tonic-gate 			switch (err) {
29147c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
29157c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
29167c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
29177c478bd9Sstevel@tonic-gate 				goto get_inst;
29187c478bd9Sstevel@tonic-gate 
29197c478bd9Sstevel@tonic-gate 			case ENOENT:
29207c478bd9Sstevel@tonic-gate 				break;
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 			case EINVAL:
29237c478bd9Sstevel@tonic-gate 			case ENOTSUP:
29247c478bd9Sstevel@tonic-gate 			default:
29257c478bd9Sstevel@tonic-gate 				bad_error("libscf_fmri_get_instance", err);
29267c478bd9Sstevel@tonic-gate 			}
29277c478bd9Sstevel@tonic-gate 		} else {
29287c478bd9Sstevel@tonic-gate 			const char *f;
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate 			if (new) {
29317c478bd9Sstevel@tonic-gate 				err = libscf_delete_enable_ovr(inst);
29327c478bd9Sstevel@tonic-gate 				f = "libscf_delete_enable_ovr";
29337c478bd9Sstevel@tonic-gate 			} else {
29347c478bd9Sstevel@tonic-gate 				err = libscf_set_enable_ovr(inst, 0);
29357c478bd9Sstevel@tonic-gate 				f = "libscf_set_enable_ovr";
29367c478bd9Sstevel@tonic-gate 			}
29377c478bd9Sstevel@tonic-gate 			scf_instance_destroy(inst);
29387c478bd9Sstevel@tonic-gate 			switch (err) {
29397c478bd9Sstevel@tonic-gate 			case 0:
29407c478bd9Sstevel@tonic-gate 			case ECANCELED:
29417c478bd9Sstevel@tonic-gate 				break;
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
29447c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
29457c478bd9Sstevel@tonic-gate 				/*
29467c478bd9Sstevel@tonic-gate 				 * We must continue so the graph is updated,
29477c478bd9Sstevel@tonic-gate 				 * but we must return ECONNABORTED so any
29487c478bd9Sstevel@tonic-gate 				 * libscf state held by any callers is reset.
29497c478bd9Sstevel@tonic-gate 				 */
29507c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
29517c478bd9Sstevel@tonic-gate 				goto get_inst;
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate 			case EROFS:
29547c478bd9Sstevel@tonic-gate 			case EPERM:
29557c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING,
29567c478bd9Sstevel@tonic-gate 				    "Could not set %s/%s for %s: %s.\n",
29577c478bd9Sstevel@tonic-gate 				    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
29587c478bd9Sstevel@tonic-gate 				    v->gv_name, strerror(err));
29597c478bd9Sstevel@tonic-gate 				break;
29607c478bd9Sstevel@tonic-gate 
29617c478bd9Sstevel@tonic-gate 			default:
29627c478bd9Sstevel@tonic-gate 				bad_error(f, err);
29637c478bd9Sstevel@tonic-gate 			}
29647c478bd9Sstevel@tonic-gate 		}
29657c478bd9Sstevel@tonic-gate 	}
29667c478bd9Sstevel@tonic-gate 
29677c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependencies);
29687c478bd9Sstevel@tonic-gate 	    e != NULL;
29697c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependencies, e)) {
29707c478bd9Sstevel@tonic-gate 		r = eval_subgraph(e->ge_vertex, h);
29717c478bd9Sstevel@tonic-gate 		if (r != 0) {
29727c478bd9Sstevel@tonic-gate 			assert(r == ECONNABORTED);
29737c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
29747c478bd9Sstevel@tonic-gate 		}
29757c478bd9Sstevel@tonic-gate 	}
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate 	return (ret);
29787c478bd9Sstevel@tonic-gate }
29797c478bd9Sstevel@tonic-gate 
29807c478bd9Sstevel@tonic-gate /*
29817c478bd9Sstevel@tonic-gate  * Delete the (property group) dependencies of v & create new ones based on
29827c478bd9Sstevel@tonic-gate  * inst.  If doing so would create a cycle, log a message and put the instance
29837c478bd9Sstevel@tonic-gate  * into maintenance.  Update GV_INSUBGRAPH flags as necessary.  Returns 0 or
29847c478bd9Sstevel@tonic-gate  * ECONNABORTED.
29857c478bd9Sstevel@tonic-gate  */
298699b44c3bSlianep int
refresh_vertex(graph_vertex_t * v,scf_instance_t * inst)29877c478bd9Sstevel@tonic-gate refresh_vertex(graph_vertex_t *v, scf_instance_t *inst)
29887c478bd9Sstevel@tonic-gate {
29897c478bd9Sstevel@tonic-gate 	int err;
29907c478bd9Sstevel@tonic-gate 	int *path;
29917c478bd9Sstevel@tonic-gate 	char *fmri;
29927c478bd9Sstevel@tonic-gate 	int r;
29937c478bd9Sstevel@tonic-gate 	scf_handle_t *h = scf_instance_handle(inst);
29947c478bd9Sstevel@tonic-gate 	uu_list_t *old_deps;
29957c478bd9Sstevel@tonic-gate 	int ret = 0;
29967c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
29973ad28c1eSrm88369 	graph_vertex_t *vv;
29987c478bd9Sstevel@tonic-gate 
299953f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
30007c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph engine: Refreshing %s.\n", v->gv_name);
30037c478bd9Sstevel@tonic-gate 
30047c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
30057c478bd9Sstevel@tonic-gate 		/*
30067c478bd9Sstevel@tonic-gate 		 * In case some of v's dependencies are being deleted we must
30077c478bd9Sstevel@tonic-gate 		 * make a list of them now for GV_INSUBGRAPH-flag evaluation
30087c478bd9Sstevel@tonic-gate 		 * after the new dependencies are in place.
30097c478bd9Sstevel@tonic-gate 		 */
30107c478bd9Sstevel@tonic-gate 		old_deps = startd_list_create(graph_edge_pool, NULL, 0);
30117c478bd9Sstevel@tonic-gate 
30127c478bd9Sstevel@tonic-gate 		err = uu_list_walk(v->gv_dependencies,
30133ad28c1eSrm88369 		    (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0);
30147c478bd9Sstevel@tonic-gate 		assert(err == 0);
30157c478bd9Sstevel@tonic-gate 	}
30167c478bd9Sstevel@tonic-gate 
30177c478bd9Sstevel@tonic-gate 	delete_instance_dependencies(v, B_FALSE);
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate 	err = set_dependencies(v, inst, &path);
30207c478bd9Sstevel@tonic-gate 	switch (err) {
30217c478bd9Sstevel@tonic-gate 	case 0:
30227c478bd9Sstevel@tonic-gate 		break;
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
30257c478bd9Sstevel@tonic-gate 		ret = err;
30267c478bd9Sstevel@tonic-gate 		goto out;
30277c478bd9Sstevel@tonic-gate 
30287c478bd9Sstevel@tonic-gate 	case EINVAL:
30297c478bd9Sstevel@tonic-gate 	case ELOOP:
30307c478bd9Sstevel@tonic-gate 		r = libscf_instance_get_fmri(inst, &fmri);
30317c478bd9Sstevel@tonic-gate 		switch (r) {
30327c478bd9Sstevel@tonic-gate 		case 0:
30337c478bd9Sstevel@tonic-gate 			break;
30347c478bd9Sstevel@tonic-gate 
30357c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
30367c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
30377c478bd9Sstevel@tonic-gate 			goto out;
30387c478bd9Sstevel@tonic-gate 
30397c478bd9Sstevel@tonic-gate 		case ECANCELED:
30407c478bd9Sstevel@tonic-gate 			ret = 0;
30417c478bd9Sstevel@tonic-gate 			goto out;
30427c478bd9Sstevel@tonic-gate 
30437c478bd9Sstevel@tonic-gate 		default:
30447c478bd9Sstevel@tonic-gate 			bad_error("libscf_instance_get_fmri", r);
30457c478bd9Sstevel@tonic-gate 		}
30467c478bd9Sstevel@tonic-gate 
30477c478bd9Sstevel@tonic-gate 		if (err == EINVAL) {
30487c478bd9Sstevel@tonic-gate 			log_error(LOG_ERR, "Transitioning %s "
30497c478bd9Sstevel@tonic-gate 			    "to maintenance due to misconfiguration.\n",
30507c478bd9Sstevel@tonic-gate 			    fmri ? fmri : "?");
30517c478bd9Sstevel@tonic-gate 			vertex_send_event(v,
30527c478bd9Sstevel@tonic-gate 			    RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY);
30537c478bd9Sstevel@tonic-gate 		} else {
30547c478bd9Sstevel@tonic-gate 			handle_cycle(fmri, path);
30557c478bd9Sstevel@tonic-gate 			vertex_send_event(v,
30567c478bd9Sstevel@tonic-gate 			    RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE);
30577c478bd9Sstevel@tonic-gate 		}
30587c478bd9Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
30597c478bd9Sstevel@tonic-gate 		ret = 0;
30607c478bd9Sstevel@tonic-gate 		goto out;
30617c478bd9Sstevel@tonic-gate 
30627c478bd9Sstevel@tonic-gate 	default:
30637c478bd9Sstevel@tonic-gate 		bad_error("set_dependencies", err);
30647c478bd9Sstevel@tonic-gate 	}
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
30677c478bd9Sstevel@tonic-gate 		boolean_t aborted = B_FALSE;
30687c478bd9Sstevel@tonic-gate 
30697c478bd9Sstevel@tonic-gate 		for (e = uu_list_first(old_deps);
30707c478bd9Sstevel@tonic-gate 		    e != NULL;
30717c478bd9Sstevel@tonic-gate 		    e = uu_list_next(old_deps, e)) {
30723ad28c1eSrm88369 			vv = e->ge_vertex;
30733ad28c1eSrm88369 
30743ad28c1eSrm88369 			if (vertex_unref(vv) == VERTEX_INUSE &&
30753ad28c1eSrm88369 			    eval_subgraph(vv, h) == ECONNABORTED)
30767c478bd9Sstevel@tonic-gate 				aborted = B_TRUE;
30777c478bd9Sstevel@tonic-gate 		}
30787c478bd9Sstevel@tonic-gate 
30797c478bd9Sstevel@tonic-gate 		for (e = uu_list_first(v->gv_dependencies);
30807c478bd9Sstevel@tonic-gate 		    e != NULL;
30817c478bd9Sstevel@tonic-gate 		    e = uu_list_next(v->gv_dependencies, e)) {
30827c478bd9Sstevel@tonic-gate 			if (eval_subgraph(e->ge_vertex, h) ==
30837c478bd9Sstevel@tonic-gate 			    ECONNABORTED)
30847c478bd9Sstevel@tonic-gate 				aborted = B_TRUE;
30857c478bd9Sstevel@tonic-gate 		}
30867c478bd9Sstevel@tonic-gate 
30877c478bd9Sstevel@tonic-gate 		if (aborted) {
30887c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
30897c478bd9Sstevel@tonic-gate 			goto out;
30907c478bd9Sstevel@tonic-gate 		}
30917c478bd9Sstevel@tonic-gate 	}
30927c478bd9Sstevel@tonic-gate 
309399b44c3bSlianep 	graph_start_if_satisfied(v);
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 	ret = 0;
30967c478bd9Sstevel@tonic-gate 
30977c478bd9Sstevel@tonic-gate out:
30987c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
30997c478bd9Sstevel@tonic-gate 		void *cookie = NULL;
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 		while ((e = uu_list_teardown(old_deps, &cookie)) != NULL)
31027c478bd9Sstevel@tonic-gate 			startd_free(e, sizeof (*e));
31037c478bd9Sstevel@tonic-gate 
31047c478bd9Sstevel@tonic-gate 		uu_list_destroy(old_deps);
31057c478bd9Sstevel@tonic-gate 	}
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate 	return (ret);
31087c478bd9Sstevel@tonic-gate }
31097c478bd9Sstevel@tonic-gate 
31107c478bd9Sstevel@tonic-gate /*
31117c478bd9Sstevel@tonic-gate  * Set up v according to inst.  That is, make sure it depends on its
31127c478bd9Sstevel@tonic-gate  * restarter and set up its dependencies.  Send the ADD_INSTANCE command to
31137c478bd9Sstevel@tonic-gate  * the restarter, and send ENABLE or DISABLE as appropriate.
31147c478bd9Sstevel@tonic-gate  *
31157c478bd9Sstevel@tonic-gate  * Returns 0 on success, ECONNABORTED on repository disconnection, or
31167c478bd9Sstevel@tonic-gate  * ECANCELED if inst is deleted.
31177c478bd9Sstevel@tonic-gate  */
31187c478bd9Sstevel@tonic-gate static int
configure_vertex(graph_vertex_t * v,scf_instance_t * inst)31197c478bd9Sstevel@tonic-gate configure_vertex(graph_vertex_t *v, scf_instance_t *inst)
31207c478bd9Sstevel@tonic-gate {
31217c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
31227c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
31237c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
31247c478bd9Sstevel@tonic-gate 	char *restarter_fmri = startd_alloc(max_scf_value_size);
31257c478bd9Sstevel@tonic-gate 	int enabled, enabled_ovr;
31267c478bd9Sstevel@tonic-gate 	int err;
31277c478bd9Sstevel@tonic-gate 	int *path;
312870cbfe41SPhilippe Jung 	int deathrow;
3129f6e214c7SGavin Maltby 	int32_t tset;
31307c478bd9Sstevel@tonic-gate 
31317c478bd9Sstevel@tonic-gate 	restarter_fmri[0] = '\0';
31327c478bd9Sstevel@tonic-gate 
313353f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
31347c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
31357c478bd9Sstevel@tonic-gate 	assert((v->gv_flags & GV_CONFIGURED) == 0);
31367c478bd9Sstevel@tonic-gate 
31377c478bd9Sstevel@tonic-gate 	/* GV_INSUBGRAPH should already be set properly. */
31387c478bd9Sstevel@tonic-gate 	assert(should_be_in_subgraph(v) ==
31397c478bd9Sstevel@tonic-gate 	    ((v->gv_flags & GV_INSUBGRAPH) != 0));
31407c478bd9Sstevel@tonic-gate 
314170cbfe41SPhilippe Jung 	/*
314270cbfe41SPhilippe Jung 	 * If the instance fmri is in the deathrow list then set the
314370cbfe41SPhilippe Jung 	 * GV_DEATHROW flag on the vertex and create and set to true the
314470cbfe41SPhilippe Jung 	 * SCF_PROPERTY_DEATHROW boolean property in the non-persistent
314570cbfe41SPhilippe Jung 	 * repository for this instance fmri.
314670cbfe41SPhilippe Jung 	 */
314770cbfe41SPhilippe Jung 	if ((v->gv_flags & GV_DEATHROW) ||
314870cbfe41SPhilippe Jung 	    (is_fmri_in_deathrow(v->gv_name) == B_TRUE)) {
314970cbfe41SPhilippe Jung 		if ((v->gv_flags & GV_DEATHROW) == 0) {
315070cbfe41SPhilippe Jung 			/*
315170cbfe41SPhilippe Jung 			 * Set flag GV_DEATHROW, create and set to true
315270cbfe41SPhilippe Jung 			 * the SCF_PROPERTY_DEATHROW property in the
315370cbfe41SPhilippe Jung 			 * non-persistent repository for this instance fmri.
315470cbfe41SPhilippe Jung 			 */
315570cbfe41SPhilippe Jung 			v->gv_flags |= GV_DEATHROW;
315670cbfe41SPhilippe Jung 
315770cbfe41SPhilippe Jung 			switch (err = libscf_set_deathrow(inst, 1)) {
315870cbfe41SPhilippe Jung 			case 0:
315970cbfe41SPhilippe Jung 				break;
316070cbfe41SPhilippe Jung 
316170cbfe41SPhilippe Jung 			case ECONNABORTED:
316270cbfe41SPhilippe Jung 			case ECANCELED:
316370cbfe41SPhilippe Jung 				startd_free(restarter_fmri, max_scf_value_size);
316470cbfe41SPhilippe Jung 				return (err);
316570cbfe41SPhilippe Jung 
316670cbfe41SPhilippe Jung 			case EROFS:
316770cbfe41SPhilippe Jung 				log_error(LOG_WARNING, "Could not set %s/%s "
316870cbfe41SPhilippe Jung 				    "for deathrow %s: %s.\n",
316970cbfe41SPhilippe Jung 				    SCF_PG_DEATHROW, SCF_PROPERTY_DEATHROW,
317070cbfe41SPhilippe Jung 				    v->gv_name, strerror(err));
317170cbfe41SPhilippe Jung 				break;
317270cbfe41SPhilippe Jung 
317370cbfe41SPhilippe Jung 			case EPERM:
317470cbfe41SPhilippe Jung 				uu_die("Permission denied.\n");
317570cbfe41SPhilippe Jung 				/* NOTREACHED */
317670cbfe41SPhilippe Jung 
317770cbfe41SPhilippe Jung 			default:
317870cbfe41SPhilippe Jung 				bad_error("libscf_set_deathrow", err);
317970cbfe41SPhilippe Jung 			}
318070cbfe41SPhilippe Jung 			log_framework(LOG_DEBUG, "Deathrow, graph set %s.\n",
318170cbfe41SPhilippe Jung 			    v->gv_name);
318270cbfe41SPhilippe Jung 		}
318370cbfe41SPhilippe Jung 		startd_free(restarter_fmri, max_scf_value_size);
318470cbfe41SPhilippe Jung 		return (0);
318570cbfe41SPhilippe Jung 	}
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(inst);
31887c478bd9Sstevel@tonic-gate 
31897c478bd9Sstevel@tonic-gate 	/*
319070cbfe41SPhilippe Jung 	 * Using a temporary deathrow boolean property, set through
319170cbfe41SPhilippe Jung 	 * libscf_set_deathrow(), only for fmris on deathrow, is necessary
319270cbfe41SPhilippe Jung 	 * because deathrow_fini() may already have been called, and in case
319370cbfe41SPhilippe Jung 	 * of a refresh, GV_DEATHROW may need to be set again.
319470cbfe41SPhilippe Jung 	 * libscf_get_deathrow() sets deathrow to 1 only if this instance
319570cbfe41SPhilippe Jung 	 * has a temporary boolean property named 'deathrow' valued true
319670cbfe41SPhilippe Jung 	 * in a property group 'deathrow', -1 or 0 in all other cases.
319770cbfe41SPhilippe Jung 	 */
319870cbfe41SPhilippe Jung 	err = libscf_get_deathrow(h, inst, &deathrow);
319970cbfe41SPhilippe Jung 	switch (err) {
320070cbfe41SPhilippe Jung 	case 0:
320170cbfe41SPhilippe Jung 		break;
320270cbfe41SPhilippe Jung 
320370cbfe41SPhilippe Jung 	case ECONNABORTED:
320470cbfe41SPhilippe Jung 	case ECANCELED:
320570cbfe41SPhilippe Jung 		startd_free(restarter_fmri, max_scf_value_size);
320670cbfe41SPhilippe Jung 		return (err);
320770cbfe41SPhilippe Jung 
320870cbfe41SPhilippe Jung 	default:
320970cbfe41SPhilippe Jung 		bad_error("libscf_get_deathrow", err);
321070cbfe41SPhilippe Jung 	}
321170cbfe41SPhilippe Jung 
321270cbfe41SPhilippe Jung 	if (deathrow == 1) {
321370cbfe41SPhilippe Jung 		v->gv_flags |= GV_DEATHROW;
321470cbfe41SPhilippe Jung 		startd_free(restarter_fmri, max_scf_value_size);
321570cbfe41SPhilippe Jung 		return (0);
321670cbfe41SPhilippe Jung 	}
321770cbfe41SPhilippe Jung 
321870cbfe41SPhilippe Jung 	log_framework(LOG_DEBUG, "Graph adding %s.\n", v->gv_name);
321970cbfe41SPhilippe Jung 
322070cbfe41SPhilippe Jung 	/*
32217c478bd9Sstevel@tonic-gate 	 * If the instance does not have a restarter property group,
32227c478bd9Sstevel@tonic-gate 	 * initialize its state to uninitialized/none, in case the restarter
32237c478bd9Sstevel@tonic-gate 	 * is not enabled.
32247c478bd9Sstevel@tonic-gate 	 */
32257c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 	if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) != 0) {
32287c478bd9Sstevel@tonic-gate 		instance_data_t idata;
32297c478bd9Sstevel@tonic-gate 		uint_t count = 0, msecs = ALLOC_DELAY;
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
32327c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
32337c478bd9Sstevel@tonic-gate 			break;
32347c478bd9Sstevel@tonic-gate 
32357c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
32367c478bd9Sstevel@tonic-gate 		default:
32377c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
3238845e9415SRenaud Manus 			startd_free(restarter_fmri, max_scf_value_size);
32397c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
32407c478bd9Sstevel@tonic-gate 
32417c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
32427c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
3243845e9415SRenaud Manus 			startd_free(restarter_fmri, max_scf_value_size);
32447c478bd9Sstevel@tonic-gate 			return (ECANCELED);
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
32477c478bd9Sstevel@tonic-gate 			bad_error("scf_instance_get_pg", scf_error());
32487c478bd9Sstevel@tonic-gate 		}
32497c478bd9Sstevel@tonic-gate 
32507c478bd9Sstevel@tonic-gate 		switch (err = libscf_instance_get_fmri(inst,
32517c478bd9Sstevel@tonic-gate 		    (char **)&idata.i_fmri)) {
32527c478bd9Sstevel@tonic-gate 		case 0:
32537c478bd9Sstevel@tonic-gate 			break;
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
32567c478bd9Sstevel@tonic-gate 		case ECANCELED:
32577c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
3258845e9415SRenaud Manus 			startd_free(restarter_fmri, max_scf_value_size);
32597c478bd9Sstevel@tonic-gate 			return (err);
32607c478bd9Sstevel@tonic-gate 
32617c478bd9Sstevel@tonic-gate 		default:
32627c478bd9Sstevel@tonic-gate 			bad_error("libscf_instance_get_fmri", err);
32637c478bd9Sstevel@tonic-gate 		}
32647c478bd9Sstevel@tonic-gate 
32657c478bd9Sstevel@tonic-gate 		idata.i_state = RESTARTER_STATE_NONE;
32667c478bd9Sstevel@tonic-gate 		idata.i_next_state = RESTARTER_STATE_NONE;
32677c478bd9Sstevel@tonic-gate 
32687c478bd9Sstevel@tonic-gate init_state:
32697c478bd9Sstevel@tonic-gate 		switch (err = _restarter_commit_states(h, &idata,
3270f6e214c7SGavin Maltby 		    RESTARTER_STATE_UNINIT, RESTARTER_STATE_NONE,
3271f6e214c7SGavin Maltby 		    restarter_get_str_short(restarter_str_insert_in_graph))) {
32727c478bd9Sstevel@tonic-gate 		case 0:
32737c478bd9Sstevel@tonic-gate 			break;
32747c478bd9Sstevel@tonic-gate 
32757c478bd9Sstevel@tonic-gate 		case ENOMEM:
32767c478bd9Sstevel@tonic-gate 			++count;
32777c478bd9Sstevel@tonic-gate 			if (count < ALLOC_RETRY) {
32787c478bd9Sstevel@tonic-gate 				(void) poll(NULL, 0, msecs);
32797c478bd9Sstevel@tonic-gate 				msecs *= ALLOC_DELAY_MULT;
32807c478bd9Sstevel@tonic-gate 				goto init_state;
32817c478bd9Sstevel@tonic-gate 			}
32827c478bd9Sstevel@tonic-gate 
32837c478bd9Sstevel@tonic-gate 			uu_die("Insufficient memory.\n");
32847c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
32857c478bd9Sstevel@tonic-gate 
32867c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
32875b6c5443Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
32887c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
3289845e9415SRenaud Manus 			startd_free(restarter_fmri, max_scf_value_size);
32907c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
32917c478bd9Sstevel@tonic-gate 
32927c478bd9Sstevel@tonic-gate 		case ENOENT:
32935b6c5443Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
32947c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
3295845e9415SRenaud Manus 			startd_free(restarter_fmri, max_scf_value_size);
32967c478bd9Sstevel@tonic-gate 			return (ECANCELED);
32977c478bd9Sstevel@tonic-gate 
32987c478bd9Sstevel@tonic-gate 		case EPERM:
32997c478bd9Sstevel@tonic-gate 		case EACCES:
33007c478bd9Sstevel@tonic-gate 		case EROFS:
33017c478bd9Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not initialize state for "
33027c478bd9Sstevel@tonic-gate 			    "%s: %s.\n", idata.i_fmri, strerror(err));
33037c478bd9Sstevel@tonic-gate 			break;
33047c478bd9Sstevel@tonic-gate 
33057c478bd9Sstevel@tonic-gate 		case EINVAL:
33067c478bd9Sstevel@tonic-gate 		default:
33077c478bd9Sstevel@tonic-gate 			bad_error("_restarter_commit_states", err);
33087c478bd9Sstevel@tonic-gate 		}
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate 		startd_free((void *)idata.i_fmri, max_scf_fmri_size);
33117c478bd9Sstevel@tonic-gate 	}
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
33147c478bd9Sstevel@tonic-gate 
33157c478bd9Sstevel@tonic-gate 	if (milestone != NULL) {
33167c478bd9Sstevel@tonic-gate 		/*
33177c478bd9Sstevel@tonic-gate 		 * Make sure the enable-override is set properly before we
33187c478bd9Sstevel@tonic-gate 		 * read whether we should be enabled.
33197c478bd9Sstevel@tonic-gate 		 */
33207c478bd9Sstevel@tonic-gate 		if (milestone == MILESTONE_NONE ||
33217c478bd9Sstevel@tonic-gate 		    !(v->gv_flags & GV_INSUBGRAPH)) {
332256e23938Sbustos 			/*
332356e23938Sbustos 			 * This might seem unjustified after the milestone
332456e23938Sbustos 			 * transition has completed (non_subgraph_svcs == 0),
332556e23938Sbustos 			 * but it's important because when we boot to
332656e23938Sbustos 			 * a milestone, we set the milestone before populating
332756e23938Sbustos 			 * the graph, and all of the new non-subgraph services
332856e23938Sbustos 			 * need to be disabled here.
332956e23938Sbustos 			 */
33307c478bd9Sstevel@tonic-gate 			switch (err = libscf_set_enable_ovr(inst, 0)) {
33317c478bd9Sstevel@tonic-gate 			case 0:
33327c478bd9Sstevel@tonic-gate 				break;
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
33357c478bd9Sstevel@tonic-gate 			case ECANCELED:
3336845e9415SRenaud Manus 				startd_free(restarter_fmri, max_scf_value_size);
33377c478bd9Sstevel@tonic-gate 				return (err);
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate 			case EROFS:
33407c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING,
33417c478bd9Sstevel@tonic-gate 				    "Could not set %s/%s for %s: %s.\n",
33427c478bd9Sstevel@tonic-gate 				    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
33437c478bd9Sstevel@tonic-gate 				    v->gv_name, strerror(err));
33447c478bd9Sstevel@tonic-gate 				break;
33457c478bd9Sstevel@tonic-gate 
33467c478bd9Sstevel@tonic-gate 			case EPERM:
33477c478bd9Sstevel@tonic-gate 				uu_die("Permission denied.\n");
33487c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate 			default:
33517c478bd9Sstevel@tonic-gate 				bad_error("libscf_set_enable_ovr", err);
33527c478bd9Sstevel@tonic-gate 			}
33537c478bd9Sstevel@tonic-gate 		} else {
33547c478bd9Sstevel@tonic-gate 			assert(v->gv_flags & GV_INSUBGRAPH);
33557c478bd9Sstevel@tonic-gate 			switch (err = libscf_delete_enable_ovr(inst)) {
33567c478bd9Sstevel@tonic-gate 			case 0:
33577c478bd9Sstevel@tonic-gate 				break;
33587c478bd9Sstevel@tonic-gate 
33597c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
33607c478bd9Sstevel@tonic-gate 			case ECANCELED:
3361845e9415SRenaud Manus 				startd_free(restarter_fmri, max_scf_value_size);
33627c478bd9Sstevel@tonic-gate 				return (err);
33637c478bd9Sstevel@tonic-gate 
33647c478bd9Sstevel@tonic-gate 			case EPERM:
33657c478bd9Sstevel@tonic-gate 				uu_die("Permission denied.\n");
33667c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
33677c478bd9Sstevel@tonic-gate 
33687c478bd9Sstevel@tonic-gate 			default:
33697c478bd9Sstevel@tonic-gate 				bad_error("libscf_delete_enable_ovr", err);
33707c478bd9Sstevel@tonic-gate 			}
33717c478bd9Sstevel@tonic-gate 		}
33727c478bd9Sstevel@tonic-gate 	}
33737c478bd9Sstevel@tonic-gate 
33747c478bd9Sstevel@tonic-gate 	err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled,
33757c478bd9Sstevel@tonic-gate 	    &enabled_ovr, &restarter_fmri);
33767c478bd9Sstevel@tonic-gate 	switch (err) {
33777c478bd9Sstevel@tonic-gate 	case 0:
33787c478bd9Sstevel@tonic-gate 		break;
33797c478bd9Sstevel@tonic-gate 
33807c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
33817c478bd9Sstevel@tonic-gate 	case ECANCELED:
33827c478bd9Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
33837c478bd9Sstevel@tonic-gate 		return (err);
33847c478bd9Sstevel@tonic-gate 
33857c478bd9Sstevel@tonic-gate 	case ENOENT:
33867c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
33877c478bd9Sstevel@tonic-gate 		    "Ignoring %s because it has no general property group.\n",
33887c478bd9Sstevel@tonic-gate 		    v->gv_name);
33897c478bd9Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
33907c478bd9Sstevel@tonic-gate 		return (0);
33917c478bd9Sstevel@tonic-gate 
33927c478bd9Sstevel@tonic-gate 	default:
33937c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_basic_instance_data", err);
33947c478bd9Sstevel@tonic-gate 	}
33957c478bd9Sstevel@tonic-gate 
3396f6e214c7SGavin Maltby 	if ((tset = libscf_get_stn_tset(inst)) == -1) {
3397f6e214c7SGavin Maltby 		log_framework(LOG_WARNING,
3398f6e214c7SGavin Maltby 		    "Failed to get notification parameters for %s: %s\n",
3399f6e214c7SGavin Maltby 		    v->gv_name, scf_strerror(scf_error()));
3400f6e214c7SGavin Maltby 		v->gv_stn_tset = 0;
3401f6e214c7SGavin Maltby 	} else {
3402f6e214c7SGavin Maltby 		v->gv_stn_tset = tset;
3403f6e214c7SGavin Maltby 	}
3404f6e214c7SGavin Maltby 	if (strcmp(v->gv_name, SCF_INSTANCE_GLOBAL) == 0)
3405f6e214c7SGavin Maltby 		stn_global = v->gv_stn_tset;
3406f6e214c7SGavin Maltby 
34077c478bd9Sstevel@tonic-gate 	if (enabled == -1) {
34087c478bd9Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
34097c478bd9Sstevel@tonic-gate 		return (0);
34107c478bd9Sstevel@tonic-gate 	}
34117c478bd9Sstevel@tonic-gate 
34127c478bd9Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) |
34137c478bd9Sstevel@tonic-gate 	    (enabled ? GV_ENBLD_NOOVR : 0);
34147c478bd9Sstevel@tonic-gate 
34157c478bd9Sstevel@tonic-gate 	if (enabled_ovr != -1)
34167c478bd9Sstevel@tonic-gate 		enabled = enabled_ovr;
34177c478bd9Sstevel@tonic-gate 
34187c478bd9Sstevel@tonic-gate 	v->gv_state = RESTARTER_STATE_UNINIT;
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate 	snap = libscf_get_or_make_running_snapshot(inst, v->gv_name, B_TRUE);
34217c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
34227c478bd9Sstevel@tonic-gate 
34237c478bd9Sstevel@tonic-gate 	/* Set up the restarter. (Sends _ADD_INSTANCE on success.) */
34247c478bd9Sstevel@tonic-gate 	err = graph_change_restarter(v, restarter_fmri, h, &path);
34257c478bd9Sstevel@tonic-gate 	if (err != 0) {
34267c478bd9Sstevel@tonic-gate 		instance_data_t idata;
34277c478bd9Sstevel@tonic-gate 		uint_t count = 0, msecs = ALLOC_DELAY;
3428f6e214c7SGavin Maltby 		restarter_str_t reason;
34297c478bd9Sstevel@tonic-gate 
34307c478bd9Sstevel@tonic-gate 		if (err == ECONNABORTED) {
34317c478bd9Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
34327c478bd9Sstevel@tonic-gate 			return (err);
34337c478bd9Sstevel@tonic-gate 		}
34347c478bd9Sstevel@tonic-gate 
34357c478bd9Sstevel@tonic-gate 		assert(err == EINVAL || err == ELOOP);
34367c478bd9Sstevel@tonic-gate 
34377c478bd9Sstevel@tonic-gate 		if (err == EINVAL) {
343899b44c3bSlianep 			log_framework(LOG_ERR, emsg_invalid_restarter,
343913d8aaa1SSean Wilcox 			    v->gv_name, restarter_fmri);
3440f6e214c7SGavin Maltby 			reason = restarter_str_invalid_restarter;
34417c478bd9Sstevel@tonic-gate 		} else {
34427c478bd9Sstevel@tonic-gate 			handle_cycle(v->gv_name, path);
3443f6e214c7SGavin Maltby 			reason = restarter_str_dependency_cycle;
34447c478bd9Sstevel@tonic-gate 		}
34457c478bd9Sstevel@tonic-gate 
34467c478bd9Sstevel@tonic-gate 		startd_free(restarter_fmri, max_scf_value_size);
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate 		/*
34497c478bd9Sstevel@tonic-gate 		 * We didn't register the instance with the restarter, so we
34507c478bd9Sstevel@tonic-gate 		 * must set maintenance mode ourselves.
34517c478bd9Sstevel@tonic-gate 		 */
34527c478bd9Sstevel@tonic-gate 		err = libscf_instance_get_fmri(inst, (char **)&idata.i_fmri);
34537c478bd9Sstevel@tonic-gate 		if (err != 0) {
34547c478bd9Sstevel@tonic-gate 			assert(err == ECONNABORTED || err == ECANCELED);
34557c478bd9Sstevel@tonic-gate 			return (err);
34567c478bd9Sstevel@tonic-gate 		}
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate 		idata.i_state = RESTARTER_STATE_NONE;
34597c478bd9Sstevel@tonic-gate 		idata.i_next_state = RESTARTER_STATE_NONE;
34607c478bd9Sstevel@tonic-gate 
34617c478bd9Sstevel@tonic-gate set_maint:
34627c478bd9Sstevel@tonic-gate 		switch (err = _restarter_commit_states(h, &idata,
3463f6e214c7SGavin Maltby 		    RESTARTER_STATE_MAINT, RESTARTER_STATE_NONE,
3464f6e214c7SGavin Maltby 		    restarter_get_str_short(reason))) {
34657c478bd9Sstevel@tonic-gate 		case 0:
34667c478bd9Sstevel@tonic-gate 			break;
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate 		case ENOMEM:
34697c478bd9Sstevel@tonic-gate 			++count;
34707c478bd9Sstevel@tonic-gate 			if (count < ALLOC_RETRY) {
34717c478bd9Sstevel@tonic-gate 				(void) poll(NULL, 0, msecs);
34727c478bd9Sstevel@tonic-gate 				msecs *= ALLOC_DELAY_MULT;
34737c478bd9Sstevel@tonic-gate 				goto set_maint;
34747c478bd9Sstevel@tonic-gate 			}
34757c478bd9Sstevel@tonic-gate 
34767c478bd9Sstevel@tonic-gate 			uu_die("Insufficient memory.\n");
34777c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
34787c478bd9Sstevel@tonic-gate 
34797c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
34805b6c5443Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
34817c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
34827c478bd9Sstevel@tonic-gate 
34837c478bd9Sstevel@tonic-gate 		case ENOENT:
34845b6c5443Srm88369 			startd_free((void *)idata.i_fmri, max_scf_fmri_size);
34857c478bd9Sstevel@tonic-gate 			return (ECANCELED);
34867c478bd9Sstevel@tonic-gate 
34877c478bd9Sstevel@tonic-gate 		case EPERM:
34887c478bd9Sstevel@tonic-gate 		case EACCES:
34897c478bd9Sstevel@tonic-gate 		case EROFS:
34907c478bd9Sstevel@tonic-gate 			log_error(LOG_NOTICE, "Could not initialize state for "
34917c478bd9Sstevel@tonic-gate 			    "%s: %s.\n", idata.i_fmri, strerror(err));
34927c478bd9Sstevel@tonic-gate 			break;
34937c478bd9Sstevel@tonic-gate 
34947c478bd9Sstevel@tonic-gate 		case EINVAL:
34957c478bd9Sstevel@tonic-gate 		default:
34967c478bd9Sstevel@tonic-gate 			bad_error("_restarter_commit_states", err);
34977c478bd9Sstevel@tonic-gate 		}
34987c478bd9Sstevel@tonic-gate 
34997c478bd9Sstevel@tonic-gate 		startd_free((void *)idata.i_fmri, max_scf_fmri_size);
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate 		v->gv_state = RESTARTER_STATE_MAINT;
35027c478bd9Sstevel@tonic-gate 
35037c478bd9Sstevel@tonic-gate 		goto out;
35047c478bd9Sstevel@tonic-gate 	}
35057c478bd9Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_value_size);
35067c478bd9Sstevel@tonic-gate 
35077c478bd9Sstevel@tonic-gate 	/* Add all the other dependencies. */
35087c478bd9Sstevel@tonic-gate 	err = refresh_vertex(v, inst);
35097c478bd9Sstevel@tonic-gate 	if (err != 0) {
35107c478bd9Sstevel@tonic-gate 		assert(err == ECONNABORTED);
35117c478bd9Sstevel@tonic-gate 		return (err);
35127c478bd9Sstevel@tonic-gate 	}
35137c478bd9Sstevel@tonic-gate 
35147c478bd9Sstevel@tonic-gate out:
35157c478bd9Sstevel@tonic-gate 	v->gv_flags |= GV_CONFIGURED;
35167c478bd9Sstevel@tonic-gate 
35177c478bd9Sstevel@tonic-gate 	graph_enable_by_vertex(v, enabled, 0);
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate 	return (0);
35207c478bd9Sstevel@tonic-gate }
35217c478bd9Sstevel@tonic-gate 
35224d53c7adSDan Price 
35234d53c7adSDan Price static void
kill_user_procs(void)35244d53c7adSDan Price kill_user_procs(void)
35254d53c7adSDan Price {
35264d53c7adSDan Price 	(void) fputs("svc.startd: Killing user processes.\n", stdout);
35274d53c7adSDan Price 
35284d53c7adSDan Price 	/*
35294d53c7adSDan Price 	 * Despite its name, killall's role is to get select user processes--
35304d53c7adSDan Price 	 * basically those representing terminal-based logins-- to die.  Victims
35314d53c7adSDan Price 	 * are located by killall in the utmp database.  Since these are most
35324d53c7adSDan Price 	 * often shell based logins, and many shells mask SIGTERM (but are
35334d53c7adSDan Price 	 * responsive to SIGHUP) we first HUP and then shortly thereafter
35344d53c7adSDan Price 	 * kill -9.
35354d53c7adSDan Price 	 */
35364d53c7adSDan Price 	(void) fork_with_timeout("/usr/sbin/killall HUP", 1, 5);
35374d53c7adSDan Price 	(void) fork_with_timeout("/usr/sbin/killall KILL", 1, 5);
35384d53c7adSDan Price 
35394d53c7adSDan Price 	/*
35404d53c7adSDan Price 	 * Note the selection of user id's 0, 1 and 15, subsequently
35414d53c7adSDan Price 	 * inverted by -v.  15 is reserved for dladmd.  Yes, this is a
35424d53c7adSDan Price 	 * kludge-- a better policy is needed.
35434d53c7adSDan Price 	 *
35444d53c7adSDan Price 	 * Note that fork_with_timeout will only wait out the 1 second
35454d53c7adSDan Price 	 * "grace time" if pkill actually returns 0.  So if there are
35464d53c7adSDan Price 	 * no matches, this will run to completion much more quickly.
35474d53c7adSDan Price 	 */
35484d53c7adSDan Price 	(void) fork_with_timeout("/usr/bin/pkill -TERM -v -u 0,1,15", 1, 5);
35494d53c7adSDan Price 	(void) fork_with_timeout("/usr/bin/pkill -KILL -v -u 0,1,15", 1, 5);
35504d53c7adSDan Price }
35514d53c7adSDan Price 
35527c478bd9Sstevel@tonic-gate static void
do_uadmin(void)35537c478bd9Sstevel@tonic-gate do_uadmin(void)
35547c478bd9Sstevel@tonic-gate {
3555753a6d45SSherry Moore 	const char * const resetting = "/etc/svc/volatile/resetting";
35564d53c7adSDan Price 	int fd;
35577c478bd9Sstevel@tonic-gate 	struct statvfs vfs;
35584d53c7adSDan Price 	time_t now;
35594d53c7adSDan Price 	struct tm nowtm;
35604d53c7adSDan Price 	char down_buf[256], time_buf[256];
3561753a6d45SSherry Moore 	uintptr_t mdep;
3562a6424c75SToomas Soome #if defined(__x86)
3563a6424c75SToomas Soome 	char *fbarg = NULL;
3564a6424c75SToomas Soome #endif	/* __x86 */
35657c478bd9Sstevel@tonic-gate 
35665f8fc372SToomas Soome 	mdep = 0;
35677c478bd9Sstevel@tonic-gate 	fd = creat(resetting, 0777);
35687c478bd9Sstevel@tonic-gate 	if (fd >= 0)
35697c478bd9Sstevel@tonic-gate 		startd_close(fd);
35707c478bd9Sstevel@tonic-gate 	else
35717c478bd9Sstevel@tonic-gate 		uu_warn("Could not create \"%s\"", resetting);
35727c478bd9Sstevel@tonic-gate 
35737c478bd9Sstevel@tonic-gate 	/* Kill dhcpagent if we're not using nfs for root */
35747c478bd9Sstevel@tonic-gate 	if ((statvfs("/", &vfs) == 0) &&
35757c478bd9Sstevel@tonic-gate 	    (strncmp(vfs.f_basetype, "nfs", sizeof ("nfs") - 1) != 0))
35764d53c7adSDan Price 		fork_with_timeout("/usr/bin/pkill -x -u 0 dhcpagent", 0, 5);
35777c478bd9Sstevel@tonic-gate 
35784d53c7adSDan Price 	/*
35794d53c7adSDan Price 	 * Call sync(2) now, before we kill off user processes.  This takes
35804d53c7adSDan Price 	 * advantage of the several seconds of pause we have before the
35814d53c7adSDan Price 	 * killalls are done.  Time we can make good use of to get pages
35824d53c7adSDan Price 	 * moving out to disk.
35834d53c7adSDan Price 	 *
35844d53c7adSDan Price 	 * Inside non-global zones, we don't bother, and it's better not to
35854d53c7adSDan Price 	 * anyway, since sync(2) can have system-wide impact.
35864d53c7adSDan Price 	 */
35874d53c7adSDan Price 	if (getzoneid() == 0)
35887c478bd9Sstevel@tonic-gate 		sync();
35897c478bd9Sstevel@tonic-gate 
35904d53c7adSDan Price 	kill_user_procs();
35917c478bd9Sstevel@tonic-gate 
35924d53c7adSDan Price 	/*
35934d53c7adSDan Price 	 * Note that this must come after the killing of user procs, since
35944d53c7adSDan Price 	 * killall relies on utmpx, and this command affects the contents of
35954d53c7adSDan Price 	 * said file.
35964d53c7adSDan Price 	 */
35974d53c7adSDan Price 	if (access("/usr/lib/acct/closewtmp", X_OK) == 0)
35984d53c7adSDan Price 		fork_with_timeout("/usr/lib/acct/closewtmp", 0, 5);
35994d53c7adSDan Price 
36004d53c7adSDan Price 	/*
36014d53c7adSDan Price 	 * For patches which may be installed as the system is shutting
36024d53c7adSDan Price 	 * down, we need to ensure, one more time, that the boot archive
36034d53c7adSDan Price 	 * really is up to date.
36044d53c7adSDan Price 	 */
36054d53c7adSDan Price 	if (getzoneid() == 0 && access("/usr/sbin/bootadm", X_OK) == 0)
36064d53c7adSDan Price 		fork_with_timeout("/usr/sbin/bootadm -ea update_all", 0, 3600);
36074d53c7adSDan Price 
36087f84ffd0SSherry Moore 	/*
36097f84ffd0SSherry Moore 	 * Right now, fast reboot is supported only on i386.
36107f84ffd0SSherry Moore 	 * scf_is_fastboot_default() should take care of it.
36117f84ffd0SSherry Moore 	 * If somehow we got there on unsupported platform -
36127f84ffd0SSherry Moore 	 * print warning and fall back to regular reboot.
36137f84ffd0SSherry Moore 	 */
36147f84ffd0SSherry Moore 	if (halting == AD_FASTREBOOT) {
3615a6424c75SToomas Soome #if defined(__x86)
3616a6424c75SToomas Soome 		if (be_get_boot_args(&fbarg, BE_ENTRY_DEFAULT) == 0) {
3617a6424c75SToomas Soome 			mdep = (uintptr_t)fbarg;
36187f84ffd0SSherry Moore 		} else {
36197f84ffd0SSherry Moore 			/*
3620a6424c75SToomas Soome 			 * Failed to read BE info, fall back to normal reboot
36217f84ffd0SSherry Moore 			 */
36227f84ffd0SSherry Moore 			halting = AD_BOOT;
3623a6424c75SToomas Soome 			uu_warn("Failed to get fast reboot arguments.\n"
3624a6424c75SToomas Soome 			    "Falling back to regular reboot.\n");
36257f84ffd0SSherry Moore 		}
3626a6424c75SToomas Soome #else	/* __x86 */
36277f84ffd0SSherry Moore 		halting = AD_BOOT;
36287f84ffd0SSherry Moore 		uu_warn("Fast reboot configured, but not supported by "
36297f84ffd0SSherry Moore 		    "this ISA\n");
3630a6424c75SToomas Soome #endif	/* __x86 */
36317f84ffd0SSherry Moore 	}
36327f84ffd0SSherry Moore 
36334d53c7adSDan Price 	fork_with_timeout("/sbin/umountall -l", 0, 5);
36344d53c7adSDan Price 	fork_with_timeout("/sbin/umount /tmp /var/adm /var/run /var "
36354d53c7adSDan Price 	    ">/dev/null 2>&1", 0, 5);
36364d53c7adSDan Price 
36374d53c7adSDan Price 	/*
36384d53c7adSDan Price 	 * Try to get to consistency for whatever UFS filesystems are left.
36394d53c7adSDan Price 	 * This is pretty expensive, so we save it for the end in the hopes of
36404d53c7adSDan Price 	 * minimizing what it must do.  The other option would be to start in
36414d53c7adSDan Price 	 * parallel with the killall's, but lockfs tends to throw out much more
36424d53c7adSDan Price 	 * than is needed, and so subsequent commands (like umountall) take a
36434d53c7adSDan Price 	 * long time to get going again.
36444d53c7adSDan Price 	 *
36454d53c7adSDan Price 	 * Inside of zones, we don't bother, since we're not about to terminate
36464d53c7adSDan Price 	 * the whole OS instance.
36474d53c7adSDan Price 	 *
36484d53c7adSDan Price 	 * On systems using only ZFS, this call to lockfs -fa is a no-op.
36494d53c7adSDan Price 	 */
36504d53c7adSDan Price 	if (getzoneid() == 0) {
36514d53c7adSDan Price 		if (access("/usr/sbin/lockfs", X_OK) == 0)
36524d53c7adSDan Price 			fork_with_timeout("/usr/sbin/lockfs -fa", 0, 30);
36534d53c7adSDan Price 
36544d53c7adSDan Price 		sync();	/* once more, with feeling */
36554d53c7adSDan Price 	}
36564d53c7adSDan Price 
36574d53c7adSDan Price 	fork_with_timeout("/sbin/umount /usr >/dev/null 2>&1", 0, 5);
36584d53c7adSDan Price 
36594d53c7adSDan Price 	/*
36604d53c7adSDan Price 	 * Construct and emit the last words from userland:
36614d53c7adSDan Price 	 * "<timestamp> The system is down.  Shutdown took <N> seconds."
36624d53c7adSDan Price 	 *
36634d53c7adSDan Price 	 * Normally we'd use syslog, but with /var and other things
36644d53c7adSDan Price 	 * potentially gone, try to minimize the external dependencies.
36654d53c7adSDan Price 	 */
36664d53c7adSDan Price 	now = time(NULL);
36674d53c7adSDan Price 	(void) localtime_r(&now, &nowtm);
36684d53c7adSDan Price 
36694d53c7adSDan Price 	if (strftime(down_buf, sizeof (down_buf),
36704d53c7adSDan Price 	    "%b %e %T The system is down.", &nowtm) == 0) {
36714d53c7adSDan Price 		(void) strlcpy(down_buf, "The system is down.",
36724d53c7adSDan Price 		    sizeof (down_buf));
36734d53c7adSDan Price 	}
36744d53c7adSDan Price 
36754d53c7adSDan Price 	if (halting_time != 0 && halting_time <= now) {
36764d53c7adSDan Price 		(void) snprintf(time_buf, sizeof (time_buf),
36774d53c7adSDan Price 		    "  Shutdown took %lu seconds.", now - halting_time);
36784d53c7adSDan Price 	} else {
36794d53c7adSDan Price 		time_buf[0] = '\0';
36804d53c7adSDan Price 	}
36814d53c7adSDan Price 	(void) printf("%s%s\n", down_buf, time_buf);
36827c478bd9Sstevel@tonic-gate 
3683753a6d45SSherry Moore 	(void) uadmin(A_SHUTDOWN, halting, mdep);
36847c478bd9Sstevel@tonic-gate 	uu_warn("uadmin() failed");
36857c478bd9Sstevel@tonic-gate 
3686a6424c75SToomas Soome #if defined(__x86)
3687753a6d45SSherry Moore 	if (halting == AD_FASTREBOOT)
3688a6424c75SToomas Soome 		free(fbarg);
3689a6424c75SToomas Soome #endif	/* __x86 */
3690753a6d45SSherry Moore 
36917c478bd9Sstevel@tonic-gate 	if (remove(resetting) != 0 && errno != ENOENT)
36927c478bd9Sstevel@tonic-gate 		uu_warn("Could not remove \"%s\"", resetting);
36937c478bd9Sstevel@tonic-gate }
36947c478bd9Sstevel@tonic-gate 
36957c478bd9Sstevel@tonic-gate /*
36967c478bd9Sstevel@tonic-gate  * If any of the up_svcs[] are online or satisfiable, return true.  If they are
36977c478bd9Sstevel@tonic-gate  * all missing, disabled, in maintenance, or unsatisfiable, return false.
36987c478bd9Sstevel@tonic-gate  */
36997c478bd9Sstevel@tonic-gate boolean_t
can_come_up(void)37007c478bd9Sstevel@tonic-gate can_come_up(void)
37017c478bd9Sstevel@tonic-gate {
37027c478bd9Sstevel@tonic-gate 	int i;
37037c478bd9Sstevel@tonic-gate 
370453f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
37057c478bd9Sstevel@tonic-gate 
37067c478bd9Sstevel@tonic-gate 	/*
37077c478bd9Sstevel@tonic-gate 	 * If we are booting to single user (boot -s),
37087c478bd9Sstevel@tonic-gate 	 * SCF_MILESTONE_SINGLE_USER is needed to come up because startd
37097c478bd9Sstevel@tonic-gate 	 * spawns sulogin after single-user is online (see specials.c).
37107c478bd9Sstevel@tonic-gate 	 */
37117c478bd9Sstevel@tonic-gate 	i = (booting_to_single_user ? 0 : 1);
37127c478bd9Sstevel@tonic-gate 
37137c478bd9Sstevel@tonic-gate 	for (; up_svcs[i] != NULL; ++i) {
37147c478bd9Sstevel@tonic-gate 		if (up_svcs_p[i] == NULL) {
37157c478bd9Sstevel@tonic-gate 			up_svcs_p[i] = vertex_get_by_name(up_svcs[i]);
37167c478bd9Sstevel@tonic-gate 
37177c478bd9Sstevel@tonic-gate 			if (up_svcs_p[i] == NULL)
37187c478bd9Sstevel@tonic-gate 				continue;
37197c478bd9Sstevel@tonic-gate 		}
37207c478bd9Sstevel@tonic-gate 
37217c478bd9Sstevel@tonic-gate 		/*
37227c478bd9Sstevel@tonic-gate 		 * Ignore unconfigured services (the ones that have been
37237c478bd9Sstevel@tonic-gate 		 * mentioned in a dependency from other services, but do
37247c478bd9Sstevel@tonic-gate 		 * not exist in the repository).  Services which exist
37257c478bd9Sstevel@tonic-gate 		 * in the repository but don't have general/enabled
37267c478bd9Sstevel@tonic-gate 		 * property will be also ignored.
37277c478bd9Sstevel@tonic-gate 		 */
37287c478bd9Sstevel@tonic-gate 		if (!(up_svcs_p[i]->gv_flags & GV_CONFIGURED))
37297c478bd9Sstevel@tonic-gate 			continue;
37307c478bd9Sstevel@tonic-gate 
37317c478bd9Sstevel@tonic-gate 		switch (up_svcs_p[i]->gv_state) {
37327c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_ONLINE:
37337c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_DEGRADED:
37347c478bd9Sstevel@tonic-gate 			/*
37357c478bd9Sstevel@tonic-gate 			 * Deactivate verbose boot once a login service has been
37367c478bd9Sstevel@tonic-gate 			 * reached.
37377c478bd9Sstevel@tonic-gate 			 */
37387c478bd9Sstevel@tonic-gate 			st->st_log_login_reached = 1;
37397c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
37407c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_UNINIT:
37417c478bd9Sstevel@tonic-gate 			return (B_TRUE);
37427c478bd9Sstevel@tonic-gate 
37437c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_OFFLINE:
37447c478bd9Sstevel@tonic-gate 			if (instance_satisfied(up_svcs_p[i], B_TRUE) != -1)
37457c478bd9Sstevel@tonic-gate 				return (B_TRUE);
37467c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
37477c478bd9Sstevel@tonic-gate 			    "can_come_up(): %s is unsatisfiable.\n",
37487c478bd9Sstevel@tonic-gate 			    up_svcs_p[i]->gv_name);
37497c478bd9Sstevel@tonic-gate 			continue;
37507c478bd9Sstevel@tonic-gate 
37517c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_DISABLED:
37527c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_MAINT:
37537c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
37547c478bd9Sstevel@tonic-gate 			    "can_come_up(): %s is in state %s.\n",
37557c478bd9Sstevel@tonic-gate 			    up_svcs_p[i]->gv_name,
37567c478bd9Sstevel@tonic-gate 			    instance_state_str[up_svcs_p[i]->gv_state]);
37577c478bd9Sstevel@tonic-gate 			continue;
37587c478bd9Sstevel@tonic-gate 
37597c478bd9Sstevel@tonic-gate 		default:
37607c478bd9Sstevel@tonic-gate #ifndef NDEBUG
37617c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Unexpected vertex state %d.\n",
37627c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__, up_svcs_p[i]->gv_state);
37637c478bd9Sstevel@tonic-gate #endif
37647c478bd9Sstevel@tonic-gate 			abort();
37657c478bd9Sstevel@tonic-gate 		}
37667c478bd9Sstevel@tonic-gate 	}
37677c478bd9Sstevel@tonic-gate 
37687c478bd9Sstevel@tonic-gate 	/*
37697c478bd9Sstevel@tonic-gate 	 * In the seed repository, console-login is unsatisfiable because
37707c478bd9Sstevel@tonic-gate 	 * services are missing.  To behave correctly in that case we don't want
37717c478bd9Sstevel@tonic-gate 	 * to return false until manifest-import is online.
37727c478bd9Sstevel@tonic-gate 	 */
37737c478bd9Sstevel@tonic-gate 
37747c478bd9Sstevel@tonic-gate 	if (manifest_import_p == NULL) {
37757c478bd9Sstevel@tonic-gate 		manifest_import_p = vertex_get_by_name(manifest_import);
37767c478bd9Sstevel@tonic-gate 
37777c478bd9Sstevel@tonic-gate 		if (manifest_import_p == NULL)
37787c478bd9Sstevel@tonic-gate 			return (B_FALSE);
37797c478bd9Sstevel@tonic-gate 	}
37807c478bd9Sstevel@tonic-gate 
37817c478bd9Sstevel@tonic-gate 	switch (manifest_import_p->gv_state) {
37827c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_ONLINE:
37837c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_DEGRADED:
37847c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_DISABLED:
37857c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_MAINT:
37867c478bd9Sstevel@tonic-gate 		break;
37877c478bd9Sstevel@tonic-gate 
37887c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_OFFLINE:
37897c478bd9Sstevel@tonic-gate 		if (instance_satisfied(manifest_import_p, B_TRUE) == -1)
37907c478bd9Sstevel@tonic-gate 			break;
37917c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
37927c478bd9Sstevel@tonic-gate 
37937c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_UNINIT:
37947c478bd9Sstevel@tonic-gate 		return (B_TRUE);
37957c478bd9Sstevel@tonic-gate 	}
37967c478bd9Sstevel@tonic-gate 
37977c478bd9Sstevel@tonic-gate 	return (B_FALSE);
37987c478bd9Sstevel@tonic-gate }
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate /*
38017c478bd9Sstevel@tonic-gate  * Runs sulogin.  Returns
38027c478bd9Sstevel@tonic-gate  *   0 - success
38037c478bd9Sstevel@tonic-gate  *   EALREADY - sulogin is already running
38047c478bd9Sstevel@tonic-gate  *   EBUSY - console-login is running
38057c478bd9Sstevel@tonic-gate  */
38067c478bd9Sstevel@tonic-gate static int
run_sulogin(const char * msg)38077c478bd9Sstevel@tonic-gate run_sulogin(const char *msg)
38087c478bd9Sstevel@tonic-gate {
38097c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
38107c478bd9Sstevel@tonic-gate 
381153f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
38127c478bd9Sstevel@tonic-gate 
38137c478bd9Sstevel@tonic-gate 	if (sulogin_running)
38147c478bd9Sstevel@tonic-gate 		return (EALREADY);
38157c478bd9Sstevel@tonic-gate 
38167c478bd9Sstevel@tonic-gate 	v = vertex_get_by_name(console_login_fmri);
38177c478bd9Sstevel@tonic-gate 	if (v != NULL && inst_running(v))
38187c478bd9Sstevel@tonic-gate 		return (EBUSY);
38197c478bd9Sstevel@tonic-gate 
38207c478bd9Sstevel@tonic-gate 	sulogin_running = B_TRUE;
38217c478bd9Sstevel@tonic-gate 
38227c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
38237c478bd9Sstevel@tonic-gate 
38247c478bd9Sstevel@tonic-gate 	fork_sulogin(B_FALSE, msg);
38257c478bd9Sstevel@tonic-gate 
38267c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
38277c478bd9Sstevel@tonic-gate 
38287c478bd9Sstevel@tonic-gate 	sulogin_running = B_FALSE;
38297c478bd9Sstevel@tonic-gate 
38307c478bd9Sstevel@tonic-gate 	if (console_login_ready) {
38317c478bd9Sstevel@tonic-gate 		v = vertex_get_by_name(console_login_fmri);
38327c478bd9Sstevel@tonic-gate 
3833845e9415SRenaud Manus 		if (v != NULL && v->gv_state == RESTARTER_STATE_OFFLINE) {
38347c478bd9Sstevel@tonic-gate 			if (v->gv_start_f == NULL)
38357c478bd9Sstevel@tonic-gate 				vertex_send_event(v,
38367c478bd9Sstevel@tonic-gate 				    RESTARTER_EVENT_TYPE_START);
38377c478bd9Sstevel@tonic-gate 			else
38387c478bd9Sstevel@tonic-gate 				v->gv_start_f(v);
38397c478bd9Sstevel@tonic-gate 		}
38407c478bd9Sstevel@tonic-gate 
38417c478bd9Sstevel@tonic-gate 		console_login_ready = B_FALSE;
38427c478bd9Sstevel@tonic-gate 	}
38437c478bd9Sstevel@tonic-gate 
38447c478bd9Sstevel@tonic-gate 	return (0);
38457c478bd9Sstevel@tonic-gate }
38467c478bd9Sstevel@tonic-gate 
38477c478bd9Sstevel@tonic-gate /*
38487c478bd9Sstevel@tonic-gate  * The sulogin thread runs sulogin while can_come_up() is false.  run_sulogin()
38497c478bd9Sstevel@tonic-gate  * keeps sulogin from stepping on console-login's toes.
38507c478bd9Sstevel@tonic-gate  */
38517c478bd9Sstevel@tonic-gate /* ARGSUSED */
38527c478bd9Sstevel@tonic-gate static void *
sulogin_thread(void * unused)38537c478bd9Sstevel@tonic-gate sulogin_thread(void *unused)
38547c478bd9Sstevel@tonic-gate {
3855ab618543SJohn Levon 	(void) pthread_setname_np(pthread_self(), "sulogin");
3856ab618543SJohn Levon 
38577c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
38587c478bd9Sstevel@tonic-gate 
38597c478bd9Sstevel@tonic-gate 	assert(sulogin_thread_running);
38607c478bd9Sstevel@tonic-gate 
38613eae19d9Swesolows 	do {
38627c478bd9Sstevel@tonic-gate 		(void) run_sulogin("Console login service(s) cannot run\n");
38633eae19d9Swesolows 	} while (!can_come_up());
38647c478bd9Sstevel@tonic-gate 
38657c478bd9Sstevel@tonic-gate 	sulogin_thread_running = B_FALSE;
38667c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
38677c478bd9Sstevel@tonic-gate 
38687c478bd9Sstevel@tonic-gate 	return (NULL);
38697c478bd9Sstevel@tonic-gate }
38707c478bd9Sstevel@tonic-gate 
38717c478bd9Sstevel@tonic-gate /* ARGSUSED */
38727c478bd9Sstevel@tonic-gate void *
single_user_thread(void * unused)38737c478bd9Sstevel@tonic-gate single_user_thread(void *unused)
38747c478bd9Sstevel@tonic-gate {
38757c478bd9Sstevel@tonic-gate 	uint_t left;
38767c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
38777c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
38787c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
38797c478bd9Sstevel@tonic-gate 	scf_value_t *val;
38807c478bd9Sstevel@tonic-gate 	const char *msg;
38817c478bd9Sstevel@tonic-gate 	char *buf;
38827c478bd9Sstevel@tonic-gate 	int r;
38837c478bd9Sstevel@tonic-gate 
3884ab618543SJohn Levon 	(void) pthread_setname_np(pthread_self(), "single_user");
3885ab618543SJohn Levon 
38867c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&single_user_thread_lock);
38877c478bd9Sstevel@tonic-gate 	single_user_thread_count++;
38887c478bd9Sstevel@tonic-gate 
38894d53c7adSDan Price 	if (!booting_to_single_user)
38904d53c7adSDan Price 		kill_user_procs();
38917c478bd9Sstevel@tonic-gate 
38927c478bd9Sstevel@tonic-gate 	if (go_single_user_mode || booting_to_single_user) {
38937c478bd9Sstevel@tonic-gate 		msg = "SINGLE USER MODE\n";
38947c478bd9Sstevel@tonic-gate 	} else {
38957c478bd9Sstevel@tonic-gate 		assert(go_to_level1);
38967c478bd9Sstevel@tonic-gate 
38977c478bd9Sstevel@tonic-gate 		fork_rc_script('1', "start", B_TRUE);
38987c478bd9Sstevel@tonic-gate 
38997c478bd9Sstevel@tonic-gate 		uu_warn("The system is ready for administration.\n");
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate 		msg = "";
39027c478bd9Sstevel@tonic-gate 	}
39037c478bd9Sstevel@tonic-gate 
39047c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&single_user_thread_lock);
39057c478bd9Sstevel@tonic-gate 
39067c478bd9Sstevel@tonic-gate 	for (;;) {
39077c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&dgraph_lock);
39087c478bd9Sstevel@tonic-gate 		r = run_sulogin(msg);
39097c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
39107c478bd9Sstevel@tonic-gate 		if (r == 0)
39117c478bd9Sstevel@tonic-gate 			break;
39127c478bd9Sstevel@tonic-gate 
39137c478bd9Sstevel@tonic-gate 		assert(r == EALREADY || r == EBUSY);
39147c478bd9Sstevel@tonic-gate 
39157c478bd9Sstevel@tonic-gate 		left = 3;
39167c478bd9Sstevel@tonic-gate 		while (left > 0)
39177c478bd9Sstevel@tonic-gate 			left = sleep(left);
39187c478bd9Sstevel@tonic-gate 	}
39197c478bd9Sstevel@tonic-gate 
39207c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&single_user_thread_lock);
39217c478bd9Sstevel@tonic-gate 
39227c478bd9Sstevel@tonic-gate 	/*
39237c478bd9Sstevel@tonic-gate 	 * If another single user thread has started, let it finish changing
39247c478bd9Sstevel@tonic-gate 	 * the run level.
39257c478bd9Sstevel@tonic-gate 	 */
39267c478bd9Sstevel@tonic-gate 	if (single_user_thread_count > 1) {
39277c478bd9Sstevel@tonic-gate 		single_user_thread_count--;
39287c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&single_user_thread_lock);
39297c478bd9Sstevel@tonic-gate 		return (NULL);
39307c478bd9Sstevel@tonic-gate 	}
39317c478bd9Sstevel@tonic-gate 
39327c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
39337c478bd9Sstevel@tonic-gate 	inst = scf_instance_create(h);
39347c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
39357c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
39367c478bd9Sstevel@tonic-gate 	buf = startd_alloc(max_scf_fmri_size);
39377c478bd9Sstevel@tonic-gate 
39387c478bd9Sstevel@tonic-gate lookup:
39397c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
39407c478bd9Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
39417c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
39427c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
39437c478bd9Sstevel@tonic-gate 			r = libscf_create_self(h);
39447c478bd9Sstevel@tonic-gate 			if (r == 0)
39457c478bd9Sstevel@tonic-gate 				goto lookup;
39467c478bd9Sstevel@tonic-gate 			assert(r == ECONNABORTED);
39477c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
39487c478bd9Sstevel@tonic-gate 
39497c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
39507c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
39517c478bd9Sstevel@tonic-gate 			goto lookup;
39527c478bd9Sstevel@tonic-gate 
39537c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
39547c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
39557c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
39567c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
39577c478bd9Sstevel@tonic-gate 		default:
39587c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
39597c478bd9Sstevel@tonic-gate 		}
39607c478bd9Sstevel@tonic-gate 	}
39617c478bd9Sstevel@tonic-gate 
39627c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
39637c478bd9Sstevel@tonic-gate 
3964eb1a3463STruong Nguyen 	r = scf_instance_delete_prop(inst, SCF_PG_OPTIONS_OVR,
39657c478bd9Sstevel@tonic-gate 	    SCF_PROPERTY_MILESTONE);
39667c478bd9Sstevel@tonic-gate 	switch (r) {
39677c478bd9Sstevel@tonic-gate 	case 0:
39687c478bd9Sstevel@tonic-gate 	case ECANCELED:
39697c478bd9Sstevel@tonic-gate 		break;
39707c478bd9Sstevel@tonic-gate 
39717c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
39727c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
39737c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
39747c478bd9Sstevel@tonic-gate 		goto lookup;
39757c478bd9Sstevel@tonic-gate 
39767c478bd9Sstevel@tonic-gate 	case EPERM:
39777c478bd9Sstevel@tonic-gate 	case EACCES:
39787c478bd9Sstevel@tonic-gate 	case EROFS:
39797c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "Could not clear temporary milestone: "
39807c478bd9Sstevel@tonic-gate 		    "%s.\n", strerror(r));
39817c478bd9Sstevel@tonic-gate 		break;
39827c478bd9Sstevel@tonic-gate 
39837c478bd9Sstevel@tonic-gate 	default:
3984eb1a3463STruong Nguyen 		bad_error("scf_instance_delete_prop", r);
39857c478bd9Sstevel@tonic-gate 	}
39867c478bd9Sstevel@tonic-gate 
39877c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
39887c478bd9Sstevel@tonic-gate 
39897c478bd9Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, buf, max_scf_fmri_size);
39907c478bd9Sstevel@tonic-gate 	switch (r) {
39917c478bd9Sstevel@tonic-gate 	case ECANCELED:
39927c478bd9Sstevel@tonic-gate 	case ENOENT:
39937c478bd9Sstevel@tonic-gate 	case EINVAL:
39947c478bd9Sstevel@tonic-gate 		(void) strcpy(buf, "all");
39957c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
39967c478bd9Sstevel@tonic-gate 
39977c478bd9Sstevel@tonic-gate 	case 0:
39987c478bd9Sstevel@tonic-gate 		uu_warn("Returning to milestone %s.\n", buf);
39997c478bd9Sstevel@tonic-gate 		break;
40007c478bd9Sstevel@tonic-gate 
40017c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
40027c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
40037c478bd9Sstevel@tonic-gate 		goto lookup;
40047c478bd9Sstevel@tonic-gate 
40057c478bd9Sstevel@tonic-gate 	default:
40067c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
40077c478bd9Sstevel@tonic-gate 	}
40087c478bd9Sstevel@tonic-gate 
40097c478bd9Sstevel@tonic-gate 	r = dgraph_set_milestone(buf, h, B_FALSE);
40107c478bd9Sstevel@tonic-gate 	switch (r) {
40117c478bd9Sstevel@tonic-gate 	case 0:
40127c478bd9Sstevel@tonic-gate 	case ECONNRESET:
40137c478bd9Sstevel@tonic-gate 	case EALREADY:
40147c478bd9Sstevel@tonic-gate 	case EINVAL:
40157c478bd9Sstevel@tonic-gate 	case ENOENT:
40167c478bd9Sstevel@tonic-gate 		break;
40177c478bd9Sstevel@tonic-gate 
40187c478bd9Sstevel@tonic-gate 	default:
40197c478bd9Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
40207c478bd9Sstevel@tonic-gate 	}
40217c478bd9Sstevel@tonic-gate 
40227c478bd9Sstevel@tonic-gate 	/*
40237c478bd9Sstevel@tonic-gate 	 * See graph_runlevel_changed().
40247c478bd9Sstevel@tonic-gate 	 */
40257c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
40267c478bd9Sstevel@tonic-gate 	utmpx_set_runlevel(target_milestone_as_runlevel(), 'S', B_TRUE);
40277c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
40287c478bd9Sstevel@tonic-gate 
40297c478bd9Sstevel@tonic-gate 	startd_free(buf, max_scf_fmri_size);
40307c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
40317c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
40327c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
40337c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
40347c478bd9Sstevel@tonic-gate 
40357c478bd9Sstevel@tonic-gate 	/*
40367c478bd9Sstevel@tonic-gate 	 * We'll give ourselves 3 seconds to respond to all of the enablings
40377c478bd9Sstevel@tonic-gate 	 * that setting the milestone should have created before checking
40387c478bd9Sstevel@tonic-gate 	 * whether to run sulogin.
40397c478bd9Sstevel@tonic-gate 	 */
40407c478bd9Sstevel@tonic-gate 	left = 3;
40417c478bd9Sstevel@tonic-gate 	while (left > 0)
40427c478bd9Sstevel@tonic-gate 		left = sleep(left);
40437c478bd9Sstevel@tonic-gate 
40447c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
40457c478bd9Sstevel@tonic-gate 	/*
40467c478bd9Sstevel@tonic-gate 	 * Clearing these variables will allow the sulogin thread to run.  We
40477c478bd9Sstevel@tonic-gate 	 * check here in case there aren't any more state updates anytime soon.
40487c478bd9Sstevel@tonic-gate 	 */
40497c478bd9Sstevel@tonic-gate 	go_to_level1 = go_single_user_mode = booting_to_single_user = B_FALSE;
40507c478bd9Sstevel@tonic-gate 	if (!sulogin_thread_running && !can_come_up()) {
40517c478bd9Sstevel@tonic-gate 		(void) startd_thread_create(sulogin_thread, NULL);
40527c478bd9Sstevel@tonic-gate 		sulogin_thread_running = B_TRUE;
40537c478bd9Sstevel@tonic-gate 	}
40547c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
40557c478bd9Sstevel@tonic-gate 	single_user_thread_count--;
40567c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&single_user_thread_lock);
40577c478bd9Sstevel@tonic-gate 	return (NULL);
40587c478bd9Sstevel@tonic-gate }
40597c478bd9Sstevel@tonic-gate 
40607c478bd9Sstevel@tonic-gate 
40617c478bd9Sstevel@tonic-gate /*
40627c478bd9Sstevel@tonic-gate  * Dependency graph operations API.  These are handle-independent thread-safe
40637c478bd9Sstevel@tonic-gate  * graph manipulation functions which are the entry points for the event
40647c478bd9Sstevel@tonic-gate  * threads below.
40657c478bd9Sstevel@tonic-gate  */
40667c478bd9Sstevel@tonic-gate 
40677c478bd9Sstevel@tonic-gate /*
40687c478bd9Sstevel@tonic-gate  * If a configured vertex exists for inst_fmri, return EEXIST.  If no vertex
40697c478bd9Sstevel@tonic-gate  * exists for inst_fmri, add one.  Then fetch the restarter from inst, make
40707c478bd9Sstevel@tonic-gate  * this vertex dependent on it, and send _ADD_INSTANCE to the restarter.
40717c478bd9Sstevel@tonic-gate  * Fetch whether the instance should be enabled from inst and send _ENABLE or
40727c478bd9Sstevel@tonic-gate  * _DISABLE as appropriate.  Finally rummage through inst's dependency
40737c478bd9Sstevel@tonic-gate  * property groups and add vertices and edges as appropriate.  If anything
40747c478bd9Sstevel@tonic-gate  * goes wrong after sending _ADD_INSTANCE, send _ADMIN_MAINT_ON to put the
40757c478bd9Sstevel@tonic-gate  * instance in maintenance.  Don't send _START or _STOP until we get a state
40767c478bd9Sstevel@tonic-gate  * update in case we're being restarted and the service is already running.
40777c478bd9Sstevel@tonic-gate  *
40787c478bd9Sstevel@tonic-gate  * To support booting to a milestone, we must also make sure all dependencies
40797c478bd9Sstevel@tonic-gate  * encountered are configured, if they exist in the repository.
40807c478bd9Sstevel@tonic-gate  *
40817c478bd9Sstevel@tonic-gate  * Returns 0 on success, ECONNABORTED on repository disconnection, EINVAL if
40827c478bd9Sstevel@tonic-gate  * inst_fmri is an invalid (or not canonical) FMRI, ECANCELED if inst is
40837c478bd9Sstevel@tonic-gate  * deleted, or EEXIST if a configured vertex for inst_fmri already exists.
40847c478bd9Sstevel@tonic-gate  */
40857c478bd9Sstevel@tonic-gate int
dgraph_add_instance(const char * inst_fmri,scf_instance_t * inst,boolean_t lock_graph)40867c478bd9Sstevel@tonic-gate dgraph_add_instance(const char *inst_fmri, scf_instance_t *inst,
40877c478bd9Sstevel@tonic-gate     boolean_t lock_graph)
40887c478bd9Sstevel@tonic-gate {
40897c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
40907c478bd9Sstevel@tonic-gate 	int err;
40917c478bd9Sstevel@tonic-gate 
40927c478bd9Sstevel@tonic-gate 	if (strcmp(inst_fmri, SCF_SERVICE_STARTD) == 0)
40937c478bd9Sstevel@tonic-gate 		return (0);
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate 	/* Check for a vertex for inst_fmri. */
40967c478bd9Sstevel@tonic-gate 	if (lock_graph) {
40977c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&dgraph_lock);
40987c478bd9Sstevel@tonic-gate 	} else {
409953f3aea0SRoger A. Faulkner 		assert(MUTEX_HELD(&dgraph_lock));
41007c478bd9Sstevel@tonic-gate 	}
41017c478bd9Sstevel@tonic-gate 
41027c478bd9Sstevel@tonic-gate 	v = vertex_get_by_name(inst_fmri);
41037c478bd9Sstevel@tonic-gate 
41047c478bd9Sstevel@tonic-gate 	if (v != NULL) {
41057c478bd9Sstevel@tonic-gate 		assert(v->gv_type == GVT_INST);
41067c478bd9Sstevel@tonic-gate 
41077c478bd9Sstevel@tonic-gate 		if (v->gv_flags & GV_CONFIGURED) {
41087c478bd9Sstevel@tonic-gate 			if (lock_graph)
41097c478bd9Sstevel@tonic-gate 				MUTEX_UNLOCK(&dgraph_lock);
41107c478bd9Sstevel@tonic-gate 			return (EEXIST);
41117c478bd9Sstevel@tonic-gate 		}
41127c478bd9Sstevel@tonic-gate 	} else {
41137c478bd9Sstevel@tonic-gate 		/* Add the vertex. */
41147c478bd9Sstevel@tonic-gate 		err = graph_insert_vertex_unconfigured(inst_fmri, GVT_INST, 0,
41157c478bd9Sstevel@tonic-gate 		    RERR_NONE, &v);
41167c478bd9Sstevel@tonic-gate 		if (err != 0) {
41177c478bd9Sstevel@tonic-gate 			assert(err == EINVAL);
41187c478bd9Sstevel@tonic-gate 			if (lock_graph)
41197c478bd9Sstevel@tonic-gate 				MUTEX_UNLOCK(&dgraph_lock);
41207c478bd9Sstevel@tonic-gate 			return (EINVAL);
41217c478bd9Sstevel@tonic-gate 		}
41227c478bd9Sstevel@tonic-gate 	}
41237c478bd9Sstevel@tonic-gate 
41247c478bd9Sstevel@tonic-gate 	err = configure_vertex(v, inst);
41257c478bd9Sstevel@tonic-gate 
41267c478bd9Sstevel@tonic-gate 	if (lock_graph)
41277c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
41287c478bd9Sstevel@tonic-gate 
41297c478bd9Sstevel@tonic-gate 	return (err);
41307c478bd9Sstevel@tonic-gate }
41317c478bd9Sstevel@tonic-gate 
41327c478bd9Sstevel@tonic-gate /*
41337c478bd9Sstevel@tonic-gate  * Locate the vertex for this property group's instance.  If it doesn't exist
41347c478bd9Sstevel@tonic-gate  * or is unconfigured, call dgraph_add_instance() & return.  Otherwise fetch
41357c478bd9Sstevel@tonic-gate  * the restarter for the instance, and if it has changed, send
41367c478bd9Sstevel@tonic-gate  * _REMOVE_INSTANCE to the old restarter, remove the dependency, make sure the
41377c478bd9Sstevel@tonic-gate  * new restarter has a vertex, add a new dependency, and send _ADD_INSTANCE to
41387c478bd9Sstevel@tonic-gate  * the new restarter.  Then fetch whether the instance should be enabled, and
41397c478bd9Sstevel@tonic-gate  * if it is different from what we had, or if we changed the restarter, send
41407c478bd9Sstevel@tonic-gate  * the appropriate _ENABLE or _DISABLE command.
41417c478bd9Sstevel@tonic-gate  *
41427c478bd9Sstevel@tonic-gate  * Returns 0 on success, ENOTSUP if the pg's parent is not an instance,
41437c478bd9Sstevel@tonic-gate  * ECONNABORTED on repository disconnection, ECANCELED if the instance is
41447c478bd9Sstevel@tonic-gate  * deleted, or -1 if the instance's general property group is deleted or if
41457c478bd9Sstevel@tonic-gate  * its enabled property is misconfigured.
41467c478bd9Sstevel@tonic-gate  */
41477c478bd9Sstevel@tonic-gate static int
dgraph_update_general(scf_propertygroup_t * pg)41487c478bd9Sstevel@tonic-gate dgraph_update_general(scf_propertygroup_t *pg)
41497c478bd9Sstevel@tonic-gate {
41507c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
41517c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
41527c478bd9Sstevel@tonic-gate 	char *fmri;
41537c478bd9Sstevel@tonic-gate 	char *restarter_fmri;
41547c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
41557c478bd9Sstevel@tonic-gate 	int err;
41567c478bd9Sstevel@tonic-gate 	int enabled, enabled_ovr;
41577c478bd9Sstevel@tonic-gate 	int oldflags;
41587c478bd9Sstevel@tonic-gate 
41597c478bd9Sstevel@tonic-gate 	/* Find the vertex for this service */
41607c478bd9Sstevel@tonic-gate 	h = scf_pg_handle(pg);
41617c478bd9Sstevel@tonic-gate 
41627c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
41637c478bd9Sstevel@tonic-gate 
41647c478bd9Sstevel@tonic-gate 	if (scf_pg_get_parent_instance(pg, inst) != 0) {
41657c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
41667c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
41677c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
41687c478bd9Sstevel@tonic-gate 
41697c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
41707c478bd9Sstevel@tonic-gate 		default:
41717c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
41727c478bd9Sstevel@tonic-gate 
41737c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
41747c478bd9Sstevel@tonic-gate 			return (0);
41757c478bd9Sstevel@tonic-gate 
41767c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
41777c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_parent_instance", scf_error());
41787c478bd9Sstevel@tonic-gate 		}
41797c478bd9Sstevel@tonic-gate 	}
41807c478bd9Sstevel@tonic-gate 
41817c478bd9Sstevel@tonic-gate 	err = libscf_instance_get_fmri(inst, &fmri);
41827c478bd9Sstevel@tonic-gate 	switch (err) {
41837c478bd9Sstevel@tonic-gate 	case 0:
41847c478bd9Sstevel@tonic-gate 		break;
41857c478bd9Sstevel@tonic-gate 
41867c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
41877c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
41887c478bd9Sstevel@tonic-gate 		return (ECONNABORTED);
41897c478bd9Sstevel@tonic-gate 
41907c478bd9Sstevel@tonic-gate 	case ECANCELED:
41917c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
41927c478bd9Sstevel@tonic-gate 		return (0);
41937c478bd9Sstevel@tonic-gate 
41947c478bd9Sstevel@tonic-gate 	default:
41957c478bd9Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", err);
41967c478bd9Sstevel@tonic-gate 	}
41977c478bd9Sstevel@tonic-gate 
41987c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG,
41997c478bd9Sstevel@tonic-gate 	    "Graph engine: Reloading general properties for %s.\n", fmri);
42007c478bd9Sstevel@tonic-gate 
42017c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
42027c478bd9Sstevel@tonic-gate 
42037c478bd9Sstevel@tonic-gate 	v = vertex_get_by_name(fmri);
42047c478bd9Sstevel@tonic-gate 	if (v == NULL || !(v->gv_flags & GV_CONFIGURED)) {
42057c478bd9Sstevel@tonic-gate 		/* Will get the up-to-date properties. */
42067c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
42077c478bd9Sstevel@tonic-gate 		err = dgraph_add_instance(fmri, inst, B_TRUE);
42087c478bd9Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
42097c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
42107c478bd9Sstevel@tonic-gate 		return (err == ECANCELED ? 0 : err);
42117c478bd9Sstevel@tonic-gate 	}
42127c478bd9Sstevel@tonic-gate 
42137c478bd9Sstevel@tonic-gate 	/* Read enabled & restarter from repository. */
42147c478bd9Sstevel@tonic-gate 	restarter_fmri = startd_alloc(max_scf_value_size);
42157c478bd9Sstevel@tonic-gate 	err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled,
42167c478bd9Sstevel@tonic-gate 	    &enabled_ovr, &restarter_fmri);
42177c478bd9Sstevel@tonic-gate 	if (err != 0 || enabled == -1) {
42187c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
42197c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
42207c478bd9Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
42217c478bd9Sstevel@tonic-gate 
42227c478bd9Sstevel@tonic-gate 		switch (err) {
42237c478bd9Sstevel@tonic-gate 		case ENOENT:
42247c478bd9Sstevel@tonic-gate 		case 0:
42257c478bd9Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
42267c478bd9Sstevel@tonic-gate 			return (-1);
42277c478bd9Sstevel@tonic-gate 
42287c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
42297c478bd9Sstevel@tonic-gate 		case ECANCELED:
42307c478bd9Sstevel@tonic-gate 			startd_free(restarter_fmri, max_scf_value_size);
42317c478bd9Sstevel@tonic-gate 			return (err);
42327c478bd9Sstevel@tonic-gate 
42337c478bd9Sstevel@tonic-gate 		default:
42347c478bd9Sstevel@tonic-gate 			bad_error("libscf_get_basic_instance_data", err);
42357c478bd9Sstevel@tonic-gate 		}
42367c478bd9Sstevel@tonic-gate 	}
42377c478bd9Sstevel@tonic-gate 
42387c478bd9Sstevel@tonic-gate 	oldflags = v->gv_flags;
42397c478bd9Sstevel@tonic-gate 	v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) |
42407c478bd9Sstevel@tonic-gate 	    (enabled ? GV_ENBLD_NOOVR : 0);
42417c478bd9Sstevel@tonic-gate 
42427c478bd9Sstevel@tonic-gate 	if (enabled_ovr != -1)
42437c478bd9Sstevel@tonic-gate 		enabled = enabled_ovr;
42447c478bd9Sstevel@tonic-gate 
42457c478bd9Sstevel@tonic-gate 	/*
42467c478bd9Sstevel@tonic-gate 	 * If GV_ENBLD_NOOVR has changed, then we need to re-evaluate the
42477c478bd9Sstevel@tonic-gate 	 * subgraph.
42487c478bd9Sstevel@tonic-gate 	 */
42497c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE && v->gv_flags != oldflags)
42507c478bd9Sstevel@tonic-gate 		(void) eval_subgraph(v, h);
42517c478bd9Sstevel@tonic-gate 
42527c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
42537c478bd9Sstevel@tonic-gate 
42547c478bd9Sstevel@tonic-gate 	/* Ignore restarter change for now. */
42557c478bd9Sstevel@tonic-gate 
42567c478bd9Sstevel@tonic-gate 	startd_free(restarter_fmri, max_scf_value_size);
42577c478bd9Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
42587c478bd9Sstevel@tonic-gate 
42597c478bd9Sstevel@tonic-gate 	/*
42607c478bd9Sstevel@tonic-gate 	 * Always send _ENABLE or _DISABLE.  We could avoid this if the
42617c478bd9Sstevel@tonic-gate 	 * restarter didn't change and the enabled value didn't change, but
42627c478bd9Sstevel@tonic-gate 	 * that's not easy to check and improbable anyway, so we'll just do
42637c478bd9Sstevel@tonic-gate 	 * this.
42647c478bd9Sstevel@tonic-gate 	 */
42657c478bd9Sstevel@tonic-gate 	graph_enable_by_vertex(v, enabled, 1);
42667c478bd9Sstevel@tonic-gate 
42677c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
42687c478bd9Sstevel@tonic-gate 
42697c478bd9Sstevel@tonic-gate 	return (0);
42707c478bd9Sstevel@tonic-gate }
42717c478bd9Sstevel@tonic-gate 
42727c478bd9Sstevel@tonic-gate /*
42737c478bd9Sstevel@tonic-gate  * Delete all of the property group dependencies of v, update inst's running
42747c478bd9Sstevel@tonic-gate  * snapshot, and add the dependencies in the new snapshot.  If any of the new
42757c478bd9Sstevel@tonic-gate  * dependencies would create a cycle, send _ADMIN_MAINT_ON.  Otherwise
42767c478bd9Sstevel@tonic-gate  * reevaluate v's dependencies, send _START or _STOP as appropriate, and do
42777c478bd9Sstevel@tonic-gate  * the same for v's dependents.
42787c478bd9Sstevel@tonic-gate  *
42797c478bd9Sstevel@tonic-gate  * Returns
42807c478bd9Sstevel@tonic-gate  *   0 - success
42817c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
42827c478bd9Sstevel@tonic-gate  *   ECANCELED - inst was deleted
42837c478bd9Sstevel@tonic-gate  *   EINVAL - inst is invalid (e.g., missing general/enabled)
42847c478bd9Sstevel@tonic-gate  *   -1 - libscf_snapshots_refresh() failed
42857c478bd9Sstevel@tonic-gate  */
42867c478bd9Sstevel@tonic-gate static int
dgraph_refresh_instance(graph_vertex_t * v,scf_instance_t * inst)42877c478bd9Sstevel@tonic-gate dgraph_refresh_instance(graph_vertex_t *v, scf_instance_t *inst)
42887c478bd9Sstevel@tonic-gate {
42897c478bd9Sstevel@tonic-gate 	int r;
42907c478bd9Sstevel@tonic-gate 	int enabled;
4291f6e214c7SGavin Maltby 	int32_t tset;
42927c478bd9Sstevel@tonic-gate 
429353f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
42947c478bd9Sstevel@tonic-gate 	assert(v->gv_type == GVT_INST);
42957c478bd9Sstevel@tonic-gate 
42967c478bd9Sstevel@tonic-gate 	/* Only refresh services with valid general/enabled properties. */
42977c478bd9Sstevel@tonic-gate 	r = libscf_get_basic_instance_data(scf_instance_handle(inst), inst,
42987c478bd9Sstevel@tonic-gate 	    v->gv_name, &enabled, NULL, NULL);
42997c478bd9Sstevel@tonic-gate 	switch (r) {
43007c478bd9Sstevel@tonic-gate 	case 0:
43017c478bd9Sstevel@tonic-gate 		break;
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
43047c478bd9Sstevel@tonic-gate 	case ECANCELED:
43057c478bd9Sstevel@tonic-gate 		return (r);
43067c478bd9Sstevel@tonic-gate 
43077c478bd9Sstevel@tonic-gate 	case ENOENT:
43087c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
43097c478bd9Sstevel@tonic-gate 		    "Ignoring %s because it has no general property group.\n",
43107c478bd9Sstevel@tonic-gate 		    v->gv_name);
43117c478bd9Sstevel@tonic-gate 		return (EINVAL);
43127c478bd9Sstevel@tonic-gate 
43137c478bd9Sstevel@tonic-gate 	default:
43147c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_basic_instance_data", r);
43157c478bd9Sstevel@tonic-gate 	}
43167c478bd9Sstevel@tonic-gate 
4317f6e214c7SGavin Maltby 	if ((tset = libscf_get_stn_tset(inst)) == -1) {
4318f6e214c7SGavin Maltby 		log_framework(LOG_WARNING,
4319f6e214c7SGavin Maltby 		    "Failed to get notification parameters for %s: %s\n",
4320f6e214c7SGavin Maltby 		    v->gv_name, scf_strerror(scf_error()));
4321f6e214c7SGavin Maltby 		tset = 0;
4322f6e214c7SGavin Maltby 	}
4323f6e214c7SGavin Maltby 	v->gv_stn_tset = tset;
4324f6e214c7SGavin Maltby 	if (strcmp(v->gv_name, SCF_INSTANCE_GLOBAL) == 0)
4325f6e214c7SGavin Maltby 		stn_global = tset;
4326f6e214c7SGavin Maltby 
43277c478bd9Sstevel@tonic-gate 	if (enabled == -1)
43287c478bd9Sstevel@tonic-gate 		return (EINVAL);
43297c478bd9Sstevel@tonic-gate 
43307c478bd9Sstevel@tonic-gate 	r = libscf_snapshots_refresh(inst, v->gv_name);
43317c478bd9Sstevel@tonic-gate 	if (r != 0) {
43327c478bd9Sstevel@tonic-gate 		if (r != -1)
43337c478bd9Sstevel@tonic-gate 			bad_error("libscf_snapshots_refresh", r);
43347c478bd9Sstevel@tonic-gate 
43357c478bd9Sstevel@tonic-gate 		/* error logged */
43367c478bd9Sstevel@tonic-gate 		return (r);
43377c478bd9Sstevel@tonic-gate 	}
43387c478bd9Sstevel@tonic-gate 
43397c478bd9Sstevel@tonic-gate 	r = refresh_vertex(v, inst);
43407c478bd9Sstevel@tonic-gate 	if (r != 0 && r != ECONNABORTED)
43417c478bd9Sstevel@tonic-gate 		bad_error("refresh_vertex", r);
43427c478bd9Sstevel@tonic-gate 	return (r);
43437c478bd9Sstevel@tonic-gate }
43447c478bd9Sstevel@tonic-gate 
43457c478bd9Sstevel@tonic-gate /*
4346aca380d7SRenaud Manus  * Returns true only if none of this service's dependents are 'up' -- online
4347aca380d7SRenaud Manus  * or degraded (offline is considered down in this situation). This function
4348aca380d7SRenaud Manus  * is somehow similar to is_nonsubgraph_leaf() but works on subtrees.
4349aca380d7SRenaud Manus  */
4350aca380d7SRenaud Manus static boolean_t
insubtree_dependents_down(graph_vertex_t * v)4351aca380d7SRenaud Manus insubtree_dependents_down(graph_vertex_t *v)
4352aca380d7SRenaud Manus {
4353aca380d7SRenaud Manus 	graph_vertex_t *vv;
4354aca380d7SRenaud Manus 	graph_edge_t *e;
4355aca380d7SRenaud Manus 
435653f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
4357aca380d7SRenaud Manus 
4358aca380d7SRenaud Manus 	for (e = uu_list_first(v->gv_dependents); e != NULL;
4359aca380d7SRenaud Manus 	    e = uu_list_next(v->gv_dependents, e)) {
4360aca380d7SRenaud Manus 		vv = e->ge_vertex;
4361aca380d7SRenaud Manus 		if (vv->gv_type == GVT_INST) {
4362aca380d7SRenaud Manus 			if ((vv->gv_flags & GV_CONFIGURED) == 0)
4363aca380d7SRenaud Manus 				continue;
4364aca380d7SRenaud Manus 
4365aca380d7SRenaud Manus 			if ((vv->gv_flags & GV_TOOFFLINE) == 0)
4366aca380d7SRenaud Manus 				continue;
4367aca380d7SRenaud Manus 
4368aca380d7SRenaud Manus 			if ((vv->gv_state == RESTARTER_STATE_ONLINE) ||
4369aca380d7SRenaud Manus 			    (vv->gv_state == RESTARTER_STATE_DEGRADED))
4370aca380d7SRenaud Manus 				return (B_FALSE);
4371aca380d7SRenaud Manus 		} else {
4372aca380d7SRenaud Manus 			/*
43733e207980SAndrew Stormont 			 * Skip all excluded dependents and decide whether
43743e207980SAndrew Stormont 			 * to offline the service based on the restart_on
43753e207980SAndrew Stormont 			 * attribute.
4376653e7459Sgowtham thommandra - Sun Microsystems - Bangalore India 			 */
4377653e7459Sgowtham thommandra - Sun Microsystems - Bangalore India 			if (is_depgrp_bypassed(vv))
4378653e7459Sgowtham thommandra - Sun Microsystems - Bangalore India 				continue;
4379653e7459Sgowtham thommandra - Sun Microsystems - Bangalore India 
4380653e7459Sgowtham thommandra - Sun Microsystems - Bangalore India 			/*
4381aca380d7SRenaud Manus 			 * For dependency groups or service vertices, keep
4382aca380d7SRenaud Manus 			 * traversing to see if instances are running.
4383aca380d7SRenaud Manus 			 */
4384aca380d7SRenaud Manus 			if (insubtree_dependents_down(vv) == B_FALSE)
4385aca380d7SRenaud Manus 				return (B_FALSE);
4386aca380d7SRenaud Manus 		}
4387aca380d7SRenaud Manus 	}
4388aca380d7SRenaud Manus 
4389aca380d7SRenaud Manus 	return (B_TRUE);
4390aca380d7SRenaud Manus }
4391aca380d7SRenaud Manus 
4392aca380d7SRenaud Manus /*
439356e23938Sbustos  * Returns true only if none of this service's dependents are 'up' -- online,
439456e23938Sbustos  * degraded, or offline.
43957c478bd9Sstevel@tonic-gate  */
43967c478bd9Sstevel@tonic-gate static int
is_nonsubgraph_leaf(graph_vertex_t * v)439756e23938Sbustos is_nonsubgraph_leaf(graph_vertex_t *v)
43987c478bd9Sstevel@tonic-gate {
43997c478bd9Sstevel@tonic-gate 	graph_vertex_t *vv;
44007c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
44017c478bd9Sstevel@tonic-gate 
440253f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
44037c478bd9Sstevel@tonic-gate 
44047c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(v->gv_dependents);
44057c478bd9Sstevel@tonic-gate 	    e != NULL;
44067c478bd9Sstevel@tonic-gate 	    e = uu_list_next(v->gv_dependents, e)) {
44077c478bd9Sstevel@tonic-gate 
44087c478bd9Sstevel@tonic-gate 		vv = e->ge_vertex;
44097c478bd9Sstevel@tonic-gate 		if (vv->gv_type == GVT_INST) {
441056e23938Sbustos 			if ((vv->gv_flags & GV_CONFIGURED) == 0)
441156e23938Sbustos 				continue;
441256e23938Sbustos 
441356e23938Sbustos 			if (vv->gv_flags & GV_INSUBGRAPH)
441456e23938Sbustos 				continue;
441556e23938Sbustos 
441656e23938Sbustos 			if (up_state(vv->gv_state))
441756e23938Sbustos 				return (0);
44187c478bd9Sstevel@tonic-gate 		} else {
44197c478bd9Sstevel@tonic-gate 			/*
44207c478bd9Sstevel@tonic-gate 			 * For dependency group or service vertices, keep
44217c478bd9Sstevel@tonic-gate 			 * traversing to see if instances are running.
4422aca380d7SRenaud Manus 			 *
4423aca380d7SRenaud Manus 			 * We should skip exclude_all dependencies otherwise
4424aca380d7SRenaud Manus 			 * the vertex will never be considered as a leaf
4425aca380d7SRenaud Manus 			 * if the dependent is offline. The main reason for
4426aca380d7SRenaud Manus 			 * this is that disable_nonsubgraph_leaves() skips
4427aca380d7SRenaud Manus 			 * exclusion dependencies.
44287c478bd9Sstevel@tonic-gate 			 */
4429aca380d7SRenaud Manus 			if (vv->gv_type == GVT_GROUP &&
4430aca380d7SRenaud Manus 			    vv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
4431aca380d7SRenaud Manus 				continue;
4432aca380d7SRenaud Manus 
443356e23938Sbustos 			if (!is_nonsubgraph_leaf(vv))
44347c478bd9Sstevel@tonic-gate 				return (0);
44357c478bd9Sstevel@tonic-gate 		}
443656e23938Sbustos 	}
443756e23938Sbustos 
443856e23938Sbustos 	return (1);
443956e23938Sbustos }
44407c478bd9Sstevel@tonic-gate 
44417c478bd9Sstevel@tonic-gate /*
444256e23938Sbustos  * Disable v temporarily.  Attempt to do this by setting its enabled override
444356e23938Sbustos  * property in the repository.  If that fails, send a _DISABLE command.
444456e23938Sbustos  * Returns 0 on success and ECONNABORTED if the repository connection is
444556e23938Sbustos  * broken.
44467c478bd9Sstevel@tonic-gate  */
444756e23938Sbustos static int
disable_service_temporarily(graph_vertex_t * v,scf_handle_t * h)444856e23938Sbustos disable_service_temporarily(graph_vertex_t *v, scf_handle_t *h)
44497c478bd9Sstevel@tonic-gate {
445056e23938Sbustos 	const char * const emsg = "Could not temporarily disable %s because "
445156e23938Sbustos 	    "%s.  Will stop service anyways.  Repository status for the "
445256e23938Sbustos 	    "service may be inaccurate.\n";
445356e23938Sbustos 	const char * const emsg_cbroken =
445456e23938Sbustos 	    "the repository connection was broken";
445556e23938Sbustos 
445656e23938Sbustos 	scf_instance_t *inst;
44577c478bd9Sstevel@tonic-gate 	int r;
44587c478bd9Sstevel@tonic-gate 
44597c478bd9Sstevel@tonic-gate 	inst = scf_instance_create(h);
44607c478bd9Sstevel@tonic-gate 	if (inst == NULL) {
446156e23938Sbustos 		char buf[100];
446256e23938Sbustos 
446356e23938Sbustos 		(void) snprintf(buf, sizeof (buf),
446456e23938Sbustos 		    "scf_instance_create() failed (%s)",
446556e23938Sbustos 		    scf_strerror(scf_error()));
446656e23938Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name, buf);
446756e23938Sbustos 
446856e23938Sbustos 		graph_enable_by_vertex(v, 0, 0);
446956e23938Sbustos 		return (0);
44707c478bd9Sstevel@tonic-gate 	}
447156e23938Sbustos 
44727c478bd9Sstevel@tonic-gate 	r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst,
44737c478bd9Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT);
44747c478bd9Sstevel@tonic-gate 	if (r != 0) {
44757c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
44767c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
447756e23938Sbustos 			log_error(LOG_WARNING, emsg, v->gv_name, emsg_cbroken);
447856e23938Sbustos 			graph_enable_by_vertex(v, 0, 0);
447956e23938Sbustos 			return (ECONNABORTED);
44807c478bd9Sstevel@tonic-gate 
44817c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
448256e23938Sbustos 			return (0);
44837c478bd9Sstevel@tonic-gate 
44847c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
44857c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
44867c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
44877c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
44887c478bd9Sstevel@tonic-gate 		default:
44897c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri",
44907c478bd9Sstevel@tonic-gate 			    scf_error());
44917c478bd9Sstevel@tonic-gate 		}
44927c478bd9Sstevel@tonic-gate 	}
449356e23938Sbustos 
44947c478bd9Sstevel@tonic-gate 	r = libscf_set_enable_ovr(inst, 0);
44957c478bd9Sstevel@tonic-gate 	switch (r) {
44967c478bd9Sstevel@tonic-gate 	case 0:
44977c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
449856e23938Sbustos 		return (0);
449956e23938Sbustos 
45007c478bd9Sstevel@tonic-gate 	case ECANCELED:
45017c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
450256e23938Sbustos 		return (0);
450356e23938Sbustos 
45047c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
450556e23938Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name, emsg_cbroken);
450656e23938Sbustos 		graph_enable_by_vertex(v, 0, 0);
450756e23938Sbustos 		return (ECONNABORTED);
450856e23938Sbustos 
45097c478bd9Sstevel@tonic-gate 	case EPERM:
451056e23938Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name,
451156e23938Sbustos 		    "the repository denied permission");
451256e23938Sbustos 		graph_enable_by_vertex(v, 0, 0);
451356e23938Sbustos 		return (0);
451456e23938Sbustos 
45157c478bd9Sstevel@tonic-gate 	case EROFS:
451656e23938Sbustos 		log_error(LOG_WARNING, emsg, v->gv_name,
451756e23938Sbustos 		    "the repository is read-only");
451856e23938Sbustos 		graph_enable_by_vertex(v, 0, 0);
451956e23938Sbustos 		return (0);
452056e23938Sbustos 
45217c478bd9Sstevel@tonic-gate 	default:
45227c478bd9Sstevel@tonic-gate 		bad_error("libscf_set_enable_ovr", r);
452356e23938Sbustos 		/* NOTREACHED */
45247c478bd9Sstevel@tonic-gate 	}
452556e23938Sbustos }
452656e23938Sbustos 
452756e23938Sbustos /*
4528aca380d7SRenaud Manus  * Of the transitive instance dependencies of v, offline those which are
4529aca380d7SRenaud Manus  * in the subtree and which are leaves (i.e., have no dependents which are
4530aca380d7SRenaud Manus  * "up").
4531aca380d7SRenaud Manus  */
4532aca380d7SRenaud Manus void
offline_subtree_leaves(graph_vertex_t * v,void * arg)4533aca380d7SRenaud Manus offline_subtree_leaves(graph_vertex_t *v, void *arg)
4534aca380d7SRenaud Manus {
453553f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
4536aca380d7SRenaud Manus 
4537aca380d7SRenaud Manus 	/* If v isn't an instance, recurse on its dependencies. */
4538aca380d7SRenaud Manus 	if (v->gv_type != GVT_INST) {
4539aca380d7SRenaud Manus 		graph_walk_dependencies(v, offline_subtree_leaves, arg);
4540aca380d7SRenaud Manus 		return;
4541aca380d7SRenaud Manus 	}
4542aca380d7SRenaud Manus 
4543aca380d7SRenaud Manus 	/*
4544aca380d7SRenaud Manus 	 * If v is not in the subtree, so should all of its dependencies,
4545aca380d7SRenaud Manus 	 * so do nothing.
4546aca380d7SRenaud Manus 	 */
4547aca380d7SRenaud Manus 	if ((v->gv_flags & GV_TOOFFLINE) == 0)
4548aca380d7SRenaud Manus 		return;
4549aca380d7SRenaud Manus 
4550aca380d7SRenaud Manus 	/* If v isn't a leaf because it's already down, recurse. */
4551aca380d7SRenaud Manus 	if (!up_state(v->gv_state)) {
4552aca380d7SRenaud Manus 		graph_walk_dependencies(v, offline_subtree_leaves, arg);
4553aca380d7SRenaud Manus 		return;
4554aca380d7SRenaud Manus 	}
4555aca380d7SRenaud Manus 
4556aca380d7SRenaud Manus 	/* if v is a leaf, offline it or disable it if it's the last one */
4557aca380d7SRenaud Manus 	if (insubtree_dependents_down(v) == B_TRUE) {
4558aca380d7SRenaud Manus 		if (v->gv_flags & GV_TODISABLE)
4559aca380d7SRenaud Manus 			vertex_send_event(v,
4560aca380d7SRenaud Manus 			    RESTARTER_EVENT_TYPE_ADMIN_DISABLE);
4561aca380d7SRenaud Manus 		else
4562aca380d7SRenaud Manus 			offline_vertex(v);
4563aca380d7SRenaud Manus 	}
4564aca380d7SRenaud Manus }
4565aca380d7SRenaud Manus 
4566aca380d7SRenaud Manus void
graph_offline_subtree_leaves(graph_vertex_t * v,void * h)4567aca380d7SRenaud Manus graph_offline_subtree_leaves(graph_vertex_t *v, void *h)
4568aca380d7SRenaud Manus {
4569aca380d7SRenaud Manus 	graph_walk_dependencies(v, offline_subtree_leaves, (void *)h);
4570aca380d7SRenaud Manus }
4571aca380d7SRenaud Manus 
4572aca380d7SRenaud Manus 
4573aca380d7SRenaud Manus /*
457456e23938Sbustos  * Of the transitive instance dependencies of v, disable those which are not
457556e23938Sbustos  * in the subgraph and which are leaves (i.e., have no dependents which are
457656e23938Sbustos  * "up").
457756e23938Sbustos  */
457856e23938Sbustos static void
disable_nonsubgraph_leaves(graph_vertex_t * v,void * arg)457956e23938Sbustos disable_nonsubgraph_leaves(graph_vertex_t *v, void *arg)
458056e23938Sbustos {
458153f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
458256e23938Sbustos 
4583b54561f4Snakanon 	/*
4584b54561f4Snakanon 	 * We must skip exclusion dependencies because they are allowed to
4585b54561f4Snakanon 	 * complete dependency cycles.  This is correct because A's exclusion
4586b54561f4Snakanon 	 * dependency on B doesn't bear on the order in which they should be
4587b54561f4Snakanon 	 * stopped.  Indeed, the exclusion dependency should guarantee that
4588b54561f4Snakanon 	 * they are never online at the same time.
4589b54561f4Snakanon 	 */
4590b54561f4Snakanon 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
4591b54561f4Snakanon 		return;
4592b54561f4Snakanon 
459356e23938Sbustos 	/* If v isn't an instance, recurse on its dependencies. */
459456e23938Sbustos 	if (v->gv_type != GVT_INST)
459556e23938Sbustos 		goto recurse;
459656e23938Sbustos 
459756e23938Sbustos 	if ((v->gv_flags & GV_CONFIGURED) == 0)
459856e23938Sbustos 		/*
459956e23938Sbustos 		 * Unconfigured instances should have no dependencies, but in
460056e23938Sbustos 		 * case they ever get them,
460156e23938Sbustos 		 */
460256e23938Sbustos 		goto recurse;
460356e23938Sbustos 
460456e23938Sbustos 	/*
460556e23938Sbustos 	 * If v is in the subgraph, so should all of its dependencies, so do
460656e23938Sbustos 	 * nothing.
460756e23938Sbustos 	 */
460856e23938Sbustos 	if (v->gv_flags & GV_INSUBGRAPH)
46097c478bd9Sstevel@tonic-gate 		return;
461056e23938Sbustos 
461156e23938Sbustos 	/* If v isn't a leaf because it's already down, recurse. */
461256e23938Sbustos 	if (!up_state(v->gv_state))
461356e23938Sbustos 		goto recurse;
461456e23938Sbustos 
461556e23938Sbustos 	/* If v is disabled but not down yet, be patient. */
461656e23938Sbustos 	if ((v->gv_flags & GV_ENABLED) == 0)
461756e23938Sbustos 		return;
461856e23938Sbustos 
461956e23938Sbustos 	/* If v is a leaf, disable it. */
462056e23938Sbustos 	if (is_nonsubgraph_leaf(v))
462156e23938Sbustos 		(void) disable_service_temporarily(v, (scf_handle_t *)arg);
462256e23938Sbustos 
462356e23938Sbustos 	return;
462456e23938Sbustos 
46257c478bd9Sstevel@tonic-gate recurse:
462656e23938Sbustos 	graph_walk_dependencies(v, disable_nonsubgraph_leaves, arg);
46277c478bd9Sstevel@tonic-gate }
46287c478bd9Sstevel@tonic-gate 
4629f6e214c7SGavin Maltby static int
stn_restarter_state(restarter_instance_state_t rstate)4630f6e214c7SGavin Maltby stn_restarter_state(restarter_instance_state_t rstate)
4631f6e214c7SGavin Maltby {
4632f6e214c7SGavin Maltby 	static const struct statemap {
4633f6e214c7SGavin Maltby 		restarter_instance_state_t restarter_state;
4634f6e214c7SGavin Maltby 		int scf_state;
4635f6e214c7SGavin Maltby 	} map[] = {
4636f6e214c7SGavin Maltby 		{ RESTARTER_STATE_UNINIT, SCF_STATE_UNINIT },
4637f6e214c7SGavin Maltby 		{ RESTARTER_STATE_MAINT, SCF_STATE_MAINT },
4638f6e214c7SGavin Maltby 		{ RESTARTER_STATE_OFFLINE, SCF_STATE_OFFLINE },
4639f6e214c7SGavin Maltby 		{ RESTARTER_STATE_DISABLED, SCF_STATE_DISABLED },
4640f6e214c7SGavin Maltby 		{ RESTARTER_STATE_ONLINE, SCF_STATE_ONLINE },
4641f6e214c7SGavin Maltby 		{ RESTARTER_STATE_DEGRADED, SCF_STATE_DEGRADED }
4642f6e214c7SGavin Maltby 	};
4643f6e214c7SGavin Maltby 
4644f6e214c7SGavin Maltby 	int i;
4645f6e214c7SGavin Maltby 
4646f6e214c7SGavin Maltby 	for (i = 0; i < sizeof (map) / sizeof (map[0]); i++) {
4647f6e214c7SGavin Maltby 		if (rstate == map[i].restarter_state)
4648f6e214c7SGavin Maltby 			return (map[i].scf_state);
4649f6e214c7SGavin Maltby 	}
4650f6e214c7SGavin Maltby 
4651f6e214c7SGavin Maltby 	return (-1);
4652f6e214c7SGavin Maltby }
4653f6e214c7SGavin Maltby 
4654f6e214c7SGavin Maltby /*
4655f6e214c7SGavin Maltby  * State transition counters
4656f6e214c7SGavin Maltby  * Not incremented atomically - indicative only
4657f6e214c7SGavin Maltby  */
4658f6e214c7SGavin Maltby static uint64_t stev_ct_maint;
4659f6e214c7SGavin Maltby static uint64_t stev_ct_hwerr;
4660f6e214c7SGavin Maltby static uint64_t stev_ct_service;
4661f6e214c7SGavin Maltby static uint64_t stev_ct_global;
4662f6e214c7SGavin Maltby static uint64_t stev_ct_noprefs;
4663f6e214c7SGavin Maltby static uint64_t stev_ct_from_uninit;
4664f6e214c7SGavin Maltby static uint64_t stev_ct_bad_state;
4665f6e214c7SGavin Maltby static uint64_t stev_ct_ovr_prefs;
4666f6e214c7SGavin Maltby 
4667f6e214c7SGavin Maltby static void
dgraph_state_transition_notify(graph_vertex_t * v,restarter_instance_state_t old_state,restarter_str_t reason)4668f6e214c7SGavin Maltby dgraph_state_transition_notify(graph_vertex_t *v,
4669f6e214c7SGavin Maltby     restarter_instance_state_t old_state, restarter_str_t reason)
4670f6e214c7SGavin Maltby {
4671f6e214c7SGavin Maltby 	restarter_instance_state_t new_state = v->gv_state;
4672f6e214c7SGavin Maltby 	int stn_transition, maint;
4673f6e214c7SGavin Maltby 	int from, to;
4674f6e214c7SGavin Maltby 	nvlist_t *attr;
4675f6e214c7SGavin Maltby 	fmev_pri_t pri = FMEV_LOPRI;
4676f6e214c7SGavin Maltby 	int raise = 0;
4677f6e214c7SGavin Maltby 
4678f6e214c7SGavin Maltby 	if ((from = stn_restarter_state(old_state)) == -1 ||
4679f6e214c7SGavin Maltby 	    (to = stn_restarter_state(new_state)) == -1) {
4680f6e214c7SGavin Maltby 		stev_ct_bad_state++;
4681f6e214c7SGavin Maltby 		return;
4682f6e214c7SGavin Maltby 	}
4683f6e214c7SGavin Maltby 
4684f6e214c7SGavin Maltby 	stn_transition = from << 16 | to;
4685f6e214c7SGavin Maltby 
4686f6e214c7SGavin Maltby 	maint = (to == SCF_STATE_MAINT || from == SCF_STATE_MAINT);
4687f6e214c7SGavin Maltby 
4688f6e214c7SGavin Maltby 	if (maint) {
4689f6e214c7SGavin Maltby 		/*
4690f6e214c7SGavin Maltby 		 * All transitions to/from maintenance state must raise
4691f6e214c7SGavin Maltby 		 * an event.
4692f6e214c7SGavin Maltby 		 */
4693f6e214c7SGavin Maltby 		raise++;
4694f6e214c7SGavin Maltby 		pri = FMEV_HIPRI;
4695f6e214c7SGavin Maltby 		stev_ct_maint++;
4696f6e214c7SGavin Maltby 	} else if (reason == restarter_str_ct_ev_hwerr) {
4697f6e214c7SGavin Maltby 		/*
4698f6e214c7SGavin Maltby 		 * All transitions caused by hardware fault must raise
4699f6e214c7SGavin Maltby 		 * an event
4700f6e214c7SGavin Maltby 		 */
4701f6e214c7SGavin Maltby 		raise++;
4702f6e214c7SGavin Maltby 		pri = FMEV_HIPRI;
4703f6e214c7SGavin Maltby 		stev_ct_hwerr++;
4704f6e214c7SGavin Maltby 	} else if (stn_transition & v->gv_stn_tset) {
4705f6e214c7SGavin Maltby 		/*
4706f6e214c7SGavin Maltby 		 * Specifically enabled event.
4707f6e214c7SGavin Maltby 		 */
4708f6e214c7SGavin Maltby 		raise++;
4709f6e214c7SGavin Maltby 		stev_ct_service++;
4710f6e214c7SGavin Maltby 	} else if (from == SCF_STATE_UNINIT) {
4711f6e214c7SGavin Maltby 		/*
4712f6e214c7SGavin Maltby 		 * Only raise these if specifically selected above.
4713f6e214c7SGavin Maltby 		 */
4714f6e214c7SGavin Maltby 		stev_ct_from_uninit++;
4715f6e214c7SGavin Maltby 	} else if (stn_transition & stn_global &&
4716f6e214c7SGavin Maltby 	    (IS_ENABLED(v) == 1 || to == SCF_STATE_DISABLED)) {
4717f6e214c7SGavin Maltby 		raise++;
4718f6e214c7SGavin Maltby 		stev_ct_global++;
4719f6e214c7SGavin Maltby 	} else {
4720f6e214c7SGavin Maltby 		stev_ct_noprefs++;
4721f6e214c7SGavin Maltby 	}
4722f6e214c7SGavin Maltby 
4723f6e214c7SGavin Maltby 	if (info_events_all) {
4724f6e214c7SGavin Maltby 		stev_ct_ovr_prefs++;
4725f6e214c7SGavin Maltby 		raise++;
4726f6e214c7SGavin Maltby 	}
4727f6e214c7SGavin Maltby 	if (!raise)
4728f6e214c7SGavin Maltby 		return;
4729f6e214c7SGavin Maltby 
4730f6e214c7SGavin Maltby 	if (nvlist_alloc(&attr, NV_UNIQUE_NAME, 0) != 0 ||
4731f6e214c7SGavin Maltby 	    nvlist_add_string(attr, "fmri", v->gv_name) != 0 ||
4732f6e214c7SGavin Maltby 	    nvlist_add_uint32(attr, "reason-version",
4733f6e214c7SGavin Maltby 	    restarter_str_version()) || nvlist_add_string(attr, "reason-short",
4734f6e214c7SGavin Maltby 	    restarter_get_str_short(reason)) != 0 ||
4735f6e214c7SGavin Maltby 	    nvlist_add_string(attr, "reason-long",
4736f6e214c7SGavin Maltby 	    restarter_get_str_long(reason)) != 0 ||
4737f6e214c7SGavin Maltby 	    nvlist_add_int32(attr, "transition", stn_transition) != 0) {
4738f6e214c7SGavin Maltby 		log_framework(LOG_WARNING,
4739f6e214c7SGavin Maltby 		    "FMEV: %s could not create nvlist for transition "
4740f6e214c7SGavin Maltby 		    "event: %s\n", v->gv_name, strerror(errno));
4741f6e214c7SGavin Maltby 		nvlist_free(attr);
4742f6e214c7SGavin Maltby 		return;
4743f6e214c7SGavin Maltby 	}
4744f6e214c7SGavin Maltby 
4745f6e214c7SGavin Maltby 	if (fmev_rspublish_nvl(FMEV_RULESET_SMF, "state-transition",
4746f6e214c7SGavin Maltby 	    instance_state_str[new_state], pri, attr) != FMEV_SUCCESS) {
4747f6e214c7SGavin Maltby 		log_framework(LOG_DEBUG,
4748f6e214c7SGavin Maltby 		    "FMEV: %s failed to publish transition event: %s\n",
4749f6e214c7SGavin Maltby 		    v->gv_name, fmev_strerror(fmev_errno));
4750f6e214c7SGavin Maltby 		nvlist_free(attr);
4751f6e214c7SGavin Maltby 	}
4752f6e214c7SGavin Maltby }
4753f6e214c7SGavin Maltby 
47547c478bd9Sstevel@tonic-gate /*
47557c478bd9Sstevel@tonic-gate  * Find the vertex for inst_name.  If it doesn't exist, return ENOENT.
47567c478bd9Sstevel@tonic-gate  * Otherwise set its state to state.  If the instance has entered a state
47577c478bd9Sstevel@tonic-gate  * which requires automatic action, take it (Uninitialized: do
47587c478bd9Sstevel@tonic-gate  * dgraph_refresh_instance() without the snapshot update.  Disabled: if the
47597c478bd9Sstevel@tonic-gate  * instance should be enabled, send _ENABLE.  Offline: if the instance should
47607c478bd9Sstevel@tonic-gate  * be disabled, send _DISABLE, and if its dependencies are satisfied, send
47617c478bd9Sstevel@tonic-gate  * _START.  Online, Degraded: if the instance wasn't running, update its start
47627c478bd9Sstevel@tonic-gate  * snapshot.  Maintenance: no action.)
47637c478bd9Sstevel@tonic-gate  *
47647c478bd9Sstevel@tonic-gate  * Also fails with ECONNABORTED, or EINVAL if state is invalid.
47657c478bd9Sstevel@tonic-gate  */
47667c478bd9Sstevel@tonic-gate static int
dgraph_set_instance_state(scf_handle_t * h,const char * inst_name,protocol_states_t * states)47677c478bd9Sstevel@tonic-gate dgraph_set_instance_state(scf_handle_t *h, const char *inst_name,
4768f6e214c7SGavin Maltby     protocol_states_t *states)
47697c478bd9Sstevel@tonic-gate {
47707c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
477199b44c3bSlianep 	int err = 0;
47727c478bd9Sstevel@tonic-gate 	restarter_instance_state_t old_state;
4773f6e214c7SGavin Maltby 	restarter_instance_state_t state = states->ps_state;
4774f6e214c7SGavin Maltby 	restarter_error_t serr = states->ps_err;
47757c478bd9Sstevel@tonic-gate 
47767c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
47777c478bd9Sstevel@tonic-gate 
47787c478bd9Sstevel@tonic-gate 	v = vertex_get_by_name(inst_name);
47797c478bd9Sstevel@tonic-gate 	if (v == NULL) {
47807c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
47817c478bd9Sstevel@tonic-gate 		return (ENOENT);
47827c478bd9Sstevel@tonic-gate 	}
47837c478bd9Sstevel@tonic-gate 
478456e23938Sbustos 	assert(v->gv_type == GVT_INST);
478556e23938Sbustos 
47867c478bd9Sstevel@tonic-gate 	switch (state) {
47877c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_UNINIT:
47887c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_DISABLED:
47897c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_OFFLINE:
47907c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_ONLINE:
47917c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_DEGRADED:
47927c478bd9Sstevel@tonic-gate 	case RESTARTER_STATE_MAINT:
47937c478bd9Sstevel@tonic-gate 		break;
47947c478bd9Sstevel@tonic-gate 
47957c478bd9Sstevel@tonic-gate 	default:
47967c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
47977c478bd9Sstevel@tonic-gate 		return (EINVAL);
47987c478bd9Sstevel@tonic-gate 	}
47997c478bd9Sstevel@tonic-gate 
48007c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph noting %s %s -> %s.\n", v->gv_name,
48017c478bd9Sstevel@tonic-gate 	    instance_state_str[v->gv_state], instance_state_str[state]);
48027c478bd9Sstevel@tonic-gate 
48037c478bd9Sstevel@tonic-gate 	old_state = v->gv_state;
48047c478bd9Sstevel@tonic-gate 	v->gv_state = state;
48057c478bd9Sstevel@tonic-gate 
4806f6e214c7SGavin Maltby 	v->gv_reason = states->ps_reason;
4807cd3bce3eSlianep 	err = gt_transition(h, v, serr, old_state);
4808f6e214c7SGavin Maltby 	if (err == 0 && v->gv_state != old_state) {
4809f6e214c7SGavin Maltby 		dgraph_state_transition_notify(v, old_state, states->ps_reason);
4810f6e214c7SGavin Maltby 	}
481199b44c3bSlianep 
481299b44c3bSlianep 	MUTEX_UNLOCK(&dgraph_lock);
481399b44c3bSlianep 	return (err);
481499b44c3bSlianep }
481599b44c3bSlianep 
481699b44c3bSlianep /*
481756e23938Sbustos  * Handle state changes during milestone shutdown.  See
481856e23938Sbustos  * dgraph_set_milestone().  If the repository connection is broken,
481956e23938Sbustos  * ECONNABORTED will be returned, though a _DISABLE command will be sent for
482056e23938Sbustos  * the vertex anyway.
482199b44c3bSlianep  */
482256e23938Sbustos int
vertex_subgraph_dependencies_shutdown(scf_handle_t * h,graph_vertex_t * v,restarter_instance_state_t old_state)482399b44c3bSlianep vertex_subgraph_dependencies_shutdown(scf_handle_t *h, graph_vertex_t *v,
482456e23938Sbustos     restarter_instance_state_t old_state)
482599b44c3bSlianep {
482656e23938Sbustos 	int was_up, now_up;
482756e23938Sbustos 	int ret = 0;
482899b44c3bSlianep 
482956e23938Sbustos 	assert(v->gv_type == GVT_INST);
48307c478bd9Sstevel@tonic-gate 
483156e23938Sbustos 	/* Don't care if we're not going to a milestone. */
483256e23938Sbustos 	if (milestone == NULL)
483356e23938Sbustos 		return (0);
483456e23938Sbustos 
483556e23938Sbustos 	/* Don't care if we already finished coming down. */
483656e23938Sbustos 	if (non_subgraph_svcs == 0)
483756e23938Sbustos 		return (0);
483856e23938Sbustos 
483956e23938Sbustos 	/* Don't care if the service is in the subgraph. */
484056e23938Sbustos 	if (v->gv_flags & GV_INSUBGRAPH)
484156e23938Sbustos 		return (0);
484256e23938Sbustos 
484356e23938Sbustos 	/*
484456e23938Sbustos 	 * Update non_subgraph_svcs.  It is the number of non-subgraph
484556e23938Sbustos 	 * services which are in online, degraded, or offline.
484656e23938Sbustos 	 */
484756e23938Sbustos 
484856e23938Sbustos 	was_up = up_state(old_state);
484956e23938Sbustos 	now_up = up_state(v->gv_state);
485056e23938Sbustos 
485156e23938Sbustos 	if (!was_up && now_up) {
485256e23938Sbustos 		++non_subgraph_svcs;
485356e23938Sbustos 	} else if (was_up && !now_up) {
48547c478bd9Sstevel@tonic-gate 		--non_subgraph_svcs;
485556e23938Sbustos 
48567c478bd9Sstevel@tonic-gate 		if (non_subgraph_svcs == 0) {
48577c478bd9Sstevel@tonic-gate 			if (halting != -1) {
48587c478bd9Sstevel@tonic-gate 				do_uadmin();
48597c478bd9Sstevel@tonic-gate 			} else if (go_single_user_mode || go_to_level1) {
48607c478bd9Sstevel@tonic-gate 				(void) startd_thread_create(single_user_thread,
48617c478bd9Sstevel@tonic-gate 				    NULL);
48627c478bd9Sstevel@tonic-gate 			}
486356e23938Sbustos 			return (0);
48647c478bd9Sstevel@tonic-gate 		}
48657c478bd9Sstevel@tonic-gate 	}
486656e23938Sbustos 
486756e23938Sbustos 	/* If this service is a leaf, it should be disabled. */
486856e23938Sbustos 	if ((v->gv_flags & GV_ENABLED) && is_nonsubgraph_leaf(v)) {
486956e23938Sbustos 		int r;
487056e23938Sbustos 
487156e23938Sbustos 		r = disable_service_temporarily(v, h);
487256e23938Sbustos 		switch (r) {
487356e23938Sbustos 		case 0:
487456e23938Sbustos 			break;
487556e23938Sbustos 
487656e23938Sbustos 		case ECONNABORTED:
487756e23938Sbustos 			ret = ECONNABORTED;
487856e23938Sbustos 			break;
487956e23938Sbustos 
488056e23938Sbustos 		default:
488156e23938Sbustos 			bad_error("disable_service_temporarily", r);
488256e23938Sbustos 		}
488356e23938Sbustos 	}
488456e23938Sbustos 
488556e23938Sbustos 	/*
488656e23938Sbustos 	 * If the service just came down, propagate the disable to the newly
488756e23938Sbustos 	 * exposed leaves.
488856e23938Sbustos 	 */
488956e23938Sbustos 	if (was_up && !now_up)
489056e23938Sbustos 		graph_walk_dependencies(v, disable_nonsubgraph_leaves,
489156e23938Sbustos 		    (void *)h);
489256e23938Sbustos 
489356e23938Sbustos 	return (ret);
48947c478bd9Sstevel@tonic-gate }
48957c478bd9Sstevel@tonic-gate 
48967c478bd9Sstevel@tonic-gate /*
489799b44c3bSlianep  * Decide whether to start up an sulogin thread after a service is
489899b44c3bSlianep  * finished changing state.  Only need to do the full can_come_up()
489999b44c3bSlianep  * evaluation if an instance is changing state, we're not halfway through
490099b44c3bSlianep  * loading the thread, and we aren't shutting down or going to the single
490199b44c3bSlianep  * user milestone.
49027c478bd9Sstevel@tonic-gate  */
490399b44c3bSlianep void
graph_transition_sulogin(restarter_instance_state_t state,restarter_instance_state_t old_state)490499b44c3bSlianep graph_transition_sulogin(restarter_instance_state_t state,
490599b44c3bSlianep     restarter_instance_state_t old_state)
490699b44c3bSlianep {
490753f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
49087c478bd9Sstevel@tonic-gate 
49097c478bd9Sstevel@tonic-gate 	if (state != old_state && st->st_load_complete &&
49107c478bd9Sstevel@tonic-gate 	    !go_single_user_mode && !go_to_level1 &&
49117c478bd9Sstevel@tonic-gate 	    halting == -1) {
491273b709eaSrm88369 		if (!sulogin_thread_running && !can_come_up()) {
49137c478bd9Sstevel@tonic-gate 			(void) startd_thread_create(sulogin_thread, NULL);
49147c478bd9Sstevel@tonic-gate 			sulogin_thread_running = B_TRUE;
49157c478bd9Sstevel@tonic-gate 		}
49167c478bd9Sstevel@tonic-gate 	}
491799b44c3bSlianep }
49187c478bd9Sstevel@tonic-gate 
491999b44c3bSlianep /*
4920cd3bce3eSlianep  * Propagate a start, stop event, or a satisfiability event.
492199b44c3bSlianep  *
4922cd3bce3eSlianep  * PROPAGATE_START and PROPAGATE_STOP simply propagate the transition event
4923cd3bce3eSlianep  * to direct dependents.  PROPAGATE_SAT propagates a start then walks the
4924cd3bce3eSlianep  * full dependent graph to check for newly satisfied nodes.  This is
4925cd3bce3eSlianep  * necessary for cases when non-direct dependents may be effected but direct
4926cd3bce3eSlianep  * dependents may not (e.g. for optional_all evaluations, see the
4927cd3bce3eSlianep  * propagate_satbility() comments).
4928cd3bce3eSlianep  *
4929cd3bce3eSlianep  * PROPAGATE_SAT should be used whenever a non-running service moves into
4930cd3bce3eSlianep  * a state which can satisfy optional dependencies, like disabled or
4931cd3bce3eSlianep  * maintenance.
493299b44c3bSlianep  */
493399b44c3bSlianep void
graph_transition_propagate(graph_vertex_t * v,propagate_event_t type,restarter_error_t rerr)4934cd3bce3eSlianep graph_transition_propagate(graph_vertex_t *v, propagate_event_t type,
493599b44c3bSlianep     restarter_error_t rerr)
493699b44c3bSlianep {
4937cd3bce3eSlianep 	if (type == PROPAGATE_STOP) {
493899b44c3bSlianep 		graph_walk_dependents(v, propagate_stop, (void *)rerr);
4939cd3bce3eSlianep 	} else if (type == PROPAGATE_START || type == PROPAGATE_SAT) {
49403e207980SAndrew Stormont 		graph_walk_dependents(v, propagate_start, (void *)RERR_NONE);
49417c478bd9Sstevel@tonic-gate 
4942cd3bce3eSlianep 		if (type == PROPAGATE_SAT)
494399b44c3bSlianep 			propagate_satbility(v);
494499b44c3bSlianep 	} else {
494599b44c3bSlianep #ifndef NDEBUG
4946cd3bce3eSlianep 		uu_warn("%s:%d: Unexpected type value %d.\n",  __FILE__,
4947cd3bce3eSlianep 		    __LINE__, type);
494899b44c3bSlianep #endif
494999b44c3bSlianep 		abort();
495099b44c3bSlianep 	}
49517c478bd9Sstevel@tonic-gate }
49527c478bd9Sstevel@tonic-gate 
49537c478bd9Sstevel@tonic-gate /*
49547c478bd9Sstevel@tonic-gate  * If a vertex for fmri exists and it is enabled, send _DISABLE to the
49557c478bd9Sstevel@tonic-gate  * restarter.  If it is running, send _STOP.  Send _REMOVE_INSTANCE.  Delete
49567c478bd9Sstevel@tonic-gate  * all property group dependencies, and the dependency on the restarter,
49577c478bd9Sstevel@tonic-gate  * disposing of vertices as appropriate.  If other vertices depend on this
49587c478bd9Sstevel@tonic-gate  * one, mark it unconfigured and return.  Otherwise remove the vertex.  Always
49597c478bd9Sstevel@tonic-gate  * returns 0.
49607c478bd9Sstevel@tonic-gate  */
49617c478bd9Sstevel@tonic-gate static int
dgraph_remove_instance(const char * fmri,scf_handle_t * h)49627c478bd9Sstevel@tonic-gate dgraph_remove_instance(const char *fmri, scf_handle_t *h)
49637c478bd9Sstevel@tonic-gate {
49647c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
49657c478bd9Sstevel@tonic-gate 	graph_edge_t *e;
49667c478bd9Sstevel@tonic-gate 	uu_list_t *old_deps;
49677c478bd9Sstevel@tonic-gate 	int err;
49687c478bd9Sstevel@tonic-gate 
49697c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Graph engine: Removing %s.\n", fmri);
49707c478bd9Sstevel@tonic-gate 
49717c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
49727c478bd9Sstevel@tonic-gate 
49737c478bd9Sstevel@tonic-gate 	v = vertex_get_by_name(fmri);
49747c478bd9Sstevel@tonic-gate 	if (v == NULL) {
49757c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
49767c478bd9Sstevel@tonic-gate 		return (0);
49777c478bd9Sstevel@tonic-gate 	}
49787c478bd9Sstevel@tonic-gate 
49797c478bd9Sstevel@tonic-gate 	/* Send restarter delete event. */
49807c478bd9Sstevel@tonic-gate 	if (v->gv_flags & GV_CONFIGURED)
49817c478bd9Sstevel@tonic-gate 		graph_unset_restarter(v);
49827c478bd9Sstevel@tonic-gate 
49837c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
49847c478bd9Sstevel@tonic-gate 		/*
49857c478bd9Sstevel@tonic-gate 		 * Make a list of v's current dependencies so we can
49867c478bd9Sstevel@tonic-gate 		 * reevaluate their GV_INSUBGRAPH flags after the dependencies
49877c478bd9Sstevel@tonic-gate 		 * are removed.
49887c478bd9Sstevel@tonic-gate 		 */
49897c478bd9Sstevel@tonic-gate 		old_deps = startd_list_create(graph_edge_pool, NULL, 0);
49907c478bd9Sstevel@tonic-gate 
49917c478bd9Sstevel@tonic-gate 		err = uu_list_walk(v->gv_dependencies,
49923ad28c1eSrm88369 		    (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0);
49937c478bd9Sstevel@tonic-gate 		assert(err == 0);
49947c478bd9Sstevel@tonic-gate 	}
49957c478bd9Sstevel@tonic-gate 
49967c478bd9Sstevel@tonic-gate 	delete_instance_dependencies(v, B_TRUE);
49977c478bd9Sstevel@tonic-gate 
49987c478bd9Sstevel@tonic-gate 	/*
49997c478bd9Sstevel@tonic-gate 	 * Deleting an instance can both satisfy and unsatisfy dependencies,
50007c478bd9Sstevel@tonic-gate 	 * depending on their type.  First propagate the stop as a RERR_RESTART
50017c478bd9Sstevel@tonic-gate 	 * event -- deletion isn't a fault, just a normal stop.  This gives
50027c478bd9Sstevel@tonic-gate 	 * dependent services the chance to do a clean shutdown.  Then, mark
50037c478bd9Sstevel@tonic-gate 	 * the service as unconfigured and propagate the start event for the
50047c478bd9Sstevel@tonic-gate 	 * optional_all dependencies that might have become satisfied.
50057c478bd9Sstevel@tonic-gate 	 */
50067c478bd9Sstevel@tonic-gate 	graph_walk_dependents(v, propagate_stop, (void *)RERR_RESTART);
50077c478bd9Sstevel@tonic-gate 
50087c478bd9Sstevel@tonic-gate 	v->gv_flags &= ~GV_CONFIGURED;
500970cbfe41SPhilippe Jung 	v->gv_flags &= ~GV_DEATHROW;
50107c478bd9Sstevel@tonic-gate 
50113e207980SAndrew Stormont 	graph_walk_dependents(v, propagate_start, (void *)RERR_NONE);
50127c478bd9Sstevel@tonic-gate 	propagate_satbility(v);
50137c478bd9Sstevel@tonic-gate 
50147c478bd9Sstevel@tonic-gate 	/*
50157c478bd9Sstevel@tonic-gate 	 * If there are no (non-service) dependents, the vertex can be
50167c478bd9Sstevel@tonic-gate 	 * completely removed.
50177c478bd9Sstevel@tonic-gate 	 */
50183ad28c1eSrm88369 	if (v != milestone && v->gv_refs == 0 &&
50193ad28c1eSrm88369 	    uu_list_numnodes(v->gv_dependents) == 1)
50207c478bd9Sstevel@tonic-gate 		remove_inst_vertex(v);
50217c478bd9Sstevel@tonic-gate 
50227c478bd9Sstevel@tonic-gate 	if (milestone > MILESTONE_NONE) {
50237c478bd9Sstevel@tonic-gate 		void *cookie = NULL;
50247c478bd9Sstevel@tonic-gate 
50257c478bd9Sstevel@tonic-gate 		while ((e = uu_list_teardown(old_deps, &cookie)) != NULL) {
50263ad28c1eSrm88369 			v = e->ge_vertex;
50273ad28c1eSrm88369 
50283ad28c1eSrm88369 			if (vertex_unref(v) == VERTEX_INUSE)
50293ad28c1eSrm88369 				while (eval_subgraph(v, h) == ECONNABORTED)
50307c478bd9Sstevel@tonic-gate 					libscf_handle_rebind(h);
50317c478bd9Sstevel@tonic-gate 
50327c478bd9Sstevel@tonic-gate 			startd_free(e, sizeof (*e));
50337c478bd9Sstevel@tonic-gate 		}
50347c478bd9Sstevel@tonic-gate 
50357c478bd9Sstevel@tonic-gate 		uu_list_destroy(old_deps);
50367c478bd9Sstevel@tonic-gate 	}
50377c478bd9Sstevel@tonic-gate 
50387c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
50397c478bd9Sstevel@tonic-gate 
50407c478bd9Sstevel@tonic-gate 	return (0);
50417c478bd9Sstevel@tonic-gate }
50427c478bd9Sstevel@tonic-gate 
50437c478bd9Sstevel@tonic-gate /*
50447c478bd9Sstevel@tonic-gate  * Return the eventual (maybe current) milestone in the form of a
50457c478bd9Sstevel@tonic-gate  * legacy runlevel.
50467c478bd9Sstevel@tonic-gate  */
50477c478bd9Sstevel@tonic-gate static char
target_milestone_as_runlevel()50487c478bd9Sstevel@tonic-gate target_milestone_as_runlevel()
50497c478bd9Sstevel@tonic-gate {
505053f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
50517c478bd9Sstevel@tonic-gate 
50527c478bd9Sstevel@tonic-gate 	if (milestone == NULL)
50537c478bd9Sstevel@tonic-gate 		return ('3');
50547c478bd9Sstevel@tonic-gate 	else if (milestone == MILESTONE_NONE)
50557c478bd9Sstevel@tonic-gate 		return ('0');
50567c478bd9Sstevel@tonic-gate 
50577c478bd9Sstevel@tonic-gate 	if (strcmp(milestone->gv_name, multi_user_fmri) == 0)
50587c478bd9Sstevel@tonic-gate 		return ('2');
50597c478bd9Sstevel@tonic-gate 	else if (strcmp(milestone->gv_name, single_user_fmri) == 0)
50607c478bd9Sstevel@tonic-gate 		return ('S');
50617c478bd9Sstevel@tonic-gate 	else if (strcmp(milestone->gv_name, multi_user_svr_fmri) == 0)
50627c478bd9Sstevel@tonic-gate 		return ('3');
50637c478bd9Sstevel@tonic-gate 
50647c478bd9Sstevel@tonic-gate #ifndef NDEBUG
50657c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:%d: Unknown milestone name \"%s\".\n",
50667c478bd9Sstevel@tonic-gate 	    __FILE__, __LINE__, milestone->gv_name);
50677c478bd9Sstevel@tonic-gate #endif
50687c478bd9Sstevel@tonic-gate 	abort();
50697c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
50707c478bd9Sstevel@tonic-gate }
50717c478bd9Sstevel@tonic-gate 
50727c478bd9Sstevel@tonic-gate static struct {
50737c478bd9Sstevel@tonic-gate 	char	rl;
50747c478bd9Sstevel@tonic-gate 	int	sig;
50757c478bd9Sstevel@tonic-gate } init_sigs[] = {
50767c478bd9Sstevel@tonic-gate 	{ 'S', SIGBUS },
50777c478bd9Sstevel@tonic-gate 	{ '0', SIGINT },
50787c478bd9Sstevel@tonic-gate 	{ '1', SIGQUIT },
50797c478bd9Sstevel@tonic-gate 	{ '2', SIGILL },
50807c478bd9Sstevel@tonic-gate 	{ '3', SIGTRAP },
50817c478bd9Sstevel@tonic-gate 	{ '4', SIGIOT },
50827c478bd9Sstevel@tonic-gate 	{ '5', SIGEMT },
50837c478bd9Sstevel@tonic-gate 	{ '6', SIGFPE },
50847c478bd9Sstevel@tonic-gate 	{ 0, 0 }
50857c478bd9Sstevel@tonic-gate };
50867c478bd9Sstevel@tonic-gate 
50877c478bd9Sstevel@tonic-gate static void
signal_init(char rl)50887c478bd9Sstevel@tonic-gate signal_init(char rl)
50897c478bd9Sstevel@tonic-gate {
50907c478bd9Sstevel@tonic-gate 	pid_t init_pid;
50917c478bd9Sstevel@tonic-gate 	int i;
50927c478bd9Sstevel@tonic-gate 
509353f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
50947c478bd9Sstevel@tonic-gate 
50957c478bd9Sstevel@tonic-gate 	if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
50967c478bd9Sstevel@tonic-gate 	    sizeof (init_pid)) != sizeof (init_pid)) {
50977c478bd9Sstevel@tonic-gate 		log_error(LOG_NOTICE, "Could not get pid to signal init.\n");
50987c478bd9Sstevel@tonic-gate 		return;
50997c478bd9Sstevel@tonic-gate 	}
51007c478bd9Sstevel@tonic-gate 
51017c478bd9Sstevel@tonic-gate 	for (i = 0; init_sigs[i].rl != 0; ++i)
51027c478bd9Sstevel@tonic-gate 		if (init_sigs[i].rl == rl)
51037c478bd9Sstevel@tonic-gate 			break;
51047c478bd9Sstevel@tonic-gate 
51057c478bd9Sstevel@tonic-gate 	if (init_sigs[i].rl != 0) {
51067c478bd9Sstevel@tonic-gate 		if (kill(init_pid, init_sigs[i].sig) != 0) {
51077c478bd9Sstevel@tonic-gate 			switch (errno) {
51087c478bd9Sstevel@tonic-gate 			case EPERM:
51097c478bd9Sstevel@tonic-gate 			case ESRCH:
51107c478bd9Sstevel@tonic-gate 				log_error(LOG_NOTICE, "Could not signal init: "
51117c478bd9Sstevel@tonic-gate 				    "%s.\n", strerror(errno));
51127c478bd9Sstevel@tonic-gate 				break;
51137c478bd9Sstevel@tonic-gate 
51147c478bd9Sstevel@tonic-gate 			case EINVAL:
51157c478bd9Sstevel@tonic-gate 			default:
51167c478bd9Sstevel@tonic-gate 				bad_error("kill", errno);
51177c478bd9Sstevel@tonic-gate 			}
51187c478bd9Sstevel@tonic-gate 		}
51197c478bd9Sstevel@tonic-gate 	}
51207c478bd9Sstevel@tonic-gate }
51217c478bd9Sstevel@tonic-gate 
51227c478bd9Sstevel@tonic-gate /*
51237c478bd9Sstevel@tonic-gate  * This is called when one of the major milestones changes state, or when
51247c478bd9Sstevel@tonic-gate  * init is signalled and tells us it was told to change runlevel.  We wait
51257c478bd9Sstevel@tonic-gate  * to reach the milestone because this allows /etc/inittab entries to retain
51267c478bd9Sstevel@tonic-gate  * some boot ordering: historically, entries could place themselves before/after
51277c478bd9Sstevel@tonic-gate  * the running of /sbin/rcX scripts but we can no longer make the
51287c478bd9Sstevel@tonic-gate  * distinction because the /sbin/rcX scripts no longer exist as punctuation
51297c478bd9Sstevel@tonic-gate  * marks in /etc/inittab.
51307c478bd9Sstevel@tonic-gate  *
51317c478bd9Sstevel@tonic-gate  * Also, we only trigger an update when we reach the eventual target
51327c478bd9Sstevel@tonic-gate  * milestone: without this, an /etc/inittab entry marked only for
51337c478bd9Sstevel@tonic-gate  * runlevel 2 would be executed for runlevel 3, which is not how
51347c478bd9Sstevel@tonic-gate  * /etc/inittab entries work.
51357c478bd9Sstevel@tonic-gate  *
51367c478bd9Sstevel@tonic-gate  * If we're single user coming online, then we set utmpx to the target
51377c478bd9Sstevel@tonic-gate  * runlevel so that legacy scripts can work as expected.
51387c478bd9Sstevel@tonic-gate  */
51397c478bd9Sstevel@tonic-gate static void
graph_runlevel_changed(char rl,int online)51407c478bd9Sstevel@tonic-gate graph_runlevel_changed(char rl, int online)
51417c478bd9Sstevel@tonic-gate {
51427c478bd9Sstevel@tonic-gate 	char trl;
51437c478bd9Sstevel@tonic-gate 
514453f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dgraph_lock));
51457c478bd9Sstevel@tonic-gate 
51467c478bd9Sstevel@tonic-gate 	trl = target_milestone_as_runlevel();
51477c478bd9Sstevel@tonic-gate 
51487c478bd9Sstevel@tonic-gate 	if (online) {
51497c478bd9Sstevel@tonic-gate 		if (rl == trl) {
5150965e507bSrm88369 			current_runlevel = trl;
51517c478bd9Sstevel@tonic-gate 			signal_init(trl);
51527c478bd9Sstevel@tonic-gate 		} else if (rl == 'S') {
51537c478bd9Sstevel@tonic-gate 			/*
51547c478bd9Sstevel@tonic-gate 			 * At boot, set the entry early for the benefit of the
51557c478bd9Sstevel@tonic-gate 			 * legacy init scripts.
51567c478bd9Sstevel@tonic-gate 			 */
51577c478bd9Sstevel@tonic-gate 			utmpx_set_runlevel(trl, 'S', B_FALSE);
51587c478bd9Sstevel@tonic-gate 		}
51597c478bd9Sstevel@tonic-gate 	} else {
51607c478bd9Sstevel@tonic-gate 		if (rl == '3' && trl == '2') {
5161965e507bSrm88369 			current_runlevel = trl;
51627c478bd9Sstevel@tonic-gate 			signal_init(trl);
51637c478bd9Sstevel@tonic-gate 		} else if (rl == '2' && trl == 'S') {
5164965e507bSrm88369 			current_runlevel = trl;
51657c478bd9Sstevel@tonic-gate 			signal_init(trl);
51667c478bd9Sstevel@tonic-gate 		}
51677c478bd9Sstevel@tonic-gate 	}
51687c478bd9Sstevel@tonic-gate }
51697c478bd9Sstevel@tonic-gate 
51707c478bd9Sstevel@tonic-gate /*
51717c478bd9Sstevel@tonic-gate  * Move to a backwards-compatible runlevel by executing the appropriate
51727c478bd9Sstevel@tonic-gate  * /etc/rc?.d/K* scripts and/or setting the milestone.
51737c478bd9Sstevel@tonic-gate  *
51747c478bd9Sstevel@tonic-gate  * Returns
51757c478bd9Sstevel@tonic-gate  *   0 - success
51767c478bd9Sstevel@tonic-gate  *   ECONNRESET - success, but handle was reset
51777c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
51787c478bd9Sstevel@tonic-gate  *   ECANCELED - pg was deleted
51797c478bd9Sstevel@tonic-gate  */
51807c478bd9Sstevel@tonic-gate static int
dgraph_set_runlevel(scf_propertygroup_t * pg,scf_property_t * prop)51817c478bd9Sstevel@tonic-gate dgraph_set_runlevel(scf_propertygroup_t *pg, scf_property_t *prop)
51827c478bd9Sstevel@tonic-gate {
51837c478bd9Sstevel@tonic-gate 	char rl;
51847c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
51857c478bd9Sstevel@tonic-gate 	int r;
51867c478bd9Sstevel@tonic-gate 	const char *ms = NULL;	/* what to commit as options/milestone */
51877c478bd9Sstevel@tonic-gate 	boolean_t rebound = B_FALSE;
51887c478bd9Sstevel@tonic-gate 	int mark_rl = 0;
51897c478bd9Sstevel@tonic-gate 
51907c478bd9Sstevel@tonic-gate 	const char * const stop = "stop";
51917c478bd9Sstevel@tonic-gate 
51927c478bd9Sstevel@tonic-gate 	r = libscf_extract_runlevel(prop, &rl);
51937c478bd9Sstevel@tonic-gate 	switch (r) {
51947c478bd9Sstevel@tonic-gate 	case 0:
51957c478bd9Sstevel@tonic-gate 		break;
51967c478bd9Sstevel@tonic-gate 
51977c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
51987c478bd9Sstevel@tonic-gate 	case ECANCELED:
51997c478bd9Sstevel@tonic-gate 		return (r);
52007c478bd9Sstevel@tonic-gate 
52017c478bd9Sstevel@tonic-gate 	case EINVAL:
52027c478bd9Sstevel@tonic-gate 	case ENOENT:
52037c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "runlevel property is misconfigured; "
52047c478bd9Sstevel@tonic-gate 		    "ignoring.\n");
52057c478bd9Sstevel@tonic-gate 		/* delete the bad property */
52067c478bd9Sstevel@tonic-gate 		goto nolock_out;
52077c478bd9Sstevel@tonic-gate 
52087c478bd9Sstevel@tonic-gate 	default:
52097c478bd9Sstevel@tonic-gate 		bad_error("libscf_extract_runlevel", r);
52107c478bd9Sstevel@tonic-gate 	}
52117c478bd9Sstevel@tonic-gate 
52127c478bd9Sstevel@tonic-gate 	switch (rl) {
52137c478bd9Sstevel@tonic-gate 	case 's':
52147c478bd9Sstevel@tonic-gate 		rl = 'S';
52157c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
52167c478bd9Sstevel@tonic-gate 
52177c478bd9Sstevel@tonic-gate 	case 'S':
52187c478bd9Sstevel@tonic-gate 	case '2':
52197c478bd9Sstevel@tonic-gate 	case '3':
52207c478bd9Sstevel@tonic-gate 		/*
52217c478bd9Sstevel@tonic-gate 		 * These cases cause a milestone change, so
52227c478bd9Sstevel@tonic-gate 		 * graph_runlevel_changed() will eventually deal with
52237c478bd9Sstevel@tonic-gate 		 * signalling init.
52247c478bd9Sstevel@tonic-gate 		 */
52257c478bd9Sstevel@tonic-gate 		break;
52267c478bd9Sstevel@tonic-gate 
52277c478bd9Sstevel@tonic-gate 	case '0':
52287c478bd9Sstevel@tonic-gate 	case '1':
52297c478bd9Sstevel@tonic-gate 	case '4':
52307c478bd9Sstevel@tonic-gate 	case '5':
52317c478bd9Sstevel@tonic-gate 	case '6':
52327c478bd9Sstevel@tonic-gate 		mark_rl = 1;
52337c478bd9Sstevel@tonic-gate 		break;
52347c478bd9Sstevel@tonic-gate 
52357c478bd9Sstevel@tonic-gate 	default:
52367c478bd9Sstevel@tonic-gate 		log_framework(LOG_NOTICE, "Unknown runlevel '%c'.\n", rl);
52377c478bd9Sstevel@tonic-gate 		ms = NULL;
52387c478bd9Sstevel@tonic-gate 		goto nolock_out;
52397c478bd9Sstevel@tonic-gate 	}
52407c478bd9Sstevel@tonic-gate 
52417c478bd9Sstevel@tonic-gate 	h = scf_pg_handle(pg);
52427c478bd9Sstevel@tonic-gate 
52437c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
52447c478bd9Sstevel@tonic-gate 
52457c478bd9Sstevel@tonic-gate 	/*
52467c478bd9Sstevel@tonic-gate 	 * Since this triggers no milestone changes, force it by hand.
52477c478bd9Sstevel@tonic-gate 	 */
52487c478bd9Sstevel@tonic-gate 	if (current_runlevel == '4' && rl == '3')
52497c478bd9Sstevel@tonic-gate 		mark_rl = 1;
52507c478bd9Sstevel@tonic-gate 
5251965e507bSrm88369 	/*
5252965e507bSrm88369 	 * 1. If we are here after an "init X":
5253965e507bSrm88369 	 *
5254965e507bSrm88369 	 * init X
5255965e507bSrm88369 	 *	init/lscf_set_runlevel()
5256965e507bSrm88369 	 *		process_pg_event()
5257965e507bSrm88369 	 *		dgraph_set_runlevel()
5258965e507bSrm88369 	 *
5259965e507bSrm88369 	 * then we haven't passed through graph_runlevel_changed() yet,
5260965e507bSrm88369 	 * therefore 'current_runlevel' has not changed for sure but 'rl' has.
5261965e507bSrm88369 	 * In consequence, if 'rl' is lower than 'current_runlevel', we change
5262965e507bSrm88369 	 * the system runlevel and execute the appropriate /etc/rc?.d/K* scripts
5263965e507bSrm88369 	 * past this test.
5264965e507bSrm88369 	 *
5265965e507bSrm88369 	 * 2. On the other hand, if we are here after a "svcadm milestone":
5266965e507bSrm88369 	 *
5267965e507bSrm88369 	 * svcadm milestone X
5268965e507bSrm88369 	 *	dgraph_set_milestone()
5269965e507bSrm88369 	 *		handle_graph_update_event()
5270965e507bSrm88369 	 *		dgraph_set_instance_state()
5271965e507bSrm88369 	 *		graph_post_X_[online|offline]()
5272965e507bSrm88369 	 *		graph_runlevel_changed()
5273965e507bSrm88369 	 *		signal_init()
5274965e507bSrm88369 	 *			init/lscf_set_runlevel()
5275965e507bSrm88369 	 *				process_pg_event()
5276965e507bSrm88369 	 *				dgraph_set_runlevel()
5277965e507bSrm88369 	 *
5278965e507bSrm88369 	 * then we already passed through graph_runlevel_changed() (by the way
5279965e507bSrm88369 	 * of dgraph_set_milestone()) and 'current_runlevel' may have changed
5280965e507bSrm88369 	 * and already be equal to 'rl' so we are going to return immediately
5281965e507bSrm88369 	 * from dgraph_set_runlevel() without changing the system runlevel and
5282965e507bSrm88369 	 * without executing the /etc/rc?.d/K* scripts.
5283965e507bSrm88369 	 */
52847c478bd9Sstevel@tonic-gate 	if (rl == current_runlevel) {
52857c478bd9Sstevel@tonic-gate 		ms = NULL;
52867c478bd9Sstevel@tonic-gate 		goto out;
52877c478bd9Sstevel@tonic-gate 	}
52887c478bd9Sstevel@tonic-gate 
52897c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Changing to runlevel '%c'.\n", rl);
52907c478bd9Sstevel@tonic-gate 
52917c478bd9Sstevel@tonic-gate 	/*
52927c478bd9Sstevel@tonic-gate 	 * Make sure stop rc scripts see the new settings via who -r.
52937c478bd9Sstevel@tonic-gate 	 */
52947c478bd9Sstevel@tonic-gate 	utmpx_set_runlevel(rl, current_runlevel, B_TRUE);
52957c478bd9Sstevel@tonic-gate 
52967c478bd9Sstevel@tonic-gate 	/*
52977c478bd9Sstevel@tonic-gate 	 * Some run levels don't have a direct correspondence to any
52987c478bd9Sstevel@tonic-gate 	 * milestones, so we have to signal init directly.
52997c478bd9Sstevel@tonic-gate 	 */
53007c478bd9Sstevel@tonic-gate 	if (mark_rl) {
53017c478bd9Sstevel@tonic-gate 		current_runlevel = rl;
53027c478bd9Sstevel@tonic-gate 		signal_init(rl);
53037c478bd9Sstevel@tonic-gate 	}
53047c478bd9Sstevel@tonic-gate 
53057c478bd9Sstevel@tonic-gate 	switch (rl) {
53067c478bd9Sstevel@tonic-gate 	case 'S':
53077c478bd9Sstevel@tonic-gate 		uu_warn("The system is coming down for administration.  "
53087c478bd9Sstevel@tonic-gate 		    "Please wait.\n");
53097c478bd9Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_FALSE);
53107c478bd9Sstevel@tonic-gate 		ms = single_user_fmri;
53117c478bd9Sstevel@tonic-gate 		go_single_user_mode = B_TRUE;
53127c478bd9Sstevel@tonic-gate 		break;
53137c478bd9Sstevel@tonic-gate 
53147c478bd9Sstevel@tonic-gate 	case '0':
53154d53c7adSDan Price 		halting_time = time(NULL);
53167c478bd9Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
53177c478bd9Sstevel@tonic-gate 		halting = AD_HALT;
53187c478bd9Sstevel@tonic-gate 		goto uadmin;
53197c478bd9Sstevel@tonic-gate 
53207c478bd9Sstevel@tonic-gate 	case '5':
53214d53c7adSDan Price 		halting_time = time(NULL);
53227c478bd9Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
53237c478bd9Sstevel@tonic-gate 		halting = AD_POWEROFF;
53247c478bd9Sstevel@tonic-gate 		goto uadmin;
53257c478bd9Sstevel@tonic-gate 
53267c478bd9Sstevel@tonic-gate 	case '6':
53274d53c7adSDan Price 		halting_time = time(NULL);
53287c478bd9Sstevel@tonic-gate 		fork_rc_script(rl, stop, B_TRUE);
5329753a6d45SSherry Moore 		if (scf_is_fastboot_default() && getzoneid() == GLOBAL_ZONEID)
5330753a6d45SSherry Moore 			halting = AD_FASTREBOOT;
5331753a6d45SSherry Moore 		else
53327c478bd9Sstevel@tonic-gate 			halting = AD_BOOT;
53337c478bd9Sstevel@tonic-gate 
53347c478bd9Sstevel@tonic-gate uadmin:
53357c478bd9Sstevel@tonic-gate 		uu_warn("The system is coming down.  Please wait.\n");
53367c478bd9Sstevel@tonic-gate 		ms = "none";
53377c478bd9Sstevel@tonic-gate 
53387c478bd9Sstevel@tonic-gate 		/*
53397c478bd9Sstevel@tonic-gate 		 * We can't wait until all services are offline since this
53407c478bd9Sstevel@tonic-gate 		 * thread is responsible for taking them offline.  Instead we
53417c478bd9Sstevel@tonic-gate 		 * set halting to the second argument for uadmin() and call
53427c478bd9Sstevel@tonic-gate 		 * do_uadmin() from dgraph_set_instance_state() when
53437c478bd9Sstevel@tonic-gate 		 * appropriate.
53447c478bd9Sstevel@tonic-gate 		 */
53457c478bd9Sstevel@tonic-gate 		break;
53467c478bd9Sstevel@tonic-gate 
53477c478bd9Sstevel@tonic-gate 	case '1':
53487c478bd9Sstevel@tonic-gate 		if (current_runlevel != 'S') {
53497c478bd9Sstevel@tonic-gate 			uu_warn("Changing to state 1.\n");
53507c478bd9Sstevel@tonic-gate 			fork_rc_script(rl, stop, B_FALSE);
53517c478bd9Sstevel@tonic-gate 		} else {
53527c478bd9Sstevel@tonic-gate 			uu_warn("The system is coming up for administration.  "
53537c478bd9Sstevel@tonic-gate 			    "Please wait.\n");
53547c478bd9Sstevel@tonic-gate 		}
53557c478bd9Sstevel@tonic-gate 		ms = single_user_fmri;
53567c478bd9Sstevel@tonic-gate 		go_to_level1 = B_TRUE;
53577c478bd9Sstevel@tonic-gate 		break;
53587c478bd9Sstevel@tonic-gate 
53597c478bd9Sstevel@tonic-gate 	case '2':
53607c478bd9Sstevel@tonic-gate 		if (current_runlevel == '3' || current_runlevel == '4')
53617c478bd9Sstevel@tonic-gate 			fork_rc_script(rl, stop, B_FALSE);
53627c478bd9Sstevel@tonic-gate 		ms = multi_user_fmri;
53637c478bd9Sstevel@tonic-gate 		break;
53647c478bd9Sstevel@tonic-gate 
53657c478bd9Sstevel@tonic-gate 	case '3':
53667c478bd9Sstevel@tonic-gate 	case '4':
53677c478bd9Sstevel@tonic-gate 		ms = "all";
53687c478bd9Sstevel@tonic-gate 		break;
53697c478bd9Sstevel@tonic-gate 
53707c478bd9Sstevel@tonic-gate 	default:
53717c478bd9Sstevel@tonic-gate #ifndef NDEBUG
53727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s:%d: Uncaught case %d ('%c').\n",
53737c478bd9Sstevel@tonic-gate 		    __FILE__, __LINE__, rl, rl);
53747c478bd9Sstevel@tonic-gate #endif
53757c478bd9Sstevel@tonic-gate 		abort();
53767c478bd9Sstevel@tonic-gate 	}
53777c478bd9Sstevel@tonic-gate 
53787c478bd9Sstevel@tonic-gate out:
53797c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
53807c478bd9Sstevel@tonic-gate 
53817c478bd9Sstevel@tonic-gate nolock_out:
53827c478bd9Sstevel@tonic-gate 	switch (r = libscf_clear_runlevel(pg, ms)) {
53837c478bd9Sstevel@tonic-gate 	case 0:
53847c478bd9Sstevel@tonic-gate 		break;
53857c478bd9Sstevel@tonic-gate 
53867c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
53877c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
53887c478bd9Sstevel@tonic-gate 		rebound = B_TRUE;
53897c478bd9Sstevel@tonic-gate 		goto nolock_out;
53907c478bd9Sstevel@tonic-gate 
53917c478bd9Sstevel@tonic-gate 	case ECANCELED:
53927c478bd9Sstevel@tonic-gate 		break;
53937c478bd9Sstevel@tonic-gate 
53947c478bd9Sstevel@tonic-gate 	case EPERM:
53957c478bd9Sstevel@tonic-gate 	case EACCES:
53967c478bd9Sstevel@tonic-gate 	case EROFS:
53977c478bd9Sstevel@tonic-gate 		log_error(LOG_NOTICE, "Could not delete \"%s/%s\" property: "
53987c478bd9Sstevel@tonic-gate 		    "%s.\n", SCF_PG_OPTIONS, "runlevel", strerror(r));
53997c478bd9Sstevel@tonic-gate 		break;
54007c478bd9Sstevel@tonic-gate 
54017c478bd9Sstevel@tonic-gate 	default:
54027c478bd9Sstevel@tonic-gate 		bad_error("libscf_clear_runlevel", r);
54037c478bd9Sstevel@tonic-gate 	}
54047c478bd9Sstevel@tonic-gate 
54057c478bd9Sstevel@tonic-gate 	return (rebound ? ECONNRESET : 0);
54067c478bd9Sstevel@tonic-gate }
54077c478bd9Sstevel@tonic-gate 
5408aca380d7SRenaud Manus /*
5409aca380d7SRenaud Manus  * mark_subtree walks the dependents and add the GV_TOOFFLINE flag
5410aca380d7SRenaud Manus  * to the instances that are supposed to go offline during an
5411aca380d7SRenaud Manus  * administrative disable operation.
5412aca380d7SRenaud Manus  */
5413aca380d7SRenaud Manus static int
mark_subtree(graph_edge_t * e,void * arg)5414aca380d7SRenaud Manus mark_subtree(graph_edge_t *e, void *arg)
5415aca380d7SRenaud Manus {
5416aca380d7SRenaud Manus 	graph_vertex_t *v;
5417aca380d7SRenaud Manus 	int r;
5418aca380d7SRenaud Manus 
5419aca380d7SRenaud Manus 	v = e->ge_vertex;
5420aca380d7SRenaud Manus 
5421aca380d7SRenaud Manus 	/* If it's already in the subgraph, skip. */
5422aca380d7SRenaud Manus 	if (v->gv_flags & GV_TOOFFLINE)
5423aca380d7SRenaud Manus 		return (UU_WALK_NEXT);
5424aca380d7SRenaud Manus 
5425aca380d7SRenaud Manus 	switch (v->gv_type) {
5426aca380d7SRenaud Manus 	case GVT_INST:
54273e207980SAndrew Stormont 		/* If the instance is already offline, skip it. */
54283e207980SAndrew Stormont 		if (!inst_running(v))
5429aca380d7SRenaud Manus 			return (UU_WALK_NEXT);
5430aca380d7SRenaud Manus 
5431aca380d7SRenaud Manus 		v->gv_flags |= GV_TOOFFLINE;
5432aca380d7SRenaud Manus 		log_framework(LOG_DEBUG, "%s added to subtree\n", v->gv_name);
5433aca380d7SRenaud Manus 		break;
5434aca380d7SRenaud Manus 	case GVT_GROUP:
5435aca380d7SRenaud Manus 		/*
54363e207980SAndrew Stormont 		 * Skip all excluded dependents and decide whether to offline
54373e207980SAndrew Stormont 		 * the service based on the restart_on attribute.
5438aca380d7SRenaud Manus 		 */
5439653e7459Sgowtham thommandra - Sun Microsystems - Bangalore India 		if (is_depgrp_bypassed(v))
5440aca380d7SRenaud Manus 			return (UU_WALK_NEXT);
5441aca380d7SRenaud Manus 		break;
5442aca380d7SRenaud Manus 	}
5443aca380d7SRenaud Manus 
5444aca380d7SRenaud Manus 	r = uu_list_walk(v->gv_dependents, (uu_walk_fn_t *)mark_subtree, arg,
5445aca380d7SRenaud Manus 	    0);
5446aca380d7SRenaud Manus 	assert(r == 0);
5447aca380d7SRenaud Manus 	return (UU_WALK_NEXT);
5448aca380d7SRenaud Manus }
5449aca380d7SRenaud Manus 
54507c478bd9Sstevel@tonic-gate static int
mark_subgraph(graph_edge_t * e,void * arg)54517c478bd9Sstevel@tonic-gate mark_subgraph(graph_edge_t *e, void *arg)
54527c478bd9Sstevel@tonic-gate {
54537c478bd9Sstevel@tonic-gate 	graph_vertex_t *v;
54547c478bd9Sstevel@tonic-gate 	int r;
54557c478bd9Sstevel@tonic-gate 	int optional = (int)arg;
54567c478bd9Sstevel@tonic-gate 
54577c478bd9Sstevel@tonic-gate 	v = e->ge_vertex;
54587c478bd9Sstevel@tonic-gate 
54597c478bd9Sstevel@tonic-gate 	/* If it's already in the subgraph, skip. */
54607c478bd9Sstevel@tonic-gate 	if (v->gv_flags & GV_INSUBGRAPH)
54617c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
54627c478bd9Sstevel@tonic-gate 
54637c478bd9Sstevel@tonic-gate 	/*
54647c478bd9Sstevel@tonic-gate 	 * Keep track if walk has entered an optional dependency group
54657c478bd9Sstevel@tonic-gate 	 */
54667c478bd9Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_OPTIONAL_ALL) {
54677c478bd9Sstevel@tonic-gate 		optional = 1;
54687c478bd9Sstevel@tonic-gate 	}
54697c478bd9Sstevel@tonic-gate 	/*
54707c478bd9Sstevel@tonic-gate 	 * Quit if we are in an optional dependency group and the instance
54717c478bd9Sstevel@tonic-gate 	 * is disabled
54727c478bd9Sstevel@tonic-gate 	 */
54737c478bd9Sstevel@tonic-gate 	if (optional && (v->gv_type == GVT_INST) &&
54747c478bd9Sstevel@tonic-gate 	    (!(v->gv_flags & GV_ENBLD_NOOVR)))
54757c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
54767c478bd9Sstevel@tonic-gate 
54777c478bd9Sstevel@tonic-gate 	v->gv_flags |= GV_INSUBGRAPH;
54787c478bd9Sstevel@tonic-gate 
54797c478bd9Sstevel@tonic-gate 	/* Skip all excluded dependencies. */
54807c478bd9Sstevel@tonic-gate 	if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
54817c478bd9Sstevel@tonic-gate 		return (UU_WALK_NEXT);
54827c478bd9Sstevel@tonic-gate 
54837c478bd9Sstevel@tonic-gate 	r = uu_list_walk(v->gv_dependencies, (uu_walk_fn_t *)mark_subgraph,
54847c478bd9Sstevel@tonic-gate 	    (void *)optional, 0);
54857c478bd9Sstevel@tonic-gate 	assert(r == 0);
54867c478bd9Sstevel@tonic-gate 	return (UU_WALK_NEXT);
54877c478bd9Sstevel@tonic-gate }
54887c478bd9Sstevel@tonic-gate 
54897c478bd9Sstevel@tonic-gate /*
549056e23938Sbustos  * Bring down all services which are not dependencies of fmri.  The
549156e23938Sbustos  * dependencies of fmri (direct & indirect) will constitute the "subgraph",
549256e23938Sbustos  * and will have the GV_INSUBGRAPH flag set.  The rest must be brought down,
549356e23938Sbustos  * which means the state is "disabled", "maintenance", or "uninitialized".  We
549456e23938Sbustos  * could consider "offline" to be down, and refrain from sending start
549556e23938Sbustos  * commands for such services, but that's not strictly necessary, so we'll
549656e23938Sbustos  * decline to intrude on the state machine.  It would probably confuse users
549756e23938Sbustos  * anyway.
549856e23938Sbustos  *
549956e23938Sbustos  * The services should be brought down in reverse-dependency order, so we
550056e23938Sbustos  * can't do it all at once here.  We initiate by override-disabling the leaves
550156e23938Sbustos  * of the dependency tree -- those services which are up but have no
550256e23938Sbustos  * dependents which are up.  When they come down,
550356e23938Sbustos  * vertex_subgraph_dependencies_shutdown() will override-disable the newly
550456e23938Sbustos  * exposed leaves.  Perseverance will ensure completion.
550556e23938Sbustos  *
550656e23938Sbustos  * Sometimes we need to take action when the transition is complete, like
550756e23938Sbustos  * start sulogin or halt the system.  To tell when we're done, we initialize
550856e23938Sbustos  * non_subgraph_svcs here to be the number of services which need to come
550956e23938Sbustos  * down.  As each does, we decrement the counter.  When it hits zero, we take
551056e23938Sbustos  * the appropriate action.  See vertex_subgraph_dependencies_shutdown().
551156e23938Sbustos  *
551256e23938Sbustos  * In case we're coming up, we also remove any enable-overrides for the
551356e23938Sbustos  * services which are dependencies of fmri.
55147c478bd9Sstevel@tonic-gate  *
55157c478bd9Sstevel@tonic-gate  * If norepository is true, the function will not change the repository.
55167c478bd9Sstevel@tonic-gate  *
5517965e507bSrm88369  * The decision to change the system run level in accordance with the milestone
5518965e507bSrm88369  * is taken in dgraph_set_runlevel().
5519965e507bSrm88369  *
55207c478bd9Sstevel@tonic-gate  * Returns
55217c478bd9Sstevel@tonic-gate  *   0 - success
55227c478bd9Sstevel@tonic-gate  *   ECONNRESET - success, but handle was rebound
55237c478bd9Sstevel@tonic-gate  *   EINVAL - fmri is invalid (error is logged)
55247c478bd9Sstevel@tonic-gate  *   EALREADY - the milestone is already set to fmri
55257c478bd9Sstevel@tonic-gate  *   ENOENT - a configured vertex does not exist for fmri (an error is logged)
55267c478bd9Sstevel@tonic-gate  */
55277c478bd9Sstevel@tonic-gate static int
dgraph_set_milestone(const char * fmri,scf_handle_t * h,boolean_t norepository)55287c478bd9Sstevel@tonic-gate dgraph_set_milestone(const char *fmri, scf_handle_t *h, boolean_t norepository)
55297c478bd9Sstevel@tonic-gate {
55307c478bd9Sstevel@tonic-gate 	const char *cfmri, *fs;
55317c478bd9Sstevel@tonic-gate 	graph_vertex_t *nm, *v;
55327c478bd9Sstevel@tonic-gate 	int ret = 0, r;
55337c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
55347c478bd9Sstevel@tonic-gate 	boolean_t isall, isnone, rebound = B_FALSE;
55357c478bd9Sstevel@tonic-gate 
55367c478bd9Sstevel@tonic-gate 	/* Validate fmri */
55377c478bd9Sstevel@tonic-gate 	isall = (strcmp(fmri, "all") == 0);
55387c478bd9Sstevel@tonic-gate 	isnone = (strcmp(fmri, "none") == 0);
55397c478bd9Sstevel@tonic-gate 
55407c478bd9Sstevel@tonic-gate 	if (!isall && !isnone) {
55417c478bd9Sstevel@tonic-gate 		if (fmri_canonify(fmri, (char **)&cfmri, B_FALSE) == EINVAL)
55427c478bd9Sstevel@tonic-gate 			goto reject;
55437c478bd9Sstevel@tonic-gate 
55447c478bd9Sstevel@tonic-gate 		if (strcmp(cfmri, single_user_fmri) != 0 &&
55457c478bd9Sstevel@tonic-gate 		    strcmp(cfmri, multi_user_fmri) != 0 &&
55467c478bd9Sstevel@tonic-gate 		    strcmp(cfmri, multi_user_svr_fmri) != 0) {
55477c478bd9Sstevel@tonic-gate 			startd_free((void *)cfmri, max_scf_fmri_size);
55487c478bd9Sstevel@tonic-gate reject:
55497c478bd9Sstevel@tonic-gate 			log_framework(LOG_WARNING,
55507c478bd9Sstevel@tonic-gate 			    "Rejecting request for invalid milestone \"%s\".\n",
55517c478bd9Sstevel@tonic-gate 			    fmri);
55527c478bd9Sstevel@tonic-gate 			return (EINVAL);
55537c478bd9Sstevel@tonic-gate 		}
55547c478bd9Sstevel@tonic-gate 	}
55557c478bd9Sstevel@tonic-gate 
55567c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
55577c478bd9Sstevel@tonic-gate 
55587c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
55597c478bd9Sstevel@tonic-gate 
55607c478bd9Sstevel@tonic-gate 	if (milestone == NULL) {
55617c478bd9Sstevel@tonic-gate 		if (isall) {
55627c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
55637c478bd9Sstevel@tonic-gate 			    "Milestone already set to all.\n");
55647c478bd9Sstevel@tonic-gate 			ret = EALREADY;
55657c478bd9Sstevel@tonic-gate 			goto out;
55667c478bd9Sstevel@tonic-gate 		}
55677c478bd9Sstevel@tonic-gate 	} else if (milestone == MILESTONE_NONE) {
55687c478bd9Sstevel@tonic-gate 		if (isnone) {
55697c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
55707c478bd9Sstevel@tonic-gate 			    "Milestone already set to none.\n");
55717c478bd9Sstevel@tonic-gate 			ret = EALREADY;
55727c478bd9Sstevel@tonic-gate 			goto out;
55737c478bd9Sstevel@tonic-gate 		}
55747c478bd9Sstevel@tonic-gate 	} else {
55757c478bd9Sstevel@tonic-gate 		if (!isall && !isnone &&
55767c478bd9Sstevel@tonic-gate 		    strcmp(cfmri, milestone->gv_name) == 0) {
55777c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
55787c478bd9Sstevel@tonic-gate 			    "Milestone already set to %s.\n", cfmri);
55797c478bd9Sstevel@tonic-gate 			ret = EALREADY;
55807c478bd9Sstevel@tonic-gate 			goto out;
55817c478bd9Sstevel@tonic-gate 		}
55827c478bd9Sstevel@tonic-gate 	}
55837c478bd9Sstevel@tonic-gate 
55847c478bd9Sstevel@tonic-gate 	if (!isall && !isnone) {
55857c478bd9Sstevel@tonic-gate 		nm = vertex_get_by_name(cfmri);
55867c478bd9Sstevel@tonic-gate 		if (nm == NULL || !(nm->gv_flags & GV_CONFIGURED)) {
55877c478bd9Sstevel@tonic-gate 			log_framework(LOG_WARNING, "Cannot set milestone to %s "
55887c478bd9Sstevel@tonic-gate 			    "because no such service exists.\n", cfmri);
55897c478bd9Sstevel@tonic-gate 			ret = ENOENT;
55907c478bd9Sstevel@tonic-gate 			goto out;
55917c478bd9Sstevel@tonic-gate 		}
55927c478bd9Sstevel@tonic-gate 	}
55937c478bd9Sstevel@tonic-gate 
55947c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Changing milestone to %s.\n", fmri);
55957c478bd9Sstevel@tonic-gate 
55967c478bd9Sstevel@tonic-gate 	/*
55977c478bd9Sstevel@tonic-gate 	 * Set milestone, removing the old one if this was the last reference.
55987c478bd9Sstevel@tonic-gate 	 */
55993ad28c1eSrm88369 	if (milestone > MILESTONE_NONE)
56003ad28c1eSrm88369 		(void) vertex_unref(milestone);
56017c478bd9Sstevel@tonic-gate 
56027c478bd9Sstevel@tonic-gate 	if (isall)
56037c478bd9Sstevel@tonic-gate 		milestone = NULL;
56047c478bd9Sstevel@tonic-gate 	else if (isnone)
56057c478bd9Sstevel@tonic-gate 		milestone = MILESTONE_NONE;
56063ad28c1eSrm88369 	else {
56077c478bd9Sstevel@tonic-gate 		milestone = nm;
56083ad28c1eSrm88369 		/* milestone should count as a reference */
56093ad28c1eSrm88369 		vertex_ref(milestone);
56103ad28c1eSrm88369 	}
56117c478bd9Sstevel@tonic-gate 
56127c478bd9Sstevel@tonic-gate 	/* Clear all GV_INSUBGRAPH bits. */
56137c478bd9Sstevel@tonic-gate 	for (v = uu_list_first(dgraph); v != NULL; v = uu_list_next(dgraph, v))
56147c478bd9Sstevel@tonic-gate 		v->gv_flags &= ~GV_INSUBGRAPH;
56157c478bd9Sstevel@tonic-gate 
56167c478bd9Sstevel@tonic-gate 	if (!isall && !isnone) {
56177c478bd9Sstevel@tonic-gate 		/* Set GV_INSUBGRAPH for milestone & descendents. */
56187c478bd9Sstevel@tonic-gate 		milestone->gv_flags |= GV_INSUBGRAPH;
56197c478bd9Sstevel@tonic-gate 
56207c478bd9Sstevel@tonic-gate 		r = uu_list_walk(milestone->gv_dependencies,
56217c478bd9Sstevel@tonic-gate 		    (uu_walk_fn_t *)mark_subgraph, NULL, 0);
56227c478bd9Sstevel@tonic-gate 		assert(r == 0);
56237c478bd9Sstevel@tonic-gate 	}
56247c478bd9Sstevel@tonic-gate 
56257c478bd9Sstevel@tonic-gate 	/* Un-override services in the subgraph & override-disable the rest. */
56267c478bd9Sstevel@tonic-gate 	if (norepository)
56277c478bd9Sstevel@tonic-gate 		goto out;
56287c478bd9Sstevel@tonic-gate 
56297c478bd9Sstevel@tonic-gate 	non_subgraph_svcs = 0;
56307c478bd9Sstevel@tonic-gate 	for (v = uu_list_first(dgraph);
56317c478bd9Sstevel@tonic-gate 	    v != NULL;
56327c478bd9Sstevel@tonic-gate 	    v = uu_list_next(dgraph, v)) {
56337c478bd9Sstevel@tonic-gate 		if (v->gv_type != GVT_INST ||
56347c478bd9Sstevel@tonic-gate 		    (v->gv_flags & GV_CONFIGURED) == 0)
56357c478bd9Sstevel@tonic-gate 			continue;
56367c478bd9Sstevel@tonic-gate 
56377c478bd9Sstevel@tonic-gate again:
56387c478bd9Sstevel@tonic-gate 		r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst,
56397c478bd9Sstevel@tonic-gate 		    NULL, NULL, SCF_DECODE_FMRI_EXACT);
56407c478bd9Sstevel@tonic-gate 		if (r != 0) {
56417c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
56427c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
56437c478bd9Sstevel@tonic-gate 			default:
56447c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
56457c478bd9Sstevel@tonic-gate 				rebound = B_TRUE;
56467c478bd9Sstevel@tonic-gate 				goto again;
56477c478bd9Sstevel@tonic-gate 
56487c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
56497c478bd9Sstevel@tonic-gate 				continue;
56507c478bd9Sstevel@tonic-gate 
56517c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
56527c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
56537c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
56547c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
56557c478bd9Sstevel@tonic-gate 				bad_error("scf_handle_decode_fmri",
56567c478bd9Sstevel@tonic-gate 				    scf_error());
56577c478bd9Sstevel@tonic-gate 			}
56587c478bd9Sstevel@tonic-gate 		}
56597c478bd9Sstevel@tonic-gate 
56607c478bd9Sstevel@tonic-gate 		if (isall || (v->gv_flags & GV_INSUBGRAPH)) {
56617c478bd9Sstevel@tonic-gate 			r = libscf_delete_enable_ovr(inst);
56627c478bd9Sstevel@tonic-gate 			fs = "libscf_delete_enable_ovr";
56637c478bd9Sstevel@tonic-gate 		} else {
56647c478bd9Sstevel@tonic-gate 			assert(isnone || (v->gv_flags & GV_INSUBGRAPH) == 0);
56657c478bd9Sstevel@tonic-gate 
566656e23938Sbustos 			/*
566756e23938Sbustos 			 * Services which are up need to come down before
566856e23938Sbustos 			 * we're done, but we can only disable the leaves
566956e23938Sbustos 			 * here.
567056e23938Sbustos 			 */
567156e23938Sbustos 
567256e23938Sbustos 			if (up_state(v->gv_state))
56737c478bd9Sstevel@tonic-gate 				++non_subgraph_svcs;
56747c478bd9Sstevel@tonic-gate 
567556e23938Sbustos 			/* If it's already disabled, don't bother. */
567656e23938Sbustos 			if ((v->gv_flags & GV_ENABLED) == 0)
567756e23938Sbustos 				continue;
567856e23938Sbustos 
567956e23938Sbustos 			if (!is_nonsubgraph_leaf(v))
56807c478bd9Sstevel@tonic-gate 				continue;
56817c478bd9Sstevel@tonic-gate 
56827c478bd9Sstevel@tonic-gate 			r = libscf_set_enable_ovr(inst, 0);
56837c478bd9Sstevel@tonic-gate 			fs = "libscf_set_enable_ovr";
56847c478bd9Sstevel@tonic-gate 		}
56857c478bd9Sstevel@tonic-gate 		switch (r) {
56867c478bd9Sstevel@tonic-gate 		case 0:
56877c478bd9Sstevel@tonic-gate 		case ECANCELED:
56887c478bd9Sstevel@tonic-gate 			break;
56897c478bd9Sstevel@tonic-gate 
56907c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
56917c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
56927c478bd9Sstevel@tonic-gate 			rebound = B_TRUE;
56937c478bd9Sstevel@tonic-gate 			goto again;
56947c478bd9Sstevel@tonic-gate 
56957c478bd9Sstevel@tonic-gate 		case EPERM:
56967c478bd9Sstevel@tonic-gate 		case EROFS:
56977c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
56987c478bd9Sstevel@tonic-gate 			    "Could not set %s/%s for %s: %s.\n",
56997c478bd9Sstevel@tonic-gate 			    SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
57007c478bd9Sstevel@tonic-gate 			    v->gv_name, strerror(r));
57017c478bd9Sstevel@tonic-gate 			break;
57027c478bd9Sstevel@tonic-gate 
57037c478bd9Sstevel@tonic-gate 		default:
57047c478bd9Sstevel@tonic-gate 			bad_error(fs, r);
57057c478bd9Sstevel@tonic-gate 		}
57067c478bd9Sstevel@tonic-gate 	}
57077c478bd9Sstevel@tonic-gate 
57087c478bd9Sstevel@tonic-gate 	if (halting != -1) {
57097c478bd9Sstevel@tonic-gate 		if (non_subgraph_svcs > 1)
57107c478bd9Sstevel@tonic-gate 			uu_warn("%d system services are now being stopped.\n",
57117c478bd9Sstevel@tonic-gate 			    non_subgraph_svcs);
57127c478bd9Sstevel@tonic-gate 		else if (non_subgraph_svcs == 1)
57137c478bd9Sstevel@tonic-gate 			uu_warn("One system service is now being stopped.\n");
57147c478bd9Sstevel@tonic-gate 		else if (non_subgraph_svcs == 0)
57157c478bd9Sstevel@tonic-gate 			do_uadmin();
57167c478bd9Sstevel@tonic-gate 	}
57177c478bd9Sstevel@tonic-gate 
57187c478bd9Sstevel@tonic-gate 	ret = rebound ? ECONNRESET : 0;
57197c478bd9Sstevel@tonic-gate 
57207c478bd9Sstevel@tonic-gate out:
57217c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
57227c478bd9Sstevel@tonic-gate 	if (!isall && !isnone)
57237c478bd9Sstevel@tonic-gate 		startd_free((void *)cfmri, max_scf_fmri_size);
57247c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
57257c478bd9Sstevel@tonic-gate 	return (ret);
57267c478bd9Sstevel@tonic-gate }
57277c478bd9Sstevel@tonic-gate 
57287c478bd9Sstevel@tonic-gate 
57297c478bd9Sstevel@tonic-gate /*
57307c478bd9Sstevel@tonic-gate  * Returns 0, ECONNABORTED, or EINVAL.
57317c478bd9Sstevel@tonic-gate  */
57327c478bd9Sstevel@tonic-gate static int
handle_graph_update_event(scf_handle_t * h,graph_protocol_event_t * e)57337c478bd9Sstevel@tonic-gate handle_graph_update_event(scf_handle_t *h, graph_protocol_event_t *e)
57347c478bd9Sstevel@tonic-gate {
57357c478bd9Sstevel@tonic-gate 	int r;
57367c478bd9Sstevel@tonic-gate 
57377c478bd9Sstevel@tonic-gate 	switch (e->gpe_type) {
57387c478bd9Sstevel@tonic-gate 	case GRAPH_UPDATE_RELOAD_GRAPH:
57397c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
57407c478bd9Sstevel@tonic-gate 		    "graph_event: reload graph unimplemented\n");
57417c478bd9Sstevel@tonic-gate 		break;
57427c478bd9Sstevel@tonic-gate 
57437c478bd9Sstevel@tonic-gate 	case GRAPH_UPDATE_STATE_CHANGE: {
57447c478bd9Sstevel@tonic-gate 		protocol_states_t *states = e->gpe_data;
57457c478bd9Sstevel@tonic-gate 
5746f6e214c7SGavin Maltby 		switch (r = dgraph_set_instance_state(h, e->gpe_inst, states)) {
57477c478bd9Sstevel@tonic-gate 		case 0:
57487c478bd9Sstevel@tonic-gate 		case ENOENT:
57497c478bd9Sstevel@tonic-gate 			break;
57507c478bd9Sstevel@tonic-gate 
57517c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
57527c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
57537c478bd9Sstevel@tonic-gate 
57547c478bd9Sstevel@tonic-gate 		case EINVAL:
57557c478bd9Sstevel@tonic-gate 		default:
57567c478bd9Sstevel@tonic-gate #ifndef NDEBUG
57577c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "dgraph_set_instance_state() "
57587c478bd9Sstevel@tonic-gate 			    "failed with unexpected error %d at %s:%d.\n", r,
57597c478bd9Sstevel@tonic-gate 			    __FILE__, __LINE__);
57607c478bd9Sstevel@tonic-gate #endif
57617c478bd9Sstevel@tonic-gate 			abort();
57627c478bd9Sstevel@tonic-gate 		}
57637c478bd9Sstevel@tonic-gate 
57647c478bd9Sstevel@tonic-gate 		startd_free(states, sizeof (protocol_states_t));
57657c478bd9Sstevel@tonic-gate 		break;
57667c478bd9Sstevel@tonic-gate 	}
57677c478bd9Sstevel@tonic-gate 
57687c478bd9Sstevel@tonic-gate 	default:
57697c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
57707c478bd9Sstevel@tonic-gate 		    "graph_event_loop received an unknown event: %d\n",
57717c478bd9Sstevel@tonic-gate 		    e->gpe_type);
57727c478bd9Sstevel@tonic-gate 		break;
57737c478bd9Sstevel@tonic-gate 	}
57747c478bd9Sstevel@tonic-gate 
57757c478bd9Sstevel@tonic-gate 	return (0);
57767c478bd9Sstevel@tonic-gate }
57777c478bd9Sstevel@tonic-gate 
57787c478bd9Sstevel@tonic-gate /*
57797c478bd9Sstevel@tonic-gate  * graph_event_thread()
57807c478bd9Sstevel@tonic-gate  *    Wait for state changes from the restarters.
57817c478bd9Sstevel@tonic-gate  */
57827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
57837c478bd9Sstevel@tonic-gate void *
graph_event_thread(void * unused)57847c478bd9Sstevel@tonic-gate graph_event_thread(void *unused)
57857c478bd9Sstevel@tonic-gate {
57867c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
57877c478bd9Sstevel@tonic-gate 	int err;
57887c478bd9Sstevel@tonic-gate 
5789ab618543SJohn Levon 	(void) pthread_setname_np(pthread_self(), "graph_event");
5790ab618543SJohn Levon 
57917c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
57927c478bd9Sstevel@tonic-gate 
57937c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
57947c478bd9Sstevel@tonic-gate 	while (1) {
57957c478bd9Sstevel@tonic-gate 		graph_protocol_event_t *e;
57967c478bd9Sstevel@tonic-gate 
57977c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&gu->gu_lock);
57987c478bd9Sstevel@tonic-gate 
57997c478bd9Sstevel@tonic-gate 		while (gu->gu_wakeup == 0)
58007c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&gu->gu_cv, &gu->gu_lock);
58017c478bd9Sstevel@tonic-gate 
58027c478bd9Sstevel@tonic-gate 		gu->gu_wakeup = 0;
58037c478bd9Sstevel@tonic-gate 
58047c478bd9Sstevel@tonic-gate 		while ((e = graph_event_dequeue()) != NULL) {
58057c478bd9Sstevel@tonic-gate 			MUTEX_LOCK(&e->gpe_lock);
58067c478bd9Sstevel@tonic-gate 			MUTEX_UNLOCK(&gu->gu_lock);
58077c478bd9Sstevel@tonic-gate 
58087c478bd9Sstevel@tonic-gate 			while ((err = handle_graph_update_event(h, e)) ==
58097c478bd9Sstevel@tonic-gate 			    ECONNABORTED)
58107c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
58117c478bd9Sstevel@tonic-gate 
58127c478bd9Sstevel@tonic-gate 			if (err == 0)
58137c478bd9Sstevel@tonic-gate 				graph_event_release(e);
58147c478bd9Sstevel@tonic-gate 			else
58157c478bd9Sstevel@tonic-gate 				graph_event_requeue(e);
58167c478bd9Sstevel@tonic-gate 
58177c478bd9Sstevel@tonic-gate 			MUTEX_LOCK(&gu->gu_lock);
58187c478bd9Sstevel@tonic-gate 		}
58197c478bd9Sstevel@tonic-gate 
58207c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&gu->gu_lock);
58217c478bd9Sstevel@tonic-gate 	}
58227c478bd9Sstevel@tonic-gate }
58237c478bd9Sstevel@tonic-gate 
58247c478bd9Sstevel@tonic-gate static void
set_initial_milestone(scf_handle_t * h)58257c478bd9Sstevel@tonic-gate set_initial_milestone(scf_handle_t *h)
58267c478bd9Sstevel@tonic-gate {
58277c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
58287c478bd9Sstevel@tonic-gate 	char *fmri, *cfmri;
58297c478bd9Sstevel@tonic-gate 	size_t sz;
58307c478bd9Sstevel@tonic-gate 	int r;
58317c478bd9Sstevel@tonic-gate 
58327c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
58337c478bd9Sstevel@tonic-gate 	fmri = startd_alloc(max_scf_fmri_size);
58347c478bd9Sstevel@tonic-gate 
58357c478bd9Sstevel@tonic-gate 	/*
58367c478bd9Sstevel@tonic-gate 	 * If -m milestone= was specified, we want to set options_ovr/milestone
58377c478bd9Sstevel@tonic-gate 	 * to it.  Otherwise we want to read what the milestone should be set
58387c478bd9Sstevel@tonic-gate 	 * to.  Either way we need our inst.
58397c478bd9Sstevel@tonic-gate 	 */
58407c478bd9Sstevel@tonic-gate get_self:
58417c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
58427c478bd9Sstevel@tonic-gate 	    NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
58437c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
58447c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
58457c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
58467c478bd9Sstevel@tonic-gate 			goto get_self;
58477c478bd9Sstevel@tonic-gate 
58487c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
58497c478bd9Sstevel@tonic-gate 			if (st->st_subgraph != NULL &&
58507c478bd9Sstevel@tonic-gate 			    st->st_subgraph[0] != '\0') {
58517c478bd9Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
58527c478bd9Sstevel@tonic-gate 				    max_scf_fmri_size);
58537c478bd9Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
58547c478bd9Sstevel@tonic-gate 			} else {
58557c478bd9Sstevel@tonic-gate 				fmri[0] = '\0';
58567c478bd9Sstevel@tonic-gate 			}
58577c478bd9Sstevel@tonic-gate 			break;
58587c478bd9Sstevel@tonic-gate 
58597c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
58607c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
58617c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
58627c478bd9Sstevel@tonic-gate 		default:
58637c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
58647c478bd9Sstevel@tonic-gate 		}
58657c478bd9Sstevel@tonic-gate 	} else {
58667c478bd9Sstevel@tonic-gate 		if (st->st_subgraph != NULL && st->st_subgraph[0] != '\0') {
58677c478bd9Sstevel@tonic-gate 			scf_propertygroup_t *pg;
58687c478bd9Sstevel@tonic-gate 
58697c478bd9Sstevel@tonic-gate 			pg = safe_scf_pg_create(h);
58707c478bd9Sstevel@tonic-gate 
58717c478bd9Sstevel@tonic-gate 			sz = strlcpy(fmri, st->st_subgraph, max_scf_fmri_size);
58727c478bd9Sstevel@tonic-gate 			assert(sz < max_scf_fmri_size);
58737c478bd9Sstevel@tonic-gate 
58747c478bd9Sstevel@tonic-gate 			r = libscf_inst_get_or_add_pg(inst, SCF_PG_OPTIONS_OVR,
58757c478bd9Sstevel@tonic-gate 			    SCF_PG_OPTIONS_OVR_TYPE, SCF_PG_OPTIONS_OVR_FLAGS,
58767c478bd9Sstevel@tonic-gate 			    pg);
58777c478bd9Sstevel@tonic-gate 			switch (r) {
58787c478bd9Sstevel@tonic-gate 			case 0:
58797c478bd9Sstevel@tonic-gate 				break;
58807c478bd9Sstevel@tonic-gate 
58817c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
58827c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
58837c478bd9Sstevel@tonic-gate 				goto get_self;
58847c478bd9Sstevel@tonic-gate 
58857c478bd9Sstevel@tonic-gate 			case EPERM:
58867c478bd9Sstevel@tonic-gate 			case EACCES:
58877c478bd9Sstevel@tonic-gate 			case EROFS:
58887c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Could not set %s/%s: "
58897c478bd9Sstevel@tonic-gate 				    "%s.\n", SCF_PG_OPTIONS_OVR,
58907c478bd9Sstevel@tonic-gate 				    SCF_PROPERTY_MILESTONE, strerror(r));
58917c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
58927c478bd9Sstevel@tonic-gate 
58937c478bd9Sstevel@tonic-gate 			case ECANCELED:
58947c478bd9Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
58957c478bd9Sstevel@tonic-gate 				    max_scf_fmri_size);
58967c478bd9Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
58977c478bd9Sstevel@tonic-gate 				break;
58987c478bd9Sstevel@tonic-gate 
58997c478bd9Sstevel@tonic-gate 			default:
59007c478bd9Sstevel@tonic-gate 				bad_error("libscf_inst_get_or_add_pg", r);
59017c478bd9Sstevel@tonic-gate 			}
59027c478bd9Sstevel@tonic-gate 
59037c478bd9Sstevel@tonic-gate 			r = libscf_clear_runlevel(pg, fmri);
59047c478bd9Sstevel@tonic-gate 			switch (r) {
59057c478bd9Sstevel@tonic-gate 			case 0:
59067c478bd9Sstevel@tonic-gate 				break;
59077c478bd9Sstevel@tonic-gate 
59087c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
59097c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
59107c478bd9Sstevel@tonic-gate 				goto get_self;
59117c478bd9Sstevel@tonic-gate 
59127c478bd9Sstevel@tonic-gate 			case EPERM:
59137c478bd9Sstevel@tonic-gate 			case EACCES:
59147c478bd9Sstevel@tonic-gate 			case EROFS:
59157c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Could not set %s/%s: "
59167c478bd9Sstevel@tonic-gate 				    "%s.\n", SCF_PG_OPTIONS_OVR,
59177c478bd9Sstevel@tonic-gate 				    SCF_PROPERTY_MILESTONE, strerror(r));
59187c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
59197c478bd9Sstevel@tonic-gate 
59207c478bd9Sstevel@tonic-gate 			case ECANCELED:
59217c478bd9Sstevel@tonic-gate 				sz = strlcpy(fmri, st->st_subgraph,
59227c478bd9Sstevel@tonic-gate 				    max_scf_fmri_size);
59237c478bd9Sstevel@tonic-gate 				assert(sz < max_scf_fmri_size);
59247c478bd9Sstevel@tonic-gate 				break;
59257c478bd9Sstevel@tonic-gate 
59267c478bd9Sstevel@tonic-gate 			default:
59277c478bd9Sstevel@tonic-gate 				bad_error("libscf_clear_runlevel", r);
59287c478bd9Sstevel@tonic-gate 			}
59297c478bd9Sstevel@tonic-gate 
59307c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
59317c478bd9Sstevel@tonic-gate 		} else {
59327c478bd9Sstevel@tonic-gate 			scf_property_t *prop;
59337c478bd9Sstevel@tonic-gate 			scf_value_t *val;
59347c478bd9Sstevel@tonic-gate 
59357c478bd9Sstevel@tonic-gate 			prop = safe_scf_property_create(h);
59367c478bd9Sstevel@tonic-gate 			val = safe_scf_value_create(h);
59377c478bd9Sstevel@tonic-gate 
59387c478bd9Sstevel@tonic-gate 			r = libscf_get_milestone(inst, prop, val, fmri,
59397c478bd9Sstevel@tonic-gate 			    max_scf_fmri_size);
59407c478bd9Sstevel@tonic-gate 			switch (r) {
59417c478bd9Sstevel@tonic-gate 			case 0:
59427c478bd9Sstevel@tonic-gate 				break;
59437c478bd9Sstevel@tonic-gate 
59447c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
59457c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
59467c478bd9Sstevel@tonic-gate 				goto get_self;
59477c478bd9Sstevel@tonic-gate 
59487c478bd9Sstevel@tonic-gate 			case EINVAL:
59497c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Milestone property is "
59507c478bd9Sstevel@tonic-gate 				    "misconfigured.  Defaulting to \"all\".\n");
59517c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
59527c478bd9Sstevel@tonic-gate 
59537c478bd9Sstevel@tonic-gate 			case ECANCELED:
59547c478bd9Sstevel@tonic-gate 			case ENOENT:
59557c478bd9Sstevel@tonic-gate 				fmri[0] = '\0';
59567c478bd9Sstevel@tonic-gate 				break;
59577c478bd9Sstevel@tonic-gate 
59587c478bd9Sstevel@tonic-gate 			default:
59597c478bd9Sstevel@tonic-gate 				bad_error("libscf_get_milestone", r);
59607c478bd9Sstevel@tonic-gate 			}
59617c478bd9Sstevel@tonic-gate 
59627c478bd9Sstevel@tonic-gate 			scf_value_destroy(val);
59637c478bd9Sstevel@tonic-gate 			scf_property_destroy(prop);
59647c478bd9Sstevel@tonic-gate 		}
59657c478bd9Sstevel@tonic-gate 	}
59667c478bd9Sstevel@tonic-gate 
59677c478bd9Sstevel@tonic-gate 	if (fmri[0] == '\0' || strcmp(fmri, "all") == 0)
59687c478bd9Sstevel@tonic-gate 		goto out;
59697c478bd9Sstevel@tonic-gate 
59707c478bd9Sstevel@tonic-gate 	if (strcmp(fmri, "none") != 0) {
59717c478bd9Sstevel@tonic-gate retry:
59727c478bd9Sstevel@tonic-gate 		if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
59737c478bd9Sstevel@tonic-gate 		    NULL, SCF_DECODE_FMRI_EXACT) != 0) {
59747c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
59757c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
59767c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING,
59777c478bd9Sstevel@tonic-gate 				    "Requested milestone \"%s\" is invalid.  "
59787c478bd9Sstevel@tonic-gate 				    "Reverting to \"all\".\n", fmri);
59797c478bd9Sstevel@tonic-gate 				goto out;
59807c478bd9Sstevel@tonic-gate 
59817c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
59827c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Requested milestone "
59837c478bd9Sstevel@tonic-gate 				    "\"%s\" does not specify an instance.  "
59847c478bd9Sstevel@tonic-gate 				    "Reverting to \"all\".\n", fmri);
59857c478bd9Sstevel@tonic-gate 				goto out;
59867c478bd9Sstevel@tonic-gate 
59877c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
59887c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
59897c478bd9Sstevel@tonic-gate 				goto retry;
59907c478bd9Sstevel@tonic-gate 
59917c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
59927c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Requested milestone "
59937c478bd9Sstevel@tonic-gate 				    "\"%s\" not in repository.  Reverting to "
59947c478bd9Sstevel@tonic-gate 				    "\"all\".\n", fmri);
59957c478bd9Sstevel@tonic-gate 				goto out;
59967c478bd9Sstevel@tonic-gate 
59977c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
59987c478bd9Sstevel@tonic-gate 			default:
59997c478bd9Sstevel@tonic-gate 				bad_error("scf_handle_decode_fmri",
60007c478bd9Sstevel@tonic-gate 				    scf_error());
60017c478bd9Sstevel@tonic-gate 			}
60027c478bd9Sstevel@tonic-gate 		}
60037c478bd9Sstevel@tonic-gate 
60047c478bd9Sstevel@tonic-gate 		r = fmri_canonify(fmri, &cfmri, B_FALSE);
60057c478bd9Sstevel@tonic-gate 		assert(r == 0);
60067c478bd9Sstevel@tonic-gate 
60077c478bd9Sstevel@tonic-gate 		r = dgraph_add_instance(cfmri, inst, B_TRUE);
60087c478bd9Sstevel@tonic-gate 		startd_free(cfmri, max_scf_fmri_size);
60097c478bd9Sstevel@tonic-gate 		switch (r) {
60107c478bd9Sstevel@tonic-gate 		case 0:
60117c478bd9Sstevel@tonic-gate 			break;
60127c478bd9Sstevel@tonic-gate 
60137c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
60147c478bd9Sstevel@tonic-gate 			goto retry;
60157c478bd9Sstevel@tonic-gate 
60167c478bd9Sstevel@tonic-gate 		case EINVAL:
60177c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
60187c478bd9Sstevel@tonic-gate 			    "Requested milestone \"%s\" is invalid.  "
60197c478bd9Sstevel@tonic-gate 			    "Reverting to \"all\".\n", fmri);
60207c478bd9Sstevel@tonic-gate 			goto out;
60217c478bd9Sstevel@tonic-gate 
60227c478bd9Sstevel@tonic-gate 		case ECANCELED:
60237c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
60247c478bd9Sstevel@tonic-gate 			    "Requested milestone \"%s\" not "
60257c478bd9Sstevel@tonic-gate 			    "in repository.  Reverting to \"all\".\n",
60267c478bd9Sstevel@tonic-gate 			    fmri);
60277c478bd9Sstevel@tonic-gate 			goto out;
60287c478bd9Sstevel@tonic-gate 
60297c478bd9Sstevel@tonic-gate 		case EEXIST:
60307c478bd9Sstevel@tonic-gate 		default:
60317c478bd9Sstevel@tonic-gate 			bad_error("dgraph_add_instance", r);
60327c478bd9Sstevel@tonic-gate 		}
60337c478bd9Sstevel@tonic-gate 	}
60347c478bd9Sstevel@tonic-gate 
60357c478bd9Sstevel@tonic-gate 	log_console(LOG_INFO, "Booting to milestone \"%s\".\n", fmri);
60367c478bd9Sstevel@tonic-gate 
60377c478bd9Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_FALSE);
60387c478bd9Sstevel@tonic-gate 	switch (r) {
60397c478bd9Sstevel@tonic-gate 	case 0:
60407c478bd9Sstevel@tonic-gate 	case ECONNRESET:
60417c478bd9Sstevel@tonic-gate 	case EALREADY:
60427c478bd9Sstevel@tonic-gate 		break;
60437c478bd9Sstevel@tonic-gate 
60447c478bd9Sstevel@tonic-gate 	case EINVAL:
60457c478bd9Sstevel@tonic-gate 	case ENOENT:
60467c478bd9Sstevel@tonic-gate 	default:
60477c478bd9Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
60487c478bd9Sstevel@tonic-gate 	}
60497c478bd9Sstevel@tonic-gate 
60507c478bd9Sstevel@tonic-gate out:
60517c478bd9Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
60527c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
60537c478bd9Sstevel@tonic-gate }
60547c478bd9Sstevel@tonic-gate 
60557c478bd9Sstevel@tonic-gate void
set_restart_milestone(scf_handle_t * h)60567c478bd9Sstevel@tonic-gate set_restart_milestone(scf_handle_t *h)
60577c478bd9Sstevel@tonic-gate {
60587c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
60597c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
60607c478bd9Sstevel@tonic-gate 	scf_value_t *val;
60617c478bd9Sstevel@tonic-gate 	char *fmri;
60627c478bd9Sstevel@tonic-gate 	int r;
60637c478bd9Sstevel@tonic-gate 
60647c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
60657c478bd9Sstevel@tonic-gate 
60667c478bd9Sstevel@tonic-gate get_self:
60677c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL,
60687c478bd9Sstevel@tonic-gate 	    inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
60697c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
60707c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
60717c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
60727c478bd9Sstevel@tonic-gate 			goto get_self;
60737c478bd9Sstevel@tonic-gate 
60747c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
60757c478bd9Sstevel@tonic-gate 			break;
60767c478bd9Sstevel@tonic-gate 
60777c478bd9Sstevel@tonic-gate 		case SCF_ERROR_INVALID_ARGUMENT:
60787c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
60797c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
60807c478bd9Sstevel@tonic-gate 		default:
60817c478bd9Sstevel@tonic-gate 			bad_error("scf_handle_decode_fmri", scf_error());
60827c478bd9Sstevel@tonic-gate 		}
60837c478bd9Sstevel@tonic-gate 
60847c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
60857c478bd9Sstevel@tonic-gate 		return;
60867c478bd9Sstevel@tonic-gate 	}
60877c478bd9Sstevel@tonic-gate 
60887c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
60897c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
60907c478bd9Sstevel@tonic-gate 	fmri = startd_alloc(max_scf_fmri_size);
60917c478bd9Sstevel@tonic-gate 
60927c478bd9Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size);
60937c478bd9Sstevel@tonic-gate 	switch (r) {
60947c478bd9Sstevel@tonic-gate 	case 0:
60957c478bd9Sstevel@tonic-gate 		break;
60967c478bd9Sstevel@tonic-gate 
60977c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
60987c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
60997c478bd9Sstevel@tonic-gate 		goto get_self;
61007c478bd9Sstevel@tonic-gate 
61017c478bd9Sstevel@tonic-gate 	case ECANCELED:
61027c478bd9Sstevel@tonic-gate 	case ENOENT:
61037c478bd9Sstevel@tonic-gate 	case EINVAL:
61047c478bd9Sstevel@tonic-gate 		goto out;
61057c478bd9Sstevel@tonic-gate 
61067c478bd9Sstevel@tonic-gate 	default:
61077c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
61087c478bd9Sstevel@tonic-gate 	}
61097c478bd9Sstevel@tonic-gate 
61107c478bd9Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_TRUE);
61117c478bd9Sstevel@tonic-gate 	switch (r) {
61127c478bd9Sstevel@tonic-gate 	case 0:
61137c478bd9Sstevel@tonic-gate 	case ECONNRESET:
61147c478bd9Sstevel@tonic-gate 	case EALREADY:
61157c478bd9Sstevel@tonic-gate 	case EINVAL:
61167c478bd9Sstevel@tonic-gate 	case ENOENT:
61177c478bd9Sstevel@tonic-gate 		break;
61187c478bd9Sstevel@tonic-gate 
61197c478bd9Sstevel@tonic-gate 	default:
61207c478bd9Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
61217c478bd9Sstevel@tonic-gate 	}
61227c478bd9Sstevel@tonic-gate 
61237c478bd9Sstevel@tonic-gate out:
61247c478bd9Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
61257c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
61267c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
61277c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
61287c478bd9Sstevel@tonic-gate }
61297c478bd9Sstevel@tonic-gate 
61307c478bd9Sstevel@tonic-gate /*
61317c478bd9Sstevel@tonic-gate  * void *graph_thread(void *)
61327c478bd9Sstevel@tonic-gate  *
61337c478bd9Sstevel@tonic-gate  * Graph management thread.
61347c478bd9Sstevel@tonic-gate  */
61357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
61367c478bd9Sstevel@tonic-gate void *
graph_thread(void * arg)61377c478bd9Sstevel@tonic-gate graph_thread(void *arg)
61387c478bd9Sstevel@tonic-gate {
61397c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
61407c478bd9Sstevel@tonic-gate 	int err;
61417c478bd9Sstevel@tonic-gate 
6142ab618543SJohn Levon 	(void) pthread_setname_np(pthread_self(), "graph");
6143ab618543SJohn Levon 
61447c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
61457c478bd9Sstevel@tonic-gate 
61467c478bd9Sstevel@tonic-gate 	if (st->st_initial)
61477c478bd9Sstevel@tonic-gate 		set_initial_milestone(h);
61487c478bd9Sstevel@tonic-gate 
61497c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
61507c478bd9Sstevel@tonic-gate 	initial_milestone_set = B_TRUE;
61517c478bd9Sstevel@tonic-gate 	err = pthread_cond_broadcast(&initial_milestone_cv);
61527c478bd9Sstevel@tonic-gate 	assert(err == 0);
61537c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
61547c478bd9Sstevel@tonic-gate 
61557c478bd9Sstevel@tonic-gate 	libscf_populate_graph(h);
61567c478bd9Sstevel@tonic-gate 
61577c478bd9Sstevel@tonic-gate 	if (!st->st_initial)
61587c478bd9Sstevel@tonic-gate 		set_restart_milestone(h);
61597c478bd9Sstevel@tonic-gate 
61607c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&st->st_load_lock);
61617c478bd9Sstevel@tonic-gate 	st->st_load_complete = 1;
61627c478bd9Sstevel@tonic-gate 	(void) pthread_cond_broadcast(&st->st_load_cv);
61637c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&st->st_load_lock);
61647c478bd9Sstevel@tonic-gate 
61657c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
61667c478bd9Sstevel@tonic-gate 	/*
61677c478bd9Sstevel@tonic-gate 	 * Now that we've set st_load_complete we need to check can_come_up()
61687c478bd9Sstevel@tonic-gate 	 * since if we booted to a milestone, then there won't be any more
61697c478bd9Sstevel@tonic-gate 	 * state updates.
61707c478bd9Sstevel@tonic-gate 	 */
61717c478bd9Sstevel@tonic-gate 	if (!go_single_user_mode && !go_to_level1 &&
61727c478bd9Sstevel@tonic-gate 	    halting == -1) {
617373b709eaSrm88369 		if (!sulogin_thread_running && !can_come_up()) {
61747c478bd9Sstevel@tonic-gate 			(void) startd_thread_create(sulogin_thread, NULL);
61757c478bd9Sstevel@tonic-gate 			sulogin_thread_running = B_TRUE;
61767c478bd9Sstevel@tonic-gate 		}
61777c478bd9Sstevel@tonic-gate 	}
61787c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
61797c478bd9Sstevel@tonic-gate 
61807c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&gu->gu_freeze_lock);
61817c478bd9Sstevel@tonic-gate 
61827c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
61837c478bd9Sstevel@tonic-gate 	while (1) {
61847c478bd9Sstevel@tonic-gate 		(void) pthread_cond_wait(&gu->gu_freeze_cv,
61857c478bd9Sstevel@tonic-gate 		    &gu->gu_freeze_lock);
61867c478bd9Sstevel@tonic-gate 	}
61877c478bd9Sstevel@tonic-gate }
61887c478bd9Sstevel@tonic-gate 
61897c478bd9Sstevel@tonic-gate 
61907c478bd9Sstevel@tonic-gate /*
61917c478bd9Sstevel@tonic-gate  * int next_action()
61927c478bd9Sstevel@tonic-gate  *   Given an array of timestamps 'a' with 'num' elements, find the
61937c478bd9Sstevel@tonic-gate  *   lowest non-zero timestamp and return its index. If there are no
61947c478bd9Sstevel@tonic-gate  *   non-zero elements, return -1.
61957c478bd9Sstevel@tonic-gate  */
61967c478bd9Sstevel@tonic-gate static int
next_action(hrtime_t * a,int num)61977c478bd9Sstevel@tonic-gate next_action(hrtime_t *a, int num)
61987c478bd9Sstevel@tonic-gate {
61997c478bd9Sstevel@tonic-gate 	hrtime_t t = 0;
62007c478bd9Sstevel@tonic-gate 	int i = 0, smallest = -1;
62017c478bd9Sstevel@tonic-gate 
62027c478bd9Sstevel@tonic-gate 	for (i = 0; i < num; i++) {
62037c478bd9Sstevel@tonic-gate 		if (t == 0) {
62047c478bd9Sstevel@tonic-gate 			t = a[i];
62057c478bd9Sstevel@tonic-gate 			smallest = i;
62067c478bd9Sstevel@tonic-gate 		} else if (a[i] != 0 && a[i] < t) {
62077c478bd9Sstevel@tonic-gate 			t = a[i];
62087c478bd9Sstevel@tonic-gate 			smallest = i;
62097c478bd9Sstevel@tonic-gate 		}
62107c478bd9Sstevel@tonic-gate 	}
62117c478bd9Sstevel@tonic-gate 
62127c478bd9Sstevel@tonic-gate 	if (t == 0)
62137c478bd9Sstevel@tonic-gate 		return (-1);
62147c478bd9Sstevel@tonic-gate 	else
62157c478bd9Sstevel@tonic-gate 		return (smallest);
62167c478bd9Sstevel@tonic-gate }
62177c478bd9Sstevel@tonic-gate 
62187c478bd9Sstevel@tonic-gate /*
62197c478bd9Sstevel@tonic-gate  * void process_actions()
62207c478bd9Sstevel@tonic-gate  *   Process actions requested by the administrator. Possibilities include:
62217c478bd9Sstevel@tonic-gate  *   refresh, restart, maintenance mode off, maintenance mode on,
62227c478bd9Sstevel@tonic-gate  *   maintenance mode immediate, and degraded.
62237c478bd9Sstevel@tonic-gate  *
62247c478bd9Sstevel@tonic-gate  *   The set of pending actions is represented in the repository as a
62257c478bd9Sstevel@tonic-gate  *   per-instance property group, with each action being a single property
62267c478bd9Sstevel@tonic-gate  *   in that group.  This property group is converted to an array, with each
62277c478bd9Sstevel@tonic-gate  *   action type having an array slot.  The actions in the array at the
62287c478bd9Sstevel@tonic-gate  *   time process_actions() is called are acted on in the order of the
62297c478bd9Sstevel@tonic-gate  *   timestamp (which is the value stored in the slot).  A value of zero
62307c478bd9Sstevel@tonic-gate  *   indicates that there is no pending action of the type associated with
62317c478bd9Sstevel@tonic-gate  *   a particular slot.
62327c478bd9Sstevel@tonic-gate  *
62337c478bd9Sstevel@tonic-gate  *   Sending an action event multiple times before the restarter has a
62347c478bd9Sstevel@tonic-gate  *   chance to process that action will force it to be run at the last
62357c478bd9Sstevel@tonic-gate  *   timestamp where it appears in the ordering.
62367c478bd9Sstevel@tonic-gate  *
62377c478bd9Sstevel@tonic-gate  *   Turning maintenance mode on trumps all other actions.
62387c478bd9Sstevel@tonic-gate  *
62397c478bd9Sstevel@tonic-gate  *   Returns 0 or ECONNABORTED.
62407c478bd9Sstevel@tonic-gate  */
62417c478bd9Sstevel@tonic-gate static int
process_actions(scf_handle_t * h,scf_propertygroup_t * pg,scf_instance_t * inst)62427c478bd9Sstevel@tonic-gate process_actions(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst)
62437c478bd9Sstevel@tonic-gate {
62447c478bd9Sstevel@tonic-gate 	scf_property_t *prop = NULL;
62457c478bd9Sstevel@tonic-gate 	scf_value_t *val = NULL;
62467c478bd9Sstevel@tonic-gate 	scf_type_t type;
62477c478bd9Sstevel@tonic-gate 	graph_vertex_t *vertex;
62487c478bd9Sstevel@tonic-gate 	admin_action_t a;
62497c478bd9Sstevel@tonic-gate 	int i, ret = 0, r;
62507c478bd9Sstevel@tonic-gate 	hrtime_t action_ts[NACTIONS];
62517c478bd9Sstevel@tonic-gate 	char *inst_name;
62527c478bd9Sstevel@tonic-gate 
62537c478bd9Sstevel@tonic-gate 	r = libscf_instance_get_fmri(inst, &inst_name);
62547c478bd9Sstevel@tonic-gate 	switch (r) {
62557c478bd9Sstevel@tonic-gate 	case 0:
62567c478bd9Sstevel@tonic-gate 		break;
62577c478bd9Sstevel@tonic-gate 
62587c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
62597c478bd9Sstevel@tonic-gate 		return (ECONNABORTED);
62607c478bd9Sstevel@tonic-gate 
62617c478bd9Sstevel@tonic-gate 	case ECANCELED:
62627c478bd9Sstevel@tonic-gate 		return (0);
62637c478bd9Sstevel@tonic-gate 
62647c478bd9Sstevel@tonic-gate 	default:
62657c478bd9Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", r);
62667c478bd9Sstevel@tonic-gate 	}
62677c478bd9Sstevel@tonic-gate 
62687c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
62697c478bd9Sstevel@tonic-gate 
62707c478bd9Sstevel@tonic-gate 	vertex = vertex_get_by_name(inst_name);
62717c478bd9Sstevel@tonic-gate 	if (vertex == NULL) {
62727c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&dgraph_lock);
62737c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "%s: Can't find graph vertex. "
62747c478bd9Sstevel@tonic-gate 		    "The instance must have been removed.\n", inst_name);
6275a2c43da6SSeth Goldberg 		startd_free(inst_name, max_scf_fmri_size);
62767c478bd9Sstevel@tonic-gate 		return (0);
62777c478bd9Sstevel@tonic-gate 	}
62787c478bd9Sstevel@tonic-gate 
62797c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
62807c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
62817c478bd9Sstevel@tonic-gate 
62827c478bd9Sstevel@tonic-gate 	for (i = 0; i < NACTIONS; i++) {
62837c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(pg, admin_actions[i], prop) != 0) {
62847c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
62857c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
62867c478bd9Sstevel@tonic-gate 			default:
62877c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
62887c478bd9Sstevel@tonic-gate 				goto out;
62897c478bd9Sstevel@tonic-gate 
62907c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
62917c478bd9Sstevel@tonic-gate 				goto out;
62927c478bd9Sstevel@tonic-gate 
62937c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
62947c478bd9Sstevel@tonic-gate 				action_ts[i] = 0;
62957c478bd9Sstevel@tonic-gate 				continue;
62967c478bd9Sstevel@tonic-gate 
62977c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
62987c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
62997c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
63007c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
63017c478bd9Sstevel@tonic-gate 			}
63027c478bd9Sstevel@tonic-gate 		}
63037c478bd9Sstevel@tonic-gate 
63047c478bd9Sstevel@tonic-gate 		if (scf_property_type(prop, &type) != 0) {
63057c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
63067c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
63077c478bd9Sstevel@tonic-gate 			default:
63087c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
63097c478bd9Sstevel@tonic-gate 				goto out;
63107c478bd9Sstevel@tonic-gate 
63117c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
63127c478bd9Sstevel@tonic-gate 				action_ts[i] = 0;
63137c478bd9Sstevel@tonic-gate 				continue;
63147c478bd9Sstevel@tonic-gate 
63157c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
63167c478bd9Sstevel@tonic-gate 				bad_error("scf_property_type", scf_error());
63177c478bd9Sstevel@tonic-gate 			}
63187c478bd9Sstevel@tonic-gate 		}
63197c478bd9Sstevel@tonic-gate 
63207c478bd9Sstevel@tonic-gate 		if (type != SCF_TYPE_INTEGER) {
63217c478bd9Sstevel@tonic-gate 			action_ts[i] = 0;
63227c478bd9Sstevel@tonic-gate 			continue;
63237c478bd9Sstevel@tonic-gate 		}
63247c478bd9Sstevel@tonic-gate 
63257c478bd9Sstevel@tonic-gate 		if (scf_property_get_value(prop, val) != 0) {
63267c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
63277c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
63287c478bd9Sstevel@tonic-gate 			default:
63297c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
63307c478bd9Sstevel@tonic-gate 				goto out;
63317c478bd9Sstevel@tonic-gate 
63327c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
63337c478bd9Sstevel@tonic-gate 				goto out;
63347c478bd9Sstevel@tonic-gate 
63357c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
63367c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
63377c478bd9Sstevel@tonic-gate 				action_ts[i] = 0;
63387c478bd9Sstevel@tonic-gate 				continue;
63397c478bd9Sstevel@tonic-gate 
63407c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
63413eae19d9Swesolows 			case SCF_ERROR_PERMISSION_DENIED:
63427c478bd9Sstevel@tonic-gate 				bad_error("scf_property_get_value",
63437c478bd9Sstevel@tonic-gate 				    scf_error());
63447c478bd9Sstevel@tonic-gate 			}
63457c478bd9Sstevel@tonic-gate 		}
63467c478bd9Sstevel@tonic-gate 
63477c478bd9Sstevel@tonic-gate 		r = scf_value_get_integer(val, &action_ts[i]);
63487c478bd9Sstevel@tonic-gate 		assert(r == 0);
63497c478bd9Sstevel@tonic-gate 	}
63507c478bd9Sstevel@tonic-gate 
63517c478bd9Sstevel@tonic-gate 	a = ADMIN_EVENT_MAINT_ON_IMMEDIATE;
63527c478bd9Sstevel@tonic-gate 	if (action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ||
63537c478bd9Sstevel@tonic-gate 	    action_ts[ADMIN_EVENT_MAINT_ON]) {
63547c478bd9Sstevel@tonic-gate 		a = action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ?
63557c478bd9Sstevel@tonic-gate 		    ADMIN_EVENT_MAINT_ON_IMMEDIATE : ADMIN_EVENT_MAINT_ON;
63567c478bd9Sstevel@tonic-gate 
63577c478bd9Sstevel@tonic-gate 		vertex_send_event(vertex, admin_events[a]);
63587c478bd9Sstevel@tonic-gate 		r = libscf_unset_action(h, pg, a, action_ts[a]);
63597c478bd9Sstevel@tonic-gate 		switch (r) {
63607c478bd9Sstevel@tonic-gate 		case 0:
63617c478bd9Sstevel@tonic-gate 		case EACCES:
63627c478bd9Sstevel@tonic-gate 			break;
63637c478bd9Sstevel@tonic-gate 
63647c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
63657c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
63667c478bd9Sstevel@tonic-gate 			goto out;
63677c478bd9Sstevel@tonic-gate 
63687c478bd9Sstevel@tonic-gate 		case EPERM:
63697c478bd9Sstevel@tonic-gate 			uu_die("Insufficient privilege.\n");
63707c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
63717c478bd9Sstevel@tonic-gate 
63727c478bd9Sstevel@tonic-gate 		default:
63737c478bd9Sstevel@tonic-gate 			bad_error("libscf_unset_action", r);
63747c478bd9Sstevel@tonic-gate 		}
63757c478bd9Sstevel@tonic-gate 	}
63767c478bd9Sstevel@tonic-gate 
63777c478bd9Sstevel@tonic-gate 	while ((a = next_action(action_ts, NACTIONS)) != -1) {
63787c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
63797c478bd9Sstevel@tonic-gate 		    "Graph: processing %s action for %s.\n", admin_actions[a],
63807c478bd9Sstevel@tonic-gate 		    inst_name);
63817c478bd9Sstevel@tonic-gate 
63827c478bd9Sstevel@tonic-gate 		if (a == ADMIN_EVENT_REFRESH) {
63837c478bd9Sstevel@tonic-gate 			r = dgraph_refresh_instance(vertex, inst);
63847c478bd9Sstevel@tonic-gate 			switch (r) {
63857c478bd9Sstevel@tonic-gate 			case 0:
63867c478bd9Sstevel@tonic-gate 			case ECANCELED:
63877c478bd9Sstevel@tonic-gate 			case EINVAL:
63887c478bd9Sstevel@tonic-gate 			case -1:
63897c478bd9Sstevel@tonic-gate 				break;
63907c478bd9Sstevel@tonic-gate 
63917c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
63927c478bd9Sstevel@tonic-gate 				/* pg & inst are reset now, so just return. */
63937c478bd9Sstevel@tonic-gate 				ret = ECONNABORTED;
63947c478bd9Sstevel@tonic-gate 				goto out;
63957c478bd9Sstevel@tonic-gate 
63967c478bd9Sstevel@tonic-gate 			default:
63977c478bd9Sstevel@tonic-gate 				bad_error("dgraph_refresh_instance", r);
63987c478bd9Sstevel@tonic-gate 			}
63997c478bd9Sstevel@tonic-gate 		}
64007c478bd9Sstevel@tonic-gate 
64017c478bd9Sstevel@tonic-gate 		vertex_send_event(vertex, admin_events[a]);
64027c478bd9Sstevel@tonic-gate 
64037c478bd9Sstevel@tonic-gate 		r = libscf_unset_action(h, pg, a, action_ts[a]);
64047c478bd9Sstevel@tonic-gate 		switch (r) {
64057c478bd9Sstevel@tonic-gate 		case 0:
64067c478bd9Sstevel@tonic-gate 		case EACCES:
64077c478bd9Sstevel@tonic-gate 			break;
64087c478bd9Sstevel@tonic-gate 
64097c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
64107c478bd9Sstevel@tonic-gate 			ret = ECONNABORTED;
64117c478bd9Sstevel@tonic-gate 			goto out;
64127c478bd9Sstevel@tonic-gate 
64137c478bd9Sstevel@tonic-gate 		case EPERM:
64147c478bd9Sstevel@tonic-gate 			uu_die("Insufficient privilege.\n");
64157c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
64167c478bd9Sstevel@tonic-gate 
64177c478bd9Sstevel@tonic-gate 		default:
64187c478bd9Sstevel@tonic-gate 			bad_error("libscf_unset_action", r);
64197c478bd9Sstevel@tonic-gate 		}
64207c478bd9Sstevel@tonic-gate 
64217c478bd9Sstevel@tonic-gate 		action_ts[a] = 0;
64227c478bd9Sstevel@tonic-gate 	}
64237c478bd9Sstevel@tonic-gate 
64247c478bd9Sstevel@tonic-gate out:
64257c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
64267c478bd9Sstevel@tonic-gate 
64277c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
64287c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
64297c478bd9Sstevel@tonic-gate 	startd_free(inst_name, max_scf_fmri_size);
64307c478bd9Sstevel@tonic-gate 	return (ret);
64317c478bd9Sstevel@tonic-gate }
64327c478bd9Sstevel@tonic-gate 
64337c478bd9Sstevel@tonic-gate /*
64347c478bd9Sstevel@tonic-gate  * inst and pg_name are scratch space, and are unset on entry.
64357c478bd9Sstevel@tonic-gate  * Returns
64367c478bd9Sstevel@tonic-gate  *   0 - success
64377c478bd9Sstevel@tonic-gate  *   ECONNRESET - success, but repository handle rebound
64387c478bd9Sstevel@tonic-gate  *   ECONNABORTED - repository connection broken
64397c478bd9Sstevel@tonic-gate  */
64407c478bd9Sstevel@tonic-gate static int
process_pg_event(scf_handle_t * h,scf_propertygroup_t * pg,scf_instance_t * inst,char * pg_name)64417c478bd9Sstevel@tonic-gate process_pg_event(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst,
64427c478bd9Sstevel@tonic-gate     char *pg_name)
64437c478bd9Sstevel@tonic-gate {
64447c478bd9Sstevel@tonic-gate 	int r;
64457c478bd9Sstevel@tonic-gate 	scf_property_t *prop;
64467c478bd9Sstevel@tonic-gate 	scf_value_t *val;
64477c478bd9Sstevel@tonic-gate 	char *fmri;
64487c478bd9Sstevel@tonic-gate 	boolean_t rebound = B_FALSE, rebind_inst = B_FALSE;
64497c478bd9Sstevel@tonic-gate 
64507c478bd9Sstevel@tonic-gate 	if (scf_pg_get_name(pg, pg_name, max_scf_value_size) < 0) {
64517c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
64527c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
64537c478bd9Sstevel@tonic-gate 		default:
64547c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
64557c478bd9Sstevel@tonic-gate 
64567c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
64577c478bd9Sstevel@tonic-gate 			return (0);
64587c478bd9Sstevel@tonic-gate 
64597c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
64607c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_name", scf_error());
64617c478bd9Sstevel@tonic-gate 		}
64627c478bd9Sstevel@tonic-gate 	}
64637c478bd9Sstevel@tonic-gate 
64647c478bd9Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_GENERAL) == 0 ||
64657c478bd9Sstevel@tonic-gate 	    strcmp(pg_name, SCF_PG_GENERAL_OVR) == 0) {
64667c478bd9Sstevel@tonic-gate 		r = dgraph_update_general(pg);
64677c478bd9Sstevel@tonic-gate 		switch (r) {
64687c478bd9Sstevel@tonic-gate 		case 0:
64697c478bd9Sstevel@tonic-gate 		case ENOTSUP:
64707c478bd9Sstevel@tonic-gate 		case ECANCELED:
64717c478bd9Sstevel@tonic-gate 			return (0);
64727c478bd9Sstevel@tonic-gate 
64737c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
64747c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
64757c478bd9Sstevel@tonic-gate 
64767c478bd9Sstevel@tonic-gate 		case -1:
64777c478bd9Sstevel@tonic-gate 			/* Error should have been logged. */
64787c478bd9Sstevel@tonic-gate 			return (0);
64797c478bd9Sstevel@tonic-gate 
64807c478bd9Sstevel@tonic-gate 		default:
64817c478bd9Sstevel@tonic-gate 			bad_error("dgraph_update_general", r);
64827c478bd9Sstevel@tonic-gate 		}
64837c478bd9Sstevel@tonic-gate 	} else if (strcmp(pg_name, SCF_PG_RESTARTER_ACTIONS) == 0) {
64847c478bd9Sstevel@tonic-gate 		if (scf_pg_get_parent_instance(pg, inst) != 0) {
64857c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
64867c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
64877c478bd9Sstevel@tonic-gate 				return (ECONNABORTED);
64887c478bd9Sstevel@tonic-gate 
64897c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
64907c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONSTRAINT_VIOLATED:
64917c478bd9Sstevel@tonic-gate 				/* Ignore commands on services. */
64927c478bd9Sstevel@tonic-gate 				return (0);
64937c478bd9Sstevel@tonic-gate 
64947c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
64957c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
64967c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
64977c478bd9Sstevel@tonic-gate 			default:
64987c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_get_parent_instance",
64997c478bd9Sstevel@tonic-gate 				    scf_error());
65007c478bd9Sstevel@tonic-gate 			}
65017c478bd9Sstevel@tonic-gate 		}
65027c478bd9Sstevel@tonic-gate 
65037c478bd9Sstevel@tonic-gate 		return (process_actions(h, pg, inst));
65047c478bd9Sstevel@tonic-gate 	}
65057c478bd9Sstevel@tonic-gate 
65067c478bd9Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_OPTIONS) != 0 &&
65077c478bd9Sstevel@tonic-gate 	    strcmp(pg_name, SCF_PG_OPTIONS_OVR) != 0)
65087c478bd9Sstevel@tonic-gate 		return (0);
65097c478bd9Sstevel@tonic-gate 
65107c478bd9Sstevel@tonic-gate 	/*
65117c478bd9Sstevel@tonic-gate 	 * We only care about the options[_ovr] property groups of our own
65127c478bd9Sstevel@tonic-gate 	 * instance, so get the fmri and compare.  Plus, once we know it's
65137c478bd9Sstevel@tonic-gate 	 * correct, if the repository connection is broken we know exactly what
65147c478bd9Sstevel@tonic-gate 	 * property group we were operating on, and can look it up again.
65157c478bd9Sstevel@tonic-gate 	 */
65167c478bd9Sstevel@tonic-gate 	if (scf_pg_get_parent_instance(pg, inst) != 0) {
65177c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
65187c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
65197c478bd9Sstevel@tonic-gate 			return (ECONNABORTED);
65207c478bd9Sstevel@tonic-gate 
65217c478bd9Sstevel@tonic-gate 		case SCF_ERROR_DELETED:
65227c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONSTRAINT_VIOLATED:
65237c478bd9Sstevel@tonic-gate 			return (0);
65247c478bd9Sstevel@tonic-gate 
65257c478bd9Sstevel@tonic-gate 		case SCF_ERROR_HANDLE_MISMATCH:
65267c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_BOUND:
65277c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
65287c478bd9Sstevel@tonic-gate 		default:
65297c478bd9Sstevel@tonic-gate 			bad_error("scf_pg_get_parent_instance",
65307c478bd9Sstevel@tonic-gate 			    scf_error());
65317c478bd9Sstevel@tonic-gate 		}
65327c478bd9Sstevel@tonic-gate 	}
65337c478bd9Sstevel@tonic-gate 
65347c478bd9Sstevel@tonic-gate 	switch (r = libscf_instance_get_fmri(inst, &fmri)) {
65357c478bd9Sstevel@tonic-gate 	case 0:
65367c478bd9Sstevel@tonic-gate 		break;
65377c478bd9Sstevel@tonic-gate 
65387c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
65397c478bd9Sstevel@tonic-gate 		return (ECONNABORTED);
65407c478bd9Sstevel@tonic-gate 
65417c478bd9Sstevel@tonic-gate 	case ECANCELED:
65427c478bd9Sstevel@tonic-gate 		return (0);
65437c478bd9Sstevel@tonic-gate 
65447c478bd9Sstevel@tonic-gate 	default:
65457c478bd9Sstevel@tonic-gate 		bad_error("libscf_instance_get_fmri", r);
65467c478bd9Sstevel@tonic-gate 	}
65477c478bd9Sstevel@tonic-gate 
65487c478bd9Sstevel@tonic-gate 	if (strcmp(fmri, SCF_SERVICE_STARTD) != 0) {
65497c478bd9Sstevel@tonic-gate 		startd_free(fmri, max_scf_fmri_size);
65507c478bd9Sstevel@tonic-gate 		return (0);
65517c478bd9Sstevel@tonic-gate 	}
65527c478bd9Sstevel@tonic-gate 
6553f6e214c7SGavin Maltby 	/*
6554f6e214c7SGavin Maltby 	 * update the information events flag
6555f6e214c7SGavin Maltby 	 */
6556f6e214c7SGavin Maltby 	if (strcmp(pg_name, SCF_PG_OPTIONS) == 0)
6557f6e214c7SGavin Maltby 		info_events_all = libscf_get_info_events_all(pg);
6558f6e214c7SGavin Maltby 
65597c478bd9Sstevel@tonic-gate 	prop = safe_scf_property_create(h);
65607c478bd9Sstevel@tonic-gate 	val = safe_scf_value_create(h);
65617c478bd9Sstevel@tonic-gate 
65627c478bd9Sstevel@tonic-gate 	if (strcmp(pg_name, SCF_PG_OPTIONS_OVR) == 0) {
65637c478bd9Sstevel@tonic-gate 		/* See if we need to set the runlevel. */
65647c478bd9Sstevel@tonic-gate 		/* CONSTCOND */
65657c478bd9Sstevel@tonic-gate 		if (0) {
65667c478bd9Sstevel@tonic-gate rebind_pg:
65677c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
65687c478bd9Sstevel@tonic-gate 			rebound = B_TRUE;
65697c478bd9Sstevel@tonic-gate 
65707c478bd9Sstevel@tonic-gate 			r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst);
65717c478bd9Sstevel@tonic-gate 			switch (r) {
65727c478bd9Sstevel@tonic-gate 			case 0:
65737c478bd9Sstevel@tonic-gate 				break;
65747c478bd9Sstevel@tonic-gate 
65757c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
65767c478bd9Sstevel@tonic-gate 				goto rebind_pg;
65777c478bd9Sstevel@tonic-gate 
65787c478bd9Sstevel@tonic-gate 			case ENOENT:
65797c478bd9Sstevel@tonic-gate 				goto out;
65807c478bd9Sstevel@tonic-gate 
65817c478bd9Sstevel@tonic-gate 			case EINVAL:
65827c478bd9Sstevel@tonic-gate 			case ENOTSUP:
65837c478bd9Sstevel@tonic-gate 				bad_error("libscf_lookup_instance", r);
65847c478bd9Sstevel@tonic-gate 			}
65857c478bd9Sstevel@tonic-gate 
65867c478bd9Sstevel@tonic-gate 			if (scf_instance_get_pg(inst, pg_name, pg) != 0) {
65877c478bd9Sstevel@tonic-gate 				switch (scf_error()) {
65887c478bd9Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
65897c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_FOUND:
65907c478bd9Sstevel@tonic-gate 					goto out;
65917c478bd9Sstevel@tonic-gate 
65927c478bd9Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
65937c478bd9Sstevel@tonic-gate 					goto rebind_pg;
65947c478bd9Sstevel@tonic-gate 
65957c478bd9Sstevel@tonic-gate 				case SCF_ERROR_HANDLE_MISMATCH:
65967c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_BOUND:
65977c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
65987c478bd9Sstevel@tonic-gate 				case SCF_ERROR_INVALID_ARGUMENT:
65997c478bd9Sstevel@tonic-gate 				default:
66007c478bd9Sstevel@tonic-gate 					bad_error("scf_instance_get_pg",
66017c478bd9Sstevel@tonic-gate 					    scf_error());
66027c478bd9Sstevel@tonic-gate 				}
66037c478bd9Sstevel@tonic-gate 			}
66047c478bd9Sstevel@tonic-gate 		}
66057c478bd9Sstevel@tonic-gate 
66067c478bd9Sstevel@tonic-gate 		if (scf_pg_get_property(pg, "runlevel", prop) == 0) {
66077c478bd9Sstevel@tonic-gate 			r = dgraph_set_runlevel(pg, prop);
66087c478bd9Sstevel@tonic-gate 			switch (r) {
66097c478bd9Sstevel@tonic-gate 			case ECONNRESET:
66107c478bd9Sstevel@tonic-gate 				rebound = B_TRUE;
66117c478bd9Sstevel@tonic-gate 				rebind_inst = B_TRUE;
66127c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
66137c478bd9Sstevel@tonic-gate 
66147c478bd9Sstevel@tonic-gate 			case 0:
66157c478bd9Sstevel@tonic-gate 				break;
66167c478bd9Sstevel@tonic-gate 
66177c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
66187c478bd9Sstevel@tonic-gate 				goto rebind_pg;
66197c478bd9Sstevel@tonic-gate 
66207c478bd9Sstevel@tonic-gate 			case ECANCELED:
66217c478bd9Sstevel@tonic-gate 				goto out;
66227c478bd9Sstevel@tonic-gate 
66237c478bd9Sstevel@tonic-gate 			default:
66247c478bd9Sstevel@tonic-gate 				bad_error("dgraph_set_runlevel", r);
66257c478bd9Sstevel@tonic-gate 			}
66267c478bd9Sstevel@tonic-gate 		} else {
66277c478bd9Sstevel@tonic-gate 			switch (scf_error()) {
66287c478bd9Sstevel@tonic-gate 			case SCF_ERROR_CONNECTION_BROKEN:
66297c478bd9Sstevel@tonic-gate 			default:
66307c478bd9Sstevel@tonic-gate 				goto rebind_pg;
66317c478bd9Sstevel@tonic-gate 
66327c478bd9Sstevel@tonic-gate 			case SCF_ERROR_DELETED:
66337c478bd9Sstevel@tonic-gate 				goto out;
66347c478bd9Sstevel@tonic-gate 
66357c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_FOUND:
66367c478bd9Sstevel@tonic-gate 				break;
66377c478bd9Sstevel@tonic-gate 
66387c478bd9Sstevel@tonic-gate 			case SCF_ERROR_INVALID_ARGUMENT:
66397c478bd9Sstevel@tonic-gate 			case SCF_ERROR_HANDLE_MISMATCH:
66407c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_BOUND:
66417c478bd9Sstevel@tonic-gate 			case SCF_ERROR_NOT_SET:
66427c478bd9Sstevel@tonic-gate 				bad_error("scf_pg_get_property", scf_error());
66437c478bd9Sstevel@tonic-gate 			}
66447c478bd9Sstevel@tonic-gate 		}
66457c478bd9Sstevel@tonic-gate 	}
66467c478bd9Sstevel@tonic-gate 
66477c478bd9Sstevel@tonic-gate 	if (rebind_inst) {
66487c478bd9Sstevel@tonic-gate lookup_inst:
66497c478bd9Sstevel@tonic-gate 		r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst);
66507c478bd9Sstevel@tonic-gate 		switch (r) {
66517c478bd9Sstevel@tonic-gate 		case 0:
66527c478bd9Sstevel@tonic-gate 			break;
66537c478bd9Sstevel@tonic-gate 
66547c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
66557c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
66567c478bd9Sstevel@tonic-gate 			rebound = B_TRUE;
66577c478bd9Sstevel@tonic-gate 			goto lookup_inst;
66587c478bd9Sstevel@tonic-gate 
66597c478bd9Sstevel@tonic-gate 		case ENOENT:
66607c478bd9Sstevel@tonic-gate 			goto out;
66617c478bd9Sstevel@tonic-gate 
66627c478bd9Sstevel@tonic-gate 		case EINVAL:
66637c478bd9Sstevel@tonic-gate 		case ENOTSUP:
66647c478bd9Sstevel@tonic-gate 			bad_error("libscf_lookup_instance", r);
66657c478bd9Sstevel@tonic-gate 		}
66667c478bd9Sstevel@tonic-gate 	}
66677c478bd9Sstevel@tonic-gate 
66687c478bd9Sstevel@tonic-gate 	r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size);
66697c478bd9Sstevel@tonic-gate 	switch (r) {
66707c478bd9Sstevel@tonic-gate 	case 0:
66717c478bd9Sstevel@tonic-gate 		break;
66727c478bd9Sstevel@tonic-gate 
66737c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
66747c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
66757c478bd9Sstevel@tonic-gate 		rebound = B_TRUE;
66767c478bd9Sstevel@tonic-gate 		goto lookup_inst;
66777c478bd9Sstevel@tonic-gate 
66787c478bd9Sstevel@tonic-gate 	case EINVAL:
66797c478bd9Sstevel@tonic-gate 		log_error(LOG_NOTICE,
66807c478bd9Sstevel@tonic-gate 		    "%s/%s property of %s is misconfigured.\n", pg_name,
66817c478bd9Sstevel@tonic-gate 		    SCF_PROPERTY_MILESTONE, SCF_SERVICE_STARTD);
66827c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
66837c478bd9Sstevel@tonic-gate 
66847c478bd9Sstevel@tonic-gate 	case ECANCELED:
66857c478bd9Sstevel@tonic-gate 	case ENOENT:
66867c478bd9Sstevel@tonic-gate 		(void) strcpy(fmri, "all");
66877c478bd9Sstevel@tonic-gate 		break;
66887c478bd9Sstevel@tonic-gate 
66897c478bd9Sstevel@tonic-gate 	default:
66907c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_milestone", r);
66917c478bd9Sstevel@tonic-gate 	}
66927c478bd9Sstevel@tonic-gate 
66937c478bd9Sstevel@tonic-gate 	r = dgraph_set_milestone(fmri, h, B_FALSE);
66947c478bd9Sstevel@tonic-gate 	switch (r) {
66957c478bd9Sstevel@tonic-gate 	case 0:
66967c478bd9Sstevel@tonic-gate 	case ECONNRESET:
66977c478bd9Sstevel@tonic-gate 	case EALREADY:
66987c478bd9Sstevel@tonic-gate 		break;
66997c478bd9Sstevel@tonic-gate 
67007c478bd9Sstevel@tonic-gate 	case EINVAL:
67017c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "Milestone %s is invalid.\n", fmri);
67027c478bd9Sstevel@tonic-gate 		break;
67037c478bd9Sstevel@tonic-gate 
67047c478bd9Sstevel@tonic-gate 	case ENOENT:
67057c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING, "Milestone %s does not exist.\n", fmri);
67067c478bd9Sstevel@tonic-gate 		break;
67077c478bd9Sstevel@tonic-gate 
67087c478bd9Sstevel@tonic-gate 	default:
67097c478bd9Sstevel@tonic-gate 		bad_error("dgraph_set_milestone", r);
67107c478bd9Sstevel@tonic-gate 	}
67117c478bd9Sstevel@tonic-gate 
67127c478bd9Sstevel@tonic-gate out:
67137c478bd9Sstevel@tonic-gate 	startd_free(fmri, max_scf_fmri_size);
67147c478bd9Sstevel@tonic-gate 	scf_value_destroy(val);
67157c478bd9Sstevel@tonic-gate 	scf_property_destroy(prop);
67167c478bd9Sstevel@tonic-gate 
67177c478bd9Sstevel@tonic-gate 	return (rebound ? ECONNRESET : 0);
67187c478bd9Sstevel@tonic-gate }
67197c478bd9Sstevel@tonic-gate 
6720ffbfbf47Srm88369 /*
6721ffbfbf47Srm88369  * process_delete() deletes an instance from the dgraph if 'fmri' is an
6722ffbfbf47Srm88369  * instance fmri or if 'fmri' matches the 'general' property group of an
6723ffbfbf47Srm88369  * instance (or the 'general/enabled' property).
6724ffbfbf47Srm88369  *
6725ffbfbf47Srm88369  * 'fmri' may be overwritten and cannot be trusted on return by the caller.
6726ffbfbf47Srm88369  */
67277c478bd9Sstevel@tonic-gate static void
process_delete(char * fmri,scf_handle_t * h)67287c478bd9Sstevel@tonic-gate process_delete(char *fmri, scf_handle_t *h)
67297c478bd9Sstevel@tonic-gate {
6730ffbfbf47Srm88369 	char *lfmri, *end_inst_fmri;
6731ffbfbf47Srm88369 	const char *inst_name = NULL;
6732ffbfbf47Srm88369 	const char *pg_name = NULL;
6733ffbfbf47Srm88369 	const char *prop_name = NULL;
67347c478bd9Sstevel@tonic-gate 
67357c478bd9Sstevel@tonic-gate 	lfmri = safe_strdup(fmri);
67367c478bd9Sstevel@tonic-gate 
67377c478bd9Sstevel@tonic-gate 	/* Determine if the FMRI is a property group or instance */
67387c478bd9Sstevel@tonic-gate 	if (scf_parse_svc_fmri(lfmri, NULL, NULL, &inst_name, &pg_name,
6739ffbfbf47Srm88369 	    &prop_name) != SCF_SUCCESS) {
67407c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
67417c478bd9Sstevel@tonic-gate 		    "Received invalid FMRI \"%s\" from repository server.\n",
67427c478bd9Sstevel@tonic-gate 		    fmri);
67437c478bd9Sstevel@tonic-gate 	} else if (inst_name != NULL && pg_name == NULL) {
67447c478bd9Sstevel@tonic-gate 		(void) dgraph_remove_instance(fmri, h);
6745ffbfbf47Srm88369 	} else if (inst_name != NULL && pg_name != NULL) {
6746ffbfbf47Srm88369 		/*
6747ffbfbf47Srm88369 		 * If we're deleting the 'general' property group or
6748ffbfbf47Srm88369 		 * 'general/enabled' property then the whole instance
6749ffbfbf47Srm88369 		 * must be removed from the dgraph.
6750ffbfbf47Srm88369 		 */
6751ffbfbf47Srm88369 		if (strcmp(pg_name, SCF_PG_GENERAL) != 0) {
6752ffbfbf47Srm88369 			free(lfmri);
6753ffbfbf47Srm88369 			return;
6754ffbfbf47Srm88369 		}
6755ffbfbf47Srm88369 
6756ffbfbf47Srm88369 		if (prop_name != NULL &&
6757ffbfbf47Srm88369 		    strcmp(prop_name, SCF_PROPERTY_ENABLED) != 0) {
6758ffbfbf47Srm88369 			free(lfmri);
6759ffbfbf47Srm88369 			return;
6760ffbfbf47Srm88369 		}
6761ffbfbf47Srm88369 
6762ffbfbf47Srm88369 		/*
6763ffbfbf47Srm88369 		 * Because the instance has already been deleted from the
6764ffbfbf47Srm88369 		 * repository, we cannot use any scf_ functions to retrieve
6765ffbfbf47Srm88369 		 * the instance FMRI however we can easily reconstruct it
6766ffbfbf47Srm88369 		 * manually.
6767ffbfbf47Srm88369 		 */
6768ffbfbf47Srm88369 		end_inst_fmri = strstr(fmri, SCF_FMRI_PROPERTYGRP_PREFIX);
6769ffbfbf47Srm88369 		if (end_inst_fmri == NULL)
6770ffbfbf47Srm88369 			bad_error("process_delete", 0);
6771ffbfbf47Srm88369 
6772ffbfbf47Srm88369 		end_inst_fmri[0] = '\0';
6773ffbfbf47Srm88369 
6774ffbfbf47Srm88369 		(void) dgraph_remove_instance(fmri, h);
67757c478bd9Sstevel@tonic-gate 	}
67767c478bd9Sstevel@tonic-gate 
67777c478bd9Sstevel@tonic-gate 	free(lfmri);
67787c478bd9Sstevel@tonic-gate }
67797c478bd9Sstevel@tonic-gate 
67807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
67817c478bd9Sstevel@tonic-gate void *
repository_event_thread(void * unused)67827c478bd9Sstevel@tonic-gate repository_event_thread(void *unused)
67837c478bd9Sstevel@tonic-gate {
67847c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
67857c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
67867c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
67877c478bd9Sstevel@tonic-gate 	char *fmri = startd_alloc(max_scf_fmri_size);
67887c478bd9Sstevel@tonic-gate 	char *pg_name = startd_alloc(max_scf_value_size);
67897c478bd9Sstevel@tonic-gate 	int r;
67907c478bd9Sstevel@tonic-gate 
6791ab618543SJohn Levon 	(void) pthread_setname_np(pthread_self(), "repository_event");
6792ab618543SJohn Levon 
67937c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
67947c478bd9Sstevel@tonic-gate 
67957c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
67967c478bd9Sstevel@tonic-gate 	inst = safe_scf_instance_create(h);
67977c478bd9Sstevel@tonic-gate 
67987c478bd9Sstevel@tonic-gate retry:
67997c478bd9Sstevel@tonic-gate 	if (_scf_notify_add_pgtype(h, SCF_GROUP_FRAMEWORK) != SCF_SUCCESS) {
68007c478bd9Sstevel@tonic-gate 		if (scf_error() == SCF_ERROR_CONNECTION_BROKEN) {
68017c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
68027c478bd9Sstevel@tonic-gate 		} else {
68037c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
68047c478bd9Sstevel@tonic-gate 			    "Couldn't set up repository notification "
68057c478bd9Sstevel@tonic-gate 			    "for property group type %s: %s\n",
68067c478bd9Sstevel@tonic-gate 			    SCF_GROUP_FRAMEWORK, scf_strerror(scf_error()));
68077c478bd9Sstevel@tonic-gate 
68087c478bd9Sstevel@tonic-gate 			(void) sleep(1);
68097c478bd9Sstevel@tonic-gate 		}
68107c478bd9Sstevel@tonic-gate 
68117c478bd9Sstevel@tonic-gate 		goto retry;
68127c478bd9Sstevel@tonic-gate 	}
68137c478bd9Sstevel@tonic-gate 
68147c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
68157c478bd9Sstevel@tonic-gate 	while (1) {
68167c478bd9Sstevel@tonic-gate 		ssize_t res;
68177c478bd9Sstevel@tonic-gate 
68187c478bd9Sstevel@tonic-gate 		/* Note: fmri is only set on delete events. */
68197c478bd9Sstevel@tonic-gate 		res = _scf_notify_wait(pg, fmri, max_scf_fmri_size);
68207c478bd9Sstevel@tonic-gate 		if (res < 0) {
68217c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
68227c478bd9Sstevel@tonic-gate 			goto retry;
68237c478bd9Sstevel@tonic-gate 		} else if (res == 0) {
68247c478bd9Sstevel@tonic-gate 			/*
68257c478bd9Sstevel@tonic-gate 			 * property group modified.  inst and pg_name are
68267c478bd9Sstevel@tonic-gate 			 * pre-allocated scratch space.
68277c478bd9Sstevel@tonic-gate 			 */
68287c478bd9Sstevel@tonic-gate 			if (scf_pg_update(pg) < 0) {
68297c478bd9Sstevel@tonic-gate 				switch (scf_error()) {
68307c478bd9Sstevel@tonic-gate 				case SCF_ERROR_DELETED:
68317c478bd9Sstevel@tonic-gate 					continue;
68327c478bd9Sstevel@tonic-gate 
68337c478bd9Sstevel@tonic-gate 				case SCF_ERROR_CONNECTION_BROKEN:
68347c478bd9Sstevel@tonic-gate 					log_error(LOG_WARNING,
68357c478bd9Sstevel@tonic-gate 					    "Lost repository event due to "
68367c478bd9Sstevel@tonic-gate 					    "disconnection.\n");
68377c478bd9Sstevel@tonic-gate 					libscf_handle_rebind(h);
68387c478bd9Sstevel@tonic-gate 					goto retry;
68397c478bd9Sstevel@tonic-gate 
68407c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_BOUND:
68417c478bd9Sstevel@tonic-gate 				case SCF_ERROR_NOT_SET:
68427c478bd9Sstevel@tonic-gate 				default:
68437c478bd9Sstevel@tonic-gate 					bad_error("scf_pg_update", scf_error());
68447c478bd9Sstevel@tonic-gate 				}
68457c478bd9Sstevel@tonic-gate 			}
68467c478bd9Sstevel@tonic-gate 
68477c478bd9Sstevel@tonic-gate 			r = process_pg_event(h, pg, inst, pg_name);
68487c478bd9Sstevel@tonic-gate 			switch (r) {
68497c478bd9Sstevel@tonic-gate 			case 0:
68507c478bd9Sstevel@tonic-gate 				break;
68517c478bd9Sstevel@tonic-gate 
68527c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
68537c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "Lost repository event "
68547c478bd9Sstevel@tonic-gate 				    "due to disconnection.\n");
68557c478bd9Sstevel@tonic-gate 				libscf_handle_rebind(h);
68567c478bd9Sstevel@tonic-gate 				/* FALLTHROUGH */
68577c478bd9Sstevel@tonic-gate 
68587c478bd9Sstevel@tonic-gate 			case ECONNRESET:
68597c478bd9Sstevel@tonic-gate 				goto retry;
68607c478bd9Sstevel@tonic-gate 
68617c478bd9Sstevel@tonic-gate 			default:
68627c478bd9Sstevel@tonic-gate 				bad_error("process_pg_event", r);
68637c478bd9Sstevel@tonic-gate 			}
68647c478bd9Sstevel@tonic-gate 		} else {
6865ffbfbf47Srm88369 			/*
6866ffbfbf47Srm88369 			 * Service, instance, or pg deleted.
6867ffbfbf47Srm88369 			 * Don't trust fmri on return.
6868ffbfbf47Srm88369 			 */
68697c478bd9Sstevel@tonic-gate 			process_delete(fmri, h);
68707c478bd9Sstevel@tonic-gate 		}
68717c478bd9Sstevel@tonic-gate 	}
68727c478bd9Sstevel@tonic-gate 
68737c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
68747c478bd9Sstevel@tonic-gate 	return (NULL);
68757c478bd9Sstevel@tonic-gate }
68767c478bd9Sstevel@tonic-gate 
68777c478bd9Sstevel@tonic-gate void
graph_engine_start()68787c478bd9Sstevel@tonic-gate graph_engine_start()
68797c478bd9Sstevel@tonic-gate {
68807c478bd9Sstevel@tonic-gate 	int err;
68817c478bd9Sstevel@tonic-gate 
68827c478bd9Sstevel@tonic-gate 	(void) startd_thread_create(graph_thread, NULL);
68837c478bd9Sstevel@tonic-gate 
68847c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&dgraph_lock);
68857c478bd9Sstevel@tonic-gate 	while (!initial_milestone_set) {
68867c478bd9Sstevel@tonic-gate 		err = pthread_cond_wait(&initial_milestone_cv, &dgraph_lock);
68877c478bd9Sstevel@tonic-gate 		assert(err == 0);
68887c478bd9Sstevel@tonic-gate 	}
68897c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&dgraph_lock);
68907c478bd9Sstevel@tonic-gate 
68917c478bd9Sstevel@tonic-gate 	(void) startd_thread_create(repository_event_thread, NULL);
68927c478bd9Sstevel@tonic-gate 	(void) startd_thread_create(graph_event_thread, NULL);
68937c478bd9Sstevel@tonic-gate }
6894