]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man7/signal.7
42f7721f157cb2b651a603f96b466607777c3538
[thirdparty/man-pages.git] / man7 / signal.7
1 .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
2 .\" and Copyright (c) 2002, 2006, 2020 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" and Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
4 .\" <mtk.manpages@gmail.com>
5 .\"
6 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\"
8 .\" Modified Sat Jul 24 17:34:08 1993 by Rik Faith (faith@cs.unc.edu)
9 .\" Modified Sun Jan 7 01:41:27 1996 by Andries Brouwer (aeb@cwi.nl)
10 .\" Modified Sun Apr 14 12:02:29 1996 by Andries Brouwer (aeb@cwi.nl)
11 .\" Modified Sat Nov 13 16:28:23 1999 by Andries Brouwer (aeb@cwi.nl)
12 .\" Modified 10 Apr 2002, by Michael Kerrisk <mtk.manpages@gmail.com>
13 .\" Modified 7 Jun 2002, by Michael Kerrisk <mtk.manpages@gmail.com>
14 .\" Added information on real-time signals
15 .\" Modified 13 Jun 2002, by Michael Kerrisk <mtk.manpages@gmail.com>
16 .\" Noted that SIGSTKFLT is in fact unused
17 .\" 2004-12-03, Modified mtk, added notes on RLIMIT_SIGPENDING
18 .\" 2006-04-24, mtk, Added text on changing signal dispositions,
19 .\" signal mask, and pending signals.
20 .\" 2008-07-04, mtk:
21 .\" Added section on system call restarting (SA_RESTART)
22 .\" Added section on stop/cont signals interrupting syscalls.
23 .\" 2008-10-05, mtk: various additions
24 .\"
25 .TH SIGNAL 7 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
26 .SH NAME
27 signal \- overview of signals
28 .SH DESCRIPTION
29 Linux supports both POSIX reliable signals (hereinafter
30 "standard signals") and POSIX real-time signals.
31 .SS Signal dispositions
32 Each signal has a current
33 .IR disposition ,
34 which determines how the process behaves when it is delivered
35 the signal.
36 .PP
37 The entries in the "Action" column of the table below specify
38 the default disposition for each signal, as follows:
39 .IP Term
40 Default action is to terminate the process.
41 .IP Ign
42 Default action is to ignore the signal.
43 .IP Core
44 Default action is to terminate the process and dump core (see
45 .BR core (5)).
46 .IP Stop
47 Default action is to stop the process.
48 .IP Cont
49 Default action is to continue the process if it is currently stopped.
50 .PP
51 A process can change the disposition of a signal using
52 .BR sigaction (2)
53 or
54 .BR signal (2).
55 (The latter is less portable when establishing a signal handler;
56 see
57 .BR signal (2)
58 for details.)
59 Using these system calls, a process can elect one of the
60 following behaviors to occur on delivery of the signal:
61 perform the default action; ignore the signal;
62 or catch the signal with a
63 .IR "signal handler" ,
64 a programmer-defined function that is automatically invoked
65 when the signal is delivered.
66 .PP
67 By default, a signal handler is invoked on the
68 normal process stack.
69 It is possible to arrange that the signal handler
70 uses an alternate stack; see
71 .BR sigaltstack (2)
72 for a discussion of how to do this and when it might be useful.
73 .PP
74 The signal disposition is a per-process attribute:
75 in a multithreaded application, the disposition of a
76 particular signal is the same for all threads.
77 .PP
78 A child created via
79 .BR fork (2)
80 inherits a copy of its parent's signal dispositions.
81 During an
82 .BR execve (2),
83 the dispositions of handled signals are reset to the default;
84 the dispositions of ignored signals are left unchanged.
85 .SS Sending a signal
86 The following system calls and library functions allow
87 the caller to send a signal:
88 .TP
89 .BR raise (3)
90 Sends a signal to the calling thread.
91 .TP
92 .BR kill (2)
93 Sends a signal to a specified process,
94 to all members of a specified process group,
95 or to all processes on the system.
96 .TP
97 .BR pidfd_send_signal (2)
98 Sends a signal to a process identified by a PID file descriptor.
99 .TP
100 .BR killpg (3)
101 Sends a signal to all of the members of a specified process group.
102 .TP
103 .BR pthread_kill (3)
104 Sends a signal to a specified POSIX thread in the same process as
105 the caller.
106 .TP
107 .BR tgkill (2)
108 Sends a signal to a specified thread within a specific process.
109 (This is the system call used to implement
110 .BR pthread_kill (3).)
111 .TP
112 .BR sigqueue (3)
113 Sends a real-time signal with accompanying data to a specified process.
114 .SS Waiting for a signal to be caught
115 The following system calls suspend execution of the calling
116 thread until a signal is caught
117 (or an unhandled signal terminates the process):
118 .TP
119 .BR pause (2)
120 Suspends execution until any signal is caught.
121 .TP
122 .BR sigsuspend (2)
123 Temporarily changes the signal mask (see below) and suspends
124 execution until one of the unmasked signals is caught.
125 .\"
126 .SS Synchronously accepting a signal
127 Rather than asynchronously catching a signal via a signal handler,
128 it is possible to synchronously accept the signal, that is,
129 to block execution until the signal is delivered,
130 at which point the kernel returns information about the
131 signal to the caller.
132 There are two general ways to do this:
133 .IP * 2
134 .BR sigwaitinfo (2),
135 .BR sigtimedwait (2),
136 and
137 .BR sigwait (3)
138 suspend execution until one of the signals in a specified
139 set is delivered.
140 Each of these calls returns information about the delivered signal.
141 .IP *
142 .BR signalfd (2)
143 returns a file descriptor that can be used to read information
144 about signals that are delivered to the caller.
145 Each
146 .BR read (2)
147 from this file descriptor blocks until one of the signals
148 in the set specified in the
149 .BR signalfd (2)
150 call is delivered to the caller.
151 The buffer returned by
152 .BR read (2)
153 contains a structure describing the signal.
154 .SS Signal mask and pending signals
155 A signal may be
156 .IR blocked ,
157 which means that it will not be delivered until it is later unblocked.
158 Between the time when it is generated and when it is delivered
159 a signal is said to be
160 .IR pending .
161 .PP
162 Each thread in a process has an independent
163 .IR "signal mask" ,
164 which indicates the set of signals that the thread is currently blocking.
165 A thread can manipulate its signal mask using
166 .BR pthread_sigmask (3).
167 In a traditional single-threaded application,
168 .BR sigprocmask (2)
169 can be used to manipulate the signal mask.
170 .PP
171 A child created via
172 .BR fork (2)
173 inherits a copy of its parent's signal mask;
174 the signal mask is preserved across
175 .BR execve (2).
176 .PP
177 A signal may be process-directed or thread-directed.
178 A process-directed signal is one that is targeted at (and thus pending for)
179 the process as a whole.
180 A signal may be process-directed
181 because it was generated by the kernel for reasons
182 other than a hardware exception, or because it was sent using
183 .BR kill (2)
184 or
185 .BR sigqueue (3).
186 A thread-directed signal is one that is targeted at a specific thread.
187 A signal may be thread-directed because it was generated as a consequence
188 of executing a specific machine-language instruction
189 that triggered a hardware exception (e.g.,
190 .B SIGSEGV
191 for an invalid memory access, or
192 .B SIGFPE
193 for a math error), or because it was
194 targeted at a specific thread using
195 interfaces such as
196 .BR tgkill (2)
197 or
198 .BR pthread_kill (3).
199 .PP
200 A process-directed signal may be delivered to any one of the
201 threads that does not currently have the signal blocked.
202 .\" Joseph C. Sible notes:
203 .\" On Linux, if the main thread has the signal unblocked, then the kernel
204 .\" will always deliver the signal there, citing this kernel code
205 .\"
206 .\" Per this comment in kernel/signal.c since time immemorial:
207 .\"
208 .\" /*
209 .\" * Now find a thread we can wake up to take the signal off the queue.
210 .\" *
211 .\" * If the main thread wants the signal, it gets first crack.
212 .\" * Probably the least surprising to the average bear.
213 .\" */
214 .\"
215 .\" But this does not mean the signal will be delivered only in the
216 .\" main thread, since if a handler is already executing in the main thread
217 .\" (and thus the signal is blocked in that thread), then a further
218 .\" might be delivered in a different thread.
219 .\"
220 If more than one of the threads has the signal unblocked, then the
221 kernel chooses an arbitrary thread to which to deliver the signal.
222 .PP
223 A thread can obtain the set of signals that it currently has pending
224 using
225 .BR sigpending (2).
226 This set will consist of the union of the set of pending
227 process-directed signals and the set of signals pending for
228 the calling thread.
229 .PP
230 A child created via
231 .BR fork (2)
232 initially has an empty pending signal set;
233 the pending signal set is preserved across an
234 .BR execve (2).
235 .\"
236 .SS Execution of signal handlers
237 Whenever there is a transition from kernel-mode to user-mode execution
238 (e.g., on return from a system call or scheduling of a thread onto the CPU),
239 the kernel checks whether there is a pending unblocked signal
240 for which the process has established a signal handler.
241 If there is such a pending signal, the following steps occur:
242 .IP 1. 3
243 The kernel performs the necessary preparatory steps for execution of
244 the signal handler:
245 .RS
246 .IP a) 3
247 The signal is removed from the set of pending signals.
248 .IP b)
249 If the signal handler was installed by a call to
250 .BR sigaction (2)
251 that specified the
252 .B SA_ONSTACK
253 flag and the thread has defined an alternate signal stack (using
254 .BR sigaltstack (2)),
255 then that stack is installed.
256 .IP c)
257 Various pieces of signal-related context are saved
258 into a special frame that is created on the stack.
259 The saved information includes:
260 .RS
261 .IP + 2
262 the program counter register
263 (i.e., the address of the next instruction in the main program that
264 should be executed when the signal handler returns);
265 .IP +
266 architecture-specific register state required for resuming the
267 interrupted program;
268 .IP +
269 the thread's current signal mask;
270 .IP +
271 the thread's alternate signal stack settings.
272 .RE
273 .IP
274 (If the signal handler was installed using the
275 .BR sigaction (2)
276 .B SA_SIGINFO
277 flag, then the above information is accessible via the
278 .I ucontext_t
279 object that is pointed to by the third argument of the signal handler.)
280 .IP d)
281 Any signals specified in
282 .I act\->sa_mask
283 when registering the handler with
284 .BR sigprocmask (2)
285 are added to the thread's signal mask.
286 The signal being delivered is also
287 added to the signal mask, unless
288 .B SA_NODEFER
289 was specified when registering the handler.
290 These signals are thus blocked while the handler executes.
291 .RE
292 .IP 2.
293 The kernel constructs a frame for the signal handler on the stack.
294 The kernel sets the program counter for the thread to point to the first
295 instruction of the signal handler function,
296 and configures the return address for that function to point to a piece
297 of user-space code known as the signal trampoline (described in
298 .BR sigreturn (2)).
299 .IP 3.
300 The kernel passes control back to user-space, where execution
301 commences at the start of the signal handler function.
302 .IP 4.
303 When the signal handler returns, control passes to the signal trampoline code.
304 .IP 5.
305 The signal trampoline calls
306 .BR sigreturn (2),
307 a system call that uses the information in the stack frame created in step 1
308 to restore the thread to its state before the signal handler was
309 called.
310 The thread's signal mask and alternate signal stack settings
311 are restored as part of this procedure.
312 Upon completion of the call to
313 .BR sigreturn (2),
314 the kernel transfers control back to user space,
315 and the thread recommences execution at the point where it was
316 interrupted by the signal handler.
317 .PP
318 Note that if the signal handler does not return
319 (e.g., control is transferred out of the handler using
320 .BR siglongjmp (3),
321 or the handler executes a new program with
322 .BR execve (2)),
323 then the final step is not performed.
324 In particular, in such scenarios it is the programmer's responsibility
325 to restore the state of the signal mask (using
326 .BR sigprocmask (2)),
327 if it is desired to unblock the signals that were blocked on entry
328 to the signal handler.
329 (Note that
330 .BR siglongjmp (3)
331 may or may not restore the signal mask, depending on the
332 .I savesigs
333 value that was specified in the corresponding call to
334 .BR sigsetjmp (3).)
335 .PP
336 From the kernel's point of view,
337 execution of the signal handler code is exactly the same as the execution
338 of any other user-space code.
339 That is to say, the kernel does not record any special state information
340 indicating that the thread is currently executing inside a signal handler.
341 All necessary state information is maintained in user-space registers
342 and the user-space stack.
343 The depth to which nested signal handlers may be invoked is thus
344 limited only by the user-space stack (and sensible software design!).
345 .\"
346 .SS Standard signals
347 Linux supports the standard signals listed below.
348 The second column of the table indicates which standard (if any)
349 specified the signal: "P1990" indicates that the signal is described
350 in the original POSIX.1-1990 standard;
351 "P2001" indicates that the signal was added in SUSv2 and POSIX.1-2001.
352 .TS
353 l c c l
354 ____
355 lB c c l.
356 Signal Standard Action Comment
357 SIGABRT P1990 Core Abort signal from \fBabort\fP(3)
358 SIGALRM P1990 Term Timer signal from \fBalarm\fP(2)
359 SIGBUS P2001 Core Bus error (bad memory access)
360 SIGCHLD P1990 Ign Child stopped or terminated
361 SIGCLD \- Ign A synonym for \fBSIGCHLD\fP
362 SIGCONT P1990 Cont Continue if stopped
363 SIGEMT \- Term Emulator trap
364 SIGFPE P1990 Core Floating-point exception
365 SIGHUP P1990 Term Hangup detected on controlling terminal
366 or death of controlling process
367 SIGILL P1990 Core Illegal Instruction
368 SIGINFO \- A synonym for \fBSIGPWR\fP
369 SIGINT P1990 Term Interrupt from keyboard
370 SIGIO \- Term I/O now possible (4.2BSD)
371 SIGIOT \- Core IOT trap. A synonym for \fBSIGABRT\fP
372 SIGKILL P1990 Term Kill signal
373 SIGLOST \- Term File lock lost (unused)
374 SIGPIPE P1990 Term Broken pipe: write to pipe with no
375 readers; see \fBpipe\fP(7)
376 SIGPOLL P2001 Term Pollable event (Sys V);
377 synonym for \fBSIGIO\fP
378 SIGPROF P2001 Term Profiling timer expired
379 SIGPWR \- Term Power failure (System V)
380 SIGQUIT P1990 Core Quit from keyboard
381 SIGSEGV P1990 Core Invalid memory reference
382 SIGSTKFLT \- Term Stack fault on coprocessor (unused)
383 SIGSTOP P1990 Stop Stop process
384 SIGTSTP P1990 Stop Stop typed at terminal
385 SIGSYS P2001 Core Bad system call (SVr4);
386 see also \fBseccomp\fP(2)
387 SIGTERM P1990 Term Termination signal
388 SIGTRAP P2001 Core Trace/breakpoint trap
389 SIGTTIN P1990 Stop Terminal input for background process
390 SIGTTOU P1990 Stop Terminal output for background process
391 SIGUNUSED \- Core Synonymous with \fBSIGSYS\fP
392 SIGURG P2001 Ign Urgent condition on socket (4.2BSD)
393 SIGUSR1 P1990 Term User-defined signal 1
394 SIGUSR2 P1990 Term User-defined signal 2
395 SIGVTALRM P2001 Term Virtual alarm clock (4.2BSD)
396 SIGXCPU P2001 Core CPU time limit exceeded (4.2BSD);
397 see \fBsetrlimit\fP(2)
398 SIGXFSZ P2001 Core File size limit exceeded (4.2BSD);
399 see \fBsetrlimit\fP(2)
400 SIGWINCH \- Ign Window resize signal (4.3BSD, Sun)
401 .TE
402 .PP
403 The signals
404 .B SIGKILL
405 and
406 .B SIGSTOP
407 cannot be caught, blocked, or ignored.
408 .PP
409 Up to and including Linux 2.2, the default behavior for
410 .BR SIGSYS ", " SIGXCPU ", " SIGXFSZ ,
411 and (on architectures other than SPARC and MIPS)
412 .B SIGBUS
413 was to terminate the process (without a core dump).
414 (On some other UNIX systems the default action for
415 .BR SIGXCPU " and " SIGXFSZ
416 is to terminate the process without a core dump.)
417 Linux 2.4 conforms to the POSIX.1-2001 requirements for these signals,
418 terminating the process with a core dump.
419 .PP
420 .B SIGEMT
421 is not specified in POSIX.1-2001, but nevertheless appears
422 on most other UNIX systems,
423 where its default action is typically to terminate
424 the process with a core dump.
425 .PP
426 .B SIGPWR
427 (which is not specified in POSIX.1-2001) is typically ignored
428 by default on those other UNIX systems where it appears.
429 .PP
430 .B SIGIO
431 (which is not specified in POSIX.1-2001) is ignored by default
432 on several other UNIX systems.
433 .\"
434 .SS Queueing and delivery semantics for standard signals
435 If multiple standard signals are pending for a process,
436 the order in which the signals are delivered is unspecified.
437 .PP
438 Standard signals do not queue.
439 If multiple instances of a standard signal are generated while
440 that signal is blocked,
441 then only one instance of the signal is marked as pending
442 (and the signal will be delivered just once when it is unblocked).
443 In the case where a standard signal is already pending, the
444 .I siginfo_t
445 structure (see
446 .BR sigaction (2))
447 associated with that signal is not overwritten
448 on arrival of subsequent instances of the same signal.
449 Thus, the process will receive the information
450 associated with the first instance of the signal.
451 .\"
452 .SS Signal numbering for standard signals
453 The numeric value for each signal is given in the table below.
454 As shown in the table, many signals have different numeric values
455 on different architectures.
456 The first numeric value in each table row shows the signal number
457 on x86, ARM, and most other architectures;
458 the second value is for Alpha and SPARC; the third is for MIPS;
459 and the last is for PARISC.
460 A dash (\-) denotes that a signal is absent on the corresponding architecture.
461 .TS
462 l c c c c l
463 l c c c c l
464 ______
465 lB c c c c l.
466 Signal x86/ARM Alpha/ MIPS PARISC Notes
467 most others SPARC
468 SIGHUP \01 \01 \01 \01
469 SIGINT \02 \02 \02 \02
470 SIGQUIT \03 \03 \03 \03
471 SIGILL \04 \04 \04 \04
472 SIGTRAP \05 \05 \05 \05
473 SIGABRT \06 \06 \06 \06
474 SIGIOT \06 \06 \06 \06
475 SIGBUS \07 10 10 10
476 SIGEMT \- \07 \07 -
477 SIGFPE \08 \08 \08 \08
478 SIGKILL \09 \09 \09 \09
479 SIGUSR1 10 30 16 16
480 SIGSEGV 11 11 11 11
481 SIGUSR2 12 31 17 17
482 SIGPIPE 13 13 13 13
483 SIGALRM 14 14 14 14
484 SIGTERM 15 15 15 15
485 SIGSTKFLT 16 \- \- \07
486 SIGCHLD 17 20 18 18
487 SIGCLD \- \- 18 \-
488 SIGCONT 18 19 25 26
489 SIGSTOP 19 17 23 24
490 SIGTSTP 20 18 24 25
491 SIGTTIN 21 21 26 27
492 SIGTTOU 22 22 27 28
493 SIGURG 23 16 21 29
494 SIGXCPU 24 24 30 12
495 SIGXFSZ 25 25 31 30
496 SIGVTALRM 26 26 28 20
497 SIGPROF 27 27 29 21
498 SIGWINCH 28 28 20 23
499 SIGIO 29 23 22 22
500 SIGPOLL Same as SIGIO
501 SIGPWR 30 29/\- 19 19
502 SIGINFO \- 29/\- \- \-
503 SIGLOST \- \-/29 \- \-
504 SIGSYS 31 12 12 31
505 SIGUNUSED 31 \- \- 31
506 .TE
507 .PP
508 Note the following:
509 .IP * 3
510 Where defined,
511 .B SIGUNUSED
512 is synonymous with
513 .BR SIGSYS .
514 Since glibc 2.26,
515 .B SIGUNUSED
516 is no longer defined on any architecture.
517 .IP *
518 Signal 29 is
519 .BR SIGINFO / SIGPWR
520 (synonyms for the same value) on Alpha but
521 .B SIGLOST
522 on SPARC.
523 .\"
524 .SS Real-time signals
525 Starting with version 2.2,
526 Linux supports real-time signals as originally defined in the POSIX.1b
527 real-time extensions (and now included in POSIX.1-2001).
528 The range of supported real-time signals is defined by the macros
529 .B SIGRTMIN
530 and
531 .BR SIGRTMAX .
532 POSIX.1-2001 requires that an implementation support at least
533 .B _POSIX_RTSIG_MAX
534 (8) real-time signals.
535 .PP
536 The Linux kernel supports a range of 33 different real-time
537 signals, numbered 32 to 64.
538 However, the glibc POSIX threads implementation internally uses
539 two (for NPTL) or three (for LinuxThreads) real-time signals
540 (see
541 .BR pthreads (7)),
542 and adjusts the value of
543 .B SIGRTMIN
544 suitably (to 34 or 35).
545 Because the range of available real-time signals varies according
546 to the glibc threading implementation (and this variation can occur
547 at run time according to the available kernel and glibc),
548 and indeed the range of real-time signals varies across UNIX systems,
549 programs should
550 .IR "never refer to real-time signals using hard-coded numbers" ,
551 but instead should always refer to real-time signals using the notation
552 .BR SIGRTMIN +n,
553 and include suitable (run-time) checks that
554 .BR SIGRTMIN +n
555 does not exceed
556 .BR SIGRTMAX .
557 .PP
558 Unlike standard signals, real-time signals have no predefined meanings:
559 the entire set of real-time signals can be used for application-defined
560 purposes.
561 .PP
562 The default action for an unhandled real-time signal is to terminate the
563 receiving process.
564 .PP
565 Real-time signals are distinguished by the following:
566 .IP 1. 4
567 Multiple instances of real-time signals can be queued.
568 By contrast, if multiple instances of a standard signal are delivered
569 while that signal is currently blocked, then only one instance is queued.
570 .IP 2. 4
571 If the signal is sent using
572 .BR sigqueue (3),
573 an accompanying value (either an integer or a pointer) can be sent
574 with the signal.
575 If the receiving process establishes a handler for this signal using the
576 .B SA_SIGINFO
577 flag to
578 .BR sigaction (2),
579 then it can obtain this data via the
580 .I si_value
581 field of the
582 .I siginfo_t
583 structure passed as the second argument to the handler.
584 Furthermore, the
585 .I si_pid
586 and
587 .I si_uid
588 fields of this structure can be used to obtain the PID
589 and real user ID of the process sending the signal.
590 .IP 3. 4
591 Real-time signals are delivered in a guaranteed order.
592 Multiple real-time signals of the same type are delivered in the order
593 they were sent.
594 If different real-time signals are sent to a process, they are delivered
595 starting with the lowest-numbered signal.
596 (I.e., low-numbered signals have highest priority.)
597 By contrast, if multiple standard signals are pending for a process,
598 the order in which they are delivered is unspecified.
599 .PP
600 If both standard and real-time signals are pending for a process,
601 POSIX leaves it unspecified which is delivered first.
602 Linux, like many other implementations, gives priority
603 to standard signals in this case.
604 .PP
605 According to POSIX, an implementation should permit at least
606 .B _POSIX_SIGQUEUE_MAX
607 (32) real-time signals to be queued to
608 a process.
609 However, Linux does things differently.
610 In kernels up to and including 2.6.7, Linux imposes
611 a system-wide limit on the number of queued real-time signals
612 for all processes.
613 This limit can be viewed and (with privilege) changed via the
614 .I /proc/sys/kernel/rtsig\-max
615 file.
616 A related file,
617 .IR /proc/sys/kernel/rtsig\-nr ,
618 can be used to find out how many real-time signals are currently queued.
619 In Linux 2.6.8, these
620 .I /proc
621 interfaces were replaced by the
622 .B RLIMIT_SIGPENDING
623 resource limit, which specifies a per-user limit for queued
624 signals; see
625 .BR setrlimit (2)
626 for further details.
627 .PP
628 The addition of real-time signals required the widening
629 of the signal set structure
630 .RI ( sigset_t )
631 from 32 to 64 bits.
632 Consequently, various system calls were superseded by new system calls
633 that supported the larger signal sets.
634 The old and new system calls are as follows:
635 .TS
636 lb lb
637 l l.
638 Linux 2.0 and earlier Linux 2.2 and later
639 \fBsigaction\fP(2) \fBrt_sigaction\fP(2)
640 \fBsigpending\fP(2) \fBrt_sigpending\fP(2)
641 \fBsigprocmask\fP(2) \fBrt_sigprocmask\fP(2)
642 \fBsigreturn\fP(2) \fBrt_sigreturn\fP(2)
643 \fBsigsuspend\fP(2) \fBrt_sigsuspend\fP(2)
644 \fBsigtimedwait\fP(2) \fBrt_sigtimedwait\fP(2)
645 .TE
646 .\"
647 .SS Interruption of system calls and library functions by signal handlers
648 If a signal handler is invoked while a system call or library
649 function call is blocked, then either:
650 .IP * 2
651 the call is automatically restarted after the signal handler returns; or
652 .IP *
653 the call fails with the error
654 .BR EINTR .
655 .PP
656 Which of these two behaviors occurs depends on the interface and
657 whether or not the signal handler was established using the
658 .B SA_RESTART
659 flag (see
660 .BR sigaction (2)).
661 The details vary across UNIX systems;
662 below, the details for Linux.
663 .PP
664 If a blocked call to one of the following interfaces is interrupted
665 by a signal handler, then the call is automatically restarted
666 after the signal handler returns if the
667 .B SA_RESTART
668 flag was used; otherwise the call fails with the error
669 .BR EINTR :
670 .\" The following system calls use ERESTARTSYS,
671 .\" so that they are restartable
672 .IP * 2
673 .BR read (2),
674 .BR readv (2),
675 .BR write (2),
676 .BR writev (2),
677 and
678 .BR ioctl (2)
679 calls on "slow" devices.
680 A "slow" device is one where the I/O call may block for an
681 indefinite time, for example, a terminal, pipe, or socket.
682 If an I/O call on a slow device has already transferred some
683 data by the time it is interrupted by a signal handler,
684 then the call will return a success status
685 (normally, the number of bytes transferred).
686 Note that a (local) disk is not a slow device according to this definition;
687 I/O operations on disk devices are not interrupted by signals.
688 .IP *
689 .BR open (2),
690 if it can block (e.g., when opening a FIFO; see
691 .BR fifo (7)).
692 .IP *
693 .BR wait (2),
694 .BR wait3 (2),
695 .BR wait4 (2),
696 .BR waitid (2),
697 and
698 .BR waitpid (2).
699 .IP *
700 Socket interfaces:
701 .\" If a timeout (setsockopt()) is in effect on the socket, then these
702 .\" system calls switch to using EINTR. Consequently, they and are not
703 .\" automatically restarted, and they show the stop/cont behavior
704 .\" described below. (Verified from 2.6.26 source, and by experiment; mtk)
705 .BR accept (2),
706 .BR connect (2),
707 .BR recv (2),
708 .BR recvfrom (2),
709 .BR recvmmsg (2),
710 .BR recvmsg (2),
711 .BR send (2),
712 .BR sendto (2),
713 and
714 .BR sendmsg (2),
715 .\" FIXME What about sendmmsg()?
716 unless a timeout has been set on the socket (see below).
717 .IP *
718 File locking interfaces:
719 .BR flock (2)
720 and
721 the
722 .B F_SETLKW
723 and
724 .B F_OFD_SETLKW
725 operations of
726 .BR fcntl (2)
727 .IP *
728 POSIX message queue interfaces:
729 .BR mq_receive (3),
730 .BR mq_timedreceive (3),
731 .BR mq_send (3),
732 and
733 .BR mq_timedsend (3).
734 .IP *
735 .BR futex (2)
736 .B FUTEX_WAIT
737 (since Linux 2.6.22;
738 .\" commit 72c1bbf308c75a136803d2d76d0e18258be14c7a
739 beforehand, always failed with
740 .BR EINTR ).
741 .IP *
742 .BR getrandom (2).
743 .IP *
744 .BR pthread_mutex_lock (3),
745 .BR pthread_cond_wait (3),
746 and related APIs.
747 .IP *
748 .BR futex (2)
749 .BR FUTEX_WAIT_BITSET .
750 .IP *
751 POSIX semaphore interfaces:
752 .BR sem_wait (3)
753 and
754 .BR sem_timedwait (3)
755 (since Linux 2.6.22;
756 .\" as a consequence of the 2.6.22 changes in the futex() implementation
757 beforehand, always failed with
758 .BR EINTR ).
759 .IP *
760 .BR read (2)
761 from an
762 .BR inotify (7)
763 file descriptor
764 (since Linux 3.8;
765 .\" commit 1ca39ab9d21ac93f94b9e3eb364ea9a5cf2aba06
766 beforehand, always failed with
767 .BR EINTR ).
768 .PP
769 The following interfaces are never restarted after
770 being interrupted by a signal handler,
771 regardless of the use of
772 .BR SA_RESTART ;
773 they always fail with the error
774 .B EINTR
775 when interrupted by a signal handler:
776 .\" These are the system calls that give EINTR or ERESTARTNOHAND
777 .\" on interruption by a signal handler.
778 .IP * 2
779 "Input" socket interfaces, when a timeout
780 .RB ( SO_RCVTIMEO )
781 has been set on the socket using
782 .BR setsockopt (2):
783 .BR accept (2),
784 .BR recv (2),
785 .BR recvfrom (2),
786 .BR recvmmsg (2)
787 (also with a non-NULL
788 .I timeout
789 argument),
790 and
791 .BR recvmsg (2).
792 .IP *
793 "Output" socket interfaces, when a timeout
794 .RB ( SO_RCVTIMEO )
795 has been set on the socket using
796 .BR setsockopt (2):
797 .BR connect (2),
798 .BR send (2),
799 .BR sendto (2),
800 and
801 .BR sendmsg (2).
802 .\" FIXME What about sendmmsg()?
803 .IP *
804 Interfaces used to wait for signals:
805 .BR pause (2),
806 .BR sigsuspend (2),
807 .BR sigtimedwait (2),
808 and
809 .BR sigwaitinfo (2).
810 .IP *
811 File descriptor multiplexing interfaces:
812 .BR epoll_wait (2),
813 .BR epoll_pwait (2),
814 .BR poll (2),
815 .BR ppoll (2),
816 .BR select (2),
817 and
818 .BR pselect (2).
819 .IP *
820 System V IPC interfaces:
821 .\" On some other systems, SA_RESTART does restart these system calls
822 .BR msgrcv (2),
823 .BR msgsnd (2),
824 .BR semop (2),
825 and
826 .BR semtimedop (2).
827 .IP *
828 Sleep interfaces:
829 .BR clock_nanosleep (2),
830 .BR nanosleep (2),
831 and
832 .BR usleep (3).
833 .IP *
834 .BR io_getevents (2).
835 .PP
836 The
837 .BR sleep (3)
838 function is also never restarted if interrupted by a handler,
839 but gives a success return: the number of seconds remaining to sleep.
840 .PP
841 In certain circumstances, the
842 .BR seccomp (2)
843 user-space notification feature can lead to restarting of system calls
844 that would otherwise never be restarted by
845 .BR SA_RESTART ;
846 for details, see
847 .BR seccomp_unotify (2).
848 .\"
849 .SS Interruption of system calls and library functions by stop signals
850 On Linux, even in the absence of signal handlers,
851 certain blocking interfaces can fail with the error
852 .B EINTR
853 after the process is stopped by one of the stop signals
854 and then resumed via
855 .BR SIGCONT .
856 This behavior is not sanctioned by POSIX.1, and doesn't occur
857 on other systems.
858 .PP
859 The Linux interfaces that display this behavior are:
860 .IP * 2
861 "Input" socket interfaces, when a timeout
862 .RB ( SO_RCVTIMEO )
863 has been set on the socket using
864 .BR setsockopt (2):
865 .BR accept (2),
866 .BR recv (2),
867 .BR recvfrom (2),
868 .BR recvmmsg (2)
869 (also with a non-NULL
870 .I timeout
871 argument),
872 and
873 .BR recvmsg (2).
874 .IP *
875 "Output" socket interfaces, when a timeout
876 .RB ( SO_RCVTIMEO )
877 has been set on the socket using
878 .BR setsockopt (2):
879 .BR connect (2),
880 .BR send (2),
881 .BR sendto (2),
882 and
883 .\" FIXME What about sendmmsg()?
884 .BR sendmsg (2),
885 if a send timeout
886 .RB ( SO_SNDTIMEO )
887 has been set.
888 .IP * 2
889 .BR epoll_wait (2),
890 .BR epoll_pwait (2).
891 .IP *
892 .BR semop (2),
893 .BR semtimedop (2).
894 .IP *
895 .BR sigtimedwait (2),
896 .BR sigwaitinfo (2).
897 .IP *
898 Linux 3.7 and earlier:
899 .BR read (2)
900 from an
901 .BR inotify (7)
902 file descriptor
903 .\" commit 1ca39ab9d21ac93f94b9e3eb364ea9a5cf2aba06
904 .IP *
905 Linux 2.6.21 and earlier:
906 .BR futex (2)
907 .BR FUTEX_WAIT ,
908 .BR sem_timedwait (3),
909 .BR sem_wait (3).
910 .IP *
911 Linux 2.6.8 and earlier:
912 .BR msgrcv (2),
913 .BR msgsnd (2).
914 .IP *
915 Linux 2.4 and earlier:
916 .BR nanosleep (2).
917 .SH STANDARDS
918 POSIX.1, except as noted.
919 .SH NOTES
920 For a discussion of async-signal-safe functions, see
921 .BR signal\-safety (7).
922 .PP
923 The
924 .I /proc/[pid]/task/[tid]/status
925 file contains various fields that show the signals
926 that a thread is blocking
927 .RI ( SigBlk ),
928 catching
929 .RI ( SigCgt ),
930 or ignoring
931 .RI ( SigIgn ).
932 (The set of signals that are caught or ignored will be the same
933 across all threads in a process.)
934 Other fields show the set of pending signals that are directed to the thread
935 .RI ( SigPnd )
936 as well as the set of pending signals that are directed
937 to the process as a whole
938 .RI ( ShdPnd ).
939 The corresponding fields in
940 .I /proc/[pid]/status
941 show the information for the main thread.
942 See
943 .BR proc (5)
944 for further details.
945 .SH BUGS
946 There are six signals that can be delivered
947 as a consequence of a hardware exception:
948 .BR SIGBUS ,
949 .BR SIGEMT ,
950 .BR SIGFPE ,
951 .BR SIGILL ,
952 .BR SIGSEGV ,
953 and
954 .BR SIGTRAP .
955 Which of these signals is delivered,
956 for any given hardware exception,
957 is not documented and does not always make sense.
958 .PP
959 For example, an invalid memory access that causes delivery of
960 .B SIGSEGV
961 on one CPU architecture may cause delivery of
962 .B SIGBUS
963 on another architecture, or vice versa.
964 .PP
965 For another example, using the x86
966 .I int
967 instruction with a forbidden argument
968 (any number other than 3 or 128)
969 causes delivery of
970 .BR SIGSEGV ,
971 even though
972 .B SIGILL
973 would make more sense,
974 because of how the CPU reports the forbidden operation to the kernel.
975 .SH SEE ALSO
976 .BR kill (1),
977 .BR clone (2),
978 .BR getrlimit (2),
979 .BR kill (2),
980 .BR pidfd_send_signal (2),
981 .BR restart_syscall (2),
982 .BR rt_sigqueueinfo (2),
983 .BR setitimer (2),
984 .BR setrlimit (2),
985 .BR sgetmask (2),
986 .BR sigaction (2),
987 .BR sigaltstack (2),
988 .BR signal (2),
989 .BR signalfd (2),
990 .BR sigpending (2),
991 .BR sigprocmask (2),
992 .BR sigreturn (2),
993 .BR sigsuspend (2),
994 .BR sigwaitinfo (2),
995 .BR abort (3),
996 .BR bsd_signal (3),
997 .BR killpg (3),
998 .BR longjmp (3),
999 .BR pthread_sigqueue (3),
1000 .BR raise (3),
1001 .BR sigqueue (3),
1002 .BR sigset (3),
1003 .BR sigsetops (3),
1004 .BR sigvec (3),
1005 .BR sigwait (3),
1006 .BR strsignal (3),
1007 .BR swapcontext (3),
1008 .BR sysv_signal (3),
1009 .BR core (5),
1010 .BR proc (5),
1011 .BR nptl (7),
1012 .BR pthreads (7),
1013 .BR sigevent (7)