SIGNAL(3POSIX)

✨ PROLOG ✨

okay bestie, real talk before we even start: this manual page is part of the POSIX Programmer's Manual. That's the vibe. But heads up πŸ’… β€” the Linux implementation of this interface may differ (so like, go consult the corresponding Linux manual page for the details of Linux behavior, iykyk), or honestly the interface may not be implemented on Linux at all. no cap.


πŸŽ€ NAME πŸŽ€

signal β€” signal management. it's giving "handle your signals, queen."


🌸 SYNOPSIS 🌸

#include <signal.h>

void (*signal(int sig, void (*func)(int)))(int);

look at that return type. she's complicated but she's worth it 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

So the functionality described on this reference page is aligned with the ISO C standard. If there's any conflict between what's written here and the ISO C standard, that conflict is unintentional β€” no drama intended, fr fr. This volume of POSIX.1-2017 defers to the ISO C standard.

The signal() function chooses one of three ways in which receipt of the signal number sig is to be subsequently handled. Pick your fighter:

  • If the value of func is SIG_DFL, default handling for that signal shall occur.
  • If the value of func is SIG_IGN, the signal shall be ignored. not me ignoring my problems πŸ’€
  • Otherwise, the application shall ensure that func points to a function to be called when that signal occurs. An invocation of such a function because of a signal, or (recursively) of any further functions called by that invocation (other than functions in the standard library), is called a "signal handler".

When a signal occurs, and func points to a function, it is implementation-defined whether the equivalent of a:

signal(sig, SIG_DFL);

is executed, or the implementation prevents some implementation-defined set of signals (at least including sig) from occurring until the current signal handling has completed. (Oh and β€” if the value of sig is SIGILL, the implementation may alternatively define that no action is taken.)

Next, the equivalent of:

(*func)(sig);

is executed. If and when the function returns, if the value of sig was SIGFPE, SIGILL, or SIGSEGV or any other implementation-defined value corresponding to a computational exception, the behavior is undefined 😭 otherwise, the program shall resume execution at the point it was interrupted.

The ISO C standard places a restriction on applications relating to the use of raise() from signal handlers. This restriction does not apply to POSIX applications, because POSIX.1-2008 requires raise() to be async-signal-safe (see Section 2.4.3, Signal Actions). slay.

Now here's the spicy part 🌢️. If the process is multi-threaded, or if the process is single-threaded and a signal handler is executed other than as the result of:

  • The process calling abort(), raise(), kill(), pthread_kill(), or sigqueue() to generate a signal that is not blocked
  • A pending signal being unblocked and being delivered before the call that unblocked it returns

then the behavior is undefined if the signal handler refers to any object other than errno with static storage duration other than by assigning a value to an object declared as volatile sig_atomic_t, or if the signal handler calls any function defined in this standard other than one of the functions listed in Section 2.4, Signal Concepts.

At program start-up, the equivalent of:

signal(sig, SIG_IGN);

is executed for some signals, and the equivalent of:

signal(sig, SIG_DFL);

is executed for all other signals (see exec).

And finally, the icon behavior: the signal() function shall not change the setting of errno if successful. we love a clean exit ✨


πŸ’œ RETURN VALUE πŸ’œ

If the request can be honored, signal() shall return the value of func for the most recent call to signal() for the specified signal sig. Otherwise, SIG_ERR shall be returned and a positive value shall be stored in errno. that's the deal, bestie.


🌈 ERRORS 🌈

The signal() function shall fail if:

  • EINVAL β€” The sig argument is not a valid signal number, or an attempt is made to catch a signal that cannot be caught, or ignore a signal that cannot be ignored.

The signal() function may fail if:

  • EINVAL β€” An attempt was made to set the action to SIG_DFL for a signal that cannot be caught or ignored (or both).

The following sections are informative.


πŸ“ EXAMPLES πŸ“

None. (yes, really. nothing. empty. the void. πŸ™ˆ)


🧚 APPLICATION USAGE 🧚

The sigaction() function provides a more comprehensive and reliable mechanism for controlling signals; new applications should use sigaction() rather than signal(). it's the glow-up, periodt πŸ‘‘


⭐ RATIONALE ⭐

None.


πŸ‘‘ FUTURE DIRECTIONS πŸ‘‘

None.


πŸ”₯ SEE ALSO πŸ”₯

  • Section 2.4, Signal Concepts
  • exec
  • pause()
  • raise()
  • sigaction()
  • sigsuspend()
  • waitid()
  • The Base Definitions volume of POSIX.1-2017, <signal.h>

Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


okay that's the whole page, you made it to the end, I'm so proud of you πŸ₯Ή this was IEEE/The Open Group, dated 2017, page SIGNAL(3POSIX) β€” and that's on periodt πŸ’…βœ¨