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