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