xref: /freebsd/lib/libc/gen/signal.3 (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
1.\" Copyright (c) 1980, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)signal.3	8.3 (Berkeley) 4/19/94
29.\"
30.Dd December 1, 2017
31.Dt SIGNAL 3
32.Os
33.Sh NAME
34.Nm signal
35.Nd simplified software signal facilities
36.Sh LIBRARY
37.Lb libc
38.Sh SYNOPSIS
39.In signal.h
40.\" The following is Quite Ugly, but syntactically correct.
41.\" Don't try to fix it.
42.Ft void
43.Fn \*(lp*signal "int sig" "void \*(lp*func\*(rp\*(lpint\*(rp\*(rp\*(rp\*(lpint"
44.Pp
45or in
46.Fx Ap s
47equivalent but easier to read typedef'd version:
48.Ft typedef "void \*(lp*sig_t\*(rp \*(lpint\*(rp" ;
49.Pp
50.Ft sig_t
51.Fn signal "int sig" "sig_t func"
52.Sh DESCRIPTION
53This
54.Fn signal
55facility
56is a simplified interface to the more general
57.Xr sigaction 2
58facility.
59.Pp
60Signals allow the manipulation of a process from outside its
61domain as well as allowing the process to manipulate itself or
62copies of itself (children).
63There are two general types of signals:
64those that cause termination of a process and those that do not.
65Signals which cause termination of a program might result from
66an irrecoverable error or might be the result of a user at a terminal
67typing the `interrupt' character.
68Signals are used when a process is stopped because it wishes to access
69its control terminal while in the background (see
70.Xr tty 4 ) .
71Signals are optionally generated
72when a process resumes after being stopped,
73when the status of child processes changes,
74or when input is ready at the control terminal.
75Most signals result in the termination of the process receiving them
76if no action
77is taken; some signals instead cause the process receiving them
78to be stopped, or are simply discarded if the process has not
79requested otherwise.
80Except for the
81.Dv SIGKILL
82and
83.Dv SIGSTOP
84signals, the
85.Fn signal
86function allows for a signal to be caught, to be ignored, or to generate
87an interrupt.
88These signals are defined in the file
89.In signal.h :
90.Bl -column No ".Dv SIGVTALRM" "create core image"
91.It Sy "Num" Ta Sy "Name" Ta Sy "Default Action" Ta Sy "Description"
92.It 1 Ta Dv SIGHUP Ta "terminate process" Ta "terminal line hangup"
93.It 2 Ta Dv SIGINT Ta "terminate process" Ta "interrupt program"
94.It 3 Ta Dv SIGQUIT Ta "create core image" Ta "quit program"
95.It 4 Ta Dv SIGILL Ta "create core image" Ta "illegal instruction"
96.It 5 Ta Dv SIGTRAP Ta "create core image" Ta "trace trap"
97.It 6 Ta Dv SIGABRT Ta "create core image" Ta "abort program"
98(formerly
99.Dv SIGIOT )
100.It 7 Ta Dv SIGEMT Ta "create core image" Ta "emulate instruction executed"
101.It 8 Ta Dv SIGFPE Ta "create core image" Ta "floating-point exception"
102.It 9 Ta Dv SIGKILL Ta "terminate process" Ta "kill program"
103.It 10 Ta Dv SIGBUS Ta "create core image" Ta "bus error"
104.It 11 Ta Dv SIGSEGV Ta "create core image" Ta "segmentation violation"
105.It 12 Ta Dv SIGSYS Ta "create core image" Ta "non-existent system call invoked"
106.It 13 Ta Dv SIGPIPE Ta "terminate process" Ta "write on a pipe with no reader"
107.It 14 Ta Dv SIGALRM Ta "terminate process" Ta "real-time timer expired"
108.It 15 Ta Dv SIGTERM Ta "terminate process" Ta "software termination signal"
109.It 16 Ta Dv SIGURG Ta "discard signal" Ta "urgent condition present on socket"
110.It 17 Ta Dv SIGSTOP Ta "stop process" Ta "stop (cannot be caught or ignored)"
111.It 18 Ta Dv SIGTSTP Ta "stop process" Ta "stop signal generated from keyboard"
112.It 19 Ta Dv SIGCONT Ta "discard signal" Ta "continue after stop"
113.It 20 Ta Dv SIGCHLD Ta "discard signal" Ta "child status has changed"
114.It 21 Ta Dv SIGTTIN Ta "stop process" Ta "background read attempted from"
115control terminal
116.It 22 Ta Dv SIGTTOU Ta "stop process" Ta "background write attempted to"
117control terminal
118.It 23 Ta Dv SIGIO Ta "discard signal" Ta Tn "I/O"
119is possible on a descriptor (see
120.Xr fcntl 2 )
121.It 24 Ta Dv SIGXCPU Ta "terminate process" Ta "cpu time limit exceeded (see"
122.Xr setrlimit 2 )
123.It 25 Ta Dv SIGXFSZ Ta "terminate process" Ta "file size limit exceeded (see"
124.Xr setrlimit 2 )
125.It 26 Ta Dv SIGVTALRM Ta "terminate process" Ta "virtual time alarm (see"
126.Xr setitimer 2 )
127.It 27 Ta Dv SIGPROF Ta "terminate process" Ta "profiling timer alarm (see"
128.Xr setitimer 2 )
129.It 28 Ta Dv SIGWINCH Ta "discard signal" Ta "Window size change"
130.It 29 Ta Dv SIGINFO Ta "discard signal" Ta "status request from keyboard"
131.It 30 Ta Dv SIGUSR1 Ta "terminate process" Ta "User defined signal 1"
132.It 31 Ta Dv SIGUSR2 Ta "terminate process" Ta "User defined signal 2"
133.It 32 Ta Dv SIGTHR Ta "terminate process" Ta "thread interrupt"
134.It 33 Ta Dv SIGLIBRT Ta "terminate process" Ta "real-time library interrupt"
135.El
136.Pp
137The
138.Fa sig
139argument specifies which signal was received.
140The
141.Fa func
142procedure allows a user to choose the action upon receipt of a signal.
143To set the default action of the signal to occur as listed above,
144.Fa func
145should be
146.Dv SIG_DFL .
147A
148.Dv SIG_DFL
149resets the default action.
150To ignore the signal
151.Fa func
152should be
153.Dv SIG_IGN .
154This will cause subsequent instances of the signal to be ignored
155and pending instances to be discarded.
156If
157.Dv SIG_IGN
158is not used,
159further occurrences of the signal are
160automatically blocked and
161.Fa func
162is called.
163.Pp
164The handled signal is unblocked when the
165function returns 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.
175Any handler installed with
176.Xr signal 3
177will have the
178.Dv SA_RESTART
179flag set, meaning that any restartable system call will not return on
180receipt of a signal.
181The affected system calls include
182.Xr read 2 ,
183.Xr write 2 ,
184.Xr sendto 2 ,
185.Xr recvfrom 2 ,
186.Xr sendmsg 2
187and
188.Xr recvmsg 2
189on a communications channel or a low speed device
190and during a
191.Xr ioctl 2
192or
193.Xr wait 2 .
194However, calls that have already committed are not restarted,
195but instead return a partial success (for example, a short read count).
196These semantics could be changed with
197.Xr siginterrupt 3 .
198.Pp
199When a process which has installed signal handlers forks,
200the child process inherits the signals.
201All caught signals may be reset to their default action by a call
202to the
203.Xr execve 2
204function;
205ignored signals remain ignored.
206.Pp
207If a process explicitly specifies
208.Dv SIG_IGN
209as the action for the signal
210.Dv SIGCHLD ,
211the system will not create zombie processes when children
212of the calling process exit.
213As a consequence, the system will discard the exit status
214from the child processes.
215If the calling process subsequently issues a call to
216.Xr wait 2
217or equivalent, it will block until all of the calling process's
218children terminate, and then return a value of \-1 with
219.Va errno
220set to
221.Er ECHILD .
222.Pp
223See
224.Xr sigaction 2
225for a list of functions
226that are considered safe for use in signal handlers.
227.Sh RETURN VALUES
228The previous action is returned on a successful call.
229Otherwise, SIG_ERR is returned and the global variable
230.Va errno
231is set to indicate the error.
232.Sh ERRORS
233The
234.Fn signal
235function
236will fail and no action will take place if one of the
237following occur:
238.Bl -tag -width Er
239.It Bq Er EINVAL
240The
241.Fa sig
242argument
243is not a valid signal number.
244.It Bq Er EINVAL
245An attempt is made to ignore or supply a handler for
246.Dv SIGKILL
247or
248.Dv SIGSTOP .
249.El
250.Sh SEE ALSO
251.Xr kill 1 ,
252.Xr kill 2 ,
253.Xr ptrace 2 ,
254.Xr sigaction 2 ,
255.Xr sigaltstack 2 ,
256.Xr sigprocmask 2 ,
257.Xr sigsuspend 2 ,
258.Xr wait 2 ,
259.Xr fpsetmask 3 ,
260.Xr setjmp 3 ,
261.Xr siginterrupt 3 ,
262.Xr tty 4
263.Sh HISTORY
264The
265.Fn signal
266function appeared in
267.At v4 .
268The current
269.Nm
270facility appeared in
271.Bx 4.0 .
272The option to avoid the creation of child zombies through ignoring
273.Dv SIGCHLD
274appeared in
275.Fx 5.0 .
276