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