Skip to content

Commit

Permalink
clipmenud: avoid spawning multiple daemons
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
N-R-K authored and cdown committed Nov 9, 2024
1 parent 1113ca8 commit f7598af
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/clipmenud.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/file.h>
#include <sys/select.h>
#include <sys/signalfd.h>
#include <time.h>
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f7598af

Please sign in to comment.