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