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