split out some modules and headers
[m6w6/ext-psi] / php_psi.h
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #ifndef PHP_PSI_H
27 #define PHP_PSI_H
28
29 #include "php.h"
30
31 extern zend_module_entry psi_module_entry;
32 #define phpext_psi_ptr &psi_module_entry
33
34 #define PHP_PSI_VERSION "0.1.0"
35
36 #ifdef PHP_WIN32
37 # define PHP_PSI_API __declspec(dllexport)
38 #elif defined(__GNUC__) && __GNUC__ >= 4
39 # define PHP_PSI_API __attribute__ ((visibility("default")))
40 #else
41 # define PHP_PSI_API
42 #endif
43
44 #ifdef ZTS
45 #include "TSRM.h"
46 #endif
47
48 static inline int psi_check_env(const char *var) {
49 char *set = getenv(var);
50 return (set && *set && (*set != '0' || set[1]));
51 }
52
53 #include <fcntl.h>
54 #include "php_network.h"
55
56 static inline int psi_fdopen(const char *desc)
57 {
58 int fd = -1;
59
60 if (desc) {
61 char *addr = strstr(desc, "://");
62
63 if (addr) {
64 addr += 3;
65 }
66 if (addr && *addr) {
67 struct sockaddr_storage sa = {0};
68 socklen_t ss = 0;
69 int rc = php_network_parse_network_address_with_port(addr,
70 strlen(addr), (struct sockaddr *) &sa, &ss);
71
72 if (SUCCESS == rc) {
73 int styp = strncmp(desc, "udp:", 4)
74 ? SOCK_STREAM
75 : SOCK_DGRAM;
76 int sfam = sa.ss_family == AF_INET6
77 ? ((struct sockaddr_in6 *) &sa)->sin6_family
78 : ((struct sockaddr_in *) &sa)->sin_family;
79
80 fd = socket(sfam, styp, 0);
81
82 if (fd > 0 && 0 != connect(fd, (struct sockaddr *) &sa, ss)) {
83 perror(desc);
84 close(fd);
85 fd = -1;
86 }
87 }
88 } else if (!strcmp(desc, "stdout")) {
89 fd = STDOUT_FILENO;
90 } else if (!strcmp(desc, "stderr")) {
91 fd = STDERR_FILENO;
92 } else if (!(fd = atoi(desc)) || -1 == fcntl(fd, F_GETFD)) {
93 fd = open(desc, O_WRONLY|O_APPEND|O_CREAT|O_CLOEXEC, 0664);
94 }
95 }
96 return fd;
97 }
98
99 typedef struct psi_object {
100 void *data;
101 void (*dtor)(void *data);
102 size_t size;
103 zend_object std;
104 } psi_object;
105
106 static inline psi_object *PSI_OBJ(zval *zv, zend_object *zo) {
107 if (zv) {
108 zo = Z_OBJ_P(zv);
109 }
110 if (!zo) {
111 return NULL;
112 }
113 return (void *) (((char *) zo) - zo->handlers->offset);
114 }
115
116 PHP_PSI_API zend_object *psi_object_init(zend_class_entry *ce);
117 PHP_PSI_API zend_object *psi_object_init_ex(zend_class_entry *ce, void *data, void (*dtor)(void *));
118 PHP_PSI_API zend_class_entry *psi_object_get_class_entry();
119
120 ZEND_BEGIN_MODULE_GLOBALS(psi)
121 char *engine;
122 char *directory;
123 char *search_path;
124 struct psi_context_ops *ops;
125 struct psi_context *context;
126 struct {
127 struct psi_plist *decls;
128 struct psi_plist *vars;
129 } blacklist;
130 ZEND_END_MODULE_GLOBALS(psi);
131
132 ZEND_EXTERN_MODULE_GLOBALS(psi);
133
134 #define PSI_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(psi, v)
135
136 #if defined(ZTS) && defined(COMPILE_DL_PSI)
137 ZEND_TSRMLS_CACHE_EXTERN();
138 #endif
139
140 #endif /* PHP_PSI_H */
141
142
143 /*
144 * Local variables:
145 * tab-width: 4
146 * c-basic-offset: 4
147 * End:
148 * vim600: noet sw=4 ts=4 fdm=marker
149 * vim<600: noet sw=4 ts=4
150 */