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