summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDimitri Staessens <[email protected]>2023-08-13 17:41:03 +0200
committerSander Vrijders <[email protected]>2023-08-23 13:09:10 +0200
commit3b39928f2a29aab3c93283d29cf4ab5e4cf6c16c (patch)
treec5f0bd70b5650c218126568492c82cb4387ba92e /src
parentbd0db9cde25c4490e3ef435842f5638b76cc59a1 (diff)
downloadouroboros-3b39928f2a29aab3c93283d29cf4ab5e4cf6c16c.tar.gz
ouroboros-3b39928f2a29aab3c93283d29cf4ab5e4cf6c16c.zip
irmd: Check lockfile first on init
The IRMd will now check the lockfile before initializing any other internals. Signed-off-by: Dimitri Staessens <[email protected]> Signed-off-by: Sander Vrijders <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/irmd/main.c68
1 files 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;
}