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.
31 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/cmn_err.h>
36 #include <sys/policy.h>
37 #include <sys/debug.h>
38 #include <sys/errno.h>
40 #include <sys/inline.h>
43 #include <sys/brand.h>
44 #include <sys/regset.h>
45 #include <sys/sysmacros.h>
46 #include <sys/systm.h>
48 #include <sys/vnode.h>
49 #include <sys/signal.h>
52 #include <sys/class.h>
53 #include <sys/fault.h>
54 #include <sys/syscall.h>
55 #include <sys/procfs.h>
57 #include <sys/copyops.h>
58 #include <sys/schedctl.h>
61 #include <fs/proc/prdata.h>
62 #include <sys/contract/process_impl.h>
64 static void pr_settrace(proc_t
*, sigset_t
*);
65 static int pr_setfpregs(prnode_t
*, prfpregset_t
*);
67 static int pr_setxregs(prnode_t
*, prxregset_t
*);
68 static int pr_setasrs(prnode_t
*, asrset_t
);
70 static int pr_setvaddr(prnode_t
*, caddr_t
);
71 static int pr_clearsig(prnode_t
*);
72 static int pr_clearflt(prnode_t
*);
73 static int pr_watch(prnode_t
*, prwatch_t
*, int *);
74 static int pr_agent(prnode_t
*, prgregset_t
, int *);
75 static int pr_rdwr(proc_t
*, enum uio_rw
, priovec_t
*);
76 static int pr_scred(proc_t
*, prcred_t
*, cred_t
*, boolean_t
);
77 static int pr_spriv(proc_t
*, prpriv_t
*, cred_t
*);
78 static int pr_szoneid(proc_t
*, zoneid_t
, cred_t
*);
79 static void pauselwps(proc_t
*);
80 static void unpauselwps(proc_t
*);
83 long sig
; /* PCKILL, PCUNKILL */
84 long nice
; /* PCNICE */
85 long timeo
; /* PCTWSTOP */
86 ulong_t flags
; /* PCRUN, PCSET, PCUNSET */
87 caddr_t vaddr
; /* PCSVADDR */
88 siginfo_t siginfo
; /* PCSSIG */
89 sigset_t sigset
; /* PCSTRACE, PCSHOLD */
90 fltset_t fltset
; /* PCSFAULT */
91 sysset_t sysset
; /* PCSENTRY, PCSEXIT */
92 prgregset_t prgregset
; /* PCSREG, PCAGENT */
93 prfpregset_t prfpregset
; /* PCSFPREG */
95 prxregset_t prxregset
; /* PCSXREG */
96 asrset_t asrset
; /* PCSASRS */
98 prwatch_t prwatch
; /* PCWATCH */
99 priovec_t priovec
; /* PCREAD, PCWRITE */
100 prcred_t prcred
; /* PCSCRED */
101 prpriv_t prpriv
; /* PCSPRIV */
102 long przoneid
; /* PCSZONE */
105 static int pr_control(long, arg_t
*, prnode_t
*, cred_t
*);
108 ctlsize(long cmd
, size_t resid
, arg_t
*argp
)
110 size_t size
= sizeof (long);
123 size
+= sizeof (siginfo_t
);
126 size
+= sizeof (long);
131 size
+= sizeof (long);
136 size
+= sizeof (ulong_t
);
139 size
+= sizeof (caddr_t
);
143 size
+= sizeof (sigset_t
);
146 size
+= sizeof (fltset_t
);
150 size
+= sizeof (sysset_t
);
154 size
+= sizeof (prgregset_t
);
157 size
+= sizeof (prfpregset_t
);
161 size
+= sizeof (prxregset_t
);
164 size
+= sizeof (asrset_t
);
168 size
+= sizeof (prwatch_t
);
172 size
+= sizeof (priovec_t
);
175 size
+= sizeof (prcred_t
);
179 * We cannot derefence the pr_ngroups fields if it
180 * we don't have enough data.
182 if (resid
< size
+ sizeof (prcred_t
) - sizeof (gid_t
))
184 ngrp
= argp
->prcred
.pr_ngroups
;
185 if (ngrp
< 0 || ngrp
> ngroups_max
)
188 /* The result can be smaller than sizeof (prcred_t) */
189 size
+= sizeof (prcred_t
) - sizeof (gid_t
);
190 size
+= ngrp
* sizeof (gid_t
);
193 if (resid
>= size
+ sizeof (prpriv_t
))
194 size
+= priv_prgetprivsize(&argp
->prpriv
);
199 size
+= sizeof (long);
205 /* Round up to a multiple of long, unless exact amount written */
207 rnd
= size
& (sizeof (long) - 1);
210 size
+= sizeof (long) - rnd
;
219 * Control operations (lots).
222 prwritectl(vnode_t
*vp
, uio_t
*uiop
, cred_t
*cr
)
224 #define MY_BUFFER_SIZE \
225 100 > 1 + sizeof (arg_t) / sizeof (long) ? \
226 100 : 1 + sizeof (arg_t) / sizeof (long)
227 long buf
[MY_BUFFER_SIZE
];
231 prnode_t
*pnp
= VTOP(vp
);
235 while (uiop
->uio_resid
) {
237 * Read several commands in one gulp.
240 if (resid
) { /* move incomplete command to front of buffer */
243 if (resid
>= sizeof (buf
))
245 tail
= (long *)((char *)buf
+ sizeof (buf
) - resid
);
248 } while ((resid
-= sizeof (long)) != 0);
250 resid
= sizeof (buf
) - ((char *)bufp
- (char *)buf
);
251 if (resid
> uiop
->uio_resid
)
252 resid
= uiop
->uio_resid
;
253 if (error
= uiomove((caddr_t
)bufp
, resid
, UIO_WRITE
, uiop
))
255 resid
+= (char *)bufp
- (char *)buf
;
258 do { /* loop over commands in buffer */
260 arg_t
*argp
= (arg_t
*)&bufp
[1];
262 size
= ctlsize(cmd
, resid
, argp
);
263 if (size
== 0) /* incomplete or invalid command */
266 * Perform the specified control operation.
269 if ((error
= prlock(pnp
, ZNO
)) != 0)
273 if (error
= pr_control(cmd
, argp
, pnp
, cr
)) {
274 if (error
== -1) /* -1 is timeout */
279 bufp
= (long *)((char *)bufp
+ size
);
280 } while ((resid
-= size
) != 0);
287 return (resid
? EINVAL
: 0);
291 pr_control(long cmd
, arg_t
*argp
, prnode_t
*pnp
, cred_t
*cr
)
301 pcp
= pnp
->pr_common
;
305 /* System processes defy control. */
306 if (p
->p_flag
& SSYS
) {
317 case PCSTOP
: /* direct process or lwp to stop and wait for stop */
318 case PCDSTOP
: /* direct process or lwp to stop, don't wait */
319 case PCWSTOP
: /* wait for process or lwp to stop */
320 case PCTWSTOP
: /* wait for process or lwp to stop, with timeout */
325 * Can't apply to a system process.
327 if (p
->p_as
== &kas
) {
332 if (cmd
== PCSTOP
|| cmd
== PCDSTOP
)
339 * If an lwp is waiting for itself or its process,
340 * don't wait. The stopped lwp would never see the
341 * fact that it is stopped.
343 if ((pcp
->prc_flags
& PRC_LWP
)?
344 (pcp
->prc_thread
== curthread
) : (p
== curproc
)) {
345 if (cmd
== PCWSTOP
|| cmd
== PCTWSTOP
)
350 timeo
= (cmd
== PCTWSTOP
)? (time_t)argp
->timeo
: 0;
351 if ((error
= pr_wait_stop(pnp
, timeo
)) != 0)
357 case PCRUN
: /* make lwp or process runnable */
358 error
= pr_setrun(pnp
, argp
->flags
);
361 case PCSTRACE
: /* set signal trace mask */
362 pr_settrace(p
, &argp
->sigset
);
365 case PCSSIG
: /* set current signal */
366 error
= pr_setsig(pnp
, &argp
->siginfo
);
367 if (argp
->siginfo
.si_signo
== SIGKILL
&& error
== 0) {
374 case PCKILL
: /* send signal */
375 error
= pr_kill(pnp
, (int)argp
->sig
, cr
);
376 if (error
== 0 && argp
->sig
== SIGKILL
) {
383 case PCUNKILL
: /* delete a pending signal */
384 error
= pr_unkill(pnp
, (int)argp
->sig
);
387 case PCNICE
: /* set nice priority */
388 error
= pr_nice(p
, (int)argp
->nice
, cr
);
391 case PCSENTRY
: /* set syscall entry bit mask */
392 case PCSEXIT
: /* set syscall exit bit mask */
393 pr_setentryexit(p
, &argp
->sysset
, cmd
== PCSENTRY
);
396 case PCSET
: /* set process flags */
397 error
= pr_set(p
, argp
->flags
);
400 case PCUNSET
: /* unset process flags */
401 error
= pr_unset(p
, argp
->flags
);
404 case PCSREG
: /* set general registers */
406 kthread_t
*t
= pr_thread(pnp
);
408 if (!ISTOPPED(t
) && !VSTOPPED(t
) && !DSTOPPED(t
)) {
413 mutex_exit(&p
->p_lock
);
414 prsetprregs(ttolwp(t
), argp
->prgregset
, 0);
415 mutex_enter(&p
->p_lock
);
420 case PCSFPREG
: /* set floating-point registers */
421 error
= pr_setfpregs(pnp
, &argp
->prfpregset
);
424 case PCSXREG
: /* set extra registers */
426 error
= pr_setxregs(pnp
, &argp
->prxregset
);
433 case PCSASRS
: /* set ancillary state registers */
434 error
= pr_setasrs(pnp
, argp
->asrset
);
438 case PCSVADDR
: /* set virtual address at which to resume */
439 error
= pr_setvaddr(pnp
, argp
->vaddr
);
442 case PCSHOLD
: /* set signal-hold mask */
443 pr_sethold(pnp
, &argp
->sigset
);
446 case PCSFAULT
: /* set mask of traced faults */
447 pr_setfault(p
, &argp
->fltset
);
450 case PCCSIG
: /* clear current signal */
451 error
= pr_clearsig(pnp
);
454 case PCCFAULT
: /* clear current fault */
455 error
= pr_clearflt(pnp
);
458 case PCWATCH
: /* set or clear watched areas */
459 error
= pr_watch(pnp
, &argp
->prwatch
, &unlocked
);
460 if (error
&& unlocked
)
464 case PCAGENT
: /* create the /proc agent lwp in the target process */
465 error
= pr_agent(pnp
, argp
->prgregset
, &unlocked
);
466 if (error
&& unlocked
)
470 case PCREAD
: /* read from the address space */
471 error
= pr_rdwr(p
, UIO_READ
, &argp
->priovec
);
474 case PCWRITE
: /* write to the address space */
475 error
= pr_rdwr(p
, UIO_WRITE
, &argp
->priovec
);
478 case PCSCRED
: /* set the process credentials */
480 error
= pr_scred(p
, &argp
->prcred
, cr
, cmd
== PCSCREDX
);
483 case PCSPRIV
: /* set the process privileges */
484 error
= pr_spriv(p
, &argp
->prpriv
, cr
);
486 case PCSZONE
: /* set the process's zoneid credentials */
487 error
= pr_szoneid(p
, (zoneid_t
)argp
->przoneid
, cr
);
496 #ifdef _SYSCALL32_IMPL
499 int32_t sig
; /* PCKILL, PCUNKILL */
500 int32_t nice
; /* PCNICE */
501 int32_t timeo
; /* PCTWSTOP */
502 uint32_t flags
; /* PCRUN, PCSET, PCUNSET */
503 caddr32_t vaddr
; /* PCSVADDR */
504 siginfo32_t siginfo
; /* PCSSIG */
505 sigset_t sigset
; /* PCSTRACE, PCSHOLD */
506 fltset_t fltset
; /* PCSFAULT */
507 sysset_t sysset
; /* PCSENTRY, PCSEXIT */
508 prgregset32_t prgregset
; /* PCSREG, PCAGENT */
509 prfpregset32_t prfpregset
; /* PCSFPREG */
511 prxregset_t prxregset
; /* PCSXREG */
513 prwatch32_t prwatch
; /* PCWATCH */
514 priovec32_t priovec
; /* PCREAD, PCWRITE */
515 prcred32_t prcred
; /* PCSCRED */
516 prpriv_t prpriv
; /* PCSPRIV */
517 int32_t przoneid
; /* PCSZONE */
520 static int pr_control32(int32_t, arg32_t
*, prnode_t
*, cred_t
*);
521 static int pr_setfpregs32(prnode_t
*, prfpregset32_t
*);
524 * Note that while ctlsize32() can use argp, it must do so only in a way
525 * that assumes 32-bit rather than 64-bit alignment as argp is a pointer
526 * to an array of 32-bit values and only 32-bit alignment is ensured.
529 ctlsize32(int32_t cmd
, size_t resid
, arg32_t
*argp
)
531 size_t size
= sizeof (int32_t);
544 size
+= sizeof (siginfo32_t
);
547 size
+= sizeof (int32_t);
552 size
+= sizeof (int32_t);
557 size
+= sizeof (uint32_t);
560 size
+= sizeof (caddr32_t
);
564 size
+= sizeof (sigset_t
);
567 size
+= sizeof (fltset_t
);
571 size
+= sizeof (sysset_t
);
575 size
+= sizeof (prgregset32_t
);
578 size
+= sizeof (prfpregset32_t
);
582 size
+= sizeof (prxregset_t
);
586 size
+= sizeof (prwatch32_t
);
590 size
+= sizeof (priovec32_t
);
593 size
+= sizeof (prcred32_t
);
597 * We cannot derefence the pr_ngroups fields if it
598 * we don't have enough data.
600 if (resid
< size
+ sizeof (prcred32_t
) - sizeof (gid32_t
))
602 ngrp
= argp
->prcred
.pr_ngroups
;
603 if (ngrp
< 0 || ngrp
> ngroups_max
)
606 /* The result can be smaller than sizeof (prcred32_t) */
607 size
+= sizeof (prcred32_t
) - sizeof (gid32_t
);
608 size
+= ngrp
* sizeof (gid32_t
);
611 if (resid
>= size
+ sizeof (prpriv_t
))
612 size
+= priv_prgetprivsize(&argp
->prpriv
);
617 size
+= sizeof (int32_t);
623 /* Round up to a multiple of int32_t */
624 rnd
= size
& (sizeof (int32_t) - 1);
627 size
+= sizeof (int32_t) - rnd
;
635 * Control operations (lots).
638 prwritectl32(struct vnode
*vp
, struct uio
*uiop
, cred_t
*cr
)
640 #define MY_BUFFER_SIZE32 \
641 100 > 1 + sizeof (arg32_t) / sizeof (int32_t) ? \
642 100 : 1 + sizeof (arg32_t) / sizeof (int32_t)
643 int32_t buf
[MY_BUFFER_SIZE32
];
648 prnode_t
*pnp
= VTOP(vp
);
652 while (uiop
->uio_resid
) {
654 * Read several commands in one gulp.
657 if (resid
) { /* move incomplete command to front of buffer */
660 if (resid
>= sizeof (buf
))
662 tail
= (int32_t *)((char *)buf
+ sizeof (buf
) - resid
);
665 } while ((resid
-= sizeof (int32_t)) != 0);
667 resid
= sizeof (buf
) - ((char *)bufp
- (char *)buf
);
668 if (resid
> uiop
->uio_resid
)
669 resid
= uiop
->uio_resid
;
670 if (error
= uiomove((caddr_t
)bufp
, resid
, UIO_WRITE
, uiop
))
672 resid
+= (char *)bufp
- (char *)buf
;
675 do { /* loop over commands in buffer */
676 int32_t cmd
= bufp
[0];
677 arg32_t
*argp
= (arg32_t
*)&bufp
[1];
679 size
= ctlsize32(cmd
, resid
, argp
);
680 if (size
== 0) /* incomplete or invalid command */
683 * Perform the specified control operation.
686 if ((error
= prlock(pnp
, ZNO
)) != 0)
692 * Since some members of the arg32_t union contain
693 * 64-bit values (which must be 64-bit aligned), we
694 * can't simply pass a pointer to the structure as
695 * it may be unaligned. Note that we do pass the
696 * potentially unaligned structure to ctlsize32()
697 * above, but that uses it a way that makes no
698 * assumptions about alignment.
700 ASSERT(size
- sizeof (cmd
) <= sizeof (arg
));
701 bcopy(argp
, &arg
, size
- sizeof (cmd
));
703 if (error
= pr_control32(cmd
, &arg
, pnp
, cr
)) {
704 if (error
== -1) /* -1 is timeout */
709 bufp
= (int32_t *)((char *)bufp
+ size
);
710 } while ((resid
-= size
) != 0);
717 return (resid
? EINVAL
: 0);
721 pr_control32(int32_t cmd
, arg32_t
*argp
, prnode_t
*pnp
, cred_t
*cr
)
731 pcp
= pnp
->pr_common
;
735 if (p
->p_flag
& SSYS
) {
746 case PCSTOP
: /* direct process or lwp to stop and wait for stop */
747 case PCDSTOP
: /* direct process or lwp to stop, don't wait */
748 case PCWSTOP
: /* wait for process or lwp to stop */
749 case PCTWSTOP
: /* wait for process or lwp to stop, with timeout */
754 * Can't apply to a system process.
756 if (p
->p_as
== &kas
) {
761 if (cmd
== PCSTOP
|| cmd
== PCDSTOP
)
768 * If an lwp is waiting for itself or its process,
769 * don't wait. The lwp will never see the fact that
772 if ((pcp
->prc_flags
& PRC_LWP
)?
773 (pcp
->prc_thread
== curthread
) : (p
== curproc
)) {
774 if (cmd
== PCWSTOP
|| cmd
== PCTWSTOP
)
779 timeo
= (cmd
== PCTWSTOP
)? (time_t)argp
->timeo
: 0;
780 if ((error
= pr_wait_stop(pnp
, timeo
)) != 0)
786 case PCRUN
: /* make lwp or process runnable */
787 error
= pr_setrun(pnp
, (ulong_t
)argp
->flags
);
790 case PCSTRACE
: /* set signal trace mask */
791 pr_settrace(p
, &argp
->sigset
);
794 case PCSSIG
: /* set current signal */
795 if (PROCESS_NOT_32BIT(p
))
798 int sig
= (int)argp
->siginfo
.si_signo
;
801 bzero(&siginfo
, sizeof (siginfo
));
802 siginfo_32tok(&argp
->siginfo
, (k_siginfo_t
*)&siginfo
);
803 error
= pr_setsig(pnp
, &siginfo
);
804 if (sig
== SIGKILL
&& error
== 0) {
812 case PCKILL
: /* send signal */
813 error
= pr_kill(pnp
, (int)argp
->sig
, cr
);
814 if (error
== 0 && argp
->sig
== SIGKILL
) {
821 case PCUNKILL
: /* delete a pending signal */
822 error
= pr_unkill(pnp
, (int)argp
->sig
);
825 case PCNICE
: /* set nice priority */
826 error
= pr_nice(p
, (int)argp
->nice
, cr
);
829 case PCSENTRY
: /* set syscall entry bit mask */
830 case PCSEXIT
: /* set syscall exit bit mask */
831 pr_setentryexit(p
, &argp
->sysset
, cmd
== PCSENTRY
);
834 case PCSET
: /* set process flags */
835 error
= pr_set(p
, (long)argp
->flags
);
838 case PCUNSET
: /* unset process flags */
839 error
= pr_unset(p
, (long)argp
->flags
);
842 case PCSREG
: /* set general registers */
843 if (PROCESS_NOT_32BIT(p
))
846 kthread_t
*t
= pr_thread(pnp
);
848 if (!ISTOPPED(t
) && !VSTOPPED(t
) && !DSTOPPED(t
)) {
852 prgregset_t prgregset
;
853 klwp_t
*lwp
= ttolwp(t
);
856 mutex_exit(&p
->p_lock
);
857 prgregset_32ton(lwp
, argp
->prgregset
,
859 prsetprregs(lwp
, prgregset
, 0);
860 mutex_enter(&p
->p_lock
);
865 case PCSFPREG
: /* set floating-point registers */
866 if (PROCESS_NOT_32BIT(p
))
869 error
= pr_setfpregs32(pnp
, &argp
->prfpregset
);
872 case PCSXREG
: /* set extra registers */
874 if (PROCESS_NOT_32BIT(p
))
877 error
= pr_setxregs(pnp
, &argp
->prxregset
);
883 case PCSVADDR
: /* set virtual address at which to resume */
884 if (PROCESS_NOT_32BIT(p
))
887 error
= pr_setvaddr(pnp
,
888 (caddr_t
)(uintptr_t)argp
->vaddr
);
891 case PCSHOLD
: /* set signal-hold mask */
892 pr_sethold(pnp
, &argp
->sigset
);
895 case PCSFAULT
: /* set mask of traced faults */
896 pr_setfault(p
, &argp
->fltset
);
899 case PCCSIG
: /* clear current signal */
900 error
= pr_clearsig(pnp
);
903 case PCCFAULT
: /* clear current fault */
904 error
= pr_clearflt(pnp
);
907 case PCWATCH
: /* set or clear watched areas */
908 if (PROCESS_NOT_32BIT(p
))
913 prwatch
.pr_vaddr
= argp
->prwatch
.pr_vaddr
;
914 prwatch
.pr_size
= argp
->prwatch
.pr_size
;
915 prwatch
.pr_wflags
= argp
->prwatch
.pr_wflags
;
916 prwatch
.pr_pad
= argp
->prwatch
.pr_pad
;
917 error
= pr_watch(pnp
, &prwatch
, &unlocked
);
918 if (error
&& unlocked
)
923 case PCAGENT
: /* create the /proc agent lwp in the target process */
924 if (PROCESS_NOT_32BIT(p
))
927 prgregset_t prgregset
;
928 kthread_t
*t
= pr_thread(pnp
);
929 klwp_t
*lwp
= ttolwp(t
);
931 mutex_exit(&p
->p_lock
);
932 prgregset_32ton(lwp
, argp
->prgregset
, prgregset
);
933 mutex_enter(&p
->p_lock
);
934 error
= pr_agent(pnp
, prgregset
, &unlocked
);
935 if (error
&& unlocked
)
940 case PCREAD
: /* read from the address space */
941 case PCWRITE
: /* write to the address space */
942 if (PROCESS_NOT_32BIT(p
) || (pnp
->pr_flags
& PR_OFFMAX
))
945 enum uio_rw rw
= (cmd
== PCREAD
)? UIO_READ
: UIO_WRITE
;
949 (void *)(uintptr_t)argp
->priovec
.pio_base
;
950 priovec
.pio_len
= (size_t)argp
->priovec
.pio_len
;
951 priovec
.pio_offset
= (off_t
)
952 (uint32_t)argp
->priovec
.pio_offset
;
953 error
= pr_rdwr(p
, rw
, &priovec
);
957 case PCSCRED
: /* set the process credentials */
961 * All the fields in these structures are exactly the
962 * same and so the structures are compatible. In case
963 * this ever changes, we catch this with the ASSERT
966 prcred_t
*prcred
= (prcred_t
*)&argp
->prcred
;
969 ASSERT(sizeof (prcred_t
) == sizeof (prcred32_t
));
972 error
= pr_scred(p
, prcred
, cr
, cmd
== PCSCREDX
);
976 case PCSPRIV
: /* set the process privileges */
977 error
= pr_spriv(p
, &argp
->prpriv
, cr
);
980 case PCSZONE
: /* set the process's zoneid */
981 error
= pr_szoneid(p
, (zoneid_t
)argp
->przoneid
, cr
);
990 #endif /* _SYSCALL32_IMPL */
993 * Return the specific or chosen thread/lwp for a control operation.
994 * Returns with the thread locked via thread_lock(t).
997 pr_thread(prnode_t
*pnp
)
999 prcommon_t
*pcp
= pnp
->pr_common
;
1002 if (pcp
->prc_flags
& PRC_LWP
) {
1003 t
= pcp
->prc_thread
;
1007 proc_t
*p
= pcp
->prc_proc
;
1008 t
= prchoose(p
); /* returns locked thread */
1016 * Direct the process or lwp to stop.
1019 pr_stop(prnode_t
*pnp
)
1021 prcommon_t
*pcp
= pnp
->pr_common
;
1022 proc_t
*p
= pcp
->prc_proc
;
1027 * If already stopped, do nothing; otherwise flag
1028 * it to be stopped the next time it tries to run.
1029 * If sleeping at interruptible priority, set it
1030 * running so it will stop within cv_wait_sig().
1032 * Take care to cooperate with jobcontrol: if an lwp
1033 * is stopped due to the default action of a jobcontrol
1034 * stop signal, flag it to be stopped the next time it
1035 * starts due to a SIGCONT signal.
1037 if (pcp
->prc_flags
& PRC_LWP
)
1038 t
= pcp
->prc_thread
;
1049 t
->t_proc_flag
|= TP_PRSTOP
;
1050 t
->t_sig_check
= 1; /* do ISSIG */
1053 /* Move the thread from wait queue to run queue */
1057 if (ISWAKEABLE(t
)) {
1058 if (t
->t_wchan0
== NULL
)
1060 else if (!VSTOPPED(t
)) {
1062 * Mark it virtually stopped.
1064 t
->t_proc_flag
|= TP_PRVSTOP
;
1069 * force the thread into the kernel
1070 * if it is not already there.
1075 (vp
= p
->p_lwpdir
[t
->t_dslot
].ld_entry
->le_trace
) != NULL
)
1077 if (pcp
->prc_flags
& PRC_LWP
)
1079 } while ((t
= t
->t_forw
) != p
->p_tlist
);
1082 * We do this just in case the thread we asked
1083 * to stop is in holdlwps() (called from cfork()).
1085 cv_broadcast(&p
->p_holdlwps
);
1089 * Sleep until the lwp stops, but cooperate with
1090 * jobcontrol: Don't wake up if the lwp is stopped
1091 * due to the default action of a jobcontrol stop signal.
1092 * If this is the process file descriptor, sleep
1093 * until all of the process's lwps stop.
1096 pr_wait_stop(prnode_t
*pnp
, time_t timeo
)
1098 prcommon_t
*pcp
= pnp
->pr_common
;
1099 proc_t
*p
= pcp
->prc_proc
;
1101 timestruc_t
*rqtp
= NULL
;
1106 if (timeo
> 0) { /* millisecond timeout */
1108 * Determine the precise future time of the requested timeout.
1112 timecheck
= timechanged
;
1115 rqtp
->tv_sec
= timeo
/ MILLISEC
;
1116 rqtp
->tv_nsec
= (timeo
% MILLISEC
) * MICROSEC
;
1117 timespecadd(rqtp
, &now
);
1120 if (pcp
->prc_flags
& PRC_LWP
) { /* lwp file descriptor */
1121 t
= pcp
->prc_thread
;
1124 while (!ISTOPPED(t
) && !VSTOPPED(t
)) {
1126 mutex_enter(&pcp
->prc_mutex
);
1128 error
= pr_wait(pcp
, rqtp
, timecheck
);
1129 if (error
) /* -1 is timeout */
1131 if ((error
= prlock(pnp
, ZNO
)) != 0)
1133 ASSERT(p
== pcp
->prc_proc
);
1134 ASSERT(t
== pcp
->prc_thread
);
1138 } else { /* process file descriptor */
1139 t
= prchoose(p
); /* returns locked thread */
1141 ASSERT(MUTEX_HELD(&p
->p_lock
));
1142 while ((!ISTOPPED(t
) && !VSTOPPED(t
) && !SUSPENDED(t
)) ||
1143 (p
->p_flag
& SEXITLWPS
)) {
1145 mutex_enter(&pcp
->prc_mutex
);
1147 error
= pr_wait(pcp
, rqtp
, timecheck
);
1148 if (error
) /* -1 is timeout */
1150 if ((error
= prlock(pnp
, ZNO
)) != 0)
1152 ASSERT(p
== pcp
->prc_proc
);
1153 t
= prchoose(p
); /* returns locked t */
1159 ASSERT(!(pcp
->prc_flags
& PRC_DESTROY
) && p
->p_stat
!= SZOMB
&&
1160 t
!= NULL
&& t
->t_state
!= TS_ZOMB
);
1166 pr_setrun(prnode_t
*pnp
, ulong_t flags
)
1168 prcommon_t
*pcp
= pnp
->pr_common
;
1169 proc_t
*p
= pcp
->prc_proc
;
1174 * Cannot set an lwp running if it is not stopped.
1175 * Also, no lwp other than the /proc agent lwp can
1176 * be set running so long as the /proc agent lwp exists.
1178 t
= pr_thread(pnp
); /* returns locked thread */
1179 if ((!ISTOPPED(t
) && !VSTOPPED(t
) &&
1180 !(t
->t_proc_flag
& TP_PRSTOP
)) ||
1181 (p
->p_agenttp
!= NULL
&&
1182 (t
!= p
->p_agenttp
|| !(pcp
->prc_flags
& PRC_LWP
)))) {
1187 if (flags
& ~(PRCSIG
|PRCFAULT
|PRSTEP
|PRSTOP
|PRSABORT
))
1190 if ((flags
& PRCSIG
) && lwp
->lwp_cursig
!= SIGKILL
) {
1192 * Discard current siginfo_t, if any.
1194 lwp
->lwp_cursig
= 0;
1195 lwp
->lwp_extsig
= 0;
1196 if (lwp
->lwp_curinfo
) {
1197 siginfofree(lwp
->lwp_curinfo
);
1198 lwp
->lwp_curinfo
= NULL
;
1201 if (flags
& PRCFAULT
)
1202 lwp
->lwp_curflt
= 0;
1204 * We can't hold p->p_lock when we touch the lwp's registers.
1205 * It may be swapped out and we will get a page fault.
1207 if (flags
& PRSTEP
) {
1208 mutex_exit(&p
->p_lock
);
1210 mutex_enter(&p
->p_lock
);
1212 if (flags
& PRSTOP
) {
1213 t
->t_proc_flag
|= TP_PRSTOP
;
1214 t
->t_sig_check
= 1; /* do ISSIG */
1216 if (flags
& PRSABORT
)
1217 lwp
->lwp_sysabort
= 1;
1219 if ((pcp
->prc_flags
& PRC_LWP
) || (flags
& (PRSTEP
|PRSTOP
))) {
1221 * Here, we are dealing with a single lwp.
1224 t
->t_schedflag
|= TS_PSTART
;
1225 t
->t_dtrace_stop
= 0;
1227 } else if (flags
& PRSABORT
) {
1229 ~(TP_PRSTOP
|TP_PRVSTOP
|TP_STOPPING
);
1231 } else if (!(flags
& PRSTOP
)) {
1233 ~(TP_PRSTOP
|TP_PRVSTOP
|TP_STOPPING
);
1238 * Here, we are dealing with the whole process.
1242 * The representative lwp is stopped on an event
1243 * of interest. We demote it to PR_REQUESTED and
1244 * choose another representative lwp. If the new
1245 * representative lwp is not stopped on an event of
1246 * interest (other than PR_REQUESTED), we set the
1247 * whole process running, else we leave the process
1248 * stopped showing the next event of interest.
1250 kthread_t
*tx
= NULL
;
1252 if (!(flags
& PRSABORT
) &&
1253 t
->t_whystop
== PR_SYSENTRY
&&
1254 t
->t_whatstop
== SYS_lwp_exit
)
1255 tx
= t
; /* remember the exiting lwp */
1256 t
->t_whystop
= PR_REQUESTED
;
1259 t
= prchoose(p
); /* returns locked t */
1260 ASSERT(ISTOPPED(t
) || VSTOPPED(t
));
1262 t
->t_whystop
== PR_REQUESTED
) {
1268 * As a special case, if the old representative
1269 * lwp was stopped on entry to _lwp_exit()
1270 * (and we are not aborting the system call),
1271 * we set the old representative lwp running.
1272 * We do this so that the next process stop
1273 * will find the exiting lwp gone.
1277 tx
->t_schedflag
|= TS_PSTART
;
1278 t
->t_dtrace_stop
= 0;
1285 * No event of interest; set all of the lwps running.
1287 if (flags
& PRSABORT
) {
1289 ~(TP_PRSTOP
|TP_PRVSTOP
|TP_STOPPING
);
1300 * Wait until process/lwp stops or until timer expires.
1301 * Return EINTR for an interruption, -1 for timeout, else 0.
1304 pr_wait(prcommon_t
*pcp
, /* prcommon referring to process/lwp */
1305 timestruc_t
*ts
, /* absolute time of timeout, if any */
1310 ASSERT(MUTEX_HELD(&pcp
->prc_mutex
));
1311 rval
= cv_waituntil_sig(&pcp
->prc_wait
, &pcp
->prc_mutex
, ts
, timecheck
);
1312 mutex_exit(&pcp
->prc_mutex
);
1324 * Make all threads in the process runnable.
1327 allsetrun(proc_t
*p
)
1331 ASSERT(MUTEX_HELD(&p
->p_lock
));
1333 if ((t
= p
->p_tlist
) != NULL
) {
1336 ASSERT(!(t
->t_proc_flag
& TP_LWPEXIT
));
1337 t
->t_proc_flag
&= ~(TP_PRSTOP
|TP_PRVSTOP
|TP_STOPPING
);
1339 t
->t_schedflag
|= TS_PSTART
;
1340 t
->t_dtrace_stop
= 0;
1344 } while ((t
= t
->t_forw
) != p
->p_tlist
);
1349 * Wait for the process to die.
1350 * We do this after sending SIGKILL because we know it will
1351 * die soon and we want subsequent operations to return ENOENT.
1354 pr_wait_die(prnode_t
*pnp
)
1358 mutex_enter(&pidlock
);
1359 while ((p
= pnp
->pr_common
->prc_proc
) != NULL
&& p
->p_stat
!= SZOMB
) {
1360 if (!cv_wait_sig(&p
->p_srwchan_cv
, &pidlock
))
1363 mutex_exit(&pidlock
);
1367 pr_settrace(proc_t
*p
, sigset_t
*sp
)
1369 prdelset(sp
, SIGKILL
);
1370 prassignset(&p
->p_sigmask
, sp
);
1371 if (!sigisempty(&p
->p_sigmask
))
1372 p
->p_proc_flag
|= P_PR_TRACE
;
1373 else if (prisempty(&p
->p_fltmask
)) {
1374 user_t
*up
= PTOU(p
);
1375 if (up
->u_systrap
== 0)
1376 p
->p_proc_flag
&= ~P_PR_TRACE
;
1381 pr_setsig(prnode_t
*pnp
, siginfo_t
*sip
)
1383 int nsig
= PROC_IS_BRANDED(curproc
)? BROP(curproc
)->b_nsig
: NSIG
;
1384 int sig
= sip
->si_signo
;
1385 prcommon_t
*pcp
= pnp
->pr_common
;
1386 proc_t
*p
= pcp
->prc_proc
;
1391 t
= pr_thread(pnp
); /* returns locked thread */
1394 if (sig
< 0 || sig
>= nsig
)
1395 /* Zero allowed here */
1397 else if (lwp
->lwp_cursig
== SIGKILL
)
1398 /* "can't happen", but just in case */
1400 else if ((lwp
->lwp_cursig
= (uchar_t
)sig
) == 0) {
1401 lwp
->lwp_extsig
= 0;
1403 * Discard current siginfo_t, if any.
1405 if (lwp
->lwp_curinfo
) {
1406 siginfofree(lwp
->lwp_curinfo
);
1407 lwp
->lwp_curinfo
= NULL
;
1413 /* drop p_lock to do kmem_alloc(KM_SLEEP) */
1414 mutex_exit(&p
->p_lock
);
1415 sqp
= kmem_zalloc(sizeof (sigqueue_t
), KM_SLEEP
);
1416 mutex_enter(&p
->p_lock
);
1418 if (lwp
->lwp_curinfo
== NULL
)
1419 lwp
->lwp_curinfo
= sqp
;
1421 kmem_free(sqp
, sizeof (sigqueue_t
));
1423 * Copy contents of info to current siginfo_t.
1425 bcopy(sip
, &lwp
->lwp_curinfo
->sq_info
,
1426 sizeof (lwp
->lwp_curinfo
->sq_info
));
1428 * Prevent contents published by si_zoneid-unaware /proc
1429 * consumers from being incorrectly filtered. Because
1430 * an uninitialized si_zoneid is the same as
1431 * GLOBAL_ZONEID, this means that you can't pr_setsig a
1432 * process in a non-global zone with a siginfo which
1433 * appears to come from the global zone.
1435 if (SI_FROMUSER(sip
) && sip
->si_zoneid
== 0)
1436 lwp
->lwp_curinfo
->sq_info
.si_zoneid
=
1439 * Side-effects for SIGKILL and jobcontrol signals.
1441 if (sig
== SIGKILL
) {
1442 p
->p_flag
|= SKILLED
;
1443 p
->p_flag
&= ~SEXTKILLED
;
1444 } else if (sig
== SIGCONT
) {
1445 p
->p_flag
|= SSCONT
;
1446 sigdelq(p
, NULL
, SIGSTOP
);
1447 sigdelq(p
, NULL
, SIGTSTP
);
1448 sigdelq(p
, NULL
, SIGTTOU
);
1449 sigdelq(p
, NULL
, SIGTTIN
);
1450 sigdiffset(&p
->p_sig
, &stopdefault
);
1451 sigdiffset(&p
->p_extsig
, &stopdefault
);
1452 if ((tx
= p
->p_tlist
) != NULL
) {
1454 sigdelq(p
, tx
, SIGSTOP
);
1455 sigdelq(p
, tx
, SIGTSTP
);
1456 sigdelq(p
, tx
, SIGTTOU
);
1457 sigdelq(p
, tx
, SIGTTIN
);
1458 sigdiffset(&tx
->t_sig
, &stopdefault
);
1459 sigdiffset(&tx
->t_extsig
, &stopdefault
);
1460 } while ((tx
= tx
->t_forw
) != p
->p_tlist
);
1462 } else if (sigismember(&stopdefault
, sig
)) {
1463 if (PTOU(p
)->u_signal
[sig
-1] == SIG_DFL
&&
1464 (sig
== SIGSTOP
|| !p
->p_pgidp
->pid_pgorphaned
))
1465 p
->p_flag
&= ~SSCONT
;
1466 sigdelq(p
, NULL
, SIGCONT
);
1467 sigdelset(&p
->p_sig
, SIGCONT
);
1468 sigdelset(&p
->p_extsig
, SIGCONT
);
1469 if ((tx
= p
->p_tlist
) != NULL
) {
1471 sigdelq(p
, tx
, SIGCONT
);
1472 sigdelset(&tx
->t_sig
, SIGCONT
);
1473 sigdelset(&tx
->t_extsig
, SIGCONT
);
1474 } while ((tx
= tx
->t_forw
) != p
->p_tlist
);
1478 if (ISWAKEABLE(t
) || ISWAITING(t
)) {
1479 /* Set signaled sleeping/waiting lwp running */
1481 } else if (t
->t_state
== TS_STOPPED
&& sig
== SIGKILL
) {
1482 /* If SIGKILL, set stopped lwp running */
1484 t
->t_schedflag
|= TS_XSTART
| TS_PSTART
;
1485 t
->t_dtrace_stop
= 0;
1488 t
->t_sig_check
= 1; /* so ISSIG will be done */
1491 * More jobcontrol side-effects.
1493 if (sig
== SIGCONT
&& (tx
= p
->p_tlist
) != NULL
) {
1497 if (tx
->t_state
== TS_STOPPED
&&
1498 tx
->t_whystop
== PR_JOBCONTROL
) {
1499 tx
->t_schedflag
|= TS_XSTART
;
1503 } while ((tx
= tx
->t_forw
) != p
->p_tlist
);
1510 pr_kill(prnode_t
*pnp
, int sig
, cred_t
*cr
)
1512 int nsig
= PROC_IS_BRANDED(curproc
)? BROP(curproc
)->b_nsig
: NSIG
;
1513 prcommon_t
*pcp
= pnp
->pr_common
;
1514 proc_t
*p
= pcp
->prc_proc
;
1517 if (sig
<= 0 || sig
>= nsig
)
1520 bzero(&info
, sizeof (info
));
1521 info
.si_signo
= sig
;
1522 info
.si_code
= SI_USER
;
1523 info
.si_pid
= curproc
->p_pid
;
1524 info
.si_ctid
= PRCTID(curproc
);
1525 info
.si_zoneid
= getzoneid();
1526 info
.si_uid
= crgetruid(cr
);
1527 sigaddq(p
, (pcp
->prc_flags
& PRC_LWP
)?
1528 pcp
->prc_thread
: NULL
, &info
, KM_NOSLEEP
);
1534 pr_unkill(prnode_t
*pnp
, int sig
)
1536 int nsig
= PROC_IS_BRANDED(curproc
)? BROP(curproc
)->b_nsig
: NSIG
;
1537 prcommon_t
*pcp
= pnp
->pr_common
;
1538 proc_t
*p
= pcp
->prc_proc
;
1539 sigqueue_t
*infop
= NULL
;
1541 if (sig
<= 0 || sig
>= nsig
|| sig
== SIGKILL
)
1544 if (pcp
->prc_flags
& PRC_LWP
)
1545 sigdeq(p
, pcp
->prc_thread
, sig
, &infop
);
1547 sigdeq(p
, NULL
, sig
, &infop
);
1556 pr_nice(proc_t
*p
, int nice
, cred_t
*cr
)
1564 ASSERT(!(t
->t_proc_flag
& TP_LWPEXIT
));
1565 err
= CL_DONICE(t
, cr
, nice
, (int *)NULL
);
1566 schedctl_set_cidpri(t
);
1569 } while ((t
= t
->t_forw
) != p
->p_tlist
);
1575 pr_setentryexit(proc_t
*p
, sysset_t
*sysset
, int entry
)
1577 user_t
*up
= PTOU(p
);
1580 prassignset(&up
->u_entrymask
, sysset
);
1582 prassignset(&up
->u_exitmask
, sysset
);
1584 if (!prisempty(&up
->u_entrymask
) ||
1585 !prisempty(&up
->u_exitmask
)) {
1587 p
->p_proc_flag
|= P_PR_TRACE
;
1588 set_proc_sys(p
); /* set pre and post-sys flags */
1591 if (sigisempty(&p
->p_sigmask
) &&
1592 prisempty(&p
->p_fltmask
))
1593 p
->p_proc_flag
&= ~P_PR_TRACE
;
1598 (PR_FORK|PR_RLC|PR_KLC|PR_ASYNC|PR_BPTADJ|PR_MSACCT|PR_MSFORK|PR_PTRACE)
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.