1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
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 272374 2008-12-31 11:17:49Z sebastian $ */
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 :
29 : #include "SAPI.h"
30 :
31 : #if defined(HAVE_LIBDL) || HAVE_MACH_O_DYLD_H
32 : #include <stdlib.h>
33 : #include <stdio.h>
34 :
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 :
52 : #endif /* defined(HAVE_LIBDL) || HAVE_MACH_O_DYLD_H */
53 :
54 :
55 : /* {{{ proto int dl(string extension_filename)
56 : Load a PHP extension at runtime */
57 : PHP_FUNCTION(dl)
58 1 : {
59 : zval **file;
60 :
61 : /* obtain arguments */
62 1 : if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &file) == FAILURE) {
63 0 : WRONG_PARAM_COUNT;
64 : }
65 :
66 1 : convert_to_string_ex(file);
67 :
68 1 : if (!PG(enable_dl)) {
69 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Dynamically loaded extensions aren't enabled");
70 0 : RETURN_FALSE;
71 1 : } else if (PG(safe_mode)) {
72 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Dynamically loaded extensions aren't allowed when running in Safe Mode");
73 0 : RETURN_FALSE;
74 : }
75 :
76 1 : if (Z_STRLEN_PP(file) >= MAXPATHLEN) {
77 1 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "File name exceeds the maximum allowed length of %d characters", MAXPATHLEN);
78 1 : RETURN_FALSE;
79 : }
80 :
81 0 : if ((strncmp(sapi_module.name, "cgi", 3)!=0) &&
82 : (strcmp(sapi_module.name, "cli")!=0) &&
83 : (strncmp(sapi_module.name, "embed", 5)!=0)) {
84 : #ifdef ZTS
85 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not supported in multithreaded Web servers - use extension=%s in your php.ini", Z_STRVAL_PP(file));
86 : RETURN_FALSE;
87 : #else
88 0 : php_error_docref(NULL TSRMLS_CC, E_STRICT, "dl() is deprecated - use extension=%s in your php.ini", Z_STRVAL_PP(file));
89 : #endif
90 : }
91 :
92 0 : php_dl(*file, MODULE_TEMPORARY, return_value, 0 TSRMLS_CC);
93 0 : EG(full_tables_cleanup) = 1;
94 : }
95 :
96 : /* }}} */
97 :
98 :
99 : #if defined(HAVE_LIBDL) || HAVE_MACH_O_DYLD_H
100 :
101 : #ifdef ZTS
102 : #define USING_ZTS 1
103 : #else
104 : #define USING_ZTS 0
105 : #endif
106 :
107 : /* {{{ php_dl
108 : */
109 : void php_dl(zval *file, int type, zval *return_value, int start_now TSRMLS_DC)
110 0 : {
111 : void *handle;
112 : char *libpath;
113 : zend_module_entry *module_entry;
114 : zend_module_entry *(*get_module)(void);
115 : int error_type;
116 : char *extension_dir;
117 :
118 0 : if (type == MODULE_PERSISTENT) {
119 0 : extension_dir = INI_STR("extension_dir");
120 : } else {
121 0 : extension_dir = PG(extension_dir);
122 : }
123 :
124 0 : if (type == MODULE_TEMPORARY) {
125 0 : error_type = E_WARNING;
126 : } else {
127 0 : error_type = E_CORE_WARNING;
128 : }
129 :
130 0 : if (extension_dir && extension_dir[0]){
131 0 : int extension_dir_len = strlen(extension_dir);
132 :
133 0 : if (type == MODULE_TEMPORARY) {
134 0 : if (strchr(Z_STRVAL_P(file), '/') != NULL || strchr(Z_STRVAL_P(file), DEFAULT_SLASH) != NULL) {
135 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Temporary module name should contain only filename");
136 0 : RETURN_FALSE;
137 : }
138 : }
139 :
140 0 : if (IS_SLASH(extension_dir[extension_dir_len-1])) {
141 0 : spprintf(&libpath, 0, "%s%s", extension_dir, Z_STRVAL_P(file));
142 : } else {
143 0 : spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, Z_STRVAL_P(file));
144 : }
145 : } else {
146 0 : libpath = estrndup(Z_STRVAL_P(file), Z_STRLEN_P(file));
147 : }
148 :
149 : /* load dynamic symbol */
150 0 : handle = DL_LOAD(libpath);
151 0 : if (!handle) {
152 0 : php_error_docref(NULL TSRMLS_CC, error_type, "Unable to load dynamic library '%s' - %s", libpath, GET_DL_ERROR());
153 0 : GET_DL_ERROR(); /* free the buffer storing the error */
154 0 : efree(libpath);
155 0 : RETURN_FALSE;
156 : }
157 :
158 0 : efree(libpath);
159 :
160 0 : get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "get_module");
161 :
162 : /*
163 : * some OS prepend _ to symbol names while their dynamic linker
164 : * does not do that automatically. Thus we check manually for
165 : * _get_module.
166 : */
167 :
168 0 : if (!get_module)
169 0 : get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "_get_module");
170 :
171 0 : if (!get_module) {
172 0 : DL_UNLOAD(handle);
173 0 : php_error_docref(NULL TSRMLS_CC, error_type, "Invalid library (maybe not a PHP library) '%s' ", Z_STRVAL_P(file));
174 0 : RETURN_FALSE;
175 : }
176 0 : module_entry = get_module();
177 0 : if ((module_entry->zend_debug != ZEND_DEBUG) || (module_entry->zts != USING_ZTS)
178 : || (module_entry->zend_api != ZEND_MODULE_API_NO)) {
179 : /* Check for pre-4.1.0 module which has a slightly different module_entry structure :( */
180 : struct pre_4_1_0_module_entry {
181 : char *name;
182 : zend_function_entry *functions;
183 : int (*module_startup_func)(INIT_FUNC_ARGS);
184 : int (*module_shutdown_func)(SHUTDOWN_FUNC_ARGS);
185 : int (*request_startup_func)(INIT_FUNC_ARGS);
186 : int (*request_shutdown_func)(SHUTDOWN_FUNC_ARGS);
187 : void (*info_func)(ZEND_MODULE_INFO_FUNC_ARGS);
188 : int (*global_startup_func)(void);
189 : int (*global_shutdown_func)(void);
190 : int globals_id;
191 : int module_started;
192 : unsigned char type;
193 : void *handle;
194 : int module_number;
195 : unsigned char zend_debug;
196 : unsigned char zts;
197 : unsigned int zend_api;
198 : };
199 :
200 : char *name;
201 : int zend_api;
202 : unsigned char zend_debug, zts;
203 :
204 0 : if ((((struct pre_4_1_0_module_entry *)module_entry)->zend_api > 20000000) &&
205 : (((struct pre_4_1_0_module_entry *)module_entry)->zend_api < 20010901)
206 : ) {
207 0 : name = ((struct pre_4_1_0_module_entry *)module_entry)->name;
208 0 : zend_api = ((struct pre_4_1_0_module_entry *)module_entry)->zend_api;
209 0 : zend_debug = ((struct pre_4_1_0_module_entry *)module_entry)->zend_debug;
210 0 : zts = ((struct pre_4_1_0_module_entry *)module_entry)->zts;
211 : } else {
212 0 : name = module_entry->name;
213 0 : zend_api = module_entry->zend_api;
214 0 : zend_debug = module_entry->zend_debug;
215 0 : zts = module_entry->zts;
216 : }
217 :
218 0 : php_error_docref(NULL TSRMLS_CC, error_type,
219 : "%s: Unable to initialize module\n"
220 : "Module compiled with module API=%d, debug=%d, thread-safety=%d\n"
221 : "PHP compiled with module API=%d, debug=%d, thread-safety=%d\n"
222 : "These options need to match\n",
223 : name, zend_api, zend_debug, zts,
224 : ZEND_MODULE_API_NO, ZEND_DEBUG, USING_ZTS);
225 0 : DL_UNLOAD(handle);
226 0 : RETURN_FALSE;
227 : }
228 0 : module_entry->type = type;
229 0 : module_entry->module_number = zend_next_free_module();
230 0 : module_entry->handle = handle;
231 :
232 0 : if ((module_entry = zend_register_module_ex(module_entry TSRMLS_CC)) == NULL) {
233 0 : DL_UNLOAD(handle);
234 0 : RETURN_FALSE;
235 : }
236 :
237 0 : if ((type == MODULE_TEMPORARY || start_now) && zend_startup_module_ex(module_entry TSRMLS_CC) == FAILURE) {
238 0 : DL_UNLOAD(handle);
239 0 : RETURN_FALSE;
240 : }
241 :
242 0 : if ((type == MODULE_TEMPORARY || start_now) && module_entry->request_startup_func) {
243 0 : if (module_entry->request_startup_func(type, module_entry->module_number TSRMLS_CC) == FAILURE) {
244 0 : php_error_docref(NULL TSRMLS_CC, error_type, "Unable to initialize module '%s'", module_entry->name);
245 0 : DL_UNLOAD(handle);
246 0 : RETURN_FALSE;
247 : }
248 : }
249 0 : RETURN_TRUE;
250 : }
251 : /* }}} */
252 :
253 : PHP_MINFO_FUNCTION(dl)
254 6 : {
255 6 : php_info_print_table_row(2, "Dynamic Library Support", "enabled");
256 6 : }
257 :
258 : #else
259 :
260 : void php_dl(zval *file, int type, zval *return_value, int start_now TSRMLS_DC)
261 : {
262 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot dynamically load %s - dynamic modules are not supported", Z_STRVAL_P(file));
263 : RETURN_FALSE;
264 : }
265 :
266 : PHP_MINFO_FUNCTION(dl)
267 : {
268 : PUTS("Dynamic Library support not available<br />.\n");
269 : }
270 :
271 : #endif
272 :
273 : /*
274 : * Local variables:
275 : * tab-width: 4
276 : * c-basic-offset: 4
277 : * End:
278 : * vim600: sw=4 ts=4 fdm=marker
279 : * vim<600: sw=4 ts=4
280 : */
|