control legacy uname with an environment variable
[unleashed.git] / kernel / fs / proc / prcontrol.c
blob9d2d583c2987e84dfa87c83a98128cc09cd851b6
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
32 #include <sys/types.h>
33 #include <sys/uio.h>
34 #include <sys/param.h>
35 #include <sys/cmn_err.h>
36 #include <sys/cred.h>
37 #include <sys/policy.h>
38 #include <sys/debug.h>
39 #include <sys/errno.h>
40 #include <sys/file.h>
41 #include <sys/inline.h>
42 #include <sys/kmem.h>
43 #include <sys/proc.h>
44 #include <sys/brand.h>
45 #include <sys/regset.h>
46 #include <sys/sysmacros.h>
47 #include <sys/systm.h>
48 #include <sys/vfs.h>
49 #include <sys/vnode.h>
50 #include <sys/signal.h>
51 #include <sys/auxv.h>
52 #include <sys/user.h>
53 #include <sys/class.h>
54 #include <sys/fault.h>
55 #include <sys/syscall.h>
56 #include <sys/procfs.h>
57 #include <sys/zone.h>
58 #include <sys/copyops.h>
59 #include <sys/schedctl.h>
60 #include <vm/as.h>
61 #include <vm/seg.h>
62 #include <sys/proc/prdata.h>
63 #include <sys/contract/process_impl.h>
65 static void pr_settrace(proc_t *, sigset_t *);
66 static int pr_setfpregs(prnode_t *, prfpregset_t *);
67 #if defined(__sparc)
68 static int pr_setxregs(prnode_t *, prxregset_t *);
69 static int pr_setasrs(prnode_t *, asrset_t);
70 #endif
71 static int pr_setvaddr(prnode_t *, caddr_t);
72 static int pr_clearsig(prnode_t *);
73 static int pr_clearflt(prnode_t *);
74 static int pr_watch(prnode_t *, prwatch_t *, int *);
75 static int pr_agent(prnode_t *, prgregset_t, int *);
76 static int pr_rdwr(proc_t *, enum uio_rw, priovec_t *);
77 static int pr_scred(proc_t *, prcred_t *, cred_t *, boolean_t);
78 static int pr_spriv(proc_t *, prpriv_t *, cred_t *);
79 static int pr_szoneid(proc_t *, zoneid_t, cred_t *);
80 static void pauselwps(proc_t *);
81 static void unpauselwps(proc_t *);
83 typedef union {
84 long sig; /* PCKILL, PCUNKILL */
85 long nice; /* PCNICE */
86 long timeo; /* PCTWSTOP */
87 ulong_t flags; /* PCRUN, PCSET, PCUNSET */
88 caddr_t vaddr; /* PCSVADDR */
89 siginfo_t siginfo; /* PCSSIG */
90 sigset_t sigset; /* PCSTRACE, PCSHOLD */
91 fltset_t fltset; /* PCSFAULT */
92 sysset_t sysset; /* PCSENTRY, PCSEXIT */
93 prgregset_t prgregset; /* PCSREG, PCAGENT */
94 prfpregset_t prfpregset; /* PCSFPREG */
95 #if defined(__sparc)
96 prxregset_t prxregset; /* PCSXREG */
97 asrset_t asrset; /* PCSASRS */
98 #endif
99 prwatch_t prwatch; /* PCWATCH */
100 priovec_t priovec; /* PCREAD, PCWRITE */
101 prcred_t prcred; /* PCSCRED */
102 prpriv_t prpriv; /* PCSPRIV */
103 long przoneid; /* PCSZONE */
104 } arg_t;
106 static int pr_control(long, arg_t *, prnode_t *, cred_t *);
108 static size_t
109 ctlsize(long cmd, size_t resid, arg_t *argp)
111 size_t size = sizeof (long);
112 size_t rnd;
113 int ngrp;
115 switch (cmd) {
116 case PCNULL:
117 case PCSTOP:
118 case PCDSTOP:
119 case PCWSTOP:
120 case PCCSIG:
121 case PCCFAULT:
122 break;
123 case PCSSIG:
124 size += sizeof (siginfo_t);
125 break;
126 case PCTWSTOP:
127 size += sizeof (long);
128 break;
129 case PCKILL:
130 case PCUNKILL:
131 case PCNICE:
132 size += sizeof (long);
133 break;
134 case PCRUN:
135 case PCSET:
136 case PCUNSET:
137 size += sizeof (ulong_t);
138 break;
139 case PCSVADDR:
140 size += sizeof (caddr_t);
141 break;
142 case PCSTRACE:
143 case PCSHOLD:
144 size += sizeof (sigset_t);
145 break;
146 case PCSFAULT:
147 size += sizeof (fltset_t);
148 break;
149 case PCSENTRY:
150 case PCSEXIT:
151 size += sizeof (sysset_t);
152 break;
153 case PCSREG:
154 case PCAGENT:
155 size += sizeof (prgregset_t);
156 break;
157 case PCSFPREG:
158 size += sizeof (prfpregset_t);
159 break;
160 #if defined(__sparc)
161 case PCSXREG:
162 size += sizeof (prxregset_t);
163 break;
164 case PCSASRS:
165 size += sizeof (asrset_t);
166 break;
167 #endif
168 case PCWATCH:
169 size += sizeof (prwatch_t);
170 break;
171 case PCREAD:
172 case PCWRITE:
173 size += sizeof (priovec_t);
174 break;
175 case PCSCRED:
176 size += sizeof (prcred_t);
177 break;
178 case PCSCREDX:
180 * We cannot derefence the pr_ngroups fields if it
181 * we don't have enough data.
183 if (resid < size + sizeof (prcred_t) - sizeof (gid_t))
184 return (0);
185 ngrp = argp->prcred.pr_ngroups;
186 if (ngrp < 0 || ngrp > ngroups_max)
187 return (0);
189 /* The result can be smaller than sizeof (prcred_t) */
190 size += sizeof (prcred_t) - sizeof (gid_t);
191 size += ngrp * sizeof (gid_t);
192 break;
193 case PCSPRIV:
194 if (resid >= size + sizeof (prpriv_t))
195 size += priv_prgetprivsize(&argp->prpriv);
196 else
197 return (0);
198 break;
199 case PCSZONE:
200 size += sizeof (long);
201 break;
202 default:
203 return (0);
206 /* Round up to a multiple of long, unless exact amount written */
207 if (size < resid) {
208 rnd = size & (sizeof (long) - 1);
210 if (rnd != 0)
211 size += sizeof (long) - rnd;
214 if (size > resid)
215 return (0);
216 return (size);
220 * Control operations (lots).
223 prwritectl(vnode_t *vp, uio_t *uiop, cred_t *cr)
225 #define MY_BUFFER_SIZE \
226 100 > 1 + sizeof (arg_t) / sizeof (long) ? \
227 100 : 1 + sizeof (arg_t) / sizeof (long)
228 long buf[MY_BUFFER_SIZE];
229 long *bufp;
230 size_t resid = 0;
231 size_t size;
232 prnode_t *pnp = VTOP(vp);
233 int error;
234 int locked = 0;
236 while (uiop->uio_resid) {
238 * Read several commands in one gulp.
240 bufp = buf;
241 if (resid) { /* move incomplete command to front of buffer */
242 long *tail;
244 if (resid >= sizeof (buf))
245 break;
246 tail = (long *)((char *)buf + sizeof (buf) - resid);
247 do {
248 *bufp++ = *tail++;
249 } while ((resid -= sizeof (long)) != 0);
251 resid = sizeof (buf) - ((char *)bufp - (char *)buf);
252 if (resid > uiop->uio_resid)
253 resid = uiop->uio_resid;
254 if (error = uiomove((caddr_t)bufp, resid, UIO_WRITE, uiop))
255 return (error);
256 resid += (char *)bufp - (char *)buf;
257 bufp = buf;
259 do { /* loop over commands in buffer */
260 long cmd = bufp[0];
261 arg_t *argp = (arg_t *)&bufp[1];
263 size = ctlsize(cmd, resid, argp);
264 if (size == 0) /* incomplete or invalid command */
265 break;
267 * Perform the specified control operation.
269 if (!locked) {
270 if ((error = prlock(pnp, ZNO)) != 0)
271 return (error);
272 locked = 1;
274 if (error = pr_control(cmd, argp, pnp, cr)) {
275 if (error == -1) /* -1 is timeout */
276 locked = 0;
277 else
278 return (error);
280 bufp = (long *)((char *)bufp + size);
281 } while ((resid -= size) != 0);
283 if (locked) {
284 prunlock(pnp);
285 locked = 0;
288 return (resid? EINVAL : 0);
291 static int
292 pr_control(long cmd, arg_t *argp, prnode_t *pnp, cred_t *cr)
294 prcommon_t *pcp;
295 proc_t *p;
296 int unlocked;
297 int error = 0;
299 if (cmd == PCNULL)
300 return (0);
302 pcp = pnp->pr_common;
303 p = pcp->prc_proc;
304 ASSERT(p != NULL);
306 /* System processes defy control. */
307 if (p->p_flag & SSYS) {
308 prunlock(pnp);
309 return (EBUSY);
312 switch (cmd) {
314 default:
315 error = EINVAL;
316 break;
318 case PCSTOP: /* direct process or lwp to stop and wait for stop */
319 case PCDSTOP: /* direct process or lwp to stop, don't wait */
320 case PCWSTOP: /* wait for process or lwp to stop */
321 case PCTWSTOP: /* wait for process or lwp to stop, with timeout */
323 time_t timeo;
326 * Can't apply to a system process.
328 if (p->p_as == &kas) {
329 error = EBUSY;
330 break;
333 if (cmd == PCSTOP || cmd == PCDSTOP)
334 pr_stop(pnp);
336 if (cmd == PCDSTOP)
337 break;
340 * If an lwp is waiting for itself or its process,
341 * don't wait. The stopped lwp would never see the
342 * fact that it is stopped.
344 if ((pcp->prc_flags & PRC_LWP)?
345 (pcp->prc_thread == curthread) : (p == curproc)) {
346 if (cmd == PCWSTOP || cmd == PCTWSTOP)
347 error = EBUSY;
348 break;
351 timeo = (cmd == PCTWSTOP)? (time_t)argp->timeo : 0;
352 if ((error = pr_wait_stop(pnp, timeo)) != 0)
353 return (error);
355 break;
358 case PCRUN: /* make lwp or process runnable */
359 error = pr_setrun(pnp, argp->flags);
360 break;
362 case PCSTRACE: /* set signal trace mask */
363 pr_settrace(p, &argp->sigset);
364 break;
366 case PCSSIG: /* set current signal */
367 error = pr_setsig(pnp, &argp->siginfo);
368 if (argp->siginfo.si_signo == SIGKILL && error == 0) {
369 prunlock(pnp);
370 pr_wait_die(pnp);
371 return (-1);
373 break;
375 case PCKILL: /* send signal */
376 error = pr_kill(pnp, (int)argp->sig, cr);
377 if (error == 0 && argp->sig == SIGKILL) {
378 prunlock(pnp);
379 pr_wait_die(pnp);
380 return (-1);
382 break;
384 case PCUNKILL: /* delete a pending signal */
385 error = pr_unkill(pnp, (int)argp->sig);
386 break;
388 case PCNICE: /* set nice priority */
389 error = pr_nice(p, (int)argp->nice, cr);
390 break;
392 case PCSENTRY: /* set syscall entry bit mask */
393 case PCSEXIT: /* set syscall exit bit mask */
394 pr_setentryexit(p, &argp->sysset, cmd == PCSENTRY);
395 break;
397 case PCSET: /* set process flags */
398 error = pr_set(p, argp->flags);
399 break;
401 case PCUNSET: /* unset process flags */
402 error = pr_unset(p, argp->flags);
403 break;
405 case PCSREG: /* set general registers */
407 kthread_t *t = pr_thread(pnp);
409 if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
410 thread_unlock(t);
411 error = EBUSY;
412 } else {
413 thread_unlock(t);
414 mutex_exit(&p->p_lock);
415 prsetprregs(ttolwp(t), argp->prgregset, 0);
416 mutex_enter(&p->p_lock);
418 break;
421 case PCSFPREG: /* set floating-point registers */
422 error = pr_setfpregs(pnp, &argp->prfpregset);
423 break;
425 case PCSXREG: /* set extra registers */
426 #if defined(__sparc)
427 error = pr_setxregs(pnp, &argp->prxregset);
428 #else
429 error = EINVAL;
430 #endif
431 break;
433 #if defined(__sparc)
434 case PCSASRS: /* set ancillary state registers */
435 error = pr_setasrs(pnp, argp->asrset);
436 break;
437 #endif
439 case PCSVADDR: /* set virtual address at which to resume */
440 error = pr_setvaddr(pnp, argp->vaddr);
441 break;
443 case PCSHOLD: /* set signal-hold mask */
444 pr_sethold(pnp, &argp->sigset);
445 break;
447 case PCSFAULT: /* set mask of traced faults */
448 pr_setfault(p, &argp->fltset);
449 break;
451 case PCCSIG: /* clear current signal */
452 error = pr_clearsig(pnp);
453 break;
455 case PCCFAULT: /* clear current fault */
456 error = pr_clearflt(pnp);
457 break;
459 case PCWATCH: /* set or clear watched areas */
460 error = pr_watch(pnp, &argp->prwatch, &unlocked);
461 if (error && unlocked)
462 return (error);
463 break;
465 case PCAGENT: /* create the /proc agent lwp in the target process */
466 error = pr_agent(pnp, argp->prgregset, &unlocked);
467 if (error && unlocked)
468 return (error);
469 break;
471 case PCREAD: /* read from the address space */
472 error = pr_rdwr(p, UIO_READ, &argp->priovec);
473 break;
475 case PCWRITE: /* write to the address space */
476 error = pr_rdwr(p, UIO_WRITE, &argp->priovec);
477 break;
479 case PCSCRED: /* set the process credentials */
480 case PCSCREDX:
481 error = pr_scred(p, &argp->prcred, cr, cmd == PCSCREDX);
482 break;
484 case PCSPRIV: /* set the process privileges */
485 error = pr_spriv(p, &argp->prpriv, cr);
486 break;
487 case PCSZONE: /* set the process's zoneid credentials */
488 error = pr_szoneid(p, (zoneid_t)argp->przoneid, cr);
489 break;
492 if (error)
493 prunlock(pnp);
494 return (error);
497 #ifdef _SYSCALL32_IMPL
499 typedef union {
500 int32_t sig; /* PCKILL, PCUNKILL */
501 int32_t nice; /* PCNICE */
502 int32_t timeo; /* PCTWSTOP */
503 uint32_t flags; /* PCRUN, PCSET, PCUNSET */
504 caddr32_t vaddr; /* PCSVADDR */
505 siginfo32_t siginfo; /* PCSSIG */
506 sigset_t sigset; /* PCSTRACE, PCSHOLD */
507 fltset_t fltset; /* PCSFAULT */
508 sysset_t sysset; /* PCSENTRY, PCSEXIT */
509 prgregset32_t prgregset; /* PCSREG, PCAGENT */
510 prfpregset32_t prfpregset; /* PCSFPREG */
511 #if defined(__sparc)
512 prxregset_t prxregset; /* PCSXREG */
513 #endif
514 prwatch32_t prwatch; /* PCWATCH */
515 priovec32_t priovec; /* PCREAD, PCWRITE */
516 prcred32_t prcred; /* PCSCRED */
517 prpriv_t prpriv; /* PCSPRIV */
518 int32_t przoneid; /* PCSZONE */
519 } arg32_t;
521 static int pr_control32(int32_t, arg32_t *, prnode_t *, cred_t *);
522 static int pr_setfpregs32(prnode_t *, prfpregset32_t *);
525 * Note that while ctlsize32() can use argp, it must do so only in a way
526 * that assumes 32-bit rather than 64-bit alignment as argp is a pointer
527 * to an array of 32-bit values and only 32-bit alignment is ensured.
529 static size_t
530 ctlsize32(int32_t cmd, size_t resid, arg32_t *argp)
532 size_t size = sizeof (int32_t);
533 size_t rnd;
534 int ngrp;
536 switch (cmd) {
537 case PCNULL:
538 case PCSTOP:
539 case PCDSTOP:
540 case PCWSTOP:
541 case PCCSIG:
542 case PCCFAULT:
543 break;
544 case PCSSIG:
545 size += sizeof (siginfo32_t);
546 break;
547 case PCTWSTOP:
548 size += sizeof (int32_t);
549 break;
550 case PCKILL:
551 case PCUNKILL:
552 case PCNICE:
553 size += sizeof (int32_t);
554 break;
555 case PCRUN:
556 case PCSET:
557 case PCUNSET:
558 size += sizeof (uint32_t);
559 break;
560 case PCSVADDR:
561 size += sizeof (caddr32_t);
562 break;
563 case PCSTRACE:
564 case PCSHOLD:
565 size += sizeof (sigset_t);
566 break;
567 case PCSFAULT:
568 size += sizeof (fltset_t);
569 break;
570 case PCSENTRY:
571 case PCSEXIT:
572 size += sizeof (sysset_t);
573 break;
574 case PCSREG:
575 case PCAGENT:
576 size += sizeof (prgregset32_t);
577 break;
578 case PCSFPREG:
579 size += sizeof (prfpregset32_t);
580 break;
581 #if defined(__sparc)
582 case PCSXREG:
583 size += sizeof (prxregset_t);
584 break;
585 #endif
586 case PCWATCH:
587 size += sizeof (prwatch32_t);
588 break;
589 case PCREAD:
590 case PCWRITE:
591 size += sizeof (priovec32_t);
592 break;
593 case PCSCRED:
594 size += sizeof (prcred32_t);
595 break;
596 case PCSCREDX:
598 * We cannot derefence the pr_ngroups fields if it
599 * we don't have enough data.
601 if (resid < size + sizeof (prcred32_t) - sizeof (gid32_t))
602 return (0);
603 ngrp = argp->prcred.pr_ngroups;
604 if (ngrp < 0 || ngrp > ngroups_max)
605 return (0);
607 /* The result can be smaller than sizeof (prcred32_t) */
608 size += sizeof (prcred32_t) - sizeof (gid32_t);
609 size += ngrp * sizeof (gid32_t);
610 break;
611 case PCSPRIV:
612 if (resid >= size + sizeof (prpriv_t))
613 size += priv_prgetprivsize(&argp->prpriv);
614 else
615 return (0);
616 break;
617 case PCSZONE:
618 size += sizeof (int32_t);
619 break;
620 default:
621 return (0);
624 /* Round up to a multiple of int32_t */
625 rnd = size & (sizeof (int32_t) - 1);
627 if (rnd != 0)
628 size += sizeof (int32_t) - rnd;
630 if (size > resid)
631 return (0);
632 return (size);
636 * Control operations (lots).
639 prwritectl32(struct vnode *vp, struct uio *uiop, cred_t *cr)
641 #define MY_BUFFER_SIZE32 \
642 100 > 1 + sizeof (arg32_t) / sizeof (int32_t) ? \
643 100 : 1 + sizeof (arg32_t) / sizeof (int32_t)
644 int32_t buf[MY_BUFFER_SIZE32];
645 int32_t *bufp;
646 arg32_t arg;
647 size_t resid = 0;
648 size_t size;
649 prnode_t *pnp = VTOP(vp);
650 int error;
651 int locked = 0;
653 while (uiop->uio_resid) {
655 * Read several commands in one gulp.
657 bufp = buf;
658 if (resid) { /* move incomplete command to front of buffer */
659 int32_t *tail;
661 if (resid >= sizeof (buf))
662 break;
663 tail = (int32_t *)((char *)buf + sizeof (buf) - resid);
664 do {
665 *bufp++ = *tail++;
666 } while ((resid -= sizeof (int32_t)) != 0);
668 resid = sizeof (buf) - ((char *)bufp - (char *)buf);
669 if (resid > uiop->uio_resid)
670 resid = uiop->uio_resid;
671 if (error = uiomove((caddr_t)bufp, resid, UIO_WRITE, uiop))
672 return (error);
673 resid += (char *)bufp - (char *)buf;
674 bufp = buf;
676 do { /* loop over commands in buffer */
677 int32_t cmd = bufp[0];
678 arg32_t *argp = (arg32_t *)&bufp[1];
680 size = ctlsize32(cmd, resid, argp);
681 if (size == 0) /* incomplete or invalid command */
682 break;
684 * Perform the specified control operation.
686 if (!locked) {
687 if ((error = prlock(pnp, ZNO)) != 0)
688 return (error);
689 locked = 1;
693 * Since some members of the arg32_t union contain
694 * 64-bit values (which must be 64-bit aligned), we
695 * can't simply pass a pointer to the structure as
696 * it may be unaligned. Note that we do pass the
697 * potentially unaligned structure to ctlsize32()
698 * above, but that uses it a way that makes no
699 * assumptions about alignment.
701 ASSERT(size - sizeof (cmd) <= sizeof (arg));
702 bcopy(argp, &arg, size - sizeof (cmd));
704 if (error = pr_control32(cmd, &arg, pnp, cr)) {
705 if (error == -1) /* -1 is timeout */
706 locked = 0;
707 else
708 return (error);
710 bufp = (int32_t *)((char *)bufp + size);
711 } while ((resid -= size) != 0);
713 if (locked) {
714 prunlock(pnp);
715 locked = 0;
718 return (resid? EINVAL : 0);
721 static int
722 pr_control32(int32_t cmd, arg32_t *argp, prnode_t *pnp, cred_t *cr)
724 prcommon_t *pcp;
725 proc_t *p;
726 int unlocked;
727 int error = 0;
729 if (cmd == PCNULL)
730 return (0);
732 pcp = pnp->pr_common;
733 p = pcp->prc_proc;
734 ASSERT(p != NULL);
736 if (p->p_flag & SSYS) {
737 prunlock(pnp);
738 return (EBUSY);
741 switch (cmd) {
743 default:
744 error = EINVAL;
745 break;
747 case PCSTOP: /* direct process or lwp to stop and wait for stop */
748 case PCDSTOP: /* direct process or lwp to stop, don't wait */
749 case PCWSTOP: /* wait for process or lwp to stop */
750 case PCTWSTOP: /* wait for process or lwp to stop, with timeout */
752 time_t timeo;
755 * Can't apply to a system process.
757 if (p->p_as == &kas) {
758 error = EBUSY;
759 break;
762 if (cmd == PCSTOP || cmd == PCDSTOP)
763 pr_stop(pnp);
765 if (cmd == PCDSTOP)
766 break;
769 * If an lwp is waiting for itself or its process,
770 * don't wait. The lwp will never see the fact that
771 * itself is stopped.
773 if ((pcp->prc_flags & PRC_LWP)?
774 (pcp->prc_thread == curthread) : (p == curproc)) {
775 if (cmd == PCWSTOP || cmd == PCTWSTOP)
776 error = EBUSY;
777 break;
780 timeo = (cmd == PCTWSTOP)? (time_t)argp->timeo : 0;
781 if ((error = pr_wait_stop(pnp, timeo)) != 0)
782 return (error);
784 break;
787 case PCRUN: /* make lwp or process runnable */
788 error = pr_setrun(pnp, (ulong_t)argp->flags);
789 break;
791 case PCSTRACE: /* set signal trace mask */
792 pr_settrace(p, &argp->sigset);
793 break;
795 case PCSSIG: /* set current signal */
796 if (PROCESS_NOT_32BIT(p))
797 error = EOVERFLOW;
798 else {
799 int sig = (int)argp->siginfo.si_signo;
800 siginfo_t siginfo;
802 bzero(&siginfo, sizeof (siginfo));
803 siginfo_32tok(&argp->siginfo, (k_siginfo_t *)&siginfo);
804 error = pr_setsig(pnp, &siginfo);
805 if (sig == SIGKILL && error == 0) {
806 prunlock(pnp);
807 pr_wait_die(pnp);
808 return (-1);
811 break;
813 case PCKILL: /* send signal */
814 error = pr_kill(pnp, (int)argp->sig, cr);
815 if (error == 0 && argp->sig == SIGKILL) {
816 prunlock(pnp);
817 pr_wait_die(pnp);
818 return (-1);
820 break;
822 case PCUNKILL: /* delete a pending signal */
823 error = pr_unkill(pnp, (int)argp->sig);
824 break;
826 case PCNICE: /* set nice priority */
827 error = pr_nice(p, (int)argp->nice, cr);
828 break;
830 case PCSENTRY: /* set syscall entry bit mask */
831 case PCSEXIT: /* set syscall exit bit mask */
832 pr_setentryexit(p, &argp->sysset, cmd == PCSENTRY);
833 break;
835 case PCSET: /* set process flags */
836 error = pr_set(p, (long)argp->flags);
837 break;
839 case PCUNSET: /* unset process flags */
840 error = pr_unset(p, (long)argp->flags);
841 break;
843 case PCSREG: /* set general registers */
844 if (PROCESS_NOT_32BIT(p))
845 error = EOVERFLOW;
846 else {
847 kthread_t *t = pr_thread(pnp);
849 if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
850 thread_unlock(t);
851 error = EBUSY;
852 } else {
853 prgregset_t prgregset;
854 klwp_t *lwp = ttolwp(t);
856 thread_unlock(t);
857 mutex_exit(&p->p_lock);
858 prgregset_32ton(lwp, argp->prgregset,
859 prgregset);
860 prsetprregs(lwp, prgregset, 0);
861 mutex_enter(&p->p_lock);
864 break;
866 case PCSFPREG: /* set floating-point registers */
867 if (PROCESS_NOT_32BIT(p))
868 error = EOVERFLOW;
869 else
870 error = pr_setfpregs32(pnp, &argp->prfpregset);
871 break;
873 case PCSXREG: /* set extra registers */
874 #if defined(__sparc)
875 if (PROCESS_NOT_32BIT(p))
876 error = EOVERFLOW;
877 else
878 error = pr_setxregs(pnp, &argp->prxregset);
879 #else
880 error = EINVAL;
881 #endif
882 break;
884 case PCSVADDR: /* set virtual address at which to resume */
885 if (PROCESS_NOT_32BIT(p))
886 error = EOVERFLOW;
887 else
888 error = pr_setvaddr(pnp,
889 (caddr_t)(uintptr_t)argp->vaddr);
890 break;
892 case PCSHOLD: /* set signal-hold mask */
893 pr_sethold(pnp, &argp->sigset);
894 break;
896 case PCSFAULT: /* set mask of traced faults */
897 pr_setfault(p, &argp->fltset);
898 break;
900 case PCCSIG: /* clear current signal */
901 error = pr_clearsig(pnp);
902 break;
904 case PCCFAULT: /* clear current fault */
905 error = pr_clearflt(pnp);
906 break;
908 case PCWATCH: /* set or clear watched areas */
909 if (PROCESS_NOT_32BIT(p))
910 error = EOVERFLOW;
911 else {
912 prwatch_t prwatch;
914 prwatch.pr_vaddr = argp->prwatch.pr_vaddr;
915 prwatch.pr_size = argp->prwatch.pr_size;
916 prwatch.pr_wflags = argp->prwatch.pr_wflags;
917 prwatch.pr_pad = argp->prwatch.pr_pad;
918 error = pr_watch(pnp, &prwatch, &unlocked);
919 if (error && unlocked)
920 return (error);
922 break;
924 case PCAGENT: /* create the /proc agent lwp in the target process */
925 if (PROCESS_NOT_32BIT(p))
926 error = EOVERFLOW;
927 else {
928 prgregset_t prgregset;
929 kthread_t *t = pr_thread(pnp);
930 klwp_t *lwp = ttolwp(t);
931 thread_unlock(t);
932 mutex_exit(&p->p_lock);
933 prgregset_32ton(lwp, argp->prgregset, prgregset);
934 mutex_enter(&p->p_lock);
935 error = pr_agent(pnp, prgregset, &unlocked);
936 if (error && unlocked)
937 return (error);
939 break;
941 case PCREAD: /* read from the address space */
942 case PCWRITE: /* write to the address space */
943 if (PROCESS_NOT_32BIT(p) || (pnp->pr_flags & PR_OFFMAX))
944 error = EOVERFLOW;
945 else {
946 enum uio_rw rw = (cmd == PCREAD)? UIO_READ : UIO_WRITE;
947 priovec_t priovec;
949 priovec.pio_base =
950 (void *)(uintptr_t)argp->priovec.pio_base;
951 priovec.pio_len = (size_t)argp->priovec.pio_len;
952 priovec.pio_offset = (off_t)
953 (uint32_t)argp->priovec.pio_offset;
954 error = pr_rdwr(p, rw, &priovec);
956 break;
958 case PCSCRED: /* set the process credentials */
959 case PCSCREDX:
962 * All the fields in these structures are exactly the
963 * same and so the structures are compatible. In case
964 * this ever changes, we catch this with the ASSERT
965 * below.
967 prcred_t *prcred = (prcred_t *)&argp->prcred;
969 ASSERT(sizeof (prcred_t) == sizeof (prcred32_t));
971 error = pr_scred(p, prcred, cr, cmd == PCSCREDX);
972 break;
975 case PCSPRIV: /* set the process privileges */
976 error = pr_spriv(p, &argp->prpriv, cr);
977 break;
979 case PCSZONE: /* set the process's zoneid */
980 error = pr_szoneid(p, (zoneid_t)argp->przoneid, cr);
981 break;
984 if (error)
985 prunlock(pnp);
986 return (error);
989 #endif /* _SYSCALL32_IMPL */
992 * Return the specific or chosen thread/lwp for a control operation.
993 * Returns with the thread locked via thread_lock(t).
995 kthread_t *
996 pr_thread(prnode_t *pnp)
998 prcommon_t *pcp = pnp->pr_common;
999 kthread_t *t;
1001 if (pcp->prc_flags & PRC_LWP) {
1002 t = pcp->prc_thread;
1003 ASSERT(t != NULL);
1004 thread_lock(t);
1005 } else {
1006 proc_t *p = pcp->prc_proc;
1007 t = prchoose(p); /* returns locked thread */
1008 ASSERT(t != NULL);
1011 return (t);
1015 * Direct the process or lwp to stop.
1017 void
1018 pr_stop(prnode_t *pnp)
1020 prcommon_t *pcp = pnp->pr_common;
1021 proc_t *p = pcp->prc_proc;
1022 kthread_t *t;
1023 vnode_t *vp;
1026 * If already stopped, do nothing; otherwise flag
1027 * it to be stopped the next time it tries to run.
1028 * If sleeping at interruptible priority, set it
1029 * running so it will stop within cv_wait_sig().
1031 * Take care to cooperate with jobcontrol: if an lwp
1032 * is stopped due to the default action of a jobcontrol
1033 * stop signal, flag it to be stopped the next time it
1034 * starts due to a SIGCONT signal.
1036 if (pcp->prc_flags & PRC_LWP)
1037 t = pcp->prc_thread;
1038 else
1039 t = p->p_tlist;
1040 ASSERT(t != NULL);
1042 do {
1043 int notify;
1045 notify = 0;
1046 thread_lock(t);
1047 if (!ISTOPPED(t)) {
1048 t->t_proc_flag |= TP_PRSTOP;
1049 t->t_sig_check = 1; /* do ISSIG */
1052 /* Move the thread from wait queue to run queue */
1053 if (ISWAITING(t))
1054 setrun_locked(t);
1056 if (ISWAKEABLE(t)) {
1057 if (t->t_wchan0 == NULL)
1058 setrun_locked(t);
1059 else if (!VSTOPPED(t)) {
1061 * Mark it virtually stopped.
1063 t->t_proc_flag |= TP_PRVSTOP;
1064 notify = 1;
1068 * force the thread into the kernel
1069 * if it is not already there.
1071 prpokethread(t);
1072 thread_unlock(t);
1073 if (notify &&
1074 (vp = p->p_lwpdir[t->t_dslot].ld_entry->le_trace) != NULL)
1075 prnotify(vp);
1076 if (pcp->prc_flags & PRC_LWP)
1077 break;
1078 } while ((t = t->t_forw) != p->p_tlist);
1081 * We do this just in case the thread we asked
1082 * to stop is in holdlwps() (called from cfork()).
1084 cv_broadcast(&p->p_holdlwps);
1088 * Sleep until the lwp stops, but cooperate with
1089 * jobcontrol: Don't wake up if the lwp is stopped
1090 * due to the default action of a jobcontrol stop signal.
1091 * If this is the process file descriptor, sleep
1092 * until all of the process's lwps stop.
1095 pr_wait_stop(prnode_t *pnp, time_t timeo)
1097 prcommon_t *pcp = pnp->pr_common;
1098 proc_t *p = pcp->prc_proc;
1099 timestruc_t rqtime;
1100 timestruc_t *rqtp = NULL;
1101 int timecheck = 0;
1102 kthread_t *t;
1103 int error;
1105 if (timeo > 0) { /* millisecond timeout */
1107 * Determine the precise future time of the requested timeout.
1109 timestruc_t now;
1111 timecheck = timechanged;
1112 gethrestime(&now);
1113 rqtp = &rqtime;
1114 rqtp->tv_sec = timeo / MILLISEC;
1115 rqtp->tv_nsec = (timeo % MILLISEC) * MICROSEC;
1116 timespecadd(rqtp, &now);
1119 if (pcp->prc_flags & PRC_LWP) { /* lwp file descriptor */
1120 t = pcp->prc_thread;
1121 ASSERT(t != NULL);
1122 thread_lock(t);
1123 while (!ISTOPPED(t) && !VSTOPPED(t)) {
1124 thread_unlock(t);
1125 mutex_enter(&pcp->prc_mutex);
1126 prunlock(pnp);
1127 error = pr_wait(pcp, rqtp, timecheck);
1128 if (error) /* -1 is timeout */
1129 return (error);
1130 if ((error = prlock(pnp, ZNO)) != 0)
1131 return (error);
1132 ASSERT(p == pcp->prc_proc);
1133 ASSERT(t == pcp->prc_thread);
1134 thread_lock(t);
1136 thread_unlock(t);
1137 } else { /* process file descriptor */
1138 t = prchoose(p); /* returns locked thread */
1139 ASSERT(t != NULL);
1140 ASSERT(MUTEX_HELD(&p->p_lock));
1141 while ((!ISTOPPED(t) && !VSTOPPED(t) && !SUSPENDED(t)) ||
1142 (p->p_flag & SEXITLWPS)) {
1143 thread_unlock(t);
1144 mutex_enter(&pcp->prc_mutex);
1145 prunlock(pnp);
1146 error = pr_wait(pcp, rqtp, timecheck);
1147 if (error) /* -1 is timeout */
1148 return (error);
1149 if ((error = prlock(pnp, ZNO)) != 0)
1150 return (error);
1151 ASSERT(p == pcp->prc_proc);
1152 t = prchoose(p); /* returns locked t */
1153 ASSERT(t != NULL);
1155 thread_unlock(t);
1158 ASSERT(!(pcp->prc_flags & PRC_DESTROY) && p->p_stat != SZOMB &&
1159 t != NULL && t->t_state != TS_ZOMB);
1161 return (0);
1165 pr_setrun(prnode_t *pnp, ulong_t flags)
1167 prcommon_t *pcp = pnp->pr_common;
1168 proc_t *p = pcp->prc_proc;
1169 kthread_t *t;
1170 klwp_t *lwp;
1173 * Cannot set an lwp running if it is not stopped.
1174 * Also, no lwp other than the /proc agent lwp can
1175 * be set running so long as the /proc agent lwp exists.
1177 t = pr_thread(pnp); /* returns locked thread */
1178 if ((!ISTOPPED(t) && !VSTOPPED(t) &&
1179 !(t->t_proc_flag & TP_PRSTOP)) ||
1180 (p->p_agenttp != NULL &&
1181 (t != p->p_agenttp || !(pcp->prc_flags & PRC_LWP)))) {
1182 thread_unlock(t);
1183 return (EBUSY);
1185 thread_unlock(t);
1186 if (flags & ~(PRCSIG|PRCFAULT|PRSTEP|PRSTOP|PRSABORT))
1187 return (EINVAL);
1188 lwp = ttolwp(t);
1189 if ((flags & PRCSIG) && lwp->lwp_cursig != SIGKILL) {
1191 * Discard current siginfo_t, if any.
1193 lwp->lwp_cursig = 0;
1194 lwp->lwp_extsig = 0;
1195 if (lwp->lwp_curinfo) {
1196 siginfofree(lwp->lwp_curinfo);
1197 lwp->lwp_curinfo = NULL;
1200 if (flags & PRCFAULT)
1201 lwp->lwp_curflt = 0;
1203 * We can't hold p->p_lock when we touch the lwp's registers.
1204 * It may be swapped out and we will get a page fault.
1206 if (flags & PRSTEP) {
1207 mutex_exit(&p->p_lock);
1208 prstep(lwp, 0);
1209 mutex_enter(&p->p_lock);
1211 if (flags & PRSTOP) {
1212 t->t_proc_flag |= TP_PRSTOP;
1213 t->t_sig_check = 1; /* do ISSIG */
1215 if (flags & PRSABORT)
1216 lwp->lwp_sysabort = 1;
1217 thread_lock(t);
1218 if ((pcp->prc_flags & PRC_LWP) || (flags & (PRSTEP|PRSTOP))) {
1220 * Here, we are dealing with a single lwp.
1222 if (ISTOPPED(t)) {
1223 t->t_schedflag |= TS_PSTART;
1224 t->t_dtrace_stop = 0;
1225 setrun_locked(t);
1226 } else if (flags & PRSABORT) {
1227 t->t_proc_flag &=
1228 ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1229 setrun_locked(t);
1230 } else if (!(flags & PRSTOP)) {
1231 t->t_proc_flag &=
1232 ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1234 thread_unlock(t);
1235 } else {
1237 * Here, we are dealing with the whole process.
1239 if (ISTOPPED(t)) {
1241 * The representative lwp is stopped on an event
1242 * of interest. We demote it to PR_REQUESTED and
1243 * choose another representative lwp. If the new
1244 * representative lwp is not stopped on an event of
1245 * interest (other than PR_REQUESTED), we set the
1246 * whole process running, else we leave the process
1247 * stopped showing the next event of interest.
1249 kthread_t *tx = NULL;
1251 if (!(flags & PRSABORT) &&
1252 t->t_whystop == PR_SYSENTRY &&
1253 t->t_whatstop == SYS_lwp_exit)
1254 tx = t; /* remember the exiting lwp */
1255 t->t_whystop = PR_REQUESTED;
1256 t->t_whatstop = 0;
1257 thread_unlock(t);
1258 t = prchoose(p); /* returns locked t */
1259 ASSERT(ISTOPPED(t) || VSTOPPED(t));
1260 if (VSTOPPED(t) ||
1261 t->t_whystop == PR_REQUESTED) {
1262 thread_unlock(t);
1263 allsetrun(p);
1264 } else {
1265 thread_unlock(t);
1267 * As a special case, if the old representative
1268 * lwp was stopped on entry to _lwp_exit()
1269 * (and we are not aborting the system call),
1270 * we set the old representative lwp running.
1271 * We do this so that the next process stop
1272 * will find the exiting lwp gone.
1274 if (tx != NULL) {
1275 thread_lock(tx);
1276 tx->t_schedflag |= TS_PSTART;
1277 t->t_dtrace_stop = 0;
1278 setrun_locked(tx);
1279 thread_unlock(tx);
1282 } else {
1284 * No event of interest; set all of the lwps running.
1286 if (flags & PRSABORT) {
1287 t->t_proc_flag &=
1288 ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1289 setrun_locked(t);
1291 thread_unlock(t);
1292 allsetrun(p);
1295 return (0);
1299 * Wait until process/lwp stops or until timer expires.
1300 * Return EINTR for an interruption, -1 for timeout, else 0.
1303 pr_wait(prcommon_t *pcp, /* prcommon referring to process/lwp */
1304 timestruc_t *ts, /* absolute time of timeout, if any */
1305 int timecheck)
1307 int rval;
1309 ASSERT(MUTEX_HELD(&pcp->prc_mutex));
1310 rval = cv_waituntil_sig(&pcp->prc_wait, &pcp->prc_mutex, ts, timecheck);
1311 mutex_exit(&pcp->prc_mutex);
1312 switch (rval) {
1313 case 0:
1314 return (EINTR);
1315 case -1:
1316 return (-1);
1317 default:
1318 return (0);
1323 * Make all threads in the process runnable.
1325 void
1326 allsetrun(proc_t *p)
1328 kthread_t *t;
1330 ASSERT(MUTEX_HELD(&p->p_lock));
1332 if ((t = p->p_tlist) != NULL) {
1333 do {
1334 thread_lock(t);
1335 ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
1336 t->t_proc_flag &= ~(TP_PRSTOP|TP_PRVSTOP|TP_STOPPING);
1337 if (ISTOPPED(t)) {
1338 t->t_schedflag |= TS_PSTART;
1339 t->t_dtrace_stop = 0;
1340 setrun_locked(t);
1342 thread_unlock(t);
1343 } while ((t = t->t_forw) != p->p_tlist);
1348 * Wait for the process to die.
1349 * We do this after sending SIGKILL because we know it will
1350 * die soon and we want subsequent operations to return ENOENT.
1352 void
1353 pr_wait_die(prnode_t *pnp)
1355 proc_t *p;
1357 mutex_enter(&pidlock);
1358 while ((p = pnp->pr_common->prc_proc) != NULL && p->p_stat != SZOMB) {
1359 if (!cv_wait_sig(&p->p_srwchan_cv, &pidlock))
1360 break;
1362 mutex_exit(&pidlock);
1365 static void
1366 pr_settrace(proc_t *p, sigset_t *sp)
1368 prdelset(sp, SIGKILL);
1369 prassignset(&p->p_sigmask, sp);
1370 if (!sigisempty(&p->p_sigmask))
1371 p->p_proc_flag |= P_PR_TRACE;
1372 else if (prisempty(&p->p_fltmask)) {
1373 user_t *up = PTOU(p);
1374 if (up->u_systrap == 0)
1375 p->p_proc_flag &= ~P_PR_TRACE;
1380 pr_setsig(prnode_t *pnp, siginfo_t *sip)
1382 int nsig = PROC_IS_BRANDED(curproc)? BROP(curproc)->b_nsig : NSIG;
1383 int sig = sip->si_signo;
1384 prcommon_t *pcp = pnp->pr_common;
1385 proc_t *p = pcp->prc_proc;
1386 kthread_t *t;
1387 klwp_t *lwp;
1388 int error = 0;
1390 t = pr_thread(pnp); /* returns locked thread */
1391 thread_unlock(t);
1392 lwp = ttolwp(t);
1393 if (sig < 0 || sig >= nsig)
1394 /* Zero allowed here */
1395 error = EINVAL;
1396 else if (lwp->lwp_cursig == SIGKILL)
1397 /* "can't happen", but just in case */
1398 error = EBUSY;
1399 else if ((lwp->lwp_cursig = (uchar_t)sig) == 0) {
1400 lwp->lwp_extsig = 0;
1402 * Discard current siginfo_t, if any.
1404 if (lwp->lwp_curinfo) {
1405 siginfofree(lwp->lwp_curinfo);
1406 lwp->lwp_curinfo = NULL;
1408 } else {
1409 kthread_t *tx;
1410 sigqueue_t *sqp;
1412 /* drop p_lock to do kmem_alloc(KM_SLEEP) */
1413 mutex_exit(&p->p_lock);
1414 sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
1415 mutex_enter(&p->p_lock);
1417 if (lwp->lwp_curinfo == NULL)
1418 lwp->lwp_curinfo = sqp;
1419 else
1420 kmem_free(sqp, sizeof (sigqueue_t));
1422 * Copy contents of info to current siginfo_t.
1424 bcopy(sip, &lwp->lwp_curinfo->sq_info,
1425 sizeof (lwp->lwp_curinfo->sq_info));
1427 * Prevent contents published by si_zoneid-unaware /proc
1428 * consumers from being incorrectly filtered. Because
1429 * an uninitialized si_zoneid is the same as
1430 * GLOBAL_ZONEID, this means that you can't pr_setsig a
1431 * process in a non-global zone with a siginfo which
1432 * appears to come from the global zone.
1434 if (SI_FROMUSER(sip) && sip->si_zoneid == 0)
1435 lwp->lwp_curinfo->sq_info.si_zoneid =
1436 p->p_zone->zone_id;
1438 * Side-effects for SIGKILL and jobcontrol signals.
1440 if (sig == SIGKILL) {
1441 p->p_flag |= SKILLED;
1442 p->p_flag &= ~SEXTKILLED;
1443 } else if (sig == SIGCONT) {
1444 p->p_flag |= SSCONT;
1445 sigdelq(p, NULL, SIGSTOP);
1446 sigdelq(p, NULL, SIGTSTP);
1447 sigdelq(p, NULL, SIGTTOU);
1448 sigdelq(p, NULL, SIGTTIN);
1449 sigdiffset(&p->p_sig, &stopdefault);
1450 sigdiffset(&p->p_extsig, &stopdefault);
1451 if ((tx = p->p_tlist) != NULL) {
1452 do {
1453 sigdelq(p, tx, SIGSTOP);
1454 sigdelq(p, tx, SIGTSTP);
1455 sigdelq(p, tx, SIGTTOU);
1456 sigdelq(p, tx, SIGTTIN);
1457 sigdiffset(&tx->t_sig, &stopdefault);
1458 sigdiffset(&tx->t_extsig, &stopdefault);
1459 } while ((tx = tx->t_forw) != p->p_tlist);
1461 } else if (sigismember(&stopdefault, sig)) {
1462 if (PTOU(p)->u_signal[sig-1] == SIG_DFL &&
1463 (sig == SIGSTOP || !p->p_pgidp->pid_pgorphaned))
1464 p->p_flag &= ~SSCONT;
1465 sigdelq(p, NULL, SIGCONT);
1466 sigdelset(&p->p_sig, SIGCONT);
1467 sigdelset(&p->p_extsig, SIGCONT);
1468 if ((tx = p->p_tlist) != NULL) {
1469 do {
1470 sigdelq(p, tx, SIGCONT);
1471 sigdelset(&tx->t_sig, SIGCONT);
1472 sigdelset(&tx->t_extsig, SIGCONT);
1473 } while ((tx = tx->t_forw) != p->p_tlist);
1476 thread_lock(t);
1477 if (ISWAKEABLE(t) || ISWAITING(t)) {
1478 /* Set signaled sleeping/waiting lwp running */
1479 setrun_locked(t);
1480 } else if (t->t_state == TS_STOPPED && sig == SIGKILL) {
1481 /* If SIGKILL, set stopped lwp running */
1482 p->p_stopsig = 0;
1483 t->t_schedflag |= TS_XSTART | TS_PSTART;
1484 t->t_dtrace_stop = 0;
1485 setrun_locked(t);
1487 t->t_sig_check = 1; /* so ISSIG will be done */
1488 thread_unlock(t);
1490 * More jobcontrol side-effects.
1492 if (sig == SIGCONT && (tx = p->p_tlist) != NULL) {
1493 p->p_stopsig = 0;
1494 do {
1495 thread_lock(tx);
1496 if (tx->t_state == TS_STOPPED &&
1497 tx->t_whystop == PR_JOBCONTROL) {
1498 tx->t_schedflag |= TS_XSTART;
1499 setrun_locked(tx);
1501 thread_unlock(tx);
1502 } while ((tx = tx->t_forw) != p->p_tlist);
1505 return (error);
1509 pr_kill(prnode_t *pnp, int sig, cred_t *cr)
1511 int nsig = PROC_IS_BRANDED(curproc)? BROP(curproc)->b_nsig : NSIG;
1512 prcommon_t *pcp = pnp->pr_common;
1513 proc_t *p = pcp->prc_proc;
1514 k_siginfo_t info;
1516 if (sig <= 0 || sig >= nsig)
1517 return (EINVAL);
1519 bzero(&info, sizeof (info));
1520 info.si_signo = sig;
1521 info.si_code = SI_USER;
1522 info.si_pid = curproc->p_pid;
1523 info.si_ctid = PRCTID(curproc);
1524 info.si_zoneid = getzoneid();
1525 info.si_uid = crgetruid(cr);
1526 sigaddq(p, (pcp->prc_flags & PRC_LWP)?
1527 pcp->prc_thread : NULL, &info, KM_NOSLEEP);
1529 return (0);
1533 pr_unkill(prnode_t *pnp, int sig)
1535 int nsig = PROC_IS_BRANDED(curproc)? BROP(curproc)->b_nsig : NSIG;
1536 prcommon_t *pcp = pnp->pr_common;
1537 proc_t *p = pcp->prc_proc;
1538 sigqueue_t *infop = NULL;
1540 if (sig <= 0 || sig >= nsig || sig == SIGKILL)
1541 return (EINVAL);
1543 if (pcp->prc_flags & PRC_LWP)
1544 sigdeq(p, pcp->prc_thread, sig, &infop);
1545 else
1546 sigdeq(p, NULL, sig, &infop);
1548 if (infop)
1549 siginfofree(infop);
1551 return (0);
1555 pr_nice(proc_t *p, int nice, cred_t *cr)
1557 kthread_t *t;
1558 int err;
1559 int error = 0;
1561 t = p->p_tlist;
1562 do {
1563 ASSERT(!(t->t_proc_flag & TP_LWPEXIT));
1564 err = CL_DONICE(t, cr, nice, NULL);
1565 schedctl_set_cidpri(t);
1566 if (error == 0)
1567 error = err;
1568 } while ((t = t->t_forw) != p->p_tlist);
1570 return (error);
1573 void
1574 pr_setentryexit(proc_t *p, sysset_t *sysset, int entry)
1576 user_t *up = PTOU(p);
1578 if (entry) {
1579 prassignset(&up->u_entrymask, sysset);
1580 } else {
1581 prassignset(&up->u_exitmask, sysset);
1583 if (!prisempty(&up->u_entrymask) ||
1584 !prisempty(&up->u_exitmask)) {
1585 up->u_systrap = 1;
1586 p->p_proc_flag |= P_PR_TRACE;
1587 set_proc_sys(p); /* set pre and post-sys flags */
1588 } else {
1589 up->u_systrap = 0;
1590 if (sigisempty(&p->p_sigmask) &&
1591 prisempty(&p->p_fltmask))
1592 p->p_proc_flag &= ~P_PR_TRACE;
1596 #define ALLFLAGS \
1597 (PR_FORK|PR_RLC|PR_KLC|PR_ASYNC|PR_BPTADJ|PR_MSACCT|PR_MSFORK|\
1598 PR_PTRACE)
1601 pr_set(proc_t *p, long flags)
1603 if ((p->p_flag & SSYS) || p->p_as == &kas)
1604 return (EBUSY);
1606 if (flags & ~ALLFLAGS)
1607 return (EINVAL);
1609 if (flags & PR_FORK)
1610 p->p_proc_flag |= P_PR_FORK;
1611 if (flags & PR_RLC)
1612 p->p_proc_flag |= P_PR_RUNLCL;
1613 if (flags & PR_KLC)
1614 p->p_proc_flag |= P_PR_KILLCL;
1615 if (flags & PR_ASYNC)
1616 p->p_proc_flag |= P_PR_ASYNC;
1617 if (flags & PR_BPTADJ)
1618 p->p_proc_flag |= P_PR_BPTADJ;
1619 if (flags & PR_MSACCT)
1620 if ((p->p_flag & SMSACCT) == 0)
1621 estimate_msacct(p->p_tlist, gethrtime());
1622 if (flags & PR_MSFORK)
1623 p->p_flag |= SMSFORK;
1624 if (flags & PR_PTRACE) {
1625 p->p_proc_flag |= P_PR_PTRACE;
1626 /* ptraced process must die if parent dead */
1627 if (p->p_ppid == 1)
1628 sigtoproc(p, NULL, SIGKILL);
1631 return (0);
1635 pr_unset(proc_t *p, long flags)
1637 if ((p->p_flag & SSYS) || p->p_as == &kas)
1638 return (EBUSY);
1640 if (flags & ~ALLFLAGS)
1641 return (EINVAL);
1643 if (flags & PR_FORK)
1644 p->p_proc_flag &= ~P_PR_FORK;
1645 if (flags & PR_RLC)
1646 p->p_proc_flag &= ~P_PR_RUNLCL;
1647 if (flags & PR_KLC)
1648 p->p_proc_flag &= ~P_PR_KILLCL;
1649 if (flags & PR_ASYNC)
1650 p->p_proc_flag &= ~P_PR_ASYNC;
1651 if (flags & PR_BPTADJ)
1652 p->p_proc_flag &= ~P_PR_BPTADJ;
1653 if (flags & PR_MSACCT)
1654 disable_msacct(p);
1655 if (flags & PR_MSFORK)
1656 p->p_flag &= ~SMSFORK;
1657 if (flags & PR_PTRACE)
1658 p->p_proc_flag &= ~P_PR_PTRACE;
1660 return (0);
1663 static int
1664 pr_setfpregs(prnode_t *pnp, prfpregset_t *prfpregset)
1666 proc_t *p = pnp->pr_common->prc_proc;
1667 kthread_t *t = pr_thread(pnp); /* returns locked thread */
1669 if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
1670 thread_unlock(t);
1671 return (EBUSY);
1673 if (!prhasfp()) {
1674 thread_unlock(t);
1675 return (EINVAL); /* No FP support */
1678 /* drop p_lock while touching the lwp's stack */
1679 thread_unlock(t);
1680 mutex_exit(&p->p_lock);
1681 prsetprfpregs(ttolwp(t), prfpregset);
1682 mutex_enter(&p->p_lock);
1684 return (0);
1687 #ifdef _SYSCALL32_IMPL
1688 static int
1689 pr_setfpregs32(prnode_t *pnp, prfpregset32_t *prfpregset)
1691 proc_t *p = pnp->pr_common->prc_proc;
1692 kthread_t *t = pr_thread(pnp); /* returns locked thread */
1694 if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
1695 thread_unlock(t);
1696 return (EBUSY);
1698 if (!prhasfp()) {
1699 thread_unlock(t);
1700 return (EINVAL); /* No FP support */
1703 /* drop p_lock while touching the lwp's stack */
1704 thread_unlock(t);
1705 mutex_exit(&p->p_lock);
1706 prsetprfpregs32(ttolwp(t), prfpregset);
1707 mutex_enter(&p->p_lock);
1709 return (0);
1711 #endif /* _SYSCALL32_IMPL */
1713 #if defined(__sparc)
1714 /* ARGSUSED */
1715 static int
1716 pr_setxregs(prnode_t *pnp, prxregset_t *prxregset)
1718 proc_t *p = pnp->pr_common->prc_proc;
1719 kthread_t *t = pr_thread(pnp); /* returns locked thread */
1721 if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
1722 thread_unlock(t);
1723 return (EBUSY);
1725 thread_unlock(t);
1727 if (!prhasx(p))
1728 return (EINVAL); /* No extra register support */
1730 /* drop p_lock while touching the lwp's stack */
1731 mutex_exit(&p->p_lock);
1732 prsetprxregs(ttolwp(t), (caddr_t)prxregset);
1733 mutex_enter(&p->p_lock);
1735 return (0);
1738 static int
1739 pr_setasrs(prnode_t *pnp, asrset_t asrset)
1741 proc_t *p = pnp->pr_common->prc_proc;
1742 kthread_t *t = pr_thread(pnp); /* returns locked thread */
1744 if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
1745 thread_unlock(t);
1746 return (EBUSY);
1748 thread_unlock(t);
1750 /* drop p_lock while touching the lwp's stack */
1751 mutex_exit(&p->p_lock);
1752 prsetasregs(ttolwp(t), asrset);
1753 mutex_enter(&p->p_lock);
1755 return (0);
1757 #endif
1759 static int
1760 pr_setvaddr(prnode_t *pnp, caddr_t vaddr)
1762 proc_t *p = pnp->pr_common->prc_proc;
1763 kthread_t *t = pr_thread(pnp); /* returns locked thread */
1765 if (!ISTOPPED(t) && !VSTOPPED(t) && !DSTOPPED(t)) {
1766 thread_unlock(t);
1767 return (EBUSY);
1770 /* drop p_lock while touching the lwp's stack */
1771 thread_unlock(t);
1772 mutex_exit(&p->p_lock);
1773 prsvaddr(ttolwp(t), vaddr);
1774 mutex_enter(&p->p_lock);
1776 return (0);
1779 void
1780 pr_sethold(prnode_t *pnp, sigset_t *sp)
1782 proc_t *p = pnp->pr_common->prc_proc;
1783 kthread_t *t = pr_thread(pnp); /* returns locked thread */
1785 schedctl_finish_sigblock(t);
1786 sigutok(sp, &t->t_hold);
1787 if (ISWAKEABLE(t) &&
1788 (fsig(&p->p_sig, t) || fsig(&t->t_sig, t)))
1789 setrun_locked(t);
1790 t->t_sig_check = 1; /* so thread will see new holdmask */
1791 thread_unlock(t);
1794 void
1795 pr_setfault(proc_t *p, fltset_t *fltp)
1797 prassignset(&p->p_fltmask, fltp);
1798 if (!prisempty(&p->p_fltmask))
1799 p->p_proc_flag |= P_PR_TRACE;
1800 else if (sigisempty(&p->p_sigmask)) {
1801 user_t *up = PTOU(p);
1802 if (up->u_systrap == 0)
1803 p->p_proc_flag &= ~P_PR_TRACE;
1807 static int
1808 pr_clearsig(prnode_t *pnp)
1810 kthread_t *t = pr_thread(pnp); /* returns locked thread */
1811 klwp_t *lwp = ttolwp(t);
1813 thread_unlock(t);
1814 if (lwp->lwp_cursig == SIGKILL)
1815 return (EBUSY);
1818 * Discard current siginfo_t, if any.
1820 lwp->lwp_cursig = 0;
1821 lwp->lwp_extsig = 0;
1822 if (lwp->lwp_curinfo) {
1823 siginfofree(lwp->lwp_curinfo);
1824 lwp->lwp_curinfo = NULL;
1827 return (0);
1830 static int
1831 pr_clearflt(prnode_t *pnp)
1833 kthread_t *t = pr_thread(pnp); /* returns locked thread */
1835 thread_unlock(t);
1836 ttolwp(t)->lwp_curflt = 0;
1838 return (0);
1841 static int
1842 pr_watch(prnode_t *pnp, prwatch_t *pwp, int *unlocked)
1844 proc_t *p = pnp->pr_common->prc_proc;
1845 struct as *as = p->p_as;
1846 uintptr_t vaddr = pwp->pr_vaddr;
1847 size_t size = pwp->pr_size;
1848 int wflags = pwp->pr_wflags;
1849 ulong_t newpage = 0;
1850 struct watched_area *pwa;
1851 int error;
1853 *unlocked = 0;
1856 * Can't apply to a system process.
1858 if ((p->p_flag & SSYS) || p->p_as == &kas)
1859 return (EBUSY);
1862 * Verify that the address range does not wrap
1863 * and that only the proper flags were specified.
1865 if ((wflags & ~WA_TRAPAFTER) == 0)
1866 size = 0;
1867 if (vaddr + size < vaddr ||
1868 (wflags & ~(WA_READ|WA_WRITE|WA_EXEC|WA_TRAPAFTER)) != 0 ||
1869 ((wflags & ~WA_TRAPAFTER) != 0 && size == 0))
1870 return (EINVAL);
1873 * Don't let the address range go above as->a_userlimit.
1874 * There is no error here, just a limitation.
1876 if (vaddr >= (uintptr_t)as->a_userlimit)
1877 return (0);
1878 if (vaddr + size > (uintptr_t)as->a_userlimit)
1879 size = (uintptr_t)as->a_userlimit - vaddr;
1882 * Compute maximum number of pages this will add.
1884 if ((wflags & ~WA_TRAPAFTER) != 0) {
1885 ulong_t pagespan = (vaddr + size) - (vaddr & PAGEMASK);
1886 newpage = btopr(pagespan);
1887 if (newpage > 2 * prnwatch)
1888 return (E2BIG);
1892 * Force the process to be fully stopped.
1894 if (p == curproc) {
1895 prunlock(pnp);
1896 while (holdwatch() != 0)
1897 continue;
1898 if ((error = prlock(pnp, ZNO)) != 0) {
1899 continuelwps(p);
1900 *unlocked = 1;
1901 return (error);
1903 } else {
1904 pauselwps(p);
1905 while (pr_allstopped(p, 0) > 0) {
1907 * This cv/mutex pair is persistent even
1908 * if the process disappears after we
1909 * unmark it and drop p->p_lock.
1911 kcondvar_t *cv = &pr_pid_cv[p->p_slot];
1912 kmutex_t *mp = &p->p_lock;
1914 prunmark(p);
1915 (void) cv_wait(cv, mp);
1916 mutex_exit(mp);
1917 if ((error = prlock(pnp, ZNO)) != 0) {
1919 * Unpause the process if it exists.
1921 p = pr_p_lock(pnp);
1922 mutex_exit(&pr_pidlock);
1923 if (p != NULL) {
1924 unpauselwps(p);
1925 prunlock(pnp);
1927 *unlocked = 1;
1928 return (error);
1934 * Drop p->p_lock in order to perform the rest of this.
1935 * The process is still locked with the P_PR_LOCK flag.
1937 mutex_exit(&p->p_lock);
1939 pwa = kmem_alloc(sizeof (struct watched_area), KM_SLEEP);
1940 pwa->wa_vaddr = (caddr_t)vaddr;
1941 pwa->wa_eaddr = (caddr_t)vaddr + size;
1942 pwa->wa_flags = (ulong_t)wflags;
1944 error = ((pwa->wa_flags & ~WA_TRAPAFTER) == 0)?
1945 clear_watched_area(p, pwa) : set_watched_area(p, pwa);
1947 if (p == curproc) {
1948 setallwatch();
1949 mutex_enter(&p->p_lock);
1950 continuelwps(p);
1951 } else {
1952 mutex_enter(&p->p_lock);
1953 unpauselwps(p);
1956 return (error);
1959 /* jobcontrol stopped, but with a /proc directed stop in effect */
1960 #define JDSTOPPED(t) \
1961 ((t)->t_state == TS_STOPPED && \
1962 (t)->t_whystop == PR_JOBCONTROL && \
1963 ((t)->t_proc_flag & TP_PRSTOP))
1966 * pr_agent() creates the agent lwp. If the process is exiting while
1967 * we are creating an agent lwp, then exitlwps() waits until the
1968 * agent has been created using prbarrier().
1970 static int
1971 pr_agent(prnode_t *pnp, prgregset_t prgregset, int *unlocked)
1973 proc_t *p = pnp->pr_common->prc_proc;
1974 prcommon_t *pcp;
1975 kthread_t *t;
1976 kthread_t *ct;
1977 klwp_t *clwp;
1978 k_sigset_t smask;
1979 int cid;
1980 void *bufp = NULL;
1981 int error;
1983 *unlocked = 0;
1986 * Cannot create the /proc agent lwp if :-
1987 * - the process is not fully stopped or directed to stop.
1988 * - there is an agent lwp already.
1989 * - the process has been killed.
1990 * - the process is exiting.
1991 * - it's a vfork(2) parent.
1993 t = prchoose(p); /* returns locked thread */
1994 ASSERT(t != NULL);
1996 if ((!ISTOPPED(t) && !VSTOPPED(t) && !SUSPENDED(t) && !JDSTOPPED(t)) ||
1997 p->p_agenttp != NULL ||
1998 (p->p_flag & (SKILLED | SEXITING | SVFWAIT))) {
1999 thread_unlock(t);
2000 return (EBUSY);
2003 thread_unlock(t);
2004 mutex_exit(&p->p_lock);
2006 sigfillset(&smask);
2007 sigdiffset(&smask, &cantmask);
2008 clwp = lwp_create(lwp_rtt, NULL, 0, p, TS_STOPPED,
2009 t->t_pri, &smask, NOCLASS, 0);
2010 if (clwp == NULL) {
2011 mutex_enter(&p->p_lock);
2012 return (ENOMEM);
2014 prsetprregs(clwp, prgregset, 1);
2017 * Because abandoning the agent inside the target process leads to
2018 * a state that is essentially undebuggable, we record the psinfo of
2019 * the process creating the agent and hang that off of the lwp.
2021 clwp->lwp_spymaster = kmem_zalloc(sizeof (psinfo_t), KM_SLEEP);
2022 mutex_enter(&curproc->p_lock);
2023 prgetpsinfo(curproc, clwp->lwp_spymaster);
2024 mutex_exit(&curproc->p_lock);
2027 * We overload pr_time in the spymaster to denote the time at which the
2028 * agent was created.
2030 gethrestime(&clwp->lwp_spymaster->pr_time);
2032 retry:
2033 cid = t->t_cid;
2034 (void) CL_ALLOC(&bufp, cid, KM_SLEEP);
2035 mutex_enter(&p->p_lock);
2036 if (cid != t->t_cid) {
2038 * Someone just changed this thread's scheduling class,
2039 * so try pre-allocating the buffer again. Hopefully we
2040 * don't hit this often.
2042 mutex_exit(&p->p_lock);
2043 CL_FREE(cid, bufp);
2044 goto retry;
2047 clwp->lwp_ap = clwp->lwp_arg;
2048 clwp->lwp_eosys = NORMALRETURN;
2049 ct = lwptot(clwp);
2050 ct->t_clfuncs = t->t_clfuncs;
2051 CL_FORK(t, ct, bufp);
2052 ct->t_cid = t->t_cid;
2053 ct->t_proc_flag |= TP_PRSTOP;
2055 * Setting t_sysnum to zero causes post_syscall()
2056 * to bypass all syscall checks and go directly to
2057 * if (issig()) psig();
2058 * so that the agent lwp will stop in issig_forreal()
2059 * showing PR_REQUESTED.
2061 ct->t_sysnum = 0;
2062 ct->t_post_sys = 1;
2063 ct->t_sig_check = 1;
2064 p->p_agenttp = ct;
2065 ct->t_proc_flag &= ~TP_HOLDLWP;
2067 pcp = pnp->pr_pcommon;
2068 mutex_enter(&pcp->prc_mutex);
2070 lwp_create_done(ct);
2073 * Don't return until the agent is stopped on PR_REQUESTED.
2076 for (;;) {
2077 prunlock(pnp);
2078 *unlocked = 1;
2081 * Wait for the agent to stop and notify us.
2082 * If we've been interrupted, return that information.
2084 error = pr_wait(pcp, NULL, 0);
2085 if (error == EINTR) {
2086 error = 0;
2087 break;
2091 * Confirm that the agent LWP has stopped.
2094 if ((error = prlock(pnp, ZNO)) != 0)
2095 break;
2096 *unlocked = 0;
2099 * Since we dropped the lock on the process, the agent
2100 * may have disappeared or changed. Grab the current
2101 * agent and check fail if it has disappeared.
2103 if ((ct = p->p_agenttp) == NULL) {
2104 error = ENOENT;
2105 break;
2108 mutex_enter(&pcp->prc_mutex);
2109 thread_lock(ct);
2111 if (ISTOPPED(ct)) {
2112 thread_unlock(ct);
2113 mutex_exit(&pcp->prc_mutex);
2114 break;
2117 thread_unlock(ct);
2120 return (error ? error : -1);
2123 static int
2124 pr_rdwr(proc_t *p, enum uio_rw rw, priovec_t *pio)
2126 caddr_t base = (caddr_t)pio->pio_base;
2127 size_t cnt = pio->pio_len;
2128 uintptr_t offset = (uintptr_t)pio->pio_offset;
2129 struct uio auio;
2130 struct iovec aiov;
2131 int error = 0;
2133 if ((p->p_flag & SSYS) || p->p_as == &kas)
2134 error = EIO;
2135 else if ((base + cnt) < base || (offset + cnt) < offset)
2136 error = EINVAL;
2137 else if (cnt != 0) {
2138 aiov.iov_base = base;
2139 aiov.iov_len = cnt;
2141 auio.uio_loffset = offset;
2142 auio.uio_iov = &aiov;
2143 auio.uio_iovcnt = 1;
2144 auio.uio_resid = cnt;
2145 auio.uio_segflg = UIO_USERSPACE;
2146 auio.uio_llimit = (longlong_t)MAXOFFSET_T;
2147 auio.uio_fmode = FREAD|FWRITE;
2148 auio.uio_extflg = UIO_COPY_DEFAULT;
2150 mutex_exit(&p->p_lock);
2151 error = prusrio(p, rw, &auio, 0);
2152 mutex_enter(&p->p_lock);
2155 * We have no way to return the i/o count,
2156 * like read() or write() would do, so we
2157 * return an error if the i/o was truncated.
2159 if (auio.uio_resid != 0 && error == 0)
2160 error = EIO;
2163 return (error);
2166 static int
2167 pr_scred(proc_t *p, prcred_t *prcred, cred_t *cr, boolean_t dogrps)
2169 kthread_t *t;
2170 cred_t *oldcred;
2171 cred_t *newcred;
2172 uid_t oldruid;
2173 int error;
2174 zone_t *zone = crgetzone(cr);
2176 if (!VALID_UID(prcred->pr_euid, zone) ||
2177 !VALID_UID(prcred->pr_ruid, zone) ||
2178 !VALID_UID(prcred->pr_suid, zone) ||
2179 !VALID_GID(prcred->pr_egid, zone) ||
2180 !VALID_GID(prcred->pr_rgid, zone) ||
2181 !VALID_GID(prcred->pr_sgid, zone))
2182 return (EINVAL);
2184 if (dogrps) {
2185 int ngrp = prcred->pr_ngroups;
2186 int i;
2188 if (ngrp < 0 || ngrp > ngroups_max)
2189 return (EINVAL);
2191 for (i = 0; i < ngrp; i++) {
2192 if (!VALID_GID(prcred->pr_groups[i], zone))
2193 return (EINVAL);
2197 error = secpolicy_allow_setid(cr, prcred->pr_euid, B_FALSE);
2199 if (error == 0 && prcred->pr_ruid != prcred->pr_euid)
2200 error = secpolicy_allow_setid(cr, prcred->pr_ruid, B_FALSE);
2202 if (error == 0 && prcred->pr_suid != prcred->pr_euid &&
2203 prcred->pr_suid != prcred->pr_ruid)
2204 error = secpolicy_allow_setid(cr, prcred->pr_suid, B_FALSE);
2206 if (error)
2207 return (error);
2209 mutex_exit(&p->p_lock);
2211 /* hold old cred so it doesn't disappear while we dup it */
2212 mutex_enter(&p->p_crlock);
2213 crhold(oldcred = p->p_cred);
2214 mutex_exit(&p->p_crlock);
2215 newcred = crdup(oldcred);
2216 oldruid = crgetruid(oldcred);
2217 crfree(oldcred);
2219 /* Error checking done above */
2220 (void) crsetresuid(newcred, prcred->pr_ruid, prcred->pr_euid,
2221 prcred->pr_suid);
2222 (void) crsetresgid(newcred, prcred->pr_rgid, prcred->pr_egid,
2223 prcred->pr_sgid);
2225 if (dogrps) {
2226 (void) crsetgroups(newcred, prcred->pr_ngroups,
2227 prcred->pr_groups);
2231 mutex_enter(&p->p_crlock);
2232 oldcred = p->p_cred;
2233 p->p_cred = newcred;
2234 mutex_exit(&p->p_crlock);
2235 crfree(oldcred);
2238 * Keep count of processes per uid consistent.
2240 if (oldruid != prcred->pr_ruid) {
2241 zoneid_t zoneid = crgetzoneid(newcred);
2243 mutex_enter(&pidlock);
2244 upcount_dec(oldruid, zoneid);
2245 upcount_inc(prcred->pr_ruid, zoneid);
2246 mutex_exit(&pidlock);
2250 * Broadcast the cred change to the threads.
2252 mutex_enter(&p->p_lock);
2253 t = p->p_tlist;
2254 do {
2255 t->t_pre_sys = 1; /* so syscall will get new cred */
2256 } while ((t = t->t_forw) != p->p_tlist);
2258 return (0);
2262 * Change process credentials to specified zone. Used to temporarily
2263 * set a process to run in the global zone; only transitions between
2264 * the process's actual zone and the global zone are allowed.
2266 static int
2267 pr_szoneid(proc_t *p, zoneid_t zoneid, cred_t *cr)
2269 kthread_t *t;
2270 cred_t *oldcred;
2271 cred_t *newcred;
2272 zone_t *zptr;
2273 zoneid_t oldzoneid;
2275 if (secpolicy_zone_config(cr) != 0)
2276 return (EPERM);
2277 if (zoneid != GLOBAL_ZONEID && zoneid != p->p_zone->zone_id)
2278 return (EINVAL);
2279 if ((zptr = zone_find_by_id(zoneid)) == NULL)
2280 return (EINVAL);
2281 mutex_exit(&p->p_lock);
2282 mutex_enter(&p->p_crlock);
2283 oldcred = p->p_cred;
2284 crhold(oldcred);
2285 mutex_exit(&p->p_crlock);
2286 newcred = crdup(oldcred);
2287 oldzoneid = crgetzoneid(oldcred);
2288 crfree(oldcred);
2290 crsetzone(newcred, zptr);
2291 zone_rele(zptr);
2293 mutex_enter(&p->p_crlock);
2294 oldcred = p->p_cred;
2295 p->p_cred = newcred;
2296 mutex_exit(&p->p_crlock);
2297 crfree(oldcred);
2300 * The target process is changing zones (according to its cred), so
2301 * update the per-zone upcounts, which are based on process creds.
2303 if (oldzoneid != zoneid) {
2304 uid_t ruid = crgetruid(newcred);
2306 mutex_enter(&pidlock);
2307 upcount_dec(ruid, oldzoneid);
2308 upcount_inc(ruid, zoneid);
2309 mutex_exit(&pidlock);
2312 * Broadcast the cred change to the threads.
2314 mutex_enter(&p->p_lock);
2315 t = p->p_tlist;
2316 do {
2317 t->t_pre_sys = 1; /* so syscall will get new cred */
2318 } while ((t = t->t_forw) != p->p_tlist);
2320 return (0);
2323 static int
2324 pr_spriv(proc_t *p, prpriv_t *prpriv, cred_t *cr)
2326 kthread_t *t;
2327 int err;
2329 ASSERT(MUTEX_HELD(&p->p_lock));
2331 if ((err = priv_pr_spriv(p, prpriv, cr)) == 0) {
2333 * Broadcast the cred change to the threads.
2335 t = p->p_tlist;
2336 do {
2337 t->t_pre_sys = 1; /* so syscall will get new cred */
2338 } while ((t = t->t_forw) != p->p_tlist);
2341 return (err);
2345 * Return -1 if the process is the parent of a vfork(1) whose child has yet to
2346 * terminate or perform an exec(2).
2348 * Returns 0 if the process is fully stopped except for the current thread (if
2349 * we are operating on our own process), 1 otherwise.
2351 * If the watchstop flag is set, then we ignore threads with TP_WATCHSTOP set.
2352 * See holdwatch() for details.
2355 pr_allstopped(proc_t *p, int watchstop)
2357 kthread_t *t;
2358 int rv = 0;
2360 ASSERT(MUTEX_HELD(&p->p_lock));
2362 if (p->p_flag & SVFWAIT) /* waiting for vfork'd child to exec */
2363 return (-1);
2365 if ((t = p->p_tlist) != NULL) {
2366 do {
2367 if (t == curthread || VSTOPPED(t) ||
2368 (watchstop && (t->t_proc_flag & TP_WATCHSTOP)))
2369 continue;
2370 thread_lock(t);
2371 switch (t->t_state) {
2372 case TS_ZOMB:
2373 case TS_STOPPED:
2374 break;
2375 case TS_SLEEP:
2376 if (!(t->t_flag & T_WAKEABLE) ||
2377 t->t_wchan0 == NULL)
2378 rv = 1;
2379 break;
2380 default:
2381 rv = 1;
2382 break;
2384 thread_unlock(t);
2385 } while (rv == 0 && (t = t->t_forw) != p->p_tlist);
2388 return (rv);
2392 * Cause all lwps in the process to pause (for watchpoint operations).
2394 static void
2395 pauselwps(proc_t *p)
2397 kthread_t *t;
2399 ASSERT(MUTEX_HELD(&p->p_lock));
2400 ASSERT(p != curproc);
2402 if ((t = p->p_tlist) != NULL) {
2403 do {
2404 thread_lock(t);
2405 t->t_proc_flag |= TP_PAUSE;
2406 aston(t);
2407 if ((ISWAKEABLE(t) && (t->t_wchan0 == NULL)) ||
2408 ISWAITING(t)) {
2409 setrun_locked(t);
2411 prpokethread(t);
2412 thread_unlock(t);
2413 } while ((t = t->t_forw) != p->p_tlist);
2418 * undo the effects of pauselwps()
2420 static void
2421 unpauselwps(proc_t *p)
2423 kthread_t *t;
2425 ASSERT(MUTEX_HELD(&p->p_lock));
2426 ASSERT(p != curproc);
2428 if ((t = p->p_tlist) != NULL) {
2429 do {
2430 thread_lock(t);
2431 t->t_proc_flag &= ~TP_PAUSE;
2432 if (t->t_state == TS_STOPPED) {
2433 t->t_schedflag |= TS_UNPAUSE;
2434 t->t_dtrace_stop = 0;
2435 setrun_locked(t);
2437 thread_unlock(t);
2438 } while ((t = t->t_forw) != p->p_tlist);
2443 * Cancel all watched areas. Called from prclose().
2445 proc_t *
2446 pr_cancel_watch(prnode_t *pnp)
2448 proc_t *p = pnp->pr_pcommon->prc_proc;
2449 struct as *as;
2450 kthread_t *t;
2452 ASSERT(MUTEX_HELD(&p->p_lock) && (p->p_proc_flag & P_PR_LOCK));
2454 if (!pr_watch_active(p))
2455 return (p);
2458 * Pause the process before dealing with the watchpoints.
2460 if (p == curproc) {
2461 prunlock(pnp);
2462 while (holdwatch() != 0)
2463 continue;
2464 p = pr_p_lock(pnp);
2465 mutex_exit(&pr_pidlock);
2466 ASSERT(p == curproc);
2467 } else {
2468 pauselwps(p);
2469 while (p != NULL && pr_allstopped(p, 0) > 0) {
2471 * This cv/mutex pair is persistent even
2472 * if the process disappears after we
2473 * unmark it and drop p->p_lock.
2475 kcondvar_t *cv = &pr_pid_cv[p->p_slot];
2476 kmutex_t *mp = &p->p_lock;
2478 prunmark(p);
2479 (void) cv_wait(cv, mp);
2480 mutex_exit(mp);
2481 p = pr_p_lock(pnp); /* NULL if process disappeared */
2482 mutex_exit(&pr_pidlock);
2486 if (p == NULL) /* the process disappeared */
2487 return (NULL);
2489 ASSERT(p == pnp->pr_pcommon->prc_proc);
2490 ASSERT(MUTEX_HELD(&p->p_lock) && (p->p_proc_flag & P_PR_LOCK));
2492 if (pr_watch_active(p)) {
2493 pr_free_watchpoints(p);
2494 if ((t = p->p_tlist) != NULL) {
2495 do {
2496 watch_disable(t);
2498 } while ((t = t->t_forw) != p->p_tlist);
2502 if ((as = p->p_as) != NULL) {
2503 avl_tree_t *tree;
2504 struct watched_page *pwp;
2507 * If this is the parent of a vfork, the watched page
2508 * list has been moved temporarily to p->p_wpage.
2510 if (avl_numnodes(&p->p_wpage) != 0)
2511 tree = &p->p_wpage;
2512 else
2513 tree = &as->a_wpage;
2515 mutex_exit(&p->p_lock);
2516 AS_LOCK_ENTER(as, RW_WRITER);
2518 for (pwp = avl_first(tree); pwp != NULL;
2519 pwp = AVL_NEXT(tree, pwp)) {
2520 pwp->wp_read = 0;
2521 pwp->wp_write = 0;
2522 pwp->wp_exec = 0;
2523 if ((pwp->wp_flags & WP_SETPROT) == 0) {
2524 pwp->wp_flags |= WP_SETPROT;
2525 pwp->wp_prot = pwp->wp_oprot;
2526 pwp->wp_list = p->p_wprot;
2527 p->p_wprot = pwp;
2531 AS_LOCK_EXIT(as);
2532 mutex_enter(&p->p_lock);
2536 * Unpause the process now.
2538 if (p == curproc)
2539 continuelwps(p);
2540 else
2541 unpauselwps(p);
2543 return (p);