Skip to content

Commit aa6cc04

Browse files
committed
rpmSetCloseOnExec: use getrlimit()
In case /proc is not available to get the actual list of opened fds, we fall back to iterating the list of all possible fds. It is possible that during the course of the program execution the limit on number of open file descriptors might be lowered, so using the current limit, as returned by sysconf(_SC_OPEN_MAX), might omit some fds. Therefore, use rlim_max from the structure filled in by gertlimit(RLIMIT_NOFILE) to make sure we're checking all fds. This slows down the function, but only in the case /proc is not available, which should be rare in practice. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent e01f944 commit aa6cc04

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

rpmio/cloexec.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <unistd.h>
44
#include <sys/types.h>
55
#include <dirent.h>
6+
#include <sys/resource.h>
67
#include "rpmutil.h"
78

89
static void set_cloexec(int fd)
@@ -39,7 +40,13 @@ void rpmSetCloseOnExec(void)
3940

4041
fallback:
4142
// iterate over all possible fds
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);
4350
if (open_max == -1) {
4451
open_max = 1024;
4552
}

0 commit comments

Comments
 (0)