xref: /csrg-svn/sys/kern/kern_sig.c (revision 21016)
1 /*	kern_sig.c	6.16	85/05/22	*/
2 
3 #include "../machine/reg.h"
4 #include "../machine/pte.h"
5 #include "../machine/psl.h"
6 
7 #include "param.h"
8 #include "systm.h"
9 #include "dir.h"
10 #include "user.h"
11 #include "inode.h"
12 #include "proc.h"
13 #include "timeb.h"
14 #include "times.h"
15 #include "buf.h"
16 #include "mount.h"
17 #include "text.h"
18 #include "seg.h"
19 #include "vm.h"
20 #include "acct.h"
21 #include "uio.h"
22 #include "kernel.h"
23 
24 #define	cantmask	(sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP))
25 
26 /*
27  * Generalized interface signal handler.
28  */
29 sigvec()
30 {
31 	register struct a {
32 		int	signo;
33 		struct	sigvec *nsv;
34 		struct	sigvec *osv;
35 	} *uap = (struct a  *)u.u_ap;
36 	struct sigvec vec;
37 	register struct sigvec *sv;
38 	register int sig;
39 	int bit;
40 
41 	sig = uap->signo;
42 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) {
43 		u.u_error = EINVAL;
44 		return;
45 	}
46 	sv = &vec;
47 	if (uap->osv) {
48 		sv->sv_handler = u.u_signal[sig];
49 		sv->sv_mask = u.u_sigmask[sig];
50 		bit = sigmask(sig);
51 		sv->sv_flags = 0;
52 		if ((u.u_sigonstack & bit) != 0)
53 			sv->sv_flags |= SV_ONSTACK;
54 		if ((u.u_sigintr & bit) != 0)
55 			sv->sv_flags |= SV_INTERRUPT;
56 		u.u_error =
57 		    copyout((caddr_t)sv, (caddr_t)uap->osv, sizeof (vec));
58 		if (u.u_error)
59 			return;
60 	}
61 	if (uap->nsv) {
62 		u.u_error =
63 		    copyin((caddr_t)uap->nsv, (caddr_t)sv, sizeof (vec));
64 		if (u.u_error)
65 			return;
66 		if (sig == SIGCONT && sv->sv_handler == SIG_IGN) {
67 			u.u_error = EINVAL;
68 			return;
69 		}
70 		setsigvec(sig, sv);
71 	}
72 }
73 
74 setsigvec(sig, sv)
75 	int sig;
76 	register struct sigvec *sv;
77 {
78 	register struct proc *p;
79 	register int bit;
80 
81 	bit = sigmask(sig);
82 	p = u.u_procp;
83 	/*
84 	 * Change setting atomically.
85 	 */
86 	(void) splhigh();
87 	u.u_signal[sig] = sv->sv_handler;
88 	u.u_sigmask[sig] = sv->sv_mask &~ cantmask;
89 	if (sv->sv_flags & SV_INTERRUPT)
90 		u.u_sigintr |= bit;
91 	else
92 		u.u_sigintr &= ~bit;
93 	if (sv->sv_flags & SV_ONSTACK)
94 		u.u_sigonstack |= bit;
95 	else
96 		u.u_sigonstack &= ~bit;
97 	if (sv->sv_handler == SIG_IGN) {
98 		p->p_sig &= ~bit;		/* never to be seen again */
99 		p->p_sigignore |= bit;
100 		p->p_sigcatch &= ~bit;
101 	} else {
102 		p->p_sigignore &= ~bit;
103 		if (sv->sv_handler == SIG_DFL)
104 			p->p_sigcatch &= ~bit;
105 		else
106 			p->p_sigcatch |= bit;
107 	}
108 	(void) spl0();
109 }
110 
111 sigblock()
112 {
113 	struct a {
114 		int	mask;
115 	} *uap = (struct a *)u.u_ap;
116 	register struct proc *p = u.u_procp;
117 
118 	(void) splhigh();
119 	u.u_r.r_val1 = p->p_sigmask;
120 	p->p_sigmask |= uap->mask &~ cantmask;
121 	(void) spl0();
122 }
123 
124 sigsetmask()
125 {
126 	struct a {
127 		int	mask;
128 	} *uap = (struct a *)u.u_ap;
129 	register struct proc *p = u.u_procp;
130 
131 	(void) splhigh();
132 	u.u_r.r_val1 = p->p_sigmask;
133 	p->p_sigmask = uap->mask &~ cantmask;
134 	(void) spl0();
135 }
136 
137 sigpause()
138 {
139 	struct a {
140 		int	mask;
141 	} *uap = (struct a *)u.u_ap;
142 	register struct proc *p = u.u_procp;
143 
144 	/*
145 	 * When returning from sigpause, we want
146 	 * the old mask to be restored after the
147 	 * signal handler has finished.  Thus, we
148 	 * save it here and mark the proc structure
149 	 * to indicate this (should be in u.).
150 	 */
151 	u.u_oldmask = p->p_sigmask;
152 	p->p_flag |= SOMASK;
153 	p->p_sigmask = uap->mask &~ cantmask;
154 	for (;;)
155 		sleep((caddr_t)&u, PSLEP);
156 	/*NOTREACHED*/
157 }
158 #undef cantmask
159 
160 sigstack()
161 {
162 	register struct a {
163 		struct	sigstack *nss;
164 		struct	sigstack *oss;
165 	} *uap = (struct a *)u.u_ap;
166 	struct sigstack ss;
167 
168 	if (uap->oss) {
169 		u.u_error = copyout((caddr_t)&u.u_sigstack, (caddr_t)uap->oss,
170 		    sizeof (struct sigstack));
171 		if (u.u_error)
172 			return;
173 	}
174 	if (uap->nss) {
175 		u.u_error =
176 		    copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss));
177 		if (u.u_error == 0)
178 			u.u_sigstack = ss;
179 	}
180 }
181 
182 kill()
183 {
184 	register struct a {
185 		int	pid;
186 		int	signo;
187 	} *uap = (struct a *)u.u_ap;
188 	register struct proc *p;
189 
190 	if (uap->signo < 0 || uap->signo > NSIG) {
191 		u.u_error = EINVAL;
192 		return;
193 	}
194 	if (uap->pid > 0) {
195 		/* kill single process */
196 		p = pfind(uap->pid);
197 		if (p == 0) {
198 			u.u_error = ESRCH;
199 			return;
200 		}
201 		if (u.u_uid && u.u_uid != p->p_uid)
202 			u.u_error = EPERM;
203 		else if (uap->signo)
204 			psignal(p, uap->signo);
205 		return;
206 	}
207 	switch (uap->pid) {
208 	case -1:		/* broadcast signal */
209 		u.u_error = killpg1(uap->signo, 0, 1);
210 		break;
211 	case 0:			/* signal own process group */
212 		u.u_error = killpg1(uap->signo, 0, 0);
213 		break;
214 	default:		/* negative explicit process group */
215 		u.u_error = killpg1(uap->signo, -uap->pid, 0);
216 		break;
217 	}
218 	return;
219 }
220 
221 killpg()
222 {
223 	register struct a {
224 		int	pgrp;
225 		int	signo;
226 	} *uap = (struct a *)u.u_ap;
227 
228 	if (uap->signo < 0 || uap->signo > NSIG) {
229 		u.u_error = EINVAL;
230 		return;
231 	}
232 	u.u_error = killpg1(uap->signo, uap->pgrp, 0);
233 }
234 
235 /* KILL CODE SHOULDNT KNOW ABOUT PROCESS INTERNALS !?! */
236 
237 killpg1(signo, pgrp, all)
238 	int signo, pgrp, all;
239 {
240 	register struct proc *p;
241 	int f, error = 0;
242 
243 	if (!all && pgrp == 0) {
244 		/*
245 		 * Zero process id means send to my process group.
246 		 */
247 		pgrp = u.u_procp->p_pgrp;
248 		if (pgrp == 0)
249 			return (ESRCH);
250 	}
251 	for (f = 0, p = allproc; p != NULL; p = p->p_nxt) {
252 		if ((p->p_pgrp != pgrp && !all) || p->p_ppid == 0 ||
253 		    (p->p_flag&SSYS) || (all && p == u.u_procp))
254 			continue;
255 		if (u.u_uid != 0 && u.u_uid != p->p_uid &&
256 		    (signo != SIGCONT || !inferior(p))) {
257 			if (!all)
258 				error = EPERM;
259 			continue;
260 		}
261 		f++;
262 		if (signo)
263 			psignal(p, signo);
264 	}
265 	return (f == 0 ? ESRCH : error);
266 }
267 
268 /*
269  * Send the specified signal to
270  * all processes with 'pgrp' as
271  * process group.
272  */
273 gsignal(pgrp, sig)
274 	register int pgrp;
275 {
276 	register struct proc *p;
277 
278 	if (pgrp == 0)
279 		return;
280 	for (p = allproc; p != NULL; p = p->p_nxt)
281 		if (p->p_pgrp == pgrp)
282 			psignal(p, sig);
283 }
284 
285 /*
286  * Send the specified signal to
287  * the specified process.
288  */
289 psignal(p, sig)
290 	register struct proc *p;
291 	register int sig;
292 {
293 	register int s;
294 	register int (*action)();
295 	int mask;
296 
297 	if ((unsigned)sig >= NSIG)
298 		return;
299 	mask = sigmask(sig);
300 
301 	/*
302 	 * If proc is traced, always give parent a chance.
303 	 */
304 	if (p->p_flag & STRC)
305 		action = SIG_DFL;
306 	else {
307 		/*
308 		 * If the signal is being ignored,
309 		 * then we forget about it immediately.
310 		 */
311 		if (p->p_sigignore & mask)
312 			return;
313 		if (p->p_sigmask & mask)
314 			action = SIG_HOLD;
315 		else if (p->p_sigcatch & mask)
316 			action = SIG_CATCH;
317 		else
318 			action = SIG_DFL;
319 	}
320 #define	stops	(sigmask(SIGSTOP)|sigmask(SIGTSTP)| \
321 			sigmask(SIGTTIN)|sigmask(SIGTTOU))
322 	if (sig) {
323 		p->p_sig |= mask;
324 		switch (sig) {
325 
326 		case SIGTERM:
327 			if ((p->p_flag&STRC) || action != SIG_DFL)
328 				break;
329 			/* fall into ... */
330 
331 		case SIGKILL:
332 			if (p->p_nice > NZERO)
333 				p->p_nice = NZERO;
334 			break;
335 
336 		case SIGCONT:
337 			p->p_sig &= ~stops;
338 			break;
339 
340 		case SIGSTOP:
341 		case SIGTSTP:
342 		case SIGTTIN:
343 		case SIGTTOU:
344 			p->p_sig &= ~sigmask(SIGCONT);
345 			break;
346 		}
347 	}
348 #undef stops
349 	/*
350 	 * Defer further processing for signals which are held.
351 	 */
352 	if (action == SIG_HOLD)
353 		return;
354 	s = splhigh();
355 	switch (p->p_stat) {
356 
357 	case SSLEEP:
358 		/*
359 		 * If process is sleeping at negative priority
360 		 * we can't interrupt the sleep... the signal will
361 		 * be noticed when the process returns through
362 		 * trap() or syscall().
363 		 */
364 		if (p->p_pri <= PZERO)
365 			goto out;
366 		/*
367 		 * Process is sleeping and traced... make it runnable
368 		 * so it can discover the signal in issig() and stop
369 		 * for the parent.
370 		 */
371 		if (p->p_flag&STRC)
372 			goto run;
373 		switch (sig) {
374 
375 		case SIGSTOP:
376 		case SIGTSTP:
377 		case SIGTTIN:
378 		case SIGTTOU:
379 			/*
380 			 * These are the signals which by default
381 			 * stop a process.
382 			 */
383 			if (action != SIG_DFL)
384 				goto run;
385 			/*
386 			 * Don't clog system with children of init
387 			 * stopped from the keyboard.
388 			 */
389 			if (sig != SIGSTOP && p->p_pptr == &proc[1]) {
390 				psignal(p, SIGKILL);
391 				p->p_sig &= ~mask;
392 				splx(s);
393 				return;
394 			}
395 			/*
396 			 * If a child in vfork(), stopping could
397 			 * cause deadlock.
398 			 */
399 			if (p->p_flag&SVFORK)
400 				goto out;
401 			p->p_sig &= ~mask;
402 			p->p_cursig = sig;
403 			psignal(p->p_pptr, SIGCHLD);
404 			stop(p);
405 			goto out;
406 
407 		case SIGIO:
408 		case SIGURG:
409 		case SIGCHLD:
410 		case SIGWINCH:
411 			/*
412 			 * These signals are special in that they
413 			 * don't get propogated... if the process
414 			 * isn't interested, forget it.
415 			 */
416 			if (action != SIG_DFL)
417 				goto run;
418 			p->p_sig &= ~mask;		/* take it away */
419 			goto out;
420 
421 		default:
422 			/*
423 			 * All other signals cause the process to run
424 			 */
425 			goto run;
426 		}
427 		/*NOTREACHED*/
428 
429 	case SSTOP:
430 		/*
431 		 * If traced process is already stopped,
432 		 * then no further action is necessary.
433 		 */
434 		if (p->p_flag&STRC)
435 			goto out;
436 		switch (sig) {
437 
438 		case SIGKILL:
439 			/*
440 			 * Kill signal always sets processes running.
441 			 */
442 			goto run;
443 
444 		case SIGCONT:
445 			/*
446 			 * If the process catches SIGCONT, let it handle
447 			 * the signal itself.  If it isn't waiting on
448 			 * an event, then it goes back to run state.
449 			 * Otherwise, process goes back to sleep state.
450 			 */
451 			if (action != SIG_DFL || p->p_wchan == 0)
452 				goto run;
453 			p->p_stat = SSLEEP;
454 			goto out;
455 
456 		case SIGSTOP:
457 		case SIGTSTP:
458 		case SIGTTIN:
459 		case SIGTTOU:
460 			/*
461 			 * Already stopped, don't need to stop again.
462 			 * (If we did the shell could get confused.)
463 			 */
464 			p->p_sig &= ~mask;		/* take it away */
465 			goto out;
466 
467 		default:
468 			/*
469 			 * If process is sleeping interruptibly, then
470 			 * unstick it so that when it is continued
471 			 * it can look at the signal.
472 			 * But don't setrun the process as its not to
473 			 * be unstopped by the signal alone.
474 			 */
475 			if (p->p_wchan && p->p_pri > PZERO)
476 				unsleep(p);
477 			goto out;
478 		}
479 		/*NOTREACHED*/
480 
481 	default:
482 		/*
483 		 * SRUN, SIDL, SZOMB do nothing with the signal,
484 		 * other than kicking ourselves if we are running.
485 		 * It will either never be noticed, or noticed very soon.
486 		 */
487 		if (p == u.u_procp && !noproc)
488 #include "../vax/mtpr.h"
489 			aston();
490 		goto out;
491 	}
492 	/*NOTREACHED*/
493 run:
494 	/*
495 	 * Raise priority to at least PUSER.
496 	 */
497 	if (p->p_pri > PUSER)
498 		p->p_pri = PUSER;
499 	setrun(p);
500 out:
501 	splx(s);
502 }
503 
504 /*
505  * Returns true if the current
506  * process has a signal to process.
507  * The signal to process is put in p_cursig.
508  * This is asked at least once each time a process enters the
509  * system (though this can usually be done without actually
510  * calling issig by checking the pending signal masks.)
511  * A signal does not do anything
512  * directly to a process; it sets
513  * a flag that asks the process to
514  * do something to itself.
515  */
516 issig()
517 {
518 	register struct proc *p;
519 	register int sig;
520 	int sigbits, mask;
521 
522 	p = u.u_procp;
523 	for (;;) {
524 		sigbits = p->p_sig &~ p->p_sigmask;
525 		if ((p->p_flag&STRC) == 0)
526 			sigbits &= ~p->p_sigignore;
527 		if (p->p_flag&SVFORK)
528 #define bit(a) (1<<(a-1))
529 			sigbits &= ~(bit(SIGSTOP)|bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU));
530 		if (sigbits == 0)
531 			break;
532 		sig = ffs(sigbits);
533 		mask = sigmask(sig);
534 		p->p_sig &= ~mask;		/* take the signal! */
535 		p->p_cursig = sig;
536 		if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
537 			/*
538 			 * If traced, always stop, and stay
539 			 * stopped until released by the parent.
540 			 */
541 			psignal(p->p_pptr, SIGCHLD);
542 			do {
543 				stop(p);
544 				swtch();
545 			} while (!procxmt() && p->p_flag&STRC);
546 
547 			/*
548 			 * If the traced bit got turned off,
549 			 * then put the signal taken above back into p_sig
550 			 * and go back up to the top to rescan signals.
551 			 * This ensures that p_sig* and u_signal are consistent.
552 			 */
553 			if ((p->p_flag&STRC) == 0) {
554 				p->p_sig |= mask;
555 				continue;
556 			}
557 
558 			/*
559 			 * If parent wants us to take the signal,
560 			 * then it will leave it in p->p_cursig;
561 			 * otherwise we just look for signals again.
562 			 */
563 			sig = p->p_cursig;
564 			if (sig == 0)
565 				continue;
566 
567 			/*
568 			 * If signal is being masked put it back
569 			 * into p_sig and look for other signals.
570 			 */
571 			mask = sigmask(sig);
572 			if (p->p_sigmask & mask) {
573 				p->p_sig |= mask;
574 				continue;
575 			}
576 		}
577 		switch (u.u_signal[sig]) {
578 
579 		case SIG_DFL:
580 			/*
581 			 * Don't take default actions on system processes.
582 			 */
583 			if (p->p_ppid == 0)
584 				break;
585 			switch (sig) {
586 
587 			case SIGTSTP:
588 			case SIGTTIN:
589 			case SIGTTOU:
590 				/*
591 				 * Children of init aren't allowed to stop
592 				 * on signals from the keyboard.
593 				 */
594 				if (p->p_pptr == &proc[1]) {
595 					psignal(p, SIGKILL);
596 					continue;
597 				}
598 				/* fall into ... */
599 
600 			case SIGSTOP:
601 				if (p->p_flag&STRC)
602 					continue;
603 				psignal(p->p_pptr, SIGCHLD);
604 				stop(p);
605 				swtch();
606 				continue;
607 
608 			case SIGCONT:
609 			case SIGCHLD:
610 			case SIGURG:
611 			case SIGIO:
612 			case SIGWINCH:
613 				/*
614 				 * These signals are normally not
615 				 * sent if the action is the default.
616 				 */
617 				continue;		/* == ignore */
618 
619 			default:
620 				goto send;
621 			}
622 			/*NOTREACHED*/
623 
624 		case SIG_HOLD:
625 		case SIG_IGN:
626 			/*
627 			 * Masking above should prevent us
628 			 * ever trying to take action on a held
629 			 * or ignored signal, unless process is traced.
630 			 */
631 			if ((p->p_flag&STRC) == 0)
632 				printf("issig\n");
633 			continue;
634 
635 		default:
636 			/*
637 			 * This signal has an action, let
638 			 * psig process it.
639 			 */
640 			goto send;
641 		}
642 		/*NOTREACHED*/
643 	}
644 	/*
645 	 * Didn't find a signal to send.
646 	 */
647 	p->p_cursig = 0;
648 	return (0);
649 
650 send:
651 	/*
652 	 * Let psig process the signal.
653 	 */
654 	return (sig);
655 }
656 
657 /*
658  * Put the argument process into the stopped
659  * state and notify the parent via wakeup.
660  * Signals are handled elsewhere.
661  */
662 stop(p)
663 	register struct proc *p;
664 {
665 
666 	p->p_stat = SSTOP;
667 	p->p_flag &= ~SWTED;
668 	wakeup((caddr_t)p->p_pptr);
669 }
670 
671 /*
672  * Perform the action specified by
673  * the current signal.
674  * The usual sequence is:
675  *	if (issig())
676  *		psig();
677  * The signal bit has already been cleared by issig,
678  * and the current signal number stored in p->p_cursig.
679  */
680 psig()
681 {
682 	register struct proc *p = u.u_procp;
683 	register int sig = p->p_cursig;
684 	int mask = sigmask(sig), returnmask;
685 	register int (*action)();
686 
687 	if (sig == 0)
688 		panic("psig");
689 	action = u.u_signal[sig];
690 	if (action != SIG_DFL) {
691 		if (action == SIG_IGN || (p->p_sigmask & mask))
692 			panic("psig action");
693 		u.u_error = 0;
694 		/*
695 		 * Set the new mask value and also defer further
696 		 * occurences of this signal (unless we're simulating
697 		 * the old signal facilities).
698 		 *
699 		 * Special case: user has done a sigpause.  Here the
700 		 * current mask is not of interest, but rather the
701 		 * mask from before the sigpause is what we want restored
702 		 * after the signal processing is completed.
703 		 */
704 		(void) splhigh();
705 		if (p->p_flag & SOUSIG) {
706 			if (sig != SIGILL && sig != SIGTRAP) {
707 				u.u_signal[sig] = SIG_DFL;
708 				p->p_sigcatch &= ~mask;
709 			}
710 			mask = 0;
711 		}
712 		if (p->p_flag & SOMASK) {
713 			returnmask = u.u_oldmask;
714 			p->p_flag &= ~SOMASK;
715 		} else
716 			returnmask = p->p_sigmask;
717 		p->p_sigmask |= u.u_sigmask[sig] | mask;
718 		(void) spl0();
719 		u.u_ru.ru_nsignals++;
720 		sendsig(action, sig, returnmask);
721 		p->p_cursig = 0;
722 		return;
723 	}
724 	u.u_acflag |= AXSIG;
725 	switch (sig) {
726 
727 	case SIGILL:
728 	case SIGIOT:
729 	case SIGBUS:
730 	case SIGQUIT:
731 	case SIGTRAP:
732 	case SIGEMT:
733 	case SIGFPE:
734 	case SIGSEGV:
735 	case SIGSYS:
736 		u.u_arg[0] = sig;
737 		if (core())
738 			sig += 0200;
739 	}
740 	exit(sig);
741 }
742 
743 /*
744  * Create a core image on the file "core"
745  * If you are looking for protection glitches,
746  * there are probably a wealth of them here
747  * when this occurs to a suid command.
748  *
749  * It writes UPAGES block of the
750  * user.h area followed by the entire
751  * data+stack segments.
752  */
753 core()
754 {
755 	register struct inode *ip;
756 	register struct nameidata *ndp = &u.u_nd;
757 
758 	if (u.u_uid != u.u_ruid || u.u_gid != u.u_rgid)
759 		return (0);
760 	if (ctob(UPAGES+u.u_dsize+u.u_ssize) >=
761 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
762 		return (0);
763 	if (u.u_procp->p_textp && access(u.u_procp->p_textp->x_iptr, IREAD))
764 		return (0);
765 	u.u_error = 0;
766 	ndp->ni_nameiop = CREATE | FOLLOW;
767 	ndp->ni_segflg = UIO_SYSSPACE;
768 	ndp->ni_dirp = "core";
769 	ip = namei(ndp);
770 	if (ip == NULL) {
771 		if (u.u_error)
772 			return (0);
773 		ip = maknode(0644, ndp);
774 		if (ip==NULL)
775 			return (0);
776 	}
777 	if (access(ip, IWRITE) ||
778 	   (ip->i_mode&IFMT) != IFREG ||
779 	   ip->i_nlink != 1) {
780 		u.u_error = EFAULT;
781 		goto out;
782 	}
783 	itrunc(ip, (u_long)0);
784 	u.u_acflag |= ACORE;
785 	u.u_error = rdwri(UIO_WRITE, ip,
786 	    (caddr_t)&u,
787 	    ctob(UPAGES),
788 	    0, 1, (int *)0);
789 	if (u.u_error == 0)
790 		u.u_error = rdwri(UIO_WRITE, ip,
791 		    (caddr_t)ctob(dptov(u.u_procp, 0)),
792 		    ctob(u.u_dsize),
793 		    ctob(UPAGES), 0, (int *)0);
794 	if (u.u_error == 0)
795 		u.u_error = rdwri(UIO_WRITE, ip,
796 		    (caddr_t)ctob(sptov(u.u_procp, u.u_ssize - 1)),
797 		    ctob(u.u_ssize),
798 		    ctob(UPAGES)+ctob(u.u_dsize), 0, (int *)0);
799 out:
800 	iput(ip);
801 	return (u.u_error == 0);
802 }
803