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