1 /*-
2 * Copyright (c) 1982, 1986, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
35 */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include "opt_compat.h"
41 #include "opt_kdtrace.h"
42 #include "opt_ktrace.h"
43 #include "opt_core.h"
44 #include "opt_procdesc.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/signalvar.h>
49 #include <sys/vnode.h>
50 #include <sys/acct.h>
51 #include <sys/capsicum.h>
52 #include <sys/condvar.h>
53 #include <sys/event.h>
54 #include <sys/fcntl.h>
55 #include <sys/imgact.h>
56 #include <sys/kernel.h>
57 #include <sys/ktr.h>
58 #include <sys/ktrace.h>
59 #include <sys/lock.h>
60 #include <sys/malloc.h>
61 #include <sys/mutex.h>
62 #include <sys/refcount.h>
63 #include <sys/namei.h>
64 #include <sys/proc.h>
65 #include <sys/procdesc.h>
66 #include <sys/posix4.h>
67 #include <sys/pioctl.h>
68 #include <sys/racct.h>
69 #include <sys/resourcevar.h>
70 #include <sys/sdt.h>
71 #include <sys/sbuf.h>
72 #include <sys/sleepqueue.h>
73 #include <sys/smp.h>
74 #include <sys/stat.h>
75 #include <sys/sx.h>
76 #include <sys/syscallsubr.h>
77 #include <sys/sysctl.h>
78 #include <sys/sysent.h>
79 #include <sys/syslog.h>
80 #include <sys/sysproto.h>
81 #include <sys/timers.h>
82 #include <sys/unistd.h>
83 #include <sys/wait.h>
84 #include <vm/vm.h>
85 #include <vm/vm_extern.h>
86 #include <vm/uma.h>
87
88 #include <sys/jail.h>
89
90 #include <machine/cpu.h>
91
92 #include <security/audit/audit.h>
93
94 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
95
96 SDT_PROVIDER_DECLARE(proc);
97 SDT_PROBE_DEFINE3(proc, kernel, , signal__send, "struct thread *",
98 "struct proc *", "int");
99 SDT_PROBE_DEFINE2(proc, kernel, , signal__clear, "int",
100 "ksiginfo_t *");
101 SDT_PROBE_DEFINE3(proc, kernel, , signal__discard,
102 "struct thread *", "struct proc *", "int");
103
104 static int coredump(struct thread *);
105 static int killpg1(struct thread *td, int sig, int pgid, int all,
106 ksiginfo_t *ksi);
107 static int issignal(struct thread *td);
108 static void tdsigwakeup(struct thread *, int, sig_t, int);
109 static void sig_suspend_threads(struct thread *, struct proc *, int);
110 static int filt_sigattach(struct knote *kn);
111 static void filt_sigdetach(struct knote *kn);
112 static int filt_signal(struct knote *kn, long hint);
113 static struct thread *sigtd(struct proc *p, int sig, int prop);
114 static void sigqueue_start(void);
115
116 static uma_zone_t ksiginfo_zone = NULL;
117 struct filterops sig_filtops = {
118 .f_isfd = 0,
119 .f_attach = filt_sigattach,
120 .f_detach = filt_sigdetach,
121 .f_event = filt_signal,
122 };
123
124 static int kern_logsigexit = 1;
125 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
126 &kern_logsigexit, 0,
127 "Log processes quitting on abnormal signals to syslog(3)");
128
129 static int kern_forcesigexit = 1;
130 SYSCTL_INT(_kern, OID_AUTO, forcesigexit, CTLFLAG_RW,
131 &kern_forcesigexit, 0, "Force trap signal to be handled");
132
133 static SYSCTL_NODE(_kern, OID_AUTO, sigqueue, CTLFLAG_RW, 0,
134 "POSIX real time signal");
135
136 static int max_pending_per_proc = 128;
137 SYSCTL_INT(_kern_sigqueue, OID_AUTO, max_pending_per_proc, CTLFLAG_RW,
138 &max_pending_per_proc, 0, "Max pending signals per proc");
139
140 static int preallocate_siginfo = 1024;
141 TUNABLE_INT("kern.sigqueue.preallocate", &preallocate_siginfo);
142 SYSCTL_INT(_kern_sigqueue, OID_AUTO, preallocate, CTLFLAG_RD,
143 &preallocate_siginfo, 0, "Preallocated signal memory size");
144
145 static int signal_overflow = 0;
146 SYSCTL_INT(_kern_sigqueue, OID_AUTO, overflow, CTLFLAG_RD,
147 &signal_overflow, 0, "Number of signals overflew");
148
149 static int signal_alloc_fail = 0;
150 SYSCTL_INT(_kern_sigqueue, OID_AUTO, alloc_fail, CTLFLAG_RD,
151 &signal_alloc_fail, 0, "signals failed to be allocated");
152
153 SYSINIT(signal, SI_SUB_P1003_1B, SI_ORDER_FIRST+3, sigqueue_start, NULL);
154
155 /*
156 * Policy -- Can ucred cr1 send SIGIO to process cr2?
157 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
158 * in the right situations.
159 */
160 #define CANSIGIO(cr1, cr2) \
161 ((cr1)->cr_uid == 0 || \
162 (cr1)->cr_ruid == (cr2)->cr_ruid || \
163 (cr1)->cr_uid == (cr2)->cr_ruid || \
164 (cr1)->cr_ruid == (cr2)->cr_uid || \
165 (cr1)->cr_uid == (cr2)->cr_uid)
166
167 static int sugid_coredump;
168 TUNABLE_INT("kern.sugid_coredump", &sugid_coredump);
169 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
170 &sugid_coredump, 0, "Allow setuid and setgid processes to dump core");
171
172 static int capmode_coredump;
173 TUNABLE_INT("kern.capmode_coredump", &capmode_coredump);
174 SYSCTL_INT(_kern, OID_AUTO, capmode_coredump, CTLFLAG_RW,
175 &capmode_coredump, 0, "Allow processes in capability mode to dump core");
176
177 static int do_coredump = 1;
178 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
179 &do_coredump, 0, "Enable/Disable coredumps");
180
181 static int set_core_nodump_flag = 0;
182 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
183 0, "Enable setting the NODUMP flag on coredump files");
184
185 /*
186 * Signal properties and actions.
187 * The array below categorizes the signals and their default actions
188 * according to the following properties:
189 */
190 #define SA_KILL 0x01 /* terminates process by default */
191 #define SA_CORE 0x02 /* ditto and coredumps */
192 #define SA_STOP 0x04 /* suspend process */
193 #define SA_TTYSTOP 0x08 /* ditto, from tty */
194 #define SA_IGNORE 0x10 /* ignore by default */
195 #define SA_CONT 0x20 /* continue if suspended */
196 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
197
198 static int sigproptbl[NSIG] = {
199 SA_KILL, /* SIGHUP */
200 SA_KILL, /* SIGINT */
201 SA_KILL|SA_CORE, /* SIGQUIT */
202 SA_KILL|SA_CORE, /* SIGILL */
203 SA_KILL|SA_CORE, /* SIGTRAP */
204 SA_KILL|SA_CORE, /* SIGABRT */
205 SA_KILL|SA_CORE, /* SIGEMT */
206 SA_KILL|SA_CORE, /* SIGFPE */
207 SA_KILL, /* SIGKILL */
208 SA_KILL|SA_CORE, /* SIGBUS */
209 SA_KILL|SA_CORE, /* SIGSEGV */
210 SA_KILL|SA_CORE, /* SIGSYS */
211 SA_KILL, /* SIGPIPE */
212 SA_KILL, /* SIGALRM */
213 SA_KILL, /* SIGTERM */
214 SA_IGNORE, /* SIGURG */
215 SA_STOP, /* SIGSTOP */
216 SA_STOP|SA_TTYSTOP, /* SIGTSTP */
217 SA_IGNORE|SA_CONT, /* SIGCONT */
218 SA_IGNORE, /* SIGCHLD */
219 SA_STOP|SA_TTYSTOP, /* SIGTTIN */
220 SA_STOP|SA_TTYSTOP, /* SIGTTOU */
221 SA_IGNORE, /* SIGIO */
222 SA_KILL, /* SIGXCPU */
223 SA_KILL, /* SIGXFSZ */
224 SA_KILL, /* SIGVTALRM */
225 SA_KILL, /* SIGPROF */
226 SA_IGNORE, /* SIGWINCH */
227 SA_IGNORE, /* SIGINFO */
228 SA_KILL, /* SIGUSR1 */
229 SA_KILL, /* SIGUSR2 */
230 };
231
232 static void reschedule_signals(struct proc *p, sigset_t block, int flags);
233
234 static void
sigqueue_start(void)235 sigqueue_start(void)
236 {
237 ksiginfo_zone = uma_zcreate("ksiginfo", sizeof(ksiginfo_t),
238 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
239 uma_prealloc(ksiginfo_zone, preallocate_siginfo);
240 p31b_setcfg(CTL_P1003_1B_REALTIME_SIGNALS, _POSIX_REALTIME_SIGNALS);
241 p31b_setcfg(CTL_P1003_1B_RTSIG_MAX, SIGRTMAX - SIGRTMIN + 1);
242 p31b_setcfg(CTL_P1003_1B_SIGQUEUE_MAX, max_pending_per_proc);
243 }
244
245 ksiginfo_t *
ksiginfo_alloc(int wait)246 ksiginfo_alloc(int wait)
247 {
248 int flags;
249
250 flags = M_ZERO;
251 if (! wait)
252 flags |= M_NOWAIT;
253 if (ksiginfo_zone != NULL)
254 return ((ksiginfo_t *)uma_zalloc(ksiginfo_zone, flags));
255 return (NULL);
256 }
257
258 void
ksiginfo_free(ksiginfo_t * ksi)259 ksiginfo_free(ksiginfo_t *ksi)
260 {
261 uma_zfree(ksiginfo_zone, ksi);
262 }
263
264 static __inline int
ksiginfo_tryfree(ksiginfo_t * ksi)265 ksiginfo_tryfree(ksiginfo_t *ksi)
266 {
267 if (!(ksi->ksi_flags & KSI_EXT)) {
268 uma_zfree(ksiginfo_zone, ksi);
269 return (1);
270 }
271 return (0);
272 }
273
274 void
sigqueue_init(sigqueue_t * list,struct proc * p)275 sigqueue_init(sigqueue_t *list, struct proc *p)
276 {
277 SIGEMPTYSET(list->sq_signals);
278 SIGEMPTYSET(list->sq_kill);
279 TAILQ_INIT(&list->sq_list);
280 list->sq_proc = p;
281 list->sq_flags = SQ_INIT;
282 }
283
284 /*
285 * Get a signal's ksiginfo.
286 * Return:
287 * 0 - signal not found
288 * others - signal number
289 */
290 static int
sigqueue_get(sigqueue_t * sq,int signo,ksiginfo_t * si)291 sigqueue_get(sigqueue_t *sq, int signo, ksiginfo_t *si)
292 {
293 struct proc *p = sq->sq_proc;
294 struct ksiginfo *ksi, *next;
295 int count = 0;
296
297 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
298
299 if (!SIGISMEMBER(sq->sq_signals, signo))
300 return (0);
301
302 if (SIGISMEMBER(sq->sq_kill, signo)) {
303 count++;
304 SIGDELSET(sq->sq_kill, signo);
305 }
306
307 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
308 if (ksi->ksi_signo == signo) {
309 if (count == 0) {
310 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
311 ksi->ksi_sigq = NULL;
312 ksiginfo_copy(ksi, si);
313 if (ksiginfo_tryfree(ksi) && p != NULL)
314 p->p_pendingcnt--;
315 }
316 if (++count > 1)
317 break;
318 }
319 }
320
321 if (count <= 1)
322 SIGDELSET(sq->sq_signals, signo);
323 si->ksi_signo = signo;
324 return (signo);
325 }
326
327 void
sigqueue_take(ksiginfo_t * ksi)328 sigqueue_take(ksiginfo_t *ksi)
329 {
330 struct ksiginfo *kp;
331 struct proc *p;
332 sigqueue_t *sq;
333
334 if (ksi == NULL || (sq = ksi->ksi_sigq) == NULL)
335 return;
336
337 p = sq->sq_proc;
338 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
339 ksi->ksi_sigq = NULL;
340 if (!(ksi->ksi_flags & KSI_EXT) && p != NULL)
341 p->p_pendingcnt--;
342
343 for (kp = TAILQ_FIRST(&sq->sq_list); kp != NULL;
344 kp = TAILQ_NEXT(kp, ksi_link)) {
345 if (kp->ksi_signo == ksi->ksi_signo)
346 break;
347 }
348 if (kp == NULL && !SIGISMEMBER(sq->sq_kill, ksi->ksi_signo))
349 SIGDELSET(sq->sq_signals, ksi->ksi_signo);
350 }
351
352 static int
sigqueue_add(sigqueue_t * sq,int signo,ksiginfo_t * si)353 sigqueue_add(sigqueue_t *sq, int signo, ksiginfo_t *si)
354 {
355 struct proc *p = sq->sq_proc;
356 struct ksiginfo *ksi;
357 int ret = 0;
358
359 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
360
361 if (signo == SIGKILL || signo == SIGSTOP || si == NULL) {
362 SIGADDSET(sq->sq_kill, signo);
363 goto out_set_bit;
364 }
365
366 /* directly insert the ksi, don't copy it */
367 if (si->ksi_flags & KSI_INS) {
368 if (si->ksi_flags & KSI_HEAD)
369 TAILQ_INSERT_HEAD(&sq->sq_list, si, ksi_link);
370 else
371 TAILQ_INSERT_TAIL(&sq->sq_list, si, ksi_link);
372 si->ksi_sigq = sq;
373 goto out_set_bit;
374 }
375
376 if (__predict_false(ksiginfo_zone == NULL)) {
377 SIGADDSET(sq->sq_kill, signo);
378 goto out_set_bit;
379 }
380
381 if (p != NULL && p->p_pendingcnt >= max_pending_per_proc) {
382 signal_overflow++;
383 ret = EAGAIN;
384 } else if ((ksi = ksiginfo_alloc(0)) == NULL) {
385 signal_alloc_fail++;
386 ret = EAGAIN;
387 } else {
388 if (p != NULL)
389 p->p_pendingcnt++;
390 ksiginfo_copy(si, ksi);
391 ksi->ksi_signo = signo;
392 if (si->ksi_flags & KSI_HEAD)
393 TAILQ_INSERT_HEAD(&sq->sq_list, ksi, ksi_link);
394 else
395 TAILQ_INSERT_TAIL(&sq->sq_list, ksi, ksi_link);
396 ksi->ksi_sigq = sq;
397 }
398
399 if ((si->ksi_flags & KSI_TRAP) != 0 ||
400 (si->ksi_flags & KSI_SIGQ) == 0) {
401 if (ret != 0)
402 SIGADDSET(sq->sq_kill, signo);
403 ret = 0;
404 goto out_set_bit;
405 }
406
407 if (ret != 0)
408 return (ret);
409
410 out_set_bit:
411 SIGADDSET(sq->sq_signals, signo);
412 return (ret);
413 }
414
415 void
sigqueue_flush(sigqueue_t * sq)416 sigqueue_flush(sigqueue_t *sq)
417 {
418 struct proc *p = sq->sq_proc;
419 ksiginfo_t *ksi;
420
421 KASSERT(sq->sq_flags & SQ_INIT, ("sigqueue not inited"));
422
423 if (p != NULL)
424 PROC_LOCK_ASSERT(p, MA_OWNED);
425
426 while ((ksi = TAILQ_FIRST(&sq->sq_list)) != NULL) {
427 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
428 ksi->ksi_sigq = NULL;
429 if (ksiginfo_tryfree(ksi) && p != NULL)
430 p->p_pendingcnt--;
431 }
432
433 SIGEMPTYSET(sq->sq_signals);
434 SIGEMPTYSET(sq->sq_kill);
435 }
436
437 static void
sigqueue_move_set(sigqueue_t * src,sigqueue_t * dst,const sigset_t * set)438 sigqueue_move_set(sigqueue_t *src, sigqueue_t *dst, const sigset_t *set)
439 {
440 sigset_t tmp;
441 struct proc *p1, *p2;
442 ksiginfo_t *ksi, *next;
443
444 KASSERT(src->sq_flags & SQ_INIT, ("src sigqueue not inited"));
445 KASSERT(dst->sq_flags & SQ_INIT, ("dst sigqueue not inited"));
446 p1 = src->sq_proc;
447 p2 = dst->sq_proc;
448 /* Move siginfo to target list */
449 TAILQ_FOREACH_SAFE(ksi, &src->sq_list, ksi_link, next) {
450 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
451 TAILQ_REMOVE(&src->sq_list, ksi, ksi_link);
452 if (p1 != NULL)
453 p1->p_pendingcnt--;
454 TAILQ_INSERT_TAIL(&dst->sq_list, ksi, ksi_link);
455 ksi->ksi_sigq = dst;
456 if (p2 != NULL)
457 p2->p_pendingcnt++;
458 }
459 }
460
461 /* Move pending bits to target list */
462 tmp = src->sq_kill;
463 SIGSETAND(tmp, *set);
464 SIGSETOR(dst->sq_kill, tmp);
465 SIGSETNAND(src->sq_kill, tmp);
466
467 tmp = src->sq_signals;
468 SIGSETAND(tmp, *set);
469 SIGSETOR(dst->sq_signals, tmp);
470 SIGSETNAND(src->sq_signals, tmp);
471 }
472
473 #if 0
474 static void
475 sigqueue_move(sigqueue_t *src, sigqueue_t *dst, int signo)
476 {
477 sigset_t set;
478
479 SIGEMPTYSET(set);
480 SIGADDSET(set, signo);
481 sigqueue_move_set(src, dst, &set);
482 }
483 #endif
484
485 static void
sigqueue_delete_set(sigqueue_t * sq,const sigset_t * set)486 sigqueue_delete_set(sigqueue_t *sq, const sigset_t *set)
487 {
488 struct proc *p = sq->sq_proc;
489 ksiginfo_t *ksi, *next;
490
491 KASSERT(sq->sq_flags & SQ_INIT, ("src sigqueue not inited"));
492
493 /* Remove siginfo queue */
494 TAILQ_FOREACH_SAFE(ksi, &sq->sq_list, ksi_link, next) {
495 if (SIGISMEMBER(*set, ksi->ksi_signo)) {
496 TAILQ_REMOVE(&sq->sq_list, ksi, ksi_link);
497 ksi->ksi_sigq = NULL;
498 if (ksiginfo_tryfree(ksi) && p != NULL)
499 p->p_pendingcnt--;
500 }
501 }
502 SIGSETNAND(sq->sq_kill, *set);
503 SIGSETNAND(sq->sq_signals, *set);
504 }
505
506 void
sigqueue_delete(sigqueue_t * sq,int signo)507 sigqueue_delete(sigqueue_t *sq, int signo)
508 {
509 sigset_t set;
510
511 SIGEMPTYSET(set);
512 SIGADDSET(set, signo);
513 sigqueue_delete_set(sq, &set);
514 }
515
516 /* Remove a set of signals for a process */
517 static void
sigqueue_delete_set_proc(struct proc * p,const sigset_t * set)518 sigqueue_delete_set_proc(struct proc *p, const sigset_t *set)
519 {
520 sigqueue_t worklist;
521 struct thread *td0;
522
523 PROC_LOCK_ASSERT(p, MA_OWNED);
524
525 sigqueue_init(&worklist, NULL);
526 sigqueue_move_set(&p->p_sigqueue, &worklist, set);
527
528 FOREACH_THREAD_IN_PROC(p, td0)
529 sigqueue_move_set(&td0->td_sigqueue, &worklist, set);
530
531 sigqueue_flush(&worklist);
532 }
533
534 void
sigqueue_delete_proc(struct proc * p,int signo)535 sigqueue_delete_proc(struct proc *p, int signo)
536 {
537 sigset_t set;
538
539 SIGEMPTYSET(set);
540 SIGADDSET(set, signo);
541 sigqueue_delete_set_proc(p, &set);
542 }
543
544 static void
sigqueue_delete_stopmask_proc(struct proc * p)545 sigqueue_delete_stopmask_proc(struct proc *p)
546 {
547 sigset_t set;
548
549 SIGEMPTYSET(set);
550 SIGADDSET(set, SIGSTOP);
551 SIGADDSET(set, SIGTSTP);
552 SIGADDSET(set, SIGTTIN);
553 SIGADDSET(set, SIGTTOU);
554 sigqueue_delete_set_proc(p, &set);
555 }
556
557 /*
558 * Determine signal that should be delivered to thread td, the current
559 * thread, 0 if none. If there is a pending stop signal with default
560 * action, the process stops in issignal().
561 */
562 int
cursig(struct thread * td)563 cursig(struct thread *td)
564 {
565 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
566 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
567 THREAD_LOCK_ASSERT(td, MA_NOTOWNED);
568 return (SIGPENDING(td) ? issignal(td) : 0);
569 }
570
571 /*
572 * Arrange for ast() to handle unmasked pending signals on return to user
573 * mode. This must be called whenever a signal is added to td_sigqueue or
574 * unmasked in td_sigmask.
575 */
576 void
signotify(struct thread * td)577 signotify(struct thread *td)
578 {
579 struct proc *p;
580
581 p = td->td_proc;
582
583 PROC_LOCK_ASSERT(p, MA_OWNED);
584
585 if (SIGPENDING(td)) {
586 thread_lock(td);
587 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
588 thread_unlock(td);
589 }
590 }
591
592 int
sigonstack(size_t sp)593 sigonstack(size_t sp)
594 {
595 struct thread *td = curthread;
596
597 return ((td->td_pflags & TDP_ALTSTACK) ?
598 #if defined(COMPAT_43)
599 ((td->td_sigstk.ss_size == 0) ?
600 (td->td_sigstk.ss_flags & SS_ONSTACK) :
601 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
602 #else
603 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
604 #endif
605 : 0);
606 }
607
608 static __inline int
__sigprop(int sig)609 __sigprop(int sig)
610 {
611
612 if (sig > 0 && sig < NSIG)
613 return (sigproptbl[_SIG_IDX(sig)]);
614 return (0);
615 }
616
617 int
sigprop(int sig)618 sigprop(int sig)
619 {
620
621 return (__sigprop(sig));
622 }
623
624 int
sig_ffs(sigset_t * set)625 sig_ffs(sigset_t *set)
626 {
627 int i;
628
629 for (i = 0; i < _SIG_WORDS; i++)
630 if (set->__bits[i])
631 return (ffs(set->__bits[i]) + (i * 32));
632 return (0);
633 }
634
635 static bool
sigact_flag_test(struct sigaction * act,int flag)636 sigact_flag_test(struct sigaction *act, int flag)
637 {
638
639 /*
640 * SA_SIGINFO is reset when signal disposition is set to
641 * ignore or default. Other flags are kept according to user
642 * settings.
643 */
644 return ((act->sa_flags & flag) != 0 && (flag != SA_SIGINFO ||
645 ((__sighandler_t *)act->sa_sigaction != SIG_IGN &&
646 (__sighandler_t *)act->sa_sigaction != SIG_DFL)));
647 }
648
649 /*
650 * kern_sigaction
651 * sigaction
652 * freebsd4_sigaction
653 * osigaction
654 */
655 int
kern_sigaction(td,sig,act,oact,flags)656 kern_sigaction(td, sig, act, oact, flags)
657 struct thread *td;
658 register int sig;
659 struct sigaction *act, *oact;
660 int flags;
661 {
662 struct sigacts *ps;
663 struct proc *p = td->td_proc;
664
665 if (!_SIG_VALID(sig))
666 return (EINVAL);
667 if (act != NULL && act->sa_handler != SIG_DFL &&
668 act->sa_handler != SIG_IGN && (act->sa_flags & ~(SA_ONSTACK |
669 SA_RESTART | SA_RESETHAND | SA_NOCLDSTOP | SA_NODEFER |
670 SA_NOCLDWAIT | SA_SIGINFO)) != 0)
671 return (EINVAL);
672
673 PROC_LOCK(p);
674 ps = p->p_sigacts;
675 mtx_lock(&ps->ps_mtx);
676 if (oact) {
677 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
678 oact->sa_flags = 0;
679 if (SIGISMEMBER(ps->ps_sigonstack, sig))
680 oact->sa_flags |= SA_ONSTACK;
681 if (!SIGISMEMBER(ps->ps_sigintr, sig))
682 oact->sa_flags |= SA_RESTART;
683 if (SIGISMEMBER(ps->ps_sigreset, sig))
684 oact->sa_flags |= SA_RESETHAND;
685 if (SIGISMEMBER(ps->ps_signodefer, sig))
686 oact->sa_flags |= SA_NODEFER;
687 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
688 oact->sa_flags |= SA_SIGINFO;
689 oact->sa_sigaction =
690 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
691 } else
692 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
693 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
694 oact->sa_flags |= SA_NOCLDSTOP;
695 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
696 oact->sa_flags |= SA_NOCLDWAIT;
697 }
698 if (act) {
699 if ((sig == SIGKILL || sig == SIGSTOP) &&
700 act->sa_handler != SIG_DFL) {
701 mtx_unlock(&ps->ps_mtx);
702 PROC_UNLOCK(p);
703 return (EINVAL);
704 }
705
706 /*
707 * Change setting atomically.
708 */
709
710 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
711 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
712 if (sigact_flag_test(act, SA_SIGINFO)) {
713 ps->ps_sigact[_SIG_IDX(sig)] =
714 (__sighandler_t *)act->sa_sigaction;
715 SIGADDSET(ps->ps_siginfo, sig);
716 } else {
717 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
718 SIGDELSET(ps->ps_siginfo, sig);
719 }
720 if (!sigact_flag_test(act, SA_RESTART))
721 SIGADDSET(ps->ps_sigintr, sig);
722 else
723 SIGDELSET(ps->ps_sigintr, sig);
724 if (sigact_flag_test(act, SA_ONSTACK))
725 SIGADDSET(ps->ps_sigonstack, sig);
726 else
727 SIGDELSET(ps->ps_sigonstack, sig);
728 if (sigact_flag_test(act, SA_RESETHAND))
729 SIGADDSET(ps->ps_sigreset, sig);
730 else
731 SIGDELSET(ps->ps_sigreset, sig);
732 if (sigact_flag_test(act, SA_NODEFER))
733 SIGADDSET(ps->ps_signodefer, sig);
734 else
735 SIGDELSET(ps->ps_signodefer, sig);
736 if (sig == SIGCHLD) {
737 if (act->sa_flags & SA_NOCLDSTOP)
738 ps->ps_flag |= PS_NOCLDSTOP;
739 else
740 ps->ps_flag &= ~PS_NOCLDSTOP;
741 if (act->sa_flags & SA_NOCLDWAIT) {
742 /*
743 * Paranoia: since SA_NOCLDWAIT is implemented
744 * by reparenting the dying child to PID 1 (and
745 * trust it to reap the zombie), PID 1 itself
746 * is forbidden to set SA_NOCLDWAIT.
747 */
748 if (p->p_pid == 1)
749 ps->ps_flag &= ~PS_NOCLDWAIT;
750 else
751 ps->ps_flag |= PS_NOCLDWAIT;
752 } else
753 ps->ps_flag &= ~PS_NOCLDWAIT;
754 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
755 ps->ps_flag |= PS_CLDSIGIGN;
756 else
757 ps->ps_flag &= ~PS_CLDSIGIGN;
758 }
759 /*
760 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
761 * and for signals set to SIG_DFL where the default is to
762 * ignore. However, don't put SIGCONT in ps_sigignore, as we
763 * have to restart the process.
764 */
765 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
766 (__sigprop(sig) & SA_IGNORE &&
767 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
768 /* never to be seen again */
769 sigqueue_delete_proc(p, sig);
770 if (sig != SIGCONT)
771 /* easier in psignal */
772 SIGADDSET(ps->ps_sigignore, sig);
773 SIGDELSET(ps->ps_sigcatch, sig);
774 } else {
775 SIGDELSET(ps->ps_sigignore, sig);
776 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
777 SIGDELSET(ps->ps_sigcatch, sig);
778 else
779 SIGADDSET(ps->ps_sigcatch, sig);
780 }
781 #ifdef COMPAT_FREEBSD4
782 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
783 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
784 (flags & KSA_FREEBSD4) == 0)
785 SIGDELSET(ps->ps_freebsd4, sig);
786 else
787 SIGADDSET(ps->ps_freebsd4, sig);
788 #endif
789 #ifdef COMPAT_43
790 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
791 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
792 (flags & KSA_OSIGSET) == 0)
793 SIGDELSET(ps->ps_osigset, sig);
794 else
795 SIGADDSET(ps->ps_osigset, sig);
796 #endif
797 }
798 mtx_unlock(&ps->ps_mtx);
799 PROC_UNLOCK(p);
800 return (0);
801 }
802
803 #ifndef _SYS_SYSPROTO_H_
804 struct sigaction_args {
805 int sig;
806 struct sigaction *act;
807 struct sigaction *oact;
808 };
809 #endif
810 int
sys_sigaction(td,uap)811 sys_sigaction(td, uap)
812 struct thread *td;
813 register struct sigaction_args *uap;
814 {
815 struct sigaction act, oact;
816 register struct sigaction *actp, *oactp;
817 int error;
818
819 actp = (uap->act != NULL) ? &act : NULL;
820 oactp = (uap->oact != NULL) ? &oact : NULL;
821 if (actp) {
822 error = copyin(uap->act, actp, sizeof(act));
823 if (error)
824 return (error);
825 }
826 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
827 if (oactp && !error)
828 error = copyout(oactp, uap->oact, sizeof(oact));
829 return (error);
830 }
831
832 #ifdef COMPAT_FREEBSD4
833 #ifndef _SYS_SYSPROTO_H_
834 struct freebsd4_sigaction_args {
835 int sig;
836 struct sigaction *act;
837 struct sigaction *oact;
838 };
839 #endif
840 int
freebsd4_sigaction(td,uap)841 freebsd4_sigaction(td, uap)
842 struct thread *td;
843 register struct freebsd4_sigaction_args *uap;
844 {
845 struct sigaction act, oact;
846 register struct sigaction *actp, *oactp;
847 int error;
848
849
850 actp = (uap->act != NULL) ? &act : NULL;
851 oactp = (uap->oact != NULL) ? &oact : NULL;
852 if (actp) {
853 error = copyin(uap->act, actp, sizeof(act));
854 if (error)
855 return (error);
856 }
857 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
858 if (oactp && !error)
859 error = copyout(oactp, uap->oact, sizeof(oact));
860 return (error);
861 }
862 #endif /* COMAPT_FREEBSD4 */
863
864 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
865 #ifndef _SYS_SYSPROTO_H_
866 struct osigaction_args {
867 int signum;
868 struct osigaction *nsa;
869 struct osigaction *osa;
870 };
871 #endif
872 int
osigaction(td,uap)873 osigaction(td, uap)
874 struct thread *td;
875 register struct osigaction_args *uap;
876 {
877 struct osigaction sa;
878 struct sigaction nsa, osa;
879 register struct sigaction *nsap, *osap;
880 int error;
881
882 if (uap->signum <= 0 || uap->signum >= ONSIG)
883 return (EINVAL);
884
885 nsap = (uap->nsa != NULL) ? &nsa : NULL;
886 osap = (uap->osa != NULL) ? &osa : NULL;
887
888 if (nsap) {
889 error = copyin(uap->nsa, &sa, sizeof(sa));
890 if (error)
891 return (error);
892 nsap->sa_handler = sa.sa_handler;
893 nsap->sa_flags = sa.sa_flags;
894 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
895 }
896 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
897 if (osap && !error) {
898 sa.sa_handler = osap->sa_handler;
899 sa.sa_flags = osap->sa_flags;
900 SIG2OSIG(osap->sa_mask, sa.sa_mask);
901 error = copyout(&sa, uap->osa, sizeof(sa));
902 }
903 return (error);
904 }
905
906 #if !defined(__i386__)
907 /* Avoid replicating the same stub everywhere */
908 int
osigreturn(td,uap)909 osigreturn(td, uap)
910 struct thread *td;
911 struct osigreturn_args *uap;
912 {
913
914 return (nosys(td, (struct nosys_args *)uap));
915 }
916 #endif
917 #endif /* COMPAT_43 */
918
919 /*
920 * Initialize signal state for process 0;
921 * set to ignore signals that are ignored by default.
922 */
923 void
siginit(p)924 siginit(p)
925 struct proc *p;
926 {
927 register int i;
928 struct sigacts *ps;
929
930 PROC_LOCK(p);
931 ps = p->p_sigacts;
932 mtx_lock(&ps->ps_mtx);
933 for (i = 1; i <= NSIG; i++) {
934 if (__sigprop(i) & SA_IGNORE && i != SIGCONT) {
935 SIGADDSET(ps->ps_sigignore, i);
936 }
937 }
938 mtx_unlock(&ps->ps_mtx);
939 PROC_UNLOCK(p);
940 }
941
942 /*
943 * Reset specified signal to the default disposition.
944 */
945 static void
sigdflt(struct sigacts * ps,int sig)946 sigdflt(struct sigacts *ps, int sig)
947 {
948
949 mtx_assert(&ps->ps_mtx, MA_OWNED);
950 SIGDELSET(ps->ps_sigcatch, sig);
951 if ((__sigprop(sig) & SA_IGNORE) != 0 && sig != SIGCONT)
952 SIGADDSET(ps->ps_sigignore, sig);
953 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
954 SIGDELSET(ps->ps_siginfo, sig);
955 }
956
957 /*
958 * Reset signals for an exec of the specified process.
959 */
960 void
execsigs(struct proc * p)961 execsigs(struct proc *p)
962 {
963 struct sigacts *ps;
964 int sig;
965 struct thread *td;
966
967 /*
968 * Reset caught signals. Held signals remain held
969 * through td_sigmask (unless they were caught,
970 * and are now ignored by default).
971 */
972 PROC_LOCK_ASSERT(p, MA_OWNED);
973 td = FIRST_THREAD_IN_PROC(p);
974 ps = p->p_sigacts;
975 mtx_lock(&ps->ps_mtx);
976 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
977 sig = sig_ffs(&ps->ps_sigcatch);
978 sigdflt(ps, sig);
979 if ((__sigprop(sig) & SA_IGNORE) != 0)
980 sigqueue_delete_proc(p, sig);
981 }
982 /*
983 * Reset stack state to the user stack.
984 * Clear set of signals caught on the signal stack.
985 */
986 td->td_sigstk.ss_flags = SS_DISABLE;
987 td->td_sigstk.ss_size = 0;
988 td->td_sigstk.ss_sp = 0;
989 td->td_pflags &= ~TDP_ALTSTACK;
990 /*
991 * Reset no zombies if child dies flag as Solaris does.
992 */
993 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
994 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
995 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
996 mtx_unlock(&ps->ps_mtx);
997 }
998
999 /*
1000 * kern_sigprocmask()
1001 *
1002 * Manipulate signal mask.
1003 */
1004 int
kern_sigprocmask(struct thread * td,int how,sigset_t * set,sigset_t * oset,int flags)1005 kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset,
1006 int flags)
1007 {
1008 sigset_t new_block, oset1;
1009 struct proc *p;
1010 int error;
1011
1012 p = td->td_proc;
1013 if ((flags & SIGPROCMASK_PROC_LOCKED) != 0)
1014 PROC_LOCK_ASSERT(p, MA_OWNED);
1015 else
1016 PROC_LOCK(p);
1017 mtx_assert(&p->p_sigacts->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0
1018 ? MA_OWNED : MA_NOTOWNED);
1019 if (oset != NULL)
1020 *oset = td->td_sigmask;
1021
1022 error = 0;
1023 if (set != NULL) {
1024 switch (how) {
1025 case SIG_BLOCK:
1026 SIG_CANTMASK(*set);
1027 oset1 = td->td_sigmask;
1028 SIGSETOR(td->td_sigmask, *set);
1029 new_block = td->td_sigmask;
1030 SIGSETNAND(new_block, oset1);
1031 break;
1032 case SIG_UNBLOCK:
1033 SIGSETNAND(td->td_sigmask, *set);
1034 signotify(td);
1035 goto out;
1036 case SIG_SETMASK:
1037 SIG_CANTMASK(*set);
1038 oset1 = td->td_sigmask;
1039 if (flags & SIGPROCMASK_OLD)
1040 SIGSETLO(td->td_sigmask, *set);
1041 else
1042 td->td_sigmask = *set;
1043 new_block = td->td_sigmask;
1044 SIGSETNAND(new_block, oset1);
1045 signotify(td);
1046 break;
1047 default:
1048 error = EINVAL;
1049 goto out;
1050 }
1051
1052 /*
1053 * The new_block set contains signals that were not previously
1054 * blocked, but are blocked now.
1055 *
1056 * In case we block any signal that was not previously blocked
1057 * for td, and process has the signal pending, try to schedule
1058 * signal delivery to some thread that does not block the
1059 * signal, possibly waking it up.
1060 */
1061 if (p->p_numthreads != 1)
1062 reschedule_signals(p, new_block, flags);
1063 }
1064
1065 out:
1066 if (!(flags & SIGPROCMASK_PROC_LOCKED))
1067 PROC_UNLOCK(p);
1068 return (error);
1069 }
1070
1071 #ifndef _SYS_SYSPROTO_H_
1072 struct sigprocmask_args {
1073 int how;
1074 const sigset_t *set;
1075 sigset_t *oset;
1076 };
1077 #endif
1078 int
sys_sigprocmask(td,uap)1079 sys_sigprocmask(td, uap)
1080 register struct thread *td;
1081 struct sigprocmask_args *uap;
1082 {
1083 sigset_t set, oset;
1084 sigset_t *setp, *osetp;
1085 int error;
1086
1087 setp = (uap->set != NULL) ? &set : NULL;
1088 osetp = (uap->oset != NULL) ? &oset : NULL;
1089 if (setp) {
1090 error = copyin(uap->set, setp, sizeof(set));
1091 if (error)
1092 return (error);
1093 }
1094 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
1095 if (osetp && !error) {
1096 error = copyout(osetp, uap->oset, sizeof(oset));
1097 }
1098 return (error);
1099 }
1100
1101 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1102 #ifndef _SYS_SYSPROTO_H_
1103 struct osigprocmask_args {
1104 int how;
1105 osigset_t mask;
1106 };
1107 #endif
1108 int
osigprocmask(td,uap)1109 osigprocmask(td, uap)
1110 register struct thread *td;
1111 struct osigprocmask_args *uap;
1112 {
1113 sigset_t set, oset;
1114 int error;
1115
1116 OSIG2SIG(uap->mask, set);
1117 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
1118 SIG2OSIG(oset, td->td_retval[0]);
1119 return (error);
1120 }
1121 #endif /* COMPAT_43 */
1122
1123 int
sys_sigwait(struct thread * td,struct sigwait_args * uap)1124 sys_sigwait(struct thread *td, struct sigwait_args *uap)
1125 {
1126 ksiginfo_t ksi;
1127 sigset_t set;
1128 int error;
1129
1130 error = copyin(uap->set, &set, sizeof(set));
1131 if (error) {
1132 td->td_retval[0] = error;
1133 return (0);
1134 }
1135
1136 error = kern_sigtimedwait(td, set, &ksi, NULL);
1137 if (error) {
1138 if (error == EINTR && td->td_proc->p_osrel < P_OSREL_SIGWAIT)
1139 error = ERESTART;
1140 if (error == ERESTART)
1141 return (error);
1142 td->td_retval[0] = error;
1143 return (0);
1144 }
1145
1146 error = copyout(&ksi.ksi_signo, uap->sig, sizeof(ksi.ksi_signo));
1147 td->td_retval[0] = error;
1148 return (0);
1149 }
1150
1151 int
sys_sigtimedwait(struct thread * td,struct sigtimedwait_args * uap)1152 sys_sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
1153 {
1154 struct timespec ts;
1155 struct timespec *timeout;
1156 sigset_t set;
1157 ksiginfo_t ksi;
1158 int error;
1159
1160 if (uap->timeout) {
1161 error = copyin(uap->timeout, &ts, sizeof(ts));
1162 if (error)
1163 return (error);
1164
1165 timeout = &ts;
1166 } else
1167 timeout = NULL;
1168
1169 error = copyin(uap->set, &set, sizeof(set));
1170 if (error)
1171 return (error);
1172
1173 error = kern_sigtimedwait(td, set, &ksi, timeout);
1174 if (error)
1175 return (error);
1176
1177 if (uap->info)
1178 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1179
1180 if (error == 0)
1181 td->td_retval[0] = ksi.ksi_signo;
1182 return (error);
1183 }
1184
1185 int
sys_sigwaitinfo(struct thread * td,struct sigwaitinfo_args * uap)1186 sys_sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
1187 {
1188 ksiginfo_t ksi;
1189 sigset_t set;
1190 int error;
1191
1192 error = copyin(uap->set, &set, sizeof(set));
1193 if (error)
1194 return (error);
1195
1196 error = kern_sigtimedwait(td, set, &ksi, NULL);
1197 if (error)
1198 return (error);
1199
1200 if (uap->info)
1201 error = copyout(&ksi.ksi_info, uap->info, sizeof(siginfo_t));
1202
1203 if (error == 0)
1204 td->td_retval[0] = ksi.ksi_signo;
1205 return (error);
1206 }
1207
1208 int
kern_sigtimedwait(struct thread * td,sigset_t waitset,ksiginfo_t * ksi,struct timespec * timeout)1209 kern_sigtimedwait(struct thread *td, sigset_t waitset, ksiginfo_t *ksi,
1210 struct timespec *timeout)
1211 {
1212 struct sigacts *ps;
1213 sigset_t saved_mask, new_block;
1214 struct proc *p;
1215 int error, sig, timo, timevalid = 0;
1216 struct timespec rts, ets, ts;
1217 struct timeval tv;
1218
1219 p = td->td_proc;
1220 error = 0;
1221 ets.tv_sec = 0;
1222 ets.tv_nsec = 0;
1223
1224 if (timeout != NULL) {
1225 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
1226 timevalid = 1;
1227 getnanouptime(&rts);
1228 ets = rts;
1229 timespecadd(&ets, timeout);
1230 }
1231 }
1232 ksiginfo_init(ksi);
1233 /* Some signals can not be waited for. */
1234 SIG_CANTMASK(waitset);
1235 ps = p->p_sigacts;
1236 PROC_LOCK(p);
1237 saved_mask = td->td_sigmask;
1238 SIGSETNAND(td->td_sigmask, waitset);
1239 for (;;) {
1240 mtx_lock(&ps->ps_mtx);
1241 sig = cursig(td);
1242 mtx_unlock(&ps->ps_mtx);
1243 if (sig != 0 && SIGISMEMBER(waitset, sig)) {
1244 if (sigqueue_get(&td->td_sigqueue, sig, ksi) != 0 ||
1245 sigqueue_get(&p->p_sigqueue, sig, ksi) != 0) {
1246 error = 0;
1247 break;
1248 }
1249 }
1250
1251 if (error != 0)
1252 break;
1253
1254 /*
1255 * POSIX says this must be checked after looking for pending
1256 * signals.
1257 */
1258 if (timeout != NULL) {
1259 if (!timevalid) {
1260 error = EINVAL;
1261 break;
1262 }
1263 getnanouptime(&rts);
1264 if (timespeccmp(&rts, &ets, >=)) {
1265 error = EAGAIN;
1266 break;
1267 }
1268 ts = ets;
1269 timespecsub(&ts, &rts);
1270 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1271 timo = tvtohz(&tv);
1272 } else {
1273 timo = 0;
1274 }
1275
1276 error = msleep(ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", timo);
1277
1278 if (timeout != NULL) {
1279 if (error == ERESTART) {
1280 /* Timeout can not be restarted. */
1281 error = EINTR;
1282 } else if (error == EAGAIN) {
1283 /* We will calculate timeout by ourself. */
1284 error = 0;
1285 }
1286 }
1287 }
1288
1289 new_block = saved_mask;
1290 SIGSETNAND(new_block, td->td_sigmask);
1291 td->td_sigmask = saved_mask;
1292 /*
1293 * Fewer signals can be delivered to us, reschedule signal
1294 * notification.
1295 */
1296 if (p->p_numthreads != 1)
1297 reschedule_signals(p, new_block, 0);
1298
1299 if (error == 0) {
1300 SDT_PROBE(proc, kernel, , signal__clear, sig, ksi, 0, 0, 0);
1301
1302 if (ksi->ksi_code == SI_TIMER)
1303 itimer_accept(p, ksi->ksi_timerid, ksi);
1304
1305 #ifdef KTRACE
1306 if (KTRPOINT(td, KTR_PSIG)) {
1307 sig_t action;
1308
1309 mtx_lock(&ps->ps_mtx);
1310 action = ps->ps_sigact[_SIG_IDX(sig)];
1311 mtx_unlock(&ps->ps_mtx);
1312 ktrpsig(sig, action, &td->td_sigmask, ksi->ksi_code);
1313 }
1314 #endif
1315 if (sig == SIGKILL)
1316 sigexit(td, sig);
1317 }
1318 PROC_UNLOCK(p);
1319 return (error);
1320 }
1321
1322 #ifndef _SYS_SYSPROTO_H_
1323 struct sigpending_args {
1324 sigset_t *set;
1325 };
1326 #endif
1327 int
sys_sigpending(td,uap)1328 sys_sigpending(td, uap)
1329 struct thread *td;
1330 struct sigpending_args *uap;
1331 {
1332 struct proc *p = td->td_proc;
1333 sigset_t pending;
1334
1335 PROC_LOCK(p);
1336 pending = p->p_sigqueue.sq_signals;
1337 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1338 PROC_UNLOCK(p);
1339 return (copyout(&pending, uap->set, sizeof(sigset_t)));
1340 }
1341
1342 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1343 #ifndef _SYS_SYSPROTO_H_
1344 struct osigpending_args {
1345 int dummy;
1346 };
1347 #endif
1348 int
osigpending(td,uap)1349 osigpending(td, uap)
1350 struct thread *td;
1351 struct osigpending_args *uap;
1352 {
1353 struct proc *p = td->td_proc;
1354 sigset_t pending;
1355
1356 PROC_LOCK(p);
1357 pending = p->p_sigqueue.sq_signals;
1358 SIGSETOR(pending, td->td_sigqueue.sq_signals);
1359 PROC_UNLOCK(p);
1360 SIG2OSIG(pending, td->td_retval[0]);
1361 return (0);
1362 }
1363 #endif /* COMPAT_43 */
1364
1365 #if defined(COMPAT_43)
1366 /*
1367 * Generalized interface signal handler, 4.3-compatible.
1368 */
1369 #ifndef _SYS_SYSPROTO_H_
1370 struct osigvec_args {
1371 int signum;
1372 struct sigvec *nsv;
1373 struct sigvec *osv;
1374 };
1375 #endif
1376 /* ARGSUSED */
1377 int
osigvec(td,uap)1378 osigvec(td, uap)
1379 struct thread *td;
1380 register struct osigvec_args *uap;
1381 {
1382 struct sigvec vec;
1383 struct sigaction nsa, osa;
1384 register struct sigaction *nsap, *osap;
1385 int error;
1386
1387 if (uap->signum <= 0 || uap->signum >= ONSIG)
1388 return (EINVAL);
1389 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1390 osap = (uap->osv != NULL) ? &osa : NULL;
1391 if (nsap) {
1392 error = copyin(uap->nsv, &vec, sizeof(vec));
1393 if (error)
1394 return (error);
1395 nsap->sa_handler = vec.sv_handler;
1396 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1397 nsap->sa_flags = vec.sv_flags;
1398 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1399 }
1400 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1401 if (osap && !error) {
1402 vec.sv_handler = osap->sa_handler;
1403 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1404 vec.sv_flags = osap->sa_flags;
1405 vec.sv_flags &= ~SA_NOCLDWAIT;
1406 vec.sv_flags ^= SA_RESTART;
1407 error = copyout(&vec, uap->osv, sizeof(vec));
1408 }
1409 return (error);
1410 }
1411
1412 #ifndef _SYS_SYSPROTO_H_
1413 struct osigblock_args {
1414 int mask;
1415 };
1416 #endif
1417 int
osigblock(td,uap)1418 osigblock(td, uap)
1419 register struct thread *td;
1420 struct osigblock_args *uap;
1421 {
1422 sigset_t set, oset;
1423
1424 OSIG2SIG(uap->mask, set);
1425 kern_sigprocmask(td, SIG_BLOCK, &set, &oset, 0);
1426 SIG2OSIG(oset, td->td_retval[0]);
1427 return (0);
1428 }
1429
1430 #ifndef _SYS_SYSPROTO_H_
1431 struct osigsetmask_args {
1432 int mask;
1433 };
1434 #endif
1435 int
osigsetmask(td,uap)1436 osigsetmask(td, uap)
1437 struct thread *td;
1438 struct osigsetmask_args *uap;
1439 {
1440 sigset_t set, oset;
1441
1442 OSIG2SIG(uap->mask, set);
1443 kern_sigprocmask(td, SIG_SETMASK, &set, &oset, 0);
1444 SIG2OSIG(oset, td->td_retval[0]);
1445 return (0);
1446 }
1447 #endif /* COMPAT_43 */
1448
1449 /*
1450 * Suspend calling thread until signal, providing mask to be set in the
1451 * meantime.
1452 */
1453 #ifndef _SYS_SYSPROTO_H_
1454 struct sigsuspend_args {
1455 const sigset_t *sigmask;
1456 };
1457 #endif
1458 /* ARGSUSED */
1459 int
sys_sigsuspend(td,uap)1460 sys_sigsuspend(td, uap)
1461 struct thread *td;
1462 struct sigsuspend_args *uap;
1463 {
1464 sigset_t mask;
1465 int error;
1466
1467 error = copyin(uap->sigmask, &mask, sizeof(mask));
1468 if (error)
1469 return (error);
1470 return (kern_sigsuspend(td, mask));
1471 }
1472
1473 int
kern_sigsuspend(struct thread * td,sigset_t mask)1474 kern_sigsuspend(struct thread *td, sigset_t mask)
1475 {
1476 struct proc *p = td->td_proc;
1477 int has_sig, sig;
1478
1479 /*
1480 * When returning from sigsuspend, we want
1481 * the old mask to be restored after the
1482 * signal handler has finished. Thus, we
1483 * save it here and mark the sigacts structure
1484 * to indicate this.
1485 */
1486 PROC_LOCK(p);
1487 kern_sigprocmask(td, SIG_SETMASK, &mask, &td->td_oldsigmask,
1488 SIGPROCMASK_PROC_LOCKED);
1489 td->td_pflags |= TDP_OLDMASK;
1490
1491 /*
1492 * Process signals now. Otherwise, we can get spurious wakeup
1493 * due to signal entered process queue, but delivered to other
1494 * thread. But sigsuspend should return only on signal
1495 * delivery.
1496 */
1497 (p->p_sysent->sv_set_syscall_retval)(td, EINTR);
1498 for (has_sig = 0; !has_sig;) {
1499 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause",
1500 0) == 0)
1501 /* void */;
1502 thread_suspend_check(0);
1503 mtx_lock(&p->p_sigacts->ps_mtx);
1504 while ((sig = cursig(td)) != 0)
1505 has_sig += postsig(sig);
1506 mtx_unlock(&p->p_sigacts->ps_mtx);
1507 }
1508 PROC_UNLOCK(p);
1509 td->td_errno = EINTR;
1510 td->td_pflags |= TDP_NERRNO;
1511 return (EJUSTRETURN);
1512 }
1513
1514 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1515 /*
1516 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1517 * convention: libc stub passes mask, not pointer, to save a copyin.
1518 */
1519 #ifndef _SYS_SYSPROTO_H_
1520 struct osigsuspend_args {
1521 osigset_t mask;
1522 };
1523 #endif
1524 /* ARGSUSED */
1525 int
osigsuspend(td,uap)1526 osigsuspend(td, uap)
1527 struct thread *td;
1528 struct osigsuspend_args *uap;
1529 {
1530 sigset_t mask;
1531
1532 OSIG2SIG(uap->mask, mask);
1533 return (kern_sigsuspend(td, mask));
1534 }
1535 #endif /* COMPAT_43 */
1536
1537 #if defined(COMPAT_43)
1538 #ifndef _SYS_SYSPROTO_H_
1539 struct osigstack_args {
1540 struct sigstack *nss;
1541 struct sigstack *oss;
1542 };
1543 #endif
1544 /* ARGSUSED */
1545 int
osigstack(td,uap)1546 osigstack(td, uap)
1547 struct thread *td;
1548 register struct osigstack_args *uap;
1549 {
1550 struct sigstack nss, oss;
1551 int error = 0;
1552
1553 if (uap->nss != NULL) {
1554 error = copyin(uap->nss, &nss, sizeof(nss));
1555 if (error)
1556 return (error);
1557 }
1558 oss.ss_sp = td->td_sigstk.ss_sp;
1559 oss.ss_onstack = sigonstack(cpu_getstack(td));
1560 if (uap->nss != NULL) {
1561 td->td_sigstk.ss_sp = nss.ss_sp;
1562 td->td_sigstk.ss_size = 0;
1563 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1564 td->td_pflags |= TDP_ALTSTACK;
1565 }
1566 if (uap->oss != NULL)
1567 error = copyout(&oss, uap->oss, sizeof(oss));
1568
1569 return (error);
1570 }
1571 #endif /* COMPAT_43 */
1572
1573 #ifndef _SYS_SYSPROTO_H_
1574 struct sigaltstack_args {
1575 stack_t *ss;
1576 stack_t *oss;
1577 };
1578 #endif
1579 /* ARGSUSED */
1580 int
sys_sigaltstack(td,uap)1581 sys_sigaltstack(td, uap)
1582 struct thread *td;
1583 register struct sigaltstack_args *uap;
1584 {
1585 stack_t ss, oss;
1586 int error;
1587
1588 if (uap->ss != NULL) {
1589 error = copyin(uap->ss, &ss, sizeof(ss));
1590 if (error)
1591 return (error);
1592 }
1593 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1594 (uap->oss != NULL) ? &oss : NULL);
1595 if (error)
1596 return (error);
1597 if (uap->oss != NULL)
1598 error = copyout(&oss, uap->oss, sizeof(stack_t));
1599 return (error);
1600 }
1601
1602 int
kern_sigaltstack(struct thread * td,stack_t * ss,stack_t * oss)1603 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1604 {
1605 struct proc *p = td->td_proc;
1606 int oonstack;
1607
1608 oonstack = sigonstack(cpu_getstack(td));
1609
1610 if (oss != NULL) {
1611 *oss = td->td_sigstk;
1612 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1613 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1614 }
1615
1616 if (ss != NULL) {
1617 if (oonstack)
1618 return (EPERM);
1619 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1620 return (EINVAL);
1621 if (!(ss->ss_flags & SS_DISABLE)) {
1622 if (ss->ss_size < p->p_sysent->sv_minsigstksz)
1623 return (ENOMEM);
1624
1625 td->td_sigstk = *ss;
1626 td->td_pflags |= TDP_ALTSTACK;
1627 } else {
1628 td->td_pflags &= ~TDP_ALTSTACK;
1629 }
1630 }
1631 return (0);
1632 }
1633
1634 /*
1635 * Common code for kill process group/broadcast kill.
1636 * cp is calling process.
1637 */
1638 static int
killpg1(struct thread * td,int sig,int pgid,int all,ksiginfo_t * ksi)1639 killpg1(struct thread *td, int sig, int pgid, int all, ksiginfo_t *ksi)
1640 {
1641 struct proc *p;
1642 struct pgrp *pgrp;
1643 int err;
1644 int ret;
1645
1646 ret = ESRCH;
1647 if (all) {
1648 /*
1649 * broadcast
1650 */
1651 sx_slock(&allproc_lock);
1652 FOREACH_PROC_IN_SYSTEM(p) {
1653 PROC_LOCK(p);
1654 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1655 p == td->td_proc || p->p_state == PRS_NEW) {
1656 PROC_UNLOCK(p);
1657 continue;
1658 }
1659 err = p_cansignal(td, p, sig);
1660 if (err == 0) {
1661 if (sig)
1662 pksignal(p, sig, ksi);
1663 ret = err;
1664 }
1665 else if (ret == ESRCH)
1666 ret = err;
1667 PROC_UNLOCK(p);
1668 }
1669 sx_sunlock(&allproc_lock);
1670 } else {
1671 sx_slock(&proctree_lock);
1672 if (pgid == 0) {
1673 /*
1674 * zero pgid means send to my process group.
1675 */
1676 pgrp = td->td_proc->p_pgrp;
1677 PGRP_LOCK(pgrp);
1678 } else {
1679 pgrp = pgfind(pgid);
1680 if (pgrp == NULL) {
1681 sx_sunlock(&proctree_lock);
1682 return (ESRCH);
1683 }
1684 }
1685 sx_sunlock(&proctree_lock);
1686 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1687 PROC_LOCK(p);
1688 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1689 p->p_state == PRS_NEW) {
1690 PROC_UNLOCK(p);
1691 continue;
1692 }
1693 err = p_cansignal(td, p, sig);
1694 if (err == 0) {
1695 if (sig)
1696 pksignal(p, sig, ksi);
1697 ret = err;
1698 }
1699 else if (ret == ESRCH)
1700 ret = err;
1701 PROC_UNLOCK(p);
1702 }
1703 PGRP_UNLOCK(pgrp);
1704 }
1705 return (ret);
1706 }
1707
1708 #ifndef _SYS_SYSPROTO_H_
1709 struct kill_args {
1710 int pid;
1711 int signum;
1712 };
1713 #endif
1714 /* ARGSUSED */
1715 int
sys_kill(struct thread * td,struct kill_args * uap)1716 sys_kill(struct thread *td, struct kill_args *uap)
1717 {
1718 ksiginfo_t ksi;
1719 struct proc *p;
1720 int error;
1721
1722 /*
1723 * A process in capability mode can send signals only to himself.
1724 * The main rationale behind this is that abort(3) is implemented as
1725 * kill(getpid(), SIGABRT).
1726 */
1727 if (IN_CAPABILITY_MODE(td) && uap->pid != td->td_proc->p_pid)
1728 return (ECAPMODE);
1729
1730 AUDIT_ARG_SIGNUM(uap->signum);
1731 AUDIT_ARG_PID(uap->pid);
1732 if ((u_int)uap->signum > _SIG_MAXSIG)
1733 return (EINVAL);
1734
1735 ksiginfo_init(&ksi);
1736 ksi.ksi_signo = uap->signum;
1737 ksi.ksi_code = SI_USER;
1738 ksi.ksi_pid = td->td_proc->p_pid;
1739 ksi.ksi_uid = td->td_ucred->cr_ruid;
1740
1741 if (uap->pid > 0) {
1742 /* kill single process */
1743 if ((p = pfind(uap->pid)) == NULL) {
1744 if ((p = zpfind(uap->pid)) == NULL)
1745 return (ESRCH);
1746 }
1747 AUDIT_ARG_PROCESS(p);
1748 error = p_cansignal(td, p, uap->signum);
1749 if (error == 0 && uap->signum)
1750 pksignal(p, uap->signum, &ksi);
1751 PROC_UNLOCK(p);
1752 return (error);
1753 }
1754 switch (uap->pid) {
1755 case -1: /* broadcast signal */
1756 return (killpg1(td, uap->signum, 0, 1, &ksi));
1757 case 0: /* signal own process group */
1758 return (killpg1(td, uap->signum, 0, 0, &ksi));
1759 default: /* negative explicit process group */
1760 return (killpg1(td, uap->signum, -uap->pid, 0, &ksi));
1761 }
1762 /* NOTREACHED */
1763 }
1764
1765 int
sys_pdkill(td,uap)1766 sys_pdkill(td, uap)
1767 struct thread *td;
1768 struct pdkill_args *uap;
1769 {
1770 #ifdef PROCDESC
1771 struct proc *p;
1772 cap_rights_t rights;
1773 int error;
1774
1775 AUDIT_ARG_SIGNUM(uap->signum);
1776 AUDIT_ARG_FD(uap->fd);
1777 if ((u_int)uap->signum > _SIG_MAXSIG)
1778 return (EINVAL);
1779
1780 error = procdesc_find(td, uap->fd,
1781 cap_rights_init(&rights, CAP_PDKILL), &p);
1782 if (error)
1783 return (error);
1784 AUDIT_ARG_PROCESS(p);
1785 error = p_cansignal(td, p, uap->signum);
1786 if (error == 0 && uap->signum)
1787 kern_psignal(p, uap->signum);
1788 PROC_UNLOCK(p);
1789 return (error);
1790 #else
1791 return (ENOSYS);
1792 #endif
1793 }
1794
1795 #if defined(COMPAT_43)
1796 #ifndef _SYS_SYSPROTO_H_
1797 struct okillpg_args {
1798 int pgid;
1799 int signum;
1800 };
1801 #endif
1802 /* ARGSUSED */
1803 int
okillpg(struct thread * td,struct okillpg_args * uap)1804 okillpg(struct thread *td, struct okillpg_args *uap)
1805 {
1806 ksiginfo_t ksi;
1807
1808 AUDIT_ARG_SIGNUM(uap->signum);
1809 AUDIT_ARG_PID(uap->pgid);
1810 if ((u_int)uap->signum > _SIG_MAXSIG)
1811 return (EINVAL);
1812
1813 ksiginfo_init(&ksi);
1814 ksi.ksi_signo = uap->signum;
1815 ksi.ksi_code = SI_USER;
1816 ksi.ksi_pid = td->td_proc->p_pid;
1817 ksi.ksi_uid = td->td_ucred->cr_ruid;
1818 return (killpg1(td, uap->signum, uap->pgid, 0, &ksi));
1819 }
1820 #endif /* COMPAT_43 */
1821
1822 #ifndef _SYS_SYSPROTO_H_
1823 struct sigqueue_args {
1824 pid_t pid;
1825 int signum;
1826 /* union sigval */ void *value;
1827 };
1828 #endif
1829 int
sys_sigqueue(struct thread * td,struct sigqueue_args * uap)1830 sys_sigqueue(struct thread *td, struct sigqueue_args *uap)
1831 {
1832 ksiginfo_t ksi;
1833 struct proc *p;
1834 int error;
1835
1836 if ((u_int)uap->signum > _SIG_MAXSIG)
1837 return (EINVAL);
1838
1839 /*
1840 * Specification says sigqueue can only send signal to
1841 * single process.
1842 */
1843 if (uap->pid <= 0)
1844 return (EINVAL);
1845
1846 if ((p = pfind(uap->pid)) == NULL) {
1847 if ((p = zpfind(uap->pid)) == NULL)
1848 return (ESRCH);
1849 }
1850 error = p_cansignal(td, p, uap->signum);
1851 if (error == 0 && uap->signum != 0) {
1852 ksiginfo_init(&ksi);
1853 ksi.ksi_flags = KSI_SIGQ;
1854 ksi.ksi_signo = uap->signum;
1855 ksi.ksi_code = SI_QUEUE;
1856 ksi.ksi_pid = td->td_proc->p_pid;
1857 ksi.ksi_uid = td->td_ucred->cr_ruid;
1858 ksi.ksi_value.sival_ptr = uap->value;
1859 error = pksignal(p, ksi.ksi_signo, &ksi);
1860 }
1861 PROC_UNLOCK(p);
1862 return (error);
1863 }
1864
1865 /*
1866 * Send a signal to a process group.
1867 */
1868 void
gsignal(int pgid,int sig,ksiginfo_t * ksi)1869 gsignal(int pgid, int sig, ksiginfo_t *ksi)
1870 {
1871 struct pgrp *pgrp;
1872
1873 if (pgid != 0) {
1874 sx_slock(&proctree_lock);
1875 pgrp = pgfind(pgid);
1876 sx_sunlock(&proctree_lock);
1877 if (pgrp != NULL) {
1878 pgsignal(pgrp, sig, 0, ksi);
1879 PGRP_UNLOCK(pgrp);
1880 }
1881 }
1882 }
1883
1884 /*
1885 * Send a signal to a process group. If checktty is 1,
1886 * limit to members which have a controlling terminal.
1887 */
1888 void
pgsignal(struct pgrp * pgrp,int sig,int checkctty,ksiginfo_t * ksi)1889 pgsignal(struct pgrp *pgrp, int sig, int checkctty, ksiginfo_t *ksi)
1890 {
1891 struct proc *p;
1892
1893 if (pgrp) {
1894 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1895 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1896 PROC_LOCK(p);
1897 if (p->p_state == PRS_NORMAL &&
1898 (checkctty == 0 || p->p_flag & P_CONTROLT))
1899 pksignal(p, sig, ksi);
1900 PROC_UNLOCK(p);
1901 }
1902 }
1903 }
1904
1905
1906 /*
1907 * Recalculate the signal mask and reset the signal disposition after
1908 * usermode frame for delivery is formed. Should be called after
1909 * mach-specific routine, because sysent->sv_sendsig() needs correct
1910 * ps_siginfo and signal mask.
1911 */
1912 static void
postsig_done(int sig,struct thread * td,struct sigacts * ps)1913 postsig_done(int sig, struct thread *td, struct sigacts *ps)
1914 {
1915 sigset_t mask;
1916
1917 mtx_assert(&ps->ps_mtx, MA_OWNED);
1918 td->td_ru.ru_nsignals++;
1919 mask = ps->ps_catchmask[_SIG_IDX(sig)];
1920 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1921 SIGADDSET(mask, sig);
1922 kern_sigprocmask(td, SIG_BLOCK, &mask, NULL,
1923 SIGPROCMASK_PROC_LOCKED | SIGPROCMASK_PS_LOCKED);
1924 if (SIGISMEMBER(ps->ps_sigreset, sig))
1925 sigdflt(ps, sig);
1926 }
1927
1928
1929 /*
1930 * Send a signal caused by a trap to the current thread. If it will be
1931 * caught immediately, deliver it with correct code. Otherwise, post it
1932 * normally.
1933 */
1934 void
trapsignal(struct thread * td,ksiginfo_t * ksi)1935 trapsignal(struct thread *td, ksiginfo_t *ksi)
1936 {
1937 struct sigacts *ps;
1938 struct proc *p;
1939 int sig;
1940 int code;
1941
1942 p = td->td_proc;
1943 sig = ksi->ksi_signo;
1944 code = ksi->ksi_code;
1945 KASSERT(_SIG_VALID(sig), ("invalid signal"));
1946
1947 PROC_LOCK(p);
1948 ps = p->p_sigacts;
1949 mtx_lock(&ps->ps_mtx);
1950 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1951 !SIGISMEMBER(td->td_sigmask, sig)) {
1952 #ifdef KTRACE
1953 if (KTRPOINT(curthread, KTR_PSIG))
1954 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1955 &td->td_sigmask, code);
1956 #endif
1957 (*p->p_sysent->sv_sendsig)(ps->ps_sigact[_SIG_IDX(sig)],
1958 ksi, &td->td_sigmask);
1959 postsig_done(sig, td, ps);
1960 mtx_unlock(&ps->ps_mtx);
1961 } else {
1962 /*
1963 * Avoid a possible infinite loop if the thread
1964 * masking the signal or process is ignoring the
1965 * signal.
1966 */
1967 if (kern_forcesigexit &&
1968 (SIGISMEMBER(td->td_sigmask, sig) ||
1969 ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN)) {
1970 SIGDELSET(td->td_sigmask, sig);
1971 SIGDELSET(ps->ps_sigcatch, sig);
1972 SIGDELSET(ps->ps_sigignore, sig);
1973 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1974 }
1975 mtx_unlock(&ps->ps_mtx);
1976 p->p_code = code; /* XXX for core dump/debugger */
1977 p->p_sig = sig; /* XXX to verify code */
1978 tdsendsignal(p, td, sig, ksi);
1979 }
1980 PROC_UNLOCK(p);
1981 }
1982
1983 static struct thread *
sigtd(struct proc * p,int sig,int prop)1984 sigtd(struct proc *p, int sig, int prop)
1985 {
1986 struct thread *td, *signal_td;
1987
1988 PROC_LOCK_ASSERT(p, MA_OWNED);
1989
1990 /*
1991 * Check if current thread can handle the signal without
1992 * switching context to another thread.
1993 */
1994 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1995 return (curthread);
1996 signal_td = NULL;
1997 FOREACH_THREAD_IN_PROC(p, td) {
1998 if (!SIGISMEMBER(td->td_sigmask, sig)) {
1999 signal_td = td;
2000 break;
2001 }
2002 }
2003 if (signal_td == NULL)
2004 signal_td = FIRST_THREAD_IN_PROC(p);
2005 return (signal_td);
2006 }
2007
2008 /*
2009 * Send the signal to the process. If the signal has an action, the action
2010 * is usually performed by the target process rather than the caller; we add
2011 * the signal to the set of pending signals for the process.
2012 *
2013 * Exceptions:
2014 * o When a stop signal is sent to a sleeping process that takes the
2015 * default action, the process is stopped without awakening it.
2016 * o SIGCONT restarts stopped processes (or puts them back to sleep)
2017 * regardless of the signal action (eg, blocked or ignored).
2018 *
2019 * Other ignored signals are discarded immediately.
2020 *
2021 * NB: This function may be entered from the debugger via the "kill" DDB
2022 * command. There is little that can be done to mitigate the possibly messy
2023 * side effects of this unwise possibility.
2024 */
2025 void
kern_psignal(struct proc * p,int sig)2026 kern_psignal(struct proc *p, int sig)
2027 {
2028 ksiginfo_t ksi;
2029
2030 ksiginfo_init(&ksi);
2031 ksi.ksi_signo = sig;
2032 ksi.ksi_code = SI_KERNEL;
2033 (void) tdsendsignal(p, NULL, sig, &ksi);
2034 }
2035
2036 int
pksignal(struct proc * p,int sig,ksiginfo_t * ksi)2037 pksignal(struct proc *p, int sig, ksiginfo_t *ksi)
2038 {
2039
2040 return (tdsendsignal(p, NULL, sig, ksi));
2041 }
2042
2043 /* Utility function for finding a thread to send signal event to. */
2044 int
sigev_findtd(struct proc * p,struct sigevent * sigev,struct thread ** ttd)2045 sigev_findtd(struct proc *p ,struct sigevent *sigev, struct thread **ttd)
2046 {
2047 struct thread *td;
2048
2049 if (sigev->sigev_notify == SIGEV_THREAD_ID) {
2050 td = tdfind(sigev->sigev_notify_thread_id, p->p_pid);
2051 if (td == NULL)
2052 return (ESRCH);
2053 *ttd = td;
2054 } else {
2055 *ttd = NULL;
2056 PROC_LOCK(p);
2057 }
2058 return (0);
2059 }
2060
2061 void
tdsignal(struct thread * td,int sig)2062 tdsignal(struct thread *td, int sig)
2063 {
2064 ksiginfo_t ksi;
2065
2066 ksiginfo_init(&ksi);
2067 ksi.ksi_signo = sig;
2068 ksi.ksi_code = SI_KERNEL;
2069 (void) tdsendsignal(td->td_proc, td, sig, &ksi);
2070 }
2071
2072 void
tdksignal(struct thread * td,int sig,ksiginfo_t * ksi)2073 tdksignal(struct thread *td, int sig, ksiginfo_t *ksi)
2074 {
2075
2076 (void) tdsendsignal(td->td_proc, td, sig, ksi);
2077 }
2078
2079 int
tdsendsignal(struct proc * p,struct thread * td,int sig,ksiginfo_t * ksi)2080 tdsendsignal(struct proc *p, struct thread *td, int sig, ksiginfo_t *ksi)
2081 {
2082 sig_t action;
2083 sigqueue_t *sigqueue;
2084 int prop;
2085 struct sigacts *ps;
2086 int intrval;
2087 int ret = 0;
2088 int wakeup_swapper;
2089
2090 MPASS(td == NULL || p == td->td_proc);
2091 PROC_LOCK_ASSERT(p, MA_OWNED);
2092
2093 if (!_SIG_VALID(sig))
2094 panic("%s(): invalid signal %d", __func__, sig);
2095
2096 KASSERT(ksi == NULL || !KSI_ONQ(ksi), ("%s: ksi on queue", __func__));
2097
2098 /*
2099 * IEEE Std 1003.1-2001: return success when killing a zombie.
2100 */
2101 if (p->p_state == PRS_ZOMBIE) {
2102 if (ksi && (ksi->ksi_flags & KSI_INS))
2103 ksiginfo_tryfree(ksi);
2104 return (ret);
2105 }
2106
2107 ps = p->p_sigacts;
2108 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
2109 prop = __sigprop(sig);
2110
2111 if (td == NULL) {
2112 td = sigtd(p, sig, prop);
2113 sigqueue = &p->p_sigqueue;
2114 } else
2115 sigqueue = &td->td_sigqueue;
2116
2117 SDT_PROBE(proc, kernel, , signal__send, td, p, sig, 0, 0 );
2118
2119 /*
2120 * If the signal is being ignored,
2121 * then we forget about it immediately.
2122 * (Note: we don't set SIGCONT in ps_sigignore,
2123 * and if it is set to SIG_IGN,
2124 * action will be SIG_DFL here.)
2125 */
2126 mtx_lock(&ps->ps_mtx);
2127 if (SIGISMEMBER(ps->ps_sigignore, sig)) {
2128 SDT_PROBE(proc, kernel, , signal__discard, td, p, sig, 0, 0 );
2129
2130 mtx_unlock(&ps->ps_mtx);
2131 if (ksi && (ksi->ksi_flags & KSI_INS))
2132 ksiginfo_tryfree(ksi);
2133 return (ret);
2134 }
2135 if (SIGISMEMBER(td->td_sigmask, sig))
2136 action = SIG_HOLD;
2137 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
2138 action = SIG_CATCH;
2139 else
2140 action = SIG_DFL;
2141 if (SIGISMEMBER(ps->ps_sigintr, sig))
2142 intrval = EINTR;
2143 else
2144 intrval = ERESTART;
2145 mtx_unlock(&ps->ps_mtx);
2146
2147 if (prop & SA_CONT)
2148 sigqueue_delete_stopmask_proc(p);
2149 else if (prop & SA_STOP) {
2150 /*
2151 * If sending a tty stop signal to a member of an orphaned
2152 * process group, discard the signal here if the action
2153 * is default; don't stop the process below if sleeping,
2154 * and don't clear any pending SIGCONT.
2155 */
2156 if ((prop & SA_TTYSTOP) &&
2157 (p->p_pgrp->pg_jobc == 0) &&
2158 (action == SIG_DFL)) {
2159 if (ksi && (ksi->ksi_flags & KSI_INS))
2160 ksiginfo_tryfree(ksi);
2161 return (ret);
2162 }
2163 sigqueue_delete_proc(p, SIGCONT);
2164 if (p->p_flag & P_CONTINUED) {
2165 p->p_flag &= ~P_CONTINUED;
2166 PROC_LOCK(p->p_pptr);
2167 sigqueue_take(p->p_ksi);
2168 PROC_UNLOCK(p->p_pptr);
2169 }
2170 }
2171
2172 ret = sigqueue_add(sigqueue, sig, ksi);
2173 if (ret != 0)
2174 return (ret);
2175 signotify(td);
2176 /*
2177 * Defer further processing for signals which are held,
2178 * except that stopped processes must be continued by SIGCONT.
2179 */
2180 if (action == SIG_HOLD &&
2181 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
2182 return (ret);
2183 /*
2184 * SIGKILL: Remove procfs STOPEVENTs.
2185 */
2186 if (sig == SIGKILL) {
2187 /* from procfs_ioctl.c: PIOCBIC */
2188 p->p_stops = 0;
2189 /* from procfs_ioctl.c: PIOCCONT */
2190 p->p_step = 0;
2191 wakeup(&p->p_step);
2192 }
2193 /*
2194 * Some signals have a process-wide effect and a per-thread
2195 * component. Most processing occurs when the process next
2196 * tries to cross the user boundary, however there are some
2197 * times when processing needs to be done immediately, such as
2198 * waking up threads so that they can cross the user boundary.
2199 * We try to do the per-process part here.
2200 */
2201 if (P_SHOULDSTOP(p)) {
2202 KASSERT(!(p->p_flag & P_WEXIT),
2203 ("signal to stopped but exiting process"));
2204 if (sig == SIGKILL) {
2205 /*
2206 * If traced process is already stopped,
2207 * then no further action is necessary.
2208 */
2209 if (p->p_flag & P_TRACED)
2210 goto out;
2211 /*
2212 * SIGKILL sets process running.
2213 * It will die elsewhere.
2214 * All threads must be restarted.
2215 */
2216 p->p_flag &= ~P_STOPPED_SIG;
2217 goto runfast;
2218 }
2219
2220 if (prop & SA_CONT) {
2221 /*
2222 * If traced process is already stopped,
2223 * then no further action is necessary.
2224 */
2225 if (p->p_flag & P_TRACED)
2226 goto out;
2227 /*
2228 * If SIGCONT is default (or ignored), we continue the
2229 * process but don't leave the signal in sigqueue as
2230 * it has no further action. If SIGCONT is held, we
2231 * continue the process and leave the signal in
2232 * sigqueue. If the process catches SIGCONT, let it
2233 * handle the signal itself. If it isn't waiting on
2234 * an event, it goes back to run state.
2235 * Otherwise, process goes back to sleep state.
2236 */
2237 p->p_flag &= ~P_STOPPED_SIG;
2238 PROC_SLOCK(p);
2239 if (p->p_numthreads == p->p_suspcount) {
2240 PROC_SUNLOCK(p);
2241 p->p_flag |= P_CONTINUED;
2242 p->p_xstat = SIGCONT;
2243 PROC_LOCK(p->p_pptr);
2244 childproc_continued(p);
2245 PROC_UNLOCK(p->p_pptr);
2246 PROC_SLOCK(p);
2247 }
2248 if (action == SIG_DFL) {
2249 thread_unsuspend(p);
2250 PROC_SUNLOCK(p);
2251 sigqueue_delete(sigqueue, sig);
2252 goto out;
2253 }
2254 if (action == SIG_CATCH) {
2255 /*
2256 * The process wants to catch it so it needs
2257 * to run at least one thread, but which one?
2258 */
2259 PROC_SUNLOCK(p);
2260 goto runfast;
2261 }
2262 /*
2263 * The signal is not ignored or caught.
2264 */
2265 thread_unsuspend(p);
2266 PROC_SUNLOCK(p);
2267 goto out;
2268 }
2269
2270 if (prop & SA_STOP) {
2271 /*
2272 * If traced process is already stopped,
2273 * then no further action is necessary.
2274 */
2275 if (p->p_flag & P_TRACED)
2276 goto out;
2277 /*
2278 * Already stopped, don't need to stop again
2279 * (If we did the shell could get confused).
2280 * Just make sure the signal STOP bit set.
2281 */
2282 p->p_flag |= P_STOPPED_SIG;
2283 sigqueue_delete(sigqueue, sig);
2284 goto out;
2285 }
2286
2287 /*
2288 * All other kinds of signals:
2289 * If a thread is sleeping interruptibly, simulate a
2290 * wakeup so that when it is continued it will be made
2291 * runnable and can look at the signal. However, don't make
2292 * the PROCESS runnable, leave it stopped.
2293 * It may run a bit until it hits a thread_suspend_check().
2294 */
2295 wakeup_swapper = 0;
2296 PROC_SLOCK(p);
2297 thread_lock(td);
2298 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
2299 wakeup_swapper = sleepq_abort(td, intrval);
2300 thread_unlock(td);
2301 PROC_SUNLOCK(p);
2302 if (wakeup_swapper)
2303 kick_proc0();
2304 goto out;
2305 /*
2306 * Mutexes are short lived. Threads waiting on them will
2307 * hit thread_suspend_check() soon.
2308 */
2309 } else if (p->p_state == PRS_NORMAL) {
2310 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
2311 tdsigwakeup(td, sig, action, intrval);
2312 goto out;
2313 }
2314
2315 MPASS(action == SIG_DFL);
2316
2317 if (prop & SA_STOP) {
2318 if (p->p_flag & (P_PPWAIT|P_WEXIT))
2319 goto out;
2320 p->p_flag |= P_STOPPED_SIG;
2321 p->p_xstat = sig;
2322 PROC_SLOCK(p);
2323 sig_suspend_threads(td, p, 1);
2324 if (p->p_numthreads == p->p_suspcount) {
2325 /*
2326 * only thread sending signal to another
2327 * process can reach here, if thread is sending
2328 * signal to its process, because thread does
2329 * not suspend itself here, p_numthreads
2330 * should never be equal to p_suspcount.
2331 */
2332 thread_stopped(p);
2333 PROC_SUNLOCK(p);
2334 sigqueue_delete_proc(p, p->p_xstat);
2335 } else
2336 PROC_SUNLOCK(p);
2337 goto out;
2338 }
2339 } else {
2340 /* Not in "NORMAL" state. discard the signal. */
2341 sigqueue_delete(sigqueue, sig);
2342 goto out;
2343 }
2344
2345 /*
2346 * The process is not stopped so we need to apply the signal to all the
2347 * running threads.
2348 */
2349 runfast:
2350 tdsigwakeup(td, sig, action, intrval);
2351 PROC_SLOCK(p);
2352 thread_unsuspend(p);
2353 PROC_SUNLOCK(p);
2354 out:
2355 /* If we jump here, proc slock should not be owned. */
2356 PROC_SLOCK_ASSERT(p, MA_NOTOWNED);
2357 return (ret);
2358 }
2359
2360 /*
2361 * The force of a signal has been directed against a single
2362 * thread. We need to see what we can do about knocking it
2363 * out of any sleep it may be in etc.
2364 */
2365 static void
tdsigwakeup(struct thread * td,int sig,sig_t action,int intrval)2366 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
2367 {
2368 struct proc *p = td->td_proc;
2369 register int prop;
2370 int wakeup_swapper;
2371
2372 wakeup_swapper = 0;
2373 PROC_LOCK_ASSERT(p, MA_OWNED);
2374 prop = __sigprop(sig);
2375
2376 PROC_SLOCK(p);
2377 thread_lock(td);
2378 /*
2379 * Bring the priority of a thread up if we want it to get
2380 * killed in this lifetime. Be careful to avoid bumping the
2381 * priority of the idle thread, since we still allow to signal
2382 * kernel processes.
2383 */
2384 if (action == SIG_DFL && (prop & SA_KILL) != 0 &&
2385 td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2386 sched_prio(td, PUSER);
2387 if (TD_ON_SLEEPQ(td)) {
2388 /*
2389 * If thread is sleeping uninterruptibly
2390 * we can't interrupt the sleep... the signal will
2391 * be noticed when the process returns through
2392 * trap() or syscall().
2393 */
2394 if ((td->td_flags & TDF_SINTR) == 0)
2395 goto out;
2396 /*
2397 * If SIGCONT is default (or ignored) and process is
2398 * asleep, we are finished; the process should not
2399 * be awakened.
2400 */
2401 if ((prop & SA_CONT) && action == SIG_DFL) {
2402 thread_unlock(td);
2403 PROC_SUNLOCK(p);
2404 sigqueue_delete(&p->p_sigqueue, sig);
2405 /*
2406 * It may be on either list in this state.
2407 * Remove from both for now.
2408 */
2409 sigqueue_delete(&td->td_sigqueue, sig);
2410 return;
2411 }
2412
2413 /*
2414 * Don't awaken a sleeping thread for SIGSTOP if the
2415 * STOP signal is deferred.
2416 */
2417 if ((prop & SA_STOP) && (td->td_flags & TDF_SBDRY))
2418 goto out;
2419
2420 /*
2421 * Give low priority threads a better chance to run.
2422 */
2423 if (td->td_priority > PUSER && !TD_IS_IDLETHREAD(td))
2424 sched_prio(td, PUSER);
2425
2426 wakeup_swapper = sleepq_abort(td, intrval);
2427 } else {
2428 /*
2429 * Other states do nothing with the signal immediately,
2430 * other than kicking ourselves if we are running.
2431 * It will either never be noticed, or noticed very soon.
2432 */
2433 #ifdef SMP
2434 if (TD_IS_RUNNING(td) && td != curthread)
2435 forward_signal(td);
2436 #endif
2437 }
2438 out:
2439 PROC_SUNLOCK(p);
2440 thread_unlock(td);
2441 if (wakeup_swapper)
2442 kick_proc0();
2443 }
2444
2445 static void
sig_suspend_threads(struct thread * td,struct proc * p,int sending)2446 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2447 {
2448 struct thread *td2;
2449
2450 PROC_LOCK_ASSERT(p, MA_OWNED);
2451 PROC_SLOCK_ASSERT(p, MA_OWNED);
2452
2453 FOREACH_THREAD_IN_PROC(p, td2) {
2454 thread_lock(td2);
2455 td2->td_flags |= TDF_ASTPENDING | TDF_NEEDSUSPCHK;
2456 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2457 (td2->td_flags & TDF_SINTR)) {
2458 if (td2->td_flags & TDF_SBDRY) {
2459 /*
2460 * Once a thread is asleep with
2461 * TDF_SBDRY set, it should never
2462 * become suspended due to this check.
2463 */
2464 KASSERT(!TD_IS_SUSPENDED(td2),
2465 ("thread with deferred stops suspended"));
2466 } else if (!TD_IS_SUSPENDED(td2)) {
2467 thread_suspend_one(td2);
2468 }
2469 } else if (!TD_IS_SUSPENDED(td2)) {
2470 if (sending || td != td2)
2471 td2->td_flags |= TDF_ASTPENDING;
2472 #ifdef SMP
2473 if (TD_IS_RUNNING(td2) && td2 != td)
2474 forward_signal(td2);
2475 #endif
2476 }
2477 thread_unlock(td2);
2478 }
2479 }
2480
2481 int
ptracestop(struct thread * td,int sig)2482 ptracestop(struct thread *td, int sig)
2483 {
2484 struct proc *p = td->td_proc;
2485
2486 PROC_LOCK_ASSERT(p, MA_OWNED);
2487 KASSERT(!(p->p_flag & P_WEXIT), ("Stopping exiting process"));
2488 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2489 &p->p_mtx.lock_object, "Stopping for traced signal");
2490
2491 td->td_dbgflags |= TDB_XSIG;
2492 td->td_xsig = sig;
2493 PROC_SLOCK(p);
2494 while ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_XSIG)) {
2495 if (p->p_flag & P_SINGLE_EXIT) {
2496 td->td_dbgflags &= ~TDB_XSIG;
2497 PROC_SUNLOCK(p);
2498 return (sig);
2499 }
2500 /*
2501 * Just make wait() to work, the last stopped thread
2502 * will win.
2503 */
2504 p->p_xstat = sig;
2505 p->p_xthread = td;
2506 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2507 sig_suspend_threads(td, p, 0);
2508 if ((td->td_dbgflags & TDB_STOPATFORK) != 0) {
2509 td->td_dbgflags &= ~TDB_STOPATFORK;
2510 cv_broadcast(&p->p_dbgwait);
2511 }
2512 stopme:
2513 thread_suspend_switch(td, p);
2514 if (p->p_xthread == td)
2515 p->p_xthread = NULL;
2516 if (!(p->p_flag & P_TRACED))
2517 break;
2518 if (td->td_dbgflags & TDB_SUSPEND) {
2519 if (p->p_flag & P_SINGLE_EXIT)
2520 break;
2521 goto stopme;
2522 }
2523 }
2524 PROC_SUNLOCK(p);
2525 return (td->td_xsig);
2526 }
2527
2528 static void
reschedule_signals(struct proc * p,sigset_t block,int flags)2529 reschedule_signals(struct proc *p, sigset_t block, int flags)
2530 {
2531 struct sigacts *ps;
2532 struct thread *td;
2533 int sig;
2534
2535 PROC_LOCK_ASSERT(p, MA_OWNED);
2536 ps = p->p_sigacts;
2537 mtx_assert(&ps->ps_mtx, (flags & SIGPROCMASK_PS_LOCKED) != 0 ?
2538 MA_OWNED : MA_NOTOWNED);
2539 if (SIGISEMPTY(p->p_siglist))
2540 return;
2541 SIGSETAND(block, p->p_siglist);
2542 while ((sig = sig_ffs(&block)) != 0) {
2543 SIGDELSET(block, sig);
2544 td = sigtd(p, sig, 0);
2545 signotify(td);
2546 if (!(flags & SIGPROCMASK_PS_LOCKED))
2547 mtx_lock(&ps->ps_mtx);
2548 if (p->p_flag & P_TRACED || SIGISMEMBER(ps->ps_sigcatch, sig))
2549 tdsigwakeup(td, sig, SIG_CATCH,
2550 (SIGISMEMBER(ps->ps_sigintr, sig) ? EINTR :
2551 ERESTART));
2552 if (!(flags & SIGPROCMASK_PS_LOCKED))
2553 mtx_unlock(&ps->ps_mtx);
2554 }
2555 }
2556
2557 void
tdsigcleanup(struct thread * td)2558 tdsigcleanup(struct thread *td)
2559 {
2560 struct proc *p;
2561 sigset_t unblocked;
2562
2563 p = td->td_proc;
2564 PROC_LOCK_ASSERT(p, MA_OWNED);
2565
2566 sigqueue_flush(&td->td_sigqueue);
2567 if (p->p_numthreads == 1)
2568 return;
2569
2570 /*
2571 * Since we cannot handle signals, notify signal post code
2572 * about this by filling the sigmask.
2573 *
2574 * Also, if needed, wake up thread(s) that do not block the
2575 * same signals as the exiting thread, since the thread might
2576 * have been selected for delivery and woken up.
2577 */
2578 SIGFILLSET(unblocked);
2579 SIGSETNAND(unblocked, td->td_sigmask);
2580 SIGFILLSET(td->td_sigmask);
2581 reschedule_signals(p, unblocked, 0);
2582
2583 }
2584
2585 /*
2586 * Defer the delivery of SIGSTOP for the current thread. Returns true
2587 * if stops were deferred and false if they were already deferred.
2588 */
2589 int
sigdeferstop(void)2590 sigdeferstop(void)
2591 {
2592 struct thread *td;
2593
2594 td = curthread;
2595 if (td->td_flags & TDF_SBDRY)
2596 return (0);
2597 thread_lock(td);
2598 td->td_flags |= TDF_SBDRY;
2599 thread_unlock(td);
2600 return (1);
2601 }
2602
2603 /*
2604 * Permit the delivery of SIGSTOP for the current thread. This does
2605 * not immediately suspend if a stop was posted. Instead, the thread
2606 * will suspend either via ast() or a subsequent interruptible sleep.
2607 */
2608 int
sigallowstop(void)2609 sigallowstop(void)
2610 {
2611 struct thread *td;
2612 int prev;
2613
2614 td = curthread;
2615 thread_lock(td);
2616 prev = (td->td_flags & TDF_SBDRY) != 0;
2617 td->td_flags &= ~TDF_SBDRY;
2618 thread_unlock(td);
2619 return (prev);
2620 }
2621
2622 /*
2623 * If the current process has received a signal (should be caught or cause
2624 * termination, should interrupt current syscall), return the signal number.
2625 * Stop signals with default action are processed immediately, then cleared;
2626 * they aren't returned. This is checked after each entry to the system for
2627 * a syscall or trap (though this can usually be done without calling issignal
2628 * by checking the pending signal masks in cursig.) The normal call
2629 * sequence is
2630 *
2631 * while (sig = cursig(curthread))
2632 * postsig(sig);
2633 */
2634 static int
issignal(struct thread * td)2635 issignal(struct thread *td)
2636 {
2637 struct proc *p;
2638 struct sigacts *ps;
2639 struct sigqueue *queue;
2640 sigset_t sigpending;
2641 int sig, prop, newsig;
2642
2643 p = td->td_proc;
2644 ps = p->p_sigacts;
2645 mtx_assert(&ps->ps_mtx, MA_OWNED);
2646 PROC_LOCK_ASSERT(p, MA_OWNED);
2647 for (;;) {
2648 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2649
2650 sigpending = td->td_sigqueue.sq_signals;
2651 SIGSETOR(sigpending, p->p_sigqueue.sq_signals);
2652 SIGSETNAND(sigpending, td->td_sigmask);
2653
2654 if (p->p_flag & P_PPWAIT || td->td_flags & TDF_SBDRY)
2655 SIG_STOPSIGMASK(sigpending);
2656 if (SIGISEMPTY(sigpending)) /* no signal to send */
2657 return (0);
2658 sig = sig_ffs(&sigpending);
2659
2660 if (p->p_stops & S_SIG) {
2661 mtx_unlock(&ps->ps_mtx);
2662 stopevent(p, S_SIG, sig);
2663 mtx_lock(&ps->ps_mtx);
2664 }
2665
2666 /*
2667 * We should see pending but ignored signals
2668 * only if P_TRACED was on when they were posted.
2669 */
2670 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2671 sigqueue_delete(&td->td_sigqueue, sig);
2672 sigqueue_delete(&p->p_sigqueue, sig);
2673 continue;
2674 }
2675 if (p->p_flag & P_TRACED && (p->p_flag & P_PPTRACE) == 0) {
2676 /*
2677 * If traced, always stop.
2678 * Remove old signal from queue before the stop.
2679 * XXX shrug off debugger, it causes siginfo to
2680 * be thrown away.
2681 */
2682 queue = &td->td_sigqueue;
2683 td->td_dbgksi.ksi_signo = 0;
2684 if (sigqueue_get(queue, sig, &td->td_dbgksi) == 0) {
2685 queue = &p->p_sigqueue;
2686 sigqueue_get(queue, sig, &td->td_dbgksi);
2687 }
2688
2689 mtx_unlock(&ps->ps_mtx);
2690 newsig = ptracestop(td, sig);
2691 mtx_lock(&ps->ps_mtx);
2692
2693 if (sig != newsig) {
2694
2695 /*
2696 * If parent wants us to take the signal,
2697 * then it will leave it in p->p_xstat;
2698 * otherwise we just look for signals again.
2699 */
2700 if (newsig == 0)
2701 continue;
2702 sig = newsig;
2703
2704 /*
2705 * Put the new signal into td_sigqueue. If the
2706 * signal is being masked, look for other
2707 * signals.
2708 */
2709 sigqueue_add(queue, sig, NULL);
2710 if (SIGISMEMBER(td->td_sigmask, sig))
2711 continue;
2712 signotify(td);
2713 } else {
2714 if (td->td_dbgksi.ksi_signo != 0) {
2715 td->td_dbgksi.ksi_flags |= KSI_HEAD;
2716 if (sigqueue_add(&td->td_sigqueue, sig,
2717 &td->td_dbgksi) != 0)
2718 td->td_dbgksi.ksi_signo = 0;
2719 }
2720 if (td->td_dbgksi.ksi_signo == 0)
2721 sigqueue_add(&td->td_sigqueue, sig,
2722 NULL);
2723 }
2724
2725 /*
2726 * If the traced bit got turned off, go back up
2727 * to the top to rescan signals. This ensures
2728 * that p_sig* and p_sigact are consistent.
2729 */
2730 if ((p->p_flag & P_TRACED) == 0)
2731 continue;
2732 }
2733
2734 prop = __sigprop(sig);
2735
2736 /*
2737 * Decide whether the signal should be returned.
2738 * Return the signal's number, or fall through
2739 * to clear it from the pending mask.
2740 */
2741 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2742
2743 case (intptr_t)SIG_DFL:
2744 /*
2745 * Don't take default actions on system processes.
2746 */
2747 if (p->p_pid <= 1) {
2748 #ifdef DIAGNOSTIC
2749 /*
2750 * Are you sure you want to ignore SIGSEGV
2751 * in init? XXX
2752 */
2753 printf("Process (pid %lu) got signal %d\n",
2754 (u_long)p->p_pid, sig);
2755 #endif
2756 break; /* == ignore */
2757 }
2758 /*
2759 * If there is a pending stop signal to process
2760 * with default action, stop here,
2761 * then clear the signal. However,
2762 * if process is member of an orphaned
2763 * process group, ignore tty stop signals.
2764 */
2765 if (prop & SA_STOP) {
2766 if (p->p_flag & (P_TRACED|P_WEXIT) ||
2767 (p->p_pgrp->pg_jobc == 0 &&
2768 prop & SA_TTYSTOP))
2769 break; /* == ignore */
2770 mtx_unlock(&ps->ps_mtx);
2771 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2772 &p->p_mtx.lock_object, "Catching SIGSTOP");
2773 p->p_flag |= P_STOPPED_SIG;
2774 p->p_xstat = sig;
2775 PROC_SLOCK(p);
2776 sig_suspend_threads(td, p, 0);
2777 thread_suspend_switch(td, p);
2778 PROC_SUNLOCK(p);
2779 mtx_lock(&ps->ps_mtx);
2780 break;
2781 } else if (prop & SA_IGNORE) {
2782 /*
2783 * Except for SIGCONT, shouldn't get here.
2784 * Default action is to ignore; drop it.
2785 */
2786 break; /* == ignore */
2787 } else
2788 return (sig);
2789 /*NOTREACHED*/
2790
2791 case (intptr_t)SIG_IGN:
2792 /*
2793 * Masking above should prevent us ever trying
2794 * to take action on an ignored signal other
2795 * than SIGCONT, unless process is traced.
2796 */
2797 if ((prop & SA_CONT) == 0 &&
2798 (p->p_flag & P_TRACED) == 0)
2799 printf("issignal\n");
2800 break; /* == ignore */
2801
2802 default:
2803 /*
2804 * This signal has an action, let
2805 * postsig() process it.
2806 */
2807 return (sig);
2808 }
2809 sigqueue_delete(&td->td_sigqueue, sig); /* take the signal! */
2810 sigqueue_delete(&p->p_sigqueue, sig);
2811 }
2812 /* NOTREACHED */
2813 }
2814
2815 void
thread_stopped(struct proc * p)2816 thread_stopped(struct proc *p)
2817 {
2818 int n;
2819
2820 PROC_LOCK_ASSERT(p, MA_OWNED);
2821 PROC_SLOCK_ASSERT(p, MA_OWNED);
2822 n = p->p_suspcount;
2823 if (p == curproc)
2824 n++;
2825 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2826 PROC_SUNLOCK(p);
2827 p->p_flag &= ~P_WAITED;
2828 PROC_LOCK(p->p_pptr);
2829 childproc_stopped(p, (p->p_flag & P_TRACED) ?
2830 CLD_TRAPPED : CLD_STOPPED);
2831 PROC_UNLOCK(p->p_pptr);
2832 PROC_SLOCK(p);
2833 }
2834 }
2835
2836 /*
2837 * Take the action for the specified signal
2838 * from the current set of pending signals.
2839 */
2840 int
postsig(sig)2841 postsig(sig)
2842 register int sig;
2843 {
2844 struct thread *td = curthread;
2845 register struct proc *p = td->td_proc;
2846 struct sigacts *ps;
2847 sig_t action;
2848 ksiginfo_t ksi;
2849 sigset_t returnmask;
2850
2851 KASSERT(sig != 0, ("postsig"));
2852
2853 PROC_LOCK_ASSERT(p, MA_OWNED);
2854 ps = p->p_sigacts;
2855 mtx_assert(&ps->ps_mtx, MA_OWNED);
2856 ksiginfo_init(&ksi);
2857 if (sigqueue_get(&td->td_sigqueue, sig, &ksi) == 0 &&
2858 sigqueue_get(&p->p_sigqueue, sig, &ksi) == 0)
2859 return (0);
2860 ksi.ksi_signo = sig;
2861 if (ksi.ksi_code == SI_TIMER)
2862 itimer_accept(p, ksi.ksi_timerid, &ksi);
2863 action = ps->ps_sigact[_SIG_IDX(sig)];
2864 #ifdef KTRACE
2865 if (KTRPOINT(td, KTR_PSIG))
2866 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2867 &td->td_oldsigmask : &td->td_sigmask, ksi.ksi_code);
2868 #endif
2869 if (p->p_stops & S_SIG) {
2870 mtx_unlock(&ps->ps_mtx);
2871 stopevent(p, S_SIG, sig);
2872 mtx_lock(&ps->ps_mtx);
2873 }
2874
2875 if (action == SIG_DFL) {
2876 /*
2877 * Default action, where the default is to kill
2878 * the process. (Other cases were ignored above.)
2879 */
2880 mtx_unlock(&ps->ps_mtx);
2881 sigexit(td, sig);
2882 /* NOTREACHED */
2883 } else {
2884 /*
2885 * If we get here, the signal must be caught.
2886 */
2887 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2888 ("postsig action"));
2889 /*
2890 * Set the new mask value and also defer further
2891 * occurrences of this signal.
2892 *
2893 * Special case: user has done a sigsuspend. Here the
2894 * current mask is not of interest, but rather the
2895 * mask from before the sigsuspend is what we want
2896 * restored after the signal processing is completed.
2897 */
2898 if (td->td_pflags & TDP_OLDMASK) {
2899 returnmask = td->td_oldsigmask;
2900 td->td_pflags &= ~TDP_OLDMASK;
2901 } else
2902 returnmask = td->td_sigmask;
2903
2904 if (p->p_sig == sig) {
2905 p->p_code = 0;
2906 p->p_sig = 0;
2907 }
2908 (*p->p_sysent->sv_sendsig)(action, &ksi, &returnmask);
2909 postsig_done(sig, td, ps);
2910 }
2911 return (1);
2912 }
2913
2914 /*
2915 * Kill the current process for stated reason.
2916 */
2917 void
killproc(p,why)2918 killproc(p, why)
2919 struct proc *p;
2920 char *why;
2921 {
2922
2923 PROC_LOCK_ASSERT(p, MA_OWNED);
2924 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)", p, p->p_pid,
2925 p->p_comm);
2926 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid,
2927 p->p_comm, p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2928 p->p_flag |= P_WKILLED;
2929 kern_psignal(p, SIGKILL);
2930 }
2931
2932 /*
2933 * Force the current process to exit with the specified signal, dumping core
2934 * if appropriate. We bypass the normal tests for masked and caught signals,
2935 * allowing unrecoverable failures to terminate the process without changing
2936 * signal state. Mark the accounting record with the signal termination.
2937 * If dumping core, save the signal number for the debugger. Calls exit and
2938 * does not return.
2939 */
2940 void
sigexit(td,sig)2941 sigexit(td, sig)
2942 struct thread *td;
2943 int sig;
2944 {
2945 struct proc *p = td->td_proc;
2946
2947 PROC_LOCK_ASSERT(p, MA_OWNED);
2948 p->p_acflag |= AXSIG;
2949 /*
2950 * We must be single-threading to generate a core dump. This
2951 * ensures that the registers in the core file are up-to-date.
2952 * Also, the ELF dump handler assumes that the thread list doesn't
2953 * change out from under it.
2954 *
2955 * XXX If another thread attempts to single-thread before us
2956 * (e.g. via fork()), we won't get a dump at all.
2957 */
2958 if ((__sigprop(sig) & SA_CORE) && (thread_single(p, SINGLE_NO_EXIT) == 0)) {
2959 p->p_sig = sig;
2960 /*
2961 * Log signals which would cause core dumps
2962 * (Log as LOG_INFO to appease those who don't want
2963 * these messages.)
2964 * XXX : Todo, as well as euid, write out ruid too
2965 * Note that coredump() drops proc lock.
2966 */
2967 if (coredump(td) == 0)
2968 sig |= WCOREFLAG;
2969 if (kern_logsigexit)
2970 log(LOG_INFO,
2971 "pid %d (%s), uid %d: exited on signal %d%s\n",
2972 p->p_pid, p->p_comm,
2973 td->td_ucred ? td->td_ucred->cr_uid : -1,
2974 sig &~ WCOREFLAG,
2975 sig & WCOREFLAG ? " (core dumped)" : "");
2976 } else
2977 PROC_UNLOCK(p);
2978 exit1(td, W_EXITCODE(0, sig));
2979 /* NOTREACHED */
2980 }
2981
2982 /*
2983 * Send queued SIGCHLD to parent when child process's state
2984 * is changed.
2985 */
2986 static void
sigparent(struct proc * p,int reason,int status)2987 sigparent(struct proc *p, int reason, int status)
2988 {
2989 PROC_LOCK_ASSERT(p, MA_OWNED);
2990 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
2991
2992 if (p->p_ksi != NULL) {
2993 p->p_ksi->ksi_signo = SIGCHLD;
2994 p->p_ksi->ksi_code = reason;
2995 p->p_ksi->ksi_status = status;
2996 p->p_ksi->ksi_pid = p->p_pid;
2997 p->p_ksi->ksi_uid = p->p_ucred->cr_ruid;
2998 if (KSI_ONQ(p->p_ksi))
2999 return;
3000 }
3001 pksignal(p->p_pptr, SIGCHLD, p->p_ksi);
3002 }
3003
3004 static void
childproc_jobstate(struct proc * p,int reason,int sig)3005 childproc_jobstate(struct proc *p, int reason, int sig)
3006 {
3007 struct sigacts *ps;
3008
3009 PROC_LOCK_ASSERT(p, MA_OWNED);
3010 PROC_LOCK_ASSERT(p->p_pptr, MA_OWNED);
3011
3012 /*
3013 * Wake up parent sleeping in kern_wait(), also send
3014 * SIGCHLD to parent, but SIGCHLD does not guarantee
3015 * that parent will awake, because parent may masked
3016 * the signal.
3017 */
3018 p->p_pptr->p_flag |= P_STATCHILD;
3019 wakeup(p->p_pptr);
3020
3021 ps = p->p_pptr->p_sigacts;
3022 mtx_lock(&ps->ps_mtx);
3023 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
3024 mtx_unlock(&ps->ps_mtx);
3025 sigparent(p, reason, sig);
3026 } else
3027 mtx_unlock(&ps->ps_mtx);
3028 }
3029
3030 void
childproc_stopped(struct proc * p,int reason)3031 childproc_stopped(struct proc *p, int reason)
3032 {
3033 /* p_xstat is a plain signal number, not a full wait() status here. */
3034 childproc_jobstate(p, reason, p->p_xstat);
3035 }
3036
3037 void
childproc_continued(struct proc * p)3038 childproc_continued(struct proc *p)
3039 {
3040 childproc_jobstate(p, CLD_CONTINUED, SIGCONT);
3041 }
3042
3043 void
childproc_exited(struct proc * p)3044 childproc_exited(struct proc *p)
3045 {
3046 int reason;
3047 int xstat = p->p_xstat; /* convert to int */
3048 int status;
3049
3050 if (WCOREDUMP(xstat))
3051 reason = CLD_DUMPED, status = WTERMSIG(xstat);
3052 else if (WIFSIGNALED(xstat))
3053 reason = CLD_KILLED, status = WTERMSIG(xstat);
3054 else
3055 reason = CLD_EXITED, status = WEXITSTATUS(xstat);
3056 /*
3057 * XXX avoid calling wakeup(p->p_pptr), the work is
3058 * done in exit1().
3059 */
3060 sigparent(p, reason, status);
3061 }
3062
3063 /*
3064 * We only have 1 character for the core count in the format
3065 * string, so the range will be 0-9
3066 */
3067 #define MAX_NUM_CORES 10
3068 static int num_cores = 5;
3069
3070 static int
sysctl_debug_num_cores_check(SYSCTL_HANDLER_ARGS)3071 sysctl_debug_num_cores_check (SYSCTL_HANDLER_ARGS)
3072 {
3073 int error;
3074 int new_val;
3075
3076 new_val = num_cores;
3077 error = sysctl_handle_int(oidp, &new_val, 0, req);
3078 if (error != 0 || req->newptr == NULL)
3079 return (error);
3080 if (new_val > MAX_NUM_CORES)
3081 new_val = MAX_NUM_CORES;
3082 if (new_val < 0)
3083 new_val = 0;
3084 num_cores = new_val;
3085 return (0);
3086 }
3087 SYSCTL_PROC(_debug, OID_AUTO, ncores, CTLTYPE_INT|CTLFLAG_RW,
3088 0, sizeof(int), sysctl_debug_num_cores_check, "I", "");
3089
3090 #if defined(COMPRESS_USER_CORES)
3091 int compress_user_cores = 1;
3092 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores, CTLFLAG_RW,
3093 &compress_user_cores, 0, "Compression of user corefiles");
3094
3095 int compress_user_cores_gzlevel = -1; /* default level */
3096 SYSCTL_INT(_kern, OID_AUTO, compress_user_cores_gzlevel, CTLFLAG_RW,
3097 &compress_user_cores_gzlevel, -1, "Corefile gzip compression level");
3098
3099 #define GZ_SUFFIX ".gz"
3100 #define GZ_SUFFIX_LEN 3
3101 #endif
3102
3103 static char corefilename[MAXPATHLEN] = {"%N.core"};
3104 TUNABLE_STR("kern.corefile", corefilename, sizeof(corefilename));
3105 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
3106 sizeof(corefilename), "Process corefile name format string");
3107
3108 /*
3109 * corefile_open(comm, uid, pid, td, compress, vpp, namep)
3110 * Expand the name described in corefilename, using name, uid, and pid
3111 * and open/create core file.
3112 * corefilename is a printf-like string, with three format specifiers:
3113 * %N name of process ("name")
3114 * %P process id (pid)
3115 * %U user id (uid)
3116 * For example, "%N.core" is the default; they can be disabled completely
3117 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
3118 * This is controlled by the sysctl variable kern.corefile (see above).
3119 */
3120 static int
corefile_open(const char * comm,uid_t uid,pid_t pid,struct thread * td,int compress,struct vnode ** vpp,char ** namep)3121 corefile_open(const char *comm, uid_t uid, pid_t pid, struct thread *td,
3122 int compress, struct vnode **vpp, char **namep)
3123 {
3124 struct nameidata nd;
3125 struct sbuf sb;
3126 const char *format;
3127 char *hostname, *name;
3128 int indexpos, i, error, cmode, flags, oflags;
3129
3130 hostname = NULL;
3131 format = corefilename;
3132 name = malloc(MAXPATHLEN, M_TEMP, M_WAITOK | M_ZERO);
3133 indexpos = -1;
3134 (void)sbuf_new(&sb, name, MAXPATHLEN, SBUF_FIXEDLEN);
3135 for (i = 0; format[i] != '\0'; i++) {
3136 switch (format[i]) {
3137 case '%': /* Format character */
3138 i++;
3139 switch (format[i]) {
3140 case '%':
3141 sbuf_putc(&sb, '%');
3142 break;
3143 case 'H': /* hostname */
3144 if (hostname == NULL) {
3145 hostname = malloc(MAXHOSTNAMELEN,
3146 M_TEMP, M_WAITOK);
3147 }
3148 getcredhostname(td->td_ucred, hostname,
3149 MAXHOSTNAMELEN);
3150 sbuf_printf(&sb, "%s", hostname);
3151 break;
3152 case 'I': /* autoincrementing index */
3153 sbuf_printf(&sb, "0");
3154 indexpos = sbuf_len(&sb) - 1;
3155 break;
3156 case 'N': /* process name */
3157 sbuf_printf(&sb, "%s", comm);
3158 break;
3159 case 'P': /* process id */
3160 sbuf_printf(&sb, "%u", pid);
3161 break;
3162 case 'U': /* user id */
3163 sbuf_printf(&sb, "%u", uid);
3164 break;
3165 default:
3166 log(LOG_ERR,
3167 "Unknown format character %c in "
3168 "corename `%s'\n", format[i], format);
3169 break;
3170 }
3171 break;
3172 default:
3173 sbuf_putc(&sb, format[i]);
3174 break;
3175 }
3176 }
3177 free(hostname, M_TEMP);
3178 #ifdef COMPRESS_USER_CORES
3179 if (compress)
3180 sbuf_printf(&sb, GZ_SUFFIX);
3181 #endif
3182 if (sbuf_error(&sb) != 0) {
3183 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too "
3184 "long\n", (long)pid, comm, (u_long)uid);
3185 sbuf_delete(&sb);
3186 free(name, M_TEMP);
3187 return (ENOMEM);
3188 }
3189 sbuf_finish(&sb);
3190 sbuf_delete(&sb);
3191
3192 cmode = S_IRUSR | S_IWUSR;
3193 oflags = VN_OPEN_NOAUDIT | VN_OPEN_NAMECACHE |
3194 (capmode_coredump ? VN_OPEN_NOCAPCHECK : 0);
3195
3196 /*
3197 * If the core format has a %I in it, then we need to check
3198 * for existing corefiles before returning a name.
3199 * To do this we iterate over 0..num_cores to find a
3200 * non-existing core file name to use.
3201 */
3202 if (indexpos != -1) {
3203 for (i = 0; i < num_cores; i++) {
3204 flags = O_CREAT | O_EXCL | FWRITE | O_NOFOLLOW;
3205 name[indexpos] = '0' + i;
3206 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3207 error = vn_open_cred(&nd, &flags, cmode, oflags,
3208 td->td_ucred, NULL);
3209 if (error) {
3210 if (error == EEXIST)
3211 continue;
3212 log(LOG_ERR,
3213 "pid %d (%s), uid (%u): Path `%s' failed "
3214 "on initial open test, error = %d\n",
3215 pid, comm, uid, name, error);
3216 }
3217 goto out;
3218 }
3219 }
3220
3221 flags = O_CREAT | FWRITE | O_NOFOLLOW;
3222 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td);
3223 error = vn_open_cred(&nd, &flags, cmode, oflags, td->td_ucred, NULL);
3224 out:
3225 if (error) {
3226 #ifdef AUDIT
3227 audit_proc_coredump(td, name, error);
3228 #endif
3229 free(name, M_TEMP);
3230 return (error);
3231 }
3232 NDFREE(&nd, NDF_ONLY_PNBUF);
3233 *vpp = nd.ni_vp;
3234 *namep = name;
3235 return (0);
3236 }
3237
3238 /*
3239 * Dump a process' core. The main routine does some
3240 * policy checking, and creates the name of the coredump;
3241 * then it passes on a vnode and a size limit to the process-specific
3242 * coredump routine if there is one; if there _is not_ one, it returns
3243 * ENOSYS; otherwise it returns the error from the process-specific routine.
3244 */
3245
3246 static int
coredump(struct thread * td)3247 coredump(struct thread *td)
3248 {
3249 struct proc *p = td->td_proc;
3250 struct ucred *cred = td->td_ucred;
3251 struct vnode *vp;
3252 struct flock lf;
3253 struct vattr vattr;
3254 int error, error1, locked;
3255 struct mount *mp;
3256 char *name; /* name of corefile */
3257 off_t limit;
3258 int compress;
3259
3260 #ifdef COMPRESS_USER_CORES
3261 compress = compress_user_cores;
3262 #else
3263 compress = 0;
3264 #endif
3265 PROC_LOCK_ASSERT(p, MA_OWNED);
3266 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
3267 _STOPEVENT(p, S_CORE, 0);
3268
3269 if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) ||
3270 (p->p_flag2 & P2_NOTRACE) != 0) {
3271 PROC_UNLOCK(p);
3272 return (EFAULT);
3273 }
3274
3275 /*
3276 * Note that the bulk of limit checking is done after
3277 * the corefile is created. The exception is if the limit
3278 * for corefiles is 0, in which case we don't bother
3279 * creating the corefile at all. This layout means that
3280 * a corefile is truncated instead of not being created,
3281 * if it is larger than the limit.
3282 */
3283 limit = (off_t)lim_cur(p, RLIMIT_CORE);
3284 if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) {
3285 PROC_UNLOCK(p);
3286 return (EFBIG);
3287 }
3288 PROC_UNLOCK(p);
3289
3290 restart:
3291 error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td, compress,
3292 &vp, &name);
3293 if (error != 0)
3294 return (error);
3295
3296 /* Don't dump to non-regular files or files with links. */
3297 if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 ||
3298 vattr.va_nlink != 1) {
3299 VOP_UNLOCK(vp, 0);
3300 error = EFAULT;
3301 goto close;
3302 }
3303
3304 VOP_UNLOCK(vp, 0);
3305 lf.l_whence = SEEK_SET;
3306 lf.l_start = 0;
3307 lf.l_len = 0;
3308 lf.l_type = F_WRLCK;
3309 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
3310
3311 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
3312 lf.l_type = F_UNLCK;
3313 if (locked)
3314 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3315 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
3316 goto out;
3317 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
3318 goto out;
3319 free(name, M_TEMP);
3320 goto restart;
3321 }
3322
3323 VATTR_NULL(&vattr);
3324 vattr.va_size = 0;
3325 if (set_core_nodump_flag)
3326 vattr.va_flags = UF_NODUMP;
3327 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3328 VOP_SETATTR(vp, &vattr, cred);
3329 VOP_UNLOCK(vp, 0);
3330 vn_finished_write(mp);
3331 PROC_LOCK(p);
3332 p->p_acflag |= ACORE;
3333 PROC_UNLOCK(p);
3334
3335 if (p->p_sysent->sv_coredump != NULL) {
3336 error = p->p_sysent->sv_coredump(td, vp, limit,
3337 compress ? IMGACT_CORE_COMPRESS : 0);
3338 } else {
3339 error = ENOSYS;
3340 }
3341
3342 if (locked) {
3343 lf.l_type = F_UNLCK;
3344 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
3345 }
3346 close:
3347 error1 = vn_close(vp, FWRITE, cred, td);
3348 if (error == 0)
3349 error = error1;
3350 out:
3351 #ifdef AUDIT
3352 audit_proc_coredump(td, name, error);
3353 #endif
3354 free(name, M_TEMP);
3355 return (error);
3356 }
3357
3358 /*
3359 * Nonexistent system call-- signal process (may want to handle it). Flag
3360 * error in case process won't see signal immediately (blocked or ignored).
3361 */
3362 #ifndef _SYS_SYSPROTO_H_
3363 struct nosys_args {
3364 int dummy;
3365 };
3366 #endif
3367 /* ARGSUSED */
3368 int
nosys(td,args)3369 nosys(td, args)
3370 struct thread *td;
3371 struct nosys_args *args;
3372 {
3373 struct proc *p = td->td_proc;
3374
3375 PROC_LOCK(p);
3376 tdsignal(td, SIGSYS);
3377 PROC_UNLOCK(p);
3378 return (ENOSYS);
3379 }
3380
3381 /*
3382 * Send a SIGIO or SIGURG signal to a process or process group using stored
3383 * credentials rather than those of the current process.
3384 */
3385 void
pgsigio(sigiop,sig,checkctty)3386 pgsigio(sigiop, sig, checkctty)
3387 struct sigio **sigiop;
3388 int sig, checkctty;
3389 {
3390 ksiginfo_t ksi;
3391 struct sigio *sigio;
3392
3393 ksiginfo_init(&ksi);
3394 ksi.ksi_signo = sig;
3395 ksi.ksi_code = SI_KERNEL;
3396
3397 SIGIO_LOCK();
3398 sigio = *sigiop;
3399 if (sigio == NULL) {
3400 SIGIO_UNLOCK();
3401 return;
3402 }
3403 if (sigio->sio_pgid > 0) {
3404 PROC_LOCK(sigio->sio_proc);
3405 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
3406 kern_psignal(sigio->sio_proc, sig);
3407 PROC_UNLOCK(sigio->sio_proc);
3408 } else if (sigio->sio_pgid < 0) {
3409 struct proc *p;
3410
3411 PGRP_LOCK(sigio->sio_pgrp);
3412 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
3413 PROC_LOCK(p);
3414 if (p->p_state == PRS_NORMAL &&
3415 CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
3416 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
3417 kern_psignal(p, sig);
3418 PROC_UNLOCK(p);
3419 }
3420 PGRP_UNLOCK(sigio->sio_pgrp);
3421 }
3422 SIGIO_UNLOCK();
3423 }
3424
3425 static int
filt_sigattach(struct knote * kn)3426 filt_sigattach(struct knote *kn)
3427 {
3428 struct proc *p = curproc;
3429
3430 kn->kn_ptr.p_proc = p;
3431 kn->kn_flags |= EV_CLEAR; /* automatically set */
3432
3433 knlist_add(&p->p_klist, kn, 0);
3434
3435 return (0);
3436 }
3437
3438 static void
filt_sigdetach(struct knote * kn)3439 filt_sigdetach(struct knote *kn)
3440 {
3441 struct proc *p = kn->kn_ptr.p_proc;
3442
3443 knlist_remove(&p->p_klist, kn, 0);
3444 }
3445
3446 /*
3447 * signal knotes are shared with proc knotes, so we apply a mask to
3448 * the hint in order to differentiate them from process hints. This
3449 * could be avoided by using a signal-specific knote list, but probably
3450 * isn't worth the trouble.
3451 */
3452 static int
filt_signal(struct knote * kn,long hint)3453 filt_signal(struct knote *kn, long hint)
3454 {
3455
3456 if (hint & NOTE_SIGNAL) {
3457 hint &= ~NOTE_SIGNAL;
3458
3459 if (kn->kn_id == hint)
3460 kn->kn_data++;
3461 }
3462 return (kn->kn_data != 0);
3463 }
3464
3465 struct sigacts *
sigacts_alloc(void)3466 sigacts_alloc(void)
3467 {
3468 struct sigacts *ps;
3469
3470 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
3471 ps->ps_refcnt = 1;
3472 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
3473 return (ps);
3474 }
3475
3476 void
sigacts_free(struct sigacts * ps)3477 sigacts_free(struct sigacts *ps)
3478 {
3479
3480 if (refcount_release(&ps->ps_refcnt) == 0)
3481 return;
3482 mtx_destroy(&ps->ps_mtx);
3483 free(ps, M_SUBPROC);
3484 }
3485
3486 struct sigacts *
sigacts_hold(struct sigacts * ps)3487 sigacts_hold(struct sigacts *ps)
3488 {
3489
3490 refcount_acquire(&ps->ps_refcnt);
3491 return (ps);
3492 }
3493
3494 void
sigacts_copy(struct sigacts * dest,struct sigacts * src)3495 sigacts_copy(struct sigacts *dest, struct sigacts *src)
3496 {
3497
3498 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
3499 mtx_lock(&src->ps_mtx);
3500 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
3501 mtx_unlock(&src->ps_mtx);
3502 }
3503
3504 int
sigacts_shared(struct sigacts * ps)3505 sigacts_shared(struct sigacts *ps)
3506 {
3507
3508 return (ps->ps_refcnt > 1);
3509 }
3510