pespin has uploaded this change for review.

View Change

core/exec: Use close_range() libc API when available

Using the libc call (which in turn uses a single system call) is far more efficient.

Change-Id: I6589564c406095a1e6584d8c0e65c28594326f31
---
M configure.ac
M src/core/exec.c
2 files changed, 15 insertions(+), 1 deletion(-)

git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/99/44099/1
diff --git a/configure.ac b/configure.ac
index cde4446..43bd2c1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -174,6 +174,11 @@
[[#define _GNU_SOURCE
#include <sched.h>]])

+dnl Check whether close_range() is available
+AC_CHECK_DECLS([close_range], [], [],
+ [[#define _GNU_SOURCE
+ #include <unistd.h>]])
+
AC_DEFUN([CHECK_TM_INCLUDES_TM_GMTOFF], [
AC_CACHE_CHECK(
[whether struct tm has tm_gmtoff member],
diff --git a/src/core/exec.c b/src/core/exec.c
index 2d1df82..c87c8cf 100644
--- a/src/core/exec.c
+++ b/src/core/exec.c
@@ -165,9 +165,17 @@
/* Iterate over files in /proc/self/fd and close all above lst_fd_to_keep */
int osmo_close_all_fds_above(int last_fd_to_keep)
{
+ int rc;
+
+#if HAVE_DECL_CLOSE_RANGE
+ rc = close_range(last_fd_to_keep + 1, ~0U, 0);
+ if (rc)
+ LOGP(DLGLOBAL, LOGL_ERROR, "Error close_range() fds=[%d..%u]: %s\n",
+ last_fd_to_keep + 1, ~0U, strerror(errno));
+ return 0;
+#else
struct dirent *ent;
DIR *dir;
- int rc;

dir = opendir("/proc/self/fd");
if (!dir) {
@@ -187,6 +195,7 @@
}
closedir(dir);
return 0;
+#endif
}

/* Seems like POSIX has no header file for this, and even glibc + __USE_GNU doesn't help */

To view, visit change 44099. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I6589564c406095a1e6584d8c0e65c28594326f31
Gerrit-Change-Number: 44099
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pespin@sysmocom.de>