cvs: php4 /ext/session session.c /ext/standard basic_functions.c lcg.c php_lcg.h

From: Date: Fri, 21 Sep 2001 13:38:45 +0000
Subject: cvs: php4 /ext/session session.c /ext/standard basic_functions.c lcg.c php_lcg.h
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-7499@lists.php.net to get a copy of this message
sas Fri Sep 21 09:38:45 2001 EDT Modified files: /php4/ext/session session.c /php4/ext/standard basic_functions.c php_lcg.h lcg.c Log: Always seed the LCG from the request-init hook, otherwise the seed would be shared among the threads which is quite pointless. Also use a function of the current time as one factor. Use gettimeofday() instead of time(), because it is faster on some operating systems. Index: php4/ext/session/session.c diff -u php4/ext/session/session.c:1.252 php4/ext/session/session.c:1.253 --- php4/ext/session/session.c:1.252 Tue Sep 11 06:16:01 2001 +++ php4/ext/session/session.c Fri Sep 21 09:38:44 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: session.c,v 1.252 2001/09/11 10:16:01 sas Exp $ */ +/* $Id: session.c,v 1.253 2001/09/21 13:38:44 sas Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -642,10 +642,11 @@ CACHE_LIMITER_FUNC(public) { char buf[MAX_STR + 1]; + struct timeval tv; time_t now; - time(&now); - now += PS(cache_expire) * 60; + gettimeofday(&tv, NULL); + now = tv.tv_sec + PS(cache_expire) * 60; #define EXPIRES "Expires: " memcpy(buf, EXPIRES, sizeof(EXPIRES) - 1); strcpy_gmt(buf + sizeof(EXPIRES) - 1, &now); @@ -656,7 +657,7 @@ last_modified(TSRMLS_C); } - + CACHE_LIMITER_FUNC(private) { char buf[MAX_STR + 1]; @@ -741,7 +742,10 @@ smart_str_appends(&ncookie, PS(id)); if (PS(cookie_lifetime) > 0) { - date_fmt = php_std_date(time(NULL) + PS(cookie_lifetime)); + struct timeval tv; + + gettimeofday(&tv, NULL); + date_fmt = php_std_date(tv.tv_sec + PS(cookie_lifetime)); smart_str_appends(&ncookie, COOKIE_EXPIRES); smart_str_appends(&ncookie, date_fmt); Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.396 php4/ext/standard/basic_functions.c:1.397 --- php4/ext/standard/basic_functions.c:1.396 Tue Sep 11 22:07:46 2001 +++ php4/ext/standard/basic_functions.c Fri Sep 21 09:38:44 2001 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.396 2001/09/12 02:07:46 sniper Exp $ */ +/* $Id: basic_functions.c,v 1.397 2001/09/21 13:38:44 sas Exp $ */ #include "php.h" #include "php_main.h" @@ -951,9 +951,7 @@ PHP_MINIT(crypt) (INIT_FUNC_ARGS_PASSTHRU); #endif -#ifdef ZTS PHP_MINIT(lcg) (INIT_FUNC_ARGS_PASSTHRU); -#endif PHP_MINIT(dir) (INIT_FUNC_ARGS_PASSTHRU); PHP_MINIT(syslog) (INIT_FUNC_ARGS_PASSTHRU); @@ -1034,9 +1032,7 @@ PHP_RINIT(crypt) (INIT_FUNC_ARGS_PASSTHRU); #endif -#ifndef ZTS PHP_RINIT(lcg) (INIT_FUNC_ARGS_PASSTHRU); -#endif PHP_RINIT(filestat) (INIT_FUNC_ARGS_PASSTHRU); PHP_RINIT(syslog) (INIT_FUNC_ARGS_PASSTHRU); Index: php4/ext/standard/php_lcg.h diff -u php4/ext/standard/php_lcg.h:1.14 php4/ext/standard/php_lcg.h:1.15 --- php4/ext/standard/php_lcg.h:1.14 Sun Aug 5 23:50:51 2001 +++ php4/ext/standard/php_lcg.h Fri Sep 21 09:38:44 2001 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_lcg.h,v 1.14 2001/08/06 03:50:51 sas Exp $ */ +/* $Id: php_lcg.h,v 1.15 2001/09/21 13:38:44 sas Exp $ */ #ifndef PHP_LCG_H #define PHP_LCG_H @@ -26,16 +26,18 @@ typedef struct { php_int32 s1; php_int32 s2; + int seeded; } php_lcg_globals; -double php_combined_lcg(TSRMLS_D); +PHPAPI double php_combined_lcg(TSRMLS_D); PHP_FUNCTION(lcg_value); -#ifdef ZTS PHP_MINIT_FUNCTION(lcg); +PHP_RINIT_FUNCTION(lcg); + +#ifdef ZTS #define LCG(v) TSRMG(lcg_globals_id, php_lcg_globals *, v) #else -PHP_RINIT_FUNCTION(lcg); #define LCG(v) (lcg_globals.v) #endif Index: php4/ext/standard/lcg.c diff -u php4/ext/standard/lcg.c:1.27 php4/ext/standard/lcg.c:1.28 --- php4/ext/standard/lcg.c:1.27 Sun Sep 9 09:29:18 2001 +++ php4/ext/standard/lcg.c Fri Sep 21 09:38:44 2001 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: lcg.c,v 1.27 2001/09/09 13:29:18 derick Exp $ */ +/* $Id: lcg.c,v 1.28 2001/09/21 13:38:44 sas Exp $ */ #include "php.h" #include "php_lcg.h" @@ -25,11 +25,14 @@ #include <unistd.h> #endif +#if HAVE_SYS_TIME_H +#include <sys/time.h> +#endif + #ifdef ZTS int lcg_globals_id; #else static php_lcg_globals lcg_globals; -static int php_lcg_initialized = 0; #endif @@ -46,7 +49,7 @@ #define MODMULT(a, b, c, m, s) q = s/a;s=b*(s-a*q)-c*q;if(s<0)s+=m -double php_combined_lcg(TSRMLS_D) +PHPAPI double php_combined_lcg(TSRMLS_D) { php_int32 q; php_int32 z; @@ -55,39 +58,53 @@ MODMULT(52774, 40692, 3791, 2147483399L, LCG(s2)); z = LCG(s1) - LCG(s2); - if(z < 1) { + if (z < 1) { z += 2147483562; } return z * 4.656613e-10; } -static void lcg_init_globals(php_lcg_globals *lcg_globals_p TSRMLS_DC) +static void lcg_seed(TSRMLS_D) { - LCG(s1) = 1; + struct timeval tv; + + if (gettimeofday(&tv, NULL) == 0) { + LCG(s1) = tv.tv_sec ^ (~tv.tv_usec); + } else { + LCG(s1) = 1; + } #ifdef ZTS LCG(s2) = (long) tsrm_thread_id(); #else LCG(s2) = (long) getpid(); #endif + + LCG(seeded) = 1; +} + +static void lcg_init_globals(php_lcg_globals *lcg_globals_p TSRMLS_DC) +{ + LCG(seeded) = 0; } -#ifdef ZTS PHP_MINIT_FUNCTION(lcg) { +#ifdef ZTS ts_allocate_id(&lcg_globals_id, sizeof(php_lcg_globals), (ts_allocate_ctor) lcg_init_globals, NULL); +#else + lcg_init_globals(&lcg_globals); +#endif return SUCCESS; } -#else + PHP_RINIT_FUNCTION(lcg) { - if (!php_lcg_initialized) { - lcg_init_globals(&lcg_globals TSRMLS_CC); - php_lcg_initialized = 1; + if (!LCG(seeded)) { + lcg_seed(TSRMLS_C); } return SUCCESS; } -#endif /* {{{ proto double lcg_value() Returns a value from the combined linear congruential generator */

« previous php.cvs (#7499) next »