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