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