]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_sig.c
epair: Simplify the transmit path and address lost wakeups
[FreeBSD/FreeBSD.git] / sys / kern / kern_sig.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)kern_sig.c  8.7 (Berkeley) 4/18/94
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include "opt_capsicum.h"
43 #include "opt_ktrace.h"
44
45 #include <sys/param.h>
46 #include <sys/capsicum.h>
47 #include <sys/ctype.h>
48 #include <sys/systm.h>
49 #include <sys/signalvar.h>
50 #include <sys/vnode.h>
51 #include <sys/acct.h>
52 #include <sys/capsicum.h>
53 #include <sys/compressor.h>
54 #include <sys/condvar.h>
55 #include <sys/devctl.h>
56 #include <sys/event.h>
57 #include <sys/fcntl.h>
58 #include <sys/imgact.h>
59 #include <sys/kernel.h>
60 #include <sys/ktr.h>
61 #include <sys/ktrace.h>
62 #include <sys/limits.h>
63 #include <sys/lock.h>
64 #include <sys/malloc.h>
65 #include <sys/mutex.h>
66 #include <sys/refcount.h>
67 #include <sys/namei.h>
68 #include <sys/proc.h>
69 #include <sys/procdesc.h>
70 #include <sys/ptrace.h>
71 #include <sys/posix4.h>
72 #include <sys/racct.h>
73 #include <sys/resourcevar.h>
74 #include <sys/sdt.h>
75 #include <sys/sbuf.h>
76 #include <sys/sleepqueue.h>
77 #include <sys/smp.h>
78 #include <sys/stat.h>
79 #include <sys/sx.h>
80 #include <sys/syscall.h>
81 #include <sys/syscallsubr.h>
82 #include <sys/sysctl.h>
83 #include <sys/sysent.h>
84 #include <sys/syslog.h>
85 #include <sys/sysproto.h>
86 #include <sys/timers.h>
87 #include <sys/unistd.h>
88 #include <sys/vmmeter.h>
89 #include <sys/wait.h>
90 #include <vm/vm.h>
91 #include <vm/vm_extern.h>
92 #include <vm/uma.h>
93
94 #include <sys/jail.h>
95
96 #include <machine/cpu.h>
97
98 #include <security/audit/audit.h>
99
100 #define ONSIG   32              /* NSIG for osig* syscalls.  XXX. */
101
102 SDT_PROVIDER_DECLARE(proc);
103 SDT_PROBE_DEFINE3(proc, , , signal__send,
104     "struct thread *", "struct proc *", "int");
105 SDT_PROBE_DEFINE2(proc, , , signal__clear,
106     "int", "ksiginfo_t *");
107 SDT_PROBE_DEFINE3(proc, , , signal__discard,
108     "struct thread *", "struct proc *", "int");
109
110 static int      coredump(struct thread *);
111 static int      killpg1(struct thread *td, int sig, int pgid, int all,
112                     ksiginfo_t *ksi);
113 static int      issignal(struct thread *td);
114 static void     reschedule_signals(struct proc *p, sigset_t block, int flags);
115 static int      sigprop(int sig);
116 static void     tdsigwakeup(struct thread *, int, sig_t, int);
117 static int      sig_suspend_threads(struct thread *, struct proc *, int);
118 static int      filt_sigattach(struct knote *kn);
119 static void     filt_sigdetach(struct knote *kn);
120 static int      filt_signal(struct knote *kn, long hint);
121 static struct thread *sigtd(struct proc *p, int sig, bool fast_sigblock);
122 static void     sigqueue_start(void);
123
124 static uma_zone_t       ksiginfo_zone = NULL;
125 struct filterops sig_filtops = {
126         .f_isfd = 0,
127         .f_attach = filt_sigattach,
128         .f_detach = filt_sigdetach,
129         .f_event = filt_signal,
130 };
131
132 static int      kern_logsigexit = 1;
133 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
134     &kern_logsigexit, 0,
135     "Log processes quitting on abnormal signals to syslog(3)");
136
137 static int      kern_forcesigexit = 1;
138 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
139     &kern_forcesigexit, 0, "Force trap signal to be handled");
140
141 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
142     "POSIX real time signal");
143
144 static int      max_pending_per_proc = 128;
145 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
146     &max_pending_per_proc, 0, "Max pending signals per proc");
147
148 static int      preallocate_siginfo = 1024;
149 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RDTUN,
150     &preallocate_siginfo, 0, "Preallocated signal memory size");
151
152 static int      signal_overflow = 0;
153 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
154     &signal_overflow, 0, "Number of signals overflew");
155
156 static int      signal_alloc_fail = 0;
157 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
158     &signal_alloc_fail, 0, "signals failed to be allocated");
159
160 static int      kern_lognosys = 0;
161 SYSCTL_INT(_kern, OID_AUTO, lognosys, CTLFLAG_RWTUN, &kern_lognosys, 0,
162     "Log invalid syscalls");
163
164 __read_frequently bool sigfastblock_fetch_always = false;
165 SYSCTL_BOOL(_kern, OID_AUTO, sigfastblock_fetch_always, CTLFLAG_RWTUN,
166     &sigfastblock_fetch_always, 0,
167     "Fetch sigfastblock word on each syscall entry for proper "
168     "blocking semantic");
169
170 static bool     kern_sig_discard_ign = true;
171 SYSCTL_BOOL(_kern, OID_AUTO, sig_discard_ign, CTLFLAG_RWTUN,
172     &kern_sig_discard_ign, 0,
173     "Discard ignored signals on delivery, otherwise queue them to "
174     "the target queue");
175
176 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
177
178 /*
179  * Policy -- Can ucred cr1 send SIGIO to process cr2?
180  * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
181  * in the right situations.
182  */
183 #define CANSIGIO(cr1, cr2) \
184         ((cr1)->cr_uid == 0 || \
185             (cr1)->cr_ruid == (cr2)->cr_ruid || \
186             (cr1)->cr_uid == (cr2)->cr_ruid || \
187             (cr1)->cr_ruid == (cr2)->cr_uid || \
188             (cr1)->cr_uid == (cr2)->cr_uid)
189
190 static int      sugid_coredump;
191 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RWTUN,
192     &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
193
194 static int      capmode_coredump;
195 SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RWTUN,
196     &capmode_coredump, 0, "Allow processes in capability mode to dump core");
197
198 static int      do_coredump = 1;
199 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
200         &do_coredump, 0, "Enable/Disable coredumps");
201
202 static int      set_core_nodump_flag = 0;
203 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
204         0, "Enable setting the NODUMP flag on coredump files");
205
206 static int      coredump_devctl = 0;
207 SYSCTL_INT(_kern, OID_AUTO, coredump_devctl, CTLFLAG_RW, &coredump_devctl,
208         0, "Generate a devctl notification when processes coredump");
209
210 /*
211  * Signal properties and actions.
212  * The array below categorizes the signals and their default actions
213  * according to the following properties:
214  */
215 #define SIGPROP_KILL            0x01    /* terminates process by default */
216 #define SIGPROP_CORE            0x02    /* ditto and coredumps */
217 #define SIGPROP_STOP            0x04    /* suspend process */
218 #define SIGPROP_TTYSTOP         0x08    /* ditto, from tty */
219 #define SIGPROP_IGNORE          0x10    /* ignore by default */
220 #define SIGPROP_CONT            0x20    /* continue if suspended */
221
222 static int sigproptbl[NSIG] = {
223         [SIGHUP] =      SIGPROP_KILL,
224         [SIGINT] =      SIGPROP_KILL,
225         [SIGQUIT] =     SIGPROP_KILL | SIGPROP_CORE,
226         [SIGILL] =      SIGPROP_KILL | SIGPROP_CORE,
227         [SIGTRAP] =     SIGPROP_KILL | SIGPROP_CORE,
228         [SIGABRT] =     SIGPROP_KILL | SIGPROP_CORE,
229         [SIGEMT] =      SIGPROP_KILL | SIGPROP_CORE,
230         [SIGFPE] =      SIGPROP_KILL | SIGPROP_CORE,
231         [SIGKILL] =     SIGPROP_KILL,
232         [SIGBUS] =      SIGPROP_KILL | SIGPROP_CORE,
233         [SIGSEGV] =     SIGPROP_KILL | SIGPROP_CORE,
234         [SIGSYS] =      SIGPROP_KILL | SIGPROP_CORE,
235         [SIGPIPE] =     SIGPROP_KILL,
236         [SIGALRM] =     SIGPROP_KILL,
237         [SIGTERM] =     SIGPROP_KILL,
238         [SIGURG] =      SIGPROP_IGNORE,
239         [SIGSTOP] =     SIGPROP_STOP,
240         [SIGTSTP] =     SIGPROP_STOP | SIGPROP_TTYSTOP,
241         [SIGCONT] =     SIGPROP_IGNORE | SIGPROP_CONT,
242         [SIGCHLD] =     SIGPROP_IGNORE,
243         [SIGTTIN] =     SIGPROP_STOP | SIGPROP_TTYSTOP,
244         [SIGTTOU] =     SIGPROP_STOP | SIGPROP_TTYSTOP,
245         [SIGIO] =       SIGPROP_IGNORE,
246         [SIGXCPU] =     SIGPROP_KILL,
247         [SIGXFSZ] =     SIGPROP_KILL,
248         [SIGVTALRM] =   SIGPROP_KILL,
249         [SIGPROF] =     SIGPROP_KILL,
250         [SIGWINCH] =    SIGPROP_IGNORE,
251         [SIGINFO] =     SIGPROP_IGNORE,
252         [SIGUSR1] =     SIGPROP_KILL,
253         [SIGUSR2] =     SIGPROP_KILL,
254 };
255
256 #define _SIG_FOREACH_ADVANCE(i, set) ({                                 \
257         int __found;                                                    \
258         for (;;) {                                                      \
259                 if (__bits != 0) {                                      \
260                         int __sig = ffs(__bits);                        \
261                         __bits &= ~(1u << (__sig - 1));                 \
262                         sig = __i * sizeof((set)->__bits[0]) * NBBY + __sig; \
263                         __found = 1;                                    \
264                         break;                                          \
265                 }                                                       \
266                 if (++__i == _SIG_WORDS) {                              \
267                         __found = 0;                                    \
268                         break;                                          \
269                 }                                                       \
270                 __bits = (set)->__bits[__i];                            \
271         }                                                               \
272         __found != 0;                                                   \
273 })
274
275 #define SIG_FOREACH(i, set)                                             \
276         for (int32_t __i = -1, __bits = 0;                              \
277             _SIG_FOREACH_ADVANCE(i, set); )                             \
278
279 sigset_t fastblock_mask;
280
281 static void
282 sigqueue_start(void)
283 {
284         ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
285                 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
286         uma_prealloc(ksiginfo_zone, preallocate_siginfo);
287         p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
288         p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
289         p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
290         SIGFILLSET(fastblock_mask);
291         SIG_CANTMASK(fastblock_mask);
292 }
293
294 ksiginfo_t *
295 ksiginfo_alloc(int mwait)
296 {
297         MPASS(mwait == M_WAITOK || mwait == M_NOWAIT);
298
299         if (ksiginfo_zone == NULL)
300                 return (NULL);
301         return (uma_zalloc(ksiginfo_zone, mwait | M_ZERO));
302 }
303
304 void
305 ksiginfo_free(ksiginfo_t *ksi)
306 {
307         uma_zfree(ksiginfo_zone, ksi);
308 }
309
310 static __inline bool
311 ksiginfo_tryfree(ksiginfo_t *ksi)
312 {
313         if ((ksi->ksi_flags & KSI_EXT) == 0) {
314                 uma_zfree(ksiginfo_zone, ksi);
315                 return (true);
316         }
317         return (false);
318 }
319
320 void
321 sigqueue_init(sigqueue_t *list, struct proc *p)
322 {
323         SIGEMPTYSET(list->sq_signals);
324         SIGEMPTYSET(list->sq_kill);
325         SIGEMPTYSET(list->sq_ptrace);
326         TAILQ_INIT(&list->sq_list);
327         list->sq_proc = p;
328         list->sq_flags = SQ_INIT;
329 }
330
331 /*
332  * Get a signal's ksiginfo.
333  * Return:
334  *      0       -       signal not found
335  *      others  -       signal number
336  */
337 static int
338 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
339 {
340         struct proc *p = sq->sq_proc;
341         struct ksiginfo *ksi, *next;
342         int count = 0;
343
344         KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
345
346         if (!SIGISMEMBER(sq->sq_signals, signo))
347                 return (0);
348
349         if (SIGISMEMBER(sq->sq_ptrace, signo)) {
350                 count++;
351                 SIGDELSET(sq->sq_ptrace, signo);
352                 si->ksi_flags |= KSI_PTRACE;
353         }
354         if (SIGISMEMBER(sq->sq_kill, signo)) {
355                 count++;
356                 if (count == 1)
357                         SIGDELSET(sq->sq_kill, signo);
358         }
359
360         TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
361                 if (ksi->ksi_signo == signo) {
362                         if (count == 0) {
363                                 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
364                                 ksi->ksi_sigq = NULL;
365                                 ksiginfo_copy(ksi, si);
366                                 if (ksiginfo_tryfree(ksi) && p != NULL)
367                                         p->p_pendingcnt--;
368                         }
369                         if (++count > 1)
370                                 break;
371                 }
372         }
373
374         if (count <= 1)
375                 SIGDELSET(sq->sq_signals, signo);
376         si->ksi_signo = signo;
377         return (signo);
378 }
379
380 void
381 sigqueue_take(ksiginfo_t *ksi)
382 {
383         struct ksiginfo *kp;
384         struct proc     *p;
385         sigqueue_t      *sq;
386
387         if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
388                 return;
389
390         p = sq->sq_proc;
391         TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
392         ksi->ksi_sigq = NULL;
393         if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
394                 p->p_pendingcnt--;
395
396         for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
397              kp = TAILQ_NEXT(kp, ksi_link)) {
398                 if (kp->ksi_signo == ksi->ksi_signo)
399                         break;
400         }
401         if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo) &&
402             !SIGISMEMBER(sq->sq_ptrace, ksi->ksi_signo))
403                 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
404 }
405
406 static int
407 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
408 {
409         struct proc *p = sq->sq_proc;
410         struct ksiginfo *ksi;
411         int ret = 0;
412
413         KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
414
415         /*
416          * SIGKILL/SIGSTOP cannot be caught or masked, so take the fast path
417          * for these signals.
418          */
419         if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
420                 SIGADDSET(sq->sq_kill, signo);
421                 goto out_set_bit;
422         }
423
424         /* directly insert the ksi, don't copy it */
425         if (si->ksi_flags & KSI_INS) {
426                 if (si->ksi_flags & KSI_HEAD)
427                         TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
428                 else
429                         TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
430                 si->ksi_sigq = sq;
431                 goto out_set_bit;
432         }
433
434         if (__predict_false(ksiginfo_zone == NULL)) {
435                 SIGADDSET(sq->sq_kill, signo);
436                 goto out_set_bit;
437         }
438
439         if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
440                 signal_overflow++;
441                 ret = EAGAIN;
442         } else if ((ksi = ksiginfo_alloc(M_NOWAIT)) == NULL) {
443                 signal_alloc_fail++;
444                 ret = EAGAIN;
445         } else {
446                 if (p != NULL)
447                         p->p_pendingcnt++;
448                 ksiginfo_copy(si, ksi);
449                 ksi->ksi_signo = signo;
450                 if (si->ksi_flags & KSI_HEAD)
451                         TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
452                 else
453                         TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
454                 ksi->ksi_sigq = sq;
455         }
456
457         if (ret != 0) {
458                 if ((si->ksi_flags & KSI_PTRACE) != 0) {
459                         SIGADDSET(sq->sq_ptrace, signo);
460                         ret = 0;
461                         goto out_set_bit;
462                 } else if ((si->ksi_flags & KSI_TRAP) != 0 ||
463                     (si->ksi_flags & KSI_SIGQ) == 0) {
464                         SIGADDSET(sq->sq_kill, signo);
465                         ret = 0;
466                         goto out_set_bit;
467                 }
468                 return (ret);
469         }
470
471 out_set_bit:
472         SIGADDSET(sq->sq_signals, signo);
473         return (ret);
474 }
475
476 void
477 sigqueue_flush(sigqueue_t *sq)
478 {
479         struct proc *p = sq->sq_proc;
480         ksiginfo_t *ksi;
481
482         KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
483
484         if (p != NULL)
485                 PROC_LOCK_ASSERT(p, MA_OWNED);
486
487         while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
488                 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
489                 ksi->ksi_sigq = NULL;
490                 if (ksiginfo_tryfree(ksi) && p != NULL)
491                         p->p_pendingcnt--;
492         }
493
494         SIGEMPTYSET(sq->sq_signals);
495         SIGEMPTYSET(sq->sq_kill);
496         SIGEMPTYSET(sq->sq_ptrace);
497 }
498
499 static void
500 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
501 {
502         sigset_t tmp;
503         struct proc *p1, *p2;
504         ksiginfo_t *ksi, *next;
505
506         KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
507         KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
508         p1 = src->sq_proc;
509         p2 = dst->sq_proc;
510         /* Move siginfo to target list */
511         TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
512                 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
513                         TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
514                         if (p1 != NULL)
515                                 p1->p_pendingcnt--;
516                         TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
517                         ksi->ksi_sigq = dst;
518                         if (p2 != NULL)
519                                 p2->p_pendingcnt++;
520                 }
521         }
522
523         /* Move pending bits to target list */
524         tmp = src->sq_kill;
525         SIGSETAND(tmp, *set);
526         SIGSETOR(dst->sq_kill, tmp);
527         SIGSETNAND(src->sq_kill, tmp);
528
529         tmp = src->sq_ptrace;
530         SIGSETAND(tmp, *set);
531         SIGSETOR(dst->sq_ptrace, tmp);
532         SIGSETNAND(src->sq_ptrace, tmp);
533
534         tmp = src->sq_signals;
535         SIGSETAND(tmp, *set);
536         SIGSETOR(dst->sq_signals, tmp);
537         SIGSETNAND(src->sq_signals, tmp);
538 }
539
540 #if 0
541 static void
542 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
543 {
544         sigset_t set;
545
546         SIGEMPTYSET(set);
547         SIGADDSET(set, signo);
548         sigqueue_move_set(src, dst, &set);
549 }
550 #endif
551
552 static void
553 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
554 {
555         struct proc *p = sq->sq_proc;
556         ksiginfo_t *ksi, *next;
557
558         KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
559
560         /* Remove siginfo queue */
561         TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
562                 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
563                         TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
564                         ksi->ksi_sigq = NULL;
565                         if (ksiginfo_tryfree(ksi) && p != NULL)
566                                 p->p_pendingcnt--;
567                 }
568         }
569         SIGSETNAND(sq->sq_kill, *set);
570         SIGSETNAND(sq->sq_ptrace, *set);
571         SIGSETNAND(sq->sq_signals, *set);
572 }
573
574 void
575 sigqueue_delete(sigqueue_t *sq, int signo)
576 {
577         sigset_t set;
578
579         SIGEMPTYSET(set);
580         SIGADDSET(set, signo);
581         sigqueue_delete_set(sq, &set);
582 }
583
584 /* Remove a set of signals for a process */
585 static void
586 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
587 {
588         sigqueue_t worklist;
589         struct thread *td0;
590
591         PROC_LOCK_ASSERT(p, MA_OWNED);
592
593         sigqueue_init(&worklist, NULL);
594         sigqueue_move_set(&p->p_sigqueue, &worklist, set);
595
596         FOREACH_THREAD_IN_PROC(p, td0)
597                 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
598
599         sigqueue_flush(&worklist);
600 }
601
602 void
603 sigqueue_delete_proc(struct proc *p, int signo)
604 {
605         sigset_t set;
606
607         SIGEMPTYSET(set);
608         SIGADDSET(set, signo);
609         sigqueue_delete_set_proc(p, &set);
610 }
611
612 static void
613 sigqueue_delete_stopmask_proc(struct proc *p)
614 {
615         sigset_t set;
616
617         SIGEMPTYSET(set);
618         SIGADDSET(set, SIGSTOP);
619         SIGADDSET(set, SIGTSTP);
620         SIGADDSET(set, SIGTTIN);
621         SIGADDSET(set, SIGTTOU);
622         sigqueue_delete_set_proc(p, &set);
623 }
624
625 /*
626  * Determine signal that should be delivered to thread td, the current
627  * thread, 0 if none.  If there is a pending stop signal with default
628  * action, the process stops in issignal().
629  */
630 int
631 cursig(struct thread *td)
632 {
633         PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
634         mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
635         THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
636         return (SIGPENDING(td) ? issignal(td) : 0);
637 }
638
639 /*
640  * Arrange for ast() to handle unmasked pending signals on return to user
641  * mode.  This must be called whenever a signal is added to td_sigqueue or
642  * unmasked in td_sigmask.
643  */
644 void
645 signotify(struct thread *td)
646 {
647
648         PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
649
650         if (SIGPENDING(td)) {
651                 thread_lock(td);
652                 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
653                 thread_unlock(td);
654         }
655 }
656
657 /*
658  * Returns 1 (true) if altstack is configured for the thread, and the
659  * passed stack bottom address falls into the altstack range.  Handles
660  * the 43 compat special case where the alt stack size is zero.
661  */
662 int
663 sigonstack(size_t sp)
664 {
665         struct thread *td;
666
667         td = curthread;
668         if ((td->td_pflags & TDP_ALTSTACK) == 0)
669                 return (0);
670 #if defined(COMPAT_43)
671         if (SV_PROC_FLAG(td->td_proc, SV_AOUT) && td->td_sigstk.ss_size == 0)
672                 return ((td->td_sigstk.ss_flags & SS_ONSTACK) != 0);
673 #endif
674         return (sp >= (size_t)td->td_sigstk.ss_sp &&
675             sp < td->td_sigstk.ss_size + (size_t)td->td_sigstk.ss_sp);
676 }
677
678 static __inline int
679 sigprop(int sig)
680 {
681
682         if (sig > 0 && sig < nitems(sigproptbl))
683                 return (sigproptbl[sig]);
684         return (0);
685 }
686
687 static bool
688 sigact_flag_test(const struct sigaction *act, int flag)
689 {
690
691         /*
692          * SA_SIGINFO is reset when signal disposition is set to
693          * ignore or default.  Other flags are kept according to user
694          * settings.
695          */
696         return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
697             ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
698             (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
699 }
700
701 /*
702  * kern_sigaction
703  * sigaction
704  * freebsd4_sigaction
705  * osigaction
706  */
707 int
708 kern_sigaction(struct thread *td, int sig, const struct sigaction *act,
709     struct sigaction *oact, int flags)
710 {
711         struct sigacts *ps;
712         struct proc *p = td->td_proc;
713
714         if (!_SIG_VALID(sig))
715                 return (EINVAL);
716         if (act != NULL && act->sa_handler != SIG_DFL &&
717             act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
718             SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
719             SA_NOCLDWAIT | SA_SIGINFO)) != 0)
720                 return (EINVAL);
721
722         PROC_LOCK(p);
723         ps = p->p_sigacts;
724         mtx_lock(&ps->ps_mtx);
725         if (oact) {
726                 memset(oact, 0, sizeof(*oact));
727                 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
728                 if (SIGISMEMBER(ps->ps_sigonstack, sig))
729                         oact->sa_flags |= SA_ONSTACK;
730                 if (!SIGISMEMBER(ps->ps_sigintr, sig))
731                         oact->sa_flags |= SA_RESTART;
732                 if (SIGISMEMBER(ps->ps_sigreset, sig))
733                         oact->sa_flags |= SA_RESETHAND;
734                 if (SIGISMEMBER(ps->ps_signodefer, sig))
735                         oact->sa_flags |= SA_NODEFER;
736                 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
737                         oact->sa_flags |= SA_SIGINFO;
738                         oact->sa_sigaction =
739                             (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
740                 } else
741                         oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
742                 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
743                         oact->sa_flags |= SA_NOCLDSTOP;
744                 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
745                         oact->sa_flags |= SA_NOCLDWAIT;
746         }
747         if (act) {
748                 if ((sig == SIGKILL || sig == SIGSTOP) &&
749                     act->sa_handler != SIG_DFL) {
750                         mtx_unlock(&ps->ps_mtx);
751                         PROC_UNLOCK(p);
752                         return (EINVAL);
753                 }
754
755                 /*
756                  * Change setting atomically.
757                  */
758
759                 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
760                 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
761                 if (sigact_flag_test(act, SA_SIGINFO)) {
762                         ps->ps_sigact[_SIG_IDX(sig)] =
763                             (__sighandler_t *)act->sa_sigaction;
764                         SIGADDSET(ps->ps_siginfo, sig);
765                 } else {
766                         ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
767                         SIGDELSET(ps->ps_siginfo, sig);
768                 }
769                 if (!sigact_flag_test(act, SA_RESTART))
770                         SIGADDSET(ps->ps_sigintr, sig);
771                 else
772                         SIGDELSET(ps->ps_sigintr, sig);
773                 if (sigact_flag_test(act, SA_ONSTACK))
774                         SIGADDSET(ps->ps_sigonstack, sig);
775                 else
776                         SIGDELSET(ps->ps_sigonstack, sig);
777                 if (sigact_flag_test(act, SA_RESETHAND))
778                         SIGADDSET(ps->ps_sigreset, sig);
779                 else
780                         SIGDELSET(ps->ps_sigreset, sig);
781                 if (sigact_flag_test(act, SA_NODEFER))
782                         SIGADDSET(ps->ps_signodefer, sig);
783                 else
784                         SIGDELSET(ps->ps_signodefer, sig);
785                 if (sig == SIGCHLD) {
786                         if (act->sa_flags & SA_NOCLDSTOP)
787                                 ps->ps_flag |= PS_NOCLDSTOP;
788                         else
789                                 ps->ps_flag &= ~PS_NOCLDSTOP;
790                         if (act->sa_flags & SA_NOCLDWAIT) {
791                                 /*
792                                  * Paranoia: since SA_NOCLDWAIT is implemented
793                                  * by reparenting the dying child to PID 1 (and
794                                  * trust it to reap the zombie), PID 1 itself
795                                  * is forbidden to set SA_NOCLDWAIT.
796                                  */
797                                 if (p->p_pid == 1)
798                                         ps->ps_flag &= ~PS_NOCLDWAIT;
799                                 else
800                                         ps->ps_flag |= PS_NOCLDWAIT;
801                         } else
802                                 ps->ps_flag &= ~PS_NOCLDWAIT;
803                         if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
804                                 ps->ps_flag |= PS_CLDSIGIGN;
805                         else
806                                 ps->ps_flag &= ~PS_CLDSIGIGN;
807                 }
808                 /*
809                  * Set bit in ps_sigignore for signals that are set to SIG_IGN,
810                  * and for signals set to SIG_DFL where the default is to
811                  * ignore. However, don't put SIGCONT in ps_sigignore, as we
812                  * have to restart the process.
813                  */
814                 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
815                     (sigprop(sig) & SIGPROP_IGNORE &&
816                      ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
817                         /* never to be seen again */
818                         sigqueue_delete_proc(p, sig);
819                         if (sig != SIGCONT)
820                                 /* easier in psignal */
821                                 SIGADDSET(ps->ps_sigignore, sig);
822                         SIGDELSET(ps->ps_sigcatch, sig);
823                 } else {
824                         SIGDELSET(ps->ps_sigignore, sig);
825                         if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
826                                 SIGDELSET(ps->ps_sigcatch, sig);
827                         else
828                                 SIGADDSET(ps->ps_sigcatch, sig);
829                 }
830 #ifdef COMPAT_FREEBSD4
831                 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
832                     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
833                     (flags & KSA_FREEBSD4) == 0)
834                         SIGDELSET(ps->ps_freebsd4, sig);
835                 else
836                         SIGADDSET(ps->ps_freebsd4, sig);
837 #endif
838 #ifdef COMPAT_43
839                 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
840                     ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
841                     (flags & KSA_OSIGSET) == 0)
842                         SIGDELSET(ps->ps_osigset, sig);
843                 else
844                         SIGADDSET(ps->ps_osigset, sig);
845 #endif
846         }
847         mtx_unlock(&ps->ps_mtx);
848         PROC_UNLOCK(p);
849         return (0);
850 }
851
852 #ifndef _SYS_SYSPROTO_H_
853 struct sigaction_args {
854         int     sig;
855         struct  sigaction *act;
856         struct  sigaction *oact;
857 };
858 #endif
859 int
860 sys_sigaction(struct thread *td, struct sigaction_args *uap)
861 {
862         struct sigaction act, oact;
863         struct sigaction *actp, *oactp;
864         int error;
865
866         actp = (uap->act != NULL) ? &act : NULL;
867         oactp = (uap->oact != NULL) ? &oact : NULL;
868         if (actp) {
869                 error = copyin(uap->act, actp, sizeof(act));
870                 if (error)
871                         return (error);
872         }
873         error = kern_sigaction(td, uap->sig, actp, oactp, 0);
874         if (oactp && !error)
875                 error = copyout(oactp, uap->oact, sizeof(oact));
876         return (error);
877 }
878
879 #ifdef COMPAT_FREEBSD4
880 #ifndef _SYS_SYSPROTO_H_
881 struct freebsd4_sigaction_args {
882         int     sig;
883         struct  sigaction *act;
884         struct  sigaction *oact;
885 };
886 #endif
887 int
888 freebsd4_sigaction(struct thread *td, struct freebsd4_sigaction_args *uap)
889 {
890         struct sigaction act, oact;
891         struct sigaction *actp, *oactp;
892         int error;
893
894         actp = (uap->act != NULL) ? &act : NULL;
895         oactp = (uap->oact != NULL) ? &oact : NULL;
896         if (actp) {
897                 error = copyin(uap->act, actp, sizeof(act));
898                 if (error)
899                         return (error);
900         }
901         error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
902         if (oactp && !error)
903                 error = copyout(oactp, uap->oact, sizeof(oact));
904         return (error);
905 }
906 #endif  /* COMAPT_FREEBSD4 */
907
908 #ifdef COMPAT_43        /* XXX - COMPAT_FBSD3 */
909 #ifndef _SYS_SYSPROTO_H_
910 struct osigaction_args {
911         int     signum;
912         struct  osigaction *nsa;
913         struct  osigaction *osa;
914 };
915 #endif
916 int
917 osigaction(struct thread *td, struct osigaction_args *uap)
918 {
919         struct osigaction sa;
920         struct sigaction nsa, osa;
921         struct sigaction *nsap, *osap;
922         int error;
923
924         if (uap->signum <= 0 || uap->signum >= ONSIG)
925                 return (EINVAL);
926
927         nsap = (uap->nsa != NULL) ? &nsa : NULL;
928         osap = (uap->osa != NULL) ? &osa : NULL;
929
930         if (nsap) {
931                 error = copyin(uap->nsa, &sa, sizeof(sa));
932                 if (error)
933                         return (error);
934                 nsap->sa_handler = sa.sa_handler;
935                 nsap->sa_flags = sa.sa_flags;
936                 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
937         }
938         error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
939         if (osap && !error) {
940                 sa.sa_handler = osap->sa_handler;
941                 sa.sa_flags = osap->sa_flags;
942                 SIG2OSIG(osap->sa_mask, sa.sa_mask);
943                 error = copyout(&sa, uap->osa, sizeof(sa));
944         }
945         return (error);
946 }
947
948 #if !defined(__i386__)
949 /* Avoid replicating the same stub everywhere */
950 int
951 osigreturn(struct thread *td, struct osigreturn_args *uap)
952 {
953
954         return (nosys(td, (struct nosys_args *)uap));
955 }
956 #endif
957 #endif /* COMPAT_43 */
958
959 /*
960  * Initialize signal state for process 0;
961  * set to ignore signals that are ignored by default.
962  */
963 void
964 siginit(struct proc *p)
965 {
966         int i;
967         struct sigacts *ps;
968
969         PROC_LOCK(p);
970         ps = p->p_sigacts;
971         mtx_lock(&ps->ps_mtx);
972         for (i = 1; i <= NSIG; i++) {
973                 if (sigprop(i) & SIGPROP_IGNORE && i != SIGCONT) {
974                         SIGADDSET(ps->ps_sigignore, i);
975                 }
976         }
977         mtx_unlock(&ps->ps_mtx);
978         PROC_UNLOCK(p);
979 }
980
981 /*
982  * Reset specified signal to the default disposition.
983  */
984 static void
985 sigdflt(struct sigacts *ps, int sig)
986 {
987
988         mtx_assert(&ps->ps_mtx, MA_OWNED);
989         SIGDELSET(ps->ps_sigcatch, sig);
990         if ((sigprop(sig) & SIGPROP_IGNORE) != 0 && sig != SIGCONT)
991                 SIGADDSET(ps->ps_sigignore, sig);
992         ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
993         SIGDELSET(ps->ps_siginfo, sig);
994 }
995
996 /*
997  * Reset signals for an exec of the specified process.
998  */
999 void
1000 execsigs(struct proc *p)
1001 {
1002         sigset_t osigignore;
1003         struct sigacts *ps;
1004         int sig;
1005         struct thread *td;
1006
1007         /*
1008          * Reset caught signals.  Held signals remain held
1009          * through td_sigmask (unless they were caught,
1010          * and are now ignored by default).
1011          */
1012         PROC_LOCK_ASSERT(p, MA_OWNED);
1013         ps = p->p_sigacts;
1014         mtx_lock(&ps->ps_mtx);
1015         sig_drop_caught(p);
1016
1017         /*
1018          * As CloudABI processes cannot modify signal handlers, fully
1019          * reset all signals to their default behavior. Do ignore
1020          * SIGPIPE, as it would otherwise be impossible to recover from
1021          * writes to broken pipes and sockets.
1022          */
1023         if (SV_PROC_ABI(p) == SV_ABI_CLOUDABI) {
1024                 osigignore = ps->ps_sigignore;
1025                 SIG_FOREACH(sig, &osigignore) {
1026                         if (sig != SIGPIPE)
1027                                 sigdflt(ps, sig);
1028                 }
1029                 SIGADDSET(ps->ps_sigignore, SIGPIPE);
1030         }
1031
1032         /*
1033          * Reset stack state to the user stack.
1034          * Clear set of signals caught on the signal stack.
1035          */
1036         td = curthread;
1037         MPASS(td->td_proc == p);
1038         td->td_sigstk.ss_flags = SS_DISABLE;
1039         td->td_sigstk.ss_size = 0;
1040         td->td_sigstk.ss_sp = 0;
1041         td->td_pflags &= ~TDP_ALTSTACK;
1042         /*
1043          * Reset no zombies if child dies flag as Solaris does.
1044          */
1045         ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
1046         if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
1047                 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
1048         mtx_unlock(&ps->ps_mtx);
1049 }
1050
1051 /*
1052  * kern_sigprocmask()
1053  *
1054  *      Manipulate signal mask.
1055  */
1056 int
1057 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
1058     int flags)
1059 {
1060         sigset_t new_block, oset1;
1061         struct proc *p;
1062         int error;
1063
1064         p = td->td_proc;
1065         if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
1066                 PROC_LOCK_ASSERT(p, MA_OWNED);
1067         else
1068                 PROC_LOCK(p);
1069         mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
1070             ? MA_OWNED : MA_NOTOWNED);
1071         if (oset != NULL)
1072                 *oset = td->td_sigmask;
1073
1074         error = 0;
1075         if (set != NULL) {
1076                 switch (how) {
1077                 case SIG_BLOCK:
1078                         SIG_CANTMASK(*set);
1079                         oset1 = td->td_sigmask;
1080                         SIGSETOR(td->td_sigmask, *set);
1081                         new_block = td->td_sigmask;
1082                         SIGSETNAND(new_block, oset1);
1083                         break;
1084                 case SIG_UNBLOCK:
1085                         SIGSETNAND(td->td_sigmask, *set);
1086                         signotify(td);
1087                         goto out;
1088                 case SIG_SETMASK:
1089                         SIG_CANTMASK(*set);
1090                         oset1 = td->td_sigmask;
1091                         if (flags & SIGPROCMASK_OLD)
1092                                 SIGSETLO(td->td_sigmask, *set);
1093                         else
1094                                 td->td_sigmask = *set;
1095                         new_block = td->td_sigmask;
1096                         SIGSETNAND(new_block, oset1);
1097                         signotify(td);
1098                         break;
1099                 default:
1100                         error = EINVAL;
1101                         goto out;
1102                 }
1103
1104                 /*
1105                  * The new_block set contains signals that were not previously
1106                  * blocked, but are blocked now.
1107                  *
1108                  * In case we block any signal that was not previously blocked
1109                  * for td, and process has the signal pending, try to schedule
1110                  * signal delivery to some thread that does not block the
1111                  * signal, possibly waking it up.
1112                  */
1113                 if (p->p_numthreads != 1)
1114                         reschedule_signals(p, new_block, flags);
1115         }
1116
1117 out:
1118         if (!(flags & SIGPROCMASK_PROC_LOCKED))
1119                 PROC_UNLOCK(p);
1120         return (error);
1121 }
1122
1123 #ifndef _SYS_SYSPROTO_H_
1124 struct sigprocmask_args {
1125         int     how;
1126         const sigset_t *set;
1127         sigset_t *oset;
1128 };
1129 #endif
1130 int
1131 sys_sigprocmask(struct thread *td, struct sigprocmask_args *uap)
1132 {
1133         sigset_t set, oset;
1134         sigset_t *setp, *osetp;
1135         int error;
1136
1137         setp = (uap->set != NULL) ? &set : NULL;
1138         osetp = (uap->oset != NULL) ? &oset : NULL;
1139         if (setp) {
1140                 error = copyin(uap->set, setp, sizeof(set));
1141                 if (error)
1142                         return (error);
1143         }
1144         error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1145         if (osetp && !error) {
1146                 error = copyout(osetp, uap->oset, sizeof(oset));
1147         }
1148         return (error);
1149 }
1150
1151 #ifdef COMPAT_43        /* XXX - COMPAT_FBSD3 */
1152 #ifndef _SYS_SYSPROTO_H_
1153 struct osigprocmask_args {
1154         int     how;
1155         osigset_t mask;
1156 };
1157 #endif
1158 int
1159 osigprocmask(struct thread *td, struct osigprocmask_args *uap)
1160 {
1161         sigset_t set, oset;
1162         int error;
1163
1164         OSIG2SIG(uap->mask, set);
1165         error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1166         SIG2OSIG(oset, td->td_retval[0]);
1167         return (error);
1168 }
1169 #endif /* COMPAT_43 */
1170
1171 int
1172 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1173 {
1174         ksiginfo_t ksi;
1175         sigset_t set;
1176         int error;
1177
1178         error = copyin(uap->set, &set, sizeof(set));
1179         if (error) {
1180                 td->td_retval[0] = error;
1181                 return (0);
1182         }
1183
1184         error = kern_sigtimedwait(td, set, &ksi, NULL);
1185         if (error) {
1186                 /*
1187                  * sigwait() function shall not return EINTR, but
1188                  * the syscall does.  Non-ancient libc provides the
1189                  * wrapper which hides EINTR.  Otherwise, EINTR return
1190                  * is used by libthr to handle required cancellation
1191                  * point in the sigwait().
1192                  */
1193                 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1194                         return (ERESTART);
1195                 td->td_retval[0] = error;
1196                 return (0);
1197         }
1198
1199         error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1200         td->td_retval[0] = error;
1201         return (0);
1202 }
1203
1204 int
1205 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1206 {
1207         struct timespec ts;
1208         struct timespec *timeout;
1209         sigset_t set;
1210         ksiginfo_t ksi;
1211         int error;
1212
1213         if (uap->timeout) {
1214                 error = copyin(uap->timeout, &ts, sizeof(ts));
1215                 if (error)
1216                         return (error);
1217
1218                 timeout = &ts;
1219         } else
1220                 timeout = NULL;
1221
1222         error = copyin(uap->set, &set, sizeof(set));
1223         if (error)
1224                 return (error);
1225
1226         error = kern_sigtimedwait(td, set, &ksi, timeout);
1227         if (error)
1228                 return (error);
1229
1230         if (uap->info)
1231                 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1232
1233         if (error == 0)
1234                 td->td_retval[0] = ksi.ksi_signo;
1235         return (error);
1236 }
1237
1238 int
1239 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1240 {
1241         ksiginfo_t ksi;
1242         sigset_t set;
1243         int error;
1244
1245         error = copyin(uap->set, &set, sizeof(set));
1246         if (error)
1247                 return (error);
1248
1249         error = kern_sigtimedwait(td, set, &ksi, NULL);
1250         if (error)
1251                 return (error);
1252
1253         if (uap->info)
1254                 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1255
1256         if (error == 0)
1257                 td->td_retval[0] = ksi.ksi_signo;
1258         return (error);
1259 }
1260
1261 static void
1262 proc_td_siginfo_capture(struct thread *td, siginfo_t *si)
1263 {
1264         struct thread *thr;
1265
1266         FOREACH_THREAD_IN_PROC(td->td_proc, thr) {
1267                 if (thr == td)
1268                         thr->td_si = *si;
1269                 else
1270                         thr->td_si.si_signo = 0;
1271         }
1272 }
1273
1274 int
1275 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1276         struct timespec *timeout)
1277 {
1278         struct sigacts *ps;
1279         sigset_t saved_mask, new_block;
1280         struct proc *p;
1281         int error, sig, timevalid = 0;
1282         sbintime_t sbt, precision, tsbt;
1283         struct timespec ts;
1284         bool traced;
1285
1286         p = td->td_proc;
1287         error = 0;
1288         traced = false;
1289
1290         /* Ensure the sigfastblock value is up to date. */
1291         sigfastblock_fetch(td);
1292
1293         if (timeout != NULL) {
1294                 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1295                         timevalid = 1;
1296                         ts = *timeout;
1297                         if (ts.tv_sec < INT32_MAX / 2) {
1298                                 tsbt = tstosbt(ts);
1299                                 precision = tsbt;
1300                                 precision >>= tc_precexp;
1301                                 if (TIMESEL(&sbt, tsbt))
1302                                         sbt += tc_tick_sbt;
1303                                 sbt += tsbt;
1304                         } else
1305                                 precision = sbt = 0;
1306                 }
1307         } else
1308                 precision = sbt = 0;
1309         ksiginfo_init(ksi);
1310         /* Some signals can not be waited for. */
1311         SIG_CANTMASK(waitset);
1312         ps = p->p_sigacts;
1313         PROC_LOCK(p);
1314         saved_mask = td->td_sigmask;
1315         SIGSETNAND(td->td_sigmask, waitset);
1316         if ((p->p_sysent->sv_flags & SV_SIG_DISCIGN) != 0 ||
1317             !kern_sig_discard_ign) {
1318                 thread_lock(td);
1319                 td->td_flags |= TDF_SIGWAIT;
1320                 thread_unlock(td);
1321         }
1322         for (;;) {
1323                 mtx_lock(&ps->ps_mtx);
1324                 sig = cursig(td);
1325                 mtx_unlock(&ps->ps_mtx);
1326                 KASSERT(sig >= 0, ("sig %d", sig));
1327                 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1328                         if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1329                             sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1330                                 error = 0;
1331                                 break;
1332                         }
1333                 }
1334
1335                 if (error != 0)
1336                         break;
1337
1338                 /*
1339                  * POSIX says this must be checked after looking for pending
1340                  * signals.
1341                  */
1342                 if (timeout != NULL && !timevalid) {
1343                         error = EINVAL;
1344                         break;
1345                 }
1346
1347                 if (traced) {
1348                         error = EINTR;
1349                         break;
1350                 }
1351
1352                 error = msleep_sbt(&p->p_sigacts, &p->p_mtx, PPAUSE | PCATCH,
1353                     "sigwait", sbt, precision, C_ABSOLUTE);
1354
1355                 /* The syscalls can not be restarted. */
1356                 if (error == ERESTART)
1357                         error = EINTR;
1358
1359                 /*
1360                  * If PTRACE_SCE or PTRACE_SCX were set after
1361                  * userspace entered the syscall, return spurious
1362                  * EINTR after wait was done.  Only do this as last
1363                  * resort after rechecking for possible queued signals
1364                  * and expired timeouts.
1365                  */
1366                 if (error == 0 && (p->p_ptevents & PTRACE_SYSCALL) != 0)
1367                         traced = true;
1368         }
1369         thread_lock(td);
1370         td->td_flags &= ~TDF_SIGWAIT;
1371         thread_unlock(td);
1372
1373         new_block = saved_mask;
1374         SIGSETNAND(new_block, td->td_sigmask);
1375         td->td_sigmask = saved_mask;
1376         /*
1377          * Fewer signals can be delivered to us, reschedule signal
1378          * notification.
1379          */
1380         if (p->p_numthreads != 1)
1381                 reschedule_signals(p, new_block, 0);
1382
1383         if (error == 0) {
1384                 SDT_PROBE2(proc, , , signal__clear, sig, ksi);
1385
1386                 if (ksi->ksi_code == SI_TIMER)
1387                         itimer_accept(p, ksi->ksi_timerid, ksi);
1388
1389 #ifdef KTRACE
1390                 if (KTRPOINT(td, KTR_PSIG)) {
1391                         sig_t action;
1392
1393                         mtx_lock(&ps->ps_mtx);
1394                         action = ps->ps_sigact[_SIG_IDX(sig)];
1395                         mtx_unlock(&ps->ps_mtx);
1396                         ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1397                 }
1398 #endif
1399                 if (sig == SIGKILL) {
1400                         proc_td_siginfo_capture(td, &ksi->ksi_info);
1401                         sigexit(td, sig);
1402                 }
1403         }
1404         PROC_UNLOCK(p);
1405         return (error);
1406 }
1407
1408 #ifndef _SYS_SYSPROTO_H_
1409 struct sigpending_args {
1410         sigset_t        *set;
1411 };
1412 #endif
1413 int
1414 sys_sigpending(struct thread *td, struct sigpending_args *uap)
1415 {
1416         struct proc *p = td->td_proc;
1417         sigset_t pending;
1418
1419         PROC_LOCK(p);
1420         pending = p->p_sigqueue.sq_signals;
1421         SIGSETOR(pending, td->td_sigqueue.sq_signals);
1422         PROC_UNLOCK(p);
1423         return (copyout(&pending, uap->set, sizeof(sigset_t)));
1424 }
1425
1426 #ifdef COMPAT_43        /* XXX - COMPAT_FBSD3 */
1427 #ifndef _SYS_SYSPROTO_H_
1428 struct osigpending_args {
1429         int     dummy;
1430 };
1431 #endif
1432 int
1433 osigpending(struct thread *td, struct osigpending_args *uap)
1434 {
1435         struct proc *p = td->td_proc;
1436         sigset_t pending;
1437
1438         PROC_LOCK(p);
1439         pending = p->p_sigqueue.sq_signals;
1440         SIGSETOR(pending, td->td_sigqueue.sq_signals);
1441         PROC_UNLOCK(p);
1442         SIG2OSIG(pending, td->td_retval[0]);
1443         return (0);
1444 }
1445 #endif /* COMPAT_43 */
1446
1447 #if defined(COMPAT_43)
1448 /*
1449  * Generalized interface signal handler, 4.3-compatible.
1450  */
1451 #ifndef _SYS_SYSPROTO_H_
1452 struct osigvec_args {
1453         int     signum;
1454         struct  sigvec *nsv;
1455         struct  sigvec *osv;
1456 };
1457 #endif
1458 /* ARGSUSED */
1459 int
1460 osigvec(struct thread *td, struct osigvec_args *uap)
1461 {
1462         struct sigvec vec;
1463         struct sigaction nsa, osa;
1464         struct sigaction *nsap, *osap;
1465         int error;
1466
1467         if (uap->signum <= 0 || uap->signum >= ONSIG)
1468                 return (EINVAL);
1469         nsap = (uap->nsv != NULL) ? &nsa : NULL;
1470         osap = (uap->osv != NULL) ? &osa : NULL;
1471         if (nsap) {
1472                 error = copyin(uap->nsv, &vec, sizeof(vec));
1473                 if (error)
1474                         return (error);
1475                 nsap->sa_handler = vec.sv_handler;
1476                 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1477                 nsap->sa_flags = vec.sv_flags;
1478                 nsap->sa_flags ^= SA_RESTART;   /* opposite of SV_INTERRUPT */
1479         }
1480         error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1481         if (osap && !error) {
1482                 vec.sv_handler = osap->sa_handler;
1483                 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1484                 vec.sv_flags = osap->sa_flags;
1485                 vec.sv_flags &= ~SA_NOCLDWAIT;
1486                 vec.sv_flags ^= SA_RESTART;
1487                 error = copyout(&vec, uap->osv, sizeof(vec));
1488         }
1489         return (error);
1490 }
1491
1492 #ifndef _SYS_SYSPROTO_H_
1493 struct osigblock_args {
1494         int     mask;
1495 };
1496 #endif
1497 int
1498 osigblock(struct thread *td, struct osigblock_args *uap)
1499 {
1500         sigset_t set, oset;
1501
1502         OSIG2SIG(uap->mask, set);
1503         kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1504         SIG2OSIG(oset, td->td_retval[0]);
1505         return (0);
1506 }
1507
1508 #ifndef _SYS_SYSPROTO_H_
1509 struct osigsetmask_args {
1510         int     mask;
1511 };
1512 #endif
1513 int
1514 osigsetmask(struct thread *td, struct osigsetmask_args *uap)
1515 {
1516         sigset_t set, oset;
1517
1518         OSIG2SIG(uap->mask, set);
1519         kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1520         SIG2OSIG(oset, td->td_retval[0]);
1521         return (0);
1522 }
1523 #endif /* COMPAT_43 */
1524
1525 /*
1526  * Suspend calling thread until signal, providing mask to be set in the
1527  * meantime.
1528  */
1529 #ifndef _SYS_SYSPROTO_H_
1530 struct sigsuspend_args {
1531         const sigset_t *sigmask;
1532 };
1533 #endif
1534 /* ARGSUSED */
1535 int
1536 sys_sigsuspend(struct thread *td, struct sigsuspend_args *uap)
1537 {
1538         sigset_t mask;
1539         int error;
1540
1541         error = copyin(uap->sigmask, &mask, sizeof(mask));
1542         if (error)
1543                 return (error);
1544         return (kern_sigsuspend(td, mask));
1545 }
1546
1547 int
1548 kern_sigsuspend(struct thread *td, sigset_t mask)
1549 {
1550         struct proc *p = td->td_proc;
1551         int has_sig, sig;
1552
1553         /* Ensure the sigfastblock value is up to date. */
1554         sigfastblock_fetch(td);
1555
1556         /*
1557          * When returning from sigsuspend, we want
1558          * the old mask to be restored after the
1559          * signal handler has finished.  Thus, we
1560          * save it here and mark the sigacts structure
1561          * to indicate this.
1562          */
1563         PROC_LOCK(p);
1564         kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1565             SIGPROCMASK_PROC_LOCKED);
1566         td->td_pflags |= TDP_OLDMASK;
1567
1568         /*
1569          * Process signals now. Otherwise, we can get spurious wakeup
1570          * due to signal entered process queue, but delivered to other
1571          * thread. But sigsuspend should return only on signal
1572          * delivery.
1573          */
1574         (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1575         for (has_sig = 0; !has_sig;) {
1576                 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1577                         0) == 0)
1578                         /* void */;
1579                 thread_suspend_check(0);
1580                 mtx_lock(&p->p_sigacts->ps_mtx);
1581                 while ((sig = cursig(td)) != 0) {
1582                         KASSERT(sig >= 0, ("sig %d", sig));
1583                         has_sig += postsig(sig);
1584                 }
1585                 mtx_unlock(&p->p_sigacts->ps_mtx);
1586
1587                 /*
1588                  * If PTRACE_SCE or PTRACE_SCX were set after
1589                  * userspace entered the syscall, return spurious
1590                  * EINTR.
1591                  */
1592                 if ((p->p_ptevents & PTRACE_SYSCALL) != 0)
1593                         has_sig += 1;
1594         }
1595         PROC_UNLOCK(p);
1596         td->td_errno = EINTR;
1597         td->td_pflags |= TDP_NERRNO;
1598         return (EJUSTRETURN);
1599 }
1600
1601 #ifdef COMPAT_43        /* XXX - COMPAT_FBSD3 */
1602 /*
1603  * Compatibility sigsuspend call for old binaries.  Note nonstandard calling
1604  * convention: libc stub passes mask, not pointer, to save a copyin.
1605  */
1606 #ifndef _SYS_SYSPROTO_H_
1607 struct osigsuspend_args {
1608         osigset_t mask;
1609 };
1610 #endif
1611 /* ARGSUSED */
1612 int
1613 osigsuspend(struct thread *td, struct osigsuspend_args *uap)
1614 {
1615         sigset_t mask;
1616
1617         OSIG2SIG(uap->mask, mask);
1618         return (kern_sigsuspend(td, mask));
1619 }
1620 #endif /* COMPAT_43 */
1621
1622 #if defined(COMPAT_43)
1623 #ifndef _SYS_SYSPROTO_H_
1624 struct osigstack_args {
1625         struct  sigstack *nss;
1626         struct  sigstack *oss;
1627 };
1628 #endif
1629 /* ARGSUSED */
1630 int
1631 osigstack(struct thread *td, struct osigstack_args *uap)
1632 {
1633         struct sigstack nss, oss;
1634         int error = 0;
1635
1636         if (uap->nss != NULL) {
1637                 error = copyin(uap->nss, &nss, sizeof(nss));
1638                 if (error)
1639                         return (error);
1640         }
1641         oss.ss_sp = td->td_sigstk.ss_sp;
1642         oss.ss_onstack = sigonstack(cpu_getstack(td));
1643         if (uap->nss != NULL) {
1644                 td->td_sigstk.ss_sp = nss.ss_sp;
1645                 td->td_sigstk.ss_size = 0;
1646                 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1647                 td->td_pflags |= TDP_ALTSTACK;
1648         }
1649         if (uap->oss != NULL)
1650                 error = copyout(&oss, uap->oss, sizeof(oss));
1651
1652         return (error);
1653 }
1654 #endif /* COMPAT_43 */
1655
1656 #ifndef _SYS_SYSPROTO_H_
1657 struct sigaltstack_args {
1658         stack_t *ss;
1659         stack_t *oss;
1660 };
1661 #endif
1662 /* ARGSUSED */
1663 int
1664 sys_sigaltstack(struct thread *td, struct sigaltstack_args *uap)
1665 {
1666         stack_t ss, oss;
1667         int error;
1668
1669         if (uap->ss != NULL) {
1670                 error = copyin(uap->ss, &ss, sizeof(ss));
1671                 if (error)
1672                         return (error);
1673         }
1674         error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1675             (uap->oss != NULL) ? &oss : NULL);
1676         if (error)
1677                 return (error);
1678         if (uap->oss != NULL)
1679                 error = copyout(&oss, uap->oss, sizeof(stack_t));
1680         return (error);
1681 }
1682
1683 int
1684 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1685 {
1686         struct proc *p = td->td_proc;
1687         int oonstack;
1688
1689         oonstack = sigonstack(cpu_getstack(td));
1690
1691         if (oss != NULL) {
1692                 *oss = td->td_sigstk;
1693                 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1694                     ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1695         }
1696
1697         if (ss != NULL) {
1698                 if (oonstack)
1699                         return (EPERM);
1700                 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1701                         return (EINVAL);
1702                 if (!(ss->ss_flags & SS_DISABLE)) {
1703                         if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1704                                 return (ENOMEM);
1705
1706                         td->td_sigstk = *ss;
1707                         td->td_pflags |= TDP_ALTSTACK;
1708                 } else {
1709                         td->td_pflags &= ~TDP_ALTSTACK;
1710                 }
1711         }
1712         return (0);
1713 }
1714
1715 struct killpg1_ctx {
1716         struct thread *td;
1717         ksiginfo_t *ksi;
1718         int sig;
1719         bool sent;
1720         bool found;
1721         int ret;
1722 };
1723
1724 static void
1725 killpg1_sendsig(struct proc *p, bool notself, struct killpg1_ctx *arg)
1726 {
1727         int err;
1728
1729         if (p->p_pid <= 1 || (p->p_flag & P_SYSTEM) != 0 ||
1730             (notself && p == arg->td->td_proc) || p->p_state == PRS_NEW)
1731                 return;
1732         PROC_LOCK(p);
1733         err = p_cansignal(arg->td, p, arg->sig);
1734         if (err == 0 && arg->sig != 0)
1735                 pksignal(p, arg->sig, arg->ksi);
1736         PROC_UNLOCK(p);
1737         if (err != ESRCH)
1738                 arg->found = true;
1739         if (err == 0)
1740                 arg->sent = true;
1741         else if (arg->ret == 0 && err != ESRCH && err != EPERM)
1742                 arg->ret = err;
1743 }
1744
1745 /*
1746  * Common code for kill process group/broadcast kill.
1747  * cp is calling process.
1748  */
1749 static int
1750 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1751 {
1752         struct proc *p;
1753         struct pgrp *pgrp;
1754         struct killpg1_ctx arg;
1755
1756         arg.td = td;
1757         arg.ksi = ksi;
1758         arg.sig = sig;
1759         arg.sent = false;
1760         arg.found = false;
1761         arg.ret = 0;
1762         if (all) {
1763                 /*
1764                  * broadcast
1765                  */
1766                 sx_slock(&allproc_lock);
1767                 FOREACH_PROC_IN_SYSTEM(p) {
1768                         killpg1_sendsig(p, true, &arg);
1769                 }
1770                 sx_sunlock(&allproc_lock);
1771         } else {
1772                 sx_slock(&proctree_lock);
1773                 if (pgid == 0) {
1774                         /*
1775                          * zero pgid means send to my process group.
1776                          */
1777                         pgrp = td->td_proc->p_pgrp;
1778                         PGRP_LOCK(pgrp);
1779                 } else {
1780                         pgrp = pgfind(pgid);
1781                         if (pgrp == NULL) {
1782                                 sx_sunlock(&proctree_lock);
1783                                 return (ESRCH);
1784                         }
1785                 }
1786                 sx_sunlock(&proctree_lock);
1787                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1788                         killpg1_sendsig(p, false, &arg);
1789                 }
1790                 PGRP_UNLOCK(pgrp);
1791         }
1792         MPASS(arg.ret != 0 || arg.found || !arg.sent);
1793         if (arg.ret == 0 && !arg.sent)
1794                 arg.ret = arg.found ? EPERM : ESRCH;
1795         return (arg.ret);
1796 }
1797
1798 #ifndef _SYS_SYSPROTO_H_
1799 struct kill_args {
1800         int     pid;
1801         int     signum;
1802 };
1803 #endif
1804 /* ARGSUSED */
1805 int
1806 sys_kill(struct thread *td, struct kill_args *uap)
1807 {
1808
1809         return (kern_kill(td, uap->pid, uap->signum));
1810 }
1811
1812 int
1813 kern_kill(struct thread *td, pid_t pid, int signum)
1814 {
1815         ksiginfo_t ksi;
1816         struct proc *p;
1817         int error;
1818
1819         /*
1820          * A process in capability mode can send signals only to himself.
1821          * The main rationale behind this is that abort(3) is implemented as
1822          * kill(getpid(), SIGABRT).
1823          */
1824         if (IN_CAPABILITY_MODE(td) && pid != td->td_proc->p_pid)
1825                 return (ECAPMODE);
1826
1827         AUDIT_ARG_SIGNUM(signum);
1828         AUDIT_ARG_PID(pid);
1829         if ((u_int)signum > _SIG_MAXSIG)
1830                 return (EINVAL);
1831
1832         ksiginfo_init(&ksi);
1833         ksi.ksi_signo = signum;
1834         ksi.ksi_code = SI_USER;
1835         ksi.ksi_pid = td->td_proc->p_pid;
1836         ksi.ksi_uid = td->td_ucred->cr_ruid;
1837
1838         if (pid > 0) {
1839                 /* kill single process */
1840                 if ((p = pfind_any(pid)) == NULL)
1841                         return (ESRCH);
1842                 AUDIT_ARG_PROCESS(p);
1843                 error = p_cansignal(td, p, signum);
1844                 if (error == 0 && signum)
1845                         pksignal(p, signum, &ksi);
1846                 PROC_UNLOCK(p);
1847                 return (error);
1848         }
1849         switch (pid) {
1850         case -1:                /* broadcast signal */
1851                 return (killpg1(td, signum, 0, 1, &ksi));
1852         case 0:                 /* signal own process group */
1853                 return (killpg1(td, signum, 0, 0, &ksi));
1854         default:                /* negative explicit process group */
1855                 return (killpg1(td, signum, -pid, 0, &ksi));
1856         }
1857         /* NOTREACHED */
1858 }
1859
1860 int
1861 sys_pdkill(struct thread *td, struct pdkill_args *uap)
1862 {
1863         struct proc *p;
1864         int error;
1865
1866         AUDIT_ARG_SIGNUM(uap->signum);
1867         AUDIT_ARG_FD(uap->fd);
1868         if ((u_int)uap->signum > _SIG_MAXSIG)
1869                 return (EINVAL);
1870
1871         error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p);
1872         if (error)
1873                 return (error);
1874         AUDIT_ARG_PROCESS(p);
1875         error = p_cansignal(td, p, uap->signum);
1876         if (error == 0 && uap->signum)
1877                 kern_psignal(p, uap->signum);
1878         PROC_UNLOCK(p);
1879         return (error);
1880 }
1881
1882 #if defined(COMPAT_43)
1883 #ifndef _SYS_SYSPROTO_H_
1884 struct okillpg_args {
1885         int     pgid;
1886         int     signum;
1887 };
1888 #endif
1889 /* ARGSUSED */
1890 int
1891 okillpg(struct thread *td, struct okillpg_args *uap)
1892 {
1893         ksiginfo_t ksi;
1894
1895         AUDIT_ARG_SIGNUM(uap->signum);
1896         AUDIT_ARG_PID(uap->pgid);
1897         if ((u_int)uap->signum > _SIG_MAXSIG)
1898                 return (EINVAL);
1899
1900         ksiginfo_init(&ksi);
1901         ksi.ksi_signo = uap->signum;
1902         ksi.ksi_code = SI_USER;
1903         ksi.ksi_pid = td->td_proc->p_pid;
1904         ksi.ksi_uid = td->td_ucred->cr_ruid;
1905         return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1906 }
1907 #endif /* COMPAT_43 */
1908
1909 #ifndef _SYS_SYSPROTO_H_
1910 struct sigqueue_args {
1911         pid_t pid;
1912         int signum;
1913         /* union sigval */ void *value;
1914 };
1915 #endif
1916 int
1917 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
1918 {
1919         union sigval sv;
1920
1921         sv.sival_ptr = uap->value;
1922
1923         return (kern_sigqueue(td, uap->pid, uap->signum, &sv));
1924 }
1925
1926 int
1927 kern_sigqueue(struct thread *td, pid_t pid, int signum, union sigval *value)
1928 {
1929         ksiginfo_t ksi;
1930         struct proc *p;
1931         int error;
1932
1933         if ((u_int)signum > _SIG_MAXSIG)
1934                 return (EINVAL);
1935
1936         /*
1937          * Specification says sigqueue can only send signal to
1938          * single process.
1939          */
1940         if (pid <= 0)
1941                 return (EINVAL);
1942
1943         if ((p = pfind_any(pid)) == NULL)
1944                 return (ESRCH);
1945         error = p_cansignal(td, p, signum);
1946         if (error == 0 && signum != 0) {
1947                 ksiginfo_init(&ksi);
1948                 ksi.ksi_flags = KSI_SIGQ;
1949                 ksi.ksi_signo = signum;
1950                 ksi.ksi_code = SI_QUEUE;
1951                 ksi.ksi_pid = td->td_proc->p_pid;
1952                 ksi.ksi_uid = td->td_ucred->cr_ruid;
1953                 ksi.ksi_value = *value;
1954                 error = pksignal(p, ksi.ksi_signo, &ksi);
1955         }
1956         PROC_UNLOCK(p);
1957         return (error);
1958 }
1959
1960 /*
1961  * Send a signal to a process group.
1962  */
1963 void
1964 gsignal(int pgid, int sig, ksiginfo_t *ksi)
1965 {
1966         struct pgrp *pgrp;
1967
1968         if (pgid != 0) {
1969                 sx_slock(&proctree_lock);
1970                 pgrp = pgfind(pgid);
1971                 sx_sunlock(&proctree_lock);
1972                 if (pgrp != NULL) {
1973                         pgsignal(pgrp, sig, 0, ksi);
1974                         PGRP_UNLOCK(pgrp);
1975                 }
1976         }
1977 }
1978
1979 /*
1980  * Send a signal to a process group.  If checktty is 1,
1981  * limit to members which have a controlling terminal.
1982  */
1983 void
1984 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1985 {
1986         struct proc *p;
1987
1988         if (pgrp) {
1989                 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1990                 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1991                         PROC_LOCK(p);
1992                         if (p->p_state == PRS_NORMAL &&
1993                             (checkctty == 0 || p->p_flag & P_CONTROLT))
1994                                 pksignal(p, sig, ksi);
1995                         PROC_UNLOCK(p);
1996                 }
1997         }
1998 }
1999
2000 /*
2001  * Recalculate the signal mask and reset the signal disposition after
2002  * usermode frame for delivery is formed.  Should be called after
2003  * mach-specific routine, because sysent->sv_sendsig() needs correct
2004  * ps_siginfo and signal mask.
2005  */
2006 static void
2007 postsig_done(int sig, struct thread *td, struct sigacts *ps)
2008 {
2009         sigset_t mask;
2010
2011         mtx_assert(&ps->ps_mtx, MA_OWNED);
2012         td->td_ru.ru_nsignals++;
2013         mask = ps->ps_catchmask[_SIG_IDX(sig)];
2014         if (!SIGISMEMBER(ps->ps_signodefer, sig))
2015                 SIGADDSET(mask, sig);
2016         kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
2017             SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
2018         if (SIGISMEMBER(ps->ps_sigreset, sig))
2019                 sigdflt(ps, sig);
2020 }
2021
2022 /*
2023  * Send a signal caused by a trap to the current thread.  If it will be
2024  * caught immediately, deliver it with correct code.  Otherwise, post it
2025  * normally.
2026  */
2027 void
2028 trapsignal(struct thread *td, ksiginfo_t *ksi)
2029 {
2030         struct sigacts *ps;
2031         struct proc *p;
2032         sigset_t sigmask;
2033         int code, sig;
2034
2035         p = td->td_proc;
2036         sig = ksi->ksi_signo;
2037         code = ksi->ksi_code;
2038         KASSERT(_SIG_VALID(sig), ("invalid signal"));
2039
2040         sigfastblock_fetch(td);
2041         PROC_LOCK(p);
2042         ps = p->p_sigacts;
2043         mtx_lock(&ps->ps_mtx);
2044         sigmask = td->td_sigmask;
2045         if (td->td_sigblock_val != 0)
2046                 SIGSETOR(sigmask, fastblock_mask);
2047         if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
2048             !SIGISMEMBER(sigmask, sig)) {
2049 #ifdef KTRACE
2050                 if (KTRPOINT(curthread, KTR_PSIG))
2051                         ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
2052                             &td->td_sigmask, code);
2053 #endif
2054                 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
2055                     ksi, &td->td_sigmask);
2056                 postsig_done(sig, td, ps);
2057                 mtx_unlock(&ps->ps_mtx);
2058         } else {
2059                 /*
2060                  * Avoid a possible infinite loop if the thread
2061                  * masking the signal or process is ignoring the
2062                  * signal.
2063                  */
2064                 if (kern_forcesigexit && (SIGISMEMBER(sigmask, sig) ||
2065                     ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
2066                         SIGDELSET(td->td_sigmask, sig);
2067                         SIGDELSET(ps->ps_sigcatch, sig);
2068                         SIGDELSET(ps->ps_sigignore, sig);
2069                         ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2070                         td->td_pflags &= ~TDP_SIGFASTBLOCK;
2071                         td->td_sigblock_val = 0;
2072                 }
2073                 mtx_unlock(&ps->ps_mtx);
2074                 p->p_sig = sig;         /* XXX to verify code */
2075                 tdsendsignal(p, td, sig, ksi);
2076         }
2077         PROC_UNLOCK(p);
2078 }
2079
2080 static struct thread *
2081 sigtd(struct proc *p, int sig, bool fast_sigblock)
2082 {
2083         struct thread *td, *signal_td;
2084
2085         PROC_LOCK_ASSERT(p, MA_OWNED);
2086         MPASS(!fast_sigblock || p == curproc);
2087
2088         /*
2089          * Check if current thread can handle the signal without
2090          * switching context to another thread.
2091          */
2092         if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig) &&
2093             (!fast_sigblock || curthread->td_sigblock_val == 0))
2094                 return (curthread);
2095         signal_td = NULL;
2096         FOREACH_THREAD_IN_PROC(p, td) {
2097                 if (!SIGISMEMBER(td->td_sigmask, sig) && (!fast_sigblock ||
2098                     td != curthread || td->td_sigblock_val == 0)) {
2099                         signal_td = td;
2100                         break;
2101                 }
2102         }
2103         if (signal_td == NULL)
2104                 signal_td = FIRST_THREAD_IN_PROC(p);
2105         return (signal_td);
2106 }
2107
2108 /*
2109  * Send the signal to the process.  If the signal has an action, the action
2110  * is usually performed by the target process rather than the caller; we add
2111  * the signal to the set of pending signals for the process.
2112  *
2113  * Exceptions:
2114  *   o When a stop signal is sent to a sleeping process that takes the
2115  *     default action, the process is stopped without awakening it.
2116  *   o SIGCONT restarts stopped processes (or puts them back to sleep)
2117  *     regardless of the signal action (eg, blocked or ignored).
2118  *
2119  * Other ignored signals are discarded immediately.
2120  *
2121  * NB: This function may be entered from the debugger via the "kill" DDB
2122  * command.  There is little that can be done to mitigate the possibly messy
2123  * side effects of this unwise possibility.
2124  */
2125 void
2126 kern_psignal(struct proc *p, int sig)
2127 {
2128         ksiginfo_t ksi;
2129
2130         ksiginfo_init(&ksi);
2131         ksi.ksi_signo = sig;
2132         ksi.ksi_code = SI_KERNEL;
2133         (void) tdsendsignal(p, NULL, sig, &ksi);
2134 }
2135
2136 int
2137 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2138 {
2139
2140         return (tdsendsignal(p, NULL, sig, ksi));
2141 }
2142
2143 /* Utility function for finding a thread to send signal event to. */
2144 int
2145 sigev_findtd(struct proc *p, struct sigevent *sigev, struct thread **ttd)
2146 {
2147         struct thread *td;
2148
2149         if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2150                 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2151                 if (td == NULL)
2152                         return (ESRCH);
2153                 *ttd = td;
2154         } else {
2155                 *ttd = NULL;
2156                 PROC_LOCK(p);
2157         }
2158         return (0);
2159 }
2160
2161 void
2162 tdsignal(struct thread *td, int sig)
2163 {
2164         ksiginfo_t ksi;
2165
2166         ksiginfo_init(&ksi);
2167         ksi.ksi_signo = sig;
2168         ksi.ksi_code = SI_KERNEL;
2169         (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2170 }
2171
2172 void
2173 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2174 {
2175
2176         (void) tdsendsignal(td->td_proc, td, sig, ksi);
2177 }
2178
2179 static int
2180 sig_sleepq_abort(struct thread *td, int intrval)
2181 {
2182         THREAD_LOCK_ASSERT(td, MA_OWNED);
2183
2184         if (intrval == 0 && (td->td_flags & TDF_SIGWAIT) == 0) {
2185                 thread_unlock(td);
2186                 return (0);
2187         }
2188         return (sleepq_abort(td, intrval));
2189 }
2190
2191 int
2192 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2193 {
2194         sig_t action;
2195         sigqueue_t *sigqueue;
2196         int prop;
2197         struct sigacts *ps;
2198         int intrval;
2199         int ret = 0;
2200         int wakeup_swapper;
2201
2202         MPASS(td == NULL || p == td->td_proc);
2203         PROC_LOCK_ASSERT(p, MA_OWNED);
2204
2205         if (!_SIG_VALID(sig))
2206                 panic("%s(): invalid signal %d", __func__, sig);
2207
2208         KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2209
2210         /*
2211          * IEEE Std 1003.1-2001: return success when killing a zombie.
2212          */
2213         if (p->p_state == PRS_ZOMBIE) {
2214                 if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2215                         ksiginfo_tryfree(ksi);
2216                 return (ret);
2217         }
2218
2219         ps = p->p_sigacts;
2220         KNOTE_LOCKED(p->p_klist, NOTE_SIGNAL | sig);
2221         prop = sigprop(sig);
2222
2223         if (td == NULL) {
2224                 td = sigtd(p, sig, false);
2225                 sigqueue = &p->p_sigqueue;
2226         } else
2227                 sigqueue = &td->td_sigqueue;
2228
2229         SDT_PROBE3(proc, , , signal__send, td, p, sig);
2230
2231         /*
2232          * If the signal is being ignored, then we forget about it
2233          * immediately, except when the target process executes
2234          * sigwait().  (Note: we don't set SIGCONT in ps_sigignore,
2235          * and if it is set to SIG_IGN, action will be SIG_DFL here.)
2236          */
2237         mtx_lock(&ps->ps_mtx);
2238         if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2239                 if (kern_sig_discard_ign &&
2240                     (p->p_sysent->sv_flags & SV_SIG_DISCIGN) == 0) {
2241                         SDT_PROBE3(proc, , , signal__discard, td, p, sig);
2242
2243                         mtx_unlock(&ps->ps_mtx);
2244                         if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2245                                 ksiginfo_tryfree(ksi);
2246                         return (ret);
2247                 } else {
2248                         action = SIG_CATCH;
2249                         intrval = 0;
2250                 }
2251         } else {
2252                 if (SIGISMEMBER(td->td_sigmask, sig))
2253                         action = SIG_HOLD;
2254                 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2255                         action = SIG_CATCH;
2256                 else
2257                         action = SIG_DFL;
2258                 if (SIGISMEMBER(ps->ps_sigintr, sig))
2259                         intrval = EINTR;
2260                 else
2261                         intrval = ERESTART;
2262         }
2263         mtx_unlock(&ps->ps_mtx);
2264
2265         if (prop & SIGPROP_CONT)
2266                 sigqueue_delete_stopmask_proc(p);
2267         else if (prop & SIGPROP_STOP) {
2268                 /*
2269                  * If sending a tty stop signal to a member of an orphaned
2270                  * process group, discard the signal here if the action
2271                  * is default; don't stop the process below if sleeping,
2272                  * and don't clear any pending SIGCONT.
2273                  */
2274                 if ((prop & SIGPROP_TTYSTOP) != 0 &&
2275                     (p->p_pgrp->pg_flags & PGRP_ORPHANED) != 0 &&
2276                     action == SIG_DFL) {
2277                         if (ksi != NULL && (ksi->ksi_flags & KSI_INS) != 0)
2278                                 ksiginfo_tryfree(ksi);
2279                         return (ret);
2280                 }
2281                 sigqueue_delete_proc(p, SIGCONT);
2282                 if (p->p_flag & P_CONTINUED) {
2283                         p->p_flag &= ~P_CONTINUED;
2284                         PROC_LOCK(p->p_pptr);
2285                         sigqueue_take(p->p_ksi);
2286                         PROC_UNLOCK(p->p_pptr);
2287                 }
2288         }
2289
2290         ret = sigqueue_add(sigqueue, sig, ksi);
2291         if (ret != 0)
2292                 return (ret);
2293         signotify(td);
2294         /*
2295          * Defer further processing for signals which are held,
2296          * except that stopped processes must be continued by SIGCONT.
2297          */
2298         if (action == SIG_HOLD &&
2299             !((prop & SIGPROP_CONT) && (p->p_flag & P_STOPPED_SIG)))
2300                 return (ret);
2301
2302         wakeup_swapper = 0;
2303
2304         /*
2305          * Some signals have a process-wide effect and a per-thread
2306          * component.  Most processing occurs when the process next
2307          * tries to cross the user boundary, however there are some
2308          * times when processing needs to be done immediately, such as
2309          * waking up threads so that they can cross the user boundary.
2310          * We try to do the per-process part here.
2311          */
2312         if (P_SHOULDSTOP(p)) {
2313                 KASSERT(!(p->p_flag & P_WEXIT),
2314                     ("signal to stopped but exiting process"));
2315                 if (sig == SIGKILL) {
2316                         /*
2317                          * If traced process is already stopped,
2318                          * then no further action is necessary.
2319                          */
2320                         if (p->p_flag & P_TRACED)
2321                                 goto out;
2322                         /*
2323                          * SIGKILL sets process running.
2324                          * It will die elsewhere.
2325                          * All threads must be restarted.
2326                          */
2327                         p->p_flag &= ~P_STOPPED_SIG;
2328                         goto runfast;
2329                 }
2330
2331                 if (prop & SIGPROP_CONT) {
2332                         /*
2333                          * If traced process is already stopped,
2334                          * then no further action is necessary.
2335                          */
2336                         if (p->p_flag & P_TRACED)
2337                                 goto out;
2338                         /*
2339                          * If SIGCONT is default (or ignored), we continue the
2340                          * process but don't leave the signal in sigqueue as
2341                          * it has no further action.  If SIGCONT is held, we
2342                          * continue the process and leave the signal in
2343                          * sigqueue.  If the process catches SIGCONT, let it
2344                          * handle the signal itself.  If it isn't waiting on
2345                          * an event, it goes back to run state.
2346                          * Otherwise, process goes back to sleep state.
2347                          */
2348                         p->p_flag &= ~P_STOPPED_SIG;
2349                         PROC_SLOCK(p);
2350                         if (p->p_numthreads == p->p_suspcount) {
2351                                 PROC_SUNLOCK(p);
2352                                 p->p_flag |= P_CONTINUED;
2353                                 p->p_xsig = SIGCONT;
2354                                 PROC_LOCK(p->p_pptr);
2355                                 childproc_continued(p);
2356                                 PROC_UNLOCK(p->p_pptr);
2357                                 PROC_SLOCK(p);
2358                         }
2359                         if (action == SIG_DFL) {
2360                                 thread_unsuspend(p);
2361                                 PROC_SUNLOCK(p);
2362                                 sigqueue_delete(sigqueue, sig);
2363                                 goto out_cont;
2364                         }
2365                         if (action == SIG_CATCH) {
2366                                 /*
2367                                  * The process wants to catch it so it needs
2368                                  * to run at least one thread, but which one?
2369                                  */
2370                                 PROC_SUNLOCK(p);
2371                                 goto runfast;
2372                         }
2373                         /*
2374                          * The signal is not ignored or caught.
2375                          */
2376                         thread_unsuspend(p);
2377                         PROC_SUNLOCK(p);
2378                         goto out_cont;
2379                 }
2380
2381                 if (prop & SIGPROP_STOP) {
2382                         /*
2383                          * If traced process is already stopped,
2384                          * then no further action is necessary.
2385                          */
2386                         if (p->p_flag & P_TRACED)
2387                                 goto out;
2388                         /*
2389                          * Already stopped, don't need to stop again
2390                          * (If we did the shell could get confused).
2391                          * Just make sure the signal STOP bit set.
2392                          */
2393                         p->p_flag |= P_STOPPED_SIG;
2394                         sigqueue_delete(sigqueue, sig);
2395                         goto out;
2396                 }
2397
2398                 /*
2399                  * All other kinds of signals:
2400                  * If a thread is sleeping interruptibly, simulate a
2401                  * wakeup so that when it is continued it will be made
2402                  * runnable and can look at the signal.  However, don't make
2403                  * the PROCESS runnable, leave it stopped.
2404                  * It may run a bit until it hits a thread_suspend_check().
2405                  */
2406                 PROC_SLOCK(p);
2407                 thread_lock(td);
2408                 if (TD_CAN_ABORT(td))
2409                         wakeup_swapper = sig_sleepq_abort(td, intrval);
2410                 else
2411                         thread_unlock(td);
2412                 PROC_SUNLOCK(p);
2413                 goto out;
2414                 /*
2415                  * Mutexes are short lived. Threads waiting on them will
2416                  * hit thread_suspend_check() soon.
2417                  */
2418         } else if (p->p_state == PRS_NORMAL) {
2419                 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2420                         tdsigwakeup(td, sig, action, intrval);
2421                         goto out;
2422                 }
2423
2424                 MPASS(action == SIG_DFL);
2425
2426                 if (prop & SIGPROP_STOP) {
2427                         if (p->p_flag & (P_PPWAIT|P_WEXIT))
2428                                 goto out;
2429                         p->p_flag |= P_STOPPED_SIG;
2430                         p->p_xsig = sig;
2431                         PROC_SLOCK(p);
2432                         wakeup_swapper = sig_suspend_threads(td, p, 1);
2433                         if (p->p_numthreads == p->p_suspcount) {
2434                                 /*
2435                                  * only thread sending signal to another
2436                                  * process can reach here, if thread is sending
2437                                  * signal to its process, because thread does
2438                                  * not suspend itself here, p_numthreads
2439                                  * should never be equal to p_suspcount.
2440                                  */
2441                                 thread_stopped(p);
2442                                 PROC_SUNLOCK(p);
2443                                 sigqueue_delete_proc(p, p->p_xsig);
2444                         } else
2445                                 PROC_SUNLOCK(p);
2446                         goto out;
2447                 }
2448         } else {
2449                 /* Not in "NORMAL" state. discard the signal. */
2450                 sigqueue_delete(sigqueue, sig);
2451                 goto out;
2452         }
2453
2454         /*
2455          * The process is not stopped so we need to apply the signal to all the
2456          * running threads.
2457          */
2458 runfast:
2459         tdsigwakeup(td, sig, action, intrval);
2460         PROC_SLOCK(p);
2461         thread_unsuspend(p);
2462         PROC_SUNLOCK(p);
2463 out_cont:
2464         itimer_proc_continue(p);
2465         kqtimer_proc_continue(p);
2466 out:
2467         /* If we jump here, proc slock should not be owned. */
2468         PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2469         if (wakeup_swapper)
2470                 kick_proc0();
2471
2472         return (ret);
2473 }
2474
2475 /*
2476  * The force of a signal has been directed against a single
2477  * thread.  We need to see what we can do about knocking it
2478  * out of any sleep it may be in etc.
2479  */
2480 static void
2481 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2482 {
2483         struct proc *p = td->td_proc;
2484         int prop, wakeup_swapper;
2485
2486         PROC_LOCK_ASSERT(p, MA_OWNED);
2487         prop = sigprop(sig);
2488
2489         PROC_SLOCK(p);
2490         thread_lock(td);
2491         /*
2492          * Bring the priority of a thread up if we want it to get
2493          * killed in this lifetime.  Be careful to avoid bumping the
2494          * priority of the idle thread, since we still allow to signal
2495          * kernel processes.
2496          */
2497         if (action == SIG_DFL && (prop & SIGPROP_KILL) != 0 &&
2498             td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2499                 sched_prio(td, PUSER);
2500         if (TD_ON_SLEEPQ(td)) {
2501                 /*
2502                  * If thread is sleeping uninterruptibly
2503                  * we can't interrupt the sleep... the signal will
2504                  * be noticed when the process returns through
2505                  * trap() or syscall().
2506                  */
2507                 if ((td->td_flags & TDF_SINTR) == 0)
2508                         goto out;
2509                 /*
2510                  * If SIGCONT is default (or ignored) and process is
2511                  * asleep, we are finished; the process should not
2512                  * be awakened.
2513                  */
2514                 if ((prop & SIGPROP_CONT) && action == SIG_DFL) {
2515                         thread_unlock(td);
2516                         PROC_SUNLOCK(p);
2517                         sigqueue_delete(&p->p_sigqueue, sig);
2518                         /*
2519                          * It may be on either list in this state.
2520                          * Remove from both for now.
2521                          */
2522                         sigqueue_delete(&td->td_sigqueue, sig);
2523                         return;
2524                 }
2525
2526                 /*
2527                  * Don't awaken a sleeping thread for SIGSTOP if the
2528                  * STOP signal is deferred.
2529                  */
2530                 if ((prop & SIGPROP_STOP) != 0 && (td->td_flags & (TDF_SBDRY |
2531                     TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
2532                         goto out;
2533
2534                 /*
2535                  * Give low priority threads a better chance to run.
2536                  */
2537                 if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2538                         sched_prio(td, PUSER);
2539
2540                 wakeup_swapper = sig_sleepq_abort(td, intrval);
2541                 PROC_SUNLOCK(p);
2542                 if (wakeup_swapper)
2543                         kick_proc0();
2544                 return;
2545         }
2546
2547         /*
2548          * Other states do nothing with the signal immediately,
2549          * other than kicking ourselves if we are running.
2550          * It will either never be noticed, or noticed very soon.
2551          */
2552 #ifdef SMP
2553         if (TD_IS_RUNNING(td) && td != curthread)
2554                 forward_signal(td);
2555 #endif
2556
2557 out:
2558         PROC_SUNLOCK(p);
2559         thread_unlock(td);
2560 }
2561
2562 static void
2563 ptrace_coredumpreq(struct thread *td, struct proc *p,
2564     struct thr_coredump_req *tcq)
2565 {
2566         void *rl_cookie;
2567
2568         if (p->p_sysent->sv_coredump == NULL) {
2569                 tcq->tc_error = ENOSYS;
2570                 return;
2571         }
2572
2573         rl_cookie = vn_rangelock_wlock(tcq->tc_vp, 0, OFF_MAX);
2574         tcq->tc_error = p->p_sysent->sv_coredump(td, tcq->tc_vp,
2575             tcq->tc_limit, tcq->tc_flags);
2576         vn_rangelock_unlock(tcq->tc_vp, rl_cookie);
2577 }
2578
2579 static void
2580 ptrace_syscallreq(struct thread *td, struct proc *p,
2581     struct thr_syscall_req *tsr)
2582 {
2583         struct sysentvec *sv;
2584         struct sysent *se;
2585         register_t rv_saved[2];
2586         int error, nerror;
2587         int sc;
2588         bool audited, sy_thr_static;
2589
2590         sv = p->p_sysent;
2591         if (sv->sv_table == NULL || sv->sv_size < tsr->ts_sa.code) {
2592                 tsr->ts_ret.sr_error = ENOSYS;
2593                 return;
2594         }
2595
2596         sc = tsr->ts_sa.code;
2597         if (sc == SYS_syscall || sc == SYS___syscall) {
2598                 sc = tsr->ts_sa.args[0];
2599                 memmove(&tsr->ts_sa.args[0], &tsr->ts_sa.args[1],
2600                     sizeof(register_t) * (tsr->ts_nargs - 1));
2601         }
2602
2603         tsr->ts_sa.callp = se = &sv->sv_table[sc];
2604
2605         VM_CNT_INC(v_syscall);
2606         td->td_pticks = 0;
2607         if (__predict_false(td->td_cowgen != atomic_load_int(
2608             &td->td_proc->p_cowgen)))
2609                 thread_cow_update(td);
2610
2611 #ifdef CAPABILITY_MODE
2612         if (IN_CAPABILITY_MODE(td) && (se->sy_flags & SYF_CAPENABLED) == 0) {
2613                 tsr->ts_ret.sr_error = ECAPMODE;
2614                 return;
2615         }
2616 #endif
2617
2618         sy_thr_static = (se->sy_thrcnt & SY_THR_STATIC) != 0;
2619         audited = AUDIT_SYSCALL_ENTER(sc, td) != 0;
2620
2621         if (!sy_thr_static) {
2622                 error = syscall_thread_enter(td, se);
2623                 if (error != 0) {
2624                         tsr->ts_ret.sr_error = error;
2625                         return;
2626                 }
2627         }
2628
2629         rv_saved[0] = td->td_retval[0];
2630         rv_saved[1] = td->td_retval[1];
2631         nerror = td->td_errno;
2632         td->td_retval[0] = 0;
2633         td->td_retval[1] = 0;
2634
2635 #ifdef KDTRACE_HOOKS
2636         if (se->sy_entry != 0)
2637                 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_ENTRY, 0);
2638 #endif
2639         tsr->ts_ret.sr_error = se->sy_call(td, tsr->ts_sa.args);
2640 #ifdef KDTRACE_HOOKS
2641         if (se->sy_return != 0)
2642                 (*systrace_probe_func)(&tsr->ts_sa, SYSTRACE_RETURN,
2643                     tsr->ts_ret.sr_error != 0 ? -1 : td->td_retval[0]);
2644 #endif
2645
2646         tsr->ts_ret.sr_retval[0] = td->td_retval[0];
2647         tsr->ts_ret.sr_retval[1] = td->td_retval[1];
2648         td->td_retval[0] = rv_saved[0];
2649         td->td_retval[1] = rv_saved[1];
2650         td->td_errno = nerror;
2651
2652         if (audited)
2653                 AUDIT_SYSCALL_EXIT(error, td);
2654         if (!sy_thr_static)
2655                 syscall_thread_exit(td, se);
2656 }
2657
2658 static void
2659 ptrace_remotereq(struct thread *td, int flag)
2660 {
2661         struct proc *p;
2662
2663         MPASS(td == curthread);
2664         p = td->td_proc;
2665         PROC_LOCK_ASSERT(p, MA_OWNED);
2666         if ((td->td_dbgflags & flag) == 0)
2667                 return;
2668         KASSERT((p->p_flag & P_STOPPED_TRACE) != 0, ("not stopped"));
2669         KASSERT(td->td_remotereq != NULL, ("td_remotereq is NULL"));
2670
2671         PROC_UNLOCK(p);
2672         switch (flag) {
2673         case TDB_COREDUMPREQ:
2674                 ptrace_coredumpreq(td, p, td->td_remotereq);
2675                 break;
2676         case TDB_SCREMOTEREQ:
2677                 ptrace_syscallreq(td, p, td->td_remotereq);
2678                 break;
2679         default:
2680                 __unreachable();
2681         }
2682         PROC_LOCK(p);
2683
2684         MPASS((td->td_dbgflags & flag) != 0);
2685         td->td_dbgflags &= ~flag;
2686         td->td_remotereq = NULL;
2687         wakeup(p);
2688 }
2689
2690 static int
2691 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2692 {
2693         struct thread *td2;
2694         int wakeup_swapper;
2695
2696         PROC_LOCK_ASSERT(p, MA_OWNED);
2697         PROC_SLOCK_ASSERT(p, MA_OWNED);
2698         MPASS(sending || td == curthread);
2699
2700         wakeup_swapper = 0;
2701         FOREACH_THREAD_IN_PROC(p, td2) {
2702                 thread_lock(td2);
2703                 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2704                 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2705                     (td2->td_flags & TDF_SINTR)) {
2706                         if (td2->td_flags & TDF_SBDRY) {
2707                                 /*
2708                                  * Once a thread is asleep with
2709                                  * TDF_SBDRY and without TDF_SERESTART
2710                                  * or TDF_SEINTR set, it should never
2711                                  * become suspended due to this check.
2712                                  */
2713                                 KASSERT(!TD_IS_SUSPENDED(td2),
2714                                     ("thread with deferred stops suspended"));
2715                                 if (TD_SBDRY_INTR(td2)) {
2716                                         wakeup_swapper |= sleepq_abort(td2,
2717                                             TD_SBDRY_ERRNO(td2));
2718                                         continue;
2719                                 }
2720                         } else if (!TD_IS_SUSPENDED(td2))
2721                                 thread_suspend_one(td2);
2722                 } else if (!TD_IS_SUSPENDED(td2)) {
2723                         if (sending || td != td2)
2724                                 td2->td_flags |= TDF_ASTPENDING;
2725 #ifdef SMP
2726                         if (TD_IS_RUNNING(td2) && td2 != td)
2727                                 forward_signal(td2);
2728 #endif
2729                 }
2730                 thread_unlock(td2);
2731         }
2732         return (wakeup_swapper);
2733 }
2734
2735 /*
2736  * Stop the process for an event deemed interesting to the debugger. If si is
2737  * non-NULL, this is a signal exchange; the new signal requested by the
2738  * debugger will be returned for handling. If si is NULL, this is some other
2739  * type of interesting event. The debugger may request a signal be delivered in
2740  * that case as well, however it will be deferred until it can be handled.
2741  */
2742 int
2743 ptracestop(struct thread *td, int sig, ksiginfo_t *si)
2744 {
2745         struct proc *p = td->td_proc;
2746         struct thread *td2;
2747         ksiginfo_t ksi;
2748
2749         PROC_LOCK_ASSERT(p, MA_OWNED);
2750         KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2751         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2752             &p->p_mtx.lock_object, "Stopping for traced signal");
2753
2754         td->td_xsig = sig;
2755
2756         if (si == NULL || (si->ksi_flags & KSI_PTRACE) == 0) {
2757                 td->td_dbgflags |= TDB_XSIG;
2758                 CTR4(KTR_PTRACE, "ptracestop: tid %d (pid %d) flags %#x sig %d",
2759                     td->td_tid, p->p_pid, td->td_dbgflags, sig);
2760                 PROC_SLOCK(p);
2761                 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2762                         if (P_KILLED(p)) {
2763                                 /*
2764                                  * Ensure that, if we've been PT_KILLed, the
2765                                  * exit status reflects that. Another thread
2766                                  * may also be in ptracestop(), having just
2767                                  * received the SIGKILL, but this thread was
2768                                  * unsuspended first.
2769                                  */
2770                                 td->td_dbgflags &= ~TDB_XSIG;
2771                                 td->td_xsig = SIGKILL;
2772                                 p->p_ptevents = 0;
2773                                 break;
2774                         }
2775                         if (p->p_flag & P_SINGLE_EXIT &&
2776                             !(td->td_dbgflags & TDB_EXIT)) {
2777                                 /*
2778                                  * Ignore ptrace stops except for thread exit
2779                                  * events when the process exits.
2780                                  */
2781                                 td->td_dbgflags &= ~TDB_XSIG;
2782                                 PROC_SUNLOCK(p);
2783                                 return (0);
2784                         }
2785
2786                         /*
2787                          * Make wait(2) work.  Ensure that right after the
2788                          * attach, the thread which was decided to become the
2789                          * leader of attach gets reported to the waiter.
2790                          * Otherwise, just avoid overwriting another thread's
2791                          * assignment to p_xthread.  If another thread has
2792                          * already set p_xthread, the current thread will get
2793                          * a chance to report itself upon the next iteration.
2794                          */
2795                         if ((td->td_dbgflags & TDB_FSTP) != 0 ||
2796                             ((p->p_flag2 & P2_PTRACE_FSTP) == 0 &&
2797                             p->p_xthread == NULL)) {
2798                                 p->p_xsig = sig;
2799                                 p->p_xthread = td;
2800
2801                                 /*
2802                                  * If we are on sleepqueue already,
2803                                  * let sleepqueue code decide if it
2804                                  * needs to go sleep after attach.
2805                                  */
2806                                 if (td->td_wchan == NULL)
2807                                         td->td_dbgflags &= ~TDB_FSTP;
2808
2809                                 p->p_flag2 &= ~P2_PTRACE_FSTP;
2810                                 p->p_flag |= P_STOPPED_SIG | P_STOPPED_TRACE;
2811                                 sig_suspend_threads(td, p, 0);
2812                         }
2813                         if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2814                                 td->td_dbgflags &= ~TDB_STOPATFORK;
2815                         }
2816 stopme:
2817                         td->td_dbgflags |= TDB_SSWITCH;
2818                         thread_suspend_switch(td, p);
2819                         td->td_dbgflags &= ~TDB_SSWITCH;
2820                         if ((td->td_dbgflags & (TDB_COREDUMPREQ |
2821                             TDB_SCREMOTEREQ)) != 0) {
2822                                 MPASS((td->td_dbgflags & (TDB_COREDUMPREQ |
2823                                     TDB_SCREMOTEREQ)) !=
2824                                     (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
2825                                 PROC_SUNLOCK(p);
2826                                 ptrace_remotereq(td, td->td_dbgflags &
2827                                     (TDB_COREDUMPREQ | TDB_SCREMOTEREQ));
2828                                 PROC_SLOCK(p);
2829                                 goto stopme;
2830                         }
2831                         if (p->p_xthread == td)
2832                                 p->p_xthread = NULL;
2833                         if (!(p->p_flag & P_TRACED))
2834                                 break;
2835                         if (td->td_dbgflags & TDB_SUSPEND) {
2836                                 if (p->p_flag & P_SINGLE_EXIT)
2837                                         break;
2838                                 goto stopme;
2839                         }
2840                 }
2841                 PROC_SUNLOCK(p);
2842         }
2843
2844         if (si != NULL && sig == td->td_xsig) {
2845                 /* Parent wants us to take the original signal unchanged. */
2846                 si->ksi_flags |= KSI_HEAD;
2847                 if (sigqueue_add(&td->td_sigqueue, sig, si) != 0)
2848                         si->ksi_signo = 0;
2849         } else if (td->td_xsig != 0) {
2850                 /*
2851                  * If parent wants us to take a new signal, then it will leave
2852                  * it in td->td_xsig; otherwise we just look for signals again.
2853                  */
2854                 ksiginfo_init(&ksi);
2855                 ksi.ksi_signo = td->td_xsig;
2856                 ksi.ksi_flags |= KSI_PTRACE;
2857                 td2 = sigtd(p, td->td_xsig, false);
2858                 tdsendsignal(p, td2, td->td_xsig, &ksi);
2859                 if (td != td2)
2860                         return (0);
2861         }
2862
2863         return (td->td_xsig);
2864 }
2865
2866 static void
2867 reschedule_signals(struct proc *p, sigset_t block, int flags)
2868 {
2869         struct sigacts *ps;
2870         struct thread *td;
2871         int sig;
2872         bool fastblk, pslocked;
2873
2874         PROC_LOCK_ASSERT(p, MA_OWNED);
2875         ps = p->p_sigacts;
2876         pslocked = (flags & SIGPROCMASK_PS_LOCKED) != 0;
2877         mtx_assert(&ps->ps_mtx, pslocked ? MA_OWNED : MA_NOTOWNED);
2878         if (SIGISEMPTY(p->p_siglist))
2879                 return;
2880         SIGSETAND(block, p->p_siglist);
2881         fastblk = (flags & SIGPROCMASK_FASTBLK) != 0;
2882         SIG_FOREACH(sig, &block) {
2883                 td = sigtd(p, sig, fastblk);
2884
2885                 /*
2886                  * If sigtd() selected us despite sigfastblock is
2887                  * blocking, do not activate AST or wake us, to avoid
2888                  * loop in AST handler.
2889                  */
2890                 if (fastblk && td == curthread)
2891                         continue;
2892
2893                 signotify(td);
2894                 if (!pslocked)
2895                         mtx_lock(&ps->ps_mtx);
2896                 if (p->p_flag & P_TRACED ||
2897                     (SIGISMEMBER(ps->ps_sigcatch, sig) &&
2898                     !SIGISMEMBER(td->td_sigmask, sig))) {
2899                         tdsigwakeup(td, sig, SIG_CATCH,
2900                             (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2901                             ERESTART));
2902                 }
2903                 if (!pslocked)
2904                         mtx_unlock(&ps->ps_mtx);
2905         }
2906 }
2907
2908 void
2909 tdsigcleanup(struct thread *td)
2910 {
2911         struct proc *p;
2912         sigset_t unblocked;
2913
2914         p = td->td_proc;
2915         PROC_LOCK_ASSERT(p, MA_OWNED);
2916
2917         sigqueue_flush(&td->td_sigqueue);
2918         if (p->p_numthreads == 1)
2919                 return;
2920
2921         /*
2922          * Since we cannot handle signals, notify signal post code
2923          * about this by filling the sigmask.
2924          *
2925          * Also, if needed, wake up thread(s) that do not block the
2926          * same signals as the exiting thread, since the thread might
2927          * have been selected for delivery and woken up.
2928          */
2929         SIGFILLSET(unblocked);
2930         SIGSETNAND(unblocked, td->td_sigmask);
2931         SIGFILLSET(td->td_sigmask);
2932         reschedule_signals(p, unblocked, 0);
2933
2934 }
2935
2936 static int
2937 sigdeferstop_curr_flags(int cflags)
2938 {
2939
2940         MPASS((cflags & (TDF_SEINTR | TDF_SERESTART)) == 0 ||
2941             (cflags & TDF_SBDRY) != 0);
2942         return (cflags & (TDF_SBDRY | TDF_SEINTR | TDF_SERESTART));
2943 }
2944
2945 /*
2946  * Defer the delivery of SIGSTOP for the current thread, according to
2947  * the requested mode.  Returns previous flags, which must be restored
2948  * by sigallowstop().
2949  *
2950  * TDF_SBDRY, TDF_SEINTR, and TDF_SERESTART flags are only set and
2951  * cleared by the current thread, which allow the lock-less read-only
2952  * accesses below.
2953  */
2954 int
2955 sigdeferstop_impl(int mode)
2956 {
2957         struct thread *td;
2958         int cflags, nflags;
2959
2960         td = curthread;
2961         cflags = sigdeferstop_curr_flags(td->td_flags);
2962         switch (mode) {
2963         case SIGDEFERSTOP_NOP:
2964                 nflags = cflags;
2965                 break;
2966         case SIGDEFERSTOP_OFF:
2967                 nflags = 0;
2968                 break;
2969         case SIGDEFERSTOP_SILENT:
2970                 nflags = (cflags | TDF_SBDRY) & ~(TDF_SEINTR | TDF_SERESTART);
2971                 break;
2972         case SIGDEFERSTOP_EINTR:
2973                 nflags = (cflags | TDF_SBDRY | TDF_SEINTR) & ~TDF_SERESTART;
2974                 break;
2975         case SIGDEFERSTOP_ERESTART:
2976                 nflags = (cflags | TDF_SBDRY | TDF_SERESTART) & ~TDF_SEINTR;
2977                 break;
2978         default:
2979                 panic("sigdeferstop: invalid mode %x", mode);
2980                 break;
2981         }
2982         if (cflags == nflags)
2983                 return (SIGDEFERSTOP_VAL_NCHG);
2984         thread_lock(td);
2985         td->td_flags = (td->td_flags & ~cflags) | nflags;
2986         thread_unlock(td);
2987         return (cflags);
2988 }
2989
2990 /*
2991  * Restores the STOP handling mode, typically permitting the delivery
2992  * of SIGSTOP for the current thread.  This does not immediately
2993  * suspend if a stop was posted.  Instead, the thread will suspend
2994  * either via ast() or a subsequent interruptible sleep.
2995  */
2996 void
2997 sigallowstop_impl(int prev)
2998 {
2999         struct thread *td;
3000         int cflags;
3001
3002         KASSERT(prev != SIGDEFERSTOP_VAL_NCHG, ("failed sigallowstop"));
3003         KASSERT((prev & ~(TDF_SBDRY | TDF_SEINTR | TDF_SERESTART)) == 0,
3004             ("sigallowstop: incorrect previous mode %x", prev));
3005         td = curthread;
3006         cflags = sigdeferstop_curr_flags(td->td_flags);
3007         if (cflags != prev) {
3008                 thread_lock(td);
3009                 td->td_flags = (td->td_flags & ~cflags) | prev;
3010                 thread_unlock(td);
3011         }
3012 }
3013
3014 enum sigstatus {
3015         SIGSTATUS_HANDLE,
3016         SIGSTATUS_HANDLED,
3017         SIGSTATUS_IGNORE,
3018         SIGSTATUS_SBDRY_STOP,
3019 };
3020
3021 /*
3022  * The thread has signal "sig" pending.  Figure out what to do with it:
3023  *
3024  * _HANDLE     -> the caller should handle the signal
3025  * _HANDLED    -> handled internally, reload pending signal set
3026  * _IGNORE     -> ignored, remove from the set of pending signals and try the
3027  *                next pending signal
3028  * _SBDRY_STOP -> the signal should stop the thread but this is not
3029  *                permitted in the current context
3030  */
3031 static enum sigstatus
3032 sigprocess(struct thread *td, int sig)
3033 {
3034         struct proc *p;
3035         struct sigacts *ps;
3036         struct sigqueue *queue;
3037         ksiginfo_t ksi;
3038         int prop;
3039
3040         KASSERT(_SIG_VALID(sig), ("%s: invalid signal %d", __func__, sig));
3041
3042         p = td->td_proc;
3043         ps = p->p_sigacts;
3044         mtx_assert(&ps->ps_mtx, MA_OWNED);
3045         PROC_LOCK_ASSERT(p, MA_OWNED);
3046
3047         /*
3048          * We should allow pending but ignored signals below
3049          * if there is sigwait() active, or P_TRACED was
3050          * on when they were posted.
3051          */
3052         if (SIGISMEMBER(ps->ps_sigignore, sig) &&
3053             (p->p_flag & P_TRACED) == 0 &&
3054             (td->td_flags & TDF_SIGWAIT) == 0) {
3055                 return (SIGSTATUS_IGNORE);
3056         }
3057
3058         /*
3059          * If the process is going to single-thread mode to prepare
3060          * for exit, there is no sense in delivering any signal
3061          * to usermode.  Another important consequence is that
3062          * msleep(..., PCATCH, ...) now is only interruptible by a
3063          * suspend request.
3064          */
3065         if ((p->p_flag2 & P2_WEXIT) != 0)
3066                 return (SIGSTATUS_IGNORE);
3067
3068         if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED) {
3069                 /*
3070                  * If traced, always stop.
3071                  * Remove old signal from queue before the stop.
3072                  * XXX shrug off debugger, it causes siginfo to
3073                  * be thrown away.
3074                  */
3075                 queue = &td->td_sigqueue;
3076                 ksiginfo_init(&ksi);
3077                 if (sigqueue_get(queue, sig, &ksi) == 0) {
3078                         queue = &p->p_sigqueue;
3079                         sigqueue_get(queue, sig, &ksi);
3080                 }
3081                 td->td_si = ksi.ksi_info;
3082
3083                 mtx_unlock(&ps->ps_mtx);
3084                 sig = ptracestop(td, sig, &ksi);
3085                 mtx_lock(&ps->ps_mtx);
3086
3087                 td->td_si.si_signo = 0;
3088
3089                 /*
3090                  * Keep looking if the debugger discarded or
3091                  * replaced the signal.
3092                  */
3093                 if (sig == 0)
3094                         return (SIGSTATUS_HANDLED);
3095
3096                 /*
3097                  * If the signal became masked, re-queue it.
3098                  */
3099                 if (SIGISMEMBER(td->td_sigmask, sig)) {
3100                         ksi.ksi_flags |= KSI_HEAD;
3101                         sigqueue_add(&p->p_sigqueue, sig, &ksi);
3102                         return (SIGSTATUS_HANDLED);
3103                 }
3104
3105                 /*
3106                  * If the traced bit got turned off, requeue the signal and
3107                  * reload the set of pending signals.  This ensures that p_sig*
3108                  * and p_sigact are consistent.
3109                  */
3110                 if ((p->p_flag & P_TRACED) == 0) {
3111                         if ((ksi.ksi_flags & KSI_PTRACE) == 0) {
3112                                 ksi.ksi_flags |= KSI_HEAD;
3113                                 sigqueue_add(queue, sig, &ksi);
3114                         }
3115                         return (SIGSTATUS_HANDLED);
3116                 }
3117         }
3118
3119         /*
3120          * Decide whether the signal should be returned.
3121          * Return the signal's number, or fall through
3122          * to clear it from the pending mask.
3123          */
3124         switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
3125         case (intptr_t)SIG_DFL:
3126                 /*
3127                  * Don't take default actions on system processes.
3128                  */
3129                 if (p->p_pid <= 1) {
3130 #ifdef DIAGNOSTIC
3131                         /*
3132                          * Are you sure you want to ignore SIGSEGV
3133                          * in init? XXX
3134                          */
3135                         printf("Process (pid %lu) got signal %d\n",
3136                                 (u_long)p->p_pid, sig);
3137 #endif
3138                         return (SIGSTATUS_IGNORE);
3139                 }
3140
3141                 /*
3142                  * If there is a pending stop signal to process with
3143                  * default action, stop here, then clear the signal.
3144                  * Traced or exiting processes should ignore stops.
3145                  * Additionally, a member of an orphaned process group
3146                  * should ignore tty stops.
3147                  */
3148                 prop = sigprop(sig);
3149                 if (prop & SIGPROP_STOP) {
3150                         mtx_unlock(&ps->ps_mtx);
3151                         if ((p->p_flag & (P_TRACED | P_WEXIT |
3152                             P_SINGLE_EXIT)) != 0 || ((p->p_pgrp->
3153                             pg_flags & PGRP_ORPHANED) != 0 &&
3154                             (prop & SIGPROP_TTYSTOP) != 0)) {
3155                                 mtx_lock(&ps->ps_mtx);
3156                                 return (SIGSTATUS_IGNORE);
3157                         }
3158                         if (TD_SBDRY_INTR(td)) {
3159                                 KASSERT((td->td_flags & TDF_SBDRY) != 0,
3160                                     ("lost TDF_SBDRY"));
3161                                 mtx_lock(&ps->ps_mtx);
3162                                 return (SIGSTATUS_SBDRY_STOP);
3163                         }
3164                         WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
3165                             &p->p_mtx.lock_object, "Catching SIGSTOP");
3166                         sigqueue_delete(&td->td_sigqueue, sig);
3167                         sigqueue_delete(&p->p_sigqueue, sig);
3168                         p->p_flag |= P_STOPPED_SIG;
3169                         p->p_xsig = sig;
3170                         PROC_SLOCK(p);
3171                         sig_suspend_threads(td, p, 0);
3172                         thread_suspend_switch(td, p);
3173                         PROC_SUNLOCK(p);
3174                         mtx_lock(&ps->ps_mtx);
3175                         return (SIGSTATUS_HANDLED);
3176                 } else if ((prop & SIGPROP_IGNORE) != 0 &&
3177                     (td->td_flags & TDF_SIGWAIT) == 0) {
3178                         /*
3179                          * Default action is to ignore; drop it if
3180                          * not in kern_sigtimedwait().
3181                          */
3182                         return (SIGSTATUS_IGNORE);
3183                 } else {
3184                         return (SIGSTATUS_HANDLE);
3185                 }
3186
3187         case (intptr_t)SIG_IGN:
3188                 if ((td->td_flags & TDF_SIGWAIT) == 0)
3189                         return (SIGSTATUS_IGNORE);
3190                 else
3191                         return (SIGSTATUS_HANDLE);
3192
3193         default:
3194                 /*
3195                  * This signal has an action, let postsig() process it.
3196                  */
3197                 return (SIGSTATUS_HANDLE);
3198         }
3199 }
3200
3201 /*
3202  * If the current process has received a signal (should be caught or cause
3203  * termination, should interrupt current syscall), return the signal number.
3204  * Stop signals with default action are processed immediately, then cleared;
3205  * they aren't returned.  This is checked after each entry to the system for
3206  * a syscall or trap (though this can usually be done without calling
3207  * issignal by checking the pending signal masks in cursig.) The normal call
3208  * sequence is
3209  *
3210  *      while (sig = cursig(curthread))
3211  *              postsig(sig);
3212  */
3213 static int
3214 issignal(struct thread *td)
3215 {
3216         struct proc *p;
3217         sigset_t sigpending;
3218         int sig;
3219
3220         p = td->td_proc;
3221         PROC_LOCK_ASSERT(p, MA_OWNED);
3222
3223         for (;;) {
3224                 sigpending = td->td_sigqueue.sq_signals;
3225                 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
3226                 SIGSETNAND(sigpending, td->td_sigmask);
3227
3228                 if ((p->p_flag & P_PPWAIT) != 0 || (td->td_flags &
3229                     (TDF_SBDRY | TDF_SERESTART | TDF_SEINTR)) == TDF_SBDRY)
3230                         SIG_STOPSIGMASK(sigpending);
3231                 if (SIGISEMPTY(sigpending))     /* no signal to send */
3232                         return (0);
3233
3234                 /*
3235                  * Do fast sigblock if requested by usermode.  Since
3236                  * we do know that there was a signal pending at this
3237                  * point, set the FAST_SIGBLOCK_PEND as indicator for
3238                  * usermode to perform a dummy call to
3239                  * FAST_SIGBLOCK_UNBLOCK, which causes immediate
3240                  * delivery of postponed pending signal.
3241                  */
3242                 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
3243                         if (td->td_sigblock_val != 0)
3244                                 SIGSETNAND(sigpending, fastblock_mask);
3245                         if (SIGISEMPTY(sigpending)) {
3246                                 td->td_pflags |= TDP_SIGFASTPENDING;
3247                                 return (0);
3248                         }
3249                 }
3250
3251                 if ((p->p_flag & (P_TRACED | P_PPTRACE)) == P_TRACED &&
3252                     (p->p_flag2 & P2_PTRACE_FSTP) != 0 &&
3253                     SIGISMEMBER(sigpending, SIGSTOP)) {
3254                         /*
3255                          * If debugger just attached, always consume
3256                          * SIGSTOP from ptrace(PT_ATTACH) first, to
3257                          * execute the debugger attach ritual in
3258                          * order.
3259                          */
3260                         td->td_dbgflags |= TDB_FSTP;
3261                         SIGEMPTYSET(sigpending);
3262                         SIGADDSET(sigpending, SIGSTOP);
3263                 }
3264
3265                 SIG_FOREACH(sig, &sigpending) {
3266                         switch (sigprocess(td, sig)) {
3267                         case SIGSTATUS_HANDLE:
3268                                 return (sig);
3269                         case SIGSTATUS_HANDLED:
3270                                 goto next;
3271                         case SIGSTATUS_IGNORE:
3272                                 sigqueue_delete(&td->td_sigqueue, sig);
3273                                 sigqueue_delete(&p->p_sigqueue, sig);
3274                                 break;
3275                         case SIGSTATUS_SBDRY_STOP:
3276                                 return (-1);
3277                         }
3278                 }
3279 next:;
3280         }
3281 }
3282
3283 void
3284 thread_stopped(struct proc *p)
3285 {
3286         int n;
3287
3288         PROC_LOCK_ASSERT(p, MA_OWNED);
3289         PROC_SLOCK_ASSERT(p, MA_OWNED);
3290         n = p->p_suspcount;
3291         if (p == curproc)
3292                 n++;
3293         if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
3294                 PROC_SUNLOCK(p);
3295                 p->p_flag &= ~P_WAITED;
3296                 PROC_LOCK(p->p_pptr);
3297                 childproc_stopped(p, (p->p_flag & P_TRACED) ?
3298                         CLD_TRAPPED : CLD_STOPPED);
3299                 PROC_UNLOCK(p->p_pptr);
3300                 PROC_SLOCK(p);
3301         }
3302 }
3303
3304 /*
3305  * Take the action for the specified signal
3306  * from the current set of pending signals.
3307  */
3308 int
3309 postsig(int sig)
3310 {
3311         struct thread *td;
3312         struct proc *p;
3313         struct sigacts *ps;
3314         sig_t action;
3315         ksiginfo_t ksi;
3316         sigset_t returnmask;
3317
3318         KASSERT(sig != 0, ("postsig"));
3319
3320         td = curthread;
3321         p = td->td_proc;
3322         PROC_LOCK_ASSERT(p, MA_OWNED);
3323         ps = p->p_sigacts;
3324         mtx_assert(&ps->ps_mtx, MA_OWNED);
3325         ksiginfo_init(&ksi);
3326         if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
3327             sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
3328                 return (0);
3329         ksi.ksi_signo = sig;
3330         if (ksi.ksi_code == SI_TIMER)
3331                 itimer_accept(p, ksi.ksi_timerid, &ksi);
3332         action = ps->ps_sigact[_SIG_IDX(sig)];
3333 #ifdef KTRACE
3334         if (KTRPOINT(td, KTR_PSIG))
3335                 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
3336                     &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
3337 #endif
3338
3339         if (action == SIG_DFL) {
3340                 /*
3341                  * Default action, where the default is to kill
3342                  * the process.  (Other cases were ignored above.)
3343                  */
3344                 mtx_unlock(&ps->ps_mtx);
3345                 proc_td_siginfo_capture(td, &ksi.ksi_info);
3346                 sigexit(td, sig);
3347                 /* NOTREACHED */
3348         } else {
3349                 /*
3350                  * If we get here, the signal must be caught.
3351                  */
3352                 KASSERT(action != SIG_IGN, ("postsig action %p", action));
3353                 KASSERT(!SIGISMEMBER(td->td_sigmask, sig),
3354                     ("postsig action: blocked sig %d", sig));
3355
3356                 /*
3357                  * Set the new mask value and also defer further
3358                  * occurrences of this signal.
3359                  *
3360                  * Special case: user has done a sigsuspend.  Here the
3361                  * current mask is not of interest, but rather the
3362                  * mask from before the sigsuspend is what we want
3363                  * restored after the signal processing is completed.
3364                  */
3365                 if (td->td_pflags & TDP_OLDMASK) {
3366                         returnmask = td->td_oldsigmask;
3367                         td->td_pflags &= ~TDP_OLDMASK;
3368                 } else
3369                         returnmask = td->td_sigmask;
3370
3371                 if (p->p_sig == sig) {
3372                         p->p_sig = 0;
3373                 }
3374                 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
3375                 postsig_done(sig, td, ps);
3376         }
3377         return (1);
3378 }
3379
3380 int
3381 sig_ast_checksusp(struct thread *td)
3382 {
3383         struct proc *p __diagused;
3384         int ret;
3385
3386         p = td->td_proc;
3387         PROC_LOCK_ASSERT(p, MA_OWNED);
3388
3389         if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
3390                 return (0);
3391
3392         ret = thread_suspend_check(1);
3393         MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
3394         return (ret);
3395 }
3396
3397 int
3398 sig_ast_needsigchk(struct thread *td)
3399 {
3400         struct proc *p;
3401         struct sigacts *ps;
3402         int ret, sig;
3403
3404         p = td->td_proc;
3405         PROC_LOCK_ASSERT(p, MA_OWNED);
3406
3407         if ((td->td_flags & TDF_NEEDSIGCHK) == 0)
3408                 return (0);
3409
3410         ps = p->p_sigacts;
3411         mtx_lock(&ps->ps_mtx);
3412         sig = cursig(td);
3413         if (sig == -1) {
3414                 mtx_unlock(&ps->ps_mtx);
3415                 KASSERT((td->td_flags & TDF_SBDRY) != 0, ("lost TDF_SBDRY"));
3416                 KASSERT(TD_SBDRY_INTR(td),
3417                     ("lost TDF_SERESTART of TDF_SEINTR"));
3418                 KASSERT((td->td_flags & (TDF_SEINTR | TDF_SERESTART)) !=
3419                     (TDF_SEINTR | TDF_SERESTART),
3420                     ("both TDF_SEINTR and TDF_SERESTART"));
3421                 ret = TD_SBDRY_ERRNO(td);
3422         } else if (sig != 0) {
3423                 ret = SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR : ERESTART;
3424                 mtx_unlock(&ps->ps_mtx);
3425         } else {
3426                 mtx_unlock(&ps->ps_mtx);
3427                 ret = 0;
3428         }
3429
3430         /*
3431          * Do not go into sleep if this thread was the ptrace(2)
3432          * attach leader.  cursig() consumed SIGSTOP from PT_ATTACH,
3433          * but we usually act on the signal by interrupting sleep, and
3434          * should do that here as well.
3435          */
3436         if ((td->td_dbgflags & TDB_FSTP) != 0) {
3437                 if (ret == 0)
3438                         ret = EINTR;
3439                 td->td_dbgflags &= ~TDB_FSTP;
3440         }
3441
3442         return (ret);
3443 }
3444
3445 int
3446 sig_intr(void)
3447 {
3448         struct thread *td;
3449         struct proc *p;
3450         int ret;
3451
3452         td = curthread;
3453         if ((td->td_flags & (TDF_NEEDSIGCHK | TDF_NEEDSUSPCHK)) == 0)
3454                 return (0);
3455
3456         p = td->td_proc;
3457
3458         PROC_LOCK(p);
3459         ret = sig_ast_checksusp(td);
3460         if (ret == 0)
3461                 ret = sig_ast_needsigchk(td);
3462         PROC_UNLOCK(p);
3463         return (ret);
3464 }
3465
3466 bool
3467 curproc_sigkilled(void)
3468 {
3469         struct thread *td;
3470         struct proc *p;
3471         struct sigacts *ps;
3472         bool res;
3473
3474         td = curthread;
3475         if ((td->td_flags & TDF_NEEDSIGCHK) == 0)
3476                 return (false);
3477
3478         p = td->td_proc;
3479         PROC_LOCK(p);
3480         ps = p->p_sigacts;
3481         mtx_lock(&ps->ps_mtx);
3482         res = SIGISMEMBER(td->td_sigqueue.sq_signals, SIGKILL) ||
3483             SIGISMEMBER(p->p_sigqueue.sq_signals, SIGKILL);
3484         mtx_unlock(&ps->ps_mtx);
3485         PROC_UNLOCK(p);
3486         return (res);
3487 }
3488
3489 void
3490 proc_wkilled(struct proc *p)
3491 {
3492
3493         PROC_LOCK_ASSERT(p, MA_OWNED);
3494         if ((p->p_flag & P_WKILLED) == 0) {
3495                 p->p_flag |= P_WKILLED;
3496                 /*
3497                  * Notify swapper that there is a process to swap in.
3498                  * The notification is racy, at worst it would take 10
3499                  * seconds for the swapper process to notice.
3500                  */
3501                 if ((p->p_flag & (P_INMEM | P_SWAPPINGIN)) == 0)
3502                         wakeup(&proc0);
3503         }
3504 }
3505
3506 /*
3507  * Kill the current process for stated reason.
3508  */
3509 void
3510 killproc(struct proc *p, const char *why)
3511 {
3512
3513         PROC_LOCK_ASSERT(p, MA_OWNED);
3514         CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
3515             p->p_comm);
3516         log(LOG_ERR, "pid %d (%s), jid %d, uid %d, was killed: %s\n",
3517             p->p_pid, p->p_comm, p->p_ucred->cr_prison->pr_id,
3518             p->p_ucred->cr_uid, why);
3519         proc_wkilled(p);
3520         kern_psignal(p, SIGKILL);
3521 }
3522
3523 /*
3524  * Force the current process to exit with the specified signal, dumping core
3525  * if appropriate.  We bypass the normal tests for masked and caught signals,
3526  * allowing unrecoverable failures to terminate the process without changing
3527  * signal state.  Mark the accounting record with the signal termination.
3528  * If dumping core, save the signal number for the debugger.  Calls exit and
3529  * does not return.
3530  */
3531 void
3532 sigexit(struct thread *td, int sig)
3533 {
3534         struct proc *p = td->td_proc;
3535
3536         PROC_LOCK_ASSERT(p, MA_OWNED);
3537         proc_set_p2_wexit(p);
3538
3539         p->p_acflag |= AXSIG;
3540         /*
3541          * We must be single-threading to generate a core dump.  This
3542          * ensures that the registers in the core file are up-to-date.
3543          * Also, the ELF dump handler assumes that the thread list doesn't
3544          * change out from under it.
3545          *
3546          * XXX If another thread attempts to single-thread before us
3547          *     (e.g. via fork()), we won't get a dump at all.
3548          */
3549         if ((sigprop(sig) & SIGPROP_CORE) &&
3550             thread_single(p, SINGLE_NO_EXIT) == 0) {
3551                 p->p_sig = sig;
3552                 /*
3553                  * Log signals which would cause core dumps
3554                  * (Log as LOG_INFO to appease those who don't want
3555                  * these messages.)
3556                  * XXX : Todo, as well as euid, write out ruid too
3557                  * Note that coredump() drops proc lock.
3558                  */
3559                 if (coredump(td) == 0)
3560                         sig |= WCOREFLAG;
3561                 if (kern_logsigexit)
3562                         log(LOG_INFO,
3563                             "pid %d (%s), jid %d, uid %d: exited on "
3564                             "signal %d%s\n", p->p_pid, p->p_comm,
3565                             p->p_ucred->cr_prison->pr_id,
3566                             td->td_ucred->cr_uid,
3567                             sig &~ WCOREFLAG,
3568                             sig & WCOREFLAG ? " (core dumped)" : "");
3569         } else
3570                 PROC_UNLOCK(p);
3571         exit1(td, 0, sig);
3572         /* NOTREACHED */
3573 }
3574
3575 /*
3576  * Send queued SIGCHLD to parent when child process's state
3577  * is changed.
3578  */
3579 static void
3580 sigparent(struct proc *p, int reason, int status)
3581 {
3582         PROC_LOCK_ASSERT(p, MA_OWNED);
3583         PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3584
3585         if (p->p_ksi != NULL) {
3586                 p->p_ksi->ksi_signo  = SIGCHLD;
3587                 p->p_ksi->ksi_code   = reason;
3588                 p->p_ksi->ksi_status = status;
3589                 p->p_ksi->ksi_pid    = p->p_pid;
3590                 p->p_ksi->ksi_uid    = p->p_ucred->cr_ruid;
3591                 if (KSI_ONQ(p->p_ksi))
3592                         return;
3593         }
3594         pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3595 }
3596
3597 static void
3598 childproc_jobstate(struct proc *p, int reason, int sig)
3599 {
3600         struct sigacts *ps;
3601
3602         PROC_LOCK_ASSERT(p, MA_OWNED);
3603         PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3604
3605         /*
3606          * Wake up parent sleeping in kern_wait(), also send
3607          * SIGCHLD to parent, but SIGCHLD does not guarantee
3608          * that parent will awake, because parent may masked
3609          * the signal.
3610          */
3611         p->p_pptr->p_flag |= P_STATCHILD;
3612         wakeup(p->p_pptr);
3613
3614         ps = p->p_pptr->p_sigacts;
3615         mtx_lock(&ps->ps_mtx);
3616         if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
3617                 mtx_unlock(&ps->ps_mtx);
3618                 sigparent(p, reason, sig);
3619         } else
3620                 mtx_unlock(&ps->ps_mtx);
3621 }
3622
3623 void
3624 childproc_stopped(struct proc *p, int reason)
3625 {
3626
3627         childproc_jobstate(p, reason, p->p_xsig);
3628 }
3629
3630 void
3631 childproc_continued(struct proc *p)
3632 {
3633         childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3634 }
3635
3636 void
3637 childproc_exited(struct proc *p)
3638 {
3639         int reason, status;
3640
3641         if (WCOREDUMP(p->p_xsig)) {
3642                 reason = CLD_DUMPED;
3643                 status = WTERMSIG(p->p_xsig);
3644         } else if (WIFSIGNALED(p->p_xsig)) {
3645                 reason = CLD_KILLED;
3646                 status = WTERMSIG(p->p_xsig);
3647         } else {
3648                 reason = CLD_EXITED;
3649                 status = p->p_xexit;
3650         }
3651         /*
3652          * XXX avoid calling wakeup(p->p_pptr), the work is
3653          * done in exit1().
3654          */
3655         sigparent(p, reason, status);
3656 }
3657
3658 #define MAX_NUM_CORE_FILES 100000
3659 #ifndef NUM_CORE_FILES
3660 #define NUM_CORE_FILES 5
3661 #endif
3662 CTASSERT(NUM_CORE_FILES >= 0 && NUM_CORE_FILES <= MAX_NUM_CORE_FILES);
3663 static int num_cores = NUM_CORE_FILES;
3664
3665 static int
3666 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3667 {
3668         int error;
3669         int new_val;
3670
3671         new_val = num_cores;
3672         error = sysctl_handle_int(oidp, &new_val, 0, req);
3673         if (error != 0 || req->newptr == NULL)
3674                 return (error);
3675         if (new_val > MAX_NUM_CORE_FILES)
3676                 new_val = MAX_NUM_CORE_FILES;
3677         if (new_val < 0)
3678                 new_val = 0;
3679         num_cores = new_val;
3680         return (0);
3681 }
3682 SYSCTL_PROC(_debug, OID_AUTO, ncores,
3683     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, sizeof(int),
3684     sysctl_debug_num_cores_check, "I",
3685     "Maximum number of generated process corefiles while using index format");
3686
3687 #define GZIP_SUFFIX     ".gz"
3688 #define ZSTD_SUFFIX     ".zst"
3689
3690 int compress_user_cores = 0;
3691
3692 static int
3693 sysctl_compress_user_cores(SYSCTL_HANDLER_ARGS)
3694 {
3695         int error, val;
3696
3697         val = compress_user_cores;
3698         error = sysctl_handle_int(oidp, &val, 0, req);
3699         if (error != 0 || req->newptr == NULL)
3700                 return (error);
3701         if (val != 0 && !compressor_avail(val))
3702                 return (EINVAL);
3703         compress_user_cores = val;
3704         return (error);
3705 }
3706 SYSCTL_PROC(_kern, OID_AUTO, compress_user_cores,
3707     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
3708     sysctl_compress_user_cores, "I",
3709     "Enable compression of user corefiles ("
3710     __XSTRING(COMPRESS_GZIP) " = gzip, "
3711     __XSTRING(COMPRESS_ZSTD) " = zstd)");
3712
3713 int compress_user_cores_level = 6;
3714 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_level, CTLFLAG_RWTUN,
3715     &compress_user_cores_level, 0,
3716     "Corefile compression level");
3717
3718 /*
3719  * Protect the access to corefilename[] by allproc_lock.
3720  */
3721 #define corefilename_lock       allproc_lock
3722
3723 static char corefilename[MAXPATHLEN] = {"%N.core"};
3724 TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
3725
3726 static int
3727 sysctl_kern_corefile(SYSCTL_HANDLER_ARGS)
3728 {
3729         int error;
3730
3731         sx_xlock(&corefilename_lock);
3732         error = sysctl_handle_string(oidp, corefilename, sizeof(corefilename),
3733             req);
3734         sx_xunlock(&corefilename_lock);
3735
3736         return (error);
3737 }
3738 SYSCTL_PROC(_kern, OID_AUTO, corefile, CTLTYPE_STRING | CTLFLAG_RW |
3739     CTLFLAG_MPSAFE, 0, 0, sysctl_kern_corefile, "A",
3740     "Process corefile name format string");
3741
3742 static void
3743 vnode_close_locked(struct thread *td, struct vnode *vp)
3744 {
3745
3746         VOP_UNLOCK(vp);
3747         vn_close(vp, FWRITE, td->td_ucred, td);
3748 }
3749
3750 /*
3751  * If the core format has a %I in it, then we need to check
3752  * for existing corefiles before defining a name.
3753  * To do this we iterate over 0..ncores to find a
3754  * non-existing core file name to use. If all core files are
3755  * already used we choose the oldest one.
3756  */
3757 static int
3758 corefile_open_last(struct thread *td, char *name, int indexpos,
3759     int indexlen, int ncores, struct vnode **vpp)
3760 {
3761         struct vnode *oldvp, *nextvp, *vp;
3762         struct vattr vattr;
3763         struct nameidata nd;
3764         int error, i, flags, oflags, cmode;
3765         char ch;
3766         struct timespec lasttime;
3767
3768         nextvp = oldvp = NULL;
3769         cmode = S_IRUSR | S_IWUSR;
3770         oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3771             (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3772
3773         for (i = 0; i < ncores; i++) {
3774                 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3775
3776                 ch = name[indexpos + indexlen];
3777                 (void)snprintf(name + indexpos, indexlen + 1, "%.*u", indexlen,
3778                     i);
3779                 name[indexpos + indexlen] = ch;
3780
3781                 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3782                 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
3783                     NULL);
3784                 if (error != 0)
3785                         break;
3786
3787                 vp = nd.ni_vp;
3788                 NDFREE(&nd, NDF_ONLY_PNBUF);
3789                 if ((flags & O_CREAT) == O_CREAT) {
3790                         nextvp = vp;
3791                         break;
3792                 }
3793
3794                 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3795                 if (error != 0) {
3796                         vnode_close_locked(td, vp);
3797                         break;
3798                 }
3799
3800                 if (oldvp == NULL ||
3801                     lasttime.tv_sec > vattr.va_mtime.tv_sec ||
3802                     (lasttime.tv_sec == vattr.va_mtime.tv_sec &&
3803                     lasttime.tv_nsec >= vattr.va_mtime.tv_nsec)) {
3804                         if (oldvp != NULL)
3805                                 vn_close(oldvp, FWRITE, td->td_ucred, td);
3806                         oldvp = vp;
3807                         VOP_UNLOCK(oldvp);
3808                         lasttime = vattr.va_mtime;
3809                 } else {
3810                         vnode_close_locked(td, vp);
3811                 }
3812         }
3813
3814         if (oldvp != NULL) {
3815                 if (nextvp == NULL) {
3816                         if ((td->td_proc->p_flag & P_SUGID) != 0) {
3817                                 error = EFAULT;
3818                                 vn_close(oldvp, FWRITE, td->td_ucred, td);
3819                         } else {
3820                                 nextvp = oldvp;
3821                                 error = vn_lock(nextvp, LK_EXCLUSIVE);
3822                                 if (error != 0) {
3823                                         vn_close(nextvp, FWRITE, td->td_ucred,
3824                                             td);
3825                                         nextvp = NULL;
3826                                 }
3827                         }
3828                 } else {
3829                         vn_close(oldvp, FWRITE, td->td_ucred, td);
3830                 }
3831         }
3832         if (error != 0) {
3833                 if (nextvp != NULL)
3834                         vnode_close_locked(td, oldvp);
3835         } else {
3836                 *vpp = nextvp;
3837         }
3838
3839         return (error);
3840 }
3841
3842 /*
3843  * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3844  * Expand the name described in corefilename, using name, uid, and pid
3845  * and open/create core file.
3846  * corefilename is a printf-like string, with three format specifiers:
3847  *      %N      name of process ("name")
3848  *      %P      process id (pid)
3849  *      %U      user id (uid)
3850  * For example, "%N.core" is the default; they can be disabled completely
3851  * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3852  * This is controlled by the sysctl variable kern.corefile (see above).
3853  */
3854 static int
3855 corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
3856     int compress, int signum, struct vnode **vpp, char **namep)
3857 {
3858         struct sbuf sb;
3859         struct nameidata nd;
3860         const char *format;
3861         char *hostname, *name;
3862         int cmode, error, flags, i, indexpos, indexlen, oflags, ncores;
3863
3864         hostname = NULL;
3865         format = corefilename;
3866         name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3867         indexlen = 0;
3868         indexpos = -1;
3869         ncores = num_cores;
3870         (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
3871         sx_slock(&corefilename_lock);
3872         for (i = 0; format[i] != '\0'; i++) {
3873                 switch (format[i]) {
3874                 case '%':       /* Format character */
3875                         i++;
3876                         switch (format[i]) {
3877                         case '%':
3878                                 sbuf_putc(&sb, '%');
3879                                 break;
3880                         case 'H':       /* hostname */
3881                                 if (hostname == NULL) {
3882                                         hostname = malloc(MAXHOSTNAMELEN,
3883                                             M_TEMP, M_WAITOK);
3884                                 }
3885                                 getcredhostname(td->td_ucred, hostname,
3886                                     MAXHOSTNAMELEN);
3887                                 sbuf_printf(&sb, "%s", hostname);
3888                                 break;
3889                         case 'I':       /* autoincrementing index */
3890                                 if (indexpos != -1) {
3891                                         sbuf_printf(&sb, "%%I");
3892                                         break;
3893                                 }
3894
3895                                 indexpos = sbuf_len(&sb);
3896                                 sbuf_printf(&sb, "%u", ncores - 1);
3897                                 indexlen = sbuf_len(&sb) - indexpos;
3898                                 break;
3899                         case 'N':       /* process name */
3900                                 sbuf_printf(&sb, "%s", comm);
3901                                 break;
3902                         case 'P':       /* process id */
3903                                 sbuf_printf(&sb, "%u", pid);
3904                                 break;
3905                         case 'S':       /* signal number */
3906                                 sbuf_printf(&sb, "%i", signum);
3907                                 break;
3908                         case 'U':       /* user id */
3909                                 sbuf_printf(&sb, "%u", uid);
3910                                 break;
3911                         default:
3912                                 log(LOG_ERR,
3913                                     "Unknown format character %c in "
3914                                     "corename `%s'\n", format[i], format);
3915                                 break;
3916                         }
3917                         break;
3918                 default:
3919                         sbuf_putc(&sb, format[i]);
3920                         break;
3921                 }
3922         }
3923         sx_sunlock(&corefilename_lock);
3924         free(hostname, M_TEMP);
3925         if (compress == COMPRESS_GZIP)
3926                 sbuf_printf(&sb, GZIP_SUFFIX);
3927         else if (compress == COMPRESS_ZSTD)
3928                 sbuf_printf(&sb, ZSTD_SUFFIX);
3929         if (sbuf_error(&sb) != 0) {
3930                 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3931                     "long\n", (long)pid, comm, (u_long)uid);
3932                 sbuf_delete(&sb);
3933                 free(name, M_TEMP);
3934                 return (ENOMEM);
3935         }
3936         sbuf_finish(&sb);
3937         sbuf_delete(&sb);
3938
3939         if (indexpos != -1) {
3940                 error = corefile_open_last(td, name, indexpos, indexlen, ncores,
3941                     vpp);
3942                 if (error != 0) {
3943                         log(LOG_ERR,
3944                             "pid %d (%s), uid (%u):  Path `%s' failed "
3945                             "on initial open test, error = %d\n",
3946                             pid, comm, uid, name, error);
3947                 }
3948         } else {
3949                 cmode = S_IRUSR | S_IWUSR;
3950                 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3951                     (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3952                 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3953                 if ((td->td_proc->p_flag & P_SUGID) != 0)
3954                         flags |= O_EXCL;
3955
3956                 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3957                 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred,
3958                     NULL);
3959                 if (error == 0) {
3960                         *vpp = nd.ni_vp;
3961                         NDFREE(&nd, NDF_ONLY_PNBUF);
3962                 }
3963         }
3964
3965         if (error != 0) {
3966 #ifdef AUDIT
3967                 audit_proc_coredump(td, name, error);
3968 #endif
3969                 free(name, M_TEMP);
3970                 return (error);
3971         }
3972         *namep = name;
3973         return (0);
3974 }
3975
3976 /*
3977  * Dump a process' core.  The main routine does some
3978  * policy checking, and creates the name of the coredump;
3979  * then it passes on a vnode and a size limit to the process-specific
3980  * coredump routine if there is one; if there _is not_ one, it returns
3981  * ENOSYS; otherwise it returns the error from the process-specific routine.
3982  */
3983
3984 static int
3985 coredump(struct thread *td)
3986 {
3987         struct proc *p = td->td_proc;
3988         struct ucred *cred = td->td_ucred;
3989         struct vnode *vp;
3990         struct flock lf;
3991         struct vattr vattr;
3992         size_t fullpathsize;
3993         int error, error1, locked;
3994         char *name;                     /* name of corefile */
3995         void *rl_cookie;
3996         off_t limit;
3997         char *fullpath, *freepath = NULL;
3998         struct sbuf *sb;
3999
4000         PROC_LOCK_ASSERT(p, MA_OWNED);
4001         MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
4002
4003         if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) ||
4004             (p->p_flag2 & P2_NOTRACE) != 0) {
4005                 PROC_UNLOCK(p);
4006                 return (EFAULT);
4007         }
4008
4009         /*
4010          * Note that the bulk of limit checking is done after
4011          * the corefile is created.  The exception is if the limit
4012          * for corefiles is 0, in which case we don't bother
4013          * creating the corefile at all.  This layout means that
4014          * a corefile is truncated instead of not being created,
4015          * if it is larger than the limit.
4016          */
4017         limit = (off_t)lim_cur(td, RLIMIT_CORE);
4018         if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
4019                 PROC_UNLOCK(p);
4020                 return (EFBIG);
4021         }
4022         PROC_UNLOCK(p);
4023
4024         error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td,
4025             compress_user_cores, p->p_sig, &vp, &name);
4026         if (error != 0)
4027                 return (error);
4028
4029         /*
4030          * Don't dump to non-regular files or files with links.
4031          * Do not dump into system files. Effective user must own the corefile.
4032          */
4033         if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
4034             vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 ||
4035             vattr.va_uid != cred->cr_uid) {
4036                 VOP_UNLOCK(vp);
4037                 error = EFAULT;
4038                 goto out;
4039         }
4040
4041         VOP_UNLOCK(vp);
4042
4043         /* Postpone other writers, including core dumps of other processes. */
4044         rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
4045
4046         lf.l_whence = SEEK_SET;
4047         lf.l_start = 0;
4048         lf.l_len = 0;
4049         lf.l_type = F_WRLCK;
4050         locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
4051
4052         VATTR_NULL(&vattr);
4053         vattr.va_size = 0;
4054         if (set_core_nodump_flag)
4055                 vattr.va_flags = UF_NODUMP;
4056         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4057         VOP_SETATTR(vp, &vattr, cred);
4058         VOP_UNLOCK(vp);
4059         PROC_LOCK(p);
4060         p->p_acflag |= ACORE;
4061         PROC_UNLOCK(p);
4062
4063         if (p->p_sysent->sv_coredump != NULL) {
4064                 error = p->p_sysent->sv_coredump(td, vp, limit, 0);
4065         } else {
4066                 error = ENOSYS;
4067         }
4068
4069         if (locked) {
4070                 lf.l_type = F_UNLCK;
4071                 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
4072         }
4073         vn_rangelock_unlock(vp, rl_cookie);
4074
4075         /*
4076          * Notify the userland helper that a process triggered a core dump.
4077          * This allows the helper to run an automated debugging session.
4078          */
4079         if (error != 0 || coredump_devctl == 0)
4080                 goto out;
4081         sb = sbuf_new_auto();
4082         if (vn_fullpath_global(p->p_textvp, &fullpath, &freepath) != 0)
4083                 goto out2;
4084         sbuf_printf(sb, "comm=\"");
4085         devctl_safe_quote_sb(sb, fullpath);
4086         free(freepath, M_TEMP);
4087         sbuf_printf(sb, "\" core=\"");
4088
4089         /*
4090          * We can't lookup core file vp directly. When we're replacing a core, and
4091          * other random times, we flush the name cache, so it will fail. Instead,
4092          * if the path of the core is relative, add the current dir in front if it.
4093          */
4094         if (name[0] != '/') {
4095                 fullpathsize = MAXPATHLEN;
4096                 freepath = malloc(fullpathsize, M_TEMP, M_WAITOK);
4097                 if (vn_getcwd(freepath, &fullpath, &fullpathsize) != 0) {
4098                         free(freepath, M_TEMP);
4099                         goto out2;
4100                 }
4101                 devctl_safe_quote_sb(sb, fullpath);
4102                 free(freepath, M_TEMP);
4103                 sbuf_putc(sb, '/');
4104         }
4105         devctl_safe_quote_sb(sb, name);
4106         sbuf_printf(sb, "\"");
4107         if (sbuf_finish(sb) == 0)
4108                 devctl_notify("kernel", "signal", "coredump", sbuf_data(sb));
4109 out2:
4110         sbuf_delete(sb);
4111 out:
4112         error1 = vn_close(vp, FWRITE, cred, td);
4113         if (error == 0)
4114                 error = error1;
4115 #ifdef AUDIT
4116         audit_proc_coredump(td, name, error);
4117 #endif
4118         free(name, M_TEMP);
4119         return (error);
4120 }
4121
4122 /*
4123  * Nonexistent system call-- signal process (may want to handle it).  Flag
4124  * error in case process won't see signal immediately (blocked or ignored).
4125  */
4126 #ifndef _SYS_SYSPROTO_H_
4127 struct nosys_args {
4128         int     dummy;
4129 };
4130 #endif
4131 /* ARGSUSED */
4132 int
4133 nosys(struct thread *td, struct nosys_args *args)
4134 {
4135         struct proc *p;
4136
4137         p = td->td_proc;
4138
4139         PROC_LOCK(p);
4140         tdsignal(td, SIGSYS);
4141         PROC_UNLOCK(p);
4142         if (kern_lognosys == 1 || kern_lognosys == 3) {
4143                 uprintf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4144                     td->td_sa.code);
4145         }
4146         if (kern_lognosys == 2 || kern_lognosys == 3 ||
4147             (p->p_pid == 1 && (kern_lognosys & 3) == 0)) {
4148                 printf("pid %d comm %s: nosys %d\n", p->p_pid, p->p_comm,
4149                     td->td_sa.code);
4150         }
4151         return (ENOSYS);
4152 }
4153
4154 /*
4155  * Send a SIGIO or SIGURG signal to a process or process group using stored
4156  * credentials rather than those of the current process.
4157  */
4158 void
4159 pgsigio(struct sigio **sigiop, int sig, int checkctty)
4160 {
4161         ksiginfo_t ksi;
4162         struct sigio *sigio;
4163
4164         ksiginfo_init(&ksi);
4165         ksi.ksi_signo = sig;
4166         ksi.ksi_code = SI_KERNEL;
4167
4168         SIGIO_LOCK();
4169         sigio = *sigiop;
4170         if (sigio == NULL) {
4171                 SIGIO_UNLOCK();
4172                 return;
4173         }
4174         if (sigio->sio_pgid > 0) {
4175                 PROC_LOCK(sigio->sio_proc);
4176                 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
4177                         kern_psignal(sigio->sio_proc, sig);
4178                 PROC_UNLOCK(sigio->sio_proc);
4179         } else if (sigio->sio_pgid < 0) {
4180                 struct proc *p;
4181
4182                 PGRP_LOCK(sigio->sio_pgrp);
4183                 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
4184                         PROC_LOCK(p);
4185                         if (p->p_state == PRS_NORMAL &&
4186                             CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
4187                             (checkctty == 0 || (p->p_flag & P_CONTROLT)))
4188                                 kern_psignal(p, sig);
4189                         PROC_UNLOCK(p);
4190                 }
4191                 PGRP_UNLOCK(sigio->sio_pgrp);
4192         }
4193         SIGIO_UNLOCK();
4194 }
4195
4196 static int
4197 filt_sigattach(struct knote *kn)
4198 {
4199         struct proc *p = curproc;
4200
4201         kn->kn_ptr.p_proc = p;
4202         kn->kn_flags |= EV_CLEAR;               /* automatically set */
4203
4204         knlist_add(p->p_klist, kn, 0);
4205
4206         return (0);
4207 }
4208
4209 static void
4210 filt_sigdetach(struct knote *kn)
4211 {
4212         struct proc *p = kn->kn_ptr.p_proc;
4213
4214         knlist_remove(p->p_klist, kn, 0);
4215 }
4216
4217 /*
4218  * signal knotes are shared with proc knotes, so we apply a mask to
4219  * the hint in order to differentiate them from process hints.  This
4220  * could be avoided by using a signal-specific knote list, but probably
4221  * isn't worth the trouble.
4222  */
4223 static int
4224 filt_signal(struct knote *kn, long hint)
4225 {
4226
4227         if (hint & NOTE_SIGNAL) {
4228                 hint &= ~NOTE_SIGNAL;
4229
4230                 if (kn->kn_id == hint)
4231                         kn->kn_data++;
4232         }
4233         return (kn->kn_data != 0);
4234 }
4235
4236 struct sigacts *
4237 sigacts_alloc(void)
4238 {
4239         struct sigacts *ps;
4240
4241         ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
4242         refcount_init(&ps->ps_refcnt, 1);
4243         mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
4244         return (ps);
4245 }
4246
4247 void
4248 sigacts_free(struct sigacts *ps)
4249 {
4250
4251         if (refcount_release(&ps->ps_refcnt) == 0)
4252                 return;
4253         mtx_destroy(&ps->ps_mtx);
4254         free(ps, M_SUBPROC);
4255 }
4256
4257 struct sigacts *
4258 sigacts_hold(struct sigacts *ps)
4259 {
4260
4261         refcount_acquire(&ps->ps_refcnt);
4262         return (ps);
4263 }
4264
4265 void
4266 sigacts_copy(struct sigacts *dest, struct sigacts *src)
4267 {
4268
4269         KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
4270         mtx_lock(&src->ps_mtx);
4271         bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
4272         mtx_unlock(&src->ps_mtx);
4273 }
4274
4275 int
4276 sigacts_shared(struct sigacts *ps)
4277 {
4278
4279         return (ps->ps_refcnt > 1);
4280 }
4281
4282 void
4283 sig_drop_caught(struct proc *p)
4284 {
4285         int sig;
4286         struct sigacts *ps;
4287
4288         ps = p->p_sigacts;
4289         PROC_LOCK_ASSERT(p, MA_OWNED);
4290         mtx_assert(&ps->ps_mtx, MA_OWNED);
4291         SIG_FOREACH(sig, &ps->ps_sigcatch) {
4292                 sigdflt(ps, sig);
4293                 if ((sigprop(sig) & SIGPROP_IGNORE) != 0)
4294                         sigqueue_delete_proc(p, sig);
4295         }
4296 }
4297
4298 static void
4299 sigfastblock_failed(struct thread *td, bool sendsig, bool write)
4300 {
4301         ksiginfo_t ksi;
4302
4303         /*
4304          * Prevent further fetches and SIGSEGVs, allowing thread to
4305          * issue syscalls despite corruption.
4306          */
4307         sigfastblock_clear(td);
4308
4309         if (!sendsig)
4310                 return;
4311         ksiginfo_init_trap(&ksi);
4312         ksi.ksi_signo = SIGSEGV;
4313         ksi.ksi_code = write ? SEGV_ACCERR : SEGV_MAPERR;
4314         ksi.ksi_addr = td->td_sigblock_ptr;
4315         trapsignal(td, &ksi);
4316 }
4317
4318 static bool
4319 sigfastblock_fetch_sig(struct thread *td, bool sendsig, uint32_t *valp)
4320 {
4321         uint32_t res;
4322
4323         if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4324                 return (true);
4325         if (fueword32((void *)td->td_sigblock_ptr, &res) == -1) {
4326                 sigfastblock_failed(td, sendsig, false);
4327                 return (false);
4328         }
4329         *valp = res;
4330         td->td_sigblock_val = res & ~SIGFASTBLOCK_FLAGS;
4331         return (true);
4332 }
4333
4334 static void
4335 sigfastblock_resched(struct thread *td, bool resched)
4336 {
4337         struct proc *p;
4338
4339         if (resched) {
4340                 p = td->td_proc;
4341                 PROC_LOCK(p);
4342                 reschedule_signals(p, td->td_sigmask, 0);
4343                 PROC_UNLOCK(p);
4344         }
4345         thread_lock(td);
4346         td->td_flags |= TDF_ASTPENDING | TDF_NEEDSIGCHK;
4347         thread_unlock(td);
4348 }
4349
4350 int
4351 sys_sigfastblock(struct thread *td, struct sigfastblock_args *uap)
4352 {
4353         struct proc *p;
4354         int error, res;
4355         uint32_t oldval;
4356
4357         error = 0;
4358         p = td->td_proc;
4359         switch (uap->cmd) {
4360         case SIGFASTBLOCK_SETPTR:
4361                 if ((td->td_pflags & TDP_SIGFASTBLOCK) != 0) {
4362                         error = EBUSY;
4363                         break;
4364                 }
4365                 if (((uintptr_t)(uap->ptr) & (sizeof(uint32_t) - 1)) != 0) {
4366                         error = EINVAL;
4367                         break;
4368                 }
4369                 td->td_pflags |= TDP_SIGFASTBLOCK;
4370                 td->td_sigblock_ptr = uap->ptr;
4371                 break;
4372
4373         case SIGFASTBLOCK_UNBLOCK:
4374                 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4375                         error = EINVAL;
4376                         break;
4377                 }
4378
4379                 for (;;) {
4380                         res = casueword32(td->td_sigblock_ptr,
4381                             SIGFASTBLOCK_PEND, &oldval, 0);
4382                         if (res == -1) {
4383                                 error = EFAULT;
4384                                 sigfastblock_failed(td, false, true);
4385                                 break;
4386                         }
4387                         if (res == 0)
4388                                 break;
4389                         MPASS(res == 1);
4390                         if (oldval != SIGFASTBLOCK_PEND) {
4391                                 error = EBUSY;
4392                                 break;
4393                         }
4394                         error = thread_check_susp(td, false);
4395                         if (error != 0)
4396                                 break;
4397                 }
4398                 if (error != 0)
4399                         break;
4400
4401                 /*
4402                  * td_sigblock_val is cleared there, but not on a
4403                  * syscall exit.  The end effect is that a single
4404                  * interruptible sleep, while user sigblock word is
4405                  * set, might return EINTR or ERESTART to usermode
4406                  * without delivering signal.  All further sleeps,
4407                  * until userspace clears the word and does
4408                  * sigfastblock(UNBLOCK), observe current word and no
4409                  * longer get interrupted.  It is slight
4410                  * non-conformance, with alternative to have read the
4411                  * sigblock word on each syscall entry.
4412                  */
4413                 td->td_sigblock_val = 0;
4414
4415                 /*
4416                  * Rely on normal ast mechanism to deliver pending
4417                  * signals to current thread.  But notify others about
4418                  * fake unblock.
4419                  */
4420                 sigfastblock_resched(td, error == 0 && p->p_numthreads != 1);
4421
4422                 break;
4423
4424         case SIGFASTBLOCK_UNSETPTR:
4425                 if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0) {
4426                         error = EINVAL;
4427                         break;
4428                 }
4429                 if (!sigfastblock_fetch_sig(td, false, &oldval)) {
4430                         error = EFAULT;
4431                         break;
4432                 }
4433                 if (oldval != 0 && oldval != SIGFASTBLOCK_PEND) {
4434                         error = EBUSY;
4435                         break;
4436                 }
4437                 sigfastblock_clear(td);
4438                 break;
4439
4440         default:
4441                 error = EINVAL;
4442                 break;
4443         }
4444         return (error);
4445 }
4446
4447 void
4448 sigfastblock_clear(struct thread *td)
4449 {
4450         bool resched;
4451
4452         if ((td->td_pflags & TDP_SIGFASTBLOCK) == 0)
4453                 return;
4454         td->td_sigblock_val = 0;
4455         resched = (td->td_pflags & TDP_SIGFASTPENDING) != 0 ||
4456             SIGPENDING(td);
4457         td->td_pflags &= ~(TDP_SIGFASTBLOCK | TDP_SIGFASTPENDING);
4458         sigfastblock_resched(td, resched);
4459 }
4460
4461 void
4462 sigfastblock_fetch(struct thread *td)
4463 {
4464         uint32_t val;
4465
4466         (void)sigfastblock_fetch_sig(td, true, &val);
4467 }
4468
4469 static void
4470 sigfastblock_setpend1(struct thread *td)
4471 {
4472         int res;
4473         uint32_t oldval;
4474
4475         if ((td->td_pflags & TDP_SIGFASTPENDING) == 0)
4476                 return;
4477         res = fueword32((void *)td->td_sigblock_ptr, &oldval);
4478         if (res == -1) {
4479                 sigfastblock_failed(td, true, false);
4480                 return;
4481         }
4482         for (;;) {
4483                 res = casueword32(td->td_sigblock_ptr, oldval, &oldval,
4484                     oldval | SIGFASTBLOCK_PEND);
4485                 if (res == -1) {
4486                         sigfastblock_failed(td, true, true);
4487                         return;
4488                 }
4489                 if (res == 0) {
4490                         td->td_sigblock_val = oldval & ~SIGFASTBLOCK_FLAGS;
4491                         td->td_pflags &= ~TDP_SIGFASTPENDING;
4492                         break;
4493                 }
4494                 MPASS(res == 1);
4495                 if (thread_check_susp(td, false) != 0)
4496                         break;
4497         }
4498 }
4499
4500 void
4501 sigfastblock_setpend(struct thread *td, bool resched)
4502 {
4503         struct proc *p;
4504
4505         sigfastblock_setpend1(td);
4506         if (resched) {
4507                 p = td->td_proc;
4508                 PROC_LOCK(p);
4509                 reschedule_signals(p, fastblock_mask, SIGPROCMASK_FASTBLK);
4510                 PROC_UNLOCK(p);
4511         }
4512 }