xref: /illumos-gate/usr/src/cmd/svc/startd/method.c (revision 5f8fc372)
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
53ad28c1eSrm  * Common Development and Distribution License (the "License").
63ad28c1eSrm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2113d8aaa1SSean Wilcox 
227c478bd9Sstevel@tonic-gate /*
23f6e214c7SGavin Maltby  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24ab618543SJohn Levon  * Copyright 2018 Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * method.c - method execution functions
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * This file contains the routines needed to run a method:  a fork(2)-exec(2)
317c478bd9Sstevel@tonic-gate  * invocation monitored using either the contract filesystem or waitpid(2).
327c478bd9Sstevel@tonic-gate  * (Plain fork1(2) support is provided in fork.c.)
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * Contract Transfer
357c478bd9Sstevel@tonic-gate  *   When we restart a service, we want to transfer any contracts that the old
367c478bd9Sstevel@tonic-gate  *   service's contract inherited.  This means that (a) we must not abandon the
377c478bd9Sstevel@tonic-gate  *   old contract when the service dies and (b) we must write the id of the old
387c478bd9Sstevel@tonic-gate  *   contract into the terms of the new contract.  There should be limits to
397c478bd9Sstevel@tonic-gate  *   (a), though, since we don't want to keep the contract around forever.  To
407c478bd9Sstevel@tonic-gate  *   this end we'll say that services in the offline state may have a contract
417c478bd9Sstevel@tonic-gate  *   to be transfered and services in the disabled or maintenance states cannot.
427c478bd9Sstevel@tonic-gate  *   This means that when a service transitions from online (or degraded) to
437c478bd9Sstevel@tonic-gate  *   offline, the contract should be preserved, and when the service transitions
447c478bd9Sstevel@tonic-gate  *   from offline to online (i.e., the start method), we'll transfer inherited
457c478bd9Sstevel@tonic-gate  *   contracts.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <sys/contract/process.h>
497c478bd9Sstevel@tonic-gate #include <sys/ctfs.h>
507c478bd9Sstevel@tonic-gate #include <sys/stat.h>
517c478bd9Sstevel@tonic-gate #include <sys/time.h>
527c478bd9Sstevel@tonic-gate #include <sys/types.h>
537c478bd9Sstevel@tonic-gate #include <sys/uio.h>
547c478bd9Sstevel@tonic-gate #include <sys/wait.h>
557c478bd9Sstevel@tonic-gate #include <alloca.h>
567c478bd9Sstevel@tonic-gate #include <assert.h>
577c478bd9Sstevel@tonic-gate #include <errno.h>
587c478bd9Sstevel@tonic-gate #include <fcntl.h>
597c478bd9Sstevel@tonic-gate #include <libcontract.h>
607c478bd9Sstevel@tonic-gate #include <libcontract_priv.h>
617c478bd9Sstevel@tonic-gate #include <libgen.h>
627c478bd9Sstevel@tonic-gate #include <librestart.h>
637c478bd9Sstevel@tonic-gate #include <libscf.h>
647c478bd9Sstevel@tonic-gate #include <limits.h>
657c478bd9Sstevel@tonic-gate #include <port.h>
667c478bd9Sstevel@tonic-gate #include <sac.h>
677c478bd9Sstevel@tonic-gate #include <signal.h>
687c478bd9Sstevel@tonic-gate #include <stdlib.h>
697c478bd9Sstevel@tonic-gate #include <string.h>
707c478bd9Sstevel@tonic-gate #include <strings.h>
717c478bd9Sstevel@tonic-gate #include <unistd.h>
72dfe57350Sjeanm #include <atomic.h>
73dfe57350Sjeanm #include <poll.h>
7422b8cfe6SJohn Sonnenschein #include <libscf_priv.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #include "startd.h"
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	SBIN_SH		"/sbin/sh"
797c478bd9Sstevel@tonic-gate 
80dfe57350Sjeanm /*
81dfe57350Sjeanm  * Used to tell if contracts are in the process of being
82dfe57350Sjeanm  * stored into the svc.startd internal hash table.
83dfe57350Sjeanm  */
84dfe57350Sjeanm volatile uint16_t	storing_contract = 0;
85dfe57350Sjeanm 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * Mapping from restart_on method-type to contract events.  Must correspond to
887c478bd9Sstevel@tonic-gate  * enum method_restart_t.
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate static uint_t method_events[] = {
917c478bd9Sstevel@tonic-gate 	/* METHOD_RESTART_ALL */
927c478bd9Sstevel@tonic-gate 	CT_PR_EV_HWERR | CT_PR_EV_SIGNAL | CT_PR_EV_CORE | CT_PR_EV_EMPTY,
937c478bd9Sstevel@tonic-gate 	/* METHOD_RESTART_EXTERNAL_FAULT */
947c478bd9Sstevel@tonic-gate 	CT_PR_EV_HWERR | CT_PR_EV_SIGNAL,
957c478bd9Sstevel@tonic-gate 	/* METHOD_RESTART_ANY_FAULT */
967c478bd9Sstevel@tonic-gate 	CT_PR_EV_HWERR | CT_PR_EV_SIGNAL | CT_PR_EV_CORE
977c478bd9Sstevel@tonic-gate };
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * method_record_start(restarter_inst_t *)
1017c478bd9Sstevel@tonic-gate  *   Record a service start for rate limiting.  Place the current time
1027c478bd9Sstevel@tonic-gate  *   in the circular array of instance starts.
1037c478bd9Sstevel@tonic-gate  */
1047c478bd9Sstevel@tonic-gate static void
method_record_start(restarter_inst_t * inst)1057c478bd9Sstevel@tonic-gate method_record_start(restarter_inst_t *inst)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate 	int index = inst->ri_start_index++ % RINST_START_TIMES;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	inst->ri_start_time[index] = gethrtime();
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * method_rate_critical(restarter_inst_t *)
1147c478bd9Sstevel@tonic-gate  *    Return true if the average start interval is less than the permitted
11522b8cfe6SJohn Sonnenschein  *    interval.  The implicit interval defaults to RINST_FAILURE_RATE_NS and
11622b8cfe6SJohn Sonnenschein  *    RINST_START_TIMES but may be overridden with the svc properties
11722b8cfe6SJohn Sonnenschein  *    startd/critical_failure_count and startd/critical_failure_period
11822b8cfe6SJohn Sonnenschein  *    which represent the number of failures to consider and the amount of
11922b8cfe6SJohn Sonnenschein  *    time in seconds in which that number may occur, respectively. Note that
12022b8cfe6SJohn Sonnenschein  *    this time is measured as of the transition to 'enabled' rather than wall
12122b8cfe6SJohn Sonnenschein  *    clock time.
12222b8cfe6SJohn Sonnenschein  *    Implicit success if insufficient measurements for an average exist.
1237c478bd9Sstevel@tonic-gate  */
1242a17138dSJerry Jelinek int
method_rate_critical(restarter_inst_t * inst)1257c478bd9Sstevel@tonic-gate method_rate_critical(restarter_inst_t *inst)
1267c478bd9Sstevel@tonic-gate {
1272a17138dSJerry Jelinek 	hrtime_t critical_failure_period;
12822b8cfe6SJohn Sonnenschein 	uint_t critical_failure_count = RINST_START_TIMES;
1297c478bd9Sstevel@tonic-gate 	uint_t n = inst->ri_start_index;
1307c478bd9Sstevel@tonic-gate 	hrtime_t avg_ns = 0;
13122b8cfe6SJohn Sonnenschein 	uint64_t scf_fr, scf_st;
13222b8cfe6SJohn Sonnenschein 	scf_propvec_t *prop = NULL;
13322b8cfe6SJohn Sonnenschein 	scf_propvec_t restart_critical[] = {
13422b8cfe6SJohn Sonnenschein 		{ "critical_failure_period", NULL, SCF_TYPE_INTEGER, NULL, 0 },
13522b8cfe6SJohn Sonnenschein 		{ "critical_failure_count", NULL, SCF_TYPE_INTEGER, NULL, 0 },
13622b8cfe6SJohn Sonnenschein 		{ NULL }
13722b8cfe6SJohn Sonnenschein 	};
1387c478bd9Sstevel@tonic-gate 
1392a17138dSJerry Jelinek 	if (instance_is_wait_style(inst))
1402a17138dSJerry Jelinek 		critical_failure_period = RINST_WT_SVC_FAILURE_RATE_NS;
1412a17138dSJerry Jelinek 	else
1422a17138dSJerry Jelinek 		critical_failure_period = RINST_FAILURE_RATE_NS;
1432a17138dSJerry Jelinek 
14422b8cfe6SJohn Sonnenschein 	restart_critical[0].pv_ptr = &scf_fr;
14522b8cfe6SJohn Sonnenschein 	restart_critical[1].pv_ptr = &scf_st;
14622b8cfe6SJohn Sonnenschein 
14722b8cfe6SJohn Sonnenschein 	if (scf_read_propvec(inst->ri_i.i_fmri, "startd",
14822b8cfe6SJohn Sonnenschein 	    B_TRUE, restart_critical, &prop) != SCF_FAILED) {
14922b8cfe6SJohn Sonnenschein 		/*
15022b8cfe6SJohn Sonnenschein 		 * critical_failure_period is expressed
15122b8cfe6SJohn Sonnenschein 		 * in seconds but tracked in ns
15222b8cfe6SJohn Sonnenschein 		 */
15322b8cfe6SJohn Sonnenschein 		critical_failure_period = (hrtime_t)scf_fr * NANOSEC;
15422b8cfe6SJohn Sonnenschein 		critical_failure_count = (uint_t)scf_st;
15522b8cfe6SJohn Sonnenschein 	}
15622b8cfe6SJohn Sonnenschein 	if (inst->ri_start_index < critical_failure_count)
1577c478bd9Sstevel@tonic-gate 		return (0);
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	avg_ns =
16022b8cfe6SJohn Sonnenschein 	    (inst->ri_start_time[(n - 1) % critical_failure_count] -
16122b8cfe6SJohn Sonnenschein 	    inst->ri_start_time[n % critical_failure_count]) /
16222b8cfe6SJohn Sonnenschein 	    (critical_failure_count - 1);
1637c478bd9Sstevel@tonic-gate 
16422b8cfe6SJohn Sonnenschein 	return (avg_ns < critical_failure_period);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * int method_is_transient()
1697c478bd9Sstevel@tonic-gate  *   Determine if the method for the given instance is transient,
1707c478bd9Sstevel@tonic-gate  *   from a contract perspective. Return 1 if it is, and 0 if it isn't.
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate static int
method_is_transient(restarter_inst_t * inst,int type)1737c478bd9Sstevel@tonic-gate method_is_transient(restarter_inst_t *inst, int type)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	if (instance_is_transient_style(inst) || type != METHOD_START)
1767c478bd9Sstevel@tonic-gate 		return (1);
1777c478bd9Sstevel@tonic-gate 	else
1787c478bd9Sstevel@tonic-gate 		return (0);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * void method_store_contract()
1837c478bd9Sstevel@tonic-gate  *   Store the newly created contract id into local structures and
1847c478bd9Sstevel@tonic-gate  *   the repository.  If the repository connection is broken it is rebound.
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate static void
method_store_contract(restarter_inst_t * inst,int type,ctid_t * cid)1877c478bd9Sstevel@tonic-gate method_store_contract(restarter_inst_t *inst, int type, ctid_t *cid)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	int r;
1907c478bd9Sstevel@tonic-gate 	boolean_t primary;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (errno = contract_latest(cid))
1937c478bd9Sstevel@tonic-gate 		uu_die("%s: Couldn't get new contract's id", inst->ri_i.i_fmri);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	primary = !method_is_transient(inst, type);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (!primary) {
1987c478bd9Sstevel@tonic-gate 		if (inst->ri_i.i_transient_ctid != 0) {
1997c478bd9Sstevel@tonic-gate 			log_framework(LOG_INFO,
2007c478bd9Sstevel@tonic-gate 			    "%s: transient ctid expected to be 0 but "
2017c478bd9Sstevel@tonic-gate 			    "was set to %ld\n", inst->ri_i.i_fmri,
2027c478bd9Sstevel@tonic-gate 			    inst->ri_i.i_transient_ctid);
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		inst->ri_i.i_transient_ctid = *cid;
2067c478bd9Sstevel@tonic-gate 	} else {
2077c478bd9Sstevel@tonic-gate 		if (inst->ri_i.i_primary_ctid != 0) {
2087c478bd9Sstevel@tonic-gate 			/*
2097c478bd9Sstevel@tonic-gate 			 * There was an old contract that we transferred.
2107c478bd9Sstevel@tonic-gate 			 * Remove it.
2117c478bd9Sstevel@tonic-gate 			 */
2127c478bd9Sstevel@tonic-gate 			method_remove_contract(inst, B_TRUE, B_FALSE);
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		if (inst->ri_i.i_primary_ctid != 0) {
2167c478bd9Sstevel@tonic-gate 			log_framework(LOG_INFO,
2177c478bd9Sstevel@tonic-gate 			    "%s: primary ctid expected to be 0 but "
2187c478bd9Sstevel@tonic-gate 			    "was set to %ld\n", inst->ri_i.i_fmri,
2197c478bd9Sstevel@tonic-gate 			    inst->ri_i.i_primary_ctid);
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		inst->ri_i.i_primary_ctid = *cid;
2237c478bd9Sstevel@tonic-gate 		inst->ri_i.i_primary_ctid_stopped = 0;
2247c478bd9Sstevel@tonic-gate 
225dfe57350Sjeanm 		log_framework(LOG_DEBUG, "Storing primary contract %ld for "
226dfe57350Sjeanm 		    "%s.\n", *cid, inst->ri_i.i_fmri);
227dfe57350Sjeanm 
2287c478bd9Sstevel@tonic-gate 		contract_hash_store(*cid, inst->ri_id);
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate again:
2327c478bd9Sstevel@tonic-gate 	if (inst->ri_mi_deleted)
2337c478bd9Sstevel@tonic-gate 		return;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	r = restarter_store_contract(inst->ri_m_inst, *cid, primary ?
2367c478bd9Sstevel@tonic-gate 	    RESTARTER_CONTRACT_PRIMARY : RESTARTER_CONTRACT_TRANSIENT);
2377c478bd9Sstevel@tonic-gate 	switch (r) {
2387c478bd9Sstevel@tonic-gate 	case 0:
2397c478bd9Sstevel@tonic-gate 		break;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	case ECANCELED:
2427c478bd9Sstevel@tonic-gate 		inst->ri_mi_deleted = B_TRUE;
2437c478bd9Sstevel@tonic-gate 		break;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
2467c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(scf_instance_handle(inst->ri_m_inst));
2477c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	case EBADF:
2507c478bd9Sstevel@tonic-gate 		libscf_reget_instance(inst);
2517c478bd9Sstevel@tonic-gate 		goto again;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	case ENOMEM:
2547c478bd9Sstevel@tonic-gate 	case EPERM:
2557c478bd9Sstevel@tonic-gate 	case EACCES:
2567c478bd9Sstevel@tonic-gate 	case EROFS:
2577c478bd9Sstevel@tonic-gate 		uu_die("%s: Couldn't store contract id %ld",
2587c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_fmri, *cid);
2597c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	case EINVAL:
2627c478bd9Sstevel@tonic-gate 	default:
2637c478bd9Sstevel@tonic-gate 		bad_error("restarter_store_contract", r);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate  * void method_remove_contract()
2697c478bd9Sstevel@tonic-gate  *   Remove any non-permanent contracts from internal structures and
2707c478bd9Sstevel@tonic-gate  *   the repository, then abandon them.
2717c478bd9Sstevel@tonic-gate  *   Returns
2727c478bd9Sstevel@tonic-gate  *     0 - success
2737c478bd9Sstevel@tonic-gate  *     ECANCELED - inst was deleted from the repository
2747c478bd9Sstevel@tonic-gate  *
2757c478bd9Sstevel@tonic-gate  *   If the repository connection was broken, it is rebound.
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate void
method_remove_contract(restarter_inst_t * inst,boolean_t primary,boolean_t abandon)2787c478bd9Sstevel@tonic-gate method_remove_contract(restarter_inst_t *inst, boolean_t primary,
2797c478bd9Sstevel@tonic-gate     boolean_t abandon)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	ctid_t * const ctidp = primary ? &inst->ri_i.i_primary_ctid :
2827c478bd9Sstevel@tonic-gate 	    &inst->ri_i.i_transient_ctid;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	int r;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	assert(*ctidp != 0);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "Removing %s contract %lu for %s.\n",
2897c478bd9Sstevel@tonic-gate 	    primary ? "primary" : "transient", *ctidp, inst->ri_i.i_fmri);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (abandon)
2927c478bd9Sstevel@tonic-gate 		contract_abandon(*ctidp);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate again:
2957c478bd9Sstevel@tonic-gate 	if (inst->ri_mi_deleted) {
2967c478bd9Sstevel@tonic-gate 		r = ECANCELED;
2977c478bd9Sstevel@tonic-gate 		goto out;
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	r = restarter_remove_contract(inst->ri_m_inst, *ctidp, primary ?
3017c478bd9Sstevel@tonic-gate 	    RESTARTER_CONTRACT_PRIMARY : RESTARTER_CONTRACT_TRANSIENT);
3027c478bd9Sstevel@tonic-gate 	switch (r) {
3037c478bd9Sstevel@tonic-gate 	case 0:
3047c478bd9Sstevel@tonic-gate 		break;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	case ECANCELED:
3077c478bd9Sstevel@tonic-gate 		inst->ri_mi_deleted = B_TRUE;
3087c478bd9Sstevel@tonic-gate 		break;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
3117c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(scf_instance_handle(inst->ri_m_inst));
3127c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	case EBADF:
3157c478bd9Sstevel@tonic-gate 		libscf_reget_instance(inst);
3167c478bd9Sstevel@tonic-gate 		goto again;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	case ENOMEM:
3197c478bd9Sstevel@tonic-gate 	case EPERM:
3207c478bd9Sstevel@tonic-gate 	case EACCES:
3217c478bd9Sstevel@tonic-gate 	case EROFS:
3227c478bd9Sstevel@tonic-gate 		log_error(LOG_INFO, "%s: Couldn't remove contract id %ld: "
3237c478bd9Sstevel@tonic-gate 		    "%s.\n", inst->ri_i.i_fmri, *ctidp, strerror(r));
3247c478bd9Sstevel@tonic-gate 		break;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	case EINVAL:
3277c478bd9Sstevel@tonic-gate 	default:
3287c478bd9Sstevel@tonic-gate 		bad_error("restarter_remove_contract", r);
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate out:
3327c478bd9Sstevel@tonic-gate 	if (primary)
3337c478bd9Sstevel@tonic-gate 		contract_hash_remove(*ctidp);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	*ctidp = 0;
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387b209c2cSacruz static const char *method_names[] = { "start", "stop", "refresh" };
3397b209c2cSacruz 
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate  * int method_ready_contract(restarter_inst_t *, int, method_restart_t, int)
3427c478bd9Sstevel@tonic-gate  *
3437c478bd9Sstevel@tonic-gate  *   Activate a contract template for the type method of inst.  type,
3447c478bd9Sstevel@tonic-gate  *   restart_on, and cte_mask dictate the critical events term of the contract.
3457c478bd9Sstevel@tonic-gate  *   Returns
3467c478bd9Sstevel@tonic-gate  *     0 - success
3477c478bd9Sstevel@tonic-gate  *     ECANCELED - inst has been deleted from the repository
3487c478bd9Sstevel@tonic-gate  */
3497c478bd9Sstevel@tonic-gate static int
method_ready_contract(restarter_inst_t * inst,int type,method_restart_t restart_on,uint_t cte_mask)3507c478bd9Sstevel@tonic-gate method_ready_contract(restarter_inst_t *inst, int type,
3517c478bd9Sstevel@tonic-gate     method_restart_t restart_on, uint_t cte_mask)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	int tmpl, err, istrans, iswait, ret;
3547c478bd9Sstevel@tonic-gate 	uint_t cevents, fevents;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	/*
3577c478bd9Sstevel@tonic-gate 	 * Correctly supporting wait-style services is tricky without
3587c478bd9Sstevel@tonic-gate 	 * rearchitecting startd to cope with multiple event sources
3597c478bd9Sstevel@tonic-gate 	 * simultaneously trying to stop an instance.  Until a better
3607c478bd9Sstevel@tonic-gate 	 * solution is implemented, we avoid this problem for
3617c478bd9Sstevel@tonic-gate 	 * wait-style services by making contract events fatal and
3627c478bd9Sstevel@tonic-gate 	 * letting the wait code alone handle stopping the service.
3637c478bd9Sstevel@tonic-gate 	 */
3647c478bd9Sstevel@tonic-gate 	iswait = instance_is_wait_style(inst);
3657c478bd9Sstevel@tonic-gate 	istrans = method_is_transient(inst, type);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	tmpl = open64(CTFS_ROOT "/process/template", O_RDWR);
3687c478bd9Sstevel@tonic-gate 	if (tmpl == -1)
3697c478bd9Sstevel@tonic-gate 		uu_die("Could not create contract template");
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	/*
3727c478bd9Sstevel@tonic-gate 	 * We assume non-login processes are unlikely to create
3737c478bd9Sstevel@tonic-gate 	 * multiple process groups, and set CT_PR_PGRPONLY for all
3747c478bd9Sstevel@tonic-gate 	 * wait-style services' contracts.
3757c478bd9Sstevel@tonic-gate 	 */
3767c478bd9Sstevel@tonic-gate 	err = ct_pr_tmpl_set_param(tmpl, CT_PR_INHERIT | CT_PR_REGENT |
3777c478bd9Sstevel@tonic-gate 	    (iswait ? CT_PR_PGRPONLY : 0));
3787c478bd9Sstevel@tonic-gate 	assert(err == 0);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	if (istrans) {
3817c478bd9Sstevel@tonic-gate 		cevents = 0;
3827c478bd9Sstevel@tonic-gate 		fevents = 0;
3837c478bd9Sstevel@tonic-gate 	} else {
3847c478bd9Sstevel@tonic-gate 		assert(restart_on >= 0);
3857c478bd9Sstevel@tonic-gate 		assert(restart_on <= METHOD_RESTART_ANY_FAULT);
3867c478bd9Sstevel@tonic-gate 		cevents = method_events[restart_on] & ~cte_mask;
3877c478bd9Sstevel@tonic-gate 		fevents = iswait ?
3887c478bd9Sstevel@tonic-gate 		    (method_events[restart_on] & ~cte_mask & CT_PR_ALLFATAL) :
3897c478bd9Sstevel@tonic-gate 		    0;
3907c478bd9Sstevel@tonic-gate 	}
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	err = ct_tmpl_set_critical(tmpl, cevents);
3937c478bd9Sstevel@tonic-gate 	assert(err == 0);
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	err = ct_tmpl_set_informative(tmpl, 0);
3967c478bd9Sstevel@tonic-gate 	assert(err == 0);
3977c478bd9Sstevel@tonic-gate 	err = ct_pr_tmpl_set_fatal(tmpl, fevents);
3987c478bd9Sstevel@tonic-gate 	assert(err == 0);
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	err = ct_tmpl_set_cookie(tmpl, istrans ?  METHOD_OTHER_COOKIE :
4017c478bd9Sstevel@tonic-gate 	    METHOD_START_COOKIE);
4027c478bd9Sstevel@tonic-gate 	assert(err == 0);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if (type == METHOD_START && inst->ri_i.i_primary_ctid != 0) {
4057c478bd9Sstevel@tonic-gate 		ret = ct_pr_tmpl_set_transfer(tmpl, inst->ri_i.i_primary_ctid);
4067c478bd9Sstevel@tonic-gate 		switch (ret) {
4077c478bd9Sstevel@tonic-gate 		case 0:
4087c478bd9Sstevel@tonic-gate 			break;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 		case ENOTEMPTY:
4117c478bd9Sstevel@tonic-gate 			/* No contracts for you! */
4127c478bd9Sstevel@tonic-gate 			method_remove_contract(inst, B_TRUE, B_TRUE);
4137c478bd9Sstevel@tonic-gate 			if (inst->ri_mi_deleted) {
4147c478bd9Sstevel@tonic-gate 				ret = ECANCELED;
4157c478bd9Sstevel@tonic-gate 				goto out;
4167c478bd9Sstevel@tonic-gate 			}
4177c478bd9Sstevel@tonic-gate 			break;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		case EINVAL:
4207c478bd9Sstevel@tonic-gate 		case ESRCH:
4217c478bd9Sstevel@tonic-gate 		case EACCES:
4227c478bd9Sstevel@tonic-gate 		default:
4237c478bd9Sstevel@tonic-gate 			bad_error("ct_pr_tmpl_set_transfer", ret);
4247c478bd9Sstevel@tonic-gate 		}
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
4277b209c2cSacruz 	err = ct_pr_tmpl_set_svc_fmri(tmpl, inst->ri_i.i_fmri);
4287b209c2cSacruz 	assert(err == 0);
4297b209c2cSacruz 	err = ct_pr_tmpl_set_svc_aux(tmpl, method_names[type]);
4307b209c2cSacruz 	assert(err == 0);
4317b209c2cSacruz 
4327c478bd9Sstevel@tonic-gate 	err = ct_tmpl_activate(tmpl);
4337c478bd9Sstevel@tonic-gate 	assert(err == 0);
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 	ret = 0;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate out:
4387c478bd9Sstevel@tonic-gate 	err = close(tmpl);
4397c478bd9Sstevel@tonic-gate 	assert(err == 0);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	return (ret);
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate static void
exec_method(const restarter_inst_t * inst,int type,const char * method,struct method_context * mcp,uint8_t need_session)4457c478bd9Sstevel@tonic-gate exec_method(const restarter_inst_t *inst, int type, const char *method,
4467c478bd9Sstevel@tonic-gate     struct method_context *mcp, uint8_t need_session)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	char *cmd;
4497c478bd9Sstevel@tonic-gate 	const char *errf;
4507c478bd9Sstevel@tonic-gate 	char **nenv;
45177c20ef8Sacruz 	int rsmc_errno = 0;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	cmd = uu_msprintf("exec %s", method);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (inst->ri_utmpx_prefix[0] != '\0' && inst->ri_utmpx_prefix != NULL)
4567c478bd9Sstevel@tonic-gate 		(void) utmpx_mark_init(getpid(), inst->ri_utmpx_prefix);
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	setlog(inst->ri_logstem);
4595ca87c7fSlianep 	log_instance(inst, B_FALSE, "Executing %s method (\"%s\").",
4607c478bd9Sstevel@tonic-gate 	    method_names[type], method);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (need_session)
4637c478bd9Sstevel@tonic-gate 		(void) setpgrp();
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	/* Set credentials. */
46677c20ef8Sacruz 	rsmc_errno = restarter_set_method_context(mcp, &errf);
46777c20ef8Sacruz 	if (rsmc_errno != 0) {
46813d8aaa1SSean Wilcox 		log_instance(inst, B_FALSE,
46913d8aaa1SSean Wilcox 		    "svc.startd could not set context for method: ");
4707c478bd9Sstevel@tonic-gate 
47177c20ef8Sacruz 		if (rsmc_errno == -1) {
4727c478bd9Sstevel@tonic-gate 			if (strcmp(errf, "core_set_process_path") == 0) {
47313d8aaa1SSean Wilcox 				log_instance(inst, B_FALSE,
47413d8aaa1SSean Wilcox 				    "Could not set corefile path.");
4757c478bd9Sstevel@tonic-gate 			} else if (strcmp(errf, "setproject") == 0) {
47613d8aaa1SSean Wilcox 				log_instance(inst, B_FALSE, "%s: a resource "
47713d8aaa1SSean Wilcox 				    "control assignment failed", errf);
4787c478bd9Sstevel@tonic-gate 			} else if (strcmp(errf, "pool_set_binding") == 0) {
47913d8aaa1SSean Wilcox 				log_instance(inst, B_FALSE, "%s: a system "
48013d8aaa1SSean Wilcox 				    "error occurred", errf);
4817c478bd9Sstevel@tonic-gate 			} else {
4827c478bd9Sstevel@tonic-gate #ifndef NDEBUG
4837c478bd9Sstevel@tonic-gate 				uu_warn("%s:%d: Bad function name \"%s\" for "
4847c478bd9Sstevel@tonic-gate 				    "error %d from "
4857c478bd9Sstevel@tonic-gate 				    "restarter_set_method_context().\n",
48677c20ef8Sacruz 				    __FILE__, __LINE__, errf, rsmc_errno);
4877c478bd9Sstevel@tonic-gate #endif
4887c478bd9Sstevel@tonic-gate 				abort();
4897c478bd9Sstevel@tonic-gate 			}
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 			exit(1);
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		if (errf != NULL && strcmp(errf, "pool_set_binding") == 0) {
49577c20ef8Sacruz 			switch (rsmc_errno) {
4967c478bd9Sstevel@tonic-gate 			case ENOENT:
49713d8aaa1SSean Wilcox 				log_instance(inst, B_FALSE, "%s: the pool "
49813d8aaa1SSean Wilcox 				    "could not be found", errf);
4997c478bd9Sstevel@tonic-gate 				break;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 			case EBADF:
50213d8aaa1SSean Wilcox 				log_instance(inst, B_FALSE, "%s: the "
50313d8aaa1SSean Wilcox 				    "configuration is invalid", errf);
5047c478bd9Sstevel@tonic-gate 				break;
5057c478bd9Sstevel@tonic-gate 
5063ad28c1eSrm 			case EINVAL:
50713d8aaa1SSean Wilcox 				log_instance(inst, B_FALSE, "%s: pool name "
50813d8aaa1SSean Wilcox 				    "\"%s\" is invalid", errf,
50913d8aaa1SSean Wilcox 				    mcp->resource_pool);
5103ad28c1eSrm 				break;
5113ad28c1eSrm 
5127c478bd9Sstevel@tonic-gate 			default:
5137c478bd9Sstevel@tonic-gate #ifndef NDEBUG
5147c478bd9Sstevel@tonic-gate 				uu_warn("%s:%d: Bad error %d for function %s "
51533875201SHengqing Hu 				    "in restarter_set_method_context().\n",
51633875201SHengqing Hu 				    __FILE__, __LINE__, rsmc_errno, errf);
51733875201SHengqing Hu #endif
51833875201SHengqing Hu 				abort();
51933875201SHengqing Hu 			}
52033875201SHengqing Hu 
52133875201SHengqing Hu 			exit(SMF_EXIT_ERR_CONFIG);
52233875201SHengqing Hu 		}
52333875201SHengqing Hu 
52433875201SHengqing Hu 		if (errf != NULL && strcmp(errf, "chdir") == 0) {
52533875201SHengqing Hu 			switch (rsmc_errno) {
52633875201SHengqing Hu 			case EACCES:
52733875201SHengqing Hu 			case EFAULT:
52833875201SHengqing Hu 			case EIO:
52933875201SHengqing Hu 			case ELOOP:
53033875201SHengqing Hu 			case ENAMETOOLONG:
53133875201SHengqing Hu 			case ENOENT:
53233875201SHengqing Hu 			case ENOLINK:
53333875201SHengqing Hu 			case ENOTDIR:
53433875201SHengqing Hu 				log_instance(inst, B_FALSE, "%s: %s (\"%s\")",
53533875201SHengqing Hu 				    errf,
53633875201SHengqing Hu 				    strerror(rsmc_errno), mcp->working_dir);
53733875201SHengqing Hu 				break;
53833875201SHengqing Hu 
53933875201SHengqing Hu 			default:
54033875201SHengqing Hu #ifndef NDEBUG
54133875201SHengqing Hu 				uu_warn("%s:%d: Bad error %d for function %s "
5427c478bd9Sstevel@tonic-gate 				    "in restarter_set_method_context().\n",
54377c20ef8Sacruz 				    __FILE__, __LINE__, rsmc_errno, errf);
5447c478bd9Sstevel@tonic-gate #endif
5457c478bd9Sstevel@tonic-gate 				abort();
5467c478bd9Sstevel@tonic-gate 			}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 			exit(SMF_EXIT_ERR_CONFIG);
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 		if (errf != NULL) {
55277c20ef8Sacruz 			errno = rsmc_errno;
5537c478bd9Sstevel@tonic-gate 			perror(errf);
5547c478bd9Sstevel@tonic-gate 
55577c20ef8Sacruz 			switch (rsmc_errno) {
5567c478bd9Sstevel@tonic-gate 			case EINVAL:
5577c478bd9Sstevel@tonic-gate 			case EPERM:
5587c478bd9Sstevel@tonic-gate 			case ENOENT:
5597c478bd9Sstevel@tonic-gate 			case ENAMETOOLONG:
5607c478bd9Sstevel@tonic-gate 			case ERANGE:
5617c478bd9Sstevel@tonic-gate 			case ESRCH:
5627c478bd9Sstevel@tonic-gate 				exit(SMF_EXIT_ERR_CONFIG);
5637c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 			default:
5667c478bd9Sstevel@tonic-gate 				exit(1);
5677c478bd9Sstevel@tonic-gate 			}
5687c478bd9Sstevel@tonic-gate 		}
5697c478bd9Sstevel@tonic-gate 
57077c20ef8Sacruz 		switch (rsmc_errno) {
5717c478bd9Sstevel@tonic-gate 		case ENOMEM:
57213d8aaa1SSean Wilcox 			log_instance(inst, B_FALSE, "Out of memory.");
5737c478bd9Sstevel@tonic-gate 			exit(1);
5747c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 		case ENOENT:
57713d8aaa1SSean Wilcox 			log_instance(inst, B_FALSE, "Missing passwd entry for "
57813d8aaa1SSean Wilcox 			    "user.");
5797c478bd9Sstevel@tonic-gate 			exit(SMF_EXIT_ERR_CONFIG);
5807c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 		default:
5837c478bd9Sstevel@tonic-gate #ifndef NDEBUG
5847c478bd9Sstevel@tonic-gate 			uu_warn("%s:%d: Bad miscellaneous error %d from "
5857c478bd9Sstevel@tonic-gate 			    "restarter_set_method_context().\n", __FILE__,
58677c20ef8Sacruz 			    __LINE__, rsmc_errno);
5877c478bd9Sstevel@tonic-gate #endif
5887c478bd9Sstevel@tonic-gate 			abort();
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5923eae19d9Swesolows 	nenv = set_smf_env(mcp->env, mcp->env_sz, NULL, inst,
5933eae19d9Swesolows 	    method_names[type]);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	log_preexec();
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	(void) execle(SBIN_SH, SBIN_SH, "-c", cmd, NULL, nenv);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	exit(10);
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate static void
write_status(restarter_inst_t * inst,const char * mname,int stat)6037c478bd9Sstevel@tonic-gate write_status(restarter_inst_t *inst, const char *mname, int stat)
6047c478bd9Sstevel@tonic-gate {
6057c478bd9Sstevel@tonic-gate 	int r;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate again:
6087c478bd9Sstevel@tonic-gate 	if (inst->ri_mi_deleted)
6097c478bd9Sstevel@tonic-gate 		return;
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	r = libscf_write_method_status(inst->ri_m_inst, mname, stat);
6127c478bd9Sstevel@tonic-gate 	switch (r) {
6137c478bd9Sstevel@tonic-gate 	case 0:
6147c478bd9Sstevel@tonic-gate 		break;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
6177c478bd9Sstevel@tonic-gate 		libscf_reget_instance(inst);
6187c478bd9Sstevel@tonic-gate 		goto again;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	case ECANCELED:
6217c478bd9Sstevel@tonic-gate 		inst->ri_mi_deleted = 1;
6227c478bd9Sstevel@tonic-gate 		break;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	case EPERM:
6257c478bd9Sstevel@tonic-gate 	case EACCES:
6267c478bd9Sstevel@tonic-gate 	case EROFS:
6277c478bd9Sstevel@tonic-gate 		log_framework(LOG_INFO, "Could not write exit status "
6287c478bd9Sstevel@tonic-gate 		    "for %s method of %s: %s.\n", mname,
6297c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_fmri, strerror(r));
6307c478bd9Sstevel@tonic-gate 		break;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	case ENAMETOOLONG:
6337c478bd9Sstevel@tonic-gate 	default:
6347c478bd9Sstevel@tonic-gate 		bad_error("libscf_write_method_status", r);
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate /*
6397c478bd9Sstevel@tonic-gate  * int method_run()
6407c478bd9Sstevel@tonic-gate  *   Execute the type method of instp.  If it requires a fork(), wait for it
6417c478bd9Sstevel@tonic-gate  *   to return and return its exit code in *exit_code.  Otherwise set
6427c478bd9Sstevel@tonic-gate  *   *exit_code to 0 if the method succeeds & -1 if it fails.  If the
6437c478bd9Sstevel@tonic-gate  *   repository connection is broken, it is rebound, but inst may not be
6447c478bd9Sstevel@tonic-gate  *   reset.
6457c478bd9Sstevel@tonic-gate  *   Returns
6467c478bd9Sstevel@tonic-gate  *     0 - success
6477c478bd9Sstevel@tonic-gate  *     EINVAL - A correct method or method context couldn't be retrieved.
6487c478bd9Sstevel@tonic-gate  *     EIO - Contract kill failed.
6497c478bd9Sstevel@tonic-gate  *     EFAULT - Method couldn't be executed successfully.
6507c478bd9Sstevel@tonic-gate  *     ELOOP - Retry threshold exceeded.
6517c478bd9Sstevel@tonic-gate  *     ECANCELED - inst was deleted from the repository before method was run
6527c478bd9Sstevel@tonic-gate  *     ERANGE - Timeout retry threshold exceeded.
6537c478bd9Sstevel@tonic-gate  *     EAGAIN - Failed due to external cause, retry.
6547c478bd9Sstevel@tonic-gate  */
6557c478bd9Sstevel@tonic-gate int
method_run(restarter_inst_t ** instp,int type,int * exit_code)6567c478bd9Sstevel@tonic-gate method_run(restarter_inst_t **instp, int type, int *exit_code)
6577c478bd9Sstevel@tonic-gate {
6587c478bd9Sstevel@tonic-gate 	char *method;
6597c478bd9Sstevel@tonic-gate 	int ret_status;
6607c478bd9Sstevel@tonic-gate 	pid_t pid;
6617c478bd9Sstevel@tonic-gate 	method_restart_t restart_on;
6627c478bd9Sstevel@tonic-gate 	uint_t cte_mask;
6637c478bd9Sstevel@tonic-gate 	uint8_t need_session;
6647c478bd9Sstevel@tonic-gate 	scf_handle_t *h;
6657c478bd9Sstevel@tonic-gate 	scf_snapshot_t *snap;
6667c478bd9Sstevel@tonic-gate 	const char *mname;
667870ad75aSSean Wilcox 	mc_error_t *m_error;
6687c478bd9Sstevel@tonic-gate 	struct method_context *mcp;
6697c478bd9Sstevel@tonic-gate 	int result = 0, timeout_fired = 0;
6707c478bd9Sstevel@tonic-gate 	int sig, r;
6717c478bd9Sstevel@tonic-gate 	boolean_t transient;
6727c478bd9Sstevel@tonic-gate 	uint64_t timeout;
6737c478bd9Sstevel@tonic-gate 	uint8_t timeout_retry;
6747c478bd9Sstevel@tonic-gate 	ctid_t ctid;
6757c478bd9Sstevel@tonic-gate 	int ctfd = -1;
6767c478bd9Sstevel@tonic-gate 	restarter_inst_t *inst = *instp;
6777c478bd9Sstevel@tonic-gate 	int id = inst->ri_id;
678ca948064Ssl 	int forkerr;
6797c478bd9Sstevel@tonic-gate 
68053f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&inst->ri_lock));
6817c478bd9Sstevel@tonic-gate 	assert(instance_in_transition(inst));
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	if (inst->ri_mi_deleted)
6847c478bd9Sstevel@tonic-gate 		return (ECANCELED);
6857c478bd9Sstevel@tonic-gate 
686f18d8787SJerry Jelinek 	*exit_code = SMF_EXIT_OK;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	assert(0 <= type && type <= 2);
6897c478bd9Sstevel@tonic-gate 	mname = method_names[type];
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	if (type == METHOD_START)
6927c478bd9Sstevel@tonic-gate 		inst->ri_pre_online_hook();
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	h = scf_instance_handle(inst->ri_m_inst);
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	snap = scf_snapshot_create(h);
6977c478bd9Sstevel@tonic-gate 	if (snap == NULL ||
6987c478bd9Sstevel@tonic-gate 	    scf_instance_get_snapshot(inst->ri_m_inst, "running", snap) != 0) {
6997c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
7007c478bd9Sstevel@tonic-gate 		    "Could not get running snapshot for %s.  "
7017c478bd9Sstevel@tonic-gate 		    "Using editing version to run method %s.\n",
7027c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_fmri, mname);
7037c478bd9Sstevel@tonic-gate 		scf_snapshot_destroy(snap);
7047c478bd9Sstevel@tonic-gate 		snap = NULL;
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	/*
7087c478bd9Sstevel@tonic-gate 	 * After this point, we may be logging to the instance log.
7097c478bd9Sstevel@tonic-gate 	 * Make sure we've noted where that log is as a property of
7107c478bd9Sstevel@tonic-gate 	 * the instance.
7117c478bd9Sstevel@tonic-gate 	 */
7127c478bd9Sstevel@tonic-gate 	r = libscf_note_method_log(inst->ri_m_inst, st->st_log_prefix,
7137c478bd9Sstevel@tonic-gate 	    inst->ri_logstem);
7147c478bd9Sstevel@tonic-gate 	if (r != 0) {
7157c478bd9Sstevel@tonic-gate 		log_framework(LOG_WARNING,
7167c478bd9Sstevel@tonic-gate 		    "%s: couldn't note log location: %s\n",
7177c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_fmri, strerror(r));
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	if ((method = libscf_get_method(h, type, inst, snap, &restart_on,
7217c478bd9Sstevel@tonic-gate 	    &cte_mask, &need_session, &timeout, &timeout_retry)) == NULL) {
7227c478bd9Sstevel@tonic-gate 		if (errno == LIBSCF_PGROUP_ABSENT)  {
7237c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
7247c478bd9Sstevel@tonic-gate 			    "%s: instance has no method property group '%s'.\n",
7257c478bd9Sstevel@tonic-gate 			    inst->ri_i.i_fmri, mname);
7267c478bd9Sstevel@tonic-gate 			if (type == METHOD_REFRESH)
7277c478bd9Sstevel@tonic-gate 				log_instance(inst, B_TRUE, "No '%s' method "
7287c478bd9Sstevel@tonic-gate 				    "defined.  Treating as :true.", mname);
7297c478bd9Sstevel@tonic-gate 			else
7307c478bd9Sstevel@tonic-gate 				log_instance(inst, B_TRUE, "Method property "
7317c478bd9Sstevel@tonic-gate 				    "group '%s' is not present.", mname);
7327c478bd9Sstevel@tonic-gate 			scf_snapshot_destroy(snap);
7337c478bd9Sstevel@tonic-gate 			return (0);
7347c478bd9Sstevel@tonic-gate 		} else if (errno == LIBSCF_PROPERTY_ABSENT)  {
7357c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG,
7367c478bd9Sstevel@tonic-gate 			    "%s: instance has no '%s/exec' method property.\n",
7377c478bd9Sstevel@tonic-gate 			    inst->ri_i.i_fmri, mname);
7387c478bd9Sstevel@tonic-gate 			log_instance(inst, B_TRUE, "Method property '%s/exec "
7397c478bd9Sstevel@tonic-gate 			    "is not present.", mname);
7407c478bd9Sstevel@tonic-gate 			scf_snapshot_destroy(snap);
7417c478bd9Sstevel@tonic-gate 			return (0);
7427c478bd9Sstevel@tonic-gate 		} else {
7437c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
7447c478bd9Sstevel@tonic-gate 			    "%s: instance libscf_get_method failed\n",
7457c478bd9Sstevel@tonic-gate 			    inst->ri_i.i_fmri);
7467c478bd9Sstevel@tonic-gate 			scf_snapshot_destroy(snap);
7477c478bd9Sstevel@tonic-gate 			return (EINVAL);
7487c478bd9Sstevel@tonic-gate 		}
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	/* open service contract if stopping a non-transient service */
7527c478bd9Sstevel@tonic-gate 	if (type == METHOD_STOP && (!instance_is_transient_style(inst))) {
7537c478bd9Sstevel@tonic-gate 		if (inst->ri_i.i_primary_ctid == 0) {
7547c478bd9Sstevel@tonic-gate 			/* service is not running, nothing to stop */
7557c478bd9Sstevel@tonic-gate 			log_framework(LOG_DEBUG, "%s: instance has no primary "
7567c478bd9Sstevel@tonic-gate 			    "contract, no service to stop.\n",
7577c478bd9Sstevel@tonic-gate 			    inst->ri_i.i_fmri);
7587c478bd9Sstevel@tonic-gate 			scf_snapshot_destroy(snap);
7597c478bd9Sstevel@tonic-gate 			return (0);
7607c478bd9Sstevel@tonic-gate 		}
7617c478bd9Sstevel@tonic-gate 		if ((ctfd = contract_open(inst->ri_i.i_primary_ctid, "process",
7627c478bd9Sstevel@tonic-gate 		    "events", O_RDONLY)) < 0) {
7637c478bd9Sstevel@tonic-gate 			result = EFAULT;
7647c478bd9Sstevel@tonic-gate 			log_instance(inst, B_TRUE, "Could not open service "
7655ca87c7fSlianep 			    "contract %ld.  Stop method not run.",
7667c478bd9Sstevel@tonic-gate 			    inst->ri_i.i_primary_ctid);
7677c478bd9Sstevel@tonic-gate 			goto out;
7687c478bd9Sstevel@tonic-gate 		}
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	if (restarter_is_null_method(method)) {
7727c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "%s: null method succeeds\n",
7737c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_fmri);
7747c478bd9Sstevel@tonic-gate 
7755ca87c7fSlianep 		log_instance(inst, B_TRUE, "Executing %s method (null).",
7765ca87c7fSlianep 		    mname);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		if (type == METHOD_START)
7797c478bd9Sstevel@tonic-gate 			write_status(inst, mname, 0);
7807c478bd9Sstevel@tonic-gate 		goto out;
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	sig = restarter_is_kill_method(method);
7847c478bd9Sstevel@tonic-gate 	if (sig >= 0) {
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 		if (inst->ri_i.i_primary_ctid == 0) {
7877c478bd9Sstevel@tonic-gate 			log_error(LOG_ERR, "%s: :kill with no contract\n",
7887c478bd9Sstevel@tonic-gate 			    inst->ri_i.i_fmri);
7895ca87c7fSlianep 			log_instance(inst, B_TRUE, "Invalid use of \":kill\" "
7905ca87c7fSlianep 			    "as stop method for transient service.");
7917c478bd9Sstevel@tonic-gate 			result = EINVAL;
7927c478bd9Sstevel@tonic-gate 			goto out;
7937c478bd9Sstevel@tonic-gate 		}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
7967c478bd9Sstevel@tonic-gate 		    "%s: :killing contract with signal %d\n",
7977c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_fmri, sig);
7987c478bd9Sstevel@tonic-gate 
7995ca87c7fSlianep 		log_instance(inst, B_TRUE, "Executing %s method (:kill).",
8007c478bd9Sstevel@tonic-gate 		    mname);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 		if (contract_kill(inst->ri_i.i_primary_ctid, sig,
8037c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_fmri) != 0) {
8047c478bd9Sstevel@tonic-gate 			result = EIO;
8057c478bd9Sstevel@tonic-gate 			goto out;
8067c478bd9Sstevel@tonic-gate 		} else
8077c478bd9Sstevel@tonic-gate 			goto assured_kill;
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "%s: forking to run method %s\n",
8117c478bd9Sstevel@tonic-gate 	    inst->ri_i.i_fmri, method);
8127c478bd9Sstevel@tonic-gate 
813870ad75aSSean Wilcox 	m_error = restarter_get_method_context(RESTARTER_METHOD_CONTEXT_VERSION,
8147c478bd9Sstevel@tonic-gate 	    inst->ri_m_inst, snap, mname, method, &mcp);
8157c478bd9Sstevel@tonic-gate 
816870ad75aSSean Wilcox 	if (m_error != NULL) {
817870ad75aSSean Wilcox 		log_instance(inst, B_TRUE, "%s", m_error->msg);
818870ad75aSSean Wilcox 		restarter_mc_error_destroy(m_error);
8197c478bd9Sstevel@tonic-gate 		result = EINVAL;
8207c478bd9Sstevel@tonic-gate 		goto out;
8217c478bd9Sstevel@tonic-gate 	}
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	r = method_ready_contract(inst, type, restart_on, cte_mask);
8247c478bd9Sstevel@tonic-gate 	if (r != 0) {
8257c478bd9Sstevel@tonic-gate 		assert(r == ECANCELED);
8267c478bd9Sstevel@tonic-gate 		assert(inst->ri_mi_deleted);
8277c478bd9Sstevel@tonic-gate 		restarter_free_method_context(mcp);
8287c478bd9Sstevel@tonic-gate 		result = ECANCELED;
8297c478bd9Sstevel@tonic-gate 		goto out;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	/*
8337c478bd9Sstevel@tonic-gate 	 * Validate safety of method contexts, to save children work.
8347c478bd9Sstevel@tonic-gate 	 */
8357c478bd9Sstevel@tonic-gate 	if (!restarter_rm_libs_loadable())
8367c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG, "%s: method contexts limited "
8377c478bd9Sstevel@tonic-gate 		    "to root-accessible libraries\n", inst->ri_i.i_fmri);
8387c478bd9Sstevel@tonic-gate 
8392a17138dSJerry Jelinek 	/*
8402a17138dSJerry Jelinek 	 * For wait-style svc, sanity check that method exists to prevent an
8412a17138dSJerry Jelinek 	 * infinite loop.
8422a17138dSJerry Jelinek 	 */
8432a17138dSJerry Jelinek 	if (instance_is_wait_style(inst) && type == METHOD_START) {
8442a17138dSJerry Jelinek 		char *pend;
8452a17138dSJerry Jelinek 		struct stat64 sbuf;
8462a17138dSJerry Jelinek 
8472a17138dSJerry Jelinek 		/*
8482a17138dSJerry Jelinek 		 * We need to handle start method strings that have arguments,
8492a17138dSJerry Jelinek 		 * such as '/lib/svc/method/console-login %i'.
8502a17138dSJerry Jelinek 		 */
8512a17138dSJerry Jelinek 		if ((pend = strchr(method, ' ')) != NULL)
8522a17138dSJerry Jelinek 			*pend = '\0';
8532a17138dSJerry Jelinek 
85452e13e00SJerry Jelinek 		if (*method == '/' && stat64(method, &sbuf) == -1 &&
85552e13e00SJerry Jelinek 		    errno == ENOENT) {
8562a17138dSJerry Jelinek 			log_instance(inst, B_TRUE, "Missing start method (%s), "
8572a17138dSJerry Jelinek 			    "changing state to maintenance.", method);
8582a17138dSJerry Jelinek 			restarter_free_method_context(mcp);
8592a17138dSJerry Jelinek 			result = ENOENT;
8602a17138dSJerry Jelinek 			goto out;
8612a17138dSJerry Jelinek 		}
8622a17138dSJerry Jelinek 		if (pend != NULL)
8632a17138dSJerry Jelinek 			*pend = ' ';
8642a17138dSJerry Jelinek 	}
8652a17138dSJerry Jelinek 
8667c478bd9Sstevel@tonic-gate 	/*
8677c478bd9Sstevel@tonic-gate 	 * If the service is restarting too quickly, send it to
8687c478bd9Sstevel@tonic-gate 	 * maintenance.
8697c478bd9Sstevel@tonic-gate 	 */
8707c478bd9Sstevel@tonic-gate 	if (type == METHOD_START) {
8717c478bd9Sstevel@tonic-gate 		method_record_start(inst);
8722a17138dSJerry Jelinek 		if (method_rate_critical(inst) &&
8732a17138dSJerry Jelinek 		    !instance_is_wait_style(inst)) {
8747c478bd9Sstevel@tonic-gate 			log_instance(inst, B_TRUE, "Restarting too quickly, "
8755ca87c7fSlianep 			    "changing state to maintenance.");
8767c478bd9Sstevel@tonic-gate 			result = ELOOP;
877d3186a0eSjeanm 			restarter_free_method_context(mcp);
8787c478bd9Sstevel@tonic-gate 			goto out;
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 
882dfe57350Sjeanm 	atomic_add_16(&storing_contract, 1);
883ca948064Ssl 	pid = startd_fork1(&forkerr);
8847c478bd9Sstevel@tonic-gate 	if (pid == 0)
8857c478bd9Sstevel@tonic-gate 		exec_method(inst, type, method, mcp, need_session);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	if (pid == -1) {
888dfe57350Sjeanm 		atomic_add_16(&storing_contract, -1);
889ca948064Ssl 		if (forkerr == EAGAIN)
890ca948064Ssl 			result = EAGAIN;
891ca948064Ssl 		else
892ca948064Ssl 			result = EFAULT;
893ca948064Ssl 
8947c478bd9Sstevel@tonic-gate 		log_error(LOG_WARNING,
895ca948064Ssl 		    "%s: Couldn't fork to execute method %s: %s\n",
896ca948064Ssl 		    inst->ri_i.i_fmri, method, strerror(forkerr));
897ca948064Ssl 
898dfe57350Sjeanm 		restarter_free_method_context(mcp);
8997c478bd9Sstevel@tonic-gate 		goto out;
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	/*
9047c478bd9Sstevel@tonic-gate 	 * Get the contract id, decide whether it is primary or transient, and
9057c478bd9Sstevel@tonic-gate 	 * stash it in inst & the repository.
9067c478bd9Sstevel@tonic-gate 	 */
9077c478bd9Sstevel@tonic-gate 	method_store_contract(inst, type, &ctid);
908dfe57350Sjeanm 	atomic_add_16(&storing_contract, -1);
909dfe57350Sjeanm 
910dfe57350Sjeanm 	restarter_free_method_context(mcp);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	/*
9137c478bd9Sstevel@tonic-gate 	 * Similarly for the start method PID.
9147c478bd9Sstevel@tonic-gate 	 */
9157c478bd9Sstevel@tonic-gate 	if (type == METHOD_START && !inst->ri_mi_deleted)
9167c478bd9Sstevel@tonic-gate 		(void) libscf_write_start_pid(inst->ri_m_inst, pid);
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	if (instance_is_wait_style(inst) && type == METHOD_START) {
9197c478bd9Sstevel@tonic-gate 		/* Wait style instances don't get timeouts on start methods. */
9207c478bd9Sstevel@tonic-gate 		if (wait_register(pid, inst->ri_i.i_fmri, 1, 0)) {
9217c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
9227c478bd9Sstevel@tonic-gate 			    "%s: couldn't register %ld for wait\n",
9237c478bd9Sstevel@tonic-gate 			    inst->ri_i.i_fmri, pid);
9247c478bd9Sstevel@tonic-gate 			result = EFAULT;
9257c478bd9Sstevel@tonic-gate 			goto contract_out;
9267c478bd9Sstevel@tonic-gate 		}
9277c478bd9Sstevel@tonic-gate 		write_status(inst, mname, 0);
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	} else {
9307c478bd9Sstevel@tonic-gate 		int r, err;
9317c478bd9Sstevel@tonic-gate 		time_t start_time;
9327c478bd9Sstevel@tonic-gate 		time_t end_time;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 		/*
9357c478bd9Sstevel@tonic-gate 		 * Because on upgrade/live-upgrade we may have no chance
9367c478bd9Sstevel@tonic-gate 		 * to override faulty timeout values on the way to
9377c478bd9Sstevel@tonic-gate 		 * manifest import, all services on the path to manifest
9387c478bd9Sstevel@tonic-gate 		 * import are treated the same as INFINITE timeout services.
9397c478bd9Sstevel@tonic-gate 		 */
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		start_time = time(NULL);
9427c478bd9Sstevel@tonic-gate 		if (timeout != METHOD_TIMEOUT_INFINITE && !is_timeout_ovr(inst))
9437c478bd9Sstevel@tonic-gate 			timeout_insert(inst, ctid, timeout);
9447c478bd9Sstevel@tonic-gate 		else
9457c478bd9Sstevel@tonic-gate 			timeout = METHOD_TIMEOUT_INFINITE;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 		/* Unlock the instance while waiting for the method. */
9487c478bd9Sstevel@tonic-gate 		MUTEX_UNLOCK(&inst->ri_lock);
9497c478bd9Sstevel@tonic-gate 
95077c20ef8Sacruz 		do {
951*5f8fc372SToomas Soome 			r = waitpid(pid, &ret_status, 0);
95277c20ef8Sacruz 		} while (r == -1 && errno == EINTR);
9537c478bd9Sstevel@tonic-gate 		if (r == -1)
9547c478bd9Sstevel@tonic-gate 			err = errno;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 		/* Re-grab the lock. */
9577c478bd9Sstevel@tonic-gate 		inst = inst_lookup_by_id(id);
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 		/*
9607c478bd9Sstevel@tonic-gate 		 * inst can't be removed, as the removal thread waits
9617c478bd9Sstevel@tonic-gate 		 * for completion of this one.
9627c478bd9Sstevel@tonic-gate 		 */
9637c478bd9Sstevel@tonic-gate 		assert(inst != NULL);
9647c478bd9Sstevel@tonic-gate 		*instp = inst;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 		if (inst->ri_timeout != NULL && inst->ri_timeout->te_fired)
9677c478bd9Sstevel@tonic-gate 			timeout_fired = 1;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 		timeout_remove(inst, ctid);
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 		log_framework(LOG_DEBUG,
9727c478bd9Sstevel@tonic-gate 		    "%s method for %s exited with status %d.\n", mname,
9737c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_fmri, WEXITSTATUS(ret_status));
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 		if (r == -1) {
9767c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
9777c478bd9Sstevel@tonic-gate 			    "Couldn't waitpid() for %s method of %s (%s).\n",
9787c478bd9Sstevel@tonic-gate 			    mname, inst->ri_i.i_fmri, strerror(err));
9797c478bd9Sstevel@tonic-gate 			result = EFAULT;
9807c478bd9Sstevel@tonic-gate 			goto contract_out;
9817c478bd9Sstevel@tonic-gate 		}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 		if (type == METHOD_START)
9847c478bd9Sstevel@tonic-gate 			write_status(inst, mname, ret_status);
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 		/* return ERANGE if this service doesn't retry on timeout */
9877c478bd9Sstevel@tonic-gate 		if (timeout_fired == 1 && timeout_retry == 0) {
9887c478bd9Sstevel@tonic-gate 			result = ERANGE;
9897c478bd9Sstevel@tonic-gate 			goto contract_out;
9907c478bd9Sstevel@tonic-gate 		}
9917c478bd9Sstevel@tonic-gate 
992f18d8787SJerry Jelinek 		if (!WIFEXITED(ret_status) &&
993f18d8787SJerry Jelinek 		    WEXITSTATUS(ret_status) != SMF_EXIT_NODAEMON) {
9947c478bd9Sstevel@tonic-gate 			/*
9957c478bd9Sstevel@tonic-gate 			 * If method didn't exit itself (it was killed by an
9967c478bd9Sstevel@tonic-gate 			 * external entity, etc.), consider the entire
9977c478bd9Sstevel@tonic-gate 			 * method_run as failed.
9987c478bd9Sstevel@tonic-gate 			 */
9997c478bd9Sstevel@tonic-gate 			if (WIFSIGNALED(ret_status)) {
10007c478bd9Sstevel@tonic-gate 				char buf[SIG2STR_MAX];
10017c478bd9Sstevel@tonic-gate 				(void) sig2str(WTERMSIG(ret_status), buf);
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "%s: Method \"%s\" "
10047c478bd9Sstevel@tonic-gate 				    "failed due to signal %s.\n",
10057c478bd9Sstevel@tonic-gate 				    inst->ri_i.i_fmri, method, buf);
10067c478bd9Sstevel@tonic-gate 				log_instance(inst, B_TRUE, "Method \"%s\" "
10075ca87c7fSlianep 				    "failed due to signal %s.", mname, buf);
10087c478bd9Sstevel@tonic-gate 			} else {
10097c478bd9Sstevel@tonic-gate 				log_error(LOG_WARNING, "%s: Method \"%s\" "
10107c478bd9Sstevel@tonic-gate 				    "failed with exit status %d.\n",
10117c478bd9Sstevel@tonic-gate 				    inst->ri_i.i_fmri, method,
10127c478bd9Sstevel@tonic-gate 				    WEXITSTATUS(ret_status));
10137c478bd9Sstevel@tonic-gate 				log_instance(inst, B_TRUE, "Method \"%s\" "
10145ca87c7fSlianep 				    "failed with exit status %d.", mname,
10157c478bd9Sstevel@tonic-gate 				    WEXITSTATUS(ret_status));
10167c478bd9Sstevel@tonic-gate 			}
10177c478bd9Sstevel@tonic-gate 			result = EAGAIN;
10187c478bd9Sstevel@tonic-gate 			goto contract_out;
10197c478bd9Sstevel@tonic-gate 		}
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 		*exit_code = WEXITSTATUS(ret_status);
1022f18d8787SJerry Jelinek 		if (*exit_code != SMF_EXIT_OK &&
1023f18d8787SJerry Jelinek 		    *exit_code != SMF_EXIT_NODAEMON) {
10247c478bd9Sstevel@tonic-gate 			log_error(LOG_WARNING,
10257c478bd9Sstevel@tonic-gate 			    "%s: Method \"%s\" failed with exit status %d.\n",
10267c478bd9Sstevel@tonic-gate 			    inst->ri_i.i_fmri, method, WEXITSTATUS(ret_status));
10277c478bd9Sstevel@tonic-gate 		}
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 		log_instance(inst, B_TRUE, "Method \"%s\" exited with status "
10305ca87c7fSlianep 		    "%d.", mname, *exit_code);
10317c478bd9Sstevel@tonic-gate 
1032f18d8787SJerry Jelinek 		/* Note: we will take this path for SMF_EXIT_NODAEMON */
1033f18d8787SJerry Jelinek 		if (*exit_code != SMF_EXIT_OK)
10347c478bd9Sstevel@tonic-gate 			goto contract_out;
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 		end_time = time(NULL);
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 		/* Give service contract remaining seconds to empty */
10397c478bd9Sstevel@tonic-gate 		if (timeout != METHOD_TIMEOUT_INFINITE)
10407c478bd9Sstevel@tonic-gate 			timeout -= (end_time - start_time);
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate assured_kill:
10447c478bd9Sstevel@tonic-gate 	/*
10457c478bd9Sstevel@tonic-gate 	 * For stop methods, assure that the service contract has emptied
10467c478bd9Sstevel@tonic-gate 	 * before returning.
10477c478bd9Sstevel@tonic-gate 	 */
10487c478bd9Sstevel@tonic-gate 	if (type == METHOD_STOP && (!instance_is_transient_style(inst)) &&
10497c478bd9Sstevel@tonic-gate 	    !(contract_is_empty(inst->ri_i.i_primary_ctid))) {
10504d53c7adSDan Price 		int times = 0;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 		if (timeout != METHOD_TIMEOUT_INFINITE)
10537c478bd9Sstevel@tonic-gate 			timeout_insert(inst, inst->ri_i.i_primary_ctid,
10547c478bd9Sstevel@tonic-gate 			    timeout);
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 		for (;;) {
10574d53c7adSDan Price 			/*
10584d53c7adSDan Price 			 * Check frequently at first, then back off.  This
10594d53c7adSDan Price 			 * keeps startd from idling while shutting down.
10604d53c7adSDan Price 			 */
10614d53c7adSDan Price 			if (times < 20) {
10624d53c7adSDan Price 				(void) poll(NULL, 0, 5);
10634d53c7adSDan Price 				times++;
10644d53c7adSDan Price 			} else {
10654d53c7adSDan Price 				(void) poll(NULL, 0, 100);
10664d53c7adSDan Price 			}
1067dfe57350Sjeanm 			if (contract_is_empty(inst->ri_i.i_primary_ctid))
10687c478bd9Sstevel@tonic-gate 				break;
10697c478bd9Sstevel@tonic-gate 		}
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 		if (timeout != METHOD_TIMEOUT_INFINITE)
10727c478bd9Sstevel@tonic-gate 			if (inst->ri_timeout->te_fired)
10737c478bd9Sstevel@tonic-gate 				result = EFAULT;
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 		timeout_remove(inst, inst->ri_i.i_primary_ctid);
10767c478bd9Sstevel@tonic-gate 	}
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate contract_out:
1079f18d8787SJerry Jelinek 	/*
1080f18d8787SJerry Jelinek 	 * Abandon contracts for transient methods, methods that exit with
1081f18d8787SJerry Jelinek 	 * SMF_EXIT_NODAEMON & methods that fail.
1082f18d8787SJerry Jelinek 	 */
10837c478bd9Sstevel@tonic-gate 	transient = method_is_transient(inst, type);
1084f18d8787SJerry Jelinek 	if ((transient || *exit_code != SMF_EXIT_OK || result != 0) &&
10857c478bd9Sstevel@tonic-gate 	    (restarter_is_kill_method(method) < 0))
10867c478bd9Sstevel@tonic-gate 		method_remove_contract(inst, !transient, B_TRUE);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate out:
10897c478bd9Sstevel@tonic-gate 	if (ctfd >= 0)
10907c478bd9Sstevel@tonic-gate 		(void) close(ctfd);
10917c478bd9Sstevel@tonic-gate 	scf_snapshot_destroy(snap);
10927c478bd9Sstevel@tonic-gate 	free(method);
10937c478bd9Sstevel@tonic-gate 	return (result);
10947c478bd9Sstevel@tonic-gate }
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate /*
10977c478bd9Sstevel@tonic-gate  * The method thread executes a service method to effect a state transition.
10987c478bd9Sstevel@tonic-gate  * The next_state of info->sf_id should be non-_NONE on entrance, and it will
10997c478bd9Sstevel@tonic-gate  * be _NONE on exit (state will either be what next_state was (on success), or
11007c478bd9Sstevel@tonic-gate  * it will be _MAINT (on error)).
11017c478bd9Sstevel@tonic-gate  *
11027c478bd9Sstevel@tonic-gate  * There are six classes of methods to consider: start & other (stop, refresh)
11037c478bd9Sstevel@tonic-gate  * for each of "normal" services, wait services, and transient services.  For
11047c478bd9Sstevel@tonic-gate  * each, the method must be fetched from the repository & executed.  fork()ed
11057c478bd9Sstevel@tonic-gate  * methods must be waited on, except for the start method of wait services
11067c478bd9Sstevel@tonic-gate  * (which must be registered with the wait subsystem via wait_register()).  If
11077c478bd9Sstevel@tonic-gate  * the method succeeded (returned 0), then for start methods its contract
11087c478bd9Sstevel@tonic-gate  * should be recorded as the primary contract for the service.  For other
11097c478bd9Sstevel@tonic-gate  * methods, it should be abandoned.  If the method fails, then depending on
11107c478bd9Sstevel@tonic-gate  * the failure, either the method should be reexecuted or the service should
11117c478bd9Sstevel@tonic-gate  * be put into maintenance.  Either way the contract should be abandoned.
11127c478bd9Sstevel@tonic-gate  */
11137c478bd9Sstevel@tonic-gate void *
method_thread(void * arg)11147c478bd9Sstevel@tonic-gate method_thread(void *arg)
11157c478bd9Sstevel@tonic-gate {
11167c478bd9Sstevel@tonic-gate 	fork_info_t *info = arg;
11177c478bd9Sstevel@tonic-gate 	restarter_inst_t *inst;
11187c478bd9Sstevel@tonic-gate 	scf_handle_t	*local_handle;
11197c478bd9Sstevel@tonic-gate 	scf_instance_t	*s_inst = NULL;
11207c478bd9Sstevel@tonic-gate 	int r, exit_code;
11217c478bd9Sstevel@tonic-gate 	boolean_t retryable;
1122f6e214c7SGavin Maltby 	restarter_str_t reason;
11237c478bd9Sstevel@tonic-gate 
1124ab618543SJohn Levon 	(void) pthread_setname_np(pthread_self(), "method");
1125ab618543SJohn Levon 
11267c478bd9Sstevel@tonic-gate 	assert(0 <= info->sf_method_type && info->sf_method_type <= 2);
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	/* Get (and lock) the restarter_inst_t. */
11297c478bd9Sstevel@tonic-gate 	inst = inst_lookup_by_id(info->sf_id);
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	assert(inst->ri_method_thread != 0);
11327c478bd9Sstevel@tonic-gate 	assert(instance_in_transition(inst) == 1);
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	/*
11357c478bd9Sstevel@tonic-gate 	 * We cannot leave this function with inst in transition, because
11367c478bd9Sstevel@tonic-gate 	 * protocol.c withholds messages for inst otherwise.
11377c478bd9Sstevel@tonic-gate 	 */
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	log_framework(LOG_DEBUG, "method_thread() running %s method for %s.\n",
11407c478bd9Sstevel@tonic-gate 	    method_names[info->sf_method_type], inst->ri_i.i_fmri);
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	local_handle = libscf_handle_create_bound_loop();
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate rebind_retry:
11457c478bd9Sstevel@tonic-gate 	/* get scf_instance_t */
11467c478bd9Sstevel@tonic-gate 	switch (r = libscf_fmri_get_instance(local_handle, inst->ri_i.i_fmri,
11477c478bd9Sstevel@tonic-gate 	    &s_inst)) {
11487c478bd9Sstevel@tonic-gate 	case 0:
11497c478bd9Sstevel@tonic-gate 		break;
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	case ECONNABORTED:
11527c478bd9Sstevel@tonic-gate 		libscf_handle_rebind(local_handle);
11537c478bd9Sstevel@tonic-gate 		goto rebind_retry;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	case ENOENT:
11567c478bd9Sstevel@tonic-gate 		/*
11577c478bd9Sstevel@tonic-gate 		 * It's not there, but we need to call this so protocol.c
11587c478bd9Sstevel@tonic-gate 		 * doesn't think it's in transition anymore.
11597c478bd9Sstevel@tonic-gate 		 */
11607c478bd9Sstevel@tonic-gate 		(void) restarter_instance_update_states(local_handle, inst,
11617c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_state, RESTARTER_STATE_NONE, RERR_NONE,
1162f6e214c7SGavin Maltby 		    restarter_str_none);
11637c478bd9Sstevel@tonic-gate 		goto out;
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 	case EINVAL:
11667c478bd9Sstevel@tonic-gate 	case ENOTSUP:
11677c478bd9Sstevel@tonic-gate 	default:
11687c478bd9Sstevel@tonic-gate 		bad_error("libscf_fmri_get_instance", r);
11697c478bd9Sstevel@tonic-gate 	}
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	inst->ri_m_inst = s_inst;
11727c478bd9Sstevel@tonic-gate 	inst->ri_mi_deleted = B_FALSE;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate retry:
11757c478bd9Sstevel@tonic-gate 	if (info->sf_method_type == METHOD_START)
11767c478bd9Sstevel@tonic-gate 		log_transition(inst, START_REQUESTED);
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	r = method_run(&inst, info->sf_method_type, &exit_code);
11797c478bd9Sstevel@tonic-gate 
1180f18d8787SJerry Jelinek 	if (r == 0 &&
1181f18d8787SJerry Jelinek 	    (exit_code == SMF_EXIT_OK || exit_code == SMF_EXIT_NODAEMON)) {
11827c478bd9Sstevel@tonic-gate 		/* Success! */
11837c478bd9Sstevel@tonic-gate 		assert(inst->ri_i.i_next_state != RESTARTER_STATE_NONE);
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 		/*
11867c478bd9Sstevel@tonic-gate 		 * When a stop method succeeds, remove the primary contract of
11877c478bd9Sstevel@tonic-gate 		 * the service, unless we're going to offline, in which case
11887c478bd9Sstevel@tonic-gate 		 * retain the contract so we can transfer inherited contracts to
11897c478bd9Sstevel@tonic-gate 		 * the replacement service.
11907c478bd9Sstevel@tonic-gate 		 */
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 		if (info->sf_method_type == METHOD_STOP &&
11937c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_primary_ctid != 0) {
11947c478bd9Sstevel@tonic-gate 			if (inst->ri_i.i_next_state == RESTARTER_STATE_OFFLINE)
11957c478bd9Sstevel@tonic-gate 				inst->ri_i.i_primary_ctid_stopped = 1;
11967c478bd9Sstevel@tonic-gate 			else
11977c478bd9Sstevel@tonic-gate 				method_remove_contract(inst, B_TRUE, B_TRUE);
11987c478bd9Sstevel@tonic-gate 		}
1199f18d8787SJerry Jelinek 
1200f18d8787SJerry Jelinek 		/*
1201f18d8787SJerry Jelinek 		 * For methods that exit with SMF_EXIT_NODAEMON, we already
1202f18d8787SJerry Jelinek 		 * called method_remove_contract in method_run.
1203f18d8787SJerry Jelinek 		 */
1204f18d8787SJerry Jelinek 
12057c478bd9Sstevel@tonic-gate 		/*
12067c478bd9Sstevel@tonic-gate 		 * We don't care whether the handle was rebound because this is
12077c478bd9Sstevel@tonic-gate 		 * the last thing we do with it.
12087c478bd9Sstevel@tonic-gate 		 */
12097c478bd9Sstevel@tonic-gate 		(void) restarter_instance_update_states(local_handle, inst,
12107c478bd9Sstevel@tonic-gate 		    inst->ri_i.i_next_state, RESTARTER_STATE_NONE,
1211f6e214c7SGavin Maltby 		    info->sf_event_type, info->sf_reason);
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 		(void) update_fault_count(inst, FAULT_COUNT_RESET);
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 		goto out;
12167c478bd9Sstevel@tonic-gate 	}
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 	/* Failure.  Retry or go to maintenance. */
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	if (r != 0 && r != EAGAIN) {
12217c478bd9Sstevel@tonic-gate 		retryable = B_FALSE;
12227c478bd9Sstevel@tonic-gate 	} else {
12237c478bd9Sstevel@tonic-gate 		switch (exit_code) {
12247c478bd9Sstevel@tonic-gate 		case SMF_EXIT_ERR_CONFIG:
12257c478bd9Sstevel@tonic-gate 		case SMF_EXIT_ERR_NOSMF:
12267c478bd9Sstevel@tonic-gate 		case SMF_EXIT_ERR_PERM:
12277c478bd9Sstevel@tonic-gate 		case SMF_EXIT_ERR_FATAL:
12287c478bd9Sstevel@tonic-gate 			retryable = B_FALSE;
12297c478bd9Sstevel@tonic-gate 			break;
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 		default:
12327c478bd9Sstevel@tonic-gate 			retryable = B_TRUE;
12337c478bd9Sstevel@tonic-gate 		}
12347c478bd9Sstevel@tonic-gate 	}
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	if (retryable && update_fault_count(inst, FAULT_COUNT_INCR) != 1)
12377c478bd9Sstevel@tonic-gate 		goto retry;
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	/* maintenance */
12407c478bd9Sstevel@tonic-gate 	if (r == ELOOP)
12417c478bd9Sstevel@tonic-gate 		log_transition(inst, START_FAILED_REPEATEDLY);
12427c478bd9Sstevel@tonic-gate 	else if (r == ERANGE)
12437c478bd9Sstevel@tonic-gate 		log_transition(inst, START_FAILED_TIMEOUT_FATAL);
12447c478bd9Sstevel@tonic-gate 	else if (exit_code == SMF_EXIT_ERR_CONFIG)
12457c478bd9Sstevel@tonic-gate 		log_transition(inst, START_FAILED_CONFIGURATION);
12467c478bd9Sstevel@tonic-gate 	else if (exit_code == SMF_EXIT_ERR_FATAL)
12477c478bd9Sstevel@tonic-gate 		log_transition(inst, START_FAILED_FATAL);
12487c478bd9Sstevel@tonic-gate 	else
12497c478bd9Sstevel@tonic-gate 		log_transition(inst, START_FAILED_OTHER);
12507c478bd9Sstevel@tonic-gate 
1251f6e214c7SGavin Maltby 	if (r == ELOOP) {
1252f6e214c7SGavin Maltby 		reason = restarter_str_restarting_too_quickly;
1253f6e214c7SGavin Maltby 	} else if (retryable) {
1254f6e214c7SGavin Maltby 		reason = restarter_str_fault_threshold_reached;
1255f6e214c7SGavin Maltby 	} else {
1256f6e214c7SGavin Maltby 		reason = restarter_str_method_failed;
1257f6e214c7SGavin Maltby 	}
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	(void) restarter_instance_update_states(local_handle, inst,
12607c478bd9Sstevel@tonic-gate 	    RESTARTER_STATE_MAINT, RESTARTER_STATE_NONE, RERR_FAULT,
1261f6e214c7SGavin Maltby 	    reason);
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	if (!method_is_transient(inst, info->sf_method_type) &&
12647c478bd9Sstevel@tonic-gate 	    inst->ri_i.i_primary_ctid != 0)
12657c478bd9Sstevel@tonic-gate 		method_remove_contract(inst, B_TRUE, B_TRUE);
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate out:
12687c478bd9Sstevel@tonic-gate 	inst->ri_method_thread = 0;
1269addbbe95Srm 
1270addbbe95Srm 	/*
1271addbbe95Srm 	 * Unlock the mutex after broadcasting to avoid a race condition
1272addbbe95Srm 	 * with restarter_delete_inst() when the 'inst' structure is freed.
1273addbbe95Srm 	 */
12747c478bd9Sstevel@tonic-gate 	(void) pthread_cond_broadcast(&inst->ri_method_cv);
1275addbbe95Srm 	MUTEX_UNLOCK(&inst->ri_lock);
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	scf_instance_destroy(s_inst);
12787c478bd9Sstevel@tonic-gate 	scf_handle_destroy(local_handle);
12797c478bd9Sstevel@tonic-gate 	startd_free(info, sizeof (fork_info_t));
12807c478bd9Sstevel@tonic-gate 	return (NULL);
12817c478bd9Sstevel@tonic-gate }
1282