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