dl.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  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. /* $Id$ */
  21. #include "php.h"
  22. #include "dl.h"
  23. #include "php_globals.h"
  24. #include "php_ini.h"
  25. #include "ext/standard/info.h"
  26. #include "SAPI.h"
  27. #if defined(HAVE_LIBDL)
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #ifdef HAVE_STRING_H
  31. #include <string.h>
  32. #else
  33. #include <strings.h>
  34. #endif
  35. #ifdef PHP_WIN32
  36. #include "win32/param.h"
  37. #include "win32/winutil.h"
  38. #define GET_DL_ERROR() php_win_err()
  39. #elif defined(NETWARE)
  40. #include <sys/param.h>
  41. #define GET_DL_ERROR() dlerror()
  42. #else
  43. #include <sys/param.h>
  44. #define GET_DL_ERROR() DL_ERROR()
  45. #endif
  46. #endif /* defined(HAVE_LIBDL) */
  47. /* {{{ proto int dl(string extension_filename)
  48. Load a PHP extension at runtime */
  49. PHPAPI PHP_FUNCTION(dl)
  50. {
  51. char *filename;
  52. int filename_len;
  53. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
  54. return;
  55. }
  56. if (!PG(enable_dl)) {
  57. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Dynamically loaded extensions aren't enabled");
  58. RETURN_FALSE;
  59. }
  60. if (filename_len >= MAXPATHLEN) {
  61. php_error_docref(NULL TSRMLS_CC, E_WARNING, "File name exceeds the maximum allowed length of %d characters", MAXPATHLEN);
  62. RETURN_FALSE;
  63. }
  64. if ((strncmp(sapi_module.name, "cgi", 3) != 0) &&
  65. (strcmp(sapi_module.name, "cli") != 0) &&
  66. (strncmp(sapi_module.name, "embed", 5) != 0)
  67. ) {
  68. #ifdef ZTS
  69. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not supported in multithreaded Web servers - use extension=%s in your php.ini", filename);
  70. RETURN_FALSE;
  71. #else
  72. php_error_docref(NULL TSRMLS_CC, E_DEPRECATED, "dl() is deprecated - use extension=%s in your php.ini", filename);
  73. #endif
  74. }
  75. php_dl(filename, MODULE_TEMPORARY, return_value, 0 TSRMLS_CC);
  76. if (Z_LVAL_P(return_value) == 1) {
  77. EG(full_tables_cleanup) = 1;
  78. }
  79. }
  80. /* }}} */
  81. #if defined(HAVE_LIBDL)
  82. #ifdef ZTS
  83. #define USING_ZTS 1
  84. #else
  85. #define USING_ZTS 0
  86. #endif
  87. /* {{{ php_load_extension
  88. */
  89. PHPAPI int php_load_extension(char *filename, int type, int start_now TSRMLS_DC)
  90. {
  91. void *handle;
  92. char *libpath;
  93. zend_module_entry *module_entry;
  94. zend_module_entry *(*get_module)(void);
  95. int error_type;
  96. char *extension_dir;
  97. if (type == MODULE_PERSISTENT) {
  98. extension_dir = INI_STR("extension_dir");
  99. } else {
  100. extension_dir = PG(extension_dir);
  101. }
  102. if (type == MODULE_TEMPORARY) {
  103. error_type = E_WARNING;
  104. } else {
  105. error_type = E_CORE_WARNING;
  106. }
  107. /* Check if passed filename contains directory separators */
  108. if (strchr(filename, '/') != NULL || strchr(filename, DEFAULT_SLASH) != NULL) {
  109. /* Passing modules with full path is not supported for dynamically loaded extensions */
  110. if (type == MODULE_TEMPORARY) {
  111. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Temporary module name should contain only filename");
  112. return FAILURE;
  113. }
  114. libpath = estrdup(filename);
  115. } else if (extension_dir && extension_dir[0]) {
  116. int extension_dir_len = strlen(extension_dir);
  117. if (IS_SLASH(extension_dir[extension_dir_len-1])) {
  118. spprintf(&libpath, 0, "%s%s", extension_dir, filename); /* SAFE */
  119. } else {
  120. spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, filename); /* SAFE */
  121. }
  122. } else {
  123. return FAILURE; /* Not full path given or extension_dir is not set */
  124. }
  125. /* load dynamic symbol */
  126. handle = DL_LOAD(libpath);
  127. if (!handle) {
  128. #if PHP_WIN32
  129. char *err = GET_DL_ERROR();
  130. if (err && (*err != "")) {
  131. php_error_docref(NULL TSRMLS_CC, error_type, "Unable to load dynamic library '%s' - %s", libpath, err);
  132. LocalFree(err);
  133. } else {
  134. php_error_docref(NULL TSRMLS_CC, error_type, "Unable to load dynamic library '%s' - %s", libpath, "Unknown reason");
  135. }
  136. #else
  137. php_error_docref(NULL TSRMLS_CC, error_type, "Unable to load dynamic library '%s' - %s", libpath, GET_DL_ERROR());
  138. GET_DL_ERROR(); /* free the buffer storing the error */
  139. #endif
  140. efree(libpath);
  141. return FAILURE;
  142. }
  143. efree(libpath);
  144. get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "get_module");
  145. /* Some OS prepend _ to symbol names while their dynamic linker
  146. * does not do that automatically. Thus we check manually for
  147. * _get_module. */
  148. if (!get_module) {
  149. get_module = (zend_module_entry *(*)(void)) DL_FETCH_SYMBOL(handle, "_get_module");
  150. }
  151. if (!get_module) {
  152. if (DL_FETCH_SYMBOL(handle, "zend_extension_entry") || DL_FETCH_SYMBOL(handle, "_zend_extension_entry")) {
  153. DL_UNLOAD(handle);
  154. php_error_docref(NULL TSRMLS_CC, error_type, "Invalid library (appears to be a Zend Extension, try loading using zend_extension=%s from php.ini)", filename);
  155. return FAILURE;
  156. }
  157. DL_UNLOAD(handle);
  158. php_error_docref(NULL TSRMLS_CC, error_type, "Invalid library (maybe not a PHP library) '%s'", filename);
  159. return FAILURE;
  160. }
  161. module_entry = get_module();
  162. if (module_entry->zend_api != ZEND_MODULE_API_NO) {
  163. /* Check for pre-4.1.0 module which has a slightly different module_entry structure :( */
  164. struct pre_4_1_0_module_entry {
  165. char *name;
  166. zend_function_entry *functions;
  167. int (*module_startup_func)(INIT_FUNC_ARGS);
  168. int (*module_shutdown_func)(SHUTDOWN_FUNC_ARGS);
  169. int (*request_startup_func)(INIT_FUNC_ARGS);
  170. int (*request_shutdown_func)(SHUTDOWN_FUNC_ARGS);
  171. void (*info_func)(ZEND_MODULE_INFO_FUNC_ARGS);
  172. int (*global_startup_func)(void);
  173. int (*global_shutdown_func)(void);
  174. int globals_id;
  175. int module_started;
  176. unsigned char type;
  177. void *handle;
  178. int module_number;
  179. unsigned char zend_debug;
  180. unsigned char zts;
  181. unsigned int zend_api;
  182. };
  183. const char *name;
  184. int zend_api;
  185. if ((((struct pre_4_1_0_module_entry *)module_entry)->zend_api > 20000000) &&
  186. (((struct pre_4_1_0_module_entry *)module_entry)->zend_api < 20010901)
  187. ) {
  188. name = ((struct pre_4_1_0_module_entry *)module_entry)->name;
  189. zend_api = ((struct pre_4_1_0_module_entry *)module_entry)->zend_api;
  190. } else {
  191. name = module_entry->name;
  192. zend_api = module_entry->zend_api;
  193. }
  194. php_error_docref(NULL TSRMLS_CC, error_type,
  195. "%s: Unable to initialize module\n"
  196. "Module compiled with module API=%d\n"
  197. "PHP compiled with module API=%d\n"
  198. "These options need to match\n",
  199. name, zend_api, ZEND_MODULE_API_NO);
  200. DL_UNLOAD(handle);
  201. return FAILURE;
  202. }
  203. if(strcmp(module_entry->build_id, ZEND_MODULE_BUILD_ID)) {
  204. php_error_docref(NULL TSRMLS_CC, error_type,
  205. "%s: Unable to initialize module\n"
  206. "Module compiled with build ID=%s\n"
  207. "PHP compiled with build ID=%s\n"
  208. "These options need to match\n",
  209. module_entry->name, module_entry->build_id, ZEND_MODULE_BUILD_ID);
  210. DL_UNLOAD(handle);
  211. return FAILURE;
  212. }
  213. module_entry->type = type;
  214. module_entry->module_number = zend_next_free_module();
  215. module_entry->handle = handle;
  216. if ((module_entry = zend_register_module_ex(module_entry TSRMLS_CC)) == NULL) {
  217. DL_UNLOAD(handle);
  218. return FAILURE;
  219. }
  220. if ((type == MODULE_TEMPORARY || start_now) && zend_startup_module_ex(module_entry TSRMLS_CC) == FAILURE) {
  221. DL_UNLOAD(handle);
  222. return FAILURE;
  223. }
  224. if ((type == MODULE_TEMPORARY || start_now) && module_entry->request_startup_func) {
  225. if (module_entry->request_startup_func(type, module_entry->module_number TSRMLS_CC) == FAILURE) {
  226. php_error_docref(NULL TSRMLS_CC, error_type, "Unable to initialize module '%s'", module_entry->name);
  227. DL_UNLOAD(handle);
  228. return FAILURE;
  229. }
  230. }
  231. return SUCCESS;
  232. }
  233. /* }}} */
  234. /* {{{ php_dl
  235. */
  236. PHPAPI void php_dl(char *file, int type, zval *return_value, int start_now TSRMLS_DC)
  237. {
  238. /* Load extension */
  239. if (php_load_extension(file, type, start_now TSRMLS_CC) == FAILURE) {
  240. RETVAL_FALSE;
  241. } else {
  242. RETVAL_TRUE;
  243. }
  244. }
  245. /* }}} */
  246. PHP_MINFO_FUNCTION(dl)
  247. {
  248. php_info_print_table_row(2, "Dynamic Library Support", "enabled");
  249. }
  250. #else
  251. PHPAPI void php_dl(char *file, int type, zval *return_value, int start_now TSRMLS_DC)
  252. {
  253. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot dynamically load %s - dynamic modules are not supported", file);
  254. RETURN_FALSE;
  255. }
  256. PHP_MINFO_FUNCTION(dl)
  257. {
  258. PUTS("Dynamic Library support not available<br />.\n");
  259. }
  260. #endif
  261. /*
  262. * Local variables:
  263. * tab-width: 4
  264. * c-basic-offset: 4
  265. * End:
  266. * vim600: sw=4 ts=4 fdm=marker
  267. * vim<600: sw=4 ts=4
  268. */