blob: 389b41b42c9f5ec6c885bd180d19643163cf84e0 [file] [log] [blame]
Brad Bishop316dfdd2018-06-25 12:45:53 -04001From 307e28b4cb08b05bc044482058eeebc9f59bb9a9 Mon Sep 17 00:00:00 2001
2From: Kir Kolyshkin <kolyshkin@gmail.com>
3Date: Tue, 29 May 2018 18:09:27 -0700
4Subject: [PATCH 3/3] rpmSetCloseOnExec: use getrlimit()
5
6In case /proc is not available to get the actual list of opened fds,
7we fall back to iterating through the list of all possible fds.
8
9It is possible that during the course of the program execution the limit
10on number of open file descriptors might be lowered, so using the
11current limit, as returned by sysconf(_SC_OPEN_MAX), might omit some
12fds. Therefore, it is better to use rlim_max from the structure
13filled in by gertlimit(RLIMIT_NOFILE) to make sure we're checking
14all fds.
15
16This slows down the function, but only in the case /proc is not
17available, which should be rare in practice.
18
19Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
20Upstream-Status: Accepted [https://github.com/rpm-software-management/rpm/pull/444]
21Signed-off-by: Peter Kjellerstedt <peter.kjellerstedt@axis.com>
22---
23 rpmio/rpmio.c | 10 +++++++++-
24 1 file changed, 9 insertions(+), 1 deletion(-)
25
26diff --git a/rpmio/rpmio.c b/rpmio/rpmio.c
27index 55351c221..e051c9863 100644
28--- a/rpmio/rpmio.c
29+++ b/rpmio/rpmio.c
30@@ -10,6 +10,7 @@
31 #include <sys/personality.h>
32 #endif
33 #include <sys/utsname.h>
34+#include <sys/resource.h>
35
36 #include <rpm/rpmlog.h>
37 #include <rpm/rpmmacro.h>
38@@ -1778,7 +1779,14 @@ void rpmSetCloseOnExec(void)
39 DIR *dir = opendir("/proc/self/fd");
40 if (dir == NULL) { /* /proc not available */
41 /* iterate over all possible fds, might be slow */
42- int open_max = sysconf(_SC_OPEN_MAX);
43+ struct rlimit rl;
44+ int open_max;
45+
46+ if (getrlimit(RLIMIT_NOFILE, &rl) == 0 && rl.rlim_max != RLIM_INFINITY)
47+ open_max = rl.rlim_max;
48+ else
49+ open_max = sysconf(_SC_OPEN_MAX);
50+
51 if (open_max == -1)
52 open_max = 1024;
53