|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include <dirent.h> |
| 14 | +#include <unistd.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <sys/resource.h> |
| 17 | +#include <limits.h> |
| 18 | + |
| 19 | +#include "include/exec_helpers.h" |
| 20 | + |
| 21 | +// glibc < 2.34 needs this. When running in a container it's possible the userland |
| 22 | +// doesn't have this defined in glibc, but the kernel supports the syscall. It's |
| 23 | +// very trivial to check via invoking it and checking for ENOSYS. |
| 24 | +#ifndef SYS_close_range |
| 25 | +#define SYS_close_range 436 |
| 26 | +#endif |
| 27 | + |
| 28 | +static int highest_open_fd_dir(const char *fd_dir) { |
| 29 | + DIR *dir = opendir(fd_dir); |
| 30 | + if (dir == NULL) { |
| 31 | + return -1; |
| 32 | + } |
| 33 | + |
| 34 | + int highest_fd = 0; |
| 35 | + struct dirent *dir_entry = NULL; |
| 36 | + |
| 37 | + while ((dir_entry = readdir(dir)) != NULL) { |
| 38 | + if (dir_entry->d_name[0] < '0' || dir_entry->d_name[0] > '9') { |
| 39 | + continue; |
| 40 | + } |
| 41 | + |
| 42 | + int fd = atoi(dir_entry->d_name); |
| 43 | + if (fd > highest_fd) { |
| 44 | + highest_fd = fd; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + closedir(dir); |
| 49 | + return highest_fd; |
| 50 | +} |
| 51 | + |
| 52 | +static int get_highest_open_fd() { |
| 53 | + int highest_fd = -1; |
| 54 | + #if defined(__APPLE__) |
| 55 | + highest_fd = highest_open_fd_dir("/dev/fd"); |
| 56 | + #elif defined(__linux__) |
| 57 | + highest_fd = highest_open_fd_dir("/proc/self/fd"); |
| 58 | + #endif |
| 59 | + |
| 60 | + if (highest_fd != -1) { |
| 61 | + return highest_fd; |
| 62 | + } |
| 63 | + |
| 64 | + struct rlimit rl; |
| 65 | + if (getrlimit(RLIMIT_NOFILE, &rl) == 0) { |
| 66 | + return rl.rlim_cur; |
| 67 | + } |
| 68 | + |
| 69 | + // Fallback to sysconf if our pal above didn't work. |
| 70 | + return sysconf(_SC_OPEN_MAX); |
| 71 | +} |
| 72 | + |
| 73 | +void close_fds_from(unsigned int from) { |
| 74 | + #if defined(__FreeBSD__) || defined(__OpenBSD__) |
| 75 | + closefrom(from); |
| 76 | + return; |
| 77 | + #endif |
| 78 | + |
| 79 | + #if defined(__linux__) |
| 80 | + int ret = syscall(SYS_close_range, from, UINT_MAX, 0); |
| 81 | + if (ret == 0) { |
| 82 | + return; |
| 83 | + } |
| 84 | + #endif |
| 85 | + |
| 86 | + int highest_fd = get_highest_open_fd(); |
| 87 | + for (int i=from; i<highest_fd; i++) { |
| 88 | + close(i); |
| 89 | + } |
| 90 | +} |
0 commit comments