FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_sig.c
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_ktrace.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/signalvar.h>
46 #include <sys/vnode.h>
47 #include <sys/acct.h>
48 #include <sys/condvar.h>
49 #include <sys/event.h>
50 #include <sys/fcntl.h>
51 #include <sys/kernel.h>
52 #include <sys/kse.h>
53 #include <sys/ktr.h>
54 #include <sys/ktrace.h>
55 #include <sys/lock.h>
56 #include <sys/malloc.h>
57 #include <sys/mutex.h>
58 #include <sys/namei.h>
59 #include <sys/proc.h>
60 #include <sys/pioctl.h>
61 #include <sys/resourcevar.h>
62 #include <sys/sched.h>
63 #include <sys/sleepqueue.h>
64 #include <sys/smp.h>
65 #include <sys/stat.h>
66 #include <sys/sx.h>
67 #include <sys/syscallsubr.h>
68 #include <sys/sysctl.h>
69 #include <sys/sysent.h>
70 #include <sys/syslog.h>
71 #include <sys/sysproto.h>
72 #include <sys/unistd.h>
73 #include <sys/wait.h>
74
75 #include <machine/cpu.h>
76
77 #include <security/audit/audit.h>
78
79 #if defined (__alpha__) && !defined(COMPAT_43)
80 #error "You *really* need COMPAT_43 on the alpha for longjmp(3)"
81 #endif
82
83 #define ONSIG 32 /* NSIG for osig* syscalls. XXX. */
84
85 static int coredump(struct thread *);
86 static char *expand_name(const char *, uid_t, pid_t);
87 static int killpg1(struct thread *td, int sig, int pgid, int all);
88 static int issignal(struct thread *p);
89 static int sigprop(int sig);
90 static void tdsigwakeup(struct thread *, int, sig_t, int);
91 static void sig_suspend_threads(struct thread *, struct proc *, int);
92 static int filt_sigattach(struct knote *kn);
93 static void filt_sigdetach(struct knote *kn);
94 static int filt_signal(struct knote *kn, long hint);
95 static struct thread *sigtd(struct proc *p, int sig, int prop);
96 static int kern_sigtimedwait(struct thread *td, sigset_t set,
97 siginfo_t *info, struct timespec *timeout);
98 static void do_tdsignal(struct thread *td, int sig, sigtarget_t target);
99
100 struct filterops sig_filtops =
101 { 0, filt_sigattach, filt_sigdetach, filt_signal };
102
103 static int kern_logsigexit = 1;
104 SYSCTL_INT(_kern, KERN_LOGSIGEXIT, logsigexit, CTLFLAG_RW,
105 &kern_logsigexit, 0,
106 "Log processes quitting on abnormal signals to syslog(3)");
107
108 /*
109 * Policy -- Can ucred cr1 send SIGIO to process cr2?
110 * Should use cr_cansignal() once cr_cansignal() allows SIGIO and SIGURG
111 * in the right situations.
112 */
113 #define CANSIGIO(cr1, cr2) \
114 ((cr1)->cr_uid == 0 || \
115 (cr1)->cr_ruid == (cr2)->cr_ruid || \
116 (cr1)->cr_uid == (cr2)->cr_ruid || \
117 (cr1)->cr_ruid == (cr2)->cr_uid || \
118 (cr1)->cr_uid == (cr2)->cr_uid)
119
120 int sugid_coredump;
121 SYSCTL_INT(_kern, OID_AUTO, sugid_coredump, CTLFLAG_RW,
122 &sugid_coredump, 0, "Enable coredumping set user/group ID processes");
123
124 static int do_coredump = 1;
125 SYSCTL_INT(_kern, OID_AUTO, coredump, CTLFLAG_RW,
126 &do_coredump, 0, "Enable/Disable coredumps");
127
128 static int set_core_nodump_flag = 0;
129 SYSCTL_INT(_kern, OID_AUTO, nodump_coredump, CTLFLAG_RW, &set_core_nodump_flag,
130 0, "Enable setting the NODUMP flag on coredump files");
131
132 /*
133 * Signal properties and actions.
134 * The array below categorizes the signals and their default actions
135 * according to the following properties:
136 */
137 #define SA_KILL 0x01 /* terminates process by default */
138 #define SA_CORE 0x02 /* ditto and coredumps */
139 #define SA_STOP 0x04 /* suspend process */
140 #define SA_TTYSTOP 0x08 /* ditto, from tty */
141 #define SA_IGNORE 0x10 /* ignore by default */
142 #define SA_CONT 0x20 /* continue if suspended */
143 #define SA_CANTMASK 0x40 /* non-maskable, catchable */
144 #define SA_PROC 0x80 /* deliverable to any thread */
145
146 static int sigproptbl[NSIG] = {
147 SA_KILL|SA_PROC, /* SIGHUP */
148 SA_KILL|SA_PROC, /* SIGINT */
149 SA_KILL|SA_CORE|SA_PROC, /* SIGQUIT */
150 SA_KILL|SA_CORE, /* SIGILL */
151 SA_KILL|SA_CORE, /* SIGTRAP */
152 SA_KILL|SA_CORE, /* SIGABRT */
153 SA_KILL|SA_CORE|SA_PROC, /* SIGEMT */
154 SA_KILL|SA_CORE, /* SIGFPE */
155 SA_KILL|SA_PROC, /* SIGKILL */
156 SA_KILL|SA_CORE, /* SIGBUS */
157 SA_KILL|SA_CORE, /* SIGSEGV */
158 SA_KILL|SA_CORE, /* SIGSYS */
159 SA_KILL|SA_PROC, /* SIGPIPE */
160 SA_KILL|SA_PROC, /* SIGALRM */
161 SA_KILL|SA_PROC, /* SIGTERM */
162 SA_IGNORE|SA_PROC, /* SIGURG */
163 SA_STOP|SA_PROC, /* SIGSTOP */
164 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTSTP */
165 SA_IGNORE|SA_CONT|SA_PROC, /* SIGCONT */
166 SA_IGNORE|SA_PROC, /* SIGCHLD */
167 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTIN */
168 SA_STOP|SA_TTYSTOP|SA_PROC, /* SIGTTOU */
169 SA_IGNORE|SA_PROC, /* SIGIO */
170 SA_KILL, /* SIGXCPU */
171 SA_KILL, /* SIGXFSZ */
172 SA_KILL|SA_PROC, /* SIGVTALRM */
173 SA_KILL|SA_PROC, /* SIGPROF */
174 SA_IGNORE|SA_PROC, /* SIGWINCH */
175 SA_IGNORE|SA_PROC, /* SIGINFO */
176 SA_KILL|SA_PROC, /* SIGUSR1 */
177 SA_KILL|SA_PROC, /* SIGUSR2 */
178 };
179
180 /*
181 * Determine signal that should be delivered to process p, the current
182 * process, 0 if none. If there is a pending stop signal with default
183 * action, the process stops in issignal().
184 * XXXKSE the check for a pending stop is not done under KSE
185 *
186 * MP SAFE.
187 */
188 int
189 cursig(struct thread *td)
190 {
191 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
192 mtx_assert(&td->td_proc->p_sigacts->ps_mtx, MA_OWNED);
193 mtx_assert(&sched_lock, MA_NOTOWNED);
194 return (SIGPENDING(td) ? issignal(td) : 0);
195 }
196
197 /*
198 * Arrange for ast() to handle unmasked pending signals on return to user
199 * mode. This must be called whenever a signal is added to td_siglist or
200 * unmasked in td_sigmask.
201 */
202 void
203 signotify(struct thread *td)
204 {
205 struct proc *p;
206 sigset_t set, saved;
207
208 p = td->td_proc;
209
210 PROC_LOCK_ASSERT(p, MA_OWNED);
211
212 /*
213 * If our mask changed we may have to move signal that were
214 * previously masked by all threads to our siglist.
215 */
216 set = p->p_siglist;
217 if (p->p_flag & P_SA)
218 saved = p->p_siglist;
219 SIGSETNAND(set, td->td_sigmask);
220 SIGSETNAND(p->p_siglist, set);
221 SIGSETOR(td->td_siglist, set);
222
223 if (SIGPENDING(td)) {
224 mtx_lock_spin(&sched_lock);
225 td->td_flags |= TDF_NEEDSIGCHK | TDF_ASTPENDING;
226 mtx_unlock_spin(&sched_lock);
227 }
228 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
229 if (!SIGSETEQ(saved, p->p_siglist)) {
230 /* pending set changed */
231 p->p_flag |= P_SIGEVENT;
232 wakeup(&p->p_siglist);
233 }
234 }
235 }
236
237 int
238 sigonstack(size_t sp)
239 {
240 struct thread *td = curthread;
241
242 return ((td->td_pflags & TDP_ALTSTACK) ?
243 #if defined(COMPAT_43)
244 ((td->td_sigstk.ss_size == 0) ?
245 (td->td_sigstk.ss_flags & SS_ONSTACK) :
246 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size))
247 #else
248 ((sp - (size_t)td->td_sigstk.ss_sp) < td->td_sigstk.ss_size)
249 #endif
250 : 0);
251 }
252
253 static __inline int
254 sigprop(int sig)
255 {
256
257 if (sig > 0 && sig < NSIG)
258 return (sigproptbl[_SIG_IDX(sig)]);
259 return (0);
260 }
261
262 int
263 sig_ffs(sigset_t *set)
264 {
265 int i;
266
267 for (i = 0; i < _SIG_WORDS; i++)
268 if (set->__bits[i])
269 return (ffs(set->__bits[i]) + (i * 32));
270 return (0);
271 }
272
273 /*
274 * kern_sigaction
275 * sigaction
276 * freebsd4_sigaction
277 * osigaction
278 *
279 * MPSAFE
280 */
281 int
282 kern_sigaction(td, sig, act, oact, flags)
283 struct thread *td;
284 register int sig;
285 struct sigaction *act, *oact;
286 int flags;
287 {
288 struct sigacts *ps;
289 struct thread *td0;
290 struct proc *p = td->td_proc;
291
292 if (!_SIG_VALID(sig))
293 return (EINVAL);
294
295 PROC_LOCK(p);
296 ps = p->p_sigacts;
297 mtx_lock(&ps->ps_mtx);
298 if (oact) {
299 oact->sa_mask = ps->ps_catchmask[_SIG_IDX(sig)];
300 oact->sa_flags = 0;
301 if (SIGISMEMBER(ps->ps_sigonstack, sig))
302 oact->sa_flags |= SA_ONSTACK;
303 if (!SIGISMEMBER(ps->ps_sigintr, sig))
304 oact->sa_flags |= SA_RESTART;
305 if (SIGISMEMBER(ps->ps_sigreset, sig))
306 oact->sa_flags |= SA_RESETHAND;
307 if (SIGISMEMBER(ps->ps_signodefer, sig))
308 oact->sa_flags |= SA_NODEFER;
309 if (SIGISMEMBER(ps->ps_siginfo, sig)) {
310 oact->sa_flags |= SA_SIGINFO;
311 oact->sa_sigaction =
312 (__siginfohandler_t *)ps->ps_sigact[_SIG_IDX(sig)];
313 } else
314 oact->sa_handler = ps->ps_sigact[_SIG_IDX(sig)];
315 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDSTOP)
316 oact->sa_flags |= SA_NOCLDSTOP;
317 if (sig == SIGCHLD && ps->ps_flag & PS_NOCLDWAIT)
318 oact->sa_flags |= SA_NOCLDWAIT;
319 }
320 if (act) {
321 if ((sig == SIGKILL || sig == SIGSTOP) &&
322 act->sa_handler != SIG_DFL) {
323 mtx_unlock(&ps->ps_mtx);
324 PROC_UNLOCK(p);
325 return (EINVAL);
326 }
327
328 /*
329 * Change setting atomically.
330 */
331
332 ps->ps_catchmask[_SIG_IDX(sig)] = act->sa_mask;
333 SIG_CANTMASK(ps->ps_catchmask[_SIG_IDX(sig)]);
334 if (act->sa_flags & SA_SIGINFO) {
335 ps->ps_sigact[_SIG_IDX(sig)] =
336 (__sighandler_t *)act->sa_sigaction;
337 SIGADDSET(ps->ps_siginfo, sig);
338 } else {
339 ps->ps_sigact[_SIG_IDX(sig)] = act->sa_handler;
340 SIGDELSET(ps->ps_siginfo, sig);
341 }
342 if (!(act->sa_flags & SA_RESTART))
343 SIGADDSET(ps->ps_sigintr, sig);
344 else
345 SIGDELSET(ps->ps_sigintr, sig);
346 if (act->sa_flags & SA_ONSTACK)
347 SIGADDSET(ps->ps_sigonstack, sig);
348 else
349 SIGDELSET(ps->ps_sigonstack, sig);
350 if (act->sa_flags & SA_RESETHAND)
351 SIGADDSET(ps->ps_sigreset, sig);
352 else
353 SIGDELSET(ps->ps_sigreset, sig);
354 if (act->sa_flags & SA_NODEFER)
355 SIGADDSET(ps->ps_signodefer, sig);
356 else
357 SIGDELSET(ps->ps_signodefer, sig);
358 if (sig == SIGCHLD) {
359 if (act->sa_flags & SA_NOCLDSTOP)
360 ps->ps_flag |= PS_NOCLDSTOP;
361 else
362 ps->ps_flag &= ~PS_NOCLDSTOP;
363 if (act->sa_flags & SA_NOCLDWAIT) {
364 /*
365 * Paranoia: since SA_NOCLDWAIT is implemented
366 * by reparenting the dying child to PID 1 (and
367 * trust it to reap the zombie), PID 1 itself
368 * is forbidden to set SA_NOCLDWAIT.
369 */
370 if (p->p_pid == 1)
371 ps->ps_flag &= ~PS_NOCLDWAIT;
372 else
373 ps->ps_flag |= PS_NOCLDWAIT;
374 } else
375 ps->ps_flag &= ~PS_NOCLDWAIT;
376 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
377 ps->ps_flag |= PS_CLDSIGIGN;
378 else
379 ps->ps_flag &= ~PS_CLDSIGIGN;
380 }
381 /*
382 * Set bit in ps_sigignore for signals that are set to SIG_IGN,
383 * and for signals set to SIG_DFL where the default is to
384 * ignore. However, don't put SIGCONT in ps_sigignore, as we
385 * have to restart the process.
386 */
387 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
388 (sigprop(sig) & SA_IGNORE &&
389 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)) {
390 if ((p->p_flag & P_SA) &&
391 SIGISMEMBER(p->p_siglist, sig)) {
392 p->p_flag |= P_SIGEVENT;
393 wakeup(&p->p_siglist);
394 }
395 /* never to be seen again */
396 SIGDELSET(p->p_siglist, sig);
397 mtx_lock_spin(&sched_lock);
398 FOREACH_THREAD_IN_PROC(p, td0)
399 SIGDELSET(td0->td_siglist, sig);
400 mtx_unlock_spin(&sched_lock);
401 if (sig != SIGCONT)
402 /* easier in psignal */
403 SIGADDSET(ps->ps_sigignore, sig);
404 SIGDELSET(ps->ps_sigcatch, sig);
405 } else {
406 SIGDELSET(ps->ps_sigignore, sig);
407 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL)
408 SIGDELSET(ps->ps_sigcatch, sig);
409 else
410 SIGADDSET(ps->ps_sigcatch, sig);
411 }
412 #ifdef COMPAT_FREEBSD4
413 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
414 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
415 (flags & KSA_FREEBSD4) == 0)
416 SIGDELSET(ps->ps_freebsd4, sig);
417 else
418 SIGADDSET(ps->ps_freebsd4, sig);
419 #endif
420 #ifdef COMPAT_43
421 if (ps->ps_sigact[_SIG_IDX(sig)] == SIG_IGN ||
422 ps->ps_sigact[_SIG_IDX(sig)] == SIG_DFL ||
423 (flags & KSA_OSIGSET) == 0)
424 SIGDELSET(ps->ps_osigset, sig);
425 else
426 SIGADDSET(ps->ps_osigset, sig);
427 #endif
428 }
429 mtx_unlock(&ps->ps_mtx);
430 PROC_UNLOCK(p);
431 return (0);
432 }
433
434 #ifndef _SYS_SYSPROTO_H_
435 struct sigaction_args {
436 int sig;
437 struct sigaction *act;
438 struct sigaction *oact;
439 };
440 #endif
441 /*
442 * MPSAFE
443 */
444 int
445 sigaction(td, uap)
446 struct thread *td;
447 register struct sigaction_args *uap;
448 {
449 struct sigaction act, oact;
450 register struct sigaction *actp, *oactp;
451 int error;
452
453 actp = (uap->act != NULL) ? &act : NULL;
454 oactp = (uap->oact != NULL) ? &oact : NULL;
455 if (actp) {
456 error = copyin(uap->act, actp, sizeof(act));
457 if (error)
458 return (error);
459 }
460 error = kern_sigaction(td, uap->sig, actp, oactp, 0);
461 if (oactp && !error)
462 error = copyout(oactp, uap->oact, sizeof(oact));
463 return (error);
464 }
465
466 #ifdef COMPAT_FREEBSD4
467 #ifndef _SYS_SYSPROTO_H_
468 struct freebsd4_sigaction_args {
469 int sig;
470 struct sigaction *act;
471 struct sigaction *oact;
472 };
473 #endif
474 /*
475 * MPSAFE
476 */
477 int
478 freebsd4_sigaction(td, uap)
479 struct thread *td;
480 register struct freebsd4_sigaction_args *uap;
481 {
482 struct sigaction act, oact;
483 register struct sigaction *actp, *oactp;
484 int error;
485
486
487 actp = (uap->act != NULL) ? &act : NULL;
488 oactp = (uap->oact != NULL) ? &oact : NULL;
489 if (actp) {
490 error = copyin(uap->act, actp, sizeof(act));
491 if (error)
492 return (error);
493 }
494 error = kern_sigaction(td, uap->sig, actp, oactp, KSA_FREEBSD4);
495 if (oactp && !error)
496 error = copyout(oactp, uap->oact, sizeof(oact));
497 return (error);
498 }
499 #endif /* COMAPT_FREEBSD4 */
500
501 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
502 #ifndef _SYS_SYSPROTO_H_
503 struct osigaction_args {
504 int signum;
505 struct osigaction *nsa;
506 struct osigaction *osa;
507 };
508 #endif
509 /*
510 * MPSAFE
511 */
512 int
513 osigaction(td, uap)
514 struct thread *td;
515 register struct osigaction_args *uap;
516 {
517 struct osigaction sa;
518 struct sigaction nsa, osa;
519 register struct sigaction *nsap, *osap;
520 int error;
521
522 if (uap->signum <= 0 || uap->signum >= ONSIG)
523 return (EINVAL);
524
525 nsap = (uap->nsa != NULL) ? &nsa : NULL;
526 osap = (uap->osa != NULL) ? &osa : NULL;
527
528 if (nsap) {
529 error = copyin(uap->nsa, &sa, sizeof(sa));
530 if (error)
531 return (error);
532 nsap->sa_handler = sa.sa_handler;
533 nsap->sa_flags = sa.sa_flags;
534 OSIG2SIG(sa.sa_mask, nsap->sa_mask);
535 }
536 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
537 if (osap && !error) {
538 sa.sa_handler = osap->sa_handler;
539 sa.sa_flags = osap->sa_flags;
540 SIG2OSIG(osap->sa_mask, sa.sa_mask);
541 error = copyout(&sa, uap->osa, sizeof(sa));
542 }
543 return (error);
544 }
545
546 #if !defined(__i386__) && !defined(__alpha__)
547 /* Avoid replicating the same stub everywhere */
548 int
549 osigreturn(td, uap)
550 struct thread *td;
551 struct osigreturn_args *uap;
552 {
553
554 return (nosys(td, (struct nosys_args *)uap));
555 }
556 #endif
557 #endif /* COMPAT_43 */
558
559 /*
560 * Initialize signal state for process 0;
561 * set to ignore signals that are ignored by default.
562 */
563 void
564 siginit(p)
565 struct proc *p;
566 {
567 register int i;
568 struct sigacts *ps;
569
570 PROC_LOCK(p);
571 ps = p->p_sigacts;
572 mtx_lock(&ps->ps_mtx);
573 for (i = 1; i <= NSIG; i++)
574 if (sigprop(i) & SA_IGNORE && i != SIGCONT)
575 SIGADDSET(ps->ps_sigignore, i);
576 mtx_unlock(&ps->ps_mtx);
577 PROC_UNLOCK(p);
578 }
579
580 /*
581 * Reset signals for an exec of the specified process.
582 */
583 void
584 execsigs(struct proc *p)
585 {
586 struct sigacts *ps;
587 int sig;
588 struct thread *td;
589
590 /*
591 * Reset caught signals. Held signals remain held
592 * through td_sigmask (unless they were caught,
593 * and are now ignored by default).
594 */
595 PROC_LOCK_ASSERT(p, MA_OWNED);
596 td = FIRST_THREAD_IN_PROC(p);
597 ps = p->p_sigacts;
598 mtx_lock(&ps->ps_mtx);
599 while (SIGNOTEMPTY(ps->ps_sigcatch)) {
600 sig = sig_ffs(&ps->ps_sigcatch);
601 SIGDELSET(ps->ps_sigcatch, sig);
602 if (sigprop(sig) & SA_IGNORE) {
603 if (sig != SIGCONT)
604 SIGADDSET(ps->ps_sigignore, sig);
605 SIGDELSET(p->p_siglist, sig);
606 /*
607 * There is only one thread at this point.
608 */
609 SIGDELSET(td->td_siglist, sig);
610 }
611 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
612 }
613 /*
614 * Reset stack state to the user stack.
615 * Clear set of signals caught on the signal stack.
616 */
617 td->td_sigstk.ss_flags = SS_DISABLE;
618 td->td_sigstk.ss_size = 0;
619 td->td_sigstk.ss_sp = 0;
620 td->td_pflags &= ~TDP_ALTSTACK;
621 /*
622 * Reset no zombies if child dies flag as Solaris does.
623 */
624 ps->ps_flag &= ~(PS_NOCLDWAIT | PS_CLDSIGIGN);
625 if (ps->ps_sigact[_SIG_IDX(SIGCHLD)] == SIG_IGN)
626 ps->ps_sigact[_SIG_IDX(SIGCHLD)] = SIG_DFL;
627 mtx_unlock(&ps->ps_mtx);
628 }
629
630 /*
631 * kern_sigprocmask()
632 *
633 * Manipulate signal mask.
634 */
635 int
636 kern_sigprocmask(td, how, set, oset, old)
637 struct thread *td;
638 int how;
639 sigset_t *set, *oset;
640 int old;
641 {
642 int error;
643
644 PROC_LOCK(td->td_proc);
645 if (oset != NULL)
646 *oset = td->td_sigmask;
647
648 error = 0;
649 if (set != NULL) {
650 switch (how) {
651 case SIG_BLOCK:
652 SIG_CANTMASK(*set);
653 SIGSETOR(td->td_sigmask, *set);
654 break;
655 case SIG_UNBLOCK:
656 SIGSETNAND(td->td_sigmask, *set);
657 signotify(td);
658 break;
659 case SIG_SETMASK:
660 SIG_CANTMASK(*set);
661 if (old)
662 SIGSETLO(td->td_sigmask, *set);
663 else
664 td->td_sigmask = *set;
665 signotify(td);
666 break;
667 default:
668 error = EINVAL;
669 break;
670 }
671 }
672 PROC_UNLOCK(td->td_proc);
673 return (error);
674 }
675
676 /*
677 * sigprocmask() - MP SAFE
678 */
679
680 #ifndef _SYS_SYSPROTO_H_
681 struct sigprocmask_args {
682 int how;
683 const sigset_t *set;
684 sigset_t *oset;
685 };
686 #endif
687 int
688 sigprocmask(td, uap)
689 register struct thread *td;
690 struct sigprocmask_args *uap;
691 {
692 sigset_t set, oset;
693 sigset_t *setp, *osetp;
694 int error;
695
696 setp = (uap->set != NULL) ? &set : NULL;
697 osetp = (uap->oset != NULL) ? &oset : NULL;
698 if (setp) {
699 error = copyin(uap->set, setp, sizeof(set));
700 if (error)
701 return (error);
702 }
703 error = kern_sigprocmask(td, uap->how, setp, osetp, 0);
704 if (osetp && !error) {
705 error = copyout(osetp, uap->oset, sizeof(oset));
706 }
707 return (error);
708 }
709
710 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
711 /*
712 * osigprocmask() - MP SAFE
713 */
714 #ifndef _SYS_SYSPROTO_H_
715 struct osigprocmask_args {
716 int how;
717 osigset_t mask;
718 };
719 #endif
720 int
721 osigprocmask(td, uap)
722 register struct thread *td;
723 struct osigprocmask_args *uap;
724 {
725 sigset_t set, oset;
726 int error;
727
728 OSIG2SIG(uap->mask, set);
729 error = kern_sigprocmask(td, uap->how, &set, &oset, 1);
730 SIG2OSIG(oset, td->td_retval[0]);
731 return (error);
732 }
733 #endif /* COMPAT_43 */
734
735 #ifndef _SYS_SYSPROTO_H_
736 struct sigpending_args {
737 sigset_t *set;
738 };
739 #endif
740 /*
741 * MPSAFE
742 */
743 int
744 sigwait(struct thread *td, struct sigwait_args *uap)
745 {
746 siginfo_t info;
747 sigset_t set;
748 int error;
749
750 error = copyin(uap->set, &set, sizeof(set));
751 if (error) {
752 td->td_retval[0] = error;
753 return (0);
754 }
755
756 error = kern_sigtimedwait(td, set, &info, NULL);
757 if (error) {
758 if (error == ERESTART)
759 return (error);
760 td->td_retval[0] = error;
761 return (0);
762 }
763
764 error = copyout(&info.si_signo, uap->sig, sizeof(info.si_signo));
765 /* Repost if we got an error. */
766 if (error && info.si_signo) {
767 PROC_LOCK(td->td_proc);
768 tdsignal(td, info.si_signo, SIGTARGET_TD);
769 PROC_UNLOCK(td->td_proc);
770 }
771 td->td_retval[0] = error;
772 return (0);
773 }
774 /*
775 * MPSAFE
776 */
777 int
778 sigtimedwait(struct thread *td, struct sigtimedwait_args *uap)
779 {
780 struct timespec ts;
781 struct timespec *timeout;
782 sigset_t set;
783 siginfo_t info;
784 int error;
785
786 if (uap->timeout) {
787 error = copyin(uap->timeout, &ts, sizeof(ts));
788 if (error)
789 return (error);
790
791 timeout = &ts;
792 } else
793 timeout = NULL;
794
795 error = copyin(uap->set, &set, sizeof(set));
796 if (error)
797 return (error);
798
799 error = kern_sigtimedwait(td, set, &info, timeout);
800 if (error)
801 return (error);
802
803 if (uap->info)
804 error = copyout(&info, uap->info, sizeof(info));
805 /* Repost if we got an error. */
806 if (error && info.si_signo) {
807 PROC_LOCK(td->td_proc);
808 tdsignal(td, info.si_signo, SIGTARGET_TD);
809 PROC_UNLOCK(td->td_proc);
810 } else {
811 td->td_retval[0] = info.si_signo;
812 }
813 return (error);
814 }
815
816 /*
817 * MPSAFE
818 */
819 int
820 sigwaitinfo(struct thread *td, struct sigwaitinfo_args *uap)
821 {
822 siginfo_t info;
823 sigset_t set;
824 int error;
825
826 error = copyin(uap->set, &set, sizeof(set));
827 if (error)
828 return (error);
829
830 error = kern_sigtimedwait(td, set, &info, NULL);
831 if (error)
832 return (error);
833
834 if (uap->info)
835 error = copyout(&info, uap->info, sizeof(info));
836 /* Repost if we got an error. */
837 if (error && info.si_signo) {
838 PROC_LOCK(td->td_proc);
839 tdsignal(td, info.si_signo, SIGTARGET_TD);
840 PROC_UNLOCK(td->td_proc);
841 } else {
842 td->td_retval[0] = info.si_signo;
843 }
844 return (error);
845 }
846
847 static int
848 kern_sigtimedwait(struct thread *td, sigset_t waitset, siginfo_t *info,
849 struct timespec *timeout)
850 {
851 struct sigacts *ps;
852 sigset_t savedmask;
853 struct proc *p;
854 int error, sig, hz, i, timevalid = 0;
855 struct timespec rts, ets, ts;
856 struct timeval tv;
857
858 p = td->td_proc;
859 error = 0;
860 sig = 0;
861 SIG_CANTMASK(waitset);
862
863 PROC_LOCK(p);
864 ps = p->p_sigacts;
865 savedmask = td->td_sigmask;
866 if (timeout) {
867 if (timeout->tv_nsec >= 0 && timeout->tv_nsec < 1000000000) {
868 timevalid = 1;
869 getnanouptime(&rts);
870 ets = rts;
871 timespecadd(&ets, timeout);
872 }
873 }
874
875 restart:
876 for (i = 1; i <= _SIG_MAXSIG; ++i) {
877 if (!SIGISMEMBER(waitset, i))
878 continue;
879 if (!SIGISMEMBER(td->td_siglist, i)) {
880 if (SIGISMEMBER(p->p_siglist, i)) {
881 if (p->p_flag & P_SA) {
882 p->p_flag |= P_SIGEVENT;
883 wakeup(&p->p_siglist);
884 }
885 SIGDELSET(p->p_siglist, i);
886 SIGADDSET(td->td_siglist, i);
887 } else
888 continue;
889 }
890
891 SIGFILLSET(td->td_sigmask);
892 SIG_CANTMASK(td->td_sigmask);
893 SIGDELSET(td->td_sigmask, i);
894 mtx_lock(&ps->ps_mtx);
895 sig = cursig(td);
896 mtx_unlock(&ps->ps_mtx);
897 if (sig)
898 goto out;
899 else {
900 /*
901 * Because cursig() may have stopped current thread,
902 * after it is resumed, things may have already been
903 * changed, it should rescan any pending signals.
904 */
905 goto restart;
906 }
907 }
908 if (error)
909 goto out;
910
911 /*
912 * POSIX says this must be checked after looking for pending
913 * signals.
914 */
915 if (timeout) {
916 if (!timevalid) {
917 error = EINVAL;
918 goto out;
919 }
920 getnanouptime(&rts);
921 if (timespeccmp(&rts, &ets, >=)) {
922 error = EAGAIN;
923 goto out;
924 }
925 ts = ets;
926 timespecsub(&ts, &rts);
927 TIMESPEC_TO_TIMEVAL(&tv, &ts);
928 hz = tvtohz(&tv);
929 } else
930 hz = 0;
931
932 td->td_sigmask = savedmask;
933 SIGSETNAND(td->td_sigmask, waitset);
934 signotify(td);
935 error = msleep(&ps, &p->p_mtx, PPAUSE|PCATCH, "sigwait", hz);
936 if (timeout) {
937 if (error == ERESTART) {
938 /* timeout can not be restarted. */
939 error = EINTR;
940 } else if (error == EAGAIN) {
941 /* will calculate timeout by ourself. */
942 error = 0;
943 }
944 }
945 goto restart;
946
947 out:
948 td->td_sigmask = savedmask;
949 signotify(td);
950 if (sig) {
951 SIGDELSET(td->td_siglist, sig);
952 bzero(info, sizeof(*info));
953 info->si_signo = sig;
954 info->si_code = 0;
955 error = 0;
956
957 #ifdef KTRACE
958 if (KTRPOINT(td, KTR_PSIG)) {
959 sig_t action;
960
961 mtx_lock(&ps->ps_mtx);
962 action = ps->ps_sigact[_SIG_IDX(sig)];
963 mtx_unlock(&ps->ps_mtx);
964 ktrpsig(sig, action, &td->td_sigmask, 0);
965 }
966 #endif
967 _STOPEVENT(p, S_SIG, sig);
968
969 if (sig == SIGKILL) {
970 p->p_code = 0;
971 p->p_sig = sig;
972 sigexit(td, sig);
973 }
974 }
975 PROC_UNLOCK(p);
976 return (error);
977 }
978
979 /*
980 * MPSAFE
981 */
982 int
983 sigpending(td, uap)
984 struct thread *td;
985 struct sigpending_args *uap;
986 {
987 struct proc *p = td->td_proc;
988 sigset_t siglist;
989
990 PROC_LOCK(p);
991 siglist = p->p_siglist;
992 SIGSETOR(siglist, td->td_siglist);
993 PROC_UNLOCK(p);
994 return (copyout(&siglist, uap->set, sizeof(sigset_t)));
995 }
996
997 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
998 #ifndef _SYS_SYSPROTO_H_
999 struct osigpending_args {
1000 int dummy;
1001 };
1002 #endif
1003 /*
1004 * MPSAFE
1005 */
1006 int
1007 osigpending(td, uap)
1008 struct thread *td;
1009 struct osigpending_args *uap;
1010 {
1011 struct proc *p = td->td_proc;
1012 sigset_t siglist;
1013
1014 PROC_LOCK(p);
1015 siglist = p->p_siglist;
1016 SIGSETOR(siglist, td->td_siglist);
1017 PROC_UNLOCK(p);
1018 SIG2OSIG(siglist, td->td_retval[0]);
1019 return (0);
1020 }
1021 #endif /* COMPAT_43 */
1022
1023 #if defined(COMPAT_43)
1024 /*
1025 * Generalized interface signal handler, 4.3-compatible.
1026 */
1027 #ifndef _SYS_SYSPROTO_H_
1028 struct osigvec_args {
1029 int signum;
1030 struct sigvec *nsv;
1031 struct sigvec *osv;
1032 };
1033 #endif
1034 /*
1035 * MPSAFE
1036 */
1037 /* ARGSUSED */
1038 int
1039 osigvec(td, uap)
1040 struct thread *td;
1041 register struct osigvec_args *uap;
1042 {
1043 struct sigvec vec;
1044 struct sigaction nsa, osa;
1045 register struct sigaction *nsap, *osap;
1046 int error;
1047
1048 if (uap->signum <= 0 || uap->signum >= ONSIG)
1049 return (EINVAL);
1050 nsap = (uap->nsv != NULL) ? &nsa : NULL;
1051 osap = (uap->osv != NULL) ? &osa : NULL;
1052 if (nsap) {
1053 error = copyin(uap->nsv, &vec, sizeof(vec));
1054 if (error)
1055 return (error);
1056 nsap->sa_handler = vec.sv_handler;
1057 OSIG2SIG(vec.sv_mask, nsap->sa_mask);
1058 nsap->sa_flags = vec.sv_flags;
1059 nsap->sa_flags ^= SA_RESTART; /* opposite of SV_INTERRUPT */
1060 }
1061 error = kern_sigaction(td, uap->signum, nsap, osap, KSA_OSIGSET);
1062 if (osap && !error) {
1063 vec.sv_handler = osap->sa_handler;
1064 SIG2OSIG(osap->sa_mask, vec.sv_mask);
1065 vec.sv_flags = osap->sa_flags;
1066 vec.sv_flags &= ~SA_NOCLDWAIT;
1067 vec.sv_flags ^= SA_RESTART;
1068 error = copyout(&vec, uap->osv, sizeof(vec));
1069 }
1070 return (error);
1071 }
1072
1073 #ifndef _SYS_SYSPROTO_H_
1074 struct osigblock_args {
1075 int mask;
1076 };
1077 #endif
1078 /*
1079 * MPSAFE
1080 */
1081 int
1082 osigblock(td, uap)
1083 register struct thread *td;
1084 struct osigblock_args *uap;
1085 {
1086 struct proc *p = td->td_proc;
1087 sigset_t set;
1088
1089 OSIG2SIG(uap->mask, set);
1090 SIG_CANTMASK(set);
1091 PROC_LOCK(p);
1092 SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1093 SIGSETOR(td->td_sigmask, set);
1094 PROC_UNLOCK(p);
1095 return (0);
1096 }
1097
1098 #ifndef _SYS_SYSPROTO_H_
1099 struct osigsetmask_args {
1100 int mask;
1101 };
1102 #endif
1103 /*
1104 * MPSAFE
1105 */
1106 int
1107 osigsetmask(td, uap)
1108 struct thread *td;
1109 struct osigsetmask_args *uap;
1110 {
1111 struct proc *p = td->td_proc;
1112 sigset_t set;
1113
1114 OSIG2SIG(uap->mask, set);
1115 SIG_CANTMASK(set);
1116 PROC_LOCK(p);
1117 SIG2OSIG(td->td_sigmask, td->td_retval[0]);
1118 SIGSETLO(td->td_sigmask, set);
1119 signotify(td);
1120 PROC_UNLOCK(p);
1121 return (0);
1122 }
1123 #endif /* COMPAT_43 */
1124
1125 /*
1126 * Suspend process until signal, providing mask to be set
1127 * in the meantime.
1128 ***** XXXKSE this doesn't make sense under KSE.
1129 ***** Do we suspend the thread or all threads in the process?
1130 ***** How do we suspend threads running NOW on another processor?
1131 */
1132 #ifndef _SYS_SYSPROTO_H_
1133 struct sigsuspend_args {
1134 const sigset_t *sigmask;
1135 };
1136 #endif
1137 /*
1138 * MPSAFE
1139 */
1140 /* ARGSUSED */
1141 int
1142 sigsuspend(td, uap)
1143 struct thread *td;
1144 struct sigsuspend_args *uap;
1145 {
1146 sigset_t mask;
1147 int error;
1148
1149 error = copyin(uap->sigmask, &mask, sizeof(mask));
1150 if (error)
1151 return (error);
1152 return (kern_sigsuspend(td, mask));
1153 }
1154
1155 int
1156 kern_sigsuspend(struct thread *td, sigset_t mask)
1157 {
1158 struct proc *p = td->td_proc;
1159
1160 /*
1161 * When returning from sigsuspend, we want
1162 * the old mask to be restored after the
1163 * signal handler has finished. Thus, we
1164 * save it here and mark the sigacts structure
1165 * to indicate this.
1166 */
1167 PROC_LOCK(p);
1168 td->td_oldsigmask = td->td_sigmask;
1169 td->td_pflags |= TDP_OLDMASK;
1170 SIG_CANTMASK(mask);
1171 td->td_sigmask = mask;
1172 signotify(td);
1173 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "pause", 0) == 0)
1174 /* void */;
1175 PROC_UNLOCK(p);
1176 /* always return EINTR rather than ERESTART... */
1177 return (EINTR);
1178 }
1179
1180 #ifdef COMPAT_43 /* XXX - COMPAT_FBSD3 */
1181 /*
1182 * Compatibility sigsuspend call for old binaries. Note nonstandard calling
1183 * convention: libc stub passes mask, not pointer, to save a copyin.
1184 */
1185 #ifndef _SYS_SYSPROTO_H_
1186 struct osigsuspend_args {
1187 osigset_t mask;
1188 };
1189 #endif
1190 /*
1191 * MPSAFE
1192 */
1193 /* ARGSUSED */
1194 int
1195 osigsuspend(td, uap)
1196 struct thread *td;
1197 struct osigsuspend_args *uap;
1198 {
1199 struct proc *p = td->td_proc;
1200 sigset_t mask;
1201
1202 PROC_LOCK(p);
1203 td->td_oldsigmask = td->td_sigmask;
1204 td->td_pflags |= TDP_OLDMASK;
1205 OSIG2SIG(uap->mask, mask);
1206 SIG_CANTMASK(mask);
1207 SIGSETLO(td->td_sigmask, mask);
1208 signotify(td);
1209 while (msleep(&p->p_sigacts, &p->p_mtx, PPAUSE|PCATCH, "opause", 0) == 0)
1210 /* void */;
1211 PROC_UNLOCK(p);
1212 /* always return EINTR rather than ERESTART... */
1213 return (EINTR);
1214 }
1215 #endif /* COMPAT_43 */
1216
1217 #if defined(COMPAT_43)
1218 #ifndef _SYS_SYSPROTO_H_
1219 struct osigstack_args {
1220 struct sigstack *nss;
1221 struct sigstack *oss;
1222 };
1223 #endif
1224 /*
1225 * MPSAFE
1226 */
1227 /* ARGSUSED */
1228 int
1229 osigstack(td, uap)
1230 struct thread *td;
1231 register struct osigstack_args *uap;
1232 {
1233 struct sigstack nss, oss;
1234 int error = 0;
1235
1236 if (uap->nss != NULL) {
1237 error = copyin(uap->nss, &nss, sizeof(nss));
1238 if (error)
1239 return (error);
1240 }
1241 oss.ss_sp = td->td_sigstk.ss_sp;
1242 oss.ss_onstack = sigonstack(cpu_getstack(td));
1243 if (uap->nss != NULL) {
1244 td->td_sigstk.ss_sp = nss.ss_sp;
1245 td->td_sigstk.ss_size = 0;
1246 td->td_sigstk.ss_flags |= nss.ss_onstack & SS_ONSTACK;
1247 td->td_pflags |= TDP_ALTSTACK;
1248 }
1249 if (uap->oss != NULL)
1250 error = copyout(&oss, uap->oss, sizeof(oss));
1251
1252 return (error);
1253 }
1254 #endif /* COMPAT_43 */
1255
1256 #ifndef _SYS_SYSPROTO_H_
1257 struct sigaltstack_args {
1258 stack_t *ss;
1259 stack_t *oss;
1260 };
1261 #endif
1262 /*
1263 * MPSAFE
1264 */
1265 /* ARGSUSED */
1266 int
1267 sigaltstack(td, uap)
1268 struct thread *td;
1269 register struct sigaltstack_args *uap;
1270 {
1271 stack_t ss, oss;
1272 int error;
1273
1274 if (uap->ss != NULL) {
1275 error = copyin(uap->ss, &ss, sizeof(ss));
1276 if (error)
1277 return (error);
1278 }
1279 error = kern_sigaltstack(td, (uap->ss != NULL) ? &ss : NULL,
1280 (uap->oss != NULL) ? &oss : NULL);
1281 if (error)
1282 return (error);
1283 if (uap->oss != NULL)
1284 error = copyout(&oss, uap->oss, sizeof(stack_t));
1285 return (error);
1286 }
1287
1288 int
1289 kern_sigaltstack(struct thread *td, stack_t *ss, stack_t *oss)
1290 {
1291 struct proc *p = td->td_proc;
1292 int oonstack;
1293
1294 oonstack = sigonstack(cpu_getstack(td));
1295
1296 if (oss != NULL) {
1297 *oss = td->td_sigstk;
1298 oss->ss_flags = (td->td_pflags & TDP_ALTSTACK)
1299 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
1300 }
1301
1302 if (ss != NULL) {
1303 if (oonstack)
1304 return (EPERM);
1305 if ((ss->ss_flags & ~SS_DISABLE) != 0)
1306 return (EINVAL);
1307 if (!(ss->ss_flags & SS_DISABLE)) {
1308 if (ss->ss_size < p->p_sysent->sv_minsigstksz) {
1309 return (ENOMEM);
1310 }
1311 td->td_sigstk = *ss;
1312 td->td_pflags |= TDP_ALTSTACK;
1313 } else {
1314 td->td_pflags &= ~TDP_ALTSTACK;
1315 }
1316 }
1317 return (0);
1318 }
1319
1320 /*
1321 * Common code for kill process group/broadcast kill.
1322 * cp is calling process.
1323 */
1324 static int
1325 killpg1(td, sig, pgid, all)
1326 register struct thread *td;
1327 int sig, pgid, all;
1328 {
1329 register struct proc *p;
1330 struct pgrp *pgrp;
1331 int nfound = 0;
1332
1333 if (all) {
1334 /*
1335 * broadcast
1336 */
1337 sx_slock(&allproc_lock);
1338 LIST_FOREACH(p, &allproc, p_list) {
1339 PROC_LOCK(p);
1340 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1341 p == td->td_proc || p->p_state == PRS_NEW) {
1342 PROC_UNLOCK(p);
1343 continue;
1344 }
1345 if (p_cansignal(td, p, sig) == 0) {
1346 nfound++;
1347 if (sig)
1348 psignal(p, sig);
1349 }
1350 PROC_UNLOCK(p);
1351 }
1352 sx_sunlock(&allproc_lock);
1353 } else {
1354 sx_slock(&proctree_lock);
1355 if (pgid == 0) {
1356 /*
1357 * zero pgid means send to my process group.
1358 */
1359 pgrp = td->td_proc->p_pgrp;
1360 PGRP_LOCK(pgrp);
1361 } else {
1362 pgrp = pgfind(pgid);
1363 if (pgrp == NULL) {
1364 sx_sunlock(&proctree_lock);
1365 return (ESRCH);
1366 }
1367 }
1368 sx_sunlock(&proctree_lock);
1369 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1370 PROC_LOCK(p);
1371 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
1372 p->p_state == PRS_NEW ) {
1373 PROC_UNLOCK(p);
1374 continue;
1375 }
1376 if (p_cansignal(td, p, sig) == 0) {
1377 nfound++;
1378 if (sig)
1379 psignal(p, sig);
1380 }
1381 PROC_UNLOCK(p);
1382 }
1383 PGRP_UNLOCK(pgrp);
1384 }
1385 return (nfound ? 0 : ESRCH);
1386 }
1387
1388 #ifndef _SYS_SYSPROTO_H_
1389 struct kill_args {
1390 int pid;
1391 int signum;
1392 };
1393 #endif
1394 /*
1395 * MPSAFE
1396 */
1397 /* ARGSUSED */
1398 int
1399 kill(td, uap)
1400 register struct thread *td;
1401 register struct kill_args *uap;
1402 {
1403 register struct proc *p;
1404 int error;
1405
1406 AUDIT_ARG(signum, uap->signum);
1407 AUDIT_ARG(pid, uap->pid);
1408 if ((u_int)uap->signum > _SIG_MAXSIG)
1409 return (EINVAL);
1410
1411 if (uap->pid > 0) {
1412 /* kill single process */
1413 if ((p = pfind(uap->pid)) == NULL) {
1414 if ((p = zpfind(uap->pid)) == NULL)
1415 return (ESRCH);
1416 }
1417 AUDIT_ARG(process, p);
1418 error = p_cansignal(td, p, uap->signum);
1419 if (error == 0 && uap->signum)
1420 psignal(p, uap->signum);
1421 PROC_UNLOCK(p);
1422 return (error);
1423 }
1424 switch (uap->pid) {
1425 case -1: /* broadcast signal */
1426 return (killpg1(td, uap->signum, 0, 1));
1427 case 0: /* signal own process group */
1428 return (killpg1(td, uap->signum, 0, 0));
1429 default: /* negative explicit process group */
1430 return (killpg1(td, uap->signum, -uap->pid, 0));
1431 }
1432 /* NOTREACHED */
1433 }
1434
1435 #if defined(COMPAT_43)
1436 #ifndef _SYS_SYSPROTO_H_
1437 struct okillpg_args {
1438 int pgid;
1439 int signum;
1440 };
1441 #endif
1442 /*
1443 * MPSAFE
1444 */
1445 /* ARGSUSED */
1446 int
1447 okillpg(td, uap)
1448 struct thread *td;
1449 register struct okillpg_args *uap;
1450 {
1451
1452 AUDIT_ARG(signum, uap->signum);
1453 AUDIT_ARG(pid, uap->pgid);
1454 if ((u_int)uap->signum > _SIG_MAXSIG)
1455 return (EINVAL);
1456 return (killpg1(td, uap->signum, uap->pgid, 0));
1457 }
1458 #endif /* COMPAT_43 */
1459
1460 /*
1461 * Send a signal to a process group.
1462 */
1463 void
1464 gsignal(pgid, sig)
1465 int pgid, sig;
1466 {
1467 struct pgrp *pgrp;
1468
1469 if (pgid != 0) {
1470 sx_slock(&proctree_lock);
1471 pgrp = pgfind(pgid);
1472 sx_sunlock(&proctree_lock);
1473 if (pgrp != NULL) {
1474 pgsignal(pgrp, sig, 0);
1475 PGRP_UNLOCK(pgrp);
1476 }
1477 }
1478 }
1479
1480 /*
1481 * Send a signal to a process group. If checktty is 1,
1482 * limit to members which have a controlling terminal.
1483 */
1484 void
1485 pgsignal(pgrp, sig, checkctty)
1486 struct pgrp *pgrp;
1487 int sig, checkctty;
1488 {
1489 register struct proc *p;
1490
1491 if (pgrp) {
1492 PGRP_LOCK_ASSERT(pgrp, MA_OWNED);
1493 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
1494 PROC_LOCK(p);
1495 if (checkctty == 0 || p->p_flag & P_CONTROLT)
1496 psignal(p, sig);
1497 PROC_UNLOCK(p);
1498 }
1499 }
1500 }
1501
1502 /*
1503 * Send a signal caused by a trap to the current thread.
1504 * If it will be caught immediately, deliver it with correct code.
1505 * Otherwise, post it normally.
1506 *
1507 * MPSAFE
1508 */
1509 void
1510 trapsignal(struct thread *td, int sig, u_long code)
1511 {
1512 struct sigacts *ps;
1513 struct proc *p;
1514 siginfo_t siginfo;
1515 int error;
1516
1517 p = td->td_proc;
1518 if (td->td_pflags & TDP_SA) {
1519 if (td->td_mailbox == NULL)
1520 thread_user_enter(td);
1521 PROC_LOCK(p);
1522 SIGDELSET(td->td_sigmask, sig);
1523 mtx_lock_spin(&sched_lock);
1524 /*
1525 * Force scheduling an upcall, so UTS has chance to
1526 * process the signal before thread runs again in
1527 * userland.
1528 */
1529 if (td->td_upcall)
1530 td->td_upcall->ku_flags |= KUF_DOUPCALL;
1531 mtx_unlock_spin(&sched_lock);
1532 } else {
1533 PROC_LOCK(p);
1534 }
1535 ps = p->p_sigacts;
1536 mtx_lock(&ps->ps_mtx);
1537 if ((p->p_flag & P_TRACED) == 0 && SIGISMEMBER(ps->ps_sigcatch, sig) &&
1538 !SIGISMEMBER(td->td_sigmask, sig)) {
1539 p->p_stats->p_ru.ru_nsignals++;
1540 #ifdef KTRACE
1541 if (KTRPOINT(curthread, KTR_PSIG))
1542 ktrpsig(sig, ps->ps_sigact[_SIG_IDX(sig)],
1543 &td->td_sigmask, code);
1544 #endif
1545 if (!(td->td_pflags & TDP_SA))
1546 (*p->p_sysent->sv_sendsig)(
1547 ps->ps_sigact[_SIG_IDX(sig)], sig,
1548 &td->td_sigmask, code);
1549 else if (td->td_mailbox == NULL) {
1550 mtx_unlock(&ps->ps_mtx);
1551 /* UTS caused a sync signal */
1552 p->p_code = code; /* XXX for core dump/debugger */
1553 p->p_sig = sig; /* XXX to verify code */
1554 sigexit(td, sig);
1555 } else {
1556 cpu_thread_siginfo(sig, code, &siginfo);
1557 mtx_unlock(&ps->ps_mtx);
1558 SIGADDSET(td->td_sigmask, sig);
1559 PROC_UNLOCK(p);
1560 error = copyout(&siginfo, &td->td_mailbox->tm_syncsig,
1561 sizeof(siginfo));
1562 PROC_LOCK(p);
1563 /* UTS memory corrupted */
1564 if (error)
1565 sigexit(td, SIGSEGV);
1566 mtx_lock(&ps->ps_mtx);
1567 }
1568 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
1569 if (!SIGISMEMBER(ps->ps_signodefer, sig))
1570 SIGADDSET(td->td_sigmask, sig);
1571 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
1572 /*
1573 * See kern_sigaction() for origin of this code.
1574 */
1575 SIGDELSET(ps->ps_sigcatch, sig);
1576 if (sig != SIGCONT &&
1577 sigprop(sig) & SA_IGNORE)
1578 SIGADDSET(ps->ps_sigignore, sig);
1579 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
1580 }
1581 mtx_unlock(&ps->ps_mtx);
1582 } else {
1583 mtx_unlock(&ps->ps_mtx);
1584 p->p_code = code; /* XXX for core dump/debugger */
1585 p->p_sig = sig; /* XXX to verify code */
1586 tdsignal(td, sig, SIGTARGET_TD);
1587 }
1588 PROC_UNLOCK(p);
1589 }
1590
1591 static struct thread *
1592 sigtd(struct proc *p, int sig, int prop)
1593 {
1594 struct thread *td, *signal_td;
1595
1596 PROC_LOCK_ASSERT(p, MA_OWNED);
1597
1598 /*
1599 * Check if current thread can handle the signal without
1600 * switching conetxt to another thread.
1601 */
1602 if (curproc == p && !SIGISMEMBER(curthread->td_sigmask, sig))
1603 return (curthread);
1604 signal_td = NULL;
1605 mtx_lock_spin(&sched_lock);
1606 FOREACH_THREAD_IN_PROC(p, td) {
1607 if (!SIGISMEMBER(td->td_sigmask, sig)) {
1608 signal_td = td;
1609 break;
1610 }
1611 }
1612 if (signal_td == NULL)
1613 signal_td = FIRST_THREAD_IN_PROC(p);
1614 mtx_unlock_spin(&sched_lock);
1615 return (signal_td);
1616 }
1617
1618 /*
1619 * Send the signal to the process. If the signal has an action, the action
1620 * is usually performed by the target process rather than the caller; we add
1621 * the signal to the set of pending signals for the process.
1622 *
1623 * Exceptions:
1624 * o When a stop signal is sent to a sleeping process that takes the
1625 * default action, the process is stopped without awakening it.
1626 * o SIGCONT restarts stopped processes (or puts them back to sleep)
1627 * regardless of the signal action (eg, blocked or ignored).
1628 *
1629 * Other ignored signals are discarded immediately.
1630 *
1631 * MPSAFE
1632 */
1633 void
1634 psignal(struct proc *p, int sig)
1635 {
1636 struct thread *td;
1637 int prop;
1638
1639 if (!_SIG_VALID(sig))
1640 panic("psignal(): invalid signal");
1641
1642 PROC_LOCK_ASSERT(p, MA_OWNED);
1643 /*
1644 * IEEE Std 1003.1-2001: return success when killing a zombie.
1645 */
1646 if (p->p_state == PRS_ZOMBIE)
1647 return;
1648 prop = sigprop(sig);
1649
1650 /*
1651 * Find a thread to deliver the signal to.
1652 */
1653 td = sigtd(p, sig, prop);
1654
1655 tdsignal(td, sig, SIGTARGET_P);
1656 }
1657
1658 /*
1659 * MPSAFE
1660 */
1661 void
1662 tdsignal(struct thread *td, int sig, sigtarget_t target)
1663 {
1664 sigset_t saved;
1665 struct proc *p = td->td_proc;
1666
1667 if (p->p_flag & P_SA)
1668 saved = p->p_siglist;
1669 do_tdsignal(td, sig, target);
1670 if ((p->p_flag & P_SA) && !(p->p_flag & P_SIGEVENT)) {
1671 if (!SIGSETEQ(saved, p->p_siglist)) {
1672 /* pending set changed */
1673 p->p_flag |= P_SIGEVENT;
1674 wakeup(&p->p_siglist);
1675 }
1676 }
1677 }
1678
1679 static void
1680 do_tdsignal(struct thread *td, int sig, sigtarget_t target)
1681 {
1682 struct proc *p;
1683 register sig_t action;
1684 sigset_t *siglist;
1685 struct thread *td0;
1686 register int prop;
1687 struct sigacts *ps;
1688 int intrval;
1689
1690 if (!_SIG_VALID(sig))
1691 panic("do_tdsignal(): invalid signal");
1692
1693 p = td->td_proc;
1694 ps = p->p_sigacts;
1695
1696 PROC_LOCK_ASSERT(p, MA_OWNED);
1697 KNOTE_LOCKED(&p->p_klist, NOTE_SIGNAL | sig);
1698
1699 prop = sigprop(sig);
1700
1701 /*
1702 * If the signal is blocked and not destined for this thread, then
1703 * assign it to the process so that we can find it later in the first
1704 * thread that unblocks it. Otherwise, assign it to this thread now.
1705 */
1706 if (target == SIGTARGET_TD) {
1707 siglist = &td->td_siglist;
1708 } else {
1709 if (!SIGISMEMBER(td->td_sigmask, sig))
1710 siglist = &td->td_siglist;
1711 else
1712 siglist = &p->p_siglist;
1713 }
1714
1715 /*
1716 * If the signal is being ignored,
1717 * then we forget about it immediately.
1718 * (Note: we don't set SIGCONT in ps_sigignore,
1719 * and if it is set to SIG_IGN,
1720 * action will be SIG_DFL here.)
1721 */
1722 mtx_lock(&ps->ps_mtx);
1723 if (SIGISMEMBER(ps->ps_sigignore, sig) ||
1724 (p->p_flag & P_WEXIT)) {
1725 mtx_unlock(&ps->ps_mtx);
1726 return;
1727 }
1728 if (SIGISMEMBER(td->td_sigmask, sig))
1729 action = SIG_HOLD;
1730 else if (SIGISMEMBER(ps->ps_sigcatch, sig))
1731 action = SIG_CATCH;
1732 else
1733 action = SIG_DFL;
1734 if (SIGISMEMBER(ps->ps_sigintr, sig))
1735 intrval = EINTR;
1736 else
1737 intrval = ERESTART;
1738 mtx_unlock(&ps->ps_mtx);
1739
1740 if (prop & SA_CONT) {
1741 SIG_STOPSIGMASK(p->p_siglist);
1742 /*
1743 * XXX Should investigate leaving STOP and CONT sigs only in
1744 * the proc's siglist.
1745 */
1746 mtx_lock_spin(&sched_lock);
1747 FOREACH_THREAD_IN_PROC(p, td0)
1748 SIG_STOPSIGMASK(td0->td_siglist);
1749 mtx_unlock_spin(&sched_lock);
1750 }
1751
1752 if (prop & SA_STOP) {
1753 /*
1754 * If sending a tty stop signal to a member of an orphaned
1755 * process group, discard the signal here if the action
1756 * is default; don't stop the process below if sleeping,
1757 * and don't clear any pending SIGCONT.
1758 */
1759 if ((prop & SA_TTYSTOP) &&
1760 (p->p_pgrp->pg_jobc == 0) &&
1761 (action == SIG_DFL))
1762 return;
1763 SIG_CONTSIGMASK(p->p_siglist);
1764 mtx_lock_spin(&sched_lock);
1765 FOREACH_THREAD_IN_PROC(p, td0)
1766 SIG_CONTSIGMASK(td0->td_siglist);
1767 mtx_unlock_spin(&sched_lock);
1768 p->p_flag &= ~P_CONTINUED;
1769 }
1770
1771 SIGADDSET(*siglist, sig);
1772 signotify(td); /* uses schedlock */
1773 /*
1774 * Defer further processing for signals which are held,
1775 * except that stopped processes must be continued by SIGCONT.
1776 */
1777 if (action == SIG_HOLD &&
1778 !((prop & SA_CONT) && (p->p_flag & P_STOPPED_SIG)))
1779 return;
1780 /*
1781 * SIGKILL: Remove procfs STOPEVENTs.
1782 */
1783 if (sig == SIGKILL) {
1784 /* from procfs_ioctl.c: PIOCBIC */
1785 p->p_stops = 0;
1786 /* from procfs_ioctl.c: PIOCCONT */
1787 p->p_step = 0;
1788 wakeup(&p->p_step);
1789 }
1790 /*
1791 * Some signals have a process-wide effect and a per-thread
1792 * component. Most processing occurs when the process next
1793 * tries to cross the user boundary, however there are some
1794 * times when processing needs to be done immediatly, such as
1795 * waking up threads so that they can cross the user boundary.
1796 * We try do the per-process part here.
1797 */
1798 if (P_SHOULDSTOP(p)) {
1799 /*
1800 * The process is in stopped mode. All the threads should be
1801 * either winding down or already on the suspended queue.
1802 */
1803 if (p->p_flag & P_TRACED) {
1804 /*
1805 * The traced process is already stopped,
1806 * so no further action is necessary.
1807 * No signal can restart us.
1808 */
1809 goto out;
1810 }
1811
1812 if (sig == SIGKILL) {
1813 /*
1814 * SIGKILL sets process running.
1815 * It will die elsewhere.
1816 * All threads must be restarted.
1817 */
1818 p->p_flag &= ~P_STOPPED_SIG;
1819 goto runfast;
1820 }
1821
1822 if (prop & SA_CONT) {
1823 /*
1824 * If SIGCONT is default (or ignored), we continue the
1825 * process but don't leave the signal in siglist as
1826 * it has no further action. If SIGCONT is held, we
1827 * continue the process and leave the signal in
1828 * siglist. If the process catches SIGCONT, let it
1829 * handle the signal itself. If it isn't waiting on
1830 * an event, it goes back to run state.
1831 * Otherwise, process goes back to sleep state.
1832 */
1833 p->p_flag &= ~P_STOPPED_SIG;
1834 p->p_flag |= P_CONTINUED;
1835 if (action == SIG_DFL) {
1836 SIGDELSET(*siglist, sig);
1837 } else if (action == SIG_CATCH) {
1838 /*
1839 * The process wants to catch it so it needs
1840 * to run at least one thread, but which one?
1841 * It would seem that the answer would be to
1842 * run an upcall in the next KSE to run, and
1843 * deliver the signal that way. In a NON KSE
1844 * process, we need to make sure that the
1845 * single thread is runnable asap.
1846 * XXXKSE for now however, make them all run.
1847 */
1848 goto runfast;
1849 }
1850 /*
1851 * The signal is not ignored or caught.
1852 */
1853 mtx_lock_spin(&sched_lock);
1854 thread_unsuspend(p);
1855 mtx_unlock_spin(&sched_lock);
1856 goto out;
1857 }
1858
1859 if (prop & SA_STOP) {
1860 /*
1861 * Already stopped, don't need to stop again
1862 * (If we did the shell could get confused).
1863 * Just make sure the signal STOP bit set.
1864 */
1865 p->p_flag |= P_STOPPED_SIG;
1866 SIGDELSET(*siglist, sig);
1867 goto out;
1868 }
1869
1870 /*
1871 * All other kinds of signals:
1872 * If a thread is sleeping interruptibly, simulate a
1873 * wakeup so that when it is continued it will be made
1874 * runnable and can look at the signal. However, don't make
1875 * the PROCESS runnable, leave it stopped.
1876 * It may run a bit until it hits a thread_suspend_check().
1877 */
1878 mtx_lock_spin(&sched_lock);
1879 if (TD_ON_SLEEPQ(td) && (td->td_flags & TDF_SINTR))
1880 sleepq_abort(td, intrval);
1881 mtx_unlock_spin(&sched_lock);
1882 goto out;
1883 /*
1884 * Mutexes are short lived. Threads waiting on them will
1885 * hit thread_suspend_check() soon.
1886 */
1887 } else if (p->p_state == PRS_NORMAL) {
1888 if (p->p_flag & P_TRACED || action == SIG_CATCH) {
1889 mtx_lock_spin(&sched_lock);
1890 tdsigwakeup(td, sig, action, intrval);
1891 mtx_unlock_spin(&sched_lock);
1892 goto out;
1893 }
1894
1895 MPASS(action == SIG_DFL);
1896
1897 if (prop & SA_STOP) {
1898 if (p->p_flag & P_PPWAIT)
1899 goto out;
1900 p->p_flag |= P_STOPPED_SIG;
1901 p->p_xstat = sig;
1902 mtx_lock_spin(&sched_lock);
1903 sig_suspend_threads(td, p, 1);
1904 thread_stopped(p);
1905 if (p->p_numthreads == p->p_suspcount) {
1906 SIGDELSET(p->p_siglist, p->p_xstat);
1907 FOREACH_THREAD_IN_PROC(p, td0)
1908 SIGDELSET(td0->td_siglist, p->p_xstat);
1909 }
1910 mtx_unlock_spin(&sched_lock);
1911 goto out;
1912 }
1913 else
1914 goto runfast;
1915 /* NOTREACHED */
1916 } else {
1917 /* Not in "NORMAL" state. discard the signal. */
1918 SIGDELSET(*siglist, sig);
1919 goto out;
1920 }
1921
1922 /*
1923 * The process is not stopped so we need to apply the signal to all the
1924 * running threads.
1925 */
1926
1927 runfast:
1928 mtx_lock_spin(&sched_lock);
1929 tdsigwakeup(td, sig, action, intrval);
1930 thread_unsuspend(p);
1931 mtx_unlock_spin(&sched_lock);
1932 out:
1933 /* If we jump here, sched_lock should not be owned. */
1934 mtx_assert(&sched_lock, MA_NOTOWNED);
1935 }
1936
1937 /*
1938 * The force of a signal has been directed against a single
1939 * thread. We need to see what we can do about knocking it
1940 * out of any sleep it may be in etc.
1941 */
1942 static void
1943 tdsigwakeup(struct thread *td, int sig, sig_t action, int intrval)
1944 {
1945 struct proc *p = td->td_proc;
1946 register int prop;
1947
1948 PROC_LOCK_ASSERT(p, MA_OWNED);
1949 mtx_assert(&sched_lock, MA_OWNED);
1950 prop = sigprop(sig);
1951
1952 /*
1953 * Bring the priority of a thread up if we want it to get
1954 * killed in this lifetime.
1955 */
1956 if (action == SIG_DFL && (prop & SA_KILL)) {
1957 if (p->p_nice > 0)
1958 sched_nice(td->td_proc, 0);
1959 if (td->td_priority > PUSER)
1960 sched_prio(td, PUSER);
1961 }
1962
1963 if (TD_ON_SLEEPQ(td)) {
1964 /*
1965 * If thread is sleeping uninterruptibly
1966 * we can't interrupt the sleep... the signal will
1967 * be noticed when the process returns through
1968 * trap() or syscall().
1969 */
1970 if ((td->td_flags & TDF_SINTR) == 0)
1971 return;
1972 /*
1973 * If SIGCONT is default (or ignored) and process is
1974 * asleep, we are finished; the process should not
1975 * be awakened.
1976 */
1977 if ((prop & SA_CONT) && action == SIG_DFL) {
1978 SIGDELSET(p->p_siglist, sig);
1979 /*
1980 * It may be on either list in this state.
1981 * Remove from both for now.
1982 */
1983 SIGDELSET(td->td_siglist, sig);
1984 return;
1985 }
1986
1987 /*
1988 * Give low priority threads a better chance to run.
1989 */
1990 if (td->td_priority > PUSER)
1991 sched_prio(td, PUSER);
1992
1993 sleepq_abort(td, intrval);
1994 } else {
1995 /*
1996 * Other states do nothing with the signal immediately,
1997 * other than kicking ourselves if we are running.
1998 * It will either never be noticed, or noticed very soon.
1999 */
2000 #ifdef SMP
2001 if (TD_IS_RUNNING(td) && td != curthread)
2002 forward_signal(td);
2003 #endif
2004 }
2005 }
2006
2007 static void
2008 sig_suspend_threads(struct thread *td, struct proc *p, int sending)
2009 {
2010 struct thread *td2;
2011
2012 PROC_LOCK_ASSERT(p, MA_OWNED);
2013 mtx_assert(&sched_lock, MA_OWNED);
2014
2015 FOREACH_THREAD_IN_PROC(p, td2) {
2016 if ((TD_IS_SLEEPING(td2) || TD_IS_SWAPPED(td2)) &&
2017 (td2->td_flags & TDF_SINTR) &&
2018 !TD_IS_SUSPENDED(td2)) {
2019 thread_suspend_one(td2);
2020 } else {
2021 if (sending || td != td2)
2022 td2->td_flags |= TDF_ASTPENDING;
2023 #ifdef SMP
2024 if (TD_IS_RUNNING(td2) && td2 != td)
2025 forward_signal(td2);
2026 #endif
2027 }
2028 }
2029 }
2030
2031 int
2032 ptracestop(struct thread *td, int sig)
2033 {
2034 struct proc *p = td->td_proc;
2035
2036 PROC_LOCK_ASSERT(p, MA_OWNED);
2037 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2038 &p->p_mtx.mtx_object, "Stopping for traced signal");
2039
2040 mtx_lock_spin(&sched_lock);
2041 td->td_flags |= TDF_XSIG;
2042 mtx_unlock_spin(&sched_lock);
2043 td->td_xsig = sig;
2044 while ((p->p_flag & P_TRACED) && (td->td_flags & TDF_XSIG)) {
2045 if (p->p_flag & P_SINGLE_EXIT) {
2046 mtx_lock_spin(&sched_lock);
2047 td->td_flags &= ~TDF_XSIG;
2048 mtx_unlock_spin(&sched_lock);
2049 return (sig);
2050 }
2051 /*
2052 * Just make wait() to work, the last stopped thread
2053 * will win.
2054 */
2055 p->p_xstat = sig;
2056 p->p_xthread = td;
2057 p->p_flag |= (P_STOPPED_SIG|P_STOPPED_TRACE);
2058 mtx_lock_spin(&sched_lock);
2059 sig_suspend_threads(td, p, 0);
2060 stopme:
2061 thread_stopped(p);
2062 thread_suspend_one(td);
2063 PROC_UNLOCK(p);
2064 DROP_GIANT();
2065 mi_switch(SW_VOL, NULL);
2066 mtx_unlock_spin(&sched_lock);
2067 PICKUP_GIANT();
2068 PROC_LOCK(p);
2069 if (!(p->p_flag & P_TRACED))
2070 break;
2071 if (td->td_flags & TDF_DBSUSPEND) {
2072 if (p->p_flag & P_SINGLE_EXIT)
2073 break;
2074 mtx_lock_spin(&sched_lock);
2075 goto stopme;
2076 }
2077 }
2078 return (td->td_xsig);
2079 }
2080
2081 /*
2082 * If the current process has received a signal (should be caught or cause
2083 * termination, should interrupt current syscall), return the signal number.
2084 * Stop signals with default action are processed immediately, then cleared;
2085 * they aren't returned. This is checked after each entry to the system for
2086 * a syscall or trap (though this can usually be done without calling issignal
2087 * by checking the pending signal masks in cursig.) The normal call
2088 * sequence is
2089 *
2090 * while (sig = cursig(curthread))
2091 * postsig(sig);
2092 */
2093 static int
2094 issignal(td)
2095 struct thread *td;
2096 {
2097 struct proc *p;
2098 struct sigacts *ps;
2099 sigset_t sigpending;
2100 int sig, prop, newsig;
2101
2102 p = td->td_proc;
2103 ps = p->p_sigacts;
2104 mtx_assert(&ps->ps_mtx, MA_OWNED);
2105 PROC_LOCK_ASSERT(p, MA_OWNED);
2106 for (;;) {
2107 int traced = (p->p_flag & P_TRACED) || (p->p_stops & S_SIG);
2108
2109 sigpending = td->td_siglist;
2110 SIGSETNAND(sigpending, td->td_sigmask);
2111
2112 if (p->p_flag & P_PPWAIT)
2113 SIG_STOPSIGMASK(sigpending);
2114 if (SIGISEMPTY(sigpending)) /* no signal to send */
2115 return (0);
2116 sig = sig_ffs(&sigpending);
2117
2118 if (p->p_stops & S_SIG) {
2119 mtx_unlock(&ps->ps_mtx);
2120 stopevent(p, S_SIG, sig);
2121 mtx_lock(&ps->ps_mtx);
2122 }
2123
2124 /*
2125 * We should see pending but ignored signals
2126 * only if P_TRACED was on when they were posted.
2127 */
2128 if (SIGISMEMBER(ps->ps_sigignore, sig) && (traced == 0)) {
2129 SIGDELSET(td->td_siglist, sig);
2130 if (td->td_pflags & TDP_SA)
2131 SIGADDSET(td->td_sigmask, sig);
2132 continue;
2133 }
2134 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
2135 /*
2136 * If traced, always stop.
2137 */
2138 mtx_unlock(&ps->ps_mtx);
2139 newsig = ptracestop(td, sig);
2140 mtx_lock(&ps->ps_mtx);
2141
2142 /*
2143 * If parent wants us to take the signal,
2144 * then it will leave it in p->p_xstat;
2145 * otherwise we just look for signals again.
2146 */
2147 SIGDELSET(td->td_siglist, sig); /* clear old signal */
2148 if (td->td_pflags & TDP_SA)
2149 SIGADDSET(td->td_sigmask, sig);
2150 if (newsig == 0)
2151 continue;
2152 sig = newsig;
2153 /*
2154 * If the traced bit got turned off, go back up
2155 * to the top to rescan signals. This ensures
2156 * that p_sig* and p_sigact are consistent.
2157 */
2158 if ((p->p_flag & P_TRACED) == 0)
2159 continue;
2160
2161 /*
2162 * Put the new signal into td_siglist. If the
2163 * signal is being masked, look for other signals.
2164 */
2165 SIGADDSET(td->td_siglist, sig);
2166 if (td->td_pflags & TDP_SA)
2167 SIGDELSET(td->td_sigmask, sig);
2168 if (SIGISMEMBER(td->td_sigmask, sig))
2169 continue;
2170 signotify(td);
2171 }
2172
2173 prop = sigprop(sig);
2174
2175 /*
2176 * Decide whether the signal should be returned.
2177 * Return the signal's number, or fall through
2178 * to clear it from the pending mask.
2179 */
2180 switch ((intptr_t)p->p_sigacts->ps_sigact[_SIG_IDX(sig)]) {
2181
2182 case (intptr_t)SIG_DFL:
2183 /*
2184 * Don't take default actions on system processes.
2185 */
2186 if (p->p_pid <= 1) {
2187 #ifdef DIAGNOSTIC
2188 /*
2189 * Are you sure you want to ignore SIGSEGV
2190 * in init? XXX
2191 */
2192 printf("Process (pid %lu) got signal %d\n",
2193 (u_long)p->p_pid, sig);
2194 #endif
2195 break; /* == ignore */
2196 }
2197 /*
2198 * If there is a pending stop signal to process
2199 * with default action, stop here,
2200 * then clear the signal. However,
2201 * if process is member of an orphaned
2202 * process group, ignore tty stop signals.
2203 */
2204 if (prop & SA_STOP) {
2205 if (p->p_flag & P_TRACED ||
2206 (p->p_pgrp->pg_jobc == 0 &&
2207 prop & SA_TTYSTOP))
2208 break; /* == ignore */
2209 mtx_unlock(&ps->ps_mtx);
2210 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
2211 &p->p_mtx.mtx_object, "Catching SIGSTOP");
2212 p->p_flag |= P_STOPPED_SIG;
2213 p->p_xstat = sig;
2214 mtx_lock_spin(&sched_lock);
2215 sig_suspend_threads(td, p, 0);
2216 thread_stopped(p);
2217 thread_suspend_one(td);
2218 PROC_UNLOCK(p);
2219 DROP_GIANT();
2220 mi_switch(SW_INVOL, NULL);
2221 mtx_unlock_spin(&sched_lock);
2222 PICKUP_GIANT();
2223 PROC_LOCK(p);
2224 mtx_lock(&ps->ps_mtx);
2225 break;
2226 } else if (prop & SA_IGNORE) {
2227 /*
2228 * Except for SIGCONT, shouldn't get here.
2229 * Default action is to ignore; drop it.
2230 */
2231 break; /* == ignore */
2232 } else
2233 return (sig);
2234 /*NOTREACHED*/
2235
2236 case (intptr_t)SIG_IGN:
2237 /*
2238 * Masking above should prevent us ever trying
2239 * to take action on an ignored signal other
2240 * than SIGCONT, unless process is traced.
2241 */
2242 if ((prop & SA_CONT) == 0 &&
2243 (p->p_flag & P_TRACED) == 0)
2244 printf("issignal\n");
2245 break; /* == ignore */
2246
2247 default:
2248 /*
2249 * This signal has an action, let
2250 * postsig() process it.
2251 */
2252 return (sig);
2253 }
2254 SIGDELSET(td->td_siglist, sig); /* take the signal! */
2255 }
2256 /* NOTREACHED */
2257 }
2258
2259 /*
2260 * MPSAFE
2261 */
2262 void
2263 thread_stopped(struct proc *p)
2264 {
2265 struct proc *p1 = curthread->td_proc;
2266 struct sigacts *ps;
2267 int n;
2268
2269 PROC_LOCK_ASSERT(p, MA_OWNED);
2270 mtx_assert(&sched_lock, MA_OWNED);
2271 n = p->p_suspcount;
2272 if (p == p1)
2273 n++;
2274 if ((p->p_flag & P_STOPPED_SIG) && (n == p->p_numthreads)) {
2275 mtx_unlock_spin(&sched_lock);
2276 p->p_flag &= ~P_WAITED;
2277 PROC_LOCK(p->p_pptr);
2278 /*
2279 * Wake up parent sleeping in kern_wait(), also send
2280 * SIGCHLD to parent, but SIGCHLD does not guarantee
2281 * that parent will awake, because parent may masked
2282 * the signal.
2283 */
2284 p->p_pptr->p_flag |= P_STATCHILD;
2285 wakeup(p->p_pptr);
2286 ps = p->p_pptr->p_sigacts;
2287 mtx_lock(&ps->ps_mtx);
2288 if ((ps->ps_flag & PS_NOCLDSTOP) == 0) {
2289 mtx_unlock(&ps->ps_mtx);
2290 psignal(p->p_pptr, SIGCHLD);
2291 } else
2292 mtx_unlock(&ps->ps_mtx);
2293 PROC_UNLOCK(p->p_pptr);
2294 mtx_lock_spin(&sched_lock);
2295 }
2296 }
2297
2298 /*
2299 * Take the action for the specified signal
2300 * from the current set of pending signals.
2301 */
2302 void
2303 postsig(sig)
2304 register int sig;
2305 {
2306 struct thread *td = curthread;
2307 register struct proc *p = td->td_proc;
2308 struct sigacts *ps;
2309 sig_t action;
2310 sigset_t returnmask;
2311 int code;
2312
2313 KASSERT(sig != 0, ("postsig"));
2314
2315 PROC_LOCK_ASSERT(p, MA_OWNED);
2316 ps = p->p_sigacts;
2317 mtx_assert(&ps->ps_mtx, MA_OWNED);
2318 SIGDELSET(td->td_siglist, sig);
2319 action = ps->ps_sigact[_SIG_IDX(sig)];
2320 #ifdef KTRACE
2321 if (KTRPOINT(td, KTR_PSIG))
2322 ktrpsig(sig, action, td->td_pflags & TDP_OLDMASK ?
2323 &td->td_oldsigmask : &td->td_sigmask, 0);
2324 #endif
2325 if (p->p_stops & S_SIG) {
2326 mtx_unlock(&ps->ps_mtx);
2327 stopevent(p, S_SIG, sig);
2328 mtx_lock(&ps->ps_mtx);
2329 }
2330
2331 if (!(td->td_pflags & TDP_SA) && action == SIG_DFL) {
2332 /*
2333 * Default action, where the default is to kill
2334 * the process. (Other cases were ignored above.)
2335 */
2336 mtx_unlock(&ps->ps_mtx);
2337 sigexit(td, sig);
2338 /* NOTREACHED */
2339 } else {
2340 if (td->td_pflags & TDP_SA) {
2341 if (sig == SIGKILL) {
2342 mtx_unlock(&ps->ps_mtx);
2343 sigexit(td, sig);
2344 }
2345 }
2346
2347 /*
2348 * If we get here, the signal must be caught.
2349 */
2350 KASSERT(action != SIG_IGN && !SIGISMEMBER(td->td_sigmask, sig),
2351 ("postsig action"));
2352 /*
2353 * Set the new mask value and also defer further
2354 * occurrences of this signal.
2355 *
2356 * Special case: user has done a sigsuspend. Here the
2357 * current mask is not of interest, but rather the
2358 * mask from before the sigsuspend is what we want
2359 * restored after the signal processing is completed.
2360 */
2361 if (td->td_pflags & TDP_OLDMASK) {
2362 returnmask = td->td_oldsigmask;
2363 td->td_pflags &= ~TDP_OLDMASK;
2364 } else
2365 returnmask = td->td_sigmask;
2366
2367 SIGSETOR(td->td_sigmask, ps->ps_catchmask[_SIG_IDX(sig)]);
2368 if (!SIGISMEMBER(ps->ps_signodefer, sig))
2369 SIGADDSET(td->td_sigmask, sig);
2370
2371 if (SIGISMEMBER(ps->ps_sigreset, sig)) {
2372 /*
2373 * See kern_sigaction() for origin of this code.
2374 */
2375 SIGDELSET(ps->ps_sigcatch, sig);
2376 if (sig != SIGCONT &&
2377 sigprop(sig) & SA_IGNORE)
2378 SIGADDSET(ps->ps_sigignore, sig);
2379 ps->ps_sigact[_SIG_IDX(sig)] = SIG_DFL;
2380 }
2381 p->p_stats->p_ru.ru_nsignals++;
2382 if (p->p_sig != sig) {
2383 code = 0;
2384 } else {
2385 code = p->p_code;
2386 p->p_code = 0;
2387 p->p_sig = 0;
2388 }
2389 if (td->td_pflags & TDP_SA)
2390 thread_signal_add(curthread, sig);
2391 else
2392 (*p->p_sysent->sv_sendsig)(action, sig,
2393 &returnmask, code);
2394 }
2395 }
2396
2397 /*
2398 * Kill the current process for stated reason.
2399 */
2400 void
2401 killproc(p, why)
2402 struct proc *p;
2403 char *why;
2404 {
2405
2406 PROC_LOCK_ASSERT(p, MA_OWNED);
2407 CTR3(KTR_PROC, "killproc: proc %p (pid %d, %s)",
2408 p, p->p_pid, p->p_comm);
2409 log(LOG_ERR, "pid %d (%s), uid %d, was killed: %s\n", p->p_pid, p->p_comm,
2410 p->p_ucred ? p->p_ucred->cr_uid : -1, why);
2411 p->p_flag |= P_WKILLED;
2412 psignal(p, SIGKILL);
2413 }
2414
2415 /*
2416 * Force the current process to exit with the specified signal, dumping core
2417 * if appropriate. We bypass the normal tests for masked and caught signals,
2418 * allowing unrecoverable failures to terminate the process without changing
2419 * signal state. Mark the accounting record with the signal termination.
2420 * If dumping core, save the signal number for the debugger. Calls exit and
2421 * does not return.
2422 *
2423 * MPSAFE
2424 */
2425 void
2426 sigexit(td, sig)
2427 struct thread *td;
2428 int sig;
2429 {
2430 struct proc *p = td->td_proc;
2431
2432 PROC_LOCK_ASSERT(p, MA_OWNED);
2433 p->p_acflag |= AXSIG;
2434 /*
2435 * We must be single-threading to generate a core dump. This
2436 * ensures that the registers in the core file are up-to-date.
2437 * Also, the ELF dump handler assumes that the thread list doesn't
2438 * change out from under it.
2439 *
2440 * XXX If another thread attempts to single-thread before us
2441 * (e.g. via fork()), we won't get a dump at all.
2442 */
2443 if ((sigprop(sig) & SA_CORE) && (thread_single(SINGLE_NO_EXIT) == 0)) {
2444 p->p_sig = sig;
2445 /*
2446 * Log signals which would cause core dumps
2447 * (Log as LOG_INFO to appease those who don't want
2448 * these messages.)
2449 * XXX : Todo, as well as euid, write out ruid too
2450 * Note that coredump() drops proc lock.
2451 */
2452 if (coredump(td) == 0)
2453 sig |= WCOREFLAG;
2454 if (kern_logsigexit)
2455 log(LOG_INFO,
2456 "pid %d (%s), uid %d: exited on signal %d%s\n",
2457 p->p_pid, p->p_comm,
2458 td->td_ucred ? td->td_ucred->cr_uid : -1,
2459 sig &~ WCOREFLAG,
2460 sig & WCOREFLAG ? " (core dumped)" : "");
2461 } else
2462 PROC_UNLOCK(p);
2463 exit1(td, W_EXITCODE(0, sig));
2464 /* NOTREACHED */
2465 }
2466
2467 static char corefilename[MAXPATHLEN] = {"%N.core"};
2468 SYSCTL_STRING(_kern, OID_AUTO, corefile, CTLFLAG_RW, corefilename,
2469 sizeof(corefilename), "process corefile name format string");
2470
2471 /*
2472 * expand_name(name, uid, pid)
2473 * Expand the name described in corefilename, using name, uid, and pid.
2474 * corefilename is a printf-like string, with three format specifiers:
2475 * %N name of process ("name")
2476 * %P process id (pid)
2477 * %U user id (uid)
2478 * For example, "%N.core" is the default; they can be disabled completely
2479 * by using "/dev/null", or all core files can be stored in "/cores/%U/%N-%P".
2480 * This is controlled by the sysctl variable kern.corefile (see above).
2481 */
2482
2483 static char *
2484 expand_name(name, uid, pid)
2485 const char *name;
2486 uid_t uid;
2487 pid_t pid;
2488 {
2489 const char *format, *appendstr;
2490 char *temp;
2491 char buf[11]; /* Buffer for pid/uid -- max 4B */
2492 size_t i, l, n;
2493
2494 format = corefilename;
2495 temp = malloc(MAXPATHLEN, M_TEMP, M_NOWAIT | M_ZERO);
2496 if (temp == NULL)
2497 return (NULL);
2498 for (i = 0, n = 0; n < MAXPATHLEN && format[i]; i++) {
2499 switch (format[i]) {
2500 case '%': /* Format character */
2501 i++;
2502 switch (format[i]) {
2503 case '%':
2504 appendstr = "%";
2505 break;
2506 case 'N': /* process name */
2507 appendstr = name;
2508 break;
2509 case 'P': /* process id */
2510 sprintf(buf, "%u", pid);
2511 appendstr = buf;
2512 break;
2513 case 'U': /* user id */
2514 sprintf(buf, "%u", uid);
2515 appendstr = buf;
2516 break;
2517 default:
2518 appendstr = "";
2519 log(LOG_ERR,
2520 "Unknown format character %c in `%s'\n",
2521 format[i], format);
2522 }
2523 l = strlen(appendstr);
2524 if ((n + l) >= MAXPATHLEN)
2525 goto toolong;
2526 memcpy(temp + n, appendstr, l);
2527 n += l;
2528 break;
2529 default:
2530 temp[n++] = format[i];
2531 }
2532 }
2533 if (format[i] != '\0')
2534 goto toolong;
2535 return (temp);
2536 toolong:
2537 log(LOG_ERR, "pid %ld (%s), uid (%lu): corename is too long\n",
2538 (long)pid, name, (u_long)uid);
2539 free(temp, M_TEMP);
2540 return (NULL);
2541 }
2542
2543 /*
2544 * Dump a process' core. The main routine does some
2545 * policy checking, and creates the name of the coredump;
2546 * then it passes on a vnode and a size limit to the process-specific
2547 * coredump routine if there is one; if there _is not_ one, it returns
2548 * ENOSYS; otherwise it returns the error from the process-specific routine.
2549 */
2550
2551 static int
2552 coredump(struct thread *td)
2553 {
2554 struct proc *p = td->td_proc;
2555 register struct vnode *vp;
2556 register struct ucred *cred = td->td_ucred;
2557 struct flock lf;
2558 struct nameidata nd;
2559 struct vattr vattr;
2560 int error, error1, flags, locked;
2561 struct mount *mp;
2562 char *name; /* name of corefile */
2563 off_t limit;
2564
2565 PROC_LOCK_ASSERT(p, MA_OWNED);
2566 MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td);
2567 _STOPEVENT(p, S_CORE, 0);
2568
2569 if (((sugid_coredump == 0) && p->p_flag & P_SUGID) || do_coredump == 0) {
2570 PROC_UNLOCK(p);
2571 return (EFAULT);
2572 }
2573
2574 /*
2575 * Note that the bulk of limit checking is done after
2576 * the corefile is created. The exception is if the limit
2577 * for corefiles is 0, in which case we don't bother
2578 * creating the corefile at all. This layout means that
2579 * a corefile is truncated instead of not being created,
2580 * if it is larger than the limit.
2581 */
2582 limit = (off_t)lim_cur(p, RLIMIT_CORE);
2583 PROC_UNLOCK(p);
2584 if (limit == 0)
2585 return (EFBIG);
2586
2587 mtx_lock(&Giant);
2588 restart:
2589 name = expand_name(p->p_comm, td->td_ucred->cr_uid, p->p_pid);
2590 if (name == NULL) {
2591 mtx_unlock(&Giant);
2592 return (EINVAL);
2593 }
2594 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, td); /* XXXKSE */
2595 flags = O_CREAT | FWRITE | O_NOFOLLOW;
2596 error = vn_open(&nd, &flags, S_IRUSR | S_IWUSR, -1);
2597 free(name, M_TEMP);
2598 if (error) {
2599 mtx_unlock(&Giant);
2600 return (error);
2601 }
2602 NDFREE(&nd, NDF_ONLY_PNBUF);
2603 vp = nd.ni_vp;
2604
2605 /* Don't dump to non-regular files or files with links. */
2606 if (vp->v_type != VREG ||
2607 VOP_GETATTR(vp, &vattr, cred, td) || vattr.va_nlink != 1) {
2608 VOP_UNLOCK(vp, 0, td);
2609 error = EFAULT;
2610 goto out;
2611 }
2612
2613 VOP_UNLOCK(vp, 0, td);
2614 lf.l_whence = SEEK_SET;
2615 lf.l_start = 0;
2616 lf.l_len = 0;
2617 lf.l_type = F_WRLCK;
2618 locked = (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &lf, F_FLOCK) == 0);
2619
2620 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2621 lf.l_type = F_UNLCK;
2622 if (locked)
2623 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2624 if ((error = vn_close(vp, FWRITE, cred, td)) != 0)
2625 return (error);
2626 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | PCATCH)) != 0)
2627 return (error);
2628 goto restart;
2629 }
2630
2631 VATTR_NULL(&vattr);
2632 vattr.va_size = 0;
2633 if (set_core_nodump_flag)
2634 vattr.va_flags = UF_NODUMP;
2635 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2636 VOP_LEASE(vp, td, cred, LEASE_WRITE);
2637 VOP_SETATTR(vp, &vattr, cred, td);
2638 VOP_UNLOCK(vp, 0, td);
2639 vn_finished_write(mp);
2640 PROC_LOCK(p);
2641 p->p_acflag |= ACORE;
2642 PROC_UNLOCK(p);
2643
2644 error = p->p_sysent->sv_coredump ?
2645 p->p_sysent->sv_coredump(td, vp, limit) :
2646 ENOSYS;
2647
2648 if (locked) {
2649 lf.l_type = F_UNLCK;
2650 VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_FLOCK);
2651 }
2652 out:
2653 error1 = vn_close(vp, FWRITE, cred, td);
2654 mtx_unlock(&Giant);
2655 if (error == 0)
2656 error = error1;
2657 return (error);
2658 }
2659
2660 /*
2661 * Nonexistent system call-- signal process (may want to handle it).
2662 * Flag error in case process won't see signal immediately (blocked or ignored).
2663 */
2664 #ifndef _SYS_SYSPROTO_H_
2665 struct nosys_args {
2666 int dummy;
2667 };
2668 #endif
2669 /*
2670 * MPSAFE
2671 */
2672 /* ARGSUSED */
2673 int
2674 nosys(td, args)
2675 struct thread *td;
2676 struct nosys_args *args;
2677 {
2678 struct proc *p = td->td_proc;
2679
2680 PROC_LOCK(p);
2681 psignal(p, SIGSYS);
2682 PROC_UNLOCK(p);
2683 return (ENOSYS);
2684 }
2685
2686 /*
2687 * Send a SIGIO or SIGURG signal to a process or process group using
2688 * stored credentials rather than those of the current process.
2689 */
2690 void
2691 pgsigio(sigiop, sig, checkctty)
2692 struct sigio **sigiop;
2693 int sig, checkctty;
2694 {
2695 struct sigio *sigio;
2696
2697 SIGIO_LOCK();
2698 sigio = *sigiop;
2699 if (sigio == NULL) {
2700 SIGIO_UNLOCK();
2701 return;
2702 }
2703 if (sigio->sio_pgid > 0) {
2704 PROC_LOCK(sigio->sio_proc);
2705 if (CANSIGIO(sigio->sio_ucred, sigio->sio_proc->p_ucred))
2706 psignal(sigio->sio_proc, sig);
2707 PROC_UNLOCK(sigio->sio_proc);
2708 } else if (sigio->sio_pgid < 0) {
2709 struct proc *p;
2710
2711 PGRP_LOCK(sigio->sio_pgrp);
2712 LIST_FOREACH(p, &sigio->sio_pgrp->pg_members, p_pglist) {
2713 PROC_LOCK(p);
2714 if (CANSIGIO(sigio->sio_ucred, p->p_ucred) &&
2715 (checkctty == 0 || (p->p_flag & P_CONTROLT)))
2716 psignal(p, sig);
2717 PROC_UNLOCK(p);
2718 }
2719 PGRP_UNLOCK(sigio->sio_pgrp);
2720 }
2721 SIGIO_UNLOCK();
2722 }
2723
2724 static int
2725 filt_sigattach(struct knote *kn)
2726 {
2727 struct proc *p = curproc;
2728
2729 kn->kn_ptr.p_proc = p;
2730 kn->kn_flags |= EV_CLEAR; /* automatically set */
2731
2732 knlist_add(&p->p_klist, kn, 0);
2733
2734 return (0);
2735 }
2736
2737 static void
2738 filt_sigdetach(struct knote *kn)
2739 {
2740 struct proc *p = kn->kn_ptr.p_proc;
2741
2742 knlist_remove(&p->p_klist, kn, 0);
2743 }
2744
2745 /*
2746 * signal knotes are shared with proc knotes, so we apply a mask to
2747 * the hint in order to differentiate them from process hints. This
2748 * could be avoided by using a signal-specific knote list, but probably
2749 * isn't worth the trouble.
2750 */
2751 static int
2752 filt_signal(struct knote *kn, long hint)
2753 {
2754
2755 if (hint & NOTE_SIGNAL) {
2756 hint &= ~NOTE_SIGNAL;
2757
2758 if (kn->kn_id == hint)
2759 kn->kn_data++;
2760 }
2761 return (kn->kn_data != 0);
2762 }
2763
2764 struct sigacts *
2765 sigacts_alloc(void)
2766 {
2767 struct sigacts *ps;
2768
2769 ps = malloc(sizeof(struct sigacts), M_SUBPROC, M_WAITOK | M_ZERO);
2770 ps->ps_refcnt = 1;
2771 mtx_init(&ps->ps_mtx, "sigacts", NULL, MTX_DEF);
2772 return (ps);
2773 }
2774
2775 void
2776 sigacts_free(struct sigacts *ps)
2777 {
2778
2779 mtx_lock(&ps->ps_mtx);
2780 ps->ps_refcnt--;
2781 if (ps->ps_refcnt == 0) {
2782 mtx_destroy(&ps->ps_mtx);
2783 free(ps, M_SUBPROC);
2784 } else
2785 mtx_unlock(&ps->ps_mtx);
2786 }
2787
2788 struct sigacts *
2789 sigacts_hold(struct sigacts *ps)
2790 {
2791 mtx_lock(&ps->ps_mtx);
2792 ps->ps_refcnt++;
2793 mtx_unlock(&ps->ps_mtx);
2794 return (ps);
2795 }
2796
2797 void
2798 sigacts_copy(struct sigacts *dest, struct sigacts *src)
2799 {
2800
2801 KASSERT(dest->ps_refcnt == 1, ("sigacts_copy to shared dest"));
2802 mtx_lock(&src->ps_mtx);
2803 bcopy(src, dest, offsetof(struct sigacts, ps_refcnt));
2804 mtx_unlock(&src->ps_mtx);
2805 }
2806
2807 int
2808 sigacts_shared(struct sigacts *ps)
2809 {
2810 int shared;
2811
2812 mtx_lock(&ps->ps_mtx);
2813 shared = ps->ps_refcnt > 1;
2814 mtx_unlock(&ps->ps_mtx);
2815 return (shared);
2816 }
Cache object: 8cee8b7aaeaebce1084d9223c41b0c4d
|