From 63eb2393377792943b91b6f3eb17776c02db921b Mon Sep 17 00:00:00 2001 From: koekeishiya Date: Tue, 21 May 2019 17:34:53 +0200 Subject: #83 use fcntl instead of flock to acquire exclusive lock --- src/skhd.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/skhd.c b/src/skhd.c index 26334e8..abc9034 100644 --- a/src/skhd.c +++ b/src/skhd.c @@ -230,13 +230,21 @@ create_pid_file(void) error("skhd: could not create path to pid-file because 'env USER' was not set! abort..\n"); } - int handle = open(pid_file, O_CREAT | O_WRONLY, 0644); + int handle = open(pid_file, O_CREAT | O_RDWR, 0644); if (handle == -1) { error("skhd: could not create pid-file! abort..\n"); } - if (flock(handle, LOCK_EX | LOCK_NB) == -1) { - error("skhd: could not lock pid-file! abort..\n"); + struct flock lockfd = { + .l_start = 0, + .l_len = 0, + .l_pid = pid, + .l_type = F_WRLCK, + .l_whence = SEEK_SET + }; + + if (fcntl(handle, F_SETLK, &lockfd) == -1) { + error("skhd: could not lock pid-file! abort.. %d\n", errno); } else if (write(handle, &pid, sizeof(pid_t)) == -1) { error("skhd: could not write pid-file! abort..\n"); } -- cgit v1.2.3