|
ginnOS
The reference: libc, the libraries and the kernel
|
POSIX signals: sending, default actions, user handlers. More...
#include "signal.h"#include "arch/arch.h"#include "arch/x86/cpu/isr.h"#include "common/memory.h"#include "common/stddef.h"#include "common/stdio.h"#include "kernel/errno.h"#include "kernel/klog/klog.h"#include "kernel/memory/heap.h"#include "kernel/process/process.h"#include "kernel/process/wait_status.h"#include "kernel/scheduler/scheduler.h"#include "kernel/sync/atomic.h"#include "kernel/syscall/syscall.h"#include "kernel/syscall/syscall_internal.h"#include "kernel/time/timer.h"#include "kernel/usermode/uaccess.h"#include "kernel/usermode/usermode.h"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. | |
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.
| #define USER_EFLAGS_MASK 0x0DD5u |
| #define EFLAGS_TF 0x0100u |
| #define EFLAGS_DF 0x0400u |
| 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.
| proc | the process. |
| sig | the signal (1 to NSIG - 1). |
| 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.
| proc | the process. |
| sig | the signal. |
| 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.
| regs | the user registers about to be restored. |
| 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.
| handlers | the set, or NULL (nothing is done). |
| 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.
called from the timer interrupt.
| now | the current tick. |
| int signal_send_group | ( | uint32_t | pgid, |
| int | sig ) |
send a signal to every process of a process group.
| pgid | the group. |
| sig | the signal. |
| 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.
| proc | any thread of the process, or NULL (nothing is done). |
| sig | the signal. |
| 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.
| 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.
| 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.
| 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.
| syscall_ret_t sys_sigpending | ( | struct registers * | regs | ) |
SYS_sigpending: the signals that are pending and blocked.
args: EBX = uint32_t * out.
| 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.
| 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.
| 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).
| 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.
| void signal_state_unlock | ( | uint32_t | flags | ) |
release the lock that signal_state_lock() took.
| flags | the value from signal_state_lock(). |
| void signal_init | ( | void | ) |
set up signal handling.
call once at boot.