xref: /illumos-gate/usr/src/man/man2/close_range.2 (revision 5400c85432c0a689ecefb8c19b8f9861809ca093)
1*5400c854SAndy Fiddaman.\"
2*5400c854SAndy Fiddaman.\" This file and its contents are supplied under the terms of the
3*5400c854SAndy Fiddaman.\" Common Development and Distribution License ("CDDL"), version 1.0.
4*5400c854SAndy Fiddaman.\" You may only use this file in accordance with the terms of version
5*5400c854SAndy Fiddaman.\" 1.0 of the CDDL.
6*5400c854SAndy Fiddaman.\"
7*5400c854SAndy Fiddaman.\" A full copy of the text of the CDDL should have accompanied this
8*5400c854SAndy Fiddaman.\" source.  A copy of the CDDL is also available via the Internet at
9*5400c854SAndy Fiddaman.\" http://www.illumos.org/license/CDDL.
10*5400c854SAndy Fiddaman.\"
11*5400c854SAndy Fiddaman.\"
12*5400c854SAndy Fiddaman.\" Copyright 2026 Oxide Computer Company
13*5400c854SAndy Fiddaman.\"
14*5400c854SAndy Fiddaman.Dd June 10, 2026
15*5400c854SAndy Fiddaman.Dt CLOSE_RANGE 2
16*5400c854SAndy Fiddaman.Os
17*5400c854SAndy Fiddaman.Sh NAME
18*5400c854SAndy Fiddaman.Nm close_range
19*5400c854SAndy Fiddaman.Nd close or modify a range of file descriptors
20*5400c854SAndy Fiddaman.Sh SYNOPSIS
21*5400c854SAndy Fiddaman.In unistd.h
22*5400c854SAndy Fiddaman.Ft int
23*5400c854SAndy Fiddaman.Fo close_range
24*5400c854SAndy Fiddaman.Fa "unsigned int low"
25*5400c854SAndy Fiddaman.Fa "unsigned int high"
26*5400c854SAndy Fiddaman.Fa "int flags"
27*5400c854SAndy Fiddaman.Fc
28*5400c854SAndy Fiddaman.Sh DESCRIPTION
29*5400c854SAndy FiddamanThe
30*5400c854SAndy Fiddaman.Nm
31*5400c854SAndy Fiddamanfunction acts on every open file descriptor of the calling process in
32*5400c854SAndy Fiddamanthe inclusive range
33*5400c854SAndy Fiddaman.Fa low
34*5400c854SAndy Fiddamanto
35*5400c854SAndy Fiddaman.Fa high .
36*5400c854SAndy FiddamanWith a
37*5400c854SAndy Fiddaman.Fa flags
38*5400c854SAndy Fiddamanargument of zero, each such descriptor is closed, as if by
39*5400c854SAndy Fiddaman.Xr close 2 .
40*5400c854SAndy Fiddaman.Pp
41*5400c854SAndy FiddamanThe range may extend beyond the highest open file descriptor, and file
42*5400c854SAndy Fiddamandescriptors within the range that are not open are ignored; it is not
43*5400c854SAndy Fiddamanan error for the range to contain no open file descriptors at all.
44*5400c854SAndy FiddamanA
45*5400c854SAndy Fiddaman.Fa high
46*5400c854SAndy Fiddamanvalue of
47*5400c854SAndy Fiddaman.Dv UINT_MAX
48*5400c854SAndy Fiddamanacts on every open file descriptor at or above
49*5400c854SAndy Fiddaman.Fa low .
50*5400c854SAndy Fiddaman.Pp
51*5400c854SAndy FiddamanThe following values may be bitwise-ORed together in the
52*5400c854SAndy Fiddaman.Fa flags
53*5400c854SAndy Fiddamanargument to modify the behaviour of the function:
54*5400c854SAndy Fiddaman.Bl -tag -width Dv
55*5400c854SAndy Fiddaman.It Dv CLOSE_RANGE_CLOEXEC
56*5400c854SAndy FiddamanRather than closing the file descriptors in the range, set the
57*5400c854SAndy Fiddaman.Dv FD_CLOEXEC
58*5400c854SAndy Fiddamanflag on each of them, causing them to be closed by any subsequent
59*5400c854SAndy Fiddaman.Xr exec 2 .
60*5400c854SAndy Fiddaman.It Dv CLOSE_RANGE_CLOFORK
61*5400c854SAndy FiddamanRather than closing the file descriptors in the range, set the
62*5400c854SAndy Fiddaman.Dv FD_CLOFORK
63*5400c854SAndy Fiddamanflag on each of them, causing them to not be inherited by any
64*5400c854SAndy Fiddamansubsequently created child process.
65*5400c854SAndy Fiddaman.El
66*5400c854SAndy Fiddaman.Pp
67*5400c854SAndy FiddamanThe descriptor flags are described further in
68*5400c854SAndy Fiddaman.Xr fcntl 2 .
69*5400c854SAndy Fiddaman.Pp
70*5400c854SAndy FiddamanErrors encountered while closing individual file descriptors within the
71*5400c854SAndy Fiddamanrange are ignored.
72*5400c854SAndy Fiddaman.Sh RETURN VALUES
73*5400c854SAndy FiddamanUpon successful completion, the
74*5400c854SAndy Fiddaman.Nm
75*5400c854SAndy Fiddamanfunction returns
76*5400c854SAndy Fiddaman.Sy 0 .
77*5400c854SAndy FiddamanOtherwise,
78*5400c854SAndy Fiddaman.Sy -1
79*5400c854SAndy Fiddamanis returned and
80*5400c854SAndy Fiddaman.Va errno
81*5400c854SAndy Fiddamanis set to indicate the error.
82*5400c854SAndy Fiddaman.Sh EXAMPLES
83*5400c854SAndy Fiddaman.Sy Example 1
84*5400c854SAndy FiddamanEmulating
85*5400c854SAndy Fiddaman.Xr close 2 .
86*5400c854SAndy Fiddaman.Pp
87*5400c854SAndy FiddamanA range containing a single file descriptor closes just that descriptor.
88*5400c854SAndy FiddamanUnlike
89*5400c854SAndy Fiddaman.Xr close 2 ,
90*5400c854SAndy Fiddamanany error from the underlying close operation is ignored:
91*5400c854SAndy Fiddaman.Bd -literal -offset indent
92*5400c854SAndy Fiddaman(void) close_range(fd, fd, 0);
93*5400c854SAndy Fiddaman.Ed
94*5400c854SAndy Fiddaman.Pp
95*5400c854SAndy Fiddaman.Sy Example 2
96*5400c854SAndy FiddamanClosing every file descriptor at or above a given value.
97*5400c854SAndy Fiddaman.Pp
98*5400c854SAndy FiddamanThe following call closes all open file descriptors other than the
99*5400c854SAndy Fiddamanstandard input, output and error streams, the equivalent of
100*5400c854SAndy Fiddaman.Xr closefrom 3C
101*5400c854SAndy Fiddamanwith a
102*5400c854SAndy Fiddaman.Fa lowfd
103*5400c854SAndy Fiddamanargument of 3:
104*5400c854SAndy Fiddaman.Bd -literal -offset indent
105*5400c854SAndy Fiddaman(void) close_range(3, UINT_MAX, 0);
106*5400c854SAndy Fiddaman.Ed
107*5400c854SAndy Fiddaman.Sh ERRORS
108*5400c854SAndy FiddamanThe
109*5400c854SAndy Fiddaman.Nm
110*5400c854SAndy Fiddamanfunction will fail if:
111*5400c854SAndy Fiddaman.Bl -tag -width Er
112*5400c854SAndy Fiddaman.It Er EINVAL
113*5400c854SAndy Fiddaman.Fa low
114*5400c854SAndy Fiddamanis greater than
115*5400c854SAndy Fiddaman.Fa high ,
116*5400c854SAndy Fiddamanor
117*5400c854SAndy Fiddaman.Fa flags
118*5400c854SAndy Fiddamancontains an unsupported value.
119*5400c854SAndy Fiddaman.El
120*5400c854SAndy Fiddaman.Sh INTERFACE STABILITY
121*5400c854SAndy Fiddaman.Sy Committed
122*5400c854SAndy Fiddaman.Sh MT-LEVEL
123*5400c854SAndy Fiddaman.Sy Async-Signal-Safe
124*5400c854SAndy Fiddaman.Sh SEE ALSO
125*5400c854SAndy Fiddaman.Xr close 2 ,
126*5400c854SAndy Fiddaman.Xr exec 2 ,
127*5400c854SAndy Fiddaman.Xr fcntl 2 ,
128*5400c854SAndy Fiddaman.Xr fork 2 ,
129*5400c854SAndy Fiddaman.Xr closefrom 3C
130*5400c854SAndy Fiddaman.Sh STANDARDS
131*5400c854SAndy FiddamanThe
132*5400c854SAndy Fiddaman.Fn close_range
133*5400c854SAndy Fiddamanfunction is not part of any current standard.
134