PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - dl.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 9 80 11.2 %
Date: 2015-03-01 Functions: 2 4 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2015 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$ */
      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)
      32             : #include <stdlib.h>
      33             : #include <stdio.h>
      34             : #ifdef HAVE_STRING_H
      35             : #include <string.h>
      36             : #else
      37             : #include <strings.h>
      38             : #endif
      39             : #ifdef PHP_WIN32
      40             : #include "win32/param.h"
      41             : #include "win32/winutil.h"
      42             : #define GET_DL_ERROR()  php_win_err()
      43             : #elif defined(NETWARE)
      44             : #include <sys/param.h>
      45             : #define GET_DL_ERROR()  dlerror()
      46             : #else
      47             : #include <sys/param.h>
      48             : #define GET_DL_ERROR()  DL_ERROR()
      49             : #endif
      50             : #endif /* defined(HAVE_LIBDL) */
      51             : 
      52             : /* {{{ proto int dl(string extension_filename)
      53             :    Load a PHP extension at runtime */
      54           1 : PHPAPI PHP_FUNCTION(dl)
      55             : {
      56             :         char *filename;
      57             :         size_t filename_len;
      58             : 
      59           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &filename, &filename_len) == FAILURE) {
      60           0 :                 return;
      61             :         }
      62             : 
      63           1 :         if (!PG(enable_dl)) {
      64           0 :                 php_error_docref(NULL, E_WARNING, "Dynamically loaded extensions aren't enabled");
      65           0 :                 RETURN_FALSE;
      66             :         }
      67             : 
      68           1 :         if (filename_len >= MAXPATHLEN) {
      69           1 :                 php_error_docref(NULL, E_WARNING, "File name exceeds the maximum allowed length of %d characters", MAXPATHLEN);
      70           1 :                 RETURN_FALSE;
      71             :         }
      72             : 
      73           0 :         php_dl(filename, MODULE_TEMPORARY, return_value, 0);
      74           0 :         if (Z_LVAL_P(return_value) == 1) {
      75           0 :                 EG(full_tables_cleanup) = 1;
      76             :         }
      77             : }
      78             : /* }}} */
      79             : 
      80             : #if defined(HAVE_LIBDL)
      81             : 
      82             : #ifdef ZTS
      83             : #define USING_ZTS 1
      84             : #else
      85             : #define USING_ZTS 0
      86             : #endif
      87             : 
      88             : /* {{{ php_load_extension
      89             :  */
      90           0 : PHPAPI int php_load_extension(char *filename, int type, int start_now)
      91             : {
      92             :         void *handle;
      93             :         char *libpath;
      94             :         zend_module_entry *module_entry;
      95             :         zend_module_entry *(*get_module)(void);
      96             :         int error_type;
      97             :         char *extension_dir;
      98             : 
      99           0 :         if (type == MODULE_PERSISTENT) {
     100           0 :                 extension_dir = INI_STR("extension_dir");
     101             :         } else {
     102           0 :                 extension_dir = PG(extension_dir);
     103             :         }
     104             : 
     105           0 :         if (type == MODULE_TEMPORARY) {
     106           0 :                 error_type = E_WARNING;
     107             :         } else {
     108           0 :                 error_type = E_CORE_WARNING;
     109             :         }
     110             : 
     111             :         /* Check if passed filename contains directory separators */
     112           0 :         if (strchr(filename, '/') != NULL || strchr(filename, DEFAULT_SLASH) != NULL) {
     113             :                 /* Passing modules with full path is not supported for dynamically loaded extensions */
     114           0 :                 if (type == MODULE_TEMPORARY) {
     115           0 :                         php_error_docref(NULL, E_WARNING, "Temporary module name should contain only filename");
     116           0 :                         return FAILURE;
     117             :                 }
     118           0 :                 libpath = estrdup(filename);
     119           0 :         } else if (extension_dir && extension_dir[0]) {
     120           0 :                 int extension_dir_len = (int)strlen(extension_dir);
     121             : 
     122           0 :                 if (IS_SLASH(extension_dir[extension_dir_len-1])) {
     123           0 :                         spprintf(&libpath, 0, "%s%s", extension_dir, filename); /* SAFE */
     124             :                 } else {
     125           0 :                         spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, filename); /* SAFE */
     126             :                 }
     127             :         } else {
     128           0 :                 return FAILURE; /* Not full path given or extension_dir is not set */
     129             :         }
     130             : 
     131             :         /* load dynamic symbol */
     132           0 :         handle = DL_LOAD(libpath);
     133           0 :         if (!handle) {
     134             : #if PHP_WIN32
     135             :                 char *err = GET_DL_ERROR();
     136             :                 if (err && (*err != '\0')) {
     137             :                         php_error_docref(NULL, error_type, "Unable to load dynamic library '%s' - %s", libpath, err);
     138             :                         LocalFree(err);
     139             :                 } else {
     140             :                         php_error_docref(NULL, error_type, "Unable to load dynamic library '%s' - %s", libpath, "Unknown reason");
     141             :                 }
     142             : #else
     143           0 :                 php_error_docref(NULL, error_type, "Unable to load dynamic library '%s' - %s", libpath, GET_DL_ERROR());
     144           0 :                 GET_DL_ERROR(); /* free the buffer storing the error */
     145             : #endif
     146           0 :                 efree(libpath);
     147           0 :                 return FAILURE;
     148             :         }
     149           0 :         efree(libpath);
     150             : 
     151           0 :         get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "get_module");
     152             : 
     153             :         /* Some OS prepend _ to symbol names while their dynamic linker
     154             :          * does not do that automatically. Thus we check manually for
     155             :          * _get_module. */
     156             : 
     157           0 :         if (!get_module) {
     158           0 :                 get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "_get_module");
     159             :         }
     160             : 
     161           0 :         if (!get_module) {
     162           0 :                 if (DL_FETCH_SYMBOL(handle, "zend_extension_entry") || DL_FETCH_SYMBOL(handle, "_zend_extension_entry")) {
     163           0 :                         DL_UNLOAD(handle);
     164           0 :                         php_error_docref(NULL, error_type, "Invalid library (appears to be a Zend Extension, try loading using zend_extension=%s from php.ini)", filename);
     165           0 :                         return FAILURE;
     166             :                 }
     167           0 :                 DL_UNLOAD(handle);
     168           0 :                 php_error_docref(NULL, error_type, "Invalid library (maybe not a PHP library) '%s'", filename);
     169           0 :                 return FAILURE;
     170             :         }
     171           0 :         module_entry = get_module();
     172           0 :         if (module_entry->zend_api != ZEND_MODULE_API_NO) {
     173           0 :                         php_error_docref(NULL, error_type,
     174             :                                         "%s: Unable to initialize module\n"
     175             :                                         "Module compiled with module API=%d\n"
     176             :                                         "PHP    compiled with module API=%d\n"
     177             :                                         "These options need to match\n",
     178             :                                         module_entry->name, module_entry->zend_api, ZEND_MODULE_API_NO);
     179           0 :                         DL_UNLOAD(handle);
     180           0 :                         return FAILURE;
     181             :         }
     182           0 :         if(strcmp(module_entry->build_id, ZEND_MODULE_BUILD_ID)) {
     183           0 :                 php_error_docref(NULL, error_type,
     184             :                                 "%s: Unable to initialize module\n"
     185             :                                 "Module compiled with build ID=%s\n"
     186             :                                 "PHP    compiled with build ID=%s\n"
     187             :                                 "These options need to match\n",
     188             :                                 module_entry->name, module_entry->build_id, ZEND_MODULE_BUILD_ID);
     189           0 :                 DL_UNLOAD(handle);
     190           0 :                 return FAILURE;
     191             :         }
     192           0 :         module_entry->type = type;
     193           0 :         module_entry->module_number = zend_next_free_module();
     194           0 :         module_entry->handle = handle;
     195             : 
     196           0 :         if ((module_entry = zend_register_module_ex(module_entry)) == NULL) {
     197           0 :                 DL_UNLOAD(handle);
     198           0 :                 return FAILURE;
     199             :         }
     200             : 
     201           0 :         if ((type == MODULE_TEMPORARY || start_now) && zend_startup_module_ex(module_entry) == FAILURE) {
     202           0 :                 DL_UNLOAD(handle);
     203           0 :                 return FAILURE;
     204             :         }
     205             : 
     206           0 :         if ((type == MODULE_TEMPORARY || start_now) && module_entry->request_startup_func) {
     207           0 :                 if (module_entry->request_startup_func(type, module_entry->module_number) == FAILURE) {
     208           0 :                         php_error_docref(NULL, error_type, "Unable to initialize module '%s'", module_entry->name);
     209           0 :                         DL_UNLOAD(handle);
     210           0 :                         return FAILURE;
     211             :                 }
     212             :         }
     213           0 :         return SUCCESS;
     214             : }
     215             : /* }}} */
     216             : 
     217             : /* {{{ php_dl
     218             :  */
     219           0 : PHPAPI void php_dl(char *file, int type, zval *return_value, int start_now)
     220             : {
     221             :         /* Load extension */
     222           0 :         if (php_load_extension(file, type, start_now) == FAILURE) {
     223           0 :                 RETVAL_FALSE;
     224             :         } else {
     225           0 :                 RETVAL_TRUE;
     226             :         }
     227           0 : }
     228             : /* }}} */
     229             : 
     230         146 : PHP_MINFO_FUNCTION(dl)
     231             : {
     232         146 :         php_info_print_table_row(2, "Dynamic Library Support", "enabled");
     233         146 : }
     234             : 
     235             : #else
     236             : 
     237             : PHPAPI void php_dl(char *file, int type, zval *return_value, int start_now)
     238             : {
     239             :         php_error_docref(NULL, E_WARNING, "Cannot dynamically load %s - dynamic modules are not supported", file);
     240             :         RETURN_FALSE;
     241             : }
     242             : 
     243             : PHP_MINFO_FUNCTION(dl)
     244             : {
     245             :         PUTS("Dynamic Library support not available<br />.\n");
     246             : }
     247             : 
     248             : #endif
     249             : 
     250             : /*
     251             :  * Local variables:
     252             :  * tab-width: 4
     253             :  * c-basic-offset: 4
     254             :  * End:
     255             :  * vim600: sw=4 ts=4 fdm=marker
     256             :  * vim<600: sw=4 ts=4
     257             :  */

Generated by: LCOV version 1.10

Generated at Sun, 01 Mar 2015 23:22:36 +0000 (5 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.