📄 signal.c
字号:
/* This file handles signals, which are asynchronous events and are generally
* a messy and unpleasant business. Signals can be generated by the KILL
* system call, or from the keyboard (SIGINT) or from the clock (SIGALRM).
* In all cases control eventually passes to check_sig() to see which processes
* can be signaled. The actual signaling is done by sig_proc().
*
* The entry points into this file are:
* do_sigaction: perform the SIGACTION system call
* do_sigpending: perform the SIGPENDING system call
* do_sigprocmask: perform the SIGPROCMASK system call
* do_sigreturn: perform the SIGRETURN system call
* do_sigsuspend: perform the SIGSUSPEND system call
* do_kill: perform the KILL system call
* do_ksig: accept a signal originating in the kernel (e.g., SIGINT)
* do_alarm: perform the ALARM system call by calling set_alarm()
* set_alarm: tell the clock task to start or stop a timer
* do_pause: perform the PAUSE system call
* do_reboot: kill all processes, then reboot system
* sig_proc: interrupt or terminate a signaled process
* check_sig: check which processes to signal with sig_proc()
*/
#include "mm.h"
#include <sys/stat.h>
#include <minix/callnr.h>
#include <minix/com.h>
#include <signal.h>
#include <sys/sigcontext.h>
#include <string.h>
#include "mproc.h"
#include "param.h"
#define CORE_MODE 0777 /* mode to use on core image files */
#define DUMPED 0200 /* bit set in status when core dumped */
#define DUMP_SIZE ((INT_MAX / BLOCK_SIZE) * BLOCK_SIZE)
/* buffer size for core dumps */
FORWARD _PROTOTYPE( void check_pending, (void) );
FORWARD _PROTOTYPE( void dump_core, (struct mproc *rmp) );
FORWARD _PROTOTYPE( void unpause, (int pro) );
/*===========================================================================*
* do_sigaction *
*===========================================================================*/
PUBLIC int do_sigaction()
{
int r;
struct sigaction svec;
struct sigaction *svp;
if (sig_nr == SIGKILL) return(OK);
if (sig_nr < 1 || sig_nr > _NSIG) return (EINVAL);
svp = &mp->mp_sigact[sig_nr];
if ((struct sigaction *) sig_osa != (struct sigaction *) NULL) {
r = sys_copy(MM_PROC_NR,D, (phys_bytes) svp,
who, D, (phys_bytes) sig_osa, (phys_bytes) sizeof(svec));
if (r != OK) return(r);
}
if ((struct sigaction *) sig_nsa == (struct sigaction *) NULL) return(OK);
/* Read in the sigaction structure. */
r = sys_copy(who, D, (phys_bytes) sig_nsa,
MM_PROC_NR, D, (phys_bytes) &svec, (phys_bytes) sizeof(svec));
if (r != OK) return(r);
if (svec.sa_handler == SIG_IGN) {
sigaddset(&mp->mp_ignore, sig_nr);
sigdelset(&mp->mp_sigpending, sig_nr);
sigdelset(&mp->mp_catch, sig_nr);
} else {
sigdelset(&mp->mp_ignore, sig_nr);
if (svec.sa_handler == SIG_DFL)
sigdelset(&mp->mp_catch, sig_nr);
else
sigaddset(&mp->mp_catch, sig_nr);
}
mp->mp_sigact[sig_nr].sa_handler = svec.sa_handler;
sigdelset(&svec.sa_mask, SIGKILL);
mp->mp_sigact[sig_nr].sa_mask = svec.sa_mask;
mp->mp_sigact[sig_nr].sa_flags = svec.sa_flags;
mp->mp_sigreturn = (vir_bytes) sig_ret;
return(OK);
}
/*===========================================================================*
* do_sigpending *
*===========================================================================*/
PUBLIC int do_sigpending()
{
ret_mask = (long) mp->mp_sigpending;
return OK;
}
/*===========================================================================*
* do_sigprocmask *
*===========================================================================*/
PUBLIC int do_sigprocmask()
{
/* Note that the library interface passes the actual mask in sigmask_set,
* not a pointer to the mask, in order to save a sys_copy. Similarly,
* the old mask is placed in the return message which the library
* interface copies (if requested) to the user specified address.
*
* The library interface must set SIG_INQUIRE if the 'act' argument
* is NULL.
*/
int i;
ret_mask = (long) mp->mp_sigmask;
switch (sig_how) {
case SIG_BLOCK:
sigdelset((sigset_t *)&sig_set, SIGKILL);
for (i = 1; i < _NSIG; i++) {
if (sigismember((sigset_t *)&sig_set, i))
sigaddset(&mp->mp_sigmask, i);
}
break;
case SIG_UNBLOCK:
for (i = 1; i < _NSIG; i++) {
if (sigismember((sigset_t *)&sig_set, i))
sigdelset(&mp->mp_sigmask, i);
}
check_pending();
break;
case SIG_SETMASK:
sigdelset((sigset_t *)&sig_set, SIGKILL);
mp->mp_sigmask = (sigset_t)sig_set;
check_pending();
break;
case SIG_INQUIRE:
break;
default:
return(EINVAL);
break;
}
return OK;
}
/*===========================================================================*
* do_sigsuspend *
*===========================================================================*/
PUBLIC int do_sigsuspend()
{
mp->mp_sigmask2 = mp->mp_sigmask; /* save the old mask */
mp->mp_sigmask = (sigset_t) sig_set;
sigdelset(&mp->mp_sigmask, SIGKILL);
mp->mp_flags |= SIGSUSPENDED;
dont_reply = TRUE;
check_pending();
return OK;
}
/*===========================================================================*
* do_sigreturn *
*===========================================================================*/
PUBLIC int do_sigreturn()
{
/* A user signal handler is done. Restore context and check for
* pending unblocked signals.
*/
int r;
mp->mp_sigmask = (sigset_t) sig_set;
sigdelset(&mp->mp_sigmask, SIGKILL);
r = sys_sigreturn(who, (vir_bytes)sig_context, sig_flags);
check_pending();
return(r);
}
/*===========================================================================*
* do_kill *
*===========================================================================*/
PUBLIC int do_kill()
{
/* Perform the kill(pid, signo) system call. */
return check_sig(pid, sig_nr);
}
/*===========================================================================*
* do_ksig *
*===========================================================================*/
PUBLIC int do_ksig()
{
/* Certain signals, such as segmentation violations and DEL, originate in the
* kernel. When the kernel detects such signals, it sets bits in a bit map.
* As soon as MM is awaiting new work, the kernel sends MM a message containing
* the process slot and bit map. That message comes here. The File System
* also uses this mechanism to signal writing on broken pipes (SIGPIPE).
*/
register struct mproc *rmp;
int i, proc_nr;
pid_t proc_id, id;
sigset_t sig_map;
/* Only kernel may make this call. */
if (who != HARDWARE) return(EPERM);
dont_reply = TRUE; /* don't reply to the kernel */
proc_nr = mm_in.SIG_PROC;
rmp = &mproc[proc_nr];
if ( (rmp->mp_flags & IN_USE) == 0 || (rmp->mp_flags & HANGING) ) return(OK);
proc_id = rmp->mp_pid;
sig_map = (sigset_t) mm_in.SIG_MAP;
mp = &mproc[0]; /* pretend kernel signals are from MM */
mp->mp_procgrp = rmp->mp_procgrp; /* get process group right */
/* Check each bit in turn to see if a signal is to be sent. Unlike
* kill(), the kernel may collect several unrelated signals for a
* process and pass them to MM in one blow. Thus loop on the bit
* map. For SIGINT and SIGQUIT, use proc_id 0 to indicate a broadcast
* to the recipient's process group. For SIGKILL, use proc_id -1 to
* indicate a systemwide broadcast.
*/
for (i = 1; i <= _NSIG; i++) {
if (!sigismember(&sig_map, i)) continue;
switch (i) {
case SIGINT:
case SIGQUIT:
id = 0; break; /* broadcast to process group */
case SIGKILL:
id = -1; break; /* broadcast to all except INIT */
case SIGALRM:
/* Disregard SIGALRM when the target process has not
* requested an alarm. This only applies for a KERNEL
* generated signal.
*/
if ((rmp->mp_flags & ALARM_ON) == 0) continue;
rmp->mp_flags &= ~ALARM_ON;
/* fall through */
default:
id = proc_id;
break;
}
check_sig(id, i);
sys_endsig(proc_nr); /* tell kernel it's done */
}
return(OK);
}
/*===========================================================================*
* do_alarm *
*===========================================================================*/
PUBLIC int do_alarm()
{
/* Perform the alarm(seconds) system call. */
return(set_alarm(who, seconds));
}
/*===========================================================================*
* set_alarm *
*===========================================================================*/
PUBLIC int set_alarm(proc_nr, sec)
int proc_nr; /* process that wants the alarm */
int sec; /* how many seconds delay before the signal */
{
/* This routine is used by do_alarm() to set the alarm timer. It is also used
* to turn the timer off when a process exits with the timer still on.
*/
message m_sig;
int remaining;
if (sec != 0)
mproc[proc_nr].mp_flags |= ALARM_ON;
else
mproc[proc_nr].mp_flags &= ~ALARM_ON;
/* Tell the clock task to provide a signal message when the time comes.
*
* Large delays cause a lot of problems. First, the alarm system call
* takes an unsigned seconds count and the library has cast it to an int.
* That probably works, but on return the library will convert "negative"
* unsigneds to errors. Presumably no one checks for these errors, so
* force this call through. Second, If unsigned and long have the same
* size, converting from seconds to ticks can easily overflow. Finally,
* the kernel has similar overflow bugs adding ticks.
*
* Fixing this requires a lot of ugly casts to fit the wrong interface
* types and to avoid overflow traps. DELTA_TICKS has the right type
* (clock_t) although it is declared as long. How can variables like
* this be declared properly without combinatorial explosion of message
* types?
*/
m_sig.m_type = SET_ALARM;
m_sig.CLOCK_PROC_NR = proc_nr;
m_sig.DELTA_TICKS = (clock_t) (HZ * (unsigned long) (unsigned) sec);
if ( (unsigned long) m_sig.DELTA_TICKS / HZ != (unsigned) sec)
m_sig.DELTA_TICKS = LONG_MAX; /* eternity (really CLOCK_T_MAX) */
if (sendrec(CLOCK, &m_sig) != OK) panic("alarm er", NO_NUM);
remaining = (int) m_sig.SECONDS_LEFT;
if (remaining != m_sig.SECONDS_LEFT || remaining < 0)
remaining = INT_MAX; /* true value is not representable */
return(remaining);
}
/*===========================================================================*
* do_pause *
*===========================================================================*/
PUBLIC int do_pause()
{
/* Perform the pause() system call. */
mp->mp_flags |= PAUSED;
dont_reply = TRUE;
return(OK);
}
/*=====================================================================*
* do_reboot *
*=====================================================================*/
PUBLIC int do_reboot()
{
register struct mproc *rmp = mp;
char monitor_code[64];
if (rmp->mp_effuid != SUPER_USER) return EPERM;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -