xref: /PHP-8.4/ext/dl_test/dl_test.c (revision f2e199e8)
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 	return SUCCESS;
98 }
99 /* }}} */
100 
101 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(dl_test)102 static PHP_MSHUTDOWN_FUNCTION(dl_test)
103 {
104 	/* Test backwards compatibility */
105 	if (getenv("PHP_DL_TEST_USE_OLD_REGISTER_INI_ENTRIES")) {
106 		zend_unregister_ini_entries(module_number);
107 	} else {
108 		UNREGISTER_INI_ENTRIES();
109 	}
110 
111 	if (getenv("PHP_DL_TEST_MODULE_DEBUG")) {
112 		fprintf(stderr, "DL TEST MSHUTDOWN\n");
113 	}
114 
115 	return SUCCESS;
116 }
117 /* }}} */
118 
119 /* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(dl_test)120 PHP_RINIT_FUNCTION(dl_test)
121 {
122 #if defined(ZTS) && defined(COMPILE_DL_DL_TEST)
123 	ZEND_TSRMLS_CACHE_UPDATE();
124 #endif
125 
126 	if (getenv("PHP_DL_TEST_MODULE_DEBUG")) {
127 		fprintf(stderr, "DL TEST RINIT\n");
128 	}
129 
130 	return SUCCESS;
131 }
132 /* }}} */
133 
134 /* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(dl_test)135 PHP_RSHUTDOWN_FUNCTION(dl_test)
136 {
137 	if (getenv("PHP_DL_TEST_MODULE_DEBUG")) {
138 		fprintf(stderr, "DL TEST RSHUTDOWN\n");
139 	}
140 
141 	return SUCCESS;
142 }
143 /* }}} */
144 
145 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(dl_test)146 PHP_MINFO_FUNCTION(dl_test)
147 {
148 	php_info_print_table_start();
149 	php_info_print_table_row(2, "dl_test support", "enabled");
150 	php_info_print_table_end();
151 
152 	DISPLAY_INI_ENTRIES();
153 }
154 /* }}} */
155 
156 /* {{{ PHP_GINIT_FUNCTION */
PHP_GINIT_FUNCTION(dl_test)157 static PHP_GINIT_FUNCTION(dl_test)
158 {
159 #if defined(COMPILE_DL_DL_TEST) && defined(ZTS)
160 	ZEND_TSRMLS_CACHE_UPDATE();
161 #endif
162 	memset(dl_test_globals, 0, sizeof(*dl_test_globals));
163 }
164 /* }}} */
165 
166 /* {{{ dl_test_module_entry */
167 zend_module_entry dl_test_module_entry = {
168 	STANDARD_MODULE_HEADER,
169 	"dl_test",
170 	ext_functions,
171 	PHP_MINIT(dl_test),
172 	PHP_MSHUTDOWN(dl_test),
173 	PHP_RINIT(dl_test),
174 	PHP_RSHUTDOWN(dl_test),
175 	PHP_MINFO(dl_test),
176 	PHP_DL_TEST_VERSION,
177 	PHP_MODULE_GLOBALS(dl_test),
178 	PHP_GINIT(dl_test),
179 	NULL,
180 	NULL,
181 	STANDARD_MODULE_PROPERTIES_EX
182 };
183 /* }}} */
184 
185 #ifdef COMPILE_DL_DL_TEST
186 # ifdef ZTS
187 ZEND_TSRMLS_CACHE_DEFINE()
188 # endif
189 ZEND_GET_MODULE(dl_test)
190 #endif
191