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