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