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