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 5*1273Sgm149974 * Common Development and Distribution License (the "License"). 6*1273Sgm149974 * 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 /* 22*1273Sgm149974 * 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 * restarter.c - service manipulation 300Sstevel@tonic-gate * 310Sstevel@tonic-gate * This component manages services whose restarter is svc.startd, the standard 320Sstevel@tonic-gate * restarter. It translates restarter protocol events from the graph engine 330Sstevel@tonic-gate * into actions on processes, as a delegated restarter would do. 340Sstevel@tonic-gate * 350Sstevel@tonic-gate * The master restarter manages a number of always-running threads: 360Sstevel@tonic-gate * - restarter event thread: events from the graph engine 370Sstevel@tonic-gate * - timeout thread: thread to fire queued timeouts 380Sstevel@tonic-gate * - contract thread: thread to handle contract events 390Sstevel@tonic-gate * - wait thread: thread to handle wait-based services 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * The other threads are created as-needed: 420Sstevel@tonic-gate * - per-instance method threads 430Sstevel@tonic-gate * - per-instance event processing threads 440Sstevel@tonic-gate * 450Sstevel@tonic-gate * The interaction of all threads must result in the following conditions 460Sstevel@tonic-gate * being satisfied (on a per-instance basis): 470Sstevel@tonic-gate * - restarter events must be processed in order 480Sstevel@tonic-gate * - method execution must be serialized 490Sstevel@tonic-gate * - instance delete must be held until outstanding methods are complete 500Sstevel@tonic-gate * - contract events shouldn't be processed while a method is running 510Sstevel@tonic-gate * - timeouts should fire even when a method is running 520Sstevel@tonic-gate * 530Sstevel@tonic-gate * Service instances are represented by restarter_inst_t's and are kept in the 540Sstevel@tonic-gate * instance_list list. 550Sstevel@tonic-gate * 560Sstevel@tonic-gate * Service States 570Sstevel@tonic-gate * The current state of a service instance is kept in 580Sstevel@tonic-gate * restarter_inst_t->ri_i.i_state. If transition to a new state could take 590Sstevel@tonic-gate * some time, then before we effect the transition we set 600Sstevel@tonic-gate * restarter_inst_t->ri_i.i_next_state to the target state, and afterwards we 610Sstevel@tonic-gate * rotate i_next_state to i_state and set i_next_state to 620Sstevel@tonic-gate * RESTARTER_STATE_NONE. So usually i_next_state is _NONE when ri_lock is not 630Sstevel@tonic-gate * held. The exception is when we launch methods, which are done with 640Sstevel@tonic-gate * a separate thread. To keep any other threads from grabbing ri_lock before 650Sstevel@tonic-gate * method_thread() does, we set ri_method_thread to the thread id of the 660Sstevel@tonic-gate * method thread, and when it is nonzero any thread with a different thread id 670Sstevel@tonic-gate * waits on ri_method_cv. 680Sstevel@tonic-gate * 690Sstevel@tonic-gate * Method execution is serialized by blocking on ri_method_cv in 700Sstevel@tonic-gate * inst_lookup_by_id() and waiting for a 0 value of ri_method_thread. This 710Sstevel@tonic-gate * also prevents the instance structure from being deleted until all 720Sstevel@tonic-gate * outstanding operations such as method_thread() have finished. 730Sstevel@tonic-gate * 740Sstevel@tonic-gate * Lock ordering: 750Sstevel@tonic-gate * 760Sstevel@tonic-gate * dgraph_lock [can be held when taking:] 770Sstevel@tonic-gate * utmpx_lock 780Sstevel@tonic-gate * dictionary->dict_lock 790Sstevel@tonic-gate * st->st_load_lock 800Sstevel@tonic-gate * wait_info_lock 810Sstevel@tonic-gate * ru->restarter_update_lock 820Sstevel@tonic-gate * restarter_queue->rpeq_lock 830Sstevel@tonic-gate * instance_list.ril_lock 840Sstevel@tonic-gate * inst->ri_lock 850Sstevel@tonic-gate * st->st_configd_live_lock 860Sstevel@tonic-gate * 870Sstevel@tonic-gate * instance_list.ril_lock 880Sstevel@tonic-gate * graph_queue->gpeq_lock 890Sstevel@tonic-gate * gu->gu_lock 900Sstevel@tonic-gate * st->st_configd_live_lock 910Sstevel@tonic-gate * dictionary->dict_lock 920Sstevel@tonic-gate * inst->ri_lock 930Sstevel@tonic-gate * graph_queue->gpeq_lock 940Sstevel@tonic-gate * gu->gu_lock 950Sstevel@tonic-gate * tu->tu_lock 960Sstevel@tonic-gate * tq->tq_lock 970Sstevel@tonic-gate * inst->ri_queue_lock 980Sstevel@tonic-gate * wait_info_lock 990Sstevel@tonic-gate * bp->cb_lock 1000Sstevel@tonic-gate * utmpx_lock 1010Sstevel@tonic-gate * 1020Sstevel@tonic-gate * single_user_thread_lock 1030Sstevel@tonic-gate * wait_info_lock 1040Sstevel@tonic-gate * utmpx_lock 1050Sstevel@tonic-gate * 1060Sstevel@tonic-gate * gu_freeze_lock 1070Sstevel@tonic-gate * 1080Sstevel@tonic-gate * logbuf_mutex nests inside pretty much everything. 1090Sstevel@tonic-gate */ 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate #include <sys/contract/process.h> 1120Sstevel@tonic-gate #include <sys/ctfs.h> 1130Sstevel@tonic-gate #include <sys/stat.h> 1140Sstevel@tonic-gate #include <sys/time.h> 1150Sstevel@tonic-gate #include <sys/types.h> 1160Sstevel@tonic-gate #include <sys/uio.h> 1170Sstevel@tonic-gate #include <sys/wait.h> 1180Sstevel@tonic-gate #include <assert.h> 1190Sstevel@tonic-gate #include <errno.h> 1200Sstevel@tonic-gate #include <fcntl.h> 1210Sstevel@tonic-gate #include <libcontract.h> 1220Sstevel@tonic-gate #include <libcontract_priv.h> 1230Sstevel@tonic-gate #include <libintl.h> 1240Sstevel@tonic-gate #include <librestart.h> 1250Sstevel@tonic-gate #include <librestart_priv.h> 1260Sstevel@tonic-gate #include <libuutil.h> 1270Sstevel@tonic-gate #include <limits.h> 1280Sstevel@tonic-gate #include <poll.h> 1290Sstevel@tonic-gate #include <port.h> 1300Sstevel@tonic-gate #include <pthread.h> 1310Sstevel@tonic-gate #include <stdarg.h> 1320Sstevel@tonic-gate #include <stdio.h> 1330Sstevel@tonic-gate #include <strings.h> 1340Sstevel@tonic-gate #include <unistd.h> 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate #include "startd.h" 1370Sstevel@tonic-gate #include "protocol.h" 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate static uu_list_pool_t *restarter_instance_pool; 1400Sstevel@tonic-gate static restarter_instance_list_t instance_list; 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate static uu_list_pool_t *restarter_queue_pool; 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate /*ARGSUSED*/ 1450Sstevel@tonic-gate static int 1460Sstevel@tonic-gate restarter_instance_compare(const void *lc_arg, const void *rc_arg, 1470Sstevel@tonic-gate void *private) 1480Sstevel@tonic-gate { 1490Sstevel@tonic-gate int lc_id = ((const restarter_inst_t *)lc_arg)->ri_id; 1500Sstevel@tonic-gate int rc_id = *(int *)rc_arg; 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate if (lc_id > rc_id) 1530Sstevel@tonic-gate return (1); 1540Sstevel@tonic-gate if (lc_id < rc_id) 1550Sstevel@tonic-gate return (-1); 1560Sstevel@tonic-gate return (0); 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate static restarter_inst_t * 1600Sstevel@tonic-gate inst_lookup_by_name(const char *name) 1610Sstevel@tonic-gate { 1620Sstevel@tonic-gate int id; 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate id = dict_lookup_byname(name); 1650Sstevel@tonic-gate if (id == -1) 1660Sstevel@tonic-gate return (NULL); 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate return (inst_lookup_by_id(id)); 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate restarter_inst_t * 1720Sstevel@tonic-gate inst_lookup_by_id(int id) 1730Sstevel@tonic-gate { 1740Sstevel@tonic-gate restarter_inst_t *inst; 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate MUTEX_LOCK(&instance_list.ril_lock); 1770Sstevel@tonic-gate inst = uu_list_find(instance_list.ril_instance_list, &id, NULL, NULL); 1780Sstevel@tonic-gate if (inst != NULL) 1790Sstevel@tonic-gate MUTEX_LOCK(&inst->ri_lock); 1800Sstevel@tonic-gate MUTEX_UNLOCK(&instance_list.ril_lock); 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate if (inst != NULL) { 1830Sstevel@tonic-gate while (inst->ri_method_thread != 0 && 1840Sstevel@tonic-gate !pthread_equal(inst->ri_method_thread, pthread_self())) { 1850Sstevel@tonic-gate ++inst->ri_method_waiters; 1860Sstevel@tonic-gate (void) pthread_cond_wait(&inst->ri_method_cv, 1870Sstevel@tonic-gate &inst->ri_lock); 1880Sstevel@tonic-gate assert(inst->ri_method_waiters > 0); 1890Sstevel@tonic-gate --inst->ri_method_waiters; 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate } 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate return (inst); 1940Sstevel@tonic-gate } 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate static restarter_inst_t * 1970Sstevel@tonic-gate inst_lookup_queue(const char *name) 1980Sstevel@tonic-gate { 1990Sstevel@tonic-gate int id; 2000Sstevel@tonic-gate restarter_inst_t *inst; 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate id = dict_lookup_byname(name); 2030Sstevel@tonic-gate if (id == -1) 2040Sstevel@tonic-gate return (NULL); 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate MUTEX_LOCK(&instance_list.ril_lock); 2070Sstevel@tonic-gate inst = uu_list_find(instance_list.ril_instance_list, &id, NULL, NULL); 2080Sstevel@tonic-gate if (inst != NULL) 2090Sstevel@tonic-gate MUTEX_LOCK(&inst->ri_queue_lock); 2100Sstevel@tonic-gate MUTEX_UNLOCK(&instance_list.ril_lock); 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate return (inst); 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate const char * 2160Sstevel@tonic-gate service_style(int flags) 2170Sstevel@tonic-gate { 2180Sstevel@tonic-gate switch (flags & RINST_STYLE_MASK) { 2190Sstevel@tonic-gate case RINST_CONTRACT: return ("contract"); 2200Sstevel@tonic-gate case RINST_TRANSIENT: return ("transient"); 2210Sstevel@tonic-gate case RINST_WAIT: return ("wait"); 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate default: 2240Sstevel@tonic-gate #ifndef NDEBUG 2250Sstevel@tonic-gate uu_warn("%s:%d: Bad flags 0x%x.\n", __FILE__, __LINE__, flags); 2260Sstevel@tonic-gate #endif 2270Sstevel@tonic-gate abort(); 2280Sstevel@tonic-gate /* NOTREACHED */ 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate } 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate /* 2330Sstevel@tonic-gate * Fails with ECONNABORTED or ECANCELED. 2340Sstevel@tonic-gate */ 2350Sstevel@tonic-gate static int 2360Sstevel@tonic-gate check_contract(restarter_inst_t *inst, boolean_t primary, 2370Sstevel@tonic-gate scf_instance_t *scf_inst) 2380Sstevel@tonic-gate { 2390Sstevel@tonic-gate ctid_t *ctidp; 2400Sstevel@tonic-gate int fd, r; 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate ctidp = primary ? &inst->ri_i.i_primary_ctid : 2430Sstevel@tonic-gate &inst->ri_i.i_transient_ctid; 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate assert(*ctidp >= 1); 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate fd = contract_open(*ctidp, NULL, "status", O_RDONLY); 2480Sstevel@tonic-gate if (fd >= 0) { 2490Sstevel@tonic-gate r = close(fd); 2500Sstevel@tonic-gate assert(r == 0); 2510Sstevel@tonic-gate return (0); 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate r = restarter_remove_contract(scf_inst, *ctidp, primary ? 2550Sstevel@tonic-gate RESTARTER_CONTRACT_PRIMARY : RESTARTER_CONTRACT_TRANSIENT); 2560Sstevel@tonic-gate switch (r) { 2570Sstevel@tonic-gate case 0: 2580Sstevel@tonic-gate case ECONNABORTED: 2590Sstevel@tonic-gate case ECANCELED: 2600Sstevel@tonic-gate *ctidp = 0; 2610Sstevel@tonic-gate return (r); 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate case ENOMEM: 2640Sstevel@tonic-gate uu_die("Out of memory\n"); 2650Sstevel@tonic-gate /* NOTREACHED */ 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate case EPERM: 2680Sstevel@tonic-gate uu_die("Insufficient privilege.\n"); 2690Sstevel@tonic-gate /* NOTREACHED */ 2700Sstevel@tonic-gate 2710Sstevel@tonic-gate case EACCES: 2720Sstevel@tonic-gate uu_die("Repository backend access denied.\n"); 2730Sstevel@tonic-gate /* NOTREACHED */ 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate case EROFS: 2760Sstevel@tonic-gate log_error(LOG_INFO, "Could not remove unusable contract id %ld " 2770Sstevel@tonic-gate "for %s from repository.\n", *ctidp, inst->ri_i.i_fmri); 2780Sstevel@tonic-gate return (0); 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate case EINVAL: 2810Sstevel@tonic-gate case EBADF: 2820Sstevel@tonic-gate default: 2830Sstevel@tonic-gate assert(0); 2840Sstevel@tonic-gate abort(); 2850Sstevel@tonic-gate /* NOTREACHED */ 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate } 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate static int stop_instance(scf_handle_t *, restarter_inst_t *, stop_cause_t); 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate /* 2920Sstevel@tonic-gate * int restarter_insert_inst(scf_handle_t *, char *) 2930Sstevel@tonic-gate * If the inst is already in the restarter list, return its id. If the inst 2940Sstevel@tonic-gate * is not in the restarter list, initialize a restarter_inst_t, initialize its 2950Sstevel@tonic-gate * states, insert it into the list, and return 0. 2960Sstevel@tonic-gate * 2970Sstevel@tonic-gate * Fails with 2980Sstevel@tonic-gate * ENOENT - name is not in the repository 2990Sstevel@tonic-gate */ 3000Sstevel@tonic-gate static int 3010Sstevel@tonic-gate restarter_insert_inst(scf_handle_t *h, const char *name) 3020Sstevel@tonic-gate { 3030Sstevel@tonic-gate int id, r; 3040Sstevel@tonic-gate restarter_inst_t *inst; 3050Sstevel@tonic-gate uu_list_index_t idx; 3060Sstevel@tonic-gate scf_service_t *scf_svc; 3070Sstevel@tonic-gate scf_instance_t *scf_inst; 308837Srm88369 scf_snapshot_t *snap = NULL; 3090Sstevel@tonic-gate scf_propertygroup_t *pg; 3100Sstevel@tonic-gate char *svc_name, *inst_name; 3110Sstevel@tonic-gate char logfilebuf[PATH_MAX]; 3120Sstevel@tonic-gate char *c; 3130Sstevel@tonic-gate boolean_t do_commit_states; 3140Sstevel@tonic-gate restarter_instance_state_t state, next_state; 3150Sstevel@tonic-gate protocol_states_t *ps; 3160Sstevel@tonic-gate pid_t start_pid; 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate MUTEX_LOCK(&instance_list.ril_lock); 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate /* 3210Sstevel@tonic-gate * We don't use inst_lookup_by_name() here because we want the lookup 3220Sstevel@tonic-gate * & insert to be atomic. 3230Sstevel@tonic-gate */ 3240Sstevel@tonic-gate id = dict_lookup_byname(name); 3250Sstevel@tonic-gate if (id != -1) { 3260Sstevel@tonic-gate inst = uu_list_find(instance_list.ril_instance_list, &id, NULL, 3270Sstevel@tonic-gate &idx); 3280Sstevel@tonic-gate if (inst != NULL) { 3290Sstevel@tonic-gate MUTEX_UNLOCK(&instance_list.ril_lock); 3300Sstevel@tonic-gate return (0); 3310Sstevel@tonic-gate } 3320Sstevel@tonic-gate } 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate /* Allocate an instance */ 3350Sstevel@tonic-gate inst = startd_zalloc(sizeof (restarter_inst_t)); 3360Sstevel@tonic-gate inst->ri_utmpx_prefix = startd_alloc(max_scf_value_size); 3370Sstevel@tonic-gate inst->ri_utmpx_prefix[0] = '\0'; 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate inst->ri_i.i_fmri = startd_alloc(strlen(name) + 1); 3400Sstevel@tonic-gate (void) strcpy((char *)inst->ri_i.i_fmri, name); 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate inst->ri_queue = startd_list_create(restarter_queue_pool, inst, 0); 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate /* 3450Sstevel@tonic-gate * id shouldn't be -1 since we use the same dictionary as graph.c, but 3460Sstevel@tonic-gate * just in case. 3470Sstevel@tonic-gate */ 3480Sstevel@tonic-gate inst->ri_id = (id != -1 ? id : dict_insert(name)); 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate special_online_hooks_get(name, &inst->ri_pre_online_hook, 3510Sstevel@tonic-gate &inst->ri_post_online_hook, &inst->ri_post_offline_hook); 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate scf_svc = safe_scf_service_create(h); 3540Sstevel@tonic-gate scf_inst = safe_scf_instance_create(h); 3550Sstevel@tonic-gate pg = safe_scf_pg_create(h); 3560Sstevel@tonic-gate svc_name = startd_alloc(max_scf_name_size); 3570Sstevel@tonic-gate inst_name = startd_alloc(max_scf_name_size); 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate rep_retry: 360837Srm88369 if (snap != NULL) 361837Srm88369 scf_snapshot_destroy(snap); 362837Srm88369 if (inst->ri_logstem != NULL) 363837Srm88369 startd_free(inst->ri_logstem, PATH_MAX); 364837Srm88369 if (inst->ri_common_name != NULL) 365837Srm88369 startd_free(inst->ri_common_name, max_scf_value_size); 366837Srm88369 if (inst->ri_C_common_name != NULL) 367837Srm88369 startd_free(inst->ri_C_common_name, max_scf_value_size); 368837Srm88369 snap = NULL; 369837Srm88369 inst->ri_logstem = NULL; 370837Srm88369 inst->ri_common_name = NULL; 371837Srm88369 inst->ri_C_common_name = NULL; 372837Srm88369 3730Sstevel@tonic-gate if (scf_handle_decode_fmri(h, name, NULL, scf_svc, scf_inst, NULL, 3740Sstevel@tonic-gate NULL, SCF_DECODE_FMRI_EXACT) != 0) { 3750Sstevel@tonic-gate switch (scf_error()) { 3760Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 3770Sstevel@tonic-gate libscf_handle_rebind(h); 3780Sstevel@tonic-gate goto rep_retry; 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 381837Srm88369 goto deleted; 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate uu_die("Can't decode FMRI %s: %s\n", name, 3850Sstevel@tonic-gate scf_strerror(scf_error())); 3860Sstevel@tonic-gate } 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate /* 3890Sstevel@tonic-gate * If there's no running snapshot, then we execute using the editing 3900Sstevel@tonic-gate * snapshot. Pending snapshots will be taken later. 3910Sstevel@tonic-gate */ 3920Sstevel@tonic-gate snap = libscf_get_running_snapshot(scf_inst); 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate if ((scf_service_get_name(scf_svc, svc_name, max_scf_name_size) < 0) || 3950Sstevel@tonic-gate (scf_instance_get_name(scf_inst, inst_name, max_scf_name_size) < 3960Sstevel@tonic-gate 0)) { 3970Sstevel@tonic-gate switch (scf_error()) { 3980Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 3990Sstevel@tonic-gate break; 4000Sstevel@tonic-gate 4010Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 4020Sstevel@tonic-gate libscf_handle_rebind(h); 4030Sstevel@tonic-gate goto rep_retry; 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate default: 4060Sstevel@tonic-gate assert(0); 4070Sstevel@tonic-gate abort(); 4080Sstevel@tonic-gate } 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate goto deleted; 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate 413345Slianep (void) snprintf(logfilebuf, PATH_MAX, "%s:%s", svc_name, inst_name); 414345Slianep for (c = logfilebuf; *c != '\0'; c++) 415345Slianep if (*c == '/') 416345Slianep *c = '-'; 417345Slianep 418345Slianep inst->ri_logstem = startd_alloc(PATH_MAX); 419345Slianep (void) snprintf(inst->ri_logstem, PATH_MAX, "%s%s", logfilebuf, 420345Slianep LOG_SUFFIX); 421345Slianep 4220Sstevel@tonic-gate /* 4230Sstevel@tonic-gate * If the restarter group is missing, use uninit/none. Otherwise, 4240Sstevel@tonic-gate * we're probably being restarted & don't want to mess up the states 4250Sstevel@tonic-gate * that are there. 4260Sstevel@tonic-gate */ 4270Sstevel@tonic-gate state = RESTARTER_STATE_UNINIT; 4280Sstevel@tonic-gate next_state = RESTARTER_STATE_NONE; 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate r = scf_instance_get_pg(scf_inst, SCF_PG_RESTARTER, pg); 4310Sstevel@tonic-gate if (r != 0) { 4320Sstevel@tonic-gate switch (scf_error()) { 4330Sstevel@tonic-gate case SCF_ERROR_CONNECTION_BROKEN: 4340Sstevel@tonic-gate libscf_handle_rebind(h); 4350Sstevel@tonic-gate goto rep_retry; 4360Sstevel@tonic-gate 4370Sstevel@tonic-gate case SCF_ERROR_NOT_SET: 4380Sstevel@tonic-gate goto deleted; 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate case SCF_ERROR_NOT_FOUND: 4410Sstevel@tonic-gate /* 4420Sstevel@tonic-gate * This shouldn't happen since the graph engine should 4430Sstevel@tonic-gate * have initialized the state to uninitialized/none if 4440Sstevel@tonic-gate * there was no restarter pg. In case somebody 4450Sstevel@tonic-gate * deleted it, though.... 4460Sstevel@tonic-gate */ 4470Sstevel@tonic-gate do_commit_states = B_TRUE; 4480Sstevel@tonic-gate break; 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate default: 4510Sstevel@tonic-gate assert(0); 4520Sstevel@tonic-gate abort(); 4530Sstevel@tonic-gate } 4540Sstevel@tonic-gate } else { 4550Sstevel@tonic-gate r = libscf_read_states(pg, &state, &next_state); 4560Sstevel@tonic-gate if (r != 0) { 4570Sstevel@tonic-gate do_commit_states = B_TRUE; 4580Sstevel@tonic-gate } else { 4590Sstevel@tonic-gate if (next_state != RESTARTER_STATE_NONE) { 4600Sstevel@tonic-gate /* 4610Sstevel@tonic-gate * Force next_state to _NONE since we 4620Sstevel@tonic-gate * don't look for method processes. 4630Sstevel@tonic-gate */ 4640Sstevel@tonic-gate next_state = RESTARTER_STATE_NONE; 4650Sstevel@tonic-gate do_commit_states = B_TRUE; 4660Sstevel@tonic-gate } else { 4670Sstevel@tonic-gate /* 4680Sstevel@tonic-gate * Inform the restarter of our state without 4690Sstevel@tonic-gate * changing the STIME in the repository. 4700Sstevel@tonic-gate */ 4710Sstevel@tonic-gate ps = startd_alloc(sizeof (*ps)); 4720Sstevel@tonic-gate inst->ri_i.i_state = ps->ps_state = state; 4730Sstevel@tonic-gate inst->ri_i.i_next_state = ps->ps_state_next = 4740Sstevel@tonic-gate next_state; 4750Sstevel@tonic-gate 4760Sstevel@tonic-gate graph_protocol_send_event(inst->ri_i.i_fmri, 4770Sstevel@tonic-gate GRAPH_UPDATE_STATE_CHANGE, ps); 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate do_commit_states = B_FALSE; 4800Sstevel@tonic-gate } 4810Sstevel@tonic-gate } 4820Sstevel@tonic-gate } 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate switch (libscf_get_startd_properties(scf_inst, snap, &inst->ri_flags, 4850Sstevel@tonic-gate &inst->ri_utmpx_prefix)) { 4860Sstevel@tonic-gate case 0: 4870Sstevel@tonic-gate break; 4880Sstevel@tonic-gate 4890Sstevel@tonic-gate case ECONNABORTED: 4900Sstevel@tonic-gate libscf_handle_rebind(h); 4910Sstevel@tonic-gate goto rep_retry; 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate case ECANCELED: 4940Sstevel@tonic-gate goto deleted; 4950Sstevel@tonic-gate 4960Sstevel@tonic-gate case ENOENT: 4970Sstevel@tonic-gate /* 4980Sstevel@tonic-gate * This is odd, because the graph engine should have required 4990Sstevel@tonic-gate * the general property group. So we'll just use default 5000Sstevel@tonic-gate * flags in anticipation of the graph engine sending us 5010Sstevel@tonic-gate * REMOVE_INSTANCE when it finds out that the general property 5020Sstevel@tonic-gate * group has been deleted. 5030Sstevel@tonic-gate */ 5040Sstevel@tonic-gate inst->ri_flags = RINST_CONTRACT; 5050Sstevel@tonic-gate break; 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate default: 5080Sstevel@tonic-gate assert(0); 5090Sstevel@tonic-gate abort(); 5100Sstevel@tonic-gate } 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate switch (libscf_get_template_values(scf_inst, snap, 5130Sstevel@tonic-gate &inst->ri_common_name, &inst->ri_C_common_name)) { 5140Sstevel@tonic-gate case 0: 5150Sstevel@tonic-gate break; 5160Sstevel@tonic-gate 5170Sstevel@tonic-gate case ECONNABORTED: 5180Sstevel@tonic-gate libscf_handle_rebind(h); 5190Sstevel@tonic-gate goto rep_retry; 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate case ECANCELED: 5220Sstevel@tonic-gate goto deleted; 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate case ECHILD: 5250Sstevel@tonic-gate case ENOENT: 5260Sstevel@tonic-gate break; 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate default: 5290Sstevel@tonic-gate assert(0); 5300Sstevel@tonic-gate abort(); 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate switch (libscf_read_method_ids(h, scf_inst, inst->ri_i.i_fmri, 5340Sstevel@tonic-gate &inst->ri_i.i_primary_ctid, &inst->ri_i.i_transient_ctid, 5350Sstevel@tonic-gate &start_pid)) { 5360Sstevel@tonic-gate case 0: 5370Sstevel@tonic-gate break; 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate case ECONNABORTED: 5400Sstevel@tonic-gate libscf_handle_rebind(h); 5410Sstevel@tonic-gate goto rep_retry; 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate case ECANCELED: 5440Sstevel@tonic-gate goto deleted; 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate default: 5470Sstevel@tonic-gate assert(0); 5480Sstevel@tonic-gate abort(); 5490Sstevel@tonic-gate } 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate if (inst->ri_i.i_primary_ctid >= 1) { 5520Sstevel@tonic-gate contract_hash_store(inst->ri_i.i_primary_ctid, inst->ri_id); 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate switch (check_contract(inst, B_TRUE, scf_inst)) { 5550Sstevel@tonic-gate case 0: 5560Sstevel@tonic-gate break; 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate case ECONNABORTED: 5590Sstevel@tonic-gate libscf_handle_rebind(h); 5600Sstevel@tonic-gate goto rep_retry; 5610Sstevel@tonic-gate 5620Sstevel@tonic-gate case ECANCELED: 5630Sstevel@tonic-gate goto deleted; 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate default: 5660Sstevel@tonic-gate assert(0); 5670Sstevel@tonic-gate abort(); 5680Sstevel@tonic-gate } 5690Sstevel@tonic-gate } 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate if (inst->ri_i.i_transient_ctid >= 1) { 5720Sstevel@tonic-gate switch (check_contract(inst, B_FALSE, scf_inst)) { 5730Sstevel@tonic-gate case 0: 5740Sstevel@tonic-gate break; 5750Sstevel@tonic-gate 5760Sstevel@tonic-gate case ECONNABORTED: 5770Sstevel@tonic-gate libscf_handle_rebind(h); 5780Sstevel@tonic-gate goto rep_retry; 5790Sstevel@tonic-gate 5800Sstevel@tonic-gate case ECANCELED: 5810Sstevel@tonic-gate goto deleted; 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate default: 5840Sstevel@tonic-gate assert(0); 5850Sstevel@tonic-gate abort(); 5860Sstevel@tonic-gate } 5870Sstevel@tonic-gate } 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate /* No more failures we live through, so add it to the list. */ 5900Sstevel@tonic-gate (void) pthread_mutex_init(&inst->ri_lock, &mutex_attrs); 5910Sstevel@tonic-gate (void) pthread_mutex_init(&inst->ri_queue_lock, &mutex_attrs); 5920Sstevel@tonic-gate MUTEX_LOCK(&inst->ri_lock); 5930Sstevel@tonic-gate MUTEX_LOCK(&inst->ri_queue_lock); 5940Sstevel@tonic-gate 5950Sstevel@tonic-gate (void) pthread_cond_init(&inst->ri_method_cv, NULL); 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate uu_list_node_init(inst, &inst->ri_link, restarter_instance_pool); 5980Sstevel@tonic-gate uu_list_insert(instance_list.ril_instance_list, inst, idx); 5990Sstevel@tonic-gate MUTEX_UNLOCK(&instance_list.ril_lock); 6000Sstevel@tonic-gate 6010Sstevel@tonic-gate if (start_pid != -1 && 6020Sstevel@tonic-gate (inst->ri_flags & RINST_STYLE_MASK) == RINST_WAIT) { 6030Sstevel@tonic-gate int ret; 6040Sstevel@tonic-gate ret = wait_register(start_pid, inst->ri_i.i_fmri, 0, 1); 6050Sstevel@tonic-gate if (ret == -1) { 6060Sstevel@tonic-gate /* 6070Sstevel@tonic-gate * Implication: if we can't reregister the 6080Sstevel@tonic-gate * instance, we will start another one. Two 6090Sstevel@tonic-gate * instances may or may not result in a resource 6100Sstevel@tonic-gate * conflict. 6110Sstevel@tonic-gate */ 6120Sstevel@tonic-gate log_error(LOG_WARNING, 6130Sstevel@tonic-gate "%s: couldn't reregister %ld for wait\n", 6140Sstevel@tonic-gate inst->ri_i.i_fmri, start_pid); 6150Sstevel@tonic-gate } else if (ret == 1) { 6160Sstevel@tonic-gate /* 6170Sstevel@tonic-gate * Leading PID has exited. 6180Sstevel@tonic-gate */ 6190Sstevel@tonic-gate (void) stop_instance(h, inst, RSTOP_EXIT); 6200Sstevel@tonic-gate } 6210Sstevel@tonic-gate } 6220Sstevel@tonic-gate 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate scf_pg_destroy(pg); 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate if (do_commit_states) 6270Sstevel@tonic-gate (void) restarter_instance_update_states(h, inst, state, 6280Sstevel@tonic-gate next_state, RERR_NONE, NULL); 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s is a %s-style service\n", name, 6310Sstevel@tonic-gate service_style(inst->ri_flags)); 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate MUTEX_UNLOCK(&inst->ri_queue_lock); 6340Sstevel@tonic-gate MUTEX_UNLOCK(&inst->ri_lock); 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate startd_free(svc_name, max_scf_name_size); 6370Sstevel@tonic-gate startd_free(inst_name, max_scf_name_size); 6380Sstevel@tonic-gate scf_snapshot_destroy(snap); 6390Sstevel@tonic-gate scf_instance_destroy(scf_inst); 6400Sstevel@tonic-gate scf_service_destroy(scf_svc); 6410Sstevel@tonic-gate 6420Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: inserted instance into restarter list\n", 6430Sstevel@tonic-gate name); 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate return (0); 646837Srm88369 647837Srm88369 deleted: 648837Srm88369 MUTEX_UNLOCK(&instance_list.ril_lock); 649837Srm88369 startd_free(inst_name, max_scf_name_size); 650837Srm88369 startd_free(svc_name, max_scf_name_size); 651837Srm88369 if (snap != NULL) 652837Srm88369 scf_snapshot_destroy(snap); 653837Srm88369 scf_pg_destroy(pg); 654837Srm88369 scf_instance_destroy(scf_inst); 655837Srm88369 scf_service_destroy(scf_svc); 656837Srm88369 startd_free((void *)inst->ri_i.i_fmri, strlen(inst->ri_i.i_fmri) + 1); 657837Srm88369 uu_list_destroy(inst->ri_queue); 658837Srm88369 if (inst->ri_logstem != NULL) 659837Srm88369 startd_free(inst->ri_logstem, PATH_MAX); 660837Srm88369 if (inst->ri_common_name != NULL) 661837Srm88369 startd_free(inst->ri_common_name, max_scf_value_size); 662837Srm88369 if (inst->ri_C_common_name != NULL) 663837Srm88369 startd_free(inst->ri_C_common_name, max_scf_value_size); 664837Srm88369 startd_free(inst->ri_utmpx_prefix, max_scf_value_size); 665837Srm88369 startd_free(inst, sizeof (restarter_inst_t)); 666837Srm88369 return (ENOENT); 6670Sstevel@tonic-gate } 6680Sstevel@tonic-gate 6690Sstevel@tonic-gate static void 6700Sstevel@tonic-gate restarter_delete_inst(restarter_inst_t *ri) 6710Sstevel@tonic-gate { 6720Sstevel@tonic-gate int id; 6730Sstevel@tonic-gate restarter_inst_t *rip; 6740Sstevel@tonic-gate void *cookie = NULL; 6750Sstevel@tonic-gate restarter_instance_qentry_t *e; 6760Sstevel@tonic-gate 6770Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&ri->ri_lock)); 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate /* 6800Sstevel@tonic-gate * Must drop the instance lock so we can pick up the instance_list 6810Sstevel@tonic-gate * lock & remove the instance. 6820Sstevel@tonic-gate */ 6830Sstevel@tonic-gate id = ri->ri_id; 6840Sstevel@tonic-gate MUTEX_UNLOCK(&ri->ri_lock); 6850Sstevel@tonic-gate 6860Sstevel@tonic-gate MUTEX_LOCK(&instance_list.ril_lock); 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate rip = uu_list_find(instance_list.ril_instance_list, &id, NULL, NULL); 6890Sstevel@tonic-gate if (rip == NULL) { 6900Sstevel@tonic-gate MUTEX_UNLOCK(&instance_list.ril_lock); 6910Sstevel@tonic-gate return; 6920Sstevel@tonic-gate } 6930Sstevel@tonic-gate 6940Sstevel@tonic-gate assert(ri == rip); 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate uu_list_remove(instance_list.ril_instance_list, ri); 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: deleted instance from restarter list\n", 6990Sstevel@tonic-gate ri->ri_i.i_fmri); 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate MUTEX_UNLOCK(&instance_list.ril_lock); 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate /* 7040Sstevel@tonic-gate * We can lock the instance without holding the instance_list lock 7050Sstevel@tonic-gate * since we removed the instance from the list. 7060Sstevel@tonic-gate */ 7070Sstevel@tonic-gate MUTEX_LOCK(&ri->ri_lock); 7080Sstevel@tonic-gate MUTEX_LOCK(&ri->ri_queue_lock); 7090Sstevel@tonic-gate 7100Sstevel@tonic-gate if (ri->ri_i.i_primary_ctid >= 1) 7110Sstevel@tonic-gate contract_hash_remove(ri->ri_i.i_primary_ctid); 7120Sstevel@tonic-gate 7130Sstevel@tonic-gate while (ri->ri_method_thread != 0 || ri->ri_method_waiters > 0) 7140Sstevel@tonic-gate (void) pthread_cond_wait(&ri->ri_method_cv, &ri->ri_lock); 7150Sstevel@tonic-gate 7160Sstevel@tonic-gate while ((e = uu_list_teardown(ri->ri_queue, &cookie)) != NULL) 7170Sstevel@tonic-gate startd_free(e, sizeof (*e)); 7180Sstevel@tonic-gate uu_list_destroy(ri->ri_queue); 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate startd_free((void *)ri->ri_i.i_fmri, strlen(ri->ri_i.i_fmri) + 1); 721345Slianep startd_free(ri->ri_logstem, PATH_MAX); 7220Sstevel@tonic-gate startd_free(ri->ri_utmpx_prefix, max_scf_value_size); 7230Sstevel@tonic-gate (void) pthread_mutex_destroy(&ri->ri_lock); 7240Sstevel@tonic-gate (void) pthread_mutex_destroy(&ri->ri_queue_lock); 7250Sstevel@tonic-gate startd_free(ri, sizeof (restarter_inst_t)); 7260Sstevel@tonic-gate } 7270Sstevel@tonic-gate 7280Sstevel@tonic-gate /* 7290Sstevel@tonic-gate * instance_is_wait_style() 7300Sstevel@tonic-gate * 7310Sstevel@tonic-gate * Returns 1 if the given instance is a "wait-style" service instance. 7320Sstevel@tonic-gate */ 7330Sstevel@tonic-gate int 7340Sstevel@tonic-gate instance_is_wait_style(restarter_inst_t *inst) 7350Sstevel@tonic-gate { 7360Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&inst->ri_lock)); 7370Sstevel@tonic-gate return ((inst->ri_flags & RINST_STYLE_MASK) == RINST_WAIT); 7380Sstevel@tonic-gate } 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate /* 7410Sstevel@tonic-gate * instance_is_transient_style() 7420Sstevel@tonic-gate * 7430Sstevel@tonic-gate * Returns 1 if the given instance is a transient service instance. 7440Sstevel@tonic-gate */ 7450Sstevel@tonic-gate int 7460Sstevel@tonic-gate instance_is_transient_style(restarter_inst_t *inst) 7470Sstevel@tonic-gate { 7480Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&inst->ri_lock)); 7490Sstevel@tonic-gate return ((inst->ri_flags & RINST_STYLE_MASK) == RINST_TRANSIENT); 7500Sstevel@tonic-gate } 7510Sstevel@tonic-gate 7520Sstevel@tonic-gate /* 7530Sstevel@tonic-gate * instance_in_transition() 7540Sstevel@tonic-gate * Returns 1 if instance is in transition, 0 if not 7550Sstevel@tonic-gate */ 7560Sstevel@tonic-gate int 7570Sstevel@tonic-gate instance_in_transition(restarter_inst_t *inst) 7580Sstevel@tonic-gate { 7590Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&inst->ri_lock)); 7600Sstevel@tonic-gate if (inst->ri_i.i_next_state == RESTARTER_STATE_NONE) 7610Sstevel@tonic-gate return (0); 7620Sstevel@tonic-gate return (1); 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate /* 766*1273Sgm149974 * returns 1 if instance is already started, 0 if not 767*1273Sgm149974 */ 768*1273Sgm149974 static int 769*1273Sgm149974 instance_started(restarter_inst_t *inst) 770*1273Sgm149974 { 771*1273Sgm149974 int ret; 772*1273Sgm149974 773*1273Sgm149974 assert(PTHREAD_MUTEX_HELD(&inst->ri_lock)); 774*1273Sgm149974 775*1273Sgm149974 if (inst->ri_i.i_state == RESTARTER_STATE_ONLINE || 776*1273Sgm149974 inst->ri_i.i_state == RESTARTER_STATE_DEGRADED) 777*1273Sgm149974 ret = 1; 778*1273Sgm149974 else 779*1273Sgm149974 ret = 0; 780*1273Sgm149974 781*1273Sgm149974 return (ret); 782*1273Sgm149974 } 783*1273Sgm149974 784*1273Sgm149974 /* 7850Sstevel@tonic-gate * Returns 7860Sstevel@tonic-gate * 0 - success 7870Sstevel@tonic-gate * ECONNRESET - success, but h was rebound 7880Sstevel@tonic-gate */ 7890Sstevel@tonic-gate int 7900Sstevel@tonic-gate restarter_instance_update_states(scf_handle_t *h, restarter_inst_t *ri, 7910Sstevel@tonic-gate restarter_instance_state_t new_state, 7920Sstevel@tonic-gate restarter_instance_state_t new_state_next, restarter_error_t err, char *aux) 7930Sstevel@tonic-gate { 7940Sstevel@tonic-gate protocol_states_t *states; 7950Sstevel@tonic-gate int e; 7960Sstevel@tonic-gate uint_t retry_count = 0, msecs = ALLOC_DELAY; 7970Sstevel@tonic-gate boolean_t rebound = B_FALSE; 798*1273Sgm149974 int prev_state_online; 799*1273Sgm149974 int state_online; 8000Sstevel@tonic-gate 8010Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&ri->ri_lock)); 8020Sstevel@tonic-gate 803*1273Sgm149974 prev_state_online = instance_started(ri); 804*1273Sgm149974 8050Sstevel@tonic-gate retry: 8060Sstevel@tonic-gate e = _restarter_commit_states(h, &ri->ri_i, new_state, new_state_next, 8070Sstevel@tonic-gate aux); 8080Sstevel@tonic-gate switch (e) { 8090Sstevel@tonic-gate case 0: 8100Sstevel@tonic-gate break; 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate case ENOMEM: 8130Sstevel@tonic-gate ++retry_count; 8140Sstevel@tonic-gate if (retry_count < ALLOC_RETRY) { 8150Sstevel@tonic-gate (void) poll(NULL, 0, msecs); 8160Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT; 8170Sstevel@tonic-gate goto retry; 8180Sstevel@tonic-gate } 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate /* Like startd_alloc(). */ 8210Sstevel@tonic-gate uu_die("Insufficient memory.\n"); 8220Sstevel@tonic-gate /* NOTREACHED */ 8230Sstevel@tonic-gate 8240Sstevel@tonic-gate case ECONNABORTED: 8250Sstevel@tonic-gate libscf_handle_rebind(h); 8260Sstevel@tonic-gate rebound = B_TRUE; 8270Sstevel@tonic-gate goto retry; 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate case EPERM: 8300Sstevel@tonic-gate case EACCES: 8310Sstevel@tonic-gate case EROFS: 8320Sstevel@tonic-gate log_error(LOG_NOTICE, "Could not commit state change for %s " 8330Sstevel@tonic-gate "to repository: %s.\n", ri->ri_i.i_fmri, strerror(e)); 8340Sstevel@tonic-gate /* FALLTHROUGH */ 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate case ENOENT: 8370Sstevel@tonic-gate ri->ri_i.i_state = new_state; 8380Sstevel@tonic-gate ri->ri_i.i_next_state = new_state_next; 8390Sstevel@tonic-gate break; 8400Sstevel@tonic-gate 8410Sstevel@tonic-gate case EINVAL: 8420Sstevel@tonic-gate default: 8430Sstevel@tonic-gate bad_error("_restarter_commit_states", e); 8440Sstevel@tonic-gate } 8450Sstevel@tonic-gate 8460Sstevel@tonic-gate states = startd_alloc(sizeof (protocol_states_t)); 8470Sstevel@tonic-gate states->ps_state = new_state; 8480Sstevel@tonic-gate states->ps_state_next = new_state_next; 8490Sstevel@tonic-gate states->ps_err = err; 8500Sstevel@tonic-gate graph_protocol_send_event(ri->ri_i.i_fmri, GRAPH_UPDATE_STATE_CHANGE, 8510Sstevel@tonic-gate (void *)states); 8520Sstevel@tonic-gate 853*1273Sgm149974 state_online = instance_started(ri); 854*1273Sgm149974 855*1273Sgm149974 if (prev_state_online && !state_online) 856*1273Sgm149974 ri->ri_post_offline_hook(); 857*1273Sgm149974 else if (!prev_state_online && state_online) 8580Sstevel@tonic-gate ri->ri_post_online_hook(); 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate return (rebound ? ECONNRESET : 0); 8610Sstevel@tonic-gate } 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate void 8640Sstevel@tonic-gate restarter_mark_pending_snapshot(const char *fmri, uint_t flag) 8650Sstevel@tonic-gate { 8660Sstevel@tonic-gate restarter_inst_t *inst; 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate assert(flag == RINST_RETAKE_RUNNING || flag == RINST_RETAKE_START); 8690Sstevel@tonic-gate 8700Sstevel@tonic-gate inst = inst_lookup_by_name(fmri); 8710Sstevel@tonic-gate if (inst == NULL) 8720Sstevel@tonic-gate return; 8730Sstevel@tonic-gate 8740Sstevel@tonic-gate inst->ri_flags |= flag; 8750Sstevel@tonic-gate 8760Sstevel@tonic-gate MUTEX_UNLOCK(&inst->ri_lock); 8770Sstevel@tonic-gate } 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate static void 8800Sstevel@tonic-gate restarter_take_pending_snapshots(scf_handle_t *h) 8810Sstevel@tonic-gate { 8820Sstevel@tonic-gate restarter_inst_t *inst; 8830Sstevel@tonic-gate int r; 8840Sstevel@tonic-gate 8850Sstevel@tonic-gate MUTEX_LOCK(&instance_list.ril_lock); 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate for (inst = uu_list_first(instance_list.ril_instance_list); 8880Sstevel@tonic-gate inst != NULL; 8890Sstevel@tonic-gate inst = uu_list_next(instance_list.ril_instance_list, inst)) { 8900Sstevel@tonic-gate const char *fmri; 8910Sstevel@tonic-gate scf_instance_t *sinst = NULL; 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate MUTEX_LOCK(&inst->ri_lock); 8940Sstevel@tonic-gate 8950Sstevel@tonic-gate /* 8960Sstevel@tonic-gate * This is where we'd check inst->ri_method_thread and if it 8970Sstevel@tonic-gate * were nonzero we'd wait in anticipation of another thread 8980Sstevel@tonic-gate * executing a method for inst. Doing so with the instance_list 8990Sstevel@tonic-gate * locked, though, leads to deadlock. Since taking a snapshot 9000Sstevel@tonic-gate * during that window won't hurt anything, we'll just continue. 9010Sstevel@tonic-gate */ 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate fmri = inst->ri_i.i_fmri; 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate if (inst->ri_flags & RINST_RETAKE_RUNNING) { 9060Sstevel@tonic-gate scf_snapshot_t *rsnap; 9070Sstevel@tonic-gate 9080Sstevel@tonic-gate (void) libscf_fmri_get_instance(h, fmri, &sinst); 9090Sstevel@tonic-gate 9100Sstevel@tonic-gate rsnap = libscf_get_or_make_running_snapshot(sinst, 9110Sstevel@tonic-gate fmri, B_FALSE); 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate scf_instance_destroy(sinst); 9140Sstevel@tonic-gate 9150Sstevel@tonic-gate if (rsnap != NULL) 9160Sstevel@tonic-gate inst->ri_flags &= ~RINST_RETAKE_RUNNING; 9170Sstevel@tonic-gate 9180Sstevel@tonic-gate scf_snapshot_destroy(rsnap); 9190Sstevel@tonic-gate } 9200Sstevel@tonic-gate 9210Sstevel@tonic-gate if (inst->ri_flags & RINST_RETAKE_START) { 9220Sstevel@tonic-gate switch (r = libscf_snapshots_poststart(h, fmri, 9230Sstevel@tonic-gate B_FALSE)) { 9240Sstevel@tonic-gate case 0: 9250Sstevel@tonic-gate case ENOENT: 9260Sstevel@tonic-gate inst->ri_flags &= ~RINST_RETAKE_START; 9270Sstevel@tonic-gate break; 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate case ECONNABORTED: 9300Sstevel@tonic-gate break; 9310Sstevel@tonic-gate 9320Sstevel@tonic-gate case EACCES: 9330Sstevel@tonic-gate default: 9340Sstevel@tonic-gate bad_error("libscf_snapshots_poststart", r); 9350Sstevel@tonic-gate } 9360Sstevel@tonic-gate } 9370Sstevel@tonic-gate 9380Sstevel@tonic-gate MUTEX_UNLOCK(&inst->ri_lock); 9390Sstevel@tonic-gate } 9400Sstevel@tonic-gate 9410Sstevel@tonic-gate MUTEX_UNLOCK(&instance_list.ril_lock); 9420Sstevel@tonic-gate } 9430Sstevel@tonic-gate 9440Sstevel@tonic-gate /* ARGSUSED */ 9450Sstevel@tonic-gate void * 9460Sstevel@tonic-gate restarter_post_fsminimal_thread(void *unused) 9470Sstevel@tonic-gate { 9480Sstevel@tonic-gate scf_handle_t *h; 9490Sstevel@tonic-gate int r; 9500Sstevel@tonic-gate 9510Sstevel@tonic-gate h = libscf_handle_create_bound_loop(); 9520Sstevel@tonic-gate 9530Sstevel@tonic-gate for (;;) { 9540Sstevel@tonic-gate r = libscf_create_self(h); 9550Sstevel@tonic-gate if (r == 0) 9560Sstevel@tonic-gate break; 9570Sstevel@tonic-gate 9580Sstevel@tonic-gate assert(r == ECONNABORTED); 9590Sstevel@tonic-gate libscf_handle_rebind(h); 9600Sstevel@tonic-gate } 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate restarter_take_pending_snapshots(h); 9630Sstevel@tonic-gate 9640Sstevel@tonic-gate (void) scf_handle_unbind(h); 9650Sstevel@tonic-gate scf_handle_destroy(h); 9660Sstevel@tonic-gate 9670Sstevel@tonic-gate return (NULL); 9680Sstevel@tonic-gate } 9690Sstevel@tonic-gate 9700Sstevel@tonic-gate /* 9710Sstevel@tonic-gate * int stop_instance() 9720Sstevel@tonic-gate * 9730Sstevel@tonic-gate * Stop the instance identified by the instance given as the second argument, 9740Sstevel@tonic-gate * for the cause stated. 9750Sstevel@tonic-gate * 9760Sstevel@tonic-gate * Returns 9770Sstevel@tonic-gate * 0 - success 9780Sstevel@tonic-gate * -1 - inst is in transition 9790Sstevel@tonic-gate */ 9800Sstevel@tonic-gate static int 9810Sstevel@tonic-gate stop_instance(scf_handle_t *local_handle, restarter_inst_t *inst, 9820Sstevel@tonic-gate stop_cause_t cause) 9830Sstevel@tonic-gate { 9840Sstevel@tonic-gate fork_info_t *info; 9850Sstevel@tonic-gate const char *cp; 9860Sstevel@tonic-gate int err; 9870Sstevel@tonic-gate restarter_error_t re; 9880Sstevel@tonic-gate 9890Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&inst->ri_lock)); 9900Sstevel@tonic-gate assert(inst->ri_method_thread == 0); 9910Sstevel@tonic-gate 9920Sstevel@tonic-gate switch (cause) { 9930Sstevel@tonic-gate case RSTOP_EXIT: 9940Sstevel@tonic-gate re = RERR_RESTART; 9950Sstevel@tonic-gate cp = "all processes in service exited"; 9960Sstevel@tonic-gate break; 9970Sstevel@tonic-gate case RSTOP_CORE: 9980Sstevel@tonic-gate re = RERR_FAULT; 9990Sstevel@tonic-gate cp = "process dumped core"; 10000Sstevel@tonic-gate break; 10010Sstevel@tonic-gate case RSTOP_SIGNAL: 10020Sstevel@tonic-gate re = RERR_FAULT; 10030Sstevel@tonic-gate cp = "process received fatal signal from outside the service"; 10040Sstevel@tonic-gate break; 10050Sstevel@tonic-gate case RSTOP_HWERR: 10060Sstevel@tonic-gate re = RERR_FAULT; 10070Sstevel@tonic-gate cp = "process killed due to uncorrectable hardware error"; 10080Sstevel@tonic-gate break; 10090Sstevel@tonic-gate case RSTOP_DEPENDENCY: 10100Sstevel@tonic-gate re = RERR_RESTART; 10110Sstevel@tonic-gate cp = "dependency activity requires stop"; 10120Sstevel@tonic-gate break; 10130Sstevel@tonic-gate case RSTOP_DISABLE: 10140Sstevel@tonic-gate re = RERR_RESTART; 10150Sstevel@tonic-gate cp = "service disabled"; 10160Sstevel@tonic-gate break; 10170Sstevel@tonic-gate case RSTOP_RESTART: 10180Sstevel@tonic-gate re = RERR_RESTART; 10190Sstevel@tonic-gate cp = "service restarting"; 10200Sstevel@tonic-gate break; 10210Sstevel@tonic-gate default: 10220Sstevel@tonic-gate #ifndef NDEBUG 10230Sstevel@tonic-gate (void) fprintf(stderr, "Unknown cause %d at %s:%d.\n", 10240Sstevel@tonic-gate cause, __FILE__, __LINE__); 10250Sstevel@tonic-gate #endif 10260Sstevel@tonic-gate abort(); 10270Sstevel@tonic-gate } 10280Sstevel@tonic-gate 10290Sstevel@tonic-gate /* Services in the disabled and maintenance state are ignored */ 10300Sstevel@tonic-gate if (inst->ri_i.i_state == RESTARTER_STATE_MAINT || 10310Sstevel@tonic-gate inst->ri_i.i_state == RESTARTER_STATE_DISABLED) { 10320Sstevel@tonic-gate log_framework(LOG_DEBUG, 10330Sstevel@tonic-gate "%s: stop_instance -> is maint/disabled\n", 10340Sstevel@tonic-gate inst->ri_i.i_fmri); 10350Sstevel@tonic-gate return (0); 10360Sstevel@tonic-gate } 10370Sstevel@tonic-gate 10380Sstevel@tonic-gate /* Already stopped instances are left alone */ 10390Sstevel@tonic-gate if (instance_started(inst) == 0) { 10400Sstevel@tonic-gate log_framework(LOG_DEBUG, "Restarter: %s is already stopped.\n", 10410Sstevel@tonic-gate inst->ri_i.i_fmri); 10420Sstevel@tonic-gate return (0); 10430Sstevel@tonic-gate } 10440Sstevel@tonic-gate 10450Sstevel@tonic-gate if (instance_in_transition(inst)) { 10460Sstevel@tonic-gate /* requeue event by returning -1 */ 10470Sstevel@tonic-gate log_framework(LOG_DEBUG, 10480Sstevel@tonic-gate "Restarter: Not stopping %s, in transition.\n", 10490Sstevel@tonic-gate inst->ri_i.i_fmri); 10500Sstevel@tonic-gate return (-1); 10510Sstevel@tonic-gate } 10520Sstevel@tonic-gate 10530Sstevel@tonic-gate log_instance(inst, B_TRUE, "Stopping because %s.", cp); 10540Sstevel@tonic-gate 10550Sstevel@tonic-gate log_framework(re == RERR_FAULT ? LOG_INFO : LOG_DEBUG, 10560Sstevel@tonic-gate "%s: Instance stopping because %s.\n", inst->ri_i.i_fmri, cp); 10570Sstevel@tonic-gate 10580Sstevel@tonic-gate if (instance_is_wait_style(inst) && cause == RSTOP_EXIT) { 10590Sstevel@tonic-gate /* 10600Sstevel@tonic-gate * No need to stop instance, as child has exited; remove 10610Sstevel@tonic-gate * contract and move the instance to the offline state. 10620Sstevel@tonic-gate */ 10630Sstevel@tonic-gate switch (err = restarter_instance_update_states(local_handle, 10640Sstevel@tonic-gate inst, inst->ri_i.i_state, RESTARTER_STATE_OFFLINE, re, 10650Sstevel@tonic-gate NULL)) { 10660Sstevel@tonic-gate case 0: 10670Sstevel@tonic-gate case ECONNRESET: 10680Sstevel@tonic-gate break; 10690Sstevel@tonic-gate 10700Sstevel@tonic-gate default: 10710Sstevel@tonic-gate bad_error("restarter_instance_update_states", err); 10720Sstevel@tonic-gate } 10730Sstevel@tonic-gate 10740Sstevel@tonic-gate (void) update_fault_count(inst, FAULT_COUNT_RESET); 10750Sstevel@tonic-gate 10760Sstevel@tonic-gate if (inst->ri_i.i_primary_ctid != 0) { 10770Sstevel@tonic-gate inst->ri_m_inst = 10780Sstevel@tonic-gate safe_scf_instance_create(local_handle); 10790Sstevel@tonic-gate inst->ri_mi_deleted = B_FALSE; 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate libscf_reget_instance(inst); 10820Sstevel@tonic-gate method_remove_contract(inst, B_TRUE, B_TRUE); 10830Sstevel@tonic-gate 10840Sstevel@tonic-gate scf_instance_destroy(inst->ri_m_inst); 10850Sstevel@tonic-gate inst->ri_m_inst = NULL; 10860Sstevel@tonic-gate } 10870Sstevel@tonic-gate 10880Sstevel@tonic-gate switch (err = restarter_instance_update_states(local_handle, 10890Sstevel@tonic-gate inst, inst->ri_i.i_next_state, RESTARTER_STATE_NONE, re, 10900Sstevel@tonic-gate NULL)) { 10910Sstevel@tonic-gate case 0: 10920Sstevel@tonic-gate case ECONNRESET: 10930Sstevel@tonic-gate break; 10940Sstevel@tonic-gate 10950Sstevel@tonic-gate default: 10960Sstevel@tonic-gate bad_error("restarter_instance_update_states", err); 10970Sstevel@tonic-gate } 10980Sstevel@tonic-gate 10990Sstevel@tonic-gate return (0); 11000Sstevel@tonic-gate } 11010Sstevel@tonic-gate 11020Sstevel@tonic-gate switch (err = restarter_instance_update_states(local_handle, inst, 11030Sstevel@tonic-gate inst->ri_i.i_state, inst->ri_i.i_enabled ? RESTARTER_STATE_OFFLINE : 11040Sstevel@tonic-gate RESTARTER_STATE_DISABLED, RERR_NONE, NULL)) { 11050Sstevel@tonic-gate case 0: 11060Sstevel@tonic-gate case ECONNRESET: 11070Sstevel@tonic-gate break; 11080Sstevel@tonic-gate 11090Sstevel@tonic-gate default: 11100Sstevel@tonic-gate bad_error("restarter_instance_update_states", err); 11110Sstevel@tonic-gate } 11120Sstevel@tonic-gate 11130Sstevel@tonic-gate info = startd_zalloc(sizeof (fork_info_t)); 11140Sstevel@tonic-gate 11150Sstevel@tonic-gate info->sf_id = inst->ri_id; 11160Sstevel@tonic-gate info->sf_method_type = METHOD_STOP; 11170Sstevel@tonic-gate info->sf_event_type = re; 11180Sstevel@tonic-gate inst->ri_method_thread = startd_thread_create(method_thread, info); 11190Sstevel@tonic-gate 11200Sstevel@tonic-gate return (0); 11210Sstevel@tonic-gate } 11220Sstevel@tonic-gate 11230Sstevel@tonic-gate /* 11240Sstevel@tonic-gate * Returns 11250Sstevel@tonic-gate * ENOENT - fmri is not in instance_list 11260Sstevel@tonic-gate * 0 - success 11270Sstevel@tonic-gate * ECONNRESET - success, though handle was rebound 11280Sstevel@tonic-gate * -1 - instance is in transition 11290Sstevel@tonic-gate */ 11300Sstevel@tonic-gate int 11310Sstevel@tonic-gate stop_instance_fmri(scf_handle_t *h, const char *fmri, uint_t flags) 11320Sstevel@tonic-gate { 11330Sstevel@tonic-gate restarter_inst_t *rip; 11340Sstevel@tonic-gate int r; 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate rip = inst_lookup_by_name(fmri); 11370Sstevel@tonic-gate if (rip == NULL) 11380Sstevel@tonic-gate return (ENOENT); 11390Sstevel@tonic-gate 11400Sstevel@tonic-gate r = stop_instance(h, rip, flags); 11410Sstevel@tonic-gate 11420Sstevel@tonic-gate MUTEX_UNLOCK(&rip->ri_lock); 11430Sstevel@tonic-gate 11440Sstevel@tonic-gate return (r); 11450Sstevel@tonic-gate } 11460Sstevel@tonic-gate 11470Sstevel@tonic-gate static void 11480Sstevel@tonic-gate unmaintain_instance(scf_handle_t *h, restarter_inst_t *rip, 11490Sstevel@tonic-gate unmaint_cause_t cause) 11500Sstevel@tonic-gate { 11510Sstevel@tonic-gate ctid_t ctid; 11520Sstevel@tonic-gate scf_instance_t *inst; 11530Sstevel@tonic-gate int r; 11540Sstevel@tonic-gate uint_t tries = 0, msecs = ALLOC_DELAY; 11550Sstevel@tonic-gate const char *cp; 11560Sstevel@tonic-gate 11570Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&rip->ri_lock)); 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate if (rip->ri_i.i_state != RESTARTER_STATE_MAINT) { 11600Sstevel@tonic-gate log_error(LOG_DEBUG, "Restarter: " 11610Sstevel@tonic-gate "Ignoring maintenance off command because %s is not in the " 11620Sstevel@tonic-gate "maintenance state.\n", rip->ri_i.i_fmri); 11630Sstevel@tonic-gate return; 11640Sstevel@tonic-gate } 11650Sstevel@tonic-gate 11660Sstevel@tonic-gate switch (cause) { 11670Sstevel@tonic-gate case RUNMAINT_CLEAR: 11680Sstevel@tonic-gate cp = "clear requested"; 11690Sstevel@tonic-gate break; 11700Sstevel@tonic-gate case RUNMAINT_DISABLE: 11710Sstevel@tonic-gate cp = "disable requested"; 11720Sstevel@tonic-gate break; 11730Sstevel@tonic-gate default: 11740Sstevel@tonic-gate #ifndef NDEBUG 11750Sstevel@tonic-gate (void) fprintf(stderr, "Uncaught case for %d at %s:%d.\n", 11760Sstevel@tonic-gate cause, __FILE__, __LINE__); 11770Sstevel@tonic-gate #endif 11780Sstevel@tonic-gate abort(); 11790Sstevel@tonic-gate } 11800Sstevel@tonic-gate 11810Sstevel@tonic-gate log_instance(rip, B_TRUE, "Leaving maintenance because %s.", 11820Sstevel@tonic-gate cp); 11830Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: Instance leaving maintenance because " 11840Sstevel@tonic-gate "%s.\n", rip->ri_i.i_fmri, cp); 11850Sstevel@tonic-gate 11860Sstevel@tonic-gate (void) restarter_instance_update_states(h, rip, RESTARTER_STATE_UNINIT, 11870Sstevel@tonic-gate RESTARTER_STATE_NONE, RERR_RESTART, NULL); 11880Sstevel@tonic-gate 11890Sstevel@tonic-gate /* 11900Sstevel@tonic-gate * If we did ADMIN_MAINT_ON_IMMEDIATE, then there might still be 11910Sstevel@tonic-gate * a primary contract. 11920Sstevel@tonic-gate */ 11930Sstevel@tonic-gate if (rip->ri_i.i_primary_ctid == 0) 11940Sstevel@tonic-gate return; 11950Sstevel@tonic-gate 11960Sstevel@tonic-gate ctid = rip->ri_i.i_primary_ctid; 11970Sstevel@tonic-gate contract_abandon(ctid); 11980Sstevel@tonic-gate rip->ri_i.i_primary_ctid = 0; 11990Sstevel@tonic-gate 12000Sstevel@tonic-gate rep_retry: 12010Sstevel@tonic-gate switch (r = libscf_fmri_get_instance(h, rip->ri_i.i_fmri, &inst)) { 12020Sstevel@tonic-gate case 0: 12030Sstevel@tonic-gate break; 12040Sstevel@tonic-gate 12050Sstevel@tonic-gate case ECONNABORTED: 12060Sstevel@tonic-gate libscf_handle_rebind(h); 12070Sstevel@tonic-gate goto rep_retry; 12080Sstevel@tonic-gate 12090Sstevel@tonic-gate case ENOENT: 12100Sstevel@tonic-gate /* Must have been deleted. */ 12110Sstevel@tonic-gate return; 12120Sstevel@tonic-gate 12130Sstevel@tonic-gate case EINVAL: 12140Sstevel@tonic-gate case ENOTSUP: 12150Sstevel@tonic-gate default: 12160Sstevel@tonic-gate bad_error("libscf_handle_rebind", r); 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate 12190Sstevel@tonic-gate again: 12200Sstevel@tonic-gate r = restarter_remove_contract(inst, ctid, RESTARTER_CONTRACT_PRIMARY); 12210Sstevel@tonic-gate switch (r) { 12220Sstevel@tonic-gate case 0: 12230Sstevel@tonic-gate break; 12240Sstevel@tonic-gate 12250Sstevel@tonic-gate case ENOMEM: 12260Sstevel@tonic-gate ++tries; 12270Sstevel@tonic-gate if (tries < ALLOC_RETRY) { 12280Sstevel@tonic-gate (void) poll(NULL, 0, msecs); 12290Sstevel@tonic-gate msecs *= ALLOC_DELAY_MULT; 12300Sstevel@tonic-gate goto again; 12310Sstevel@tonic-gate } 12320Sstevel@tonic-gate 12330Sstevel@tonic-gate uu_die("Insufficient memory.\n"); 12340Sstevel@tonic-gate /* NOTREACHED */ 12350Sstevel@tonic-gate 12360Sstevel@tonic-gate case ECONNABORTED: 12370Sstevel@tonic-gate scf_instance_destroy(inst); 12380Sstevel@tonic-gate libscf_handle_rebind(h); 12390Sstevel@tonic-gate goto rep_retry; 12400Sstevel@tonic-gate 12410Sstevel@tonic-gate case ECANCELED: 12420Sstevel@tonic-gate break; 12430Sstevel@tonic-gate 12440Sstevel@tonic-gate case EPERM: 12450Sstevel@tonic-gate case EACCES: 12460Sstevel@tonic-gate case EROFS: 12470Sstevel@tonic-gate log_error(LOG_INFO, 12480Sstevel@tonic-gate "Could not remove contract id %lu for %s (%s).\n", ctid, 12490Sstevel@tonic-gate rip->ri_i.i_fmri, strerror(r)); 12500Sstevel@tonic-gate break; 12510Sstevel@tonic-gate 12520Sstevel@tonic-gate case EINVAL: 12530Sstevel@tonic-gate case EBADF: 12540Sstevel@tonic-gate default: 12550Sstevel@tonic-gate bad_error("restarter_remove_contract", r); 12560Sstevel@tonic-gate } 12570Sstevel@tonic-gate 12580Sstevel@tonic-gate scf_instance_destroy(inst); 12590Sstevel@tonic-gate } 12600Sstevel@tonic-gate 12610Sstevel@tonic-gate /* 12620Sstevel@tonic-gate * enable_inst() 12630Sstevel@tonic-gate * Set inst->ri_i.i_enabled. Expects 'e' to be _ENABLE, _DISABLE, or 12640Sstevel@tonic-gate * _ADMIN_DISABLE. If the event is _ENABLE and inst is uninitialized or 12650Sstevel@tonic-gate * disabled, move it to offline. If the event is _DISABLE or 12660Sstevel@tonic-gate * _ADMIN_DISABLE, make sure inst will move to disabled. 12670Sstevel@tonic-gate * 12680Sstevel@tonic-gate * Returns 12690Sstevel@tonic-gate * 0 - success 12700Sstevel@tonic-gate * ECONNRESET - h was rebound 12710Sstevel@tonic-gate */ 12720Sstevel@tonic-gate static int 12730Sstevel@tonic-gate enable_inst(scf_handle_t *h, restarter_inst_t *inst, restarter_event_type_t e) 12740Sstevel@tonic-gate { 12750Sstevel@tonic-gate restarter_instance_state_t state; 12760Sstevel@tonic-gate int r; 12770Sstevel@tonic-gate 12780Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&inst->ri_lock)); 12790Sstevel@tonic-gate assert(e == RESTARTER_EVENT_TYPE_ADMIN_DISABLE || 12800Sstevel@tonic-gate e == RESTARTER_EVENT_TYPE_DISABLE || 12810Sstevel@tonic-gate e == RESTARTER_EVENT_TYPE_ENABLE); 12820Sstevel@tonic-gate assert(instance_in_transition(inst) == 0); 12830Sstevel@tonic-gate 12840Sstevel@tonic-gate state = inst->ri_i.i_state; 12850Sstevel@tonic-gate 12860Sstevel@tonic-gate if (e == RESTARTER_EVENT_TYPE_ENABLE) { 12870Sstevel@tonic-gate inst->ri_i.i_enabled = 1; 12880Sstevel@tonic-gate 12890Sstevel@tonic-gate if (state == RESTARTER_STATE_UNINIT || 12900Sstevel@tonic-gate state == RESTARTER_STATE_DISABLED) { 12910Sstevel@tonic-gate /* 12920Sstevel@tonic-gate * B_FALSE: Don't log an error if the log_instance() 12930Sstevel@tonic-gate * fails because it will fail on the miniroot before 12940Sstevel@tonic-gate * install-discovery runs. 12950Sstevel@tonic-gate */ 12960Sstevel@tonic-gate log_instance(inst, B_FALSE, "Enabled."); 12970Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: Instance enabled.\n", 12980Sstevel@tonic-gate inst->ri_i.i_fmri); 12990Sstevel@tonic-gate (void) restarter_instance_update_states(h, inst, 13000Sstevel@tonic-gate RESTARTER_STATE_OFFLINE, RESTARTER_STATE_NONE, 13010Sstevel@tonic-gate RERR_NONE, NULL); 13020Sstevel@tonic-gate } else { 13030Sstevel@tonic-gate log_framework(LOG_DEBUG, "Restarter: " 13040Sstevel@tonic-gate "Not changing state of %s for enable command.\n", 13050Sstevel@tonic-gate inst->ri_i.i_fmri); 13060Sstevel@tonic-gate } 13070Sstevel@tonic-gate } else { 13080Sstevel@tonic-gate inst->ri_i.i_enabled = 0; 13090Sstevel@tonic-gate 13100Sstevel@tonic-gate switch (state) { 13110Sstevel@tonic-gate case RESTARTER_STATE_ONLINE: 13120Sstevel@tonic-gate case RESTARTER_STATE_DEGRADED: 13130Sstevel@tonic-gate r = stop_instance(h, inst, RSTOP_DISABLE); 13140Sstevel@tonic-gate return (r == ECONNRESET ? 0 : r); 13150Sstevel@tonic-gate 13160Sstevel@tonic-gate case RESTARTER_STATE_OFFLINE: 13170Sstevel@tonic-gate case RESTARTER_STATE_UNINIT: 13180Sstevel@tonic-gate if (inst->ri_i.i_primary_ctid != 0) { 13190Sstevel@tonic-gate inst->ri_m_inst = safe_scf_instance_create(h); 13200Sstevel@tonic-gate inst->ri_mi_deleted = B_FALSE; 13210Sstevel@tonic-gate 13220Sstevel@tonic-gate libscf_reget_instance(inst); 13230Sstevel@tonic-gate method_remove_contract(inst, B_TRUE, B_TRUE); 13240Sstevel@tonic-gate 13250Sstevel@tonic-gate scf_instance_destroy(inst->ri_m_inst); 13260Sstevel@tonic-gate } 13270Sstevel@tonic-gate /* B_FALSE: See log_instance(..., "Enabled."); above */ 13280Sstevel@tonic-gate log_instance(inst, B_FALSE, "Disabled."); 13290Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: Instance disabled.\n", 13300Sstevel@tonic-gate inst->ri_i.i_fmri); 13310Sstevel@tonic-gate (void) restarter_instance_update_states(h, inst, 13320Sstevel@tonic-gate RESTARTER_STATE_DISABLED, RESTARTER_STATE_NONE, 13330Sstevel@tonic-gate RERR_RESTART, NULL); 13340Sstevel@tonic-gate return (0); 13350Sstevel@tonic-gate 13360Sstevel@tonic-gate case RESTARTER_STATE_DISABLED: 13370Sstevel@tonic-gate break; 13380Sstevel@tonic-gate 13390Sstevel@tonic-gate case RESTARTER_STATE_MAINT: 13400Sstevel@tonic-gate /* 13410Sstevel@tonic-gate * We only want to pull the instance out of maintenance 13420Sstevel@tonic-gate * if the disable is on adminstrative request. The 13430Sstevel@tonic-gate * graph engine sends _DISABLE events whenever a 13440Sstevel@tonic-gate * service isn't in the disabled state, and we don't 13450Sstevel@tonic-gate * want to pull the service out of maintenance if, 13460Sstevel@tonic-gate * for example, it is there due to a dependency cycle. 13470Sstevel@tonic-gate */ 13480Sstevel@tonic-gate if (e == RESTARTER_EVENT_TYPE_ADMIN_DISABLE) 13490Sstevel@tonic-gate unmaintain_instance(h, inst, RUNMAINT_DISABLE); 13500Sstevel@tonic-gate break; 13510Sstevel@tonic-gate 13520Sstevel@tonic-gate default: 13530Sstevel@tonic-gate #ifndef NDEBUG 13540Sstevel@tonic-gate (void) fprintf(stderr, "Restarter instance %s has " 13550Sstevel@tonic-gate "unknown state %d.\n", inst->ri_i.i_fmri, state); 13560Sstevel@tonic-gate #endif 13570Sstevel@tonic-gate abort(); 13580Sstevel@tonic-gate } 13590Sstevel@tonic-gate } 13600Sstevel@tonic-gate 13610Sstevel@tonic-gate return (0); 13620Sstevel@tonic-gate } 13630Sstevel@tonic-gate 13640Sstevel@tonic-gate static void 13650Sstevel@tonic-gate start_instance(scf_handle_t *local_handle, restarter_inst_t *inst) 13660Sstevel@tonic-gate { 13670Sstevel@tonic-gate fork_info_t *info; 13680Sstevel@tonic-gate 13690Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&inst->ri_lock)); 13700Sstevel@tonic-gate assert(instance_in_transition(inst) == 0); 13710Sstevel@tonic-gate assert(inst->ri_method_thread == 0); 13720Sstevel@tonic-gate 13730Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: trying to start instance\n", 13740Sstevel@tonic-gate inst->ri_i.i_fmri); 13750Sstevel@tonic-gate 13760Sstevel@tonic-gate /* Services in the disabled and maintenance state are ignored */ 13770Sstevel@tonic-gate if (inst->ri_i.i_state == RESTARTER_STATE_MAINT || 13780Sstevel@tonic-gate inst->ri_i.i_state == RESTARTER_STATE_DISABLED || 13790Sstevel@tonic-gate inst->ri_i.i_enabled == 0) { 13800Sstevel@tonic-gate log_framework(LOG_DEBUG, 13810Sstevel@tonic-gate "%s: start_instance -> is maint/disabled\n", 13820Sstevel@tonic-gate inst->ri_i.i_fmri); 13830Sstevel@tonic-gate return; 13840Sstevel@tonic-gate } 13850Sstevel@tonic-gate 13860Sstevel@tonic-gate /* Already started instances are left alone */ 13870Sstevel@tonic-gate if (instance_started(inst) == 1) { 13880Sstevel@tonic-gate log_framework(LOG_DEBUG, 13890Sstevel@tonic-gate "%s: start_instance -> is already started\n", 13900Sstevel@tonic-gate inst->ri_i.i_fmri); 13910Sstevel@tonic-gate return; 13920Sstevel@tonic-gate } 13930Sstevel@tonic-gate 13940Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: starting instance.\n", inst->ri_i.i_fmri); 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate (void) restarter_instance_update_states(local_handle, inst, 13970Sstevel@tonic-gate inst->ri_i.i_state, RESTARTER_STATE_ONLINE, RERR_NONE, NULL); 13980Sstevel@tonic-gate 13990Sstevel@tonic-gate info = startd_zalloc(sizeof (fork_info_t)); 14000Sstevel@tonic-gate 14010Sstevel@tonic-gate info->sf_id = inst->ri_id; 14020Sstevel@tonic-gate info->sf_method_type = METHOD_START; 14030Sstevel@tonic-gate info->sf_event_type = RERR_NONE; 14040Sstevel@tonic-gate inst->ri_method_thread = startd_thread_create(method_thread, info); 14050Sstevel@tonic-gate } 14060Sstevel@tonic-gate 14070Sstevel@tonic-gate static void 14080Sstevel@tonic-gate maintain_instance(scf_handle_t *h, restarter_inst_t *rip, int immediate, 14090Sstevel@tonic-gate const char *aux) 14100Sstevel@tonic-gate { 14110Sstevel@tonic-gate fork_info_t *info; 14120Sstevel@tonic-gate 14130Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&rip->ri_lock)); 14140Sstevel@tonic-gate assert(aux != NULL); 14150Sstevel@tonic-gate assert(rip->ri_method_thread == 0); 14160Sstevel@tonic-gate 14170Sstevel@tonic-gate log_instance(rip, B_TRUE, "Stopping for maintenance due to %s.", aux); 14180Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: stopping for maintenance due to %s.\n", 14190Sstevel@tonic-gate rip->ri_i.i_fmri, aux); 14200Sstevel@tonic-gate 14210Sstevel@tonic-gate /* Services in the maintenance state are ignored */ 14220Sstevel@tonic-gate if (rip->ri_i.i_state == RESTARTER_STATE_MAINT) { 14230Sstevel@tonic-gate log_framework(LOG_DEBUG, 14240Sstevel@tonic-gate "%s: maintain_instance -> is already in maintenance\n", 14250Sstevel@tonic-gate rip->ri_i.i_fmri); 14260Sstevel@tonic-gate return; 14270Sstevel@tonic-gate } 14280Sstevel@tonic-gate 14290Sstevel@tonic-gate if (immediate || !instance_started(rip)) { 14300Sstevel@tonic-gate if (rip->ri_i.i_primary_ctid != 0) { 14310Sstevel@tonic-gate rip->ri_m_inst = safe_scf_instance_create(h); 14320Sstevel@tonic-gate rip->ri_mi_deleted = B_FALSE; 14330Sstevel@tonic-gate 14340Sstevel@tonic-gate libscf_reget_instance(rip); 14350Sstevel@tonic-gate method_remove_contract(rip, B_TRUE, B_TRUE); 14360Sstevel@tonic-gate 14370Sstevel@tonic-gate scf_instance_destroy(rip->ri_m_inst); 14380Sstevel@tonic-gate } 14390Sstevel@tonic-gate 14400Sstevel@tonic-gate (void) restarter_instance_update_states(h, rip, 14410Sstevel@tonic-gate RESTARTER_STATE_MAINT, RESTARTER_STATE_NONE, RERR_RESTART, 14420Sstevel@tonic-gate (char *)aux); 14430Sstevel@tonic-gate return; 14440Sstevel@tonic-gate } 14450Sstevel@tonic-gate 14460Sstevel@tonic-gate (void) restarter_instance_update_states(h, rip, rip->ri_i.i_state, 14470Sstevel@tonic-gate RESTARTER_STATE_MAINT, RERR_NONE, (char *)aux); 14480Sstevel@tonic-gate 14490Sstevel@tonic-gate info = startd_zalloc(sizeof (*info)); 14500Sstevel@tonic-gate info->sf_id = rip->ri_id; 14510Sstevel@tonic-gate info->sf_method_type = METHOD_STOP; 14520Sstevel@tonic-gate info->sf_event_type = RERR_RESTART; 14530Sstevel@tonic-gate rip->ri_method_thread = startd_thread_create(method_thread, info); 14540Sstevel@tonic-gate } 14550Sstevel@tonic-gate 14560Sstevel@tonic-gate static void 14570Sstevel@tonic-gate refresh_instance(scf_handle_t *h, restarter_inst_t *rip) 14580Sstevel@tonic-gate { 14590Sstevel@tonic-gate scf_instance_t *inst; 14600Sstevel@tonic-gate scf_snapshot_t *snap; 14610Sstevel@tonic-gate fork_info_t *info; 14620Sstevel@tonic-gate int r; 14630Sstevel@tonic-gate 14640Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&rip->ri_lock)); 14650Sstevel@tonic-gate 14660Sstevel@tonic-gate log_instance(rip, B_TRUE, "Rereading configuration."); 14670Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: rereading configuration.\n", 14680Sstevel@tonic-gate rip->ri_i.i_fmri); 14690Sstevel@tonic-gate 14700Sstevel@tonic-gate rep_retry: 14710Sstevel@tonic-gate r = libscf_fmri_get_instance(h, rip->ri_i.i_fmri, &inst); 14720Sstevel@tonic-gate switch (r) { 14730Sstevel@tonic-gate case 0: 14740Sstevel@tonic-gate break; 14750Sstevel@tonic-gate 14760Sstevel@tonic-gate case ECONNABORTED: 14770Sstevel@tonic-gate libscf_handle_rebind(h); 14780Sstevel@tonic-gate goto rep_retry; 14790Sstevel@tonic-gate 14800Sstevel@tonic-gate case ENOENT: 14810Sstevel@tonic-gate /* Must have been deleted. */ 14820Sstevel@tonic-gate return; 14830Sstevel@tonic-gate 14840Sstevel@tonic-gate case EINVAL: 14850Sstevel@tonic-gate case ENOTSUP: 14860Sstevel@tonic-gate default: 14870Sstevel@tonic-gate bad_error("libscf_fmri_get_instance", r); 14880Sstevel@tonic-gate } 14890Sstevel@tonic-gate 14900Sstevel@tonic-gate snap = libscf_get_running_snapshot(inst); 14910Sstevel@tonic-gate 14920Sstevel@tonic-gate r = libscf_get_startd_properties(inst, snap, &rip->ri_flags, 14930Sstevel@tonic-gate &rip->ri_utmpx_prefix); 14940Sstevel@tonic-gate switch (r) { 14950Sstevel@tonic-gate case 0: 14960Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s is a %s-style service\n", 14970Sstevel@tonic-gate rip->ri_i.i_fmri, service_style(rip->ri_flags)); 14980Sstevel@tonic-gate break; 14990Sstevel@tonic-gate 15000Sstevel@tonic-gate case ECONNABORTED: 15010Sstevel@tonic-gate scf_instance_destroy(inst); 15020Sstevel@tonic-gate scf_snapshot_destroy(snap); 15030Sstevel@tonic-gate libscf_handle_rebind(h); 15040Sstevel@tonic-gate goto rep_retry; 15050Sstevel@tonic-gate 15060Sstevel@tonic-gate case ECANCELED: 15070Sstevel@tonic-gate case ENOENT: 15080Sstevel@tonic-gate /* Succeed in anticipation of REMOVE_INSTANCE. */ 15090Sstevel@tonic-gate break; 15100Sstevel@tonic-gate 15110Sstevel@tonic-gate default: 15120Sstevel@tonic-gate bad_error("libscf_get_startd_properties", r); 15130Sstevel@tonic-gate } 15140Sstevel@tonic-gate 15150Sstevel@tonic-gate if (instance_started(rip)) { 15160Sstevel@tonic-gate /* Refresh does not change the state. */ 15170Sstevel@tonic-gate (void) restarter_instance_update_states(h, rip, 15180Sstevel@tonic-gate rip->ri_i.i_state, rip->ri_i.i_state, RERR_NONE, NULL); 15190Sstevel@tonic-gate 15200Sstevel@tonic-gate info = startd_zalloc(sizeof (*info)); 15210Sstevel@tonic-gate info->sf_id = rip->ri_id; 15220Sstevel@tonic-gate info->sf_method_type = METHOD_REFRESH; 15230Sstevel@tonic-gate info->sf_event_type = RERR_REFRESH; 15240Sstevel@tonic-gate 15250Sstevel@tonic-gate assert(rip->ri_method_thread == 0); 15260Sstevel@tonic-gate rip->ri_method_thread = 15270Sstevel@tonic-gate startd_thread_create(method_thread, info); 15280Sstevel@tonic-gate } 15290Sstevel@tonic-gate 15300Sstevel@tonic-gate scf_snapshot_destroy(snap); 15310Sstevel@tonic-gate scf_instance_destroy(inst); 15320Sstevel@tonic-gate } 15330Sstevel@tonic-gate 15340Sstevel@tonic-gate const char *event_names[] = { "INVALID", "ADD_INSTANCE", "REMOVE_INSTANCE", 15350Sstevel@tonic-gate "ENABLE", "DISABLE", "ADMIN_DEGRADED", "ADMIN_REFRESH", 15360Sstevel@tonic-gate "ADMIN_RESTART", "ADMIN_MAINT_OFF", "ADMIN_MAINT_ON", 15370Sstevel@tonic-gate "ADMIN_MAINT_ON_IMMEDIATE", "STOP", "START", "DEPENDENCY_CYCLE", 15380Sstevel@tonic-gate "INVALID_DEPENDENCY", "ADMIN_DISABLE" 15390Sstevel@tonic-gate }; 15400Sstevel@tonic-gate 15410Sstevel@tonic-gate /* 15420Sstevel@tonic-gate * void *restarter_process_events() 15430Sstevel@tonic-gate * 15440Sstevel@tonic-gate * Called in a separate thread to process the events on an instance's 15450Sstevel@tonic-gate * queue. Empties the queue completely, and tries to keep the thread 15460Sstevel@tonic-gate * around for a little while after the queue is empty to save on 15470Sstevel@tonic-gate * startup costs. 15480Sstevel@tonic-gate */ 15490Sstevel@tonic-gate static void * 15500Sstevel@tonic-gate restarter_process_events(void *arg) 15510Sstevel@tonic-gate { 15520Sstevel@tonic-gate scf_handle_t *h; 15530Sstevel@tonic-gate restarter_instance_qentry_t *event; 15540Sstevel@tonic-gate restarter_inst_t *rip; 15550Sstevel@tonic-gate char *fmri = (char *)arg; 15560Sstevel@tonic-gate struct timespec to; 15570Sstevel@tonic-gate 15580Sstevel@tonic-gate assert(fmri != NULL); 15590Sstevel@tonic-gate 15600Sstevel@tonic-gate h = libscf_handle_create_bound_loop(); 15610Sstevel@tonic-gate 15620Sstevel@tonic-gate /* grab the queue lock */ 15630Sstevel@tonic-gate rip = inst_lookup_queue(fmri); 15640Sstevel@tonic-gate if (rip == NULL) 15650Sstevel@tonic-gate goto out; 15660Sstevel@tonic-gate 15670Sstevel@tonic-gate again: 15680Sstevel@tonic-gate 15690Sstevel@tonic-gate while ((event = uu_list_first(rip->ri_queue)) != NULL) { 15700Sstevel@tonic-gate restarter_inst_t *inst; 15710Sstevel@tonic-gate 15720Sstevel@tonic-gate /* drop the queue lock */ 15730Sstevel@tonic-gate MUTEX_UNLOCK(&rip->ri_queue_lock); 15740Sstevel@tonic-gate 15750Sstevel@tonic-gate /* 15760Sstevel@tonic-gate * Grab the inst lock -- this waits until any outstanding 15770Sstevel@tonic-gate * method finishes running. 15780Sstevel@tonic-gate */ 15790Sstevel@tonic-gate inst = inst_lookup_by_name(fmri); 15800Sstevel@tonic-gate if (inst == NULL) { 15810Sstevel@tonic-gate /* Getting deleted in the middle isn't an error. */ 15820Sstevel@tonic-gate goto cont; 15830Sstevel@tonic-gate } 15840Sstevel@tonic-gate 15850Sstevel@tonic-gate assert(instance_in_transition(inst) == 0); 15860Sstevel@tonic-gate 15870Sstevel@tonic-gate /* process the event */ 15880Sstevel@tonic-gate switch (event->riq_type) { 15890Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ENABLE: 15900Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DISABLE: 15910Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_DISABLE: 15920Sstevel@tonic-gate (void) enable_inst(h, inst, event->riq_type); 15930Sstevel@tonic-gate break; 15940Sstevel@tonic-gate 15950Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE: 15960Sstevel@tonic-gate restarter_delete_inst(inst); 15970Sstevel@tonic-gate inst = NULL; 15980Sstevel@tonic-gate goto cont; 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_STOP: 16010Sstevel@tonic-gate (void) stop_instance(h, inst, RSTOP_DEPENDENCY); 16020Sstevel@tonic-gate break; 16030Sstevel@tonic-gate 16040Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_START: 16050Sstevel@tonic-gate start_instance(h, inst); 16060Sstevel@tonic-gate break; 16070Sstevel@tonic-gate 16080Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE: 16090Sstevel@tonic-gate maintain_instance(h, inst, 0, "dependency_cycle"); 16100Sstevel@tonic-gate break; 16110Sstevel@tonic-gate 16120Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY: 16130Sstevel@tonic-gate maintain_instance(h, inst, 0, "invalid_dependency"); 16140Sstevel@tonic-gate break; 16150Sstevel@tonic-gate 16160Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 16170Sstevel@tonic-gate maintain_instance(h, inst, 0, "administrative_request"); 16180Sstevel@tonic-gate break; 16190Sstevel@tonic-gate 16200Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE: 16210Sstevel@tonic-gate maintain_instance(h, inst, 1, "administrative_request"); 16220Sstevel@tonic-gate break; 16230Sstevel@tonic-gate 16240Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF: 16250Sstevel@tonic-gate unmaintain_instance(h, inst, RUNMAINT_CLEAR); 16260Sstevel@tonic-gate break; 16270Sstevel@tonic-gate 16280Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_REFRESH: 16290Sstevel@tonic-gate refresh_instance(h, inst); 16300Sstevel@tonic-gate break; 16310Sstevel@tonic-gate 16320Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_DEGRADED: 16330Sstevel@tonic-gate log_framework(LOG_WARNING, "Restarter: " 16340Sstevel@tonic-gate "%s command (for %s) unimplemented.\n", 16350Sstevel@tonic-gate event_names[event->riq_type], inst->ri_i.i_fmri); 16360Sstevel@tonic-gate break; 16370Sstevel@tonic-gate 16380Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_RESTART: 16390Sstevel@tonic-gate if (!instance_started(inst)) { 16400Sstevel@tonic-gate log_framework(LOG_DEBUG, "Restarter: " 16410Sstevel@tonic-gate "Not restarting %s; not running.\n", 16420Sstevel@tonic-gate inst->ri_i.i_fmri); 16430Sstevel@tonic-gate } else { 16440Sstevel@tonic-gate /* 16450Sstevel@tonic-gate * Stop the instance. If it can be restarted, 16460Sstevel@tonic-gate * the graph engine will send a new event. 16470Sstevel@tonic-gate */ 16480Sstevel@tonic-gate (void) stop_instance(h, inst, RSTOP_RESTART); 16490Sstevel@tonic-gate } 16500Sstevel@tonic-gate break; 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADD_INSTANCE: 16530Sstevel@tonic-gate default: 16540Sstevel@tonic-gate #ifndef NDEBUG 16550Sstevel@tonic-gate uu_warn("%s:%d: Bad restarter event %d. " 16560Sstevel@tonic-gate "Aborting.\n", __FILE__, __LINE__, event->riq_type); 16570Sstevel@tonic-gate #endif 16580Sstevel@tonic-gate abort(); 16590Sstevel@tonic-gate } 16600Sstevel@tonic-gate 16610Sstevel@tonic-gate assert(inst != NULL); 16620Sstevel@tonic-gate MUTEX_UNLOCK(&inst->ri_lock); 16630Sstevel@tonic-gate 16640Sstevel@tonic-gate cont: 16650Sstevel@tonic-gate /* grab the queue lock */ 16660Sstevel@tonic-gate rip = inst_lookup_queue(fmri); 16670Sstevel@tonic-gate if (rip == NULL) 16680Sstevel@tonic-gate goto out; 16690Sstevel@tonic-gate 16700Sstevel@tonic-gate /* delete the event */ 16710Sstevel@tonic-gate uu_list_remove(rip->ri_queue, event); 16720Sstevel@tonic-gate startd_free(event, sizeof (restarter_instance_qentry_t)); 16730Sstevel@tonic-gate } 16740Sstevel@tonic-gate 16750Sstevel@tonic-gate assert(rip != NULL); 16760Sstevel@tonic-gate 16770Sstevel@tonic-gate /* 16780Sstevel@tonic-gate * Try to preserve the thread for a little while for future use. 16790Sstevel@tonic-gate */ 16800Sstevel@tonic-gate to.tv_sec = 3; 16810Sstevel@tonic-gate to.tv_nsec = 0; 16820Sstevel@tonic-gate (void) pthread_cond_reltimedwait_np(&rip->ri_queue_cv, 16830Sstevel@tonic-gate &rip->ri_queue_lock, &to); 16840Sstevel@tonic-gate 16850Sstevel@tonic-gate if (uu_list_first(rip->ri_queue) != NULL) 16860Sstevel@tonic-gate goto again; 16870Sstevel@tonic-gate 16880Sstevel@tonic-gate rip->ri_queue_thread = 0; 16890Sstevel@tonic-gate MUTEX_UNLOCK(&rip->ri_queue_lock); 16900Sstevel@tonic-gate out: 16910Sstevel@tonic-gate (void) scf_handle_unbind(h); 16920Sstevel@tonic-gate scf_handle_destroy(h); 16930Sstevel@tonic-gate free(fmri); 16940Sstevel@tonic-gate return (NULL); 16950Sstevel@tonic-gate } 16960Sstevel@tonic-gate 16970Sstevel@tonic-gate static int 16980Sstevel@tonic-gate is_admin_event(restarter_event_type_t t) { 16990Sstevel@tonic-gate 17000Sstevel@tonic-gate switch (t) { 17010Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON: 17020Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE: 17030Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF: 17040Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_REFRESH: 17050Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_DEGRADED: 17060Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADMIN_RESTART: 17070Sstevel@tonic-gate return (1); 17080Sstevel@tonic-gate default: 17090Sstevel@tonic-gate return (0); 17100Sstevel@tonic-gate } 17110Sstevel@tonic-gate } 17120Sstevel@tonic-gate 17130Sstevel@tonic-gate static void 17140Sstevel@tonic-gate restarter_queue_event(restarter_inst_t *ri, restarter_protocol_event_t *e) 17150Sstevel@tonic-gate { 17160Sstevel@tonic-gate restarter_instance_qentry_t *qe; 17170Sstevel@tonic-gate int r; 17180Sstevel@tonic-gate 17190Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&ri->ri_queue_lock)); 17200Sstevel@tonic-gate assert(!PTHREAD_MUTEX_HELD(&ri->ri_lock)); 17210Sstevel@tonic-gate 17220Sstevel@tonic-gate qe = startd_zalloc(sizeof (restarter_instance_qentry_t)); 17230Sstevel@tonic-gate qe->riq_type = e->rpe_type; 17240Sstevel@tonic-gate 17250Sstevel@tonic-gate uu_list_node_init(qe, &qe->riq_link, restarter_queue_pool); 17260Sstevel@tonic-gate r = uu_list_insert_before(ri->ri_queue, NULL, qe); 17270Sstevel@tonic-gate assert(r == 0); 17280Sstevel@tonic-gate } 17290Sstevel@tonic-gate 17300Sstevel@tonic-gate /* 17310Sstevel@tonic-gate * void *restarter_event_thread() 17320Sstevel@tonic-gate * 17330Sstevel@tonic-gate * Handle incoming graph events by placing them on a per-instance 17340Sstevel@tonic-gate * queue. We can't lock the main part of the instance structure, so 17350Sstevel@tonic-gate * just modify the seprarately locked event queue portion. 17360Sstevel@tonic-gate */ 17370Sstevel@tonic-gate /*ARGSUSED*/ 17380Sstevel@tonic-gate static void * 17390Sstevel@tonic-gate restarter_event_thread(void *unused) 17400Sstevel@tonic-gate { 17410Sstevel@tonic-gate scf_handle_t *h; 17420Sstevel@tonic-gate 17430Sstevel@tonic-gate /* 17440Sstevel@tonic-gate * This is a new thread, and thus, gets its own handle 17450Sstevel@tonic-gate * to the repository. 17460Sstevel@tonic-gate */ 17470Sstevel@tonic-gate h = libscf_handle_create_bound_loop(); 17480Sstevel@tonic-gate 17490Sstevel@tonic-gate MUTEX_LOCK(&ru->restarter_update_lock); 17500Sstevel@tonic-gate 17510Sstevel@tonic-gate /*CONSTCOND*/ 17520Sstevel@tonic-gate while (1) { 17530Sstevel@tonic-gate restarter_protocol_event_t *e; 17540Sstevel@tonic-gate 17550Sstevel@tonic-gate while (ru->restarter_update_wakeup == 0) 17560Sstevel@tonic-gate (void) pthread_cond_wait(&ru->restarter_update_cv, 17570Sstevel@tonic-gate &ru->restarter_update_lock); 17580Sstevel@tonic-gate 17590Sstevel@tonic-gate ru->restarter_update_wakeup = 0; 17600Sstevel@tonic-gate 17610Sstevel@tonic-gate while ((e = restarter_event_dequeue()) != NULL) { 17620Sstevel@tonic-gate restarter_inst_t *rip; 17630Sstevel@tonic-gate char *fmri; 17640Sstevel@tonic-gate 17650Sstevel@tonic-gate MUTEX_UNLOCK(&ru->restarter_update_lock); 17660Sstevel@tonic-gate 17670Sstevel@tonic-gate /* 17680Sstevel@tonic-gate * ADD_INSTANCE is special: there's likely no 17690Sstevel@tonic-gate * instance structure yet, so we need to handle the 17700Sstevel@tonic-gate * addition synchronously. 17710Sstevel@tonic-gate */ 17720Sstevel@tonic-gate switch (e->rpe_type) { 17730Sstevel@tonic-gate case RESTARTER_EVENT_TYPE_ADD_INSTANCE: 17740Sstevel@tonic-gate if (restarter_insert_inst(h, e->rpe_inst) != 0) 17750Sstevel@tonic-gate log_error(LOG_INFO, "Restarter: " 17760Sstevel@tonic-gate "Could not add %s.\n", e->rpe_inst); 17770Sstevel@tonic-gate 17780Sstevel@tonic-gate MUTEX_LOCK(&st->st_load_lock); 17790Sstevel@tonic-gate if (--st->st_load_instances == 0) 17800Sstevel@tonic-gate (void) pthread_cond_broadcast( 17810Sstevel@tonic-gate &st->st_load_cv); 17820Sstevel@tonic-gate MUTEX_UNLOCK(&st->st_load_lock); 17830Sstevel@tonic-gate 17840Sstevel@tonic-gate goto nolookup; 17850Sstevel@tonic-gate } 17860Sstevel@tonic-gate 17870Sstevel@tonic-gate /* 17880Sstevel@tonic-gate * Lookup the instance, locking only the event queue. 17890Sstevel@tonic-gate * Can't grab ri_lock here because it might be held 17900Sstevel@tonic-gate * by a long-running method. 17910Sstevel@tonic-gate */ 17920Sstevel@tonic-gate rip = inst_lookup_queue(e->rpe_inst); 17930Sstevel@tonic-gate if (rip == NULL) { 17940Sstevel@tonic-gate log_error(LOG_INFO, "Restarter: " 17950Sstevel@tonic-gate "Ignoring %s command for unknown service " 17960Sstevel@tonic-gate "%s.\n", event_names[e->rpe_type], 17970Sstevel@tonic-gate e->rpe_inst); 17980Sstevel@tonic-gate goto nolookup; 17990Sstevel@tonic-gate } 18000Sstevel@tonic-gate 18010Sstevel@tonic-gate /* Keep ADMIN events from filling up the queue. */ 18020Sstevel@tonic-gate if (is_admin_event(e->rpe_type) && 18030Sstevel@tonic-gate uu_list_numnodes(rip->ri_queue) > 18040Sstevel@tonic-gate RINST_QUEUE_THRESHOLD) { 18050Sstevel@tonic-gate MUTEX_UNLOCK(&rip->ri_queue_lock); 18060Sstevel@tonic-gate log_instance(rip, B_TRUE, "Instance event " 18070Sstevel@tonic-gate "queue overflow. Dropping administrative " 18080Sstevel@tonic-gate "request."); 18090Sstevel@tonic-gate log_framework(LOG_DEBUG, "%s: Instance event " 18100Sstevel@tonic-gate "queue overflow. Dropping administrative " 18110Sstevel@tonic-gate "request.\n", rip->ri_i.i_fmri); 18120Sstevel@tonic-gate goto nolookup; 18130Sstevel@tonic-gate } 18140Sstevel@tonic-gate 18150Sstevel@tonic-gate /* Now add the event to the instance queue. */ 18160Sstevel@tonic-gate restarter_queue_event(rip, e); 18170Sstevel@tonic-gate 18180Sstevel@tonic-gate if (rip->ri_queue_thread == 0) { 18190Sstevel@tonic-gate /* 18200Sstevel@tonic-gate * Start a thread if one isn't already 18210Sstevel@tonic-gate * running. 18220Sstevel@tonic-gate */ 18230Sstevel@tonic-gate fmri = safe_strdup(e->rpe_inst); 18240Sstevel@tonic-gate rip->ri_queue_thread = startd_thread_create( 18250Sstevel@tonic-gate restarter_process_events, (void *)fmri); 18260Sstevel@tonic-gate } else { 18270Sstevel@tonic-gate /* 18280Sstevel@tonic-gate * Signal the existing thread that there's 18290Sstevel@tonic-gate * a new event. 18300Sstevel@tonic-gate */ 18310Sstevel@tonic-gate (void) pthread_cond_broadcast( 18320Sstevel@tonic-gate &rip->ri_queue_cv); 18330Sstevel@tonic-gate } 18340Sstevel@tonic-gate 18350Sstevel@tonic-gate MUTEX_UNLOCK(&rip->ri_queue_lock); 18360Sstevel@tonic-gate nolookup: 18370Sstevel@tonic-gate restarter_event_release(e); 18380Sstevel@tonic-gate 18390Sstevel@tonic-gate MUTEX_LOCK(&ru->restarter_update_lock); 18400Sstevel@tonic-gate } 18410Sstevel@tonic-gate } 18420Sstevel@tonic-gate 18430Sstevel@tonic-gate /* 18440Sstevel@tonic-gate * Unreachable for now -- there's currently no graceful cleanup 18450Sstevel@tonic-gate * called on exit(). 18460Sstevel@tonic-gate */ 18470Sstevel@tonic-gate (void) scf_handle_unbind(h); 18480Sstevel@tonic-gate scf_handle_destroy(h); 18490Sstevel@tonic-gate return (NULL); 18500Sstevel@tonic-gate } 18510Sstevel@tonic-gate 18520Sstevel@tonic-gate static restarter_inst_t * 18530Sstevel@tonic-gate contract_to_inst(ctid_t ctid) 18540Sstevel@tonic-gate { 18550Sstevel@tonic-gate restarter_inst_t *inst; 18560Sstevel@tonic-gate int id; 18570Sstevel@tonic-gate 18580Sstevel@tonic-gate id = lookup_inst_by_contract(ctid); 18590Sstevel@tonic-gate if (id == -1) 18600Sstevel@tonic-gate return (NULL); 18610Sstevel@tonic-gate 18620Sstevel@tonic-gate inst = inst_lookup_by_id(id); 18630Sstevel@tonic-gate if (inst != NULL) { 18640Sstevel@tonic-gate /* 18650Sstevel@tonic-gate * Since ri_lock isn't held by the contract id lookup, this 18660Sstevel@tonic-gate * instance may have been restarted and now be in a new 18670Sstevel@tonic-gate * contract, making the old contract no longer valid for this 18680Sstevel@tonic-gate * instance. 18690Sstevel@tonic-gate */ 18700Sstevel@tonic-gate if (ctid != inst->ri_i.i_primary_ctid) { 18710Sstevel@tonic-gate MUTEX_UNLOCK(&inst->ri_lock); 18720Sstevel@tonic-gate inst = NULL; 18730Sstevel@tonic-gate } 18740Sstevel@tonic-gate } 18750Sstevel@tonic-gate return (inst); 18760Sstevel@tonic-gate } 18770Sstevel@tonic-gate 18780Sstevel@tonic-gate /* 18790Sstevel@tonic-gate * void contract_action() 18800Sstevel@tonic-gate * Take action on contract events. 18810Sstevel@tonic-gate */ 18820Sstevel@tonic-gate static void 18830Sstevel@tonic-gate contract_action(scf_handle_t *h, restarter_inst_t *inst, ctid_t id, 18840Sstevel@tonic-gate uint32_t type) 18850Sstevel@tonic-gate { 18860Sstevel@tonic-gate const char *fmri = inst->ri_i.i_fmri; 18870Sstevel@tonic-gate 18880Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&inst->ri_lock)); 18890Sstevel@tonic-gate 18900Sstevel@tonic-gate /* 18910Sstevel@tonic-gate * If startd has stopped this contract, there is no need to 18920Sstevel@tonic-gate * stop it again. 18930Sstevel@tonic-gate */ 18940Sstevel@tonic-gate if (inst->ri_i.i_primary_ctid > 0 && 18950Sstevel@tonic-gate inst->ri_i.i_primary_ctid_stopped) 18960Sstevel@tonic-gate return; 18970Sstevel@tonic-gate 18980Sstevel@tonic-gate if ((type & (CT_PR_EV_EMPTY | CT_PR_EV_CORE | CT_PR_EV_SIGNAL 18990Sstevel@tonic-gate | CT_PR_EV_HWERR)) == 0) { 19000Sstevel@tonic-gate /* 19010Sstevel@tonic-gate * There shouldn't be other events, since that's not how we set 19020Sstevel@tonic-gate * the terms. Thus, just log an error and drive on. 19030Sstevel@tonic-gate */ 19040Sstevel@tonic-gate log_framework(LOG_NOTICE, 19050Sstevel@tonic-gate "%s: contract %ld received unexpected critical event " 19060Sstevel@tonic-gate "(%d)\n", fmri, id, type); 19070Sstevel@tonic-gate return; 19080Sstevel@tonic-gate } 19090Sstevel@tonic-gate 19100Sstevel@tonic-gate assert(instance_in_transition(inst) == 0); 19110Sstevel@tonic-gate 19120Sstevel@tonic-gate if (instance_is_wait_style(inst)) { 19130Sstevel@tonic-gate /* 19140Sstevel@tonic-gate * We ignore all events; if they impact the 19150Sstevel@tonic-gate * process we're monitoring, then the 19160Sstevel@tonic-gate * wait_thread will stop the instance. 19170Sstevel@tonic-gate */ 19180Sstevel@tonic-gate log_framework(LOG_DEBUG, 19190Sstevel@tonic-gate "%s: ignoring contract event on wait-style service\n", 19200Sstevel@tonic-gate fmri); 19210Sstevel@tonic-gate } else { 19220Sstevel@tonic-gate /* 19230Sstevel@tonic-gate * A CT_PR_EV_EMPTY event is an RSTOP_EXIT request. 19240Sstevel@tonic-gate */ 19250Sstevel@tonic-gate switch (type) { 19260Sstevel@tonic-gate case CT_PR_EV_EMPTY: 19270Sstevel@tonic-gate (void) stop_instance(h, inst, RSTOP_EXIT); 19280Sstevel@tonic-gate break; 19290Sstevel@tonic-gate case CT_PR_EV_CORE: 19300Sstevel@tonic-gate (void) stop_instance(h, inst, RSTOP_CORE); 19310Sstevel@tonic-gate break; 19320Sstevel@tonic-gate case CT_PR_EV_SIGNAL: 19330Sstevel@tonic-gate (void) stop_instance(h, inst, RSTOP_SIGNAL); 19340Sstevel@tonic-gate break; 19350Sstevel@tonic-gate case CT_PR_EV_HWERR: 19360Sstevel@tonic-gate (void) stop_instance(h, inst, RSTOP_HWERR); 19370Sstevel@tonic-gate break; 19380Sstevel@tonic-gate } 19390Sstevel@tonic-gate } 19400Sstevel@tonic-gate } 19410Sstevel@tonic-gate 19420Sstevel@tonic-gate /* 19430Sstevel@tonic-gate * void *restarter_contract_event_thread(void *) 19440Sstevel@tonic-gate * Listens to the process contract bundle for critical events, taking action 19450Sstevel@tonic-gate * on events from contracts we know we are responsible for. 19460Sstevel@tonic-gate */ 19470Sstevel@tonic-gate /*ARGSUSED*/ 19480Sstevel@tonic-gate static void * 19490Sstevel@tonic-gate restarter_contracts_event_thread(void *unused) 19500Sstevel@tonic-gate { 19510Sstevel@tonic-gate int fd, err; 19520Sstevel@tonic-gate scf_handle_t *local_handle; 19530Sstevel@tonic-gate 19540Sstevel@tonic-gate /* 19550Sstevel@tonic-gate * Await graph load completion. That is, stop here, until we've scanned 19560Sstevel@tonic-gate * the repository for contract - instance associations. 19570Sstevel@tonic-gate */ 19580Sstevel@tonic-gate MUTEX_LOCK(&st->st_load_lock); 19590Sstevel@tonic-gate while (!(st->st_load_complete && st->st_load_instances == 0)) 19600Sstevel@tonic-gate (void) pthread_cond_wait(&st->st_load_cv, &st->st_load_lock); 19610Sstevel@tonic-gate MUTEX_UNLOCK(&st->st_load_lock); 19620Sstevel@tonic-gate 19630Sstevel@tonic-gate /* 19640Sstevel@tonic-gate * This is a new thread, and thus, gets its own handle 19650Sstevel@tonic-gate * to the repository. 19660Sstevel@tonic-gate */ 19670Sstevel@tonic-gate if ((local_handle = libscf_handle_create_bound(SCF_VERSION)) == NULL) 19680Sstevel@tonic-gate uu_die("Unable to bind a new repository handle: %s\n", 19690Sstevel@tonic-gate scf_strerror(scf_error())); 19700Sstevel@tonic-gate 19710Sstevel@tonic-gate fd = open64(CTFS_ROOT "/process/pbundle", O_RDONLY); 19720Sstevel@tonic-gate if (fd == -1) 19730Sstevel@tonic-gate uu_die("process bundle open failed"); 19740Sstevel@tonic-gate 19750Sstevel@tonic-gate /* 19760Sstevel@tonic-gate * Make sure we get all events (including those generated by configd 19770Sstevel@tonic-gate * before this thread was started). 19780Sstevel@tonic-gate */ 19790Sstevel@tonic-gate err = ct_event_reset(fd); 19800Sstevel@tonic-gate assert(err == 0); 19810Sstevel@tonic-gate 19820Sstevel@tonic-gate for (;;) { 19830Sstevel@tonic-gate int efd, sfd; 19840Sstevel@tonic-gate ct_evthdl_t ev; 19850Sstevel@tonic-gate uint32_t type; 19860Sstevel@tonic-gate ctevid_t evid; 19870Sstevel@tonic-gate ct_stathdl_t status; 19880Sstevel@tonic-gate ctid_t ctid; 19890Sstevel@tonic-gate restarter_inst_t *inst; 19900Sstevel@tonic-gate uint64_t cookie; 19910Sstevel@tonic-gate 19920Sstevel@tonic-gate if (err = ct_event_read_critical(fd, &ev)) { 19930Sstevel@tonic-gate log_error(LOG_WARNING, 19940Sstevel@tonic-gate "Error reading next contract event: %s", 19950Sstevel@tonic-gate strerror(err)); 19960Sstevel@tonic-gate continue; 19970Sstevel@tonic-gate } 19980Sstevel@tonic-gate 19990Sstevel@tonic-gate evid = ct_event_get_evid(ev); 20000Sstevel@tonic-gate ctid = ct_event_get_ctid(ev); 20010Sstevel@tonic-gate type = ct_event_get_type(ev); 20020Sstevel@tonic-gate 20030Sstevel@tonic-gate /* Fetch cookie. */ 20040Sstevel@tonic-gate if ((sfd = contract_open(ctid, "process", "status", O_RDONLY)) 20050Sstevel@tonic-gate < 0) { 20060Sstevel@tonic-gate ct_event_free(ev); 20070Sstevel@tonic-gate continue; 20080Sstevel@tonic-gate } 20090Sstevel@tonic-gate 20100Sstevel@tonic-gate if (err = ct_status_read(sfd, CTD_COMMON, &status)) { 20110Sstevel@tonic-gate log_framework(LOG_WARNING, "Could not get status for " 20120Sstevel@tonic-gate "contract %ld: %s\n", ctid, strerror(err)); 20130Sstevel@tonic-gate 20140Sstevel@tonic-gate startd_close(sfd); 20150Sstevel@tonic-gate ct_event_free(ev); 20160Sstevel@tonic-gate continue; 20170Sstevel@tonic-gate } 20180Sstevel@tonic-gate 20190Sstevel@tonic-gate cookie = ct_status_get_cookie(status); 20200Sstevel@tonic-gate 20210Sstevel@tonic-gate ct_status_free(status); 20220Sstevel@tonic-gate 20230Sstevel@tonic-gate startd_close(sfd); 20240Sstevel@tonic-gate 20250Sstevel@tonic-gate /* 20260Sstevel@tonic-gate * svc.configd(1M) restart handling performed by the 20270Sstevel@tonic-gate * fork_configd_thread. We don't acknowledge, as that thread 20280Sstevel@tonic-gate * will do so. 20290Sstevel@tonic-gate */ 20300Sstevel@tonic-gate if (cookie == CONFIGD_COOKIE) { 20310Sstevel@tonic-gate ct_event_free(ev); 20320Sstevel@tonic-gate continue; 20330Sstevel@tonic-gate } 20340Sstevel@tonic-gate 20350Sstevel@tonic-gate inst = contract_to_inst(ctid); 20360Sstevel@tonic-gate if (inst == NULL) { 20370Sstevel@tonic-gate /* 20380Sstevel@tonic-gate * This can happen if we receive an EMPTY 20390Sstevel@tonic-gate * event for an abandoned contract. 20400Sstevel@tonic-gate */ 20410Sstevel@tonic-gate log_framework(LOG_DEBUG, 20420Sstevel@tonic-gate "Received event %d for unknown contract id " 20430Sstevel@tonic-gate "%ld\n", type, ctid); 20440Sstevel@tonic-gate } else { 20450Sstevel@tonic-gate log_framework(LOG_DEBUG, 20460Sstevel@tonic-gate "Received event %d for contract id " 20470Sstevel@tonic-gate "%ld (%s)\n", type, ctid, 20480Sstevel@tonic-gate inst->ri_i.i_fmri); 20490Sstevel@tonic-gate 20500Sstevel@tonic-gate contract_action(local_handle, inst, ctid, type); 20510Sstevel@tonic-gate 20520Sstevel@tonic-gate MUTEX_UNLOCK(&inst->ri_lock); 20530Sstevel@tonic-gate } 20540Sstevel@tonic-gate 20550Sstevel@tonic-gate efd = contract_open(ct_event_get_ctid(ev), "process", "ctl", 20560Sstevel@tonic-gate O_WRONLY); 20570Sstevel@tonic-gate if (efd != -1) { 20580Sstevel@tonic-gate (void) ct_ctl_ack(efd, evid); 20590Sstevel@tonic-gate startd_close(efd); 20600Sstevel@tonic-gate } 20610Sstevel@tonic-gate 20620Sstevel@tonic-gate ct_event_free(ev); 20630Sstevel@tonic-gate 20640Sstevel@tonic-gate } 20650Sstevel@tonic-gate 20660Sstevel@tonic-gate /*NOTREACHED*/ 20670Sstevel@tonic-gate return (NULL); 20680Sstevel@tonic-gate } 20690Sstevel@tonic-gate 20700Sstevel@tonic-gate /* 20710Sstevel@tonic-gate * Timeout queue, processed by restarter_timeouts_event_thread(). 20720Sstevel@tonic-gate */ 20730Sstevel@tonic-gate timeout_queue_t *timeouts; 20740Sstevel@tonic-gate static uu_list_pool_t *timeout_pool; 20750Sstevel@tonic-gate 20760Sstevel@tonic-gate typedef struct timeout_update { 20770Sstevel@tonic-gate pthread_mutex_t tu_lock; 20780Sstevel@tonic-gate pthread_cond_t tu_cv; 20790Sstevel@tonic-gate int tu_wakeup; 20800Sstevel@tonic-gate } timeout_update_t; 20810Sstevel@tonic-gate 20820Sstevel@tonic-gate timeout_update_t *tu; 20830Sstevel@tonic-gate 20840Sstevel@tonic-gate static const char *timeout_ovr_svcs[] = { 20850Sstevel@tonic-gate "svc:/system/manifest-import:default", 20860Sstevel@tonic-gate "svc:/network/initial:default", 20870Sstevel@tonic-gate "svc:/network/service:default", 20880Sstevel@tonic-gate "svc:/system/rmtmpfiles:default", 20890Sstevel@tonic-gate "svc:/network/loopback:default", 20900Sstevel@tonic-gate "svc:/network/physical:default", 20910Sstevel@tonic-gate "svc:/system/device/local:default", 20920Sstevel@tonic-gate "svc:/system/metainit:default", 20930Sstevel@tonic-gate "svc:/system/filesystem/usr:default", 20940Sstevel@tonic-gate "svc:/system/filesystem/minimal:default", 20950Sstevel@tonic-gate "svc:/system/filesystem/local:default", 20960Sstevel@tonic-gate NULL 20970Sstevel@tonic-gate }; 20980Sstevel@tonic-gate 20990Sstevel@tonic-gate int 21000Sstevel@tonic-gate is_timeout_ovr(restarter_inst_t *inst) 21010Sstevel@tonic-gate { 21020Sstevel@tonic-gate int i; 21030Sstevel@tonic-gate 21040Sstevel@tonic-gate for (i = 0; timeout_ovr_svcs[i] != NULL; ++i) { 21050Sstevel@tonic-gate if (strcmp(inst->ri_i.i_fmri, timeout_ovr_svcs[i]) == 0) { 21060Sstevel@tonic-gate log_instance(inst, B_TRUE, "Timeout override by " 21070Sstevel@tonic-gate "svc.startd. Using infinite timeout"); 21080Sstevel@tonic-gate return (1); 21090Sstevel@tonic-gate } 21100Sstevel@tonic-gate } 21110Sstevel@tonic-gate 21120Sstevel@tonic-gate return (0); 21130Sstevel@tonic-gate } 21140Sstevel@tonic-gate 21150Sstevel@tonic-gate /*ARGSUSED*/ 21160Sstevel@tonic-gate static int 21170Sstevel@tonic-gate timeout_compare(const void *lc_arg, const void *rc_arg, void *private) 21180Sstevel@tonic-gate { 21190Sstevel@tonic-gate hrtime_t t1 = ((const timeout_entry_t *)lc_arg)->te_timeout; 21200Sstevel@tonic-gate hrtime_t t2 = ((const timeout_entry_t *)rc_arg)->te_timeout; 21210Sstevel@tonic-gate 21220Sstevel@tonic-gate if (t1 > t2) 21230Sstevel@tonic-gate return (1); 21240Sstevel@tonic-gate else if (t1 < t2) 21250Sstevel@tonic-gate return (-1); 21260Sstevel@tonic-gate return (0); 21270Sstevel@tonic-gate } 21280Sstevel@tonic-gate 21290Sstevel@tonic-gate void 21300Sstevel@tonic-gate timeout_init() 21310Sstevel@tonic-gate { 21320Sstevel@tonic-gate timeouts = startd_zalloc(sizeof (timeout_queue_t)); 21330Sstevel@tonic-gate 21340Sstevel@tonic-gate (void) pthread_mutex_init(&timeouts->tq_lock, &mutex_attrs); 21350Sstevel@tonic-gate 21360Sstevel@tonic-gate timeout_pool = startd_list_pool_create("timeouts", 21370Sstevel@tonic-gate sizeof (timeout_entry_t), offsetof(timeout_entry_t, te_link), 21380Sstevel@tonic-gate timeout_compare, UU_LIST_POOL_DEBUG); 21390Sstevel@tonic-gate assert(timeout_pool != NULL); 21400Sstevel@tonic-gate 21410Sstevel@tonic-gate timeouts->tq_list = startd_list_create(timeout_pool, 21420Sstevel@tonic-gate timeouts, UU_LIST_SORTED); 21430Sstevel@tonic-gate assert(timeouts->tq_list != NULL); 21440Sstevel@tonic-gate 21450Sstevel@tonic-gate tu = startd_zalloc(sizeof (timeout_update_t)); 21460Sstevel@tonic-gate (void) pthread_cond_init(&tu->tu_cv, NULL); 21470Sstevel@tonic-gate (void) pthread_mutex_init(&tu->tu_lock, &mutex_attrs); 21480Sstevel@tonic-gate } 21490Sstevel@tonic-gate 21500Sstevel@tonic-gate void 21510Sstevel@tonic-gate timeout_insert(restarter_inst_t *inst, ctid_t cid, uint64_t timeout_sec) 21520Sstevel@tonic-gate { 21530Sstevel@tonic-gate hrtime_t now, timeout; 21540Sstevel@tonic-gate timeout_entry_t *entry; 21550Sstevel@tonic-gate uu_list_index_t idx; 21560Sstevel@tonic-gate 21570Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&inst->ri_lock)); 21580Sstevel@tonic-gate 21590Sstevel@tonic-gate now = gethrtime(); 21600Sstevel@tonic-gate 21610Sstevel@tonic-gate /* 21620Sstevel@tonic-gate * If we overflow LLONG_MAX, we're never timing out anyways, so 21630Sstevel@tonic-gate * just return. 21640Sstevel@tonic-gate */ 21650Sstevel@tonic-gate if (timeout_sec >= (LLONG_MAX - now) / 1000000000LL) { 21660Sstevel@tonic-gate log_instance(inst, B_TRUE, "timeout_seconds too large, " 21670Sstevel@tonic-gate "treating as infinite."); 21680Sstevel@tonic-gate return; 21690Sstevel@tonic-gate } 21700Sstevel@tonic-gate 21710Sstevel@tonic-gate /* hrtime is in nanoseconds. Convert timeout_sec. */ 21720Sstevel@tonic-gate timeout = now + (timeout_sec * 1000000000LL); 21730Sstevel@tonic-gate 21740Sstevel@tonic-gate entry = startd_alloc(sizeof (timeout_entry_t)); 21750Sstevel@tonic-gate entry->te_timeout = timeout; 21760Sstevel@tonic-gate entry->te_ctid = cid; 21770Sstevel@tonic-gate entry->te_fmri = safe_strdup(inst->ri_i.i_fmri); 21780Sstevel@tonic-gate entry->te_logstem = safe_strdup(inst->ri_logstem); 21790Sstevel@tonic-gate entry->te_fired = 0; 21800Sstevel@tonic-gate /* Insert the calculated timeout time onto the queue. */ 21810Sstevel@tonic-gate MUTEX_LOCK(&timeouts->tq_lock); 21820Sstevel@tonic-gate (void) uu_list_find(timeouts->tq_list, entry, NULL, &idx); 21830Sstevel@tonic-gate uu_list_node_init(entry, &entry->te_link, timeout_pool); 21840Sstevel@tonic-gate uu_list_insert(timeouts->tq_list, entry, idx); 21850Sstevel@tonic-gate MUTEX_UNLOCK(&timeouts->tq_lock); 21860Sstevel@tonic-gate 21870Sstevel@tonic-gate assert(inst->ri_timeout == NULL); 21880Sstevel@tonic-gate inst->ri_timeout = entry; 21890Sstevel@tonic-gate 21900Sstevel@tonic-gate MUTEX_LOCK(&tu->tu_lock); 21910Sstevel@tonic-gate tu->tu_wakeup = 1; 21920Sstevel@tonic-gate (void) pthread_cond_broadcast(&tu->tu_cv); 21930Sstevel@tonic-gate MUTEX_UNLOCK(&tu->tu_lock); 21940Sstevel@tonic-gate } 21950Sstevel@tonic-gate 21960Sstevel@tonic-gate 21970Sstevel@tonic-gate void 21980Sstevel@tonic-gate timeout_remove(restarter_inst_t *inst, ctid_t cid) 21990Sstevel@tonic-gate { 22000Sstevel@tonic-gate assert(PTHREAD_MUTEX_HELD(&inst->ri_lock)); 22010Sstevel@tonic-gate 22020Sstevel@tonic-gate if (inst->ri_timeout == NULL) 22030Sstevel@tonic-gate return; 22040Sstevel@tonic-gate 22050Sstevel@tonic-gate assert(inst->ri_timeout->te_ctid == cid); 22060Sstevel@tonic-gate 22070Sstevel@tonic-gate MUTEX_LOCK(&timeouts->tq_lock); 22080Sstevel@tonic-gate uu_list_remove(timeouts->tq_list, inst->ri_timeout); 22090Sstevel@tonic-gate MUTEX_UNLOCK(&timeouts->tq_lock); 22100Sstevel@tonic-gate 22110Sstevel@tonic-gate free(inst->ri_timeout->te_fmri); 22120Sstevel@tonic-gate free(inst->ri_timeout->te_logstem); 22130Sstevel@tonic-gate startd_free(inst->ri_timeout, sizeof (timeout_entry_t)); 22140Sstevel@tonic-gate inst->ri_timeout = NULL; 22150Sstevel@tonic-gate } 22160Sstevel@tonic-gate 22170Sstevel@tonic-gate static int 22180Sstevel@tonic-gate timeout_now() 22190Sstevel@tonic-gate { 22200Sstevel@tonic-gate timeout_entry_t *e; 22210Sstevel@tonic-gate hrtime_t now; 22220Sstevel@tonic-gate int ret; 22230Sstevel@tonic-gate 22240Sstevel@tonic-gate now = gethrtime(); 22250Sstevel@tonic-gate 22260Sstevel@tonic-gate /* 22270Sstevel@tonic-gate * Walk through the (sorted) timeouts list. While the timeout 22280Sstevel@tonic-gate * at the head of the list is <= the current time, kill the 22290Sstevel@tonic-gate * method. 22300Sstevel@tonic-gate */ 22310Sstevel@tonic-gate MUTEX_LOCK(&timeouts->tq_lock); 22320Sstevel@tonic-gate 22330Sstevel@tonic-gate for (e = uu_list_first(timeouts->tq_list); 22340Sstevel@tonic-gate e != NULL && e->te_timeout <= now; 22350Sstevel@tonic-gate e = uu_list_next(timeouts->tq_list, e)) { 22360Sstevel@tonic-gate log_framework(LOG_WARNING, "%s: Method or service exit timed " 22370Sstevel@tonic-gate "out. Killing contract %ld.\n", e->te_fmri, e->te_ctid); 22380Sstevel@tonic-gate log_instance_fmri(e->te_fmri, e->te_logstem, B_TRUE, 22390Sstevel@tonic-gate "Method or service exit timed out. Killing contract %ld", 22400Sstevel@tonic-gate e->te_ctid); 22410Sstevel@tonic-gate e->te_fired = 1; 22420Sstevel@tonic-gate (void) contract_kill(e->te_ctid, SIGKILL, e->te_fmri); 22430Sstevel@tonic-gate } 22440Sstevel@tonic-gate 22450Sstevel@tonic-gate if (uu_list_numnodes(timeouts->tq_list) > 0) 22460Sstevel@tonic-gate ret = 0; 22470Sstevel@tonic-gate else 22480Sstevel@tonic-gate ret = -1; 22490Sstevel@tonic-gate 22500Sstevel@tonic-gate MUTEX_UNLOCK(&timeouts->tq_lock); 22510Sstevel@tonic-gate 22520Sstevel@tonic-gate return (ret); 22530Sstevel@tonic-gate } 22540Sstevel@tonic-gate 22550Sstevel@tonic-gate /* 22560Sstevel@tonic-gate * void *restarter_timeouts_event_thread(void *) 22570Sstevel@tonic-gate * Responsible for monitoring the method timeouts. This thread must 22580Sstevel@tonic-gate * be started before any methods are called. 22590Sstevel@tonic-gate */ 22600Sstevel@tonic-gate /*ARGSUSED*/ 22610Sstevel@tonic-gate static void * 22620Sstevel@tonic-gate restarter_timeouts_event_thread(void *unused) 22630Sstevel@tonic-gate { 22640Sstevel@tonic-gate /* 22650Sstevel@tonic-gate * Timeouts are entered on a priority queue, which is processed by 22660Sstevel@tonic-gate * this thread. As timeouts are specified in seconds, we'll do 22670Sstevel@tonic-gate * the necessary processing every second, as long as the queue 22680Sstevel@tonic-gate * is not empty. 22690Sstevel@tonic-gate */ 22700Sstevel@tonic-gate 22710Sstevel@tonic-gate /*CONSTCOND*/ 22720Sstevel@tonic-gate while (1) { 22730Sstevel@tonic-gate /* 22740Sstevel@tonic-gate * As long as the timeout list isn't empty, process it 22750Sstevel@tonic-gate * every second. 22760Sstevel@tonic-gate */ 22770Sstevel@tonic-gate if (timeout_now() == 0) { 22780Sstevel@tonic-gate (void) sleep(1); 22790Sstevel@tonic-gate continue; 22800Sstevel@tonic-gate } 22810Sstevel@tonic-gate 22820Sstevel@tonic-gate /* The list is empty, wait until we have more timeouts. */ 22830Sstevel@tonic-gate MUTEX_LOCK(&tu->tu_lock); 22840Sstevel@tonic-gate 22850Sstevel@tonic-gate while (tu->tu_wakeup == 0) 22860Sstevel@tonic-gate (void) pthread_cond_wait(&tu->tu_cv, &tu->tu_lock); 22870Sstevel@tonic-gate 22880Sstevel@tonic-gate tu->tu_wakeup = 0; 22890Sstevel@tonic-gate MUTEX_UNLOCK(&tu->tu_lock); 22900Sstevel@tonic-gate } 22910Sstevel@tonic-gate 22920Sstevel@tonic-gate return (NULL); 22930Sstevel@tonic-gate } 22940Sstevel@tonic-gate 22950Sstevel@tonic-gate void 22960Sstevel@tonic-gate restarter_start() 22970Sstevel@tonic-gate { 22980Sstevel@tonic-gate (void) startd_thread_create(restarter_timeouts_event_thread, NULL); 22990Sstevel@tonic-gate (void) startd_thread_create(restarter_event_thread, NULL); 23000Sstevel@tonic-gate (void) startd_thread_create(restarter_contracts_event_thread, NULL); 23010Sstevel@tonic-gate (void) startd_thread_create(wait_thread, NULL); 23020Sstevel@tonic-gate } 23030Sstevel@tonic-gate 23040Sstevel@tonic-gate 23050Sstevel@tonic-gate void 23060Sstevel@tonic-gate restarter_init() 23070Sstevel@tonic-gate { 23080Sstevel@tonic-gate restarter_instance_pool = startd_list_pool_create("restarter_instances", 23090Sstevel@tonic-gate sizeof (restarter_inst_t), offsetof(restarter_inst_t, 23100Sstevel@tonic-gate ri_link), restarter_instance_compare, UU_LIST_POOL_DEBUG); 23110Sstevel@tonic-gate (void) memset(&instance_list, 0, sizeof (instance_list)); 23120Sstevel@tonic-gate 23130Sstevel@tonic-gate (void) pthread_mutex_init(&instance_list.ril_lock, &mutex_attrs); 23140Sstevel@tonic-gate instance_list.ril_instance_list = startd_list_create( 23150Sstevel@tonic-gate restarter_instance_pool, &instance_list, UU_LIST_SORTED); 23160Sstevel@tonic-gate 23170Sstevel@tonic-gate restarter_queue_pool = startd_list_pool_create( 23180Sstevel@tonic-gate "restarter_instance_queue", sizeof (restarter_instance_qentry_t), 23190Sstevel@tonic-gate offsetof(restarter_instance_qentry_t, riq_link), NULL, 23200Sstevel@tonic-gate UU_LIST_POOL_DEBUG); 23210Sstevel@tonic-gate 23220Sstevel@tonic-gate contract_list_pool = startd_list_pool_create( 23230Sstevel@tonic-gate "contract_list", sizeof (contract_entry_t), 23240Sstevel@tonic-gate offsetof(contract_entry_t, ce_link), NULL, 23250Sstevel@tonic-gate UU_LIST_POOL_DEBUG); 23260Sstevel@tonic-gate contract_hash_init(); 23270Sstevel@tonic-gate 23280Sstevel@tonic-gate log_framework(LOG_DEBUG, "Initialized restarter\n"); 23290Sstevel@tonic-gate } 2330