xref: /openbsd-src/lib/libc/gen/signal.3 (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1.\"	$OpenBSD: signal.3,v 1.34 2007/05/31 19:19:28 jmc 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 31 2007 $
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" Xr 	abort 3
106call (formerly
107.Dv SIGIOT )
108.It Dv SIGEMT No "	create core image" "	emulate instruction executed"
109.It Dv SIGFPE No "	create core image" "	floating-point exception"
110.It Dv SIGKILL No "	terminate process" "	kill program"
111.It Dv SIGBUS No "	create core image" "	bus error"
112.It Dv SIGSEGV No "	create core image" "	segmentation violation"
113.It Dv SIGSYS No "	create core image" "	system call given invalid argument"
114.It Dv SIGPIPE No "	terminate process" "	write on a pipe with no reader"
115.It Dv SIGALRM No "	terminate process" "	real-time timer expired"
116.It Dv SIGTERM No "	terminate process" "	software termination signal"
117.It Dv SIGURG No "	discard signal" "	urgent condition present on socket"
118.It Dv SIGSTOP No "	stop process" "	stop (cannot be caught or ignored)"
119.It Dv SIGTSTP No "	stop process" "	stop signal generated from keyboard"
120.It Dv SIGCONT No "	discard signal" "	continue after stop"
121.It Dv SIGCHLD No "	discard signal" "	child status has changed"
122.It Dv SIGTTIN No "	stop process" "	background read attempted from control terminal"
123.It Dv SIGTTOU No "	stop process" "	background write attempted to control terminal"
124.It Dv SIGIO No "	discard signal" Tn "	I/O"
125is possible on a descriptor (see
126.Xr fcntl 2 )
127.It Dv SIGXCPU No "	terminate process" "	CPU time limit exceeded (see"
128.Xr setrlimit 2 )
129.It Dv SIGXFSZ No "	terminate process" "	file size limit exceeded (see"
130.Xr setrlimit 2 )
131.It Dv SIGVTALRM No "	terminate process" "	virtual time alarm (see"
132.Xr setitimer 2 )
133.It Dv SIGPROF No "	terminate process" "	profiling timer alarm (see"
134.Xr setitimer 2 )
135.It Dv SIGWINCH No "	discard signal" "	window size change"
136.It Dv SIGINFO No "	discard signal" "	status request from keyboard"
137.It Dv SIGUSR1 No "	terminate process" "	user-defined signal 1"
138.It Dv SIGUSR2 No "	terminate process" "	user-defined signal 2"
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 may be reset to their default action by a call
234to the
235.Xr execve 2
236function;
237ignored signals remain ignored.
238.Pp
239The following functions are either reentrant or not interruptible
240by signals and are asynchronous-signal safe.
241Therefore applications may
242invoke them, without restriction, from signal-catching functions:
243.Pp
244Base Interfaces:
245.Pp
246.Fn _exit ,
247.Fn access ,
248.Fn alarm ,
249.Fn cfgetispeed ,
250.Fn cfgetospeed ,
251.Fn cfsetispeed ,
252.Fn cfsetospeed ,
253.Fn chdir ,
254.Fn chmod ,
255.Fn chown ,
256.Fn close ,
257.Fn creat ,
258.Fn dup ,
259.Fn dup2 ,
260.Fn execle ,
261.Fn execve ,
262.Fn fcntl ,
263.Fn fork ,
264.Fn fpathconf ,
265.Fn fstat ,
266.Fn fsync ,
267.Fn getegid ,
268.Fn geteuid ,
269.Fn getgid ,
270.Fn getgroups ,
271.Fn getpgrp ,
272.Fn getpid ,
273.Fn getppid ,
274.Fn getuid ,
275.Fn kill ,
276.Fn link ,
277.Fn lseek ,
278.Fn mkdir ,
279.Fn mkfifo ,
280.Fn open ,
281.Fn pathconf ,
282.Fn pause ,
283.Fn pipe ,
284.Fn raise ,
285.Fn read ,
286.Fn rename ,
287.Fn rmdir ,
288.Fn setgid ,
289.Fn setpgid ,
290.Fn setsid ,
291.Fn setuid ,
292.Fn sigaction ,
293.Fn sigaddset ,
294.Fn sigdelset ,
295.Fn sigemptyset ,
296.Fn sigfillset ,
297.Fn sigismember ,
298.Fn signal ,
299.Fn sigpending ,
300.Fn sigprocmask ,
301.Fn sigsuspend ,
302.Fn sleep ,
303.Fn stat ,
304.Fn sysconf ,
305.Fn tcdrain ,
306.Fn tcflow ,
307.Fn tcflush ,
308.Fn tcgetattr ,
309.Fn tcgetpgrp ,
310.Fn tcsendbreak ,
311.Fn tcsetattr ,
312.Fn tcsetpgrp ,
313.Fn time ,
314.Fn times ,
315.Fn umask ,
316.Fn uname ,
317.Fn unlink ,
318.Fn utime ,
319.Fn wait ,
320.Fn waitpid ,
321.Fn write .
322.Pp
323Realtime Interfaces:
324.Pp
325.Fn aio_error ,
326.Fn clock_gettime ,
327.Fn sigpause ,
328.Fn timer_getoverrun ,
329.Fn aio_return ,
330.Fn fdatasync ,
331.Fn sigqueue ,
332.Fn timer_gettime ,
333.Fn aio_suspend ,
334.Fn sem_post ,
335.Fn sigset ,
336.Fn timer_settime .
337.Pp
338ANSI C Interfaces:
339.Pp
340.Fn strcpy ,
341.Fn strcat ,
342.Fn strncpy ,
343.Fn strncat ,
344and perhaps some others.
345.Pp
346Extension Interfaces:
347.Pp
348.Fn strlcpy ,
349.Fn strlcat .
350.Pp
351Most functions not in the above lists are considered to be unsafe
352with respect to signals.
353That is to say, the behaviour of such functions when called from a
354signal handler is undefined.
355In general though, signal handlers should do little more than set a
356flag; most other actions are not safe.
357.Pp
358Additionally, inside the signal handler it is also considered more safe to
359make a copy of the global variable
360.Va errno
361and restore it before returning from the signal handler.
362.Pp
363A few other functions are signal race safe in
364.Ox
365but probably not on other systems:
366.Pp
367.Bl -tag -offset indent -compact -width foofoofoofoo
368.It Fn snprintf
369Safe.
370.It Fn vsnprintf
371Safe.
372.It Fn syslog_r
373Safe if the
374.Va syslog_data
375struct is initialized as a local variable.
376.El
377.Sh RETURN VALUES
378The previous action is returned on a successful call.
379Otherwise,
380.Dv SIG_ERR
381is returned and the global variable
382.Va errno
383is set to indicate the error.
384.Sh ERRORS
385.Fn signal
386will fail and no action will take place if one of the
387following occurs:
388.Bl -tag -width Er
389.It Bq Er EINVAL
390A specified signal
391is not a valid signal number.
392.It Bq Er EINVAL
393An attempt is made to ignore or supply a handler for
394.Dv SIGKILL
395or
396.Dv SIGSTOP .
397.El
398.Sh SEE ALSO
399.Xr kill 1 ,
400.Xr kill 2 ,
401.Xr ptrace 2 ,
402.Xr sigaction 2 ,
403.Xr sigaltstack 2 ,
404.Xr sigprocmask 2 ,
405.Xr sigsuspend 2 ,
406.Xr setjmp 3 ,
407.Xr siginterrupt 3 ,
408.Xr tty 4
409.Sh HISTORY
410This
411.Fn signal
412facility appeared in
413.Bx 4.0 .
414