summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorJesper Jensen <jesper@jnsn.dev>2025-05-04 20:07:01 +0200
committerJesper Jensen <jesper@jnsn.dev>2025-05-04 20:07:01 +0200
commitcae316c3dc72f127e9e98a71df3dcabad9ce30cb (patch)
treebe00ee83ccd60f84d0bc556e7ba04d9812785088 /src/main.c
parent605a97b83950acaa98c67623e7e31e6df438d16e (diff)
Use atomic-rename trick to avoid clobbering state
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c5
1 files changed, 4 insertions, 1 deletions
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() {