xref: /onnv-gate/usr/src/uts/common/contract/process.c (revision 8921:bfe44bc5fea2)
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
54845Svikram  * Common Development and Distribution License (the "License").
64845Svikram  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8921SVamsi.Krishna@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/mutex.h>
270Sstevel@tonic-gate #include <sys/debug.h>
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/kmem.h>
310Sstevel@tonic-gate #include <sys/thread.h>
320Sstevel@tonic-gate #include <sys/id_space.h>
330Sstevel@tonic-gate #include <sys/avl.h>
340Sstevel@tonic-gate #include <sys/list.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #include <sys/proc.h>
370Sstevel@tonic-gate #include <sys/contract.h>
380Sstevel@tonic-gate #include <sys/contract_impl.h>
390Sstevel@tonic-gate #include <sys/contract/process.h>
400Sstevel@tonic-gate #include <sys/contract/process_impl.h>
410Sstevel@tonic-gate #include <sys/cmn_err.h>
420Sstevel@tonic-gate #include <sys/nvpair.h>
430Sstevel@tonic-gate #include <sys/policy.h>
446073Sacruz #include <sys/refstr.h>
456073Sacruz #include <sys/sunddi.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * Process Contracts
490Sstevel@tonic-gate  * -----------------
500Sstevel@tonic-gate  *
510Sstevel@tonic-gate  * Generally speaking, a process contract is a contract between a
520Sstevel@tonic-gate  * process and a set of its descendent processes.  In some cases, when
530Sstevel@tonic-gate  * the child processes outlive the author of the contract, the contract
540Sstevel@tonic-gate  * may be held by (and therefore be between the child processes and) a
550Sstevel@tonic-gate  * successor process which adopts the contract after the death of the
560Sstevel@tonic-gate  * original author.
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  * The process contract adds two new concepts to the Solaris process
590Sstevel@tonic-gate  * model.  The first is that a process contract forms a rigid fault
600Sstevel@tonic-gate  * boundary around a set of processes.  Hardware, software, and even
610Sstevel@tonic-gate  * administrator errors impacting a process in a process contract
620Sstevel@tonic-gate  * generate specific events and can be requested to atomically shutdown
630Sstevel@tonic-gate  * all processes in the contract.  The second is that a process
640Sstevel@tonic-gate  * contract is a process collective whose leader is not a member of the
650Sstevel@tonic-gate  * collective.  This means that the leader can reliably react to events
660Sstevel@tonic-gate  * in the collective, and may also act upon the collective without
670Sstevel@tonic-gate  * special casing itself.
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * A composite outcome of these two concepts is that we can now create
700Sstevel@tonic-gate  * a tree of process contracts, rooted at init(1M), which represent
710Sstevel@tonic-gate  * services and subservices that are reliably observed and can be
720Sstevel@tonic-gate  * restarted when fatal errors occur.  The service management framework
730Sstevel@tonic-gate  * (SMF) realizes this structure.
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  * For more details, see the "restart agreements" case, PSARC 2003/193.
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  * There are four sets of routines in this file: the process contract
780Sstevel@tonic-gate  * standard template operations, the process contract standard contract
790Sstevel@tonic-gate  * operations, a couple routines used only by the contract subsystem to
800Sstevel@tonic-gate  * handle process contracts' unique role as a temporary holder of
810Sstevel@tonic-gate  * abandoned contracts, and the interfaces which allow the system to
820Sstevel@tonic-gate  * create and act upon process contracts.  The first two are defined by
830Sstevel@tonic-gate  * the contracts framework and won't be discussed further.  As for the
840Sstevel@tonic-gate  * remaining two:
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  * Special framework interfaces
870Sstevel@tonic-gate  * ----------------------------
880Sstevel@tonic-gate  *
890Sstevel@tonic-gate  * contract_process_accept - determines if a process contract is a
900Sstevel@tonic-gate  *   regent, i.e. if it can inherit other contracts.
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * contract_process_take - tells a regent process contract to inherit
930Sstevel@tonic-gate  *   an abandoned contract
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  * contract_process_adopt - tells a regent process contract that a
960Sstevel@tonic-gate  *   contract it has inherited is being adopted by a process.
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  * Process contract interfaces
990Sstevel@tonic-gate  * ---------------------------
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  * contract_process_fork - called when a process is created; adds the
1020Sstevel@tonic-gate  *   new process to an existing contract or to a newly created one.
1030Sstevel@tonic-gate  *
1040Sstevel@tonic-gate  * contract_process_exit - called when a process exits
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * contract_process_core - called when a process would have dumped core
1070Sstevel@tonic-gate  *   (even if a core file wasn't generated)
1080Sstevel@tonic-gate  *
1090Sstevel@tonic-gate  * contract_process_hwerr - called when a process was killed because of
1100Sstevel@tonic-gate  *   an uncorrectable hardware error
1110Sstevel@tonic-gate  *
1120Sstevel@tonic-gate  * contract_process_sig - called when a process was killed by a fatal
1130Sstevel@tonic-gate  *   signal sent by a process in another process contract
1140Sstevel@tonic-gate  *
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate ct_type_t *process_type;
1180Sstevel@tonic-gate ctmpl_process_t *sys_process_tmpl;
1196073Sacruz refstr_t *conp_svc_aux_default;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate  * Macro predicates for determining when events should be sent and how.
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate #define	EVSENDP(ctp, flag) \
1250Sstevel@tonic-gate 	((ctp->conp_contract.ct_ev_info | ctp->conp_contract.ct_ev_crit) & flag)
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate #define	EVINFOP(ctp, flag) \
1280Sstevel@tonic-gate 	((ctp->conp_contract.ct_ev_crit & flag) == 0)
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate #define	EVFATALP(ctp, flag) \
1310Sstevel@tonic-gate 	(ctp->conp_ev_fatal & flag)
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * Process contract template implementation
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate  * ctmpl_process_dup
1400Sstevel@tonic-gate  *
1410Sstevel@tonic-gate  * The process contract template dup entry point.  Other than the
1420Sstevel@tonic-gate  * to-be-subsumed contract, which must be held, this simply copies all
1430Sstevel@tonic-gate  * the fields of the original.
1440Sstevel@tonic-gate  */
1450Sstevel@tonic-gate static struct ct_template *
ctmpl_process_dup(struct ct_template * template)1460Sstevel@tonic-gate ctmpl_process_dup(struct ct_template *template)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	ctmpl_process_t *new;
1490Sstevel@tonic-gate 	ctmpl_process_t *old = template->ctmpl_data;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	new = kmem_alloc(sizeof (ctmpl_process_t), KM_SLEEP);
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	ctmpl_copy(&new->ctp_ctmpl, template);
1540Sstevel@tonic-gate 	new->ctp_ctmpl.ctmpl_data = new;
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate 	new->ctp_subsume = old->ctp_subsume;
1570Sstevel@tonic-gate 	if (new->ctp_subsume)
1580Sstevel@tonic-gate 		contract_hold(new->ctp_subsume);
1590Sstevel@tonic-gate 	new->ctp_params = old->ctp_params;
1600Sstevel@tonic-gate 	new->ctp_ev_fatal = old->ctp_ev_fatal;
1616073Sacruz 	new->ctp_svc_fmri = old->ctp_svc_fmri;
1626073Sacruz 	if (new->ctp_svc_fmri != NULL) {
1636073Sacruz 		refstr_hold(new->ctp_svc_fmri);
1646073Sacruz 	}
1656073Sacruz 	new->ctp_svc_aux = old->ctp_svc_aux;
1666073Sacruz 	if (new->ctp_svc_aux != NULL) {
1676073Sacruz 		refstr_hold(new->ctp_svc_aux);
1686073Sacruz 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	return (&new->ctp_ctmpl);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1746073Sacruz  * ctmpl_process_free
1750Sstevel@tonic-gate  *
1760Sstevel@tonic-gate  * The process contract template free entry point.  Just releases a
1770Sstevel@tonic-gate  * to-be-subsumed contract and frees the template.
1780Sstevel@tonic-gate  */
1790Sstevel@tonic-gate static void
ctmpl_process_free(struct ct_template * template)1800Sstevel@tonic-gate ctmpl_process_free(struct ct_template *template)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate 	ctmpl_process_t *ctp = template->ctmpl_data;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	if (ctp->ctp_subsume)
1850Sstevel@tonic-gate 		contract_rele(ctp->ctp_subsume);
1866073Sacruz 	if (ctp->ctp_svc_fmri != NULL) {
1876073Sacruz 		refstr_rele(ctp->ctp_svc_fmri);
1886073Sacruz 	}
1896073Sacruz 	if (ctp->ctp_svc_aux != NULL) {
1906073Sacruz 		refstr_rele(ctp->ctp_svc_aux);
1916073Sacruz 	}
1920Sstevel@tonic-gate 	kmem_free(template, sizeof (ctmpl_process_t));
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate /*
1960Sstevel@tonic-gate  * SAFE_EV is the set of events which a non-privileged process is
1970Sstevel@tonic-gate  * allowed to make critical but not fatal or if the PGRPONLY parameter
1980Sstevel@tonic-gate  * is set.  EXCESS tells us if "value", a critical event set, requires
1990Sstevel@tonic-gate  * additional privilege given the template "ctp".
2000Sstevel@tonic-gate  */
2010Sstevel@tonic-gate #define	SAFE_EV			(CT_PR_EV_EMPTY)
2020Sstevel@tonic-gate #define	EXCESS(ctp, value)	\
2030Sstevel@tonic-gate 	(((value) & ~((ctp)->ctp_ev_fatal | SAFE_EV)) || \
2040Sstevel@tonic-gate 	(((value) & ~SAFE_EV) && (ctp->ctp_params & CT_PR_PGRPONLY)))
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate  * ctmpl_process_set
2080Sstevel@tonic-gate  *
2090Sstevel@tonic-gate  * The process contract template set entry point.  None of the terms
2100Sstevel@tonic-gate  * may be unconditionally set, and setting the parameters or fatal
2110Sstevel@tonic-gate  * event set may result in events being implicitly removed from to the
2120Sstevel@tonic-gate  * critical event set and added to the informative event set.  The
2130Sstevel@tonic-gate  * (admittedly subtle) reason we implicitly change the critical event
2140Sstevel@tonic-gate  * set when the parameter or fatal event set is modified but not the
2150Sstevel@tonic-gate  * other way around is because a change to the critical event set only
2160Sstevel@tonic-gate  * affects the contract's owner, whereas a change to the parameter set
2170Sstevel@tonic-gate  * and fatal set can affect the execution of the application running in
2180Sstevel@tonic-gate  * the contract (and should therefore be only made explicitly).  We
2190Sstevel@tonic-gate  * allow implicit changes at all so that setting contract terms doesn't
2200Sstevel@tonic-gate  * become a complex dance dependent on the template's initial state and
2210Sstevel@tonic-gate  * the desired terms.
2220Sstevel@tonic-gate  */
2230Sstevel@tonic-gate static int
ctmpl_process_set(struct ct_template * tmpl,ct_kparam_t * kparam,const cred_t * cr)2247937SAntonello.Cruz@Sun.COM ctmpl_process_set(struct ct_template *tmpl, ct_kparam_t *kparam,
2257937SAntonello.Cruz@Sun.COM     const cred_t *cr)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	ctmpl_process_t *ctp = tmpl->ctmpl_data;
2287937SAntonello.Cruz@Sun.COM 	ct_param_t *param = &kparam->param;
2290Sstevel@tonic-gate 	contract_t *ct;
2300Sstevel@tonic-gate 	int error;
2316073Sacruz 	uint64_t param_value;
2326073Sacruz 	char *str_value;
2330Sstevel@tonic-gate 
2346073Sacruz 	if ((param->ctpm_id == CTPP_SVC_FMRI) ||
2356073Sacruz 	    (param->ctpm_id == CTPP_CREATOR_AUX)) {
2367937SAntonello.Cruz@Sun.COM 		str_value = (char *)kparam->ctpm_kbuf;
2376073Sacruz 		str_value[param->ctpm_size - 1] = '\0';
2386073Sacruz 	} else {
2396196Sacruz 		if (param->ctpm_size < sizeof (uint64_t))
2406196Sacruz 			return (EINVAL);
2417937SAntonello.Cruz@Sun.COM 		param_value = *(uint64_t *)kparam->ctpm_kbuf;
2426073Sacruz 		/*
2436073Sacruz 		 * No process contract parameters are > 32 bits.
2446073Sacruz 		 * Unless it is a string.
2456073Sacruz 		 */
2466073Sacruz 		if (param_value & ~UINT32_MAX)
2476073Sacruz 			return (EINVAL);
2486073Sacruz 	}
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	switch (param->ctpm_id) {
2510Sstevel@tonic-gate 	case CTPP_SUBSUME:
2526073Sacruz 		if (param_value != 0) {
2530Sstevel@tonic-gate 			/*
2540Sstevel@tonic-gate 			 * Ensure that the contract exists, that we
2550Sstevel@tonic-gate 			 * hold the contract, and that the contract is
2560Sstevel@tonic-gate 			 * empty.
2570Sstevel@tonic-gate 			 */
2586073Sacruz 			ct = contract_type_ptr(process_type, param_value,
2590Sstevel@tonic-gate 			    curproc->p_zone->zone_uniqid);
2600Sstevel@tonic-gate 			if (ct == NULL)
2610Sstevel@tonic-gate 				return (ESRCH);
2620Sstevel@tonic-gate 			if (ct->ct_owner != curproc) {
2630Sstevel@tonic-gate 				contract_rele(ct);
2640Sstevel@tonic-gate 				return (EACCES);
2650Sstevel@tonic-gate 			}
2660Sstevel@tonic-gate 			if (((cont_process_t *)ct->ct_data)->conp_nmembers) {
2670Sstevel@tonic-gate 				contract_rele(ct);
2680Sstevel@tonic-gate 				return (ENOTEMPTY);
2690Sstevel@tonic-gate 			}
2700Sstevel@tonic-gate 		} else {
2710Sstevel@tonic-gate 			ct = NULL;
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 		if (ctp->ctp_subsume)
2740Sstevel@tonic-gate 			contract_rele(ctp->ctp_subsume);
2750Sstevel@tonic-gate 		ctp->ctp_subsume = ct;
2760Sstevel@tonic-gate 		break;
2770Sstevel@tonic-gate 	case CTPP_PARAMS:
2786073Sacruz 		if (param_value & ~CT_PR_ALLPARAM)
2790Sstevel@tonic-gate 			return (EINVAL);
2806073Sacruz 		ctp->ctp_params = param_value;
2810Sstevel@tonic-gate 		/*
2820Sstevel@tonic-gate 		 * If an unprivileged process requests that
2830Sstevel@tonic-gate 		 * CT_PR_PGRPONLY be set, remove any unsafe events from
2840Sstevel@tonic-gate 		 * the critical event set and add them to the
2850Sstevel@tonic-gate 		 * informative event set.
2860Sstevel@tonic-gate 		 */
2870Sstevel@tonic-gate 		if ((ctp->ctp_params & CT_PR_PGRPONLY) &&
2880Sstevel@tonic-gate 		    EXCESS(ctp, tmpl->ctmpl_ev_crit) &&
2890Sstevel@tonic-gate 		    !secpolicy_contract_event_choice(cr)) {
2900Sstevel@tonic-gate 			tmpl->ctmpl_ev_info |= (tmpl->ctmpl_ev_crit & ~SAFE_EV);
2910Sstevel@tonic-gate 			tmpl->ctmpl_ev_crit &= SAFE_EV;
2920Sstevel@tonic-gate 		}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 		break;
2956073Sacruz 	case CTPP_SVC_FMRI:
2966073Sacruz 		if (error = secpolicy_contract_identity(cr))
2976073Sacruz 			return (error);
2986073Sacruz 		if (ctp->ctp_svc_fmri != NULL)
2996073Sacruz 			refstr_rele(ctp->ctp_svc_fmri);
3006073Sacruz 		if (strcmp(CT_PR_SVC_DEFAULT, str_value) == 0)
3016073Sacruz 			ctp->ctp_svc_fmri = NULL;
3026073Sacruz 		else
3036073Sacruz 			ctp->ctp_svc_fmri =
3046073Sacruz 			    refstr_alloc(str_value);
3056073Sacruz 		break;
3066073Sacruz 	case CTPP_CREATOR_AUX:
3076073Sacruz 		if (ctp->ctp_svc_aux != NULL)
3086073Sacruz 			refstr_rele(ctp->ctp_svc_aux);
3096073Sacruz 		if (param->ctpm_size == 1) /* empty string */
3106073Sacruz 			ctp->ctp_svc_aux = NULL;
3116073Sacruz 		else
3126073Sacruz 			ctp->ctp_svc_aux =
3136073Sacruz 			    refstr_alloc(str_value);
3146073Sacruz 		break;
3150Sstevel@tonic-gate 	case CTP_EV_CRITICAL:
3160Sstevel@tonic-gate 		/*
3170Sstevel@tonic-gate 		 * We simply don't allow adding events to the critical
3180Sstevel@tonic-gate 		 * event set which aren't permitted by our policy or by
3190Sstevel@tonic-gate 		 * privilege.
3200Sstevel@tonic-gate 		 */
3216073Sacruz 		if (EXCESS(ctp, param_value) &&
3220Sstevel@tonic-gate 		    (error = secpolicy_contract_event(cr)) != 0)
3230Sstevel@tonic-gate 			return (error);
3246073Sacruz 		tmpl->ctmpl_ev_crit = param_value;
3250Sstevel@tonic-gate 		break;
3260Sstevel@tonic-gate 	case CTPP_EV_FATAL:
3276073Sacruz 		if (param_value & ~CT_PR_ALLFATAL)
3280Sstevel@tonic-gate 			return (EINVAL);
3296073Sacruz 		ctp->ctp_ev_fatal = param_value;
3300Sstevel@tonic-gate 		/*
3310Sstevel@tonic-gate 		 * Check to see if an unprivileged process is
3320Sstevel@tonic-gate 		 * requesting that events be removed from the fatal
3330Sstevel@tonic-gate 		 * event set which are still in the critical event set.
3340Sstevel@tonic-gate 		 */
3350Sstevel@tonic-gate 		if (EXCESS(ctp, tmpl->ctmpl_ev_crit) &&
3360Sstevel@tonic-gate 		    !secpolicy_contract_event_choice(cr)) {
3370Sstevel@tonic-gate 			int allowed =
3380Sstevel@tonic-gate 			    SAFE_EV | (ctp->ctp_params & CT_PR_PGRPONLY) ?
3390Sstevel@tonic-gate 			    0 : ctp->ctp_ev_fatal;
3400Sstevel@tonic-gate 			tmpl->ctmpl_ev_info |= (tmpl->ctmpl_ev_crit & ~allowed);
3410Sstevel@tonic-gate 			tmpl->ctmpl_ev_crit &= allowed;
3420Sstevel@tonic-gate 		}
3430Sstevel@tonic-gate 		break;
3440Sstevel@tonic-gate 	default:
3450Sstevel@tonic-gate 		return (EINVAL);
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	return (0);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate /*
3520Sstevel@tonic-gate  * ctmpl_process_get
3530Sstevel@tonic-gate  *
3540Sstevel@tonic-gate  * The process contract template get entry point.  Simply fetches and
3550Sstevel@tonic-gate  * returns the requested term.
3560Sstevel@tonic-gate  */
3570Sstevel@tonic-gate static int
ctmpl_process_get(struct ct_template * template,ct_kparam_t * kparam)3587937SAntonello.Cruz@Sun.COM ctmpl_process_get(struct ct_template *template, ct_kparam_t *kparam)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	ctmpl_process_t *ctp = template->ctmpl_data;
3617937SAntonello.Cruz@Sun.COM 	ct_param_t *param = &kparam->param;
3627937SAntonello.Cruz@Sun.COM 	uint64_t *param_value = kparam->ctpm_kbuf;
3630Sstevel@tonic-gate 
3646196Sacruz 	if (param->ctpm_id == CTPP_SUBSUME ||
3656196Sacruz 	    param->ctpm_id == CTPP_PARAMS ||
3666196Sacruz 	    param->ctpm_id == CTPP_EV_FATAL) {
3676196Sacruz 		if (param->ctpm_size < sizeof (uint64_t))
3686196Sacruz 			return (EINVAL);
3697937SAntonello.Cruz@Sun.COM 		kparam->ret_size = sizeof (uint64_t);
3706196Sacruz 	}
3716196Sacruz 
3720Sstevel@tonic-gate 	switch (param->ctpm_id) {
3730Sstevel@tonic-gate 	case CTPP_SUBSUME:
3746073Sacruz 		*param_value = ctp->ctp_subsume ?
3750Sstevel@tonic-gate 		    ctp->ctp_subsume->ct_id : 0;
3760Sstevel@tonic-gate 		break;
3770Sstevel@tonic-gate 	case CTPP_PARAMS:
3786073Sacruz 		*param_value = ctp->ctp_params;
3796073Sacruz 		break;
3806073Sacruz 	case CTPP_SVC_FMRI:
3816073Sacruz 		if (ctp->ctp_svc_fmri == NULL) {
3827937SAntonello.Cruz@Sun.COM 			kparam->ret_size =
3837937SAntonello.Cruz@Sun.COM 			    strlcpy((char *)kparam->ctpm_kbuf,
3846073Sacruz 			    CT_PR_SVC_DEFAULT, param->ctpm_size);
3856073Sacruz 		} else {
3867937SAntonello.Cruz@Sun.COM 			kparam->ret_size =
3877937SAntonello.Cruz@Sun.COM 			    strlcpy((char *)kparam->ctpm_kbuf,
3886073Sacruz 			    refstr_value(ctp->ctp_svc_fmri), param->ctpm_size);
3896073Sacruz 		}
3907937SAntonello.Cruz@Sun.COM 		kparam->ret_size++;
3916073Sacruz 		break;
3926073Sacruz 	case CTPP_CREATOR_AUX:
3936073Sacruz 		if (ctp->ctp_svc_aux == NULL) {
3947937SAntonello.Cruz@Sun.COM 			kparam->ret_size =
3957937SAntonello.Cruz@Sun.COM 			    strlcpy((char *)kparam->ctpm_kbuf,
3966073Sacruz 			    refstr_value(conp_svc_aux_default),
3976073Sacruz 			    param->ctpm_size);
3986073Sacruz 		} else {
3997937SAntonello.Cruz@Sun.COM 			kparam->ret_size =
4007937SAntonello.Cruz@Sun.COM 			    strlcpy((char *)kparam->ctpm_kbuf,
4016073Sacruz 			    refstr_value(ctp->ctp_svc_aux), param->ctpm_size);
4026073Sacruz 		}
4037937SAntonello.Cruz@Sun.COM 		kparam->ret_size++;
4040Sstevel@tonic-gate 		break;
4050Sstevel@tonic-gate 	case CTPP_EV_FATAL:
4066073Sacruz 		*param_value = ctp->ctp_ev_fatal;
4070Sstevel@tonic-gate 		break;
4080Sstevel@tonic-gate 	default:
4090Sstevel@tonic-gate 		return (EINVAL);
4100Sstevel@tonic-gate 	}
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	return (0);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate static ctmplops_t ctmpl_process_ops = {
4160Sstevel@tonic-gate 	ctmpl_process_dup,		/* ctop_dup */
4170Sstevel@tonic-gate 	ctmpl_process_free,		/* ctop_free */
4180Sstevel@tonic-gate 	ctmpl_process_set,		/* ctop_set */
4190Sstevel@tonic-gate 	ctmpl_process_get,		/* ctop_get */
4200Sstevel@tonic-gate 	ctmpl_create_inval,		/* ctop_create */
4210Sstevel@tonic-gate 	CT_PR_ALLEVENT
4220Sstevel@tonic-gate };
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate /*
4260Sstevel@tonic-gate  * Process contract implementation
4270Sstevel@tonic-gate  */
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate /*
4300Sstevel@tonic-gate  * ctmpl_process_default
4310Sstevel@tonic-gate  *
4320Sstevel@tonic-gate  * The process contract default template entry point.  Creates a
4330Sstevel@tonic-gate  * process contract template with no parameters set, with informative
4340Sstevel@tonic-gate  * core and signal events, critical empty and hwerr events, and fatal
4350Sstevel@tonic-gate  * hwerr events.
4360Sstevel@tonic-gate  */
4370Sstevel@tonic-gate static ct_template_t *
contract_process_default(void)4380Sstevel@tonic-gate contract_process_default(void)
4390Sstevel@tonic-gate {
4400Sstevel@tonic-gate 	ctmpl_process_t *new;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	new = kmem_alloc(sizeof (ctmpl_process_t), KM_SLEEP);
4430Sstevel@tonic-gate 	ctmpl_init(&new->ctp_ctmpl, &ctmpl_process_ops, process_type, new);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	new->ctp_subsume = NULL;
4460Sstevel@tonic-gate 	new->ctp_params = 0;
4470Sstevel@tonic-gate 	new->ctp_ctmpl.ctmpl_ev_info = CT_PR_EV_CORE | CT_PR_EV_SIGNAL;
4480Sstevel@tonic-gate 	new->ctp_ctmpl.ctmpl_ev_crit = CT_PR_EV_EMPTY | CT_PR_EV_HWERR;
4490Sstevel@tonic-gate 	new->ctp_ev_fatal = CT_PR_EV_HWERR;
4506073Sacruz 	new->ctp_svc_fmri = NULL;
4516073Sacruz 	new->ctp_svc_aux = NULL;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	return (&new->ctp_ctmpl);
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate /*
4570Sstevel@tonic-gate  * contract_process_free
4580Sstevel@tonic-gate  *
4590Sstevel@tonic-gate  * The process contract free entry point.
4600Sstevel@tonic-gate  */
4610Sstevel@tonic-gate static void
contract_process_free(contract_t * ct)4620Sstevel@tonic-gate contract_process_free(contract_t *ct)
4630Sstevel@tonic-gate {
4640Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
4650Sstevel@tonic-gate 	crfree(ctp->conp_cred);
4660Sstevel@tonic-gate 	list_destroy(&ctp->conp_members);
4670Sstevel@tonic-gate 	list_destroy(&ctp->conp_inherited);
4686073Sacruz 	if (ctp->conp_svc_fmri != NULL) {
4696073Sacruz 		refstr_rele(ctp->conp_svc_fmri);
4706073Sacruz 	}
4716073Sacruz 	if (ctp->conp_svc_aux != NULL) {
4726073Sacruz 		refstr_rele(ctp->conp_svc_aux);
4736073Sacruz 	}
4746073Sacruz 	if (ctp->conp_svc_creator != NULL) {
4756073Sacruz 		refstr_rele(ctp->conp_svc_creator);
4766073Sacruz 	}
4770Sstevel@tonic-gate 	kmem_free(ctp, sizeof (cont_process_t));
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate /*
4810Sstevel@tonic-gate  * contract_process_cankill
4820Sstevel@tonic-gate  *
4830Sstevel@tonic-gate  * Determine if the contract author had or if the process generating
4840Sstevel@tonic-gate  * the event, sp, has adequate privileges to kill process tp.
4850Sstevel@tonic-gate  */
4860Sstevel@tonic-gate static int
contract_process_cankill(proc_t * tp,proc_t * sp,cont_process_t * ctp)4870Sstevel@tonic-gate contract_process_cankill(proc_t *tp, proc_t *sp, cont_process_t *ctp)
4880Sstevel@tonic-gate {
4890Sstevel@tonic-gate 	int cankill;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	mutex_enter(&tp->p_crlock);
4920Sstevel@tonic-gate 	cankill = hasprocperm(tp->p_cred, ctp->conp_cred);
4930Sstevel@tonic-gate 	mutex_exit(&tp->p_crlock);
4940Sstevel@tonic-gate 	if (cankill || (sp && prochasprocperm(tp, sp, CRED())))
4950Sstevel@tonic-gate 		return (1);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	return (0);
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate  * contract_process_kill
5020Sstevel@tonic-gate  *
5030Sstevel@tonic-gate  * Kills all processes in a contract, or all processes in the
5040Sstevel@tonic-gate  * intersection of a contract and ex's process group (if ex is non-NULL
5050Sstevel@tonic-gate  * and the contract's PGRPONLY parameter is set).  If checkpriv is
5060Sstevel@tonic-gate  * true, only those processes which may be signaled by the contract
5070Sstevel@tonic-gate  * author or ex are killed.
5080Sstevel@tonic-gate  */
5090Sstevel@tonic-gate static void
contract_process_kill(contract_t * ct,proc_t * ex,int checkpriv)5100Sstevel@tonic-gate contract_process_kill(contract_t *ct, proc_t *ex, int checkpriv)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
5130Sstevel@tonic-gate 	proc_t *p;
5140Sstevel@tonic-gate 	pid_t pgrp = -1;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	if (ex && (ctp->conp_params & CT_PR_PGRPONLY)) {
5190Sstevel@tonic-gate 		pgrp = ex->p_pgrp;
5200Sstevel@tonic-gate 		mutex_enter(&pidlock);
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	for (p = list_head(&ctp->conp_members); p != NULL;
5240Sstevel@tonic-gate 	    p = list_next(&ctp->conp_members, p)) {
525*8921SVamsi.Krishna@Sun.COM 		if ((p == ex) ||
526*8921SVamsi.Krishna@Sun.COM 		    (pgrp != -1 && (p->p_stat == SIDL || p->p_pgrp != pgrp)) ||
5270Sstevel@tonic-gate 		    (checkpriv && !contract_process_cankill(p, ex, ctp)))
5280Sstevel@tonic-gate 			continue;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 		psignal(p, SIGKILL);
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	if (pgrp != -1)
5340Sstevel@tonic-gate 		mutex_exit(&pidlock);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate /*
5390Sstevel@tonic-gate  * contract_process_accept
5400Sstevel@tonic-gate  *
5410Sstevel@tonic-gate  * Tests if the process contract is willing to act as a regent for
5420Sstevel@tonic-gate  * inherited contracts.  Though brief and only called from one place,
5430Sstevel@tonic-gate  * this functionality is kept here to avoid including knowledge of
5440Sstevel@tonic-gate  * process contract implementation in the generic contract code.
5450Sstevel@tonic-gate  */
5460Sstevel@tonic-gate int
contract_process_accept(contract_t * parent)5470Sstevel@tonic-gate contract_process_accept(contract_t *parent)
5480Sstevel@tonic-gate {
5490Sstevel@tonic-gate 	cont_process_t *ctp = parent->ct_data;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	ASSERT(parent->ct_type == process_type);
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	return (ctp->conp_params & CT_PR_REGENT);
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate /*
5570Sstevel@tonic-gate  * contract_process_take
5580Sstevel@tonic-gate  *
5590Sstevel@tonic-gate  * Executes the process contract side of inheriting a contract.
5600Sstevel@tonic-gate  */
5610Sstevel@tonic-gate void
contract_process_take(contract_t * parent,contract_t * child)5620Sstevel@tonic-gate contract_process_take(contract_t *parent, contract_t *child)
5630Sstevel@tonic-gate {
5640Sstevel@tonic-gate 	cont_process_t *ctp = parent->ct_data;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&parent->ct_lock));
5670Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&child->ct_lock));
5680Sstevel@tonic-gate 	ASSERT(parent->ct_type == process_type);
5690Sstevel@tonic-gate 	ASSERT(ctp->conp_params & CT_PR_REGENT);
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	list_insert_head(&ctp->conp_inherited, child);
5720Sstevel@tonic-gate 	ctp->conp_ninherited++;
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate /*
5760Sstevel@tonic-gate  * contract_process_adopt
5770Sstevel@tonic-gate  *
5780Sstevel@tonic-gate  * Executes the process contract side of adopting a contract.
5790Sstevel@tonic-gate  */
5800Sstevel@tonic-gate void
contract_process_adopt(contract_t * ct,proc_t * p)5810Sstevel@tonic-gate contract_process_adopt(contract_t *ct, proc_t *p)
5820Sstevel@tonic-gate {
5830Sstevel@tonic-gate 	cont_process_t *parent = p->p_ct_process;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&parent->conp_contract.ct_lock));
5860Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	list_remove(&parent->conp_inherited, ct);
5890Sstevel@tonic-gate 	parent->conp_ninherited--;
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	/*
5920Sstevel@tonic-gate 	 * We drop the parent lock first because a) we are passing the
5930Sstevel@tonic-gate 	 * contract reference to the child, and b) contract_adopt
5940Sstevel@tonic-gate 	 * expects us to return with the contract lock held.
5950Sstevel@tonic-gate 	 */
5960Sstevel@tonic-gate 	mutex_exit(&parent->conp_contract.ct_lock);
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate /*
6004845Svikram  * contract_process_abandon
6010Sstevel@tonic-gate  *
6020Sstevel@tonic-gate  * The process contract abandon entry point.
6030Sstevel@tonic-gate  */
6040Sstevel@tonic-gate static void
contract_process_abandon(contract_t * ct)6050Sstevel@tonic-gate contract_process_abandon(contract_t *ct)
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	/*
6120Sstevel@tonic-gate 	 * Shall we stay or shall we go?
6130Sstevel@tonic-gate 	 */
6140Sstevel@tonic-gate 	if (list_head(&ctp->conp_members) == NULL) {
6150Sstevel@tonic-gate 		contract_destroy(ct);
6160Sstevel@tonic-gate 	} else {
6170Sstevel@tonic-gate 		/*
6180Sstevel@tonic-gate 		 * Strictly speaking, we actually do orphan the contract.
6190Sstevel@tonic-gate 		 * Assuming our credentials allow us to kill all
6200Sstevel@tonic-gate 		 * processes in the contract, this is only temporary.
6210Sstevel@tonic-gate 		 */
6220Sstevel@tonic-gate 		if (ctp->conp_params & CT_PR_NOORPHAN)
6230Sstevel@tonic-gate 			contract_process_kill(ct, NULL, B_TRUE);
6240Sstevel@tonic-gate 		contract_orphan(ct);
6250Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
6260Sstevel@tonic-gate 		contract_rele(ct);
6270Sstevel@tonic-gate 	}
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate /*
6310Sstevel@tonic-gate  * contract_process_destroy
6320Sstevel@tonic-gate  *
6330Sstevel@tonic-gate  * The process contract destroy entry point.
6340Sstevel@tonic-gate  */
6350Sstevel@tonic-gate static void
contract_process_destroy(contract_t * ct)6360Sstevel@tonic-gate contract_process_destroy(contract_t *ct)
6370Sstevel@tonic-gate {
6380Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
6390Sstevel@tonic-gate 	contract_t *cct;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	/*
6440Sstevel@tonic-gate 	 * contract_destroy all empty children, kill or orphan the rest
6450Sstevel@tonic-gate 	 */
6460Sstevel@tonic-gate 	while (cct = list_head(&ctp->conp_inherited)) {
6470Sstevel@tonic-gate 		mutex_enter(&cct->ct_lock);
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 		ASSERT(cct->ct_state == CTS_INHERITED);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 		list_remove(&ctp->conp_inherited, cct);
6520Sstevel@tonic-gate 		ctp->conp_ninherited--;
6530Sstevel@tonic-gate 		cct->ct_regent = NULL;
6540Sstevel@tonic-gate 		cct->ct_type->ct_type_ops->contop_abandon(cct);
6550Sstevel@tonic-gate 	}
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate /*
6590Sstevel@tonic-gate  * contract_process_status
6600Sstevel@tonic-gate  *
6610Sstevel@tonic-gate  * The process contract status entry point.
6620Sstevel@tonic-gate  */
6630Sstevel@tonic-gate static void
contract_process_status(contract_t * ct,zone_t * zone,int detail,nvlist_t * nvl,void * status,model_t model)6640Sstevel@tonic-gate contract_process_status(contract_t *ct, zone_t *zone, int detail, nvlist_t *nvl,
6650Sstevel@tonic-gate     void *status, model_t model)
6660Sstevel@tonic-gate {
6670Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
6680Sstevel@tonic-gate 	uint32_t *pids, *ctids;
6690Sstevel@tonic-gate 	uint_t npids, nctids;
6700Sstevel@tonic-gate 	uint_t spids, sctids;
6716073Sacruz 	ctid_t local_svc_zone_enter;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	if (detail == CTD_FIXED) {
6740Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
6750Sstevel@tonic-gate 		contract_status_common(ct, zone, status, model);
6766073Sacruz 		local_svc_zone_enter = ctp->conp_svc_zone_enter;
6770Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
6780Sstevel@tonic-gate 	} else {
6790Sstevel@tonic-gate 		contract_t *cnext;
6800Sstevel@tonic-gate 		proc_t *pnext;
6810Sstevel@tonic-gate 		uint_t loc;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 		ASSERT(detail == CTD_ALL);
6840Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
6850Sstevel@tonic-gate 		for (;;) {
6860Sstevel@tonic-gate 			spids = ctp->conp_nmembers + 5;
6870Sstevel@tonic-gate 			sctids = ctp->conp_ninherited + 5;
6880Sstevel@tonic-gate 			mutex_exit(&ct->ct_lock);
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 			pids = kmem_alloc(spids * sizeof (uint32_t), KM_SLEEP);
6910Sstevel@tonic-gate 			ctids = kmem_alloc(sctids * sizeof (uint32_t),
6920Sstevel@tonic-gate 			    KM_SLEEP);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 			mutex_enter(&ct->ct_lock);
6950Sstevel@tonic-gate 			npids = ctp->conp_nmembers;
6960Sstevel@tonic-gate 			nctids = ctp->conp_ninherited;
6970Sstevel@tonic-gate 			if (spids >= npids && sctids >= nctids)
6980Sstevel@tonic-gate 				break;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 			kmem_free(pids, spids * sizeof (uint32_t));
7010Sstevel@tonic-gate 			kmem_free(ctids, sctids * sizeof (uint32_t));
7020Sstevel@tonic-gate 		}
7030Sstevel@tonic-gate 		contract_status_common(ct, zone, status, model);
7040Sstevel@tonic-gate 		for (loc = 0, cnext = list_head(&ctp->conp_inherited); cnext;
7050Sstevel@tonic-gate 		    cnext = list_next(&ctp->conp_inherited, cnext))
7060Sstevel@tonic-gate 			ctids[loc++] = cnext->ct_id;
7070Sstevel@tonic-gate 		ASSERT(loc == nctids);
7080Sstevel@tonic-gate 		for (loc = 0, pnext = list_head(&ctp->conp_members); pnext;
7090Sstevel@tonic-gate 		    pnext = list_next(&ctp->conp_members, pnext))
7100Sstevel@tonic-gate 			pids[loc++] = pnext->p_pid;
7110Sstevel@tonic-gate 		ASSERT(loc == npids);
7126073Sacruz 		local_svc_zone_enter = ctp->conp_svc_zone_enter;
7130Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
7140Sstevel@tonic-gate 	}
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	/*
7170Sstevel@tonic-gate 	 * Contract terms are static; there's no need to hold the
7180Sstevel@tonic-gate 	 * contract lock while accessing them.
7190Sstevel@tonic-gate 	 */
7200Sstevel@tonic-gate 	VERIFY(nvlist_add_uint32(nvl, CTPS_PARAMS, ctp->conp_params) == 0);
7210Sstevel@tonic-gate 	VERIFY(nvlist_add_uint32(nvl, CTPS_EV_FATAL, ctp->conp_ev_fatal) == 0);
7220Sstevel@tonic-gate 	if (detail == CTD_ALL) {
7230Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32_array(nvl, CTPS_MEMBERS, pids,
7240Sstevel@tonic-gate 		    npids) == 0);
7250Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32_array(nvl, CTPS_CONTRACTS, ctids,
7260Sstevel@tonic-gate 		    nctids) == 0);
7276073Sacruz 		VERIFY(nvlist_add_string(nvl, CTPS_CREATOR_AUX,
7286073Sacruz 		    refstr_value(ctp->conp_svc_aux)) == 0);
7296073Sacruz 		VERIFY(nvlist_add_string(nvl, CTPS_SVC_CREATOR,
7306073Sacruz 		    refstr_value(ctp->conp_svc_creator)) == 0);
7310Sstevel@tonic-gate 		kmem_free(pids, spids * sizeof (uint32_t));
7320Sstevel@tonic-gate 		kmem_free(ctids, sctids * sizeof (uint32_t));
7330Sstevel@tonic-gate 	}
7346073Sacruz 
7356073Sacruz 	/*
7366073Sacruz 	 * if we are in a local zone and svc_fmri was inherited from
7376073Sacruz 	 * the global zone, we provide fake svc_fmri and svc_ctid
7386073Sacruz 	 */
7396073Sacruz 	if (local_svc_zone_enter == 0||
7406073Sacruz 	    zone->zone_uniqid == GLOBAL_ZONEUNIQID) {
7416073Sacruz 		if (detail > CTD_COMMON) {
7426073Sacruz 			VERIFY(nvlist_add_int32(nvl, CTPS_SVC_CTID,
7436073Sacruz 			    ctp->conp_svc_ctid) == 0);
7446073Sacruz 		}
7456073Sacruz 		if (detail == CTD_ALL) {
7466073Sacruz 			VERIFY(nvlist_add_string(nvl, CTPS_SVC_FMRI,
7476073Sacruz 			    refstr_value(ctp->conp_svc_fmri)) == 0);
7486073Sacruz 		}
7496073Sacruz 	} else {
7506073Sacruz 		if (detail > CTD_COMMON) {
7516073Sacruz 			VERIFY(nvlist_add_int32(nvl, CTPS_SVC_CTID,
7526073Sacruz 			    local_svc_zone_enter) == 0);
7536073Sacruz 		}
7546073Sacruz 		if (detail == CTD_ALL) {
7556073Sacruz 			VERIFY(nvlist_add_string(nvl, CTPS_SVC_FMRI,
7566073Sacruz 			    CT_PR_SVC_FMRI_ZONE_ENTER) == 0);
7576073Sacruz 		}
7586073Sacruz 	}
7590Sstevel@tonic-gate }
7600Sstevel@tonic-gate 
7614845Svikram /*ARGSUSED*/
7624845Svikram static int
contract_process_newct(contract_t * ct)7634845Svikram contract_process_newct(contract_t *ct)
7644845Svikram {
7654845Svikram 	return (0);
7664845Svikram }
7674845Svikram 
7684845Svikram /* process contracts don't negotiate */
7690Sstevel@tonic-gate static contops_t contract_process_ops = {
7700Sstevel@tonic-gate 	contract_process_free,		/* contop_free */
7710Sstevel@tonic-gate 	contract_process_abandon,	/* contop_abandon */
7720Sstevel@tonic-gate 	contract_process_destroy,	/* contop_destroy */
7734845Svikram 	contract_process_status,	/* contop_status */
7744845Svikram 	contract_ack_inval,		/* contop_ack */
7754845Svikram 	contract_ack_inval,		/* contop_nack */
7764845Svikram 	contract_qack_inval,		/* contop_qack */
7774845Svikram 	contract_process_newct		/* contop_newct */
7780Sstevel@tonic-gate };
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate /*
7810Sstevel@tonic-gate  * contract_process_init
7820Sstevel@tonic-gate  *
7830Sstevel@tonic-gate  * Initializes the process contract type.  Also creates a template for
7840Sstevel@tonic-gate  * use by newproc() when it creates user processes.
7850Sstevel@tonic-gate  */
7860Sstevel@tonic-gate void
contract_process_init(void)7870Sstevel@tonic-gate contract_process_init(void)
7880Sstevel@tonic-gate {
7890Sstevel@tonic-gate 	process_type = contract_type_init(CTT_PROCESS, "process",
7900Sstevel@tonic-gate 	    &contract_process_ops, contract_process_default);
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	/*
7930Sstevel@tonic-gate 	 * Create a template for use with init(1M) and other
7940Sstevel@tonic-gate 	 * kernel-started processes.
7950Sstevel@tonic-gate 	 */
7960Sstevel@tonic-gate 	sys_process_tmpl = kmem_alloc(sizeof (ctmpl_process_t), KM_SLEEP);
7970Sstevel@tonic-gate 	ctmpl_init(&sys_process_tmpl->ctp_ctmpl, &ctmpl_process_ops,
7980Sstevel@tonic-gate 	    process_type, sys_process_tmpl);
7990Sstevel@tonic-gate 	sys_process_tmpl->ctp_subsume = NULL;
8000Sstevel@tonic-gate 	sys_process_tmpl->ctp_params = CT_PR_NOORPHAN;
8010Sstevel@tonic-gate 	sys_process_tmpl->ctp_ev_fatal = CT_PR_EV_HWERR;
8026073Sacruz 	sys_process_tmpl->ctp_svc_fmri =
8036073Sacruz 	    refstr_alloc("svc:/system/init:default");
8046073Sacruz 	sys_process_tmpl->ctp_svc_aux = refstr_alloc("");
8056073Sacruz 	conp_svc_aux_default = sys_process_tmpl->ctp_svc_aux;
8066073Sacruz 	refstr_hold(conp_svc_aux_default);
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate /*
8100Sstevel@tonic-gate  * contract_process_create
8110Sstevel@tonic-gate  *
8120Sstevel@tonic-gate  * create a process contract given template "tmpl" and parent process
8130Sstevel@tonic-gate  * "parent".  May fail and return NULL if project.max-contracts would
8140Sstevel@tonic-gate  * have been exceeded.
8150Sstevel@tonic-gate  */
8160Sstevel@tonic-gate static cont_process_t *
contract_process_create(ctmpl_process_t * tmpl,proc_t * parent,int canfail)8170Sstevel@tonic-gate contract_process_create(ctmpl_process_t *tmpl, proc_t *parent, int canfail)
8180Sstevel@tonic-gate {
8190Sstevel@tonic-gate 	cont_process_t *ctp;
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	ASSERT(tmpl != NULL);
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	(void) contract_type_pbundle(process_type, parent);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	ctp = kmem_zalloc(sizeof (cont_process_t), KM_SLEEP);
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	list_create(&ctp->conp_members, sizeof (proc_t),
8280Sstevel@tonic-gate 	    offsetof(proc_t, p_ct_member));
8290Sstevel@tonic-gate 	list_create(&ctp->conp_inherited, sizeof (contract_t),
8300Sstevel@tonic-gate 	    offsetof(contract_t, ct_ctlist));
8310Sstevel@tonic-gate 	mutex_enter(&tmpl->ctp_ctmpl.ctmpl_lock);
8320Sstevel@tonic-gate 	ctp->conp_params = tmpl->ctp_params;
8330Sstevel@tonic-gate 	ctp->conp_ev_fatal = tmpl->ctp_ev_fatal;
8340Sstevel@tonic-gate 	crhold(ctp->conp_cred = CRED());
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	if (contract_ctor(&ctp->conp_contract, process_type, &tmpl->ctp_ctmpl,
8370Sstevel@tonic-gate 	    ctp, (ctp->conp_params & CT_PR_INHERIT) ? CTF_INHERIT : 0,
8380Sstevel@tonic-gate 	    parent, canfail)) {
8390Sstevel@tonic-gate 		mutex_exit(&tmpl->ctp_ctmpl.ctmpl_lock);
8400Sstevel@tonic-gate 		contract_process_free(&ctp->conp_contract);
8410Sstevel@tonic-gate 		return (NULL);
8420Sstevel@tonic-gate 	}
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	/*
8456073Sacruz 	 * inherit svc_fmri if not defined by consumer. In this case, inherit
8466073Sacruz 	 * also svc_ctid to keep track of the contract id where
8476073Sacruz 	 * svc_fmri was set
8486073Sacruz 	 */
8496073Sacruz 	if (tmpl->ctp_svc_fmri == NULL) {
8506073Sacruz 		ctp->conp_svc_fmri = parent->p_ct_process->conp_svc_fmri;
8516073Sacruz 		ctp->conp_svc_ctid = parent->p_ct_process->conp_svc_ctid;
8526073Sacruz 		ctp->conp_svc_zone_enter =
8536073Sacruz 		    parent->p_ct_process->conp_svc_zone_enter;
8546073Sacruz 	} else {
8556073Sacruz 		ctp->conp_svc_fmri = tmpl->ctp_svc_fmri;
8566073Sacruz 		ctp->conp_svc_ctid = ctp->conp_contract.ct_id;
8576073Sacruz 		/* make svc_zone_enter flag false when svc_fmri is set */
8586073Sacruz 		ctp->conp_svc_zone_enter = 0;
8596073Sacruz 	}
8606073Sacruz 	refstr_hold(ctp->conp_svc_fmri);
8616073Sacruz 	/* set svc_aux to default value if not defined in template */
8626073Sacruz 	if (tmpl->ctp_svc_aux == NULL) {
8636073Sacruz 		ctp->conp_svc_aux = conp_svc_aux_default;
8646073Sacruz 	} else {
8656073Sacruz 		ctp->conp_svc_aux = tmpl->ctp_svc_aux;
8666073Sacruz 	}
8676073Sacruz 	refstr_hold(ctp->conp_svc_aux);
8686073Sacruz 	/*
8696073Sacruz 	 * set svc_creator to execname
8706073Sacruz 	 * We special case pid0 because when newproc() creates
8716073Sacruz 	 * the init process, the p_user.u_comm field of sched's proc_t
8726073Sacruz 	 * has not been populated yet.
8736073Sacruz 	 */
8746073Sacruz 	if (parent->p_pidp == &pid0) /* if the kernel is the creator */
8756073Sacruz 		ctp->conp_svc_creator = refstr_alloc("sched");
8766073Sacruz 	else
8776073Sacruz 		ctp->conp_svc_creator = refstr_alloc(parent->p_user.u_comm);
8786073Sacruz 
8796073Sacruz 	/*
8800Sstevel@tonic-gate 	 * Transfer subcontracts only after new contract is visible.
8810Sstevel@tonic-gate 	 * Also, only transfer contracts if the parent matches -- we
8820Sstevel@tonic-gate 	 * don't want to create a cycle in the tree of contracts.
8830Sstevel@tonic-gate 	 */
8840Sstevel@tonic-gate 	if (tmpl->ctp_subsume && tmpl->ctp_subsume->ct_owner == parent) {
8850Sstevel@tonic-gate 		cont_process_t *sct = tmpl->ctp_subsume->ct_data;
8860Sstevel@tonic-gate 		contract_t *ct;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 		mutex_enter(&tmpl->ctp_subsume->ct_lock);
8890Sstevel@tonic-gate 		mutex_enter(&ctp->conp_contract.ct_lock);
8900Sstevel@tonic-gate 		while (ct = list_head(&sct->conp_inherited)) {
8910Sstevel@tonic-gate 			mutex_enter(&ct->ct_lock);
8920Sstevel@tonic-gate 			list_remove(&sct->conp_inherited, ct);
8930Sstevel@tonic-gate 			list_insert_tail(&ctp->conp_inherited, ct);
8940Sstevel@tonic-gate 			ct->ct_regent = &ctp->conp_contract;
8950Sstevel@tonic-gate 			mutex_exit(&ct->ct_lock);
8960Sstevel@tonic-gate 		}
8970Sstevel@tonic-gate 		ctp->conp_ninherited += sct->conp_ninherited;
8980Sstevel@tonic-gate 		sct->conp_ninherited = 0;
8990Sstevel@tonic-gate 		mutex_exit(&ctp->conp_contract.ct_lock);
9000Sstevel@tonic-gate 		mutex_exit(&tmpl->ctp_subsume->ct_lock);
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 		/*
9030Sstevel@tonic-gate 		 * Automatically abandon the contract.
9040Sstevel@tonic-gate 		 */
9050Sstevel@tonic-gate 		(void) contract_abandon(tmpl->ctp_subsume, parent, 1);
9060Sstevel@tonic-gate 	}
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	mutex_exit(&tmpl->ctp_ctmpl.ctmpl_lock);
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	return (ctp);
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate /*
9140Sstevel@tonic-gate  * contract_process_exit
9150Sstevel@tonic-gate  *
9160Sstevel@tonic-gate  * Called on process exit.  Removes process p from process contract
9170Sstevel@tonic-gate  * ctp.  Generates an exit event, if requested.  Generates an empty
9180Sstevel@tonic-gate  * event, if p is the last member of the the process contract and empty
9190Sstevel@tonic-gate  * events were requested.
9200Sstevel@tonic-gate  */
9210Sstevel@tonic-gate void
contract_process_exit(cont_process_t * ctp,proc_t * p,int exitstatus)9220Sstevel@tonic-gate contract_process_exit(cont_process_t *ctp, proc_t *p, int exitstatus)
9230Sstevel@tonic-gate {
9240Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
9250Sstevel@tonic-gate 	ct_kevent_t *event;
9260Sstevel@tonic-gate 	int empty;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	/*
9290Sstevel@tonic-gate 	 * Remove self from process contract.
9300Sstevel@tonic-gate 	 */
9310Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
9320Sstevel@tonic-gate 	list_remove(&ctp->conp_members, p);
9330Sstevel@tonic-gate 	ctp->conp_nmembers--;
9340Sstevel@tonic-gate 	mutex_enter(&p->p_lock);	/* in case /proc is watching */
9350Sstevel@tonic-gate 	p->p_ct_process = NULL;
9360Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	/*
9390Sstevel@tonic-gate 	 * We check for emptiness before dropping the contract lock to
9400Sstevel@tonic-gate 	 * send the exit event, otherwise we could end up with two
9410Sstevel@tonic-gate 	 * empty events.
9420Sstevel@tonic-gate 	 */
9430Sstevel@tonic-gate 	empty = (list_head(&ctp->conp_members) == NULL);
9440Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_EXIT)) {
9450Sstevel@tonic-gate 		nvlist_t *nvl;
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
9480Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
9490Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
9500Sstevel@tonic-gate 		VERIFY(nvlist_add_int32(nvl, CTPE_EXITSTATUS, exitstatus) == 0);
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
9530Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_EXIT) ? CTE_INFO : 0;
9540Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_EXIT;
9554845Svikram 		(void) cte_publish_all(ct, event, nvl, NULL);
9560Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
9570Sstevel@tonic-gate 	}
9580Sstevel@tonic-gate 	if (empty) {
9590Sstevel@tonic-gate 		/*
9600Sstevel@tonic-gate 		 * Send EMPTY message.
9610Sstevel@tonic-gate 		 */
9620Sstevel@tonic-gate 		if (EVSENDP(ctp, CT_PR_EV_EMPTY)) {
9630Sstevel@tonic-gate 			nvlist_t *nvl;
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 			mutex_exit(&ct->ct_lock);
9660Sstevel@tonic-gate 			VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME,
9670Sstevel@tonic-gate 			    KM_SLEEP) == 0);
9680Sstevel@tonic-gate 			VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 			event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
9710Sstevel@tonic-gate 			event->cte_flags = EVINFOP(ctp, CT_PR_EV_EMPTY) ?
9720Sstevel@tonic-gate 			    CTE_INFO : 0;
9730Sstevel@tonic-gate 			event->cte_type = CT_PR_EV_EMPTY;
9744845Svikram 			(void) cte_publish_all(ct, event, nvl, NULL);
9750Sstevel@tonic-gate 			mutex_enter(&ct->ct_lock);
9760Sstevel@tonic-gate 		}
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 		/*
9790Sstevel@tonic-gate 		 * The last one to leave an orphaned contract turns out
9800Sstevel@tonic-gate 		 * the lights.
9810Sstevel@tonic-gate 		 */
9820Sstevel@tonic-gate 		if (ct->ct_state == CTS_ORPHAN) {
9830Sstevel@tonic-gate 			contract_destroy(ct);
9840Sstevel@tonic-gate 			return;
9850Sstevel@tonic-gate 		}
9860Sstevel@tonic-gate 	}
9870Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
9880Sstevel@tonic-gate 	contract_rele(ct);
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate 
9910Sstevel@tonic-gate /*
9920Sstevel@tonic-gate  * contract_process_fork
9930Sstevel@tonic-gate  *
9940Sstevel@tonic-gate  * Called on process fork.  If the current lwp has a active process
9950Sstevel@tonic-gate  * contract template, we attempt to create a new process contract.
9960Sstevel@tonic-gate  * Failure to create a process contract when required is a failure in
9970Sstevel@tonic-gate  * fork so, in such an event, we return NULL.
9980Sstevel@tonic-gate  *
9990Sstevel@tonic-gate  * Assuming we succeeded or skipped the previous step, we add the child
10000Sstevel@tonic-gate  * process to the new contract (success) or to the parent's process
10010Sstevel@tonic-gate  * contract (skip).  If requested, we also send a fork event to that
10020Sstevel@tonic-gate  * contract.
10030Sstevel@tonic-gate  *
10040Sstevel@tonic-gate  * Because contract_process_fork() may fail, and because we would
10050Sstevel@tonic-gate  * prefer that process contracts not be created for processes which
10060Sstevel@tonic-gate  * don't complete forking, this should be the last function called
10070Sstevel@tonic-gate  * before the "all clear" point in cfork.
10080Sstevel@tonic-gate  */
10090Sstevel@tonic-gate cont_process_t *
contract_process_fork(ctmpl_process_t * rtmpl,proc_t * cp,proc_t * pp,int canfail)10100Sstevel@tonic-gate contract_process_fork(ctmpl_process_t *rtmpl, proc_t *cp, proc_t *pp,
10110Sstevel@tonic-gate     int canfail)
10120Sstevel@tonic-gate {
10130Sstevel@tonic-gate 	contract_t *ct;
10140Sstevel@tonic-gate 	cont_process_t *ctp;
10150Sstevel@tonic-gate 	ct_kevent_t *event;
10160Sstevel@tonic-gate 	ct_template_t *tmpl;
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	if (rtmpl == NULL && (tmpl = ttolwp(curthread)->lwp_ct_active[
10190Sstevel@tonic-gate 	    process_type->ct_type_index]) != NULL)
10200Sstevel@tonic-gate 		rtmpl = tmpl->ctmpl_data;
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	if (rtmpl == NULL)
10230Sstevel@tonic-gate 		ctp = curproc->p_ct_process;
10240Sstevel@tonic-gate 	else if ((ctp = contract_process_create(rtmpl, pp, canfail)) == NULL)
10250Sstevel@tonic-gate 		return (NULL);
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	ct = &ctp->conp_contract;
10280Sstevel@tonic-gate 	/*
10290Sstevel@tonic-gate 	 * Prevent contract_process_kill() from missing forked children
10300Sstevel@tonic-gate 	 * by failing forks by parents that have just been killed.
10310Sstevel@tonic-gate 	 * It's not worth hoisting the ctp test since contract creation
10320Sstevel@tonic-gate 	 * is by no means the common case.
10330Sstevel@tonic-gate 	 */
10340Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
10350Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
10360Sstevel@tonic-gate 	if (ctp == curproc->p_ct_process && (pp->p_flag & SKILLED) != 0 &&
10370Sstevel@tonic-gate 	    canfail) {
10380Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
10390Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
10400Sstevel@tonic-gate 		return (NULL);
10410Sstevel@tonic-gate 	}
10420Sstevel@tonic-gate 	cp->p_ct_process = ctp;
10430Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
10440Sstevel@tonic-gate 	contract_hold(ct);
10450Sstevel@tonic-gate 	list_insert_head(&ctp->conp_members, cp);
10460Sstevel@tonic-gate 	ctp->conp_nmembers++;
10470Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
10480Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_FORK)) {
10490Sstevel@tonic-gate 		nvlist_t *nvl;
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
10520Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, cp->p_pid) == 0);
10530Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PPID, pp->p_pid) == 0);
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
10560Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_FORK) ? CTE_INFO : 0;
10570Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_FORK;
10584845Svikram 		(void) cte_publish_all(ct, event, nvl, NULL);
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate 	return (ctp);
10610Sstevel@tonic-gate }
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate /*
10640Sstevel@tonic-gate  * contract_process_core
10650Sstevel@tonic-gate  *
10660Sstevel@tonic-gate  * Called on core file generation attempts.  Generates a core event, if
10670Sstevel@tonic-gate  * requested, containing the names of the process, global, and
10680Sstevel@tonic-gate  * system-global ("zone") core files.  If dumping core is in the fatal
10690Sstevel@tonic-gate  * event set, calls contract_process_kill().
10700Sstevel@tonic-gate  */
10710Sstevel@tonic-gate void
contract_process_core(cont_process_t * ctp,proc_t * p,int sig,const char * process,const char * global,const char * zone)10720Sstevel@tonic-gate contract_process_core(cont_process_t *ctp, proc_t *p, int sig,
10730Sstevel@tonic-gate     const char *process, const char *global, const char *zone)
10740Sstevel@tonic-gate {
10750Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_CORE)) {
10780Sstevel@tonic-gate 		ct_kevent_t *event;
10790Sstevel@tonic-gate 		nvlist_t *nvl, *gnvl = NULL;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
10820Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
10830Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_SIGNAL, sig) == 0);
10840Sstevel@tonic-gate 		if (process)
10850Sstevel@tonic-gate 			VERIFY(nvlist_add_string(nvl, CTPE_PCOREFILE,
10860Sstevel@tonic-gate 			    (char *)process) == 0);
10870Sstevel@tonic-gate 		if (global)
10880Sstevel@tonic-gate 			VERIFY(nvlist_add_string(nvl, CTPE_GCOREFILE,
10890Sstevel@tonic-gate 			    (char *)global) == 0);
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 		if (zone) {
10920Sstevel@tonic-gate 			/*
10930Sstevel@tonic-gate 			 * Only the global zone is informed of the
10940Sstevel@tonic-gate 			 * local-zone generated global-zone core.
10950Sstevel@tonic-gate 			 */
10960Sstevel@tonic-gate 			VERIFY(nvlist_alloc(&gnvl, NV_UNIQUE_NAME,
10970Sstevel@tonic-gate 			    KM_SLEEP) == 0);
10980Sstevel@tonic-gate 			VERIFY(nvlist_add_string(gnvl, CTPE_ZCOREFILE,
10990Sstevel@tonic-gate 			    (char *)zone) == 0);
11000Sstevel@tonic-gate 		}
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
11030Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_CORE) ? CTE_INFO : 0;
11040Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_CORE;
11054845Svikram 		(void) cte_publish_all(ct, event, nvl, gnvl);
11060Sstevel@tonic-gate 	}
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 	if (EVFATALP(ctp, CT_PR_EV_CORE)) {
11090Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
11100Sstevel@tonic-gate 		contract_process_kill(ct, p, B_TRUE);
11110Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
11120Sstevel@tonic-gate 	}
11130Sstevel@tonic-gate }
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate /*
11160Sstevel@tonic-gate  * contract_process_hwerr
11170Sstevel@tonic-gate  *
11180Sstevel@tonic-gate  * Called when a process is killed by an unrecoverable hardware error.
11190Sstevel@tonic-gate  * Generates an hwerr event, if requested.  If hardware errors are in
11200Sstevel@tonic-gate  * the fatal event set, calls contract_process_kill().
11210Sstevel@tonic-gate  */
11220Sstevel@tonic-gate void
contract_process_hwerr(cont_process_t * ctp,proc_t * p)11230Sstevel@tonic-gate contract_process_hwerr(cont_process_t *ctp, proc_t *p)
11240Sstevel@tonic-gate {
11250Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_HWERR)) {
11280Sstevel@tonic-gate 		ct_kevent_t *event;
11290Sstevel@tonic-gate 		nvlist_t *nvl;
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
11320Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
11350Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_HWERR) ? CTE_INFO : 0;
11360Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_HWERR;
11374845Svikram 		(void) cte_publish_all(ct, event, nvl, NULL);
11380Sstevel@tonic-gate 	}
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	if (EVFATALP(ctp, CT_PR_EV_HWERR)) {
11410Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
11420Sstevel@tonic-gate 		contract_process_kill(ct, p, B_FALSE);
11430Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
11440Sstevel@tonic-gate 	}
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate /*
11480Sstevel@tonic-gate  * contract_process_sig
11490Sstevel@tonic-gate  *
11500Sstevel@tonic-gate  * Called when a process is killed by a signal originating from a
11510Sstevel@tonic-gate  * process outside of its process contract or its process contract's
11520Sstevel@tonic-gate  * holder.  Generates an signal event, if requested, containing the
11530Sstevel@tonic-gate  * signal number, and the sender's pid and contract id (if available).
11540Sstevel@tonic-gate  * If signals are in the fatal event set, calls
11550Sstevel@tonic-gate  * contract_process_kill().
11560Sstevel@tonic-gate  */
11570Sstevel@tonic-gate void
contract_process_sig(cont_process_t * ctp,proc_t * p,int sig,pid_t pid,ctid_t ctid,zoneid_t zoneid)11580Sstevel@tonic-gate contract_process_sig(cont_process_t *ctp, proc_t *p, int sig, pid_t pid,
11590Sstevel@tonic-gate     ctid_t ctid, zoneid_t zoneid)
11600Sstevel@tonic-gate {
11610Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_SIGNAL)) {
11640Sstevel@tonic-gate 		ct_kevent_t *event;
11650Sstevel@tonic-gate 		nvlist_t *dest, *nvl, *gnvl = NULL;
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
11680Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
11690Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_SIGNAL, sig) == 0);
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 		if (zoneid >= 0 && p->p_zone->zone_id != zoneid) {
11720Sstevel@tonic-gate 			VERIFY(nvlist_alloc(&gnvl, NV_UNIQUE_NAME,
11730Sstevel@tonic-gate 			    KM_SLEEP) == 0);
11740Sstevel@tonic-gate 			dest = gnvl;
11750Sstevel@tonic-gate 		} else {
11760Sstevel@tonic-gate 			dest = nvl;
11770Sstevel@tonic-gate 		}
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 		if (pid != -1)
11800Sstevel@tonic-gate 			VERIFY(nvlist_add_uint32(dest, CTPE_SENDER, pid) == 0);
11810Sstevel@tonic-gate 		if (ctid != 0)
11820Sstevel@tonic-gate 			VERIFY(nvlist_add_uint32(dest, CTPE_SENDCT, ctid) == 0);
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
11850Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_SIGNAL) ? CTE_INFO : 0;
11860Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_SIGNAL;
11874845Svikram 		(void) cte_publish_all(ct, event, nvl, gnvl);
11880Sstevel@tonic-gate 	}
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	if (EVFATALP(ctp, CT_PR_EV_SIGNAL)) {
11910Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
11920Sstevel@tonic-gate 		contract_process_kill(ct, p, B_TRUE);
11930Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
11940Sstevel@tonic-gate 	}
11950Sstevel@tonic-gate }
1196