kern_sig.c revision 1.123 1 /* $NetBSD: kern_sig.c,v 1.123 2002/08/25 21:47:50 thorpej 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)kern_sig.c 8.14 (Berkeley) 5/14/95
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.123 2002/08/25 21:47:50 thorpej Exp $");
45
46 #include "opt_ktrace.h"
47 #include "opt_compat_sunos.h"
48 #include "opt_compat_netbsd32.h"
49
50 #define SIGPROP /* include signal properties table */
51 #include <sys/param.h>
52 #include <sys/signalvar.h>
53 #include <sys/resourcevar.h>
54 #include <sys/namei.h>
55 #include <sys/vnode.h>
56 #include <sys/proc.h>
57 #include <sys/systm.h>
58 #include <sys/timeb.h>
59 #include <sys/times.h>
60 #include <sys/buf.h>
61 #include <sys/acct.h>
62 #include <sys/file.h>
63 #include <sys/kernel.h>
64 #include <sys/wait.h>
65 #include <sys/ktrace.h>
66 #include <sys/syslog.h>
67 #include <sys/stat.h>
68 #include <sys/core.h>
69 #include <sys/filedesc.h>
70 #include <sys/malloc.h>
71 #include <sys/pool.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_extern.h>
82
83 static void proc_stop(struct proc *p);
84 static int build_corename(struct proc *, char [MAXPATHLEN]);
85 sigset_t contsigmask, stopsigmask, sigcantmask;
86
87 struct pool sigacts_pool; /* memory pool for sigacts structures */
88
89 /*
90 * Can process p, with pcred pc, send the signal signum to process q?
91 */
92 #define CANSIGNAL(p, pc, q, signum) \
93 ((pc)->pc_ucred->cr_uid == 0 || \
94 (pc)->p_ruid == (q)->p_cred->p_ruid || \
95 (pc)->pc_ucred->cr_uid == (q)->p_cred->p_ruid || \
96 (pc)->p_ruid == (q)->p_ucred->cr_uid || \
97 (pc)->pc_ucred->cr_uid == (q)->p_ucred->cr_uid || \
98 ((signum) == SIGCONT && (q)->p_session == (p)->p_session))
99
100 /*
101 * Initialize signal-related data structures.
102 */
103 void
104 signal_init(void)
105 {
106
107 pool_init(&sigacts_pool, sizeof(struct sigacts), 0, 0, 0, "sigapl",
108 &pool_allocator_nointr);
109 }
110
111 /*
112 * Create an initial sigctx structure, using the same signal state
113 * as p. If 'share' is set, share the sigctx_proc part, otherwise just
114 * copy it from parent.
115 */
116 void
117 sigactsinit(struct proc *np, struct proc *pp, int share)
118 {
119 struct sigacts *ps;
120
121 if (share) {
122 np->p_sigacts = pp->p_sigacts;
123 pp->p_sigacts->sa_refcnt++;
124 } else {
125 ps = pool_get(&sigacts_pool, PR_WAITOK);
126 if (pp)
127 memcpy(ps, pp->p_sigacts, sizeof(struct sigacts));
128 else
129 memset(ps, '\0', sizeof(struct sigacts));
130 ps->sa_refcnt = 1;
131 np->p_sigacts = ps;
132 }
133 }
134
135 /*
136 * Make this process not share its sigctx, maintaining all
137 * signal state.
138 */
139 void
140 sigactsunshare(struct proc *p)
141 {
142 struct sigacts *oldps;
143
144 if (p->p_sigacts->sa_refcnt == 1)
145 return;
146
147 oldps = p->p_sigacts;
148 sigactsinit(p, NULL, 0);
149
150 if (--oldps->sa_refcnt == 0)
151 pool_put(&sigacts_pool, oldps);
152 }
153
154 /*
155 * Release a sigctx structure.
156 */
157 void
158 sigactsfree(struct proc *p)
159 {
160 struct sigacts *ps;
161
162 ps = p->p_sigacts;
163 if (--ps->sa_refcnt > 0)
164 return;
165
166 pool_put(&sigacts_pool, ps);
167 }
168
169 int
170 sigaction1(struct proc *p, int signum, const struct sigaction *nsa,
171 struct sigaction *osa, void *tramp, int vers)
172 {
173 struct sigacts *ps;
174 int prop;
175
176 ps = p->p_sigacts;
177 if (signum <= 0 || signum >= NSIG)
178 return (EINVAL);
179
180 /*
181 * Trampoline ABI version 0 is reserved for the legacy
182 * kernel-provided on-stack trampoline. Conversely, if
183 * we are using a non-0 ABI version, we must have a
184 * trampoline.
185 */
186 if ((vers != 0 && tramp == NULL) ||
187 (vers == 0 && tramp != NULL))
188 return (EINVAL);
189
190 if (osa)
191 *osa = SIGACTION_PS(ps, signum);
192
193 if (nsa) {
194 if (nsa->sa_flags & ~SA_ALLBITS)
195 return (EINVAL);
196
197 prop = sigprop[signum];
198 if (prop & SA_CANTMASK)
199 return (EINVAL);
200
201 (void) splsched(); /* XXXSMP */
202 SIGACTION_PS(ps, signum) = *nsa;
203 ps->sa_sigdesc[signum].sd_tramp = tramp;
204 ps->sa_sigdesc[signum].sd_vers = vers;
205 sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
206 if ((prop & SA_NORESET) != 0)
207 SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
208 if (signum == SIGCHLD) {
209 if (nsa->sa_flags & SA_NOCLDSTOP)
210 p->p_flag |= P_NOCLDSTOP;
211 else
212 p->p_flag &= ~P_NOCLDSTOP;
213 if (nsa->sa_flags & SA_NOCLDWAIT) {
214 /*
215 * Paranoia: since SA_NOCLDWAIT is implemented
216 * by reparenting the dying child to PID 1 (and
217 * trust it to reap the zombie), PID 1 itself
218 * is forbidden to set SA_NOCLDWAIT.
219 */
220 if (p->p_pid == 1)
221 p->p_flag &= ~P_NOCLDWAIT;
222 else
223 p->p_flag |= P_NOCLDWAIT;
224 } else
225 p->p_flag &= ~P_NOCLDWAIT;
226 }
227 if ((nsa->sa_flags & SA_NODEFER) == 0)
228 sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
229 else
230 sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
231 /*
232 * Set bit in p_sigctx.ps_sigignore for signals that are set to
233 * SIG_IGN, and for signals set to SIG_DFL where the default is
234 * to ignore. However, don't put SIGCONT in
235 * p_sigctx.ps_sigignore, as we have to restart the process.
236 */
237 if (nsa->sa_handler == SIG_IGN ||
238 (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
239 /* never to be seen again */
240 sigdelset(&p->p_sigctx.ps_siglist, signum);
241 if (signum != SIGCONT) {
242 /* easier in psignal */
243 sigaddset(&p->p_sigctx.ps_sigignore, signum);
244 }
245 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
246 } else {
247 sigdelset(&p->p_sigctx.ps_sigignore, signum);
248 if (nsa->sa_handler == SIG_DFL)
249 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
250 else
251 sigaddset(&p->p_sigctx.ps_sigcatch, signum);
252 }
253 (void) spl0();
254 }
255
256 return (0);
257 }
258
259 /* ARGSUSED */
260 int
261 sys___sigaction14(struct proc *p, void *v, register_t *retval)
262 {
263 struct sys___sigaction14_args /* {
264 syscallarg(int) signum;
265 syscallarg(const struct sigaction *) nsa;
266 syscallarg(struct sigaction *) osa;
267 } */ *uap = v;
268 struct sigaction nsa, osa;
269 int error;
270
271 if (SCARG(uap, nsa)) {
272 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
273 if (error)
274 return (error);
275 }
276 error = sigaction1(p, SCARG(uap, signum),
277 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
278 NULL, 0);
279 if (error)
280 return (error);
281 if (SCARG(uap, osa)) {
282 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
283 if (error)
284 return (error);
285 }
286 return (0);
287 }
288
289 /* ARGSUSED */
290 int
291 sys___sigaction_sigtramp(struct proc *p, void *v, register_t *retval)
292 {
293 struct sys___sigaction_sigtramp_args /* {
294 syscallarg(int) signum;
295 syscallarg(const struct sigaction *) nsa;
296 syscallarg(struct sigaction *) osa;
297 syscallarg(void *) tramp;
298 syscallarg(int) vers;
299 } */ *uap = v;
300 struct sigaction nsa, osa;
301 int error;
302
303 if (SCARG(uap, nsa)) {
304 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
305 if (error)
306 return (error);
307 }
308 error = sigaction1(p, SCARG(uap, signum),
309 SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
310 SCARG(uap, tramp), SCARG(uap, vers));
311 if (error)
312 return (error);
313 if (SCARG(uap, osa)) {
314 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
315 if (error)
316 return (error);
317 }
318 return (0);
319 }
320
321 /*
322 * Initialize signal state for process 0;
323 * set to ignore signals that are ignored by default and disable the signal
324 * stack.
325 */
326 void
327 siginit(struct proc *p)
328 {
329 struct sigacts *ps;
330 int signum, prop;
331
332 ps = p->p_sigacts;
333 sigemptyset(&contsigmask);
334 sigemptyset(&stopsigmask);
335 sigemptyset(&sigcantmask);
336 for (signum = 1; signum < NSIG; signum++) {
337 prop = sigprop[signum];
338 if (prop & SA_CONT)
339 sigaddset(&contsigmask, signum);
340 if (prop & SA_STOP)
341 sigaddset(&stopsigmask, signum);
342 if (prop & SA_CANTMASK)
343 sigaddset(&sigcantmask, signum);
344 if (prop & SA_IGNORE && signum != SIGCONT)
345 sigaddset(&p->p_sigctx.ps_sigignore, signum);
346 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
347 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
348 }
349 sigemptyset(&p->p_sigctx.ps_sigcatch);
350 p->p_flag &= ~P_NOCLDSTOP;
351
352 /*
353 * Reset stack state to the user stack.
354 */
355 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
356 p->p_sigctx.ps_sigstk.ss_size = 0;
357 p->p_sigctx.ps_sigstk.ss_sp = 0;
358
359 /* One reference. */
360 ps->sa_refcnt = 1;
361 }
362
363 /*
364 * Reset signals for an exec of the specified process.
365 */
366 void
367 execsigs(struct proc *p)
368 {
369 struct sigacts *ps;
370 int signum, prop;
371
372 sigactsunshare(p);
373
374 ps = p->p_sigacts;
375
376 /*
377 * Reset caught signals. Held signals remain held
378 * through p_sigctx.ps_sigmask (unless they were caught,
379 * and are now ignored by default).
380 */
381 for (signum = 1; signum < NSIG; signum++) {
382 if (sigismember(&p->p_sigctx.ps_sigcatch, signum)) {
383 prop = sigprop[signum];
384 if (prop & SA_IGNORE) {
385 if ((prop & SA_CONT) == 0)
386 sigaddset(&p->p_sigctx.ps_sigignore,
387 signum);
388 sigdelset(&p->p_sigctx.ps_siglist, signum);
389 }
390 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
391 }
392 sigemptyset(&SIGACTION_PS(ps, signum).sa_mask);
393 SIGACTION_PS(ps, signum).sa_flags = SA_RESTART;
394 }
395 sigemptyset(&p->p_sigctx.ps_sigcatch);
396 p->p_flag &= ~P_NOCLDSTOP;
397
398 /*
399 * Reset stack state to the user stack.
400 */
401 p->p_sigctx.ps_sigstk.ss_flags = SS_DISABLE;
402 p->p_sigctx.ps_sigstk.ss_size = 0;
403 p->p_sigctx.ps_sigstk.ss_sp = 0;
404 }
405
406 int
407 sigprocmask1(struct proc *p, int how, const sigset_t *nss, sigset_t *oss)
408 {
409
410 if (oss)
411 *oss = p->p_sigctx.ps_sigmask;
412
413 if (nss) {
414 (void)splsched(); /* XXXSMP */
415 switch (how) {
416 case SIG_BLOCK:
417 sigplusset(nss, &p->p_sigctx.ps_sigmask);
418 break;
419 case SIG_UNBLOCK:
420 sigminusset(nss, &p->p_sigctx.ps_sigmask);
421 CHECKSIGS(p);
422 break;
423 case SIG_SETMASK:
424 p->p_sigctx.ps_sigmask = *nss;
425 CHECKSIGS(p);
426 break;
427 default:
428 (void)spl0(); /* XXXSMP */
429 return (EINVAL);
430 }
431 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
432 (void)spl0(); /* XXXSMP */
433 }
434
435 return (0);
436 }
437
438 /*
439 * Manipulate signal mask.
440 * Note that we receive new mask, not pointer,
441 * and return old mask as return value;
442 * the library stub does the rest.
443 */
444 int
445 sys___sigprocmask14(struct proc *p, void *v, register_t *retval)
446 {
447 struct sys___sigprocmask14_args /* {
448 syscallarg(int) how;
449 syscallarg(const sigset_t *) set;
450 syscallarg(sigset_t *) oset;
451 } */ *uap = v;
452 sigset_t nss, oss;
453 int error;
454
455 if (SCARG(uap, set)) {
456 error = copyin(SCARG(uap, set), &nss, sizeof(nss));
457 if (error)
458 return (error);
459 }
460 error = sigprocmask1(p, SCARG(uap, how),
461 SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
462 if (error)
463 return (error);
464 if (SCARG(uap, oset)) {
465 error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
466 if (error)
467 return (error);
468 }
469 return (0);
470 }
471
472 void
473 sigpending1(struct proc *p, sigset_t *ss)
474 {
475
476 *ss = p->p_sigctx.ps_siglist;
477 sigminusset(&p->p_sigctx.ps_sigmask, ss);
478 }
479
480 /* ARGSUSED */
481 int
482 sys___sigpending14(struct proc *p, void *v, register_t *retval)
483 {
484 struct sys___sigpending14_args /* {
485 syscallarg(sigset_t *) set;
486 } */ *uap = v;
487 sigset_t ss;
488
489 sigpending1(p, &ss);
490 return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
491 }
492
493 int
494 sigsuspend1(struct proc *p, const sigset_t *ss)
495 {
496 struct sigacts *ps;
497
498 ps = p->p_sigacts;
499 if (ss) {
500 /*
501 * When returning from sigpause, we want
502 * the old mask to be restored after the
503 * signal handler has finished. Thus, we
504 * save it here and mark the sigctx structure
505 * to indicate this.
506 */
507 p->p_sigctx.ps_oldmask = p->p_sigctx.ps_sigmask;
508 p->p_sigctx.ps_flags |= SAS_OLDMASK;
509 (void) splsched(); /* XXXSMP */
510 p->p_sigctx.ps_sigmask = *ss;
511 CHECKSIGS(p);
512 sigminusset(&sigcantmask, &p->p_sigctx.ps_sigmask);
513 (void) spl0(); /* XXXSMP */
514 }
515
516 while (tsleep((caddr_t) ps, PPAUSE|PCATCH, "pause", 0) == 0)
517 /* void */;
518 /* always return EINTR rather than ERESTART... */
519 return (EINTR);
520 }
521
522 /*
523 * Suspend process until signal, providing mask to be set
524 * in the meantime. Note nonstandard calling convention:
525 * libc stub passes mask, not pointer, to save a copyin.
526 */
527 /* ARGSUSED */
528 int
529 sys___sigsuspend14(struct proc *p, void *v, register_t *retval)
530 {
531 struct sys___sigsuspend14_args /* {
532 syscallarg(const sigset_t *) set;
533 } */ *uap = v;
534 sigset_t ss;
535 int error;
536
537 if (SCARG(uap, set)) {
538 error = copyin(SCARG(uap, set), &ss, sizeof(ss));
539 if (error)
540 return (error);
541 }
542
543 return (sigsuspend1(p, SCARG(uap, set) ? &ss : 0));
544 }
545
546 int
547 sigaltstack1(struct proc *p, const struct sigaltstack *nss,
548 struct sigaltstack *oss)
549 {
550
551 if (oss)
552 *oss = p->p_sigctx.ps_sigstk;
553
554 if (nss) {
555 if (nss->ss_flags & ~SS_ALLBITS)
556 return (EINVAL);
557
558 if (nss->ss_flags & SS_DISABLE) {
559 if (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
560 return (EINVAL);
561 } else {
562 if (nss->ss_size < MINSIGSTKSZ)
563 return (ENOMEM);
564 }
565 p->p_sigctx.ps_sigstk = *nss;
566 }
567
568 return (0);
569 }
570
571 /* ARGSUSED */
572 int
573 sys___sigaltstack14(struct proc *p, void *v, register_t *retval)
574 {
575 struct sys___sigaltstack14_args /* {
576 syscallarg(const struct sigaltstack *) nss;
577 syscallarg(struct sigaltstack *) oss;
578 } */ *uap = v;
579 struct sigaltstack nss, oss;
580 int error;
581
582 if (SCARG(uap, nss)) {
583 error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
584 if (error)
585 return (error);
586 }
587 error = sigaltstack1(p,
588 SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
589 if (error)
590 return (error);
591 if (SCARG(uap, oss)) {
592 error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
593 if (error)
594 return (error);
595 }
596 return (0);
597 }
598
599 /* ARGSUSED */
600 int
601 sys_kill(struct proc *cp, void *v, register_t *retval)
602 {
603 struct sys_kill_args /* {
604 syscallarg(int) pid;
605 syscallarg(int) signum;
606 } */ *uap = v;
607 struct proc *p;
608 struct pcred *pc;
609
610 pc = cp->p_cred;
611 if ((u_int)SCARG(uap, signum) >= NSIG)
612 return (EINVAL);
613 if (SCARG(uap, pid) > 0) {
614 /* kill single process */
615 if ((p = pfind(SCARG(uap, pid))) == NULL)
616 return (ESRCH);
617 if (!CANSIGNAL(cp, pc, p, SCARG(uap, signum)))
618 return (EPERM);
619 if (SCARG(uap, signum))
620 psignal(p, SCARG(uap, signum));
621 return (0);
622 }
623 switch (SCARG(uap, pid)) {
624 case -1: /* broadcast signal */
625 return (killpg1(cp, SCARG(uap, signum), 0, 1));
626 case 0: /* signal own process group */
627 return (killpg1(cp, SCARG(uap, signum), 0, 0));
628 default: /* negative explicit process group */
629 return (killpg1(cp, SCARG(uap, signum), -SCARG(uap, pid), 0));
630 }
631 /* NOTREACHED */
632 }
633
634 /*
635 * Common code for kill process group/broadcast kill.
636 * cp is calling process.
637 */
638 int
639 killpg1(struct proc *cp, int signum, int pgid, int all)
640 {
641 struct proc *p;
642 struct pcred *pc;
643 struct pgrp *pgrp;
644 int nfound;
645
646 pc = cp->p_cred;
647 nfound = 0;
648 if (all) {
649 /*
650 * broadcast
651 */
652 proclist_lock_read();
653 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
654 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
655 p == cp || !CANSIGNAL(cp, pc, p, signum))
656 continue;
657 nfound++;
658 if (signum)
659 psignal(p, signum);
660 }
661 proclist_unlock_read();
662 } else {
663 if (pgid == 0)
664 /*
665 * zero pgid means send to my process group.
666 */
667 pgrp = cp->p_pgrp;
668 else {
669 pgrp = pgfind(pgid);
670 if (pgrp == NULL)
671 return (ESRCH);
672 }
673 for (p = pgrp->pg_members.lh_first;
674 p != 0;
675 p = p->p_pglist.le_next) {
676 if (p->p_pid <= 1 || p->p_flag & P_SYSTEM ||
677 !CANSIGNAL(cp, pc, p, signum))
678 continue;
679 nfound++;
680 if (signum && P_ZOMBIE(p) == 0)
681 psignal(p, signum);
682 }
683 }
684 return (nfound ? 0 : ESRCH);
685 }
686
687 /*
688 * Send a signal to a process group.
689 */
690 void
691 gsignal(int pgid, int signum)
692 {
693 struct pgrp *pgrp;
694
695 if (pgid && (pgrp = pgfind(pgid)))
696 pgsignal(pgrp, signum, 0);
697 }
698
699 /*
700 * Send a signal to a process group. If checktty is 1,
701 * limit to members which have a controlling terminal.
702 */
703 void
704 pgsignal(struct pgrp *pgrp, int signum, int checkctty)
705 {
706 struct proc *p;
707
708 if (pgrp)
709 for (p = pgrp->pg_members.lh_first; p != 0;
710 p = p->p_pglist.le_next)
711 if (checkctty == 0 || p->p_flag & P_CONTROLT)
712 psignal(p, signum);
713 }
714
715 /*
716 * Send a signal caused by a trap to the current process.
717 * If it will be caught immediately, deliver it with correct code.
718 * Otherwise, post it normally.
719 */
720 void
721 trapsignal(struct proc *p, int signum, u_long code)
722 {
723 struct sigacts *ps;
724
725 ps = p->p_sigacts;
726 if ((p->p_flag & P_TRACED) == 0 &&
727 sigismember(&p->p_sigctx.ps_sigcatch, signum) &&
728 !sigismember(&p->p_sigctx.ps_sigmask, signum)) {
729 p->p_stats->p_ru.ru_nsignals++;
730 #ifdef KTRACE
731 if (KTRPOINT(p, KTR_PSIG))
732 ktrpsig(p, signum,
733 SIGACTION_PS(ps, signum).sa_handler,
734 &p->p_sigctx.ps_sigmask, code);
735 #endif
736 (*p->p_emul->e_sendsig)(signum, &p->p_sigctx.ps_sigmask,
737 code);
738 (void) splsched(); /* XXXSMP */
739 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
740 &p->p_sigctx.ps_sigmask);
741 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
742 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
743 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
744 sigaddset(&p->p_sigctx.ps_sigignore, signum);
745 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
746 }
747 (void) spl0(); /* XXXSMP */
748 } else {
749 p->p_sigctx.ps_code = code; /* XXX for core dump/debugger */
750 p->p_sigctx.ps_sig = signum; /* XXX to verify code */
751 psignal(p, signum);
752 }
753 }
754
755 /*
756 * Send the signal to the process. If the signal has an action, the action
757 * is usually performed by the target process rather than the caller; we add
758 * the signal to the set of pending signals for the process.
759 *
760 * Exceptions:
761 * o When a stop signal is sent to a sleeping process that takes the
762 * default action, the process is stopped without awakening it.
763 * o SIGCONT restarts stopped processes (or puts them back to sleep)
764 * regardless of the signal action (eg, blocked or ignored).
765 *
766 * Other ignored signals are discarded immediately.
767 *
768 * XXXSMP: Invoked as psignal() or sched_psignal().
769 */
770 void
771 psignal1(struct proc *p, int signum,
772 int dolock) /* XXXSMP: works, but icky */
773 {
774 int s, prop;
775 sig_t action;
776
777 #ifdef DIAGNOSTIC
778 if (signum <= 0 || signum >= NSIG)
779 panic("psignal signal number");
780
781 /* XXXSMP: works, but icky */
782 if (dolock)
783 SCHED_ASSERT_UNLOCKED();
784 else
785 SCHED_ASSERT_LOCKED();
786 #endif
787 prop = sigprop[signum];
788
789 /*
790 * If proc is traced, always give parent a chance.
791 */
792 if (p->p_flag & P_TRACED)
793 action = SIG_DFL;
794 else {
795 /*
796 * If the signal is being ignored,
797 * then we forget about it immediately.
798 * (Note: we don't set SIGCONT in p_sigctx.ps_sigignore,
799 * and if it is set to SIG_IGN,
800 * action will be SIG_DFL here.)
801 */
802 if (sigismember(&p->p_sigctx.ps_sigignore, signum))
803 return;
804 if (sigismember(&p->p_sigctx.ps_sigmask, signum))
805 action = SIG_HOLD;
806 else if (sigismember(&p->p_sigctx.ps_sigcatch, signum))
807 action = SIG_CATCH;
808 else {
809 action = SIG_DFL;
810
811 if (prop & SA_KILL && p->p_nice > NZERO)
812 p->p_nice = NZERO;
813
814 /*
815 * If sending a tty stop signal to a member of an
816 * orphaned process group, discard the signal here if
817 * the action is default; don't stop the process below
818 * if sleeping, and don't clear any pending SIGCONT.
819 */
820 if (prop & SA_TTYSTOP && p->p_pgrp->pg_jobc == 0)
821 return;
822 }
823 }
824
825 if (prop & SA_CONT)
826 sigminusset(&stopsigmask, &p->p_sigctx.ps_siglist);
827
828 if (prop & SA_STOP)
829 sigminusset(&contsigmask, &p->p_sigctx.ps_siglist);
830
831 sigaddset(&p->p_sigctx.ps_siglist, signum);
832
833 /* CHECKSIGS() is "inlined" here. */
834 p->p_sigctx.ps_sigcheck = 1;
835
836 /*
837 * Defer further processing for signals which are held,
838 * except that stopped processes must be continued by SIGCONT.
839 */
840 if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
841 return;
842
843 /* XXXSMP: works, but icky */
844 if (dolock)
845 SCHED_LOCK(s);
846
847 switch (p->p_stat) {
848 case SSLEEP:
849 /*
850 * If process is sleeping uninterruptibly
851 * we can't interrupt the sleep... the signal will
852 * be noticed when the process returns through
853 * trap() or syscall().
854 */
855 if ((p->p_flag & P_SINTR) == 0)
856 goto out;
857 /*
858 * Process is sleeping and traced... make it runnable
859 * so it can discover the signal in issignal() and stop
860 * for the parent.
861 */
862 if (p->p_flag & P_TRACED)
863 goto run;
864 /*
865 * If SIGCONT is default (or ignored) and process is
866 * asleep, we are finished; the process should not
867 * be awakened.
868 */
869 if ((prop & SA_CONT) && action == SIG_DFL) {
870 sigdelset(&p->p_sigctx.ps_siglist, signum);
871 goto out;
872 }
873 /*
874 * When a sleeping process receives a stop
875 * signal, process immediately if possible.
876 */
877 if ((prop & SA_STOP) && action == SIG_DFL) {
878 /*
879 * If a child holding parent blocked,
880 * stopping could cause deadlock.
881 */
882 if (p->p_flag & P_PPWAIT)
883 goto out;
884 sigdelset(&p->p_sigctx.ps_siglist, signum);
885 p->p_xstat = signum;
886 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0) {
887 /*
888 * XXXSMP: recursive call; don't lock
889 * the second time around.
890 */
891 sched_psignal(p->p_pptr, SIGCHLD);
892 }
893 proc_stop(p); /* XXXSMP: recurse? */
894 goto out;
895 }
896 /*
897 * All other (caught or default) signals
898 * cause the process to run.
899 */
900 goto runfast;
901 /*NOTREACHED*/
902
903 case SSTOP:
904 /*
905 * If traced process is already stopped,
906 * then no further action is necessary.
907 */
908 if (p->p_flag & P_TRACED)
909 goto out;
910
911 /*
912 * Kill signal always sets processes running.
913 */
914 if (signum == SIGKILL)
915 goto runfast;
916
917 if (prop & SA_CONT) {
918 /*
919 * If SIGCONT is default (or ignored), we continue the
920 * process but don't leave the signal in p_sigctx.ps_siglist, as
921 * it has no further action. If SIGCONT is held, we
922 * continue the process and leave the signal in
923 * p_sigctx.ps_siglist. If the process catches SIGCONT, let it
924 * handle the signal itself. If it isn't waiting on
925 * an event, then it goes back to run state.
926 * Otherwise, process goes back to sleep state.
927 */
928 if (action == SIG_DFL)
929 sigdelset(&p->p_sigctx.ps_siglist, signum);
930 if (action == SIG_CATCH)
931 goto runfast;
932 if (p->p_wchan == 0)
933 goto run;
934 p->p_stat = SSLEEP;
935 goto out;
936 }
937
938 if (prop & SA_STOP) {
939 /*
940 * Already stopped, don't need to stop again.
941 * (If we did the shell could get confused.)
942 */
943 sigdelset(&p->p_sigctx.ps_siglist, signum);
944 goto out;
945 }
946
947 /*
948 * If process is sleeping interruptibly, then simulate a
949 * wakeup so that when it is continued, it will be made
950 * runnable and can look at the signal. But don't make
951 * the process runnable, leave it stopped.
952 */
953 if (p->p_wchan && p->p_flag & P_SINTR)
954 unsleep(p);
955 goto out;
956 #ifdef __HAVE_AST_PERPROC
957 case SONPROC:
958 case SRUN:
959 case SIDL:
960 /*
961 * SONPROC: We're running, notice the signal when
962 * we return back to userspace.
963 *
964 * SRUN, SIDL: Notice the signal when we run again
965 * and return to back to userspace.
966 */
967 signotify(p);
968 goto out;
969
970 default:
971 /*
972 * SDEAD, SZOMB: The signal will never be noticed.
973 */
974 goto out;
975 #else /* ! __HAVE_AST_PERPROC */
976 case SONPROC:
977 /*
978 * We're running; notice the signal.
979 */
980 signotify(p);
981 goto out;
982
983 default:
984 /*
985 * SRUN, SIDL, SDEAD, SZOMB do nothing with the signal.
986 * It will either never be noticed, or noticed very soon.
987 */
988 goto out;
989 #endif /* __HAVE_AST_PERPROC */
990 }
991 /*NOTREACHED*/
992
993 runfast:
994 /*
995 * Raise priority to at least PUSER.
996 */
997 if (p->p_priority > PUSER)
998 p->p_priority = PUSER;
999 run:
1000 setrunnable(p); /* XXXSMP: recurse? */
1001 out:
1002 /* XXXSMP: works, but icky */
1003 if (dolock)
1004 SCHED_UNLOCK(s);
1005 }
1006
1007 static __inline int firstsig(const sigset_t *);
1008
1009 static __inline int
1010 firstsig(const sigset_t *ss)
1011 {
1012 int sig;
1013
1014 sig = ffs(ss->__bits[0]);
1015 if (sig != 0)
1016 return (sig);
1017 #if NSIG > 33
1018 sig = ffs(ss->__bits[1]);
1019 if (sig != 0)
1020 return (sig + 32);
1021 #endif
1022 #if NSIG > 65
1023 sig = ffs(ss->__bits[2]);
1024 if (sig != 0)
1025 return (sig + 64);
1026 #endif
1027 #if NSIG > 97
1028 sig = ffs(ss->__bits[3]);
1029 if (sig != 0)
1030 return (sig + 96);
1031 #endif
1032 return (0);
1033 }
1034
1035 /*
1036 * If the current process has received a signal (should be caught or cause
1037 * termination, should interrupt current syscall), return the signal number.
1038 * Stop signals with default action are processed immediately, then cleared;
1039 * they aren't returned. This is checked after each entry to the system for
1040 * a syscall or trap (though this can usually be done without calling issignal
1041 * by checking the pending signal masks in the CURSIG macro.) The normal call
1042 * sequence is
1043 *
1044 * while (signum = CURSIG(curproc))
1045 * postsig(signum);
1046 */
1047 int
1048 issignal(struct proc *p)
1049 {
1050 int s, signum, prop;
1051 int dolock = (p->p_flag & P_SINTR) == 0, locked = !dolock;
1052 sigset_t ss;
1053
1054 for (;;) {
1055 sigpending1(p, &ss);
1056 if (p->p_flag & P_PPWAIT)
1057 sigminusset(&stopsigmask, &ss);
1058 signum = firstsig(&ss);
1059 if (signum == 0) { /* no signal to send */
1060 p->p_sigctx.ps_sigcheck = 0;
1061 if (locked && dolock)
1062 SCHED_LOCK(s);
1063 return (0);
1064 }
1065 /* take the signal! */
1066 sigdelset(&p->p_sigctx.ps_siglist, signum);
1067
1068 /*
1069 * We should see pending but ignored signals
1070 * only if P_TRACED was on when they were posted.
1071 */
1072 if (sigismember(&p->p_sigctx.ps_sigignore, signum) &&
1073 (p->p_flag & P_TRACED) == 0)
1074 continue;
1075
1076 if (p->p_flag & P_TRACED && (p->p_flag & P_PPWAIT) == 0) {
1077 /*
1078 * If traced, always stop, and stay
1079 * stopped until released by the debugger.
1080 */
1081 p->p_xstat = signum;
1082 if ((p->p_flag & P_FSTRACE) == 0)
1083 psignal1(p->p_pptr, SIGCHLD, dolock);
1084 if (dolock)
1085 SCHED_LOCK(s);
1086 proc_stop(p);
1087 mi_switch(p);
1088 SCHED_ASSERT_UNLOCKED();
1089 if (dolock)
1090 splx(s);
1091 else
1092 dolock = 1;
1093
1094 /*
1095 * If we are no longer being traced, or the parent
1096 * didn't give us a signal, look for more signals.
1097 */
1098 if ((p->p_flag & P_TRACED) == 0 || p->p_xstat == 0)
1099 continue;
1100
1101 /*
1102 * If the new signal is being masked, look for other
1103 * signals.
1104 */
1105 signum = p->p_xstat;
1106 /*
1107 * `p->p_sigctx.ps_siglist |= mask' is done
1108 * in setrunnable().
1109 */
1110 if (sigismember(&p->p_sigctx.ps_sigmask, signum))
1111 continue;
1112 /* take the signal! */
1113 sigdelset(&p->p_sigctx.ps_siglist, signum);
1114 }
1115
1116 prop = sigprop[signum];
1117
1118 /*
1119 * Decide whether the signal should be returned.
1120 * Return the signal's number, or fall through
1121 * to clear it from the pending mask.
1122 */
1123 switch ((long)SIGACTION(p, signum).sa_handler) {
1124
1125 case (long)SIG_DFL:
1126 /*
1127 * Don't take default actions on system processes.
1128 */
1129 if (p->p_pid <= 1) {
1130 #ifdef DIAGNOSTIC
1131 /*
1132 * Are you sure you want to ignore SIGSEGV
1133 * in init? XXX
1134 */
1135 printf("Process (pid %d) got signal %d\n",
1136 p->p_pid, signum);
1137 #endif
1138 break; /* == ignore */
1139 }
1140 /*
1141 * If there is a pending stop signal to process
1142 * with default action, stop here,
1143 * then clear the signal. However,
1144 * if process is member of an orphaned
1145 * process group, ignore tty stop signals.
1146 */
1147 if (prop & SA_STOP) {
1148 if (p->p_flag & P_TRACED ||
1149 (p->p_pgrp->pg_jobc == 0 &&
1150 prop & SA_TTYSTOP))
1151 break; /* == ignore */
1152 p->p_xstat = signum;
1153 if ((p->p_pptr->p_flag & P_NOCLDSTOP) == 0)
1154 psignal1(p->p_pptr, SIGCHLD, dolock);
1155 if (dolock)
1156 SCHED_LOCK(s);
1157 proc_stop(p);
1158 mi_switch(p);
1159 SCHED_ASSERT_UNLOCKED();
1160 if (dolock)
1161 splx(s);
1162 else
1163 dolock = 1;
1164 break;
1165 } else if (prop & SA_IGNORE) {
1166 /*
1167 * Except for SIGCONT, shouldn't get here.
1168 * Default action is to ignore; drop it.
1169 */
1170 break; /* == ignore */
1171 } else
1172 goto keep;
1173 /*NOTREACHED*/
1174
1175 case (long)SIG_IGN:
1176 /*
1177 * Masking above should prevent us ever trying
1178 * to take action on an ignored signal other
1179 * than SIGCONT, unless process is traced.
1180 */
1181 if ((prop & SA_CONT) == 0 &&
1182 (p->p_flag & P_TRACED) == 0)
1183 printf("issignal\n");
1184 break; /* == ignore */
1185
1186 default:
1187 /*
1188 * This signal has an action, let
1189 * postsig() process it.
1190 */
1191 goto keep;
1192 }
1193 }
1194 /* NOTREACHED */
1195
1196 keep:
1197 /* leave the signal for later */
1198 sigaddset(&p->p_sigctx.ps_siglist, signum);
1199 CHECKSIGS(p);
1200 if (locked && dolock)
1201 SCHED_LOCK(s);
1202 return (signum);
1203 }
1204
1205 /*
1206 * Put the argument process into the stopped state and notify the parent
1207 * via wakeup. Signals are handled elsewhere. The process must not be
1208 * on the run queue.
1209 */
1210 static void
1211 proc_stop(struct proc *p)
1212 {
1213
1214 SCHED_ASSERT_LOCKED();
1215
1216 p->p_stat = SSTOP;
1217 p->p_flag &= ~P_WAITED;
1218 sched_wakeup((caddr_t)p->p_pptr);
1219 }
1220
1221 /*
1222 * Take the action for the specified signal
1223 * from the current set of pending signals.
1224 */
1225 void
1226 postsig(int signum)
1227 {
1228 struct proc *p;
1229 struct sigacts *ps;
1230 sig_t action;
1231 u_long code;
1232 sigset_t *returnmask;
1233
1234 p = curproc;
1235 ps = p->p_sigacts;
1236 #ifdef DIAGNOSTIC
1237 if (signum == 0)
1238 panic("postsig");
1239 #endif
1240
1241 KERNEL_PROC_LOCK(p);
1242
1243 sigdelset(&p->p_sigctx.ps_siglist, signum);
1244 action = SIGACTION_PS(ps, signum).sa_handler;
1245 #ifdef KTRACE
1246 if (KTRPOINT(p, KTR_PSIG))
1247 ktrpsig(p,
1248 signum, action, p->p_sigctx.ps_flags & SAS_OLDMASK ?
1249 &p->p_sigctx.ps_oldmask : &p->p_sigctx.ps_sigmask, 0);
1250 #endif
1251 if (action == SIG_DFL) {
1252 /*
1253 * Default action, where the default is to kill
1254 * the process. (Other cases were ignored above.)
1255 */
1256 sigexit(p, signum);
1257 /* NOTREACHED */
1258 } else {
1259 /*
1260 * If we get here, the signal must be caught.
1261 */
1262 #ifdef DIAGNOSTIC
1263 if (action == SIG_IGN ||
1264 sigismember(&p->p_sigctx.ps_sigmask, signum))
1265 panic("postsig action");
1266 #endif
1267 /*
1268 * Set the new mask value and also defer further
1269 * occurences of this signal.
1270 *
1271 * Special case: user has done a sigpause. Here the
1272 * current mask is not of interest, but rather the
1273 * mask from before the sigpause is what we want
1274 * restored after the signal processing is completed.
1275 */
1276 if (p->p_sigctx.ps_flags & SAS_OLDMASK) {
1277 returnmask = &p->p_sigctx.ps_oldmask;
1278 p->p_sigctx.ps_flags &= ~SAS_OLDMASK;
1279 } else
1280 returnmask = &p->p_sigctx.ps_sigmask;
1281 p->p_stats->p_ru.ru_nsignals++;
1282 if (p->p_sigctx.ps_sig != signum) {
1283 code = 0;
1284 } else {
1285 code = p->p_sigctx.ps_code;
1286 p->p_sigctx.ps_code = 0;
1287 p->p_sigctx.ps_sig = 0;
1288 }
1289 (*p->p_emul->e_sendsig)(signum, returnmask, code);
1290 (void) splsched(); /* XXXSMP */
1291 sigplusset(&SIGACTION_PS(ps, signum).sa_mask,
1292 &p->p_sigctx.ps_sigmask);
1293 if (SIGACTION_PS(ps, signum).sa_flags & SA_RESETHAND) {
1294 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
1295 if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
1296 sigaddset(&p->p_sigctx.ps_sigignore, signum);
1297 SIGACTION_PS(ps, signum).sa_handler = SIG_DFL;
1298 }
1299 (void) spl0(); /* XXXSMP */
1300 }
1301
1302 KERNEL_PROC_UNLOCK(p);
1303 }
1304
1305 /*
1306 * Kill the current process for stated reason.
1307 */
1308 void
1309 killproc(struct proc *p, const char *why)
1310 {
1311
1312 log(LOG_ERR, "pid %d was killed: %s\n", p->p_pid, why);
1313 uprintf("sorry, pid %d was killed: %s\n", p->p_pid, why);
1314 psignal(p, SIGKILL);
1315 }
1316
1317 /*
1318 * Force the current process to exit with the specified signal, dumping core
1319 * if appropriate. We bypass the normal tests for masked and caught signals,
1320 * allowing unrecoverable failures to terminate the process without changing
1321 * signal state. Mark the accounting record with the signal termination.
1322 * If dumping core, save the signal number for the debugger. Calls exit and
1323 * does not return.
1324 */
1325
1326 #if defined(DEBUG)
1327 int kern_logsigexit = 1; /* not static to make public for sysctl */
1328 #else
1329 int kern_logsigexit = 0; /* not static to make public for sysctl */
1330 #endif
1331
1332 static const char logcoredump[] =
1333 "pid %d (%s), uid %d: exited on signal %d (core dumped)\n";
1334 static const char lognocoredump[] =
1335 "pid %d (%s), uid %d: exited on signal %d (core not dumped, err = %d)\n";
1336
1337 void
1338 sigexit(struct proc *p, int signum)
1339 {
1340 int error, exitsig;
1341
1342 exitsig = signum;
1343 p->p_acflag |= AXSIG;
1344 if (sigprop[signum] & SA_CORE) {
1345 p->p_sigctx.ps_sig = signum;
1346 if ((error = coredump(p)) == 0)
1347 exitsig |= WCOREFLAG;
1348
1349 if (kern_logsigexit) {
1350 /* XXX What if we ever have really large UIDs? */
1351 int uid = p->p_cred && p->p_ucred ?
1352 (int) p->p_ucred->cr_uid : -1;
1353
1354 if (error)
1355 log(LOG_INFO, lognocoredump, p->p_pid,
1356 p->p_comm, uid, signum, error);
1357 else
1358 log(LOG_INFO, logcoredump, p->p_pid,
1359 p->p_comm, uid, signum);
1360 }
1361
1362 }
1363
1364 exit1(p, W_EXITCODE(0, exitsig));
1365 /* NOTREACHED */
1366 }
1367
1368 /*
1369 * Dump core, into a file named "progname.core" or "core" (depending on the
1370 * value of shortcorename), unless the process was setuid/setgid.
1371 */
1372 int
1373 coredump(struct proc *p)
1374 {
1375 struct vnode *vp;
1376 struct vmspace *vm;
1377 struct ucred *cred;
1378 struct nameidata nd;
1379 struct vattr vattr;
1380 int error, error1;
1381 char name[MAXPATHLEN];
1382
1383 vm = p->p_vmspace;
1384 cred = p->p_cred->pc_ucred;
1385
1386 /*
1387 * Make sure the process has not set-id, to prevent data leaks.
1388 */
1389 if (p->p_flag & P_SUGID)
1390 return (EPERM);
1391
1392 /*
1393 * Refuse to core if the data + stack + user size is larger than
1394 * the core dump limit. XXX THIS IS WRONG, because of mapped
1395 * data.
1396 */
1397 if (USPACE + ctob(vm->vm_dsize + vm->vm_ssize) >=
1398 p->p_rlimit[RLIMIT_CORE].rlim_cur)
1399 return (EFBIG); /* better error code? */
1400
1401 /*
1402 * The core dump will go in the current working directory. Make
1403 * sure that the directory is still there and that the mount flags
1404 * allow us to write core dumps there.
1405 */
1406 vp = p->p_cwdi->cwdi_cdir;
1407 if (vp->v_mount == NULL ||
1408 (vp->v_mount->mnt_flag & MNT_NOCOREDUMP) != 0)
1409 return (EPERM);
1410
1411 error = build_corename(p, name);
1412 if (error)
1413 return error;
1414
1415 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1416 error = vn_open(&nd, O_CREAT | FWRITE | FNOSYMLINK, S_IRUSR | S_IWUSR);
1417 if (error)
1418 return (error);
1419 vp = nd.ni_vp;
1420
1421 /* Don't dump to non-regular files or files with links. */
1422 if (vp->v_type != VREG ||
1423 VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
1424 error = EINVAL;
1425 goto out;
1426 }
1427 VATTR_NULL(&vattr);
1428 vattr.va_size = 0;
1429 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1430 VOP_SETATTR(vp, &vattr, cred, p);
1431 p->p_acflag |= ACORE;
1432
1433 /* Now dump the actual core file. */
1434 error = (*p->p_execsw->es_coredump)(p, vp, cred);
1435 out:
1436 VOP_UNLOCK(vp, 0);
1437 error1 = vn_close(vp, FWRITE, cred, p);
1438 if (error == 0)
1439 error = error1;
1440 return (error);
1441 }
1442
1443 /*
1444 * Nonexistent system call-- signal process (may want to handle it).
1445 * Flag error in case process won't see signal immediately (blocked or ignored).
1446 */
1447 /* ARGSUSED */
1448 int
1449 sys_nosys(struct proc *p, void *v, register_t *retval)
1450 {
1451
1452 psignal(p, SIGSYS);
1453 return (ENOSYS);
1454 }
1455
1456 static int
1457 build_corename(struct proc *p, char dst[MAXPATHLEN])
1458 {
1459 const char *s;
1460 char *d, *end;
1461 int i;
1462
1463 for (s = p->p_limit->pl_corename, d = dst, end = d + MAXPATHLEN;
1464 *s != '\0'; s++) {
1465 if (*s == '%') {
1466 switch (*(s + 1)) {
1467 case 'n':
1468 i = snprintf(d, end - d, "%s", p->p_comm);
1469 break;
1470 case 'p':
1471 i = snprintf(d, end - d, "%d", p->p_pid);
1472 break;
1473 case 'u':
1474 i = snprintf(d, end - d, "%s",
1475 p->p_pgrp->pg_session->s_login);
1476 break;
1477 case 't':
1478 i = snprintf(d, end - d, "%ld",
1479 p->p_stats->p_start.tv_sec);
1480 break;
1481 default:
1482 goto copy;
1483 }
1484 d += i;
1485 s++;
1486 } else {
1487 copy: *d = *s;
1488 d++;
1489 }
1490 if (d >= end)
1491 return (ENAMETOOLONG);
1492 }
1493 *d = '\0';
1494 return (0);
1495 }
1496
1497 /*
1498 * Returns true if signal is ignored or masked for passed process.
1499 */
1500 int
1501 sigismasked(struct proc *p, int sig)
1502 {
1503
1504 return (sigismember(&p->p_sigctx.ps_sigignore, sig) ||
1505 sigismember(&p->p_sigctx.ps_sigmask, sig));
1506 }
1507