1.\"	$NetBSD: signal.3,v 1.27 2016/06/06 08:28:18 wiz 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.\"     @(#)signal.3	8.3 (Berkeley) 4/19/94
31.\"
32.Dd June 5, 2016
33.Dt SIGNAL 3
34.Os
35.Sh NAME
36.Nm signal
37.Nd simplified software signal facilities
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In signal.h
42.\" The following is Quite Ugly, but syntactically correct.  Don't try to
43.\" fix it.
44.Ft void \*(lp*
45.Fn signal "int sig" "void \*(lp*func\*(rp\*(lpint\*(rp\*(rp\*(rp\*(lpint"
46.Sh DESCRIPTION
47This
48.Fn signal
49facility
50is a simplified interface to the more general
51.Xr sigaction 2
52facility.
53.Pp
54Signals allow the manipulation of a process from outside its
55domain as well as allowing the process to manipulate itself or
56copies of itself (children).
57There are two general types of signals:
58those that cause termination of a process and those that do not.
59Signals which cause termination of a program might result from
60an irrecoverable error or might be the result of a user at a terminal
61typing the `interrupt' character.
62Signals are used when a process is stopped because it wishes to access
63its control terminal while in the background (see
64.Xr tty 4 ) .
65Signals are optionally generated
66when a process resumes after being stopped,
67when the status of child processes changes,
68or when input is ready at the control terminal.
69Most signals result in the termination of the process receiving them
70if no action
71is taken; some signals instead cause the process receiving them
72to be stopped, or are simply discarded if the process has not
73requested otherwise.
74Except for the
75.Dv SIGKILL
76and
77.Dv SIGSTOP
78signals, the
79.Fn signal
80function allows for a signal to be caught, to be ignored, or to generate
81an interrupt.
82See
83.Xr signal 7
84for comprehensive list of supported signals.
85.Pp
86The
87.Fa func
88procedure allows a user to choose the action upon receipt of a signal.
89To set the default action of the signal to occur as listed above,
90.Fa func
91should be
92.Dv SIG_DFL .
93A
94.Dv SIG_DFL
95resets the default action.
96To ignore the signal
97.Fa func
98should be
99.Dv SIG_IGN .
100This will cause subsequent instances of the signal to be ignored
101and pending instances to be discarded.
102If
103.Dv SIG_IGN
104is not used,
105further occurrences of the signal are
106automatically blocked and
107.Fa func
108is called.
109.Pp
110The handled signal is unblocked when the
111function returns and
112the process continues from where it left off when the signal occurred.
113.Bf -symbolic
114Unlike previous signal facilities, the handler
115.Fn func
116remains installed after a signal has been delivered.
117.Ef
118.Pp
119For some system calls, if a signal is caught while the call is
120executing and the call is prematurely terminated,
121the call is automatically restarted.
122(The handler is installed using the
123.Dv SA_RESTART
124flag with
125.Xr sigaction 2 ) .
126The affected system calls include
127.Xr read 2 ,
128.Xr write 2 ,
129.Xr sendto 2 ,
130.Xr recvfrom 2 ,
131.Xr sendmsg 2
132and
133.Xr recvmsg 2
134on a communications channel or a low speed device
135and during a
136.Xr ioctl 2
137or
138.Xr wait 2 .
139However, calls that have already committed are not restarted,
140but instead return a partial success (for example, a short read count).
141.Pp
142When a process which has installed signal handlers forks,
143the child process inherits the signals.
144All caught signals may be reset to their default action by a call
145to the
146.Xr execve 2
147function;
148ignored signals remain ignored.
149.Pp
150Only functions that are async-signal-safe can safely be used in signal
151handlers, see
152.Xr signal 7
153for a complete list.
154.Sh RETURN VALUES
155The previous action is returned on a successful call.
156Otherwise,
157.Dv SIG_ERR
158is returned and the global variable
159.Va errno
160is set to indicate the error.
161.Sh ERRORS
162.Fn signal
163will fail and no action will take place if one of the following occur:
164.Bl -tag -width Er
165.It Bq Er EINVAL
166Specified
167.Em sig
168is not a valid signal number;
169or an attempt is made to ignore or supply a handler for
170.Dv SIGKILL
171or
172.Dv SIGSTOP .
173.El
174.Sh SEE ALSO
175.Xr kill 1 ,
176.Xr kill 2 ,
177.Xr ptrace 2 ,
178.Xr sigaction 2 ,
179.Xr sigaltstack 2 ,
180.Xr sigprocmask 2 ,
181.Xr sigsuspend 2 ,
182.Xr bsd_signal 3 ,
183.Xr psignal 3 ,
184.Xr setjmp 3 ,
185.Xr strsignal 3 ,
186.Xr tty 4 ,
187.Xr signal 7
188.Sh HISTORY
189This
190.Fn signal
191facility appeared in
192.Bx 4.0 .
193