diff options
| author | Jesper Jensen <jesper@jnsn.dev> | 2025-05-04 20:07:01 +0200 |
|---|---|---|
| committer | Jesper Jensen <jesper@jnsn.dev> | 2025-05-04 20:07:01 +0200 |
| commit | cae316c3dc72f127e9e98a71df3dcabad9ce30cb (patch) | |
| tree | be00ee83ccd60f84d0bc556e7ba04d9812785088 | |
| parent | 605a97b83950acaa98c67623e7e31e6df438d16e (diff) | |
Use atomic-rename trick to avoid clobbering state
| -rw-r--r-- | src/main.c | 5 |
1 files changed, 4 insertions, 1 deletions
@@ -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() { |
