summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2020-05-27 20:16:33 +0200
committerBruno Haible <bruno@clisp.org>2020-05-28 21:40:23 +0200
commiteaab07cf200cfca9bcff5dc166639a159d2ed361 (patch)
treeef2ba6c99f0dc99934986f7589771d768c2c0af2 /lib
parente5f59849aeb762dd2636b2cda85f1b392a938481 (diff)
downloadgnulib-eaab07cf200cfca9bcff5dc166639a159d2ed361.tar.gz
getloadavg: Make more robust in multithreaded applications.
* lib/getloadavg.c (getloadavg): Pass an O_CLOEXEC flag to open(). Simplify use of O_CLOEXEC. * modules/getloadavg (Depends-on): Add 'open'.
Diffstat (limited to 'lib')
-rw-r--r--lib/getloadavg.c22
1 files changed, 3 insertions, 19 deletions
diff --git a/lib/getloadavg.c b/lib/getloadavg.c
index 7e11c3208e..61307d3772 100644
--- a/lib/getloadavg.c
+++ b/lib/getloadavg.c
@@ -512,7 +512,7 @@ getloadavg (double loadavg[], int nelem)
char const *ptr = ldavgbuf;
int fd, count, saved_errno;
- fd = open (LINUX_LDAV_FILE, O_RDONLY);
+ fd = open (LINUX_LDAV_FILE, O_RDONLY | O_CLOEXEC);
if (fd == -1)
return -1;
count = read (fd, ldavgbuf, sizeof ldavgbuf - 1);
@@ -869,27 +869,11 @@ getloadavg (double loadavg[], int nelem)
if (!getloadavg_initialized)
{
# ifndef SUNOS_5
- /* Set the channel to close on exec, so it does not
- litter any child's descriptor table. */
-# ifndef O_CLOEXEC
-# define O_CLOEXEC 0
-# endif
int fd = open ("/dev/kmem", O_RDONLY | O_CLOEXEC);
if (0 <= fd)
{
-# if F_DUPFD_CLOEXEC
- if (fd <= STDERR_FILENO)
- {
- int fd1 = fcntl (fd, F_DUPFD_CLOEXEC, STDERR_FILENO + 1);
- close (fd);
- fd = fd1;
- }
-# endif
- if (0 <= fd)
- {
- channel = fd;
- getloadavg_initialized = true;
- }
+ channel = fd;
+ getloadavg_initialized = true;
}
# else /* SUNOS_5 */
/* We pass 0 for the kernel, corefile, and swapfile names