xref: /freebsd-14.2/lib/libc/stdlib/exit.3 (revision 2a1e8d7c)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"
33.Dd July 24, 2024
34.Dt EXIT 3
35.Os
36.Sh NAME
37.Nm exit , _Exit
38.Nd perform normal program termination
39.Sh LIBRARY
40.Lb libc
41.Sh SYNOPSIS
42.In stdlib.h
43.Ft void
44.Fn exit "int status"
45.Ft void
46.Fn _Exit "int status"
47.Sh DESCRIPTION
48The
49.Fn exit
50and
51.Fn _Exit
52functions terminate a process.
53.Pp
54Before termination,
55.Fn exit
56performs the following functions in the order listed:
57.Bl -enum -offset indent
58.It
59Call all functions registered with the
60.Xr __cxa_atexit 3
61function
62(which are typically destructors from the loaded dynamic objects),
63and the functions registered with the
64.Xr atexit 3
65function, in the reverse order of their registration.
66.It
67Flush all open output streams.
68.It
69Close all open streams.
70.El
71.Pp
72The
73.Fn _Exit
74function terminates without calling the functions registered with the
75.Xr atexit 3
76function, and may or may not perform the other actions listed.
77The
78.Fx
79implementation of the
80.Fn _Exit
81function does not call destructors registered with
82.Xr __cxa_atexit 3,
83does not flush buffers, and does not close streams.
84.Pp
85Both functions make the low-order eight bits of the
86.Fa status
87argument available to a parent process which has called a
88.Xr wait 2 Ns -family
89function.
90.Pp
91The C Standard
92.Pq St -isoC-99
93defines the values
94.Li 0 ,
95.Dv EXIT_SUCCESS ,
96and
97.Dv EXIT_FAILURE
98as possible values of
99.Fa status .
100Cooperating processes may use other values;
101in a program which might be called by a mail transfer agent, the
102values described in
103.Xr sysexits 3
104may be used to provide more information to the parent process.
105.Pp
106The complete
107.Fa status
108value is avaliable as
109.Va si_status
110member of the
111.Vt siginfo_t
112structure, to the
113.Xr wait6 2
114and
115.Xr sigwaitinfo 2
116callers, and
117.Va SIGCHLD
118signal handlers.
119.Pp
120Calls to the
121.Fn exit
122function are serialized.
123All functions registered by
124.Xr atexit 3
125are executed in the first thread that called
126.Nm exit .
127If any other thread of the process calls
128.Nm exit
129before all registered functions have completed or before the process
130terminates, the thread is blocked until the process terminates.
131The exit status of the process is the
132.Fa status
133argument of the first
134.Nm exit
135call which thread proceeds the atexit handlers.
136.Pp
137Note that
138.Fn exit
139does nothing to prevent bottomless recursion should a function registered
140using
141.Xr atexit 3
142itself call
143.Fn exit .
144Such functions must call
145.Fn _Exit
146instead (although this has other effects as well which may not be desired).
147.Sh RETURN VALUES
148The
149.Fn exit
150and
151.Fn _Exit
152functions
153never return.
154.Sh SEE ALSO
155.Xr _exit 2 ,
156.Xr abort2 2 ,
157.Xr wait 2 ,
158.Xr at_quick_exit 3 ,
159.Xr atexit 3 ,
160.Xr intro 3 ,
161.Xr quick_exit 3 ,
162.Xr sysexits 3 ,
163.Xr tmpfile 3
164.Sh STANDARDS
165The
166.Fn exit
167and
168.Fn _Exit
169functions conform to
170.St -isoC-99 .
171.Sh HISTORY
172The
173.Fn exit
174function appeared in
175.At v1 .
176