xref: /minix/minix/servers/pm/signal.c (revision fb4fbf7a)
1 /* This file handles signals, which are asynchronous events and are generally
2  * a messy and unpleasant business.  Signals can be generated by the KILL
3  * system call, or from the keyboard (SIGINT) or from the clock (SIGALRM).
4  * In all cases control eventually passes to check_sig() to see which processes
5  * can be signaled.  The actual signaling is done by sig_proc().
6  *
7  * The entry points into this file are:
8  *   do_sigaction:	perform the SIGACTION system call
9  *   do_sigpending:	perform the SIGPENDING system call
10  *   do_sigprocmask:	perform the SIGPROCMASK system call
11  *   do_sigreturn:	perform the SIGRETURN system call
12  *   do_sigsuspend:	perform the SIGSUSPEND system call
13  *   do_kill:		perform the KILL system call
14  *   process_ksig:	process a signal an behalf of the kernel
15  *   sig_proc:		interrupt or terminate a signaled process
16  *   check_sig:		check which processes to signal with sig_proc()
17  *   check_pending:	check if a pending signal can now be delivered
18  *   restart_sigs: 	restart signal work after finishing a VFS call
19  */
20 
21 #include "pm.h"
22 #include <sys/stat.h>
23 #include <sys/ptrace.h>
24 #include <minix/callnr.h>
25 #include <minix/endpoint.h>
26 #include <minix/com.h>
27 #include <minix/vm.h>
28 #include <signal.h>
29 #include <sys/resource.h>
30 #include <assert.h>
31 #include "mproc.h"
32 
33 static int unpause(struct mproc *rmp);
34 static int sig_send(struct mproc *rmp, int signo);
35 static void sig_proc_exit(struct mproc *rmp, int signo);
36 
37 /*===========================================================================*
38  *				do_sigaction				     *
39  *===========================================================================*/
40 int do_sigaction(void)
41 {
42   int r, sig_nr;
43   struct sigaction svec;
44   struct sigaction *svp;
45 
46   assert(!(mp->mp_flags & (PROC_STOPPED | VFS_CALL | UNPAUSED | EVENT_CALL)));
47 
48   sig_nr = m_in.m_lc_pm_sig.nr;
49   if (sig_nr == SIGKILL) return(OK);
50   if (sig_nr < 1 || sig_nr >= _NSIG) return(EINVAL);
51 
52   svp = &mp->mp_sigact[sig_nr];
53   if (m_in.m_lc_pm_sig.oact != 0) {
54 	r = sys_datacopy(PM_PROC_NR,(vir_bytes) svp, who_e,
55 		m_in.m_lc_pm_sig.oact, (phys_bytes) sizeof(svec));
56 	if (r != OK) return(r);
57   }
58 
59   if (m_in.m_lc_pm_sig.act == 0)
60   	return(OK);
61 
62   /* Read in the sigaction structure. */
63   r = sys_datacopy(who_e, m_in.m_lc_pm_sig.act, PM_PROC_NR, (vir_bytes) &svec,
64 	  (phys_bytes) sizeof(svec));
65   if (r != OK) return(r);
66 
67   if (svec.sa_handler == SIG_IGN) {
68 	sigaddset(&mp->mp_ignore, sig_nr);
69 	sigdelset(&mp->mp_sigpending, sig_nr);
70 	sigdelset(&mp->mp_ksigpending, sig_nr);
71 	sigdelset(&mp->mp_catch, sig_nr);
72   } else if (svec.sa_handler == SIG_DFL) {
73 	sigdelset(&mp->mp_ignore, sig_nr);
74 	sigdelset(&mp->mp_catch, sig_nr);
75   } else {
76 	sigdelset(&mp->mp_ignore, sig_nr);
77 	sigaddset(&mp->mp_catch, sig_nr);
78   }
79   mp->mp_sigact[sig_nr].sa_handler = svec.sa_handler;
80   sigdelset(&svec.sa_mask, SIGKILL);
81   sigdelset(&svec.sa_mask, SIGSTOP);
82   mp->mp_sigact[sig_nr].sa_mask = svec.sa_mask;
83   mp->mp_sigact[sig_nr].sa_flags = svec.sa_flags;
84   mp->mp_sigreturn = m_in.m_lc_pm_sig.ret;
85   return(OK);
86 }
87 
88 /*===========================================================================*
89  *				do_sigpending                                *
90  *===========================================================================*/
91 int do_sigpending(void)
92 {
93   assert(!(mp->mp_flags & (PROC_STOPPED | VFS_CALL | UNPAUSED | EVENT_CALL)));
94 
95   mp->mp_reply.m_pm_lc_sigset.set = mp->mp_sigpending;
96   return OK;
97 }
98 
99 /*===========================================================================*
100  *				do_sigprocmask                               *
101  *===========================================================================*/
102 int do_sigprocmask(void)
103 {
104 /* Note that the library interface passes the actual mask in sigmask_set,
105  * not a pointer to the mask, in order to save a copy.  Similarly,
106  * the old mask is placed in the return message which the library
107  * interface copies (if requested) to the user specified address.
108  *
109  * The library interface must set SIG_INQUIRE if the 'act' argument
110  * is NULL.
111  *
112  * KILL and STOP can't be masked.
113  */
114   sigset_t set;
115   int i;
116 
117   assert(!(mp->mp_flags & (PROC_STOPPED | VFS_CALL | UNPAUSED | EVENT_CALL)));
118 
119   set = m_in.m_lc_pm_sigset.set;
120   mp->mp_reply.m_pm_lc_sigset.set = mp->mp_sigmask;
121 
122   switch (m_in.m_lc_pm_sigset.how) {
123       case SIG_BLOCK:
124 	sigdelset(&set, SIGKILL);
125 	sigdelset(&set, SIGSTOP);
126 	for (i = 1; i < _NSIG; i++) {
127 		if (sigismember(&set, i))
128 			sigaddset(&mp->mp_sigmask, i);
129 	}
130 	break;
131 
132       case SIG_UNBLOCK:
133 	for (i = 1; i < _NSIG; i++) {
134 		if (sigismember(&set, i))
135 			sigdelset(&mp->mp_sigmask, i);
136 	}
137 	check_pending(mp);
138 	break;
139 
140       case SIG_SETMASK:
141 	sigdelset(&set, SIGKILL);
142 	sigdelset(&set, SIGSTOP);
143 	mp->mp_sigmask = set;
144 	check_pending(mp);
145 	break;
146 
147       case SIG_INQUIRE:
148 	break;
149 
150       default:
151 	return(EINVAL);
152 	break;
153   }
154   return OK;
155 }
156 
157 /*===========================================================================*
158  *				do_sigsuspend                                *
159  *===========================================================================*/
160 int do_sigsuspend(void)
161 {
162   assert(!(mp->mp_flags & (PROC_STOPPED | VFS_CALL | UNPAUSED | EVENT_CALL)));
163 
164   mp->mp_sigmask2 = mp->mp_sigmask;	/* save the old mask */
165   mp->mp_sigmask = m_in.m_lc_pm_sigset.set;
166   sigdelset(&mp->mp_sigmask, SIGKILL);
167   sigdelset(&mp->mp_sigmask, SIGSTOP);
168   mp->mp_flags |= SIGSUSPENDED;
169   check_pending(mp);
170   return(SUSPEND);
171 }
172 
173 /*===========================================================================*
174  *				do_sigreturn				     *
175  *===========================================================================*/
176 int do_sigreturn(void)
177 {
178 /* A user signal handler is done.  Restore context and check for
179  * pending unblocked signals.
180  */
181   int r;
182 
183   assert(!(mp->mp_flags & (PROC_STOPPED | VFS_CALL | UNPAUSED | EVENT_CALL)));
184 
185   mp->mp_sigmask = m_in.m_lc_pm_sigset.set;
186   sigdelset(&mp->mp_sigmask, SIGKILL);
187   sigdelset(&mp->mp_sigmask, SIGSTOP);
188 
189   r = sys_sigreturn(who_e, (struct sigmsg *)m_in.m_lc_pm_sigset.ctx);
190   check_pending(mp);
191   return(r);
192 }
193 
194 /*===========================================================================*
195  *				do_kill					     *
196  *===========================================================================*/
197 int do_kill(void)
198 {
199 /* Perform the kill(pid, signo) system call. */
200 
201   return check_sig(m_in.m_lc_pm_sig.pid, m_in.m_lc_pm_sig.nr, FALSE /* ksig */);
202 }
203 
204 /*===========================================================================*
205  *			      do_srv_kill				     *
206  *===========================================================================*/
207 int do_srv_kill(void)
208 {
209 /* Perform the srv_kill(pid, signo) system call. */
210 
211   /* Only RS is allowed to use srv_kill. */
212   if (mp->mp_endpoint != RS_PROC_NR)
213 	return EPERM;
214 
215   /* Pretend the signal comes from the kernel when RS wants to deliver a signal
216    * to a system process. RS sends a SIGKILL when it wants to perform cleanup.
217    * In that case, ksig == TRUE forces PM to exit the process immediately.
218    */
219   return check_sig(m_in.m_rs_pm_srv_kill.pid, m_in.m_rs_pm_srv_kill.nr,
220 	  TRUE /* ksig */);
221 }
222 
223 /*===========================================================================*
224  *				stop_proc				     *
225  *===========================================================================*/
226 static int stop_proc(struct mproc *rmp, int may_delay)
227 {
228 /* Try to stop the given process in the kernel. If successful, mark the process
229  * as stopped and return TRUE.  If the process is still busy sending a message,
230  * the behavior depends on the 'may_delay' parameter. If set, the process will
231  * be marked as having a delay call pending, and the function returns FALSE. If
232  * not set, the caller already knows that the process has no delay call, and PM
233  * will panic.
234  */
235   int r;
236 
237   assert(!(rmp->mp_flags & (PROC_STOPPED | DELAY_CALL | UNPAUSED)));
238 
239   r = sys_delay_stop(rmp->mp_endpoint);
240 
241   /* If the process is still busy sending a message, the kernel will give us
242    * EBUSY now and send a SIGSNDELAY to the process as soon as sending is done.
243    */
244   switch (r) {
245   case OK:
246 	rmp->mp_flags |= PROC_STOPPED;
247 
248 	return TRUE;
249 
250   case EBUSY:
251 	if (!may_delay)
252 		panic("stop_proc: unexpected delay call");
253 
254 	rmp->mp_flags |= DELAY_CALL;
255 
256 	return FALSE;
257 
258   default:
259 	panic("sys_delay_stop failed: %d", r);
260   }
261 }
262 
263 /*===========================================================================*
264  *				try_resume_proc				     *
265  *===========================================================================*/
266 static void try_resume_proc(struct mproc *rmp)
267 {
268 /* Resume the given process if possible. */
269   int r;
270 
271   assert(rmp->mp_flags & PROC_STOPPED);
272 
273   /* If the process is blocked on a VFS call or a process event notification,
274    * do not resume it now.  Most likely it will be unpausing, in which case the
275    * process must remain stopped.  Otherwise, it will still be resumed once the
276    * VFS or event call is replied to.  If the process has died, do not resume
277    * it either.
278    */
279   if (rmp->mp_flags & (VFS_CALL | EVENT_CALL | EXITING))
280 	return;
281 
282   if ((r = sys_resume(rmp->mp_endpoint)) != OK)
283 	panic("sys_resume failed: %d", r);
284 
285   /* Also unset the unpaused flag. We can safely assume that a stopped process
286    * need only be unpaused once, but once it is resumed, all bets are off.
287    */
288   rmp->mp_flags &= ~(PROC_STOPPED | UNPAUSED);
289 }
290 
291 /*===========================================================================*
292  *				process_ksig				     *
293  *===========================================================================*/
294 int process_ksig(endpoint_t proc_nr_e, int signo)
295 {
296   register struct mproc *rmp;
297   int proc_nr;
298   pid_t proc_id, id;
299 
300   if(pm_isokendpt(proc_nr_e, &proc_nr) != OK) {
301 	printf("PM: process_ksig: %d?? not ok\n", proc_nr_e);
302 	return EDEADEPT; /* process is gone. */
303   }
304   rmp = &mproc[proc_nr];
305   if ((rmp->mp_flags & (IN_USE | EXITING)) != IN_USE) {
306 #if 0
307 	printf("PM: process_ksig: %d?? exiting / not in use\n", proc_nr_e);
308 #endif
309 	return EDEADEPT; /* process is gone. */
310   }
311   proc_id = rmp->mp_pid;
312   mp = &mproc[0];			/* pretend signals are from PM */
313   mp->mp_procgrp = rmp->mp_procgrp;	/* get process group right */
314 
315   /* For SIGVTALRM and SIGPROF, see if we need to restart a
316    * virtual timer. For SIGINT, SIGINFO, SIGWINCH and SIGQUIT, use proc_id 0
317    * to indicate a broadcast to the recipient's process group.  For
318    * SIGKILL, use proc_id -1 to indicate a systemwide broadcast.
319    */
320   switch (signo) {
321       case SIGINT:
322       case SIGQUIT:
323       case SIGWINCH:
324       case SIGINFO:
325   	id = 0; break;	/* broadcast to process group */
326       case SIGVTALRM:
327       case SIGPROF:
328       	check_vtimer(proc_nr, signo);
329       	/* fall-through */
330       default:
331   	id = proc_id;
332   	break;
333   }
334   check_sig(id, signo, TRUE /* ksig */);
335   mp->mp_procgrp = 0;			/* restore proper PM process group */
336 
337   /* If SIGSNDELAY is set, an earlier sys_stop() failed because the process was
338    * still sending, and the kernel hereby tells us that the process is now done
339    * with that. We can now try to resume what we planned to do in the first
340    * place: set up a signal handler. However, the process's message may have
341    * been a call to PM, in which case the process may have changed any of its
342    * signal settings. The process may also have forked, exited etcetera.
343    */
344   if (signo == SIGSNDELAY && (rmp->mp_flags & DELAY_CALL)) {
345 	/* When getting SIGSNDELAY, the process is stopped at least until the
346 	 * receipt of the SIGSNDELAY signal is acknowledged to the kernel. The
347 	 * process is not stopped on PROC_STOP in the kernel. However, now that
348 	 * there is no longer a delay call, stop_proc() is guaranteed to
349 	 * succeed immediately.
350 	 */
351 	rmp->mp_flags &= ~DELAY_CALL;
352 
353 	assert(!(rmp->mp_flags & PROC_STOPPED));
354 
355 	/* If the delay call was to PM, it may have resulted in a VFS call. In
356 	 * that case, we must wait with further signal processing until VFS has
357 	 * replied. Stop the process.
358 	 */
359 	if (rmp->mp_flags & (VFS_CALL | EVENT_CALL)) {
360 		stop_proc(rmp, FALSE /*may_delay*/);
361 
362 		return OK;
363 	}
364 
365 	/* Process as many normal signals as possible. */
366 	check_pending(rmp);
367 
368 	assert(!(rmp->mp_flags & DELAY_CALL));
369   }
370 
371   /* See if the process is still alive */
372   if ((mproc[proc_nr].mp_flags & (IN_USE | EXITING)) == IN_USE)  {
373       return OK; /* signal has been delivered */
374   }
375   else {
376       return EDEADEPT; /* process is gone */
377   }
378 }
379 
380 /*===========================================================================*
381  *				sig_proc				     *
382  *===========================================================================*/
383 void sig_proc(rmp, signo, trace, ksig)
384 register struct mproc *rmp;	/* pointer to the process to be signaled */
385 int signo;			/* signal to send to process (1 to _NSIG-1) */
386 int trace;			/* pass signal to tracer first? */
387 int ksig;			/* non-zero means signal comes from kernel  */
388 {
389 /* Send a signal to a process.  Check to see if the signal is to be caught,
390  * ignored, tranformed into a message (for system processes) or blocked.
391  *  - If the signal is to be transformed into a message, request the KERNEL to
392  * send the target process a system notification with the pending signal as an
393  * argument.
394  *  - If the signal is to be caught, request the KERNEL to push a sigcontext
395  * structure and a sigframe structure onto the catcher's stack.  Also, KERNEL
396  * will reset the program counter and stack pointer, so that when the process
397  * next runs, it will be executing the signal handler. When the signal handler
398  * returns,  sigreturn(2) will be called.  Then KERNEL will restore the signal
399  * context from the sigcontext structure.
400  * If there is insufficient stack space, kill the process.
401  */
402   int slot, badignore;
403 
404   slot = (int) (rmp - mproc);
405   if ((rmp->mp_flags & (IN_USE | EXITING)) != IN_USE) {
406 	panic("PM: signal %d sent to exiting process %d\n", signo, slot);
407   }
408 
409   if (trace == TRUE && rmp->mp_tracer != NO_TRACER && signo != SIGKILL) {
410 	/* Signal should be passed to the debugger first.
411 	 * This happens before any checks on block/ignore masks; otherwise,
412 	 * the process itself could block/ignore debugger signals.
413 	 */
414 
415 	sigaddset(&rmp->mp_sigtrace, signo);
416 
417 	if (!(rmp->mp_flags & TRACE_STOPPED))
418 		trace_stop(rmp, signo);	/* a signal causes it to stop */
419 
420 	return;
421   }
422 
423   if (rmp->mp_flags & (VFS_CALL | EVENT_CALL)) {
424 	sigaddset(&rmp->mp_sigpending, signo);
425 	if(ksig)
426 		sigaddset(&rmp->mp_ksigpending, signo);
427 
428 	/* Process the signal once VFS and process event subscribers reply.
429 	 * Stop the process in the meantime, so that it cannot make another
430 	 * call after the VFS reply comes in but before we look at its signals
431 	 * again. Since we always stop the process to deliver signals during a
432 	 * VFS or event call, the PROC_STOPPED flag doubles as an indicator in
433 	 * restart_sigs() that signals must be rechecked after a reply arrives.
434 	 */
435 	if (!(rmp->mp_flags & (PROC_STOPPED | DELAY_CALL))) {
436 		/* If a VFS call is ongoing and the process is not yet stopped,
437 		 * the process must have made a call to PM. Therefore, there
438 		 * can be no delay calls in this case.
439 		 */
440 		stop_proc(rmp, FALSE /*delay_call*/);
441 	}
442 	return;
443   }
444 
445   /* Handle system signals for system processes first. */
446   if(rmp->mp_flags & PRIV_PROC) {
447    	/* Always skip signals for PM (only necessary when broadcasting). */
448    	if(rmp->mp_endpoint == PM_PROC_NR) {
449  		return;
450    	}
451 
452    	/* System signals have always to go through the kernel first to let it
453    	 * pick the right signal manager. If PM is the assigned signal manager,
454    	 * the signal will come back and will actually be processed.
455    	 */
456    	if(!ksig) {
457  		sys_kill(rmp->mp_endpoint, signo);
458  		return;
459    	}
460 
461   	/* Print stacktrace if necessary. */
462   	if(SIGS_IS_STACKTRACE(signo)) {
463 		sys_diagctl_stacktrace(rmp->mp_endpoint);
464   	}
465 
466   	if(!SIGS_IS_TERMINATION(signo)) {
467 		/* Translate every non-termination sys signal into a message. */
468 		message m;
469 		m.m_type = SIGS_SIGNAL_RECEIVED;
470 		m.m_pm_lsys_sigs_signal.num = signo;
471 		asynsend3(rmp->mp_endpoint, &m, AMF_NOREPLY);
472 	}
473 	else {
474 		/* Exit the process in case of termination system signal. */
475 		sig_proc_exit(rmp, signo);
476 	}
477 	return;
478   }
479 
480   /* Handle user processes now. See if the signal cannot be safely ignored. */
481   badignore = ksig && sigismember(&noign_sset, signo) && (
482 	  sigismember(&rmp->mp_ignore, signo) ||
483 	  sigismember(&rmp->mp_sigmask, signo));
484 
485   if (!badignore && sigismember(&rmp->mp_ignore, signo)) {
486 	/* Signal should be ignored. */
487 	return;
488   }
489   if (!badignore && sigismember(&rmp->mp_sigmask, signo)) {
490 	/* Signal should be blocked. */
491 	sigaddset(&rmp->mp_sigpending, signo);
492 	if(ksig)
493 		sigaddset(&rmp->mp_ksigpending, signo);
494 	return;
495   }
496 
497   if ((rmp->mp_flags & TRACE_STOPPED) && signo != SIGKILL) {
498 	/* If the process is stopped for a debugger, do not deliver any signals
499 	 * (except SIGKILL) in order not to confuse the debugger. The signals
500 	 * will be delivered using the check_pending() calls in do_trace().
501 	 */
502 	sigaddset(&rmp->mp_sigpending, signo);
503 	if(ksig)
504 		sigaddset(&rmp->mp_ksigpending, signo);
505 	return;
506   }
507   if (!badignore && sigismember(&rmp->mp_catch, signo)) {
508 	/* Signal is caught. First interrupt the process's current call, if
509 	 * applicable. This may involve a roundtrip to VFS, in which case we'll
510 	 * have to check back later.
511 	 */
512 	if (!unpause(rmp)) {
513 		/* not yet unpaused; continue later */
514 		sigaddset(&rmp->mp_sigpending, signo);
515 		if(ksig)
516 			sigaddset(&rmp->mp_ksigpending, signo);
517 
518 		return;
519 	}
520 
521 	/* Then send the actual signal to the process, by setting up a signal
522 	 * handler.
523 	 */
524 	if (sig_send(rmp, signo))
525 		return;
526 
527 	/* We were unable to spawn a signal handler. Kill the process. */
528 	printf("PM: %d can't catch signal %d - killing\n",
529 		rmp->mp_pid, signo);
530   }
531   else if (!badignore && sigismember(&ign_sset, signo)) {
532 	/* Signal defaults to being ignored. */
533 	return;
534   }
535 
536   /* Terminate process */
537   sig_proc_exit(rmp, signo);
538 }
539 
540 /*===========================================================================*
541  *				sig_proc_exit				     *
542  *===========================================================================*/
543 static void sig_proc_exit(rmp, signo)
544 struct mproc *rmp;		/* process that must exit */
545 int signo;			/* signal that caused termination */
546 {
547   rmp->mp_sigstatus = (char) signo;
548   if (sigismember(&core_sset, signo)) {
549 	if(!(rmp->mp_flags & PRIV_PROC)) {
550 		printf("PM: coredump signal %d for %d / %s\n", signo,
551 			rmp->mp_pid, rmp->mp_name);
552 		sys_diagctl_stacktrace(rmp->mp_endpoint);
553 	}
554 	exit_proc(rmp, 0, TRUE /*dump_core*/);
555   }
556   else {
557   	exit_proc(rmp, 0, FALSE /*dump_core*/);
558   }
559 }
560 
561 /*===========================================================================*
562  *				check_sig				     *
563  *===========================================================================*/
564 int check_sig(proc_id, signo, ksig)
565 pid_t proc_id;			/* pid of proc to sig, or 0 or -1, or -pgrp */
566 int signo;			/* signal to send to process (0 to _NSIG-1) */
567 int ksig;			/* non-zero means signal comes from kernel  */
568 {
569 /* Check to see if it is possible to send a signal.  The signal may have to be
570  * sent to a group of processes.  This routine is invoked by the KILL system
571  * call, and also when the kernel catches a DEL or other signal.
572  */
573 
574   register struct mproc *rmp;
575   int count;			/* count # of signals sent */
576   int error_code;
577 
578   if (signo < 0 || signo >= _NSIG) return(EINVAL);
579 
580   /* Return EINVAL for attempts to send SIGKILL to INIT alone. */
581   if (proc_id == INIT_PID && signo == SIGKILL) return(EINVAL);
582 
583   /* Signal RS first when broadcasting SIGTERM. */
584   if (proc_id == -1 && signo == SIGTERM)
585       sys_kill(RS_PROC_NR, signo);
586 
587   /* Search the proc table for processes to signal. Start from the end of the
588    * table to analyze core system processes at the end when broadcasting.
589    * (See forkexit.c about pid magic.)
590    */
591   count = 0;
592   error_code = ESRCH;
593   for (rmp = &mproc[NR_PROCS-1]; rmp >= &mproc[0]; rmp--) {
594 	if (!(rmp->mp_flags & IN_USE)) continue;
595 
596 	/* Check for selection. */
597 	if (proc_id > 0 && proc_id != rmp->mp_pid) continue;
598 	if (proc_id == 0 && mp->mp_procgrp != rmp->mp_procgrp) continue;
599 	if (proc_id == -1 && rmp->mp_pid <= INIT_PID) continue;
600 	if (proc_id < -1 && rmp->mp_procgrp != -proc_id) continue;
601 
602 	/* Do not kill servers and drivers when broadcasting SIGKILL. */
603 	if (proc_id == -1 && signo == SIGKILL &&
604 		(rmp->mp_flags & PRIV_PROC)) continue;
605 
606 	/* Skip VM entirely as it might lead to a deadlock with its signal
607 	 * manager if the manager page faults at the same time.
608 	 */
609 	if (rmp->mp_endpoint == VM_PROC_NR) continue;
610 
611 	/* Disallow lethal signals sent by user processes to sys processes. */
612 	if (!ksig && SIGS_IS_LETHAL(signo) && (rmp->mp_flags & PRIV_PROC)) {
613 	    error_code = EPERM;
614 	    continue;
615 	}
616 
617 	/* Check for permission. */
618 	if (mp->mp_effuid != SUPER_USER
619 	    && mp->mp_realuid != rmp->mp_realuid
620 	    && mp->mp_effuid != rmp->mp_realuid
621 	    && mp->mp_realuid != rmp->mp_effuid
622 	    && mp->mp_effuid != rmp->mp_effuid) {
623 		error_code = EPERM;
624 		continue;
625 	}
626 
627 	count++;
628 	if (signo == 0 || (rmp->mp_flags & EXITING)) continue;
629 
630 	/* 'sig_proc' will handle the disposition of the signal.  The
631 	 * signal may be caught, blocked, ignored, or cause process
632 	 * termination, possibly with core dump.
633 	 */
634 	sig_proc(rmp, signo, TRUE /*trace*/, ksig);
635 
636 	if (proc_id > 0) break;	/* only one process being signaled */
637   }
638 
639   /* If the calling process has killed itself, don't reply. */
640   if ((mp->mp_flags & (IN_USE | EXITING)) != IN_USE) return(SUSPEND);
641   return(count > 0 ? OK : error_code);
642 }
643 
644 /*===========================================================================*
645  *				check_pending				     *
646  *===========================================================================*/
647 void check_pending(rmp)
648 register struct mproc *rmp;
649 {
650   /* Check to see if any pending signals have been unblocked. Deliver as many
651    * of them as we can, until we have to wait for a reply from VFS first.
652    *
653    * There are several places in this file where the signal mask is
654    * changed.  At each such place, check_pending() should be called to
655    * check for newly unblocked signals.
656    */
657   int i;
658   int ksig;
659 
660   for (i = 1; i < _NSIG; i++) {
661 	if (sigismember(&rmp->mp_sigpending, i) &&
662 		!sigismember(&rmp->mp_sigmask, i)) {
663 		ksig = sigismember(&rmp->mp_ksigpending, i);
664 		sigdelset(&rmp->mp_sigpending, i);
665 		sigdelset(&rmp->mp_ksigpending, i);
666 		sig_proc(rmp, i, FALSE /*trace*/, ksig);
667 
668 		if (rmp->mp_flags & (VFS_CALL | EVENT_CALL)) {
669 			/* Signals must be rechecked upon return from the new
670 			 * VFS call, unless the process was killed. In both
671 			 * cases, the process is stopped.
672 			 */
673 			assert(rmp->mp_flags & PROC_STOPPED);
674 			break;
675 		}
676 	}
677   }
678 }
679 
680 /*===========================================================================*
681  *				restart_sigs				     *
682  *===========================================================================*/
683 void restart_sigs(rmp)
684 struct mproc *rmp;
685 {
686 /* VFS has replied to a request from us; do signal-related work.
687  */
688 
689   if (rmp->mp_flags & (VFS_CALL | EVENT_CALL | EXITING)) return;
690 
691   if (rmp->mp_flags & TRACE_EXIT) {
692 	/* Tracer requested exit with specific exit value */
693 	exit_proc(rmp, rmp->mp_exitstatus, FALSE /*dump_core*/);
694   }
695   else if (rmp->mp_flags & PROC_STOPPED) {
696 	/* If a signal arrives while we are performing a VFS call, the process
697 	 * will always be stopped immediately. Thus, if the process is stopped
698 	 * once the reply from VFS arrives, we might have to check signals.
699 	 */
700 	assert(!(rmp->mp_flags & DELAY_CALL));
701 
702 	/* We saved signal(s) for after finishing a VFS call. Deal with this.
703 	 * PROC_STOPPED remains set to indicate the process is still stopped.
704 	 */
705 	check_pending(rmp);
706 
707 	/* Resume the process now, unless there is a reason not to. */
708 	try_resume_proc(rmp);
709   }
710 }
711 
712 /*===========================================================================*
713  *				unpause					     *
714  *===========================================================================*/
715 static int unpause(rmp)
716 struct mproc *rmp;		/* which process */
717 {
718 /* A signal is to be sent to a process.  If that process is hanging on a
719  * system call, the system call must be terminated with EINTR.  First check if
720  * the process is hanging on an PM call.  If not, tell VFS, so it can check for
721  * interruptible calls such as READs and WRITEs from pipes, ttys and the like.
722  */
723   message m;
724 
725   assert(!(rmp->mp_flags & (VFS_CALL | EVENT_CALL)));
726 
727   /* If the UNPAUSED flag is set, VFS replied to an earlier unpause request. */
728   if (rmp->mp_flags & UNPAUSED) {
729 	assert((rmp->mp_flags & (DELAY_CALL | PROC_STOPPED)) == PROC_STOPPED);
730 
731 	return TRUE;
732   }
733 
734   /* If the process is already stopping, don't do anything now. */
735   if (rmp->mp_flags & DELAY_CALL)
736 	return FALSE;
737 
738   /* Check to see if process is hanging on a WAIT or SIGSUSPEND call. */
739   if (rmp->mp_flags & (WAITING | SIGSUSPENDED)) {
740 	/* Stop the process from running. Do not interrupt the actual call yet.
741 	 * sig_send() will interrupt the call and resume the process afterward.
742 	 * No delay calls: we know for a fact that the process called us.
743 	 */
744 	stop_proc(rmp, FALSE /*may_delay*/);
745 
746 	return TRUE;
747   }
748 
749   /* Not paused in PM. Let VFS, and after that any matching process event
750    * subscribers, try to unpause the process. The process needs to be stopped
751    * for this. If it is not already stopped, try to stop it now. If that does
752    * not succeed immediately, postpone signal delivery.
753    */
754   if (!(rmp->mp_flags & PROC_STOPPED) && !stop_proc(rmp, TRUE /*may_delay*/))
755 	return FALSE;
756 
757   memset(&m, 0, sizeof(m));
758   m.m_type = VFS_PM_UNPAUSE;
759   m.VFS_PM_ENDPT = rmp->mp_endpoint;
760 
761   tell_vfs(rmp, &m);
762 
763   return FALSE;
764 }
765 
766 /*===========================================================================*
767  *				sig_send				     *
768  *===========================================================================*/
769 static int sig_send(rmp, signo)
770 struct mproc *rmp;		/* what process to spawn a signal handler in */
771 int signo;			/* signal to send to process (1 to _NSIG-1) */
772 {
773 /* The process is supposed to catch this signal. Spawn a signal handler.
774  * Return TRUE if this succeeded, FALSE otherwise.
775  */
776   struct sigmsg sigmsg;
777   int i, r, sigflags, slot;
778 
779   assert(rmp->mp_flags & PROC_STOPPED);
780 
781   sigflags = rmp->mp_sigact[signo].sa_flags;
782   slot = (int) (rmp - mproc);
783 
784   if (rmp->mp_flags & SIGSUSPENDED)
785 	sigmsg.sm_mask = rmp->mp_sigmask2;
786   else
787 	sigmsg.sm_mask = rmp->mp_sigmask;
788   sigmsg.sm_signo = signo;
789   sigmsg.sm_sighandler =
790 	(vir_bytes) rmp->mp_sigact[signo].sa_handler;
791   sigmsg.sm_sigreturn = rmp->mp_sigreturn;
792   for (i = 1; i < _NSIG; i++) {
793 	if (sigismember(&rmp->mp_sigact[signo].sa_mask, i))
794 		sigaddset(&rmp->mp_sigmask, i);
795   }
796 
797   if (sigflags & SA_NODEFER)
798 	sigdelset(&rmp->mp_sigmask, signo);
799   else
800 	sigaddset(&rmp->mp_sigmask, signo);
801 
802   if (sigflags & SA_RESETHAND) {
803 	sigdelset(&rmp->mp_catch, signo);
804 	rmp->mp_sigact[signo].sa_handler = SIG_DFL;
805   }
806   sigdelset(&rmp->mp_sigpending, signo);
807   sigdelset(&rmp->mp_ksigpending, signo);
808 
809   /* Ask the kernel to deliver the signal */
810   r = sys_sigsend(rmp->mp_endpoint, &sigmsg);
811   /* sys_sigsend can fail legitimately with EFAULT or ENOMEM if the process
812    * memory can't accommodate the signal handler.  The target process will be
813    * killed in that case, so do not bother interrupting or resuming it.
814    */
815   if(r == EFAULT || r == ENOMEM) {
816 	return(FALSE);
817   }
818   /* Other errors are unexpected pm/kernel discrepancies. */
819   if (r != OK) {
820 	panic("sys_sigsend failed: %d", r);
821   }
822 
823   /* Was the process suspended in PM? Then interrupt the blocking call. */
824   if (rmp->mp_flags & (WAITING | SIGSUSPENDED)) {
825 	rmp->mp_flags &= ~(WAITING | SIGSUSPENDED);
826 
827 	reply(slot, EINTR);
828 
829 	/* The process must just have been stopped by unpause(), which means
830 	 * that the UNPAUSE flag is not set.
831 	 */
832 	assert(!(rmp->mp_flags & UNPAUSED));
833 
834 	try_resume_proc(rmp);
835 
836 	assert(!(rmp->mp_flags & PROC_STOPPED));
837   } else {
838 	/* If the process was not suspended in PM, VFS must first have
839 	 * confirmed that it has tried to unsuspend any blocking call. Thus, we
840 	 * got here from restart_sigs() as part of handling PM_UNPAUSE_REPLY,
841 	 * and restart_sigs() will resume the process later.
842 	 */
843 	assert(rmp->mp_flags & UNPAUSED);
844   }
845 
846   return(TRUE);
847 }
848