The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
signal.c File Reference

POSIX signals: sending, default actions, user handlers. More...

Macros

#define STOP_SIGNALS   (SIG_BIT(SIGSTOP) | SIG_BIT(SIGTSTP) | SIG_BIT(SIGTTIN) | SIG_BIT(SIGTTOU))
#define IGNORE_SIGNALS   (SIG_BIT(SIGCHLD) | SIG_BIT(SIGCONT) | SIG_BIT(SIGURG) | SIG_BIT(SIGWINCH))
#define USER_EFLAGS_MASK   0x0DD5u
#define EFLAGS_TF   0x0100u
#define EFLAGS_DF   0x0400u

Functions

void signal_send (process_t *proc, int sig)
 send a signal to one thread.
void signal_force (process_t *proc, int sig)
 send a signal caused by the process's own instruction (a fault).
bool signal_pending (const process_t *proc)
void signal_deliver (struct registers *regs)
 act on pending signals before returning to user mode: run the default action, or set up the user handler's frame.
void signal_fork (process_t *child, const process_t *parent)
signal_handlers_t * signal_handlers_create (void)
 make a set of handlers, all SIG_DFL, with one user.
signal_handlers_t * signal_handlers_get (signal_handlers_t *handlers)
 add a user to a set of handlers (a new thread that shares it).
void signal_handlers_put (signal_handlers_t *handlers)
 remove a user from a set of handlers; the last user frees it.
void signal_exec (process_t *proc)
 the signal state after execve: handlers go back to SIG_DFL (ignored signals stay ignored); the mask and pending signals stay.
void signal_alarm_tick (uint64_t now)
 send SIGALRM to processes whose alarm is due.
int signal_send_group (uint32_t pgid, int sig)
 send a signal to every process of a process group.
void signal_send_process (process_t *proc, int sig)
 send a signal to a process (a thread group): one of its threads takes it (signal_send), one that does not block it if there is one.
syscall_ret_t sys_kill (struct registers *regs)
 SYS_kill: send a signal.
syscall_ret_t sys_tgkill (struct registers *regs)
 SYS_tgkill: send a signal to one thread.
syscall_ret_t sys_sigaction (struct registers *regs)
 SYS_sigaction: read and change what a process does with a signal.
syscall_ret_t sys_sigprocmask (struct registers *regs)
 SYS_sigprocmask: read and change the blocked mask.
syscall_ret_t sys_sigpending (struct registers *regs)
 SYS_sigpending: the signals that are pending and blocked.
syscall_ret_t sys_sigsuspend (struct registers *regs)
 SYS_sigsuspend: wait for a signal with a temporary mask.
syscall_ret_t sys_sigreturn (struct registers *regs)
 SYS_sigreturn: return from a signal handler (called by the restorer).
syscall_ret_t sys_alarm (struct registers *regs)
 SYS_alarm: send SIGALRM after a number of seconds (0 cancels).
uint32_t signal_state_lock (void)
 take the lock of the signal state of all processes, with interrupts masked.
void signal_state_unlock (uint32_t flags)
 release the lock that signal_state_lock() took.
void signal_init (void)
 set up signal handling.

Detailed Description

POSIX signals: sending, default actions, user handlers.

a signal is a bit in the target's pending mask. it is acted on when the process next returns to user mode (signal_deliver, at the end of every interrupt and syscall from user mode): ignored, a default action (terminate, stop), or a jump to the user's handler on a frame built on the user stack. the handler returns through the restorer libc passes to sigaction, which calls sigreturn to restore the registers saved in the frame.

locking: signal_lock guards the signal state of every process (the pending and blocked masks, the handlers, the stop and continue state). signals are also sent from interrupt handlers (alarms, the keyboard), so it is taken with interrupts masked. it nests outside the process table lock and sched_lock. Linux has a lock for each process (sighand->siglock); one lock for all is the small form.

Macro Definition Documentation

◆ STOP_SIGNALS

#define STOP_SIGNALS   (SIG_BIT(SIGSTOP) | SIG_BIT(SIGTSTP) | SIG_BIT(SIGTTIN) | SIG_BIT(SIGTTOU))

◆ IGNORE_SIGNALS

#define IGNORE_SIGNALS   (SIG_BIT(SIGCHLD) | SIG_BIT(SIGCONT) | SIG_BIT(SIGURG) | SIG_BIT(SIGWINCH))

◆ USER_EFLAGS_MASK

#define USER_EFLAGS_MASK   0x0DD5u

◆ EFLAGS_TF

#define EFLAGS_TF   0x0100u

◆ EFLAGS_DF

#define EFLAGS_DF   0x0400u

Function Documentation

◆ signal_send()

void signal_send ( struct process * proc,
int sig )

send a signal to one thread.

a signal the process ignores is dropped; a deliverable one wakes the process from an interruptible sleep. SIGCONT continues a stopped process, SIGKILL wakes one. safe in any context.

Parameters
procthe process.
sigthe signal (1 to NSIG - 1).

◆ signal_force()

void signal_force ( struct process * proc,
int sig )

send a signal caused by the process's own instruction (a fault).

it is delivered even if blocked or ignored: blocked or ignored, it kills.

Parameters
procthe process.
sigthe signal.

◆ signal_pending()

bool signal_pending ( const process_t * proc)

◆ signal_deliver()

void signal_deliver ( struct registers * regs)

act on pending signals before returning to user mode: run the default action, or set up the user handler's frame.

also turns an interrupted syscall's -ERESTARTSYS into EINTR or a restart. called at the end of every interrupt and syscall that came from user mode.

Parameters
regsthe user registers about to be restored.

◆ signal_fork()

void signal_fork ( process_t * child,
const process_t * parent )

◆ signal_handlers_create()

signal_handlers_t * signal_handlers_create ( void )

make a set of handlers, all SIG_DFL, with one user.

Returns
the set, or NULL if there is no memory.

◆ signal_handlers_get()

signal_handlers_t * signal_handlers_get ( signal_handlers_t * handlers)

add a user to a set of handlers (a new thread that shares it).

Returns
handlers.

◆ signal_handlers_put()

void signal_handlers_put ( signal_handlers_t * handlers)

remove a user from a set of handlers; the last user frees it.

Parameters
handlersthe set, or NULL (nothing is done).

◆ signal_exec()

void signal_exec ( process_t * proc)

the signal state after execve: handlers go back to SIG_DFL (ignored signals stay ignored); the mask and pending signals stay.

◆ signal_alarm_tick()

void signal_alarm_tick ( uint64_t now)

send SIGALRM to processes whose alarm is due.

called from the timer interrupt.

Parameters
nowthe current tick.

◆ signal_send_group()

int signal_send_group ( uint32_t pgid,
int sig )

send a signal to every process of a process group.

Parameters
pgidthe group.
sigthe signal.
Returns
the number of processes it went to.

◆ signal_send_process()

void signal_send_process ( struct process * proc,
int sig )

send a signal to a process (a thread group): one of its threads takes it (signal_send), one that does not block it if there is one.

SIGCONT goes to every thread.

Parameters
procany thread of the process, or NULL (nothing is done).
sigthe signal.

◆ sys_kill()

syscall_ret_t sys_kill ( struct registers * regs)

SYS_kill: send a signal.

args: EBX = pid: a process (> 0), the caller's process group (0), every process but init (-1), or the process group -pid (< -1); ECX = signal (0 only checks that the target exists). returns 0, or -ESRCH, -EINVAL.

◆ sys_tgkill()

syscall_ret_t sys_tgkill ( struct registers * regs)

SYS_tgkill: send a signal to one thread.

args: the thread group (pid), the thread's id, the signal (0 only checks that the thread exists). SIGCONT continues the whole process, as for kill. returns 0, or -ESRCH, -EINVAL.

◆ sys_sigaction()

syscall_ret_t sys_sigaction ( struct registers * regs)

SYS_sigaction: read and change what a process does with a signal.

args: EBX = signal, ECX = new action (ksigaction_t, may be NULL), EDX = old action out (may be NULL). SIGKILL and SIGSTOP cannot be changed. returns 0 or a negative errno.

◆ sys_sigprocmask()

syscall_ret_t sys_sigprocmask ( struct registers * regs)

SYS_sigprocmask: read and change the blocked mask.

args: EBX = how (SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK), ECX = set (uint32_t *, may be NULL), EDX = old set out (may be NULL). returns 0 or a negative errno.

◆ sys_sigpending()

syscall_ret_t sys_sigpending ( struct registers * regs)

SYS_sigpending: the signals that are pending and blocked.

args: EBX = uint32_t * out.

◆ sys_sigsuspend()

syscall_ret_t sys_sigsuspend ( struct registers * regs)

SYS_sigsuspend: wait for a signal with a temporary mask.

args: EBX = uint32_t * mask. the mask is in place until a signal comes; after its handler (or its default action) the old mask is back. always returns -EINTR.

◆ sys_sigreturn()

syscall_ret_t sys_sigreturn ( struct registers * regs)

SYS_sigreturn: return from a signal handler (called by the restorer).

restores the registers and mask saved in the handler's frame; the result is the interrupted code's EAX.

◆ sys_alarm()

syscall_ret_t sys_alarm ( struct registers * regs)

SYS_alarm: send SIGALRM after a number of seconds (0 cancels).

returns the seconds that were left of the previous alarm (rounded up).

◆ signal_state_lock()

uint32_t signal_state_lock ( void )

take the lock of the signal state of all processes, with interrupts masked.

it also guards the exit and exec state of each thread group (group_exiting, group_status, exec_thread) and the waitpid reports, as the signal_struct lock does in Linux. it nests outside the process table lock and sched_lock.

Returns
the interrupt state for signal_state_unlock().

◆ signal_state_unlock()

void signal_state_unlock ( uint32_t flags)

release the lock that signal_state_lock() took.

Parameters
flagsthe value from signal_state_lock().

◆ signal_init()

void signal_init ( void )

set up signal handling.

call once at boot.