aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstrongswan/utils.c
diff options
context:
space:
mode:
authorTobias Brunner <tobias@strongswan.org>2011-10-13 11:10:05 +0200
committerTobias Brunner <tobias@strongswan.org>2011-10-13 11:21:57 +0200
commit5051bd23eb81c8ac53a0966badcd6a02479f3601 (patch)
tree59544c63de0ca7665591d708c1abb5bd96abff03 /src/libstrongswan/utils.c
parent652ddf5ce2fad08f6569096dd56a821500cc5ba4 (diff)
downloadstrongswan-5051bd23eb81c8ac53a0966badcd6a02479f3601.tar.bz2
strongswan-5051bd23eb81c8ac53a0966badcd6a02479f3601.tar.xz
Only close open file descriptors on Linux.
Diffstat (limited to 'src/libstrongswan/utils.c')
-rw-r--r--src/libstrongswan/utils.c27
1 files changed, 26 insertions, 1 deletions
diff --git a/src/libstrongswan/utils.c b/src/libstrongswan/utils.c
index eca613a5d..7ad948f70 100644
--- a/src/libstrongswan/utils.c
+++ b/src/libstrongswan/utils.c
@@ -28,6 +28,7 @@
#include "enum.h"
#include "debug.h"
+#include "utils/enumerator.h"
ENUM(status_names, SUCCESS, NEED_MORE,
"SUCCESS",
@@ -199,7 +200,31 @@ bool mkdir_p(const char *path, mode_t mode)
*/
void closefrom(int lowfd)
{
- int maxfd, fd;
+ char fd_dir[PATH_MAX];
+ int maxfd, fd, len;
+
+ /* try to close only open file descriptors on Linux... */
+ len = snprintf(fd_dir, sizeof(fd_dir), "/proc/%u/fd", getpid());
+ if (len > 0 && len < sizeof(fd_dir))
+ {
+ enumerator_t *enumerator = enumerator_create_directory(fd_dir);
+ if (enumerator)
+ {
+ char *rel, *end;
+ while (enumerator->enumerate(enumerator, &rel, NULL, NULL))
+ {
+ fd = atoi(rel);
+ if (fd >= lowfd)
+ {
+ close(fd);
+ }
+ }
+ enumerator->destroy(enumerator);
+ return;
+ }
+ }
+
+ /* ...fall back to closing all fds otherwise */
maxfd = (int)sysconf(_SC_OPEN_MAX);
if (maxfd < 0)
{