8d7649ccdc370eb64f060be8434a4941540fab3c
[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 "php_network.h"
54
55 static inline int psi_fdopen(const char *desc)
56 {
57 int fd = -1;
58
59 if (desc) {
60 char *addr = strstr(desc, "://");
61
62 if (addr) {
63 addr += 3;
64 }
65 if (addr && *addr) {
66 struct sockaddr_storage sa = {0};
67 socklen_t ss = 0;
68 int rc = php_network_parse_network_address_with_port(addr,
69 strlen(addr), (struct sockaddr *) &sa, &ss);
70
71 if (SUCCESS == rc) {
72 int styp = strncmp(desc, "udp:", 4)
73 ? SOCK_STREAM
74 : SOCK_DGRAM;
75 int sfam = sa.ss_family == AF_INET6
76 ? ((struct sockaddr_in6 *) &sa)->sin6_family
77 : ((struct sockaddr_in *) &sa)->sin_family;
78
79 fd = socket(sfam, styp, 0);
80
81 if (fd > 0 && 0 != connect(fd, (struct sockaddr *) &sa, ss)) {
82 perror(desc);
83 close(fd);
84 fd = -1;
85 }
86 }
87 } else if (!strcmp(desc, "stdout")) {
88 fd = STDOUT_FILENO;
89 } else if (!strcmp(desc, "stderr")) {
90 fd = STDERR_FILENO;
91 } else if (!(fd = atoi(desc)) || -1 == fcntl(fd, F_GETFD)) {
92 fd = open(desc, O_WRONLY|O_APPEND|O_CREAT|O_CLOEXEC, 0664);
93 }
94 }
95 return fd;
96 }
97
98 typedef struct psi_object {
99 void *data;
100 void (*dtor)(void *data);
101 size_t size;
102 zend_object std;
103 } psi_object;
104
105 static inline psi_object *PSI_OBJ(zval *zv, zend_object *zo) {
106 if (zv) {
107 zo = Z_OBJ_P(zv);
108 }
109 if (!zo) {
110 return NULL;
111 }
112 return (void *) (((char *) zo) - zo->handlers->offset);
113 }
114
115 PHP_PSI_API zend_object *psi_object_init(zend_class_entry *ce);
116 PHP_PSI_API zend_object *psi_object_init_ex(zend_class_entry *ce, void *data, void (*dtor)(void *));
117 PHP_PSI_API zend_class_entry *psi_object_get_class_entry();
118
119 ZEND_BEGIN_MODULE_GLOBALS(psi)
120 char *engine;
121 char *directory;
122 char *search_path;
123 struct psi_context_ops *ops;
124 struct psi_context *context;
125 struct {
126 struct psi_plist *decls;
127 struct psi_plist *vars;
128 } blacklist;
129 ZEND_END_MODULE_GLOBALS(psi);
130
131 ZEND_EXTERN_MODULE_GLOBALS(psi);
132
133 #define PSI_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(psi, v)
134
135 #if defined(ZTS) && defined(COMPILE_DL_PSI)
136 ZEND_TSRMLS_CACHE_EXTERN();
137 #endif
138
139 #endif /* PHP_PSI_H */
140
141
142 /*
143 * Local variables:
144 * tab-width: 4
145 * c-basic-offset: 4
146 * End:
147 * vim600: noet sw=4 ts=4 fdm=marker
148 * vim<600: noet sw=4 ts=4
149 */