summaryrefslogtreecommitdiff
path: root/src/missing.cc
diff options
context:
space:
mode:
authorChristian Persch <chpe@src.gnome.org>2021-11-07 13:38:23 +0100
committerChristian Persch <chpe@src.gnome.org>2021-11-07 13:38:23 +0100
commitebecf6ac04488d0d697c255d582ea891c3bf1577 (patch)
tree2cd7469f1cc01d2ec6eca934ceb47eb33ade169f /src/missing.cc
parent86d004afa2bc563977e4ffd9a12181b58b770b0e (diff)
downloadvte-ebecf6ac04488d0d697c255d582ea891c3bf1577.tar.gz
lib: Check for close_range function and use it when available
This should enable using close_range also on non-linux platforms that do have close_range, e.g. freebsd and (soon) hurd. Part of https://gitlab.gnome.org/GNOME/vte/-/issues/2528 .
Diffstat (limited to 'src/missing.cc')
-rw-r--r--src/missing.cc29
1 files changed, 9 insertions, 20 deletions
diff --git a/src/missing.cc b/src/missing.cc
index 536b1e03..b5a0742e 100644
--- a/src/missing.cc
+++ b/src/missing.cc
@@ -134,17 +134,17 @@ getrlimit_NOFILE_max(void)
return RLIM_INFINITY;
}
-#ifdef __linux__
+#ifndef HAVE_CLOSE_RANGE
-static inline int
-_vte_close_range(int first_fd,
- int last_fd,
- unsigned flags)
+int
+close_range(unsigned int first_fd,
+ unsigned int last_fd,
+ unsigned int flags)
{
-#ifdef SYS_close_range
+#if defined(__linux__) && defined(SYS_close_range)
return syscall(SYS_close_range,
- unsigned(first_fd),
- last_fd == -1 ? ~0u : unsigned(last_fd),
+ first_fd,
+ last_fd == unsigned(-1) ? ~0u : last_fd,
flags);
#else
errno = ENOSYS;
@@ -152,7 +152,7 @@ _vte_close_range(int first_fd,
#endif
}
-#endif /* __linux__ */
+#endif /* !HAVE_CLOSE_RANGE */
/* This function is called between fork and execve/_exit and so must be
* async-signal-safe; see man:signal-safety(7).
@@ -170,17 +170,6 @@ fdwalk(int (*cb)(void *data, int fd),
#ifdef __linux__
- /* First, try close_range(CLOEXEC) which is faster than the methods
- * below, and works even if /proc is not available.
- */
- res = _vte_close_range(0, -1, CLOSE_RANGE_CLOEXEC);
- if (res == 0)
- return 0;
- if (res == -1 &&
- errno != ENOSYS /* old kernel */ &&
- errno != EINVAL /* flags not supported */)
- return res;
-
/* Fall back to iterating over /proc/self/fd.
* Avoid use of opendir/closedir since these are not async-signal-safe.
*/