OSDN Git Service

(split) Import translated manuals from JM CVS Repository.
[linuxjm/LDP_man-pages.git] / original / man3 / atexit.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\"     Linux libc source code
25 .\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\"     386BSD man pages
27 .\" Modified 1993-03-29, David Metcalfe
28 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
29 .\" Modified 2003-10-25, Walter Harms
30 .\"
31 .TH ATEXIT 3  2008-12-05 "Linux" "Linux Programmer's Manual"
32 .SH NAME
33 atexit \- register a function to be called at normal process termination
34 .SH SYNOPSIS
35 .nf
36 .B #include <stdlib.h>
37 .sp
38 .BI "int atexit(void (*" function )(void));
39 .fi
40 .SH DESCRIPTION
41 The
42 .BR atexit ()
43 function registers the given \fIfunction\fP to be
44 called at normal process termination, either via
45 .BR exit (3)
46 or via return from the program's \fImain\fP().
47 Functions so registered are called in
48 the reverse order of their registration; no arguments are passed.
49
50 The same function may be registered multiple times:
51 it is called once for each registration.
52 .LP
53 POSIX.1-2001 requires that an implementation allow at least
54 .B ATEXIT_MAX
55 (32) such functions to be registered.
56 The actual limit supported by an implementation can be obtained using
57 .BR sysconf (3).
58 .LP
59 When a child process is created via
60 .BR fork (2),
61 it inherits copies of its parent's registrations.
62 Upon a successful call to one of the
63 .BR exec (3)
64 functions,
65 all registrations are removed.
66 .SH "RETURN VALUE"
67 The
68 .BR atexit ()
69 function returns the value 0 if successful; otherwise
70 it returns a nonzero value.
71 .SH "CONFORMING TO"
72 SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
73 .SH NOTES
74 Functions registered using
75 .BR atexit ()
76 (and
77 .BR on_exit (3))
78 are not called if a process terminates abnormally because
79 of the delivery of a signal.
80
81 If one of the functions registered functions calls
82 .BR _exit (2),
83 then any remaining functions are not invoked,
84 and the other process termination steps performed by
85 .BR exit (3)
86 are not performed.
87
88 POSIX.1-2001 says that the result of calling
89 .BR exit (3)
90 more than once (i.e., calling
91 .BR exit (3)
92 within a function registered using
93 .BR atexit (3))
94 is undefined.
95 On some systems (but not Linux), this can result in an infinite recursion;
96 .\" This can happen on OpenBSD 4.2 for example, and is documented
97 .\" as occurring on FreeBSD as well.
98 .\" Glibc does "the Right Thing" -- invocation of the remaining
99 .\" exit handlers carries on as normal.
100 portable programs should not invoke
101 .BR exit (3)
102 inside a function registered using
103 .BR atexit (3).
104
105 The
106 .BR atexit ()
107 and
108 .BR on_exit (3)
109 functions register functions on the same list:
110 at normal process termination,
111 the registered functions are invoked in reverse order
112 of their registration by these two functions.
113
114 POSIX.1-2001 says that the result is undefined if
115 .BR longjmp (3)
116 is used to terminate execution of one of the functions registered
117 .BR atexit ().
118 .\" In glibc, things seem to be handled okay
119 .SS "Linux Notes"
120 Since glibc 2.2.3,
121 .BR atexit ()
122 (and
123 .BR on_exit (3))
124 can be used within a shared library to establish functions
125 that are called when the shared library is unloaded.
126 .SH EXAMPLE
127 .nf
128 #include <stdio.h>
129 #include <stdlib.h>
130 #include <unistd.h>
131
132 void
133 bye(void)
134 {
135     printf("That was all, folks\en");
136 }
137
138 int
139 main(void)
140 {
141     long a;
142     int i;
143
144     a = sysconf(_SC_ATEXIT_MAX);
145     printf("ATEXIT_MAX = %ld\en", a);
146
147     i = atexit(bye);
148     if (i != 0) {
149         fprintf(stderr, "cannot set exit function\en");
150         exit(EXIT_FAILURE);
151     }
152
153     exit(EXIT_SUCCESS);
154 }
155 .fi
156 .SH "SEE ALSO"
157 .BR _exit (2),
158 .BR exit (3),
159 .BR on_exit (3)