source: trunk/minix/servers/pm/signal.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 23.3 KB
RevLine 
[9]1/* This file handles signals, which are asynchronous events and are generally
2 * a messy and unpleasant business. Signals can be generated by the KILL
3 * system call, or from the keyboard (SIGINT) or from the clock (SIGALRM).
4 * In all cases control eventually passes to check_sig() to see which processes
5 * can be signaled. The actual signaling is done by sig_proc().
6 *
7 * The entry points into this file are:
8 * do_sigaction: perform the SIGACTION system call
9 * do_sigpending: perform the SIGPENDING system call
10 * do_sigprocmask: perform the SIGPROCMASK system call
11 * do_sigreturn: perform the SIGRETURN system call
12 * do_sigsuspend: perform the SIGSUSPEND system call
13 * do_kill: perform the KILL system call
14 * do_alarm: perform the ALARM system call by calling set_alarm()
15 * set_alarm: tell the clock task to start or stop a timer
16 * do_pause: perform the PAUSE system call
17 * ksig_pending: the kernel notified about pending signals
18 * sig_proc: interrupt or terminate a signaled process
19 * check_sig: check which processes to signal with sig_proc()
20 * check_pending: check if a pending signal can now be delivered
21 */
22
23#include "pm.h"
24#include <sys/stat.h>
25#include <sys/ptrace.h>
26#include <minix/callnr.h>
27#include <minix/endpoint.h>
28#include <minix/com.h>
29#include <signal.h>
30#include <sys/sigcontext.h>
31#include <string.h>
32#include "mproc.h"
33#include "param.h"
34
35#define CORE_MODE 0777 /* mode to use on core image files */
36#define DUMPED 0200 /* bit set in status when core dumped */
37
38FORWARD _PROTOTYPE( void dump_core, (struct mproc *rmp) );
39FORWARD _PROTOTYPE( void unpause, (int pro) );
40FORWARD _PROTOTYPE( void handle_ksig, (int proc_nr, sigset_t sig_map) );
41FORWARD _PROTOTYPE( void cause_sigalrm, (struct timer *tp) );
42
43/*===========================================================================*
44 * do_sigaction *
45 *===========================================================================*/
46PUBLIC int do_sigaction()
47{
48 int r;
49 struct sigaction svec;
50 struct sigaction *svp;
51
52 if (m_in.sig_nr == SIGKILL) return(OK);
53 if (m_in.sig_nr < 1 || m_in.sig_nr > _NSIG) return (EINVAL);
54 svp = &mp->mp_sigact[m_in.sig_nr];
55 if ((struct sigaction *) m_in.sig_osa != (struct sigaction *) NULL) {
56 r = sys_datacopy(PM_PROC_NR,(vir_bytes) svp,
57 who_e, (vir_bytes) m_in.sig_osa, (phys_bytes) sizeof(svec));
58 if (r != OK) return(r);
59 }
60
61 if ((struct sigaction *) m_in.sig_nsa == (struct sigaction *) NULL)
62 return(OK);
63
64 /* Read in the sigaction structure. */
65 r = sys_datacopy(who_e, (vir_bytes) m_in.sig_nsa,
66 PM_PROC_NR, (vir_bytes) &svec, (phys_bytes) sizeof(svec));
67 if (r != OK) return(r);
68
69 if (svec.sa_handler == SIG_IGN) {
70 sigaddset(&mp->mp_ignore, m_in.sig_nr);
71 sigdelset(&mp->mp_sigpending, m_in.sig_nr);
72 sigdelset(&mp->mp_catch, m_in.sig_nr);
73 sigdelset(&mp->mp_sig2mess, m_in.sig_nr);
74 } else if (svec.sa_handler == SIG_DFL) {
75 sigdelset(&mp->mp_ignore, m_in.sig_nr);
76 sigdelset(&mp->mp_catch, m_in.sig_nr);
77 sigdelset(&mp->mp_sig2mess, m_in.sig_nr);
78 } else if (svec.sa_handler == SIG_MESS) {
79 if (! (mp->mp_flags & PRIV_PROC)) return(EPERM);
80 sigdelset(&mp->mp_ignore, m_in.sig_nr);
81 sigaddset(&mp->mp_sig2mess, m_in.sig_nr);
82 sigdelset(&mp->mp_catch, m_in.sig_nr);
83 } else {
84 sigdelset(&mp->mp_ignore, m_in.sig_nr);
85 sigaddset(&mp->mp_catch, m_in.sig_nr);
86 sigdelset(&mp->mp_sig2mess, m_in.sig_nr);
87 }
88 mp->mp_sigact[m_in.sig_nr].sa_handler = svec.sa_handler;
89 sigdelset(&svec.sa_mask, SIGKILL);
90 mp->mp_sigact[m_in.sig_nr].sa_mask = svec.sa_mask;
91 mp->mp_sigact[m_in.sig_nr].sa_flags = svec.sa_flags;
92 mp->mp_sigreturn = (vir_bytes) m_in.sig_ret;
93 return(OK);
94}
95
96/*===========================================================================*
97 * do_sigpending *
98 *===========================================================================*/
99PUBLIC int do_sigpending()
100{
101 mp->mp_reply.reply_mask = (long) mp->mp_sigpending;
102 return OK;
103}
104
105/*===========================================================================*
106 * do_sigprocmask *
107 *===========================================================================*/
108PUBLIC int do_sigprocmask()
109{
110/* Note that the library interface passes the actual mask in sigmask_set,
111 * not a pointer to the mask, in order to save a copy. Similarly,
112 * the old mask is placed in the return message which the library
113 * interface copies (if requested) to the user specified address.
114 *
115 * The library interface must set SIG_INQUIRE if the 'act' argument
116 * is NULL.
117 *
118 * KILL and STOP can't be masked.
119 */
120
121 int i;
122
123 mp->mp_reply.reply_mask = (long) mp->mp_sigmask;
124
125 switch (m_in.sig_how) {
126 case SIG_BLOCK:
127 sigdelset((sigset_t *)&m_in.sig_set, SIGKILL);
128 sigdelset((sigset_t *)&m_in.sig_set, SIGSTOP);
129 for (i = 1; i <= _NSIG; i++) {
130 if (sigismember((sigset_t *)&m_in.sig_set, i))
131 sigaddset(&mp->mp_sigmask, i);
132 }
133 break;
134
135 case SIG_UNBLOCK:
136 for (i = 1; i <= _NSIG; i++) {
137 if (sigismember((sigset_t *)&m_in.sig_set, i))
138 sigdelset(&mp->mp_sigmask, i);
139 }
140 check_pending(mp);
141 break;
142
143 case SIG_SETMASK:
144 sigdelset((sigset_t *) &m_in.sig_set, SIGKILL);
145 sigdelset((sigset_t *) &m_in.sig_set, SIGSTOP);
146 mp->mp_sigmask = (sigset_t) m_in.sig_set;
147 check_pending(mp);
148 break;
149
150 case SIG_INQUIRE:
151 break;
152
153 default:
154 return(EINVAL);
155 break;
156 }
157 return OK;
158}
159
160/*===========================================================================*
161 * do_sigsuspend *
162 *===========================================================================*/
163PUBLIC int do_sigsuspend()
164{
165 mp->mp_sigmask2 = mp->mp_sigmask; /* save the old mask */
166 mp->mp_sigmask = (sigset_t) m_in.sig_set;
167 sigdelset(&mp->mp_sigmask, SIGKILL);
168 mp->mp_flags |= SIGSUSPENDED;
169 check_pending(mp);
170 return(SUSPEND);
171}
172
173/*===========================================================================*
174 * do_sigreturn *
175 *===========================================================================*/
176PUBLIC int do_sigreturn()
177{
178/* A user signal handler is done. Restore context and check for
179 * pending unblocked signals.
180 */
181
182 int r;
183
184 mp->mp_sigmask = (sigset_t) m_in.sig_set;
185 sigdelset(&mp->mp_sigmask, SIGKILL);
186
187 r = sys_sigreturn(who_e, (struct sigmsg *) m_in.sig_context);
188 check_pending(mp);
189 return(r);
190}
191
192/*===========================================================================*
193 * do_kill *
194 *===========================================================================*/
195PUBLIC int do_kill()
196{
197/* Perform the kill(pid, signo) system call. */
198
199 return check_sig(m_in.pid, m_in.sig_nr);
200}
201
202/*===========================================================================*
203 * ksig_pending *
204 *===========================================================================*/
205PUBLIC int ksig_pending()
206{
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).
212 *
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.
216 */
217 int proc_nr_e;
218 sigset_t sig_map;
219
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(__FILE__,"sys_getksig failed", 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(__FILE__,"sys_getksig strange process", 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 ...
234 */
235 if ((mproc[proc_nr_p].mp_flags & (IN_USE | ZOMBIE)) == IN_USE)
236 if((r=sys_endksig(proc_nr_e)) != OK) /* ... tell kernel it's done */
237 panic(__FILE__,"sys_endksig failed", r);
238 }
239 }
240 return(SUSPEND); /* prevents sending reply */
241}
242
243/*===========================================================================*
244 * handle_ksig *
245 *===========================================================================*/
246PRIVATE void handle_ksig(proc_nr_e, sig_map)
247int proc_nr_e;
248sigset_t sig_map;
249{
250 register struct mproc *rmp;
251 int i, proc_nr;
252 pid_t proc_id, id;
253
254 if(pm_isokendpt(proc_nr_e, &proc_nr) != OK || proc_nr < 0)
255 return;
256 rmp = &mproc[proc_nr];
257 if ((rmp->mp_flags & (IN_USE | ZOMBIE)) != IN_USE)
258 return;
259 proc_id = rmp->mp_pid;
260 mp = &mproc[0]; /* pretend signals are from PM */
261 mp->mp_procgrp = rmp->mp_procgrp; /* get process group right */
262
263 /* Check each bit in turn to see if a signal is to be sent. Unlike
264 * kill(), the kernel may collect several unrelated signals for a
265 * process and pass them to PM in one blow. Thus loop on the bit
266 * map. For SIGINT, SIGWINCH and SIGQUIT, use proc_id 0 to indicate
267 * a broadcast to the recipient's process group. For SIGKILL, use
268 * proc_id -1 to indicate a systemwide broadcast.
269 */
270 for (i = 1; i <= _NSIG; i++) {
271 if (!sigismember(&sig_map, i)) continue;
272 switch (i) {
273 case SIGINT:
274 case SIGQUIT:
275 case SIGWINCH:
276 id = 0; break; /* broadcast to process group */
277#if 0
278 case SIGKILL:
279 id = -1; break; /* broadcast to all except INIT */
280#endif
281 default:
282 id = proc_id;
283 break;
284 }
285 check_sig(id, i);
286 }
287}
288
289/*===========================================================================*
290 * do_alarm *
291 *===========================================================================*/
292PUBLIC int do_alarm()
293{
294/* Perform the alarm(seconds) system call. */
295 return(set_alarm(who_e, m_in.seconds));
296}
297
298/*===========================================================================*
299 * set_alarm *
300 *===========================================================================*/
301PUBLIC int set_alarm(proc_nr_e, sec)
302int proc_nr_e; /* process that wants the alarm */
303int sec; /* how many seconds delay before the signal */
304{
305/* This routine is used by do_alarm() to set the alarm timer. It is also used
306 * to turn the timer off when a process exits with the timer still on.
307 */
308 clock_t ticks; /* number of ticks for alarm */
309 clock_t exptime; /* needed for remaining time on previous alarm */
310 clock_t uptime; /* current system time */
311 int remaining; /* previous time left in seconds */
312 int s;
313 int proc_nr_n;
314
315 if(pm_isokendpt(proc_nr_e, &proc_nr_n) != OK)
316 return EINVAL;
317
318 /* First determine remaining time of previous alarm, if set. */
319 if (mproc[proc_nr_n].mp_flags & ALARM_ON) {
320 if ( (s=getuptime(&uptime)) != OK)
321 panic(__FILE__,"set_alarm couldn't get uptime", s);
322 exptime = *tmr_exp_time(&mproc[proc_nr_n].mp_timer);
323 remaining = (int) ((exptime - uptime + (HZ-1))/HZ);
324 if (remaining < 0) remaining = 0;
325 } else {
326 remaining = 0;
327 }
328
329 /* Tell the clock task to provide a signal message when the time comes.
330 *
331 * Large delays cause a lot of problems. First, the alarm system call
332 * takes an unsigned seconds count and the library has cast it to an int.
333 * That probably works, but on return the library will convert "negative"
334 * unsigneds to errors. Presumably no one checks for these errors, so
335 * force this call through. Second, If unsigned and long have the same
336 * size, converting from seconds to ticks can easily overflow. Finally,
337 * the kernel has similar overflow bugs adding ticks.
338 *
339 * Fixing this requires a lot of ugly casts to fit the wrong interface
340 * types and to avoid overflow traps. ALRM_EXP_TIME has the right type
341 * (clock_t) although it is declared as long. How can variables like
342 * this be declared properly without combinatorial explosion of message
343 * types?
344 */
345 ticks = (clock_t) (HZ * (unsigned long) (unsigned) sec);
346 if ( (unsigned long) ticks / HZ != (unsigned) sec)
347 ticks = LONG_MAX; /* eternity (really TMR_NEVER) */
348
349 if (ticks != 0) {
350 pm_set_timer(&mproc[proc_nr_n].mp_timer, ticks,
351 cause_sigalrm, proc_nr_e);
352 mproc[proc_nr_n].mp_flags |= ALARM_ON;
353 } else if (mproc[proc_nr_n].mp_flags & ALARM_ON) {
354 pm_cancel_timer(&mproc[proc_nr_n].mp_timer);
355 mproc[proc_nr_n].mp_flags &= ~ALARM_ON;
356 }
357 return(remaining);
358}
359
360/*===========================================================================*
361 * cause_sigalrm *
362 *===========================================================================*/
363PRIVATE void cause_sigalrm(tp)
364struct timer *tp;
365{
366 int proc_nr_e, proc_nr_n;
367 register struct mproc *rmp;
368
369 /* get process from timer */
370 if(pm_isokendpt(tmr_arg(tp)->ta_int, &proc_nr_n) != OK) {
371 printf("PM: ignoring timer for invalid endpoint %d\n",
372 tmr_arg(tp)->ta_int);
373 return;
374 }
375
376 rmp = &mproc[proc_nr_n];
377
378 if ((rmp->mp_flags & (IN_USE | ZOMBIE)) != IN_USE) return;
379 if ((rmp->mp_flags & ALARM_ON) == 0) return;
380 rmp->mp_flags &= ~ALARM_ON;
381 check_sig(rmp->mp_pid, SIGALRM);
382}
383
384/*===========================================================================*
385 * do_pause *
386 *===========================================================================*/
387PUBLIC int do_pause()
388{
389/* Perform the pause() system call. */
390
391 mp->mp_flags |= PAUSED;
392 return(SUSPEND);
393}
394
395/*===========================================================================*
396 * sig_proc *
397 *===========================================================================*/
398PUBLIC void sig_proc(rmp, signo)
399register struct mproc *rmp; /* pointer to the process to be signaled */
400int signo; /* signal to send to process (1 to _NSIG) */
401{
402/* Send a signal to a process. Check to see if the signal is to be caught,
403 * ignored, tranformed into a message (for system processes) or blocked.
404 * - If the signal is to be transformed into a message, request the KERNEL to
405 * send the target process a system notification with the pending signal as an
406 * argument.
407 * - If the signal is to be caught, request the KERNEL to push a sigcontext
408 * structure and a sigframe structure onto the catcher's stack. Also, KERNEL
409 * will reset the program counter and stack pointer, so that when the process
410 * next runs, it will be executing the signal handler. When the signal handler
411 * returns, sigreturn(2) will be called. Then KERNEL will restore the signal
412 * context from the sigcontext structure.
413 * If there is insufficient stack space, kill the process.
414 */
415
416 vir_bytes new_sp;
417 int s;
418 int slot;
419 int sigflags;
420 struct sigmsg sm;
421
422 slot = (int) (rmp - mproc);
423 if ((rmp->mp_flags & (IN_USE | ZOMBIE)) != IN_USE) {
424 printf("PM: signal %d sent to %s process %d\n",
425 signo, (rmp->mp_flags & ZOMBIE) ? "zombie" : "dead", slot);
426 panic(__FILE__,"", NO_NUM);
427 }
428 if ((rmp->mp_flags & TRACED) && signo != SIGKILL) {
429 /* A traced process has special handling. */
430 unpause(slot);
431 stop_proc(rmp, signo); /* a signal causes it to stop */
432 return;
433 }
434 /* Some signals are ignored by default. */
435 if (sigismember(&rmp->mp_ignore, signo)) {
436 return;
437 }
438 if (sigismember(&rmp->mp_sigmask, signo)) {
439 /* Signal should be blocked. */
440 sigaddset(&rmp->mp_sigpending, signo);
441 return;
442 }
443#if ENABLE_SWAP
444 if (rmp->mp_flags & ONSWAP) {
445 /* Process is swapped out, leave signal pending. */
446 sigaddset(&rmp->mp_sigpending, signo);
447 swap_inqueue(rmp);
448 return;
449 }
450#endif
451 sigflags = rmp->mp_sigact[signo].sa_flags;
452 if (sigismember(&rmp->mp_catch, signo)) {
453 if (rmp->mp_flags & SIGSUSPENDED)
454 sm.sm_mask = rmp->mp_sigmask2;
455 else
456 sm.sm_mask = rmp->mp_sigmask;
457 sm.sm_signo = signo;
458 sm.sm_sighandler = (vir_bytes) rmp->mp_sigact[signo].sa_handler;
459 sm.sm_sigreturn = rmp->mp_sigreturn;
460 if ((s=get_stack_ptr(rmp->mp_endpoint, &new_sp)) != OK)
461 panic(__FILE__,"couldn't get new stack pointer (for sig)",s);
462 sm.sm_stkptr = new_sp;
463
464 /* Make room for the sigcontext and sigframe struct. */
465 new_sp -= sizeof(struct sigcontext)
466 + 3 * sizeof(char *) + 2 * sizeof(int);
467
468 if (adjust(rmp, rmp->mp_seg[D].mem_len, new_sp) != OK)
469 goto doterminate;
470
471 rmp->mp_sigmask |= rmp->mp_sigact[signo].sa_mask;
472 if (sigflags & SA_NODEFER)
473 sigdelset(&rmp->mp_sigmask, signo);
474 else
475 sigaddset(&rmp->mp_sigmask, signo);
476
477 if (sigflags & SA_RESETHAND) {
478 sigdelset(&rmp->mp_catch, signo);
479 rmp->mp_sigact[signo].sa_handler = SIG_DFL;
480 }
481
482 if (OK == (s=sys_sigsend(rmp->mp_endpoint, &sm))) {
483
484 sigdelset(&rmp->mp_sigpending, signo);
485 /* If process is hanging on PAUSE, WAIT, SIGSUSPEND, tty,
486 * pipe, etc., release it.
487 */
488 unpause(slot);
489 return;
490 }
491 panic(__FILE__, "sys_sigsend failed", s);
492 }
493 else if (sigismember(&rmp->mp_sig2mess, signo)) {
494
495 /* Mark event pending in process slot and send notification. */
496 sigaddset(&rmp->mp_sigpending, signo);
497 notify(rmp->mp_endpoint);
498 return;
499 }
500
501doterminate:
502 /* Signal should not or cannot be caught. Take default action. */
503 if (sigismember(&ign_sset, signo)) return;
504
505 rmp->mp_sigstatus = (char) signo;
506 if (sigismember(&core_sset, signo) && slot != FS_PROC_NR) {
507#if ENABLE_SWAP
508 if (rmp->mp_flags & ONSWAP) {
509 /* Process is swapped out, leave signal pending. */
510 sigaddset(&rmp->mp_sigpending, signo);
511 swap_inqueue(rmp);
512 return;
513 }
514#endif
515 /* Switch to the user's FS environment and dump core. */
516 tell_fs(CHDIR, rmp->mp_endpoint, FALSE, 0);
517 dump_core(rmp);
518 }
519 pm_exit(rmp, 0); /* terminate process */
520}
521
522/*===========================================================================*
523 * check_sig *
524 *===========================================================================*/
525PUBLIC int check_sig(proc_id, signo)
526pid_t proc_id; /* pid of proc to sig, or 0 or -1, or -pgrp */
527int signo; /* signal to send to process (0 to _NSIG) */
528{
529/* Check to see if it is possible to send a signal. The signal may have to be
530 * sent to a group of processes. This routine is invoked by the KILL system
531 * call, and also when the kernel catches a DEL or other signal.
532 */
533
534 register struct mproc *rmp;
535 int count; /* count # of signals sent */
536 int error_code;
537
538 if (signo < 0 || signo > _NSIG) return(EINVAL);
539
540 /* Return EINVAL for attempts to send SIGKILL to INIT alone. */
541 if (proc_id == INIT_PID && signo == SIGKILL) return(EINVAL);
542
543 /* Search the proc table for processes to signal.
544 * (See forkexit.c aboutpid magic.)
545 */
546 count = 0;
547 error_code = ESRCH;
548 for (rmp = &mproc[0]; rmp < &mproc[NR_PROCS]; rmp++) {
549 if (!(rmp->mp_flags & IN_USE)) continue;
550 if ((rmp->mp_flags & ZOMBIE) && signo != 0) continue;
551
552 /* Check for selection. */
553 if (proc_id > 0 && proc_id != rmp->mp_pid) continue;
554 if (proc_id == 0 && mp->mp_procgrp != rmp->mp_procgrp) continue;
555 if (proc_id == -1 && rmp->mp_pid <= INIT_PID) continue;
556 if (proc_id < -1 && rmp->mp_procgrp != -proc_id) continue;
557
558 /* Do not kill servers and drivers when broadcasting SIGKILL. */
559 if (proc_id == -1 && signo == SIGKILL &&
560 (rmp->mp_flags & PRIV_PROC)) continue;
561
562 /* Check for permission. */
563 if (mp->mp_effuid != SUPER_USER
564 && mp->mp_realuid != rmp->mp_realuid
565 && mp->mp_effuid != rmp->mp_realuid
566 && mp->mp_realuid != rmp->mp_effuid
567 && mp->mp_effuid != rmp->mp_effuid) {
568 error_code = EPERM;
569 continue;
570 }
571
572 count++;
573 if (signo == 0) continue;
574
575 /* 'sig_proc' will handle the disposition of the signal. The
576 * signal may be caught, blocked, ignored, or cause process
577 * termination, possibly with core dump.
578 */
579 sig_proc(rmp, signo);
580
581 if (proc_id > 0) break; /* only one process being signaled */
582 }
583
584 /* If the calling process has killed itself, don't reply. */
585 if ((mp->mp_flags & (IN_USE | ZOMBIE)) != IN_USE) return(SUSPEND);
586 return(count > 0 ? OK : error_code);
587}
588
589/*===========================================================================*
590 * check_pending *
591 *===========================================================================*/
592PUBLIC void check_pending(rmp)
593register struct mproc *rmp;
594{
595 /* Check to see if any pending signals have been unblocked. The
596 * first such signal found is delivered.
597 *
598 * If multiple pending unmasked signals are found, they will be
599 * delivered sequentially.
600 *
601 * There are several places in this file where the signal mask is
602 * changed. At each such place, check_pending() should be called to
603 * check for newly unblocked signals.
604 */
605
606 int i;
607
608 for (i = 1; i <= _NSIG; i++) {
609 if (sigismember(&rmp->mp_sigpending, i) &&
610 !sigismember(&rmp->mp_sigmask, i)) {
611 sigdelset(&rmp->mp_sigpending, i);
612 sig_proc(rmp, i);
613 break;
614 }
615 }
616}
617
618/*===========================================================================*
619 * unpause *
620 *===========================================================================*/
621PRIVATE void unpause(pro)
622int pro; /* which process number */
623{
624/* A signal is to be sent to a process. If that process is hanging on a
625 * system call, the system call must be terminated with EINTR. Possible
626 * calls are PAUSE, WAIT, READ and WRITE, the latter two for pipes and ttys.
627 * First check if the process is hanging on an PM call. If not, tell FS,
628 * so it can check for READs and WRITEs from pipes, ttys and the like.
629 */
630
631 register struct mproc *rmp;
632
633 rmp = &mproc[pro];
634
635 /* Check to see if process is hanging on a PAUSE, WAIT or SIGSUSPEND call. */
636 if (rmp->mp_flags & (PAUSED | WAITING | SIGSUSPENDED)) {
637 rmp->mp_flags &= ~(PAUSED | WAITING | SIGSUSPENDED);
638 setreply(pro, EINTR);
639 return;
640 }
641
642 /* Process is not hanging on an PM call. Ask FS to take a look. */
643 tell_fs(UNPAUSE, rmp->mp_endpoint, 0, 0);
644}
645
646/*===========================================================================*
647 * dump_core *
648 *===========================================================================*/
649PRIVATE void dump_core(rmp)
650register struct mproc *rmp; /* whose core is to be dumped */
651{
652/* Make a core dump on the file "core", if possible. */
653
654 int s, fd, seg, slot;
655 vir_bytes current_sp;
656 long trace_data, trace_off;
657
658 slot = (int) (rmp - mproc);
659
660 /* Can core file be written? We are operating in the user's FS environment,
661 * so no special permission checks are needed.
662 */
663 if (rmp->mp_realuid != rmp->mp_effuid) return;
664 if ( (fd = open(core_name, O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK,
665 CORE_MODE)) < 0) return;
666 rmp->mp_sigstatus |= DUMPED;
667
668 /* Make sure the stack segment is up to date.
669 * We don't want adjust() to fail unless current_sp is preposterous,
670 * but it might fail due to safety checking. Also, we don't really want
671 * the adjust() for sending a signal to fail due to safety checking.
672 * Maybe make SAFETY_BYTES a parameter.
673 */
674 if ((s=get_stack_ptr(rmp->mp_endpoint, &current_sp)) != OK)
675 panic(__FILE__,"couldn't get new stack pointer (for core)",s);
676 adjust(rmp, rmp->mp_seg[D].mem_len, current_sp);
677
678 /* Write the memory map of all segments to begin the core file. */
679 if (write(fd, (char *) rmp->mp_seg, (unsigned) sizeof rmp->mp_seg)
680 != (unsigned) sizeof rmp->mp_seg) {
681 close(fd);
682 return;
683 }
684
685 /* Write out the whole kernel process table entry to get the regs. */
686 trace_off = 0;
687 while (sys_trace(T_GETUSER, rmp->mp_endpoint, trace_off, &trace_data) == OK) {
688 if (write(fd, (char *) &trace_data, (unsigned) sizeof (long))
689 != (unsigned) sizeof (long)) {
690 close(fd);
691 return;
692 }
693 trace_off += sizeof (long);
694 }
695
696 /* Loop through segments and write the segments themselves out. */
697 for (seg = 0; seg < NR_LOCAL_SEGS; seg++) {
698 rw_seg(1, fd, rmp->mp_endpoint, seg,
699 (phys_bytes) rmp->mp_seg[seg].mem_len << CLICK_SHIFT);
700 }
701 close(fd);
702}
703
Note: See TracBrowser for help on using the repository browser.