xref: /netbsd-src/external/bsd/cron/dist/closeall.c (revision 065057e63525be61eed7190f73cb58a269959e81)
1*065057e6Schristos #include <unistd.h>
2*065057e6Schristos #include <errno.h>
3*065057e6Schristos #include <fcntl.h>
4*065057e6Schristos 
5*065057e6Schristos #ifdef __linux__
6*065057e6Schristos #include <linux/limits.h>
7*065057e6Schristos #endif
8*065057e6Schristos 
9*065057e6Schristos #include "cron.h"
10*065057e6Schristos 
close_all(int start)11*065057e6Schristos int close_all(int start)
12*065057e6Schristos {
13*065057e6Schristos #ifdef F_CLOSEM
14*065057e6Schristos 	return fcntl(start, F_CLOSEM);
15*065057e6Schristos #else
16*065057e6Schristos 	int fd, max;
17*065057e6Schristos 
18*065057e6Schristos 	max = sysconf(_SC_OPEN_MAX);
19*065057e6Schristos 	if (max <= 0)
20*065057e6Schristos 		return -1;
21*065057e6Schristos 
22*065057e6Schristos #ifdef __linux__
23*065057e6Schristos 	if (max < NR_OPEN)
24*065057e6Schristos 		max = NR_OPEN;
25*065057e6Schristos #endif
26*065057e6Schristos 
27*065057e6Schristos 	for (fd = start; fd < max; fd++) {
28*065057e6Schristos 		if (close(fd) && errno != EBADF)
29*065057e6Schristos 			return -1;
30*065057e6Schristos 	}
31*065057e6Schristos 
32*065057e6Schristos 	return 0;
33*065057e6Schristos #endif
34*065057e6Schristos }
35