php-src/ext/session/mod_mm.c

513 lines
10 KiB
C
Raw Normal View History

2009-05-18 16:10:09 +00:00
/*
+----------------------------------------------------------------------+
2004-01-08 08:18:22 +00:00
| PHP Version 5 |
+----------------------------------------------------------------------+
2014-01-03 03:06:16 +00:00
| Copyright (c) 1997-2014 The PHP Group |
+----------------------------------------------------------------------+
2006-01-01 12:51:34 +00:00
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
2006-01-01 12:51:34 +00:00
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
2002-02-28 08:29:35 +00:00
| Author: Sascha Schumann <sascha@schumann.cx> |
+----------------------------------------------------------------------+
*/
2001-06-06 14:32:27 +00:00
/* $Id$ */
#include "php.h"
#ifdef HAVE_LIBMM
#include <unistd.h>
#include <mm.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "php_session.h"
#include "mod_mm.h"
#include "SAPI.h"
#ifdef ZTS
# error mm is not thread-safe
#endif
#define PS_MM_FILE "session_mm_"
/* For php_uint32 */
#include "ext/standard/basic_functions.h"
2009-05-18 16:10:09 +00:00
/* This list holds all data associated with one session. */
typedef struct ps_sd {
struct ps_sd *next;
php_uint32 hv; /* hash value of key */
time_t ctime; /* time of last change */
void *data;
size_t datalen; /* amount of valid data */
size_t alloclen; /* amount of allocated memory for data */
char key[1]; /* inline key */
} ps_sd;
typedef struct {
MM *mm;
ps_sd **hash;
php_uint32 hash_max;
php_uint32 hash_cnt;
pid_t owner;
} ps_mm;
static ps_mm *ps_mm_instance = NULL;
#if 0
2009-05-18 16:10:09 +00:00
# define ps_mm_debug(a) printf a
#else
2009-05-18 16:10:09 +00:00
# define ps_mm_debug(a)
#endif
static inline php_uint32 ps_sd_hash(const char *data, int len)
{
php_uint32 h;
const char *e = data + len;
2009-05-18 16:10:09 +00:00
for (h = 2166136261U; data < e; ) {
h *= 16777619;
h ^= *data++;
}
2009-05-18 16:10:09 +00:00
return h;
}
static void hash_split(ps_mm *data)
{
php_uint32 nmax;
ps_sd **nhash;
ps_sd **ohash, **ehash;
ps_sd *ps, *next;
2009-05-18 16:10:09 +00:00
nmax = ((data->hash_max + 1) << 1) - 1;
nhash = mm_calloc(data->mm, nmax + 1, sizeof(*data->hash));
2009-05-18 16:10:09 +00:00
if (!nhash) {
/* no further memory to expand hash table */
return;
}
ehash = data->hash + data->hash_max + 1;
for (ohash = data->hash; ohash < ehash; ohash++) {
for (ps = *ohash; ps; ps = next) {
next = ps->next;
ps->next = nhash[ps->hv & nmax];
nhash[ps->hv & nmax] = ps;
}
}
mm_free(data->mm, data->hash);
data->hash = nhash;
data->hash_max = nmax;
}
static ps_sd *ps_sd_new(ps_mm *data, const char *key)
{
php_uint32 hv, slot;
ps_sd *sd;
int keylen;
2009-05-18 16:10:09 +00:00
keylen = strlen(key);
2009-05-18 16:10:09 +00:00
sd = mm_malloc(data->mm, sizeof(ps_sd) + keylen);
if (!sd) {
2002-12-05 20:39:43 +00:00
TSRMLS_FETCH();
2002-12-05 20:13:35 +00:00
2013-06-25 10:47:04 +00:00
php_error_docref(NULL TSRMLS_CC, E_WARNING, "mm_malloc failed, avail %ld, err %s", mm_available(data->mm), mm_error());
return NULL;
}
hv = ps_sd_hash(key, keylen);
slot = hv & data->hash_max;
2009-05-18 16:10:09 +00:00
sd->ctime = 0;
sd->hv = hv;
sd->data = NULL;
sd->alloclen = sd->datalen = 0;
2009-05-18 16:10:09 +00:00
memcpy(sd->key, key, keylen + 1);
2009-05-18 16:10:09 +00:00
sd->next = data->hash[slot];
data->hash[slot] = sd;
data->hash_cnt++;
2009-05-18 16:10:09 +00:00
if (!sd->next) {
2009-05-18 16:10:09 +00:00
if (data->hash_cnt >= data->hash_max) {
hash_split(data);
2009-05-18 16:10:09 +00:00
}
}
2009-05-18 16:10:09 +00:00
ps_mm_debug(("inserting %s(%p) into slot %d\n", key, sd, slot));
return sd;
}
static void ps_sd_destroy(ps_mm *data, ps_sd *sd)
{
php_uint32 slot;
slot = ps_sd_hash(sd->key, strlen(sd->key)) & data->hash_max;
2009-05-18 16:10:09 +00:00
if (data->hash[slot] == sd) {
data->hash[slot] = sd->next;
2009-05-18 16:10:09 +00:00
} else {
ps_sd *prev;
/* There must be some entry before the one we want to delete */
for (prev = data->hash[slot]; prev->next != sd; prev = prev->next);
prev->next = sd->next;
}
2009-05-18 16:10:09 +00:00
data->hash_cnt--;
2009-05-18 16:10:09 +00:00
if (sd->data) {
2000-02-11 13:41:30 +00:00
mm_free(data->mm, sd->data);
2009-05-18 16:10:09 +00:00
}
mm_free(data->mm, sd);
}
static ps_sd *ps_sd_lookup(ps_mm *data, const char *key, int rw)
{
php_uint32 hv, slot;
ps_sd *ret, *prev;
hv = ps_sd_hash(key, strlen(key));
slot = hv & data->hash_max;
2009-05-18 16:10:09 +00:00
for (prev = NULL, ret = data->hash[slot]; ret; prev = ret, ret = ret->next) {
if (ret->hv == hv && !strcmp(ret->key, key)) {
2000-02-11 13:41:30 +00:00
break;
2009-05-18 16:10:09 +00:00
}
}
if (ret && rw && ret != data->hash[slot]) {
/* Move the entry to the top of the linked list */
2009-05-18 16:10:09 +00:00
if (prev) {
prev->next = ret->next;
2009-05-18 16:10:09 +00:00
}
ret->next = data->hash[slot];
data->hash[slot] = ret;
}
ps_mm_debug(("lookup(%s): ret=%p,hv=%u,slot=%d\n", key, ret, hv, slot));
2009-05-18 16:10:09 +00:00
return ret;
}
2013-06-25 10:47:04 +00:00
static int ps_mm_key_exists(ps_mm *data, const char *key TSRMLS_DC)
{
ps_sd *sd;
if (!key) {
return FAILURE;
}
sd = ps_sd_lookup(data, key, 0);
if (sd) {
return SUCCESS;
}
return FAILURE;
}
ps_module ps_mod_mm = {
2013-06-25 10:47:04 +00:00
PS_MOD_SID(mm)
};
#define PS_MM_DATA ps_mm *data = PS_GET_MOD_DATA()
static int ps_mm_initialize(ps_mm *data, const char *path)
{
data->owner = getpid();
data->mm = mm_create(0, path);
if (!data->mm) {
return FAILURE;
}
data->hash_cnt = 0;
data->hash_max = 511;
data->hash = mm_calloc(data->mm, data->hash_max + 1, sizeof(ps_sd *));
if (!data->hash) {
mm_destroy(data->mm);
return FAILURE;
}
return SUCCESS;
}
static void ps_mm_destroy(ps_mm *data)
{
int h;
ps_sd *sd, *next;
/* This function is called during each module shutdown,
but we must not release the shared memory pool, when
an Apache child dies! */
2009-05-18 16:10:09 +00:00
if (data->owner != getpid()) {
return;
}
2009-05-18 16:10:09 +00:00
for (h = 0; h < data->hash_max + 1; h++) {
2000-02-11 13:41:30 +00:00
for (sd = data->hash[h]; sd; sd = next) {
next = sd->next;
ps_sd_destroy(data, sd);
}
2009-05-18 16:10:09 +00:00
}
mm_free(data->mm, data->hash);
mm_destroy(data->mm);
free(data);
}
2001-05-23 17:10:40 +00:00
PHP_MINIT_FUNCTION(ps_mm)
{
int save_path_len = strlen(PS(save_path));
int mod_name_len = strlen(sapi_module.name);
2007-02-19 23:53:00 +00:00
int euid_len;
char *ps_mm_path, euid[30];
int ret;
ps_mm_instance = calloc(sizeof(*ps_mm_instance), 1);
2009-05-18 16:10:09 +00:00
if (!ps_mm_instance) {
return FAILURE;
2007-02-19 23:53:00 +00:00
}
2007-02-27 03:28:17 +00:00
if (!(euid_len = slprintf(euid, sizeof(euid), "%d", geteuid()))) {
free(ps_mm_instance);
ps_mm_instance = NULL;
return FAILURE;
2007-02-19 23:53:00 +00:00
}
2009-05-18 16:10:09 +00:00
/* Directory + '/' + File + Module Name + Effective UID + \0 */
2007-02-19 23:53:00 +00:00
ps_mm_path = emalloc(save_path_len + 1 + (sizeof(PS_MM_FILE) - 1) + mod_name_len + euid_len + 1);
2009-05-18 16:10:09 +00:00
2007-02-19 23:53:00 +00:00
memcpy(ps_mm_path, PS(save_path), save_path_len);
if (save_path_len && PS(save_path)[save_path_len - 1] != DEFAULT_SLASH) {
ps_mm_path[save_path_len] = DEFAULT_SLASH;
2007-02-19 23:53:00 +00:00
save_path_len++;
}
2007-02-26 17:47:21 +00:00
memcpy(ps_mm_path + save_path_len, PS_MM_FILE, sizeof(PS_MM_FILE) - 1);
2007-02-19 23:53:00 +00:00
save_path_len += sizeof(PS_MM_FILE) - 1;
memcpy(ps_mm_path + save_path_len, sapi_module.name, mod_name_len);
save_path_len += mod_name_len;
memcpy(ps_mm_path + save_path_len, euid, euid_len);
ps_mm_path[save_path_len + euid_len] = '\0';
ret = ps_mm_initialize(ps_mm_instance, ps_mm_path);
2009-05-18 16:10:09 +00:00
efree(ps_mm_path);
2009-05-18 16:10:09 +00:00
if (ret != SUCCESS) {
free(ps_mm_instance);
ps_mm_instance = NULL;
return FAILURE;
}
2009-05-18 16:10:09 +00:00
php_session_register_module(&ps_mod_mm);
return SUCCESS;
}
2001-05-23 17:10:40 +00:00
PHP_MSHUTDOWN_FUNCTION(ps_mm)
{
if (ps_mm_instance) {
ps_mm_destroy(ps_mm_instance);
return SUCCESS;
}
return FAILURE;
}
PS_OPEN_FUNC(mm)
{
ps_mm_debug(("open: ps_mm_instance=%p\n", ps_mm_instance));
2009-05-18 16:10:09 +00:00
if (!ps_mm_instance) {
return FAILURE;
2009-05-18 16:10:09 +00:00
}
PS_SET_MOD_DATA(ps_mm_instance);
2009-05-18 16:10:09 +00:00
return SUCCESS;
}
PS_CLOSE_FUNC(mm)
{
PS_SET_MOD_DATA(NULL);
return SUCCESS;
}
PS_READ_FUNC(mm)
{
PS_MM_DATA;
ps_sd *sd;
int ret = FAILURE;
mm_lock(data->mm, MM_LOCK_RD);
2009-05-18 16:10:09 +00:00
2013-06-25 10:47:04 +00:00
/* If there is an ID and strict mode, verify existence */
if (PS(use_strict_mode)
&& ps_mm_key_exists(data, key TSRMLS_CC) == FAILURE) {
/* key points to PS(id), but cannot change here. */
if (key) {
efree(PS(id));
PS(id) = NULL;
}
PS(id) = PS(mod)->s_create_sid((void **)&data, NULL TSRMLS_CC);
if (!PS(id)) {
return FAILURE;
}
if (PS(use_cookies)) {
PS(send_cookie) = 1;
}
2013-08-20 23:08:55 +00:00
php_session_reset_id(TSRMLS_C);
PS(session_status) = php_session_active;
2013-06-25 10:47:04 +00:00
}
2013-08-20 23:08:55 +00:00
sd = ps_sd_lookup(data, PS(id), 0);
2000-02-11 13:41:30 +00:00
if (sd) {
*vallen = sd->datalen;
*val = emalloc(sd->datalen + 1);
memcpy(*val, sd->data, sd->datalen);
(*val)[sd->datalen] = '\0';
ret = SUCCESS;
}
mm_unlock(data->mm);
2009-05-18 16:10:09 +00:00
return ret;
}
PS_WRITE_FUNC(mm)
{
PS_MM_DATA;
ps_sd *sd;
mm_lock(data->mm, MM_LOCK_RW);
sd = ps_sd_lookup(data, key, 1);
2000-02-11 13:41:30 +00:00
if (!sd) {
sd = ps_sd_new(data, key);
ps_mm_debug(("new entry for %s\n", key));
}
if (sd) {
if (vallen >= sd->alloclen) {
2009-05-18 16:10:09 +00:00
if (data->mm) {
mm_free(data->mm, sd->data);
2009-05-18 16:10:09 +00:00
}
sd->alloclen = vallen + 1;
sd->data = mm_malloc(data->mm, sd->alloclen);
if (!sd->data) {
ps_sd_destroy(data, sd);
2002-12-05 20:13:35 +00:00
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot allocate new data segment");
sd = NULL;
}
}
if (sd) {
sd->datalen = vallen;
memcpy(sd->data, val, vallen);
time(&sd->ctime);
}
}
mm_unlock(data->mm);
2009-05-18 16:10:09 +00:00
return sd ? SUCCESS : FAILURE;
}
PS_DESTROY_FUNC(mm)
{
PS_MM_DATA;
ps_sd *sd;
2009-05-18 16:10:09 +00:00
mm_lock(data->mm, MM_LOCK_RW);
2009-05-18 16:10:09 +00:00
sd = ps_sd_lookup(data, key, 0);
2009-05-18 16:10:09 +00:00
if (sd) {
ps_sd_destroy(data, sd);
2009-05-18 16:10:09 +00:00
}
mm_unlock(data->mm);
2009-05-18 16:10:09 +00:00
return SUCCESS;
}
2009-05-18 16:10:09 +00:00
PS_GC_FUNC(mm)
{
PS_MM_DATA;
time_t limit;
ps_sd **ohash, **ehash;
ps_sd *sd, *next;
2009-05-18 16:10:09 +00:00
*nrdels = 0;
ps_mm_debug(("gc\n"));
2009-05-18 16:10:09 +00:00
time(&limit);
limit -= maxlifetime;
mm_lock(data->mm, MM_LOCK_RW);
ehash = data->hash + data->hash_max + 1;
2009-05-18 16:10:09 +00:00
for (ohash = data->hash; ohash < ehash; ohash++) {
for (sd = *ohash; sd; sd = next) {
next = sd->next;
if (sd->ctime < limit) {
ps_mm_debug(("purging %s\n", sd->key));
ps_sd_destroy(data, sd);
(*nrdels)++;
}
}
2009-05-18 16:10:09 +00:00
}
mm_unlock(data->mm);
2009-05-18 16:10:09 +00:00
return SUCCESS;
}
2013-06-25 10:47:04 +00:00
PS_CREATE_SID_FUNC(mm)
{
char *sid;
int maxfail = 3;
2013-06-25 10:47:04 +00:00
PS_MM_DATA;
do {
sid = php_session_create_id((void **)&data, newlen TSRMLS_CC);
/* Check collision */
if (ps_mm_key_exists(data, sid TSRMLS_CC) == SUCCESS) {
if (sid) {
efree(sid);
sid = NULL;
}
if (!(maxfail--)) {
return NULL;
}
}
} while(!sid);
2013-06-25 10:47:04 +00:00
return sid;
}
#endif
2001-06-06 14:32:27 +00:00
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
2001-06-06 14:32:27 +00:00
*/