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