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