diff options
author | Bernd Schubert <bschubert@ddn.com> | 2024-09-13 20:43:33 +0200 |
---|---|---|
committer | Bernd Schubert <bernd.schubert@fastmail.fm> | 2024-09-16 13:59:27 +0200 |
commit | 3fe1b25d51e74416acb26aaf495adb524ee61342 (patch) | |
tree | 96c1853910ea1370f65bb264ef22cb95978ab683 /util | |
parent | 5f137f01c1c8f97e9b3207a651090895e33a7dd3 (diff) | |
download | libfuse-3fe1b25d51e74416acb26aaf495adb524ee61342.tar.gz |
fusermount: Close file descriptors with close_range() if possible
close_range() is much more efficient.
Also remove the lower limit of 3 and set it to 0, as 0 to 1
might have been closed by the application and might be valid.
Diffstat (limited to 'util')
-rw-r--r-- | util/fusermount.c | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/util/fusermount.c b/util/fusermount.c index b8437a0..8528342 100644 --- a/util/fusermount.c +++ b/util/fusermount.c @@ -1456,8 +1456,33 @@ static void show_version(void) static void close_inherited_fds(int cfd) { int max_fd = sysconf(_SC_OPEN_MAX); + int rc; - for (int fd = 3; fd <= max_fd; fd++) { +#ifdef CLOSE_RANGE_CLOEXEC + /* high range first to be able to log errors through stdout/err*/ + rc = close_range(cfd + 1, ~0U, 0); + if (rc < 0) { + fprintf(stderr, "Failed to close high range of FDs: %s", + strerror(errno)); + goto fallback; + } + + rc = close_range(0, cfd - 1, 0); + if (rc < 0) { + fprintf(stderr, "Failed to close low range of FDs: %s", + strerror(errno)); + goto fallback; + } +#endif + +fallback: + /* + * This also needs to close stdout/stderr, as the application + * using libfuse might have closed these FDs and might be using + * it. Although issue is now that logging errors won't be possible + * after that. + */ + for (int fd = 0; fd <= max_fd; fd++) { if (fd != cfd) close(fd); } |