| 1 |
'\" t |
| 2 |
.\" Copyright 1993, David Metcalfe <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 6 |
.\" |
| 7 |
.TH atexit 3 2026-02-08 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
atexit \- register a function to be called at normal process termination |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ,\~ \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B #include <stdlib.h> |
| 16 |
.P |
| 17 |
.BI "int atexit(typeof(void (void)) *" function ); |
| 18 |
.fi |
| 19 |
.SH DESCRIPTION |
| 20 |
The |
| 21 |
.BR atexit () |
| 22 |
function registers the given |
| 23 |
.I function |
| 24 |
to be |
| 25 |
called at normal process termination, either via |
| 26 |
.BR exit (3) |
| 27 |
or via return from the program's |
| 28 |
.IR main (). |
| 29 |
Functions so registered are called |
| 30 |
in the reverse order of their registration; |
| 31 |
no arguments are passed. |
| 32 |
.P |
| 33 |
The same function may be registered multiple times: |
| 34 |
it is called once for each registration. |
| 35 |
.P |
| 36 |
POSIX.1 requires that an implementation allow at least |
| 37 |
.\" POSIX.1-2001, POSIX.1-2008 |
| 38 |
.B ATEXIT_MAX |
| 39 |
(32) such functions to be registered. |
| 40 |
The actual limit supported by an implementation can be obtained using |
| 41 |
.BR sysconf (3). |
| 42 |
.P |
| 43 |
When a child process is created via |
| 44 |
.BR fork (2), |
| 45 |
it inherits copies of its parent's registrations. |
| 46 |
Upon a successful call to one of the |
| 47 |
.BR exec (3) |
| 48 |
functions, |
| 49 |
all registrations are removed. |
| 50 |
.SH RETURN VALUE |
| 51 |
The |
| 52 |
.BR atexit () |
| 53 |
function returns the value 0 if successful; |
| 54 |
otherwise, |
| 55 |
it returns a nonzero value. |
| 56 |
.SH ATTRIBUTES |
| 57 |
For an explanation of the terms used in this section, see |
| 58 |
.BR attributes (7). |
| 59 |
.TS |
| 60 |
allbox; |
| 61 |
lbx lb lb |
| 62 |
l l l. |
| 63 |
Interface Attribute Value |
| 64 |
T{ |
| 65 |
.na |
| 66 |
.nh |
| 67 |
.BR atexit () |
| 68 |
T} Thread safety MT-Safe |
| 69 |
.TE |
| 70 |
.SH VERSIONS |
| 71 |
POSIX.1 says that the result of calling |
| 72 |
.\" POSIX.1-2001, POSIX.1-2008 |
| 73 |
.BR exit (3) |
| 74 |
more than once (i.e., calling |
| 75 |
.BR exit (3) |
| 76 |
within a function registered using |
| 77 |
.BR atexit ()) |
| 78 |
is undefined. |
| 79 |
On some systems (but not Linux), this can result in an infinite recursion; |
| 80 |
.\" This can happen on OpenBSD 4.2 for example, and is documented |
| 81 |
.\" as occurring on FreeBSD as well. |
| 82 |
.\" glibc does "the Right Thing" -- invocation of the remaining |
| 83 |
.\" exit handlers carries on as normal. |
| 84 |
portable programs should not invoke |
| 85 |
.BR exit (3) |
| 86 |
inside a function registered using |
| 87 |
.BR atexit (). |
| 88 |
.SH STANDARDS |
| 89 |
C11, POSIX.1-2008. |
| 90 |
.SH HISTORY |
| 91 |
POSIX.1-2001, C89, C99, SVr4, 4.3BSD. |
| 92 |
.SH NOTES |
| 93 |
Functions registered using |
| 94 |
.BR atexit () |
| 95 |
(and |
| 96 |
.BR on_exit (3)) |
| 97 |
are not called if a process terminates abnormally because |
| 98 |
of the delivery of a signal. |
| 99 |
.P |
| 100 |
If one of the registered functions calls |
| 101 |
.BR _exit (2), |
| 102 |
then any remaining functions are not invoked, |
| 103 |
and the other process termination steps performed by |
| 104 |
.BR exit (3) |
| 105 |
are not performed. |
| 106 |
.P |
| 107 |
The |
| 108 |
.BR atexit () |
| 109 |
and |
| 110 |
.BR on_exit (3) |
| 111 |
functions register functions on the same list: |
| 112 |
at normal process termination, |
| 113 |
the registered functions are invoked in reverse order |
| 114 |
of their registration by these two functions. |
| 115 |
.P |
| 116 |
According to POSIX.1, the result is undefined if |
| 117 |
.BR longjmp (3) |
| 118 |
is used to terminate execution of one of the functions registered using |
| 119 |
.BR atexit (). |
| 120 |
.\" In glibc, things seem to be handled okay |
| 121 |
.SS Linux notes |
| 122 |
Since glibc 2.2.3, |
| 123 |
.BR atexit () |
| 124 |
(and |
| 125 |
.BR on_exit (3)) |
| 126 |
can be used within a shared library to establish functions |
| 127 |
that are called when the shared library is unloaded. |
| 128 |
.SH EXAMPLES |
| 129 |
.\" SRC BEGIN (atexit.c) |
| 130 |
.EX |
| 131 |
#include <stdio.h> |
| 132 |
#include <stdlib.h> |
| 133 |
#include <unistd.h> |
| 134 |
\& |
| 135 |
static void |
| 136 |
bye(void) |
| 137 |
{ |
| 138 |
printf("That was all, folks\[rs]n"); |
| 139 |
} |
| 140 |
\& |
| 141 |
int |
| 142 |
main(void) |
| 143 |
{ |
| 144 |
long a; |
| 145 |
int i; |
| 146 |
\& |
| 147 |
a = sysconf(_SC_ATEXIT_MAX); |
| 148 |
printf("ATEXIT_MAX = %ld\[rs]n", a); |
| 149 |
\& |
| 150 |
i = atexit(bye); |
| 151 |
if (i != 0) { |
| 152 |
fprintf(stderr, "cannot set exit function\[rs]n"); |
| 153 |
exit(EXIT_FAILURE); |
| 154 |
} |
| 155 |
\& |
| 156 |
exit(EXIT_SUCCESS); |
| 157 |
} |
| 158 |
.EE |
| 159 |
.\" SRC END |
| 160 |
.SH SEE ALSO |
| 161 |
.BR _exit (2), |
| 162 |
.BR dlopen (3), |
| 163 |
.BR exit (3), |
| 164 |
.BR on_exit (3) |