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: 25 80 31.2 %
Date: 2016-09-18 Functions: 3 4 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2016 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_TYPE_P(return_value) == IS_TRUE) {
      75           0 :                 EG(full_tables_cleanup) = 1;
      76             :         }
      77             : }
      78             : /* }}} */
      79             : 
      80             : #if defined(HAVE_LIBDL)
      81             : 
      82             : /* {{{ php_load_extension
      83             :  */
      84           1 : PHPAPI int php_load_extension(char *filename, int type, int start_now)
      85             : {
      86             :         void *handle;
      87             :         char *libpath;
      88             :         zend_module_entry *module_entry;
      89             :         zend_module_entry *(*get_module)(void);
      90             :         int error_type;
      91             :         char *extension_dir;
      92             : 
      93           1 :         if (type == MODULE_PERSISTENT) {
      94           1 :                 extension_dir = INI_STR("extension_dir");
      95             :         } else {
      96           0 :                 extension_dir = PG(extension_dir);
      97             :         }
      98             : 
      99           1 :         if (type == MODULE_TEMPORARY) {
     100           0 :                 error_type = E_WARNING;
     101             :         } else {
     102           1 :                 error_type = E_CORE_WARNING;
     103             :         }
     104             : 
     105             :         /* Check if passed filename contains directory separators */
     106           1 :         if (strchr(filename, '/') != NULL || strchr(filename, DEFAULT_SLASH) != NULL) {
     107             :                 /* Passing modules with full path is not supported for dynamically loaded extensions */
     108           0 :                 if (type == MODULE_TEMPORARY) {
     109           0 :                         php_error_docref(NULL, E_WARNING, "Temporary module name should contain only filename");
     110           0 :                         return FAILURE;
     111             :                 }
     112           0 :                 libpath = estrdup(filename);
     113           2 :         } else if (extension_dir && extension_dir[0]) {
     114           1 :                 int extension_dir_len = (int)strlen(extension_dir);
     115             : 
     116           1 :                 if (IS_SLASH(extension_dir[extension_dir_len-1])) {
     117           0 :                         spprintf(&libpath, 0, "%s%s", extension_dir, filename); /* SAFE */
     118             :                 } else {
     119           1 :                         spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, filename); /* SAFE */
     120             :                 }
     121             :         } else {
     122           0 :                 return FAILURE; /* Not full path given or extension_dir is not set */
     123             :         }
     124             : 
     125             :         /* load dynamic symbol */
     126           1 :         handle = DL_LOAD(libpath);
     127           1 :         if (!handle) {
     128             : #if PHP_WIN32
     129             :                 char *err = GET_DL_ERROR();
     130             :                 if (err && (*err != '\0')) {
     131             :                         php_error_docref(NULL, error_type, "Unable to load dynamic library '%s' - %s", libpath, err);
     132             :                         LocalFree(err);
     133             :                 } else {
     134             :                         php_error_docref(NULL, error_type, "Unable to load dynamic library '%s' - %s", libpath, "Unknown reason");
     135             :                 }
     136             : #else
     137           1 :                 php_error_docref(NULL, error_type, "Unable to load dynamic library '%s' - %s", libpath, GET_DL_ERROR());
     138           1 :                 GET_DL_ERROR(); /* free the buffer storing the error */
     139             : #endif
     140           1 :                 efree(libpath);
     141           1 :                 return FAILURE;
     142             :         }
     143           0 :         efree(libpath);
     144             : 
     145           0 :         get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "get_module");
     146             : 
     147             :         /* Some OS prepend _ to symbol names while their dynamic linker
     148             :          * does not do that automatically. Thus we check manually for
     149             :          * _get_module. */
     150             : 
     151           0 :         if (!get_module) {
     152           0 :                 get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "_get_module");
     153             :         }
     154             : 
     155           0 :         if (!get_module) {
     156           0 :                 if (DL_FETCH_SYMBOL(handle, "zend_extension_entry") || DL_FETCH_SYMBOL(handle, "_zend_extension_entry")) {
     157           0 :                         DL_UNLOAD(handle);
     158           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);
     159           0 :                         return FAILURE;
     160             :                 }
     161           0 :                 DL_UNLOAD(handle);
     162           0 :                 php_error_docref(NULL, error_type, "Invalid library (maybe not a PHP library) '%s'", filename);
     163           0 :                 return FAILURE;
     164             :         }
     165           0 :         module_entry = get_module();
     166           0 :         if (module_entry->zend_api != ZEND_MODULE_API_NO) {
     167           0 :                         php_error_docref(NULL, error_type,
     168             :                                         "%s: Unable to initialize module\n"
     169             :                                         "Module compiled with module API=%d\n"
     170             :                                         "PHP    compiled with module API=%d\n"
     171             :                                         "These options need to match\n",
     172             :                                         module_entry->name, module_entry->zend_api, ZEND_MODULE_API_NO);
     173           0 :                         DL_UNLOAD(handle);
     174           0 :                         return FAILURE;
     175             :         }
     176           0 :         if(strcmp(module_entry->build_id, ZEND_MODULE_BUILD_ID)) {
     177           0 :                 php_error_docref(NULL, error_type,
     178             :                                 "%s: Unable to initialize module\n"
     179             :                                 "Module compiled with build ID=%s\n"
     180             :                                 "PHP    compiled with build ID=%s\n"
     181             :                                 "These options need to match\n",
     182             :                                 module_entry->name, module_entry->build_id, ZEND_MODULE_BUILD_ID);
     183           0 :                 DL_UNLOAD(handle);
     184           0 :                 return FAILURE;
     185             :         }
     186           0 :         module_entry->type = type;
     187           0 :         module_entry->module_number = zend_next_free_module();
     188           0 :         module_entry->handle = handle;
     189             : 
     190           0 :         if ((module_entry = zend_register_module_ex(module_entry)) == NULL) {
     191           0 :                 DL_UNLOAD(handle);
     192           0 :                 return FAILURE;
     193             :         }
     194             : 
     195           0 :         if ((type == MODULE_TEMPORARY || start_now) && zend_startup_module_ex(module_entry) == FAILURE) {
     196           0 :                 DL_UNLOAD(handle);
     197           0 :                 return FAILURE;
     198             :         }
     199             : 
     200           0 :         if ((type == MODULE_TEMPORARY || start_now) && module_entry->request_startup_func) {
     201           0 :                 if (module_entry->request_startup_func(type, module_entry->module_number) == FAILURE) {
     202           0 :                         php_error_docref(NULL, error_type, "Unable to initialize module '%s'", module_entry->name);
     203           0 :                         DL_UNLOAD(handle);
     204           0 :                         return FAILURE;
     205             :                 }
     206             :         }
     207           0 :         return SUCCESS;
     208             : }
     209             : /* }}} */
     210             : 
     211             : /* {{{ php_dl
     212             :  */
     213           0 : PHPAPI void php_dl(char *file, int type, zval *return_value, int start_now)
     214             : {
     215             :         /* Load extension */
     216           0 :         if (php_load_extension(file, type, start_now) == FAILURE) {
     217           0 :                 RETVAL_FALSE;
     218             :         } else {
     219           0 :                 RETVAL_TRUE;
     220             :         }
     221           0 : }
     222             : /* }}} */
     223             : 
     224         151 : PHP_MINFO_FUNCTION(dl)
     225             : {
     226         151 :         php_info_print_table_row(2, "Dynamic Library Support", "enabled");
     227         151 : }
     228             : 
     229             : #else
     230             : 
     231             : PHPAPI void php_dl(char *file, int type, zval *return_value, int start_now)
     232             : {
     233             :         php_error_docref(NULL, E_WARNING, "Cannot dynamically load %s - dynamic modules are not supported", file);
     234             :         RETVAL_FALSE;
     235             : }
     236             : 
     237             : PHP_MINFO_FUNCTION(dl)
     238             : {
     239             :         PUTS("Dynamic Library support not available<br />.\n");
     240             : }
     241             : 
     242             : #endif
     243             : 
     244             : /*
     245             :  * Local variables:
     246             :  * tab-width: 4
     247             :  * c-basic-offset: 4
     248             :  * End:
     249             :  * vim600: sw=4 ts=4 fdm=marker
     250             :  * vim<600: sw=4 ts=4
     251             :  */

Generated by: LCOV version 1.10

Generated at Sun, 18 Sep 2016 08:20:18 +0000 (9 days ago)

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