xref: /titanic_44/usr/src/uts/common/contract/process.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/thread.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/id_space.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/avl.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/list.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/contract.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/contract/process.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/contract/process_impl.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/policy.h>
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate /*
49*7c478bd9Sstevel@tonic-gate  * Process Contracts
50*7c478bd9Sstevel@tonic-gate  * -----------------
51*7c478bd9Sstevel@tonic-gate  *
52*7c478bd9Sstevel@tonic-gate  * Generally speaking, a process contract is a contract between a
53*7c478bd9Sstevel@tonic-gate  * process and a set of its descendent processes.  In some cases, when
54*7c478bd9Sstevel@tonic-gate  * the child processes outlive the author of the contract, the contract
55*7c478bd9Sstevel@tonic-gate  * may be held by (and therefore be between the child processes and) a
56*7c478bd9Sstevel@tonic-gate  * successor process which adopts the contract after the death of the
57*7c478bd9Sstevel@tonic-gate  * original author.
58*7c478bd9Sstevel@tonic-gate  *
59*7c478bd9Sstevel@tonic-gate  * The process contract adds two new concepts to the Solaris process
60*7c478bd9Sstevel@tonic-gate  * model.  The first is that a process contract forms a rigid fault
61*7c478bd9Sstevel@tonic-gate  * boundary around a set of processes.  Hardware, software, and even
62*7c478bd9Sstevel@tonic-gate  * administrator errors impacting a process in a process contract
63*7c478bd9Sstevel@tonic-gate  * generate specific events and can be requested to atomically shutdown
64*7c478bd9Sstevel@tonic-gate  * all processes in the contract.  The second is that a process
65*7c478bd9Sstevel@tonic-gate  * contract is a process collective whose leader is not a member of the
66*7c478bd9Sstevel@tonic-gate  * collective.  This means that the leader can reliably react to events
67*7c478bd9Sstevel@tonic-gate  * in the collective, and may also act upon the collective without
68*7c478bd9Sstevel@tonic-gate  * special casing itself.
69*7c478bd9Sstevel@tonic-gate  *
70*7c478bd9Sstevel@tonic-gate  * A composite outcome of these two concepts is that we can now create
71*7c478bd9Sstevel@tonic-gate  * a tree of process contracts, rooted at init(1M), which represent
72*7c478bd9Sstevel@tonic-gate  * services and subservices that are reliably observed and can be
73*7c478bd9Sstevel@tonic-gate  * restarted when fatal errors occur.  The service management framework
74*7c478bd9Sstevel@tonic-gate  * (SMF) realizes this structure.
75*7c478bd9Sstevel@tonic-gate  *
76*7c478bd9Sstevel@tonic-gate  * For more details, see the "restart agreements" case, PSARC 2003/193.
77*7c478bd9Sstevel@tonic-gate  *
78*7c478bd9Sstevel@tonic-gate  * There are four sets of routines in this file: the process contract
79*7c478bd9Sstevel@tonic-gate  * standard template operations, the process contract standard contract
80*7c478bd9Sstevel@tonic-gate  * operations, a couple routines used only by the contract subsystem to
81*7c478bd9Sstevel@tonic-gate  * handle process contracts' unique role as a temporary holder of
82*7c478bd9Sstevel@tonic-gate  * abandoned contracts, and the interfaces which allow the system to
83*7c478bd9Sstevel@tonic-gate  * create and act upon process contracts.  The first two are defined by
84*7c478bd9Sstevel@tonic-gate  * the contracts framework and won't be discussed further.  As for the
85*7c478bd9Sstevel@tonic-gate  * remaining two:
86*7c478bd9Sstevel@tonic-gate  *
87*7c478bd9Sstevel@tonic-gate  * Special framework interfaces
88*7c478bd9Sstevel@tonic-gate  * ----------------------------
89*7c478bd9Sstevel@tonic-gate  *
90*7c478bd9Sstevel@tonic-gate  * contract_process_accept - determines if a process contract is a
91*7c478bd9Sstevel@tonic-gate  *   regent, i.e. if it can inherit other contracts.
92*7c478bd9Sstevel@tonic-gate  *
93*7c478bd9Sstevel@tonic-gate  * contract_process_take - tells a regent process contract to inherit
94*7c478bd9Sstevel@tonic-gate  *   an abandoned contract
95*7c478bd9Sstevel@tonic-gate  *
96*7c478bd9Sstevel@tonic-gate  * contract_process_adopt - tells a regent process contract that a
97*7c478bd9Sstevel@tonic-gate  *   contract it has inherited is being adopted by a process.
98*7c478bd9Sstevel@tonic-gate  *
99*7c478bd9Sstevel@tonic-gate  * Process contract interfaces
100*7c478bd9Sstevel@tonic-gate  * ---------------------------
101*7c478bd9Sstevel@tonic-gate  *
102*7c478bd9Sstevel@tonic-gate  * contract_process_fork - called when a process is created; adds the
103*7c478bd9Sstevel@tonic-gate  *   new process to an existing contract or to a newly created one.
104*7c478bd9Sstevel@tonic-gate  *
105*7c478bd9Sstevel@tonic-gate  * contract_process_exit - called when a process exits
106*7c478bd9Sstevel@tonic-gate  *
107*7c478bd9Sstevel@tonic-gate  * contract_process_core - called when a process would have dumped core
108*7c478bd9Sstevel@tonic-gate  *   (even if a core file wasn't generated)
109*7c478bd9Sstevel@tonic-gate  *
110*7c478bd9Sstevel@tonic-gate  * contract_process_hwerr - called when a process was killed because of
111*7c478bd9Sstevel@tonic-gate  *   an uncorrectable hardware error
112*7c478bd9Sstevel@tonic-gate  *
113*7c478bd9Sstevel@tonic-gate  * contract_process_sig - called when a process was killed by a fatal
114*7c478bd9Sstevel@tonic-gate  *   signal sent by a process in another process contract
115*7c478bd9Sstevel@tonic-gate  *
116*7c478bd9Sstevel@tonic-gate  */
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate ct_type_t *process_type;
119*7c478bd9Sstevel@tonic-gate ctmpl_process_t *sys_process_tmpl;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate /*
122*7c478bd9Sstevel@tonic-gate  * Macro predicates for determining when events should be sent and how.
123*7c478bd9Sstevel@tonic-gate  */
124*7c478bd9Sstevel@tonic-gate #define	EVSENDP(ctp, flag) \
125*7c478bd9Sstevel@tonic-gate 	((ctp->conp_contract.ct_ev_info | ctp->conp_contract.ct_ev_crit) & flag)
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate #define	EVINFOP(ctp, flag) \
128*7c478bd9Sstevel@tonic-gate 	((ctp->conp_contract.ct_ev_crit & flag) == 0)
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate #define	EVFATALP(ctp, flag) \
131*7c478bd9Sstevel@tonic-gate 	(ctp->conp_ev_fatal & flag)
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate /*
135*7c478bd9Sstevel@tonic-gate  * Process contract template implementation
136*7c478bd9Sstevel@tonic-gate  */
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate /*
139*7c478bd9Sstevel@tonic-gate  * ctmpl_process_dup
140*7c478bd9Sstevel@tonic-gate  *
141*7c478bd9Sstevel@tonic-gate  * The process contract template dup entry point.  Other than the
142*7c478bd9Sstevel@tonic-gate  * to-be-subsumed contract, which must be held, this simply copies all
143*7c478bd9Sstevel@tonic-gate  * the fields of the original.
144*7c478bd9Sstevel@tonic-gate  */
145*7c478bd9Sstevel@tonic-gate static struct ct_template *
146*7c478bd9Sstevel@tonic-gate ctmpl_process_dup(struct ct_template *template)
147*7c478bd9Sstevel@tonic-gate {
148*7c478bd9Sstevel@tonic-gate 	ctmpl_process_t *new;
149*7c478bd9Sstevel@tonic-gate 	ctmpl_process_t *old = template->ctmpl_data;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	new = kmem_alloc(sizeof (ctmpl_process_t), KM_SLEEP);
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	ctmpl_copy(&new->ctp_ctmpl, template);
154*7c478bd9Sstevel@tonic-gate 	new->ctp_ctmpl.ctmpl_data = new;
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	new->ctp_subsume = old->ctp_subsume;
157*7c478bd9Sstevel@tonic-gate 	if (new->ctp_subsume)
158*7c478bd9Sstevel@tonic-gate 		contract_hold(new->ctp_subsume);
159*7c478bd9Sstevel@tonic-gate 	new->ctp_params = old->ctp_params;
160*7c478bd9Sstevel@tonic-gate 	new->ctp_ev_fatal = old->ctp_ev_fatal;
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	return (&new->ctp_ctmpl);
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate /*
166*7c478bd9Sstevel@tonic-gate  * ctmpl_process_dup
167*7c478bd9Sstevel@tonic-gate  *
168*7c478bd9Sstevel@tonic-gate  * The process contract template free entry point.  Just releases a
169*7c478bd9Sstevel@tonic-gate  * to-be-subsumed contract and frees the template.
170*7c478bd9Sstevel@tonic-gate  */
171*7c478bd9Sstevel@tonic-gate static void
172*7c478bd9Sstevel@tonic-gate ctmpl_process_free(struct ct_template *template)
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate 	ctmpl_process_t *ctp = template->ctmpl_data;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	if (ctp->ctp_subsume)
177*7c478bd9Sstevel@tonic-gate 		contract_rele(ctp->ctp_subsume);
178*7c478bd9Sstevel@tonic-gate 	kmem_free(template, sizeof (ctmpl_process_t));
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate /*
182*7c478bd9Sstevel@tonic-gate  * SAFE_EV is the set of events which a non-privileged process is
183*7c478bd9Sstevel@tonic-gate  * allowed to make critical but not fatal or if the PGRPONLY parameter
184*7c478bd9Sstevel@tonic-gate  * is set.  EXCESS tells us if "value", a critical event set, requires
185*7c478bd9Sstevel@tonic-gate  * additional privilege given the template "ctp".
186*7c478bd9Sstevel@tonic-gate  */
187*7c478bd9Sstevel@tonic-gate #define	SAFE_EV			(CT_PR_EV_EMPTY)
188*7c478bd9Sstevel@tonic-gate #define	EXCESS(ctp, value)	\
189*7c478bd9Sstevel@tonic-gate 	(((value) & ~((ctp)->ctp_ev_fatal | SAFE_EV)) || \
190*7c478bd9Sstevel@tonic-gate 	(((value) & ~SAFE_EV) && (ctp->ctp_params & CT_PR_PGRPONLY)))
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate /*
193*7c478bd9Sstevel@tonic-gate  * ctmpl_process_set
194*7c478bd9Sstevel@tonic-gate  *
195*7c478bd9Sstevel@tonic-gate  * The process contract template set entry point.  None of the terms
196*7c478bd9Sstevel@tonic-gate  * may be unconditionally set, and setting the parameters or fatal
197*7c478bd9Sstevel@tonic-gate  * event set may result in events being implicitly removed from to the
198*7c478bd9Sstevel@tonic-gate  * critical event set and added to the informative event set.  The
199*7c478bd9Sstevel@tonic-gate  * (admittedly subtle) reason we implicitly change the critical event
200*7c478bd9Sstevel@tonic-gate  * set when the parameter or fatal event set is modified but not the
201*7c478bd9Sstevel@tonic-gate  * other way around is because a change to the critical event set only
202*7c478bd9Sstevel@tonic-gate  * affects the contract's owner, whereas a change to the parameter set
203*7c478bd9Sstevel@tonic-gate  * and fatal set can affect the execution of the application running in
204*7c478bd9Sstevel@tonic-gate  * the contract (and should therefore be only made explicitly).  We
205*7c478bd9Sstevel@tonic-gate  * allow implicit changes at all so that setting contract terms doesn't
206*7c478bd9Sstevel@tonic-gate  * become a complex dance dependent on the template's initial state and
207*7c478bd9Sstevel@tonic-gate  * the desired terms.
208*7c478bd9Sstevel@tonic-gate  */
209*7c478bd9Sstevel@tonic-gate static int
210*7c478bd9Sstevel@tonic-gate ctmpl_process_set(struct ct_template *tmpl, ct_param_t *param, const cred_t *cr)
211*7c478bd9Sstevel@tonic-gate {
212*7c478bd9Sstevel@tonic-gate 	ctmpl_process_t *ctp = tmpl->ctmpl_data;
213*7c478bd9Sstevel@tonic-gate 	contract_t *ct;
214*7c478bd9Sstevel@tonic-gate 	int error;
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	/*
217*7c478bd9Sstevel@tonic-gate 	 * No process contract parameters are > 32 bits.
218*7c478bd9Sstevel@tonic-gate 	 */
219*7c478bd9Sstevel@tonic-gate 	if (param->ctpm_value & ~UINT32_MAX)
220*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	switch (param->ctpm_id) {
223*7c478bd9Sstevel@tonic-gate 	case CTPP_SUBSUME:
224*7c478bd9Sstevel@tonic-gate 		if (param->ctpm_value != 0) {
225*7c478bd9Sstevel@tonic-gate 			/*
226*7c478bd9Sstevel@tonic-gate 			 * Ensure that the contract exists, that we
227*7c478bd9Sstevel@tonic-gate 			 * hold the contract, and that the contract is
228*7c478bd9Sstevel@tonic-gate 			 * empty.
229*7c478bd9Sstevel@tonic-gate 			 */
230*7c478bd9Sstevel@tonic-gate 			ct = contract_type_ptr(process_type, param->ctpm_value,
231*7c478bd9Sstevel@tonic-gate 			    curproc->p_zone->zone_uniqid);
232*7c478bd9Sstevel@tonic-gate 			if (ct == NULL)
233*7c478bd9Sstevel@tonic-gate 				return (ESRCH);
234*7c478bd9Sstevel@tonic-gate 			if (ct->ct_owner != curproc) {
235*7c478bd9Sstevel@tonic-gate 				contract_rele(ct);
236*7c478bd9Sstevel@tonic-gate 				return (EACCES);
237*7c478bd9Sstevel@tonic-gate 			}
238*7c478bd9Sstevel@tonic-gate 			if (((cont_process_t *)ct->ct_data)->conp_nmembers) {
239*7c478bd9Sstevel@tonic-gate 				contract_rele(ct);
240*7c478bd9Sstevel@tonic-gate 				return (ENOTEMPTY);
241*7c478bd9Sstevel@tonic-gate 			}
242*7c478bd9Sstevel@tonic-gate 		} else {
243*7c478bd9Sstevel@tonic-gate 			ct = NULL;
244*7c478bd9Sstevel@tonic-gate 		}
245*7c478bd9Sstevel@tonic-gate 		if (ctp->ctp_subsume)
246*7c478bd9Sstevel@tonic-gate 			contract_rele(ctp->ctp_subsume);
247*7c478bd9Sstevel@tonic-gate 		ctp->ctp_subsume = ct;
248*7c478bd9Sstevel@tonic-gate 		break;
249*7c478bd9Sstevel@tonic-gate 	case CTPP_PARAMS:
250*7c478bd9Sstevel@tonic-gate 		if (param->ctpm_value & ~CT_PR_ALLPARAM)
251*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
252*7c478bd9Sstevel@tonic-gate 		ctp->ctp_params = param->ctpm_value;
253*7c478bd9Sstevel@tonic-gate 		/*
254*7c478bd9Sstevel@tonic-gate 		 * If an unprivileged process requests that
255*7c478bd9Sstevel@tonic-gate 		 * CT_PR_PGRPONLY be set, remove any unsafe events from
256*7c478bd9Sstevel@tonic-gate 		 * the critical event set and add them to the
257*7c478bd9Sstevel@tonic-gate 		 * informative event set.
258*7c478bd9Sstevel@tonic-gate 		 */
259*7c478bd9Sstevel@tonic-gate 		if ((ctp->ctp_params & CT_PR_PGRPONLY) &&
260*7c478bd9Sstevel@tonic-gate 		    EXCESS(ctp, tmpl->ctmpl_ev_crit) &&
261*7c478bd9Sstevel@tonic-gate 		    !secpolicy_contract_event_choice(cr)) {
262*7c478bd9Sstevel@tonic-gate 			tmpl->ctmpl_ev_info |= (tmpl->ctmpl_ev_crit & ~SAFE_EV);
263*7c478bd9Sstevel@tonic-gate 			tmpl->ctmpl_ev_crit &= SAFE_EV;
264*7c478bd9Sstevel@tonic-gate 		}
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate 		break;
267*7c478bd9Sstevel@tonic-gate 	case CTP_EV_CRITICAL:
268*7c478bd9Sstevel@tonic-gate 		/*
269*7c478bd9Sstevel@tonic-gate 		 * We simply don't allow adding events to the critical
270*7c478bd9Sstevel@tonic-gate 		 * event set which aren't permitted by our policy or by
271*7c478bd9Sstevel@tonic-gate 		 * privilege.
272*7c478bd9Sstevel@tonic-gate 		 */
273*7c478bd9Sstevel@tonic-gate 		if (EXCESS(ctp, param->ctpm_value) &&
274*7c478bd9Sstevel@tonic-gate 		    (error = secpolicy_contract_event(cr)) != 0)
275*7c478bd9Sstevel@tonic-gate 			return (error);
276*7c478bd9Sstevel@tonic-gate 		tmpl->ctmpl_ev_crit = param->ctpm_value;
277*7c478bd9Sstevel@tonic-gate 		break;
278*7c478bd9Sstevel@tonic-gate 	case CTPP_EV_FATAL:
279*7c478bd9Sstevel@tonic-gate 		if (param->ctpm_value & ~CT_PR_ALLFATAL)
280*7c478bd9Sstevel@tonic-gate 			return (EINVAL);
281*7c478bd9Sstevel@tonic-gate 		ctp->ctp_ev_fatal = param->ctpm_value;
282*7c478bd9Sstevel@tonic-gate 		/*
283*7c478bd9Sstevel@tonic-gate 		 * Check to see if an unprivileged process is
284*7c478bd9Sstevel@tonic-gate 		 * requesting that events be removed from the fatal
285*7c478bd9Sstevel@tonic-gate 		 * event set which are still in the critical event set.
286*7c478bd9Sstevel@tonic-gate 		 */
287*7c478bd9Sstevel@tonic-gate 		if (EXCESS(ctp, tmpl->ctmpl_ev_crit) &&
288*7c478bd9Sstevel@tonic-gate 		    !secpolicy_contract_event_choice(cr)) {
289*7c478bd9Sstevel@tonic-gate 			int allowed =
290*7c478bd9Sstevel@tonic-gate 			    SAFE_EV | (ctp->ctp_params & CT_PR_PGRPONLY) ?
291*7c478bd9Sstevel@tonic-gate 			    0 : ctp->ctp_ev_fatal;
292*7c478bd9Sstevel@tonic-gate 			tmpl->ctmpl_ev_info |= (tmpl->ctmpl_ev_crit & ~allowed);
293*7c478bd9Sstevel@tonic-gate 			tmpl->ctmpl_ev_crit &= allowed;
294*7c478bd9Sstevel@tonic-gate 		}
295*7c478bd9Sstevel@tonic-gate 		break;
296*7c478bd9Sstevel@tonic-gate 	default:
297*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
298*7c478bd9Sstevel@tonic-gate 	}
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 	return (0);
301*7c478bd9Sstevel@tonic-gate }
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate /*
304*7c478bd9Sstevel@tonic-gate  * ctmpl_process_get
305*7c478bd9Sstevel@tonic-gate  *
306*7c478bd9Sstevel@tonic-gate  * The process contract template get entry point.  Simply fetches and
307*7c478bd9Sstevel@tonic-gate  * returns the requested term.
308*7c478bd9Sstevel@tonic-gate  */
309*7c478bd9Sstevel@tonic-gate static int
310*7c478bd9Sstevel@tonic-gate ctmpl_process_get(struct ct_template *template, ct_param_t *param)
311*7c478bd9Sstevel@tonic-gate {
312*7c478bd9Sstevel@tonic-gate 	ctmpl_process_t *ctp = template->ctmpl_data;
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 	switch (param->ctpm_id) {
315*7c478bd9Sstevel@tonic-gate 	case CTPP_SUBSUME:
316*7c478bd9Sstevel@tonic-gate 		param->ctpm_value = ctp->ctp_subsume ?
317*7c478bd9Sstevel@tonic-gate 		    ctp->ctp_subsume->ct_id : 0;
318*7c478bd9Sstevel@tonic-gate 		break;
319*7c478bd9Sstevel@tonic-gate 	case CTPP_PARAMS:
320*7c478bd9Sstevel@tonic-gate 		param->ctpm_value = ctp->ctp_params;
321*7c478bd9Sstevel@tonic-gate 		break;
322*7c478bd9Sstevel@tonic-gate 	case CTPP_EV_FATAL:
323*7c478bd9Sstevel@tonic-gate 		param->ctpm_value = ctp->ctp_ev_fatal;
324*7c478bd9Sstevel@tonic-gate 		break;
325*7c478bd9Sstevel@tonic-gate 	default:
326*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
327*7c478bd9Sstevel@tonic-gate 	}
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 	return (0);
330*7c478bd9Sstevel@tonic-gate }
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate static ctmplops_t ctmpl_process_ops = {
333*7c478bd9Sstevel@tonic-gate 	ctmpl_process_dup,		/* ctop_dup */
334*7c478bd9Sstevel@tonic-gate 	ctmpl_process_free,		/* ctop_free */
335*7c478bd9Sstevel@tonic-gate 	ctmpl_process_set,		/* ctop_set */
336*7c478bd9Sstevel@tonic-gate 	ctmpl_process_get,		/* ctop_get */
337*7c478bd9Sstevel@tonic-gate 	ctmpl_create_inval,		/* ctop_create */
338*7c478bd9Sstevel@tonic-gate 	CT_PR_ALLEVENT
339*7c478bd9Sstevel@tonic-gate };
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate /*
343*7c478bd9Sstevel@tonic-gate  * Process contract implementation
344*7c478bd9Sstevel@tonic-gate  */
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate /*
347*7c478bd9Sstevel@tonic-gate  * ctmpl_process_default
348*7c478bd9Sstevel@tonic-gate  *
349*7c478bd9Sstevel@tonic-gate  * The process contract default template entry point.  Creates a
350*7c478bd9Sstevel@tonic-gate  * process contract template with no parameters set, with informative
351*7c478bd9Sstevel@tonic-gate  * core and signal events, critical empty and hwerr events, and fatal
352*7c478bd9Sstevel@tonic-gate  * hwerr events.
353*7c478bd9Sstevel@tonic-gate  */
354*7c478bd9Sstevel@tonic-gate static ct_template_t *
355*7c478bd9Sstevel@tonic-gate contract_process_default(void)
356*7c478bd9Sstevel@tonic-gate {
357*7c478bd9Sstevel@tonic-gate 	ctmpl_process_t *new;
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	new = kmem_alloc(sizeof (ctmpl_process_t), KM_SLEEP);
360*7c478bd9Sstevel@tonic-gate 	ctmpl_init(&new->ctp_ctmpl, &ctmpl_process_ops, process_type, new);
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 	new->ctp_subsume = NULL;
363*7c478bd9Sstevel@tonic-gate 	new->ctp_params = 0;
364*7c478bd9Sstevel@tonic-gate 	new->ctp_ctmpl.ctmpl_ev_info = CT_PR_EV_CORE | CT_PR_EV_SIGNAL;
365*7c478bd9Sstevel@tonic-gate 	new->ctp_ctmpl.ctmpl_ev_crit = CT_PR_EV_EMPTY | CT_PR_EV_HWERR;
366*7c478bd9Sstevel@tonic-gate 	new->ctp_ev_fatal = CT_PR_EV_HWERR;
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 	return (&new->ctp_ctmpl);
369*7c478bd9Sstevel@tonic-gate }
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate /*
372*7c478bd9Sstevel@tonic-gate  * contract_process_free
373*7c478bd9Sstevel@tonic-gate  *
374*7c478bd9Sstevel@tonic-gate  * The process contract free entry point.
375*7c478bd9Sstevel@tonic-gate  */
376*7c478bd9Sstevel@tonic-gate static void
377*7c478bd9Sstevel@tonic-gate contract_process_free(contract_t *ct)
378*7c478bd9Sstevel@tonic-gate {
379*7c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
380*7c478bd9Sstevel@tonic-gate 	crfree(ctp->conp_cred);
381*7c478bd9Sstevel@tonic-gate 	list_destroy(&ctp->conp_members);
382*7c478bd9Sstevel@tonic-gate 	list_destroy(&ctp->conp_inherited);
383*7c478bd9Sstevel@tonic-gate 	kmem_free(ctp, sizeof (cont_process_t));
384*7c478bd9Sstevel@tonic-gate }
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate /*
387*7c478bd9Sstevel@tonic-gate  * contract_process_cankill
388*7c478bd9Sstevel@tonic-gate  *
389*7c478bd9Sstevel@tonic-gate  * Determine if the contract author had or if the process generating
390*7c478bd9Sstevel@tonic-gate  * the event, sp, has adequate privileges to kill process tp.
391*7c478bd9Sstevel@tonic-gate  */
392*7c478bd9Sstevel@tonic-gate static int
393*7c478bd9Sstevel@tonic-gate contract_process_cankill(proc_t *tp, proc_t *sp, cont_process_t *ctp)
394*7c478bd9Sstevel@tonic-gate {
395*7c478bd9Sstevel@tonic-gate 	int cankill;
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tp->p_crlock);
398*7c478bd9Sstevel@tonic-gate 	cankill = hasprocperm(tp->p_cred, ctp->conp_cred);
399*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tp->p_crlock);
400*7c478bd9Sstevel@tonic-gate 	if (cankill || (sp && prochasprocperm(tp, sp, CRED())))
401*7c478bd9Sstevel@tonic-gate 		return (1);
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 	return (0);
404*7c478bd9Sstevel@tonic-gate }
405*7c478bd9Sstevel@tonic-gate 
406*7c478bd9Sstevel@tonic-gate /*
407*7c478bd9Sstevel@tonic-gate  * contract_process_kill
408*7c478bd9Sstevel@tonic-gate  *
409*7c478bd9Sstevel@tonic-gate  * Kills all processes in a contract, or all processes in the
410*7c478bd9Sstevel@tonic-gate  * intersection of a contract and ex's process group (if ex is non-NULL
411*7c478bd9Sstevel@tonic-gate  * and the contract's PGRPONLY parameter is set).  If checkpriv is
412*7c478bd9Sstevel@tonic-gate  * true, only those processes which may be signaled by the contract
413*7c478bd9Sstevel@tonic-gate  * author or ex are killed.
414*7c478bd9Sstevel@tonic-gate  */
415*7c478bd9Sstevel@tonic-gate static void
416*7c478bd9Sstevel@tonic-gate contract_process_kill(contract_t *ct, proc_t *ex, int checkpriv)
417*7c478bd9Sstevel@tonic-gate {
418*7c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
419*7c478bd9Sstevel@tonic-gate 	proc_t *p;
420*7c478bd9Sstevel@tonic-gate 	pid_t pgrp = -1;
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate 	if (ex && (ctp->conp_params & CT_PR_PGRPONLY)) {
425*7c478bd9Sstevel@tonic-gate 		pgrp = ex->p_pgrp;
426*7c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
427*7c478bd9Sstevel@tonic-gate 	}
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate 	for (p = list_head(&ctp->conp_members); p != NULL;
430*7c478bd9Sstevel@tonic-gate 	    p = list_next(&ctp->conp_members, p)) {
431*7c478bd9Sstevel@tonic-gate 		if ((p == ex) || (pgrp != -1 && p->p_pgrp != pgrp) ||
432*7c478bd9Sstevel@tonic-gate 		    (checkpriv && !contract_process_cankill(p, ex, ctp)))
433*7c478bd9Sstevel@tonic-gate 			continue;
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 		psignal(p, SIGKILL);
436*7c478bd9Sstevel@tonic-gate 	}
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate 	if (pgrp != -1)
439*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
440*7c478bd9Sstevel@tonic-gate }
441*7c478bd9Sstevel@tonic-gate 
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate /*
444*7c478bd9Sstevel@tonic-gate  * contract_process_accept
445*7c478bd9Sstevel@tonic-gate  *
446*7c478bd9Sstevel@tonic-gate  * Tests if the process contract is willing to act as a regent for
447*7c478bd9Sstevel@tonic-gate  * inherited contracts.  Though brief and only called from one place,
448*7c478bd9Sstevel@tonic-gate  * this functionality is kept here to avoid including knowledge of
449*7c478bd9Sstevel@tonic-gate  * process contract implementation in the generic contract code.
450*7c478bd9Sstevel@tonic-gate  */
451*7c478bd9Sstevel@tonic-gate int
452*7c478bd9Sstevel@tonic-gate contract_process_accept(contract_t *parent)
453*7c478bd9Sstevel@tonic-gate {
454*7c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = parent->ct_data;
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 	ASSERT(parent->ct_type == process_type);
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 	return (ctp->conp_params & CT_PR_REGENT);
459*7c478bd9Sstevel@tonic-gate }
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate /*
462*7c478bd9Sstevel@tonic-gate  * contract_process_take
463*7c478bd9Sstevel@tonic-gate  *
464*7c478bd9Sstevel@tonic-gate  * Executes the process contract side of inheriting a contract.
465*7c478bd9Sstevel@tonic-gate  */
466*7c478bd9Sstevel@tonic-gate void
467*7c478bd9Sstevel@tonic-gate contract_process_take(contract_t *parent, contract_t *child)
468*7c478bd9Sstevel@tonic-gate {
469*7c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = parent->ct_data;
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&parent->ct_lock));
472*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&child->ct_lock));
473*7c478bd9Sstevel@tonic-gate 	ASSERT(parent->ct_type == process_type);
474*7c478bd9Sstevel@tonic-gate 	ASSERT(ctp->conp_params & CT_PR_REGENT);
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate 	list_insert_head(&ctp->conp_inherited, child);
477*7c478bd9Sstevel@tonic-gate 	ctp->conp_ninherited++;
478*7c478bd9Sstevel@tonic-gate }
479*7c478bd9Sstevel@tonic-gate 
480*7c478bd9Sstevel@tonic-gate /*
481*7c478bd9Sstevel@tonic-gate  * contract_process_adopt
482*7c478bd9Sstevel@tonic-gate  *
483*7c478bd9Sstevel@tonic-gate  * Executes the process contract side of adopting a contract.
484*7c478bd9Sstevel@tonic-gate  */
485*7c478bd9Sstevel@tonic-gate void
486*7c478bd9Sstevel@tonic-gate contract_process_adopt(contract_t *ct, proc_t *p)
487*7c478bd9Sstevel@tonic-gate {
488*7c478bd9Sstevel@tonic-gate 	cont_process_t *parent = p->p_ct_process;
489*7c478bd9Sstevel@tonic-gate 
490*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&parent->conp_contract.ct_lock));
491*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 	list_remove(&parent->conp_inherited, ct);
494*7c478bd9Sstevel@tonic-gate 	parent->conp_ninherited--;
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate 	/*
497*7c478bd9Sstevel@tonic-gate 	 * We drop the parent lock first because a) we are passing the
498*7c478bd9Sstevel@tonic-gate 	 * contract reference to the child, and b) contract_adopt
499*7c478bd9Sstevel@tonic-gate 	 * expects us to return with the contract lock held.
500*7c478bd9Sstevel@tonic-gate 	 */
501*7c478bd9Sstevel@tonic-gate 	mutex_exit(&parent->conp_contract.ct_lock);
502*7c478bd9Sstevel@tonic-gate }
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate /*
505*7c478bd9Sstevel@tonic-gate  * contract_process_status
506*7c478bd9Sstevel@tonic-gate  *
507*7c478bd9Sstevel@tonic-gate  * The process contract abandon entry point.
508*7c478bd9Sstevel@tonic-gate  */
509*7c478bd9Sstevel@tonic-gate static void
510*7c478bd9Sstevel@tonic-gate contract_process_abandon(contract_t *ct)
511*7c478bd9Sstevel@tonic-gate {
512*7c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
515*7c478bd9Sstevel@tonic-gate 
516*7c478bd9Sstevel@tonic-gate 	/*
517*7c478bd9Sstevel@tonic-gate 	 * Shall we stay or shall we go?
518*7c478bd9Sstevel@tonic-gate 	 */
519*7c478bd9Sstevel@tonic-gate 	if (list_head(&ctp->conp_members) == NULL) {
520*7c478bd9Sstevel@tonic-gate 		contract_destroy(ct);
521*7c478bd9Sstevel@tonic-gate 	} else {
522*7c478bd9Sstevel@tonic-gate 		/*
523*7c478bd9Sstevel@tonic-gate 		 * Strictly speaking, we actually do orphan the contract.
524*7c478bd9Sstevel@tonic-gate 		 * Assuming our credentials allow us to kill all
525*7c478bd9Sstevel@tonic-gate 		 * processes in the contract, this is only temporary.
526*7c478bd9Sstevel@tonic-gate 		 */
527*7c478bd9Sstevel@tonic-gate 		if (ctp->conp_params & CT_PR_NOORPHAN)
528*7c478bd9Sstevel@tonic-gate 			contract_process_kill(ct, NULL, B_TRUE);
529*7c478bd9Sstevel@tonic-gate 		contract_orphan(ct);
530*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
531*7c478bd9Sstevel@tonic-gate 		contract_rele(ct);
532*7c478bd9Sstevel@tonic-gate 	}
533*7c478bd9Sstevel@tonic-gate }
534*7c478bd9Sstevel@tonic-gate 
535*7c478bd9Sstevel@tonic-gate /*
536*7c478bd9Sstevel@tonic-gate  * contract_process_destroy
537*7c478bd9Sstevel@tonic-gate  *
538*7c478bd9Sstevel@tonic-gate  * The process contract destroy entry point.
539*7c478bd9Sstevel@tonic-gate  */
540*7c478bd9Sstevel@tonic-gate static void
541*7c478bd9Sstevel@tonic-gate contract_process_destroy(contract_t *ct)
542*7c478bd9Sstevel@tonic-gate {
543*7c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
544*7c478bd9Sstevel@tonic-gate 	contract_t *cct;
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ct->ct_lock));
547*7c478bd9Sstevel@tonic-gate 
548*7c478bd9Sstevel@tonic-gate 	/*
549*7c478bd9Sstevel@tonic-gate 	 * contract_destroy all empty children, kill or orphan the rest
550*7c478bd9Sstevel@tonic-gate 	 */
551*7c478bd9Sstevel@tonic-gate 	while (cct = list_head(&ctp->conp_inherited)) {
552*7c478bd9Sstevel@tonic-gate 		mutex_enter(&cct->ct_lock);
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate 		ASSERT(cct->ct_state == CTS_INHERITED);
555*7c478bd9Sstevel@tonic-gate 
556*7c478bd9Sstevel@tonic-gate 		list_remove(&ctp->conp_inherited, cct);
557*7c478bd9Sstevel@tonic-gate 		ctp->conp_ninherited--;
558*7c478bd9Sstevel@tonic-gate 		cct->ct_regent = NULL;
559*7c478bd9Sstevel@tonic-gate 		cct->ct_type->ct_type_ops->contop_abandon(cct);
560*7c478bd9Sstevel@tonic-gate 	}
561*7c478bd9Sstevel@tonic-gate }
562*7c478bd9Sstevel@tonic-gate 
563*7c478bd9Sstevel@tonic-gate /*
564*7c478bd9Sstevel@tonic-gate  * contract_process_status
565*7c478bd9Sstevel@tonic-gate  *
566*7c478bd9Sstevel@tonic-gate  * The process contract status entry point.
567*7c478bd9Sstevel@tonic-gate  */
568*7c478bd9Sstevel@tonic-gate static void
569*7c478bd9Sstevel@tonic-gate contract_process_status(contract_t *ct, zone_t *zone, int detail, nvlist_t *nvl,
570*7c478bd9Sstevel@tonic-gate     void *status, model_t model)
571*7c478bd9Sstevel@tonic-gate {
572*7c478bd9Sstevel@tonic-gate 	cont_process_t *ctp = ct->ct_data;
573*7c478bd9Sstevel@tonic-gate 	uint32_t *pids, *ctids;
574*7c478bd9Sstevel@tonic-gate 	uint_t npids, nctids;
575*7c478bd9Sstevel@tonic-gate 	uint_t spids, sctids;
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate 	if (detail == CTD_FIXED) {
578*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
579*7c478bd9Sstevel@tonic-gate 		contract_status_common(ct, zone, status, model);
580*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
581*7c478bd9Sstevel@tonic-gate 	} else {
582*7c478bd9Sstevel@tonic-gate 		contract_t *cnext;
583*7c478bd9Sstevel@tonic-gate 		proc_t *pnext;
584*7c478bd9Sstevel@tonic-gate 		uint_t loc;
585*7c478bd9Sstevel@tonic-gate 
586*7c478bd9Sstevel@tonic-gate 		ASSERT(detail == CTD_ALL);
587*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
588*7c478bd9Sstevel@tonic-gate 		for (;;) {
589*7c478bd9Sstevel@tonic-gate 			spids = ctp->conp_nmembers + 5;
590*7c478bd9Sstevel@tonic-gate 			sctids = ctp->conp_ninherited + 5;
591*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ct->ct_lock);
592*7c478bd9Sstevel@tonic-gate 
593*7c478bd9Sstevel@tonic-gate 			pids = kmem_alloc(spids * sizeof (uint32_t), KM_SLEEP);
594*7c478bd9Sstevel@tonic-gate 			ctids = kmem_alloc(sctids * sizeof (uint32_t),
595*7c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
596*7c478bd9Sstevel@tonic-gate 
597*7c478bd9Sstevel@tonic-gate 			mutex_enter(&ct->ct_lock);
598*7c478bd9Sstevel@tonic-gate 			npids = ctp->conp_nmembers;
599*7c478bd9Sstevel@tonic-gate 			nctids = ctp->conp_ninherited;
600*7c478bd9Sstevel@tonic-gate 			if (spids >= npids && sctids >= nctids)
601*7c478bd9Sstevel@tonic-gate 				break;
602*7c478bd9Sstevel@tonic-gate 
603*7c478bd9Sstevel@tonic-gate 			kmem_free(pids, spids * sizeof (uint32_t));
604*7c478bd9Sstevel@tonic-gate 			kmem_free(ctids, sctids * sizeof (uint32_t));
605*7c478bd9Sstevel@tonic-gate 		}
606*7c478bd9Sstevel@tonic-gate 		contract_status_common(ct, zone, status, model);
607*7c478bd9Sstevel@tonic-gate 		for (loc = 0, cnext = list_head(&ctp->conp_inherited); cnext;
608*7c478bd9Sstevel@tonic-gate 		    cnext = list_next(&ctp->conp_inherited, cnext))
609*7c478bd9Sstevel@tonic-gate 			ctids[loc++] = cnext->ct_id;
610*7c478bd9Sstevel@tonic-gate 		ASSERT(loc == nctids);
611*7c478bd9Sstevel@tonic-gate 		for (loc = 0, pnext = list_head(&ctp->conp_members); pnext;
612*7c478bd9Sstevel@tonic-gate 		    pnext = list_next(&ctp->conp_members, pnext))
613*7c478bd9Sstevel@tonic-gate 			pids[loc++] = pnext->p_pid;
614*7c478bd9Sstevel@tonic-gate 		ASSERT(loc == npids);
615*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
616*7c478bd9Sstevel@tonic-gate 
617*7c478bd9Sstevel@tonic-gate 	}
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 	/*
620*7c478bd9Sstevel@tonic-gate 	 * Contract terms are static; there's no need to hold the
621*7c478bd9Sstevel@tonic-gate 	 * contract lock while accessing them.
622*7c478bd9Sstevel@tonic-gate 	 */
623*7c478bd9Sstevel@tonic-gate 	VERIFY(nvlist_add_uint32(nvl, CTPS_PARAMS, ctp->conp_params) == 0);
624*7c478bd9Sstevel@tonic-gate 	VERIFY(nvlist_add_uint32(nvl, CTPS_EV_FATAL, ctp->conp_ev_fatal) == 0);
625*7c478bd9Sstevel@tonic-gate 	if (detail == CTD_ALL) {
626*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32_array(nvl, CTPS_MEMBERS, pids,
627*7c478bd9Sstevel@tonic-gate 		    npids) == 0);
628*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32_array(nvl, CTPS_CONTRACTS, ctids,
629*7c478bd9Sstevel@tonic-gate 		    nctids) == 0);
630*7c478bd9Sstevel@tonic-gate 		kmem_free(pids, spids * sizeof (uint32_t));
631*7c478bd9Sstevel@tonic-gate 		kmem_free(ctids, sctids * sizeof (uint32_t));
632*7c478bd9Sstevel@tonic-gate 	}
633*7c478bd9Sstevel@tonic-gate }
634*7c478bd9Sstevel@tonic-gate 
635*7c478bd9Sstevel@tonic-gate static contops_t contract_process_ops = {
636*7c478bd9Sstevel@tonic-gate 	contract_process_free,		/* contop_free */
637*7c478bd9Sstevel@tonic-gate 	contract_process_abandon,	/* contop_abandon */
638*7c478bd9Sstevel@tonic-gate 	contract_process_destroy,	/* contop_destroy */
639*7c478bd9Sstevel@tonic-gate 	contract_process_status		/* contop_status */
640*7c478bd9Sstevel@tonic-gate };
641*7c478bd9Sstevel@tonic-gate 
642*7c478bd9Sstevel@tonic-gate /*
643*7c478bd9Sstevel@tonic-gate  * contract_process_init
644*7c478bd9Sstevel@tonic-gate  *
645*7c478bd9Sstevel@tonic-gate  * Initializes the process contract type.  Also creates a template for
646*7c478bd9Sstevel@tonic-gate  * use by newproc() when it creates user processes.
647*7c478bd9Sstevel@tonic-gate  */
648*7c478bd9Sstevel@tonic-gate void
649*7c478bd9Sstevel@tonic-gate contract_process_init(void)
650*7c478bd9Sstevel@tonic-gate {
651*7c478bd9Sstevel@tonic-gate 	process_type = contract_type_init(CTT_PROCESS, "process",
652*7c478bd9Sstevel@tonic-gate 	    &contract_process_ops, contract_process_default);
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate 	/*
655*7c478bd9Sstevel@tonic-gate 	 * Create a template for use with init(1M) and other
656*7c478bd9Sstevel@tonic-gate 	 * kernel-started processes.
657*7c478bd9Sstevel@tonic-gate 	 */
658*7c478bd9Sstevel@tonic-gate 	sys_process_tmpl = kmem_alloc(sizeof (ctmpl_process_t), KM_SLEEP);
659*7c478bd9Sstevel@tonic-gate 	ctmpl_init(&sys_process_tmpl->ctp_ctmpl, &ctmpl_process_ops,
660*7c478bd9Sstevel@tonic-gate 	    process_type, sys_process_tmpl);
661*7c478bd9Sstevel@tonic-gate 	sys_process_tmpl->ctp_subsume = NULL;
662*7c478bd9Sstevel@tonic-gate 	sys_process_tmpl->ctp_params = CT_PR_NOORPHAN;
663*7c478bd9Sstevel@tonic-gate 	sys_process_tmpl->ctp_ev_fatal = CT_PR_EV_HWERR;
664*7c478bd9Sstevel@tonic-gate }
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate /*
667*7c478bd9Sstevel@tonic-gate  * contract_process_create
668*7c478bd9Sstevel@tonic-gate  *
669*7c478bd9Sstevel@tonic-gate  * create a process contract given template "tmpl" and parent process
670*7c478bd9Sstevel@tonic-gate  * "parent".  May fail and return NULL if project.max-contracts would
671*7c478bd9Sstevel@tonic-gate  * have been exceeded.
672*7c478bd9Sstevel@tonic-gate  */
673*7c478bd9Sstevel@tonic-gate static cont_process_t *
674*7c478bd9Sstevel@tonic-gate contract_process_create(ctmpl_process_t *tmpl, proc_t *parent, int canfail)
675*7c478bd9Sstevel@tonic-gate {
676*7c478bd9Sstevel@tonic-gate 	cont_process_t *ctp;
677*7c478bd9Sstevel@tonic-gate 
678*7c478bd9Sstevel@tonic-gate 	ASSERT(tmpl != NULL);
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate 	(void) contract_type_pbundle(process_type, parent);
681*7c478bd9Sstevel@tonic-gate 
682*7c478bd9Sstevel@tonic-gate 	ctp = kmem_zalloc(sizeof (cont_process_t), KM_SLEEP);
683*7c478bd9Sstevel@tonic-gate 
684*7c478bd9Sstevel@tonic-gate 	list_create(&ctp->conp_members, sizeof (proc_t),
685*7c478bd9Sstevel@tonic-gate 	    offsetof(proc_t, p_ct_member));
686*7c478bd9Sstevel@tonic-gate 	list_create(&ctp->conp_inherited, sizeof (contract_t),
687*7c478bd9Sstevel@tonic-gate 	    offsetof(contract_t, ct_ctlist));
688*7c478bd9Sstevel@tonic-gate 	mutex_enter(&tmpl->ctp_ctmpl.ctmpl_lock);
689*7c478bd9Sstevel@tonic-gate 	ctp->conp_params = tmpl->ctp_params;
690*7c478bd9Sstevel@tonic-gate 	ctp->conp_ev_fatal = tmpl->ctp_ev_fatal;
691*7c478bd9Sstevel@tonic-gate 	crhold(ctp->conp_cred = CRED());
692*7c478bd9Sstevel@tonic-gate 
693*7c478bd9Sstevel@tonic-gate 	if (contract_ctor(&ctp->conp_contract, process_type, &tmpl->ctp_ctmpl,
694*7c478bd9Sstevel@tonic-gate 	    ctp, (ctp->conp_params & CT_PR_INHERIT) ? CTF_INHERIT : 0,
695*7c478bd9Sstevel@tonic-gate 	    parent, canfail)) {
696*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tmpl->ctp_ctmpl.ctmpl_lock);
697*7c478bd9Sstevel@tonic-gate 		contract_process_free(&ctp->conp_contract);
698*7c478bd9Sstevel@tonic-gate 		return (NULL);
699*7c478bd9Sstevel@tonic-gate 	}
700*7c478bd9Sstevel@tonic-gate 
701*7c478bd9Sstevel@tonic-gate 	/*
702*7c478bd9Sstevel@tonic-gate 	 * Transfer subcontracts only after new contract is visible.
703*7c478bd9Sstevel@tonic-gate 	 * Also, only transfer contracts if the parent matches -- we
704*7c478bd9Sstevel@tonic-gate 	 * don't want to create a cycle in the tree of contracts.
705*7c478bd9Sstevel@tonic-gate 	 */
706*7c478bd9Sstevel@tonic-gate 	if (tmpl->ctp_subsume && tmpl->ctp_subsume->ct_owner == parent) {
707*7c478bd9Sstevel@tonic-gate 		cont_process_t *sct = tmpl->ctp_subsume->ct_data;
708*7c478bd9Sstevel@tonic-gate 		contract_t *ct;
709*7c478bd9Sstevel@tonic-gate 
710*7c478bd9Sstevel@tonic-gate 		mutex_enter(&tmpl->ctp_subsume->ct_lock);
711*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ctp->conp_contract.ct_lock);
712*7c478bd9Sstevel@tonic-gate 		while (ct = list_head(&sct->conp_inherited)) {
713*7c478bd9Sstevel@tonic-gate 			mutex_enter(&ct->ct_lock);
714*7c478bd9Sstevel@tonic-gate 			list_remove(&sct->conp_inherited, ct);
715*7c478bd9Sstevel@tonic-gate 			list_insert_tail(&ctp->conp_inherited, ct);
716*7c478bd9Sstevel@tonic-gate 			ct->ct_regent = &ctp->conp_contract;
717*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ct->ct_lock);
718*7c478bd9Sstevel@tonic-gate 		}
719*7c478bd9Sstevel@tonic-gate 		ctp->conp_ninherited += sct->conp_ninherited;
720*7c478bd9Sstevel@tonic-gate 		sct->conp_ninherited = 0;
721*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ctp->conp_contract.ct_lock);
722*7c478bd9Sstevel@tonic-gate 		mutex_exit(&tmpl->ctp_subsume->ct_lock);
723*7c478bd9Sstevel@tonic-gate 
724*7c478bd9Sstevel@tonic-gate 		/*
725*7c478bd9Sstevel@tonic-gate 		 * Automatically abandon the contract.
726*7c478bd9Sstevel@tonic-gate 		 */
727*7c478bd9Sstevel@tonic-gate 		(void) contract_abandon(tmpl->ctp_subsume, parent, 1);
728*7c478bd9Sstevel@tonic-gate 	}
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate 	mutex_exit(&tmpl->ctp_ctmpl.ctmpl_lock);
731*7c478bd9Sstevel@tonic-gate 
732*7c478bd9Sstevel@tonic-gate 	return (ctp);
733*7c478bd9Sstevel@tonic-gate }
734*7c478bd9Sstevel@tonic-gate 
735*7c478bd9Sstevel@tonic-gate /*
736*7c478bd9Sstevel@tonic-gate  * contract_process_exit
737*7c478bd9Sstevel@tonic-gate  *
738*7c478bd9Sstevel@tonic-gate  * Called on process exit.  Removes process p from process contract
739*7c478bd9Sstevel@tonic-gate  * ctp.  Generates an exit event, if requested.  Generates an empty
740*7c478bd9Sstevel@tonic-gate  * event, if p is the last member of the the process contract and empty
741*7c478bd9Sstevel@tonic-gate  * events were requested.
742*7c478bd9Sstevel@tonic-gate  */
743*7c478bd9Sstevel@tonic-gate void
744*7c478bd9Sstevel@tonic-gate contract_process_exit(cont_process_t *ctp, proc_t *p, int exitstatus)
745*7c478bd9Sstevel@tonic-gate {
746*7c478bd9Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
747*7c478bd9Sstevel@tonic-gate 	ct_kevent_t *event;
748*7c478bd9Sstevel@tonic-gate 	int empty;
749*7c478bd9Sstevel@tonic-gate 
750*7c478bd9Sstevel@tonic-gate 	/*
751*7c478bd9Sstevel@tonic-gate 	 * Remove self from process contract.
752*7c478bd9Sstevel@tonic-gate 	 */
753*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
754*7c478bd9Sstevel@tonic-gate 	list_remove(&ctp->conp_members, p);
755*7c478bd9Sstevel@tonic-gate 	ctp->conp_nmembers--;
756*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);	/* in case /proc is watching */
757*7c478bd9Sstevel@tonic-gate 	p->p_ct_process = NULL;
758*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
759*7c478bd9Sstevel@tonic-gate 
760*7c478bd9Sstevel@tonic-gate 	/*
761*7c478bd9Sstevel@tonic-gate 	 * We check for emptiness before dropping the contract lock to
762*7c478bd9Sstevel@tonic-gate 	 * send the exit event, otherwise we could end up with two
763*7c478bd9Sstevel@tonic-gate 	 * empty events.
764*7c478bd9Sstevel@tonic-gate 	 */
765*7c478bd9Sstevel@tonic-gate 	empty = (list_head(&ctp->conp_members) == NULL);
766*7c478bd9Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_EXIT)) {
767*7c478bd9Sstevel@tonic-gate 		nvlist_t *nvl;
768*7c478bd9Sstevel@tonic-gate 
769*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
770*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
771*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
772*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_int32(nvl, CTPE_EXITSTATUS, exitstatus) == 0);
773*7c478bd9Sstevel@tonic-gate 
774*7c478bd9Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
775*7c478bd9Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_EXIT) ? CTE_INFO : 0;
776*7c478bd9Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_EXIT;
777*7c478bd9Sstevel@tonic-gate 		cte_publish_all(ct, event, nvl, NULL);
778*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
779*7c478bd9Sstevel@tonic-gate 	}
780*7c478bd9Sstevel@tonic-gate 	if (empty) {
781*7c478bd9Sstevel@tonic-gate 		/*
782*7c478bd9Sstevel@tonic-gate 		 * Send EMPTY message.
783*7c478bd9Sstevel@tonic-gate 		 */
784*7c478bd9Sstevel@tonic-gate 		if (EVSENDP(ctp, CT_PR_EV_EMPTY)) {
785*7c478bd9Sstevel@tonic-gate 			nvlist_t *nvl;
786*7c478bd9Sstevel@tonic-gate 
787*7c478bd9Sstevel@tonic-gate 			mutex_exit(&ct->ct_lock);
788*7c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME,
789*7c478bd9Sstevel@tonic-gate 			    KM_SLEEP) == 0);
790*7c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
791*7c478bd9Sstevel@tonic-gate 
792*7c478bd9Sstevel@tonic-gate 			event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
793*7c478bd9Sstevel@tonic-gate 			event->cte_flags = EVINFOP(ctp, CT_PR_EV_EMPTY) ?
794*7c478bd9Sstevel@tonic-gate 			    CTE_INFO : 0;
795*7c478bd9Sstevel@tonic-gate 			event->cte_type = CT_PR_EV_EMPTY;
796*7c478bd9Sstevel@tonic-gate 			cte_publish_all(ct, event, nvl, NULL);
797*7c478bd9Sstevel@tonic-gate 			mutex_enter(&ct->ct_lock);
798*7c478bd9Sstevel@tonic-gate 		}
799*7c478bd9Sstevel@tonic-gate 
800*7c478bd9Sstevel@tonic-gate 		/*
801*7c478bd9Sstevel@tonic-gate 		 * The last one to leave an orphaned contract turns out
802*7c478bd9Sstevel@tonic-gate 		 * the lights.
803*7c478bd9Sstevel@tonic-gate 		 */
804*7c478bd9Sstevel@tonic-gate 		if (ct->ct_state == CTS_ORPHAN) {
805*7c478bd9Sstevel@tonic-gate 			contract_destroy(ct);
806*7c478bd9Sstevel@tonic-gate 			return;
807*7c478bd9Sstevel@tonic-gate 		}
808*7c478bd9Sstevel@tonic-gate 	}
809*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
810*7c478bd9Sstevel@tonic-gate 	contract_rele(ct);
811*7c478bd9Sstevel@tonic-gate }
812*7c478bd9Sstevel@tonic-gate 
813*7c478bd9Sstevel@tonic-gate /*
814*7c478bd9Sstevel@tonic-gate  * contract_process_fork
815*7c478bd9Sstevel@tonic-gate  *
816*7c478bd9Sstevel@tonic-gate  * Called on process fork.  If the current lwp has a active process
817*7c478bd9Sstevel@tonic-gate  * contract template, we attempt to create a new process contract.
818*7c478bd9Sstevel@tonic-gate  * Failure to create a process contract when required is a failure in
819*7c478bd9Sstevel@tonic-gate  * fork so, in such an event, we return NULL.
820*7c478bd9Sstevel@tonic-gate  *
821*7c478bd9Sstevel@tonic-gate  * Assuming we succeeded or skipped the previous step, we add the child
822*7c478bd9Sstevel@tonic-gate  * process to the new contract (success) or to the parent's process
823*7c478bd9Sstevel@tonic-gate  * contract (skip).  If requested, we also send a fork event to that
824*7c478bd9Sstevel@tonic-gate  * contract.
825*7c478bd9Sstevel@tonic-gate  *
826*7c478bd9Sstevel@tonic-gate  * Because contract_process_fork() may fail, and because we would
827*7c478bd9Sstevel@tonic-gate  * prefer that process contracts not be created for processes which
828*7c478bd9Sstevel@tonic-gate  * don't complete forking, this should be the last function called
829*7c478bd9Sstevel@tonic-gate  * before the "all clear" point in cfork.
830*7c478bd9Sstevel@tonic-gate  */
831*7c478bd9Sstevel@tonic-gate cont_process_t *
832*7c478bd9Sstevel@tonic-gate contract_process_fork(ctmpl_process_t *rtmpl, proc_t *cp, proc_t *pp,
833*7c478bd9Sstevel@tonic-gate     int canfail)
834*7c478bd9Sstevel@tonic-gate {
835*7c478bd9Sstevel@tonic-gate 	contract_t *ct;
836*7c478bd9Sstevel@tonic-gate 	cont_process_t *ctp;
837*7c478bd9Sstevel@tonic-gate 	ct_kevent_t *event;
838*7c478bd9Sstevel@tonic-gate 	ct_template_t *tmpl;
839*7c478bd9Sstevel@tonic-gate 
840*7c478bd9Sstevel@tonic-gate 	if (rtmpl == NULL && (tmpl = ttolwp(curthread)->lwp_ct_active[
841*7c478bd9Sstevel@tonic-gate 	    process_type->ct_type_index]) != NULL)
842*7c478bd9Sstevel@tonic-gate 		rtmpl = tmpl->ctmpl_data;
843*7c478bd9Sstevel@tonic-gate 
844*7c478bd9Sstevel@tonic-gate 	if (rtmpl == NULL)
845*7c478bd9Sstevel@tonic-gate 		ctp = curproc->p_ct_process;
846*7c478bd9Sstevel@tonic-gate 	else if ((ctp = contract_process_create(rtmpl, pp, canfail)) == NULL)
847*7c478bd9Sstevel@tonic-gate 		return (NULL);
848*7c478bd9Sstevel@tonic-gate 
849*7c478bd9Sstevel@tonic-gate 	ct = &ctp->conp_contract;
850*7c478bd9Sstevel@tonic-gate 	/*
851*7c478bd9Sstevel@tonic-gate 	 * Prevent contract_process_kill() from missing forked children
852*7c478bd9Sstevel@tonic-gate 	 * by failing forks by parents that have just been killed.
853*7c478bd9Sstevel@tonic-gate 	 * It's not worth hoisting the ctp test since contract creation
854*7c478bd9Sstevel@tonic-gate 	 * is by no means the common case.
855*7c478bd9Sstevel@tonic-gate 	 */
856*7c478bd9Sstevel@tonic-gate 	mutex_enter(&ct->ct_lock);
857*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_lock);
858*7c478bd9Sstevel@tonic-gate 	if (ctp == curproc->p_ct_process && (pp->p_flag & SKILLED) != 0 &&
859*7c478bd9Sstevel@tonic-gate 	    canfail) {
860*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pp->p_lock);
861*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
862*7c478bd9Sstevel@tonic-gate 		return (NULL);
863*7c478bd9Sstevel@tonic-gate 	}
864*7c478bd9Sstevel@tonic-gate 	cp->p_ct_process = ctp;
865*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_lock);
866*7c478bd9Sstevel@tonic-gate 	contract_hold(ct);
867*7c478bd9Sstevel@tonic-gate 	list_insert_head(&ctp->conp_members, cp);
868*7c478bd9Sstevel@tonic-gate 	ctp->conp_nmembers++;
869*7c478bd9Sstevel@tonic-gate 	mutex_exit(&ct->ct_lock);
870*7c478bd9Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_FORK)) {
871*7c478bd9Sstevel@tonic-gate 		nvlist_t *nvl;
872*7c478bd9Sstevel@tonic-gate 
873*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
874*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, cp->p_pid) == 0);
875*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PPID, pp->p_pid) == 0);
876*7c478bd9Sstevel@tonic-gate 
877*7c478bd9Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
878*7c478bd9Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_FORK) ? CTE_INFO : 0;
879*7c478bd9Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_FORK;
880*7c478bd9Sstevel@tonic-gate 		cte_publish_all(ct, event, nvl, NULL);
881*7c478bd9Sstevel@tonic-gate 	}
882*7c478bd9Sstevel@tonic-gate 	return (ctp);
883*7c478bd9Sstevel@tonic-gate }
884*7c478bd9Sstevel@tonic-gate 
885*7c478bd9Sstevel@tonic-gate /*
886*7c478bd9Sstevel@tonic-gate  * contract_process_core
887*7c478bd9Sstevel@tonic-gate  *
888*7c478bd9Sstevel@tonic-gate  * Called on core file generation attempts.  Generates a core event, if
889*7c478bd9Sstevel@tonic-gate  * requested, containing the names of the process, global, and
890*7c478bd9Sstevel@tonic-gate  * system-global ("zone") core files.  If dumping core is in the fatal
891*7c478bd9Sstevel@tonic-gate  * event set, calls contract_process_kill().
892*7c478bd9Sstevel@tonic-gate  */
893*7c478bd9Sstevel@tonic-gate void
894*7c478bd9Sstevel@tonic-gate contract_process_core(cont_process_t *ctp, proc_t *p, int sig,
895*7c478bd9Sstevel@tonic-gate     const char *process, const char *global, const char *zone)
896*7c478bd9Sstevel@tonic-gate {
897*7c478bd9Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
898*7c478bd9Sstevel@tonic-gate 
899*7c478bd9Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_CORE)) {
900*7c478bd9Sstevel@tonic-gate 		ct_kevent_t *event;
901*7c478bd9Sstevel@tonic-gate 		nvlist_t *nvl, *gnvl = NULL;
902*7c478bd9Sstevel@tonic-gate 
903*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
904*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
905*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_SIGNAL, sig) == 0);
906*7c478bd9Sstevel@tonic-gate 		if (process)
907*7c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_string(nvl, CTPE_PCOREFILE,
908*7c478bd9Sstevel@tonic-gate 			    (char *)process) == 0);
909*7c478bd9Sstevel@tonic-gate 		if (global)
910*7c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_string(nvl, CTPE_GCOREFILE,
911*7c478bd9Sstevel@tonic-gate 			    (char *)global) == 0);
912*7c478bd9Sstevel@tonic-gate 
913*7c478bd9Sstevel@tonic-gate 		if (zone) {
914*7c478bd9Sstevel@tonic-gate 			/*
915*7c478bd9Sstevel@tonic-gate 			 * Only the global zone is informed of the
916*7c478bd9Sstevel@tonic-gate 			 * local-zone generated global-zone core.
917*7c478bd9Sstevel@tonic-gate 			 */
918*7c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_alloc(&gnvl, NV_UNIQUE_NAME,
919*7c478bd9Sstevel@tonic-gate 			    KM_SLEEP) == 0);
920*7c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_string(gnvl, CTPE_ZCOREFILE,
921*7c478bd9Sstevel@tonic-gate 			    (char *)zone) == 0);
922*7c478bd9Sstevel@tonic-gate 		}
923*7c478bd9Sstevel@tonic-gate 
924*7c478bd9Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
925*7c478bd9Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_CORE) ? CTE_INFO : 0;
926*7c478bd9Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_CORE;
927*7c478bd9Sstevel@tonic-gate 		cte_publish_all(ct, event, nvl, gnvl);
928*7c478bd9Sstevel@tonic-gate 	}
929*7c478bd9Sstevel@tonic-gate 
930*7c478bd9Sstevel@tonic-gate 	if (EVFATALP(ctp, CT_PR_EV_CORE)) {
931*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
932*7c478bd9Sstevel@tonic-gate 		contract_process_kill(ct, p, B_TRUE);
933*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
934*7c478bd9Sstevel@tonic-gate 	}
935*7c478bd9Sstevel@tonic-gate }
936*7c478bd9Sstevel@tonic-gate 
937*7c478bd9Sstevel@tonic-gate /*
938*7c478bd9Sstevel@tonic-gate  * contract_process_hwerr
939*7c478bd9Sstevel@tonic-gate  *
940*7c478bd9Sstevel@tonic-gate  * Called when a process is killed by an unrecoverable hardware error.
941*7c478bd9Sstevel@tonic-gate  * Generates an hwerr event, if requested.  If hardware errors are in
942*7c478bd9Sstevel@tonic-gate  * the fatal event set, calls contract_process_kill().
943*7c478bd9Sstevel@tonic-gate  */
944*7c478bd9Sstevel@tonic-gate void
945*7c478bd9Sstevel@tonic-gate contract_process_hwerr(cont_process_t *ctp, proc_t *p)
946*7c478bd9Sstevel@tonic-gate {
947*7c478bd9Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
948*7c478bd9Sstevel@tonic-gate 
949*7c478bd9Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_HWERR)) {
950*7c478bd9Sstevel@tonic-gate 		ct_kevent_t *event;
951*7c478bd9Sstevel@tonic-gate 		nvlist_t *nvl;
952*7c478bd9Sstevel@tonic-gate 
953*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
954*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
955*7c478bd9Sstevel@tonic-gate 
956*7c478bd9Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
957*7c478bd9Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_HWERR) ? CTE_INFO : 0;
958*7c478bd9Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_HWERR;
959*7c478bd9Sstevel@tonic-gate 		cte_publish_all(ct, event, nvl, NULL);
960*7c478bd9Sstevel@tonic-gate 	}
961*7c478bd9Sstevel@tonic-gate 
962*7c478bd9Sstevel@tonic-gate 	if (EVFATALP(ctp, CT_PR_EV_HWERR)) {
963*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
964*7c478bd9Sstevel@tonic-gate 		contract_process_kill(ct, p, B_FALSE);
965*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
966*7c478bd9Sstevel@tonic-gate 	}
967*7c478bd9Sstevel@tonic-gate }
968*7c478bd9Sstevel@tonic-gate 
969*7c478bd9Sstevel@tonic-gate /*
970*7c478bd9Sstevel@tonic-gate  * contract_process_sig
971*7c478bd9Sstevel@tonic-gate  *
972*7c478bd9Sstevel@tonic-gate  * Called when a process is killed by a signal originating from a
973*7c478bd9Sstevel@tonic-gate  * process outside of its process contract or its process contract's
974*7c478bd9Sstevel@tonic-gate  * holder.  Generates an signal event, if requested, containing the
975*7c478bd9Sstevel@tonic-gate  * signal number, and the sender's pid and contract id (if available).
976*7c478bd9Sstevel@tonic-gate  * If signals are in the fatal event set, calls
977*7c478bd9Sstevel@tonic-gate  * contract_process_kill().
978*7c478bd9Sstevel@tonic-gate  */
979*7c478bd9Sstevel@tonic-gate void
980*7c478bd9Sstevel@tonic-gate contract_process_sig(cont_process_t *ctp, proc_t *p, int sig, pid_t pid,
981*7c478bd9Sstevel@tonic-gate     ctid_t ctid, zoneid_t zoneid)
982*7c478bd9Sstevel@tonic-gate {
983*7c478bd9Sstevel@tonic-gate 	contract_t *ct = &ctp->conp_contract;
984*7c478bd9Sstevel@tonic-gate 
985*7c478bd9Sstevel@tonic-gate 	if (EVSENDP(ctp, CT_PR_EV_SIGNAL)) {
986*7c478bd9Sstevel@tonic-gate 		ct_kevent_t *event;
987*7c478bd9Sstevel@tonic-gate 		nvlist_t *dest, *nvl, *gnvl = NULL;
988*7c478bd9Sstevel@tonic-gate 
989*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
990*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_PID, p->p_pid) == 0);
991*7c478bd9Sstevel@tonic-gate 		VERIFY(nvlist_add_uint32(nvl, CTPE_SIGNAL, sig) == 0);
992*7c478bd9Sstevel@tonic-gate 
993*7c478bd9Sstevel@tonic-gate 		if (zoneid >= 0 && p->p_zone->zone_id != zoneid) {
994*7c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_alloc(&gnvl, NV_UNIQUE_NAME,
995*7c478bd9Sstevel@tonic-gate 			    KM_SLEEP) == 0);
996*7c478bd9Sstevel@tonic-gate 			dest = gnvl;
997*7c478bd9Sstevel@tonic-gate 		} else {
998*7c478bd9Sstevel@tonic-gate 			dest = nvl;
999*7c478bd9Sstevel@tonic-gate 		}
1000*7c478bd9Sstevel@tonic-gate 
1001*7c478bd9Sstevel@tonic-gate 		if (pid != -1)
1002*7c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_uint32(dest, CTPE_SENDER, pid) == 0);
1003*7c478bd9Sstevel@tonic-gate 		if (ctid != 0)
1004*7c478bd9Sstevel@tonic-gate 			VERIFY(nvlist_add_uint32(dest, CTPE_SENDCT, ctid) == 0);
1005*7c478bd9Sstevel@tonic-gate 
1006*7c478bd9Sstevel@tonic-gate 		event = kmem_zalloc(sizeof (ct_kevent_t), KM_SLEEP);
1007*7c478bd9Sstevel@tonic-gate 		event->cte_flags = EVINFOP(ctp, CT_PR_EV_SIGNAL) ? CTE_INFO : 0;
1008*7c478bd9Sstevel@tonic-gate 		event->cte_type = CT_PR_EV_SIGNAL;
1009*7c478bd9Sstevel@tonic-gate 		cte_publish_all(ct, event, nvl, gnvl);
1010*7c478bd9Sstevel@tonic-gate 	}
1011*7c478bd9Sstevel@tonic-gate 
1012*7c478bd9Sstevel@tonic-gate 	if (EVFATALP(ctp, CT_PR_EV_SIGNAL)) {
1013*7c478bd9Sstevel@tonic-gate 		mutex_enter(&ct->ct_lock);
1014*7c478bd9Sstevel@tonic-gate 		contract_process_kill(ct, p, B_TRUE);
1015*7c478bd9Sstevel@tonic-gate 		mutex_exit(&ct->ct_lock);
1016*7c478bd9Sstevel@tonic-gate 	}
1017*7c478bd9Sstevel@tonic-gate }
1018