xref: /titanic_52/usr/src/cmd/svc/startd/restarter.c (revision 1855af6b0ae55b1e6119c15cb3c684bad4b16f64)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*1855af6bSlianep  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * restarter.c - service manipulation
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * This component manages services whose restarter is svc.startd, the standard
337c478bd9Sstevel@tonic-gate  * restarter.  It translates restarter protocol events from the graph engine
347c478bd9Sstevel@tonic-gate  * into actions on processes, as a delegated restarter would do.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * The master restarter manages a number of always-running threads:
377c478bd9Sstevel@tonic-gate  *   - restarter event thread: events from the graph engine
387c478bd9Sstevel@tonic-gate  *   - timeout thread: thread to fire queued timeouts
397c478bd9Sstevel@tonic-gate  *   - contract thread: thread to handle contract events
407c478bd9Sstevel@tonic-gate  *   - wait thread: thread to handle wait-based services
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * The other threads are created as-needed:
437c478bd9Sstevel@tonic-gate  *   - per-instance method threads
447c478bd9Sstevel@tonic-gate  *   - per-instance event processing threads
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * The interaction of all threads must result in the following conditions
477c478bd9Sstevel@tonic-gate  * being satisfied (on a per-instance basis):
487c478bd9Sstevel@tonic-gate  *   - restarter events must be processed in order
497c478bd9Sstevel@tonic-gate  *   - method execution must be serialized
507c478bd9Sstevel@tonic-gate  *   - instance delete must be held until outstanding methods are complete
517c478bd9Sstevel@tonic-gate  *   - contract events shouldn't be processed while a method is running
527c478bd9Sstevel@tonic-gate  *   - timeouts should fire even when a method is running
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * Service instances are represented by restarter_inst_t's and are kept in the
557c478bd9Sstevel@tonic-gate  * instance_list list.
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * Service States
587c478bd9Sstevel@tonic-gate  *   The current state of a service instance is kept in
597c478bd9Sstevel@tonic-gate  *   restarter_inst_t->ri_i.i_state.  If transition to a new state could take
607c478bd9Sstevel@tonic-gate  *   some time, then before we effect the transition we set
617c478bd9Sstevel@tonic-gate  *   restarter_inst_t->ri_i.i_next_state to the target state, and afterwards we
627c478bd9Sstevel@tonic-gate  *   rotate i_next_state to i_state and set i_next_state to
637c478bd9Sstevel@tonic-gate  *   RESTARTER_STATE_NONE.  So usually i_next_state is _NONE when ri_lock is not
647c478bd9Sstevel@tonic-gate  *   held.  The exception is when we launch methods, which are done with
657c478bd9Sstevel@tonic-gate  *   a separate thread.  To keep any other threads from grabbing ri_lock before
667c478bd9Sstevel@tonic-gate  *   method_thread() does, we set ri_method_thread to the thread id of the
677c478bd9Sstevel@tonic-gate  *   method thread, and when it is nonzero any thread with a different thread id
687c478bd9Sstevel@tonic-gate  *   waits on ri_method_cv.
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  * Method execution is serialized by blocking on ri_method_cv in
717c478bd9Sstevel@tonic-gate  * inst_lookup_by_id() and waiting for a 0 value of ri_method_thread.  This
727c478bd9Sstevel@tonic-gate  * also prevents the instance structure from being deleted until all
737c478bd9Sstevel@tonic-gate  * outstanding operations such as method_thread() have finished.
747c478bd9Sstevel@tonic-gate  *
757c478bd9Sstevel@tonic-gate  * Lock ordering:
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * dgraph_lock [can be held when taking:]
787c478bd9Sstevel@tonic-gate  *   utmpx_lock
797c478bd9Sstevel@tonic-gate  *   dictionary->dict_lock
807c478bd9Sstevel@tonic-gate  *   st->st_load_lock
817c478bd9Sstevel@tonic-gate  *   wait_info_lock
827c478bd9Sstevel@tonic-gate  *   ru->restarter_update_lock
837c478bd9Sstevel@tonic-gate  *     restarter_queue->rpeq_lock
847c478bd9Sstevel@tonic-gate  *   instance_list.ril_lock
857c478bd9Sstevel@tonic-gate  *     inst->ri_lock
867c478bd9Sstevel@tonic-gate  *   st->st_configd_live_lock
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * instance_list.ril_lock
897c478bd9Sstevel@tonic-gate  *   graph_queue->gpeq_lock
907c478bd9Sstevel@tonic-gate  *   gu->gu_lock
917c478bd9Sstevel@tonic-gate  *   st->st_configd_live_lock
927c478bd9Sstevel@tonic-gate  *   dictionary->dict_lock
937c478bd9Sstevel@tonic-gate  *   inst->ri_lock
947c478bd9Sstevel@tonic-gate  *     graph_queue->gpeq_lock
957c478bd9Sstevel@tonic-gate  *     gu->gu_lock
967c478bd9Sstevel@tonic-gate  *     tu->tu_lock
977c478bd9Sstevel@tonic-gate  *     tq->tq_lock
987c478bd9Sstevel@tonic-gate  *     inst->ri_queue_lock
997c478bd9Sstevel@tonic-gate  *       wait_info_lock
1007c478bd9Sstevel@tonic-gate  *       bp->cb_lock
1017c478bd9Sstevel@tonic-gate  *     utmpx_lock
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * single_user_thread_lock
1047c478bd9Sstevel@tonic-gate  *   wait_info_lock
1057c478bd9Sstevel@tonic-gate  *   utmpx_lock
1067c478bd9Sstevel@tonic-gate  *
1077c478bd9Sstevel@tonic-gate  * gu_freeze_lock
1087c478bd9Sstevel@tonic-gate  *
1097c478bd9Sstevel@tonic-gate  * logbuf_mutex nests inside pretty much everything.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate #include <sys/contract/process.h>
1137c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
1147c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1157c478bd9Sstevel@tonic-gate #include <sys/time.h>
1167c478bd9Sstevel@tonic-gate #include <sys/types.h>
1177c478bd9Sstevel@tonic-gate #include <sys/uio.h>
1187c478bd9Sstevel@tonic-gate #include <sys/wait.h>
1197c478bd9Sstevel@tonic-gate #include <assert.h>
1207c478bd9Sstevel@tonic-gate #include <errno.h>
1217c478bd9Sstevel@tonic-gate #include <fcntl.h>
1227c478bd9Sstevel@tonic-gate #include <libcontract.h>
1237c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
1247c478bd9Sstevel@tonic-gate #include <libintl.h>
1257c478bd9Sstevel@tonic-gate #include <librestart.h>
1267c478bd9Sstevel@tonic-gate #include <librestart_priv.h>
1277c478bd9Sstevel@tonic-gate #include <libuutil.h>
1287c478bd9Sstevel@tonic-gate #include <limits.h>
1297c478bd9Sstevel@tonic-gate #include <poll.h>
1307c478bd9Sstevel@tonic-gate #include <port.h>
1317c478bd9Sstevel@tonic-gate #include <pthread.h>
1327c478bd9Sstevel@tonic-gate #include <stdarg.h>
1337c478bd9Sstevel@tonic-gate #include <stdio.h>
1347c478bd9Sstevel@tonic-gate #include <strings.h>
1357c478bd9Sstevel@tonic-gate #include <unistd.h>
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate #include "startd.h"
1387c478bd9Sstevel@tonic-gate #include "protocol.h"
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate static uu_list_pool_t *restarter_instance_pool;
1417c478bd9Sstevel@tonic-gate static restarter_instance_list_t instance_list;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static uu_list_pool_t *restarter_queue_pool;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1467c478bd9Sstevel@tonic-gate static int
1477c478bd9Sstevel@tonic-gate restarter_instance_compare(const void *lc_arg, const void *rc_arg,
1487c478bd9Sstevel@tonic-gate     void *private)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	int lc_id = ((const restarter_inst_t *)lc_arg)->ri_id;
1517c478bd9Sstevel@tonic-gate 	int rc_id = *(int *)rc_arg;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (lc_id > rc_id)
1547c478bd9Sstevel@tonic-gate 		return (1);
1557c478bd9Sstevel@tonic-gate 	if (lc_id < rc_id)
1567c478bd9Sstevel@tonic-gate 		return (-1);
1577c478bd9Sstevel@tonic-gate 	return (0);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate static restarter_inst_t *
1617c478bd9Sstevel@tonic-gate inst_lookup_by_name(const char *name)
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	int id;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	id = dict_lookup_byname(name);
1667c478bd9Sstevel@tonic-gate 	if (id == -1)
1677c478bd9Sstevel@tonic-gate 		return (NULL);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	return (inst_lookup_by_id(id));
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate restarter_inst_t *
1737c478bd9Sstevel@tonic-gate inst_lookup_by_id(int id)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	restarter_inst_t *inst;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&instance_list.ril_lock);
1787c478bd9Sstevel@tonic-gate 	inst = uu_list_find(instance_list.ril_instance_list, &id, NULL, NULL);
1797c478bd9Sstevel@tonic-gate 	if (inst != NULL)
1807c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&inst->ri_lock);
1817c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&instance_list.ril_lock);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (inst != NULL) {
1847c478bd9Sstevel@tonic-gate 		while (inst->ri_method_thread != 0 &&
1857c478bd9Sstevel@tonic-gate 		    !pthread_equal(inst->ri_method_thread, pthread_self())) {
1867c478bd9Sstevel@tonic-gate 			++inst->ri_method_waiters;
1877c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&inst->ri_method_cv,
1887c478bd9Sstevel@tonic-gate 			    &inst->ri_lock);
1897c478bd9Sstevel@tonic-gate 			assert(inst->ri_method_waiters > 0);
1907c478bd9Sstevel@tonic-gate 			--inst->ri_method_waiters;
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	return (inst);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate static restarter_inst_t *
1987c478bd9Sstevel@tonic-gate inst_lookup_queue(const char *name)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	int id;
2017c478bd9Sstevel@tonic-gate 	restarter_inst_t *inst;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	id = dict_lookup_byname(name);
2047c478bd9Sstevel@tonic-gate 	if (id == -1)
2057c478bd9Sstevel@tonic-gate 		return (NULL);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&instance_list.ril_lock);
2087c478bd9Sstevel@tonic-gate 	inst = uu_list_find(instance_list.ril_instance_list, &id, NULL, NULL);
2097c478bd9Sstevel@tonic-gate 	if (inst != NULL)
2107c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&inst->ri_queue_lock);
2117c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&instance_list.ril_lock);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	return (inst);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate const char *
2177c478bd9Sstevel@tonic-gate service_style(int flags)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	switch (flags & RINST_STYLE_MASK) {
2207c478bd9Sstevel@tonic-gate 	case RINST_CONTRACT:	return ("contract");
2217c478bd9Sstevel@tonic-gate 	case RINST_TRANSIENT:	return ("transient");
2227c478bd9Sstevel@tonic-gate 	case RINST_WAIT:	return ("wait");
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	default:
2257c478bd9Sstevel@tonic-gate #ifndef NDEBUG
2267c478bd9Sstevel@tonic-gate 		uu_warn("%s:%d: Bad flags 0x%x.\n", __FILE__, __LINE__, flags);
2277c478bd9Sstevel@tonic-gate #endif
2287c478bd9Sstevel@tonic-gate 		abort();
2297c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * Fails with ECONNABORTED or ECANCELED.
2357c478bd9Sstevel@tonic-gate  */
2367c478bd9Sstevel@tonic-gate static int
2377c478bd9Sstevel@tonic-gate check_contract(restarter_inst_t *inst, boolean_t primary,
2387c478bd9Sstevel@tonic-gate     scf_instance_t *scf_inst)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	ctid_t *ctidp;
2417c478bd9Sstevel@tonic-gate 	int fd, r;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	ctidp = primary ? &inst->ri_i.i_primary_ctid :
2447c478bd9Sstevel@tonic-gate 	    &inst->ri_i.i_transient_ctid;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	assert(*ctidp >= 1);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	fd = contract_open(*ctidp, NULL, "status", O_RDONLY);
2497c478bd9Sstevel@tonic-gate 	if (fd >= 0) {
2507c478bd9Sstevel@tonic-gate 		r = close(fd);
2517c478bd9Sstevel@tonic-gate 		assert(r == 0);
2527c478bd9Sstevel@tonic-gate 		return (0);
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	r = restarter_remove_contract(scf_inst, *ctidp, primary ?
2567c478bd9Sstevel@tonic-gate 	    RESTARTER_CONTRACT_PRIMARY : RESTARTER_CONTRACT_TRANSIENT);
2577c478bd9Sstevel@tonic-gate 	switch (r) {
2587c478bd9Sstevel@tonic-gate 	case 0:
2597c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
2607c478bd9Sstevel@tonic-gate 	case ECANCELED:
2617c478bd9Sstevel@tonic-gate 		*ctidp = 0;
2627c478bd9Sstevel@tonic-gate 		return (r);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	case ENOMEM:
2657c478bd9Sstevel@tonic-gate 		uu_die("Out of memory\n");
2667c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	case EPERM:
2697c478bd9Sstevel@tonic-gate 		uu_die("Insufficient privilege.\n");
2707c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	case EACCES:
2737c478bd9Sstevel@tonic-gate 		uu_die("Repository backend access denied.\n");
2747c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	case EROFS:
2777c478bd9Sstevel@tonic-gate 		log_error(LOG_INFO, "Could not remove unusable contract id %ld "
2787c478bd9Sstevel@tonic-gate 		    "for %s from repository.\n", *ctidp, inst->ri_i.i_fmri);
2797c478bd9Sstevel@tonic-gate 		return (0);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	case EINVAL:
2827c478bd9Sstevel@tonic-gate 	case EBADF:
2837c478bd9Sstevel@tonic-gate 	default:
2847c478bd9Sstevel@tonic-gate 		assert(0);
2857c478bd9Sstevel@tonic-gate 		abort();
2867c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate static int stop_instance(scf_handle_t *, restarter_inst_t *, stop_cause_t);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * int restarter_insert_inst(scf_handle_t *, char *)
2947c478bd9Sstevel@tonic-gate  *   If the inst is already in the restarter list, return its id.  If the inst
2957c478bd9Sstevel@tonic-gate  *   is not in the restarter list, initialize a restarter_inst_t, initialize its
2967c478bd9Sstevel@tonic-gate  *   states, insert it into the list, and return 0.
2977c478bd9Sstevel@tonic-gate  *
2987c478bd9Sstevel@tonic-gate  *   Fails with
2997c478bd9Sstevel@tonic-gate  *     ENOENT - name is not in the repository
3007c478bd9Sstevel@tonic-gate  */
3017c478bd9Sstevel@tonic-gate static int
3027c478bd9Sstevel@tonic-gate restarter_insert_inst(scf_handle_t *h, const char *name)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	int id, r;
3057c478bd9Sstevel@tonic-gate 	restarter_inst_t *inst;
3067c478bd9Sstevel@tonic-gate 	uu_list_index_t idx;
3077c478bd9Sstevel@tonic-gate 	scf_service_t *scf_svc;
3087c478bd9Sstevel@tonic-gate 	scf_instance_t *scf_inst;
3097c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
3107c478bd9Sstevel@tonic-gate 	scf_propertygroup_t *pg;
3117c478bd9Sstevel@tonic-gate 	char *svc_name, *inst_name;
3127c478bd9Sstevel@tonic-gate 	char logfilebuf[PATH_MAX];
3137c478bd9Sstevel@tonic-gate 	char *c;
3147c478bd9Sstevel@tonic-gate 	boolean_t do_commit_states;
3157c478bd9Sstevel@tonic-gate 	restarter_instance_state_t state, next_state;
3167c478bd9Sstevel@tonic-gate 	protocol_states_t *ps;
3177c478bd9Sstevel@tonic-gate 	pid_t start_pid;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&instance_list.ril_lock);
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	/*
3227c478bd9Sstevel@tonic-gate 	 * We don't use inst_lookup_by_name() here because we want the lookup
3237c478bd9Sstevel@tonic-gate 	 * & insert to be atomic.
3247c478bd9Sstevel@tonic-gate 	 */
3257c478bd9Sstevel@tonic-gate 	id = dict_lookup_byname(name);
3267c478bd9Sstevel@tonic-gate 	if (id != -1) {
3277c478bd9Sstevel@tonic-gate 		inst = uu_list_find(instance_list.ril_instance_list, &id, NULL,
3287c478bd9Sstevel@tonic-gate 		    &idx);
3297c478bd9Sstevel@tonic-gate 		if (inst != NULL) {
3307c478bd9Sstevel@tonic-gate 			MUTEX_UNLOCK(&instance_list.ril_lock);
3317c478bd9Sstevel@tonic-gate 			return (0);
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	/* Allocate an instance */
3367c478bd9Sstevel@tonic-gate 	inst = startd_zalloc(sizeof (restarter_inst_t));
3377c478bd9Sstevel@tonic-gate 	inst->ri_utmpx_prefix = startd_alloc(max_scf_value_size);
3387c478bd9Sstevel@tonic-gate 	inst->ri_utmpx_prefix[0] = '\0';
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	inst->ri_i.i_fmri = startd_alloc(strlen(name) + 1);
3417c478bd9Sstevel@tonic-gate 	(void) strcpy((char *)inst->ri_i.i_fmri, name);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	inst->ri_queue = startd_list_create(restarter_queue_pool, inst, 0);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	/*
3467c478bd9Sstevel@tonic-gate 	 * id shouldn't be -1 since we use the same dictionary as graph.c, but
3477c478bd9Sstevel@tonic-gate 	 * just in case.
3487c478bd9Sstevel@tonic-gate 	 */
3497c478bd9Sstevel@tonic-gate 	inst->ri_id = (id != -1 ? id : dict_insert(name));
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	special_online_hooks_get(name, &inst->ri_pre_online_hook,
3527c478bd9Sstevel@tonic-gate 	    &inst->ri_post_online_hook, &inst->ri_post_offline_hook);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	scf_svc = safe_scf_service_create(h);
3557c478bd9Sstevel@tonic-gate 	scf_inst = safe_scf_instance_create(h);
3567c478bd9Sstevel@tonic-gate 	pg = safe_scf_pg_create(h);
3577c478bd9Sstevel@tonic-gate 	svc_name = startd_alloc(max_scf_name_size);
3587c478bd9Sstevel@tonic-gate 	inst_name = startd_alloc(max_scf_name_size);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate rep_retry:
3617c478bd9Sstevel@tonic-gate 	if (scf_handle_decode_fmri(h, name, NULL, scf_svc, scf_inst, NULL,
3627c478bd9Sstevel@tonic-gate 	    NULL, SCF_DECODE_FMRI_EXACT) != 0) {
3637c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
3647c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
3657c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
3667c478bd9Sstevel@tonic-gate 			goto rep_retry;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
3697c478bd9Sstevel@tonic-gate deleted:
3707c478bd9Sstevel@tonic-gate 			MUTEX_UNLOCK(&instance_list.ril_lock);
3717c478bd9Sstevel@tonic-gate 			startd_free(inst_name, max_scf_name_size);
3727c478bd9Sstevel@tonic-gate 			startd_free(svc_name, max_scf_name_size);
3737c478bd9Sstevel@tonic-gate 			scf_pg_destroy(pg);
3747c478bd9Sstevel@tonic-gate 			scf_instance_destroy(scf_inst);
3757c478bd9Sstevel@tonic-gate 			scf_service_destroy(scf_svc);
3767c478bd9Sstevel@tonic-gate 			startd_free((void *)inst->ri_i.i_fmri,
3777c478bd9Sstevel@tonic-gate 			    strlen(inst->ri_i.i_fmri) + 1);
3787c478bd9Sstevel@tonic-gate 			startd_free(inst, sizeof (restarter_inst_t));
3797c478bd9Sstevel@tonic-gate 			return (ENOENT);
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 		uu_die("Can't decode FMRI %s: %s\n", name,
3837c478bd9Sstevel@tonic-gate 		    scf_strerror(scf_error()));
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/*
3877c478bd9Sstevel@tonic-gate 	 * If there's no running snapshot, then we execute using the editing
3887c478bd9Sstevel@tonic-gate 	 * snapshot.  Pending snapshots will be taken later.
3897c478bd9Sstevel@tonic-gate 	 */
3907c478bd9Sstevel@tonic-gate 	snap = libscf_get_running_snapshot(scf_inst);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if ((scf_service_get_name(scf_svc, svc_name, max_scf_name_size) < 0) ||
3937c478bd9Sstevel@tonic-gate 	    (scf_instance_get_name(scf_inst, inst_name, max_scf_name_size) <
3947c478bd9Sstevel@tonic-gate 	    0)) {
3957c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
3967c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
3977c478bd9Sstevel@tonic-gate 			break;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
4007c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
4017c478bd9Sstevel@tonic-gate 			goto rep_retry;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 		default:
4047c478bd9Sstevel@tonic-gate 			assert(0);
4057c478bd9Sstevel@tonic-gate 			abort();
4067c478bd9Sstevel@tonic-gate 		}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
4097c478bd9Sstevel@tonic-gate 		goto deleted;
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 
412*1855af6bSlianep 	(void) snprintf(logfilebuf, PATH_MAX, "%s:%s", svc_name, inst_name);
413*1855af6bSlianep 	for (c = logfilebuf; *c != '\0'; c++)
414*1855af6bSlianep 		if (*c == '/')
415*1855af6bSlianep 			*c = '-';
416*1855af6bSlianep 
417*1855af6bSlianep 	inst->ri_logstem = startd_alloc(PATH_MAX);
418*1855af6bSlianep 	(void) snprintf(inst->ri_logstem, PATH_MAX, "%s%s", logfilebuf,
419*1855af6bSlianep 	    LOG_SUFFIX);
420*1855af6bSlianep 
4217c478bd9Sstevel@tonic-gate 	/*
4227c478bd9Sstevel@tonic-gate 	 * If the restarter group is missing, use uninit/none.  Otherwise,
4237c478bd9Sstevel@tonic-gate 	 * we're probably being restarted & don't want to mess up the states
4247c478bd9Sstevel@tonic-gate 	 * that are there.
4257c478bd9Sstevel@tonic-gate 	 */
4267c478bd9Sstevel@tonic-gate 	state = RESTARTER_STATE_UNINIT;
4277c478bd9Sstevel@tonic-gate 	next_state = RESTARTER_STATE_NONE;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	r = scf_instance_get_pg(scf_inst, SCF_PG_RESTARTER, pg);
4307c478bd9Sstevel@tonic-gate 	if (r != 0) {
4317c478bd9Sstevel@tonic-gate 		switch (scf_error()) {
4327c478bd9Sstevel@tonic-gate 		case SCF_ERROR_CONNECTION_BROKEN:
4337c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
4347c478bd9Sstevel@tonic-gate 			goto rep_retry;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_SET:
4377c478bd9Sstevel@tonic-gate 			scf_snapshot_destroy(snap);
4387c478bd9Sstevel@tonic-gate 			goto deleted;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		case SCF_ERROR_NOT_FOUND:
4417c478bd9Sstevel@tonic-gate 			/*
4427c478bd9Sstevel@tonic-gate 			 * This shouldn't happen since the graph engine should
4437c478bd9Sstevel@tonic-gate 			 * have initialized the state to uninitialized/none if
4447c478bd9Sstevel@tonic-gate 			 * there was no restarter pg.  In case somebody
4457c478bd9Sstevel@tonic-gate 			 * deleted it, though....
4467c478bd9Sstevel@tonic-gate 			 */
4477c478bd9Sstevel@tonic-gate 			do_commit_states = B_TRUE;
4487c478bd9Sstevel@tonic-gate 			break;
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		default:
4517c478bd9Sstevel@tonic-gate 			assert(0);
4527c478bd9Sstevel@tonic-gate 			abort();
4537c478bd9Sstevel@tonic-gate 		}
4547c478bd9Sstevel@tonic-gate 	} else {
4557c478bd9Sstevel@tonic-gate 		r = libscf_read_states(pg, &state, &next_state);
4567c478bd9Sstevel@tonic-gate 		if (r != 0) {
4577c478bd9Sstevel@tonic-gate 			do_commit_states = B_TRUE;
4587c478bd9Sstevel@tonic-gate 		} else {
4597c478bd9Sstevel@tonic-gate 			if (next_state != RESTARTER_STATE_NONE) {
4607c478bd9Sstevel@tonic-gate 				/*
4617c478bd9Sstevel@tonic-gate 				 * Force next_state to _NONE since we
4627c478bd9Sstevel@tonic-gate 				 * don't look for method processes.
4637c478bd9Sstevel@tonic-gate 				 */
4647c478bd9Sstevel@tonic-gate 				next_state = RESTARTER_STATE_NONE;
4657c478bd9Sstevel@tonic-gate 				do_commit_states = B_TRUE;
4667c478bd9Sstevel@tonic-gate 			} else {
4677c478bd9Sstevel@tonic-gate 				/*
4687c478bd9Sstevel@tonic-gate 				 * Inform the restarter of our state without
4697c478bd9Sstevel@tonic-gate 				 * changing the STIME in the repository.
4707c478bd9Sstevel@tonic-gate 				 */
4717c478bd9Sstevel@tonic-gate 				ps = startd_alloc(sizeof (*ps));
4727c478bd9Sstevel@tonic-gate 				inst->ri_i.i_state = ps->ps_state = state;
4737c478bd9Sstevel@tonic-gate 				inst->ri_i.i_next_state = ps->ps_state_next =
4747c478bd9Sstevel@tonic-gate 				    next_state;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 				graph_protocol_send_event(inst->ri_i.i_fmri,
4777c478bd9Sstevel@tonic-gate 				    GRAPH_UPDATE_STATE_CHANGE, ps);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 				do_commit_states = B_FALSE;
4807c478bd9Sstevel@tonic-gate 			}
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	switch (libscf_get_startd_properties(scf_inst, snap, &inst->ri_flags,
4857c478bd9Sstevel@tonic-gate 	    &inst->ri_utmpx_prefix)) {
4867c478bd9Sstevel@tonic-gate 	case 0:
4877c478bd9Sstevel@tonic-gate 		break;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
4907c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
4917c478bd9Sstevel@tonic-gate 		goto rep_retry;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	case ECANCELED:
4947c478bd9Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
4957c478bd9Sstevel@tonic-gate 		startd_free(inst->ri_utmpx_prefix, max_scf_value_size);
4967c478bd9Sstevel@tonic-gate 		goto deleted;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	case ENOENT:
4997c478bd9Sstevel@tonic-gate 		/*
5007c478bd9Sstevel@tonic-gate 		 * This is odd, because the graph engine should have required
5017c478bd9Sstevel@tonic-gate 		 * the general property group.  So we'll just use default
5027c478bd9Sstevel@tonic-gate 		 * flags in anticipation of the graph engine sending us
5037c478bd9Sstevel@tonic-gate 		 * REMOVE_INSTANCE when it finds out that the general property
5047c478bd9Sstevel@tonic-gate 		 * group has been deleted.
5057c478bd9Sstevel@tonic-gate 		 */
5067c478bd9Sstevel@tonic-gate 		inst->ri_flags = RINST_CONTRACT;
5077c478bd9Sstevel@tonic-gate 		break;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	default:
5107c478bd9Sstevel@tonic-gate 		assert(0);
5117c478bd9Sstevel@tonic-gate 		abort();
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	switch (libscf_get_template_values(scf_inst, snap,
5157c478bd9Sstevel@tonic-gate 	    &inst->ri_common_name, &inst->ri_C_common_name)) {
5167c478bd9Sstevel@tonic-gate 	case 0:
5177c478bd9Sstevel@tonic-gate 		break;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
5207c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
5217c478bd9Sstevel@tonic-gate 		goto rep_retry;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	case ECANCELED:
5247c478bd9Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
5257c478bd9Sstevel@tonic-gate 		startd_free(inst->ri_common_name, max_scf_value_size);
5267c478bd9Sstevel@tonic-gate 		inst->ri_common_name = NULL;
5277c478bd9Sstevel@tonic-gate 		goto deleted;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	case ECHILD:
5307c478bd9Sstevel@tonic-gate 	case ENOENT:
5317c478bd9Sstevel@tonic-gate 		break;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	default:
5347c478bd9Sstevel@tonic-gate 		assert(0);
5357c478bd9Sstevel@tonic-gate 		abort();
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	switch (libscf_read_method_ids(h, scf_inst, inst->ri_i.i_fmri,
5397c478bd9Sstevel@tonic-gate 	    &inst->ri_i.i_primary_ctid, &inst->ri_i.i_transient_ctid,
5407c478bd9Sstevel@tonic-gate 	    &start_pid)) {
5417c478bd9Sstevel@tonic-gate 	case 0:
5427c478bd9Sstevel@tonic-gate 		break;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
5457c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
5467c478bd9Sstevel@tonic-gate 		goto rep_retry;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	case ECANCELED:
5497c478bd9Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
5507c478bd9Sstevel@tonic-gate 		goto deleted;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	default:
5537c478bd9Sstevel@tonic-gate 		assert(0);
5547c478bd9Sstevel@tonic-gate 		abort();
5557c478bd9Sstevel@tonic-gate 	}
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	if (inst->ri_i.i_primary_ctid >= 1) {
5587c478bd9Sstevel@tonic-gate 		contract_hash_store(inst->ri_i.i_primary_ctid, inst->ri_id);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 		switch (check_contract(inst, B_TRUE, scf_inst)) {
5617c478bd9Sstevel@tonic-gate 		case 0:
5627c478bd9Sstevel@tonic-gate 			break;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
5657c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
5667c478bd9Sstevel@tonic-gate 			goto rep_retry;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 		case ECANCELED:
5697c478bd9Sstevel@tonic-gate 			scf_snapshot_destroy(snap);
5707c478bd9Sstevel@tonic-gate 			goto deleted;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 		default:
5737c478bd9Sstevel@tonic-gate 			assert(0);
5747c478bd9Sstevel@tonic-gate 			abort();
5757c478bd9Sstevel@tonic-gate 		}
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	if (inst->ri_i.i_transient_ctid >= 1) {
5797c478bd9Sstevel@tonic-gate 		switch (check_contract(inst, B_FALSE, scf_inst)) {
5807c478bd9Sstevel@tonic-gate 		case 0:
5817c478bd9Sstevel@tonic-gate 			break;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 		case ECONNABORTED:
5847c478bd9Sstevel@tonic-gate 			libscf_handle_rebind(h);
5857c478bd9Sstevel@tonic-gate 			goto rep_retry;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		case ECANCELED:
5887c478bd9Sstevel@tonic-gate 			scf_snapshot_destroy(snap);
5897c478bd9Sstevel@tonic-gate 			goto deleted;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		default:
5927c478bd9Sstevel@tonic-gate 			assert(0);
5937c478bd9Sstevel@tonic-gate 			abort();
5947c478bd9Sstevel@tonic-gate 		}
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	/* No more failures we live through, so add it to the list. */
5987c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&inst->ri_lock, &mutex_attrs);
5997c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&inst->ri_queue_lock, &mutex_attrs);
6007c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&inst->ri_lock);
6017c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&inst->ri_queue_lock);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	(void) pthread_cond_init(&inst->ri_method_cv, NULL);
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	uu_list_node_init(inst, &inst->ri_link, restarter_instance_pool);
6067c478bd9Sstevel@tonic-gate 	uu_list_insert(instance_list.ril_instance_list, inst, idx);
6077c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&instance_list.ril_lock);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if (start_pid != -1 &&
6107c478bd9Sstevel@tonic-gate 	    (inst->ri_flags & RINST_STYLE_MASK) == RINST_WAIT) {
6117c478bd9Sstevel@tonic-gate 		int ret;
6127c478bd9Sstevel@tonic-gate 		ret = wait_register(start_pid, inst->ri_i.i_fmri, 0, 1);
6137c478bd9Sstevel@tonic-gate 		if (ret == -1) {
6147c478bd9Sstevel@tonic-gate 			/*
6157c478bd9Sstevel@tonic-gate 			 * Implication:  if we can't reregister the
6167c478bd9Sstevel@tonic-gate 			 * instance, we will start another one.  Two
6177c478bd9Sstevel@tonic-gate 			 * instances may or may not result in a resource
6187c478bd9Sstevel@tonic-gate 			 * conflict.
6197c478bd9Sstevel@tonic-gate 			 */
6207c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
6217c478bd9Sstevel@tonic-gate 			    "%s: couldn't reregister %ld for wait\n",
6227c478bd9Sstevel@tonic-gate 			    inst->ri_i.i_fmri, start_pid);
6237c478bd9Sstevel@tonic-gate 		} else if (ret == 1) {
6247c478bd9Sstevel@tonic-gate 			/*
6257c478bd9Sstevel@tonic-gate 			 * Leading PID has exited.
6267c478bd9Sstevel@tonic-gate 			 */
6277c478bd9Sstevel@tonic-gate 			(void) stop_instance(h, inst, RSTOP_EXIT);
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	scf_pg_destroy(pg);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	if (do_commit_states)
6357c478bd9Sstevel@tonic-gate 		(void) restarter_instance_update_states(h, inst, state,
6367c478bd9Sstevel@tonic-gate 		    next_state, RERR_NONE, NULL);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "%s is a %s-style service\n", name,
6397c478bd9Sstevel@tonic-gate 	    service_style(inst->ri_flags));
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&inst->ri_queue_lock);
6427c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&inst->ri_lock);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	startd_free(svc_name, max_scf_name_size);
6457c478bd9Sstevel@tonic-gate 	startd_free(inst_name, max_scf_name_size);
6467c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
6477c478bd9Sstevel@tonic-gate 	scf_instance_destroy(scf_inst);
6487c478bd9Sstevel@tonic-gate 	scf_service_destroy(scf_svc);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "%s: inserted instance into restarter list\n",
6517c478bd9Sstevel@tonic-gate 	    name);
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	return (0);
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate static void
6577c478bd9Sstevel@tonic-gate restarter_delete_inst(restarter_inst_t *ri)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate 	int id;
6607c478bd9Sstevel@tonic-gate 	restarter_inst_t *rip;
6617c478bd9Sstevel@tonic-gate 	void *cookie = NULL;
6627c478bd9Sstevel@tonic-gate 	restarter_instance_qentry_t *e;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&ri->ri_lock));
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	/*
6677c478bd9Sstevel@tonic-gate 	 * Must drop the instance lock so we can pick up the instance_list
6687c478bd9Sstevel@tonic-gate 	 * lock & remove the instance.
6697c478bd9Sstevel@tonic-gate 	 */
6707c478bd9Sstevel@tonic-gate 	id = ri->ri_id;
6717c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&ri->ri_lock);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&instance_list.ril_lock);
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 	rip = uu_list_find(instance_list.ril_instance_list, &id, NULL, NULL);
6767c478bd9Sstevel@tonic-gate 	if (rip == NULL) {
6777c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&instance_list.ril_lock);
6787c478bd9Sstevel@tonic-gate 		return;
6797c478bd9Sstevel@tonic-gate 	}
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	assert(ri == rip);
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	uu_list_remove(instance_list.ril_instance_list, ri);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "%s: deleted instance from restarter list\n",
6867c478bd9Sstevel@tonic-gate 	    ri->ri_i.i_fmri);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&instance_list.ril_lock);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	/*
6917c478bd9Sstevel@tonic-gate 	 * We can lock the instance without holding the instance_list lock
6927c478bd9Sstevel@tonic-gate 	 * since we removed the instance from the list.
6937c478bd9Sstevel@tonic-gate 	 */
6947c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&ri->ri_lock);
6957c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&ri->ri_queue_lock);
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	if (ri->ri_i.i_primary_ctid >= 1)
6987c478bd9Sstevel@tonic-gate 		contract_hash_remove(ri->ri_i.i_primary_ctid);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	while (ri->ri_method_thread != 0 || ri->ri_method_waiters > 0)
7017c478bd9Sstevel@tonic-gate 		(void) pthread_cond_wait(&ri->ri_method_cv, &ri->ri_lock);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	while ((e = uu_list_teardown(ri->ri_queue, &cookie)) != NULL)
7047c478bd9Sstevel@tonic-gate 		startd_free(e, sizeof (*e));
7057c478bd9Sstevel@tonic-gate 	uu_list_destroy(ri->ri_queue);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	startd_free((void *)ri->ri_i.i_fmri, strlen(ri->ri_i.i_fmri) + 1);
708*1855af6bSlianep 	startd_free(ri->ri_logstem, PATH_MAX);
7097c478bd9Sstevel@tonic-gate 	startd_free(ri->ri_utmpx_prefix, max_scf_value_size);
7107c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&ri->ri_lock);
7117c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_destroy(&ri->ri_queue_lock);
7127c478bd9Sstevel@tonic-gate 	startd_free(ri, sizeof (restarter_inst_t));
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate /*
7167c478bd9Sstevel@tonic-gate  * instance_is_wait_style()
7177c478bd9Sstevel@tonic-gate  *
7187c478bd9Sstevel@tonic-gate  *   Returns 1 if the given instance is a "wait-style" service instance.
7197c478bd9Sstevel@tonic-gate  */
7207c478bd9Sstevel@tonic-gate int
7217c478bd9Sstevel@tonic-gate instance_is_wait_style(restarter_inst_t *inst)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&inst->ri_lock));
7247c478bd9Sstevel@tonic-gate 	return ((inst->ri_flags & RINST_STYLE_MASK) == RINST_WAIT);
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate /*
7287c478bd9Sstevel@tonic-gate  * instance_is_transient_style()
7297c478bd9Sstevel@tonic-gate  *
7307c478bd9Sstevel@tonic-gate  *   Returns 1 if the given instance is a transient service instance.
7317c478bd9Sstevel@tonic-gate  */
7327c478bd9Sstevel@tonic-gate int
7337c478bd9Sstevel@tonic-gate instance_is_transient_style(restarter_inst_t *inst)
7347c478bd9Sstevel@tonic-gate {
7357c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&inst->ri_lock));
7367c478bd9Sstevel@tonic-gate 	return ((inst->ri_flags & RINST_STYLE_MASK) == RINST_TRANSIENT);
7377c478bd9Sstevel@tonic-gate }
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate /*
7407c478bd9Sstevel@tonic-gate  * instance_in_transition()
7417c478bd9Sstevel@tonic-gate  * Returns 1 if instance is in transition, 0 if not
7427c478bd9Sstevel@tonic-gate  */
7437c478bd9Sstevel@tonic-gate int
7447c478bd9Sstevel@tonic-gate instance_in_transition(restarter_inst_t *inst)
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&inst->ri_lock));
7477c478bd9Sstevel@tonic-gate 	if (inst->ri_i.i_next_state == RESTARTER_STATE_NONE)
7487c478bd9Sstevel@tonic-gate 		return (0);
7497c478bd9Sstevel@tonic-gate 	return (1);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate  * Returns
7547c478bd9Sstevel@tonic-gate  *   0 - success
7557c478bd9Sstevel@tonic-gate  *   ECONNRESET - success, but h was rebound
7567c478bd9Sstevel@tonic-gate  */
7577c478bd9Sstevel@tonic-gate int
7587c478bd9Sstevel@tonic-gate restarter_instance_update_states(scf_handle_t *h, restarter_inst_t *ri,
7597c478bd9Sstevel@tonic-gate     restarter_instance_state_t new_state,
7607c478bd9Sstevel@tonic-gate     restarter_instance_state_t new_state_next, restarter_error_t err, char *aux)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	protocol_states_t *states;
7637c478bd9Sstevel@tonic-gate 	int e;
7647c478bd9Sstevel@tonic-gate 	uint_t retry_count = 0, msecs = ALLOC_DELAY;
7657c478bd9Sstevel@tonic-gate 	boolean_t rebound = B_FALSE;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&ri->ri_lock));
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate retry:
7707c478bd9Sstevel@tonic-gate 	e = _restarter_commit_states(h, &ri->ri_i, new_state, new_state_next,
7717c478bd9Sstevel@tonic-gate 	    aux);
7727c478bd9Sstevel@tonic-gate 	switch (e) {
7737c478bd9Sstevel@tonic-gate 	case 0:
7747c478bd9Sstevel@tonic-gate 		break;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	case ENOMEM:
7777c478bd9Sstevel@tonic-gate 		++retry_count;
7787c478bd9Sstevel@tonic-gate 		if (retry_count < ALLOC_RETRY) {
7797c478bd9Sstevel@tonic-gate 			(void) poll(NULL, 0, msecs);
7807c478bd9Sstevel@tonic-gate 			msecs *= ALLOC_DELAY_MULT;
7817c478bd9Sstevel@tonic-gate 			goto retry;
7827c478bd9Sstevel@tonic-gate 		}
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		/* Like startd_alloc(). */
7857c478bd9Sstevel@tonic-gate 		uu_die("Insufficient memory.\n");
7867c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
7897c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
7907c478bd9Sstevel@tonic-gate 		rebound = B_TRUE;
7917c478bd9Sstevel@tonic-gate 		goto retry;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	case EPERM:
7947c478bd9Sstevel@tonic-gate 	case EACCES:
7957c478bd9Sstevel@tonic-gate 	case EROFS:
7967c478bd9Sstevel@tonic-gate 		log_error(LOG_NOTICE, "Could not commit state change for %s "
7977c478bd9Sstevel@tonic-gate 		    "to repository: %s.\n", ri->ri_i.i_fmri, strerror(e));
7987c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	case ENOENT:
8017c478bd9Sstevel@tonic-gate 		ri->ri_i.i_state = new_state;
8027c478bd9Sstevel@tonic-gate 		ri->ri_i.i_next_state = new_state_next;
8037c478bd9Sstevel@tonic-gate 		break;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	case EINVAL:
8067c478bd9Sstevel@tonic-gate 	default:
8077c478bd9Sstevel@tonic-gate 		bad_error("_restarter_commit_states", e);
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	states = startd_alloc(sizeof (protocol_states_t));
8117c478bd9Sstevel@tonic-gate 	states->ps_state = new_state;
8127c478bd9Sstevel@tonic-gate 	states->ps_state_next = new_state_next;
8137c478bd9Sstevel@tonic-gate 	states->ps_err = err;
8147c478bd9Sstevel@tonic-gate 	graph_protocol_send_event(ri->ri_i.i_fmri, GRAPH_UPDATE_STATE_CHANGE,
8157c478bd9Sstevel@tonic-gate 	    (void *)states);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	if (new_state == RESTARTER_STATE_ONLINE)
8187c478bd9Sstevel@tonic-gate 		ri->ri_post_online_hook();
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	return (rebound ? ECONNRESET : 0);
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate void
8247c478bd9Sstevel@tonic-gate restarter_mark_pending_snapshot(const char *fmri, uint_t flag)
8257c478bd9Sstevel@tonic-gate {
8267c478bd9Sstevel@tonic-gate 	restarter_inst_t *inst;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	assert(flag == RINST_RETAKE_RUNNING || flag == RINST_RETAKE_START);
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	inst = inst_lookup_by_name(fmri);
8317c478bd9Sstevel@tonic-gate 	if (inst == NULL)
8327c478bd9Sstevel@tonic-gate 		return;
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	inst->ri_flags |= flag;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&inst->ri_lock);
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate static void
8407c478bd9Sstevel@tonic-gate restarter_take_pending_snapshots(scf_handle_t *h)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate 	restarter_inst_t *inst;
8437c478bd9Sstevel@tonic-gate 	int r;
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&instance_list.ril_lock);
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	for (inst = uu_list_first(instance_list.ril_instance_list);
8487c478bd9Sstevel@tonic-gate 	    inst != NULL;
8497c478bd9Sstevel@tonic-gate 	    inst = uu_list_next(instance_list.ril_instance_list, inst)) {
8507c478bd9Sstevel@tonic-gate 		const char *fmri;
8517c478bd9Sstevel@tonic-gate 		scf_instance_t *sinst = NULL;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&inst->ri_lock);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 		/*
8567c478bd9Sstevel@tonic-gate 		 * This is where we'd check inst->ri_method_thread and if it
8577c478bd9Sstevel@tonic-gate 		 * were nonzero we'd wait in anticipation of another thread
8587c478bd9Sstevel@tonic-gate 		 * executing a method for inst.  Doing so with the instance_list
8597c478bd9Sstevel@tonic-gate 		 * locked, though, leads to deadlock.  Since taking a snapshot
8607c478bd9Sstevel@tonic-gate 		 * during that window won't hurt anything, we'll just continue.
8617c478bd9Sstevel@tonic-gate 		 */
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 		fmri = inst->ri_i.i_fmri;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 		if (inst->ri_flags & RINST_RETAKE_RUNNING) {
8667c478bd9Sstevel@tonic-gate 			scf_snapshot_t *rsnap;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 			(void) libscf_fmri_get_instance(h, fmri, &sinst);
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 			rsnap = libscf_get_or_make_running_snapshot(sinst,
8717c478bd9Sstevel@tonic-gate 			    fmri, B_FALSE);
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 			scf_instance_destroy(sinst);
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 			if (rsnap != NULL)
8767c478bd9Sstevel@tonic-gate 				inst->ri_flags &= ~RINST_RETAKE_RUNNING;
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 			scf_snapshot_destroy(rsnap);
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 		if (inst->ri_flags & RINST_RETAKE_START) {
8827c478bd9Sstevel@tonic-gate 			switch (r = libscf_snapshots_poststart(h, fmri,
8837c478bd9Sstevel@tonic-gate 			    B_FALSE)) {
8847c478bd9Sstevel@tonic-gate 			case 0:
8857c478bd9Sstevel@tonic-gate 			case ENOENT:
8867c478bd9Sstevel@tonic-gate 				inst->ri_flags &= ~RINST_RETAKE_START;
8877c478bd9Sstevel@tonic-gate 				break;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 			case ECONNABORTED:
8907c478bd9Sstevel@tonic-gate 				break;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 			case EACCES:
8937c478bd9Sstevel@tonic-gate 			default:
8947c478bd9Sstevel@tonic-gate 				bad_error("libscf_snapshots_poststart", r);
8957c478bd9Sstevel@tonic-gate 			}
8967c478bd9Sstevel@tonic-gate 		}
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&inst->ri_lock);
8997c478bd9Sstevel@tonic-gate 	}
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&instance_list.ril_lock);
9027c478bd9Sstevel@tonic-gate }
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate /* ARGSUSED */
9057c478bd9Sstevel@tonic-gate void *
9067c478bd9Sstevel@tonic-gate restarter_post_fsminimal_thread(void *unused)
9077c478bd9Sstevel@tonic-gate {
9087c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
9097c478bd9Sstevel@tonic-gate 	int r;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	for (;;) {
9147c478bd9Sstevel@tonic-gate 		r = libscf_create_self(h);
9157c478bd9Sstevel@tonic-gate 		if (r == 0)
9167c478bd9Sstevel@tonic-gate 			break;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 		assert(r == ECONNABORTED);
9197c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	restarter_take_pending_snapshots(h);
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	(void) scf_handle_unbind(h);
9257c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	return (NULL);
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate /*
9317c478bd9Sstevel@tonic-gate  * returns 1 if instance is already started, 0 if not
9327c478bd9Sstevel@tonic-gate  */
9337c478bd9Sstevel@tonic-gate static int
9347c478bd9Sstevel@tonic-gate instance_started(restarter_inst_t *inst)
9357c478bd9Sstevel@tonic-gate {
9367c478bd9Sstevel@tonic-gate 	int ret;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&inst->ri_lock));
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	if (inst->ri_i.i_state == RESTARTER_STATE_ONLINE ||
9417c478bd9Sstevel@tonic-gate 	    inst->ri_i.i_state == RESTARTER_STATE_DEGRADED)
9427c478bd9Sstevel@tonic-gate 		ret = 1;
9437c478bd9Sstevel@tonic-gate 	else
9447c478bd9Sstevel@tonic-gate 		ret = 0;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	return (ret);
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate /*
9507c478bd9Sstevel@tonic-gate  * int stop_instance()
9517c478bd9Sstevel@tonic-gate  *
9527c478bd9Sstevel@tonic-gate  *   Stop the instance identified by the instance given as the second argument,
9537c478bd9Sstevel@tonic-gate  *   for the cause stated.
9547c478bd9Sstevel@tonic-gate  *
9557c478bd9Sstevel@tonic-gate  *   Returns
9567c478bd9Sstevel@tonic-gate  *     0 - success
9577c478bd9Sstevel@tonic-gate  *     -1 - inst is in transition
9587c478bd9Sstevel@tonic-gate  */
9597c478bd9Sstevel@tonic-gate static int
9607c478bd9Sstevel@tonic-gate stop_instance(scf_handle_t *local_handle, restarter_inst_t *inst,
9617c478bd9Sstevel@tonic-gate     stop_cause_t cause)
9627c478bd9Sstevel@tonic-gate {
9637c478bd9Sstevel@tonic-gate 	fork_info_t *info;
9647c478bd9Sstevel@tonic-gate 	const char *cp;
9657c478bd9Sstevel@tonic-gate 	int err;
9667c478bd9Sstevel@tonic-gate 	restarter_error_t re;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&inst->ri_lock));
9697c478bd9Sstevel@tonic-gate 	assert(inst->ri_method_thread == 0);
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	switch (cause) {
9727c478bd9Sstevel@tonic-gate 	case RSTOP_EXIT:
9737c478bd9Sstevel@tonic-gate 		re = RERR_RESTART;
9747c478bd9Sstevel@tonic-gate 		cp = "all processes in service exited";
9757c478bd9Sstevel@tonic-gate 		break;
9767c478bd9Sstevel@tonic-gate 	case RSTOP_CORE:
9777c478bd9Sstevel@tonic-gate 		re = RERR_FAULT;
9787c478bd9Sstevel@tonic-gate 		cp = "process dumped core";
9797c478bd9Sstevel@tonic-gate 		break;
9807c478bd9Sstevel@tonic-gate 	case RSTOP_SIGNAL:
9817c478bd9Sstevel@tonic-gate 		re = RERR_FAULT;
9827c478bd9Sstevel@tonic-gate 		cp = "process received fatal signal from outside the service";
9837c478bd9Sstevel@tonic-gate 		break;
9847c478bd9Sstevel@tonic-gate 	case RSTOP_HWERR:
9857c478bd9Sstevel@tonic-gate 		re = RERR_FAULT;
9867c478bd9Sstevel@tonic-gate 		cp = "process killed due to uncorrectable hardware error";
9877c478bd9Sstevel@tonic-gate 		break;
9887c478bd9Sstevel@tonic-gate 	case RSTOP_DEPENDENCY:
9897c478bd9Sstevel@tonic-gate 		re = RERR_RESTART;
9907c478bd9Sstevel@tonic-gate 		cp = "dependency activity requires stop";
9917c478bd9Sstevel@tonic-gate 		break;
9927c478bd9Sstevel@tonic-gate 	case RSTOP_DISABLE:
9937c478bd9Sstevel@tonic-gate 		re = RERR_RESTART;
9947c478bd9Sstevel@tonic-gate 		cp = "service disabled";
9957c478bd9Sstevel@tonic-gate 		break;
9967c478bd9Sstevel@tonic-gate 	case RSTOP_RESTART:
9977c478bd9Sstevel@tonic-gate 		re = RERR_RESTART;
9987c478bd9Sstevel@tonic-gate 		cp = "service restarting";
9997c478bd9Sstevel@tonic-gate 		break;
10007c478bd9Sstevel@tonic-gate 	default:
10017c478bd9Sstevel@tonic-gate #ifndef NDEBUG
10027c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Unknown cause %d at %s:%d.\n",
10037c478bd9Sstevel@tonic-gate 		    cause, __FILE__, __LINE__);
10047c478bd9Sstevel@tonic-gate #endif
10057c478bd9Sstevel@tonic-gate 		abort();
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	/* Services in the disabled and maintenance state are ignored */
10097c478bd9Sstevel@tonic-gate 	if (inst->ri_i.i_state == RESTARTER_STATE_MAINT ||
10107c478bd9Sstevel@tonic-gate 	    inst->ri_i.i_state == RESTARTER_STATE_DISABLED) {
10117c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
10127c478bd9Sstevel@tonic-gate 		    "%s: stop_instance -> is maint/disabled\n",
10137c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_fmri);
10147c478bd9Sstevel@tonic-gate 		return (0);
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	/* Already stopped instances are left alone */
10187c478bd9Sstevel@tonic-gate 	if (instance_started(inst) == 0) {
10197c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "Restarter: %s is already stopped.\n",
10207c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_fmri);
10217c478bd9Sstevel@tonic-gate 		return (0);
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	if (instance_in_transition(inst)) {
10257c478bd9Sstevel@tonic-gate 		/* requeue event by returning -1 */
10267c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
10277c478bd9Sstevel@tonic-gate 		    "Restarter: Not stopping %s, in transition.\n",
10287c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_fmri);
10297c478bd9Sstevel@tonic-gate 		return (-1);
10307c478bd9Sstevel@tonic-gate 	}
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	log_instance(inst, B_TRUE, "Stopping because %s.", cp);
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	log_framework(re == RERR_FAULT ? LOG_INFO : LOG_DEBUG,
10357c478bd9Sstevel@tonic-gate 	    "%s: Instance stopping because %s.\n", inst->ri_i.i_fmri, cp);
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	if (instance_is_wait_style(inst) && cause == RSTOP_EXIT) {
10387c478bd9Sstevel@tonic-gate 		/*
10397c478bd9Sstevel@tonic-gate 		 * No need to stop instance, as child has exited; remove
10407c478bd9Sstevel@tonic-gate 		 * contract and move the instance to the offline state.
10417c478bd9Sstevel@tonic-gate 		 */
10427c478bd9Sstevel@tonic-gate 		switch (err = restarter_instance_update_states(local_handle,
10437c478bd9Sstevel@tonic-gate 		    inst, inst->ri_i.i_state, RESTARTER_STATE_OFFLINE, re,
10447c478bd9Sstevel@tonic-gate 		    NULL)) {
10457c478bd9Sstevel@tonic-gate 		case 0:
10467c478bd9Sstevel@tonic-gate 		case ECONNRESET:
10477c478bd9Sstevel@tonic-gate 			break;
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 		default:
10507c478bd9Sstevel@tonic-gate 			bad_error("restarter_instance_update_states", err);
10517c478bd9Sstevel@tonic-gate 		}
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 		(void) update_fault_count(inst, FAULT_COUNT_RESET);
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 		if (inst->ri_i.i_primary_ctid != 0) {
10567c478bd9Sstevel@tonic-gate 			inst->ri_m_inst =
10577c478bd9Sstevel@tonic-gate 			    safe_scf_instance_create(local_handle);
10587c478bd9Sstevel@tonic-gate 			inst->ri_mi_deleted = B_FALSE;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 			libscf_reget_instance(inst);
10617c478bd9Sstevel@tonic-gate 			method_remove_contract(inst, B_TRUE, B_TRUE);
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 			scf_instance_destroy(inst->ri_m_inst);
10647c478bd9Sstevel@tonic-gate 			inst->ri_m_inst = NULL;
10657c478bd9Sstevel@tonic-gate 		}
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 		switch (err = restarter_instance_update_states(local_handle,
10687c478bd9Sstevel@tonic-gate 		    inst, inst->ri_i.i_next_state, RESTARTER_STATE_NONE, re,
10697c478bd9Sstevel@tonic-gate 		    NULL)) {
10707c478bd9Sstevel@tonic-gate 		case 0:
10717c478bd9Sstevel@tonic-gate 		case ECONNRESET:
10727c478bd9Sstevel@tonic-gate 			break;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 		default:
10757c478bd9Sstevel@tonic-gate 			bad_error("restarter_instance_update_states", err);
10767c478bd9Sstevel@tonic-gate 		}
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 		return (0);
10797c478bd9Sstevel@tonic-gate 	}
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	switch (err = restarter_instance_update_states(local_handle, inst,
10827c478bd9Sstevel@tonic-gate 	    inst->ri_i.i_state, inst->ri_i.i_enabled ? RESTARTER_STATE_OFFLINE :
10837c478bd9Sstevel@tonic-gate 	    RESTARTER_STATE_DISABLED, RERR_NONE, NULL)) {
10847c478bd9Sstevel@tonic-gate 	case 0:
10857c478bd9Sstevel@tonic-gate 	case ECONNRESET:
10867c478bd9Sstevel@tonic-gate 		break;
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 	default:
10897c478bd9Sstevel@tonic-gate 		bad_error("restarter_instance_update_states", err);
10907c478bd9Sstevel@tonic-gate 	}
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	info = startd_zalloc(sizeof (fork_info_t));
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	info->sf_id = inst->ri_id;
10957c478bd9Sstevel@tonic-gate 	info->sf_method_type = METHOD_STOP;
10967c478bd9Sstevel@tonic-gate 	info->sf_event_type = re;
10977c478bd9Sstevel@tonic-gate 	inst->ri_method_thread = startd_thread_create(method_thread, info);
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	return (0);
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate /*
11037c478bd9Sstevel@tonic-gate  * Returns
11047c478bd9Sstevel@tonic-gate  *   ENOENT - fmri is not in instance_list
11057c478bd9Sstevel@tonic-gate  *   0 - success
11067c478bd9Sstevel@tonic-gate  *   ECONNRESET - success, though handle was rebound
11077c478bd9Sstevel@tonic-gate  *   -1 - instance is in transition
11087c478bd9Sstevel@tonic-gate  */
11097c478bd9Sstevel@tonic-gate int
11107c478bd9Sstevel@tonic-gate stop_instance_fmri(scf_handle_t *h, const char *fmri, uint_t flags)
11117c478bd9Sstevel@tonic-gate {
11127c478bd9Sstevel@tonic-gate 	restarter_inst_t *rip;
11137c478bd9Sstevel@tonic-gate 	int r;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	rip = inst_lookup_by_name(fmri);
11167c478bd9Sstevel@tonic-gate 	if (rip == NULL)
11177c478bd9Sstevel@tonic-gate 		return (ENOENT);
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	r = stop_instance(h, rip, flags);
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&rip->ri_lock);
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	return (r);
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate static void
11277c478bd9Sstevel@tonic-gate unmaintain_instance(scf_handle_t *h, restarter_inst_t *rip,
11287c478bd9Sstevel@tonic-gate     unmaint_cause_t cause)
11297c478bd9Sstevel@tonic-gate {
11307c478bd9Sstevel@tonic-gate 	ctid_t ctid;
11317c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
11327c478bd9Sstevel@tonic-gate 	int r;
11337c478bd9Sstevel@tonic-gate 	uint_t tries = 0, msecs = ALLOC_DELAY;
11347c478bd9Sstevel@tonic-gate 	const char *cp;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&rip->ri_lock));
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	if (rip->ri_i.i_state != RESTARTER_STATE_MAINT) {
11397c478bd9Sstevel@tonic-gate 		log_error(LOG_DEBUG, "Restarter: "
11407c478bd9Sstevel@tonic-gate 		    "Ignoring maintenance off command because %s is not in the "
11417c478bd9Sstevel@tonic-gate 		    "maintenance state.\n", rip->ri_i.i_fmri);
11427c478bd9Sstevel@tonic-gate 		return;
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	switch (cause) {
11467c478bd9Sstevel@tonic-gate 	case RUNMAINT_CLEAR:
11477c478bd9Sstevel@tonic-gate 		cp = "clear requested";
11487c478bd9Sstevel@tonic-gate 		break;
11497c478bd9Sstevel@tonic-gate 	case RUNMAINT_DISABLE:
11507c478bd9Sstevel@tonic-gate 		cp = "disable requested";
11517c478bd9Sstevel@tonic-gate 		break;
11527c478bd9Sstevel@tonic-gate 	default:
11537c478bd9Sstevel@tonic-gate #ifndef NDEBUG
11547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Uncaught case for %d at %s:%d.\n",
11557c478bd9Sstevel@tonic-gate 		    cause, __FILE__, __LINE__);
11567c478bd9Sstevel@tonic-gate #endif
11577c478bd9Sstevel@tonic-gate 		abort();
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	log_instance(rip, B_TRUE, "Leaving maintenance because %s.",
11617c478bd9Sstevel@tonic-gate 	    cp);
11627c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "%s: Instance leaving maintenance because "
11637c478bd9Sstevel@tonic-gate 	    "%s.\n", rip->ri_i.i_fmri, cp);
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	(void) restarter_instance_update_states(h, rip, RESTARTER_STATE_UNINIT,
11667c478bd9Sstevel@tonic-gate 	    RESTARTER_STATE_NONE, RERR_RESTART, NULL);
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	/*
11697c478bd9Sstevel@tonic-gate 	 * If we did ADMIN_MAINT_ON_IMMEDIATE, then there might still be
11707c478bd9Sstevel@tonic-gate 	 * a primary contract.
11717c478bd9Sstevel@tonic-gate 	 */
11727c478bd9Sstevel@tonic-gate 	if (rip->ri_i.i_primary_ctid == 0)
11737c478bd9Sstevel@tonic-gate 		return;
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	ctid = rip->ri_i.i_primary_ctid;
11767c478bd9Sstevel@tonic-gate 	contract_abandon(ctid);
11777c478bd9Sstevel@tonic-gate 	rip->ri_i.i_primary_ctid = 0;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate rep_retry:
11807c478bd9Sstevel@tonic-gate 	switch (r = libscf_fmri_get_instance(h, rip->ri_i.i_fmri, &inst)) {
11817c478bd9Sstevel@tonic-gate 	case 0:
11827c478bd9Sstevel@tonic-gate 		break;
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
11857c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
11867c478bd9Sstevel@tonic-gate 		goto rep_retry;
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	case ENOENT:
11897c478bd9Sstevel@tonic-gate 		/* Must have been deleted. */
11907c478bd9Sstevel@tonic-gate 		return;
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	case EINVAL:
11937c478bd9Sstevel@tonic-gate 	case ENOTSUP:
11947c478bd9Sstevel@tonic-gate 	default:
11957c478bd9Sstevel@tonic-gate 		bad_error("libscf_handle_rebind", r);
11967c478bd9Sstevel@tonic-gate 	}
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate again:
11997c478bd9Sstevel@tonic-gate 	r = restarter_remove_contract(inst, ctid, RESTARTER_CONTRACT_PRIMARY);
12007c478bd9Sstevel@tonic-gate 	switch (r) {
12017c478bd9Sstevel@tonic-gate 	case 0:
12027c478bd9Sstevel@tonic-gate 		break;
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	case ENOMEM:
12057c478bd9Sstevel@tonic-gate 		++tries;
12067c478bd9Sstevel@tonic-gate 		if (tries < ALLOC_RETRY) {
12077c478bd9Sstevel@tonic-gate 			(void) poll(NULL, 0, msecs);
12087c478bd9Sstevel@tonic-gate 			msecs *= ALLOC_DELAY_MULT;
12097c478bd9Sstevel@tonic-gate 			goto again;
12107c478bd9Sstevel@tonic-gate 		}
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 		uu_die("Insufficient memory.\n");
12137c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
12167c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
12177c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
12187c478bd9Sstevel@tonic-gate 		goto rep_retry;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	case ECANCELED:
12217c478bd9Sstevel@tonic-gate 		break;
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	case EPERM:
12247c478bd9Sstevel@tonic-gate 	case EACCES:
12257c478bd9Sstevel@tonic-gate 	case EROFS:
12267c478bd9Sstevel@tonic-gate 		log_error(LOG_INFO,
12277c478bd9Sstevel@tonic-gate 		    "Could not remove contract id %lu for %s (%s).\n", ctid,
12287c478bd9Sstevel@tonic-gate 		    rip->ri_i.i_fmri, strerror(r));
12297c478bd9Sstevel@tonic-gate 		break;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	case EINVAL:
12327c478bd9Sstevel@tonic-gate 	case EBADF:
12337c478bd9Sstevel@tonic-gate 	default:
12347c478bd9Sstevel@tonic-gate 		bad_error("restarter_remove_contract", r);
12357c478bd9Sstevel@tonic-gate 	}
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
12387c478bd9Sstevel@tonic-gate }
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate /*
12417c478bd9Sstevel@tonic-gate  * enable_inst()
12427c478bd9Sstevel@tonic-gate  *   Set inst->ri_i.i_enabled.  Expects 'e' to be _ENABLE, _DISABLE, or
12437c478bd9Sstevel@tonic-gate  *   _ADMIN_DISABLE.  If the event is _ENABLE and inst is uninitialized or
12447c478bd9Sstevel@tonic-gate  *   disabled, move it to offline.  If the event is _DISABLE or
12457c478bd9Sstevel@tonic-gate  *   _ADMIN_DISABLE, make sure inst will move to disabled.
12467c478bd9Sstevel@tonic-gate  *
12477c478bd9Sstevel@tonic-gate  *   Returns
12487c478bd9Sstevel@tonic-gate  *     0 - success
12497c478bd9Sstevel@tonic-gate  *     ECONNRESET - h was rebound
12507c478bd9Sstevel@tonic-gate  */
12517c478bd9Sstevel@tonic-gate static int
12527c478bd9Sstevel@tonic-gate enable_inst(scf_handle_t *h, restarter_inst_t *inst, restarter_event_type_t e)
12537c478bd9Sstevel@tonic-gate {
12547c478bd9Sstevel@tonic-gate 	restarter_instance_state_t state;
12557c478bd9Sstevel@tonic-gate 	int r;
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&inst->ri_lock));
12587c478bd9Sstevel@tonic-gate 	assert(e == RESTARTER_EVENT_TYPE_ADMIN_DISABLE ||
12597c478bd9Sstevel@tonic-gate 	    e == RESTARTER_EVENT_TYPE_DISABLE ||
12607c478bd9Sstevel@tonic-gate 	    e == RESTARTER_EVENT_TYPE_ENABLE);
12617c478bd9Sstevel@tonic-gate 	assert(instance_in_transition(inst) == 0);
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	state = inst->ri_i.i_state;
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	if (e == RESTARTER_EVENT_TYPE_ENABLE) {
12667c478bd9Sstevel@tonic-gate 		inst->ri_i.i_enabled = 1;
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 		if (state == RESTARTER_STATE_UNINIT ||
12697c478bd9Sstevel@tonic-gate 		    state == RESTARTER_STATE_DISABLED) {
12707c478bd9Sstevel@tonic-gate 			/*
12717c478bd9Sstevel@tonic-gate 			 * B_FALSE: Don't log an error if the log_instance()
12727c478bd9Sstevel@tonic-gate 			 * fails because it will fail on the miniroot before
12737c478bd9Sstevel@tonic-gate 			 * install-discovery runs.
12747c478bd9Sstevel@tonic-gate 			 */
12757c478bd9Sstevel@tonic-gate 			log_instance(inst, B_FALSE, "Enabled.");
12767c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG, "%s: Instance enabled.\n",
12777c478bd9Sstevel@tonic-gate 			    inst->ri_i.i_fmri);
12787c478bd9Sstevel@tonic-gate 			(void) restarter_instance_update_states(h, inst,
12797c478bd9Sstevel@tonic-gate 			    RESTARTER_STATE_OFFLINE, RESTARTER_STATE_NONE,
12807c478bd9Sstevel@tonic-gate 			    RERR_NONE, NULL);
12817c478bd9Sstevel@tonic-gate 		} else {
12827c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG, "Restarter: "
12837c478bd9Sstevel@tonic-gate 			    "Not changing state of %s for enable command.\n",
12847c478bd9Sstevel@tonic-gate 			    inst->ri_i.i_fmri);
12857c478bd9Sstevel@tonic-gate 		}
12867c478bd9Sstevel@tonic-gate 	} else {
12877c478bd9Sstevel@tonic-gate 		inst->ri_i.i_enabled = 0;
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 		switch (state) {
12907c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_ONLINE:
12917c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_DEGRADED:
12927c478bd9Sstevel@tonic-gate 			r = stop_instance(h, inst, RSTOP_DISABLE);
12937c478bd9Sstevel@tonic-gate 			return (r == ECONNRESET ? 0 : r);
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_OFFLINE:
12967c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_UNINIT:
12977c478bd9Sstevel@tonic-gate 			if (inst->ri_i.i_primary_ctid != 0) {
12987c478bd9Sstevel@tonic-gate 				inst->ri_m_inst = safe_scf_instance_create(h);
12997c478bd9Sstevel@tonic-gate 				inst->ri_mi_deleted = B_FALSE;
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 				libscf_reget_instance(inst);
13027c478bd9Sstevel@tonic-gate 				method_remove_contract(inst, B_TRUE, B_TRUE);
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 				scf_instance_destroy(inst->ri_m_inst);
13057c478bd9Sstevel@tonic-gate 			}
13067c478bd9Sstevel@tonic-gate 			/* B_FALSE: See log_instance(..., "Enabled."); above */
13077c478bd9Sstevel@tonic-gate 			log_instance(inst, B_FALSE, "Disabled.");
13087c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG, "%s: Instance disabled.\n",
13097c478bd9Sstevel@tonic-gate 			    inst->ri_i.i_fmri);
13107c478bd9Sstevel@tonic-gate 			(void) restarter_instance_update_states(h, inst,
13117c478bd9Sstevel@tonic-gate 			    RESTARTER_STATE_DISABLED, RESTARTER_STATE_NONE,
13127c478bd9Sstevel@tonic-gate 			    RERR_RESTART, NULL);
13137c478bd9Sstevel@tonic-gate 			return (0);
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_DISABLED:
13167c478bd9Sstevel@tonic-gate 			break;
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 		case RESTARTER_STATE_MAINT:
13197c478bd9Sstevel@tonic-gate 			/*
13207c478bd9Sstevel@tonic-gate 			 * We only want to pull the instance out of maintenance
13217c478bd9Sstevel@tonic-gate 			 * if the disable is on adminstrative request.  The
13227c478bd9Sstevel@tonic-gate 			 * graph engine sends _DISABLE events whenever a
13237c478bd9Sstevel@tonic-gate 			 * service isn't in the disabled state, and we don't
13247c478bd9Sstevel@tonic-gate 			 * want to pull the service out of maintenance if,
13257c478bd9Sstevel@tonic-gate 			 * for example, it is there due to a dependency cycle.
13267c478bd9Sstevel@tonic-gate 			 */
13277c478bd9Sstevel@tonic-gate 			if (e == RESTARTER_EVENT_TYPE_ADMIN_DISABLE)
13287c478bd9Sstevel@tonic-gate 				unmaintain_instance(h, inst, RUNMAINT_DISABLE);
13297c478bd9Sstevel@tonic-gate 			break;
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 		default:
13327c478bd9Sstevel@tonic-gate #ifndef NDEBUG
13337c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Restarter instance %s has "
13347c478bd9Sstevel@tonic-gate 			    "unknown state %d.\n", inst->ri_i.i_fmri, state);
13357c478bd9Sstevel@tonic-gate #endif
13367c478bd9Sstevel@tonic-gate 			abort();
13377c478bd9Sstevel@tonic-gate 		}
13387c478bd9Sstevel@tonic-gate 	}
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	return (0);
13417c478bd9Sstevel@tonic-gate }
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate static void
13447c478bd9Sstevel@tonic-gate start_instance(scf_handle_t *local_handle, restarter_inst_t *inst)
13457c478bd9Sstevel@tonic-gate {
13467c478bd9Sstevel@tonic-gate 	fork_info_t *info;
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&inst->ri_lock));
13497c478bd9Sstevel@tonic-gate 	assert(instance_in_transition(inst) == 0);
13507c478bd9Sstevel@tonic-gate 	assert(inst->ri_method_thread == 0);
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "%s: trying to start instance\n",
13537c478bd9Sstevel@tonic-gate 	    inst->ri_i.i_fmri);
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	/* Services in the disabled and maintenance state are ignored */
13567c478bd9Sstevel@tonic-gate 	if (inst->ri_i.i_state == RESTARTER_STATE_MAINT ||
13577c478bd9Sstevel@tonic-gate 	    inst->ri_i.i_state == RESTARTER_STATE_DISABLED ||
13587c478bd9Sstevel@tonic-gate 	    inst->ri_i.i_enabled == 0) {
13597c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
13607c478bd9Sstevel@tonic-gate 		    "%s: start_instance -> is maint/disabled\n",
13617c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_fmri);
13627c478bd9Sstevel@tonic-gate 		return;
13637c478bd9Sstevel@tonic-gate 	}
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	/* Already started instances are left alone */
13667c478bd9Sstevel@tonic-gate 	if (instance_started(inst) == 1) {
13677c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
13687c478bd9Sstevel@tonic-gate 		    "%s: start_instance -> is already started\n",
13697c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_fmri);
13707c478bd9Sstevel@tonic-gate 		return;
13717c478bd9Sstevel@tonic-gate 	}
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "%s: starting instance.\n", inst->ri_i.i_fmri);
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 	(void) restarter_instance_update_states(local_handle, inst,
13767c478bd9Sstevel@tonic-gate 	    inst->ri_i.i_state, RESTARTER_STATE_ONLINE, RERR_NONE, NULL);
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	info = startd_zalloc(sizeof (fork_info_t));
13797c478bd9Sstevel@tonic-gate 
13807c478bd9Sstevel@tonic-gate 	info->sf_id = inst->ri_id;
13817c478bd9Sstevel@tonic-gate 	info->sf_method_type = METHOD_START;
13827c478bd9Sstevel@tonic-gate 	info->sf_event_type = RERR_NONE;
13837c478bd9Sstevel@tonic-gate 	inst->ri_method_thread = startd_thread_create(method_thread, info);
13847c478bd9Sstevel@tonic-gate }
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate static void
13877c478bd9Sstevel@tonic-gate maintain_instance(scf_handle_t *h, restarter_inst_t *rip, int immediate,
13887c478bd9Sstevel@tonic-gate     const char *aux)
13897c478bd9Sstevel@tonic-gate {
13907c478bd9Sstevel@tonic-gate 	fork_info_t *info;
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&rip->ri_lock));
13937c478bd9Sstevel@tonic-gate 	assert(aux != NULL);
13947c478bd9Sstevel@tonic-gate 	assert(rip->ri_method_thread == 0);
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	log_instance(rip, B_TRUE, "Stopping for maintenance due to %s.", aux);
13977c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "%s: stopping for maintenance due to %s.\n",
13987c478bd9Sstevel@tonic-gate 	    rip->ri_i.i_fmri, aux);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	/* Services in the maintenance state are ignored */
14017c478bd9Sstevel@tonic-gate 	if (rip->ri_i.i_state == RESTARTER_STATE_MAINT) {
14027c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
14037c478bd9Sstevel@tonic-gate 		    "%s: maintain_instance -> is already in maintenance\n",
14047c478bd9Sstevel@tonic-gate 		    rip->ri_i.i_fmri);
14057c478bd9Sstevel@tonic-gate 		return;
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	if (immediate || !instance_started(rip)) {
14097c478bd9Sstevel@tonic-gate 		if (rip->ri_i.i_primary_ctid != 0) {
14107c478bd9Sstevel@tonic-gate 			rip->ri_m_inst = safe_scf_instance_create(h);
14117c478bd9Sstevel@tonic-gate 			rip->ri_mi_deleted = B_FALSE;
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 			libscf_reget_instance(rip);
14147c478bd9Sstevel@tonic-gate 			method_remove_contract(rip, B_TRUE, B_TRUE);
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 			scf_instance_destroy(rip->ri_m_inst);
14177c478bd9Sstevel@tonic-gate 		}
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 		(void) restarter_instance_update_states(h, rip,
14207c478bd9Sstevel@tonic-gate 		    RESTARTER_STATE_MAINT, RESTARTER_STATE_NONE, RERR_RESTART,
14217c478bd9Sstevel@tonic-gate 		    (char *)aux);
14227c478bd9Sstevel@tonic-gate 		return;
14237c478bd9Sstevel@tonic-gate 	}
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 	(void) restarter_instance_update_states(h, rip, rip->ri_i.i_state,
14267c478bd9Sstevel@tonic-gate 	    RESTARTER_STATE_MAINT, RERR_NONE, (char *)aux);
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	info = startd_zalloc(sizeof (*info));
14297c478bd9Sstevel@tonic-gate 	info->sf_id = rip->ri_id;
14307c478bd9Sstevel@tonic-gate 	info->sf_method_type = METHOD_STOP;
14317c478bd9Sstevel@tonic-gate 	info->sf_event_type = RERR_RESTART;
14327c478bd9Sstevel@tonic-gate 	rip->ri_method_thread = startd_thread_create(method_thread, info);
14337c478bd9Sstevel@tonic-gate }
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate static void
14367c478bd9Sstevel@tonic-gate refresh_instance(scf_handle_t *h, restarter_inst_t *rip)
14377c478bd9Sstevel@tonic-gate {
14387c478bd9Sstevel@tonic-gate 	scf_instance_t *inst;
14397c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
14407c478bd9Sstevel@tonic-gate 	fork_info_t *info;
14417c478bd9Sstevel@tonic-gate 	int r;
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&rip->ri_lock));
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	log_instance(rip, B_TRUE, "Rereading configuration.");
14467c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "%s: rereading configuration.\n",
14477c478bd9Sstevel@tonic-gate 	    rip->ri_i.i_fmri);
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate rep_retry:
14507c478bd9Sstevel@tonic-gate 	r = libscf_fmri_get_instance(h, rip->ri_i.i_fmri, &inst);
14517c478bd9Sstevel@tonic-gate 	switch (r) {
14527c478bd9Sstevel@tonic-gate 	case 0:
14537c478bd9Sstevel@tonic-gate 		break;
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
14567c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
14577c478bd9Sstevel@tonic-gate 		goto rep_retry;
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	case ENOENT:
14607c478bd9Sstevel@tonic-gate 		/* Must have been deleted. */
14617c478bd9Sstevel@tonic-gate 		return;
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	case EINVAL:
14647c478bd9Sstevel@tonic-gate 	case ENOTSUP:
14657c478bd9Sstevel@tonic-gate 	default:
14667c478bd9Sstevel@tonic-gate 		bad_error("libscf_fmri_get_instance", r);
14677c478bd9Sstevel@tonic-gate 	}
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 	snap = libscf_get_running_snapshot(inst);
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	r = libscf_get_startd_properties(inst, snap, &rip->ri_flags,
14727c478bd9Sstevel@tonic-gate 	    &rip->ri_utmpx_prefix);
14737c478bd9Sstevel@tonic-gate 	switch (r) {
14747c478bd9Sstevel@tonic-gate 	case 0:
14757c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "%s is a %s-style service\n",
14767c478bd9Sstevel@tonic-gate 		    rip->ri_i.i_fmri, service_style(rip->ri_flags));
14777c478bd9Sstevel@tonic-gate 		break;
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
14807c478bd9Sstevel@tonic-gate 		scf_instance_destroy(inst);
14817c478bd9Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
14827c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(h);
14837c478bd9Sstevel@tonic-gate 		goto rep_retry;
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	case ECANCELED:
14867c478bd9Sstevel@tonic-gate 	case ENOENT:
14877c478bd9Sstevel@tonic-gate 		/* Succeed in anticipation of REMOVE_INSTANCE. */
14887c478bd9Sstevel@tonic-gate 		break;
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	default:
14917c478bd9Sstevel@tonic-gate 		bad_error("libscf_get_startd_properties", r);
14927c478bd9Sstevel@tonic-gate 	}
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	if (instance_started(rip)) {
14957c478bd9Sstevel@tonic-gate 		/* Refresh does not change the state. */
14967c478bd9Sstevel@tonic-gate 		(void) restarter_instance_update_states(h, rip,
14977c478bd9Sstevel@tonic-gate 		    rip->ri_i.i_state, rip->ri_i.i_state, RERR_NONE, NULL);
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 		info = startd_zalloc(sizeof (*info));
15007c478bd9Sstevel@tonic-gate 		info->sf_id = rip->ri_id;
15017c478bd9Sstevel@tonic-gate 		info->sf_method_type = METHOD_REFRESH;
15027c478bd9Sstevel@tonic-gate 		info->sf_event_type = RERR_REFRESH;
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 		assert(rip->ri_method_thread == 0);
15057c478bd9Sstevel@tonic-gate 		rip->ri_method_thread =
15067c478bd9Sstevel@tonic-gate 		    startd_thread_create(method_thread, info);
15077c478bd9Sstevel@tonic-gate 	}
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
15107c478bd9Sstevel@tonic-gate 	scf_instance_destroy(inst);
15117c478bd9Sstevel@tonic-gate }
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate const char *event_names[] = { "INVALID", "ADD_INSTANCE", "REMOVE_INSTANCE",
15147c478bd9Sstevel@tonic-gate 	"ENABLE", "DISABLE", "ADMIN_DEGRADED", "ADMIN_REFRESH",
15157c478bd9Sstevel@tonic-gate 	"ADMIN_RESTART", "ADMIN_MAINT_OFF", "ADMIN_MAINT_ON",
15167c478bd9Sstevel@tonic-gate 	"ADMIN_MAINT_ON_IMMEDIATE", "STOP", "START", "DEPENDENCY_CYCLE",
15177c478bd9Sstevel@tonic-gate 	"INVALID_DEPENDENCY", "ADMIN_DISABLE"
15187c478bd9Sstevel@tonic-gate };
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate /*
15217c478bd9Sstevel@tonic-gate  * void *restarter_process_events()
15227c478bd9Sstevel@tonic-gate  *
15237c478bd9Sstevel@tonic-gate  *   Called in a separate thread to process the events on an instance's
15247c478bd9Sstevel@tonic-gate  *   queue.  Empties the queue completely, and tries to keep the thread
15257c478bd9Sstevel@tonic-gate  *   around for a little while after the queue is empty to save on
15267c478bd9Sstevel@tonic-gate  *   startup costs.
15277c478bd9Sstevel@tonic-gate  */
15287c478bd9Sstevel@tonic-gate static void *
15297c478bd9Sstevel@tonic-gate restarter_process_events(void *arg)
15307c478bd9Sstevel@tonic-gate {
15317c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
15327c478bd9Sstevel@tonic-gate 	restarter_instance_qentry_t *event;
15337c478bd9Sstevel@tonic-gate 	restarter_inst_t *rip;
15347c478bd9Sstevel@tonic-gate 	char *fmri = (char *)arg;
15357c478bd9Sstevel@tonic-gate 	struct timespec to;
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	assert(fmri != NULL);
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	/* grab the queue lock */
15427c478bd9Sstevel@tonic-gate 	rip = inst_lookup_queue(fmri);
15437c478bd9Sstevel@tonic-gate 	if (rip == NULL)
15447c478bd9Sstevel@tonic-gate 		goto out;
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate again:
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	while ((event = uu_list_first(rip->ri_queue)) != NULL) {
15497c478bd9Sstevel@tonic-gate 		restarter_inst_t *inst;
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 		/* drop the queue lock */
15527c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&rip->ri_queue_lock);
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 		/*
15557c478bd9Sstevel@tonic-gate 		 * Grab the inst lock -- this waits until any outstanding
15567c478bd9Sstevel@tonic-gate 		 * method finishes running.
15577c478bd9Sstevel@tonic-gate 		 */
15587c478bd9Sstevel@tonic-gate 		inst = inst_lookup_by_name(fmri);
15597c478bd9Sstevel@tonic-gate 		if (inst == NULL) {
15607c478bd9Sstevel@tonic-gate 			/* Getting deleted in the middle isn't an error. */
15617c478bd9Sstevel@tonic-gate 			goto cont;
15627c478bd9Sstevel@tonic-gate 		}
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 		assert(instance_in_transition(inst) == 0);
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 		/* process the event */
15677c478bd9Sstevel@tonic-gate 		switch (event->riq_type) {
15687c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ENABLE:
15697c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DISABLE:
15707c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_DISABLE:
15717c478bd9Sstevel@tonic-gate 			(void) enable_inst(h, inst, event->riq_type);
15727c478bd9Sstevel@tonic-gate 			break;
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_REMOVE_INSTANCE:
15757c478bd9Sstevel@tonic-gate 			restarter_delete_inst(inst);
15767c478bd9Sstevel@tonic-gate 			inst = NULL;
15777c478bd9Sstevel@tonic-gate 			goto cont;
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_STOP:
15807c478bd9Sstevel@tonic-gate 			(void) stop_instance(h, inst, RSTOP_DEPENDENCY);
15817c478bd9Sstevel@tonic-gate 			break;
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_START:
15847c478bd9Sstevel@tonic-gate 			start_instance(h, inst);
15857c478bd9Sstevel@tonic-gate 			break;
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_DEPENDENCY_CYCLE:
15887c478bd9Sstevel@tonic-gate 			maintain_instance(h, inst, 0, "dependency_cycle");
15897c478bd9Sstevel@tonic-gate 			break;
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_INVALID_DEPENDENCY:
15927c478bd9Sstevel@tonic-gate 			maintain_instance(h, inst, 0, "invalid_dependency");
15937c478bd9Sstevel@tonic-gate 			break;
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
15967c478bd9Sstevel@tonic-gate 			maintain_instance(h, inst, 0, "administrative_request");
15977c478bd9Sstevel@tonic-gate 			break;
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE:
16007c478bd9Sstevel@tonic-gate 			maintain_instance(h, inst, 1, "administrative_request");
16017c478bd9Sstevel@tonic-gate 			break;
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF:
16047c478bd9Sstevel@tonic-gate 			unmaintain_instance(h, inst, RUNMAINT_CLEAR);
16057c478bd9Sstevel@tonic-gate 			break;
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_REFRESH:
16087c478bd9Sstevel@tonic-gate 			refresh_instance(h, inst);
16097c478bd9Sstevel@tonic-gate 			break;
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_DEGRADED:
16127c478bd9Sstevel@tonic-gate 			log_framework(LOG_WARNING, "Restarter: "
16137c478bd9Sstevel@tonic-gate 			    "%s command (for %s) unimplemented.\n",
16147c478bd9Sstevel@tonic-gate 			    event_names[event->riq_type], inst->ri_i.i_fmri);
16157c478bd9Sstevel@tonic-gate 			break;
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADMIN_RESTART:
16187c478bd9Sstevel@tonic-gate 			if (!instance_started(inst)) {
16197c478bd9Sstevel@tonic-gate 				log_framework(LOG_DEBUG, "Restarter: "
16207c478bd9Sstevel@tonic-gate 				    "Not restarting %s; not running.\n",
16217c478bd9Sstevel@tonic-gate 				    inst->ri_i.i_fmri);
16227c478bd9Sstevel@tonic-gate 			} else {
16237c478bd9Sstevel@tonic-gate 				/*
16247c478bd9Sstevel@tonic-gate 				 * Stop the instance.  If it can be restarted,
16257c478bd9Sstevel@tonic-gate 				 * the graph engine will send a new event.
16267c478bd9Sstevel@tonic-gate 				 */
16277c478bd9Sstevel@tonic-gate 				(void) stop_instance(h, inst, RSTOP_RESTART);
16287c478bd9Sstevel@tonic-gate 			}
16297c478bd9Sstevel@tonic-gate 			break;
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 		case RESTARTER_EVENT_TYPE_ADD_INSTANCE:
16327c478bd9Sstevel@tonic-gate 		default:
16337c478bd9Sstevel@tonic-gate #ifndef NDEBUG
16347c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Bad restarter event %d.  "
16357c478bd9Sstevel@tonic-gate 			    "Aborting.\n", __FILE__, __LINE__, event->riq_type);
16367c478bd9Sstevel@tonic-gate #endif
16377c478bd9Sstevel@tonic-gate 			abort();
16387c478bd9Sstevel@tonic-gate 		}
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 		assert(inst != NULL);
16417c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&inst->ri_lock);
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate cont:
16447c478bd9Sstevel@tonic-gate 		/* grab the queue lock */
16457c478bd9Sstevel@tonic-gate 		rip = inst_lookup_queue(fmri);
16467c478bd9Sstevel@tonic-gate 		if (rip == NULL)
16477c478bd9Sstevel@tonic-gate 			goto out;
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 		/* delete the event */
16507c478bd9Sstevel@tonic-gate 		uu_list_remove(rip->ri_queue, event);
16517c478bd9Sstevel@tonic-gate 		startd_free(event, sizeof (restarter_instance_qentry_t));
16527c478bd9Sstevel@tonic-gate 	}
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	assert(rip != NULL);
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	/*
16577c478bd9Sstevel@tonic-gate 	 * Try to preserve the thread for a little while for future use.
16587c478bd9Sstevel@tonic-gate 	 */
16597c478bd9Sstevel@tonic-gate 	to.tv_sec = 3;
16607c478bd9Sstevel@tonic-gate 	to.tv_nsec = 0;
16617c478bd9Sstevel@tonic-gate 	(void) pthread_cond_reltimedwait_np(&rip->ri_queue_cv,
16627c478bd9Sstevel@tonic-gate 	    &rip->ri_queue_lock, &to);
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	if (uu_list_first(rip->ri_queue) != NULL)
16657c478bd9Sstevel@tonic-gate 		goto again;
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	rip->ri_queue_thread = 0;
16687c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&rip->ri_queue_lock);
16697c478bd9Sstevel@tonic-gate out:
16707c478bd9Sstevel@tonic-gate 	(void) scf_handle_unbind(h);
16717c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
16727c478bd9Sstevel@tonic-gate 	free(fmri);
16737c478bd9Sstevel@tonic-gate 	return (NULL);
16747c478bd9Sstevel@tonic-gate }
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate static int
16777c478bd9Sstevel@tonic-gate is_admin_event(restarter_event_type_t t) {
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	switch (t) {
16807c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON:
16817c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_ON_IMMEDIATE:
16827c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_MAINT_OFF:
16837c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_REFRESH:
16847c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_DEGRADED:
16857c478bd9Sstevel@tonic-gate 	case RESTARTER_EVENT_TYPE_ADMIN_RESTART:
16867c478bd9Sstevel@tonic-gate 		return (1);
16877c478bd9Sstevel@tonic-gate 	default:
16887c478bd9Sstevel@tonic-gate 		return (0);
16897c478bd9Sstevel@tonic-gate 	}
16907c478bd9Sstevel@tonic-gate }
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate static void
16937c478bd9Sstevel@tonic-gate restarter_queue_event(restarter_inst_t *ri, restarter_protocol_event_t *e)
16947c478bd9Sstevel@tonic-gate {
16957c478bd9Sstevel@tonic-gate 	restarter_instance_qentry_t *qe;
16967c478bd9Sstevel@tonic-gate 	int r;
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&ri->ri_queue_lock));
16997c478bd9Sstevel@tonic-gate 	assert(!PTHREAD_MUTEX_HELD(&ri->ri_lock));
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	qe = startd_zalloc(sizeof (restarter_instance_qentry_t));
17027c478bd9Sstevel@tonic-gate 	qe->riq_type = e->rpe_type;
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 	uu_list_node_init(qe, &qe->riq_link, restarter_queue_pool);
17057c478bd9Sstevel@tonic-gate 	r = uu_list_insert_before(ri->ri_queue, NULL, qe);
17067c478bd9Sstevel@tonic-gate 	assert(r == 0);
17077c478bd9Sstevel@tonic-gate }
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate /*
17107c478bd9Sstevel@tonic-gate  * void *restarter_event_thread()
17117c478bd9Sstevel@tonic-gate  *
17127c478bd9Sstevel@tonic-gate  *  Handle incoming graph events by placing them on a per-instance
17137c478bd9Sstevel@tonic-gate  *  queue.  We can't lock the main part of the instance structure, so
17147c478bd9Sstevel@tonic-gate  *  just modify the seprarately locked event queue portion.
17157c478bd9Sstevel@tonic-gate  */
17167c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17177c478bd9Sstevel@tonic-gate static void *
17187c478bd9Sstevel@tonic-gate restarter_event_thread(void *unused)
17197c478bd9Sstevel@tonic-gate {
17207c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	/*
17237c478bd9Sstevel@tonic-gate 	 * This is a new thread, and thus, gets its own handle
17247c478bd9Sstevel@tonic-gate 	 * to the repository.
17257c478bd9Sstevel@tonic-gate 	 */
17267c478bd9Sstevel@tonic-gate 	h = libscf_handle_create_bound_loop();
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&ru->restarter_update_lock);
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
17317c478bd9Sstevel@tonic-gate 	while (1) {
17327c478bd9Sstevel@tonic-gate 		restarter_protocol_event_t *e;
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 		while (ru->restarter_update_wakeup == 0)
17357c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&ru->restarter_update_cv,
17367c478bd9Sstevel@tonic-gate 			    &ru->restarter_update_lock);
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 		ru->restarter_update_wakeup = 0;
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 		while ((e = restarter_event_dequeue()) != NULL) {
17417c478bd9Sstevel@tonic-gate 			restarter_inst_t *rip;
17427c478bd9Sstevel@tonic-gate 			char *fmri;
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 			MUTEX_UNLOCK(&ru->restarter_update_lock);
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 			/*
17477c478bd9Sstevel@tonic-gate 			 * ADD_INSTANCE is special: there's likely no
17487c478bd9Sstevel@tonic-gate 			 * instance structure yet, so we need to handle the
17497c478bd9Sstevel@tonic-gate 			 * addition synchronously.
17507c478bd9Sstevel@tonic-gate 			 */
17517c478bd9Sstevel@tonic-gate 			switch (e->rpe_type) {
17527c478bd9Sstevel@tonic-gate 			case RESTARTER_EVENT_TYPE_ADD_INSTANCE:
17537c478bd9Sstevel@tonic-gate 				if (restarter_insert_inst(h, e->rpe_inst) != 0)
17547c478bd9Sstevel@tonic-gate 					log_error(LOG_INFO, "Restarter: "
17557c478bd9Sstevel@tonic-gate 					    "Could not add %s.\n", e->rpe_inst);
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 				MUTEX_LOCK(&st->st_load_lock);
17587c478bd9Sstevel@tonic-gate 				if (--st->st_load_instances == 0)
17597c478bd9Sstevel@tonic-gate 					(void) pthread_cond_broadcast(
17607c478bd9Sstevel@tonic-gate 					    &st->st_load_cv);
17617c478bd9Sstevel@tonic-gate 				MUTEX_UNLOCK(&st->st_load_lock);
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 				goto nolookup;
17647c478bd9Sstevel@tonic-gate 			}
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 			/*
17677c478bd9Sstevel@tonic-gate 			 * Lookup the instance, locking only the event queue.
17687c478bd9Sstevel@tonic-gate 			 * Can't grab ri_lock here because it might be held
17697c478bd9Sstevel@tonic-gate 			 * by a long-running method.
17707c478bd9Sstevel@tonic-gate 			 */
17717c478bd9Sstevel@tonic-gate 			rip = inst_lookup_queue(e->rpe_inst);
17727c478bd9Sstevel@tonic-gate 			if (rip == NULL) {
17737c478bd9Sstevel@tonic-gate 				log_error(LOG_INFO, "Restarter: "
17747c478bd9Sstevel@tonic-gate 				    "Ignoring %s command for unknown service "
17757c478bd9Sstevel@tonic-gate 				    "%s.\n", event_names[e->rpe_type],
17767c478bd9Sstevel@tonic-gate 				    e->rpe_inst);
17777c478bd9Sstevel@tonic-gate 				goto nolookup;
17787c478bd9Sstevel@tonic-gate 			}
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 			/* Keep ADMIN events from filling up the queue. */
17817c478bd9Sstevel@tonic-gate 			if (is_admin_event(e->rpe_type) &&
17827c478bd9Sstevel@tonic-gate 			    uu_list_numnodes(rip->ri_queue) >
17837c478bd9Sstevel@tonic-gate 			    RINST_QUEUE_THRESHOLD) {
17847c478bd9Sstevel@tonic-gate 				MUTEX_UNLOCK(&rip->ri_queue_lock);
17857c478bd9Sstevel@tonic-gate 				log_instance(rip, B_TRUE, "Instance event "
17867c478bd9Sstevel@tonic-gate 				    "queue overflow.  Dropping administrative "
17877c478bd9Sstevel@tonic-gate 				    "request.");
17887c478bd9Sstevel@tonic-gate 				log_framework(LOG_DEBUG, "%s: Instance event "
17897c478bd9Sstevel@tonic-gate 				    "queue overflow.  Dropping administrative "
17907c478bd9Sstevel@tonic-gate 				    "request.\n", rip->ri_i.i_fmri);
17917c478bd9Sstevel@tonic-gate 				goto nolookup;
17927c478bd9Sstevel@tonic-gate 			}
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate 			/* Now add the event to the instance queue. */
17957c478bd9Sstevel@tonic-gate 			restarter_queue_event(rip, e);
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 			if (rip->ri_queue_thread == 0) {
17987c478bd9Sstevel@tonic-gate 				/*
17997c478bd9Sstevel@tonic-gate 				 * Start a thread if one isn't already
18007c478bd9Sstevel@tonic-gate 				 * running.
18017c478bd9Sstevel@tonic-gate 				 */
18027c478bd9Sstevel@tonic-gate 				fmri = safe_strdup(e->rpe_inst);
18037c478bd9Sstevel@tonic-gate 				rip->ri_queue_thread =  startd_thread_create(
18047c478bd9Sstevel@tonic-gate 				    restarter_process_events, (void *)fmri);
18057c478bd9Sstevel@tonic-gate 			} else {
18067c478bd9Sstevel@tonic-gate 				/*
18077c478bd9Sstevel@tonic-gate 				 * Signal the existing thread that there's
18087c478bd9Sstevel@tonic-gate 				 * a new event.
18097c478bd9Sstevel@tonic-gate 				 */
18107c478bd9Sstevel@tonic-gate 				(void) pthread_cond_broadcast(
18117c478bd9Sstevel@tonic-gate 				    &rip->ri_queue_cv);
18127c478bd9Sstevel@tonic-gate 			}
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 			MUTEX_UNLOCK(&rip->ri_queue_lock);
18157c478bd9Sstevel@tonic-gate nolookup:
18167c478bd9Sstevel@tonic-gate 			restarter_event_release(e);
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 			MUTEX_LOCK(&ru->restarter_update_lock);
18197c478bd9Sstevel@tonic-gate 		}
18207c478bd9Sstevel@tonic-gate 	}
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	/*
18237c478bd9Sstevel@tonic-gate 	 * Unreachable for now -- there's currently no graceful cleanup
18247c478bd9Sstevel@tonic-gate 	 * called on exit().
18257c478bd9Sstevel@tonic-gate 	 */
18267c478bd9Sstevel@tonic-gate 	(void) scf_handle_unbind(h);
18277c478bd9Sstevel@tonic-gate 	scf_handle_destroy(h);
18287c478bd9Sstevel@tonic-gate 	return (NULL);
18297c478bd9Sstevel@tonic-gate }
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate static restarter_inst_t *
18327c478bd9Sstevel@tonic-gate contract_to_inst(ctid_t ctid)
18337c478bd9Sstevel@tonic-gate {
18347c478bd9Sstevel@tonic-gate 	restarter_inst_t *inst;
18357c478bd9Sstevel@tonic-gate 	int id;
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	id = lookup_inst_by_contract(ctid);
18387c478bd9Sstevel@tonic-gate 	if (id == -1)
18397c478bd9Sstevel@tonic-gate 		return (NULL);
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 	inst = inst_lookup_by_id(id);
18427c478bd9Sstevel@tonic-gate 	if (inst != NULL) {
18437c478bd9Sstevel@tonic-gate 		/*
18447c478bd9Sstevel@tonic-gate 		 * Since ri_lock isn't held by the contract id lookup, this
18457c478bd9Sstevel@tonic-gate 		 * instance may have been restarted and now be in a new
18467c478bd9Sstevel@tonic-gate 		 * contract, making the old contract no longer valid for this
18477c478bd9Sstevel@tonic-gate 		 * instance.
18487c478bd9Sstevel@tonic-gate 		 */
18497c478bd9Sstevel@tonic-gate 		if (ctid != inst->ri_i.i_primary_ctid) {
18507c478bd9Sstevel@tonic-gate 			MUTEX_UNLOCK(&inst->ri_lock);
18517c478bd9Sstevel@tonic-gate 			inst = NULL;
18527c478bd9Sstevel@tonic-gate 		}
18537c478bd9Sstevel@tonic-gate 	}
18547c478bd9Sstevel@tonic-gate 	return (inst);
18557c478bd9Sstevel@tonic-gate }
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate /*
18587c478bd9Sstevel@tonic-gate  * void contract_action()
18597c478bd9Sstevel@tonic-gate  *   Take action on contract events.
18607c478bd9Sstevel@tonic-gate  */
18617c478bd9Sstevel@tonic-gate static void
18627c478bd9Sstevel@tonic-gate contract_action(scf_handle_t *h, restarter_inst_t *inst, ctid_t id,
18637c478bd9Sstevel@tonic-gate     uint32_t type)
18647c478bd9Sstevel@tonic-gate {
18657c478bd9Sstevel@tonic-gate 	const char *fmri = inst->ri_i.i_fmri;
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&inst->ri_lock));
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	/*
18707c478bd9Sstevel@tonic-gate 	 * If startd has stopped this contract, there is no need to
18717c478bd9Sstevel@tonic-gate 	 * stop it again.
18727c478bd9Sstevel@tonic-gate 	 */
18737c478bd9Sstevel@tonic-gate 	if (inst->ri_i.i_primary_ctid > 0 &&
18747c478bd9Sstevel@tonic-gate 	    inst->ri_i.i_primary_ctid_stopped)
18757c478bd9Sstevel@tonic-gate 		return;
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	if ((type & (CT_PR_EV_EMPTY | CT_PR_EV_CORE | CT_PR_EV_SIGNAL
18787c478bd9Sstevel@tonic-gate 	    | CT_PR_EV_HWERR)) == 0) {
18797c478bd9Sstevel@tonic-gate 		/*
18807c478bd9Sstevel@tonic-gate 		 * There shouldn't be other events, since that's not how we set
18817c478bd9Sstevel@tonic-gate 		 * the terms. Thus, just log an error and drive on.
18827c478bd9Sstevel@tonic-gate 		 */
18837c478bd9Sstevel@tonic-gate 		log_framework(LOG_NOTICE,
18847c478bd9Sstevel@tonic-gate 		    "%s: contract %ld received unexpected critical event "
18857c478bd9Sstevel@tonic-gate 		    "(%d)\n", fmri, id, type);
18867c478bd9Sstevel@tonic-gate 		    return;
18877c478bd9Sstevel@tonic-gate 	}
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 	assert(instance_in_transition(inst) == 0);
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	if (instance_is_wait_style(inst)) {
18927c478bd9Sstevel@tonic-gate 		/*
18937c478bd9Sstevel@tonic-gate 		 * We ignore all events; if they impact the
18947c478bd9Sstevel@tonic-gate 		 * process we're monitoring, then the
18957c478bd9Sstevel@tonic-gate 		 * wait_thread will stop the instance.
18967c478bd9Sstevel@tonic-gate 		 */
18977c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
18987c478bd9Sstevel@tonic-gate 		    "%s: ignoring contract event on wait-style service\n",
18997c478bd9Sstevel@tonic-gate 		    fmri);
19007c478bd9Sstevel@tonic-gate 	} else {
19017c478bd9Sstevel@tonic-gate 		/*
19027c478bd9Sstevel@tonic-gate 		 * A CT_PR_EV_EMPTY event is an RSTOP_EXIT request.
19037c478bd9Sstevel@tonic-gate 		 */
19047c478bd9Sstevel@tonic-gate 		switch (type) {
19057c478bd9Sstevel@tonic-gate 		case CT_PR_EV_EMPTY:
19067c478bd9Sstevel@tonic-gate 			(void) stop_instance(h, inst, RSTOP_EXIT);
19077c478bd9Sstevel@tonic-gate 			break;
19087c478bd9Sstevel@tonic-gate 		case CT_PR_EV_CORE:
19097c478bd9Sstevel@tonic-gate 			(void) stop_instance(h, inst, RSTOP_CORE);
19107c478bd9Sstevel@tonic-gate 			break;
19117c478bd9Sstevel@tonic-gate 		case CT_PR_EV_SIGNAL:
19127c478bd9Sstevel@tonic-gate 			(void) stop_instance(h, inst, RSTOP_SIGNAL);
19137c478bd9Sstevel@tonic-gate 			break;
19147c478bd9Sstevel@tonic-gate 		case CT_PR_EV_HWERR:
19157c478bd9Sstevel@tonic-gate 			(void) stop_instance(h, inst, RSTOP_HWERR);
19167c478bd9Sstevel@tonic-gate 			break;
19177c478bd9Sstevel@tonic-gate 		}
19187c478bd9Sstevel@tonic-gate 	}
19197c478bd9Sstevel@tonic-gate }
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate /*
19227c478bd9Sstevel@tonic-gate  * void *restarter_contract_event_thread(void *)
19237c478bd9Sstevel@tonic-gate  *   Listens to the process contract bundle for critical events, taking action
19247c478bd9Sstevel@tonic-gate  *   on events from contracts we know we are responsible for.
19257c478bd9Sstevel@tonic-gate  */
19267c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19277c478bd9Sstevel@tonic-gate static void *
19287c478bd9Sstevel@tonic-gate restarter_contracts_event_thread(void *unused)
19297c478bd9Sstevel@tonic-gate {
19307c478bd9Sstevel@tonic-gate 	int fd, err;
19317c478bd9Sstevel@tonic-gate 	scf_handle_t *local_handle;
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 	/*
19347c478bd9Sstevel@tonic-gate 	 * Await graph load completion.  That is, stop here, until we've scanned
19357c478bd9Sstevel@tonic-gate 	 * the repository for contract - instance associations.
19367c478bd9Sstevel@tonic-gate 	 */
19377c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&st->st_load_lock);
19387c478bd9Sstevel@tonic-gate 	while (!(st->st_load_complete && st->st_load_instances == 0))
19397c478bd9Sstevel@tonic-gate 		(void) pthread_cond_wait(&st->st_load_cv, &st->st_load_lock);
19407c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&st->st_load_lock);
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate 	/*
19437c478bd9Sstevel@tonic-gate 	 * This is a new thread, and thus, gets its own handle
19447c478bd9Sstevel@tonic-gate 	 * to the repository.
19457c478bd9Sstevel@tonic-gate 	 */
19467c478bd9Sstevel@tonic-gate 	if ((local_handle = libscf_handle_create_bound(SCF_VERSION)) == NULL)
19477c478bd9Sstevel@tonic-gate 		uu_die("Unable to bind a new repository handle: %s\n",
19487c478bd9Sstevel@tonic-gate 		    scf_strerror(scf_error()));
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 	fd = open64(CTFS_ROOT "/process/pbundle", O_RDONLY);
19517c478bd9Sstevel@tonic-gate 	if (fd == -1)
19527c478bd9Sstevel@tonic-gate 		uu_die("process bundle open failed");
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	/*
19557c478bd9Sstevel@tonic-gate 	 * Make sure we get all events (including those generated by configd
19567c478bd9Sstevel@tonic-gate 	 * before this thread was started).
19577c478bd9Sstevel@tonic-gate 	 */
19587c478bd9Sstevel@tonic-gate 	err = ct_event_reset(fd);
19597c478bd9Sstevel@tonic-gate 	assert(err == 0);
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	for (;;) {
19627c478bd9Sstevel@tonic-gate 		int efd, sfd;
19637c478bd9Sstevel@tonic-gate 		ct_evthdl_t ev;
19647c478bd9Sstevel@tonic-gate 		uint32_t type;
19657c478bd9Sstevel@tonic-gate 		ctevid_t evid;
19667c478bd9Sstevel@tonic-gate 		ct_stathdl_t status;
19677c478bd9Sstevel@tonic-gate 		ctid_t ctid;
19687c478bd9Sstevel@tonic-gate 		restarter_inst_t *inst;
19697c478bd9Sstevel@tonic-gate 		uint64_t cookie;
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 		if (err = ct_event_read_critical(fd, &ev)) {
19727c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
19737c478bd9Sstevel@tonic-gate 			    "Error reading next contract event: %s",
19747c478bd9Sstevel@tonic-gate 			    strerror(err));
19757c478bd9Sstevel@tonic-gate 			continue;
19767c478bd9Sstevel@tonic-gate 		}
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate 		evid = ct_event_get_evid(ev);
19797c478bd9Sstevel@tonic-gate 		ctid = ct_event_get_ctid(ev);
19807c478bd9Sstevel@tonic-gate 		type = ct_event_get_type(ev);
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 		/* Fetch cookie. */
19837c478bd9Sstevel@tonic-gate 		if ((sfd = contract_open(ctid, "process", "status", O_RDONLY))
19847c478bd9Sstevel@tonic-gate 		    < 0) {
19857c478bd9Sstevel@tonic-gate 			ct_event_free(ev);
19867c478bd9Sstevel@tonic-gate 			continue;
19877c478bd9Sstevel@tonic-gate 		}
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 		if (err = ct_status_read(sfd, CTD_COMMON, &status)) {
19907c478bd9Sstevel@tonic-gate 			log_framework(LOG_WARNING, "Could not get status for "
19917c478bd9Sstevel@tonic-gate 			    "contract %ld: %s\n", ctid, strerror(err));
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 			startd_close(sfd);
19947c478bd9Sstevel@tonic-gate 			ct_event_free(ev);
19957c478bd9Sstevel@tonic-gate 			continue;
19967c478bd9Sstevel@tonic-gate 		}
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 		cookie = ct_status_get_cookie(status);
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 		ct_status_free(status);
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 		startd_close(sfd);
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 		/*
20057c478bd9Sstevel@tonic-gate 		 * svc.configd(1M) restart handling performed by the
20067c478bd9Sstevel@tonic-gate 		 * fork_configd_thread.  We don't acknowledge, as that thread
20077c478bd9Sstevel@tonic-gate 		 * will do so.
20087c478bd9Sstevel@tonic-gate 		 */
20097c478bd9Sstevel@tonic-gate 		if (cookie == CONFIGD_COOKIE) {
20107c478bd9Sstevel@tonic-gate 			ct_event_free(ev);
20117c478bd9Sstevel@tonic-gate 			continue;
20127c478bd9Sstevel@tonic-gate 		}
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 		inst = contract_to_inst(ctid);
20157c478bd9Sstevel@tonic-gate 		if (inst == NULL) {
20167c478bd9Sstevel@tonic-gate 			/*
20177c478bd9Sstevel@tonic-gate 			 * This can happen if we receive an EMPTY
20187c478bd9Sstevel@tonic-gate 			 * event for an abandoned contract.
20197c478bd9Sstevel@tonic-gate 			 */
20207c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
20217c478bd9Sstevel@tonic-gate 			    "Received event %d for unknown contract id "
20227c478bd9Sstevel@tonic-gate 			    "%ld\n", type, ctid);
20237c478bd9Sstevel@tonic-gate 		} else {
20247c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
20257c478bd9Sstevel@tonic-gate 			    "Received event %d for contract id "
20267c478bd9Sstevel@tonic-gate 			    "%ld (%s)\n", type, ctid,
20277c478bd9Sstevel@tonic-gate 			    inst->ri_i.i_fmri);
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 			contract_action(local_handle, inst, ctid, type);
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 			MUTEX_UNLOCK(&inst->ri_lock);
20327c478bd9Sstevel@tonic-gate 		}
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 		efd = contract_open(ct_event_get_ctid(ev), "process", "ctl",
20357c478bd9Sstevel@tonic-gate 		    O_WRONLY);
20367c478bd9Sstevel@tonic-gate 		if (efd != -1) {
20377c478bd9Sstevel@tonic-gate 			(void) ct_ctl_ack(efd, evid);
20387c478bd9Sstevel@tonic-gate 			startd_close(efd);
20397c478bd9Sstevel@tonic-gate 		}
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 		ct_event_free(ev);
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 	}
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
20467c478bd9Sstevel@tonic-gate 	return (NULL);
20477c478bd9Sstevel@tonic-gate }
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate /*
20507c478bd9Sstevel@tonic-gate  * Timeout queue, processed by restarter_timeouts_event_thread().
20517c478bd9Sstevel@tonic-gate  */
20527c478bd9Sstevel@tonic-gate timeout_queue_t *timeouts;
20537c478bd9Sstevel@tonic-gate static uu_list_pool_t *timeout_pool;
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate typedef struct timeout_update {
20567c478bd9Sstevel@tonic-gate 	pthread_mutex_t		tu_lock;
20577c478bd9Sstevel@tonic-gate 	pthread_cond_t		tu_cv;
20587c478bd9Sstevel@tonic-gate 	int			tu_wakeup;
20597c478bd9Sstevel@tonic-gate } timeout_update_t;
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate timeout_update_t *tu;
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate static const char *timeout_ovr_svcs[] = {
20647c478bd9Sstevel@tonic-gate 	"svc:/system/manifest-import:default",
20657c478bd9Sstevel@tonic-gate 	"svc:/network/initial:default",
20667c478bd9Sstevel@tonic-gate 	"svc:/network/service:default",
20677c478bd9Sstevel@tonic-gate 	"svc:/system/rmtmpfiles:default",
20687c478bd9Sstevel@tonic-gate 	"svc:/network/loopback:default",
20697c478bd9Sstevel@tonic-gate 	"svc:/network/physical:default",
20707c478bd9Sstevel@tonic-gate 	"svc:/system/device/local:default",
20717c478bd9Sstevel@tonic-gate 	"svc:/system/metainit:default",
20727c478bd9Sstevel@tonic-gate 	"svc:/system/filesystem/usr:default",
20737c478bd9Sstevel@tonic-gate 	"svc:/system/filesystem/minimal:default",
20747c478bd9Sstevel@tonic-gate 	"svc:/system/filesystem/local:default",
20757c478bd9Sstevel@tonic-gate 	NULL
20767c478bd9Sstevel@tonic-gate };
20777c478bd9Sstevel@tonic-gate 
20787c478bd9Sstevel@tonic-gate int
20797c478bd9Sstevel@tonic-gate is_timeout_ovr(restarter_inst_t *inst)
20807c478bd9Sstevel@tonic-gate {
20817c478bd9Sstevel@tonic-gate 	int i;
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	for (i = 0; timeout_ovr_svcs[i] != NULL; ++i) {
20847c478bd9Sstevel@tonic-gate 		if (strcmp(inst->ri_i.i_fmri, timeout_ovr_svcs[i]) == 0) {
20857c478bd9Sstevel@tonic-gate 			log_instance(inst, B_TRUE, "Timeout override by "
20867c478bd9Sstevel@tonic-gate 			    "svc.startd.  Using infinite timeout");
20877c478bd9Sstevel@tonic-gate 			return (1);
20887c478bd9Sstevel@tonic-gate 		}
20897c478bd9Sstevel@tonic-gate 	}
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	return (0);
20927c478bd9Sstevel@tonic-gate }
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20957c478bd9Sstevel@tonic-gate static int
20967c478bd9Sstevel@tonic-gate timeout_compare(const void *lc_arg, const void *rc_arg, void *private)
20977c478bd9Sstevel@tonic-gate {
20987c478bd9Sstevel@tonic-gate 	hrtime_t t1 = ((const timeout_entry_t *)lc_arg)->te_timeout;
20997c478bd9Sstevel@tonic-gate 	hrtime_t t2 = ((const timeout_entry_t *)rc_arg)->te_timeout;
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	if (t1 > t2)
21027c478bd9Sstevel@tonic-gate 		return (1);
21037c478bd9Sstevel@tonic-gate 	else if (t1 < t2)
21047c478bd9Sstevel@tonic-gate 		return (-1);
21057c478bd9Sstevel@tonic-gate 	return (0);
21067c478bd9Sstevel@tonic-gate }
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate void
21097c478bd9Sstevel@tonic-gate timeout_init()
21107c478bd9Sstevel@tonic-gate {
21117c478bd9Sstevel@tonic-gate 	timeouts = startd_zalloc(sizeof (timeout_queue_t));
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&timeouts->tq_lock, &mutex_attrs);
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	timeout_pool = startd_list_pool_create("timeouts",
21167c478bd9Sstevel@tonic-gate 	    sizeof (timeout_entry_t), offsetof(timeout_entry_t, te_link),
21177c478bd9Sstevel@tonic-gate 	    timeout_compare, UU_LIST_POOL_DEBUG);
21187c478bd9Sstevel@tonic-gate 	assert(timeout_pool != NULL);
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 	timeouts->tq_list = startd_list_create(timeout_pool,
21217c478bd9Sstevel@tonic-gate 	    timeouts, UU_LIST_SORTED);
21227c478bd9Sstevel@tonic-gate 	assert(timeouts->tq_list != NULL);
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 	tu = startd_zalloc(sizeof (timeout_update_t));
21257c478bd9Sstevel@tonic-gate 	(void) pthread_cond_init(&tu->tu_cv, NULL);
21267c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&tu->tu_lock, &mutex_attrs);
21277c478bd9Sstevel@tonic-gate }
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate void
21307c478bd9Sstevel@tonic-gate timeout_insert(restarter_inst_t *inst, ctid_t cid, uint64_t timeout_sec)
21317c478bd9Sstevel@tonic-gate {
21327c478bd9Sstevel@tonic-gate 	hrtime_t now, timeout;
21337c478bd9Sstevel@tonic-gate 	timeout_entry_t *entry;
21347c478bd9Sstevel@tonic-gate 	uu_list_index_t idx;
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&inst->ri_lock));
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 	now = gethrtime();
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	/*
21417c478bd9Sstevel@tonic-gate 	 * If we overflow LLONG_MAX, we're never timing out anyways, so
21427c478bd9Sstevel@tonic-gate 	 * just return.
21437c478bd9Sstevel@tonic-gate 	 */
21447c478bd9Sstevel@tonic-gate 	if (timeout_sec >= (LLONG_MAX - now) / 1000000000LL) {
21457c478bd9Sstevel@tonic-gate 		log_instance(inst, B_TRUE, "timeout_seconds too large, "
21467c478bd9Sstevel@tonic-gate 		    "treating as infinite.");
21477c478bd9Sstevel@tonic-gate 		return;
21487c478bd9Sstevel@tonic-gate 	}
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	/* hrtime is in nanoseconds. Convert timeout_sec. */
21517c478bd9Sstevel@tonic-gate 	timeout = now + (timeout_sec * 1000000000LL);
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate 	entry = startd_alloc(sizeof (timeout_entry_t));
21547c478bd9Sstevel@tonic-gate 	entry->te_timeout = timeout;
21557c478bd9Sstevel@tonic-gate 	entry->te_ctid = cid;
21567c478bd9Sstevel@tonic-gate 	entry->te_fmri = safe_strdup(inst->ri_i.i_fmri);
21577c478bd9Sstevel@tonic-gate 	entry->te_logstem = safe_strdup(inst->ri_logstem);
21587c478bd9Sstevel@tonic-gate 	entry->te_fired = 0;
21597c478bd9Sstevel@tonic-gate 	/* Insert the calculated timeout time onto the queue. */
21607c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&timeouts->tq_lock);
21617c478bd9Sstevel@tonic-gate 	(void) uu_list_find(timeouts->tq_list, entry, NULL, &idx);
21627c478bd9Sstevel@tonic-gate 	uu_list_node_init(entry, &entry->te_link, timeout_pool);
21637c478bd9Sstevel@tonic-gate 	uu_list_insert(timeouts->tq_list, entry, idx);
21647c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&timeouts->tq_lock);
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 	assert(inst->ri_timeout == NULL);
21677c478bd9Sstevel@tonic-gate 	inst->ri_timeout = entry;
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&tu->tu_lock);
21707c478bd9Sstevel@tonic-gate 	tu->tu_wakeup = 1;
21717c478bd9Sstevel@tonic-gate 	(void) pthread_cond_broadcast(&tu->tu_cv);
21727c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&tu->tu_lock);
21737c478bd9Sstevel@tonic-gate }
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 
21767c478bd9Sstevel@tonic-gate void
21777c478bd9Sstevel@tonic-gate timeout_remove(restarter_inst_t *inst, ctid_t cid)
21787c478bd9Sstevel@tonic-gate {
21797c478bd9Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&inst->ri_lock));
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 	if (inst->ri_timeout == NULL)
21827c478bd9Sstevel@tonic-gate 		return;
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 	assert(inst->ri_timeout->te_ctid == cid);
21857c478bd9Sstevel@tonic-gate 
21867c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&timeouts->tq_lock);
21877c478bd9Sstevel@tonic-gate 	uu_list_remove(timeouts->tq_list, inst->ri_timeout);
21887c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&timeouts->tq_lock);
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 	free(inst->ri_timeout->te_fmri);
21917c478bd9Sstevel@tonic-gate 	free(inst->ri_timeout->te_logstem);
21927c478bd9Sstevel@tonic-gate 	startd_free(inst->ri_timeout, sizeof (timeout_entry_t));
21937c478bd9Sstevel@tonic-gate 	inst->ri_timeout = NULL;
21947c478bd9Sstevel@tonic-gate }
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate static int
21977c478bd9Sstevel@tonic-gate timeout_now()
21987c478bd9Sstevel@tonic-gate {
21997c478bd9Sstevel@tonic-gate 	timeout_entry_t *e;
22007c478bd9Sstevel@tonic-gate 	hrtime_t now;
22017c478bd9Sstevel@tonic-gate 	int ret;
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate 	now = gethrtime();
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 	/*
22067c478bd9Sstevel@tonic-gate 	 * Walk through the (sorted) timeouts list.  While the timeout
22077c478bd9Sstevel@tonic-gate 	 * at the head of the list is <= the current time, kill the
22087c478bd9Sstevel@tonic-gate 	 * method.
22097c478bd9Sstevel@tonic-gate 	 */
22107c478bd9Sstevel@tonic-gate 	MUTEX_LOCK(&timeouts->tq_lock);
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	for (e = uu_list_first(timeouts->tq_list);
22137c478bd9Sstevel@tonic-gate 	    e != NULL && e->te_timeout <= now;
22147c478bd9Sstevel@tonic-gate 	    e = uu_list_next(timeouts->tq_list, e)) {
22157c478bd9Sstevel@tonic-gate 		log_framework(LOG_WARNING, "%s: Method or service exit timed "
22167c478bd9Sstevel@tonic-gate 		    "out.  Killing contract %ld.\n", e->te_fmri, e->te_ctid);
22177c478bd9Sstevel@tonic-gate 		log_instance_fmri(e->te_fmri, e->te_logstem, B_TRUE,
22187c478bd9Sstevel@tonic-gate 		    "Method or service exit timed out.  Killing contract %ld",
22197c478bd9Sstevel@tonic-gate 		    e->te_ctid);
22207c478bd9Sstevel@tonic-gate 		e->te_fired = 1;
22217c478bd9Sstevel@tonic-gate 		(void) contract_kill(e->te_ctid, SIGKILL, e->te_fmri);
22227c478bd9Sstevel@tonic-gate 	}
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 	if (uu_list_numnodes(timeouts->tq_list) > 0)
22257c478bd9Sstevel@tonic-gate 		ret = 0;
22267c478bd9Sstevel@tonic-gate 	else
22277c478bd9Sstevel@tonic-gate 		ret = -1;
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	MUTEX_UNLOCK(&timeouts->tq_lock);
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 	return (ret);
22327c478bd9Sstevel@tonic-gate }
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate /*
22357c478bd9Sstevel@tonic-gate  * void *restarter_timeouts_event_thread(void *)
22367c478bd9Sstevel@tonic-gate  *   Responsible for monitoring the method timeouts.  This thread must
22377c478bd9Sstevel@tonic-gate  *   be started before any methods are called.
22387c478bd9Sstevel@tonic-gate  */
22397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22407c478bd9Sstevel@tonic-gate static void *
22417c478bd9Sstevel@tonic-gate restarter_timeouts_event_thread(void *unused)
22427c478bd9Sstevel@tonic-gate {
22437c478bd9Sstevel@tonic-gate 	/*
22447c478bd9Sstevel@tonic-gate 	 * Timeouts are entered on a priority queue, which is processed by
22457c478bd9Sstevel@tonic-gate 	 * this thread.  As timeouts are specified in seconds, we'll do
22467c478bd9Sstevel@tonic-gate 	 * the necessary processing every second, as long as the queue
22477c478bd9Sstevel@tonic-gate 	 * is not empty.
22487c478bd9Sstevel@tonic-gate 	 */
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate 	/*CONSTCOND*/
22517c478bd9Sstevel@tonic-gate 	while (1) {
22527c478bd9Sstevel@tonic-gate 		/*
22537c478bd9Sstevel@tonic-gate 		 * As long as the timeout list isn't empty, process it
22547c478bd9Sstevel@tonic-gate 		 * every second.
22557c478bd9Sstevel@tonic-gate 		 */
22567c478bd9Sstevel@tonic-gate 		if (timeout_now() == 0) {
22577c478bd9Sstevel@tonic-gate 			(void) sleep(1);
22587c478bd9Sstevel@tonic-gate 			continue;
22597c478bd9Sstevel@tonic-gate 		}
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 		/* The list is empty, wait until we have more timeouts. */
22627c478bd9Sstevel@tonic-gate 		MUTEX_LOCK(&tu->tu_lock);
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate 		while (tu->tu_wakeup == 0)
22657c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&tu->tu_cv, &tu->tu_lock);
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 		tu->tu_wakeup = 0;
22687c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&tu->tu_lock);
22697c478bd9Sstevel@tonic-gate 	}
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 	return (NULL);
22727c478bd9Sstevel@tonic-gate }
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate void
22757c478bd9Sstevel@tonic-gate restarter_start()
22767c478bd9Sstevel@tonic-gate {
22777c478bd9Sstevel@tonic-gate 	(void) startd_thread_create(restarter_timeouts_event_thread, NULL);
22787c478bd9Sstevel@tonic-gate 	(void) startd_thread_create(restarter_event_thread, NULL);
22797c478bd9Sstevel@tonic-gate 	(void) startd_thread_create(restarter_contracts_event_thread, NULL);
22807c478bd9Sstevel@tonic-gate 	(void) startd_thread_create(wait_thread, NULL);
22817c478bd9Sstevel@tonic-gate }
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 
22847c478bd9Sstevel@tonic-gate void
22857c478bd9Sstevel@tonic-gate restarter_init()
22867c478bd9Sstevel@tonic-gate {
22877c478bd9Sstevel@tonic-gate 	restarter_instance_pool = startd_list_pool_create("restarter_instances",
22887c478bd9Sstevel@tonic-gate 	    sizeof (restarter_inst_t), offsetof(restarter_inst_t,
22897c478bd9Sstevel@tonic-gate 		ri_link), restarter_instance_compare, UU_LIST_POOL_DEBUG);
22907c478bd9Sstevel@tonic-gate 	(void) memset(&instance_list, 0, sizeof (instance_list));
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&instance_list.ril_lock, &mutex_attrs);
22937c478bd9Sstevel@tonic-gate 	instance_list.ril_instance_list = startd_list_create(
22947c478bd9Sstevel@tonic-gate 	    restarter_instance_pool, &instance_list, UU_LIST_SORTED);
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	restarter_queue_pool = startd_list_pool_create(
22977c478bd9Sstevel@tonic-gate 	    "restarter_instance_queue", sizeof (restarter_instance_qentry_t),
22987c478bd9Sstevel@tonic-gate 	    offsetof(restarter_instance_qentry_t,  riq_link), NULL,
22997c478bd9Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG);
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	contract_list_pool = startd_list_pool_create(
23027c478bd9Sstevel@tonic-gate 	    "contract_list", sizeof (contract_entry_t),
23037c478bd9Sstevel@tonic-gate 	    offsetof(contract_entry_t,  ce_link), NULL,
23047c478bd9Sstevel@tonic-gate 	    UU_LIST_POOL_DEBUG);
23057c478bd9Sstevel@tonic-gate 	contract_hash_init();
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Initialized restarter\n");
23087c478bd9Sstevel@tonic-gate }
2309