Blame man3/atexit.3

Packit 7cfc04
.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" References consulted:
Packit 7cfc04
.\"     Linux libc source code
Packit 7cfc04
.\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
Packit 7cfc04
.\"     386BSD man pages
Packit 7cfc04
.\" Modified 1993-03-29, David Metcalfe
Packit 7cfc04
.\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
Packit 7cfc04
.\" Modified 2003-10-25, Walter Harms
Packit 7cfc04
.\"
Packit 7cfc04
.TH ATEXIT 3  2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
atexit \- register a function to be called at normal process termination
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <stdlib.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "int atexit(void (*" function )(void));
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR atexit ()
Packit 7cfc04
function registers the given
Packit 7cfc04
.I function
Packit 7cfc04
to be
Packit 7cfc04
called at normal process termination, either via
Packit 7cfc04
.BR exit (3)
Packit 7cfc04
or via return from the program's
Packit 7cfc04
.IR main ().
Packit 7cfc04
Functions so registered are called in
Packit 7cfc04
the reverse order of their registration; no arguments are passed.
Packit 7cfc04
.PP
Packit 7cfc04
The same function may be registered multiple times:
Packit 7cfc04
it is called once for each registration.
Packit 7cfc04
.PP
Packit 7cfc04
POSIX.1 requires that an implementation allow at least
Packit 7cfc04
.\" POSIX.1-2001, POSIX.1-2008
Packit 7cfc04
.B ATEXIT_MAX
Packit 7cfc04
(32) such functions to be registered.
Packit 7cfc04
The actual limit supported by an implementation can be obtained using
Packit 7cfc04
.BR sysconf (3).
Packit 7cfc04
.PP
Packit 7cfc04
When a child process is created via
Packit 7cfc04
.BR fork (2),
Packit 7cfc04
it inherits copies of its parent's registrations.
Packit 7cfc04
Upon a successful call to one of the
Packit 7cfc04
.BR exec (3)
Packit 7cfc04
functions,
Packit 7cfc04
all registrations are removed.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
The
Packit 7cfc04
.BR atexit ()
Packit 7cfc04
function returns the value 0 if successful; otherwise
Packit 7cfc04
it returns a nonzero value.
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lb lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR atexit ()
Packit 7cfc04
T}	Thread safety	MT-Safe
Packit 7cfc04
.TE
Packit 7cfc04
.sp 1
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
Functions registered using
Packit 7cfc04
.BR atexit ()
Packit 7cfc04
(and
Packit 7cfc04
.BR on_exit (3))
Packit 7cfc04
are not called if a process terminates abnormally because
Packit 7cfc04
of the delivery of a signal.
Packit 7cfc04
.PP
Packit 7cfc04
If one of the functions registered functions calls
Packit 7cfc04
.BR _exit (2),
Packit 7cfc04
then any remaining functions are not invoked,
Packit 7cfc04
and the other process termination steps performed by
Packit 7cfc04
.BR exit (3)
Packit 7cfc04
are not performed.
Packit 7cfc04
.PP
Packit 7cfc04
POSIX.1 says that the result of calling
Packit 7cfc04
.\" POSIX.1-2001, POSIX.1-2008
Packit 7cfc04
.BR exit (3)
Packit 7cfc04
more than once (i.e., calling
Packit 7cfc04
.BR exit (3)
Packit 7cfc04
within a function registered using
Packit 7cfc04
.BR atexit ())
Packit 7cfc04
is undefined.
Packit 7cfc04
On some systems (but not Linux), this can result in an infinite recursion;
Packit 7cfc04
.\" This can happen on OpenBSD 4.2 for example, and is documented
Packit 7cfc04
.\" as occurring on FreeBSD as well.
Packit 7cfc04
.\" Glibc does "the Right Thing" -- invocation of the remaining
Packit 7cfc04
.\" exit handlers carries on as normal.
Packit 7cfc04
portable programs should not invoke
Packit 7cfc04
.BR exit (3)
Packit 7cfc04
inside a function registered using
Packit 7cfc04
.BR atexit ().
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR atexit ()
Packit 7cfc04
and
Packit 7cfc04
.BR on_exit (3)
Packit 7cfc04
functions register functions on the same list:
Packit 7cfc04
at normal process termination,
Packit 7cfc04
the registered functions are invoked in reverse order
Packit 7cfc04
of their registration by these two functions.
Packit 7cfc04
.PP
Packit 7cfc04
According to POSIX.1, the result is undefined if
Packit 7cfc04
.BR longjmp (3)
Packit 7cfc04
is used to terminate execution of one of the functions registered using
Packit 7cfc04
.BR atexit ().
Packit 7cfc04
.\" In glibc, things seem to be handled okay
Packit 7cfc04
.SS Linux notes
Packit 7cfc04
Since glibc 2.2.3,
Packit 7cfc04
.BR atexit ()
Packit 7cfc04
(and
Packit 7cfc04
.BR on_exit (3))
Packit 7cfc04
can be used within a shared library to establish functions
Packit 7cfc04
that are called when the shared library is unloaded.
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
.EX
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
Packit 7cfc04
void
Packit 7cfc04
bye(void)
Packit 7cfc04
{
Packit 7cfc04
    printf("That was all, folks\en");
Packit 7cfc04
}
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(void)
Packit 7cfc04
{
Packit 7cfc04
    long a;
Packit 7cfc04
    int i;
Packit 7cfc04
Packit 7cfc04
    a = sysconf(_SC_ATEXIT_MAX);
Packit 7cfc04
    printf("ATEXIT_MAX = %ld\en", a);
Packit 7cfc04
Packit 7cfc04
    i = atexit(bye);
Packit 7cfc04
    if (i != 0) {
Packit 7cfc04
        fprintf(stderr, "cannot set exit function\en");
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    exit(EXIT_SUCCESS);
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR _exit (2),
Packit 7cfc04
.BR dlopen (3),
Packit 7cfc04
.BR exit (3),
Packit 7cfc04
.BR on_exit (3)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.