Cleanup ext/session so that I can do a unicode update without going insane.

This commit is contained in:
Sara Golemon 2007-01-04 22:04:38 +00:00
parent b34d9eaa74
commit 21bac192e9
5 changed files with 761 additions and 734 deletions

View File

@ -87,9 +87,10 @@ static int ps_files_valid_key(const char *key)
len = p - key;
if (len == 0)
if (len == 0) {
ret = 0;
}
return ret;
}
@ -101,9 +102,11 @@ static char *ps_files_path_create(char *buf, size_t buflen, ps_files *data, cons
int n;
key_len = strlen(key);
if (key_len <= data->dirdepth || buflen <
(strlen(data->basedir) + 2 * data->dirdepth + key_len + 5 + sizeof(FILE_PREFIX)))
if (key_len <= data->dirdepth ||
buflen < (strlen(data->basedir) + 2 * data->dirdepth + key_len + 5 + sizeof(FILE_PREFIX))) {
return NULL;
}
p = key;
memcpy(buf, data->basedir, data->basedir_len);
n = data->basedir_len;
@ -149,27 +152,27 @@ static void ps_files_open(ps_files *data, const char *key TSRMLS_DC)
}
ps_files_close(data);
if (!ps_files_valid_key(key)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The session id contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,'");
PS(invalid_session_id) = 1;
return;
}
if (!ps_files_path_create(buf, sizeof(buf), data, key))
if (!ps_files_path_create(buf, sizeof(buf), data, key)) {
return;
}
data->lastkey = estrdup(key);
data->fd = VCWD_OPEN_MODE(buf, O_CREAT | O_RDWR | O_BINARY,
data->filemode);
data->fd = VCWD_OPEN_MODE(buf, O_CREAT | O_RDWR | O_BINARY, data->filemode);
if (data->fd != -1) {
flock(data->fd, LOCK_EX);
#ifdef F_SETFD
#ifndef FD_CLOEXEC
#define FD_CLOEXEC 1
#endif
# ifndef FD_CLOEXEC
# define FD_CLOEXEC 1
# endif
if (fcntl(data->fd, F_SETFD, FD_CLOEXEC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "fcntl(%d, F_SETFD, FD_CLOEXEC) failed: %s (%d)", data->fd, strerror(errno), errno);
}
@ -209,15 +212,16 @@ static int ps_files_cleanup_dir(const char *dirname, int maxlifetime TSRMLS_DC)
while (php_readdir_r(dir, (struct dirent *) dentry, &entry) == 0 && entry) {
/* does the file start with our prefix? */
if (!strncmp(entry->d_name, FILE_PREFIX, sizeof(FILE_PREFIX) - 1)) {
size_t entry_len;
size_t entry_len = strlen(entry->d_name);
entry_len = strlen(entry->d_name);
/* does it fit into our buffer? */
if (entry_len + dirname_len + 2 < MAXPATHLEN) {
/* create the full path.. */
memcpy(buf + dirname_len + 1, entry->d_name, entry_len);
/* NUL terminate it and */
buf[dirname_len + entry_len + 1] = '\0';
/* check whether its last access was more than maxlifet ago */
if (VCWD_STAT(buf, &sbuf) == 0 &&
#ifdef NETWARE
@ -268,8 +272,7 @@ PS_OPEN_FUNC(files)
errno = 0;
dirdepth = (size_t) strtol(argv[0], NULL, 10);
if (errno == ERANGE) {
php_error(E_WARNING,
"The first parameter in session.save_path is invalid");
php_error(E_WARNING, "The first parameter in session.save_path is invalid");
return FAILURE;
}
}
@ -278,15 +281,13 @@ PS_OPEN_FUNC(files)
errno = 0;
filemode = strtol(argv[1], NULL, 8);
if (errno == ERANGE || filemode < 0 || filemode > 07777) {
php_error(E_WARNING,
"The second parameter in session.save_path is invalid");
php_error(E_WARNING, "The second parameter in session.save_path is invalid");
return FAILURE;
}
}
save_path = argv[argc - 1];
data = emalloc(sizeof(*data));
memset(data, 0, sizeof(*data));
data = ecalloc(1, sizeof(*data));
data->fd = -1;
data->dirdepth = dirdepth;
@ -305,8 +306,10 @@ PS_CLOSE_FUNC(files)
ps_files_close(data);
if (data->lastkey)
if (data->lastkey) {
efree(data->lastkey);
}
efree(data->basedir);
efree(data);
*mod_data = NULL;
@ -321,19 +324,21 @@ PS_READ_FUNC(files)
PS_FILES_DATA;
ps_files_open(data, key TSRMLS_CC);
if (data->fd < 0)
if (data->fd < 0) {
return FAILURE;
if (fstat(data->fd, &sbuf))
}
if (fstat(data->fd, &sbuf)) {
return FAILURE;
}
data->st_size = *vallen = sbuf.st_size;
if (sbuf.st_size == 0) {
*val = STR_EMPTY_ALLOC();
return SUCCESS;
}
*val = emalloc(sbuf.st_size);
#if defined(HAVE_PREAD)
@ -344,10 +349,11 @@ PS_READ_FUNC(files)
#endif
if (n != sbuf.st_size) {
if (n == -1)
if (n == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "read failed: %s (%d)", strerror(errno), errno);
else
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "read returned less bytes than requested");
}
efree(*val);
return FAILURE;
}
@ -361,16 +367,18 @@ PS_WRITE_FUNC(files)
PS_FILES_DATA;
ps_files_open(data, key TSRMLS_CC);
if (data->fd < 0)
if (data->fd < 0) {
return FAILURE;
}
/*
* truncate file, if the amount of new data is smaller than
* the existing data set.
*/
if (vallen < (int)data->st_size)
if (vallen < (int)data->st_size) {
ftruncate(data->fd, 0);
}
#if defined(HAVE_PWRITE)
n = pwrite(data->fd, val, vallen, 0);
@ -380,10 +388,11 @@ PS_WRITE_FUNC(files)
#endif
if (n != vallen) {
if (n == -1)
if (n == -1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "write failed: %s (%d)", strerror(errno), errno);
else
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "write wrote less bytes than requested");
}
return FAILURE;
}
@ -395,9 +404,10 @@ PS_DESTROY_FUNC(files)
char buf[MAXPATHLEN];
PS_FILES_DATA;
if (!ps_files_path_create(buf, sizeof(buf), data, key))
if (!ps_files_path_create(buf, sizeof(buf), data, key)) {
return FAILURE;
}
if (data->fd != -1) {
ps_files_close(data);
@ -422,9 +432,10 @@ PS_GC_FUNC(files)
we return SUCCESS, since all cleanup should be handled by
an external entity (i.e. find -ctime x | xargs rm) */
if (data->dirdepth == 0)
if (data->dirdepth == 0) {
*nrdels = ps_files_cleanup_dir(data->basedir, maxlifetime TSRMLS_CC);
}
return SUCCESS;
}

View File

@ -161,9 +161,9 @@ static void ps_sd_destroy(ps_mm *data, ps_sd *sd)
slot = ps_sd_hash(sd->key, strlen(sd->key)) & data->hash_max;
if (data->hash[slot] == sd)
if (data->hash[slot] == sd) {
data->hash[slot] = sd->next;
else {
} else {
ps_sd *prev;
/* There must be some entry before the one we want to delete */
@ -172,8 +172,11 @@ static void ps_sd_destroy(ps_mm *data, ps_sd *sd)
}
data->hash_cnt--;
if (sd->data)
if (sd->data) {
mm_free(data->mm, sd->data);
}
mm_free(data->mm, sd);
}
@ -185,15 +188,19 @@ static ps_sd *ps_sd_lookup(ps_mm *data, const char *key, int rw)
hv = ps_sd_hash(key, strlen(key));
slot = hv & data->hash_max;
for (prev = NULL, ret = data->hash[slot]; ret; prev = ret, ret = ret->next)
if (ret->hv == hv && !strcmp(ret->key, key))
for (prev = NULL, ret = data->hash[slot]; ret; prev = ret, ret = ret->next) {
if (ret->hv == hv && !strcmp(ret->key, key)) {
break;
}
}
if (ret && rw && ret != data->hash[slot]) {
/* Move the entry to the top of the linked list */
if (prev)
if (prev) {
prev->next = ret->next;
}
ret->next = data->hash[slot];
data->hash[slot] = ret;
}
@ -238,11 +245,12 @@ static void ps_mm_destroy(ps_mm *data)
an Apache child dies! */
if (data->owner != getpid()) return;
for (h = 0; h < data->hash_max + 1; h++)
for (h = 0; h < data->hash_max + 1; 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);
@ -257,13 +265,15 @@ PHP_MINIT_FUNCTION(ps_mm)
int ret;
ps_mm_instance = calloc(sizeof(*ps_mm_instance), 1);
if (!ps_mm_instance)
if (!ps_mm_instance) {
return FAILURE;
}
if (!sprintf(euid,"%d", geteuid()))
if (!sprintf(euid,"%d", geteuid())) {
return FAILURE;
}
/* Directory + '/' + File + Module Name + Effective UID + \0 */
/* Directory + '/' + File + Module Name + Effective UID + \0 */
ps_mm_path = emalloc(save_path_len+1+sizeof(PS_MM_FILE)+mod_name_len+strlen(euid)+1);
memcpy(ps_mm_path, PS(save_path), save_path_len + 1);
@ -271,6 +281,7 @@ PHP_MINIT_FUNCTION(ps_mm)
ps_mm_path[save_path_len] = DEFAULT_SLASH;
ps_mm_path[save_path_len+1] = '\0';
}
strcat(ps_mm_path, PS_MM_FILE);
strcat(ps_mm_path, sapi_module.name);
strcat(ps_mm_path, euid);
@ -302,8 +313,9 @@ PS_OPEN_FUNC(mm)
{
ps_mm_debug(("open: ps_mm_instance=%p\n", ps_mm_instance));
if (!ps_mm_instance)
if (!ps_mm_instance) {
return FAILURE;
}
PS_SET_MOD_DATA(ps_mm_instance);
@ -354,8 +366,9 @@ PS_WRITE_FUNC(mm)
if (sd) {
if (vallen >= sd->alloclen) {
if (data->mm)
if (data->mm) {
mm_free(data->mm, sd->data);
}
sd->alloclen = vallen + 1;
sd->data = mm_malloc(data->mm, sd->alloclen);
@ -385,9 +398,10 @@ PS_DESTROY_FUNC(mm)
mm_lock(data->mm, MM_LOCK_RW);
sd = ps_sd_lookup(data, key, 0);
if (sd)
if (sd) {
ps_sd_destroy(data, sd);
}
mm_unlock(data->mm);
return SUCCESS;
@ -410,7 +424,7 @@ PS_GC_FUNC(mm)
mm_lock(data->mm, MM_LOCK_RW);
ehash = data->hash + data->hash_max + 1;
for (ohash = data->hash; ohash < ehash; ohash++)
for (ohash = data->hash; ohash < ehash; ohash++) {
for (sd = *ohash; sd; sd = next) {
next = sd->next;
if (sd->ctime < limit) {
@ -419,6 +433,7 @@ PS_GC_FUNC(mm)
(*nrdels)++;
}
}
}
mm_unlock(data->mm);

View File

@ -29,25 +29,20 @@ ps_module ps_mod_user = {
#define SESS_ZVAL_LONG(val, a) \
{ \
MAKE_STD_ZVAL(a); \
Z_TYPE_P(a) = IS_LONG; \
Z_LVAL_P(a) = val; \
ZVAL_LONG(a, val); \
}
#define SESS_ZVAL_STRING(vl, a) \
{ \
int len = strlen(vl); \
MAKE_STD_ZVAL(a); \
Z_TYPE_P(a) = IS_STRING; \
Z_STRLEN_P(a) = len; \
Z_STRVAL_P(a) = estrndup(vl, len); \
ZVAL_STRINGL(a, vl, len, 1); \
}
#define SESS_ZVAL_STRINGN(vl, ln, a) \
{ \
MAKE_STD_ZVAL(a); \
Z_TYPE_P(a) = IS_STRING; \
Z_STRLEN_P(a) = ln; \
Z_STRVAL_P(a) = estrndup(vl, ln); \
ZVAL_STRINGL(a, vl, ln, 1); \
}
@ -74,8 +69,7 @@ static zval *ps_call_handler(zval *func, int argc, zval **argv TSRMLS_DC)
zval *retval; \
int ret = FAILURE; \
ps_user *mdata = PS_GET_MOD_DATA(); \
if (!mdata) \
return FAILURE
if (!mdata) { return FAILURE; }
#define PSF(a) mdata->name.ps_##a
@ -92,8 +86,8 @@ PS_OPEN_FUNC(user)
zval *args[2];
STDVARS;
SESS_ZVAL_STRING(save_path, args[0]);
SESS_ZVAL_STRING(session_name, args[1]);
SESS_ZVAL_STRING((char*)save_path, args[0]);
SESS_ZVAL_STRING((char*)session_name, args[1]);
retval = ps_call_handler(PSF(open), 2, args TSRMLS_CC);
@ -107,8 +101,10 @@ PS_CLOSE_FUNC(user)
retval = ps_call_handler(PSF(close), 0, NULL TSRMLS_CC);
for (i = 0; i < 6; i++)
for (i = 0; i < 6; i++) {
zval_ptr_dtor(&mdata->names[i]);
}
efree(mdata);
PS_SET_MOD_DATA(NULL);
@ -121,7 +117,7 @@ PS_READ_FUNC(user)
zval *args[1];
STDVARS;
SESS_ZVAL_STRING(key, args[0]);
SESS_ZVAL_STRING((char*)key, args[0]);
retval = ps_call_handler(PSF(read), 1, args TSRMLS_CC);
@ -142,8 +138,8 @@ PS_WRITE_FUNC(user)
zval *args[2];
STDVARS;
SESS_ZVAL_STRING(key, args[0]);
SESS_ZVAL_STRINGN(val, vallen, args[1]);
SESS_ZVAL_STRING((char*)key, args[0]);
SESS_ZVAL_STRINGN((char*)val, vallen, args[1]);
retval = ps_call_handler(PSF(write), 2, args TSRMLS_CC);
@ -155,7 +151,7 @@ PS_DESTROY_FUNC(user)
zval *args[1];
STDVARS;
SESS_ZVAL_STRING(key, args[0]);
SESS_ZVAL_STRING((char*)key, args[0]);
retval = ps_call_handler(PSF(destroy), 1, args TSRMLS_CC);

View File

@ -132,23 +132,6 @@ typedef php_ps_globals zend_ps_globals;
extern zend_module_entry session_module_entry;
#define phpext_session_ptr &session_module_entry
PHP_FUNCTION(session_name);
PHP_FUNCTION(session_module_name);
PHP_FUNCTION(session_save_path);
PHP_FUNCTION(session_id);
PHP_FUNCTION(session_regenerate_id);
PHP_FUNCTION(session_decode);
PHP_FUNCTION(session_encode);
PHP_FUNCTION(session_start);
PHP_FUNCTION(session_destroy);
PHP_FUNCTION(session_unset);
PHP_FUNCTION(session_set_save_handler);
PHP_FUNCTION(session_cache_expire);
PHP_FUNCTION(session_cache_limiter);
PHP_FUNCTION(session_set_cookie_params);
PHP_FUNCTION(session_get_cookie_params);
PHP_FUNCTION(session_write_close);
#ifdef ZTS
#define PS(v) TSRMG(ps_globals_id, php_ps_globals *, v)
#else

File diff suppressed because it is too large Load Diff