]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/atexit.3
434aa301f41a516087c72abfcf564146eb7950fb
[thirdparty/man-pages.git] / 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 2003-11-01 "" "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 \fBatexit\fP() function registers the given \fIfunction\fP to be
42 called at normal process termination, either via
43 .BR exit (3)
44 or via return from the program's \fImain\fP().
45 Functions so registered are called in
46 the reverse order of their registration; no arguments are passed.
47 .LP
48 POSIX.1-2001 requires that an implementation allow at least ATEXIT_MAX (32)
49 such functions to be registered.
50 The actual limit supported by an implementation can be obtained using
51 .BR sysconf (3).
52 .LP
53 When a child process is created via
54 .BR fork (),
55 it inherits copies of the its parents registrations.
56 Upon a successful call to one of the
57 .BR exec ()
58 functions,
59 all registrations are removed.
60 .SH "RETURN VALUE"
61 The \fBatexit\fP() function returns the value 0 if successful; otherwise
62 it returns a non-zero value.
63 .SH EXAMPLE
64 .nf
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <unistd.h>
68
69 void bye(void) {
70 printf("That was all, folks\en");
71 }
72
73 int main(){
74 long a;
75 int i;
76
77 a = sysconf(_SC_ATEXIT_MAX);
78 printf("ATEXIT_MAX = %ld\en", a);
79
80 i = atexit(bye);
81 if (i != 0) {
82 fprintf(stderr, "cannot set exit function\en");
83 return EXIT_FAILURE;
84 }
85 return EXIT_SUCCESS;
86 }
87 .fi
88 .SH NOTES
89 Since glibc 2.2.3, \fBatexit\fP() (and \fBon_exit\fP())
90 can be used to within a shared library to establish functions
91 that are called when the shared library is unloaded.
92 .PP
93 Functions registered using \fBatexit\fP() (and \fBon_exit\fP())
94 are not called if a process terminates abnormally because
95 of the delivery of a signal.
96 .SH "CONFORMING TO"
97 SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
98 .SH "SEE ALSO"
99 .BR _exit (3),
100 .BR exit (3),
101 .BR on_exit (3)