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
525e8c5aaSvikram  * Common Development and Distribution License (the "License").
625e8c5aaSvikram  * 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  */
217c478bd9Sstevel@tonic-gate /*
226e092be7SVamsi Nagineni  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24a2e92fdbSJohn Levon  * Copyright 2019 Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
287c478bd9Sstevel@tonic-gate #include <sys/debug.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
327c478bd9Sstevel@tonic-gate #include <sys/thread.h>
337c478bd9Sstevel@tonic-gate #include <sys/id_space.h>
347c478bd9Sstevel@tonic-gate #include <sys/avl.h>
357c478bd9Sstevel@tonic-gate #include <sys/list.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <sys/proc.h>
387c478bd9Sstevel@tonic-gate #include <sys/contract.h>
397c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h>
407c478bd9Sstevel@tonic-gate #include <sys/contract/process.h>
417c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
427c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
437c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
447c478bd9Sstevel@tonic-gate #include <sys/policy.h>
457b209c2cSacruz #include <sys/refstr.h>
467b209c2cSacruz #include <sys/sunddi.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * Process Contracts
507c478bd9Sstevel@tonic-gate  * -----------------
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * Generally speaking, a process contract is a contract between a
537c478bd9Sstevel@tonic-gate  * process and a set of its descendent processes.  In some cases, when
547c478bd9Sstevel@tonic-gate  * the child processes outlive the author of the contract, the contract
557c478bd9Sstevel@tonic-gate  * may be held by (and therefore be between the child processes and) a
567c478bd9Sstevel@tonic-gate  * successor process which adopts the contract after the death of the
577c478bd9Sstevel@tonic-gate  * original author.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * The process contract adds two new concepts to the Solaris process
607c478bd9Sstevel@tonic-gate  * model.  The first is that a process contract forms a rigid fault
617c478bd9Sstevel@tonic-gate  * boundary around a set of processes.  Hardware, software, and even
627c478bd9Sstevel@tonic-gate  * administrator errors impacting a process in a process contract
637c478bd9Sstevel@tonic-gate  * generate specific events and can be requested to atomically shutdown
647c478bd9Sstevel@tonic-gate  * all processes in the contract.  The second is that a process
657c478bd9Sstevel@tonic-gate  * contract is a process collective whose leader is not a member of the
667c478bd9Sstevel@tonic-gate  * collective.  This means that the leader can reliably react to events
677c478bd9Sstevel@tonic-gate  * in the collective, and may also act upon the collective without
687c478bd9Sstevel@tonic-gate  * special casing itself.
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  * A composite outcome of these two concepts is that we can now create
71*bbf21555SRichard Lowe  * a tree of process contracts, rooted at init(8), which represent
727c478bd9Sstevel@tonic-gate  * services and subservices that are reliably observed and can be
737c478bd9Sstevel@tonic-gate  * restarted when fatal errors occur.  The service management framework
747c478bd9Sstevel@tonic-gate  * (SMF) realizes this structure.
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  * For more details, see the "restart agreements" case, PSARC 2003/193.
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  * There are four sets of routines in this file: the process contract
797c478bd9Sstevel@tonic-gate  * standard template operations, the process contract standard contract
807c478bd9Sstevel@tonic-gate  * operations, a couple routines used only by the contract subsystem to
817c478bd9Sstevel@tonic-gate  * handle process contracts' unique role as a temporary holder of
827c478bd9Sstevel@tonic-gate  * abandoned contracts, and the interfaces which allow the system to
837c478bd9Sstevel@tonic-gate  * create and act upon process contracts.  The first two are defined by
847c478bd9Sstevel@tonic-gate  * the contracts framework and won't be discussed further.  As for the
857c478bd9Sstevel@tonic-gate  * remaining two:
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  * Special framework interfaces
887c478bd9Sstevel@tonic-gate  * ----------------------------
897c478bd9Sstevel@tonic-gate  *
907c478bd9Sstevel@tonic-gate  * contract_process_accept - determines if a process contract is a
917c478bd9Sstevel@tonic-gate  *   regent, i.e. if it can inherit other contracts.
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  * contract_process_take - tells a regent process contract to inherit
947c478bd9Sstevel@tonic-gate  *   an abandoned contract
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  * contract_process_adopt - tells a regent process contract that a
977c478bd9Sstevel@tonic-gate  *   contract it has inherited is being adopted by a process.
987c478bd9Sstevel@tonic-gate  *
997c478bd9Sstevel@tonic-gate  * Process contract interfaces
1007c478bd9Sstevel@tonic-gate  * ---------------------------
1017c478bd9Sstevel@tonic-gate  *
1027c478bd9Sstevel@tonic-gate  * contract_process_fork - called when a process is created; adds the
1037c478bd9Sstevel@tonic-gate  *   new process to an existing contract or to a newly created one.
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  * contract_process_exit - called when a process exits
1067c478bd9Sstevel@tonic-gate  *
1077c478bd9Sstevel@tonic-gate  * contract_process_core - called when a process would have dumped core
1087c478bd9Sstevel@tonic-gate  *   (even if a core file wasn't generated)
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  * contract_process_hwerr - called when a process was killed because of
1117c478bd9Sstevel@tonic-gate  *   an uncorrectable hardware error
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  * contract_process_sig - called when a process was killed by a fatal
1147c478bd9Sstevel@tonic-gate  *   signal sent by a process in another process contract
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate ct_type_t *process_type;
1197c478bd9Sstevel@tonic-gate ctmpl_process_t *sys_process_tmpl;
1207b209c2cSacruz refstr_t *conp_svc_aux_default;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * Macro predicates for determining when events should be sent and how.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate #define	EVSENDP(ctp, flag) \
1267c478bd9Sstevel@tonic-gate 	((ctp->conp_contract.ct_ev_info | ctp->conp_contract.ct_ev_crit) & flag)
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #define	EVINFOP(ctp, flag) \
1297c478bd9Sstevel@tonic-gate 	((ctp->conp_contract.ct_ev_crit & flag) == 0)
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #define	EVFATALP(ctp, flag) \
1327c478bd9Sstevel@tonic-gate 	(ctp->conp_ev_fatal & flag)
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * Process contract template implementation
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * ctmpl_process_dup
1417c478bd9Sstevel@tonic-gate  *
1427c478bd9Sstevel@tonic-gate  * The process contract template dup entry point.  Other than the
1437c478bd9Sstevel@tonic-gate  * to-be-subsumed contract, which must be held, this simply copies all
1447c478bd9Sstevel@tonic-gate  * the fields of the original.
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate static struct ct_template *
ctmpl_process_dup(struct ct_template * template)1477c478bd9Sstevel@tonic-gate ctmpl_process_dup(struct ct_template *template)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	ctmpl_process_t *new;
1507c478bd9Sstevel@tonic-gate 	ctmpl_process_t *old = template->ctmpl_data;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	new = kmem_alloc(sizeof (ctmpl_process_t), KM_SLEEP);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	ctmpl_copy(&new->ctp_ctmpl, template);
1557c478bd9Sstevel@tonic-gate 	new->ctp_ctmpl.ctmpl_data = new;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	new->ctp_subsume = old->ctp_subsume;
1587c478bd9Sstevel@tonic-gate 	if (new->ctp_subsume)
1597c478bd9Sstevel@tonic-gate 		contract_hold(new->ctp_subsume);
1607c478bd9Sstevel@tonic-gate 	new->ctp_params = old->ctp_params;
1617c478bd9Sstevel@tonic-gate 	new->ctp_ev_fatal = old->ctp_ev_fatal;
1627b209c2cSacruz 	new->ctp_svc_fmri = old->ctp_svc_fmri;
1637b209c2cSacruz 	if (new->ctp_svc_fmri != NULL) {
1647b209c2cSacruz 		refstr_hold(new->ctp_svc_fmri);
1657b209c2cSacruz 	}
1667b209c2cSacruz 	new->ctp_svc_aux = old->ctp_svc_aux;
1677b209c2cSacruz 	if (new->ctp_svc_aux != NULL) {
1687b209c2cSacruz 		refstr_hold(new->ctp_svc_aux);
1697b209c2cSacruz 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	return (&new->ctp_ctmpl);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757b209c2cSacruz  * ctmpl_process_free
1767c478bd9Sstevel@tonic-gate  *
1777c478bd9Sstevel@tonic-gate  * The process contract template free entry point.  Just releases a
1787c478bd9Sstevel@tonic-gate  * to-be-subsumed contract and frees the template.
1797c478bd9Sstevel@tonic-gate  */
1807c478bd9Sstevel@tonic-gate static void
ctmpl_process_free(struct ct_template * template)1817c478bd9Sstevel@tonic-gate ctmpl_process_free(struct ct_template *template)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	ctmpl_process_t *ctp = template->ctmpl_data;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	if (ctp->ctp_subsume)
1867c478bd9Sstevel@tonic-gate 		contract_rele(ctp->ctp_subsume);
1877b209c2cSacruz 	if (ctp->ctp_svc_fmri != NULL) {
1887b209c2cSacruz 		refstr_rele(ctp->ctp_svc_fmri);
1897b209c2cSacruz 	}
1907b209c2cSacruz 	if (ctp->ctp_svc_aux != NULL) {
1917b209c2cSacruz 		refstr_rele(ctp->ctp_svc_aux);
1927b209c2cSacruz 	}
1937c478bd9Sstevel@tonic-gate 	kmem_free(template, sizeof (ctmpl_process_t));
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * SAFE_EV is the set of events which a non-privileged process is
1987c478bd9Sstevel@tonic-gate  * allowed to make critical but not fatal or if the PGRPONLY parameter
1997c478bd9Sstevel@tonic-gate  * is set.  EXCESS tells us if "value", a critical event set, requires
2007c478bd9Sstevel@tonic-gate  * additional privilege given the template "ctp".
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate #define	SAFE_EV			(CT_PR_EV_EMPTY)
2037c478bd9Sstevel@tonic-gate #define	EXCESS(ctp, value)	\
2047c478bd9Sstevel@tonic-gate 	(((value) & ~((ctp)->ctp_ev_fatal | SAFE_EV)) || \
2057c478bd9Sstevel@tonic-gate 	(((value) & ~SAFE_EV) && (ctp->ctp_params & CT_PR_PGRPONLY)))
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * ctmpl_process_set
2097c478bd9Sstevel@tonic-gate  *
2107c478bd9Sstevel@tonic-gate  * The process contract template set entry point.  None of the terms
2117c478bd9Sstevel@tonic-gate  * may be unconditionally set, and setting the parameters or fatal
2127c478bd9Sstevel@tonic-gate  * event set may result in events being implicitly removed from to the
2137c478bd9Sstevel@tonic-gate  * critical event set and added to the informative event set.  The
2147c478bd9Sstevel@tonic-gate  * (admittedly subtle) reason we implicitly change the critical event
2157c478bd9Sstevel@tonic-gate  * set when the parameter or fatal event set is modified but not the
2167c478bd9Sstevel@tonic-gate  * other way around is because a change to the critical event set only
2177c478bd9Sstevel@tonic-gate  * affects the contract's owner, whereas a change to the parameter set
2187c478bd9Sstevel@tonic-gate  * and fatal set can affect the execution of the application running in
2197c478bd9Sstevel@tonic-gate  * the contract (and should therefore be only made explicitly).  We
2207c478bd9Sstevel@tonic-gate  * allow implicit changes at all so that setting contract terms doesn't
2217c478bd9Sstevel@tonic-gate  * become a complex dance dependent on the template's initial state and
2227c478bd9Sstevel@tonic-gate  * the desired terms.
2237c478bd9Sstevel@tonic-gate  */
2247c478bd9Sstevel@tonic-gate static int
ctmpl_process_set(struct ct_template * tmpl,ct_kparam_t * kparam,const cred_t * cr)225c5a9a4fcSAntonello Cruz ctmpl_process_set(struct ct_template *tmpl, ct_kparam_t *kparam,
226c5a9a4fcSAntonello Cruz     const cred_t *cr)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	ctmpl_process_t *ctp = tmpl->ctmpl_data;
229c5a9a4fcSAntonello Cruz 	ct_param_t *param = &kparam->param;
2307c478bd9Sstevel@tonic-gate 	contract_t *ct;
2317c478bd9Sstevel@tonic-gate 	int error;
232c6f039c7SToomas Soome 	uint64_t param_value = 0;
2337b209c2cSacruz 	char *str_value;
2347c478bd9Sstevel@tonic-gate 
2357b209c2cSacruz 	if ((param->ctpm_id == CTPP_SVC_FMRI) ||
2367b209c2cSacruz 	    (param->ctpm_id == CTPP_CREATOR_AUX)) {
237c5a9a4fcSAntonello Cruz 		str_value = (char *)kparam->ctpm_kbuf;
2387b209c2cSacruz 		str_value[param->ctpm_size - 1] = '\0';
2397b209c2cSacruz 	} else {
240d170b13aSacruz 		if (param->ctpm_size < sizeof (uint64_t))
241d170b13aSacruz 			return (EINVAL);
242c5a9a4fcSAntonello Cruz 		param_value = *(uint64_t *)kparam->ctpm_kbuf;
2437b209c2cSacruz 		/*
2447b209c2cSacruz 		 * No process contract parameters are > 32 bits.
2457b209c2cSacruz 		 * Unless it is a string.
2467b209c2cSacruz 		 */
2477b209c2cSacruz 		if (param_value & ~UINT32_MAX)
2487b209c2cSacruz 			return (EINVAL);
2497b209c2cSacruz 	}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	switch (param->ctpm_id) {
2527c478bd9Sstevel@tonic-gate 	case CTPP_SUBSUME:
2537b209c2cSacruz 		if (param_value != 0) {
2547c478bd9Sstevel@tonic-gate 			/*
2557c478bd9Sstevel@tonic-gate 			 * Ensure that the contract exists, that we
2567c478bd9Sstevel@tonic-gate 			 * hold the contract, and that the contract is
2577c478bd9Sstevel@tonic-gate 			 * empty.
2587c478bd9Sstevel@tonic-gate 			 */
2597b209c2cSacruz 			ct = contract_type_ptr(process_type, param_value,
2607c478bd9Sstevel@tonic-gate 			    curproc->p_zone->zone_uniqid);
2617c478bd9Sstevel@tonic-gate 			if (ct == NULL)
2627c478bd9Sstevel@tonic-gate 				return (ESRCH);
2637c478bd9Sstevel@tonic-gate 			if (ct->ct_owner != curproc) {
2647c478bd9Sstevel@tonic-gate 				contract_rele(ct);
2657c478bd9Sstevel@tonic-gate 				return (EACCES);
2667c478bd9Sstevel@tonic-gate 			}
2677c478bd9Sstevel@tonic-gate 			if (((cont_process_t *)ct->ct_data)->conp_nmembers) {
2687c478bd9Sstevel@tonic-gate 				contract_rele(ct);
2697c478bd9Sstevel@tonic-gate 				return (ENOTEMPTY);
2707c478bd9Sstevel@tonic-gate 			}
2717c478bd9Sstevel@tonic-gate 		} else {
2727c478bd9Sstevel@tonic-gate 			ct = NULL;
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 		if (ctp->ctp_subsume)
2757c478bd9Sstevel@tonic-gate 			contract_rele(ctp->ctp_subsume);
2767c478bd9Sstevel@tonic-gate 		ctp->ctp_subsume = ct;
2777c478bd9Sstevel@tonic-gate 		break;
2787c478bd9Sstevel@tonic-gate 	case CTPP_PARAMS:
2797b209c2cSacruz 		if (param_value & ~CT_PR_ALLPARAM)
2807c478bd9Sstevel@tonic-gate 			return (EINVAL);
2817b209c2cSacruz 		ctp->ctp_params = param_value;
2827c478bd9Sstevel@tonic-gate 		/*
2837c478bd9Sstevel@tonic-gate 		 * If an unprivileged process requests that
2847c478bd9Sstevel@tonic-gate 		 * CT_PR_PGRPONLY be set, remove any unsafe events from
2857c478bd9Sstevel@tonic-gate 		 * the critical event set and add them to the
2867c478bd9Sstevel@tonic-gate 		 * informative event set.
2877c478bd9Sstevel@tonic-gate 		 */
2887c478bd9Sstevel@tonic-gate 		if ((ctp->ctp_params & CT_PR_PGRPONLY) &&
2897c478bd9Sstevel@tonic-gate 		    EXCESS(ctp, tmpl->ctmpl_ev_crit) &&
2907c478bd9Sstevel@tonic-gate 		    !secpolicy_contract_event_choice(cr)) {
2917c478bd9Sstevel@tonic-gate 			tmpl->ctmpl_ev_info |= (tmpl->ctmpl_ev_crit & ~SAFE_EV);
2927c478bd9Sstevel@tonic-gate 			tmpl->ctmpl_ev_crit &= SAFE_EV;
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 		break;
2967b209c2cSacruz 	case CTPP_SVC_FMRI:
2977b209c2cSacruz 		if (error = secpolicy_contract_identity(cr))
2987b209c2cSacruz 			return (error);
2997b209c2cSacruz 		if (ctp->ctp_svc_fmri != NULL)
3007b209c2cSacruz 			refstr_rele(ctp->ctp_svc_fmri);
3017b209c2cSacruz 		if (strcmp(CT_PR_SVC_DEFAULT, str_value) == 0)
3027b209c2cSacruz 			ctp->ctp_svc_fmri = NULL;
3037b209c2cSacruz 		else
3047b209c2cSacruz 			ctp->ctp_svc_fmri =
3057b209c2cSacruz 			    refstr_alloc(str_value);
3067b209c2cSacruz 		break;
3077b209c2cSacruz 	case CTPP_CREATOR_AUX:
3087b209c2cSacruz 		if (ctp->ctp_svc_aux != NULL)
3097b209c2cSacruz 			refstr_rele(ctp->ctp_svc_aux);
3107b209c2cSacruz 		if (param->ctpm_size == 1) /* empty string */
3117b209c2cSacruz 			ctp->ctp_svc_aux = NULL;
3127b209c2cSacruz 		else
3137b209c2cSacruz 			ctp->ctp_svc_aux =
3147b209c2cSacruz 			    refstr_alloc(str_value);
3157b209c2cSacruz 		break;
3167c478bd9Sstevel@tonic-gate 	case CTP_EV_CRITICAL:
3177c478bd9Sstevel@tonic-gate 		/*
3187c478bd9Sstevel@tonic-gate 		 * We simply don't allow adding events to the critical
3197c478bd9Sstevel@tonic-gate 		 * event set which aren't permitted by our policy or by
3207c478bd9Sstevel@tonic-gate 		 * privilege.
3217c478bd9Sstevel@tonic-gate 		 */
3227b209c2cSacruz 		if (EXCESS(ctp, param_value) &&
3237c478bd9Sstevel@tonic-gate 		    (error = secpolicy_contract_event(cr)) != 0)
3247c478bd9Sstevel@tonic-gate 			return (error);
3257b209c2cSacruz 		tmpl->ctmpl_ev_crit = param_value;
3267c478bd9Sstevel@tonic-gate 		break;
3277c478bd9Sstevel@tonic-gate 	case CTPP_EV_FATAL:
3287b209c2cSacruz 		if (param_value & ~CT_PR_ALLFATAL)
3297c478bd9Sstevel@tonic-gate 			return (EINVAL);
3307b209c2cSacruz 		ctp->ctp_ev_fatal = param_value;
3317c478bd9Sstevel@tonic-gate 		/*
3327c478bd9Sstevel@tonic-gate 		 * Check to see if an unprivileged process is
3337c478bd9Sstevel@tonic-gate 		 * requesting that events be removed from the fatal
3347c478bd9Sstevel@tonic-gate 		 * event set which are still in the critical event set.
3357c478bd9Sstevel@tonic-gate 		 */
3367c478bd9Sstevel@tonic-gate 		if (EXCESS(ctp, tmpl->ctmpl_ev_crit) &&
3377c478bd9Sstevel@tonic-gate 		    !secpolicy_contract_event_choice(cr)) {
3387c478bd9Sstevel@tonic-gate 			int allowed =
3397c478bd9Sstevel@tonic-gate 			    SAFE_EV | (ctp->ctp_params & CT_PR_PGRPONLY) ?
3407c478bd9Sstevel@tonic-gate 			    0 : ctp->ctp_ev_fatal;
3417c478bd9Sstevel@tonic-gate 			tmpl->ctmpl_ev_info |= (tmpl->ctmpl_ev_crit & ~allowed);
3427c478bd9Sstevel@tonic-gate 			tmpl->ctmpl_ev_crit &= allowed;
3437c478bd9Sstevel@tonic-gate 		}
3447c478bd9Sstevel@tonic-gate 		break;
3457c478bd9Sstevel@tonic-gate 	default:
3467c478bd9Sstevel@tonic-gate 		return (EINVAL);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	return (0);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate  * ctmpl_process_get
3547c478bd9Sstevel@tonic-gate  *
3557c478bd9Sstevel@tonic-gate  * The process contract template get entry point.  Simply fetches and
3567c478bd9Sstevel@tonic-gate  * returns the requested term.
3577c478bd9Sstevel@tonic-gate  */
3587c478bd9Sstevel@tonic-gate static int
ctmpl_process_get(struct ct_template * template,ct_kparam_t * kparam)359c5a9a4fcSAntonello Cruz ctmpl_process_get(struct ct_template *template, ct_kparam_t *kparam)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate 	ctmpl_process_t *ctp = template->ctmpl_data;
362c5a9a4fcSAntonello Cruz 	ct_param_t *param = &kparam->param;
363c5a9a4fcSAntonello Cruz 	uint64_t *param_value = kparam->ctpm_kbuf;
3647c478bd9Sstevel@tonic-gate 
365d170b13aSacruz 	if (param->ctpm_id == CTPP_SUBSUME ||
366d170b13aSacruz 	    param->ctpm_id == CTPP_PARAMS ||
367d170b13aSacruz 	    param->ctpm_id == CTPP_EV_FATAL) {
368d170b13aSacruz 		if (param->ctpm_size < sizeof (uint64_t))
369d170b13aSacruz 			return (EINVAL);
370c5a9a4fcSAntonello Cruz 		kparam->ret_size = sizeof (uint64_t);
371d170b13aSacruz 	}
372d170b13aSacruz 
3737c478bd9Sstevel@tonic-gate 	switch (param->ctpm_id) {
3747c478bd9Sstevel@tonic-gate 	case CTPP_SUBSUME:
3757b209c2cSacruz 		*param_value = ctp->ctp_subsume ?
3767c478bd9Sstevel@tonic-gate 		    ctp->ctp_subsume->ct_id : 0;
3777c478bd9Sstevel@tonic-gate 		break;
3787c478bd9Sstevel@tonic-gate 	case CTPP_PARAMS:
3797b209c2cSacruz 		*param_value = ctp->ctp_params;
3807b209c2cSacruz 		break;
3817b209c2cSacruz 	case CTPP_SVC_FMRI:
3827b209c2cSacruz 		if (ctp->ctp_svc_fmri == NULL) {
383c5a9a4fcSAntonello Cruz 			kparam->ret_size =
384c5a9a4fcSAntonello Cruz 			    strlcpy((char *)kparam->ctpm_kbuf,
3857b209c2cSacruz 			    CT_PR_SVC_DEFAULT, param->ctpm_size);
3867b209c2cSacruz 		} else {
387c5a9a4fcSAntonello Cruz 			kparam->ret_size =
388c5a9a4fcSAntonello Cruz 			    strlcpy((char *)kparam->ctpm_kbuf,
3897b209c2cSacruz 			    refstr_value(ctp->ctp_svc_fmri), param->ctpm_size);
3907b209c2cSacruz 		}
391c5a9a4fcSAntonello Cruz 		kparam->ret_size++;
3927b209c2cSacruz 		break;
3937b209c2cSacruz 	case CTPP_CREATOR_AUX:
3947b209c2cSacruz 		if (ctp->ctp_svc_aux == NULL) {
395c5a9a4fcSAntonello Cruz 			kparam->ret_size =
396c5a9a4fcSAntonello Cruz 			    strlcpy((char *)kparam->ctpm_kbuf,
3977b209c2cSacruz 			    refstr_value(conp_svc_aux_default),
3987b209c2cSacruz 			    param->ctpm_size);
3997b209c2cSacruz 		} else {
400c5a9a4fcSAntonello Cruz 			kparam->ret_size =
401c5a9a4fcSAntonello Cruz 			    strlcpy((char *)kparam->ctpm_kbuf,
4027b209c2cSacruz 			    refstr_value(ctp->ctp_svc_aux), param->ctpm_size);
4037b209c2cSacruz 		}
404c5a9a4fcSAntonello Cruz 		kparam->ret_size++;
4057c478bd9Sstevel@tonic-gate 		break;
4067c478bd9Sstevel@tonic-gate 	case CTPP_EV_FATAL:
4077b209c2cSacruz 		*param_value = ctp->ctp_ev_fatal;
4087c478bd9Sstevel@tonic-gate 		break;
4097c478bd9Sstevel@tonic-gate 	default:
4107c478bd9Sstevel@tonic-gate 		return (EINVAL);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	return (0);
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate static ctmplops_t ctmpl_process_ops = {
4177c478bd9Sstevel@tonic-gate 	ctmpl_process_dup,		/* ctop_dup */
4187c478bd9Sstevel@tonic-gate 	ctmpl_process_free,		/* ctop_free */
4197c478bd9Sstevel@tonic-gate 	ctmpl_process_set,		/* ctop_set */
4207c478bd9Sstevel@tonic-gate 	ctmpl_process_get,		/* ctop_get */
4217c478bd9Sstevel@tonic-gate 	ctmpl_create_inval,		/* ctop_create */
4227c478bd9Sstevel@tonic-gate 	CT_PR_ALLEVENT
4237c478bd9Sstevel@tonic-gate };
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate  * Process contract implementation
4287c478bd9Sstevel@tonic-gate  */
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate /*
4317c478bd9Sstevel@tonic-gate  * ctmpl_process_default
4327c478bd9Sstevel@tonic-gate  *
4337c478bd9Sstevel@tonic-gate  * The process contract default template entry point.  Creates a
4347c478bd9Sstevel@tonic-gate  * process contract template with no parameters set, with informative
4357c478bd9Sstevel@tonic-gate  * core and signal events, critical empty and hwerr events, and fatal
4367c478bd9Sstevel@tonic-gate  * hwerr events.
4377c478bd9Sstevel@tonic-gate  */
4387c478bd9Sstevel@tonic-gate static ct_template_t *
contract_process_default(void)4397c478bd9Sstevel@tonic-gate contract_process_default(void)
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	ctmpl_process_t *new;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	new = kmem_alloc(sizeof (ctmpl_process_t), KM_SLEEP);
4447c478bd9Sstevel@tonic-gate 	ctmpl_init(&new->ctp_ctmpl, &ctmpl_process_ops, process_type, new);
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	new->ctp_subsume = NULL;
4477c478bd9Sstevel@tonic-gate 	new->ctp_params = 0;
4487c478bd9Sstevel@tonic-gate 	new->ctp_ctmpl.ctmpl_ev_info = CT_PR_EV_CORE | CT_PR_EV_SIGNAL;
4497c478bd9Sstevel@tonic-gate 	new->ctp_ctmpl.ctmpl_ev_crit = CT_PR_EV_EMPTY | CT_PR_EV_HWERR;
4507c478bd9Sstevel@tonic-gate 	new->ctp_ev_fatal = CT_PR_EV_HWERR;
4517b209c2cSacruz 	new->ctp_svc_fmri = NULL;
4527b209c2cSacruz 	new->ctp_svc_aux = NULL;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	return (&new->ctp_ctmpl);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate /*
4587c478bd9Sstevel@tonic-gate  * contract_process_free
4597c478bd9Sstevel@tonic-gate  *
4607c478bd9Sstevel@tonic-gate  * The process contract free entry point.
4617c478bd9Sstevel@tonic-gate  */
4627c478bd9Sstevel@tonic-gate static void
contract_process_free(contract_t * ct)4637c478bd9Sstevel@tonic-gate contract_process_free(contract_t *ct)
4647c478bd9Sstevel@tonic-gate {
4657c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
4667c478bd9Sstevel@tonic-gate 	crfree(ctp->conp_cred);
4677c478bd9Sstevel@tonic-gate 	list_destroy(&ctp->conp_members);
4687c478bd9Sstevel@tonic-gate 	list_destroy(&ctp->conp_inherited);
4697b209c2cSacruz 	if (ctp->conp_svc_fmri != NULL) {
4707b209c2cSacruz 		refstr_rele(ctp->conp_svc_fmri);
4717b209c2cSacruz 	}
4727b209c2cSacruz 	if (ctp->conp_svc_aux != NULL) {
4737b209c2cSacruz 		refstr_rele(ctp->conp_svc_aux);
4747b209c2cSacruz 	}
4757b209c2cSacruz 	if (ctp->conp_svc_creator != NULL) {
4767b209c2cSacruz 		refstr_rele(ctp->conp_svc_creator);
4777b209c2cSacruz 	}
4787c478bd9Sstevel@tonic-gate 	kmem_free(ctp, sizeof (cont_process_t));
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate /*
4827c478bd9Sstevel@tonic-gate  * contract_process_cankill
4837c478bd9Sstevel@tonic-gate  *
4847c478bd9Sstevel@tonic-gate  * Determine if the contract author had or if the process generating
4857c478bd9Sstevel@tonic-gate  * the event, sp, has adequate privileges to kill process tp.
4867c478bd9Sstevel@tonic-gate  */
4877c478bd9Sstevel@tonic-gate static int
contract_process_cankill(proc_t * tp,proc_t * sp,cont_process_t * ctp)4887c478bd9Sstevel@tonic-gate contract_process_cankill(proc_t *tp, proc_t *sp, cont_process_t *ctp)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	int cankill;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	mutex_enter(&tp->p_crlock);
4937c478bd9Sstevel@tonic-gate 	cankill = hasprocperm(tp->p_cred, ctp->conp_cred);
4947c478bd9Sstevel@tonic-gate 	mutex_exit(&tp->p_crlock);
4957c478bd9Sstevel@tonic-gate 	if (cankill || (sp && prochasprocperm(tp, sp, CRED())))
4967c478bd9Sstevel@tonic-gate 		return (1);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	return (0);
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate /*
5027c478bd9Sstevel@tonic-gate  * contract_process_kill
5037c478bd9Sstevel@tonic-gate  *
5047c478bd9Sstevel@tonic-gate  * Kills all processes in a contract, or all processes in the
5057c478bd9Sstevel@tonic-gate  * intersection of a contract and ex's process group (if ex is non-NULL
5067c478bd9Sstevel@tonic-gate  * and the contract's PGRPONLY parameter is set).  If checkpriv is
5077c478bd9Sstevel@tonic-gate  * true, only those processes which may be signaled by the contract
5087c478bd9Sstevel@tonic-gate  * author or ex are killed.
5097c478bd9Sstevel@tonic-gate  */
5107c478bd9Sstevel@tonic-gate static void
contract_process_kill(contract_t * ct,proc_t * ex,int checkpriv)5117c478bd9Sstevel@tonic-gate contract_process_kill(contract_t *ct, proc_t *ex, int checkpriv)
5127c478bd9Sstevel@tonic-gate {
5137c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
5147c478bd9Sstevel@tonic-gate 	proc_t *p;
5157c478bd9Sstevel@tonic-gate 	pid_t pgrp = -1;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	if (ex && (ctp->conp_params & CT_PR_PGRPONLY)) {
5207c478bd9Sstevel@tonic-gate 		pgrp = ex->p_pgrp;
5217c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	for (p = list_head(&ctp->conp_members); p != NULL;
5257c478bd9Sstevel@tonic-gate 	    p = list_next(&ctp->conp_members, p)) {
5266e092be7SVamsi Nagineni 		if ((p == ex) ||
5276e092be7SVamsi Nagineni 		    (pgrp != -1 && (p->p_stat == SIDL || p->p_pgrp != pgrp)) ||
5287c478bd9Sstevel@tonic-gate 		    (checkpriv && !contract_process_cankill(p, ex, ctp)))
5297c478bd9Sstevel@tonic-gate 			continue;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 		psignal(p, SIGKILL);
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	if (pgrp != -1)
5357c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate  * contract_process_accept
5417c478bd9Sstevel@tonic-gate  *
5427c478bd9Sstevel@tonic-gate  * Tests if the process contract is willing to act as a regent for
5437c478bd9Sstevel@tonic-gate  * inherited contracts.  Though brief and only called from one place,
5447c478bd9Sstevel@tonic-gate  * this functionality is kept here to avoid including knowledge of
5457c478bd9Sstevel@tonic-gate  * process contract implementation in the generic contract code.
5467c478bd9Sstevel@tonic-gate  */
5477c478bd9Sstevel@tonic-gate int
contract_process_accept(contract_t * parent)5487c478bd9Sstevel@tonic-gate contract_process_accept(contract_t *parent)
5497c478bd9Sstevel@tonic-gate {
5507c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = parent->ct_data;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	ASSERT(parent->ct_type == process_type);
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	return (ctp->conp_params & CT_PR_REGENT);
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate /*
5587c478bd9Sstevel@tonic-gate  * contract_process_take
5597c478bd9Sstevel@tonic-gate  *
5607c478bd9Sstevel@tonic-gate  * Executes the process contract side of inheriting a contract.
5617c478bd9Sstevel@tonic-gate  */
5627c478bd9Sstevel@tonic-gate void
contract_process_take(contract_t * parent,contract_t * child)5637c478bd9Sstevel@tonic-gate contract_process_take(contract_t *parent, contract_t *child)
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = parent->ct_data;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&parent->ct_lock));
5687c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&child->ct_lock));
5697c478bd9Sstevel@tonic-gate 	ASSERT(parent->ct_type == process_type);
5707c478bd9Sstevel@tonic-gate 	ASSERT(ctp->conp_params & CT_PR_REGENT);
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	list_insert_head(&ctp->conp_inherited, child);
5737c478bd9Sstevel@tonic-gate 	ctp->conp_ninherited++;
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate /*
5777c478bd9Sstevel@tonic-gate  * contract_process_adopt
5787c478bd9Sstevel@tonic-gate  *
5797c478bd9Sstevel@tonic-gate  * Executes the process contract side of adopting a contract.
5807c478bd9Sstevel@tonic-gate  */
5817c478bd9Sstevel@tonic-gate void
contract_process_adopt(contract_t * ct,proc_t * p)5827c478bd9Sstevel@tonic-gate contract_process_adopt(contract_t *ct, proc_t *p)
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 	cont_process_t *parent = p->p_ct_process;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&parent->conp_contract.ct_lock));
5877c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	list_remove(&parent->conp_inherited, ct);
5907c478bd9Sstevel@tonic-gate 	parent->conp_ninherited--;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * We drop the parent lock first because a) we are passing the
5947c478bd9Sstevel@tonic-gate 	 * contract reference to the child, and b) contract_adopt
5957c478bd9Sstevel@tonic-gate 	 * expects us to return with the contract lock held.
5967c478bd9Sstevel@tonic-gate 	 */
5977c478bd9Sstevel@tonic-gate 	mutex_exit(&parent->conp_contract.ct_lock);
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate /*
60125e8c5aaSvikram  * contract_process_abandon
6027c478bd9Sstevel@tonic-gate  *
6037c478bd9Sstevel@tonic-gate  * The process contract abandon entry point.
6047c478bd9Sstevel@tonic-gate  */
6057c478bd9Sstevel@tonic-gate static void
contract_process_abandon(contract_t * ct)6067c478bd9Sstevel@tonic-gate contract_process_abandon(contract_t *ct)
6077c478bd9Sstevel@tonic-gate {
6087c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	/*
6137c478bd9Sstevel@tonic-gate 	 * Shall we stay or shall we go?
6147c478bd9Sstevel@tonic-gate 	 */
6157c478bd9Sstevel@tonic-gate 	if (list_head(&ctp->conp_members) == NULL) {
6167c478bd9Sstevel@tonic-gate 		contract_destroy(ct);
6177c478bd9Sstevel@tonic-gate 	} else {
6187c478bd9Sstevel@tonic-gate 		/*
6197c478bd9Sstevel@tonic-gate 		 * Strictly speaking, we actually do orphan the contract.
6207c478bd9Sstevel@tonic-gate 		 * Assuming our credentials allow us to kill all
6217c478bd9Sstevel@tonic-gate 		 * processes in the contract, this is only temporary.
6227c478bd9Sstevel@tonic-gate 		 */
6237c478bd9Sstevel@tonic-gate 		if (ctp->conp_params & CT_PR_NOORPHAN)
6247c478bd9Sstevel@tonic-gate 			contract_process_kill(ct, NULL, B_TRUE);
6257c478bd9Sstevel@tonic-gate 		contract_orphan(ct);
6267c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
6277c478bd9Sstevel@tonic-gate 		contract_rele(ct);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate /*
6327c478bd9Sstevel@tonic-gate  * contract_process_destroy
6337c478bd9Sstevel@tonic-gate  *
6347c478bd9Sstevel@tonic-gate  * The process contract destroy entry point.
6357c478bd9Sstevel@tonic-gate  */
6367c478bd9Sstevel@tonic-gate static void
contract_process_destroy(contract_t * ct)6377c478bd9Sstevel@tonic-gate contract_process_destroy(contract_t *ct)
6387c478bd9Sstevel@tonic-gate {
6397c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
6407c478bd9Sstevel@tonic-gate 	contract_t *cct;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	/*
6457c478bd9Sstevel@tonic-gate 	 * contract_destroy all empty children, kill or orphan the rest
6467c478bd9Sstevel@tonic-gate 	 */
6477c478bd9Sstevel@tonic-gate 	while (cct = list_head(&ctp->conp_inherited)) {
6487c478bd9Sstevel@tonic-gate 		mutex_enter(&cct->ct_lock);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		ASSERT(cct->ct_state == CTS_INHERITED);
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 		list_remove(&ctp->conp_inherited, cct);
6537c478bd9Sstevel@tonic-gate 		ctp->conp_ninherited--;
6547c478bd9Sstevel@tonic-gate 		cct->ct_regent = NULL;
6557c478bd9Sstevel@tonic-gate 		cct->ct_type->ct_type_ops->contop_abandon(cct);
6567c478bd9Sstevel@tonic-gate 	}
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate  * contract_process_status
6617c478bd9Sstevel@tonic-gate  *
6627c478bd9Sstevel@tonic-gate  * The process contract status entry point.
6637c478bd9Sstevel@tonic-gate  */
6647c478bd9Sstevel@tonic-gate static void
contract_process_status(contract_t * ct,zone_t * zone,int detail,nvlist_t * nvl,void * status,model_t model)6657c478bd9Sstevel@tonic-gate contract_process_status(contract_t *ct, zone_t *zone, int detail, nvlist_t *nvl,
6667c478bd9Sstevel@tonic-gate     void *status, model_t model)
6677c478bd9Sstevel@tonic-gate {
6687c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
6697c478bd9Sstevel@tonic-gate 	uint32_t *pids, *ctids;
6707c478bd9Sstevel@tonic-gate 	uint_t npids, nctids;
6717c478bd9Sstevel@tonic-gate 	uint_t spids, sctids;
6727b209c2cSacruz 	ctid_t local_svc_zone_enter;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	if (detail == CTD_FIXED) {
6757c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
6767c478bd9Sstevel@tonic-gate 		contract_status_common(ct, zone, status, model);
6777b209c2cSacruz 		local_svc_zone_enter = ctp->conp_svc_zone_enter;
6787c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
6797c478bd9Sstevel@tonic-gate 	} else {
6807c478bd9Sstevel@tonic-gate 		contract_t *cnext;
6817c478bd9Sstevel@tonic-gate 		proc_t *pnext;
6827c478bd9Sstevel@tonic-gate 		uint_t loc;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		ASSERT(detail == CTD_ALL);
6857c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
6867c478bd9Sstevel@tonic-gate 		for (;;) {
6877c478bd9Sstevel@tonic-gate 			spids = ctp->conp_nmembers + 5;
6887c478bd9Sstevel@tonic-gate 			sctids = ctp->conp_ninherited + 5;
6897c478bd9Sstevel@tonic-gate 			mutex_exit(&ct->ct_lock);
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 			pids = kmem_alloc(spids * sizeof (uint32_t), KM_SLEEP);
6927c478bd9Sstevel@tonic-gate 			ctids = kmem_alloc(sctids * sizeof (uint32_t),
6937c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 			mutex_enter(&ct->ct_lock);
6967c478bd9Sstevel@tonic-gate 			npids = ctp->conp_nmembers;
6977c478bd9Sstevel@tonic-gate 			nctids = ctp->conp_ninherited;
6987c478bd9Sstevel@tonic-gate 			if (spids >= npids && sctids >= nctids)
6997c478bd9Sstevel@tonic-gate 				break;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 			kmem_free(pids, spids * sizeof (uint32_t));
7027c478bd9Sstevel@tonic-gate 			kmem_free(ctids, sctids * sizeof (uint32_t));
7037c478bd9Sstevel@tonic-gate 		}
7047c478bd9Sstevel@tonic-gate 		contract_status_common(ct, zone, status, model);
7057c478bd9Sstevel@tonic-gate 		for (loc = 0, cnext = list_head(&ctp->conp_inherited); cnext;
7067c478bd9Sstevel@tonic-gate 		    cnext = list_next(&ctp->conp_inherited, cnext))
7077c478bd9Sstevel@tonic-gate 			ctids[loc++] = cnext->ct_id;
7087c478bd9Sstevel@tonic-gate 		ASSERT(loc == nctids);
7097c478bd9Sstevel@tonic-gate 		for (loc = 0, pnext = list_head(&ctp->conp_members); pnext;
7107c478bd9Sstevel@tonic-gate 		    pnext = list_next(&ctp->conp_members, pnext))
7117c478bd9Sstevel@tonic-gate 			pids[loc++] = pnext->p_pid;
7127c478bd9Sstevel@tonic-gate 		ASSERT(loc == npids);
7137b209c2cSacruz 		local_svc_zone_enter = ctp->conp_svc_zone_enter;
7147c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	/*
7187c478bd9Sstevel@tonic-gate 	 * Contract terms are static; there's no need to hold the
7197c478bd9Sstevel@tonic-gate 	 * contract lock while accessing them.
7207c478bd9Sstevel@tonic-gate 	 */
7217c478bd9Sstevel@tonic-gate 	VERIFY(nvlist_add_uint32(nvl, CTPS_PARAMS, ctp->conp_params) == 0);
7227c478bd9Sstevel@tonic-gate 	VERIFY(nvlist_add_uint32(nvl, CTPS_EV_FATAL, ctp->conp_ev_fatal) == 0);
7237c478bd9Sstevel@tonic-gate 	if (detail == CTD_ALL) {
7247c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32_array(nvl, CTPS_MEMBERS, pids,
7257c478bd9Sstevel@tonic-gate 		    npids) == 0);
7267c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32_array(nvl, CTPS_CONTRACTS, ctids,
7277c478bd9Sstevel@tonic-gate 		    nctids) == 0);
7287b209c2cSacruz 		VERIFY(nvlist_add_string(nvl, CTPS_CREATOR_AUX,
7297b209c2cSacruz 		    refstr_value(ctp->conp_svc_aux)) == 0);
7307b209c2cSacruz 		VERIFY(nvlist_add_string(nvl, CTPS_SVC_CREATOR,
7317b209c2cSacruz 		    refstr_value(ctp->conp_svc_creator)) == 0);
7327c478bd9Sstevel@tonic-gate 		kmem_free(pids, spids * sizeof (uint32_t));
7337c478bd9Sstevel@tonic-gate 		kmem_free(ctids, sctids * sizeof (uint32_t));
7347c478bd9Sstevel@tonic-gate 	}
7357b209c2cSacruz 
7367b209c2cSacruz 	/*
7377b209c2cSacruz 	 * if we are in a local zone and svc_fmri was inherited from
7387b209c2cSacruz 	 * the global zone, we provide fake svc_fmri and svc_ctid
7397b209c2cSacruz 	 */
740a2e92fdbSJohn Levon 	if (local_svc_zone_enter == 0 ||
7417b209c2cSacruz 	    zone->zone_uniqid == GLOBAL_ZONEUNIQID) {
7427b209c2cSacruz 		if (detail > CTD_COMMON) {
7437b209c2cSacruz 			VERIFY(nvlist_add_int32(nvl, CTPS_SVC_CTID,
7447b209c2cSacruz 			    ctp->conp_svc_ctid) == 0);
7457b209c2cSacruz 			VERIFY(nvlist_add_string(nvl, CTPS_SVC_FMRI,
7467b209c2cSacruz 			    refstr_value(ctp->conp_svc_fmri)) == 0);
7477b209c2cSacruz 		}
7487b209c2cSacruz 	} else {
7497b209c2cSacruz 		if (detail > CTD_COMMON) {
7507b209c2cSacruz 			VERIFY(nvlist_add_int32(nvl, CTPS_SVC_CTID,
7517b209c2cSacruz 			    local_svc_zone_enter) == 0);
7527b209c2cSacruz 			VERIFY(nvlist_add_string(nvl, CTPS_SVC_FMRI,
7537b209c2cSacruz 			    CT_PR_SVC_FMRI_ZONE_ENTER) == 0);
7547b209c2cSacruz 		}
7557b209c2cSacruz 	}
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate 
75825e8c5aaSvikram /*ARGSUSED*/
75925e8c5aaSvikram static int
contract_process_newct(contract_t * ct)76025e8c5aaSvikram contract_process_newct(contract_t *ct)
76125e8c5aaSvikram {
76225e8c5aaSvikram 	return (0);
76325e8c5aaSvikram }
76425e8c5aaSvikram 
76525e8c5aaSvikram /* process contracts don't negotiate */
7667c478bd9Sstevel@tonic-gate static contops_t contract_process_ops = {
7677c478bd9Sstevel@tonic-gate 	contract_process_free,		/* contop_free */
7687c478bd9Sstevel@tonic-gate 	contract_process_abandon,	/* contop_abandon */
7697c478bd9Sstevel@tonic-gate 	contract_process_destroy,	/* contop_destroy */
77025e8c5aaSvikram 	contract_process_status,	/* contop_status */
77125e8c5aaSvikram 	contract_ack_inval,		/* contop_ack */
77225e8c5aaSvikram 	contract_ack_inval,		/* contop_nack */
77325e8c5aaSvikram 	contract_qack_inval,		/* contop_qack */
77425e8c5aaSvikram 	contract_process_newct		/* contop_newct */
7757c478bd9Sstevel@tonic-gate };
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate /*
7787c478bd9Sstevel@tonic-gate  * contract_process_init
7797c478bd9Sstevel@tonic-gate  *
7807c478bd9Sstevel@tonic-gate  * Initializes the process contract type.  Also creates a template for
7817c478bd9Sstevel@tonic-gate  * use by newproc() when it creates user processes.
7827c478bd9Sstevel@tonic-gate  */
7837c478bd9Sstevel@tonic-gate void
contract_process_init(void)7847c478bd9Sstevel@tonic-gate contract_process_init(void)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	process_type = contract_type_init(CTT_PROCESS, "process",
7877c478bd9Sstevel@tonic-gate 	    &contract_process_ops, contract_process_default);
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	/*
790*bbf21555SRichard Lowe 	 * Create a template for use with init(8) and other
7917c478bd9Sstevel@tonic-gate 	 * kernel-started processes.
7927c478bd9Sstevel@tonic-gate 	 */
7937c478bd9Sstevel@tonic-gate 	sys_process_tmpl = kmem_alloc(sizeof (ctmpl_process_t), KM_SLEEP);
7947c478bd9Sstevel@tonic-gate 	ctmpl_init(&sys_process_tmpl->ctp_ctmpl, &ctmpl_process_ops,
7957c478bd9Sstevel@tonic-gate 	    process_type, sys_process_tmpl);
7967c478bd9Sstevel@tonic-gate 	sys_process_tmpl->ctp_subsume = NULL;
7977c478bd9Sstevel@tonic-gate 	sys_process_tmpl->ctp_params = CT_PR_NOORPHAN;
7987c478bd9Sstevel@tonic-gate 	sys_process_tmpl->ctp_ev_fatal = CT_PR_EV_HWERR;
7997b209c2cSacruz 	sys_process_tmpl->ctp_svc_fmri =
8007b209c2cSacruz 	    refstr_alloc("svc:/system/init:default");
8017b209c2cSacruz 	sys_process_tmpl->ctp_svc_aux = refstr_alloc("");
8027b209c2cSacruz 	conp_svc_aux_default = sys_process_tmpl->ctp_svc_aux;
8037b209c2cSacruz 	refstr_hold(conp_svc_aux_default);
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate /*
8077c478bd9Sstevel@tonic-gate  * contract_process_create
8087c478bd9Sstevel@tonic-gate  *
8097c478bd9Sstevel@tonic-gate  * create a process contract given template "tmpl" and parent process
8107c478bd9Sstevel@tonic-gate  * "parent".  May fail and return NULL if project.max-contracts would
8117c478bd9Sstevel@tonic-gate  * have been exceeded.
8127c478bd9Sstevel@tonic-gate  */
8137c478bd9Sstevel@tonic-gate static cont_process_t *
contract_process_create(ctmpl_process_t * tmpl,proc_t * parent,int canfail)8147c478bd9Sstevel@tonic-gate contract_process_create(ctmpl_process_t *tmpl, proc_t *parent, int canfail)
8157c478bd9Sstevel@tonic-gate {
8167c478bd9Sstevel@tonic-gate 	cont_process_t *ctp;
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	ASSERT(tmpl != NULL);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	(void) contract_type_pbundle(process_type, parent);
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	ctp = kmem_zalloc(sizeof (cont_process_t), KM_SLEEP);
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	list_create(&ctp->conp_members, sizeof (proc_t),
8257c478bd9Sstevel@tonic-gate 	    offsetof(proc_t, p_ct_member));
8267c478bd9Sstevel@tonic-gate 	list_create(&ctp->conp_inherited, sizeof (contract_t),
8277c478bd9Sstevel@tonic-gate 	    offsetof(contract_t, ct_ctlist));
8287c478bd9Sstevel@tonic-gate 	mutex_enter(&tmpl->ctp_ctmpl.ctmpl_lock);
8297c478bd9Sstevel@tonic-gate 	ctp->conp_params = tmpl->ctp_params;
8307c478bd9Sstevel@tonic-gate 	ctp->conp_ev_fatal = tmpl->ctp_ev_fatal;
8317c478bd9Sstevel@tonic-gate 	crhold(ctp->conp_cred = CRED());
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	if (contract_ctor(&ctp->conp_contract, process_type, &tmpl->ctp_ctmpl,
8347c478bd9Sstevel@tonic-gate 	    ctp, (ctp->conp_params & CT_PR_INHERIT) ? CTF_INHERIT : 0,
8357c478bd9Sstevel@tonic-gate 	    parent, canfail)) {
8367c478bd9Sstevel@tonic-gate 		mutex_exit(&tmpl->ctp_ctmpl.ctmpl_lock);
8377c478bd9Sstevel@tonic-gate 		contract_process_free(&ctp->conp_contract);
8387c478bd9Sstevel@tonic-gate 		return (NULL);
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate 
8417b209c2cSacruz 	/*
8427b209c2cSacruz 	 * inherit svc_fmri if not defined by consumer. In this case, inherit
8437b209c2cSacruz 	 * also svc_ctid to keep track of the contract id where
8447b209c2cSacruz 	 * svc_fmri was set
8457b209c2cSacruz 	 */
8467b209c2cSacruz 	if (tmpl->ctp_svc_fmri == NULL) {
8477b209c2cSacruz 		ctp->conp_svc_fmri = parent->p_ct_process->conp_svc_fmri;
8487b209c2cSacruz 		ctp->conp_svc_ctid = parent->p_ct_process->conp_svc_ctid;
8497b209c2cSacruz 		ctp->conp_svc_zone_enter =
8507b209c2cSacruz 		    parent->p_ct_process->conp_svc_zone_enter;
8517b209c2cSacruz 	} else {
8527b209c2cSacruz 		ctp->conp_svc_fmri = tmpl->ctp_svc_fmri;
8537b209c2cSacruz 		ctp->conp_svc_ctid = ctp->conp_contract.ct_id;
8547b209c2cSacruz 		/* make svc_zone_enter flag false when svc_fmri is set */
8557b209c2cSacruz 		ctp->conp_svc_zone_enter = 0;
8567b209c2cSacruz 	}
8577b209c2cSacruz 	refstr_hold(ctp->conp_svc_fmri);
8587b209c2cSacruz 	/* set svc_aux to default value if not defined in template */
8597b209c2cSacruz 	if (tmpl->ctp_svc_aux == NULL) {
8607b209c2cSacruz 		ctp->conp_svc_aux = conp_svc_aux_default;
8617b209c2cSacruz 	} else {
8627b209c2cSacruz 		ctp->conp_svc_aux = tmpl->ctp_svc_aux;
8637b209c2cSacruz 	}
8647b209c2cSacruz 	refstr_hold(ctp->conp_svc_aux);
8657b209c2cSacruz 	/*
8667b209c2cSacruz 	 * set svc_creator to execname
8677b209c2cSacruz 	 * We special case pid0 because when newproc() creates
8687b209c2cSacruz 	 * the init process, the p_user.u_comm field of sched's proc_t
8697b209c2cSacruz 	 * has not been populated yet.
8707b209c2cSacruz 	 */
8717b209c2cSacruz 	if (parent->p_pidp == &pid0) /* if the kernel is the creator */
8727b209c2cSacruz 		ctp->conp_svc_creator = refstr_alloc("sched");
8737b209c2cSacruz 	else
8747b209c2cSacruz 		ctp->conp_svc_creator = refstr_alloc(parent->p_user.u_comm);
8757b209c2cSacruz 
8767c478bd9Sstevel@tonic-gate 	/*
8777c478bd9Sstevel@tonic-gate 	 * Transfer subcontracts only after new contract is visible.
8787c478bd9Sstevel@tonic-gate 	 * Also, only transfer contracts if the parent matches -- we
8797c478bd9Sstevel@tonic-gate 	 * don't want to create a cycle in the tree of contracts.
8807c478bd9Sstevel@tonic-gate 	 */
8817c478bd9Sstevel@tonic-gate 	if (tmpl->ctp_subsume && tmpl->ctp_subsume->ct_owner == parent) {
8827c478bd9Sstevel@tonic-gate 		cont_process_t *sct = tmpl->ctp_subsume->ct_data;
8837c478bd9Sstevel@tonic-gate 		contract_t *ct;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		mutex_enter(&tmpl->ctp_subsume->ct_lock);
8867c478bd9Sstevel@tonic-gate 		mutex_enter(&ctp->conp_contract.ct_lock);
8877c478bd9Sstevel@tonic-gate 		while (ct = list_head(&sct->conp_inherited)) {
8887c478bd9Sstevel@tonic-gate 			mutex_enter(&ct->ct_lock);
8897c478bd9Sstevel@tonic-gate 			list_remove(&sct->conp_inherited, ct);
8907c478bd9Sstevel@tonic-gate 			list_insert_tail(&ctp->conp_inherited, ct);
8917c478bd9Sstevel@tonic-gate 			ct->ct_regent = &ctp->conp_contract;
8927c478bd9Sstevel@tonic-gate 			mutex_exit(&ct->ct_lock);
8937c478bd9Sstevel@tonic-gate 		}
8947c478bd9Sstevel@tonic-gate 		ctp->conp_ninherited += sct->conp_ninherited;
8957c478bd9Sstevel@tonic-gate 		sct->conp_ninherited = 0;
8967c478bd9Sstevel@tonic-gate 		mutex_exit(&ctp->conp_contract.ct_lock);
8977c478bd9Sstevel@tonic-gate 		mutex_exit(&tmpl->ctp_subsume->ct_lock);
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 		/*
9007c478bd9Sstevel@tonic-gate 		 * Automatically abandon the contract.
9017c478bd9Sstevel@tonic-gate 		 */
9027c478bd9Sstevel@tonic-gate 		(void) contract_abandon(tmpl->ctp_subsume, parent, 1);
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	mutex_exit(&tmpl->ctp_ctmpl.ctmpl_lock);
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	return (ctp);
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate /*
9117c478bd9Sstevel@tonic-gate  * contract_process_exit
9127c478bd9Sstevel@tonic-gate  *
9137c478bd9Sstevel@tonic-gate  * Called on process exit.  Removes process p from process contract
9147c478bd9Sstevel@tonic-gate  * ctp.  Generates an exit event, if requested.  Generates an empty
9157c478bd9Sstevel@tonic-gate  * event, if p is the last member of the the process contract and empty
9167c478bd9Sstevel@tonic-gate  * events were requested.
9177c478bd9Sstevel@tonic-gate  */
9187c478bd9Sstevel@tonic-gate void
contract_process_exit(cont_process_t * ctp,proc_t * p,int exitstatus)9197c478bd9Sstevel@tonic-gate contract_process_exit(cont_process_t *ctp, proc_t *p, int exitstatus)
9207c478bd9Sstevel@tonic-gate {
9217c478bd9Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
9227c478bd9Sstevel@tonic-gate 	ct_kevent_t *event;
9237c478bd9Sstevel@tonic-gate 	int empty;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	/*
9267c478bd9Sstevel@tonic-gate 	 * Remove self from process contract.
9277c478bd9Sstevel@tonic-gate 	 */
9287c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
9297c478bd9Sstevel@tonic-gate 	list_remove(&ctp->conp_members, p);
9307c478bd9Sstevel@tonic-gate 	ctp->conp_nmembers--;
9317c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);	/* in case /proc is watching */
9327c478bd9Sstevel@tonic-gate 	p->p_ct_process = NULL;
9337c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	/*
9367c478bd9Sstevel@tonic-gate 	 * We check for emptiness before dropping the contract lock to
9377c478bd9Sstevel@tonic-gate 	 * send the exit event, otherwise we could end up with two
9387c478bd9Sstevel@tonic-gate 	 * empty events.
9397c478bd9Sstevel@tonic-gate 	 */
9407c478bd9Sstevel@tonic-gate 	empty = (list_head(&ctp->conp_members) == NULL);
9417c478bd9Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_EXIT)) {
9427c478bd9Sstevel@tonic-gate 		nvlist_t *nvl;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
9457c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
9467c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
9477c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_int32(nvl, CTPE_EXITSTATUS, exitstatus) == 0);
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
9507c478bd9Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_EXIT) ? CTE_INFO : 0;
9517c478bd9Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_EXIT;
95225e8c5aaSvikram 		(void) cte_publish_all(ct, event, nvl, NULL);
9537c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate 	if (empty) {
9567c478bd9Sstevel@tonic-gate 		/*
9577c478bd9Sstevel@tonic-gate 		 * Send EMPTY message.
9587c478bd9Sstevel@tonic-gate 		 */
9597c478bd9Sstevel@tonic-gate 		if (EVSENDP(ctp, CT_PR_EV_EMPTY)) {
9607c478bd9Sstevel@tonic-gate 			nvlist_t *nvl;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 			mutex_exit(&ct->ct_lock);
9637c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME,
9647c478bd9Sstevel@tonic-gate 			    KM_SLEEP) == 0);
9657c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 			event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
9687c478bd9Sstevel@tonic-gate 			event->cte_flags = EVINFOP(ctp, CT_PR_EV_EMPTY) ?
9697c478bd9Sstevel@tonic-gate 			    CTE_INFO : 0;
9707c478bd9Sstevel@tonic-gate 			event->cte_type = CT_PR_EV_EMPTY;
97125e8c5aaSvikram 			(void) cte_publish_all(ct, event, nvl, NULL);
9727c478bd9Sstevel@tonic-gate 			mutex_enter(&ct->ct_lock);
9737c478bd9Sstevel@tonic-gate 		}
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 		/*
9767c478bd9Sstevel@tonic-gate 		 * The last one to leave an orphaned contract turns out
9777c478bd9Sstevel@tonic-gate 		 * the lights.
9787c478bd9Sstevel@tonic-gate 		 */
9797c478bd9Sstevel@tonic-gate 		if (ct->ct_state == CTS_ORPHAN) {
9807c478bd9Sstevel@tonic-gate 			contract_destroy(ct);
9817c478bd9Sstevel@tonic-gate 			return;
9827c478bd9Sstevel@tonic-gate 		}
9837c478bd9Sstevel@tonic-gate 	}
9847c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
9857c478bd9Sstevel@tonic-gate 	contract_rele(ct);
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate /*
9897c478bd9Sstevel@tonic-gate  * contract_process_fork
9907c478bd9Sstevel@tonic-gate  *
9917c478bd9Sstevel@tonic-gate  * Called on process fork.  If the current lwp has a active process
9927c478bd9Sstevel@tonic-gate  * contract template, we attempt to create a new process contract.
9937c478bd9Sstevel@tonic-gate  * Failure to create a process contract when required is a failure in
9947c478bd9Sstevel@tonic-gate  * fork so, in such an event, we return NULL.
9957c478bd9Sstevel@tonic-gate  *
9967c478bd9Sstevel@tonic-gate  * Assuming we succeeded or skipped the previous step, we add the child
9977c478bd9Sstevel@tonic-gate  * process to the new contract (success) or to the parent's process
9987c478bd9Sstevel@tonic-gate  * contract (skip).  If requested, we also send a fork event to that
9997c478bd9Sstevel@tonic-gate  * contract.
10007c478bd9Sstevel@tonic-gate  *
10017c478bd9Sstevel@tonic-gate  * Because contract_process_fork() may fail, and because we would
10027c478bd9Sstevel@tonic-gate  * prefer that process contracts not be created for processes which
10037c478bd9Sstevel@tonic-gate  * don't complete forking, this should be the last function called
10047c478bd9Sstevel@tonic-gate  * before the "all clear" point in cfork.
10057c478bd9Sstevel@tonic-gate  */
10067c478bd9Sstevel@tonic-gate cont_process_t *
contract_process_fork(ctmpl_process_t * rtmpl,proc_t * cp,proc_t * pp,int canfail)10077c478bd9Sstevel@tonic-gate contract_process_fork(ctmpl_process_t *rtmpl, proc_t *cp, proc_t *pp,
10087c478bd9Sstevel@tonic-gate     int canfail)
10097c478bd9Sstevel@tonic-gate {
10107c478bd9Sstevel@tonic-gate 	contract_t *ct;
10117c478bd9Sstevel@tonic-gate 	cont_process_t *ctp;
10127c478bd9Sstevel@tonic-gate 	ct_kevent_t *event;
10137c478bd9Sstevel@tonic-gate 	ct_template_t *tmpl;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	if (rtmpl == NULL && (tmpl = ttolwp(curthread)->lwp_ct_active[
10167c478bd9Sstevel@tonic-gate 	    process_type->ct_type_index]) != NULL)
10177c478bd9Sstevel@tonic-gate 		rtmpl = tmpl->ctmpl_data;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	if (rtmpl == NULL)
10207c478bd9Sstevel@tonic-gate 		ctp = curproc->p_ct_process;
10217c478bd9Sstevel@tonic-gate 	else if ((ctp = contract_process_create(rtmpl, pp, canfail)) == NULL)
10227c478bd9Sstevel@tonic-gate 		return (NULL);
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	ct = &ctp->conp_contract;
10257c478bd9Sstevel@tonic-gate 	/*
10267c478bd9Sstevel@tonic-gate 	 * Prevent contract_process_kill() from missing forked children
10277c478bd9Sstevel@tonic-gate 	 * by failing forks by parents that have just been killed.
10287c478bd9Sstevel@tonic-gate 	 * It's not worth hoisting the ctp test since contract creation
10297c478bd9Sstevel@tonic-gate 	 * is by no means the common case.
10307c478bd9Sstevel@tonic-gate 	 */
10317c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
10327c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
10337c478bd9Sstevel@tonic-gate 	if (ctp == curproc->p_ct_process && (pp->p_flag & SKILLED) != 0 &&
10347c478bd9Sstevel@tonic-gate 	    canfail) {
10357c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
10367c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
10377c478bd9Sstevel@tonic-gate 		return (NULL);
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate 	cp->p_ct_process = ctp;
10407c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
10417c478bd9Sstevel@tonic-gate 	contract_hold(ct);
10427c478bd9Sstevel@tonic-gate 	list_insert_head(&ctp->conp_members, cp);
10437c478bd9Sstevel@tonic-gate 	ctp->conp_nmembers++;
10447c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
10457c478bd9Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_FORK)) {
10467c478bd9Sstevel@tonic-gate 		nvlist_t *nvl;
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
10497c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, cp->p_pid) == 0);
10507c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PPID, pp->p_pid) == 0);
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
10537c478bd9Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_FORK) ? CTE_INFO : 0;
10547c478bd9Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_FORK;
105525e8c5aaSvikram 		(void) cte_publish_all(ct, event, nvl, NULL);
10567c478bd9Sstevel@tonic-gate 	}
10577c478bd9Sstevel@tonic-gate 	return (ctp);
10587c478bd9Sstevel@tonic-gate }
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate /*
10617c478bd9Sstevel@tonic-gate  * contract_process_core
10627c478bd9Sstevel@tonic-gate  *
10637c478bd9Sstevel@tonic-gate  * Called on core file generation attempts.  Generates a core event, if
10647c478bd9Sstevel@tonic-gate  * requested, containing the names of the process, global, and
10657c478bd9Sstevel@tonic-gate  * system-global ("zone") core files.  If dumping core is in the fatal
10667c478bd9Sstevel@tonic-gate  * event set, calls contract_process_kill().
10677c478bd9Sstevel@tonic-gate  */
10687c478bd9Sstevel@tonic-gate void
contract_process_core(cont_process_t * ctp,proc_t * p,int sig,const char * process,const char * global,const char * zone)10697c478bd9Sstevel@tonic-gate contract_process_core(cont_process_t *ctp, proc_t *p, int sig,
10707c478bd9Sstevel@tonic-gate     const char *process, const char *global, const char *zone)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_CORE)) {
10757c478bd9Sstevel@tonic-gate 		ct_kevent_t *event;
10767c478bd9Sstevel@tonic-gate 		nvlist_t *nvl, *gnvl = NULL;
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
10797c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
10807c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_SIGNAL, sig) == 0);
10817c478bd9Sstevel@tonic-gate 		if (process)
10827c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_string(nvl, CTPE_PCOREFILE,
10837c478bd9Sstevel@tonic-gate 			    (char *)process) == 0);
10847c478bd9Sstevel@tonic-gate 		if (global)
10857c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_string(nvl, CTPE_GCOREFILE,
10867c478bd9Sstevel@tonic-gate 			    (char *)global) == 0);
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 		if (zone) {
10897c478bd9Sstevel@tonic-gate 			/*
10907c478bd9Sstevel@tonic-gate 			 * Only the global zone is informed of the
10917c478bd9Sstevel@tonic-gate 			 * local-zone generated global-zone core.
10927c478bd9Sstevel@tonic-gate 			 */
10937c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_alloc(&gnvl, NV_UNIQUE_NAME,
10947c478bd9Sstevel@tonic-gate 			    KM_SLEEP) == 0);
10957c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_string(gnvl, CTPE_ZCOREFILE,
10967c478bd9Sstevel@tonic-gate 			    (char *)zone) == 0);
10977c478bd9Sstevel@tonic-gate 		}
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
11007c478bd9Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_CORE) ? CTE_INFO : 0;
11017c478bd9Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_CORE;
110225e8c5aaSvikram 		(void) cte_publish_all(ct, event, nvl, gnvl);
11037c478bd9Sstevel@tonic-gate 	}
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	if (EVFATALP(ctp, CT_PR_EV_CORE)) {
11067c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
11077c478bd9Sstevel@tonic-gate 		contract_process_kill(ct, p, B_TRUE);
11087c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate }
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate /*
11137c478bd9Sstevel@tonic-gate  * contract_process_hwerr
11147c478bd9Sstevel@tonic-gate  *
11157c478bd9Sstevel@tonic-gate  * Called when a process is killed by an unrecoverable hardware error.
11167c478bd9Sstevel@tonic-gate  * Generates an hwerr event, if requested.  If hardware errors are in
11177c478bd9Sstevel@tonic-gate  * the fatal event set, calls contract_process_kill().
11187c478bd9Sstevel@tonic-gate  */
11197c478bd9Sstevel@tonic-gate void
contract_process_hwerr(cont_process_t * ctp,proc_t * p)11207c478bd9Sstevel@tonic-gate contract_process_hwerr(cont_process_t *ctp, proc_t *p)
11217c478bd9Sstevel@tonic-gate {
11227c478bd9Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_HWERR)) {
11257c478bd9Sstevel@tonic-gate 		ct_kevent_t *event;
11267c478bd9Sstevel@tonic-gate 		nvlist_t *nvl;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
11297c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
11327c478bd9Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_HWERR) ? CTE_INFO : 0;
11337c478bd9Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_HWERR;
113425e8c5aaSvikram 		(void) cte_publish_all(ct, event, nvl, NULL);
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	if (EVFATALP(ctp, CT_PR_EV_HWERR)) {
11387c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
11397c478bd9Sstevel@tonic-gate 		contract_process_kill(ct, p, B_FALSE);
11407c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
11417c478bd9Sstevel@tonic-gate 	}
11427c478bd9Sstevel@tonic-gate }
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate /*
11457c478bd9Sstevel@tonic-gate  * contract_process_sig
11467c478bd9Sstevel@tonic-gate  *
11477c478bd9Sstevel@tonic-gate  * Called when a process is killed by a signal originating from a
11487c478bd9Sstevel@tonic-gate  * process outside of its process contract or its process contract's
11497c478bd9Sstevel@tonic-gate  * holder.  Generates an signal event, if requested, containing the
11507c478bd9Sstevel@tonic-gate  * signal number, and the sender's pid and contract id (if available).
11517c478bd9Sstevel@tonic-gate  * If signals are in the fatal event set, calls
11527c478bd9Sstevel@tonic-gate  * contract_process_kill().
11537c478bd9Sstevel@tonic-gate  */
11547c478bd9Sstevel@tonic-gate void
contract_process_sig(cont_process_t * ctp,proc_t * p,int sig,pid_t pid,ctid_t ctid,zoneid_t zoneid)11557c478bd9Sstevel@tonic-gate contract_process_sig(cont_process_t *ctp, proc_t *p, int sig, pid_t pid,
11567c478bd9Sstevel@tonic-gate     ctid_t ctid, zoneid_t zoneid)
11577c478bd9Sstevel@tonic-gate {
11587c478bd9Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_SIGNAL)) {
11617c478bd9Sstevel@tonic-gate 		ct_kevent_t *event;
11627c478bd9Sstevel@tonic-gate 		nvlist_t *dest, *nvl, *gnvl = NULL;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
11657c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
11667c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_SIGNAL, sig) == 0);
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 		if (zoneid >= 0 && p->p_zone->zone_id != zoneid) {
11697c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_alloc(&gnvl, NV_UNIQUE_NAME,
11707c478bd9Sstevel@tonic-gate 			    KM_SLEEP) == 0);
11717c478bd9Sstevel@tonic-gate 			dest = gnvl;
11727c478bd9Sstevel@tonic-gate 		} else {
11737c478bd9Sstevel@tonic-gate 			dest = nvl;
11747c478bd9Sstevel@tonic-gate 		}
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 		if (pid != -1)
11777c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_uint32(dest, CTPE_SENDER, pid) == 0);
11787c478bd9Sstevel@tonic-gate 		if (ctid != 0)
11797c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_uint32(dest, CTPE_SENDCT, ctid) == 0);
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
11827c478bd9Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_SIGNAL) ? CTE_INFO : 0;
11837c478bd9Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_SIGNAL;
118425e8c5aaSvikram 		(void) cte_publish_all(ct, event, nvl, gnvl);
11857c478bd9Sstevel@tonic-gate 	}
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	if (EVFATALP(ctp, CT_PR_EV_SIGNAL)) {
11887c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
11897c478bd9Sstevel@tonic-gate 		contract_process_kill(ct, p, B_TRUE);
11907c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
11917c478bd9Sstevel@tonic-gate 	}
11927c478bd9Sstevel@tonic-gate }
1193