From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id C91403857364 for ; Mon, 9 May 2022 22:57:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C91403857364 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from reform (deer0x07.wildebeest.org [172.31.17.137]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 1B88930007D1; Tue, 10 May 2022 00:57:54 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id AAC0A2E8237E; Tue, 10 May 2022 00:57:53 +0200 (CEST) From: Mark Wielaard To: elfutils-devel@sourceware.org Cc: Mark Wielaard Subject: [PATCH 1/3] debuginfod: Make sure debuginfod_config_cache always returns valid stat Date: Tue, 10 May 2022 00:57:21 +0200 Message-Id: <20220509225723.96902-2-mark@klomp.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220509225723.96902-1-mark@klomp.org> References: <20220509225723.96902-1-mark@klomp.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 09 May 2022 22:57:57 -0000 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 --- debuginfod/ChangeLog | 6 ++++++ debuginfod/debuginfod-client.c | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/debuginfod/ChangeLog b/debuginfod/ChangeLog index 257ac39f..46a6e22b 100644 --- a/debuginfod/ChangeLog +++ b/debuginfod/ChangeLog @@ -1,3 +1,9 @@ +2022-05-09 Mark Wielaard + + * 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 * debuginfod.cxx (conninfo): Always provide servname to getnameinfo. 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; } -- 2.30.2