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