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