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 committed Jun 6, 2024
1 parent f5b67c0 commit 80b2120
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 @@ -384,6 +385,14 @@ int main(void) {
int evt_base;

cfg = setup("clipmenud");

_drop_(close) int session_fd =
open(get_session_lock_path(&cfg), O_WRONLY | O_CREAT, 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 80b2120

Please sign in to comment.