1daemon.c: Fix race window for writing of the pid file
2
3The parent process should write the pid file such that the pid file
4will can be checked immediately following exit of the fork from the
5parent.
6
7This allows external monitoring applications to watch the daemon
8without having to add sleep calls to wait for the pid file be written
9on a busy system.
10
11Signed-off-by: Jason Wessel <jason.wessel@windriver.com>
12
13Upstream-Status: Submitted http://sourceforge.net/p/unfs3/bugs/5/
14
15---
16 daemon.c |   12 +++++++++---
17 1 file changed, 9 insertions(+), 3 deletions(-)
18
19--- a/daemon.c
20+++ b/daemon.c
21@@ -153,7 +153,7 @@ int get_socket_type(struct svc_req *rqst
22 /*
23  * write current pid to a file
24  */
25-static void create_pid_file(void)
26+static void create_pid_file(int pid)
27 {
28     char buf[16];
29     int fd, res, len;
30@@ -175,7 +175,7 @@ static void create_pid_file(void)
31     }
32 #endif
33
34-    sprintf(buf, "%i\n", backend_getpid());
35+    sprintf(buf, "%i\n", pid);
36     len = strlen(buf);
37
38     res = backend_pwrite(fd, buf, len, 0);
39@@ -970,6 +970,10 @@ int main(int argc, char **argv)
40 	    fprintf(stderr, "could not fork into background\n");
41 	    daemon_exit(0);
42 	}
43+	if (pid)
44+	    create_pid_file(pid);
45+    } else {
46+	create_pid_file(backend_getpid());
47     }
48 #endif				       /* WIN32 */
49
50@@ -1006,8 +1010,10 @@ int main(int argc, char **argv)
51 	/* no umask to not screw up create modes */
52 	umask(0);
53
54+#ifdef WIN32
55 	/* create pid file if wanted */
56-	create_pid_file();
57+	create_pid_file(backend_getpid());
58+#endif
59
60 	/* initialize internal stuff */
61 	fh_cache_init();
62