From cae316c3dc72f127e9e98a71df3dcabad9ce30cb Mon Sep 17 00:00:00 2001 From: Jesper Jensen Date: Sun, 4 May 2025 20:07:01 +0200 Subject: Use atomic-rename trick to avoid clobbering state --- src/main.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 937e151..3169068 100644 --- a/src/main.c +++ b/src/main.c @@ -61,7 +61,7 @@ void sigint_handler(int sig) { #define CONF_ENO 1 void save_config() { - FILE* config = fopen("conf.dmp", "w"); + FILE* config = fopen("conf.dmp.tmp", "w"); if(config == NULL) fatal("Couldn't open config for writing"); @@ -77,8 +77,11 @@ void save_config() { if(fwrite(peer_table, sizeof(struct peer_entry), peer_table_size, config) != peer_table_size) fatal("Couldn't write peer table"); + fflush(config); if(fclose(config) != 0) fatal("Couldn't close config file"); + + rename("conf.dmp.tmp", "conf.dmp"); } int read_config() { -- cgit v1.2.3