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 #include #include +#include #include #include #include @@ -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)