xref: /PHP-7.3/Zend/zend_signal.h (revision 11ddf766)
1 /*
2   +----------------------------------------------------------------------+
3   | Zend Signal Handling                                                 |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2008-2018 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 # ifdef HAVE_SIGNAL_H
27 #  include <signal.h>
28 # endif
29 
30 #ifndef NSIG
31 #define NSIG 65
32 #endif
33 
34 #ifndef ZEND_SIGNAL_QUEUE_SIZE
35 #define ZEND_SIGNAL_QUEUE_SIZE 64
36 #endif
37 
38 /* Signal structs */
39 typedef struct _zend_signal_entry_t {
40 	int   flags;          /* sigaction style flags */
41 	void* handler;      /* signal handler or context */
42 } zend_signal_entry_t;
43 
44 typedef struct _zend_signal_t {
45 	int signo;
46 	siginfo_t *siginfo;
47 	void* context;
48 } zend_signal_t;
49 
50 typedef struct _zend_signal_queue_t {
51 	zend_signal_t zend_signal;
52 	struct _zend_signal_queue_t *next;
53 } zend_signal_queue_t;
54 
55 /* Signal Globals */
56 typedef struct _zend_signal_globals_t {
57 	int depth;
58 	int blocked;            /* 1==TRUE, 0==FALSE */
59 	int running;            /* in signal handler execution */
60 	int active;             /* internal signal handling is enabled */
61 	zend_bool check;        /* check for replaced handlers on shutdown */
62 	zend_signal_entry_t handlers[NSIG];
63 	zend_signal_queue_t pstorage[ZEND_SIGNAL_QUEUE_SIZE], *phead, *ptail, *pavail; /* pending queue */
64 } zend_signal_globals_t;
65 
66 # ifdef ZTS
67 #  define SIGG(v) ZEND_TSRMG(zend_signal_globals_id, zend_signal_globals_t *, v)
68 BEGIN_EXTERN_C()
69 ZEND_API extern int zend_signal_globals_id;
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 
114 /*
115  * Local variables:
116  * tab-width: 4
117  * c-basic-offset: 4
118  * indent-tabs-mode: t
119  * End:
120  * vim600: sw=4 ts=4 fdm=marker
121  * vim<600: sw=4 ts=4
122  */
123