1
|
From 3fdb9db0c1662abafa5de3005ad9a23b289bc8a0 Mon Sep 17 00:00:00 2001
|
2
|
From: "Kyle J. McKay" <mackyle@gmail.com>
|
3
|
Date: Fri, 4 Dec 2015 16:45:25 -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 | 56 +++++++++++++++++++++++++++++++++++++++++---------------
|
23
|
1 file changed, 41 insertions(+), 15 deletions(-)
|
24
|
|
25
|
diff --git a/src/server.c b/src/server.c
|
26
|
index c5845e2c..2764c9a5 100644
|
27
|
--- a/src/server.c
|
28
|
+++ b/src/server.c
|
29
|
@@ -718,6 +718,7 @@ int main (int argc, char **argv) {
|
30
|
return -1;
|
31
|
}
|
32
|
}
|
33
|
+ fd_close_on_exec(pid_fd);
|
34
|
}
|
35
|
|
36
|
if (srv->event_handler == FDEVENT_HANDLER_SELECT) {
|
37
|
@@ -1012,8 +1013,6 @@ int main (int argc, char **argv) {
|
38
|
close(pid_fd);
|
39
|
return -1;
|
40
|
}
|
41
|
- close(pid_fd);
|
42
|
- pid_fd = -1;
|
43
|
}
|
44
|
|
45
|
/* Close stderr ASAP in the child process to make sure that nothing
|
46
|
@@ -1437,23 +1436,36 @@ int main (int argc, char **argv) {
|
47
|
srv_socket->fd = -1;
|
48
|
|
49
|
/* network_close() will cleanup after us */
|
50
|
-
|
51
|
- if (!buffer_string_is_empty(srv->srvconf.pid_file) &&
|
52
|
- buffer_string_is_empty(srv->srvconf.changeroot)) {
|
53
|
- if (0 != unlink(srv->srvconf.pid_file->ptr)) {
|
54
|
- if (errno != EACCES && errno != EPERM) {
|
55
|
- log_error_write(srv, __FILE__, __LINE__, "sbds",
|
56
|
- "unlink failed for:",
|
57
|
- srv->srvconf.pid_file,
|
58
|
- errno,
|
59
|
- strerror(errno));
|
60
|
- }
|
61
|
- }
|
62
|
- }
|
63
|
}
|
64
|
}
|
65
|
|
66
|
if (graceful_shutdown) {
|
67
|
+ if (!buffer_string_is_empty(srv->srvconf.pid_file) &&
|
68
|
+ 0 <= pid_fd) {
|
69
|
+ if (0 != ftruncate(pid_fd, 0)) {
|
70
|
+ log_error_write(srv, __FILE__, __LINE__, "sbds",
|
71
|
+ "ftruncate failed for:",
|
72
|
+ srv->srvconf.pid_file,
|
73
|
+ errno,
|
74
|
+ strerror(errno));
|
75
|
+ }
|
76
|
+ }
|
77
|
+ if (0 <= pid_fd) {
|
78
|
+ close(pid_fd);
|
79
|
+ pid_fd = -1;
|
80
|
+ }
|
81
|
+ if (!buffer_string_is_empty(srv->srvconf.pid_file) &&
|
82
|
+ buffer_string_is_empty(srv->srvconf.changeroot)) {
|
83
|
+ if (0 != unlink(srv->srvconf.pid_file->ptr)) {
|
84
|
+ if (errno != EACCES && errno != EPERM) {
|
85
|
+ log_error_write(srv, __FILE__, __LINE__, "sbds",
|
86
|
+ "unlink failed for:",
|
87
|
+ srv->srvconf.pid_file,
|
88
|
+ errno,
|
89
|
+ strerror(errno));
|
90
|
+ }
|
91
|
+ }
|
92
|
+ }
|
93
|
log_error_write(srv, __FILE__, __LINE__, "s", "[note] graceful shutdown started");
|
94
|
} else if (srv->conns->used >= srv->max_conns) {
|
95
|
log_error_write(srv, __FILE__, __LINE__, "s", "[note] sockets disabled, connection limit reached");
|
96
|
@@ -1556,6 +1568,20 @@ int main (int argc, char **argv) {
|
97
|
}
|
98
|
|
99
|
if (!buffer_string_is_empty(srv->srvconf.pid_file) &&
|
100
|
+ 0 == graceful_shutdown && 0 <= pid_fd) {
|
101
|
+ if (0 != ftruncate(pid_fd, 0)) {
|
102
|
+ log_error_write(srv, __FILE__, __LINE__, "sbds",
|
103
|
+ "ftruncate failed for:",
|
104
|
+ srv->srvconf.pid_file,
|
105
|
+ errno,
|
106
|
+ strerror(errno));
|
107
|
+ }
|
108
|
+ }
|
109
|
+ if (0 <= pid_fd) {
|
110
|
+ close(pid_fd);
|
111
|
+ pid_fd = -1;
|
112
|
+ }
|
113
|
+ if (!buffer_string_is_empty(srv->srvconf.pid_file) &&
|
114
|
buffer_string_is_empty(srv->srvconf.changeroot) &&
|
115
|
0 == graceful_shutdown) {
|
116
|
if (0 != unlink(srv->srvconf.pid_file->ptr)) {
|
117
|
--
|
118
|
2.4.10
|
119
|
|