xref: /php-src/ext/dl_test/dl_test.c (revision 452c5ac9)
1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) The PHP Group                                          |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.01 of the PHP license,      |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | https://www.php.net/license/3_01.txt                                 |
9   | If you did not receive a copy of the PHP license and are unable to   |
10   | obtain it through the world-wide-web, please send a note to          |
11   | license@php.net so we can mail you a copy immediately.               |
12   +----------------------------------------------------------------------+
13   | Author: Arnaud Le Blanc <arnaud.lb@gmail.com>                        |
14   +----------------------------------------------------------------------+
15 */
16 
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
20 
21 #include "php.h"
22 #include "ext/standard/info.h"
23 #include "php_dl_test.h"
24 #include "dl_test_arginfo.h"
25 
26 ZEND_DECLARE_MODULE_GLOBALS(dl_test)
27 
28 /* {{{ void dl_test_test1() */
PHP_FUNCTION(dl_test_test1)29 PHP_FUNCTION(dl_test_test1)
30 {
31 	ZEND_PARSE_PARAMETERS_NONE();
32 
33 	php_printf("The extension %s is loaded and working!\r\n", "dl_test");
34 }
35 /* }}} */
36 
37 /* {{{ string dl_test_test2( [ string $var ] ) */
PHP_FUNCTION(dl_test_test2)38 PHP_FUNCTION(dl_test_test2)
39 {
40 	char *var = "World";
41 	size_t var_len = sizeof("World") - 1;
42 	zend_string *retval;
43 
44 	ZEND_PARSE_PARAMETERS_START(0, 1)
45 		Z_PARAM_OPTIONAL
46 		Z_PARAM_STRING(var, var_len)
47 	ZEND_PARSE_PARAMETERS_END();
48 
49 	retval = strpprintf(0, "Hello %s", var);
50 
51 	RETURN_STR(retval);
52 }
53 /* }}}*/
54 
55 /* {{{ PHP_DL_TEST_USE_REGISTER_FUNCTIONS_DIRECTLY */
56 ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_dl_test_use_register_functions_directly, 0, 0, IS_STRING, 0)
ZEND_END_ARG_INFO()57 ZEND_END_ARG_INFO()
58 
59 PHP_FUNCTION(dl_test_use_register_functions_directly)
60 {
61 	ZEND_PARSE_PARAMETERS_NONE();
62 
63 	RETURN_STRING("OK");
64 }
65 
66 static const zend_function_entry php_dl_test_use_register_functions_directly_functions[] = {
67 	ZEND_FENTRY(dl_test_use_register_functions_directly, ZEND_FN(dl_test_use_register_functions_directly), arginfo_dl_test_use_register_functions_directly, 0)
68 	ZEND_FE_END
69 };
70 /* }}} */
71 
72 /* {{{ INI */
73 PHP_INI_BEGIN()
74 	STD_PHP_INI_ENTRY("dl_test.long",       "0", PHP_INI_ALL, OnUpdateLong,   long_value,   zend_dl_test_globals, dl_test_globals)
75 	STD_PHP_INI_ENTRY("dl_test.string", "hello", PHP_INI_ALL, OnUpdateString, string_value, zend_dl_test_globals, dl_test_globals)
PHP_INI_END()76 PHP_INI_END()
77 /* }}} */
78 
79 /* {{{ PHP_MINIT_FUNCTION */
80 PHP_MINIT_FUNCTION(dl_test)
81 {
82 	/* Test backwards compatibility */
83 	if (getenv("PHP_DL_TEST_USE_OLD_REGISTER_INI_ENTRIES")) {
84 		zend_register_ini_entries(ini_entries, module_number);
85 	} else {
86 		REGISTER_INI_ENTRIES();
87 	}
88 
89 	if (getenv("PHP_DL_TEST_USE_REGISTER_FUNCTIONS_DIRECTLY")) {
90 		zend_register_functions(NULL, php_dl_test_use_register_functions_directly_functions, NULL, type);
91 	}
92 
93 	if (getenv("PHP_DL_TEST_MODULE_DEBUG")) {
94 		fprintf(stderr, "DL TEST MINIT\n");
95 	}
96 
97 	register_dl_test_symbols(module_number);
98 
99 	return SUCCESS;
100 }
101 /* }}} */
102 
103 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(dl_test)104 static PHP_MSHUTDOWN_FUNCTION(dl_test)
105 {
106 	/* Test backwards compatibility */
107 	if (getenv("PHP_DL_TEST_USE_OLD_REGISTER_INI_ENTRIES")) {
108 		zend_unregister_ini_entries(module_number);
109 	} else {
110 		UNREGISTER_INI_ENTRIES();
111 	}
112 
113 	if (getenv("PHP_DL_TEST_MODULE_DEBUG")) {
114 		fprintf(stderr, "DL TEST MSHUTDOWN\n");
115 	}
116 
117 	return SUCCESS;
118 }
119 /* }}} */
120 
121 /* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(dl_test)122 PHP_RINIT_FUNCTION(dl_test)
123 {
124 #if defined(ZTS) && defined(COMPILE_DL_DL_TEST)
125 	ZEND_TSRMLS_CACHE_UPDATE();
126 #endif
127 
128 	if (getenv("PHP_DL_TEST_MODULE_DEBUG")) {
129 		fprintf(stderr, "DL TEST RINIT\n");
130 	}
131 
132 	return SUCCESS;
133 }
134 /* }}} */
135 
136 /* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(dl_test)137 PHP_RSHUTDOWN_FUNCTION(dl_test)
138 {
139 	if (getenv("PHP_DL_TEST_MODULE_DEBUG")) {
140 		fprintf(stderr, "DL TEST RSHUTDOWN\n");
141 	}
142 
143 	return SUCCESS;
144 }
145 /* }}} */
146 
147 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(dl_test)148 PHP_MINFO_FUNCTION(dl_test)
149 {
150 	php_info_print_table_start();
151 	php_info_print_table_row(2, "dl_test support", "enabled");
152 	php_info_print_table_end();
153 
154 	DISPLAY_INI_ENTRIES();
155 }
156 /* }}} */
157 
158 /* {{{ PHP_GINIT_FUNCTION */
PHP_GINIT_FUNCTION(dl_test)159 static PHP_GINIT_FUNCTION(dl_test)
160 {
161 #if defined(COMPILE_DL_DL_TEST) && defined(ZTS)
162 	ZEND_TSRMLS_CACHE_UPDATE();
163 #endif
164 	memset(dl_test_globals, 0, sizeof(*dl_test_globals));
165 }
166 /* }}} */
167 
168 /* {{{ dl_test_module_entry */
169 zend_module_entry dl_test_module_entry = {
170 	STANDARD_MODULE_HEADER,
171 	"dl_test",
172 	ext_functions,
173 	PHP_MINIT(dl_test),
174 	PHP_MSHUTDOWN(dl_test),
175 	PHP_RINIT(dl_test),
176 	PHP_RSHUTDOWN(dl_test),
177 	PHP_MINFO(dl_test),
178 	PHP_DL_TEST_VERSION,
179 	PHP_MODULE_GLOBALS(dl_test),
180 	PHP_GINIT(dl_test),
181 	NULL,
182 	NULL,
183 	STANDARD_MODULE_PROPERTIES_EX
184 };
185 /* }}} */
186 
187 #ifdef COMPILE_DL_DL_TEST
188 # ifdef ZTS
189 ZEND_TSRMLS_CACHE_DEFINE()
190 # endif
191 ZEND_GET_MODULE(dl_test)
192 #endif
193