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