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