SIGNAL(9) | Kernel Developer's Manual | SIGNAL(9)

# NAME

`signal`, `siginit`, `sigactsinit`, `sigactsunshare`, `sigactsfree`, `execsigs`, `sigaction1`, `sigprocmask1`, `sigpending1`, `sigsuspend1`, `sigaltstack1`, `pgsignal`, `kpgsignal`, `psignal`, `kpsignal`, `issignal`, `postsig`, `killproc`, `sigexit`, `trapsignal`, `sendsig`, `sigcode`, `sigtramp` — software signal facilities

# SYNOPSIS

`#include <sys/signal.h>`
`#include <sys/signalvar.h>`

void
`siginit`(struct proc *p);

void
`sigactsinit`(struct proc *pp, int share);

void
`sigactsunshare`(struct proc *p);

void
`sigactsfree`(struct proc *p);

void
`execsigs`(struct proc *p);

int
`sigaction1`(struct lwp *l, int signum, const struct sigaction *nsa, struct sigaction *osa, void *tramp, int vers);

int
`sigprocmask1`(struct lwp *l, int how, const sigset_t *nss, sigset_t *oss);

void
`sigpending1`(struct lwp *l, sigset_t *ss);

int
`sigsuspend1`(struct lwp *l, const sigset_t *ss);

int
`sigaltstack1`(struct lwp *l, const struct sigaltstack *nss, struct sigaltstack *oss);

void
`pgsignal`(struct pgrp *pgrp, int signum, int checkctty);

void
`kpgsignal`(struct pgrp *pgrp, ksiginfo_t *ks, void *data, int checkctty);

void
`psignal`(struct proc *p, int signum);

void
`kpsignal`(struct proc *p, ksiginfo_t *ks, void *data);

int
`issignal`(struct lwp *l);

void
`postsig`(int signum);

void
`killproc`(struct proc *p, const char *why);

void
`sigexit`(struct lwp *l, int signum);

void
`trapsignal`(struct lwp *l, const ksiginfo_t *ks);

void
`sendsig`(const ksiginfo_t *ks, const sigset_t *mask);

# DESCRIPTION

The system defines a set of signals that may be delivered to a process. These functions implement the kernel portion of the signal facility.

Signal numbers used throughout the kernel signal facilities should always be within the range of [1-NSIG].

Most of the kernel's signal infrastructure is implemented in machine-independent code. Machine-dependent code provides support for invoking a process's signal handler, restoring context when the signal handler returns, generating signals when hardware traps occur, triggering the delivery of signals when a process is about to return from the kernel to userspace.

The signal state for a process is contained in struct sigctx. This includes the list of signals with delivery pending, information about the signal handler stack, the signal mask, and the address of the signal trampoline.

The registered signal handlers for a process are recorded in struct sigacts. This structure may be shared by multiple processes.

The kernel's signal facilities are implemented by the following functions:

`siginit`(p)

  This function initializes the signal state of proc0 to the system default. This signal state is then inherited by init(8) when it is started by the kernel.

`sigactsinit`(pp, share)

  This function creates an initial struct sigacts for the process pp. If the share argument is non-zero, then pp shares the struct sigacts by holding a reference. Otherwise, pp receives a new struct sigacts which is copied from the parent.

`sigactsunshare`(p)

  This function causes the process p to no longer share its struct sigacts The current state of the signal actions is maintained in the new copy.

`sigactsfree`(p)

  This function decrements the reference count on the struct sigacts of process p. If the reference count reaches zero, the struct sigacts is freed.

`execsigs`(p)

  This function is used to reset the signal state of the process p to the system defaults when the process execs a new program image.

`sigaction1`(l, signum, nsa, osa, tramp, vers)

  This function implements the sigaction(2) system call. The tramp and vers arguments provide support for userspace signal trampolines. Trampoline version 0 is reserved for the legacy kernel-provided signal trampoline; tramp must be `NULL` in this case. Otherwise, vers specifies the ABI of the trampoline specified by tramp. The signal trampoline ABI is machine-dependent, and must be coordinated with the `sendsig`() function.

`sigprocmask1`(l, how, nss, oss)

  This function implements the sigprocmask(2) system call.

`sigpending1`(l, ss)

  This function implements the sigpending(2) system call.

`sigsuspend1`(l, ss)

  This function implements the sigsuspend(2) system call.

`sigaltstack1`(l, nss, oss)

  This function implements the sigaltstack(2) system call.

`pgsignal`(pgrp, signum, checkctty)

  This is a wrapper function for `kpgsignal`() which is described below.

`kpgsignal`(pgrp, ks, data, checkctty)

  Schedule the signal ks->ksi_signo to be delivered to all members of the process group pgrp. If checkctty is non-zero, the signal is only sent to processes which have a controlling terminal. The data argument and the complete signal scheduling semantics are described in the `kpsignal`() function below.

`trapsignal`(l, ks)

  Sends the signal ks->ksi_signo caused by a hardware trap to the current process.

`psignal`(p, signum)

  This is a wrapper function for `kpsignal`() which is described below.

`kpsignal`(p, ks, data)

  Schedule the signal ks->ksi_signo to be delivered to the process p. The data argument, if not `NULL`, points to the file descriptor data that caused the signal to be generated in the `SIGIO` case.

  With a few exceptions noted below, the target process signal disposition is updated and is marked as runnable, so further handling of the signal is done in the context of the target process after a context switch; see `issignal`() below. Note that `kpsignal`() does not by itself cause a context switch to happen.

  The target process is not marked as runnable in the following cases:

  - The target process is sleeping uninterruptibly. The signal will be noticed when the process returns from the system call or trap.
  - The target process is currently ignoring the signal.
  - If a stop signal is sent to a sleeping process that takes the default action (see sigaction(2)), the process is stopped without awakening it.
  - SIGCONT restarts a stopped process (or puts them back to sleep) regardless of the signal action (e.g., blocked or ignored).

  If the target process is being traced, `kpsignal`() behaves as if the target process were taking the default action for signum. This allows the tracing process to be notified of the signal.

`issignal`(l)

  This function determines which signal, if any, is to be posted to the current process. A signal is to be posted if:

  - The signal has a handler provided by the program image.
  - The signal should cause the process to dump core and/or terminate.
  - The signal should interrupt the current system call.

  Signals which cause the process to be stopped are handled within `issignal`() directly.

  `issignal`() should be called by machine-dependent code when returning to userspace from a system call or other trap or interrupt by using the following code:

      while (signum = CURSIG(curproc))
      	postsig(signum);

`postsig`(signum)

  The `postsig`() function is used to invoke the action for the signal signum in the current process. If the default action of a signal is to terminate the process, and the signal does not have a registered handler, the process exits using `sigexit`(), dumping a core image if necessary.

`killproc`(p, why)

  This function sends a SIGKILL signal to the specified process. The message provided by why is sent to the system log and is also displayed on the process's controlling terminal.

`sigexit`(l, signum)

  This function forces the current process to exit with the signal signum, generating a core file if appropriate. No checks are made for masked or caught signals; the process always exits.

`sendsig`(ks, mask)

  This function is provided by machine-dependent code, and is used to invoke a signal handler for the current process. `sendsig`() must prepare the registers and stack of the current process to invoke the signal handler stored in the process's struct sigacts. This may include switching to an alternate signal stack specified by the process. The previous register, stack, and signal state are stored in a ucontext_t, which is then copied out to the user's stack.

  The registers and stack must be set up to invoke the signal handler as follows:

      (*handler)(int signum, siginfo_t *info, void *ctx)

  where signum is the signal number, info contains additional signal specific information when `SA_SIGINFO` is specified when setting up the signal handler. ctx is the pointer to ucontext_t on the user's stack. The registers and stack must also arrange for the signal handler to return to the signal trampoline. The trampoline is then used to return to the code which was executing when the signal was delivered using the setcontext(2) system call.

  For performance reasons, it is recommended that `sendsig`() arrange for the signal handler to be invoked directly on architectures where it is convenient to do so. In this case, the trampoline is used only for the signal return path. If it is not feasible to directly invoke the signal handler, the trampoline is also used to invoke the handler, performing any final set up that was not possible for `sendsig`() to perform.

  `sendsig`() must invoke the signal trampoline with the correct ABI. The ABI of the signal trampoline is specified on a per-signal basis in the `sigacts`() structure for the process. Trampoline version 0 is reserved for the legacy kernel-provided, on-stack signal trampoline. All other trampoline versions indicate a specific trampoline ABI. This ABI is coordinated with machine-dependent code in the system C library.

## SIGNAL TRAMPOLINE

The signal trampoline is a special piece of code which provides support for invoking the signal handlers for a process. The trampoline is used to return from the signal handler back to the code which was executing when the signal was delivered, and is also used to invoke the handler itself on architectures where it is not feasible to have the kernel invoke the handler directly.

In traditional UNIX systems, the signal trampoline, also referred to as the “sigcode”, is provided by the kernel and copied to the top of the user's stack when a new process is created or a new program image is exec'd. Starting in NetBSD 2.0, the signal trampoline is provided by the system C library. This allows for more flexibility when the signal facility is extended, makes dealing with signals easier in debuggers, such as gdb(1), and may also enhance system security by allowing the kernel to disallow execution of code on the stack.

The signal trampoline is specified on a per-signal basis. The correct trampoline is selected automatically by the C library when a signal handler is registered by a process.

Signal trampolines have a special naming convention which enables debuggers to determine the characteristics of the signal handler and its arguments. Trampoline functions are named like so:

    __sigtramp_<flavor>_<version>

where:

⟨flavor⟩
  The flavor of the signal handler. The following flavors are valid:
  sigcontext
    Specifies a traditional BSD-style (deprecated) signal handler with the following signature:

        void (*handler)(int signum,
        	int code,
        	struct sigcontext *scp);

  siginfo
    Specifies a POSIX-style signal handler with the following signature:

        void (*handler)(int signum,
        	siginfo_t *si,
        	void *uc);

    Note: sigcontext style signal handlers are deprecated, and retained only for compatibility with older binaries.

⟨version⟩
  Specifies the ABI version of the signal trampoline. The trampoline ABI is coordinated with the machine-dependent kernel `sendsig`() function. The trampoline version needs to be unique even across different trampoline flavors, in order to simplify trampoline selection in the kernel.

The following is an example if a signal trampoline name which indicates that the trampoline is used for traditional BSD-style signal handlers and implements version 1 of the signal trampoline ABI:

    __sigtramp_sigcontext_1

The current signal trampoline is:

    __sigtramp_siginfo_2

# SEE ALSO

sigaction(2), signal(7), condvar(9)

April 29, 2010 | NetBSD 11.0