FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_sig.c
1 /* $NetBSD: kern_sig.c,v 1.202.2.3 2007/01/19 21:46:54 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.202.2.3 2007/01/19 21:46:54 bouyer Exp $");
41
42 #include "opt_ktrace.h"
43 #include "opt_compat_sunos.h"
44 #include "opt_compat_netbsd.h"
45 #include "opt_compat_netbsd32.h"
46
47 #define SIGPROP /* include signal properties table */
48 #include <sys/param.h>
49 #include <sys/signalvar.h>
50 #include <sys/resourcevar.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/proc.h>
54 #include <sys/systm.h>
55 #include <sys/timeb.h>
56 #include <sys/times.h>
57 #include <sys/buf.h>
58 #include <sys/acct.h>
59 #include <sys/file.h>
60 #include <sys/kernel.h>
61 #include <sys/wait.h>
62 #include <sys/ktrace.h>
63 #include <sys/syslog.h>
64 #include <sys/stat.h>
65 #include <sys/core.h>
66 #include <sys/filedesc.h>
67 #include <sys/malloc.h>
68 #include <sys/pool.h>
69 #include <sys/ucontext.h>
70 #include <sys/sa.h>
71 #include <sys/savar.h>
72 #include <sys/exec.h>
73
74 #include <sys/mount.h>
75 #include <sys/syscallargs.h>
76
77 #include <machine/cpu.h>
78
79 #include <sys/user.h> /* for coredump */
80
81 #include <uvm/uvm.h>
82 #include <uvm/uvm_extern.h>
83
84 static void child_psignal(struct proc *, int);
85 static int build_corename(struct proc *, char *, const char *, size_t);
86 static void ksiginfo_exithook(struct proc *, void *);
87 static void ksiginfo_put(struct proc *, const ksiginfo_t *);
88 static ksiginfo_t *ksiginfo_get(struct proc *, int);
89 static void kpsignal2(struct proc *, const ksiginfo_t *, int);
90
91 sigset_t contsigmask, stopsigmask, sigcantmask;
92
93 struct pool sigacts_pool; /* memory pool for sigacts structures */
94
95 /*
96 * struct sigacts memory pool allocator.
97 */
98
99 static void *
100 sigacts_poolpage_alloc(struct pool *pp, int flags)
101 {
102
103 return (void *)uvm_km_kmemalloc1(kernel_map,
104 uvm.kernel_object, (PAGE_SIZE)*2, (PAGE_SIZE)*2, UVM_UNKNOWN_OFFSET,
105 (flags & PR_WAITOK) ? 0 : UVM_KMF_NOWAIT | UVM_KMF_TRYLOCK);
106 }
107
108 static void
109 sigacts_poolpage_free(struct pool *pp, void *v)
110 {
111 uvm_km_free(kernel_map, (vaddr_t)v, (PAGE_SIZE)*2);
112 }
113
114 static struct pool_allocator sigactspool_allocator = {
115 sigacts_poolpage_alloc, sigacts_poolpage_free,
116 };
117
118 POOL_INIT(siginfo_pool, sizeof(siginfo_t), 0, 0, 0, "siginfo",
119 &pool_allocator_nointr);
120 POOL_INIT(ksiginfo_pool, sizeof(ksiginfo_t), 0, 0, 0, "ksiginfo", NULL);
121
122 /*
123 * Can process p, with pcred pc, send the signal signum to process q?
124 */
125 #define CANSIGNAL(p, pc, q, signum) \
126 ((pc)->pc_ucred->cr_uid == 0 || \
127 (pc)->p_ruid == (q)->p_cred->p_ruid || \
128 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
129 (pc)->p_ruid == (q)->p_ucred->cr_uid || \
130 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
131 ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
132
133 /*
134 * Remove and return the first ksiginfo element that matches our requested
135 * signal, or return NULL if one not found.
136 */
137 static ksiginfo_t *
138 ksiginfo_get(struct proc *p, int signo)
139 {
140 ksiginfo_t *ksi;
141 int s;
142
143 s = splsoftclock();
144 simple_lock(&p->p_sigctx.ps_silock);
145 CIRCLEQ_FOREACH(ksi, &p->p_sigctx.ps_siginfo, ksi_list) {
146 if (ksi->ksi_signo == signo) {
147 CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list);
148 goto out;
149 }
150 }
151 ksi = NULL;
152 out:
153 simple_unlock(&p->p_sigctx.ps_silock);
154 splx(s);
155 return ksi;
156 }
157
158 /*
159 * Append a new ksiginfo element to the list of pending ksiginfo's, if
160 * we need to (SA_SIGINFO was requested). We replace non RT signals if
161 * they already existed in the queue and we add new entries for RT signals,
162 * or for non RT signals with non-existing entries.
163 */
164 static void
165 ksiginfo_put(struct proc *p, const ksiginfo_t *ksi)
166 {
167 ksiginfo_t *kp;
168 struct sigaction *sa = &SIGACTION_PS(p->p_sigacts, ksi->ksi_signo);
169 int s;
170
171 if ((sa->sa_flags & SA_SIGINFO) == 0)
172 return;
173 /*
174 * If there's no info, don't save it.
175 */
176 if (KSI_EMPTY_P(ksi))
177 return;
178
179 s = splsoftclock();
180 simple_lock(&p->p_sigctx.ps_silock);
181 #ifdef notyet /* XXX: QUEUING */
182 if (ksi->ksi_signo < SIGRTMIN)
183 #endif
184 {
185 CIRCLEQ_FOREACH(kp, &p->p_sigctx.ps_siginfo, ksi_list) {
186 if (kp->ksi_signo == ksi->ksi_signo) {
187 KSI_COPY(ksi, kp);
188 goto out;
189 }
190 }
191 }
192 kp = pool_get(&ksiginfo_pool, PR_NOWAIT);
193 if (kp == NULL) {
194 #ifdef DIAGNOSTIC
195 printf("Out of memory allocating siginfo for pid %d\n",
196 p->p_pid);
197 #endif
198 goto out;
199 }
200 *kp = *ksi;
201 CIRCLEQ_INSERT_TAIL(&p->p_sigctx.ps_siginfo, kp, ksi_list);
202 out:
203 simple_unlock(&p->p_sigctx.ps_silock);
204 splx(s);
205 }
206
207 /*
208 * free all pending ksiginfo on exit
209 */
210 static void
211 ksiginfo_exithook(struct proc *p, void *v)
212 {
213 int s;
214
215 s = splsoftclock();
216 simple_lock(&p->p_sigctx.ps_silock);
217 while (!CIRCLEQ_EMPTY(&p->p_sigctx.ps_siginfo)) {
218 ksiginfo_t *ksi = CIRCLEQ_FIRST(&p->p_sigctx.ps_siginfo);
219 CIRCLEQ_REMOVE(&p->p_sigctx.ps_siginfo, ksi, ksi_list);
220 pool_put(&ksiginfo_pool, ksi);
221 }
222 simple_unlock(&p->p_sigctx.ps_silock);
223 splx(s);
224 }
225
226 /*
227 * Initialize signal-related data structures.
228 */
229 void
230 signal_init(void)
231 {
232
233 sigactspool_allocator.pa_pagesz = (PAGE_SIZE)*2;
234
235 pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
236 sizeof(struct sigacts) > PAGE_SIZE ?
237 &sigactspool_allocator : &pool_allocator_nointr);
238
239 exithook_establish(ksiginfo_exithook, NULL);
240 exechook_establish(ksiginfo_exithook, NULL);
241 }
242
243 /*
244 * Create an initial sigctx structure, using the same signal state
245 * as p. If 'share' is set, share the sigctx_proc part, otherwise just
246 * copy it from parent.
247 */
248 void
249 sigactsinit(struct proc *np, struct proc *pp, int share)
250 {
251 struct sigacts *ps;
252
253 if (share) {
254 np->p_sigacts = pp->p_sigacts;
255 pp->p_sigacts->sa_refcnt++;
256 } else {
257 ps = pool_get(&sigacts_pool, PR_WAITOK);
258 if (pp)
259 memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
260 else
261 memset(ps, '\0', sizeof(struct sigacts));
262 ps->sa_refcnt = 1;
263 np->p_sigacts = ps;
264 }
265 }
266
267 /*
268 * Make this process not share its sigctx, maintaining all
269 * signal state.
270 */
271 void
272 sigactsunshare(struct proc *p)
273 {
274 struct sigacts *oldps;
275
276 if (p->p_sigacts->sa_refcnt == 1)
277 return;
278
279 oldps = p->p_sigacts;
280 sigactsinit(p, NULL, 0);
281
282 if (--oldps->sa_refcnt == 0)
283 pool_put(&sigacts_pool, oldps);
284 }
285
286 /*
287 * Release a sigctx structure.
288 */
289 void
290 sigactsfree(struct sigacts *ps)
291 {
292
293 if (--ps->sa_refcnt > 0)
294 return;
295
296 pool_put(&sigacts_pool, ps);
297 }
298
299 int
300 sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
301 struct sigaction *osa, const void *tramp, int vers)
302 {
303 struct sigacts *ps;
304 int prop;
305
306 ps = p->p_sigacts;
307 if (signum <= 0 || signum >= NSIG)
308 return (EINVAL);
309
310 /*
311 * Trampoline ABI version 0 is reserved for the legacy
312 * kernel-provided on-stack trampoline. Conversely, if we are
313 * using a non-0 ABI version, we must have a trampoline. Only
314 * validate the vers if a new sigaction was supplied. Emulations
315 * use legacy kernel trampolines with version 0, alternatively
316 * check for that too.
317 */
318 if ((vers != 0 && tramp == NULL) ||
319 #ifdef SIGTRAMP_VALID
320 (nsa != NULL &&
321 ((vers == 0) ?
322 (p->p_emul->e_sigcode == NULL) :
323 !SIGTRAMP_VALID(vers))) ||
324 #endif
325 (vers == 0 && tramp != NULL))
326 return (EINVAL);
327
328 if (osa)
329 *osa = SIGACTION_PS(ps, signum);
330
331 if (nsa) {
332 if (nsa->sa_flags & ~SA_ALLBITS)
333 return (EINVAL);
334
335 prop = sigprop[signum];
336 if (prop & SA_CANTMASK)
337 return (EINVAL);
338
339 (void) splsched(); /* XXXSMP */
340 SIGACTION_PS(ps, signum) = *nsa;
341 ps->sa_sigdesc[signum].sd_tramp = tramp;
342 ps->sa_sigdesc[signum].sd_vers = vers;
343 sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
344 if ((prop & SA_NORESET) != 0)
345 SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
346 if (signum == SIGCHLD) {
347 if (nsa->sa_flags & SA_NOCLDSTOP)
348 p->p_flag |= P_NOCLDSTOP;
349 else
350 p->p_flag &= ~P_NOCLDSTOP;
351 if (nsa->sa_flags & SA_NOCLDWAIT) {
352 /*
353 * Paranoia: since SA_NOCLDWAIT is implemented
354 * by reparenting the dying child to PID 1 (and
355 * trust it to reap the zombie), PID 1 itself
356 * is forbidden to set SA_NOCLDWAIT.
357 */
358 if (p->p_pid == 1)
359 p->p_flag &= ~P_NOCLDWAIT;
360 else
361 p->p_flag |= P_NOCLDWAIT;
362 } else
363 p->p_flag &= ~P_NOCLDWAIT;
364 }
365 if ((nsa->sa_flags & SA_NODEFER) == 0)
366 sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
367 else
368 sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
369 /*
370 * Set bit in p_sigctx.ps_sigignore for signals that are set to
371 * SIG_IGN, and for signals set to SIG_DFL where the default is
372 * to ignore. However, don't put SIGCONT in
373 * p_sigctx.ps_sigignore, as we have to restart the process.
374 */
375 if (nsa->sa_handler == SIG_IGN ||
376 (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
377 /* never to be seen again */
378 sigdelset(&p->p_sigctx.ps_siglist, signum);
379 if (signum != SIGCONT) {
380 /* easier in psignal */
381 sigaddset(&p->p_sigctx.ps_sigignore, signum);
382 }
383 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
384 } else {
385 sigdelset(&p->p_sigctx.ps_sigignore, signum);
386 if (nsa->sa_handler == SIG_DFL)
387 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
388 else
389 sigaddset(&p->p_sigctx.ps_sigcatch, signum);
390 }
391 (void) spl0();
392 }
393
394 return (0);
395 }
396
397 #ifdef COMPAT_16
398 /* ARGSUSED */
399 int
400 compat_16_sys___sigaction14(struct lwp *l, void *v, register_t *retval)
401 {
402 struct compat_16_sys___sigaction14_args /* {
403 syscallarg(int) signum;
404 syscallarg(const struct sigaction *) nsa;
405 syscallarg(struct sigaction *) osa;
406 } */ *uap = v;
407 struct proc *p;
408 struct sigaction nsa, osa;
409 int error;
410
411 if (SCARG(uap, nsa)) {
412 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
413 if (error)
414 return (error);
415 }
416 p = l->l_proc;
417 error = sigaction1(p, SCARG(uap, signum),
418 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
419 NULL, 0);
420 if (error)
421 return (error);
422 if (SCARG(uap, osa)) {
423 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
424 if (error)
425 return (error);
426 }
427 return (0);
428 }
429 #endif
430
431 /* ARGSUSED */
432 int
433 sys___sigaction_sigtramp(struct lwp *l, void *v, register_t *retval)
434 {
435 struct sys___sigaction_sigtramp_args /* {
436 syscallarg(int) signum;
437 syscallarg(const struct sigaction *) nsa;
438 syscallarg(struct sigaction *) osa;
439 syscallarg(void *) tramp;
440 syscallarg(int) vers;
441 } */ *uap = v;
442 struct proc *p = l->l_proc;
443 struct sigaction nsa, osa;
444 int error;
445
446 if (SCARG(uap, nsa)) {
447 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
448 if (error)
449 return (error);
450 }
451 error = sigaction1(p, SCARG(uap, signum),
452 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
453 SCARG(uap, tramp), SCARG(uap, vers));
454 if (error)
455 return (error);
456 if (SCARG(uap, osa)) {
457 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
458 if (error)
459 return (error);
460 }
461 return (0);
462 }
463
464 /*
465 * Initialize signal state for process 0;
466 * set to ignore signals that are ignored by default and disable the signal
467 * stack.
468 */
469 void
470 siginit(struct proc *p)
471 {
472 struct sigacts *ps;
473 int signum, prop;
474
475 ps = p->p_sigacts;
476 sigemptyset(&contsigmask);
477 sigemptyset(&stopsigmask);
478 sigemptyset(&sigcantmask);
479 for (signum = 1; signum < NSIG; signum++) {
480 prop = sigprop[signum];
481 if (prop & SA_CONT)
482 sigaddset(&contsigmask, signum);
483 if (prop & SA_STOP)
484 sigaddset(&stopsigmask, signum);
485 if (prop & SA_CANTMASK)
486 sigaddset(&sigcantmask, signum);
487 if (prop & SA_IGNORE && signum != SIGCONT)
488 sigaddset(&p->p_sigctx.ps_sigignore, signum);
489 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
490 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
491 }
492 sigemptyset(&p->p_sigctx.ps_sigcatch);
493 p->p_sigctx.ps_sigwaited = NULL;
494 p->p_flag &= ~P_NOCLDSTOP;
495
496 /*
497 * Reset stack state to the user stack.
498 */
499 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
500 p->p_sigctx.ps_sigstk.ss_size = 0;
501 p->p_sigctx.ps_sigstk.ss_sp = 0;
502
503 /* One reference. */
504 ps->sa_refcnt = 1;
505 }
506
507 /*
508 * Reset signals for an exec of the specified process.
509 */
510 void
511 execsigs(struct proc *p)
512 {
513 struct sigacts *ps;
514 int signum, prop;
515
516 sigactsunshare(p);
517
518 ps = p->p_sigacts;
519
520 /*
521 * Reset caught signals. Held signals remain held
522 * through p_sigctx.ps_sigmask (unless they were caught,
523 * and are now ignored by default).
524 */
525 for (signum = 1; signum < NSIG; signum++) {
526 if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
527 prop = sigprop[signum];
528 if (prop & SA_IGNORE) {
529 if ((prop & SA_CONT) == 0)
530 sigaddset(&p->p_sigctx.ps_sigignore,
531 signum);
532 sigdelset(&p->p_sigctx.ps_siglist, signum);
533 }
534 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
535 }
536 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
537 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
538 }
539 sigemptyset(&p->p_sigctx.ps_sigcatch);
540 p->p_sigctx.ps_sigwaited = NULL;
541 p->p_flag &= ~P_NOCLDSTOP;
542
543 /*
544 * Reset stack state to the user stack.
545 */
546 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
547 p->p_sigctx.ps_sigstk.ss_size = 0;
548 p->p_sigctx.ps_sigstk.ss_sp = 0;
549 }
550
551 int
552 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
553 {
554
555 if (oss)
556 *oss = p->p_sigctx.ps_sigmask;
557
558 if (nss) {
559 (void)splsched(); /* XXXSMP */
560 switch (how) {
561 case SIG_BLOCK:
562 sigplusset(nss, &p->p_sigctx.ps_sigmask);
563 break;
564 case SIG_UNBLOCK:
565 sigminusset(nss, &p->p_sigctx.ps_sigmask);
566 CHECKSIGS(p);
567 break;
568 case SIG_SETMASK:
569 p->p_sigctx.ps_sigmask = *nss;
570 CHECKSIGS(p);
571 break;
572 default:
573 (void)spl0(); /* XXXSMP */
574 return (EINVAL);
575 }
576 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
577 (void)spl0(); /* XXXSMP */
578 }
579
580 return (0);
581 }
582
583 /*
584 * Manipulate signal mask.
585 * Note that we receive new mask, not pointer,
586 * and return old mask as return value;
587 * the library stub does the rest.
588 */
589 int
590 sys___sigprocmask14(struct lwp *l, void *v, register_t *retval)
591 {
592 struct sys___sigprocmask14_args /* {
593 syscallarg(int) how;
594 syscallarg(const sigset_t *) set;
595 syscallarg(sigset_t *) oset;
596 } */ *uap = v;
597 struct proc *p;
598 sigset_t nss, oss;
599 int error;
600
601 if (SCARG(uap, set)) {
602 error = copyin(SCARG(uap, set), &nss, sizeof(nss));
603 if (error)
604 return (error);
605 }
606 p = l->l_proc;
607 error = sigprocmask1(p, SCARG(uap, how),
608 SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
609 if (error)
610 return (error);
611 if (SCARG(uap, oset)) {
612 error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
613 if (error)
614 return (error);
615 }
616 return (0);
617 }
618
619 void
620 sigpending1(struct proc *p, sigset_t *ss)
621 {
622
623 *ss = p->p_sigctx.ps_siglist;
624 sigminusset(&p->p_sigctx.ps_sigmask, ss);
625 }
626
627 /* ARGSUSED */
628 int
629 sys___sigpending14(struct lwp *l, void *v, register_t *retval)
630 {
631 struct sys___sigpending14_args /* {
632 syscallarg(sigset_t *) set;
633 } */ *uap = v;
634 struct proc *p;
635 sigset_t ss;
636
637 p = l->l_proc;
638 sigpending1(p, &ss);
639 return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
640 }
641
642 int
643 sigsuspend1(struct proc *p, const sigset_t *ss)
644 {
645 struct sigacts *ps;
646
647 ps = p->p_sigacts;
648 if (ss) {
649 /*
650 * When returning from sigpause, we want
651 * the old mask to be restored after the
652 * signal handler has finished. Thus, we
653 * save it here and mark the sigctx structure
654 * to indicate this.
655 */
656 p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
657 p->p_sigctx.ps_flags |= SAS_OLDMASK;
658 (void) splsched(); /* XXXSMP */
659 p->p_sigctx.ps_sigmask = *ss;
660 CHECKSIGS(p);
661 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
662 (void) spl0(); /* XXXSMP */
663 }
664
665 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
666 /* void */;
667
668 /* always return EINTR rather than ERESTART... */
669 return (EINTR);
670 }
671
672 /*
673 * Suspend process until signal, providing mask to be set
674 * in the meantime. Note nonstandard calling convention:
675 * libc stub passes mask, not pointer, to save a copyin.
676 */
677 /* ARGSUSED */
678 int
679 sys___sigsuspend14(struct lwp *l, void *v, register_t *retval)
680 {
681 struct sys___sigsuspend14_args /* {
682 syscallarg(const sigset_t *) set;
683 } */ *uap = v;
684 struct proc *p;
685 sigset_t ss;
686 int error;
687
688 if (SCARG(uap, set)) {
689 error = copyin(SCARG(uap, set), &ss, sizeof(ss));
690 if (error)
691 return (error);
692 }
693
694 p = l->l_proc;
695 return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
696 }
697
698 int
699 sigaltstack1(struct proc *p, const struct sigaltstack *nss,
700 struct sigaltstack *oss)
701 {
702
703 if (oss)
704 *oss = p->p_sigctx.ps_sigstk;
705
706 if (nss) {
707 if (nss->ss_flags & ~SS_ALLBITS)
708 return (EINVAL);
709
710 if (nss->ss_flags & SS_DISABLE) {
711 if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
712 return (EINVAL);
713 } else {
714 if (nss->ss_size < MINSIGSTKSZ)
715 return (ENOMEM);
716 }
717 p->p_sigctx.ps_sigstk = *nss;
718 }
719
720 return (0);
721 }
722
723 /* ARGSUSED */
724 int
725 sys___sigaltstack14(struct lwp *l, void *v, register_t *retval)
726 {
727 struct sys___sigaltstack14_args /* {
728 syscallarg(const struct sigaltstack *) nss;
729 syscallarg(struct sigaltstack *) oss;
730 } */ *uap = v;
731 struct proc *p;
732 struct sigaltstack nss, oss;
733 int error;
734
735 if (SCARG(uap, nss)) {
736 error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
737 if (error)
738 return (error);
739 }
740 p = l->l_proc;
741 error = sigaltstack1(p,
742 SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
743 if (error)
744 return (error);
745 if (SCARG(uap, oss)) {
746 error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
747 if (error)
748 return (error);
749 }
750 return (0);
751 }
752
753 /* ARGSUSED */
754 int
755 sys_kill(struct lwp *l, void *v, register_t *retval)
756 {
757 struct sys_kill_args /* {
758 syscallarg(int) pid;
759 syscallarg(int) signum;
760 } */ *uap = v;
761 struct proc *cp, *p;
762 struct pcred *pc;
763 ksiginfo_t ksi;
764
765 cp = l->l_proc;
766 pc = cp->p_cred;
767 if ((u_int)SCARG(uap, signum) >= NSIG)
768 return (EINVAL);
769 KSI_INIT(&ksi);
770 ksi.ksi_signo = SCARG(uap, signum);
771 ksi.ksi_code = SI_USER;
772 ksi.ksi_pid = cp->p_pid;
773 ksi.ksi_uid = cp->p_ucred->cr_uid;
774 if (SCARG(uap, pid) > 0) {
775 /* kill single process */
776 if ((p = pfind(SCARG(uap, pid))) == NULL)
777 return (ESRCH);
778 if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
779 return (EPERM);
780 if (SCARG(uap, signum))
781 kpsignal2(p, &ksi, 1);
782 return (0);
783 }
784 switch (SCARG(uap, pid)) {
785 case -1: /* broadcast signal */
786 return (killpg1(cp, &ksi, 0, 1));
787 case 0: /* signal own process group */
788 return (killpg1(cp, &ksi, 0, 0));
789 default: /* negative explicit process group */
790 return (killpg1(cp, &ksi, -SCARG(uap, pid), 0));
791 }
792 /* NOTREACHED */
793 }
794
795 /*
796 * Common code for kill process group/broadcast kill.
797 * cp is calling process.
798 */
799 int
800 killpg1(struct proc *cp, ksiginfo_t *ksi, int pgid, int all)
801 {
802 struct proc *p;
803 struct pcred *pc;
804 struct pgrp *pgrp;
805 int nfound;
806 int signum = ksi->ksi_signo;
807
808 pc = cp->p_cred;
809 nfound = 0;
810 if (all) {
811 /*
812 * broadcast
813 */
814 proclist_lock_read();
815 PROCLIST_FOREACH(p, &allproc) {
816 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
817 p == cp || !CANSIGNAL(cp, pc, p, signum))
818 continue;
819 nfound++;
820 if (signum)
821 kpsignal2(p, ksi, 1);
822 }
823 proclist_unlock_read();
824 } else {
825 if (pgid == 0)
826 /*
827 * zero pgid means send to my process group.
828 */
829 pgrp = cp->p_pgrp;
830 else {
831 pgrp = pgfind(pgid);
832 if (pgrp == NULL)
833 return (ESRCH);
834 }
835 LIST_FOREACH(p, &pgrp->pg_members, p_pglist) {
836 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
837 !CANSIGNAL(cp, pc, p, signum))
838 continue;
839 nfound++;
840 if (signum && P_ZOMBIE(p) == 0)
841 kpsignal2(p, ksi, 1);
842 }
843 }
844 return (nfound ? 0 : ESRCH);
845 }
846
847 /*
848 * Send a signal to a process group.
849 */
850 void
851 gsignal(int pgid, int signum)
852 {
853 ksiginfo_t ksi;
854 KSI_INIT_EMPTY(&ksi);
855 ksi.ksi_signo = signum;
856 kgsignal(pgid, &ksi, NULL);
857 }
858
859 void
860 kgsignal(int pgid, ksiginfo_t *ksi, void *data)
861 {
862 struct pgrp *pgrp;
863
864 if (pgid && (pgrp = pgfind(pgid)))
865 kpgsignal(pgrp, ksi, data, 0);
866 }
867
868 /*
869 * Send a signal to a process group. If checktty is 1,
870 * limit to members which have a controlling terminal.
871 */
872 void
873 pgsignal(struct pgrp *pgrp, int sig, int checkctty)
874 {
875 ksiginfo_t ksi;
876 KSI_INIT_EMPTY(&ksi);
877 ksi.ksi_signo = sig;
878 kpgsignal(pgrp, &ksi, NULL, checkctty);
879 }
880
881 void
882 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
883 {
884 struct proc *p;
885
886 if (pgrp)
887 LIST_FOREACH(p, &pgrp->pg_members, p_pglist)
888 if (checkctty == 0 || p->p_flag & P_CONTROLT)
889 kpsignal(p, ksi, data);
890 }
891
892 /*
893 * Send a signal caused by a trap to the current process.
894 * If it will be caught immediately, deliver it with correct code.
895 * Otherwise, post it normally.
896 */
897 void
898 trapsignal(struct lwp *l, const ksiginfo_t *ksi)
899 {
900 struct proc *p;
901 struct sigacts *ps;
902 int signum = ksi->ksi_signo;
903
904 KASSERT(KSI_TRAP_P(ksi));
905
906 p = l->l_proc;
907 ps = p->p_sigacts;
908 if ((p->p_flag & P_TRACED) == 0 &&
909 sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
910 !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
911 p->p_stats->p_ru.ru_nsignals++;
912 #ifdef KTRACE
913 if (KTRPOINT(p, KTR_PSIG))
914 ktrpsig(p, signum, SIGACTION_PS(ps, signum).sa_handler,
915 &p->p_sigctx.ps_sigmask, ksi);
916 #endif
917 kpsendsig(l, ksi, &p->p_sigctx.ps_sigmask);
918 (void) splsched(); /* XXXSMP */
919 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
920 &p->p_sigctx.ps_sigmask);
921 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
922 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
923 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
924 sigaddset(&p->p_sigctx.ps_sigignore, signum);
925 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
926 }
927 (void) spl0(); /* XXXSMP */
928 } else {
929 p->p_sigctx.ps_lwp = l->l_lid;
930 /* XXX for core dump/debugger */
931 p->p_sigctx.ps_signo = ksi->ksi_signo;
932 p->p_sigctx.ps_code = ksi->ksi_trap;
933 kpsignal2(p, ksi, 1);
934 }
935 }
936
937 /*
938 * Fill in signal information and signal the parent for a child status change.
939 */
940 static void
941 child_psignal(struct proc *p, int dolock)
942 {
943 ksiginfo_t ksi;
944
945 KSI_INIT(&ksi);
946 ksi.ksi_signo = SIGCHLD;
947 ksi.ksi_code = p->p_xstat == SIGCONT ? CLD_CONTINUED : CLD_STOPPED;
948 ksi.ksi_pid = p->p_pid;
949 ksi.ksi_uid = p->p_ucred->cr_uid;
950 ksi.ksi_status = p->p_xstat;
951 ksi.ksi_utime = p->p_stats->p_ru.ru_utime.tv_sec;
952 ksi.ksi_stime = p->p_stats->p_ru.ru_stime.tv_sec;
953 kpsignal2(p->p_pptr, &ksi, dolock);
954 }
955
956 /*
957 * Send the signal to the process. If the signal has an action, the action
958 * is usually performed by the target process rather than the caller; we add
959 * the signal to the set of pending signals for the process.
960 *
961 * Exceptions:
962 * o When a stop signal is sent to a sleeping process that takes the
963 * default action, the process is stopped without awakening it.
964 * o SIGCONT restarts stopped processes (or puts them back to sleep)
965 * regardless of the signal action (eg, blocked or ignored).
966 *
967 * Other ignored signals are discarded immediately.
968 *
969 * XXXSMP: Invoked as psignal() or sched_psignal().
970 */
971 void
972 psignal1(struct proc *p, int signum, int dolock)
973 {
974 ksiginfo_t ksi;
975
976 KSI_INIT_EMPTY(&ksi);
977 ksi.ksi_signo = signum;
978 kpsignal2(p, &ksi, dolock);
979 }
980
981 void
982 kpsignal1(struct proc *p, ksiginfo_t *ksi, void *data, int dolock)
983 {
984
985 if ((p->p_flag & P_WEXIT) == 0 && data) {
986 size_t fd;
987 struct filedesc *fdp = p->p_fd;
988
989 ksi->ksi_fd = -1;
990 for (fd = 0; fd < fdp->fd_nfiles; fd++) {
991 struct file *fp = fdp->fd_ofiles[fd];
992 /* XXX: lock? */
993 if (fp && fp->f_data == data) {
994 ksi->ksi_fd = fd;
995 break;
996 }
997 }
998 }
999 kpsignal2(p, ksi, dolock);
1000 }
1001
1002 static void
1003 kpsignal2(struct proc *p, const ksiginfo_t *ksi, int dolock)
1004 {
1005 struct lwp *l, *suspended = NULL;
1006 struct sadata_vp *vp;
1007 int s = 0, prop, allsusp;
1008 sig_t action;
1009 int signum = ksi->ksi_signo;
1010
1011 #ifdef DIAGNOSTIC
1012 if (signum <= 0 || signum >= NSIG)
1013 panic("psignal signal number %d", signum);
1014
1015 /* XXXSMP: works, but icky */
1016 if (dolock)
1017 SCHED_ASSERT_UNLOCKED();
1018 else
1019 SCHED_ASSERT_LOCKED();
1020 #endif
1021
1022 /*
1023 * Notify any interested parties in the signal.
1024 */
1025 KNOTE(&p->p_klist, NOTE_SIGNAL | signum);
1026
1027 prop = sigprop[signum];
1028
1029 /*
1030 * If proc is traced, always give parent a chance.
1031 */
1032 if (p->p_flag & P_TRACED) {
1033 action = SIG_DFL;
1034
1035 /*
1036 * If the process is being traced and the signal is being
1037 * caught, make sure to save any ksiginfo.
1038 */
1039 if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
1040 ksiginfo_put(p, ksi);
1041 } else {
1042 /*
1043 * If the signal was the result of a trap, reset it
1044 * to default action if it's currently masked, so that it would
1045 * coredump immediatelly instead of spinning repeatedly
1046 * taking the signal.
1047 */
1048 if (KSI_TRAP_P(ksi)
1049 && sigismember(&p->p_sigctx.ps_sigmask, signum)
1050 && !sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
1051 sigdelset(&p->p_sigctx.ps_sigignore, signum);
1052 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1053 sigdelset(&p->p_sigctx.ps_sigmask, signum);
1054 SIGACTION(p, signum).sa_handler = SIG_DFL;
1055 }
1056
1057 /*
1058 * If the signal is being ignored,
1059 * then we forget about it immediately.
1060 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
1061 * and if it is set to SIG_IGN,
1062 * action will be SIG_DFL here.)
1063 */
1064 if (sigismember(&p->p_sigctx.ps_sigignore, signum))
1065 return;
1066 if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1067 action = SIG_HOLD;
1068 else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
1069 action = SIG_CATCH;
1070 else {
1071 action = SIG_DFL;
1072
1073 if (prop & SA_KILL && p->p_nice > NZERO)
1074 p->p_nice = NZERO;
1075
1076 /*
1077 * If sending a tty stop signal to a member of an
1078 * orphaned process group, discard the signal here if
1079 * the action is default; don't stop the process below
1080 * if sleeping, and don't clear any pending SIGCONT.
1081 */
1082 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
1083 return;
1084 }
1085 }
1086
1087 if (prop & SA_CONT)
1088 sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
1089
1090 if (prop & SA_STOP)
1091 sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
1092
1093 /*
1094 * If the signal doesn't have SA_CANTMASK (no override for SIGKILL,
1095 * please!), check if anything waits on it. If yes, save the
1096 * info into provided ps_sigwaited, and wake-up the waiter.
1097 * The signal won't be processed further here.
1098 */
1099 if ((prop & SA_CANTMASK) == 0
1100 && p->p_sigctx.ps_sigwaited
1101 && sigismember(p->p_sigctx.ps_sigwait, signum)
1102 && p->p_stat != SSTOP) {
1103 p->p_sigctx.ps_sigwaited->ksi_info = ksi->ksi_info;
1104 p->p_sigctx.ps_sigwaited = NULL;
1105 if (dolock)
1106 wakeup_one(&p->p_sigctx.ps_sigwait);
1107 else
1108 sched_wakeup(&p->p_sigctx.ps_sigwait);
1109 return;
1110 }
1111
1112 sigaddset(&p->p_sigctx.ps_siglist, signum);
1113
1114 /* CHECKSIGS() is "inlined" here. */
1115 p->p_sigctx.ps_sigcheck = 1;
1116
1117 /*
1118 * Defer further processing for signals which are held,
1119 * except that stopped processes must be continued by SIGCONT.
1120 */
1121 if (action == SIG_HOLD &&
1122 ((prop & SA_CONT) == 0 || p->p_stat != SSTOP)) {
1123 ksiginfo_put(p, ksi);
1124 return;
1125 }
1126 /* XXXSMP: works, but icky */
1127 if (dolock)
1128 SCHED_LOCK(s);
1129
1130 if (p->p_flag & P_SA) {
1131 allsusp = 0;
1132 l = NULL;
1133 if (p->p_stat == SACTIVE) {
1134 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1135 l = vp->savp_lwp;
1136 KDASSERT(l != NULL);
1137 if (l->l_flag & L_SA_IDLE) {
1138 /* wakeup idle LWP */
1139 goto found;
1140 /*NOTREACHED*/
1141 } else if (l->l_flag & L_SA_YIELD) {
1142 /* idle LWP is already waking up */
1143 goto out;
1144 /*NOTREACHED*/
1145 }
1146 }
1147 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1148 l = vp->savp_lwp;
1149 if (l->l_stat == LSRUN ||
1150 l->l_stat == LSONPROC) {
1151 signotify(p);
1152 goto out;
1153 /*NOTREACHED*/
1154 }
1155 if (l->l_stat == LSSLEEP &&
1156 l->l_flag & L_SINTR) {
1157 /* ok to signal vp lwp */
1158 break;
1159 } else
1160 l = NULL;
1161 }
1162 } else if (p->p_stat == SSTOP) {
1163 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1164 l = vp->savp_lwp;
1165 if (l->l_stat == LSSLEEP && (l->l_flag & L_SINTR) != 0)
1166 break;
1167 l = NULL;
1168 }
1169 }
1170 } else if (p->p_nrlwps > 0 && (p->p_stat != SSTOP)) {
1171 /*
1172 * At least one LWP is running or on a run queue.
1173 * The signal will be noticed when one of them returns
1174 * to userspace.
1175 */
1176 signotify(p);
1177 /*
1178 * The signal will be noticed very soon.
1179 */
1180 goto out;
1181 /*NOTREACHED*/
1182 } else {
1183 /*
1184 * Find out if any of the sleeps are interruptable,
1185 * and if all the live LWPs remaining are suspended.
1186 */
1187 allsusp = 1;
1188 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1189 if (l->l_stat == LSSLEEP &&
1190 l->l_flag & L_SINTR)
1191 break;
1192 if (l->l_stat == LSSUSPENDED)
1193 suspended = l;
1194 else if ((l->l_stat != LSZOMB) &&
1195 (l->l_stat != LSDEAD))
1196 allsusp = 0;
1197 }
1198 }
1199
1200 found:
1201 switch (p->p_stat) {
1202 case SACTIVE:
1203
1204 if (l != NULL && (p->p_flag & P_TRACED))
1205 goto run;
1206
1207 /*
1208 * If SIGCONT is default (or ignored) and process is
1209 * asleep, we are finished; the process should not
1210 * be awakened.
1211 */
1212 if ((prop & SA_CONT) && action == SIG_DFL) {
1213 sigdelset(&p->p_sigctx.ps_siglist, signum);
1214 goto done;
1215 }
1216
1217 /*
1218 * When a sleeping process receives a stop
1219 * signal, process immediately if possible.
1220 */
1221 if ((prop & SA_STOP) && action == SIG_DFL) {
1222 /*
1223 * If a child holding parent blocked,
1224 * stopping could cause deadlock.
1225 */
1226 if (p->p_flag & P_PPWAIT) {
1227 goto out;
1228 }
1229 sigdelset(&p->p_sigctx.ps_siglist, signum);
1230 p->p_xstat = signum;
1231 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
1232 /*
1233 * XXXSMP: recursive call; don't lock
1234 * the second time around.
1235 */
1236 child_psignal(p, 0);
1237 }
1238 proc_stop(p, 1); /* XXXSMP: recurse? */
1239 goto done;
1240 }
1241
1242 if (l == NULL) {
1243 /*
1244 * Special case: SIGKILL of a process
1245 * which is entirely composed of
1246 * suspended LWPs should succeed. We
1247 * make this happen by unsuspending one of
1248 * them.
1249 */
1250 if (allsusp && (signum == SIGKILL)) {
1251 lwp_continue(suspended);
1252 }
1253 goto done;
1254 }
1255 /*
1256 * All other (caught or default) signals
1257 * cause the process to run.
1258 */
1259 goto runfast;
1260 /*NOTREACHED*/
1261 case SSTOP:
1262 /* Process is stopped */
1263 /*
1264 * If traced process is already stopped,
1265 * then no further action is necessary.
1266 */
1267 if (p->p_flag & P_TRACED)
1268 goto done;
1269
1270 /*
1271 * Kill signal always sets processes running,
1272 * if possible.
1273 */
1274 if (signum == SIGKILL) {
1275 l = proc_unstop(p);
1276 if (l)
1277 goto runfast;
1278 goto done;
1279 }
1280
1281 if (prop & SA_CONT) {
1282 /*
1283 * If SIGCONT is default (or ignored),
1284 * we continue the process but don't
1285 * leave the signal in ps_siglist, as
1286 * it has no further action. If
1287 * SIGCONT is held, we continue the
1288 * process and leave the signal in
1289 * ps_siglist. If the process catches
1290 * SIGCONT, let it handle the signal
1291 * itself. If it isn't waiting on an
1292 * event, then it goes back to run
1293 * state. Otherwise, process goes
1294 * back to sleep state.
1295 */
1296 if (action == SIG_DFL)
1297 sigdelset(&p->p_sigctx.ps_siglist,
1298 signum);
1299 l = proc_unstop(p);
1300 if (l && (action == SIG_CATCH))
1301 goto runfast;
1302 goto out;
1303 }
1304
1305 if (prop & SA_STOP) {
1306 /*
1307 * Already stopped, don't need to stop again.
1308 * (If we did the shell could get confused.)
1309 */
1310 sigdelset(&p->p_sigctx.ps_siglist, signum);
1311 goto done;
1312 }
1313
1314 /*
1315 * If a lwp is sleeping interruptibly, then
1316 * wake it up; it will run until the kernel
1317 * boundary, where it will stop in issignal(),
1318 * since p->p_stat is still SSTOP. When the
1319 * process is continued, it will be made
1320 * runnable and can look at the signal.
1321 */
1322 if (l)
1323 goto run;
1324 goto out;
1325 case SIDL:
1326 /* Process is being created by fork */
1327 /* XXX: We are not ready to receive signals yet */
1328 goto done;
1329 default:
1330 /* Else what? */
1331 panic("psignal: Invalid process state %d.", p->p_stat);
1332 }
1333 /*NOTREACHED*/
1334
1335 runfast:
1336 if (action == SIG_CATCH) {
1337 ksiginfo_put(p, ksi);
1338 action = SIG_HOLD;
1339 }
1340 /*
1341 * Raise priority to at least PUSER.
1342 */
1343 if (l->l_priority > PUSER)
1344 l->l_priority = PUSER;
1345 run:
1346 if (action == SIG_CATCH) {
1347 ksiginfo_put(p, ksi);
1348 action = SIG_HOLD;
1349 }
1350
1351 setrunnable(l); /* XXXSMP: recurse? */
1352 out:
1353 if (action == SIG_CATCH)
1354 ksiginfo_put(p, ksi);
1355 done:
1356 /* XXXSMP: works, but icky */
1357 if (dolock)
1358 SCHED_UNLOCK(s);
1359 }
1360
1361 siginfo_t *
1362 siginfo_alloc(int flags)
1363 {
1364
1365 return pool_get(&siginfo_pool, flags);
1366 }
1367
1368 void
1369 siginfo_free(void *arg)
1370 {
1371
1372 pool_put(&siginfo_pool, arg);
1373 }
1374
1375 void
1376 kpsendsig(struct lwp *l, const ksiginfo_t *ksi, const sigset_t *mask)
1377 {
1378 struct proc *p = l->l_proc;
1379 struct lwp *le, *li;
1380 siginfo_t *si;
1381 int f;
1382
1383 if (p->p_flag & P_SA) {
1384
1385 /* XXXUPSXXX What if not on sa_vp ? */
1386
1387 f = l->l_flag & L_SA;
1388 l->l_flag &= ~L_SA;
1389 si = siginfo_alloc(PR_WAITOK);
1390 si->_info = ksi->ksi_info;
1391 le = li = NULL;
1392 if (KSI_TRAP_P(ksi))
1393 le = l;
1394 else
1395 li = l;
1396 if (sa_upcall(l, SA_UPCALL_SIGNAL | SA_UPCALL_DEFER, le, li,
1397 sizeof(*si), si, siginfo_free) != 0) {
1398 siginfo_free(si);
1399 if (KSI_TRAP_P(ksi))
1400 /* XXX What do we do here?? */;
1401 }
1402 l->l_flag |= f;
1403 return;
1404 }
1405
1406 (*p->p_emul->e_sendsig)(ksi, mask);
1407 }
1408
1409 static __inline int firstsig(const sigset_t *);
1410
1411 static __inline int
1412 firstsig(const sigset_t *ss)
1413 {
1414 int sig;
1415
1416 sig = ffs(ss->__bits[0]);
1417 if (sig != 0)
1418 return (sig);
1419 #if NSIG > 33
1420 sig = ffs(ss->__bits[1]);
1421 if (sig != 0)
1422 return (sig + 32);
1423 #endif
1424 #if NSIG > 65
1425 sig = ffs(ss->__bits[2]);
1426 if (sig != 0)
1427 return (sig + 64);
1428 #endif
1429 #if NSIG > 97
1430 sig = ffs(ss->__bits[3]);
1431 if (sig != 0)
1432 return (sig + 96);
1433 #endif
1434 return (0);
1435 }
1436
1437 /*
1438 * If the current process has received a signal (should be caught or cause
1439 * termination, should interrupt current syscall), return the signal number.
1440 * Stop signals with default action are processed immediately, then cleared;
1441 * they aren't returned. This is checked after each entry to the system for
1442 * a syscall or trap (though this can usually be done without calling issignal
1443 * by checking the pending signal masks in the CURSIG macro.) The normal call
1444 * sequence is
1445 *
1446 * while (signum = CURSIG(curlwp))
1447 * postsig(signum);
1448 */
1449 int
1450 issignal(struct lwp *l)
1451 {
1452 struct proc *p = l->l_proc;
1453 int s = 0, signum, prop;
1454 int dolock = (l->l_flag & L_SINTR) == 0, locked = !dolock;
1455 sigset_t ss;
1456
1457 /* Bail out if we do not own the virtual processor */
1458 if (l->l_flag & L_SA && l->l_savp->savp_lwp != l)
1459 return 0;
1460
1461 if (p->p_stat == SSTOP) {
1462 /*
1463 * The process is stopped/stopping. Stop ourselves now that
1464 * we're on the kernel/userspace boundary.
1465 */
1466 if (dolock)
1467 SCHED_LOCK(s);
1468 l->l_stat = LSSTOP;
1469 p->p_nrlwps--;
1470 if (p->p_flag & P_TRACED)
1471 goto sigtraceswitch;
1472 else
1473 goto sigswitch;
1474 }
1475 for (;;) {
1476 sigpending1(p, &ss);
1477 if (p->p_flag & P_PPWAIT)
1478 sigminusset(&stopsigmask, &ss);
1479 signum = firstsig(&ss);
1480 if (signum == 0) { /* no signal to send */
1481 p->p_sigctx.ps_sigcheck = 0;
1482 if (locked && dolock)
1483 SCHED_LOCK(s);
1484 return (0);
1485 }
1486 /* take the signal! */
1487 sigdelset(&p->p_sigctx.ps_siglist, signum);
1488
1489 /*
1490 * We should see pending but ignored signals
1491 * only if P_TRACED was on when they were posted.
1492 */
1493 if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
1494 (p->p_flag & P_TRACED) == 0)
1495 continue;
1496
1497 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1498 /*
1499 * If traced, always stop, and stay
1500 * stopped until released by the debugger.
1501 */
1502 p->p_xstat = signum;
1503
1504 /* Emulation-specific handling of signal trace */
1505 if ((p->p_emul->e_tracesig != NULL) &&
1506 ((*p->p_emul->e_tracesig)(p, signum) != 0))
1507 goto childresumed;
1508
1509 if ((p->p_flag & P_FSTRACE) == 0)
1510 child_psignal(p, dolock);
1511 if (dolock)
1512 SCHED_LOCK(s);
1513 proc_stop(p, 1);
1514 sigtraceswitch:
1515 mi_switch(l, NULL);
1516 SCHED_ASSERT_UNLOCKED();
1517 if (dolock)
1518 splx(s);
1519 else
1520 dolock = 1;
1521
1522 childresumed:
1523 /*
1524 * If we are no longer being traced, or the parent
1525 * didn't give us a signal, look for more signals.
1526 */
1527 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
1528 continue;
1529
1530 /*
1531 * If the new signal is being masked, look for other
1532 * signals.
1533 */
1534 signum = p->p_xstat;
1535 p->p_xstat = 0;
1536 /*
1537 * `p->p_sigctx.ps_siglist |= mask' is done
1538 * in setrunnable().
1539 */
1540 if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1541 continue;
1542 /* take the signal! */
1543 sigdelset(&p->p_sigctx.ps_siglist, signum);
1544 }
1545
1546 prop = sigprop[signum];
1547
1548 /*
1549 * Decide whether the signal should be returned.
1550 * Return the signal's number, or fall through
1551 * to clear it from the pending mask.
1552 */
1553 switch ((long)SIGACTION(p, signum).sa_handler) {
1554
1555 case (long)SIG_DFL:
1556 /*
1557 * Don't take default actions on system processes.
1558 */
1559 if (p->p_pid <= 1) {
1560 #ifdef DIAGNOSTIC
1561 /*
1562 * Are you sure you want to ignore SIGSEGV
1563 * in init? XXX
1564 */
1565 printf("Process (pid %d) got signal %d\n",
1566 p->p_pid, signum);
1567 #endif
1568 break; /* == ignore */
1569 }
1570 /*
1571 * If there is a pending stop signal to process
1572 * with default action, stop here,
1573 * then clear the signal. However,
1574 * if process is member of an orphaned
1575 * process group, ignore tty stop signals.
1576 */
1577 if (prop & SA_STOP) {
1578 if (p->p_flag & P_TRACED ||
1579 (p->p_pgrp->pg_jobc == 0 &&
1580 prop & SA_TTYSTOP))
1581 break; /* == ignore */
1582 p->p_xstat = signum;
1583 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1584 child_psignal(p, dolock);
1585 if (dolock)
1586 SCHED_LOCK(s);
1587 proc_stop(p, 1);
1588 sigswitch:
1589 mi_switch(l, NULL);
1590 SCHED_ASSERT_UNLOCKED();
1591 if (dolock)
1592 splx(s);
1593 else
1594 dolock = 1;
1595 break;
1596 } else if (prop & SA_IGNORE) {
1597 /*
1598 * Except for SIGCONT, shouldn't get here.
1599 * Default action is to ignore; drop it.
1600 */
1601 break; /* == ignore */
1602 } else
1603 goto keep;
1604 /*NOTREACHED*/
1605
1606 case (long)SIG_IGN:
1607 /*
1608 * Masking above should prevent us ever trying
1609 * to take action on an ignored signal other
1610 * than SIGCONT, unless process is traced.
1611 */
1612 #ifdef DEBUG_ISSIGNAL
1613 if ((prop & SA_CONT) == 0 &&
1614 (p->p_flag & P_TRACED) == 0)
1615 printf("issignal\n");
1616 #endif
1617 break; /* == ignore */
1618
1619 default:
1620 /*
1621 * This signal has an action, let
1622 * postsig() process it.
1623 */
1624 goto keep;
1625 }
1626 }
1627 /* NOTREACHED */
1628
1629 keep:
1630 /* leave the signal for later */
1631 sigaddset(&p->p_sigctx.ps_siglist, signum);
1632 CHECKSIGS(p);
1633 if (locked && dolock)
1634 SCHED_LOCK(s);
1635 return (signum);
1636 }
1637
1638 /*
1639 * Put the argument process into the stopped state and notify the parent
1640 * via wakeup. Signals are handled elsewhere. The process must not be
1641 * on the run queue.
1642 */
1643 void
1644 proc_stop(struct proc *p, int wakeup)
1645 {
1646 struct lwp *l;
1647 struct proc *parent;
1648 struct sadata_vp *vp;
1649
1650 SCHED_ASSERT_LOCKED();
1651
1652 /* XXX lock process LWP state */
1653 p->p_flag &= ~P_WAITED;
1654 p->p_stat = SSTOP;
1655 parent = p->p_pptr;
1656 parent->p_nstopchild++;
1657
1658 if (p->p_flag & P_SA) {
1659 /*
1660 * Only (try to) put the LWP on the VP in stopped
1661 * state.
1662 * All other LWPs will suspend in sa_setwoken()
1663 * because the VP-LWP in stopped state cannot be
1664 * repossessed.
1665 */
1666 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1667 l = vp->savp_lwp;
1668 if (l->l_stat == LSONPROC && l->l_cpu == curcpu()) {
1669 l->l_stat = LSSTOP;
1670 p->p_nrlwps--;
1671 } else if (l->l_stat == LSRUN) {
1672 /* Remove LWP from the run queue */
1673 remrunqueue(l);
1674 l->l_stat = LSSTOP;
1675 p->p_nrlwps--;
1676 } else if (l->l_stat == LSSLEEP &&
1677 l->l_flag & L_SA_IDLE) {
1678 l->l_flag &= ~L_SA_IDLE;
1679 l->l_stat = LSSTOP;
1680 }
1681 }
1682 goto out;
1683 }
1684
1685 /*
1686 * Put as many LWP's as possible in stopped state.
1687 * Sleeping ones will notice the stopped state as they try to
1688 * return to userspace.
1689 */
1690
1691 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1692 if (l->l_stat == LSONPROC) {
1693 /* XXX SMP this assumes that a LWP that is LSONPROC
1694 * is curlwp and hence is about to be mi_switched
1695 * away; the only callers of proc_stop() are:
1696 * - psignal
1697 * - issignal()
1698 * For the former, proc_stop() is only called when
1699 * no processes are running, so we don't worry.
1700 * For the latter, proc_stop() is called right
1701 * before mi_switch().
1702 */
1703 l->l_stat = LSSTOP;
1704 p->p_nrlwps--;
1705 } else if (l->l_stat == LSRUN) {
1706 /* Remove LWP from the run queue */
1707 remrunqueue(l);
1708 l->l_stat = LSSTOP;
1709 p->p_nrlwps--;
1710 } else if ((l->l_stat == LSSLEEP) ||
1711 (l->l_stat == LSSUSPENDED) ||
1712 (l->l_stat == LSZOMB) ||
1713 (l->l_stat == LSDEAD)) {
1714 /*
1715 * Don't do anything; let sleeping LWPs
1716 * discover the stopped state of the process
1717 * on their way out of the kernel; otherwise,
1718 * things like NFS threads that sleep with
1719 * locks will block the rest of the system
1720 * from getting any work done.
1721 *
1722 * Suspended/dead/zombie LWPs aren't going
1723 * anywhere, so we don't need to touch them.
1724 */
1725 }
1726 #ifdef DIAGNOSTIC
1727 else {
1728 panic("proc_stop: process %d lwp %d "
1729 "in unstoppable state %d.\n",
1730 p->p_pid, l->l_lid, l->l_stat);
1731 }
1732 #endif
1733 }
1734
1735 out:
1736 /* XXX unlock process LWP state */
1737
1738 if (wakeup)
1739 sched_wakeup((caddr_t)p->p_pptr);
1740 }
1741
1742 /*
1743 * Given a process in state SSTOP, set the state back to SACTIVE and
1744 * move LSSTOP'd LWPs to LSSLEEP or make them runnable.
1745 *
1746 * If no LWPs ended up runnable (and therefore able to take a signal),
1747 * return a LWP that is sleeping interruptably. The caller can wake
1748 * that LWP up to take a signal.
1749 */
1750 struct lwp *
1751 proc_unstop(struct proc *p)
1752 {
1753 struct lwp *l, *lr = NULL;
1754 struct sadata_vp *vp;
1755 int cantake = 0;
1756
1757 SCHED_ASSERT_LOCKED();
1758
1759 /*
1760 * Our caller wants to be informed if there are only sleeping
1761 * and interruptable LWPs left after we have run so that it
1762 * can invoke setrunnable() if required - return one of the
1763 * interruptable LWPs if this is the case.
1764 */
1765
1766 if (!(p->p_flag & P_WAITED))
1767 p->p_pptr->p_nstopchild--;
1768 p->p_stat = SACTIVE;
1769 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1770 if (l->l_stat == LSRUN) {
1771 lr = NULL;
1772 cantake = 1;
1773 }
1774 if (l->l_stat != LSSTOP)
1775 continue;
1776
1777 if (l->l_wchan != NULL) {
1778 l->l_stat = LSSLEEP;
1779 if ((cantake == 0) && (l->l_flag & L_SINTR)) {
1780 lr = l;
1781 cantake = 1;
1782 }
1783 } else {
1784 setrunnable(l);
1785 lr = NULL;
1786 cantake = 1;
1787 }
1788 }
1789 if (p->p_flag & P_SA) {
1790 /* Only consider returning the LWP on the VP. */
1791 SLIST_FOREACH(vp, &p->p_sa->sa_vps, savp_next) {
1792 lr = vp->savp_lwp;
1793 if (lr->l_stat == LSSLEEP) {
1794 if (lr->l_flag & L_SA_YIELD) {
1795 setrunnable(lr);
1796 break;
1797 } else if (lr->l_flag & L_SINTR)
1798 return lr;
1799 }
1800 }
1801 return NULL;
1802 }
1803 return lr;
1804 }
1805
1806 /*
1807 * Take the action for the specified signal
1808 * from the current set of pending signals.
1809 */
1810 void
1811 postsig(int signum)
1812 {
1813 struct lwp *l;
1814 struct proc *p;
1815 struct sigacts *ps;
1816 sig_t action;
1817 sigset_t *returnmask;
1818
1819 l = curlwp;
1820 p = l->l_proc;
1821 ps = p->p_sigacts;
1822 #ifdef DIAGNOSTIC
1823 if (signum == 0)
1824 panic("postsig");
1825 #endif
1826
1827 KERNEL_PROC_LOCK(l);
1828
1829 #ifdef MULTIPROCESSOR
1830 /*
1831 * On MP, issignal() can return the same signal to multiple
1832 * LWPs. The LWPs will block above waiting for the kernel
1833 * lock and the first LWP which gets through will then remove
1834 * the signal from ps_siglist. All other LWPs exit here.
1835 */
1836 if (!sigismember(&p->p_sigctx.ps_siglist, signum)) {
1837 KERNEL_PROC_UNLOCK(l);
1838 return;
1839 }
1840 #endif
1841 sigdelset(&p->p_sigctx.ps_siglist, signum);
1842 action = SIGACTION_PS(ps, signum).sa_handler;
1843 if (action == SIG_DFL) {
1844 #ifdef KTRACE
1845 if (KTRPOINT(p, KTR_PSIG))
1846 ktrpsig(p, signum, action,
1847 p->p_sigctx.ps_flags & SAS_OLDMASK ?
1848 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
1849 NULL);
1850 #endif
1851 /*
1852 * Default action, where the default is to kill
1853 * the process. (Other cases were ignored above.)
1854 */
1855 sigexit(l, signum);
1856 /* NOTREACHED */
1857 } else {
1858 ksiginfo_t *ksi;
1859 /*
1860 * If we get here, the signal must be caught.
1861 */
1862 #ifdef DIAGNOSTIC
1863 if (action == SIG_IGN ||
1864 sigismember(&p->p_sigctx.ps_sigmask, signum))
1865 panic("postsig action");
1866 #endif
1867 /*
1868 * Set the new mask value and also defer further
1869 * occurrences of this signal.
1870 *
1871 * Special case: user has done a sigpause. Here the
1872 * current mask is not of interest, but rather the
1873 * mask from before the sigpause is what we want
1874 * restored after the signal processing is completed.
1875 */
1876 if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
1877 returnmask = &p->p_sigctx.ps_oldmask;
1878 p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
1879 } else
1880 returnmask = &p->p_sigctx.ps_sigmask;
1881 p->p_stats->p_ru.ru_nsignals++;
1882 ksi = ksiginfo_get(p, signum);
1883 #ifdef KTRACE
1884 if (KTRPOINT(p, KTR_PSIG))
1885 ktrpsig(p, signum, action,
1886 p->p_sigctx.ps_flags & SAS_OLDMASK ?
1887 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask,
1888 ksi);
1889 #endif
1890 if (ksi == NULL) {
1891 ksiginfo_t ksi1;
1892 /*
1893 * we did not save any siginfo for this, either
1894 * because the signal was not caught, or because the
1895 * user did not request SA_SIGINFO
1896 */
1897 KSI_INIT_EMPTY(&ksi1);
1898 ksi1.ksi_signo = signum;
1899 kpsendsig(l, &ksi1, returnmask);
1900 } else {
1901 kpsendsig(l, ksi, returnmask);
1902 pool_put(&ksiginfo_pool, ksi);
1903 }
1904 p->p_sigctx.ps_lwp = 0;
1905 p->p_sigctx.ps_code = 0;
1906 p->p_sigctx.ps_signo = 0;
1907 (void) splsched(); /* XXXSMP */
1908 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
1909 &p->p_sigctx.ps_sigmask);
1910 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
1911 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1912 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1913 sigaddset(&p->p_sigctx.ps_sigignore, signum);
1914 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1915 }
1916 (void) spl0(); /* XXXSMP */
1917 }
1918
1919 KERNEL_PROC_UNLOCK(l);
1920 }
1921
1922 /*
1923 * Kill the current process for stated reason.
1924 */
1925 void
1926 killproc(struct proc *p, const char *why)
1927 {
1928 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1929 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1930 psignal(p, SIGKILL);
1931 }
1932
1933 /*
1934 * Force the current process to exit with the specified signal, dumping core
1935 * if appropriate. We bypass the normal tests for masked and caught signals,
1936 * allowing unrecoverable failures to terminate the process without changing
1937 * signal state. Mark the accounting record with the signal termination.
1938 * If dumping core, save the signal number for the debugger. Calls exit and
1939 * does not return.
1940 */
1941
1942 #if defined(DEBUG)
1943 int kern_logsigexit = 1; /* not static to make public for sysctl */
1944 #else
1945 int kern_logsigexit = 0; /* not static to make public for sysctl */
1946 #endif
1947
1948 static const char logcoredump[] =
1949 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1950 static const char lognocoredump[] =
1951 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
1952
1953 /* Wrapper function for use in p_userret */
1954 static void
1955 lwp_coredump_hook(struct lwp *l, void *arg)
1956 {
1957 int s;
1958
1959 /*
1960 * Suspend ourselves, so that the kernel stack and therefore
1961 * the userland registers saved in the trapframe are around
1962 * for coredump() to write them out.
1963 */
1964 KERNEL_PROC_LOCK(l);
1965 l->l_flag &= ~L_DETACHED;
1966 SCHED_LOCK(s);
1967 l->l_stat = LSSUSPENDED;
1968 l->l_proc->p_nrlwps--;
1969 /* XXX NJWLWP check if this makes sense here: */
1970 l->l_proc->p_stats->p_ru.ru_nvcsw++;
1971 mi_switch(l, NULL);
1972 SCHED_ASSERT_UNLOCKED();
1973 splx(s);
1974
1975 lwp_exit(l);
1976 }
1977
1978 void
1979 sigexit(struct lwp *l, int signum)
1980 {
1981 struct proc *p;
1982 #if 0
1983 struct lwp *l2;
1984 #endif
1985 int error, exitsig;
1986
1987 p = l->l_proc;
1988
1989 /*
1990 * Don't permit coredump() or exit1() multiple times
1991 * in the same process.
1992 */
1993 if (p->p_flag & P_WEXIT) {
1994 KERNEL_PROC_UNLOCK(l);
1995 (*p->p_userret)(l, p->p_userret_arg);
1996 }
1997 p->p_flag |= P_WEXIT;
1998 /* We don't want to switch away from exiting. */
1999 /* XXX multiprocessor: stop LWPs on other processors. */
2000 #if 0
2001 if (p->p_flag & P_SA) {
2002 LIST_FOREACH(l2, &p->p_lwps, l_sibling)
2003 l2->l_flag &= ~L_SA;
2004 p->p_flag &= ~P_SA;
2005 }
2006 #endif
2007
2008 /* Make other LWPs stick around long enough to be dumped */
2009 p->p_userret = lwp_coredump_hook;
2010 p->p_userret_arg = NULL;
2011
2012 exitsig = signum;
2013 p->p_acflag |= AXSIG;
2014 if (sigprop[signum] & SA_CORE) {
2015 p->p_sigctx.ps_signo = signum;
2016 if ((error = coredump(l, NULL)) == 0)
2017 exitsig |= WCOREFLAG;
2018
2019 if (kern_logsigexit) {
2020 /* XXX What if we ever have really large UIDs? */
2021 int uid = p->p_cred && p->p_ucred ?
2022 (int) p->p_ucred->cr_uid : -1;
2023
2024 if (error)
2025 log(LOG_INFO, lognocoredump, p->p_pid,
2026 p->p_comm, uid, signum, error);
2027 else
2028 log(LOG_INFO, logcoredump, p->p_pid,
2029 p->p_comm, uid, signum);
2030 }
2031
2032 }
2033
2034 exit1(l, W_EXITCODE(0, exitsig));
2035 /* NOTREACHED */
2036 }
2037
2038 /*
2039 * Dump core, into a file named "progname.core" or "core" (depending on the
2040 * value of shortcorename), unless the process was setuid/setgid.
2041 */
2042 int
2043 coredump(struct lwp *l, const char *pattern)
2044 {
2045 struct vnode *vp;
2046 struct proc *p;
2047 struct vmspace *vm;
2048 struct ucred *cred;
2049 struct nameidata nd;
2050 struct vattr vattr;
2051 struct mount *mp;
2052 int error, error1;
2053 char name[MAXPATHLEN];
2054
2055 p = l->l_proc;
2056 vm = p->p_vmspace;
2057 cred = p->p_cred->pc_ucred;
2058
2059 /*
2060 * Make sure the process has not set-id, to prevent data leaks.
2061 */
2062 if (p->p_flag & P_SUGID)
2063 return (EPERM);
2064
2065 /*
2066 * Refuse to core if the data + stack + user size is larger than
2067 * the core dump limit. XXX THIS IS WRONG, because of mapped
2068 * data.
2069 */
2070 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
2071 p->p_rlimit[RLIMIT_CORE].rlim_cur)
2072 return (EFBIG); /* better error code? */
2073
2074 restart:
2075 /*
2076 * The core dump will go in the current working directory. Make
2077 * sure that the directory is still there and that the mount flags
2078 * allow us to write core dumps there.
2079 */
2080 vp = p->p_cwdi->cwdi_cdir;
2081 if (vp->v_mount == NULL ||
2082 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
2083 return (EPERM);
2084
2085 if (pattern == NULL)
2086 pattern = p->p_limit->pl_corename;
2087 if ((error = build_corename(p, name, pattern, sizeof(name))) != 0)
2088 return error;
2089
2090 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
2091 error = vn_open(&nd, O_CREAT | O_NOFOLLOW | FWRITE, S_IRUSR | S_IWUSR);
2092 if (error)
2093 return (error);
2094 vp = nd.ni_vp;
2095
2096 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2097 VOP_UNLOCK(vp, 0);
2098 if ((error = vn_close(vp, FWRITE, cred, p)) != 0)
2099 return (error);
2100 if ((error = vn_start_write(NULL, &mp,
2101 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
2102 return (error);
2103 goto restart;
2104 }
2105
2106 /* Don't dump to non-regular files or files with links. */
2107 if (vp->v_type != VREG ||
2108 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
2109 error = EINVAL;
2110 goto out;
2111 }
2112 VATTR_NULL(&vattr);
2113 vattr.va_size = 0;
2114 VOP_LEASE(vp, p, cred, LEASE_WRITE);
2115 VOP_SETATTR(vp, &vattr, cred, p);
2116 p->p_acflag |= ACORE;
2117
2118 /* Now dump the actual core file. */
2119 error = (*p->p_execsw->es_coredump)(l, vp, cred);
2120 out:
2121 VOP_UNLOCK(vp, 0);
2122 vn_finished_write(mp, 0);
2123 error1 = vn_close(vp, FWRITE, cred, p);
2124 if (error == 0)
2125 error = error1;
2126 return (error);
2127 }
2128
2129 /*
2130 * Nonexistent system call-- signal process (may want to handle it).
2131 * Flag error in case process won't see signal immediately (blocked or ignored).
2132 */
2133 /* ARGSUSED */
2134 int
2135 sys_nosys(struct lwp *l, void *v, register_t *retval)
2136 {
2137 struct proc *p;
2138
2139 p = l->l_proc;
2140 psignal(p, SIGSYS);
2141 return (ENOSYS);
2142 }
2143
2144 static int
2145 build_corename(struct proc *p, char *dst, const char *src, size_t len)
2146 {
2147 const char *s;
2148 char *d, *end;
2149 int i;
2150
2151 for (s = src, d = dst, end = d + len; *s != '\0'; s++) {
2152 if (*s == '%') {
2153 switch (*(s + 1)) {
2154 case 'n':
2155 i = snprintf(d, end - d, "%s", p->p_comm);
2156 break;
2157 case 'p':
2158 i = snprintf(d, end - d, "%d", p->p_pid);
2159 break;
2160 case 'u':
2161 i = snprintf(d, end - d, "%.*s",
2162 (int)sizeof p->p_pgrp->pg_session->s_login,
2163 p->p_pgrp->pg_session->s_login);
2164 break;
2165 case 't':
2166 i = snprintf(d, end - d, "%ld",
2167 p->p_stats->p_start.tv_sec);
2168 break;
2169 default:
2170 goto copy;
2171 }
2172 d += i;
2173 s++;
2174 } else {
2175 copy: *d = *s;
2176 d++;
2177 }
2178 if (d >= end)
2179 return (ENAMETOOLONG);
2180 }
2181 *d = '\0';
2182 return 0;
2183 }
2184
2185 void
2186 getucontext(struct lwp *l, ucontext_t *ucp)
2187 {
2188 struct proc *p;
2189
2190 p = l->l_proc;
2191
2192 ucp->uc_flags = 0;
2193 ucp->uc_link = l->l_ctxlink;
2194
2195 (void)sigprocmask1(p, 0, NULL, &ucp->uc_sigmask);
2196 ucp->uc_flags |= _UC_SIGMASK;
2197
2198 /*
2199 * The (unsupplied) definition of the `current execution stack'
2200 * in the System V Interface Definition appears to allow returning
2201 * the main context stack.
2202 */
2203 if ((p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK) == 0) {
2204 ucp->uc_stack.ss_sp = (void *)USRSTACK;
2205 ucp->uc_stack.ss_size = ctob(p->p_vmspace->vm_ssize);
2206 ucp->uc_stack.ss_flags = 0; /* XXX, def. is Very Fishy */
2207 } else {
2208 /* Simply copy alternate signal execution stack. */
2209 ucp->uc_stack = p->p_sigctx.ps_sigstk;
2210 }
2211 ucp->uc_flags |= _UC_STACK;
2212
2213 cpu_getmcontext(l, &ucp->uc_mcontext, &ucp->uc_flags);
2214 }
2215
2216 /* ARGSUSED */
2217 int
2218 sys_getcontext(struct lwp *l, void *v, register_t *retval)
2219 {
2220 struct sys_getcontext_args /* {
2221 syscallarg(struct __ucontext *) ucp;
2222 } */ *uap = v;
2223 ucontext_t uc;
2224
2225 getucontext(l, &uc);
2226
2227 return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
2228 }
2229
2230 int
2231 setucontext(struct lwp *l, const ucontext_t *ucp)
2232 {
2233 struct proc *p;
2234 int error;
2235
2236 p = l->l_proc;
2237 if ((error = cpu_setmcontext(l, &ucp->uc_mcontext, ucp->uc_flags)) != 0)
2238 return (error);
2239 l->l_ctxlink = ucp->uc_link;
2240
2241 if ((ucp->uc_flags & _UC_SIGMASK) != 0)
2242 sigprocmask1(p, SIG_SETMASK, &ucp->uc_sigmask, NULL);
2243
2244 /*
2245 * If there was stack information, update whether or not we are
2246 * still running on an alternate signal stack.
2247 */
2248 if ((ucp->uc_flags & _UC_STACK) != 0) {
2249 if (ucp->uc_stack.ss_flags & SS_ONSTACK)
2250 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
2251 else
2252 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
2253 }
2254
2255 return 0;
2256 }
2257
2258 /* ARGSUSED */
2259 int
2260 sys_setcontext(struct lwp *l, void *v, register_t *retval)
2261 {
2262 struct sys_setcontext_args /* {
2263 syscallarg(const ucontext_t *) ucp;
2264 } */ *uap = v;
2265 ucontext_t uc;
2266 int error;
2267
2268 if (SCARG(uap, ucp) == NULL) /* i.e. end of uc_link chain */
2269 exit1(l, W_EXITCODE(0, 0));
2270 else if ((error = copyin(SCARG(uap, ucp), &uc, sizeof (uc))) != 0 ||
2271 (error = setucontext(l, &uc)) != 0)
2272 return (error);
2273
2274 return (EJUSTRETURN);
2275 }
2276
2277 /*
2278 * sigtimedwait(2) system call, used also for implementation
2279 * of sigwaitinfo() and sigwait().
2280 *
2281 * This only handles single LWP in signal wait. libpthread provides
2282 * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
2283 */
2284 int
2285 sys___sigtimedwait(struct lwp *l, void *v, register_t *retval)
2286 {
2287 struct sys___sigtimedwait_args /* {
2288 syscallarg(const sigset_t *) set;
2289 syscallarg(siginfo_t *) info;
2290 syscallarg(struct timespec *) timeout;
2291 } */ *uap = v;
2292 sigset_t *waitset, twaitset;
2293 struct proc *p = l->l_proc;
2294 int error, signum, s;
2295 int timo = 0;
2296 struct timeval tvstart;
2297 struct timespec ts;
2298 ksiginfo_t *ksi;
2299
2300 MALLOC(waitset, sigset_t *, sizeof(sigset_t), M_TEMP, M_WAITOK);
2301
2302 if ((error = copyin(SCARG(uap, set), waitset, sizeof(sigset_t))))
2303 goto free_waitset;
2304
2305 /*
2306 * Silently ignore SA_CANTMASK signals. psignal1() would
2307 * ignore SA_CANTMASK signals in waitset, we do this
2308 * only for the below siglist check.
2309 */
2310 sigminusset(&sigcantmask, waitset);
2311
2312 /*
2313 * First scan siglist and check if there is signal from
2314 * our waitset already pending.
2315 */
2316 twaitset = *waitset;
2317 __sigandset(&p->p_sigctx.ps_siglist, &twaitset);
2318 if ((signum = firstsig(&twaitset))) {
2319 /* found pending signal */
2320 sigdelset(&p->p_sigctx.ps_siglist, signum);
2321 ksi = ksiginfo_get(p, signum);
2322 if (!ksi) {
2323 /* No queued siginfo, manufacture one */
2324 ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
2325 KSI_INIT(ksi);
2326 ksi->ksi_info._signo = signum;
2327 ksi->ksi_info._code = SI_USER;
2328 }
2329
2330 goto sig;
2331 }
2332
2333 /*
2334 * Calculate timeout, if it was specified.
2335 */
2336 if (SCARG(uap, timeout)) {
2337 uint64_t ms;
2338
2339 if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))))
2340 goto free_waitset;
2341
2342 ms = (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
2343 timo = mstohz(ms);
2344 if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec > 0)
2345 timo = 1;
2346 if (timo <= 0) {
2347 error = EAGAIN;
2348 goto free_waitset;
2349 }
2350
2351 /*
2352 * Remember current mono_time, it would be used in
2353 * ECANCELED/ERESTART case.
2354 */
2355 s = splclock();
2356 tvstart = mono_time;
2357 splx(s);
2358 }
2359
2360 /*
2361 * Setup ps_sigwait list. Pass pointer to malloced memory
2362 * here; it's not possible to pass pointer to a structure
2363 * on current process's stack, the current process might
2364 * be swapped out at the time the signal would get delivered.
2365 */
2366 ksi = pool_get(&ksiginfo_pool, PR_WAITOK);
2367 p->p_sigctx.ps_sigwaited = ksi;
2368 p->p_sigctx.ps_sigwait = waitset;
2369
2370 /*
2371 * Wait for signal to arrive. We can either be woken up or
2372 * time out.
2373 */
2374 error = tsleep(&p->p_sigctx.ps_sigwait, PPAUSE|PCATCH, "sigwait", timo);
2375
2376 /*
2377 * Need to find out if we woke as a result of lwp_wakeup()
2378 * or a signal outside our wait set.
2379 */
2380 if (error == EINTR && p->p_sigctx.ps_sigwaited
2381 && !firstsig(&p->p_sigctx.ps_siglist)) {
2382 /* wakeup via _lwp_wakeup() */
2383 error = ECANCELED;
2384 } else if (!error && p->p_sigctx.ps_sigwaited) {
2385 /* spurious wakeup - arrange for syscall restart */
2386 error = ERESTART;
2387 goto fail;
2388 }
2389
2390 /*
2391 * On error, clear sigwait indication. psignal1() clears it
2392 * in !error case.
2393 */
2394 if (error) {
2395 p->p_sigctx.ps_sigwaited = NULL;
2396
2397 /*
2398 * If the sleep was interrupted (either by signal or wakeup),
2399 * update the timeout and copyout new value back.
2400 * It would be used when the syscall would be restarted
2401 * or called again.
2402 */
2403 if (timo && (error == ERESTART || error == ECANCELED)) {
2404 struct timeval tvnow, tvtimo;
2405 int err;
2406
2407 s = splclock();
2408 tvnow = mono_time;
2409 splx(s);
2410
2411 TIMESPEC_TO_TIMEVAL(&tvtimo, &ts);
2412
2413 /* compute how much time has passed since start */
2414 timersub(&tvnow, &tvstart, &tvnow);
2415 /* substract passed time from timeout */
2416 timersub(&tvtimo, &tvnow, &tvtimo);
2417
2418 if (tvtimo.tv_sec < 0) {
2419 error = EAGAIN;
2420 goto fail;
2421 }
2422
2423 TIMEVAL_TO_TIMESPEC(&tvtimo, &ts);
2424
2425 /* copy updated timeout to userland */
2426 if ((err = copyout(&ts, SCARG(uap, timeout), sizeof(ts)))) {
2427 error = err;
2428 goto fail;
2429 }
2430 }
2431
2432 goto fail;
2433 }
2434
2435 /*
2436 * If a signal from the wait set arrived, copy it to userland.
2437 * Copy only the used part of siginfo, the padding part is
2438 * left unchanged (userland is not supposed to touch it anyway).
2439 */
2440 sig:
2441 error = copyout(&ksi->ksi_info, SCARG(uap, info), sizeof(ksi->ksi_info));
2442
2443 fail:
2444 pool_put(&ksiginfo_pool, ksi);
2445 p->p_sigctx.ps_sigwait = NULL;
2446 free_waitset:
2447 FREE(waitset, M_TEMP);
2448
2449 return (error);
2450 }
2451
2452 /*
2453 * Returns true if signal is ignored or masked for passed process.
2454 */
2455 int
2456 sigismasked(struct proc *p, int sig)
2457 {
2458
2459 return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
2460 sigismember(&p->p_sigctx.ps_sigmask, sig));
2461 }
2462
2463 static int
2464 filt_sigattach(struct knote *kn)
2465 {
2466 struct proc *p = curproc;
2467
2468 kn->kn_ptr.p_proc = p;
2469 kn->kn_flags |= EV_CLEAR; /* automatically set */
2470
2471 SLIST_INSERT_HEAD(&p->p_klist, kn, kn_selnext);
2472
2473 return (0);
2474 }
2475
2476 static void
2477 filt_sigdetach(struct knote *kn)
2478 {
2479 struct proc *p = kn->kn_ptr.p_proc;
2480
2481 SLIST_REMOVE(&p->p_klist, kn, knote, kn_selnext);
2482 }
2483
2484 /*
2485 * signal knotes are shared with proc knotes, so we apply a mask to
2486 * the hint in order to differentiate them from process hints. This
2487 * could be avoided by using a signal-specific knote list, but probably
2488 * isn't worth the trouble.
2489 */
2490 static int
2491 filt_signal(struct knote *kn, long hint)
2492 {
2493
2494 if (hint & NOTE_SIGNAL) {
2495 hint &= ~NOTE_SIGNAL;
2496
2497 if (kn->kn_id == hint)
2498 kn->kn_data++;
2499 }
2500 return (kn->kn_data != 0);
2501 }
2502
2503 const struct filterops sig_filtops = {
2504 0, filt_sigattach, filt_sigdetach, filt_signal
2505 };
Cache object: 1436b2267458a46d7ce9372efa5a938a
|