xref: /onnv-gate/usr/src/cmd/svc/startd/method.c (revision 9765:2522fef20c5f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51712Srm88369  * Common Development and Distribution License (the "License").
61712Srm88369  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
219263SSean.Wilcox@Sun.COM 
220Sstevel@tonic-gate /*
238944Sdp@eng.sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * method.c - method execution functions
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * This file contains the routines needed to run a method:  a fork(2)-exec(2)
310Sstevel@tonic-gate  * invocation monitored using either the contract filesystem or waitpid(2).
320Sstevel@tonic-gate  * (Plain fork1(2) support is provided in fork.c.)
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * Contract Transfer
350Sstevel@tonic-gate  *   When we restart a service, we want to transfer any contracts that the old
360Sstevel@tonic-gate  *   service's contract inherited.  This means that (a) we must not abandon the
370Sstevel@tonic-gate  *   old contract when the service dies and (b) we must write the id of the old
380Sstevel@tonic-gate  *   contract into the terms of the new contract.  There should be limits to
390Sstevel@tonic-gate  *   (a), though, since we don't want to keep the contract around forever.  To
400Sstevel@tonic-gate  *   this end we'll say that services in the offline state may have a contract
410Sstevel@tonic-gate  *   to be transfered and services in the disabled or maintenance states cannot.
420Sstevel@tonic-gate  *   This means that when a service transitions from online (or degraded) to
430Sstevel@tonic-gate  *   offline, the contract should be preserved, and when the service transitions
440Sstevel@tonic-gate  *   from offline to online (i.e., the start method), we'll transfer inherited
450Sstevel@tonic-gate  *   contracts.
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include <sys/contract/process.h>
490Sstevel@tonic-gate #include <sys/ctfs.h>
500Sstevel@tonic-gate #include <sys/stat.h>
510Sstevel@tonic-gate #include <sys/time.h>
520Sstevel@tonic-gate #include <sys/types.h>
530Sstevel@tonic-gate #include <sys/uio.h>
540Sstevel@tonic-gate #include <sys/wait.h>
550Sstevel@tonic-gate #include <alloca.h>
560Sstevel@tonic-gate #include <assert.h>
570Sstevel@tonic-gate #include <errno.h>
580Sstevel@tonic-gate #include <fcntl.h>
590Sstevel@tonic-gate #include <libcontract.h>
600Sstevel@tonic-gate #include <libcontract_priv.h>
610Sstevel@tonic-gate #include <libgen.h>
620Sstevel@tonic-gate #include <librestart.h>
630Sstevel@tonic-gate #include <libscf.h>
640Sstevel@tonic-gate #include <limits.h>
650Sstevel@tonic-gate #include <port.h>
660Sstevel@tonic-gate #include <sac.h>
670Sstevel@tonic-gate #include <signal.h>
680Sstevel@tonic-gate #include <stdlib.h>
690Sstevel@tonic-gate #include <string.h>
700Sstevel@tonic-gate #include <strings.h>
710Sstevel@tonic-gate #include <unistd.h>
724244Sjeanm #include <atomic.h>
734244Sjeanm #include <poll.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #include "startd.h"
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #define	SBIN_SH		"/sbin/sh"
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
804244Sjeanm  * Used to tell if contracts are in the process of being
814244Sjeanm  * stored into the svc.startd internal hash table.
824244Sjeanm  */
834244Sjeanm volatile uint16_t	storing_contract = 0;
844244Sjeanm 
854244Sjeanm /*
860Sstevel@tonic-gate  * Mapping from restart_on method-type to contract events.  Must correspond to
870Sstevel@tonic-gate  * enum method_restart_t.
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate static uint_t method_events[] = {
900Sstevel@tonic-gate 	/* METHOD_RESTART_ALL */
910Sstevel@tonic-gate 	CT_PR_EV_HWERR | CT_PR_EV_SIGNAL | CT_PR_EV_CORE | CT_PR_EV_EMPTY,
920Sstevel@tonic-gate 	/* METHOD_RESTART_EXTERNAL_FAULT */
930Sstevel@tonic-gate 	CT_PR_EV_HWERR | CT_PR_EV_SIGNAL,
940Sstevel@tonic-gate 	/* METHOD_RESTART_ANY_FAULT */
950Sstevel@tonic-gate 	CT_PR_EV_HWERR | CT_PR_EV_SIGNAL | CT_PR_EV_CORE
960Sstevel@tonic-gate };
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * method_record_start(restarter_inst_t *)
1000Sstevel@tonic-gate  *   Record a service start for rate limiting.  Place the current time
1010Sstevel@tonic-gate  *   in the circular array of instance starts.
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate static void
1040Sstevel@tonic-gate method_record_start(restarter_inst_t *inst)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate 	int index = inst->ri_start_index++ % RINST_START_TIMES;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	inst->ri_start_time[index] = gethrtime();
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  * method_rate_critical(restarter_inst_t *)
1130Sstevel@tonic-gate  *    Return true if the average start interval is less than the permitted
1140Sstevel@tonic-gate  *    interval.  Implicit success if insufficient measurements for an
1150Sstevel@tonic-gate  *    average exist.
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate static int
1180Sstevel@tonic-gate method_rate_critical(restarter_inst_t *inst)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	uint_t n = inst->ri_start_index;
1210Sstevel@tonic-gate 	hrtime_t avg_ns = 0;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	if (inst->ri_start_index < RINST_START_TIMES)
1240Sstevel@tonic-gate 		return (0);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	avg_ns =
1270Sstevel@tonic-gate 	    (inst->ri_start_time[(n - 1) % RINST_START_TIMES] -
1280Sstevel@tonic-gate 	    inst->ri_start_time[n % RINST_START_TIMES]) /
1290Sstevel@tonic-gate 	    (RINST_START_TIMES - 1);
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	return (avg_ns < RINST_FAILURE_RATE_NS);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * int method_is_transient()
1360Sstevel@tonic-gate  *   Determine if the method for the given instance is transient,
1370Sstevel@tonic-gate  *   from a contract perspective. Return 1 if it is, and 0 if it isn't.
1380Sstevel@tonic-gate  */
1390Sstevel@tonic-gate static int
1400Sstevel@tonic-gate method_is_transient(restarter_inst_t *inst, int type)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate 	if (instance_is_transient_style(inst) || type != METHOD_START)
1430Sstevel@tonic-gate 		return (1);
1440Sstevel@tonic-gate 	else
1450Sstevel@tonic-gate 		return (0);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate  * void method_store_contract()
1500Sstevel@tonic-gate  *   Store the newly created contract id into local structures and
1510Sstevel@tonic-gate  *   the repository.  If the repository connection is broken it is rebound.
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate static void
1540Sstevel@tonic-gate method_store_contract(restarter_inst_t *inst, int type, ctid_t *cid)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	int r;
1570Sstevel@tonic-gate 	boolean_t primary;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	if (errno = contract_latest(cid))
1600Sstevel@tonic-gate 		uu_die("%s: Couldn't get new contract's id", inst->ri_i.i_fmri);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	primary = !method_is_transient(inst, type);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (!primary) {
1650Sstevel@tonic-gate 		if (inst->ri_i.i_transient_ctid != 0) {
1660Sstevel@tonic-gate 			log_framework(LOG_INFO,
1670Sstevel@tonic-gate 			    "%s: transient ctid expected to be 0 but "
1680Sstevel@tonic-gate 			    "was set to %ld\n", inst->ri_i.i_fmri,
1690Sstevel@tonic-gate 			    inst->ri_i.i_transient_ctid);
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 		inst->ri_i.i_transient_ctid = *cid;
1730Sstevel@tonic-gate 	} else {
1740Sstevel@tonic-gate 		if (inst->ri_i.i_primary_ctid != 0) {
1750Sstevel@tonic-gate 			/*
1760Sstevel@tonic-gate 			 * There was an old contract that we transferred.
1770Sstevel@tonic-gate 			 * Remove it.
1780Sstevel@tonic-gate 			 */
1790Sstevel@tonic-gate 			method_remove_contract(inst, B_TRUE, B_FALSE);
1800Sstevel@tonic-gate 		}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 		if (inst->ri_i.i_primary_ctid != 0) {
1830Sstevel@tonic-gate 			log_framework(LOG_INFO,
1840Sstevel@tonic-gate 			    "%s: primary ctid expected to be 0 but "
1850Sstevel@tonic-gate 			    "was set to %ld\n", inst->ri_i.i_fmri,
1860Sstevel@tonic-gate 			    inst->ri_i.i_primary_ctid);
1870Sstevel@tonic-gate 		}
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 		inst->ri_i.i_primary_ctid = *cid;
1900Sstevel@tonic-gate 		inst->ri_i.i_primary_ctid_stopped = 0;
1910Sstevel@tonic-gate 
1924244Sjeanm 		log_framework(LOG_DEBUG, "Storing primary contract %ld for "
1934244Sjeanm 		    "%s.\n", *cid, inst->ri_i.i_fmri);
1944244Sjeanm 
1950Sstevel@tonic-gate 		contract_hash_store(*cid, inst->ri_id);
1960Sstevel@tonic-gate 	}
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate again:
1990Sstevel@tonic-gate 	if (inst->ri_mi_deleted)
2000Sstevel@tonic-gate 		return;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	r = restarter_store_contract(inst->ri_m_inst, *cid, primary ?
2030Sstevel@tonic-gate 	    RESTARTER_CONTRACT_PRIMARY : RESTARTER_CONTRACT_TRANSIENT);
2040Sstevel@tonic-gate 	switch (r) {
2050Sstevel@tonic-gate 	case 0:
2060Sstevel@tonic-gate 		break;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	case ECANCELED:
2090Sstevel@tonic-gate 		inst->ri_mi_deleted = B_TRUE;
2100Sstevel@tonic-gate 		break;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	case ECONNABORTED:
2130Sstevel@tonic-gate 		libscf_handle_rebind(scf_instance_handle(inst->ri_m_inst));
2140Sstevel@tonic-gate 		/* FALLTHROUGH */
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	case EBADF:
2170Sstevel@tonic-gate 		libscf_reget_instance(inst);
2180Sstevel@tonic-gate 		goto again;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	case ENOMEM:
2210Sstevel@tonic-gate 	case EPERM:
2220Sstevel@tonic-gate 	case EACCES:
2230Sstevel@tonic-gate 	case EROFS:
2240Sstevel@tonic-gate 		uu_die("%s: Couldn't store contract id %ld",
2250Sstevel@tonic-gate 		    inst->ri_i.i_fmri, *cid);
2260Sstevel@tonic-gate 		/* NOTREACHED */
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	case EINVAL:
2290Sstevel@tonic-gate 	default:
2300Sstevel@tonic-gate 		bad_error("restarter_store_contract", r);
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate  * void method_remove_contract()
2360Sstevel@tonic-gate  *   Remove any non-permanent contracts from internal structures and
2370Sstevel@tonic-gate  *   the repository, then abandon them.
2380Sstevel@tonic-gate  *   Returns
2390Sstevel@tonic-gate  *     0 - success
2400Sstevel@tonic-gate  *     ECANCELED - inst was deleted from the repository
2410Sstevel@tonic-gate  *
2420Sstevel@tonic-gate  *   If the repository connection was broken, it is rebound.
2430Sstevel@tonic-gate  */
2440Sstevel@tonic-gate void
2450Sstevel@tonic-gate method_remove_contract(restarter_inst_t *inst, boolean_t primary,
2460Sstevel@tonic-gate     boolean_t abandon)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate 	ctid_t * const ctidp = primary ? &inst->ri_i.i_primary_ctid :
2490Sstevel@tonic-gate 	    &inst->ri_i.i_transient_ctid;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	int r;
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	assert(*ctidp != 0);
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Removing %s contract %lu for %s.\n",
2560Sstevel@tonic-gate 	    primary ? "primary" : "transient", *ctidp, inst->ri_i.i_fmri);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	if (abandon)
2590Sstevel@tonic-gate 		contract_abandon(*ctidp);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate again:
2620Sstevel@tonic-gate 	if (inst->ri_mi_deleted) {
2630Sstevel@tonic-gate 		r = ECANCELED;
2640Sstevel@tonic-gate 		goto out;
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	r = restarter_remove_contract(inst->ri_m_inst, *ctidp, primary ?
2680Sstevel@tonic-gate 	    RESTARTER_CONTRACT_PRIMARY : RESTARTER_CONTRACT_TRANSIENT);
2690Sstevel@tonic-gate 	switch (r) {
2700Sstevel@tonic-gate 	case 0:
2710Sstevel@tonic-gate 		break;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	case ECANCELED:
2740Sstevel@tonic-gate 		inst->ri_mi_deleted = B_TRUE;
2750Sstevel@tonic-gate 		break;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	case ECONNABORTED:
2780Sstevel@tonic-gate 		libscf_handle_rebind(scf_instance_handle(inst->ri_m_inst));
2790Sstevel@tonic-gate 		/* FALLTHROUGH */
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	case EBADF:
2820Sstevel@tonic-gate 		libscf_reget_instance(inst);
2830Sstevel@tonic-gate 		goto again;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	case ENOMEM:
2860Sstevel@tonic-gate 	case EPERM:
2870Sstevel@tonic-gate 	case EACCES:
2880Sstevel@tonic-gate 	case EROFS:
2890Sstevel@tonic-gate 		log_error(LOG_INFO, "%s: Couldn't remove contract id %ld: "
2900Sstevel@tonic-gate 		    "%s.\n", inst->ri_i.i_fmri, *ctidp, strerror(r));
2910Sstevel@tonic-gate 		break;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	case EINVAL:
2940Sstevel@tonic-gate 	default:
2950Sstevel@tonic-gate 		bad_error("restarter_remove_contract", r);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate out:
2990Sstevel@tonic-gate 	if (primary)
3000Sstevel@tonic-gate 		contract_hash_remove(*ctidp);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	*ctidp = 0;
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3056073Sacruz static const char *method_names[] = { "start", "stop", "refresh" };
3066073Sacruz 
3070Sstevel@tonic-gate /*
3080Sstevel@tonic-gate  * int method_ready_contract(restarter_inst_t *, int, method_restart_t, int)
3090Sstevel@tonic-gate  *
3100Sstevel@tonic-gate  *   Activate a contract template for the type method of inst.  type,
3110Sstevel@tonic-gate  *   restart_on, and cte_mask dictate the critical events term of the contract.
3120Sstevel@tonic-gate  *   Returns
3130Sstevel@tonic-gate  *     0 - success
3140Sstevel@tonic-gate  *     ECANCELED - inst has been deleted from the repository
3150Sstevel@tonic-gate  */
3160Sstevel@tonic-gate static int
3170Sstevel@tonic-gate method_ready_contract(restarter_inst_t *inst, int type,
3180Sstevel@tonic-gate     method_restart_t restart_on, uint_t cte_mask)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate 	int tmpl, err, istrans, iswait, ret;
3210Sstevel@tonic-gate 	uint_t cevents, fevents;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	/*
3240Sstevel@tonic-gate 	 * Correctly supporting wait-style services is tricky without
3250Sstevel@tonic-gate 	 * rearchitecting startd to cope with multiple event sources
3260Sstevel@tonic-gate 	 * simultaneously trying to stop an instance.  Until a better
3270Sstevel@tonic-gate 	 * solution is implemented, we avoid this problem for
3280Sstevel@tonic-gate 	 * wait-style services by making contract events fatal and
3290Sstevel@tonic-gate 	 * letting the wait code alone handle stopping the service.
3300Sstevel@tonic-gate 	 */
3310Sstevel@tonic-gate 	iswait = instance_is_wait_style(inst);
3320Sstevel@tonic-gate 	istrans = method_is_transient(inst, type);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	tmpl = open64(CTFS_ROOT "/process/template", O_RDWR);
3350Sstevel@tonic-gate 	if (tmpl == -1)
3360Sstevel@tonic-gate 		uu_die("Could not create contract template");
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	/*
3390Sstevel@tonic-gate 	 * We assume non-login processes are unlikely to create
3400Sstevel@tonic-gate 	 * multiple process groups, and set CT_PR_PGRPONLY for all
3410Sstevel@tonic-gate 	 * wait-style services' contracts.
3420Sstevel@tonic-gate 	 */
3430Sstevel@tonic-gate 	err = ct_pr_tmpl_set_param(tmpl, CT_PR_INHERIT | CT_PR_REGENT |
3440Sstevel@tonic-gate 	    (iswait ? CT_PR_PGRPONLY : 0));
3450Sstevel@tonic-gate 	assert(err == 0);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if (istrans) {
3480Sstevel@tonic-gate 		cevents = 0;
3490Sstevel@tonic-gate 		fevents = 0;
3500Sstevel@tonic-gate 	} else {
3510Sstevel@tonic-gate 		assert(restart_on >= 0);
3520Sstevel@tonic-gate 		assert(restart_on <= METHOD_RESTART_ANY_FAULT);
3530Sstevel@tonic-gate 		cevents = method_events[restart_on] & ~cte_mask;
3540Sstevel@tonic-gate 		fevents = iswait ?
3550Sstevel@tonic-gate 		    (method_events[restart_on] & ~cte_mask & CT_PR_ALLFATAL) :
3560Sstevel@tonic-gate 		    0;
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	err = ct_tmpl_set_critical(tmpl, cevents);
3600Sstevel@tonic-gate 	assert(err == 0);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	err = ct_tmpl_set_informative(tmpl, 0);
3630Sstevel@tonic-gate 	assert(err == 0);
3640Sstevel@tonic-gate 	err = ct_pr_tmpl_set_fatal(tmpl, fevents);
3650Sstevel@tonic-gate 	assert(err == 0);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 	err = ct_tmpl_set_cookie(tmpl, istrans ?  METHOD_OTHER_COOKIE :
3680Sstevel@tonic-gate 	    METHOD_START_COOKIE);
3690Sstevel@tonic-gate 	assert(err == 0);
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	if (type == METHOD_START && inst->ri_i.i_primary_ctid != 0) {
3720Sstevel@tonic-gate 		ret = ct_pr_tmpl_set_transfer(tmpl, inst->ri_i.i_primary_ctid);
3730Sstevel@tonic-gate 		switch (ret) {
3740Sstevel@tonic-gate 		case 0:
3750Sstevel@tonic-gate 			break;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		case ENOTEMPTY:
3780Sstevel@tonic-gate 			/* No contracts for you! */
3790Sstevel@tonic-gate 			method_remove_contract(inst, B_TRUE, B_TRUE);
3800Sstevel@tonic-gate 			if (inst->ri_mi_deleted) {
3810Sstevel@tonic-gate 				ret = ECANCELED;
3820Sstevel@tonic-gate 				goto out;
3830Sstevel@tonic-gate 			}
3840Sstevel@tonic-gate 			break;
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 		case EINVAL:
3870Sstevel@tonic-gate 		case ESRCH:
3880Sstevel@tonic-gate 		case EACCES:
3890Sstevel@tonic-gate 		default:
3900Sstevel@tonic-gate 			bad_error("ct_pr_tmpl_set_transfer", ret);
3910Sstevel@tonic-gate 		}
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 
3946073Sacruz 	err = ct_pr_tmpl_set_svc_fmri(tmpl, inst->ri_i.i_fmri);
3956073Sacruz 	assert(err == 0);
3966073Sacruz 	err = ct_pr_tmpl_set_svc_aux(tmpl, method_names[type]);
3976073Sacruz 	assert(err == 0);
3986073Sacruz 
3990Sstevel@tonic-gate 	err = ct_tmpl_activate(tmpl);
4000Sstevel@tonic-gate 	assert(err == 0);
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	ret = 0;
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate out:
4050Sstevel@tonic-gate 	err = close(tmpl);
4060Sstevel@tonic-gate 	assert(err == 0);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	return (ret);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate static void
4120Sstevel@tonic-gate exec_method(const restarter_inst_t *inst, int type, const char *method,
4130Sstevel@tonic-gate     struct method_context *mcp, uint8_t need_session)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	char *cmd;
4160Sstevel@tonic-gate 	const char *errf;
4170Sstevel@tonic-gate 	char **nenv;
4184816Sacruz 	int rsmc_errno = 0;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	cmd = uu_msprintf("exec %s", method);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	if (inst->ri_utmpx_prefix[0] != '\0' && inst->ri_utmpx_prefix != NULL)
4230Sstevel@tonic-gate 		(void) utmpx_mark_init(getpid(), inst->ri_utmpx_prefix);
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	setlog(inst->ri_logstem);
4265238Slianep 	log_instance(inst, B_FALSE, "Executing %s method (\"%s\").",
4270Sstevel@tonic-gate 	    method_names[type], method);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	if (need_session)
4300Sstevel@tonic-gate 		(void) setpgrp();
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	/* Set credentials. */
4334816Sacruz 	rsmc_errno = restarter_set_method_context(mcp, &errf);
4344816Sacruz 	if (rsmc_errno != 0) {
4359263SSean.Wilcox@Sun.COM 		log_instance(inst, B_FALSE,
4369263SSean.Wilcox@Sun.COM 		    "svc.startd could not set context for method: ");
4370Sstevel@tonic-gate 
4384816Sacruz 		if (rsmc_errno == -1) {
4390Sstevel@tonic-gate 			if (strcmp(errf, "core_set_process_path") == 0) {
4409263SSean.Wilcox@Sun.COM 				log_instance(inst, B_FALSE,
4419263SSean.Wilcox@Sun.COM 				    "Could not set corefile path.");
4420Sstevel@tonic-gate 			} else if (strcmp(errf, "setproject") == 0) {
4439263SSean.Wilcox@Sun.COM 				log_instance(inst, B_FALSE, "%s: a resource "
4449263SSean.Wilcox@Sun.COM 				    "control assignment failed", errf);
4450Sstevel@tonic-gate 			} else if (strcmp(errf, "pool_set_binding") == 0) {
4469263SSean.Wilcox@Sun.COM 				log_instance(inst, B_FALSE, "%s: a system "
4479263SSean.Wilcox@Sun.COM 				    "error occurred", errf);
4480Sstevel@tonic-gate 			} else {
4490Sstevel@tonic-gate #ifndef NDEBUG
4500Sstevel@tonic-gate 				uu_warn("%s:%d: Bad function name \"%s\" for "
4510Sstevel@tonic-gate 				    "error %d from "
4520Sstevel@tonic-gate 				    "restarter_set_method_context().\n",
4534816Sacruz 				    __FILE__, __LINE__, errf, rsmc_errno);
4540Sstevel@tonic-gate #endif
4550Sstevel@tonic-gate 				abort();
4560Sstevel@tonic-gate 			}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 			exit(1);
4590Sstevel@tonic-gate 		}
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 		if (errf != NULL && strcmp(errf, "pool_set_binding") == 0) {
4624816Sacruz 			switch (rsmc_errno) {
4630Sstevel@tonic-gate 			case ENOENT:
4649263SSean.Wilcox@Sun.COM 				log_instance(inst, B_FALSE, "%s: the pool "
4659263SSean.Wilcox@Sun.COM 				    "could not be found", errf);
4660Sstevel@tonic-gate 				break;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 			case EBADF:
4699263SSean.Wilcox@Sun.COM 				log_instance(inst, B_FALSE, "%s: the "
4709263SSean.Wilcox@Sun.COM 				    "configuration is invalid", errf);
4710Sstevel@tonic-gate 				break;
4720Sstevel@tonic-gate 
4731712Srm88369 			case EINVAL:
4749263SSean.Wilcox@Sun.COM 				log_instance(inst, B_FALSE, "%s: pool name "
4759263SSean.Wilcox@Sun.COM 				    "\"%s\" is invalid", errf,
4769263SSean.Wilcox@Sun.COM 				    mcp->resource_pool);
4771712Srm88369 				break;
4781712Srm88369 
4790Sstevel@tonic-gate 			default:
4800Sstevel@tonic-gate #ifndef NDEBUG
4810Sstevel@tonic-gate 				uu_warn("%s:%d: Bad error %d for function %s "
4820Sstevel@tonic-gate 				    "in restarter_set_method_context().\n",
4834816Sacruz 				    __FILE__, __LINE__, rsmc_errno, errf);
4840Sstevel@tonic-gate #endif
4850Sstevel@tonic-gate 				abort();
4860Sstevel@tonic-gate 			}
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 			exit(SMF_EXIT_ERR_CONFIG);
4890Sstevel@tonic-gate 		}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 		if (errf != NULL) {
4924816Sacruz 			errno = rsmc_errno;
4930Sstevel@tonic-gate 			perror(errf);
4940Sstevel@tonic-gate 
4954816Sacruz 			switch (rsmc_errno) {
4960Sstevel@tonic-gate 			case EINVAL:
4970Sstevel@tonic-gate 			case EPERM:
4980Sstevel@tonic-gate 			case ENOENT:
4990Sstevel@tonic-gate 			case ENAMETOOLONG:
5000Sstevel@tonic-gate 			case ERANGE:
5010Sstevel@tonic-gate 			case ESRCH:
5020Sstevel@tonic-gate 				exit(SMF_EXIT_ERR_CONFIG);
5030Sstevel@tonic-gate 				/* NOTREACHED */
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 			default:
5060Sstevel@tonic-gate 				exit(1);
5070Sstevel@tonic-gate 			}
5080Sstevel@tonic-gate 		}
5090Sstevel@tonic-gate 
5104816Sacruz 		switch (rsmc_errno) {
5110Sstevel@tonic-gate 		case ENOMEM:
5129263SSean.Wilcox@Sun.COM 			log_instance(inst, B_FALSE, "Out of memory.");
5130Sstevel@tonic-gate 			exit(1);
5140Sstevel@tonic-gate 			/* NOTREACHED */
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 		case ENOENT:
5179263SSean.Wilcox@Sun.COM 			log_instance(inst, B_FALSE, "Missing passwd entry for "
5189263SSean.Wilcox@Sun.COM 			    "user.");
5190Sstevel@tonic-gate 			exit(SMF_EXIT_ERR_CONFIG);
5200Sstevel@tonic-gate 			/* NOTREACHED */
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 		default:
5230Sstevel@tonic-gate #ifndef NDEBUG
5240Sstevel@tonic-gate 			uu_warn("%s:%d: Bad miscellaneous error %d from "
5250Sstevel@tonic-gate 			    "restarter_set_method_context().\n", __FILE__,
5264816Sacruz 			    __LINE__, rsmc_errno);
5270Sstevel@tonic-gate #endif
5280Sstevel@tonic-gate 			abort();
5290Sstevel@tonic-gate 		}
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 
5325040Swesolows 	nenv = set_smf_env(mcp->env, mcp->env_sz, NULL, inst,
5335040Swesolows 	    method_names[type]);
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	log_preexec();
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	(void) execle(SBIN_SH, SBIN_SH, "-c", cmd, NULL, nenv);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	exit(10);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate static void
5430Sstevel@tonic-gate write_status(restarter_inst_t *inst, const char *mname, int stat)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	int r;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate again:
5480Sstevel@tonic-gate 	if (inst->ri_mi_deleted)
5490Sstevel@tonic-gate 		return;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	r = libscf_write_method_status(inst->ri_m_inst, mname, stat);
5520Sstevel@tonic-gate 	switch (r) {
5530Sstevel@tonic-gate 	case 0:
5540Sstevel@tonic-gate 		break;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	case ECONNABORTED:
5570Sstevel@tonic-gate 		libscf_reget_instance(inst);
5580Sstevel@tonic-gate 		goto again;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	case ECANCELED:
5610Sstevel@tonic-gate 		inst->ri_mi_deleted = 1;
5620Sstevel@tonic-gate 		break;
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	case EPERM:
5650Sstevel@tonic-gate 	case EACCES:
5660Sstevel@tonic-gate 	case EROFS:
5670Sstevel@tonic-gate 		log_framework(LOG_INFO, "Could not write exit status "
5680Sstevel@tonic-gate 		    "for %s method of %s: %s.\n", mname,
5690Sstevel@tonic-gate 		    inst->ri_i.i_fmri, strerror(r));
5700Sstevel@tonic-gate 		break;
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	case ENAMETOOLONG:
5730Sstevel@tonic-gate 	default:
5740Sstevel@tonic-gate 		bad_error("libscf_write_method_status", r);
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate }
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate /*
5790Sstevel@tonic-gate  * int method_run()
5800Sstevel@tonic-gate  *   Execute the type method of instp.  If it requires a fork(), wait for it
5810Sstevel@tonic-gate  *   to return and return its exit code in *exit_code.  Otherwise set
5820Sstevel@tonic-gate  *   *exit_code to 0 if the method succeeds & -1 if it fails.  If the
5830Sstevel@tonic-gate  *   repository connection is broken, it is rebound, but inst may not be
5840Sstevel@tonic-gate  *   reset.
5850Sstevel@tonic-gate  *   Returns
5860Sstevel@tonic-gate  *     0 - success
5870Sstevel@tonic-gate  *     EINVAL - A correct method or method context couldn't be retrieved.
5880Sstevel@tonic-gate  *     EIO - Contract kill failed.
5890Sstevel@tonic-gate  *     EFAULT - Method couldn't be executed successfully.
5900Sstevel@tonic-gate  *     ELOOP - Retry threshold exceeded.
5910Sstevel@tonic-gate  *     ECANCELED - inst was deleted from the repository before method was run
5920Sstevel@tonic-gate  *     ERANGE - Timeout retry threshold exceeded.
5930Sstevel@tonic-gate  *     EAGAIN - Failed due to external cause, retry.
5940Sstevel@tonic-gate  */
5950Sstevel@tonic-gate int
5960Sstevel@tonic-gate method_run(restarter_inst_t **instp, int type, int *exit_code)
5970Sstevel@tonic-gate {
5980Sstevel@tonic-gate 	char *method;
5990Sstevel@tonic-gate 	int ret_status;
6000Sstevel@tonic-gate 	pid_t pid;
6010Sstevel@tonic-gate 	method_restart_t restart_on;
6020Sstevel@tonic-gate 	uint_t cte_mask;
6030Sstevel@tonic-gate 	uint8_t need_session;
6040Sstevel@tonic-gate 	scf_handle_t *h;
6050Sstevel@tonic-gate 	scf_snapshot_t *snap;
6060Sstevel@tonic-gate 	const char *mname;
607*9765SSean.Wilcox@Sun.COM 	mc_error_t *m_error;
6080Sstevel@tonic-gate 	struct method_context *mcp;
6090Sstevel@tonic-gate 	int result = 0, timeout_fired = 0;
6100Sstevel@tonic-gate 	int sig, r;
6110Sstevel@tonic-gate 	boolean_t transient;
6120Sstevel@tonic-gate 	uint64_t timeout;
6130Sstevel@tonic-gate 	uint8_t timeout_retry;
6140Sstevel@tonic-gate 	ctid_t ctid;
6150Sstevel@tonic-gate 	int ctfd = -1;
6160Sstevel@tonic-gate 	restarter_inst_t *inst = *instp;
6170Sstevel@tonic-gate 	int id = inst->ri_id;
618119Ssl108498 	int forkerr;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	assert(PTHREAD_MUTEX_HELD(&inst->ri_lock));
6210Sstevel@tonic-gate 	assert(instance_in_transition(inst));
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	if (inst->ri_mi_deleted)
6240Sstevel@tonic-gate 		return (ECANCELED);
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	*exit_code = 0;
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	assert(0 <= type && type <= 2);
6290Sstevel@tonic-gate 	mname = method_names[type];
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate 	if (type == METHOD_START)
6320Sstevel@tonic-gate 		inst->ri_pre_online_hook();
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	h = scf_instance_handle(inst->ri_m_inst);
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	snap = scf_snapshot_create(h);
6370Sstevel@tonic-gate 	if (snap == NULL ||
6380Sstevel@tonic-gate 	    scf_instance_get_snapshot(inst->ri_m_inst, "running", snap) != 0) {
6390Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
6400Sstevel@tonic-gate 		    "Could not get running snapshot for %s.  "
6410Sstevel@tonic-gate 		    "Using editing version to run method %s.\n",
6420Sstevel@tonic-gate 		    inst->ri_i.i_fmri, mname);
6430Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
6440Sstevel@tonic-gate 		snap = NULL;
6450Sstevel@tonic-gate 	}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	/*
6480Sstevel@tonic-gate 	 * After this point, we may be logging to the instance log.
6490Sstevel@tonic-gate 	 * Make sure we've noted where that log is as a property of
6500Sstevel@tonic-gate 	 * the instance.
6510Sstevel@tonic-gate 	 */
6520Sstevel@tonic-gate 	r = libscf_note_method_log(inst->ri_m_inst, st->st_log_prefix,
6530Sstevel@tonic-gate 	    inst->ri_logstem);
6540Sstevel@tonic-gate 	if (r != 0) {
6550Sstevel@tonic-gate 		log_framework(LOG_WARNING,
6560Sstevel@tonic-gate 		    "%s: couldn't note log location: %s\n",
6570Sstevel@tonic-gate 		    inst->ri_i.i_fmri, strerror(r));
6580Sstevel@tonic-gate 	}
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	if ((method = libscf_get_method(h, type, inst, snap, &restart_on,
6610Sstevel@tonic-gate 	    &cte_mask, &need_session, &timeout, &timeout_retry)) == NULL) {
6620Sstevel@tonic-gate 		if (errno == LIBSCF_PGROUP_ABSENT)  {
6630Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
6640Sstevel@tonic-gate 			    "%s: instance has no method property group '%s'.\n",
6650Sstevel@tonic-gate 			    inst->ri_i.i_fmri, mname);
6660Sstevel@tonic-gate 			if (type == METHOD_REFRESH)
6670Sstevel@tonic-gate 				log_instance(inst, B_TRUE, "No '%s' method "
6680Sstevel@tonic-gate 				    "defined.  Treating as :true.", mname);
6690Sstevel@tonic-gate 			else
6700Sstevel@tonic-gate 				log_instance(inst, B_TRUE, "Method property "
6710Sstevel@tonic-gate 				    "group '%s' is not present.", mname);
6720Sstevel@tonic-gate 			scf_snapshot_destroy(snap);
6730Sstevel@tonic-gate 			return (0);
6740Sstevel@tonic-gate 		} else if (errno == LIBSCF_PROPERTY_ABSENT)  {
6750Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
6760Sstevel@tonic-gate 			    "%s: instance has no '%s/exec' method property.\n",
6770Sstevel@tonic-gate 			    inst->ri_i.i_fmri, mname);
6780Sstevel@tonic-gate 			log_instance(inst, B_TRUE, "Method property '%s/exec "
6790Sstevel@tonic-gate 			    "is not present.", mname);
6800Sstevel@tonic-gate 			scf_snapshot_destroy(snap);
6810Sstevel@tonic-gate 			return (0);
6820Sstevel@tonic-gate 		} else {
6830Sstevel@tonic-gate 			log_error(LOG_WARNING,
6840Sstevel@tonic-gate 			    "%s: instance libscf_get_method failed\n",
6850Sstevel@tonic-gate 			    inst->ri_i.i_fmri);
6860Sstevel@tonic-gate 			scf_snapshot_destroy(snap);
6870Sstevel@tonic-gate 			return (EINVAL);
6880Sstevel@tonic-gate 		}
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	/* open service contract if stopping a non-transient service */
6920Sstevel@tonic-gate 	if (type == METHOD_STOP && (!instance_is_transient_style(inst))) {
6930Sstevel@tonic-gate 		if (inst->ri_i.i_primary_ctid == 0) {
6940Sstevel@tonic-gate 			/* service is not running, nothing to stop */
6950Sstevel@tonic-gate 			log_framework(LOG_DEBUG, "%s: instance has no primary "
6960Sstevel@tonic-gate 			    "contract, no service to stop.\n",
6970Sstevel@tonic-gate 			    inst->ri_i.i_fmri);
6980Sstevel@tonic-gate 			scf_snapshot_destroy(snap);
6990Sstevel@tonic-gate 			return (0);
7000Sstevel@tonic-gate 		}
7010Sstevel@tonic-gate 		if ((ctfd = contract_open(inst->ri_i.i_primary_ctid, "process",
7020Sstevel@tonic-gate 		    "events", O_RDONLY)) < 0) {
7030Sstevel@tonic-gate 			result = EFAULT;
7040Sstevel@tonic-gate 			log_instance(inst, B_TRUE, "Could not open service "
7055238Slianep 			    "contract %ld.  Stop method not run.",
7060Sstevel@tonic-gate 			    inst->ri_i.i_primary_ctid);
7070Sstevel@tonic-gate 			goto out;
7080Sstevel@tonic-gate 		}
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	if (restarter_is_null_method(method)) {
7120Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "%s: null method succeeds\n",
7130Sstevel@tonic-gate 		    inst->ri_i.i_fmri);
7140Sstevel@tonic-gate 
7155238Slianep 		log_instance(inst, B_TRUE, "Executing %s method (null).",
7165238Slianep 		    mname);
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 		if (type == METHOD_START)
7190Sstevel@tonic-gate 			write_status(inst, mname, 0);
7200Sstevel@tonic-gate 		goto out;
7210Sstevel@tonic-gate 	}
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	sig = restarter_is_kill_method(method);
7240Sstevel@tonic-gate 	if (sig >= 0) {
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 		if (inst->ri_i.i_primary_ctid == 0) {
7270Sstevel@tonic-gate 			log_error(LOG_ERR, "%s: :kill with no contract\n",
7280Sstevel@tonic-gate 			    inst->ri_i.i_fmri);
7295238Slianep 			log_instance(inst, B_TRUE, "Invalid use of \":kill\" "
7305238Slianep 			    "as stop method for transient service.");
7310Sstevel@tonic-gate 			result = EINVAL;
7320Sstevel@tonic-gate 			goto out;
7330Sstevel@tonic-gate 		}
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
7360Sstevel@tonic-gate 		    "%s: :killing contract with signal %d\n",
7370Sstevel@tonic-gate 		    inst->ri_i.i_fmri, sig);
7380Sstevel@tonic-gate 
7395238Slianep 		log_instance(inst, B_TRUE, "Executing %s method (:kill).",
7400Sstevel@tonic-gate 		    mname);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 		if (contract_kill(inst->ri_i.i_primary_ctid, sig,
7430Sstevel@tonic-gate 		    inst->ri_i.i_fmri) != 0) {
7440Sstevel@tonic-gate 			result = EIO;
7450Sstevel@tonic-gate 			goto out;
7460Sstevel@tonic-gate 		} else
7470Sstevel@tonic-gate 			goto assured_kill;
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "%s: forking to run method %s\n",
7510Sstevel@tonic-gate 	    inst->ri_i.i_fmri, method);
7520Sstevel@tonic-gate 
753*9765SSean.Wilcox@Sun.COM 	m_error = restarter_get_method_context(RESTARTER_METHOD_CONTEXT_VERSION,
7540Sstevel@tonic-gate 	    inst->ri_m_inst, snap, mname, method, &mcp);
7550Sstevel@tonic-gate 
756*9765SSean.Wilcox@Sun.COM 	if (m_error != NULL) {
757*9765SSean.Wilcox@Sun.COM 		log_instance(inst, B_TRUE, "%s", m_error->msg);
758*9765SSean.Wilcox@Sun.COM 		restarter_mc_error_destroy(m_error);
7590Sstevel@tonic-gate 		result = EINVAL;
7600Sstevel@tonic-gate 		goto out;
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	r = method_ready_contract(inst, type, restart_on, cte_mask);
7640Sstevel@tonic-gate 	if (r != 0) {
7650Sstevel@tonic-gate 		assert(r == ECANCELED);
7660Sstevel@tonic-gate 		assert(inst->ri_mi_deleted);
7670Sstevel@tonic-gate 		restarter_free_method_context(mcp);
7680Sstevel@tonic-gate 		result = ECANCELED;
7690Sstevel@tonic-gate 		goto out;
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	/*
7730Sstevel@tonic-gate 	 * Validate safety of method contexts, to save children work.
7740Sstevel@tonic-gate 	 */
7750Sstevel@tonic-gate 	if (!restarter_rm_libs_loadable())
7760Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "%s: method contexts limited "
7770Sstevel@tonic-gate 		    "to root-accessible libraries\n", inst->ri_i.i_fmri);
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	/*
7800Sstevel@tonic-gate 	 * If the service is restarting too quickly, send it to
7810Sstevel@tonic-gate 	 * maintenance.
7820Sstevel@tonic-gate 	 */
7830Sstevel@tonic-gate 	if (type == METHOD_START) {
7840Sstevel@tonic-gate 		method_record_start(inst);
7850Sstevel@tonic-gate 		if (method_rate_critical(inst)) {
7860Sstevel@tonic-gate 			log_instance(inst, B_TRUE, "Restarting too quickly, "
7875238Slianep 			    "changing state to maintenance.");
7880Sstevel@tonic-gate 			result = ELOOP;
7893179Sjeanm 			restarter_free_method_context(mcp);
7900Sstevel@tonic-gate 			goto out;
7910Sstevel@tonic-gate 		}
7920Sstevel@tonic-gate 	}
7930Sstevel@tonic-gate 
7944244Sjeanm 	atomic_add_16(&storing_contract, 1);
795119Ssl108498 	pid = startd_fork1(&forkerr);
7960Sstevel@tonic-gate 	if (pid == 0)
7970Sstevel@tonic-gate 		exec_method(inst, type, method, mcp, need_session);
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	if (pid == -1) {
8004244Sjeanm 		atomic_add_16(&storing_contract, -1);
801119Ssl108498 		if (forkerr == EAGAIN)
802119Ssl108498 			result = EAGAIN;
803119Ssl108498 		else
804119Ssl108498 			result = EFAULT;
805119Ssl108498 
8060Sstevel@tonic-gate 		log_error(LOG_WARNING,
807119Ssl108498 		    "%s: Couldn't fork to execute method %s: %s\n",
808119Ssl108498 		    inst->ri_i.i_fmri, method, strerror(forkerr));
809119Ssl108498 
8104244Sjeanm 		restarter_free_method_context(mcp);
8110Sstevel@tonic-gate 		goto out;
8120Sstevel@tonic-gate 	}
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	/*
8160Sstevel@tonic-gate 	 * Get the contract id, decide whether it is primary or transient, and
8170Sstevel@tonic-gate 	 * stash it in inst & the repository.
8180Sstevel@tonic-gate 	 */
8190Sstevel@tonic-gate 	method_store_contract(inst, type, &ctid);
8204244Sjeanm 	atomic_add_16(&storing_contract, -1);
8214244Sjeanm 
8224244Sjeanm 	restarter_free_method_context(mcp);
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	/*
8250Sstevel@tonic-gate 	 * Similarly for the start method PID.
8260Sstevel@tonic-gate 	 */
8270Sstevel@tonic-gate 	if (type == METHOD_START && !inst->ri_mi_deleted)
8280Sstevel@tonic-gate 		(void) libscf_write_start_pid(inst->ri_m_inst, pid);
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	if (instance_is_wait_style(inst) && type == METHOD_START) {
8310Sstevel@tonic-gate 		/* Wait style instances don't get timeouts on start methods. */
8320Sstevel@tonic-gate 		if (wait_register(pid, inst->ri_i.i_fmri, 1, 0)) {
8330Sstevel@tonic-gate 			log_error(LOG_WARNING,
8340Sstevel@tonic-gate 			    "%s: couldn't register %ld for wait\n",
8350Sstevel@tonic-gate 			    inst->ri_i.i_fmri, pid);
8360Sstevel@tonic-gate 			result = EFAULT;
8370Sstevel@tonic-gate 			goto contract_out;
8380Sstevel@tonic-gate 		}
8390Sstevel@tonic-gate 		write_status(inst, mname, 0);
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	} else {
8420Sstevel@tonic-gate 		int r, err;
8430Sstevel@tonic-gate 		time_t start_time;
8440Sstevel@tonic-gate 		time_t end_time;
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 		/*
8470Sstevel@tonic-gate 		 * Because on upgrade/live-upgrade we may have no chance
8480Sstevel@tonic-gate 		 * to override faulty timeout values on the way to
8490Sstevel@tonic-gate 		 * manifest import, all services on the path to manifest
8500Sstevel@tonic-gate 		 * import are treated the same as INFINITE timeout services.
8510Sstevel@tonic-gate 		 */
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 		start_time = time(NULL);
8540Sstevel@tonic-gate 		if (timeout != METHOD_TIMEOUT_INFINITE && !is_timeout_ovr(inst))
8550Sstevel@tonic-gate 			timeout_insert(inst, ctid, timeout);
8560Sstevel@tonic-gate 		else
8570Sstevel@tonic-gate 			timeout = METHOD_TIMEOUT_INFINITE;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 		/* Unlock the instance while waiting for the method. */
8600Sstevel@tonic-gate 		MUTEX_UNLOCK(&inst->ri_lock);
8610Sstevel@tonic-gate 
8624816Sacruz 		do {
8630Sstevel@tonic-gate 			r = waitpid(pid, &ret_status, NULL);
8644816Sacruz 		} while (r == -1 && errno == EINTR);
8650Sstevel@tonic-gate 		if (r == -1)
8660Sstevel@tonic-gate 			err = errno;
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 		/* Re-grab the lock. */
8690Sstevel@tonic-gate 		inst = inst_lookup_by_id(id);
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 		/*
8720Sstevel@tonic-gate 		 * inst can't be removed, as the removal thread waits
8730Sstevel@tonic-gate 		 * for completion of this one.
8740Sstevel@tonic-gate 		 */
8750Sstevel@tonic-gate 		assert(inst != NULL);
8760Sstevel@tonic-gate 		*instp = inst;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 		if (inst->ri_timeout != NULL && inst->ri_timeout->te_fired)
8790Sstevel@tonic-gate 			timeout_fired = 1;
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 		timeout_remove(inst, ctid);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
8840Sstevel@tonic-gate 		    "%s method for %s exited with status %d.\n", mname,
8850Sstevel@tonic-gate 		    inst->ri_i.i_fmri, WEXITSTATUS(ret_status));
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 		if (r == -1) {
8880Sstevel@tonic-gate 			log_error(LOG_WARNING,
8890Sstevel@tonic-gate 			    "Couldn't waitpid() for %s method of %s (%s).\n",
8900Sstevel@tonic-gate 			    mname, inst->ri_i.i_fmri, strerror(err));
8910Sstevel@tonic-gate 			result = EFAULT;
8920Sstevel@tonic-gate 			goto contract_out;
8930Sstevel@tonic-gate 		}
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 		if (type == METHOD_START)
8960Sstevel@tonic-gate 			write_status(inst, mname, ret_status);
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 		/* return ERANGE if this service doesn't retry on timeout */
8990Sstevel@tonic-gate 		if (timeout_fired == 1 && timeout_retry == 0) {
9000Sstevel@tonic-gate 			result = ERANGE;
9010Sstevel@tonic-gate 			goto contract_out;
9020Sstevel@tonic-gate 		}
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 		if (!WIFEXITED(ret_status)) {
9050Sstevel@tonic-gate 			/*
9060Sstevel@tonic-gate 			 * If method didn't exit itself (it was killed by an
9070Sstevel@tonic-gate 			 * external entity, etc.), consider the entire
9080Sstevel@tonic-gate 			 * method_run as failed.
9090Sstevel@tonic-gate 			 */
9100Sstevel@tonic-gate 			if (WIFSIGNALED(ret_status)) {
9110Sstevel@tonic-gate 				char buf[SIG2STR_MAX];
9120Sstevel@tonic-gate 				(void) sig2str(WTERMSIG(ret_status), buf);
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 				log_error(LOG_WARNING, "%s: Method \"%s\" "
9150Sstevel@tonic-gate 				    "failed due to signal %s.\n",
9160Sstevel@tonic-gate 				    inst->ri_i.i_fmri, method, buf);
9170Sstevel@tonic-gate 				log_instance(inst, B_TRUE, "Method \"%s\" "
9185238Slianep 				    "failed due to signal %s.", mname, buf);
9190Sstevel@tonic-gate 			} else {
9200Sstevel@tonic-gate 				log_error(LOG_WARNING, "%s: Method \"%s\" "
9210Sstevel@tonic-gate 				    "failed with exit status %d.\n",
9220Sstevel@tonic-gate 				    inst->ri_i.i_fmri, method,
9230Sstevel@tonic-gate 				    WEXITSTATUS(ret_status));
9240Sstevel@tonic-gate 				log_instance(inst, B_TRUE, "Method \"%s\" "
9255238Slianep 				    "failed with exit status %d.", mname,
9260Sstevel@tonic-gate 				    WEXITSTATUS(ret_status));
9270Sstevel@tonic-gate 			}
9280Sstevel@tonic-gate 			result = EAGAIN;
9290Sstevel@tonic-gate 			goto contract_out;
9300Sstevel@tonic-gate 		}
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 		*exit_code = WEXITSTATUS(ret_status);
9330Sstevel@tonic-gate 		if (*exit_code != 0) {
9340Sstevel@tonic-gate 			log_error(LOG_WARNING,
9350Sstevel@tonic-gate 			    "%s: Method \"%s\" failed with exit status %d.\n",
9360Sstevel@tonic-gate 			    inst->ri_i.i_fmri, method, WEXITSTATUS(ret_status));
9370Sstevel@tonic-gate 		}
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 		log_instance(inst, B_TRUE, "Method \"%s\" exited with status "
9405238Slianep 		    "%d.", mname, *exit_code);
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 		if (*exit_code != 0)
9430Sstevel@tonic-gate 			goto contract_out;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 		end_time = time(NULL);
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 		/* Give service contract remaining seconds to empty */
9480Sstevel@tonic-gate 		if (timeout != METHOD_TIMEOUT_INFINITE)
9490Sstevel@tonic-gate 			timeout -= (end_time - start_time);
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate assured_kill:
9530Sstevel@tonic-gate 	/*
9540Sstevel@tonic-gate 	 * For stop methods, assure that the service contract has emptied
9550Sstevel@tonic-gate 	 * before returning.
9560Sstevel@tonic-gate 	 */
9570Sstevel@tonic-gate 	if (type == METHOD_STOP && (!instance_is_transient_style(inst)) &&
9580Sstevel@tonic-gate 	    !(contract_is_empty(inst->ri_i.i_primary_ctid))) {
9598944Sdp@eng.sun.com 		int times = 0;
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 		if (timeout != METHOD_TIMEOUT_INFINITE)
9620Sstevel@tonic-gate 			timeout_insert(inst, inst->ri_i.i_primary_ctid,
9630Sstevel@tonic-gate 			    timeout);
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 		for (;;) {
9668944Sdp@eng.sun.com 			/*
9678944Sdp@eng.sun.com 			 * Check frequently at first, then back off.  This
9688944Sdp@eng.sun.com 			 * keeps startd from idling while shutting down.
9698944Sdp@eng.sun.com 			 */
9708944Sdp@eng.sun.com 			if (times < 20) {
9718944Sdp@eng.sun.com 				(void) poll(NULL, 0, 5);
9728944Sdp@eng.sun.com 				times++;
9738944Sdp@eng.sun.com 			} else {
9748944Sdp@eng.sun.com 				(void) poll(NULL, 0, 100);
9758944Sdp@eng.sun.com 			}
9764244Sjeanm 			if (contract_is_empty(inst->ri_i.i_primary_ctid))
9770Sstevel@tonic-gate 				break;
9780Sstevel@tonic-gate 		}
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 		if (timeout != METHOD_TIMEOUT_INFINITE)
9810Sstevel@tonic-gate 			if (inst->ri_timeout->te_fired)
9820Sstevel@tonic-gate 				result = EFAULT;
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 		timeout_remove(inst, inst->ri_i.i_primary_ctid);
9850Sstevel@tonic-gate 	}
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate contract_out:
9880Sstevel@tonic-gate 	/* Abandon contracts for transient methods & methods that fail. */
9890Sstevel@tonic-gate 	transient = method_is_transient(inst, type);
9900Sstevel@tonic-gate 	if ((transient || *exit_code != 0 || result != 0) &&
9910Sstevel@tonic-gate 	    (restarter_is_kill_method(method) < 0))
9920Sstevel@tonic-gate 		method_remove_contract(inst, !transient, B_TRUE);
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate out:
9950Sstevel@tonic-gate 	if (ctfd >= 0)
9960Sstevel@tonic-gate 		(void) close(ctfd);
9970Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
9980Sstevel@tonic-gate 	free(method);
9990Sstevel@tonic-gate 	return (result);
10000Sstevel@tonic-gate }
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate /*
10030Sstevel@tonic-gate  * The method thread executes a service method to effect a state transition.
10040Sstevel@tonic-gate  * The next_state of info->sf_id should be non-_NONE on entrance, and it will
10050Sstevel@tonic-gate  * be _NONE on exit (state will either be what next_state was (on success), or
10060Sstevel@tonic-gate  * it will be _MAINT (on error)).
10070Sstevel@tonic-gate  *
10080Sstevel@tonic-gate  * There are six classes of methods to consider: start & other (stop, refresh)
10090Sstevel@tonic-gate  * for each of "normal" services, wait services, and transient services.  For
10100Sstevel@tonic-gate  * each, the method must be fetched from the repository & executed.  fork()ed
10110Sstevel@tonic-gate  * methods must be waited on, except for the start method of wait services
10120Sstevel@tonic-gate  * (which must be registered with the wait subsystem via wait_register()).  If
10130Sstevel@tonic-gate  * the method succeeded (returned 0), then for start methods its contract
10140Sstevel@tonic-gate  * should be recorded as the primary contract for the service.  For other
10150Sstevel@tonic-gate  * methods, it should be abandoned.  If the method fails, then depending on
10160Sstevel@tonic-gate  * the failure, either the method should be reexecuted or the service should
10170Sstevel@tonic-gate  * be put into maintenance.  Either way the contract should be abandoned.
10180Sstevel@tonic-gate  */
10190Sstevel@tonic-gate void *
10200Sstevel@tonic-gate method_thread(void *arg)
10210Sstevel@tonic-gate {
10220Sstevel@tonic-gate 	fork_info_t *info = arg;
10230Sstevel@tonic-gate 	restarter_inst_t *inst;
10240Sstevel@tonic-gate 	scf_handle_t	*local_handle;
10250Sstevel@tonic-gate 	scf_instance_t	*s_inst = NULL;
10260Sstevel@tonic-gate 	int r, exit_code;
10270Sstevel@tonic-gate 	boolean_t retryable;
10280Sstevel@tonic-gate 	const char *aux;
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	assert(0 <= info->sf_method_type && info->sf_method_type <= 2);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	/* Get (and lock) the restarter_inst_t. */
10330Sstevel@tonic-gate 	inst = inst_lookup_by_id(info->sf_id);
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	assert(inst->ri_method_thread != 0);
10360Sstevel@tonic-gate 	assert(instance_in_transition(inst) == 1);
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	/*
10390Sstevel@tonic-gate 	 * We cannot leave this function with inst in transition, because
10400Sstevel@tonic-gate 	 * protocol.c withholds messages for inst otherwise.
10410Sstevel@tonic-gate 	 */
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "method_thread() running %s method for %s.\n",
10440Sstevel@tonic-gate 	    method_names[info->sf_method_type], inst->ri_i.i_fmri);
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate 	local_handle = libscf_handle_create_bound_loop();
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate rebind_retry:
10490Sstevel@tonic-gate 	/* get scf_instance_t */
10500Sstevel@tonic-gate 	switch (r = libscf_fmri_get_instance(local_handle, inst->ri_i.i_fmri,
10510Sstevel@tonic-gate 	    &s_inst)) {
10520Sstevel@tonic-gate 	case 0:
10530Sstevel@tonic-gate 		break;
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	case ECONNABORTED:
10560Sstevel@tonic-gate 		libscf_handle_rebind(local_handle);
10570Sstevel@tonic-gate 		goto rebind_retry;
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	case ENOENT:
10600Sstevel@tonic-gate 		/*
10610Sstevel@tonic-gate 		 * It's not there, but we need to call this so protocol.c
10620Sstevel@tonic-gate 		 * doesn't think it's in transition anymore.
10630Sstevel@tonic-gate 		 */
10640Sstevel@tonic-gate 		(void) restarter_instance_update_states(local_handle, inst,
10650Sstevel@tonic-gate 		    inst->ri_i.i_state, RESTARTER_STATE_NONE, RERR_NONE,
10660Sstevel@tonic-gate 		    NULL);
10670Sstevel@tonic-gate 		goto out;
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	case EINVAL:
10700Sstevel@tonic-gate 	case ENOTSUP:
10710Sstevel@tonic-gate 	default:
10720Sstevel@tonic-gate 		bad_error("libscf_fmri_get_instance", r);
10730Sstevel@tonic-gate 	}
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	inst->ri_m_inst = s_inst;
10760Sstevel@tonic-gate 	inst->ri_mi_deleted = B_FALSE;
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate retry:
10790Sstevel@tonic-gate 	if (info->sf_method_type == METHOD_START)
10800Sstevel@tonic-gate 		log_transition(inst, START_REQUESTED);
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	r = method_run(&inst, info->sf_method_type, &exit_code);
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	if (r == 0 && exit_code == 0) {
10850Sstevel@tonic-gate 		/* Success! */
10860Sstevel@tonic-gate 		assert(inst->ri_i.i_next_state != RESTARTER_STATE_NONE);
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 		/*
10890Sstevel@tonic-gate 		 * When a stop method succeeds, remove the primary contract of
10900Sstevel@tonic-gate 		 * the service, unless we're going to offline, in which case
10910Sstevel@tonic-gate 		 * retain the contract so we can transfer inherited contracts to
10920Sstevel@tonic-gate 		 * the replacement service.
10930Sstevel@tonic-gate 		 */
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 		if (info->sf_method_type == METHOD_STOP &&
10960Sstevel@tonic-gate 		    inst->ri_i.i_primary_ctid != 0) {
10970Sstevel@tonic-gate 			if (inst->ri_i.i_next_state == RESTARTER_STATE_OFFLINE)
10980Sstevel@tonic-gate 				inst->ri_i.i_primary_ctid_stopped = 1;
10990Sstevel@tonic-gate 			else
11000Sstevel@tonic-gate 				method_remove_contract(inst, B_TRUE, B_TRUE);
11010Sstevel@tonic-gate 		}
11020Sstevel@tonic-gate 		/*
11030Sstevel@tonic-gate 		 * We don't care whether the handle was rebound because this is
11040Sstevel@tonic-gate 		 * the last thing we do with it.
11050Sstevel@tonic-gate 		 */
11060Sstevel@tonic-gate 		(void) restarter_instance_update_states(local_handle, inst,
11070Sstevel@tonic-gate 		    inst->ri_i.i_next_state, RESTARTER_STATE_NONE,
11080Sstevel@tonic-gate 		    info->sf_event_type, NULL);
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 		(void) update_fault_count(inst, FAULT_COUNT_RESET);
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 		goto out;
11130Sstevel@tonic-gate 	}
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 	/* Failure.  Retry or go to maintenance. */
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	if (r != 0 && r != EAGAIN) {
11180Sstevel@tonic-gate 		retryable = B_FALSE;
11190Sstevel@tonic-gate 	} else {
11200Sstevel@tonic-gate 		switch (exit_code) {
11210Sstevel@tonic-gate 		case SMF_EXIT_ERR_CONFIG:
11220Sstevel@tonic-gate 		case SMF_EXIT_ERR_NOSMF:
11230Sstevel@tonic-gate 		case SMF_EXIT_ERR_PERM:
11240Sstevel@tonic-gate 		case SMF_EXIT_ERR_FATAL:
11250Sstevel@tonic-gate 			retryable = B_FALSE;
11260Sstevel@tonic-gate 			break;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 		default:
11290Sstevel@tonic-gate 			retryable = B_TRUE;
11300Sstevel@tonic-gate 		}
11310Sstevel@tonic-gate 	}
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	if (retryable && update_fault_count(inst, FAULT_COUNT_INCR) != 1)
11340Sstevel@tonic-gate 		goto retry;
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	/* maintenance */
11370Sstevel@tonic-gate 	if (r == ELOOP)
11380Sstevel@tonic-gate 		log_transition(inst, START_FAILED_REPEATEDLY);
11390Sstevel@tonic-gate 	else if (r == ERANGE)
11400Sstevel@tonic-gate 		log_transition(inst, START_FAILED_TIMEOUT_FATAL);
11410Sstevel@tonic-gate 	else if (exit_code == SMF_EXIT_ERR_CONFIG)
11420Sstevel@tonic-gate 		log_transition(inst, START_FAILED_CONFIGURATION);
11430Sstevel@tonic-gate 	else if (exit_code == SMF_EXIT_ERR_FATAL)
11440Sstevel@tonic-gate 		log_transition(inst, START_FAILED_FATAL);
11450Sstevel@tonic-gate 	else
11460Sstevel@tonic-gate 		log_transition(inst, START_FAILED_OTHER);
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	if (r == ELOOP)
11490Sstevel@tonic-gate 		aux = "restarting_too_quickly";
11500Sstevel@tonic-gate 	else if (retryable)
11510Sstevel@tonic-gate 		aux = "fault_threshold_reached";
11520Sstevel@tonic-gate 	else
11530Sstevel@tonic-gate 		aux = "method_failed";
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	(void) restarter_instance_update_states(local_handle, inst,
11560Sstevel@tonic-gate 	    RESTARTER_STATE_MAINT, RESTARTER_STATE_NONE, RERR_FAULT,
11570Sstevel@tonic-gate 	    (char *)aux);
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	if (!method_is_transient(inst, info->sf_method_type) &&
11600Sstevel@tonic-gate 	    inst->ri_i.i_primary_ctid != 0)
11610Sstevel@tonic-gate 		method_remove_contract(inst, B_TRUE, B_TRUE);
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate out:
11640Sstevel@tonic-gate 	inst->ri_method_thread = 0;
11656748Srm88369 
11666748Srm88369 	/*
11676748Srm88369 	 * Unlock the mutex after broadcasting to avoid a race condition
11686748Srm88369 	 * with restarter_delete_inst() when the 'inst' structure is freed.
11696748Srm88369 	 */
11706748Srm88369 	(void) pthread_cond_broadcast(&inst->ri_method_cv);
11710Sstevel@tonic-gate 	MUTEX_UNLOCK(&inst->ri_lock);
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	scf_instance_destroy(s_inst);
11740Sstevel@tonic-gate 	scf_handle_destroy(local_handle);
11750Sstevel@tonic-gate 	startd_free(info, sizeof (fork_info_t));
11760Sstevel@tonic-gate 	return (NULL);
11770Sstevel@tonic-gate }
1178