Line data Source code
1 : /* $OpenBSD: kern_sig.c,v 1.224 2018/08/03 14:47:56 deraadt Exp $ */
2 : /* $NetBSD: kern_sig.c,v 1.54 1996/04/22 01:38:32 christos Exp $ */
3 :
4 : /*
5 : * Copyright (c) 1997 Theo de Raadt. All rights reserved.
6 : * Copyright (c) 1982, 1986, 1989, 1991, 1993
7 : * The Regents of the University of California. All rights reserved.
8 : * (c) UNIX System Laboratories, Inc.
9 : * All or some portions of this file are derived from material licensed
10 : * to the University of California by American Telephone and Telegraph
11 : * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12 : * the permission of UNIX System Laboratories, Inc.
13 : *
14 : * Redistribution and use in source and binary forms, with or without
15 : * modification, are permitted provided that the following conditions
16 : * are met:
17 : * 1. Redistributions of source code must retain the above copyright
18 : * notice, this list of conditions and the following disclaimer.
19 : * 2. Redistributions in binary form must reproduce the above copyright
20 : * notice, this list of conditions and the following disclaimer in the
21 : * documentation and/or other materials provided with the distribution.
22 : * 3. Neither the name of the University nor the names of its contributors
23 : * may be used to endorse or promote products derived from this software
24 : * without specific prior written permission.
25 : *
26 : * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 : * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 : * SUCH DAMAGE.
37 : *
38 : * @(#)kern_sig.c 8.7 (Berkeley) 4/18/94
39 : */
40 :
41 : #define SIGPROP /* include signal properties table */
42 : #include <sys/param.h>
43 : #include <sys/signalvar.h>
44 : #include <sys/resourcevar.h>
45 : #include <sys/queue.h>
46 : #include <sys/namei.h>
47 : #include <sys/vnode.h>
48 : #include <sys/event.h>
49 : #include <sys/proc.h>
50 : #include <sys/systm.h>
51 : #include <sys/acct.h>
52 : #include <sys/fcntl.h>
53 : #include <sys/filedesc.h>
54 : #include <sys/kernel.h>
55 : #include <sys/wait.h>
56 : #include <sys/ktrace.h>
57 : #include <sys/stat.h>
58 : #include <sys/core.h>
59 : #include <sys/malloc.h>
60 : #include <sys/pool.h>
61 : #include <sys/ptrace.h>
62 : #include <sys/sched.h>
63 : #include <sys/user.h>
64 : #include <sys/syslog.h>
65 : #include <sys/pledge.h>
66 : #include <sys/witness.h>
67 :
68 : #include <sys/mount.h>
69 : #include <sys/syscallargs.h>
70 :
71 : #include <uvm/uvm_extern.h>
72 : #include <machine/tcb.h>
73 :
74 : int filt_sigattach(struct knote *kn);
75 : void filt_sigdetach(struct knote *kn);
76 : int filt_signal(struct knote *kn, long hint);
77 :
78 : struct filterops sig_filtops =
79 : { 0, filt_sigattach, filt_sigdetach, filt_signal };
80 :
81 : void proc_stop(struct proc *p, int);
82 : void proc_stop_sweep(void *);
83 : struct timeout proc_stop_to;
84 :
85 : void postsig(struct proc *, int);
86 : int cansignal(struct proc *, struct process *, int);
87 :
88 : struct pool sigacts_pool; /* memory pool for sigacts structures */
89 :
90 : /*
91 : * Can thread p, send the signal signum to process qr?
92 : */
93 : int
94 0 : cansignal(struct proc *p, struct process *qr, int signum)
95 : {
96 0 : struct process *pr = p->p_p;
97 0 : struct ucred *uc = p->p_ucred;
98 0 : struct ucred *quc = qr->ps_ucred;
99 :
100 0 : if (uc->cr_uid == 0)
101 0 : return (1); /* root can always signal */
102 :
103 0 : if (pr == qr)
104 0 : return (1); /* process can always signal itself */
105 :
106 : /* optimization: if the same creds then the tests below will pass */
107 0 : if (uc == quc)
108 0 : return (1);
109 :
110 0 : if (signum == SIGCONT && qr->ps_session == pr->ps_session)
111 0 : return (1); /* SIGCONT in session */
112 :
113 : /*
114 : * Using kill(), only certain signals can be sent to setugid
115 : * child processes
116 : */
117 0 : if (qr->ps_flags & PS_SUGID) {
118 0 : switch (signum) {
119 : case 0:
120 : case SIGKILL:
121 : case SIGINT:
122 : case SIGTERM:
123 : case SIGALRM:
124 : case SIGSTOP:
125 : case SIGTTIN:
126 : case SIGTTOU:
127 : case SIGTSTP:
128 : case SIGHUP:
129 : case SIGUSR1:
130 : case SIGUSR2:
131 0 : if (uc->cr_ruid == quc->cr_ruid ||
132 0 : uc->cr_uid == quc->cr_ruid)
133 0 : return (1);
134 : }
135 0 : return (0);
136 : }
137 :
138 0 : if (uc->cr_ruid == quc->cr_ruid ||
139 0 : uc->cr_ruid == quc->cr_svuid ||
140 0 : uc->cr_uid == quc->cr_ruid ||
141 0 : uc->cr_uid == quc->cr_svuid)
142 0 : return (1);
143 0 : return (0);
144 0 : }
145 :
146 : /*
147 : * Initialize signal-related data structures.
148 : */
149 : void
150 0 : signal_init(void)
151 : {
152 0 : timeout_set(&proc_stop_to, proc_stop_sweep, NULL);
153 :
154 0 : pool_init(&sigacts_pool, sizeof(struct sigacts), 0, IPL_NONE,
155 : PR_WAITOK, "sigapl", NULL);
156 0 : }
157 :
158 : /*
159 : * Create an initial sigacts structure, using the same signal state
160 : * as p.
161 : */
162 : struct sigacts *
163 0 : sigactsinit(struct process *pr)
164 : {
165 : struct sigacts *ps;
166 :
167 0 : ps = pool_get(&sigacts_pool, PR_WAITOK);
168 0 : memcpy(ps, pr->ps_sigacts, sizeof(struct sigacts));
169 0 : ps->ps_refcnt = 1;
170 0 : return (ps);
171 : }
172 :
173 : /*
174 : * Share a sigacts structure.
175 : */
176 : struct sigacts *
177 0 : sigactsshare(struct process *pr)
178 : {
179 0 : struct sigacts *ps = pr->ps_sigacts;
180 :
181 0 : ps->ps_refcnt++;
182 0 : return ps;
183 : }
184 :
185 : /*
186 : * Initialize a new sigaltstack structure.
187 : */
188 : void
189 0 : sigstkinit(struct sigaltstack *ss)
190 : {
191 0 : ss->ss_flags = SS_DISABLE;
192 0 : ss->ss_size = 0;
193 0 : ss->ss_sp = 0;
194 0 : }
195 :
196 : /*
197 : * Make this process not share its sigacts, maintaining all
198 : * signal state.
199 : */
200 : void
201 0 : sigactsunshare(struct process *pr)
202 : {
203 : struct sigacts *newps;
204 :
205 0 : if (pr->ps_sigacts->ps_refcnt == 1)
206 0 : return;
207 :
208 0 : newps = sigactsinit(pr);
209 0 : sigactsfree(pr);
210 0 : pr->ps_sigacts = newps;
211 0 : }
212 :
213 : /*
214 : * Release a sigacts structure.
215 : */
216 : void
217 0 : sigactsfree(struct process *pr)
218 : {
219 0 : struct sigacts *ps = pr->ps_sigacts;
220 :
221 0 : if (--ps->ps_refcnt > 0)
222 0 : return;
223 :
224 0 : pr->ps_sigacts = NULL;
225 :
226 0 : pool_put(&sigacts_pool, ps);
227 0 : }
228 :
229 : int
230 0 : sys_sigaction(struct proc *p, void *v, register_t *retval)
231 : {
232 : struct sys_sigaction_args /* {
233 : syscallarg(int) signum;
234 : syscallarg(const struct sigaction *) nsa;
235 : syscallarg(struct sigaction *) osa;
236 0 : } */ *uap = v;
237 0 : struct sigaction vec;
238 : #ifdef KTRACE
239 0 : struct sigaction ovec;
240 : #endif
241 : struct sigaction *sa;
242 : const struct sigaction *nsa;
243 : struct sigaction *osa;
244 0 : struct sigacts *ps = p->p_p->ps_sigacts;
245 : int signum;
246 : int bit, error;
247 :
248 0 : signum = SCARG(uap, signum);
249 0 : nsa = SCARG(uap, nsa);
250 0 : osa = SCARG(uap, osa);
251 :
252 0 : if (signum <= 0 || signum >= NSIG ||
253 0 : (nsa && (signum == SIGKILL || signum == SIGSTOP)))
254 0 : return (EINVAL);
255 : sa = &vec;
256 0 : if (osa) {
257 0 : sa->sa_handler = ps->ps_sigact[signum];
258 0 : sa->sa_mask = ps->ps_catchmask[signum];
259 0 : bit = sigmask(signum);
260 0 : sa->sa_flags = 0;
261 0 : if ((ps->ps_sigonstack & bit) != 0)
262 0 : sa->sa_flags |= SA_ONSTACK;
263 0 : if ((ps->ps_sigintr & bit) == 0)
264 0 : sa->sa_flags |= SA_RESTART;
265 0 : if ((ps->ps_sigreset & bit) != 0)
266 0 : sa->sa_flags |= SA_RESETHAND;
267 0 : if ((ps->ps_siginfo & bit) != 0)
268 0 : sa->sa_flags |= SA_SIGINFO;
269 0 : if (signum == SIGCHLD) {
270 0 : if ((ps->ps_flags & SAS_NOCLDSTOP) != 0)
271 0 : sa->sa_flags |= SA_NOCLDSTOP;
272 0 : if ((ps->ps_flags & SAS_NOCLDWAIT) != 0)
273 0 : sa->sa_flags |= SA_NOCLDWAIT;
274 : }
275 0 : if ((sa->sa_mask & bit) == 0)
276 0 : sa->sa_flags |= SA_NODEFER;
277 0 : sa->sa_mask &= ~bit;
278 0 : error = copyout(sa, osa, sizeof (vec));
279 0 : if (error)
280 0 : return (error);
281 : #ifdef KTRACE
282 0 : if (KTRPOINT(p, KTR_STRUCT))
283 0 : ovec = vec;
284 : #endif
285 : }
286 0 : if (nsa) {
287 0 : error = copyin(nsa, sa, sizeof (vec));
288 0 : if (error)
289 0 : return (error);
290 : #ifdef KTRACE
291 0 : if (KTRPOINT(p, KTR_STRUCT))
292 0 : ktrsigaction(p, sa);
293 : #endif
294 0 : setsigvec(p, signum, sa);
295 0 : }
296 : #ifdef KTRACE
297 0 : if (osa && KTRPOINT(p, KTR_STRUCT))
298 0 : ktrsigaction(p, &ovec);
299 : #endif
300 0 : return (0);
301 0 : }
302 :
303 : void
304 0 : setsigvec(struct proc *p, int signum, struct sigaction *sa)
305 : {
306 0 : struct sigacts *ps = p->p_p->ps_sigacts;
307 : int bit;
308 : int s;
309 :
310 0 : bit = sigmask(signum);
311 : /*
312 : * Change setting atomically.
313 : */
314 0 : s = splhigh();
315 0 : ps->ps_sigact[signum] = sa->sa_handler;
316 0 : if ((sa->sa_flags & SA_NODEFER) == 0)
317 0 : sa->sa_mask |= sigmask(signum);
318 0 : ps->ps_catchmask[signum] = sa->sa_mask &~ sigcantmask;
319 0 : if (signum == SIGCHLD) {
320 0 : if (sa->sa_flags & SA_NOCLDSTOP)
321 0 : atomic_setbits_int(&ps->ps_flags, SAS_NOCLDSTOP);
322 : else
323 0 : atomic_clearbits_int(&ps->ps_flags, SAS_NOCLDSTOP);
324 : /*
325 : * If the SA_NOCLDWAIT flag is set or the handler
326 : * is SIG_IGN we reparent the dying child to PID 1
327 : * (init) which will reap the zombie. Because we use
328 : * init to do our dirty work we never set SAS_NOCLDWAIT
329 : * for PID 1.
330 : * XXX exit1 rework means this is unnecessary?
331 : */
332 0 : if (initprocess->ps_sigacts != ps &&
333 0 : ((sa->sa_flags & SA_NOCLDWAIT) ||
334 0 : sa->sa_handler == SIG_IGN))
335 0 : atomic_setbits_int(&ps->ps_flags, SAS_NOCLDWAIT);
336 : else
337 0 : atomic_clearbits_int(&ps->ps_flags, SAS_NOCLDWAIT);
338 : }
339 0 : if ((sa->sa_flags & SA_RESETHAND) != 0)
340 0 : ps->ps_sigreset |= bit;
341 : else
342 0 : ps->ps_sigreset &= ~bit;
343 0 : if ((sa->sa_flags & SA_SIGINFO) != 0)
344 0 : ps->ps_siginfo |= bit;
345 : else
346 0 : ps->ps_siginfo &= ~bit;
347 0 : if ((sa->sa_flags & SA_RESTART) == 0)
348 0 : ps->ps_sigintr |= bit;
349 : else
350 0 : ps->ps_sigintr &= ~bit;
351 0 : if ((sa->sa_flags & SA_ONSTACK) != 0)
352 0 : ps->ps_sigonstack |= bit;
353 : else
354 0 : ps->ps_sigonstack &= ~bit;
355 : /*
356 : * Set bit in ps_sigignore for signals that are set to SIG_IGN,
357 : * and for signals set to SIG_DFL where the default is to ignore.
358 : * However, don't put SIGCONT in ps_sigignore,
359 : * as we have to restart the process.
360 : */
361 0 : if (sa->sa_handler == SIG_IGN ||
362 0 : (sigprop[signum] & SA_IGNORE && sa->sa_handler == SIG_DFL)) {
363 0 : atomic_clearbits_int(&p->p_siglist, bit);
364 0 : if (signum != SIGCONT)
365 0 : ps->ps_sigignore |= bit; /* easier in psignal */
366 0 : ps->ps_sigcatch &= ~bit;
367 0 : } else {
368 0 : ps->ps_sigignore &= ~bit;
369 0 : if (sa->sa_handler == SIG_DFL)
370 0 : ps->ps_sigcatch &= ~bit;
371 : else
372 0 : ps->ps_sigcatch |= bit;
373 : }
374 0 : splx(s);
375 0 : }
376 :
377 : /*
378 : * Initialize signal state for process 0;
379 : * set to ignore signals that are ignored by default.
380 : */
381 : void
382 0 : siginit(struct process *pr)
383 : {
384 0 : struct sigacts *ps = pr->ps_sigacts;
385 : int i;
386 :
387 0 : for (i = 0; i < NSIG; i++)
388 0 : if (sigprop[i] & SA_IGNORE && i != SIGCONT)
389 0 : ps->ps_sigignore |= sigmask(i);
390 0 : ps->ps_flags = SAS_NOCLDWAIT | SAS_NOCLDSTOP;
391 0 : }
392 :
393 : /*
394 : * Reset signals for an exec by the specified thread.
395 : */
396 : void
397 0 : execsigs(struct proc *p)
398 : {
399 : struct sigacts *ps;
400 : int nc, mask;
401 :
402 0 : sigactsunshare(p->p_p);
403 0 : ps = p->p_p->ps_sigacts;
404 :
405 : /*
406 : * Reset caught signals. Held signals remain held
407 : * through p_sigmask (unless they were caught,
408 : * and are now ignored by default).
409 : */
410 0 : while (ps->ps_sigcatch) {
411 0 : nc = ffs((long)ps->ps_sigcatch);
412 0 : mask = sigmask(nc);
413 0 : ps->ps_sigcatch &= ~mask;
414 0 : if (sigprop[nc] & SA_IGNORE) {
415 0 : if (nc != SIGCONT)
416 0 : ps->ps_sigignore |= mask;
417 0 : atomic_clearbits_int(&p->p_siglist, mask);
418 0 : }
419 0 : ps->ps_sigact[nc] = SIG_DFL;
420 : }
421 : /*
422 : * Reset stack state to the user stack.
423 : * Clear set of signals caught on the signal stack.
424 : */
425 0 : sigstkinit(&p->p_sigstk);
426 0 : ps->ps_flags &= ~SAS_NOCLDWAIT;
427 0 : if (ps->ps_sigact[SIGCHLD] == SIG_IGN)
428 0 : ps->ps_sigact[SIGCHLD] = SIG_DFL;
429 0 : }
430 :
431 : /*
432 : * Manipulate signal mask.
433 : * Note that we receive new mask, not pointer,
434 : * and return old mask as return value;
435 : * the library stub does the rest.
436 : */
437 : int
438 0 : sys_sigprocmask(struct proc *p, void *v, register_t *retval)
439 : {
440 : struct sys_sigprocmask_args /* {
441 : syscallarg(int) how;
442 : syscallarg(sigset_t) mask;
443 0 : } */ *uap = v;
444 : int error = 0;
445 : sigset_t mask;
446 :
447 0 : *retval = p->p_sigmask;
448 0 : mask = SCARG(uap, mask) &~ sigcantmask;
449 :
450 0 : switch (SCARG(uap, how)) {
451 : case SIG_BLOCK:
452 0 : atomic_setbits_int(&p->p_sigmask, mask);
453 0 : break;
454 : case SIG_UNBLOCK:
455 0 : atomic_clearbits_int(&p->p_sigmask, mask);
456 0 : break;
457 : case SIG_SETMASK:
458 0 : p->p_sigmask = mask;
459 0 : break;
460 : default:
461 : error = EINVAL;
462 0 : break;
463 : }
464 0 : return (error);
465 : }
466 :
467 : int
468 0 : sys_sigpending(struct proc *p, void *v, register_t *retval)
469 : {
470 :
471 0 : *retval = p->p_siglist;
472 0 : return (0);
473 : }
474 :
475 : /*
476 : * Temporarily replace calling proc's signal mask for the duration of a
477 : * system call. Original signal mask will be restored by userret().
478 : */
479 : void
480 0 : dosigsuspend(struct proc *p, sigset_t newmask)
481 : {
482 0 : KASSERT(p == curproc);
483 :
484 0 : p->p_oldmask = p->p_sigmask;
485 0 : atomic_setbits_int(&p->p_flag, P_SIGSUSPEND);
486 0 : p->p_sigmask = newmask;
487 0 : }
488 :
489 : /*
490 : * Suspend process until signal, providing mask to be set
491 : * in the meantime. Note nonstandard calling convention:
492 : * libc stub passes mask, not pointer, to save a copyin.
493 : */
494 : int
495 0 : sys_sigsuspend(struct proc *p, void *v, register_t *retval)
496 : {
497 : struct sys_sigsuspend_args /* {
498 : syscallarg(int) mask;
499 0 : } */ *uap = v;
500 0 : struct process *pr = p->p_p;
501 0 : struct sigacts *ps = pr->ps_sigacts;
502 :
503 0 : dosigsuspend(p, SCARG(uap, mask) &~ sigcantmask);
504 0 : while (tsleep(ps, PPAUSE|PCATCH, "pause", 0) == 0)
505 : /* void */;
506 : /* always return EINTR rather than ERESTART... */
507 0 : return (EINTR);
508 : }
509 :
510 : int
511 0 : sigonstack(size_t stack)
512 : {
513 0 : const struct sigaltstack *ss = &curproc->p_sigstk;
514 :
515 0 : return (ss->ss_flags & SS_DISABLE ? 0 :
516 0 : (stack - (size_t)ss->ss_sp < ss->ss_size));
517 : }
518 :
519 : int
520 0 : sys_sigaltstack(struct proc *p, void *v, register_t *retval)
521 : {
522 : struct sys_sigaltstack_args /* {
523 : syscallarg(const struct sigaltstack *) nss;
524 : syscallarg(struct sigaltstack *) oss;
525 0 : } */ *uap = v;
526 0 : struct sigaltstack ss;
527 : const struct sigaltstack *nss;
528 : struct sigaltstack *oss;
529 0 : int onstack = sigonstack(PROC_STACK(p));
530 : int error;
531 :
532 0 : nss = SCARG(uap, nss);
533 0 : oss = SCARG(uap, oss);
534 :
535 0 : if (oss != NULL) {
536 0 : ss = p->p_sigstk;
537 0 : if (onstack)
538 0 : ss.ss_flags |= SS_ONSTACK;
539 0 : if ((error = copyout(&ss, oss, sizeof(ss))))
540 0 : return (error);
541 : }
542 0 : if (nss == NULL)
543 0 : return (0);
544 0 : error = copyin(nss, &ss, sizeof(ss));
545 0 : if (error)
546 0 : return (error);
547 0 : if (onstack)
548 0 : return (EPERM);
549 0 : if (ss.ss_flags & ~SS_DISABLE)
550 0 : return (EINVAL);
551 0 : if (ss.ss_flags & SS_DISABLE) {
552 0 : p->p_sigstk.ss_flags = ss.ss_flags;
553 0 : return (0);
554 : }
555 0 : if (ss.ss_size < MINSIGSTKSZ)
556 0 : return (ENOMEM);
557 :
558 0 : error = uvm_map_remap_as_stack(p, (vaddr_t)ss.ss_sp, ss.ss_size);
559 0 : if (error)
560 0 : return (error);
561 :
562 0 : p->p_sigstk = ss;
563 0 : return (0);
564 0 : }
565 :
566 : int
567 0 : sys_kill(struct proc *cp, void *v, register_t *retval)
568 : {
569 : struct sys_kill_args /* {
570 : syscallarg(int) pid;
571 : syscallarg(int) signum;
572 0 : } */ *uap = v;
573 : struct process *pr;
574 0 : int pid = SCARG(uap, pid);
575 0 : int signum = SCARG(uap, signum);
576 : int error;
577 : int zombie = 0;
578 :
579 0 : if ((error = pledge_kill(cp, pid)) != 0)
580 0 : return (error);
581 0 : if (((u_int)signum) >= NSIG)
582 0 : return (EINVAL);
583 0 : if (pid > 0) {
584 0 : if ((pr = prfind(pid)) == NULL) {
585 0 : if ((pr = zombiefind(pid)) == NULL)
586 0 : return (ESRCH);
587 : else
588 : zombie = 1;
589 0 : }
590 0 : if (!cansignal(cp, pr, signum))
591 0 : return (EPERM);
592 :
593 : /* kill single process */
594 0 : if (signum && !zombie)
595 0 : prsignal(pr, signum);
596 0 : return (0);
597 : }
598 0 : switch (pid) {
599 : case -1: /* broadcast signal */
600 0 : return (killpg1(cp, signum, 0, 1));
601 : case 0: /* signal own process group */
602 0 : return (killpg1(cp, signum, 0, 0));
603 : default: /* negative explicit process group */
604 0 : return (killpg1(cp, signum, -pid, 0));
605 : }
606 0 : }
607 :
608 : int
609 0 : sys_thrkill(struct proc *cp, void *v, register_t *retval)
610 : {
611 : struct sys_thrkill_args /* {
612 : syscallarg(pid_t) tid;
613 : syscallarg(int) signum;
614 : syscallarg(void *) tcb;
615 0 : } */ *uap = v;
616 : struct proc *p;
617 0 : int tid = SCARG(uap, tid);
618 0 : int signum = SCARG(uap, signum);
619 : void *tcb;
620 :
621 0 : if (((u_int)signum) >= NSIG)
622 0 : return (EINVAL);
623 0 : if (tid > THREAD_PID_OFFSET) {
624 0 : if ((p = tfind(tid - THREAD_PID_OFFSET)) == NULL)
625 0 : return (ESRCH);
626 :
627 : /* can only kill threads in the same process */
628 0 : if (p->p_p != cp->p_p)
629 0 : return (ESRCH);
630 0 : } else if (tid == 0)
631 : p = cp;
632 : else
633 0 : return (EINVAL);
634 :
635 : /* optionally require the target thread to have the given tcb addr */
636 0 : tcb = SCARG(uap, tcb);
637 0 : if (tcb != NULL && tcb != TCB_GET(p))
638 0 : return (ESRCH);
639 :
640 0 : if (signum)
641 0 : ptsignal(p, signum, STHREAD);
642 0 : return (0);
643 0 : }
644 :
645 : /*
646 : * Common code for kill process group/broadcast kill.
647 : * cp is calling process.
648 : */
649 : int
650 0 : killpg1(struct proc *cp, int signum, int pgid, int all)
651 : {
652 : struct process *pr;
653 : struct pgrp *pgrp;
654 : int nfound = 0;
655 :
656 0 : if (all) {
657 : /*
658 : * broadcast
659 : */
660 0 : LIST_FOREACH(pr, &allprocess, ps_list) {
661 0 : if (pr->ps_pid <= 1 ||
662 0 : pr->ps_flags & (PS_SYSTEM | PS_NOBROADCASTKILL) ||
663 0 : pr == cp->p_p || !cansignal(cp, pr, signum))
664 : continue;
665 0 : nfound++;
666 0 : if (signum)
667 0 : prsignal(pr, signum);
668 : }
669 : } else {
670 0 : if (pgid == 0)
671 : /*
672 : * zero pgid means send to my process group.
673 : */
674 0 : pgrp = cp->p_p->ps_pgrp;
675 : else {
676 0 : pgrp = pgfind(pgid);
677 0 : if (pgrp == NULL)
678 0 : return (ESRCH);
679 : }
680 0 : LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist) {
681 0 : if (pr->ps_pid <= 1 || pr->ps_flags & PS_SYSTEM ||
682 0 : !cansignal(cp, pr, signum))
683 : continue;
684 0 : nfound++;
685 0 : if (signum)
686 0 : prsignal(pr, signum);
687 : }
688 : }
689 0 : return (nfound ? 0 : ESRCH);
690 0 : }
691 :
692 : #define CANDELIVER(uid, euid, pr) \
693 : (euid == 0 || \
694 : (uid) == (pr)->ps_ucred->cr_ruid || \
695 : (uid) == (pr)->ps_ucred->cr_svuid || \
696 : (uid) == (pr)->ps_ucred->cr_uid || \
697 : (euid) == (pr)->ps_ucred->cr_ruid || \
698 : (euid) == (pr)->ps_ucred->cr_svuid || \
699 : (euid) == (pr)->ps_ucred->cr_uid)
700 :
701 : /*
702 : * Deliver signum to pgid, but first check uid/euid against each
703 : * process and see if it is permitted.
704 : */
705 : void
706 0 : csignal(pid_t pgid, int signum, uid_t uid, uid_t euid)
707 : {
708 : struct pgrp *pgrp;
709 : struct process *pr;
710 :
711 0 : if (pgid == 0)
712 0 : return;
713 0 : if (pgid < 0) {
714 0 : pgid = -pgid;
715 0 : if ((pgrp = pgfind(pgid)) == NULL)
716 0 : return;
717 0 : LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist)
718 0 : if (CANDELIVER(uid, euid, pr))
719 0 : prsignal(pr, signum);
720 : } else {
721 0 : if ((pr = prfind(pgid)) == NULL)
722 0 : return;
723 0 : if (CANDELIVER(uid, euid, pr))
724 0 : prsignal(pr, signum);
725 : }
726 0 : }
727 :
728 : /*
729 : * Send a signal to a process group.
730 : */
731 : void
732 0 : gsignal(int pgid, int signum)
733 : {
734 : struct pgrp *pgrp;
735 :
736 0 : if (pgid && (pgrp = pgfind(pgid)))
737 0 : pgsignal(pgrp, signum, 0);
738 0 : }
739 :
740 : /*
741 : * Send a signal to a process group. If checktty is 1,
742 : * limit to members which have a controlling terminal.
743 : */
744 : void
745 0 : pgsignal(struct pgrp *pgrp, int signum, int checkctty)
746 : {
747 : struct process *pr;
748 :
749 0 : if (pgrp)
750 0 : LIST_FOREACH(pr, &pgrp->pg_members, ps_pglist)
751 0 : if (checkctty == 0 || pr->ps_flags & PS_CONTROLT)
752 0 : prsignal(pr, signum);
753 0 : }
754 :
755 : /*
756 : * Recalculate the signal mask and reset the signal disposition after
757 : * usermode frame for delivery is formed.
758 : */
759 : void
760 0 : postsig_done(struct proc *p, int signum, struct sigacts *ps)
761 : {
762 0 : int mask = sigmask(signum);
763 :
764 0 : KERNEL_ASSERT_LOCKED();
765 :
766 0 : p->p_ru.ru_nsignals++;
767 0 : atomic_setbits_int(&p->p_sigmask, ps->ps_catchmask[signum]);
768 0 : if ((ps->ps_sigreset & mask) != 0) {
769 0 : ps->ps_sigcatch &= ~mask;
770 0 : if (signum != SIGCONT && sigprop[signum] & SA_IGNORE)
771 0 : ps->ps_sigignore |= mask;
772 0 : ps->ps_sigact[signum] = SIG_DFL;
773 0 : }
774 0 : }
775 :
776 : /*
777 : * Send a signal caused by a trap to the current thread
778 : * If it will be caught immediately, deliver it with correct code.
779 : * Otherwise, post it normally.
780 : */
781 : void
782 0 : trapsignal(struct proc *p, int signum, u_long trapno, int code,
783 : union sigval sigval)
784 : {
785 0 : struct process *pr = p->p_p;
786 0 : struct sigacts *ps = pr->ps_sigacts;
787 : int mask;
788 :
789 0 : switch (signum) {
790 : case SIGILL:
791 : case SIGBUS:
792 : case SIGSEGV:
793 0 : pr->ps_acflag |= ATRAP;
794 0 : break;
795 : }
796 :
797 0 : mask = sigmask(signum);
798 0 : if ((pr->ps_flags & PS_TRACED) == 0 &&
799 0 : (ps->ps_sigcatch & mask) != 0 &&
800 0 : (p->p_sigmask & mask) == 0) {
801 0 : siginfo_t si;
802 0 : initsiginfo(&si, signum, trapno, code, sigval);
803 : #ifdef KTRACE
804 0 : if (KTRPOINT(p, KTR_PSIG)) {
805 0 : ktrpsig(p, signum, ps->ps_sigact[signum],
806 0 : p->p_sigmask, code, &si);
807 0 : }
808 : #endif
809 0 : sendsig(ps->ps_sigact[signum], signum, p->p_sigmask, &si);
810 0 : postsig_done(p, signum, ps);
811 0 : } else {
812 0 : p->p_sisig = signum;
813 0 : p->p_sitrapno = trapno; /* XXX for core dump/debugger */
814 0 : p->p_sicode = code;
815 0 : p->p_sigval = sigval;
816 :
817 : /*
818 : * Signals like SIGBUS and SIGSEGV should not, when
819 : * generated by the kernel, be ignorable or blockable.
820 : * If it is and we're not being traced, then just kill
821 : * the process.
822 : */
823 0 : if ((pr->ps_flags & PS_TRACED) == 0 &&
824 0 : (sigprop[signum] & SA_KILL) &&
825 0 : ((p->p_sigmask & mask) || (ps->ps_sigignore & mask)))
826 0 : sigexit(p, signum);
827 0 : ptsignal(p, signum, STHREAD);
828 : }
829 0 : }
830 :
831 : /*
832 : * Send the signal to the process. If the signal has an action, the action
833 : * is usually performed by the target process rather than the caller; we add
834 : * the signal to the set of pending signals for the process.
835 : *
836 : * Exceptions:
837 : * o When a stop signal is sent to a sleeping process that takes the
838 : * default action, the process is stopped without awakening it.
839 : * o SIGCONT restarts stopped processes (or puts them back to sleep)
840 : * regardless of the signal action (eg, blocked or ignored).
841 : *
842 : * Other ignored signals are discarded immediately.
843 : */
844 : void
845 0 : psignal(struct proc *p, int signum)
846 : {
847 0 : ptsignal(p, signum, SPROCESS);
848 0 : }
849 :
850 : /*
851 : * type = SPROCESS process signal, can be diverted (sigwait())
852 : * XXX if blocked in all threads, mark as pending in struct process
853 : * type = STHREAD thread signal, but should be propagated if unhandled
854 : * type = SPROPAGATED propagated to this thread, so don't propagate again
855 : */
856 : void
857 0 : ptsignal(struct proc *p, int signum, enum signal_type type)
858 : {
859 : int s, prop;
860 : sig_t action;
861 : int mask;
862 0 : struct process *pr = p->p_p;
863 : struct proc *q;
864 : int wakeparent = 0;
865 :
866 : #ifdef DIAGNOSTIC
867 0 : if ((u_int)signum >= NSIG || signum == 0)
868 0 : panic("psignal signal number");
869 : #endif
870 :
871 : /* Ignore signal if the target process is exiting */
872 0 : if (pr->ps_flags & PS_EXITING)
873 0 : return;
874 :
875 0 : mask = sigmask(signum);
876 :
877 0 : if (type == SPROCESS) {
878 : /* Accept SIGKILL to coredumping processes */
879 0 : if (pr->ps_flags & PS_COREDUMP && signum == SIGKILL) {
880 0 : if (pr->ps_single != NULL)
881 0 : p = pr->ps_single;
882 0 : atomic_setbits_int(&p->p_siglist, mask);
883 0 : return;
884 : }
885 :
886 : /*
887 : * If the current thread can process the signal
888 : * immediately (it's unblocked) then have it take it.
889 : */
890 0 : q = curproc;
891 0 : if (q != NULL && q->p_p == pr && (q->p_flag & P_WEXIT) == 0 &&
892 0 : (q->p_sigmask & mask) == 0)
893 0 : p = q;
894 : else {
895 : /*
896 : * A process-wide signal can be diverted to a
897 : * different thread that's in sigwait() for this
898 : * signal. If there isn't such a thread, then
899 : * pick a thread that doesn't have it blocked so
900 : * that the stop/kill consideration isn't
901 : * delayed. Otherwise, mark it pending on the
902 : * main thread.
903 : */
904 0 : TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
905 : /* ignore exiting threads */
906 0 : if (q->p_flag & P_WEXIT)
907 : continue;
908 :
909 : /* skip threads that have the signal blocked */
910 0 : if ((q->p_sigmask & mask) != 0)
911 : continue;
912 :
913 : /* okay, could send to this thread */
914 : p = q;
915 :
916 : /*
917 : * sigsuspend, sigwait, ppoll/pselect, etc?
918 : * Definitely go to this thread, as it's
919 : * already blocked in the kernel.
920 : */
921 0 : if (q->p_flag & P_SIGSUSPEND)
922 : break;
923 : }
924 : }
925 : }
926 :
927 0 : if (type != SPROPAGATED)
928 0 : KNOTE(&pr->ps_klist, NOTE_SIGNAL | signum);
929 :
930 0 : prop = sigprop[signum];
931 :
932 : /*
933 : * If proc is traced, always give parent a chance.
934 : */
935 0 : if (pr->ps_flags & PS_TRACED) {
936 : action = SIG_DFL;
937 0 : atomic_setbits_int(&p->p_siglist, mask);
938 0 : } else {
939 : /*
940 : * If the signal is being ignored,
941 : * then we forget about it immediately.
942 : * (Note: we don't set SIGCONT in ps_sigignore,
943 : * and if it is set to SIG_IGN,
944 : * action will be SIG_DFL here.)
945 : */
946 0 : if (pr->ps_sigacts->ps_sigignore & mask)
947 0 : return;
948 0 : if (p->p_sigmask & mask) {
949 : action = SIG_HOLD;
950 0 : } else if (pr->ps_sigacts->ps_sigcatch & mask) {
951 : action = SIG_CATCH;
952 0 : } else {
953 : action = SIG_DFL;
954 :
955 0 : if (prop & SA_KILL && pr->ps_nice > NZERO)
956 0 : pr->ps_nice = NZERO;
957 :
958 : /*
959 : * If sending a tty stop signal to a member of an
960 : * orphaned process group, discard the signal here if
961 : * the action is default; don't stop the process below
962 : * if sleeping, and don't clear any pending SIGCONT.
963 : */
964 0 : if (prop & SA_TTYSTOP && pr->ps_pgrp->pg_jobc == 0)
965 0 : return;
966 : }
967 :
968 0 : atomic_setbits_int(&p->p_siglist, mask);
969 : }
970 :
971 0 : if (prop & SA_CONT)
972 0 : atomic_clearbits_int(&p->p_siglist, stopsigmask);
973 :
974 0 : if (prop & SA_STOP) {
975 0 : atomic_clearbits_int(&p->p_siglist, contsigmask);
976 0 : atomic_clearbits_int(&p->p_flag, P_CONTINUED);
977 0 : }
978 :
979 : /*
980 : * XXX delay processing of SA_STOP signals unless action == SIG_DFL?
981 : */
982 0 : if (prop & (SA_CONT | SA_STOP) && type != SPROPAGATED)
983 0 : TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link)
984 0 : if (q != p)
985 0 : ptsignal(q, signum, SPROPAGATED);
986 :
987 : /*
988 : * Defer further processing for signals which are held,
989 : * except that stopped processes must be continued by SIGCONT.
990 : */
991 0 : if (action == SIG_HOLD && ((prop & SA_CONT) == 0 || p->p_stat != SSTOP))
992 0 : return;
993 :
994 0 : SCHED_LOCK(s);
995 :
996 0 : switch (p->p_stat) {
997 :
998 : case SSLEEP:
999 : /*
1000 : * If process is sleeping uninterruptibly
1001 : * we can't interrupt the sleep... the signal will
1002 : * be noticed when the process returns through
1003 : * trap() or syscall().
1004 : */
1005 0 : if ((p->p_flag & P_SINTR) == 0)
1006 : goto out;
1007 : /*
1008 : * Process is sleeping and traced... make it runnable
1009 : * so it can discover the signal in issignal() and stop
1010 : * for the parent.
1011 : */
1012 0 : if (pr->ps_flags & PS_TRACED)
1013 : goto run;
1014 : /*
1015 : * If SIGCONT is default (or ignored) and process is
1016 : * asleep, we are finished; the process should not
1017 : * be awakened.
1018 : */
1019 0 : if ((prop & SA_CONT) && action == SIG_DFL) {
1020 0 : atomic_clearbits_int(&p->p_siglist, mask);
1021 0 : goto out;
1022 : }
1023 : /*
1024 : * When a sleeping process receives a stop
1025 : * signal, process immediately if possible.
1026 : */
1027 0 : if ((prop & SA_STOP) && action == SIG_DFL) {
1028 : /*
1029 : * If a child holding parent blocked,
1030 : * stopping could cause deadlock.
1031 : */
1032 0 : if (pr->ps_flags & PS_PPWAIT)
1033 : goto out;
1034 0 : atomic_clearbits_int(&p->p_siglist, mask);
1035 0 : p->p_xstat = signum;
1036 0 : proc_stop(p, 0);
1037 0 : goto out;
1038 : }
1039 : /*
1040 : * All other (caught or default) signals
1041 : * cause the process to run.
1042 : */
1043 : goto runfast;
1044 : /*NOTREACHED*/
1045 :
1046 : case SSTOP:
1047 : /*
1048 : * If traced process is already stopped,
1049 : * then no further action is necessary.
1050 : */
1051 0 : if (pr->ps_flags & PS_TRACED)
1052 : goto out;
1053 :
1054 : /*
1055 : * Kill signal always sets processes running.
1056 : */
1057 0 : if (signum == SIGKILL) {
1058 0 : atomic_clearbits_int(&p->p_flag, P_SUSPSIG);
1059 0 : goto runfast;
1060 : }
1061 :
1062 0 : if (prop & SA_CONT) {
1063 : /*
1064 : * If SIGCONT is default (or ignored), we continue the
1065 : * process but don't leave the signal in p_siglist, as
1066 : * it has no further action. If SIGCONT is held, we
1067 : * continue the process and leave the signal in
1068 : * p_siglist. If the process catches SIGCONT, let it
1069 : * handle the signal itself. If it isn't waiting on
1070 : * an event, then it goes back to run state.
1071 : * Otherwise, process goes back to sleep state.
1072 : */
1073 0 : atomic_setbits_int(&p->p_flag, P_CONTINUED);
1074 0 : atomic_clearbits_int(&p->p_flag, P_SUSPSIG);
1075 : wakeparent = 1;
1076 0 : if (action == SIG_DFL)
1077 0 : atomic_clearbits_int(&p->p_siglist, mask);
1078 0 : if (action == SIG_CATCH)
1079 : goto runfast;
1080 0 : if (p->p_wchan == 0)
1081 : goto run;
1082 0 : p->p_stat = SSLEEP;
1083 0 : goto out;
1084 : }
1085 :
1086 0 : if (prop & SA_STOP) {
1087 : /*
1088 : * Already stopped, don't need to stop again.
1089 : * (If we did the shell could get confused.)
1090 : */
1091 0 : atomic_clearbits_int(&p->p_siglist, mask);
1092 0 : goto out;
1093 : }
1094 :
1095 : /*
1096 : * If process is sleeping interruptibly, then simulate a
1097 : * wakeup so that when it is continued, it will be made
1098 : * runnable and can look at the signal. But don't make
1099 : * the process runnable, leave it stopped.
1100 : */
1101 0 : if (p->p_wchan && p->p_flag & P_SINTR)
1102 0 : unsleep(p);
1103 : goto out;
1104 :
1105 : case SONPROC:
1106 0 : signotify(p);
1107 : /* FALLTHROUGH */
1108 : default:
1109 : /*
1110 : * SRUN, SIDL, SDEAD do nothing with the signal,
1111 : * other than kicking ourselves if we are running.
1112 : * It will either never be noticed, or noticed very soon.
1113 : */
1114 : goto out;
1115 : }
1116 : /*NOTREACHED*/
1117 :
1118 : runfast:
1119 : /*
1120 : * Raise priority to at least PUSER.
1121 : */
1122 0 : if (p->p_priority > PUSER)
1123 0 : p->p_priority = PUSER;
1124 : run:
1125 0 : setrunnable(p);
1126 : out:
1127 0 : SCHED_UNLOCK(s);
1128 0 : if (wakeparent)
1129 0 : wakeup(pr->ps_pptr);
1130 0 : }
1131 :
1132 : /*
1133 : * If the current process has received a signal (should be caught or cause
1134 : * termination, should interrupt current syscall), return the signal number.
1135 : * Stop signals with default action are processed immediately, then cleared;
1136 : * they aren't returned. This is checked after each entry to the system for
1137 : * a syscall or trap (though this can usually be done without calling issignal
1138 : * by checking the pending signal masks in the CURSIG macro.) The normal call
1139 : * sequence is
1140 : *
1141 : * while (signum = CURSIG(curproc))
1142 : * postsig(signum);
1143 : *
1144 : * Assumes that if the P_SINTR flag is set, we're holding both the
1145 : * kernel and scheduler locks.
1146 : */
1147 : int
1148 0 : issignal(struct proc *p)
1149 : {
1150 0 : struct process *pr = p->p_p;
1151 : int signum, mask, prop;
1152 0 : int dolock = (p->p_flag & P_SINTR) == 0;
1153 : int s;
1154 :
1155 0 : for (;;) {
1156 0 : mask = p->p_siglist & ~p->p_sigmask;
1157 0 : if (pr->ps_flags & PS_PPWAIT)
1158 0 : mask &= ~stopsigmask;
1159 0 : if (mask == 0) /* no signal to send */
1160 0 : return (0);
1161 0 : signum = ffs((long)mask);
1162 0 : mask = sigmask(signum);
1163 0 : atomic_clearbits_int(&p->p_siglist, mask);
1164 :
1165 : /*
1166 : * We should see pending but ignored signals
1167 : * only if PS_TRACED was on when they were posted.
1168 : */
1169 0 : if (mask & pr->ps_sigacts->ps_sigignore &&
1170 0 : (pr->ps_flags & PS_TRACED) == 0)
1171 0 : continue;
1172 :
1173 : /*
1174 : * If traced, always stop, and stay stopped until released
1175 : * by the debugger. If our parent process is waiting for
1176 : * us, don't hang as we could deadlock.
1177 : */
1178 0 : if (((pr->ps_flags & (PS_TRACED | PS_PPWAIT)) == PS_TRACED) &&
1179 0 : signum != SIGKILL) {
1180 0 : p->p_xstat = signum;
1181 :
1182 0 : if (dolock)
1183 0 : KERNEL_LOCK();
1184 0 : single_thread_set(p, SINGLE_PTRACE, 0);
1185 0 : if (dolock)
1186 0 : KERNEL_UNLOCK();
1187 :
1188 0 : if (dolock)
1189 0 : SCHED_LOCK(s);
1190 0 : proc_stop(p, 1);
1191 0 : if (dolock)
1192 0 : SCHED_UNLOCK(s);
1193 :
1194 0 : if (dolock)
1195 0 : KERNEL_LOCK();
1196 0 : single_thread_clear(p, 0);
1197 0 : if (dolock)
1198 0 : KERNEL_UNLOCK();
1199 :
1200 : /*
1201 : * If we are no longer being traced, or the parent
1202 : * didn't give us a signal, look for more signals.
1203 : */
1204 0 : if ((pr->ps_flags & PS_TRACED) == 0 || p->p_xstat == 0)
1205 0 : continue;
1206 :
1207 : /*
1208 : * If the new signal is being masked, look for other
1209 : * signals.
1210 : */
1211 : signum = p->p_xstat;
1212 0 : mask = sigmask(signum);
1213 0 : if ((p->p_sigmask & mask) != 0)
1214 0 : continue;
1215 :
1216 : /* take the signal! */
1217 0 : atomic_clearbits_int(&p->p_siglist, mask);
1218 0 : }
1219 :
1220 0 : prop = sigprop[signum];
1221 :
1222 : /*
1223 : * Decide whether the signal should be returned.
1224 : * Return the signal's number, or fall through
1225 : * to clear it from the pending mask.
1226 : */
1227 0 : switch ((long)pr->ps_sigacts->ps_sigact[signum]) {
1228 : case (long)SIG_DFL:
1229 : /*
1230 : * Don't take default actions on system processes.
1231 : */
1232 0 : if (pr->ps_pid <= 1) {
1233 : #ifdef DIAGNOSTIC
1234 : /*
1235 : * Are you sure you want to ignore SIGSEGV
1236 : * in init? XXX
1237 : */
1238 0 : printf("Process (pid %d) got signal"
1239 : " %d\n", pr->ps_pid, signum);
1240 : #endif
1241 0 : break; /* == ignore */
1242 : }
1243 : /*
1244 : * If there is a pending stop signal to process
1245 : * with default action, stop here,
1246 : * then clear the signal. However,
1247 : * if process is member of an orphaned
1248 : * process group, ignore tty stop signals.
1249 : */
1250 0 : if (prop & SA_STOP) {
1251 0 : if (pr->ps_flags & PS_TRACED ||
1252 0 : (pr->ps_pgrp->pg_jobc == 0 &&
1253 0 : prop & SA_TTYSTOP))
1254 : break; /* == ignore */
1255 0 : p->p_xstat = signum;
1256 0 : if (dolock)
1257 0 : SCHED_LOCK(s);
1258 0 : proc_stop(p, 1);
1259 0 : if (dolock)
1260 0 : SCHED_UNLOCK(s);
1261 : break;
1262 0 : } else if (prop & SA_IGNORE) {
1263 : /*
1264 : * Except for SIGCONT, shouldn't get here.
1265 : * Default action is to ignore; drop it.
1266 : */
1267 : break; /* == ignore */
1268 : } else
1269 : goto keep;
1270 : /*NOTREACHED*/
1271 : case (long)SIG_IGN:
1272 : /*
1273 : * Masking above should prevent us ever trying
1274 : * to take action on an ignored signal other
1275 : * than SIGCONT, unless process is traced.
1276 : */
1277 0 : if ((prop & SA_CONT) == 0 &&
1278 0 : (pr->ps_flags & PS_TRACED) == 0)
1279 0 : printf("issignal\n");
1280 : break; /* == ignore */
1281 : default:
1282 : /*
1283 : * This signal has an action, let
1284 : * postsig() process it.
1285 : */
1286 : goto keep;
1287 : }
1288 : }
1289 : /* NOTREACHED */
1290 :
1291 : keep:
1292 0 : atomic_setbits_int(&p->p_siglist, mask); /*leave the signal for later */
1293 0 : return (signum);
1294 0 : }
1295 :
1296 : /*
1297 : * Put the argument process into the stopped state and notify the parent
1298 : * via wakeup. Signals are handled elsewhere. The process must not be
1299 : * on the run queue.
1300 : */
1301 : void
1302 0 : proc_stop(struct proc *p, int sw)
1303 : {
1304 0 : struct process *pr = p->p_p;
1305 : extern void *softclock_si;
1306 :
1307 : #ifdef MULTIPROCESSOR
1308 0 : SCHED_ASSERT_LOCKED();
1309 : #endif
1310 :
1311 0 : p->p_stat = SSTOP;
1312 0 : atomic_clearbits_int(&pr->ps_flags, PS_WAITED);
1313 0 : atomic_setbits_int(&pr->ps_flags, PS_STOPPED);
1314 0 : atomic_setbits_int(&p->p_flag, P_SUSPSIG);
1315 0 : if (!timeout_pending(&proc_stop_to)) {
1316 0 : timeout_add(&proc_stop_to, 0);
1317 : /*
1318 : * We need this soft interrupt to be handled fast.
1319 : * Extra calls to softclock don't hurt.
1320 : */
1321 0 : softintr_schedule(softclock_si);
1322 0 : }
1323 0 : if (sw)
1324 0 : mi_switch();
1325 0 : }
1326 :
1327 : /*
1328 : * Called from a timeout to send signals to the parents of stopped processes.
1329 : * We can't do this in proc_stop because it's called with nasty locks held
1330 : * and we would need recursive scheduler lock to deal with that.
1331 : */
1332 : void
1333 0 : proc_stop_sweep(void *v)
1334 : {
1335 : struct process *pr;
1336 :
1337 0 : LIST_FOREACH(pr, &allprocess, ps_list) {
1338 0 : if ((pr->ps_flags & PS_STOPPED) == 0)
1339 : continue;
1340 0 : atomic_clearbits_int(&pr->ps_flags, PS_STOPPED);
1341 :
1342 0 : if ((pr->ps_pptr->ps_sigacts->ps_flags & SAS_NOCLDSTOP) == 0)
1343 0 : prsignal(pr->ps_pptr, SIGCHLD);
1344 0 : wakeup(pr->ps_pptr);
1345 0 : }
1346 0 : }
1347 :
1348 : /*
1349 : * Take the action for the specified signal
1350 : * from the current set of pending signals.
1351 : */
1352 : void
1353 0 : postsig(struct proc *p, int signum)
1354 : {
1355 0 : struct process *pr = p->p_p;
1356 0 : struct sigacts *ps = pr->ps_sigacts;
1357 : sig_t action;
1358 : u_long trapno;
1359 : int mask, returnmask;
1360 0 : siginfo_t si;
1361 : union sigval sigval;
1362 : int s, code;
1363 :
1364 0 : KASSERT(signum != 0);
1365 0 : KERNEL_ASSERT_LOCKED();
1366 :
1367 0 : mask = sigmask(signum);
1368 0 : atomic_clearbits_int(&p->p_siglist, mask);
1369 0 : action = ps->ps_sigact[signum];
1370 : sigval.sival_ptr = 0;
1371 :
1372 0 : if (p->p_sisig != signum) {
1373 : trapno = 0;
1374 : code = SI_USER;
1375 : sigval.sival_ptr = 0;
1376 0 : } else {
1377 0 : trapno = p->p_sitrapno;
1378 0 : code = p->p_sicode;
1379 0 : sigval = p->p_sigval;
1380 : }
1381 0 : initsiginfo(&si, signum, trapno, code, sigval);
1382 :
1383 : #ifdef KTRACE
1384 0 : if (KTRPOINT(p, KTR_PSIG)) {
1385 0 : ktrpsig(p, signum, action, p->p_flag & P_SIGSUSPEND ?
1386 0 : p->p_oldmask : p->p_sigmask, code, &si);
1387 0 : }
1388 : #endif
1389 0 : if (action == SIG_DFL) {
1390 : /*
1391 : * Default action, where the default is to kill
1392 : * the process. (Other cases were ignored above.)
1393 : */
1394 0 : sigexit(p, signum);
1395 : /* NOTREACHED */
1396 0 : } else {
1397 : /*
1398 : * If we get here, the signal must be caught.
1399 : */
1400 : #ifdef DIAGNOSTIC
1401 0 : if (action == SIG_IGN || (p->p_sigmask & mask))
1402 0 : panic("postsig action");
1403 : #endif
1404 : /*
1405 : * Set the new mask value and also defer further
1406 : * occurrences of this signal.
1407 : *
1408 : * Special case: user has done a sigpause. Here the
1409 : * current mask is not of interest, but rather the
1410 : * mask from before the sigpause is what we want
1411 : * restored after the signal processing is completed.
1412 : */
1413 : #ifdef MULTIPROCESSOR
1414 0 : s = splsched();
1415 : #else
1416 : s = splhigh();
1417 : #endif
1418 0 : if (p->p_flag & P_SIGSUSPEND) {
1419 0 : atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND);
1420 0 : returnmask = p->p_oldmask;
1421 0 : } else {
1422 0 : returnmask = p->p_sigmask;
1423 : }
1424 0 : if (p->p_sisig == signum) {
1425 0 : p->p_sisig = 0;
1426 0 : p->p_sitrapno = 0;
1427 0 : p->p_sicode = SI_USER;
1428 0 : p->p_sigval.sival_ptr = NULL;
1429 0 : }
1430 :
1431 0 : sendsig(action, signum, returnmask, &si);
1432 0 : postsig_done(p, signum, ps);
1433 0 : splx(s);
1434 : }
1435 0 : }
1436 :
1437 : /*
1438 : * Force the current process to exit with the specified signal, dumping core
1439 : * if appropriate. We bypass the normal tests for masked and caught signals,
1440 : * allowing unrecoverable failures to terminate the process without changing
1441 : * signal state. Mark the accounting record with the signal termination.
1442 : * If dumping core, save the signal number for the debugger. Calls exit and
1443 : * does not return.
1444 : */
1445 : void
1446 0 : sigexit(struct proc *p, int signum)
1447 : {
1448 : /* Mark process as going away */
1449 0 : atomic_setbits_int(&p->p_flag, P_WEXIT);
1450 :
1451 0 : p->p_p->ps_acflag |= AXSIG;
1452 0 : if (sigprop[signum] & SA_CORE) {
1453 0 : p->p_sisig = signum;
1454 :
1455 : /* if there are other threads, pause them */
1456 0 : if (P_HASSIBLING(p))
1457 0 : single_thread_set(p, SINGLE_SUSPEND, 0);
1458 :
1459 0 : if (coredump(p) == 0)
1460 0 : signum |= WCOREFLAG;
1461 : }
1462 0 : exit1(p, W_EXITCODE(0, signum), EXIT_NORMAL);
1463 : /* NOTREACHED */
1464 0 : }
1465 :
1466 : int nosuidcoredump = 1;
1467 :
1468 : struct coredump_iostate {
1469 : struct proc *io_proc;
1470 : struct vnode *io_vp;
1471 : struct ucred *io_cred;
1472 : off_t io_offset;
1473 : };
1474 :
1475 : /*
1476 : * Dump core, into a file named "progname.core", unless the process was
1477 : * setuid/setgid.
1478 : */
1479 : int
1480 0 : coredump(struct proc *p)
1481 : {
1482 : #ifdef SMALL_KERNEL
1483 : return EPERM;
1484 : #else
1485 0 : struct process *pr = p->p_p;
1486 : struct vnode *vp;
1487 0 : struct ucred *cred = p->p_ucred;
1488 0 : struct vmspace *vm = p->p_vmspace;
1489 0 : struct nameidata nd;
1490 0 : struct vattr vattr;
1491 0 : struct coredump_iostate io;
1492 : int error, len, incrash = 0;
1493 0 : char name[MAXPATHLEN];
1494 : const char *dir = "/var/crash";
1495 :
1496 0 : if (pr->ps_emul->e_coredump == NULL)
1497 0 : return (EINVAL);
1498 :
1499 0 : pr->ps_flags |= PS_COREDUMP;
1500 :
1501 : /*
1502 : * If the process has inconsistent uids, nosuidcoredump
1503 : * determines coredump placement policy.
1504 : */
1505 0 : if (((pr->ps_flags & PS_SUGID) && (error = suser(p))) ||
1506 0 : ((pr->ps_flags & PS_SUGID) && nosuidcoredump)) {
1507 0 : if (nosuidcoredump == 3 || nosuidcoredump == 2)
1508 : incrash = 1;
1509 : else
1510 0 : return (EPERM);
1511 0 : }
1512 :
1513 : /* Don't dump if will exceed file size limit. */
1514 0 : if (USPACE + ptoa(vm->vm_dsize + vm->vm_ssize) >=
1515 0 : p->p_rlimit[RLIMIT_CORE].rlim_cur)
1516 0 : return (EFBIG);
1517 :
1518 0 : if (incrash && nosuidcoredump == 3) {
1519 : /*
1520 : * If the program directory does not exist, dumps of
1521 : * that core will silently fail.
1522 : */
1523 0 : len = snprintf(name, sizeof(name), "%s/%s/%u.core",
1524 0 : dir, pr->ps_comm, pr->ps_pid);
1525 0 : } else if (incrash && nosuidcoredump == 2)
1526 0 : len = snprintf(name, sizeof(name), "%s/%s.core",
1527 0 : dir, pr->ps_comm);
1528 : else
1529 0 : len = snprintf(name, sizeof(name), "%s.core", pr->ps_comm);
1530 0 : if (len >= sizeof(name))
1531 0 : return (EACCES);
1532 :
1533 : /*
1534 : * Control the UID used to write out. The normal case uses
1535 : * the real UID. If the sugid case is going to write into the
1536 : * controlled directory, we do so as root.
1537 : */
1538 0 : if (incrash == 0) {
1539 0 : cred = crdup(cred);
1540 0 : cred->cr_uid = cred->cr_ruid;
1541 0 : cred->cr_gid = cred->cr_rgid;
1542 0 : } else {
1543 0 : if (p->p_fd->fd_rdir) {
1544 0 : vrele(p->p_fd->fd_rdir);
1545 0 : p->p_fd->fd_rdir = NULL;
1546 0 : }
1547 0 : p->p_ucred = crdup(p->p_ucred);
1548 0 : crfree(cred);
1549 0 : cred = p->p_ucred;
1550 0 : crhold(cred);
1551 0 : cred->cr_uid = 0;
1552 0 : cred->cr_gid = 0;
1553 : }
1554 :
1555 0 : NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
1556 :
1557 0 : error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
1558 :
1559 0 : if (error)
1560 : goto out;
1561 :
1562 : /*
1563 : * Don't dump to non-regular files, files with links, or files
1564 : * owned by someone else.
1565 : */
1566 0 : vp = nd.ni_vp;
1567 0 : if ((error = VOP_GETATTR(vp, &vattr, cred, p)) != 0) {
1568 0 : VOP_UNLOCK(vp);
1569 0 : vn_close(vp, FWRITE, cred, p);
1570 0 : goto out;
1571 : }
1572 0 : if (vp->v_type != VREG || vattr.va_nlink != 1 ||
1573 0 : vattr.va_mode & ((VREAD | VWRITE) >> 3 | (VREAD | VWRITE) >> 6) ||
1574 0 : vattr.va_uid != cred->cr_uid) {
1575 : error = EACCES;
1576 0 : VOP_UNLOCK(vp);
1577 0 : vn_close(vp, FWRITE, cred, p);
1578 0 : goto out;
1579 : }
1580 0 : VATTR_NULL(&vattr);
1581 0 : vattr.va_size = 0;
1582 0 : VOP_SETATTR(vp, &vattr, cred, p);
1583 0 : pr->ps_acflag |= ACORE;
1584 :
1585 0 : io.io_proc = p;
1586 0 : io.io_vp = vp;
1587 0 : io.io_cred = cred;
1588 0 : io.io_offset = 0;
1589 0 : VOP_UNLOCK(vp);
1590 0 : vref(vp);
1591 0 : error = vn_close(vp, FWRITE, cred, p);
1592 0 : if (error == 0)
1593 0 : error = (*pr->ps_emul->e_coredump)(p, &io);
1594 0 : vrele(vp);
1595 : out:
1596 0 : crfree(cred);
1597 0 : return (error);
1598 : #endif
1599 0 : }
1600 :
1601 : #ifndef SMALL_KERNEL
1602 : int
1603 0 : coredump_write(void *cookie, enum uio_seg segflg, const void *data, size_t len)
1604 : {
1605 0 : struct coredump_iostate *io = cookie;
1606 : off_t coffset = 0;
1607 : size_t csize;
1608 : int chunk, error;
1609 :
1610 : csize = len;
1611 0 : do {
1612 0 : if (io->io_proc->p_siglist & sigmask(SIGKILL))
1613 0 : return (EINTR);
1614 :
1615 : /* Rest of the loop sleeps with lock held, so... */
1616 0 : yield();
1617 :
1618 0 : chunk = MIN(csize, MAXPHYS);
1619 0 : error = vn_rdwr(UIO_WRITE, io->io_vp,
1620 0 : (caddr_t)data + coffset, chunk,
1621 0 : io->io_offset + coffset, segflg,
1622 0 : IO_UNIT, io->io_cred, NULL, io->io_proc);
1623 0 : if (error) {
1624 0 : struct process *pr = io->io_proc->p_p;
1625 :
1626 0 : if (error == ENOSPC)
1627 0 : log(LOG_ERR,
1628 : "coredump of %s(%d) failed, filesystem full\n",
1629 : pr->ps_comm, pr->ps_pid);
1630 : else
1631 0 : log(LOG_ERR,
1632 : "coredump of %s(%d), write failed: errno %d\n",
1633 : pr->ps_comm, pr->ps_pid, error);
1634 : return (error);
1635 : }
1636 :
1637 0 : coffset += chunk;
1638 0 : csize -= chunk;
1639 0 : } while (csize > 0);
1640 :
1641 0 : io->io_offset += len;
1642 0 : return (0);
1643 0 : }
1644 :
1645 : void
1646 0 : coredump_unmap(void *cookie, vaddr_t start, vaddr_t end)
1647 : {
1648 0 : struct coredump_iostate *io = cookie;
1649 :
1650 0 : uvm_unmap(&io->io_proc->p_vmspace->vm_map, start, end);
1651 0 : }
1652 :
1653 : #endif /* !SMALL_KERNEL */
1654 :
1655 : /*
1656 : * Nonexistent system call-- signal process (may want to handle it).
1657 : * Flag error in case process won't see signal immediately (blocked or ignored).
1658 : */
1659 : int
1660 0 : sys_nosys(struct proc *p, void *v, register_t *retval)
1661 : {
1662 :
1663 0 : ptsignal(p, SIGSYS, STHREAD);
1664 0 : return (ENOSYS);
1665 : }
1666 :
1667 : int
1668 0 : sys___thrsigdivert(struct proc *p, void *v, register_t *retval)
1669 : {
1670 : static int sigwaitsleep;
1671 : struct sys___thrsigdivert_args /* {
1672 : syscallarg(sigset_t) sigmask;
1673 : syscallarg(siginfo_t *) info;
1674 : syscallarg(const struct timespec *) timeout;
1675 0 : } */ *uap = v;
1676 0 : struct process *pr = p->p_p;
1677 : sigset_t *m;
1678 0 : sigset_t mask = SCARG(uap, sigmask) &~ sigcantmask;
1679 0 : siginfo_t si;
1680 : uint64_t to_ticks = 0;
1681 : int timeinvalid = 0;
1682 : int error = 0;
1683 :
1684 0 : memset(&si, 0, sizeof(si));
1685 :
1686 0 : if (SCARG(uap, timeout) != NULL) {
1687 0 : struct timespec ts;
1688 0 : if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0)
1689 0 : return (error);
1690 : #ifdef KTRACE
1691 0 : if (KTRPOINT(p, KTR_STRUCT))
1692 0 : ktrreltimespec(p, &ts);
1693 : #endif
1694 0 : if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000)
1695 0 : timeinvalid = 1;
1696 : else {
1697 0 : to_ticks = (uint64_t)hz * ts.tv_sec +
1698 0 : ts.tv_nsec / (tick * 1000);
1699 0 : if (to_ticks > INT_MAX)
1700 : to_ticks = INT_MAX;
1701 0 : if (to_ticks == 0 && ts.tv_nsec)
1702 0 : to_ticks = 1;
1703 : }
1704 0 : }
1705 :
1706 0 : dosigsuspend(p, p->p_sigmask &~ mask);
1707 0 : for (;;) {
1708 0 : si.si_signo = CURSIG(p);
1709 0 : if (si.si_signo != 0) {
1710 0 : sigset_t smask = sigmask(si.si_signo);
1711 0 : if (smask & mask) {
1712 0 : if (p->p_siglist & smask)
1713 0 : m = &p->p_siglist;
1714 0 : else if (pr->ps_mainproc->p_siglist & smask)
1715 : m = &pr->ps_mainproc->p_siglist;
1716 : else {
1717 : /* signal got eaten by someone else? */
1718 0 : continue;
1719 : }
1720 0 : atomic_clearbits_int(m, smask);
1721 : error = 0;
1722 0 : break;
1723 : }
1724 0 : }
1725 :
1726 : /* per-POSIX, delay this error until after the above */
1727 0 : if (timeinvalid)
1728 0 : error = EINVAL;
1729 :
1730 0 : if (SCARG(uap, timeout) != NULL && to_ticks == 0)
1731 0 : error = EAGAIN;
1732 :
1733 0 : if (error != 0)
1734 : break;
1735 :
1736 0 : error = tsleep(&sigwaitsleep, PPAUSE|PCATCH, "sigwait",
1737 0 : (int)to_ticks);
1738 : }
1739 :
1740 0 : if (error == 0) {
1741 0 : *retval = si.si_signo;
1742 0 : if (SCARG(uap, info) != NULL)
1743 0 : error = copyout(&si, SCARG(uap, info), sizeof(si));
1744 0 : } else if (error == ERESTART && SCARG(uap, timeout) != NULL) {
1745 : /*
1746 : * Restarting is wrong if there's a timeout, as it'll be
1747 : * for the same interval again
1748 : */
1749 : error = EINTR;
1750 0 : }
1751 :
1752 0 : return (error);
1753 0 : }
1754 :
1755 : void
1756 0 : initsiginfo(siginfo_t *si, int sig, u_long trapno, int code, union sigval val)
1757 : {
1758 0 : memset(si, 0, sizeof(*si));
1759 :
1760 0 : si->si_signo = sig;
1761 0 : si->si_code = code;
1762 0 : if (code == SI_USER) {
1763 0 : si->si_value = val;
1764 0 : } else {
1765 0 : switch (sig) {
1766 : case SIGSEGV:
1767 : case SIGILL:
1768 : case SIGBUS:
1769 : case SIGFPE:
1770 0 : si->si_addr = val.sival_ptr;
1771 0 : si->si_trapno = trapno;
1772 0 : break;
1773 : case SIGXFSZ:
1774 : break;
1775 : }
1776 : }
1777 0 : }
1778 :
1779 : int
1780 0 : filt_sigattach(struct knote *kn)
1781 : {
1782 0 : struct process *pr = curproc->p_p;
1783 :
1784 0 : if (kn->kn_id >= NSIG)
1785 0 : return EINVAL;
1786 :
1787 0 : kn->kn_ptr.p_process = pr;
1788 0 : kn->kn_flags |= EV_CLEAR; /* automatically set */
1789 :
1790 : /* XXX lock the proc here while adding to the list? */
1791 0 : SLIST_INSERT_HEAD(&pr->ps_klist, kn, kn_selnext);
1792 :
1793 0 : return (0);
1794 0 : }
1795 :
1796 : void
1797 0 : filt_sigdetach(struct knote *kn)
1798 : {
1799 0 : struct process *pr = kn->kn_ptr.p_process;
1800 :
1801 0 : SLIST_REMOVE(&pr->ps_klist, kn, knote, kn_selnext);
1802 0 : }
1803 :
1804 : /*
1805 : * signal knotes are shared with proc knotes, so we apply a mask to
1806 : * the hint in order to differentiate them from process hints. This
1807 : * could be avoided by using a signal-specific knote list, but probably
1808 : * isn't worth the trouble.
1809 : */
1810 : int
1811 0 : filt_signal(struct knote *kn, long hint)
1812 : {
1813 :
1814 0 : if (hint & NOTE_SIGNAL) {
1815 0 : hint &= ~NOTE_SIGNAL;
1816 :
1817 0 : if (kn->kn_id == hint)
1818 0 : kn->kn_data++;
1819 : }
1820 0 : return (kn->kn_data != 0);
1821 : }
1822 :
1823 : void
1824 0 : userret(struct proc *p)
1825 : {
1826 : int signum;
1827 :
1828 : /* send SIGPROF or SIGVTALRM if their timers interrupted this thread */
1829 88 : if (p->p_flag & P_PROFPEND) {
1830 0 : atomic_clearbits_int(&p->p_flag, P_PROFPEND);
1831 0 : KERNEL_LOCK();
1832 0 : psignal(p, SIGPROF);
1833 0 : KERNEL_UNLOCK();
1834 0 : }
1835 88 : if (p->p_flag & P_ALRMPEND) {
1836 0 : atomic_clearbits_int(&p->p_flag, P_ALRMPEND);
1837 0 : KERNEL_LOCK();
1838 0 : psignal(p, SIGVTALRM);
1839 0 : KERNEL_UNLOCK();
1840 0 : }
1841 :
1842 173 : if (SIGPENDING(p)) {
1843 0 : KERNEL_LOCK();
1844 0 : while ((signum = CURSIG(p)) != 0)
1845 0 : postsig(p, signum);
1846 0 : KERNEL_UNLOCK();
1847 0 : }
1848 :
1849 : /*
1850 : * If P_SIGSUSPEND is still set here, then we still need to restore
1851 : * the original sigmask before returning to userspace. Also, this
1852 : * might unmask some pending signals, so we need to check a second
1853 : * time for signals to post.
1854 : */
1855 0 : if (p->p_flag & P_SIGSUSPEND) {
1856 0 : atomic_clearbits_int(&p->p_flag, P_SIGSUSPEND);
1857 0 : p->p_sigmask = p->p_oldmask;
1858 :
1859 0 : KERNEL_LOCK();
1860 0 : while ((signum = CURSIG(p)) != 0)
1861 0 : postsig(p, signum);
1862 0 : KERNEL_UNLOCK();
1863 0 : }
1864 :
1865 86 : if (p->p_flag & P_SUSPSINGLE) {
1866 0 : KERNEL_LOCK();
1867 0 : single_thread_check(p, 0);
1868 0 : KERNEL_UNLOCK();
1869 0 : }
1870 :
1871 : WITNESS_WARN(WARN_PANIC, NULL, "userret: returning");
1872 :
1873 85 : p->p_cpu->ci_schedstate.spc_curpriority = p->p_priority = p->p_usrpri;
1874 0 : }
1875 :
1876 : int
1877 0 : single_thread_check(struct proc *p, int deep)
1878 : {
1879 0 : struct process *pr = p->p_p;
1880 :
1881 0 : if (pr->ps_single != NULL && pr->ps_single != p) {
1882 0 : do {
1883 : int s;
1884 :
1885 : /* if we're in deep, we need to unwind to the edge */
1886 0 : if (deep) {
1887 0 : if (pr->ps_flags & PS_SINGLEUNWIND)
1888 0 : return (ERESTART);
1889 0 : if (pr->ps_flags & PS_SINGLEEXIT)
1890 0 : return (EINTR);
1891 : }
1892 :
1893 0 : if (--pr->ps_singlecount == 0)
1894 0 : wakeup(&pr->ps_singlecount);
1895 0 : if (pr->ps_flags & PS_SINGLEEXIT)
1896 0 : exit1(p, 0, EXIT_THREAD_NOCHECK);
1897 :
1898 : /* not exiting and don't need to unwind, so suspend */
1899 0 : SCHED_LOCK(s);
1900 0 : p->p_stat = SSTOP;
1901 0 : mi_switch();
1902 0 : SCHED_UNLOCK(s);
1903 0 : } while (pr->ps_single != NULL);
1904 : }
1905 :
1906 0 : return (0);
1907 0 : }
1908 :
1909 : /*
1910 : * Stop other threads in the process. The mode controls how and
1911 : * where the other threads should stop:
1912 : * - SINGLE_SUSPEND: stop wherever they are, will later either be told to exit
1913 : * (by setting to SINGLE_EXIT) or be released (via single_thread_clear())
1914 : * - SINGLE_PTRACE: stop wherever they are, will wait for them to stop
1915 : * later (via single_thread_wait()) and released as with SINGLE_SUSPEND
1916 : * - SINGLE_UNWIND: just unwind to kernel boundary, will be told to exit
1917 : * or released as with SINGLE_SUSPEND
1918 : * - SINGLE_EXIT: unwind to kernel boundary and exit
1919 : */
1920 : int
1921 0 : single_thread_set(struct proc *p, enum single_thread_mode mode, int deep)
1922 : {
1923 0 : struct process *pr = p->p_p;
1924 : struct proc *q;
1925 : int error;
1926 :
1927 0 : KERNEL_ASSERT_LOCKED();
1928 :
1929 0 : if ((error = single_thread_check(p, deep)))
1930 0 : return error;
1931 :
1932 0 : switch (mode) {
1933 : case SINGLE_SUSPEND:
1934 : case SINGLE_PTRACE:
1935 : break;
1936 : case SINGLE_UNWIND:
1937 0 : atomic_setbits_int(&pr->ps_flags, PS_SINGLEUNWIND);
1938 0 : break;
1939 : case SINGLE_EXIT:
1940 0 : atomic_setbits_int(&pr->ps_flags, PS_SINGLEEXIT);
1941 0 : atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND);
1942 0 : break;
1943 : #ifdef DIAGNOSTIC
1944 : default:
1945 0 : panic("single_thread_mode = %d", mode);
1946 : #endif
1947 : }
1948 0 : pr->ps_single = p;
1949 0 : pr->ps_singlecount = 0;
1950 0 : TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
1951 : int s;
1952 :
1953 0 : if (q == p)
1954 0 : continue;
1955 0 : if (q->p_flag & P_WEXIT) {
1956 0 : if (mode == SINGLE_EXIT) {
1957 0 : SCHED_LOCK(s);
1958 0 : if (q->p_stat == SSTOP) {
1959 0 : setrunnable(q);
1960 0 : pr->ps_singlecount++;
1961 0 : }
1962 0 : SCHED_UNLOCK(s);
1963 0 : }
1964 0 : continue;
1965 : }
1966 0 : SCHED_LOCK(s);
1967 0 : atomic_setbits_int(&q->p_flag, P_SUSPSINGLE);
1968 0 : switch (q->p_stat) {
1969 : case SIDL:
1970 : case SRUN:
1971 0 : pr->ps_singlecount++;
1972 0 : break;
1973 : case SSLEEP:
1974 : /* if it's not interruptible, then just have to wait */
1975 0 : if (q->p_flag & P_SINTR) {
1976 : /* merely need to suspend? just stop it */
1977 0 : if (mode == SINGLE_SUSPEND ||
1978 0 : mode == SINGLE_PTRACE) {
1979 0 : q->p_stat = SSTOP;
1980 0 : break;
1981 : }
1982 : /* need to unwind or exit, so wake it */
1983 0 : setrunnable(q);
1984 0 : }
1985 0 : pr->ps_singlecount++;
1986 0 : break;
1987 : case SSTOP:
1988 0 : if (mode == SINGLE_EXIT) {
1989 0 : setrunnable(q);
1990 0 : pr->ps_singlecount++;
1991 0 : }
1992 : break;
1993 : case SDEAD:
1994 : break;
1995 : case SONPROC:
1996 0 : pr->ps_singlecount++;
1997 0 : signotify(q);
1998 0 : break;
1999 : }
2000 0 : SCHED_UNLOCK(s);
2001 0 : }
2002 :
2003 0 : if (mode != SINGLE_PTRACE)
2004 0 : single_thread_wait(pr);
2005 :
2006 0 : return 0;
2007 0 : }
2008 :
2009 : void
2010 0 : single_thread_wait(struct process *pr)
2011 : {
2012 : /* wait until they're all suspended */
2013 0 : while (pr->ps_singlecount > 0)
2014 0 : tsleep(&pr->ps_singlecount, PUSER, "suspend", 0);
2015 0 : }
2016 :
2017 : void
2018 0 : single_thread_clear(struct proc *p, int flag)
2019 : {
2020 0 : struct process *pr = p->p_p;
2021 : struct proc *q;
2022 :
2023 0 : KASSERT(pr->ps_single == p);
2024 0 : KERNEL_ASSERT_LOCKED();
2025 :
2026 0 : pr->ps_single = NULL;
2027 0 : atomic_clearbits_int(&pr->ps_flags, PS_SINGLEUNWIND | PS_SINGLEEXIT);
2028 0 : TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
2029 : int s;
2030 :
2031 0 : if (q == p || (q->p_flag & P_SUSPSINGLE) == 0)
2032 0 : continue;
2033 0 : atomic_clearbits_int(&q->p_flag, P_SUSPSINGLE);
2034 :
2035 : /*
2036 : * if the thread was only stopped for single threading
2037 : * then clearing that either makes it runnable or puts
2038 : * it back into some sleep queue
2039 : */
2040 0 : SCHED_LOCK(s);
2041 0 : if (q->p_stat == SSTOP && (q->p_flag & flag) == 0) {
2042 0 : if (q->p_wchan == 0)
2043 0 : setrunnable(q);
2044 : else
2045 0 : q->p_stat = SSLEEP;
2046 : }
2047 0 : SCHED_UNLOCK(s);
2048 0 : }
2049 0 : }
|