From: Sara Golemon Date: Mon, 21 Jun 2004 19:33:48 +0000 (+0000) Subject: MFH: BugFix#28868 Wrapper hash not thread-safe X-Git-Tag: php-4.3.9RC1~88 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=10a0500ad960085c02aeeccfaeb290ad80efd56b;p=php MFH: BugFix#28868 Wrapper hash not thread-safe --- diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 731ddd4942..2939dd6173 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -1188,6 +1188,9 @@ PHP_RINIT_FUNCTION(basic) /* Reset magic_quotes_runtime */ PG(magic_quotes_runtime) = INI_BOOL("magic_quotes_runtime"); + /* Default to global wrappers only */ + FG(stream_wrappers) = NULL; + return SUCCESS; } @@ -1211,6 +1214,12 @@ PHP_RSHUTDOWN_FUNCTION(basic) } STR_FREE(BG(locale_string)); + if (FG(stream_wrappers)) { + zend_hash_destroy(FG(stream_wrappers)); + efree(FG(stream_wrappers)); + FG(stream_wrappers) = NULL; + } + PHP_RSHUTDOWN(fsock) (SHUTDOWN_FUNC_ARGS_PASSTHRU); PHP_RSHUTDOWN(filestat) (SHUTDOWN_FUNC_ARGS_PASSTHRU); PHP_RSHUTDOWN(syslog) (SHUTDOWN_FUNC_ARGS_PASSTHRU); diff --git a/ext/standard/file.h b/ext/standard/file.h index 5bee48c7ea..7109789abb 100644 --- a/ext/standard/file.h +++ b/ext/standard/file.h @@ -118,6 +118,7 @@ typedef struct { long default_socket_timeout; char *user_agent; char *user_stream_current_filename; /* for simple recursion protection */ + HashTable *stream_wrappers; /* per-request copy of url_stream_wrappers_hash */ } php_file_globals; #ifdef ZTS diff --git a/main/php_streams.h b/main/php_streams.h index 636dbf07bf..52c6f8d886 100755 --- a/main/php_streams.h +++ b/main/php_streams.h @@ -530,6 +530,7 @@ PHP_RSHUTDOWN_FUNCTION(streams); PHPAPI int php_register_url_stream_wrapper(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC); PHPAPI int php_unregister_url_stream_wrapper(char *protocol TSRMLS_DC); +PHPAPI int php_register_url_stream_wrapper_volatile(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC); PHPAPI php_stream *_php_stream_open_wrapper_ex(char *path, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC); PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, char **path_for_open, int options TSRMLS_DC); PHPAPI char *php_stream_locate_eol(php_stream *stream, char *buf, size_t buf_len TSRMLS_DC); @@ -627,7 +628,8 @@ PHPAPI php_stream_context *php_stream_context_set(php_stream *stream, php_stream /* Give other modules access to the url_stream_wrappers_hash */ -PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash(); +PHPAPI HashTable *_php_stream_get_url_stream_wrappers_hash(TSRMLS_D); +#define php_stream_get_url_stream_wrappers_hash() _php_stream_get_url_stream_wrappers_hash(TSRMLS_C) #endif diff --git a/main/streams.c b/main/streams.c index 14d0bc827e..1fe6c6b219 100755 --- a/main/streams.c +++ b/main/streams.c @@ -96,6 +96,7 @@ FILE *fopencookie(void *cookie, const char *mode, COOKIE_IO_FUNCTIONS_T *funcs) # define PHP_STREAM_COOKIE_FUNCTIONS stream_cookie_functions #endif +/* Global wrapper hash, copied to FG(stream_wrappers) on registration of volatile wrapper */ static HashTable url_stream_wrappers_hash; static int le_stream = FAILURE; /* true global */ static int le_pstream = FAILURE; /* true global */ @@ -2335,6 +2336,7 @@ int php_shutdown_stream_wrappers(int module_number TSRMLS_DC) return SUCCESS; } +/* API for registering GLOBAL wrappers */ PHPAPI int php_register_url_stream_wrapper(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC) { return zend_hash_add(&url_stream_wrappers_hash, protocol, strlen(protocol), wrapper, sizeof(*wrapper), NULL); @@ -2344,6 +2346,20 @@ PHPAPI int php_unregister_url_stream_wrapper(char *protocol TSRMLS_DC) { return zend_hash_del(&url_stream_wrappers_hash, protocol, strlen(protocol)); } + +/* API for registering VOLATILE wrappers */ +PHPAPI int php_register_url_stream_wrapper_volatile(char *protocol, php_stream_wrapper *wrapper TSRMLS_DC) +{ + if (!FG(stream_wrappers)) { + php_stream_wrapper tmpwrapper; + + FG(stream_wrappers) = emalloc(sizeof(HashTable)); + zend_hash_init(FG(stream_wrappers), 0, NULL, NULL, 1); + zend_hash_copy(FG(stream_wrappers), &url_stream_wrappers_hash, NULL, &tmpwrapper, sizeof(php_stream_wrapper)); + } + + return zend_hash_add(FG(stream_wrappers), protocol, strlen(protocol), wrapper, sizeof(*wrapper), NULL); +} /* }}} */ @@ -2459,6 +2475,7 @@ static php_stream_wrapper php_plain_files_wrapper = { PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, char **path_for_open, int options TSRMLS_DC) { + HashTable *wrapper_hash = (FG(stream_wrappers) ? FG(stream_wrappers) : &url_stream_wrappers_hash); php_stream_wrapper *wrapper = NULL; const char *p, *protocol = NULL; int n = 0; @@ -2485,7 +2502,7 @@ PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, char } if (protocol) { - if (FAILURE == zend_hash_find(&url_stream_wrappers_hash, (char*)protocol, n, (void**)&wrapper)) { + if (FAILURE == zend_hash_find(wrapper_hash, (char*)protocol, n, (void**)&wrapper)) { char wrapper_name[32]; if (options & REPORT_ERRORS) { @@ -2911,9 +2928,9 @@ PHPAPI int php_stream_context_set_option(php_stream_context *context, return zend_hash_update(Z_ARRVAL_PP(wrapperhash), (char*)optionname, strlen(optionname)+1, (void**)&copied_val, sizeof(zval *), NULL); } -PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash() +PHPAPI HashTable *_php_stream_get_url_stream_wrappers_hash(TSRMLS_D) { - return &url_stream_wrappers_hash; + return (FG(stream_wrappers) ? FG(stream_wrappers) : &url_stream_wrappers_hash); } /* diff --git a/main/user_streams.c b/main/user_streams.c index 55130544c7..d807555190 100644 --- a/main/user_streams.c +++ b/main/user_streams.c @@ -51,7 +51,6 @@ static void stream_wrapper_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) { struct php_user_stream_wrapper * uwrap = (struct php_user_stream_wrapper*)rsrc->ptr; - php_unregister_url_stream_wrapper(uwrap->protoname TSRMLS_CC); efree(uwrap->protoname); efree(uwrap->classname); efree(uwrap); @@ -361,7 +360,7 @@ PHP_FUNCTION(stream_register_wrapper) #ifdef ZEND_ENGINE_2 uwrap->ce = *(zend_class_entry**)uwrap->ce; #endif - if (php_register_url_stream_wrapper(protocol, &uwrap->wrapper TSRMLS_CC) == SUCCESS) { + if (php_register_url_stream_wrapper_volatile(protocol, &uwrap->wrapper TSRMLS_CC) == SUCCESS) { RETURN_TRUE; } } else {