summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Wielaard <mark@klomp.org>2022-05-08 23:55:19 +0200
committerMark Wielaard <mark@klomp.org>2022-05-15 00:18:18 +0200
commita6b3e9dd4e725e1500872f3726f97954b81d59ad (patch)
treef2540f376fa40092c94c11a63c4a4b4844a6d9d5
parent4b5c8d1380715123f1b832fca932722e8c6651d2 (diff)
downloadelfutils-a6b3e9dd4e725e1500872f3726f97954b81d59ad.tar.gz
debuginfod: Make sure debuginfod_config_cache always returns valid stat
If the condig file which value was requested from debuginfod_config_cache didn't exist yet, stat would fail and no valid struct stat would be returned even when the file was correctly created. Fix this by always using O_CREAT to open the file, and reuse that file descriptor to call fstat and for either writing the default value or reading the config file value. Signed-off-by: Mark Wielaard <mark@klomp.org>
-rw-r--r--debuginfod/ChangeLog6
-rw-r--r--debuginfod/debuginfod-client.c21
2 files changed, 20 insertions, 7 deletions
diff --git a/debuginfod/ChangeLog b/debuginfod/ChangeLog
index 39f7d731..26c1a076 100644
--- a/debuginfod/ChangeLog
+++ b/debuginfod/ChangeLog
@@ -1,5 +1,11 @@
2022-05-09 Mark Wielaard <mark@klomp.org>
+ * debuginfod-client.c (debuginfod_config_cache): Always open with
+ O_CREATE first, then use fstat, only write the cache_config_default_s
+ value if st_size == 0, otherwise read value from file.
+
+2022-05-09 Mark Wielaard <mark@klomp.org>
+
* debuginfod.cxx (conninfo): Always provide servname to getnameinfo.
2022-05-09 Mark Wielaard <mark@klomp.org>
diff --git a/debuginfod/debuginfod-client.c b/debuginfod/debuginfod-client.c
index 89208216..3b2728f1 100644
--- a/debuginfod/debuginfod-client.c
+++ b/debuginfod/debuginfod-client.c
@@ -235,14 +235,19 @@ debuginfod_config_cache(char *config_path,
long cache_config_default_s,
struct stat *st)
{
- int fd;
- /* if the config file doesn't exist, create one with DEFFILEMODE*/
- if(stat(config_path, st) == -1)
+ int fd = open(config_path, O_CREAT | O_RDWR, DEFFILEMODE);
+ if (fd < 0)
+ return -errno;
+
+ if (fstat (fd, st) < 0)
{
- fd = open(config_path, O_CREAT | O_RDWR, DEFFILEMODE);
- if (fd < 0)
- return -errno;
+ int ret = -errno;
+ close (fd);
+ return ret;
+ }
+ if (st->st_size == 0)
+ {
if (dprintf(fd, "%ld", cache_config_default_s) < 0)
{
int ret = -errno;
@@ -251,10 +256,11 @@ debuginfod_config_cache(char *config_path,
}
close (fd);
+ return cache_config_default_s;
}
long cache_config;
- FILE *config_file = fopen(config_path, "r");
+ FILE *config_file = fdopen(fd, "r");
if (config_file)
{
if (fscanf(config_file, "%ld", &cache_config) != 1)
@@ -264,6 +270,7 @@ debuginfod_config_cache(char *config_path,
else
cache_config = cache_config_default_s;
+ close (fd);
return cache_config;
}