From 78608dc2faa58dd55a961868bedfa7d9e67938b7 Mon Sep 17 00:00:00 2001 From: NRK Date: Thu, 6 Jun 2024 20:36:44 +0000 Subject: [PATCH] clipmenud: avoid spawning multiple daemons things can get messy if a running clipmenud is diabled via clipctl and then another clipmenud daemon is started. avoid spawning multiple daemons by using a session lock similar to the old bash script. --- src/clipmenud.c | 9 +++++++++ src/config.h | 1 + 2 files changed, 10 insertions(+) diff --git a/src/clipmenud.c b/src/clipmenud.c index 3a1f718..118e5b2 100644 --- a/src/clipmenud.c +++ b/src/clipmenud.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -385,6 +386,14 @@ int main(int argc, char *argv[]) { int evt_base; cfg = setup("clipmenud"); + + _drop_(close) int session_fd = + open(get_session_lock_path(&cfg), O_WRONLY | O_CREAT | O_CLOEXEC, 0600); + die_on(session_fd < 0, "Failed to open session file: %s\n", + strerror(errno)); + die_on(flock(session_fd, LOCK_EX | LOCK_NB) < 0, + "Failed to lock session file -- is another clipmenud running?\n"); + write_status(); _drop_(close) int content_dir_fd = open(get_cache_dir(&cfg), O_RDONLY); diff --git a/src/config.h b/src/config.h index e464ab2..55759ee 100644 --- a/src/config.h +++ b/src/config.h @@ -79,6 +79,7 @@ char *get_cache_dir(struct config *cfg); DEFINE_GET_PATH_FUNCTION(line_cache) DEFINE_GET_PATH_FUNCTION(enabled) +DEFINE_GET_PATH_FUNCTION(session_lock) extern const char *prog_name; struct config _nonnull_ setup(const char *inner_prog_name);