]> granicus.if.org Git - php/commitdiff
Always seed the LCG from the request-init hook, otherwise the seed
authorSascha Schumann <sas@php.net>
Fri, 21 Sep 2001 13:38:44 +0000 (13:38 +0000)
committerSascha Schumann <sas@php.net>
Fri, 21 Sep 2001 13:38:44 +0000 (13:38 +0000)
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.

ext/session/session.c
ext/standard/basic_functions.c
ext/standard/lcg.c
ext/standard/php_lcg.h

index 3e9f9931f364b5f6e097b1b0aea8778891ea3ae6..a33e8374eb6bd30de20573342ce04f75a50f298c 100644 (file)
@@ -642,10 +642,11 @@ static void last_modified(TSRMLS_D)
 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 @@ CACHE_LIMITER_FUNC(public)
        
        last_modified(TSRMLS_C);
 }
-       
+
 CACHE_LIMITER_FUNC(private)
 {
        char buf[MAX_STR + 1];
@@ -741,7 +742,10 @@ static void php_session_send_cookie(TSRMLS_D)
        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 f401bd192a23b8234eabe37f2655bddda303c55d..24420cd6c16509149c620fee11e8294f41ad5b2f 100644 (file)
@@ -951,9 +951,7 @@ PHP_MINIT_FUNCTION(basic)
        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_FUNCTION(basic)
        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 91863d0a4842e1ebf229eeae1ee409ffda971a22..a76725ec381bcb2914ba378eb7ce3dce042e1b7d 100644 (file)
 #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 @@ static int php_lcg_initialized = 0;
 
 #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 @@ double php_combined_lcg(TSRMLS_D)
        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 */
index 05bcc83c4415242838f12523b36ad01c65f0d951..ecb97178acd3137044caacfae2549148a0709f76 100644 (file)
 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