From 000d6ce28584b1d319be7e4449bf506f16fe5d58 Mon Sep 17 00:00:00 2001 From: Guy Helmer Date: Mon, 6 Feb 2012 14:11:24 +0000 Subject: [PATCH] Using the O_CLOEXEC flag on open(2) caused the pidfile lock to be lost when the child process execs daemon's target program thanks to flock(2) semantics. So, we apparently have to leak the open pidfile's file descriptor to keep the lock for the pidfile(3) functions to work properly. Test case demonstrated by trociny: ref8-amd64:/home/trociny% uname -r 8.2-STABLE ref8-amd64:/home/trociny% daemon -p /tmp/sleep.pid sleep 10 ref8-amd64:/home/trociny% daemon -p /tmp/sleep.pid sleep 10 daemon: process already running, pid: 19799 kopusha:~% uname -r 10.0-CURRENT kopusha:~% daemon -p /tmp/sleep.pid sleep 10 kopusha:~% daemon -p /tmp/sleep.pid sleep 10 kopusha:~% --- lib/libutil/pidfile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/libutil/pidfile.c b/lib/libutil/pidfile.c index bca0315b219c..55e3e0fa1830 100644 --- a/lib/libutil/pidfile.c +++ b/lib/libutil/pidfile.c @@ -124,7 +124,7 @@ pidfile_open(const char *path, mode_t mode, pid_t *pidptr) * pidfile_write() can be called multiple times. */ fd = flopen(pfh->pf_path, - O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NONBLOCK, mode); + O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode); if (fd == -1) { if (errno == EWOULDBLOCK && pidptr != NULL) { count = 20;