xref: /freebsd/contrib/openbsm/compat/closefrom.h (revision b6a05070)
1*aa772005SRobert Watson /*-
2*aa772005SRobert Watson  * Copyright (c) 2012 The FreeBSD Foundation
3*aa772005SRobert Watson  * All rights reserved.
4*aa772005SRobert Watson  *
5*aa772005SRobert Watson  * This software was developed by Pawel Jakub Dawidek under sponsorship from
6*aa772005SRobert Watson  * the FreeBSD Foundation.
7*aa772005SRobert Watson  *
8*aa772005SRobert Watson  * Redistribution and use in source and binary forms, with or without
9*aa772005SRobert Watson  * modification, are permitted provided that the following conditions
10*aa772005SRobert Watson  * are met:
11*aa772005SRobert Watson  * 1. Redistributions of source code must retain the above copyright
12*aa772005SRobert Watson  *    notice, this list of conditions and the following disclaimer.
13*aa772005SRobert Watson  * 2. Redistributions in binary form must reproduce the above copyright
14*aa772005SRobert Watson  *    notice, this list of conditions and the following disclaimer in the
15*aa772005SRobert Watson  *    documentation and/or other materials provided with the distribution.
16*aa772005SRobert Watson  *
17*aa772005SRobert Watson  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18*aa772005SRobert Watson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*aa772005SRobert Watson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*aa772005SRobert Watson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21*aa772005SRobert Watson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*aa772005SRobert Watson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*aa772005SRobert Watson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*aa772005SRobert Watson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*aa772005SRobert Watson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*aa772005SRobert Watson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*aa772005SRobert Watson  * SUCH DAMAGE.
28*aa772005SRobert Watson  */
29*aa772005SRobert Watson 
30*aa772005SRobert Watson #ifndef	_CLOSEFROM_H_
31*aa772005SRobert Watson #define	_CLOSEFROM_H_
32*aa772005SRobert Watson 
33*aa772005SRobert Watson #include <unistd.h>
34*aa772005SRobert Watson 
35*aa772005SRobert Watson static void
closefrom(int lowfd)36*aa772005SRobert Watson closefrom(int lowfd)
37*aa772005SRobert Watson {
38*aa772005SRobert Watson 	int error, fd, maxfd;
39*aa772005SRobert Watson 
40*aa772005SRobert Watson 	error = errno;
41*aa772005SRobert Watson 
42*aa772005SRobert Watson 	maxfd = sysconf(_SC_OPEN_MAX);
43*aa772005SRobert Watson 	if (maxfd < 0)
44*aa772005SRobert Watson 		maxfd = 16384;
45*aa772005SRobert Watson 	for (fd = lowfd; fd <= maxfd; fd++)
46*aa772005SRobert Watson 		(void)close(fd);
47*aa772005SRobert Watson 
48*aa772005SRobert Watson 	errno = error;
49*aa772005SRobert Watson }
50*aa772005SRobert Watson 
51*aa772005SRobert Watson #endif /* !_CLOSEFROM_H_ */
52