10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51514Srm88369 * Common Development and Distribution License (the "License").
61514Srm88369 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
2111466SRoger.Faulkner@Sun.COM
220Sstevel@tonic-gate /*
2312225SGowtham.Thommandra@Sun.COM * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * graph.c - master restarter graph engine
280Sstevel@tonic-gate *
290Sstevel@tonic-gate * The graph engine keeps a dependency graph of all service instances on the
300Sstevel@tonic-gate * system, as recorded in the repository. It decides when services should
310Sstevel@tonic-gate * be brought up or down based on service states and dependencies and sends
320Sstevel@tonic-gate * commands to restarters to effect any changes. It also executes
330Sstevel@tonic-gate * administrator commands sent by svcadm via the repository.
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * The graph is stored in uu_list_t *dgraph and its vertices are
360Sstevel@tonic-gate * graph_vertex_t's, each of which has a name and an integer id unique to
370Sstevel@tonic-gate * its name (see dict.c). A vertex's type attribute designates the type
380Sstevel@tonic-gate * of object it represents: GVT_INST for service instances, GVT_SVC for
390Sstevel@tonic-gate * service objects (since service instances may depend on another service,
400Sstevel@tonic-gate * rather than service instance), GVT_FILE for files (which services may
410Sstevel@tonic-gate * depend on), and GVT_GROUP for dependencies on multiple objects. GVT_GROUP
420Sstevel@tonic-gate * vertices are necessary because dependency lists may have particular
430Sstevel@tonic-gate * grouping types (require any, require all, optional, or exclude) and
440Sstevel@tonic-gate * event-propagation characteristics.
450Sstevel@tonic-gate *
460Sstevel@tonic-gate * The initial graph is built by libscf_populate_graph() invoking
470Sstevel@tonic-gate * dgraph_add_instance() for each instance in the repository. The function
480Sstevel@tonic-gate * adds a GVT_SVC vertex for the service if one does not already exist, adds
490Sstevel@tonic-gate * a GVT_INST vertex named by the FMRI of the instance, and sets up the edges.
500Sstevel@tonic-gate * The resulting web of vertices & edges associated with an instance's vertex
510Sstevel@tonic-gate * includes
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * - an edge from the GVT_SVC vertex for the instance's service
540Sstevel@tonic-gate *
550Sstevel@tonic-gate * - an edge to the GVT_INST vertex of the instance's resarter, if its
560Sstevel@tonic-gate * restarter is not svc.startd
570Sstevel@tonic-gate *
580Sstevel@tonic-gate * - edges from other GVT_INST vertices if the instance is a restarter
590Sstevel@tonic-gate *
600Sstevel@tonic-gate * - for each dependency property group in the instance's "running"
610Sstevel@tonic-gate * snapshot, an edge to a GVT_GROUP vertex named by the FMRI of the
620Sstevel@tonic-gate * instance and the name of the property group
630Sstevel@tonic-gate *
640Sstevel@tonic-gate * - for each value of the "entities" property in each dependency property
650Sstevel@tonic-gate * group, an edge from the corresponding GVT_GROUP vertex to a
660Sstevel@tonic-gate * GVT_INST, GVT_SVC, or GVT_FILE vertex
670Sstevel@tonic-gate *
680Sstevel@tonic-gate * - edges from GVT_GROUP vertices for each dependent instance
690Sstevel@tonic-gate *
700Sstevel@tonic-gate * After the edges are set up the vertex's GV_CONFIGURED flag is set. If
710Sstevel@tonic-gate * there are problems, or if a service is mentioned in a dependency but does
720Sstevel@tonic-gate * not exist in the repository, the GV_CONFIGURED flag will be clear.
730Sstevel@tonic-gate *
740Sstevel@tonic-gate * The graph and all of its vertices are protected by the dgraph_lock mutex.
750Sstevel@tonic-gate * See restarter.c for more information.
760Sstevel@tonic-gate *
770Sstevel@tonic-gate * The properties of an instance fall into two classes: immediate and
780Sstevel@tonic-gate * snapshotted. Immediate properties should have an immediate effect when
790Sstevel@tonic-gate * changed. Snapshotted properties should be read from a snapshot, so they
800Sstevel@tonic-gate * only change when the snapshot changes. The immediate properties used by
810Sstevel@tonic-gate * the graph engine are general/enabled, general/restarter, and the properties
820Sstevel@tonic-gate * in the restarter_actions property group. Since they are immediate, they
830Sstevel@tonic-gate * are not read out of a snapshot. The snapshotted properties used by the
840Sstevel@tonic-gate * graph engine are those in the property groups with type "dependency" and
850Sstevel@tonic-gate * are read out of the "running" snapshot. The "running" snapshot is created
860Sstevel@tonic-gate * by the the graph engine as soon as possible, and it is updated, along with
870Sstevel@tonic-gate * in-core copies of the data (dependency information for the graph engine) on
880Sstevel@tonic-gate * receipt of the refresh command from svcadm. In addition, the graph engine
890Sstevel@tonic-gate * updates the "start" snapshot from the "running" snapshot whenever a service
900Sstevel@tonic-gate * comes online.
917630SRenaud.Manus@Sun.COM *
927630SRenaud.Manus@Sun.COM * When a DISABLE event is requested by the administrator, svc.startd shutdown
937630SRenaud.Manus@Sun.COM * the dependents first before shutting down the requested service.
947630SRenaud.Manus@Sun.COM * In graph_enable_by_vertex, we create a subtree that contains the dependent
957630SRenaud.Manus@Sun.COM * vertices by marking those vertices with the GV_TOOFFLINE flag. And we mark
967630SRenaud.Manus@Sun.COM * the vertex to disable with the GV_TODISABLE flag. Once the tree is created,
977630SRenaud.Manus@Sun.COM * we send the _ADMIN_DISABLE event to the leaves. The leaves will then
987630SRenaud.Manus@Sun.COM * transition from STATE_ONLINE/STATE_DEGRADED to STATE_OFFLINE/STATE_MAINT.
997630SRenaud.Manus@Sun.COM * In gt_enter_offline and gt_enter_maint if the vertex was in a subtree then
1007630SRenaud.Manus@Sun.COM * we clear the GV_TOOFFLINE flag and walk the dependencies to offline the new
1017630SRenaud.Manus@Sun.COM * exposed leaves. We do the same until we reach the last leaf (the one with
1027630SRenaud.Manus@Sun.COM * the GV_TODISABLE flag). If the vertex to disable is also part of a larger
1037630SRenaud.Manus@Sun.COM * subtree (eg. multiple DISABLE events on vertices in the same subtree) then
1047630SRenaud.Manus@Sun.COM * once the first vertex is disabled (GV_TODISABLE flag is removed), we
1057630SRenaud.Manus@Sun.COM * continue to propagate the offline event to the vertex's dependencies.
106*12967Sgavin.maltby@oracle.com *
107*12967Sgavin.maltby@oracle.com *
108*12967Sgavin.maltby@oracle.com * SMF state transition notifications
109*12967Sgavin.maltby@oracle.com *
110*12967Sgavin.maltby@oracle.com * When an instance of a service managed by SMF changes state, svc.startd may
111*12967Sgavin.maltby@oracle.com * publish a GPEC sysevent. All transitions to or from maintenance, a
112*12967Sgavin.maltby@oracle.com * transition cause by a hardware error will generate an event.
113*12967Sgavin.maltby@oracle.com * Other transitions will generate an event if there exist notification
114*12967Sgavin.maltby@oracle.com * parameter for that transition. Notification parameters are stored in the
115*12967Sgavin.maltby@oracle.com * SMF repository for the service/instance they refer to. System-wide
116*12967Sgavin.maltby@oracle.com * notification parameters are stored in the global instance.
117*12967Sgavin.maltby@oracle.com * svc.startd can be told to send events for all SMF state transitions despite
118*12967Sgavin.maltby@oracle.com * of notification parameters by setting options/info_events_all to true in
119*12967Sgavin.maltby@oracle.com * restarter:default
120*12967Sgavin.maltby@oracle.com *
121*12967Sgavin.maltby@oracle.com * The set of transitions that generate events is cached in the
122*12967Sgavin.maltby@oracle.com * dgraph_vertex_t gv_stn_tset for service/instance and in the global
123*12967Sgavin.maltby@oracle.com * stn_global for the system-wide set. They are re-read when instances are
124*12967Sgavin.maltby@oracle.com * refreshed.
125*12967Sgavin.maltby@oracle.com *
126*12967Sgavin.maltby@oracle.com * The GPEC events published by svc.startd are consumed by fmd(1M). After
127*12967Sgavin.maltby@oracle.com * processing these events, fmd(1M) publishes the processed events to
128*12967Sgavin.maltby@oracle.com * notification agents. The notification agents read the notification
129*12967Sgavin.maltby@oracle.com * parameters from the SMF repository through libscf(3LIB) interfaces and send
130*12967Sgavin.maltby@oracle.com * the notification, or not, based on those parameters.
131*12967Sgavin.maltby@oracle.com *
132*12967Sgavin.maltby@oracle.com * Subscription and publishing to the GPEC channels is done with the
133*12967Sgavin.maltby@oracle.com * libfmevent(3LIB) wrappers fmev_[r]publish_*() and
134*12967Sgavin.maltby@oracle.com * fmev_shdl_(un)subscribe().
135*12967Sgavin.maltby@oracle.com *
1360Sstevel@tonic-gate */
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate #include <sys/uadmin.h>
1390Sstevel@tonic-gate #include <sys/wait.h>
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate #include <assert.h>
1420Sstevel@tonic-gate #include <errno.h>
1430Sstevel@tonic-gate #include <fcntl.h>
144*12967Sgavin.maltby@oracle.com #include <fm/libfmevent.h>
1450Sstevel@tonic-gate #include <libscf.h>
1460Sstevel@tonic-gate #include <libscf_priv.h>
147*12967Sgavin.maltby@oracle.com #include <librestart.h>
1480Sstevel@tonic-gate #include <libuutil.h>
1490Sstevel@tonic-gate #include <locale.h>
1500Sstevel@tonic-gate #include <poll.h>
1510Sstevel@tonic-gate #include <pthread.h>
1520Sstevel@tonic-gate #include <signal.h>
1530Sstevel@tonic-gate #include <stddef.h>
1540Sstevel@tonic-gate #include <stdio.h>
1550Sstevel@tonic-gate #include <stdlib.h>
1560Sstevel@tonic-gate #include <string.h>
1570Sstevel@tonic-gate #include <strings.h>
1580Sstevel@tonic-gate #include <sys/statvfs.h>
1590Sstevel@tonic-gate #include <sys/uadmin.h>
1600Sstevel@tonic-gate #include <zone.h>
1619160SSherry.Moore@Sun.COM #if defined(__i386)
1629160SSherry.Moore@Sun.COM #include <libgrubmgmt.h>
1639160SSherry.Moore@Sun.COM #endif /* __i386 */
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate #include "startd.h"
1660Sstevel@tonic-gate #include "protocol.h"
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate #define MILESTONE_NONE ((graph_vertex_t *)1)
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate #define CONSOLE_LOGIN_FMRI "svc:/system/console-login:default"
1720Sstevel@tonic-gate #define FS_MINIMAL_FMRI "svc:/system/filesystem/minimal:default"
1730Sstevel@tonic-gate
1741712Srm88369 #define VERTEX_REMOVED 0 /* vertex has been freed */
1751712Srm88369 #define VERTEX_INUSE 1 /* vertex is still in use */
1761712Srm88369
177*12967Sgavin.maltby@oracle.com #define IS_ENABLED(v) ((v)->gv_flags & (GV_ENABLED | GV_ENBLD_NOOVR))
178*12967Sgavin.maltby@oracle.com
179*12967Sgavin.maltby@oracle.com /*
180*12967Sgavin.maltby@oracle.com * stn_global holds the tset for the system wide notification parameters.
181*12967Sgavin.maltby@oracle.com * It is updated on refresh of svc:/system/svc/global:default
182*12967Sgavin.maltby@oracle.com *
183*12967Sgavin.maltby@oracle.com * There are two assumptions that relax the need for a mutex:
184*12967Sgavin.maltby@oracle.com * 1. 32-bit value assignments are atomic
185*12967Sgavin.maltby@oracle.com * 2. Its value is consumed only in one point at
186*12967Sgavin.maltby@oracle.com * dgraph_state_transition_notify(). There are no test and set races.
187*12967Sgavin.maltby@oracle.com *
188*12967Sgavin.maltby@oracle.com * If either assumption is broken, we'll need a mutex to synchronize
189*12967Sgavin.maltby@oracle.com * access to stn_global
190*12967Sgavin.maltby@oracle.com */
191*12967Sgavin.maltby@oracle.com int32_t stn_global;
192*12967Sgavin.maltby@oracle.com /*
193*12967Sgavin.maltby@oracle.com * info_events_all holds a flag to override notification parameters and send
194*12967Sgavin.maltby@oracle.com * Information events for all state transitions.
195*12967Sgavin.maltby@oracle.com * same about the need of a mutex here.
196*12967Sgavin.maltby@oracle.com */
197*12967Sgavin.maltby@oracle.com int info_events_all;
198*12967Sgavin.maltby@oracle.com
1992747Sbustos /*
2002747Sbustos * Services in these states are not considered 'down' by the
2012747Sbustos * milestone/shutdown code.
2022747Sbustos */
2032747Sbustos #define up_state(state) ((state) == RESTARTER_STATE_ONLINE || \
2042747Sbustos (state) == RESTARTER_STATE_DEGRADED || \
2052747Sbustos (state) == RESTARTER_STATE_OFFLINE)
2062747Sbustos
20712225SGowtham.Thommandra@Sun.COM #define is_depgrp_bypassed(v) ((v->gv_type == GVT_GROUP) && \
20812225SGowtham.Thommandra@Sun.COM ((v->gv_depgroup == DEPGRP_EXCLUDE_ALL) || \
20912225SGowtham.Thommandra@Sun.COM (v->gv_depgroup == DEPGRP_OPTIONAL_ALL) || \
21012225SGowtham.Thommandra@Sun.COM (v->gv_restart < RERR_RESTART)))
21112225SGowtham.Thommandra@Sun.COM
2120Sstevel@tonic-gate static uu_list_pool_t *graph_edge_pool, *graph_vertex_pool;
2130Sstevel@tonic-gate static uu_list_t *dgraph;
2140Sstevel@tonic-gate static pthread_mutex_t dgraph_lock;
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate /*
2170Sstevel@tonic-gate * milestone indicates the current subgraph. When NULL, it is the entire
2180Sstevel@tonic-gate * graph. When MILESTONE_NONE, it is the empty graph. Otherwise, it is all
2190Sstevel@tonic-gate * services on which the target vertex depends.
2200Sstevel@tonic-gate */
2210Sstevel@tonic-gate static graph_vertex_t *milestone = NULL;
2220Sstevel@tonic-gate static boolean_t initial_milestone_set = B_FALSE;
2230Sstevel@tonic-gate static pthread_cond_t initial_milestone_cv = PTHREAD_COND_INITIALIZER;
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /* protected by dgraph_lock */
2260Sstevel@tonic-gate static boolean_t sulogin_thread_running = B_FALSE;
2270Sstevel@tonic-gate static boolean_t sulogin_running = B_FALSE;
2280Sstevel@tonic-gate static boolean_t console_login_ready = B_FALSE;
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate /* Number of services to come down to complete milestone transition. */
2310Sstevel@tonic-gate static uint_t non_subgraph_svcs;
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate /*
2340Sstevel@tonic-gate * These variables indicate what should be done when we reach the milestone
2350Sstevel@tonic-gate * target milestone, i.e., when non_subgraph_svcs == 0. They are acted upon in
2360Sstevel@tonic-gate * dgraph_set_instance_state().
2370Sstevel@tonic-gate */
2380Sstevel@tonic-gate static int halting = -1;
2390Sstevel@tonic-gate static boolean_t go_single_user_mode = B_FALSE;
2400Sstevel@tonic-gate static boolean_t go_to_level1 = B_FALSE;
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate /*
2438944Sdp@eng.sun.com * Tracks when we started halting.
2448944Sdp@eng.sun.com */
2458944Sdp@eng.sun.com static time_t halting_time = 0;
2468944Sdp@eng.sun.com
2478944Sdp@eng.sun.com /*
2480Sstevel@tonic-gate * This tracks the legacy runlevel to ensure we signal init and manage
2490Sstevel@tonic-gate * utmpx entries correctly.
2500Sstevel@tonic-gate */
2510Sstevel@tonic-gate static char current_runlevel = '\0';
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate /* Number of single user threads currently running */
2540Sstevel@tonic-gate static pthread_mutex_t single_user_thread_lock;
2550Sstevel@tonic-gate static int single_user_thread_count = 0;
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate /* Statistics for dependency cycle-checking */
2580Sstevel@tonic-gate static u_longlong_t dep_inserts = 0;
2590Sstevel@tonic-gate static u_longlong_t dep_cycle_ns = 0;
2600Sstevel@tonic-gate static u_longlong_t dep_insert_ns = 0;
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate static const char * const emsg_invalid_restarter =
2641958Slianep "Transitioning %s to maintenance, restarter FMRI %s is invalid "
2651958Slianep "(see 'svcs -xv' for details).\n";
2660Sstevel@tonic-gate static const char * const console_login_fmri = CONSOLE_LOGIN_FMRI;
2670Sstevel@tonic-gate static const char * const single_user_fmri = SCF_MILESTONE_SINGLE_USER;
2680Sstevel@tonic-gate static const char * const multi_user_fmri = SCF_MILESTONE_MULTI_USER;
2690Sstevel@tonic-gate static const char * const multi_user_svr_fmri = SCF_MILESTONE_MULTI_USER_SERVER;
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate * These services define the system being "up". If none of them can come
2740Sstevel@tonic-gate * online, then we will run sulogin on the console. Note that the install ones
2750Sstevel@tonic-gate * are for the miniroot and when installing CDs after the first. can_come_up()
2760Sstevel@tonic-gate * does the decision making, and an sulogin_thread() runs sulogin, which can be
2770Sstevel@tonic-gate * started by dgraph_set_instance_state() or single_user_thread().
2780Sstevel@tonic-gate *
2790Sstevel@tonic-gate * NOTE: can_come_up() relies on SCF_MILESTONE_SINGLE_USER being the first
2800Sstevel@tonic-gate * entry, which is only used when booting_to_single_user (boot -s) is set.
2810Sstevel@tonic-gate * This is because when doing a "boot -s", sulogin is started from specials.c
2820Sstevel@tonic-gate * after milestone/single-user comes online, for backwards compatibility.
2830Sstevel@tonic-gate * In this case, SCF_MILESTONE_SINGLE_USER needs to be part of up_svcs
2840Sstevel@tonic-gate * to ensure sulogin will be spawned if milestone/single-user cannot be reached.
2850Sstevel@tonic-gate */
2860Sstevel@tonic-gate static const char * const up_svcs[] = {
2870Sstevel@tonic-gate SCF_MILESTONE_SINGLE_USER,
2880Sstevel@tonic-gate CONSOLE_LOGIN_FMRI,
2890Sstevel@tonic-gate "svc:/system/install-setup:default",
2900Sstevel@tonic-gate "svc:/system/install:default",
2910Sstevel@tonic-gate NULL
2920Sstevel@tonic-gate };
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate /* This array must have an element for each non-NULL element of up_svcs[]. */
2950Sstevel@tonic-gate static graph_vertex_t *up_svcs_p[] = { NULL, NULL, NULL, NULL };
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate /* These are for seed repository magic. See can_come_up(). */
29811996SThomas.Whitten@Sun.COM static const char * const manifest_import = SCF_INSTANCE_MI;
2990Sstevel@tonic-gate static graph_vertex_t *manifest_import_p = NULL;
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate static char target_milestone_as_runlevel(void);
3030Sstevel@tonic-gate static void graph_runlevel_changed(char rl, int online);
3040Sstevel@tonic-gate static int dgraph_set_milestone(const char *, scf_handle_t *, boolean_t);
3050Sstevel@tonic-gate static boolean_t should_be_in_subgraph(graph_vertex_t *v);
3067630SRenaud.Manus@Sun.COM static int mark_subtree(graph_edge_t *, void *);
3077630SRenaud.Manus@Sun.COM static boolean_t insubtree_dependents_down(graph_vertex_t *);
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate * graph_vertex_compare()
3110Sstevel@tonic-gate * This function can compare either int *id or * graph_vertex_t *gv
3120Sstevel@tonic-gate * values, as the vertex id is always the first element of a
3130Sstevel@tonic-gate * graph_vertex structure.
3140Sstevel@tonic-gate */
3150Sstevel@tonic-gate /* ARGSUSED */
3160Sstevel@tonic-gate static int
graph_vertex_compare(const void * lc_arg,const void * rc_arg,void * private)3170Sstevel@tonic-gate graph_vertex_compare(const void *lc_arg, const void *rc_arg, void *private)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate int lc_id = ((const graph_vertex_t *)lc_arg)->gv_id;
3200Sstevel@tonic-gate int rc_id = *(int *)rc_arg;
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate if (lc_id > rc_id)
3230Sstevel@tonic-gate return (1);
3240Sstevel@tonic-gate if (lc_id < rc_id)
3250Sstevel@tonic-gate return (-1);
3260Sstevel@tonic-gate return (0);
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate void
graph_init()3300Sstevel@tonic-gate graph_init()
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate graph_edge_pool = startd_list_pool_create("graph_edges",
3330Sstevel@tonic-gate sizeof (graph_edge_t), offsetof(graph_edge_t, ge_link), NULL,
3340Sstevel@tonic-gate UU_LIST_POOL_DEBUG);
3350Sstevel@tonic-gate assert(graph_edge_pool != NULL);
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate graph_vertex_pool = startd_list_pool_create("graph_vertices",
3380Sstevel@tonic-gate sizeof (graph_vertex_t), offsetof(graph_vertex_t, gv_link),
3390Sstevel@tonic-gate graph_vertex_compare, UU_LIST_POOL_DEBUG);
3400Sstevel@tonic-gate assert(graph_vertex_pool != NULL);
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate (void) pthread_mutex_init(&dgraph_lock, &mutex_attrs);
3430Sstevel@tonic-gate (void) pthread_mutex_init(&single_user_thread_lock, &mutex_attrs);
3440Sstevel@tonic-gate dgraph = startd_list_create(graph_vertex_pool, NULL, UU_LIST_SORTED);
3450Sstevel@tonic-gate assert(dgraph != NULL);
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate if (!st->st_initial)
3480Sstevel@tonic-gate current_runlevel = utmpx_get_runlevel();
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate log_framework(LOG_DEBUG, "Initialized graph\n");
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate static graph_vertex_t *
vertex_get_by_name(const char * name)3540Sstevel@tonic-gate vertex_get_by_name(const char *name)
3550Sstevel@tonic-gate {
3560Sstevel@tonic-gate int id;
3570Sstevel@tonic-gate
35811466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate id = dict_lookup_byname(name);
3610Sstevel@tonic-gate if (id == -1)
3620Sstevel@tonic-gate return (NULL);
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate return (uu_list_find(dgraph, &id, NULL, NULL));
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate static graph_vertex_t *
vertex_get_by_id(int id)3680Sstevel@tonic-gate vertex_get_by_id(int id)
3690Sstevel@tonic-gate {
37011466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate if (id == -1)
3730Sstevel@tonic-gate return (NULL);
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate return (uu_list_find(dgraph, &id, NULL, NULL));
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate /*
3790Sstevel@tonic-gate * Creates a new vertex with the given name, adds it to the graph, and returns
3800Sstevel@tonic-gate * a pointer to it. The graph lock must be held by this thread on entry.
3810Sstevel@tonic-gate */
3820Sstevel@tonic-gate static graph_vertex_t *
graph_add_vertex(const char * name)3830Sstevel@tonic-gate graph_add_vertex(const char *name)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate int id;
3860Sstevel@tonic-gate graph_vertex_t *v;
3870Sstevel@tonic-gate void *p;
3880Sstevel@tonic-gate uu_list_index_t idx;
3890Sstevel@tonic-gate
39011466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate id = dict_insert(name);
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate v = startd_zalloc(sizeof (*v));
3950Sstevel@tonic-gate
3960Sstevel@tonic-gate v->gv_id = id;
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate v->gv_name = startd_alloc(strlen(name) + 1);
3990Sstevel@tonic-gate (void) strcpy(v->gv_name, name);
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate v->gv_dependencies = startd_list_create(graph_edge_pool, v, 0);
4020Sstevel@tonic-gate v->gv_dependents = startd_list_create(graph_edge_pool, v, 0);
4030Sstevel@tonic-gate
4040Sstevel@tonic-gate p = uu_list_find(dgraph, &id, NULL, &idx);
4050Sstevel@tonic-gate assert(p == NULL);
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate uu_list_node_init(v, &v->gv_link, graph_vertex_pool);
4080Sstevel@tonic-gate uu_list_insert(dgraph, v, idx);
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate return (v);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate * Removes v from the graph and frees it. The graph should be locked by this
4150Sstevel@tonic-gate * thread, and v should have no edges associated with it.
4160Sstevel@tonic-gate */
4170Sstevel@tonic-gate static void
graph_remove_vertex(graph_vertex_t * v)4180Sstevel@tonic-gate graph_remove_vertex(graph_vertex_t *v)
4190Sstevel@tonic-gate {
42011466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
4210Sstevel@tonic-gate
4220Sstevel@tonic-gate assert(uu_list_numnodes(v->gv_dependencies) == 0);
4230Sstevel@tonic-gate assert(uu_list_numnodes(v->gv_dependents) == 0);
4241712Srm88369 assert(v->gv_refs == 0);
4250Sstevel@tonic-gate
4260Sstevel@tonic-gate startd_free(v->gv_name, strlen(v->gv_name) + 1);
4270Sstevel@tonic-gate uu_list_destroy(v->gv_dependencies);
4280Sstevel@tonic-gate uu_list_destroy(v->gv_dependents);
4290Sstevel@tonic-gate uu_list_remove(dgraph, v);
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate startd_free(v, sizeof (graph_vertex_t));
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate static void
graph_add_edge(graph_vertex_t * fv,graph_vertex_t * tv)4350Sstevel@tonic-gate graph_add_edge(graph_vertex_t *fv, graph_vertex_t *tv)
4360Sstevel@tonic-gate {
4370Sstevel@tonic-gate graph_edge_t *e, *re;
4380Sstevel@tonic-gate int r;
4390Sstevel@tonic-gate
44011466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
4410Sstevel@tonic-gate
4420Sstevel@tonic-gate e = startd_alloc(sizeof (graph_edge_t));
4430Sstevel@tonic-gate re = startd_alloc(sizeof (graph_edge_t));
4440Sstevel@tonic-gate
4450Sstevel@tonic-gate e->ge_parent = fv;
4460Sstevel@tonic-gate e->ge_vertex = tv;
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate re->ge_parent = tv;
4490Sstevel@tonic-gate re->ge_vertex = fv;
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate uu_list_node_init(e, &e->ge_link, graph_edge_pool);
4520Sstevel@tonic-gate r = uu_list_insert_before(fv->gv_dependencies, NULL, e);
4530Sstevel@tonic-gate assert(r == 0);
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate uu_list_node_init(re, &re->ge_link, graph_edge_pool);
4560Sstevel@tonic-gate r = uu_list_insert_before(tv->gv_dependents, NULL, re);
4570Sstevel@tonic-gate assert(r == 0);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate
4600Sstevel@tonic-gate static void
graph_remove_edge(graph_vertex_t * v,graph_vertex_t * dv)4610Sstevel@tonic-gate graph_remove_edge(graph_vertex_t *v, graph_vertex_t *dv)
4620Sstevel@tonic-gate {
4630Sstevel@tonic-gate graph_edge_t *e;
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate for (e = uu_list_first(v->gv_dependencies);
4660Sstevel@tonic-gate e != NULL;
4670Sstevel@tonic-gate e = uu_list_next(v->gv_dependencies, e)) {
4680Sstevel@tonic-gate if (e->ge_vertex == dv) {
4690Sstevel@tonic-gate uu_list_remove(v->gv_dependencies, e);
4700Sstevel@tonic-gate startd_free(e, sizeof (graph_edge_t));
4710Sstevel@tonic-gate break;
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate for (e = uu_list_first(dv->gv_dependents);
4760Sstevel@tonic-gate e != NULL;
4770Sstevel@tonic-gate e = uu_list_next(dv->gv_dependents, e)) {
4780Sstevel@tonic-gate if (e->ge_vertex == v) {
4790Sstevel@tonic-gate uu_list_remove(dv->gv_dependents, e);
4800Sstevel@tonic-gate startd_free(e, sizeof (graph_edge_t));
4810Sstevel@tonic-gate break;
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate static void
remove_inst_vertex(graph_vertex_t * v)4871712Srm88369 remove_inst_vertex(graph_vertex_t *v)
4881712Srm88369 {
4891712Srm88369 graph_edge_t *e;
4901712Srm88369 graph_vertex_t *sv;
4911712Srm88369 int i;
4921712Srm88369
49311466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
4941712Srm88369 assert(uu_list_numnodes(v->gv_dependents) == 1);
4951712Srm88369 assert(uu_list_numnodes(v->gv_dependencies) == 0);
4961712Srm88369 assert(v->gv_refs == 0);
4971712Srm88369 assert((v->gv_flags & GV_CONFIGURED) == 0);
4981712Srm88369
4991712Srm88369 e = uu_list_first(v->gv_dependents);
5001712Srm88369 sv = e->ge_vertex;
5011712Srm88369 graph_remove_edge(sv, v);
5021712Srm88369
5031712Srm88369 for (i = 0; up_svcs[i] != NULL; ++i) {
5041712Srm88369 if (up_svcs_p[i] == v)
5051712Srm88369 up_svcs_p[i] = NULL;
5061712Srm88369 }
5071712Srm88369
5081712Srm88369 if (manifest_import_p == v)
5091712Srm88369 manifest_import_p = NULL;
5101712Srm88369
5111712Srm88369 graph_remove_vertex(v);
5121712Srm88369
5131712Srm88369 if (uu_list_numnodes(sv->gv_dependencies) == 0 &&
5141712Srm88369 uu_list_numnodes(sv->gv_dependents) == 0 &&
5151712Srm88369 sv->gv_refs == 0)
5161712Srm88369 graph_remove_vertex(sv);
5171712Srm88369 }
5181712Srm88369
5191712Srm88369 static void
graph_walk_dependents(graph_vertex_t * v,void (* func)(graph_vertex_t *,void *),void * arg)5200Sstevel@tonic-gate graph_walk_dependents(graph_vertex_t *v, void (*func)(graph_vertex_t *, void *),
5210Sstevel@tonic-gate void *arg)
5220Sstevel@tonic-gate {
5230Sstevel@tonic-gate graph_edge_t *e;
5240Sstevel@tonic-gate
5250Sstevel@tonic-gate for (e = uu_list_first(v->gv_dependents);
5260Sstevel@tonic-gate e != NULL;
5270Sstevel@tonic-gate e = uu_list_next(v->gv_dependents, e))
5280Sstevel@tonic-gate func(e->ge_vertex, arg);
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate
5310Sstevel@tonic-gate static void
graph_walk_dependencies(graph_vertex_t * v,void (* func)(graph_vertex_t *,void *),void * arg)5320Sstevel@tonic-gate graph_walk_dependencies(graph_vertex_t *v, void (*func)(graph_vertex_t *,
5330Sstevel@tonic-gate void *), void *arg)
5340Sstevel@tonic-gate {
5350Sstevel@tonic-gate graph_edge_t *e;
5360Sstevel@tonic-gate
53711466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
5380Sstevel@tonic-gate
5390Sstevel@tonic-gate for (e = uu_list_first(v->gv_dependencies);
5400Sstevel@tonic-gate e != NULL;
5410Sstevel@tonic-gate e = uu_list_next(v->gv_dependencies, e)) {
5420Sstevel@tonic-gate
5430Sstevel@tonic-gate func(e->ge_vertex, arg);
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate /*
5480Sstevel@tonic-gate * Generic graph walking function.
5490Sstevel@tonic-gate *
5500Sstevel@tonic-gate * Given a vertex, this function will walk either dependencies
5510Sstevel@tonic-gate * (WALK_DEPENDENCIES) or dependents (WALK_DEPENDENTS) of a vertex recursively
5520Sstevel@tonic-gate * for the entire graph. It will avoid cycles and never visit the same vertex
5530Sstevel@tonic-gate * twice.
5540Sstevel@tonic-gate *
5550Sstevel@tonic-gate * We avoid traversing exclusion dependencies, because they are allowed to
5560Sstevel@tonic-gate * create cycles in the graph. When propagating satisfiability, there is no
5570Sstevel@tonic-gate * need to walk exclusion dependencies because exclude_all_satisfied() doesn't
5580Sstevel@tonic-gate * test for satisfiability.
5590Sstevel@tonic-gate *
5600Sstevel@tonic-gate * The walker takes two callbacks. The first is called before examining the
5610Sstevel@tonic-gate * dependents of each vertex. The second is called on each vertex after
5620Sstevel@tonic-gate * examining its dependents. This allows is_path_to() to construct a path only
5630Sstevel@tonic-gate * after the target vertex has been found.
5640Sstevel@tonic-gate */
5650Sstevel@tonic-gate typedef enum {
5660Sstevel@tonic-gate WALK_DEPENDENTS,
5670Sstevel@tonic-gate WALK_DEPENDENCIES
5680Sstevel@tonic-gate } graph_walk_dir_t;
5690Sstevel@tonic-gate
5700Sstevel@tonic-gate typedef int (*graph_walk_cb_t)(graph_vertex_t *, void *);
5710Sstevel@tonic-gate
5720Sstevel@tonic-gate typedef struct graph_walk_info {
5730Sstevel@tonic-gate graph_walk_dir_t gi_dir;
5740Sstevel@tonic-gate uchar_t *gi_visited; /* vertex bitmap */
5750Sstevel@tonic-gate int (*gi_pre)(graph_vertex_t *, void *);
5760Sstevel@tonic-gate void (*gi_post)(graph_vertex_t *, void *);
5770Sstevel@tonic-gate void *gi_arg; /* callback arg */
5780Sstevel@tonic-gate int gi_ret; /* return value */
5790Sstevel@tonic-gate } graph_walk_info_t;
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate static int
graph_walk_recurse(graph_edge_t * e,graph_walk_info_t * gip)5820Sstevel@tonic-gate graph_walk_recurse(graph_edge_t *e, graph_walk_info_t *gip)
5830Sstevel@tonic-gate {
5840Sstevel@tonic-gate uu_list_t *list;
5850Sstevel@tonic-gate int r;
5860Sstevel@tonic-gate graph_vertex_t *v = e->ge_vertex;
5870Sstevel@tonic-gate int i;
5880Sstevel@tonic-gate uint_t b;
5890Sstevel@tonic-gate
5900Sstevel@tonic-gate i = v->gv_id / 8;
5910Sstevel@tonic-gate b = 1 << (v->gv_id % 8);
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate /*
5940Sstevel@tonic-gate * Check to see if we've visited this vertex already.
5950Sstevel@tonic-gate */
5960Sstevel@tonic-gate if (gip->gi_visited[i] & b)
5970Sstevel@tonic-gate return (UU_WALK_NEXT);
5980Sstevel@tonic-gate
5990Sstevel@tonic-gate gip->gi_visited[i] |= b;
6000Sstevel@tonic-gate
6010Sstevel@tonic-gate /*
6020Sstevel@tonic-gate * Don't follow exclusions.
6030Sstevel@tonic-gate */
6040Sstevel@tonic-gate if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
6050Sstevel@tonic-gate return (UU_WALK_NEXT);
6060Sstevel@tonic-gate
6070Sstevel@tonic-gate /*
6080Sstevel@tonic-gate * Call pre-visit callback. If this doesn't terminate the walk,
6090Sstevel@tonic-gate * continue search.
6100Sstevel@tonic-gate */
6110Sstevel@tonic-gate if ((gip->gi_ret = gip->gi_pre(v, gip->gi_arg)) == UU_WALK_NEXT) {
6120Sstevel@tonic-gate /*
6130Sstevel@tonic-gate * Recurse using appropriate list.
6140Sstevel@tonic-gate */
6150Sstevel@tonic-gate if (gip->gi_dir == WALK_DEPENDENTS)
6160Sstevel@tonic-gate list = v->gv_dependents;
6170Sstevel@tonic-gate else
6180Sstevel@tonic-gate list = v->gv_dependencies;
6190Sstevel@tonic-gate
6200Sstevel@tonic-gate r = uu_list_walk(list, (uu_walk_fn_t *)graph_walk_recurse,
6210Sstevel@tonic-gate gip, 0);
6220Sstevel@tonic-gate assert(r == 0);
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate
6250Sstevel@tonic-gate /*
6260Sstevel@tonic-gate * Callbacks must return either UU_WALK_NEXT or UU_WALK_DONE.
6270Sstevel@tonic-gate */
6280Sstevel@tonic-gate assert(gip->gi_ret == UU_WALK_NEXT || gip->gi_ret == UU_WALK_DONE);
6290Sstevel@tonic-gate
6300Sstevel@tonic-gate /*
6310Sstevel@tonic-gate * If given a post-callback, call the function for every vertex.
6320Sstevel@tonic-gate */
6330Sstevel@tonic-gate if (gip->gi_post != NULL)
6340Sstevel@tonic-gate (void) gip->gi_post(v, gip->gi_arg);
6350Sstevel@tonic-gate
6360Sstevel@tonic-gate /*
6370Sstevel@tonic-gate * Preserve the callback's return value. If the callback returns
6380Sstevel@tonic-gate * UU_WALK_DONE, then we propagate that to the caller in order to
6390Sstevel@tonic-gate * terminate the walk.
6400Sstevel@tonic-gate */
6410Sstevel@tonic-gate return (gip->gi_ret);
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate
6440Sstevel@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)6450Sstevel@tonic-gate graph_walk(graph_vertex_t *v, graph_walk_dir_t dir,
6460Sstevel@tonic-gate int (*pre)(graph_vertex_t *, void *),
6470Sstevel@tonic-gate void (*post)(graph_vertex_t *, void *), void *arg)
6480Sstevel@tonic-gate {
6490Sstevel@tonic-gate graph_walk_info_t gi;
6500Sstevel@tonic-gate graph_edge_t fake;
6510Sstevel@tonic-gate size_t sz = dictionary->dict_new_id / 8 + 1;
6520Sstevel@tonic-gate
6530Sstevel@tonic-gate gi.gi_visited = startd_zalloc(sz);
6540Sstevel@tonic-gate gi.gi_pre = pre;
6550Sstevel@tonic-gate gi.gi_post = post;
6560Sstevel@tonic-gate gi.gi_arg = arg;
6570Sstevel@tonic-gate gi.gi_dir = dir;
6580Sstevel@tonic-gate gi.gi_ret = 0;
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate /*
6610Sstevel@tonic-gate * Fake up an edge for the first iteration
6620Sstevel@tonic-gate */
6630Sstevel@tonic-gate fake.ge_vertex = v;
6640Sstevel@tonic-gate (void) graph_walk_recurse(&fake, &gi);
6650Sstevel@tonic-gate
6660Sstevel@tonic-gate startd_free(gi.gi_visited, sz);
6670Sstevel@tonic-gate }
6680Sstevel@tonic-gate
6690Sstevel@tonic-gate typedef struct child_search {
6700Sstevel@tonic-gate int id; /* id of vertex to look for */
6710Sstevel@tonic-gate uint_t depth; /* recursion depth */
6720Sstevel@tonic-gate /*
6730Sstevel@tonic-gate * While the vertex is not found, path is NULL. After the search, if
6740Sstevel@tonic-gate * the vertex was found then path should point to a -1-terminated
6750Sstevel@tonic-gate * array of vertex id's which constitute the path to the vertex.
6760Sstevel@tonic-gate */
6770Sstevel@tonic-gate int *path;
6780Sstevel@tonic-gate } child_search_t;
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate static int
child_pre(graph_vertex_t * v,void * arg)6810Sstevel@tonic-gate child_pre(graph_vertex_t *v, void *arg)
6820Sstevel@tonic-gate {
6830Sstevel@tonic-gate child_search_t *cs = arg;
6840Sstevel@tonic-gate
6850Sstevel@tonic-gate cs->depth++;
6860Sstevel@tonic-gate
6870Sstevel@tonic-gate if (v->gv_id == cs->id) {
6880Sstevel@tonic-gate cs->path = startd_alloc((cs->depth + 1) * sizeof (int));
6890Sstevel@tonic-gate cs->path[cs->depth] = -1;
6900Sstevel@tonic-gate return (UU_WALK_DONE);
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate
6930Sstevel@tonic-gate return (UU_WALK_NEXT);
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate
6960Sstevel@tonic-gate static void
child_post(graph_vertex_t * v,void * arg)6970Sstevel@tonic-gate child_post(graph_vertex_t *v, void *arg)
6980Sstevel@tonic-gate {
6990Sstevel@tonic-gate child_search_t *cs = arg;
7000Sstevel@tonic-gate
7010Sstevel@tonic-gate cs->depth--;
7020Sstevel@tonic-gate
7030Sstevel@tonic-gate if (cs->path != NULL)
7040Sstevel@tonic-gate cs->path[cs->depth] = v->gv_id;
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate /*
7080Sstevel@tonic-gate * Look for a path from from to to. If one exists, returns a pointer to
7090Sstevel@tonic-gate * a NULL-terminated array of pointers to the vertices along the path. If
7100Sstevel@tonic-gate * there is no path, returns NULL.
7110Sstevel@tonic-gate */
7120Sstevel@tonic-gate static int *
is_path_to(graph_vertex_t * from,graph_vertex_t * to)7130Sstevel@tonic-gate is_path_to(graph_vertex_t *from, graph_vertex_t *to)
7140Sstevel@tonic-gate {
7150Sstevel@tonic-gate child_search_t cs;
7160Sstevel@tonic-gate
7170Sstevel@tonic-gate cs.id = to->gv_id;
7180Sstevel@tonic-gate cs.depth = 0;
7190Sstevel@tonic-gate cs.path = NULL;
7200Sstevel@tonic-gate
7210Sstevel@tonic-gate graph_walk(from, WALK_DEPENDENCIES, child_pre, child_post, &cs);
7220Sstevel@tonic-gate
7230Sstevel@tonic-gate return (cs.path);
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate
7260Sstevel@tonic-gate /*
7270Sstevel@tonic-gate * Given an array of int's as returned by is_path_to, allocates a string of
7280Sstevel@tonic-gate * their names joined by newlines. Returns the size of the allocated buffer
7290Sstevel@tonic-gate * in *sz and frees path.
7300Sstevel@tonic-gate */
7310Sstevel@tonic-gate static void
path_to_str(int * path,char ** cpp,size_t * sz)7320Sstevel@tonic-gate path_to_str(int *path, char **cpp, size_t *sz)
7330Sstevel@tonic-gate {
7340Sstevel@tonic-gate int i;
7350Sstevel@tonic-gate graph_vertex_t *v;
7360Sstevel@tonic-gate size_t allocd, new_allocd;
7370Sstevel@tonic-gate char *new, *name;
7380Sstevel@tonic-gate
73911466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
7400Sstevel@tonic-gate assert(path[0] != -1);
7410Sstevel@tonic-gate
7420Sstevel@tonic-gate allocd = 1;
7430Sstevel@tonic-gate *cpp = startd_alloc(1);
7440Sstevel@tonic-gate (*cpp)[0] = '\0';
7450Sstevel@tonic-gate
7460Sstevel@tonic-gate for (i = 0; path[i] != -1; ++i) {
7470Sstevel@tonic-gate name = NULL;
7480Sstevel@tonic-gate
7490Sstevel@tonic-gate v = vertex_get_by_id(path[i]);
7500Sstevel@tonic-gate
7510Sstevel@tonic-gate if (v == NULL)
7520Sstevel@tonic-gate name = "<deleted>";
7530Sstevel@tonic-gate else if (v->gv_type == GVT_INST || v->gv_type == GVT_SVC)
7540Sstevel@tonic-gate name = v->gv_name;
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate if (name != NULL) {
7570Sstevel@tonic-gate new_allocd = allocd + strlen(name) + 1;
7580Sstevel@tonic-gate new = startd_alloc(new_allocd);
7590Sstevel@tonic-gate (void) strcpy(new, *cpp);
7600Sstevel@tonic-gate (void) strcat(new, name);
7610Sstevel@tonic-gate (void) strcat(new, "\n");
7620Sstevel@tonic-gate
7630Sstevel@tonic-gate startd_free(*cpp, allocd);
7640Sstevel@tonic-gate
7650Sstevel@tonic-gate *cpp = new;
7660Sstevel@tonic-gate allocd = new_allocd;
7670Sstevel@tonic-gate }
7680Sstevel@tonic-gate }
7690Sstevel@tonic-gate
7700Sstevel@tonic-gate startd_free(path, sizeof (int) * (i + 1));
7710Sstevel@tonic-gate
7720Sstevel@tonic-gate *sz = allocd;
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate
7750Sstevel@tonic-gate
7760Sstevel@tonic-gate /*
7770Sstevel@tonic-gate * This function along with run_sulogin() implements an exclusion relationship
7780Sstevel@tonic-gate * between system/console-login and sulogin. run_sulogin() will fail if
7790Sstevel@tonic-gate * system/console-login is online, and the graph engine should call
7800Sstevel@tonic-gate * graph_clogin_start() to bring system/console-login online, which defers the
7810Sstevel@tonic-gate * start if sulogin is running.
7820Sstevel@tonic-gate */
7830Sstevel@tonic-gate static void
graph_clogin_start(graph_vertex_t * v)7840Sstevel@tonic-gate graph_clogin_start(graph_vertex_t *v)
7850Sstevel@tonic-gate {
78611466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
7870Sstevel@tonic-gate
7880Sstevel@tonic-gate if (sulogin_running)
7890Sstevel@tonic-gate console_login_ready = B_TRUE;
7900Sstevel@tonic-gate else
7910Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate
7940Sstevel@tonic-gate static void
graph_su_start(graph_vertex_t * v)7950Sstevel@tonic-gate graph_su_start(graph_vertex_t *v)
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate /*
7980Sstevel@tonic-gate * /etc/inittab used to have the initial /sbin/rcS as a 'sysinit'
7990Sstevel@tonic-gate * entry with a runlevel of 'S', before jumping to the final
8000Sstevel@tonic-gate * target runlevel (as set in initdefault). We mimic that legacy
8010Sstevel@tonic-gate * behavior here.
8020Sstevel@tonic-gate */
8030Sstevel@tonic-gate utmpx_set_runlevel('S', '0', B_FALSE);
8040Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
8050Sstevel@tonic-gate }
8060Sstevel@tonic-gate
8070Sstevel@tonic-gate static void
graph_post_su_online(void)8080Sstevel@tonic-gate graph_post_su_online(void)
8090Sstevel@tonic-gate {
8100Sstevel@tonic-gate graph_runlevel_changed('S', 1);
8110Sstevel@tonic-gate }
8120Sstevel@tonic-gate
8130Sstevel@tonic-gate static void
graph_post_su_disable(void)8140Sstevel@tonic-gate graph_post_su_disable(void)
8150Sstevel@tonic-gate {
8160Sstevel@tonic-gate graph_runlevel_changed('S', 0);
8170Sstevel@tonic-gate }
8180Sstevel@tonic-gate
8190Sstevel@tonic-gate static void
graph_post_mu_online(void)8200Sstevel@tonic-gate graph_post_mu_online(void)
8210Sstevel@tonic-gate {
8220Sstevel@tonic-gate graph_runlevel_changed('2', 1);
8230Sstevel@tonic-gate }
8240Sstevel@tonic-gate
8250Sstevel@tonic-gate static void
graph_post_mu_disable(void)8260Sstevel@tonic-gate graph_post_mu_disable(void)
8270Sstevel@tonic-gate {
8280Sstevel@tonic-gate graph_runlevel_changed('2', 0);
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate
8310Sstevel@tonic-gate static void
graph_post_mus_online(void)8320Sstevel@tonic-gate graph_post_mus_online(void)
8330Sstevel@tonic-gate {
8340Sstevel@tonic-gate graph_runlevel_changed('3', 1);
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate
8370Sstevel@tonic-gate static void
graph_post_mus_disable(void)8380Sstevel@tonic-gate graph_post_mus_disable(void)
8390Sstevel@tonic-gate {
8400Sstevel@tonic-gate graph_runlevel_changed('3', 0);
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate
8430Sstevel@tonic-gate static struct special_vertex_info {
8440Sstevel@tonic-gate const char *name;
8450Sstevel@tonic-gate void (*start_f)(graph_vertex_t *);
8460Sstevel@tonic-gate void (*post_online_f)(void);
8470Sstevel@tonic-gate void (*post_disable_f)(void);
8480Sstevel@tonic-gate } special_vertices[] = {
8490Sstevel@tonic-gate { CONSOLE_LOGIN_FMRI, graph_clogin_start, NULL, NULL },
8500Sstevel@tonic-gate { SCF_MILESTONE_SINGLE_USER, graph_su_start,
8510Sstevel@tonic-gate graph_post_su_online, graph_post_su_disable },
8520Sstevel@tonic-gate { SCF_MILESTONE_MULTI_USER, NULL,
8530Sstevel@tonic-gate graph_post_mu_online, graph_post_mu_disable },
8540Sstevel@tonic-gate { SCF_MILESTONE_MULTI_USER_SERVER, NULL,
8550Sstevel@tonic-gate graph_post_mus_online, graph_post_mus_disable },
8560Sstevel@tonic-gate { NULL },
8570Sstevel@tonic-gate };
8580Sstevel@tonic-gate
8590Sstevel@tonic-gate
8600Sstevel@tonic-gate void
vertex_send_event(graph_vertex_t * v,restarter_event_type_t e)8610Sstevel@tonic-gate vertex_send_event(graph_vertex_t *v, restarter_event_type_t e)
8620Sstevel@tonic-gate {
8630Sstevel@tonic-gate switch (e) {
8640Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADD_INSTANCE:
8650Sstevel@tonic-gate assert(v->gv_state == RESTARTER_STATE_UNINIT);
8660Sstevel@tonic-gate
8670Sstevel@tonic-gate MUTEX_LOCK(&st->st_load_lock);
8680Sstevel@tonic-gate st->st_load_instances++;
8690Sstevel@tonic-gate MUTEX_UNLOCK(&st->st_load_lock);
8700Sstevel@tonic-gate break;
8710Sstevel@tonic-gate
8720Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ENABLE:
8730Sstevel@tonic-gate log_framework(LOG_DEBUG, "Enabling %s.\n", v->gv_name);
8740Sstevel@tonic-gate assert(v->gv_state == RESTARTER_STATE_UNINIT ||
8750Sstevel@tonic-gate v->gv_state == RESTARTER_STATE_DISABLED ||
8760Sstevel@tonic-gate v->gv_state == RESTARTER_STATE_MAINT);
8770Sstevel@tonic-gate break;
8780Sstevel@tonic-gate
8790Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DISABLE:
8800Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
8810Sstevel@tonic-gate log_framework(LOG_DEBUG, "Disabling %s.\n", v->gv_name);
8820Sstevel@tonic-gate assert(v->gv_state != RESTARTER_STATE_DISABLED);
8830Sstevel@tonic-gate break;
8840Sstevel@tonic-gate
88511482SSean.Wilcox@Sun.COM case RESTARTER_EVENT_TYPE_STOP_RESET:
8860Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_STOP:
8870Sstevel@tonic-gate log_framework(LOG_DEBUG, "Stopping %s.\n", v->gv_name);
8880Sstevel@tonic-gate assert(v->gv_state == RESTARTER_STATE_DEGRADED ||
8890Sstevel@tonic-gate v->gv_state == RESTARTER_STATE_ONLINE);
8900Sstevel@tonic-gate break;
8910Sstevel@tonic-gate
8920Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_START:
8930Sstevel@tonic-gate log_framework(LOG_DEBUG, "Starting %s.\n", v->gv_name);
8940Sstevel@tonic-gate assert(v->gv_state == RESTARTER_STATE_OFFLINE);
8950Sstevel@tonic-gate break;
8960Sstevel@tonic-gate
8970Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE:
8980Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_DEGRADED:
8990Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_REFRESH:
9000Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_RESTART:
9010Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF:
9020Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
9030Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE:
9040Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
9050Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
9060Sstevel@tonic-gate break;
9070Sstevel@tonic-gate
9080Sstevel@tonic-gate default:
9090Sstevel@tonic-gate #ifndef NDEBUG
9100Sstevel@tonic-gate uu_warn("%s:%d: Bad event %d.\n", __FILE__, __LINE__, e);
9110Sstevel@tonic-gate #endif
9120Sstevel@tonic-gate abort();
9130Sstevel@tonic-gate }
9140Sstevel@tonic-gate
915*12967Sgavin.maltby@oracle.com restarter_protocol_send_event(v->gv_name, v->gv_restarter_channel, e,
916*12967Sgavin.maltby@oracle.com v->gv_reason);
9170Sstevel@tonic-gate }
9180Sstevel@tonic-gate
9190Sstevel@tonic-gate static void
graph_unset_restarter(graph_vertex_t * v)9200Sstevel@tonic-gate graph_unset_restarter(graph_vertex_t *v)
9210Sstevel@tonic-gate {
92211466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
9230Sstevel@tonic-gate assert(v->gv_flags & GV_CONFIGURED);
9240Sstevel@tonic-gate
9250Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_REMOVE_INSTANCE);
9260Sstevel@tonic-gate
9270Sstevel@tonic-gate if (v->gv_restarter_id != -1) {
9280Sstevel@tonic-gate graph_vertex_t *rv;
9290Sstevel@tonic-gate
9300Sstevel@tonic-gate rv = vertex_get_by_id(v->gv_restarter_id);
9310Sstevel@tonic-gate graph_remove_edge(v, rv);
9320Sstevel@tonic-gate }
9330Sstevel@tonic-gate
9340Sstevel@tonic-gate v->gv_restarter_id = -1;
9350Sstevel@tonic-gate v->gv_restarter_channel = NULL;
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate
9381712Srm88369 /*
9391712Srm88369 * Return VERTEX_REMOVED when the vertex passed in argument is deleted from the
9401712Srm88369 * dgraph otherwise return VERTEX_INUSE.
9411712Srm88369 */
9421712Srm88369 static int
free_if_unrefed(graph_vertex_t * v)9431712Srm88369 free_if_unrefed(graph_vertex_t *v)
9441712Srm88369 {
94511466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
9461712Srm88369
9471712Srm88369 if (v->gv_refs > 0)
9481712Srm88369 return (VERTEX_INUSE);
9491712Srm88369
9501712Srm88369 if (v->gv_type == GVT_SVC &&
9511712Srm88369 uu_list_numnodes(v->gv_dependents) == 0 &&
9521712Srm88369 uu_list_numnodes(v->gv_dependencies) == 0) {
9531712Srm88369 graph_remove_vertex(v);
9541712Srm88369 return (VERTEX_REMOVED);
9551712Srm88369 } else if (v->gv_type == GVT_INST &&
9561712Srm88369 (v->gv_flags & GV_CONFIGURED) == 0 &&
9571712Srm88369 uu_list_numnodes(v->gv_dependents) == 1 &&
9581712Srm88369 uu_list_numnodes(v->gv_dependencies) == 0) {
9591712Srm88369 remove_inst_vertex(v);
9601712Srm88369 return (VERTEX_REMOVED);
9611712Srm88369 }
9621712Srm88369
9631712Srm88369 return (VERTEX_INUSE);
9641712Srm88369 }
9651712Srm88369
9660Sstevel@tonic-gate static void
delete_depgroup(graph_vertex_t * v)9670Sstevel@tonic-gate delete_depgroup(graph_vertex_t *v)
9680Sstevel@tonic-gate {
9690Sstevel@tonic-gate graph_edge_t *e;
9700Sstevel@tonic-gate graph_vertex_t *dv;
9710Sstevel@tonic-gate
97211466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
9730Sstevel@tonic-gate assert(v->gv_type == GVT_GROUP);
9740Sstevel@tonic-gate assert(uu_list_numnodes(v->gv_dependents) == 0);
9750Sstevel@tonic-gate
9760Sstevel@tonic-gate while ((e = uu_list_first(v->gv_dependencies)) != NULL) {
9770Sstevel@tonic-gate dv = e->ge_vertex;
9780Sstevel@tonic-gate
9790Sstevel@tonic-gate graph_remove_edge(v, dv);
9800Sstevel@tonic-gate
9810Sstevel@tonic-gate switch (dv->gv_type) {
9820Sstevel@tonic-gate case GVT_INST: /* instance dependency */
9830Sstevel@tonic-gate case GVT_SVC: /* service dependency */
9841712Srm88369 (void) free_if_unrefed(dv);
9850Sstevel@tonic-gate break;
9860Sstevel@tonic-gate
9870Sstevel@tonic-gate case GVT_FILE: /* file dependency */
9880Sstevel@tonic-gate assert(uu_list_numnodes(dv->gv_dependencies) == 0);
9890Sstevel@tonic-gate if (uu_list_numnodes(dv->gv_dependents) == 0)
9900Sstevel@tonic-gate graph_remove_vertex(dv);
9910Sstevel@tonic-gate break;
9920Sstevel@tonic-gate
9930Sstevel@tonic-gate default:
9940Sstevel@tonic-gate #ifndef NDEBUG
9950Sstevel@tonic-gate uu_warn("%s:%d: Unexpected node type %d", __FILE__,
9960Sstevel@tonic-gate __LINE__, dv->gv_type);
9970Sstevel@tonic-gate #endif
9980Sstevel@tonic-gate abort();
9990Sstevel@tonic-gate }
10000Sstevel@tonic-gate }
10010Sstevel@tonic-gate
10020Sstevel@tonic-gate graph_remove_vertex(v);
10030Sstevel@tonic-gate }
10040Sstevel@tonic-gate
10050Sstevel@tonic-gate static int
delete_instance_deps_cb(graph_edge_t * e,void ** ptrs)10060Sstevel@tonic-gate delete_instance_deps_cb(graph_edge_t *e, void **ptrs)
10070Sstevel@tonic-gate {
10080Sstevel@tonic-gate graph_vertex_t *v = ptrs[0];
10090Sstevel@tonic-gate boolean_t delete_restarter_dep = (boolean_t)ptrs[1];
10100Sstevel@tonic-gate graph_vertex_t *dv;
10110Sstevel@tonic-gate
10120Sstevel@tonic-gate dv = e->ge_vertex;
10130Sstevel@tonic-gate
10140Sstevel@tonic-gate /*
10150Sstevel@tonic-gate * We have four possibilities here:
10160Sstevel@tonic-gate * - GVT_INST: restarter
10170Sstevel@tonic-gate * - GVT_GROUP - GVT_INST: instance dependency
10180Sstevel@tonic-gate * - GVT_GROUP - GVT_SVC - GV_INST: service dependency
10190Sstevel@tonic-gate * - GVT_GROUP - GVT_FILE: file dependency
10200Sstevel@tonic-gate */
10210Sstevel@tonic-gate switch (dv->gv_type) {
10220Sstevel@tonic-gate case GVT_INST: /* restarter */
10230Sstevel@tonic-gate assert(dv->gv_id == v->gv_restarter_id);
10240Sstevel@tonic-gate if (delete_restarter_dep)
10250Sstevel@tonic-gate graph_remove_edge(v, dv);
10260Sstevel@tonic-gate break;
10270Sstevel@tonic-gate
10280Sstevel@tonic-gate case GVT_GROUP: /* pg dependency */
10290Sstevel@tonic-gate graph_remove_edge(v, dv);
10300Sstevel@tonic-gate delete_depgroup(dv);
10310Sstevel@tonic-gate break;
10320Sstevel@tonic-gate
10330Sstevel@tonic-gate case GVT_FILE:
10340Sstevel@tonic-gate /* These are currently not direct dependencies */
10350Sstevel@tonic-gate
10360Sstevel@tonic-gate default:
10370Sstevel@tonic-gate #ifndef NDEBUG
10380Sstevel@tonic-gate uu_warn("%s:%d: Bad vertex type %d.\n", __FILE__, __LINE__,
10390Sstevel@tonic-gate dv->gv_type);
10400Sstevel@tonic-gate #endif
10410Sstevel@tonic-gate abort();
10420Sstevel@tonic-gate }
10430Sstevel@tonic-gate
10440Sstevel@tonic-gate return (UU_WALK_NEXT);
10450Sstevel@tonic-gate }
10460Sstevel@tonic-gate
10470Sstevel@tonic-gate static void
delete_instance_dependencies(graph_vertex_t * v,boolean_t delete_restarter_dep)10480Sstevel@tonic-gate delete_instance_dependencies(graph_vertex_t *v, boolean_t delete_restarter_dep)
10490Sstevel@tonic-gate {
10500Sstevel@tonic-gate void *ptrs[2];
10510Sstevel@tonic-gate int r;
10520Sstevel@tonic-gate
105311466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
10540Sstevel@tonic-gate assert(v->gv_type == GVT_INST);
10550Sstevel@tonic-gate
10560Sstevel@tonic-gate ptrs[0] = v;
10570Sstevel@tonic-gate ptrs[1] = (void *)delete_restarter_dep;
10580Sstevel@tonic-gate
10590Sstevel@tonic-gate r = uu_list_walk(v->gv_dependencies,
10600Sstevel@tonic-gate (uu_walk_fn_t *)delete_instance_deps_cb, &ptrs, UU_WALK_ROBUST);
10610Sstevel@tonic-gate assert(r == 0);
10620Sstevel@tonic-gate }
10630Sstevel@tonic-gate
10640Sstevel@tonic-gate /*
10650Sstevel@tonic-gate * int graph_insert_vertex_unconfigured()
10660Sstevel@tonic-gate * Insert a vertex without sending any restarter events. If the vertex
10670Sstevel@tonic-gate * already exists or creation is successful, return a pointer to it in *vp.
10680Sstevel@tonic-gate *
10690Sstevel@tonic-gate * If type is not GVT_GROUP, dt can remain unset.
10700Sstevel@tonic-gate *
10710Sstevel@tonic-gate * Returns 0, EEXIST, or EINVAL if the arguments are invalid (i.e., fmri
10720Sstevel@tonic-gate * doesn't agree with type, or type doesn't agree with dt).
10730Sstevel@tonic-gate */
10740Sstevel@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)10750Sstevel@tonic-gate graph_insert_vertex_unconfigured(const char *fmri, gv_type_t type,
10760Sstevel@tonic-gate depgroup_type_t dt, restarter_error_t rt, graph_vertex_t **vp)
10770Sstevel@tonic-gate {
10780Sstevel@tonic-gate int r;
10790Sstevel@tonic-gate int i;
10800Sstevel@tonic-gate
108111466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
10820Sstevel@tonic-gate
10830Sstevel@tonic-gate switch (type) {
10840Sstevel@tonic-gate case GVT_SVC:
10850Sstevel@tonic-gate case GVT_INST:
10860Sstevel@tonic-gate if (strncmp(fmri, "svc:", sizeof ("svc:") - 1) != 0)
10870Sstevel@tonic-gate return (EINVAL);
10880Sstevel@tonic-gate break;
10890Sstevel@tonic-gate
10900Sstevel@tonic-gate case GVT_FILE:
10910Sstevel@tonic-gate if (strncmp(fmri, "file:", sizeof ("file:") - 1) != 0)
10920Sstevel@tonic-gate return (EINVAL);
10930Sstevel@tonic-gate break;
10940Sstevel@tonic-gate
10950Sstevel@tonic-gate case GVT_GROUP:
10960Sstevel@tonic-gate if (dt <= 0 || rt < 0)
10970Sstevel@tonic-gate return (EINVAL);
10980Sstevel@tonic-gate break;
10990Sstevel@tonic-gate
11000Sstevel@tonic-gate default:
11010Sstevel@tonic-gate #ifndef NDEBUG
11020Sstevel@tonic-gate uu_warn("%s:%d: Unknown type %d.\n", __FILE__, __LINE__, type);
11030Sstevel@tonic-gate #endif
11040Sstevel@tonic-gate abort();
11050Sstevel@tonic-gate }
11060Sstevel@tonic-gate
11070Sstevel@tonic-gate *vp = vertex_get_by_name(fmri);
11080Sstevel@tonic-gate if (*vp != NULL)
11090Sstevel@tonic-gate return (EEXIST);
11100Sstevel@tonic-gate
11110Sstevel@tonic-gate *vp = graph_add_vertex(fmri);
11120Sstevel@tonic-gate
11130Sstevel@tonic-gate (*vp)->gv_type = type;
11140Sstevel@tonic-gate (*vp)->gv_depgroup = dt;
11150Sstevel@tonic-gate (*vp)->gv_restart = rt;
11160Sstevel@tonic-gate
11170Sstevel@tonic-gate (*vp)->gv_flags = 0;
11180Sstevel@tonic-gate (*vp)->gv_state = RESTARTER_STATE_NONE;
11190Sstevel@tonic-gate
11200Sstevel@tonic-gate for (i = 0; special_vertices[i].name != NULL; ++i) {
11210Sstevel@tonic-gate if (strcmp(fmri, special_vertices[i].name) == 0) {
11220Sstevel@tonic-gate (*vp)->gv_start_f = special_vertices[i].start_f;
11230Sstevel@tonic-gate (*vp)->gv_post_online_f =
11240Sstevel@tonic-gate special_vertices[i].post_online_f;
11250Sstevel@tonic-gate (*vp)->gv_post_disable_f =
11260Sstevel@tonic-gate special_vertices[i].post_disable_f;
11270Sstevel@tonic-gate break;
11280Sstevel@tonic-gate }
11290Sstevel@tonic-gate }
11300Sstevel@tonic-gate
11310Sstevel@tonic-gate (*vp)->gv_restarter_id = -1;
11320Sstevel@tonic-gate (*vp)->gv_restarter_channel = 0;
11330Sstevel@tonic-gate
11340Sstevel@tonic-gate if (type == GVT_INST) {
11350Sstevel@tonic-gate char *sfmri;
11360Sstevel@tonic-gate graph_vertex_t *sv;
11370Sstevel@tonic-gate
11380Sstevel@tonic-gate sfmri = inst_fmri_to_svc_fmri(fmri);
11390Sstevel@tonic-gate sv = vertex_get_by_name(sfmri);
11400Sstevel@tonic-gate if (sv == NULL) {
11410Sstevel@tonic-gate r = graph_insert_vertex_unconfigured(sfmri, GVT_SVC, 0,
11420Sstevel@tonic-gate 0, &sv);
11430Sstevel@tonic-gate assert(r == 0);
11440Sstevel@tonic-gate }
11450Sstevel@tonic-gate startd_free(sfmri, max_scf_fmri_size);
11460Sstevel@tonic-gate
11470Sstevel@tonic-gate graph_add_edge(sv, *vp);
11480Sstevel@tonic-gate }
11490Sstevel@tonic-gate
11500Sstevel@tonic-gate /*
11510Sstevel@tonic-gate * If this vertex is in the subgraph, mark it as so, for both
11520Sstevel@tonic-gate * GVT_INST and GVT_SERVICE verteces.
11530Sstevel@tonic-gate * A GVT_SERVICE vertex can only be in the subgraph if another instance
11540Sstevel@tonic-gate * depends on it, in which case it's already been added to the graph
11550Sstevel@tonic-gate * and marked as in the subgraph (by refresh_vertex()). If a
11560Sstevel@tonic-gate * GVT_SERVICE vertex was freshly added (by the code above), it means
11570Sstevel@tonic-gate * that it has no dependents, and cannot be in the subgraph.
11580Sstevel@tonic-gate * Regardless of this, we still check that gv_flags includes
11590Sstevel@tonic-gate * GV_INSUBGRAPH in the event that future behavior causes the above
11600Sstevel@tonic-gate * code to add a GVT_SERVICE vertex which should be in the subgraph.
11610Sstevel@tonic-gate */
11620Sstevel@tonic-gate
11630Sstevel@tonic-gate (*vp)->gv_flags |= (should_be_in_subgraph(*vp)? GV_INSUBGRAPH : 0);
11640Sstevel@tonic-gate
11650Sstevel@tonic-gate return (0);
11660Sstevel@tonic-gate }
11670Sstevel@tonic-gate
11680Sstevel@tonic-gate /*
11690Sstevel@tonic-gate * Returns 0 on success or ELOOP if the dependency would create a cycle.
11700Sstevel@tonic-gate */
11710Sstevel@tonic-gate static int
graph_insert_dependency(graph_vertex_t * fv,graph_vertex_t * tv,int ** pathp)11720Sstevel@tonic-gate graph_insert_dependency(graph_vertex_t *fv, graph_vertex_t *tv, int **pathp)
11730Sstevel@tonic-gate {
11740Sstevel@tonic-gate hrtime_t now;
11750Sstevel@tonic-gate
117611466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
11770Sstevel@tonic-gate
11780Sstevel@tonic-gate /* cycle detection */
11790Sstevel@tonic-gate now = gethrtime();
11800Sstevel@tonic-gate
11810Sstevel@tonic-gate /* Don't follow exclusions. */
11820Sstevel@tonic-gate if (!(fv->gv_type == GVT_GROUP &&
11830Sstevel@tonic-gate fv->gv_depgroup == DEPGRP_EXCLUDE_ALL)) {
11840Sstevel@tonic-gate *pathp = is_path_to(tv, fv);
11850Sstevel@tonic-gate if (*pathp)
11860Sstevel@tonic-gate return (ELOOP);
11870Sstevel@tonic-gate }
11880Sstevel@tonic-gate
11890Sstevel@tonic-gate dep_cycle_ns += gethrtime() - now;
11900Sstevel@tonic-gate ++dep_inserts;
11910Sstevel@tonic-gate now = gethrtime();
11920Sstevel@tonic-gate
11930Sstevel@tonic-gate graph_add_edge(fv, tv);
11940Sstevel@tonic-gate
11950Sstevel@tonic-gate dep_insert_ns += gethrtime() - now;
11960Sstevel@tonic-gate
11970Sstevel@tonic-gate /* Check if the dependency adds the "to" vertex to the subgraph */
11980Sstevel@tonic-gate tv->gv_flags |= (should_be_in_subgraph(tv) ? GV_INSUBGRAPH : 0);
11990Sstevel@tonic-gate
12000Sstevel@tonic-gate return (0);
12010Sstevel@tonic-gate }
12020Sstevel@tonic-gate
12030Sstevel@tonic-gate static int
inst_running(graph_vertex_t * v)12040Sstevel@tonic-gate inst_running(graph_vertex_t *v)
12050Sstevel@tonic-gate {
12060Sstevel@tonic-gate assert(v->gv_type == GVT_INST);
12070Sstevel@tonic-gate
12080Sstevel@tonic-gate if (v->gv_state == RESTARTER_STATE_ONLINE ||
12090Sstevel@tonic-gate v->gv_state == RESTARTER_STATE_DEGRADED)
12100Sstevel@tonic-gate return (1);
12110Sstevel@tonic-gate
12120Sstevel@tonic-gate return (0);
12130Sstevel@tonic-gate }
12140Sstevel@tonic-gate
12150Sstevel@tonic-gate /*
12160Sstevel@tonic-gate * The dependency evaluation functions return
12170Sstevel@tonic-gate * 1 - dependency satisfied
12180Sstevel@tonic-gate * 0 - dependency unsatisfied
12190Sstevel@tonic-gate * -1 - dependency unsatisfiable (without administrator intervention)
12200Sstevel@tonic-gate *
12210Sstevel@tonic-gate * The functions also take a boolean satbility argument. When true, the
12220Sstevel@tonic-gate * functions may recurse in order to determine satisfiability.
12230Sstevel@tonic-gate */
12240Sstevel@tonic-gate static int require_any_satisfied(graph_vertex_t *, boolean_t);
12250Sstevel@tonic-gate static int dependency_satisfied(graph_vertex_t *, boolean_t);
12260Sstevel@tonic-gate
12270Sstevel@tonic-gate /*
12280Sstevel@tonic-gate * A require_all dependency is unsatisfied if any elements are unsatisfied. It
12290Sstevel@tonic-gate * is unsatisfiable if any elements are unsatisfiable.
12300Sstevel@tonic-gate */
12310Sstevel@tonic-gate static int
require_all_satisfied(graph_vertex_t * groupv,boolean_t satbility)12320Sstevel@tonic-gate require_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
12330Sstevel@tonic-gate {
12340Sstevel@tonic-gate graph_edge_t *edge;
12350Sstevel@tonic-gate int i;
12360Sstevel@tonic-gate boolean_t any_unsatisfied;
12370Sstevel@tonic-gate
12380Sstevel@tonic-gate if (uu_list_numnodes(groupv->gv_dependencies) == 0)
12390Sstevel@tonic-gate return (1);
12400Sstevel@tonic-gate
12410Sstevel@tonic-gate any_unsatisfied = B_FALSE;
12420Sstevel@tonic-gate
12430Sstevel@tonic-gate for (edge = uu_list_first(groupv->gv_dependencies);
12440Sstevel@tonic-gate edge != NULL;
12450Sstevel@tonic-gate edge = uu_list_next(groupv->gv_dependencies, edge)) {
12460Sstevel@tonic-gate i = dependency_satisfied(edge->ge_vertex, satbility);
12470Sstevel@tonic-gate if (i == 1)
12480Sstevel@tonic-gate continue;
12490Sstevel@tonic-gate
12505680Srm88369 log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
12510Sstevel@tonic-gate "require_all(%s): %s is unsatisfi%s.\n", groupv->gv_name,
12520Sstevel@tonic-gate edge->ge_vertex->gv_name, i == 0 ? "ed" : "able");
12530Sstevel@tonic-gate
12540Sstevel@tonic-gate if (!satbility)
12550Sstevel@tonic-gate return (0);
12560Sstevel@tonic-gate
12570Sstevel@tonic-gate if (i == -1)
12580Sstevel@tonic-gate return (-1);
12590Sstevel@tonic-gate
12600Sstevel@tonic-gate any_unsatisfied = B_TRUE;
12610Sstevel@tonic-gate }
12620Sstevel@tonic-gate
12630Sstevel@tonic-gate return (any_unsatisfied ? 0 : 1);
12640Sstevel@tonic-gate }
12650Sstevel@tonic-gate
12660Sstevel@tonic-gate /*
12670Sstevel@tonic-gate * A require_any dependency is satisfied if any element is satisfied. It is
12680Sstevel@tonic-gate * satisfiable if any element is satisfiable.
12690Sstevel@tonic-gate */
12700Sstevel@tonic-gate static int
require_any_satisfied(graph_vertex_t * groupv,boolean_t satbility)12710Sstevel@tonic-gate require_any_satisfied(graph_vertex_t *groupv, boolean_t satbility)
12720Sstevel@tonic-gate {
12730Sstevel@tonic-gate graph_edge_t *edge;
12740Sstevel@tonic-gate int s;
12750Sstevel@tonic-gate boolean_t satisfiable;
12760Sstevel@tonic-gate
12770Sstevel@tonic-gate if (uu_list_numnodes(groupv->gv_dependencies) == 0)
12780Sstevel@tonic-gate return (1);
12790Sstevel@tonic-gate
12800Sstevel@tonic-gate satisfiable = B_FALSE;
12810Sstevel@tonic-gate
12820Sstevel@tonic-gate for (edge = uu_list_first(groupv->gv_dependencies);
12830Sstevel@tonic-gate edge != NULL;
12840Sstevel@tonic-gate edge = uu_list_next(groupv->gv_dependencies, edge)) {
12850Sstevel@tonic-gate s = dependency_satisfied(edge->ge_vertex, satbility);
12860Sstevel@tonic-gate
12870Sstevel@tonic-gate if (s == 1)
12880Sstevel@tonic-gate return (1);
12890Sstevel@tonic-gate
12905680Srm88369 log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
12910Sstevel@tonic-gate "require_any(%s): %s is unsatisfi%s.\n",
12920Sstevel@tonic-gate groupv->gv_name, edge->ge_vertex->gv_name,
12930Sstevel@tonic-gate s == 0 ? "ed" : "able");
12940Sstevel@tonic-gate
12950Sstevel@tonic-gate if (satbility && s == 0)
12960Sstevel@tonic-gate satisfiable = B_TRUE;
12970Sstevel@tonic-gate }
12980Sstevel@tonic-gate
12990Sstevel@tonic-gate return (!satbility || satisfiable ? 0 : -1);
13000Sstevel@tonic-gate }
13010Sstevel@tonic-gate
13020Sstevel@tonic-gate /*
13030Sstevel@tonic-gate * An optional_all dependency only considers elements which are configured,
13040Sstevel@tonic-gate * enabled, and not in maintenance. If any are unsatisfied, then the dependency
13050Sstevel@tonic-gate * is unsatisfied.
13060Sstevel@tonic-gate *
13070Sstevel@tonic-gate * Offline dependencies which are waiting for a dependency to come online are
13080Sstevel@tonic-gate * unsatisfied. Offline dependences which cannot possibly come online
13090Sstevel@tonic-gate * (unsatisfiable) are always considered satisfied.
13100Sstevel@tonic-gate */
13110Sstevel@tonic-gate static int
optional_all_satisfied(graph_vertex_t * groupv,boolean_t satbility)13120Sstevel@tonic-gate optional_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
13130Sstevel@tonic-gate {
13140Sstevel@tonic-gate graph_edge_t *edge;
13150Sstevel@tonic-gate graph_vertex_t *v;
13160Sstevel@tonic-gate boolean_t any_qualified;
13170Sstevel@tonic-gate boolean_t any_unsatisfied;
13180Sstevel@tonic-gate int i;
13190Sstevel@tonic-gate
13200Sstevel@tonic-gate any_qualified = B_FALSE;
13210Sstevel@tonic-gate any_unsatisfied = B_FALSE;
13220Sstevel@tonic-gate
13230Sstevel@tonic-gate for (edge = uu_list_first(groupv->gv_dependencies);
13240Sstevel@tonic-gate edge != NULL;
13250Sstevel@tonic-gate edge = uu_list_next(groupv->gv_dependencies, edge)) {
13260Sstevel@tonic-gate v = edge->ge_vertex;
13270Sstevel@tonic-gate
13280Sstevel@tonic-gate switch (v->gv_type) {
13290Sstevel@tonic-gate case GVT_INST:
13300Sstevel@tonic-gate /* Skip missing or disabled instances */
13310Sstevel@tonic-gate if ((v->gv_flags & (GV_CONFIGURED | GV_ENABLED)) !=
13320Sstevel@tonic-gate (GV_CONFIGURED | GV_ENABLED))
13330Sstevel@tonic-gate continue;
13340Sstevel@tonic-gate
13350Sstevel@tonic-gate if (v->gv_state == RESTARTER_STATE_MAINT)
13360Sstevel@tonic-gate continue;
13370Sstevel@tonic-gate
13388354SRenaud.Manus@Sun.COM if (v->gv_flags & GV_TOOFFLINE)
13398354SRenaud.Manus@Sun.COM continue;
13408354SRenaud.Manus@Sun.COM
13410Sstevel@tonic-gate any_qualified = B_TRUE;
13420Sstevel@tonic-gate if (v->gv_state == RESTARTER_STATE_OFFLINE) {
13430Sstevel@tonic-gate /*
13440Sstevel@tonic-gate * For offline dependencies, treat unsatisfiable
13450Sstevel@tonic-gate * as satisfied.
13460Sstevel@tonic-gate */
13470Sstevel@tonic-gate i = dependency_satisfied(v, B_TRUE);
13480Sstevel@tonic-gate if (i == -1)
13490Sstevel@tonic-gate i = 1;
13500Sstevel@tonic-gate } else if (v->gv_state == RESTARTER_STATE_DISABLED) {
13510Sstevel@tonic-gate /*
13520Sstevel@tonic-gate * The service is enabled, but hasn't
13530Sstevel@tonic-gate * transitioned out of disabled yet. Treat it
13540Sstevel@tonic-gate * as unsatisfied (not unsatisfiable).
13550Sstevel@tonic-gate */
13560Sstevel@tonic-gate i = 0;
13570Sstevel@tonic-gate } else {
13580Sstevel@tonic-gate i = dependency_satisfied(v, satbility);
13590Sstevel@tonic-gate }
13600Sstevel@tonic-gate break;
13610Sstevel@tonic-gate
13620Sstevel@tonic-gate case GVT_FILE:
13630Sstevel@tonic-gate any_qualified = B_TRUE;
13640Sstevel@tonic-gate i = dependency_satisfied(v, satbility);
13650Sstevel@tonic-gate
13660Sstevel@tonic-gate break;
13670Sstevel@tonic-gate
13680Sstevel@tonic-gate case GVT_SVC: {
13690Sstevel@tonic-gate boolean_t svc_any_qualified;
13700Sstevel@tonic-gate boolean_t svc_satisfied;
13710Sstevel@tonic-gate boolean_t svc_satisfiable;
13720Sstevel@tonic-gate graph_vertex_t *v2;
13730Sstevel@tonic-gate graph_edge_t *e2;
13740Sstevel@tonic-gate
13750Sstevel@tonic-gate svc_any_qualified = B_FALSE;
13760Sstevel@tonic-gate svc_satisfied = B_FALSE;
13770Sstevel@tonic-gate svc_satisfiable = B_FALSE;
13780Sstevel@tonic-gate
13790Sstevel@tonic-gate for (e2 = uu_list_first(v->gv_dependencies);
13800Sstevel@tonic-gate e2 != NULL;
13810Sstevel@tonic-gate e2 = uu_list_next(v->gv_dependencies, e2)) {
13820Sstevel@tonic-gate v2 = e2->ge_vertex;
13830Sstevel@tonic-gate assert(v2->gv_type == GVT_INST);
13840Sstevel@tonic-gate
13850Sstevel@tonic-gate if ((v2->gv_flags &
13860Sstevel@tonic-gate (GV_CONFIGURED | GV_ENABLED)) !=
13870Sstevel@tonic-gate (GV_CONFIGURED | GV_ENABLED))
13880Sstevel@tonic-gate continue;
13890Sstevel@tonic-gate
13900Sstevel@tonic-gate if (v2->gv_state == RESTARTER_STATE_MAINT)
13910Sstevel@tonic-gate continue;
13920Sstevel@tonic-gate
13938354SRenaud.Manus@Sun.COM if (v2->gv_flags & GV_TOOFFLINE)
13948354SRenaud.Manus@Sun.COM continue;
13958354SRenaud.Manus@Sun.COM
13960Sstevel@tonic-gate svc_any_qualified = B_TRUE;
13970Sstevel@tonic-gate
13980Sstevel@tonic-gate if (v2->gv_state == RESTARTER_STATE_OFFLINE) {
13990Sstevel@tonic-gate /*
14000Sstevel@tonic-gate * For offline dependencies, treat
14010Sstevel@tonic-gate * unsatisfiable as satisfied.
14020Sstevel@tonic-gate */
14030Sstevel@tonic-gate i = dependency_satisfied(v2, B_TRUE);
14040Sstevel@tonic-gate if (i == -1)
14050Sstevel@tonic-gate i = 1;
14060Sstevel@tonic-gate } else if (v2->gv_state ==
14070Sstevel@tonic-gate RESTARTER_STATE_DISABLED) {
14080Sstevel@tonic-gate i = 0;
14090Sstevel@tonic-gate } else {
14100Sstevel@tonic-gate i = dependency_satisfied(v2, satbility);
14110Sstevel@tonic-gate }
14120Sstevel@tonic-gate
14130Sstevel@tonic-gate if (i == 1) {
14140Sstevel@tonic-gate svc_satisfied = B_TRUE;
14150Sstevel@tonic-gate break;
14160Sstevel@tonic-gate }
14170Sstevel@tonic-gate if (i == 0)
14180Sstevel@tonic-gate svc_satisfiable = B_TRUE;
14190Sstevel@tonic-gate }
14200Sstevel@tonic-gate
14210Sstevel@tonic-gate if (!svc_any_qualified)
14220Sstevel@tonic-gate continue;
14230Sstevel@tonic-gate any_qualified = B_TRUE;
14240Sstevel@tonic-gate if (svc_satisfied) {
14250Sstevel@tonic-gate i = 1;
14260Sstevel@tonic-gate } else if (svc_satisfiable) {
14270Sstevel@tonic-gate i = 0;
14280Sstevel@tonic-gate } else {
14290Sstevel@tonic-gate i = -1;
14300Sstevel@tonic-gate }
14310Sstevel@tonic-gate break;
14320Sstevel@tonic-gate }
14330Sstevel@tonic-gate
14340Sstevel@tonic-gate case GVT_GROUP:
14350Sstevel@tonic-gate default:
14360Sstevel@tonic-gate #ifndef NDEBUG
14370Sstevel@tonic-gate uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
14380Sstevel@tonic-gate __LINE__, v->gv_type);
14390Sstevel@tonic-gate #endif
14400Sstevel@tonic-gate abort();
14410Sstevel@tonic-gate }
14420Sstevel@tonic-gate
14430Sstevel@tonic-gate if (i == 1)
14440Sstevel@tonic-gate continue;
14450Sstevel@tonic-gate
14465680Srm88369 log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES,
14470Sstevel@tonic-gate "optional_all(%s): %s is unsatisfi%s.\n", groupv->gv_name,
14480Sstevel@tonic-gate v->gv_name, i == 0 ? "ed" : "able");
14490Sstevel@tonic-gate
14500Sstevel@tonic-gate if (!satbility)
14510Sstevel@tonic-gate return (0);
14520Sstevel@tonic-gate if (i == -1)
14530Sstevel@tonic-gate return (-1);
14540Sstevel@tonic-gate any_unsatisfied = B_TRUE;
14550Sstevel@tonic-gate }
14560Sstevel@tonic-gate
14570Sstevel@tonic-gate if (!any_qualified)
14580Sstevel@tonic-gate return (1);
14590Sstevel@tonic-gate
14600Sstevel@tonic-gate return (any_unsatisfied ? 0 : 1);
14610Sstevel@tonic-gate }
14620Sstevel@tonic-gate
14630Sstevel@tonic-gate /*
14640Sstevel@tonic-gate * An exclude_all dependency is unsatisfied if any non-service element is
14650Sstevel@tonic-gate * satisfied or any service instance which is configured, enabled, and not in
14660Sstevel@tonic-gate * maintenance is satisfied. Usually when unsatisfied, it is also
14670Sstevel@tonic-gate * unsatisfiable.
14680Sstevel@tonic-gate */
14690Sstevel@tonic-gate #define LOG_EXCLUDE(u, v) \
14705680Srm88369 log_framework2(LOG_DEBUG, DEBUG_DEPENDENCIES, \
14715680Srm88369 "exclude_all(%s): %s is satisfied.\n", \
14720Sstevel@tonic-gate (u)->gv_name, (v)->gv_name)
14730Sstevel@tonic-gate
14740Sstevel@tonic-gate /* ARGSUSED */
14750Sstevel@tonic-gate static int
exclude_all_satisfied(graph_vertex_t * groupv,boolean_t satbility)14760Sstevel@tonic-gate exclude_all_satisfied(graph_vertex_t *groupv, boolean_t satbility)
14770Sstevel@tonic-gate {
14780Sstevel@tonic-gate graph_edge_t *edge, *e2;
14790Sstevel@tonic-gate graph_vertex_t *v, *v2;
14800Sstevel@tonic-gate
14810Sstevel@tonic-gate for (edge = uu_list_first(groupv->gv_dependencies);
14820Sstevel@tonic-gate edge != NULL;
14830Sstevel@tonic-gate edge = uu_list_next(groupv->gv_dependencies, edge)) {
14840Sstevel@tonic-gate v = edge->ge_vertex;
14850Sstevel@tonic-gate
14860Sstevel@tonic-gate switch (v->gv_type) {
14870Sstevel@tonic-gate case GVT_INST:
14880Sstevel@tonic-gate if ((v->gv_flags & GV_CONFIGURED) == 0)
14890Sstevel@tonic-gate continue;
14900Sstevel@tonic-gate
14910Sstevel@tonic-gate switch (v->gv_state) {
14920Sstevel@tonic-gate case RESTARTER_STATE_ONLINE:
14930Sstevel@tonic-gate case RESTARTER_STATE_DEGRADED:
14940Sstevel@tonic-gate LOG_EXCLUDE(groupv, v);
14950Sstevel@tonic-gate return (v->gv_flags & GV_ENABLED ? -1 : 0);
14960Sstevel@tonic-gate
14970Sstevel@tonic-gate case RESTARTER_STATE_OFFLINE:
14980Sstevel@tonic-gate case RESTARTER_STATE_UNINIT:
14990Sstevel@tonic-gate LOG_EXCLUDE(groupv, v);
15000Sstevel@tonic-gate return (0);
15010Sstevel@tonic-gate
15020Sstevel@tonic-gate case RESTARTER_STATE_DISABLED:
15030Sstevel@tonic-gate case RESTARTER_STATE_MAINT:
15040Sstevel@tonic-gate continue;
15050Sstevel@tonic-gate
15060Sstevel@tonic-gate default:
15070Sstevel@tonic-gate #ifndef NDEBUG
15080Sstevel@tonic-gate uu_warn("%s:%d: Unexpected vertex state %d.\n",
15090Sstevel@tonic-gate __FILE__, __LINE__, v->gv_state);
15100Sstevel@tonic-gate #endif
15110Sstevel@tonic-gate abort();
15120Sstevel@tonic-gate }
15130Sstevel@tonic-gate /* NOTREACHED */
15140Sstevel@tonic-gate
15150Sstevel@tonic-gate case GVT_SVC:
15160Sstevel@tonic-gate break;
15170Sstevel@tonic-gate
15180Sstevel@tonic-gate case GVT_FILE:
15190Sstevel@tonic-gate if (!file_ready(v))
15200Sstevel@tonic-gate continue;
15210Sstevel@tonic-gate LOG_EXCLUDE(groupv, v);
15220Sstevel@tonic-gate return (-1);
15230Sstevel@tonic-gate
15240Sstevel@tonic-gate case GVT_GROUP:
15250Sstevel@tonic-gate default:
15260Sstevel@tonic-gate #ifndef NDEBUG
15270Sstevel@tonic-gate uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
15280Sstevel@tonic-gate __LINE__, v->gv_type);
15290Sstevel@tonic-gate #endif
15300Sstevel@tonic-gate abort();
15310Sstevel@tonic-gate }
15320Sstevel@tonic-gate
15330Sstevel@tonic-gate /* v represents a service */
15340Sstevel@tonic-gate if (uu_list_numnodes(v->gv_dependencies) == 0)
15350Sstevel@tonic-gate continue;
15360Sstevel@tonic-gate
15370Sstevel@tonic-gate for (e2 = uu_list_first(v->gv_dependencies);
15380Sstevel@tonic-gate e2 != NULL;
15390Sstevel@tonic-gate e2 = uu_list_next(v->gv_dependencies, e2)) {
15400Sstevel@tonic-gate v2 = e2->ge_vertex;
15410Sstevel@tonic-gate assert(v2->gv_type == GVT_INST);
15420Sstevel@tonic-gate
15430Sstevel@tonic-gate if ((v2->gv_flags & GV_CONFIGURED) == 0)
15440Sstevel@tonic-gate continue;
15450Sstevel@tonic-gate
15460Sstevel@tonic-gate switch (v2->gv_state) {
15470Sstevel@tonic-gate case RESTARTER_STATE_ONLINE:
15480Sstevel@tonic-gate case RESTARTER_STATE_DEGRADED:
15490Sstevel@tonic-gate LOG_EXCLUDE(groupv, v2);
15500Sstevel@tonic-gate return (v2->gv_flags & GV_ENABLED ? -1 : 0);
15510Sstevel@tonic-gate
15520Sstevel@tonic-gate case RESTARTER_STATE_OFFLINE:
15530Sstevel@tonic-gate case RESTARTER_STATE_UNINIT:
15540Sstevel@tonic-gate LOG_EXCLUDE(groupv, v2);
15550Sstevel@tonic-gate return (0);
15560Sstevel@tonic-gate
15570Sstevel@tonic-gate case RESTARTER_STATE_DISABLED:
15580Sstevel@tonic-gate case RESTARTER_STATE_MAINT:
15590Sstevel@tonic-gate continue;
15600Sstevel@tonic-gate
15610Sstevel@tonic-gate default:
15620Sstevel@tonic-gate #ifndef NDEBUG
15630Sstevel@tonic-gate uu_warn("%s:%d: Unexpected vertex type %d.\n",
15640Sstevel@tonic-gate __FILE__, __LINE__, v2->gv_type);
15650Sstevel@tonic-gate #endif
15660Sstevel@tonic-gate abort();
15670Sstevel@tonic-gate }
15680Sstevel@tonic-gate }
15690Sstevel@tonic-gate }
15700Sstevel@tonic-gate
15710Sstevel@tonic-gate return (1);
15720Sstevel@tonic-gate }
15730Sstevel@tonic-gate
15740Sstevel@tonic-gate /*
15750Sstevel@tonic-gate * int instance_satisfied()
15760Sstevel@tonic-gate * Determine if all the dependencies are satisfied for the supplied instance
15770Sstevel@tonic-gate * vertex. Return 1 if they are, 0 if they aren't, and -1 if they won't be
15780Sstevel@tonic-gate * without administrator intervention.
15790Sstevel@tonic-gate */
15800Sstevel@tonic-gate static int
instance_satisfied(graph_vertex_t * v,boolean_t satbility)15810Sstevel@tonic-gate instance_satisfied(graph_vertex_t *v, boolean_t satbility)
15820Sstevel@tonic-gate {
15830Sstevel@tonic-gate assert(v->gv_type == GVT_INST);
15840Sstevel@tonic-gate assert(!inst_running(v));
15850Sstevel@tonic-gate
15860Sstevel@tonic-gate return (require_all_satisfied(v, satbility));
15870Sstevel@tonic-gate }
15880Sstevel@tonic-gate
15890Sstevel@tonic-gate /*
15900Sstevel@tonic-gate * Decide whether v can satisfy a dependency. v can either be a child of
15910Sstevel@tonic-gate * a group vertex, or of an instance vertex.
15920Sstevel@tonic-gate */
15930Sstevel@tonic-gate static int
dependency_satisfied(graph_vertex_t * v,boolean_t satbility)15940Sstevel@tonic-gate dependency_satisfied(graph_vertex_t *v, boolean_t satbility)
15950Sstevel@tonic-gate {
15960Sstevel@tonic-gate switch (v->gv_type) {
15970Sstevel@tonic-gate case GVT_INST:
15987475SPhilippe.Jung@Sun.COM if ((v->gv_flags & GV_CONFIGURED) == 0) {
15997475SPhilippe.Jung@Sun.COM if (v->gv_flags & GV_DEATHROW) {
16007475SPhilippe.Jung@Sun.COM /*
16017475SPhilippe.Jung@Sun.COM * A dependency on an instance with GV_DEATHROW
16027475SPhilippe.Jung@Sun.COM * flag is always considered as satisfied.
16037475SPhilippe.Jung@Sun.COM */
16047475SPhilippe.Jung@Sun.COM return (1);
16057475SPhilippe.Jung@Sun.COM }
16060Sstevel@tonic-gate return (-1);
16077475SPhilippe.Jung@Sun.COM }
16080Sstevel@tonic-gate
16098354SRenaud.Manus@Sun.COM /*
16108354SRenaud.Manus@Sun.COM * Any vertex with the GV_TOOFFLINE flag set is guaranteed
16118354SRenaud.Manus@Sun.COM * to have its dependencies unsatisfiable.
16128354SRenaud.Manus@Sun.COM */
16138354SRenaud.Manus@Sun.COM if (v->gv_flags & GV_TOOFFLINE)
16148354SRenaud.Manus@Sun.COM return (-1);
16158354SRenaud.Manus@Sun.COM
16160Sstevel@tonic-gate switch (v->gv_state) {
16170Sstevel@tonic-gate case RESTARTER_STATE_ONLINE:
16180Sstevel@tonic-gate case RESTARTER_STATE_DEGRADED:
16190Sstevel@tonic-gate return (1);
16200Sstevel@tonic-gate
16210Sstevel@tonic-gate case RESTARTER_STATE_OFFLINE:
16220Sstevel@tonic-gate if (!satbility)
16230Sstevel@tonic-gate return (0);
16240Sstevel@tonic-gate return (instance_satisfied(v, satbility) != -1 ?
16250Sstevel@tonic-gate 0 : -1);
16260Sstevel@tonic-gate
16270Sstevel@tonic-gate case RESTARTER_STATE_DISABLED:
16280Sstevel@tonic-gate case RESTARTER_STATE_MAINT:
16290Sstevel@tonic-gate return (-1);
16300Sstevel@tonic-gate
16310Sstevel@tonic-gate case RESTARTER_STATE_UNINIT:
16320Sstevel@tonic-gate return (0);
16330Sstevel@tonic-gate
16340Sstevel@tonic-gate default:
16350Sstevel@tonic-gate #ifndef NDEBUG
16360Sstevel@tonic-gate uu_warn("%s:%d: Unexpected vertex state %d.\n",
16370Sstevel@tonic-gate __FILE__, __LINE__, v->gv_state);
16380Sstevel@tonic-gate #endif
16390Sstevel@tonic-gate abort();
16400Sstevel@tonic-gate /* NOTREACHED */
16410Sstevel@tonic-gate }
16420Sstevel@tonic-gate
16430Sstevel@tonic-gate case GVT_SVC:
16440Sstevel@tonic-gate if (uu_list_numnodes(v->gv_dependencies) == 0)
16450Sstevel@tonic-gate return (-1);
16460Sstevel@tonic-gate return (require_any_satisfied(v, satbility));
16470Sstevel@tonic-gate
16480Sstevel@tonic-gate case GVT_FILE:
16490Sstevel@tonic-gate /* i.e., we assume files will not be automatically generated */
16500Sstevel@tonic-gate return (file_ready(v) ? 1 : -1);
16510Sstevel@tonic-gate
16520Sstevel@tonic-gate case GVT_GROUP:
16530Sstevel@tonic-gate break;
16540Sstevel@tonic-gate
16550Sstevel@tonic-gate default:
16560Sstevel@tonic-gate #ifndef NDEBUG
16570Sstevel@tonic-gate uu_warn("%s:%d: Unexpected node type %d.\n", __FILE__, __LINE__,
16580Sstevel@tonic-gate v->gv_type);
16590Sstevel@tonic-gate #endif
16600Sstevel@tonic-gate abort();
16610Sstevel@tonic-gate /* NOTREACHED */
16620Sstevel@tonic-gate }
16630Sstevel@tonic-gate
16640Sstevel@tonic-gate switch (v->gv_depgroup) {
16650Sstevel@tonic-gate case DEPGRP_REQUIRE_ANY:
16660Sstevel@tonic-gate return (require_any_satisfied(v, satbility));
16670Sstevel@tonic-gate
16680Sstevel@tonic-gate case DEPGRP_REQUIRE_ALL:
16690Sstevel@tonic-gate return (require_all_satisfied(v, satbility));
16700Sstevel@tonic-gate
16710Sstevel@tonic-gate case DEPGRP_OPTIONAL_ALL:
16720Sstevel@tonic-gate return (optional_all_satisfied(v, satbility));
16730Sstevel@tonic-gate
16740Sstevel@tonic-gate case DEPGRP_EXCLUDE_ALL:
16750Sstevel@tonic-gate return (exclude_all_satisfied(v, satbility));
16760Sstevel@tonic-gate
16770Sstevel@tonic-gate default:
16780Sstevel@tonic-gate #ifndef NDEBUG
16790Sstevel@tonic-gate uu_warn("%s:%d: Unknown dependency grouping %d.\n", __FILE__,
16800Sstevel@tonic-gate __LINE__, v->gv_depgroup);
16810Sstevel@tonic-gate #endif
16820Sstevel@tonic-gate abort();
16830Sstevel@tonic-gate }
16840Sstevel@tonic-gate }
16850Sstevel@tonic-gate
16861958Slianep void
graph_start_if_satisfied(graph_vertex_t * v)16871958Slianep graph_start_if_satisfied(graph_vertex_t *v)
16880Sstevel@tonic-gate {
16890Sstevel@tonic-gate if (v->gv_state == RESTARTER_STATE_OFFLINE &&
16900Sstevel@tonic-gate instance_satisfied(v, B_FALSE) == 1) {
16910Sstevel@tonic-gate if (v->gv_start_f == NULL)
16920Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_START);
16930Sstevel@tonic-gate else
16940Sstevel@tonic-gate v->gv_start_f(v);
16950Sstevel@tonic-gate }
16960Sstevel@tonic-gate }
16970Sstevel@tonic-gate
16980Sstevel@tonic-gate /*
16990Sstevel@tonic-gate * propagate_satbility()
17000Sstevel@tonic-gate *
17010Sstevel@tonic-gate * This function is used when the given vertex changes state in such a way that
17020Sstevel@tonic-gate * one of its dependents may become unsatisfiable. This happens when an
17030Sstevel@tonic-gate * instance transitions between offline -> online, or from !running ->
17040Sstevel@tonic-gate * maintenance, as well as when an instance is removed from the graph.
17050Sstevel@tonic-gate *
17062339Slianep * We have to walk all the dependents, since optional_all dependencies several
17070Sstevel@tonic-gate * levels up could become (un)satisfied, instead of unsatisfiable. For example,
17080Sstevel@tonic-gate *
17090Sstevel@tonic-gate * +-----+ optional_all +-----+ require_all +-----+
17100Sstevel@tonic-gate * | A |--------------->| B |-------------->| C |
17110Sstevel@tonic-gate * +-----+ +-----+ +-----+
17120Sstevel@tonic-gate *
17130Sstevel@tonic-gate * offline -> maintenance
17140Sstevel@tonic-gate *
17150Sstevel@tonic-gate * If C goes into maintenance, it's not enough simply to check B. Because A has
17160Sstevel@tonic-gate * an optional dependency, what was previously an unsatisfiable situation is now
17170Sstevel@tonic-gate * satisfied (B will never come online, even though its state hasn't changed).
17180Sstevel@tonic-gate *
17190Sstevel@tonic-gate * Note that it's not necessary to continue examining dependents after reaching
17200Sstevel@tonic-gate * an optional_all dependency. It's not possible for an optional_all dependency
17210Sstevel@tonic-gate * to change satisfiability without also coming online, in which case we get a
17220Sstevel@tonic-gate * start event and propagation continues naturally. However, it does no harm to
17230Sstevel@tonic-gate * continue propagating satisfiability (as it is a relatively rare event), and
17240Sstevel@tonic-gate * keeps the walker code simple and generic.
17250Sstevel@tonic-gate */
17260Sstevel@tonic-gate /*ARGSUSED*/
17270Sstevel@tonic-gate static int
satbility_cb(graph_vertex_t * v,void * arg)17280Sstevel@tonic-gate satbility_cb(graph_vertex_t *v, void *arg)
17290Sstevel@tonic-gate {
17300Sstevel@tonic-gate if (v->gv_type == GVT_INST)
17311958Slianep graph_start_if_satisfied(v);
17320Sstevel@tonic-gate
17330Sstevel@tonic-gate return (UU_WALK_NEXT);
17340Sstevel@tonic-gate }
17350Sstevel@tonic-gate
17360Sstevel@tonic-gate static void
propagate_satbility(graph_vertex_t * v)17370Sstevel@tonic-gate propagate_satbility(graph_vertex_t *v)
17380Sstevel@tonic-gate {
17390Sstevel@tonic-gate graph_walk(v, WALK_DEPENDENTS, satbility_cb, NULL, NULL);
17400Sstevel@tonic-gate }
17410Sstevel@tonic-gate
17420Sstevel@tonic-gate static void propagate_stop(graph_vertex_t *, void *);
17430Sstevel@tonic-gate
17440Sstevel@tonic-gate /* ARGSUSED */
17450Sstevel@tonic-gate static void
propagate_start(graph_vertex_t * v,void * arg)17460Sstevel@tonic-gate propagate_start(graph_vertex_t *v, void *arg)
17470Sstevel@tonic-gate {
17480Sstevel@tonic-gate switch (v->gv_type) {
17490Sstevel@tonic-gate case GVT_INST:
17501958Slianep graph_start_if_satisfied(v);
17510Sstevel@tonic-gate break;
17520Sstevel@tonic-gate
17530Sstevel@tonic-gate case GVT_GROUP:
17540Sstevel@tonic-gate if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
17550Sstevel@tonic-gate graph_walk_dependents(v, propagate_stop,
17560Sstevel@tonic-gate (void *)RERR_RESTART);
17570Sstevel@tonic-gate break;
17580Sstevel@tonic-gate }
17590Sstevel@tonic-gate /* FALLTHROUGH */
17600Sstevel@tonic-gate
17610Sstevel@tonic-gate case GVT_SVC:
17620Sstevel@tonic-gate graph_walk_dependents(v, propagate_start, NULL);
17630Sstevel@tonic-gate break;
17640Sstevel@tonic-gate
17650Sstevel@tonic-gate case GVT_FILE:
17660Sstevel@tonic-gate #ifndef NDEBUG
17670Sstevel@tonic-gate uu_warn("%s:%d: propagate_start() encountered GVT_FILE.\n",
17680Sstevel@tonic-gate __FILE__, __LINE__);
17690Sstevel@tonic-gate #endif
17700Sstevel@tonic-gate abort();
17710Sstevel@tonic-gate /* NOTREACHED */
17720Sstevel@tonic-gate
17730Sstevel@tonic-gate default:
17740Sstevel@tonic-gate #ifndef NDEBUG
17750Sstevel@tonic-gate uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__,
17760Sstevel@tonic-gate v->gv_type);
17770Sstevel@tonic-gate #endif
17780Sstevel@tonic-gate abort();
17790Sstevel@tonic-gate }
17800Sstevel@tonic-gate }
17810Sstevel@tonic-gate
17820Sstevel@tonic-gate static void
propagate_stop(graph_vertex_t * v,void * arg)17830Sstevel@tonic-gate propagate_stop(graph_vertex_t *v, void *arg)
17840Sstevel@tonic-gate {
17850Sstevel@tonic-gate graph_edge_t *e;
17860Sstevel@tonic-gate graph_vertex_t *svc;
17870Sstevel@tonic-gate restarter_error_t err = (restarter_error_t)arg;
17880Sstevel@tonic-gate
17890Sstevel@tonic-gate switch (v->gv_type) {
17900Sstevel@tonic-gate case GVT_INST:
17910Sstevel@tonic-gate /* Restarter */
179211482SSean.Wilcox@Sun.COM if (err > RERR_NONE && inst_running(v)) {
179311623SSean.Wilcox@Sun.COM if (err == RERR_RESTART || err == RERR_REFRESH) {
179411482SSean.Wilcox@Sun.COM vertex_send_event(v,
179511482SSean.Wilcox@Sun.COM RESTARTER_EVENT_TYPE_STOP_RESET);
179611482SSean.Wilcox@Sun.COM } else {
179711482SSean.Wilcox@Sun.COM vertex_send_event(v, RESTARTER_EVENT_TYPE_STOP);
179811482SSean.Wilcox@Sun.COM }
179911482SSean.Wilcox@Sun.COM }
18000Sstevel@tonic-gate break;
18010Sstevel@tonic-gate
18020Sstevel@tonic-gate case GVT_SVC:
18030Sstevel@tonic-gate graph_walk_dependents(v, propagate_stop, arg);
18040Sstevel@tonic-gate break;
18050Sstevel@tonic-gate
18060Sstevel@tonic-gate case GVT_FILE:
18070Sstevel@tonic-gate #ifndef NDEBUG
18080Sstevel@tonic-gate uu_warn("%s:%d: propagate_stop() encountered GVT_FILE.\n",
18090Sstevel@tonic-gate __FILE__, __LINE__);
18100Sstevel@tonic-gate #endif
18110Sstevel@tonic-gate abort();
18120Sstevel@tonic-gate /* NOTREACHED */
18130Sstevel@tonic-gate
18140Sstevel@tonic-gate case GVT_GROUP:
18150Sstevel@tonic-gate if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) {
18160Sstevel@tonic-gate graph_walk_dependents(v, propagate_start, NULL);
18170Sstevel@tonic-gate break;
18180Sstevel@tonic-gate }
18190Sstevel@tonic-gate
18200Sstevel@tonic-gate if (err == RERR_NONE || err > v->gv_restart)
18210Sstevel@tonic-gate break;
18220Sstevel@tonic-gate
18230Sstevel@tonic-gate assert(uu_list_numnodes(v->gv_dependents) == 1);
18240Sstevel@tonic-gate e = uu_list_first(v->gv_dependents);
18250Sstevel@tonic-gate svc = e->ge_vertex;
18260Sstevel@tonic-gate
182711482SSean.Wilcox@Sun.COM if (inst_running(svc)) {
182811623SSean.Wilcox@Sun.COM if (err == RERR_RESTART || err == RERR_REFRESH) {
182911482SSean.Wilcox@Sun.COM vertex_send_event(svc,
183011482SSean.Wilcox@Sun.COM RESTARTER_EVENT_TYPE_STOP_RESET);
183111482SSean.Wilcox@Sun.COM } else {
183211482SSean.Wilcox@Sun.COM vertex_send_event(svc,
183311482SSean.Wilcox@Sun.COM RESTARTER_EVENT_TYPE_STOP);
183411482SSean.Wilcox@Sun.COM }
183511482SSean.Wilcox@Sun.COM }
18360Sstevel@tonic-gate break;
18370Sstevel@tonic-gate
18380Sstevel@tonic-gate default:
18390Sstevel@tonic-gate #ifndef NDEBUG
18400Sstevel@tonic-gate uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__,
18410Sstevel@tonic-gate v->gv_type);
18420Sstevel@tonic-gate #endif
18430Sstevel@tonic-gate abort();
18440Sstevel@tonic-gate }
18450Sstevel@tonic-gate }
18460Sstevel@tonic-gate
18479333SRenaud.Manus@Sun.COM void
offline_vertex(graph_vertex_t * v)18487630SRenaud.Manus@Sun.COM offline_vertex(graph_vertex_t *v)
18497630SRenaud.Manus@Sun.COM {
18507630SRenaud.Manus@Sun.COM scf_handle_t *h = libscf_handle_create_bound_loop();
18517630SRenaud.Manus@Sun.COM scf_instance_t *scf_inst = safe_scf_instance_create(h);
18527630SRenaud.Manus@Sun.COM scf_propertygroup_t *pg = safe_scf_pg_create(h);
18537630SRenaud.Manus@Sun.COM restarter_instance_state_t state, next_state;
18547630SRenaud.Manus@Sun.COM int r;
18557630SRenaud.Manus@Sun.COM
18567630SRenaud.Manus@Sun.COM assert(v->gv_type == GVT_INST);
18577630SRenaud.Manus@Sun.COM
18587630SRenaud.Manus@Sun.COM if (scf_inst == NULL)
18597630SRenaud.Manus@Sun.COM bad_error("safe_scf_instance_create", scf_error());
18607630SRenaud.Manus@Sun.COM if (pg == NULL)
18617630SRenaud.Manus@Sun.COM bad_error("safe_scf_pg_create", scf_error());
18627630SRenaud.Manus@Sun.COM
18637630SRenaud.Manus@Sun.COM /* if the vertex is already going offline, return */
18647630SRenaud.Manus@Sun.COM rep_retry:
18657630SRenaud.Manus@Sun.COM if (scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, scf_inst, NULL,
18667630SRenaud.Manus@Sun.COM NULL, SCF_DECODE_FMRI_EXACT) != 0) {
18677630SRenaud.Manus@Sun.COM switch (scf_error()) {
18687630SRenaud.Manus@Sun.COM case SCF_ERROR_CONNECTION_BROKEN:
18697630SRenaud.Manus@Sun.COM libscf_handle_rebind(h);
18707630SRenaud.Manus@Sun.COM goto rep_retry;
18717630SRenaud.Manus@Sun.COM
18727630SRenaud.Manus@Sun.COM case SCF_ERROR_NOT_FOUND:
18737630SRenaud.Manus@Sun.COM scf_pg_destroy(pg);
18747630SRenaud.Manus@Sun.COM scf_instance_destroy(scf_inst);
18757630SRenaud.Manus@Sun.COM (void) scf_handle_unbind(h);
18767630SRenaud.Manus@Sun.COM scf_handle_destroy(h);
18777630SRenaud.Manus@Sun.COM return;
18787630SRenaud.Manus@Sun.COM }
18797630SRenaud.Manus@Sun.COM uu_die("Can't decode FMRI %s: %s\n", v->gv_name,
18807630SRenaud.Manus@Sun.COM scf_strerror(scf_error()));
18817630SRenaud.Manus@Sun.COM }
18827630SRenaud.Manus@Sun.COM
18837630SRenaud.Manus@Sun.COM r = scf_instance_get_pg(scf_inst, SCF_PG_RESTARTER, pg);
18847630SRenaud.Manus@Sun.COM if (r != 0) {
18857630SRenaud.Manus@Sun.COM switch (scf_error()) {
18867630SRenaud.Manus@Sun.COM case SCF_ERROR_CONNECTION_BROKEN:
18877630SRenaud.Manus@Sun.COM libscf_handle_rebind(h);
18887630SRenaud.Manus@Sun.COM goto rep_retry;
18897630SRenaud.Manus@Sun.COM
18907630SRenaud.Manus@Sun.COM case SCF_ERROR_NOT_SET:
18917630SRenaud.Manus@Sun.COM case SCF_ERROR_NOT_FOUND:
18927630SRenaud.Manus@Sun.COM scf_pg_destroy(pg);
18937630SRenaud.Manus@Sun.COM scf_instance_destroy(scf_inst);
18947630SRenaud.Manus@Sun.COM (void) scf_handle_unbind(h);
18957630SRenaud.Manus@Sun.COM scf_handle_destroy(h);
18967630SRenaud.Manus@Sun.COM return;
18977630SRenaud.Manus@Sun.COM
18987630SRenaud.Manus@Sun.COM default:
18997630SRenaud.Manus@Sun.COM bad_error("scf_instance_get_pg", scf_error());
19007630SRenaud.Manus@Sun.COM }
19017630SRenaud.Manus@Sun.COM } else {
19027630SRenaud.Manus@Sun.COM r = libscf_read_states(pg, &state, &next_state);
19037630SRenaud.Manus@Sun.COM if (r == 0 && (next_state == RESTARTER_STATE_OFFLINE ||
19047630SRenaud.Manus@Sun.COM next_state == RESTARTER_STATE_DISABLED)) {
19057630SRenaud.Manus@Sun.COM log_framework(LOG_DEBUG,
19067630SRenaud.Manus@Sun.COM "%s: instance is already going down.\n",
19077630SRenaud.Manus@Sun.COM v->gv_name);
19087630SRenaud.Manus@Sun.COM scf_pg_destroy(pg);
19097630SRenaud.Manus@Sun.COM scf_instance_destroy(scf_inst);
19107630SRenaud.Manus@Sun.COM (void) scf_handle_unbind(h);
19117630SRenaud.Manus@Sun.COM scf_handle_destroy(h);
19127630SRenaud.Manus@Sun.COM return;
19137630SRenaud.Manus@Sun.COM }
19147630SRenaud.Manus@Sun.COM }
19157630SRenaud.Manus@Sun.COM
19167630SRenaud.Manus@Sun.COM scf_pg_destroy(pg);
19177630SRenaud.Manus@Sun.COM scf_instance_destroy(scf_inst);
19187630SRenaud.Manus@Sun.COM (void) scf_handle_unbind(h);
19197630SRenaud.Manus@Sun.COM scf_handle_destroy(h);
19207815SRenaud.Manus@Sun.COM
192111482SSean.Wilcox@Sun.COM vertex_send_event(v, RESTARTER_EVENT_TYPE_STOP_RESET);
19227630SRenaud.Manus@Sun.COM }
19237630SRenaud.Manus@Sun.COM
19240Sstevel@tonic-gate /*
19250Sstevel@tonic-gate * void graph_enable_by_vertex()
19260Sstevel@tonic-gate * If admin is non-zero, this is an administrative request for change
19270Sstevel@tonic-gate * of the enabled property. Thus, send the ADMIN_DISABLE rather than
19280Sstevel@tonic-gate * a plain DISABLE restarter event.
19290Sstevel@tonic-gate */
19301958Slianep void
graph_enable_by_vertex(graph_vertex_t * vertex,int enable,int admin)19310Sstevel@tonic-gate graph_enable_by_vertex(graph_vertex_t *vertex, int enable, int admin)
19320Sstevel@tonic-gate {
19337630SRenaud.Manus@Sun.COM graph_vertex_t *v;
19347630SRenaud.Manus@Sun.COM int r;
19357630SRenaud.Manus@Sun.COM
193611466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
19370Sstevel@tonic-gate assert((vertex->gv_flags & GV_CONFIGURED));
19380Sstevel@tonic-gate
19390Sstevel@tonic-gate vertex->gv_flags = (vertex->gv_flags & ~GV_ENABLED) |
19400Sstevel@tonic-gate (enable ? GV_ENABLED : 0);
19410Sstevel@tonic-gate
19420Sstevel@tonic-gate if (enable) {
19430Sstevel@tonic-gate if (vertex->gv_state != RESTARTER_STATE_OFFLINE &&
19440Sstevel@tonic-gate vertex->gv_state != RESTARTER_STATE_DEGRADED &&
19457630SRenaud.Manus@Sun.COM vertex->gv_state != RESTARTER_STATE_ONLINE) {
19467630SRenaud.Manus@Sun.COM /*
19477630SRenaud.Manus@Sun.COM * In case the vertex was notified to go down,
19487630SRenaud.Manus@Sun.COM * but now can return online, clear the _TOOFFLINE
19497630SRenaud.Manus@Sun.COM * and _TODISABLE flags.
19507630SRenaud.Manus@Sun.COM */
19517630SRenaud.Manus@Sun.COM vertex->gv_flags &= ~GV_TOOFFLINE;
19527630SRenaud.Manus@Sun.COM vertex->gv_flags &= ~GV_TODISABLE;
19537630SRenaud.Manus@Sun.COM
19540Sstevel@tonic-gate vertex_send_event(vertex, RESTARTER_EVENT_TYPE_ENABLE);
19550Sstevel@tonic-gate }
19567630SRenaud.Manus@Sun.COM
19577630SRenaud.Manus@Sun.COM /*
19587630SRenaud.Manus@Sun.COM * Wait for state update from restarter before sending _START or
19597630SRenaud.Manus@Sun.COM * _STOP.
19607630SRenaud.Manus@Sun.COM */
19617630SRenaud.Manus@Sun.COM
19627630SRenaud.Manus@Sun.COM return;
19637630SRenaud.Manus@Sun.COM }
19647630SRenaud.Manus@Sun.COM
19657630SRenaud.Manus@Sun.COM if (vertex->gv_state == RESTARTER_STATE_DISABLED)
19667630SRenaud.Manus@Sun.COM return;
19677630SRenaud.Manus@Sun.COM
19687630SRenaud.Manus@Sun.COM if (!admin) {
19697630SRenaud.Manus@Sun.COM vertex_send_event(vertex, RESTARTER_EVENT_TYPE_DISABLE);
19707630SRenaud.Manus@Sun.COM
19717630SRenaud.Manus@Sun.COM /*
19727630SRenaud.Manus@Sun.COM * Wait for state update from restarter before sending _START or
19737630SRenaud.Manus@Sun.COM * _STOP.
19747630SRenaud.Manus@Sun.COM */
19757630SRenaud.Manus@Sun.COM
19767630SRenaud.Manus@Sun.COM return;
19770Sstevel@tonic-gate }
19780Sstevel@tonic-gate
19790Sstevel@tonic-gate /*
19807630SRenaud.Manus@Sun.COM * If it is a DISABLE event requested by the administrator then we are
19817630SRenaud.Manus@Sun.COM * offlining the dependents first.
19827630SRenaud.Manus@Sun.COM */
19837630SRenaud.Manus@Sun.COM
19847630SRenaud.Manus@Sun.COM /*
19857630SRenaud.Manus@Sun.COM * Set GV_TOOFFLINE for the services we are offlining. We cannot
19867630SRenaud.Manus@Sun.COM * clear the GV_TOOFFLINE bits from all the services because
19877630SRenaud.Manus@Sun.COM * other DISABLE events might be handled at the same time.
19880Sstevel@tonic-gate */
19897630SRenaud.Manus@Sun.COM vertex->gv_flags |= GV_TOOFFLINE;
19907630SRenaud.Manus@Sun.COM
19917630SRenaud.Manus@Sun.COM /* remember which vertex to disable... */
19927630SRenaud.Manus@Sun.COM vertex->gv_flags |= GV_TODISABLE;
19937630SRenaud.Manus@Sun.COM
19948354SRenaud.Manus@Sun.COM log_framework(LOG_DEBUG, "Marking in-subtree vertices before "
19958354SRenaud.Manus@Sun.COM "disabling %s.\n", vertex->gv_name);
19968354SRenaud.Manus@Sun.COM
19977630SRenaud.Manus@Sun.COM /* set GV_TOOFFLINE for its dependents */
19987630SRenaud.Manus@Sun.COM r = uu_list_walk(vertex->gv_dependents, (uu_walk_fn_t *)mark_subtree,
19997630SRenaud.Manus@Sun.COM NULL, 0);
20007630SRenaud.Manus@Sun.COM assert(r == 0);
20017630SRenaud.Manus@Sun.COM
20027630SRenaud.Manus@Sun.COM /* disable the instance now if there is nothing else to offline */
20037630SRenaud.Manus@Sun.COM if (insubtree_dependents_down(vertex) == B_TRUE) {
20047630SRenaud.Manus@Sun.COM vertex_send_event(vertex, RESTARTER_EVENT_TYPE_ADMIN_DISABLE);
20057630SRenaud.Manus@Sun.COM return;
20067630SRenaud.Manus@Sun.COM }
20077630SRenaud.Manus@Sun.COM
20087630SRenaud.Manus@Sun.COM /*
20097630SRenaud.Manus@Sun.COM * This loop is similar to the one used for the graph reversal shutdown
20107630SRenaud.Manus@Sun.COM * and could be improved in term of performance for the subtree reversal
20117630SRenaud.Manus@Sun.COM * disable case.
20127630SRenaud.Manus@Sun.COM */
20137630SRenaud.Manus@Sun.COM for (v = uu_list_first(dgraph); v != NULL;
20147630SRenaud.Manus@Sun.COM v = uu_list_next(dgraph, v)) {
20157630SRenaud.Manus@Sun.COM /* skip the vertex we are disabling for now */
20167630SRenaud.Manus@Sun.COM if (v == vertex)
20177630SRenaud.Manus@Sun.COM continue;
20187630SRenaud.Manus@Sun.COM
20197630SRenaud.Manus@Sun.COM if (v->gv_type != GVT_INST ||
20207630SRenaud.Manus@Sun.COM (v->gv_flags & GV_CONFIGURED) == 0 ||
20217630SRenaud.Manus@Sun.COM (v->gv_flags & GV_ENABLED) == 0 ||
20227630SRenaud.Manus@Sun.COM (v->gv_flags & GV_TOOFFLINE) == 0)
20237630SRenaud.Manus@Sun.COM continue;
20247630SRenaud.Manus@Sun.COM
20257630SRenaud.Manus@Sun.COM if ((v->gv_state != RESTARTER_STATE_ONLINE) &&
20267630SRenaud.Manus@Sun.COM (v->gv_state != RESTARTER_STATE_DEGRADED)) {
20277630SRenaud.Manus@Sun.COM /* continue if there is nothing to offline */
20287630SRenaud.Manus@Sun.COM continue;
20297630SRenaud.Manus@Sun.COM }
20307630SRenaud.Manus@Sun.COM
20317630SRenaud.Manus@Sun.COM /*
20327630SRenaud.Manus@Sun.COM * Instances which are up need to come down before we're
20337630SRenaud.Manus@Sun.COM * done, but we can only offline the leaves here. An
20347630SRenaud.Manus@Sun.COM * instance is a leaf when all its dependents are down.
20357630SRenaud.Manus@Sun.COM */
20368354SRenaud.Manus@Sun.COM if (insubtree_dependents_down(v) == B_TRUE) {
20378354SRenaud.Manus@Sun.COM log_framework(LOG_DEBUG, "Offlining in-subtree "
20388354SRenaud.Manus@Sun.COM "instance %s for %s.\n",
20398354SRenaud.Manus@Sun.COM v->gv_name, vertex->gv_name);
20407630SRenaud.Manus@Sun.COM offline_vertex(v);
20418354SRenaud.Manus@Sun.COM }
20427630SRenaud.Manus@Sun.COM }
20430Sstevel@tonic-gate }
20440Sstevel@tonic-gate
20450Sstevel@tonic-gate static int configure_vertex(graph_vertex_t *, scf_instance_t *);
20460Sstevel@tonic-gate
20470Sstevel@tonic-gate /*
20480Sstevel@tonic-gate * Set the restarter for v to fmri_arg. That is, make sure a vertex for
20490Sstevel@tonic-gate * fmri_arg exists, make v depend on it, and send _ADD_INSTANCE for v. If
20500Sstevel@tonic-gate * v is already configured and fmri_arg indicates the current restarter, do
20510Sstevel@tonic-gate * nothing. If v is configured and fmri_arg is a new restarter, delete v's
20520Sstevel@tonic-gate * dependency on the restarter, send _REMOVE_INSTANCE for v, and set the new
20530Sstevel@tonic-gate * restarter. Returns 0 on success, EINVAL if the FMRI is invalid,
20540Sstevel@tonic-gate * ECONNABORTED if the repository connection is broken, and ELOOP
20550Sstevel@tonic-gate * if the dependency would create a cycle. In the last case, *pathp will
20560Sstevel@tonic-gate * point to a -1-terminated array of ids which compose the path from v to
20570Sstevel@tonic-gate * restarter_fmri.
20580Sstevel@tonic-gate */
20590Sstevel@tonic-gate int
graph_change_restarter(graph_vertex_t * v,const char * fmri_arg,scf_handle_t * h,int ** pathp)20600Sstevel@tonic-gate graph_change_restarter(graph_vertex_t *v, const char *fmri_arg, scf_handle_t *h,
20610Sstevel@tonic-gate int **pathp)
20620Sstevel@tonic-gate {
20630Sstevel@tonic-gate char *restarter_fmri = NULL;
20640Sstevel@tonic-gate graph_vertex_t *rv;
20650Sstevel@tonic-gate int err;
20660Sstevel@tonic-gate int id;
20670Sstevel@tonic-gate
206811466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
20690Sstevel@tonic-gate
20700Sstevel@tonic-gate if (fmri_arg[0] != '\0') {
20710Sstevel@tonic-gate err = fmri_canonify(fmri_arg, &restarter_fmri, B_TRUE);
20720Sstevel@tonic-gate if (err != 0) {
20730Sstevel@tonic-gate assert(err == EINVAL);
20740Sstevel@tonic-gate return (err);
20750Sstevel@tonic-gate }
20760Sstevel@tonic-gate }
20770Sstevel@tonic-gate
20780Sstevel@tonic-gate if (restarter_fmri == NULL ||
20790Sstevel@tonic-gate strcmp(restarter_fmri, SCF_SERVICE_STARTD) == 0) {
20800Sstevel@tonic-gate if (v->gv_flags & GV_CONFIGURED) {
20810Sstevel@tonic-gate if (v->gv_restarter_id == -1) {
20820Sstevel@tonic-gate if (restarter_fmri != NULL)
20830Sstevel@tonic-gate startd_free(restarter_fmri,
20840Sstevel@tonic-gate max_scf_fmri_size);
20850Sstevel@tonic-gate return (0);
20860Sstevel@tonic-gate }
20870Sstevel@tonic-gate
20880Sstevel@tonic-gate graph_unset_restarter(v);
20890Sstevel@tonic-gate }
20900Sstevel@tonic-gate
20910Sstevel@tonic-gate /* Master restarter, nothing to do. */
20920Sstevel@tonic-gate v->gv_restarter_id = -1;
20930Sstevel@tonic-gate v->gv_restarter_channel = NULL;
20940Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE);
20950Sstevel@tonic-gate return (0);
20960Sstevel@tonic-gate }
20970Sstevel@tonic-gate
20980Sstevel@tonic-gate if (v->gv_flags & GV_CONFIGURED) {
20990Sstevel@tonic-gate id = dict_lookup_byname(restarter_fmri);
21000Sstevel@tonic-gate if (id != -1 && v->gv_restarter_id == id) {
21010Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_fmri_size);
21020Sstevel@tonic-gate return (0);
21030Sstevel@tonic-gate }
21040Sstevel@tonic-gate
21050Sstevel@tonic-gate graph_unset_restarter(v);
21060Sstevel@tonic-gate }
21070Sstevel@tonic-gate
21080Sstevel@tonic-gate err = graph_insert_vertex_unconfigured(restarter_fmri, GVT_INST, 0,
21090Sstevel@tonic-gate RERR_NONE, &rv);
21100Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_fmri_size);
21110Sstevel@tonic-gate assert(err == 0 || err == EEXIST);
21120Sstevel@tonic-gate
21130Sstevel@tonic-gate if (rv->gv_delegate_initialized == 0) {
21149263SSean.Wilcox@Sun.COM if ((rv->gv_delegate_channel = restarter_protocol_init_delegate(
21159263SSean.Wilcox@Sun.COM rv->gv_name)) == NULL)
21169263SSean.Wilcox@Sun.COM return (EINVAL);
21170Sstevel@tonic-gate rv->gv_delegate_initialized = 1;
21180Sstevel@tonic-gate }
21190Sstevel@tonic-gate v->gv_restarter_id = rv->gv_id;
21200Sstevel@tonic-gate v->gv_restarter_channel = rv->gv_delegate_channel;
21210Sstevel@tonic-gate
21220Sstevel@tonic-gate err = graph_insert_dependency(v, rv, pathp);
21230Sstevel@tonic-gate if (err != 0) {
21240Sstevel@tonic-gate assert(err == ELOOP);
21250Sstevel@tonic-gate return (ELOOP);
21260Sstevel@tonic-gate }
21270Sstevel@tonic-gate
21280Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE);
21290Sstevel@tonic-gate
21300Sstevel@tonic-gate if (!(rv->gv_flags & GV_CONFIGURED)) {
21310Sstevel@tonic-gate scf_instance_t *inst;
21320Sstevel@tonic-gate
21330Sstevel@tonic-gate err = libscf_fmri_get_instance(h, rv->gv_name, &inst);
21340Sstevel@tonic-gate switch (err) {
21350Sstevel@tonic-gate case 0:
21360Sstevel@tonic-gate err = configure_vertex(rv, inst);
21370Sstevel@tonic-gate scf_instance_destroy(inst);
21380Sstevel@tonic-gate switch (err) {
21390Sstevel@tonic-gate case 0:
21400Sstevel@tonic-gate case ECANCELED:
21410Sstevel@tonic-gate break;
21420Sstevel@tonic-gate
21430Sstevel@tonic-gate case ECONNABORTED:
21440Sstevel@tonic-gate return (ECONNABORTED);
21450Sstevel@tonic-gate
21460Sstevel@tonic-gate default:
21470Sstevel@tonic-gate bad_error("configure_vertex", err);
21480Sstevel@tonic-gate }
21490Sstevel@tonic-gate break;
21500Sstevel@tonic-gate
21510Sstevel@tonic-gate case ECONNABORTED:
21520Sstevel@tonic-gate return (ECONNABORTED);
21530Sstevel@tonic-gate
21540Sstevel@tonic-gate case ENOENT:
21550Sstevel@tonic-gate break;
21560Sstevel@tonic-gate
21570Sstevel@tonic-gate case ENOTSUP:
21580Sstevel@tonic-gate /*
21590Sstevel@tonic-gate * The fmri doesn't specify an instance - translate
21600Sstevel@tonic-gate * to EINVAL.
21610Sstevel@tonic-gate */
21620Sstevel@tonic-gate return (EINVAL);
21630Sstevel@tonic-gate
21640Sstevel@tonic-gate case EINVAL:
21650Sstevel@tonic-gate default:
21660Sstevel@tonic-gate bad_error("libscf_fmri_get_instance", err);
21670Sstevel@tonic-gate }
21680Sstevel@tonic-gate }
21690Sstevel@tonic-gate
21700Sstevel@tonic-gate return (0);
21710Sstevel@tonic-gate }
21720Sstevel@tonic-gate
21730Sstevel@tonic-gate
21740Sstevel@tonic-gate /*
21750Sstevel@tonic-gate * Add all of the instances of the service named by fmri to the graph.
21760Sstevel@tonic-gate * Returns
21770Sstevel@tonic-gate * 0 - success
21780Sstevel@tonic-gate * ENOENT - service indicated by fmri does not exist
21790Sstevel@tonic-gate *
21800Sstevel@tonic-gate * In both cases *reboundp will be B_TRUE if the handle was rebound, or B_FALSE
21810Sstevel@tonic-gate * otherwise.
21820Sstevel@tonic-gate */
21830Sstevel@tonic-gate static int
add_service(const char * fmri,scf_handle_t * h,boolean_t * reboundp)21840Sstevel@tonic-gate add_service(const char *fmri, scf_handle_t *h, boolean_t *reboundp)
21850Sstevel@tonic-gate {
21860Sstevel@tonic-gate scf_service_t *svc;
21870Sstevel@tonic-gate scf_instance_t *inst;
21880Sstevel@tonic-gate scf_iter_t *iter;
21890Sstevel@tonic-gate char *inst_fmri;
21900Sstevel@tonic-gate int ret, r;
21910Sstevel@tonic-gate
21920Sstevel@tonic-gate *reboundp = B_FALSE;
21930Sstevel@tonic-gate
21940Sstevel@tonic-gate svc = safe_scf_service_create(h);
21950Sstevel@tonic-gate inst = safe_scf_instance_create(h);
21960Sstevel@tonic-gate iter = safe_scf_iter_create(h);
21970Sstevel@tonic-gate inst_fmri = startd_alloc(max_scf_fmri_size);
21980Sstevel@tonic-gate
21990Sstevel@tonic-gate rebound:
22000Sstevel@tonic-gate if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL,
22010Sstevel@tonic-gate SCF_DECODE_FMRI_EXACT) != 0) {
22020Sstevel@tonic-gate switch (scf_error()) {
22030Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
22040Sstevel@tonic-gate default:
22050Sstevel@tonic-gate libscf_handle_rebind(h);
22060Sstevel@tonic-gate *reboundp = B_TRUE;
22070Sstevel@tonic-gate goto rebound;
22080Sstevel@tonic-gate
22090Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
22100Sstevel@tonic-gate ret = ENOENT;
22110Sstevel@tonic-gate goto out;
22120Sstevel@tonic-gate
22130Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
22140Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
22150Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
22160Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
22170Sstevel@tonic-gate bad_error("scf_handle_decode_fmri", scf_error());
22180Sstevel@tonic-gate }
22190Sstevel@tonic-gate }
22200Sstevel@tonic-gate
22210Sstevel@tonic-gate if (scf_iter_service_instances(iter, svc) != 0) {
22220Sstevel@tonic-gate switch (scf_error()) {
22230Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
22240Sstevel@tonic-gate default:
22250Sstevel@tonic-gate libscf_handle_rebind(h);
22260Sstevel@tonic-gate *reboundp = B_TRUE;
22270Sstevel@tonic-gate goto rebound;
22280Sstevel@tonic-gate
22290Sstevel@tonic-gate case SCF_ERROR_DELETED:
22300Sstevel@tonic-gate ret = ENOENT;
22310Sstevel@tonic-gate goto out;
22320Sstevel@tonic-gate
22330Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
22340Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
22350Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
22365040Swesolows bad_error("scf_iter_service_instances", scf_error());
22370Sstevel@tonic-gate }
22380Sstevel@tonic-gate }
22390Sstevel@tonic-gate
22400Sstevel@tonic-gate for (;;) {
22410Sstevel@tonic-gate r = scf_iter_next_instance(iter, inst);
22420Sstevel@tonic-gate if (r == 0)
22430Sstevel@tonic-gate break;
22440Sstevel@tonic-gate if (r != 1) {
22450Sstevel@tonic-gate switch (scf_error()) {
22460Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
22470Sstevel@tonic-gate default:
22480Sstevel@tonic-gate libscf_handle_rebind(h);
22490Sstevel@tonic-gate *reboundp = B_TRUE;
22500Sstevel@tonic-gate goto rebound;
22510Sstevel@tonic-gate
22520Sstevel@tonic-gate case SCF_ERROR_DELETED:
22530Sstevel@tonic-gate ret = ENOENT;
22540Sstevel@tonic-gate goto out;
22550Sstevel@tonic-gate
22560Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
22570Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
22580Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
22590Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
22600Sstevel@tonic-gate bad_error("scf_iter_next_instance",
22610Sstevel@tonic-gate scf_error());
22620Sstevel@tonic-gate }
22630Sstevel@tonic-gate }
22640Sstevel@tonic-gate
22650Sstevel@tonic-gate if (scf_instance_to_fmri(inst, inst_fmri, max_scf_fmri_size) <
22660Sstevel@tonic-gate 0) {
22670Sstevel@tonic-gate switch (scf_error()) {
22680Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
22690Sstevel@tonic-gate libscf_handle_rebind(h);
22700Sstevel@tonic-gate *reboundp = B_TRUE;
22710Sstevel@tonic-gate goto rebound;
22720Sstevel@tonic-gate
22730Sstevel@tonic-gate case SCF_ERROR_DELETED:
22740Sstevel@tonic-gate continue;
22750Sstevel@tonic-gate
22760Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
22770Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
22780Sstevel@tonic-gate bad_error("scf_instance_to_fmri", scf_error());
22790Sstevel@tonic-gate }
22800Sstevel@tonic-gate }
22810Sstevel@tonic-gate
22820Sstevel@tonic-gate r = dgraph_add_instance(inst_fmri, inst, B_FALSE);
22830Sstevel@tonic-gate switch (r) {
22840Sstevel@tonic-gate case 0:
22850Sstevel@tonic-gate case ECANCELED:
22860Sstevel@tonic-gate break;
22870Sstevel@tonic-gate
22880Sstevel@tonic-gate case EEXIST:
22890Sstevel@tonic-gate continue;
22900Sstevel@tonic-gate
22910Sstevel@tonic-gate case ECONNABORTED:
22920Sstevel@tonic-gate libscf_handle_rebind(h);
22930Sstevel@tonic-gate *reboundp = B_TRUE;
22940Sstevel@tonic-gate goto rebound;
22950Sstevel@tonic-gate
22960Sstevel@tonic-gate case EINVAL:
22970Sstevel@tonic-gate default:
22980Sstevel@tonic-gate bad_error("dgraph_add_instance", r);
22990Sstevel@tonic-gate }
23000Sstevel@tonic-gate }
23010Sstevel@tonic-gate
23020Sstevel@tonic-gate ret = 0;
23030Sstevel@tonic-gate
23040Sstevel@tonic-gate out:
23050Sstevel@tonic-gate startd_free(inst_fmri, max_scf_fmri_size);
23060Sstevel@tonic-gate scf_iter_destroy(iter);
23070Sstevel@tonic-gate scf_instance_destroy(inst);
23080Sstevel@tonic-gate scf_service_destroy(svc);
23090Sstevel@tonic-gate return (ret);
23100Sstevel@tonic-gate }
23110Sstevel@tonic-gate
23120Sstevel@tonic-gate struct depfmri_info {
23130Sstevel@tonic-gate graph_vertex_t *v; /* GVT_GROUP vertex */
23140Sstevel@tonic-gate gv_type_t type; /* type of dependency */
23150Sstevel@tonic-gate const char *inst_fmri; /* FMRI of parental GVT_INST vert. */
23160Sstevel@tonic-gate const char *pg_name; /* Name of dependency pg */
23170Sstevel@tonic-gate scf_handle_t *h;
23180Sstevel@tonic-gate int err; /* return error code */
23190Sstevel@tonic-gate int **pathp; /* return circular dependency path */
23200Sstevel@tonic-gate };
23210Sstevel@tonic-gate
23220Sstevel@tonic-gate /*
23230Sstevel@tonic-gate * Find or create a vertex for fmri and make info->v depend on it.
23240Sstevel@tonic-gate * Returns
23250Sstevel@tonic-gate * 0 - success
23260Sstevel@tonic-gate * nonzero - failure
23270Sstevel@tonic-gate *
23280Sstevel@tonic-gate * On failure, sets info->err to
23290Sstevel@tonic-gate * EINVAL - fmri is invalid
23300Sstevel@tonic-gate * fmri does not match info->type
23310Sstevel@tonic-gate * ELOOP - Adding the dependency creates a circular dependency. *info->pathp
23320Sstevel@tonic-gate * will point to an array of the ids of the members of the cycle.
23330Sstevel@tonic-gate * ECONNABORTED - repository connection was broken
23340Sstevel@tonic-gate * ECONNRESET - succeeded, but repository connection was reset
23350Sstevel@tonic-gate */
23360Sstevel@tonic-gate static int
process_dependency_fmri(const char * fmri,struct depfmri_info * info)23370Sstevel@tonic-gate process_dependency_fmri(const char *fmri, struct depfmri_info *info)
23380Sstevel@tonic-gate {
23390Sstevel@tonic-gate int err;
23400Sstevel@tonic-gate graph_vertex_t *depgroup_v, *v;
23410Sstevel@tonic-gate char *fmri_copy, *cfmri;
23420Sstevel@tonic-gate size_t fmri_copy_sz;
23430Sstevel@tonic-gate const char *scope, *service, *instance, *pg;
23440Sstevel@tonic-gate scf_instance_t *inst;
23450Sstevel@tonic-gate boolean_t rebound;
23460Sstevel@tonic-gate
234711466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
23480Sstevel@tonic-gate
23490Sstevel@tonic-gate /* Get or create vertex for FMRI */
23500Sstevel@tonic-gate depgroup_v = info->v;
23510Sstevel@tonic-gate
23520Sstevel@tonic-gate if (strncmp(fmri, "file:", sizeof ("file:") - 1) == 0) {
23530Sstevel@tonic-gate if (info->type != GVT_FILE) {
23540Sstevel@tonic-gate log_framework(LOG_NOTICE,
23550Sstevel@tonic-gate "FMRI \"%s\" is not allowed for the \"%s\" "
23560Sstevel@tonic-gate "dependency's type of instance %s.\n", fmri,
23570Sstevel@tonic-gate info->pg_name, info->inst_fmri);
23580Sstevel@tonic-gate return (info->err = EINVAL);
23590Sstevel@tonic-gate }
23600Sstevel@tonic-gate
23610Sstevel@tonic-gate err = graph_insert_vertex_unconfigured(fmri, info->type, 0,
23620Sstevel@tonic-gate RERR_NONE, &v);
23630Sstevel@tonic-gate switch (err) {
23640Sstevel@tonic-gate case 0:
23650Sstevel@tonic-gate break;
23660Sstevel@tonic-gate
23670Sstevel@tonic-gate case EEXIST:
23680Sstevel@tonic-gate assert(v->gv_type == GVT_FILE);
23690Sstevel@tonic-gate break;
23700Sstevel@tonic-gate
23710Sstevel@tonic-gate case EINVAL: /* prevented above */
23720Sstevel@tonic-gate default:
23730Sstevel@tonic-gate bad_error("graph_insert_vertex_unconfigured", err);
23740Sstevel@tonic-gate }
23750Sstevel@tonic-gate } else {
23760Sstevel@tonic-gate if (info->type != GVT_INST) {
23770Sstevel@tonic-gate log_framework(LOG_NOTICE,
23780Sstevel@tonic-gate "FMRI \"%s\" is not allowed for the \"%s\" "
23790Sstevel@tonic-gate "dependency's type of instance %s.\n", fmri,
23800Sstevel@tonic-gate info->pg_name, info->inst_fmri);
23810Sstevel@tonic-gate return (info->err = EINVAL);
23820Sstevel@tonic-gate }
23830Sstevel@tonic-gate
23840Sstevel@tonic-gate /*
23850Sstevel@tonic-gate * We must canonify fmri & add a vertex for it.
23860Sstevel@tonic-gate */
23870Sstevel@tonic-gate fmri_copy_sz = strlen(fmri) + 1;
23880Sstevel@tonic-gate fmri_copy = startd_alloc(fmri_copy_sz);
23890Sstevel@tonic-gate (void) strcpy(fmri_copy, fmri);
23900Sstevel@tonic-gate
23910Sstevel@tonic-gate /* Determine if the FMRI is a property group or instance */
23920Sstevel@tonic-gate if (scf_parse_svc_fmri(fmri_copy, &scope, &service,
23930Sstevel@tonic-gate &instance, &pg, NULL) != 0) {
23940Sstevel@tonic-gate startd_free(fmri_copy, fmri_copy_sz);
23950Sstevel@tonic-gate log_framework(LOG_NOTICE,
23960Sstevel@tonic-gate "Dependency \"%s\" of %s has invalid FMRI "
23970Sstevel@tonic-gate "\"%s\".\n", info->pg_name, info->inst_fmri,
23980Sstevel@tonic-gate fmri);
23990Sstevel@tonic-gate return (info->err = EINVAL);
24000Sstevel@tonic-gate }
24010Sstevel@tonic-gate
24020Sstevel@tonic-gate if (service == NULL || pg != NULL) {
24030Sstevel@tonic-gate startd_free(fmri_copy, fmri_copy_sz);
24040Sstevel@tonic-gate log_framework(LOG_NOTICE,
24050Sstevel@tonic-gate "Dependency \"%s\" of %s does not designate a "
24060Sstevel@tonic-gate "service or instance.\n", info->pg_name,
24070Sstevel@tonic-gate info->inst_fmri);
24080Sstevel@tonic-gate return (info->err = EINVAL);
24090Sstevel@tonic-gate }
24100Sstevel@tonic-gate
24110Sstevel@tonic-gate if (scope == NULL || strcmp(scope, SCF_SCOPE_LOCAL) == 0) {
24120Sstevel@tonic-gate cfmri = uu_msprintf("svc:/%s%s%s",
24130Sstevel@tonic-gate service, instance ? ":" : "", instance ? instance :
24140Sstevel@tonic-gate "");
24150Sstevel@tonic-gate } else {
24160Sstevel@tonic-gate cfmri = uu_msprintf("svc://%s/%s%s%s",
24170Sstevel@tonic-gate scope, service, instance ? ":" : "", instance ?
24180Sstevel@tonic-gate instance : "");
24190Sstevel@tonic-gate }
24200Sstevel@tonic-gate
24210Sstevel@tonic-gate startd_free(fmri_copy, fmri_copy_sz);
24220Sstevel@tonic-gate
24230Sstevel@tonic-gate err = graph_insert_vertex_unconfigured(cfmri, instance ?
24240Sstevel@tonic-gate GVT_INST : GVT_SVC, instance ? 0 : DEPGRP_REQUIRE_ANY,
24250Sstevel@tonic-gate RERR_NONE, &v);
24260Sstevel@tonic-gate uu_free(cfmri);
24270Sstevel@tonic-gate switch (err) {
24280Sstevel@tonic-gate case 0:
24290Sstevel@tonic-gate break;
24300Sstevel@tonic-gate
24310Sstevel@tonic-gate case EEXIST:
24320Sstevel@tonic-gate /* Verify v. */
24330Sstevel@tonic-gate if (instance != NULL)
24340Sstevel@tonic-gate assert(v->gv_type == GVT_INST);
24350Sstevel@tonic-gate else
24360Sstevel@tonic-gate assert(v->gv_type == GVT_SVC);
24370Sstevel@tonic-gate break;
24380Sstevel@tonic-gate
24390Sstevel@tonic-gate default:
24400Sstevel@tonic-gate bad_error("graph_insert_vertex_unconfigured", err);
24410Sstevel@tonic-gate }
24420Sstevel@tonic-gate }
24430Sstevel@tonic-gate
24440Sstevel@tonic-gate /* Add dependency from depgroup_v to new vertex */
24450Sstevel@tonic-gate info->err = graph_insert_dependency(depgroup_v, v, info->pathp);
24460Sstevel@tonic-gate switch (info->err) {
24470Sstevel@tonic-gate case 0:
24480Sstevel@tonic-gate break;
24490Sstevel@tonic-gate
24500Sstevel@tonic-gate case ELOOP:
24510Sstevel@tonic-gate return (ELOOP);
24520Sstevel@tonic-gate
24530Sstevel@tonic-gate default:
24540Sstevel@tonic-gate bad_error("graph_insert_dependency", info->err);
24550Sstevel@tonic-gate }
24560Sstevel@tonic-gate
24570Sstevel@tonic-gate /* This must be after we insert the dependency, to avoid looping. */
24580Sstevel@tonic-gate switch (v->gv_type) {
24590Sstevel@tonic-gate case GVT_INST:
24600Sstevel@tonic-gate if ((v->gv_flags & GV_CONFIGURED) != 0)
24610Sstevel@tonic-gate break;
24620Sstevel@tonic-gate
24630Sstevel@tonic-gate inst = safe_scf_instance_create(info->h);
24640Sstevel@tonic-gate
24650Sstevel@tonic-gate rebound = B_FALSE;
24660Sstevel@tonic-gate
24670Sstevel@tonic-gate rebound:
24680Sstevel@tonic-gate err = libscf_lookup_instance(v->gv_name, inst);
24690Sstevel@tonic-gate switch (err) {
24700Sstevel@tonic-gate case 0:
24710Sstevel@tonic-gate err = configure_vertex(v, inst);
24720Sstevel@tonic-gate switch (err) {
24730Sstevel@tonic-gate case 0:
24740Sstevel@tonic-gate case ECANCELED:
24750Sstevel@tonic-gate break;
24760Sstevel@tonic-gate
24770Sstevel@tonic-gate case ECONNABORTED:
24780Sstevel@tonic-gate libscf_handle_rebind(info->h);
24790Sstevel@tonic-gate rebound = B_TRUE;
24800Sstevel@tonic-gate goto rebound;
24810Sstevel@tonic-gate
24820Sstevel@tonic-gate default:
24830Sstevel@tonic-gate bad_error("configure_vertex", err);
24840Sstevel@tonic-gate }
24850Sstevel@tonic-gate break;
24860Sstevel@tonic-gate
24870Sstevel@tonic-gate case ENOENT:
24880Sstevel@tonic-gate break;
24890Sstevel@tonic-gate
24900Sstevel@tonic-gate case ECONNABORTED:
24910Sstevel@tonic-gate libscf_handle_rebind(info->h);
24920Sstevel@tonic-gate rebound = B_TRUE;
24930Sstevel@tonic-gate goto rebound;
24940Sstevel@tonic-gate
24950Sstevel@tonic-gate case EINVAL:
24960Sstevel@tonic-gate case ENOTSUP:
24970Sstevel@tonic-gate default:
24980Sstevel@tonic-gate bad_error("libscf_fmri_get_instance", err);
24990Sstevel@tonic-gate }
25000Sstevel@tonic-gate
25010Sstevel@tonic-gate scf_instance_destroy(inst);
25020Sstevel@tonic-gate
25030Sstevel@tonic-gate if (rebound)
25040Sstevel@tonic-gate return (info->err = ECONNRESET);
25050Sstevel@tonic-gate break;
25060Sstevel@tonic-gate
25070Sstevel@tonic-gate case GVT_SVC:
25080Sstevel@tonic-gate (void) add_service(v->gv_name, info->h, &rebound);
25090Sstevel@tonic-gate if (rebound)
25100Sstevel@tonic-gate return (info->err = ECONNRESET);
25110Sstevel@tonic-gate }
25120Sstevel@tonic-gate
25130Sstevel@tonic-gate return (0);
25140Sstevel@tonic-gate }
25150Sstevel@tonic-gate
25160Sstevel@tonic-gate struct deppg_info {
25170Sstevel@tonic-gate graph_vertex_t *v; /* GVT_INST vertex */
25180Sstevel@tonic-gate int err; /* return error */
25190Sstevel@tonic-gate int **pathp; /* return circular dependency path */
25200Sstevel@tonic-gate };
25210Sstevel@tonic-gate
25220Sstevel@tonic-gate /*
25230Sstevel@tonic-gate * Make info->v depend on a new GVT_GROUP node for this property group,
25240Sstevel@tonic-gate * and then call process_dependency_fmri() for the values of the entity
25250Sstevel@tonic-gate * property. Return 0 on success, or if something goes wrong return nonzero
25260Sstevel@tonic-gate * and set info->err to ECONNABORTED, EINVAL, or the error code returned by
25270Sstevel@tonic-gate * process_dependency_fmri().
25280Sstevel@tonic-gate */
25290Sstevel@tonic-gate static int
process_dependency_pg(scf_propertygroup_t * pg,struct deppg_info * info)25300Sstevel@tonic-gate process_dependency_pg(scf_propertygroup_t *pg, struct deppg_info *info)
25310Sstevel@tonic-gate {
25320Sstevel@tonic-gate scf_handle_t *h;
25330Sstevel@tonic-gate depgroup_type_t deptype;
25342704Srm88369 restarter_error_t rerr;
25350Sstevel@tonic-gate struct depfmri_info linfo;
25360Sstevel@tonic-gate char *fmri, *pg_name;
25370Sstevel@tonic-gate size_t fmri_sz;
25380Sstevel@tonic-gate graph_vertex_t *depgrp;
25390Sstevel@tonic-gate scf_property_t *prop;
25400Sstevel@tonic-gate int err;
25410Sstevel@tonic-gate int empty;
25420Sstevel@tonic-gate scf_error_t scferr;
25430Sstevel@tonic-gate ssize_t len;
25440Sstevel@tonic-gate
254511466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
25460Sstevel@tonic-gate
25470Sstevel@tonic-gate h = scf_pg_handle(pg);
25480Sstevel@tonic-gate
25490Sstevel@tonic-gate pg_name = startd_alloc(max_scf_name_size);
25500Sstevel@tonic-gate
25510Sstevel@tonic-gate len = scf_pg_get_name(pg, pg_name, max_scf_name_size);
25520Sstevel@tonic-gate if (len < 0) {
25530Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size);
25540Sstevel@tonic-gate switch (scf_error()) {
25550Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
25560Sstevel@tonic-gate default:
25570Sstevel@tonic-gate return (info->err = ECONNABORTED);
25580Sstevel@tonic-gate
25590Sstevel@tonic-gate case SCF_ERROR_DELETED:
25600Sstevel@tonic-gate return (info->err = 0);
25610Sstevel@tonic-gate
25620Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
25630Sstevel@tonic-gate bad_error("scf_pg_get_name", scf_error());
25640Sstevel@tonic-gate }
25650Sstevel@tonic-gate }
25660Sstevel@tonic-gate
25670Sstevel@tonic-gate /*
25680Sstevel@tonic-gate * Skip over empty dependency groups. Since dependency property
25690Sstevel@tonic-gate * groups are updated atomically, they are either empty or
25700Sstevel@tonic-gate * fully populated.
25710Sstevel@tonic-gate */
25720Sstevel@tonic-gate empty = depgroup_empty(h, pg);
25730Sstevel@tonic-gate if (empty < 0) {
25740Sstevel@tonic-gate log_error(LOG_INFO,
25750Sstevel@tonic-gate "Error reading dependency group \"%s\" of %s: %s\n",
25760Sstevel@tonic-gate pg_name, info->v->gv_name, scf_strerror(scf_error()));
25770Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size);
25780Sstevel@tonic-gate return (info->err = EINVAL);
25790Sstevel@tonic-gate
25800Sstevel@tonic-gate } else if (empty == 1) {
25810Sstevel@tonic-gate log_framework(LOG_DEBUG,
25820Sstevel@tonic-gate "Ignoring empty dependency group \"%s\" of %s\n",
25830Sstevel@tonic-gate pg_name, info->v->gv_name);
25840Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size);
25850Sstevel@tonic-gate return (info->err = 0);
25860Sstevel@tonic-gate }
25870Sstevel@tonic-gate
25880Sstevel@tonic-gate fmri_sz = strlen(info->v->gv_name) + 1 + len + 1;
25890Sstevel@tonic-gate fmri = startd_alloc(fmri_sz);
25900Sstevel@tonic-gate
25910Sstevel@tonic-gate (void) snprintf(fmri, max_scf_name_size, "%s>%s", info->v->gv_name,
25920Sstevel@tonic-gate pg_name);
25930Sstevel@tonic-gate
25940Sstevel@tonic-gate /* Validate the pg before modifying the graph */
25950Sstevel@tonic-gate deptype = depgroup_read_grouping(h, pg);
25960Sstevel@tonic-gate if (deptype == DEPGRP_UNSUPPORTED) {
25970Sstevel@tonic-gate log_error(LOG_INFO,
25980Sstevel@tonic-gate "Dependency \"%s\" of %s has an unknown grouping value.\n",
25990Sstevel@tonic-gate pg_name, info->v->gv_name);
26000Sstevel@tonic-gate startd_free(fmri, fmri_sz);
26010Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size);
26020Sstevel@tonic-gate return (info->err = EINVAL);
26030Sstevel@tonic-gate }
26040Sstevel@tonic-gate
26052704Srm88369 rerr = depgroup_read_restart(h, pg);
26062704Srm88369 if (rerr == RERR_UNSUPPORTED) {
26072704Srm88369 log_error(LOG_INFO,
26082704Srm88369 "Dependency \"%s\" of %s has an unknown restart_on value."
26092704Srm88369 "\n", pg_name, info->v->gv_name);
26102704Srm88369 startd_free(fmri, fmri_sz);
26112704Srm88369 startd_free(pg_name, max_scf_name_size);
26122704Srm88369 return (info->err = EINVAL);
26132704Srm88369 }
26142704Srm88369
26150Sstevel@tonic-gate prop = safe_scf_property_create(h);
26160Sstevel@tonic-gate
26170Sstevel@tonic-gate if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0) {
26180Sstevel@tonic-gate scferr = scf_error();
26190Sstevel@tonic-gate scf_property_destroy(prop);
26200Sstevel@tonic-gate if (scferr == SCF_ERROR_DELETED) {
26210Sstevel@tonic-gate startd_free(fmri, fmri_sz);
26220Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size);
26230Sstevel@tonic-gate return (info->err = 0);
26240Sstevel@tonic-gate } else if (scferr != SCF_ERROR_NOT_FOUND) {
26250Sstevel@tonic-gate startd_free(fmri, fmri_sz);
26260Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size);
26270Sstevel@tonic-gate return (info->err = ECONNABORTED);
26280Sstevel@tonic-gate }
26290Sstevel@tonic-gate
26300Sstevel@tonic-gate log_error(LOG_INFO,
26310Sstevel@tonic-gate "Dependency \"%s\" of %s is missing a \"%s\" property.\n",
26320Sstevel@tonic-gate pg_name, info->v->gv_name, SCF_PROPERTY_ENTITIES);
26330Sstevel@tonic-gate
26340Sstevel@tonic-gate startd_free(fmri, fmri_sz);
26350Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size);
26360Sstevel@tonic-gate
26370Sstevel@tonic-gate return (info->err = EINVAL);
26380Sstevel@tonic-gate }
26390Sstevel@tonic-gate
26400Sstevel@tonic-gate /* Create depgroup vertex for pg */
26410Sstevel@tonic-gate err = graph_insert_vertex_unconfigured(fmri, GVT_GROUP, deptype,
26422704Srm88369 rerr, &depgrp);
26430Sstevel@tonic-gate assert(err == 0);
26440Sstevel@tonic-gate startd_free(fmri, fmri_sz);
26450Sstevel@tonic-gate
26460Sstevel@tonic-gate /* Add dependency from inst vertex to new vertex */
26470Sstevel@tonic-gate err = graph_insert_dependency(info->v, depgrp, info->pathp);
26480Sstevel@tonic-gate /* ELOOP can't happen because this should be a new vertex */
26490Sstevel@tonic-gate assert(err == 0);
26500Sstevel@tonic-gate
26510Sstevel@tonic-gate linfo.v = depgrp;
26520Sstevel@tonic-gate linfo.type = depgroup_read_scheme(h, pg);
26530Sstevel@tonic-gate linfo.inst_fmri = info->v->gv_name;
26540Sstevel@tonic-gate linfo.pg_name = pg_name;
26550Sstevel@tonic-gate linfo.h = h;
26560Sstevel@tonic-gate linfo.err = 0;
26570Sstevel@tonic-gate linfo.pathp = info->pathp;
26580Sstevel@tonic-gate err = walk_property_astrings(prop, (callback_t)process_dependency_fmri,
26590Sstevel@tonic-gate &linfo);
26600Sstevel@tonic-gate
26610Sstevel@tonic-gate scf_property_destroy(prop);
26620Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size);
26630Sstevel@tonic-gate
26640Sstevel@tonic-gate switch (err) {
26650Sstevel@tonic-gate case 0:
26660Sstevel@tonic-gate case EINTR:
26670Sstevel@tonic-gate return (info->err = linfo.err);
26680Sstevel@tonic-gate
26690Sstevel@tonic-gate case ECONNABORTED:
26700Sstevel@tonic-gate case EINVAL:
26710Sstevel@tonic-gate return (info->err = err);
26720Sstevel@tonic-gate
26730Sstevel@tonic-gate case ECANCELED:
26740Sstevel@tonic-gate return (info->err = 0);
26750Sstevel@tonic-gate
26760Sstevel@tonic-gate case ECONNRESET:
26770Sstevel@tonic-gate return (info->err = ECONNABORTED);
26780Sstevel@tonic-gate
26790Sstevel@tonic-gate default:
26800Sstevel@tonic-gate bad_error("walk_property_astrings", err);
26810Sstevel@tonic-gate /* NOTREACHED */
26820Sstevel@tonic-gate }
26830Sstevel@tonic-gate }
26840Sstevel@tonic-gate
26850Sstevel@tonic-gate /*
26860Sstevel@tonic-gate * Build the dependency info for v from the repository. Returns 0 on success,
26870Sstevel@tonic-gate * ECONNABORTED on repository disconnection, EINVAL if the repository
26880Sstevel@tonic-gate * configuration is invalid, and ELOOP if a dependency would cause a cycle.
26890Sstevel@tonic-gate * In the last case, *pathp will point to a -1-terminated array of ids which
26900Sstevel@tonic-gate * constitute the rest of the dependency cycle.
26910Sstevel@tonic-gate */
26920Sstevel@tonic-gate static int
set_dependencies(graph_vertex_t * v,scf_instance_t * inst,int ** pathp)26930Sstevel@tonic-gate set_dependencies(graph_vertex_t *v, scf_instance_t *inst, int **pathp)
26940Sstevel@tonic-gate {
26950Sstevel@tonic-gate struct deppg_info info;
26960Sstevel@tonic-gate int err;
26970Sstevel@tonic-gate uint_t old_configured;
26980Sstevel@tonic-gate
269911466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
27000Sstevel@tonic-gate
27010Sstevel@tonic-gate /*
27020Sstevel@tonic-gate * Mark the vertex as configured during dependency insertion to avoid
27030Sstevel@tonic-gate * dependency cycles (which can appear in the graph if one of the
27040Sstevel@tonic-gate * vertices is an exclusion-group).
27050Sstevel@tonic-gate */
27060Sstevel@tonic-gate old_configured = v->gv_flags & GV_CONFIGURED;
27070Sstevel@tonic-gate v->gv_flags |= GV_CONFIGURED;
27080Sstevel@tonic-gate
27090Sstevel@tonic-gate info.err = 0;
27100Sstevel@tonic-gate info.v = v;
27110Sstevel@tonic-gate info.pathp = pathp;
27120Sstevel@tonic-gate
27130Sstevel@tonic-gate err = walk_dependency_pgs(inst, (callback_t)process_dependency_pg,
27140Sstevel@tonic-gate &info);
27150Sstevel@tonic-gate
27160Sstevel@tonic-gate if (!old_configured)
27170Sstevel@tonic-gate v->gv_flags &= ~GV_CONFIGURED;
27180Sstevel@tonic-gate
27190Sstevel@tonic-gate switch (err) {
27200Sstevel@tonic-gate case 0:
27210Sstevel@tonic-gate case EINTR:
27220Sstevel@tonic-gate return (info.err);
27230Sstevel@tonic-gate
27240Sstevel@tonic-gate case ECONNABORTED:
27250Sstevel@tonic-gate return (ECONNABORTED);
27260Sstevel@tonic-gate
27270Sstevel@tonic-gate case ECANCELED:
27280Sstevel@tonic-gate /* Should get delete event, so return 0. */
27290Sstevel@tonic-gate return (0);
27300Sstevel@tonic-gate
27310Sstevel@tonic-gate default:
27320Sstevel@tonic-gate bad_error("walk_dependency_pgs", err);
27330Sstevel@tonic-gate /* NOTREACHED */
27340Sstevel@tonic-gate }
27350Sstevel@tonic-gate }
27360Sstevel@tonic-gate
27370Sstevel@tonic-gate
27380Sstevel@tonic-gate static void
handle_cycle(const char * fmri,int * path)27390Sstevel@tonic-gate handle_cycle(const char *fmri, int *path)
27400Sstevel@tonic-gate {
27410Sstevel@tonic-gate const char *cp;
27420Sstevel@tonic-gate size_t sz;
27430Sstevel@tonic-gate
274411466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
27450Sstevel@tonic-gate
27460Sstevel@tonic-gate path_to_str(path, (char **)&cp, &sz);
27470Sstevel@tonic-gate
27481958Slianep log_error(LOG_ERR, "Transitioning %s to maintenance "
27491958Slianep "because it completes a dependency cycle (see svcs -xv for "
27501958Slianep "details):\n%s", fmri ? fmri : "?", cp);
27510Sstevel@tonic-gate
27520Sstevel@tonic-gate startd_free((void *)cp, sz);
27530Sstevel@tonic-gate }
27540Sstevel@tonic-gate
27550Sstevel@tonic-gate /*
27561712Srm88369 * Increment the vertex's reference count to prevent the vertex removal
27571712Srm88369 * from the dgraph.
27581712Srm88369 */
27591712Srm88369 static void
vertex_ref(graph_vertex_t * v)27601712Srm88369 vertex_ref(graph_vertex_t *v)
27611712Srm88369 {
276211466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
27631712Srm88369
27641712Srm88369 v->gv_refs++;
27651712Srm88369 }
27661712Srm88369
27671712Srm88369 /*
27681712Srm88369 * Decrement the vertex's reference count and remove the vertex from
27691712Srm88369 * the dgraph when possible.
27701712Srm88369 *
27711712Srm88369 * Return VERTEX_REMOVED when the vertex has been removed otherwise
27721712Srm88369 * return VERTEX_INUSE.
27730Sstevel@tonic-gate */
27740Sstevel@tonic-gate static int
vertex_unref(graph_vertex_t * v)27751712Srm88369 vertex_unref(graph_vertex_t *v)
27761712Srm88369 {
277711466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
27781712Srm88369 assert(v->gv_refs > 0);
27791712Srm88369
27801712Srm88369 v->gv_refs--;
27811712Srm88369
27821712Srm88369 return (free_if_unrefed(v));
27831712Srm88369 }
27841712Srm88369
27851712Srm88369 /*
27861712Srm88369 * When run on the dependencies of a vertex, populates list with
27871712Srm88369 * graph_edge_t's which point to the service vertices or the instance
27881712Srm88369 * vertices (no GVT_GROUP nodes) on which the vertex depends.
27891712Srm88369 *
27901712Srm88369 * Increment the vertex's reference count once the vertex is inserted
27911712Srm88369 * in the list. The vertex won't be able to be deleted from the dgraph
27921712Srm88369 * while it is referenced.
27931712Srm88369 */
27941712Srm88369 static int
append_svcs_or_insts(graph_edge_t * e,uu_list_t * list)27951712Srm88369 append_svcs_or_insts(graph_edge_t *e, uu_list_t *list)
27960Sstevel@tonic-gate {
27970Sstevel@tonic-gate graph_vertex_t *v = e->ge_vertex;
27980Sstevel@tonic-gate graph_edge_t *new;
27990Sstevel@tonic-gate int r;
28000Sstevel@tonic-gate
28010Sstevel@tonic-gate switch (v->gv_type) {
28020Sstevel@tonic-gate case GVT_INST:
28030Sstevel@tonic-gate case GVT_SVC:
28040Sstevel@tonic-gate break;
28050Sstevel@tonic-gate
28060Sstevel@tonic-gate case GVT_GROUP:
28070Sstevel@tonic-gate r = uu_list_walk(v->gv_dependencies,
28081712Srm88369 (uu_walk_fn_t *)append_svcs_or_insts, list, 0);
28090Sstevel@tonic-gate assert(r == 0);
28100Sstevel@tonic-gate return (UU_WALK_NEXT);
28110Sstevel@tonic-gate
28120Sstevel@tonic-gate case GVT_FILE:
28130Sstevel@tonic-gate return (UU_WALK_NEXT);
28140Sstevel@tonic-gate
28150Sstevel@tonic-gate default:
28160Sstevel@tonic-gate #ifndef NDEBUG
28170Sstevel@tonic-gate uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__,
28180Sstevel@tonic-gate __LINE__, v->gv_type);
28190Sstevel@tonic-gate #endif
28200Sstevel@tonic-gate abort();
28210Sstevel@tonic-gate }
28220Sstevel@tonic-gate
28230Sstevel@tonic-gate new = startd_alloc(sizeof (*new));
28240Sstevel@tonic-gate new->ge_vertex = v;
28250Sstevel@tonic-gate uu_list_node_init(new, &new->ge_link, graph_edge_pool);
28260Sstevel@tonic-gate r = uu_list_insert_before(list, NULL, new);
28270Sstevel@tonic-gate assert(r == 0);
28281712Srm88369
28291712Srm88369 /*
28301712Srm88369 * Because we are inserting the vertex in a list, we don't want
28311712Srm88369 * the vertex to be freed while the list is in use. In order to
28321712Srm88369 * achieve that, increment the vertex's reference count.
28331712Srm88369 */
28341712Srm88369 vertex_ref(v);
28351712Srm88369
28360Sstevel@tonic-gate return (UU_WALK_NEXT);
28370Sstevel@tonic-gate }
28380Sstevel@tonic-gate
28390Sstevel@tonic-gate static boolean_t
should_be_in_subgraph(graph_vertex_t * v)28400Sstevel@tonic-gate should_be_in_subgraph(graph_vertex_t *v)
28410Sstevel@tonic-gate {
28420Sstevel@tonic-gate graph_edge_t *e;
28430Sstevel@tonic-gate
28440Sstevel@tonic-gate if (v == milestone)
28450Sstevel@tonic-gate return (B_TRUE);
28460Sstevel@tonic-gate
28470Sstevel@tonic-gate /*
28480Sstevel@tonic-gate * v is in the subgraph if any of its dependents are in the subgraph.
28490Sstevel@tonic-gate * Except for EXCLUDE_ALL dependents. And OPTIONAL dependents only
28500Sstevel@tonic-gate * count if we're enabled.
28510Sstevel@tonic-gate */
28520Sstevel@tonic-gate for (e = uu_list_first(v->gv_dependents);
28530Sstevel@tonic-gate e != NULL;
28540Sstevel@tonic-gate e = uu_list_next(v->gv_dependents, e)) {
28550Sstevel@tonic-gate graph_vertex_t *dv = e->ge_vertex;
28560Sstevel@tonic-gate
28570Sstevel@tonic-gate if (!(dv->gv_flags & GV_INSUBGRAPH))
28580Sstevel@tonic-gate continue;
28590Sstevel@tonic-gate
28600Sstevel@tonic-gate /*
28610Sstevel@tonic-gate * Don't include instances that are optional and disabled.
28620Sstevel@tonic-gate */
28630Sstevel@tonic-gate if (v->gv_type == GVT_INST && dv->gv_type == GVT_SVC) {
28640Sstevel@tonic-gate
28650Sstevel@tonic-gate int in = 0;
28660Sstevel@tonic-gate graph_edge_t *ee;
28670Sstevel@tonic-gate
28680Sstevel@tonic-gate for (ee = uu_list_first(dv->gv_dependents);
28690Sstevel@tonic-gate ee != NULL;
28700Sstevel@tonic-gate ee = uu_list_next(dv->gv_dependents, ee)) {
28710Sstevel@tonic-gate
28720Sstevel@tonic-gate graph_vertex_t *ddv = e->ge_vertex;
28730Sstevel@tonic-gate
28740Sstevel@tonic-gate if (ddv->gv_type == GVT_GROUP &&
28750Sstevel@tonic-gate ddv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
28760Sstevel@tonic-gate continue;
28770Sstevel@tonic-gate
28780Sstevel@tonic-gate if (ddv->gv_type == GVT_GROUP &&
28790Sstevel@tonic-gate ddv->gv_depgroup == DEPGRP_OPTIONAL_ALL &&
28800Sstevel@tonic-gate !(v->gv_flags & GV_ENBLD_NOOVR))
28810Sstevel@tonic-gate continue;
28820Sstevel@tonic-gate
28830Sstevel@tonic-gate in = 1;
28840Sstevel@tonic-gate }
28850Sstevel@tonic-gate if (!in)
28860Sstevel@tonic-gate continue;
28870Sstevel@tonic-gate }
28880Sstevel@tonic-gate if (v->gv_type == GVT_INST &&
28890Sstevel@tonic-gate dv->gv_type == GVT_GROUP &&
28900Sstevel@tonic-gate dv->gv_depgroup == DEPGRP_OPTIONAL_ALL &&
28910Sstevel@tonic-gate !(v->gv_flags & GV_ENBLD_NOOVR))
28920Sstevel@tonic-gate continue;
28930Sstevel@tonic-gate
28940Sstevel@tonic-gate /* Don't include excluded services and instances */
28950Sstevel@tonic-gate if (dv->gv_type == GVT_GROUP &&
28960Sstevel@tonic-gate dv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
28970Sstevel@tonic-gate continue;
28980Sstevel@tonic-gate
28990Sstevel@tonic-gate return (B_TRUE);
29000Sstevel@tonic-gate }
29010Sstevel@tonic-gate
29020Sstevel@tonic-gate return (B_FALSE);
29030Sstevel@tonic-gate }
29040Sstevel@tonic-gate
29050Sstevel@tonic-gate /*
29060Sstevel@tonic-gate * Ensures that GV_INSUBGRAPH is set properly for v and its descendents. If
29070Sstevel@tonic-gate * any bits change, manipulate the repository appropriately. Returns 0 or
29080Sstevel@tonic-gate * ECONNABORTED.
29090Sstevel@tonic-gate */
29100Sstevel@tonic-gate static int
eval_subgraph(graph_vertex_t * v,scf_handle_t * h)29110Sstevel@tonic-gate eval_subgraph(graph_vertex_t *v, scf_handle_t *h)
29120Sstevel@tonic-gate {
29130Sstevel@tonic-gate boolean_t old = (v->gv_flags & GV_INSUBGRAPH) != 0;
29140Sstevel@tonic-gate boolean_t new;
29150Sstevel@tonic-gate graph_edge_t *e;
29160Sstevel@tonic-gate scf_instance_t *inst;
29170Sstevel@tonic-gate int ret = 0, r;
29180Sstevel@tonic-gate
29190Sstevel@tonic-gate assert(milestone != NULL && milestone != MILESTONE_NONE);
29200Sstevel@tonic-gate
29210Sstevel@tonic-gate new = should_be_in_subgraph(v);
29220Sstevel@tonic-gate
29230Sstevel@tonic-gate if (new == old)
29240Sstevel@tonic-gate return (0);
29250Sstevel@tonic-gate
29260Sstevel@tonic-gate log_framework(LOG_DEBUG, new ? "Adding %s to the subgraph.\n" :
29270Sstevel@tonic-gate "Removing %s from the subgraph.\n", v->gv_name);
29280Sstevel@tonic-gate
29290Sstevel@tonic-gate v->gv_flags = (v->gv_flags & ~GV_INSUBGRAPH) |
29300Sstevel@tonic-gate (new ? GV_INSUBGRAPH : 0);
29310Sstevel@tonic-gate
29320Sstevel@tonic-gate if (v->gv_type == GVT_INST && (v->gv_flags & GV_CONFIGURED)) {
29330Sstevel@tonic-gate int err;
29340Sstevel@tonic-gate
29350Sstevel@tonic-gate get_inst:
29360Sstevel@tonic-gate err = libscf_fmri_get_instance(h, v->gv_name, &inst);
29370Sstevel@tonic-gate if (err != 0) {
29380Sstevel@tonic-gate switch (err) {
29390Sstevel@tonic-gate case ECONNABORTED:
29400Sstevel@tonic-gate libscf_handle_rebind(h);
29410Sstevel@tonic-gate ret = ECONNABORTED;
29420Sstevel@tonic-gate goto get_inst;
29430Sstevel@tonic-gate
29440Sstevel@tonic-gate case ENOENT:
29450Sstevel@tonic-gate break;
29460Sstevel@tonic-gate
29470Sstevel@tonic-gate case EINVAL:
29480Sstevel@tonic-gate case ENOTSUP:
29490Sstevel@tonic-gate default:
29500Sstevel@tonic-gate bad_error("libscf_fmri_get_instance", err);
29510Sstevel@tonic-gate }
29520Sstevel@tonic-gate } else {
29530Sstevel@tonic-gate const char *f;
29540Sstevel@tonic-gate
29550Sstevel@tonic-gate if (new) {
29560Sstevel@tonic-gate err = libscf_delete_enable_ovr(inst);
29570Sstevel@tonic-gate f = "libscf_delete_enable_ovr";
29580Sstevel@tonic-gate } else {
29590Sstevel@tonic-gate err = libscf_set_enable_ovr(inst, 0);
29600Sstevel@tonic-gate f = "libscf_set_enable_ovr";
29610Sstevel@tonic-gate }
29620Sstevel@tonic-gate scf_instance_destroy(inst);
29630Sstevel@tonic-gate switch (err) {
29640Sstevel@tonic-gate case 0:
29650Sstevel@tonic-gate case ECANCELED:
29660Sstevel@tonic-gate break;
29670Sstevel@tonic-gate
29680Sstevel@tonic-gate case ECONNABORTED:
29690Sstevel@tonic-gate libscf_handle_rebind(h);
29700Sstevel@tonic-gate /*
29710Sstevel@tonic-gate * We must continue so the graph is updated,
29720Sstevel@tonic-gate * but we must return ECONNABORTED so any
29730Sstevel@tonic-gate * libscf state held by any callers is reset.
29740Sstevel@tonic-gate */
29750Sstevel@tonic-gate ret = ECONNABORTED;
29760Sstevel@tonic-gate goto get_inst;
29770Sstevel@tonic-gate
29780Sstevel@tonic-gate case EROFS:
29790Sstevel@tonic-gate case EPERM:
29800Sstevel@tonic-gate log_error(LOG_WARNING,
29810Sstevel@tonic-gate "Could not set %s/%s for %s: %s.\n",
29820Sstevel@tonic-gate SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
29830Sstevel@tonic-gate v->gv_name, strerror(err));
29840Sstevel@tonic-gate break;
29850Sstevel@tonic-gate
29860Sstevel@tonic-gate default:
29870Sstevel@tonic-gate bad_error(f, err);
29880Sstevel@tonic-gate }
29890Sstevel@tonic-gate }
29900Sstevel@tonic-gate }
29910Sstevel@tonic-gate
29920Sstevel@tonic-gate for (e = uu_list_first(v->gv_dependencies);
29930Sstevel@tonic-gate e != NULL;
29940Sstevel@tonic-gate e = uu_list_next(v->gv_dependencies, e)) {
29950Sstevel@tonic-gate r = eval_subgraph(e->ge_vertex, h);
29960Sstevel@tonic-gate if (r != 0) {
29970Sstevel@tonic-gate assert(r == ECONNABORTED);
29980Sstevel@tonic-gate ret = ECONNABORTED;
29990Sstevel@tonic-gate }
30000Sstevel@tonic-gate }
30010Sstevel@tonic-gate
30020Sstevel@tonic-gate return (ret);
30030Sstevel@tonic-gate }
30040Sstevel@tonic-gate
30050Sstevel@tonic-gate /*
30060Sstevel@tonic-gate * Delete the (property group) dependencies of v & create new ones based on
30070Sstevel@tonic-gate * inst. If doing so would create a cycle, log a message and put the instance
30080Sstevel@tonic-gate * into maintenance. Update GV_INSUBGRAPH flags as necessary. Returns 0 or
30090Sstevel@tonic-gate * ECONNABORTED.
30100Sstevel@tonic-gate */
30111958Slianep int
refresh_vertex(graph_vertex_t * v,scf_instance_t * inst)30120Sstevel@tonic-gate refresh_vertex(graph_vertex_t *v, scf_instance_t *inst)
30130Sstevel@tonic-gate {
30140Sstevel@tonic-gate int err;
30150Sstevel@tonic-gate int *path;
30160Sstevel@tonic-gate char *fmri;
30170Sstevel@tonic-gate int r;
30180Sstevel@tonic-gate scf_handle_t *h = scf_instance_handle(inst);
30190Sstevel@tonic-gate uu_list_t *old_deps;
30200Sstevel@tonic-gate int ret = 0;
30210Sstevel@tonic-gate graph_edge_t *e;
30221712Srm88369 graph_vertex_t *vv;
30230Sstevel@tonic-gate
302411466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
30250Sstevel@tonic-gate assert(v->gv_type == GVT_INST);
30260Sstevel@tonic-gate
30270Sstevel@tonic-gate log_framework(LOG_DEBUG, "Graph engine: Refreshing %s.\n", v->gv_name);
30280Sstevel@tonic-gate
30290Sstevel@tonic-gate if (milestone > MILESTONE_NONE) {
30300Sstevel@tonic-gate /*
30310Sstevel@tonic-gate * In case some of v's dependencies are being deleted we must
30320Sstevel@tonic-gate * make a list of them now for GV_INSUBGRAPH-flag evaluation
30330Sstevel@tonic-gate * after the new dependencies are in place.
30340Sstevel@tonic-gate */
30350Sstevel@tonic-gate old_deps = startd_list_create(graph_edge_pool, NULL, 0);
30360Sstevel@tonic-gate
30370Sstevel@tonic-gate err = uu_list_walk(v->gv_dependencies,
30381712Srm88369 (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0);
30390Sstevel@tonic-gate assert(err == 0);
30400Sstevel@tonic-gate }
30410Sstevel@tonic-gate
30420Sstevel@tonic-gate delete_instance_dependencies(v, B_FALSE);
30430Sstevel@tonic-gate
30440Sstevel@tonic-gate err = set_dependencies(v, inst, &path);
30450Sstevel@tonic-gate switch (err) {
30460Sstevel@tonic-gate case 0:
30470Sstevel@tonic-gate break;
30480Sstevel@tonic-gate
30490Sstevel@tonic-gate case ECONNABORTED:
30500Sstevel@tonic-gate ret = err;
30510Sstevel@tonic-gate goto out;
30520Sstevel@tonic-gate
30530Sstevel@tonic-gate case EINVAL:
30540Sstevel@tonic-gate case ELOOP:
30550Sstevel@tonic-gate r = libscf_instance_get_fmri(inst, &fmri);
30560Sstevel@tonic-gate switch (r) {
30570Sstevel@tonic-gate case 0:
30580Sstevel@tonic-gate break;
30590Sstevel@tonic-gate
30600Sstevel@tonic-gate case ECONNABORTED:
30610Sstevel@tonic-gate ret = ECONNABORTED;
30620Sstevel@tonic-gate goto out;
30630Sstevel@tonic-gate
30640Sstevel@tonic-gate case ECANCELED:
30650Sstevel@tonic-gate ret = 0;
30660Sstevel@tonic-gate goto out;
30670Sstevel@tonic-gate
30680Sstevel@tonic-gate default:
30690Sstevel@tonic-gate bad_error("libscf_instance_get_fmri", r);
30700Sstevel@tonic-gate }
30710Sstevel@tonic-gate
30720Sstevel@tonic-gate if (err == EINVAL) {
30730Sstevel@tonic-gate log_error(LOG_ERR, "Transitioning %s "
30740Sstevel@tonic-gate "to maintenance due to misconfiguration.\n",
30750Sstevel@tonic-gate fmri ? fmri : "?");
30760Sstevel@tonic-gate vertex_send_event(v,
30770Sstevel@tonic-gate RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY);
30780Sstevel@tonic-gate } else {
30790Sstevel@tonic-gate handle_cycle(fmri, path);
30800Sstevel@tonic-gate vertex_send_event(v,
30810Sstevel@tonic-gate RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE);
30820Sstevel@tonic-gate }
30830Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size);
30840Sstevel@tonic-gate ret = 0;
30850Sstevel@tonic-gate goto out;
30860Sstevel@tonic-gate
30870Sstevel@tonic-gate default:
30880Sstevel@tonic-gate bad_error("set_dependencies", err);
30890Sstevel@tonic-gate }
30900Sstevel@tonic-gate
30910Sstevel@tonic-gate if (milestone > MILESTONE_NONE) {
30920Sstevel@tonic-gate boolean_t aborted = B_FALSE;
30930Sstevel@tonic-gate
30940Sstevel@tonic-gate for (e = uu_list_first(old_deps);
30950Sstevel@tonic-gate e != NULL;
30960Sstevel@tonic-gate e = uu_list_next(old_deps, e)) {
30971712Srm88369 vv = e->ge_vertex;
30981712Srm88369
30991712Srm88369 if (vertex_unref(vv) == VERTEX_INUSE &&
31001712Srm88369 eval_subgraph(vv, h) == ECONNABORTED)
31010Sstevel@tonic-gate aborted = B_TRUE;
31020Sstevel@tonic-gate }
31030Sstevel@tonic-gate
31040Sstevel@tonic-gate for (e = uu_list_first(v->gv_dependencies);
31050Sstevel@tonic-gate e != NULL;
31060Sstevel@tonic-gate e = uu_list_next(v->gv_dependencies, e)) {
31070Sstevel@tonic-gate if (eval_subgraph(e->ge_vertex, h) ==
31080Sstevel@tonic-gate ECONNABORTED)
31090Sstevel@tonic-gate aborted = B_TRUE;
31100Sstevel@tonic-gate }
31110Sstevel@tonic-gate
31120Sstevel@tonic-gate if (aborted) {
31130Sstevel@tonic-gate ret = ECONNABORTED;
31140Sstevel@tonic-gate goto out;
31150Sstevel@tonic-gate }
31160Sstevel@tonic-gate }
31170Sstevel@tonic-gate
31181958Slianep graph_start_if_satisfied(v);
31190Sstevel@tonic-gate
31200Sstevel@tonic-gate ret = 0;
31210Sstevel@tonic-gate
31220Sstevel@tonic-gate out:
31230Sstevel@tonic-gate if (milestone > MILESTONE_NONE) {
31240Sstevel@tonic-gate void *cookie = NULL;
31250Sstevel@tonic-gate
31260Sstevel@tonic-gate while ((e = uu_list_teardown(old_deps, &cookie)) != NULL)
31270Sstevel@tonic-gate startd_free(e, sizeof (*e));
31280Sstevel@tonic-gate
31290Sstevel@tonic-gate uu_list_destroy(old_deps);
31300Sstevel@tonic-gate }
31310Sstevel@tonic-gate
31320Sstevel@tonic-gate return (ret);
31330Sstevel@tonic-gate }
31340Sstevel@tonic-gate
31350Sstevel@tonic-gate /*
31360Sstevel@tonic-gate * Set up v according to inst. That is, make sure it depends on its
31370Sstevel@tonic-gate * restarter and set up its dependencies. Send the ADD_INSTANCE command to
31380Sstevel@tonic-gate * the restarter, and send ENABLE or DISABLE as appropriate.
31390Sstevel@tonic-gate *
31400Sstevel@tonic-gate * Returns 0 on success, ECONNABORTED on repository disconnection, or
31410Sstevel@tonic-gate * ECANCELED if inst is deleted.
31420Sstevel@tonic-gate */
31430Sstevel@tonic-gate static int
configure_vertex(graph_vertex_t * v,scf_instance_t * inst)31440Sstevel@tonic-gate configure_vertex(graph_vertex_t *v, scf_instance_t *inst)
31450Sstevel@tonic-gate {
31460Sstevel@tonic-gate scf_handle_t *h;
31470Sstevel@tonic-gate scf_propertygroup_t *pg;
31480Sstevel@tonic-gate scf_snapshot_t *snap;
31490Sstevel@tonic-gate char *restarter_fmri = startd_alloc(max_scf_value_size);
31500Sstevel@tonic-gate int enabled, enabled_ovr;
31510Sstevel@tonic-gate int err;
31520Sstevel@tonic-gate int *path;
31537475SPhilippe.Jung@Sun.COM int deathrow;
3154*12967Sgavin.maltby@oracle.com int32_t tset;
31550Sstevel@tonic-gate
31560Sstevel@tonic-gate restarter_fmri[0] = '\0';
31570Sstevel@tonic-gate
315811466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
31590Sstevel@tonic-gate assert(v->gv_type == GVT_INST);
31600Sstevel@tonic-gate assert((v->gv_flags & GV_CONFIGURED) == 0);
31610Sstevel@tonic-gate
31620Sstevel@tonic-gate /* GV_INSUBGRAPH should already be set properly. */
31630Sstevel@tonic-gate assert(should_be_in_subgraph(v) ==
31640Sstevel@tonic-gate ((v->gv_flags & GV_INSUBGRAPH) != 0));
31650Sstevel@tonic-gate
31667475SPhilippe.Jung@Sun.COM /*
31677475SPhilippe.Jung@Sun.COM * If the instance fmri is in the deathrow list then set the
31687475SPhilippe.Jung@Sun.COM * GV_DEATHROW flag on the vertex and create and set to true the
31697475SPhilippe.Jung@Sun.COM * SCF_PROPERTY_DEATHROW boolean property in the non-persistent
31707475SPhilippe.Jung@Sun.COM * repository for this instance fmri.
31717475SPhilippe.Jung@Sun.COM */
31727475SPhilippe.Jung@Sun.COM if ((v->gv_flags & GV_DEATHROW) ||
31737475SPhilippe.Jung@Sun.COM (is_fmri_in_deathrow(v->gv_name) == B_TRUE)) {
31747475SPhilippe.Jung@Sun.COM if ((v->gv_flags & GV_DEATHROW) == 0) {
31757475SPhilippe.Jung@Sun.COM /*
31767475SPhilippe.Jung@Sun.COM * Set flag GV_DEATHROW, create and set to true
31777475SPhilippe.Jung@Sun.COM * the SCF_PROPERTY_DEATHROW property in the
31787475SPhilippe.Jung@Sun.COM * non-persistent repository for this instance fmri.
31797475SPhilippe.Jung@Sun.COM */
31807475SPhilippe.Jung@Sun.COM v->gv_flags |= GV_DEATHROW;
31817475SPhilippe.Jung@Sun.COM
31827475SPhilippe.Jung@Sun.COM switch (err = libscf_set_deathrow(inst, 1)) {
31837475SPhilippe.Jung@Sun.COM case 0:
31847475SPhilippe.Jung@Sun.COM break;
31857475SPhilippe.Jung@Sun.COM
31867475SPhilippe.Jung@Sun.COM case ECONNABORTED:
31877475SPhilippe.Jung@Sun.COM case ECANCELED:
31887475SPhilippe.Jung@Sun.COM startd_free(restarter_fmri, max_scf_value_size);
31897475SPhilippe.Jung@Sun.COM return (err);
31907475SPhilippe.Jung@Sun.COM
31917475SPhilippe.Jung@Sun.COM case EROFS:
31927475SPhilippe.Jung@Sun.COM log_error(LOG_WARNING, "Could not set %s/%s "
31937475SPhilippe.Jung@Sun.COM "for deathrow %s: %s.\n",
31947475SPhilippe.Jung@Sun.COM SCF_PG_DEATHROW, SCF_PROPERTY_DEATHROW,
31957475SPhilippe.Jung@Sun.COM v->gv_name, strerror(err));
31967475SPhilippe.Jung@Sun.COM break;
31977475SPhilippe.Jung@Sun.COM
31987475SPhilippe.Jung@Sun.COM case EPERM:
31997475SPhilippe.Jung@Sun.COM uu_die("Permission denied.\n");
32007475SPhilippe.Jung@Sun.COM /* NOTREACHED */
32017475SPhilippe.Jung@Sun.COM
32027475SPhilippe.Jung@Sun.COM default:
32037475SPhilippe.Jung@Sun.COM bad_error("libscf_set_deathrow", err);
32047475SPhilippe.Jung@Sun.COM }
32057475SPhilippe.Jung@Sun.COM log_framework(LOG_DEBUG, "Deathrow, graph set %s.\n",
32067475SPhilippe.Jung@Sun.COM v->gv_name);
32077475SPhilippe.Jung@Sun.COM }
32087475SPhilippe.Jung@Sun.COM startd_free(restarter_fmri, max_scf_value_size);
32097475SPhilippe.Jung@Sun.COM return (0);
32107475SPhilippe.Jung@Sun.COM }
32110Sstevel@tonic-gate
32120Sstevel@tonic-gate h = scf_instance_handle(inst);
32130Sstevel@tonic-gate
32140Sstevel@tonic-gate /*
32157475SPhilippe.Jung@Sun.COM * Using a temporary deathrow boolean property, set through
32167475SPhilippe.Jung@Sun.COM * libscf_set_deathrow(), only for fmris on deathrow, is necessary
32177475SPhilippe.Jung@Sun.COM * because deathrow_fini() may already have been called, and in case
32187475SPhilippe.Jung@Sun.COM * of a refresh, GV_DEATHROW may need to be set again.
32197475SPhilippe.Jung@Sun.COM * libscf_get_deathrow() sets deathrow to 1 only if this instance
32207475SPhilippe.Jung@Sun.COM * has a temporary boolean property named 'deathrow' valued true
32217475SPhilippe.Jung@Sun.COM * in a property group 'deathrow', -1 or 0 in all other cases.
32227475SPhilippe.Jung@Sun.COM */
32237475SPhilippe.Jung@Sun.COM err = libscf_get_deathrow(h, inst, &deathrow);
32247475SPhilippe.Jung@Sun.COM switch (err) {
32257475SPhilippe.Jung@Sun.COM case 0:
32267475SPhilippe.Jung@Sun.COM break;
32277475SPhilippe.Jung@Sun.COM
32287475SPhilippe.Jung@Sun.COM case ECONNABORTED:
32297475SPhilippe.Jung@Sun.COM case ECANCELED:
32307475SPhilippe.Jung@Sun.COM startd_free(restarter_fmri, max_scf_value_size);
32317475SPhilippe.Jung@Sun.COM return (err);
32327475SPhilippe.Jung@Sun.COM
32337475SPhilippe.Jung@Sun.COM default:
32347475SPhilippe.Jung@Sun.COM bad_error("libscf_get_deathrow", err);
32357475SPhilippe.Jung@Sun.COM }
32367475SPhilippe.Jung@Sun.COM
32377475SPhilippe.Jung@Sun.COM if (deathrow == 1) {
32387475SPhilippe.Jung@Sun.COM v->gv_flags |= GV_DEATHROW;
32397475SPhilippe.Jung@Sun.COM startd_free(restarter_fmri, max_scf_value_size);
32407475SPhilippe.Jung@Sun.COM return (0);
32417475SPhilippe.Jung@Sun.COM }
32427475SPhilippe.Jung@Sun.COM
32437475SPhilippe.Jung@Sun.COM log_framework(LOG_DEBUG, "Graph adding %s.\n", v->gv_name);
32447475SPhilippe.Jung@Sun.COM
32457475SPhilippe.Jung@Sun.COM /*
32460Sstevel@tonic-gate * If the instance does not have a restarter property group,
32470Sstevel@tonic-gate * initialize its state to uninitialized/none, in case the restarter
32480Sstevel@tonic-gate * is not enabled.
32490Sstevel@tonic-gate */
32500Sstevel@tonic-gate pg = safe_scf_pg_create(h);
32510Sstevel@tonic-gate
32520Sstevel@tonic-gate if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) != 0) {
32530Sstevel@tonic-gate instance_data_t idata;
32540Sstevel@tonic-gate uint_t count = 0, msecs = ALLOC_DELAY;
32550Sstevel@tonic-gate
32560Sstevel@tonic-gate switch (scf_error()) {
32570Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
32580Sstevel@tonic-gate break;
32590Sstevel@tonic-gate
32600Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
32610Sstevel@tonic-gate default:
32620Sstevel@tonic-gate scf_pg_destroy(pg);
32639333SRenaud.Manus@Sun.COM startd_free(restarter_fmri, max_scf_value_size);
32640Sstevel@tonic-gate return (ECONNABORTED);
32650Sstevel@tonic-gate
32660Sstevel@tonic-gate case SCF_ERROR_DELETED:
32670Sstevel@tonic-gate scf_pg_destroy(pg);
32689333SRenaud.Manus@Sun.COM startd_free(restarter_fmri, max_scf_value_size);
32690Sstevel@tonic-gate return (ECANCELED);
32700Sstevel@tonic-gate
32710Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
32720Sstevel@tonic-gate bad_error("scf_instance_get_pg", scf_error());
32730Sstevel@tonic-gate }
32740Sstevel@tonic-gate
32750Sstevel@tonic-gate switch (err = libscf_instance_get_fmri(inst,
32760Sstevel@tonic-gate (char **)&idata.i_fmri)) {
32770Sstevel@tonic-gate case 0:
32780Sstevel@tonic-gate break;
32790Sstevel@tonic-gate
32800Sstevel@tonic-gate case ECONNABORTED:
32810Sstevel@tonic-gate case ECANCELED:
32820Sstevel@tonic-gate scf_pg_destroy(pg);
32839333SRenaud.Manus@Sun.COM startd_free(restarter_fmri, max_scf_value_size);
32840Sstevel@tonic-gate return (err);
32850Sstevel@tonic-gate
32860Sstevel@tonic-gate default:
32870Sstevel@tonic-gate bad_error("libscf_instance_get_fmri", err);
32880Sstevel@tonic-gate }
32890Sstevel@tonic-gate
32900Sstevel@tonic-gate idata.i_state = RESTARTER_STATE_NONE;
32910Sstevel@tonic-gate idata.i_next_state = RESTARTER_STATE_NONE;
32920Sstevel@tonic-gate
32930Sstevel@tonic-gate init_state:
32940Sstevel@tonic-gate switch (err = _restarter_commit_states(h, &idata,
3295*12967Sgavin.maltby@oracle.com RESTARTER_STATE_UNINIT, RESTARTER_STATE_NONE,
3296*12967Sgavin.maltby@oracle.com restarter_get_str_short(restarter_str_insert_in_graph))) {
32970Sstevel@tonic-gate case 0:
32980Sstevel@tonic-gate break;
32990Sstevel@tonic-gate
33000Sstevel@tonic-gate case ENOMEM:
33010Sstevel@tonic-gate ++count;
33020Sstevel@tonic-gate if (count < ALLOC_RETRY) {
33030Sstevel@tonic-gate (void) poll(NULL, 0, msecs);
33040Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT;
33050Sstevel@tonic-gate goto init_state;
33060Sstevel@tonic-gate }
33070Sstevel@tonic-gate
33080Sstevel@tonic-gate uu_die("Insufficient memory.\n");
33090Sstevel@tonic-gate /* NOTREACHED */
33100Sstevel@tonic-gate
33110Sstevel@tonic-gate case ECONNABORTED:
33122682Srm88369 startd_free((void *)idata.i_fmri, max_scf_fmri_size);
33130Sstevel@tonic-gate scf_pg_destroy(pg);
33149333SRenaud.Manus@Sun.COM startd_free(restarter_fmri, max_scf_value_size);
33150Sstevel@tonic-gate return (ECONNABORTED);
33160Sstevel@tonic-gate
33170Sstevel@tonic-gate case ENOENT:
33182682Srm88369 startd_free((void *)idata.i_fmri, max_scf_fmri_size);
33190Sstevel@tonic-gate scf_pg_destroy(pg);
33209333SRenaud.Manus@Sun.COM startd_free(restarter_fmri, max_scf_value_size);
33210Sstevel@tonic-gate return (ECANCELED);
33220Sstevel@tonic-gate
33230Sstevel@tonic-gate case EPERM:
33240Sstevel@tonic-gate case EACCES:
33250Sstevel@tonic-gate case EROFS:
33260Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not initialize state for "
33270Sstevel@tonic-gate "%s: %s.\n", idata.i_fmri, strerror(err));
33280Sstevel@tonic-gate break;
33290Sstevel@tonic-gate
33300Sstevel@tonic-gate case EINVAL:
33310Sstevel@tonic-gate default:
33320Sstevel@tonic-gate bad_error("_restarter_commit_states", err);
33330Sstevel@tonic-gate }
33340Sstevel@tonic-gate
33350Sstevel@tonic-gate startd_free((void *)idata.i_fmri, max_scf_fmri_size);
33360Sstevel@tonic-gate }
33370Sstevel@tonic-gate
33380Sstevel@tonic-gate scf_pg_destroy(pg);
33390Sstevel@tonic-gate
33400Sstevel@tonic-gate if (milestone != NULL) {
33410Sstevel@tonic-gate /*
33420Sstevel@tonic-gate * Make sure the enable-override is set properly before we
33430Sstevel@tonic-gate * read whether we should be enabled.
33440Sstevel@tonic-gate */
33450Sstevel@tonic-gate if (milestone == MILESTONE_NONE ||
33460Sstevel@tonic-gate !(v->gv_flags & GV_INSUBGRAPH)) {
33472747Sbustos /*
33482747Sbustos * This might seem unjustified after the milestone
33492747Sbustos * transition has completed (non_subgraph_svcs == 0),
33502747Sbustos * but it's important because when we boot to
33512747Sbustos * a milestone, we set the milestone before populating
33522747Sbustos * the graph, and all of the new non-subgraph services
33532747Sbustos * need to be disabled here.
33542747Sbustos */
33550Sstevel@tonic-gate switch (err = libscf_set_enable_ovr(inst, 0)) {
33560Sstevel@tonic-gate case 0:
33570Sstevel@tonic-gate break;
33580Sstevel@tonic-gate
33590Sstevel@tonic-gate case ECONNABORTED:
33600Sstevel@tonic-gate case ECANCELED:
33619333SRenaud.Manus@Sun.COM startd_free(restarter_fmri, max_scf_value_size);
33620Sstevel@tonic-gate return (err);
33630Sstevel@tonic-gate
33640Sstevel@tonic-gate case EROFS:
33650Sstevel@tonic-gate log_error(LOG_WARNING,
33660Sstevel@tonic-gate "Could not set %s/%s for %s: %s.\n",
33670Sstevel@tonic-gate SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
33680Sstevel@tonic-gate v->gv_name, strerror(err));
33690Sstevel@tonic-gate break;
33700Sstevel@tonic-gate
33710Sstevel@tonic-gate case EPERM:
33720Sstevel@tonic-gate uu_die("Permission denied.\n");
33730Sstevel@tonic-gate /* NOTREACHED */
33740Sstevel@tonic-gate
33750Sstevel@tonic-gate default:
33760Sstevel@tonic-gate bad_error("libscf_set_enable_ovr", err);
33770Sstevel@tonic-gate }
33780Sstevel@tonic-gate } else {
33790Sstevel@tonic-gate assert(v->gv_flags & GV_INSUBGRAPH);
33800Sstevel@tonic-gate switch (err = libscf_delete_enable_ovr(inst)) {
33810Sstevel@tonic-gate case 0:
33820Sstevel@tonic-gate break;
33830Sstevel@tonic-gate
33840Sstevel@tonic-gate case ECONNABORTED:
33850Sstevel@tonic-gate case ECANCELED:
33869333SRenaud.Manus@Sun.COM startd_free(restarter_fmri, max_scf_value_size);
33870Sstevel@tonic-gate return (err);
33880Sstevel@tonic-gate
33890Sstevel@tonic-gate case EPERM:
33900Sstevel@tonic-gate uu_die("Permission denied.\n");
33910Sstevel@tonic-gate /* NOTREACHED */
33920Sstevel@tonic-gate
33930Sstevel@tonic-gate default:
33940Sstevel@tonic-gate bad_error("libscf_delete_enable_ovr", err);
33950Sstevel@tonic-gate }
33960Sstevel@tonic-gate }
33970Sstevel@tonic-gate }
33980Sstevel@tonic-gate
33990Sstevel@tonic-gate err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled,
34000Sstevel@tonic-gate &enabled_ovr, &restarter_fmri);
34010Sstevel@tonic-gate switch (err) {
34020Sstevel@tonic-gate case 0:
34030Sstevel@tonic-gate break;
34040Sstevel@tonic-gate
34050Sstevel@tonic-gate case ECONNABORTED:
34060Sstevel@tonic-gate case ECANCELED:
34070Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size);
34080Sstevel@tonic-gate return (err);
34090Sstevel@tonic-gate
34100Sstevel@tonic-gate case ENOENT:
34110Sstevel@tonic-gate log_framework(LOG_DEBUG,
34120Sstevel@tonic-gate "Ignoring %s because it has no general property group.\n",
34130Sstevel@tonic-gate v->gv_name);
34140Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size);
34150Sstevel@tonic-gate return (0);
34160Sstevel@tonic-gate
34170Sstevel@tonic-gate default:
34180Sstevel@tonic-gate bad_error("libscf_get_basic_instance_data", err);
34190Sstevel@tonic-gate }
34200Sstevel@tonic-gate
3421*12967Sgavin.maltby@oracle.com if ((tset = libscf_get_stn_tset(inst)) == -1) {
3422*12967Sgavin.maltby@oracle.com log_framework(LOG_WARNING,
3423*12967Sgavin.maltby@oracle.com "Failed to get notification parameters for %s: %s\n",
3424*12967Sgavin.maltby@oracle.com v->gv_name, scf_strerror(scf_error()));
3425*12967Sgavin.maltby@oracle.com v->gv_stn_tset = 0;
3426*12967Sgavin.maltby@oracle.com } else {
3427*12967Sgavin.maltby@oracle.com v->gv_stn_tset = tset;
3428*12967Sgavin.maltby@oracle.com }
3429*12967Sgavin.maltby@oracle.com if (strcmp(v->gv_name, SCF_INSTANCE_GLOBAL) == 0)
3430*12967Sgavin.maltby@oracle.com stn_global = v->gv_stn_tset;
3431*12967Sgavin.maltby@oracle.com
34320Sstevel@tonic-gate if (enabled == -1) {
34330Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size);
34340Sstevel@tonic-gate return (0);
34350Sstevel@tonic-gate }
34360Sstevel@tonic-gate
34370Sstevel@tonic-gate v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) |
34380Sstevel@tonic-gate (enabled ? GV_ENBLD_NOOVR : 0);
34390Sstevel@tonic-gate
34400Sstevel@tonic-gate if (enabled_ovr != -1)
34410Sstevel@tonic-gate enabled = enabled_ovr;
34420Sstevel@tonic-gate
34430Sstevel@tonic-gate v->gv_state = RESTARTER_STATE_UNINIT;
34440Sstevel@tonic-gate
34450Sstevel@tonic-gate snap = libscf_get_or_make_running_snapshot(inst, v->gv_name, B_TRUE);
34460Sstevel@tonic-gate scf_snapshot_destroy(snap);
34470Sstevel@tonic-gate
34480Sstevel@tonic-gate /* Set up the restarter. (Sends _ADD_INSTANCE on success.) */
34490Sstevel@tonic-gate err = graph_change_restarter(v, restarter_fmri, h, &path);
34500Sstevel@tonic-gate if (err != 0) {
34510Sstevel@tonic-gate instance_data_t idata;
34520Sstevel@tonic-gate uint_t count = 0, msecs = ALLOC_DELAY;
3453*12967Sgavin.maltby@oracle.com restarter_str_t reason;
34540Sstevel@tonic-gate
34550Sstevel@tonic-gate if (err == ECONNABORTED) {
34560Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size);
34570Sstevel@tonic-gate return (err);
34580Sstevel@tonic-gate }
34590Sstevel@tonic-gate
34600Sstevel@tonic-gate assert(err == EINVAL || err == ELOOP);
34610Sstevel@tonic-gate
34620Sstevel@tonic-gate if (err == EINVAL) {
34631958Slianep log_framework(LOG_ERR, emsg_invalid_restarter,
34649263SSean.Wilcox@Sun.COM v->gv_name, restarter_fmri);
3465*12967Sgavin.maltby@oracle.com reason = restarter_str_invalid_restarter;
34660Sstevel@tonic-gate } else {
34670Sstevel@tonic-gate handle_cycle(v->gv_name, path);
3468*12967Sgavin.maltby@oracle.com reason = restarter_str_dependency_cycle;
34690Sstevel@tonic-gate }
34700Sstevel@tonic-gate
34710Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size);
34720Sstevel@tonic-gate
34730Sstevel@tonic-gate /*
34740Sstevel@tonic-gate * We didn't register the instance with the restarter, so we
34750Sstevel@tonic-gate * must set maintenance mode ourselves.
34760Sstevel@tonic-gate */
34770Sstevel@tonic-gate err = libscf_instance_get_fmri(inst, (char **)&idata.i_fmri);
34780Sstevel@tonic-gate if (err != 0) {
34790Sstevel@tonic-gate assert(err == ECONNABORTED || err == ECANCELED);
34800Sstevel@tonic-gate return (err);
34810Sstevel@tonic-gate }
34820Sstevel@tonic-gate
34830Sstevel@tonic-gate idata.i_state = RESTARTER_STATE_NONE;
34840Sstevel@tonic-gate idata.i_next_state = RESTARTER_STATE_NONE;
34850Sstevel@tonic-gate
34860Sstevel@tonic-gate set_maint:
34870Sstevel@tonic-gate switch (err = _restarter_commit_states(h, &idata,
3488*12967Sgavin.maltby@oracle.com RESTARTER_STATE_MAINT, RESTARTER_STATE_NONE,
3489*12967Sgavin.maltby@oracle.com restarter_get_str_short(reason))) {
34900Sstevel@tonic-gate case 0:
34910Sstevel@tonic-gate break;
34920Sstevel@tonic-gate
34930Sstevel@tonic-gate case ENOMEM:
34940Sstevel@tonic-gate ++count;
34950Sstevel@tonic-gate if (count < ALLOC_RETRY) {
34960Sstevel@tonic-gate (void) poll(NULL, 0, msecs);
34970Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT;
34980Sstevel@tonic-gate goto set_maint;
34990Sstevel@tonic-gate }
35000Sstevel@tonic-gate
35010Sstevel@tonic-gate uu_die("Insufficient memory.\n");
35020Sstevel@tonic-gate /* NOTREACHED */
35030Sstevel@tonic-gate
35040Sstevel@tonic-gate case ECONNABORTED:
35052682Srm88369 startd_free((void *)idata.i_fmri, max_scf_fmri_size);
35060Sstevel@tonic-gate return (ECONNABORTED);
35070Sstevel@tonic-gate
35080Sstevel@tonic-gate case ENOENT:
35092682Srm88369 startd_free((void *)idata.i_fmri, max_scf_fmri_size);
35100Sstevel@tonic-gate return (ECANCELED);
35110Sstevel@tonic-gate
35120Sstevel@tonic-gate case EPERM:
35130Sstevel@tonic-gate case EACCES:
35140Sstevel@tonic-gate case EROFS:
35150Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not initialize state for "
35160Sstevel@tonic-gate "%s: %s.\n", idata.i_fmri, strerror(err));
35170Sstevel@tonic-gate break;
35180Sstevel@tonic-gate
35190Sstevel@tonic-gate case EINVAL:
35200Sstevel@tonic-gate default:
35210Sstevel@tonic-gate bad_error("_restarter_commit_states", err);
35220Sstevel@tonic-gate }
35230Sstevel@tonic-gate
35240Sstevel@tonic-gate startd_free((void *)idata.i_fmri, max_scf_fmri_size);
35250Sstevel@tonic-gate
35260Sstevel@tonic-gate v->gv_state = RESTARTER_STATE_MAINT;
35270Sstevel@tonic-gate
35280Sstevel@tonic-gate goto out;
35290Sstevel@tonic-gate }
35300Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size);
35310Sstevel@tonic-gate
35320Sstevel@tonic-gate /* Add all the other dependencies. */
35330Sstevel@tonic-gate err = refresh_vertex(v, inst);
35340Sstevel@tonic-gate if (err != 0) {
35350Sstevel@tonic-gate assert(err == ECONNABORTED);
35360Sstevel@tonic-gate return (err);
35370Sstevel@tonic-gate }
35380Sstevel@tonic-gate
35390Sstevel@tonic-gate out:
35400Sstevel@tonic-gate v->gv_flags |= GV_CONFIGURED;
35410Sstevel@tonic-gate
35420Sstevel@tonic-gate graph_enable_by_vertex(v, enabled, 0);
35430Sstevel@tonic-gate
35440Sstevel@tonic-gate return (0);
35450Sstevel@tonic-gate }
35460Sstevel@tonic-gate
35478944Sdp@eng.sun.com
35488944Sdp@eng.sun.com static void
kill_user_procs(void)35498944Sdp@eng.sun.com kill_user_procs(void)
35508944Sdp@eng.sun.com {
35518944Sdp@eng.sun.com (void) fputs("svc.startd: Killing user processes.\n", stdout);
35528944Sdp@eng.sun.com
35538944Sdp@eng.sun.com /*
35548944Sdp@eng.sun.com * Despite its name, killall's role is to get select user processes--
35558944Sdp@eng.sun.com * basically those representing terminal-based logins-- to die. Victims
35568944Sdp@eng.sun.com * are located by killall in the utmp database. Since these are most
35578944Sdp@eng.sun.com * often shell based logins, and many shells mask SIGTERM (but are
35588944Sdp@eng.sun.com * responsive to SIGHUP) we first HUP and then shortly thereafter
35598944Sdp@eng.sun.com * kill -9.
35608944Sdp@eng.sun.com */
35618944Sdp@eng.sun.com (void) fork_with_timeout("/usr/sbin/killall HUP", 1, 5);
35628944Sdp@eng.sun.com (void) fork_with_timeout("/usr/sbin/killall KILL", 1, 5);
35638944Sdp@eng.sun.com
35648944Sdp@eng.sun.com /*
35658944Sdp@eng.sun.com * Note the selection of user id's 0, 1 and 15, subsequently
35668944Sdp@eng.sun.com * inverted by -v. 15 is reserved for dladmd. Yes, this is a
35678944Sdp@eng.sun.com * kludge-- a better policy is needed.
35688944Sdp@eng.sun.com *
35698944Sdp@eng.sun.com * Note that fork_with_timeout will only wait out the 1 second
35708944Sdp@eng.sun.com * "grace time" if pkill actually returns 0. So if there are
35718944Sdp@eng.sun.com * no matches, this will run to completion much more quickly.
35728944Sdp@eng.sun.com */
35738944Sdp@eng.sun.com (void) fork_with_timeout("/usr/bin/pkill -TERM -v -u 0,1,15", 1, 5);
35748944Sdp@eng.sun.com (void) fork_with_timeout("/usr/bin/pkill -KILL -v -u 0,1,15", 1, 5);
35758944Sdp@eng.sun.com }
35768944Sdp@eng.sun.com
35770Sstevel@tonic-gate static void
do_uadmin(void)35780Sstevel@tonic-gate do_uadmin(void)
35790Sstevel@tonic-gate {
35809160SSherry.Moore@Sun.COM const char * const resetting = "/etc/svc/volatile/resetting";
35818944Sdp@eng.sun.com int fd;
35820Sstevel@tonic-gate struct statvfs vfs;
35838944Sdp@eng.sun.com time_t now;
35848944Sdp@eng.sun.com struct tm nowtm;
35858944Sdp@eng.sun.com char down_buf[256], time_buf[256];
35869160SSherry.Moore@Sun.COM uintptr_t mdep;
35879160SSherry.Moore@Sun.COM #if defined(__i386)
35889160SSherry.Moore@Sun.COM grub_boot_args_t fbarg;
35899160SSherry.Moore@Sun.COM #endif /* __i386 */
35909160SSherry.Moore@Sun.COM
35919160SSherry.Moore@Sun.COM mdep = NULL;
35920Sstevel@tonic-gate fd = creat(resetting, 0777);
35930Sstevel@tonic-gate if (fd >= 0)
35940Sstevel@tonic-gate startd_close(fd);
35950Sstevel@tonic-gate else
35960Sstevel@tonic-gate uu_warn("Could not create \"%s\"", resetting);
35970Sstevel@tonic-gate
35980Sstevel@tonic-gate /* Kill dhcpagent if we're not using nfs for root */
35990Sstevel@tonic-gate if ((statvfs("/", &vfs) == 0) &&
36000Sstevel@tonic-gate (strncmp(vfs.f_basetype, "nfs", sizeof ("nfs") - 1) != 0))
36018944Sdp@eng.sun.com fork_with_timeout("/usr/bin/pkill -x -u 0 dhcpagent", 0, 5);
36028944Sdp@eng.sun.com
36038944Sdp@eng.sun.com /*
36048944Sdp@eng.sun.com * Call sync(2) now, before we kill off user processes. This takes
36058944Sdp@eng.sun.com * advantage of the several seconds of pause we have before the
36068944Sdp@eng.sun.com * killalls are done. Time we can make good use of to get pages
36078944Sdp@eng.sun.com * moving out to disk.
36088944Sdp@eng.sun.com *
36098944Sdp@eng.sun.com * Inside non-global zones, we don't bother, and it's better not to
36108944Sdp@eng.sun.com * anyway, since sync(2) can have system-wide impact.
36118944Sdp@eng.sun.com */
36128944Sdp@eng.sun.com if (getzoneid() == 0)
36138944Sdp@eng.sun.com sync();
36148944Sdp@eng.sun.com
36158944Sdp@eng.sun.com kill_user_procs();
36168944Sdp@eng.sun.com
36178944Sdp@eng.sun.com /*
36188944Sdp@eng.sun.com * Note that this must come after the killing of user procs, since
36198944Sdp@eng.sun.com * killall relies on utmpx, and this command affects the contents of
36208944Sdp@eng.sun.com * said file.
36218944Sdp@eng.sun.com */
36228944Sdp@eng.sun.com if (access("/usr/lib/acct/closewtmp", X_OK) == 0)
36238944Sdp@eng.sun.com fork_with_timeout("/usr/lib/acct/closewtmp", 0, 5);
36248944Sdp@eng.sun.com
36258944Sdp@eng.sun.com /*
36268944Sdp@eng.sun.com * For patches which may be installed as the system is shutting
36278944Sdp@eng.sun.com * down, we need to ensure, one more time, that the boot archive
36288944Sdp@eng.sun.com * really is up to date.
36298944Sdp@eng.sun.com */
36308944Sdp@eng.sun.com if (getzoneid() == 0 && access("/usr/sbin/bootadm", X_OK) == 0)
36318944Sdp@eng.sun.com fork_with_timeout("/usr/sbin/bootadm -ea update_all", 0, 3600);
36328944Sdp@eng.sun.com
36339479SSherry.Moore@Sun.COM /*
36349479SSherry.Moore@Sun.COM * Right now, fast reboot is supported only on i386.
36359479SSherry.Moore@Sun.COM * scf_is_fastboot_default() should take care of it.
36369479SSherry.Moore@Sun.COM * If somehow we got there on unsupported platform -
36379479SSherry.Moore@Sun.COM * print warning and fall back to regular reboot.
36389479SSherry.Moore@Sun.COM */
36399479SSherry.Moore@Sun.COM if (halting == AD_FASTREBOOT) {
36409479SSherry.Moore@Sun.COM #if defined(__i386)
36419479SSherry.Moore@Sun.COM int rc;
36429479SSherry.Moore@Sun.COM
36439479SSherry.Moore@Sun.COM if ((rc = grub_get_boot_args(&fbarg, NULL,
36449479SSherry.Moore@Sun.COM GRUB_ENTRY_DEFAULT)) == 0) {
36459479SSherry.Moore@Sun.COM mdep = (uintptr_t)&fbarg.gba_bootargs;
36469479SSherry.Moore@Sun.COM } else {
36479479SSherry.Moore@Sun.COM /*
36489479SSherry.Moore@Sun.COM * Failed to read GRUB menu, fall back to normal reboot
36499479SSherry.Moore@Sun.COM */
36509479SSherry.Moore@Sun.COM halting = AD_BOOT;
36519479SSherry.Moore@Sun.COM uu_warn("Failed to process GRUB menu entry "
36529479SSherry.Moore@Sun.COM "for fast reboot.\n\t%s\n"
36539479SSherry.Moore@Sun.COM "Falling back to regular reboot.\n",
36549479SSherry.Moore@Sun.COM grub_strerror(rc));
36559479SSherry.Moore@Sun.COM }
36569479SSherry.Moore@Sun.COM #else /* __i386 */
36579479SSherry.Moore@Sun.COM halting = AD_BOOT;
36589479SSherry.Moore@Sun.COM uu_warn("Fast reboot configured, but not supported by "
36599479SSherry.Moore@Sun.COM "this ISA\n");
36609479SSherry.Moore@Sun.COM #endif /* __i386 */
36619479SSherry.Moore@Sun.COM }
36629479SSherry.Moore@Sun.COM
36638944Sdp@eng.sun.com fork_with_timeout("/sbin/umountall -l", 0, 5);
36648944Sdp@eng.sun.com fork_with_timeout("/sbin/umount /tmp /var/adm /var/run /var "
36658944Sdp@eng.sun.com ">/dev/null 2>&1", 0, 5);
36668944Sdp@eng.sun.com
36678944Sdp@eng.sun.com /*
36688944Sdp@eng.sun.com * Try to get to consistency for whatever UFS filesystems are left.
36698944Sdp@eng.sun.com * This is pretty expensive, so we save it for the end in the hopes of
36708944Sdp@eng.sun.com * minimizing what it must do. The other option would be to start in
36718944Sdp@eng.sun.com * parallel with the killall's, but lockfs tends to throw out much more
36728944Sdp@eng.sun.com * than is needed, and so subsequent commands (like umountall) take a
36738944Sdp@eng.sun.com * long time to get going again.
36748944Sdp@eng.sun.com *
36758944Sdp@eng.sun.com * Inside of zones, we don't bother, since we're not about to terminate
36768944Sdp@eng.sun.com * the whole OS instance.
36778944Sdp@eng.sun.com *
36788944Sdp@eng.sun.com * On systems using only ZFS, this call to lockfs -fa is a no-op.
36798944Sdp@eng.sun.com */
36808944Sdp@eng.sun.com if (getzoneid() == 0) {
36818944Sdp@eng.sun.com if (access("/usr/sbin/lockfs", X_OK) == 0)
36828944Sdp@eng.sun.com fork_with_timeout("/usr/sbin/lockfs -fa", 0, 30);
36838944Sdp@eng.sun.com
36848944Sdp@eng.sun.com sync(); /* once more, with feeling */
36858944Sdp@eng.sun.com }
36868944Sdp@eng.sun.com
36878944Sdp@eng.sun.com fork_with_timeout("/sbin/umount /usr >/dev/null 2>&1", 0, 5);
36888944Sdp@eng.sun.com
36898944Sdp@eng.sun.com /*
36908944Sdp@eng.sun.com * Construct and emit the last words from userland:
36918944Sdp@eng.sun.com * "<timestamp> The system is down. Shutdown took <N> seconds."
36928944Sdp@eng.sun.com *
36938944Sdp@eng.sun.com * Normally we'd use syslog, but with /var and other things
36948944Sdp@eng.sun.com * potentially gone, try to minimize the external dependencies.
36958944Sdp@eng.sun.com */
36968944Sdp@eng.sun.com now = time(NULL);
36978944Sdp@eng.sun.com (void) localtime_r(&now, &nowtm);
36988944Sdp@eng.sun.com
36998944Sdp@eng.sun.com if (strftime(down_buf, sizeof (down_buf),
37008944Sdp@eng.sun.com "%b %e %T The system is down.", &nowtm) == 0) {
37018944Sdp@eng.sun.com (void) strlcpy(down_buf, "The system is down.",
37028944Sdp@eng.sun.com sizeof (down_buf));
37038944Sdp@eng.sun.com }
37048944Sdp@eng.sun.com
37058944Sdp@eng.sun.com if (halting_time != 0 && halting_time <= now) {
37068944Sdp@eng.sun.com (void) snprintf(time_buf, sizeof (time_buf),
37078944Sdp@eng.sun.com " Shutdown took %lu seconds.", now - halting_time);
37088944Sdp@eng.sun.com } else {
37098944Sdp@eng.sun.com time_buf[0] = '\0';
37108944Sdp@eng.sun.com }
37118944Sdp@eng.sun.com (void) printf("%s%s\n", down_buf, time_buf);
37120Sstevel@tonic-gate
37139160SSherry.Moore@Sun.COM (void) uadmin(A_SHUTDOWN, halting, mdep);
37140Sstevel@tonic-gate uu_warn("uadmin() failed");
37150Sstevel@tonic-gate
37169160SSherry.Moore@Sun.COM #if defined(__i386)
37179160SSherry.Moore@Sun.COM /* uadmin fail, cleanup grub_boot_args */
37189160SSherry.Moore@Sun.COM if (halting == AD_FASTREBOOT)
37199160SSherry.Moore@Sun.COM grub_cleanup_boot_args(&fbarg);
37209160SSherry.Moore@Sun.COM #endif /* __i386 */
37219160SSherry.Moore@Sun.COM
37220Sstevel@tonic-gate if (remove(resetting) != 0 && errno != ENOENT)
37230Sstevel@tonic-gate uu_warn("Could not remove \"%s\"", resetting);
37240Sstevel@tonic-gate }
37250Sstevel@tonic-gate
37260Sstevel@tonic-gate /*
37270Sstevel@tonic-gate * If any of the up_svcs[] are online or satisfiable, return true. If they are
37280Sstevel@tonic-gate * all missing, disabled, in maintenance, or unsatisfiable, return false.
37290Sstevel@tonic-gate */
37300Sstevel@tonic-gate boolean_t
can_come_up(void)37310Sstevel@tonic-gate can_come_up(void)
37320Sstevel@tonic-gate {
37330Sstevel@tonic-gate int i;
37340Sstevel@tonic-gate
373511466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
37360Sstevel@tonic-gate
37370Sstevel@tonic-gate /*
37380Sstevel@tonic-gate * If we are booting to single user (boot -s),
37390Sstevel@tonic-gate * SCF_MILESTONE_SINGLE_USER is needed to come up because startd
37400Sstevel@tonic-gate * spawns sulogin after single-user is online (see specials.c).
37410Sstevel@tonic-gate */
37420Sstevel@tonic-gate i = (booting_to_single_user ? 0 : 1);
37430Sstevel@tonic-gate
37440Sstevel@tonic-gate for (; up_svcs[i] != NULL; ++i) {
37450Sstevel@tonic-gate if (up_svcs_p[i] == NULL) {
37460Sstevel@tonic-gate up_svcs_p[i] = vertex_get_by_name(up_svcs[i]);
37470Sstevel@tonic-gate
37480Sstevel@tonic-gate if (up_svcs_p[i] == NULL)
37490Sstevel@tonic-gate continue;
37500Sstevel@tonic-gate }
37510Sstevel@tonic-gate
37520Sstevel@tonic-gate /*
37530Sstevel@tonic-gate * Ignore unconfigured services (the ones that have been
37540Sstevel@tonic-gate * mentioned in a dependency from other services, but do
37550Sstevel@tonic-gate * not exist in the repository). Services which exist
37560Sstevel@tonic-gate * in the repository but don't have general/enabled
37570Sstevel@tonic-gate * property will be also ignored.
37580Sstevel@tonic-gate */
37590Sstevel@tonic-gate if (!(up_svcs_p[i]->gv_flags & GV_CONFIGURED))
37600Sstevel@tonic-gate continue;
37610Sstevel@tonic-gate
37620Sstevel@tonic-gate switch (up_svcs_p[i]->gv_state) {
37630Sstevel@tonic-gate case RESTARTER_STATE_ONLINE:
37640Sstevel@tonic-gate case RESTARTER_STATE_DEGRADED:
37650Sstevel@tonic-gate /*
37660Sstevel@tonic-gate * Deactivate verbose boot once a login service has been
37670Sstevel@tonic-gate * reached.
37680Sstevel@tonic-gate */
37690Sstevel@tonic-gate st->st_log_login_reached = 1;
37700Sstevel@tonic-gate /*FALLTHROUGH*/
37710Sstevel@tonic-gate case RESTARTER_STATE_UNINIT:
37720Sstevel@tonic-gate return (B_TRUE);
37730Sstevel@tonic-gate
37740Sstevel@tonic-gate case RESTARTER_STATE_OFFLINE:
37750Sstevel@tonic-gate if (instance_satisfied(up_svcs_p[i], B_TRUE) != -1)
37760Sstevel@tonic-gate return (B_TRUE);
37770Sstevel@tonic-gate log_framework(LOG_DEBUG,
37780Sstevel@tonic-gate "can_come_up(): %s is unsatisfiable.\n",
37790Sstevel@tonic-gate up_svcs_p[i]->gv_name);
37800Sstevel@tonic-gate continue;
37810Sstevel@tonic-gate
37820Sstevel@tonic-gate case RESTARTER_STATE_DISABLED:
37830Sstevel@tonic-gate case RESTARTER_STATE_MAINT:
37840Sstevel@tonic-gate log_framework(LOG_DEBUG,
37850Sstevel@tonic-gate "can_come_up(): %s is in state %s.\n",
37860Sstevel@tonic-gate up_svcs_p[i]->gv_name,
37870Sstevel@tonic-gate instance_state_str[up_svcs_p[i]->gv_state]);
37880Sstevel@tonic-gate continue;
37890Sstevel@tonic-gate
37900Sstevel@tonic-gate default:
37910Sstevel@tonic-gate #ifndef NDEBUG
37920Sstevel@tonic-gate uu_warn("%s:%d: Unexpected vertex state %d.\n",
37930Sstevel@tonic-gate __FILE__, __LINE__, up_svcs_p[i]->gv_state);
37940Sstevel@tonic-gate #endif
37950Sstevel@tonic-gate abort();
37960Sstevel@tonic-gate }
37970Sstevel@tonic-gate }
37980Sstevel@tonic-gate
37990Sstevel@tonic-gate /*
38000Sstevel@tonic-gate * In the seed repository, console-login is unsatisfiable because
38010Sstevel@tonic-gate * services are missing. To behave correctly in that case we don't want
38020Sstevel@tonic-gate * to return false until manifest-import is online.
38030Sstevel@tonic-gate */
38040Sstevel@tonic-gate
38050Sstevel@tonic-gate if (manifest_import_p == NULL) {
38060Sstevel@tonic-gate manifest_import_p = vertex_get_by_name(manifest_import);
38070Sstevel@tonic-gate
38080Sstevel@tonic-gate if (manifest_import_p == NULL)
38090Sstevel@tonic-gate return (B_FALSE);
38100Sstevel@tonic-gate }
38110Sstevel@tonic-gate
38120Sstevel@tonic-gate switch (manifest_import_p->gv_state) {
38130Sstevel@tonic-gate case RESTARTER_STATE_ONLINE:
38140Sstevel@tonic-gate case RESTARTER_STATE_DEGRADED:
38150Sstevel@tonic-gate case RESTARTER_STATE_DISABLED:
38160Sstevel@tonic-gate case RESTARTER_STATE_MAINT:
38170Sstevel@tonic-gate break;
38180Sstevel@tonic-gate
38190Sstevel@tonic-gate case RESTARTER_STATE_OFFLINE:
38200Sstevel@tonic-gate if (instance_satisfied(manifest_import_p, B_TRUE) == -1)
38210Sstevel@tonic-gate break;
38220Sstevel@tonic-gate /* FALLTHROUGH */
38230Sstevel@tonic-gate
38240Sstevel@tonic-gate case RESTARTER_STATE_UNINIT:
38250Sstevel@tonic-gate return (B_TRUE);
38260Sstevel@tonic-gate }
38270Sstevel@tonic-gate
38280Sstevel@tonic-gate return (B_FALSE);
38290Sstevel@tonic-gate }
38300Sstevel@tonic-gate
38310Sstevel@tonic-gate /*
38320Sstevel@tonic-gate * Runs sulogin. Returns
38330Sstevel@tonic-gate * 0 - success
38340Sstevel@tonic-gate * EALREADY - sulogin is already running
38350Sstevel@tonic-gate * EBUSY - console-login is running
38360Sstevel@tonic-gate */
38370Sstevel@tonic-gate static int
run_sulogin(const char * msg)38380Sstevel@tonic-gate run_sulogin(const char *msg)
38390Sstevel@tonic-gate {
38400Sstevel@tonic-gate graph_vertex_t *v;
38410Sstevel@tonic-gate
384211466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
38430Sstevel@tonic-gate
38440Sstevel@tonic-gate if (sulogin_running)
38450Sstevel@tonic-gate return (EALREADY);
38460Sstevel@tonic-gate
38470Sstevel@tonic-gate v = vertex_get_by_name(console_login_fmri);
38480Sstevel@tonic-gate if (v != NULL && inst_running(v))
38490Sstevel@tonic-gate return (EBUSY);
38500Sstevel@tonic-gate
38510Sstevel@tonic-gate sulogin_running = B_TRUE;
38520Sstevel@tonic-gate
38530Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
38540Sstevel@tonic-gate
38550Sstevel@tonic-gate fork_sulogin(B_FALSE, msg);
38560Sstevel@tonic-gate
38570Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
38580Sstevel@tonic-gate
38590Sstevel@tonic-gate sulogin_running = B_FALSE;
38600Sstevel@tonic-gate
38610Sstevel@tonic-gate if (console_login_ready) {
38620Sstevel@tonic-gate v = vertex_get_by_name(console_login_fmri);
38630Sstevel@tonic-gate
38649333SRenaud.Manus@Sun.COM if (v != NULL && v->gv_state == RESTARTER_STATE_OFFLINE) {
38650Sstevel@tonic-gate if (v->gv_start_f == NULL)
38660Sstevel@tonic-gate vertex_send_event(v,
38670Sstevel@tonic-gate RESTARTER_EVENT_TYPE_START);
38680Sstevel@tonic-gate else
38690Sstevel@tonic-gate v->gv_start_f(v);
38700Sstevel@tonic-gate }
38710Sstevel@tonic-gate
38720Sstevel@tonic-gate console_login_ready = B_FALSE;
38730Sstevel@tonic-gate }
38740Sstevel@tonic-gate
38750Sstevel@tonic-gate return (0);
38760Sstevel@tonic-gate }
38770Sstevel@tonic-gate
38780Sstevel@tonic-gate /*
38790Sstevel@tonic-gate * The sulogin thread runs sulogin while can_come_up() is false. run_sulogin()
38800Sstevel@tonic-gate * keeps sulogin from stepping on console-login's toes.
38810Sstevel@tonic-gate */
38820Sstevel@tonic-gate /* ARGSUSED */
38830Sstevel@tonic-gate static void *
sulogin_thread(void * unused)38840Sstevel@tonic-gate sulogin_thread(void *unused)
38850Sstevel@tonic-gate {
38860Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
38870Sstevel@tonic-gate
38880Sstevel@tonic-gate assert(sulogin_thread_running);
38890Sstevel@tonic-gate
38905040Swesolows do {
38910Sstevel@tonic-gate (void) run_sulogin("Console login service(s) cannot run\n");
38925040Swesolows } while (!can_come_up());
38930Sstevel@tonic-gate
38940Sstevel@tonic-gate sulogin_thread_running = B_FALSE;
38950Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
38960Sstevel@tonic-gate
38970Sstevel@tonic-gate return (NULL);
38980Sstevel@tonic-gate }
38990Sstevel@tonic-gate
39000Sstevel@tonic-gate /* ARGSUSED */
39010Sstevel@tonic-gate void *
single_user_thread(void * unused)39020Sstevel@tonic-gate single_user_thread(void *unused)
39030Sstevel@tonic-gate {
39040Sstevel@tonic-gate uint_t left;
39050Sstevel@tonic-gate scf_handle_t *h;
39060Sstevel@tonic-gate scf_instance_t *inst;
39070Sstevel@tonic-gate scf_property_t *prop;
39080Sstevel@tonic-gate scf_value_t *val;
39090Sstevel@tonic-gate const char *msg;
39100Sstevel@tonic-gate char *buf;
39110Sstevel@tonic-gate int r;
39120Sstevel@tonic-gate
39130Sstevel@tonic-gate MUTEX_LOCK(&single_user_thread_lock);
39140Sstevel@tonic-gate single_user_thread_count++;
39150Sstevel@tonic-gate
39168944Sdp@eng.sun.com if (!booting_to_single_user)
39178944Sdp@eng.sun.com kill_user_procs();
39180Sstevel@tonic-gate
39190Sstevel@tonic-gate if (go_single_user_mode || booting_to_single_user) {
39200Sstevel@tonic-gate msg = "SINGLE USER MODE\n";
39210Sstevel@tonic-gate } else {
39220Sstevel@tonic-gate assert(go_to_level1);
39230Sstevel@tonic-gate
39240Sstevel@tonic-gate fork_rc_script('1', "start", B_TRUE);
39250Sstevel@tonic-gate
39260Sstevel@tonic-gate uu_warn("The system is ready for administration.\n");
39270Sstevel@tonic-gate
39280Sstevel@tonic-gate msg = "";
39290Sstevel@tonic-gate }
39300Sstevel@tonic-gate
39310Sstevel@tonic-gate MUTEX_UNLOCK(&single_user_thread_lock);
39320Sstevel@tonic-gate
39330Sstevel@tonic-gate for (;;) {
39340Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
39350Sstevel@tonic-gate r = run_sulogin(msg);
39360Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
39370Sstevel@tonic-gate if (r == 0)
39380Sstevel@tonic-gate break;
39390Sstevel@tonic-gate
39400Sstevel@tonic-gate assert(r == EALREADY || r == EBUSY);
39410Sstevel@tonic-gate
39420Sstevel@tonic-gate left = 3;
39430Sstevel@tonic-gate while (left > 0)
39440Sstevel@tonic-gate left = sleep(left);
39450Sstevel@tonic-gate }
39460Sstevel@tonic-gate
39470Sstevel@tonic-gate MUTEX_LOCK(&single_user_thread_lock);
39480Sstevel@tonic-gate
39490Sstevel@tonic-gate /*
39500Sstevel@tonic-gate * If another single user thread has started, let it finish changing
39510Sstevel@tonic-gate * the run level.
39520Sstevel@tonic-gate */
39530Sstevel@tonic-gate if (single_user_thread_count > 1) {
39540Sstevel@tonic-gate single_user_thread_count--;
39550Sstevel@tonic-gate MUTEX_UNLOCK(&single_user_thread_lock);
39560Sstevel@tonic-gate return (NULL);
39570Sstevel@tonic-gate }
39580Sstevel@tonic-gate
39590Sstevel@tonic-gate h = libscf_handle_create_bound_loop();
39600Sstevel@tonic-gate inst = scf_instance_create(h);
39610Sstevel@tonic-gate prop = safe_scf_property_create(h);
39620Sstevel@tonic-gate val = safe_scf_value_create(h);
39630Sstevel@tonic-gate buf = startd_alloc(max_scf_fmri_size);
39640Sstevel@tonic-gate
39650Sstevel@tonic-gate lookup:
39660Sstevel@tonic-gate if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
39670Sstevel@tonic-gate NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
39680Sstevel@tonic-gate switch (scf_error()) {
39690Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
39700Sstevel@tonic-gate r = libscf_create_self(h);
39710Sstevel@tonic-gate if (r == 0)
39720Sstevel@tonic-gate goto lookup;
39730Sstevel@tonic-gate assert(r == ECONNABORTED);
39740Sstevel@tonic-gate /* FALLTHROUGH */
39750Sstevel@tonic-gate
39760Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
39770Sstevel@tonic-gate libscf_handle_rebind(h);
39780Sstevel@tonic-gate goto lookup;
39790Sstevel@tonic-gate
39800Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
39810Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
39820Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
39830Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
39840Sstevel@tonic-gate default:
39850Sstevel@tonic-gate bad_error("scf_handle_decode_fmri", scf_error());
39860Sstevel@tonic-gate }
39870Sstevel@tonic-gate }
39880Sstevel@tonic-gate
39890Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
39900Sstevel@tonic-gate
39918823STruong.Q.Nguyen@Sun.COM r = scf_instance_delete_prop(inst, SCF_PG_OPTIONS_OVR,
39920Sstevel@tonic-gate SCF_PROPERTY_MILESTONE);
39930Sstevel@tonic-gate switch (r) {
39940Sstevel@tonic-gate case 0:
39950Sstevel@tonic-gate case ECANCELED:
39960Sstevel@tonic-gate break;
39970Sstevel@tonic-gate
39980Sstevel@tonic-gate case ECONNABORTED:
39990Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
40000Sstevel@tonic-gate libscf_handle_rebind(h);
40010Sstevel@tonic-gate goto lookup;
40020Sstevel@tonic-gate
40030Sstevel@tonic-gate case EPERM:
40040Sstevel@tonic-gate case EACCES:
40050Sstevel@tonic-gate case EROFS:
40060Sstevel@tonic-gate log_error(LOG_WARNING, "Could not clear temporary milestone: "
40070Sstevel@tonic-gate "%s.\n", strerror(r));
40080Sstevel@tonic-gate break;
40090Sstevel@tonic-gate
40100Sstevel@tonic-gate default:
40118823STruong.Q.Nguyen@Sun.COM bad_error("scf_instance_delete_prop", r);
40120Sstevel@tonic-gate }
40130Sstevel@tonic-gate
40140Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
40150Sstevel@tonic-gate
40160Sstevel@tonic-gate r = libscf_get_milestone(inst, prop, val, buf, max_scf_fmri_size);
40170Sstevel@tonic-gate switch (r) {
40180Sstevel@tonic-gate case ECANCELED:
40190Sstevel@tonic-gate case ENOENT:
40200Sstevel@tonic-gate case EINVAL:
40210Sstevel@tonic-gate (void) strcpy(buf, "all");
40220Sstevel@tonic-gate /* FALLTHROUGH */
40230Sstevel@tonic-gate
40240Sstevel@tonic-gate case 0:
40250Sstevel@tonic-gate uu_warn("Returning to milestone %s.\n", buf);
40260Sstevel@tonic-gate break;
40270Sstevel@tonic-gate
40280Sstevel@tonic-gate case ECONNABORTED:
40290Sstevel@tonic-gate libscf_handle_rebind(h);
40300Sstevel@tonic-gate goto lookup;
40310Sstevel@tonic-gate
40320Sstevel@tonic-gate default:
40330Sstevel@tonic-gate bad_error("libscf_get_milestone", r);
40340Sstevel@tonic-gate }
40350Sstevel@tonic-gate
40360Sstevel@tonic-gate r = dgraph_set_milestone(buf, h, B_FALSE);
40370Sstevel@tonic-gate switch (r) {
40380Sstevel@tonic-gate case 0:
40390Sstevel@tonic-gate case ECONNRESET:
40400Sstevel@tonic-gate case EALREADY:
40410Sstevel@tonic-gate case EINVAL:
40420Sstevel@tonic-gate case ENOENT:
40430Sstevel@tonic-gate break;
40440Sstevel@tonic-gate
40450Sstevel@tonic-gate default:
40460Sstevel@tonic-gate bad_error("dgraph_set_milestone", r);
40470Sstevel@tonic-gate }
40480Sstevel@tonic-gate
40490Sstevel@tonic-gate /*
40500Sstevel@tonic-gate * See graph_runlevel_changed().
40510Sstevel@tonic-gate */
40520Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
40530Sstevel@tonic-gate utmpx_set_runlevel(target_milestone_as_runlevel(), 'S', B_TRUE);
40540Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
40550Sstevel@tonic-gate
40560Sstevel@tonic-gate startd_free(buf, max_scf_fmri_size);
40570Sstevel@tonic-gate scf_value_destroy(val);
40580Sstevel@tonic-gate scf_property_destroy(prop);
40590Sstevel@tonic-gate scf_instance_destroy(inst);
40600Sstevel@tonic-gate scf_handle_destroy(h);
40610Sstevel@tonic-gate
40620Sstevel@tonic-gate /*
40630Sstevel@tonic-gate * We'll give ourselves 3 seconds to respond to all of the enablings
40640Sstevel@tonic-gate * that setting the milestone should have created before checking
40650Sstevel@tonic-gate * whether to run sulogin.
40660Sstevel@tonic-gate */
40670Sstevel@tonic-gate left = 3;
40680Sstevel@tonic-gate while (left > 0)
40690Sstevel@tonic-gate left = sleep(left);
40700Sstevel@tonic-gate
40710Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
40720Sstevel@tonic-gate /*
40730Sstevel@tonic-gate * Clearing these variables will allow the sulogin thread to run. We
40740Sstevel@tonic-gate * check here in case there aren't any more state updates anytime soon.
40750Sstevel@tonic-gate */
40760Sstevel@tonic-gate go_to_level1 = go_single_user_mode = booting_to_single_user = B_FALSE;
40770Sstevel@tonic-gate if (!sulogin_thread_running && !can_come_up()) {
40780Sstevel@tonic-gate (void) startd_thread_create(sulogin_thread, NULL);
40790Sstevel@tonic-gate sulogin_thread_running = B_TRUE;
40800Sstevel@tonic-gate }
40810Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
40820Sstevel@tonic-gate single_user_thread_count--;
40830Sstevel@tonic-gate MUTEX_UNLOCK(&single_user_thread_lock);
40840Sstevel@tonic-gate return (NULL);
40850Sstevel@tonic-gate }
40860Sstevel@tonic-gate
40870Sstevel@tonic-gate
40880Sstevel@tonic-gate /*
40890Sstevel@tonic-gate * Dependency graph operations API. These are handle-independent thread-safe
40900Sstevel@tonic-gate * graph manipulation functions which are the entry points for the event
40910Sstevel@tonic-gate * threads below.
40920Sstevel@tonic-gate */
40930Sstevel@tonic-gate
40940Sstevel@tonic-gate /*
40950Sstevel@tonic-gate * If a configured vertex exists for inst_fmri, return EEXIST. If no vertex
40960Sstevel@tonic-gate * exists for inst_fmri, add one. Then fetch the restarter from inst, make
40970Sstevel@tonic-gate * this vertex dependent on it, and send _ADD_INSTANCE to the restarter.
40980Sstevel@tonic-gate * Fetch whether the instance should be enabled from inst and send _ENABLE or
40990Sstevel@tonic-gate * _DISABLE as appropriate. Finally rummage through inst's dependency
41000Sstevel@tonic-gate * property groups and add vertices and edges as appropriate. If anything
41010Sstevel@tonic-gate * goes wrong after sending _ADD_INSTANCE, send _ADMIN_MAINT_ON to put the
41020Sstevel@tonic-gate * instance in maintenance. Don't send _START or _STOP until we get a state
41030Sstevel@tonic-gate * update in case we're being restarted and the service is already running.
41040Sstevel@tonic-gate *
41050Sstevel@tonic-gate * To support booting to a milestone, we must also make sure all dependencies
41060Sstevel@tonic-gate * encountered are configured, if they exist in the repository.
41070Sstevel@tonic-gate *
41080Sstevel@tonic-gate * Returns 0 on success, ECONNABORTED on repository disconnection, EINVAL if
41090Sstevel@tonic-gate * inst_fmri is an invalid (or not canonical) FMRI, ECANCELED if inst is
41100Sstevel@tonic-gate * deleted, or EEXIST if a configured vertex for inst_fmri already exists.
41110Sstevel@tonic-gate */
41120Sstevel@tonic-gate int
dgraph_add_instance(const char * inst_fmri,scf_instance_t * inst,boolean_t lock_graph)41130Sstevel@tonic-gate dgraph_add_instance(const char *inst_fmri, scf_instance_t *inst,
41140Sstevel@tonic-gate boolean_t lock_graph)
41150Sstevel@tonic-gate {
41160Sstevel@tonic-gate graph_vertex_t *v;
41170Sstevel@tonic-gate int err;
41180Sstevel@tonic-gate
41190Sstevel@tonic-gate if (strcmp(inst_fmri, SCF_SERVICE_STARTD) == 0)
41200Sstevel@tonic-gate return (0);
41210Sstevel@tonic-gate
41220Sstevel@tonic-gate /* Check for a vertex for inst_fmri. */
41230Sstevel@tonic-gate if (lock_graph) {
41240Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
41250Sstevel@tonic-gate } else {
412611466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
41270Sstevel@tonic-gate }
41280Sstevel@tonic-gate
41290Sstevel@tonic-gate v = vertex_get_by_name(inst_fmri);
41300Sstevel@tonic-gate
41310Sstevel@tonic-gate if (v != NULL) {
41320Sstevel@tonic-gate assert(v->gv_type == GVT_INST);
41330Sstevel@tonic-gate
41340Sstevel@tonic-gate if (v->gv_flags & GV_CONFIGURED) {
41350Sstevel@tonic-gate if (lock_graph)
41360Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
41370Sstevel@tonic-gate return (EEXIST);
41380Sstevel@tonic-gate }
41390Sstevel@tonic-gate } else {
41400Sstevel@tonic-gate /* Add the vertex. */
41410Sstevel@tonic-gate err = graph_insert_vertex_unconfigured(inst_fmri, GVT_INST, 0,
41420Sstevel@tonic-gate RERR_NONE, &v);
41430Sstevel@tonic-gate if (err != 0) {
41440Sstevel@tonic-gate assert(err == EINVAL);
41450Sstevel@tonic-gate if (lock_graph)
41460Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
41470Sstevel@tonic-gate return (EINVAL);
41480Sstevel@tonic-gate }
41490Sstevel@tonic-gate }
41500Sstevel@tonic-gate
41510Sstevel@tonic-gate err = configure_vertex(v, inst);
41520Sstevel@tonic-gate
41530Sstevel@tonic-gate if (lock_graph)
41540Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
41550Sstevel@tonic-gate
41560Sstevel@tonic-gate return (err);
41570Sstevel@tonic-gate }
41580Sstevel@tonic-gate
41590Sstevel@tonic-gate /*
41600Sstevel@tonic-gate * Locate the vertex for this property group's instance. If it doesn't exist
41610Sstevel@tonic-gate * or is unconfigured, call dgraph_add_instance() & return. Otherwise fetch
41620Sstevel@tonic-gate * the restarter for the instance, and if it has changed, send
41630Sstevel@tonic-gate * _REMOVE_INSTANCE to the old restarter, remove the dependency, make sure the
41640Sstevel@tonic-gate * new restarter has a vertex, add a new dependency, and send _ADD_INSTANCE to
41650Sstevel@tonic-gate * the new restarter. Then fetch whether the instance should be enabled, and
41660Sstevel@tonic-gate * if it is different from what we had, or if we changed the restarter, send
41670Sstevel@tonic-gate * the appropriate _ENABLE or _DISABLE command.
41680Sstevel@tonic-gate *
41690Sstevel@tonic-gate * Returns 0 on success, ENOTSUP if the pg's parent is not an instance,
41700Sstevel@tonic-gate * ECONNABORTED on repository disconnection, ECANCELED if the instance is
41710Sstevel@tonic-gate * deleted, or -1 if the instance's general property group is deleted or if
41720Sstevel@tonic-gate * its enabled property is misconfigured.
41730Sstevel@tonic-gate */
41740Sstevel@tonic-gate static int
dgraph_update_general(scf_propertygroup_t * pg)41750Sstevel@tonic-gate dgraph_update_general(scf_propertygroup_t *pg)
41760Sstevel@tonic-gate {
41770Sstevel@tonic-gate scf_handle_t *h;
41780Sstevel@tonic-gate scf_instance_t *inst;
41790Sstevel@tonic-gate char *fmri;
41800Sstevel@tonic-gate char *restarter_fmri;
41810Sstevel@tonic-gate graph_vertex_t *v;
41820Sstevel@tonic-gate int err;
41830Sstevel@tonic-gate int enabled, enabled_ovr;
41840Sstevel@tonic-gate int oldflags;
41850Sstevel@tonic-gate
41860Sstevel@tonic-gate /* Find the vertex for this service */
41870Sstevel@tonic-gate h = scf_pg_handle(pg);
41880Sstevel@tonic-gate
41890Sstevel@tonic-gate inst = safe_scf_instance_create(h);
41900Sstevel@tonic-gate
41910Sstevel@tonic-gate if (scf_pg_get_parent_instance(pg, inst) != 0) {
41920Sstevel@tonic-gate switch (scf_error()) {
41930Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
41940Sstevel@tonic-gate return (ENOTSUP);
41950Sstevel@tonic-gate
41960Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
41970Sstevel@tonic-gate default:
41980Sstevel@tonic-gate return (ECONNABORTED);
41990Sstevel@tonic-gate
42000Sstevel@tonic-gate case SCF_ERROR_DELETED:
42010Sstevel@tonic-gate return (0);
42020Sstevel@tonic-gate
42030Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
42040Sstevel@tonic-gate bad_error("scf_pg_get_parent_instance", scf_error());
42050Sstevel@tonic-gate }
42060Sstevel@tonic-gate }
42070Sstevel@tonic-gate
42080Sstevel@tonic-gate err = libscf_instance_get_fmri(inst, &fmri);
42090Sstevel@tonic-gate switch (err) {
42100Sstevel@tonic-gate case 0:
42110Sstevel@tonic-gate break;
42120Sstevel@tonic-gate
42130Sstevel@tonic-gate case ECONNABORTED:
42140Sstevel@tonic-gate scf_instance_destroy(inst);
42150Sstevel@tonic-gate return (ECONNABORTED);
42160Sstevel@tonic-gate
42170Sstevel@tonic-gate case ECANCELED:
42180Sstevel@tonic-gate scf_instance_destroy(inst);
42190Sstevel@tonic-gate return (0);
42200Sstevel@tonic-gate
42210Sstevel@tonic-gate default:
42220Sstevel@tonic-gate bad_error("libscf_instance_get_fmri", err);
42230Sstevel@tonic-gate }
42240Sstevel@tonic-gate
42250Sstevel@tonic-gate log_framework(LOG_DEBUG,
42260Sstevel@tonic-gate "Graph engine: Reloading general properties for %s.\n", fmri);
42270Sstevel@tonic-gate
42280Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
42290Sstevel@tonic-gate
42300Sstevel@tonic-gate v = vertex_get_by_name(fmri);
42310Sstevel@tonic-gate if (v == NULL || !(v->gv_flags & GV_CONFIGURED)) {
42320Sstevel@tonic-gate /* Will get the up-to-date properties. */
42330Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
42340Sstevel@tonic-gate err = dgraph_add_instance(fmri, inst, B_TRUE);
42350Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size);
42360Sstevel@tonic-gate scf_instance_destroy(inst);
42370Sstevel@tonic-gate return (err == ECANCELED ? 0 : err);
42380Sstevel@tonic-gate }
42390Sstevel@tonic-gate
42400Sstevel@tonic-gate /* Read enabled & restarter from repository. */
42410Sstevel@tonic-gate restarter_fmri = startd_alloc(max_scf_value_size);
42420Sstevel@tonic-gate err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled,
42430Sstevel@tonic-gate &enabled_ovr, &restarter_fmri);
42440Sstevel@tonic-gate if (err != 0 || enabled == -1) {
42450Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
42460Sstevel@tonic-gate scf_instance_destroy(inst);
42470Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size);
42480Sstevel@tonic-gate
42490Sstevel@tonic-gate switch (err) {
42500Sstevel@tonic-gate case ENOENT:
42510Sstevel@tonic-gate case 0:
42520Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size);
42530Sstevel@tonic-gate return (-1);
42540Sstevel@tonic-gate
42550Sstevel@tonic-gate case ECONNABORTED:
42560Sstevel@tonic-gate case ECANCELED:
42570Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size);
42580Sstevel@tonic-gate return (err);
42590Sstevel@tonic-gate
42600Sstevel@tonic-gate default:
42610Sstevel@tonic-gate bad_error("libscf_get_basic_instance_data", err);
42620Sstevel@tonic-gate }
42630Sstevel@tonic-gate }
42640Sstevel@tonic-gate
42650Sstevel@tonic-gate oldflags = v->gv_flags;
42660Sstevel@tonic-gate v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) |
42670Sstevel@tonic-gate (enabled ? GV_ENBLD_NOOVR : 0);
42680Sstevel@tonic-gate
42690Sstevel@tonic-gate if (enabled_ovr != -1)
42700Sstevel@tonic-gate enabled = enabled_ovr;
42710Sstevel@tonic-gate
42720Sstevel@tonic-gate /*
42730Sstevel@tonic-gate * If GV_ENBLD_NOOVR has changed, then we need to re-evaluate the
42740Sstevel@tonic-gate * subgraph.
42750Sstevel@tonic-gate */
42760Sstevel@tonic-gate if (milestone > MILESTONE_NONE && v->gv_flags != oldflags)
42770Sstevel@tonic-gate (void) eval_subgraph(v, h);
42780Sstevel@tonic-gate
42790Sstevel@tonic-gate scf_instance_destroy(inst);
42800Sstevel@tonic-gate
42810Sstevel@tonic-gate /* Ignore restarter change for now. */
42820Sstevel@tonic-gate
42830Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size);
42840Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size);
42850Sstevel@tonic-gate
42860Sstevel@tonic-gate /*
42870Sstevel@tonic-gate * Always send _ENABLE or _DISABLE. We could avoid this if the
42880Sstevel@tonic-gate * restarter didn't change and the enabled value didn't change, but
42890Sstevel@tonic-gate * that's not easy to check and improbable anyway, so we'll just do
42900Sstevel@tonic-gate * this.
42910Sstevel@tonic-gate */
42920Sstevel@tonic-gate graph_enable_by_vertex(v, enabled, 1);
42930Sstevel@tonic-gate
42940Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
42950Sstevel@tonic-gate
42960Sstevel@tonic-gate return (0);
42970Sstevel@tonic-gate }
42980Sstevel@tonic-gate
42990Sstevel@tonic-gate /*
43000Sstevel@tonic-gate * Delete all of the property group dependencies of v, update inst's running
43010Sstevel@tonic-gate * snapshot, and add the dependencies in the new snapshot. If any of the new
43020Sstevel@tonic-gate * dependencies would create a cycle, send _ADMIN_MAINT_ON. Otherwise
43030Sstevel@tonic-gate * reevaluate v's dependencies, send _START or _STOP as appropriate, and do
43040Sstevel@tonic-gate * the same for v's dependents.
43050Sstevel@tonic-gate *
43060Sstevel@tonic-gate * Returns
43070Sstevel@tonic-gate * 0 - success
43080Sstevel@tonic-gate * ECONNABORTED - repository connection broken
43090Sstevel@tonic-gate * ECANCELED - inst was deleted
43100Sstevel@tonic-gate * EINVAL - inst is invalid (e.g., missing general/enabled)
43110Sstevel@tonic-gate * -1 - libscf_snapshots_refresh() failed
43120Sstevel@tonic-gate */
43130Sstevel@tonic-gate static int
dgraph_refresh_instance(graph_vertex_t * v,scf_instance_t * inst)43140Sstevel@tonic-gate dgraph_refresh_instance(graph_vertex_t *v, scf_instance_t *inst)
43150Sstevel@tonic-gate {
43160Sstevel@tonic-gate int r;
43170Sstevel@tonic-gate int enabled;
4318*12967Sgavin.maltby@oracle.com int32_t tset;
43190Sstevel@tonic-gate
432011466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
43210Sstevel@tonic-gate assert(v->gv_type == GVT_INST);
43220Sstevel@tonic-gate
43230Sstevel@tonic-gate /* Only refresh services with valid general/enabled properties. */
43240Sstevel@tonic-gate r = libscf_get_basic_instance_data(scf_instance_handle(inst), inst,
43250Sstevel@tonic-gate v->gv_name, &enabled, NULL, NULL);
43260Sstevel@tonic-gate switch (r) {
43270Sstevel@tonic-gate case 0:
43280Sstevel@tonic-gate break;
43290Sstevel@tonic-gate
43300Sstevel@tonic-gate case ECONNABORTED:
43310Sstevel@tonic-gate case ECANCELED:
43320Sstevel@tonic-gate return (r);
43330Sstevel@tonic-gate
43340Sstevel@tonic-gate case ENOENT:
43350Sstevel@tonic-gate log_framework(LOG_DEBUG,
43360Sstevel@tonic-gate "Ignoring %s because it has no general property group.\n",
43370Sstevel@tonic-gate v->gv_name);
43380Sstevel@tonic-gate return (EINVAL);
43390Sstevel@tonic-gate
43400Sstevel@tonic-gate default:
43410Sstevel@tonic-gate bad_error("libscf_get_basic_instance_data", r);
43420Sstevel@tonic-gate }
43430Sstevel@tonic-gate
4344*12967Sgavin.maltby@oracle.com if ((tset = libscf_get_stn_tset(inst)) == -1) {
4345*12967Sgavin.maltby@oracle.com log_framework(LOG_WARNING,
4346*12967Sgavin.maltby@oracle.com "Failed to get notification parameters for %s: %s\n",
4347*12967Sgavin.maltby@oracle.com v->gv_name, scf_strerror(scf_error()));
4348*12967Sgavin.maltby@oracle.com tset = 0;
4349*12967Sgavin.maltby@oracle.com }
4350*12967Sgavin.maltby@oracle.com v->gv_stn_tset = tset;
4351*12967Sgavin.maltby@oracle.com if (strcmp(v->gv_name, SCF_INSTANCE_GLOBAL) == 0)
4352*12967Sgavin.maltby@oracle.com stn_global = tset;
4353*12967Sgavin.maltby@oracle.com
43540Sstevel@tonic-gate if (enabled == -1)
43550Sstevel@tonic-gate return (EINVAL);
43560Sstevel@tonic-gate
43570Sstevel@tonic-gate r = libscf_snapshots_refresh(inst, v->gv_name);
43580Sstevel@tonic-gate if (r != 0) {
43590Sstevel@tonic-gate if (r != -1)
43600Sstevel@tonic-gate bad_error("libscf_snapshots_refresh", r);
43610Sstevel@tonic-gate
43620Sstevel@tonic-gate /* error logged */
43630Sstevel@tonic-gate return (r);
43640Sstevel@tonic-gate }
43650Sstevel@tonic-gate
43660Sstevel@tonic-gate r = refresh_vertex(v, inst);
43670Sstevel@tonic-gate if (r != 0 && r != ECONNABORTED)
43680Sstevel@tonic-gate bad_error("refresh_vertex", r);
43690Sstevel@tonic-gate return (r);
43700Sstevel@tonic-gate }
43710Sstevel@tonic-gate
43720Sstevel@tonic-gate /*
43737630SRenaud.Manus@Sun.COM * Returns true only if none of this service's dependents are 'up' -- online
43747630SRenaud.Manus@Sun.COM * or degraded (offline is considered down in this situation). This function
43757630SRenaud.Manus@Sun.COM * is somehow similar to is_nonsubgraph_leaf() but works on subtrees.
43767630SRenaud.Manus@Sun.COM */
43777630SRenaud.Manus@Sun.COM static boolean_t
insubtree_dependents_down(graph_vertex_t * v)43787630SRenaud.Manus@Sun.COM insubtree_dependents_down(graph_vertex_t *v)
43797630SRenaud.Manus@Sun.COM {
43807630SRenaud.Manus@Sun.COM graph_vertex_t *vv;
43817630SRenaud.Manus@Sun.COM graph_edge_t *e;
43827630SRenaud.Manus@Sun.COM
438311466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
43847630SRenaud.Manus@Sun.COM
43857630SRenaud.Manus@Sun.COM for (e = uu_list_first(v->gv_dependents); e != NULL;
43867630SRenaud.Manus@Sun.COM e = uu_list_next(v->gv_dependents, e)) {
43877630SRenaud.Manus@Sun.COM vv = e->ge_vertex;
43887630SRenaud.Manus@Sun.COM if (vv->gv_type == GVT_INST) {
43897630SRenaud.Manus@Sun.COM if ((vv->gv_flags & GV_CONFIGURED) == 0)
43907630SRenaud.Manus@Sun.COM continue;
43917630SRenaud.Manus@Sun.COM
43927630SRenaud.Manus@Sun.COM if ((vv->gv_flags & GV_TOOFFLINE) == 0)
43937630SRenaud.Manus@Sun.COM continue;
43947630SRenaud.Manus@Sun.COM
43957630SRenaud.Manus@Sun.COM if ((vv->gv_state == RESTARTER_STATE_ONLINE) ||
43967630SRenaud.Manus@Sun.COM (vv->gv_state == RESTARTER_STATE_DEGRADED))
43977630SRenaud.Manus@Sun.COM return (B_FALSE);
43987630SRenaud.Manus@Sun.COM } else {
43997630SRenaud.Manus@Sun.COM /*
440012225SGowtham.Thommandra@Sun.COM * Skip all excluded and optional_all dependencies
440112225SGowtham.Thommandra@Sun.COM * and decide whether to offline the service based
440212225SGowtham.Thommandra@Sun.COM * on restart_on attribute.
440312225SGowtham.Thommandra@Sun.COM */
440412225SGowtham.Thommandra@Sun.COM if (is_depgrp_bypassed(vv))
440512225SGowtham.Thommandra@Sun.COM continue;
440612225SGowtham.Thommandra@Sun.COM
440712225SGowtham.Thommandra@Sun.COM /*
44087630SRenaud.Manus@Sun.COM * For dependency groups or service vertices, keep
44097630SRenaud.Manus@Sun.COM * traversing to see if instances are running.
44107630SRenaud.Manus@Sun.COM */
44117630SRenaud.Manus@Sun.COM if (insubtree_dependents_down(vv) == B_FALSE)
44127630SRenaud.Manus@Sun.COM return (B_FALSE);
44137630SRenaud.Manus@Sun.COM }
44147630SRenaud.Manus@Sun.COM }
44157630SRenaud.Manus@Sun.COM
44167630SRenaud.Manus@Sun.COM return (B_TRUE);
44177630SRenaud.Manus@Sun.COM }
44187630SRenaud.Manus@Sun.COM
44197630SRenaud.Manus@Sun.COM /*
44202747Sbustos * Returns true only if none of this service's dependents are 'up' -- online,
44212747Sbustos * degraded, or offline.
44220Sstevel@tonic-gate */
44230Sstevel@tonic-gate static int
is_nonsubgraph_leaf(graph_vertex_t * v)44242747Sbustos is_nonsubgraph_leaf(graph_vertex_t *v)
44250Sstevel@tonic-gate {
44260Sstevel@tonic-gate graph_vertex_t *vv;
44270Sstevel@tonic-gate graph_edge_t *e;
44280Sstevel@tonic-gate
442911466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
44300Sstevel@tonic-gate
44310Sstevel@tonic-gate for (e = uu_list_first(v->gv_dependents);
44320Sstevel@tonic-gate e != NULL;
44330Sstevel@tonic-gate e = uu_list_next(v->gv_dependents, e)) {
44340Sstevel@tonic-gate
44350Sstevel@tonic-gate vv = e->ge_vertex;
44360Sstevel@tonic-gate if (vv->gv_type == GVT_INST) {
44372747Sbustos if ((vv->gv_flags & GV_CONFIGURED) == 0)
44382747Sbustos continue;
44392747Sbustos
44402747Sbustos if (vv->gv_flags & GV_INSUBGRAPH)
44412747Sbustos continue;
44422747Sbustos
44432747Sbustos if (up_state(vv->gv_state))
44442747Sbustos return (0);
44450Sstevel@tonic-gate } else {
44460Sstevel@tonic-gate /*
44470Sstevel@tonic-gate * For dependency group or service vertices, keep
44480Sstevel@tonic-gate * traversing to see if instances are running.
44497630SRenaud.Manus@Sun.COM *
44507630SRenaud.Manus@Sun.COM * We should skip exclude_all dependencies otherwise
44517630SRenaud.Manus@Sun.COM * the vertex will never be considered as a leaf
44527630SRenaud.Manus@Sun.COM * if the dependent is offline. The main reason for
44537630SRenaud.Manus@Sun.COM * this is that disable_nonsubgraph_leaves() skips
44547630SRenaud.Manus@Sun.COM * exclusion dependencies.
44550Sstevel@tonic-gate */
44567630SRenaud.Manus@Sun.COM if (vv->gv_type == GVT_GROUP &&
44577630SRenaud.Manus@Sun.COM vv->gv_depgroup == DEPGRP_EXCLUDE_ALL)
44587630SRenaud.Manus@Sun.COM continue;
44597630SRenaud.Manus@Sun.COM
44602747Sbustos if (!is_nonsubgraph_leaf(vv))
44612747Sbustos return (0);
44620Sstevel@tonic-gate }
44630Sstevel@tonic-gate }
44642747Sbustos
44652747Sbustos return (1);
44660Sstevel@tonic-gate }
44670Sstevel@tonic-gate
44680Sstevel@tonic-gate /*
44692747Sbustos * Disable v temporarily. Attempt to do this by setting its enabled override
44702747Sbustos * property in the repository. If that fails, send a _DISABLE command.
44712747Sbustos * Returns 0 on success and ECONNABORTED if the repository connection is
44722747Sbustos * broken.
44730Sstevel@tonic-gate */
44742747Sbustos static int
disable_service_temporarily(graph_vertex_t * v,scf_handle_t * h)44752747Sbustos disable_service_temporarily(graph_vertex_t *v, scf_handle_t *h)
44760Sstevel@tonic-gate {
44772747Sbustos const char * const emsg = "Could not temporarily disable %s because "
44782747Sbustos "%s. Will stop service anyways. Repository status for the "
44792747Sbustos "service may be inaccurate.\n";
44802747Sbustos const char * const emsg_cbroken =
44812747Sbustos "the repository connection was broken";
44822747Sbustos
44832747Sbustos scf_instance_t *inst;
44840Sstevel@tonic-gate int r;
44850Sstevel@tonic-gate
44860Sstevel@tonic-gate inst = scf_instance_create(h);
44870Sstevel@tonic-gate if (inst == NULL) {
44882747Sbustos char buf[100];
44892747Sbustos
44902747Sbustos (void) snprintf(buf, sizeof (buf),
44912747Sbustos "scf_instance_create() failed (%s)",
44922747Sbustos scf_strerror(scf_error()));
44932747Sbustos log_error(LOG_WARNING, emsg, v->gv_name, buf);
44942747Sbustos
44952747Sbustos graph_enable_by_vertex(v, 0, 0);
44962747Sbustos return (0);
44970Sstevel@tonic-gate }
44982747Sbustos
44990Sstevel@tonic-gate r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst,
45000Sstevel@tonic-gate NULL, NULL, SCF_DECODE_FMRI_EXACT);
45010Sstevel@tonic-gate if (r != 0) {
45020Sstevel@tonic-gate switch (scf_error()) {
45030Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
45042747Sbustos log_error(LOG_WARNING, emsg, v->gv_name, emsg_cbroken);
45052747Sbustos graph_enable_by_vertex(v, 0, 0);
45062747Sbustos return (ECONNABORTED);
45070Sstevel@tonic-gate
45080Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
45092747Sbustos return (0);
45100Sstevel@tonic-gate
45110Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
45120Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
45130Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
45140Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
45150Sstevel@tonic-gate default:
45160Sstevel@tonic-gate bad_error("scf_handle_decode_fmri",
45170Sstevel@tonic-gate scf_error());
45180Sstevel@tonic-gate }
45190Sstevel@tonic-gate }
45202747Sbustos
45210Sstevel@tonic-gate r = libscf_set_enable_ovr(inst, 0);
45220Sstevel@tonic-gate switch (r) {
45230Sstevel@tonic-gate case 0:
45240Sstevel@tonic-gate scf_instance_destroy(inst);
45252747Sbustos return (0);
45262747Sbustos
45270Sstevel@tonic-gate case ECANCELED:
45280Sstevel@tonic-gate scf_instance_destroy(inst);
45292747Sbustos return (0);
45302747Sbustos
45310Sstevel@tonic-gate case ECONNABORTED:
45322747Sbustos log_error(LOG_WARNING, emsg, v->gv_name, emsg_cbroken);
45332747Sbustos graph_enable_by_vertex(v, 0, 0);
45342747Sbustos return (ECONNABORTED);
45352747Sbustos
45360Sstevel@tonic-gate case EPERM:
45372747Sbustos log_error(LOG_WARNING, emsg, v->gv_name,
45382747Sbustos "the repository denied permission");
45392747Sbustos graph_enable_by_vertex(v, 0, 0);
45402747Sbustos return (0);
45412747Sbustos
45420Sstevel@tonic-gate case EROFS:
45432747Sbustos log_error(LOG_WARNING, emsg, v->gv_name,
45442747Sbustos "the repository is read-only");
45452747Sbustos graph_enable_by_vertex(v, 0, 0);
45462747Sbustos return (0);
45472747Sbustos
45480Sstevel@tonic-gate default:
45490Sstevel@tonic-gate bad_error("libscf_set_enable_ovr", r);
45502747Sbustos /* NOTREACHED */
45510Sstevel@tonic-gate }
45522747Sbustos }
45532747Sbustos
45542747Sbustos /*
45557630SRenaud.Manus@Sun.COM * Of the transitive instance dependencies of v, offline those which are
45567630SRenaud.Manus@Sun.COM * in the subtree and which are leaves (i.e., have no dependents which are
45577630SRenaud.Manus@Sun.COM * "up").
45587630SRenaud.Manus@Sun.COM */
45597630SRenaud.Manus@Sun.COM void
offline_subtree_leaves(graph_vertex_t * v,void * arg)45607630SRenaud.Manus@Sun.COM offline_subtree_leaves(graph_vertex_t *v, void *arg)
45617630SRenaud.Manus@Sun.COM {
456211466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
45637630SRenaud.Manus@Sun.COM
45647630SRenaud.Manus@Sun.COM /* If v isn't an instance, recurse on its dependencies. */
45657630SRenaud.Manus@Sun.COM if (v->gv_type != GVT_INST) {
45667630SRenaud.Manus@Sun.COM graph_walk_dependencies(v, offline_subtree_leaves, arg);
45677630SRenaud.Manus@Sun.COM return;
45687630SRenaud.Manus@Sun.COM }
45697630SRenaud.Manus@Sun.COM
45707630SRenaud.Manus@Sun.COM /*
45717630SRenaud.Manus@Sun.COM * If v is not in the subtree, so should all of its dependencies,
45727630SRenaud.Manus@Sun.COM * so do nothing.
45737630SRenaud.Manus@Sun.COM */
45747630SRenaud.Manus@Sun.COM if ((v->gv_flags & GV_TOOFFLINE) == 0)
45757630SRenaud.Manus@Sun.COM return;
45767630SRenaud.Manus@Sun.COM
45777630SRenaud.Manus@Sun.COM /* If v isn't a leaf because it's already down, recurse. */
45787630SRenaud.Manus@Sun.COM if (!up_state(v->gv_state)) {
45797630SRenaud.Manus@Sun.COM graph_walk_dependencies(v, offline_subtree_leaves, arg);
45807630SRenaud.Manus@Sun.COM return;
45817630SRenaud.Manus@Sun.COM }
45827630SRenaud.Manus@Sun.COM
45837630SRenaud.Manus@Sun.COM /* if v is a leaf, offline it or disable it if it's the last one */
45847630SRenaud.Manus@Sun.COM if (insubtree_dependents_down(v) == B_TRUE) {
45857630SRenaud.Manus@Sun.COM if (v->gv_flags & GV_TODISABLE)
45867630SRenaud.Manus@Sun.COM vertex_send_event(v,
45877630SRenaud.Manus@Sun.COM RESTARTER_EVENT_TYPE_ADMIN_DISABLE);
45887630SRenaud.Manus@Sun.COM else
45897630SRenaud.Manus@Sun.COM offline_vertex(v);
45907630SRenaud.Manus@Sun.COM }
45917630SRenaud.Manus@Sun.COM }
45927630SRenaud.Manus@Sun.COM
45937630SRenaud.Manus@Sun.COM void
graph_offline_subtree_leaves(graph_vertex_t * v,void * h)45947630SRenaud.Manus@Sun.COM graph_offline_subtree_leaves(graph_vertex_t *v, void *h)
45957630SRenaud.Manus@Sun.COM {
45967630SRenaud.Manus@Sun.COM graph_walk_dependencies(v, offline_subtree_leaves, (void *)h);
45977630SRenaud.Manus@Sun.COM }
45987630SRenaud.Manus@Sun.COM
45997630SRenaud.Manus@Sun.COM
46007630SRenaud.Manus@Sun.COM /*
46012747Sbustos * Of the transitive instance dependencies of v, disable those which are not
46022747Sbustos * in the subgraph and which are leaves (i.e., have no dependents which are
46032747Sbustos * "up").
46042747Sbustos */
46052747Sbustos static void
disable_nonsubgraph_leaves(graph_vertex_t * v,void * arg)46062747Sbustos disable_nonsubgraph_leaves(graph_vertex_t *v, void *arg)
46072747Sbustos {
460811466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
46092747Sbustos
46104762Snakanon /*
46114762Snakanon * We must skip exclusion dependencies because they are allowed to
46124762Snakanon * complete dependency cycles. This is correct because A's exclusion
46134762Snakanon * dependency on B doesn't bear on the order in which they should be
46144762Snakanon * stopped. Indeed, the exclusion dependency should guarantee that
46154762Snakanon * they are never online at the same time.
46164762Snakanon */
46174762Snakanon if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
46184762Snakanon return;
46194762Snakanon
46202747Sbustos /* If v isn't an instance, recurse on its dependencies. */
46212747Sbustos if (v->gv_type != GVT_INST)
46222747Sbustos goto recurse;
46232747Sbustos
46242747Sbustos if ((v->gv_flags & GV_CONFIGURED) == 0)
46252747Sbustos /*
46262747Sbustos * Unconfigured instances should have no dependencies, but in
46272747Sbustos * case they ever get them,
46282747Sbustos */
46292747Sbustos goto recurse;
46302747Sbustos
46312747Sbustos /*
46322747Sbustos * If v is in the subgraph, so should all of its dependencies, so do
46332747Sbustos * nothing.
46342747Sbustos */
46352747Sbustos if (v->gv_flags & GV_INSUBGRAPH)
46362747Sbustos return;
46372747Sbustos
46382747Sbustos /* If v isn't a leaf because it's already down, recurse. */
46392747Sbustos if (!up_state(v->gv_state))
46402747Sbustos goto recurse;
46412747Sbustos
46422747Sbustos /* If v is disabled but not down yet, be patient. */
46432747Sbustos if ((v->gv_flags & GV_ENABLED) == 0)
46442747Sbustos return;
46452747Sbustos
46462747Sbustos /* If v is a leaf, disable it. */
46472747Sbustos if (is_nonsubgraph_leaf(v))
46482747Sbustos (void) disable_service_temporarily(v, (scf_handle_t *)arg);
46492747Sbustos
46500Sstevel@tonic-gate return;
46512747Sbustos
46520Sstevel@tonic-gate recurse:
46532747Sbustos graph_walk_dependencies(v, disable_nonsubgraph_leaves, arg);
46540Sstevel@tonic-gate }
46550Sstevel@tonic-gate
4656*12967Sgavin.maltby@oracle.com static int
stn_restarter_state(restarter_instance_state_t rstate)4657*12967Sgavin.maltby@oracle.com stn_restarter_state(restarter_instance_state_t rstate)
4658*12967Sgavin.maltby@oracle.com {
4659*12967Sgavin.maltby@oracle.com static const struct statemap {
4660*12967Sgavin.maltby@oracle.com restarter_instance_state_t restarter_state;
4661*12967Sgavin.maltby@oracle.com int scf_state;
4662*12967Sgavin.maltby@oracle.com } map[] = {
4663*12967Sgavin.maltby@oracle.com { RESTARTER_STATE_UNINIT, SCF_STATE_UNINIT },
4664*12967Sgavin.maltby@oracle.com { RESTARTER_STATE_MAINT, SCF_STATE_MAINT },
4665*12967Sgavin.maltby@oracle.com { RESTARTER_STATE_OFFLINE, SCF_STATE_OFFLINE },
4666*12967Sgavin.maltby@oracle.com { RESTARTER_STATE_DISABLED, SCF_STATE_DISABLED },
4667*12967Sgavin.maltby@oracle.com { RESTARTER_STATE_ONLINE, SCF_STATE_ONLINE },
4668*12967Sgavin.maltby@oracle.com { RESTARTER_STATE_DEGRADED, SCF_STATE_DEGRADED }
4669*12967Sgavin.maltby@oracle.com };
4670*12967Sgavin.maltby@oracle.com
4671*12967Sgavin.maltby@oracle.com int i;
4672*12967Sgavin.maltby@oracle.com
4673*12967Sgavin.maltby@oracle.com for (i = 0; i < sizeof (map) / sizeof (map[0]); i++) {
4674*12967Sgavin.maltby@oracle.com if (rstate == map[i].restarter_state)
4675*12967Sgavin.maltby@oracle.com return (map[i].scf_state);
4676*12967Sgavin.maltby@oracle.com }
4677*12967Sgavin.maltby@oracle.com
4678*12967Sgavin.maltby@oracle.com return (-1);
4679*12967Sgavin.maltby@oracle.com }
4680*12967Sgavin.maltby@oracle.com
4681*12967Sgavin.maltby@oracle.com /*
4682*12967Sgavin.maltby@oracle.com * State transition counters
4683*12967Sgavin.maltby@oracle.com * Not incremented atomically - indicative only
4684*12967Sgavin.maltby@oracle.com */
4685*12967Sgavin.maltby@oracle.com static uint64_t stev_ct_maint;
4686*12967Sgavin.maltby@oracle.com static uint64_t stev_ct_hwerr;
4687*12967Sgavin.maltby@oracle.com static uint64_t stev_ct_service;
4688*12967Sgavin.maltby@oracle.com static uint64_t stev_ct_global;
4689*12967Sgavin.maltby@oracle.com static uint64_t stev_ct_noprefs;
4690*12967Sgavin.maltby@oracle.com static uint64_t stev_ct_from_uninit;
4691*12967Sgavin.maltby@oracle.com static uint64_t stev_ct_bad_state;
4692*12967Sgavin.maltby@oracle.com static uint64_t stev_ct_ovr_prefs;
4693*12967Sgavin.maltby@oracle.com
4694*12967Sgavin.maltby@oracle.com static void
dgraph_state_transition_notify(graph_vertex_t * v,restarter_instance_state_t old_state,restarter_str_t reason)4695*12967Sgavin.maltby@oracle.com dgraph_state_transition_notify(graph_vertex_t *v,
4696*12967Sgavin.maltby@oracle.com restarter_instance_state_t old_state, restarter_str_t reason)
4697*12967Sgavin.maltby@oracle.com {
4698*12967Sgavin.maltby@oracle.com restarter_instance_state_t new_state = v->gv_state;
4699*12967Sgavin.maltby@oracle.com int stn_transition, maint;
4700*12967Sgavin.maltby@oracle.com int from, to;
4701*12967Sgavin.maltby@oracle.com nvlist_t *attr;
4702*12967Sgavin.maltby@oracle.com fmev_pri_t pri = FMEV_LOPRI;
4703*12967Sgavin.maltby@oracle.com int raise = 0;
4704*12967Sgavin.maltby@oracle.com
4705*12967Sgavin.maltby@oracle.com if ((from = stn_restarter_state(old_state)) == -1 ||
4706*12967Sgavin.maltby@oracle.com (to = stn_restarter_state(new_state)) == -1) {
4707*12967Sgavin.maltby@oracle.com stev_ct_bad_state++;
4708*12967Sgavin.maltby@oracle.com return;
4709*12967Sgavin.maltby@oracle.com }
4710*12967Sgavin.maltby@oracle.com
4711*12967Sgavin.maltby@oracle.com stn_transition = from << 16 | to;
4712*12967Sgavin.maltby@oracle.com
4713*12967Sgavin.maltby@oracle.com maint = (to == SCF_STATE_MAINT || from == SCF_STATE_MAINT);
4714*12967Sgavin.maltby@oracle.com
4715*12967Sgavin.maltby@oracle.com if (maint) {
4716*12967Sgavin.maltby@oracle.com /*
4717*12967Sgavin.maltby@oracle.com * All transitions to/from maintenance state must raise
4718*12967Sgavin.maltby@oracle.com * an event.
4719*12967Sgavin.maltby@oracle.com */
4720*12967Sgavin.maltby@oracle.com raise++;
4721*12967Sgavin.maltby@oracle.com pri = FMEV_HIPRI;
4722*12967Sgavin.maltby@oracle.com stev_ct_maint++;
4723*12967Sgavin.maltby@oracle.com } else if (reason == restarter_str_ct_ev_hwerr) {
4724*12967Sgavin.maltby@oracle.com /*
4725*12967Sgavin.maltby@oracle.com * All transitions caused by hardware fault must raise
4726*12967Sgavin.maltby@oracle.com * an event
4727*12967Sgavin.maltby@oracle.com */
4728*12967Sgavin.maltby@oracle.com raise++;
4729*12967Sgavin.maltby@oracle.com pri = FMEV_HIPRI;
4730*12967Sgavin.maltby@oracle.com stev_ct_hwerr++;
4731*12967Sgavin.maltby@oracle.com } else if (stn_transition & v->gv_stn_tset) {
4732*12967Sgavin.maltby@oracle.com /*
4733*12967Sgavin.maltby@oracle.com * Specifically enabled event.
4734*12967Sgavin.maltby@oracle.com */
4735*12967Sgavin.maltby@oracle.com raise++;
4736*12967Sgavin.maltby@oracle.com stev_ct_service++;
4737*12967Sgavin.maltby@oracle.com } else if (from == SCF_STATE_UNINIT) {
4738*12967Sgavin.maltby@oracle.com /*
4739*12967Sgavin.maltby@oracle.com * Only raise these if specifically selected above.
4740*12967Sgavin.maltby@oracle.com */
4741*12967Sgavin.maltby@oracle.com stev_ct_from_uninit++;
4742*12967Sgavin.maltby@oracle.com } else if (stn_transition & stn_global &&
4743*12967Sgavin.maltby@oracle.com (IS_ENABLED(v) == 1 || to == SCF_STATE_DISABLED)) {
4744*12967Sgavin.maltby@oracle.com raise++;
4745*12967Sgavin.maltby@oracle.com stev_ct_global++;
4746*12967Sgavin.maltby@oracle.com } else {
4747*12967Sgavin.maltby@oracle.com stev_ct_noprefs++;
4748*12967Sgavin.maltby@oracle.com }
4749*12967Sgavin.maltby@oracle.com
4750*12967Sgavin.maltby@oracle.com if (info_events_all) {
4751*12967Sgavin.maltby@oracle.com stev_ct_ovr_prefs++;
4752*12967Sgavin.maltby@oracle.com raise++;
4753*12967Sgavin.maltby@oracle.com }
4754*12967Sgavin.maltby@oracle.com if (!raise)
4755*12967Sgavin.maltby@oracle.com return;
4756*12967Sgavin.maltby@oracle.com
4757*12967Sgavin.maltby@oracle.com if (nvlist_alloc(&attr, NV_UNIQUE_NAME, 0) != 0 ||
4758*12967Sgavin.maltby@oracle.com nvlist_add_string(attr, "fmri", v->gv_name) != 0 ||
4759*12967Sgavin.maltby@oracle.com nvlist_add_uint32(attr, "reason-version",
4760*12967Sgavin.maltby@oracle.com restarter_str_version()) || nvlist_add_string(attr, "reason-short",
4761*12967Sgavin.maltby@oracle.com restarter_get_str_short(reason)) != 0 ||
4762*12967Sgavin.maltby@oracle.com nvlist_add_string(attr, "reason-long",
4763*12967Sgavin.maltby@oracle.com restarter_get_str_long(reason)) != 0 ||
4764*12967Sgavin.maltby@oracle.com nvlist_add_int32(attr, "transition", stn_transition) != 0) {
4765*12967Sgavin.maltby@oracle.com log_framework(LOG_WARNING,
4766*12967Sgavin.maltby@oracle.com "FMEV: %s could not create nvlist for transition "
4767*12967Sgavin.maltby@oracle.com "event: %s\n", v->gv_name, strerror(errno));
4768*12967Sgavin.maltby@oracle.com nvlist_free(attr);
4769*12967Sgavin.maltby@oracle.com return;
4770*12967Sgavin.maltby@oracle.com }
4771*12967Sgavin.maltby@oracle.com
4772*12967Sgavin.maltby@oracle.com if (fmev_rspublish_nvl(FMEV_RULESET_SMF, "state-transition",
4773*12967Sgavin.maltby@oracle.com instance_state_str[new_state], pri, attr) != FMEV_SUCCESS) {
4774*12967Sgavin.maltby@oracle.com log_framework(LOG_DEBUG,
4775*12967Sgavin.maltby@oracle.com "FMEV: %s failed to publish transition event: %s\n",
4776*12967Sgavin.maltby@oracle.com v->gv_name, fmev_strerror(fmev_errno));
4777*12967Sgavin.maltby@oracle.com nvlist_free(attr);
4778*12967Sgavin.maltby@oracle.com }
4779*12967Sgavin.maltby@oracle.com }
4780*12967Sgavin.maltby@oracle.com
47810Sstevel@tonic-gate /*
47820Sstevel@tonic-gate * Find the vertex for inst_name. If it doesn't exist, return ENOENT.
47830Sstevel@tonic-gate * Otherwise set its state to state. If the instance has entered a state
47840Sstevel@tonic-gate * which requires automatic action, take it (Uninitialized: do
47850Sstevel@tonic-gate * dgraph_refresh_instance() without the snapshot update. Disabled: if the
47860Sstevel@tonic-gate * instance should be enabled, send _ENABLE. Offline: if the instance should
47870Sstevel@tonic-gate * be disabled, send _DISABLE, and if its dependencies are satisfied, send
47880Sstevel@tonic-gate * _START. Online, Degraded: if the instance wasn't running, update its start
47890Sstevel@tonic-gate * snapshot. Maintenance: no action.)
47900Sstevel@tonic-gate *
47910Sstevel@tonic-gate * Also fails with ECONNABORTED, or EINVAL if state is invalid.
47920Sstevel@tonic-gate */
47930Sstevel@tonic-gate static int
dgraph_set_instance_state(scf_handle_t * h,const char * inst_name,protocol_states_t * states)47940Sstevel@tonic-gate dgraph_set_instance_state(scf_handle_t *h, const char *inst_name,
4795*12967Sgavin.maltby@oracle.com protocol_states_t *states)
47960Sstevel@tonic-gate {
47970Sstevel@tonic-gate graph_vertex_t *v;
47981958Slianep int err = 0;
47990Sstevel@tonic-gate restarter_instance_state_t old_state;
4800*12967Sgavin.maltby@oracle.com restarter_instance_state_t state = states->ps_state;
4801*12967Sgavin.maltby@oracle.com restarter_error_t serr = states->ps_err;
48020Sstevel@tonic-gate
48030Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
48040Sstevel@tonic-gate
48050Sstevel@tonic-gate v = vertex_get_by_name(inst_name);
48060Sstevel@tonic-gate if (v == NULL) {
48070Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
48080Sstevel@tonic-gate return (ENOENT);
48090Sstevel@tonic-gate }
48100Sstevel@tonic-gate
48112747Sbustos assert(v->gv_type == GVT_INST);
48122747Sbustos
48130Sstevel@tonic-gate switch (state) {
48140Sstevel@tonic-gate case RESTARTER_STATE_UNINIT:
48150Sstevel@tonic-gate case RESTARTER_STATE_DISABLED:
48160Sstevel@tonic-gate case RESTARTER_STATE_OFFLINE:
48170Sstevel@tonic-gate case RESTARTER_STATE_ONLINE:
48180Sstevel@tonic-gate case RESTARTER_STATE_DEGRADED:
48190Sstevel@tonic-gate case RESTARTER_STATE_MAINT:
48200Sstevel@tonic-gate break;
48210Sstevel@tonic-gate
48220Sstevel@tonic-gate default:
48230Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
48240Sstevel@tonic-gate return (EINVAL);
48250Sstevel@tonic-gate }
48260Sstevel@tonic-gate
48270Sstevel@tonic-gate log_framework(LOG_DEBUG, "Graph noting %s %s -> %s.\n", v->gv_name,
48280Sstevel@tonic-gate instance_state_str[v->gv_state], instance_state_str[state]);
48290Sstevel@tonic-gate
48300Sstevel@tonic-gate old_state = v->gv_state;
48310Sstevel@tonic-gate v->gv_state = state;
48320Sstevel@tonic-gate
4833*12967Sgavin.maltby@oracle.com v->gv_reason = states->ps_reason;
48342339Slianep err = gt_transition(h, v, serr, old_state);
4835*12967Sgavin.maltby@oracle.com if (err == 0 && v->gv_state != old_state) {
4836*12967Sgavin.maltby@oracle.com dgraph_state_transition_notify(v, old_state, states->ps_reason);
4837*12967Sgavin.maltby@oracle.com }
48381958Slianep
48391958Slianep MUTEX_UNLOCK(&dgraph_lock);
48401958Slianep return (err);
48411958Slianep }
48421958Slianep
48431958Slianep /*
48442747Sbustos * Handle state changes during milestone shutdown. See
48452747Sbustos * dgraph_set_milestone(). If the repository connection is broken,
48462747Sbustos * ECONNABORTED will be returned, though a _DISABLE command will be sent for
48472747Sbustos * the vertex anyway.
48481958Slianep */
48492747Sbustos int
vertex_subgraph_dependencies_shutdown(scf_handle_t * h,graph_vertex_t * v,restarter_instance_state_t old_state)48501958Slianep vertex_subgraph_dependencies_shutdown(scf_handle_t *h, graph_vertex_t *v,
48512747Sbustos restarter_instance_state_t old_state)
48521958Slianep {
48532747Sbustos int was_up, now_up;
48542747Sbustos int ret = 0;
48552747Sbustos
48562747Sbustos assert(v->gv_type == GVT_INST);
48572747Sbustos
48582747Sbustos /* Don't care if we're not going to a milestone. */
48592747Sbustos if (milestone == NULL)
48602747Sbustos return (0);
48612747Sbustos
48622747Sbustos /* Don't care if we already finished coming down. */
48632747Sbustos if (non_subgraph_svcs == 0)
48642747Sbustos return (0);
48652747Sbustos
48662747Sbustos /* Don't care if the service is in the subgraph. */
48672747Sbustos if (v->gv_flags & GV_INSUBGRAPH)
48682747Sbustos return (0);
48692747Sbustos
48702747Sbustos /*
48712747Sbustos * Update non_subgraph_svcs. It is the number of non-subgraph
48722747Sbustos * services which are in online, degraded, or offline.
48732747Sbustos */
48742747Sbustos
48752747Sbustos was_up = up_state(old_state);
48762747Sbustos now_up = up_state(v->gv_state);
48772747Sbustos
48782747Sbustos if (!was_up && now_up) {
48792747Sbustos ++non_subgraph_svcs;
48802747Sbustos } else if (was_up && !now_up) {
48810Sstevel@tonic-gate --non_subgraph_svcs;
48822747Sbustos
48830Sstevel@tonic-gate if (non_subgraph_svcs == 0) {
48840Sstevel@tonic-gate if (halting != -1) {
48850Sstevel@tonic-gate do_uadmin();
48860Sstevel@tonic-gate } else if (go_single_user_mode || go_to_level1) {
48870Sstevel@tonic-gate (void) startd_thread_create(single_user_thread,
48880Sstevel@tonic-gate NULL);
48890Sstevel@tonic-gate }
48902747Sbustos return (0);
48910Sstevel@tonic-gate }
48920Sstevel@tonic-gate }
48932747Sbustos
48942747Sbustos /* If this service is a leaf, it should be disabled. */
48952747Sbustos if ((v->gv_flags & GV_ENABLED) && is_nonsubgraph_leaf(v)) {
48962747Sbustos int r;
48972747Sbustos
48982747Sbustos r = disable_service_temporarily(v, h);
48992747Sbustos switch (r) {
49002747Sbustos case 0:
49012747Sbustos break;
49022747Sbustos
49032747Sbustos case ECONNABORTED:
49042747Sbustos ret = ECONNABORTED;
49052747Sbustos break;
49062747Sbustos
49072747Sbustos default:
49082747Sbustos bad_error("disable_service_temporarily", r);
49092747Sbustos }
49102747Sbustos }
49112747Sbustos
49122747Sbustos /*
49132747Sbustos * If the service just came down, propagate the disable to the newly
49142747Sbustos * exposed leaves.
49152747Sbustos */
49162747Sbustos if (was_up && !now_up)
49172747Sbustos graph_walk_dependencies(v, disable_nonsubgraph_leaves,
49182747Sbustos (void *)h);
49192747Sbustos
49202747Sbustos return (ret);
49211958Slianep }
49221958Slianep
49231958Slianep /*
49241958Slianep * Decide whether to start up an sulogin thread after a service is
49251958Slianep * finished changing state. Only need to do the full can_come_up()
49261958Slianep * evaluation if an instance is changing state, we're not halfway through
49271958Slianep * loading the thread, and we aren't shutting down or going to the single
49281958Slianep * user milestone.
49291958Slianep */
49301958Slianep void
graph_transition_sulogin(restarter_instance_state_t state,restarter_instance_state_t old_state)49311958Slianep graph_transition_sulogin(restarter_instance_state_t state,
49321958Slianep restarter_instance_state_t old_state)
49331958Slianep {
493411466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
49350Sstevel@tonic-gate
49360Sstevel@tonic-gate if (state != old_state && st->st_load_complete &&
49370Sstevel@tonic-gate !go_single_user_mode && !go_to_level1 &&
49380Sstevel@tonic-gate halting == -1) {
49393639Srm88369 if (!sulogin_thread_running && !can_come_up()) {
49400Sstevel@tonic-gate (void) startd_thread_create(sulogin_thread, NULL);
49410Sstevel@tonic-gate sulogin_thread_running = B_TRUE;
49420Sstevel@tonic-gate }
49430Sstevel@tonic-gate }
49441958Slianep }
49451958Slianep
49461958Slianep /*
49472339Slianep * Propagate a start, stop event, or a satisfiability event.
49481958Slianep *
49492339Slianep * PROPAGATE_START and PROPAGATE_STOP simply propagate the transition event
49502339Slianep * to direct dependents. PROPAGATE_SAT propagates a start then walks the
49512339Slianep * full dependent graph to check for newly satisfied nodes. This is
49522339Slianep * necessary for cases when non-direct dependents may be effected but direct
49532339Slianep * dependents may not (e.g. for optional_all evaluations, see the
49542339Slianep * propagate_satbility() comments).
49552339Slianep *
49562339Slianep * PROPAGATE_SAT should be used whenever a non-running service moves into
49572339Slianep * a state which can satisfy optional dependencies, like disabled or
49582339Slianep * maintenance.
49591958Slianep */
49601958Slianep void
graph_transition_propagate(graph_vertex_t * v,propagate_event_t type,restarter_error_t rerr)49612339Slianep graph_transition_propagate(graph_vertex_t *v, propagate_event_t type,
49621958Slianep restarter_error_t rerr)
49631958Slianep {
49642339Slianep if (type == PROPAGATE_STOP) {
49651958Slianep graph_walk_dependents(v, propagate_stop, (void *)rerr);
49662339Slianep } else if (type == PROPAGATE_START || type == PROPAGATE_SAT) {
49671958Slianep graph_walk_dependents(v, propagate_start, NULL);
49681958Slianep
49692339Slianep if (type == PROPAGATE_SAT)
49701958Slianep propagate_satbility(v);
49711958Slianep } else {
49721958Slianep #ifndef NDEBUG
49732339Slianep uu_warn("%s:%d: Unexpected type value %d.\n", __FILE__,
49742339Slianep __LINE__, type);
49751958Slianep #endif
49761958Slianep abort();
49771958Slianep }
49780Sstevel@tonic-gate }
49790Sstevel@tonic-gate
49800Sstevel@tonic-gate /*
49810Sstevel@tonic-gate * If a vertex for fmri exists and it is enabled, send _DISABLE to the
49820Sstevel@tonic-gate * restarter. If it is running, send _STOP. Send _REMOVE_INSTANCE. Delete
49830Sstevel@tonic-gate * all property group dependencies, and the dependency on the restarter,
49840Sstevel@tonic-gate * disposing of vertices as appropriate. If other vertices depend on this
49850Sstevel@tonic-gate * one, mark it unconfigured and return. Otherwise remove the vertex. Always
49860Sstevel@tonic-gate * returns 0.
49870Sstevel@tonic-gate */
49880Sstevel@tonic-gate static int
dgraph_remove_instance(const char * fmri,scf_handle_t * h)49890Sstevel@tonic-gate dgraph_remove_instance(const char *fmri, scf_handle_t *h)
49900Sstevel@tonic-gate {
49910Sstevel@tonic-gate graph_vertex_t *v;
49920Sstevel@tonic-gate graph_edge_t *e;
49930Sstevel@tonic-gate uu_list_t *old_deps;
49940Sstevel@tonic-gate int err;
49950Sstevel@tonic-gate
49960Sstevel@tonic-gate log_framework(LOG_DEBUG, "Graph engine: Removing %s.\n", fmri);
49970Sstevel@tonic-gate
49980Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
49990Sstevel@tonic-gate
50000Sstevel@tonic-gate v = vertex_get_by_name(fmri);
50010Sstevel@tonic-gate if (v == NULL) {
50020Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
50030Sstevel@tonic-gate return (0);
50040Sstevel@tonic-gate }
50050Sstevel@tonic-gate
50060Sstevel@tonic-gate /* Send restarter delete event. */
50070Sstevel@tonic-gate if (v->gv_flags & GV_CONFIGURED)
50080Sstevel@tonic-gate graph_unset_restarter(v);
50090Sstevel@tonic-gate
50100Sstevel@tonic-gate if (milestone > MILESTONE_NONE) {
50110Sstevel@tonic-gate /*
50120Sstevel@tonic-gate * Make a list of v's current dependencies so we can
50130Sstevel@tonic-gate * reevaluate their GV_INSUBGRAPH flags after the dependencies
50140Sstevel@tonic-gate * are removed.
50150Sstevel@tonic-gate */
50160Sstevel@tonic-gate old_deps = startd_list_create(graph_edge_pool, NULL, 0);
50170Sstevel@tonic-gate
50180Sstevel@tonic-gate err = uu_list_walk(v->gv_dependencies,
50191712Srm88369 (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0);
50200Sstevel@tonic-gate assert(err == 0);
50210Sstevel@tonic-gate }
50220Sstevel@tonic-gate
50230Sstevel@tonic-gate delete_instance_dependencies(v, B_TRUE);
50240Sstevel@tonic-gate
50250Sstevel@tonic-gate /*
50260Sstevel@tonic-gate * Deleting an instance can both satisfy and unsatisfy dependencies,
50270Sstevel@tonic-gate * depending on their type. First propagate the stop as a RERR_RESTART
50280Sstevel@tonic-gate * event -- deletion isn't a fault, just a normal stop. This gives
50290Sstevel@tonic-gate * dependent services the chance to do a clean shutdown. Then, mark
50300Sstevel@tonic-gate * the service as unconfigured and propagate the start event for the
50310Sstevel@tonic-gate * optional_all dependencies that might have become satisfied.
50320Sstevel@tonic-gate */
50330Sstevel@tonic-gate graph_walk_dependents(v, propagate_stop, (void *)RERR_RESTART);
50340Sstevel@tonic-gate
50350Sstevel@tonic-gate v->gv_flags &= ~GV_CONFIGURED;
50367475SPhilippe.Jung@Sun.COM v->gv_flags &= ~GV_DEATHROW;
50370Sstevel@tonic-gate
50380Sstevel@tonic-gate graph_walk_dependents(v, propagate_start, NULL);
50390Sstevel@tonic-gate propagate_satbility(v);
50400Sstevel@tonic-gate
50410Sstevel@tonic-gate /*
50420Sstevel@tonic-gate * If there are no (non-service) dependents, the vertex can be
50430Sstevel@tonic-gate * completely removed.
50440Sstevel@tonic-gate */
50451712Srm88369 if (v != milestone && v->gv_refs == 0 &&
50461712Srm88369 uu_list_numnodes(v->gv_dependents) == 1)
50470Sstevel@tonic-gate remove_inst_vertex(v);
50480Sstevel@tonic-gate
50490Sstevel@tonic-gate if (milestone > MILESTONE_NONE) {
50500Sstevel@tonic-gate void *cookie = NULL;
50510Sstevel@tonic-gate
50520Sstevel@tonic-gate while ((e = uu_list_teardown(old_deps, &cookie)) != NULL) {
50531712Srm88369 v = e->ge_vertex;
50541712Srm88369
50551712Srm88369 if (vertex_unref(v) == VERTEX_INUSE)
50561712Srm88369 while (eval_subgraph(v, h) == ECONNABORTED)
50571712Srm88369 libscf_handle_rebind(h);
50580Sstevel@tonic-gate
50590Sstevel@tonic-gate startd_free(e, sizeof (*e));
50600Sstevel@tonic-gate }
50610Sstevel@tonic-gate
50620Sstevel@tonic-gate uu_list_destroy(old_deps);
50630Sstevel@tonic-gate }
50640Sstevel@tonic-gate
50650Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
50660Sstevel@tonic-gate
50670Sstevel@tonic-gate return (0);
50680Sstevel@tonic-gate }
50690Sstevel@tonic-gate
50700Sstevel@tonic-gate /*
50710Sstevel@tonic-gate * Return the eventual (maybe current) milestone in the form of a
50720Sstevel@tonic-gate * legacy runlevel.
50730Sstevel@tonic-gate */
50740Sstevel@tonic-gate static char
target_milestone_as_runlevel()50750Sstevel@tonic-gate target_milestone_as_runlevel()
50760Sstevel@tonic-gate {
507711466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
50780Sstevel@tonic-gate
50790Sstevel@tonic-gate if (milestone == NULL)
50800Sstevel@tonic-gate return ('3');
50810Sstevel@tonic-gate else if (milestone == MILESTONE_NONE)
50820Sstevel@tonic-gate return ('0');
50830Sstevel@tonic-gate
50840Sstevel@tonic-gate if (strcmp(milestone->gv_name, multi_user_fmri) == 0)
50850Sstevel@tonic-gate return ('2');
50860Sstevel@tonic-gate else if (strcmp(milestone->gv_name, single_user_fmri) == 0)
50870Sstevel@tonic-gate return ('S');
50880Sstevel@tonic-gate else if (strcmp(milestone->gv_name, multi_user_svr_fmri) == 0)
50890Sstevel@tonic-gate return ('3');
50900Sstevel@tonic-gate
50910Sstevel@tonic-gate #ifndef NDEBUG
50920Sstevel@tonic-gate (void) fprintf(stderr, "%s:%d: Unknown milestone name \"%s\".\n",
50930Sstevel@tonic-gate __FILE__, __LINE__, milestone->gv_name);
50940Sstevel@tonic-gate #endif
50950Sstevel@tonic-gate abort();
50960Sstevel@tonic-gate /* NOTREACHED */
50970Sstevel@tonic-gate }
50980Sstevel@tonic-gate
50990Sstevel@tonic-gate static struct {
51000Sstevel@tonic-gate char rl;
51010Sstevel@tonic-gate int sig;
51020Sstevel@tonic-gate } init_sigs[] = {
51030Sstevel@tonic-gate { 'S', SIGBUS },
51040Sstevel@tonic-gate { '0', SIGINT },
51050Sstevel@tonic-gate { '1', SIGQUIT },
51060Sstevel@tonic-gate { '2', SIGILL },
51070Sstevel@tonic-gate { '3', SIGTRAP },
51080Sstevel@tonic-gate { '4', SIGIOT },
51090Sstevel@tonic-gate { '5', SIGEMT },
51100Sstevel@tonic-gate { '6', SIGFPE },
51110Sstevel@tonic-gate { 0, 0 }
51120Sstevel@tonic-gate };
51130Sstevel@tonic-gate
51140Sstevel@tonic-gate static void
signal_init(char rl)51150Sstevel@tonic-gate signal_init(char rl)
51160Sstevel@tonic-gate {
51170Sstevel@tonic-gate pid_t init_pid;
51180Sstevel@tonic-gate int i;
51190Sstevel@tonic-gate
512011466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
51210Sstevel@tonic-gate
51220Sstevel@tonic-gate if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid,
51230Sstevel@tonic-gate sizeof (init_pid)) != sizeof (init_pid)) {
51240Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not get pid to signal init.\n");
51250Sstevel@tonic-gate return;
51260Sstevel@tonic-gate }
51270Sstevel@tonic-gate
51280Sstevel@tonic-gate for (i = 0; init_sigs[i].rl != 0; ++i)
51290Sstevel@tonic-gate if (init_sigs[i].rl == rl)
51300Sstevel@tonic-gate break;
51310Sstevel@tonic-gate
51320Sstevel@tonic-gate if (init_sigs[i].rl != 0) {
51330Sstevel@tonic-gate if (kill(init_pid, init_sigs[i].sig) != 0) {
51340Sstevel@tonic-gate switch (errno) {
51350Sstevel@tonic-gate case EPERM:
51360Sstevel@tonic-gate case ESRCH:
51370Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not signal init: "
51380Sstevel@tonic-gate "%s.\n", strerror(errno));
51390Sstevel@tonic-gate break;
51400Sstevel@tonic-gate
51410Sstevel@tonic-gate case EINVAL:
51420Sstevel@tonic-gate default:
51430Sstevel@tonic-gate bad_error("kill", errno);
51440Sstevel@tonic-gate }
51450Sstevel@tonic-gate }
51460Sstevel@tonic-gate }
51470Sstevel@tonic-gate }
51480Sstevel@tonic-gate
51490Sstevel@tonic-gate /*
51500Sstevel@tonic-gate * This is called when one of the major milestones changes state, or when
51510Sstevel@tonic-gate * init is signalled and tells us it was told to change runlevel. We wait
51520Sstevel@tonic-gate * to reach the milestone because this allows /etc/inittab entries to retain
51530Sstevel@tonic-gate * some boot ordering: historically, entries could place themselves before/after
51540Sstevel@tonic-gate * the running of /sbin/rcX scripts but we can no longer make the
51550Sstevel@tonic-gate * distinction because the /sbin/rcX scripts no longer exist as punctuation
51560Sstevel@tonic-gate * marks in /etc/inittab.
51570Sstevel@tonic-gate *
51580Sstevel@tonic-gate * Also, we only trigger an update when we reach the eventual target
51590Sstevel@tonic-gate * milestone: without this, an /etc/inittab entry marked only for
51600Sstevel@tonic-gate * runlevel 2 would be executed for runlevel 3, which is not how
51610Sstevel@tonic-gate * /etc/inittab entries work.
51620Sstevel@tonic-gate *
51630Sstevel@tonic-gate * If we're single user coming online, then we set utmpx to the target
51640Sstevel@tonic-gate * runlevel so that legacy scripts can work as expected.
51650Sstevel@tonic-gate */
51660Sstevel@tonic-gate static void
graph_runlevel_changed(char rl,int online)51670Sstevel@tonic-gate graph_runlevel_changed(char rl, int online)
51680Sstevel@tonic-gate {
51690Sstevel@tonic-gate char trl;
51700Sstevel@tonic-gate
517111466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dgraph_lock));
51720Sstevel@tonic-gate
51730Sstevel@tonic-gate trl = target_milestone_as_runlevel();
51740Sstevel@tonic-gate
51750Sstevel@tonic-gate if (online) {
51760Sstevel@tonic-gate if (rl == trl) {
51771514Srm88369 current_runlevel = trl;
51780Sstevel@tonic-gate signal_init(trl);
51790Sstevel@tonic-gate } else if (rl == 'S') {
51800Sstevel@tonic-gate /*
51810Sstevel@tonic-gate * At boot, set the entry early for the benefit of the
51820Sstevel@tonic-gate * legacy init scripts.
51830Sstevel@tonic-gate */
51840Sstevel@tonic-gate utmpx_set_runlevel(trl, 'S', B_FALSE);
51850Sstevel@tonic-gate }
51860Sstevel@tonic-gate } else {
51870Sstevel@tonic-gate if (rl == '3' && trl == '2') {
51881514Srm88369 current_runlevel = trl;
51890Sstevel@tonic-gate signal_init(trl);
51900Sstevel@tonic-gate } else if (rl == '2' && trl == 'S') {
51911514Srm88369 current_runlevel = trl;
51920Sstevel@tonic-gate signal_init(trl);
51930Sstevel@tonic-gate }
51940Sstevel@tonic-gate }
51950Sstevel@tonic-gate }
51960Sstevel@tonic-gate
51970Sstevel@tonic-gate /*
51980Sstevel@tonic-gate * Move to a backwards-compatible runlevel by executing the appropriate
51990Sstevel@tonic-gate * /etc/rc?.d/K* scripts and/or setting the milestone.
52000Sstevel@tonic-gate *
52010Sstevel@tonic-gate * Returns
52020Sstevel@tonic-gate * 0 - success
52030Sstevel@tonic-gate * ECONNRESET - success, but handle was reset
52040Sstevel@tonic-gate * ECONNABORTED - repository connection broken
52050Sstevel@tonic-gate * ECANCELED - pg was deleted
52060Sstevel@tonic-gate */
52070Sstevel@tonic-gate static int
dgraph_set_runlevel(scf_propertygroup_t * pg,scf_property_t * prop)52080Sstevel@tonic-gate dgraph_set_runlevel(scf_propertygroup_t *pg, scf_property_t *prop)
52090Sstevel@tonic-gate {
52100Sstevel@tonic-gate char rl;
52110Sstevel@tonic-gate scf_handle_t *h;
52120Sstevel@tonic-gate int r;
52130Sstevel@tonic-gate const char *ms = NULL; /* what to commit as options/milestone */
52140Sstevel@tonic-gate boolean_t rebound = B_FALSE;
52150Sstevel@tonic-gate int mark_rl = 0;
52160Sstevel@tonic-gate
52170Sstevel@tonic-gate const char * const stop = "stop";
52180Sstevel@tonic-gate
52190Sstevel@tonic-gate r = libscf_extract_runlevel(prop, &rl);
52200Sstevel@tonic-gate switch (r) {
52210Sstevel@tonic-gate case 0:
52220Sstevel@tonic-gate break;
52230Sstevel@tonic-gate
52240Sstevel@tonic-gate case ECONNABORTED:
52250Sstevel@tonic-gate case ECANCELED:
52260Sstevel@tonic-gate return (r);
52270Sstevel@tonic-gate
52280Sstevel@tonic-gate case EINVAL:
52290Sstevel@tonic-gate case ENOENT:
52300Sstevel@tonic-gate log_error(LOG_WARNING, "runlevel property is misconfigured; "
52310Sstevel@tonic-gate "ignoring.\n");
52320Sstevel@tonic-gate /* delete the bad property */
52330Sstevel@tonic-gate goto nolock_out;
52340Sstevel@tonic-gate
52350Sstevel@tonic-gate default:
52360Sstevel@tonic-gate bad_error("libscf_extract_runlevel", r);
52370Sstevel@tonic-gate }
52380Sstevel@tonic-gate
52390Sstevel@tonic-gate switch (rl) {
52400Sstevel@tonic-gate case 's':
52410Sstevel@tonic-gate rl = 'S';
52420Sstevel@tonic-gate /* FALLTHROUGH */
52430Sstevel@tonic-gate
52440Sstevel@tonic-gate case 'S':
52450Sstevel@tonic-gate case '2':
52460Sstevel@tonic-gate case '3':
52470Sstevel@tonic-gate /*
52480Sstevel@tonic-gate * These cases cause a milestone change, so
52490Sstevel@tonic-gate * graph_runlevel_changed() will eventually deal with
52500Sstevel@tonic-gate * signalling init.
52510Sstevel@tonic-gate */
52520Sstevel@tonic-gate break;
52530Sstevel@tonic-gate
52540Sstevel@tonic-gate case '0':
52550Sstevel@tonic-gate case '1':
52560Sstevel@tonic-gate case '4':
52570Sstevel@tonic-gate case '5':
52580Sstevel@tonic-gate case '6':
52590Sstevel@tonic-gate mark_rl = 1;
52600Sstevel@tonic-gate break;
52610Sstevel@tonic-gate
52620Sstevel@tonic-gate default:
52630Sstevel@tonic-gate log_framework(LOG_NOTICE, "Unknown runlevel '%c'.\n", rl);
52640Sstevel@tonic-gate ms = NULL;
52650Sstevel@tonic-gate goto nolock_out;
52660Sstevel@tonic-gate }
52670Sstevel@tonic-gate
52680Sstevel@tonic-gate h = scf_pg_handle(pg);
52690Sstevel@tonic-gate
52700Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
52710Sstevel@tonic-gate
52720Sstevel@tonic-gate /*
52730Sstevel@tonic-gate * Since this triggers no milestone changes, force it by hand.
52740Sstevel@tonic-gate */
52750Sstevel@tonic-gate if (current_runlevel == '4' && rl == '3')
52760Sstevel@tonic-gate mark_rl = 1;
52770Sstevel@tonic-gate
52781514Srm88369 /*
52791514Srm88369 * 1. If we are here after an "init X":
52801514Srm88369 *
52811514Srm88369 * init X
52821514Srm88369 * init/lscf_set_runlevel()
52831514Srm88369 * process_pg_event()
52841514Srm88369 * dgraph_set_runlevel()
52851514Srm88369 *
52861514Srm88369 * then we haven't passed through graph_runlevel_changed() yet,
52871514Srm88369 * therefore 'current_runlevel' has not changed for sure but 'rl' has.
52881514Srm88369 * In consequence, if 'rl' is lower than 'current_runlevel', we change
52891514Srm88369 * the system runlevel and execute the appropriate /etc/rc?.d/K* scripts
52901514Srm88369 * past this test.
52911514Srm88369 *
52921514Srm88369 * 2. On the other hand, if we are here after a "svcadm milestone":
52931514Srm88369 *
52941514Srm88369 * svcadm milestone X
52951514Srm88369 * dgraph_set_milestone()
52961514Srm88369 * handle_graph_update_event()
52971514Srm88369 * dgraph_set_instance_state()
52981514Srm88369 * graph_post_X_[online|offline]()
52991514Srm88369 * graph_runlevel_changed()
53001514Srm88369 * signal_init()
53011514Srm88369 * init/lscf_set_runlevel()
53021514Srm88369 * process_pg_event()
53031514Srm88369 * dgraph_set_runlevel()
53041514Srm88369 *
53051514Srm88369 * then we already passed through graph_runlevel_changed() (by the way
53061514Srm88369 * of dgraph_set_milestone()) and 'current_runlevel' may have changed
53071514Srm88369 * and already be equal to 'rl' so we are going to return immediately
53081514Srm88369 * from dgraph_set_runlevel() without changing the system runlevel and
53091514Srm88369 * without executing the /etc/rc?.d/K* scripts.
53101514Srm88369 */
53110Sstevel@tonic-gate if (rl == current_runlevel) {
53120Sstevel@tonic-gate ms = NULL;
53130Sstevel@tonic-gate goto out;
53140Sstevel@tonic-gate }
53150Sstevel@tonic-gate
53160Sstevel@tonic-gate log_framework(LOG_DEBUG, "Changing to runlevel '%c'.\n", rl);
53170Sstevel@tonic-gate
53180Sstevel@tonic-gate /*
53190Sstevel@tonic-gate * Make sure stop rc scripts see the new settings via who -r.
53200Sstevel@tonic-gate */
53210Sstevel@tonic-gate utmpx_set_runlevel(rl, current_runlevel, B_TRUE);
53220Sstevel@tonic-gate
53230Sstevel@tonic-gate /*
53240Sstevel@tonic-gate * Some run levels don't have a direct correspondence to any
53250Sstevel@tonic-gate * milestones, so we have to signal init directly.
53260Sstevel@tonic-gate */
53270Sstevel@tonic-gate if (mark_rl) {
53280Sstevel@tonic-gate current_runlevel = rl;
53290Sstevel@tonic-gate signal_init(rl);
53300Sstevel@tonic-gate }
53310Sstevel@tonic-gate
53320Sstevel@tonic-gate switch (rl) {
53330Sstevel@tonic-gate case 'S':
53340Sstevel@tonic-gate uu_warn("The system is coming down for administration. "
53350Sstevel@tonic-gate "Please wait.\n");
53360Sstevel@tonic-gate fork_rc_script(rl, stop, B_FALSE);
53370Sstevel@tonic-gate ms = single_user_fmri;
53380Sstevel@tonic-gate go_single_user_mode = B_TRUE;
53390Sstevel@tonic-gate break;
53400Sstevel@tonic-gate
53410Sstevel@tonic-gate case '0':
53428944Sdp@eng.sun.com halting_time = time(NULL);
53430Sstevel@tonic-gate fork_rc_script(rl, stop, B_TRUE);
53440Sstevel@tonic-gate halting = AD_HALT;
53450Sstevel@tonic-gate goto uadmin;
53460Sstevel@tonic-gate
53470Sstevel@tonic-gate case '5':
53488944Sdp@eng.sun.com halting_time = time(NULL);
53490Sstevel@tonic-gate fork_rc_script(rl, stop, B_TRUE);
53500Sstevel@tonic-gate halting = AD_POWEROFF;
53510Sstevel@tonic-gate goto uadmin;
53520Sstevel@tonic-gate
53530Sstevel@tonic-gate case '6':
53548944Sdp@eng.sun.com halting_time = time(NULL);
53550Sstevel@tonic-gate fork_rc_script(rl, stop, B_TRUE);
53569160SSherry.Moore@Sun.COM if (scf_is_fastboot_default() && getzoneid() == GLOBAL_ZONEID)
53579160SSherry.Moore@Sun.COM halting = AD_FASTREBOOT;
53589160SSherry.Moore@Sun.COM else
53599160SSherry.Moore@Sun.COM halting = AD_BOOT;
53600Sstevel@tonic-gate
53610Sstevel@tonic-gate uadmin:
53620Sstevel@tonic-gate uu_warn("The system is coming down. Please wait.\n");
53630Sstevel@tonic-gate ms = "none";
53640Sstevel@tonic-gate
53650Sstevel@tonic-gate /*
53660Sstevel@tonic-gate * We can't wait until all services are offline since this
53670Sstevel@tonic-gate * thread is responsible for taking them offline. Instead we
53680Sstevel@tonic-gate * set halting to the second argument for uadmin() and call
53690Sstevel@tonic-gate * do_uadmin() from dgraph_set_instance_state() when
53700Sstevel@tonic-gate * appropriate.
53710Sstevel@tonic-gate */
53720Sstevel@tonic-gate break;
53730Sstevel@tonic-gate
53740Sstevel@tonic-gate case '1':
53750Sstevel@tonic-gate if (current_runlevel != 'S') {
53760Sstevel@tonic-gate uu_warn("Changing to state 1.\n");
53770Sstevel@tonic-gate fork_rc_script(rl, stop, B_FALSE);
53780Sstevel@tonic-gate } else {
53790Sstevel@tonic-gate uu_warn("The system is coming up for administration. "
53800Sstevel@tonic-gate "Please wait.\n");
53810Sstevel@tonic-gate }
53820Sstevel@tonic-gate ms = single_user_fmri;
53830Sstevel@tonic-gate go_to_level1 = B_TRUE;
53840Sstevel@tonic-gate break;
53850Sstevel@tonic-gate
53860Sstevel@tonic-gate case '2':
53870Sstevel@tonic-gate if (current_runlevel == '3' || current_runlevel == '4')
53880Sstevel@tonic-gate fork_rc_script(rl, stop, B_FALSE);
53890Sstevel@tonic-gate ms = multi_user_fmri;
53900Sstevel@tonic-gate break;
53910Sstevel@tonic-gate
53920Sstevel@tonic-gate case '3':
53930Sstevel@tonic-gate case '4':
53940Sstevel@tonic-gate ms = "all";
53950Sstevel@tonic-gate break;
53960Sstevel@tonic-gate
53970Sstevel@tonic-gate default:
53980Sstevel@tonic-gate #ifndef NDEBUG
53990Sstevel@tonic-gate (void) fprintf(stderr, "%s:%d: Uncaught case %d ('%c').\n",
54000Sstevel@tonic-gate __FILE__, __LINE__, rl, rl);
54010Sstevel@tonic-gate #endif
54020Sstevel@tonic-gate abort();
54030Sstevel@tonic-gate }
54040Sstevel@tonic-gate
54050Sstevel@tonic-gate out:
54060Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
54070Sstevel@tonic-gate
54080Sstevel@tonic-gate nolock_out:
54090Sstevel@tonic-gate switch (r = libscf_clear_runlevel(pg, ms)) {
54100Sstevel@tonic-gate case 0:
54110Sstevel@tonic-gate break;
54120Sstevel@tonic-gate
54130Sstevel@tonic-gate case ECONNABORTED:
54140Sstevel@tonic-gate libscf_handle_rebind(h);
54150Sstevel@tonic-gate rebound = B_TRUE;
54160Sstevel@tonic-gate goto nolock_out;
54170Sstevel@tonic-gate
54180Sstevel@tonic-gate case ECANCELED:
54190Sstevel@tonic-gate break;
54200Sstevel@tonic-gate
54210Sstevel@tonic-gate case EPERM:
54220Sstevel@tonic-gate case EACCES:
54230Sstevel@tonic-gate case EROFS:
54240Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not delete \"%s/%s\" property: "
54250Sstevel@tonic-gate "%s.\n", SCF_PG_OPTIONS, "runlevel", strerror(r));
54260Sstevel@tonic-gate break;
54270Sstevel@tonic-gate
54280Sstevel@tonic-gate default:
54290Sstevel@tonic-gate bad_error("libscf_clear_runlevel", r);
54300Sstevel@tonic-gate }
54310Sstevel@tonic-gate
54320Sstevel@tonic-gate return (rebound ? ECONNRESET : 0);
54330Sstevel@tonic-gate }
54340Sstevel@tonic-gate
54357630SRenaud.Manus@Sun.COM /*
54367630SRenaud.Manus@Sun.COM * mark_subtree walks the dependents and add the GV_TOOFFLINE flag
54377630SRenaud.Manus@Sun.COM * to the instances that are supposed to go offline during an
54387630SRenaud.Manus@Sun.COM * administrative disable operation.
54397630SRenaud.Manus@Sun.COM */
54407630SRenaud.Manus@Sun.COM static int
mark_subtree(graph_edge_t * e,void * arg)54417630SRenaud.Manus@Sun.COM mark_subtree(graph_edge_t *e, void *arg)
54427630SRenaud.Manus@Sun.COM {
54437630SRenaud.Manus@Sun.COM graph_vertex_t *v;
54447630SRenaud.Manus@Sun.COM int r;
54457630SRenaud.Manus@Sun.COM
54467630SRenaud.Manus@Sun.COM v = e->ge_vertex;
54477630SRenaud.Manus@Sun.COM
54487630SRenaud.Manus@Sun.COM /* If it's already in the subgraph, skip. */
54497630SRenaud.Manus@Sun.COM if (v->gv_flags & GV_TOOFFLINE)
54507630SRenaud.Manus@Sun.COM return (UU_WALK_NEXT);
54517630SRenaud.Manus@Sun.COM
54527630SRenaud.Manus@Sun.COM switch (v->gv_type) {
54537630SRenaud.Manus@Sun.COM case GVT_INST:
54547630SRenaud.Manus@Sun.COM /* If the instance is already disabled, skip it. */
54557630SRenaud.Manus@Sun.COM if (!(v->gv_flags & GV_ENABLED))
54567630SRenaud.Manus@Sun.COM return (UU_WALK_NEXT);
54577630SRenaud.Manus@Sun.COM
54587630SRenaud.Manus@Sun.COM v->gv_flags |= GV_TOOFFLINE;
54597630SRenaud.Manus@Sun.COM log_framework(LOG_DEBUG, "%s added to subtree\n", v->gv_name);
54607630SRenaud.Manus@Sun.COM break;
54617630SRenaud.Manus@Sun.COM case GVT_GROUP:
54627630SRenaud.Manus@Sun.COM /*
54638354SRenaud.Manus@Sun.COM * Skip all excluded and optional_all dependencies and decide
54648354SRenaud.Manus@Sun.COM * whether to offline the service based on restart_on attribute.
54657630SRenaud.Manus@Sun.COM */
546612225SGowtham.Thommandra@Sun.COM if (is_depgrp_bypassed(v))
54677630SRenaud.Manus@Sun.COM return (UU_WALK_NEXT);
54687630SRenaud.Manus@Sun.COM break;
54697630SRenaud.Manus@Sun.COM }
54707630SRenaud.Manus@Sun.COM
54717630SRenaud.Manus@Sun.COM r = uu_list_walk(v->gv_dependents, (uu_walk_fn_t *)mark_subtree, arg,
54727630SRenaud.Manus@Sun.COM 0);
54737630SRenaud.Manus@Sun.COM assert(r == 0);
54747630SRenaud.Manus@Sun.COM return (UU_WALK_NEXT);
54757630SRenaud.Manus@Sun.COM }
54767630SRenaud.Manus@Sun.COM
54770Sstevel@tonic-gate static int
mark_subgraph(graph_edge_t * e,void * arg)54780Sstevel@tonic-gate mark_subgraph(graph_edge_t *e, void *arg)
54790Sstevel@tonic-gate {
54800Sstevel@tonic-gate graph_vertex_t *v;
54810Sstevel@tonic-gate int r;
54820Sstevel@tonic-gate int optional = (int)arg;
54830Sstevel@tonic-gate
54840Sstevel@tonic-gate v = e->ge_vertex;
54850Sstevel@tonic-gate
54860Sstevel@tonic-gate /* If it's already in the subgraph, skip. */
54870Sstevel@tonic-gate if (v->gv_flags & GV_INSUBGRAPH)
54880Sstevel@tonic-gate return (UU_WALK_NEXT);
54890Sstevel@tonic-gate
54900Sstevel@tonic-gate /*
54910Sstevel@tonic-gate * Keep track if walk has entered an optional dependency group
54920Sstevel@tonic-gate */
54930Sstevel@tonic-gate if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_OPTIONAL_ALL) {
54940Sstevel@tonic-gate optional = 1;
54950Sstevel@tonic-gate }
54960Sstevel@tonic-gate /*
54970Sstevel@tonic-gate * Quit if we are in an optional dependency group and the instance
54980Sstevel@tonic-gate * is disabled
54990Sstevel@tonic-gate */
55000Sstevel@tonic-gate if (optional && (v->gv_type == GVT_INST) &&
55010Sstevel@tonic-gate (!(v->gv_flags & GV_ENBLD_NOOVR)))
55020Sstevel@tonic-gate return (UU_WALK_NEXT);
55030Sstevel@tonic-gate
55040Sstevel@tonic-gate v->gv_flags |= GV_INSUBGRAPH;
55050Sstevel@tonic-gate
55060Sstevel@tonic-gate /* Skip all excluded dependencies. */
55070Sstevel@tonic-gate if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL)
55080Sstevel@tonic-gate return (UU_WALK_NEXT);
55090Sstevel@tonic-gate
55100Sstevel@tonic-gate r = uu_list_walk(v->gv_dependencies, (uu_walk_fn_t *)mark_subgraph,
55110Sstevel@tonic-gate (void *)optional, 0);
55120Sstevel@tonic-gate assert(r == 0);
55130Sstevel@tonic-gate return (UU_WALK_NEXT);
55140Sstevel@tonic-gate }
55150Sstevel@tonic-gate
55160Sstevel@tonic-gate /*
55172747Sbustos * Bring down all services which are not dependencies of fmri. The
55182747Sbustos * dependencies of fmri (direct & indirect) will constitute the "subgraph",
55192747Sbustos * and will have the GV_INSUBGRAPH flag set. The rest must be brought down,
55202747Sbustos * which means the state is "disabled", "maintenance", or "uninitialized". We
55212747Sbustos * could consider "offline" to be down, and refrain from sending start
55222747Sbustos * commands for such services, but that's not strictly necessary, so we'll
55232747Sbustos * decline to intrude on the state machine. It would probably confuse users
55242747Sbustos * anyway.
55252747Sbustos *
55262747Sbustos * The services should be brought down in reverse-dependency order, so we
55272747Sbustos * can't do it all at once here. We initiate by override-disabling the leaves
55282747Sbustos * of the dependency tree -- those services which are up but have no
55292747Sbustos * dependents which are up. When they come down,
55302747Sbustos * vertex_subgraph_dependencies_shutdown() will override-disable the newly
55312747Sbustos * exposed leaves. Perseverance will ensure completion.
55322747Sbustos *
55332747Sbustos * Sometimes we need to take action when the transition is complete, like
55342747Sbustos * start sulogin or halt the system. To tell when we're done, we initialize
55352747Sbustos * non_subgraph_svcs here to be the number of services which need to come
55362747Sbustos * down. As each does, we decrement the counter. When it hits zero, we take
55372747Sbustos * the appropriate action. See vertex_subgraph_dependencies_shutdown().
55382747Sbustos *
55392747Sbustos * In case we're coming up, we also remove any enable-overrides for the
55402747Sbustos * services which are dependencies of fmri.
55410Sstevel@tonic-gate *
55420Sstevel@tonic-gate * If norepository is true, the function will not change the repository.
55430Sstevel@tonic-gate *
55441514Srm88369 * The decision to change the system run level in accordance with the milestone
55451514Srm88369 * is taken in dgraph_set_runlevel().
55461514Srm88369 *
55470Sstevel@tonic-gate * Returns
55480Sstevel@tonic-gate * 0 - success
55490Sstevel@tonic-gate * ECONNRESET - success, but handle was rebound
55500Sstevel@tonic-gate * EINVAL - fmri is invalid (error is logged)
55510Sstevel@tonic-gate * EALREADY - the milestone is already set to fmri
55520Sstevel@tonic-gate * ENOENT - a configured vertex does not exist for fmri (an error is logged)
55530Sstevel@tonic-gate */
55540Sstevel@tonic-gate static int
dgraph_set_milestone(const char * fmri,scf_handle_t * h,boolean_t norepository)55550Sstevel@tonic-gate dgraph_set_milestone(const char *fmri, scf_handle_t *h, boolean_t norepository)
55560Sstevel@tonic-gate {
55570Sstevel@tonic-gate const char *cfmri, *fs;
55580Sstevel@tonic-gate graph_vertex_t *nm, *v;
55590Sstevel@tonic-gate int ret = 0, r;
55600Sstevel@tonic-gate scf_instance_t *inst;
55610Sstevel@tonic-gate boolean_t isall, isnone, rebound = B_FALSE;
55620Sstevel@tonic-gate
55630Sstevel@tonic-gate /* Validate fmri */
55640Sstevel@tonic-gate isall = (strcmp(fmri, "all") == 0);
55650Sstevel@tonic-gate isnone = (strcmp(fmri, "none") == 0);
55660Sstevel@tonic-gate
55670Sstevel@tonic-gate if (!isall && !isnone) {
55680Sstevel@tonic-gate if (fmri_canonify(fmri, (char **)&cfmri, B_FALSE) == EINVAL)
55690Sstevel@tonic-gate goto reject;
55700Sstevel@tonic-gate
55710Sstevel@tonic-gate if (strcmp(cfmri, single_user_fmri) != 0 &&
55720Sstevel@tonic-gate strcmp(cfmri, multi_user_fmri) != 0 &&
55730Sstevel@tonic-gate strcmp(cfmri, multi_user_svr_fmri) != 0) {
55740Sstevel@tonic-gate startd_free((void *)cfmri, max_scf_fmri_size);
55750Sstevel@tonic-gate reject:
55760Sstevel@tonic-gate log_framework(LOG_WARNING,
55770Sstevel@tonic-gate "Rejecting request for invalid milestone \"%s\".\n",
55780Sstevel@tonic-gate fmri);
55790Sstevel@tonic-gate return (EINVAL);
55800Sstevel@tonic-gate }
55810Sstevel@tonic-gate }
55820Sstevel@tonic-gate
55830Sstevel@tonic-gate inst = safe_scf_instance_create(h);
55840Sstevel@tonic-gate
55850Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
55860Sstevel@tonic-gate
55870Sstevel@tonic-gate if (milestone == NULL) {
55880Sstevel@tonic-gate if (isall) {
55890Sstevel@tonic-gate log_framework(LOG_DEBUG,
55900Sstevel@tonic-gate "Milestone already set to all.\n");
55910Sstevel@tonic-gate ret = EALREADY;
55920Sstevel@tonic-gate goto out;
55930Sstevel@tonic-gate }
55940Sstevel@tonic-gate } else if (milestone == MILESTONE_NONE) {
55950Sstevel@tonic-gate if (isnone) {
55960Sstevel@tonic-gate log_framework(LOG_DEBUG,
55970Sstevel@tonic-gate "Milestone already set to none.\n");
55980Sstevel@tonic-gate ret = EALREADY;
55990Sstevel@tonic-gate goto out;
56000Sstevel@tonic-gate }
56010Sstevel@tonic-gate } else {
56020Sstevel@tonic-gate if (!isall && !isnone &&
56030Sstevel@tonic-gate strcmp(cfmri, milestone->gv_name) == 0) {
56040Sstevel@tonic-gate log_framework(LOG_DEBUG,
56050Sstevel@tonic-gate "Milestone already set to %s.\n", cfmri);
56060Sstevel@tonic-gate ret = EALREADY;
56070Sstevel@tonic-gate goto out;
56080Sstevel@tonic-gate }
56090Sstevel@tonic-gate }
56100Sstevel@tonic-gate
56110Sstevel@tonic-gate if (!isall && !isnone) {
56120Sstevel@tonic-gate nm = vertex_get_by_name(cfmri);
56130Sstevel@tonic-gate if (nm == NULL || !(nm->gv_flags & GV_CONFIGURED)) {
56140Sstevel@tonic-gate log_framework(LOG_WARNING, "Cannot set milestone to %s "
56150Sstevel@tonic-gate "because no such service exists.\n", cfmri);
56160Sstevel@tonic-gate ret = ENOENT;
56170Sstevel@tonic-gate goto out;
56180Sstevel@tonic-gate }
56190Sstevel@tonic-gate }
56200Sstevel@tonic-gate
56210Sstevel@tonic-gate log_framework(LOG_DEBUG, "Changing milestone to %s.\n", fmri);
56220Sstevel@tonic-gate
56230Sstevel@tonic-gate /*
56240Sstevel@tonic-gate * Set milestone, removing the old one if this was the last reference.
56250Sstevel@tonic-gate */
56261712Srm88369 if (milestone > MILESTONE_NONE)
56271712Srm88369 (void) vertex_unref(milestone);
56280Sstevel@tonic-gate
56290Sstevel@tonic-gate if (isall)
56300Sstevel@tonic-gate milestone = NULL;
56310Sstevel@tonic-gate else if (isnone)
56320Sstevel@tonic-gate milestone = MILESTONE_NONE;
56331712Srm88369 else {
56340Sstevel@tonic-gate milestone = nm;
56351712Srm88369 /* milestone should count as a reference */
56361712Srm88369 vertex_ref(milestone);
56371712Srm88369 }
56380Sstevel@tonic-gate
56390Sstevel@tonic-gate /* Clear all GV_INSUBGRAPH bits. */
56400Sstevel@tonic-gate for (v = uu_list_first(dgraph); v != NULL; v = uu_list_next(dgraph, v))
56410Sstevel@tonic-gate v->gv_flags &= ~GV_INSUBGRAPH;
56420Sstevel@tonic-gate
56430Sstevel@tonic-gate if (!isall && !isnone) {
56440Sstevel@tonic-gate /* Set GV_INSUBGRAPH for milestone & descendents. */
56450Sstevel@tonic-gate milestone->gv_flags |= GV_INSUBGRAPH;
56460Sstevel@tonic-gate
56470Sstevel@tonic-gate r = uu_list_walk(milestone->gv_dependencies,
56480Sstevel@tonic-gate (uu_walk_fn_t *)mark_subgraph, NULL, 0);
56490Sstevel@tonic-gate assert(r == 0);
56500Sstevel@tonic-gate }
56510Sstevel@tonic-gate
56520Sstevel@tonic-gate /* Un-override services in the subgraph & override-disable the rest. */
56530Sstevel@tonic-gate if (norepository)
56540Sstevel@tonic-gate goto out;
56550Sstevel@tonic-gate
56560Sstevel@tonic-gate non_subgraph_svcs = 0;
56570Sstevel@tonic-gate for (v = uu_list_first(dgraph);
56580Sstevel@tonic-gate v != NULL;
56590Sstevel@tonic-gate v = uu_list_next(dgraph, v)) {
56600Sstevel@tonic-gate if (v->gv_type != GVT_INST ||
56610Sstevel@tonic-gate (v->gv_flags & GV_CONFIGURED) == 0)
56620Sstevel@tonic-gate continue;
56630Sstevel@tonic-gate
56640Sstevel@tonic-gate again:
56650Sstevel@tonic-gate r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst,
56660Sstevel@tonic-gate NULL, NULL, SCF_DECODE_FMRI_EXACT);
56670Sstevel@tonic-gate if (r != 0) {
56680Sstevel@tonic-gate switch (scf_error()) {
56690Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
56700Sstevel@tonic-gate default:
56710Sstevel@tonic-gate libscf_handle_rebind(h);
56720Sstevel@tonic-gate rebound = B_TRUE;
56730Sstevel@tonic-gate goto again;
56740Sstevel@tonic-gate
56750Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
56760Sstevel@tonic-gate continue;
56770Sstevel@tonic-gate
56780Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
56790Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
56800Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
56810Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
56820Sstevel@tonic-gate bad_error("scf_handle_decode_fmri",
56830Sstevel@tonic-gate scf_error());
56840Sstevel@tonic-gate }
56850Sstevel@tonic-gate }
56860Sstevel@tonic-gate
56870Sstevel@tonic-gate if (isall || (v->gv_flags & GV_INSUBGRAPH)) {
56880Sstevel@tonic-gate r = libscf_delete_enable_ovr(inst);
56890Sstevel@tonic-gate fs = "libscf_delete_enable_ovr";
56900Sstevel@tonic-gate } else {
56910Sstevel@tonic-gate assert(isnone || (v->gv_flags & GV_INSUBGRAPH) == 0);
56920Sstevel@tonic-gate
56932747Sbustos /*
56942747Sbustos * Services which are up need to come down before
56952747Sbustos * we're done, but we can only disable the leaves
56962747Sbustos * here.
56972747Sbustos */
56982747Sbustos
56992747Sbustos if (up_state(v->gv_state))
57000Sstevel@tonic-gate ++non_subgraph_svcs;
57010Sstevel@tonic-gate
57022747Sbustos /* If it's already disabled, don't bother. */
57032747Sbustos if ((v->gv_flags & GV_ENABLED) == 0)
57042747Sbustos continue;
57052747Sbustos
57062747Sbustos if (!is_nonsubgraph_leaf(v))
57070Sstevel@tonic-gate continue;
57080Sstevel@tonic-gate
57090Sstevel@tonic-gate r = libscf_set_enable_ovr(inst, 0);
57100Sstevel@tonic-gate fs = "libscf_set_enable_ovr";
57110Sstevel@tonic-gate }
57120Sstevel@tonic-gate switch (r) {
57130Sstevel@tonic-gate case 0:
57140Sstevel@tonic-gate case ECANCELED:
57150Sstevel@tonic-gate break;
57160Sstevel@tonic-gate
57170Sstevel@tonic-gate case ECONNABORTED:
57180Sstevel@tonic-gate libscf_handle_rebind(h);
57190Sstevel@tonic-gate rebound = B_TRUE;
57200Sstevel@tonic-gate goto again;
57210Sstevel@tonic-gate
57220Sstevel@tonic-gate case EPERM:
57230Sstevel@tonic-gate case EROFS:
57240Sstevel@tonic-gate log_error(LOG_WARNING,
57250Sstevel@tonic-gate "Could not set %s/%s for %s: %s.\n",
57260Sstevel@tonic-gate SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED,
57270Sstevel@tonic-gate v->gv_name, strerror(r));
57280Sstevel@tonic-gate break;
57290Sstevel@tonic-gate
57300Sstevel@tonic-gate default:
57310Sstevel@tonic-gate bad_error(fs, r);
57320Sstevel@tonic-gate }
57330Sstevel@tonic-gate }
57340Sstevel@tonic-gate
57350Sstevel@tonic-gate if (halting != -1) {
57360Sstevel@tonic-gate if (non_subgraph_svcs > 1)
57370Sstevel@tonic-gate uu_warn("%d system services are now being stopped.\n",
57380Sstevel@tonic-gate non_subgraph_svcs);
57390Sstevel@tonic-gate else if (non_subgraph_svcs == 1)
57400Sstevel@tonic-gate uu_warn("One system service is now being stopped.\n");
57410Sstevel@tonic-gate else if (non_subgraph_svcs == 0)
57420Sstevel@tonic-gate do_uadmin();
57430Sstevel@tonic-gate }
57440Sstevel@tonic-gate
57450Sstevel@tonic-gate ret = rebound ? ECONNRESET : 0;
57460Sstevel@tonic-gate
57470Sstevel@tonic-gate out:
57480Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
57490Sstevel@tonic-gate if (!isall && !isnone)
57500Sstevel@tonic-gate startd_free((void *)cfmri, max_scf_fmri_size);
57510Sstevel@tonic-gate scf_instance_destroy(inst);
57520Sstevel@tonic-gate return (ret);
57530Sstevel@tonic-gate }
57540Sstevel@tonic-gate
57550Sstevel@tonic-gate
57560Sstevel@tonic-gate /*
57570Sstevel@tonic-gate * Returns 0, ECONNABORTED, or EINVAL.
57580Sstevel@tonic-gate */
57590Sstevel@tonic-gate static int
handle_graph_update_event(scf_handle_t * h,graph_protocol_event_t * e)57600Sstevel@tonic-gate handle_graph_update_event(scf_handle_t *h, graph_protocol_event_t *e)
57610Sstevel@tonic-gate {
57620Sstevel@tonic-gate int r;
57630Sstevel@tonic-gate
57640Sstevel@tonic-gate switch (e->gpe_type) {
57650Sstevel@tonic-gate case GRAPH_UPDATE_RELOAD_GRAPH:
57660Sstevel@tonic-gate log_error(LOG_WARNING,
57670Sstevel@tonic-gate "graph_event: reload graph unimplemented\n");
57680Sstevel@tonic-gate break;
57690Sstevel@tonic-gate
57700Sstevel@tonic-gate case GRAPH_UPDATE_STATE_CHANGE: {
57710Sstevel@tonic-gate protocol_states_t *states = e->gpe_data;
57720Sstevel@tonic-gate
5773*12967Sgavin.maltby@oracle.com switch (r = dgraph_set_instance_state(h, e->gpe_inst, states)) {
57740Sstevel@tonic-gate case 0:
57750Sstevel@tonic-gate case ENOENT:
57760Sstevel@tonic-gate break;
57770Sstevel@tonic-gate
57780Sstevel@tonic-gate case ECONNABORTED:
57790Sstevel@tonic-gate return (ECONNABORTED);
57800Sstevel@tonic-gate
57810Sstevel@tonic-gate case EINVAL:
57820Sstevel@tonic-gate default:
57830Sstevel@tonic-gate #ifndef NDEBUG
57840Sstevel@tonic-gate (void) fprintf(stderr, "dgraph_set_instance_state() "
57850Sstevel@tonic-gate "failed with unexpected error %d at %s:%d.\n", r,
57860Sstevel@tonic-gate __FILE__, __LINE__);
57870Sstevel@tonic-gate #endif
57880Sstevel@tonic-gate abort();
57890Sstevel@tonic-gate }
57900Sstevel@tonic-gate
57910Sstevel@tonic-gate startd_free(states, sizeof (protocol_states_t));
57920Sstevel@tonic-gate break;
57930Sstevel@tonic-gate }
57940Sstevel@tonic-gate
57950Sstevel@tonic-gate default:
57960Sstevel@tonic-gate log_error(LOG_WARNING,
57970Sstevel@tonic-gate "graph_event_loop received an unknown event: %d\n",
57980Sstevel@tonic-gate e->gpe_type);
57990Sstevel@tonic-gate break;
58000Sstevel@tonic-gate }
58010Sstevel@tonic-gate
58020Sstevel@tonic-gate return (0);
58030Sstevel@tonic-gate }
58040Sstevel@tonic-gate
58050Sstevel@tonic-gate /*
58060Sstevel@tonic-gate * graph_event_thread()
58070Sstevel@tonic-gate * Wait for state changes from the restarters.
58080Sstevel@tonic-gate */
58090Sstevel@tonic-gate /*ARGSUSED*/
58100Sstevel@tonic-gate void *
graph_event_thread(void * unused)58110Sstevel@tonic-gate graph_event_thread(void *unused)
58120Sstevel@tonic-gate {
58130Sstevel@tonic-gate scf_handle_t *h;
58140Sstevel@tonic-gate int err;
58150Sstevel@tonic-gate
58160Sstevel@tonic-gate h = libscf_handle_create_bound_loop();
58170Sstevel@tonic-gate
58180Sstevel@tonic-gate /*CONSTCOND*/
58190Sstevel@tonic-gate while (1) {
58200Sstevel@tonic-gate graph_protocol_event_t *e;
58210Sstevel@tonic-gate
58220Sstevel@tonic-gate MUTEX_LOCK(&gu->gu_lock);
58230Sstevel@tonic-gate
58240Sstevel@tonic-gate while (gu->gu_wakeup == 0)
58250Sstevel@tonic-gate (void) pthread_cond_wait(&gu->gu_cv, &gu->gu_lock);
58260Sstevel@tonic-gate
58270Sstevel@tonic-gate gu->gu_wakeup = 0;
58280Sstevel@tonic-gate
58290Sstevel@tonic-gate while ((e = graph_event_dequeue()) != NULL) {
58300Sstevel@tonic-gate MUTEX_LOCK(&e->gpe_lock);
58310Sstevel@tonic-gate MUTEX_UNLOCK(&gu->gu_lock);
58320Sstevel@tonic-gate
58330Sstevel@tonic-gate while ((err = handle_graph_update_event(h, e)) ==
58340Sstevel@tonic-gate ECONNABORTED)
58350Sstevel@tonic-gate libscf_handle_rebind(h);
58360Sstevel@tonic-gate
58370Sstevel@tonic-gate if (err == 0)
58380Sstevel@tonic-gate graph_event_release(e);
58390Sstevel@tonic-gate else
58400Sstevel@tonic-gate graph_event_requeue(e);
58410Sstevel@tonic-gate
58420Sstevel@tonic-gate MUTEX_LOCK(&gu->gu_lock);
58430Sstevel@tonic-gate }
58440Sstevel@tonic-gate
58450Sstevel@tonic-gate MUTEX_UNLOCK(&gu->gu_lock);
58460Sstevel@tonic-gate }
58470Sstevel@tonic-gate
58480Sstevel@tonic-gate /*
58490Sstevel@tonic-gate * Unreachable for now -- there's currently no graceful cleanup
58500Sstevel@tonic-gate * called on exit().
58510Sstevel@tonic-gate */
58520Sstevel@tonic-gate MUTEX_UNLOCK(&gu->gu_lock);
58530Sstevel@tonic-gate scf_handle_destroy(h);
58540Sstevel@tonic-gate return (NULL);
58550Sstevel@tonic-gate }
58560Sstevel@tonic-gate
58570Sstevel@tonic-gate static void
set_initial_milestone(scf_handle_t * h)58580Sstevel@tonic-gate set_initial_milestone(scf_handle_t *h)
58590Sstevel@tonic-gate {
58600Sstevel@tonic-gate scf_instance_t *inst;
58610Sstevel@tonic-gate char *fmri, *cfmri;
58620Sstevel@tonic-gate size_t sz;
58630Sstevel@tonic-gate int r;
58640Sstevel@tonic-gate
58650Sstevel@tonic-gate inst = safe_scf_instance_create(h);
58660Sstevel@tonic-gate fmri = startd_alloc(max_scf_fmri_size);
58670Sstevel@tonic-gate
58680Sstevel@tonic-gate /*
58690Sstevel@tonic-gate * If -m milestone= was specified, we want to set options_ovr/milestone
58700Sstevel@tonic-gate * to it. Otherwise we want to read what the milestone should be set
58710Sstevel@tonic-gate * to. Either way we need our inst.
58720Sstevel@tonic-gate */
58730Sstevel@tonic-gate get_self:
58740Sstevel@tonic-gate if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst,
58750Sstevel@tonic-gate NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
58760Sstevel@tonic-gate switch (scf_error()) {
58770Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
58780Sstevel@tonic-gate libscf_handle_rebind(h);
58790Sstevel@tonic-gate goto get_self;
58800Sstevel@tonic-gate
58810Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
58820Sstevel@tonic-gate if (st->st_subgraph != NULL &&
58830Sstevel@tonic-gate st->st_subgraph[0] != '\0') {
58840Sstevel@tonic-gate sz = strlcpy(fmri, st->st_subgraph,
58850Sstevel@tonic-gate max_scf_fmri_size);
58860Sstevel@tonic-gate assert(sz < max_scf_fmri_size);
58870Sstevel@tonic-gate } else {
58880Sstevel@tonic-gate fmri[0] = '\0';
58890Sstevel@tonic-gate }
58900Sstevel@tonic-gate break;
58910Sstevel@tonic-gate
58920Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
58930Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
58940Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
58950Sstevel@tonic-gate default:
58960Sstevel@tonic-gate bad_error("scf_handle_decode_fmri", scf_error());
58970Sstevel@tonic-gate }
58980Sstevel@tonic-gate } else {
58990Sstevel@tonic-gate if (st->st_subgraph != NULL && st->st_subgraph[0] != '\0') {
59000Sstevel@tonic-gate scf_propertygroup_t *pg;
59010Sstevel@tonic-gate
59020Sstevel@tonic-gate pg = safe_scf_pg_create(h);
59030Sstevel@tonic-gate
59040Sstevel@tonic-gate sz = strlcpy(fmri, st->st_subgraph, max_scf_fmri_size);
59050Sstevel@tonic-gate assert(sz < max_scf_fmri_size);
59060Sstevel@tonic-gate
59070Sstevel@tonic-gate r = libscf_inst_get_or_add_pg(inst, SCF_PG_OPTIONS_OVR,
59080Sstevel@tonic-gate SCF_PG_OPTIONS_OVR_TYPE, SCF_PG_OPTIONS_OVR_FLAGS,
59090Sstevel@tonic-gate pg);
59100Sstevel@tonic-gate switch (r) {
59110Sstevel@tonic-gate case 0:
59120Sstevel@tonic-gate break;
59130Sstevel@tonic-gate
59140Sstevel@tonic-gate case ECONNABORTED:
59150Sstevel@tonic-gate libscf_handle_rebind(h);
59160Sstevel@tonic-gate goto get_self;
59170Sstevel@tonic-gate
59180Sstevel@tonic-gate case EPERM:
59190Sstevel@tonic-gate case EACCES:
59200Sstevel@tonic-gate case EROFS:
59210Sstevel@tonic-gate log_error(LOG_WARNING, "Could not set %s/%s: "
59220Sstevel@tonic-gate "%s.\n", SCF_PG_OPTIONS_OVR,
59230Sstevel@tonic-gate SCF_PROPERTY_MILESTONE, strerror(r));
59240Sstevel@tonic-gate /* FALLTHROUGH */
59250Sstevel@tonic-gate
59260Sstevel@tonic-gate case ECANCELED:
59270Sstevel@tonic-gate sz = strlcpy(fmri, st->st_subgraph,
59280Sstevel@tonic-gate max_scf_fmri_size);
59290Sstevel@tonic-gate assert(sz < max_scf_fmri_size);
59300Sstevel@tonic-gate break;
59310Sstevel@tonic-gate
59320Sstevel@tonic-gate default:
59330Sstevel@tonic-gate bad_error("libscf_inst_get_or_add_pg", r);
59340Sstevel@tonic-gate }
59350Sstevel@tonic-gate
59360Sstevel@tonic-gate r = libscf_clear_runlevel(pg, fmri);
59370Sstevel@tonic-gate switch (r) {
59380Sstevel@tonic-gate case 0:
59390Sstevel@tonic-gate break;
59400Sstevel@tonic-gate
59410Sstevel@tonic-gate case ECONNABORTED:
59420Sstevel@tonic-gate libscf_handle_rebind(h);
59430Sstevel@tonic-gate goto get_self;
59440Sstevel@tonic-gate
59450Sstevel@tonic-gate case EPERM:
59460Sstevel@tonic-gate case EACCES:
59470Sstevel@tonic-gate case EROFS:
59480Sstevel@tonic-gate log_error(LOG_WARNING, "Could not set %s/%s: "
59490Sstevel@tonic-gate "%s.\n", SCF_PG_OPTIONS_OVR,
59500Sstevel@tonic-gate SCF_PROPERTY_MILESTONE, strerror(r));
59510Sstevel@tonic-gate /* FALLTHROUGH */
59520Sstevel@tonic-gate
59530Sstevel@tonic-gate case ECANCELED:
59540Sstevel@tonic-gate sz = strlcpy(fmri, st->st_subgraph,
59550Sstevel@tonic-gate max_scf_fmri_size);
59560Sstevel@tonic-gate assert(sz < max_scf_fmri_size);
59570Sstevel@tonic-gate break;
59580Sstevel@tonic-gate
59590Sstevel@tonic-gate default:
59600Sstevel@tonic-gate bad_error("libscf_clear_runlevel", r);
59610Sstevel@tonic-gate }
59620Sstevel@tonic-gate
59630Sstevel@tonic-gate scf_pg_destroy(pg);
59640Sstevel@tonic-gate } else {
59650Sstevel@tonic-gate scf_property_t *prop;
59660Sstevel@tonic-gate scf_value_t *val;
59670Sstevel@tonic-gate
59680Sstevel@tonic-gate prop = safe_scf_property_create(h);
59690Sstevel@tonic-gate val = safe_scf_value_create(h);
59700Sstevel@tonic-gate
59710Sstevel@tonic-gate r = libscf_get_milestone(inst, prop, val, fmri,
59720Sstevel@tonic-gate max_scf_fmri_size);
59730Sstevel@tonic-gate switch (r) {
59740Sstevel@tonic-gate case 0:
59750Sstevel@tonic-gate break;
59760Sstevel@tonic-gate
59770Sstevel@tonic-gate case ECONNABORTED:
59780Sstevel@tonic-gate libscf_handle_rebind(h);
59790Sstevel@tonic-gate goto get_self;
59800Sstevel@tonic-gate
59810Sstevel@tonic-gate case EINVAL:
59820Sstevel@tonic-gate log_error(LOG_WARNING, "Milestone property is "
59830Sstevel@tonic-gate "misconfigured. Defaulting to \"all\".\n");
59840Sstevel@tonic-gate /* FALLTHROUGH */
59850Sstevel@tonic-gate
59860Sstevel@tonic-gate case ECANCELED:
59870Sstevel@tonic-gate case ENOENT:
59880Sstevel@tonic-gate fmri[0] = '\0';
59890Sstevel@tonic-gate break;
59900Sstevel@tonic-gate
59910Sstevel@tonic-gate default:
59920Sstevel@tonic-gate bad_error("libscf_get_milestone", r);
59930Sstevel@tonic-gate }
59940Sstevel@tonic-gate
59950Sstevel@tonic-gate scf_value_destroy(val);
59960Sstevel@tonic-gate scf_property_destroy(prop);
59970Sstevel@tonic-gate }
59980Sstevel@tonic-gate }
59990Sstevel@tonic-gate
60000Sstevel@tonic-gate if (fmri[0] == '\0' || strcmp(fmri, "all") == 0)
60010Sstevel@tonic-gate goto out;
60020Sstevel@tonic-gate
60030Sstevel@tonic-gate if (strcmp(fmri, "none") != 0) {
60040Sstevel@tonic-gate retry:
60050Sstevel@tonic-gate if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL,
60060Sstevel@tonic-gate NULL, SCF_DECODE_FMRI_EXACT) != 0) {
60070Sstevel@tonic-gate switch (scf_error()) {
60080Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
60090Sstevel@tonic-gate log_error(LOG_WARNING,
60100Sstevel@tonic-gate "Requested milestone \"%s\" is invalid. "
60110Sstevel@tonic-gate "Reverting to \"all\".\n", fmri);
60120Sstevel@tonic-gate goto out;
60130Sstevel@tonic-gate
60140Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
60150Sstevel@tonic-gate log_error(LOG_WARNING, "Requested milestone "
60160Sstevel@tonic-gate "\"%s\" does not specify an instance. "
60170Sstevel@tonic-gate "Reverting to \"all\".\n", fmri);
60180Sstevel@tonic-gate goto out;
60190Sstevel@tonic-gate
60200Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
60210Sstevel@tonic-gate libscf_handle_rebind(h);
60220Sstevel@tonic-gate goto retry;
60230Sstevel@tonic-gate
60240Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
60250Sstevel@tonic-gate log_error(LOG_WARNING, "Requested milestone "
60260Sstevel@tonic-gate "\"%s\" not in repository. Reverting to "
60270Sstevel@tonic-gate "\"all\".\n", fmri);
60280Sstevel@tonic-gate goto out;
60290Sstevel@tonic-gate
60300Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
60310Sstevel@tonic-gate default:
60320Sstevel@tonic-gate bad_error("scf_handle_decode_fmri",
60330Sstevel@tonic-gate scf_error());
60340Sstevel@tonic-gate }
60350Sstevel@tonic-gate }
60360Sstevel@tonic-gate
60370Sstevel@tonic-gate r = fmri_canonify(fmri, &cfmri, B_FALSE);
60380Sstevel@tonic-gate assert(r == 0);
60390Sstevel@tonic-gate
60400Sstevel@tonic-gate r = dgraph_add_instance(cfmri, inst, B_TRUE);
60410Sstevel@tonic-gate startd_free(cfmri, max_scf_fmri_size);
60420Sstevel@tonic-gate switch (r) {
60430Sstevel@tonic-gate case 0:
60440Sstevel@tonic-gate break;
60450Sstevel@tonic-gate
60460Sstevel@tonic-gate case ECONNABORTED:
60470Sstevel@tonic-gate goto retry;
60480Sstevel@tonic-gate
60490Sstevel@tonic-gate case EINVAL:
60500Sstevel@tonic-gate log_error(LOG_WARNING,
60510Sstevel@tonic-gate "Requested milestone \"%s\" is invalid. "
60520Sstevel@tonic-gate "Reverting to \"all\".\n", fmri);
60530Sstevel@tonic-gate goto out;
60540Sstevel@tonic-gate
60550Sstevel@tonic-gate case ECANCELED:
60560Sstevel@tonic-gate log_error(LOG_WARNING,
60570Sstevel@tonic-gate "Requested milestone \"%s\" not "
60580Sstevel@tonic-gate "in repository. Reverting to \"all\".\n",
60590Sstevel@tonic-gate fmri);
60600Sstevel@tonic-gate goto out;
60610Sstevel@tonic-gate
60620Sstevel@tonic-gate case EEXIST:
60630Sstevel@tonic-gate default:
60640Sstevel@tonic-gate bad_error("dgraph_add_instance", r);
60650Sstevel@tonic-gate }
60660Sstevel@tonic-gate }
60670Sstevel@tonic-gate
60680Sstevel@tonic-gate log_console(LOG_INFO, "Booting to milestone \"%s\".\n", fmri);
60690Sstevel@tonic-gate
60700Sstevel@tonic-gate r = dgraph_set_milestone(fmri, h, B_FALSE);
60710Sstevel@tonic-gate switch (r) {
60720Sstevel@tonic-gate case 0:
60730Sstevel@tonic-gate case ECONNRESET:
60740Sstevel@tonic-gate case EALREADY:
60750Sstevel@tonic-gate break;
60760Sstevel@tonic-gate
60770Sstevel@tonic-gate case EINVAL:
60780Sstevel@tonic-gate case ENOENT:
60790Sstevel@tonic-gate default:
60800Sstevel@tonic-gate bad_error("dgraph_set_milestone", r);
60810Sstevel@tonic-gate }
60820Sstevel@tonic-gate
60830Sstevel@tonic-gate out:
60840Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size);
60850Sstevel@tonic-gate scf_instance_destroy(inst);
60860Sstevel@tonic-gate }
60870Sstevel@tonic-gate
60880Sstevel@tonic-gate void
set_restart_milestone(scf_handle_t * h)60890Sstevel@tonic-gate set_restart_milestone(scf_handle_t *h)
60900Sstevel@tonic-gate {
60910Sstevel@tonic-gate scf_instance_t *inst;
60920Sstevel@tonic-gate scf_property_t *prop;
60930Sstevel@tonic-gate scf_value_t *val;
60940Sstevel@tonic-gate char *fmri;
60950Sstevel@tonic-gate int r;
60960Sstevel@tonic-gate
60970Sstevel@tonic-gate inst = safe_scf_instance_create(h);
60980Sstevel@tonic-gate
60990Sstevel@tonic-gate get_self:
61000Sstevel@tonic-gate if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL,
61010Sstevel@tonic-gate inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) {
61020Sstevel@tonic-gate switch (scf_error()) {
61030Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
61040Sstevel@tonic-gate libscf_handle_rebind(h);
61050Sstevel@tonic-gate goto get_self;
61060Sstevel@tonic-gate
61070Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
61080Sstevel@tonic-gate break;
61090Sstevel@tonic-gate
61100Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
61110Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
61120Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
61130Sstevel@tonic-gate default:
61140Sstevel@tonic-gate bad_error("scf_handle_decode_fmri", scf_error());
61150Sstevel@tonic-gate }
61160Sstevel@tonic-gate
61170Sstevel@tonic-gate scf_instance_destroy(inst);
61180Sstevel@tonic-gate return;
61190Sstevel@tonic-gate }
61200Sstevel@tonic-gate
61210Sstevel@tonic-gate prop = safe_scf_property_create(h);
61220Sstevel@tonic-gate val = safe_scf_value_create(h);
61230Sstevel@tonic-gate fmri = startd_alloc(max_scf_fmri_size);
61240Sstevel@tonic-gate
61250Sstevel@tonic-gate r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size);
61260Sstevel@tonic-gate switch (r) {
61270Sstevel@tonic-gate case 0:
61280Sstevel@tonic-gate break;
61290Sstevel@tonic-gate
61300Sstevel@tonic-gate case ECONNABORTED:
61310Sstevel@tonic-gate libscf_handle_rebind(h);
61320Sstevel@tonic-gate goto get_self;
61330Sstevel@tonic-gate
61340Sstevel@tonic-gate case ECANCELED:
61350Sstevel@tonic-gate case ENOENT:
61360Sstevel@tonic-gate case EINVAL:
61370Sstevel@tonic-gate goto out;
61380Sstevel@tonic-gate
61390Sstevel@tonic-gate default:
61400Sstevel@tonic-gate bad_error("libscf_get_milestone", r);
61410Sstevel@tonic-gate }
61420Sstevel@tonic-gate
61430Sstevel@tonic-gate r = dgraph_set_milestone(fmri, h, B_TRUE);
61440Sstevel@tonic-gate switch (r) {
61450Sstevel@tonic-gate case 0:
61460Sstevel@tonic-gate case ECONNRESET:
61470Sstevel@tonic-gate case EALREADY:
61480Sstevel@tonic-gate case EINVAL:
61490Sstevel@tonic-gate case ENOENT:
61500Sstevel@tonic-gate break;
61510Sstevel@tonic-gate
61520Sstevel@tonic-gate default:
61530Sstevel@tonic-gate bad_error("dgraph_set_milestone", r);
61540Sstevel@tonic-gate }
61550Sstevel@tonic-gate
61560Sstevel@tonic-gate out:
61570Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size);
61580Sstevel@tonic-gate scf_value_destroy(val);
61590Sstevel@tonic-gate scf_property_destroy(prop);
61600Sstevel@tonic-gate scf_instance_destroy(inst);
61610Sstevel@tonic-gate }
61620Sstevel@tonic-gate
61630Sstevel@tonic-gate /*
61640Sstevel@tonic-gate * void *graph_thread(void *)
61650Sstevel@tonic-gate *
61660Sstevel@tonic-gate * Graph management thread.
61670Sstevel@tonic-gate */
61680Sstevel@tonic-gate /*ARGSUSED*/
61690Sstevel@tonic-gate void *
graph_thread(void * arg)61700Sstevel@tonic-gate graph_thread(void *arg)
61710Sstevel@tonic-gate {
61720Sstevel@tonic-gate scf_handle_t *h;
61730Sstevel@tonic-gate int err;
61740Sstevel@tonic-gate
61750Sstevel@tonic-gate h = libscf_handle_create_bound_loop();
61760Sstevel@tonic-gate
61770Sstevel@tonic-gate if (st->st_initial)
61780Sstevel@tonic-gate set_initial_milestone(h);
61790Sstevel@tonic-gate
61800Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
61810Sstevel@tonic-gate initial_milestone_set = B_TRUE;
61820Sstevel@tonic-gate err = pthread_cond_broadcast(&initial_milestone_cv);
61830Sstevel@tonic-gate assert(err == 0);
61840Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
61850Sstevel@tonic-gate
61860Sstevel@tonic-gate libscf_populate_graph(h);
61870Sstevel@tonic-gate
61880Sstevel@tonic-gate if (!st->st_initial)
61890Sstevel@tonic-gate set_restart_milestone(h);
61900Sstevel@tonic-gate
61910Sstevel@tonic-gate MUTEX_LOCK(&st->st_load_lock);
61920Sstevel@tonic-gate st->st_load_complete = 1;
61930Sstevel@tonic-gate (void) pthread_cond_broadcast(&st->st_load_cv);
61940Sstevel@tonic-gate MUTEX_UNLOCK(&st->st_load_lock);
61950Sstevel@tonic-gate
61960Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
61970Sstevel@tonic-gate /*
61980Sstevel@tonic-gate * Now that we've set st_load_complete we need to check can_come_up()
61990Sstevel@tonic-gate * since if we booted to a milestone, then there won't be any more
62000Sstevel@tonic-gate * state updates.
62010Sstevel@tonic-gate */
62020Sstevel@tonic-gate if (!go_single_user_mode && !go_to_level1 &&
62030Sstevel@tonic-gate halting == -1) {
62043639Srm88369 if (!sulogin_thread_running && !can_come_up()) {
62050Sstevel@tonic-gate (void) startd_thread_create(sulogin_thread, NULL);
62060Sstevel@tonic-gate sulogin_thread_running = B_TRUE;
62070Sstevel@tonic-gate }
62080Sstevel@tonic-gate }
62090Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
62100Sstevel@tonic-gate
62110Sstevel@tonic-gate (void) pthread_mutex_lock(&gu->gu_freeze_lock);
62120Sstevel@tonic-gate
62130Sstevel@tonic-gate /*CONSTCOND*/
62140Sstevel@tonic-gate while (1) {
62150Sstevel@tonic-gate (void) pthread_cond_wait(&gu->gu_freeze_cv,
62160Sstevel@tonic-gate &gu->gu_freeze_lock);
62170Sstevel@tonic-gate }
62180Sstevel@tonic-gate
62190Sstevel@tonic-gate /*
62200Sstevel@tonic-gate * Unreachable for now -- there's currently no graceful cleanup
62210Sstevel@tonic-gate * called on exit().
62220Sstevel@tonic-gate */
62230Sstevel@tonic-gate (void) pthread_mutex_unlock(&gu->gu_freeze_lock);
62240Sstevel@tonic-gate scf_handle_destroy(h);
62250Sstevel@tonic-gate
62260Sstevel@tonic-gate return (NULL);
62270Sstevel@tonic-gate }
62280Sstevel@tonic-gate
62290Sstevel@tonic-gate
62300Sstevel@tonic-gate /*
62310Sstevel@tonic-gate * int next_action()
62320Sstevel@tonic-gate * Given an array of timestamps 'a' with 'num' elements, find the
62330Sstevel@tonic-gate * lowest non-zero timestamp and return its index. If there are no
62340Sstevel@tonic-gate * non-zero elements, return -1.
62350Sstevel@tonic-gate */
62360Sstevel@tonic-gate static int
next_action(hrtime_t * a,int num)62370Sstevel@tonic-gate next_action(hrtime_t *a, int num)
62380Sstevel@tonic-gate {
62390Sstevel@tonic-gate hrtime_t t = 0;
62400Sstevel@tonic-gate int i = 0, smallest = -1;
62410Sstevel@tonic-gate
62420Sstevel@tonic-gate for (i = 0; i < num; i++) {
62430Sstevel@tonic-gate if (t == 0) {
62440Sstevel@tonic-gate t = a[i];
62450Sstevel@tonic-gate smallest = i;
62460Sstevel@tonic-gate } else if (a[i] != 0 && a[i] < t) {
62470Sstevel@tonic-gate t = a[i];
62480Sstevel@tonic-gate smallest = i;
62490Sstevel@tonic-gate }
62500Sstevel@tonic-gate }
62510Sstevel@tonic-gate
62520Sstevel@tonic-gate if (t == 0)
62530Sstevel@tonic-gate return (-1);
62540Sstevel@tonic-gate else
62550Sstevel@tonic-gate return (smallest);
62560Sstevel@tonic-gate }
62570Sstevel@tonic-gate
62580Sstevel@tonic-gate /*
62590Sstevel@tonic-gate * void process_actions()
62600Sstevel@tonic-gate * Process actions requested by the administrator. Possibilities include:
62610Sstevel@tonic-gate * refresh, restart, maintenance mode off, maintenance mode on,
62620Sstevel@tonic-gate * maintenance mode immediate, and degraded.
62630Sstevel@tonic-gate *
62640Sstevel@tonic-gate * The set of pending actions is represented in the repository as a
62650Sstevel@tonic-gate * per-instance property group, with each action being a single property
62660Sstevel@tonic-gate * in that group. This property group is converted to an array, with each
62670Sstevel@tonic-gate * action type having an array slot. The actions in the array at the
62680Sstevel@tonic-gate * time process_actions() is called are acted on in the order of the
62690Sstevel@tonic-gate * timestamp (which is the value stored in the slot). A value of zero
62700Sstevel@tonic-gate * indicates that there is no pending action of the type associated with
62710Sstevel@tonic-gate * a particular slot.
62720Sstevel@tonic-gate *
62730Sstevel@tonic-gate * Sending an action event multiple times before the restarter has a
62740Sstevel@tonic-gate * chance to process that action will force it to be run at the last
62750Sstevel@tonic-gate * timestamp where it appears in the ordering.
62760Sstevel@tonic-gate *
62770Sstevel@tonic-gate * Turning maintenance mode on trumps all other actions.
62780Sstevel@tonic-gate *
62790Sstevel@tonic-gate * Returns 0 or ECONNABORTED.
62800Sstevel@tonic-gate */
62810Sstevel@tonic-gate static int
process_actions(scf_handle_t * h,scf_propertygroup_t * pg,scf_instance_t * inst)62820Sstevel@tonic-gate process_actions(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst)
62830Sstevel@tonic-gate {
62840Sstevel@tonic-gate scf_property_t *prop = NULL;
62850Sstevel@tonic-gate scf_value_t *val = NULL;
62860Sstevel@tonic-gate scf_type_t type;
62870Sstevel@tonic-gate graph_vertex_t *vertex;
62880Sstevel@tonic-gate admin_action_t a;
62890Sstevel@tonic-gate int i, ret = 0, r;
62900Sstevel@tonic-gate hrtime_t action_ts[NACTIONS];
62910Sstevel@tonic-gate char *inst_name;
62920Sstevel@tonic-gate
62930Sstevel@tonic-gate r = libscf_instance_get_fmri(inst, &inst_name);
62940Sstevel@tonic-gate switch (r) {
62950Sstevel@tonic-gate case 0:
62960Sstevel@tonic-gate break;
62970Sstevel@tonic-gate
62980Sstevel@tonic-gate case ECONNABORTED:
62990Sstevel@tonic-gate return (ECONNABORTED);
63000Sstevel@tonic-gate
63010Sstevel@tonic-gate case ECANCELED:
63020Sstevel@tonic-gate return (0);
63030Sstevel@tonic-gate
63040Sstevel@tonic-gate default:
63050Sstevel@tonic-gate bad_error("libscf_instance_get_fmri", r);
63060Sstevel@tonic-gate }
63070Sstevel@tonic-gate
63080Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
63090Sstevel@tonic-gate
63100Sstevel@tonic-gate vertex = vertex_get_by_name(inst_name);
63110Sstevel@tonic-gate if (vertex == NULL) {
63120Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
63130Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: Can't find graph vertex. "
63140Sstevel@tonic-gate "The instance must have been removed.\n", inst_name);
631510297SSeth.Goldberg@Sun.COM startd_free(inst_name, max_scf_fmri_size);
63160Sstevel@tonic-gate return (0);
63170Sstevel@tonic-gate }
63180Sstevel@tonic-gate
63190Sstevel@tonic-gate prop = safe_scf_property_create(h);
63200Sstevel@tonic-gate val = safe_scf_value_create(h);
63210Sstevel@tonic-gate
63220Sstevel@tonic-gate for (i = 0; i < NACTIONS; i++) {
63230Sstevel@tonic-gate if (scf_pg_get_property(pg, admin_actions[i], prop) != 0) {
63240Sstevel@tonic-gate switch (scf_error()) {
63250Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
63260Sstevel@tonic-gate default:
63270Sstevel@tonic-gate ret = ECONNABORTED;
63280Sstevel@tonic-gate goto out;
63290Sstevel@tonic-gate
63300Sstevel@tonic-gate case SCF_ERROR_DELETED:
63310Sstevel@tonic-gate goto out;
63320Sstevel@tonic-gate
63330Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
63340Sstevel@tonic-gate action_ts[i] = 0;
63350Sstevel@tonic-gate continue;
63360Sstevel@tonic-gate
63370Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
63380Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
63390Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
63400Sstevel@tonic-gate bad_error("scf_pg_get_property", scf_error());
63410Sstevel@tonic-gate }
63420Sstevel@tonic-gate }
63430Sstevel@tonic-gate
63440Sstevel@tonic-gate if (scf_property_type(prop, &type) != 0) {
63450Sstevel@tonic-gate switch (scf_error()) {
63460Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
63470Sstevel@tonic-gate default:
63480Sstevel@tonic-gate ret = ECONNABORTED;
63490Sstevel@tonic-gate goto out;
63500Sstevel@tonic-gate
63510Sstevel@tonic-gate case SCF_ERROR_DELETED:
63520Sstevel@tonic-gate action_ts[i] = 0;
63530Sstevel@tonic-gate continue;
63540Sstevel@tonic-gate
63550Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
63560Sstevel@tonic-gate bad_error("scf_property_type", scf_error());
63570Sstevel@tonic-gate }
63580Sstevel@tonic-gate }
63590Sstevel@tonic-gate
63600Sstevel@tonic-gate if (type != SCF_TYPE_INTEGER) {
63610Sstevel@tonic-gate action_ts[i] = 0;
63620Sstevel@tonic-gate continue;
63630Sstevel@tonic-gate }
63640Sstevel@tonic-gate
63650Sstevel@tonic-gate if (scf_property_get_value(prop, val) != 0) {
63660Sstevel@tonic-gate switch (scf_error()) {
63670Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
63680Sstevel@tonic-gate default:
63690Sstevel@tonic-gate ret = ECONNABORTED;
63700Sstevel@tonic-gate goto out;
63710Sstevel@tonic-gate
63720Sstevel@tonic-gate case SCF_ERROR_DELETED:
63730Sstevel@tonic-gate goto out;
63740Sstevel@tonic-gate
63750Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
63760Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
63770Sstevel@tonic-gate action_ts[i] = 0;
63780Sstevel@tonic-gate continue;
63790Sstevel@tonic-gate
63800Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
63815040Swesolows case SCF_ERROR_PERMISSION_DENIED:
63820Sstevel@tonic-gate bad_error("scf_property_get_value",
63830Sstevel@tonic-gate scf_error());
63840Sstevel@tonic-gate }
63850Sstevel@tonic-gate }
63860Sstevel@tonic-gate
63870Sstevel@tonic-gate r = scf_value_get_integer(val, &action_ts[i]);
63880Sstevel@tonic-gate assert(r == 0);
63890Sstevel@tonic-gate }
63900Sstevel@tonic-gate
63910Sstevel@tonic-gate a = ADMIN_EVENT_MAINT_ON_IMMEDIATE;
63920Sstevel@tonic-gate if (action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ||
63930Sstevel@tonic-gate action_ts[ADMIN_EVENT_MAINT_ON]) {
63940Sstevel@tonic-gate a = action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ?
63950Sstevel@tonic-gate ADMIN_EVENT_MAINT_ON_IMMEDIATE : ADMIN_EVENT_MAINT_ON;
63960Sstevel@tonic-gate
63970Sstevel@tonic-gate vertex_send_event(vertex, admin_events[a]);
63980Sstevel@tonic-gate r = libscf_unset_action(h, pg, a, action_ts[a]);
63990Sstevel@tonic-gate switch (r) {
64000Sstevel@tonic-gate case 0:
64010Sstevel@tonic-gate case EACCES:
64020Sstevel@tonic-gate break;
64030Sstevel@tonic-gate
64040Sstevel@tonic-gate case ECONNABORTED:
64050Sstevel@tonic-gate ret = ECONNABORTED;
64060Sstevel@tonic-gate goto out;
64070Sstevel@tonic-gate
64080Sstevel@tonic-gate case EPERM:
64090Sstevel@tonic-gate uu_die("Insufficient privilege.\n");
64100Sstevel@tonic-gate /* NOTREACHED */
64110Sstevel@tonic-gate
64120Sstevel@tonic-gate default:
64130Sstevel@tonic-gate bad_error("libscf_unset_action", r);
64140Sstevel@tonic-gate }
64150Sstevel@tonic-gate }
64160Sstevel@tonic-gate
64170Sstevel@tonic-gate while ((a = next_action(action_ts, NACTIONS)) != -1) {
64180Sstevel@tonic-gate log_framework(LOG_DEBUG,
64190Sstevel@tonic-gate "Graph: processing %s action for %s.\n", admin_actions[a],
64200Sstevel@tonic-gate inst_name);
64210Sstevel@tonic-gate
64220Sstevel@tonic-gate if (a == ADMIN_EVENT_REFRESH) {
64230Sstevel@tonic-gate r = dgraph_refresh_instance(vertex, inst);
64240Sstevel@tonic-gate switch (r) {
64250Sstevel@tonic-gate case 0:
64260Sstevel@tonic-gate case ECANCELED:
64270Sstevel@tonic-gate case EINVAL:
64280Sstevel@tonic-gate case -1:
64290Sstevel@tonic-gate break;
64300Sstevel@tonic-gate
64310Sstevel@tonic-gate case ECONNABORTED:
64320Sstevel@tonic-gate /* pg & inst are reset now, so just return. */
64330Sstevel@tonic-gate ret = ECONNABORTED;
64340Sstevel@tonic-gate goto out;
64350Sstevel@tonic-gate
64360Sstevel@tonic-gate default:
64370Sstevel@tonic-gate bad_error("dgraph_refresh_instance", r);
64380Sstevel@tonic-gate }
64390Sstevel@tonic-gate }
64400Sstevel@tonic-gate
64410Sstevel@tonic-gate vertex_send_event(vertex, admin_events[a]);
64420Sstevel@tonic-gate
64430Sstevel@tonic-gate r = libscf_unset_action(h, pg, a, action_ts[a]);
64440Sstevel@tonic-gate switch (r) {
64450Sstevel@tonic-gate case 0:
64460Sstevel@tonic-gate case EACCES:
64470Sstevel@tonic-gate break;
64480Sstevel@tonic-gate
64490Sstevel@tonic-gate case ECONNABORTED:
64500Sstevel@tonic-gate ret = ECONNABORTED;
64510Sstevel@tonic-gate goto out;
64520Sstevel@tonic-gate
64530Sstevel@tonic-gate case EPERM:
64540Sstevel@tonic-gate uu_die("Insufficient privilege.\n");
64550Sstevel@tonic-gate /* NOTREACHED */
64560Sstevel@tonic-gate
64570Sstevel@tonic-gate default:
64580Sstevel@tonic-gate bad_error("libscf_unset_action", r);
64590Sstevel@tonic-gate }
64600Sstevel@tonic-gate
64610Sstevel@tonic-gate action_ts[a] = 0;
64620Sstevel@tonic-gate }
64630Sstevel@tonic-gate
64640Sstevel@tonic-gate out:
64650Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
64660Sstevel@tonic-gate
64670Sstevel@tonic-gate scf_property_destroy(prop);
64680Sstevel@tonic-gate scf_value_destroy(val);
64690Sstevel@tonic-gate startd_free(inst_name, max_scf_fmri_size);
64700Sstevel@tonic-gate return (ret);
64710Sstevel@tonic-gate }
64720Sstevel@tonic-gate
64730Sstevel@tonic-gate /*
64740Sstevel@tonic-gate * inst and pg_name are scratch space, and are unset on entry.
64750Sstevel@tonic-gate * Returns
64760Sstevel@tonic-gate * 0 - success
64770Sstevel@tonic-gate * ECONNRESET - success, but repository handle rebound
64780Sstevel@tonic-gate * ECONNABORTED - repository connection broken
64790Sstevel@tonic-gate */
64800Sstevel@tonic-gate static int
process_pg_event(scf_handle_t * h,scf_propertygroup_t * pg,scf_instance_t * inst,char * pg_name)64810Sstevel@tonic-gate process_pg_event(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst,
64820Sstevel@tonic-gate char *pg_name)
64830Sstevel@tonic-gate {
64840Sstevel@tonic-gate int r;
64850Sstevel@tonic-gate scf_property_t *prop;
64860Sstevel@tonic-gate scf_value_t *val;
64870Sstevel@tonic-gate char *fmri;
64880Sstevel@tonic-gate boolean_t rebound = B_FALSE, rebind_inst = B_FALSE;
64890Sstevel@tonic-gate
64900Sstevel@tonic-gate if (scf_pg_get_name(pg, pg_name, max_scf_value_size) < 0) {
64910Sstevel@tonic-gate switch (scf_error()) {
64920Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
64930Sstevel@tonic-gate default:
64940Sstevel@tonic-gate return (ECONNABORTED);
64950Sstevel@tonic-gate
64960Sstevel@tonic-gate case SCF_ERROR_DELETED:
64970Sstevel@tonic-gate return (0);
64980Sstevel@tonic-gate
64990Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
65000Sstevel@tonic-gate bad_error("scf_pg_get_name", scf_error());
65010Sstevel@tonic-gate }
65020Sstevel@tonic-gate }
65030Sstevel@tonic-gate
65040Sstevel@tonic-gate if (strcmp(pg_name, SCF_PG_GENERAL) == 0 ||
65050Sstevel@tonic-gate strcmp(pg_name, SCF_PG_GENERAL_OVR) == 0) {
65060Sstevel@tonic-gate r = dgraph_update_general(pg);
65070Sstevel@tonic-gate switch (r) {
65080Sstevel@tonic-gate case 0:
65090Sstevel@tonic-gate case ENOTSUP:
65100Sstevel@tonic-gate case ECANCELED:
65110Sstevel@tonic-gate return (0);
65120Sstevel@tonic-gate
65130Sstevel@tonic-gate case ECONNABORTED:
65140Sstevel@tonic-gate return (ECONNABORTED);
65150Sstevel@tonic-gate
65160Sstevel@tonic-gate case -1:
65170Sstevel@tonic-gate /* Error should have been logged. */
65180Sstevel@tonic-gate return (0);
65190Sstevel@tonic-gate
65200Sstevel@tonic-gate default:
65210Sstevel@tonic-gate bad_error("dgraph_update_general", r);
65220Sstevel@tonic-gate }
65230Sstevel@tonic-gate } else if (strcmp(pg_name, SCF_PG_RESTARTER_ACTIONS) == 0) {
65240Sstevel@tonic-gate if (scf_pg_get_parent_instance(pg, inst) != 0) {
65250Sstevel@tonic-gate switch (scf_error()) {
65260Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
65270Sstevel@tonic-gate return (ECONNABORTED);
65280Sstevel@tonic-gate
65290Sstevel@tonic-gate case SCF_ERROR_DELETED:
65300Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
65310Sstevel@tonic-gate /* Ignore commands on services. */
65320Sstevel@tonic-gate return (0);
65330Sstevel@tonic-gate
65340Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
65350Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
65360Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
65370Sstevel@tonic-gate default:
65380Sstevel@tonic-gate bad_error("scf_pg_get_parent_instance",
65390Sstevel@tonic-gate scf_error());
65400Sstevel@tonic-gate }
65410Sstevel@tonic-gate }
65420Sstevel@tonic-gate
65430Sstevel@tonic-gate return (process_actions(h, pg, inst));
65440Sstevel@tonic-gate }
65450Sstevel@tonic-gate
65460Sstevel@tonic-gate if (strcmp(pg_name, SCF_PG_OPTIONS) != 0 &&
65470Sstevel@tonic-gate strcmp(pg_name, SCF_PG_OPTIONS_OVR) != 0)
65480Sstevel@tonic-gate return (0);
65490Sstevel@tonic-gate
65500Sstevel@tonic-gate /*
65510Sstevel@tonic-gate * We only care about the options[_ovr] property groups of our own
65520Sstevel@tonic-gate * instance, so get the fmri and compare. Plus, once we know it's
65530Sstevel@tonic-gate * correct, if the repository connection is broken we know exactly what
65540Sstevel@tonic-gate * property group we were operating on, and can look it up again.
65550Sstevel@tonic-gate */
65560Sstevel@tonic-gate if (scf_pg_get_parent_instance(pg, inst) != 0) {
65570Sstevel@tonic-gate switch (scf_error()) {
65580Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
65590Sstevel@tonic-gate return (ECONNABORTED);
65600Sstevel@tonic-gate
65610Sstevel@tonic-gate case SCF_ERROR_DELETED:
65620Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED:
65630Sstevel@tonic-gate return (0);
65640Sstevel@tonic-gate
65650Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
65660Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
65670Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
65680Sstevel@tonic-gate default:
65690Sstevel@tonic-gate bad_error("scf_pg_get_parent_instance",
65700Sstevel@tonic-gate scf_error());
65710Sstevel@tonic-gate }
65720Sstevel@tonic-gate }
65730Sstevel@tonic-gate
65740Sstevel@tonic-gate switch (r = libscf_instance_get_fmri(inst, &fmri)) {
65750Sstevel@tonic-gate case 0:
65760Sstevel@tonic-gate break;
65770Sstevel@tonic-gate
65780Sstevel@tonic-gate case ECONNABORTED:
65790Sstevel@tonic-gate return (ECONNABORTED);
65800Sstevel@tonic-gate
65810Sstevel@tonic-gate case ECANCELED:
65820Sstevel@tonic-gate return (0);
65830Sstevel@tonic-gate
65840Sstevel@tonic-gate default:
65850Sstevel@tonic-gate bad_error("libscf_instance_get_fmri", r);
65860Sstevel@tonic-gate }
65870Sstevel@tonic-gate
65880Sstevel@tonic-gate if (strcmp(fmri, SCF_SERVICE_STARTD) != 0) {
65890Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size);
65900Sstevel@tonic-gate return (0);
65910Sstevel@tonic-gate }
65920Sstevel@tonic-gate
6593*12967Sgavin.maltby@oracle.com /*
6594*12967Sgavin.maltby@oracle.com * update the information events flag
6595*12967Sgavin.maltby@oracle.com */
6596*12967Sgavin.maltby@oracle.com if (strcmp(pg_name, SCF_PG_OPTIONS) == 0)
6597*12967Sgavin.maltby@oracle.com info_events_all = libscf_get_info_events_all(pg);
6598*12967Sgavin.maltby@oracle.com
65990Sstevel@tonic-gate prop = safe_scf_property_create(h);
66000Sstevel@tonic-gate val = safe_scf_value_create(h);
66010Sstevel@tonic-gate
66020Sstevel@tonic-gate if (strcmp(pg_name, SCF_PG_OPTIONS_OVR) == 0) {
66030Sstevel@tonic-gate /* See if we need to set the runlevel. */
66040Sstevel@tonic-gate /* CONSTCOND */
66050Sstevel@tonic-gate if (0) {
66060Sstevel@tonic-gate rebind_pg:
66070Sstevel@tonic-gate libscf_handle_rebind(h);
66080Sstevel@tonic-gate rebound = B_TRUE;
66090Sstevel@tonic-gate
66100Sstevel@tonic-gate r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst);
66110Sstevel@tonic-gate switch (r) {
66120Sstevel@tonic-gate case 0:
66130Sstevel@tonic-gate break;
66140Sstevel@tonic-gate
66150Sstevel@tonic-gate case ECONNABORTED:
66160Sstevel@tonic-gate goto rebind_pg;
66170Sstevel@tonic-gate
66180Sstevel@tonic-gate case ENOENT:
66190Sstevel@tonic-gate goto out;
66200Sstevel@tonic-gate
66210Sstevel@tonic-gate case EINVAL:
66220Sstevel@tonic-gate case ENOTSUP:
66230Sstevel@tonic-gate bad_error("libscf_lookup_instance", r);
66240Sstevel@tonic-gate }
66250Sstevel@tonic-gate
66260Sstevel@tonic-gate if (scf_instance_get_pg(inst, pg_name, pg) != 0) {
66270Sstevel@tonic-gate switch (scf_error()) {
66280Sstevel@tonic-gate case SCF_ERROR_DELETED:
66290Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
66300Sstevel@tonic-gate goto out;
66310Sstevel@tonic-gate
66320Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
66330Sstevel@tonic-gate goto rebind_pg;
66340Sstevel@tonic-gate
66350Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
66360Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
66370Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
66380Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
66390Sstevel@tonic-gate default:
66400Sstevel@tonic-gate bad_error("scf_instance_get_pg",
66410Sstevel@tonic-gate scf_error());
66420Sstevel@tonic-gate }
66430Sstevel@tonic-gate }
66440Sstevel@tonic-gate }
66450Sstevel@tonic-gate
66460Sstevel@tonic-gate if (scf_pg_get_property(pg, "runlevel", prop) == 0) {
66470Sstevel@tonic-gate r = dgraph_set_runlevel(pg, prop);
66480Sstevel@tonic-gate switch (r) {
66490Sstevel@tonic-gate case ECONNRESET:
66500Sstevel@tonic-gate rebound = B_TRUE;
66510Sstevel@tonic-gate rebind_inst = B_TRUE;
66520Sstevel@tonic-gate /* FALLTHROUGH */
66530Sstevel@tonic-gate
66540Sstevel@tonic-gate case 0:
66550Sstevel@tonic-gate break;
66560Sstevel@tonic-gate
66570Sstevel@tonic-gate case ECONNABORTED:
66580Sstevel@tonic-gate goto rebind_pg;
66590Sstevel@tonic-gate
66600Sstevel@tonic-gate case ECANCELED:
66610Sstevel@tonic-gate goto out;
66620Sstevel@tonic-gate
66630Sstevel@tonic-gate default:
66640Sstevel@tonic-gate bad_error("dgraph_set_runlevel", r);
66650Sstevel@tonic-gate }
66660Sstevel@tonic-gate } else {
66670Sstevel@tonic-gate switch (scf_error()) {
66680Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
66690Sstevel@tonic-gate default:
66700Sstevel@tonic-gate goto rebind_pg;
66710Sstevel@tonic-gate
66720Sstevel@tonic-gate case SCF_ERROR_DELETED:
66730Sstevel@tonic-gate goto out;
66740Sstevel@tonic-gate
66750Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND:
66760Sstevel@tonic-gate break;
66770Sstevel@tonic-gate
66780Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT:
66790Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH:
66800Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
66810Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
66820Sstevel@tonic-gate bad_error("scf_pg_get_property", scf_error());
66830Sstevel@tonic-gate }
66840Sstevel@tonic-gate }
66850Sstevel@tonic-gate }
66860Sstevel@tonic-gate
66870Sstevel@tonic-gate if (rebind_inst) {
66880Sstevel@tonic-gate lookup_inst:
66890Sstevel@tonic-gate r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst);
66900Sstevel@tonic-gate switch (r) {
66910Sstevel@tonic-gate case 0:
66920Sstevel@tonic-gate break;
66930Sstevel@tonic-gate
66940Sstevel@tonic-gate case ECONNABORTED:
66950Sstevel@tonic-gate libscf_handle_rebind(h);
66960Sstevel@tonic-gate rebound = B_TRUE;
66970Sstevel@tonic-gate goto lookup_inst;
66980Sstevel@tonic-gate
66990Sstevel@tonic-gate case ENOENT:
67000Sstevel@tonic-gate goto out;
67010Sstevel@tonic-gate
67020Sstevel@tonic-gate case EINVAL:
67030Sstevel@tonic-gate case ENOTSUP:
67040Sstevel@tonic-gate bad_error("libscf_lookup_instance", r);
67050Sstevel@tonic-gate }
67060Sstevel@tonic-gate }
67070Sstevel@tonic-gate
67080Sstevel@tonic-gate r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size);
67090Sstevel@tonic-gate switch (r) {
67100Sstevel@tonic-gate case 0:
67110Sstevel@tonic-gate break;
67120Sstevel@tonic-gate
67130Sstevel@tonic-gate case ECONNABORTED:
67140Sstevel@tonic-gate libscf_handle_rebind(h);
67150Sstevel@tonic-gate rebound = B_TRUE;
67160Sstevel@tonic-gate goto lookup_inst;
67170Sstevel@tonic-gate
67180Sstevel@tonic-gate case EINVAL:
67190Sstevel@tonic-gate log_error(LOG_NOTICE,
67200Sstevel@tonic-gate "%s/%s property of %s is misconfigured.\n", pg_name,
67210Sstevel@tonic-gate SCF_PROPERTY_MILESTONE, SCF_SERVICE_STARTD);
67220Sstevel@tonic-gate /* FALLTHROUGH */
67230Sstevel@tonic-gate
67240Sstevel@tonic-gate case ECANCELED:
67250Sstevel@tonic-gate case ENOENT:
67260Sstevel@tonic-gate (void) strcpy(fmri, "all");
67270Sstevel@tonic-gate break;
67280Sstevel@tonic-gate
67290Sstevel@tonic-gate default:
67300Sstevel@tonic-gate bad_error("libscf_get_milestone", r);
67310Sstevel@tonic-gate }
67320Sstevel@tonic-gate
67330Sstevel@tonic-gate r = dgraph_set_milestone(fmri, h, B_FALSE);
67340Sstevel@tonic-gate switch (r) {
67350Sstevel@tonic-gate case 0:
67360Sstevel@tonic-gate case ECONNRESET:
67370Sstevel@tonic-gate case EALREADY:
67380Sstevel@tonic-gate break;
67390Sstevel@tonic-gate
67400Sstevel@tonic-gate case EINVAL:
67410Sstevel@tonic-gate log_error(LOG_WARNING, "Milestone %s is invalid.\n", fmri);
67420Sstevel@tonic-gate break;
67430Sstevel@tonic-gate
67440Sstevel@tonic-gate case ENOENT:
67450Sstevel@tonic-gate log_error(LOG_WARNING, "Milestone %s does not exist.\n", fmri);
67460Sstevel@tonic-gate break;
67470Sstevel@tonic-gate
67480Sstevel@tonic-gate default:
67490Sstevel@tonic-gate bad_error("dgraph_set_milestone", r);
67500Sstevel@tonic-gate }
67510Sstevel@tonic-gate
67520Sstevel@tonic-gate out:
67530Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size);
67540Sstevel@tonic-gate scf_value_destroy(val);
67550Sstevel@tonic-gate scf_property_destroy(prop);
67560Sstevel@tonic-gate
67570Sstevel@tonic-gate return (rebound ? ECONNRESET : 0);
67580Sstevel@tonic-gate }
67590Sstevel@tonic-gate
67606055Srm88369 /*
67616055Srm88369 * process_delete() deletes an instance from the dgraph if 'fmri' is an
67626055Srm88369 * instance fmri or if 'fmri' matches the 'general' property group of an
67636055Srm88369 * instance (or the 'general/enabled' property).
67646055Srm88369 *
67656055Srm88369 * 'fmri' may be overwritten and cannot be trusted on return by the caller.
67666055Srm88369 */
67670Sstevel@tonic-gate static void
process_delete(char * fmri,scf_handle_t * h)67680Sstevel@tonic-gate process_delete(char *fmri, scf_handle_t *h)
67690Sstevel@tonic-gate {
67706055Srm88369 char *lfmri, *end_inst_fmri;
67716055Srm88369 const char *inst_name = NULL;
67726055Srm88369 const char *pg_name = NULL;
67736055Srm88369 const char *prop_name = NULL;
67740Sstevel@tonic-gate
67750Sstevel@tonic-gate lfmri = safe_strdup(fmri);
67760Sstevel@tonic-gate
67770Sstevel@tonic-gate /* Determine if the FMRI is a property group or instance */
67780Sstevel@tonic-gate if (scf_parse_svc_fmri(lfmri, NULL, NULL, &inst_name, &pg_name,
67796055Srm88369 &prop_name) != SCF_SUCCESS) {
67800Sstevel@tonic-gate log_error(LOG_WARNING,
67810Sstevel@tonic-gate "Received invalid FMRI \"%s\" from repository server.\n",
67820Sstevel@tonic-gate fmri);
67830Sstevel@tonic-gate } else if (inst_name != NULL && pg_name == NULL) {
67840Sstevel@tonic-gate (void) dgraph_remove_instance(fmri, h);
67856055Srm88369 } else if (inst_name != NULL && pg_name != NULL) {
67866055Srm88369 /*
67876055Srm88369 * If we're deleting the 'general' property group or
67886055Srm88369 * 'general/enabled' property then the whole instance
67896055Srm88369 * must be removed from the dgraph.
67906055Srm88369 */
67916055Srm88369 if (strcmp(pg_name, SCF_PG_GENERAL) != 0) {
67926055Srm88369 free(lfmri);
67936055Srm88369 return;
67946055Srm88369 }
67956055Srm88369
67966055Srm88369 if (prop_name != NULL &&
67976055Srm88369 strcmp(prop_name, SCF_PROPERTY_ENABLED) != 0) {
67986055Srm88369 free(lfmri);
67996055Srm88369 return;
68006055Srm88369 }
68016055Srm88369
68026055Srm88369 /*
68036055Srm88369 * Because the instance has already been deleted from the
68046055Srm88369 * repository, we cannot use any scf_ functions to retrieve
68056055Srm88369 * the instance FMRI however we can easily reconstruct it
68066055Srm88369 * manually.
68076055Srm88369 */
68086055Srm88369 end_inst_fmri = strstr(fmri, SCF_FMRI_PROPERTYGRP_PREFIX);
68096055Srm88369 if (end_inst_fmri == NULL)
68106055Srm88369 bad_error("process_delete", 0);
68116055Srm88369
68126055Srm88369 end_inst_fmri[0] = '\0';
68136055Srm88369
68146055Srm88369 (void) dgraph_remove_instance(fmri, h);
68150Sstevel@tonic-gate }
68160Sstevel@tonic-gate
68170Sstevel@tonic-gate free(lfmri);
68180Sstevel@tonic-gate }
68190Sstevel@tonic-gate
68200Sstevel@tonic-gate /*ARGSUSED*/
68210Sstevel@tonic-gate void *
repository_event_thread(void * unused)68220Sstevel@tonic-gate repository_event_thread(void *unused)
68230Sstevel@tonic-gate {
68240Sstevel@tonic-gate scf_handle_t *h;
68250Sstevel@tonic-gate scf_propertygroup_t *pg;
68260Sstevel@tonic-gate scf_instance_t *inst;
68270Sstevel@tonic-gate char *fmri = startd_alloc(max_scf_fmri_size);
68280Sstevel@tonic-gate char *pg_name = startd_alloc(max_scf_value_size);
68290Sstevel@tonic-gate int r;
68300Sstevel@tonic-gate
68310Sstevel@tonic-gate h = libscf_handle_create_bound_loop();
68320Sstevel@tonic-gate
68330Sstevel@tonic-gate pg = safe_scf_pg_create(h);
68340Sstevel@tonic-gate inst = safe_scf_instance_create(h);
68350Sstevel@tonic-gate
68360Sstevel@tonic-gate retry:
68370Sstevel@tonic-gate if (_scf_notify_add_pgtype(h, SCF_GROUP_FRAMEWORK) != SCF_SUCCESS) {
68380Sstevel@tonic-gate if (scf_error() == SCF_ERROR_CONNECTION_BROKEN) {
68390Sstevel@tonic-gate libscf_handle_rebind(h);
68400Sstevel@tonic-gate } else {
68410Sstevel@tonic-gate log_error(LOG_WARNING,
68420Sstevel@tonic-gate "Couldn't set up repository notification "
68430Sstevel@tonic-gate "for property group type %s: %s\n",
68440Sstevel@tonic-gate SCF_GROUP_FRAMEWORK, scf_strerror(scf_error()));
68450Sstevel@tonic-gate
68460Sstevel@tonic-gate (void) sleep(1);
68470Sstevel@tonic-gate }
68480Sstevel@tonic-gate
68490Sstevel@tonic-gate goto retry;
68500Sstevel@tonic-gate }
68510Sstevel@tonic-gate
68520Sstevel@tonic-gate /*CONSTCOND*/
68530Sstevel@tonic-gate while (1) {
68540Sstevel@tonic-gate ssize_t res;
68550Sstevel@tonic-gate
68560Sstevel@tonic-gate /* Note: fmri is only set on delete events. */
68570Sstevel@tonic-gate res = _scf_notify_wait(pg, fmri, max_scf_fmri_size);
68580Sstevel@tonic-gate if (res < 0) {
68590Sstevel@tonic-gate libscf_handle_rebind(h);
68600Sstevel@tonic-gate goto retry;
68610Sstevel@tonic-gate } else if (res == 0) {
68620Sstevel@tonic-gate /*
68630Sstevel@tonic-gate * property group modified. inst and pg_name are
68640Sstevel@tonic-gate * pre-allocated scratch space.
68650Sstevel@tonic-gate */
68660Sstevel@tonic-gate if (scf_pg_update(pg) < 0) {
68670Sstevel@tonic-gate switch (scf_error()) {
68680Sstevel@tonic-gate case SCF_ERROR_DELETED:
68690Sstevel@tonic-gate continue;
68700Sstevel@tonic-gate
68710Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN:
68720Sstevel@tonic-gate log_error(LOG_WARNING,
68730Sstevel@tonic-gate "Lost repository event due to "
68740Sstevel@tonic-gate "disconnection.\n");
68750Sstevel@tonic-gate libscf_handle_rebind(h);
68760Sstevel@tonic-gate goto retry;
68770Sstevel@tonic-gate
68780Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND:
68790Sstevel@tonic-gate case SCF_ERROR_NOT_SET:
68800Sstevel@tonic-gate default:
68810Sstevel@tonic-gate bad_error("scf_pg_update", scf_error());
68820Sstevel@tonic-gate }
68830Sstevel@tonic-gate }
68840Sstevel@tonic-gate
68850Sstevel@tonic-gate r = process_pg_event(h, pg, inst, pg_name);
68860Sstevel@tonic-gate switch (r) {
68870Sstevel@tonic-gate case 0:
68880Sstevel@tonic-gate break;
68890Sstevel@tonic-gate
68900Sstevel@tonic-gate case ECONNABORTED:
68910Sstevel@tonic-gate log_error(LOG_WARNING, "Lost repository event "
68920Sstevel@tonic-gate "due to disconnection.\n");
68930Sstevel@tonic-gate libscf_handle_rebind(h);
68940Sstevel@tonic-gate /* FALLTHROUGH */
68950Sstevel@tonic-gate
68960Sstevel@tonic-gate case ECONNRESET:
68970Sstevel@tonic-gate goto retry;
68980Sstevel@tonic-gate
68990Sstevel@tonic-gate default:
69000Sstevel@tonic-gate bad_error("process_pg_event", r);
69010Sstevel@tonic-gate }
69020Sstevel@tonic-gate } else {
69036055Srm88369 /*
69046055Srm88369 * Service, instance, or pg deleted.
69056055Srm88369 * Don't trust fmri on return.
69066055Srm88369 */
69070Sstevel@tonic-gate process_delete(fmri, h);
69080Sstevel@tonic-gate }
69090Sstevel@tonic-gate }
69100Sstevel@tonic-gate
69110Sstevel@tonic-gate /*NOTREACHED*/
69120Sstevel@tonic-gate return (NULL);
69130Sstevel@tonic-gate }
69140Sstevel@tonic-gate
69150Sstevel@tonic-gate void
graph_engine_start()69160Sstevel@tonic-gate graph_engine_start()
69170Sstevel@tonic-gate {
69180Sstevel@tonic-gate int err;
69190Sstevel@tonic-gate
69200Sstevel@tonic-gate (void) startd_thread_create(graph_thread, NULL);
69210Sstevel@tonic-gate
69220Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock);
69230Sstevel@tonic-gate while (!initial_milestone_set) {
69240Sstevel@tonic-gate err = pthread_cond_wait(&initial_milestone_cv, &dgraph_lock);
69250Sstevel@tonic-gate assert(err == 0);
69260Sstevel@tonic-gate }
69270Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock);
69280Sstevel@tonic-gate
69290Sstevel@tonic-gate (void) startd_thread_create(repository_event_thread, NULL);
69300Sstevel@tonic-gate (void) startd_thread_create(graph_event_thread, NULL);
69310Sstevel@tonic-gate }
6932