xref: /csrg-svn/sys/kern/kern_sig.c (revision 12969)
1 /*	kern_sig.c	5.21	83/06/10	*/
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 or signal
527 			 * is being masked, then put the signal taken
528 			 * above back into p_sig and go back up to the
529 			 * top to rescan signals.  This ensures that
530 			 * p_sig* and u_signal are consistent.
531 			 */
532 			if ((p->p_flag&STRC) == 0 || (p->p_sigmask & sigmask)) {
533 				p->p_sig |= sigmask;
534 				continue;
535 			}
536 
537 			/*
538 			 * If parent wants us to take the signal,
539 			 * then it will leave it in p->p_cursig;
540 			 * otherwise we just look for signals again.
541 			 */
542 			sig = p->p_cursig;
543 			if (sig == 0)
544 				continue;
545 		}
546 		switch (u.u_signal[sig]) {
547 
548 		case SIG_DFL:
549 			/*
550 			 * Don't take default actions on system processes.
551 			 */
552 			if (p->p_ppid == 0)
553 				break;
554 			switch (sig) {
555 
556 			case SIGTSTP:
557 			case SIGTTIN:
558 			case SIGTTOU:
559 				/*
560 				 * Children of init aren't allowed to stop
561 				 * on signals from the keyboard.
562 				 */
563 				if (p->p_pptr == &proc[1]) {
564 					psignal(p, SIGKILL);
565 					continue;
566 				}
567 				/* fall into ... */
568 
569 			case SIGSTOP:
570 				if (p->p_flag&STRC)
571 					continue;
572 				stop(p);
573 				swtch();
574 				continue;
575 
576 			case SIGCONT:
577 			case SIGCHLD:
578 			case SIGURG:
579 			case SIGIO:
580 				/*
581 				 * These signals are normally not
582 				 * sent if the action is the default.
583 				 */
584 				continue;		/* == ignore */
585 
586 			default:
587 				goto send;
588 			}
589 			/*NOTREACHED*/
590 
591 		case SIG_HOLD:
592 		case SIG_IGN:
593 			/*
594 			 * Masking above should prevent us
595 			 * ever trying to take action on a held
596 			 * or ignored signal, unless process is traced.
597 			 */
598 			if ((p->p_flag&STRC) == 0)
599 				printf("issig\n");
600 			continue;
601 
602 		default:
603 			/*
604 			 * This signal has an action, let
605 			 * psig process it.
606 			 */
607 			goto send;
608 		}
609 		/*NOTREACHED*/
610 	}
611 	/*
612 	 * Didn't find a signal to send.
613 	 */
614 	p->p_cursig = 0;
615 	return (0);
616 
617 send:
618 	/*
619 	 * Let psig process the signal.
620 	 */
621 	return (sig);
622 }
623 
624 /*
625  * Put the argument process into the stopped
626  * state and notify the parent via wakeup and/or signal.
627  */
628 stop(p)
629 	register struct proc *p;
630 {
631 
632 	p->p_stat = SSTOP;
633 	p->p_flag &= ~SWTED;
634 	wakeup((caddr_t)p->p_pptr);
635 	/*
636 	 * Avoid sending signal to parent if process is traced
637 	 */
638 	if (p->p_flag&STRC)
639 		return;
640 	psignal(p->p_pptr, SIGCHLD);
641 }
642 
643 /*
644  * Perform the action specified by
645  * the current signal.
646  * The usual sequence is:
647  *	if (issig())
648  *		psig();
649  * The signal bit has already been cleared by issig,
650  * and the current signal number stored in p->p_cursig.
651  */
652 psig()
653 {
654 	register struct proc *p = u.u_procp;
655 	register int sig = p->p_cursig;
656 	int sigmask = 1 << (sig - 1), returnmask;
657 	register int (*action)();
658 
659 	if (sig == 0)
660 		panic("psig");
661 	action = u.u_signal[sig];
662 	if (action != SIG_DFL) {
663 		if (action == SIG_IGN || (p->p_sigmask & sigmask))
664 			panic("psig action");
665 		u.u_error = 0;
666 		/*
667 		 * Set the new mask value and also defer further
668 		 * occurences of this signal (unless we're simulating
669 		 * the old signal facilities).
670 		 *
671 		 * Special case: user has done a sigpause.  Here the
672 		 * current mask is not of interest, but rather the
673 		 * mask from before the sigpause is what we want restored
674 		 * after the signal processing is completed.
675 		 */
676 		(void) spl6();
677 		if (p->p_flag & SOUSIG) {
678 			if (sig != SIGILL && sig != SIGTRAP) {
679 				u.u_signal[sig] = SIG_DFL;
680 				p->p_sigcatch &= ~sigmask;
681 			}
682 			sigmask = 0;
683 		}
684 		if (p->p_flag & SOMASK) {
685 			returnmask = u.u_oldmask;
686 			p->p_flag &= ~SOMASK;
687 		} else
688 			returnmask = p->p_sigmask;
689 		p->p_sigmask |= u.u_sigmask[sig] | sigmask;
690 		(void) spl0();
691 		u.u_ru.ru_nsignals++;
692 		sendsig(action, sig, returnmask);
693 		p->p_cursig = 0;
694 		return;
695 	}
696 	u.u_acflag |= AXSIG;
697 	switch (sig) {
698 
699 	case SIGILL:
700 	case SIGIOT:
701 	case SIGBUS:
702 	case SIGQUIT:
703 	case SIGTRAP:
704 	case SIGEMT:
705 	case SIGFPE:
706 	case SIGSEGV:
707 	case SIGSYS:
708 		u.u_arg[0] = sig;
709 		if (core())
710 			sig += 0200;
711 	}
712 	exit(sig);
713 }
714 
715 /*
716  * Create a core image on the file "core"
717  * If you are looking for protection glitches,
718  * there are probably a wealth of them here
719  * when this occurs to a suid command.
720  *
721  * It writes UPAGES block of the
722  * user.h area followed by the entire
723  * data+stack segments.
724  */
725 core()
726 {
727 	register struct inode *ip;
728 	extern schar();
729 
730 	if (u.u_uid != u.u_ruid || u.u_gid != u.u_rgid)
731 		return (0);
732 	if (ctob(UPAGES+u.u_dsize+u.u_ssize) >=
733 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
734 		return (0);
735 	u.u_error = 0;
736 	u.u_dirp = "core";
737 	ip = namei(schar, CREATE, 1);
738 	if (ip == NULL) {
739 		if (u.u_error)
740 			return (0);
741 		ip = maknode(0644);
742 		if (ip==NULL)
743 			return (0);
744 	}
745 	if (access(ip, IWRITE) ||
746 	   (ip->i_mode&IFMT) != IFREG ||
747 	   ip->i_nlink != 1) {
748 		u.u_error = EFAULT;
749 		goto out;
750 	}
751 	itrunc(ip, (u_long)0);
752 	u.u_acflag |= ACORE;
753 	u.u_error = rdwri(UIO_WRITE, ip,
754 	    (caddr_t)&u,
755 	    ctob(UPAGES),
756 	    0, 1, (int *)0);
757 	if (u.u_error == 0)
758 		u.u_error = rdwri(UIO_WRITE, ip,
759 		    (caddr_t)ctob(dptov(u.u_procp, 0)),
760 		    ctob(u.u_dsize),
761 		    ctob(UPAGES), 0, (int *)0);
762 	if (u.u_error == 0)
763 		u.u_error = rdwri(UIO_WRITE, ip,
764 		    (caddr_t)ctob(sptov(u.u_procp, u.u_ssize - 1)),
765 		    ctob(u.u_ssize),
766 		    ctob(UPAGES)+ctob(u.u_dsize), 0, (int *)0);
767 out:
768 	iput(ip);
769 	return (u.u_error == 0);
770 }
771