zend_extensions.c 9.9 KB

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