1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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: Brian Schaffner <brian@tool.net> |
16 : | Shane Caraveo <shane@caraveo.com> |
17 : | Zeev Suraski <zeev@zend.com> |
18 : +----------------------------------------------------------------------+
19 : */
20 :
21 : /* $Id: dl.c 289821 2009-10-21 06:42:08Z pajoye $ */
22 :
23 : #include "php.h"
24 : #include "dl.h"
25 : #include "php_globals.h"
26 : #include "php_ini.h"
27 : #include "ext/standard/info.h"
28 : #include "ext/standard/file.h"
29 :
30 : #include "SAPI.h"
31 :
32 : #if defined(HAVE_LIBDL)
33 : #include <stdlib.h>
34 : #include <stdio.h>
35 : #ifdef HAVE_STRING_H
36 : #include <string.h>
37 : #else
38 : #include <strings.h>
39 : #endif
40 : #ifdef PHP_WIN32
41 : #include "win32/param.h"
42 : #include "win32/winutil.h"
43 : #define GET_DL_ERROR() php_win_err()
44 : #elif defined(NETWARE)
45 : #include <sys/param.h>
46 : #define GET_DL_ERROR() dlerror()
47 : #else
48 : #include <sys/param.h>
49 : #define GET_DL_ERROR() DL_ERROR()
50 : #endif
51 : #endif /* defined(HAVE_LIBDL) */
52 :
53 : /* {{{ proto int dl(string extension_filename) U
54 : Load a PHP extension at runtime */
55 : PHPAPI PHP_FUNCTION(dl)
56 1 : {
57 : char *filename;
58 : int filename_len;
59 :
60 1 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&", &filename, &filename_len, ZEND_U_CONVERTER(UG(filesystem_encoding_conv))) == FAILURE) {
61 0 : return;
62 : }
63 :
64 1 : if (filename_len >= MAXPATHLEN) {
65 1 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "File name exceeds the maximum allowed length of %d characters", MAXPATHLEN);
66 1 : RETURN_FALSE;
67 : }
68 :
69 0 : if ((strncmp(sapi_module.name, "cgi", 3) != 0) &&
70 : (strcmp(sapi_module.name, "cli") != 0) &&
71 : (strncmp(sapi_module.name, "embed", 5) != 0)
72 : ) {
73 : #ifdef ZTS
74 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not supported in multithreaded Web servers - use extension=%s in your php.ini", filename);
75 : RETURN_FALSE;
76 : #else
77 0 : php_error_docref(NULL TSRMLS_CC, E_DEPRECATED, "dl() is deprecated - use extension=%s in your php.ini", filename);
78 : #endif
79 : }
80 :
81 0 : php_dl(filename, MODULE_TEMPORARY, return_value, 0 TSRMLS_CC);
82 0 : if (Z_LVAL_P(return_value) == 1) {
83 0 : EG(full_tables_cleanup) = 1;
84 : }
85 : }
86 : /* }}} */
87 :
88 : #if defined(HAVE_LIBDL)
89 :
90 : #ifdef ZTS
91 : #define USING_ZTS 1
92 : #else
93 : #define USING_ZTS 0
94 : #endif
95 :
96 : /* {{{ php_dl
97 : */
98 : PHPAPI int php_load_extension(char *filename, int type, int start_now TSRMLS_DC) /* {{{ */
99 0 : {
100 : void *handle;
101 : char *libpath;
102 : zend_module_entry *module_entry;
103 : zend_module_entry *(*get_module)(void);
104 : int error_type;
105 : char *extension_dir;
106 :
107 0 : if (type == MODULE_PERSISTENT) {
108 0 : extension_dir = INI_STR("extension_dir");
109 : } else {
110 0 : extension_dir = PG(extension_dir);
111 : }
112 :
113 0 : if (type == MODULE_TEMPORARY) {
114 0 : error_type = E_WARNING;
115 : } else {
116 0 : error_type = E_CORE_WARNING;
117 : }
118 :
119 : /* Check if passed filename contains directory separators */
120 0 : if (strchr(filename, '/') != NULL || strchr(filename, DEFAULT_SLASH) != NULL) {
121 : /* Passing modules with full path is not supported for dynamically loaded extensions */
122 0 : if (type == MODULE_TEMPORARY) {
123 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Temporary module name should contain only filename");
124 0 : return FAILURE;
125 : }
126 0 : libpath = estrdup(filename);
127 0 : } else if (extension_dir && extension_dir[0]) {
128 0 : int extension_dir_len = strlen(extension_dir);
129 :
130 0 : if (IS_SLASH(extension_dir[extension_dir_len-1])) {
131 0 : spprintf(&libpath, 0, "%s%s", extension_dir, filename); /* SAFE */
132 : } else {
133 0 : spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, filename); /* SAFE */
134 : }
135 : } else {
136 0 : return FAILURE; /* Not full path given or extension_dir is not set */
137 : }
138 :
139 : /* load dynamic symbol */
140 0 : handle = DL_LOAD(libpath);
141 0 : if (!handle) {
142 : #if PHP_WIN32
143 : char *err = GET_DL_ERROR();
144 : if (err) {
145 : php_error_docref(NULL TSRMLS_CC, error_type, "Unable to load dynamic library '%s' - %s", libpath, err);
146 : LocalFree(err);
147 : } else {
148 : php_error_docref(NULL TSRMLS_CC, error_type, "Unable to load dynamic library '%s' - %s", libpath, "Unknown reason");
149 : }
150 : #else
151 0 : php_error_docref(NULL TSRMLS_CC, error_type, "Unable to load dynamic library '%s' - %s", libpath, GET_DL_ERROR());
152 0 : GET_DL_ERROR(); /* free the buffer storing the error */
153 : #endif
154 0 : efree(libpath);
155 0 : return FAILURE;
156 : }
157 0 : efree(libpath);
158 :
159 0 : get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "get_module");
160 :
161 : /* Some OS prepend _ to symbol names while their dynamic linker
162 : * does not do that automatically. Thus we check manually for
163 : * _get_module. */
164 :
165 0 : if (!get_module) {
166 0 : get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "_get_module");
167 : }
168 :
169 0 : if (!get_module) {
170 0 : DL_UNLOAD(handle);
171 0 : php_error_docref(NULL TSRMLS_CC, error_type, "Invalid library (maybe not a PHP library) '%s'", filename);
172 0 : return FAILURE;
173 : }
174 0 : module_entry = get_module();
175 0 : if (module_entry->zend_api != ZEND_MODULE_API_NO) {
176 : /* Check for pre-4.1.0 module which has a slightly different module_entry structure :( */
177 : struct pre_4_1_0_module_entry {
178 : char *name;
179 : zend_function_entry *functions;
180 : int (*module_startup_func)(INIT_FUNC_ARGS);
181 : int (*module_shutdown_func)(SHUTDOWN_FUNC_ARGS);
182 : int (*request_startup_func)(INIT_FUNC_ARGS);
183 : int (*request_shutdown_func)(SHUTDOWN_FUNC_ARGS);
184 : void (*info_func)(ZEND_MODULE_INFO_FUNC_ARGS);
185 : int (*global_startup_func)(void);
186 : int (*global_shutdown_func)(void);
187 : int globals_id;
188 : int module_started;
189 : unsigned char type;
190 : void *handle;
191 : int module_number;
192 : unsigned char zend_debug;
193 : unsigned char zts;
194 : unsigned int zend_api;
195 : };
196 :
197 : const char *name;
198 : int zend_api;
199 :
200 0 : if ((((struct pre_4_1_0_module_entry *)module_entry)->zend_api > 20000000) &&
201 : (((struct pre_4_1_0_module_entry *)module_entry)->zend_api < 20010901)
202 : ) {
203 0 : name = ((struct pre_4_1_0_module_entry *)module_entry)->name;
204 0 : zend_api = ((struct pre_4_1_0_module_entry *)module_entry)->zend_api;
205 : } else {
206 0 : name = module_entry->name;
207 0 : zend_api = module_entry->zend_api;
208 : }
209 :
210 0 : php_error_docref(NULL TSRMLS_CC, error_type,
211 : "%s: Unable to initialize module\n"
212 : "Module compiled with module API=%d\n"
213 : "PHP compiled with module API=%d\n"
214 : "These options need to match\n",
215 : name, zend_api, ZEND_MODULE_API_NO);
216 0 : DL_UNLOAD(handle);
217 0 : return FAILURE;
218 : }
219 0 : if(strcmp(module_entry->build_id, ZEND_MODULE_BUILD_ID)) {
220 0 : php_error_docref(NULL TSRMLS_CC, error_type,
221 : "%s: Unable to initialize module\n"
222 : "Module compiled with build ID=%s\n"
223 : "PHP compiled with build ID=%s\n"
224 : "These options need to match\n",
225 : module_entry->name, module_entry->build_id, ZEND_MODULE_BUILD_ID);
226 0 : DL_UNLOAD(handle);
227 0 : return FAILURE;
228 : }
229 0 : module_entry->type = type;
230 0 : module_entry->module_number = zend_next_free_module();
231 0 : module_entry->handle = handle;
232 :
233 0 : if ((module_entry = zend_register_module_ex(module_entry TSRMLS_CC)) == NULL) {
234 0 : DL_UNLOAD(handle);
235 0 : return FAILURE;
236 : }
237 :
238 0 : if ((type == MODULE_TEMPORARY || start_now) && zend_startup_module_ex(module_entry TSRMLS_CC) == FAILURE) {
239 0 : DL_UNLOAD(handle);
240 0 : return FAILURE;
241 : }
242 :
243 0 : if ((type == MODULE_TEMPORARY || start_now) && module_entry->request_startup_func) {
244 0 : if (module_entry->request_startup_func(type, module_entry->module_number TSRMLS_CC) == FAILURE) {
245 0 : php_error_docref(NULL TSRMLS_CC, error_type, "Unable to initialize module '%s'", module_entry->name);
246 0 : DL_UNLOAD(handle);
247 0 : return FAILURE;
248 : }
249 : }
250 0 : return SUCCESS;
251 : }
252 : /* }}} */
253 :
254 : /* {{{ php_dl
255 : */
256 : PHPAPI void php_dl(char *file, int type, zval *return_value, int start_now TSRMLS_DC)
257 0 : {
258 : /* Load extension */
259 0 : if (php_load_extension(file, type, start_now TSRMLS_CC) == FAILURE) {
260 0 : RETVAL_FALSE;
261 : } else {
262 0 : RETVAL_TRUE;
263 : }
264 0 : }
265 : /* }}} */
266 :
267 : PHP_MINFO_FUNCTION(dl)
268 44 : {
269 44 : php_info_print_table_row(2, "Dynamic Library Support", "enabled");
270 44 : }
271 :
272 : #else
273 :
274 : PHPAPI void php_dl(char *file, int type, zval *return_value, int start_now TSRMLS_DC)
275 : {
276 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot dynamically load %s - dynamic modules are not supported", file);
277 : RETURN_FALSE;
278 : }
279 :
280 : PHP_MINFO_FUNCTION(dl)
281 : {
282 : PUTS("Dynamic Library support not available<br />.\n");
283 : }
284 :
285 : #endif
286 :
287 : /*
288 : * Local variables:
289 : * tab-width: 4
290 : * c-basic-offset: 4
291 : * End:
292 : * vim600: sw=4 ts=4 fdm=marker
293 : * vim<600: sw=4 ts=4
294 : */
|