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