Project

General

Profile

Bug #2695 » 0009-server.c-call-ftruncate-on-pid-file_patch.txt

mackyle, 2015-12-03 23:05

 
1
From 2586bb88fb2d817ef5e2a2941ba326b893c143e2 Mon Sep 17 00:00:00 2001
2
From: "Kyle J. McKay" <mackyle@gmail.com>
3
Date: Thu, 3 Dec 2015 11:20:37 -0800
4
Subject: [PATCH] server.c: call ftruncate on pid file
5

    
6
If the server has changed its uid or is running in a chroot
7
it may be unable to remove the pid file when it exits.
8

    
9
However, if it holds on to an open handle to the pid file
10
that has write permission, it will be able to truncate the
11
pid file to 0 bytes in length.
12

    
13
Most monitoring software recognizes a 0-length pid file
14
as indicating there is no process running.
15

    
16
Therefore always attempt to truncate the pid file before
17
trying to remove it so that it's not left containing the
18
pid of a process that is no longer running.
19

    
20
Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
21
---
22
 src/server.c | 16 ++++++++++++++--
23
 1 file changed, 14 insertions(+), 2 deletions(-)
24

    
25
diff --git a/src/server.c b/src/server.c
26
index 853928a2..6200c94c 100644
27
--- a/src/server.c
28
+++ b/src/server.c
29
@@ -1007,8 +1007,6 @@ int main (int argc, char **argv) {
30
 			close(pid_fd);
31
 			return -1;
32
 		}
33
-		close(pid_fd);
34
-		pid_fd = -1;
35
 	}
36
 
37
 	/* Close stderr ASAP in the child process to make sure that nothing
38
@@ -1551,6 +1549,20 @@ int main (int argc, char **argv) {
39
 	}
40
 
41
 	if (!buffer_string_is_empty(srv->srvconf.pid_file) &&
42
+	    0 == graceful_shutdown && 0 <= pid_fd) {
43
+		if (0 != ftruncate(pid_fd, 0)) {
44
+			log_error_write(srv, __FILE__, __LINE__, "sbds",
45
+					"ftruncate failed for:",
46
+					srv->srvconf.pid_file,
47
+					errno,
48
+					strerror(errno));
49
+		}
50
+	}
51
+	if (0 <= pid_fd) {
52
+		close(pid_fd);
53
+		pid_fd = -1;
54
+	}
55
+	if (!buffer_string_is_empty(srv->srvconf.pid_file) &&
56
 	    buffer_string_is_empty(srv->srvconf.changeroot) &&
57
 	    0 == graceful_shutdown) {
58
 		if (0 != unlink(srv->srvconf.pid_file->ptr)) {
59
-- 
60
2.4.10
61

    
(1-1/4)