mount_setattr.2: Minor tweaks to Christian's patch
[man-pages.git] / man3 / atexit.3
bloba9d5aac98c38c7fec7d021d0097a41206f9bee5a
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
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.
12 .\"
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.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
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 .\"
33 .TH ATEXIT 3  2021-03-22 "Linux" "Linux Programmer's Manual"
34 .SH NAME
35 atexit \- register a function to be called at normal process termination
36 .SH SYNOPSIS
37 .nf
38 .B #include <stdlib.h>
39 .PP
40 .BI "int atexit(void (*" function )(void));
41 .fi
42 .SH DESCRIPTION
43 The
44 .BR atexit ()
45 function registers the given
46 .I function
47 to be
48 called at normal process termination, either via
49 .BR exit (3)
50 or via return from the program's
51 .IR main ().
52 Functions so registered are called in
53 the reverse order of their registration; no arguments are passed.
54 .PP
55 The same function may be registered multiple times:
56 it is called once for each registration.
57 .PP
58 POSIX.1 requires that an implementation allow at least
59 .\" POSIX.1-2001, POSIX.1-2008
60 .B ATEXIT_MAX
61 (32) such functions to be registered.
62 The actual limit supported by an implementation can be obtained using
63 .BR sysconf (3).
64 .PP
65 When a child process is created via
66 .BR fork (2),
67 it inherits copies of its parent's registrations.
68 Upon a successful call to one of the
69 .BR exec (3)
70 functions,
71 all registrations are removed.
72 .SH RETURN VALUE
73 The
74 .BR atexit ()
75 function returns the value 0 if successful; otherwise
76 it returns a nonzero value.
77 .SH ATTRIBUTES
78 For an explanation of the terms used in this section, see
79 .BR attributes (7).
80 .ad l
81 .nh
82 .TS
83 allbox;
84 lbx lb lb
85 l l l.
86 Interface       Attribute       Value
88 .BR atexit ()
89 T}      Thread safety   MT-Safe
90 .TE
91 .hy
92 .ad
93 .sp 1
94 .SH CONFORMING TO
95 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
96 .SH NOTES
97 Functions registered using
98 .BR atexit ()
99 (and
100 .BR on_exit (3))
101 are not called if a process terminates abnormally because
102 of the delivery of a signal.
104 If one of the registered functions calls
105 .BR _exit (2),
106 then any remaining functions are not invoked,
107 and the other process termination steps performed by
108 .BR exit (3)
109 are not performed.
111 POSIX.1 says that the result of calling
112 .\" POSIX.1-2001, POSIX.1-2008
113 .BR exit (3)
114 more than once (i.e., calling
115 .BR exit (3)
116 within a function registered using
117 .BR atexit ())
118 is undefined.
119 On some systems (but not Linux), this can result in an infinite recursion;
120 .\" This can happen on OpenBSD 4.2 for example, and is documented
121 .\" as occurring on FreeBSD as well.
122 .\" Glibc does "the Right Thing" -- invocation of the remaining
123 .\" exit handlers carries on as normal.
124 portable programs should not invoke
125 .BR exit (3)
126 inside a function registered using
127 .BR atexit ().
130 .BR atexit ()
132 .BR on_exit (3)
133 functions register functions on the same list:
134 at normal process termination,
135 the registered functions are invoked in reverse order
136 of their registration by these two functions.
138 According to POSIX.1, the result is undefined if
139 .BR longjmp (3)
140 is used to terminate execution of one of the functions registered using
141 .BR atexit ().
142 .\" In glibc, things seem to be handled okay
143 .SS Linux notes
144 Since glibc 2.2.3,
145 .BR atexit ()
146 (and
147 .BR on_exit (3))
148 can be used within a shared library to establish functions
149 that are called when the shared library is unloaded.
150 .SH EXAMPLES
152 #include <stdio.h>
153 #include <stdlib.h>
154 #include <unistd.h>
156 void
157 bye(void)
159     printf("That was all, folks\en");
163 main(void)
165     long a;
166     int i;
168     a = sysconf(_SC_ATEXIT_MAX);
169     printf("ATEXIT_MAX = %ld\en", a);
171     i = atexit(bye);
172     if (i != 0) {
173         fprintf(stderr, "cannot set exit function\en");
174         exit(EXIT_FAILURE);
175     }
177     exit(EXIT_SUCCESS);
180 .SH SEE ALSO
181 .BR _exit (2),
182 .BR dlopen (3),
183 .BR exit (3),
184 .BR on_exit (3)