xref: /openbsd-src/lib/libc/gen/signal.3 (revision a6de02d933e60b90e67ad028a1a47750d7003852)
1.\"	$OpenBSD: signal.3,v 1.59 2024/07/14 03:47:44 jsg Exp $
2.\"
3.\" Copyright (c) 1980, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: July 14 2024 $
31.Dt SIGNAL 3
32.Os
33.Sh NAME
34.Nm signal ,
35.Nm bsd_signal
36.Nd simplified software signal facilities
37.Sh SYNOPSIS
38.In signal.h
39.Ft void
40.Fo "(*signal(int sigcatch, void (*func)(int sigraised)))"
41.Fa int
42.Fc
43.Ft void
44.Fo "(*bsd_signal(int sigcatch, void (*func)(int sigraised)))"
45.Fa int
46.Fc
47.Sh DESCRIPTION
48The
49.Fn signal
50and
51.Fn bsd_signal
52facilities are simplified interfaces to the more general
53.Xr sigaction 2
54facility.
55The
56.Fn bsd_signal
57interface is provided for source compatibility only.
58It is mainly used on systems where the standard
59.Fn signal
60does not have
61.Bx
62semantics.
63On
64.Ox
65the two interfaces are identical.
66.Pp
67Signals allow the manipulation of a process from outside its
68domain as well as allowing the process to manipulate itself or
69copies of itself (children).
70There are two general types of signals:
71those that cause termination of a process and those that do not.
72Signals which cause termination of a program might result from
73an irrecoverable error or might be the result of a user at a terminal
74typing the
75.Dq interrupt
76character.
77.Pp
78Signals are used when a process is stopped because it wishes to access
79its controlling terminal while in the background (see
80.Xr tty 4 ) .
81Signals are optionally generated
82when a process resumes after being stopped,
83when the status of child processes changes,
84or when input is ready at the controlling terminal.
85Most signals result in the termination of the process receiving them
86if no action
87is taken; some signals instead cause the process receiving them
88to be stopped, or are simply discarded if the process has not
89requested otherwise.
90.Pp
91Except for the
92.Dv SIGKILL
93and
94.Dv SIGSTOP
95signals, the
96.Fn signal
97function allows for any signal to be caught, to be ignored, or to generate
98an interrupt.
99These signals are defined in the file
100.In signal.h :
101.Bl -column "SIGVTALRM" "create core image" "terminal line hangup"
102.It Sy "Name" Ta Sy "Default Action" Ta Sy "Description"
103.It Dv SIGHUP Ta "terminate process" Ta "terminal line hangup"
104.It Dv SIGINT Ta "terminate process" Ta "interrupt program"
105.It Dv SIGQUIT Ta "create core image" Ta "quit program"
106.It Dv SIGILL Ta "create core image" Ta "illegal instruction"
107.It Dv SIGTRAP Ta "create core image" Ta "trace trap"
108.It Dv SIGABRT Ta "create core image" Ta "abort(3) call (formerly SIGIOT)"
109.It Dv SIGEMT Ta "create core image" Ta "emulate instruction executed"
110.It Dv SIGFPE Ta "create core image" Ta "floating-point exception"
111.It Dv SIGKILL Ta "terminate process" Ta "kill program (cannot be caught or ignored)"
112.It Dv SIGBUS Ta "create core image" Ta "bus error"
113.It Dv SIGSEGV Ta "create core image" Ta "segmentation violation"
114.It Dv SIGSYS Ta "create core image" Ta "system call given invalid argument"
115.It Dv SIGPIPE Ta "terminate process" Ta "write on a pipe with no reader"
116.It Dv SIGALRM Ta "terminate process" Ta "real-time timer expired"
117.It Dv SIGTERM Ta "terminate process" Ta "software termination signal"
118.It Dv SIGURG Ta "discard signal" Ta "urgent condition present on socket"
119.It Dv SIGSTOP Ta "stop process" Ta "stop (cannot be caught or ignored)"
120.It Dv SIGTSTP Ta "stop process" Ta "stop signal generated from keyboard"
121.It Dv SIGCONT Ta "discard signal" Ta "continue after stop"
122.It Dv SIGCHLD Ta "discard signal" Ta "child status has changed"
123.It Dv SIGTTIN Ta "stop process" Ta "background read attempted from controlling terminal"
124.It Dv SIGTTOU Ta "stop process" Ta "background write attempted to controlling terminal"
125.It Dv SIGIO Ta "discard signal" Ta "I/O is possible on a descriptor (see"
126.Xr fcntl 2 )
127.It Dv SIGXCPU Ta "terminate process" Ta "CPU time limit exceeded (see"
128.Xr setrlimit 2 )
129.It Dv SIGXFSZ Ta "terminate process" Ta "file size limit exceeded (see"
130.Xr setrlimit 2 )
131.It Dv SIGVTALRM Ta "terminate process" Ta "virtual time alarm (see"
132.Xr setitimer 2 )
133.It Dv SIGPROF Ta "terminate process" Ta "profiling timer alarm (see"
134.Xr setitimer 2 )
135.It Dv SIGWINCH Ta "discard signal" Ta "window size change"
136.It Dv SIGINFO Ta "discard signal" Ta "status request from keyboard"
137.It Dv SIGUSR1 Ta "terminate process" Ta "user-defined signal 1"
138.It Dv SIGUSR2 Ta "terminate process" Ta "user-defined signal 2"
139.It Dv SIGTHR Ta "discard signal" Ta "thread AST"
140.El
141.Pp
142The
143.Fa func
144argument is a function to be called as the action upon receipt of the signal
145.Fa sigcatch .
146The function will be called with one argument,
147.Fa sigraised ,
148which is the signal raised (thus the same function,
149.Fa func ,
150can be used by more than one signal).
151To set the default action of the signal to occur as listed above,
152.Fa func
153should be
154.Dv SIG_DFL .
155A
156.Dv SIG_DFL
157resets the default action.
158To ignore the signal,
159.Fa func
160should be
161.Dv SIG_IGN .
162This will cause subsequent instances of the signal to be ignored
163and pending instances to be discarded.
164If
165.Dv SIG_IGN
166is not used,
167further occurrences of the signal are
168automatically blocked and
169.Fa func
170is called.
171.Pp
172If the
173.Fa func
174is set to
175.Dv SIG_IGN
176for the
177.Dv SIGCHLD
178signal, the system will not create zombie processes when children of
179the calling process exit.
180If the calling process subsequently issues a
181.Xr wait 2
182(or equivalent), it blocks until all of the calling process's child
183processes terminate, and then returns a value of \-1 with
184.Va errno
185set to
186.Dv ECHILD .
187.Bf -symbolic
188This differs from historical
189.Bx
190behavior but is consistent with
191.At V
192as well as the
193.St -xpg4.2 .
194.Ef
195.Pp
196The handled signal is unblocked when
197.Fa func
198returns and
199the process continues from where it left off when the signal occurred.
200.Bf -symbolic
201Unlike previous signal facilities, the handler
202func() remains installed after a signal has been delivered.
203.Ef
204.Pp
205For some system calls, if a signal is caught while the call is
206executing and the call is prematurely terminated,
207the call is automatically restarted.
208(The handler is installed using the
209.Dv SA_RESTART
210flag with
211.Xr sigaction 2 . )
212The affected system calls include
213.Xr read 2 ,
214.Xr write 2 ,
215.Xr sendto 2 ,
216.Xr recvfrom 2 ,
217.Xr sendmsg 2 ,
218and
219.Xr recvmsg 2
220on a communications channel or a low-speed device
221and during a
222.Xr ioctl 2
223or
224.Xr wait 2 .
225However, calls that have already committed are not restarted,
226but instead return a partial success (for example, a short read count).
227The
228.Xr siginterrupt 3
229function can be used to change the system call restart behavior for
230a specific signal.
231.Pp
232When a process which has installed signal handlers forks,
233the child process inherits the signals.
234All caught signals, as well as
235.Dv SIGCHLD ,
236are reset to their default action by a call
237to the
238.Xr execve 2
239function;
240other
241ignored signals remain ignored.
242.Pp
243Signal handlers should be as minimal as possible, and use only signal-safe
244operations.
245The safest handlers only change a single variable of type
246.Va volatile sig_atomic_t ,
247which is inspected by an event loop.
248Other variables accessed inside the handler must be either const, or
249local to the handler.
250More complicated global variables (such as strings, structs, or lists)
251will require external methods to guarantee consistency, such as
252signal-blocking with
253.Xr sigprocmask 2 .
254.Pp
255More complicated handlers must restrict themselves to calling only the following
256list of signal-safe functions directly.
257Avoid abstracting the work to helper functions which are also called from
258other contexts because future coders will forget the signal-safe requirement.
259.Pp
260Standard Interfaces:
261.Pp
262.Fn _exit ,
263.Fn _Exit ,
264.Fn abort ,
265.Fn accept ,
266.Fn access ,
267.Fn alarm ,
268.Fn bind ,
269.Fn cfgetispeed ,
270.Fn cfgetospeed ,
271.Fn cfsetispeed ,
272.Fn cfsetospeed ,
273.Fn chdir ,
274.Fn chmod ,
275.Fn chown ,
276.Fn clock_gettime ,
277.Fn close ,
278.Fn connect ,
279.Fn creat ,
280.Fn dup ,
281.Fn dup2 ,
282.Fn execl ,
283.Fn execle ,
284.Fn execv ,
285.Fn execve ,
286.Fn faccessat ,
287.Fn fchdir ,
288.Fn fchmod ,
289.Fn fchmodat ,
290.Fn fchown ,
291.Fn fchownat ,
292.Fn fcntl ,
293.Fn fdatasync ,
294.Fn fork ,
295.Fn fpathconf ,
296.Fn fstat ,
297.Fn fstatat ,
298.Fn fsync ,
299.Fn ftruncate ,
300.Fn futimens ,
301.Fn futimes ,
302.Fn getegid ,
303.Fn geteuid ,
304.Fn getgid ,
305.Fn getgroups ,
306.Fn getpeername ,
307.Fn getpgrp ,
308.Fn getpid ,
309.Fn getppid ,
310.Fn getsockname ,
311.Fn getsockopt ,
312.Fn getuid ,
313.Fn kill ,
314.Fn link ,
315.Fn linkat ,
316.Fn listen ,
317.Fn lseek ,
318.Fn lstat ,
319.Fn mkdir ,
320.Fn mkdirat ,
321.Fn mkfifo ,
322.Fn mkfifoat ,
323.Fn mknod ,
324.Fn mknodat ,
325.Fn open ,
326.Fn openat ,
327.Fn pathconf ,
328.Fn pause ,
329.Fn pipe ,
330.Fn poll ,
331.Fn pselect ,
332.Fn pthread_sigmask ,
333.Fn raise ,
334.Fn read ,
335.Fn readlink ,
336.Fn readlinkat ,
337.Fn recv ,
338.Fn recvfrom ,
339.Fn recvmsg ,
340.Fn rename ,
341.Fn renameat ,
342.Fn rmdir ,
343.Fn select ,
344.Fn send ,
345.Fn sendmsg ,
346.Fn sendto ,
347.Fn setgid ,
348.Fn setpgid ,
349.Fn setsid ,
350.Fn setsockopt ,
351.Fn setuid ,
352.Fn shutdown ,
353.Fn sigaction ,
354.Fn sigaddset ,
355.Fn sigdelset ,
356.Fn sigemptyset ,
357.Fn sigfillset  ,
358.Fn sigismember ,
359.Fn signal ,
360.Fn sigpause ,
361.Fn sigpending ,
362.Fn sigprocmask ,
363.Fn sigsuspend ,
364.Fn sleep ,
365.Fn sockatmark ,
366.Fn socket ,
367.Fn socketpair ,
368.Fn stat ,
369.Fn strcat ,
370.Fn strcpy ,
371.Fn strncat ,
372.Fn strncpy ,
373.Fn symlink ,
374.Fn symlinkat ,
375.Fn sysconf ,
376.Fn tcdrain ,
377.Fn tcflow ,
378.Fn tcflush ,
379.Fn tcgetattr ,
380.Fn tcgetpgrp ,
381.Fn tcsendbreak ,
382.Fn tcsetattr ,
383.Fn tcsetpgrp ,
384.Fn time ,
385.Fn times ,
386.Fn umask ,
387.Fn uname ,
388.Fn unlink ,
389.Fn unlinkat ,
390.Fn utime ,
391.Fn utimensat ,
392.Fn utimes ,
393.Fn wait ,
394.Fn waitpid ,
395.Fn write ,
396and perhaps some others.
397.\" unimplemented functions that should be async-sig-safe, if we had them
398.\" POSIX Issue 7 additions
399.\" .Pp
400.\" .Fn fexecve .
401.\"
402.\" Realtime Interfaces:
403.\" .Pp
404.\" .Fn aio_error ,
405.\" .Fn aio_return ,
406.\" .Fn aio_suspend ,
407.\" .Fn sem_post ,
408.\" .Fn sigqueue ,
409.\" .Fn timer_getoverrun ,
410.\" .Fn timer_gettime ,
411.\" .Fn timer_settime .
412.Pp
413Extension Interfaces:
414.Pp
415.Fn accept4 ,
416.Fn chflags ,
417.Fn chflagsat ,
418.Fn dup3 ,
419.Fn fchflags ,
420.Fn getentropy ,
421.Fn getresgid ,
422.Fn getresuid ,
423.Fn pipe2 ,
424.Fn ppoll ,
425.Fn sendsyslog ,
426.Fn setresgid ,
427.Fn setresuid ,
428.Fn strlcat ,
429.Fn strlcpy ,
430.Fn wait3 ,
431.Fn wait4 .
432.Pp
433Since signal-safe functions can encounter system call errors,
434.Va errno
435should be protected inside the handler with the following pattern:
436.Bd -literal -offset indent
437void
438handler(int sig)
439{
440	int save_errno = errno;
441
442	...
443	errno = save_errno;
444}
445.Ed
446.Pp
447On
448.Ox ,
449a few more functions are signal-safe (except when the format string contains
450floating-point arguments).
451These functions are expected to be unsafe on other systems, so be very cautious of
452the portability trap!
453.Pp
454.Bl -tag -offset indent -compact -width foofoofoofoo
455.It Fn dprintf
456Safe.
457.It Fn vdprintf
458Safe.
459.It Fn snprintf
460Safe.
461.It Fn vsnprintf
462Safe.
463.It Fn syslog_r
464Safe if the
465.Va syslog_data
466struct is initialized as a local variable.
467.El
468.Sh RETURN VALUES
469The previous action is returned on a successful call.
470Otherwise,
471.Dv SIG_ERR
472is returned and the global variable
473.Va errno
474is set to indicate the error.
475.Sh ERRORS
476.Fn signal
477will fail and no action will take place if one of the
478following occurs:
479.Bl -tag -width Er
480.It Bq Er EINVAL
481A specified signal
482is not a valid signal number.
483.It Bq Er EINVAL
484An attempt is made to ignore or supply a handler for
485.Dv SIGKILL
486or
487.Dv SIGSTOP .
488.El
489.Sh SEE ALSO
490.Xr kill 1 ,
491.Xr kill 2 ,
492.Xr ptrace 2 ,
493.Xr sigaction 2 ,
494.Xr sigaltstack 2 ,
495.Xr sigprocmask 2 ,
496.Xr sigsuspend 2 ,
497.Xr setjmp 3 ,
498.Xr siginterrupt 3 ,
499.Xr tty 4
500.Sh HISTORY
501A
502.Fn signal
503system call first appeared in
504.At v4 .
505In
506.Bx 4.2 ,
507it was reimplemented as a wrapper around the former
508.Fn sigvec
509system call, and for
510.Bx 4.3 Reno ,
511it was rewritten to use
512.Xr sigaction 2
513instead.
514