Skip to content

Commit a4a2f66

Browse files
author
Yasuo Ohgaki
committed
Revert "Revert "Implement RFC Add session_gc() https://wiki.php.net/rfc/session-gc""
This reverts commit 355c7e7.
1 parent b36ae74 commit a4a2f66

File tree

6 files changed

+62
-17
lines changed

6 files changed

+62
-17
lines changed

ext/session/mod_files.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,11 @@ PS_GC_FUNC(files)
643643

644644
if (data->dirdepth == 0) {
645645
*nrdels = ps_files_cleanup_dir(data->basedir, maxlifetime);
646+
} else {
647+
*nrdels = -1; // Cannot process multiple depth save dir
646648
}
647649

648-
return SUCCESS;
650+
return *nrdels;
649651
}
650652

651653

ext/session/mod_mm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ PS_GC_FUNC(mm)
468468

469469
mm_unlock(data->mm);
470470

471-
return SUCCESS;
471+
return nrdels;
472472
}
473473

474474
PS_CREATE_SID_FUNC(mm)

ext/session/mod_user.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,22 @@ PS_DESTROY_FUNC(user)
176176
PS_GC_FUNC(user)
177177
{
178178
zval args[1];
179-
STDVARS;
179+
zval retval;
180180

181181
ZVAL_LONG(&args[0], maxlifetime);
182182

183183
ps_call_handler(&PSF(gc), 1, args, &retval);
184184

185-
FINISH;
185+
if (Z_TYPE(retval) == IS_LONG) {
186+
convert_to_long(&retval);
187+
return Z_LVAL(retval);
188+
}
189+
/* This is for older API compatibility */
190+
if (Z_TYPE(retval) == IS_TRUE) {
191+
return 1;
192+
}
193+
/* Anything else is some kind of error */
194+
return -1; // Error
186195
}
187196

188197
PS_CREATE_SID_FUNC(user)

ext/session/mod_user_class.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,18 @@ PHP_METHOD(SessionHandler, destroy)
148148
PHP_METHOD(SessionHandler, gc)
149149
{
150150
zend_long maxlifetime;
151-
int nrdels;
151+
zend_long nrdels = -1;
152152

153153
PS_SANITY_CHECK_IS_OPEN;
154154

155155
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &maxlifetime) == FAILURE) {
156156
return;
157157
}
158158

159-
RETURN_BOOL(SUCCESS == PS(default_mod)->s_gc(&PS(mod_data), maxlifetime, &nrdels));
159+
if (PS(default_mod)->s_gc(&PS(mod_data), maxlifetime, &nrdels) == FAILURE) {
160+
RETURN_FALSE;
161+
}
162+
RETURN_LONG(nrdels);
160163
}
161164
/* }}} */
162165

ext/session/php_session.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#define PS_READ_ARGS void **mod_data, zend_string *key, zend_string **val, zend_long maxlifetime
4040
#define PS_WRITE_ARGS void **mod_data, zend_string *key, zend_string *val, zend_long maxlifetime
4141
#define PS_DESTROY_ARGS void **mod_data, zend_string *key
42-
#define PS_GC_ARGS void **mod_data, zend_long maxlifetime, int *nrdels
42+
#define PS_GC_ARGS void **mod_data, zend_long maxlifetime, zend_long *nrdels
4343
#define PS_CREATE_SID_ARGS void **mod_data
4444
#define PS_VALIDATE_SID_ARGS void **mod_data, zend_string *key
4545
#define PS_UPDATE_TIMESTAMP_ARGS void **mod_data, zend_string *key, zend_string *val, zend_long maxlifetime
@@ -51,7 +51,7 @@ typedef struct ps_module_struct {
5151
int (*s_read)(PS_READ_ARGS);
5252
int (*s_write)(PS_WRITE_ARGS);
5353
int (*s_destroy)(PS_DESTROY_ARGS);
54-
int (*s_gc)(PS_GC_ARGS);
54+
zend_long (*s_gc)(PS_GC_ARGS);
5555
zend_string *(*s_create_sid)(PS_CREATE_SID_ARGS);
5656
int (*s_validate_sid)(PS_VALIDATE_SID_ARGS);
5757
int (*s_update_timestamp)(PS_UPDATE_TIMESTAMP_ARGS);
@@ -65,7 +65,7 @@ typedef struct ps_module_struct {
6565
#define PS_READ_FUNC(x) int ps_read_##x(PS_READ_ARGS)
6666
#define PS_WRITE_FUNC(x) int ps_write_##x(PS_WRITE_ARGS)
6767
#define PS_DESTROY_FUNC(x) int ps_delete_##x(PS_DESTROY_ARGS)
68-
#define PS_GC_FUNC(x) int ps_gc_##x(PS_GC_ARGS)
68+
#define PS_GC_FUNC(x) zend_long ps_gc_##x(PS_GC_ARGS)
6969
#define PS_CREATE_SID_FUNC(x) zend_string *ps_create_sid_##x(PS_CREATE_SID_ARGS)
7070
#define PS_VALIDATE_SID_FUNC(x) int ps_validate_sid_##x(PS_VALIDATE_SID_ARGS)
7171
#define PS_UPDATE_TIMESTAMP_FUNC(x) int ps_update_timestamp_##x(PS_UPDATE_TIMESTAMP_ARGS)

ext/session/session.c

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,19 +353,23 @@ PHPAPI int php_session_valid_key(const char *key) /* {{{ */
353353
/* }}} */
354354

355355

356-
static void php_session_gc(void) /* {{{ */
356+
static zend_long php_session_gc(zend_bool immediate) /* {{{ */
357357
{
358358
int nrand;
359+
zend_long num = -1;
359360

360361
/* GC must be done before reading session data. */
361-
if ((PS(mod_data) || PS(mod_user_implemented)) && PS(gc_probability) > 0) {
362-
int nrdels = -1;
363-
364-
nrand = (int) ((float) PS(gc_divisor) * php_combined_lcg());
365-
if (nrand < PS(gc_probability)) {
366-
PS(mod)->s_gc(&PS(mod_data), PS(gc_maxlifetime), &nrdels);
362+
if ((PS(mod_data) || PS(mod_user_implemented))) {
363+
if (immediate) {
364+
PS(mod)->s_gc(&PS(mod_data), PS(gc_maxlifetime), &num);
365+
return num;
366+
}
367+
nrand = (zend_long) ((float) PS(gc_divisor) * php_combined_lcg());
368+
if (PS(gc_probability) > 0 && nrand < PS(gc_probability)) {
369+
PS(mod)->s_gc(&PS(mod_data), PS(gc_maxlifetime), &num);
367370
}
368371
}
372+
return num;
369373
} /* }}} */
370374

371375
static void php_session_initialize(void) /* {{{ */
@@ -430,7 +434,7 @@ static void php_session_initialize(void) /* {{{ */
430434
}
431435

432436
/* GC must be done after read */
433-
php_session_gc();
437+
php_session_gc(0);
434438

435439
if (PS(session_vars)) {
436440
zend_string_release(PS(session_vars));
@@ -2247,6 +2251,32 @@ static PHP_FUNCTION(session_unset)
22472251
}
22482252
/* }}} */
22492253

2254+
/* {{{ proto int session_gc(void)
2255+
Perform GC and return number of deleted sessions */
2256+
static PHP_FUNCTION(session_gc)
2257+
{
2258+
zend_long num;
2259+
2260+
if (zend_parse_parameters_none() == FAILURE) {
2261+
return;
2262+
}
2263+
2264+
if (PS(session_status) != php_session_active) {
2265+
php_error_docref(NULL, E_WARNING, "Session is not active");
2266+
RETURN_FALSE;
2267+
}
2268+
2269+
num = php_session_gc(1);
2270+
if (num < 0) {
2271+
php_error_docref(NULL, E_WARNING, "Failed to perfom session GC");
2272+
RETURN_FALSE;
2273+
}
2274+
2275+
RETURN_LONG(num);
2276+
}
2277+
/* }}} */
2278+
2279+
22502280
/* {{{ proto void session_write_close(void)
22512281
Write session data and end session */
22522282
static PHP_FUNCTION(session_write_close)
@@ -2429,6 +2459,7 @@ static const zend_function_entry session_functions[] = {
24292459
PHP_FE(session_start, arginfo_session_void)
24302460
PHP_FE(session_destroy, arginfo_session_void)
24312461
PHP_FE(session_unset, arginfo_session_void)
2462+
PHP_FE(session_gc, arginfo_session_void)
24322463
PHP_FE(session_set_save_handler, arginfo_session_set_save_handler)
24332464
PHP_FE(session_cache_limiter, arginfo_session_cache_limiter)
24342465
PHP_FE(session_cache_expire, arginfo_session_cache_expire)

0 commit comments

Comments
 (0)