about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRichard Nyberg <rnyberg@murmeldjur.se>2006-02-10 15:38:14 +0000
committerRichard Nyberg <rnyberg@murmeldjur.se>2006-02-10 15:38:14 +0000
commitbf75d6af1c573fea1043c81cdeb2c634dedb240c (patch)
treed9f39d5141832f5bd466f95df8875646ee566017
parent7dedb28137dd5c08c30ca27f4e576fd8f9f3b593 (diff)
downloadbtpd-bf75d6af1c573fea1043c81cdeb2c634dedb240c.tar.gz
btpd-bf75d6af1c573fea1043c81cdeb2c634dedb240c.zip
Glibc doesn't support locking in open. Use flock after open instead.
-rw-r--r--btpd/main.c14
1 files changed, 6 insertions, 8 deletions
diff --git a/btpd/main.c b/btpd/main.c
index 7d11d64..97690fe 100644
--- a/btpd/main.c
+++ b/btpd/main.c
@@ -5,6 +5,7 @@
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <file.h>
 #include <getopt.h>
 #include <locale.h>
 #include <stdio.h>
@@ -45,14 +46,11 @@ setup_daemon(int daemonize, const char *dir, const char *log)
     if (mkdir("torrents", 0777) == -1 && errno != EEXIST)
         err(1, "Couldn't create torrents subdir");
 
-    pidfd = open("pid", O_CREAT|O_WRONLY|O_NONBLOCK|O_EXLOCK, 0666);
-    if (pidfd == -1) {
-        if (errno == EAGAIN)
-            errx(1, "Another instance of btpd is probably running in %s.",
-                dir);
-        else
-            err(1, "Couldn't open 'pid'");
-    }
+    if ((pidfd = open("pid", O_CREAT|O_WRONLY, 0666)) == -1)
+        err(1, "Couldn't open 'pid'");
+
+    if (flock(pidfd, LOCK_NB|LOCK_EX) == -1)
+        errx(1, "Another instance of btpd is probably running in %s.", dir);
 
     if (daemonize) {
         if (daemon(1, 1) != 0)