]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) |
2 | .\" | |
93015253 | 3 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
4 | .\" Permission is granted to make and distribute verbatim copies of this |
5 | .\" manual provided the copyright notice and this permission notice are | |
6 | .\" preserved on all copies. | |
7 | .\" | |
8 | .\" Permission is granted to copy and distribute modified versions of this | |
9 | .\" manual under the conditions for verbatim copying, provided that the | |
10 | .\" entire resulting derived work is distributed under the terms of a | |
11 | .\" permission notice identical to this one. | |
c13182ef | 12 | .\" |
fea681da MK |
13 | .\" Since the Linux kernel and libraries are constantly changing, this |
14 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
15 | .\" responsibility for errors or omissions, or for damages resulting from | |
16 | .\" the use of the information contained herein. The author(s) may not | |
17 | .\" have taken the same level of care in the production of this manual, | |
18 | .\" which is licensed free of charge, as they might when working | |
19 | .\" professionally. | |
c13182ef | 20 | .\" |
fea681da MK |
21 | .\" Formatted or processed versions of this manual, if unaccompanied by |
22 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 23 | .\" %%%LICENSE_END |
fea681da MK |
24 | .\" |
25 | .\" References consulted: | |
26 | .\" Linux libc source code | |
27 | .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) | |
28 | .\" 386BSD man pages | |
29 | .\" Modified 1993-03-29, David Metcalfe | |
30 | .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu) | |
31 | .\" Modified 2003-10-25, Walter Harms | |
32 | .\" | |
4b8c67d9 | 33 | .TH ATEXIT 3 2017-09-15 "Linux" "Linux Programmer's Manual" |
fea681da | 34 | .SH NAME |
5af3e8ee | 35 | atexit \- register a function to be called at normal process termination |
fea681da MK |
36 | .SH SYNOPSIS |
37 | .nf | |
38 | .B #include <stdlib.h> | |
68e4db0a | 39 | .PP |
fea681da MK |
40 | .BI "int atexit(void (*" function )(void)); |
41 | .fi | |
42 | .SH DESCRIPTION | |
60a90ecd MK |
43 | The |
44 | .BR atexit () | |
c6fa0841 MK |
45 | function registers the given |
46 | .I function | |
47 | to be | |
5af3e8ee | 48 | called at normal process termination, either via |
fea681da | 49 | .BR exit (3) |
c6fa0841 MK |
50 | or via return from the program's |
51 | .IR main (). | |
fea681da MK |
52 | Functions so registered are called in |
53 | the reverse order of their registration; no arguments are passed. | |
847e0d88 | 54 | .PP |
ee394477 MK |
55 | The same function may be registered multiple times: |
56 | it is called once for each registration. | |
dd3568a1 | 57 | .PP |
46913bec MK |
58 | POSIX.1 requires that an implementation allow at least |
59 | .\" POSIX.1-2001, POSIX.1-2008 | |
0f2111e3 MK |
60 | .B ATEXIT_MAX |
61 | (32) such functions to be registered. | |
5af3e8ee | 62 | The actual limit supported by an implementation can be obtained using |
fea681da | 63 | .BR sysconf (3). |
dd3568a1 | 64 | .PP |
c13182ef | 65 | When a child process is created via |
fb186734 | 66 | .BR fork (2), |
0b0bb11a | 67 | it inherits copies of its parent's registrations. |
c13182ef | 68 | Upon a successful call to one of the |
363a3cc9 | 69 | .BR exec (3) |
1e321034 | 70 | functions, |
18f2ce40 | 71 | all registrations are removed. |
47297adb | 72 | .SH RETURN VALUE |
60a90ecd MK |
73 | The |
74 | .BR atexit () | |
75 | function returns the value 0 if successful; otherwise | |
c7094399 | 76 | it returns a nonzero value. |
e4a2e237 ZL |
77 | .SH ATTRIBUTES |
78 | For an explanation of the terms used in this section, see | |
79 | .BR attributes (7). | |
80 | .TS | |
81 | allbox; | |
82 | lb lb lb | |
83 | l l l. | |
84 | Interface Attribute Value | |
85 | T{ | |
86 | .BR atexit () | |
87 | T} Thread safety MT-Safe | |
88 | .TE | |
847e0d88 | 89 | .sp 1 |
47297adb | 90 | .SH CONFORMING TO |
46913bec | 91 | POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD. |
2b2581ee | 92 | .SH NOTES |
2b2581ee MK |
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. | |
847e0d88 | 99 | .PP |
0512f57c | 100 | If one of the registered functions calls |
41bf770c MK |
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. | |
847e0d88 | 106 | .PP |
46913bec MK |
107 | POSIX.1 says that the result of calling |
108 | .\" POSIX.1-2001, POSIX.1-2008 | |
2656acee MK |
109 | .BR exit (3) |
110 | more than once (i.e., calling | |
111 | .BR exit (3) | |
112 | within a function registered using | |
27d47e71 | 113 | .BR atexit ()) |
2656acee MK |
114 | is undefined. |
115 | On some systems (but not Linux), this can result in an infinite recursion; | |
116 | .\" This can happen on OpenBSD 4.2 for example, and is documented | |
117 | .\" as occurring on FreeBSD as well. | |
118 | .\" Glibc does "the Right Thing" -- invocation of the remaining | |
119 | .\" exit handlers carries on as normal. | |
120 | portable programs should not invoke | |
121 | .BR exit (3) | |
122 | inside a function registered using | |
27d47e71 | 123 | .BR atexit (). |
847e0d88 | 124 | .PP |
374b9a8f MK |
125 | The |
126 | .BR atexit () | |
127 | and | |
128 | .BR on_exit (3) | |
129 | functions register functions on the same list: | |
130 | at normal process termination, | |
131 | the registered functions are invoked in reverse order | |
132 | of their registration by these two functions. | |
847e0d88 | 133 | .PP |
46913bec | 134 | According to POSIX.1, the result is undefined if |
7c298e46 | 135 | .BR longjmp (3) |
be4c9c66 | 136 | is used to terminate execution of one of the functions registered using |
7c298e46 | 137 | .BR atexit (). |
84fc5566 | 138 | .\" In glibc, things seem to be handled okay |
c634028a | 139 | .SS Linux notes |
206ed587 MK |
140 | Since glibc 2.2.3, |
141 | .BR atexit () | |
142 | (and | |
143 | .BR on_exit (3)) | |
144 | can be used within a shared library to establish functions | |
145 | that are called when the shared library is unloaded. | |
fea681da | 146 | .SH EXAMPLE |
207050fa | 147 | .EX |
fea681da MK |
148 | #include <stdio.h> |
149 | #include <stdlib.h> | |
150 | #include <unistd.h> | |
151 | ||
c13182ef MK |
152 | void |
153 | bye(void) | |
cf0a9ace | 154 | { |
31a6818e | 155 | printf("That was all, folks\en"); |
fea681da MK |
156 | } |
157 | ||
c13182ef | 158 | int |
cf0a9ace MK |
159 | main(void) |
160 | { | |
161 | long a; | |
162 | int i; | |
fea681da | 163 | |
cf0a9ace | 164 | a = sysconf(_SC_ATEXIT_MAX); |
31a6818e | 165 | printf("ATEXIT_MAX = %ld\en", a); |
fea681da | 166 | |
cf0a9ace MK |
167 | i = atexit(bye); |
168 | if (i != 0) { | |
31a6818e | 169 | fprintf(stderr, "cannot set exit function\en"); |
cf634cef | 170 | exit(EXIT_FAILURE); |
cf0a9ace | 171 | } |
cf634cef MK |
172 | |
173 | exit(EXIT_SUCCESS); | |
fea681da | 174 | } |
207050fa | 175 | .EE |
47297adb | 176 | .SH SEE ALSO |
aa3a009e | 177 | .BR _exit (2), |
92bade29 | 178 | .BR dlopen (3), |
fea681da MK |
179 | .BR exit (3), |
180 | .BR on_exit (3) |