summaryrefslogtreecommitdiffstats
path: root/php-5.3.17-fpm.patch
blob: 1789986e3fac18f860ca66c4d7b87c6279a948af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
From 75c63c5503b1c6b72e5e1daf5b4bfd02c68a4b79 Mon Sep 17 00:00:00 2001
From: Jerome Loyet <fat@php.net>
Date: Thu, 27 Sep 2012 23:54:35 +0200
Subject: [PATCH] - Fixed bug #62954 (startup problems fpm / php-fpm) - Fixed
 bug #62886 (PHP-FPM may segfault/hang on startup)

---
 NEWS                       |    4 ++
 sapi/fpm/fpm/fpm.c         |    2 +-
 sapi/fpm/fpm/fpm.h         |    2 +-
 sapi/fpm/fpm/fpm_main.c    |   16 +++++---
 sapi/fpm/fpm/fpm_signals.c |   12 ------
 sapi/fpm/fpm/fpm_signals.h |    3 --
 sapi/fpm/fpm/fpm_unix.c    |   81 +++++++++++++++++++++++++------------------
 7 files changed, 63 insertions(+), 57 deletions(-)

diff --git a/sapi/fpm/fpm/fpm.c b/sapi/fpm/fpm/fpm.c
index dab415d..2f42175 100644
--- a/sapi/fpm/fpm/fpm.c
+++ b/sapi/fpm/fpm/fpm.c
@@ -39,7 +39,7 @@ struct fpm_globals_s fpm_globals = {
 	.test_successful = 0,
 	.heartbeat = 0,
 	.run_as_root = 0,
-	.send_config_signal = 0,
+	.send_config_pipe = {0, 0},
 };
 
 int fpm_init(int argc, char **argv, char *config, char *prefix, char *pid, int test_conf, int run_as_root) /* {{{ */
diff --git a/sapi/fpm/fpm/fpm.h b/sapi/fpm/fpm/fpm.h
index 7a2903d..c576876 100644
--- a/sapi/fpm/fpm/fpm.h
+++ b/sapi/fpm/fpm/fpm.h
@@ -55,7 +55,7 @@ struct fpm_globals_s {
 	int test_successful;
 	int heartbeat;
 	int run_as_root;
-	int send_config_signal;
+	int send_config_pipe[2];
 };
 
 extern struct fpm_globals_s fpm_globals;
diff --git a/sapi/fpm/fpm/fpm_main.c b/sapi/fpm/fpm/fpm_main.c
index b058d7a..70e917a 100644
--- a/sapi/fpm/fpm/fpm_main.c
+++ b/sapi/fpm/fpm/fpm_main.c
@@ -1804,16 +1804,20 @@ consult the installation file that came with this distribution, or visit \n\
 
 	if (0 > fpm_init(argc, argv, fpm_config ? fpm_config : CGIG(fpm_config), fpm_prefix, fpm_pid, test_conf, php_allow_to_run_as_root)) {
 
-		if (fpm_globals.send_config_signal) {
-			zlog(ZLOG_DEBUG, "Sending SIGUSR2 (error) to parent %d", getppid());
-			kill(getppid(), SIGUSR2);
+		if (fpm_globals.send_config_pipe[1]) {
+			int writeval = 0;
+			zlog(ZLOG_DEBUG, "Sending \"0\" (error) to parent via fd=%d", fpm_globals.send_config_pipe[1]);
+			write(fpm_globals.send_config_pipe[1], &writeval, sizeof(writeval));
+			close(fpm_globals.send_config_pipe[1]);
 		}
 		return FPM_EXIT_CONFIG;
 	}
 
-	if (fpm_globals.send_config_signal) {
-		zlog(ZLOG_DEBUG, "Sending SIGUSR1 (OK) to parent %d", getppid());
-		kill(getppid(), SIGUSR1);
+	if (fpm_globals.send_config_pipe[1]) {
+		int writeval = 1;
+		zlog(ZLOG_DEBUG, "Sending \"1\" (OK) to parent via fd=%d", fpm_globals.send_config_pipe[1]);
+		write(fpm_globals.send_config_pipe[1], &writeval, sizeof(writeval));
+		close(fpm_globals.send_config_pipe[1]);
 	}
 	fpm_is_running = 1;
 
diff --git a/sapi/fpm/fpm/fpm_signals.c b/sapi/fpm/fpm/fpm_signals.c
index 656269f..8993a86 100644
--- a/sapi/fpm/fpm/fpm_signals.c
+++ b/sapi/fpm/fpm/fpm_signals.c
@@ -249,15 +249,3 @@ int fpm_signals_get_fd() /* {{{ */
 }
 /* }}} */
 
-void fpm_signals_sighandler_exit_ok(pid_t pid) /* {{{ */
-{
-	exit(FPM_EXIT_OK);
-}
-/* }}} */
-
-void fpm_signals_sighandler_exit_config(pid_t pid) /* {{{ */
-{
-	exit(FPM_EXIT_CONFIG);
-}
-/* }}} */
-
diff --git a/sapi/fpm/fpm/fpm_signals.h b/sapi/fpm/fpm/fpm_signals.h
index 13484cb..eb80fae 100644
--- a/sapi/fpm/fpm/fpm_signals.h
+++ b/sapi/fpm/fpm/fpm_signals.h
@@ -11,9 +11,6 @@ int fpm_signals_init_main();
 int fpm_signals_init_child();
 int fpm_signals_get_fd();
 
-void fpm_signals_sighandler_exit_ok(pid_t pid);
-void fpm_signals_sighandler_exit_config(pid_t pid);
-
 extern const char *fpm_signal_names[NSIG + 1];
 
 #endif
diff --git a/sapi/fpm/fpm/fpm_unix.c b/sapi/fpm/fpm/fpm_unix.c
index 5c5e37c..1a75944 100644
--- a/sapi/fpm/fpm/fpm_unix.c
+++ b/sapi/fpm/fpm/fpm_unix.c
@@ -262,36 +262,19 @@ int fpm_unix_init_main() /* {{{ */
 		 * The parent process has then to wait for the master
 		 * process to initialize to return a consistent exit
 		 * value. For this pupose, the master process will
-		 * send USR1 if everything went well and USR2
-		 * otherwise.
+		 * send \"1\" into the pipe if everything went well 
+		 * and \"0\" otherwise.
 		 */
 
-		struct sigaction act;
-		struct sigaction oldact_usr1;
-		struct sigaction oldact_usr2;
-		struct timeval tv;
 
-		/*
-		 * set sigaction for USR1 before fork
-		 * save old sigaction to restore it after
-		 * fork in the child process (the master process)
-		 */
-		memset(&act, 0, sizeof(act));
-		memset(&act, 0, sizeof(oldact_usr1));
-		act.sa_handler = fpm_signals_sighandler_exit_ok;
-		sigfillset(&act.sa_mask);
-		sigaction(SIGUSR1, &act, &oldact_usr1);
+		struct timeval tv;
+		fd_set rfds;
+		int ret;
 
-		/*
-		 * set sigaction for USR2 before fork
-		 * save old sigaction to restore it after
-		 * fork in the child process (the master process)
-		 */
-		memset(&act, 0, sizeof(act));
-		memset(&act, 0, sizeof(oldact_usr2));
-		act.sa_handler = fpm_signals_sighandler_exit_config;
-		sigfillset(&act.sa_mask);
-		sigaction(SIGUSR2, &act, &oldact_usr2);
+		if (pipe(fpm_globals.send_config_pipe) == -1) {
+			zlog(ZLOG_SYSERROR, "failed to create pipe");
+			return -1;
+		}
 
 		/* then fork */
 		pid_t pid = fork();
@@ -302,24 +285,54 @@ int fpm_unix_init_main() /* {{{ */
 				return -1;
 
 			case 0 : /* children */
-				/* restore USR1 and USR2 sigaction */
-				sigaction(SIGUSR1, &oldact_usr1, NULL);
-				sigaction(SIGUSR2, &oldact_usr2, NULL);
-				fpm_globals.send_config_signal = 1;
+				close(fpm_globals.send_config_pipe[0]); /* close the read side of the pipe */
 				break;
 
 			default : /* parent */
-				fpm_cleanups_run(FPM_CLEANUP_PARENT_EXIT);
+				close(fpm_globals.send_config_pipe[1]); /* close the write side of the pipe */
 
 				/*
 				 * wait for 10s before exiting with error
-				 * the child is supposed to send USR1 or USR2 to tell the parent
+				 * the child is supposed to send 1 or 0 into the pipe to tell the parent
 				 * how it goes for it
 				 */
+				FD_ZERO(&rfds);
+				FD_SET(fpm_globals.send_config_pipe[0], &rfds);
+
 				tv.tv_sec = 10;
 				tv.tv_usec = 0;
-				zlog(ZLOG_DEBUG, "The calling process is waiting for the master process to ping");
-				select(0, NULL, NULL, NULL, &tv);
+
+				zlog(ZLOG_DEBUG, "The calling process is waiting for the master process to ping via fd=%d", fpm_globals.send_config_pipe[0]);
+				ret = select(fpm_globals.send_config_pipe[0] + 1, &rfds, NULL, NULL, &tv);
+				if (ret == -1) {
+					zlog(ZLOG_SYSERROR, "failed to select");
+					exit(FPM_EXIT_SOFTWARE);
+				}
+				if (ret) { /* data available */
+					int readval;
+					ret = read(fpm_globals.send_config_pipe[0], &readval, sizeof(readval));
+					if (ret == -1) {
+						zlog(ZLOG_SYSERROR, "failed to read from pipe");
+						exit(FPM_EXIT_SOFTWARE);
+					}
+
+					if (ret == 0) {
+						zlog(ZLOG_ERROR, "no data have been read from pipe");
+						exit(FPM_EXIT_SOFTWARE);
+					} else {
+						if (readval == 1) {
+							zlog(ZLOG_DEBUG, "I received a valid acknoledge from the master process, I can exit without error");
+							fpm_cleanups_run(FPM_CLEANUP_PARENT_EXIT);
+							exit(FPM_EXIT_OK);
+						} else {
+							zlog(ZLOG_DEBUG, "The master process returned an error !");
+							exit(FPM_EXIT_SOFTWARE);
+						}
+					}
+				} else { /* no date sent ! */
+					zlog(ZLOG_ERROR, "the master process didn't send back its status (via the pipe to the calling process)");
+				  exit(FPM_EXIT_SOFTWARE);
+				}
 				exit(FPM_EXIT_SOFTWARE);
 		}
 	}
-- 
1.7.8