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]
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>
34 #include <sys/param.h>
35 #include <sys/cmn_err.h>
37 #include <sys/policy.h>
38 #include <sys/debug.h>
39 #include <sys/errno.h>
41 #include <sys/inline.h>
44 #include <sys/brand.h>
45 #include <sys/regset.h>
46 #include <sys/sysmacros.h>
47 #include <sys/systm.h>
49 #include <sys/vnode.h>
50 #include <sys/signal.h>
53 #include <sys/class.h>
54 #include <sys/fault.h>
55 #include <sys/syscall.h>
56 #include <sys/procfs.h>
58 #include <sys/copyops.h>
59 #include <sys/schedctl.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
*);
68 static int pr_setxregs(prnode_t
*, prxregset_t
*);
69 static int pr_setasrs(prnode_t
*, asrset_t
);
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
*);
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 */
96 prxregset_t prxregset
; /* PCSXREG */
97 asrset_t asrset
; /* PCSASRS */
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 */
106 static int pr_control(long, arg_t
*, prnode_t
*, cred_t
*);
109 ctlsize(long cmd
, size_t resid
, arg_t
*argp
)
111 size_t size
= sizeof (long);
124 size
+= sizeof (siginfo_t
);
127 size
+= sizeof (long);
132 size
+= sizeof (long);
137 size
+= sizeof (ulong_t
);
140 size
+= sizeof (caddr_t
);
144 size
+= sizeof (sigset_t
);
147 size
+= sizeof (fltset_t
);
151 size
+= sizeof (sysset_t
);
155 size
+= sizeof (prgregset_t
);
158 size
+= sizeof (prfpregset_t
);
162 size
+= sizeof (prxregset_t
);
165 size
+= sizeof (asrset_t
);
169 size
+= sizeof (prwatch_t
);
173 size
+= sizeof (priovec_t
);
176 size
+= sizeof (prcred_t
);
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
))
185 ngrp
= argp
->prcred
.pr_ngroups
;
186 if (ngrp
< 0 || ngrp
> ngroups_max
)
189 /* The result can be smaller than sizeof (prcred_t) */
190 size
+= sizeof (prcred_t
) - sizeof (gid_t
);
191 size
+= ngrp
* sizeof (gid_t
);
194 if (resid
>= size
+ sizeof (prpriv_t
))
195 size
+= priv_prgetprivsize(&argp
->prpriv
);
200 size
+= sizeof (long);
206 /* Round up to a multiple of long, unless exact amount written */
208 rnd
= size
& (sizeof (long) - 1);
211 size
+= sizeof (long) - rnd
;
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
];
232 prnode_t
*pnp
= VTOP(vp
);
236 while (uiop
->uio_resid
) {
238 * Read several commands in one gulp.
241 if (resid
) { /* move incomplete command to front of buffer */
244 if (resid
>= sizeof (buf
))
246 tail
= (long *)((char *)buf
+ sizeof (buf
) - resid
);
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
))
256 resid
+= (char *)bufp
- (char *)buf
;
259 do { /* loop over commands in buffer */
261 arg_t
*argp
= (arg_t
*)&bufp
[1];
263 size
= ctlsize(cmd
, resid
, argp
);
264 if (size
== 0) /* incomplete or invalid command */
267 * Perform the specified control operation.
270 if ((error
= prlock(pnp
, ZNO
)) != 0)
274 if (error
= pr_control(cmd
, argp
, pnp
, cr
)) {
275 if (error
== -1) /* -1 is timeout */
280 bufp
= (long *)((char *)bufp
+ size
);
281 } while ((resid
-= size
) != 0);
288 return (resid
? EINVAL
: 0);
292 pr_control(long cmd
, arg_t
*argp
, prnode_t
*pnp
, cred_t
*cr
)
302 pcp
= pnp
->pr_common
;
306 /* System processes defy control. */
307 if (p
->p_flag
& SSYS
) {
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 */
326 * Can't apply to a system process.
328 if (p
->p_as
== &kas
) {
333 if (cmd
== PCSTOP
|| cmd
== PCDSTOP
)
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
)
351 timeo
= (cmd
== PCTWSTOP
)? (time_t)argp
->timeo
: 0;
352 if ((error
= pr_wait_stop(pnp
, timeo
)) != 0)
358 case PCRUN
: /* make lwp or process runnable */
359 error
= pr_setrun(pnp
, argp
->flags
);
362 case PCSTRACE
: /* set signal trace mask */
363 pr_settrace(p
, &argp
->sigset
);
366 case PCSSIG
: /* set current signal */
367 error
= pr_setsig(pnp
, &argp
->siginfo
);
368 if (argp
->siginfo
.si_signo
== SIGKILL
&& error
== 0) {
375 case PCKILL
: /* send signal */
376 error
= pr_kill(pnp
, (int)argp
->sig
, cr
);
377 if (error
== 0 && argp
->sig
== SIGKILL
) {
384 case PCUNKILL
: /* delete a pending signal */
385 error
= pr_unkill(pnp
, (int)argp
->sig
);
388 case PCNICE
: /* set nice priority */
389 error
= pr_nice(p
, (int)argp
->nice
, cr
);
392 case PCSENTRY
: /* set syscall entry bit mask */
393 case PCSEXIT
: /* set syscall exit bit mask */
394 pr_setentryexit(p
, &argp
->sysset
, cmd
== PCSENTRY
);
397 case PCSET
: /* set process flags */
398 error
= pr_set(p
, argp
->flags
);
401 case PCUNSET
: /* unset process flags */
402 error
= pr_unset(p
, argp
->flags
);
405 case PCSREG
: /* set general registers */
407 kthread_t
*t
= pr_thread(pnp
);
409 if (!ISTOPPED(t
) && !VSTOPPED(t
) && !DSTOPPED(t
)) {
414 mutex_exit(&p
->p_lock
);
415 prsetprregs(ttolwp(t
), argp
->prgregset
, 0);
416 mutex_enter(&p
->p_lock
);
421 case PCSFPREG
: /* set floating-point registers */
422 error
= pr_setfpregs(pnp
, &argp
->prfpregset
);
425 case PCSXREG
: /* set extra registers */
427 error
= pr_setxregs(pnp
, &argp
->prxregset
);
434 case PCSASRS
: /* set ancillary state registers */
435 error
= pr_setasrs(pnp
, argp
->asrset
);
439 case PCSVADDR
: /* set virtual address at which to resume */
440 error
= pr_setvaddr(pnp
, argp
->vaddr
);
443 case PCSHOLD
: /* set signal-hold mask */
444 pr_sethold(pnp
, &argp
->sigset
);
447 case PCSFAULT
: /* set mask of traced faults */
448 pr_setfault(p
, &argp
->fltset
);
451 case PCCSIG
: /* clear current signal */
452 error
= pr_clearsig(pnp
);
455 case PCCFAULT
: /* clear current fault */
456 error
= pr_clearflt(pnp
);
459 case PCWATCH
: /* set or clear watched areas */
460 error
= pr_watch(pnp
, &argp
->prwatch
, &unlocked
);
461 if (error
&& unlocked
)
465 case PCAGENT
: /* create the /proc agent lwp in the target process */
466 error
= pr_agent(pnp
, argp
->prgregset
, &unlocked
);
467 if (error
&& unlocked
)
471 case PCREAD
: /* read from the address space */
472 error
= pr_rdwr(p
, UIO_READ
, &argp
->priovec
);
475 case PCWRITE
: /* write to the address space */
476 error
= pr_rdwr(p
, UIO_WRITE
, &argp
->priovec
);
479 case PCSCRED
: /* set the process credentials */
481 error
= pr_scred(p
, &argp
->prcred
, cr
, cmd
== PCSCREDX
);
484 case PCSPRIV
: /* set the process privileges */
485 error
= pr_spriv(p
, &argp
->prpriv
, cr
);
487 case PCSZONE
: /* set the process's zoneid credentials */
488 error
= pr_szoneid(p
, (zoneid_t
)argp
->przoneid
, cr
);
497 #ifdef _SYSCALL32_IMPL
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 */
512 prxregset_t prxregset
; /* PCSXREG */
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 */
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.
530 ctlsize32(int32_t cmd
, size_t resid
, arg32_t
*argp
)
532 size_t size
= sizeof (int32_t);
545 size
+= sizeof (siginfo32_t
);
548 size
+= sizeof (int32_t);
553 size
+= sizeof (int32_t);
558 size
+= sizeof (uint32_t);
561 size
+= sizeof (caddr32_t
);
565 size
+= sizeof (sigset_t
);
568 size
+= sizeof (fltset_t
);
572 size
+= sizeof (sysset_t
);
576 size
+= sizeof (prgregset32_t
);
579 size
+= sizeof (prfpregset32_t
);
583 size
+= sizeof (prxregset_t
);
587 size
+= sizeof (prwatch32_t
);
591 size
+= sizeof (priovec32_t
);
594 size
+= sizeof (prcred32_t
);
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
))
603 ngrp
= argp
->prcred
.pr_ngroups
;
604 if (ngrp
< 0 || ngrp
> ngroups_max
)
607 /* The result can be smaller than sizeof (prcred32_t) */
608 size
+= sizeof (prcred32_t
) - sizeof (gid32_t
);
609 size
+= ngrp
* sizeof (gid32_t
);
612 if (resid
>= size
+ sizeof (prpriv_t
))
613 size
+= priv_prgetprivsize(&argp
->prpriv
);
618 size
+= sizeof (int32_t);
624 /* Round up to a multiple of int32_t */
625 rnd
= size
& (sizeof (int32_t) - 1);
628 size
+= sizeof (int32_t) - rnd
;
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
];
649 prnode_t
*pnp
= VTOP(vp
);
653 while (uiop
->uio_resid
) {
655 * Read several commands in one gulp.
658 if (resid
) { /* move incomplete command to front of buffer */
661 if (resid
>= sizeof (buf
))
663 tail
= (int32_t *)((char *)buf
+ sizeof (buf
) - resid
);
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
))
673 resid
+= (char *)bufp
- (char *)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 */
684 * Perform the specified control operation.
687 if ((error
= prlock(pnp
, ZNO
)) != 0)
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 */
710 bufp
= (int32_t *)((char *)bufp
+ size
);
711 } while ((resid
-= size
) != 0);
718 return (resid
? EINVAL
: 0);
722 pr_control32(int32_t cmd
, arg32_t
*argp
, prnode_t
*pnp
, cred_t
*cr
)
732 pcp
= pnp
->pr_common
;
736 if (p
->p_flag
& SSYS
) {
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 */
755 * Can't apply to a system process.
757 if (p
->p_as
== &kas
) {
762 if (cmd
== PCSTOP
|| cmd
== PCDSTOP
)
769 * If an lwp is waiting for itself or its process,
770 * don't wait. The lwp will never see the fact that
773 if ((pcp
->prc_flags
& PRC_LWP
)?
774 (pcp
->prc_thread
== curthread
) : (p
== curproc
)) {
775 if (cmd
== PCWSTOP
|| cmd
== PCTWSTOP
)
780 timeo
= (cmd
== PCTWSTOP
)? (time_t)argp
->timeo
: 0;
781 if ((error
= pr_wait_stop(pnp
, timeo
)) != 0)
787 case PCRUN
: /* make lwp or process runnable */
788 error
= pr_setrun(pnp
, (ulong_t
)argp
->flags
);
791 case PCSTRACE
: /* set signal trace mask */
792 pr_settrace(p
, &argp
->sigset
);
795 case PCSSIG
: /* set current signal */
796 if (PROCESS_NOT_32BIT(p
))
799 int sig
= (int)argp
->siginfo
.si_signo
;
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) {
813 case PCKILL
: /* send signal */
814 error
= pr_kill(pnp
, (int)argp
->sig
, cr
);
815 if (error
== 0 && argp
->sig
== SIGKILL
) {
822 case PCUNKILL
: /* delete a pending signal */
823 error
= pr_unkill(pnp
, (int)argp
->sig
);
826 case PCNICE
: /* set nice priority */
827 error
= pr_nice(p
, (int)argp
->nice
, cr
);
830 case PCSENTRY
: /* set syscall entry bit mask */
831 case PCSEXIT
: /* set syscall exit bit mask */
832 pr_setentryexit(p
, &argp
->sysset
, cmd
== PCSENTRY
);
835 case PCSET
: /* set process flags */
836 error
= pr_set(p
, (long)argp
->flags
);
839 case PCUNSET
: /* unset process flags */
840 error
= pr_unset(p
, (long)argp
->flags
);
843 case PCSREG
: /* set general registers */
844 if (PROCESS_NOT_32BIT(p
))
847 kthread_t
*t
= pr_thread(pnp
);
849 if (!ISTOPPED(t
) && !VSTOPPED(t
) && !DSTOPPED(t
)) {
853 prgregset_t prgregset
;
854 klwp_t
*lwp
= ttolwp(t
);
857 mutex_exit(&p
->p_lock
);
858 prgregset_32ton(lwp
, argp
->prgregset
,
860 prsetprregs(lwp
, prgregset
, 0);
861 mutex_enter(&p
->p_lock
);
866 case PCSFPREG
: /* set floating-point registers */
867 if (PROCESS_NOT_32BIT(p
))
870 error
= pr_setfpregs32(pnp
, &argp
->prfpregset
);
873 case PCSXREG
: /* set extra registers */
875 if (PROCESS_NOT_32BIT(p
))
878 error
= pr_setxregs(pnp
, &argp
->prxregset
);
884 case PCSVADDR
: /* set virtual address at which to resume */
885 if (PROCESS_NOT_32BIT(p
))
888 error
= pr_setvaddr(pnp
,
889 (caddr_t
)(uintptr_t)argp
->vaddr
);
892 case PCSHOLD
: /* set signal-hold mask */
893 pr_sethold(pnp
, &argp
->sigset
);
896 case PCSFAULT
: /* set mask of traced faults */
897 pr_setfault(p
, &argp
->fltset
);
900 case PCCSIG
: /* clear current signal */
901 error
= pr_clearsig(pnp
);
904 case PCCFAULT
: /* clear current fault */
905 error
= pr_clearflt(pnp
);
908 case PCWATCH
: /* set or clear watched areas */
909 if (PROCESS_NOT_32BIT(p
))
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
)
924 case PCAGENT
: /* create the /proc agent lwp in the target process */
925 if (PROCESS_NOT_32BIT(p
))
928 prgregset_t prgregset
;
929 kthread_t
*t
= pr_thread(pnp
);
930 klwp_t
*lwp
= ttolwp(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
)
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
))
946 enum uio_rw rw
= (cmd
== PCREAD
)? UIO_READ
: UIO_WRITE
;
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
);
958 case PCSCRED
: /* set the process credentials */
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
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
);
975 case PCSPRIV
: /* set the process privileges */
976 error
= pr_spriv(p
, &argp
->prpriv
, cr
);
979 case PCSZONE
: /* set the process's zoneid */
980 error
= pr_szoneid(p
, (zoneid_t
)argp
->przoneid
, cr
);
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).
996 pr_thread(prnode_t
*pnp
)
998 prcommon_t
*pcp
= pnp
->pr_common
;
1001 if (pcp
->prc_flags
& PRC_LWP
) {
1002 t
= pcp
->prc_thread
;
1006 proc_t
*p
= pcp
->prc_proc
;
1007 t
= prchoose(p
); /* returns locked thread */
1015 * Direct the process or lwp to stop.
1018 pr_stop(prnode_t
*pnp
)
1020 prcommon_t
*pcp
= pnp
->pr_common
;
1021 proc_t
*p
= pcp
->prc_proc
;
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
;
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 */
1056 if (ISWAKEABLE(t
)) {
1057 if (t
->t_wchan0
== NULL
)
1059 else if (!VSTOPPED(t
)) {
1061 * Mark it virtually stopped.
1063 t
->t_proc_flag
|= TP_PRVSTOP
;
1068 * force the thread into the kernel
1069 * if it is not already there.
1074 (vp
= p
->p_lwpdir
[t
->t_dslot
].ld_entry
->le_trace
) != NULL
)
1076 if (pcp
->prc_flags
& PRC_LWP
)
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
;
1100 timestruc_t
*rqtp
= NULL
;
1105 if (timeo
> 0) { /* millisecond timeout */
1107 * Determine the precise future time of the requested timeout.
1111 timecheck
= timechanged
;
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
;
1123 while (!ISTOPPED(t
) && !VSTOPPED(t
)) {
1125 mutex_enter(&pcp
->prc_mutex
);
1127 error
= pr_wait(pcp
, rqtp
, timecheck
);
1128 if (error
) /* -1 is timeout */
1130 if ((error
= prlock(pnp
, ZNO
)) != 0)
1132 ASSERT(p
== pcp
->prc_proc
);
1133 ASSERT(t
== pcp
->prc_thread
);
1137 } else { /* process file descriptor */
1138 t
= prchoose(p
); /* returns locked thread */
1140 ASSERT(MUTEX_HELD(&p
->p_lock
));
1141 while ((!ISTOPPED(t
) && !VSTOPPED(t
) && !SUSPENDED(t
)) ||
1142 (p
->p_flag
& SEXITLWPS
)) {
1144 mutex_enter(&pcp
->prc_mutex
);
1146 error
= pr_wait(pcp
, rqtp
, timecheck
);
1147 if (error
) /* -1 is timeout */
1149 if ((error
= prlock(pnp
, ZNO
)) != 0)
1151 ASSERT(p
== pcp
->prc_proc
);
1152 t
= prchoose(p
); /* returns locked t */
1158 ASSERT(!(pcp
->prc_flags
& PRC_DESTROY
) && p
->p_stat
!= SZOMB
&&
1159 t
!= NULL
&& t
->t_state
!= TS_ZOMB
);
1165 pr_setrun(prnode_t
*pnp
, ulong_t flags
)
1167 prcommon_t
*pcp
= pnp
->pr_common
;
1168 proc_t
*p
= pcp
->prc_proc
;
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
)))) {
1186 if (flags
& ~(PRCSIG
|PRCFAULT
|PRSTEP
|PRSTOP
|PRSABORT
))
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
);
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;
1218 if ((pcp
->prc_flags
& PRC_LWP
) || (flags
& (PRSTEP
|PRSTOP
))) {
1220 * Here, we are dealing with a single lwp.
1223 t
->t_schedflag
|= TS_PSTART
;
1224 t
->t_dtrace_stop
= 0;
1226 } else if (flags
& PRSABORT
) {
1228 ~(TP_PRSTOP
|TP_PRVSTOP
|TP_STOPPING
);
1230 } else if (!(flags
& PRSTOP
)) {
1232 ~(TP_PRSTOP
|TP_PRVSTOP
|TP_STOPPING
);
1237 * Here, we are dealing with the whole process.
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
;
1258 t
= prchoose(p
); /* returns locked t */
1259 ASSERT(ISTOPPED(t
) || VSTOPPED(t
));
1261 t
->t_whystop
== PR_REQUESTED
) {
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.
1276 tx
->t_schedflag
|= TS_PSTART
;
1277 t
->t_dtrace_stop
= 0;
1284 * No event of interest; set all of the lwps running.
1286 if (flags
& PRSABORT
) {
1288 ~(TP_PRSTOP
|TP_PRVSTOP
|TP_STOPPING
);
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 */
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
);
1323 * Make all threads in the process runnable.
1326 allsetrun(proc_t
*p
)
1330 ASSERT(MUTEX_HELD(&p
->p_lock
));
1332 if ((t
= p
->p_tlist
) != NULL
) {
1335 ASSERT(!(t
->t_proc_flag
& TP_LWPEXIT
));
1336 t
->t_proc_flag
&= ~(TP_PRSTOP
|TP_PRVSTOP
|TP_STOPPING
);
1338 t
->t_schedflag
|= TS_PSTART
;
1339 t
->t_dtrace_stop
= 0;
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.
1353 pr_wait_die(prnode_t
*pnp
)
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
))
1362 mutex_exit(&pidlock
);
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
;
1390 t
= pr_thread(pnp
); /* returns locked thread */
1393 if (sig
< 0 || sig
>= nsig
)
1394 /* Zero allowed here */
1396 else if (lwp
->lwp_cursig
== SIGKILL
)
1397 /* "can't happen", but just in case */
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
;
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
;
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
=
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
) {
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
) {
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
);
1477 if (ISWAKEABLE(t
) || ISWAITING(t
)) {
1478 /* Set signaled sleeping/waiting lwp running */
1480 } else if (t
->t_state
== TS_STOPPED
&& sig
== SIGKILL
) {
1481 /* If SIGKILL, set stopped lwp running */
1483 t
->t_schedflag
|= TS_XSTART
| TS_PSTART
;
1484 t
->t_dtrace_stop
= 0;
1487 t
->t_sig_check
= 1; /* so ISSIG will be done */
1490 * More jobcontrol side-effects.
1492 if (sig
== SIGCONT
&& (tx
= p
->p_tlist
) != NULL
) {
1496 if (tx
->t_state
== TS_STOPPED
&&
1497 tx
->t_whystop
== PR_JOBCONTROL
) {
1498 tx
->t_schedflag
|= TS_XSTART
;
1502 } while ((tx
= tx
->t_forw
) != p
->p_tlist
);
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
;
1516 if (sig
<= 0 || sig
>= nsig
)
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
);
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
)
1543 if (pcp
->prc_flags
& PRC_LWP
)
1544 sigdeq(p
, pcp
->prc_thread
, sig
, &infop
);
1546 sigdeq(p
, NULL
, sig
, &infop
);
1555 pr_nice(proc_t
*p
, int nice
, cred_t
*cr
)
1563 ASSERT(!(t
->t_proc_flag
& TP_LWPEXIT
));
1564 err
= CL_DONICE(t
, cr
, nice
, NULL
);
1565 schedctl_set_cidpri(t
);
1568 } while ((t
= t
->t_forw
) != p
->p_tlist
);
1574 pr_setentryexit(proc_t
*p
, sysset_t
*sysset
, int entry
)
1576 user_t
*up
= PTOU(p
);
1579 prassignset(&up
->u_entrymask
, sysset
);
1581 prassignset(&up
->u_exitmask
, sysset
);
1583 if (!prisempty(&up
->u_entrymask
) ||
1584 !prisempty(&up
->u_exitmask
)) {
1586 p
->p_proc_flag
|= P_PR_TRACE
;
1587 set_proc_sys(p
); /* set pre and post-sys flags */
1590 if (sigisempty(&p
->p_sigmask
) &&
1591 prisempty(&p
->p_fltmask
))
1592 p
->p_proc_flag
&= ~P_PR_TRACE
;
1597 (PR_FORK|PR_RLC|PR_KLC|PR_ASYNC|PR_BPTADJ|PR_MSACCT|PR_MSFORK|\
1601 pr_set(proc_t
*p
, long flags
)
1603 if ((p
->p_flag
& SSYS
) || p
->p_as
== &kas
)
1606 if (flags
& ~ALLFLAGS
)
1609 if (flags
& PR_FORK
)
1610 p
->p_proc_flag
|= P_PR_FORK
;
1612 p
->p_proc_flag
|= P_PR_RUNLCL
;
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 */
1628 sigtoproc(p
, NULL
, SIGKILL
);
1635 pr_unset(proc_t
*p
, long flags
)
1637 if ((p
->p_flag
& SSYS
) || p
->p_as
== &kas
)
1640 if (flags
& ~ALLFLAGS
)
1643 if (flags
& PR_FORK
)
1644 p
->p_proc_flag
&= ~P_PR_FORK
;
1646 p
->p_proc_flag
&= ~P_PR_RUNLCL
;
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
)
1655 if (flags
& PR_MSFORK
)
1656 p
->p_flag
&= ~SMSFORK
;
1657 if (flags
& PR_PTRACE
)
1658 p
->p_proc_flag
&= ~P_PR_PTRACE
;
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
)) {
1675 return (EINVAL
); /* No FP support */
1678 /* drop p_lock while touching the lwp's stack */
1680 mutex_exit(&p
->p_lock
);
1681 prsetprfpregs(ttolwp(t
), prfpregset
);
1682 mutex_enter(&p
->p_lock
);
1687 #ifdef _SYSCALL32_IMPL
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
)) {
1700 return (EINVAL
); /* No FP support */
1703 /* drop p_lock while touching the lwp's stack */
1705 mutex_exit(&p
->p_lock
);
1706 prsetprfpregs32(ttolwp(t
), prfpregset
);
1707 mutex_enter(&p
->p_lock
);
1711 #endif /* _SYSCALL32_IMPL */
1713 #if defined(__sparc)
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
)) {
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
);
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
)) {
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
);
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
)) {
1770 /* drop p_lock while touching the lwp's stack */
1772 mutex_exit(&p
->p_lock
);
1773 prsvaddr(ttolwp(t
), vaddr
);
1774 mutex_enter(&p
->p_lock
);
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
)))
1790 t
->t_sig_check
= 1; /* so thread will see new holdmask */
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
;
1808 pr_clearsig(prnode_t
*pnp
)
1810 kthread_t
*t
= pr_thread(pnp
); /* returns locked thread */
1811 klwp_t
*lwp
= ttolwp(t
);
1814 if (lwp
->lwp_cursig
== SIGKILL
)
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
;
1831 pr_clearflt(prnode_t
*pnp
)
1833 kthread_t
*t
= pr_thread(pnp
); /* returns locked thread */
1836 ttolwp(t
)->lwp_curflt
= 0;
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
;
1856 * Can't apply to a system process.
1858 if ((p
->p_flag
& SSYS
) || p
->p_as
== &kas
)
1862 * Verify that the address range does not wrap
1863 * and that only the proper flags were specified.
1865 if ((wflags
& ~WA_TRAPAFTER
) == 0)
1867 if (vaddr
+ size
< vaddr
||
1868 (wflags
& ~(WA_READ
|WA_WRITE
|WA_EXEC
|WA_TRAPAFTER
)) != 0 ||
1869 ((wflags
& ~WA_TRAPAFTER
) != 0 && size
== 0))
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
)
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
)
1892 * Force the process to be fully stopped.
1896 while (holdwatch() != 0)
1898 if ((error
= prlock(pnp
, ZNO
)) != 0) {
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
;
1915 (void) cv_wait(cv
, mp
);
1917 if ((error
= prlock(pnp
, ZNO
)) != 0) {
1919 * Unpause the process if it exists.
1922 mutex_exit(&pr_pidlock
);
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
);
1949 mutex_enter(&p
->p_lock
);
1952 mutex_enter(&p
->p_lock
);
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().
1971 pr_agent(prnode_t
*pnp
, prgregset_t prgregset
, int *unlocked
)
1973 proc_t
*p
= pnp
->pr_common
->prc_proc
;
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 */
1996 if ((!ISTOPPED(t
) && !VSTOPPED(t
) && !SUSPENDED(t
) && !JDSTOPPED(t
)) ||
1997 p
->p_agenttp
!= NULL
||
1998 (p
->p_flag
& (SKILLED
| SEXITING
| SVFWAIT
))) {
2004 mutex_exit(&p
->p_lock
);
2007 sigdiffset(&smask
, &cantmask
);
2008 clwp
= lwp_create(lwp_rtt
, NULL
, 0, p
, TS_STOPPED
,
2009 t
->t_pri
, &smask
, NOCLASS
, 0);
2011 mutex_enter(&p
->p_lock
);
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
);
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
);
2047 clwp
->lwp_ap
= clwp
->lwp_arg
;
2048 clwp
->lwp_eosys
= NORMALRETURN
;
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.
2063 ct
->t_sig_check
= 1;
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.
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
) {
2091 * Confirm that the agent LWP has stopped.
2094 if ((error
= prlock(pnp
, ZNO
)) != 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
) {
2108 mutex_enter(&pcp
->prc_mutex
);
2113 mutex_exit(&pcp
->prc_mutex
);
2120 return (error
? error
: -1);
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
;
2133 if ((p
->p_flag
& SSYS
) || p
->p_as
== &kas
)
2135 else if ((base
+ cnt
) < base
|| (offset
+ cnt
) < offset
)
2137 else if (cnt
!= 0) {
2138 aiov
.iov_base
= base
;
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)
2167 pr_scred(proc_t
*p
, prcred_t
*prcred
, cred_t
*cr
, boolean_t dogrps
)
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
))
2185 int ngrp
= prcred
->pr_ngroups
;
2188 if (ngrp
< 0 || ngrp
> ngroups_max
)
2191 for (i
= 0; i
< ngrp
; i
++) {
2192 if (!VALID_GID(prcred
->pr_groups
[i
], zone
))
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
);
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
);
2219 /* Error checking done above */
2220 (void) crsetresuid(newcred
, prcred
->pr_ruid
, prcred
->pr_euid
,
2222 (void) crsetresgid(newcred
, prcred
->pr_rgid
, prcred
->pr_egid
,
2226 (void) crsetgroups(newcred
, prcred
->pr_ngroups
,
2231 mutex_enter(&p
->p_crlock
);
2232 oldcred
= p
->p_cred
;
2233 p
->p_cred
= newcred
;
2234 mutex_exit(&p
->p_crlock
);
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
);
2255 t
->t_pre_sys
= 1; /* so syscall will get new cred */
2256 } while ((t
= t
->t_forw
) != p
->p_tlist
);
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.
2267 pr_szoneid(proc_t
*p
, zoneid_t zoneid
, cred_t
*cr
)
2275 if (secpolicy_zone_config(cr
) != 0)
2277 if (zoneid
!= GLOBAL_ZONEID
&& zoneid
!= p
->p_zone
->zone_id
)
2279 if ((zptr
= zone_find_by_id(zoneid
)) == NULL
)
2281 mutex_exit(&p
->p_lock
);
2282 mutex_enter(&p
->p_crlock
);
2283 oldcred
= p
->p_cred
;
2285 mutex_exit(&p
->p_crlock
);
2286 newcred
= crdup(oldcred
);
2287 oldzoneid
= crgetzoneid(oldcred
);
2290 crsetzone(newcred
, zptr
);
2293 mutex_enter(&p
->p_crlock
);
2294 oldcred
= p
->p_cred
;
2295 p
->p_cred
= newcred
;
2296 mutex_exit(&p
->p_crlock
);
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
);
2317 t
->t_pre_sys
= 1; /* so syscall will get new cred */
2318 } while ((t
= t
->t_forw
) != p
->p_tlist
);
2324 pr_spriv(proc_t
*p
, prpriv_t
*prpriv
, cred_t
*cr
)
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.
2337 t
->t_pre_sys
= 1; /* so syscall will get new cred */
2338 } while ((t
= t
->t_forw
) != p
->p_tlist
);
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
)
2360 ASSERT(MUTEX_HELD(&p
->p_lock
));
2362 if (p
->p_flag
& SVFWAIT
) /* waiting for vfork'd child to exec */
2365 if ((t
= p
->p_tlist
) != NULL
) {
2367 if (t
== curthread
|| VSTOPPED(t
) ||
2368 (watchstop
&& (t
->t_proc_flag
& TP_WATCHSTOP
)))
2371 switch (t
->t_state
) {
2376 if (!(t
->t_flag
& T_WAKEABLE
) ||
2377 t
->t_wchan0
== NULL
)
2385 } while (rv
== 0 && (t
= t
->t_forw
) != p
->p_tlist
);
2392 * Cause all lwps in the process to pause (for watchpoint operations).
2395 pauselwps(proc_t
*p
)
2399 ASSERT(MUTEX_HELD(&p
->p_lock
));
2400 ASSERT(p
!= curproc
);
2402 if ((t
= p
->p_tlist
) != NULL
) {
2405 t
->t_proc_flag
|= TP_PAUSE
;
2407 if ((ISWAKEABLE(t
) && (t
->t_wchan0
== NULL
)) ||
2413 } while ((t
= t
->t_forw
) != p
->p_tlist
);
2418 * undo the effects of pauselwps()
2421 unpauselwps(proc_t
*p
)
2425 ASSERT(MUTEX_HELD(&p
->p_lock
));
2426 ASSERT(p
!= curproc
);
2428 if ((t
= p
->p_tlist
) != NULL
) {
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;
2438 } while ((t
= t
->t_forw
) != p
->p_tlist
);
2443 * Cancel all watched areas. Called from prclose().
2446 pr_cancel_watch(prnode_t
*pnp
)
2448 proc_t
*p
= pnp
->pr_pcommon
->prc_proc
;
2452 ASSERT(MUTEX_HELD(&p
->p_lock
) && (p
->p_proc_flag
& P_PR_LOCK
));
2454 if (!pr_watch_active(p
))
2458 * Pause the process before dealing with the watchpoints.
2462 while (holdwatch() != 0)
2465 mutex_exit(&pr_pidlock
);
2466 ASSERT(p
== curproc
);
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
;
2479 (void) cv_wait(cv
, mp
);
2481 p
= pr_p_lock(pnp
); /* NULL if process disappeared */
2482 mutex_exit(&pr_pidlock
);
2486 if (p
== NULL
) /* the process disappeared */
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
) {
2498 } while ((t
= t
->t_forw
) != p
->p_tlist
);
2502 if ((as
= p
->p_as
) != NULL
) {
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)
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
)) {
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
;
2532 mutex_enter(&p
->p_lock
);
2536 * Unpause the process now.