man-pages/man3/atexit.3.html
2021-03-31 01:06:50 +01:00

250 lines
6.3 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of ATEXIT</TITLE>
</HEAD><BODY>
<H1>ATEXIT</H1>
Section: Linux Programmer's Manual (3)<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>
atexit - register a function to be called at normal process termination
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;</B>
<B>int atexit(void (*</B><I>function</I><B>)(void));</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>atexit</B>()
function registers the given
<I>function</I>
to be
called at normal process termination, either via
<B><A HREF="/cgi-bin/man/man2html?3+exit">exit</A></B>(3)
or via return from the program's
<I>main</I>().
Functions so registered are called in
the reverse order of their registration; no arguments are passed.
<P>
The same function may be registered multiple times:
it is called once for each registration.
<P>
POSIX.1 requires that an implementation allow at least
<B>ATEXIT_MAX</B>
(32) such functions to be registered.
The actual limit supported by an implementation can be obtained using
<B><A HREF="/cgi-bin/man/man2html?3+sysconf">sysconf</A></B>(3).
<P>
When a child process is created via
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
it inherits copies of its parent's registrations.
Upon a successful call to one of the
<B><A HREF="/cgi-bin/man/man2html?3+exec">exec</A></B>(3)
functions,
all registrations are removed.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
The
<B>atexit</B>()
function returns the value 0 if successful; otherwise
it returns a nonzero value.
<A NAME="lbAF">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>atexit</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
Functions registered using
<B>atexit</B>()
(and
<B><A HREF="/cgi-bin/man/man2html?3+on_exit">on_exit</A></B>(3))
are not called if a process terminates abnormally because
of the delivery of a signal.
<P>
If one of the registered functions calls
<B><A HREF="/cgi-bin/man/man2html?2+_exit">_exit</A></B>(2),
then any remaining functions are not invoked,
and the other process termination steps performed by
<B><A HREF="/cgi-bin/man/man2html?3+exit">exit</A></B>(3)
are not performed.
<P>
POSIX.1 says that the result of calling
<B><A HREF="/cgi-bin/man/man2html?3+exit">exit</A></B>(3)
more than once (i.e., calling
<B><A HREF="/cgi-bin/man/man2html?3+exit">exit</A></B>(3)
within a function registered using
<B>atexit</B>())
is undefined.
On some systems (but not Linux), this can result in an infinite recursion;
portable programs should not invoke
<B><A HREF="/cgi-bin/man/man2html?3+exit">exit</A></B>(3)
inside a function registered using
<B>atexit</B>().
<P>
The
<B>atexit</B>()
and
<B><A HREF="/cgi-bin/man/man2html?3+on_exit">on_exit</A></B>(3)
functions register functions on the same list:
at normal process termination,
the registered functions are invoked in reverse order
of their registration by these two functions.
<P>
According to POSIX.1, the result is undefined if
<B><A HREF="/cgi-bin/man/man2html?3+longjmp">longjmp</A></B>(3)
is used to terminate execution of one of the functions registered using
<B>atexit</B>().
<A NAME="lbAI">&nbsp;</A>
<H3>Linux notes</H3>
Since glibc 2.2.3,
<B>atexit</B>()
(and
<B><A HREF="/cgi-bin/man/man2html?3+on_exit">on_exit</A></B>(3))
can be used within a shared library to establish functions
that are called when the shared library is unloaded.
<A NAME="lbAJ">&nbsp;</A>
<H2>EXAMPLE</H2>
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
<P>
void
bye(void)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;That&nbsp;was&nbsp;all,&nbsp;folks\n&quot;);
}
<P>
int
main(void)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;long&nbsp;a;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;i;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;=&nbsp;sysconf(_SC_ATEXIT_MAX);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;ATEXIT_MAX&nbsp;=&nbsp;%ld\n&quot;,&nbsp;a);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;=&nbsp;atexit(bye);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(i&nbsp;!=&nbsp;0)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;cannot&nbsp;set&nbsp;exit&nbsp;function\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+_exit">_exit</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+dlopen">dlopen</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+exit">exit</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+on_exit">on_exit</A></B>(3)
<A NAME="lbAL">&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="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="4"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="5"><A HREF="#lbAF">ATTRIBUTES</A><DD>
<DT id="6"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="7"><A HREF="#lbAH">NOTES</A><DD>
<DL>
<DT id="8"><A HREF="#lbAI">Linux notes</A><DD>
</DL>
<DT id="9"><A HREF="#lbAJ">EXAMPLE</A><DD>
<DT id="10"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="11"><A HREF="#lbAL">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:36 GMT, March 31, 2021
</BODY>
</HTML>