Project

General

Profile

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

mackyle, 2015-12-03 23:05

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

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

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

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

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

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

diff --git a/src/server.c b/src/server.c
index 853928a2..6200c94c 100644
--- a/src/server.c
+++ b/src/server.c
@@ -1007,8 +1007,6 @@ int main (int argc, char **argv) {
close(pid_fd);
return -1;
}
- close(pid_fd);
- pid_fd = -1;
}
/* Close stderr ASAP in the child process to make sure that nothing
@@ -1551,6 +1549,20 @@ int main (int argc, char **argv) {
}
if (!buffer_string_is_empty(srv->srvconf.pid_file) &&
+ 0 == graceful_shutdown && 0 <= pid_fd) {
+ if (0 != ftruncate(pid_fd, 0)) {
+ log_error_write(srv, __FILE__, __LINE__, "sbds",
+ "ftruncate failed for:",
+ srv->srvconf.pid_file,
+ errno,
+ strerror(errno));
+ }
+ }
+ if (0 <= pid_fd) {
+ close(pid_fd);
+ pid_fd = -1;
+ }
+ if (!buffer_string_is_empty(srv->srvconf.pid_file) &&
buffer_string_is_empty(srv->srvconf.changeroot) &&
0 == graceful_shutdown) {
if (0 != unlink(srv->srvconf.pid_file->ptr)) {
--
2.4.10

(1-1/4)