aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkoekeishiya <aasvi93@hotmail.com>2019-05-21 17:34:53 +0200
committerkoekeishiya <aasvi93@hotmail.com>2019-05-21 17:34:53 +0200
commit63eb2393377792943b91b6f3eb17776c02db921b (patch)
tree2d967c3653e46150628ada75850b1bf862f54aac
parent97a2ea9744dfc1946bc022389caae834b62a298f (diff)
downloadskhd-63eb2393377792943b91b6f3eb17776c02db921b.tar.gz
skhd-63eb2393377792943b91b6f3eb17776c02db921b.zip
#83 use fcntl instead of flock to acquire exclusive lock
-rw-r--r--src/skhd.c14
1 files 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");
}