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