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 */ 210Sstevel@tonic-gate /* 221514Srm88369 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * graph.c - master restarter graph engine 300Sstevel@tonic-gate * 310Sstevel@tonic-gate * The graph engine keeps a dependency graph of all service instances on the 320Sstevel@tonic-gate * system, as recorded in the repository. It decides when services should 330Sstevel@tonic-gate * be brought up or down based on service states and dependencies and sends 340Sstevel@tonic-gate * commands to restarters to effect any changes. It also executes 350Sstevel@tonic-gate * administrator commands sent by svcadm via the repository. 360Sstevel@tonic-gate * 370Sstevel@tonic-gate * The graph is stored in uu_list_t *dgraph and its vertices are 380Sstevel@tonic-gate * graph_vertex_t's, each of which has a name and an integer id unique to 390Sstevel@tonic-gate * its name (see dict.c). A vertex's type attribute designates the type 400Sstevel@tonic-gate * of object it represents: GVT_INST for service instances, GVT_SVC for 410Sstevel@tonic-gate * service objects (since service instances may depend on another service, 420Sstevel@tonic-gate * rather than service instance), GVT_FILE for files (which services may 430Sstevel@tonic-gate * depend on), and GVT_GROUP for dependencies on multiple objects. GVT_GROUP 440Sstevel@tonic-gate * vertices are necessary because dependency lists may have particular 450Sstevel@tonic-gate * grouping types (require any, require all, optional, or exclude) and 460Sstevel@tonic-gate * event-propagation characteristics. 470Sstevel@tonic-gate * 480Sstevel@tonic-gate * The initial graph is built by libscf_populate_graph() invoking 490Sstevel@tonic-gate * dgraph_add_instance() for each instance in the repository. The function 500Sstevel@tonic-gate * adds a GVT_SVC vertex for the service if one does not already exist, adds 510Sstevel@tonic-gate * a GVT_INST vertex named by the FMRI of the instance, and sets up the edges. 520Sstevel@tonic-gate * The resulting web of vertices & edges associated with an instance's vertex 530Sstevel@tonic-gate * includes 540Sstevel@tonic-gate * 550Sstevel@tonic-gate * - an edge from the GVT_SVC vertex for the instance's service 560Sstevel@tonic-gate * 570Sstevel@tonic-gate * - an edge to the GVT_INST vertex of the instance's resarter, if its 580Sstevel@tonic-gate * restarter is not svc.startd 590Sstevel@tonic-gate * 600Sstevel@tonic-gate * - edges from other GVT_INST vertices if the instance is a restarter 610Sstevel@tonic-gate * 620Sstevel@tonic-gate * - for each dependency property group in the instance's "running" 630Sstevel@tonic-gate * snapshot, an edge to a GVT_GROUP vertex named by the FMRI of the 640Sstevel@tonic-gate * instance and the name of the property group 650Sstevel@tonic-gate * 660Sstevel@tonic-gate * - for each value of the "entities" property in each dependency property 670Sstevel@tonic-gate * group, an edge from the corresponding GVT_GROUP vertex to a 680Sstevel@tonic-gate * GVT_INST, GVT_SVC, or GVT_FILE vertex 690Sstevel@tonic-gate * 700Sstevel@tonic-gate * - edges from GVT_GROUP vertices for each dependent instance 710Sstevel@tonic-gate * 720Sstevel@tonic-gate * After the edges are set up the vertex's GV_CONFIGURED flag is set. If 730Sstevel@tonic-gate * there are problems, or if a service is mentioned in a dependency but does 740Sstevel@tonic-gate * not exist in the repository, the GV_CONFIGURED flag will be clear. 750Sstevel@tonic-gate * 760Sstevel@tonic-gate * The graph and all of its vertices are protected by the dgraph_lock mutex. 770Sstevel@tonic-gate * See restarter.c for more information. 780Sstevel@tonic-gate * 790Sstevel@tonic-gate * The properties of an instance fall into two classes: immediate and 800Sstevel@tonic-gate * snapshotted. Immediate properties should have an immediate effect when 810Sstevel@tonic-gate * changed. Snapshotted properties should be read from a snapshot, so they 820Sstevel@tonic-gate * only change when the snapshot changes. The immediate properties used by 830Sstevel@tonic-gate * the graph engine are general/enabled, general/restarter, and the properties 840Sstevel@tonic-gate * in the restarter_actions property group. Since they are immediate, they 850Sstevel@tonic-gate * are not read out of a snapshot. The snapshotted properties used by the 860Sstevel@tonic-gate * graph engine are those in the property groups with type "dependency" and 870Sstevel@tonic-gate * are read out of the "running" snapshot. The "running" snapshot is created 880Sstevel@tonic-gate * by the the graph engine as soon as possible, and it is updated, along with 890Sstevel@tonic-gate * in-core copies of the data (dependency information for the graph engine) on 900Sstevel@tonic-gate * receipt of the refresh command from svcadm. In addition, the graph engine 910Sstevel@tonic-gate * updates the "start" snapshot from the "running" snapshot whenever a service 920Sstevel@tonic-gate * comes online. 930Sstevel@tonic-gate */ 940Sstevel@tonic-gate 950Sstevel@tonic-gate #include <sys/uadmin.h> 960Sstevel@tonic-gate #include <sys/wait.h> 970Sstevel@tonic-gate 980Sstevel@tonic-gate #include <assert.h> 990Sstevel@tonic-gate #include <errno.h> 1000Sstevel@tonic-gate #include <fcntl.h> 1010Sstevel@tonic-gate #include <libscf.h> 1020Sstevel@tonic-gate #include <libscf_priv.h> 1030Sstevel@tonic-gate #include <libuutil.h> 1040Sstevel@tonic-gate #include <locale.h> 1050Sstevel@tonic-gate #include <poll.h> 1060Sstevel@tonic-gate #include <pthread.h> 1070Sstevel@tonic-gate #include <signal.h> 1080Sstevel@tonic-gate #include <stddef.h> 1090Sstevel@tonic-gate #include <stdio.h> 1100Sstevel@tonic-gate #include <stdlib.h> 1110Sstevel@tonic-gate #include <string.h> 1120Sstevel@tonic-gate #include <strings.h> 1130Sstevel@tonic-gate #include <sys/statvfs.h> 1140Sstevel@tonic-gate #include <sys/uadmin.h> 1150Sstevel@tonic-gate #include <zone.h> 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate #include "startd.h" 1180Sstevel@tonic-gate #include "protocol.h" 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate #define MILESTONE_NONE ((graph_vertex_t *)1) 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate #define CONSOLE_LOGIN_FMRI "svc:/system/console-login:default" 1240Sstevel@tonic-gate #define FS_MINIMAL_FMRI "svc:/system/filesystem/minimal:default" 1250Sstevel@tonic-gate 126*1712Srm88369 #define VERTEX_REMOVED 0 /* vertex has been freed */ 127*1712Srm88369 #define VERTEX_INUSE 1 /* vertex is still in use */ 128*1712Srm88369 1290Sstevel@tonic-gate static uu_list_pool_t *graph_edge_pool, *graph_vertex_pool; 1300Sstevel@tonic-gate static uu_list_t *dgraph; 1310Sstevel@tonic-gate static pthread_mutex_t dgraph_lock; 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate /* 1340Sstevel@tonic-gate * milestone indicates the current subgraph. When NULL, it is the entire 1350Sstevel@tonic-gate * graph. When MILESTONE_NONE, it is the empty graph. Otherwise, it is all 1360Sstevel@tonic-gate * services on which the target vertex depends. 1370Sstevel@tonic-gate */ 1380Sstevel@tonic-gate static graph_vertex_t *milestone = NULL; 1390Sstevel@tonic-gate static boolean_t initial_milestone_set = B_FALSE; 1400Sstevel@tonic-gate static pthread_cond_t initial_milestone_cv = PTHREAD_COND_INITIALIZER; 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate /* protected by dgraph_lock */ 1430Sstevel@tonic-gate static boolean_t sulogin_thread_running = B_FALSE; 1440Sstevel@tonic-gate static boolean_t sulogin_running = B_FALSE; 1450Sstevel@tonic-gate static boolean_t console_login_ready = B_FALSE; 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate /* Number of services to come down to complete milestone transition. */ 1480Sstevel@tonic-gate static uint_t non_subgraph_svcs; 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate /* 1510Sstevel@tonic-gate * These variables indicate what should be done when we reach the milestone 1520Sstevel@tonic-gate * target milestone, i.e., when non_subgraph_svcs == 0. They are acted upon in 1530Sstevel@tonic-gate * dgraph_set_instance_state(). 1540Sstevel@tonic-gate */ 1550Sstevel@tonic-gate static int halting = -1; 1560Sstevel@tonic-gate static boolean_t go_single_user_mode = B_FALSE; 1570Sstevel@tonic-gate static boolean_t go_to_level1 = B_FALSE; 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate /* 1600Sstevel@tonic-gate * This tracks the legacy runlevel to ensure we signal init and manage 1610Sstevel@tonic-gate * utmpx entries correctly. 1620Sstevel@tonic-gate */ 1630Sstevel@tonic-gate static char current_runlevel = '\0'; 1640Sstevel@tonic-gate 1650Sstevel@tonic-gate /* Number of single user threads currently running */ 1660Sstevel@tonic-gate static pthread_mutex_t single_user_thread_lock; 1670Sstevel@tonic-gate static int single_user_thread_count = 0; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate /* Statistics for dependency cycle-checking */ 1700Sstevel@tonic-gate static u_longlong_t dep_inserts = 0; 1710Sstevel@tonic-gate static u_longlong_t dep_cycle_ns = 0; 1720Sstevel@tonic-gate static u_longlong_t dep_insert_ns = 0; 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate static const char * const emsg_invalid_restarter = 1760Sstevel@tonic-gate "Restarter FMRI for %s is invalid. Transitioning to maintenance.\n"; 1770Sstevel@tonic-gate static const char * const console_login_fmri = CONSOLE_LOGIN_FMRI; 1780Sstevel@tonic-gate static const char * const single_user_fmri = SCF_MILESTONE_SINGLE_USER; 1790Sstevel@tonic-gate static const char * const multi_user_fmri = SCF_MILESTONE_MULTI_USER; 1800Sstevel@tonic-gate static const char * const multi_user_svr_fmri = SCF_MILESTONE_MULTI_USER_SERVER; 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate /* 1840Sstevel@tonic-gate * These services define the system being "up". If none of them can come 1850Sstevel@tonic-gate * online, then we will run sulogin on the console. Note that the install ones 1860Sstevel@tonic-gate * are for the miniroot and when installing CDs after the first. can_come_up() 1870Sstevel@tonic-gate * does the decision making, and an sulogin_thread() runs sulogin, which can be 1880Sstevel@tonic-gate * started by dgraph_set_instance_state() or single_user_thread(). 1890Sstevel@tonic-gate * 1900Sstevel@tonic-gate * NOTE: can_come_up() relies on SCF_MILESTONE_SINGLE_USER being the first 1910Sstevel@tonic-gate * entry, which is only used when booting_to_single_user (boot -s) is set. 1920Sstevel@tonic-gate * This is because when doing a "boot -s", sulogin is started from specials.c 1930Sstevel@tonic-gate * after milestone/single-user comes online, for backwards compatibility. 1940Sstevel@tonic-gate * In this case, SCF_MILESTONE_SINGLE_USER needs to be part of up_svcs 1950Sstevel@tonic-gate * to ensure sulogin will be spawned if milestone/single-user cannot be reached. 1960Sstevel@tonic-gate */ 1970Sstevel@tonic-gate static const char * const up_svcs[] = { 1980Sstevel@tonic-gate SCF_MILESTONE_SINGLE_USER, 1990Sstevel@tonic-gate CONSOLE_LOGIN_FMRI, 2000Sstevel@tonic-gate "svc:/system/install-setup:default", 2010Sstevel@tonic-gate "svc:/system/install:default", 2020Sstevel@tonic-gate NULL 2030Sstevel@tonic-gate }; 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate /* This array must have an element for each non-NULL element of up_svcs[]. */ 2060Sstevel@tonic-gate static graph_vertex_t *up_svcs_p[] = { NULL, NULL, NULL, NULL }; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate /* These are for seed repository magic. See can_come_up(). */ 2090Sstevel@tonic-gate static const char * const manifest_import = 2100Sstevel@tonic-gate "svc:/system/manifest-import:default"; 2110Sstevel@tonic-gate static graph_vertex_t *manifest_import_p = NULL; 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate static char target_milestone_as_runlevel(void); 2150Sstevel@tonic-gate static void graph_runlevel_changed(char rl, int online); 2160Sstevel@tonic-gate static int dgraph_set_milestone(const char *, scf_handle_t *, boolean_t); 2170Sstevel@tonic-gate static void vertex_send_event(graph_vertex_t *v, restarter_event_type_t e); 2180Sstevel@tonic-gate static boolean_t should_be_in_subgraph(graph_vertex_t *v); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate /* 2210Sstevel@tonic-gate * graph_vertex_compare() 2220Sstevel@tonic-gate * This function can compare either int *id or * graph_vertex_t *gv 2230Sstevel@tonic-gate * values, as the vertex id is always the first element of a 2240Sstevel@tonic-gate * graph_vertex structure. 2250Sstevel@tonic-gate */ 2260Sstevel@tonic-gate /* ARGSUSED */ 2270Sstevel@tonic-gate static int 2280Sstevel@tonic-gate graph_vertex_compare(const void *lc_arg, const void *rc_arg, void *private) 2290Sstevel@tonic-gate { 2300Sstevel@tonic-gate int lc_id = ((const graph_vertex_t *)lc_arg)->gv_id; 2310Sstevel@tonic-gate int rc_id = *(int *)rc_arg; 2320Sstevel@tonic-gate 2330Sstevel@tonic-gate if (lc_id > rc_id) 2340Sstevel@tonic-gate return (1); 2350Sstevel@tonic-gate if (lc_id < rc_id) 2360Sstevel@tonic-gate return (-1); 2370Sstevel@tonic-gate return (0); 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate void 2410Sstevel@tonic-gate graph_init() 2420Sstevel@tonic-gate { 2430Sstevel@tonic-gate graph_edge_pool = startd_list_pool_create("graph_edges", 2440Sstevel@tonic-gate sizeof (graph_edge_t), offsetof(graph_edge_t, ge_link), NULL, 2450Sstevel@tonic-gate UU_LIST_POOL_DEBUG); 2460Sstevel@tonic-gate assert(graph_edge_pool != NULL); 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate graph_vertex_pool = startd_list_pool_create("graph_vertices", 2490Sstevel@tonic-gate sizeof (graph_vertex_t), offsetof(graph_vertex_t, gv_link), 2500Sstevel@tonic-gate graph_vertex_compare, UU_LIST_POOL_DEBUG); 2510Sstevel@tonic-gate assert(graph_vertex_pool != NULL); 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate (void) pthread_mutex_init(&dgraph_lock, &mutex_attrs); 2540Sstevel@tonic-gate (void) pthread_mutex_init(&single_user_thread_lock, &mutex_attrs); 2550Sstevel@tonic-gate dgraph = startd_list_create(graph_vertex_pool, NULL, UU_LIST_SORTED); 2560Sstevel@tonic-gate assert(dgraph != NULL); 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate if (!st->st_initial) 2590Sstevel@tonic-gate current_runlevel = utmpx_get_runlevel(); 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate log_framework(LOG_DEBUG, "Initialized graph\n"); 2620Sstevel@tonic-gate } 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate static graph_vertex_t * 2650Sstevel@tonic-gate vertex_get_by_name(const char *name) 2660Sstevel@tonic-gate { 2670Sstevel@tonic-gate int id; 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate id = dict_lookup_byname(name); 2720Sstevel@tonic-gate if (id == -1) 2730Sstevel@tonic-gate return (NULL); 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate return (uu_list_find(dgraph, &id, NULL, NULL)); 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate static graph_vertex_t * 2790Sstevel@tonic-gate vertex_get_by_id(int id) 2800Sstevel@tonic-gate { 2810Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate if (id == -1) 2840Sstevel@tonic-gate return (NULL); 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate return (uu_list_find(dgraph, &id, NULL, NULL)); 2870Sstevel@tonic-gate } 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate /* 2900Sstevel@tonic-gate * Creates a new vertex with the given name, adds it to the graph, and returns 2910Sstevel@tonic-gate * a pointer to it. The graph lock must be held by this thread on entry. 2920Sstevel@tonic-gate */ 2930Sstevel@tonic-gate static graph_vertex_t * 2940Sstevel@tonic-gate graph_add_vertex(const char *name) 2950Sstevel@tonic-gate { 2960Sstevel@tonic-gate int id; 2970Sstevel@tonic-gate graph_vertex_t *v; 2980Sstevel@tonic-gate void *p; 2990Sstevel@tonic-gate uu_list_index_t idx; 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate id = dict_insert(name); 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate v = startd_zalloc(sizeof (*v)); 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate v->gv_id = id; 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate v->gv_name = startd_alloc(strlen(name) + 1); 3100Sstevel@tonic-gate (void) strcpy(v->gv_name, name); 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate v->gv_dependencies = startd_list_create(graph_edge_pool, v, 0); 3130Sstevel@tonic-gate v->gv_dependents = startd_list_create(graph_edge_pool, v, 0); 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate p = uu_list_find(dgraph, &id, NULL, &idx); 3160Sstevel@tonic-gate assert(p == NULL); 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate uu_list_node_init(v, &v->gv_link, graph_vertex_pool); 3190Sstevel@tonic-gate uu_list_insert(dgraph, v, idx); 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate return (v); 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate /* 3250Sstevel@tonic-gate * Removes v from the graph and frees it. The graph should be locked by this 3260Sstevel@tonic-gate * thread, and v should have no edges associated with it. 3270Sstevel@tonic-gate */ 3280Sstevel@tonic-gate static void 3290Sstevel@tonic-gate graph_remove_vertex(graph_vertex_t *v) 3300Sstevel@tonic-gate { 3310Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate assert(uu_list_numnodes(v->gv_dependencies) == 0); 3340Sstevel@tonic-gate assert(uu_list_numnodes(v->gv_dependents) == 0); 335*1712Srm88369 assert(v->gv_refs == 0); 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate startd_free(v->gv_name, strlen(v->gv_name) + 1); 3380Sstevel@tonic-gate uu_list_destroy(v->gv_dependencies); 3390Sstevel@tonic-gate uu_list_destroy(v->gv_dependents); 3400Sstevel@tonic-gate uu_list_remove(dgraph, v); 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate startd_free(v, sizeof (graph_vertex_t)); 3430Sstevel@tonic-gate } 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate static void 3460Sstevel@tonic-gate graph_add_edge(graph_vertex_t *fv, graph_vertex_t *tv) 3470Sstevel@tonic-gate { 3480Sstevel@tonic-gate graph_edge_t *e, *re; 3490Sstevel@tonic-gate int r; 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate e = startd_alloc(sizeof (graph_edge_t)); 3540Sstevel@tonic-gate re = startd_alloc(sizeof (graph_edge_t)); 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate e->ge_parent = fv; 3570Sstevel@tonic-gate e->ge_vertex = tv; 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate re->ge_parent = tv; 3600Sstevel@tonic-gate re->ge_vertex = fv; 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate uu_list_node_init(e, &e->ge_link, graph_edge_pool); 3630Sstevel@tonic-gate r = uu_list_insert_before(fv->gv_dependencies, NULL, e); 3640Sstevel@tonic-gate assert(r == 0); 3650Sstevel@tonic-gate 3660Sstevel@tonic-gate uu_list_node_init(re, &re->ge_link, graph_edge_pool); 3670Sstevel@tonic-gate r = uu_list_insert_before(tv->gv_dependents, NULL, re); 3680Sstevel@tonic-gate assert(r == 0); 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate static void 3720Sstevel@tonic-gate graph_remove_edge(graph_vertex_t *v, graph_vertex_t *dv) 3730Sstevel@tonic-gate { 3740Sstevel@tonic-gate graph_edge_t *e; 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate for (e = uu_list_first(v->gv_dependencies); 3770Sstevel@tonic-gate e != NULL; 3780Sstevel@tonic-gate e = uu_list_next(v->gv_dependencies, e)) { 3790Sstevel@tonic-gate if (e->ge_vertex == dv) { 3800Sstevel@tonic-gate uu_list_remove(v->gv_dependencies, e); 3810Sstevel@tonic-gate startd_free(e, sizeof (graph_edge_t)); 3820Sstevel@tonic-gate break; 3830Sstevel@tonic-gate } 3840Sstevel@tonic-gate } 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate for (e = uu_list_first(dv->gv_dependents); 3870Sstevel@tonic-gate e != NULL; 3880Sstevel@tonic-gate e = uu_list_next(dv->gv_dependents, e)) { 3890Sstevel@tonic-gate if (e->ge_vertex == v) { 3900Sstevel@tonic-gate uu_list_remove(dv->gv_dependents, e); 3910Sstevel@tonic-gate startd_free(e, sizeof (graph_edge_t)); 3920Sstevel@tonic-gate break; 3930Sstevel@tonic-gate } 3940Sstevel@tonic-gate } 3950Sstevel@tonic-gate } 3960Sstevel@tonic-gate 3970Sstevel@tonic-gate static void 398*1712Srm88369 remove_inst_vertex(graph_vertex_t *v) 399*1712Srm88369 { 400*1712Srm88369 graph_edge_t *e; 401*1712Srm88369 graph_vertex_t *sv; 402*1712Srm88369 int i; 403*1712Srm88369 404*1712Srm88369 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 405*1712Srm88369 assert(uu_list_numnodes(v->gv_dependents) == 1); 406*1712Srm88369 assert(uu_list_numnodes(v->gv_dependencies) == 0); 407*1712Srm88369 assert(v->gv_refs == 0); 408*1712Srm88369 assert((v->gv_flags & GV_CONFIGURED) == 0); 409*1712Srm88369 410*1712Srm88369 e = uu_list_first(v->gv_dependents); 411*1712Srm88369 sv = e->ge_vertex; 412*1712Srm88369 graph_remove_edge(sv, v); 413*1712Srm88369 414*1712Srm88369 for (i = 0; up_svcs[i] != NULL; ++i) { 415*1712Srm88369 if (up_svcs_p[i] == v) 416*1712Srm88369 up_svcs_p[i] = NULL; 417*1712Srm88369 } 418*1712Srm88369 419*1712Srm88369 if (manifest_import_p == v) 420*1712Srm88369 manifest_import_p = NULL; 421*1712Srm88369 422*1712Srm88369 graph_remove_vertex(v); 423*1712Srm88369 424*1712Srm88369 if (uu_list_numnodes(sv->gv_dependencies) == 0 && 425*1712Srm88369 uu_list_numnodes(sv->gv_dependents) == 0 && 426*1712Srm88369 sv->gv_refs == 0) 427*1712Srm88369 graph_remove_vertex(sv); 428*1712Srm88369 } 429*1712Srm88369 430*1712Srm88369 static void 4310Sstevel@tonic-gate graph_walk_dependents(graph_vertex_t *v, void (*func)(graph_vertex_t *, void *), 4320Sstevel@tonic-gate void *arg) 4330Sstevel@tonic-gate { 4340Sstevel@tonic-gate graph_edge_t *e; 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate for (e = uu_list_first(v->gv_dependents); 4370Sstevel@tonic-gate e != NULL; 4380Sstevel@tonic-gate e = uu_list_next(v->gv_dependents, e)) 4390Sstevel@tonic-gate func(e->ge_vertex, arg); 4400Sstevel@tonic-gate } 4410Sstevel@tonic-gate 4420Sstevel@tonic-gate static void 4430Sstevel@tonic-gate graph_walk_dependencies(graph_vertex_t *v, void (*func)(graph_vertex_t *, 4440Sstevel@tonic-gate void *), void *arg) 4450Sstevel@tonic-gate { 4460Sstevel@tonic-gate graph_edge_t *e; 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate for (e = uu_list_first(v->gv_dependencies); 4510Sstevel@tonic-gate e != NULL; 4520Sstevel@tonic-gate e = uu_list_next(v->gv_dependencies, e)) { 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate func(e->ge_vertex, arg); 4550Sstevel@tonic-gate } 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate /* 4590Sstevel@tonic-gate * Generic graph walking function. 4600Sstevel@tonic-gate * 4610Sstevel@tonic-gate * Given a vertex, this function will walk either dependencies 4620Sstevel@tonic-gate * (WALK_DEPENDENCIES) or dependents (WALK_DEPENDENTS) of a vertex recursively 4630Sstevel@tonic-gate * for the entire graph. It will avoid cycles and never visit the same vertex 4640Sstevel@tonic-gate * twice. 4650Sstevel@tonic-gate * 4660Sstevel@tonic-gate * We avoid traversing exclusion dependencies, because they are allowed to 4670Sstevel@tonic-gate * create cycles in the graph. When propagating satisfiability, there is no 4680Sstevel@tonic-gate * need to walk exclusion dependencies because exclude_all_satisfied() doesn't 4690Sstevel@tonic-gate * test for satisfiability. 4700Sstevel@tonic-gate * 4710Sstevel@tonic-gate * The walker takes two callbacks. The first is called before examining the 4720Sstevel@tonic-gate * dependents of each vertex. The second is called on each vertex after 4730Sstevel@tonic-gate * examining its dependents. This allows is_path_to() to construct a path only 4740Sstevel@tonic-gate * after the target vertex has been found. 4750Sstevel@tonic-gate */ 4760Sstevel@tonic-gate typedef enum { 4770Sstevel@tonic-gate WALK_DEPENDENTS, 4780Sstevel@tonic-gate WALK_DEPENDENCIES 4790Sstevel@tonic-gate } graph_walk_dir_t; 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate typedef int (*graph_walk_cb_t)(graph_vertex_t *, void *); 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate typedef struct graph_walk_info { 4840Sstevel@tonic-gate graph_walk_dir_t gi_dir; 4850Sstevel@tonic-gate uchar_t *gi_visited; /* vertex bitmap */ 4860Sstevel@tonic-gate int (*gi_pre)(graph_vertex_t *, void *); 4870Sstevel@tonic-gate void (*gi_post)(graph_vertex_t *, void *); 4880Sstevel@tonic-gate void *gi_arg; /* callback arg */ 4890Sstevel@tonic-gate int gi_ret; /* return value */ 4900Sstevel@tonic-gate } graph_walk_info_t; 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate static int 4930Sstevel@tonic-gate graph_walk_recurse(graph_edge_t *e, graph_walk_info_t *gip) 4940Sstevel@tonic-gate { 4950Sstevel@tonic-gate uu_list_t *list; 4960Sstevel@tonic-gate int r; 4970Sstevel@tonic-gate graph_vertex_t *v = e->ge_vertex; 4980Sstevel@tonic-gate int i; 4990Sstevel@tonic-gate uint_t b; 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate i = v->gv_id / 8; 5020Sstevel@tonic-gate b = 1 << (v->gv_id % 8); 5030Sstevel@tonic-gate 5040Sstevel@tonic-gate /* 5050Sstevel@tonic-gate * Check to see if we've visited this vertex already. 5060Sstevel@tonic-gate */ 5070Sstevel@tonic-gate if (gip->gi_visited[i] & b) 5080Sstevel@tonic-gate return (UU_WALK_NEXT); 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate gip->gi_visited[i] |= b; 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate /* 5130Sstevel@tonic-gate * Don't follow exclusions. 5140Sstevel@tonic-gate */ 5150Sstevel@tonic-gate if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL) 5160Sstevel@tonic-gate return (UU_WALK_NEXT); 5170Sstevel@tonic-gate 5180Sstevel@tonic-gate /* 5190Sstevel@tonic-gate * Call pre-visit callback. If this doesn't terminate the walk, 5200Sstevel@tonic-gate * continue search. 5210Sstevel@tonic-gate */ 5220Sstevel@tonic-gate if ((gip->gi_ret = gip->gi_pre(v, gip->gi_arg)) == UU_WALK_NEXT) { 5230Sstevel@tonic-gate /* 5240Sstevel@tonic-gate * Recurse using appropriate list. 5250Sstevel@tonic-gate */ 5260Sstevel@tonic-gate if (gip->gi_dir == WALK_DEPENDENTS) 5270Sstevel@tonic-gate list = v->gv_dependents; 5280Sstevel@tonic-gate else 5290Sstevel@tonic-gate list = v->gv_dependencies; 5300Sstevel@tonic-gate 5310Sstevel@tonic-gate r = uu_list_walk(list, (uu_walk_fn_t *)graph_walk_recurse, 5320Sstevel@tonic-gate gip, 0); 5330Sstevel@tonic-gate assert(r == 0); 5340Sstevel@tonic-gate } 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate /* 5370Sstevel@tonic-gate * Callbacks must return either UU_WALK_NEXT or UU_WALK_DONE. 5380Sstevel@tonic-gate */ 5390Sstevel@tonic-gate assert(gip->gi_ret == UU_WALK_NEXT || gip->gi_ret == UU_WALK_DONE); 5400Sstevel@tonic-gate 5410Sstevel@tonic-gate /* 5420Sstevel@tonic-gate * If given a post-callback, call the function for every vertex. 5430Sstevel@tonic-gate */ 5440Sstevel@tonic-gate if (gip->gi_post != NULL) 5450Sstevel@tonic-gate (void) gip->gi_post(v, gip->gi_arg); 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate /* 5480Sstevel@tonic-gate * Preserve the callback's return value. If the callback returns 5490Sstevel@tonic-gate * UU_WALK_DONE, then we propagate that to the caller in order to 5500Sstevel@tonic-gate * terminate the walk. 5510Sstevel@tonic-gate */ 5520Sstevel@tonic-gate return (gip->gi_ret); 5530Sstevel@tonic-gate } 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate static void 5560Sstevel@tonic-gate graph_walk(graph_vertex_t *v, graph_walk_dir_t dir, 5570Sstevel@tonic-gate int (*pre)(graph_vertex_t *, void *), 5580Sstevel@tonic-gate void (*post)(graph_vertex_t *, void *), void *arg) 5590Sstevel@tonic-gate { 5600Sstevel@tonic-gate graph_walk_info_t gi; 5610Sstevel@tonic-gate graph_edge_t fake; 5620Sstevel@tonic-gate size_t sz = dictionary->dict_new_id / 8 + 1; 5630Sstevel@tonic-gate 5640Sstevel@tonic-gate gi.gi_visited = startd_zalloc(sz); 5650Sstevel@tonic-gate gi.gi_pre = pre; 5660Sstevel@tonic-gate gi.gi_post = post; 5670Sstevel@tonic-gate gi.gi_arg = arg; 5680Sstevel@tonic-gate gi.gi_dir = dir; 5690Sstevel@tonic-gate gi.gi_ret = 0; 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate /* 5720Sstevel@tonic-gate * Fake up an edge for the first iteration 5730Sstevel@tonic-gate */ 5740Sstevel@tonic-gate fake.ge_vertex = v; 5750Sstevel@tonic-gate (void) graph_walk_recurse(&fake, &gi); 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate startd_free(gi.gi_visited, sz); 5780Sstevel@tonic-gate } 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate typedef struct child_search { 5810Sstevel@tonic-gate int id; /* id of vertex to look for */ 5820Sstevel@tonic-gate uint_t depth; /* recursion depth */ 5830Sstevel@tonic-gate /* 5840Sstevel@tonic-gate * While the vertex is not found, path is NULL. After the search, if 5850Sstevel@tonic-gate * the vertex was found then path should point to a -1-terminated 5860Sstevel@tonic-gate * array of vertex id's which constitute the path to the vertex. 5870Sstevel@tonic-gate */ 5880Sstevel@tonic-gate int *path; 5890Sstevel@tonic-gate } child_search_t; 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate static int 5920Sstevel@tonic-gate child_pre(graph_vertex_t *v, void *arg) 5930Sstevel@tonic-gate { 5940Sstevel@tonic-gate child_search_t *cs = arg; 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate cs->depth++; 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate if (v->gv_id == cs->id) { 5990Sstevel@tonic-gate cs->path = startd_alloc((cs->depth + 1) * sizeof (int)); 6000Sstevel@tonic-gate cs->path[cs->depth] = -1; 6010Sstevel@tonic-gate return (UU_WALK_DONE); 6020Sstevel@tonic-gate } 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate return (UU_WALK_NEXT); 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate static void 6080Sstevel@tonic-gate child_post(graph_vertex_t *v, void *arg) 6090Sstevel@tonic-gate { 6100Sstevel@tonic-gate child_search_t *cs = arg; 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate cs->depth--; 6130Sstevel@tonic-gate 6140Sstevel@tonic-gate if (cs->path != NULL) 6150Sstevel@tonic-gate cs->path[cs->depth] = v->gv_id; 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate /* 6190Sstevel@tonic-gate * Look for a path from from to to. If one exists, returns a pointer to 6200Sstevel@tonic-gate * a NULL-terminated array of pointers to the vertices along the path. If 6210Sstevel@tonic-gate * there is no path, returns NULL. 6220Sstevel@tonic-gate */ 6230Sstevel@tonic-gate static int * 6240Sstevel@tonic-gate is_path_to(graph_vertex_t *from, graph_vertex_t *to) 6250Sstevel@tonic-gate { 6260Sstevel@tonic-gate child_search_t cs; 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate cs.id = to->gv_id; 6290Sstevel@tonic-gate cs.depth = 0; 6300Sstevel@tonic-gate cs.path = NULL; 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate graph_walk(from, WALK_DEPENDENCIES, child_pre, child_post, &cs); 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate return (cs.path); 6350Sstevel@tonic-gate } 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate /* 6380Sstevel@tonic-gate * Given an array of int's as returned by is_path_to, allocates a string of 6390Sstevel@tonic-gate * their names joined by newlines. Returns the size of the allocated buffer 6400Sstevel@tonic-gate * in *sz and frees path. 6410Sstevel@tonic-gate */ 6420Sstevel@tonic-gate static void 6430Sstevel@tonic-gate path_to_str(int *path, char **cpp, size_t *sz) 6440Sstevel@tonic-gate { 6450Sstevel@tonic-gate int i; 6460Sstevel@tonic-gate graph_vertex_t *v; 6470Sstevel@tonic-gate size_t allocd, new_allocd; 6480Sstevel@tonic-gate char *new, *name; 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 6510Sstevel@tonic-gate assert(path[0] != -1); 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate allocd = 1; 6540Sstevel@tonic-gate *cpp = startd_alloc(1); 6550Sstevel@tonic-gate (*cpp)[0] = '\0'; 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate for (i = 0; path[i] != -1; ++i) { 6580Sstevel@tonic-gate name = NULL; 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate v = vertex_get_by_id(path[i]); 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate if (v == NULL) 6630Sstevel@tonic-gate name = "<deleted>"; 6640Sstevel@tonic-gate else if (v->gv_type == GVT_INST || v->gv_type == GVT_SVC) 6650Sstevel@tonic-gate name = v->gv_name; 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate if (name != NULL) { 6680Sstevel@tonic-gate new_allocd = allocd + strlen(name) + 1; 6690Sstevel@tonic-gate new = startd_alloc(new_allocd); 6700Sstevel@tonic-gate (void) strcpy(new, *cpp); 6710Sstevel@tonic-gate (void) strcat(new, name); 6720Sstevel@tonic-gate (void) strcat(new, "\n"); 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate startd_free(*cpp, allocd); 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate *cpp = new; 6770Sstevel@tonic-gate allocd = new_allocd; 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate } 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate startd_free(path, sizeof (int) * (i + 1)); 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate *sz = allocd; 6840Sstevel@tonic-gate } 6850Sstevel@tonic-gate 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate /* 6880Sstevel@tonic-gate * This function along with run_sulogin() implements an exclusion relationship 6890Sstevel@tonic-gate * between system/console-login and sulogin. run_sulogin() will fail if 6900Sstevel@tonic-gate * system/console-login is online, and the graph engine should call 6910Sstevel@tonic-gate * graph_clogin_start() to bring system/console-login online, which defers the 6920Sstevel@tonic-gate * start if sulogin is running. 6930Sstevel@tonic-gate */ 6940Sstevel@tonic-gate static void 6950Sstevel@tonic-gate graph_clogin_start(graph_vertex_t *v) 6960Sstevel@tonic-gate { 6970Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate if (sulogin_running) 7000Sstevel@tonic-gate console_login_ready = B_TRUE; 7010Sstevel@tonic-gate else 7020Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_START); 7030Sstevel@tonic-gate } 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate static void 7060Sstevel@tonic-gate graph_su_start(graph_vertex_t *v) 7070Sstevel@tonic-gate { 7080Sstevel@tonic-gate /* 7090Sstevel@tonic-gate * /etc/inittab used to have the initial /sbin/rcS as a 'sysinit' 7100Sstevel@tonic-gate * entry with a runlevel of 'S', before jumping to the final 7110Sstevel@tonic-gate * target runlevel (as set in initdefault). We mimic that legacy 7120Sstevel@tonic-gate * behavior here. 7130Sstevel@tonic-gate */ 7140Sstevel@tonic-gate utmpx_set_runlevel('S', '0', B_FALSE); 7150Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_START); 7160Sstevel@tonic-gate } 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate static void 7190Sstevel@tonic-gate graph_post_su_online(void) 7200Sstevel@tonic-gate { 7210Sstevel@tonic-gate graph_runlevel_changed('S', 1); 7220Sstevel@tonic-gate } 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate static void 7250Sstevel@tonic-gate graph_post_su_disable(void) 7260Sstevel@tonic-gate { 7270Sstevel@tonic-gate graph_runlevel_changed('S', 0); 7280Sstevel@tonic-gate } 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate static void 7310Sstevel@tonic-gate graph_post_mu_online(void) 7320Sstevel@tonic-gate { 7330Sstevel@tonic-gate graph_runlevel_changed('2', 1); 7340Sstevel@tonic-gate } 7350Sstevel@tonic-gate 7360Sstevel@tonic-gate static void 7370Sstevel@tonic-gate graph_post_mu_disable(void) 7380Sstevel@tonic-gate { 7390Sstevel@tonic-gate graph_runlevel_changed('2', 0); 7400Sstevel@tonic-gate } 7410Sstevel@tonic-gate 7420Sstevel@tonic-gate static void 7430Sstevel@tonic-gate graph_post_mus_online(void) 7440Sstevel@tonic-gate { 7450Sstevel@tonic-gate graph_runlevel_changed('3', 1); 7460Sstevel@tonic-gate } 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate static void 7490Sstevel@tonic-gate graph_post_mus_disable(void) 7500Sstevel@tonic-gate { 7510Sstevel@tonic-gate graph_runlevel_changed('3', 0); 7520Sstevel@tonic-gate } 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate static struct special_vertex_info { 7550Sstevel@tonic-gate const char *name; 7560Sstevel@tonic-gate void (*start_f)(graph_vertex_t *); 7570Sstevel@tonic-gate void (*post_online_f)(void); 7580Sstevel@tonic-gate void (*post_disable_f)(void); 7590Sstevel@tonic-gate } special_vertices[] = { 7600Sstevel@tonic-gate { CONSOLE_LOGIN_FMRI, graph_clogin_start, NULL, NULL }, 7610Sstevel@tonic-gate { SCF_MILESTONE_SINGLE_USER, graph_su_start, 7620Sstevel@tonic-gate graph_post_su_online, graph_post_su_disable }, 7630Sstevel@tonic-gate { SCF_MILESTONE_MULTI_USER, NULL, 7640Sstevel@tonic-gate graph_post_mu_online, graph_post_mu_disable }, 7650Sstevel@tonic-gate { SCF_MILESTONE_MULTI_USER_SERVER, NULL, 7660Sstevel@tonic-gate graph_post_mus_online, graph_post_mus_disable }, 7670Sstevel@tonic-gate { NULL }, 7680Sstevel@tonic-gate }; 7690Sstevel@tonic-gate 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate void 7720Sstevel@tonic-gate vertex_send_event(graph_vertex_t *v, restarter_event_type_t e) 7730Sstevel@tonic-gate { 7740Sstevel@tonic-gate switch (e) { 7750Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADD_INSTANCE: 7760Sstevel@tonic-gate assert(v->gv_state == RESTARTER_STATE_UNINIT); 7770Sstevel@tonic-gate 7780Sstevel@tonic-gate MUTEX_LOCK(&st->st_load_lock); 7790Sstevel@tonic-gate st->st_load_instances++; 7800Sstevel@tonic-gate MUTEX_UNLOCK(&st->st_load_lock); 7810Sstevel@tonic-gate break; 7820Sstevel@tonic-gate 7830Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ENABLE: 7840Sstevel@tonic-gate log_framework(LOG_DEBUG, "Enabling %s.\n", v->gv_name); 7850Sstevel@tonic-gate assert(v->gv_state == RESTARTER_STATE_UNINIT || 7860Sstevel@tonic-gate v->gv_state == RESTARTER_STATE_DISABLED || 7870Sstevel@tonic-gate v->gv_state == RESTARTER_STATE_MAINT); 7880Sstevel@tonic-gate break; 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DISABLE: 7910Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_DISABLE: 7920Sstevel@tonic-gate log_framework(LOG_DEBUG, "Disabling %s.\n", v->gv_name); 7930Sstevel@tonic-gate assert(v->gv_state != RESTARTER_STATE_DISABLED); 7940Sstevel@tonic-gate break; 7950Sstevel@tonic-gate 7960Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_STOP: 7970Sstevel@tonic-gate log_framework(LOG_DEBUG, "Stopping %s.\n", v->gv_name); 7980Sstevel@tonic-gate assert(v->gv_state == RESTARTER_STATE_DEGRADED || 7990Sstevel@tonic-gate v->gv_state == RESTARTER_STATE_ONLINE); 8000Sstevel@tonic-gate break; 8010Sstevel@tonic-gate 8020Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_START: 8030Sstevel@tonic-gate log_framework(LOG_DEBUG, "Starting %s.\n", v->gv_name); 8040Sstevel@tonic-gate assert(v->gv_state == RESTARTER_STATE_OFFLINE); 8050Sstevel@tonic-gate break; 8060Sstevel@tonic-gate 8070Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE: 8080Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_DEGRADED: 8090Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_REFRESH: 8100Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_RESTART: 8110Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF: 8120Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 8130Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE: 8140Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE: 8150Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY: 8160Sstevel@tonic-gate break; 8170Sstevel@tonic-gate 8180Sstevel@tonic-gate default: 8190Sstevel@tonic-gate #ifndef NDEBUG 8200Sstevel@tonic-gate uu_warn("%s:%d: Bad event %d.\n", __FILE__, __LINE__, e); 8210Sstevel@tonic-gate #endif 8220Sstevel@tonic-gate abort(); 8230Sstevel@tonic-gate } 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate restarter_protocol_send_event(v->gv_name, v->gv_restarter_channel, e); 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate static void 8290Sstevel@tonic-gate graph_unset_restarter(graph_vertex_t *v) 8300Sstevel@tonic-gate { 8310Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 8320Sstevel@tonic-gate assert(v->gv_flags & GV_CONFIGURED); 8330Sstevel@tonic-gate 8340Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_REMOVE_INSTANCE); 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate if (v->gv_restarter_id != -1) { 8370Sstevel@tonic-gate graph_vertex_t *rv; 8380Sstevel@tonic-gate 8390Sstevel@tonic-gate rv = vertex_get_by_id(v->gv_restarter_id); 8400Sstevel@tonic-gate graph_remove_edge(v, rv); 8410Sstevel@tonic-gate } 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate v->gv_restarter_id = -1; 8440Sstevel@tonic-gate v->gv_restarter_channel = NULL; 8450Sstevel@tonic-gate } 8460Sstevel@tonic-gate 847*1712Srm88369 /* 848*1712Srm88369 * Return VERTEX_REMOVED when the vertex passed in argument is deleted from the 849*1712Srm88369 * dgraph otherwise return VERTEX_INUSE. 850*1712Srm88369 */ 851*1712Srm88369 static int 852*1712Srm88369 free_if_unrefed(graph_vertex_t *v) 853*1712Srm88369 { 854*1712Srm88369 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 855*1712Srm88369 856*1712Srm88369 if (v->gv_refs > 0) 857*1712Srm88369 return (VERTEX_INUSE); 858*1712Srm88369 859*1712Srm88369 if (v->gv_type == GVT_SVC && 860*1712Srm88369 uu_list_numnodes(v->gv_dependents) == 0 && 861*1712Srm88369 uu_list_numnodes(v->gv_dependencies) == 0) { 862*1712Srm88369 graph_remove_vertex(v); 863*1712Srm88369 return (VERTEX_REMOVED); 864*1712Srm88369 } else if (v->gv_type == GVT_INST && 865*1712Srm88369 (v->gv_flags & GV_CONFIGURED) == 0 && 866*1712Srm88369 uu_list_numnodes(v->gv_dependents) == 1 && 867*1712Srm88369 uu_list_numnodes(v->gv_dependencies) == 0) { 868*1712Srm88369 remove_inst_vertex(v); 869*1712Srm88369 return (VERTEX_REMOVED); 870*1712Srm88369 } 871*1712Srm88369 872*1712Srm88369 return (VERTEX_INUSE); 873*1712Srm88369 } 874*1712Srm88369 8750Sstevel@tonic-gate static void 8760Sstevel@tonic-gate delete_depgroup(graph_vertex_t *v) 8770Sstevel@tonic-gate { 8780Sstevel@tonic-gate graph_edge_t *e; 8790Sstevel@tonic-gate graph_vertex_t *dv; 8800Sstevel@tonic-gate 8810Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 8820Sstevel@tonic-gate assert(v->gv_type == GVT_GROUP); 8830Sstevel@tonic-gate assert(uu_list_numnodes(v->gv_dependents) == 0); 8840Sstevel@tonic-gate 8850Sstevel@tonic-gate while ((e = uu_list_first(v->gv_dependencies)) != NULL) { 8860Sstevel@tonic-gate dv = e->ge_vertex; 8870Sstevel@tonic-gate 8880Sstevel@tonic-gate graph_remove_edge(v, dv); 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate switch (dv->gv_type) { 8910Sstevel@tonic-gate case GVT_INST: /* instance dependency */ 8920Sstevel@tonic-gate case GVT_SVC: /* service dependency */ 893*1712Srm88369 (void) free_if_unrefed(dv); 8940Sstevel@tonic-gate break; 8950Sstevel@tonic-gate 8960Sstevel@tonic-gate case GVT_FILE: /* file dependency */ 8970Sstevel@tonic-gate assert(uu_list_numnodes(dv->gv_dependencies) == 0); 8980Sstevel@tonic-gate if (uu_list_numnodes(dv->gv_dependents) == 0) 8990Sstevel@tonic-gate graph_remove_vertex(dv); 9000Sstevel@tonic-gate break; 9010Sstevel@tonic-gate 9020Sstevel@tonic-gate default: 9030Sstevel@tonic-gate #ifndef NDEBUG 9040Sstevel@tonic-gate uu_warn("%s:%d: Unexpected node type %d", __FILE__, 9050Sstevel@tonic-gate __LINE__, dv->gv_type); 9060Sstevel@tonic-gate #endif 9070Sstevel@tonic-gate abort(); 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate } 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate graph_remove_vertex(v); 9120Sstevel@tonic-gate } 9130Sstevel@tonic-gate 9140Sstevel@tonic-gate static int 9150Sstevel@tonic-gate delete_instance_deps_cb(graph_edge_t *e, void **ptrs) 9160Sstevel@tonic-gate { 9170Sstevel@tonic-gate graph_vertex_t *v = ptrs[0]; 9180Sstevel@tonic-gate boolean_t delete_restarter_dep = (boolean_t)ptrs[1]; 9190Sstevel@tonic-gate graph_vertex_t *dv; 9200Sstevel@tonic-gate 9210Sstevel@tonic-gate dv = e->ge_vertex; 9220Sstevel@tonic-gate 9230Sstevel@tonic-gate /* 9240Sstevel@tonic-gate * We have four possibilities here: 9250Sstevel@tonic-gate * - GVT_INST: restarter 9260Sstevel@tonic-gate * - GVT_GROUP - GVT_INST: instance dependency 9270Sstevel@tonic-gate * - GVT_GROUP - GVT_SVC - GV_INST: service dependency 9280Sstevel@tonic-gate * - GVT_GROUP - GVT_FILE: file dependency 9290Sstevel@tonic-gate */ 9300Sstevel@tonic-gate switch (dv->gv_type) { 9310Sstevel@tonic-gate case GVT_INST: /* restarter */ 9320Sstevel@tonic-gate assert(dv->gv_id == v->gv_restarter_id); 9330Sstevel@tonic-gate if (delete_restarter_dep) 9340Sstevel@tonic-gate graph_remove_edge(v, dv); 9350Sstevel@tonic-gate break; 9360Sstevel@tonic-gate 9370Sstevel@tonic-gate case GVT_GROUP: /* pg dependency */ 9380Sstevel@tonic-gate graph_remove_edge(v, dv); 9390Sstevel@tonic-gate delete_depgroup(dv); 9400Sstevel@tonic-gate break; 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate case GVT_FILE: 9430Sstevel@tonic-gate /* These are currently not direct dependencies */ 9440Sstevel@tonic-gate 9450Sstevel@tonic-gate default: 9460Sstevel@tonic-gate #ifndef NDEBUG 9470Sstevel@tonic-gate uu_warn("%s:%d: Bad vertex type %d.\n", __FILE__, __LINE__, 9480Sstevel@tonic-gate dv->gv_type); 9490Sstevel@tonic-gate #endif 9500Sstevel@tonic-gate abort(); 9510Sstevel@tonic-gate } 9520Sstevel@tonic-gate 9530Sstevel@tonic-gate return (UU_WALK_NEXT); 9540Sstevel@tonic-gate } 9550Sstevel@tonic-gate 9560Sstevel@tonic-gate static void 9570Sstevel@tonic-gate delete_instance_dependencies(graph_vertex_t *v, boolean_t delete_restarter_dep) 9580Sstevel@tonic-gate { 9590Sstevel@tonic-gate void *ptrs[2]; 9600Sstevel@tonic-gate int r; 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 9630Sstevel@tonic-gate assert(v->gv_type == GVT_INST); 9640Sstevel@tonic-gate 9650Sstevel@tonic-gate ptrs[0] = v; 9660Sstevel@tonic-gate ptrs[1] = (void *)delete_restarter_dep; 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate r = uu_list_walk(v->gv_dependencies, 9690Sstevel@tonic-gate (uu_walk_fn_t *)delete_instance_deps_cb, &ptrs, UU_WALK_ROBUST); 9700Sstevel@tonic-gate assert(r == 0); 9710Sstevel@tonic-gate } 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate /* 9740Sstevel@tonic-gate * int graph_insert_vertex_unconfigured() 9750Sstevel@tonic-gate * Insert a vertex without sending any restarter events. If the vertex 9760Sstevel@tonic-gate * already exists or creation is successful, return a pointer to it in *vp. 9770Sstevel@tonic-gate * 9780Sstevel@tonic-gate * If type is not GVT_GROUP, dt can remain unset. 9790Sstevel@tonic-gate * 9800Sstevel@tonic-gate * Returns 0, EEXIST, or EINVAL if the arguments are invalid (i.e., fmri 9810Sstevel@tonic-gate * doesn't agree with type, or type doesn't agree with dt). 9820Sstevel@tonic-gate */ 9830Sstevel@tonic-gate static int 9840Sstevel@tonic-gate graph_insert_vertex_unconfigured(const char *fmri, gv_type_t type, 9850Sstevel@tonic-gate depgroup_type_t dt, restarter_error_t rt, graph_vertex_t **vp) 9860Sstevel@tonic-gate { 9870Sstevel@tonic-gate int r; 9880Sstevel@tonic-gate int i; 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 9910Sstevel@tonic-gate 9920Sstevel@tonic-gate switch (type) { 9930Sstevel@tonic-gate case GVT_SVC: 9940Sstevel@tonic-gate case GVT_INST: 9950Sstevel@tonic-gate if (strncmp(fmri, "svc:", sizeof ("svc:") - 1) != 0) 9960Sstevel@tonic-gate return (EINVAL); 9970Sstevel@tonic-gate break; 9980Sstevel@tonic-gate 9990Sstevel@tonic-gate case GVT_FILE: 10000Sstevel@tonic-gate if (strncmp(fmri, "file:", sizeof ("file:") - 1) != 0) 10010Sstevel@tonic-gate return (EINVAL); 10020Sstevel@tonic-gate break; 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate case GVT_GROUP: 10050Sstevel@tonic-gate if (dt <= 0 || rt < 0) 10060Sstevel@tonic-gate return (EINVAL); 10070Sstevel@tonic-gate break; 10080Sstevel@tonic-gate 10090Sstevel@tonic-gate default: 10100Sstevel@tonic-gate #ifndef NDEBUG 10110Sstevel@tonic-gate uu_warn("%s:%d: Unknown type %d.\n", __FILE__, __LINE__, type); 10120Sstevel@tonic-gate #endif 10130Sstevel@tonic-gate abort(); 10140Sstevel@tonic-gate } 10150Sstevel@tonic-gate 10160Sstevel@tonic-gate *vp = vertex_get_by_name(fmri); 10170Sstevel@tonic-gate if (*vp != NULL) 10180Sstevel@tonic-gate return (EEXIST); 10190Sstevel@tonic-gate 10200Sstevel@tonic-gate *vp = graph_add_vertex(fmri); 10210Sstevel@tonic-gate 10220Sstevel@tonic-gate (*vp)->gv_type = type; 10230Sstevel@tonic-gate (*vp)->gv_depgroup = dt; 10240Sstevel@tonic-gate (*vp)->gv_restart = rt; 10250Sstevel@tonic-gate 10260Sstevel@tonic-gate (*vp)->gv_flags = 0; 10270Sstevel@tonic-gate (*vp)->gv_state = RESTARTER_STATE_NONE; 10280Sstevel@tonic-gate 10290Sstevel@tonic-gate for (i = 0; special_vertices[i].name != NULL; ++i) { 10300Sstevel@tonic-gate if (strcmp(fmri, special_vertices[i].name) == 0) { 10310Sstevel@tonic-gate (*vp)->gv_start_f = special_vertices[i].start_f; 10320Sstevel@tonic-gate (*vp)->gv_post_online_f = 10330Sstevel@tonic-gate special_vertices[i].post_online_f; 10340Sstevel@tonic-gate (*vp)->gv_post_disable_f = 10350Sstevel@tonic-gate special_vertices[i].post_disable_f; 10360Sstevel@tonic-gate break; 10370Sstevel@tonic-gate } 10380Sstevel@tonic-gate } 10390Sstevel@tonic-gate 10400Sstevel@tonic-gate (*vp)->gv_restarter_id = -1; 10410Sstevel@tonic-gate (*vp)->gv_restarter_channel = 0; 10420Sstevel@tonic-gate 10430Sstevel@tonic-gate if (type == GVT_INST) { 10440Sstevel@tonic-gate char *sfmri; 10450Sstevel@tonic-gate graph_vertex_t *sv; 10460Sstevel@tonic-gate 10470Sstevel@tonic-gate sfmri = inst_fmri_to_svc_fmri(fmri); 10480Sstevel@tonic-gate sv = vertex_get_by_name(sfmri); 10490Sstevel@tonic-gate if (sv == NULL) { 10500Sstevel@tonic-gate r = graph_insert_vertex_unconfigured(sfmri, GVT_SVC, 0, 10510Sstevel@tonic-gate 0, &sv); 10520Sstevel@tonic-gate assert(r == 0); 10530Sstevel@tonic-gate } 10540Sstevel@tonic-gate startd_free(sfmri, max_scf_fmri_size); 10550Sstevel@tonic-gate 10560Sstevel@tonic-gate graph_add_edge(sv, *vp); 10570Sstevel@tonic-gate } 10580Sstevel@tonic-gate 10590Sstevel@tonic-gate /* 10600Sstevel@tonic-gate * If this vertex is in the subgraph, mark it as so, for both 10610Sstevel@tonic-gate * GVT_INST and GVT_SERVICE verteces. 10620Sstevel@tonic-gate * A GVT_SERVICE vertex can only be in the subgraph if another instance 10630Sstevel@tonic-gate * depends on it, in which case it's already been added to the graph 10640Sstevel@tonic-gate * and marked as in the subgraph (by refresh_vertex()). If a 10650Sstevel@tonic-gate * GVT_SERVICE vertex was freshly added (by the code above), it means 10660Sstevel@tonic-gate * that it has no dependents, and cannot be in the subgraph. 10670Sstevel@tonic-gate * Regardless of this, we still check that gv_flags includes 10680Sstevel@tonic-gate * GV_INSUBGRAPH in the event that future behavior causes the above 10690Sstevel@tonic-gate * code to add a GVT_SERVICE vertex which should be in the subgraph. 10700Sstevel@tonic-gate */ 10710Sstevel@tonic-gate 10720Sstevel@tonic-gate (*vp)->gv_flags |= (should_be_in_subgraph(*vp)? GV_INSUBGRAPH : 0); 10730Sstevel@tonic-gate 10740Sstevel@tonic-gate return (0); 10750Sstevel@tonic-gate } 10760Sstevel@tonic-gate 10770Sstevel@tonic-gate /* 10780Sstevel@tonic-gate * Returns 0 on success or ELOOP if the dependency would create a cycle. 10790Sstevel@tonic-gate */ 10800Sstevel@tonic-gate static int 10810Sstevel@tonic-gate graph_insert_dependency(graph_vertex_t *fv, graph_vertex_t *tv, int **pathp) 10820Sstevel@tonic-gate { 10830Sstevel@tonic-gate hrtime_t now; 10840Sstevel@tonic-gate 10850Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 10860Sstevel@tonic-gate 10870Sstevel@tonic-gate /* cycle detection */ 10880Sstevel@tonic-gate now = gethrtime(); 10890Sstevel@tonic-gate 10900Sstevel@tonic-gate /* Don't follow exclusions. */ 10910Sstevel@tonic-gate if (!(fv->gv_type == GVT_GROUP && 10920Sstevel@tonic-gate fv->gv_depgroup == DEPGRP_EXCLUDE_ALL)) { 10930Sstevel@tonic-gate *pathp = is_path_to(tv, fv); 10940Sstevel@tonic-gate if (*pathp) 10950Sstevel@tonic-gate return (ELOOP); 10960Sstevel@tonic-gate } 10970Sstevel@tonic-gate 10980Sstevel@tonic-gate dep_cycle_ns += gethrtime() - now; 10990Sstevel@tonic-gate ++dep_inserts; 11000Sstevel@tonic-gate now = gethrtime(); 11010Sstevel@tonic-gate 11020Sstevel@tonic-gate graph_add_edge(fv, tv); 11030Sstevel@tonic-gate 11040Sstevel@tonic-gate dep_insert_ns += gethrtime() - now; 11050Sstevel@tonic-gate 11060Sstevel@tonic-gate /* Check if the dependency adds the "to" vertex to the subgraph */ 11070Sstevel@tonic-gate tv->gv_flags |= (should_be_in_subgraph(tv) ? GV_INSUBGRAPH : 0); 11080Sstevel@tonic-gate 11090Sstevel@tonic-gate return (0); 11100Sstevel@tonic-gate } 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate static int 11130Sstevel@tonic-gate inst_running(graph_vertex_t *v) 11140Sstevel@tonic-gate { 11150Sstevel@tonic-gate assert(v->gv_type == GVT_INST); 11160Sstevel@tonic-gate 11170Sstevel@tonic-gate if (v->gv_state == RESTARTER_STATE_ONLINE || 11180Sstevel@tonic-gate v->gv_state == RESTARTER_STATE_DEGRADED) 11190Sstevel@tonic-gate return (1); 11200Sstevel@tonic-gate 11210Sstevel@tonic-gate return (0); 11220Sstevel@tonic-gate } 11230Sstevel@tonic-gate 11240Sstevel@tonic-gate /* 11250Sstevel@tonic-gate * The dependency evaluation functions return 11260Sstevel@tonic-gate * 1 - dependency satisfied 11270Sstevel@tonic-gate * 0 - dependency unsatisfied 11280Sstevel@tonic-gate * -1 - dependency unsatisfiable (without administrator intervention) 11290Sstevel@tonic-gate * 11300Sstevel@tonic-gate * The functions also take a boolean satbility argument. When true, the 11310Sstevel@tonic-gate * functions may recurse in order to determine satisfiability. 11320Sstevel@tonic-gate */ 11330Sstevel@tonic-gate static int require_any_satisfied(graph_vertex_t *, boolean_t); 11340Sstevel@tonic-gate static int dependency_satisfied(graph_vertex_t *, boolean_t); 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate /* 11370Sstevel@tonic-gate * A require_all dependency is unsatisfied if any elements are unsatisfied. It 11380Sstevel@tonic-gate * is unsatisfiable if any elements are unsatisfiable. 11390Sstevel@tonic-gate */ 11400Sstevel@tonic-gate static int 11410Sstevel@tonic-gate require_all_satisfied(graph_vertex_t *groupv, boolean_t satbility) 11420Sstevel@tonic-gate { 11430Sstevel@tonic-gate graph_edge_t *edge; 11440Sstevel@tonic-gate int i; 11450Sstevel@tonic-gate boolean_t any_unsatisfied; 11460Sstevel@tonic-gate 11470Sstevel@tonic-gate if (uu_list_numnodes(groupv->gv_dependencies) == 0) 11480Sstevel@tonic-gate return (1); 11490Sstevel@tonic-gate 11500Sstevel@tonic-gate any_unsatisfied = B_FALSE; 11510Sstevel@tonic-gate 11520Sstevel@tonic-gate for (edge = uu_list_first(groupv->gv_dependencies); 11530Sstevel@tonic-gate edge != NULL; 11540Sstevel@tonic-gate edge = uu_list_next(groupv->gv_dependencies, edge)) { 11550Sstevel@tonic-gate i = dependency_satisfied(edge->ge_vertex, satbility); 11560Sstevel@tonic-gate if (i == 1) 11570Sstevel@tonic-gate continue; 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate log_framework(LOG_DEBUG, 11600Sstevel@tonic-gate "require_all(%s): %s is unsatisfi%s.\n", groupv->gv_name, 11610Sstevel@tonic-gate edge->ge_vertex->gv_name, i == 0 ? "ed" : "able"); 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate if (!satbility) 11640Sstevel@tonic-gate return (0); 11650Sstevel@tonic-gate 11660Sstevel@tonic-gate if (i == -1) 11670Sstevel@tonic-gate return (-1); 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate any_unsatisfied = B_TRUE; 11700Sstevel@tonic-gate } 11710Sstevel@tonic-gate 11720Sstevel@tonic-gate return (any_unsatisfied ? 0 : 1); 11730Sstevel@tonic-gate } 11740Sstevel@tonic-gate 11750Sstevel@tonic-gate /* 11760Sstevel@tonic-gate * A require_any dependency is satisfied if any element is satisfied. It is 11770Sstevel@tonic-gate * satisfiable if any element is satisfiable. 11780Sstevel@tonic-gate */ 11790Sstevel@tonic-gate static int 11800Sstevel@tonic-gate require_any_satisfied(graph_vertex_t *groupv, boolean_t satbility) 11810Sstevel@tonic-gate { 11820Sstevel@tonic-gate graph_edge_t *edge; 11830Sstevel@tonic-gate int s; 11840Sstevel@tonic-gate boolean_t satisfiable; 11850Sstevel@tonic-gate 11860Sstevel@tonic-gate if (uu_list_numnodes(groupv->gv_dependencies) == 0) 11870Sstevel@tonic-gate return (1); 11880Sstevel@tonic-gate 11890Sstevel@tonic-gate satisfiable = B_FALSE; 11900Sstevel@tonic-gate 11910Sstevel@tonic-gate for (edge = uu_list_first(groupv->gv_dependencies); 11920Sstevel@tonic-gate edge != NULL; 11930Sstevel@tonic-gate edge = uu_list_next(groupv->gv_dependencies, edge)) { 11940Sstevel@tonic-gate s = dependency_satisfied(edge->ge_vertex, satbility); 11950Sstevel@tonic-gate 11960Sstevel@tonic-gate if (s == 1) 11970Sstevel@tonic-gate return (1); 11980Sstevel@tonic-gate 11990Sstevel@tonic-gate log_framework(LOG_DEBUG, 12000Sstevel@tonic-gate "require_any(%s): %s is unsatisfi%s.\n", 12010Sstevel@tonic-gate groupv->gv_name, edge->ge_vertex->gv_name, 12020Sstevel@tonic-gate s == 0 ? "ed" : "able"); 12030Sstevel@tonic-gate 12040Sstevel@tonic-gate if (satbility && s == 0) 12050Sstevel@tonic-gate satisfiable = B_TRUE; 12060Sstevel@tonic-gate } 12070Sstevel@tonic-gate 12080Sstevel@tonic-gate return (!satbility || satisfiable ? 0 : -1); 12090Sstevel@tonic-gate } 12100Sstevel@tonic-gate 12110Sstevel@tonic-gate /* 12120Sstevel@tonic-gate * An optional_all dependency only considers elements which are configured, 12130Sstevel@tonic-gate * enabled, and not in maintenance. If any are unsatisfied, then the dependency 12140Sstevel@tonic-gate * is unsatisfied. 12150Sstevel@tonic-gate * 12160Sstevel@tonic-gate * Offline dependencies which are waiting for a dependency to come online are 12170Sstevel@tonic-gate * unsatisfied. Offline dependences which cannot possibly come online 12180Sstevel@tonic-gate * (unsatisfiable) are always considered satisfied. 12190Sstevel@tonic-gate */ 12200Sstevel@tonic-gate static int 12210Sstevel@tonic-gate optional_all_satisfied(graph_vertex_t *groupv, boolean_t satbility) 12220Sstevel@tonic-gate { 12230Sstevel@tonic-gate graph_edge_t *edge; 12240Sstevel@tonic-gate graph_vertex_t *v; 12250Sstevel@tonic-gate boolean_t any_qualified; 12260Sstevel@tonic-gate boolean_t any_unsatisfied; 12270Sstevel@tonic-gate int i; 12280Sstevel@tonic-gate 12290Sstevel@tonic-gate any_qualified = B_FALSE; 12300Sstevel@tonic-gate any_unsatisfied = B_FALSE; 12310Sstevel@tonic-gate 12320Sstevel@tonic-gate for (edge = uu_list_first(groupv->gv_dependencies); 12330Sstevel@tonic-gate edge != NULL; 12340Sstevel@tonic-gate edge = uu_list_next(groupv->gv_dependencies, edge)) { 12350Sstevel@tonic-gate v = edge->ge_vertex; 12360Sstevel@tonic-gate 12370Sstevel@tonic-gate switch (v->gv_type) { 12380Sstevel@tonic-gate case GVT_INST: 12390Sstevel@tonic-gate /* Skip missing or disabled instances */ 12400Sstevel@tonic-gate if ((v->gv_flags & (GV_CONFIGURED | GV_ENABLED)) != 12410Sstevel@tonic-gate (GV_CONFIGURED | GV_ENABLED)) 12420Sstevel@tonic-gate continue; 12430Sstevel@tonic-gate 12440Sstevel@tonic-gate if (v->gv_state == RESTARTER_STATE_MAINT) 12450Sstevel@tonic-gate continue; 12460Sstevel@tonic-gate 12470Sstevel@tonic-gate any_qualified = B_TRUE; 12480Sstevel@tonic-gate if (v->gv_state == RESTARTER_STATE_OFFLINE) { 12490Sstevel@tonic-gate /* 12500Sstevel@tonic-gate * For offline dependencies, treat unsatisfiable 12510Sstevel@tonic-gate * as satisfied. 12520Sstevel@tonic-gate */ 12530Sstevel@tonic-gate i = dependency_satisfied(v, B_TRUE); 12540Sstevel@tonic-gate if (i == -1) 12550Sstevel@tonic-gate i = 1; 12560Sstevel@tonic-gate } else if (v->gv_state == RESTARTER_STATE_DISABLED) { 12570Sstevel@tonic-gate /* 12580Sstevel@tonic-gate * The service is enabled, but hasn't 12590Sstevel@tonic-gate * transitioned out of disabled yet. Treat it 12600Sstevel@tonic-gate * as unsatisfied (not unsatisfiable). 12610Sstevel@tonic-gate */ 12620Sstevel@tonic-gate i = 0; 12630Sstevel@tonic-gate } else { 12640Sstevel@tonic-gate i = dependency_satisfied(v, satbility); 12650Sstevel@tonic-gate } 12660Sstevel@tonic-gate break; 12670Sstevel@tonic-gate 12680Sstevel@tonic-gate case GVT_FILE: 12690Sstevel@tonic-gate any_qualified = B_TRUE; 12700Sstevel@tonic-gate i = dependency_satisfied(v, satbility); 12710Sstevel@tonic-gate 12720Sstevel@tonic-gate break; 12730Sstevel@tonic-gate 12740Sstevel@tonic-gate case GVT_SVC: { 12750Sstevel@tonic-gate boolean_t svc_any_qualified; 12760Sstevel@tonic-gate boolean_t svc_satisfied; 12770Sstevel@tonic-gate boolean_t svc_satisfiable; 12780Sstevel@tonic-gate graph_vertex_t *v2; 12790Sstevel@tonic-gate graph_edge_t *e2; 12800Sstevel@tonic-gate 12810Sstevel@tonic-gate svc_any_qualified = B_FALSE; 12820Sstevel@tonic-gate svc_satisfied = B_FALSE; 12830Sstevel@tonic-gate svc_satisfiable = B_FALSE; 12840Sstevel@tonic-gate 12850Sstevel@tonic-gate for (e2 = uu_list_first(v->gv_dependencies); 12860Sstevel@tonic-gate e2 != NULL; 12870Sstevel@tonic-gate e2 = uu_list_next(v->gv_dependencies, e2)) { 12880Sstevel@tonic-gate v2 = e2->ge_vertex; 12890Sstevel@tonic-gate assert(v2->gv_type == GVT_INST); 12900Sstevel@tonic-gate 12910Sstevel@tonic-gate if ((v2->gv_flags & 12920Sstevel@tonic-gate (GV_CONFIGURED | GV_ENABLED)) != 12930Sstevel@tonic-gate (GV_CONFIGURED | GV_ENABLED)) 12940Sstevel@tonic-gate continue; 12950Sstevel@tonic-gate 12960Sstevel@tonic-gate if (v2->gv_state == RESTARTER_STATE_MAINT) 12970Sstevel@tonic-gate continue; 12980Sstevel@tonic-gate 12990Sstevel@tonic-gate svc_any_qualified = B_TRUE; 13000Sstevel@tonic-gate 13010Sstevel@tonic-gate if (v2->gv_state == RESTARTER_STATE_OFFLINE) { 13020Sstevel@tonic-gate /* 13030Sstevel@tonic-gate * For offline dependencies, treat 13040Sstevel@tonic-gate * unsatisfiable as satisfied. 13050Sstevel@tonic-gate */ 13060Sstevel@tonic-gate i = dependency_satisfied(v2, B_TRUE); 13070Sstevel@tonic-gate if (i == -1) 13080Sstevel@tonic-gate i = 1; 13090Sstevel@tonic-gate } else if (v2->gv_state == 13100Sstevel@tonic-gate RESTARTER_STATE_DISABLED) { 13110Sstevel@tonic-gate i = 0; 13120Sstevel@tonic-gate } else { 13130Sstevel@tonic-gate i = dependency_satisfied(v2, satbility); 13140Sstevel@tonic-gate } 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate if (i == 1) { 13170Sstevel@tonic-gate svc_satisfied = B_TRUE; 13180Sstevel@tonic-gate break; 13190Sstevel@tonic-gate } 13200Sstevel@tonic-gate if (i == 0) 13210Sstevel@tonic-gate svc_satisfiable = B_TRUE; 13220Sstevel@tonic-gate } 13230Sstevel@tonic-gate 13240Sstevel@tonic-gate if (!svc_any_qualified) 13250Sstevel@tonic-gate continue; 13260Sstevel@tonic-gate any_qualified = B_TRUE; 13270Sstevel@tonic-gate if (svc_satisfied) { 13280Sstevel@tonic-gate i = 1; 13290Sstevel@tonic-gate } else if (svc_satisfiable) { 13300Sstevel@tonic-gate i = 0; 13310Sstevel@tonic-gate } else { 13320Sstevel@tonic-gate i = -1; 13330Sstevel@tonic-gate } 13340Sstevel@tonic-gate break; 13350Sstevel@tonic-gate } 13360Sstevel@tonic-gate 13370Sstevel@tonic-gate case GVT_GROUP: 13380Sstevel@tonic-gate default: 13390Sstevel@tonic-gate #ifndef NDEBUG 13400Sstevel@tonic-gate uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__, 13410Sstevel@tonic-gate __LINE__, v->gv_type); 13420Sstevel@tonic-gate #endif 13430Sstevel@tonic-gate abort(); 13440Sstevel@tonic-gate } 13450Sstevel@tonic-gate 13460Sstevel@tonic-gate if (i == 1) 13470Sstevel@tonic-gate continue; 13480Sstevel@tonic-gate 13490Sstevel@tonic-gate log_framework(LOG_DEBUG, 13500Sstevel@tonic-gate "optional_all(%s): %s is unsatisfi%s.\n", groupv->gv_name, 13510Sstevel@tonic-gate v->gv_name, i == 0 ? "ed" : "able"); 13520Sstevel@tonic-gate 13530Sstevel@tonic-gate if (!satbility) 13540Sstevel@tonic-gate return (0); 13550Sstevel@tonic-gate if (i == -1) 13560Sstevel@tonic-gate return (-1); 13570Sstevel@tonic-gate any_unsatisfied = B_TRUE; 13580Sstevel@tonic-gate } 13590Sstevel@tonic-gate 13600Sstevel@tonic-gate if (!any_qualified) 13610Sstevel@tonic-gate return (1); 13620Sstevel@tonic-gate 13630Sstevel@tonic-gate return (any_unsatisfied ? 0 : 1); 13640Sstevel@tonic-gate } 13650Sstevel@tonic-gate 13660Sstevel@tonic-gate /* 13670Sstevel@tonic-gate * An exclude_all dependency is unsatisfied if any non-service element is 13680Sstevel@tonic-gate * satisfied or any service instance which is configured, enabled, and not in 13690Sstevel@tonic-gate * maintenance is satisfied. Usually when unsatisfied, it is also 13700Sstevel@tonic-gate * unsatisfiable. 13710Sstevel@tonic-gate */ 13720Sstevel@tonic-gate #define LOG_EXCLUDE(u, v) \ 13730Sstevel@tonic-gate log_framework(LOG_DEBUG, "exclude_all(%s): %s is satisfied.\n", \ 13740Sstevel@tonic-gate (u)->gv_name, (v)->gv_name) 13750Sstevel@tonic-gate 13760Sstevel@tonic-gate /* ARGSUSED */ 13770Sstevel@tonic-gate static int 13780Sstevel@tonic-gate exclude_all_satisfied(graph_vertex_t *groupv, boolean_t satbility) 13790Sstevel@tonic-gate { 13800Sstevel@tonic-gate graph_edge_t *edge, *e2; 13810Sstevel@tonic-gate graph_vertex_t *v, *v2; 13820Sstevel@tonic-gate 13830Sstevel@tonic-gate for (edge = uu_list_first(groupv->gv_dependencies); 13840Sstevel@tonic-gate edge != NULL; 13850Sstevel@tonic-gate edge = uu_list_next(groupv->gv_dependencies, edge)) { 13860Sstevel@tonic-gate v = edge->ge_vertex; 13870Sstevel@tonic-gate 13880Sstevel@tonic-gate switch (v->gv_type) { 13890Sstevel@tonic-gate case GVT_INST: 13900Sstevel@tonic-gate if ((v->gv_flags & GV_CONFIGURED) == 0) 13910Sstevel@tonic-gate continue; 13920Sstevel@tonic-gate 13930Sstevel@tonic-gate switch (v->gv_state) { 13940Sstevel@tonic-gate case RESTARTER_STATE_ONLINE: 13950Sstevel@tonic-gate case RESTARTER_STATE_DEGRADED: 13960Sstevel@tonic-gate LOG_EXCLUDE(groupv, v); 13970Sstevel@tonic-gate return (v->gv_flags & GV_ENABLED ? -1 : 0); 13980Sstevel@tonic-gate 13990Sstevel@tonic-gate case RESTARTER_STATE_OFFLINE: 14000Sstevel@tonic-gate case RESTARTER_STATE_UNINIT: 14010Sstevel@tonic-gate LOG_EXCLUDE(groupv, v); 14020Sstevel@tonic-gate return (0); 14030Sstevel@tonic-gate 14040Sstevel@tonic-gate case RESTARTER_STATE_DISABLED: 14050Sstevel@tonic-gate case RESTARTER_STATE_MAINT: 14060Sstevel@tonic-gate continue; 14070Sstevel@tonic-gate 14080Sstevel@tonic-gate default: 14090Sstevel@tonic-gate #ifndef NDEBUG 14100Sstevel@tonic-gate uu_warn("%s:%d: Unexpected vertex state %d.\n", 14110Sstevel@tonic-gate __FILE__, __LINE__, v->gv_state); 14120Sstevel@tonic-gate #endif 14130Sstevel@tonic-gate abort(); 14140Sstevel@tonic-gate } 14150Sstevel@tonic-gate /* NOTREACHED */ 14160Sstevel@tonic-gate 14170Sstevel@tonic-gate case GVT_SVC: 14180Sstevel@tonic-gate break; 14190Sstevel@tonic-gate 14200Sstevel@tonic-gate case GVT_FILE: 14210Sstevel@tonic-gate if (!file_ready(v)) 14220Sstevel@tonic-gate continue; 14230Sstevel@tonic-gate LOG_EXCLUDE(groupv, v); 14240Sstevel@tonic-gate return (-1); 14250Sstevel@tonic-gate 14260Sstevel@tonic-gate case GVT_GROUP: 14270Sstevel@tonic-gate default: 14280Sstevel@tonic-gate #ifndef NDEBUG 14290Sstevel@tonic-gate uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__, 14300Sstevel@tonic-gate __LINE__, v->gv_type); 14310Sstevel@tonic-gate #endif 14320Sstevel@tonic-gate abort(); 14330Sstevel@tonic-gate } 14340Sstevel@tonic-gate 14350Sstevel@tonic-gate /* v represents a service */ 14360Sstevel@tonic-gate if (uu_list_numnodes(v->gv_dependencies) == 0) 14370Sstevel@tonic-gate continue; 14380Sstevel@tonic-gate 14390Sstevel@tonic-gate for (e2 = uu_list_first(v->gv_dependencies); 14400Sstevel@tonic-gate e2 != NULL; 14410Sstevel@tonic-gate e2 = uu_list_next(v->gv_dependencies, e2)) { 14420Sstevel@tonic-gate v2 = e2->ge_vertex; 14430Sstevel@tonic-gate assert(v2->gv_type == GVT_INST); 14440Sstevel@tonic-gate 14450Sstevel@tonic-gate if ((v2->gv_flags & GV_CONFIGURED) == 0) 14460Sstevel@tonic-gate continue; 14470Sstevel@tonic-gate 14480Sstevel@tonic-gate switch (v2->gv_state) { 14490Sstevel@tonic-gate case RESTARTER_STATE_ONLINE: 14500Sstevel@tonic-gate case RESTARTER_STATE_DEGRADED: 14510Sstevel@tonic-gate LOG_EXCLUDE(groupv, v2); 14520Sstevel@tonic-gate return (v2->gv_flags & GV_ENABLED ? -1 : 0); 14530Sstevel@tonic-gate 14540Sstevel@tonic-gate case RESTARTER_STATE_OFFLINE: 14550Sstevel@tonic-gate case RESTARTER_STATE_UNINIT: 14560Sstevel@tonic-gate LOG_EXCLUDE(groupv, v2); 14570Sstevel@tonic-gate return (0); 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate case RESTARTER_STATE_DISABLED: 14600Sstevel@tonic-gate case RESTARTER_STATE_MAINT: 14610Sstevel@tonic-gate continue; 14620Sstevel@tonic-gate 14630Sstevel@tonic-gate default: 14640Sstevel@tonic-gate #ifndef NDEBUG 14650Sstevel@tonic-gate uu_warn("%s:%d: Unexpected vertex type %d.\n", 14660Sstevel@tonic-gate __FILE__, __LINE__, v2->gv_type); 14670Sstevel@tonic-gate #endif 14680Sstevel@tonic-gate abort(); 14690Sstevel@tonic-gate } 14700Sstevel@tonic-gate } 14710Sstevel@tonic-gate } 14720Sstevel@tonic-gate 14730Sstevel@tonic-gate return (1); 14740Sstevel@tonic-gate } 14750Sstevel@tonic-gate 14760Sstevel@tonic-gate /* 14770Sstevel@tonic-gate * int instance_satisfied() 14780Sstevel@tonic-gate * Determine if all the dependencies are satisfied for the supplied instance 14790Sstevel@tonic-gate * vertex. Return 1 if they are, 0 if they aren't, and -1 if they won't be 14800Sstevel@tonic-gate * without administrator intervention. 14810Sstevel@tonic-gate */ 14820Sstevel@tonic-gate static int 14830Sstevel@tonic-gate instance_satisfied(graph_vertex_t *v, boolean_t satbility) 14840Sstevel@tonic-gate { 14850Sstevel@tonic-gate assert(v->gv_type == GVT_INST); 14860Sstevel@tonic-gate assert(!inst_running(v)); 14870Sstevel@tonic-gate 14880Sstevel@tonic-gate return (require_all_satisfied(v, satbility)); 14890Sstevel@tonic-gate } 14900Sstevel@tonic-gate 14910Sstevel@tonic-gate /* 14920Sstevel@tonic-gate * Decide whether v can satisfy a dependency. v can either be a child of 14930Sstevel@tonic-gate * a group vertex, or of an instance vertex. 14940Sstevel@tonic-gate */ 14950Sstevel@tonic-gate static int 14960Sstevel@tonic-gate dependency_satisfied(graph_vertex_t *v, boolean_t satbility) 14970Sstevel@tonic-gate { 14980Sstevel@tonic-gate switch (v->gv_type) { 14990Sstevel@tonic-gate case GVT_INST: 15000Sstevel@tonic-gate if ((v->gv_flags & GV_CONFIGURED) == 0) 15010Sstevel@tonic-gate return (-1); 15020Sstevel@tonic-gate 15030Sstevel@tonic-gate switch (v->gv_state) { 15040Sstevel@tonic-gate case RESTARTER_STATE_ONLINE: 15050Sstevel@tonic-gate case RESTARTER_STATE_DEGRADED: 15060Sstevel@tonic-gate return (1); 15070Sstevel@tonic-gate 15080Sstevel@tonic-gate case RESTARTER_STATE_OFFLINE: 15090Sstevel@tonic-gate if (!satbility) 15100Sstevel@tonic-gate return (0); 15110Sstevel@tonic-gate return (instance_satisfied(v, satbility) != -1 ? 15120Sstevel@tonic-gate 0 : -1); 15130Sstevel@tonic-gate 15140Sstevel@tonic-gate case RESTARTER_STATE_DISABLED: 15150Sstevel@tonic-gate case RESTARTER_STATE_MAINT: 15160Sstevel@tonic-gate return (-1); 15170Sstevel@tonic-gate 15180Sstevel@tonic-gate case RESTARTER_STATE_UNINIT: 15190Sstevel@tonic-gate return (0); 15200Sstevel@tonic-gate 15210Sstevel@tonic-gate default: 15220Sstevel@tonic-gate #ifndef NDEBUG 15230Sstevel@tonic-gate uu_warn("%s:%d: Unexpected vertex state %d.\n", 15240Sstevel@tonic-gate __FILE__, __LINE__, v->gv_state); 15250Sstevel@tonic-gate #endif 15260Sstevel@tonic-gate abort(); 15270Sstevel@tonic-gate /* NOTREACHED */ 15280Sstevel@tonic-gate } 15290Sstevel@tonic-gate 15300Sstevel@tonic-gate case GVT_SVC: 15310Sstevel@tonic-gate if (uu_list_numnodes(v->gv_dependencies) == 0) 15320Sstevel@tonic-gate return (-1); 15330Sstevel@tonic-gate return (require_any_satisfied(v, satbility)); 15340Sstevel@tonic-gate 15350Sstevel@tonic-gate case GVT_FILE: 15360Sstevel@tonic-gate /* i.e., we assume files will not be automatically generated */ 15370Sstevel@tonic-gate return (file_ready(v) ? 1 : -1); 15380Sstevel@tonic-gate 15390Sstevel@tonic-gate case GVT_GROUP: 15400Sstevel@tonic-gate break; 15410Sstevel@tonic-gate 15420Sstevel@tonic-gate default: 15430Sstevel@tonic-gate #ifndef NDEBUG 15440Sstevel@tonic-gate uu_warn("%s:%d: Unexpected node type %d.\n", __FILE__, __LINE__, 15450Sstevel@tonic-gate v->gv_type); 15460Sstevel@tonic-gate #endif 15470Sstevel@tonic-gate abort(); 15480Sstevel@tonic-gate /* NOTREACHED */ 15490Sstevel@tonic-gate } 15500Sstevel@tonic-gate 15510Sstevel@tonic-gate switch (v->gv_depgroup) { 15520Sstevel@tonic-gate case DEPGRP_REQUIRE_ANY: 15530Sstevel@tonic-gate return (require_any_satisfied(v, satbility)); 15540Sstevel@tonic-gate 15550Sstevel@tonic-gate case DEPGRP_REQUIRE_ALL: 15560Sstevel@tonic-gate return (require_all_satisfied(v, satbility)); 15570Sstevel@tonic-gate 15580Sstevel@tonic-gate case DEPGRP_OPTIONAL_ALL: 15590Sstevel@tonic-gate return (optional_all_satisfied(v, satbility)); 15600Sstevel@tonic-gate 15610Sstevel@tonic-gate case DEPGRP_EXCLUDE_ALL: 15620Sstevel@tonic-gate return (exclude_all_satisfied(v, satbility)); 15630Sstevel@tonic-gate 15640Sstevel@tonic-gate default: 15650Sstevel@tonic-gate #ifndef NDEBUG 15660Sstevel@tonic-gate uu_warn("%s:%d: Unknown dependency grouping %d.\n", __FILE__, 15670Sstevel@tonic-gate __LINE__, v->gv_depgroup); 15680Sstevel@tonic-gate #endif 15690Sstevel@tonic-gate abort(); 15700Sstevel@tonic-gate } 15710Sstevel@tonic-gate } 15720Sstevel@tonic-gate 15730Sstevel@tonic-gate static void 15740Sstevel@tonic-gate start_if_satisfied(graph_vertex_t *v) 15750Sstevel@tonic-gate { 15760Sstevel@tonic-gate if (v->gv_state == RESTARTER_STATE_OFFLINE && 15770Sstevel@tonic-gate instance_satisfied(v, B_FALSE) == 1) { 15780Sstevel@tonic-gate if (v->gv_start_f == NULL) 15790Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_START); 15800Sstevel@tonic-gate else 15810Sstevel@tonic-gate v->gv_start_f(v); 15820Sstevel@tonic-gate } 15830Sstevel@tonic-gate } 15840Sstevel@tonic-gate 15850Sstevel@tonic-gate /* 15860Sstevel@tonic-gate * propagate_satbility() 15870Sstevel@tonic-gate * 15880Sstevel@tonic-gate * This function is used when the given vertex changes state in such a way that 15890Sstevel@tonic-gate * one of its dependents may become unsatisfiable. This happens when an 15900Sstevel@tonic-gate * instance transitions between offline -> online, or from !running -> 15910Sstevel@tonic-gate * maintenance, as well as when an instance is removed from the graph. 15920Sstevel@tonic-gate * 15930Sstevel@tonic-gate * We have to walk the all dependents, since optional_all dependencies several 15940Sstevel@tonic-gate * levels up could become (un)satisfied, instead of unsatisfiable. For example, 15950Sstevel@tonic-gate * 15960Sstevel@tonic-gate * +-----+ optional_all +-----+ require_all +-----+ 15970Sstevel@tonic-gate * | A |--------------->| B |-------------->| C | 15980Sstevel@tonic-gate * +-----+ +-----+ +-----+ 15990Sstevel@tonic-gate * 16000Sstevel@tonic-gate * offline -> maintenance 16010Sstevel@tonic-gate * 16020Sstevel@tonic-gate * If C goes into maintenance, it's not enough simply to check B. Because A has 16030Sstevel@tonic-gate * an optional dependency, what was previously an unsatisfiable situation is now 16040Sstevel@tonic-gate * satisfied (B will never come online, even though its state hasn't changed). 16050Sstevel@tonic-gate * 16060Sstevel@tonic-gate * Note that it's not necessary to continue examining dependents after reaching 16070Sstevel@tonic-gate * an optional_all dependency. It's not possible for an optional_all dependency 16080Sstevel@tonic-gate * to change satisfiability without also coming online, in which case we get a 16090Sstevel@tonic-gate * start event and propagation continues naturally. However, it does no harm to 16100Sstevel@tonic-gate * continue propagating satisfiability (as it is a relatively rare event), and 16110Sstevel@tonic-gate * keeps the walker code simple and generic. 16120Sstevel@tonic-gate */ 16130Sstevel@tonic-gate /*ARGSUSED*/ 16140Sstevel@tonic-gate static int 16150Sstevel@tonic-gate satbility_cb(graph_vertex_t *v, void *arg) 16160Sstevel@tonic-gate { 16170Sstevel@tonic-gate if (v->gv_type == GVT_INST) 16180Sstevel@tonic-gate start_if_satisfied(v); 16190Sstevel@tonic-gate 16200Sstevel@tonic-gate return (UU_WALK_NEXT); 16210Sstevel@tonic-gate } 16220Sstevel@tonic-gate 16230Sstevel@tonic-gate static void 16240Sstevel@tonic-gate propagate_satbility(graph_vertex_t *v) 16250Sstevel@tonic-gate { 16260Sstevel@tonic-gate graph_walk(v, WALK_DEPENDENTS, satbility_cb, NULL, NULL); 16270Sstevel@tonic-gate } 16280Sstevel@tonic-gate 16290Sstevel@tonic-gate static void propagate_stop(graph_vertex_t *, void *); 16300Sstevel@tonic-gate 16310Sstevel@tonic-gate /* ARGSUSED */ 16320Sstevel@tonic-gate static void 16330Sstevel@tonic-gate propagate_start(graph_vertex_t *v, void *arg) 16340Sstevel@tonic-gate { 16350Sstevel@tonic-gate switch (v->gv_type) { 16360Sstevel@tonic-gate case GVT_INST: 16370Sstevel@tonic-gate start_if_satisfied(v); 16380Sstevel@tonic-gate break; 16390Sstevel@tonic-gate 16400Sstevel@tonic-gate case GVT_GROUP: 16410Sstevel@tonic-gate if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) { 16420Sstevel@tonic-gate graph_walk_dependents(v, propagate_stop, 16430Sstevel@tonic-gate (void *)RERR_RESTART); 16440Sstevel@tonic-gate break; 16450Sstevel@tonic-gate } 16460Sstevel@tonic-gate /* FALLTHROUGH */ 16470Sstevel@tonic-gate 16480Sstevel@tonic-gate case GVT_SVC: 16490Sstevel@tonic-gate graph_walk_dependents(v, propagate_start, NULL); 16500Sstevel@tonic-gate break; 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate case GVT_FILE: 16530Sstevel@tonic-gate #ifndef NDEBUG 16540Sstevel@tonic-gate uu_warn("%s:%d: propagate_start() encountered GVT_FILE.\n", 16550Sstevel@tonic-gate __FILE__, __LINE__); 16560Sstevel@tonic-gate #endif 16570Sstevel@tonic-gate abort(); 16580Sstevel@tonic-gate /* NOTREACHED */ 16590Sstevel@tonic-gate 16600Sstevel@tonic-gate default: 16610Sstevel@tonic-gate #ifndef NDEBUG 16620Sstevel@tonic-gate uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__, 16630Sstevel@tonic-gate v->gv_type); 16640Sstevel@tonic-gate #endif 16650Sstevel@tonic-gate abort(); 16660Sstevel@tonic-gate } 16670Sstevel@tonic-gate } 16680Sstevel@tonic-gate 16690Sstevel@tonic-gate static void 16700Sstevel@tonic-gate propagate_stop(graph_vertex_t *v, void *arg) 16710Sstevel@tonic-gate { 16720Sstevel@tonic-gate graph_edge_t *e; 16730Sstevel@tonic-gate graph_vertex_t *svc; 16740Sstevel@tonic-gate restarter_error_t err = (restarter_error_t)arg; 16750Sstevel@tonic-gate 16760Sstevel@tonic-gate switch (v->gv_type) { 16770Sstevel@tonic-gate case GVT_INST: 16780Sstevel@tonic-gate /* Restarter */ 16790Sstevel@tonic-gate if (err > RERR_NONE && inst_running(v)) 16800Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_STOP); 16810Sstevel@tonic-gate break; 16820Sstevel@tonic-gate 16830Sstevel@tonic-gate case GVT_SVC: 16840Sstevel@tonic-gate graph_walk_dependents(v, propagate_stop, arg); 16850Sstevel@tonic-gate break; 16860Sstevel@tonic-gate 16870Sstevel@tonic-gate case GVT_FILE: 16880Sstevel@tonic-gate #ifndef NDEBUG 16890Sstevel@tonic-gate uu_warn("%s:%d: propagate_stop() encountered GVT_FILE.\n", 16900Sstevel@tonic-gate __FILE__, __LINE__); 16910Sstevel@tonic-gate #endif 16920Sstevel@tonic-gate abort(); 16930Sstevel@tonic-gate /* NOTREACHED */ 16940Sstevel@tonic-gate 16950Sstevel@tonic-gate case GVT_GROUP: 16960Sstevel@tonic-gate if (v->gv_depgroup == DEPGRP_EXCLUDE_ALL) { 16970Sstevel@tonic-gate graph_walk_dependents(v, propagate_start, NULL); 16980Sstevel@tonic-gate break; 16990Sstevel@tonic-gate } 17000Sstevel@tonic-gate 17010Sstevel@tonic-gate if (err == RERR_NONE || err > v->gv_restart) 17020Sstevel@tonic-gate break; 17030Sstevel@tonic-gate 17040Sstevel@tonic-gate assert(uu_list_numnodes(v->gv_dependents) == 1); 17050Sstevel@tonic-gate e = uu_list_first(v->gv_dependents); 17060Sstevel@tonic-gate svc = e->ge_vertex; 17070Sstevel@tonic-gate 17080Sstevel@tonic-gate if (inst_running(svc)) 17090Sstevel@tonic-gate vertex_send_event(svc, RESTARTER_EVENT_TYPE_STOP); 17100Sstevel@tonic-gate break; 17110Sstevel@tonic-gate 17120Sstevel@tonic-gate default: 17130Sstevel@tonic-gate #ifndef NDEBUG 17140Sstevel@tonic-gate uu_warn("%s:%d: Unknown vertex type %d.\n", __FILE__, __LINE__, 17150Sstevel@tonic-gate v->gv_type); 17160Sstevel@tonic-gate #endif 17170Sstevel@tonic-gate abort(); 17180Sstevel@tonic-gate } 17190Sstevel@tonic-gate } 17200Sstevel@tonic-gate 17210Sstevel@tonic-gate /* 17220Sstevel@tonic-gate * void graph_enable_by_vertex() 17230Sstevel@tonic-gate * If admin is non-zero, this is an administrative request for change 17240Sstevel@tonic-gate * of the enabled property. Thus, send the ADMIN_DISABLE rather than 17250Sstevel@tonic-gate * a plain DISABLE restarter event. 17260Sstevel@tonic-gate */ 17270Sstevel@tonic-gate static void 17280Sstevel@tonic-gate graph_enable_by_vertex(graph_vertex_t *vertex, int enable, int admin) 17290Sstevel@tonic-gate { 17300Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 17310Sstevel@tonic-gate assert((vertex->gv_flags & GV_CONFIGURED)); 17320Sstevel@tonic-gate 17330Sstevel@tonic-gate vertex->gv_flags = (vertex->gv_flags & ~GV_ENABLED) | 17340Sstevel@tonic-gate (enable ? GV_ENABLED : 0); 17350Sstevel@tonic-gate 17360Sstevel@tonic-gate if (enable) { 17370Sstevel@tonic-gate if (vertex->gv_state != RESTARTER_STATE_OFFLINE && 17380Sstevel@tonic-gate vertex->gv_state != RESTARTER_STATE_DEGRADED && 17390Sstevel@tonic-gate vertex->gv_state != RESTARTER_STATE_ONLINE) 17400Sstevel@tonic-gate vertex_send_event(vertex, RESTARTER_EVENT_TYPE_ENABLE); 17410Sstevel@tonic-gate } else { 17420Sstevel@tonic-gate if (vertex->gv_state != RESTARTER_STATE_DISABLED) { 17430Sstevel@tonic-gate if (admin) 17440Sstevel@tonic-gate vertex_send_event(vertex, 17450Sstevel@tonic-gate RESTARTER_EVENT_TYPE_ADMIN_DISABLE); 17460Sstevel@tonic-gate else 17470Sstevel@tonic-gate vertex_send_event(vertex, 17480Sstevel@tonic-gate RESTARTER_EVENT_TYPE_DISABLE); 17490Sstevel@tonic-gate } 17500Sstevel@tonic-gate } 17510Sstevel@tonic-gate 17520Sstevel@tonic-gate /* 17530Sstevel@tonic-gate * Wait for state update from restarter before sending _START or 17540Sstevel@tonic-gate * _STOP. 17550Sstevel@tonic-gate */ 17560Sstevel@tonic-gate } 17570Sstevel@tonic-gate 17580Sstevel@tonic-gate static int configure_vertex(graph_vertex_t *, scf_instance_t *); 17590Sstevel@tonic-gate 17600Sstevel@tonic-gate /* 17610Sstevel@tonic-gate * Set the restarter for v to fmri_arg. That is, make sure a vertex for 17620Sstevel@tonic-gate * fmri_arg exists, make v depend on it, and send _ADD_INSTANCE for v. If 17630Sstevel@tonic-gate * v is already configured and fmri_arg indicates the current restarter, do 17640Sstevel@tonic-gate * nothing. If v is configured and fmri_arg is a new restarter, delete v's 17650Sstevel@tonic-gate * dependency on the restarter, send _REMOVE_INSTANCE for v, and set the new 17660Sstevel@tonic-gate * restarter. Returns 0 on success, EINVAL if the FMRI is invalid, 17670Sstevel@tonic-gate * ECONNABORTED if the repository connection is broken, and ELOOP 17680Sstevel@tonic-gate * if the dependency would create a cycle. In the last case, *pathp will 17690Sstevel@tonic-gate * point to a -1-terminated array of ids which compose the path from v to 17700Sstevel@tonic-gate * restarter_fmri. 17710Sstevel@tonic-gate */ 17720Sstevel@tonic-gate int 17730Sstevel@tonic-gate graph_change_restarter(graph_vertex_t *v, const char *fmri_arg, scf_handle_t *h, 17740Sstevel@tonic-gate int **pathp) 17750Sstevel@tonic-gate { 17760Sstevel@tonic-gate char *restarter_fmri = NULL; 17770Sstevel@tonic-gate graph_vertex_t *rv; 17780Sstevel@tonic-gate int err; 17790Sstevel@tonic-gate int id; 17800Sstevel@tonic-gate 17810Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 17820Sstevel@tonic-gate 17830Sstevel@tonic-gate if (fmri_arg[0] != '\0') { 17840Sstevel@tonic-gate err = fmri_canonify(fmri_arg, &restarter_fmri, B_TRUE); 17850Sstevel@tonic-gate if (err != 0) { 17860Sstevel@tonic-gate assert(err == EINVAL); 17870Sstevel@tonic-gate return (err); 17880Sstevel@tonic-gate } 17890Sstevel@tonic-gate } 17900Sstevel@tonic-gate 17910Sstevel@tonic-gate if (restarter_fmri == NULL || 17920Sstevel@tonic-gate strcmp(restarter_fmri, SCF_SERVICE_STARTD) == 0) { 17930Sstevel@tonic-gate if (v->gv_flags & GV_CONFIGURED) { 17940Sstevel@tonic-gate if (v->gv_restarter_id == -1) { 17950Sstevel@tonic-gate if (restarter_fmri != NULL) 17960Sstevel@tonic-gate startd_free(restarter_fmri, 17970Sstevel@tonic-gate max_scf_fmri_size); 17980Sstevel@tonic-gate return (0); 17990Sstevel@tonic-gate } 18000Sstevel@tonic-gate 18010Sstevel@tonic-gate graph_unset_restarter(v); 18020Sstevel@tonic-gate } 18030Sstevel@tonic-gate 18040Sstevel@tonic-gate /* Master restarter, nothing to do. */ 18050Sstevel@tonic-gate v->gv_restarter_id = -1; 18060Sstevel@tonic-gate v->gv_restarter_channel = NULL; 18070Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE); 18080Sstevel@tonic-gate return (0); 18090Sstevel@tonic-gate } 18100Sstevel@tonic-gate 18110Sstevel@tonic-gate if (v->gv_flags & GV_CONFIGURED) { 18120Sstevel@tonic-gate id = dict_lookup_byname(restarter_fmri); 18130Sstevel@tonic-gate if (id != -1 && v->gv_restarter_id == id) { 18140Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_fmri_size); 18150Sstevel@tonic-gate return (0); 18160Sstevel@tonic-gate } 18170Sstevel@tonic-gate 18180Sstevel@tonic-gate graph_unset_restarter(v); 18190Sstevel@tonic-gate } 18200Sstevel@tonic-gate 18210Sstevel@tonic-gate err = graph_insert_vertex_unconfigured(restarter_fmri, GVT_INST, 0, 18220Sstevel@tonic-gate RERR_NONE, &rv); 18230Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_fmri_size); 18240Sstevel@tonic-gate assert(err == 0 || err == EEXIST); 18250Sstevel@tonic-gate 18260Sstevel@tonic-gate if (rv->gv_delegate_initialized == 0) { 18270Sstevel@tonic-gate rv->gv_delegate_channel = restarter_protocol_init_delegate( 18280Sstevel@tonic-gate rv->gv_name); 18290Sstevel@tonic-gate rv->gv_delegate_initialized = 1; 18300Sstevel@tonic-gate } 18310Sstevel@tonic-gate v->gv_restarter_id = rv->gv_id; 18320Sstevel@tonic-gate v->gv_restarter_channel = rv->gv_delegate_channel; 18330Sstevel@tonic-gate 18340Sstevel@tonic-gate err = graph_insert_dependency(v, rv, pathp); 18350Sstevel@tonic-gate if (err != 0) { 18360Sstevel@tonic-gate assert(err == ELOOP); 18370Sstevel@tonic-gate return (ELOOP); 18380Sstevel@tonic-gate } 18390Sstevel@tonic-gate 18400Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_ADD_INSTANCE); 18410Sstevel@tonic-gate 18420Sstevel@tonic-gate if (!(rv->gv_flags & GV_CONFIGURED)) { 18430Sstevel@tonic-gate scf_instance_t *inst; 18440Sstevel@tonic-gate 18450Sstevel@tonic-gate err = libscf_fmri_get_instance(h, rv->gv_name, &inst); 18460Sstevel@tonic-gate switch (err) { 18470Sstevel@tonic-gate case 0: 18480Sstevel@tonic-gate err = configure_vertex(rv, inst); 18490Sstevel@tonic-gate scf_instance_destroy(inst); 18500Sstevel@tonic-gate switch (err) { 18510Sstevel@tonic-gate case 0: 18520Sstevel@tonic-gate case ECANCELED: 18530Sstevel@tonic-gate break; 18540Sstevel@tonic-gate 18550Sstevel@tonic-gate case ECONNABORTED: 18560Sstevel@tonic-gate return (ECONNABORTED); 18570Sstevel@tonic-gate 18580Sstevel@tonic-gate default: 18590Sstevel@tonic-gate bad_error("configure_vertex", err); 18600Sstevel@tonic-gate } 18610Sstevel@tonic-gate break; 18620Sstevel@tonic-gate 18630Sstevel@tonic-gate case ECONNABORTED: 18640Sstevel@tonic-gate return (ECONNABORTED); 18650Sstevel@tonic-gate 18660Sstevel@tonic-gate case ENOENT: 18670Sstevel@tonic-gate break; 18680Sstevel@tonic-gate 18690Sstevel@tonic-gate case ENOTSUP: 18700Sstevel@tonic-gate /* 18710Sstevel@tonic-gate * The fmri doesn't specify an instance - translate 18720Sstevel@tonic-gate * to EINVAL. 18730Sstevel@tonic-gate */ 18740Sstevel@tonic-gate return (EINVAL); 18750Sstevel@tonic-gate 18760Sstevel@tonic-gate case EINVAL: 18770Sstevel@tonic-gate default: 18780Sstevel@tonic-gate bad_error("libscf_fmri_get_instance", err); 18790Sstevel@tonic-gate } 18800Sstevel@tonic-gate } 18810Sstevel@tonic-gate 18820Sstevel@tonic-gate return (0); 18830Sstevel@tonic-gate } 18840Sstevel@tonic-gate 18850Sstevel@tonic-gate 18860Sstevel@tonic-gate /* 18870Sstevel@tonic-gate * Add all of the instances of the service named by fmri to the graph. 18880Sstevel@tonic-gate * Returns 18890Sstevel@tonic-gate * 0 - success 18900Sstevel@tonic-gate * ENOENT - service indicated by fmri does not exist 18910Sstevel@tonic-gate * 18920Sstevel@tonic-gate * In both cases *reboundp will be B_TRUE if the handle was rebound, or B_FALSE 18930Sstevel@tonic-gate * otherwise. 18940Sstevel@tonic-gate */ 18950Sstevel@tonic-gate static int 18960Sstevel@tonic-gate add_service(const char *fmri, scf_handle_t *h, boolean_t *reboundp) 18970Sstevel@tonic-gate { 18980Sstevel@tonic-gate scf_service_t *svc; 18990Sstevel@tonic-gate scf_instance_t *inst; 19000Sstevel@tonic-gate scf_iter_t *iter; 19010Sstevel@tonic-gate char *inst_fmri; 19020Sstevel@tonic-gate int ret, r; 19030Sstevel@tonic-gate 19040Sstevel@tonic-gate *reboundp = B_FALSE; 19050Sstevel@tonic-gate 19060Sstevel@tonic-gate svc = safe_scf_service_create(h); 19070Sstevel@tonic-gate inst = safe_scf_instance_create(h); 19080Sstevel@tonic-gate iter = safe_scf_iter_create(h); 19090Sstevel@tonic-gate inst_fmri = startd_alloc(max_scf_fmri_size); 19100Sstevel@tonic-gate 19110Sstevel@tonic-gate rebound: 19120Sstevel@tonic-gate if (scf_handle_decode_fmri(h, fmri, NULL, svc, NULL, NULL, NULL, 19130Sstevel@tonic-gate SCF_DECODE_FMRI_EXACT) != 0) { 19140Sstevel@tonic-gate switch (scf_error()) { 19150Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 19160Sstevel@tonic-gate default: 19170Sstevel@tonic-gate libscf_handle_rebind(h); 19180Sstevel@tonic-gate *reboundp = B_TRUE; 19190Sstevel@tonic-gate goto rebound; 19200Sstevel@tonic-gate 19210Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 19220Sstevel@tonic-gate ret = ENOENT; 19230Sstevel@tonic-gate goto out; 19240Sstevel@tonic-gate 19250Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 19260Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED: 19270Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 19280Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 19290Sstevel@tonic-gate bad_error("scf_handle_decode_fmri", scf_error()); 19300Sstevel@tonic-gate } 19310Sstevel@tonic-gate } 19320Sstevel@tonic-gate 19330Sstevel@tonic-gate if (scf_iter_service_instances(iter, svc) != 0) { 19340Sstevel@tonic-gate switch (scf_error()) { 19350Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 19360Sstevel@tonic-gate default: 19370Sstevel@tonic-gate libscf_handle_rebind(h); 19380Sstevel@tonic-gate *reboundp = B_TRUE; 19390Sstevel@tonic-gate goto rebound; 19400Sstevel@tonic-gate 19410Sstevel@tonic-gate case SCF_ERROR_DELETED: 19420Sstevel@tonic-gate ret = ENOENT; 19430Sstevel@tonic-gate goto out; 19440Sstevel@tonic-gate 19450Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 19460Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 19470Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 19480Sstevel@tonic-gate bad_error("scf_iter_service_instances", scf_error()) 19490Sstevel@tonic-gate } 19500Sstevel@tonic-gate } 19510Sstevel@tonic-gate 19520Sstevel@tonic-gate for (;;) { 19530Sstevel@tonic-gate r = scf_iter_next_instance(iter, inst); 19540Sstevel@tonic-gate if (r == 0) 19550Sstevel@tonic-gate break; 19560Sstevel@tonic-gate if (r != 1) { 19570Sstevel@tonic-gate switch (scf_error()) { 19580Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 19590Sstevel@tonic-gate default: 19600Sstevel@tonic-gate libscf_handle_rebind(h); 19610Sstevel@tonic-gate *reboundp = B_TRUE; 19620Sstevel@tonic-gate goto rebound; 19630Sstevel@tonic-gate 19640Sstevel@tonic-gate case SCF_ERROR_DELETED: 19650Sstevel@tonic-gate ret = ENOENT; 19660Sstevel@tonic-gate goto out; 19670Sstevel@tonic-gate 19680Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 19690Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 19700Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 19710Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 19720Sstevel@tonic-gate bad_error("scf_iter_next_instance", 19730Sstevel@tonic-gate scf_error()); 19740Sstevel@tonic-gate } 19750Sstevel@tonic-gate } 19760Sstevel@tonic-gate 19770Sstevel@tonic-gate if (scf_instance_to_fmri(inst, inst_fmri, max_scf_fmri_size) < 19780Sstevel@tonic-gate 0) { 19790Sstevel@tonic-gate switch (scf_error()) { 19800Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 19810Sstevel@tonic-gate libscf_handle_rebind(h); 19820Sstevel@tonic-gate *reboundp = B_TRUE; 19830Sstevel@tonic-gate goto rebound; 19840Sstevel@tonic-gate 19850Sstevel@tonic-gate case SCF_ERROR_DELETED: 19860Sstevel@tonic-gate continue; 19870Sstevel@tonic-gate 19880Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 19890Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 19900Sstevel@tonic-gate bad_error("scf_instance_to_fmri", scf_error()); 19910Sstevel@tonic-gate } 19920Sstevel@tonic-gate } 19930Sstevel@tonic-gate 19940Sstevel@tonic-gate r = dgraph_add_instance(inst_fmri, inst, B_FALSE); 19950Sstevel@tonic-gate switch (r) { 19960Sstevel@tonic-gate case 0: 19970Sstevel@tonic-gate case ECANCELED: 19980Sstevel@tonic-gate break; 19990Sstevel@tonic-gate 20000Sstevel@tonic-gate case EEXIST: 20010Sstevel@tonic-gate continue; 20020Sstevel@tonic-gate 20030Sstevel@tonic-gate case ECONNABORTED: 20040Sstevel@tonic-gate libscf_handle_rebind(h); 20050Sstevel@tonic-gate *reboundp = B_TRUE; 20060Sstevel@tonic-gate goto rebound; 20070Sstevel@tonic-gate 20080Sstevel@tonic-gate case EINVAL: 20090Sstevel@tonic-gate default: 20100Sstevel@tonic-gate bad_error("dgraph_add_instance", r); 20110Sstevel@tonic-gate } 20120Sstevel@tonic-gate } 20130Sstevel@tonic-gate 20140Sstevel@tonic-gate ret = 0; 20150Sstevel@tonic-gate 20160Sstevel@tonic-gate out: 20170Sstevel@tonic-gate startd_free(inst_fmri, max_scf_fmri_size); 20180Sstevel@tonic-gate scf_iter_destroy(iter); 20190Sstevel@tonic-gate scf_instance_destroy(inst); 20200Sstevel@tonic-gate scf_service_destroy(svc); 20210Sstevel@tonic-gate return (ret); 20220Sstevel@tonic-gate } 20230Sstevel@tonic-gate 20240Sstevel@tonic-gate struct depfmri_info { 20250Sstevel@tonic-gate graph_vertex_t *v; /* GVT_GROUP vertex */ 20260Sstevel@tonic-gate gv_type_t type; /* type of dependency */ 20270Sstevel@tonic-gate const char *inst_fmri; /* FMRI of parental GVT_INST vert. */ 20280Sstevel@tonic-gate const char *pg_name; /* Name of dependency pg */ 20290Sstevel@tonic-gate scf_handle_t *h; 20300Sstevel@tonic-gate int err; /* return error code */ 20310Sstevel@tonic-gate int **pathp; /* return circular dependency path */ 20320Sstevel@tonic-gate }; 20330Sstevel@tonic-gate 20340Sstevel@tonic-gate /* 20350Sstevel@tonic-gate * Find or create a vertex for fmri and make info->v depend on it. 20360Sstevel@tonic-gate * Returns 20370Sstevel@tonic-gate * 0 - success 20380Sstevel@tonic-gate * nonzero - failure 20390Sstevel@tonic-gate * 20400Sstevel@tonic-gate * On failure, sets info->err to 20410Sstevel@tonic-gate * EINVAL - fmri is invalid 20420Sstevel@tonic-gate * fmri does not match info->type 20430Sstevel@tonic-gate * ELOOP - Adding the dependency creates a circular dependency. *info->pathp 20440Sstevel@tonic-gate * will point to an array of the ids of the members of the cycle. 20450Sstevel@tonic-gate * ECONNABORTED - repository connection was broken 20460Sstevel@tonic-gate * ECONNRESET - succeeded, but repository connection was reset 20470Sstevel@tonic-gate */ 20480Sstevel@tonic-gate static int 20490Sstevel@tonic-gate process_dependency_fmri(const char *fmri, struct depfmri_info *info) 20500Sstevel@tonic-gate { 20510Sstevel@tonic-gate int err; 20520Sstevel@tonic-gate graph_vertex_t *depgroup_v, *v; 20530Sstevel@tonic-gate char *fmri_copy, *cfmri; 20540Sstevel@tonic-gate size_t fmri_copy_sz; 20550Sstevel@tonic-gate const char *scope, *service, *instance, *pg; 20560Sstevel@tonic-gate scf_instance_t *inst; 20570Sstevel@tonic-gate boolean_t rebound; 20580Sstevel@tonic-gate 20590Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 20600Sstevel@tonic-gate 20610Sstevel@tonic-gate /* Get or create vertex for FMRI */ 20620Sstevel@tonic-gate depgroup_v = info->v; 20630Sstevel@tonic-gate 20640Sstevel@tonic-gate if (strncmp(fmri, "file:", sizeof ("file:") - 1) == 0) { 20650Sstevel@tonic-gate if (info->type != GVT_FILE) { 20660Sstevel@tonic-gate log_framework(LOG_NOTICE, 20670Sstevel@tonic-gate "FMRI \"%s\" is not allowed for the \"%s\" " 20680Sstevel@tonic-gate "dependency's type of instance %s.\n", fmri, 20690Sstevel@tonic-gate info->pg_name, info->inst_fmri); 20700Sstevel@tonic-gate return (info->err = EINVAL); 20710Sstevel@tonic-gate } 20720Sstevel@tonic-gate 20730Sstevel@tonic-gate err = graph_insert_vertex_unconfigured(fmri, info->type, 0, 20740Sstevel@tonic-gate RERR_NONE, &v); 20750Sstevel@tonic-gate switch (err) { 20760Sstevel@tonic-gate case 0: 20770Sstevel@tonic-gate break; 20780Sstevel@tonic-gate 20790Sstevel@tonic-gate case EEXIST: 20800Sstevel@tonic-gate assert(v->gv_type == GVT_FILE); 20810Sstevel@tonic-gate break; 20820Sstevel@tonic-gate 20830Sstevel@tonic-gate case EINVAL: /* prevented above */ 20840Sstevel@tonic-gate default: 20850Sstevel@tonic-gate bad_error("graph_insert_vertex_unconfigured", err); 20860Sstevel@tonic-gate } 20870Sstevel@tonic-gate } else { 20880Sstevel@tonic-gate if (info->type != GVT_INST) { 20890Sstevel@tonic-gate log_framework(LOG_NOTICE, 20900Sstevel@tonic-gate "FMRI \"%s\" is not allowed for the \"%s\" " 20910Sstevel@tonic-gate "dependency's type of instance %s.\n", fmri, 20920Sstevel@tonic-gate info->pg_name, info->inst_fmri); 20930Sstevel@tonic-gate return (info->err = EINVAL); 20940Sstevel@tonic-gate } 20950Sstevel@tonic-gate 20960Sstevel@tonic-gate /* 20970Sstevel@tonic-gate * We must canonify fmri & add a vertex for it. 20980Sstevel@tonic-gate */ 20990Sstevel@tonic-gate fmri_copy_sz = strlen(fmri) + 1; 21000Sstevel@tonic-gate fmri_copy = startd_alloc(fmri_copy_sz); 21010Sstevel@tonic-gate (void) strcpy(fmri_copy, fmri); 21020Sstevel@tonic-gate 21030Sstevel@tonic-gate /* Determine if the FMRI is a property group or instance */ 21040Sstevel@tonic-gate if (scf_parse_svc_fmri(fmri_copy, &scope, &service, 21050Sstevel@tonic-gate &instance, &pg, NULL) != 0) { 21060Sstevel@tonic-gate startd_free(fmri_copy, fmri_copy_sz); 21070Sstevel@tonic-gate log_framework(LOG_NOTICE, 21080Sstevel@tonic-gate "Dependency \"%s\" of %s has invalid FMRI " 21090Sstevel@tonic-gate "\"%s\".\n", info->pg_name, info->inst_fmri, 21100Sstevel@tonic-gate fmri); 21110Sstevel@tonic-gate return (info->err = EINVAL); 21120Sstevel@tonic-gate } 21130Sstevel@tonic-gate 21140Sstevel@tonic-gate if (service == NULL || pg != NULL) { 21150Sstevel@tonic-gate startd_free(fmri_copy, fmri_copy_sz); 21160Sstevel@tonic-gate log_framework(LOG_NOTICE, 21170Sstevel@tonic-gate "Dependency \"%s\" of %s does not designate a " 21180Sstevel@tonic-gate "service or instance.\n", info->pg_name, 21190Sstevel@tonic-gate info->inst_fmri); 21200Sstevel@tonic-gate return (info->err = EINVAL); 21210Sstevel@tonic-gate } 21220Sstevel@tonic-gate 21230Sstevel@tonic-gate if (scope == NULL || strcmp(scope, SCF_SCOPE_LOCAL) == 0) { 21240Sstevel@tonic-gate cfmri = uu_msprintf("svc:/%s%s%s", 21250Sstevel@tonic-gate service, instance ? ":" : "", instance ? instance : 21260Sstevel@tonic-gate ""); 21270Sstevel@tonic-gate } else { 21280Sstevel@tonic-gate cfmri = uu_msprintf("svc://%s/%s%s%s", 21290Sstevel@tonic-gate scope, service, instance ? ":" : "", instance ? 21300Sstevel@tonic-gate instance : ""); 21310Sstevel@tonic-gate } 21320Sstevel@tonic-gate 21330Sstevel@tonic-gate startd_free(fmri_copy, fmri_copy_sz); 21340Sstevel@tonic-gate 21350Sstevel@tonic-gate err = graph_insert_vertex_unconfigured(cfmri, instance ? 21360Sstevel@tonic-gate GVT_INST : GVT_SVC, instance ? 0 : DEPGRP_REQUIRE_ANY, 21370Sstevel@tonic-gate RERR_NONE, &v); 21380Sstevel@tonic-gate uu_free(cfmri); 21390Sstevel@tonic-gate switch (err) { 21400Sstevel@tonic-gate case 0: 21410Sstevel@tonic-gate break; 21420Sstevel@tonic-gate 21430Sstevel@tonic-gate case EEXIST: 21440Sstevel@tonic-gate /* Verify v. */ 21450Sstevel@tonic-gate if (instance != NULL) 21460Sstevel@tonic-gate assert(v->gv_type == GVT_INST); 21470Sstevel@tonic-gate else 21480Sstevel@tonic-gate assert(v->gv_type == GVT_SVC); 21490Sstevel@tonic-gate break; 21500Sstevel@tonic-gate 21510Sstevel@tonic-gate default: 21520Sstevel@tonic-gate bad_error("graph_insert_vertex_unconfigured", err); 21530Sstevel@tonic-gate } 21540Sstevel@tonic-gate } 21550Sstevel@tonic-gate 21560Sstevel@tonic-gate /* Add dependency from depgroup_v to new vertex */ 21570Sstevel@tonic-gate info->err = graph_insert_dependency(depgroup_v, v, info->pathp); 21580Sstevel@tonic-gate switch (info->err) { 21590Sstevel@tonic-gate case 0: 21600Sstevel@tonic-gate break; 21610Sstevel@tonic-gate 21620Sstevel@tonic-gate case ELOOP: 21630Sstevel@tonic-gate return (ELOOP); 21640Sstevel@tonic-gate 21650Sstevel@tonic-gate default: 21660Sstevel@tonic-gate bad_error("graph_insert_dependency", info->err); 21670Sstevel@tonic-gate } 21680Sstevel@tonic-gate 21690Sstevel@tonic-gate /* This must be after we insert the dependency, to avoid looping. */ 21700Sstevel@tonic-gate switch (v->gv_type) { 21710Sstevel@tonic-gate case GVT_INST: 21720Sstevel@tonic-gate if ((v->gv_flags & GV_CONFIGURED) != 0) 21730Sstevel@tonic-gate break; 21740Sstevel@tonic-gate 21750Sstevel@tonic-gate inst = safe_scf_instance_create(info->h); 21760Sstevel@tonic-gate 21770Sstevel@tonic-gate rebound = B_FALSE; 21780Sstevel@tonic-gate 21790Sstevel@tonic-gate rebound: 21800Sstevel@tonic-gate err = libscf_lookup_instance(v->gv_name, inst); 21810Sstevel@tonic-gate switch (err) { 21820Sstevel@tonic-gate case 0: 21830Sstevel@tonic-gate err = configure_vertex(v, inst); 21840Sstevel@tonic-gate switch (err) { 21850Sstevel@tonic-gate case 0: 21860Sstevel@tonic-gate case ECANCELED: 21870Sstevel@tonic-gate break; 21880Sstevel@tonic-gate 21890Sstevel@tonic-gate case ECONNABORTED: 21900Sstevel@tonic-gate libscf_handle_rebind(info->h); 21910Sstevel@tonic-gate rebound = B_TRUE; 21920Sstevel@tonic-gate goto rebound; 21930Sstevel@tonic-gate 21940Sstevel@tonic-gate default: 21950Sstevel@tonic-gate bad_error("configure_vertex", err); 21960Sstevel@tonic-gate } 21970Sstevel@tonic-gate break; 21980Sstevel@tonic-gate 21990Sstevel@tonic-gate case ENOENT: 22000Sstevel@tonic-gate break; 22010Sstevel@tonic-gate 22020Sstevel@tonic-gate case ECONNABORTED: 22030Sstevel@tonic-gate libscf_handle_rebind(info->h); 22040Sstevel@tonic-gate rebound = B_TRUE; 22050Sstevel@tonic-gate goto rebound; 22060Sstevel@tonic-gate 22070Sstevel@tonic-gate case EINVAL: 22080Sstevel@tonic-gate case ENOTSUP: 22090Sstevel@tonic-gate default: 22100Sstevel@tonic-gate bad_error("libscf_fmri_get_instance", err); 22110Sstevel@tonic-gate } 22120Sstevel@tonic-gate 22130Sstevel@tonic-gate scf_instance_destroy(inst); 22140Sstevel@tonic-gate 22150Sstevel@tonic-gate if (rebound) 22160Sstevel@tonic-gate return (info->err = ECONNRESET); 22170Sstevel@tonic-gate break; 22180Sstevel@tonic-gate 22190Sstevel@tonic-gate case GVT_SVC: 22200Sstevel@tonic-gate (void) add_service(v->gv_name, info->h, &rebound); 22210Sstevel@tonic-gate if (rebound) 22220Sstevel@tonic-gate return (info->err = ECONNRESET); 22230Sstevel@tonic-gate } 22240Sstevel@tonic-gate 22250Sstevel@tonic-gate return (0); 22260Sstevel@tonic-gate } 22270Sstevel@tonic-gate 22280Sstevel@tonic-gate struct deppg_info { 22290Sstevel@tonic-gate graph_vertex_t *v; /* GVT_INST vertex */ 22300Sstevel@tonic-gate int err; /* return error */ 22310Sstevel@tonic-gate int **pathp; /* return circular dependency path */ 22320Sstevel@tonic-gate }; 22330Sstevel@tonic-gate 22340Sstevel@tonic-gate /* 22350Sstevel@tonic-gate * Make info->v depend on a new GVT_GROUP node for this property group, 22360Sstevel@tonic-gate * and then call process_dependency_fmri() for the values of the entity 22370Sstevel@tonic-gate * property. Return 0 on success, or if something goes wrong return nonzero 22380Sstevel@tonic-gate * and set info->err to ECONNABORTED, EINVAL, or the error code returned by 22390Sstevel@tonic-gate * process_dependency_fmri(). 22400Sstevel@tonic-gate */ 22410Sstevel@tonic-gate static int 22420Sstevel@tonic-gate process_dependency_pg(scf_propertygroup_t *pg, struct deppg_info *info) 22430Sstevel@tonic-gate { 22440Sstevel@tonic-gate scf_handle_t *h; 22450Sstevel@tonic-gate depgroup_type_t deptype; 22460Sstevel@tonic-gate struct depfmri_info linfo; 22470Sstevel@tonic-gate char *fmri, *pg_name; 22480Sstevel@tonic-gate size_t fmri_sz; 22490Sstevel@tonic-gate graph_vertex_t *depgrp; 22500Sstevel@tonic-gate scf_property_t *prop; 22510Sstevel@tonic-gate int err; 22520Sstevel@tonic-gate int empty; 22530Sstevel@tonic-gate scf_error_t scferr; 22540Sstevel@tonic-gate ssize_t len; 22550Sstevel@tonic-gate 22560Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 22570Sstevel@tonic-gate 22580Sstevel@tonic-gate h = scf_pg_handle(pg); 22590Sstevel@tonic-gate 22600Sstevel@tonic-gate pg_name = startd_alloc(max_scf_name_size); 22610Sstevel@tonic-gate 22620Sstevel@tonic-gate len = scf_pg_get_name(pg, pg_name, max_scf_name_size); 22630Sstevel@tonic-gate if (len < 0) { 22640Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size); 22650Sstevel@tonic-gate switch (scf_error()) { 22660Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 22670Sstevel@tonic-gate default: 22680Sstevel@tonic-gate return (info->err = ECONNABORTED); 22690Sstevel@tonic-gate 22700Sstevel@tonic-gate case SCF_ERROR_DELETED: 22710Sstevel@tonic-gate return (info->err = 0); 22720Sstevel@tonic-gate 22730Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 22740Sstevel@tonic-gate bad_error("scf_pg_get_name", scf_error()); 22750Sstevel@tonic-gate } 22760Sstevel@tonic-gate } 22770Sstevel@tonic-gate 22780Sstevel@tonic-gate /* 22790Sstevel@tonic-gate * Skip over empty dependency groups. Since dependency property 22800Sstevel@tonic-gate * groups are updated atomically, they are either empty or 22810Sstevel@tonic-gate * fully populated. 22820Sstevel@tonic-gate */ 22830Sstevel@tonic-gate empty = depgroup_empty(h, pg); 22840Sstevel@tonic-gate if (empty < 0) { 22850Sstevel@tonic-gate log_error(LOG_INFO, 22860Sstevel@tonic-gate "Error reading dependency group \"%s\" of %s: %s\n", 22870Sstevel@tonic-gate pg_name, info->v->gv_name, scf_strerror(scf_error())); 22880Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size); 22890Sstevel@tonic-gate return (info->err = EINVAL); 22900Sstevel@tonic-gate 22910Sstevel@tonic-gate } else if (empty == 1) { 22920Sstevel@tonic-gate log_framework(LOG_DEBUG, 22930Sstevel@tonic-gate "Ignoring empty dependency group \"%s\" of %s\n", 22940Sstevel@tonic-gate pg_name, info->v->gv_name); 22950Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size); 22960Sstevel@tonic-gate return (info->err = 0); 22970Sstevel@tonic-gate } 22980Sstevel@tonic-gate 22990Sstevel@tonic-gate fmri_sz = strlen(info->v->gv_name) + 1 + len + 1; 23000Sstevel@tonic-gate fmri = startd_alloc(fmri_sz); 23010Sstevel@tonic-gate 23020Sstevel@tonic-gate (void) snprintf(fmri, max_scf_name_size, "%s>%s", info->v->gv_name, 23030Sstevel@tonic-gate pg_name); 23040Sstevel@tonic-gate 23050Sstevel@tonic-gate /* Validate the pg before modifying the graph */ 23060Sstevel@tonic-gate deptype = depgroup_read_grouping(h, pg); 23070Sstevel@tonic-gate if (deptype == DEPGRP_UNSUPPORTED) { 23080Sstevel@tonic-gate log_error(LOG_INFO, 23090Sstevel@tonic-gate "Dependency \"%s\" of %s has an unknown grouping value.\n", 23100Sstevel@tonic-gate pg_name, info->v->gv_name); 23110Sstevel@tonic-gate startd_free(fmri, fmri_sz); 23120Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size); 23130Sstevel@tonic-gate return (info->err = EINVAL); 23140Sstevel@tonic-gate } 23150Sstevel@tonic-gate 23160Sstevel@tonic-gate prop = safe_scf_property_create(h); 23170Sstevel@tonic-gate 23180Sstevel@tonic-gate if (scf_pg_get_property(pg, SCF_PROPERTY_ENTITIES, prop) != 0) { 23190Sstevel@tonic-gate scferr = scf_error(); 23200Sstevel@tonic-gate scf_property_destroy(prop); 23210Sstevel@tonic-gate if (scferr == SCF_ERROR_DELETED) { 23220Sstevel@tonic-gate startd_free(fmri, fmri_sz); 23230Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size); 23240Sstevel@tonic-gate return (info->err = 0); 23250Sstevel@tonic-gate } else if (scferr != SCF_ERROR_NOT_FOUND) { 23260Sstevel@tonic-gate startd_free(fmri, fmri_sz); 23270Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size); 23280Sstevel@tonic-gate return (info->err = ECONNABORTED); 23290Sstevel@tonic-gate } 23300Sstevel@tonic-gate 23310Sstevel@tonic-gate log_error(LOG_INFO, 23320Sstevel@tonic-gate "Dependency \"%s\" of %s is missing a \"%s\" property.\n", 23330Sstevel@tonic-gate pg_name, info->v->gv_name, SCF_PROPERTY_ENTITIES); 23340Sstevel@tonic-gate 23350Sstevel@tonic-gate startd_free(fmri, fmri_sz); 23360Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size); 23370Sstevel@tonic-gate 23380Sstevel@tonic-gate return (info->err = EINVAL); 23390Sstevel@tonic-gate } 23400Sstevel@tonic-gate 23410Sstevel@tonic-gate /* Create depgroup vertex for pg */ 23420Sstevel@tonic-gate err = graph_insert_vertex_unconfigured(fmri, GVT_GROUP, deptype, 23430Sstevel@tonic-gate depgroup_read_restart(h, pg), &depgrp); 23440Sstevel@tonic-gate assert(err == 0); 23450Sstevel@tonic-gate startd_free(fmri, fmri_sz); 23460Sstevel@tonic-gate 23470Sstevel@tonic-gate /* Add dependency from inst vertex to new vertex */ 23480Sstevel@tonic-gate err = graph_insert_dependency(info->v, depgrp, info->pathp); 23490Sstevel@tonic-gate /* ELOOP can't happen because this should be a new vertex */ 23500Sstevel@tonic-gate assert(err == 0); 23510Sstevel@tonic-gate 23520Sstevel@tonic-gate linfo.v = depgrp; 23530Sstevel@tonic-gate linfo.type = depgroup_read_scheme(h, pg); 23540Sstevel@tonic-gate linfo.inst_fmri = info->v->gv_name; 23550Sstevel@tonic-gate linfo.pg_name = pg_name; 23560Sstevel@tonic-gate linfo.h = h; 23570Sstevel@tonic-gate linfo.err = 0; 23580Sstevel@tonic-gate linfo.pathp = info->pathp; 23590Sstevel@tonic-gate err = walk_property_astrings(prop, (callback_t)process_dependency_fmri, 23600Sstevel@tonic-gate &linfo); 23610Sstevel@tonic-gate 23620Sstevel@tonic-gate scf_property_destroy(prop); 23630Sstevel@tonic-gate startd_free(pg_name, max_scf_name_size); 23640Sstevel@tonic-gate 23650Sstevel@tonic-gate switch (err) { 23660Sstevel@tonic-gate case 0: 23670Sstevel@tonic-gate case EINTR: 23680Sstevel@tonic-gate return (info->err = linfo.err); 23690Sstevel@tonic-gate 23700Sstevel@tonic-gate case ECONNABORTED: 23710Sstevel@tonic-gate case EINVAL: 23720Sstevel@tonic-gate return (info->err = err); 23730Sstevel@tonic-gate 23740Sstevel@tonic-gate case ECANCELED: 23750Sstevel@tonic-gate return (info->err = 0); 23760Sstevel@tonic-gate 23770Sstevel@tonic-gate case ECONNRESET: 23780Sstevel@tonic-gate return (info->err = ECONNABORTED); 23790Sstevel@tonic-gate 23800Sstevel@tonic-gate default: 23810Sstevel@tonic-gate bad_error("walk_property_astrings", err); 23820Sstevel@tonic-gate /* NOTREACHED */ 23830Sstevel@tonic-gate } 23840Sstevel@tonic-gate } 23850Sstevel@tonic-gate 23860Sstevel@tonic-gate /* 23870Sstevel@tonic-gate * Build the dependency info for v from the repository. Returns 0 on success, 23880Sstevel@tonic-gate * ECONNABORTED on repository disconnection, EINVAL if the repository 23890Sstevel@tonic-gate * configuration is invalid, and ELOOP if a dependency would cause a cycle. 23900Sstevel@tonic-gate * In the last case, *pathp will point to a -1-terminated array of ids which 23910Sstevel@tonic-gate * constitute the rest of the dependency cycle. 23920Sstevel@tonic-gate */ 23930Sstevel@tonic-gate static int 23940Sstevel@tonic-gate set_dependencies(graph_vertex_t *v, scf_instance_t *inst, int **pathp) 23950Sstevel@tonic-gate { 23960Sstevel@tonic-gate struct deppg_info info; 23970Sstevel@tonic-gate int err; 23980Sstevel@tonic-gate uint_t old_configured; 23990Sstevel@tonic-gate 24000Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 24010Sstevel@tonic-gate 24020Sstevel@tonic-gate /* 24030Sstevel@tonic-gate * Mark the vertex as configured during dependency insertion to avoid 24040Sstevel@tonic-gate * dependency cycles (which can appear in the graph if one of the 24050Sstevel@tonic-gate * vertices is an exclusion-group). 24060Sstevel@tonic-gate */ 24070Sstevel@tonic-gate old_configured = v->gv_flags & GV_CONFIGURED; 24080Sstevel@tonic-gate v->gv_flags |= GV_CONFIGURED; 24090Sstevel@tonic-gate 24100Sstevel@tonic-gate info.err = 0; 24110Sstevel@tonic-gate info.v = v; 24120Sstevel@tonic-gate info.pathp = pathp; 24130Sstevel@tonic-gate 24140Sstevel@tonic-gate err = walk_dependency_pgs(inst, (callback_t)process_dependency_pg, 24150Sstevel@tonic-gate &info); 24160Sstevel@tonic-gate 24170Sstevel@tonic-gate if (!old_configured) 24180Sstevel@tonic-gate v->gv_flags &= ~GV_CONFIGURED; 24190Sstevel@tonic-gate 24200Sstevel@tonic-gate switch (err) { 24210Sstevel@tonic-gate case 0: 24220Sstevel@tonic-gate case EINTR: 24230Sstevel@tonic-gate return (info.err); 24240Sstevel@tonic-gate 24250Sstevel@tonic-gate case ECONNABORTED: 24260Sstevel@tonic-gate return (ECONNABORTED); 24270Sstevel@tonic-gate 24280Sstevel@tonic-gate case ECANCELED: 24290Sstevel@tonic-gate /* Should get delete event, so return 0. */ 24300Sstevel@tonic-gate return (0); 24310Sstevel@tonic-gate 24320Sstevel@tonic-gate default: 24330Sstevel@tonic-gate bad_error("walk_dependency_pgs", err); 24340Sstevel@tonic-gate /* NOTREACHED */ 24350Sstevel@tonic-gate } 24360Sstevel@tonic-gate } 24370Sstevel@tonic-gate 24380Sstevel@tonic-gate 24390Sstevel@tonic-gate static void 24400Sstevel@tonic-gate handle_cycle(const char *fmri, int *path) 24410Sstevel@tonic-gate { 24420Sstevel@tonic-gate const char *cp; 24430Sstevel@tonic-gate size_t sz; 24440Sstevel@tonic-gate 24450Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 24460Sstevel@tonic-gate 24470Sstevel@tonic-gate path_to_str(path, (char **)&cp, &sz); 24480Sstevel@tonic-gate 24490Sstevel@tonic-gate log_error(LOG_ERR, "Putting service %s into maintenance " 24500Sstevel@tonic-gate "because it completes a dependency cycle:\n%s", fmri ? fmri : "?", 24510Sstevel@tonic-gate cp); 24520Sstevel@tonic-gate 24530Sstevel@tonic-gate startd_free((void *)cp, sz); 24540Sstevel@tonic-gate } 24550Sstevel@tonic-gate 24560Sstevel@tonic-gate /* 2457*1712Srm88369 * Increment the vertex's reference count to prevent the vertex removal 2458*1712Srm88369 * from the dgraph. 2459*1712Srm88369 */ 2460*1712Srm88369 static void 2461*1712Srm88369 vertex_ref(graph_vertex_t *v) 2462*1712Srm88369 { 2463*1712Srm88369 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 2464*1712Srm88369 2465*1712Srm88369 v->gv_refs++; 2466*1712Srm88369 } 2467*1712Srm88369 2468*1712Srm88369 /* 2469*1712Srm88369 * Decrement the vertex's reference count and remove the vertex from 2470*1712Srm88369 * the dgraph when possible. 2471*1712Srm88369 * 2472*1712Srm88369 * Return VERTEX_REMOVED when the vertex has been removed otherwise 2473*1712Srm88369 * return VERTEX_INUSE. 24740Sstevel@tonic-gate */ 24750Sstevel@tonic-gate static int 2476*1712Srm88369 vertex_unref(graph_vertex_t *v) 2477*1712Srm88369 { 2478*1712Srm88369 assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 2479*1712Srm88369 assert(v->gv_refs > 0); 2480*1712Srm88369 2481*1712Srm88369 v->gv_refs--; 2482*1712Srm88369 2483*1712Srm88369 return (free_if_unrefed(v)); 2484*1712Srm88369 } 2485*1712Srm88369 2486*1712Srm88369 /* 2487*1712Srm88369 * When run on the dependencies of a vertex, populates list with 2488*1712Srm88369 * graph_edge_t's which point to the service vertices or the instance 2489*1712Srm88369 * vertices (no GVT_GROUP nodes) on which the vertex depends. 2490*1712Srm88369 * 2491*1712Srm88369 * Increment the vertex's reference count once the vertex is inserted 2492*1712Srm88369 * in the list. The vertex won't be able to be deleted from the dgraph 2493*1712Srm88369 * while it is referenced. 2494*1712Srm88369 */ 2495*1712Srm88369 static int 2496*1712Srm88369 append_svcs_or_insts(graph_edge_t *e, uu_list_t *list) 24970Sstevel@tonic-gate { 24980Sstevel@tonic-gate graph_vertex_t *v = e->ge_vertex; 24990Sstevel@tonic-gate graph_edge_t *new; 25000Sstevel@tonic-gate int r; 25010Sstevel@tonic-gate 25020Sstevel@tonic-gate switch (v->gv_type) { 25030Sstevel@tonic-gate case GVT_INST: 25040Sstevel@tonic-gate case GVT_SVC: 25050Sstevel@tonic-gate break; 25060Sstevel@tonic-gate 25070Sstevel@tonic-gate case GVT_GROUP: 25080Sstevel@tonic-gate r = uu_list_walk(v->gv_dependencies, 2509*1712Srm88369 (uu_walk_fn_t *)append_svcs_or_insts, list, 0); 25100Sstevel@tonic-gate assert(r == 0); 25110Sstevel@tonic-gate return (UU_WALK_NEXT); 25120Sstevel@tonic-gate 25130Sstevel@tonic-gate case GVT_FILE: 25140Sstevel@tonic-gate return (UU_WALK_NEXT); 25150Sstevel@tonic-gate 25160Sstevel@tonic-gate default: 25170Sstevel@tonic-gate #ifndef NDEBUG 25180Sstevel@tonic-gate uu_warn("%s:%d: Unexpected vertex type %d.\n", __FILE__, 25190Sstevel@tonic-gate __LINE__, v->gv_type); 25200Sstevel@tonic-gate #endif 25210Sstevel@tonic-gate abort(); 25220Sstevel@tonic-gate } 25230Sstevel@tonic-gate 25240Sstevel@tonic-gate new = startd_alloc(sizeof (*new)); 25250Sstevel@tonic-gate new->ge_vertex = v; 25260Sstevel@tonic-gate uu_list_node_init(new, &new->ge_link, graph_edge_pool); 25270Sstevel@tonic-gate r = uu_list_insert_before(list, NULL, new); 25280Sstevel@tonic-gate assert(r == 0); 2529*1712Srm88369 2530*1712Srm88369 /* 2531*1712Srm88369 * Because we are inserting the vertex in a list, we don't want 2532*1712Srm88369 * the vertex to be freed while the list is in use. In order to 2533*1712Srm88369 * achieve that, increment the vertex's reference count. 2534*1712Srm88369 */ 2535*1712Srm88369 vertex_ref(v); 2536*1712Srm88369 25370Sstevel@tonic-gate return (UU_WALK_NEXT); 25380Sstevel@tonic-gate } 25390Sstevel@tonic-gate 25400Sstevel@tonic-gate static boolean_t 25410Sstevel@tonic-gate should_be_in_subgraph(graph_vertex_t *v) 25420Sstevel@tonic-gate { 25430Sstevel@tonic-gate graph_edge_t *e; 25440Sstevel@tonic-gate 25450Sstevel@tonic-gate if (v == milestone) 25460Sstevel@tonic-gate return (B_TRUE); 25470Sstevel@tonic-gate 25480Sstevel@tonic-gate /* 25490Sstevel@tonic-gate * v is in the subgraph if any of its dependents are in the subgraph. 25500Sstevel@tonic-gate * Except for EXCLUDE_ALL dependents. And OPTIONAL dependents only 25510Sstevel@tonic-gate * count if we're enabled. 25520Sstevel@tonic-gate */ 25530Sstevel@tonic-gate for (e = uu_list_first(v->gv_dependents); 25540Sstevel@tonic-gate e != NULL; 25550Sstevel@tonic-gate e = uu_list_next(v->gv_dependents, e)) { 25560Sstevel@tonic-gate graph_vertex_t *dv = e->ge_vertex; 25570Sstevel@tonic-gate 25580Sstevel@tonic-gate if (!(dv->gv_flags & GV_INSUBGRAPH)) 25590Sstevel@tonic-gate continue; 25600Sstevel@tonic-gate 25610Sstevel@tonic-gate /* 25620Sstevel@tonic-gate * Don't include instances that are optional and disabled. 25630Sstevel@tonic-gate */ 25640Sstevel@tonic-gate if (v->gv_type == GVT_INST && dv->gv_type == GVT_SVC) { 25650Sstevel@tonic-gate 25660Sstevel@tonic-gate int in = 0; 25670Sstevel@tonic-gate graph_edge_t *ee; 25680Sstevel@tonic-gate 25690Sstevel@tonic-gate for (ee = uu_list_first(dv->gv_dependents); 25700Sstevel@tonic-gate ee != NULL; 25710Sstevel@tonic-gate ee = uu_list_next(dv->gv_dependents, ee)) { 25720Sstevel@tonic-gate 25730Sstevel@tonic-gate graph_vertex_t *ddv = e->ge_vertex; 25740Sstevel@tonic-gate 25750Sstevel@tonic-gate if (ddv->gv_type == GVT_GROUP && 25760Sstevel@tonic-gate ddv->gv_depgroup == DEPGRP_EXCLUDE_ALL) 25770Sstevel@tonic-gate continue; 25780Sstevel@tonic-gate 25790Sstevel@tonic-gate if (ddv->gv_type == GVT_GROUP && 25800Sstevel@tonic-gate ddv->gv_depgroup == DEPGRP_OPTIONAL_ALL && 25810Sstevel@tonic-gate !(v->gv_flags & GV_ENBLD_NOOVR)) 25820Sstevel@tonic-gate continue; 25830Sstevel@tonic-gate 25840Sstevel@tonic-gate in = 1; 25850Sstevel@tonic-gate } 25860Sstevel@tonic-gate if (!in) 25870Sstevel@tonic-gate continue; 25880Sstevel@tonic-gate } 25890Sstevel@tonic-gate if (v->gv_type == GVT_INST && 25900Sstevel@tonic-gate dv->gv_type == GVT_GROUP && 25910Sstevel@tonic-gate dv->gv_depgroup == DEPGRP_OPTIONAL_ALL && 25920Sstevel@tonic-gate !(v->gv_flags & GV_ENBLD_NOOVR)) 25930Sstevel@tonic-gate continue; 25940Sstevel@tonic-gate 25950Sstevel@tonic-gate /* Don't include excluded services and instances */ 25960Sstevel@tonic-gate if (dv->gv_type == GVT_GROUP && 25970Sstevel@tonic-gate dv->gv_depgroup == DEPGRP_EXCLUDE_ALL) 25980Sstevel@tonic-gate continue; 25990Sstevel@tonic-gate 26000Sstevel@tonic-gate return (B_TRUE); 26010Sstevel@tonic-gate } 26020Sstevel@tonic-gate 26030Sstevel@tonic-gate return (B_FALSE); 26040Sstevel@tonic-gate } 26050Sstevel@tonic-gate 26060Sstevel@tonic-gate /* 26070Sstevel@tonic-gate * Ensures that GV_INSUBGRAPH is set properly for v and its descendents. If 26080Sstevel@tonic-gate * any bits change, manipulate the repository appropriately. Returns 0 or 26090Sstevel@tonic-gate * ECONNABORTED. 26100Sstevel@tonic-gate */ 26110Sstevel@tonic-gate static int 26120Sstevel@tonic-gate eval_subgraph(graph_vertex_t *v, scf_handle_t *h) 26130Sstevel@tonic-gate { 26140Sstevel@tonic-gate boolean_t old = (v->gv_flags & GV_INSUBGRAPH) != 0; 26150Sstevel@tonic-gate boolean_t new; 26160Sstevel@tonic-gate graph_edge_t *e; 26170Sstevel@tonic-gate scf_instance_t *inst; 26180Sstevel@tonic-gate int ret = 0, r; 26190Sstevel@tonic-gate 26200Sstevel@tonic-gate assert(milestone != NULL && milestone != MILESTONE_NONE); 26210Sstevel@tonic-gate 26220Sstevel@tonic-gate new = should_be_in_subgraph(v); 26230Sstevel@tonic-gate 26240Sstevel@tonic-gate if (new == old) 26250Sstevel@tonic-gate return (0); 26260Sstevel@tonic-gate 26270Sstevel@tonic-gate log_framework(LOG_DEBUG, new ? "Adding %s to the subgraph.\n" : 26280Sstevel@tonic-gate "Removing %s from the subgraph.\n", v->gv_name); 26290Sstevel@tonic-gate 26300Sstevel@tonic-gate v->gv_flags = (v->gv_flags & ~GV_INSUBGRAPH) | 26310Sstevel@tonic-gate (new ? GV_INSUBGRAPH : 0); 26320Sstevel@tonic-gate 26330Sstevel@tonic-gate if (v->gv_type == GVT_INST && (v->gv_flags & GV_CONFIGURED)) { 26340Sstevel@tonic-gate int err; 26350Sstevel@tonic-gate 26360Sstevel@tonic-gate get_inst: 26370Sstevel@tonic-gate err = libscf_fmri_get_instance(h, v->gv_name, &inst); 26380Sstevel@tonic-gate if (err != 0) { 26390Sstevel@tonic-gate switch (err) { 26400Sstevel@tonic-gate case ECONNABORTED: 26410Sstevel@tonic-gate libscf_handle_rebind(h); 26420Sstevel@tonic-gate ret = ECONNABORTED; 26430Sstevel@tonic-gate goto get_inst; 26440Sstevel@tonic-gate 26450Sstevel@tonic-gate case ENOENT: 26460Sstevel@tonic-gate break; 26470Sstevel@tonic-gate 26480Sstevel@tonic-gate case EINVAL: 26490Sstevel@tonic-gate case ENOTSUP: 26500Sstevel@tonic-gate default: 26510Sstevel@tonic-gate bad_error("libscf_fmri_get_instance", err); 26520Sstevel@tonic-gate } 26530Sstevel@tonic-gate } else { 26540Sstevel@tonic-gate const char *f; 26550Sstevel@tonic-gate 26560Sstevel@tonic-gate if (new) { 26570Sstevel@tonic-gate err = libscf_delete_enable_ovr(inst); 26580Sstevel@tonic-gate f = "libscf_delete_enable_ovr"; 26590Sstevel@tonic-gate } else { 26600Sstevel@tonic-gate err = libscf_set_enable_ovr(inst, 0); 26610Sstevel@tonic-gate f = "libscf_set_enable_ovr"; 26620Sstevel@tonic-gate } 26630Sstevel@tonic-gate scf_instance_destroy(inst); 26640Sstevel@tonic-gate switch (err) { 26650Sstevel@tonic-gate case 0: 26660Sstevel@tonic-gate case ECANCELED: 26670Sstevel@tonic-gate break; 26680Sstevel@tonic-gate 26690Sstevel@tonic-gate case ECONNABORTED: 26700Sstevel@tonic-gate libscf_handle_rebind(h); 26710Sstevel@tonic-gate /* 26720Sstevel@tonic-gate * We must continue so the graph is updated, 26730Sstevel@tonic-gate * but we must return ECONNABORTED so any 26740Sstevel@tonic-gate * libscf state held by any callers is reset. 26750Sstevel@tonic-gate */ 26760Sstevel@tonic-gate ret = ECONNABORTED; 26770Sstevel@tonic-gate goto get_inst; 26780Sstevel@tonic-gate 26790Sstevel@tonic-gate case EROFS: 26800Sstevel@tonic-gate case EPERM: 26810Sstevel@tonic-gate log_error(LOG_WARNING, 26820Sstevel@tonic-gate "Could not set %s/%s for %s: %s.\n", 26830Sstevel@tonic-gate SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED, 26840Sstevel@tonic-gate v->gv_name, strerror(err)); 26850Sstevel@tonic-gate break; 26860Sstevel@tonic-gate 26870Sstevel@tonic-gate default: 26880Sstevel@tonic-gate bad_error(f, err); 26890Sstevel@tonic-gate } 26900Sstevel@tonic-gate } 26910Sstevel@tonic-gate } 26920Sstevel@tonic-gate 26930Sstevel@tonic-gate for (e = uu_list_first(v->gv_dependencies); 26940Sstevel@tonic-gate e != NULL; 26950Sstevel@tonic-gate e = uu_list_next(v->gv_dependencies, e)) { 26960Sstevel@tonic-gate r = eval_subgraph(e->ge_vertex, h); 26970Sstevel@tonic-gate if (r != 0) { 26980Sstevel@tonic-gate assert(r == ECONNABORTED); 26990Sstevel@tonic-gate ret = ECONNABORTED; 27000Sstevel@tonic-gate } 27010Sstevel@tonic-gate } 27020Sstevel@tonic-gate 27030Sstevel@tonic-gate return (ret); 27040Sstevel@tonic-gate } 27050Sstevel@tonic-gate 27060Sstevel@tonic-gate /* 27070Sstevel@tonic-gate * Delete the (property group) dependencies of v & create new ones based on 27080Sstevel@tonic-gate * inst. If doing so would create a cycle, log a message and put the instance 27090Sstevel@tonic-gate * into maintenance. Update GV_INSUBGRAPH flags as necessary. Returns 0 or 27100Sstevel@tonic-gate * ECONNABORTED. 27110Sstevel@tonic-gate */ 27120Sstevel@tonic-gate static int 27130Sstevel@tonic-gate refresh_vertex(graph_vertex_t *v, scf_instance_t *inst) 27140Sstevel@tonic-gate { 27150Sstevel@tonic-gate int err; 27160Sstevel@tonic-gate int *path; 27170Sstevel@tonic-gate char *fmri; 27180Sstevel@tonic-gate int r; 27190Sstevel@tonic-gate scf_handle_t *h = scf_instance_handle(inst); 27200Sstevel@tonic-gate uu_list_t *old_deps; 27210Sstevel@tonic-gate int ret = 0; 27220Sstevel@tonic-gate graph_edge_t *e; 2723*1712Srm88369 graph_vertex_t *vv; 27240Sstevel@tonic-gate 27250Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 27260Sstevel@tonic-gate assert(v->gv_type == GVT_INST); 27270Sstevel@tonic-gate 27280Sstevel@tonic-gate log_framework(LOG_DEBUG, "Graph engine: Refreshing %s.\n", v->gv_name); 27290Sstevel@tonic-gate 27300Sstevel@tonic-gate if (milestone > MILESTONE_NONE) { 27310Sstevel@tonic-gate /* 27320Sstevel@tonic-gate * In case some of v's dependencies are being deleted we must 27330Sstevel@tonic-gate * make a list of them now for GV_INSUBGRAPH-flag evaluation 27340Sstevel@tonic-gate * after the new dependencies are in place. 27350Sstevel@tonic-gate */ 27360Sstevel@tonic-gate old_deps = startd_list_create(graph_edge_pool, NULL, 0); 27370Sstevel@tonic-gate 27380Sstevel@tonic-gate err = uu_list_walk(v->gv_dependencies, 2739*1712Srm88369 (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0); 27400Sstevel@tonic-gate assert(err == 0); 27410Sstevel@tonic-gate } 27420Sstevel@tonic-gate 27430Sstevel@tonic-gate delete_instance_dependencies(v, B_FALSE); 27440Sstevel@tonic-gate 27450Sstevel@tonic-gate err = set_dependencies(v, inst, &path); 27460Sstevel@tonic-gate switch (err) { 27470Sstevel@tonic-gate case 0: 27480Sstevel@tonic-gate break; 27490Sstevel@tonic-gate 27500Sstevel@tonic-gate case ECONNABORTED: 27510Sstevel@tonic-gate ret = err; 27520Sstevel@tonic-gate goto out; 27530Sstevel@tonic-gate 27540Sstevel@tonic-gate case EINVAL: 27550Sstevel@tonic-gate case ELOOP: 27560Sstevel@tonic-gate r = libscf_instance_get_fmri(inst, &fmri); 27570Sstevel@tonic-gate switch (r) { 27580Sstevel@tonic-gate case 0: 27590Sstevel@tonic-gate break; 27600Sstevel@tonic-gate 27610Sstevel@tonic-gate case ECONNABORTED: 27620Sstevel@tonic-gate ret = ECONNABORTED; 27630Sstevel@tonic-gate goto out; 27640Sstevel@tonic-gate 27650Sstevel@tonic-gate case ECANCELED: 27660Sstevel@tonic-gate ret = 0; 27670Sstevel@tonic-gate goto out; 27680Sstevel@tonic-gate 27690Sstevel@tonic-gate default: 27700Sstevel@tonic-gate bad_error("libscf_instance_get_fmri", r); 27710Sstevel@tonic-gate } 27720Sstevel@tonic-gate 27730Sstevel@tonic-gate if (err == EINVAL) { 27740Sstevel@tonic-gate log_error(LOG_ERR, "Transitioning %s " 27750Sstevel@tonic-gate "to maintenance due to misconfiguration.\n", 27760Sstevel@tonic-gate fmri ? fmri : "?"); 27770Sstevel@tonic-gate vertex_send_event(v, 27780Sstevel@tonic-gate RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY); 27790Sstevel@tonic-gate } else { 27800Sstevel@tonic-gate handle_cycle(fmri, path); 27810Sstevel@tonic-gate vertex_send_event(v, 27820Sstevel@tonic-gate RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE); 27830Sstevel@tonic-gate } 27840Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size); 27850Sstevel@tonic-gate ret = 0; 27860Sstevel@tonic-gate goto out; 27870Sstevel@tonic-gate 27880Sstevel@tonic-gate default: 27890Sstevel@tonic-gate bad_error("set_dependencies", err); 27900Sstevel@tonic-gate } 27910Sstevel@tonic-gate 27920Sstevel@tonic-gate if (milestone > MILESTONE_NONE) { 27930Sstevel@tonic-gate boolean_t aborted = B_FALSE; 27940Sstevel@tonic-gate 27950Sstevel@tonic-gate for (e = uu_list_first(old_deps); 27960Sstevel@tonic-gate e != NULL; 27970Sstevel@tonic-gate e = uu_list_next(old_deps, e)) { 2798*1712Srm88369 vv = e->ge_vertex; 2799*1712Srm88369 2800*1712Srm88369 if (vertex_unref(vv) == VERTEX_INUSE && 2801*1712Srm88369 eval_subgraph(vv, h) == ECONNABORTED) 28020Sstevel@tonic-gate aborted = B_TRUE; 28030Sstevel@tonic-gate } 28040Sstevel@tonic-gate 28050Sstevel@tonic-gate for (e = uu_list_first(v->gv_dependencies); 28060Sstevel@tonic-gate e != NULL; 28070Sstevel@tonic-gate e = uu_list_next(v->gv_dependencies, e)) { 28080Sstevel@tonic-gate if (eval_subgraph(e->ge_vertex, h) == 28090Sstevel@tonic-gate ECONNABORTED) 28100Sstevel@tonic-gate aborted = B_TRUE; 28110Sstevel@tonic-gate } 28120Sstevel@tonic-gate 28130Sstevel@tonic-gate if (aborted) { 28140Sstevel@tonic-gate ret = ECONNABORTED; 28150Sstevel@tonic-gate goto out; 28160Sstevel@tonic-gate } 28170Sstevel@tonic-gate } 28180Sstevel@tonic-gate 28190Sstevel@tonic-gate if (v->gv_state == RESTARTER_STATE_OFFLINE) { 28200Sstevel@tonic-gate if (instance_satisfied(v, B_FALSE) == 1) { 28210Sstevel@tonic-gate if (v->gv_start_f == NULL) 28220Sstevel@tonic-gate vertex_send_event(v, 28230Sstevel@tonic-gate RESTARTER_EVENT_TYPE_START); 28240Sstevel@tonic-gate else 28250Sstevel@tonic-gate v->gv_start_f(v); 28260Sstevel@tonic-gate } 28270Sstevel@tonic-gate } 28280Sstevel@tonic-gate 28290Sstevel@tonic-gate ret = 0; 28300Sstevel@tonic-gate 28310Sstevel@tonic-gate out: 28320Sstevel@tonic-gate if (milestone > MILESTONE_NONE) { 28330Sstevel@tonic-gate void *cookie = NULL; 28340Sstevel@tonic-gate 28350Sstevel@tonic-gate while ((e = uu_list_teardown(old_deps, &cookie)) != NULL) 28360Sstevel@tonic-gate startd_free(e, sizeof (*e)); 28370Sstevel@tonic-gate 28380Sstevel@tonic-gate uu_list_destroy(old_deps); 28390Sstevel@tonic-gate } 28400Sstevel@tonic-gate 28410Sstevel@tonic-gate return (ret); 28420Sstevel@tonic-gate } 28430Sstevel@tonic-gate 28440Sstevel@tonic-gate /* 28450Sstevel@tonic-gate * Set up v according to inst. That is, make sure it depends on its 28460Sstevel@tonic-gate * restarter and set up its dependencies. Send the ADD_INSTANCE command to 28470Sstevel@tonic-gate * the restarter, and send ENABLE or DISABLE as appropriate. 28480Sstevel@tonic-gate * 28490Sstevel@tonic-gate * Returns 0 on success, ECONNABORTED on repository disconnection, or 28500Sstevel@tonic-gate * ECANCELED if inst is deleted. 28510Sstevel@tonic-gate */ 28520Sstevel@tonic-gate static int 28530Sstevel@tonic-gate configure_vertex(graph_vertex_t *v, scf_instance_t *inst) 28540Sstevel@tonic-gate { 28550Sstevel@tonic-gate scf_handle_t *h; 28560Sstevel@tonic-gate scf_propertygroup_t *pg; 28570Sstevel@tonic-gate scf_snapshot_t *snap; 28580Sstevel@tonic-gate char *restarter_fmri = startd_alloc(max_scf_value_size); 28590Sstevel@tonic-gate int enabled, enabled_ovr; 28600Sstevel@tonic-gate int err; 28610Sstevel@tonic-gate int *path; 28620Sstevel@tonic-gate 28630Sstevel@tonic-gate restarter_fmri[0] = '\0'; 28640Sstevel@tonic-gate 28650Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 28660Sstevel@tonic-gate assert(v->gv_type == GVT_INST); 28670Sstevel@tonic-gate assert((v->gv_flags & GV_CONFIGURED) == 0); 28680Sstevel@tonic-gate 28690Sstevel@tonic-gate /* GV_INSUBGRAPH should already be set properly. */ 28700Sstevel@tonic-gate assert(should_be_in_subgraph(v) == 28710Sstevel@tonic-gate ((v->gv_flags & GV_INSUBGRAPH) != 0)); 28720Sstevel@tonic-gate 28730Sstevel@tonic-gate log_framework(LOG_DEBUG, "Graph adding %s.\n", v->gv_name); 28740Sstevel@tonic-gate 28750Sstevel@tonic-gate h = scf_instance_handle(inst); 28760Sstevel@tonic-gate 28770Sstevel@tonic-gate /* 28780Sstevel@tonic-gate * If the instance does not have a restarter property group, 28790Sstevel@tonic-gate * initialize its state to uninitialized/none, in case the restarter 28800Sstevel@tonic-gate * is not enabled. 28810Sstevel@tonic-gate */ 28820Sstevel@tonic-gate pg = safe_scf_pg_create(h); 28830Sstevel@tonic-gate 28840Sstevel@tonic-gate if (scf_instance_get_pg(inst, SCF_PG_RESTARTER, pg) != 0) { 28850Sstevel@tonic-gate instance_data_t idata; 28860Sstevel@tonic-gate uint_t count = 0, msecs = ALLOC_DELAY; 28870Sstevel@tonic-gate 28880Sstevel@tonic-gate switch (scf_error()) { 28890Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 28900Sstevel@tonic-gate break; 28910Sstevel@tonic-gate 28920Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 28930Sstevel@tonic-gate default: 28940Sstevel@tonic-gate scf_pg_destroy(pg); 28950Sstevel@tonic-gate return (ECONNABORTED); 28960Sstevel@tonic-gate 28970Sstevel@tonic-gate case SCF_ERROR_DELETED: 28980Sstevel@tonic-gate scf_pg_destroy(pg); 28990Sstevel@tonic-gate return (ECANCELED); 29000Sstevel@tonic-gate 29010Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 29020Sstevel@tonic-gate bad_error("scf_instance_get_pg", scf_error()); 29030Sstevel@tonic-gate } 29040Sstevel@tonic-gate 29050Sstevel@tonic-gate switch (err = libscf_instance_get_fmri(inst, 29060Sstevel@tonic-gate (char **)&idata.i_fmri)) { 29070Sstevel@tonic-gate case 0: 29080Sstevel@tonic-gate break; 29090Sstevel@tonic-gate 29100Sstevel@tonic-gate case ECONNABORTED: 29110Sstevel@tonic-gate case ECANCELED: 29120Sstevel@tonic-gate scf_pg_destroy(pg); 29130Sstevel@tonic-gate return (err); 29140Sstevel@tonic-gate 29150Sstevel@tonic-gate default: 29160Sstevel@tonic-gate bad_error("libscf_instance_get_fmri", err); 29170Sstevel@tonic-gate } 29180Sstevel@tonic-gate 29190Sstevel@tonic-gate idata.i_state = RESTARTER_STATE_NONE; 29200Sstevel@tonic-gate idata.i_next_state = RESTARTER_STATE_NONE; 29210Sstevel@tonic-gate 29220Sstevel@tonic-gate init_state: 29230Sstevel@tonic-gate switch (err = _restarter_commit_states(h, &idata, 29240Sstevel@tonic-gate RESTARTER_STATE_UNINIT, RESTARTER_STATE_NONE, NULL)) { 29250Sstevel@tonic-gate case 0: 29260Sstevel@tonic-gate break; 29270Sstevel@tonic-gate 29280Sstevel@tonic-gate case ENOMEM: 29290Sstevel@tonic-gate ++count; 29300Sstevel@tonic-gate if (count < ALLOC_RETRY) { 29310Sstevel@tonic-gate (void) poll(NULL, 0, msecs); 29320Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT; 29330Sstevel@tonic-gate goto init_state; 29340Sstevel@tonic-gate } 29350Sstevel@tonic-gate 29360Sstevel@tonic-gate uu_die("Insufficient memory.\n"); 29370Sstevel@tonic-gate /* NOTREACHED */ 29380Sstevel@tonic-gate 29390Sstevel@tonic-gate case ECONNABORTED: 29400Sstevel@tonic-gate scf_pg_destroy(pg); 29410Sstevel@tonic-gate return (ECONNABORTED); 29420Sstevel@tonic-gate 29430Sstevel@tonic-gate case ENOENT: 29440Sstevel@tonic-gate scf_pg_destroy(pg); 29450Sstevel@tonic-gate return (ECANCELED); 29460Sstevel@tonic-gate 29470Sstevel@tonic-gate case EPERM: 29480Sstevel@tonic-gate case EACCES: 29490Sstevel@tonic-gate case EROFS: 29500Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not initialize state for " 29510Sstevel@tonic-gate "%s: %s.\n", idata.i_fmri, strerror(err)); 29520Sstevel@tonic-gate break; 29530Sstevel@tonic-gate 29540Sstevel@tonic-gate case EINVAL: 29550Sstevel@tonic-gate default: 29560Sstevel@tonic-gate bad_error("_restarter_commit_states", err); 29570Sstevel@tonic-gate } 29580Sstevel@tonic-gate 29590Sstevel@tonic-gate startd_free((void *)idata.i_fmri, max_scf_fmri_size); 29600Sstevel@tonic-gate } 29610Sstevel@tonic-gate 29620Sstevel@tonic-gate scf_pg_destroy(pg); 29630Sstevel@tonic-gate 29640Sstevel@tonic-gate if (milestone != NULL) { 29650Sstevel@tonic-gate /* 29660Sstevel@tonic-gate * Make sure the enable-override is set properly before we 29670Sstevel@tonic-gate * read whether we should be enabled. 29680Sstevel@tonic-gate */ 29690Sstevel@tonic-gate if (milestone == MILESTONE_NONE || 29700Sstevel@tonic-gate !(v->gv_flags & GV_INSUBGRAPH)) { 29710Sstevel@tonic-gate switch (err = libscf_set_enable_ovr(inst, 0)) { 29720Sstevel@tonic-gate case 0: 29730Sstevel@tonic-gate break; 29740Sstevel@tonic-gate 29750Sstevel@tonic-gate case ECONNABORTED: 29760Sstevel@tonic-gate case ECANCELED: 29770Sstevel@tonic-gate return (err); 29780Sstevel@tonic-gate 29790Sstevel@tonic-gate case EROFS: 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 case EPERM: 29870Sstevel@tonic-gate uu_die("Permission denied.\n"); 29880Sstevel@tonic-gate /* NOTREACHED */ 29890Sstevel@tonic-gate 29900Sstevel@tonic-gate default: 29910Sstevel@tonic-gate bad_error("libscf_set_enable_ovr", err); 29920Sstevel@tonic-gate } 29930Sstevel@tonic-gate } else { 29940Sstevel@tonic-gate assert(v->gv_flags & GV_INSUBGRAPH); 29950Sstevel@tonic-gate switch (err = libscf_delete_enable_ovr(inst)) { 29960Sstevel@tonic-gate case 0: 29970Sstevel@tonic-gate break; 29980Sstevel@tonic-gate 29990Sstevel@tonic-gate case ECONNABORTED: 30000Sstevel@tonic-gate case ECANCELED: 30010Sstevel@tonic-gate return (err); 30020Sstevel@tonic-gate 30030Sstevel@tonic-gate case EPERM: 30040Sstevel@tonic-gate uu_die("Permission denied.\n"); 30050Sstevel@tonic-gate /* NOTREACHED */ 30060Sstevel@tonic-gate 30070Sstevel@tonic-gate default: 30080Sstevel@tonic-gate bad_error("libscf_delete_enable_ovr", err); 30090Sstevel@tonic-gate } 30100Sstevel@tonic-gate } 30110Sstevel@tonic-gate } 30120Sstevel@tonic-gate 30130Sstevel@tonic-gate err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled, 30140Sstevel@tonic-gate &enabled_ovr, &restarter_fmri); 30150Sstevel@tonic-gate switch (err) { 30160Sstevel@tonic-gate case 0: 30170Sstevel@tonic-gate break; 30180Sstevel@tonic-gate 30190Sstevel@tonic-gate case ECONNABORTED: 30200Sstevel@tonic-gate case ECANCELED: 30210Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size); 30220Sstevel@tonic-gate return (err); 30230Sstevel@tonic-gate 30240Sstevel@tonic-gate case ENOENT: 30250Sstevel@tonic-gate log_framework(LOG_DEBUG, 30260Sstevel@tonic-gate "Ignoring %s because it has no general property group.\n", 30270Sstevel@tonic-gate v->gv_name); 30280Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size); 30290Sstevel@tonic-gate return (0); 30300Sstevel@tonic-gate 30310Sstevel@tonic-gate default: 30320Sstevel@tonic-gate bad_error("libscf_get_basic_instance_data", err); 30330Sstevel@tonic-gate } 30340Sstevel@tonic-gate 30350Sstevel@tonic-gate if (enabled == -1) { 30360Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size); 30370Sstevel@tonic-gate return (0); 30380Sstevel@tonic-gate } 30390Sstevel@tonic-gate 30400Sstevel@tonic-gate v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) | 30410Sstevel@tonic-gate (enabled ? GV_ENBLD_NOOVR : 0); 30420Sstevel@tonic-gate 30430Sstevel@tonic-gate if (enabled_ovr != -1) 30440Sstevel@tonic-gate enabled = enabled_ovr; 30450Sstevel@tonic-gate 30460Sstevel@tonic-gate v->gv_state = RESTARTER_STATE_UNINIT; 30470Sstevel@tonic-gate 30480Sstevel@tonic-gate snap = libscf_get_or_make_running_snapshot(inst, v->gv_name, B_TRUE); 30490Sstevel@tonic-gate scf_snapshot_destroy(snap); 30500Sstevel@tonic-gate 30510Sstevel@tonic-gate /* Set up the restarter. (Sends _ADD_INSTANCE on success.) */ 30520Sstevel@tonic-gate err = graph_change_restarter(v, restarter_fmri, h, &path); 30530Sstevel@tonic-gate if (err != 0) { 30540Sstevel@tonic-gate instance_data_t idata; 30550Sstevel@tonic-gate uint_t count = 0, msecs = ALLOC_DELAY; 30560Sstevel@tonic-gate const char *reason; 30570Sstevel@tonic-gate 30580Sstevel@tonic-gate if (err == ECONNABORTED) { 30590Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size); 30600Sstevel@tonic-gate return (err); 30610Sstevel@tonic-gate } 30620Sstevel@tonic-gate 30630Sstevel@tonic-gate assert(err == EINVAL || err == ELOOP); 30640Sstevel@tonic-gate 30650Sstevel@tonic-gate if (err == EINVAL) { 30660Sstevel@tonic-gate log_framework(LOG_WARNING, emsg_invalid_restarter, 30670Sstevel@tonic-gate v->gv_name); 30680Sstevel@tonic-gate reason = "invalid_restarter"; 30690Sstevel@tonic-gate } else { 30700Sstevel@tonic-gate handle_cycle(v->gv_name, path); 30710Sstevel@tonic-gate reason = "dependency_cycle"; 30720Sstevel@tonic-gate } 30730Sstevel@tonic-gate 30740Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size); 30750Sstevel@tonic-gate 30760Sstevel@tonic-gate /* 30770Sstevel@tonic-gate * We didn't register the instance with the restarter, so we 30780Sstevel@tonic-gate * must set maintenance mode ourselves. 30790Sstevel@tonic-gate */ 30800Sstevel@tonic-gate err = libscf_instance_get_fmri(inst, (char **)&idata.i_fmri); 30810Sstevel@tonic-gate if (err != 0) { 30820Sstevel@tonic-gate assert(err == ECONNABORTED || err == ECANCELED); 30830Sstevel@tonic-gate return (err); 30840Sstevel@tonic-gate } 30850Sstevel@tonic-gate 30860Sstevel@tonic-gate idata.i_state = RESTARTER_STATE_NONE; 30870Sstevel@tonic-gate idata.i_next_state = RESTARTER_STATE_NONE; 30880Sstevel@tonic-gate 30890Sstevel@tonic-gate set_maint: 30900Sstevel@tonic-gate switch (err = _restarter_commit_states(h, &idata, 30910Sstevel@tonic-gate RESTARTER_STATE_MAINT, RESTARTER_STATE_NONE, reason)) { 30920Sstevel@tonic-gate case 0: 30930Sstevel@tonic-gate break; 30940Sstevel@tonic-gate 30950Sstevel@tonic-gate case ENOMEM: 30960Sstevel@tonic-gate ++count; 30970Sstevel@tonic-gate if (count < ALLOC_RETRY) { 30980Sstevel@tonic-gate (void) poll(NULL, 0, msecs); 30990Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT; 31000Sstevel@tonic-gate goto set_maint; 31010Sstevel@tonic-gate } 31020Sstevel@tonic-gate 31030Sstevel@tonic-gate uu_die("Insufficient memory.\n"); 31040Sstevel@tonic-gate /* NOTREACHED */ 31050Sstevel@tonic-gate 31060Sstevel@tonic-gate case ECONNABORTED: 31070Sstevel@tonic-gate return (ECONNABORTED); 31080Sstevel@tonic-gate 31090Sstevel@tonic-gate case ENOENT: 31100Sstevel@tonic-gate return (ECANCELED); 31110Sstevel@tonic-gate 31120Sstevel@tonic-gate case EPERM: 31130Sstevel@tonic-gate case EACCES: 31140Sstevel@tonic-gate case EROFS: 31150Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not initialize state for " 31160Sstevel@tonic-gate "%s: %s.\n", idata.i_fmri, strerror(err)); 31170Sstevel@tonic-gate break; 31180Sstevel@tonic-gate 31190Sstevel@tonic-gate case EINVAL: 31200Sstevel@tonic-gate default: 31210Sstevel@tonic-gate bad_error("_restarter_commit_states", err); 31220Sstevel@tonic-gate } 31230Sstevel@tonic-gate 31240Sstevel@tonic-gate startd_free((void *)idata.i_fmri, max_scf_fmri_size); 31250Sstevel@tonic-gate 31260Sstevel@tonic-gate v->gv_state = RESTARTER_STATE_MAINT; 31270Sstevel@tonic-gate 31280Sstevel@tonic-gate goto out; 31290Sstevel@tonic-gate } 31300Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size); 31310Sstevel@tonic-gate 31320Sstevel@tonic-gate /* Add all the other dependencies. */ 31330Sstevel@tonic-gate err = refresh_vertex(v, inst); 31340Sstevel@tonic-gate if (err != 0) { 31350Sstevel@tonic-gate assert(err == ECONNABORTED); 31360Sstevel@tonic-gate return (err); 31370Sstevel@tonic-gate } 31380Sstevel@tonic-gate 31390Sstevel@tonic-gate out: 31400Sstevel@tonic-gate v->gv_flags |= GV_CONFIGURED; 31410Sstevel@tonic-gate 31420Sstevel@tonic-gate graph_enable_by_vertex(v, enabled, 0); 31430Sstevel@tonic-gate 31440Sstevel@tonic-gate return (0); 31450Sstevel@tonic-gate } 31460Sstevel@tonic-gate 31470Sstevel@tonic-gate static void 31480Sstevel@tonic-gate do_uadmin(void) 31490Sstevel@tonic-gate { 31500Sstevel@tonic-gate int fd, left; 31510Sstevel@tonic-gate struct statvfs vfs; 31520Sstevel@tonic-gate 31530Sstevel@tonic-gate const char * const resetting = "/etc/svc/volatile/resetting"; 31540Sstevel@tonic-gate 31550Sstevel@tonic-gate fd = creat(resetting, 0777); 31560Sstevel@tonic-gate if (fd >= 0) 31570Sstevel@tonic-gate startd_close(fd); 31580Sstevel@tonic-gate else 31590Sstevel@tonic-gate uu_warn("Could not create \"%s\"", resetting); 31600Sstevel@tonic-gate 31610Sstevel@tonic-gate /* Kill dhcpagent if we're not using nfs for root */ 31620Sstevel@tonic-gate if ((statvfs("/", &vfs) == 0) && 31630Sstevel@tonic-gate (strncmp(vfs.f_basetype, "nfs", sizeof ("nfs") - 1) != 0)) 31640Sstevel@tonic-gate (void) system("/usr/bin/pkill -x -u 0 dhcpagent"); 31650Sstevel@tonic-gate 31660Sstevel@tonic-gate (void) system("/usr/sbin/killall"); 31670Sstevel@tonic-gate left = 5; 31680Sstevel@tonic-gate while (left > 0) 31690Sstevel@tonic-gate left = sleep(left); 31700Sstevel@tonic-gate 31710Sstevel@tonic-gate (void) system("/usr/sbin/killall 9"); 31720Sstevel@tonic-gate left = 10; 31730Sstevel@tonic-gate while (left > 0) 31740Sstevel@tonic-gate left = sleep(left); 31750Sstevel@tonic-gate 31760Sstevel@tonic-gate sync(); 31770Sstevel@tonic-gate sync(); 31780Sstevel@tonic-gate sync(); 31790Sstevel@tonic-gate 31800Sstevel@tonic-gate (void) system("/sbin/umountall"); 31810Sstevel@tonic-gate (void) system("/sbin/umount /tmp >/dev/null 2>&1"); 31820Sstevel@tonic-gate (void) system("/sbin/umount /var/adm >/dev/null 2>&1"); 31830Sstevel@tonic-gate (void) system("/sbin/umount /var/run >/dev/null 2>&1"); 31840Sstevel@tonic-gate (void) system("/sbin/umount /var >/dev/null 2>&1"); 31850Sstevel@tonic-gate (void) system("/sbin/umount /usr >/dev/null 2>&1"); 31860Sstevel@tonic-gate 31870Sstevel@tonic-gate uu_warn("The system is down.\n"); 31880Sstevel@tonic-gate 31890Sstevel@tonic-gate (void) uadmin(A_SHUTDOWN, halting, NULL); 31900Sstevel@tonic-gate uu_warn("uadmin() failed"); 31910Sstevel@tonic-gate 31920Sstevel@tonic-gate if (remove(resetting) != 0 && errno != ENOENT) 31930Sstevel@tonic-gate uu_warn("Could not remove \"%s\"", resetting); 31940Sstevel@tonic-gate } 31950Sstevel@tonic-gate 31960Sstevel@tonic-gate /* 31970Sstevel@tonic-gate * If any of the up_svcs[] are online or satisfiable, return true. If they are 31980Sstevel@tonic-gate * all missing, disabled, in maintenance, or unsatisfiable, return false. 31990Sstevel@tonic-gate */ 32000Sstevel@tonic-gate boolean_t 32010Sstevel@tonic-gate can_come_up(void) 32020Sstevel@tonic-gate { 32030Sstevel@tonic-gate int i; 32040Sstevel@tonic-gate 32050Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 32060Sstevel@tonic-gate 32070Sstevel@tonic-gate /* 32080Sstevel@tonic-gate * If we are booting to single user (boot -s), 32090Sstevel@tonic-gate * SCF_MILESTONE_SINGLE_USER is needed to come up because startd 32100Sstevel@tonic-gate * spawns sulogin after single-user is online (see specials.c). 32110Sstevel@tonic-gate */ 32120Sstevel@tonic-gate i = (booting_to_single_user ? 0 : 1); 32130Sstevel@tonic-gate 32140Sstevel@tonic-gate for (; up_svcs[i] != NULL; ++i) { 32150Sstevel@tonic-gate if (up_svcs_p[i] == NULL) { 32160Sstevel@tonic-gate up_svcs_p[i] = vertex_get_by_name(up_svcs[i]); 32170Sstevel@tonic-gate 32180Sstevel@tonic-gate if (up_svcs_p[i] == NULL) 32190Sstevel@tonic-gate continue; 32200Sstevel@tonic-gate } 32210Sstevel@tonic-gate 32220Sstevel@tonic-gate /* 32230Sstevel@tonic-gate * Ignore unconfigured services (the ones that have been 32240Sstevel@tonic-gate * mentioned in a dependency from other services, but do 32250Sstevel@tonic-gate * not exist in the repository). Services which exist 32260Sstevel@tonic-gate * in the repository but don't have general/enabled 32270Sstevel@tonic-gate * property will be also ignored. 32280Sstevel@tonic-gate */ 32290Sstevel@tonic-gate if (!(up_svcs_p[i]->gv_flags & GV_CONFIGURED)) 32300Sstevel@tonic-gate continue; 32310Sstevel@tonic-gate 32320Sstevel@tonic-gate switch (up_svcs_p[i]->gv_state) { 32330Sstevel@tonic-gate case RESTARTER_STATE_ONLINE: 32340Sstevel@tonic-gate case RESTARTER_STATE_DEGRADED: 32350Sstevel@tonic-gate /* 32360Sstevel@tonic-gate * Deactivate verbose boot once a login service has been 32370Sstevel@tonic-gate * reached. 32380Sstevel@tonic-gate */ 32390Sstevel@tonic-gate st->st_log_login_reached = 1; 32400Sstevel@tonic-gate /*FALLTHROUGH*/ 32410Sstevel@tonic-gate case RESTARTER_STATE_UNINIT: 32420Sstevel@tonic-gate return (B_TRUE); 32430Sstevel@tonic-gate 32440Sstevel@tonic-gate case RESTARTER_STATE_OFFLINE: 32450Sstevel@tonic-gate if (instance_satisfied(up_svcs_p[i], B_TRUE) != -1) 32460Sstevel@tonic-gate return (B_TRUE); 32470Sstevel@tonic-gate log_framework(LOG_DEBUG, 32480Sstevel@tonic-gate "can_come_up(): %s is unsatisfiable.\n", 32490Sstevel@tonic-gate up_svcs_p[i]->gv_name); 32500Sstevel@tonic-gate continue; 32510Sstevel@tonic-gate 32520Sstevel@tonic-gate case RESTARTER_STATE_DISABLED: 32530Sstevel@tonic-gate case RESTARTER_STATE_MAINT: 32540Sstevel@tonic-gate log_framework(LOG_DEBUG, 32550Sstevel@tonic-gate "can_come_up(): %s is in state %s.\n", 32560Sstevel@tonic-gate up_svcs_p[i]->gv_name, 32570Sstevel@tonic-gate instance_state_str[up_svcs_p[i]->gv_state]); 32580Sstevel@tonic-gate continue; 32590Sstevel@tonic-gate 32600Sstevel@tonic-gate default: 32610Sstevel@tonic-gate #ifndef NDEBUG 32620Sstevel@tonic-gate uu_warn("%s:%d: Unexpected vertex state %d.\n", 32630Sstevel@tonic-gate __FILE__, __LINE__, up_svcs_p[i]->gv_state); 32640Sstevel@tonic-gate #endif 32650Sstevel@tonic-gate abort(); 32660Sstevel@tonic-gate } 32670Sstevel@tonic-gate } 32680Sstevel@tonic-gate 32690Sstevel@tonic-gate /* 32700Sstevel@tonic-gate * In the seed repository, console-login is unsatisfiable because 32710Sstevel@tonic-gate * services are missing. To behave correctly in that case we don't want 32720Sstevel@tonic-gate * to return false until manifest-import is online. 32730Sstevel@tonic-gate */ 32740Sstevel@tonic-gate 32750Sstevel@tonic-gate if (manifest_import_p == NULL) { 32760Sstevel@tonic-gate manifest_import_p = vertex_get_by_name(manifest_import); 32770Sstevel@tonic-gate 32780Sstevel@tonic-gate if (manifest_import_p == NULL) 32790Sstevel@tonic-gate return (B_FALSE); 32800Sstevel@tonic-gate } 32810Sstevel@tonic-gate 32820Sstevel@tonic-gate switch (manifest_import_p->gv_state) { 32830Sstevel@tonic-gate case RESTARTER_STATE_ONLINE: 32840Sstevel@tonic-gate case RESTARTER_STATE_DEGRADED: 32850Sstevel@tonic-gate case RESTARTER_STATE_DISABLED: 32860Sstevel@tonic-gate case RESTARTER_STATE_MAINT: 32870Sstevel@tonic-gate break; 32880Sstevel@tonic-gate 32890Sstevel@tonic-gate case RESTARTER_STATE_OFFLINE: 32900Sstevel@tonic-gate if (instance_satisfied(manifest_import_p, B_TRUE) == -1) 32910Sstevel@tonic-gate break; 32920Sstevel@tonic-gate /* FALLTHROUGH */ 32930Sstevel@tonic-gate 32940Sstevel@tonic-gate case RESTARTER_STATE_UNINIT: 32950Sstevel@tonic-gate return (B_TRUE); 32960Sstevel@tonic-gate } 32970Sstevel@tonic-gate 32980Sstevel@tonic-gate return (B_FALSE); 32990Sstevel@tonic-gate } 33000Sstevel@tonic-gate 33010Sstevel@tonic-gate /* 33020Sstevel@tonic-gate * Runs sulogin. Returns 33030Sstevel@tonic-gate * 0 - success 33040Sstevel@tonic-gate * EALREADY - sulogin is already running 33050Sstevel@tonic-gate * EBUSY - console-login is running 33060Sstevel@tonic-gate */ 33070Sstevel@tonic-gate static int 33080Sstevel@tonic-gate run_sulogin(const char *msg) 33090Sstevel@tonic-gate { 33100Sstevel@tonic-gate graph_vertex_t *v; 33110Sstevel@tonic-gate 33120Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 33130Sstevel@tonic-gate 33140Sstevel@tonic-gate if (sulogin_running) 33150Sstevel@tonic-gate return (EALREADY); 33160Sstevel@tonic-gate 33170Sstevel@tonic-gate v = vertex_get_by_name(console_login_fmri); 33180Sstevel@tonic-gate if (v != NULL && inst_running(v)) 33190Sstevel@tonic-gate return (EBUSY); 33200Sstevel@tonic-gate 33210Sstevel@tonic-gate sulogin_running = B_TRUE; 33220Sstevel@tonic-gate 33230Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 33240Sstevel@tonic-gate 33250Sstevel@tonic-gate fork_sulogin(B_FALSE, msg); 33260Sstevel@tonic-gate 33270Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 33280Sstevel@tonic-gate 33290Sstevel@tonic-gate sulogin_running = B_FALSE; 33300Sstevel@tonic-gate 33310Sstevel@tonic-gate if (console_login_ready) { 33320Sstevel@tonic-gate v = vertex_get_by_name(console_login_fmri); 33330Sstevel@tonic-gate 33340Sstevel@tonic-gate if (v != NULL && v->gv_state == RESTARTER_STATE_OFFLINE && 33350Sstevel@tonic-gate !inst_running(v)) { 33360Sstevel@tonic-gate if (v->gv_start_f == NULL) 33370Sstevel@tonic-gate vertex_send_event(v, 33380Sstevel@tonic-gate RESTARTER_EVENT_TYPE_START); 33390Sstevel@tonic-gate else 33400Sstevel@tonic-gate v->gv_start_f(v); 33410Sstevel@tonic-gate } 33420Sstevel@tonic-gate 33430Sstevel@tonic-gate console_login_ready = B_FALSE; 33440Sstevel@tonic-gate } 33450Sstevel@tonic-gate 33460Sstevel@tonic-gate return (0); 33470Sstevel@tonic-gate } 33480Sstevel@tonic-gate 33490Sstevel@tonic-gate /* 33500Sstevel@tonic-gate * The sulogin thread runs sulogin while can_come_up() is false. run_sulogin() 33510Sstevel@tonic-gate * keeps sulogin from stepping on console-login's toes. 33520Sstevel@tonic-gate */ 33530Sstevel@tonic-gate /* ARGSUSED */ 33540Sstevel@tonic-gate static void * 33550Sstevel@tonic-gate sulogin_thread(void *unused) 33560Sstevel@tonic-gate { 33570Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 33580Sstevel@tonic-gate 33590Sstevel@tonic-gate assert(sulogin_thread_running); 33600Sstevel@tonic-gate 33610Sstevel@tonic-gate do 33620Sstevel@tonic-gate (void) run_sulogin("Console login service(s) cannot run\n"); 33630Sstevel@tonic-gate while (!can_come_up()); 33640Sstevel@tonic-gate 33650Sstevel@tonic-gate sulogin_thread_running = B_FALSE; 33660Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 33670Sstevel@tonic-gate 33680Sstevel@tonic-gate return (NULL); 33690Sstevel@tonic-gate } 33700Sstevel@tonic-gate 33710Sstevel@tonic-gate /* ARGSUSED */ 33720Sstevel@tonic-gate void * 33730Sstevel@tonic-gate single_user_thread(void *unused) 33740Sstevel@tonic-gate { 33750Sstevel@tonic-gate uint_t left; 33760Sstevel@tonic-gate scf_handle_t *h; 33770Sstevel@tonic-gate scf_instance_t *inst; 33780Sstevel@tonic-gate scf_property_t *prop; 33790Sstevel@tonic-gate scf_value_t *val; 33800Sstevel@tonic-gate const char *msg; 33810Sstevel@tonic-gate char *buf; 33820Sstevel@tonic-gate int r; 33830Sstevel@tonic-gate 33840Sstevel@tonic-gate MUTEX_LOCK(&single_user_thread_lock); 33850Sstevel@tonic-gate single_user_thread_count++; 33860Sstevel@tonic-gate 33870Sstevel@tonic-gate if (!booting_to_single_user) { 33880Sstevel@tonic-gate /* 33890Sstevel@tonic-gate * From rcS.sh: Look for ttymon, in.telnetd, in.rlogind and 33900Sstevel@tonic-gate * processes in their process groups so they can be terminated. 33910Sstevel@tonic-gate */ 33920Sstevel@tonic-gate (void) fputs("svc.startd: Killing user processes: ", stdout); 33930Sstevel@tonic-gate (void) system("/usr/sbin/killall"); 33940Sstevel@tonic-gate (void) system("/usr/sbin/killall 9"); 33950Sstevel@tonic-gate (void) system("/usr/bin/pkill -TERM -v -u 0,1"); 33960Sstevel@tonic-gate 33970Sstevel@tonic-gate left = 5; 33980Sstevel@tonic-gate while (left > 0) 33990Sstevel@tonic-gate left = sleep(left); 34000Sstevel@tonic-gate 34010Sstevel@tonic-gate (void) system("/usr/bin/pkill -KILL -v -u 0,1"); 34020Sstevel@tonic-gate (void) puts("done."); 34030Sstevel@tonic-gate } 34040Sstevel@tonic-gate 34050Sstevel@tonic-gate if (go_single_user_mode || booting_to_single_user) { 34060Sstevel@tonic-gate msg = "SINGLE USER MODE\n"; 34070Sstevel@tonic-gate } else { 34080Sstevel@tonic-gate assert(go_to_level1); 34090Sstevel@tonic-gate 34100Sstevel@tonic-gate fork_rc_script('1', "start", B_TRUE); 34110Sstevel@tonic-gate 34120Sstevel@tonic-gate uu_warn("The system is ready for administration.\n"); 34130Sstevel@tonic-gate 34140Sstevel@tonic-gate msg = ""; 34150Sstevel@tonic-gate } 34160Sstevel@tonic-gate 34170Sstevel@tonic-gate MUTEX_UNLOCK(&single_user_thread_lock); 34180Sstevel@tonic-gate 34190Sstevel@tonic-gate for (;;) { 34200Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 34210Sstevel@tonic-gate r = run_sulogin(msg); 34220Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 34230Sstevel@tonic-gate if (r == 0) 34240Sstevel@tonic-gate break; 34250Sstevel@tonic-gate 34260Sstevel@tonic-gate assert(r == EALREADY || r == EBUSY); 34270Sstevel@tonic-gate 34280Sstevel@tonic-gate left = 3; 34290Sstevel@tonic-gate while (left > 0) 34300Sstevel@tonic-gate left = sleep(left); 34310Sstevel@tonic-gate } 34320Sstevel@tonic-gate 34330Sstevel@tonic-gate MUTEX_LOCK(&single_user_thread_lock); 34340Sstevel@tonic-gate 34350Sstevel@tonic-gate /* 34360Sstevel@tonic-gate * If another single user thread has started, let it finish changing 34370Sstevel@tonic-gate * the run level. 34380Sstevel@tonic-gate */ 34390Sstevel@tonic-gate if (single_user_thread_count > 1) { 34400Sstevel@tonic-gate single_user_thread_count--; 34410Sstevel@tonic-gate MUTEX_UNLOCK(&single_user_thread_lock); 34420Sstevel@tonic-gate return (NULL); 34430Sstevel@tonic-gate } 34440Sstevel@tonic-gate 34450Sstevel@tonic-gate h = libscf_handle_create_bound_loop(); 34460Sstevel@tonic-gate inst = scf_instance_create(h); 34470Sstevel@tonic-gate prop = safe_scf_property_create(h); 34480Sstevel@tonic-gate val = safe_scf_value_create(h); 34490Sstevel@tonic-gate buf = startd_alloc(max_scf_fmri_size); 34500Sstevel@tonic-gate 34510Sstevel@tonic-gate lookup: 34520Sstevel@tonic-gate if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst, 34530Sstevel@tonic-gate NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) { 34540Sstevel@tonic-gate switch (scf_error()) { 34550Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 34560Sstevel@tonic-gate r = libscf_create_self(h); 34570Sstevel@tonic-gate if (r == 0) 34580Sstevel@tonic-gate goto lookup; 34590Sstevel@tonic-gate assert(r == ECONNABORTED); 34600Sstevel@tonic-gate /* FALLTHROUGH */ 34610Sstevel@tonic-gate 34620Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 34630Sstevel@tonic-gate libscf_handle_rebind(h); 34640Sstevel@tonic-gate goto lookup; 34650Sstevel@tonic-gate 34660Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 34670Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED: 34680Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 34690Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 34700Sstevel@tonic-gate default: 34710Sstevel@tonic-gate bad_error("scf_handle_decode_fmri", scf_error()); 34720Sstevel@tonic-gate } 34730Sstevel@tonic-gate } 34740Sstevel@tonic-gate 34750Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 34760Sstevel@tonic-gate 34770Sstevel@tonic-gate r = libscf_inst_delete_prop(inst, SCF_PG_OPTIONS_OVR, 34780Sstevel@tonic-gate SCF_PROPERTY_MILESTONE); 34790Sstevel@tonic-gate switch (r) { 34800Sstevel@tonic-gate case 0: 34810Sstevel@tonic-gate case ECANCELED: 34820Sstevel@tonic-gate break; 34830Sstevel@tonic-gate 34840Sstevel@tonic-gate case ECONNABORTED: 34850Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 34860Sstevel@tonic-gate libscf_handle_rebind(h); 34870Sstevel@tonic-gate goto lookup; 34880Sstevel@tonic-gate 34890Sstevel@tonic-gate case EPERM: 34900Sstevel@tonic-gate case EACCES: 34910Sstevel@tonic-gate case EROFS: 34920Sstevel@tonic-gate log_error(LOG_WARNING, "Could not clear temporary milestone: " 34930Sstevel@tonic-gate "%s.\n", strerror(r)); 34940Sstevel@tonic-gate break; 34950Sstevel@tonic-gate 34960Sstevel@tonic-gate default: 34970Sstevel@tonic-gate bad_error("libscf_inst_delete_prop", r); 34980Sstevel@tonic-gate } 34990Sstevel@tonic-gate 35000Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 35010Sstevel@tonic-gate 35020Sstevel@tonic-gate r = libscf_get_milestone(inst, prop, val, buf, max_scf_fmri_size); 35030Sstevel@tonic-gate switch (r) { 35040Sstevel@tonic-gate case ECANCELED: 35050Sstevel@tonic-gate case ENOENT: 35060Sstevel@tonic-gate case EINVAL: 35070Sstevel@tonic-gate (void) strcpy(buf, "all"); 35080Sstevel@tonic-gate /* FALLTHROUGH */ 35090Sstevel@tonic-gate 35100Sstevel@tonic-gate case 0: 35110Sstevel@tonic-gate uu_warn("Returning to milestone %s.\n", buf); 35120Sstevel@tonic-gate break; 35130Sstevel@tonic-gate 35140Sstevel@tonic-gate case ECONNABORTED: 35150Sstevel@tonic-gate libscf_handle_rebind(h); 35160Sstevel@tonic-gate goto lookup; 35170Sstevel@tonic-gate 35180Sstevel@tonic-gate default: 35190Sstevel@tonic-gate bad_error("libscf_get_milestone", r); 35200Sstevel@tonic-gate } 35210Sstevel@tonic-gate 35220Sstevel@tonic-gate r = dgraph_set_milestone(buf, h, B_FALSE); 35230Sstevel@tonic-gate switch (r) { 35240Sstevel@tonic-gate case 0: 35250Sstevel@tonic-gate case ECONNRESET: 35260Sstevel@tonic-gate case EALREADY: 35270Sstevel@tonic-gate case EINVAL: 35280Sstevel@tonic-gate case ENOENT: 35290Sstevel@tonic-gate break; 35300Sstevel@tonic-gate 35310Sstevel@tonic-gate default: 35320Sstevel@tonic-gate bad_error("dgraph_set_milestone", r); 35330Sstevel@tonic-gate } 35340Sstevel@tonic-gate 35350Sstevel@tonic-gate /* 35360Sstevel@tonic-gate * See graph_runlevel_changed(). 35370Sstevel@tonic-gate */ 35380Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 35390Sstevel@tonic-gate utmpx_set_runlevel(target_milestone_as_runlevel(), 'S', B_TRUE); 35400Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 35410Sstevel@tonic-gate 35420Sstevel@tonic-gate startd_free(buf, max_scf_fmri_size); 35430Sstevel@tonic-gate scf_value_destroy(val); 35440Sstevel@tonic-gate scf_property_destroy(prop); 35450Sstevel@tonic-gate scf_instance_destroy(inst); 35460Sstevel@tonic-gate scf_handle_destroy(h); 35470Sstevel@tonic-gate 35480Sstevel@tonic-gate /* 35490Sstevel@tonic-gate * We'll give ourselves 3 seconds to respond to all of the enablings 35500Sstevel@tonic-gate * that setting the milestone should have created before checking 35510Sstevel@tonic-gate * whether to run sulogin. 35520Sstevel@tonic-gate */ 35530Sstevel@tonic-gate left = 3; 35540Sstevel@tonic-gate while (left > 0) 35550Sstevel@tonic-gate left = sleep(left); 35560Sstevel@tonic-gate 35570Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 35580Sstevel@tonic-gate /* 35590Sstevel@tonic-gate * Clearing these variables will allow the sulogin thread to run. We 35600Sstevel@tonic-gate * check here in case there aren't any more state updates anytime soon. 35610Sstevel@tonic-gate */ 35620Sstevel@tonic-gate go_to_level1 = go_single_user_mode = booting_to_single_user = B_FALSE; 35630Sstevel@tonic-gate if (!sulogin_thread_running && !can_come_up()) { 35640Sstevel@tonic-gate (void) startd_thread_create(sulogin_thread, NULL); 35650Sstevel@tonic-gate sulogin_thread_running = B_TRUE; 35660Sstevel@tonic-gate } 35670Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 35680Sstevel@tonic-gate single_user_thread_count--; 35690Sstevel@tonic-gate MUTEX_UNLOCK(&single_user_thread_lock); 35700Sstevel@tonic-gate return (NULL); 35710Sstevel@tonic-gate } 35720Sstevel@tonic-gate 35730Sstevel@tonic-gate 35740Sstevel@tonic-gate /* 35750Sstevel@tonic-gate * Dependency graph operations API. These are handle-independent thread-safe 35760Sstevel@tonic-gate * graph manipulation functions which are the entry points for the event 35770Sstevel@tonic-gate * threads below. 35780Sstevel@tonic-gate */ 35790Sstevel@tonic-gate 35800Sstevel@tonic-gate /* 35810Sstevel@tonic-gate * If a configured vertex exists for inst_fmri, return EEXIST. If no vertex 35820Sstevel@tonic-gate * exists for inst_fmri, add one. Then fetch the restarter from inst, make 35830Sstevel@tonic-gate * this vertex dependent on it, and send _ADD_INSTANCE to the restarter. 35840Sstevel@tonic-gate * Fetch whether the instance should be enabled from inst and send _ENABLE or 35850Sstevel@tonic-gate * _DISABLE as appropriate. Finally rummage through inst's dependency 35860Sstevel@tonic-gate * property groups and add vertices and edges as appropriate. If anything 35870Sstevel@tonic-gate * goes wrong after sending _ADD_INSTANCE, send _ADMIN_MAINT_ON to put the 35880Sstevel@tonic-gate * instance in maintenance. Don't send _START or _STOP until we get a state 35890Sstevel@tonic-gate * update in case we're being restarted and the service is already running. 35900Sstevel@tonic-gate * 35910Sstevel@tonic-gate * To support booting to a milestone, we must also make sure all dependencies 35920Sstevel@tonic-gate * encountered are configured, if they exist in the repository. 35930Sstevel@tonic-gate * 35940Sstevel@tonic-gate * Returns 0 on success, ECONNABORTED on repository disconnection, EINVAL if 35950Sstevel@tonic-gate * inst_fmri is an invalid (or not canonical) FMRI, ECANCELED if inst is 35960Sstevel@tonic-gate * deleted, or EEXIST if a configured vertex for inst_fmri already exists. 35970Sstevel@tonic-gate */ 35980Sstevel@tonic-gate int 35990Sstevel@tonic-gate dgraph_add_instance(const char *inst_fmri, scf_instance_t *inst, 36000Sstevel@tonic-gate boolean_t lock_graph) 36010Sstevel@tonic-gate { 36020Sstevel@tonic-gate graph_vertex_t *v; 36030Sstevel@tonic-gate int err; 36040Sstevel@tonic-gate 36050Sstevel@tonic-gate if (strcmp(inst_fmri, SCF_SERVICE_STARTD) == 0) 36060Sstevel@tonic-gate return (0); 36070Sstevel@tonic-gate 36080Sstevel@tonic-gate /* Check for a vertex for inst_fmri. */ 36090Sstevel@tonic-gate if (lock_graph) { 36100Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 36110Sstevel@tonic-gate } else { 36120Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 36130Sstevel@tonic-gate } 36140Sstevel@tonic-gate 36150Sstevel@tonic-gate v = vertex_get_by_name(inst_fmri); 36160Sstevel@tonic-gate 36170Sstevel@tonic-gate if (v != NULL) { 36180Sstevel@tonic-gate assert(v->gv_type == GVT_INST); 36190Sstevel@tonic-gate 36200Sstevel@tonic-gate if (v->gv_flags & GV_CONFIGURED) { 36210Sstevel@tonic-gate if (lock_graph) 36220Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 36230Sstevel@tonic-gate return (EEXIST); 36240Sstevel@tonic-gate } 36250Sstevel@tonic-gate } else { 36260Sstevel@tonic-gate /* Add the vertex. */ 36270Sstevel@tonic-gate err = graph_insert_vertex_unconfigured(inst_fmri, GVT_INST, 0, 36280Sstevel@tonic-gate RERR_NONE, &v); 36290Sstevel@tonic-gate if (err != 0) { 36300Sstevel@tonic-gate assert(err == EINVAL); 36310Sstevel@tonic-gate if (lock_graph) 36320Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 36330Sstevel@tonic-gate return (EINVAL); 36340Sstevel@tonic-gate } 36350Sstevel@tonic-gate } 36360Sstevel@tonic-gate 36370Sstevel@tonic-gate err = configure_vertex(v, inst); 36380Sstevel@tonic-gate 36390Sstevel@tonic-gate if (lock_graph) 36400Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 36410Sstevel@tonic-gate 36420Sstevel@tonic-gate return (err); 36430Sstevel@tonic-gate } 36440Sstevel@tonic-gate 36450Sstevel@tonic-gate /* 36460Sstevel@tonic-gate * Locate the vertex for this property group's instance. If it doesn't exist 36470Sstevel@tonic-gate * or is unconfigured, call dgraph_add_instance() & return. Otherwise fetch 36480Sstevel@tonic-gate * the restarter for the instance, and if it has changed, send 36490Sstevel@tonic-gate * _REMOVE_INSTANCE to the old restarter, remove the dependency, make sure the 36500Sstevel@tonic-gate * new restarter has a vertex, add a new dependency, and send _ADD_INSTANCE to 36510Sstevel@tonic-gate * the new restarter. Then fetch whether the instance should be enabled, and 36520Sstevel@tonic-gate * if it is different from what we had, or if we changed the restarter, send 36530Sstevel@tonic-gate * the appropriate _ENABLE or _DISABLE command. 36540Sstevel@tonic-gate * 36550Sstevel@tonic-gate * Returns 0 on success, ENOTSUP if the pg's parent is not an instance, 36560Sstevel@tonic-gate * ECONNABORTED on repository disconnection, ECANCELED if the instance is 36570Sstevel@tonic-gate * deleted, or -1 if the instance's general property group is deleted or if 36580Sstevel@tonic-gate * its enabled property is misconfigured. 36590Sstevel@tonic-gate */ 36600Sstevel@tonic-gate static int 36610Sstevel@tonic-gate dgraph_update_general(scf_propertygroup_t *pg) 36620Sstevel@tonic-gate { 36630Sstevel@tonic-gate scf_handle_t *h; 36640Sstevel@tonic-gate scf_instance_t *inst; 36650Sstevel@tonic-gate char *fmri; 36660Sstevel@tonic-gate char *restarter_fmri; 36670Sstevel@tonic-gate graph_vertex_t *v; 36680Sstevel@tonic-gate int err; 36690Sstevel@tonic-gate int enabled, enabled_ovr; 36700Sstevel@tonic-gate int oldflags; 36710Sstevel@tonic-gate 36720Sstevel@tonic-gate /* Find the vertex for this service */ 36730Sstevel@tonic-gate h = scf_pg_handle(pg); 36740Sstevel@tonic-gate 36750Sstevel@tonic-gate inst = safe_scf_instance_create(h); 36760Sstevel@tonic-gate 36770Sstevel@tonic-gate if (scf_pg_get_parent_instance(pg, inst) != 0) { 36780Sstevel@tonic-gate switch (scf_error()) { 36790Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED: 36800Sstevel@tonic-gate return (ENOTSUP); 36810Sstevel@tonic-gate 36820Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 36830Sstevel@tonic-gate default: 36840Sstevel@tonic-gate return (ECONNABORTED); 36850Sstevel@tonic-gate 36860Sstevel@tonic-gate case SCF_ERROR_DELETED: 36870Sstevel@tonic-gate return (0); 36880Sstevel@tonic-gate 36890Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 36900Sstevel@tonic-gate bad_error("scf_pg_get_parent_instance", scf_error()); 36910Sstevel@tonic-gate } 36920Sstevel@tonic-gate } 36930Sstevel@tonic-gate 36940Sstevel@tonic-gate err = libscf_instance_get_fmri(inst, &fmri); 36950Sstevel@tonic-gate switch (err) { 36960Sstevel@tonic-gate case 0: 36970Sstevel@tonic-gate break; 36980Sstevel@tonic-gate 36990Sstevel@tonic-gate case ECONNABORTED: 37000Sstevel@tonic-gate scf_instance_destroy(inst); 37010Sstevel@tonic-gate return (ECONNABORTED); 37020Sstevel@tonic-gate 37030Sstevel@tonic-gate case ECANCELED: 37040Sstevel@tonic-gate scf_instance_destroy(inst); 37050Sstevel@tonic-gate return (0); 37060Sstevel@tonic-gate 37070Sstevel@tonic-gate default: 37080Sstevel@tonic-gate bad_error("libscf_instance_get_fmri", err); 37090Sstevel@tonic-gate } 37100Sstevel@tonic-gate 37110Sstevel@tonic-gate log_framework(LOG_DEBUG, 37120Sstevel@tonic-gate "Graph engine: Reloading general properties for %s.\n", fmri); 37130Sstevel@tonic-gate 37140Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 37150Sstevel@tonic-gate 37160Sstevel@tonic-gate v = vertex_get_by_name(fmri); 37170Sstevel@tonic-gate if (v == NULL || !(v->gv_flags & GV_CONFIGURED)) { 37180Sstevel@tonic-gate /* Will get the up-to-date properties. */ 37190Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 37200Sstevel@tonic-gate err = dgraph_add_instance(fmri, inst, B_TRUE); 37210Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size); 37220Sstevel@tonic-gate scf_instance_destroy(inst); 37230Sstevel@tonic-gate return (err == ECANCELED ? 0 : err); 37240Sstevel@tonic-gate } 37250Sstevel@tonic-gate 37260Sstevel@tonic-gate /* Read enabled & restarter from repository. */ 37270Sstevel@tonic-gate restarter_fmri = startd_alloc(max_scf_value_size); 37280Sstevel@tonic-gate err = libscf_get_basic_instance_data(h, inst, v->gv_name, &enabled, 37290Sstevel@tonic-gate &enabled_ovr, &restarter_fmri); 37300Sstevel@tonic-gate if (err != 0 || enabled == -1) { 37310Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 37320Sstevel@tonic-gate scf_instance_destroy(inst); 37330Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size); 37340Sstevel@tonic-gate 37350Sstevel@tonic-gate switch (err) { 37360Sstevel@tonic-gate case ENOENT: 37370Sstevel@tonic-gate case 0: 37380Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size); 37390Sstevel@tonic-gate return (-1); 37400Sstevel@tonic-gate 37410Sstevel@tonic-gate case ECONNABORTED: 37420Sstevel@tonic-gate case ECANCELED: 37430Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size); 37440Sstevel@tonic-gate return (err); 37450Sstevel@tonic-gate 37460Sstevel@tonic-gate default: 37470Sstevel@tonic-gate bad_error("libscf_get_basic_instance_data", err); 37480Sstevel@tonic-gate } 37490Sstevel@tonic-gate } 37500Sstevel@tonic-gate 37510Sstevel@tonic-gate oldflags = v->gv_flags; 37520Sstevel@tonic-gate v->gv_flags = (v->gv_flags & ~GV_ENBLD_NOOVR) | 37530Sstevel@tonic-gate (enabled ? GV_ENBLD_NOOVR : 0); 37540Sstevel@tonic-gate 37550Sstevel@tonic-gate if (enabled_ovr != -1) 37560Sstevel@tonic-gate enabled = enabled_ovr; 37570Sstevel@tonic-gate 37580Sstevel@tonic-gate /* 37590Sstevel@tonic-gate * If GV_ENBLD_NOOVR has changed, then we need to re-evaluate the 37600Sstevel@tonic-gate * subgraph. 37610Sstevel@tonic-gate */ 37620Sstevel@tonic-gate if (milestone > MILESTONE_NONE && v->gv_flags != oldflags) 37630Sstevel@tonic-gate (void) eval_subgraph(v, h); 37640Sstevel@tonic-gate 37650Sstevel@tonic-gate scf_instance_destroy(inst); 37660Sstevel@tonic-gate 37670Sstevel@tonic-gate /* Ignore restarter change for now. */ 37680Sstevel@tonic-gate 37690Sstevel@tonic-gate startd_free(restarter_fmri, max_scf_value_size); 37700Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size); 37710Sstevel@tonic-gate 37720Sstevel@tonic-gate /* 37730Sstevel@tonic-gate * Always send _ENABLE or _DISABLE. We could avoid this if the 37740Sstevel@tonic-gate * restarter didn't change and the enabled value didn't change, but 37750Sstevel@tonic-gate * that's not easy to check and improbable anyway, so we'll just do 37760Sstevel@tonic-gate * this. 37770Sstevel@tonic-gate */ 37780Sstevel@tonic-gate graph_enable_by_vertex(v, enabled, 1); 37790Sstevel@tonic-gate 37800Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 37810Sstevel@tonic-gate 37820Sstevel@tonic-gate return (0); 37830Sstevel@tonic-gate } 37840Sstevel@tonic-gate 37850Sstevel@tonic-gate /* 37860Sstevel@tonic-gate * Delete all of the property group dependencies of v, update inst's running 37870Sstevel@tonic-gate * snapshot, and add the dependencies in the new snapshot. If any of the new 37880Sstevel@tonic-gate * dependencies would create a cycle, send _ADMIN_MAINT_ON. Otherwise 37890Sstevel@tonic-gate * reevaluate v's dependencies, send _START or _STOP as appropriate, and do 37900Sstevel@tonic-gate * the same for v's dependents. 37910Sstevel@tonic-gate * 37920Sstevel@tonic-gate * Returns 37930Sstevel@tonic-gate * 0 - success 37940Sstevel@tonic-gate * ECONNABORTED - repository connection broken 37950Sstevel@tonic-gate * ECANCELED - inst was deleted 37960Sstevel@tonic-gate * EINVAL - inst is invalid (e.g., missing general/enabled) 37970Sstevel@tonic-gate * -1 - libscf_snapshots_refresh() failed 37980Sstevel@tonic-gate */ 37990Sstevel@tonic-gate static int 38000Sstevel@tonic-gate dgraph_refresh_instance(graph_vertex_t *v, scf_instance_t *inst) 38010Sstevel@tonic-gate { 38020Sstevel@tonic-gate int r; 38030Sstevel@tonic-gate int enabled; 38040Sstevel@tonic-gate 38050Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 38060Sstevel@tonic-gate assert(v->gv_type == GVT_INST); 38070Sstevel@tonic-gate 38080Sstevel@tonic-gate /* Only refresh services with valid general/enabled properties. */ 38090Sstevel@tonic-gate r = libscf_get_basic_instance_data(scf_instance_handle(inst), inst, 38100Sstevel@tonic-gate v->gv_name, &enabled, NULL, NULL); 38110Sstevel@tonic-gate switch (r) { 38120Sstevel@tonic-gate case 0: 38130Sstevel@tonic-gate break; 38140Sstevel@tonic-gate 38150Sstevel@tonic-gate case ECONNABORTED: 38160Sstevel@tonic-gate case ECANCELED: 38170Sstevel@tonic-gate return (r); 38180Sstevel@tonic-gate 38190Sstevel@tonic-gate case ENOENT: 38200Sstevel@tonic-gate log_framework(LOG_DEBUG, 38210Sstevel@tonic-gate "Ignoring %s because it has no general property group.\n", 38220Sstevel@tonic-gate v->gv_name); 38230Sstevel@tonic-gate return (EINVAL); 38240Sstevel@tonic-gate 38250Sstevel@tonic-gate default: 38260Sstevel@tonic-gate bad_error("libscf_get_basic_instance_data", r); 38270Sstevel@tonic-gate } 38280Sstevel@tonic-gate 38290Sstevel@tonic-gate if (enabled == -1) 38300Sstevel@tonic-gate return (EINVAL); 38310Sstevel@tonic-gate 38320Sstevel@tonic-gate r = libscf_snapshots_refresh(inst, v->gv_name); 38330Sstevel@tonic-gate if (r != 0) { 38340Sstevel@tonic-gate if (r != -1) 38350Sstevel@tonic-gate bad_error("libscf_snapshots_refresh", r); 38360Sstevel@tonic-gate 38370Sstevel@tonic-gate /* error logged */ 38380Sstevel@tonic-gate return (r); 38390Sstevel@tonic-gate } 38400Sstevel@tonic-gate 38410Sstevel@tonic-gate r = refresh_vertex(v, inst); 38420Sstevel@tonic-gate if (r != 0 && r != ECONNABORTED) 38430Sstevel@tonic-gate bad_error("refresh_vertex", r); 38440Sstevel@tonic-gate return (r); 38450Sstevel@tonic-gate } 38460Sstevel@tonic-gate 38470Sstevel@tonic-gate /* 38480Sstevel@tonic-gate * Returns 1 if any instances which directly depend on the passed instance 38490Sstevel@tonic-gate * (or it's service) are running. 38500Sstevel@tonic-gate */ 38510Sstevel@tonic-gate static int 38520Sstevel@tonic-gate has_running_nonsubgraph_dependents(graph_vertex_t *v) 38530Sstevel@tonic-gate { 38540Sstevel@tonic-gate graph_vertex_t *vv; 38550Sstevel@tonic-gate graph_edge_t *e; 38560Sstevel@tonic-gate 38570Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 38580Sstevel@tonic-gate 38590Sstevel@tonic-gate for (e = uu_list_first(v->gv_dependents); 38600Sstevel@tonic-gate e != NULL; 38610Sstevel@tonic-gate e = uu_list_next(v->gv_dependents, e)) { 38620Sstevel@tonic-gate 38630Sstevel@tonic-gate vv = e->ge_vertex; 38640Sstevel@tonic-gate if (vv->gv_type == GVT_INST) { 38650Sstevel@tonic-gate if (inst_running(vv) && 38660Sstevel@tonic-gate ((vv->gv_flags & GV_INSUBGRAPH) == 0)) 38670Sstevel@tonic-gate return (1); 38680Sstevel@tonic-gate } else { 38690Sstevel@tonic-gate /* 38700Sstevel@tonic-gate * For dependency group or service vertices, keep 38710Sstevel@tonic-gate * traversing to see if instances are running. 38720Sstevel@tonic-gate */ 38730Sstevel@tonic-gate if (has_running_nonsubgraph_dependents(vv)) 38740Sstevel@tonic-gate return (1); 38750Sstevel@tonic-gate } 38760Sstevel@tonic-gate } 38770Sstevel@tonic-gate return (0); 38780Sstevel@tonic-gate } 38790Sstevel@tonic-gate 38800Sstevel@tonic-gate /* 38810Sstevel@tonic-gate * For the dependency, disable the instance which makes up the dependency if 38820Sstevel@tonic-gate * it is not in the subgraph and running. If the dependency instance is in 38830Sstevel@tonic-gate * the subgraph or it is not running, continue by disabling all of it's 38840Sstevel@tonic-gate * non-subgraph dependencies. 38850Sstevel@tonic-gate */ 38860Sstevel@tonic-gate static void 38870Sstevel@tonic-gate disable_nonsubgraph_dependencies(graph_vertex_t *v, void *arg) 38880Sstevel@tonic-gate { 38890Sstevel@tonic-gate int r; 38900Sstevel@tonic-gate scf_handle_t *h = (scf_handle_t *)arg; 38910Sstevel@tonic-gate scf_instance_t *inst = NULL; 38920Sstevel@tonic-gate 38930Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 38940Sstevel@tonic-gate 38950Sstevel@tonic-gate /* Continue recursing non-inst nodes */ 38960Sstevel@tonic-gate if (v->gv_type != GVT_INST) 38970Sstevel@tonic-gate goto recurse; 38980Sstevel@tonic-gate 38990Sstevel@tonic-gate /* 39000Sstevel@tonic-gate * For instances that are in the subgraph or already not running, 39010Sstevel@tonic-gate * skip and attempt to disable their non-dependencies. 39020Sstevel@tonic-gate */ 39030Sstevel@tonic-gate if ((v->gv_flags & GV_INSUBGRAPH) || (!inst_running(v))) 39040Sstevel@tonic-gate goto recurse; 39050Sstevel@tonic-gate 39060Sstevel@tonic-gate /* 39070Sstevel@tonic-gate * If not all this instance's dependents have stopped 39080Sstevel@tonic-gate * running, do not disable. 39090Sstevel@tonic-gate */ 39100Sstevel@tonic-gate if (has_running_nonsubgraph_dependents(v)) 39110Sstevel@tonic-gate return; 39120Sstevel@tonic-gate 39130Sstevel@tonic-gate inst = scf_instance_create(h); 39140Sstevel@tonic-gate if (inst == NULL) { 39150Sstevel@tonic-gate log_error(LOG_WARNING, "Unable to gracefully disable instance:" 39160Sstevel@tonic-gate " %s due to lack of resources\n", v->gv_name); 39170Sstevel@tonic-gate goto disable; 39180Sstevel@tonic-gate } 39190Sstevel@tonic-gate again: 39200Sstevel@tonic-gate r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst, 39210Sstevel@tonic-gate NULL, NULL, SCF_DECODE_FMRI_EXACT); 39220Sstevel@tonic-gate if (r != 0) { 39230Sstevel@tonic-gate switch (scf_error()) { 39240Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 39250Sstevel@tonic-gate libscf_handle_rebind(h); 39260Sstevel@tonic-gate goto again; 39270Sstevel@tonic-gate 39280Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 39290Sstevel@tonic-gate goto recurse; 39300Sstevel@tonic-gate 39310Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 39320Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 39330Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED: 39340Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 39350Sstevel@tonic-gate default: 39360Sstevel@tonic-gate bad_error("scf_handle_decode_fmri", 39370Sstevel@tonic-gate scf_error()); 39380Sstevel@tonic-gate } 39390Sstevel@tonic-gate } 39400Sstevel@tonic-gate r = libscf_set_enable_ovr(inst, 0); 39410Sstevel@tonic-gate switch (r) { 39420Sstevel@tonic-gate case 0: 39430Sstevel@tonic-gate scf_instance_destroy(inst); 39440Sstevel@tonic-gate return; 39450Sstevel@tonic-gate case ECANCELED: 39460Sstevel@tonic-gate scf_instance_destroy(inst); 39470Sstevel@tonic-gate goto recurse; 39480Sstevel@tonic-gate case ECONNABORTED: 39490Sstevel@tonic-gate libscf_handle_rebind(h); 39500Sstevel@tonic-gate goto again; 39510Sstevel@tonic-gate case EPERM: 39520Sstevel@tonic-gate case EROFS: 39530Sstevel@tonic-gate log_error(LOG_WARNING, 39540Sstevel@tonic-gate "Could not set %s/%s for %s: %s.\n", 39550Sstevel@tonic-gate SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED, 39560Sstevel@tonic-gate v->gv_name, strerror(r)); 39570Sstevel@tonic-gate goto disable; 39580Sstevel@tonic-gate default: 39590Sstevel@tonic-gate bad_error("libscf_set_enable_ovr", r); 39600Sstevel@tonic-gate } 39610Sstevel@tonic-gate disable: 39620Sstevel@tonic-gate graph_enable_by_vertex(v, 0, 0); 39630Sstevel@tonic-gate return; 39640Sstevel@tonic-gate recurse: 39650Sstevel@tonic-gate graph_walk_dependencies(v, disable_nonsubgraph_dependencies, 39660Sstevel@tonic-gate arg); 39670Sstevel@tonic-gate } 39680Sstevel@tonic-gate 39690Sstevel@tonic-gate /* 39700Sstevel@tonic-gate * Find the vertex for inst_name. If it doesn't exist, return ENOENT. 39710Sstevel@tonic-gate * Otherwise set its state to state. If the instance has entered a state 39720Sstevel@tonic-gate * which requires automatic action, take it (Uninitialized: do 39730Sstevel@tonic-gate * dgraph_refresh_instance() without the snapshot update. Disabled: if the 39740Sstevel@tonic-gate * instance should be enabled, send _ENABLE. Offline: if the instance should 39750Sstevel@tonic-gate * be disabled, send _DISABLE, and if its dependencies are satisfied, send 39760Sstevel@tonic-gate * _START. Online, Degraded: if the instance wasn't running, update its start 39770Sstevel@tonic-gate * snapshot. Maintenance: no action.) 39780Sstevel@tonic-gate * 39790Sstevel@tonic-gate * Also fails with ECONNABORTED, or EINVAL if state is invalid. 39800Sstevel@tonic-gate */ 39810Sstevel@tonic-gate static int 39820Sstevel@tonic-gate dgraph_set_instance_state(scf_handle_t *h, const char *inst_name, 39830Sstevel@tonic-gate restarter_instance_state_t state, restarter_error_t serr) 39840Sstevel@tonic-gate { 39850Sstevel@tonic-gate graph_vertex_t *v; 39860Sstevel@tonic-gate int err = 0, r; 39870Sstevel@tonic-gate int was_running, up_or_down; 39880Sstevel@tonic-gate restarter_instance_state_t old_state; 39890Sstevel@tonic-gate 39900Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 39910Sstevel@tonic-gate 39920Sstevel@tonic-gate v = vertex_get_by_name(inst_name); 39930Sstevel@tonic-gate if (v == NULL) { 39940Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 39950Sstevel@tonic-gate return (ENOENT); 39960Sstevel@tonic-gate } 39970Sstevel@tonic-gate 39980Sstevel@tonic-gate switch (state) { 39990Sstevel@tonic-gate case RESTARTER_STATE_UNINIT: 40000Sstevel@tonic-gate case RESTARTER_STATE_DISABLED: 40010Sstevel@tonic-gate case RESTARTER_STATE_OFFLINE: 40020Sstevel@tonic-gate case RESTARTER_STATE_ONLINE: 40030Sstevel@tonic-gate case RESTARTER_STATE_DEGRADED: 40040Sstevel@tonic-gate case RESTARTER_STATE_MAINT: 40050Sstevel@tonic-gate break; 40060Sstevel@tonic-gate 40070Sstevel@tonic-gate default: 40080Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 40090Sstevel@tonic-gate return (EINVAL); 40100Sstevel@tonic-gate } 40110Sstevel@tonic-gate 40120Sstevel@tonic-gate log_framework(LOG_DEBUG, "Graph noting %s %s -> %s.\n", v->gv_name, 40130Sstevel@tonic-gate instance_state_str[v->gv_state], instance_state_str[state]); 40140Sstevel@tonic-gate 40150Sstevel@tonic-gate old_state = v->gv_state; 40160Sstevel@tonic-gate was_running = inst_running(v); 40170Sstevel@tonic-gate 40180Sstevel@tonic-gate v->gv_state = state; 40190Sstevel@tonic-gate 40200Sstevel@tonic-gate up_or_down = was_running ^ inst_running(v); 40210Sstevel@tonic-gate 40220Sstevel@tonic-gate if (up_or_down && milestone != NULL && !inst_running(v) && 40230Sstevel@tonic-gate ((v->gv_flags & GV_INSUBGRAPH) == 0 || 40240Sstevel@tonic-gate milestone == MILESTONE_NONE)) { 40250Sstevel@tonic-gate --non_subgraph_svcs; 40260Sstevel@tonic-gate if (non_subgraph_svcs == 0) { 40270Sstevel@tonic-gate if (halting != -1) { 40280Sstevel@tonic-gate do_uadmin(); 40290Sstevel@tonic-gate } else if (go_single_user_mode || go_to_level1) { 40300Sstevel@tonic-gate (void) startd_thread_create(single_user_thread, 40310Sstevel@tonic-gate NULL); 40320Sstevel@tonic-gate } 40330Sstevel@tonic-gate } else { 40340Sstevel@tonic-gate graph_walk_dependencies(v, 40350Sstevel@tonic-gate disable_nonsubgraph_dependencies, (void *)h); 40360Sstevel@tonic-gate } 40370Sstevel@tonic-gate } 40380Sstevel@tonic-gate 40390Sstevel@tonic-gate switch (state) { 40400Sstevel@tonic-gate case RESTARTER_STATE_UNINIT: { 40410Sstevel@tonic-gate scf_instance_t *inst; 40420Sstevel@tonic-gate 40430Sstevel@tonic-gate /* Initialize instance by refreshing it. */ 40440Sstevel@tonic-gate 40450Sstevel@tonic-gate err = libscf_fmri_get_instance(h, v->gv_name, &inst); 40460Sstevel@tonic-gate switch (err) { 40470Sstevel@tonic-gate case 0: 40480Sstevel@tonic-gate break; 40490Sstevel@tonic-gate 40500Sstevel@tonic-gate case ECONNABORTED: 40510Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 40520Sstevel@tonic-gate return (ECONNABORTED); 40530Sstevel@tonic-gate 40540Sstevel@tonic-gate case ENOENT: 40550Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 40560Sstevel@tonic-gate return (0); 40570Sstevel@tonic-gate 40580Sstevel@tonic-gate case EINVAL: 40590Sstevel@tonic-gate case ENOTSUP: 40600Sstevel@tonic-gate default: 40610Sstevel@tonic-gate bad_error("libscf_fmri_get_instance", err); 40620Sstevel@tonic-gate } 40630Sstevel@tonic-gate 40640Sstevel@tonic-gate err = refresh_vertex(v, inst); 40650Sstevel@tonic-gate if (err == 0) 40660Sstevel@tonic-gate graph_enable_by_vertex(v, v->gv_flags & GV_ENABLED, 0); 40670Sstevel@tonic-gate 40680Sstevel@tonic-gate scf_instance_destroy(inst); 40690Sstevel@tonic-gate break; 40700Sstevel@tonic-gate } 40710Sstevel@tonic-gate 40720Sstevel@tonic-gate case RESTARTER_STATE_DISABLED: 40730Sstevel@tonic-gate /* 40740Sstevel@tonic-gate * If the instance should be disabled, no problem. Otherwise, 40750Sstevel@tonic-gate * send an enable command, which should result in the instance 40760Sstevel@tonic-gate * moving to OFFLINE. 40770Sstevel@tonic-gate */ 40780Sstevel@tonic-gate if (v->gv_flags & GV_ENABLED) { 40790Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_ENABLE); 40800Sstevel@tonic-gate } else if (was_running && v->gv_post_disable_f) { 40810Sstevel@tonic-gate v->gv_post_disable_f(); 40820Sstevel@tonic-gate } 40830Sstevel@tonic-gate break; 40840Sstevel@tonic-gate 40850Sstevel@tonic-gate case RESTARTER_STATE_OFFLINE: 40860Sstevel@tonic-gate /* 40870Sstevel@tonic-gate * If the instance should be enabled, see if we can start it. 40880Sstevel@tonic-gate * Otherwise send a disable command. 40890Sstevel@tonic-gate */ 40900Sstevel@tonic-gate if (v->gv_flags & GV_ENABLED) { 40910Sstevel@tonic-gate if (instance_satisfied(v, B_FALSE) == 1) { 40920Sstevel@tonic-gate if (v->gv_start_f == NULL) { 40930Sstevel@tonic-gate vertex_send_event(v, 40940Sstevel@tonic-gate RESTARTER_EVENT_TYPE_START); 40950Sstevel@tonic-gate } else { 40960Sstevel@tonic-gate v->gv_start_f(v); 40970Sstevel@tonic-gate } 40980Sstevel@tonic-gate } else { 40990Sstevel@tonic-gate log_framework(LOG_DEBUG, 41000Sstevel@tonic-gate "Dependencies of %s not satisfied, " 41010Sstevel@tonic-gate "not starting.\n", v->gv_name); 41020Sstevel@tonic-gate } 41030Sstevel@tonic-gate } else { 41040Sstevel@tonic-gate if (was_running && v->gv_post_disable_f) 41050Sstevel@tonic-gate v->gv_post_disable_f(); 41060Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_DISABLE); 41070Sstevel@tonic-gate } 41080Sstevel@tonic-gate break; 41090Sstevel@tonic-gate 41100Sstevel@tonic-gate case RESTARTER_STATE_ONLINE: 41110Sstevel@tonic-gate case RESTARTER_STATE_DEGRADED: 41120Sstevel@tonic-gate /* 41130Sstevel@tonic-gate * If the instance has just come up, update the start 41140Sstevel@tonic-gate * snapshot. 41150Sstevel@tonic-gate */ 41160Sstevel@tonic-gate if (!was_running) { 41170Sstevel@tonic-gate /* 41180Sstevel@tonic-gate * Don't fire if we're just recovering state 41190Sstevel@tonic-gate * after a restart. 41200Sstevel@tonic-gate */ 41210Sstevel@tonic-gate if (old_state != RESTARTER_STATE_UNINIT && 41220Sstevel@tonic-gate v->gv_post_online_f) 41230Sstevel@tonic-gate v->gv_post_online_f(); 41240Sstevel@tonic-gate 41250Sstevel@tonic-gate r = libscf_snapshots_poststart(h, v->gv_name, B_TRUE); 41260Sstevel@tonic-gate switch (r) { 41270Sstevel@tonic-gate case 0: 41280Sstevel@tonic-gate case ENOENT: 41290Sstevel@tonic-gate /* 41300Sstevel@tonic-gate * If ENOENT, the instance must have been 41310Sstevel@tonic-gate * deleted. Pretend we were successful since 41320Sstevel@tonic-gate * we should get a delete event later. 41330Sstevel@tonic-gate */ 41340Sstevel@tonic-gate break; 41350Sstevel@tonic-gate 41360Sstevel@tonic-gate case ECONNABORTED: 41370Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 41380Sstevel@tonic-gate return (ECONNABORTED); 41390Sstevel@tonic-gate 41400Sstevel@tonic-gate case EACCES: 41410Sstevel@tonic-gate case ENOTSUP: 41420Sstevel@tonic-gate default: 41430Sstevel@tonic-gate bad_error("libscf_snapshots_poststart", r); 41440Sstevel@tonic-gate } 41450Sstevel@tonic-gate } 41460Sstevel@tonic-gate if (!(v->gv_flags & GV_ENABLED)) 41470Sstevel@tonic-gate vertex_send_event(v, RESTARTER_EVENT_TYPE_DISABLE); 41480Sstevel@tonic-gate break; 41490Sstevel@tonic-gate 41500Sstevel@tonic-gate case RESTARTER_STATE_MAINT: 41510Sstevel@tonic-gate /* No action. */ 41520Sstevel@tonic-gate break; 41530Sstevel@tonic-gate 41540Sstevel@tonic-gate default: 41550Sstevel@tonic-gate /* Should have been caught above. */ 41560Sstevel@tonic-gate #ifndef NDEBUG 41570Sstevel@tonic-gate uu_warn("%s:%d: Uncaught case %d.\n", __FILE__, __LINE__, 41580Sstevel@tonic-gate state); 41590Sstevel@tonic-gate #endif 41600Sstevel@tonic-gate abort(); 41610Sstevel@tonic-gate } 41620Sstevel@tonic-gate 41630Sstevel@tonic-gate /* 41640Sstevel@tonic-gate * If the service came up or went down, propagate the event. We must 41650Sstevel@tonic-gate * treat offline -> disabled as a start since it can satisfy 41660Sstevel@tonic-gate * optional_all dependencies. And we must treat !running -> maintenance 41670Sstevel@tonic-gate * as a start because maintenance satisfies optional and exclusion 41680Sstevel@tonic-gate * dependencies. 41690Sstevel@tonic-gate */ 41700Sstevel@tonic-gate if (inst_running(v)) { 41710Sstevel@tonic-gate if (!was_running) { 41720Sstevel@tonic-gate log_framework(LOG_DEBUG, "Propagating start of %s.\n", 41730Sstevel@tonic-gate v->gv_name); 41740Sstevel@tonic-gate 41750Sstevel@tonic-gate graph_walk_dependents(v, propagate_start, NULL); 41760Sstevel@tonic-gate } else if (serr == RERR_REFRESH) { 41770Sstevel@tonic-gate /* For refresh we'll get a message sans state change */ 41780Sstevel@tonic-gate 41790Sstevel@tonic-gate log_framework(LOG_DEBUG, "Propagating refresh of %s.\n", 41800Sstevel@tonic-gate v->gv_name); 41810Sstevel@tonic-gate 41820Sstevel@tonic-gate graph_walk_dependents(v, propagate_stop, (void *)serr); 41830Sstevel@tonic-gate } 41840Sstevel@tonic-gate } else if (was_running) { 41850Sstevel@tonic-gate log_framework(LOG_DEBUG, "Propagating stop of %s.\n", 41860Sstevel@tonic-gate v->gv_name); 41870Sstevel@tonic-gate 41880Sstevel@tonic-gate graph_walk_dependents(v, propagate_stop, (void *)serr); 41890Sstevel@tonic-gate } else if (v->gv_state == RESTARTER_STATE_DISABLED) { 41900Sstevel@tonic-gate log_framework(LOG_DEBUG, "Propagating disable of %s.\n", 41910Sstevel@tonic-gate v->gv_name); 41920Sstevel@tonic-gate 41930Sstevel@tonic-gate graph_walk_dependents(v, propagate_start, NULL); 41940Sstevel@tonic-gate propagate_satbility(v); 41950Sstevel@tonic-gate } else if (v->gv_state == RESTARTER_STATE_MAINT) { 41960Sstevel@tonic-gate log_framework(LOG_DEBUG, "Propagating maintenance of %s.\n", 41970Sstevel@tonic-gate v->gv_name); 41980Sstevel@tonic-gate 41990Sstevel@tonic-gate graph_walk_dependents(v, propagate_start, NULL); 42000Sstevel@tonic-gate propagate_satbility(v); 42010Sstevel@tonic-gate } 42020Sstevel@tonic-gate 42030Sstevel@tonic-gate if (state != old_state && st->st_load_complete && 42040Sstevel@tonic-gate !go_single_user_mode && !go_to_level1 && 42050Sstevel@tonic-gate halting == -1) { 42060Sstevel@tonic-gate if (!can_come_up() && !sulogin_thread_running) { 42070Sstevel@tonic-gate (void) startd_thread_create(sulogin_thread, NULL); 42080Sstevel@tonic-gate sulogin_thread_running = B_TRUE; 42090Sstevel@tonic-gate } 42100Sstevel@tonic-gate } 42110Sstevel@tonic-gate 42120Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 42130Sstevel@tonic-gate 42140Sstevel@tonic-gate return (err); 42150Sstevel@tonic-gate } 42160Sstevel@tonic-gate 42170Sstevel@tonic-gate /* 42180Sstevel@tonic-gate * If a vertex for fmri exists and it is enabled, send _DISABLE to the 42190Sstevel@tonic-gate * restarter. If it is running, send _STOP. Send _REMOVE_INSTANCE. Delete 42200Sstevel@tonic-gate * all property group dependencies, and the dependency on the restarter, 42210Sstevel@tonic-gate * disposing of vertices as appropriate. If other vertices depend on this 42220Sstevel@tonic-gate * one, mark it unconfigured and return. Otherwise remove the vertex. Always 42230Sstevel@tonic-gate * returns 0. 42240Sstevel@tonic-gate */ 42250Sstevel@tonic-gate static int 42260Sstevel@tonic-gate dgraph_remove_instance(const char *fmri, scf_handle_t *h) 42270Sstevel@tonic-gate { 42280Sstevel@tonic-gate graph_vertex_t *v; 42290Sstevel@tonic-gate graph_edge_t *e; 42300Sstevel@tonic-gate uu_list_t *old_deps; 42310Sstevel@tonic-gate int err; 42320Sstevel@tonic-gate 42330Sstevel@tonic-gate log_framework(LOG_DEBUG, "Graph engine: Removing %s.\n", fmri); 42340Sstevel@tonic-gate 42350Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 42360Sstevel@tonic-gate 42370Sstevel@tonic-gate v = vertex_get_by_name(fmri); 42380Sstevel@tonic-gate if (v == NULL) { 42390Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 42400Sstevel@tonic-gate return (0); 42410Sstevel@tonic-gate } 42420Sstevel@tonic-gate 42430Sstevel@tonic-gate /* Send restarter delete event. */ 42440Sstevel@tonic-gate if (v->gv_flags & GV_CONFIGURED) 42450Sstevel@tonic-gate graph_unset_restarter(v); 42460Sstevel@tonic-gate 42470Sstevel@tonic-gate if (milestone > MILESTONE_NONE) { 42480Sstevel@tonic-gate /* 42490Sstevel@tonic-gate * Make a list of v's current dependencies so we can 42500Sstevel@tonic-gate * reevaluate their GV_INSUBGRAPH flags after the dependencies 42510Sstevel@tonic-gate * are removed. 42520Sstevel@tonic-gate */ 42530Sstevel@tonic-gate old_deps = startd_list_create(graph_edge_pool, NULL, 0); 42540Sstevel@tonic-gate 42550Sstevel@tonic-gate err = uu_list_walk(v->gv_dependencies, 4256*1712Srm88369 (uu_walk_fn_t *)append_svcs_or_insts, old_deps, 0); 42570Sstevel@tonic-gate assert(err == 0); 42580Sstevel@tonic-gate } 42590Sstevel@tonic-gate 42600Sstevel@tonic-gate delete_instance_dependencies(v, B_TRUE); 42610Sstevel@tonic-gate 42620Sstevel@tonic-gate /* 42630Sstevel@tonic-gate * Deleting an instance can both satisfy and unsatisfy dependencies, 42640Sstevel@tonic-gate * depending on their type. First propagate the stop as a RERR_RESTART 42650Sstevel@tonic-gate * event -- deletion isn't a fault, just a normal stop. This gives 42660Sstevel@tonic-gate * dependent services the chance to do a clean shutdown. Then, mark 42670Sstevel@tonic-gate * the service as unconfigured and propagate the start event for the 42680Sstevel@tonic-gate * optional_all dependencies that might have become satisfied. 42690Sstevel@tonic-gate */ 42700Sstevel@tonic-gate graph_walk_dependents(v, propagate_stop, (void *)RERR_RESTART); 42710Sstevel@tonic-gate 42720Sstevel@tonic-gate v->gv_flags &= ~GV_CONFIGURED; 42730Sstevel@tonic-gate 42740Sstevel@tonic-gate graph_walk_dependents(v, propagate_start, NULL); 42750Sstevel@tonic-gate propagate_satbility(v); 42760Sstevel@tonic-gate 42770Sstevel@tonic-gate /* 42780Sstevel@tonic-gate * If there are no (non-service) dependents, the vertex can be 42790Sstevel@tonic-gate * completely removed. 42800Sstevel@tonic-gate */ 4281*1712Srm88369 if (v != milestone && v->gv_refs == 0 && 4282*1712Srm88369 uu_list_numnodes(v->gv_dependents) == 1) 42830Sstevel@tonic-gate remove_inst_vertex(v); 42840Sstevel@tonic-gate 42850Sstevel@tonic-gate if (milestone > MILESTONE_NONE) { 42860Sstevel@tonic-gate void *cookie = NULL; 42870Sstevel@tonic-gate 42880Sstevel@tonic-gate while ((e = uu_list_teardown(old_deps, &cookie)) != NULL) { 4289*1712Srm88369 v = e->ge_vertex; 4290*1712Srm88369 4291*1712Srm88369 if (vertex_unref(v) == VERTEX_INUSE) 4292*1712Srm88369 while (eval_subgraph(v, h) == ECONNABORTED) 4293*1712Srm88369 libscf_handle_rebind(h); 42940Sstevel@tonic-gate 42950Sstevel@tonic-gate startd_free(e, sizeof (*e)); 42960Sstevel@tonic-gate } 42970Sstevel@tonic-gate 42980Sstevel@tonic-gate uu_list_destroy(old_deps); 42990Sstevel@tonic-gate } 43000Sstevel@tonic-gate 43010Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 43020Sstevel@tonic-gate 43030Sstevel@tonic-gate return (0); 43040Sstevel@tonic-gate } 43050Sstevel@tonic-gate 43060Sstevel@tonic-gate /* 43070Sstevel@tonic-gate * Return the eventual (maybe current) milestone in the form of a 43080Sstevel@tonic-gate * legacy runlevel. 43090Sstevel@tonic-gate */ 43100Sstevel@tonic-gate static char 43110Sstevel@tonic-gate target_milestone_as_runlevel() 43120Sstevel@tonic-gate { 43130Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 43140Sstevel@tonic-gate 43150Sstevel@tonic-gate if (milestone == NULL) 43160Sstevel@tonic-gate return ('3'); 43170Sstevel@tonic-gate else if (milestone == MILESTONE_NONE) 43180Sstevel@tonic-gate return ('0'); 43190Sstevel@tonic-gate 43200Sstevel@tonic-gate if (strcmp(milestone->gv_name, multi_user_fmri) == 0) 43210Sstevel@tonic-gate return ('2'); 43220Sstevel@tonic-gate else if (strcmp(milestone->gv_name, single_user_fmri) == 0) 43230Sstevel@tonic-gate return ('S'); 43240Sstevel@tonic-gate else if (strcmp(milestone->gv_name, multi_user_svr_fmri) == 0) 43250Sstevel@tonic-gate return ('3'); 43260Sstevel@tonic-gate 43270Sstevel@tonic-gate #ifndef NDEBUG 43280Sstevel@tonic-gate (void) fprintf(stderr, "%s:%d: Unknown milestone name \"%s\".\n", 43290Sstevel@tonic-gate __FILE__, __LINE__, milestone->gv_name); 43300Sstevel@tonic-gate #endif 43310Sstevel@tonic-gate abort(); 43320Sstevel@tonic-gate /* NOTREACHED */ 43330Sstevel@tonic-gate } 43340Sstevel@tonic-gate 43350Sstevel@tonic-gate static struct { 43360Sstevel@tonic-gate char rl; 43370Sstevel@tonic-gate int sig; 43380Sstevel@tonic-gate } init_sigs[] = { 43390Sstevel@tonic-gate { 'S', SIGBUS }, 43400Sstevel@tonic-gate { '0', SIGINT }, 43410Sstevel@tonic-gate { '1', SIGQUIT }, 43420Sstevel@tonic-gate { '2', SIGILL }, 43430Sstevel@tonic-gate { '3', SIGTRAP }, 43440Sstevel@tonic-gate { '4', SIGIOT }, 43450Sstevel@tonic-gate { '5', SIGEMT }, 43460Sstevel@tonic-gate { '6', SIGFPE }, 43470Sstevel@tonic-gate { 0, 0 } 43480Sstevel@tonic-gate }; 43490Sstevel@tonic-gate 43500Sstevel@tonic-gate static void 43510Sstevel@tonic-gate signal_init(char rl) 43520Sstevel@tonic-gate { 43530Sstevel@tonic-gate pid_t init_pid; 43540Sstevel@tonic-gate int i; 43550Sstevel@tonic-gate 43560Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 43570Sstevel@tonic-gate 43580Sstevel@tonic-gate if (zone_getattr(getzoneid(), ZONE_ATTR_INITPID, &init_pid, 43590Sstevel@tonic-gate sizeof (init_pid)) != sizeof (init_pid)) { 43600Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not get pid to signal init.\n"); 43610Sstevel@tonic-gate return; 43620Sstevel@tonic-gate } 43630Sstevel@tonic-gate 43640Sstevel@tonic-gate for (i = 0; init_sigs[i].rl != 0; ++i) 43650Sstevel@tonic-gate if (init_sigs[i].rl == rl) 43660Sstevel@tonic-gate break; 43670Sstevel@tonic-gate 43680Sstevel@tonic-gate if (init_sigs[i].rl != 0) { 43690Sstevel@tonic-gate if (kill(init_pid, init_sigs[i].sig) != 0) { 43700Sstevel@tonic-gate switch (errno) { 43710Sstevel@tonic-gate case EPERM: 43720Sstevel@tonic-gate case ESRCH: 43730Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not signal init: " 43740Sstevel@tonic-gate "%s.\n", strerror(errno)); 43750Sstevel@tonic-gate break; 43760Sstevel@tonic-gate 43770Sstevel@tonic-gate case EINVAL: 43780Sstevel@tonic-gate default: 43790Sstevel@tonic-gate bad_error("kill", errno); 43800Sstevel@tonic-gate } 43810Sstevel@tonic-gate } 43820Sstevel@tonic-gate } 43830Sstevel@tonic-gate } 43840Sstevel@tonic-gate 43850Sstevel@tonic-gate /* 43860Sstevel@tonic-gate * This is called when one of the major milestones changes state, or when 43870Sstevel@tonic-gate * init is signalled and tells us it was told to change runlevel. We wait 43880Sstevel@tonic-gate * to reach the milestone because this allows /etc/inittab entries to retain 43890Sstevel@tonic-gate * some boot ordering: historically, entries could place themselves before/after 43900Sstevel@tonic-gate * the running of /sbin/rcX scripts but we can no longer make the 43910Sstevel@tonic-gate * distinction because the /sbin/rcX scripts no longer exist as punctuation 43920Sstevel@tonic-gate * marks in /etc/inittab. 43930Sstevel@tonic-gate * 43940Sstevel@tonic-gate * Also, we only trigger an update when we reach the eventual target 43950Sstevel@tonic-gate * milestone: without this, an /etc/inittab entry marked only for 43960Sstevel@tonic-gate * runlevel 2 would be executed for runlevel 3, which is not how 43970Sstevel@tonic-gate * /etc/inittab entries work. 43980Sstevel@tonic-gate * 43990Sstevel@tonic-gate * If we're single user coming online, then we set utmpx to the target 44000Sstevel@tonic-gate * runlevel so that legacy scripts can work as expected. 44010Sstevel@tonic-gate */ 44020Sstevel@tonic-gate static void 44030Sstevel@tonic-gate graph_runlevel_changed(char rl, int online) 44040Sstevel@tonic-gate { 44050Sstevel@tonic-gate char trl; 44060Sstevel@tonic-gate 44070Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&dgraph_lock)); 44080Sstevel@tonic-gate 44090Sstevel@tonic-gate trl = target_milestone_as_runlevel(); 44100Sstevel@tonic-gate 44110Sstevel@tonic-gate if (online) { 44120Sstevel@tonic-gate if (rl == trl) { 44131514Srm88369 current_runlevel = trl; 44140Sstevel@tonic-gate signal_init(trl); 44150Sstevel@tonic-gate } else if (rl == 'S') { 44160Sstevel@tonic-gate /* 44170Sstevel@tonic-gate * At boot, set the entry early for the benefit of the 44180Sstevel@tonic-gate * legacy init scripts. 44190Sstevel@tonic-gate */ 44200Sstevel@tonic-gate utmpx_set_runlevel(trl, 'S', B_FALSE); 44210Sstevel@tonic-gate } 44220Sstevel@tonic-gate } else { 44230Sstevel@tonic-gate if (rl == '3' && trl == '2') { 44241514Srm88369 current_runlevel = trl; 44250Sstevel@tonic-gate signal_init(trl); 44260Sstevel@tonic-gate } else if (rl == '2' && trl == 'S') { 44271514Srm88369 current_runlevel = trl; 44280Sstevel@tonic-gate signal_init(trl); 44290Sstevel@tonic-gate } 44300Sstevel@tonic-gate } 44310Sstevel@tonic-gate } 44320Sstevel@tonic-gate 44330Sstevel@tonic-gate /* 44340Sstevel@tonic-gate * Move to a backwards-compatible runlevel by executing the appropriate 44350Sstevel@tonic-gate * /etc/rc?.d/K* scripts and/or setting the milestone. 44360Sstevel@tonic-gate * 44370Sstevel@tonic-gate * Returns 44380Sstevel@tonic-gate * 0 - success 44390Sstevel@tonic-gate * ECONNRESET - success, but handle was reset 44400Sstevel@tonic-gate * ECONNABORTED - repository connection broken 44410Sstevel@tonic-gate * ECANCELED - pg was deleted 44420Sstevel@tonic-gate */ 44430Sstevel@tonic-gate static int 44440Sstevel@tonic-gate dgraph_set_runlevel(scf_propertygroup_t *pg, scf_property_t *prop) 44450Sstevel@tonic-gate { 44460Sstevel@tonic-gate char rl; 44470Sstevel@tonic-gate scf_handle_t *h; 44480Sstevel@tonic-gate int r; 44490Sstevel@tonic-gate const char *ms = NULL; /* what to commit as options/milestone */ 44500Sstevel@tonic-gate boolean_t rebound = B_FALSE; 44510Sstevel@tonic-gate int mark_rl = 0; 44520Sstevel@tonic-gate 44530Sstevel@tonic-gate const char * const stop = "stop"; 44540Sstevel@tonic-gate 44550Sstevel@tonic-gate r = libscf_extract_runlevel(prop, &rl); 44560Sstevel@tonic-gate switch (r) { 44570Sstevel@tonic-gate case 0: 44580Sstevel@tonic-gate break; 44590Sstevel@tonic-gate 44600Sstevel@tonic-gate case ECONNABORTED: 44610Sstevel@tonic-gate case ECANCELED: 44620Sstevel@tonic-gate return (r); 44630Sstevel@tonic-gate 44640Sstevel@tonic-gate case EINVAL: 44650Sstevel@tonic-gate case ENOENT: 44660Sstevel@tonic-gate log_error(LOG_WARNING, "runlevel property is misconfigured; " 44670Sstevel@tonic-gate "ignoring.\n"); 44680Sstevel@tonic-gate /* delete the bad property */ 44690Sstevel@tonic-gate goto nolock_out; 44700Sstevel@tonic-gate 44710Sstevel@tonic-gate default: 44720Sstevel@tonic-gate bad_error("libscf_extract_runlevel", r); 44730Sstevel@tonic-gate } 44740Sstevel@tonic-gate 44750Sstevel@tonic-gate switch (rl) { 44760Sstevel@tonic-gate case 's': 44770Sstevel@tonic-gate rl = 'S'; 44780Sstevel@tonic-gate /* FALLTHROUGH */ 44790Sstevel@tonic-gate 44800Sstevel@tonic-gate case 'S': 44810Sstevel@tonic-gate case '2': 44820Sstevel@tonic-gate case '3': 44830Sstevel@tonic-gate /* 44840Sstevel@tonic-gate * These cases cause a milestone change, so 44850Sstevel@tonic-gate * graph_runlevel_changed() will eventually deal with 44860Sstevel@tonic-gate * signalling init. 44870Sstevel@tonic-gate */ 44880Sstevel@tonic-gate break; 44890Sstevel@tonic-gate 44900Sstevel@tonic-gate case '0': 44910Sstevel@tonic-gate case '1': 44920Sstevel@tonic-gate case '4': 44930Sstevel@tonic-gate case '5': 44940Sstevel@tonic-gate case '6': 44950Sstevel@tonic-gate mark_rl = 1; 44960Sstevel@tonic-gate break; 44970Sstevel@tonic-gate 44980Sstevel@tonic-gate default: 44990Sstevel@tonic-gate log_framework(LOG_NOTICE, "Unknown runlevel '%c'.\n", rl); 45000Sstevel@tonic-gate ms = NULL; 45010Sstevel@tonic-gate goto nolock_out; 45020Sstevel@tonic-gate } 45030Sstevel@tonic-gate 45040Sstevel@tonic-gate h = scf_pg_handle(pg); 45050Sstevel@tonic-gate 45060Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 45070Sstevel@tonic-gate 45080Sstevel@tonic-gate /* 45090Sstevel@tonic-gate * Since this triggers no milestone changes, force it by hand. 45100Sstevel@tonic-gate */ 45110Sstevel@tonic-gate if (current_runlevel == '4' && rl == '3') 45120Sstevel@tonic-gate mark_rl = 1; 45130Sstevel@tonic-gate 45141514Srm88369 /* 45151514Srm88369 * 1. If we are here after an "init X": 45161514Srm88369 * 45171514Srm88369 * init X 45181514Srm88369 * init/lscf_set_runlevel() 45191514Srm88369 * process_pg_event() 45201514Srm88369 * dgraph_set_runlevel() 45211514Srm88369 * 45221514Srm88369 * then we haven't passed through graph_runlevel_changed() yet, 45231514Srm88369 * therefore 'current_runlevel' has not changed for sure but 'rl' has. 45241514Srm88369 * In consequence, if 'rl' is lower than 'current_runlevel', we change 45251514Srm88369 * the system runlevel and execute the appropriate /etc/rc?.d/K* scripts 45261514Srm88369 * past this test. 45271514Srm88369 * 45281514Srm88369 * 2. On the other hand, if we are here after a "svcadm milestone": 45291514Srm88369 * 45301514Srm88369 * svcadm milestone X 45311514Srm88369 * dgraph_set_milestone() 45321514Srm88369 * handle_graph_update_event() 45331514Srm88369 * dgraph_set_instance_state() 45341514Srm88369 * graph_post_X_[online|offline]() 45351514Srm88369 * graph_runlevel_changed() 45361514Srm88369 * signal_init() 45371514Srm88369 * init/lscf_set_runlevel() 45381514Srm88369 * process_pg_event() 45391514Srm88369 * dgraph_set_runlevel() 45401514Srm88369 * 45411514Srm88369 * then we already passed through graph_runlevel_changed() (by the way 45421514Srm88369 * of dgraph_set_milestone()) and 'current_runlevel' may have changed 45431514Srm88369 * and already be equal to 'rl' so we are going to return immediately 45441514Srm88369 * from dgraph_set_runlevel() without changing the system runlevel and 45451514Srm88369 * without executing the /etc/rc?.d/K* scripts. 45461514Srm88369 */ 45470Sstevel@tonic-gate if (rl == current_runlevel) { 45480Sstevel@tonic-gate ms = NULL; 45490Sstevel@tonic-gate goto out; 45500Sstevel@tonic-gate } 45510Sstevel@tonic-gate 45520Sstevel@tonic-gate log_framework(LOG_DEBUG, "Changing to runlevel '%c'.\n", rl); 45530Sstevel@tonic-gate 45540Sstevel@tonic-gate /* 45550Sstevel@tonic-gate * Make sure stop rc scripts see the new settings via who -r. 45560Sstevel@tonic-gate */ 45570Sstevel@tonic-gate utmpx_set_runlevel(rl, current_runlevel, B_TRUE); 45580Sstevel@tonic-gate 45590Sstevel@tonic-gate /* 45600Sstevel@tonic-gate * Some run levels don't have a direct correspondence to any 45610Sstevel@tonic-gate * milestones, so we have to signal init directly. 45620Sstevel@tonic-gate */ 45630Sstevel@tonic-gate if (mark_rl) { 45640Sstevel@tonic-gate current_runlevel = rl; 45650Sstevel@tonic-gate signal_init(rl); 45660Sstevel@tonic-gate } 45670Sstevel@tonic-gate 45680Sstevel@tonic-gate switch (rl) { 45690Sstevel@tonic-gate case 'S': 45700Sstevel@tonic-gate uu_warn("The system is coming down for administration. " 45710Sstevel@tonic-gate "Please wait.\n"); 45720Sstevel@tonic-gate fork_rc_script(rl, stop, B_FALSE); 45730Sstevel@tonic-gate ms = single_user_fmri; 45740Sstevel@tonic-gate go_single_user_mode = B_TRUE; 45750Sstevel@tonic-gate break; 45760Sstevel@tonic-gate 45770Sstevel@tonic-gate case '0': 45780Sstevel@tonic-gate fork_rc_script(rl, stop, B_TRUE); 45790Sstevel@tonic-gate halting = AD_HALT; 45800Sstevel@tonic-gate goto uadmin; 45810Sstevel@tonic-gate 45820Sstevel@tonic-gate case '5': 45830Sstevel@tonic-gate fork_rc_script(rl, stop, B_TRUE); 45840Sstevel@tonic-gate halting = AD_POWEROFF; 45850Sstevel@tonic-gate goto uadmin; 45860Sstevel@tonic-gate 45870Sstevel@tonic-gate case '6': 45880Sstevel@tonic-gate fork_rc_script(rl, stop, B_TRUE); 45890Sstevel@tonic-gate halting = AD_BOOT; 45900Sstevel@tonic-gate goto uadmin; 45910Sstevel@tonic-gate 45920Sstevel@tonic-gate uadmin: 45930Sstevel@tonic-gate uu_warn("The system is coming down. Please wait.\n"); 45940Sstevel@tonic-gate ms = "none"; 45950Sstevel@tonic-gate 45960Sstevel@tonic-gate /* 45970Sstevel@tonic-gate * We can't wait until all services are offline since this 45980Sstevel@tonic-gate * thread is responsible for taking them offline. Instead we 45990Sstevel@tonic-gate * set halting to the second argument for uadmin() and call 46000Sstevel@tonic-gate * do_uadmin() from dgraph_set_instance_state() when 46010Sstevel@tonic-gate * appropriate. 46020Sstevel@tonic-gate */ 46030Sstevel@tonic-gate break; 46040Sstevel@tonic-gate 46050Sstevel@tonic-gate case '1': 46060Sstevel@tonic-gate if (current_runlevel != 'S') { 46070Sstevel@tonic-gate uu_warn("Changing to state 1.\n"); 46080Sstevel@tonic-gate fork_rc_script(rl, stop, B_FALSE); 46090Sstevel@tonic-gate } else { 46100Sstevel@tonic-gate uu_warn("The system is coming up for administration. " 46110Sstevel@tonic-gate "Please wait.\n"); 46120Sstevel@tonic-gate } 46130Sstevel@tonic-gate ms = single_user_fmri; 46140Sstevel@tonic-gate go_to_level1 = B_TRUE; 46150Sstevel@tonic-gate break; 46160Sstevel@tonic-gate 46170Sstevel@tonic-gate case '2': 46180Sstevel@tonic-gate if (current_runlevel == '3' || current_runlevel == '4') 46190Sstevel@tonic-gate fork_rc_script(rl, stop, B_FALSE); 46200Sstevel@tonic-gate ms = multi_user_fmri; 46210Sstevel@tonic-gate break; 46220Sstevel@tonic-gate 46230Sstevel@tonic-gate case '3': 46240Sstevel@tonic-gate case '4': 46250Sstevel@tonic-gate ms = "all"; 46260Sstevel@tonic-gate break; 46270Sstevel@tonic-gate 46280Sstevel@tonic-gate default: 46290Sstevel@tonic-gate #ifndef NDEBUG 46300Sstevel@tonic-gate (void) fprintf(stderr, "%s:%d: Uncaught case %d ('%c').\n", 46310Sstevel@tonic-gate __FILE__, __LINE__, rl, rl); 46320Sstevel@tonic-gate #endif 46330Sstevel@tonic-gate abort(); 46340Sstevel@tonic-gate } 46350Sstevel@tonic-gate 46360Sstevel@tonic-gate out: 46370Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 46380Sstevel@tonic-gate 46390Sstevel@tonic-gate nolock_out: 46400Sstevel@tonic-gate switch (r = libscf_clear_runlevel(pg, ms)) { 46410Sstevel@tonic-gate case 0: 46420Sstevel@tonic-gate break; 46430Sstevel@tonic-gate 46440Sstevel@tonic-gate case ECONNABORTED: 46450Sstevel@tonic-gate libscf_handle_rebind(h); 46460Sstevel@tonic-gate rebound = B_TRUE; 46470Sstevel@tonic-gate goto nolock_out; 46480Sstevel@tonic-gate 46490Sstevel@tonic-gate case ECANCELED: 46500Sstevel@tonic-gate break; 46510Sstevel@tonic-gate 46520Sstevel@tonic-gate case EPERM: 46530Sstevel@tonic-gate case EACCES: 46540Sstevel@tonic-gate case EROFS: 46550Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not delete \"%s/%s\" property: " 46560Sstevel@tonic-gate "%s.\n", SCF_PG_OPTIONS, "runlevel", strerror(r)); 46570Sstevel@tonic-gate break; 46580Sstevel@tonic-gate 46590Sstevel@tonic-gate default: 46600Sstevel@tonic-gate bad_error("libscf_clear_runlevel", r); 46610Sstevel@tonic-gate } 46620Sstevel@tonic-gate 46630Sstevel@tonic-gate return (rebound ? ECONNRESET : 0); 46640Sstevel@tonic-gate } 46650Sstevel@tonic-gate 46660Sstevel@tonic-gate static int 46670Sstevel@tonic-gate mark_subgraph(graph_edge_t *e, void *arg) 46680Sstevel@tonic-gate { 46690Sstevel@tonic-gate graph_vertex_t *v; 46700Sstevel@tonic-gate int r; 46710Sstevel@tonic-gate int optional = (int)arg; 46720Sstevel@tonic-gate 46730Sstevel@tonic-gate v = e->ge_vertex; 46740Sstevel@tonic-gate 46750Sstevel@tonic-gate /* If it's already in the subgraph, skip. */ 46760Sstevel@tonic-gate if (v->gv_flags & GV_INSUBGRAPH) 46770Sstevel@tonic-gate return (UU_WALK_NEXT); 46780Sstevel@tonic-gate 46790Sstevel@tonic-gate /* 46800Sstevel@tonic-gate * Keep track if walk has entered an optional dependency group 46810Sstevel@tonic-gate */ 46820Sstevel@tonic-gate if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_OPTIONAL_ALL) { 46830Sstevel@tonic-gate optional = 1; 46840Sstevel@tonic-gate } 46850Sstevel@tonic-gate /* 46860Sstevel@tonic-gate * Quit if we are in an optional dependency group and the instance 46870Sstevel@tonic-gate * is disabled 46880Sstevel@tonic-gate */ 46890Sstevel@tonic-gate if (optional && (v->gv_type == GVT_INST) && 46900Sstevel@tonic-gate (!(v->gv_flags & GV_ENBLD_NOOVR))) 46910Sstevel@tonic-gate return (UU_WALK_NEXT); 46920Sstevel@tonic-gate 46930Sstevel@tonic-gate v->gv_flags |= GV_INSUBGRAPH; 46940Sstevel@tonic-gate 46950Sstevel@tonic-gate /* Skip all excluded dependencies. */ 46960Sstevel@tonic-gate if (v->gv_type == GVT_GROUP && v->gv_depgroup == DEPGRP_EXCLUDE_ALL) 46970Sstevel@tonic-gate return (UU_WALK_NEXT); 46980Sstevel@tonic-gate 46990Sstevel@tonic-gate r = uu_list_walk(v->gv_dependencies, (uu_walk_fn_t *)mark_subgraph, 47000Sstevel@tonic-gate (void *)optional, 0); 47010Sstevel@tonic-gate assert(r == 0); 47020Sstevel@tonic-gate return (UU_WALK_NEXT); 47030Sstevel@tonic-gate } 47040Sstevel@tonic-gate 47050Sstevel@tonic-gate /* 47060Sstevel@tonic-gate * "Restrict" the graph to dependencies of fmri. We implement it by walking 47070Sstevel@tonic-gate * all services, override-disabling those which are not descendents of the 47080Sstevel@tonic-gate * instance, and removing any enable-override for the rest. milestone is set 47090Sstevel@tonic-gate * to the vertex which represents fmri so that the other graph operations may 47100Sstevel@tonic-gate * act appropriately. 47110Sstevel@tonic-gate * 47120Sstevel@tonic-gate * If norepository is true, the function will not change the repository. 47130Sstevel@tonic-gate * 47141514Srm88369 * The decision to change the system run level in accordance with the milestone 47151514Srm88369 * is taken in dgraph_set_runlevel(). 47161514Srm88369 * 47170Sstevel@tonic-gate * Returns 47180Sstevel@tonic-gate * 0 - success 47190Sstevel@tonic-gate * ECONNRESET - success, but handle was rebound 47200Sstevel@tonic-gate * EINVAL - fmri is invalid (error is logged) 47210Sstevel@tonic-gate * EALREADY - the milestone is already set to fmri 47220Sstevel@tonic-gate * ENOENT - a configured vertex does not exist for fmri (an error is logged) 47230Sstevel@tonic-gate */ 47240Sstevel@tonic-gate static int 47250Sstevel@tonic-gate dgraph_set_milestone(const char *fmri, scf_handle_t *h, boolean_t norepository) 47260Sstevel@tonic-gate { 47270Sstevel@tonic-gate const char *cfmri, *fs; 47280Sstevel@tonic-gate graph_vertex_t *nm, *v; 47290Sstevel@tonic-gate int ret = 0, r; 47300Sstevel@tonic-gate scf_instance_t *inst; 47310Sstevel@tonic-gate boolean_t isall, isnone, rebound = B_FALSE; 47320Sstevel@tonic-gate 47330Sstevel@tonic-gate /* Validate fmri */ 47340Sstevel@tonic-gate isall = (strcmp(fmri, "all") == 0); 47350Sstevel@tonic-gate isnone = (strcmp(fmri, "none") == 0); 47360Sstevel@tonic-gate 47370Sstevel@tonic-gate if (!isall && !isnone) { 47380Sstevel@tonic-gate if (fmri_canonify(fmri, (char **)&cfmri, B_FALSE) == EINVAL) 47390Sstevel@tonic-gate goto reject; 47400Sstevel@tonic-gate 47410Sstevel@tonic-gate if (strcmp(cfmri, single_user_fmri) != 0 && 47420Sstevel@tonic-gate strcmp(cfmri, multi_user_fmri) != 0 && 47430Sstevel@tonic-gate strcmp(cfmri, multi_user_svr_fmri) != 0) { 47440Sstevel@tonic-gate startd_free((void *)cfmri, max_scf_fmri_size); 47450Sstevel@tonic-gate reject: 47460Sstevel@tonic-gate log_framework(LOG_WARNING, 47470Sstevel@tonic-gate "Rejecting request for invalid milestone \"%s\".\n", 47480Sstevel@tonic-gate fmri); 47490Sstevel@tonic-gate return (EINVAL); 47500Sstevel@tonic-gate } 47510Sstevel@tonic-gate } 47520Sstevel@tonic-gate 47530Sstevel@tonic-gate inst = safe_scf_instance_create(h); 47540Sstevel@tonic-gate 47550Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 47560Sstevel@tonic-gate 47570Sstevel@tonic-gate if (milestone == NULL) { 47580Sstevel@tonic-gate if (isall) { 47590Sstevel@tonic-gate log_framework(LOG_DEBUG, 47600Sstevel@tonic-gate "Milestone already set to all.\n"); 47610Sstevel@tonic-gate ret = EALREADY; 47620Sstevel@tonic-gate goto out; 47630Sstevel@tonic-gate } 47640Sstevel@tonic-gate } else if (milestone == MILESTONE_NONE) { 47650Sstevel@tonic-gate if (isnone) { 47660Sstevel@tonic-gate log_framework(LOG_DEBUG, 47670Sstevel@tonic-gate "Milestone already set to none.\n"); 47680Sstevel@tonic-gate ret = EALREADY; 47690Sstevel@tonic-gate goto out; 47700Sstevel@tonic-gate } 47710Sstevel@tonic-gate } else { 47720Sstevel@tonic-gate if (!isall && !isnone && 47730Sstevel@tonic-gate strcmp(cfmri, milestone->gv_name) == 0) { 47740Sstevel@tonic-gate log_framework(LOG_DEBUG, 47750Sstevel@tonic-gate "Milestone already set to %s.\n", cfmri); 47760Sstevel@tonic-gate ret = EALREADY; 47770Sstevel@tonic-gate goto out; 47780Sstevel@tonic-gate } 47790Sstevel@tonic-gate } 47800Sstevel@tonic-gate 47810Sstevel@tonic-gate if (!isall && !isnone) { 47820Sstevel@tonic-gate nm = vertex_get_by_name(cfmri); 47830Sstevel@tonic-gate if (nm == NULL || !(nm->gv_flags & GV_CONFIGURED)) { 47840Sstevel@tonic-gate log_framework(LOG_WARNING, "Cannot set milestone to %s " 47850Sstevel@tonic-gate "because no such service exists.\n", cfmri); 47860Sstevel@tonic-gate ret = ENOENT; 47870Sstevel@tonic-gate goto out; 47880Sstevel@tonic-gate } 47890Sstevel@tonic-gate } 47900Sstevel@tonic-gate 47910Sstevel@tonic-gate log_framework(LOG_DEBUG, "Changing milestone to %s.\n", fmri); 47920Sstevel@tonic-gate 47930Sstevel@tonic-gate /* 47940Sstevel@tonic-gate * Set milestone, removing the old one if this was the last reference. 47950Sstevel@tonic-gate */ 4796*1712Srm88369 if (milestone > MILESTONE_NONE) 4797*1712Srm88369 (void) vertex_unref(milestone); 47980Sstevel@tonic-gate 47990Sstevel@tonic-gate if (isall) 48000Sstevel@tonic-gate milestone = NULL; 48010Sstevel@tonic-gate else if (isnone) 48020Sstevel@tonic-gate milestone = MILESTONE_NONE; 4803*1712Srm88369 else { 48040Sstevel@tonic-gate milestone = nm; 4805*1712Srm88369 /* milestone should count as a reference */ 4806*1712Srm88369 vertex_ref(milestone); 4807*1712Srm88369 } 48080Sstevel@tonic-gate 48090Sstevel@tonic-gate /* Clear all GV_INSUBGRAPH bits. */ 48100Sstevel@tonic-gate for (v = uu_list_first(dgraph); v != NULL; v = uu_list_next(dgraph, v)) 48110Sstevel@tonic-gate v->gv_flags &= ~GV_INSUBGRAPH; 48120Sstevel@tonic-gate 48130Sstevel@tonic-gate if (!isall && !isnone) { 48140Sstevel@tonic-gate /* Set GV_INSUBGRAPH for milestone & descendents. */ 48150Sstevel@tonic-gate milestone->gv_flags |= GV_INSUBGRAPH; 48160Sstevel@tonic-gate 48170Sstevel@tonic-gate r = uu_list_walk(milestone->gv_dependencies, 48180Sstevel@tonic-gate (uu_walk_fn_t *)mark_subgraph, NULL, 0); 48190Sstevel@tonic-gate assert(r == 0); 48200Sstevel@tonic-gate } 48210Sstevel@tonic-gate 48220Sstevel@tonic-gate /* Un-override services in the subgraph & override-disable the rest. */ 48230Sstevel@tonic-gate if (norepository) 48240Sstevel@tonic-gate goto out; 48250Sstevel@tonic-gate 48260Sstevel@tonic-gate non_subgraph_svcs = 0; 48270Sstevel@tonic-gate for (v = uu_list_first(dgraph); 48280Sstevel@tonic-gate v != NULL; 48290Sstevel@tonic-gate v = uu_list_next(dgraph, v)) { 48300Sstevel@tonic-gate if (v->gv_type != GVT_INST || 48310Sstevel@tonic-gate (v->gv_flags & GV_CONFIGURED) == 0) 48320Sstevel@tonic-gate continue; 48330Sstevel@tonic-gate 48340Sstevel@tonic-gate again: 48350Sstevel@tonic-gate r = scf_handle_decode_fmri(h, v->gv_name, NULL, NULL, inst, 48360Sstevel@tonic-gate NULL, NULL, SCF_DECODE_FMRI_EXACT); 48370Sstevel@tonic-gate if (r != 0) { 48380Sstevel@tonic-gate switch (scf_error()) { 48390Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 48400Sstevel@tonic-gate default: 48410Sstevel@tonic-gate libscf_handle_rebind(h); 48420Sstevel@tonic-gate rebound = B_TRUE; 48430Sstevel@tonic-gate goto again; 48440Sstevel@tonic-gate 48450Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 48460Sstevel@tonic-gate continue; 48470Sstevel@tonic-gate 48480Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 48490Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 48500Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED: 48510Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 48520Sstevel@tonic-gate bad_error("scf_handle_decode_fmri", 48530Sstevel@tonic-gate scf_error()); 48540Sstevel@tonic-gate } 48550Sstevel@tonic-gate } 48560Sstevel@tonic-gate 48570Sstevel@tonic-gate if (isall || (v->gv_flags & GV_INSUBGRAPH)) { 48580Sstevel@tonic-gate r = libscf_delete_enable_ovr(inst); 48590Sstevel@tonic-gate fs = "libscf_delete_enable_ovr"; 48600Sstevel@tonic-gate } else { 48610Sstevel@tonic-gate assert(isnone || (v->gv_flags & GV_INSUBGRAPH) == 0); 48620Sstevel@tonic-gate 48630Sstevel@tonic-gate if (inst_running(v)) 48640Sstevel@tonic-gate ++non_subgraph_svcs; 48650Sstevel@tonic-gate 48660Sstevel@tonic-gate if (has_running_nonsubgraph_dependents(v)) 48670Sstevel@tonic-gate continue; 48680Sstevel@tonic-gate 48690Sstevel@tonic-gate r = libscf_set_enable_ovr(inst, 0); 48700Sstevel@tonic-gate fs = "libscf_set_enable_ovr"; 48710Sstevel@tonic-gate } 48720Sstevel@tonic-gate switch (r) { 48730Sstevel@tonic-gate case 0: 48740Sstevel@tonic-gate case ECANCELED: 48750Sstevel@tonic-gate break; 48760Sstevel@tonic-gate 48770Sstevel@tonic-gate case ECONNABORTED: 48780Sstevel@tonic-gate libscf_handle_rebind(h); 48790Sstevel@tonic-gate rebound = B_TRUE; 48800Sstevel@tonic-gate goto again; 48810Sstevel@tonic-gate 48820Sstevel@tonic-gate case EPERM: 48830Sstevel@tonic-gate case EROFS: 48840Sstevel@tonic-gate log_error(LOG_WARNING, 48850Sstevel@tonic-gate "Could not set %s/%s for %s: %s.\n", 48860Sstevel@tonic-gate SCF_PG_GENERAL_OVR, SCF_PROPERTY_ENABLED, 48870Sstevel@tonic-gate v->gv_name, strerror(r)); 48880Sstevel@tonic-gate break; 48890Sstevel@tonic-gate 48900Sstevel@tonic-gate default: 48910Sstevel@tonic-gate bad_error(fs, r); 48920Sstevel@tonic-gate } 48930Sstevel@tonic-gate } 48940Sstevel@tonic-gate 48950Sstevel@tonic-gate if (halting != -1) { 48960Sstevel@tonic-gate if (non_subgraph_svcs > 1) 48970Sstevel@tonic-gate uu_warn("%d system services are now being stopped.\n", 48980Sstevel@tonic-gate non_subgraph_svcs); 48990Sstevel@tonic-gate else if (non_subgraph_svcs == 1) 49000Sstevel@tonic-gate uu_warn("One system service is now being stopped.\n"); 49010Sstevel@tonic-gate else if (non_subgraph_svcs == 0) 49020Sstevel@tonic-gate do_uadmin(); 49030Sstevel@tonic-gate } 49040Sstevel@tonic-gate 49050Sstevel@tonic-gate ret = rebound ? ECONNRESET : 0; 49060Sstevel@tonic-gate 49070Sstevel@tonic-gate out: 49080Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 49090Sstevel@tonic-gate if (!isall && !isnone) 49100Sstevel@tonic-gate startd_free((void *)cfmri, max_scf_fmri_size); 49110Sstevel@tonic-gate scf_instance_destroy(inst); 49120Sstevel@tonic-gate return (ret); 49130Sstevel@tonic-gate } 49140Sstevel@tonic-gate 49150Sstevel@tonic-gate 49160Sstevel@tonic-gate /* 49170Sstevel@tonic-gate * Returns 0, ECONNABORTED, or EINVAL. 49180Sstevel@tonic-gate */ 49190Sstevel@tonic-gate static int 49200Sstevel@tonic-gate handle_graph_update_event(scf_handle_t *h, graph_protocol_event_t *e) 49210Sstevel@tonic-gate { 49220Sstevel@tonic-gate int r; 49230Sstevel@tonic-gate 49240Sstevel@tonic-gate switch (e->gpe_type) { 49250Sstevel@tonic-gate case GRAPH_UPDATE_RELOAD_GRAPH: 49260Sstevel@tonic-gate log_error(LOG_WARNING, 49270Sstevel@tonic-gate "graph_event: reload graph unimplemented\n"); 49280Sstevel@tonic-gate break; 49290Sstevel@tonic-gate 49300Sstevel@tonic-gate case GRAPH_UPDATE_STATE_CHANGE: { 49310Sstevel@tonic-gate protocol_states_t *states = e->gpe_data; 49320Sstevel@tonic-gate 49330Sstevel@tonic-gate switch (r = dgraph_set_instance_state(h, e->gpe_inst, 49340Sstevel@tonic-gate states->ps_state, states->ps_err)) { 49350Sstevel@tonic-gate case 0: 49360Sstevel@tonic-gate case ENOENT: 49370Sstevel@tonic-gate break; 49380Sstevel@tonic-gate 49390Sstevel@tonic-gate case ECONNABORTED: 49400Sstevel@tonic-gate return (ECONNABORTED); 49410Sstevel@tonic-gate 49420Sstevel@tonic-gate case EINVAL: 49430Sstevel@tonic-gate default: 49440Sstevel@tonic-gate #ifndef NDEBUG 49450Sstevel@tonic-gate (void) fprintf(stderr, "dgraph_set_instance_state() " 49460Sstevel@tonic-gate "failed with unexpected error %d at %s:%d.\n", r, 49470Sstevel@tonic-gate __FILE__, __LINE__); 49480Sstevel@tonic-gate #endif 49490Sstevel@tonic-gate abort(); 49500Sstevel@tonic-gate } 49510Sstevel@tonic-gate 49520Sstevel@tonic-gate startd_free(states, sizeof (protocol_states_t)); 49530Sstevel@tonic-gate break; 49540Sstevel@tonic-gate } 49550Sstevel@tonic-gate 49560Sstevel@tonic-gate default: 49570Sstevel@tonic-gate log_error(LOG_WARNING, 49580Sstevel@tonic-gate "graph_event_loop received an unknown event: %d\n", 49590Sstevel@tonic-gate e->gpe_type); 49600Sstevel@tonic-gate break; 49610Sstevel@tonic-gate } 49620Sstevel@tonic-gate 49630Sstevel@tonic-gate return (0); 49640Sstevel@tonic-gate } 49650Sstevel@tonic-gate 49660Sstevel@tonic-gate /* 49670Sstevel@tonic-gate * graph_event_thread() 49680Sstevel@tonic-gate * Wait for state changes from the restarters. 49690Sstevel@tonic-gate */ 49700Sstevel@tonic-gate /*ARGSUSED*/ 49710Sstevel@tonic-gate void * 49720Sstevel@tonic-gate graph_event_thread(void *unused) 49730Sstevel@tonic-gate { 49740Sstevel@tonic-gate scf_handle_t *h; 49750Sstevel@tonic-gate int err; 49760Sstevel@tonic-gate 49770Sstevel@tonic-gate h = libscf_handle_create_bound_loop(); 49780Sstevel@tonic-gate 49790Sstevel@tonic-gate /*CONSTCOND*/ 49800Sstevel@tonic-gate while (1) { 49810Sstevel@tonic-gate graph_protocol_event_t *e; 49820Sstevel@tonic-gate 49830Sstevel@tonic-gate MUTEX_LOCK(&gu->gu_lock); 49840Sstevel@tonic-gate 49850Sstevel@tonic-gate while (gu->gu_wakeup == 0) 49860Sstevel@tonic-gate (void) pthread_cond_wait(&gu->gu_cv, &gu->gu_lock); 49870Sstevel@tonic-gate 49880Sstevel@tonic-gate gu->gu_wakeup = 0; 49890Sstevel@tonic-gate 49900Sstevel@tonic-gate while ((e = graph_event_dequeue()) != NULL) { 49910Sstevel@tonic-gate MUTEX_LOCK(&e->gpe_lock); 49920Sstevel@tonic-gate MUTEX_UNLOCK(&gu->gu_lock); 49930Sstevel@tonic-gate 49940Sstevel@tonic-gate while ((err = handle_graph_update_event(h, e)) == 49950Sstevel@tonic-gate ECONNABORTED) 49960Sstevel@tonic-gate libscf_handle_rebind(h); 49970Sstevel@tonic-gate 49980Sstevel@tonic-gate if (err == 0) 49990Sstevel@tonic-gate graph_event_release(e); 50000Sstevel@tonic-gate else 50010Sstevel@tonic-gate graph_event_requeue(e); 50020Sstevel@tonic-gate 50030Sstevel@tonic-gate MUTEX_LOCK(&gu->gu_lock); 50040Sstevel@tonic-gate } 50050Sstevel@tonic-gate 50060Sstevel@tonic-gate MUTEX_UNLOCK(&gu->gu_lock); 50070Sstevel@tonic-gate } 50080Sstevel@tonic-gate 50090Sstevel@tonic-gate /* 50100Sstevel@tonic-gate * Unreachable for now -- there's currently no graceful cleanup 50110Sstevel@tonic-gate * called on exit(). 50120Sstevel@tonic-gate */ 50130Sstevel@tonic-gate MUTEX_UNLOCK(&gu->gu_lock); 50140Sstevel@tonic-gate scf_handle_destroy(h); 50150Sstevel@tonic-gate return (NULL); 50160Sstevel@tonic-gate } 50170Sstevel@tonic-gate 50180Sstevel@tonic-gate static void 50190Sstevel@tonic-gate set_initial_milestone(scf_handle_t *h) 50200Sstevel@tonic-gate { 50210Sstevel@tonic-gate scf_instance_t *inst; 50220Sstevel@tonic-gate char *fmri, *cfmri; 50230Sstevel@tonic-gate size_t sz; 50240Sstevel@tonic-gate int r; 50250Sstevel@tonic-gate 50260Sstevel@tonic-gate inst = safe_scf_instance_create(h); 50270Sstevel@tonic-gate fmri = startd_alloc(max_scf_fmri_size); 50280Sstevel@tonic-gate 50290Sstevel@tonic-gate /* 50300Sstevel@tonic-gate * If -m milestone= was specified, we want to set options_ovr/milestone 50310Sstevel@tonic-gate * to it. Otherwise we want to read what the milestone should be set 50320Sstevel@tonic-gate * to. Either way we need our inst. 50330Sstevel@tonic-gate */ 50340Sstevel@tonic-gate get_self: 50350Sstevel@tonic-gate if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, inst, 50360Sstevel@tonic-gate NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) { 50370Sstevel@tonic-gate switch (scf_error()) { 50380Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 50390Sstevel@tonic-gate libscf_handle_rebind(h); 50400Sstevel@tonic-gate goto get_self; 50410Sstevel@tonic-gate 50420Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 50430Sstevel@tonic-gate if (st->st_subgraph != NULL && 50440Sstevel@tonic-gate st->st_subgraph[0] != '\0') { 50450Sstevel@tonic-gate sz = strlcpy(fmri, st->st_subgraph, 50460Sstevel@tonic-gate max_scf_fmri_size); 50470Sstevel@tonic-gate assert(sz < max_scf_fmri_size); 50480Sstevel@tonic-gate } else { 50490Sstevel@tonic-gate fmri[0] = '\0'; 50500Sstevel@tonic-gate } 50510Sstevel@tonic-gate break; 50520Sstevel@tonic-gate 50530Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 50540Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED: 50550Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 50560Sstevel@tonic-gate default: 50570Sstevel@tonic-gate bad_error("scf_handle_decode_fmri", scf_error()); 50580Sstevel@tonic-gate } 50590Sstevel@tonic-gate } else { 50600Sstevel@tonic-gate if (st->st_subgraph != NULL && st->st_subgraph[0] != '\0') { 50610Sstevel@tonic-gate scf_propertygroup_t *pg; 50620Sstevel@tonic-gate 50630Sstevel@tonic-gate pg = safe_scf_pg_create(h); 50640Sstevel@tonic-gate 50650Sstevel@tonic-gate sz = strlcpy(fmri, st->st_subgraph, max_scf_fmri_size); 50660Sstevel@tonic-gate assert(sz < max_scf_fmri_size); 50670Sstevel@tonic-gate 50680Sstevel@tonic-gate r = libscf_inst_get_or_add_pg(inst, SCF_PG_OPTIONS_OVR, 50690Sstevel@tonic-gate SCF_PG_OPTIONS_OVR_TYPE, SCF_PG_OPTIONS_OVR_FLAGS, 50700Sstevel@tonic-gate pg); 50710Sstevel@tonic-gate switch (r) { 50720Sstevel@tonic-gate case 0: 50730Sstevel@tonic-gate break; 50740Sstevel@tonic-gate 50750Sstevel@tonic-gate case ECONNABORTED: 50760Sstevel@tonic-gate libscf_handle_rebind(h); 50770Sstevel@tonic-gate goto get_self; 50780Sstevel@tonic-gate 50790Sstevel@tonic-gate case EPERM: 50800Sstevel@tonic-gate case EACCES: 50810Sstevel@tonic-gate case EROFS: 50820Sstevel@tonic-gate log_error(LOG_WARNING, "Could not set %s/%s: " 50830Sstevel@tonic-gate "%s.\n", SCF_PG_OPTIONS_OVR, 50840Sstevel@tonic-gate SCF_PROPERTY_MILESTONE, strerror(r)); 50850Sstevel@tonic-gate /* FALLTHROUGH */ 50860Sstevel@tonic-gate 50870Sstevel@tonic-gate case ECANCELED: 50880Sstevel@tonic-gate sz = strlcpy(fmri, st->st_subgraph, 50890Sstevel@tonic-gate max_scf_fmri_size); 50900Sstevel@tonic-gate assert(sz < max_scf_fmri_size); 50910Sstevel@tonic-gate break; 50920Sstevel@tonic-gate 50930Sstevel@tonic-gate default: 50940Sstevel@tonic-gate bad_error("libscf_inst_get_or_add_pg", r); 50950Sstevel@tonic-gate } 50960Sstevel@tonic-gate 50970Sstevel@tonic-gate r = libscf_clear_runlevel(pg, fmri); 50980Sstevel@tonic-gate switch (r) { 50990Sstevel@tonic-gate case 0: 51000Sstevel@tonic-gate break; 51010Sstevel@tonic-gate 51020Sstevel@tonic-gate case ECONNABORTED: 51030Sstevel@tonic-gate libscf_handle_rebind(h); 51040Sstevel@tonic-gate goto get_self; 51050Sstevel@tonic-gate 51060Sstevel@tonic-gate case EPERM: 51070Sstevel@tonic-gate case EACCES: 51080Sstevel@tonic-gate case EROFS: 51090Sstevel@tonic-gate log_error(LOG_WARNING, "Could not set %s/%s: " 51100Sstevel@tonic-gate "%s.\n", SCF_PG_OPTIONS_OVR, 51110Sstevel@tonic-gate SCF_PROPERTY_MILESTONE, strerror(r)); 51120Sstevel@tonic-gate /* FALLTHROUGH */ 51130Sstevel@tonic-gate 51140Sstevel@tonic-gate case ECANCELED: 51150Sstevel@tonic-gate sz = strlcpy(fmri, st->st_subgraph, 51160Sstevel@tonic-gate max_scf_fmri_size); 51170Sstevel@tonic-gate assert(sz < max_scf_fmri_size); 51180Sstevel@tonic-gate break; 51190Sstevel@tonic-gate 51200Sstevel@tonic-gate default: 51210Sstevel@tonic-gate bad_error("libscf_clear_runlevel", r); 51220Sstevel@tonic-gate } 51230Sstevel@tonic-gate 51240Sstevel@tonic-gate scf_pg_destroy(pg); 51250Sstevel@tonic-gate } else { 51260Sstevel@tonic-gate scf_property_t *prop; 51270Sstevel@tonic-gate scf_value_t *val; 51280Sstevel@tonic-gate 51290Sstevel@tonic-gate prop = safe_scf_property_create(h); 51300Sstevel@tonic-gate val = safe_scf_value_create(h); 51310Sstevel@tonic-gate 51320Sstevel@tonic-gate r = libscf_get_milestone(inst, prop, val, fmri, 51330Sstevel@tonic-gate max_scf_fmri_size); 51340Sstevel@tonic-gate switch (r) { 51350Sstevel@tonic-gate case 0: 51360Sstevel@tonic-gate break; 51370Sstevel@tonic-gate 51380Sstevel@tonic-gate case ECONNABORTED: 51390Sstevel@tonic-gate libscf_handle_rebind(h); 51400Sstevel@tonic-gate goto get_self; 51410Sstevel@tonic-gate 51420Sstevel@tonic-gate case EINVAL: 51430Sstevel@tonic-gate log_error(LOG_WARNING, "Milestone property is " 51440Sstevel@tonic-gate "misconfigured. Defaulting to \"all\".\n"); 51450Sstevel@tonic-gate /* FALLTHROUGH */ 51460Sstevel@tonic-gate 51470Sstevel@tonic-gate case ECANCELED: 51480Sstevel@tonic-gate case ENOENT: 51490Sstevel@tonic-gate fmri[0] = '\0'; 51500Sstevel@tonic-gate break; 51510Sstevel@tonic-gate 51520Sstevel@tonic-gate default: 51530Sstevel@tonic-gate bad_error("libscf_get_milestone", r); 51540Sstevel@tonic-gate } 51550Sstevel@tonic-gate 51560Sstevel@tonic-gate scf_value_destroy(val); 51570Sstevel@tonic-gate scf_property_destroy(prop); 51580Sstevel@tonic-gate } 51590Sstevel@tonic-gate } 51600Sstevel@tonic-gate 51610Sstevel@tonic-gate if (fmri[0] == '\0' || strcmp(fmri, "all") == 0) 51620Sstevel@tonic-gate goto out; 51630Sstevel@tonic-gate 51640Sstevel@tonic-gate if (strcmp(fmri, "none") != 0) { 51650Sstevel@tonic-gate retry: 51660Sstevel@tonic-gate if (scf_handle_decode_fmri(h, fmri, NULL, NULL, inst, NULL, 51670Sstevel@tonic-gate NULL, SCF_DECODE_FMRI_EXACT) != 0) { 51680Sstevel@tonic-gate switch (scf_error()) { 51690Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 51700Sstevel@tonic-gate log_error(LOG_WARNING, 51710Sstevel@tonic-gate "Requested milestone \"%s\" is invalid. " 51720Sstevel@tonic-gate "Reverting to \"all\".\n", fmri); 51730Sstevel@tonic-gate goto out; 51740Sstevel@tonic-gate 51750Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED: 51760Sstevel@tonic-gate log_error(LOG_WARNING, "Requested milestone " 51770Sstevel@tonic-gate "\"%s\" does not specify an instance. " 51780Sstevel@tonic-gate "Reverting to \"all\".\n", fmri); 51790Sstevel@tonic-gate goto out; 51800Sstevel@tonic-gate 51810Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 51820Sstevel@tonic-gate libscf_handle_rebind(h); 51830Sstevel@tonic-gate goto retry; 51840Sstevel@tonic-gate 51850Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 51860Sstevel@tonic-gate log_error(LOG_WARNING, "Requested milestone " 51870Sstevel@tonic-gate "\"%s\" not in repository. Reverting to " 51880Sstevel@tonic-gate "\"all\".\n", fmri); 51890Sstevel@tonic-gate goto out; 51900Sstevel@tonic-gate 51910Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 51920Sstevel@tonic-gate default: 51930Sstevel@tonic-gate bad_error("scf_handle_decode_fmri", 51940Sstevel@tonic-gate scf_error()); 51950Sstevel@tonic-gate } 51960Sstevel@tonic-gate } 51970Sstevel@tonic-gate 51980Sstevel@tonic-gate r = fmri_canonify(fmri, &cfmri, B_FALSE); 51990Sstevel@tonic-gate assert(r == 0); 52000Sstevel@tonic-gate 52010Sstevel@tonic-gate r = dgraph_add_instance(cfmri, inst, B_TRUE); 52020Sstevel@tonic-gate startd_free(cfmri, max_scf_fmri_size); 52030Sstevel@tonic-gate switch (r) { 52040Sstevel@tonic-gate case 0: 52050Sstevel@tonic-gate break; 52060Sstevel@tonic-gate 52070Sstevel@tonic-gate case ECONNABORTED: 52080Sstevel@tonic-gate goto retry; 52090Sstevel@tonic-gate 52100Sstevel@tonic-gate case EINVAL: 52110Sstevel@tonic-gate log_error(LOG_WARNING, 52120Sstevel@tonic-gate "Requested milestone \"%s\" is invalid. " 52130Sstevel@tonic-gate "Reverting to \"all\".\n", fmri); 52140Sstevel@tonic-gate goto out; 52150Sstevel@tonic-gate 52160Sstevel@tonic-gate case ECANCELED: 52170Sstevel@tonic-gate log_error(LOG_WARNING, 52180Sstevel@tonic-gate "Requested milestone \"%s\" not " 52190Sstevel@tonic-gate "in repository. Reverting to \"all\".\n", 52200Sstevel@tonic-gate fmri); 52210Sstevel@tonic-gate goto out; 52220Sstevel@tonic-gate 52230Sstevel@tonic-gate case EEXIST: 52240Sstevel@tonic-gate default: 52250Sstevel@tonic-gate bad_error("dgraph_add_instance", r); 52260Sstevel@tonic-gate } 52270Sstevel@tonic-gate } 52280Sstevel@tonic-gate 52290Sstevel@tonic-gate log_console(LOG_INFO, "Booting to milestone \"%s\".\n", fmri); 52300Sstevel@tonic-gate 52310Sstevel@tonic-gate r = dgraph_set_milestone(fmri, h, B_FALSE); 52320Sstevel@tonic-gate switch (r) { 52330Sstevel@tonic-gate case 0: 52340Sstevel@tonic-gate case ECONNRESET: 52350Sstevel@tonic-gate case EALREADY: 52360Sstevel@tonic-gate break; 52370Sstevel@tonic-gate 52380Sstevel@tonic-gate case EINVAL: 52390Sstevel@tonic-gate case ENOENT: 52400Sstevel@tonic-gate default: 52410Sstevel@tonic-gate bad_error("dgraph_set_milestone", r); 52420Sstevel@tonic-gate } 52430Sstevel@tonic-gate 52440Sstevel@tonic-gate out: 52450Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size); 52460Sstevel@tonic-gate scf_instance_destroy(inst); 52470Sstevel@tonic-gate } 52480Sstevel@tonic-gate 52490Sstevel@tonic-gate void 52500Sstevel@tonic-gate set_restart_milestone(scf_handle_t *h) 52510Sstevel@tonic-gate { 52520Sstevel@tonic-gate scf_instance_t *inst; 52530Sstevel@tonic-gate scf_property_t *prop; 52540Sstevel@tonic-gate scf_value_t *val; 52550Sstevel@tonic-gate char *fmri; 52560Sstevel@tonic-gate int r; 52570Sstevel@tonic-gate 52580Sstevel@tonic-gate inst = safe_scf_instance_create(h); 52590Sstevel@tonic-gate 52600Sstevel@tonic-gate get_self: 52610Sstevel@tonic-gate if (scf_handle_decode_fmri(h, SCF_SERVICE_STARTD, NULL, NULL, 52620Sstevel@tonic-gate inst, NULL, NULL, SCF_DECODE_FMRI_EXACT) != 0) { 52630Sstevel@tonic-gate switch (scf_error()) { 52640Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 52650Sstevel@tonic-gate libscf_handle_rebind(h); 52660Sstevel@tonic-gate goto get_self; 52670Sstevel@tonic-gate 52680Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 52690Sstevel@tonic-gate break; 52700Sstevel@tonic-gate 52710Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 52720Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED: 52730Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 52740Sstevel@tonic-gate default: 52750Sstevel@tonic-gate bad_error("scf_handle_decode_fmri", scf_error()); 52760Sstevel@tonic-gate } 52770Sstevel@tonic-gate 52780Sstevel@tonic-gate scf_instance_destroy(inst); 52790Sstevel@tonic-gate return; 52800Sstevel@tonic-gate } 52810Sstevel@tonic-gate 52820Sstevel@tonic-gate prop = safe_scf_property_create(h); 52830Sstevel@tonic-gate val = safe_scf_value_create(h); 52840Sstevel@tonic-gate fmri = startd_alloc(max_scf_fmri_size); 52850Sstevel@tonic-gate 52860Sstevel@tonic-gate r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size); 52870Sstevel@tonic-gate switch (r) { 52880Sstevel@tonic-gate case 0: 52890Sstevel@tonic-gate break; 52900Sstevel@tonic-gate 52910Sstevel@tonic-gate case ECONNABORTED: 52920Sstevel@tonic-gate libscf_handle_rebind(h); 52930Sstevel@tonic-gate goto get_self; 52940Sstevel@tonic-gate 52950Sstevel@tonic-gate case ECANCELED: 52960Sstevel@tonic-gate case ENOENT: 52970Sstevel@tonic-gate case EINVAL: 52980Sstevel@tonic-gate goto out; 52990Sstevel@tonic-gate 53000Sstevel@tonic-gate default: 53010Sstevel@tonic-gate bad_error("libscf_get_milestone", r); 53020Sstevel@tonic-gate } 53030Sstevel@tonic-gate 53040Sstevel@tonic-gate r = dgraph_set_milestone(fmri, h, B_TRUE); 53050Sstevel@tonic-gate switch (r) { 53060Sstevel@tonic-gate case 0: 53070Sstevel@tonic-gate case ECONNRESET: 53080Sstevel@tonic-gate case EALREADY: 53090Sstevel@tonic-gate case EINVAL: 53100Sstevel@tonic-gate case ENOENT: 53110Sstevel@tonic-gate break; 53120Sstevel@tonic-gate 53130Sstevel@tonic-gate default: 53140Sstevel@tonic-gate bad_error("dgraph_set_milestone", r); 53150Sstevel@tonic-gate } 53160Sstevel@tonic-gate 53170Sstevel@tonic-gate out: 53180Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size); 53190Sstevel@tonic-gate scf_value_destroy(val); 53200Sstevel@tonic-gate scf_property_destroy(prop); 53210Sstevel@tonic-gate scf_instance_destroy(inst); 53220Sstevel@tonic-gate } 53230Sstevel@tonic-gate 53240Sstevel@tonic-gate /* 53250Sstevel@tonic-gate * void *graph_thread(void *) 53260Sstevel@tonic-gate * 53270Sstevel@tonic-gate * Graph management thread. 53280Sstevel@tonic-gate */ 53290Sstevel@tonic-gate /*ARGSUSED*/ 53300Sstevel@tonic-gate void * 53310Sstevel@tonic-gate graph_thread(void *arg) 53320Sstevel@tonic-gate { 53330Sstevel@tonic-gate scf_handle_t *h; 53340Sstevel@tonic-gate int err; 53350Sstevel@tonic-gate 53360Sstevel@tonic-gate h = libscf_handle_create_bound_loop(); 53370Sstevel@tonic-gate 53380Sstevel@tonic-gate if (st->st_initial) 53390Sstevel@tonic-gate set_initial_milestone(h); 53400Sstevel@tonic-gate 53410Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 53420Sstevel@tonic-gate initial_milestone_set = B_TRUE; 53430Sstevel@tonic-gate err = pthread_cond_broadcast(&initial_milestone_cv); 53440Sstevel@tonic-gate assert(err == 0); 53450Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 53460Sstevel@tonic-gate 53470Sstevel@tonic-gate libscf_populate_graph(h); 53480Sstevel@tonic-gate 53490Sstevel@tonic-gate if (!st->st_initial) 53500Sstevel@tonic-gate set_restart_milestone(h); 53510Sstevel@tonic-gate 53520Sstevel@tonic-gate MUTEX_LOCK(&st->st_load_lock); 53530Sstevel@tonic-gate st->st_load_complete = 1; 53540Sstevel@tonic-gate (void) pthread_cond_broadcast(&st->st_load_cv); 53550Sstevel@tonic-gate MUTEX_UNLOCK(&st->st_load_lock); 53560Sstevel@tonic-gate 53570Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 53580Sstevel@tonic-gate /* 53590Sstevel@tonic-gate * Now that we've set st_load_complete we need to check can_come_up() 53600Sstevel@tonic-gate * since if we booted to a milestone, then there won't be any more 53610Sstevel@tonic-gate * state updates. 53620Sstevel@tonic-gate */ 53630Sstevel@tonic-gate if (!go_single_user_mode && !go_to_level1 && 53640Sstevel@tonic-gate halting == -1) { 53650Sstevel@tonic-gate if (!can_come_up() && !sulogin_thread_running) { 53660Sstevel@tonic-gate (void) startd_thread_create(sulogin_thread, NULL); 53670Sstevel@tonic-gate sulogin_thread_running = B_TRUE; 53680Sstevel@tonic-gate } 53690Sstevel@tonic-gate } 53700Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 53710Sstevel@tonic-gate 53720Sstevel@tonic-gate (void) pthread_mutex_lock(&gu->gu_freeze_lock); 53730Sstevel@tonic-gate 53740Sstevel@tonic-gate /*CONSTCOND*/ 53750Sstevel@tonic-gate while (1) { 53760Sstevel@tonic-gate (void) pthread_cond_wait(&gu->gu_freeze_cv, 53770Sstevel@tonic-gate &gu->gu_freeze_lock); 53780Sstevel@tonic-gate } 53790Sstevel@tonic-gate 53800Sstevel@tonic-gate /* 53810Sstevel@tonic-gate * Unreachable for now -- there's currently no graceful cleanup 53820Sstevel@tonic-gate * called on exit(). 53830Sstevel@tonic-gate */ 53840Sstevel@tonic-gate (void) pthread_mutex_unlock(&gu->gu_freeze_lock); 53850Sstevel@tonic-gate scf_handle_destroy(h); 53860Sstevel@tonic-gate 53870Sstevel@tonic-gate return (NULL); 53880Sstevel@tonic-gate } 53890Sstevel@tonic-gate 53900Sstevel@tonic-gate 53910Sstevel@tonic-gate /* 53920Sstevel@tonic-gate * int next_action() 53930Sstevel@tonic-gate * Given an array of timestamps 'a' with 'num' elements, find the 53940Sstevel@tonic-gate * lowest non-zero timestamp and return its index. If there are no 53950Sstevel@tonic-gate * non-zero elements, return -1. 53960Sstevel@tonic-gate */ 53970Sstevel@tonic-gate static int 53980Sstevel@tonic-gate next_action(hrtime_t *a, int num) 53990Sstevel@tonic-gate { 54000Sstevel@tonic-gate hrtime_t t = 0; 54010Sstevel@tonic-gate int i = 0, smallest = -1; 54020Sstevel@tonic-gate 54030Sstevel@tonic-gate for (i = 0; i < num; i++) { 54040Sstevel@tonic-gate if (t == 0) { 54050Sstevel@tonic-gate t = a[i]; 54060Sstevel@tonic-gate smallest = i; 54070Sstevel@tonic-gate } else if (a[i] != 0 && a[i] < t) { 54080Sstevel@tonic-gate t = a[i]; 54090Sstevel@tonic-gate smallest = i; 54100Sstevel@tonic-gate } 54110Sstevel@tonic-gate } 54120Sstevel@tonic-gate 54130Sstevel@tonic-gate if (t == 0) 54140Sstevel@tonic-gate return (-1); 54150Sstevel@tonic-gate else 54160Sstevel@tonic-gate return (smallest); 54170Sstevel@tonic-gate } 54180Sstevel@tonic-gate 54190Sstevel@tonic-gate /* 54200Sstevel@tonic-gate * void process_actions() 54210Sstevel@tonic-gate * Process actions requested by the administrator. Possibilities include: 54220Sstevel@tonic-gate * refresh, restart, maintenance mode off, maintenance mode on, 54230Sstevel@tonic-gate * maintenance mode immediate, and degraded. 54240Sstevel@tonic-gate * 54250Sstevel@tonic-gate * The set of pending actions is represented in the repository as a 54260Sstevel@tonic-gate * per-instance property group, with each action being a single property 54270Sstevel@tonic-gate * in that group. This property group is converted to an array, with each 54280Sstevel@tonic-gate * action type having an array slot. The actions in the array at the 54290Sstevel@tonic-gate * time process_actions() is called are acted on in the order of the 54300Sstevel@tonic-gate * timestamp (which is the value stored in the slot). A value of zero 54310Sstevel@tonic-gate * indicates that there is no pending action of the type associated with 54320Sstevel@tonic-gate * a particular slot. 54330Sstevel@tonic-gate * 54340Sstevel@tonic-gate * Sending an action event multiple times before the restarter has a 54350Sstevel@tonic-gate * chance to process that action will force it to be run at the last 54360Sstevel@tonic-gate * timestamp where it appears in the ordering. 54370Sstevel@tonic-gate * 54380Sstevel@tonic-gate * Turning maintenance mode on trumps all other actions. 54390Sstevel@tonic-gate * 54400Sstevel@tonic-gate * Returns 0 or ECONNABORTED. 54410Sstevel@tonic-gate */ 54420Sstevel@tonic-gate static int 54430Sstevel@tonic-gate process_actions(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst) 54440Sstevel@tonic-gate { 54450Sstevel@tonic-gate scf_property_t *prop = NULL; 54460Sstevel@tonic-gate scf_value_t *val = NULL; 54470Sstevel@tonic-gate scf_type_t type; 54480Sstevel@tonic-gate graph_vertex_t *vertex; 54490Sstevel@tonic-gate admin_action_t a; 54500Sstevel@tonic-gate int i, ret = 0, r; 54510Sstevel@tonic-gate hrtime_t action_ts[NACTIONS]; 54520Sstevel@tonic-gate char *inst_name; 54530Sstevel@tonic-gate 54540Sstevel@tonic-gate r = libscf_instance_get_fmri(inst, &inst_name); 54550Sstevel@tonic-gate switch (r) { 54560Sstevel@tonic-gate case 0: 54570Sstevel@tonic-gate break; 54580Sstevel@tonic-gate 54590Sstevel@tonic-gate case ECONNABORTED: 54600Sstevel@tonic-gate return (ECONNABORTED); 54610Sstevel@tonic-gate 54620Sstevel@tonic-gate case ECANCELED: 54630Sstevel@tonic-gate return (0); 54640Sstevel@tonic-gate 54650Sstevel@tonic-gate default: 54660Sstevel@tonic-gate bad_error("libscf_instance_get_fmri", r); 54670Sstevel@tonic-gate } 54680Sstevel@tonic-gate 54690Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 54700Sstevel@tonic-gate 54710Sstevel@tonic-gate vertex = vertex_get_by_name(inst_name); 54720Sstevel@tonic-gate if (vertex == NULL) { 54730Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 54740Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: Can't find graph vertex. " 54750Sstevel@tonic-gate "The instance must have been removed.\n", inst_name); 54760Sstevel@tonic-gate return (0); 54770Sstevel@tonic-gate } 54780Sstevel@tonic-gate 54790Sstevel@tonic-gate prop = safe_scf_property_create(h); 54800Sstevel@tonic-gate val = safe_scf_value_create(h); 54810Sstevel@tonic-gate 54820Sstevel@tonic-gate for (i = 0; i < NACTIONS; i++) { 54830Sstevel@tonic-gate if (scf_pg_get_property(pg, admin_actions[i], prop) != 0) { 54840Sstevel@tonic-gate switch (scf_error()) { 54850Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 54860Sstevel@tonic-gate default: 54870Sstevel@tonic-gate ret = ECONNABORTED; 54880Sstevel@tonic-gate goto out; 54890Sstevel@tonic-gate 54900Sstevel@tonic-gate case SCF_ERROR_DELETED: 54910Sstevel@tonic-gate goto out; 54920Sstevel@tonic-gate 54930Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 54940Sstevel@tonic-gate action_ts[i] = 0; 54950Sstevel@tonic-gate continue; 54960Sstevel@tonic-gate 54970Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 54980Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 54990Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 55000Sstevel@tonic-gate bad_error("scf_pg_get_property", scf_error()); 55010Sstevel@tonic-gate } 55020Sstevel@tonic-gate } 55030Sstevel@tonic-gate 55040Sstevel@tonic-gate if (scf_property_type(prop, &type) != 0) { 55050Sstevel@tonic-gate switch (scf_error()) { 55060Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 55070Sstevel@tonic-gate default: 55080Sstevel@tonic-gate ret = ECONNABORTED; 55090Sstevel@tonic-gate goto out; 55100Sstevel@tonic-gate 55110Sstevel@tonic-gate case SCF_ERROR_DELETED: 55120Sstevel@tonic-gate action_ts[i] = 0; 55130Sstevel@tonic-gate continue; 55140Sstevel@tonic-gate 55150Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 55160Sstevel@tonic-gate bad_error("scf_property_type", scf_error()); 55170Sstevel@tonic-gate } 55180Sstevel@tonic-gate } 55190Sstevel@tonic-gate 55200Sstevel@tonic-gate if (type != SCF_TYPE_INTEGER) { 55210Sstevel@tonic-gate action_ts[i] = 0; 55220Sstevel@tonic-gate continue; 55230Sstevel@tonic-gate } 55240Sstevel@tonic-gate 55250Sstevel@tonic-gate if (scf_property_get_value(prop, val) != 0) { 55260Sstevel@tonic-gate switch (scf_error()) { 55270Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 55280Sstevel@tonic-gate default: 55290Sstevel@tonic-gate ret = ECONNABORTED; 55300Sstevel@tonic-gate goto out; 55310Sstevel@tonic-gate 55320Sstevel@tonic-gate case SCF_ERROR_DELETED: 55330Sstevel@tonic-gate goto out; 55340Sstevel@tonic-gate 55350Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 55360Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED: 55370Sstevel@tonic-gate action_ts[i] = 0; 55380Sstevel@tonic-gate continue; 55390Sstevel@tonic-gate 55400Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 55410Sstevel@tonic-gate bad_error("scf_property_get_value", 55420Sstevel@tonic-gate scf_error()); 55430Sstevel@tonic-gate } 55440Sstevel@tonic-gate } 55450Sstevel@tonic-gate 55460Sstevel@tonic-gate r = scf_value_get_integer(val, &action_ts[i]); 55470Sstevel@tonic-gate assert(r == 0); 55480Sstevel@tonic-gate } 55490Sstevel@tonic-gate 55500Sstevel@tonic-gate a = ADMIN_EVENT_MAINT_ON_IMMEDIATE; 55510Sstevel@tonic-gate if (action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] || 55520Sstevel@tonic-gate action_ts[ADMIN_EVENT_MAINT_ON]) { 55530Sstevel@tonic-gate a = action_ts[ADMIN_EVENT_MAINT_ON_IMMEDIATE] ? 55540Sstevel@tonic-gate ADMIN_EVENT_MAINT_ON_IMMEDIATE : ADMIN_EVENT_MAINT_ON; 55550Sstevel@tonic-gate 55560Sstevel@tonic-gate vertex_send_event(vertex, admin_events[a]); 55570Sstevel@tonic-gate r = libscf_unset_action(h, pg, a, action_ts[a]); 55580Sstevel@tonic-gate switch (r) { 55590Sstevel@tonic-gate case 0: 55600Sstevel@tonic-gate case EACCES: 55610Sstevel@tonic-gate break; 55620Sstevel@tonic-gate 55630Sstevel@tonic-gate case ECONNABORTED: 55640Sstevel@tonic-gate ret = ECONNABORTED; 55650Sstevel@tonic-gate goto out; 55660Sstevel@tonic-gate 55670Sstevel@tonic-gate case EPERM: 55680Sstevel@tonic-gate uu_die("Insufficient privilege.\n"); 55690Sstevel@tonic-gate /* NOTREACHED */ 55700Sstevel@tonic-gate 55710Sstevel@tonic-gate default: 55720Sstevel@tonic-gate bad_error("libscf_unset_action", r); 55730Sstevel@tonic-gate } 55740Sstevel@tonic-gate } 55750Sstevel@tonic-gate 55760Sstevel@tonic-gate while ((a = next_action(action_ts, NACTIONS)) != -1) { 55770Sstevel@tonic-gate log_framework(LOG_DEBUG, 55780Sstevel@tonic-gate "Graph: processing %s action for %s.\n", admin_actions[a], 55790Sstevel@tonic-gate inst_name); 55800Sstevel@tonic-gate 55810Sstevel@tonic-gate if (a == ADMIN_EVENT_REFRESH) { 55820Sstevel@tonic-gate r = dgraph_refresh_instance(vertex, inst); 55830Sstevel@tonic-gate switch (r) { 55840Sstevel@tonic-gate case 0: 55850Sstevel@tonic-gate case ECANCELED: 55860Sstevel@tonic-gate case EINVAL: 55870Sstevel@tonic-gate case -1: 55880Sstevel@tonic-gate break; 55890Sstevel@tonic-gate 55900Sstevel@tonic-gate case ECONNABORTED: 55910Sstevel@tonic-gate /* pg & inst are reset now, so just return. */ 55920Sstevel@tonic-gate ret = ECONNABORTED; 55930Sstevel@tonic-gate goto out; 55940Sstevel@tonic-gate 55950Sstevel@tonic-gate default: 55960Sstevel@tonic-gate bad_error("dgraph_refresh_instance", r); 55970Sstevel@tonic-gate } 55980Sstevel@tonic-gate } 55990Sstevel@tonic-gate 56000Sstevel@tonic-gate vertex_send_event(vertex, admin_events[a]); 56010Sstevel@tonic-gate 56020Sstevel@tonic-gate r = libscf_unset_action(h, pg, a, action_ts[a]); 56030Sstevel@tonic-gate switch (r) { 56040Sstevel@tonic-gate case 0: 56050Sstevel@tonic-gate case EACCES: 56060Sstevel@tonic-gate break; 56070Sstevel@tonic-gate 56080Sstevel@tonic-gate case ECONNABORTED: 56090Sstevel@tonic-gate ret = ECONNABORTED; 56100Sstevel@tonic-gate goto out; 56110Sstevel@tonic-gate 56120Sstevel@tonic-gate case EPERM: 56130Sstevel@tonic-gate uu_die("Insufficient privilege.\n"); 56140Sstevel@tonic-gate /* NOTREACHED */ 56150Sstevel@tonic-gate 56160Sstevel@tonic-gate default: 56170Sstevel@tonic-gate bad_error("libscf_unset_action", r); 56180Sstevel@tonic-gate } 56190Sstevel@tonic-gate 56200Sstevel@tonic-gate action_ts[a] = 0; 56210Sstevel@tonic-gate } 56220Sstevel@tonic-gate 56230Sstevel@tonic-gate out: 56240Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 56250Sstevel@tonic-gate 56260Sstevel@tonic-gate scf_property_destroy(prop); 56270Sstevel@tonic-gate scf_value_destroy(val); 56280Sstevel@tonic-gate startd_free(inst_name, max_scf_fmri_size); 56290Sstevel@tonic-gate return (ret); 56300Sstevel@tonic-gate } 56310Sstevel@tonic-gate 56320Sstevel@tonic-gate /* 56330Sstevel@tonic-gate * inst and pg_name are scratch space, and are unset on entry. 56340Sstevel@tonic-gate * Returns 56350Sstevel@tonic-gate * 0 - success 56360Sstevel@tonic-gate * ECONNRESET - success, but repository handle rebound 56370Sstevel@tonic-gate * ECONNABORTED - repository connection broken 56380Sstevel@tonic-gate */ 56390Sstevel@tonic-gate static int 56400Sstevel@tonic-gate process_pg_event(scf_handle_t *h, scf_propertygroup_t *pg, scf_instance_t *inst, 56410Sstevel@tonic-gate char *pg_name) 56420Sstevel@tonic-gate { 56430Sstevel@tonic-gate int r; 56440Sstevel@tonic-gate scf_property_t *prop; 56450Sstevel@tonic-gate scf_value_t *val; 56460Sstevel@tonic-gate char *fmri; 56470Sstevel@tonic-gate boolean_t rebound = B_FALSE, rebind_inst = B_FALSE; 56480Sstevel@tonic-gate 56490Sstevel@tonic-gate if (scf_pg_get_name(pg, pg_name, max_scf_value_size) < 0) { 56500Sstevel@tonic-gate switch (scf_error()) { 56510Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 56520Sstevel@tonic-gate default: 56530Sstevel@tonic-gate return (ECONNABORTED); 56540Sstevel@tonic-gate 56550Sstevel@tonic-gate case SCF_ERROR_DELETED: 56560Sstevel@tonic-gate return (0); 56570Sstevel@tonic-gate 56580Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 56590Sstevel@tonic-gate bad_error("scf_pg_get_name", scf_error()); 56600Sstevel@tonic-gate } 56610Sstevel@tonic-gate } 56620Sstevel@tonic-gate 56630Sstevel@tonic-gate if (strcmp(pg_name, SCF_PG_GENERAL) == 0 || 56640Sstevel@tonic-gate strcmp(pg_name, SCF_PG_GENERAL_OVR) == 0) { 56650Sstevel@tonic-gate r = dgraph_update_general(pg); 56660Sstevel@tonic-gate switch (r) { 56670Sstevel@tonic-gate case 0: 56680Sstevel@tonic-gate case ENOTSUP: 56690Sstevel@tonic-gate case ECANCELED: 56700Sstevel@tonic-gate return (0); 56710Sstevel@tonic-gate 56720Sstevel@tonic-gate case ECONNABORTED: 56730Sstevel@tonic-gate return (ECONNABORTED); 56740Sstevel@tonic-gate 56750Sstevel@tonic-gate case -1: 56760Sstevel@tonic-gate /* Error should have been logged. */ 56770Sstevel@tonic-gate return (0); 56780Sstevel@tonic-gate 56790Sstevel@tonic-gate default: 56800Sstevel@tonic-gate bad_error("dgraph_update_general", r); 56810Sstevel@tonic-gate } 56820Sstevel@tonic-gate } else if (strcmp(pg_name, SCF_PG_RESTARTER_ACTIONS) == 0) { 56830Sstevel@tonic-gate if (scf_pg_get_parent_instance(pg, inst) != 0) { 56840Sstevel@tonic-gate switch (scf_error()) { 56850Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 56860Sstevel@tonic-gate return (ECONNABORTED); 56870Sstevel@tonic-gate 56880Sstevel@tonic-gate case SCF_ERROR_DELETED: 56890Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED: 56900Sstevel@tonic-gate /* Ignore commands on services. */ 56910Sstevel@tonic-gate return (0); 56920Sstevel@tonic-gate 56930Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 56940Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 56950Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 56960Sstevel@tonic-gate default: 56970Sstevel@tonic-gate bad_error("scf_pg_get_parent_instance", 56980Sstevel@tonic-gate scf_error()); 56990Sstevel@tonic-gate } 57000Sstevel@tonic-gate } 57010Sstevel@tonic-gate 57020Sstevel@tonic-gate return (process_actions(h, pg, inst)); 57030Sstevel@tonic-gate } 57040Sstevel@tonic-gate 57050Sstevel@tonic-gate if (strcmp(pg_name, SCF_PG_OPTIONS) != 0 && 57060Sstevel@tonic-gate strcmp(pg_name, SCF_PG_OPTIONS_OVR) != 0) 57070Sstevel@tonic-gate return (0); 57080Sstevel@tonic-gate 57090Sstevel@tonic-gate /* 57100Sstevel@tonic-gate * We only care about the options[_ovr] property groups of our own 57110Sstevel@tonic-gate * instance, so get the fmri and compare. Plus, once we know it's 57120Sstevel@tonic-gate * correct, if the repository connection is broken we know exactly what 57130Sstevel@tonic-gate * property group we were operating on, and can look it up again. 57140Sstevel@tonic-gate */ 57150Sstevel@tonic-gate if (scf_pg_get_parent_instance(pg, inst) != 0) { 57160Sstevel@tonic-gate switch (scf_error()) { 57170Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 57180Sstevel@tonic-gate return (ECONNABORTED); 57190Sstevel@tonic-gate 57200Sstevel@tonic-gate case SCF_ERROR_DELETED: 57210Sstevel@tonic-gate case SCF_ERROR_CONSTRAINT_VIOLATED: 57220Sstevel@tonic-gate return (0); 57230Sstevel@tonic-gate 57240Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 57250Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 57260Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 57270Sstevel@tonic-gate default: 57280Sstevel@tonic-gate bad_error("scf_pg_get_parent_instance", 57290Sstevel@tonic-gate scf_error()); 57300Sstevel@tonic-gate } 57310Sstevel@tonic-gate } 57320Sstevel@tonic-gate 57330Sstevel@tonic-gate switch (r = libscf_instance_get_fmri(inst, &fmri)) { 57340Sstevel@tonic-gate case 0: 57350Sstevel@tonic-gate break; 57360Sstevel@tonic-gate 57370Sstevel@tonic-gate case ECONNABORTED: 57380Sstevel@tonic-gate return (ECONNABORTED); 57390Sstevel@tonic-gate 57400Sstevel@tonic-gate case ECANCELED: 57410Sstevel@tonic-gate return (0); 57420Sstevel@tonic-gate 57430Sstevel@tonic-gate default: 57440Sstevel@tonic-gate bad_error("libscf_instance_get_fmri", r); 57450Sstevel@tonic-gate } 57460Sstevel@tonic-gate 57470Sstevel@tonic-gate if (strcmp(fmri, SCF_SERVICE_STARTD) != 0) { 57480Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size); 57490Sstevel@tonic-gate return (0); 57500Sstevel@tonic-gate } 57510Sstevel@tonic-gate 57520Sstevel@tonic-gate prop = safe_scf_property_create(h); 57530Sstevel@tonic-gate val = safe_scf_value_create(h); 57540Sstevel@tonic-gate 57550Sstevel@tonic-gate if (strcmp(pg_name, SCF_PG_OPTIONS_OVR) == 0) { 57560Sstevel@tonic-gate /* See if we need to set the runlevel. */ 57570Sstevel@tonic-gate /* CONSTCOND */ 57580Sstevel@tonic-gate if (0) { 57590Sstevel@tonic-gate rebind_pg: 57600Sstevel@tonic-gate libscf_handle_rebind(h); 57610Sstevel@tonic-gate rebound = B_TRUE; 57620Sstevel@tonic-gate 57630Sstevel@tonic-gate r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst); 57640Sstevel@tonic-gate switch (r) { 57650Sstevel@tonic-gate case 0: 57660Sstevel@tonic-gate break; 57670Sstevel@tonic-gate 57680Sstevel@tonic-gate case ECONNABORTED: 57690Sstevel@tonic-gate goto rebind_pg; 57700Sstevel@tonic-gate 57710Sstevel@tonic-gate case ENOENT: 57720Sstevel@tonic-gate goto out; 57730Sstevel@tonic-gate 57740Sstevel@tonic-gate case EINVAL: 57750Sstevel@tonic-gate case ENOTSUP: 57760Sstevel@tonic-gate bad_error("libscf_lookup_instance", r); 57770Sstevel@tonic-gate } 57780Sstevel@tonic-gate 57790Sstevel@tonic-gate if (scf_instance_get_pg(inst, pg_name, pg) != 0) { 57800Sstevel@tonic-gate switch (scf_error()) { 57810Sstevel@tonic-gate case SCF_ERROR_DELETED: 57820Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 57830Sstevel@tonic-gate goto out; 57840Sstevel@tonic-gate 57850Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 57860Sstevel@tonic-gate goto rebind_pg; 57870Sstevel@tonic-gate 57880Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 57890Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 57900Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 57910Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 57920Sstevel@tonic-gate default: 57930Sstevel@tonic-gate bad_error("scf_instance_get_pg", 57940Sstevel@tonic-gate scf_error()); 57950Sstevel@tonic-gate } 57960Sstevel@tonic-gate } 57970Sstevel@tonic-gate } 57980Sstevel@tonic-gate 57990Sstevel@tonic-gate if (scf_pg_get_property(pg, "runlevel", prop) == 0) { 58000Sstevel@tonic-gate r = dgraph_set_runlevel(pg, prop); 58010Sstevel@tonic-gate switch (r) { 58020Sstevel@tonic-gate case ECONNRESET: 58030Sstevel@tonic-gate rebound = B_TRUE; 58040Sstevel@tonic-gate rebind_inst = B_TRUE; 58050Sstevel@tonic-gate /* FALLTHROUGH */ 58060Sstevel@tonic-gate 58070Sstevel@tonic-gate case 0: 58080Sstevel@tonic-gate break; 58090Sstevel@tonic-gate 58100Sstevel@tonic-gate case ECONNABORTED: 58110Sstevel@tonic-gate goto rebind_pg; 58120Sstevel@tonic-gate 58130Sstevel@tonic-gate case ECANCELED: 58140Sstevel@tonic-gate goto out; 58150Sstevel@tonic-gate 58160Sstevel@tonic-gate default: 58170Sstevel@tonic-gate bad_error("dgraph_set_runlevel", r); 58180Sstevel@tonic-gate } 58190Sstevel@tonic-gate } else { 58200Sstevel@tonic-gate switch (scf_error()) { 58210Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 58220Sstevel@tonic-gate default: 58230Sstevel@tonic-gate goto rebind_pg; 58240Sstevel@tonic-gate 58250Sstevel@tonic-gate case SCF_ERROR_DELETED: 58260Sstevel@tonic-gate goto out; 58270Sstevel@tonic-gate 58280Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 58290Sstevel@tonic-gate break; 58300Sstevel@tonic-gate 58310Sstevel@tonic-gate case SCF_ERROR_INVALID_ARGUMENT: 58320Sstevel@tonic-gate case SCF_ERROR_HANDLE_MISMATCH: 58330Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 58340Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 58350Sstevel@tonic-gate bad_error("scf_pg_get_property", scf_error()); 58360Sstevel@tonic-gate } 58370Sstevel@tonic-gate } 58380Sstevel@tonic-gate } 58390Sstevel@tonic-gate 58400Sstevel@tonic-gate if (rebind_inst) { 58410Sstevel@tonic-gate lookup_inst: 58420Sstevel@tonic-gate r = libscf_lookup_instance(SCF_SERVICE_STARTD, inst); 58430Sstevel@tonic-gate switch (r) { 58440Sstevel@tonic-gate case 0: 58450Sstevel@tonic-gate break; 58460Sstevel@tonic-gate 58470Sstevel@tonic-gate case ECONNABORTED: 58480Sstevel@tonic-gate libscf_handle_rebind(h); 58490Sstevel@tonic-gate rebound = B_TRUE; 58500Sstevel@tonic-gate goto lookup_inst; 58510Sstevel@tonic-gate 58520Sstevel@tonic-gate case ENOENT: 58530Sstevel@tonic-gate goto out; 58540Sstevel@tonic-gate 58550Sstevel@tonic-gate case EINVAL: 58560Sstevel@tonic-gate case ENOTSUP: 58570Sstevel@tonic-gate bad_error("libscf_lookup_instance", r); 58580Sstevel@tonic-gate } 58590Sstevel@tonic-gate } 58600Sstevel@tonic-gate 58610Sstevel@tonic-gate r = libscf_get_milestone(inst, prop, val, fmri, max_scf_fmri_size); 58620Sstevel@tonic-gate switch (r) { 58630Sstevel@tonic-gate case 0: 58640Sstevel@tonic-gate break; 58650Sstevel@tonic-gate 58660Sstevel@tonic-gate case ECONNABORTED: 58670Sstevel@tonic-gate libscf_handle_rebind(h); 58680Sstevel@tonic-gate rebound = B_TRUE; 58690Sstevel@tonic-gate goto lookup_inst; 58700Sstevel@tonic-gate 58710Sstevel@tonic-gate case EINVAL: 58720Sstevel@tonic-gate log_error(LOG_NOTICE, 58730Sstevel@tonic-gate "%s/%s property of %s is misconfigured.\n", pg_name, 58740Sstevel@tonic-gate SCF_PROPERTY_MILESTONE, SCF_SERVICE_STARTD); 58750Sstevel@tonic-gate /* FALLTHROUGH */ 58760Sstevel@tonic-gate 58770Sstevel@tonic-gate case ECANCELED: 58780Sstevel@tonic-gate case ENOENT: 58790Sstevel@tonic-gate (void) strcpy(fmri, "all"); 58800Sstevel@tonic-gate break; 58810Sstevel@tonic-gate 58820Sstevel@tonic-gate default: 58830Sstevel@tonic-gate bad_error("libscf_get_milestone", r); 58840Sstevel@tonic-gate } 58850Sstevel@tonic-gate 58860Sstevel@tonic-gate r = dgraph_set_milestone(fmri, h, B_FALSE); 58870Sstevel@tonic-gate switch (r) { 58880Sstevel@tonic-gate case 0: 58890Sstevel@tonic-gate case ECONNRESET: 58900Sstevel@tonic-gate case EALREADY: 58910Sstevel@tonic-gate break; 58920Sstevel@tonic-gate 58930Sstevel@tonic-gate case EINVAL: 58940Sstevel@tonic-gate log_error(LOG_WARNING, "Milestone %s is invalid.\n", fmri); 58950Sstevel@tonic-gate break; 58960Sstevel@tonic-gate 58970Sstevel@tonic-gate case ENOENT: 58980Sstevel@tonic-gate log_error(LOG_WARNING, "Milestone %s does not exist.\n", fmri); 58990Sstevel@tonic-gate break; 59000Sstevel@tonic-gate 59010Sstevel@tonic-gate default: 59020Sstevel@tonic-gate bad_error("dgraph_set_milestone", r); 59030Sstevel@tonic-gate } 59040Sstevel@tonic-gate 59050Sstevel@tonic-gate out: 59060Sstevel@tonic-gate startd_free(fmri, max_scf_fmri_size); 59070Sstevel@tonic-gate scf_value_destroy(val); 59080Sstevel@tonic-gate scf_property_destroy(prop); 59090Sstevel@tonic-gate 59100Sstevel@tonic-gate return (rebound ? ECONNRESET : 0); 59110Sstevel@tonic-gate } 59120Sstevel@tonic-gate 59130Sstevel@tonic-gate static void 59140Sstevel@tonic-gate process_delete(char *fmri, scf_handle_t *h) 59150Sstevel@tonic-gate { 59160Sstevel@tonic-gate char *lfmri; 59170Sstevel@tonic-gate const char *inst_name, *pg_name; 59180Sstevel@tonic-gate 59190Sstevel@tonic-gate lfmri = safe_strdup(fmri); 59200Sstevel@tonic-gate 59210Sstevel@tonic-gate /* Determine if the FMRI is a property group or instance */ 59220Sstevel@tonic-gate if (scf_parse_svc_fmri(lfmri, NULL, NULL, &inst_name, &pg_name, 59230Sstevel@tonic-gate NULL) != SCF_SUCCESS) { 59240Sstevel@tonic-gate log_error(LOG_WARNING, 59250Sstevel@tonic-gate "Received invalid FMRI \"%s\" from repository server.\n", 59260Sstevel@tonic-gate fmri); 59270Sstevel@tonic-gate } else if (inst_name != NULL && pg_name == NULL) { 59280Sstevel@tonic-gate (void) dgraph_remove_instance(fmri, h); 59290Sstevel@tonic-gate } 59300Sstevel@tonic-gate 59310Sstevel@tonic-gate free(lfmri); 59320Sstevel@tonic-gate } 59330Sstevel@tonic-gate 59340Sstevel@tonic-gate /*ARGSUSED*/ 59350Sstevel@tonic-gate void * 59360Sstevel@tonic-gate repository_event_thread(void *unused) 59370Sstevel@tonic-gate { 59380Sstevel@tonic-gate scf_handle_t *h; 59390Sstevel@tonic-gate scf_propertygroup_t *pg; 59400Sstevel@tonic-gate scf_instance_t *inst; 59410Sstevel@tonic-gate char *fmri = startd_alloc(max_scf_fmri_size); 59420Sstevel@tonic-gate char *pg_name = startd_alloc(max_scf_value_size); 59430Sstevel@tonic-gate int r; 59440Sstevel@tonic-gate 59450Sstevel@tonic-gate h = libscf_handle_create_bound_loop(); 59460Sstevel@tonic-gate 59470Sstevel@tonic-gate pg = safe_scf_pg_create(h); 59480Sstevel@tonic-gate inst = safe_scf_instance_create(h); 59490Sstevel@tonic-gate 59500Sstevel@tonic-gate retry: 59510Sstevel@tonic-gate if (_scf_notify_add_pgtype(h, SCF_GROUP_FRAMEWORK) != SCF_SUCCESS) { 59520Sstevel@tonic-gate if (scf_error() == SCF_ERROR_CONNECTION_BROKEN) { 59530Sstevel@tonic-gate libscf_handle_rebind(h); 59540Sstevel@tonic-gate } else { 59550Sstevel@tonic-gate log_error(LOG_WARNING, 59560Sstevel@tonic-gate "Couldn't set up repository notification " 59570Sstevel@tonic-gate "for property group type %s: %s\n", 59580Sstevel@tonic-gate SCF_GROUP_FRAMEWORK, scf_strerror(scf_error())); 59590Sstevel@tonic-gate 59600Sstevel@tonic-gate (void) sleep(1); 59610Sstevel@tonic-gate } 59620Sstevel@tonic-gate 59630Sstevel@tonic-gate goto retry; 59640Sstevel@tonic-gate } 59650Sstevel@tonic-gate 59660Sstevel@tonic-gate /*CONSTCOND*/ 59670Sstevel@tonic-gate while (1) { 59680Sstevel@tonic-gate ssize_t res; 59690Sstevel@tonic-gate 59700Sstevel@tonic-gate /* Note: fmri is only set on delete events. */ 59710Sstevel@tonic-gate res = _scf_notify_wait(pg, fmri, max_scf_fmri_size); 59720Sstevel@tonic-gate if (res < 0) { 59730Sstevel@tonic-gate libscf_handle_rebind(h); 59740Sstevel@tonic-gate goto retry; 59750Sstevel@tonic-gate } else if (res == 0) { 59760Sstevel@tonic-gate /* 59770Sstevel@tonic-gate * property group modified. inst and pg_name are 59780Sstevel@tonic-gate * pre-allocated scratch space. 59790Sstevel@tonic-gate */ 59800Sstevel@tonic-gate if (scf_pg_update(pg) < 0) { 59810Sstevel@tonic-gate switch (scf_error()) { 59820Sstevel@tonic-gate case SCF_ERROR_DELETED: 59830Sstevel@tonic-gate continue; 59840Sstevel@tonic-gate 59850Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 59860Sstevel@tonic-gate log_error(LOG_WARNING, 59870Sstevel@tonic-gate "Lost repository event due to " 59880Sstevel@tonic-gate "disconnection.\n"); 59890Sstevel@tonic-gate libscf_handle_rebind(h); 59900Sstevel@tonic-gate goto retry; 59910Sstevel@tonic-gate 59920Sstevel@tonic-gate case SCF_ERROR_NOT_BOUND: 59930Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 59940Sstevel@tonic-gate default: 59950Sstevel@tonic-gate bad_error("scf_pg_update", scf_error()); 59960Sstevel@tonic-gate } 59970Sstevel@tonic-gate } 59980Sstevel@tonic-gate 59990Sstevel@tonic-gate r = process_pg_event(h, pg, inst, pg_name); 60000Sstevel@tonic-gate switch (r) { 60010Sstevel@tonic-gate case 0: 60020Sstevel@tonic-gate break; 60030Sstevel@tonic-gate 60040Sstevel@tonic-gate case ECONNABORTED: 60050Sstevel@tonic-gate log_error(LOG_WARNING, "Lost repository event " 60060Sstevel@tonic-gate "due to disconnection.\n"); 60070Sstevel@tonic-gate libscf_handle_rebind(h); 60080Sstevel@tonic-gate /* FALLTHROUGH */ 60090Sstevel@tonic-gate 60100Sstevel@tonic-gate case ECONNRESET: 60110Sstevel@tonic-gate goto retry; 60120Sstevel@tonic-gate 60130Sstevel@tonic-gate default: 60140Sstevel@tonic-gate bad_error("process_pg_event", r); 60150Sstevel@tonic-gate } 60160Sstevel@tonic-gate } else { 60170Sstevel@tonic-gate /* service, instance, or pg deleted. */ 60180Sstevel@tonic-gate process_delete(fmri, h); 60190Sstevel@tonic-gate } 60200Sstevel@tonic-gate } 60210Sstevel@tonic-gate 60220Sstevel@tonic-gate /*NOTREACHED*/ 60230Sstevel@tonic-gate return (NULL); 60240Sstevel@tonic-gate } 60250Sstevel@tonic-gate 60260Sstevel@tonic-gate void 60270Sstevel@tonic-gate graph_engine_start() 60280Sstevel@tonic-gate { 60290Sstevel@tonic-gate int err; 60300Sstevel@tonic-gate 60310Sstevel@tonic-gate (void) startd_thread_create(graph_thread, NULL); 60320Sstevel@tonic-gate 60330Sstevel@tonic-gate MUTEX_LOCK(&dgraph_lock); 60340Sstevel@tonic-gate while (!initial_milestone_set) { 60350Sstevel@tonic-gate err = pthread_cond_wait(&initial_milestone_cv, &dgraph_lock); 60360Sstevel@tonic-gate assert(err == 0); 60370Sstevel@tonic-gate } 60380Sstevel@tonic-gate MUTEX_UNLOCK(&dgraph_lock); 60390Sstevel@tonic-gate 60400Sstevel@tonic-gate (void) startd_thread_create(repository_event_thread, NULL); 60410Sstevel@tonic-gate (void) startd_thread_create(graph_event_thread, NULL); 60420Sstevel@tonic-gate } 6043