xref: /csrg-svn/sys/kern/kern_sig.c (revision 18308)
1 /*	kern_sig.c	6.11	85/03/12	*/
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 			stop(p);
390 			goto out;
391 
392 		case SIGIO:
393 		case SIGURG:
394 		case SIGCHLD:
395 		case SIGWINCH:
396 			/*
397 			 * These signals are special in that they
398 			 * don't get propogated... if the process
399 			 * isn't interested, forget it.
400 			 */
401 			if (action != SIG_DFL)
402 				goto run;
403 			p->p_sig &= ~mask;		/* take it away */
404 			goto out;
405 
406 		default:
407 			/*
408 			 * All other signals cause the process to run
409 			 */
410 			goto run;
411 		}
412 		/*NOTREACHED*/
413 
414 	case SSTOP:
415 		/*
416 		 * If traced process is already stopped,
417 		 * then no further action is necessary.
418 		 */
419 		if (p->p_flag&STRC)
420 			goto out;
421 		switch (sig) {
422 
423 		case SIGKILL:
424 			/*
425 			 * Kill signal always sets processes running.
426 			 */
427 			goto run;
428 
429 		case SIGCONT:
430 			/*
431 			 * If the process catches SIGCONT, let it handle
432 			 * the signal itself.  If it isn't waiting on
433 			 * an event, then it goes back to run state.
434 			 * Otherwise, process goes back to sleep state.
435 			 */
436 			if (action != SIG_DFL || p->p_wchan == 0)
437 				goto run;
438 			p->p_stat = SSLEEP;
439 			goto out;
440 
441 		case SIGSTOP:
442 		case SIGTSTP:
443 		case SIGTTIN:
444 		case SIGTTOU:
445 			/*
446 			 * Already stopped, don't need to stop again.
447 			 * (If we did the shell could get confused.)
448 			 */
449 			p->p_sig &= ~mask;		/* take it away */
450 			goto out;
451 
452 		default:
453 			/*
454 			 * If process is sleeping interruptibly, then
455 			 * unstick it so that when it is continued
456 			 * it can look at the signal.
457 			 * But don't setrun the process as its not to
458 			 * be unstopped by the signal alone.
459 			 */
460 			if (p->p_wchan && p->p_pri > PZERO)
461 				unsleep(p);
462 			goto out;
463 		}
464 		/*NOTREACHED*/
465 
466 	default:
467 		/*
468 		 * SRUN, SIDL, SZOMB do nothing with the signal,
469 		 * other than kicking ourselves if we are running.
470 		 * It will either never be noticed, or noticed very soon.
471 		 */
472 		if (p == u.u_procp && !noproc)
473 #include "../vax/mtpr.h"
474 			aston();
475 		goto out;
476 	}
477 	/*NOTREACHED*/
478 run:
479 	/*
480 	 * Raise priority to at least PUSER.
481 	 */
482 	if (p->p_pri > PUSER)
483 		p->p_pri = PUSER;
484 	setrun(p);
485 out:
486 	splx(s);
487 }
488 
489 /*
490  * Returns true if the current
491  * process has a signal to process.
492  * The signal to process is put in p_cursig.
493  * This is asked at least once each time a process enters the
494  * system (though this can usually be done without actually
495  * calling issig by checking the pending signal masks.)
496  * A signal does not do anything
497  * directly to a process; it sets
498  * a flag that asks the process to
499  * do something to itself.
500  */
501 issig()
502 {
503 	register struct proc *p;
504 	register int sig;
505 	int sigbits, mask;
506 
507 	p = u.u_procp;
508 	for (;;) {
509 		sigbits = p->p_sig &~ p->p_sigmask;
510 		if ((p->p_flag&STRC) == 0)
511 			sigbits &= ~p->p_sigignore;
512 		if (p->p_flag&SVFORK)
513 #define bit(a) (1<<(a-1))
514 			sigbits &= ~(bit(SIGSTOP)|bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU));
515 		if (sigbits == 0)
516 			break;
517 		sig = ffs(sigbits);
518 		mask = sigmask(sig);
519 		p->p_sig &= ~mask;		/* take the signal! */
520 		p->p_cursig = sig;
521 		if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
522 			/*
523 			 * If traced, always stop, and stay
524 			 * stopped until released by the parent.
525 			 */
526 			do {
527 				stop(p);
528 				swtch();
529 			} while (!procxmt() && p->p_flag&STRC);
530 
531 			/*
532 			 * If the traced bit got turned off,
533 			 * then put the signal taken above back into p_sig
534 			 * and go back up to the top to rescan signals.
535 			 * This ensures that p_sig* and u_signal are consistent.
536 			 */
537 			if ((p->p_flag&STRC) == 0) {
538 				p->p_sig |= mask;
539 				continue;
540 			}
541 
542 			/*
543 			 * If parent wants us to take the signal,
544 			 * then it will leave it in p->p_cursig;
545 			 * otherwise we just look for signals again.
546 			 */
547 			sig = p->p_cursig;
548 			if (sig == 0)
549 				continue;
550 
551 			/*
552 			 * If signal is being masked put it back
553 			 * into p_sig and look for other signals.
554 			 */
555 			mask = sigmask(sig);
556 			if (p->p_sigmask & mask) {
557 				p->p_sig |= mask;
558 				continue;
559 			}
560 		}
561 		switch (u.u_signal[sig]) {
562 
563 		case SIG_DFL:
564 			/*
565 			 * Don't take default actions on system processes.
566 			 */
567 			if (p->p_ppid == 0)
568 				break;
569 			switch (sig) {
570 
571 			case SIGTSTP:
572 			case SIGTTIN:
573 			case SIGTTOU:
574 				/*
575 				 * Children of init aren't allowed to stop
576 				 * on signals from the keyboard.
577 				 */
578 				if (p->p_pptr == &proc[1]) {
579 					psignal(p, SIGKILL);
580 					continue;
581 				}
582 				/* fall into ... */
583 
584 			case SIGSTOP:
585 				if (p->p_flag&STRC)
586 					continue;
587 				stop(p);
588 				swtch();
589 				continue;
590 
591 			case SIGCONT:
592 			case SIGCHLD:
593 			case SIGURG:
594 			case SIGIO:
595 			case SIGWINCH:
596 				/*
597 				 * These signals are normally not
598 				 * sent if the action is the default.
599 				 */
600 				continue;		/* == ignore */
601 
602 			default:
603 				goto send;
604 			}
605 			/*NOTREACHED*/
606 
607 		case SIG_HOLD:
608 		case SIG_IGN:
609 			/*
610 			 * Masking above should prevent us
611 			 * ever trying to take action on a held
612 			 * or ignored signal, unless process is traced.
613 			 */
614 			if ((p->p_flag&STRC) == 0)
615 				printf("issig\n");
616 			continue;
617 
618 		default:
619 			/*
620 			 * This signal has an action, let
621 			 * psig process it.
622 			 */
623 			goto send;
624 		}
625 		/*NOTREACHED*/
626 	}
627 	/*
628 	 * Didn't find a signal to send.
629 	 */
630 	p->p_cursig = 0;
631 	return (0);
632 
633 send:
634 	/*
635 	 * Let psig process the signal.
636 	 */
637 	return (sig);
638 }
639 
640 /*
641  * Put the argument process into the stopped
642  * state and notify the parent via wakeup and/or signal.
643  */
644 stop(p)
645 	register struct proc *p;
646 {
647 
648 	p->p_stat = SSTOP;
649 	p->p_flag &= ~SWTED;
650 	wakeup((caddr_t)p->p_pptr);
651 	/*
652 	 * Avoid sending signal to parent if process is traced
653 	 */
654 	if (p->p_flag&STRC)
655 		return;
656 	psignal(p->p_pptr, SIGCHLD);
657 }
658 
659 /*
660  * Perform the action specified by
661  * the current signal.
662  * The usual sequence is:
663  *	if (issig())
664  *		psig();
665  * The signal bit has already been cleared by issig,
666  * and the current signal number stored in p->p_cursig.
667  */
668 psig()
669 {
670 	register struct proc *p = u.u_procp;
671 	register int sig = p->p_cursig;
672 	int mask = sigmask(sig), returnmask;
673 	register int (*action)();
674 
675 	if (sig == 0)
676 		panic("psig");
677 	action = u.u_signal[sig];
678 	if (action != SIG_DFL) {
679 		if (action == SIG_IGN || (p->p_sigmask & mask))
680 			panic("psig action");
681 		u.u_error = 0;
682 		/*
683 		 * Set the new mask value and also defer further
684 		 * occurences of this signal (unless we're simulating
685 		 * the old signal facilities).
686 		 *
687 		 * Special case: user has done a sigpause.  Here the
688 		 * current mask is not of interest, but rather the
689 		 * mask from before the sigpause is what we want restored
690 		 * after the signal processing is completed.
691 		 */
692 		(void) splhigh();
693 		if (p->p_flag & SOUSIG) {
694 			if (sig != SIGILL && sig != SIGTRAP) {
695 				u.u_signal[sig] = SIG_DFL;
696 				p->p_sigcatch &= ~mask;
697 			}
698 			mask = 0;
699 		}
700 		if (p->p_flag & SOMASK) {
701 			returnmask = u.u_oldmask;
702 			p->p_flag &= ~SOMASK;
703 		} else
704 			returnmask = p->p_sigmask;
705 		p->p_sigmask |= u.u_sigmask[sig] | mask;
706 		(void) spl0();
707 		u.u_ru.ru_nsignals++;
708 		sendsig(action, sig, returnmask);
709 		p->p_cursig = 0;
710 		return;
711 	}
712 	u.u_acflag |= AXSIG;
713 	switch (sig) {
714 
715 	case SIGILL:
716 	case SIGIOT:
717 	case SIGBUS:
718 	case SIGQUIT:
719 	case SIGTRAP:
720 	case SIGEMT:
721 	case SIGFPE:
722 	case SIGSEGV:
723 	case SIGSYS:
724 		u.u_arg[0] = sig;
725 		if (core())
726 			sig += 0200;
727 	}
728 	exit(sig);
729 }
730 
731 /*
732  * Create a core image on the file "core"
733  * If you are looking for protection glitches,
734  * there are probably a wealth of them here
735  * when this occurs to a suid command.
736  *
737  * It writes UPAGES block of the
738  * user.h area followed by the entire
739  * data+stack segments.
740  */
741 core()
742 {
743 	register struct inode *ip;
744 	register struct nameidata *ndp = &u.u_nd;
745 
746 	if (u.u_uid != u.u_ruid || u.u_gid != u.u_rgid)
747 		return (0);
748 	if (ctob(UPAGES+u.u_dsize+u.u_ssize) >=
749 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
750 		return (0);
751 	u.u_error = 0;
752 	ndp->ni_nameiop = CREATE | FOLLOW;
753 	ndp->ni_segflg = UIO_SYSSPACE;
754 	ndp->ni_dirp = "core";
755 	ip = namei(ndp);
756 	if (ip == NULL) {
757 		if (u.u_error)
758 			return (0);
759 		ip = maknode(0644, ndp);
760 		if (ip==NULL)
761 			return (0);
762 	}
763 	if (access(ip, IWRITE) ||
764 	   (ip->i_mode&IFMT) != IFREG ||
765 	   ip->i_nlink != 1) {
766 		u.u_error = EFAULT;
767 		goto out;
768 	}
769 	itrunc(ip, (u_long)0);
770 	u.u_acflag |= ACORE;
771 	u.u_error = rdwri(UIO_WRITE, ip,
772 	    (caddr_t)&u,
773 	    ctob(UPAGES),
774 	    0, 1, (int *)0);
775 	if (u.u_error == 0)
776 		u.u_error = rdwri(UIO_WRITE, ip,
777 		    (caddr_t)ctob(dptov(u.u_procp, 0)),
778 		    ctob(u.u_dsize),
779 		    ctob(UPAGES), 0, (int *)0);
780 	if (u.u_error == 0)
781 		u.u_error = rdwri(UIO_WRITE, ip,
782 		    (caddr_t)ctob(sptov(u.u_procp, u.u_ssize - 1)),
783 		    ctob(u.u_ssize),
784 		    ctob(UPAGES)+ctob(u.u_dsize), 0, (int *)0);
785 out:
786 	iput(ip);
787 	return (u.u_error == 0);
788 }
789