[master] 14721f537 On platforms without closefrom() try to find out the highest open fd by reading /proc/`getpid`/fd directory.
Poul-Henning Kamp
phk at FreeBSD.org
Mon Mar 4 23:09:06 UTC 2019
commit 14721f5379ea644cec138dc73958ecbfcee09888
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Mon Mar 4 23:06:43 2019 +0000
On platforms without closefrom() try to find out the
highest open fd by reading /proc/`getpid`/fd directory.
If it fails we fall back to sysconf(_SC_OPEN_MAX) as usual.
diff --git a/lib/libvarnish/vsub.c b/lib/libvarnish/vsub.c
index 06457992c..fc2083cbb 100644
--- a/lib/libvarnish/vsub.c
+++ b/lib/libvarnish/vsub.c
@@ -33,10 +33,14 @@
#include <sys/wait.h>
+#include <stdio.h>
#include <stdint.h>
#include <stdlib.h> // Solaris closefrom(3c)
#include <string.h>
#include <unistd.h>
+#ifndef HAVE_CLOSEFROM
+# include <dirent.h>
+#endif
#include "vdef.h"
@@ -61,8 +65,32 @@ VSUB_closefrom(int fd)
#ifdef HAVE_CLOSEFROM
closefrom(fd);
+ return;
#else
- int i = sysconf(_SC_OPEN_MAX);
+ char buf[128];
+ int i, maxfd = 0;
+ DIR *d;
+ struct dirent *de;
+ char *p;
+
+ bprintf(buf, "/proc/%d/fd/", getpid());
+ d = opendir(buf);
+ if (d != NULL) {
+ while (1) {
+ de = readdir(d);
+ if (de == NULL)
+ break;
+ i = strtoul(de->d_name, &p, 10);
+ if (*p != '\0')
+ continue;
+ if (i > maxfd)
+ maxfd = i;
+ }
+ AZ(closedir(d));
+ }
+
+ if (maxfd == 0)
+ maxfd = sysconf(_SC_OPEN_MAX);
assert(i > 0);
for (; i > fd; i--)
(void)close(i);
More information about the varnish-commit
mailing list