From 3b39928f2a29aab3c93283d29cf4ab5e4cf6c16c Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Sun, 13 Aug 2023 17:41:03 +0200 Subject: irmd: Check lockfile first on init The IRMd will now check the lockfile before initializing any other internals. Signed-off-by: Dimitri Staessens Signed-off-by: Sander Vrijders --- src/irmd/main.c | 68 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/src/irmd/main.c b/src/irmd/main.c index 33f65cf4..99ab0b24 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -2445,6 +2445,14 @@ static void irm_fini(void) #endif } +static int ouroboros_reset(void) +{ + shm_rdrbuff_purge(); + lockfile_destroy(irmd.lf); + + return 0; +} + static int irm_init(void) { struct stat st; @@ -2456,6 +2464,37 @@ static int irm_init(void) log_init(!irmd.log_stdout); + irmd.lf = lockfile_create(); + if (irmd.lf == NULL) { + irmd.lf = lockfile_open(); + if (irmd.lf == NULL) { + log_err("Lockfile error."); + goto fail_lockfile; + } + + if (kill(lockfile_owner(irmd.lf), 0) < 0) { + log_warn("IRMd didn't properly shut down last time."); + if (ouroboros_reset() < 0) { + log_err("Failed to clean stale resources."); + lockfile_close(irmd.lf); + goto fail_lockfile; + } + + log_warn("Stale resources cleaned."); + irmd.lf = lockfile_create(); + } else { + log_warn("IRMd already running (%d), exiting.", + lockfile_owner(irmd.lf)); + lockfile_close(irmd.lf); + goto fail_lockfile; + } + } + + if (irmd.lf == NULL) { + log_err("Failed to create lockfile."); + goto fail_lockfile; + } + if (pthread_rwlock_init(&irmd.state_lock, NULL)) { log_err("Failed to initialize rwlock."); goto fail_state_lock; @@ -2506,31 +2545,6 @@ static int irm_init(void) goto fail_flow_ids; } - if ((irmd.lf = lockfile_create()) == NULL) { - if ((irmd.lf = lockfile_open()) == NULL) { - log_err("Lockfile error."); - goto fail_lockfile; - } - - if (kill(lockfile_owner(irmd.lf), 0) < 0) { - log_info("IRMd didn't properly shut down last time."); - shm_rdrbuff_purge(); - log_info("Stale resources cleaned."); - lockfile_destroy(irmd.lf); - irmd.lf = lockfile_create(); - } else { - log_info("IRMd already running (%d), exiting.", - lockfile_owner(irmd.lf)); - lockfile_close(irmd.lf); - goto fail_lockfile; - } - } - - if (irmd.lf == NULL) { - log_err("Failed to create lockfile."); - goto fail_lockfile; - } - if (stat(SOCK_PATH, &st) == -1) { if (mkdir(SOCK_PATH, 0777)) { log_err("Failed to create sockets directory."); @@ -2601,8 +2615,6 @@ static int irm_init(void) fail_sock_path: unlink(IRM_SOCK_PATH); fail_stat: - lockfile_destroy(irmd.lf); - fail_lockfile: bmp_destroy(irmd.flow_ids); fail_flow_ids: pthread_cond_destroy(&irmd.cmd_cond); @@ -2615,6 +2627,8 @@ static int irm_init(void) fail_reg_lock: pthread_rwlock_destroy(&irmd.state_lock); fail_state_lock: + lockfile_destroy(irmd.lf); + fail_lockfile: log_fini(); return -1; } -- cgit v1.2.3