core: Fix possible deadlock due to get_max_fds.

* src/posix-io.c (get_max_fds): Do not use the Linux optimization.
--

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2017-02-03 13:13:22 +01:00
parent afc308598d
commit 93a59070c6
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B

View File

@ -287,37 +287,43 @@ get_max_fds (void)
int rc; int rc;
/* Under Linux we can figure out the highest used file descriptor by /* Under Linux we can figure out the highest used file descriptor by
* reading /proc/self/fd. This is in the common cases much fast than * reading /proc/self/fd. This is in the common cases much faster
* for example doing 4096 close calls where almost all of them will * than for example doing 4096 close calls where almost all of them
* fail. */ * will fail.
#ifdef __linux__ *
{ * Unfortunately we can't call opendir between fork and exec in a
DIR *dir = NULL; * multi-threaded process because opendir uses malloc and thus a
struct dirent *dir_entry; * mutex which may deadlock with a malloc in another thread. Thus
const char *s; * the code is not used until we can have a opendir variant which
int x; * does not use malloc. */
/* #ifdef __linux__ */
/* { */
/* DIR *dir = NULL; */
/* struct dirent *dir_entry; */
/* const char *s; */
/* int x; */
dir = opendir ("/proc/self/fd"); /* dir = opendir ("/proc/self/fd"); */
if (dir) /* if (dir) */
{ /* { */
while ((dir_entry = readdir (dir))) /* while ((dir_entry = readdir (dir))) */
{ /* { */
s = dir_entry->d_name; /* s = dir_entry->d_name; */
if ( *s < '0' || *s > '9') /* if ( *s < '0' || *s > '9') */
continue; /* continue; */
x = atoi (s); /* x = atoi (s); */
if (x > fds) /* if (x > fds) */
fds = x; /* fds = x; */
} /* } */
closedir (dir); /* closedir (dir); */
} /* } */
if (fds != -1) /* if (fds != -1) */
{ /* { */
fds++; /* fds++; */
source = "/proc"; /* source = "/proc"; */
} /* } */
} /* } */
#endif /* __linux__ */ /* #endif /\* __linux__ *\/ */
#ifdef RLIMIT_NOFILE #ifdef RLIMIT_NOFILE
if (fds == -1) if (fds == -1)