xref: /openbsd/lib/libc/gen/signal.3 (revision 3d8817e4)
1.\"	$OpenBSD: signal.3,v 1.36 2010/02/25 17:10:34 schwarze 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: February 25 2010 $
31.Dt SIGNAL 3
32.Os
33.Sh NAME
34.Nm signal
35.Nd simplified software signal facilities
36.Sh SYNOPSIS
37.Fd #include <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.Aq Pa signal.h :
98.Bl -column SIGVTALA "create core imag"
99.It Sy "Name	Default Action	Description"
100.It Dv SIGHUP No "	terminate process" "	terminal line hangup"
101.It Dv SIGINT No "	terminate process" "	interrupt program"
102.It Dv SIGQUIT No "	create core image" "	quit program"
103.It Dv SIGILL No "	create core image" "	illegal instruction"
104.It Dv SIGTRAP No "	create core image" "	trace trap"
105.It Dv SIGABRT No "	create core image" "	abort(3) call (formerly SIGIOT)"
106.It Dv SIGEMT No "	create core image" "	emulate instruction executed"
107.It Dv SIGFPE No "	create core image" "	floating-point exception"
108.It Dv SIGKILL No "	terminate process" "	kill program"
109.It Dv SIGBUS No "	create core image" "	bus error"
110.It Dv SIGSEGV No "	create core image" "	segmentation violation"
111.It Dv SIGSYS No "	create core image" "	system call given invalid argument"
112.It Dv SIGPIPE No "	terminate process" "	write on a pipe with no reader"
113.It Dv SIGALRM No "	terminate process" "	real-time timer expired"
114.It Dv SIGTERM No "	terminate process" "	software termination signal"
115.It Dv SIGURG No "	discard signal" "	urgent condition present on socket"
116.It Dv SIGSTOP No "	stop process" "	stop (cannot be caught or ignored)"
117.It Dv SIGTSTP No "	stop process" "	stop signal generated from keyboard"
118.It Dv SIGCONT No "	discard signal" "	continue after stop"
119.It Dv SIGCHLD No "	discard signal" "	child status has changed"
120.It Dv SIGTTIN No "	stop process" "	background read attempted from control terminal"
121.It Dv SIGTTOU No "	stop process" "	background write attempted to control terminal"
122.It Dv SIGIO No "	discard signal" Tn "	I/O"
123is possible on a descriptor (see
124.Xr fcntl 2 )
125.It Dv SIGXCPU No "	terminate process" "	CPU time limit exceeded (see"
126.Xr setrlimit 2 )
127.It Dv SIGXFSZ No "	terminate process" "	file size limit exceeded (see"
128.Xr setrlimit 2 )
129.It Dv SIGVTALRM No "	terminate process" "	virtual time alarm (see"
130.Xr setitimer 2 )
131.It Dv SIGPROF No "	terminate process" "	profiling timer alarm (see"
132.Xr setitimer 2 )
133.It Dv SIGWINCH No "	discard signal" "	window size change"
134.It Dv SIGINFO No "	discard signal" "	status request from keyboard"
135.It Dv SIGUSR1 No "	terminate process" "	user-defined signal 1"
136.It Dv SIGUSR2 No "	terminate process" "	user-defined signal 2"
137.It Dv SIGTHR No "	discard signal" "	thread AST"
138.El
139.Pp
140The
141.Fa func
142argument is a function to be called as the action upon receipt of the signal
143.Fa sigcatch .
144The function will be called with one argument,
145.Fa sigraised ,
146which is the signal raised (thus the same function,
147.Fa func ,
148can be used by more than one signal).
149To set the default action of the signal to occur as listed above,
150.Fa func
151should be
152.Dv SIG_DFL .
153A
154.Dv SIG_DFL
155resets the default action.
156To ignore the signal,
157.Fa func
158should be
159.Dv SIG_IGN .
160This will cause subsequent instances of the signal to be ignored
161and pending instances to be discarded.
162If
163.Dv SIG_IGN
164is not used,
165further occurrences of the signal are
166automatically blocked and
167.Fa func
168is called.
169.Pp
170If the
171.Fa func
172is set to
173.Dv SIG_IGN
174for the
175.Dv SIGCHLD
176signal, the system will not create zombie processes when children of
177the calling process exit.
178If the calling process subsequently issues a
179.Xr wait 2
180(or equivalent), it blocks until all of the calling process's child
181processes terminate, and then returns a value of \-1 with
182.Va errno
183set to
184.Dv ECHILD .
185.Bf -symbolic
186This differs from historical
187.Bx
188behavior but is consistent with
189.At V
190as well as the
191.St -xpg4.2 .
192.Ef
193.Pp
194The handled signal is unblocked when
195.Fa func
196returns and
197the process continues from where it left off when the signal occurred.
198.Bf -symbolic
199Unlike previous signal facilities, the handler
200func() remains installed after a signal has been delivered.
201.Ef
202.Pp
203For some system calls, if a signal is caught while the call is
204executing and the call is prematurely terminated,
205the call is automatically restarted.
206(The handler is installed using the
207.Dv SA_RESTART
208flag with
209.Xr sigaction 2 . )
210The affected system calls include
211.Xr read 2 ,
212.Xr write 2 ,
213.Xr sendto 2 ,
214.Xr recvfrom 2 ,
215.Xr sendmsg 2 ,
216and
217.Xr recvmsg 2
218on a communications channel or a low-speed device
219and during a
220.Xr ioctl 2
221or
222.Xr wait 2 .
223However, calls that have already committed are not restarted,
224but instead return a partial success (for example, a short read count).
225The
226.Xr siginterrupt 3
227function can be used to change the system call restart behavior for
228a specific signal.
229.Pp
230When a process which has installed signal handlers forks,
231the child process inherits the signals.
232All caught signals may be reset to their default action by a call
233to the
234.Xr execve 2
235function;
236ignored signals remain ignored.
237.Pp
238The following functions are either reentrant or not interruptible
239by signals and are async-signal safe.
240Therefore applications may
241invoke them, without restriction, from signal-catching functions:
242.Pp
243Base Interfaces:
244.Pp
245.Fn _Exit ,
246.Fn _exit ,
247.\" SUSv7 says abort() is safe, but since it flushes stdio buffers,
248.\" that's not practical
249.Fn accept ,
250.Fn access ,
251.Fn alarm ,
252.Fn bind ,
253.Fn cfgetispeed ,
254.Fn cfgetospeed ,
255.Fn cfsetispeed ,
256.Fn cfsetospeed ,
257.Fn chdir ,
258.Fn chmod ,
259.Fn chown ,
260.Fn clock_gettime ,
261.Fn close ,
262.Fn connect ,
263.Fn creat ,
264.Fn dup ,
265.Fn dup2 ,
266.Fn execl ,
267.Fn execle ,
268.Fn execv ,
269.Fn execve ,
270.Fn fchdir ,
271.Fn fchmod ,
272.Fn fchown ,
273.Fn fcntl ,
274.Fn fork ,
275.Fn fpathconf ,
276.Fn fstat ,
277.Fn fsync ,
278.Fn ftruncate ,
279.Fn futimes ,
280.Fn getegid ,
281.Fn geteuid ,
282.Fn getgid ,
283.Fn getgroups ,
284.Fn getpeername ,
285.Fn getpgrp ,
286.Fn getpid ,
287.Fn getppid ,
288.Fn getsockname ,
289.Fn getsockopt ,
290.Fn getuid ,
291.Fn kill ,
292.Fn link ,
293.Fn listen ,
294.Fn lseek ,
295.Fn lstate ,
296.Fn mkdir ,
297.Fn mkfifo ,
298.Fn mknod ,
299.Fn open ,
300.Fn pathconf ,
301.Fn pause ,
302.Fn pipe ,
303.Fn poll ,
304.Fn raise ,
305.Fn read ,
306.Fn readlink ,
307.Fn recv ,
308.Fn recvfrom ,
309.Fn recvmsg ,
310.Fn rename ,
311.Fn rmdir ,
312.Fn select ,
313.Fn send ,
314.Fn sendmsg ,
315.Fn sendto ,
316.Fn setgid ,
317.Fn setpgid ,
318.Fn setsid ,
319.Fn setsockopt ,
320.Fn setuid ,
321.Fn shutdown ,
322.Fn sigaction ,
323.Fn sigaddset ,
324.Fn sigdelset ,
325.Fn sigemptyset ,
326.Fn sigfillset  ,
327.Fn sigismember ,
328.Fn signal ,
329.Fn sigpause ,
330.Fn sigpending ,
331.Fn sigprocmask ,
332.Fn sigsuspend ,
333.Fn sleep ,
334.Fn socket ,
335.Fn socketpair ,
336.Fn stat ,
337.Fn symlink ,
338.Fn sysconf ,
339.Fn tcdrain ,
340.Fn tcflow ,
341.Fn tcflush ,
342.Fn tcgetattr ,
343.Fn tcgetpgrp ,
344.Fn tcsendbreak ,
345.Fn tcsetattr ,
346.Fn tcsetpgrp ,
347.Fn time ,
348.Fn times ,
349.Fn umask ,
350.Fn uname ,
351.Fn unlink ,
352.Fn utime ,
353.Fn utimes ,
354.Fn wait ,
355.Fn waitpid ,
356.Fn write .
357.Pp
358.\" unimplemented functions that should be async-sig-safe, if we had them
359.\" SUSv[56] additions
360.\" .Fn pselect ,
361.\" .Fn sockatmark .
362.\"
363.\" SUSv7 additions
364.\" .Pp
365.\" .Fn faccessat ,
366.\" .Fn fchmodat ,
367.\" .Fn fchownat ,
368.\" .Fn fexecve ,
369.\" .Fn fstatat ,
370.\" .Fn futimens ,
371.\" .Fn linkat ,
372.\" .Fn mkdirat ,
373.\" .Fn mkfifoat ,
374.\" .Fn mknodat ,
375.\" .Fn openat ,
376.\" .Fn readlinkat ,
377.\" .Fn renameat ,
378.\" .Fn symlinkat ,
379.\" .Fn unlinkat ,
380.\" .Fn utimensat .
381.\"
382.\" Realtime Interfaces:
383.\" .Pp
384.\" .Fn aio_error ,
385.\" .Fn aio_return ,
386.\" .Fn aio_suspend ,
387.\" .Fn fdatasync ,
388.\" .Fn sem_post ,
389.\" .Fn sigqueue ,
390.\" .Fn timer_getoverrun ,
391.\" .Fn timer_gettime ,
392.\" .Fn timer_settime .
393ANSI C Interfaces:
394.Pp
395.Fn strcat ,
396.Fn strcpy ,
397.Fn strncat ,
398.Fn strncpy ,
399and perhaps some others.
400.Pp
401Extension Interfaces:
402.Pp
403.Fn chflags ,
404.Fn fchflags ,
405.Fn getresgid ,
406.Fn getresuid ,
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 snprintf
447Safe.
448.It Fn vsnprintf
449Safe.
450.It Fn syslog_r
451Safe if the
452.Va syslog_data
453struct is initialized as a local variable.
454.El
455.Sh RETURN VALUES
456The previous action is returned on a successful call.
457Otherwise,
458.Dv SIG_ERR
459is returned and the global variable
460.Va errno
461is set to indicate the error.
462.Sh ERRORS
463.Fn signal
464will fail and no action will take place if one of the
465following occurs:
466.Bl -tag -width Er
467.It Bq Er EINVAL
468A specified signal
469is not a valid signal number.
470.It Bq Er EINVAL
471An attempt is made to ignore or supply a handler for
472.Dv SIGKILL
473or
474.Dv SIGSTOP .
475.El
476.Sh SEE ALSO
477.Xr kill 1 ,
478.Xr kill 2 ,
479.Xr ptrace 2 ,
480.Xr sigaction 2 ,
481.Xr sigaltstack 2 ,
482.Xr sigprocmask 2 ,
483.Xr sigsuspend 2 ,
484.Xr setjmp 3 ,
485.Xr siginterrupt 3 ,
486.Xr tty 4
487.Sh HISTORY
488This
489.Fn signal
490facility appeared in
491.Bx 4.0 .
492