man-pages/man3/atexit.3

160 lines
4.5 KiB
Groff
Raw Normal View History

2004-11-03 13:51:07 +00:00
.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
2004-11-03 13:51:07 +00:00
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
2004-11-03 13:51:07 +00:00
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.\" References consulted:
.\" Linux libc source code
.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
.\" 386BSD man pages
.\" Modified 1993-03-29, David Metcalfe
.\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
.\" Modified 2003-10-25, Walter Harms
.\"
.TH ATEXIT 3 2008-12-05 "Linux" "Linux Programmer's Manual"
2004-11-03 13:51:07 +00:00
.SH NAME
2005-06-14 11:22:06 +00:00
atexit \- register a function to be called at normal process termination
2004-11-03 13:51:07 +00:00
.SH SYNOPSIS
.nf
.B #include <stdlib.h>
.sp
.BI "int atexit(void (*" function )(void));
.fi
.SH DESCRIPTION
The
.BR atexit ()
function registers the given \fIfunction\fP to be
2005-06-14 11:22:06 +00:00
called at normal process termination, either via
2004-11-03 13:51:07 +00:00
.BR exit (3)
or via return from the program's \fImain\fP().
2004-11-03 13:51:07 +00:00
Functions so registered are called in
the reverse order of their registration; no arguments are passed.
The same function may be registered multiple times:
it is called once for each registration.
2004-11-03 13:51:07 +00:00
.LP
2007-07-13 08:32:28 +00:00
POSIX.1-2001 requires that an implementation allow at least
.B ATEXIT_MAX
(32) such functions to be registered.
2005-06-14 11:22:06 +00:00
The actual limit supported by an implementation can be obtained using
2004-11-03 13:51:07 +00:00
.BR sysconf (3).
.LP
When a child process is created via
.BR fork (2),
2007-05-30 15:34:47 +00:00
it inherits copies of its parent's registrations.
Upon a successful call to one of the
2007-05-21 21:23:17 +00:00
.BR exec (3)
2005-10-20 15:11:10 +00:00
functions,
all registrations are removed.
2004-11-03 13:51:07 +00:00
.SH "RETURN VALUE"
The
.BR atexit ()
function returns the value 0 if successful; otherwise
intro.1, time.1, adjtimex.2, capget.2, eventfd.2, fcntl.2, getrlimit.2, getsockopt.2, gettimeofday.2, intro.2, ioctl_list.2, ioperm.2, mlock.2, pivot_root.2, poll.2, prctl.2, ptrace.2, sched_setscheduler.2, select_tut.2, semget.2, sigaltstack.2, signalfd.2, sysctl.2, timer_settime.2, timerfd_create.2, wait.2, CPU_SET.3, argz_add.3, assert_perror.3, atexit.3, backtrace.3, bcmp.3, clearenv.3, ctime.3, dl_iterate_phdr.3, dlopen.3, ecvt.3, errno.3, error.3, ether_aton.3, exit.3, fenv.3, ferror.3, finite.3, flockfile.3, fnmatch.3, fpathconf.3, fpclassify.3, ftime.3, ftok.3, ftw.3, fwide.3, getaddrinfo.3, gethostbyname.3, getlogin.3, getnameinfo.3, getnetent.3, getopt.3, getprotoent.3, getrpcent.3, getservent.3, glob.3, hsearch.3, inet.3, isalpha.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, longjmp.3, lsearch.3, malloc.3, matherr.3, mblen.3, mbsinit.3, mbtowc.3, on_exit.3, printf.3, pthread_attr_init.3, pthread_attr_setaffinity_np.3, pthread_attr_setdetachstate.3, pthread_attr_setguardsize.3, pthread_attr_setinheritsched.3, pthread_attr_setschedparam.3, pthread_attr_setschedpolicy.3, pthread_attr_setscope.3, pthread_attr_setstack.3, pthread_attr_setstackaddr.3, pthread_attr_setstacksize.3, pthread_cancel.3, pthread_cleanup_push.3, pthread_equal.3, pthread_getattr_np.3, pthread_getcpuclockid.3, pthread_setaffinity_np.3, pthread_setcancelstate.3, pthread_setconcurrency.3, pthread_setschedparam.3, pthread_setschedprio.3, ptsname.3, putenv.3, putgrent.3, raise.3, rcmd.3, regex.3, rexec.3, rpc.3, rpmatch.3, rtnetlink.3, scandir.3, sem_init.3, setaliasent.3, setbuf.3, setenv.3, setjmp.3, signbit.3, stdio_ext.3, strtod.3, strtol.3, strtoul.3, system.3, termios.3, timeradd.3, tzset.3, ualarm.3, wctomb.3, xdr.3, st.4, tty_ioctl.4, core.5, elf.5, proc.5, bootparam.7, capabilities.7, icmp.7, ip.7, ipv6.7, math_error.7, mdoc.samples.7, mq_overview.7, pthreads.7, raw.7, regex.7, socket.7, tcp.7, tzselect.8: Global fix: s/non-zero/nonzero/ The tendency in English, as prescribed in style guides like Chicago MoS, is towards removing hyphens after prefixes like "non-" etc. Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:40:55 +00:00
it returns a nonzero value.
.SH "CONFORMING TO"
SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
.SH NOTES
Functions registered using
.BR atexit ()
(and
.BR on_exit (3))
are not called if a process terminates abnormally because
of the delivery of a signal.
If one of the functions registered functions calls
.BR _exit (2),
then any remaining functions are not invoked,
and the other process termination steps performed by
.BR exit (3)
are not performed.
POSIX.1-2001 says that the result of calling
.BR exit (3)
more than once (i.e., calling
.BR exit (3)
within a function registered using
.BR atexit ())
is undefined.
On some systems (but not Linux), this can result in an infinite recursion;
.\" This can happen on OpenBSD 4.2 for example, and is documented
.\" as occurring on FreeBSD as well.
.\" Glibc does "the Right Thing" -- invocation of the remaining
.\" exit handlers carries on as normal.
portable programs should not invoke
.BR exit (3)
inside a function registered using
.BR atexit ().
The
.BR atexit ()
and
.BR on_exit (3)
functions register functions on the same list:
at normal process termination,
the registered functions are invoked in reverse order
of their registration by these two functions.
POSIX.1-2001 says that the result is undefined if
.BR longjmp (3)
is used to terminate execution of one of the functions registered
.BR atexit ().
.\" In glibc, things seem to be handled okay
intro.1, time.1, _syscall.2, accept.2, brk.2, capget.2, faccessat.2, futimesat.2, gethostname.2, intro.2, ioprio_set.2, kill.2, madvise.2, mlock.2, mount.2, msgget.2, perf_event_open.2, poll.2, readv.2, sched_rr_get_interval.2, sched_setscheduler.2, select.2, select_tut.2, shmget.2, stat.2, timer_create.2, ustat.2, vfork.2, wait.2, alloca.3, atexit.3, catopen.3, crypt.3, dbopen.3, fenv.3, fopen.3, getaddrinfo_a.3, gethostbyname.3, getutent.3, intro.3, login.3, mallopt.3, queue.3, regex.3, sigpause.3, sigsetops.3, stdio.3, strftime.3, strptime.3, sysconf.3, ttyslot.3, undocumented.3, cciss.4, console_codes.4, hpsa.4, initrd.4, intro.4, rtc.4, st.4, tty_ioctl.4, wavelan.4, acct.5, charmap.5, dir_colors.5, hosts.5, intro.5, termcap.5, utmp.5, intro.6, armscii-8.7, boot.7, bootparam.7, capabilities.7, cpuset.7, credentials.7, ddp.7, epoll.7, glob.7, intro.7, ip.7, ipv6.7, iso_8859-1.7, iso_8859-10.7, iso_8859-11.7, iso_8859-13.7, iso_8859-14.7, iso_8859-15.7, iso_8859-16.7, iso_8859-2.7, iso_8859-3.7, iso_8859-4.7, iso_8859-5.7, iso_8859-6.7, iso_8859-7.7, iso_8859-8.7, iso_8859-9.7, koi8-r.7, koi8-u.7, man-pages.7, man.7, math_error.7, netlink.7, numa.7, packet.7, path_resolution.7, pipe.7, pthreads.7, raw.7, rtnetlink.7, signal.7, socket.7, spufs.7, svipc.7, tcp.7, time.7, udp.7, udplite.7, unicode.7, unix.7, uri.7, utf-8.7, x25.7, intro.8: Global fix: use consistent capitalization in .SS headings Capitalization in .SS sections across pages (and sometimes even within a single page) is wildly inconsistent. Make it consistent. Capitalize first word in heading, but otherwise use lower case, except where English usage (e.g., proper nouns) or programming language requirements (e.g., identifier names) dictate otherwise. Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2013-02-24 14:08:38 +00:00
.SS Linux notes
Since glibc 2.2.3,
.BR atexit ()
(and
.BR on_exit (3))
can be used within a shared library to establish functions
that are called when the shared library is unloaded.
2004-11-03 13:51:07 +00:00
.SH EXAMPLE
.nf
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void
bye(void)
2007-04-05 12:36:57 +00:00
{
printf("That was all, folks\en");
2004-11-03 13:51:07 +00:00
}
int
2007-04-05 12:36:57 +00:00
main(void)
{
long a;
int i;
2004-11-03 13:51:07 +00:00
2007-04-05 12:36:57 +00:00
a = sysconf(_SC_ATEXIT_MAX);
printf("ATEXIT_MAX = %ld\en", a);
2004-11-03 13:51:07 +00:00
2007-04-05 12:36:57 +00:00
i = atexit(bye);
if (i != 0) {
fprintf(stderr, "cannot set exit function\en");
2007-12-25 16:24:14 +00:00
exit(EXIT_FAILURE);
2007-04-05 12:36:57 +00:00
}
2007-12-25 16:24:14 +00:00
exit(EXIT_SUCCESS);
2004-11-03 13:51:07 +00:00
}
.fi
.SH "SEE ALSO"
2007-06-01 06:08:17 +00:00
.BR _exit (2),
2004-11-03 13:51:07 +00:00
.BR exit (3),
.BR on_exit (3)