panic() cleanup.
[minix.git] / servers / pm / signal.c
blobf4d3c6ca0e2a3ba263d959f196dfa936b953ce26
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().
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 * do_pause: perform the PAUSE system call
15 * ksig_pending: the kernel notified about pending signals
16 * sig_proc: interrupt or terminate a signaled process
17 * check_sig: check which processes to signal with sig_proc()
18 * check_pending: check if a pending signal can now be delivered
19 * restart_sigs: restart signal work after finishing a FS call
22 #include "pm.h"
23 #include <sys/stat.h>
24 #include <sys/ptrace.h>
25 #include <minix/callnr.h>
26 #include <minix/endpoint.h>
27 #include <minix/com.h>
28 #include <minix/vm.h>
29 #include <signal.h>
30 #include <sys/resource.h>
31 #include <sys/sigcontext.h>
32 #include <string.h>
33 #include "mproc.h"
34 #include "param.h"
36 FORWARD _PROTOTYPE( void unpause, (struct mproc *rmp) );
37 FORWARD _PROTOTYPE( void handle_ksig, (int proc_nr, sigset_t sig_map) );
38 FORWARD _PROTOTYPE( int sig_send, (struct mproc *rmp, int signo) );
40 /*===========================================================================*
41 * do_sigaction *
42 *===========================================================================*/
43 PUBLIC int do_sigaction()
45 int r;
46 struct sigaction svec;
47 struct sigaction *svp;
49 if (m_in.sig_nr == SIGKILL) return(OK);
50 if (m_in.sig_nr < 1 || m_in.sig_nr >= _NSIG) return(EINVAL);
51 svp = &mp->mp_sigact[m_in.sig_nr];
52 if ((struct sigaction *) m_in.sig_osa != (struct sigaction *) NULL) {
53 r = sys_datacopy(PM_PROC_NR,(vir_bytes) svp,
54 who_e, (vir_bytes) m_in.sig_osa, (phys_bytes) sizeof(svec));
55 if (r != OK) return(r);
58 if ((struct sigaction *) m_in.sig_nsa == (struct sigaction *) NULL)
59 return(OK);
61 /* Read in the sigaction structure. */
62 r = sys_datacopy(who_e, (vir_bytes) m_in.sig_nsa,
63 PM_PROC_NR, (vir_bytes) &svec, (phys_bytes) sizeof(svec));
64 if (r != OK) return(r);
66 if (svec.sa_handler == SIG_IGN) {
67 sigaddset(&mp->mp_ignore, m_in.sig_nr);
68 sigdelset(&mp->mp_sigpending, m_in.sig_nr);
69 sigdelset(&mp->mp_catch, m_in.sig_nr);
70 sigdelset(&mp->mp_sig2mess, m_in.sig_nr);
71 } else if (svec.sa_handler == SIG_DFL) {
72 sigdelset(&mp->mp_ignore, m_in.sig_nr);
73 sigdelset(&mp->mp_catch, m_in.sig_nr);
74 sigdelset(&mp->mp_sig2mess, m_in.sig_nr);
75 } else if (svec.sa_handler == SIG_MESS) {
76 if (! (mp->mp_flags & PRIV_PROC)) return(EPERM);
77 sigdelset(&mp->mp_ignore, m_in.sig_nr);
78 sigaddset(&mp->mp_sig2mess, m_in.sig_nr);
79 sigdelset(&mp->mp_catch, m_in.sig_nr);
80 } else {
81 sigdelset(&mp->mp_ignore, m_in.sig_nr);
82 sigaddset(&mp->mp_catch, m_in.sig_nr);
83 sigdelset(&mp->mp_sig2mess, m_in.sig_nr);
85 mp->mp_sigact[m_in.sig_nr].sa_handler = svec.sa_handler;
86 sigdelset(&svec.sa_mask, SIGKILL);
87 sigdelset(&svec.sa_mask, SIGSTOP);
88 mp->mp_sigact[m_in.sig_nr].sa_mask = svec.sa_mask;
89 mp->mp_sigact[m_in.sig_nr].sa_flags = svec.sa_flags;
90 mp->mp_sigreturn = (vir_bytes) m_in.sig_ret;
91 return(OK);
94 /*===========================================================================*
95 * do_sigpending *
96 *===========================================================================*/
97 PUBLIC int do_sigpending()
99 mp->mp_reply.reply_mask = (long) mp->mp_sigpending;
100 return OK;
103 /*===========================================================================*
104 * do_sigprocmask *
105 *===========================================================================*/
106 PUBLIC int do_sigprocmask()
108 /* Note that the library interface passes the actual mask in sigmask_set,
109 * not a pointer to the mask, in order to save a copy. Similarly,
110 * the old mask is placed in the return message which the library
111 * interface copies (if requested) to the user specified address.
113 * The library interface must set SIG_INQUIRE if the 'act' argument
114 * is NULL.
116 * KILL and STOP can't be masked.
119 int i;
121 mp->mp_reply.reply_mask = (long) mp->mp_sigmask;
123 switch (m_in.sig_how) {
124 case SIG_BLOCK:
125 sigdelset((sigset_t *)&m_in.sig_set, SIGKILL);
126 sigdelset((sigset_t *)&m_in.sig_set, SIGSTOP);
127 for (i = 1; i < _NSIG; i++) {
128 if (sigismember((sigset_t *)&m_in.sig_set, i))
129 sigaddset(&mp->mp_sigmask, i);
131 break;
133 case SIG_UNBLOCK:
134 for (i = 1; i < _NSIG; i++) {
135 if (sigismember((sigset_t *)&m_in.sig_set, i))
136 sigdelset(&mp->mp_sigmask, i);
138 check_pending(mp);
139 break;
141 case SIG_SETMASK:
142 sigdelset((sigset_t *) &m_in.sig_set, SIGKILL);
143 sigdelset((sigset_t *) &m_in.sig_set, SIGSTOP);
144 mp->mp_sigmask = (sigset_t) m_in.sig_set;
145 check_pending(mp);
146 break;
148 case SIG_INQUIRE:
149 break;
151 default:
152 return(EINVAL);
153 break;
155 return OK;
158 /*===========================================================================*
159 * do_sigsuspend *
160 *===========================================================================*/
161 PUBLIC int do_sigsuspend()
163 mp->mp_sigmask2 = mp->mp_sigmask; /* save the old mask */
164 mp->mp_sigmask = (sigset_t) m_in.sig_set;
165 sigdelset(&mp->mp_sigmask, SIGKILL);
166 sigdelset(&mp->mp_sigmask, SIGSTOP);
167 mp->mp_flags |= SIGSUSPENDED;
168 check_pending(mp);
169 return(SUSPEND);
172 /*===========================================================================*
173 * do_sigreturn *
174 *===========================================================================*/
175 PUBLIC int do_sigreturn()
177 /* A user signal handler is done. Restore context and check for
178 * pending unblocked signals.
181 int r;
183 mp->mp_sigmask = (sigset_t) m_in.sig_set;
184 sigdelset(&mp->mp_sigmask, SIGKILL);
185 sigdelset(&mp->mp_sigmask, SIGSTOP);
187 r = sys_sigreturn(who_e, (struct sigmsg *) m_in.sig_context);
188 check_pending(mp);
189 return(r);
192 /*===========================================================================*
193 * do_kill *
194 *===========================================================================*/
195 PUBLIC int do_kill()
197 /* Perform the kill(pid, signo) system call. */
199 return check_sig(m_in.pid, m_in.sig_nr, FALSE /* ksig */);
202 /*===========================================================================*
203 * ksig_pending *
204 *===========================================================================*/
205 PUBLIC int ksig_pending()
207 /* Certain signals, such as segmentation violations originate in the kernel.
208 * When the kernel detects such signals, it notifies the PM to take further
209 * action. The PM requests the kernel to send messages with the process
210 * slot and bit map for all signaled processes. The File System, for example,
211 * uses this mechanism to signal writing on broken pipes (SIGPIPE).
213 * The kernel has notified the PM about pending signals. Request pending
214 * signals until all signals are handled. If there are no more signals,
215 * NONE is returned in the process number field.
217 endpoint_t proc_nr_e;
218 sigset_t sig_map;
220 while (TRUE) {
221 int r;
222 /* get an arbitrary pending signal */
223 if((r=sys_getksig(&proc_nr_e, &sig_map)) != OK)
224 panic("sys_getksig failed: %d", r);
225 if (NONE == proc_nr_e) { /* stop if no more pending signals */
226 break;
227 } else {
228 int proc_nr_p;
229 if(pm_isokendpt(proc_nr_e, &proc_nr_p) != OK)
230 panic("sys_getksig strange process: %d", proc_nr_e);
231 handle_ksig(proc_nr_e, sig_map); /* handle the received signal */
232 /* If the process still exists to the kernel after the signal
233 * has been handled ...
235 if ((mproc[proc_nr_p].mp_flags & (IN_USE | EXITING)) == IN_USE)
237 if((r=sys_endksig(proc_nr_e)) != OK) /* ... tell kernel it's done */
238 panic("sys_endksig failed: %d", r);
242 return(SUSPEND); /* prevents sending reply */
245 /*===========================================================================*
246 * handle_ksig *
247 *===========================================================================*/
248 PRIVATE void handle_ksig(proc_nr_e, sig_map)
249 int proc_nr_e;
250 sigset_t sig_map;
252 register struct mproc *rmp;
253 int i, proc_nr;
254 pid_t proc_id, id;
256 if(pm_isokendpt(proc_nr_e, &proc_nr) != OK || proc_nr < 0) {
257 printf("PM: handle_ksig: %d?? not ok\n", proc_nr_e);
258 return;
260 rmp = &mproc[proc_nr];
261 if ((rmp->mp_flags & (IN_USE | EXITING)) != IN_USE) {
262 #if 0
263 printf("PM: handle_ksig: %d?? exiting / not in use\n", proc_nr_e);
264 #endif
265 return;
267 proc_id = rmp->mp_pid;
268 mp = &mproc[0]; /* pretend signals are from PM */
269 mp->mp_procgrp = rmp->mp_procgrp; /* get process group right */
271 /* Check each bit in turn to see if a signal is to be sent. Unlike
272 * kill(), the kernel may collect several unrelated signals for a
273 * process and pass them to PM in one blow. Thus loop on the bit
274 * map. For SIGVTALRM and SIGPROF, see if we need to restart a
275 * virtual timer. For SIGINT, SIGWINCH and SIGQUIT, use proc_id 0
276 * to indicate a broadcast to the recipient's process group. For
277 * SIGKILL, use proc_id -1 to indicate a systemwide broadcast.
279 for (i = 1; i < _NSIG; i++) {
280 if (!sigismember(&sig_map, i)) continue;
281 #if 0
282 printf("PM: sig %d for %d from kernel\n",
283 i, proc_nr_e);
284 #endif
285 switch (i) {
286 case SIGINT:
287 case SIGQUIT:
288 case SIGWINCH:
289 id = 0; break; /* broadcast to process group */
290 case SIGVTALRM:
291 case SIGPROF:
292 check_vtimer(proc_nr, i);
293 /* fall-through */
294 default:
295 id = proc_id;
296 break;
298 check_sig(id, i, TRUE /* ksig */);
301 /* If SIGNDELAY is set, an earlier sys_stop() failed because the process was
302 * still sending, and the kernel hereby tells us that the process is now done
303 * with that. We can now try to resume what we planned to do in the first
304 * place: set up a signal handler. However, the process's message may have
305 * been a call to PM, in which case the process may have changed any of its
306 * signal settings. The process may also have forked, exited etcetera.
308 if (sigismember(&sig_map, SIGNDELAY) && (rmp->mp_flags & DELAY_CALL)) {
309 rmp->mp_flags &= ~DELAY_CALL;
311 if (rmp->mp_flags & (FS_CALL | PM_SIG_PENDING))
312 panic("handle_ksig: bad process state");
314 /* Process as many normal signals as possible. */
315 check_pending(rmp);
317 if (rmp->mp_flags & DELAY_CALL)
318 panic("handle_ksig: multiple delay calls?");
322 /*===========================================================================*
323 * do_pause *
324 *===========================================================================*/
325 PUBLIC int do_pause()
327 /* Perform the pause() system call. */
329 mp->mp_flags |= PAUSED;
330 return(SUSPEND);
333 /*===========================================================================*
334 * sig_proc *
335 *===========================================================================*/
336 PUBLIC void sig_proc(rmp, signo, trace, ksig)
337 register struct mproc *rmp; /* pointer to the process to be signaled */
338 int signo; /* signal to send to process (1 to _NSIG-1) */
339 int trace; /* pass signal to tracer first? */
340 int ksig; /* non-zero means signal comes from kernel */
342 /* Send a signal to a process. Check to see if the signal is to be caught,
343 * ignored, tranformed into a message (for system processes) or blocked.
344 * - If the signal is to be transformed into a message, request the KERNEL to
345 * send the target process a system notification with the pending signal as an
346 * argument.
347 * - If the signal is to be caught, request the KERNEL to push a sigcontext
348 * structure and a sigframe structure onto the catcher's stack. Also, KERNEL
349 * will reset the program counter and stack pointer, so that when the process
350 * next runs, it will be executing the signal handler. When the signal handler
351 * returns, sigreturn(2) will be called. Then KERNEL will restore the signal
352 * context from the sigcontext structure.
353 * If there is insufficient stack space, kill the process.
355 int r, slot, badignore;
357 slot = (int) (rmp - mproc);
358 if ((rmp->mp_flags & (IN_USE | EXITING)) != IN_USE) {
359 printf("PM: signal %d sent to exiting process %d\n", signo, slot);
360 panic("");
363 if (trace == TRUE && rmp->mp_tracer != NO_TRACER && signo != SIGKILL) {
364 /* Signal should be passed to the debugger first.
365 * This happens before any checks on block/ignore masks; otherwise,
366 * the process itself could block/ignore debugger signals.
369 sigaddset(&rmp->mp_sigtrace, signo);
371 if (!(rmp->mp_flags & STOPPED))
372 stop_proc(rmp, signo); /* a signal causes it to stop */
374 return;
377 if (rmp->mp_flags & FS_CALL) {
378 sigaddset(&rmp->mp_sigpending, signo);
380 if (!(rmp->mp_flags & PM_SIG_PENDING)) {
381 /* No delay calls: FS_CALL implies the process called us. */
382 if ((r = sys_stop(rmp->mp_endpoint)) != OK)
383 panic("sys_stop failed: %d", r);
385 rmp->mp_flags |= PM_SIG_PENDING;
388 return;
391 /* some signals cannot be safely ignored */
392 badignore = ksig && sigismember(&noign_sset, signo) && (
393 sigismember(&rmp->mp_ignore, signo) ||
394 sigismember(&rmp->mp_sigmask, signo) ||
395 sigismember(&rmp->mp_sig2mess, signo));
397 if (!badignore && sigismember(&rmp->mp_ignore, signo)) {
398 /* Signal should be ignored. */
399 return;
401 if (!badignore && sigismember(&rmp->mp_sigmask, signo)) {
402 /* Signal should be blocked. */
403 sigaddset(&rmp->mp_sigpending, signo);
404 return;
406 if (!badignore && sigismember(&rmp->mp_sig2mess, signo)) {
407 /* Mark event pending in process slot and send notification. */
408 sigaddset(&rmp->mp_sigpending, signo);
409 notify(rmp->mp_endpoint);
410 return;
413 if ((rmp->mp_flags & STOPPED) && signo != SIGKILL) {
414 /* If the process is stopped for a debugger, do not deliver any signals
415 * (except SIGKILL) in order not to confuse the debugger. The signals
416 * will be delivered using the check_pending() calls in do_trace().
418 sigaddset(&rmp->mp_sigpending, signo);
419 return;
422 if (!badignore && sigismember(&rmp->mp_catch, signo)) {
423 /* Signal is caught. First interrupt the process's current call, if
424 * applicable. This may involve a roundtrip to FS, in which case we'll
425 * have to check back later.
427 if (!(rmp->mp_flags & UNPAUSED)) {
428 unpause(rmp);
430 if (!(rmp->mp_flags & UNPAUSED)) {
431 /* not yet unpaused; continue later */
432 sigaddset(&rmp->mp_sigpending, signo);
434 return;
438 /* Then send the actual signal to the process, by setting up a signal
439 * handler.
441 if (sig_send(rmp, signo))
442 return;
444 /* We were unable to spawn a signal handler. Kill the process. */
446 else if (!badignore && sigismember(&ign_sset, signo)) {
447 /* Signal defaults to being ignored. */
448 return;
451 /* Terminate process */
452 rmp->mp_sigstatus = (char) signo;
453 if (sigismember(&core_sset, signo)) {
454 printf("PM: coredump signal %d for %d / %s\n", signo, rmp->mp_pid,
455 rmp->mp_name);
456 exit_proc(rmp, 0, TRUE /*dump_core*/);
458 else {
459 exit_proc(rmp, 0, FALSE /*dump_core*/);
463 /*===========================================================================*
464 * check_sig *
465 *===========================================================================*/
466 PUBLIC int check_sig(proc_id, signo, ksig)
467 pid_t proc_id; /* pid of proc to sig, or 0 or -1, or -pgrp */
468 int signo; /* signal to send to process (0 to _NSIG-1) */
469 int ksig; /* non-zero means signal comes from kernel */
471 /* Check to see if it is possible to send a signal. The signal may have to be
472 * sent to a group of processes. This routine is invoked by the KILL system
473 * call, and also when the kernel catches a DEL or other signal.
476 register struct mproc *rmp;
477 int count; /* count # of signals sent */
478 int error_code;
480 if (signo < 0 || signo >= _NSIG) return(EINVAL);
482 /* Return EINVAL for attempts to send SIGKILL to INIT alone. */
483 if (proc_id == INIT_PID && signo == SIGKILL) return(EINVAL);
485 /* Search the proc table for processes to signal.
486 * (See forkexit.c about pid magic.)
488 count = 0;
489 error_code = ESRCH;
490 for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) {
491 if (!(rmp->mp_flags & IN_USE)) continue;
493 /* Check for selection. */
494 if (proc_id > 0 && proc_id != rmp->mp_pid) continue;
495 if (proc_id == 0 && mp->mp_procgrp != rmp->mp_procgrp) continue;
496 if (proc_id == -1 && rmp->mp_pid <= INIT_PID) continue;
497 if (proc_id < -1 && rmp->mp_procgrp != -proc_id) continue;
499 /* Do not kill servers and drivers when broadcasting SIGKILL. */
500 if (proc_id == -1 && signo == SIGKILL &&
501 (rmp->mp_flags & PRIV_PROC)) continue;
503 /* Check for permission. */
504 if (mp->mp_effuid != SUPER_USER
505 && mp->mp_realuid != rmp->mp_realuid
506 && mp->mp_effuid != rmp->mp_realuid
507 && mp->mp_realuid != rmp->mp_effuid
508 && mp->mp_effuid != rmp->mp_effuid) {
509 error_code = EPERM;
510 continue;
513 count++;
514 if (signo == 0 || (rmp->mp_flags & EXITING)) continue;
516 /* 'sig_proc' will handle the disposition of the signal. The
517 * signal may be caught, blocked, ignored, or cause process
518 * termination, possibly with core dump.
520 sig_proc(rmp, signo, TRUE /*trace*/, ksig);
522 if (proc_id > 0) break; /* only one process being signaled */
525 /* If the calling process has killed itself, don't reply. */
526 if ((mp->mp_flags & (IN_USE | EXITING)) != IN_USE) return(SUSPEND);
527 return(count > 0 ? OK : error_code);
530 /*===========================================================================*
531 * check_pending *
532 *===========================================================================*/
533 PUBLIC void check_pending(rmp)
534 register struct mproc *rmp;
536 /* Check to see if any pending signals have been unblocked. Deliver as many
537 * of them as we can, until we have to wait for a reply from VFS first.
539 * There are several places in this file where the signal mask is
540 * changed. At each such place, check_pending() should be called to
541 * check for newly unblocked signals.
544 int i;
546 for (i = 1; i < _NSIG; i++) {
547 if (sigismember(&rmp->mp_sigpending, i) &&
548 !sigismember(&rmp->mp_sigmask, i)) {
549 sigdelset(&rmp->mp_sigpending, i);
550 sig_proc(rmp, i, FALSE /*trace*/, FALSE /* ksig */);
552 if (rmp->mp_flags & FS_CALL)
553 break;
558 /*===========================================================================*
559 * restart_sigs *
560 *===========================================================================*/
561 PUBLIC void restart_sigs(rmp)
562 struct mproc *rmp;
564 /* FS has replied to a request from us; do signal-related work.
566 int r;
568 if (rmp->mp_flags & (FS_CALL | EXITING)) return;
570 if (rmp->mp_flags & TRACE_EXIT) {
571 /* Tracer requested exit with specific exit value */
572 exit_proc(rmp, rmp->mp_exitstatus, FALSE /*dump_core*/);
574 else if (rmp->mp_flags & PM_SIG_PENDING) {
575 /* We saved signal(s) for after finishing a FS call. Deal with this.
576 * PM_SIG_PENDING remains set to indicate the process is still stopped.
578 check_pending(rmp);
580 /* The process may now be FS-blocked again, because a signal exited the
581 * process or was caught. Restart the process only when this is NOT the
582 * case.
584 if (!(rmp->mp_flags & FS_CALL)) {
585 rmp->mp_flags &= ~(PM_SIG_PENDING | UNPAUSED);
587 if ((r = sys_resume(rmp->mp_endpoint)) != OK)
588 panic("sys_resume failed: %d", r);
593 /*===========================================================================*
594 * unpause *
595 *===========================================================================*/
596 PRIVATE void unpause(rmp)
597 struct mproc *rmp; /* which process */
599 /* A signal is to be sent to a process. If that process is hanging on a
600 * system call, the system call must be terminated with EINTR. Possible
601 * calls are PAUSE, WAIT, READ and WRITE, the latter two for pipes and ttys.
602 * First check if the process is hanging on an PM call. If not, tell FS,
603 * so it can check for READs and WRITEs from pipes, ttys and the like.
605 message m;
606 int r, slot;
608 /* If we're already waiting for a delayed call, don't do anything now. */
609 if (rmp->mp_flags & DELAY_CALL)
610 return;
612 /* Check to see if process is hanging on a PAUSE, WAIT or SIGSUSPEND call. */
613 if (rmp->mp_flags & (PAUSED | WAITING | SIGSUSPENDED)) {
614 /* Stop process from running. No delay calls: it called us. */
615 if ((r = sys_stop(rmp->mp_endpoint)) != OK)
616 panic("sys_stop failed: %d", r);
618 rmp->mp_flags |= UNPAUSED;
620 /* We interrupt the actual call from sig_send() below. */
621 return;
624 /* Not paused in PM. Let FS try to unpause the process. */
625 if (!(rmp->mp_flags & PM_SIG_PENDING)) {
626 /* Stop process from running. */
627 r = sys_delay_stop(rmp->mp_endpoint);
629 /* If the process is still busy sending a message, the kernel will give
630 * us EBUSY now and send a SIGNDELAY to the process as soon as sending
631 * is done.
633 if (r == EBUSY) {
634 rmp->mp_flags |= DELAY_CALL;
636 return;
638 else if (r != OK) panic("sys_stop failed: %d", r);
640 rmp->mp_flags |= PM_SIG_PENDING;
643 m.m_type = PM_UNPAUSE;
644 m.PM_PROC = rmp->mp_endpoint;
646 tell_fs(rmp, &m);
648 /* Also tell VM. */
649 vm_notify_sig_wrapper(rmp->mp_endpoint);
652 /*===========================================================================*
653 * sig_send *
654 *===========================================================================*/
655 PRIVATE int sig_send(rmp, signo)
656 struct mproc *rmp; /* what process to spawn a signal handler in */
657 int signo; /* signal to send to process (1 to _NSIG-1) */
659 /* The process is supposed to catch this signal. Spawn a signal handler.
660 * Return TRUE if this succeeded, FALSE otherwise.
662 struct sigmsg sigmsg;
663 vir_bytes cur_sp;
664 int r, sigflags, slot;
666 if (!(rmp->mp_flags & UNPAUSED))
667 panic("sig_send: process not unpaused");
669 sigflags = rmp->mp_sigact[signo].sa_flags;
670 slot = (int) (rmp - mproc);
672 if (rmp->mp_flags & SIGSUSPENDED)
673 sigmsg.sm_mask = rmp->mp_sigmask2;
674 else
675 sigmsg.sm_mask = rmp->mp_sigmask;
676 sigmsg.sm_signo = signo;
677 sigmsg.sm_sighandler =
678 (vir_bytes) rmp->mp_sigact[signo].sa_handler;
679 sigmsg.sm_sigreturn = rmp->mp_sigreturn;
680 rmp->mp_sigmask |= rmp->mp_sigact[signo].sa_mask;
682 if (sigflags & SA_NODEFER)
683 sigdelset(&rmp->mp_sigmask, signo);
684 else
685 sigaddset(&rmp->mp_sigmask, signo);
687 if (sigflags & SA_RESETHAND) {
688 sigdelset(&rmp->mp_catch, signo);
689 rmp->mp_sigact[signo].sa_handler = SIG_DFL;
691 sigdelset(&rmp->mp_sigpending, signo);
693 if(vm_push_sig(rmp->mp_endpoint, &cur_sp) != OK)
694 return(FALSE);
696 sigmsg.sm_stkptr = cur_sp;
698 /* Ask the kernel to deliver the signal */
699 r = sys_sigsend(rmp->mp_endpoint, &sigmsg);
700 if (r != OK)
701 panic("sys_sigsend failed: %d", r);
703 /* Was the process suspended in PM? Then interrupt the blocking call. */
704 if (rmp->mp_flags & (PAUSED | WAITING | SIGSUSPENDED)) {
705 rmp->mp_flags &= ~(PAUSED | WAITING | SIGSUSPENDED);
707 setreply(slot, EINTR);
710 /* Was the process stopped just for this signal? Then resume it. */
711 if ((rmp->mp_flags & (PM_SIG_PENDING | UNPAUSED)) == UNPAUSED) {
712 rmp->mp_flags &= ~UNPAUSED;
714 if ((r = sys_resume(rmp->mp_endpoint)) != OK)
715 panic("sys_resume failed: %d", r);
718 return(TRUE);
721 /*===========================================================================*
722 * vm_notify_sig_wrapper *
723 *===========================================================================*/
724 PUBLIC void vm_notify_sig_wrapper(endpoint_t ep)
726 /* get IPC's endpoint,
727 * the reason that we directly get the endpoint
728 * instead of from DS server is that otherwise
729 * it will cause deadlock between PM, VM and DS.
731 struct mproc *rmp;
732 endpoint_t ipc_ep = 0;
734 for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) {
735 if (!(rmp->mp_flags & IN_USE))
736 continue;
737 if (!strcmp(rmp->mp_name, "ipc")) {
738 ipc_ep = rmp->mp_endpoint;
739 vm_notify_sig(ep, ipc_ep);
741 return;