From a205fe19b78d44170661dbb624b5166220254f72 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 10 Sep 2007 16:52:17 +0000 Subject: if opening the pid file on O_RDWR doesn't work, fallback to O_RDONLY git-svn-id: file:///home/lennart/svn/public/libdaemon/trunk@131 153bfa13-eec0-0310-be40-b0cb6a0e1b4b --- libdaemon/dpid.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/libdaemon/dpid.c b/libdaemon/dpid.c index 125fada..39c87f4 100644 --- a/libdaemon/dpid.c +++ b/libdaemon/dpid.c @@ -69,6 +69,14 @@ static int lock_file(int fd, int enable) { f.l_len = 0; if (fcntl(fd, F_SETLKW, &f) < 0) { + + if (enable && errno == EBADF) { + f.l_type = F_RDLCK; + + if (fcntl(fd, F_SETLKW, &f) >= 0) + return 0; + } + daemon_log(LOG_WARNING, "fcntl(F_SETLKW) failed: %s", strerror(errno)); return -1; } @@ -91,10 +99,12 @@ pid_t daemon_pid_file_is_running(void) { } if ((fd = open(fn, O_RDWR, 0644)) < 0) { - if (errno != ENOENT) - daemon_log(LOG_WARNING, "Failed to open PID file: %s", strerror(errno)); + if ((fd = open(fn, O_RDONLY, 0644)) < 0) { + if (errno != ENOENT) + daemon_log(LOG_WARNING, "Failed to open PID file: %s", strerror(errno)); - goto finish; + goto finish; + } } if ((locked = lock_file(fd, 1)) < 0) -- cgit