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
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/>.
29 static int closefrom_sysconf(int lower)
33 max_files = sysconf(_SC_OPEN_MAX);
34 if (max_files == -1) {
38 for (fd=lower; fd<max_files; fd++) {
45 static int closefrom_procfs(int lower)
52 size_t fd_array_size = 0;
56 dirp = opendir("/proc/self/fd");
67 while ((dp = readdir(dirp)) != NULL) {
69 unsigned long long fd;
73 fd = strtoull(dp->d_name, &endptr, 10);
74 if ((fd == 0) && (errno == EINVAL)) {
77 if ((fd == ULLONG_MAX) && (errno == ERANGE)) {
80 if (*endptr != '\0') {
93 if (num_fds >= (fd_array_size / sizeof(int))) {
96 if (fd_array_size == 0) {
97 fd_array_size = 16 * sizeof(int);
99 if (fd_array_size + fd_array_size <
104 fd_array_size = fd_array_size + fd_array_size;
107 tmp = realloc(fds, fd_array_size);
117 for (i=0; i<num_fds; i++) {
128 int rep_closefrom(int lower)
132 ret = closefrom_procfs(lower);
137 return closefrom_sysconf(lower);