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