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