man-pages/man2/signal.2.html
2021-03-31 01:06:50 +01:00

418 lines
8.9 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of SIGNAL</TITLE>
</HEAD><BODY>
<H1>SIGNAL</H1>
Section: Linux Programmer's Manual (2)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
signal - ANSI C signal handling
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/signal.h">signal.h</A>&gt;</B>
<P>
<B>typedef void (*sighandler_t)(int);</B>
<P>
<B>sighandler_t signal(int </B><I>signum</I><B>, sighandler_t </B><I>handler</I><B>);</B>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The behavior of
<B>signal</B>()
varies across UNIX versions,
and has also varied historically across different versions of Linux.
<B>Avoid its use</B>: use
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2)
instead.
See <I>Portability</I> below.
<P>
<B>signal</B>()
sets the disposition of the signal
<I>signum</I>
to
<I>handler</I>,
which is either
<B>SIG_IGN</B>,
<B>SIG_DFL</B>,
or the address of a programmer-defined function (a &quot;signal handler&quot;).
<P>
If the signal
<I>signum</I>
is delivered to the process, then one of the following happens:
<DL COMPACT>
<DT id="1">*<DD>
If the disposition is set to
<B>SIG_IGN</B>,
then the signal is ignored.
<DT id="2">*<DD>
If the disposition is set to
<B>SIG_DFL</B>,
then the default action associated with the signal (see
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7))
occurs.
<DT id="3">*<DD>
If the disposition is set to a function,
then first either the disposition is reset to
<B>SIG_DFL</B>,
or the signal is blocked (see <I>Portability</I> below), and then
<I>handler</I>
is called with argument
<I>signum</I>.
If invocation of the handler caused the signal to be blocked,
then the signal is unblocked upon return from the handler.
</DL>
<P>
The signals
<B>SIGKILL</B>
and
<B>SIGSTOP</B>
cannot be caught or ignored.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
<B>signal</B>()
returns the previous value of the signal handler, or
<B>SIG_ERR</B>
on error.
In the event of an error,
<I>errno</I>
is set to indicate the cause.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="4"><B>EINVAL</B>
<DD>
<I>signum</I>
is invalid.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008, C89, C99.
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
The effects of
<B>signal</B>()
in a multithreaded process are unspecified.
<P>
According to POSIX, the behavior of a process is undefined after it
ignores a
<B>SIGFPE</B>,
<B>SIGILL</B>,
or
<B>SIGSEGV</B>
signal that was not generated by
<B><A HREF="/cgi-bin/man/man2html?2+kill">kill</A></B>(2)
or
<B><A HREF="/cgi-bin/man/man2html?3+raise">raise</A></B>(3).
Integer division by zero has undefined result.
On some architectures it will generate a
<B>SIGFPE</B>
signal.
(Also dividing the most negative integer by -1 may generate
<B>SIGFPE</B>.)
Ignoring this signal might lead to an endless loop.
<P>
See
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2)
for details on what happens when the disposition
<B>SIGCHLD</B>
is set to
<B>SIG_IGN</B>.
<P>
See
<B><A HREF="/cgi-bin/man/man2html?7+signal-safety">signal-safety</A></B>(7)
for a list of the async-signal-safe functions that can be
safely called from inside a signal handler.
<P>
The use of
<I>sighandler_t</I>
is a GNU extension, exposed if
<B>_GNU_SOURCE</B>
is defined;
glibc also defines (the BSD-derived)
<I>sig_t</I>
if
<B>_BSD_SOURCE</B>
(glibc 2.19 and earlier)
or
<B>_DEFAULT_SOURCE</B>
(glibc 2.19 and later)
is defined.
Without use of such a type, the declaration of
<B>signal</B>()
is the somewhat harder to read:
<P>
<B>void ( *</B><I>signal</I><B>(int </B><I>signum</I><B>, void (*</B><I>handler</I><B>)(int)) ) (int);</B>
<A NAME="lbAI">&nbsp;</A>
<H3>Portability</H3>
The only portable use of
<B>signal</B>()
is to set a signal's disposition to
<B>SIG_DFL</B>
or
<B>SIG_IGN</B>.
The semantics when using
<B>signal</B>()
to establish a signal handler vary across systems
(and POSIX.1 explicitly permits this variation);
<B>do not use it for this purpose.</B>
<P>
POSIX.1 solved the portability mess by specifying
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2),
which provides explicit control of the semantics when a
signal handler is invoked; use that interface instead of
<B>signal</B>().
<P>
In the original UNIX systems, when a handler that was established using
<B>signal</B>()
was invoked by the delivery of a signal,
the disposition of the signal would be reset to
<B>SIG_DFL</B>,
and the system did not block delivery of further instances of the signal.
This is equivalent to calling
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2)
with the following flags:
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;sa.sa_flags&nbsp;=&nbsp;SA_RESETHAND&nbsp;|&nbsp;SA_NODEFER;
<P>
System&nbsp;V also provides these semantics for
<B>signal</B>().
This was bad because the signal might be delivered again
before the handler had a chance to reestablish itself.
Furthermore, rapid deliveries of the same signal could
result in recursive invocations of the handler.
<P>
BSD improved on this situation, but unfortunately also
changed the semantics of the existing
<B>signal</B>()
interface while doing so.
On BSD, when a signal handler is invoked,
the signal disposition is not reset,
and further instances of the signal are blocked from
being delivered while the handler is executing.
Furthermore, certain blocking system calls are automatically
restarted if interrupted by a signal handler (see
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7)).
The BSD semantics are equivalent to calling
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2)
with the following flags:
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;sa.sa_flags&nbsp;=&nbsp;SA_RESTART;
<P>
The situation on Linux is as follows:
<DL COMPACT>
<DT id="5">*<DD>
The kernel's
<B>signal</B>()
system call provides System&nbsp;V semantics.
<DT id="6">*<DD>
By default, in glibc 2 and later, the
<B>signal</B>()
wrapper function does not invoke the kernel system call.
Instead, it calls
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2)
using flags that supply BSD semantics.
This default behavior is provided as long as a suitable
feature test macro is defined:
<B>_BSD_SOURCE</B>
on glibc 2.19 and earlier or
<B>_DEFAULT_SOURCE</B>
in glibc 2.19 and later.
(By default, these macros are defined; see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)
for details.)
If such a feature test macro is not defined, then
<B>signal</B>()
provides System&nbsp;V semantics.
</DL>
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+kill">kill</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+alarm">alarm</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+kill">kill</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+pause">pause</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+sigaction">sigaction</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+signalfd">signalfd</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+sigpending">sigpending</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+sigprocmask">sigprocmask</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+sigsuspend">sigsuspend</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+bsd_signal">bsd_signal</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+killpg">killpg</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+raise">raise</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+siginterrupt">siginterrupt</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+sigqueue">sigqueue</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+sigsetops">sigsetops</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+sigvec">sigvec</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+sysv_signal">sysv_signal</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7)
<A NAME="lbAK">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="7"><A HREF="#lbAB">NAME</A><DD>
<DT id="8"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="9"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="10"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="11"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="12"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="13"><A HREF="#lbAH">NOTES</A><DD>
<DL>
<DT id="14"><A HREF="#lbAI">Portability</A><DD>
</DL>
<DT id="15"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="16"><A HREF="#lbAK">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:34 GMT, March 31, 2021
</BODY>
</HTML>