xref: /openbsd/lib/libc/gen/signal.3 (revision d485f761)
1.\"	$OpenBSD: signal.3,v 1.16 2001/11/07 18:46:58 deraadt 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. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.Dd April 19, 1994
35.Dt SIGNAL 3
36.Os
37.Sh NAME
38.Nm signal
39.Nd simplified software signal facilities
40.Sh SYNOPSIS
41.Fd #include <signal.h>
42.Ft void
43.\" can not do this with just .Fn yet
44.\" .br
45.\" .Po
46.Fn (*signal "int sigcatch" "void (*func)(int sigraised))) (int"
47.\" .Pc Ns \*(lp\*(rp
48.Sh DESCRIPTION
49The
50.Fn signal
51facility
52is a simplified interface to the more general
53.Xr sigaction 2
54facility.
55.Pp
56Signals allow the manipulation of a process from outside its
57domain as well as allowing the process to manipulate itself or
58copies of itself (children).
59There are two general types of signals:
60those that cause termination of a process and those that do not.
61Signals which cause termination of a program might result from
62an irrecoverable error or might be the result of a user at a terminal
63typing the
64.Dq interrupt
65character.
66.Pp
67Signals are used when a process is stopped because it wishes to access
68its control terminal while in the background (see
69.Xr tty 4 ) .
70Signals are optionally generated
71when a process resumes after being stopped,
72when the status of child processes changes,
73or when input is ready at the control terminal.
74Most signals result in the termination of the process receiving them
75if no action
76is taken; some signals instead cause the process receiving them
77to be stopped, or are simply discarded if the process has not
78requested otherwise.
79.Pp
80Except for the
81.Dv SIGKILL
82and
83.Dv SIGSTOP
84signals, the
85.Fn signal
86function allows for any signal to be caught, to be ignored, or to generate
87an interrupt.
88These signals are defined in the file
89.Aq Pa signal.h :
90.Bl -column SIGVTALARMXX "create core imagexxx"
91.It Sy "Name	Default Action	Description"
92.It Dv SIGHUP No "	terminate process" "	terminal line hangup"
93.It Dv SIGINT No "	terminate process" "	interrupt program"
94.It Dv SIGQUIT No "	create core image" "	quit program"
95.It Dv SIGILL No "	create core image" "	illegal instruction"
96.It Dv SIGTRAP No "	create core image" "	trace trap"
97.It Dv SIGABRT No "	create core image" Xr 	abort 3
98call (formerly
99.Dv SIGIOT )
100.It Dv SIGEMT No "	create core image" "	emulate instruction executed"
101.It Dv SIGFPE No "	create core image" "	floating-point exception"
102.It Dv SIGKILL No "	terminate process" "	kill program"
103.It Dv SIGBUS No "	create core image" "	bus error"
104.It Dv SIGSEGV No "	create core image" "	segmentation violation"
105.It Dv SIGSYS No "	create core image" "	system call given invalid argument"
106.It Dv SIGPIPE No "	terminate process" "	write on a pipe with no reader"
107.It Dv SIGALRM No "	terminate process" "	real-time timer expired"
108.It Dv SIGTERM No "	terminate process" "	software termination signal"
109.It Dv SIGURG No "	discard signal" "	urgent condition present on socket"
110.It Dv SIGSTOP No "	stop process" "	stop (cannot be caught or ignored)"
111.It Dv SIGTSTP No "	stop process" "	stop signal generated from keyboard"
112.It Dv SIGCONT No "	discard signal" "	continue after stop"
113.It Dv SIGCHLD No "	discard signal" "	child status has changed"
114.It Dv SIGTTIN No "	stop process" "	background read attempted from control terminal"
115.It Dv SIGTTOU No "	stop process" "	background write attempted to control terminal"
116.It Dv SIGIO No "	discard signal" Tn "	I/O"
117is possible on a descriptor (see
118.Xr fcntl 2 )
119.It Dv SIGXCPU No "	terminate process" "	cpu time limit exceeded (see"
120.Xr setrlimit 2 )
121.It Dv SIGXFSZ No "	terminate process" "	file size limit exceeded (see"
122.Xr setrlimit 2 )
123.It Dv SIGVTALRM No "	terminate process" "	virtual time alarm (see"
124.Xr setitimer 2 )
125.It Dv SIGPROF No "	terminate process" "	profiling timer alarm (see"
126.Xr setitimer 2 )
127.It Dv SIGWINCH No "	discard signal" "	window size change"
128.It Dv SIGINFO No "	discard signal" "	status request from keyboard"
129.It Dv SIGUSR1 No "	terminate process" "	user-defined signal 1"
130.It Dv SIGUSR2 No "	terminate process" "	user-defined signal 2"
131.El
132.Pp
133The
134.Fa func
135argument is a function to be called as the action upon receipt of the signal
136.Fa sigcatch .
137The function will be called with one argument,
138.Fa sigraised ,
139which is the signal raised (thus the same function,
140.Fa func ,
141can be used by more than one signal).
142To set the default action of the signal to occur as listed above,
143.Fa func
144should be
145.Dv SIG_DFL .
146A
147.Dv SIG_DFL
148resets the default action.
149To ignore the signal,
150.Fa func
151should be
152.Dv SIG_IGN .
153This will cause subsequent instances of the signal to be ignored
154and pending instances to be discarded.
155If
156.Dv SIG_IGN
157is not used,
158further occurrences of the signal are
159automatically blocked and
160.Fa func
161is called.
162.Pp
163The handled signal is unblocked when
164.Fa func
165returns and
166the process continues from where it left off when the signal occurred.
167.Bf -symbolic
168Unlike previous signal facilities, the handler
169func() remains installed after a signal has been delivered.
170.Ef
171.Pp
172For some system calls, if a signal is caught while the call is
173executing and the call is prematurely terminated,
174the call is automatically restarted.
175(The handler is installed using the
176.Dv SA_RESTART
177flag with
178.Xr sigaction 2 . )
179The affected system calls include
180.Xr read 2 ,
181.Xr write 2 ,
182.Xr sendto 2 ,
183.Xr recvfrom 2 ,
184.Xr sendmsg 2 ,
185and
186.Xr recvmsg 2
187on a communications channel or a low-speed device
188and during a
189.Xr ioctl 2
190or
191.Xr wait 2 .
192However, calls that have already committed are not restarted,
193but instead return a partial success (for example, a short read count).
194.Pp
195When a process which has installed signal handlers forks,
196the child process inherits the signals.
197All caught signals may be reset to their default action by a call
198to the
199.Xr execve 2
200function;
201ignored signals remain ignored.
202.Pp
203The following functions are either reentrant or not interruptible
204by signals and are async-signal safe.
205Therefore applications may
206invoke them, without restriction, from signal-catching functions:
207.Pp
208Base Interfaces:
209.Pp
210.Fn _exit ,
211.Fn access ,
212.Fn alarm ,
213.Fn cfgetispeed ,
214.Fn cfgetospeed ,
215.Fn cfsetispeed ,
216.Fn cfsetospeed ,
217.Fn chdir ,
218.Fn chmod ,
219.Fn chown ,
220.Fn close ,
221.Fn creat ,
222.Fn dup ,
223.Fn dup2 ,
224.Fn execle ,
225.Fn execve ,
226.Fn fcntl ,
227.Fn fork ,
228.Fn fpathconf ,
229.Fn fstat ,
230.Fn fsync ,
231.Fn getegid ,
232.Fn geteuid ,
233.Fn getgid ,
234.Fn getgroups ,
235.Fn getpgrp ,
236.Fn getpid ,
237.Fn getppid ,
238.Fn getuid ,
239.Fn kill ,
240.Fn link ,
241.Fn lseek ,
242.Fn mkdir ,
243.Fn mkfifo ,
244.Fn open ,
245.Fn pathconf ,
246.Fn pause ,
247.Fn pipe ,
248.Fn raise ,
249.Fn read ,
250.Fn rename ,
251.Fn rmdir ,
252.Fn setgid ,
253.Fn setpgid ,
254.Fn setsid ,
255.Fn setuid ,
256.Fn sigaction ,
257.Fn sigaddset ,
258.Fn sigdelset ,
259.Fn sigemptyset ,
260.Fn sigfillset  ,
261.Fn sigismember ,
262.Fn signal ,
263.Fn sigpending ,
264.Fn sigprocmask ,
265.Fn sigsuspend ,
266.Fn sleep ,
267.Fn stat ,
268.Fn sysconf ,
269.Fn tcdrain ,
270.Fn tcflow ,
271.Fn tcflush ,
272.Fn tcgetattr ,
273.Fn tcgetpgrp ,
274.Fn tcsendbreak ,
275.Fn tcsetattr ,
276.Fn tcsetpgrp ,
277.Fn time ,
278.Fn times ,
279.Fn umask ,
280.Fn uname ,
281.Fn unlink ,
282.Fn utime ,
283.Fn wait ,
284.Fn waitpid ,
285.Fn write .
286.Pp
287Realtime Interfaces:
288.Pp
289.Fn aio_error ,
290.Fn clock_gettime ,
291.Fn sigpause ,
292.Fn timer_getoverrun ,
293.Fn aio_return ,
294.Fn fdatasync ,
295.Fn sigqueue ,
296.Fn timer_gettime ,
297.Fn aio_suspend ,
298.Fn sem_post ,
299.Fn sigset ,
300.Fn timer_settime .
301.Pp
302ANSI C Interfaces:
303.Pp
304.Fn strcpy ,
305.Fn strcat ,
306.Fn strncpy ,
307.Fn strncat ,
308and perhaps some others.
309.Pp
310Extension Interfaces:
311.Pp
312.Fn strlcpy ,
313.Fn strlcat ,
314.Fn syslog_r .
315.Pp
316All functions not in the above lists are considered to be unsafe
317with respect to signals.
318That is to say, the behaviour of such functions when called from a
319signal handler is undefined.
320In general though, signal handlers should do little more than set a
321flag; most other actions are not safe.
322.Pp
323As well, inside the signal handler it is also considered more safe to
324make a copy the global variable
325.Va errno
326and restore it before returning from the signal handler.
327.Sh RETURN VALUES
328The previous action is returned on a successful call.
329Otherwise,
330.Fa SIG_ERR
331is returned and the global variable
332.Va errno
333is set to indicate the error.
334.Sh ERRORS
335.Fn signal
336will fail and no action will take place if one of the
337following occur:
338.Bl -tag -width Er
339.It Bq Er EINVAL
340A specified signal
341is not a valid signal number.
342.It Bq Er EINVAL
343An attempt is made to ignore or supply a handler for
344.Dv SIGKILL
345or
346.Ev SIGSTOP .
347.El
348.Sh SEE ALSO
349.Xr kill 1 ,
350.Xr kill 2 ,
351.Xr ptrace 2 ,
352.Xr sigaction 2 ,
353.Xr sigaltstack 2 ,
354.Xr sigprocmask 2 ,
355.Xr sigsuspend 2 ,
356.Xr setjmp 3 ,
357.Xr tty 4
358.Sh HISTORY
359This
360.Fn signal
361facility appeared in
362.Bx 4.0 .
363