xref: /csrg-svn/sys/kern/kern_sig.c (revision 18336)
1 /*	kern_sig.c	6.13	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()
184 {
185 	register struct a {
186 		int	pid;
187 		int	signo;
188 	} *uap = (struct a *)u.u_ap;
189 	register struct proc *p;
190 
191 	if (uap->signo < 0 || uap->signo > NSIG) {
192 		u.u_error = EINVAL;
193 		return;
194 	}
195 	if (uap->pid > 0) {
196 		/* kill single process */
197 		p = pfind(uap->pid);
198 		if (p == 0) {
199 			u.u_error = ESRCH;
200 			return;
201 		}
202 		if (u.u_uid && u.u_uid != p->p_uid)
203 			u.u_error = EPERM;
204 		else if (uap->signo)
205 			psignal(p, uap->signo);
206 		return;
207 	}
208 	switch (uap->pid) {
209 	case -1:		/* broadcast signal */
210 		if (suser())
211 			u.u_error = killpg1(uap->signo, 0, 1);
212 		break;
213 	case 0:			/* signal own process group */
214 		u.u_error = killpg1(uap->signo, 0, 0);
215 		break;
216 	default:		/* negative explicit process group */
217 		u.u_error = killpg1(uap->signo, -uap->pid, 0);
218 		break;
219 	}
220 	return;
221 }
222 
223 killpg()
224 {
225 	register struct a {
226 		int	pgrp;
227 		int	signo;
228 	} *uap = (struct a *)u.u_ap;
229 
230 	if (uap->signo < 0 || uap->signo > NSIG) {
231 		u.u_error = EINVAL;
232 		return;
233 	}
234 	u.u_error = killpg1(uap->signo, uap->pgrp, 0);
235 }
236 
237 /* KILL CODE SHOULDNT KNOW ABOUT PROCESS INTERNALS !?! */
238 
239 killpg1(signo, pgrp, all)
240 	int signo, pgrp, all;
241 {
242 	register struct proc *p;
243 	int f, error = 0;
244 
245 	if (!all && pgrp == 0) {
246 		/*
247 		 * Zero process id means send to my process group.
248 		 */
249 		pgrp = u.u_procp->p_pgrp;
250 		if (pgrp == 0)
251 			return (EINVAL);
252 	}
253 	for (f = 0, p = allproc; p != NULL; p = p->p_nxt) {
254 		if ((p->p_pgrp != pgrp && !all) || p->p_ppid == 0 ||
255 		    (p->p_flag&SSYS) || (all && p == u.u_procp))
256 			continue;
257 		f++;
258 		if (u.u_uid != 0 && u.u_uid != p->p_uid &&
259 		    (signo != SIGCONT || !inferior(p))) {
260 			error = EPERM;
261 			continue;
262 		}
263 		if (signo)
264 			psignal(p, signo);
265 	}
266 	return (f == 0 ? ESRCH : error);
267 }
268 
269 /*
270  * Send the specified signal to
271  * all processes with 'pgrp' as
272  * process group.
273  */
274 gsignal(pgrp, sig)
275 	register int pgrp;
276 {
277 	register struct proc *p;
278 
279 	if (pgrp == 0)
280 		return;
281 	for (p = allproc; p != NULL; p = p->p_nxt)
282 		if (p->p_pgrp == pgrp)
283 			psignal(p, sig);
284 }
285 
286 /*
287  * Send the specified signal to
288  * the specified process.
289  */
290 psignal(p, sig)
291 	register struct proc *p;
292 	register int sig;
293 {
294 	register int s;
295 	register int (*action)();
296 	int mask;
297 
298 	if ((unsigned)sig >= NSIG)
299 		return;
300 	mask = sigmask(sig);
301 
302 	/*
303 	 * If proc is traced, always give parent a chance.
304 	 */
305 	if (p->p_flag & STRC)
306 		action = SIG_DFL;
307 	else {
308 		/*
309 		 * If the signal is being ignored,
310 		 * then we forget about it immediately.
311 		 */
312 		if (p->p_sigignore & mask)
313 			return;
314 		if (p->p_sigmask & mask)
315 			action = SIG_HOLD;
316 		else if (p->p_sigcatch & mask)
317 			action = SIG_CATCH;
318 		else
319 			action = SIG_DFL;
320 	}
321 #define	stops	(sigmask(SIGSTOP)|sigmask(SIGTSTP)| \
322 			sigmask(SIGTTIN)|sigmask(SIGTTOU))
323 	if (sig) {
324 		p->p_sig |= mask;
325 		switch (sig) {
326 
327 		case SIGTERM:
328 			if ((p->p_flag&STRC) || action != SIG_DFL)
329 				break;
330 			/* fall into ... */
331 
332 		case SIGKILL:
333 			if (p->p_nice > NZERO)
334 				p->p_nice = NZERO;
335 			break;
336 
337 		case SIGCONT:
338 			p->p_sig &= ~stops;
339 			break;
340 
341 		case SIGSTOP:
342 		case SIGTSTP:
343 		case SIGTTIN:
344 		case SIGTTOU:
345 			p->p_sig &= ~sigmask(SIGCONT);
346 			break;
347 		}
348 	}
349 #undef stops
350 	/*
351 	 * Defer further processing for signals which are held.
352 	 */
353 	if (action == SIG_HOLD)
354 		return;
355 	s = splhigh();
356 	switch (p->p_stat) {
357 
358 	case SSLEEP:
359 		/*
360 		 * If process is sleeping at negative priority
361 		 * we can't interrupt the sleep... the signal will
362 		 * be noticed when the process returns through
363 		 * trap() or syscall().
364 		 */
365 		if (p->p_pri <= PZERO)
366 			goto out;
367 		/*
368 		 * Process is sleeping and traced... make it runnable
369 		 * so it can discover the signal in issig() and stop
370 		 * for the parent.
371 		 */
372 		if (p->p_flag&STRC)
373 			goto run;
374 		switch (sig) {
375 
376 		case SIGSTOP:
377 		case SIGTSTP:
378 		case SIGTTIN:
379 		case SIGTTOU:
380 			/*
381 			 * These are the signals which by default
382 			 * stop a process.
383 			 */
384 			if (action != SIG_DFL)
385 				goto run;
386 			/*
387 			 * Don't clog system with children of init
388 			 * stopped from the keyboard.
389 			 */
390 			if (sig != SIGSTOP && p->p_pptr == &proc[1]) {
391 				psignal(p, SIGKILL);
392 				p->p_sig &= ~mask;
393 				splx(s);
394 				return;
395 			}
396 			/*
397 			 * If a child in vfork(), stopping could
398 			 * cause deadlock.
399 			 */
400 			if (p->p_flag&SVFORK)
401 				goto out;
402 			p->p_sig &= ~mask;
403 			p->p_cursig = sig;
404 			psignal(p->p_pptr, SIGCHLD);
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 			psignal(p->p_pptr, SIGCHLD);
543 			do {
544 				stop(p);
545 				swtch();
546 			} while (!procxmt() && p->p_flag&STRC);
547 
548 			/*
549 			 * If the traced bit got turned off,
550 			 * then put the signal taken above back into p_sig
551 			 * and go back up to the top to rescan signals.
552 			 * This ensures that p_sig* and u_signal are consistent.
553 			 */
554 			if ((p->p_flag&STRC) == 0) {
555 				p->p_sig |= mask;
556 				continue;
557 			}
558 
559 			/*
560 			 * If parent wants us to take the signal,
561 			 * then it will leave it in p->p_cursig;
562 			 * otherwise we just look for signals again.
563 			 */
564 			sig = p->p_cursig;
565 			if (sig == 0)
566 				continue;
567 
568 			/*
569 			 * If signal is being masked put it back
570 			 * into p_sig and look for other signals.
571 			 */
572 			mask = sigmask(sig);
573 			if (p->p_sigmask & mask) {
574 				p->p_sig |= mask;
575 				continue;
576 			}
577 		}
578 		switch (u.u_signal[sig]) {
579 
580 		case SIG_DFL:
581 			/*
582 			 * Don't take default actions on system processes.
583 			 */
584 			if (p->p_ppid == 0)
585 				break;
586 			switch (sig) {
587 
588 			case SIGTSTP:
589 			case SIGTTIN:
590 			case SIGTTOU:
591 				/*
592 				 * Children of init aren't allowed to stop
593 				 * on signals from the keyboard.
594 				 */
595 				if (p->p_pptr == &proc[1]) {
596 					psignal(p, SIGKILL);
597 					continue;
598 				}
599 				/* fall into ... */
600 
601 			case SIGSTOP:
602 				if (p->p_flag&STRC)
603 					continue;
604 				psignal(p->p_pptr, SIGCHLD);
605 				stop(p);
606 				swtch();
607 				continue;
608 
609 			case SIGCONT:
610 			case SIGCHLD:
611 			case SIGURG:
612 			case SIGIO:
613 			case SIGWINCH:
614 				/*
615 				 * These signals are normally not
616 				 * sent if the action is the default.
617 				 */
618 				continue;		/* == ignore */
619 
620 			default:
621 				goto send;
622 			}
623 			/*NOTREACHED*/
624 
625 		case SIG_HOLD:
626 		case SIG_IGN:
627 			/*
628 			 * Masking above should prevent us
629 			 * ever trying to take action on a held
630 			 * or ignored signal, unless process is traced.
631 			 */
632 			if ((p->p_flag&STRC) == 0)
633 				printf("issig\n");
634 			continue;
635 
636 		default:
637 			/*
638 			 * This signal has an action, let
639 			 * psig process it.
640 			 */
641 			goto send;
642 		}
643 		/*NOTREACHED*/
644 	}
645 	/*
646 	 * Didn't find a signal to send.
647 	 */
648 	p->p_cursig = 0;
649 	return (0);
650 
651 send:
652 	/*
653 	 * Let psig process the signal.
654 	 */
655 	return (sig);
656 }
657 
658 /*
659  * Put the argument process into the stopped
660  * state and notify the parent via wakeup.
661  * Signals are handled elsewhere.
662  */
663 stop(p)
664 	register struct proc *p;
665 {
666 
667 	p->p_stat = SSTOP;
668 	p->p_flag &= ~SWTED;
669 	wakeup((caddr_t)p->p_pptr);
670 }
671 
672 /*
673  * Perform the action specified by
674  * the current signal.
675  * The usual sequence is:
676  *	if (issig())
677  *		psig();
678  * The signal bit has already been cleared by issig,
679  * and the current signal number stored in p->p_cursig.
680  */
681 psig()
682 {
683 	register struct proc *p = u.u_procp;
684 	register int sig = p->p_cursig;
685 	int mask = sigmask(sig), returnmask;
686 	register int (*action)();
687 
688 	if (sig == 0)
689 		panic("psig");
690 	action = u.u_signal[sig];
691 	if (action != SIG_DFL) {
692 		if (action == SIG_IGN || (p->p_sigmask & mask))
693 			panic("psig action");
694 		u.u_error = 0;
695 		/*
696 		 * Set the new mask value and also defer further
697 		 * occurences of this signal (unless we're simulating
698 		 * the old signal facilities).
699 		 *
700 		 * Special case: user has done a sigpause.  Here the
701 		 * current mask is not of interest, but rather the
702 		 * mask from before the sigpause is what we want restored
703 		 * after the signal processing is completed.
704 		 */
705 		(void) splhigh();
706 		if (p->p_flag & SOUSIG) {
707 			if (sig != SIGILL && sig != SIGTRAP) {
708 				u.u_signal[sig] = SIG_DFL;
709 				p->p_sigcatch &= ~mask;
710 			}
711 			mask = 0;
712 		}
713 		if (p->p_flag & SOMASK) {
714 			returnmask = u.u_oldmask;
715 			p->p_flag &= ~SOMASK;
716 		} else
717 			returnmask = p->p_sigmask;
718 		p->p_sigmask |= u.u_sigmask[sig] | mask;
719 		(void) spl0();
720 		u.u_ru.ru_nsignals++;
721 		sendsig(action, sig, returnmask);
722 		p->p_cursig = 0;
723 		return;
724 	}
725 	u.u_acflag |= AXSIG;
726 	switch (sig) {
727 
728 	case SIGILL:
729 	case SIGIOT:
730 	case SIGBUS:
731 	case SIGQUIT:
732 	case SIGTRAP:
733 	case SIGEMT:
734 	case SIGFPE:
735 	case SIGSEGV:
736 	case SIGSYS:
737 		u.u_arg[0] = sig;
738 		if (core())
739 			sig += 0200;
740 	}
741 	exit(sig);
742 }
743 
744 /*
745  * Create a core image on the file "core"
746  * If you are looking for protection glitches,
747  * there are probably a wealth of them here
748  * when this occurs to a suid command.
749  *
750  * It writes UPAGES block of the
751  * user.h area followed by the entire
752  * data+stack segments.
753  */
754 core()
755 {
756 	register struct inode *ip;
757 	register struct nameidata *ndp = &u.u_nd;
758 
759 	if (u.u_uid != u.u_ruid || u.u_gid != u.u_rgid)
760 		return (0);
761 	if (ctob(UPAGES+u.u_dsize+u.u_ssize) >=
762 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
763 		return (0);
764 	u.u_error = 0;
765 	ndp->ni_nameiop = CREATE | FOLLOW;
766 	ndp->ni_segflg = UIO_SYSSPACE;
767 	ndp->ni_dirp = "core";
768 	ip = namei(ndp);
769 	if (ip == NULL) {
770 		if (u.u_error)
771 			return (0);
772 		ip = maknode(0644, ndp);
773 		if (ip==NULL)
774 			return (0);
775 	}
776 	if (access(ip, IWRITE) ||
777 	   (ip->i_mode&IFMT) != IFREG ||
778 	   ip->i_nlink != 1) {
779 		u.u_error = EFAULT;
780 		goto out;
781 	}
782 	itrunc(ip, (u_long)0);
783 	u.u_acflag |= ACORE;
784 	u.u_error = rdwri(UIO_WRITE, ip,
785 	    (caddr_t)&u,
786 	    ctob(UPAGES),
787 	    0, 1, (int *)0);
788 	if (u.u_error == 0)
789 		u.u_error = rdwri(UIO_WRITE, ip,
790 		    (caddr_t)ctob(dptov(u.u_procp, 0)),
791 		    ctob(u.u_dsize),
792 		    ctob(UPAGES), 0, (int *)0);
793 	if (u.u_error == 0)
794 		u.u_error = rdwri(UIO_WRITE, ip,
795 		    (caddr_t)ctob(sptov(u.u_procp, u.u_ssize - 1)),
796 		    ctob(u.u_ssize),
797 		    ctob(UPAGES)+ctob(u.u_dsize), 0, (int *)0);
798 out:
799 	iput(ip);
800 	return (u.u_error == 0);
801 }
802