1433d6423SLionel Sambuc /* This file handles signals, which are asynchronous events and are generally
2433d6423SLionel Sambuc * a messy and unpleasant business. Signals can be generated by the KILL
3433d6423SLionel Sambuc * system call, or from the keyboard (SIGINT) or from the clock (SIGALRM).
4433d6423SLionel Sambuc * In all cases control eventually passes to check_sig() to see which processes
5433d6423SLionel Sambuc * can be signaled. The actual signaling is done by sig_proc().
6433d6423SLionel Sambuc *
7433d6423SLionel Sambuc * The entry points into this file are:
8433d6423SLionel Sambuc * do_sigaction: perform the SIGACTION system call
9433d6423SLionel Sambuc * do_sigpending: perform the SIGPENDING system call
10433d6423SLionel Sambuc * do_sigprocmask: perform the SIGPROCMASK system call
11433d6423SLionel Sambuc * do_sigreturn: perform the SIGRETURN system call
12433d6423SLionel Sambuc * do_sigsuspend: perform the SIGSUSPEND system call
13433d6423SLionel Sambuc * do_kill: perform the KILL system call
14433d6423SLionel Sambuc * process_ksig: process a signal an behalf of the kernel
15433d6423SLionel Sambuc * sig_proc: interrupt or terminate a signaled process
16433d6423SLionel Sambuc * check_sig: check which processes to signal with sig_proc()
17433d6423SLionel Sambuc * check_pending: check if a pending signal can now be delivered
18433d6423SLionel Sambuc * restart_sigs: restart signal work after finishing a VFS call
19433d6423SLionel Sambuc */
20433d6423SLionel Sambuc
21433d6423SLionel Sambuc #include "pm.h"
22433d6423SLionel Sambuc #include <sys/stat.h>
23433d6423SLionel Sambuc #include <sys/ptrace.h>
24433d6423SLionel Sambuc #include <minix/callnr.h>
25433d6423SLionel Sambuc #include <minix/endpoint.h>
26433d6423SLionel Sambuc #include <minix/com.h>
27433d6423SLionel Sambuc #include <minix/vm.h>
28433d6423SLionel Sambuc #include <signal.h>
29433d6423SLionel Sambuc #include <sys/resource.h>
30433d6423SLionel Sambuc #include <assert.h>
31433d6423SLionel Sambuc #include "mproc.h"
32433d6423SLionel Sambuc
33433d6423SLionel Sambuc static int unpause(struct mproc *rmp);
34433d6423SLionel Sambuc static int sig_send(struct mproc *rmp, int signo);
35433d6423SLionel Sambuc static void sig_proc_exit(struct mproc *rmp, int signo);
36433d6423SLionel Sambuc
37433d6423SLionel Sambuc /*===========================================================================*
38433d6423SLionel Sambuc * do_sigaction *
39433d6423SLionel Sambuc *===========================================================================*/
do_sigaction(void)40433d6423SLionel Sambuc int do_sigaction(void)
41433d6423SLionel Sambuc {
42433d6423SLionel Sambuc int r, sig_nr;
43433d6423SLionel Sambuc struct sigaction svec;
44433d6423SLionel Sambuc struct sigaction *svp;
45433d6423SLionel Sambuc
46910831cbSDavid van Moolenbroek assert(!(mp->mp_flags & (PROC_STOPPED | VFS_CALL | UNPAUSED | EVENT_CALL)));
47433d6423SLionel Sambuc
48433d6423SLionel Sambuc sig_nr = m_in.m_lc_pm_sig.nr;
49433d6423SLionel Sambuc if (sig_nr == SIGKILL) return(OK);
50433d6423SLionel Sambuc if (sig_nr < 1 || sig_nr >= _NSIG) return(EINVAL);
51433d6423SLionel Sambuc
52433d6423SLionel Sambuc svp = &mp->mp_sigact[sig_nr];
53433d6423SLionel Sambuc if (m_in.m_lc_pm_sig.oact != 0) {
54433d6423SLionel Sambuc r = sys_datacopy(PM_PROC_NR,(vir_bytes) svp, who_e,
55433d6423SLionel Sambuc m_in.m_lc_pm_sig.oact, (phys_bytes) sizeof(svec));
56433d6423SLionel Sambuc if (r != OK) return(r);
57433d6423SLionel Sambuc }
58433d6423SLionel Sambuc
59433d6423SLionel Sambuc if (m_in.m_lc_pm_sig.act == 0)
60433d6423SLionel Sambuc return(OK);
61433d6423SLionel Sambuc
62433d6423SLionel Sambuc /* Read in the sigaction structure. */
63433d6423SLionel Sambuc r = sys_datacopy(who_e, m_in.m_lc_pm_sig.act, PM_PROC_NR, (vir_bytes) &svec,
64433d6423SLionel Sambuc (phys_bytes) sizeof(svec));
65433d6423SLionel Sambuc if (r != OK) return(r);
66433d6423SLionel Sambuc
67433d6423SLionel Sambuc if (svec.sa_handler == SIG_IGN) {
68433d6423SLionel Sambuc sigaddset(&mp->mp_ignore, sig_nr);
69433d6423SLionel Sambuc sigdelset(&mp->mp_sigpending, sig_nr);
70433d6423SLionel Sambuc sigdelset(&mp->mp_ksigpending, sig_nr);
71433d6423SLionel Sambuc sigdelset(&mp->mp_catch, sig_nr);
72433d6423SLionel Sambuc } else if (svec.sa_handler == SIG_DFL) {
73433d6423SLionel Sambuc sigdelset(&mp->mp_ignore, sig_nr);
74433d6423SLionel Sambuc sigdelset(&mp->mp_catch, sig_nr);
75433d6423SLionel Sambuc } else {
76433d6423SLionel Sambuc sigdelset(&mp->mp_ignore, sig_nr);
77433d6423SLionel Sambuc sigaddset(&mp->mp_catch, sig_nr);
78433d6423SLionel Sambuc }
79433d6423SLionel Sambuc mp->mp_sigact[sig_nr].sa_handler = svec.sa_handler;
80433d6423SLionel Sambuc sigdelset(&svec.sa_mask, SIGKILL);
81433d6423SLionel Sambuc sigdelset(&svec.sa_mask, SIGSTOP);
82433d6423SLionel Sambuc mp->mp_sigact[sig_nr].sa_mask = svec.sa_mask;
83433d6423SLionel Sambuc mp->mp_sigact[sig_nr].sa_flags = svec.sa_flags;
84433d6423SLionel Sambuc mp->mp_sigreturn = m_in.m_lc_pm_sig.ret;
85433d6423SLionel Sambuc return(OK);
86433d6423SLionel Sambuc }
87433d6423SLionel Sambuc
88433d6423SLionel Sambuc /*===========================================================================*
89433d6423SLionel Sambuc * do_sigpending *
90433d6423SLionel Sambuc *===========================================================================*/
do_sigpending(void)91433d6423SLionel Sambuc int do_sigpending(void)
92433d6423SLionel Sambuc {
93910831cbSDavid van Moolenbroek assert(!(mp->mp_flags & (PROC_STOPPED | VFS_CALL | UNPAUSED | EVENT_CALL)));
94433d6423SLionel Sambuc
95433d6423SLionel Sambuc mp->mp_reply.m_pm_lc_sigset.set = mp->mp_sigpending;
96433d6423SLionel Sambuc return OK;
97433d6423SLionel Sambuc }
98433d6423SLionel Sambuc
99433d6423SLionel Sambuc /*===========================================================================*
100433d6423SLionel Sambuc * do_sigprocmask *
101433d6423SLionel Sambuc *===========================================================================*/
do_sigprocmask(void)102433d6423SLionel Sambuc int do_sigprocmask(void)
103433d6423SLionel Sambuc {
104433d6423SLionel Sambuc /* Note that the library interface passes the actual mask in sigmask_set,
105433d6423SLionel Sambuc * not a pointer to the mask, in order to save a copy. Similarly,
106433d6423SLionel Sambuc * the old mask is placed in the return message which the library
107433d6423SLionel Sambuc * interface copies (if requested) to the user specified address.
108433d6423SLionel Sambuc *
109433d6423SLionel Sambuc * The library interface must set SIG_INQUIRE if the 'act' argument
110433d6423SLionel Sambuc * is NULL.
111433d6423SLionel Sambuc *
112433d6423SLionel Sambuc * KILL and STOP can't be masked.
113433d6423SLionel Sambuc */
114433d6423SLionel Sambuc sigset_t set;
115433d6423SLionel Sambuc int i;
116433d6423SLionel Sambuc
117910831cbSDavid van Moolenbroek assert(!(mp->mp_flags & (PROC_STOPPED | VFS_CALL | UNPAUSED | EVENT_CALL)));
118433d6423SLionel Sambuc
119433d6423SLionel Sambuc set = m_in.m_lc_pm_sigset.set;
120433d6423SLionel Sambuc mp->mp_reply.m_pm_lc_sigset.set = mp->mp_sigmask;
121433d6423SLionel Sambuc
122433d6423SLionel Sambuc switch (m_in.m_lc_pm_sigset.how) {
123433d6423SLionel Sambuc case SIG_BLOCK:
124433d6423SLionel Sambuc sigdelset(&set, SIGKILL);
125433d6423SLionel Sambuc sigdelset(&set, SIGSTOP);
126433d6423SLionel Sambuc for (i = 1; i < _NSIG; i++) {
127433d6423SLionel Sambuc if (sigismember(&set, i))
128433d6423SLionel Sambuc sigaddset(&mp->mp_sigmask, i);
129433d6423SLionel Sambuc }
130433d6423SLionel Sambuc break;
131433d6423SLionel Sambuc
132433d6423SLionel Sambuc case SIG_UNBLOCK:
133433d6423SLionel Sambuc for (i = 1; i < _NSIG; i++) {
134433d6423SLionel Sambuc if (sigismember(&set, i))
135433d6423SLionel Sambuc sigdelset(&mp->mp_sigmask, i);
136433d6423SLionel Sambuc }
137433d6423SLionel Sambuc check_pending(mp);
138433d6423SLionel Sambuc break;
139433d6423SLionel Sambuc
140433d6423SLionel Sambuc case SIG_SETMASK:
141433d6423SLionel Sambuc sigdelset(&set, SIGKILL);
142433d6423SLionel Sambuc sigdelset(&set, SIGSTOP);
143433d6423SLionel Sambuc mp->mp_sigmask = set;
144433d6423SLionel Sambuc check_pending(mp);
145433d6423SLionel Sambuc break;
146433d6423SLionel Sambuc
147433d6423SLionel Sambuc case SIG_INQUIRE:
148433d6423SLionel Sambuc break;
149433d6423SLionel Sambuc
150433d6423SLionel Sambuc default:
151433d6423SLionel Sambuc return(EINVAL);
152433d6423SLionel Sambuc break;
153433d6423SLionel Sambuc }
154433d6423SLionel Sambuc return OK;
155433d6423SLionel Sambuc }
156433d6423SLionel Sambuc
157433d6423SLionel Sambuc /*===========================================================================*
158433d6423SLionel Sambuc * do_sigsuspend *
159433d6423SLionel Sambuc *===========================================================================*/
do_sigsuspend(void)160433d6423SLionel Sambuc int do_sigsuspend(void)
161433d6423SLionel Sambuc {
162910831cbSDavid van Moolenbroek assert(!(mp->mp_flags & (PROC_STOPPED | VFS_CALL | UNPAUSED | EVENT_CALL)));
163433d6423SLionel Sambuc
164433d6423SLionel Sambuc mp->mp_sigmask2 = mp->mp_sigmask; /* save the old mask */
165433d6423SLionel Sambuc mp->mp_sigmask = m_in.m_lc_pm_sigset.set;
166433d6423SLionel Sambuc sigdelset(&mp->mp_sigmask, SIGKILL);
167433d6423SLionel Sambuc sigdelset(&mp->mp_sigmask, SIGSTOP);
168433d6423SLionel Sambuc mp->mp_flags |= SIGSUSPENDED;
169433d6423SLionel Sambuc check_pending(mp);
170433d6423SLionel Sambuc return(SUSPEND);
171433d6423SLionel Sambuc }
172433d6423SLionel Sambuc
173433d6423SLionel Sambuc /*===========================================================================*
174433d6423SLionel Sambuc * do_sigreturn *
175433d6423SLionel Sambuc *===========================================================================*/
do_sigreturn(void)176433d6423SLionel Sambuc int do_sigreturn(void)
177433d6423SLionel Sambuc {
178433d6423SLionel Sambuc /* A user signal handler is done. Restore context and check for
179433d6423SLionel Sambuc * pending unblocked signals.
180433d6423SLionel Sambuc */
181433d6423SLionel Sambuc int r;
182433d6423SLionel Sambuc
183910831cbSDavid van Moolenbroek assert(!(mp->mp_flags & (PROC_STOPPED | VFS_CALL | UNPAUSED | EVENT_CALL)));
184433d6423SLionel Sambuc
185433d6423SLionel Sambuc mp->mp_sigmask = m_in.m_lc_pm_sigset.set;
186433d6423SLionel Sambuc sigdelset(&mp->mp_sigmask, SIGKILL);
187433d6423SLionel Sambuc sigdelset(&mp->mp_sigmask, SIGSTOP);
188433d6423SLionel Sambuc
189433d6423SLionel Sambuc r = sys_sigreturn(who_e, (struct sigmsg *)m_in.m_lc_pm_sigset.ctx);
190433d6423SLionel Sambuc check_pending(mp);
191433d6423SLionel Sambuc return(r);
192433d6423SLionel Sambuc }
193433d6423SLionel Sambuc
194433d6423SLionel Sambuc /*===========================================================================*
195433d6423SLionel Sambuc * do_kill *
196433d6423SLionel Sambuc *===========================================================================*/
do_kill(void)197433d6423SLionel Sambuc int do_kill(void)
198433d6423SLionel Sambuc {
199433d6423SLionel Sambuc /* Perform the kill(pid, signo) system call. */
200433d6423SLionel Sambuc
201433d6423SLionel Sambuc return check_sig(m_in.m_lc_pm_sig.pid, m_in.m_lc_pm_sig.nr, FALSE /* ksig */);
202433d6423SLionel Sambuc }
203433d6423SLionel Sambuc
204433d6423SLionel Sambuc /*===========================================================================*
205433d6423SLionel Sambuc * do_srv_kill *
206433d6423SLionel Sambuc *===========================================================================*/
do_srv_kill(void)207433d6423SLionel Sambuc int do_srv_kill(void)
208433d6423SLionel Sambuc {
209433d6423SLionel Sambuc /* Perform the srv_kill(pid, signo) system call. */
210433d6423SLionel Sambuc
211433d6423SLionel Sambuc /* Only RS is allowed to use srv_kill. */
212433d6423SLionel Sambuc if (mp->mp_endpoint != RS_PROC_NR)
213433d6423SLionel Sambuc return EPERM;
214433d6423SLionel Sambuc
215433d6423SLionel Sambuc /* Pretend the signal comes from the kernel when RS wants to deliver a signal
216433d6423SLionel Sambuc * to a system process. RS sends a SIGKILL when it wants to perform cleanup.
217433d6423SLionel Sambuc * In that case, ksig == TRUE forces PM to exit the process immediately.
218433d6423SLionel Sambuc */
219433d6423SLionel Sambuc return check_sig(m_in.m_rs_pm_srv_kill.pid, m_in.m_rs_pm_srv_kill.nr,
220433d6423SLionel Sambuc TRUE /* ksig */);
221433d6423SLionel Sambuc }
222433d6423SLionel Sambuc
223433d6423SLionel Sambuc /*===========================================================================*
224433d6423SLionel Sambuc * stop_proc *
225433d6423SLionel Sambuc *===========================================================================*/
stop_proc(struct mproc * rmp,int may_delay)226433d6423SLionel Sambuc static int stop_proc(struct mproc *rmp, int may_delay)
227433d6423SLionel Sambuc {
228433d6423SLionel Sambuc /* Try to stop the given process in the kernel. If successful, mark the process
229433d6423SLionel Sambuc * as stopped and return TRUE. If the process is still busy sending a message,
230433d6423SLionel Sambuc * the behavior depends on the 'may_delay' parameter. If set, the process will
231433d6423SLionel Sambuc * be marked as having a delay call pending, and the function returns FALSE. If
232433d6423SLionel Sambuc * not set, the caller already knows that the process has no delay call, and PM
233433d6423SLionel Sambuc * will panic.
234433d6423SLionel Sambuc */
235433d6423SLionel Sambuc int r;
236433d6423SLionel Sambuc
237433d6423SLionel Sambuc assert(!(rmp->mp_flags & (PROC_STOPPED | DELAY_CALL | UNPAUSED)));
238433d6423SLionel Sambuc
239433d6423SLionel Sambuc r = sys_delay_stop(rmp->mp_endpoint);
240433d6423SLionel Sambuc
241433d6423SLionel Sambuc /* If the process is still busy sending a message, the kernel will give us
242433d6423SLionel Sambuc * EBUSY now and send a SIGSNDELAY to the process as soon as sending is done.
243433d6423SLionel Sambuc */
244433d6423SLionel Sambuc switch (r) {
245433d6423SLionel Sambuc case OK:
246433d6423SLionel Sambuc rmp->mp_flags |= PROC_STOPPED;
247433d6423SLionel Sambuc
248433d6423SLionel Sambuc return TRUE;
249433d6423SLionel Sambuc
250433d6423SLionel Sambuc case EBUSY:
251433d6423SLionel Sambuc if (!may_delay)
252433d6423SLionel Sambuc panic("stop_proc: unexpected delay call");
253433d6423SLionel Sambuc
254433d6423SLionel Sambuc rmp->mp_flags |= DELAY_CALL;
255433d6423SLionel Sambuc
256433d6423SLionel Sambuc return FALSE;
257433d6423SLionel Sambuc
258433d6423SLionel Sambuc default:
259433d6423SLionel Sambuc panic("sys_delay_stop failed: %d", r);
260433d6423SLionel Sambuc }
261433d6423SLionel Sambuc }
262433d6423SLionel Sambuc
263433d6423SLionel Sambuc /*===========================================================================*
264433d6423SLionel Sambuc * try_resume_proc *
265433d6423SLionel Sambuc *===========================================================================*/
try_resume_proc(struct mproc * rmp)266433d6423SLionel Sambuc static void try_resume_proc(struct mproc *rmp)
267433d6423SLionel Sambuc {
268433d6423SLionel Sambuc /* Resume the given process if possible. */
269433d6423SLionel Sambuc int r;
270433d6423SLionel Sambuc
271433d6423SLionel Sambuc assert(rmp->mp_flags & PROC_STOPPED);
272433d6423SLionel Sambuc
273910831cbSDavid van Moolenbroek /* If the process is blocked on a VFS call or a process event notification,
274910831cbSDavid van Moolenbroek * do not resume it now. Most likely it will be unpausing, in which case the
275910831cbSDavid van Moolenbroek * process must remain stopped. Otherwise, it will still be resumed once the
276910831cbSDavid van Moolenbroek * VFS or event call is replied to. If the process has died, do not resume
277910831cbSDavid van Moolenbroek * it either.
278433d6423SLionel Sambuc */
279910831cbSDavid van Moolenbroek if (rmp->mp_flags & (VFS_CALL | EVENT_CALL | EXITING))
280433d6423SLionel Sambuc return;
281433d6423SLionel Sambuc
282433d6423SLionel Sambuc if ((r = sys_resume(rmp->mp_endpoint)) != OK)
283433d6423SLionel Sambuc panic("sys_resume failed: %d", r);
284433d6423SLionel Sambuc
285433d6423SLionel Sambuc /* Also unset the unpaused flag. We can safely assume that a stopped process
286433d6423SLionel Sambuc * need only be unpaused once, but once it is resumed, all bets are off.
287433d6423SLionel Sambuc */
288433d6423SLionel Sambuc rmp->mp_flags &= ~(PROC_STOPPED | UNPAUSED);
289433d6423SLionel Sambuc }
290433d6423SLionel Sambuc
291433d6423SLionel Sambuc /*===========================================================================*
292433d6423SLionel Sambuc * process_ksig *
293433d6423SLionel Sambuc *===========================================================================*/
process_ksig(endpoint_t proc_nr_e,int signo)294433d6423SLionel Sambuc int process_ksig(endpoint_t proc_nr_e, int signo)
295433d6423SLionel Sambuc {
296433d6423SLionel Sambuc register struct mproc *rmp;
297433d6423SLionel Sambuc int proc_nr;
298433d6423SLionel Sambuc pid_t proc_id, id;
299433d6423SLionel Sambuc
300433d6423SLionel Sambuc if(pm_isokendpt(proc_nr_e, &proc_nr) != OK) {
301433d6423SLionel Sambuc printf("PM: process_ksig: %d?? not ok\n", proc_nr_e);
302433d6423SLionel Sambuc return EDEADEPT; /* process is gone. */
303433d6423SLionel Sambuc }
304433d6423SLionel Sambuc rmp = &mproc[proc_nr];
305433d6423SLionel Sambuc if ((rmp->mp_flags & (IN_USE | EXITING)) != IN_USE) {
306433d6423SLionel Sambuc #if 0
307433d6423SLionel Sambuc printf("PM: process_ksig: %d?? exiting / not in use\n", proc_nr_e);
308433d6423SLionel Sambuc #endif
309433d6423SLionel Sambuc return EDEADEPT; /* process is gone. */
310433d6423SLionel Sambuc }
311433d6423SLionel Sambuc proc_id = rmp->mp_pid;
312433d6423SLionel Sambuc mp = &mproc[0]; /* pretend signals are from PM */
313433d6423SLionel Sambuc mp->mp_procgrp = rmp->mp_procgrp; /* get process group right */
314433d6423SLionel Sambuc
315433d6423SLionel Sambuc /* For SIGVTALRM and SIGPROF, see if we need to restart a
316433d6423SLionel Sambuc * virtual timer. For SIGINT, SIGINFO, SIGWINCH and SIGQUIT, use proc_id 0
317433d6423SLionel Sambuc * to indicate a broadcast to the recipient's process group. For
318433d6423SLionel Sambuc * SIGKILL, use proc_id -1 to indicate a systemwide broadcast.
319433d6423SLionel Sambuc */
320433d6423SLionel Sambuc switch (signo) {
321433d6423SLionel Sambuc case SIGINT:
322433d6423SLionel Sambuc case SIGQUIT:
323433d6423SLionel Sambuc case SIGWINCH:
324433d6423SLionel Sambuc case SIGINFO:
325433d6423SLionel Sambuc id = 0; break; /* broadcast to process group */
326433d6423SLionel Sambuc case SIGVTALRM:
327433d6423SLionel Sambuc case SIGPROF:
328433d6423SLionel Sambuc check_vtimer(proc_nr, signo);
329433d6423SLionel Sambuc /* fall-through */
330433d6423SLionel Sambuc default:
331433d6423SLionel Sambuc id = proc_id;
332433d6423SLionel Sambuc break;
333433d6423SLionel Sambuc }
334433d6423SLionel Sambuc check_sig(id, signo, TRUE /* ksig */);
3351b75f635SDavid van Moolenbroek mp->mp_procgrp = 0; /* restore proper PM process group */
336433d6423SLionel Sambuc
337433d6423SLionel Sambuc /* If SIGSNDELAY is set, an earlier sys_stop() failed because the process was
338433d6423SLionel Sambuc * still sending, and the kernel hereby tells us that the process is now done
339433d6423SLionel Sambuc * with that. We can now try to resume what we planned to do in the first
340433d6423SLionel Sambuc * place: set up a signal handler. However, the process's message may have
341433d6423SLionel Sambuc * been a call to PM, in which case the process may have changed any of its
342433d6423SLionel Sambuc * signal settings. The process may also have forked, exited etcetera.
343433d6423SLionel Sambuc */
344433d6423SLionel Sambuc if (signo == SIGSNDELAY && (rmp->mp_flags & DELAY_CALL)) {
345433d6423SLionel Sambuc /* When getting SIGSNDELAY, the process is stopped at least until the
346433d6423SLionel Sambuc * receipt of the SIGSNDELAY signal is acknowledged to the kernel. The
347433d6423SLionel Sambuc * process is not stopped on PROC_STOP in the kernel. However, now that
348433d6423SLionel Sambuc * there is no longer a delay call, stop_proc() is guaranteed to
349433d6423SLionel Sambuc * succeed immediately.
350433d6423SLionel Sambuc */
351433d6423SLionel Sambuc rmp->mp_flags &= ~DELAY_CALL;
352433d6423SLionel Sambuc
353433d6423SLionel Sambuc assert(!(rmp->mp_flags & PROC_STOPPED));
354433d6423SLionel Sambuc
355433d6423SLionel Sambuc /* If the delay call was to PM, it may have resulted in a VFS call. In
356433d6423SLionel Sambuc * that case, we must wait with further signal processing until VFS has
357433d6423SLionel Sambuc * replied. Stop the process.
358433d6423SLionel Sambuc */
359910831cbSDavid van Moolenbroek if (rmp->mp_flags & (VFS_CALL | EVENT_CALL)) {
360433d6423SLionel Sambuc stop_proc(rmp, FALSE /*may_delay*/);
361433d6423SLionel Sambuc
362433d6423SLionel Sambuc return OK;
363433d6423SLionel Sambuc }
364433d6423SLionel Sambuc
365433d6423SLionel Sambuc /* Process as many normal signals as possible. */
366433d6423SLionel Sambuc check_pending(rmp);
367433d6423SLionel Sambuc
368433d6423SLionel Sambuc assert(!(rmp->mp_flags & DELAY_CALL));
369433d6423SLionel Sambuc }
370433d6423SLionel Sambuc
371433d6423SLionel Sambuc /* See if the process is still alive */
372433d6423SLionel Sambuc if ((mproc[proc_nr].mp_flags & (IN_USE | EXITING)) == IN_USE) {
373433d6423SLionel Sambuc return OK; /* signal has been delivered */
374433d6423SLionel Sambuc }
375433d6423SLionel Sambuc else {
376433d6423SLionel Sambuc return EDEADEPT; /* process is gone */
377433d6423SLionel Sambuc }
378433d6423SLionel Sambuc }
379433d6423SLionel Sambuc
380433d6423SLionel Sambuc /*===========================================================================*
381433d6423SLionel Sambuc * sig_proc *
382433d6423SLionel Sambuc *===========================================================================*/
383*637f688fSRichard Sailer void
sig_proc(register struct mproc * rmp,int signo,int trace,int ksig)384*637f688fSRichard Sailer sig_proc(
385*637f688fSRichard Sailer register struct mproc *rmp, /* pointer to the process to be signaled */
386*637f688fSRichard Sailer int signo, /* signal to send to process (1 to _NSIG-1) */
387*637f688fSRichard Sailer int trace, /* pass signal to tracer first? */
388*637f688fSRichard Sailer int ksig /* non-zero means signal comes from kernel */
389*637f688fSRichard Sailer )
390433d6423SLionel Sambuc {
391433d6423SLionel Sambuc /* Send a signal to a process. Check to see if the signal is to be caught,
392433d6423SLionel Sambuc * ignored, tranformed into a message (for system processes) or blocked.
393433d6423SLionel Sambuc * - If the signal is to be transformed into a message, request the KERNEL to
394433d6423SLionel Sambuc * send the target process a system notification with the pending signal as an
395433d6423SLionel Sambuc * argument.
396433d6423SLionel Sambuc * - If the signal is to be caught, request the KERNEL to push a sigcontext
397433d6423SLionel Sambuc * structure and a sigframe structure onto the catcher's stack. Also, KERNEL
398433d6423SLionel Sambuc * will reset the program counter and stack pointer, so that when the process
399433d6423SLionel Sambuc * next runs, it will be executing the signal handler. When the signal handler
400433d6423SLionel Sambuc * returns, sigreturn(2) will be called. Then KERNEL will restore the signal
401433d6423SLionel Sambuc * context from the sigcontext structure.
402433d6423SLionel Sambuc * If there is insufficient stack space, kill the process.
403433d6423SLionel Sambuc */
404433d6423SLionel Sambuc int slot, badignore;
405433d6423SLionel Sambuc
406433d6423SLionel Sambuc slot = (int) (rmp - mproc);
407433d6423SLionel Sambuc if ((rmp->mp_flags & (IN_USE | EXITING)) != IN_USE) {
408433d6423SLionel Sambuc panic("PM: signal %d sent to exiting process %d\n", signo, slot);
409433d6423SLionel Sambuc }
410433d6423SLionel Sambuc
411433d6423SLionel Sambuc if (trace == TRUE && rmp->mp_tracer != NO_TRACER && signo != SIGKILL) {
412433d6423SLionel Sambuc /* Signal should be passed to the debugger first.
413433d6423SLionel Sambuc * This happens before any checks on block/ignore masks; otherwise,
414433d6423SLionel Sambuc * the process itself could block/ignore debugger signals.
415433d6423SLionel Sambuc */
416433d6423SLionel Sambuc
417433d6423SLionel Sambuc sigaddset(&rmp->mp_sigtrace, signo);
418433d6423SLionel Sambuc
419433d6423SLionel Sambuc if (!(rmp->mp_flags & TRACE_STOPPED))
420433d6423SLionel Sambuc trace_stop(rmp, signo); /* a signal causes it to stop */
421433d6423SLionel Sambuc
422433d6423SLionel Sambuc return;
423433d6423SLionel Sambuc }
424433d6423SLionel Sambuc
425910831cbSDavid van Moolenbroek if (rmp->mp_flags & (VFS_CALL | EVENT_CALL)) {
426433d6423SLionel Sambuc sigaddset(&rmp->mp_sigpending, signo);
427433d6423SLionel Sambuc if(ksig)
428433d6423SLionel Sambuc sigaddset(&rmp->mp_ksigpending, signo);
429433d6423SLionel Sambuc
430910831cbSDavid van Moolenbroek /* Process the signal once VFS and process event subscribers reply.
431910831cbSDavid van Moolenbroek * Stop the process in the meantime, so that it cannot make another
432910831cbSDavid van Moolenbroek * call after the VFS reply comes in but before we look at its signals
433910831cbSDavid van Moolenbroek * again. Since we always stop the process to deliver signals during a
434910831cbSDavid van Moolenbroek * VFS or event call, the PROC_STOPPED flag doubles as an indicator in
435910831cbSDavid van Moolenbroek * restart_sigs() that signals must be rechecked after a reply arrives.
436433d6423SLionel Sambuc */
437433d6423SLionel Sambuc if (!(rmp->mp_flags & (PROC_STOPPED | DELAY_CALL))) {
438433d6423SLionel Sambuc /* If a VFS call is ongoing and the process is not yet stopped,
439433d6423SLionel Sambuc * the process must have made a call to PM. Therefore, there
440433d6423SLionel Sambuc * can be no delay calls in this case.
441433d6423SLionel Sambuc */
442433d6423SLionel Sambuc stop_proc(rmp, FALSE /*delay_call*/);
443433d6423SLionel Sambuc }
444433d6423SLionel Sambuc return;
445433d6423SLionel Sambuc }
446433d6423SLionel Sambuc
447433d6423SLionel Sambuc /* Handle system signals for system processes first. */
448433d6423SLionel Sambuc if(rmp->mp_flags & PRIV_PROC) {
449433d6423SLionel Sambuc /* Always skip signals for PM (only necessary when broadcasting). */
450433d6423SLionel Sambuc if(rmp->mp_endpoint == PM_PROC_NR) {
451433d6423SLionel Sambuc return;
452433d6423SLionel Sambuc }
453433d6423SLionel Sambuc
454433d6423SLionel Sambuc /* System signals have always to go through the kernel first to let it
455433d6423SLionel Sambuc * pick the right signal manager. If PM is the assigned signal manager,
456433d6423SLionel Sambuc * the signal will come back and will actually be processed.
457433d6423SLionel Sambuc */
458433d6423SLionel Sambuc if(!ksig) {
459433d6423SLionel Sambuc sys_kill(rmp->mp_endpoint, signo);
460433d6423SLionel Sambuc return;
461433d6423SLionel Sambuc }
462433d6423SLionel Sambuc
463433d6423SLionel Sambuc /* Print stacktrace if necessary. */
464433d6423SLionel Sambuc if(SIGS_IS_STACKTRACE(signo)) {
465433d6423SLionel Sambuc sys_diagctl_stacktrace(rmp->mp_endpoint);
466433d6423SLionel Sambuc }
467433d6423SLionel Sambuc
468433d6423SLionel Sambuc if(!SIGS_IS_TERMINATION(signo)) {
469433d6423SLionel Sambuc /* Translate every non-termination sys signal into a message. */
470433d6423SLionel Sambuc message m;
471433d6423SLionel Sambuc m.m_type = SIGS_SIGNAL_RECEIVED;
472433d6423SLionel Sambuc m.m_pm_lsys_sigs_signal.num = signo;
473433d6423SLionel Sambuc asynsend3(rmp->mp_endpoint, &m, AMF_NOREPLY);
474433d6423SLionel Sambuc }
475433d6423SLionel Sambuc else {
476433d6423SLionel Sambuc /* Exit the process in case of termination system signal. */
477433d6423SLionel Sambuc sig_proc_exit(rmp, signo);
478433d6423SLionel Sambuc }
479433d6423SLionel Sambuc return;
480433d6423SLionel Sambuc }
481433d6423SLionel Sambuc
482433d6423SLionel Sambuc /* Handle user processes now. See if the signal cannot be safely ignored. */
483433d6423SLionel Sambuc badignore = ksig && sigismember(&noign_sset, signo) && (
484433d6423SLionel Sambuc sigismember(&rmp->mp_ignore, signo) ||
485433d6423SLionel Sambuc sigismember(&rmp->mp_sigmask, signo));
486433d6423SLionel Sambuc
487433d6423SLionel Sambuc if (!badignore && sigismember(&rmp->mp_ignore, signo)) {
488433d6423SLionel Sambuc /* Signal should be ignored. */
489433d6423SLionel Sambuc return;
490433d6423SLionel Sambuc }
491433d6423SLionel Sambuc if (!badignore && sigismember(&rmp->mp_sigmask, signo)) {
492433d6423SLionel Sambuc /* Signal should be blocked. */
493433d6423SLionel Sambuc sigaddset(&rmp->mp_sigpending, signo);
494433d6423SLionel Sambuc if(ksig)
495433d6423SLionel Sambuc sigaddset(&rmp->mp_ksigpending, signo);
496433d6423SLionel Sambuc return;
497433d6423SLionel Sambuc }
498433d6423SLionel Sambuc
499433d6423SLionel Sambuc if ((rmp->mp_flags & TRACE_STOPPED) && signo != SIGKILL) {
500433d6423SLionel Sambuc /* If the process is stopped for a debugger, do not deliver any signals
501433d6423SLionel Sambuc * (except SIGKILL) in order not to confuse the debugger. The signals
502433d6423SLionel Sambuc * will be delivered using the check_pending() calls in do_trace().
503433d6423SLionel Sambuc */
504433d6423SLionel Sambuc sigaddset(&rmp->mp_sigpending, signo);
505433d6423SLionel Sambuc if(ksig)
506433d6423SLionel Sambuc sigaddset(&rmp->mp_ksigpending, signo);
507433d6423SLionel Sambuc return;
508433d6423SLionel Sambuc }
509433d6423SLionel Sambuc if (!badignore && sigismember(&rmp->mp_catch, signo)) {
510433d6423SLionel Sambuc /* Signal is caught. First interrupt the process's current call, if
511433d6423SLionel Sambuc * applicable. This may involve a roundtrip to VFS, in which case we'll
512433d6423SLionel Sambuc * have to check back later.
513433d6423SLionel Sambuc */
514433d6423SLionel Sambuc if (!unpause(rmp)) {
515433d6423SLionel Sambuc /* not yet unpaused; continue later */
516433d6423SLionel Sambuc sigaddset(&rmp->mp_sigpending, signo);
517433d6423SLionel Sambuc if(ksig)
518433d6423SLionel Sambuc sigaddset(&rmp->mp_ksigpending, signo);
519433d6423SLionel Sambuc
520433d6423SLionel Sambuc return;
521433d6423SLionel Sambuc }
522433d6423SLionel Sambuc
523433d6423SLionel Sambuc /* Then send the actual signal to the process, by setting up a signal
524433d6423SLionel Sambuc * handler.
525433d6423SLionel Sambuc */
526433d6423SLionel Sambuc if (sig_send(rmp, signo))
527433d6423SLionel Sambuc return;
528433d6423SLionel Sambuc
529433d6423SLionel Sambuc /* We were unable to spawn a signal handler. Kill the process. */
530433d6423SLionel Sambuc printf("PM: %d can't catch signal %d - killing\n",
531433d6423SLionel Sambuc rmp->mp_pid, signo);
532433d6423SLionel Sambuc }
533433d6423SLionel Sambuc else if (!badignore && sigismember(&ign_sset, signo)) {
534433d6423SLionel Sambuc /* Signal defaults to being ignored. */
535433d6423SLionel Sambuc return;
536433d6423SLionel Sambuc }
537433d6423SLionel Sambuc
538433d6423SLionel Sambuc /* Terminate process */
539433d6423SLionel Sambuc sig_proc_exit(rmp, signo);
540433d6423SLionel Sambuc }
541433d6423SLionel Sambuc
542433d6423SLionel Sambuc /*===========================================================================*
543433d6423SLionel Sambuc * sig_proc_exit *
544433d6423SLionel Sambuc *===========================================================================*/
545*637f688fSRichard Sailer static void
sig_proc_exit(struct mproc * rmp,int signo)546*637f688fSRichard Sailer sig_proc_exit(
547*637f688fSRichard Sailer struct mproc *rmp, /* process that must exit */
548*637f688fSRichard Sailer int signo /* signal that caused termination */
549*637f688fSRichard Sailer )
550433d6423SLionel Sambuc {
551433d6423SLionel Sambuc rmp->mp_sigstatus = (char) signo;
552433d6423SLionel Sambuc if (sigismember(&core_sset, signo)) {
553433d6423SLionel Sambuc if(!(rmp->mp_flags & PRIV_PROC)) {
554433d6423SLionel Sambuc printf("PM: coredump signal %d for %d / %s\n", signo,
555433d6423SLionel Sambuc rmp->mp_pid, rmp->mp_name);
556433d6423SLionel Sambuc sys_diagctl_stacktrace(rmp->mp_endpoint);
557433d6423SLionel Sambuc }
558433d6423SLionel Sambuc exit_proc(rmp, 0, TRUE /*dump_core*/);
559433d6423SLionel Sambuc }
560433d6423SLionel Sambuc else {
561433d6423SLionel Sambuc exit_proc(rmp, 0, FALSE /*dump_core*/);
562433d6423SLionel Sambuc }
563433d6423SLionel Sambuc }
564433d6423SLionel Sambuc
565433d6423SLionel Sambuc /*===========================================================================*
566433d6423SLionel Sambuc * check_sig *
567433d6423SLionel Sambuc *===========================================================================*/
check_sig(proc_id,signo,ksig)568433d6423SLionel Sambuc int check_sig(proc_id, signo, ksig)
569433d6423SLionel Sambuc pid_t proc_id; /* pid of proc to sig, or 0 or -1, or -pgrp */
570433d6423SLionel Sambuc int signo; /* signal to send to process (0 to _NSIG-1) */
571433d6423SLionel Sambuc int ksig; /* non-zero means signal comes from kernel */
572433d6423SLionel Sambuc {
573433d6423SLionel Sambuc /* Check to see if it is possible to send a signal. The signal may have to be
574433d6423SLionel Sambuc * sent to a group of processes. This routine is invoked by the KILL system
575433d6423SLionel Sambuc * call, and also when the kernel catches a DEL or other signal.
576433d6423SLionel Sambuc */
577433d6423SLionel Sambuc
578433d6423SLionel Sambuc register struct mproc *rmp;
579433d6423SLionel Sambuc int count; /* count # of signals sent */
580433d6423SLionel Sambuc int error_code;
581433d6423SLionel Sambuc
582433d6423SLionel Sambuc if (signo < 0 || signo >= _NSIG) return(EINVAL);
583433d6423SLionel Sambuc
584433d6423SLionel Sambuc /* Return EINVAL for attempts to send SIGKILL to INIT alone. */
585433d6423SLionel Sambuc if (proc_id == INIT_PID && signo == SIGKILL) return(EINVAL);
586433d6423SLionel Sambuc
587433d6423SLionel Sambuc /* Signal RS first when broadcasting SIGTERM. */
588433d6423SLionel Sambuc if (proc_id == -1 && signo == SIGTERM)
589433d6423SLionel Sambuc sys_kill(RS_PROC_NR, signo);
590433d6423SLionel Sambuc
591433d6423SLionel Sambuc /* Search the proc table for processes to signal. Start from the end of the
592433d6423SLionel Sambuc * table to analyze core system processes at the end when broadcasting.
593433d6423SLionel Sambuc * (See forkexit.c about pid magic.)
594433d6423SLionel Sambuc */
595433d6423SLionel Sambuc count = 0;
596433d6423SLionel Sambuc error_code = ESRCH;
597433d6423SLionel Sambuc for (rmp = &mproc[NR_PROCS-1]; rmp >= &mproc[0]; rmp--) {
598433d6423SLionel Sambuc if (!(rmp->mp_flags & IN_USE)) continue;
599433d6423SLionel Sambuc
600433d6423SLionel Sambuc /* Check for selection. */
601433d6423SLionel Sambuc if (proc_id > 0 && proc_id != rmp->mp_pid) continue;
602433d6423SLionel Sambuc if (proc_id == 0 && mp->mp_procgrp != rmp->mp_procgrp) continue;
603433d6423SLionel Sambuc if (proc_id == -1 && rmp->mp_pid <= INIT_PID) continue;
604433d6423SLionel Sambuc if (proc_id < -1 && rmp->mp_procgrp != -proc_id) continue;
605433d6423SLionel Sambuc
606433d6423SLionel Sambuc /* Do not kill servers and drivers when broadcasting SIGKILL. */
607433d6423SLionel Sambuc if (proc_id == -1 && signo == SIGKILL &&
608433d6423SLionel Sambuc (rmp->mp_flags & PRIV_PROC)) continue;
609433d6423SLionel Sambuc
610433d6423SLionel Sambuc /* Skip VM entirely as it might lead to a deadlock with its signal
611433d6423SLionel Sambuc * manager if the manager page faults at the same time.
612433d6423SLionel Sambuc */
613433d6423SLionel Sambuc if (rmp->mp_endpoint == VM_PROC_NR) continue;
614433d6423SLionel Sambuc
615433d6423SLionel Sambuc /* Disallow lethal signals sent by user processes to sys processes. */
616433d6423SLionel Sambuc if (!ksig && SIGS_IS_LETHAL(signo) && (rmp->mp_flags & PRIV_PROC)) {
617433d6423SLionel Sambuc error_code = EPERM;
618433d6423SLionel Sambuc continue;
619433d6423SLionel Sambuc }
620433d6423SLionel Sambuc
621433d6423SLionel Sambuc /* Check for permission. */
622433d6423SLionel Sambuc if (mp->mp_effuid != SUPER_USER
623433d6423SLionel Sambuc && mp->mp_realuid != rmp->mp_realuid
624433d6423SLionel Sambuc && mp->mp_effuid != rmp->mp_realuid
625433d6423SLionel Sambuc && mp->mp_realuid != rmp->mp_effuid
626433d6423SLionel Sambuc && mp->mp_effuid != rmp->mp_effuid) {
627433d6423SLionel Sambuc error_code = EPERM;
628433d6423SLionel Sambuc continue;
629433d6423SLionel Sambuc }
630433d6423SLionel Sambuc
631433d6423SLionel Sambuc count++;
632433d6423SLionel Sambuc if (signo == 0 || (rmp->mp_flags & EXITING)) continue;
633433d6423SLionel Sambuc
634433d6423SLionel Sambuc /* 'sig_proc' will handle the disposition of the signal. The
635433d6423SLionel Sambuc * signal may be caught, blocked, ignored, or cause process
636433d6423SLionel Sambuc * termination, possibly with core dump.
637433d6423SLionel Sambuc */
638433d6423SLionel Sambuc sig_proc(rmp, signo, TRUE /*trace*/, ksig);
639433d6423SLionel Sambuc
640433d6423SLionel Sambuc if (proc_id > 0) break; /* only one process being signaled */
641433d6423SLionel Sambuc }
642433d6423SLionel Sambuc
643433d6423SLionel Sambuc /* If the calling process has killed itself, don't reply. */
644433d6423SLionel Sambuc if ((mp->mp_flags & (IN_USE | EXITING)) != IN_USE) return(SUSPEND);
645433d6423SLionel Sambuc return(count > 0 ? OK : error_code);
646433d6423SLionel Sambuc }
647433d6423SLionel Sambuc
648433d6423SLionel Sambuc /*===========================================================================*
649433d6423SLionel Sambuc * check_pending *
650433d6423SLionel Sambuc *===========================================================================*/
651*637f688fSRichard Sailer void
check_pending(register struct mproc * rmp)652*637f688fSRichard Sailer check_pending(register struct mproc *rmp)
653433d6423SLionel Sambuc {
654433d6423SLionel Sambuc /* Check to see if any pending signals have been unblocked. Deliver as many
655433d6423SLionel Sambuc * of them as we can, until we have to wait for a reply from VFS first.
656433d6423SLionel Sambuc *
657433d6423SLionel Sambuc * There are several places in this file where the signal mask is
658433d6423SLionel Sambuc * changed. At each such place, check_pending() should be called to
659433d6423SLionel Sambuc * check for newly unblocked signals.
660433d6423SLionel Sambuc */
661433d6423SLionel Sambuc int i;
662433d6423SLionel Sambuc int ksig;
663433d6423SLionel Sambuc
664433d6423SLionel Sambuc for (i = 1; i < _NSIG; i++) {
665433d6423SLionel Sambuc if (sigismember(&rmp->mp_sigpending, i) &&
666433d6423SLionel Sambuc !sigismember(&rmp->mp_sigmask, i)) {
667433d6423SLionel Sambuc ksig = sigismember(&rmp->mp_ksigpending, i);
668433d6423SLionel Sambuc sigdelset(&rmp->mp_sigpending, i);
669433d6423SLionel Sambuc sigdelset(&rmp->mp_ksigpending, i);
670433d6423SLionel Sambuc sig_proc(rmp, i, FALSE /*trace*/, ksig);
671433d6423SLionel Sambuc
672910831cbSDavid van Moolenbroek if (rmp->mp_flags & (VFS_CALL | EVENT_CALL)) {
673433d6423SLionel Sambuc /* Signals must be rechecked upon return from the new
674433d6423SLionel Sambuc * VFS call, unless the process was killed. In both
675433d6423SLionel Sambuc * cases, the process is stopped.
676433d6423SLionel Sambuc */
677433d6423SLionel Sambuc assert(rmp->mp_flags & PROC_STOPPED);
678433d6423SLionel Sambuc break;
679433d6423SLionel Sambuc }
680433d6423SLionel Sambuc }
681433d6423SLionel Sambuc }
682433d6423SLionel Sambuc }
683433d6423SLionel Sambuc
684433d6423SLionel Sambuc /*===========================================================================*
685433d6423SLionel Sambuc * restart_sigs *
686433d6423SLionel Sambuc *===========================================================================*/
687*637f688fSRichard Sailer void
restart_sigs(struct mproc * rmp)688*637f688fSRichard Sailer restart_sigs(struct mproc *rmp)
689433d6423SLionel Sambuc {
690433d6423SLionel Sambuc /* VFS has replied to a request from us; do signal-related work.
691433d6423SLionel Sambuc */
692433d6423SLionel Sambuc
693910831cbSDavid van Moolenbroek if (rmp->mp_flags & (VFS_CALL | EVENT_CALL | EXITING)) return;
694433d6423SLionel Sambuc
695433d6423SLionel Sambuc if (rmp->mp_flags & TRACE_EXIT) {
696433d6423SLionel Sambuc /* Tracer requested exit with specific exit value */
697433d6423SLionel Sambuc exit_proc(rmp, rmp->mp_exitstatus, FALSE /*dump_core*/);
698433d6423SLionel Sambuc }
699433d6423SLionel Sambuc else if (rmp->mp_flags & PROC_STOPPED) {
700433d6423SLionel Sambuc /* If a signal arrives while we are performing a VFS call, the process
701433d6423SLionel Sambuc * will always be stopped immediately. Thus, if the process is stopped
702433d6423SLionel Sambuc * once the reply from VFS arrives, we might have to check signals.
703433d6423SLionel Sambuc */
704433d6423SLionel Sambuc assert(!(rmp->mp_flags & DELAY_CALL));
705433d6423SLionel Sambuc
706433d6423SLionel Sambuc /* We saved signal(s) for after finishing a VFS call. Deal with this.
707433d6423SLionel Sambuc * PROC_STOPPED remains set to indicate the process is still stopped.
708433d6423SLionel Sambuc */
709433d6423SLionel Sambuc check_pending(rmp);
710433d6423SLionel Sambuc
711433d6423SLionel Sambuc /* Resume the process now, unless there is a reason not to. */
712433d6423SLionel Sambuc try_resume_proc(rmp);
713433d6423SLionel Sambuc }
714433d6423SLionel Sambuc }
715433d6423SLionel Sambuc
716433d6423SLionel Sambuc /*===========================================================================*
717433d6423SLionel Sambuc * unpause *
718433d6423SLionel Sambuc *===========================================================================*/
719*637f688fSRichard Sailer static int
unpause(struct mproc * rmp)720*637f688fSRichard Sailer unpause(
721*637f688fSRichard Sailer struct mproc *rmp /* which process */
722*637f688fSRichard Sailer )
723433d6423SLionel Sambuc {
724433d6423SLionel Sambuc /* A signal is to be sent to a process. If that process is hanging on a
725433d6423SLionel Sambuc * system call, the system call must be terminated with EINTR. First check if
726433d6423SLionel Sambuc * the process is hanging on an PM call. If not, tell VFS, so it can check for
727433d6423SLionel Sambuc * interruptible calls such as READs and WRITEs from pipes, ttys and the like.
728433d6423SLionel Sambuc */
729433d6423SLionel Sambuc message m;
730433d6423SLionel Sambuc
731910831cbSDavid van Moolenbroek assert(!(rmp->mp_flags & (VFS_CALL | EVENT_CALL)));
732433d6423SLionel Sambuc
733433d6423SLionel Sambuc /* If the UNPAUSED flag is set, VFS replied to an earlier unpause request. */
734433d6423SLionel Sambuc if (rmp->mp_flags & UNPAUSED) {
735433d6423SLionel Sambuc assert((rmp->mp_flags & (DELAY_CALL | PROC_STOPPED)) == PROC_STOPPED);
736433d6423SLionel Sambuc
737433d6423SLionel Sambuc return TRUE;
738433d6423SLionel Sambuc }
739433d6423SLionel Sambuc
740433d6423SLionel Sambuc /* If the process is already stopping, don't do anything now. */
741433d6423SLionel Sambuc if (rmp->mp_flags & DELAY_CALL)
742433d6423SLionel Sambuc return FALSE;
743433d6423SLionel Sambuc
744433d6423SLionel Sambuc /* Check to see if process is hanging on a WAIT or SIGSUSPEND call. */
745433d6423SLionel Sambuc if (rmp->mp_flags & (WAITING | SIGSUSPENDED)) {
746433d6423SLionel Sambuc /* Stop the process from running. Do not interrupt the actual call yet.
747433d6423SLionel Sambuc * sig_send() will interrupt the call and resume the process afterward.
748433d6423SLionel Sambuc * No delay calls: we know for a fact that the process called us.
749433d6423SLionel Sambuc */
750433d6423SLionel Sambuc stop_proc(rmp, FALSE /*may_delay*/);
751433d6423SLionel Sambuc
752433d6423SLionel Sambuc return TRUE;
753433d6423SLionel Sambuc }
754433d6423SLionel Sambuc
755910831cbSDavid van Moolenbroek /* Not paused in PM. Let VFS, and after that any matching process event
756910831cbSDavid van Moolenbroek * subscribers, try to unpause the process. The process needs to be stopped
757910831cbSDavid van Moolenbroek * for this. If it is not already stopped, try to stop it now. If that does
758910831cbSDavid van Moolenbroek * not succeed immediately, postpone signal delivery.
759433d6423SLionel Sambuc */
760433d6423SLionel Sambuc if (!(rmp->mp_flags & PROC_STOPPED) && !stop_proc(rmp, TRUE /*may_delay*/))
761433d6423SLionel Sambuc return FALSE;
762433d6423SLionel Sambuc
763433d6423SLionel Sambuc memset(&m, 0, sizeof(m));
764433d6423SLionel Sambuc m.m_type = VFS_PM_UNPAUSE;
765433d6423SLionel Sambuc m.VFS_PM_ENDPT = rmp->mp_endpoint;
766433d6423SLionel Sambuc
767433d6423SLionel Sambuc tell_vfs(rmp, &m);
768433d6423SLionel Sambuc
769433d6423SLionel Sambuc return FALSE;
770433d6423SLionel Sambuc }
771433d6423SLionel Sambuc
772433d6423SLionel Sambuc /*===========================================================================*
773433d6423SLionel Sambuc * sig_send *
774433d6423SLionel Sambuc *===========================================================================*/
775*637f688fSRichard Sailer static int
sig_send(struct mproc * rmp,int signo)776*637f688fSRichard Sailer sig_send(
777*637f688fSRichard Sailer struct mproc *rmp, /* what process to spawn a signal handler in */
778*637f688fSRichard Sailer int signo /* signal to send to process (1 to _NSIG-1) */
779*637f688fSRichard Sailer )
780433d6423SLionel Sambuc {
781433d6423SLionel Sambuc /* The process is supposed to catch this signal. Spawn a signal handler.
782433d6423SLionel Sambuc * Return TRUE if this succeeded, FALSE otherwise.
783433d6423SLionel Sambuc */
784433d6423SLionel Sambuc struct sigmsg sigmsg;
785433d6423SLionel Sambuc int i, r, sigflags, slot;
786433d6423SLionel Sambuc
787433d6423SLionel Sambuc assert(rmp->mp_flags & PROC_STOPPED);
788433d6423SLionel Sambuc
789433d6423SLionel Sambuc sigflags = rmp->mp_sigact[signo].sa_flags;
790433d6423SLionel Sambuc slot = (int) (rmp - mproc);
791433d6423SLionel Sambuc
792433d6423SLionel Sambuc if (rmp->mp_flags & SIGSUSPENDED)
793433d6423SLionel Sambuc sigmsg.sm_mask = rmp->mp_sigmask2;
794433d6423SLionel Sambuc else
795433d6423SLionel Sambuc sigmsg.sm_mask = rmp->mp_sigmask;
796433d6423SLionel Sambuc sigmsg.sm_signo = signo;
797433d6423SLionel Sambuc sigmsg.sm_sighandler =
798433d6423SLionel Sambuc (vir_bytes) rmp->mp_sigact[signo].sa_handler;
799433d6423SLionel Sambuc sigmsg.sm_sigreturn = rmp->mp_sigreturn;
800433d6423SLionel Sambuc for (i = 1; i < _NSIG; i++) {
801433d6423SLionel Sambuc if (sigismember(&rmp->mp_sigact[signo].sa_mask, i))
802433d6423SLionel Sambuc sigaddset(&rmp->mp_sigmask, i);
803433d6423SLionel Sambuc }
804433d6423SLionel Sambuc
805433d6423SLionel Sambuc if (sigflags & SA_NODEFER)
806433d6423SLionel Sambuc sigdelset(&rmp->mp_sigmask, signo);
807433d6423SLionel Sambuc else
808433d6423SLionel Sambuc sigaddset(&rmp->mp_sigmask, signo);
809433d6423SLionel Sambuc
810433d6423SLionel Sambuc if (sigflags & SA_RESETHAND) {
811433d6423SLionel Sambuc sigdelset(&rmp->mp_catch, signo);
812433d6423SLionel Sambuc rmp->mp_sigact[signo].sa_handler = SIG_DFL;
813433d6423SLionel Sambuc }
814433d6423SLionel Sambuc sigdelset(&rmp->mp_sigpending, signo);
815433d6423SLionel Sambuc sigdelset(&rmp->mp_ksigpending, signo);
816433d6423SLionel Sambuc
817433d6423SLionel Sambuc /* Ask the kernel to deliver the signal */
818433d6423SLionel Sambuc r = sys_sigsend(rmp->mp_endpoint, &sigmsg);
819433d6423SLionel Sambuc /* sys_sigsend can fail legitimately with EFAULT or ENOMEM if the process
820433d6423SLionel Sambuc * memory can't accommodate the signal handler. The target process will be
821433d6423SLionel Sambuc * killed in that case, so do not bother interrupting or resuming it.
822433d6423SLionel Sambuc */
823433d6423SLionel Sambuc if(r == EFAULT || r == ENOMEM) {
824433d6423SLionel Sambuc return(FALSE);
825433d6423SLionel Sambuc }
826433d6423SLionel Sambuc /* Other errors are unexpected pm/kernel discrepancies. */
827433d6423SLionel Sambuc if (r != OK) {
828433d6423SLionel Sambuc panic("sys_sigsend failed: %d", r);
829433d6423SLionel Sambuc }
830433d6423SLionel Sambuc
831433d6423SLionel Sambuc /* Was the process suspended in PM? Then interrupt the blocking call. */
832433d6423SLionel Sambuc if (rmp->mp_flags & (WAITING | SIGSUSPENDED)) {
833433d6423SLionel Sambuc rmp->mp_flags &= ~(WAITING | SIGSUSPENDED);
834433d6423SLionel Sambuc
835433d6423SLionel Sambuc reply(slot, EINTR);
836433d6423SLionel Sambuc
837433d6423SLionel Sambuc /* The process must just have been stopped by unpause(), which means
838433d6423SLionel Sambuc * that the UNPAUSE flag is not set.
839433d6423SLionel Sambuc */
840433d6423SLionel Sambuc assert(!(rmp->mp_flags & UNPAUSED));
841433d6423SLionel Sambuc
842433d6423SLionel Sambuc try_resume_proc(rmp);
843433d6423SLionel Sambuc
844433d6423SLionel Sambuc assert(!(rmp->mp_flags & PROC_STOPPED));
845433d6423SLionel Sambuc } else {
846433d6423SLionel Sambuc /* If the process was not suspended in PM, VFS must first have
847433d6423SLionel Sambuc * confirmed that it has tried to unsuspend any blocking call. Thus, we
848433d6423SLionel Sambuc * got here from restart_sigs() as part of handling PM_UNPAUSE_REPLY,
849433d6423SLionel Sambuc * and restart_sigs() will resume the process later.
850433d6423SLionel Sambuc */
851433d6423SLionel Sambuc assert(rmp->mp_flags & UNPAUSED);
852433d6423SLionel Sambuc }
853433d6423SLionel Sambuc
854433d6423SLionel Sambuc return(TRUE);
855433d6423SLionel Sambuc }
856