Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clipmenud: avoid spawning multiple daemons #221

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 =
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably also O_CLOEXEC?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. But does clipmenud do any exec? Also what about content_dir_fd and snip_fd, shouldn't they be O_CLOEXECed too if it's just out of precaution/principle?

Copy link
Owner

@cdown cdown Jun 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O_CLOEXEC is more important on flock()ed files than regular files because flock() maintains locking semantics across process images, so (for example) clipserve may then end up holding the lock. It would be cleaner elsewhere, too, but less essential.

We do fork()/exec() on run_clipserve().

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
Loading