1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/*
+----------------------------------------------------------------------+
| PHP version 4.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.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. |
+----------------------------------------------------------------------+
| Authors: Sascha Schumann <sascha@schumann.cx> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#ifdef HAVE_LIBMM
#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"
#define PS_MM_PATH "/tmp/session_mm"
/*
* this list holds all data associated with one session
*/
typedef struct ps_sd {
struct ps_sd *next, *prev;
time_t ctime;
char *key;
void *data;
size_t datalen;
} ps_sd;
typedef struct {
MM *mm;
ps_sd **hash;
} ps_mm;
static ps_mm *ps_mm_instance = NULL;
/* should be a prime */
#define HASH_SIZE 577
#if 0
#define ps_mm_debug(a...) fprintf(stderr, a)
#else
#define ps_mm_debug(a...)
#endif
#define BITS_IN_int (sizeof(int) * CHAR_BIT)
#define THREE_QUARTERS ((int) ((BITS_IN_int * 3) / 4))
#define ONE_EIGTH ((int) (BITS_IN_int / 8))
#define HIGH_BITS (~((unsigned int)(~0) >> ONE_EIGTH))
/*
* Weinberger's generic hash algorithm, adapted by Holub
* (published in [Holub 1990])
*/
static unsigned int ps_sd_hash(const char *data)
{
unsigned int val, i;
for (val = 0; *data; data++) {
val = (val << ONE_EIGTH) + *data;
if ((i = val & HIGH_BITS) != 0)
val = (val ^ (i >> THREE_QUARTERS)) & -HIGH_BITS;
}
return val;
}
static ps_sd *ps_sd_new(ps_mm *data, const char *key, const void *sdata, size_t sdatalen)
{
unsigned int h;
ps_sd *sd;
h = ps_sd_hash(key) % HASH_SIZE;
sd = mm_malloc(data->mm, sizeof(*sd));
if (!sd)
return NULL;
sd->ctime = 0;
sd->data = mm_malloc(data->mm, sdatalen);
if (!sd->data) {
mm_free(data->mm, sd);
return NULL;
}
sd->datalen = sdatalen;
sd->key = mm_strdup(data->mm, key);
if (!sd->key) {
mm_free(data->mm, sd->data);
mm_free(data->mm, sd);
return NULL;
}
memcpy(sd->data, sdata, sdatalen);
if ((sd->next = data->hash[h]))
sd->next->prev = sd;
sd->prev = NULL;
ps_mm_debug("inserting %s(%p) into %d\n", key, sd, h);
data->hash[h] = sd;
return sd;
}
static void ps_sd_destroy(ps_mm *data, ps_sd *sd)
{
unsigned int h;
h = ps_sd_hash(sd->key) % HASH_SIZE;
if (sd->next)
sd->next->prev = sd->prev;
if (sd->prev)
sd->prev->next = sd->next;
if (data->hash[h] == sd)
data->hash[h] = sd->next;
mm_free(data->mm, sd->key);
if (sd->data)
mm_free(data->mm, sd->data);
mm_free(data->mm, sd);
}
static ps_sd *ps_sd_lookup(ps_mm *data, const char *key, int rw)
{
unsigned int h;
ps_sd *ret;
h = ps_sd_hash(key) % HASH_SIZE;
for (ret = data->hash[h]; ret; ret = ret->next)
if (!strcmp(ret->key, key))
break;
if (ret && rw && ret != data->hash[h]) {
data->hash[h]->prev = ret;
ret->next = data->hash[h];
data->hash[h] = ret;
ps_mm_debug("optimizing\n");
}
ps_mm_debug(stderr, "lookup(%s): ret=%p,h=%d\n", key, ret, h);
return ret;
}
ps_module ps_mod_mm = {
PS_MOD(mm)
};
#define PS_MM_DATA ps_mm *data = PS_GET_MOD_DATA()
static int ps_mm_initialize(ps_mm *data, const char *path)
{
data->mm = mm_create(0, path);
data->hash = mm_calloc(data->mm, HASH_SIZE, sizeof(*data->hash));
return SUCCESS;
}
static void ps_mm_destroy(ps_mm *data)
{
int h;
ps_sd *sd, *next;
for (h = 0; h < HASH_SIZE; h++)
for (sd = data->hash[h]; sd; sd = next) {
next = sd->next;
ps_sd_destroy(data, sd);
}
mm_free(data->mm, data->hash);
mm_destroy(data->mm);
}
PHP_GINIT_FUNCTION(ps_mm)
{
ps_mm_instance = calloc(sizeof(*ps_mm_instance), 1);
ps_mm_initialize(ps_mm_instance, PS_MM_PATH);
return SUCCESS;
}
PHP_GSHUTDOWN_FUNCTION(ps_mm)
{
ps_mm_destroy(ps_mm_instance);
free(ps_mm_instance);
return SUCCESS;
}
PS_OPEN_FUNC(mm)
{
ps_mm_debug("open: ps_mm_instance=%p\n", ps_mm_instance);
if (!ps_mm_instance)
return FAILURE;
PS_SET_MOD_DATA(ps_mm_instance);
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);
sd = ps_sd_lookup(data, key, 0);
if (sd) {
*vallen = sd->datalen;
*val = emalloc(sd->datalen);
memcpy(*val, sd->data, sd->datalen);
ret = SUCCESS;
}
mm_unlock(data->mm);
return ret;
}
PS_WRITE_FUNC(mm)
{
PS_MM_DATA;
ps_sd *sd;
if (vallen == 0) return SUCCESS;
mm_lock(data->mm, MM_LOCK_RW);
sd = ps_sd_lookup(data, key, 1);
if (!sd) {
sd = ps_sd_new(data, key, val, vallen);
ps_mm_debug(stderr, "new one for %s\n", key);
} else {
ps_mm_debug(stderr, "found existing one for %s\n", key);
mm_free(data->mm, sd->data);
sd->datalen = vallen;
sd->data = mm_malloc(data->mm, vallen);
if (!sd->data) {
ps_sd_destroy(data, sd);
sd = NULL;
} else
memcpy(sd->data, val, vallen);
}
if (sd)
time(&sd->ctime);
mm_unlock(data->mm);
return sd ? SUCCESS : FAILURE;
}
PS_DESTROY_FUNC(mm)
{
PS_MM_DATA;
ps_sd *sd;
mm_lock(data->mm, MM_LOCK_RW);
sd = ps_sd_lookup(data, key, 0);
if (sd)
ps_sd_destroy(data, sd);
mm_unlock(data->mm);
return SUCCESS;
}
PS_GC_FUNC(mm)
{
PS_MM_DATA;
int h;
time_t now;
ps_sd *sd, *next;
*nrdels = 0;
ps_mm_debug("gc\n");
mm_lock(data->mm, MM_LOCK_RW);
time(&now);
for (h = 0; h < HASH_SIZE; h++)
for (sd = data->hash[h]; sd; sd = next) {
next = sd->next;
ps_mm_debug("looking at %s\n", sd->key);
if ((now - sd->ctime) > maxlifetime) {
ps_sd_destroy(data, sd);
*nrdels++;
}
}
mm_unlock(data->mm);
return SUCCESS;
}
zend_module_entry php_session_mm_module = {
"Session MM",
NULL,
NULL, NULL,
NULL, NULL,
NULL,
PHP_GINIT(ps_mm), PHP_GSHUTDOWN(ps_mm),
STANDARD_MODULE_PROPERTIES_EX
};
#endif
|