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 - Zend - zend_extensions.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 63 126 50.0 %
Date: 2016-05-26 Functions: 13 16 81.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2016 Zend Technologies Ltd. (http://www.zend.com) |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 2.00 of the Zend 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.zend.com/license/2_00.txt.                                |
      11             :    | If you did not receive a copy of the Zend license and are unable to  |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@zend.com so we can mail you a copy immediately.              |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Andi Gutmans <andi@zend.com>                                |
      16             :    |          Zeev Suraski <zeev@zend.com>                                |
      17             :    +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #include "zend_extensions.h"
      23             : 
      24             : ZEND_API zend_llist zend_extensions;
      25             : ZEND_API uint32_t zend_extension_flags = 0;
      26             : static int last_resource_number;
      27             : 
      28       22414 : int zend_load_extension(const char *path)
      29             : {
      30             : #if ZEND_EXTENSIONS_SUPPORT
      31             :         DL_HANDLE handle;
      32             :         zend_extension *new_extension;
      33             :         zend_extension_version_info *extension_version_info;
      34             : 
      35       22414 :         handle = DL_LOAD(path);
      36       22414 :         if (!handle) {
      37             : #ifndef ZEND_WIN32
      38           0 :                 fprintf(stderr, "Failed loading %s:  %s\n", path, DL_ERROR());
      39             : #else
      40             :                 fprintf(stderr, "Failed loading %s\n", path);
      41             :                 /* See http://support.microsoft.com/kb/190351 */
      42             :                 fflush(stderr);
      43             : #endif
      44           0 :                 return FAILURE;
      45             :         }
      46             : 
      47       22414 :         extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "extension_version_info");
      48       22414 :         if (!extension_version_info) {
      49           0 :                 extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "_extension_version_info");
      50             :         }
      51       22414 :         new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "zend_extension_entry");
      52       22414 :         if (!new_extension) {
      53           0 :                 new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "_zend_extension_entry");
      54             :         }
      55       22414 :         if (!extension_version_info || !new_extension) {
      56           0 :                 fprintf(stderr, "%s doesn't appear to be a valid Zend extension\n", path);
      57             : /* See http://support.microsoft.com/kb/190351 */
      58             : #ifdef ZEND_WIN32
      59             :                 fflush(stderr);
      60             : #endif
      61           0 :                 DL_UNLOAD(handle);
      62           0 :                 return FAILURE;
      63             :         }
      64             : 
      65             : 
      66             :         /* allow extension to proclaim compatibility with any Zend version */
      67       22414 :         if (extension_version_info->zend_extension_api_no != ZEND_EXTENSION_API_NO &&(!new_extension->api_no_check || new_extension->api_no_check(ZEND_EXTENSION_API_NO) != SUCCESS)) {
      68           0 :                 if (extension_version_info->zend_extension_api_no > ZEND_EXTENSION_API_NO) {
      69           0 :                         fprintf(stderr, "%s requires Zend Engine API version %d.\n"
      70             :                                         "The Zend Engine API version %d which is installed, is outdated.\n\n",
      71             :                                         new_extension->name,
      72             :                                         extension_version_info->zend_extension_api_no,
      73             :                                         ZEND_EXTENSION_API_NO);
      74             : /* See http://support.microsoft.com/kb/190351 */
      75             : #ifdef ZEND_WIN32
      76             :                         fflush(stderr);
      77             : #endif
      78           0 :                         DL_UNLOAD(handle);
      79           0 :                         return FAILURE;
      80           0 :                 } else if (extension_version_info->zend_extension_api_no < ZEND_EXTENSION_API_NO) {
      81           0 :                         fprintf(stderr, "%s requires Zend Engine API version %d.\n"
      82             :                                         "The Zend Engine API version %d which is installed, is newer.\n"
      83             :                                         "Contact %s at %s for a later version of %s.\n\n",
      84             :                                         new_extension->name,
      85             :                                         extension_version_info->zend_extension_api_no,
      86             :                                         ZEND_EXTENSION_API_NO,
      87             :                                         new_extension->author,
      88             :                                         new_extension->URL,
      89             :                                         new_extension->name);
      90             : /* See http://support.microsoft.com/kb/190351 */
      91             : #ifdef ZEND_WIN32
      92             :                         fflush(stderr);
      93             : #endif
      94           0 :                         DL_UNLOAD(handle);
      95           0 :                         return FAILURE;
      96             :                 }
      97       22414 :         } else if (strcmp(ZEND_EXTENSION_BUILD_ID, extension_version_info->build_id) &&
      98           0 :                    (!new_extension->build_id_check || new_extension->build_id_check(ZEND_EXTENSION_BUILD_ID) != SUCCESS)) {
      99           0 :                 fprintf(stderr, "Cannot load %s - it was built with configuration %s, whereas running engine is %s\n",
     100             :                                         new_extension->name, extension_version_info->build_id, ZEND_EXTENSION_BUILD_ID);
     101             : /* See http://support.microsoft.com/kb/190351 */
     102             : #ifdef ZEND_WIN32
     103             :                 fflush(stderr);
     104             : #endif
     105           0 :                 DL_UNLOAD(handle);
     106           0 :                 return FAILURE;
     107       22414 :         } else if (zend_get_extension(new_extension->name)) {
     108           0 :                 fprintf(stderr, "Cannot load %s - it was already loaded\n", new_extension->name);
     109             : /* See http://support.microsoft.com/kb/190351 */
     110             : #ifdef ZEND_WIN32
     111             :                 fflush(stderr);
     112             : #endif
     113           0 :                 DL_UNLOAD(handle);
     114           0 :                 return FAILURE;
     115       22414 :         } else if (zend_get_extension(new_extension->name)) {
     116           0 :                 fprintf(stderr, "Cannot load %s - extension already loaded\n", new_extension->name);
     117             : /* See http://support.microsoft.com/kb/190351 */
     118             : #ifdef PHP_WIN32
     119             :                 fflush(stderr);
     120             : #endif
     121           0 :                 DL_UNLOAD(handle);
     122           0 :                 return FAILURE;
     123             :         }
     124             : 
     125       22414 :         return zend_register_extension(new_extension, handle);
     126             : #else
     127             :         fprintf(stderr, "Extensions are not supported on this platform.\n");
     128             : /* See http://support.microsoft.com/kb/190351 */
     129             : #ifdef ZEND_WIN32
     130             :         fflush(stderr);
     131             : #endif
     132             :         return FAILURE;
     133             : #endif
     134             : }
     135             : 
     136             : 
     137       22414 : int zend_register_extension(zend_extension *new_extension, DL_HANDLE handle)
     138             : {
     139             : #if ZEND_EXTENSIONS_SUPPORT
     140             :         zend_extension extension;
     141             : 
     142       22414 :         extension = *new_extension;
     143       22414 :         extension.handle = handle;
     144             : 
     145       22414 :         zend_extension_dispatch_message(ZEND_EXTMSG_NEW_EXTENSION, &extension);
     146             : 
     147       22414 :         zend_llist_add_element(&zend_extensions, &extension);
     148             : 
     149       22414 :         if (extension.op_array_ctor) {
     150           0 :                 zend_extension_flags |= ZEND_EXTENSIONS_HAVE_OP_ARRAY_CTOR;
     151             :         }
     152       22414 :         if (extension.op_array_dtor) {
     153           0 :                 zend_extension_flags |= ZEND_EXTENSIONS_HAVE_OP_ARRAY_DTOR;
     154             :         }
     155       22414 :         if (extension.op_array_handler) {
     156           0 :                 zend_extension_flags |= ZEND_EXTENSIONS_HAVE_OP_ARRAY_HANDLER;
     157             :         }
     158       22414 :         if (extension.op_array_persist_calc) {
     159           0 :                 zend_extension_flags |= ZEND_EXTENSIONS_HAVE_OP_ARRAY_PERSIST_CALC;
     160             :         }
     161       22414 :         if (extension.op_array_persist) {
     162           0 :                 zend_extension_flags |= ZEND_EXTENSIONS_HAVE_OP_ARRAY_PERSIST;
     163             :         }
     164             :         /*fprintf(stderr, "Loaded %s, version %s\n", extension.name, extension.version);*/
     165             : #endif
     166             : 
     167       22414 :         return SUCCESS;
     168             : }
     169             : 
     170             : 
     171       22453 : static void zend_extension_shutdown(zend_extension *extension)
     172             : {
     173             : #if ZEND_EXTENSIONS_SUPPORT
     174       22453 :         if (extension->shutdown) {
     175           0 :                 extension->shutdown(extension);
     176             :         }
     177             : #endif
     178       22453 : }
     179             : 
     180       22414 : static int zend_extension_startup(zend_extension *extension)
     181             : {
     182             : #if ZEND_EXTENSIONS_SUPPORT
     183       22414 :         if (extension->startup) {
     184       22414 :                 if (extension->startup(extension)!=SUCCESS) {
     185           0 :                         return 1;
     186             :                 }
     187       22414 :                 zend_append_version_info(extension);
     188             :         }
     189             : #endif
     190       22414 :         return 0;
     191             : }
     192             : 
     193             : 
     194       22587 : int zend_startup_extensions_mechanism()
     195             : {
     196             :         /* Startup extensions mechanism */
     197       22587 :         zend_llist_init(&zend_extensions, sizeof(zend_extension), (void (*)(void *)) zend_extension_dtor, 1);
     198       22587 :         last_resource_number = 0;
     199       22587 :         return SUCCESS;
     200             : }
     201             : 
     202             : 
     203       22587 : int zend_startup_extensions()
     204             : {
     205       22587 :         zend_llist_apply_with_del(&zend_extensions, (int (*)(void *)) zend_extension_startup);
     206       22587 :         return SUCCESS;
     207             : }
     208             : 
     209             : 
     210       22623 : void zend_shutdown_extensions(void)
     211             : {
     212       22623 :         zend_llist_apply(&zend_extensions, (llist_apply_func_t) zend_extension_shutdown);
     213       22623 :         zend_llist_destroy(&zend_extensions);
     214       22623 : }
     215             : 
     216             : 
     217       22453 : void zend_extension_dtor(zend_extension *extension)
     218             : {
     219             : #if ZEND_EXTENSIONS_SUPPORT && !ZEND_DEBUG
     220       22453 :         if (extension->handle && !getenv("ZEND_DONT_UNLOAD_MODULES")) {
     221           2 :                 DL_UNLOAD(extension->handle);
     222             :         }
     223             : #endif
     224       22453 : }
     225             : 
     226             : 
     227           0 : static void zend_extension_message_dispatcher(const zend_extension *extension, int num_args, va_list args)
     228             : {
     229             :         int message;
     230             :         void *arg;
     231             : 
     232           0 :         if (!extension->message_handler || num_args!=2) {
     233           0 :                 return;
     234             :         }
     235           0 :         message = va_arg(args, int);
     236           0 :         arg = va_arg(args, void *);
     237           0 :         extension->message_handler(message, arg);
     238             : }
     239             : 
     240             : 
     241       22414 : ZEND_API void zend_extension_dispatch_message(int message, void *arg)
     242             : {
     243       22414 :         zend_llist_apply_with_arguments(&zend_extensions, (llist_apply_with_args_func_t) zend_extension_message_dispatcher, 2, message, arg);
     244       22414 : }
     245             : 
     246             : 
     247         378 : ZEND_API int zend_get_resource_handle(zend_extension *extension)
     248             : {
     249         378 :         if (last_resource_number<ZEND_MAX_RESERVED_RESOURCES) {
     250         378 :                 extension->resource_number = last_resource_number;
     251         378 :                 return last_resource_number++;
     252             :         } else {
     253           0 :                 return -1;
     254             :         }
     255             : }
     256             : 
     257             : 
     258       67415 : ZEND_API zend_extension *zend_get_extension(const char *extension_name)
     259             : {
     260             :         zend_llist_element *element;
     261             : 
     262       67415 :         for (element = zend_extensions.head; element; element = element->next) {
     263           0 :                 zend_extension *extension = (zend_extension *) element->data;
     264             : 
     265           0 :                 if (!strcmp(extension->name, extension_name)) {
     266           0 :                         return extension;
     267             :                 }
     268             :         }
     269       67415 :         return NULL;
     270             : }
     271             : 
     272             : typedef struct _zend_extension_persist_data {
     273             :         zend_op_array *op_array;
     274             :         size_t         size;
     275             :         char          *mem;
     276             : } zend_extension_persist_data;
     277             : 
     278           0 : static void zend_extension_op_array_persist_calc_handler(zend_extension *extension, zend_extension_persist_data *data)
     279             : {
     280           0 :         if (extension->op_array_persist_calc) {
     281           0 :                 data->size += extension->op_array_persist_calc(data->op_array);
     282             :         }
     283           0 : }
     284             : 
     285           0 : static void zend_extension_op_array_persist_handler(zend_extension *extension, zend_extension_persist_data *data)
     286             : {
     287           0 :         if (extension->op_array_persist) {
     288           0 :                 size_t size = extension->op_array_persist(data->op_array, data->mem);
     289           0 :                 if (size) {
     290           0 :                         data->mem = (void*)((char*)data->mem + size);
     291           0 :                         data->size += size;
     292             :                 }
     293             :         }
     294           0 : }
     295             : 
     296         463 : ZEND_API size_t zend_extensions_op_array_persist_calc(zend_op_array *op_array)
     297             : {
     298         463 :         if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_PERSIST_CALC) {
     299             :                 zend_extension_persist_data data;
     300             : 
     301           0 :                 data.op_array = op_array;
     302           0 :                 data.size = 0;
     303           0 :                 data.mem  = NULL;
     304           0 :                 zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_persist_calc_handler, &data);
     305           0 :                 return data.size;
     306             :         }
     307         463 :         return 0;
     308             : }
     309             : 
     310         463 : ZEND_API size_t zend_extensions_op_array_persist(zend_op_array *op_array, void *mem)
     311             : {
     312         463 :         if (zend_extension_flags & ZEND_EXTENSIONS_HAVE_OP_ARRAY_PERSIST) {
     313             :                 zend_extension_persist_data data;
     314             : 
     315           0 :                 data.op_array = op_array;
     316           0 :                 data.size = 0;
     317           0 :                 data.mem  = mem;
     318           0 :                 zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_persist_handler, &data);
     319           0 :                 return data.size;
     320             :         }
     321         463 :         return 0;
     322             : }
     323             : 
     324             : /*
     325             :  * Local variables:
     326             :  * tab-width: 4
     327             :  * c-basic-offset: 4
     328             :  * indent-tabs-mode: t
     329             :  * End:
     330             :  */

Generated by: LCOV version 1.10

Generated at Thu, 26 May 2016 12:47:53 +0000 (37 hours ago)

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