Back to index

php5  5.3.10
dl.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Brian Schaffner <brian@tool.net>                            |
00016    |          Shane Caraveo <shane@caraveo.com>                           |
00017    |          Zeev Suraski <zeev@zend.com>                                |
00018    +----------------------------------------------------------------------+
00019 */
00020 
00021 /* $Id: dl.c 321634 2012-01-01 13:15:04Z felipe $ */
00022 
00023 #include "php.h"
00024 #include "dl.h"
00025 #include "php_globals.h"
00026 #include "php_ini.h"
00027 #include "ext/standard/info.h"
00028 
00029 #include "SAPI.h"
00030 
00031 #if defined(HAVE_LIBDL)
00032 #include <stdlib.h>
00033 #include <stdio.h>
00034 #ifdef HAVE_STRING_H
00035 #include <string.h>
00036 #else
00037 #include <strings.h>
00038 #endif
00039 #ifdef PHP_WIN32
00040 #include "win32/param.h"
00041 #include "win32/winutil.h"
00042 #define GET_DL_ERROR()      php_win_err()
00043 #elif defined(NETWARE)
00044 #include <sys/param.h>
00045 #define GET_DL_ERROR()      dlerror()
00046 #else
00047 #include <sys/param.h>
00048 #define GET_DL_ERROR()      DL_ERROR()
00049 #endif
00050 #endif /* defined(HAVE_LIBDL) */
00051 
00052 /* {{{ proto int dl(string extension_filename)
00053    Load a PHP extension at runtime */
00054 PHPAPI PHP_FUNCTION(dl)
00055 {
00056        char *filename;
00057        int filename_len;
00058 
00059        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
00060               return;
00061        }
00062 
00063        if (!PG(enable_dl)) {
00064               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Dynamically loaded extensions aren't enabled");
00065               RETURN_FALSE;
00066        } else if (PG(safe_mode)) {
00067               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Dynamically loaded extensions aren't allowed when running in Safe Mode");
00068               RETURN_FALSE;
00069        }
00070 
00071        if (filename_len >= MAXPATHLEN) {
00072               php_error_docref(NULL TSRMLS_CC, E_WARNING, "File name exceeds the maximum allowed length of %d characters", MAXPATHLEN);
00073               RETURN_FALSE;
00074        }
00075 
00076        if ((strncmp(sapi_module.name, "cgi", 3) != 0) &&
00077               (strcmp(sapi_module.name, "cli") != 0) &&
00078               (strncmp(sapi_module.name, "embed", 5) != 0)
00079        ) {
00080 #ifdef ZTS
00081               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not supported in multithreaded Web servers - use extension=%s in your php.ini", filename);
00082               RETURN_FALSE;
00083 #else
00084               php_error_docref(NULL TSRMLS_CC, E_DEPRECATED, "dl() is deprecated - use extension=%s in your php.ini", filename);
00085 #endif
00086        }
00087 
00088        php_dl(filename, MODULE_TEMPORARY, return_value, 0 TSRMLS_CC);
00089        if (Z_LVAL_P(return_value) == 1) {
00090               EG(full_tables_cleanup) = 1;
00091        }
00092 }
00093 /* }}} */
00094 
00095 #if defined(HAVE_LIBDL)
00096 
00097 #ifdef ZTS
00098 #define USING_ZTS 1
00099 #else
00100 #define USING_ZTS 0
00101 #endif
00102 
00103 /* {{{ php_dl
00104  */
00105 PHPAPI int php_load_extension(char *filename, int type, int start_now TSRMLS_DC) /* {{{ */
00106 {
00107        void *handle;
00108        char *libpath;
00109        zend_module_entry *module_entry;
00110        zend_module_entry *(*get_module)(void);
00111        int error_type;
00112        char *extension_dir;
00113 
00114        if (type == MODULE_PERSISTENT) {
00115               extension_dir = INI_STR("extension_dir");
00116        } else {
00117               extension_dir = PG(extension_dir);
00118        }
00119 
00120        if (type == MODULE_TEMPORARY) {
00121               error_type = E_WARNING;
00122        } else {
00123               error_type = E_CORE_WARNING;
00124        }
00125 
00126        /* Check if passed filename contains directory separators */
00127        if (strchr(filename, '/') != NULL || strchr(filename, DEFAULT_SLASH) != NULL) {
00128               /* Passing modules with full path is not supported for dynamically loaded extensions */
00129               if (type == MODULE_TEMPORARY) {
00130                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Temporary module name should contain only filename");
00131                      return FAILURE;
00132               }
00133               libpath = estrdup(filename);
00134        } else if (extension_dir && extension_dir[0]) {
00135               int extension_dir_len = strlen(extension_dir);
00136 
00137               if (IS_SLASH(extension_dir[extension_dir_len-1])) {
00138                      spprintf(&libpath, 0, "%s%s", extension_dir, filename); /* SAFE */
00139               } else {
00140                      spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, filename); /* SAFE */
00141               }
00142        } else {
00143               return FAILURE; /* Not full path given or extension_dir is not set */
00144        }
00145 
00146        /* load dynamic symbol */
00147        handle = DL_LOAD(libpath);
00148        if (!handle) {
00149 #if PHP_WIN32
00150               char *err = GET_DL_ERROR();
00151               if (err && (*err != "")) {
00152                      php_error_docref(NULL TSRMLS_CC, error_type, "Unable to load dynamic library '%s' - %s", libpath, err);
00153                      LocalFree(err);
00154               } else {
00155                      php_error_docref(NULL TSRMLS_CC, error_type, "Unable to load dynamic library '%s' - %s", libpath, "Unknown reason");
00156               }
00157 #else
00158               php_error_docref(NULL TSRMLS_CC, error_type, "Unable to load dynamic library '%s' - %s", libpath, GET_DL_ERROR());
00159               GET_DL_ERROR(); /* free the buffer storing the error */
00160 #endif
00161               efree(libpath);
00162               return FAILURE;
00163        }
00164        efree(libpath);
00165 
00166        get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "get_module");
00167 
00168        /* Some OS prepend _ to symbol names while their dynamic linker
00169         * does not do that automatically. Thus we check manually for
00170         * _get_module. */
00171 
00172        if (!get_module) {
00173               get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "_get_module");
00174        }
00175 
00176        if (!get_module) {
00177               DL_UNLOAD(handle);
00178               php_error_docref(NULL TSRMLS_CC, error_type, "Invalid library (maybe not a PHP library) '%s'", filename);
00179               return FAILURE;
00180        }
00181        module_entry = get_module();
00182        if (module_entry->zend_api != ZEND_MODULE_API_NO) {
00183               /* Check for pre-4.1.0 module which has a slightly different module_entry structure :( */
00184                      struct pre_4_1_0_module_entry {
00185                             char *name;
00186                             zend_function_entry *functions;
00187                             int (*module_startup_func)(INIT_FUNC_ARGS);
00188                             int (*module_shutdown_func)(SHUTDOWN_FUNC_ARGS);
00189                             int (*request_startup_func)(INIT_FUNC_ARGS);
00190                             int (*request_shutdown_func)(SHUTDOWN_FUNC_ARGS);
00191                             void (*info_func)(ZEND_MODULE_INFO_FUNC_ARGS);
00192                             int (*global_startup_func)(void);
00193                             int (*global_shutdown_func)(void);
00194                             int globals_id;
00195                             int module_started;
00196                             unsigned char type;
00197                             void *handle;
00198                             int module_number;
00199                             unsigned char zend_debug;
00200                             unsigned char zts;
00201                             unsigned int zend_api;
00202                      };
00203 
00204                      const char *name;
00205                      int zend_api;
00206 
00207                      if ((((struct pre_4_1_0_module_entry *)module_entry)->zend_api > 20000000) &&
00208                             (((struct pre_4_1_0_module_entry *)module_entry)->zend_api < 20010901)
00209                      ) {
00210                             name          = ((struct pre_4_1_0_module_entry *)module_entry)->name;
00211                             zend_api      = ((struct pre_4_1_0_module_entry *)module_entry)->zend_api;
00212                      } else {
00213                             name          = module_entry->name;
00214                             zend_api      = module_entry->zend_api;
00215                      }
00216 
00217                      php_error_docref(NULL TSRMLS_CC, error_type,
00218                                    "%s: Unable to initialize module\n"
00219                                    "Module compiled with module API=%d\n"
00220                                    "PHP    compiled with module API=%d\n"
00221                                    "These options need to match\n",
00222                                    name, zend_api, ZEND_MODULE_API_NO);
00223                      DL_UNLOAD(handle);
00224                      return FAILURE;
00225        }
00226        if(strcmp(module_entry->build_id, ZEND_MODULE_BUILD_ID)) {
00227               php_error_docref(NULL TSRMLS_CC, error_type,
00228                             "%s: Unable to initialize module\n"
00229                             "Module compiled with build ID=%s\n"
00230                             "PHP    compiled with build ID=%s\n"
00231                             "These options need to match\n",
00232                             module_entry->name, module_entry->build_id, ZEND_MODULE_BUILD_ID);
00233               DL_UNLOAD(handle);
00234               return FAILURE;
00235        }
00236        module_entry->type = type;
00237        module_entry->module_number = zend_next_free_module();
00238        module_entry->handle = handle;
00239 
00240        if ((module_entry = zend_register_module_ex(module_entry TSRMLS_CC)) == NULL) {
00241               DL_UNLOAD(handle);
00242               return FAILURE;
00243        }
00244 
00245        if ((type == MODULE_TEMPORARY || start_now) && zend_startup_module_ex(module_entry TSRMLS_CC) == FAILURE) {
00246               DL_UNLOAD(handle);
00247               return FAILURE;
00248        }
00249 
00250        if ((type == MODULE_TEMPORARY || start_now) && module_entry->request_startup_func) {
00251               if (module_entry->request_startup_func(type, module_entry->module_number TSRMLS_CC) == FAILURE) {
00252                      php_error_docref(NULL TSRMLS_CC, error_type, "Unable to initialize module '%s'", module_entry->name);
00253                      DL_UNLOAD(handle);
00254                      return FAILURE;
00255               }
00256        }
00257        return SUCCESS;
00258 }
00259 /* }}} */
00260 
00261 /* {{{ php_dl
00262  */
00263 PHPAPI void php_dl(char *file, int type, zval *return_value, int start_now TSRMLS_DC)
00264 {
00265        /* Load extension */
00266        if (php_load_extension(file, type, start_now TSRMLS_CC) == FAILURE) {
00267               RETVAL_FALSE;
00268        } else {
00269               RETVAL_TRUE;
00270        }
00271 }
00272 /* }}} */
00273 
00274 PHP_MINFO_FUNCTION(dl)
00275 {
00276        php_info_print_table_row(2, "Dynamic Library Support", "enabled");
00277 }
00278 
00279 #else
00280 
00281 PHPAPI void php_dl(char *file, int type, zval *return_value, int start_now TSRMLS_DC)
00282 {
00283        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot dynamically load %s - dynamic modules are not supported", file);
00284        RETURN_FALSE;
00285 }
00286 
00287 PHP_MINFO_FUNCTION(dl)
00288 {
00289        PUTS("Dynamic Library support not available<br />.\n");
00290 }
00291 
00292 #endif
00293 
00294 /*
00295  * Local variables:
00296  * tab-width: 4
00297  * c-basic-offset: 4
00298  * End:
00299  * vim600: sw=4 ts=4 fdm=marker
00300  * vim<600: sw=4 ts=4
00301  */