summaryrefslogtreecommitdiff
path: root/Zend/zend_extensions.c
blob: c08e0c086a9029e8d6277811adef7e3effb32f06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
   +----------------------------------------------------------------------+
   | Zend Engine                                                          |
   +----------------------------------------------------------------------+
   | Copyright (c) 1998, 1999 Andi Gutmans, Zeev Suraski                  |
   +----------------------------------------------------------------------+
   | This source file is subject to version 0.91 of the Zend license,     |
   | that is bundled with this package in the file LICENSE, and is        | 
   | available at through the world-wide-web at                           |
   | http://www.zend.com/license/0_91.txt.                                |
   | 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.              |
   +----------------------------------------------------------------------+
   | Authors: Andi Gutmans <andi@zend.com>                                |
   |          Zeev Suraski <zeev@zend.com>                                |
   +----------------------------------------------------------------------+
*/


#include "zend_extensions.h"

ZEND_API zend_llist zend_extensions;
static int last_resource_number;

int zend_load_extensions(char **extension_paths)
{
	char **p = extension_paths;

	if (!p) {
		return SUCCESS;
	}
	while (*p) {
		if (zend_load_extension(*p)==FAILURE) {
			return FAILURE;
		}
		p++;
	}
	return SUCCESS;
}


int zend_load_extension(char *path)
{
#if ZEND_EXTENSIONS_SUPPORT
	DL_HANDLE handle;
	zend_extension extension, *new_extension;
	zend_extension_version_info *extension_version_info;

	handle = DL_LOAD(path);
	if (!handle) {
#if !(WIN32||WINNT)
		fprintf(stderr, "Failed loading %s:  %s\n", path, dlerror());
#else
		fprintf(stderr, "Failed loading %s\n", path);
#endif
		return FAILURE;
	}

	extension_version_info = (zend_extension_version_info *) DL_FETCH_SYMBOL(handle, "extension_version_info");
	new_extension = (zend_extension *) DL_FETCH_SYMBOL(handle, "zend_extension_entry");
	if (!extension_version_info || !new_extension) {
		fprintf(stderr, "%s doesn't appear to be a valid Zend extension\n", path);
		return FAILURE;
	}

	if (extension_version_info->zend_extension_api_no > ZEND_EXTENSION_API_NO) {
		fprintf(stderr, "%s requires Zend version %s or later\n"
					"Current version %s, API version %d\n",
					new_extension->name,
					extension_version_info->required_zend_version,
					ZEND_VERSION,
					ZEND_EXTENSION_API_NO);
		DL_UNLOAD(handle);
		return FAILURE;
	} else if (extension_version_info->zend_extension_api_no < ZEND_EXTENSION_API_NO) {
		/* we may be able to allow for downwards compatability in some harmless cases. */
		fprintf(stderr, "%s is outdated (API version %d, current version %d)\n"
					"Contact %s at %s for a later version of this module.\n",
					new_extension->name,
					extension_version_info->zend_extension_api_no,
					ZEND_EXTENSION_API_NO,
					new_extension->author,
					new_extension->URL);
		DL_UNLOAD(handle);
		return FAILURE;
	} else if (ZTS_V!=extension_version_info->thread_safe) {
		fprintf(stderr, "Cannot load %s - it %s thread safe, whereas Zend %s\n",
					new_extension->name,
					(extension_version_info->thread_safe?"is":"isn't"),
					(ZTS_V?"is":"isn't"));
		DL_UNLOAD(handle);
		return FAILURE;
	} else if (ZEND_DEBUG!=extension_version_info->debug) {
		fprintf(stderr, "Cannot load %s - it %s debug information, whereas Zend %s\n",
					new_extension->name,
					(extension_version_info->debug?"contains":"does not contain"),
					(ZEND_DEBUG?"does":"does not"));
		DL_UNLOAD(handle);
		return FAILURE;
	}

	if (new_extension->startup) {
		if (new_extension->startup(new_extension)!=SUCCESS) {
			DL_UNLOAD(handle);
			return FAILURE;
		}
	}
	extension = *new_extension;
	extension.handle = handle;

	zend_llist_add_element(&zend_extensions, &extension);

	/*fprintf(stderr, "Loaded %s, version %s\n", extension.name, extension.version);*/

	zend_append_version_info(&extension);
	return SUCCESS;
#else
	fprintf(stderr, "Extensions are not supported on this platform.\n");
	return FAILURE;
#endif
}

static void zend_extension_shutdown(zend_extension *extension)
{
#if ZEND_EXTENSIONS_SUPPORT
	if (extension->shutdown) {
		extension->shutdown(extension);
	}
#endif
}


void zend_shutdown_extensions()
{
	zend_llist_apply(&zend_extensions, (void (*)(void *)) zend_extension_shutdown);
	zend_llist_destroy(&zend_extensions);
}


void zend_extension_dtor(zend_extension *extension)
{
#if ZEND_EXTENSIONS_SUPPORT
	DL_UNLOAD(extension->handle);
#endif
}


ZEND_API int zend_get_resource_handle(zend_extension *extension)
{
	if (last_resource_number<4) {
		extension->resource_number = last_resource_number;
		return last_resource_number;
	} else {
		return -1;
	}
}