xref: /minix/minix/servers/pm/signal.c (revision 83133719)
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)));
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)));
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)));
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)));
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)));
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, do not resume it now. Most likely    * it will be unpausing, in which case the process must remain stopped.
274    * Otherwise, it will still be resumed once the VFS call returns. If the
275    * process has died, do not resume it either.
276    */
277   if (rmp->mp_flags & (VFS_CALL | EXITING))
278 	return;
279 
280   if ((r = sys_resume(rmp->mp_endpoint)) != OK)
281 	panic("sys_resume failed: %d", r);
282 
283   /* Also unset the unpaused flag. We can safely assume that a stopped process
284    * need only be unpaused once, but once it is resumed, all bets are off.
285    */
286   rmp->mp_flags &= ~(PROC_STOPPED | UNPAUSED);
287 }
288 
289 /*===========================================================================*
290  *				process_ksig				     *
291  *===========================================================================*/
292 int process_ksig(endpoint_t proc_nr_e, int signo)
293 {
294   register struct mproc *rmp;
295   int proc_nr;
296   pid_t proc_id, id;
297 
298   if(pm_isokendpt(proc_nr_e, &proc_nr) != OK) {
299 	printf("PM: process_ksig: %d?? not ok\n", proc_nr_e);
300 	return EDEADEPT; /* process is gone. */
301   }
302   rmp = &mproc[proc_nr];
303   if ((rmp->mp_flags & (IN_USE | EXITING)) != IN_USE) {
304 #if 0
305 	printf("PM: process_ksig: %d?? exiting / not in use\n", proc_nr_e);
306 #endif
307 	return EDEADEPT; /* process is gone. */
308   }
309   proc_id = rmp->mp_pid;
310   mp = &mproc[0];			/* pretend signals are from PM */
311   mp->mp_procgrp = rmp->mp_procgrp;	/* get process group right */
312 
313   /* For SIGVTALRM and SIGPROF, see if we need to restart a
314    * virtual timer. For SIGINT, SIGINFO, SIGWINCH and SIGQUIT, use proc_id 0
315    * to indicate a broadcast to the recipient's process group.  For
316    * SIGKILL, use proc_id -1 to indicate a systemwide broadcast.
317    */
318   switch (signo) {
319       case SIGINT:
320       case SIGQUIT:
321       case SIGWINCH:
322       case SIGINFO:
323   	id = 0; break;	/* broadcast to process group */
324       case SIGVTALRM:
325       case SIGPROF:
326       	check_vtimer(proc_nr, signo);
327       	/* fall-through */
328       default:
329   	id = proc_id;
330   	break;
331   }
332   check_sig(id, signo, TRUE /* ksig */);
333 
334   /* If SIGSNDELAY is set, an earlier sys_stop() failed because the process was
335    * still sending, and the kernel hereby tells us that the process is now done
336    * with that. We can now try to resume what we planned to do in the first
337    * place: set up a signal handler. However, the process's message may have
338    * been a call to PM, in which case the process may have changed any of its
339    * signal settings. The process may also have forked, exited etcetera.
340    */
341   if (signo == SIGSNDELAY && (rmp->mp_flags & DELAY_CALL)) {
342 	/* When getting SIGSNDELAY, the process is stopped at least until the
343 	 * receipt of the SIGSNDELAY signal is acknowledged to the kernel. The
344 	 * process is not stopped on PROC_STOP in the kernel. However, now that
345 	 * there is no longer a delay call, stop_proc() is guaranteed to
346 	 * succeed immediately.
347 	 */
348 	rmp->mp_flags &= ~DELAY_CALL;
349 
350 	assert(!(rmp->mp_flags & PROC_STOPPED));
351 
352 	/* If the delay call was to PM, it may have resulted in a VFS call. In
353 	 * that case, we must wait with further signal processing until VFS has
354 	 * replied. Stop the process.
355 	 */
356 	if (rmp->mp_flags & VFS_CALL) {
357 		stop_proc(rmp, FALSE /*may_delay*/);
358 
359 		return OK;
360 	}
361 
362 	/* Process as many normal signals as possible. */
363 	check_pending(rmp);
364 
365 	assert(!(rmp->mp_flags & DELAY_CALL));
366   }
367 
368   /* See if the process is still alive */
369   if ((mproc[proc_nr].mp_flags & (IN_USE | EXITING)) == IN_USE)  {
370       return OK; /* signal has been delivered */
371   }
372   else {
373       return EDEADEPT; /* process is gone */
374   }
375 }
376 
377 /*===========================================================================*
378  *				sig_proc				     *
379  *===========================================================================*/
380 void sig_proc(rmp, signo, trace, ksig)
381 register struct mproc *rmp;	/* pointer to the process to be signaled */
382 int signo;			/* signal to send to process (1 to _NSIG-1) */
383 int trace;			/* pass signal to tracer first? */
384 int ksig;			/* non-zero means signal comes from kernel  */
385 {
386 /* Send a signal to a process.  Check to see if the signal is to be caught,
387  * ignored, tranformed into a message (for system processes) or blocked.
388  *  - If the signal is to be transformed into a message, request the KERNEL to
389  * send the target process a system notification with the pending signal as an
390  * argument.
391  *  - If the signal is to be caught, request the KERNEL to push a sigcontext
392  * structure and a sigframe structure onto the catcher's stack.  Also, KERNEL
393  * will reset the program counter and stack pointer, so that when the process
394  * next runs, it will be executing the signal handler. When the signal handler
395  * returns,  sigreturn(2) will be called.  Then KERNEL will restore the signal
396  * context from the sigcontext structure.
397  * If there is insufficient stack space, kill the process.
398  */
399   int slot, badignore;
400 
401   slot = (int) (rmp - mproc);
402   if ((rmp->mp_flags & (IN_USE | EXITING)) != IN_USE) {
403 	panic("PM: signal %d sent to exiting process %d\n", signo, slot);
404   }
405 
406   if (trace == TRUE && rmp->mp_tracer != NO_TRACER && signo != SIGKILL) {
407 	/* Signal should be passed to the debugger first.
408 	 * This happens before any checks on block/ignore masks; otherwise,
409 	 * the process itself could block/ignore debugger signals.
410 	 */
411 
412 	sigaddset(&rmp->mp_sigtrace, signo);
413 
414 	if (!(rmp->mp_flags & TRACE_STOPPED))
415 		trace_stop(rmp, signo);	/* a signal causes it to stop */
416 
417 	return;
418   }
419 
420   if (rmp->mp_flags & VFS_CALL) {
421 	sigaddset(&rmp->mp_sigpending, signo);
422 	if(ksig)
423 		sigaddset(&rmp->mp_ksigpending, signo);
424 
425 	/* Process the signal once VFS replies. Stop the process in the
426 	 * meantime, so that it cannot make another call after the VFS reply
427 	 * comes in but before we look at its signals again. Since we always
428 	 * stop the process to deliver signals during a VFS call, the
429 	 * PROC_STOPPED flag doubles as an indicator in restart_sigs() that
430 	 * signals must be rechecked after a VFS reply comes in.
431 	 */
432 	if (!(rmp->mp_flags & (PROC_STOPPED | DELAY_CALL))) {
433 		/* If a VFS call is ongoing and the process is not yet stopped,
434 		 * the process must have made a call to PM. Therefore, there
435 		 * can be no delay calls in this case.
436 		 */
437 		stop_proc(rmp, FALSE /*delay_call*/);
438 	}
439 	return;
440   }
441 
442   /* Handle system signals for system processes first. */
443   if(rmp->mp_flags & PRIV_PROC) {
444    	/* Always skip signals for PM (only necessary when broadcasting). */
445    	if(rmp->mp_endpoint == PM_PROC_NR) {
446  		return;
447    	}
448 
449    	/* System signals have always to go through the kernel first to let it
450    	 * pick the right signal manager. If PM is the assigned signal manager,
451    	 * the signal will come back and will actually be processed.
452    	 */
453    	if(!ksig) {
454  		sys_kill(rmp->mp_endpoint, signo);
455  		return;
456    	}
457 
458   	/* Print stacktrace if necessary. */
459   	if(SIGS_IS_STACKTRACE(signo)) {
460 		sys_diagctl_stacktrace(rmp->mp_endpoint);
461   	}
462 
463   	if(!SIGS_IS_TERMINATION(signo)) {
464 		/* Translate every non-termination sys signal into a message. */
465 		message m;
466 		m.m_type = SIGS_SIGNAL_RECEIVED;
467 		m.m_pm_lsys_sigs_signal.num = signo;
468 		asynsend3(rmp->mp_endpoint, &m, AMF_NOREPLY);
469 	}
470 	else {
471 		/* Exit the process in case of termination system signal. */
472 		sig_proc_exit(rmp, signo);
473 	}
474 	return;
475   }
476 
477   /* Handle user processes now. See if the signal cannot be safely ignored. */
478   badignore = ksig && sigismember(&noign_sset, signo) && (
479 	  sigismember(&rmp->mp_ignore, signo) ||
480 	  sigismember(&rmp->mp_sigmask, signo));
481 
482   if (!badignore && sigismember(&rmp->mp_ignore, signo)) {
483 	/* Signal should be ignored. */
484 	return;
485   }
486   if (!badignore && sigismember(&rmp->mp_sigmask, signo)) {
487 	/* Signal should be blocked. */
488 	sigaddset(&rmp->mp_sigpending, signo);
489 	if(ksig)
490 		sigaddset(&rmp->mp_ksigpending, signo);
491 	return;
492   }
493 
494   if ((rmp->mp_flags & TRACE_STOPPED) && signo != SIGKILL) {
495 	/* If the process is stopped for a debugger, do not deliver any signals
496 	 * (except SIGKILL) in order not to confuse the debugger. The signals
497 	 * will be delivered using the check_pending() calls in do_trace().
498 	 */
499 	sigaddset(&rmp->mp_sigpending, signo);
500 	if(ksig)
501 		sigaddset(&rmp->mp_ksigpending, signo);
502 	return;
503   }
504   if (!badignore && sigismember(&rmp->mp_catch, signo)) {
505 	/* Signal is caught. First interrupt the process's current call, if
506 	 * applicable. This may involve a roundtrip to VFS, in which case we'll
507 	 * have to check back later.
508 	 */
509 	if (!unpause(rmp)) {
510 		/* not yet unpaused; continue later */
511 		sigaddset(&rmp->mp_sigpending, signo);
512 		if(ksig)
513 			sigaddset(&rmp->mp_ksigpending, signo);
514 
515 		return;
516 	}
517 
518 	/* Then send the actual signal to the process, by setting up a signal
519 	 * handler.
520 	 */
521 	if (sig_send(rmp, signo))
522 		return;
523 
524 	/* We were unable to spawn a signal handler. Kill the process. */
525 	printf("PM: %d can't catch signal %d - killing\n",
526 		rmp->mp_pid, signo);
527   }
528   else if (!badignore && sigismember(&ign_sset, signo)) {
529 	/* Signal defaults to being ignored. */
530 	return;
531   }
532 
533   /* Terminate process */
534   sig_proc_exit(rmp, signo);
535 }
536 
537 /*===========================================================================*
538  *				sig_proc_exit				     *
539  *===========================================================================*/
540 static void sig_proc_exit(rmp, signo)
541 struct mproc *rmp;		/* process that must exit */
542 int signo;			/* signal that caused termination */
543 {
544   rmp->mp_sigstatus = (char) signo;
545   if (sigismember(&core_sset, signo)) {
546 	if(!(rmp->mp_flags & PRIV_PROC)) {
547 		printf("PM: coredump signal %d for %d / %s\n", signo,
548 			rmp->mp_pid, rmp->mp_name);
549 		sys_diagctl_stacktrace(rmp->mp_endpoint);
550 	}
551 	exit_proc(rmp, 0, TRUE /*dump_core*/);
552   }
553   else {
554   	exit_proc(rmp, 0, FALSE /*dump_core*/);
555   }
556 }
557 
558 /*===========================================================================*
559  *				check_sig				     *
560  *===========================================================================*/
561 int check_sig(proc_id, signo, ksig)
562 pid_t proc_id;			/* pid of proc to sig, or 0 or -1, or -pgrp */
563 int signo;			/* signal to send to process (0 to _NSIG-1) */
564 int ksig;			/* non-zero means signal comes from kernel  */
565 {
566 /* Check to see if it is possible to send a signal.  The signal may have to be
567  * sent to a group of processes.  This routine is invoked by the KILL system
568  * call, and also when the kernel catches a DEL or other signal.
569  */
570 
571   register struct mproc *rmp;
572   int count;			/* count # of signals sent */
573   int error_code;
574 
575   if (signo < 0 || signo >= _NSIG) return(EINVAL);
576 
577   /* Return EINVAL for attempts to send SIGKILL to INIT alone. */
578   if (proc_id == INIT_PID && signo == SIGKILL) return(EINVAL);
579 
580   /* Signal RS first when broadcasting SIGTERM. */
581   if (proc_id == -1 && signo == SIGTERM)
582       sys_kill(RS_PROC_NR, signo);
583 
584   /* Search the proc table for processes to signal. Start from the end of the
585    * table to analyze core system processes at the end when broadcasting.
586    * (See forkexit.c about pid magic.)
587    */
588   count = 0;
589   error_code = ESRCH;
590   for (rmp = &mproc[NR_PROCS-1]; rmp >= &mproc[0]; rmp--) {
591 	if (!(rmp->mp_flags & IN_USE)) continue;
592 
593 	/* Check for selection. */
594 	if (proc_id > 0 && proc_id != rmp->mp_pid) continue;
595 	if (proc_id == 0 && mp->mp_procgrp != rmp->mp_procgrp) continue;
596 	if (proc_id == -1 && rmp->mp_pid <= INIT_PID) continue;
597 	if (proc_id < -1 && rmp->mp_procgrp != -proc_id) continue;
598 
599 	/* Do not kill servers and drivers when broadcasting SIGKILL. */
600 	if (proc_id == -1 && signo == SIGKILL &&
601 		(rmp->mp_flags & PRIV_PROC)) continue;
602 
603 	/* Skip VM entirely as it might lead to a deadlock with its signal
604 	 * manager if the manager page faults at the same time.
605 	 */
606 	if (rmp->mp_endpoint == VM_PROC_NR) continue;
607 
608 	/* Disallow lethal signals sent by user processes to sys processes. */
609 	if (!ksig && SIGS_IS_LETHAL(signo) && (rmp->mp_flags & PRIV_PROC)) {
610 	    error_code = EPERM;
611 	    continue;
612 	}
613 
614 	/* Check for permission. */
615 	if (mp->mp_effuid != SUPER_USER
616 	    && mp->mp_realuid != rmp->mp_realuid
617 	    && mp->mp_effuid != rmp->mp_realuid
618 	    && mp->mp_realuid != rmp->mp_effuid
619 	    && mp->mp_effuid != rmp->mp_effuid) {
620 		error_code = EPERM;
621 		continue;
622 	}
623 
624 	count++;
625 	if (signo == 0 || (rmp->mp_flags & EXITING)) continue;
626 
627 	/* 'sig_proc' will handle the disposition of the signal.  The
628 	 * signal may be caught, blocked, ignored, or cause process
629 	 * termination, possibly with core dump.
630 	 */
631 	sig_proc(rmp, signo, TRUE /*trace*/, ksig);
632 
633 	if (proc_id > 0) break;	/* only one process being signaled */
634   }
635 
636   /* If the calling process has killed itself, don't reply. */
637   if ((mp->mp_flags & (IN_USE | EXITING)) != IN_USE) return(SUSPEND);
638   return(count > 0 ? OK : error_code);
639 }
640 
641 /*===========================================================================*
642  *				check_pending				     *
643  *===========================================================================*/
644 void check_pending(rmp)
645 register struct mproc *rmp;
646 {
647   /* Check to see if any pending signals have been unblocked. Deliver as many
648    * of them as we can, until we have to wait for a reply from VFS first.
649    *
650    * There are several places in this file where the signal mask is
651    * changed.  At each such place, check_pending() should be called to
652    * check for newly unblocked signals.
653    */
654   int i;
655   int ksig;
656 
657   for (i = 1; i < _NSIG; i++) {
658 	if (sigismember(&rmp->mp_sigpending, i) &&
659 		!sigismember(&rmp->mp_sigmask, i)) {
660 		ksig = sigismember(&rmp->mp_ksigpending, i);
661 		sigdelset(&rmp->mp_sigpending, i);
662 		sigdelset(&rmp->mp_ksigpending, i);
663 		sig_proc(rmp, i, FALSE /*trace*/, ksig);
664 
665 		if (rmp->mp_flags & VFS_CALL) {
666 			/* Signals must be rechecked upon return from the new
667 			 * VFS call, unless the process was killed. In both
668 			 * cases, the process is stopped.
669 			 */
670 			assert(rmp->mp_flags & PROC_STOPPED);
671 			break;
672 		}
673 	}
674   }
675 }
676 
677 /*===========================================================================*
678  *				restart_sigs				     *
679  *===========================================================================*/
680 void restart_sigs(rmp)
681 struct mproc *rmp;
682 {
683 /* VFS has replied to a request from us; do signal-related work.
684  */
685 
686   if (rmp->mp_flags & (VFS_CALL | EXITING)) return;
687 
688   if (rmp->mp_flags & TRACE_EXIT) {
689 	/* Tracer requested exit with specific exit value */
690 	exit_proc(rmp, rmp->mp_exitstatus, FALSE /*dump_core*/);
691   }
692   else if (rmp->mp_flags & PROC_STOPPED) {
693 	/* If a signal arrives while we are performing a VFS call, the process
694 	 * will always be stopped immediately. Thus, if the process is stopped
695 	 * once the reply from VFS arrives, we might have to check signals.
696 	 */
697 	assert(!(rmp->mp_flags & DELAY_CALL));
698 
699 	/* We saved signal(s) for after finishing a VFS call. Deal with this.
700 	 * PROC_STOPPED remains set to indicate the process is still stopped.
701 	 */
702 	check_pending(rmp);
703 
704 	/* Resume the process now, unless there is a reason not to. */
705 	try_resume_proc(rmp);
706   }
707 }
708 
709 /*===========================================================================*
710  *				unpause					     *
711  *===========================================================================*/
712 static int unpause(rmp)
713 struct mproc *rmp;		/* which process */
714 {
715 /* A signal is to be sent to a process.  If that process is hanging on a
716  * system call, the system call must be terminated with EINTR.  First check if
717  * the process is hanging on an PM call.  If not, tell VFS, so it can check for
718  * interruptible calls such as READs and WRITEs from pipes, ttys and the like.
719  */
720   message m;
721 
722   assert(!(rmp->mp_flags & VFS_CALL));
723 
724   /* If the UNPAUSED flag is set, VFS replied to an earlier unpause request. */
725   if (rmp->mp_flags & UNPAUSED) {
726 	assert((rmp->mp_flags & (DELAY_CALL | PROC_STOPPED)) == PROC_STOPPED);
727 
728 	return TRUE;
729   }
730 
731   /* If the process is already stopping, don't do anything now. */
732   if (rmp->mp_flags & DELAY_CALL)
733 	return FALSE;
734 
735   /* Check to see if process is hanging on a WAIT or SIGSUSPEND call. */
736   if (rmp->mp_flags & (WAITING | SIGSUSPENDED)) {
737 	/* Stop the process from running. Do not interrupt the actual call yet.
738 	 * sig_send() will interrupt the call and resume the process afterward.
739 	 * No delay calls: we know for a fact that the process called us.
740 	 */
741 	stop_proc(rmp, FALSE /*may_delay*/);
742 
743 	return TRUE;
744   }
745 
746   /* Not paused in PM. Let VFS try to unpause the process. The process needs to
747    * be stopped for this. If it is not already stopped, try to stop it now. If
748    * that does not succeed immediately, postpone signal delivery.
749    */
750   if (!(rmp->mp_flags & PROC_STOPPED) && !stop_proc(rmp, TRUE /*may_delay*/))
751 	return FALSE;
752 
753   memset(&m, 0, sizeof(m));
754   m.m_type = VFS_PM_UNPAUSE;
755   m.VFS_PM_ENDPT = rmp->mp_endpoint;
756 
757   tell_vfs(rmp, &m);
758 
759   /* Also tell VM. */
760   vm_notify_sig_wrapper(rmp->mp_endpoint);
761 
762   return FALSE;
763 }
764 
765 /*===========================================================================*
766  *				sig_send				     *
767  *===========================================================================*/
768 static int sig_send(rmp, signo)
769 struct mproc *rmp;		/* what process to spawn a signal handler in */
770 int signo;			/* signal to send to process (1 to _NSIG-1) */
771 {
772 /* The process is supposed to catch this signal. Spawn a signal handler.
773  * Return TRUE if this succeeded, FALSE otherwise.
774  */
775   struct sigmsg sigmsg;
776   int i, r, sigflags, slot;
777 
778   assert(rmp->mp_flags & PROC_STOPPED);
779 
780   sigflags = rmp->mp_sigact[signo].sa_flags;
781   slot = (int) (rmp - mproc);
782 
783   if (rmp->mp_flags & SIGSUSPENDED)
784 	sigmsg.sm_mask = rmp->mp_sigmask2;
785   else
786 	sigmsg.sm_mask = rmp->mp_sigmask;
787   sigmsg.sm_signo = signo;
788   sigmsg.sm_sighandler =
789 	(vir_bytes) rmp->mp_sigact[signo].sa_handler;
790   sigmsg.sm_sigreturn = rmp->mp_sigreturn;
791   for (i = 1; i < _NSIG; i++) {
792 	if (sigismember(&rmp->mp_sigact[signo].sa_mask, i))
793 		sigaddset(&rmp->mp_sigmask, i);
794   }
795 
796   if (sigflags & SA_NODEFER)
797 	sigdelset(&rmp->mp_sigmask, signo);
798   else
799 	sigaddset(&rmp->mp_sigmask, signo);
800 
801   if (sigflags & SA_RESETHAND) {
802 	sigdelset(&rmp->mp_catch, signo);
803 	rmp->mp_sigact[signo].sa_handler = SIG_DFL;
804   }
805   sigdelset(&rmp->mp_sigpending, signo);
806   sigdelset(&rmp->mp_ksigpending, signo);
807 
808   /* Ask the kernel to deliver the signal */
809   r = sys_sigsend(rmp->mp_endpoint, &sigmsg);
810   /* sys_sigsend can fail legitimately with EFAULT or ENOMEM if the process
811    * memory can't accommodate the signal handler.  The target process will be
812    * killed in that case, so do not bother interrupting or resuming it.
813    */
814   if(r == EFAULT || r == ENOMEM) {
815 	return(FALSE);
816   }
817   /* Other errors are unexpected pm/kernel discrepancies. */
818   if (r != OK) {
819 	panic("sys_sigsend failed: %d", r);
820   }
821 
822   /* Was the process suspended in PM? Then interrupt the blocking call. */
823   if (rmp->mp_flags & (WAITING | SIGSUSPENDED)) {
824 	rmp->mp_flags &= ~(WAITING | SIGSUSPENDED);
825 
826 	reply(slot, EINTR);
827 
828 	/* The process must just have been stopped by unpause(), which means
829 	 * that the UNPAUSE flag is not set.
830 	 */
831 	assert(!(rmp->mp_flags & UNPAUSED));
832 
833 	try_resume_proc(rmp);
834 
835 	assert(!(rmp->mp_flags & PROC_STOPPED));
836   } else {
837 	/* If the process was not suspended in PM, VFS must first have
838 	 * confirmed that it has tried to unsuspend any blocking call. Thus, we
839 	 * got here from restart_sigs() as part of handling PM_UNPAUSE_REPLY,
840 	 * and restart_sigs() will resume the process later.
841 	 */
842 	assert(rmp->mp_flags & UNPAUSED);
843   }
844 
845   return(TRUE);
846 }
847 
848 /*===========================================================================*
849  *				vm_notify_sig_wrapper			     *
850  *===========================================================================*/
851 void vm_notify_sig_wrapper(endpoint_t ep)
852 {
853 /* get IPC's endpoint,
854  * the reason that we directly get the endpoint
855  * instead of from DS server is that otherwise
856  * it will cause deadlock between PM, VM and DS.
857  */
858   struct mproc *rmp;
859   endpoint_t ipc_ep = 0;
860 
861   for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) {
862 	if (!(rmp->mp_flags & IN_USE))
863 		continue;
864 	if (!strcmp(rmp->mp_name, "ipc")) {
865 		ipc_ep = rmp->mp_endpoint;
866 		vm_notify_sig(ep, ipc_ep);
867 
868 		return;
869 	}
870   }
871 }
872