SIGNAL(3)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1

πŸ’– SIGNAL(3) β€” simplified software signal facilities πŸ’–

okay bestie, welcome to signal(3), the ✨ Standard C Library ✨ (libc, -lc) era of signal handling. this is literally the manual page for the simplified software signal facilities and i'm gonna walk you through it, no cap 🌸


✨ SYNOPSIS ✨

so first up, the vibe of what you're actually calling:

#include <signal.h>

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

yes bestie that's a function returning a pointer to a function, it's giving "i survived the declarator" energy πŸ’€

or, in FreeBSD's equivalent but easier to read typedef'd version (bless):

typedef void (*sig_t) (int);

sig_t
signal(int sig, sig_t func);

πŸ¦‹ DESCRIPTION πŸ¦‹

this signal() facility is a simplified interface to the more general sigaction(2) facility. like, it's the same idea but wearing a cuter outfit πŸŽ€

signals allow the manipulation of a process from outside its domain, and also let the process manipulate itself or copies of itself (children). iconic, honestly πŸ‘‘

there are two general types of signals:

  • those that cause termination of a process
  • those that don't

signals which cause termination of a program might result from an irrecoverable error, or might be the result of a user at a terminal typing the interrupt character. signals are used when a process is stopped because it wishes to access its control terminal while in the background (see tty(4)). signals are optionally generated when a process resumes after being stopped, when the status of child processes changes, or when input is ready at the control terminal.

most signals result in the termination of the process receiving them if no action is taken. some signals instead cause the process receiving them to be stopped, or are simply discarded if the process has not requested otherwise.

and here's the big one: except for the SIGKILL and SIGSTOP signals, the signal() function allows for a signal to be caught, to be ignored, or to generate an interrupt. these signals are defined in the file <signal.h> 🌈

πŸ’… the signal lineup (yes, all 33 of them)

  • 1 SIGHUP β€” default action: terminate process β€” terminal line hangup
  • 2 SIGINT β€” default action: terminate process β€” interrupt program
  • 3 SIGQUIT β€” default action: create core image β€” quit program
  • 4 SIGILL β€” default action: create core image β€” illegal instruction
  • 5 SIGTRAP β€” default action: create core image β€” trace trap
  • 6 SIGABRT β€” default action: create core image β€” abort program (formerly SIGIOT)
  • 7 SIGEMT β€” default action: create core image β€” emulate instruction executed
  • 8 SIGFPE β€” default action: create core image β€” floating-point exception
  • 9 SIGKILL β€” default action: terminate process β€” kill program
  • 10 SIGBUS β€” default action: create core image β€” bus error
  • 11 SIGSEGV β€” default action: create core image β€” segmentation violation
  • 12 SIGSYS β€” default action: create core image β€” non-existent system call invoked
  • 13 SIGPIPE β€” default action: terminate process β€” write on a pipe with no reader
  • 14 SIGALRM β€” default action: terminate process β€” real-time timer expired
  • 15 SIGTERM β€” default action: terminate process β€” software termination signal
  • 16 SIGURG β€” default action: discard signal β€” urgent condition present on socket
  • 17 SIGSTOP β€” default action: stop process β€” stop (cannot be caught or ignored)
  • 18 SIGTSTP β€” default action: stop process β€” stop signal generated from keyboard
  • 19 SIGCONT β€” default action: discard signal β€” continue after stop
  • 20 SIGCHLD β€” default action: discard signal β€” child status has changed
  • 21 SIGTTIN β€” default action: stop process β€” background read attempted from control terminal
  • 22 SIGTTOU β€” default action: stop process β€” background write attempted to control terminal
  • 23 SIGIO β€” default action: discard signal β€” I/O is possible on a descriptor (see fcntl(2))
  • 24 SIGXCPU β€” default action: terminate process β€” cpu time limit exceeded (see setrlimit(2))
  • 25 SIGXFSZ β€” default action: terminate process β€” file size limit exceeded (see setrlimit(2))
  • 26 SIGVTALRM β€” default action: terminate process β€” virtual time alarm (see setitimer(2))
  • 27 SIGPROF β€” default action: terminate process β€” profiling timer alarm (see setitimer(2))
  • 28 SIGWINCH β€” default action: discard signal β€” Window size change
  • 29 SIGINFO β€” default action: discard signal β€” status request from keyboard
  • 30 SIGUSR1 β€” default action: terminate process β€” User defined signal 1
  • 31 SIGUSR2 β€” default action: terminate process β€” User defined signal 2
  • 32 SIGTHR β€” default action: terminate process β€” thread interrupt
  • 33 SIGLIBRT β€” default action: terminate process β€” real-time library interrupt

🧚 the sig and func girlies

the sig argument specifies which signal was received. the func procedure allows a user to choose the action upon receipt of a signal. so like, you're the main character choosing your destiny πŸ’«

  • to set the default action of the signal to occur as listed above, func should be SIG_DFL. a SIG_DFL resets the default action.
  • to ignore the signal, func should be SIG_IGN. this will cause subsequent instances of the signal to be ignored and pending instances to be discarded.

if SIG_IGN is not used, further occurrences of the signal are automatically blocked and func is called.

the handled signal is unblocked when the function returns, and the process continues from where it left off when the signal occurred. and unlike previous signal facilities, the handler func() remains installed after a signal has been delivered β€” she's staying, periodt πŸ’…

🫢 the restart situation

for some system calls, if a signal is caught while the call is executing and the call is prematurely terminated, the call is automatically restarted. any handler installed with signal(3) will have the SA_RESTART flag set, meaning that any restartable system call will not return on receipt of a signal.

the affected system calls include read(2), write(2), sendto(2), recvfrom(2), sendmsg(2) and recvmsg(2) on a communications channel or a low speed device and during a ioctl(2) or wait(2).

however β€” calls that have already committed are not restarted, but instead return a partial success (for example, a short read count). these semantics could be changed with siginterrupt(3).

🌸 forks, exec, and inheriting your baggage

when a process which has installed signal handlers forks, the child process inherits the signals. all caught signals may be reset to their default action by a call to the execve(2) function; ignored signals remain ignored.

πŸ’€ the zombie situation (very on brand)

if a process explicitly specifies SIG_IGN as the action for the signal SIGCHLD, the system will not create zombie processes when children of the calling process exit. slay, undeclared.

as a consequence, the system will discard the exit status from the child processes. and if the calling process subsequently issues a call to wait(2) or equivalent, it will block until all of the calling process's children terminate, and then return a value of -1 with errno set to ECHILD. 😭

see sigaction(2) for a list of functions that are considered safe for use in signal handlers. that's the canon list bestie, don't freestyle it.


⭐ RETURN VALUES ⭐

the previous action is returned on a successful call. otherwise, SIG_ERR is returned and the global variable errno is set to indicate the error.


πŸ™ˆ ERRORS πŸ™ˆ

the signal() function will fail and no action will take place if one of the following occur:

  • [EINVAL] β€” The sig argument is not a valid signal number.
  • [EINVAL] β€” An attempt is made to ignore or supply a handler for SIGKILL or SIGSTOP.
fr fr, SIGKILL and SIGSTOP are not negotiable, they will not be caught, they will not be ignored, they are the unbothered queens of the signal world πŸ‘‘

πŸ”₯ SEE ALSO πŸ”₯

kill(1), kill(2), ptrace(2), sigaction(2), sigaltstack(2), sigprocmask(2), sigsuspend(2), wait(2), fpsetmask(3), setjmp(3), siginterrupt(3), tty(4)


πŸ‘‘ HISTORY πŸ‘‘

the signal() function appeared in Version 4 AT&T UNIX. the current signal facility appeared in 4.0BSD. the option to avoid the creation of child zombies through ignoring SIGCHLD appeared in FreeBSD 5.0. a whole glow-up timeline, honestly ✨


that's the whole page bestie, go forth and handle your signals responsibly πŸ¦‹πŸ’œ

mwah πŸ’‹ β€” this Debian page is dated December 1, 2017, and the source/version footer says Debian β€” catch you in the next manual, ily! πŸŽ€

πŸ“¦ where this page comes from
  • freebsd 15.1: base 15.1
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms