xref: /titanic_50/usr/src/uts/common/contract/process.c (revision 7b209c2cc5ea45251aba06dcc6181d3f23da807a)
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 /*
22*7b209c2cSacruz  * Copyright 2008 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>
46*7b209c2cSacruz #include <sys/refstr.h>
47*7b209c2cSacruz #include <sys/sunddi.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Process Contracts
517c478bd9Sstevel@tonic-gate  * -----------------
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * Generally speaking, a process contract is a contract between a
547c478bd9Sstevel@tonic-gate  * process and a set of its descendent processes.  In some cases, when
557c478bd9Sstevel@tonic-gate  * the child processes outlive the author of the contract, the contract
567c478bd9Sstevel@tonic-gate  * may be held by (and therefore be between the child processes and) a
577c478bd9Sstevel@tonic-gate  * successor process which adopts the contract after the death of the
587c478bd9Sstevel@tonic-gate  * original author.
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  * The process contract adds two new concepts to the Solaris process
617c478bd9Sstevel@tonic-gate  * model.  The first is that a process contract forms a rigid fault
627c478bd9Sstevel@tonic-gate  * boundary around a set of processes.  Hardware, software, and even
637c478bd9Sstevel@tonic-gate  * administrator errors impacting a process in a process contract
647c478bd9Sstevel@tonic-gate  * generate specific events and can be requested to atomically shutdown
657c478bd9Sstevel@tonic-gate  * all processes in the contract.  The second is that a process
667c478bd9Sstevel@tonic-gate  * contract is a process collective whose leader is not a member of the
677c478bd9Sstevel@tonic-gate  * collective.  This means that the leader can reliably react to events
687c478bd9Sstevel@tonic-gate  * in the collective, and may also act upon the collective without
697c478bd9Sstevel@tonic-gate  * special casing itself.
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  * A composite outcome of these two concepts is that we can now create
727c478bd9Sstevel@tonic-gate  * a tree of process contracts, rooted at init(1M), which represent
737c478bd9Sstevel@tonic-gate  * services and subservices that are reliably observed and can be
747c478bd9Sstevel@tonic-gate  * restarted when fatal errors occur.  The service management framework
757c478bd9Sstevel@tonic-gate  * (SMF) realizes this structure.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * For more details, see the "restart agreements" case, PSARC 2003/193.
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  * There are four sets of routines in this file: the process contract
807c478bd9Sstevel@tonic-gate  * standard template operations, the process contract standard contract
817c478bd9Sstevel@tonic-gate  * operations, a couple routines used only by the contract subsystem to
827c478bd9Sstevel@tonic-gate  * handle process contracts' unique role as a temporary holder of
837c478bd9Sstevel@tonic-gate  * abandoned contracts, and the interfaces which allow the system to
847c478bd9Sstevel@tonic-gate  * create and act upon process contracts.  The first two are defined by
857c478bd9Sstevel@tonic-gate  * the contracts framework and won't be discussed further.  As for the
867c478bd9Sstevel@tonic-gate  * remaining two:
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * Special framework interfaces
897c478bd9Sstevel@tonic-gate  * ----------------------------
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  * contract_process_accept - determines if a process contract is a
927c478bd9Sstevel@tonic-gate  *   regent, i.e. if it can inherit other contracts.
937c478bd9Sstevel@tonic-gate  *
947c478bd9Sstevel@tonic-gate  * contract_process_take - tells a regent process contract to inherit
957c478bd9Sstevel@tonic-gate  *   an abandoned contract
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  * contract_process_adopt - tells a regent process contract that a
987c478bd9Sstevel@tonic-gate  *   contract it has inherited is being adopted by a process.
997c478bd9Sstevel@tonic-gate  *
1007c478bd9Sstevel@tonic-gate  * Process contract interfaces
1017c478bd9Sstevel@tonic-gate  * ---------------------------
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * contract_process_fork - called when a process is created; adds the
1047c478bd9Sstevel@tonic-gate  *   new process to an existing contract or to a newly created one.
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  * contract_process_exit - called when a process exits
1077c478bd9Sstevel@tonic-gate  *
1087c478bd9Sstevel@tonic-gate  * contract_process_core - called when a process would have dumped core
1097c478bd9Sstevel@tonic-gate  *   (even if a core file wasn't generated)
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  * contract_process_hwerr - called when a process was killed because of
1127c478bd9Sstevel@tonic-gate  *   an uncorrectable hardware error
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  * contract_process_sig - called when a process was killed by a fatal
1157c478bd9Sstevel@tonic-gate  *   signal sent by a process in another process contract
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate ct_type_t *process_type;
1207c478bd9Sstevel@tonic-gate ctmpl_process_t *sys_process_tmpl;
121*7b209c2cSacruz refstr_t *conp_svc_aux_default;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * Macro predicates for determining when events should be sent and how.
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate #define	EVSENDP(ctp, flag) \
1277c478bd9Sstevel@tonic-gate 	((ctp->conp_contract.ct_ev_info | ctp->conp_contract.ct_ev_crit) & flag)
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate #define	EVINFOP(ctp, flag) \
1307c478bd9Sstevel@tonic-gate 	((ctp->conp_contract.ct_ev_crit & flag) == 0)
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #define	EVFATALP(ctp, flag) \
1337c478bd9Sstevel@tonic-gate 	(ctp->conp_ev_fatal & flag)
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * Process contract template implementation
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * ctmpl_process_dup
1427c478bd9Sstevel@tonic-gate  *
1437c478bd9Sstevel@tonic-gate  * The process contract template dup entry point.  Other than the
1447c478bd9Sstevel@tonic-gate  * to-be-subsumed contract, which must be held, this simply copies all
1457c478bd9Sstevel@tonic-gate  * the fields of the original.
1467c478bd9Sstevel@tonic-gate  */
1477c478bd9Sstevel@tonic-gate static struct ct_template *
1487c478bd9Sstevel@tonic-gate ctmpl_process_dup(struct ct_template *template)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	ctmpl_process_t *new;
1517c478bd9Sstevel@tonic-gate 	ctmpl_process_t *old = template->ctmpl_data;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	new = kmem_alloc(sizeof (ctmpl_process_t), KM_SLEEP);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	ctmpl_copy(&new->ctp_ctmpl, template);
1567c478bd9Sstevel@tonic-gate 	new->ctp_ctmpl.ctmpl_data = new;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	new->ctp_subsume = old->ctp_subsume;
1597c478bd9Sstevel@tonic-gate 	if (new->ctp_subsume)
1607c478bd9Sstevel@tonic-gate 		contract_hold(new->ctp_subsume);
1617c478bd9Sstevel@tonic-gate 	new->ctp_params = old->ctp_params;
1627c478bd9Sstevel@tonic-gate 	new->ctp_ev_fatal = old->ctp_ev_fatal;
163*7b209c2cSacruz 	new->ctp_svc_fmri = old->ctp_svc_fmri;
164*7b209c2cSacruz 	if (new->ctp_svc_fmri != NULL) {
165*7b209c2cSacruz 		refstr_hold(new->ctp_svc_fmri);
166*7b209c2cSacruz 	}
167*7b209c2cSacruz 	new->ctp_svc_aux = old->ctp_svc_aux;
168*7b209c2cSacruz 	if (new->ctp_svc_aux != NULL) {
169*7b209c2cSacruz 		refstr_hold(new->ctp_svc_aux);
170*7b209c2cSacruz 	}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	return (&new->ctp_ctmpl);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
176*7b209c2cSacruz  * ctmpl_process_free
1777c478bd9Sstevel@tonic-gate  *
1787c478bd9Sstevel@tonic-gate  * The process contract template free entry point.  Just releases a
1797c478bd9Sstevel@tonic-gate  * to-be-subsumed contract and frees the template.
1807c478bd9Sstevel@tonic-gate  */
1817c478bd9Sstevel@tonic-gate static void
1827c478bd9Sstevel@tonic-gate ctmpl_process_free(struct ct_template *template)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	ctmpl_process_t *ctp = template->ctmpl_data;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (ctp->ctp_subsume)
1877c478bd9Sstevel@tonic-gate 		contract_rele(ctp->ctp_subsume);
188*7b209c2cSacruz 	if (ctp->ctp_svc_fmri != NULL) {
189*7b209c2cSacruz 		refstr_rele(ctp->ctp_svc_fmri);
190*7b209c2cSacruz 	}
191*7b209c2cSacruz 	if (ctp->ctp_svc_aux != NULL) {
192*7b209c2cSacruz 		refstr_rele(ctp->ctp_svc_aux);
193*7b209c2cSacruz 	}
1947c478bd9Sstevel@tonic-gate 	kmem_free(template, sizeof (ctmpl_process_t));
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * SAFE_EV is the set of events which a non-privileged process is
1997c478bd9Sstevel@tonic-gate  * allowed to make critical but not fatal or if the PGRPONLY parameter
2007c478bd9Sstevel@tonic-gate  * is set.  EXCESS tells us if "value", a critical event set, requires
2017c478bd9Sstevel@tonic-gate  * additional privilege given the template "ctp".
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate #define	SAFE_EV			(CT_PR_EV_EMPTY)
2047c478bd9Sstevel@tonic-gate #define	EXCESS(ctp, value)	\
2057c478bd9Sstevel@tonic-gate 	(((value) & ~((ctp)->ctp_ev_fatal | SAFE_EV)) || \
2067c478bd9Sstevel@tonic-gate 	(((value) & ~SAFE_EV) && (ctp->ctp_params & CT_PR_PGRPONLY)))
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * ctmpl_process_set
2107c478bd9Sstevel@tonic-gate  *
2117c478bd9Sstevel@tonic-gate  * The process contract template set entry point.  None of the terms
2127c478bd9Sstevel@tonic-gate  * may be unconditionally set, and setting the parameters or fatal
2137c478bd9Sstevel@tonic-gate  * event set may result in events being implicitly removed from to the
2147c478bd9Sstevel@tonic-gate  * critical event set and added to the informative event set.  The
2157c478bd9Sstevel@tonic-gate  * (admittedly subtle) reason we implicitly change the critical event
2167c478bd9Sstevel@tonic-gate  * set when the parameter or fatal event set is modified but not the
2177c478bd9Sstevel@tonic-gate  * other way around is because a change to the critical event set only
2187c478bd9Sstevel@tonic-gate  * affects the contract's owner, whereas a change to the parameter set
2197c478bd9Sstevel@tonic-gate  * and fatal set can affect the execution of the application running in
2207c478bd9Sstevel@tonic-gate  * the contract (and should therefore be only made explicitly).  We
2217c478bd9Sstevel@tonic-gate  * allow implicit changes at all so that setting contract terms doesn't
2227c478bd9Sstevel@tonic-gate  * become a complex dance dependent on the template's initial state and
2237c478bd9Sstevel@tonic-gate  * the desired terms.
2247c478bd9Sstevel@tonic-gate  */
2257c478bd9Sstevel@tonic-gate static int
2267c478bd9Sstevel@tonic-gate ctmpl_process_set(struct ct_template *tmpl, ct_param_t *param, const cred_t *cr)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	ctmpl_process_t *ctp = tmpl->ctmpl_data;
2297c478bd9Sstevel@tonic-gate 	contract_t *ct;
2307c478bd9Sstevel@tonic-gate 	int error;
231*7b209c2cSacruz 	uint64_t param_value;
232*7b209c2cSacruz 	char *str_value;
2337c478bd9Sstevel@tonic-gate 
234*7b209c2cSacruz 	if ((param->ctpm_id == CTPP_SVC_FMRI) ||
235*7b209c2cSacruz 	    (param->ctpm_id == CTPP_CREATOR_AUX)) {
236*7b209c2cSacruz 		str_value = (char *)param->ctpm_value;
237*7b209c2cSacruz 		str_value[param->ctpm_size - 1] = '\0';
238*7b209c2cSacruz 	} else {
239*7b209c2cSacruz 		param_value = *(uint64_t *)param->ctpm_value;
2407c478bd9Sstevel@tonic-gate 		/*
2417c478bd9Sstevel@tonic-gate 		 * No process contract parameters are > 32 bits.
242*7b209c2cSacruz 		 * Unless it is a string.
2437c478bd9Sstevel@tonic-gate 		 */
244*7b209c2cSacruz 		if (param_value & ~UINT32_MAX)
2457c478bd9Sstevel@tonic-gate 			return (EINVAL);
246*7b209c2cSacruz 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	switch (param->ctpm_id) {
2497c478bd9Sstevel@tonic-gate 	case CTPP_SUBSUME:
250*7b209c2cSacruz 		if (param_value != 0) {
2517c478bd9Sstevel@tonic-gate 			/*
2527c478bd9Sstevel@tonic-gate 			 * Ensure that the contract exists, that we
2537c478bd9Sstevel@tonic-gate 			 * hold the contract, and that the contract is
2547c478bd9Sstevel@tonic-gate 			 * empty.
2557c478bd9Sstevel@tonic-gate 			 */
256*7b209c2cSacruz 			ct = contract_type_ptr(process_type, param_value,
2577c478bd9Sstevel@tonic-gate 			    curproc->p_zone->zone_uniqid);
2587c478bd9Sstevel@tonic-gate 			if (ct == NULL)
2597c478bd9Sstevel@tonic-gate 				return (ESRCH);
2607c478bd9Sstevel@tonic-gate 			if (ct->ct_owner != curproc) {
2617c478bd9Sstevel@tonic-gate 				contract_rele(ct);
2627c478bd9Sstevel@tonic-gate 				return (EACCES);
2637c478bd9Sstevel@tonic-gate 			}
2647c478bd9Sstevel@tonic-gate 			if (((cont_process_t *)ct->ct_data)->conp_nmembers) {
2657c478bd9Sstevel@tonic-gate 				contract_rele(ct);
2667c478bd9Sstevel@tonic-gate 				return (ENOTEMPTY);
2677c478bd9Sstevel@tonic-gate 			}
2687c478bd9Sstevel@tonic-gate 		} else {
2697c478bd9Sstevel@tonic-gate 			ct = NULL;
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 		if (ctp->ctp_subsume)
2727c478bd9Sstevel@tonic-gate 			contract_rele(ctp->ctp_subsume);
2737c478bd9Sstevel@tonic-gate 		ctp->ctp_subsume = ct;
2747c478bd9Sstevel@tonic-gate 		break;
2757c478bd9Sstevel@tonic-gate 	case CTPP_PARAMS:
276*7b209c2cSacruz 		if (param_value & ~CT_PR_ALLPARAM)
2777c478bd9Sstevel@tonic-gate 			return (EINVAL);
278*7b209c2cSacruz 		ctp->ctp_params = param_value;
2797c478bd9Sstevel@tonic-gate 		/*
2807c478bd9Sstevel@tonic-gate 		 * If an unprivileged process requests that
2817c478bd9Sstevel@tonic-gate 		 * CT_PR_PGRPONLY be set, remove any unsafe events from
2827c478bd9Sstevel@tonic-gate 		 * the critical event set and add them to the
2837c478bd9Sstevel@tonic-gate 		 * informative event set.
2847c478bd9Sstevel@tonic-gate 		 */
2857c478bd9Sstevel@tonic-gate 		if ((ctp->ctp_params & CT_PR_PGRPONLY) &&
2867c478bd9Sstevel@tonic-gate 		    EXCESS(ctp, tmpl->ctmpl_ev_crit) &&
2877c478bd9Sstevel@tonic-gate 		    !secpolicy_contract_event_choice(cr)) {
2887c478bd9Sstevel@tonic-gate 			tmpl->ctmpl_ev_info |= (tmpl->ctmpl_ev_crit & ~SAFE_EV);
2897c478bd9Sstevel@tonic-gate 			tmpl->ctmpl_ev_crit &= SAFE_EV;
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		break;
293*7b209c2cSacruz 	case CTPP_SVC_FMRI:
294*7b209c2cSacruz 		if (error = secpolicy_contract_identity(cr))
295*7b209c2cSacruz 			return (error);
296*7b209c2cSacruz 		if (ctp->ctp_svc_fmri != NULL)
297*7b209c2cSacruz 			refstr_rele(ctp->ctp_svc_fmri);
298*7b209c2cSacruz 		if (strcmp(CT_PR_SVC_DEFAULT, str_value) == 0)
299*7b209c2cSacruz 			ctp->ctp_svc_fmri = NULL;
300*7b209c2cSacruz 		else
301*7b209c2cSacruz 			ctp->ctp_svc_fmri =
302*7b209c2cSacruz 			    refstr_alloc(str_value);
303*7b209c2cSacruz 		break;
304*7b209c2cSacruz 	case CTPP_CREATOR_AUX:
305*7b209c2cSacruz 		if (ctp->ctp_svc_aux != NULL)
306*7b209c2cSacruz 			refstr_rele(ctp->ctp_svc_aux);
307*7b209c2cSacruz 		if (param->ctpm_size == 1) /* empty string */
308*7b209c2cSacruz 			ctp->ctp_svc_aux = NULL;
309*7b209c2cSacruz 		else
310*7b209c2cSacruz 			ctp->ctp_svc_aux =
311*7b209c2cSacruz 			    refstr_alloc(str_value);
312*7b209c2cSacruz 		break;
3137c478bd9Sstevel@tonic-gate 	case CTP_EV_CRITICAL:
3147c478bd9Sstevel@tonic-gate 		/*
3157c478bd9Sstevel@tonic-gate 		 * We simply don't allow adding events to the critical
3167c478bd9Sstevel@tonic-gate 		 * event set which aren't permitted by our policy or by
3177c478bd9Sstevel@tonic-gate 		 * privilege.
3187c478bd9Sstevel@tonic-gate 		 */
319*7b209c2cSacruz 		if (EXCESS(ctp, param_value) &&
3207c478bd9Sstevel@tonic-gate 		    (error = secpolicy_contract_event(cr)) != 0)
3217c478bd9Sstevel@tonic-gate 			return (error);
322*7b209c2cSacruz 		tmpl->ctmpl_ev_crit = param_value;
3237c478bd9Sstevel@tonic-gate 		break;
3247c478bd9Sstevel@tonic-gate 	case CTPP_EV_FATAL:
325*7b209c2cSacruz 		if (param_value & ~CT_PR_ALLFATAL)
3267c478bd9Sstevel@tonic-gate 			return (EINVAL);
327*7b209c2cSacruz 		ctp->ctp_ev_fatal = param_value;
3287c478bd9Sstevel@tonic-gate 		/*
3297c478bd9Sstevel@tonic-gate 		 * Check to see if an unprivileged process is
3307c478bd9Sstevel@tonic-gate 		 * requesting that events be removed from the fatal
3317c478bd9Sstevel@tonic-gate 		 * event set which are still in the critical event set.
3327c478bd9Sstevel@tonic-gate 		 */
3337c478bd9Sstevel@tonic-gate 		if (EXCESS(ctp, tmpl->ctmpl_ev_crit) &&
3347c478bd9Sstevel@tonic-gate 		    !secpolicy_contract_event_choice(cr)) {
3357c478bd9Sstevel@tonic-gate 			int allowed =
3367c478bd9Sstevel@tonic-gate 			    SAFE_EV | (ctp->ctp_params & CT_PR_PGRPONLY) ?
3377c478bd9Sstevel@tonic-gate 			    0 : ctp->ctp_ev_fatal;
3387c478bd9Sstevel@tonic-gate 			tmpl->ctmpl_ev_info |= (tmpl->ctmpl_ev_crit & ~allowed);
3397c478bd9Sstevel@tonic-gate 			tmpl->ctmpl_ev_crit &= allowed;
3407c478bd9Sstevel@tonic-gate 		}
3417c478bd9Sstevel@tonic-gate 		break;
3427c478bd9Sstevel@tonic-gate 	default:
3437c478bd9Sstevel@tonic-gate 		return (EINVAL);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	return (0);
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /*
3507c478bd9Sstevel@tonic-gate  * ctmpl_process_get
3517c478bd9Sstevel@tonic-gate  *
3527c478bd9Sstevel@tonic-gate  * The process contract template get entry point.  Simply fetches and
3537c478bd9Sstevel@tonic-gate  * returns the requested term.
3547c478bd9Sstevel@tonic-gate  */
3557c478bd9Sstevel@tonic-gate static int
3567c478bd9Sstevel@tonic-gate ctmpl_process_get(struct ct_template *template, ct_param_t *param)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	ctmpl_process_t *ctp = template->ctmpl_data;
359*7b209c2cSacruz 	uint64_t *param_value = param->ctpm_value;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	switch (param->ctpm_id) {
3627c478bd9Sstevel@tonic-gate 	case CTPP_SUBSUME:
363*7b209c2cSacruz 		*param_value = ctp->ctp_subsume ?
3647c478bd9Sstevel@tonic-gate 		    ctp->ctp_subsume->ct_id : 0;
3657c478bd9Sstevel@tonic-gate 		break;
3667c478bd9Sstevel@tonic-gate 	case CTPP_PARAMS:
367*7b209c2cSacruz 		*param_value = ctp->ctp_params;
368*7b209c2cSacruz 		break;
369*7b209c2cSacruz 	case CTPP_SVC_FMRI:
370*7b209c2cSacruz 		if (ctp->ctp_svc_fmri == NULL) {
371*7b209c2cSacruz 			param->ctpm_size =
372*7b209c2cSacruz 			    strlcpy((char *)param->ctpm_value,
373*7b209c2cSacruz 			    CT_PR_SVC_DEFAULT, param->ctpm_size);
374*7b209c2cSacruz 		} else {
375*7b209c2cSacruz 			param->ctpm_size =
376*7b209c2cSacruz 			    strlcpy((char *)param->ctpm_value,
377*7b209c2cSacruz 			    refstr_value(ctp->ctp_svc_fmri), param->ctpm_size);
378*7b209c2cSacruz 		}
379*7b209c2cSacruz 		param->ctpm_size++;
380*7b209c2cSacruz 		break;
381*7b209c2cSacruz 	case CTPP_CREATOR_AUX:
382*7b209c2cSacruz 		if (ctp->ctp_svc_aux == NULL) {
383*7b209c2cSacruz 			param->ctpm_size =
384*7b209c2cSacruz 			    strlcpy((char *)param->ctpm_value,
385*7b209c2cSacruz 			    refstr_value(conp_svc_aux_default),
386*7b209c2cSacruz 			    param->ctpm_size);
387*7b209c2cSacruz 		} else {
388*7b209c2cSacruz 			param->ctpm_size =
389*7b209c2cSacruz 			    strlcpy((char *)param->ctpm_value,
390*7b209c2cSacruz 			    refstr_value(ctp->ctp_svc_aux), param->ctpm_size);
391*7b209c2cSacruz 		}
392*7b209c2cSacruz 		param->ctpm_size++;
3937c478bd9Sstevel@tonic-gate 		break;
3947c478bd9Sstevel@tonic-gate 	case CTPP_EV_FATAL:
395*7b209c2cSacruz 		*param_value = ctp->ctp_ev_fatal;
3967c478bd9Sstevel@tonic-gate 		break;
3977c478bd9Sstevel@tonic-gate 	default:
3987c478bd9Sstevel@tonic-gate 		return (EINVAL);
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	return (0);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate static ctmplops_t ctmpl_process_ops = {
4057c478bd9Sstevel@tonic-gate 	ctmpl_process_dup,		/* ctop_dup */
4067c478bd9Sstevel@tonic-gate 	ctmpl_process_free,		/* ctop_free */
4077c478bd9Sstevel@tonic-gate 	ctmpl_process_set,		/* ctop_set */
4087c478bd9Sstevel@tonic-gate 	ctmpl_process_get,		/* ctop_get */
4097c478bd9Sstevel@tonic-gate 	ctmpl_create_inval,		/* ctop_create */
4107c478bd9Sstevel@tonic-gate 	CT_PR_ALLEVENT
4117c478bd9Sstevel@tonic-gate };
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate  * Process contract implementation
4167c478bd9Sstevel@tonic-gate  */
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate /*
4197c478bd9Sstevel@tonic-gate  * ctmpl_process_default
4207c478bd9Sstevel@tonic-gate  *
4217c478bd9Sstevel@tonic-gate  * The process contract default template entry point.  Creates a
4227c478bd9Sstevel@tonic-gate  * process contract template with no parameters set, with informative
4237c478bd9Sstevel@tonic-gate  * core and signal events, critical empty and hwerr events, and fatal
4247c478bd9Sstevel@tonic-gate  * hwerr events.
4257c478bd9Sstevel@tonic-gate  */
4267c478bd9Sstevel@tonic-gate static ct_template_t *
4277c478bd9Sstevel@tonic-gate contract_process_default(void)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 	ctmpl_process_t *new;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	new = kmem_alloc(sizeof (ctmpl_process_t), KM_SLEEP);
4327c478bd9Sstevel@tonic-gate 	ctmpl_init(&new->ctp_ctmpl, &ctmpl_process_ops, process_type, new);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	new->ctp_subsume = NULL;
4357c478bd9Sstevel@tonic-gate 	new->ctp_params = 0;
4367c478bd9Sstevel@tonic-gate 	new->ctp_ctmpl.ctmpl_ev_info = CT_PR_EV_CORE | CT_PR_EV_SIGNAL;
4377c478bd9Sstevel@tonic-gate 	new->ctp_ctmpl.ctmpl_ev_crit = CT_PR_EV_EMPTY | CT_PR_EV_HWERR;
4387c478bd9Sstevel@tonic-gate 	new->ctp_ev_fatal = CT_PR_EV_HWERR;
439*7b209c2cSacruz 	new->ctp_svc_fmri = NULL;
440*7b209c2cSacruz 	new->ctp_svc_aux = NULL;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	return (&new->ctp_ctmpl);
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate /*
4467c478bd9Sstevel@tonic-gate  * contract_process_free
4477c478bd9Sstevel@tonic-gate  *
4487c478bd9Sstevel@tonic-gate  * The process contract free entry point.
4497c478bd9Sstevel@tonic-gate  */
4507c478bd9Sstevel@tonic-gate static void
4517c478bd9Sstevel@tonic-gate contract_process_free(contract_t *ct)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
4547c478bd9Sstevel@tonic-gate 	crfree(ctp->conp_cred);
4557c478bd9Sstevel@tonic-gate 	list_destroy(&ctp->conp_members);
4567c478bd9Sstevel@tonic-gate 	list_destroy(&ctp->conp_inherited);
457*7b209c2cSacruz 	if (ctp->conp_svc_fmri != NULL) {
458*7b209c2cSacruz 		refstr_rele(ctp->conp_svc_fmri);
459*7b209c2cSacruz 	}
460*7b209c2cSacruz 	if (ctp->conp_svc_aux != NULL) {
461*7b209c2cSacruz 		refstr_rele(ctp->conp_svc_aux);
462*7b209c2cSacruz 	}
463*7b209c2cSacruz 	if (ctp->conp_svc_creator != NULL) {
464*7b209c2cSacruz 		refstr_rele(ctp->conp_svc_creator);
465*7b209c2cSacruz 	}
4667c478bd9Sstevel@tonic-gate 	kmem_free(ctp, sizeof (cont_process_t));
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate /*
4707c478bd9Sstevel@tonic-gate  * contract_process_cankill
4717c478bd9Sstevel@tonic-gate  *
4727c478bd9Sstevel@tonic-gate  * Determine if the contract author had or if the process generating
4737c478bd9Sstevel@tonic-gate  * the event, sp, has adequate privileges to kill process tp.
4747c478bd9Sstevel@tonic-gate  */
4757c478bd9Sstevel@tonic-gate static int
4767c478bd9Sstevel@tonic-gate contract_process_cankill(proc_t *tp, proc_t *sp, cont_process_t *ctp)
4777c478bd9Sstevel@tonic-gate {
4787c478bd9Sstevel@tonic-gate 	int cankill;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	mutex_enter(&tp->p_crlock);
4817c478bd9Sstevel@tonic-gate 	cankill = hasprocperm(tp->p_cred, ctp->conp_cred);
4827c478bd9Sstevel@tonic-gate 	mutex_exit(&tp->p_crlock);
4837c478bd9Sstevel@tonic-gate 	if (cankill || (sp && prochasprocperm(tp, sp, CRED())))
4847c478bd9Sstevel@tonic-gate 		return (1);
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	return (0);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate  * contract_process_kill
4917c478bd9Sstevel@tonic-gate  *
4927c478bd9Sstevel@tonic-gate  * Kills all processes in a contract, or all processes in the
4937c478bd9Sstevel@tonic-gate  * intersection of a contract and ex's process group (if ex is non-NULL
4947c478bd9Sstevel@tonic-gate  * and the contract's PGRPONLY parameter is set).  If checkpriv is
4957c478bd9Sstevel@tonic-gate  * true, only those processes which may be signaled by the contract
4967c478bd9Sstevel@tonic-gate  * author or ex are killed.
4977c478bd9Sstevel@tonic-gate  */
4987c478bd9Sstevel@tonic-gate static void
4997c478bd9Sstevel@tonic-gate contract_process_kill(contract_t *ct, proc_t *ex, int checkpriv)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
5027c478bd9Sstevel@tonic-gate 	proc_t *p;
5037c478bd9Sstevel@tonic-gate 	pid_t pgrp = -1;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	if (ex && (ctp->conp_params & CT_PR_PGRPONLY)) {
5087c478bd9Sstevel@tonic-gate 		pgrp = ex->p_pgrp;
5097c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	for (p = list_head(&ctp->conp_members); p != NULL;
5137c478bd9Sstevel@tonic-gate 	    p = list_next(&ctp->conp_members, p)) {
5147c478bd9Sstevel@tonic-gate 		if ((p == ex) || (pgrp != -1 && p->p_pgrp != pgrp) ||
5157c478bd9Sstevel@tonic-gate 		    (checkpriv && !contract_process_cankill(p, ex, ctp)))
5167c478bd9Sstevel@tonic-gate 			continue;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 		psignal(p, SIGKILL);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if (pgrp != -1)
5227c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate /*
5277c478bd9Sstevel@tonic-gate  * contract_process_accept
5287c478bd9Sstevel@tonic-gate  *
5297c478bd9Sstevel@tonic-gate  * Tests if the process contract is willing to act as a regent for
5307c478bd9Sstevel@tonic-gate  * inherited contracts.  Though brief and only called from one place,
5317c478bd9Sstevel@tonic-gate  * this functionality is kept here to avoid including knowledge of
5327c478bd9Sstevel@tonic-gate  * process contract implementation in the generic contract code.
5337c478bd9Sstevel@tonic-gate  */
5347c478bd9Sstevel@tonic-gate int
5357c478bd9Sstevel@tonic-gate contract_process_accept(contract_t *parent)
5367c478bd9Sstevel@tonic-gate {
5377c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = parent->ct_data;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	ASSERT(parent->ct_type == process_type);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	return (ctp->conp_params & CT_PR_REGENT);
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate /*
5457c478bd9Sstevel@tonic-gate  * contract_process_take
5467c478bd9Sstevel@tonic-gate  *
5477c478bd9Sstevel@tonic-gate  * Executes the process contract side of inheriting a contract.
5487c478bd9Sstevel@tonic-gate  */
5497c478bd9Sstevel@tonic-gate void
5507c478bd9Sstevel@tonic-gate contract_process_take(contract_t *parent, contract_t *child)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = parent->ct_data;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&parent->ct_lock));
5557c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&child->ct_lock));
5567c478bd9Sstevel@tonic-gate 	ASSERT(parent->ct_type == process_type);
5577c478bd9Sstevel@tonic-gate 	ASSERT(ctp->conp_params & CT_PR_REGENT);
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	list_insert_head(&ctp->conp_inherited, child);
5607c478bd9Sstevel@tonic-gate 	ctp->conp_ninherited++;
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate /*
5647c478bd9Sstevel@tonic-gate  * contract_process_adopt
5657c478bd9Sstevel@tonic-gate  *
5667c478bd9Sstevel@tonic-gate  * Executes the process contract side of adopting a contract.
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate void
5697c478bd9Sstevel@tonic-gate contract_process_adopt(contract_t *ct, proc_t *p)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	cont_process_t *parent = p->p_ct_process;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&parent->conp_contract.ct_lock));
5747c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	list_remove(&parent->conp_inherited, ct);
5777c478bd9Sstevel@tonic-gate 	parent->conp_ninherited--;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	/*
5807c478bd9Sstevel@tonic-gate 	 * We drop the parent lock first because a) we are passing the
5817c478bd9Sstevel@tonic-gate 	 * contract reference to the child, and b) contract_adopt
5827c478bd9Sstevel@tonic-gate 	 * expects us to return with the contract lock held.
5837c478bd9Sstevel@tonic-gate 	 */
5847c478bd9Sstevel@tonic-gate 	mutex_exit(&parent->conp_contract.ct_lock);
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate /*
58825e8c5aaSvikram  * contract_process_abandon
5897c478bd9Sstevel@tonic-gate  *
5907c478bd9Sstevel@tonic-gate  * The process contract abandon entry point.
5917c478bd9Sstevel@tonic-gate  */
5927c478bd9Sstevel@tonic-gate static void
5937c478bd9Sstevel@tonic-gate contract_process_abandon(contract_t *ct)
5947c478bd9Sstevel@tonic-gate {
5957c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	/*
6007c478bd9Sstevel@tonic-gate 	 * Shall we stay or shall we go?
6017c478bd9Sstevel@tonic-gate 	 */
6027c478bd9Sstevel@tonic-gate 	if (list_head(&ctp->conp_members) == NULL) {
6037c478bd9Sstevel@tonic-gate 		contract_destroy(ct);
6047c478bd9Sstevel@tonic-gate 	} else {
6057c478bd9Sstevel@tonic-gate 		/*
6067c478bd9Sstevel@tonic-gate 		 * Strictly speaking, we actually do orphan the contract.
6077c478bd9Sstevel@tonic-gate 		 * Assuming our credentials allow us to kill all
6087c478bd9Sstevel@tonic-gate 		 * processes in the contract, this is only temporary.
6097c478bd9Sstevel@tonic-gate 		 */
6107c478bd9Sstevel@tonic-gate 		if (ctp->conp_params & CT_PR_NOORPHAN)
6117c478bd9Sstevel@tonic-gate 			contract_process_kill(ct, NULL, B_TRUE);
6127c478bd9Sstevel@tonic-gate 		contract_orphan(ct);
6137c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
6147c478bd9Sstevel@tonic-gate 		contract_rele(ct);
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate  * contract_process_destroy
6207c478bd9Sstevel@tonic-gate  *
6217c478bd9Sstevel@tonic-gate  * The process contract destroy entry point.
6227c478bd9Sstevel@tonic-gate  */
6237c478bd9Sstevel@tonic-gate static void
6247c478bd9Sstevel@tonic-gate contract_process_destroy(contract_t *ct)
6257c478bd9Sstevel@tonic-gate {
6267c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
6277c478bd9Sstevel@tonic-gate 	contract_t *cct;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/*
6327c478bd9Sstevel@tonic-gate 	 * contract_destroy all empty children, kill or orphan the rest
6337c478bd9Sstevel@tonic-gate 	 */
6347c478bd9Sstevel@tonic-gate 	while (cct = list_head(&ctp->conp_inherited)) {
6357c478bd9Sstevel@tonic-gate 		mutex_enter(&cct->ct_lock);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 		ASSERT(cct->ct_state == CTS_INHERITED);
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 		list_remove(&ctp->conp_inherited, cct);
6407c478bd9Sstevel@tonic-gate 		ctp->conp_ninherited--;
6417c478bd9Sstevel@tonic-gate 		cct->ct_regent = NULL;
6427c478bd9Sstevel@tonic-gate 		cct->ct_type->ct_type_ops->contop_abandon(cct);
6437c478bd9Sstevel@tonic-gate 	}
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate /*
6477c478bd9Sstevel@tonic-gate  * contract_process_status
6487c478bd9Sstevel@tonic-gate  *
6497c478bd9Sstevel@tonic-gate  * The process contract status entry point.
6507c478bd9Sstevel@tonic-gate  */
6517c478bd9Sstevel@tonic-gate static void
6527c478bd9Sstevel@tonic-gate contract_process_status(contract_t *ct, zone_t *zone, int detail, nvlist_t *nvl,
6537c478bd9Sstevel@tonic-gate     void *status, model_t model)
6547c478bd9Sstevel@tonic-gate {
6557c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
6567c478bd9Sstevel@tonic-gate 	uint32_t *pids, *ctids;
6577c478bd9Sstevel@tonic-gate 	uint_t npids, nctids;
6587c478bd9Sstevel@tonic-gate 	uint_t spids, sctids;
659*7b209c2cSacruz 	ctid_t local_svc_zone_enter;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	if (detail == CTD_FIXED) {
6627c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
6637c478bd9Sstevel@tonic-gate 		contract_status_common(ct, zone, status, model);
664*7b209c2cSacruz 		local_svc_zone_enter = ctp->conp_svc_zone_enter;
6657c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
6667c478bd9Sstevel@tonic-gate 	} else {
6677c478bd9Sstevel@tonic-gate 		contract_t *cnext;
6687c478bd9Sstevel@tonic-gate 		proc_t *pnext;
6697c478bd9Sstevel@tonic-gate 		uint_t loc;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 		ASSERT(detail == CTD_ALL);
6727c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
6737c478bd9Sstevel@tonic-gate 		for (;;) {
6747c478bd9Sstevel@tonic-gate 			spids = ctp->conp_nmembers + 5;
6757c478bd9Sstevel@tonic-gate 			sctids = ctp->conp_ninherited + 5;
6767c478bd9Sstevel@tonic-gate 			mutex_exit(&ct->ct_lock);
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 			pids = kmem_alloc(spids * sizeof (uint32_t), KM_SLEEP);
6797c478bd9Sstevel@tonic-gate 			ctids = kmem_alloc(sctids * sizeof (uint32_t),
6807c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 			mutex_enter(&ct->ct_lock);
6837c478bd9Sstevel@tonic-gate 			npids = ctp->conp_nmembers;
6847c478bd9Sstevel@tonic-gate 			nctids = ctp->conp_ninherited;
6857c478bd9Sstevel@tonic-gate 			if (spids >= npids && sctids >= nctids)
6867c478bd9Sstevel@tonic-gate 				break;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 			kmem_free(pids, spids * sizeof (uint32_t));
6897c478bd9Sstevel@tonic-gate 			kmem_free(ctids, sctids * sizeof (uint32_t));
6907c478bd9Sstevel@tonic-gate 		}
6917c478bd9Sstevel@tonic-gate 		contract_status_common(ct, zone, status, model);
6927c478bd9Sstevel@tonic-gate 		for (loc = 0, cnext = list_head(&ctp->conp_inherited); cnext;
6937c478bd9Sstevel@tonic-gate 		    cnext = list_next(&ctp->conp_inherited, cnext))
6947c478bd9Sstevel@tonic-gate 			ctids[loc++] = cnext->ct_id;
6957c478bd9Sstevel@tonic-gate 		ASSERT(loc == nctids);
6967c478bd9Sstevel@tonic-gate 		for (loc = 0, pnext = list_head(&ctp->conp_members); pnext;
6977c478bd9Sstevel@tonic-gate 		    pnext = list_next(&ctp->conp_members, pnext))
6987c478bd9Sstevel@tonic-gate 			pids[loc++] = pnext->p_pid;
6997c478bd9Sstevel@tonic-gate 		ASSERT(loc == npids);
700*7b209c2cSacruz 		local_svc_zone_enter = ctp->conp_svc_zone_enter;
7017c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	/*
7057c478bd9Sstevel@tonic-gate 	 * Contract terms are static; there's no need to hold the
7067c478bd9Sstevel@tonic-gate 	 * contract lock while accessing them.
7077c478bd9Sstevel@tonic-gate 	 */
7087c478bd9Sstevel@tonic-gate 	VERIFY(nvlist_add_uint32(nvl, CTPS_PARAMS, ctp->conp_params) == 0);
7097c478bd9Sstevel@tonic-gate 	VERIFY(nvlist_add_uint32(nvl, CTPS_EV_FATAL, ctp->conp_ev_fatal) == 0);
7107c478bd9Sstevel@tonic-gate 	if (detail == CTD_ALL) {
7117c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32_array(nvl, CTPS_MEMBERS, pids,
7127c478bd9Sstevel@tonic-gate 		    npids) == 0);
7137c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32_array(nvl, CTPS_CONTRACTS, ctids,
7147c478bd9Sstevel@tonic-gate 		    nctids) == 0);
715*7b209c2cSacruz 		VERIFY(nvlist_add_string(nvl, CTPS_CREATOR_AUX,
716*7b209c2cSacruz 		    refstr_value(ctp->conp_svc_aux)) == 0);
717*7b209c2cSacruz 		VERIFY(nvlist_add_string(nvl, CTPS_SVC_CREATOR,
718*7b209c2cSacruz 		    refstr_value(ctp->conp_svc_creator)) == 0);
7197c478bd9Sstevel@tonic-gate 		kmem_free(pids, spids * sizeof (uint32_t));
7207c478bd9Sstevel@tonic-gate 		kmem_free(ctids, sctids * sizeof (uint32_t));
7217c478bd9Sstevel@tonic-gate 	}
722*7b209c2cSacruz 
723*7b209c2cSacruz 	/*
724*7b209c2cSacruz 	 * if we are in a local zone and svc_fmri was inherited from
725*7b209c2cSacruz 	 * the global zone, we provide fake svc_fmri and svc_ctid
726*7b209c2cSacruz 	 */
727*7b209c2cSacruz 	if (local_svc_zone_enter == 0||
728*7b209c2cSacruz 	    zone->zone_uniqid == GLOBAL_ZONEUNIQID) {
729*7b209c2cSacruz 		if (detail > CTD_COMMON) {
730*7b209c2cSacruz 			VERIFY(nvlist_add_int32(nvl, CTPS_SVC_CTID,
731*7b209c2cSacruz 			    ctp->conp_svc_ctid) == 0);
732*7b209c2cSacruz 		}
733*7b209c2cSacruz 		if (detail == CTD_ALL) {
734*7b209c2cSacruz 			VERIFY(nvlist_add_string(nvl, CTPS_SVC_FMRI,
735*7b209c2cSacruz 			    refstr_value(ctp->conp_svc_fmri)) == 0);
736*7b209c2cSacruz 		}
737*7b209c2cSacruz 	} else {
738*7b209c2cSacruz 		if (detail > CTD_COMMON) {
739*7b209c2cSacruz 			VERIFY(nvlist_add_int32(nvl, CTPS_SVC_CTID,
740*7b209c2cSacruz 			    local_svc_zone_enter) == 0);
741*7b209c2cSacruz 		}
742*7b209c2cSacruz 		if (detail == CTD_ALL) {
743*7b209c2cSacruz 			VERIFY(nvlist_add_string(nvl, CTPS_SVC_FMRI,
744*7b209c2cSacruz 			    CT_PR_SVC_FMRI_ZONE_ENTER) == 0);
745*7b209c2cSacruz 		}
746*7b209c2cSacruz 	}
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate 
74925e8c5aaSvikram /*ARGSUSED*/
75025e8c5aaSvikram static int
75125e8c5aaSvikram contract_process_newct(contract_t *ct)
75225e8c5aaSvikram {
75325e8c5aaSvikram 	return (0);
75425e8c5aaSvikram }
75525e8c5aaSvikram 
75625e8c5aaSvikram /* process contracts don't negotiate */
7577c478bd9Sstevel@tonic-gate static contops_t contract_process_ops = {
7587c478bd9Sstevel@tonic-gate 	contract_process_free,		/* contop_free */
7597c478bd9Sstevel@tonic-gate 	contract_process_abandon,	/* contop_abandon */
7607c478bd9Sstevel@tonic-gate 	contract_process_destroy,	/* contop_destroy */
76125e8c5aaSvikram 	contract_process_status,	/* contop_status */
76225e8c5aaSvikram 	contract_ack_inval,		/* contop_ack */
76325e8c5aaSvikram 	contract_ack_inval,		/* contop_nack */
76425e8c5aaSvikram 	contract_qack_inval,		/* contop_qack */
76525e8c5aaSvikram 	contract_process_newct		/* contop_newct */
7667c478bd9Sstevel@tonic-gate };
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate /*
7697c478bd9Sstevel@tonic-gate  * contract_process_init
7707c478bd9Sstevel@tonic-gate  *
7717c478bd9Sstevel@tonic-gate  * Initializes the process contract type.  Also creates a template for
7727c478bd9Sstevel@tonic-gate  * use by newproc() when it creates user processes.
7737c478bd9Sstevel@tonic-gate  */
7747c478bd9Sstevel@tonic-gate void
7757c478bd9Sstevel@tonic-gate contract_process_init(void)
7767c478bd9Sstevel@tonic-gate {
7777c478bd9Sstevel@tonic-gate 	process_type = contract_type_init(CTT_PROCESS, "process",
7787c478bd9Sstevel@tonic-gate 	    &contract_process_ops, contract_process_default);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	/*
7817c478bd9Sstevel@tonic-gate 	 * Create a template for use with init(1M) and other
7827c478bd9Sstevel@tonic-gate 	 * kernel-started processes.
7837c478bd9Sstevel@tonic-gate 	 */
7847c478bd9Sstevel@tonic-gate 	sys_process_tmpl = kmem_alloc(sizeof (ctmpl_process_t), KM_SLEEP);
7857c478bd9Sstevel@tonic-gate 	ctmpl_init(&sys_process_tmpl->ctp_ctmpl, &ctmpl_process_ops,
7867c478bd9Sstevel@tonic-gate 	    process_type, sys_process_tmpl);
7877c478bd9Sstevel@tonic-gate 	sys_process_tmpl->ctp_subsume = NULL;
7887c478bd9Sstevel@tonic-gate 	sys_process_tmpl->ctp_params = CT_PR_NOORPHAN;
7897c478bd9Sstevel@tonic-gate 	sys_process_tmpl->ctp_ev_fatal = CT_PR_EV_HWERR;
790*7b209c2cSacruz 	sys_process_tmpl->ctp_svc_fmri =
791*7b209c2cSacruz 	    refstr_alloc("svc:/system/init:default");
792*7b209c2cSacruz 	sys_process_tmpl->ctp_svc_aux = refstr_alloc("");
793*7b209c2cSacruz 	conp_svc_aux_default = sys_process_tmpl->ctp_svc_aux;
794*7b209c2cSacruz 	refstr_hold(conp_svc_aux_default);
7957c478bd9Sstevel@tonic-gate }
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate /*
7987c478bd9Sstevel@tonic-gate  * contract_process_create
7997c478bd9Sstevel@tonic-gate  *
8007c478bd9Sstevel@tonic-gate  * create a process contract given template "tmpl" and parent process
8017c478bd9Sstevel@tonic-gate  * "parent".  May fail and return NULL if project.max-contracts would
8027c478bd9Sstevel@tonic-gate  * have been exceeded.
8037c478bd9Sstevel@tonic-gate  */
8047c478bd9Sstevel@tonic-gate static cont_process_t *
8057c478bd9Sstevel@tonic-gate contract_process_create(ctmpl_process_t *tmpl, proc_t *parent, int canfail)
8067c478bd9Sstevel@tonic-gate {
8077c478bd9Sstevel@tonic-gate 	cont_process_t *ctp;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	ASSERT(tmpl != NULL);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	(void) contract_type_pbundle(process_type, parent);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	ctp = kmem_zalloc(sizeof (cont_process_t), KM_SLEEP);
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	list_create(&ctp->conp_members, sizeof (proc_t),
8167c478bd9Sstevel@tonic-gate 	    offsetof(proc_t, p_ct_member));
8177c478bd9Sstevel@tonic-gate 	list_create(&ctp->conp_inherited, sizeof (contract_t),
8187c478bd9Sstevel@tonic-gate 	    offsetof(contract_t, ct_ctlist));
8197c478bd9Sstevel@tonic-gate 	mutex_enter(&tmpl->ctp_ctmpl.ctmpl_lock);
8207c478bd9Sstevel@tonic-gate 	ctp->conp_params = tmpl->ctp_params;
8217c478bd9Sstevel@tonic-gate 	ctp->conp_ev_fatal = tmpl->ctp_ev_fatal;
8227c478bd9Sstevel@tonic-gate 	crhold(ctp->conp_cred = CRED());
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	if (contract_ctor(&ctp->conp_contract, process_type, &tmpl->ctp_ctmpl,
8257c478bd9Sstevel@tonic-gate 	    ctp, (ctp->conp_params & CT_PR_INHERIT) ? CTF_INHERIT : 0,
8267c478bd9Sstevel@tonic-gate 	    parent, canfail)) {
8277c478bd9Sstevel@tonic-gate 		mutex_exit(&tmpl->ctp_ctmpl.ctmpl_lock);
8287c478bd9Sstevel@tonic-gate 		contract_process_free(&ctp->conp_contract);
8297c478bd9Sstevel@tonic-gate 		return (NULL);
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	/*
833*7b209c2cSacruz 	 * inherit svc_fmri if not defined by consumer. In this case, inherit
834*7b209c2cSacruz 	 * also svc_ctid to keep track of the contract id where
835*7b209c2cSacruz 	 * svc_fmri was set
836*7b209c2cSacruz 	 */
837*7b209c2cSacruz 	if (tmpl->ctp_svc_fmri == NULL) {
838*7b209c2cSacruz 		ctp->conp_svc_fmri = parent->p_ct_process->conp_svc_fmri;
839*7b209c2cSacruz 		ctp->conp_svc_ctid = parent->p_ct_process->conp_svc_ctid;
840*7b209c2cSacruz 		ctp->conp_svc_zone_enter =
841*7b209c2cSacruz 		    parent->p_ct_process->conp_svc_zone_enter;
842*7b209c2cSacruz 	} else {
843*7b209c2cSacruz 		ctp->conp_svc_fmri = tmpl->ctp_svc_fmri;
844*7b209c2cSacruz 		ctp->conp_svc_ctid = ctp->conp_contract.ct_id;
845*7b209c2cSacruz 		/* make svc_zone_enter flag false when svc_fmri is set */
846*7b209c2cSacruz 		ctp->conp_svc_zone_enter = 0;
847*7b209c2cSacruz 	}
848*7b209c2cSacruz 	refstr_hold(ctp->conp_svc_fmri);
849*7b209c2cSacruz 	/* set svc_aux to default value if not defined in template */
850*7b209c2cSacruz 	if (tmpl->ctp_svc_aux == NULL) {
851*7b209c2cSacruz 		ctp->conp_svc_aux = conp_svc_aux_default;
852*7b209c2cSacruz 	} else {
853*7b209c2cSacruz 		ctp->conp_svc_aux = tmpl->ctp_svc_aux;
854*7b209c2cSacruz 	}
855*7b209c2cSacruz 	refstr_hold(ctp->conp_svc_aux);
856*7b209c2cSacruz 	/*
857*7b209c2cSacruz 	 * set svc_creator to execname
858*7b209c2cSacruz 	 * We special case pid0 because when newproc() creates
859*7b209c2cSacruz 	 * the init process, the p_user.u_comm field of sched's proc_t
860*7b209c2cSacruz 	 * has not been populated yet.
861*7b209c2cSacruz 	 */
862*7b209c2cSacruz 	if (parent->p_pidp == &pid0) /* if the kernel is the creator */
863*7b209c2cSacruz 		ctp->conp_svc_creator = refstr_alloc("sched");
864*7b209c2cSacruz 	else
865*7b209c2cSacruz 		ctp->conp_svc_creator = refstr_alloc(parent->p_user.u_comm);
866*7b209c2cSacruz 
867*7b209c2cSacruz 	/*
8687c478bd9Sstevel@tonic-gate 	 * Transfer subcontracts only after new contract is visible.
8697c478bd9Sstevel@tonic-gate 	 * Also, only transfer contracts if the parent matches -- we
8707c478bd9Sstevel@tonic-gate 	 * don't want to create a cycle in the tree of contracts.
8717c478bd9Sstevel@tonic-gate 	 */
8727c478bd9Sstevel@tonic-gate 	if (tmpl->ctp_subsume && tmpl->ctp_subsume->ct_owner == parent) {
8737c478bd9Sstevel@tonic-gate 		cont_process_t *sct = tmpl->ctp_subsume->ct_data;
8747c478bd9Sstevel@tonic-gate 		contract_t *ct;
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 		mutex_enter(&tmpl->ctp_subsume->ct_lock);
8777c478bd9Sstevel@tonic-gate 		mutex_enter(&ctp->conp_contract.ct_lock);
8787c478bd9Sstevel@tonic-gate 		while (ct = list_head(&sct->conp_inherited)) {
8797c478bd9Sstevel@tonic-gate 			mutex_enter(&ct->ct_lock);
8807c478bd9Sstevel@tonic-gate 			list_remove(&sct->conp_inherited, ct);
8817c478bd9Sstevel@tonic-gate 			list_insert_tail(&ctp->conp_inherited, ct);
8827c478bd9Sstevel@tonic-gate 			ct->ct_regent = &ctp->conp_contract;
8837c478bd9Sstevel@tonic-gate 			mutex_exit(&ct->ct_lock);
8847c478bd9Sstevel@tonic-gate 		}
8857c478bd9Sstevel@tonic-gate 		ctp->conp_ninherited += sct->conp_ninherited;
8867c478bd9Sstevel@tonic-gate 		sct->conp_ninherited = 0;
8877c478bd9Sstevel@tonic-gate 		mutex_exit(&ctp->conp_contract.ct_lock);
8887c478bd9Sstevel@tonic-gate 		mutex_exit(&tmpl->ctp_subsume->ct_lock);
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 		/*
8917c478bd9Sstevel@tonic-gate 		 * Automatically abandon the contract.
8927c478bd9Sstevel@tonic-gate 		 */
8937c478bd9Sstevel@tonic-gate 		(void) contract_abandon(tmpl->ctp_subsume, parent, 1);
8947c478bd9Sstevel@tonic-gate 	}
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	mutex_exit(&tmpl->ctp_ctmpl.ctmpl_lock);
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	return (ctp);
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate /*
9027c478bd9Sstevel@tonic-gate  * contract_process_exit
9037c478bd9Sstevel@tonic-gate  *
9047c478bd9Sstevel@tonic-gate  * Called on process exit.  Removes process p from process contract
9057c478bd9Sstevel@tonic-gate  * ctp.  Generates an exit event, if requested.  Generates an empty
9067c478bd9Sstevel@tonic-gate  * event, if p is the last member of the the process contract and empty
9077c478bd9Sstevel@tonic-gate  * events were requested.
9087c478bd9Sstevel@tonic-gate  */
9097c478bd9Sstevel@tonic-gate void
9107c478bd9Sstevel@tonic-gate contract_process_exit(cont_process_t *ctp, proc_t *p, int exitstatus)
9117c478bd9Sstevel@tonic-gate {
9127c478bd9Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
9137c478bd9Sstevel@tonic-gate 	ct_kevent_t *event;
9147c478bd9Sstevel@tonic-gate 	int empty;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	/*
9177c478bd9Sstevel@tonic-gate 	 * Remove self from process contract.
9187c478bd9Sstevel@tonic-gate 	 */
9197c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
9207c478bd9Sstevel@tonic-gate 	list_remove(&ctp->conp_members, p);
9217c478bd9Sstevel@tonic-gate 	ctp->conp_nmembers--;
9227c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);	/* in case /proc is watching */
9237c478bd9Sstevel@tonic-gate 	p->p_ct_process = NULL;
9247c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	/*
9277c478bd9Sstevel@tonic-gate 	 * We check for emptiness before dropping the contract lock to
9287c478bd9Sstevel@tonic-gate 	 * send the exit event, otherwise we could end up with two
9297c478bd9Sstevel@tonic-gate 	 * empty events.
9307c478bd9Sstevel@tonic-gate 	 */
9317c478bd9Sstevel@tonic-gate 	empty = (list_head(&ctp->conp_members) == NULL);
9327c478bd9Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_EXIT)) {
9337c478bd9Sstevel@tonic-gate 		nvlist_t *nvl;
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
9367c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
9377c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
9387c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_int32(nvl, CTPE_EXITSTATUS, exitstatus) == 0);
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
9417c478bd9Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_EXIT) ? CTE_INFO : 0;
9427c478bd9Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_EXIT;
94325e8c5aaSvikram 		(void) cte_publish_all(ct, event, nvl, NULL);
9447c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 	if (empty) {
9477c478bd9Sstevel@tonic-gate 		/*
9487c478bd9Sstevel@tonic-gate 		 * Send EMPTY message.
9497c478bd9Sstevel@tonic-gate 		 */
9507c478bd9Sstevel@tonic-gate 		if (EVSENDP(ctp, CT_PR_EV_EMPTY)) {
9517c478bd9Sstevel@tonic-gate 			nvlist_t *nvl;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 			mutex_exit(&ct->ct_lock);
9547c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME,
9557c478bd9Sstevel@tonic-gate 			    KM_SLEEP) == 0);
9567c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 			event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
9597c478bd9Sstevel@tonic-gate 			event->cte_flags = EVINFOP(ctp, CT_PR_EV_EMPTY) ?
9607c478bd9Sstevel@tonic-gate 			    CTE_INFO : 0;
9617c478bd9Sstevel@tonic-gate 			event->cte_type = CT_PR_EV_EMPTY;
96225e8c5aaSvikram 			(void) cte_publish_all(ct, event, nvl, NULL);
9637c478bd9Sstevel@tonic-gate 			mutex_enter(&ct->ct_lock);
9647c478bd9Sstevel@tonic-gate 		}
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 		/*
9677c478bd9Sstevel@tonic-gate 		 * The last one to leave an orphaned contract turns out
9687c478bd9Sstevel@tonic-gate 		 * the lights.
9697c478bd9Sstevel@tonic-gate 		 */
9707c478bd9Sstevel@tonic-gate 		if (ct->ct_state == CTS_ORPHAN) {
9717c478bd9Sstevel@tonic-gate 			contract_destroy(ct);
9727c478bd9Sstevel@tonic-gate 			return;
9737c478bd9Sstevel@tonic-gate 		}
9747c478bd9Sstevel@tonic-gate 	}
9757c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
9767c478bd9Sstevel@tonic-gate 	contract_rele(ct);
9777c478bd9Sstevel@tonic-gate }
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate /*
9807c478bd9Sstevel@tonic-gate  * contract_process_fork
9817c478bd9Sstevel@tonic-gate  *
9827c478bd9Sstevel@tonic-gate  * Called on process fork.  If the current lwp has a active process
9837c478bd9Sstevel@tonic-gate  * contract template, we attempt to create a new process contract.
9847c478bd9Sstevel@tonic-gate  * Failure to create a process contract when required is a failure in
9857c478bd9Sstevel@tonic-gate  * fork so, in such an event, we return NULL.
9867c478bd9Sstevel@tonic-gate  *
9877c478bd9Sstevel@tonic-gate  * Assuming we succeeded or skipped the previous step, we add the child
9887c478bd9Sstevel@tonic-gate  * process to the new contract (success) or to the parent's process
9897c478bd9Sstevel@tonic-gate  * contract (skip).  If requested, we also send a fork event to that
9907c478bd9Sstevel@tonic-gate  * contract.
9917c478bd9Sstevel@tonic-gate  *
9927c478bd9Sstevel@tonic-gate  * Because contract_process_fork() may fail, and because we would
9937c478bd9Sstevel@tonic-gate  * prefer that process contracts not be created for processes which
9947c478bd9Sstevel@tonic-gate  * don't complete forking, this should be the last function called
9957c478bd9Sstevel@tonic-gate  * before the "all clear" point in cfork.
9967c478bd9Sstevel@tonic-gate  */
9977c478bd9Sstevel@tonic-gate cont_process_t *
9987c478bd9Sstevel@tonic-gate contract_process_fork(ctmpl_process_t *rtmpl, proc_t *cp, proc_t *pp,
9997c478bd9Sstevel@tonic-gate     int canfail)
10007c478bd9Sstevel@tonic-gate {
10017c478bd9Sstevel@tonic-gate 	contract_t *ct;
10027c478bd9Sstevel@tonic-gate 	cont_process_t *ctp;
10037c478bd9Sstevel@tonic-gate 	ct_kevent_t *event;
10047c478bd9Sstevel@tonic-gate 	ct_template_t *tmpl;
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	if (rtmpl == NULL && (tmpl = ttolwp(curthread)->lwp_ct_active[
10077c478bd9Sstevel@tonic-gate 	    process_type->ct_type_index]) != NULL)
10087c478bd9Sstevel@tonic-gate 		rtmpl = tmpl->ctmpl_data;
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	if (rtmpl == NULL)
10117c478bd9Sstevel@tonic-gate 		ctp = curproc->p_ct_process;
10127c478bd9Sstevel@tonic-gate 	else if ((ctp = contract_process_create(rtmpl, pp, canfail)) == NULL)
10137c478bd9Sstevel@tonic-gate 		return (NULL);
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	ct = &ctp->conp_contract;
10167c478bd9Sstevel@tonic-gate 	/*
10177c478bd9Sstevel@tonic-gate 	 * Prevent contract_process_kill() from missing forked children
10187c478bd9Sstevel@tonic-gate 	 * by failing forks by parents that have just been killed.
10197c478bd9Sstevel@tonic-gate 	 * It's not worth hoisting the ctp test since contract creation
10207c478bd9Sstevel@tonic-gate 	 * is by no means the common case.
10217c478bd9Sstevel@tonic-gate 	 */
10227c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
10237c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
10247c478bd9Sstevel@tonic-gate 	if (ctp == curproc->p_ct_process && (pp->p_flag & SKILLED) != 0 &&
10257c478bd9Sstevel@tonic-gate 	    canfail) {
10267c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
10277c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
10287c478bd9Sstevel@tonic-gate 		return (NULL);
10297c478bd9Sstevel@tonic-gate 	}
10307c478bd9Sstevel@tonic-gate 	cp->p_ct_process = ctp;
10317c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
10327c478bd9Sstevel@tonic-gate 	contract_hold(ct);
10337c478bd9Sstevel@tonic-gate 	list_insert_head(&ctp->conp_members, cp);
10347c478bd9Sstevel@tonic-gate 	ctp->conp_nmembers++;
10357c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
10367c478bd9Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_FORK)) {
10377c478bd9Sstevel@tonic-gate 		nvlist_t *nvl;
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
10407c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, cp->p_pid) == 0);
10417c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PPID, pp->p_pid) == 0);
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
10447c478bd9Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_FORK) ? CTE_INFO : 0;
10457c478bd9Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_FORK;
104625e8c5aaSvikram 		(void) cte_publish_all(ct, event, nvl, NULL);
10477c478bd9Sstevel@tonic-gate 	}
10487c478bd9Sstevel@tonic-gate 	return (ctp);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate /*
10527c478bd9Sstevel@tonic-gate  * contract_process_core
10537c478bd9Sstevel@tonic-gate  *
10547c478bd9Sstevel@tonic-gate  * Called on core file generation attempts.  Generates a core event, if
10557c478bd9Sstevel@tonic-gate  * requested, containing the names of the process, global, and
10567c478bd9Sstevel@tonic-gate  * system-global ("zone") core files.  If dumping core is in the fatal
10577c478bd9Sstevel@tonic-gate  * event set, calls contract_process_kill().
10587c478bd9Sstevel@tonic-gate  */
10597c478bd9Sstevel@tonic-gate void
10607c478bd9Sstevel@tonic-gate contract_process_core(cont_process_t *ctp, proc_t *p, int sig,
10617c478bd9Sstevel@tonic-gate     const char *process, const char *global, const char *zone)
10627c478bd9Sstevel@tonic-gate {
10637c478bd9Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_CORE)) {
10667c478bd9Sstevel@tonic-gate 		ct_kevent_t *event;
10677c478bd9Sstevel@tonic-gate 		nvlist_t *nvl, *gnvl = NULL;
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
10707c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
10717c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_SIGNAL, sig) == 0);
10727c478bd9Sstevel@tonic-gate 		if (process)
10737c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_string(nvl, CTPE_PCOREFILE,
10747c478bd9Sstevel@tonic-gate 			    (char *)process) == 0);
10757c478bd9Sstevel@tonic-gate 		if (global)
10767c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_string(nvl, CTPE_GCOREFILE,
10777c478bd9Sstevel@tonic-gate 			    (char *)global) == 0);
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 		if (zone) {
10807c478bd9Sstevel@tonic-gate 			/*
10817c478bd9Sstevel@tonic-gate 			 * Only the global zone is informed of the
10827c478bd9Sstevel@tonic-gate 			 * local-zone generated global-zone core.
10837c478bd9Sstevel@tonic-gate 			 */
10847c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_alloc(&gnvl, NV_UNIQUE_NAME,
10857c478bd9Sstevel@tonic-gate 			    KM_SLEEP) == 0);
10867c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_string(gnvl, CTPE_ZCOREFILE,
10877c478bd9Sstevel@tonic-gate 			    (char *)zone) == 0);
10887c478bd9Sstevel@tonic-gate 		}
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
10917c478bd9Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_CORE) ? CTE_INFO : 0;
10927c478bd9Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_CORE;
109325e8c5aaSvikram 		(void) cte_publish_all(ct, event, nvl, gnvl);
10947c478bd9Sstevel@tonic-gate 	}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	if (EVFATALP(ctp, CT_PR_EV_CORE)) {
10977c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
10987c478bd9Sstevel@tonic-gate 		contract_process_kill(ct, p, B_TRUE);
10997c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
11007c478bd9Sstevel@tonic-gate 	}
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate /*
11047c478bd9Sstevel@tonic-gate  * contract_process_hwerr
11057c478bd9Sstevel@tonic-gate  *
11067c478bd9Sstevel@tonic-gate  * Called when a process is killed by an unrecoverable hardware error.
11077c478bd9Sstevel@tonic-gate  * Generates an hwerr event, if requested.  If hardware errors are in
11087c478bd9Sstevel@tonic-gate  * the fatal event set, calls contract_process_kill().
11097c478bd9Sstevel@tonic-gate  */
11107c478bd9Sstevel@tonic-gate void
11117c478bd9Sstevel@tonic-gate contract_process_hwerr(cont_process_t *ctp, proc_t *p)
11127c478bd9Sstevel@tonic-gate {
11137c478bd9Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_HWERR)) {
11167c478bd9Sstevel@tonic-gate 		ct_kevent_t *event;
11177c478bd9Sstevel@tonic-gate 		nvlist_t *nvl;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
11207c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
11237c478bd9Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_HWERR) ? CTE_INFO : 0;
11247c478bd9Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_HWERR;
112525e8c5aaSvikram 		(void) cte_publish_all(ct, event, nvl, NULL);
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	if (EVFATALP(ctp, CT_PR_EV_HWERR)) {
11297c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
11307c478bd9Sstevel@tonic-gate 		contract_process_kill(ct, p, B_FALSE);
11317c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate /*
11367c478bd9Sstevel@tonic-gate  * contract_process_sig
11377c478bd9Sstevel@tonic-gate  *
11387c478bd9Sstevel@tonic-gate  * Called when a process is killed by a signal originating from a
11397c478bd9Sstevel@tonic-gate  * process outside of its process contract or its process contract's
11407c478bd9Sstevel@tonic-gate  * holder.  Generates an signal event, if requested, containing the
11417c478bd9Sstevel@tonic-gate  * signal number, and the sender's pid and contract id (if available).
11427c478bd9Sstevel@tonic-gate  * If signals are in the fatal event set, calls
11437c478bd9Sstevel@tonic-gate  * contract_process_kill().
11447c478bd9Sstevel@tonic-gate  */
11457c478bd9Sstevel@tonic-gate void
11467c478bd9Sstevel@tonic-gate contract_process_sig(cont_process_t *ctp, proc_t *p, int sig, pid_t pid,
11477c478bd9Sstevel@tonic-gate     ctid_t ctid, zoneid_t zoneid)
11487c478bd9Sstevel@tonic-gate {
11497c478bd9Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_SIGNAL)) {
11527c478bd9Sstevel@tonic-gate 		ct_kevent_t *event;
11537c478bd9Sstevel@tonic-gate 		nvlist_t *dest, *nvl, *gnvl = NULL;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
11567c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
11577c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_SIGNAL, sig) == 0);
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 		if (zoneid >= 0 && p->p_zone->zone_id != zoneid) {
11607c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_alloc(&gnvl, NV_UNIQUE_NAME,
11617c478bd9Sstevel@tonic-gate 			    KM_SLEEP) == 0);
11627c478bd9Sstevel@tonic-gate 			dest = gnvl;
11637c478bd9Sstevel@tonic-gate 		} else {
11647c478bd9Sstevel@tonic-gate 			dest = nvl;
11657c478bd9Sstevel@tonic-gate 		}
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 		if (pid != -1)
11687c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_uint32(dest, CTPE_SENDER, pid) == 0);
11697c478bd9Sstevel@tonic-gate 		if (ctid != 0)
11707c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_uint32(dest, CTPE_SENDCT, ctid) == 0);
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
11737c478bd9Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_SIGNAL) ? CTE_INFO : 0;
11747c478bd9Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_SIGNAL;
117525e8c5aaSvikram 		(void) cte_publish_all(ct, event, nvl, gnvl);
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	if (EVFATALP(ctp, CT_PR_EV_SIGNAL)) {
11797c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
11807c478bd9Sstevel@tonic-gate 		contract_process_kill(ct, p, B_TRUE);
11817c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
11827c478bd9Sstevel@tonic-gate 	}
11837c478bd9Sstevel@tonic-gate }
1184