signal(7) Miscellaneous Information Manual signal(7) signal - Linux (reliable) POSIX (, << >>) POSIX. , , . <<>>, : Term -- . Ign -- . Core -- ( core(5)). Stop -- . Cont -- , . sigaction(2) signal(2) ( , ; signal(2)). : , , -- , , . . , ; sigaltstack(2). : . , fork(2), . execve(2) ; . : raise(3) . kill(2) , . pidfd_send_signal(2) Sends a signal to a process identified by a PID file descriptor. killpg(3) . pthread_kill(3) POSIX , . tgkill(2) ( pthread_kill(3)). sigqueue(3) . , ( ): pause(2) , . sigsuspend(2) ( ) . , , , . : o sigwaitinfo(2), sigtimedwait(2) sigwait(3). . . o signalfd(2). , , . read(2) , , signalfd(2), . read(2) , . . , , . . , , , , . pthread_sigmask(3). sigprocmask(2). , fork(2), ; execve(2). A signal may be process-directed or thread-directed. A process-directed signal is one that is targeted at (and thus pending for) the process as a whole. A signal may be process-directed because it was generated by the kernel for reasons other than a hardware exception, or because it was sent using kill(2) or sigqueue(3). A thread-directed signal is one that is targeted at a specific thread. A signal may be thread-directed because it was generated as a consequence of executing a specific machine-language instruction that triggered a hardware exception (e.g., SIGSEGV for an invalid memory access, or SIGFPE for a math error), or because it was targeted at a specific thread using interfaces such as tgkill(2) or pthread_kill(3). , . , , . , sigpending(2). , , . , fork(2), ; execve(2). Execution of signal handlers Whenever there is a transition from kernel-mode to user-mode execution (e.g., on return from a system call or scheduling of a thread onto the CPU), the kernel checks whether there is a pending unblocked signal for which the process has established a signal handler. If there is such a pending signal, the following steps occur: (1) The kernel performs the necessary preparatory steps for execution of the signal handler: (1.1) The signal is removed from the set of pending signals. (1.2) If the signal handler was installed by a call to sigaction(2) that specified the SA_ONSTACK flag and the thread has defined an alternate signal stack (using sigaltstack(2)), then that stack is installed. (1.3) Various pieces of signal-related context are saved into a special frame that is created on the stack. The saved information includes: o the program counter register (i.e., the address of the next instruction in the main program that should be executed when the signal handler returns); o architecture-specific register state required for resuming the interrupted program; o the thread's current signal mask; o the thread's alternate signal stack settings. (If the signal handler was installed using the sigaction(2) SA_SIGINFO flag, then the above information is accessible via the ucontext_t object that is pointed to by the third argument of the signal handler.) (1.4) Any signals specified in act->sa_mask when registering the handler with sigprocmask(2) are added to the thread's signal mask. The signal being delivered is also added to the signal mask, unless SA_NODEFER was specified when registering the handler. These signals are thus blocked while the handler executes. (2) The kernel constructs a frame for the signal handler on the stack. The kernel sets the program counter for the thread to point to the first instruction of the signal handler function, and configures the return address for that function to point to a piece of user-space code known as the signal trampoline (described in sigreturn(2)). (3) The kernel passes control back to user-space, where execution commences at the start of the signal handler function. (4) When the signal handler returns, control passes to the signal trampoline code. (5) The signal trampoline calls sigreturn(2), a system call that uses the information in the stack frame created in step 1 to restore the thread to its state before the signal handler was called. The thread's signal mask and alternate signal stack settings are restored as part of this procedure. Upon completion of the call to sigreturn(2), the kernel transfers control back to user space, and the thread recommences execution at the point where it was interrupted by the signal handler. Note that if the signal handler does not return (e.g., control is transferred out of the handler using siglongjmp(3), or the handler executes a new program with execve(2)), then the final step is not performed. In particular, in such scenarios it is the programmer's responsibility to restore the state of the signal mask (using sigprocmask(2)), if it is desired to unblock the signals that were blocked on entry to the signal handler. (Note that siglongjmp(3) may or may not restore the signal mask, depending on the savesigs value that was specified in the corresponding call to sigsetjmp(3).) From the kernel's point of view, execution of the signal handler code is exactly the same as the execution of any other user-space code. That is to say, the kernel does not record any special state information indicating that the thread is currently executing inside a signal handler. All necessary state information is maintained in user-space registers and the user-space stack. The depth to which nested signal handlers may be invoked is thus limited only by the user-space stack (and sensible software design!). Linux , . ( ), , , <> -- POSIX.1-1990; <> -- SUSv2 POSIX.1-2001. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- SIGABRT P1990 Core (abort), abort(3) SIGALRM P1990 Term , alarm(2) SIGBUS P2001 Core ( ) SIGCHLD P1990 Ign SIGCLD - Ign SIGCHLD SIGCONT P1990 Cont , SIGEMT - Term SIGFPE P1990 Core SIGHUP P1990 Term , SIGILL P1990 Core SIGINFO - SIGPWR SIGINT P1990 Term SIGIO - Term / (4.2BSD) SIGIOT - Core IOT. SIGABRT SIGKILL P1990 Term Kill- SIGLOST - Term ( ) SIGPIPE P1990 Term : ; pipe(7) SIGPOLL P2001 Term Pollable event (Sys V); synonym for SIGIO SIGPROF P2001 Term SIGPWR - Term (System V) SIGQUIT P1990 Core SIGSEGV P1990 Core SIGSTKFLT - Term ( ) SIGSTOP P1990 Stop SIGTSTP P1990 Stop SIGSYS P2001 Core (SVr4); seccomp(2) SIGTERM P1990 Term SIGTRAP P2001 Core - / SIGTTIN P1990 Stop SIGTTOU P1990 Stop SIGUNUSED - Core SIGSYS SIGURG P2001 Ign (4.2BSD) SIGUSR1 P1990 Term 1 SIGUSR2 P1990 Term 2 SIGVTALRM P2001 Term (4.2BSD) SIGXCPU P2001 Core (4.2BSD); setrlimit(2) SIGXFSZ P2001 Core (4.2BSD); setrlimit(2) SIGWINCH - Ign (4.3BSD, Sun) SIGKILL SIGSTOP , . Linux 2.2 SIGSYS, SIGXCPU, SIGXFSZ SIGBUS ( SPARC MIPS) ( UNIX SIGXCPU SIGXFSZ ). Linux 2.4 POSIX.1-2001 . SIGEMT POSIX.1-2001, , , UNIX, . SIGPWR ( POSIX.1-2001) , , ( UNIX). SIGIO ( POSIX.1-2001) UNIX . , . . , ( ). , , siginfo_t ( sigaction(2)), , . , , . . . x86, ARM ; Alpha SPARC, MIPS, PARISC. (-) , . x86/ARM Alpha/ MIPS PARISC SPARC --------------------------------------------------------------------------------------------------------------------------------------- SIGHUP 1 1 1 1 SIGINT 2 2 2 2 SIGQUIT 3 3 3 3 SIGILL 4 4 4 4 SIGTRAP 5 5 5 5 SIGABRT 6 6 6 6 SIGIOT 6 6 6 6 SIGBUS 7 10 10 10 SIGEMT - 7 7 - SIGFPE 8 8 8 8 SIGKILL 9 9 9 9 SIGUSR1 10 30 16 16 SIGSEGV 11 11 11 11 SIGUSR2 12 31 17 17 SIGPIPE 13 13 13 13 SIGALRM 14 14 14 14 SIGTERM 15 15 15 15 SIGSTKFLT 16 - - 7 SIGCHLD 17 20 18 18 SIGCLD - - 18 - SIGCONT 18 19 25 26 SIGSTOP 19 17 23 24 SIGTSTP 20 18 24 25 SIGTTIN 21 21 26 27 SIGTTOU 22 22 27 28 SIGURG 23 16 21 29 SIGXCPU 24 24 30 12 SIGXFSZ 25 25 31 30 SIGVTALRM 26 26 28 20 SIGPROF 27 27 29 21 SIGWINCH 28 28 20 23 SIGIO 29 23 22 22 SIGPOLL , SIGIO SIGPWR 30 29/- 19 19 SIGINFO - 29/- - - SIGLOST - -/29 - - SIGSYS 31 12 12 31 SIGUNUSED 31 - - 31 : o SIGUNUSED, SIGSYS. glibc 2.26, SIGUNUSED . o 29 Alpha SIGINFO/SIGPWR ( ), SPARC SIGLOST. Starting with Linux 2.2, Linux supports real-time signals as originally defined in the POSIX.1b real-time extensions (and now included in POSIX.1-2001). The range of supported real-time signals is defined by the macros SIGRTMIN and SIGRTMAX. POSIX.1-2001 requires that an implementation support at least _POSIX_RTSIG_MAX (8) real-time signals. Linux 33 , 32 64. POSIX glibc ( NPTL) ( LinuxThreads) ( pthreads(7)), SIGRTMIN ( 34 35). glibc ( glibc), , , UNIX, , SIGRTMIN+n ( ), SIGRTMIN+n SIGRTMAX. , : , . (terminate). : o . , , , . o sigqueue(3), (, ). , SA_SIGINFO sigaction(2), si_value siginfo_t, . , si_pid si_uid , . o . , . , ( ). . , , POSIX , . Linux, , . According to POSIX, an implementation should permit at least _POSIX_SIGQUEUE_MAX (32) real-time signals to be queued to a process. However, Linux does things differently. Up to and including Linux 2.6.7, Linux imposes a system-wide limit on the number of queued real-time signals for all processes. This limit can be viewed and (with privilege) changed via the /proc/sys/kernel/rtsig-max file. A related file, /proc/sys/kernel/rtsig-nr, can be used to find out how many real-time signals are currently queued. In Linux 2.6.8, these /proc interfaces were replaced by the RLIMIT_SIGPENDING resource limit, which specifies a per-user limit for queued signals; see setrlimit(2) for further details. (sigset_t) 32 64 . , , . : Linux 2.0 Linux 2.2 sigaction(2) rt_sigaction(2) sigpending(2) rt_sigpending(2) sigprocmask(2) rt_sigprocmask(2) sigreturn(2) rt_sigreturn(2) sigsuspend(2) rt_sigsuspend(2) sigtimedwait(2) rt_sigtimedwait(2) , : o ; o EINTR. , SA_RESTART ( sigaction(2)). UNIX ; Linux. , , SA_RESTART; EINTR: o read(2), readv(2), write(2), writev(2) ioctl(2) <<>> . <<>> , -, , , . - , (, ). , () ; - . o open(2), (, FIFO; fifo(7)). o wait(2), wait3(2), wait4(2), waitid(2) waitpid(2). o : accept(2), connect(2), recv(2), recvfrom(2), recvmmsg(2), recvmsg(2), send(2), sendto(2) sendmsg(2), ( ). o : flock(2) F_SETLKW F_OFD_SETLKW fcntl(2). o POSIX: mq_receive(3), mq_timedreceive(3), mq_send(3) mq_timedsend(3). o futex(2) FUTEX_WAIT ( Linux 2.6.22; EINTR). o getrandom(2). o pthread_mutex_lock(3), pthread_cond_wait(3) . o futex(2) FUTEX_WAIT_BITSET. o POSIX: sem_wait(3) sem_timedwait(3) ( Linux 2.6.22; EINTR). o read(2) inotify(7) ( Linux 3.8; EINTR). SA_RESTART; EINTR, : o <<>> , (SO_RCVTIMEO) setsockopt(2): accept(2), recv(2), recvfrom(2), recvmmsg(2) ( timeout, NULL) recvmsg(2). o <<>> , (SO_RCVTIMEO) setsockopt(2): connect(2), send(2), sendto(2) sendmsg(2). o , : pause(2), sigsuspend(2), sigtimedwait(2) sigwaitinfo(2). o (multiplexing) : epoll_wait(2), epoll_pwait(2), poll(2), ppoll(2), select(2) pselect(2). o IPC- System V: msgrcv(2), msgsnd(2), semop(2) semtimedop(2). o : clock_nanosleep(2), nanosleep(2) usleep(3). o io_getevents(2). sleep(3) , , : . In certain circumstances, the seccomp(2) user-space notification feature can lead to restarting of system calls that would otherwise never be restarted by SA_RESTART; for details, see seccomp_unotify(2). Linux, , EINTR, SIGCONT. POSIX.1 . Linux, : o <<>> , (SO_RCVTIMEO) setsockopt(2): accept(2), recv(2), recvfrom(2), recvmmsg(2) ( timeout, NULL) recvmsg(2). o <<>> , (SO_RCVTIMEO) setsockopt(2): connect(2), send(2), sendto(2) sendmsg(2), (SO_SNDTIMEO). o epoll_wait(2), epoll_pwait(2). o semop(2), semtimedop(2). o sigtimedwait(2), sigwaitinfo(2). o Linux 3.7 : read(2) inotify(7) o Linux 2.6.21 : futex(2) FUTEX_WAIT, sem_timedwait(3), sem_wait(3). o Linux 2.6.8 : msgrcv(2), msgsnd(2). o Linux 2.4 : nanosleep(2). POSIX.1, . signal-safety(7). The /proc/pid/task/tid/status file contains various fields that show the signals that a thread is blocking (SigBlk), catching (SigCgt), or ignoring (SigIgn). (The set of signals that are caught or ignored will be the same across all threads in a process.) Other fields show the set of pending signals that are directed to the thread (SigPnd) as well as the set of pending signals that are directed to the process as a whole (ShdPnd). The corresponding fields in /proc/pid/status show the information for the main thread. See proc(5) for further details. There are six signals that can be delivered as a consequence of a hardware exception: SIGBUS, SIGEMT, SIGFPE, SIGILL, SIGSEGV, and SIGTRAP. Which of these signals is delivered, for any given hardware exception, is not documented and does not always make sense. For example, an invalid memory access that causes delivery of SIGSEGV on one CPU architecture may cause delivery of SIGBUS on another architecture, or vice versa. For another example, using the x86 int instruction with a forbidden argument (any number other than 3 or 128) causes delivery of SIGSEGV, even though SIGILL would make more sense, because of how the CPU reports the forbidden operation to the kernel. . kill(1), clone(2), getrlimit(2), kill(2), pidfd_send_signal(2), restart_syscall(2), rt_sigqueueinfo(2), setitimer(2), setrlimit(2), sgetmask(2), sigaction(2), sigaltstack(2), signal(2), signalfd(2), sigpending(2), sigprocmask(2), sigreturn(2), sigsuspend(2), sigwaitinfo(2), abort(3), bsd_signal(3), killpg(3), longjmp(3), pthread_sigqueue(3), raise(3), sigqueue(3), sigset(3), sigsetops(3), sigvec(3), sigwait(3), strsignal(3), swapcontext(3), sysv_signal(3), core(5), proc(5), nptl(7), pthreads(7), sigevent(3type) Alexander Golubev , Azamat Hackimov , Hotellook, Nikita , Spiros Georgaras , Vladislav , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . signal(7)