Skip to content

Commit

Permalink
rpmSetCloseOnExec: use getrlimit()
Browse files Browse the repository at this point in the history
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 <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed May 18, 2018
1 parent e01f944 commit aa6cc04
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion rpmio/cloexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/resource.h>
#include "rpmutil.h"

static void set_cloexec(int fd)
Expand Down Expand Up @@ -39,7 +40,13 @@ void rpmSetCloseOnExec(void)

fallback:
// iterate over all possible fds
int open_max = sysconf(_SC_OPEN_MAX);
struct rlimit rl;
int open_max;

if (getrlimit(RLIMIT_NOFILE, &rl) == 0 && rl.rlim_max != RLIM_INFINITY)
open_max = rl.rlim_max;
else
open_max = sysconf(_SC_OPEN_MAX);
if (open_max == -1) {
open_max = 1024;
}
Expand Down

0 comments on commit aa6cc04

Please sign in to comment.