php-src/ext/session/mod_files.c

353 lines
7.4 KiB
C
Raw Normal View History

1999-06-06 03:52:58 +08:00
/*
+----------------------------------------------------------------------+
2001-12-11 23:32:16 +08:00
| PHP Version 4 |
1999-06-06 03:52:58 +08:00
+----------------------------------------------------------------------+
2001-12-11 23:32:16 +08:00
| Copyright (c) 1997-2002 The PHP Group |
1999-06-06 03:52:58 +08:00
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
1999-07-16 21:13:16 +08:00
| 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. |
1999-07-16 21:13:16 +08:00
| 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. |
1999-06-06 03:52:58 +08:00
+----------------------------------------------------------------------+
2000-07-10 18:09:15 +08:00
| Authors: Sascha Schumann <sascha@schumann.cx> |
1999-06-06 03:52:58 +08:00
+----------------------------------------------------------------------+
*/
2001-06-06 22:32:27 +08:00
/* $Id$ */
1999-07-16 03:43:26 +08:00
#include "php.h"
1999-06-06 03:52:58 +08:00
#include <sys/stat.h>
#include <sys/types.h>
1999-07-04 07:43:02 +08:00
#if HAVE_SYS_FILE_H
1999-07-04 03:08:03 +08:00
#include <sys/file.h>
1999-07-04 07:43:02 +08:00
#endif
#if HAVE_DIRENT_H
#include <dirent.h>
#endif
2000-02-11 23:59:30 +08:00
#ifdef PHP_WIN32
#include "win32/readdir.h"
#endif
#include <time.h>
1999-06-06 03:52:58 +08:00
#include <fcntl.h>
#include <errno.h>
1999-06-06 03:52:58 +08:00
#include "php_session.h"
#include "mod_files.h"
#include "ext/standard/flock_compat.h"
1999-06-06 03:52:58 +08:00
#define FILE_PREFIX "sess_"
1999-06-06 03:52:58 +08:00
typedef struct {
int fd;
char *lastkey;
char *basedir;
size_t basedir_len;
2001-05-13 15:37:28 +08:00
size_t dirdepth;
size_t st_size;
1999-06-06 03:52:58 +08:00
} ps_files;
ps_module ps_mod_files = {
PS_MOD(files)
};
static int ps_files_valid_key(const char *key)
{
size_t len;
const char *p;
char c;
int ret = 1;
2000-02-11 21:41:30 +08:00
for (p = key; (c = *p); p++) {
/* valid characters are a..z,A..Z,0..9 */
2000-02-11 21:41:30 +08:00
if (!((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
1999-10-22 02:51:57 +08:00
(c >= '0' && c <= '9'))) {
ret = 0;
break;
}
}
len = p - key;
2000-02-11 21:41:30 +08:00
if (len == 0)
ret = 0;
return ret;
}
static char *ps_files_path_create(char *buf, size_t buflen, ps_files *data, const char *key)
1999-06-06 03:52:58 +08:00
{
size_t key_len;
const char *p;
int i;
int n;
1999-09-04 16:33:31 +08:00
key_len = strlen(key);
if (key_len <= data->dirdepth || buflen <
(strlen(data->basedir) + 2 * data->dirdepth + key_len + 5 + sizeof(FILE_PREFIX)))
1999-09-04 16:33:31 +08:00
return NULL;
p = key;
memcpy(buf, data->basedir, data->basedir_len);
n = data->basedir_len;
buf[n++] = PHP_DIR_SEPARATOR;
2001-06-11 07:26:35 +08:00
for (i = 0; i < (int)data->dirdepth; i++) {
1999-09-04 16:33:31 +08:00
buf[n++] = *p++;
2000-08-21 06:18:19 +08:00
buf[n++] = PHP_DIR_SEPARATOR;
1999-09-04 16:33:31 +08:00
}
memcpy(buf + n, FILE_PREFIX, sizeof(FILE_PREFIX) - 1);
n += sizeof(FILE_PREFIX) - 1;
memcpy(buf + n, key, key_len);
n += key_len;
buf[n] = '\0';
1999-09-04 16:33:31 +08:00
return buf;
}
#ifndef O_BINARY
#define O_BINARY 0
#endif
2000-10-16 18:35:43 +08:00
static void ps_files_close(ps_files *data)
{
if (data->fd != -1) {
close(data->fd);
data->fd = -1;
}
}
static void ps_files_open(ps_files *data, const char *key)
1999-09-04 16:33:31 +08:00
{
char buf[MAXPATHLEN];
2001-08-05 09:43:02 +08:00
TSRMLS_FETCH();
2000-02-11 21:41:30 +08:00
if (data->fd < 0 || !data->lastkey || strcmp(key, data->lastkey)) {
if (data->lastkey) {
efree(data->lastkey);
data->lastkey = NULL;
}
2000-10-16 18:35:43 +08:00
2000-10-17 10:50:15 +08:00
ps_files_close(data);
1999-09-04 16:33:31 +08:00
if (!ps_files_valid_key(key) ||
!ps_files_path_create(buf, sizeof(buf), data, key))
return;
1999-06-06 03:52:58 +08:00
data->lastkey = estrdup(key);
1999-08-22 21:43:23 +08:00
#ifdef O_EXCL
2001-08-05 09:43:02 +08:00
data->fd = VCWD_OPEN(buf, O_RDWR | O_BINARY);
if (data->fd == -1 && errno == ENOENT)
2001-08-05 09:43:02 +08:00
data->fd = VCWD_OPEN_MODE(buf, O_EXCL | O_RDWR | O_CREAT | O_BINARY, 0600);
1999-08-22 21:43:23 +08:00
#else
2001-08-05 09:43:02 +08:00
data->fd = VCWD_OPEN_MODE(buf, O_CREAT | O_RDWR | O_BINARY, 0600);
1999-08-22 21:43:23 +08:00
#endif
if (data->fd != -1)
flock(data->fd, LOCK_EX);
2000-03-30 04:37:12 +08:00
if (data->fd == -1)
php_error(E_WARNING, "open(%s, O_RDWR) failed: %s (%d)", buf,
strerror(errno), errno);
1999-06-06 03:52:58 +08:00
}
}
static int ps_files_cleanup_dir(const char *dirname, int maxlifetime)
1999-09-04 16:33:31 +08:00
{
DIR *dir;
char dentry[sizeof(struct dirent) + MAXPATHLEN];
2000-09-11 23:24:28 +08:00
struct dirent *entry = (struct dirent *) &dentry;
1999-09-04 16:33:31 +08:00
struct stat sbuf;
char buf[MAXPATHLEN];
time_t now;
2000-03-30 04:37:12 +08:00
int nrdels = 0;
size_t dirname_len;
2001-08-05 09:43:02 +08:00
TSRMLS_FETCH();
1999-09-04 16:33:31 +08:00
dir = opendir(dirname);
2000-03-30 04:37:12 +08:00
if (!dir) {
php_error(E_NOTICE, "ps_files_cleanup_dir: opendir(%s) failed: %s (%d)\n", dirname, strerror(errno), errno);
2000-03-30 04:37:12 +08:00
return (0);
}
1999-09-04 16:33:31 +08:00
time(&now);
dirname_len = strlen(dirname);
/* Prepare buffer (dirname never changes) */
memcpy(buf, dirname, dirname_len);
buf[dirname_len] = PHP_DIR_SEPARATOR;
while (php_readdir_r(dir, (struct dirent *) dentry, &entry) == 0 && entry) {
1999-09-04 16:33:31 +08:00
/* does the file start with our prefix? */
if (!strncmp(entry->d_name, FILE_PREFIX, sizeof(FILE_PREFIX) - 1)) {
size_t entry_len;
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 &&
(now - sbuf.st_atime) > maxlifetime) {
VCWD_UNLINK(buf);
nrdels++;
}
}
1999-09-04 16:33:31 +08:00
}
}
closedir(dir);
2000-03-30 04:37:12 +08:00
return (nrdels);
1999-09-04 16:33:31 +08:00
}
#define PS_FILES_DATA ps_files *data = PS_GET_MOD_DATA()
PS_OPEN_FUNC(files)
{
ps_files *data;
char *p;
data = ecalloc(sizeof(*data), 1);
PS_SET_MOD_DATA(data);
data->fd = -1;
if ((p = strchr(save_path, ';'))) {
2001-05-13 15:37:28 +08:00
data->dirdepth = (size_t) strtol(save_path, NULL, 10);
1999-09-04 16:33:31 +08:00
save_path = p + 1;
}
data->basedir_len = strlen(save_path);
data->basedir = estrndup(save_path, data->basedir_len);
1999-09-04 16:33:31 +08:00
return SUCCESS;
}
PS_CLOSE_FUNC(files)
{
PS_FILES_DATA;
2000-10-16 18:35:43 +08:00
ps_files_close(data);
2000-02-11 21:41:30 +08:00
if (data->lastkey)
efree(data->lastkey);
1999-09-04 16:33:31 +08:00
efree(data->basedir);
efree(data);
*mod_data = NULL;
return SUCCESS;
}
1999-06-06 03:52:58 +08:00
PS_READ_FUNC(files)
{
long n;
1999-06-06 03:52:58 +08:00
struct stat sbuf;
PS_FILES_DATA;
ps_files_open(data, key);
2000-02-11 21:41:30 +08:00
if (data->fd < 0)
1999-06-06 03:52:58 +08:00
return FAILURE;
2000-02-11 21:41:30 +08:00
if (fstat(data->fd, &sbuf))
1999-06-06 03:52:58 +08:00
return FAILURE;
data->st_size = *vallen = sbuf.st_size;
1999-06-06 03:52:58 +08:00
*val = emalloc(sbuf.st_size);
#ifdef HAVE_PREAD
n = pread(data->fd, *val, sbuf.st_size, 0);
#else
lseek(data->fd, 0, SEEK_SET);
1999-06-06 03:52:58 +08:00
n = read(data->fd, *val, sbuf.st_size);
#endif
2000-02-11 21:41:30 +08:00
if (n != sbuf.st_size) {
1999-06-06 03:52:58 +08:00
efree(*val);
return FAILURE;
}
return SUCCESS;
}
PS_WRITE_FUNC(files)
{
long n;
1999-06-06 03:52:58 +08:00
PS_FILES_DATA;
ps_files_open(data, key);
2000-02-11 21:41:30 +08:00
if (data->fd < 0)
1999-06-06 03:52:58 +08:00
return FAILURE;
/*
* truncate file, if the amount of new data is smaller than
* the existing data set.
*/
2001-06-11 07:26:35 +08:00
if (vallen < (int)data->st_size)
ftruncate(data->fd, 0);
#ifdef HAVE_PWRITE
n = pwrite(data->fd, val, vallen, 0);
#else
1999-06-06 03:52:58 +08:00
lseek(data->fd, 0, SEEK_SET);
n = write(data->fd, val, vallen);
#endif
if (n != vallen) {
php_error(E_WARNING, "write failed: %s (%d)", strerror(errno), errno);
2000-05-15 21:50:39 +08:00
return FAILURE;
}
1999-06-06 03:52:58 +08:00
return SUCCESS;
}
PS_DESTROY_FUNC(files)
1999-06-06 03:52:58 +08:00
{
char buf[MAXPATHLEN];
1999-06-06 03:52:58 +08:00
PS_FILES_DATA;
2001-08-05 09:43:02 +08:00
TSRMLS_FETCH();
1999-06-06 03:52:58 +08:00
if (!ps_files_path_create(buf, sizeof(buf), data, key))
1999-09-04 16:33:31 +08:00
return FAILURE;
2000-10-16 18:35:43 +08:00
ps_files_close(data);
if (VCWD_UNLINK(buf) == -1) {
return FAILURE;
}
1999-06-06 03:52:58 +08:00
return SUCCESS;
}
PS_GC_FUNC(files)
{
PS_FILES_DATA;
/* we don't perform any cleanup, if dirdepth is larger than 0.
we return SUCCESS, since all cleanup should be handled by
an external entity (i.e. find -ctime x | xargs rm) */
2000-02-11 21:41:30 +08:00
if (data->dirdepth == 0)
*nrdels = ps_files_cleanup_dir(data->basedir, maxlifetime);
1999-06-06 03:52:58 +08:00
return SUCCESS;
}
2001-06-06 22:32:27 +08: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 22:32:27 +08:00
*/