xref: /PHP-8.0/Zend/zend_signal.c (revision 54248b18)
1 /*
2   +----------------------------------------------------------------------+
3   | Zend Signal Handling                                                 |
4   +----------------------------------------------------------------------+
5   | Copyright (c) The PHP Group                                          |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Authors: Lucas Nealan <lucas@php.net>                                |
16   |          Arnaud Le Blanc <lbarnaud@php.net>                          |
17   +----------------------------------------------------------------------+
18 
19    This software was contributed to PHP by Facebook Inc. in 2008.
20 
21    Future revisions and derivatives of this source code must acknowledge
22    Facebook Inc. as the original contributor of this module by leaving
23    this note intact in the source code.
24 
25    All other licensing and usage conditions are those of the PHP Group.
26 */
27 
28 #define _GNU_SOURCE
29 #include <string.h>
30 
31 #include "zend.h"
32 #include "zend_globals.h"
33 #include <signal.h>
34 
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 
39 #ifdef ZEND_SIGNALS
40 
41 #include "zend_signal.h"
42 
43 #ifdef ZTS
44 ZEND_API int zend_signal_globals_id;
45 ZEND_API size_t zend_signal_globals_offset;
46 #else
47 ZEND_API zend_signal_globals_t zend_signal_globals;
48 #endif /* not ZTS */
49 
50 #define SIGNAL_BEGIN_CRITICAL() \
51 	sigset_t oldmask; \
52 	zend_sigprocmask(SIG_BLOCK, &global_sigmask, &oldmask);
53 #define SIGNAL_END_CRITICAL() \
54 	zend_sigprocmask(SIG_SETMASK, &oldmask, NULL);
55 
56 #ifdef ZTS
57 # define zend_sigprocmask(signo, set, oldset) tsrm_sigmask((signo), (set), (oldset))
58 #else
59 # define zend_sigprocmask(signo, set, oldset) sigprocmask((signo), (set), (oldset))
60 #endif
61 
62 static void zend_signal_handler(int signo, siginfo_t *siginfo, void *context);
63 static int zend_signal_register(int signo, void (*handler)(int, siginfo_t*, void*));
64 
65 #if defined(__CYGWIN__) || defined(__PASE__)
66 /* Matches zend_excute_API.c; these platforms don't support ITIMER_PROF. */
67 #define TIMEOUT_SIG SIGALRM
68 #else
69 #define TIMEOUT_SIG SIGPROF
70 #endif
71 
72 static int zend_sigs[] = { TIMEOUT_SIG, SIGHUP, SIGINT, SIGQUIT, SIGTERM, SIGUSR1, SIGUSR2 };
73 
74 #define SA_FLAGS_MASK ~(SA_NODEFER | SA_RESETHAND)
75 
76 /* True globals, written only at process startup */
77 static zend_signal_entry_t global_orig_handlers[NSIG];
78 static sigset_t            global_sigmask;
79 
80 /* {{{ zend_signal_handler_defer
81  *  Blocks signals if in critical section */
zend_signal_handler_defer(int signo,siginfo_t * siginfo,void * context)82 void zend_signal_handler_defer(int signo, siginfo_t *siginfo, void *context)
83 {
84 	int errno_save = errno;
85 	zend_signal_queue_t *queue, *qtmp;
86 
87 #ifdef ZTS
88 	/* A signal could hit after TSRM shutdown, in this case globals are already freed. */
89 	if (tsrm_is_shutdown()) {
90 		/* Forward to default handler handler */
91 		zend_signal_handler(signo, siginfo, context);
92 		return;
93 	}
94 #endif
95 
96 	if (EXPECTED(SIGG(active))) {
97 		if (UNEXPECTED(SIGG(depth) == 0)) { /* try to handle signal */
98 			if (UNEXPECTED(SIGG(blocked))) {
99 				SIGG(blocked) = 0;
100 			}
101 			if (EXPECTED(SIGG(running) == 0)) {
102 				SIGG(running) = 1;
103 				zend_signal_handler(signo, siginfo, context);
104 
105 				queue = SIGG(phead);
106 				SIGG(phead) = NULL;
107 
108 				while (queue) {
109 					zend_signal_handler(queue->zend_signal.signo, queue->zend_signal.siginfo, queue->zend_signal.context);
110 					qtmp = queue->next;
111 					queue->next = SIGG(pavail);
112 					queue->zend_signal.signo = 0;
113 					SIGG(pavail) = queue;
114 					queue = qtmp;
115 				}
116 				SIGG(running) = 0;
117 			}
118 		} else { /* delay signal handling */
119 			SIGG(blocked) = 1; /* signal is blocked */
120 
121 			if ((queue = SIGG(pavail))) { /* if none available it's simply forgotton */
122 				SIGG(pavail) = queue->next;
123 				queue->zend_signal.signo = signo;
124 				queue->zend_signal.siginfo = siginfo;
125 				queue->zend_signal.context = context;
126 				queue->next = NULL;
127 
128 				if (SIGG(phead) && SIGG(ptail)) {
129 					SIGG(ptail)->next = queue;
130 				} else {
131 					SIGG(phead) = queue;
132 				}
133 				SIGG(ptail) = queue;
134 			}
135 #if ZEND_DEBUG
136 			else { /* this may not be safe to do, but could work and be useful */
137 				zend_output_debug_string(0, "zend_signal: not enough queue storage, lost signal (%d)", signo);
138 			}
139 #endif
140 		}
141 	} else {
142 		/* need to just run handler if we're inactive and getting a signal */
143 		zend_signal_handler(signo, siginfo, context);
144 	}
145 
146 	errno = errno_save;
147 } /* }}} */
148 
149 /* {{{ zend_signal_handler_unblock
150  * Handle deferred signal from HANDLE_UNBLOCK_ALARMS */
zend_signal_handler_unblock(void)151 ZEND_API void zend_signal_handler_unblock(void)
152 {
153 	zend_signal_queue_t *queue;
154 	zend_signal_t zend_signal;
155 
156 	if (EXPECTED(SIGG(active))) {
157 		SIGNAL_BEGIN_CRITICAL(); /* procmask to protect handler_defer as if it were called by the kernel */
158 		queue = SIGG(phead);
159 		SIGG(phead) = queue->next;
160 		zend_signal = queue->zend_signal;
161 		queue->next = SIGG(pavail);
162 		queue->zend_signal.signo = 0;
163 		SIGG(pavail) = queue;
164 
165 		zend_signal_handler_defer(zend_signal.signo, zend_signal.siginfo, zend_signal.context);
166 		SIGNAL_END_CRITICAL();
167 	}
168 }
169 /* }}} */
170 
171 /* {{{ zend_signal_handler
172  *  Call the previously registered handler for a signal
173  */
zend_signal_handler(int signo,siginfo_t * siginfo,void * context)174 static void zend_signal_handler(int signo, siginfo_t *siginfo, void *context)
175 {
176 	int errno_save = errno;
177 	struct sigaction sa;
178 	sigset_t sigset;
179 	zend_signal_entry_t p_sig;
180 #ifdef ZTS
181 	if (tsrm_is_shutdown()) {
182 		p_sig.flags = 0;
183 		p_sig.handler = SIG_DFL;
184 	} else
185 #endif
186 	p_sig = SIGG(handlers)[signo-1];
187 
188 	if (p_sig.handler == SIG_DFL) { /* raise default handler */
189 		if (sigaction(signo, NULL, &sa) == 0) {
190 			sa.sa_handler = SIG_DFL;
191 			sigemptyset(&sa.sa_mask);
192 
193 			sigemptyset(&sigset);
194 			sigaddset(&sigset, signo);
195 
196 			if (sigaction(signo, &sa, NULL) == 0) {
197 				/* throw away any blocked signals */
198 				zend_sigprocmask(SIG_UNBLOCK, &sigset, NULL);
199 #ifdef ZTS
200 # define RAISE_ERROR "raise() failed\n"
201 				if (raise(signo) != 0) {
202 					/* On some systems raise() fails with errno 3: No such process */
203 					kill(getpid(), signo);
204 				}
205 #else
206 				kill(getpid(), signo);
207 #endif
208 			}
209 		}
210 	} else if (p_sig.handler != SIG_IGN) {
211 		if (p_sig.flags & SA_SIGINFO) {
212 			if (p_sig.flags & SA_RESETHAND) {
213 				SIGG(handlers)[signo-1].flags   = 0;
214 				SIGG(handlers)[signo-1].handler = SIG_DFL;
215 			}
216 			(*(void (*)(int, siginfo_t*, void*))p_sig.handler)(signo, siginfo, context);
217 		} else {
218 			(*(void (*)(int))p_sig.handler)(signo);
219 		}
220 	}
221 
222 	errno = errno_save;
223 } /* }}} */
224 
225 /* {{{ zend_sigaction
226  *  Register a signal handler that will be deferred in critical sections */
zend_sigaction(int signo,const struct sigaction * act,struct sigaction * oldact)227 ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct sigaction *oldact)
228 {
229 	struct sigaction sa;
230 	sigset_t sigset;
231 
232 	if (oldact != NULL) {
233 		oldact->sa_flags   = SIGG(handlers)[signo-1].flags;
234 		oldact->sa_handler = (void *) SIGG(handlers)[signo-1].handler;
235 		oldact->sa_mask    = global_sigmask;
236 	}
237 	if (act != NULL) {
238 		SIGG(handlers)[signo-1].flags = act->sa_flags;
239 		if (act->sa_flags & SA_SIGINFO) {
240 			SIGG(handlers)[signo-1].handler = (void *) act->sa_sigaction;
241 		} else {
242 			SIGG(handlers)[signo-1].handler = (void *) act->sa_handler;
243 		}
244 
245 		memset(&sa, 0, sizeof(sa));
246 		if (SIGG(handlers)[signo-1].handler == (void *) SIG_IGN) {
247 			sa.sa_sigaction = (void *) SIG_IGN;
248 		} else {
249 			sa.sa_flags     = SA_SIGINFO | (act->sa_flags & SA_FLAGS_MASK);
250 			sa.sa_sigaction = zend_signal_handler_defer;
251 			sa.sa_mask      = global_sigmask;
252 		}
253 
254 		if (sigaction(signo, &sa, NULL) < 0) {
255 			zend_error_noreturn(E_ERROR, "Error installing signal handler for %d", signo);
256 		}
257 
258 		/* unsure this signal is not blocked */
259 		sigemptyset(&sigset);
260 		sigaddset(&sigset, signo);
261 		zend_sigprocmask(SIG_UNBLOCK, &sigset, NULL);
262 	}
263 }
264 /* }}} */
265 
266 /* {{{ zend_signal
267  *  Register a signal handler that will be deferred in critical sections */
zend_signal(int signo,void (* handler)(int))268 ZEND_API void zend_signal(int signo, void (*handler)(int))
269 {
270 	struct sigaction sa;
271 
272 	memset(&sa, 0, sizeof(sa));
273 	sa.sa_flags   = 0;
274 	sa.sa_handler = handler;
275 	sa.sa_mask    = global_sigmask;
276 
277 	zend_sigaction(signo, &sa, NULL);
278 }
279 /* }}} */
280 
281 /* {{{ zend_signal_register
282  *  Set a handler for a signal we want to defer.
283  *  Previously set handler must have been saved before.
284  */
zend_signal_register(int signo,void (* handler)(int,siginfo_t *,void *))285 static zend_result zend_signal_register(int signo, void (*handler)(int, siginfo_t*, void*))
286 {
287 	struct sigaction sa;
288 
289 	if (sigaction(signo, NULL, &sa) == 0) {
290 		if ((sa.sa_flags & SA_SIGINFO) && sa.sa_sigaction == handler) {
291 			return FAILURE;
292 		}
293 
294 		SIGG(handlers)[signo-1].flags = sa.sa_flags;
295 		if (sa.sa_flags & SA_SIGINFO) {
296 			SIGG(handlers)[signo-1].handler = (void *)sa.sa_sigaction;
297 		} else {
298 			SIGG(handlers)[signo-1].handler = (void *)sa.sa_handler;
299 		}
300 
301 		sa.sa_flags     = SA_SIGINFO; /* we'll use a siginfo handler */
302 		sa.sa_sigaction = handler;
303 		sa.sa_mask      = global_sigmask;
304 
305 		if (sigaction(signo, &sa, NULL) < 0) {
306 			zend_error_noreturn(E_ERROR, "Error installing signal handler for %d", signo);
307 		}
308 
309 		return SUCCESS;
310 	}
311 	return FAILURE;
312 } /* }}} */
313 
314 /* {{{ zend_signal_activate
315  *  Install our signal handlers, per request */
zend_signal_activate(void)316 void zend_signal_activate(void)
317 {
318 	size_t x;
319 
320 	memcpy(&SIGG(handlers), &global_orig_handlers, sizeof(global_orig_handlers));
321 
322 	if (SIGG(reset)) {
323 		for (x = 0; x < sizeof(zend_sigs) / sizeof(*zend_sigs); x++) {
324 			zend_signal_register(zend_sigs[x], zend_signal_handler_defer);
325 		}
326 	}
327 
328 	SIGG(active) = 1;
329 	SIGG(depth)  = 0;
330 } /* }}} */
331 
332 /* {{{ zend_signal_deactivate */
zend_signal_deactivate(void)333 void zend_signal_deactivate(void)
334 {
335 	if (SIGG(check)) {
336 		size_t x;
337 		struct sigaction sa;
338 
339 		if (SIGG(depth) != 0) {
340 			zend_error(E_CORE_WARNING, "zend_signal: shutdown with non-zero blocking depth (%d)", SIGG(depth));
341 		}
342 
343 		/* did anyone steal our installed handler */
344 		for (x = 0; x < sizeof(zend_sigs) / sizeof(*zend_sigs); x++) {
345 			sigaction(zend_sigs[x], NULL, &sa);
346 			if (sa.sa_sigaction != zend_signal_handler_defer &&
347 					sa.sa_sigaction != (void *) SIG_IGN) {
348 				zend_error(E_CORE_WARNING, "zend_signal: handler was replaced for signal (%d) after startup", zend_sigs[x]);
349 			}
350 		}
351 	}
352 
353 	/* After active=0 is set, signal handlers will be called directly and other
354 	 * state that is reset below will not be accessed. */
355 	*((volatile int *) &SIGG(active)) = 0;
356 
357 	SIGG(running) = 0;
358 	SIGG(blocked) = 0;
359 	SIGG(depth) = 0;
360 
361 	/* If there are any queued signals because of a missed unblock, drop them. */
362 	if (SIGG(phead) && SIGG(ptail)) {
363 		SIGG(ptail)->next = SIGG(pavail);
364 		SIGG(pavail) = SIGG(phead);
365 		SIGG(phead) = NULL;
366 		SIGG(ptail) = NULL;
367 	}
368 }
369 /* }}} */
370 
zend_signal_globals_ctor(zend_signal_globals_t * zend_signal_globals)371 static void zend_signal_globals_ctor(zend_signal_globals_t *zend_signal_globals) /* {{{ */
372 {
373 	size_t x;
374 
375 	memset(zend_signal_globals, 0, sizeof(*zend_signal_globals));
376 	zend_signal_globals->reset = 1;
377 
378 	for (x = 0; x < sizeof(zend_signal_globals->pstorage) / sizeof(*zend_signal_globals->pstorage); ++x) {
379 		zend_signal_queue_t *queue = &zend_signal_globals->pstorage[x];
380 		queue->zend_signal.signo = 0;
381 		queue->next = zend_signal_globals->pavail;
382 		zend_signal_globals->pavail = queue;
383 	}
384 }
385 /* }}} */
386 
zend_signal_init(void)387 void zend_signal_init(void) /* {{{ */
388 {
389 	int signo;
390 	struct sigaction sa;
391 
392 	/* Save previously registered signal handlers into orig_handlers */
393 	memset(&global_orig_handlers, 0, sizeof(global_orig_handlers));
394 	for (signo = 1; signo < NSIG; ++signo) {
395 		if (sigaction(signo, NULL, &sa) == 0) {
396 			global_orig_handlers[signo-1].flags = sa.sa_flags;
397 			if (sa.sa_flags & SA_SIGINFO) {
398 				global_orig_handlers[signo-1].handler = (void *) sa.sa_sigaction;
399 			} else {
400 				global_orig_handlers[signo-1].handler = (void *) sa.sa_handler;
401 			}
402 		}
403 	}
404 }
405 /* }}} */
406 
407 /* {{{ zend_signal_startup
408  * alloc zend signal globals */
zend_signal_startup(void)409 ZEND_API void zend_signal_startup(void)
410 {
411 
412 #ifdef ZTS
413 	ts_allocate_fast_id(&zend_signal_globals_id, &zend_signal_globals_offset, sizeof(zend_signal_globals_t), (ts_allocate_ctor) zend_signal_globals_ctor, NULL);
414 #else
415 	zend_signal_globals_ctor(&zend_signal_globals);
416 #endif
417 
418 	/* Used to block signals during execution of signal handlers */
419 	sigfillset(&global_sigmask);
420 	sigdelset(&global_sigmask, SIGILL);
421 	sigdelset(&global_sigmask, SIGABRT);
422 	sigdelset(&global_sigmask, SIGFPE);
423 	sigdelset(&global_sigmask, SIGKILL);
424 	sigdelset(&global_sigmask, SIGSEGV);
425 	sigdelset(&global_sigmask, SIGCONT);
426 	sigdelset(&global_sigmask, SIGSTOP);
427 	sigdelset(&global_sigmask, SIGTSTP);
428 	sigdelset(&global_sigmask, SIGTTIN);
429 	sigdelset(&global_sigmask, SIGTTOU);
430 #ifdef SIGBUS
431 	sigdelset(&global_sigmask, SIGBUS);
432 #endif
433 #ifdef SIGSYS
434 	sigdelset(&global_sigmask, SIGSYS);
435 #endif
436 #ifdef SIGTRAP
437 	sigdelset(&global_sigmask, SIGTRAP);
438 #endif
439 
440 	zend_signal_init();
441 }
442 /* }}} */
443 
444 
445 #endif /* ZEND_SIGNALS */
446