1999-04-07 18:10:10 +00:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Zend Engine |
|
|
|
|
+----------------------------------------------------------------------+
|
2006-01-04 23:53:05 +00:00
|
|
|
| Copyright (c) 1998-2006 Zend Technologies Ltd. (http://www.zend.com) |
|
1999-04-07 18:10:10 +00:00
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 15:16:21 +00:00
|
|
|
| This source file is subject to version 2.00 of the Zend license, |
|
1999-07-16 14:58:16 +00:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-10 20:04:29 +00:00
|
|
|
| available through the world-wide-web at the following url: |
|
2001-12-11 15:16:21 +00:00
|
|
|
| http://www.zend.com/license/2_00.txt. |
|
1999-07-16 14:58:16 +00:00
|
|
|
| If you did not receive a copy of the Zend license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@zend.com so we can mail you a copy immediately. |
|
1999-04-07 18:10:10 +00:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Andi Gutmans <andi@zend.com> |
|
|
|
|
| Zeev Suraski <zeev@zend.com> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2003-02-01 01:49:15 +00:00
|
|
|
/* $Id$ */
|
1999-07-16 14:58:16 +00:00
|
|
|
|
1999-04-07 18:10:10 +00:00
|
|
|
#include "zend_extensions.h"
|
|
|
|
|
|
|
|
ZEND_API zend_llist zend_extensions;
|
2000-04-28 15:52:02 +00:00
|
|
|
static int last_resource_number;
|
1999-04-07 18:10:10 +00:00
|
|
|
|
|
|
|
int zend_load_extension(char *path)
|
|
|
|
{
|
|
|
|
#if ZEND_EXTENSIONS_SUPPORT
|
|
|
|
DL_HANDLE handle;
|
2000-03-29 19:26:34 +00:00
|
|
|
zend_extension *new_extension;
|
1999-04-07 18:10:10 +00:00
|
|
|
zend_extension_version_info *extension_version_info;
|
|
|
|
|
|
|
|
handle = DL_LOAD(path);
|
|
|
|
if (!handle) {
|
2000-02-11 15:59:30 +00:00
|
|
|
#ifndef ZEND_WIN32
|
2002-08-23 22:07:59 +00:00
|
|
|
fprintf(stderr, "Failed loading %s: %s\n", path, DL_ERROR());
|
1999-06-08 19:37:40 +00:00
|
|
|
#else
|
|
|
|
fprintf(stderr, "Failed loading %s\n", path);
|
|
|
|
#endif
|
1999-04-07 18:10:10 +00:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "extension_version_info");
|
2003-04-18 17:55:45 +00:00
|
|
|
if (!extension_version_info) {
|
|
|
|
extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "_extension_version_info");
|
|
|
|
}
|
1999-04-07 18:10:10 +00:00
|
|
|
new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "zend_extension_entry");
|
2003-04-18 17:55:45 +00:00
|
|
|
if (!new_extension) {
|
|
|
|
new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "_zend_extension_entry");
|
|
|
|
}
|
1999-04-07 18:10:10 +00:00
|
|
|
if (!extension_version_info || !new_extension) {
|
1999-06-08 19:37:40 +00:00
|
|
|
fprintf(stderr, "%s doesn't appear to be a valid Zend extension\n", path);
|
2003-03-17 14:41:04 +00:00
|
|
|
DL_UNLOAD(handle);
|
1999-04-07 18:10:10 +00:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2000-10-29 16:25:16 +00:00
|
|
|
|
|
|
|
/* allow extension to proclaim compatibility with any Zend version */
|
2001-04-27 18:51:56 +00:00
|
|
|
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)) {
|
2000-10-29 16:25:16 +00:00
|
|
|
if (extension_version_info->zend_extension_api_no > ZEND_EXTENSION_API_NO) {
|
2001-08-15 18:06:06 +00:00
|
|
|
fprintf(stderr, "%s requires Zend Engine API version %d.\n"
|
|
|
|
"The Zend Engine API version %d which is installed, is outdated.\n\n",
|
1999-04-07 18:10:10 +00:00
|
|
|
new_extension->name,
|
2000-06-22 18:50:55 +00:00
|
|
|
extension_version_info->zend_extension_api_no,
|
1999-04-07 18:10:10 +00:00
|
|
|
ZEND_EXTENSION_API_NO);
|
2000-10-29 16:25:16 +00:00
|
|
|
DL_UNLOAD(handle);
|
|
|
|
return FAILURE;
|
|
|
|
} else if (extension_version_info->zend_extension_api_no < ZEND_EXTENSION_API_NO) {
|
2001-08-15 18:06:06 +00:00
|
|
|
fprintf(stderr, "%s requires Zend Engine API version %d.\n"
|
|
|
|
"The Zend Engine API version %d which is installed, is newer.\n"
|
|
|
|
"Contact %s at %s for a later version of %s.\n\n",
|
1999-04-07 18:10:10 +00:00
|
|
|
new_extension->name,
|
|
|
|
extension_version_info->zend_extension_api_no,
|
|
|
|
ZEND_EXTENSION_API_NO,
|
|
|
|
new_extension->author,
|
2001-08-15 18:06:06 +00:00
|
|
|
new_extension->URL,
|
|
|
|
new_extension->name);
|
2000-10-29 16:25:16 +00:00
|
|
|
DL_UNLOAD(handle);
|
|
|
|
return FAILURE;
|
2006-05-09 23:53:23 +00:00
|
|
|
}
|
1999-04-07 18:10:10 +00:00
|
|
|
} else if (ZTS_V!=extension_version_info->thread_safe) {
|
1999-06-08 19:37:40 +00:00
|
|
|
fprintf(stderr, "Cannot load %s - it %s thread safe, whereas Zend %s\n",
|
1999-04-07 18:10:10 +00:00
|
|
|
new_extension->name,
|
|
|
|
(extension_version_info->thread_safe?"is":"isn't"),
|
|
|
|
(ZTS_V?"is":"isn't"));
|
1999-04-19 21:39:18 +00:00
|
|
|
DL_UNLOAD(handle);
|
1999-04-07 18:10:10 +00:00
|
|
|
return FAILURE;
|
1999-08-28 21:51:12 +00:00
|
|
|
} else if (ZEND_DEBUG!=extension_version_info->debug) {
|
1999-06-08 19:37:40 +00:00
|
|
|
fprintf(stderr, "Cannot load %s - it %s debug information, whereas Zend %s\n",
|
1999-04-27 11:00:59 +00:00
|
|
|
new_extension->name,
|
|
|
|
(extension_version_info->debug?"contains":"does not contain"),
|
1999-08-28 21:51:12 +00:00
|
|
|
(ZEND_DEBUG?"does":"does not"));
|
1999-04-27 11:00:59 +00:00
|
|
|
DL_UNLOAD(handle);
|
|
|
|
return FAILURE;
|
1999-04-07 18:10:10 +00:00
|
|
|
}
|
|
|
|
|
2000-03-29 19:26:34 +00:00
|
|
|
return zend_register_extension(new_extension, handle);
|
|
|
|
#else
|
|
|
|
fprintf(stderr, "Extensions are not supported on this platform.\n");
|
|
|
|
return FAILURE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int zend_register_extension(zend_extension *new_extension, DL_HANDLE handle)
|
|
|
|
{
|
2000-04-15 15:32:32 +00:00
|
|
|
#if ZEND_EXTENSIONS_SUPPORT
|
2000-03-29 19:26:34 +00:00
|
|
|
zend_extension extension;
|
|
|
|
|
1999-04-07 18:10:10 +00:00
|
|
|
extension = *new_extension;
|
|
|
|
extension.handle = handle;
|
|
|
|
|
2000-04-29 10:20:40 +00:00
|
|
|
zend_extension_dispatch_message(ZEND_EXTMSG_NEW_EXTENSION, &extension);
|
|
|
|
|
1999-04-07 18:10:10 +00:00
|
|
|
zend_llist_add_element(&zend_extensions, &extension);
|
|
|
|
|
1999-06-08 19:37:40 +00:00
|
|
|
/*fprintf(stderr, "Loaded %s, version %s\n", extension.name, extension.version);*/
|
2000-04-15 15:32:32 +00:00
|
|
|
#endif
|
1999-04-07 18:10:10 +00:00
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2000-03-29 19:26:34 +00:00
|
|
|
|
2001-07-31 04:53:54 +00:00
|
|
|
static void zend_extension_shutdown(zend_extension *extension TSRMLS_DC)
|
1999-04-07 18:10:10 +00:00
|
|
|
{
|
|
|
|
#if ZEND_EXTENSIONS_SUPPORT
|
|
|
|
if (extension->shutdown) {
|
|
|
|
extension->shutdown(extension);
|
|
|
|
}
|
1999-04-18 15:11:52 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2000-10-18 20:15:53 +00:00
|
|
|
static int zend_extension_startup(zend_extension *extension)
|
2000-09-25 18:10:45 +00:00
|
|
|
{
|
2000-10-14 00:16:46 +00:00
|
|
|
#if ZEND_EXTENSIONS_SUPPORT
|
2000-09-25 18:10:45 +00:00
|
|
|
if (extension->startup) {
|
|
|
|
if (extension->startup(extension)!=SUCCESS) {
|
2000-10-18 20:15:53 +00:00
|
|
|
return 1;
|
2000-09-25 18:10:45 +00:00
|
|
|
}
|
2001-02-13 13:53:42 +00:00
|
|
|
zend_append_version_info(extension);
|
2000-09-25 18:10:45 +00:00
|
|
|
}
|
2000-10-14 00:16:46 +00:00
|
|
|
#endif
|
2000-10-18 20:15:53 +00:00
|
|
|
return 0;
|
2000-09-25 18:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int zend_startup_extensions_mechanism()
|
2000-04-28 15:52:02 +00:00
|
|
|
{
|
2000-09-25 18:10:45 +00:00
|
|
|
/* Startup extensions mechanism */
|
2000-04-28 15:52:02 +00:00
|
|
|
zend_llist_init(&zend_extensions, sizeof(zend_extension), (void (*)(void *)) zend_extension_dtor, 1);
|
|
|
|
last_resource_number = 0;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-25 18:10:45 +00:00
|
|
|
int zend_startup_extensions()
|
|
|
|
{
|
2000-10-18 20:15:53 +00:00
|
|
|
zend_llist_apply_with_del(&zend_extensions, (int (*)(void *)) zend_extension_startup);
|
2000-09-25 18:10:45 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-31 04:53:54 +00:00
|
|
|
void zend_shutdown_extensions(TSRMLS_D)
|
1999-04-18 15:11:52 +00:00
|
|
|
{
|
2001-07-31 04:53:54 +00:00
|
|
|
zend_llist_apply(&zend_extensions, (llist_apply_func_t) zend_extension_shutdown TSRMLS_CC);
|
1999-04-18 15:11:52 +00:00
|
|
|
zend_llist_destroy(&zend_extensions);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void zend_extension_dtor(zend_extension *extension)
|
|
|
|
{
|
2000-09-25 18:10:45 +00:00
|
|
|
#if ZEND_EXTENSIONS_SUPPORT && !ZEND_DEBUG
|
2000-03-29 19:26:34 +00:00
|
|
|
if (extension->handle) {
|
|
|
|
DL_UNLOAD(extension->handle);
|
|
|
|
}
|
1999-04-07 18:10:10 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-31 04:53:54 +00:00
|
|
|
static void zend_extension_message_dispatcher(zend_extension *extension, int num_args, va_list args TSRMLS_DC)
|
2000-04-29 10:20:40 +00:00
|
|
|
{
|
|
|
|
int message;
|
|
|
|
void *arg;
|
|
|
|
|
2000-04-29 10:34:03 +00:00
|
|
|
if (!extension->message_handler || num_args!=2) {
|
2000-04-29 10:20:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
message = va_arg(args, int);
|
|
|
|
arg = va_arg(args, void *);
|
|
|
|
extension->message_handler(message, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEND_API void zend_extension_dispatch_message(int message, void *arg)
|
|
|
|
{
|
2001-07-31 04:53:54 +00:00
|
|
|
TSRMLS_FETCH();
|
|
|
|
|
|
|
|
zend_llist_apply_with_arguments(&zend_extensions, (llist_apply_with_args_func_t) zend_extension_message_dispatcher TSRMLS_CC, 2, message, arg);
|
2000-04-29 10:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-07 18:10:10 +00:00
|
|
|
ZEND_API int zend_get_resource_handle(zend_extension *extension)
|
|
|
|
{
|
2000-04-25 14:20:52 +00:00
|
|
|
if (last_resource_number<ZEND_MAX_RESERVED_RESOURCES) {
|
1999-04-07 18:10:10 +00:00
|
|
|
extension->resource_number = last_resource_number;
|
2000-04-25 14:20:52 +00:00
|
|
|
return last_resource_number++;
|
1999-04-07 18:10:10 +00:00
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2000-11-08 14:25:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
ZEND_API zend_extension *zend_get_extension(char *extension_name)
|
|
|
|
{
|
|
|
|
zend_llist_element *element;
|
|
|
|
|
|
|
|
for (element = zend_extensions.head; element; element = element->next) {
|
|
|
|
zend_extension *extension = (zend_extension *) element->data;
|
|
|
|
|
|
|
|
if (!strcmp(extension->name, extension_name)) {
|
|
|
|
return extension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-07-18 11:16:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Support for dynamic loading of MH_BUNDLEs on Darwin / Mac OS X
|
|
|
|
*
|
|
|
|
*/
|
2006-05-09 23:53:23 +00:00
|
|
|
|
2002-07-18 11:16:34 +00:00
|
|
|
#if HAVE_MACH_O_DYLD_H
|
|
|
|
|
2006-05-09 23:53:23 +00:00
|
|
|
void *zend_mh_bundle_load(char* bundle_path)
|
2002-07-18 11:16:34 +00:00
|
|
|
{
|
|
|
|
NSObjectFileImage bundle_image;
|
|
|
|
NSModule bundle_handle;
|
|
|
|
NSSymbol bundle_init_nssymbol;
|
|
|
|
void (*bundle_init)(void);
|
|
|
|
|
|
|
|
if (NSCreateObjectFileImageFromFile(bundle_path, &bundle_image) != NSObjectFileImageSuccess) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-05-09 23:53:23 +00:00
|
|
|
|
2002-07-18 11:16:34 +00:00
|
|
|
bundle_handle = NSLinkModule(bundle_image, bundle_path, NSLINKMODULE_OPTION_PRIVATE);
|
|
|
|
NSDestroyObjectFileImage(bundle_image);
|
2006-05-09 23:53:23 +00:00
|
|
|
|
2002-07-18 11:16:34 +00:00
|
|
|
/* call the init function of the bundle */
|
|
|
|
bundle_init_nssymbol = NSLookupSymbolInModule(bundle_handle, "__init");
|
|
|
|
if (bundle_init_nssymbol != NULL) {
|
|
|
|
bundle_init = NSAddressOfSymbol(bundle_init_nssymbol);
|
|
|
|
bundle_init();
|
|
|
|
}
|
2006-05-09 23:53:23 +00:00
|
|
|
|
2002-07-18 11:16:34 +00:00
|
|
|
return bundle_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
int zend_mh_bundle_unload(void *bundle_handle)
|
|
|
|
{
|
|
|
|
NSSymbol bundle_fini_nssymbol;
|
|
|
|
void (*bundle_fini)(void);
|
2006-05-09 23:53:23 +00:00
|
|
|
|
2002-07-18 11:16:34 +00:00
|
|
|
/* call the fini function of the bundle */
|
|
|
|
bundle_fini_nssymbol = NSLookupSymbolInModule(bundle_handle, "__fini");
|
|
|
|
if (bundle_fini_nssymbol != NULL) {
|
|
|
|
bundle_fini = NSAddressOfSymbol(bundle_fini_nssymbol);
|
|
|
|
bundle_fini();
|
|
|
|
}
|
2006-05-09 23:53:23 +00:00
|
|
|
|
2002-07-18 11:16:34 +00:00
|
|
|
return (int) NSUnLinkModule(bundle_handle, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *zend_mh_bundle_symbol(void *bundle_handle, const char *symbol_name)
|
|
|
|
{
|
|
|
|
NSSymbol symbol;
|
|
|
|
symbol = NSLookupSymbolInModule(bundle_handle, symbol_name);
|
|
|
|
return NSAddressOfSymbol(symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *zend_mh_bundle_error(void)
|
|
|
|
{
|
|
|
|
/* Witness the state of the art error reporting */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_MACH_O_DYLD_H */
|
2003-02-01 01:49:15 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*/
|