xref: /PHP-7.3/sapi/fpm/fpm/fpm_signals.c (revision cc5c51e7)
1 	/* (c) 2007,2008 Andrei Nigmatulin */
2 
3 #include "fpm_config.h"
4 
5 #include <signal.h>
6 #include <stdio.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <errno.h>
14 
15 #include "fpm.h"
16 #include "fpm_signals.h"
17 #include "fpm_sockets.h"
18 #include "fpm_php.h"
19 #include "zlog.h"
20 
21 static int sp[2];
22 
23 const char *fpm_signal_names[NSIG + 1] = {
24 #ifdef SIGHUP
25 	[SIGHUP] 		= "SIGHUP",
26 #endif
27 #ifdef SIGINT
28 	[SIGINT] 		= "SIGINT",
29 #endif
30 #ifdef SIGQUIT
31 	[SIGQUIT] 		= "SIGQUIT",
32 #endif
33 #ifdef SIGILL
34 	[SIGILL] 		= "SIGILL",
35 #endif
36 #ifdef SIGTRAP
37 	[SIGTRAP] 		= "SIGTRAP",
38 #endif
39 #ifdef SIGABRT
40 	[SIGABRT] 		= "SIGABRT",
41 #endif
42 #ifdef SIGEMT
43 	[SIGEMT] 		= "SIGEMT",
44 #endif
45 #ifdef SIGBUS
46 	[SIGBUS] 		= "SIGBUS",
47 #endif
48 #ifdef SIGFPE
49 	[SIGFPE] 		= "SIGFPE",
50 #endif
51 #ifdef SIGKILL
52 	[SIGKILL] 		= "SIGKILL",
53 #endif
54 #ifdef SIGUSR1
55 	[SIGUSR1] 		= "SIGUSR1",
56 #endif
57 #ifdef SIGSEGV
58 	[SIGSEGV] 		= "SIGSEGV",
59 #endif
60 #ifdef SIGUSR2
61 	[SIGUSR2] 		= "SIGUSR2",
62 #endif
63 #ifdef SIGPIPE
64 	[SIGPIPE] 		= "SIGPIPE",
65 #endif
66 #ifdef SIGALRM
67 	[SIGALRM] 		= "SIGALRM",
68 #endif
69 #ifdef SIGTERM
70 	[SIGTERM] 		= "SIGTERM",
71 #endif
72 #ifdef SIGCHLD
73 	[SIGCHLD] 		= "SIGCHLD",
74 #endif
75 #ifdef SIGCONT
76 	[SIGCONT] 		= "SIGCONT",
77 #endif
78 #ifdef SIGSTOP
79 	[SIGSTOP] 		= "SIGSTOP",
80 #endif
81 #ifdef SIGTSTP
82 	[SIGTSTP] 		= "SIGTSTP",
83 #endif
84 #ifdef SIGTTIN
85 	[SIGTTIN] 		= "SIGTTIN",
86 #endif
87 #ifdef SIGTTOU
88 	[SIGTTOU] 		= "SIGTTOU",
89 #endif
90 #ifdef SIGURG
91 	[SIGURG] 		= "SIGURG",
92 #endif
93 #ifdef SIGXCPU
94 	[SIGXCPU] 		= "SIGXCPU",
95 #endif
96 #ifdef SIGXFSZ
97 	[SIGXFSZ] 		= "SIGXFSZ",
98 #endif
99 #ifdef SIGVTALRM
100 	[SIGVTALRM] 	= "SIGVTALRM",
101 #endif
102 #ifdef SIGPROF
103 	[SIGPROF] 		= "SIGPROF",
104 #endif
105 #ifdef SIGWINCH
106 	[SIGWINCH] 		= "SIGWINCH",
107 #endif
108 #ifdef SIGINFO
109 	[SIGINFO] 		= "SIGINFO",
110 #endif
111 #ifdef SIGIO
112 	[SIGIO] 		= "SIGIO",
113 #endif
114 #ifdef SIGPWR
115 	[SIGPWR] 		= "SIGPWR",
116 #endif
117 #ifdef SIGSYS
118 	[SIGSYS] 		= "SIGSYS",
119 #endif
120 #ifdef SIGWAITING
121 	[SIGWAITING] 	= "SIGWAITING",
122 #endif
123 #ifdef SIGLWP
124 	[SIGLWP] 		= "SIGLWP",
125 #endif
126 #ifdef SIGFREEZE
127 	[SIGFREEZE] 	= "SIGFREEZE",
128 #endif
129 #ifdef SIGTHAW
130 	[SIGTHAW] 		= "SIGTHAW",
131 #endif
132 #ifdef SIGCANCEL
133 	[SIGCANCEL] 	= "SIGCANCEL",
134 #endif
135 #ifdef SIGLOST
136 	[SIGLOST] 		= "SIGLOST",
137 #endif
138 };
139 
sig_soft_quit(int signo)140 static void sig_soft_quit(int signo) /* {{{ */
141 {
142 	int saved_errno = errno;
143 
144 	/* closing fastcgi listening socket will force fcgi_accept() exit immediately */
145 	close(fpm_globals.listening_socket);
146 	if (0 > socket(AF_UNIX, SOCK_STREAM, 0)) {
147 		zlog(ZLOG_WARNING, "failed to create a new socket");
148 	}
149 	fpm_php_soft_quit();
150 	errno = saved_errno;
151 }
152 /* }}} */
153 
sig_handler(int signo)154 static void sig_handler(int signo) /* {{{ */
155 {
156 	static const char sig_chars[NSIG + 1] = {
157 		[SIGTERM] = 'T',
158 		[SIGINT]  = 'I',
159 		[SIGUSR1] = '1',
160 		[SIGUSR2] = '2',
161 		[SIGQUIT] = 'Q',
162 		[SIGCHLD] = 'C'
163 	};
164 	char s;
165 	int saved_errno;
166 
167 	if (fpm_globals.parent_pid != getpid()) {
168 		/* prevent a signal race condition when child process
169 			have not set up it's own signal handler yet */
170 		return;
171 	}
172 
173 	saved_errno = errno;
174 	s = sig_chars[signo];
175 	zend_quiet_write(sp[1], &s, sizeof(s));
176 	errno = saved_errno;
177 }
178 /* }}} */
179 
fpm_signals_init_main()180 int fpm_signals_init_main() /* {{{ */
181 {
182 	struct sigaction act;
183 
184 	if (0 > socketpair(AF_UNIX, SOCK_STREAM, 0, sp)) {
185 		zlog(ZLOG_SYSERROR, "failed to init signals: socketpair()");
186 		return -1;
187 	}
188 
189 	if (0 > fd_set_blocked(sp[0], 0) || 0 > fd_set_blocked(sp[1], 0)) {
190 		zlog(ZLOG_SYSERROR, "failed to init signals: fd_set_blocked()");
191 		return -1;
192 	}
193 
194 	if (0 > fcntl(sp[0], F_SETFD, FD_CLOEXEC) || 0 > fcntl(sp[1], F_SETFD, FD_CLOEXEC)) {
195 		zlog(ZLOG_SYSERROR, "falied to init signals: fcntl(F_SETFD, FD_CLOEXEC)");
196 		return -1;
197 	}
198 
199 	memset(&act, 0, sizeof(act));
200 	act.sa_handler = sig_handler;
201 	sigfillset(&act.sa_mask);
202 
203 	if (0 > sigaction(SIGTERM,  &act, 0) ||
204 	    0 > sigaction(SIGINT,   &act, 0) ||
205 	    0 > sigaction(SIGUSR1,  &act, 0) ||
206 	    0 > sigaction(SIGUSR2,  &act, 0) ||
207 	    0 > sigaction(SIGCHLD,  &act, 0) ||
208 	    0 > sigaction(SIGQUIT,  &act, 0)) {
209 
210 		zlog(ZLOG_SYSERROR, "failed to init signals: sigaction()");
211 		return -1;
212 	}
213 	return 0;
214 }
215 /* }}} */
216 
fpm_signals_init_child()217 int fpm_signals_init_child() /* {{{ */
218 {
219 	struct sigaction act, act_dfl;
220 
221 	memset(&act, 0, sizeof(act));
222 	memset(&act_dfl, 0, sizeof(act_dfl));
223 
224 	act.sa_handler = &sig_soft_quit;
225 	act.sa_flags |= SA_RESTART;
226 
227 	act_dfl.sa_handler = SIG_DFL;
228 
229 	close(sp[0]);
230 	close(sp[1]);
231 
232 	if (0 > sigaction(SIGTERM,  &act_dfl,  0) ||
233 	    0 > sigaction(SIGINT,   &act_dfl,  0) ||
234 	    0 > sigaction(SIGUSR1,  &act_dfl,  0) ||
235 	    0 > sigaction(SIGUSR2,  &act_dfl,  0) ||
236 	    0 > sigaction(SIGCHLD,  &act_dfl,  0) ||
237 	    0 > sigaction(SIGQUIT,  &act,      0)) {
238 
239 		zlog(ZLOG_SYSERROR, "failed to init child signals: sigaction()");
240 		return -1;
241 	}
242 
243 	zend_signal_init();
244 	return 0;
245 }
246 /* }}} */
247 
fpm_signals_get_fd()248 int fpm_signals_get_fd() /* {{{ */
249 {
250 	return sp[0];
251 }
252 /* }}} */
253