Search for location of waf script
[Samba.git] / lib / replace / closefrom.c
bloba61a80f1b0ff10995d0f65d81181dd1b5da1afc3
1 /*
2 * Unix SMB/CIFS implementation.
3 * Samba utility functions
4 * Copyright (C) Volker Lendecke 2016
6 * ** NOTE! The following LGPL license applies to the replace
7 * ** library. This does NOT imply that all of Samba is released
8 * ** under the LGPL
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 3 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 #include "replace.h"
25 #include <dirent.h>
26 #include <unistd.h>
27 #include <limits.h>
29 static int closefrom_sysconf(int lower)
31 long max_files, fd;
33 max_files = sysconf(_SC_OPEN_MAX);
34 if (max_files == -1) {
35 max_files = 65536;
38 for (fd=lower; fd<max_files; fd++) {
39 close(fd);
42 return 0;
45 static int closefrom_procfs(int lower)
47 DIR *dirp;
48 int dir_fd;
49 struct dirent *dp;
50 int *fds = NULL;
51 size_t num_fds = 0;
52 size_t fd_array_size = 0;
53 size_t i;
54 int ret = ENOMEM;
56 dirp = opendir("/proc/self/fd");
57 if (dirp == 0) {
58 return errno;
61 dir_fd = dirfd(dirp);
62 if (dir_fd == -1) {
63 ret = errno;
64 goto fail;
67 while ((dp = readdir(dirp)) != NULL) {
68 char *endptr;
69 unsigned long long fd;
71 errno = 0;
73 fd = strtoull(dp->d_name, &endptr, 10);
74 if ((fd == 0) && (errno == EINVAL)) {
75 continue;
77 if ((fd == ULLONG_MAX) && (errno == ERANGE)) {
78 continue;
80 if (*endptr != '\0') {
81 continue;
83 if (fd == dir_fd) {
84 continue;
86 if (fd > INT_MAX) {
87 continue;
89 if (fd < lower) {
90 continue;
93 if (num_fds >= (fd_array_size / sizeof(int))) {
94 void *tmp;
96 if (fd_array_size == 0) {
97 fd_array_size = 16 * sizeof(int);
98 } else {
99 if (fd_array_size + fd_array_size <
100 fd_array_size) {
101 /* overflow */
102 goto fail;
104 fd_array_size = fd_array_size + fd_array_size;
107 tmp = realloc(fds, fd_array_size);
108 if (tmp == NULL) {
109 goto fail;
111 fds = tmp;
114 fds[num_fds++] = fd;
117 for (i=0; i<num_fds; i++) {
118 close(fds[i]);
121 ret = 0;
122 fail:
123 closedir(dirp);
124 free(fds);
125 return ret;
128 int rep_closefrom(int lower)
130 int ret;
132 ret = closefrom_procfs(lower);
133 if (ret == 0) {
134 return 0;
137 return closefrom_sysconf(lower);