diff --git a/cmd/zed/zed_file.c b/cmd/zed/zed_file.c index 4437892e90..c7b0d00b6b 100644 --- a/cmd/zed/zed_file.c +++ b/cmd/zed/zed_file.c @@ -12,6 +12,7 @@ * You may not use this file except in compliance with the license. */ +#include #include #include #include @@ -160,6 +161,13 @@ zed_file_is_locked(int fd) return (lock.l_pid); } + +#if __APPLE__ +#define PROC_SELF_FD "/dev/fd" +#else /* Linux-compatible layout */ +#define PROC_SELF_FD "/proc/self/fd" +#endif + /* * Close all open file descriptors greater than or equal to [lowfd]. * Any errors encountered while closing file descriptors are ignored. @@ -167,17 +175,23 @@ zed_file_is_locked(int fd) void zed_file_close_from(int lowfd) { - const int maxfd_def = 256; - int errno_bak; + static const int maxfd_def = 256; + int errno_bak = errno; struct rlimit rl; - int maxfd; + int maxfd = 0; int fd; + DIR *fddir; + struct dirent *fdent; - errno_bak = errno; - - if (getrlimit(RLIMIT_NOFILE, &rl) < 0) { - maxfd = maxfd_def; - } else if (rl.rlim_max == RLIM_INFINITY) { + if ((fddir = opendir(PROC_SELF_FD)) != NULL) { + while ((fdent = readdir(fddir)) != NULL) { + fd = atoi(fdent->d_name); + if (fd > maxfd && fd != dirfd(fddir)) + maxfd = fd; + } + (void) closedir(fddir); + } else if (getrlimit(RLIMIT_NOFILE, &rl) < 0 || + rl.rlim_max == RLIM_INFINITY) { maxfd = maxfd_def; } else { maxfd = rl.rlim_max;