xref: /PHP-7.4/Zend/zend_signal.h (revision 5f891578)
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  */
20 
21 #ifndef ZEND_SIGNAL_H
22 #define ZEND_SIGNAL_H
23 
24 #ifdef ZEND_SIGNALS
25 
26 #include <signal.h>
27 
28 #ifndef NSIG
29 #define NSIG 65
30 #endif
31 
32 #ifndef ZEND_SIGNAL_QUEUE_SIZE
33 #define ZEND_SIGNAL_QUEUE_SIZE 64
34 #endif
35 
36 /* Signal structs */
37 typedef struct _zend_signal_entry_t {
38 	int   flags;          /* sigaction style flags */
39 	void* handler;      /* signal handler or context */
40 } zend_signal_entry_t;
41 
42 typedef struct _zend_signal_t {
43 	int signo;
44 	siginfo_t *siginfo;
45 	void* context;
46 } zend_signal_t;
47 
48 typedef struct _zend_signal_queue_t {
49 	zend_signal_t zend_signal;
50 	struct _zend_signal_queue_t *next;
51 } zend_signal_queue_t;
52 
53 /* Signal Globals */
54 typedef struct _zend_signal_globals_t {
55 	int depth;
56 	int blocked;            /* 1==TRUE, 0==FALSE */
57 	int running;            /* in signal handler execution */
58 	int active;             /* internal signal handling is enabled */
59 	zend_bool check;        /* check for replaced handlers on shutdown */
60 	zend_bool reset;        /* reset signal handlers on each request */
61 	zend_signal_entry_t handlers[NSIG];
62 	zend_signal_queue_t pstorage[ZEND_SIGNAL_QUEUE_SIZE], *phead, *ptail, *pavail; /* pending queue */
63 } zend_signal_globals_t;
64 
65 # ifdef ZTS
66 #  define SIGG(v) ZEND_TSRMG_FAST(zend_signal_globals_offset, zend_signal_globals_t *, v)
67 BEGIN_EXTERN_C()
68 ZEND_API extern int zend_signal_globals_id;
69 ZEND_API extern size_t zend_signal_globals_offset;
70 END_EXTERN_C()
71 # else
72 #  define SIGG(v) (zend_signal_globals.v)
73 BEGIN_EXTERN_C()
74 ZEND_API extern zend_signal_globals_t zend_signal_globals;
75 END_EXTERN_C()
76 # endif /* not ZTS */
77 
78 # ifdef ZTS
79 #  define ZEND_SIGNAL_BLOCK_INTERRUPTIONS() if (EXPECTED(zend_signal_globals_id)) { SIGG(depth)++; }
80 #  define ZEND_SIGNAL_UNBLOCK_INTERRUPTIONS() if (EXPECTED(zend_signal_globals_id) && UNEXPECTED(((SIGG(depth)--) == SIGG(blocked)))) { zend_signal_handler_unblock(); }
81 # else /* ZTS */
82 #  define ZEND_SIGNAL_BLOCK_INTERRUPTIONS()  SIGG(depth)++;
83 #  define ZEND_SIGNAL_UNBLOCK_INTERRUPTIONS() if (((SIGG(depth)--) == SIGG(blocked))) { zend_signal_handler_unblock(); }
84 # endif /* not ZTS */
85 
86 ZEND_API void zend_signal_handler_unblock(void);
87 void zend_signal_activate(void);
88 void zend_signal_deactivate(void);
89 BEGIN_EXTERN_C()
90 ZEND_API void zend_signal_startup(void);
91 END_EXTERN_C()
92 void zend_signal_init(void);
93 
94 ZEND_API int zend_signal(int signo, void (*handler)(int));
95 ZEND_API int zend_sigaction(int signo, const struct sigaction *act, struct sigaction *oldact);
96 
97 #else /* ZEND_SIGNALS */
98 
99 # define ZEND_SIGNAL_BLOCK_INTERRUPTIONS()
100 # define ZEND_SIGNAL_UNBLOCK_INTERRUPTIONS()
101 
102 # define zend_signal_activate()
103 # define zend_signal_deactivate()
104 # define zend_signal_startup()
105 # define zend_signal_init()
106 
107 # define zend_signal(signo, handler)           signal(signo, handler)
108 # define zend_sigaction(signo, act, oldact)    sigaction(signo, act, oldact)
109 
110 #endif /* ZEND_SIGNALS */
111 
112 #endif /* ZEND_SIGNAL_H */
113