signal(2)

โœจ signal(2) โœจ

okay bestie, sit down, we need to talk about signal โ€” it's the ANSI C signal handling vibe check, and honestly? it's giving "legacy situationship". ๐Ÿ’…

LIBRARY: Standard C library (libc, -lc) ๐ŸŽ€


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <signal.h>

typedef typeof(void (int))  *sighandler_t;

sighandler_t signal(int signum, sighandler_t handler);

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

๐Ÿšจ WARNING: the behavior of signal() is not consistent across UNIX versions, and it's also been all over the place historically across different versions of Linux. Just don't. Avoid its use: use sigaction(2) instead. (There's a whole Portability section below, go read it, i'm begging.) ๐Ÿ˜ญ

So like, signal() sets the disposition of the signal signum to handler, and handler can be one of three things: SIG_IGN, SIG_DFL, or the address of a programmer-defined function (a "signal handler"). that's it, that's the whole job. ๐Ÿซถ

Now, if the signal signum gets delivered to the process, one of these three things happens:

  • if the disposition is set to SIG_IGN, the signal is ignored. bye, don't care, not my problem. ๐Ÿ™ˆ
  • if the disposition is set to SIG_DFL, the default action associated with the signal (peep signal(7)) occurs.
  • if the disposition is set to a function, then first either the disposition gets reset to SIG_DFL, or the signal gets blocked (check the Portability section below for the tea โ˜•), and then handler is called with argument signum. and if invoking the handler is what caused the signal to be blocked, then the signal gets unblocked upon return from the handler. slay.

and just so we're clear: the signals SIGKILL and SIGSTOP cannot be caught or ignored. those two are the main characters and you cannot touch them, periodt. ๐Ÿ‘‘


๐Ÿ’– RETURN VALUE ๐Ÿ’–

signal() returns the previous value of the signal handler. if it fails, it returns SIG_ERR, and errno is set to indicate the error. ๐Ÿ’€


๐ŸŒˆ ERRORS ๐ŸŒˆ

  • EINVAL โ€” signum is invalid. like, you passed it something that isn't a real signal. not the move, bestie.

โญ VERSIONS โญ

The use of sighandler_t is a GNU extension, exposed if _GNU_SOURCE is defined. glibc also defines (the BSD-derived) sig_t if _BSD_SOURCE (glibc 2.19 and earlier) or _DEFAULT_SOURCE (glibc 2.19 and later) is defined. okay and the standard definition of signal() is:

typeof(void (int)) *signal(int signum, typeof(void (int)) *handler);

๐Ÿงš Portability

The only portable use of signal() is to set a signal's disposition to SIG_DFL or SIG_IGN. literally that's it. The semantics when you use signal() to establish an actual signal handler vary across systems (and POSIX.1 explicitly permits this variation), so do not use it for this purpose. i'm not kidding, it's a trap. ๐Ÿ˜ญ

POSIX.1 solved the whole portability mess by specifying sigaction(2), which gives you explicit control of the semantics when a signal handler is invoked. use that interface instead of signal(). do it for me. ๐Ÿซถ


๐ŸŽ€ STANDARDS ๐ŸŽ€

C11, POSIX.1-2024.


๐Ÿ‘‘ HISTORY ๐Ÿ‘‘

C89, POSIX.1-2001.

okay story time, because this one's actually iconic in a messy way. ๐Ÿ“–

In the original UNIX systems, when a handler that was established using signal() got invoked by the delivery of a signal, the disposition of the signal would be reset to SIG_DFL, and the system did not block delivery of further instances of the signal. that's equivalent to calling sigaction(2) with these flags:

sa.sa_flags = SA_RESETHAND | SA_NODEFER;

System V also provides these semantics for signal(). and this was bad โ€” like, genuinely bad โ€” because the signal might get delivered again before the handler even had a chance to reestablish itself. and on top of that, rapid deliveries of the same signal could result in recursive invocations of the handler. chaos. recursive chaos. ๐Ÿ’€

BSD improved on this situation, but unfortunately also changed the semantics of the existing signal() interface while doing so (not them doing a stealth edit ๐Ÿ˜ญ). On BSD, when a signal handler is invoked, the signal disposition is not reset, and further instances of the signal are blocked from being delivered while the handler is executing. furthermore, certain blocking system calls are automatically restarted if interrupted by a signal handler (peep signal(7)). The BSD semantics are equivalent to calling sigaction(2) with these flags:

sa.sa_flags = SA_RESTART;

and the situation on Linux is as follows:

  • The kernel's signal() system call provides System V semantics.
  • By default, in glibc 2 and later, the signal() wrapper function does not invoke the kernel system call. Instead, it calls sigaction(2) using flags that supply BSD semantics. This default behavior is provided as long as a suitable feature test macro is defined: _BSD_SOURCE on glibc 2.19 and earlier, or _DEFAULT_SOURCE in glibc 2.19 and later. (By default, these macros are defined; see feature_test_macros(7) for details.) If such a feature test macro is not defined, then signal() provides System V semantics.

so yeah, which semantics you get literally depends on your feature test macros. lowkey unhinged but okay. ๐Ÿฆ‹


๐Ÿ’œ NOTES ๐Ÿ’œ

The effects of signal() in a multithreaded process are unspecified. unspecified. we simply do not know. ๐Ÿ™ˆ

According to POSIX, the behavior of a process is undefined after it ignores a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by kill(2) or raise(3). and like, integer division by zero has an undefined result. on some architectures it will generate a SIGFPE signal. (also, dividing the most negative integer by -1 may generate SIGFPE.) ignoring this signal might lead to an endless loop. an endless one. you have been warned. ๐Ÿšจ

See sigaction(2) for the details on what happens when the disposition SIGCHLD is set to SIG_IGN.

See signal-safety(7) for the list of the async-signal-safe functions that can be safely called from inside a signal handler. only those ones. none of the others. iykyk. โœจ


๐ŸŒŸ SEE ALSO ๐ŸŒŸ

kill(1), alarm(2), kill(2), pause(2), sigaction(2), signalfd(2), sigpending(2), sigprocmask(2), sigsuspend(2), bsd_signal(3), killpg(3), raise(3), siginterrupt(3), sigqueue(3), sigsetops(3), sigvec(3), sysv_signal(3), signal(7)


okay that's the whole page bestie ๐Ÿ’– go use sigaction(2), drink some water, and stay async-signal-safe out there ๐Ÿซถโœจ

this lil masterpiece came from Linux man-pages 6.20, dated 2026-02-08 โ€” the signal(2) page itself. mwah. ๐Ÿ’…๐ŸŽ€

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms