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