1 /* $OpenBSD: kern_sig.c,v 1.348 2024/11/06 17:14:01 claudio Exp $ */
2 /* $NetBSD: kern_sig.c,v 1.54 1996/04/22 01:38:32 christos Exp $ */
3
4 /*
5 * Copyright (c) 1997 Theo de Raadt. All rights reserved.
6 * Copyright (c) 1982, 1986, 1989, 1991, 1993
7 * The Regents of the University of California. All rights reserved.
8 * (c) UNIX System Laboratories, Inc.
9 * All or some portions of this file are derived from material licensed
10 * to the University of California by American Telephone and Telegraph
11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12 * the permission of UNIX System Laboratories, Inc.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
39 */
40
41 #include <sys/param.h>
42 #include <sys/signalvar.h>
43 #include <sys/queue.h>
44 #include <sys/namei.h>
45 #include <sys/vnode.h>
46 #include <sys/event.h>
47 #include <sys/proc.h>
48 #include <sys/systm.h>
49 #include <sys/acct.h>
50 #include <sys/fcntl.h>
51 #include <sys/filedesc.h>
52 #include <sys/wait.h>
53 #include <sys/ktrace.h>
54 #include <sys/stat.h>
55 #include <sys/malloc.h>
56 #include <sys/pool.h>
57 #include <sys/sched.h>
58 #include <sys/user.h>
59 #include <sys/syslog.h>
60 #include <sys/ttycom.h>
61 #include <sys/pledge.h>
62 #include <sys/witness.h>
63 #include <sys/exec_elf.h>
64
65 #include <sys/mount.h>
66 #include <sys/syscallargs.h>
67
68 #include <uvm/uvm_extern.h>
69 #include <machine/tcb.h>
70
71 int nosuidcoredump = 1;
72
73 /*
74 * The array below categorizes the signals and their default actions.
75 */
76 const int sigprop[NSIG] = {
77 0, /* unused */
78 SA_KILL, /* SIGHUP */
79 SA_KILL, /* SIGINT */
80 SA_KILL|SA_CORE, /* SIGQUIT */
81 SA_KILL|SA_CORE, /* SIGILL */
82 SA_KILL|SA_CORE, /* SIGTRAP */
83 SA_KILL|SA_CORE, /* SIGABRT */
84 SA_KILL|SA_CORE, /* SIGEMT */
85 SA_KILL|SA_CORE, /* SIGFPE */
86 SA_KILL, /* SIGKILL */
87 SA_KILL|SA_CORE, /* SIGBUS */
88 SA_KILL|SA_CORE, /* SIGSEGV */
89 SA_KILL|SA_CORE, /* SIGSYS */
90 SA_KILL, /* SIGPIPE */
91 SA_KILL, /* SIGALRM */
92 SA_KILL, /* SIGTERM */
93 SA_IGNORE, /* SIGURG */
94 SA_STOP, /* SIGSTOP */
95 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
96 SA_IGNORE|SA_CONT, /* SIGCONT */
97 SA_IGNORE, /* SIGCHLD */
98 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
99 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
100 SA_IGNORE, /* SIGIO */
101 SA_KILL, /* SIGXCPU */
102 SA_KILL, /* SIGXFSZ */
103 SA_KILL, /* SIGVTALRM */
104 SA_KILL, /* SIGPROF */
105 SA_IGNORE, /* SIGWINCH */
106 SA_IGNORE, /* SIGINFO */
107 SA_KILL, /* SIGUSR1 */
108 SA_KILL, /* SIGUSR2 */
109 SA_IGNORE, /* SIGTHR */
110 };
111
112 #define CONTSIGMASK (sigmask(SIGCONT))
113 #define STOPSIGMASK (sigmask(SIGSTOP) | sigmask(SIGTSTP) | \
114 sigmask(SIGTTIN) | sigmask(SIGTTOU))
115
116 void setsigvec(struct proc *, int, struct sigaction *);
117
118 int proc_trap(struct proc *, int);
119 void proc_stop(struct proc *p, int);
120 void proc_stop_sweep(void *);
121 void *proc_stop_si;
122
123 void setsigctx(struct proc *, int, struct sigctx *);
124 void postsig_done(struct proc *, int, sigset_t, int);
125 void postsig(struct proc *, int, struct sigctx *);
126 int cansignal(struct proc *, struct process *, int);
127
128 void ptsignal_locked(struct proc *, int, enum signal_type);
129
130 struct pool sigacts_pool; /* memory pool for sigacts structures */
131
132 void sigio_del(struct sigiolst *);
133 void sigio_unlink(struct sigio_ref *, struct sigiolst *);
134 struct mutex sigio_lock = MUTEX_INITIALIZER(IPL_HIGH);
135
136 /*
137 * Can thread p, send the signal signum to process qr?
138 */
139 int
cansignal(struct proc * p,struct process * qr,int signum)140 cansignal(struct proc *p, struct process *qr, int signum)
141 {
142 struct process *pr = p->p_p;
143 struct ucred *uc = p->p_ucred;
144 struct ucred *quc = qr->ps_ucred;
145
146 if (uc->cr_uid == 0)
147 return (1); /* root can always signal */
148
149 if (pr == qr)
150 return (1); /* process can always signal itself */
151
152 /* optimization: if the same creds then the tests below will pass */
153 if (uc == quc)
154 return (1);
155
156 if (signum == SIGCONT && qr->ps_session == pr->ps_session)
157 return (1); /* SIGCONT in session */
158
159 /*
160 * Using kill(), only certain signals can be sent to setugid
161 * child processes
162 */
163 if (qr->ps_flags & PS_SUGID) {
164 switch (signum) {
165 case 0:
166 case SIGKILL:
167 case SIGINT:
168 case SIGTERM:
169 case SIGALRM:
170 case SIGSTOP:
171 case SIGTTIN:
172 case SIGTTOU:
173 case SIGTSTP:
174 case SIGHUP:
175 case SIGUSR1:
176 case SIGUSR2:
177 if (uc->cr_ruid == quc->cr_ruid ||
178 uc->cr_uid == quc->cr_ruid)
179 return (1);
180 }
181 return (0);
182 }
183
184 if (uc->cr_ruid == quc->cr_ruid ||
185 uc->cr_ruid == quc->cr_svuid ||
186 uc->cr_uid == quc->cr_ruid ||
187 uc->cr_uid == quc->cr_svuid)
188 return (1);
189 return (0);
190 }
191
192 /*
193 * Initialize signal-related data structures.
194 */
195 void
signal_init(void)196 signal_init(void)
197 {
198 proc_stop_si = softintr_establish(IPL_SOFTCLOCK, proc_stop_sweep,
199 NULL);
200 if (proc_stop_si == NULL)
201 panic("signal_init failed to register softintr");
202
203 pool_init(&sigacts_pool, sizeof(struct sigacts), 0, IPL_NONE,
204 PR_WAITOK, "sigapl", NULL);
205 }
206
207 /*
208 * Initialize a new sigaltstack structure.
209 */
210 void
sigstkinit(struct sigaltstack * ss)211 sigstkinit(struct sigaltstack *ss)
212 {
213 ss->ss_flags = SS_DISABLE;
214 ss->ss_size = 0;
215 ss->ss_sp = NULL;
216 }
217
218 /*
219 * Create an initial sigacts structure, using the same signal state
220 * as pr.
221 */
222 struct sigacts *
sigactsinit(struct process * pr)223 sigactsinit(struct process *pr)
224 {
225 struct sigacts *ps;
226
227 ps = pool_get(&sigacts_pool, PR_WAITOK);
228 memcpy(ps, pr->ps_sigacts, sizeof(struct sigacts));
229 return (ps);
230 }
231
232 /*
233 * Release a sigacts structure.
234 */
235 void
sigactsfree(struct sigacts * ps)236 sigactsfree(struct sigacts *ps)
237 {
238 pool_put(&sigacts_pool, ps);
239 }
240
241 int
sys_sigaction(struct proc * p,void * v,register_t * retval)242 sys_sigaction(struct proc *p, void *v, register_t *retval)
243 {
244 struct sys_sigaction_args /* {
245 syscallarg(int) signum;
246 syscallarg(const struct sigaction *) nsa;
247 syscallarg(struct sigaction *) osa;
248 } */ *uap = v;
249 struct sigaction vec;
250 #ifdef KTRACE
251 struct sigaction ovec;
252 #endif
253 struct sigaction *sa;
254 const struct sigaction *nsa;
255 struct sigaction *osa;
256 struct sigacts *ps = p->p_p->ps_sigacts;
257 int signum;
258 int bit, error;
259
260 signum = SCARG(uap, signum);
261 nsa = SCARG(uap, nsa);
262 osa = SCARG(uap, osa);
263
264 if (signum <= 0 || signum >= NSIG ||
265 (nsa && (signum == SIGKILL || signum == SIGSTOP)))
266 return (EINVAL);
267 sa = &vec;
268 if (osa) {
269 mtx_enter(&p->p_p->ps_mtx);
270 sa->sa_handler = ps->ps_sigact[signum];
271 sa->sa_mask = ps->ps_catchmask[signum];
272 bit = sigmask(signum);
273 sa->sa_flags = 0;
274 if ((ps->ps_sigonstack & bit) != 0)
275 sa->sa_flags |= SA_ONSTACK;
276 if ((ps->ps_sigintr & bit) == 0)
277 sa->sa_flags |= SA_RESTART;
278 if ((ps->ps_sigreset & bit) != 0)
279 sa->sa_flags |= SA_RESETHAND;
280 if ((ps->ps_siginfo & bit) != 0)
281 sa->sa_flags |= SA_SIGINFO;
282 if (signum == SIGCHLD) {
283 if ((ps->ps_sigflags & SAS_NOCLDSTOP) != 0)
284 sa->sa_flags |= SA_NOCLDSTOP;
285 if ((ps->ps_sigflags & SAS_NOCLDWAIT) != 0)
286 sa->sa_flags |= SA_NOCLDWAIT;
287 }
288 mtx_leave(&p->p_p->ps_mtx);
289 if ((sa->sa_mask & bit) == 0)
290 sa->sa_flags |= SA_NODEFER;
291 sa->sa_mask &= ~bit;
292 error = copyout(sa, osa, sizeof (vec));
293 if (error)
294 return (error);
295 #ifdef KTRACE
296 if (KTRPOINT(p, KTR_STRUCT))
297 ovec = vec;
298 #endif
299 }
300 if (nsa) {
301 error = copyin(nsa, sa, sizeof (vec));
302 if (error)
303 return (error);
304 #ifdef KTRACE
305 if (KTRPOINT(p, KTR_STRUCT))
306 ktrsigaction(p, sa);
307 #endif
308 setsigvec(p, signum, sa);
309 }
310 #ifdef KTRACE
311 if (osa && KTRPOINT(p, KTR_STRUCT))
312 ktrsigaction(p, &ovec);
313 #endif
314 return (0);
315 }
316
317 void
setsigvec(struct proc * p,int signum,struct sigaction * sa)318 setsigvec(struct proc *p, int signum, struct sigaction *sa)
319 {
320 struct sigacts *ps = p->p_p->ps_sigacts;
321 int bit;
322
323 bit = sigmask(signum);
324
325 mtx_enter(&p->p_p->ps_mtx);
326 ps->ps_sigact[signum] = sa->sa_handler;
327 if ((sa->sa_flags & SA_NODEFER) == 0)
328 sa->sa_mask |= sigmask(signum);
329 ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
330 if (signum == SIGCHLD) {
331 if (sa->sa_flags & SA_NOCLDSTOP)
332 atomic_setbits_int(&ps->ps_sigflags, SAS_NOCLDSTOP);
333 else
334 atomic_clearbits_int(&ps->ps_sigflags, SAS_NOCLDSTOP);
335 /*
336 * If the SA_NOCLDWAIT flag is set or the handler
337 * is SIG_IGN we reparent the dying child to PID 1
338 * (init) which will reap the zombie. Because we use
339 * init to do our dirty work we never set SAS_NOCLDWAIT
340 * for PID 1.
341 * XXX exit1 rework means this is unnecessary?
342 */
343 if (initprocess->ps_sigacts != ps &&
344 ((sa->sa_flags & SA_NOCLDWAIT) ||
345 sa->sa_handler == SIG_IGN))
346 atomic_setbits_int(&ps->ps_sigflags, SAS_NOCLDWAIT);
347 else
348 atomic_clearbits_int(&ps->ps_sigflags, SAS_NOCLDWAIT);
349 }
350 if ((sa->sa_flags & SA_RESETHAND) != 0)
351 ps->ps_sigreset |= bit;
352 else
353 ps->ps_sigreset &= ~bit;
354 if ((sa->sa_flags & SA_SIGINFO) != 0)
355 ps->ps_siginfo |= bit;
356 else
357 ps->ps_siginfo &= ~bit;
358 if ((sa->sa_flags & SA_RESTART) == 0)
359 ps->ps_sigintr |= bit;
360 else
361 ps->ps_sigintr &= ~bit;
362 if ((sa->sa_flags & SA_ONSTACK) != 0)
363 ps->ps_sigonstack |= bit;
364 else
365 ps->ps_sigonstack &= ~bit;
366 /*
367 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
368 * and for signals set to SIG_DFL where the default is to ignore.
369 * However, don't put SIGCONT in ps_sigignore,
370 * as we have to restart the process.
371 */
372 if (sa->sa_handler == SIG_IGN ||
373 (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
374 atomic_clearbits_int(&p->p_siglist, bit);
375 atomic_clearbits_int(&p->p_p->ps_siglist, bit);
376 if (signum != SIGCONT)
377 ps->ps_sigignore |= bit; /* easier in psignal */
378 ps->ps_sigcatch &= ~bit;
379 } else {
380 ps->ps_sigignore &= ~bit;
381 if (sa->sa_handler == SIG_DFL)
382 ps->ps_sigcatch &= ~bit;
383 else
384 ps->ps_sigcatch |= bit;
385 }
386 mtx_leave(&p->p_p->ps_mtx);
387 }
388
389 /*
390 * Initialize signal state for process 0;
391 * set to ignore signals that are ignored by default.
392 */
393 void
siginit(struct sigacts * ps)394 siginit(struct sigacts *ps)
395 {
396 int i;
397
398 for (i = 0; i < NSIG; i++)
399 if (sigprop[i] & SA_IGNORE && i != SIGCONT)
400 ps->ps_sigignore |= sigmask(i);
401 ps->ps_sigflags = SAS_NOCLDWAIT | SAS_NOCLDSTOP;
402 }
403
404 /*
405 * Reset signals for an exec by the specified thread.
406 */
407 void
execsigs(struct proc * p)408 execsigs(struct proc *p)
409 {
410 struct sigacts *ps;
411 int nc, mask;
412
413 ps = p->p_p->ps_sigacts;
414 mtx_enter(&p->p_p->ps_mtx);
415
416 /*
417 * Reset caught signals. Held signals remain held
418 * through p_sigmask (unless they were caught,
419 * and are now ignored by default).
420 */
421 while (ps->ps_sigcatch) {
422 nc = ffs((long)ps->ps_sigcatch);
423 mask = sigmask(nc);
424 ps->ps_sigcatch &= ~mask;
425 if (sigprop[nc] & SA_IGNORE) {
426 if (nc != SIGCONT)
427 ps->ps_sigignore |= mask;
428 atomic_clearbits_int(&p->p_siglist, mask);
429 atomic_clearbits_int(&p->p_p->ps_siglist, mask);
430 }
431 ps->ps_sigact[nc] = SIG_DFL;
432 }
433 /*
434 * Reset stack state to the user stack.
435 * Clear set of signals caught on the signal stack.
436 */
437 sigstkinit(&p->p_sigstk);
438 atomic_clearbits_int(&ps->ps_sigflags, SAS_NOCLDWAIT);
439 if (ps->ps_sigact[SIGCHLD] == SIG_IGN)
440 ps->ps_sigact[SIGCHLD] = SIG_DFL;
441 mtx_leave(&p->p_p->ps_mtx);
442 }
443
444 /*
445 * Manipulate signal mask.
446 * Note that we receive new mask, not pointer,
447 * and return old mask as return value;
448 * the library stub does the rest.
449 */
450 int
sys_sigprocmask(struct proc * p,void * v,register_t * retval)451 sys_sigprocmask(struct proc *p, void *v, register_t *retval)
452 {
453 struct sys_sigprocmask_args /* {
454 syscallarg(int) how;
455 syscallarg(sigset_t) mask;
456 } */ *uap = v;
457 int error = 0;
458 sigset_t mask;
459
460 KASSERT(p == curproc);
461
462 *retval = p->p_sigmask;
463 mask = SCARG(uap, mask) &~ sigcantmask;
464
465 switch (SCARG(uap, how)) {
466 case SIG_BLOCK:
467 SET(p->p_sigmask, mask);
468 break;
469 case SIG_UNBLOCK:
470 CLR(p->p_sigmask, mask);
471 break;
472 case SIG_SETMASK:
473 p->p_sigmask = mask;
474 break;
475 default:
476 error = EINVAL;
477 break;
478 }
479 return (error);
480 }
481
482 int
sys_sigpending(struct proc * p,void * v,register_t * retval)483 sys_sigpending(struct proc *p, void *v, register_t *retval)
484 {
485 *retval = p->p_siglist | p->p_p->ps_siglist;
486 return (0);
487 }
488
489 /*
490 * Temporarily replace calling proc's signal mask for the duration of a
491 * system call. Original signal mask will be restored by userret().
492 */
493 void
dosigsuspend(struct proc * p,sigset_t newmask)494 dosigsuspend(struct proc *p, sigset_t newmask)
495 {
496 KASSERT(p == curproc);
497
498 p->p_oldmask = p->p_sigmask;
499 p->p_sigmask = newmask;
500 atomic_setbits_int(&p->p_flag, P_SIGSUSPEND);
501 }
502
503 /*
504 * Suspend thread until signal, providing mask to be set
505 * in the meantime. Note nonstandard calling convention:
506 * libc stub passes mask, not pointer, to save a copyin.
507 */
508 int
sys_sigsuspend(struct proc * p,void * v,register_t * retval)509 sys_sigsuspend(struct proc *p, void *v, register_t *retval)
510 {
511 struct sys_sigsuspend_args /* {
512 syscallarg(int) mask;
513 } */ *uap = v;
514
515 dosigsuspend(p, SCARG(uap, mask) &~ sigcantmask);
516 while (tsleep_nsec(&nowake, PPAUSE|PCATCH, "sigsusp", INFSLP) == 0)
517 continue;
518 /* always return EINTR rather than ERESTART... */
519 return (EINTR);
520 }
521
522 int
sigonstack(size_t stack)523 sigonstack(size_t stack)
524 {
525 const struct sigaltstack *ss = &curproc->p_sigstk;
526
527 return (ss->ss_flags & SS_DISABLE ? 0 :
528 (stack - (size_t)ss->ss_sp < ss->ss_size));
529 }
530
531 int
sys_sigaltstack(struct proc * p,void * v,register_t * retval)532 sys_sigaltstack(struct proc *p, void *v, register_t *retval)
533 {
534 struct sys_sigaltstack_args /* {
535 syscallarg(const struct sigaltstack *) nss;
536 syscallarg(struct sigaltstack *) oss;
537 } */ *uap = v;
538 struct sigaltstack ss;
539 const struct sigaltstack *nss;
540 struct sigaltstack *oss;
541 int onstack = sigonstack(PROC_STACK(p));
542 int error;
543
544 nss = SCARG(uap, nss);
545 oss = SCARG(uap, oss);
546
547 if (oss != NULL) {
548 ss = p->p_sigstk;
549 if (onstack)
550 ss.ss_flags |= SS_ONSTACK;
551 if ((error = copyout(&ss, oss, sizeof(ss))))
552 return (error);
553 }
554 if (nss == NULL)
555 return (0);
556 error = copyin(nss, &ss, sizeof(ss));
557 if (error)
558 return (error);
559 if (onstack)
560 return (EPERM);
561 if (ss.ss_flags & ~SS_DISABLE)
562 return (EINVAL);
563 if (ss.ss_flags & SS_DISABLE) {
564 p->p_sigstk.ss_flags = ss.ss_flags;
565 return (0);
566 }
567 if (ss.ss_size < MINSIGSTKSZ)
568 return (ENOMEM);
569
570 error = uvm_map_remap_as_stack(p, (vaddr_t)ss.ss_sp, ss.ss_size);
571 if (error)
572 return (error);
573
574 p->p_sigstk = ss;
575 return (0);
576 }
577
578 int
sys_kill(struct proc * cp,void * v,register_t * retval)579 sys_kill(struct proc *cp, void *v, register_t *retval)
580 {
581 struct sys_kill_args /* {
582 syscallarg(int) pid;
583 syscallarg(int) signum;
584 } */ *uap = v;
585 struct process *pr;
586 int pid = SCARG(uap, pid);
587 int signum = SCARG(uap, signum);
588 int error;
589 int zombie = 0;
590
591 if ((error = pledge_kill(cp, pid)) != 0)
592 return (error);
593 if (((u_int)signum) >= NSIG)
594 return (EINVAL);
595 if (pid > 0) {
596 if ((pr = prfind(pid)) == NULL) {
597 if ((pr = zombiefind(pid)) == NULL)
598 return (ESRCH);
599 else
600 zombie = 1;
601 }
602 if (!cansignal(cp, pr, signum))
603 return (EPERM);
604
605 /* kill single process */
606 if (signum && !zombie)
607 prsignal(pr, signum);
608 return (0);
609 }
610 switch (pid) {
611 case -1: /* broadcast signal */
612 return (killpg1(cp, signum, 0, 1));
613 case 0: /* signal own process group */
614 return (killpg1(cp, signum, 0, 0));
615 default: /* negative explicit process group */
616 return (killpg1(cp, signum, -pid, 0));
617 }
618 }
619
620 int
sys_thrkill(struct proc * cp,void * v,register_t * retval)621 sys_thrkill(struct proc *cp, void *v, register_t *retval)
622 {
623 struct sys_thrkill_args /* {
624 syscallarg(pid_t) tid;
625 syscallarg(int) signum;
626 syscallarg(void *) tcb;
627 } */ *uap = v;
628 struct proc *p;
629 int tid = SCARG(uap, tid);
630 int signum = SCARG(uap, signum);
631 void *tcb;
632
633 if (((u_int)signum) >= NSIG)
634 return (EINVAL);
635
636 p = tid ? tfind_user(tid, cp->p_p) : cp;
637 if (p == NULL)
638 return (ESRCH);
639
640 /* optionally require the target thread to have the given tcb addr */
641 tcb = SCARG(uap, tcb);
642 if (tcb != NULL && tcb != TCB_GET(p))
643 return (ESRCH);
644
645 if (signum)
646 ptsignal(p, signum, STHREAD);
647 return (0);
648 }
649
650 /*
651 * Common code for kill process group/broadcast kill.
652 * cp is calling process.
653 */
654 int
killpg1(struct proc * cp,int signum,int pgid,int all)655 killpg1(struct proc *cp, int signum, int pgid, int all)
656 {
657 struct process *pr;
658 struct pgrp *pgrp;
659 int nfound = 0;
660
661 if (all) {
662 /*
663 * broadcast
664 */
665 LIST_FOREACH(pr, &allprocess, ps_list) {
666 if (pr->ps_pid <= 1 ||
667 pr->ps_flags & (PS_SYSTEM | PS_NOBROADCASTKILL) ||
668 pr == cp->p_p || !cansignal(cp, pr, signum))
669 continue;
670 nfound++;
671 if (signum)
672 prsignal(pr, signum);
673 }
674 } else {
675 if (pgid == 0)
676 /*
677 * zero pgid means send to my process group.
678 */
679 pgrp = cp->p_p->ps_pgrp;
680 else {
681 pgrp = pgfind(pgid);
682 if (pgrp == NULL)
683 return (ESRCH);
684 }
685 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) {
686 if (pr->ps_pid <= 1 || pr->ps_flags & PS_SYSTEM ||
687 !cansignal(cp, pr, signum))
688 continue;
689 nfound++;
690 if (signum)
691 prsignal(pr, signum);
692 }
693 }
694 return (nfound ? 0 : ESRCH);
695 }
696
697 #define CANDELIVER(uid, euid, pr) \
698 (euid == 0 || \
699 (uid) == (pr)->ps_ucred->cr_ruid || \
700 (uid) == (pr)->ps_ucred->cr_svuid || \
701 (uid) == (pr)->ps_ucred->cr_uid || \
702 (euid) == (pr)->ps_ucred->cr_ruid || \
703 (euid) == (pr)->ps_ucred->cr_svuid || \
704 (euid) == (pr)->ps_ucred->cr_uid)
705
706 #define CANSIGIO(cr, pr) \
707 CANDELIVER((cr)->cr_ruid, (cr)->cr_uid, (pr))
708
709 /*
710 * Send a signal to a process group. If checktty is 1,
711 * limit to members which have a controlling terminal.
712 */
713 void
pgsignal(struct pgrp * pgrp,int signum,int checkctty)714 pgsignal(struct pgrp *pgrp, int signum, int checkctty)
715 {
716 struct process *pr;
717
718 if (pgrp)
719 LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist)
720 if (checkctty == 0 || pr->ps_flags & PS_CONTROLT)
721 prsignal(pr, signum);
722 }
723
724 /*
725 * Send a SIGIO or SIGURG signal to a process or process group using stored
726 * credentials rather than those of the current process.
727 */
728 void
pgsigio(struct sigio_ref * sir,int sig,int checkctty)729 pgsigio(struct sigio_ref *sir, int sig, int checkctty)
730 {
731 struct process *pr;
732 struct sigio *sigio;
733
734 if (sir->sir_sigio == NULL)
735 return;
736
737 KERNEL_LOCK();
738 mtx_enter(&sigio_lock);
739 sigio = sir->sir_sigio;
740 if (sigio == NULL)
741 goto out;
742 if (sigio->sio_pgid > 0) {
743 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc))
744 prsignal(sigio->sio_proc, sig);
745 } else if (sigio->sio_pgid < 0) {
746 LIST_FOREACH(pr, &sigio->sio_pgrp->pg_members, ps_pglist) {
747 if (CANSIGIO(sigio->sio_ucred, pr) &&
748 (checkctty == 0 || (pr->ps_flags & PS_CONTROLT)))
749 prsignal(pr, sig);
750 }
751 }
752 out:
753 mtx_leave(&sigio_lock);
754 KERNEL_UNLOCK();
755 }
756
757 /*
758 * Recalculate the signal mask and reset the signal disposition after
759 * usermode frame for delivery is formed.
760 */
761 void
postsig_done(struct proc * p,int signum,sigset_t catchmask,int reset)762 postsig_done(struct proc *p, int signum, sigset_t catchmask, int reset)
763 {
764 p->p_ru.ru_nsignals++;
765 SET(p->p_sigmask, catchmask);
766 if (reset != 0) {
767 sigset_t mask = sigmask(signum);
768 struct sigacts *ps = p->p_p->ps_sigacts;
769
770 mtx_enter(&p->p_p->ps_mtx);
771 ps->ps_sigcatch &= ~mask;
772 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
773 ps->ps_sigignore |= mask;
774 ps->ps_sigact[signum] = SIG_DFL;
775 mtx_leave(&p->p_p->ps_mtx);
776 }
777 }
778
779 /*
780 * Send a signal caused by a trap to the current thread
781 * If it will be caught immediately, deliver it with correct code.
782 * Otherwise, post it normally.
783 */
784 void
trapsignal(struct proc * p,int signum,u_long trapno,int code,union sigval sigval)785 trapsignal(struct proc *p, int signum, u_long trapno, int code,
786 union sigval sigval)
787 {
788 struct process *pr = p->p_p;
789 struct sigctx ctx;
790 int mask;
791
792 switch (signum) {
793 case SIGILL:
794 if (code == ILL_BTCFI) {
795 pr->ps_acflag |= ABTCFI;
796 break;
797 }
798 /* FALLTHROUGH */
799 case SIGBUS:
800 case SIGSEGV:
801 pr->ps_acflag |= ATRAP;
802 break;
803 }
804
805 mask = sigmask(signum);
806 setsigctx(p, signum, &ctx);
807 if ((pr->ps_flags & PS_TRACED) == 0 && ctx.sig_catch != 0 &&
808 (p->p_sigmask & mask) == 0) {
809 siginfo_t si;
810
811 initsiginfo(&si, signum, trapno, code, sigval);
812 #ifdef KTRACE
813 if (KTRPOINT(p, KTR_PSIG)) {
814 ktrpsig(p, signum, ctx.sig_action,
815 p->p_sigmask, code, &si);
816 }
817 #endif
818 if (sendsig(ctx.sig_action, signum, p->p_sigmask, &si,
819 ctx.sig_info, ctx.sig_onstack)) {
820 KERNEL_LOCK();
821 sigexit(p, SIGILL);
822 /* NOTREACHED */
823 }
824 postsig_done(p, signum, ctx.sig_catchmask, ctx.sig_reset);
825 } else {
826 p->p_sisig = signum;
827 p->p_sitrapno = trapno; /* XXX for core dump/debugger */
828 p->p_sicode = code;
829 p->p_sigval = sigval;
830
831 /*
832 * If traced, stop if signal is masked, and stay stopped
833 * until released by the debugger. If our parent process
834 * is waiting for us, don't hang as we could deadlock.
835 */
836 if (((pr->ps_flags & (PS_TRACED | PS_PPWAIT)) == PS_TRACED) &&
837 signum != SIGKILL && (p->p_sigmask & mask) != 0) {
838 signum = proc_trap(p, signum);
839
840 mask = sigmask(signum);
841 setsigctx(p, signum, &ctx);
842
843 /*
844 * If we are no longer being traced, or the parent
845 * didn't give us a signal, skip sending the signal.
846 */
847 if ((pr->ps_flags & PS_TRACED) == 0 || signum == 0)
848 return;
849
850 /* update signal info */
851 p->p_sisig = signum;
852 }
853
854 /*
855 * Signals like SIGBUS and SIGSEGV should not, when
856 * generated by the kernel, be ignorable or blockable.
857 * If it is and we're not being traced, then just kill
858 * the process.
859 * After vfs_shutdown(9), init(8) cannot receive signals
860 * because new code pages of the signal handler cannot be
861 * mapped from halted storage. init(8) may not die or the
862 * kernel panics. Better loop between signal handler and
863 * page fault trap until the machine is halted.
864 */
865 if ((pr->ps_flags & PS_TRACED) == 0 &&
866 (sigprop[signum] & SA_KILL) &&
867 ((p->p_sigmask & mask) || ctx.sig_ignore) &&
868 pr->ps_pid != 1) {
869 KERNEL_LOCK();
870 sigexit(p, signum);
871 /* NOTREACHED */
872 }
873 ptsignal(p, signum, STHREAD);
874 }
875 }
876
877 /*
878 * Send the signal to the process. If the signal has an action, the action
879 * is usually performed by the target process rather than the caller; we add
880 * the signal to the set of pending signals for the process.
881 *
882 * Exceptions:
883 * o When a stop signal is sent to a sleeping process that takes the
884 * default action, the process is stopped without awakening it.
885 * o SIGCONT restarts stopped processes (or puts them back to sleep)
886 * regardless of the signal action (eg, blocked or ignored).
887 *
888 * Other ignored signals are discarded immediately.
889 */
890 void
psignal(struct proc * p,int signum)891 psignal(struct proc *p, int signum)
892 {
893 ptsignal(p, signum, SPROCESS);
894 }
895
896 void
prsignal(struct process * pr,int signum)897 prsignal(struct process *pr, int signum)
898 {
899 mtx_enter(&pr->ps_mtx);
900 /* Ignore signal if the target process is exiting */
901 if (pr->ps_flags & PS_EXITING) {
902 mtx_leave(&pr->ps_mtx);
903 return;
904 }
905 ptsignal_locked(TAILQ_FIRST(&pr->ps_threads), signum, SPROCESS);
906 mtx_leave(&pr->ps_mtx);
907 }
908
909 /*
910 * type = SPROCESS process signal, can be diverted (sigwait())
911 * type = STHREAD thread signal, but should be propagated if unhandled
912 * type = SPROPAGATED propagated to this thread, so don't propagate again
913 */
914 void
ptsignal(struct proc * p,int signum,enum signal_type type)915 ptsignal(struct proc *p, int signum, enum signal_type type)
916 {
917 struct process *pr = p->p_p;
918
919 mtx_enter(&pr->ps_mtx);
920 ptsignal_locked(p, signum, type);
921 mtx_leave(&pr->ps_mtx);
922 }
923
924 void
ptsignal_locked(struct proc * p,int signum,enum signal_type type)925 ptsignal_locked(struct proc *p, int signum, enum signal_type type)
926 {
927 int prop;
928 sig_t action, altaction = SIG_DFL;
929 sigset_t mask, sigmask;
930 int *siglist;
931 struct process *pr = p->p_p;
932 struct proc *q;
933 int wakeparent = 0;
934
935 MUTEX_ASSERT_LOCKED(&pr->ps_mtx);
936
937 #ifdef DIAGNOSTIC
938 if ((u_int)signum >= NSIG || signum == 0)
939 panic("psignal signal number");
940 #endif
941
942 /* Ignore signal if the target process is exiting */
943 if (pr->ps_flags & PS_EXITING)
944 return;
945
946 mask = sigmask(signum);
947 sigmask = READ_ONCE(p->p_sigmask);
948
949 if (type == SPROCESS) {
950 sigset_t tmpmask;
951
952 /* Accept SIGKILL to coredumping processes */
953 if (pr->ps_flags & PS_COREDUMP && signum == SIGKILL) {
954 atomic_setbits_int(&pr->ps_siglist, mask);
955 return;
956 }
957
958 /*
959 * If the current thread can process the signal
960 * immediately (it's unblocked) then have it take it.
961 */
962 q = curproc;
963 tmpmask = READ_ONCE(q->p_sigmask);
964 if (q->p_p == pr && (q->p_flag & P_WEXIT) == 0 &&
965 (tmpmask & mask) == 0) {
966 p = q;
967 sigmask = tmpmask;
968 } else {
969 /*
970 * A process-wide signal can be diverted to a
971 * different thread that's in sigwait() for this
972 * signal. If there isn't such a thread, then
973 * pick a thread that doesn't have it blocked so
974 * that the stop/kill consideration isn't
975 * delayed. Otherwise, mark it pending on the
976 * main thread.
977 */
978 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
979
980 /* ignore exiting threads */
981 if (q->p_flag & P_WEXIT)
982 continue;
983
984 /* skip threads that have the signal blocked */
985 tmpmask = READ_ONCE(q->p_sigmask);
986 if ((tmpmask & mask) != 0)
987 continue;
988
989 /* okay, could send to this thread */
990 p = q;
991 sigmask = tmpmask;
992
993 /*
994 * sigsuspend, sigwait, ppoll/pselect, etc?
995 * Definitely go to this thread, as it's
996 * already blocked in the kernel.
997 */
998 if (q->p_flag & P_SIGSUSPEND)
999 break;
1000 }
1001 }
1002 }
1003
1004 if (type != SPROPAGATED)
1005 knote_locked(&pr->ps_klist, NOTE_SIGNAL | signum);
1006
1007 prop = sigprop[signum];
1008
1009 /*
1010 * If proc is traced, always give parent a chance.
1011 */
1012 if (pr->ps_flags & PS_TRACED) {
1013 action = SIG_DFL;
1014 } else {
1015 sigset_t sigcatch, sigignore;
1016
1017 /*
1018 * If the signal is being ignored,
1019 * then we forget about it immediately.
1020 * (Note: we don't set SIGCONT in ps_sigignore,
1021 * and if it is set to SIG_IGN,
1022 * action will be SIG_DFL here.)
1023 */
1024 sigignore = pr->ps_sigacts->ps_sigignore;
1025 sigcatch = pr->ps_sigacts->ps_sigcatch;
1026
1027 if (sigignore & mask)
1028 return;
1029 if (sigmask & mask) {
1030 action = SIG_HOLD;
1031 if (sigcatch & mask)
1032 altaction = SIG_CATCH;
1033 } else if (sigcatch & mask) {
1034 action = SIG_CATCH;
1035 } else {
1036 action = SIG_DFL;
1037
1038 if (prop & SA_KILL && pr->ps_nice > NZERO)
1039 pr->ps_nice = NZERO;
1040
1041 /*
1042 * If sending a tty stop signal to a member of an
1043 * orphaned process group, discard the signal here if
1044 * the action is default; don't stop the process below
1045 * if sleeping, and don't clear any pending SIGCONT.
1046 */
1047 if (prop & SA_TTYSTOP && pr->ps_pgrp->pg_jobc == 0)
1048 return;
1049 }
1050 }
1051 /*
1052 * If delivered to process, mark as pending there. Continue and stop
1053 * signals will be propagated to all threads. So they are always
1054 * marked at thread level.
1055 */
1056 siglist = (type == SPROCESS) ? &pr->ps_siglist : &p->p_siglist;
1057 if (prop & (SA_CONT | SA_STOP))
1058 siglist = &p->p_siglist;
1059
1060 /*
1061 * XXX delay processing of SA_STOP signals unless action == SIG_DFL?
1062 */
1063 if (prop & (SA_CONT | SA_STOP) && type != SPROPAGATED)
1064 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link)
1065 if (q != p)
1066 ptsignal_locked(q, signum, SPROPAGATED);
1067
1068 SCHED_LOCK();
1069
1070 switch (p->p_stat) {
1071
1072 case SSTOP:
1073 /*
1074 * If traced process is already stopped,
1075 * then no further action is necessary.
1076 */
1077 if (pr->ps_flags & PS_TRACED)
1078 goto out;
1079
1080 /*
1081 * Kill signal always sets processes running.
1082 */
1083 if (signum == SIGKILL) {
1084 atomic_clearbits_int(&p->p_flag, P_SUSPSIG);
1085 /* Raise priority to at least PUSER. */
1086 if (p->p_usrpri > PUSER)
1087 p->p_usrpri = PUSER;
1088 unsleep(p);
1089 setrunnable(p);
1090 goto out;
1091 }
1092
1093 if (prop & SA_CONT) {
1094 /*
1095 * If SIGCONT is default (or ignored), we continue the
1096 * process but don't leave the signal in p_siglist, as
1097 * it has no further action. If SIGCONT is held, we
1098 * continue the process and leave the signal in
1099 * p_siglist. If the process catches SIGCONT, let it
1100 * handle the signal itself. If it isn't waiting on
1101 * an event, then it goes back to run state.
1102 * Otherwise, process goes back to sleep state.
1103 */
1104 atomic_setbits_int(&pr->ps_flags, PS_CONTINUED);
1105 atomic_clearbits_int(&pr->ps_flags,
1106 PS_WAITED | PS_STOPPED);
1107 atomic_clearbits_int(&p->p_flag, P_SUSPSIG);
1108 wakeparent = 1;
1109 if (action == SIG_DFL)
1110 mask = 0;
1111 if (action == SIG_CATCH) {
1112 /* Raise priority to at least PUSER. */
1113 if (p->p_usrpri > PUSER)
1114 p->p_usrpri = PUSER;
1115 unsleep(p);
1116 setrunnable(p);
1117 goto out;
1118 }
1119 if (p->p_wchan == NULL) {
1120 unsleep(p);
1121 setrunnable(p);
1122 goto out;
1123 }
1124 atomic_clearbits_int(&p->p_flag, P_WSLEEP);
1125 p->p_stat = SSLEEP;
1126 goto out;
1127 }
1128
1129 /*
1130 * Defer further processing for signals which are held,
1131 * except that stopped processes must be continued by SIGCONT.
1132 */
1133 if (action == SIG_HOLD)
1134 goto out;
1135
1136 if (prop & SA_STOP) {
1137 /*
1138 * Already stopped, don't need to stop again.
1139 * (If we did the shell could get confused.)
1140 */
1141 mask = 0;
1142 goto out;
1143 }
1144
1145 /*
1146 * If process is sleeping interruptibly, then simulate a
1147 * wakeup so that when it is continued, it will be made
1148 * runnable and can look at the signal. But don't make
1149 * the process runnable, leave it stopped.
1150 */
1151 if (p->p_flag & P_SINTR)
1152 unsleep(p);
1153 goto out;
1154
1155 case SSLEEP:
1156 /*
1157 * If process is sleeping uninterruptibly
1158 * we can't interrupt the sleep... the signal will
1159 * be noticed when the process returns through
1160 * trap() or syscall().
1161 */
1162 if ((p->p_flag & P_SINTR) == 0)
1163 goto out;
1164 /*
1165 * Process is sleeping and traced... make it runnable
1166 * so it can discover the signal in cursig() and stop
1167 * for the parent.
1168 */
1169 if (pr->ps_flags & PS_TRACED) {
1170 unsleep(p);
1171 setrunnable(p);
1172 goto out;
1173 }
1174
1175 /*
1176 * Recheck sigmask before waking up the process,
1177 * there is a chance that while sending the signal
1178 * the process changed sigmask and went to sleep.
1179 */
1180 sigmask = READ_ONCE(p->p_sigmask);
1181 if (sigmask & mask)
1182 goto out;
1183 else if (action == SIG_HOLD) {
1184 /* signal got unmasked, get proper action */
1185 action = altaction;
1186
1187 if (action == SIG_DFL) {
1188 if (prop & SA_KILL && pr->ps_nice > NZERO)
1189 pr->ps_nice = NZERO;
1190
1191 /*
1192 * Discard tty stop signals sent to an
1193 * orphaned process group, see above.
1194 */
1195 if (prop & SA_TTYSTOP &&
1196 pr->ps_pgrp->pg_jobc == 0) {
1197 mask = 0;
1198 prop = 0;
1199 goto out;
1200 }
1201 }
1202 }
1203
1204 /*
1205 * If SIGCONT is default (or ignored) and process is
1206 * asleep, we are finished; the process should not
1207 * be awakened.
1208 */
1209 if ((prop & SA_CONT) && action == SIG_DFL) {
1210 mask = 0;
1211 goto out;
1212 }
1213 /*
1214 * When a sleeping process receives a stop
1215 * signal, process immediately if possible.
1216 */
1217 if ((prop & SA_STOP) && action == SIG_DFL) {
1218 /*
1219 * If a child holding parent blocked,
1220 * stopping could cause deadlock.
1221 */
1222 if (pr->ps_flags & PS_PPWAIT)
1223 goto out;
1224 mask = 0;
1225 pr->ps_xsig = signum;
1226 proc_stop(p, 0);
1227 goto out;
1228 }
1229 /*
1230 * All other (caught or default) signals
1231 * cause the process to run.
1232 * Raise priority to at least PUSER.
1233 */
1234 if (p->p_usrpri > PUSER)
1235 p->p_usrpri = PUSER;
1236 unsleep(p);
1237 setrunnable(p);
1238 goto out;
1239 /* NOTREACHED */
1240
1241 case SONPROC:
1242 if (action == SIG_HOLD)
1243 goto out;
1244
1245 /* set siglist before issuing the ast */
1246 atomic_setbits_int(siglist, mask);
1247 mask = 0;
1248 signotify(p);
1249 /* FALLTHROUGH */
1250 default:
1251 /*
1252 * SRUN, SIDL, SDEAD do nothing with the signal,
1253 * other than kicking ourselves if we are running.
1254 * It will either never be noticed, or noticed very soon.
1255 */
1256 goto out;
1257 }
1258 /* NOTREACHED */
1259
1260 out:
1261 /* finally adjust siglist */
1262 if (mask)
1263 atomic_setbits_int(siglist, mask);
1264 if (prop & SA_CONT) {
1265 atomic_clearbits_int(siglist, STOPSIGMASK);
1266 }
1267 if (prop & SA_STOP) {
1268 atomic_clearbits_int(siglist, CONTSIGMASK);
1269 atomic_clearbits_int(&pr->ps_flags, PS_CONTINUED);
1270 }
1271
1272 SCHED_UNLOCK();
1273 if (wakeparent)
1274 wakeup(pr->ps_pptr);
1275 }
1276
1277 /* fill the signal context which should be used by postsig() and issignal() */
1278 void
setsigctx(struct proc * p,int signum,struct sigctx * sctx)1279 setsigctx(struct proc *p, int signum, struct sigctx *sctx)
1280 {
1281 struct process *pr = p->p_p;
1282 struct sigacts *ps = pr->ps_sigacts;
1283 sigset_t mask;
1284
1285 mtx_enter(&pr->ps_mtx);
1286 mask = sigmask(signum);
1287 sctx->sig_action = ps->ps_sigact[signum];
1288 sctx->sig_catchmask = ps->ps_catchmask[signum];
1289 sctx->sig_reset = (ps->ps_sigreset & mask) != 0;
1290 sctx->sig_info = (ps->ps_siginfo & mask) != 0;
1291 sctx->sig_intr = (ps->ps_sigintr & mask) != 0;
1292 sctx->sig_onstack = (ps->ps_sigonstack & mask) != 0;
1293 sctx->sig_ignore = (ps->ps_sigignore & mask) != 0;
1294 sctx->sig_catch = (ps->ps_sigcatch & mask) != 0;
1295 sctx->sig_stop = sigprop[signum] & SA_STOP &&
1296 (long)sctx->sig_action == (long)SIG_DFL;
1297 if (sctx->sig_stop) {
1298 /*
1299 * If the process is a member of an orphaned
1300 * process group, ignore tty stop signals.
1301 */
1302 if (pr->ps_flags & PS_TRACED ||
1303 (pr->ps_pgrp->pg_jobc == 0 &&
1304 sigprop[signum] & SA_TTYSTOP)) {
1305 sctx->sig_stop = 0;
1306 sctx->sig_ignore = 1;
1307 }
1308 }
1309 mtx_leave(&pr->ps_mtx);
1310 }
1311
1312 /*
1313 * Determine signal that should be delivered to process p, the current
1314 * process, 0 if none.
1315 *
1316 * If the current process has received a signal (should be caught or cause
1317 * termination, should interrupt current syscall), return the signal number.
1318 * Stop signals with default action are processed immediately, then cleared;
1319 * they aren't returned. This is checked after each entry to the system for
1320 * a syscall or trap. The normal call sequence is
1321 *
1322 * while (signum = cursig(curproc, &ctx, 0))
1323 * postsig(signum, &ctx);
1324 *
1325 * Assumes that if the P_SINTR flag is set, we're holding both the
1326 * kernel and scheduler locks.
1327 */
1328 int
cursig(struct proc * p,struct sigctx * sctx,int deep)1329 cursig(struct proc *p, struct sigctx *sctx, int deep)
1330 {
1331 struct process *pr = p->p_p;
1332 int signum, mask, prop;
1333 sigset_t ps_siglist;
1334
1335 KASSERT(p == curproc);
1336
1337 for (;;) {
1338 ps_siglist = READ_ONCE(pr->ps_siglist);
1339 membar_consumer();
1340 mask = SIGPENDING(p);
1341 if (pr->ps_flags & PS_PPWAIT)
1342 mask &= ~STOPSIGMASK;
1343 if (mask == 0) /* no signal to send */
1344 return (0);
1345 signum = ffs((long)mask);
1346 mask = sigmask(signum);
1347
1348 /* take the signal! */
1349 if (atomic_cas_uint(&pr->ps_siglist, ps_siglist,
1350 ps_siglist & ~mask) != ps_siglist) {
1351 /* lost race taking the process signal, restart */
1352 continue;
1353 }
1354 atomic_clearbits_int(&p->p_siglist, mask);
1355 setsigctx(p, signum, sctx);
1356
1357 /*
1358 * We should see pending but ignored signals
1359 * only if PS_TRACED was on when they were posted.
1360 */
1361 if (sctx->sig_ignore && (pr->ps_flags & PS_TRACED) == 0)
1362 continue;
1363
1364 /*
1365 * If cursig is called while going to sleep, abort now
1366 * and stop the sleep. When the call unwinded to userret
1367 * cursig is called again and there the signal can be
1368 * handled cleanly.
1369 */
1370 if (deep)
1371 goto keep;
1372
1373 /*
1374 * If traced, always stop, and stay stopped until released
1375 * by the debugger. If our parent process is waiting for
1376 * us, don't hang as we could deadlock.
1377 */
1378 if (((pr->ps_flags & (PS_TRACED | PS_PPWAIT)) == PS_TRACED) &&
1379 signum != SIGKILL) {
1380 signum = proc_trap(p, signum);
1381
1382 mask = sigmask(signum);
1383 setsigctx(p, signum, sctx);
1384
1385 /*
1386 * If we are no longer being traced, or the parent
1387 * didn't give us a signal, or the signal is ignored,
1388 * look for more signals.
1389 */
1390 if ((pr->ps_flags & PS_TRACED) == 0 || signum == 0 ||
1391 sctx->sig_ignore)
1392 continue;
1393
1394 /*
1395 * If the new signal is being masked, look for other
1396 * signals but leave it for later.
1397 */
1398 if ((p->p_sigmask & mask) != 0) {
1399 atomic_setbits_int(&p->p_siglist, mask);
1400 continue;
1401 }
1402
1403 }
1404
1405 prop = sigprop[signum];
1406
1407 /*
1408 * Decide whether the signal should be returned.
1409 * Return the signal's number, or fall through
1410 * to clear it from the pending mask.
1411 */
1412 switch ((long)sctx->sig_action) {
1413 case (long)SIG_DFL:
1414 /*
1415 * Don't take default actions on system processes.
1416 */
1417 if (pr->ps_pid <= 1) {
1418 #ifdef DIAGNOSTIC
1419 /*
1420 * Are you sure you want to ignore SIGSEGV
1421 * in init? XXX
1422 */
1423 printf("Process (pid %d) got signal"
1424 " %d\n", pr->ps_pid, signum);
1425 #endif
1426 break; /* == ignore */
1427 }
1428 /*
1429 * If there is a pending stop signal to process
1430 * with default action, stop here,
1431 * then clear the signal.
1432 */
1433 if (sctx->sig_stop) {
1434 mtx_enter(&pr->ps_mtx);
1435 if (pr->ps_flags & PS_TRACED ||
1436 (pr->ps_pgrp->pg_jobc == 0 &&
1437 prop & SA_TTYSTOP)) {
1438 mtx_leave(&pr->ps_mtx);
1439 break; /* == ignore */
1440 }
1441 mtx_leave(&pr->ps_mtx);
1442 pr->ps_xsig = signum;
1443 SCHED_LOCK();
1444 proc_stop(p, 1);
1445 SCHED_UNLOCK();
1446 break;
1447 } else if (prop & SA_IGNORE) {
1448 /*
1449 * Except for SIGCONT, shouldn't get here.
1450 * Default action is to ignore; drop it.
1451 */
1452 break; /* == ignore */
1453 } else
1454 goto keep;
1455 /* NOTREACHED */
1456 case (long)SIG_IGN:
1457 /*
1458 * Masking above should prevent us ever trying
1459 * to take action on an ignored signal other
1460 * than SIGCONT, unless process is traced.
1461 */
1462 if ((prop & SA_CONT) == 0 &&
1463 (pr->ps_flags & PS_TRACED) == 0)
1464 printf("%s\n", __func__);
1465 break; /* == ignore */
1466 default:
1467 /*
1468 * This signal has an action, let
1469 * postsig() process it.
1470 */
1471 goto keep;
1472 }
1473 }
1474 /* NOTREACHED */
1475
1476 keep:
1477 atomic_setbits_int(&p->p_siglist, mask); /*leave the signal for later */
1478 return (signum);
1479 }
1480
1481 int
proc_trap(struct proc * p,int signum)1482 proc_trap(struct proc *p, int signum)
1483 {
1484 struct process *pr = p->p_p;
1485
1486 single_thread_set(p, SINGLE_SUSPEND | SINGLE_NOWAIT);
1487 pr->ps_xsig = signum;
1488
1489 SCHED_LOCK();
1490 proc_stop(p, 1);
1491 SCHED_UNLOCK();
1492
1493 signum = pr->ps_xsig;
1494 pr->ps_xsig = 0;
1495 if ((p->p_flag & P_TRACESINGLE) == 0)
1496 single_thread_clear(p, 0);
1497 atomic_clearbits_int(&p->p_flag, P_TRACESINGLE);
1498
1499 return signum;
1500 }
1501
1502 /*
1503 * Put the argument process into the stopped state and notify the parent
1504 * via wakeup. Signals are handled elsewhere. The process must not be
1505 * on the run queue.
1506 */
1507 void
proc_stop(struct proc * p,int sw)1508 proc_stop(struct proc *p, int sw)
1509 {
1510 struct process *pr = p->p_p;
1511
1512 #ifdef MULTIPROCESSOR
1513 SCHED_ASSERT_LOCKED();
1514 #endif
1515 /* do not stop exiting procs */
1516 if (ISSET(p->p_flag, P_WEXIT))
1517 return;
1518
1519 p->p_stat = SSTOP;
1520 atomic_clearbits_int(&pr->ps_flags, PS_WAITED);
1521 atomic_setbits_int(&pr->ps_flags, PS_STOPPING);
1522 atomic_setbits_int(&p->p_flag, P_SUSPSIG);
1523 /*
1524 * We need this soft interrupt to be handled fast.
1525 * Extra calls to softclock don't hurt.
1526 */
1527 softintr_schedule(proc_stop_si);
1528 if (sw)
1529 mi_switch();
1530 }
1531
1532 /*
1533 * Called from a soft interrupt to send signals to the parents of stopped
1534 * processes.
1535 * We can't do this in proc_stop because it's called with nasty locks held
1536 * and we would need recursive scheduler lock to deal with that.
1537 */
1538 void
proc_stop_sweep(void * v)1539 proc_stop_sweep(void *v)
1540 {
1541 struct process *pr;
1542
1543 LIST_FOREACH(pr, &allprocess, ps_list) {
1544 if ((pr->ps_flags & PS_STOPPING) == 0)
1545 continue;
1546 atomic_setbits_int(&pr->ps_flags, PS_STOPPED);
1547 atomic_clearbits_int(&pr->ps_flags, PS_STOPPING);
1548
1549 if ((pr->ps_pptr->ps_sigacts->ps_sigflags & SAS_NOCLDSTOP) == 0)
1550 prsignal(pr->ps_pptr, SIGCHLD);
1551 wakeup(pr->ps_pptr);
1552 }
1553 }
1554
1555 /*
1556 * Take the action for the specified signal
1557 * from the current set of pending signals.
1558 */
1559 void
postsig(struct proc * p,int signum,struct sigctx * sctx)1560 postsig(struct proc *p, int signum, struct sigctx *sctx)
1561 {
1562 u_long trapno;
1563 int mask, returnmask;
1564 siginfo_t si;
1565 union sigval sigval;
1566 int code;
1567
1568 KASSERT(signum != 0);
1569
1570 mask = sigmask(signum);
1571 atomic_clearbits_int(&p->p_siglist, mask);
1572 sigval.sival_ptr = NULL;
1573
1574 if (p->p_sisig != signum) {
1575 trapno = 0;
1576 code = SI_USER;
1577 sigval.sival_ptr = NULL;
1578 } else {
1579 trapno = p->p_sitrapno;
1580 code = p->p_sicode;
1581 sigval = p->p_sigval;
1582 }
1583 initsiginfo(&si, signum, trapno, code, sigval);
1584
1585 #ifdef KTRACE
1586 if (KTRPOINT(p, KTR_PSIG)) {
1587 ktrpsig(p, signum, sctx->sig_action, p->p_flag & P_SIGSUSPEND ?
1588 p->p_oldmask : p->p_sigmask, code, &si);
1589 }
1590 #endif
1591 if (sctx->sig_action == SIG_DFL) {
1592 /*
1593 * Default action, where the default is to kill
1594 * the process. (Other cases were ignored above.)
1595 */
1596 KERNEL_LOCK();
1597 sigexit(p, signum);
1598 /* NOTREACHED */
1599 } else {
1600 /*
1601 * If we get here, the signal must be caught.
1602 */
1603 #ifdef DIAGNOSTIC
1604 if (sctx->sig_action == SIG_IGN || (p->p_sigmask & mask))
1605 panic("postsig action");
1606 #endif
1607 /*
1608 * Set the new mask value and also defer further
1609 * occurrences of this signal.
1610 *
1611 * Special case: user has done a sigpause. Here the
1612 * current mask is not of interest, but rather the
1613 * mask from before the sigpause is what we want
1614 * restored after the signal processing is completed.
1615 */
1616 if (p->p_flag & P_SIGSUSPEND) {
1617 atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND);
1618 returnmask = p->p_oldmask;
1619 } else {
1620 returnmask = p->p_sigmask;
1621 }
1622 if (p->p_sisig == signum) {
1623 p->p_sisig = 0;
1624 p->p_sitrapno = 0;
1625 p->p_sicode = SI_USER;
1626 p->p_sigval.sival_ptr = NULL;
1627 }
1628
1629 if (sendsig(sctx->sig_action, signum, returnmask, &si,
1630 sctx->sig_info, sctx->sig_onstack)) {
1631 KERNEL_LOCK();
1632 sigexit(p, SIGILL);
1633 /* NOTREACHED */
1634 }
1635 postsig_done(p, signum, sctx->sig_catchmask, sctx->sig_reset);
1636 }
1637 }
1638
1639 /*
1640 * Force the current process to exit with the specified signal, dumping core
1641 * if appropriate. We bypass the normal tests for masked and caught signals,
1642 * allowing unrecoverable failures to terminate the process without changing
1643 * signal state. Mark the accounting record with the signal termination.
1644 * If dumping core, save the signal number for the debugger. Calls exit and
1645 * does not return.
1646 */
1647 void
sigexit(struct proc * p,int signum)1648 sigexit(struct proc *p, int signum)
1649 {
1650 /* Mark process as going away */
1651 atomic_setbits_int(&p->p_flag, P_WEXIT);
1652
1653 p->p_p->ps_acflag |= AXSIG;
1654 if (sigprop[signum] & SA_CORE) {
1655 p->p_sisig = signum;
1656
1657 /* if there are other threads, pause them */
1658 if (P_HASSIBLING(p))
1659 single_thread_set(p, SINGLE_UNWIND);
1660
1661 if (coredump(p) == 0)
1662 signum |= WCOREFLAG;
1663 }
1664 exit1(p, 0, signum, EXIT_NORMAL);
1665 /* NOTREACHED */
1666 }
1667
1668 /*
1669 * Send uncatchable SIGABRT for coredump.
1670 */
1671 void
sigabort(struct proc * p)1672 sigabort(struct proc *p)
1673 {
1674 struct sigaction sa;
1675
1676 KASSERT(p == curproc || panicstr || db_active);
1677
1678 memset(&sa, 0, sizeof sa);
1679 sa.sa_handler = SIG_DFL;
1680 setsigvec(p, SIGABRT, &sa);
1681 CLR(p->p_sigmask, sigmask(SIGABRT));
1682 psignal(p, SIGABRT);
1683 }
1684
1685 /*
1686 * Return 1 if `sig', a given signal, is ignored or masked for `p', a given
1687 * thread, and 0 otherwise.
1688 */
1689 int
sigismasked(struct proc * p,int sig)1690 sigismasked(struct proc *p, int sig)
1691 {
1692 struct process *pr = p->p_p;
1693 int rv;
1694
1695 KASSERT(p == curproc);
1696
1697 mtx_enter(&pr->ps_mtx);
1698 rv = (pr->ps_sigacts->ps_sigignore & sigmask(sig)) ||
1699 (p->p_sigmask & sigmask(sig));
1700 mtx_leave(&pr->ps_mtx);
1701
1702 return !!rv;
1703 }
1704
1705 struct coredump_iostate {
1706 struct proc *io_proc;
1707 struct vnode *io_vp;
1708 struct ucred *io_cred;
1709 off_t io_offset;
1710 };
1711
1712 /*
1713 * Dump core, into a file named "progname.core", unless the process was
1714 * setuid/setgid.
1715 */
1716 int
coredump(struct proc * p)1717 coredump(struct proc *p)
1718 {
1719 #ifdef SMALL_KERNEL
1720 return EPERM;
1721 #else
1722 struct process *pr = p->p_p;
1723 struct vnode *vp;
1724 struct ucred *cred = p->p_ucred;
1725 struct vmspace *vm = p->p_vmspace;
1726 struct nameidata nd;
1727 struct vattr vattr;
1728 struct coredump_iostate io;
1729 int error, len, incrash = 0;
1730 char *name;
1731 const char *dir = "/var/crash";
1732
1733 atomic_setbits_int(&pr->ps_flags, PS_COREDUMP);
1734
1735 #ifdef PMAP_CHECK_COPYIN
1736 /* disable copyin checks, so we can write out text sections if needed */
1737 p->p_vmspace->vm_map.check_copyin_count = 0;
1738 #endif
1739
1740 /* Don't dump if will exceed file size limit. */
1741 if (USPACE + ptoa(vm->vm_dsize + vm->vm_ssize) >= lim_cur(RLIMIT_CORE))
1742 return (EFBIG);
1743
1744 name = pool_get(&namei_pool, PR_WAITOK);
1745
1746 /*
1747 * If the process has inconsistent uids, nosuidcoredump
1748 * determines coredump placement policy.
1749 */
1750 if (((pr->ps_flags & PS_SUGID) && (error = suser(p))) ||
1751 ((pr->ps_flags & PS_SUGID) && nosuidcoredump)) {
1752 if (nosuidcoredump == 3) {
1753 /*
1754 * If the program directory does not exist, dumps of
1755 * that core will silently fail.
1756 */
1757 len = snprintf(name, MAXPATHLEN, "%s/%s/%u.core",
1758 dir, pr->ps_comm, pr->ps_pid);
1759 incrash = KERNELPATH;
1760 } else if (nosuidcoredump == 2) {
1761 len = snprintf(name, MAXPATHLEN, "%s/%s.core",
1762 dir, pr->ps_comm);
1763 incrash = KERNELPATH;
1764 } else {
1765 pool_put(&namei_pool, name);
1766 return (EPERM);
1767 }
1768 } else
1769 len = snprintf(name, MAXPATHLEN, "%s.core", pr->ps_comm);
1770
1771 if (len >= MAXPATHLEN) {
1772 pool_put(&namei_pool, name);
1773 return (EACCES);
1774 }
1775
1776 /*
1777 * Control the UID used to write out. The normal case uses
1778 * the real UID. If the sugid case is going to write into the
1779 * controlled directory, we do so as root.
1780 */
1781 if (incrash == 0) {
1782 cred = crdup(cred);
1783 cred->cr_uid = cred->cr_ruid;
1784 cred->cr_gid = cred->cr_rgid;
1785 } else {
1786 if (p->p_fd->fd_rdir) {
1787 vrele(p->p_fd->fd_rdir);
1788 p->p_fd->fd_rdir = NULL;
1789 }
1790 p->p_ucred = crdup(p->p_ucred);
1791 crfree(cred);
1792 cred = p->p_ucred;
1793 crhold(cred);
1794 cred->cr_uid = 0;
1795 cred->cr_gid = 0;
1796 }
1797
1798 /* incrash should be 0 or KERNELPATH only */
1799 NDINIT(&nd, 0, BYPASSUNVEIL | incrash, UIO_SYSSPACE, name, p);
1800
1801 error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW | O_NONBLOCK,
1802 S_IRUSR | S_IWUSR);
1803
1804 if (error)
1805 goto out;
1806
1807 /*
1808 * Don't dump to non-regular files, files with links, or files
1809 * owned by someone else.
1810 */
1811 vp = nd.ni_vp;
1812 if ((error = VOP_GETATTR(vp, &vattr, cred, p)) != 0) {
1813 VOP_UNLOCK(vp);
1814 vn_close(vp, FWRITE, cred, p);
1815 goto out;
1816 }
1817 if (vp->v_type != VREG || vattr.va_nlink != 1 ||
1818 vattr.va_mode & ((VREAD | VWRITE) >> 3 | (VREAD | VWRITE) >> 6) ||
1819 vattr.va_uid != cred->cr_uid) {
1820 error = EACCES;
1821 VOP_UNLOCK(vp);
1822 vn_close(vp, FWRITE, cred, p);
1823 goto out;
1824 }
1825 vattr_null(&vattr);
1826 vattr.va_size = 0;
1827 VOP_SETATTR(vp, &vattr, cred, p);
1828 pr->ps_acflag |= ACORE;
1829
1830 io.io_proc = p;
1831 io.io_vp = vp;
1832 io.io_cred = cred;
1833 io.io_offset = 0;
1834 VOP_UNLOCK(vp);
1835 vref(vp);
1836 error = vn_close(vp, FWRITE, cred, p);
1837 if (error == 0)
1838 error = coredump_elf(p, &io);
1839 vrele(vp);
1840 out:
1841 crfree(cred);
1842 pool_put(&namei_pool, name);
1843 return (error);
1844 #endif
1845 }
1846
1847 #ifndef SMALL_KERNEL
1848 int
coredump_write(void * cookie,enum uio_seg segflg,const void * data,size_t len,int isvnode)1849 coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len,
1850 int isvnode)
1851 {
1852 struct coredump_iostate *io = cookie;
1853 off_t coffset = 0;
1854 size_t csize;
1855 int chunk, error;
1856
1857 csize = len;
1858 do {
1859 if (sigmask(SIGKILL) &
1860 (io->io_proc->p_siglist | io->io_proc->p_p->ps_siglist))
1861 return (EINTR);
1862
1863 /* Rest of the loop sleeps with lock held, so... */
1864 yield();
1865
1866 chunk = MIN(csize, MAXPHYS);
1867 error = vn_rdwr(UIO_WRITE, io->io_vp,
1868 (caddr_t)data + coffset, chunk,
1869 io->io_offset + coffset, segflg,
1870 IO_UNIT, io->io_cred, NULL, io->io_proc);
1871 if (error && (error != EFAULT || !isvnode)) {
1872 struct process *pr = io->io_proc->p_p;
1873
1874 if (error == ENOSPC)
1875 log(LOG_ERR,
1876 "coredump of %s(%d) failed, filesystem full\n",
1877 pr->ps_comm, pr->ps_pid);
1878 else
1879 log(LOG_ERR,
1880 "coredump of %s(%d), write failed: errno %d\n",
1881 pr->ps_comm, pr->ps_pid, error);
1882 return (error);
1883 }
1884
1885 coffset += chunk;
1886 csize -= chunk;
1887 } while (csize > 0);
1888
1889 io->io_offset += len;
1890 return (0);
1891 }
1892
1893 void
coredump_unmap(void * cookie,vaddr_t start,vaddr_t end)1894 coredump_unmap(void *cookie, vaddr_t start, vaddr_t end)
1895 {
1896 struct coredump_iostate *io = cookie;
1897
1898 uvm_unmap(&io->io_proc->p_vmspace->vm_map, start, end);
1899 }
1900
1901 #endif /* !SMALL_KERNEL */
1902
1903 /*
1904 * Nonexistent system call-- signal process (may want to handle it).
1905 * Flag error in case process won't see signal immediately (blocked or ignored).
1906 */
1907 int
sys_nosys(struct proc * p,void * v,register_t * retval)1908 sys_nosys(struct proc *p, void *v, register_t *retval)
1909 {
1910 ptsignal(p, SIGSYS, STHREAD);
1911 return (ENOSYS);
1912 }
1913
1914 int
sys___thrsigdivert(struct proc * p,void * v,register_t * retval)1915 sys___thrsigdivert(struct proc *p, void *v, register_t *retval)
1916 {
1917 struct sys___thrsigdivert_args /* {
1918 syscallarg(sigset_t) sigmask;
1919 syscallarg(siginfo_t *) info;
1920 syscallarg(const struct timespec *) timeout;
1921 } */ *uap = v;
1922 struct sigctx ctx;
1923 sigset_t mask = SCARG(uap, sigmask) &~ sigcantmask;
1924 siginfo_t si;
1925 uint64_t nsecs = INFSLP;
1926 int timeinvalid = 0;
1927 int error = 0;
1928
1929 memset(&si, 0, sizeof(si));
1930
1931 if (SCARG(uap, timeout) != NULL) {
1932 struct timespec ts;
1933 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0)
1934 return (error);
1935 #ifdef KTRACE
1936 if (KTRPOINT(p, KTR_STRUCT))
1937 ktrreltimespec(p, &ts);
1938 #endif
1939 if (!timespecisvalid(&ts))
1940 timeinvalid = 1;
1941 else
1942 nsecs = TIMESPEC_TO_NSEC(&ts);
1943 }
1944
1945 dosigsuspend(p, p->p_sigmask &~ mask);
1946 for (;;) {
1947 si.si_signo = cursig(p, &ctx, 0);
1948 if (si.si_signo != 0) {
1949 sigset_t smask = sigmask(si.si_signo);
1950 if (smask & mask) {
1951 atomic_clearbits_int(&p->p_siglist, smask);
1952 error = 0;
1953 break;
1954 }
1955 }
1956
1957 /* per-POSIX, delay this error until after the above */
1958 if (timeinvalid)
1959 error = EINVAL;
1960 /* per-POSIX, return immediately if timeout is zero-valued */
1961 if (nsecs == 0)
1962 error = EAGAIN;
1963
1964 if (error != 0)
1965 break;
1966
1967 error = tsleep_nsec(&nowake, PPAUSE|PCATCH, "sigwait", nsecs);
1968 }
1969
1970 if (error == 0) {
1971 *retval = si.si_signo;
1972 if (SCARG(uap, info) != NULL) {
1973 error = copyout(&si, SCARG(uap, info), sizeof(si));
1974 #ifdef KTRACE
1975 if (error == 0 && KTRPOINT(p, KTR_STRUCT))
1976 ktrsiginfo(p, &si);
1977 #endif
1978 }
1979 } else if (error == ERESTART && SCARG(uap, timeout) != NULL) {
1980 /*
1981 * Restarting is wrong if there's a timeout, as it'll be
1982 * for the same interval again
1983 */
1984 error = EINTR;
1985 }
1986
1987 return (error);
1988 }
1989
1990 void
initsiginfo(siginfo_t * si,int sig,u_long trapno,int code,union sigval val)1991 initsiginfo(siginfo_t *si, int sig, u_long trapno, int code, union sigval val)
1992 {
1993 memset(si, 0, sizeof(*si));
1994
1995 si->si_signo = sig;
1996 si->si_code = code;
1997 if (code == SI_USER) {
1998 si->si_value = val;
1999 } else {
2000 switch (sig) {
2001 case SIGSEGV:
2002 case SIGILL:
2003 case SIGBUS:
2004 case SIGFPE:
2005 si->si_addr = val.sival_ptr;
2006 si->si_trapno = trapno;
2007 break;
2008 case SIGXFSZ:
2009 break;
2010 }
2011 }
2012 }
2013
2014 void
userret(struct proc * p)2015 userret(struct proc *p)
2016 {
2017 struct sigctx ctx;
2018 int signum;
2019
2020 if (p->p_flag & P_SUSPSINGLE)
2021 single_thread_check(p, 0);
2022
2023 /* send SIGPROF or SIGVTALRM if their timers interrupted this thread */
2024 if (p->p_flag & P_PROFPEND) {
2025 atomic_clearbits_int(&p->p_flag, P_PROFPEND);
2026 psignal(p, SIGPROF);
2027 }
2028 if (p->p_flag & P_ALRMPEND) {
2029 atomic_clearbits_int(&p->p_flag, P_ALRMPEND);
2030 psignal(p, SIGVTALRM);
2031 }
2032
2033 if (SIGPENDING(p) != 0) {
2034 while ((signum = cursig(p, &ctx, 0)) != 0)
2035 postsig(p, signum, &ctx);
2036 }
2037
2038 /*
2039 * If P_SIGSUSPEND is still set here, then we still need to restore
2040 * the original sigmask before returning to userspace. Also, this
2041 * might unmask some pending signals, so we need to check a second
2042 * time for signals to post.
2043 */
2044 if (p->p_flag & P_SIGSUSPEND) {
2045 p->p_sigmask = p->p_oldmask;
2046 atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND);
2047
2048 while ((signum = cursig(p, &ctx, 0)) != 0)
2049 postsig(p, signum, &ctx);
2050 }
2051
2052 WITNESS_WARN(WARN_PANIC, NULL, "userret: returning");
2053
2054 p->p_cpu->ci_schedstate.spc_curpriority = p->p_usrpri;
2055 }
2056
2057 int
single_thread_check_locked(struct proc * p,int deep)2058 single_thread_check_locked(struct proc *p, int deep)
2059 {
2060 struct process *pr = p->p_p;
2061
2062 MUTEX_ASSERT_LOCKED(&pr->ps_mtx);
2063
2064 if (pr->ps_single == NULL || pr->ps_single == p)
2065 return (0);
2066
2067 do {
2068 /* if we're in deep, we need to unwind to the edge */
2069 if (deep) {
2070 if (pr->ps_flags & PS_SINGLEUNWIND)
2071 return (ERESTART);
2072 if (pr->ps_flags & PS_SINGLEEXIT)
2073 return (EINTR);
2074 }
2075
2076 if (pr->ps_flags & PS_SINGLEEXIT) {
2077 mtx_leave(&pr->ps_mtx);
2078 KERNEL_LOCK();
2079 exit1(p, 0, 0, EXIT_THREAD_NOCHECK);
2080 /* NOTREACHED */
2081 }
2082
2083 if (--pr->ps_singlecnt == 0)
2084 wakeup(&pr->ps_singlecnt);
2085
2086 /* not exiting and don't need to unwind, so suspend */
2087 mtx_leave(&pr->ps_mtx);
2088
2089 SCHED_LOCK();
2090 p->p_stat = SSTOP;
2091 mi_switch();
2092 SCHED_UNLOCK();
2093 mtx_enter(&pr->ps_mtx);
2094 } while (pr->ps_single != NULL);
2095
2096 return (0);
2097 }
2098
2099 int
single_thread_check(struct proc * p,int deep)2100 single_thread_check(struct proc *p, int deep)
2101 {
2102 int error;
2103
2104 mtx_enter(&p->p_p->ps_mtx);
2105 error = single_thread_check_locked(p, deep);
2106 mtx_leave(&p->p_p->ps_mtx);
2107
2108 return error;
2109 }
2110
2111 /*
2112 * Stop other threads in the process. The mode controls how and
2113 * where the other threads should stop:
2114 * - SINGLE_SUSPEND: stop wherever they are, will later be released (via
2115 * single_thread_clear())
2116 * - SINGLE_UNWIND: just unwind to kernel boundary, will be told to exit
2117 * (by setting to SINGLE_EXIT) or released as with SINGLE_SUSPEND
2118 * - SINGLE_EXIT: unwind to kernel boundary and exit
2119 */
2120 int
single_thread_set(struct proc * p,int flags)2121 single_thread_set(struct proc *p, int flags)
2122 {
2123 struct process *pr = p->p_p;
2124 struct proc *q;
2125 int error, mode = flags & SINGLE_MASK;
2126
2127 KASSERT(curproc == p);
2128
2129 mtx_enter(&pr->ps_mtx);
2130 error = single_thread_check_locked(p, flags & SINGLE_DEEP);
2131 if (error) {
2132 mtx_leave(&pr->ps_mtx);
2133 return error;
2134 }
2135
2136 switch (mode) {
2137 case SINGLE_SUSPEND:
2138 break;
2139 case SINGLE_UNWIND:
2140 atomic_setbits_int(&pr->ps_flags, PS_SINGLEUNWIND);
2141 break;
2142 case SINGLE_EXIT:
2143 atomic_setbits_int(&pr->ps_flags, PS_SINGLEEXIT);
2144 atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND);
2145 break;
2146 #ifdef DIAGNOSTIC
2147 default:
2148 panic("single_thread_mode = %d", mode);
2149 #endif
2150 }
2151 KASSERT((p->p_flag & P_SUSPSINGLE) == 0);
2152 pr->ps_single = p;
2153 pr->ps_singlecnt = pr->ps_threadcnt;
2154
2155 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
2156 if (q == p)
2157 continue;
2158 SCHED_LOCK();
2159 atomic_setbits_int(&q->p_flag, P_SUSPSINGLE);
2160 switch (q->p_stat) {
2161 case SSTOP:
2162 if (mode == SINGLE_EXIT) {
2163 unsleep(q);
2164 setrunnable(q);
2165 } else
2166 --pr->ps_singlecnt;
2167 break;
2168 case SSLEEP:
2169 /* if it's not interruptible, then just have to wait */
2170 if (q->p_flag & P_SINTR) {
2171 /* merely need to suspend? just stop it */
2172 if (mode == SINGLE_SUSPEND) {
2173 q->p_stat = SSTOP;
2174 --pr->ps_singlecnt;
2175 break;
2176 }
2177 /* need to unwind or exit, so wake it */
2178 unsleep(q);
2179 setrunnable(q);
2180 }
2181 break;
2182 case SONPROC:
2183 signotify(q);
2184 break;
2185 case SRUN:
2186 case SIDL:
2187 case SDEAD:
2188 break;
2189 }
2190 SCHED_UNLOCK();
2191 }
2192
2193 /* count ourself out */
2194 --pr->ps_singlecnt;
2195 mtx_leave(&pr->ps_mtx);
2196
2197 if ((flags & SINGLE_NOWAIT) == 0)
2198 single_thread_wait(pr, 1);
2199
2200 return 0;
2201 }
2202
2203 /*
2204 * Wait for other threads to stop. If recheck is false then the function
2205 * returns non-zero if the caller needs to restart the check else 0 is
2206 * returned. If recheck is true the return value is always 0.
2207 */
2208 int
single_thread_wait(struct process * pr,int recheck)2209 single_thread_wait(struct process *pr, int recheck)
2210 {
2211 int wait;
2212
2213 /* wait until they're all suspended */
2214 mtx_enter(&pr->ps_mtx);
2215 while ((wait = pr->ps_singlecnt > 0)) {
2216 msleep_nsec(&pr->ps_singlecnt, &pr->ps_mtx, PWAIT, "suspend",
2217 INFSLP);
2218 if (!recheck)
2219 break;
2220 }
2221 KASSERT((pr->ps_single->p_flag & P_SUSPSINGLE) == 0);
2222 mtx_leave(&pr->ps_mtx);
2223
2224 return wait;
2225 }
2226
2227 void
single_thread_clear(struct proc * p,int flag)2228 single_thread_clear(struct proc *p, int flag)
2229 {
2230 struct process *pr = p->p_p;
2231 struct proc *q;
2232
2233 KASSERT(pr->ps_single == p);
2234 KASSERT(curproc == p);
2235
2236 mtx_enter(&pr->ps_mtx);
2237 pr->ps_single = NULL;
2238 atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND | PS_SINGLEEXIT);
2239
2240 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
2241 if (q == p || (q->p_flag & P_SUSPSINGLE) == 0)
2242 continue;
2243 atomic_clearbits_int(&q->p_flag, P_SUSPSINGLE);
2244
2245 /*
2246 * if the thread was only stopped for single threading
2247 * then clearing that either makes it runnable or puts
2248 * it back into some sleep queue
2249 */
2250 SCHED_LOCK();
2251 if (q->p_stat == SSTOP && (q->p_flag & flag) == 0) {
2252 if (q->p_wchan == NULL)
2253 setrunnable(q);
2254 else {
2255 atomic_clearbits_int(&q->p_flag, P_WSLEEP);
2256 q->p_stat = SSLEEP;
2257 }
2258 }
2259 SCHED_UNLOCK();
2260 }
2261 mtx_leave(&pr->ps_mtx);
2262 }
2263
2264 void
sigio_del(struct sigiolst * rmlist)2265 sigio_del(struct sigiolst *rmlist)
2266 {
2267 struct sigio *sigio;
2268
2269 while ((sigio = LIST_FIRST(rmlist)) != NULL) {
2270 LIST_REMOVE(sigio, sio_pgsigio);
2271 crfree(sigio->sio_ucred);
2272 free(sigio, M_SIGIO, sizeof(*sigio));
2273 }
2274 }
2275
2276 void
sigio_unlink(struct sigio_ref * sir,struct sigiolst * rmlist)2277 sigio_unlink(struct sigio_ref *sir, struct sigiolst *rmlist)
2278 {
2279 struct sigio *sigio;
2280
2281 MUTEX_ASSERT_LOCKED(&sigio_lock);
2282
2283 sigio = sir->sir_sigio;
2284 if (sigio != NULL) {
2285 KASSERT(sigio->sio_myref == sir);
2286 sir->sir_sigio = NULL;
2287
2288 if (sigio->sio_pgid > 0)
2289 sigio->sio_proc = NULL;
2290 else
2291 sigio->sio_pgrp = NULL;
2292 LIST_REMOVE(sigio, sio_pgsigio);
2293
2294 LIST_INSERT_HEAD(rmlist, sigio, sio_pgsigio);
2295 }
2296 }
2297
2298 void
sigio_free(struct sigio_ref * sir)2299 sigio_free(struct sigio_ref *sir)
2300 {
2301 struct sigiolst rmlist;
2302
2303 if (sir->sir_sigio == NULL)
2304 return;
2305
2306 LIST_INIT(&rmlist);
2307
2308 mtx_enter(&sigio_lock);
2309 sigio_unlink(sir, &rmlist);
2310 mtx_leave(&sigio_lock);
2311
2312 sigio_del(&rmlist);
2313 }
2314
2315 void
sigio_freelist(struct sigiolst * sigiolst)2316 sigio_freelist(struct sigiolst *sigiolst)
2317 {
2318 struct sigiolst rmlist;
2319 struct sigio *sigio;
2320
2321 if (LIST_EMPTY(sigiolst))
2322 return;
2323
2324 LIST_INIT(&rmlist);
2325
2326 mtx_enter(&sigio_lock);
2327 while ((sigio = LIST_FIRST(sigiolst)) != NULL)
2328 sigio_unlink(sigio->sio_myref, &rmlist);
2329 mtx_leave(&sigio_lock);
2330
2331 sigio_del(&rmlist);
2332 }
2333
2334 int
sigio_setown(struct sigio_ref * sir,u_long cmd,caddr_t data)2335 sigio_setown(struct sigio_ref *sir, u_long cmd, caddr_t data)
2336 {
2337 struct sigiolst rmlist;
2338 struct proc *p = curproc;
2339 struct pgrp *pgrp = NULL;
2340 struct process *pr = NULL;
2341 struct sigio *sigio;
2342 int error;
2343 pid_t pgid = *(int *)data;
2344
2345 if (pgid == 0) {
2346 sigio_free(sir);
2347 return (0);
2348 }
2349
2350 if (cmd == TIOCSPGRP) {
2351 if (pgid < 0)
2352 return (EINVAL);
2353 pgid = -pgid;
2354 }
2355
2356 sigio = malloc(sizeof(*sigio), M_SIGIO, M_WAITOK);
2357 sigio->sio_pgid = pgid;
2358 sigio->sio_ucred = crhold(p->p_ucred);
2359 sigio->sio_myref = sir;
2360
2361 LIST_INIT(&rmlist);
2362
2363 /*
2364 * The kernel lock, and not sleeping between prfind()/pgfind() and
2365 * linking of the sigio ensure that the process or process group does
2366 * not disappear unexpectedly.
2367 */
2368 KERNEL_LOCK();
2369 mtx_enter(&sigio_lock);
2370
2371 if (pgid > 0) {
2372 pr = prfind(pgid);
2373 if (pr == NULL) {
2374 error = ESRCH;
2375 goto fail;
2376 }
2377
2378 /*
2379 * Policy - Don't allow a process to FSETOWN a process
2380 * in another session.
2381 *
2382 * Remove this test to allow maximum flexibility or
2383 * restrict FSETOWN to the current process or process
2384 * group for maximum safety.
2385 */
2386 if (pr->ps_session != p->p_p->ps_session) {
2387 error = EPERM;
2388 goto fail;
2389 }
2390
2391 if ((pr->ps_flags & PS_EXITING) != 0) {
2392 error = ESRCH;
2393 goto fail;
2394 }
2395 } else /* if (pgid < 0) */ {
2396 pgrp = pgfind(-pgid);
2397 if (pgrp == NULL) {
2398 error = ESRCH;
2399 goto fail;
2400 }
2401
2402 /*
2403 * Policy - Don't allow a process to FSETOWN a process
2404 * in another session.
2405 *
2406 * Remove this test to allow maximum flexibility or
2407 * restrict FSETOWN to the current process or process
2408 * group for maximum safety.
2409 */
2410 if (pgrp->pg_session != p->p_p->ps_session) {
2411 error = EPERM;
2412 goto fail;
2413 }
2414 }
2415
2416 if (pgid > 0) {
2417 sigio->sio_proc = pr;
2418 LIST_INSERT_HEAD(&pr->ps_sigiolst, sigio, sio_pgsigio);
2419 } else {
2420 sigio->sio_pgrp = pgrp;
2421 LIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
2422 }
2423
2424 sigio_unlink(sir, &rmlist);
2425 sir->sir_sigio = sigio;
2426
2427 mtx_leave(&sigio_lock);
2428 KERNEL_UNLOCK();
2429
2430 sigio_del(&rmlist);
2431
2432 return (0);
2433
2434 fail:
2435 mtx_leave(&sigio_lock);
2436 KERNEL_UNLOCK();
2437
2438 crfree(sigio->sio_ucred);
2439 free(sigio, M_SIGIO, sizeof(*sigio));
2440
2441 return (error);
2442 }
2443
2444 void
sigio_getown(struct sigio_ref * sir,u_long cmd,caddr_t data)2445 sigio_getown(struct sigio_ref *sir, u_long cmd, caddr_t data)
2446 {
2447 struct sigio *sigio;
2448 pid_t pgid = 0;
2449
2450 mtx_enter(&sigio_lock);
2451 sigio = sir->sir_sigio;
2452 if (sigio != NULL)
2453 pgid = sigio->sio_pgid;
2454 mtx_leave(&sigio_lock);
2455
2456 if (cmd == TIOCGPGRP)
2457 pgid = -pgid;
2458
2459 *(int *)data = pgid;
2460 }
2461
2462 void
sigio_copy(struct sigio_ref * dst,struct sigio_ref * src)2463 sigio_copy(struct sigio_ref *dst, struct sigio_ref *src)
2464 {
2465 struct sigiolst rmlist;
2466 struct sigio *newsigio, *sigio;
2467
2468 sigio_free(dst);
2469
2470 if (src->sir_sigio == NULL)
2471 return;
2472
2473 newsigio = malloc(sizeof(*newsigio), M_SIGIO, M_WAITOK);
2474 LIST_INIT(&rmlist);
2475
2476 mtx_enter(&sigio_lock);
2477
2478 sigio = src->sir_sigio;
2479 if (sigio == NULL) {
2480 mtx_leave(&sigio_lock);
2481 free(newsigio, M_SIGIO, sizeof(*newsigio));
2482 return;
2483 }
2484
2485 newsigio->sio_pgid = sigio->sio_pgid;
2486 newsigio->sio_ucred = crhold(sigio->sio_ucred);
2487 newsigio->sio_myref = dst;
2488 if (newsigio->sio_pgid > 0) {
2489 newsigio->sio_proc = sigio->sio_proc;
2490 LIST_INSERT_HEAD(&newsigio->sio_proc->ps_sigiolst, newsigio,
2491 sio_pgsigio);
2492 } else {
2493 newsigio->sio_pgrp = sigio->sio_pgrp;
2494 LIST_INSERT_HEAD(&newsigio->sio_pgrp->pg_sigiolst, newsigio,
2495 sio_pgsigio);
2496 }
2497
2498 sigio_unlink(dst, &rmlist);
2499 dst->sir_sigio = newsigio;
2500
2501 mtx_leave(&sigio_lock);
2502
2503 sigio_del(&rmlist);
2504 }
2505