OSDN Git Service

(split) LDP: Change Makefile to stamp-based compilation
[linuxjm/LDP_man-pages.git] / original / man3 / atexit.3
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  2008-12-05 "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 .sp
40 .BI "int atexit(void (*" function )(void));
41 .fi
42 .SH DESCRIPTION
43 The
44 .BR atexit ()
45 function registers the given \fIfunction\fP to be
46 called at normal process termination, either via
47 .BR exit (3)
48 or via return from the program's \fImain\fP().
49 Functions so registered are called in
50 the reverse order of their registration; no arguments are passed.
51
52 The same function may be registered multiple times:
53 it is called once for each registration.
54 .LP
55 POSIX.1-2001 requires that an implementation allow at least
56 .B ATEXIT_MAX
57 (32) such functions to be registered.
58 The actual limit supported by an implementation can be obtained using
59 .BR sysconf (3).
60 .LP
61 When a child process is created via
62 .BR fork (2),
63 it inherits copies of its parent's registrations.
64 Upon a successful call to one of the
65 .BR exec (3)
66 functions,
67 all registrations are removed.
68 .SH RETURN VALUE
69 The
70 .BR atexit ()
71 function returns the value 0 if successful; otherwise
72 it returns a nonzero value.
73 .SH CONFORMING TO
74 SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
75 .SH NOTES
76 Functions registered using
77 .BR atexit ()
78 (and
79 .BR on_exit (3))
80 are not called if a process terminates abnormally because
81 of the delivery of a signal.
82
83 If one of the functions registered functions calls
84 .BR _exit (2),
85 then any remaining functions are not invoked,
86 and the other process termination steps performed by
87 .BR exit (3)
88 are not performed.
89
90 POSIX.1-2001 says that the result of calling
91 .BR exit (3)
92 more than once (i.e., calling
93 .BR exit (3)
94 within a function registered using
95 .BR atexit ())
96 is undefined.
97 On some systems (but not Linux), this can result in an infinite recursion;
98 .\" This can happen on OpenBSD 4.2 for example, and is documented
99 .\" as occurring on FreeBSD as well.
100 .\" Glibc does "the Right Thing" -- invocation of the remaining
101 .\" exit handlers carries on as normal.
102 portable programs should not invoke
103 .BR exit (3)
104 inside a function registered using
105 .BR atexit ().
106
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
116 POSIX.1-2001 says that the result is undefined if
117 .BR longjmp (3)
118 is used to terminate execution of one of the functions registered
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 EXAMPLE
129 .nf
130 #include <stdio.h>
131 #include <stdlib.h>
132 #include <unistd.h>
133
134 void
135 bye(void)
136 {
137     printf("That was all, folks\en");
138 }
139
140 int
141 main(void)
142 {
143     long a;
144     int i;
145
146     a = sysconf(_SC_ATEXIT_MAX);
147     printf("ATEXIT_MAX = %ld\en", a);
148
149     i = atexit(bye);
150     if (i != 0) {
151         fprintf(stderr, "cannot set exit function\en");
152         exit(EXIT_FAILURE);
153     }
154
155     exit(EXIT_SUCCESS);
156 }
157 .fi
158 .SH SEE ALSO
159 .BR _exit (2),
160 .BR exit (3),
161 .BR on_exit (3)