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 * 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
23 #include <sys/ptrace.h>
24 #include <minix/callnr.h>
25 #include <minix/endpoint.h>
26 #include <minix/com.h>
29 #include <sys/resource.h>
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
);
37 /*===========================================================================*
39 *===========================================================================*/
40 int do_sigaction(void)
43 struct sigaction svec
;
44 struct sigaction
*svp
;
46 assert(!(mp
->mp_flags
& (PROC_STOPPED
| VFS_CALL
| UNPAUSED
)));
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
);
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
);
59 if (m_in
.m_lc_pm_sig
.act
== 0)
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
);
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
);
76 sigdelset(&mp
->mp_ignore
, sig_nr
);
77 sigaddset(&mp
->mp_catch
, sig_nr
);
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
;
88 /*===========================================================================*
90 *===========================================================================*/
91 int do_sigpending(void)
93 assert(!(mp
->mp_flags
& (PROC_STOPPED
| VFS_CALL
| UNPAUSED
)));
95 mp
->mp_reply
.m_pm_lc_sigset
.set
= mp
->mp_sigpending
;
99 /*===========================================================================*
101 *===========================================================================*/
102 int do_sigprocmask(void)
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.
109 * The library interface must set SIG_INQUIRE if the 'act' argument
112 * KILL and STOP can't be masked.
117 assert(!(mp
->mp_flags
& (PROC_STOPPED
| VFS_CALL
| UNPAUSED
)));
119 set
= m_in
.m_lc_pm_sigset
.set
;
120 mp
->mp_reply
.m_pm_lc_sigset
.set
= mp
->mp_sigmask
;
122 switch (m_in
.m_lc_pm_sigset
.how
) {
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
);
133 for (i
= 1; i
< _NSIG
; i
++) {
134 if (sigismember(&set
, i
))
135 sigdelset(&mp
->mp_sigmask
, i
);
141 sigdelset(&set
, SIGKILL
);
142 sigdelset(&set
, SIGSTOP
);
143 mp
->mp_sigmask
= set
;
157 /*===========================================================================*
159 *===========================================================================*/
160 int do_sigsuspend(void)
162 assert(!(mp
->mp_flags
& (PROC_STOPPED
| VFS_CALL
| UNPAUSED
)));
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
;
173 /*===========================================================================*
175 *===========================================================================*/
176 int do_sigreturn(void)
178 /* A user signal handler is done. Restore context and check for
179 * pending unblocked signals.
183 assert(!(mp
->mp_flags
& (PROC_STOPPED
| VFS_CALL
| UNPAUSED
)));
185 mp
->mp_sigmask
= m_in
.m_lc_pm_sigset
.set
;
186 sigdelset(&mp
->mp_sigmask
, SIGKILL
);
187 sigdelset(&mp
->mp_sigmask
, SIGSTOP
);
189 r
= sys_sigreturn(who_e
, (struct sigmsg
*)m_in
.m_lc_pm_sigset
.ctx
);
194 /*===========================================================================*
196 *===========================================================================*/
199 /* Perform the kill(pid, signo) system call. */
201 return check_sig(m_in
.m_lc_pm_sig
.pid
, m_in
.m_lc_pm_sig
.nr
, FALSE
/* ksig */);
204 /*===========================================================================*
206 *===========================================================================*/
207 int do_srv_kill(void)
209 /* Perform the srv_kill(pid, signo) system call. */
211 /* Only RS is allowed to use srv_kill. */
212 if (mp
->mp_endpoint
!= RS_PROC_NR
)
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.
219 return check_sig(m_in
.m_rs_pm_srv_kill
.pid
, m_in
.m_rs_pm_srv_kill
.nr
,
223 /*===========================================================================*
225 *===========================================================================*/
226 static int stop_proc(struct mproc
*rmp
, int may_delay
)
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
237 assert(!(rmp
->mp_flags
& (PROC_STOPPED
| DELAY_CALL
| UNPAUSED
)));
239 r
= sys_delay_stop(rmp
->mp_endpoint
);
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.
246 rmp
->mp_flags
|= PROC_STOPPED
;
252 panic("stop_proc: unexpected delay call");
254 rmp
->mp_flags
|= DELAY_CALL
;
259 panic("sys_delay_stop failed: %d", r
);
263 /*===========================================================================*
265 *===========================================================================*/
266 static void try_resume_proc(struct mproc
*rmp
)
268 /* Resume the given process if possible. */
271 assert(rmp
->mp_flags
& PROC_STOPPED
);
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.
277 if (rmp
->mp_flags
& (VFS_CALL
| EXITING
))
280 if ((r
= sys_resume(rmp
->mp_endpoint
)) != OK
)
281 panic("sys_resume failed: %d", r
);
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.
286 rmp
->mp_flags
&= ~(PROC_STOPPED
| UNPAUSED
);
289 /*===========================================================================*
291 *===========================================================================*/
292 int process_ksig(endpoint_t proc_nr_e
, int signo
)
294 register struct mproc
*rmp
;
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. */
302 rmp
= &mproc
[proc_nr
];
303 if ((rmp
->mp_flags
& (IN_USE
| EXITING
)) != IN_USE
) {
305 printf("PM: process_ksig: %d?? exiting / not in use\n", proc_nr_e
);
307 return EDEADEPT
; /* process is gone. */
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 */
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.
323 id
= 0; break; /* broadcast to process group */
326 check_vtimer(proc_nr
, signo
);
332 check_sig(id
, signo
, TRUE
/* ksig */);
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.
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.
348 rmp
->mp_flags
&= ~DELAY_CALL
;
350 assert(!(rmp
->mp_flags
& PROC_STOPPED
));
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.
356 if (rmp
->mp_flags
& VFS_CALL
) {
357 stop_proc(rmp
, FALSE
/*may_delay*/);
362 /* Process as many normal signals as possible. */
365 assert(!(rmp
->mp_flags
& DELAY_CALL
));
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 */
373 return EDEADEPT
; /* process is gone */
377 /*===========================================================================*
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 */
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
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.
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
);
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.
412 sigaddset(&rmp
->mp_sigtrace
, signo
);
414 if (!(rmp
->mp_flags
& TRACE_STOPPED
))
415 trace_stop(rmp
, signo
); /* a signal causes it to stop */
420 if (rmp
->mp_flags
& VFS_CALL
) {
421 sigaddset(&rmp
->mp_sigpending
, signo
);
423 sigaddset(&rmp
->mp_ksigpending
, signo
);
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.
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.
437 stop_proc(rmp
, FALSE
/*delay_call*/);
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
) {
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.
454 sys_kill(rmp
->mp_endpoint
, signo
);
458 /* Print stacktrace if necessary. */
459 if(SIGS_IS_STACKTRACE(signo
)) {
460 sys_diagctl_stacktrace(rmp
->mp_endpoint
);
463 if(!SIGS_IS_TERMINATION(signo
)) {
464 /* Translate every non-termination sys signal into a message. */
466 m
.m_type
= SIGS_SIGNAL_RECEIVED
;
467 m
.m_pm_lsys_sigs_signal
.num
= signo
;
468 asynsend3(rmp
->mp_endpoint
, &m
, AMF_NOREPLY
);
471 /* Exit the process in case of termination system signal. */
472 sig_proc_exit(rmp
, signo
);
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
));
482 if (!badignore
&& sigismember(&rmp
->mp_ignore
, signo
)) {
483 /* Signal should be ignored. */
486 if (!badignore
&& sigismember(&rmp
->mp_sigmask
, signo
)) {
487 /* Signal should be blocked. */
488 sigaddset(&rmp
->mp_sigpending
, signo
);
490 sigaddset(&rmp
->mp_ksigpending
, signo
);
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().
499 sigaddset(&rmp
->mp_sigpending
, signo
);
501 sigaddset(&rmp
->mp_ksigpending
, signo
);
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.
510 /* not yet unpaused; continue later */
511 sigaddset(&rmp
->mp_sigpending
, signo
);
513 sigaddset(&rmp
->mp_ksigpending
, signo
);
518 /* Then send the actual signal to the process, by setting up a signal
521 if (sig_send(rmp
, signo
))
524 /* We were unable to spawn a signal handler. Kill the process. */
525 printf("PM: %d can't catch signal %d - killing\n",
528 else if (!badignore
&& sigismember(&ign_sset
, signo
)) {
529 /* Signal defaults to being ignored. */
533 /* Terminate process */
534 sig_proc_exit(rmp
, signo
);
537 /*===========================================================================*
539 *===========================================================================*/
540 static void sig_proc_exit(rmp
, signo
)
541 struct mproc
*rmp
; /* process that must exit */
542 int signo
; /* signal that caused termination */
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
);
551 exit_proc(rmp
, 0, TRUE
/*dump_core*/);
554 exit_proc(rmp
, 0, FALSE
/*dump_core*/);
558 /*===========================================================================*
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 */
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.
571 register struct mproc
*rmp
;
572 int count
; /* count # of signals sent */
575 if (signo
< 0 || signo
>= _NSIG
) return(EINVAL
);
577 /* Return EINVAL for attempts to send SIGKILL to INIT alone. */
578 if (proc_id
== INIT_PID
&& signo
== SIGKILL
) return(EINVAL
);
580 /* Signal RS first when broadcasting SIGTERM. */
581 if (proc_id
== -1 && signo
== SIGTERM
)
582 sys_kill(RS_PROC_NR
, signo
);
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.)
590 for (rmp
= &mproc
[NR_PROCS
-1]; rmp
>= &mproc
[0]; rmp
--) {
591 if (!(rmp
->mp_flags
& IN_USE
)) continue;
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;
599 /* Do not kill servers and drivers when broadcasting SIGKILL. */
600 if (proc_id
== -1 && signo
== SIGKILL
&&
601 (rmp
->mp_flags
& PRIV_PROC
)) continue;
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.
606 if (rmp
->mp_endpoint
== VM_PROC_NR
) continue;
608 /* Disallow lethal signals sent by user processes to sys processes. */
609 if (!ksig
&& SIGS_IS_LETHAL(signo
) && (rmp
->mp_flags
& PRIV_PROC
)) {
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
) {
625 if (signo
== 0 || (rmp
->mp_flags
& EXITING
)) continue;
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.
631 sig_proc(rmp
, signo
, TRUE
/*trace*/, ksig
);
633 if (proc_id
> 0) break; /* only one process being signaled */
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
);
641 /*===========================================================================*
643 *===========================================================================*/
644 void check_pending(rmp
)
645 register struct mproc
*rmp
;
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.
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.
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
);
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.
670 assert(rmp
->mp_flags
& PROC_STOPPED
);
677 /*===========================================================================*
679 *===========================================================================*/
680 void restart_sigs(rmp
)
683 /* VFS has replied to a request from us; do signal-related work.
686 if (rmp
->mp_flags
& (VFS_CALL
| EXITING
)) return;
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*/);
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.
697 assert(!(rmp
->mp_flags
& DELAY_CALL
));
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.
704 /* Resume the process now, unless there is a reason not to. */
705 try_resume_proc(rmp
);
709 /*===========================================================================*
711 *===========================================================================*/
712 static int unpause(rmp
)
713 struct mproc
*rmp
; /* which process */
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.
722 assert(!(rmp
->mp_flags
& VFS_CALL
));
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
);
731 /* If the process is already stopping, don't do anything now. */
732 if (rmp
->mp_flags
& DELAY_CALL
)
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.
741 stop_proc(rmp
, FALSE
/*may_delay*/);
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.
750 if (!(rmp
->mp_flags
& PROC_STOPPED
) && !stop_proc(rmp
, TRUE
/*may_delay*/))
753 memset(&m
, 0, sizeof(m
));
754 m
.m_type
= VFS_PM_UNPAUSE
;
755 m
.VFS_PM_ENDPT
= rmp
->mp_endpoint
;
760 vm_notify_sig_wrapper(rmp
->mp_endpoint
);
765 /*===========================================================================*
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) */
772 /* The process is supposed to catch this signal. Spawn a signal handler.
773 * Return TRUE if this succeeded, FALSE otherwise.
775 struct sigmsg sigmsg
;
776 int i
, r
, sigflags
, slot
;
778 assert(rmp
->mp_flags
& PROC_STOPPED
);
780 sigflags
= rmp
->mp_sigact
[signo
].sa_flags
;
781 slot
= (int) (rmp
- mproc
);
783 if (rmp
->mp_flags
& SIGSUSPENDED
)
784 sigmsg
.sm_mask
= rmp
->mp_sigmask2
;
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
);
796 if (sigflags
& SA_NODEFER
)
797 sigdelset(&rmp
->mp_sigmask
, signo
);
799 sigaddset(&rmp
->mp_sigmask
, signo
);
801 if (sigflags
& SA_RESETHAND
) {
802 sigdelset(&rmp
->mp_catch
, signo
);
803 rmp
->mp_sigact
[signo
].sa_handler
= SIG_DFL
;
805 sigdelset(&rmp
->mp_sigpending
, signo
);
806 sigdelset(&rmp
->mp_ksigpending
, signo
);
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.
814 if(r
== EFAULT
|| r
== ENOMEM
) {
817 /* Other errors are unexpected pm/kernel discrepancies. */
819 panic("sys_sigsend failed: %d", r
);
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
);
828 /* The process must just have been stopped by unpause(), which means
829 * that the UNPAUSE flag is not set.
831 assert(!(rmp
->mp_flags
& UNPAUSED
));
833 try_resume_proc(rmp
);
835 assert(!(rmp
->mp_flags
& PROC_STOPPED
));
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.
842 assert(rmp
->mp_flags
& UNPAUSED
);
848 /*===========================================================================*
849 * vm_notify_sig_wrapper *
850 *===========================================================================*/
851 void vm_notify_sig_wrapper(endpoint_t ep
)
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.
859 endpoint_t ipc_ep
= 0;
861 for (rmp
= &mproc
[0]; rmp
< &mproc
[NR_PROCS
]; rmp
++) {
862 if (!(rmp
->mp_flags
& IN_USE
))
864 if (!strcmp(rmp
->mp_name
, "ipc")) {
865 ipc_ep
= rmp
->mp_endpoint
;
866 vm_notify_sig(ep
, ipc_ep
);