Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 1 | From 307e28b4cb08b05bc044482058eeebc9f59bb9a9 Mon Sep 17 00:00:00 2001 |
| 2 | From: Kir Kolyshkin <kolyshkin@gmail.com> |
| 3 | Date: Tue, 29 May 2018 18:09:27 -0700 |
| 4 | Subject: [PATCH 3/3] rpmSetCloseOnExec: use getrlimit() |
| 5 | |
| 6 | In case /proc is not available to get the actual list of opened fds, |
| 7 | we fall back to iterating through the list of all possible fds. |
| 8 | |
| 9 | It is possible that during the course of the program execution the limit |
| 10 | on number of open file descriptors might be lowered, so using the |
| 11 | current limit, as returned by sysconf(_SC_OPEN_MAX), might omit some |
| 12 | fds. Therefore, it is better to use rlim_max from the structure |
| 13 | filled in by gertlimit(RLIMIT_NOFILE) to make sure we're checking |
| 14 | all fds. |
| 15 | |
| 16 | This slows down the function, but only in the case /proc is not |
| 17 | available, which should be rare in practice. |
| 18 | |
| 19 | Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com> |
| 20 | Upstream-Status: Accepted [https://github.com/rpm-software-management/rpm/pull/444] |
| 21 | Signed-off-by: Peter Kjellerstedt <peter.kjellerstedt@axis.com> |
| 22 | --- |
| 23 | rpmio/rpmio.c | 10 +++++++++- |
| 24 | 1 file changed, 9 insertions(+), 1 deletion(-) |
| 25 | |
| 26 | diff --git a/rpmio/rpmio.c b/rpmio/rpmio.c |
| 27 | index 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 | |