cvs: pear /PECL/session_pgsql session_pgsql.c session_pgsql.h

From: Date: Tue, 14 Jan 2003 10:46:35 +0000
Subject: cvs: pear /PECL/session_pgsql session_pgsql.c session_pgsql.h
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-8238@lists.php.net to get a copy of this message
yohgaki Tue Jan 14 05:46:35 2003 EDT Modified files: /pear/PECL/session_pgsql session_pgsql.c session_pgsql.h Log:

Index: pear/PECL/session_pgsql/session_pgsql.c diff -u pear/PECL/session_pgsql/session_pgsql.c:1.28 pear/PECL/session_pgsql/session_pgsql.c:1.29 --- pear/PECL/session_pgsql/session_pgsql.c:1.28 Mon Jan 13 19:45:44 2003 +++ pear/PECL/session_pgsql/session_pgsql.c Tue Jan 14 05:46:35 2003 @@ -6,7 +6,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: session_pgsql.c,v 1.28 2003/01/14 00:45:44 yohgaki Exp $ */ +/* $Id: session_pgsql.c,v 1.29 2003/01/14 10:46:35 yohgaki Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -53,8 +53,21 @@ php_session_pgsql_globals session_pgsql_globals; #endif +static void php_session_pgsql_init_globals(php_session_pgsql_globals *session_pgsql_globals_p TSRMLS_DC); +static int php_pg_pgsql_init_servers(TSRMLS_D); +static int php_ps_pgsql_create_table(int id TSRMLS_DC); +static PGconn *php_ps_pgsql_connect(int id TSRMLS_DC); +static PGconn *php_ps_pgsql_get_db(const char *key TSRMLS_DC); + +static int ps_pgsql_app_read(TSRMLS_D); +static int ps_pgsql_app_write(TSRMLS_D); +static int ps_pgsql_sess_read(const char *key, char **val, int *vallen TSRMLS_DC); +static int ps_pgsql_sess_write(const char *key, const char *val, const int vallen TSRMLS_DC); +static int ps_pgsql_gc(TSRMLS_D); + #define PS_PGSQL_DATA ps_pgsql *data = PS_GET_MOD_DATA() -#define QUERY_BUF_SIZE 250 +#define QUERY_BUF_SIZE 256 +#define BUF_SIZE 512 #if 0 #define ELOG( x ) php_log_err( x TSRMLS_CC) @@ -62,33 +75,34 @@ #define ELOG( x ) #endif -function_entry session_pgsql_functions[] = { - PHP_FE(session_pgsql_status, NULL) - PHP_FE(session_pgsql_info, NULL) - PHP_FE(session_pgsql_add_error, NULL) - PHP_FE(session_pgsql_get_error, NULL) - { NULL, NULL, NULL } -}; - static PHP_INI_MH(OnUpdate_session_pgsql_db) { int i, cnt=0; int len = strlen(new_value); - - PS_PGSQL(connstr)[cnt++] = new_value; + char *tmp; + + tmp = new_value; for (i = 0; i < len; i++) { if (new_value[i] == ';') { if (cnt > MAX_PGSQL_SERVERS) { php_log_err("session pgsql: Too many session database servers. Some servers are ignored." TSRMLS_CC); break; } - new_value[i] = '\0'; - if (i != len) { - PS_PGSQL(connstr)[cnt++] = &new_value[i+1]; - } + PS_PGSQL(connstr)[cnt] = malloc(&new_value[i] - tmp + 1); + memcpy(PS_PGSQL(connstr)[cnt], tmp, &new_value[i] - tmp); + PS_PGSQL(connstr)[cnt][&new_value[i] - tmp] = '\0'; + cnt++; + tmp = &new_value[++i]; } } - PS_PGSQL(servers) = cnt; + if (tmp != &new_value[i]) { + /* should be last server w/o ';' */ + PS_PGSQL(connstr)[cnt] = malloc(&new_value[i] - tmp + 1); + memcpy(PS_PGSQL(connstr)[cnt], tmp, &new_value[i] - tmp); + PS_PGSQL(connstr)[cnt][&new_value[i] - tmp] = '\0'; + cnt++; + } + PS_PGSQL(servers) = cnt; return SUCCESS; } @@ -113,6 +127,20 @@ PHP_RSHUTDOWN_FUNCTION(session_pgsql); PHP_MINFO_FUNCTION(session_pgsql); +/* {{{ session_pgsql_functions[] + */ +function_entry session_pgsql_functions[] = { + PHP_FE(session_pgsql_status, NULL) + PHP_FE(session_pgsql_reset, NULL) + PHP_FE(session_pgsql_info, NULL) + PHP_FE(session_pgsql_set_field, NULL) + PHP_FE(session_pgsql_get_field, NULL) + PHP_FE(session_pgsql_add_error, NULL) + PHP_FE(session_pgsql_get_error, NULL) + {NULL, NULL, NULL} +}; +/* }}} */ + /* {{{ session_pgsql_module_entry */ zend_module_entry session_pgsql_module_entry = { @@ -131,226 +159,11 @@ ZEND_GET_MODULE(session_pgsql) #endif -/* {{{ php_pg_pgsql_create_table - */ -static int php_ps_pgsql_create_table(int id TSRMLS_DC) -{ - PGresult *pg_result; - char *query_create_sess_table = - "CREATE TABLE php_session ( " - "sess_id text, " - "sess_name text, " - "sess_data text, " - "sess_created integer, " - "sess_modified integer, " - "sess_expire integer, " - "sess_addr_created cidr, " - "sess_addr_modified cidr, " - "sess_counter integer, " - "sess_error integer, " - "sess_warning integer, " - "sess_notice integer, " - "sess_err_message text, " - "sess_custom text); "; - - char *query_create_app_vars_table = - "CREATE TABLE php_app_vars ( " - "app_modified integer, " - "app_name text, " - "app_vars text);"; - - int num; - - assert(PS_PGSQL(pgsql_link)[id] != NULL); - pg_result = PQexec(PS_PGSQL(pgsql_link)[id], - "SELECT relname FROM pg_class WHERE relname = 'php_session';"); - if (!pg_result) { - goto cleanup; - } - num = PQntuples(pg_result); - PQclear(pg_result); - if (!num) { - /* No session table */ - pg_result = PQexec(PS_PGSQL(pgsql_link)[id], query_create_sess_table); - if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { - goto cleanup; - } - PQclear(pg_result); - pg_result = PQexec(PS_PGSQL(pgsql_link)[id], - "CREATE INDEX php_session_idx ON php_session USING BTREE (sess_id);"); - PQclear(pg_result); - } - - pg_result = PQexec(PS_PGSQL(pgsql_link)[id], - "SELECT relname FROM pg_class WHERE relname = 'php_app_vars';"); - if (!pg_result) { - goto cleanup; - } - num = PQntuples(pg_result); - PQclear(pg_result); - if (!num) { - pg_result = PQexec(PS_PGSQL(pgsql_link)[id], query_create_app_vars_table); - if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { - goto cleanup; - } - PQclear(pg_result); - } - return SUCCESS; - -cleanup: - PQclear(pg_result); - PQfinish(PS_PGSQL(pgsql_link)[id]); - PS_PGSQL(pgsql_link)[id] = NULL; - return FAILURE; -} -/* }}} */ - -/* {{{ php_ps_pgsql_connect - */ -static PGconn *php_ps_pgsql_connect(int id TSRMLS_DC) -{ - if (!PS_PGSQL(connstr)[id]) { - /* bailout while debugging. PS_PGSQL(connstr)[id] can be null when - session_pgsql is compiled in and user tried to load session_pgsql - as a external module */ - assert(PS_PGSQL(connstr)[id] != NULL); - return NULL; - } - if ((PQstatus(PS_PGSQL(pgsql_link)[id])) == CONNECTION_BAD) { - PQfinish(PS_PGSQL(pgsql_link)[id]); - PS_PGSQL(pgsql_link)[id] = NULL; - php_error(E_WARNING, "session pgsql: PostgreSQL server connection is broken or bad connection string (%s)", PS_PGSQL(connstr)[id]); - return NULL; - } - return PS_PGSQL(pgsql_link)[id]; -} -/* }}} */ - -/* {{{ php_ps_pgsql_init_servers - */ -static int php_pg_pgsql_init_servers(TSRMLS_D) -{ - int id; - for (id = 0; id < PS_PGSQL(servers); id++) { - /* if link is bad, NULL is returned */ - assert(PS_PGSQL(connstr)[id]); - PS_PGSQL(pgsql_link)[id] = PQconnectdb(PS_PGSQL(connstr)[id]); - if (PS_PGSQL(pgsql_link)[id] && PS_PGSQL(create_table)) { - /* if there is problem, link is set to NULL */ - if (php_ps_pgsql_create_table(id TSRMLS_CC) == FAILURE) { - php_error(E_NOTICE, "session pgsql: Cannot create tables (%s)", PS_PGSQL(connstr)[id]); - } - } - } - for (id = 0; id < PS_PGSQL(servers); id++) { - if (PS_PGSQL(pgsql_link)[id]) { - /* there is at least one server that is usable */ - return SUCCESS; - } - } - return FAILURE; -} -/* }}} */ - -/* {{{ php_ps_pgsql_get_db - */ -static PGconn *php_ps_pgsql_get_db(const char *key TSRMLS_DC) -{ - PGconn *db = NULL; - - if (PS_PGSQL(servers) == 1) { - db = php_ps_pgsql_connect(0); - PS_PGSQL(current_id) = 0; - } - else { - /* take care load balance and failover */ - div_t tmp; - int i, sum = 0; - int len, id = 0; - /* don't distribute session if failover_mode is enabled */ - if (!PS_PGSQL(failover_mode)) { - len = strlen(key); - for(i = 0; i < len; i++) { - sum += (int)key[i]; - } - tmp = div(sum, PS_PGSQL(servers)); - id = tmp.rem; - db = php_ps_pgsql_connect(id); - PS_PGSQL(current_id) = id; - } - if (!db) { - /* if something wrong. use 1st server that is usable */ - for (id = 0; id < PS_PGSQL(servers); id++) { - if (PS_PGSQL(pgsql_link)[id]) { - /* if link is bad, NULL is returned */ - db = php_ps_pgsql_connect(id); - if (db) { - PS_PGSQL(current_id) = id; - break; - } - } - } - } - } - return db; -} -/* }}} */ - -/* {{{ php_pg_pgsql_gc - */ -static int php_ps_pgsql_gc(TSRMLS_D) -{ - PGresult *pg_result; - char query[QUERY_BUF_SIZE+1]; - char *query_gc = "DELETE FROM php_session WHERE sess_expire < %d;"; - char *query_vacuum = "VACUUM ANALYZE php_session; VACUUM ANALYZE php_app_vars; REINDEX TABLE php_session;"; - int id; - - ELOG("GC Called"); - sprintf(query, query_gc, time(NULL)); - if (*(ps_pgsql_instance->last_gc) && - *(ps_pgsql_instance->last_gc) < time(NULL) - PS_PGSQL(gc_interval)) { - *(ps_pgsql_instance->last_gc) = time(NULL); - for (id = 0; id < PS_PGSQL(servers); id++) { - if (PS_PGSQL(pgsql_link)[id]) { - pg_result = PQexec(PS_PGSQL(pgsql_link)[id], query); - PQclear(pg_result); - } - } - } - if (*(ps_pgsql_instance->last_vacuum) && - *(ps_pgsql_instance->last_vacuum) < time(NULL) - PS_PGSQL(vacuum_interval)) { - *(ps_pgsql_instance->last_vacuum) = time(NULL); - for (id = 0; id < PS_PGSQL(servers); id++) { - if (PS_PGSQL(pgsql_link)[id]) { - pg_result = PQexec(PS_PGSQL(pgsql_link)[id], query_vacuum); - PQclear(pg_result); - } - } - } - return SUCCESS; -} -/* }}} */ - -/* {{{ php_session_pgsql_init_globals - */ -static void php_session_pgsql_init_globals(php_session_pgsql_globals *session_pgsql_globals_p TSRMLS_DC) -{ - int i; - for (i = 0; i < MAX_PGSQL_SERVERS; i++) { - PS_PGSQL(pgsql_link)[i] = NULL; - PS_PGSQL(connstr)[i] = NULL; - } - PS_PGSQL(sess_error_message) = NULL; -} -/* }}} */ - /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(session_pgsql) { smart_str buf={0}; - #ifdef ZTS php_session_pgsql_globals *session_pgsql_globals; ts_allocate_id(&session_pgsql_globals_id, sizeof(php_session_pgsql_globals), php_session_pgsql_init_globals, NULL); @@ -363,23 +176,23 @@ REGISTER_INI_ENTRIES(); - ps_pgsql_instance = calloc(sizeof(*ps_pgsql_instance), 1); - if (!ps_pgsql_instance) { - return FAILURE; - } - /* if sapi is bianry sapi(CGI/CLI) disable session pgsql. session pgsql rely on shared memory to work. it's problematic using session pgsql with PHP bianry */ if (PS_PGSQL(disable) || !strcmp(sapi_module.name, "cli") || !strcmp(sapi_module.name, "cgi") || !strcmp(sapi_module.name, "cgi-fcgi")) { - if (!PS_PGSQL(disable)) { + if (!PS_PGSQL(disable) && strcmp(sapi_module.name, "cli")) { php_log_err("session pgsql: Disabled. It will not work with CLI or CGI and. Set session_pgsql.disable in php.ini to remove this error message." TSRMLS_CC); } PS_PGSQL(disable) = 1; return SUCCESS; /* Don't spit annoying error messages */ } + ps_pgsql_instance = calloc(sizeof(*ps_pgsql_instance), 1); + if (!ps_pgsql_instance) { + return FAILURE; + } + /* create shared memory file using sapi_name */ if (PS_PGSQL(sem_file_name) && PS_PGSQL(sem_file_name)[0]) { smart_str_appends(&buf,PS_PGSQL(sem_file_name)); @@ -434,65 +247,263 @@ { int i; - /* link is closed at shutdown */ ELOG("MSHUTDOWN Called"); + /* link is closed at shutdown + These values will be initilized */ + for (i = 0; i < PS_PGSQL(servers); i++) { + if (PS_PGSQL(pgsql_link)[i]) { + PQfinish(PS_PGSQL(pgsql_link)[i]); + } + if (PS_PGSQL(connstr)[i]) { + free(PS_PGSQL(connstr)[i]); + } + } if (PS_PGSQL(disable)) { return SUCCESS; } - - for (i = 0; i < PS_PGSQL(servers); i++) { - PQfinish(PS_PGSQL(pgsql_link)[i]); + /* clean up mm */ + if (ps_pgsql_instance->owner == getpid() && ps_pgsql_instance->mm) { + if (ps_pgsql_instance->last_gc) { + mm_free(ps_pgsql_instance->mm, ps_pgsql_instance->last_gc); + } + if (ps_pgsql_instance->last_vacuum) { + mm_free(ps_pgsql_instance->mm, ps_pgsql_instance->last_vacuum); + } + mm_destroy(ps_pgsql_instance->mm); + free(ps_pgsql_instance); + } + return SUCCESS; +} +/* }}} */ + + +/* {{{ PHP_RSHUTDOEN_FUNCTION + */ +PHP_RINIT_FUNCTION(session_pgsql) +{ + ELOG("RINIT Called"); + /* These clean up cannot be done at rshutdown, since it executed + before session write */ + if (PS_PGSQL(sess_custom)) { + /* looks like emalloced memory is freed to early */ + free(PS_PGSQL(sess_custom)); + PS_PGSQL(sess_custom) = NULL; + } + if (PS_PGSQL(sess_error_message)) { + free(PS_PGSQL(sess_error_message)); + PS_PGSQL(sess_error_message) = NULL; + } + return SUCCESS; +} +/* }}} */ + + +/* {{{ PHP_RSHUTDOEN_FUNCTION + */ +PHP_RSHUTDOWN_FUNCTION(session_pgsql) +{ + ELOG("RSHUTDOWN Called"); + /* NOTE: RSHUTDOWN is called *before* writing/closing session... */ + if (PS_PGSQL(disable)) { + return SUCCESS; + } + return ps_pgsql_gc(TSRMLS_C); +} +/* }}} */ + +/* {{{ PHP_MINFO_FUNCTION + */ +PHP_MINFO_FUNCTION(session_pgsql) +{ + php_info_print_table_start(); + php_info_print_table_header(2, "PostgreSQL Session Save Handler Support", "enabled"); + php_info_print_table_end(); + + DISPLAY_INI_ENTRIES(); +} +/* }}} */ + +/* {{{ php_session_pgsql_init_globals + */ +static void php_session_pgsql_init_globals(php_session_pgsql_globals *session_pgsql_globals_p TSRMLS_DC) +{ + int i; + for (i = 0; i < MAX_PGSQL_SERVERS; i++) { + PS_PGSQL(pgsql_link)[i] = NULL; + PS_PGSQL(connstr)[i] = NULL; + } + PS_PGSQL(sess_custom) = NULL; + PS_PGSQL(sess_error_message) = NULL; +} +/* }}} */ + +/* {{{ php_ps_pgsql_init_servers + */ +static int php_pg_pgsql_init_servers(TSRMLS_D) +{ + int id; + for (id = 0; id < PS_PGSQL(servers); id++) { + assert(PS_PGSQL(connstr)[id]); + /* if link is bad, NULL is returned */ + PS_PGSQL(pgsql_link)[id] = PQconnectdb(PS_PGSQL(connstr)[id]); + if (PS_PGSQL(pgsql_link)[id] && PS_PGSQL(create_table)) { + /* if there is problem, link is set to NULL */ + if (php_ps_pgsql_create_table(id TSRMLS_CC) == FAILURE) { + php_error(E_NOTICE, "session pgsql: Cannot create tables (%s)", PS_PGSQL(connstr)[id]); + } + } + } + for (id = 0; id < PS_PGSQL(servers); id++) { + if (PS_PGSQL(pgsql_link)[id]) { + /* there is at least one server that is usable */ + return SUCCESS; + } + } + return FAILURE; +} +/* }}} */ + +/* {{{ php_pg_pgsql_create_table + */ +static int php_ps_pgsql_create_table(int id TSRMLS_DC) +{ + PGresult *pg_result; + char *query_create_sess_table = + "CREATE TABLE php_session ( " + "sess_id text, " + "sess_name text, " + "sess_data text, " + "sess_created integer, " + "sess_modified integer, " + "sess_expire integer, " + "sess_addr_created cidr, " + "sess_addr_modified cidr, " + "sess_counter integer, " + "sess_error integer, " + "sess_warning integer, " + "sess_notice integer, " + "sess_err_message text, " + "sess_custom text); "; + + char *query_create_app_vars_table = + "CREATE TABLE php_app_vars ( " + "app_modified integer, " + "app_name text, " + "app_vars text);"; + + int num; + + assert(PS_PGSQL(pgsql_link)[id] != NULL); + pg_result = PQexec(PS_PGSQL(pgsql_link)[id], + "SELECT relname FROM pg_class WHERE relname = 'php_session';"); + if (!pg_result) { + goto cleanup; } - if (ps_pgsql_instance->owner == getpid() && ps_pgsql_instance->mm) { - if (ps_pgsql_instance->last_gc) { - mm_free(ps_pgsql_instance->mm, ps_pgsql_instance->last_gc); + num = PQntuples(pg_result); + PQclear(pg_result); + if (!num) { + /* No session table */ + pg_result = PQexec(PS_PGSQL(pgsql_link)[id], query_create_sess_table); + if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { + goto cleanup; } - if (ps_pgsql_instance->last_vacuum) { - mm_free(ps_pgsql_instance->mm, ps_pgsql_instance->last_vacuum); + PQclear(pg_result); + pg_result = PQexec(PS_PGSQL(pgsql_link)[id], + "CREATE INDEX php_session_idx ON php_session USING BTREE (sess_id);"); + PQclear(pg_result); + } + + pg_result = PQexec(PS_PGSQL(pgsql_link)[id], + "SELECT relname FROM pg_class WHERE relname = 'php_app_vars';"); + if (!pg_result) { + goto cleanup; + } + num = PQntuples(pg_result); + PQclear(pg_result); + if (!num) { + pg_result = PQexec(PS_PGSQL(pgsql_link)[id], query_create_app_vars_table); + if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { + goto cleanup; } - mm_destroy(ps_pgsql_instance->mm); - free(ps_pgsql_instance); + PQclear(pg_result); } - return SUCCESS; -} -/* }}} */ - -/* {{{ PHP_RSHUTDOEN_FUNCTION - */ -PHP_RINIT_FUNCTION(session_pgsql) -{ - PS_PGSQL(sess_custom) = empty_string; - return SUCCESS; +cleanup: + if (pg_result) + PQclear(pg_result); + PQfinish(PS_PGSQL(pgsql_link)[id]); + PS_PGSQL(pgsql_link)[id] = NULL; + return FAILURE; } /* }}} */ - -/* {{{ PHP_RSHUTDOEN_FUNCTION +/* {{{ php_ps_pgsql_connect */ -PHP_RSHUTDOWN_FUNCTION(session_pgsql) +static PGconn *php_ps_pgsql_connect(int id TSRMLS_DC) { - ELOG("RSHUTDOWN Called"); - if (PS_PGSQL(disable)) { - return SUCCESS; + if (!PS_PGSQL(connstr)[id]) { + /* bailout while debugging. PS_PGSQL(connstr)[id] can be null when + session_pgsql is compiled in and user tried to load session_pgsql + as a external module */ + assert(PS_PGSQL(connstr)[id] != NULL); + return NULL; } - if (PS_PGSQL(sess_custom)) { - efree(PS_PGSQL(sess_custom)); + if ((PQstatus(PS_PGSQL(pgsql_link)[id])) == CONNECTION_BAD) { + PQreset(PS_PGSQL(pgsql_link)[id]); /* reset connection. database server may be rebooted */ + if (PQstatus(PS_PGSQL(pgsql_link)[id]) == CONNECTION_BAD) { + /* seems it's really dead */ + PQfinish(PS_PGSQL(pgsql_link)[id]); + PS_PGSQL(pgsql_link)[id] = NULL; + php_error(E_WARNING, "session pgsql: PostgreSQL server connection is broken or bad connection string (%s)", PS_PGSQL(connstr)[id]); + return NULL; + } } - return php_ps_pgsql_gc(TSRMLS_C); + return PS_PGSQL(pgsql_link)[id]; } /* }}} */ -/* {{{ PHP_MINFO_FUNCTION +/* {{{ php_ps_pgsql_get_db */ -PHP_MINFO_FUNCTION(session_pgsql) +static PGconn *php_ps_pgsql_get_db(const char *key TSRMLS_DC) { - php_info_print_table_start(); - php_info_print_table_header(2, "PostgreSQL Session Save Handler Support", "enabled"); - php_info_print_table_end(); - - DISPLAY_INI_ENTRIES(); + PGconn *db = NULL; + div_t tmp; + int i, sum = 0; + int len, id = 0; + + if (PS_PGSQL(servers) == 1) { + db = php_ps_pgsql_connect(0); + PS_PGSQL(current_id) = 0; + return db; + } + /* take care load balance and failover */ + /* don't distribute session if failover_mode is enabled */ + if (!PS_PGSQL(failover_mode)) { + len = strlen(key); + for(i = 0; i < len; i++) { + sum += (int)key[i]; + } + tmp = div(sum, PS_PGSQL(servers)); + id = tmp.rem; + db = php_ps_pgsql_connect(id); + PS_PGSQL(current_id) = id; + } + if (!db) { + /* if something wrong. use 1st server that is usable */ + PS_PGSQL(current_id) = -1; + for (id = 0; id < PS_PGSQL(servers); id++) { + if (PS_PGSQL(pgsql_link)[id]) { + /* if link is bad, NULL is returned */ + db = php_ps_pgsql_connect(id); + if (db) { + PS_PGSQL(current_id) = id; + break; + } + } + } + } + return db; } /* }}} */ @@ -523,11 +534,11 @@ } /* }}} */ - /* {{{ ps_pgsql_app_read */ -static int ps_pgsql_app_read(PGconn *pg_link TSRMLS_DC) +static int ps_pgsql_app_read(TSRMLS_D) { + PGconn *pg_link = PS_PGSQL(current_db); PGresult *pg_result; char query[QUERY_BUF_SIZE+1]; int ret = SUCCESS; @@ -569,8 +580,9 @@ /* {{{ ps_pgsql_app_write */ -static int ps_pgsql_app_write(PGconn *pg_link TSRMLS_DC) +static int ps_pgsql_app_write(TSRMLS_DC) { + PGconn *pg_link = PS_PGSQL(current_db); PGresult *pg_result; char *query_insert = "INSERT INTO php_app_vars (app_modified, app_name, app_vars) VALUES (%d, '%s', '%s');"; char *query_update = "UPDATE php_app_vars SET app_modified = %d, app_vars = '%s'"; @@ -615,60 +627,16 @@ } /* }}} */ -/*********** session save handler functions ************/ -/* {{{ PS_OPEN_FUNC - */ -PS_OPEN_FUNC(pgsql) -{ - ELOG("OPEN CALLED"); - *mod_data = (void *)1; /* mod_data cannot be NULL to make session save handler module work */ - - return SUCCESS; -} -/* }}} */ - - -/* {{{ PS_CLOSE_FUNC - */ -PS_CLOSE_FUNC(pgsql) -{ - ELOG("CLOSE Called"); - *mod_data = (void *)0; /* mod_data should be set to NULL to avoid additional close call */ - - return SUCCESS; -} -/* }}} */ - - -/* {{{ PS_READ_FUNC +/* {{{ ps_pgsql_sess_read */ -PS_READ_FUNC(pgsql) +static int ps_pgsql_sess_read(const char *key, char **val, int *vallen TSRMLS_DC) { PGresult *pg_result; - ExecStatusType pg_status; +/* ExecStatusType pg_status; */ char query[QUERY_BUF_SIZE+1]; char *query_tpl = "SELECT sess_expire, sess_counter, sess_error, sess_warning, sess_notice, sess_data, sess_custom FROM php_session WHERE sess_id = '%s';"; int ret = FAILURE; - ELOG("READ Called"); - - PS_PGSQL(current_db) = php_ps_pgsql_get_db(key TSRMLS_CC); - if (PS_PGSQL(current_db) == NULL) { - return FAILURE; - } - - /* clean up & check last write */ - while ((pg_result = PQgetResult(PS_PGSQL(current_db)))) { - pg_status = PQresultStatus(pg_result); - switch(pg_status) { - case PGRES_COMMAND_OK: - case PGRES_TUPLES_OK: - break; - default: - php_log_err("session pgsql: There is an error during last session write.\n" TSRMLS_CC); - } - } - /* start reading */ if (PS_PGSQL(serializable)) { pg_result = PQexec(PS_PGSQL(current_db), "BEGIN; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;"); @@ -705,6 +673,7 @@ PS_PGSQL(sess_del) = 1; } else { + char *tmp; /* update counter */ PS_PGSQL(sess_cnt) = (int)atoi(PQgetvalue(pg_result, 0, 1)); PS_PGSQL(sess_cnt)++; @@ -713,45 +682,38 @@ PS_PGSQL(sess_warning) = (int)atoi(PQgetvalue(pg_result, 0, 3)); PS_PGSQL(sess_notice) = (int)atoi(PQgetvalue(pg_result, 0, 4)); /* session data - PQgetvalue reuturns "" for NULL */ - *val = PQgetvalue(pg_result, 0, 5); - if (*val) { - *vallen = strlen(*val); - *val = safe_estrndup(*val, *vallen); - } + tmp = PQgetvalue(pg_result, 0, 5); + *vallen = strlen(tmp); + *val = estrndup(tmp, *vallen); /* custom field */ - PS_PGSQL(sess_custom) = safe_estrdup(PQgetvalue(pg_result, 0, 5)); + tmp = PQgetvalue(pg_result, 0, 6); + PS_PGSQL(sess_custom) = strdup(tmp); } - ret = SUCCESS; } + ret = SUCCESS; } else { - /* something wrong, but try to insert data anyway */ + /* something wrong, but try to delete and insert data anyway */ PS_PGSQL(sess_new) = 1; + PS_PGSQL(sess_del) = 1; + ret = SUCCESS; } PQclear(pg_result); } else { php_error(E_NOTICE,"session pgsql: Invalid Session ID detected"); } - if (*vallen == 0) { *val = safe_estrndup("",0); } - if (PS_PGSQL(use_app_vars)) { - /* Init app vars */ - ps_pgsql_app_read(PS_PGSQL(current_db) TSRMLS_CC); - } - - return ret; + return ret; } /* }}} */ -#define PS_PGSQL_CHECK_APP_MODIFIED() 1 - -/* {{{ PS_WRITE_FUNC +/* {{{ ps_pgsql_sess_write */ -PS_WRITE_FUNC(pgsql) +static int ps_pgsql_sess_write(const char *key, const char *val, const int vallen TSRMLS_DC) { PGresult *pg_result; size_t query_len; @@ -767,13 +729,8 @@ "WHERE sess_id = '%s';"; char *escaped_val, *escaped_custom; smart_str buf= {0}; - int len, custom_len; - - ELOG("WRITE Called"); + int val_len, custom_len; - if (!PS_PGSQL(current_db)) { - return FAILURE; - } if (PS_PGSQL(sess_del)) { query_len = strlen(query_delete)+strlen(key); query = emalloc(query_len+1); @@ -786,7 +743,7 @@ now = time(NULL); exp = now + PS(gc_maxlifetime); escaped_val = (char *)emalloc(vallen*2+1); - len = PQescapeString(escaped_val, val, strlen(val)); + val_len = PQescapeString(escaped_val, val, vallen); if (PS_PGSQL(sess_new)) { /* INSERT */ if (PS_PGSQL(sess_custom) && PS_PGSQL(sess_custom)[0]) { @@ -794,7 +751,7 @@ custom_len = strlen(PS_PGSQL(sess_custom)); escaped_custom = (char *)emalloc(custom_len*2+1); custom_len = PQescapeString(escaped_custom, PS_PGSQL(sess_custom), custom_len); - smart_str_appendl(&buf, escaped_custom, len); + smart_str_appendl(&buf, escaped_custom, custom_len); smart_str_appends(&buf, "'"); smart_str_0(&buf); efree(escaped_custom); @@ -804,7 +761,7 @@ smart_str_0(&buf); custom_len = 0; } - query_len = strlen(query_insert)+len+strlen(key)+custom_len+128; + query_len = strlen(query_insert)+val_len+strlen(key)+custom_len+buf.len+128; query = emalloc(query_len+1); snprintf(query, query_len, query_insert, key, now, now, exp, escaped_val, buf.c); pg_result = PQexec(PS_PGSQL(current_db), query); @@ -816,7 +773,7 @@ custom_len = strlen(PS_PGSQL(sess_custom)); escaped_custom = (char *)emalloc(custom_len*2+1); custom_len = PQescapeString(escaped_custom, PS_PGSQL(sess_custom), custom_len); - smart_str_appendl(&buf, escaped_custom, len); + smart_str_appendl(&buf, escaped_custom, custom_len); smart_str_appends(&buf, "'"); smart_str_0(&buf); efree(escaped_custom); @@ -826,7 +783,7 @@ smart_str_0(&buf); custom_len = 0; } - query_len = strlen(query_update)+strlen(key)+len+custom_len+128; + query_len = strlen(query_update)+strlen(key)+val_len+custom_len+buf.len+128; query = emalloc(query_len+1); snprintf(query, query_len, query_update, escaped_val, now, exp, PS_PGSQL(sess_cnt), PS_PGSQL(sess_error), PS_PGSQL(sess_warning), PS_PGSQL(sess_notice), buf.c, key); pg_result = PQexec(PS_PGSQL(current_db), query); @@ -836,12 +793,6 @@ efree(query); efree(escaped_val); - /* Save Application Variables */ - if (PS_PGSQL(use_app_vars)) { - if (PS_PGSQL_CHECK_APP_MODIFIED()) { - ps_pgsql_app_write(PS_PGSQL(current_db) TSRMLS_CC); - } - } /* save error message is any */ if (PS_PGSQL(sess_error_message)) { char *escaped; @@ -861,15 +812,118 @@ PQclear(pg_result); smart_str_free(&buf); efree(escaped); - efree(PS_PGSQL(sess_error_message)); } + pg_result = PQexec(PS_PGSQL(current_db), "END;"); + if (PQresultStatus(pg_result) != PGRES_COMMAND_OK) { + PQclear(pg_result); + return FAILURE; + } + PQclear(pg_result); - PQexec(PS_PGSQL(current_db), "END;"); - return SUCCESS; } /* }}} */ +/* {{{ pg_pgsql_gc + */ +static int ps_pgsql_gc(TSRMLS_D) +{ + PGresult *pg_result; + char query[QUERY_BUF_SIZE+1]; + char *query_gc = "DELETE FROM php_session WHERE sess_expire < %d;"; + char *query_vacuum = "VACUUM ANALYZE php_session; VACUUM ANALYZE php_app_vars; REINDEX TABLE php_session;"; + int id; + + ELOG("GC Called"); + sprintf(query, query_gc, time(NULL)); + if (*(ps_pgsql_instance->last_gc) && + *(ps_pgsql_instance->last_gc) < time(NULL) - PS_PGSQL(gc_interval)) { + *(ps_pgsql_instance->last_gc) = time(NULL); + for (id = 0; id < PS_PGSQL(servers); id++) { + if (PS_PGSQL(pgsql_link)[id]) { + pg_result = PQexec(PS_PGSQL(pgsql_link)[id], query); + PQclear(pg_result); + } + } + } + if (*(ps_pgsql_instance->last_vacuum) && + *(ps_pgsql_instance->last_vacuum) < time(NULL) - PS_PGSQL(vacuum_interval)) { + *(ps_pgsql_instance->last_vacuum) = time(NULL); + for (id = 0; id < PS_PGSQL(servers); id++) { + if (PS_PGSQL(pgsql_link)[id]) { + pg_result = PQexec(PS_PGSQL(pgsql_link)[id], query_vacuum); + PQclear(pg_result); + } + } + } + return SUCCESS; +} +/* }}} */ + + +/*********** session save handler functions ************/ +/* {{{ PS_OPEN_FUNC + */ +PS_OPEN_FUNC(pgsql) +{ + ELOG("OPEN CALLED"); + *mod_data = (void *)1; /* mod_data cannot be NULL to make session save handler module work */ + + return SUCCESS; +} +/* }}} */ + + +/* {{{ PS_CLOSE_FUNC + */ +PS_CLOSE_FUNC(pgsql) +{ + ELOG("CLOSE Called"); + *mod_data = (void *)0; /* mod_data should be set to NULL to avoid additional close call */ + + return SUCCESS; +} +/* }}} */ + + +/* {{{ PS_READ_FUNC + */ +PS_READ_FUNC(pgsql) +{ + int ret; + ELOG("READ Called"); + PS_PGSQL(current_db) = php_ps_pgsql_get_db(key TSRMLS_CC); + if (PS_PGSQL(current_db) == NULL) { + return FAILURE; + } + ret = ps_pgsql_sess_read(key, val, vallen TSRMLS_CC); + if (ret != FAILURE && PS_PGSQL(use_app_vars)) { + /* Init app vars */ + ret = ps_pgsql_app_read(TSRMLS_C); + } + return ret; +} +/* }}} */ + +#define PS_PGSQL_CHECK_APP_MODIFIED() 1 + +/* {{{ PS_WRITE_FUNC + */ +PS_WRITE_FUNC(pgsql) +{ + int ret; + ELOG("WRITE Called"); + if (!PS_PGSQL(current_db)) { + return FAILURE; + } + ret = ps_pgsql_sess_write(key, val, vallen TSRMLS_CC); + if (ret != FAILURE && PS_PGSQL(use_app_vars)) { + ret = ps_pgsql_app_write(TSRMLS_C); + } + return ret; +} +/* }}} */ + /* {{{ PS_DESTROY_FUNC */ PS_DESTROY_FUNC(pgsql) @@ -917,19 +971,19 @@ PHP_FUNCTION(session_pgsql_status) { int i; - char buf[512]; + char buf[BUF_SIZE]; if (ZEND_NUM_ARGS()) { WRONG_PARAM_COUNT; } array_init(return_value); - add_assoc_long(return_value, "number of servers", PS_PGSQL(servers)); - add_assoc_long(return_value, "failover mode", PS_PGSQL(failover_mode)); + add_assoc_long(return_value, "Number of Servers", PS_PGSQL(servers)); + add_assoc_long(return_value, "Failover Mode", PS_PGSQL(failover_mode)); for (i = 0; i < PS_PGSQL(servers); i++) { - snprintf(buf, 512, "connection string %d", i); + snprintf(buf, BUF_SIZE, "Connection String #%d", i); add_assoc_string(return_value, buf, PS_PGSQL(connstr)[i], 1); - snprintf(buf, 512, "server status %d", i); + snprintf(buf, BUF_SIZE, "Server Status #%d", i); if (PS_PGSQL(pgsql_link)[i]) { add_assoc_long(return_value, buf, 1); } @@ -960,14 +1014,19 @@ } array_init(return_value); - add_assoc_string(return_value, "session id", PS(id), 1); - add_assoc_long(return_value, "server id", PS_PGSQL(current_id)); - add_assoc_string(return_value, "connection", PS_PGSQL(connstr)[PS_PGSQL(current_id)], 1); - add_assoc_long(return_value, "accesses", PS_PGSQL(sess_cnt)); - add_assoc_long(return_value, "errors", PS_PGSQL(sess_error)); - add_assoc_long(return_value, "warnings", PS_PGSQL(sess_warning)); - add_assoc_long(return_value, "notices", PS_PGSQL(sess_notice)); - add_assoc_string(return_value, "custom", PS_PGSQL(sess_custom), 1); + add_assoc_string(return_value, "Session ID", PS(id), 1); + add_assoc_long(return_value, "Server ID", PS_PGSQL(current_id)); + add_assoc_string(return_value, "Connection", PS_PGSQL(connstr)[PS_PGSQL(current_id)], 1); + add_assoc_long(return_value, "Accesses", PS_PGSQL(sess_cnt)); + add_assoc_long(return_value, "Errors", PS_PGSQL(sess_error)); + add_assoc_long(return_value, "Warnings", PS_PGSQL(sess_warning)); + add_assoc_long(return_value, "Notices", PS_PGSQL(sess_notice)); + if (PS_PGSQL(sess_custom)) { + add_assoc_string(return_value, "Custom", PS_PGSQL(sess_custom), 1); + } + else { + add_assoc_string(return_value, "Custom", empty_string, 0); + } } /* }}} */ @@ -976,14 +1035,19 @@ PHP_FUNCTION(session_pgsql_set_field) { char *field = NULL; + long len; int argc = ZEND_NUM_ARGS(); - if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, "s", - &field) == FAILURE) { + if (zend_parse_parameters(argc TSRMLS_CC, "s", &field, &len) == FAILURE) { RETURN_FALSE; } - PS_PGSQL(sess_custom) = safe_estrdup(field); + if (PS_PGSQL(sess_custom)) { + free(PS_PGSQL(sess_custom)); + } + PS_PGSQL(sess_custom) = (char *)malloc(len+1); + memcpy(PS_PGSQL(sess_custom), field, len); + PS_PGSQL(sess_custom)[len] = '\0'; RETURN_TRUE; } /* }}} */ @@ -995,8 +1059,10 @@ if (ZEND_NUM_ARGS()) { WRONG_PARAM_COUNT; } - - RETURN_STRING(PS_PGSQL(sess_custom), 1); + if (PS_PGSQL(sess_custom) && PS_PGSQL(sess_custom)[0]) { + RETURN_STRING(PS_PGSQL(sess_custom), 1); + } + RETURN_STRING(empty_string, 0); } /* }}} */ @@ -1004,12 +1070,11 @@ Increments error counts and sets last error message */ PHP_FUNCTION(session_pgsql_add_error) { - zval *error_message, *session_id; + char *error_message; + long error_level, len; int argc = ZEND_NUM_ARGS(); - long error_level; - if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, "l|s", - &error_level, &error_message, &session_id) == FAILURE) { + if (zend_parse_parameters(argc TSRMLS_CC, "l|s", &error_level, &error_message, &len) == FAILURE) { RETURN_FALSE; } @@ -1031,7 +1096,12 @@ RETURN_FALSE; } if (argc > 1) { - PS_PGSQL(sess_error_message) = estrdup(Z_STRVAL_P(error_message)); + if (PS_PGSQL(sess_error_message)) { + free(PS_PGSQL(sess_error_message)); + } + PS_PGSQL(sess_error_message) = malloc(len+1); + memcpy(PS_PGSQL(sess_error_message), error_message, len); + PS_PGSQL(sess_error_message)[len] = '\0'; } RETURN_TRUE; } @@ -1043,15 +1113,14 @@ { int with_error_message = 0; - if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "|b", - &with_error_message) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &with_error_message) == FAILURE) { RETURN_FALSE; } array_init(return_value); - add_assoc_long(return_value, "errors", PS_PGSQL(sess_error)); - add_assoc_long(return_value, "warnings", PS_PGSQL(sess_warning)); - add_assoc_long(return_value, "notices", PS_PGSQL(sess_notice)); + add_assoc_long(return_value, "Errors", PS_PGSQL(sess_error)); + add_assoc_long(return_value, "Warnings", PS_PGSQL(sess_warning)); + add_assoc_long(return_value, "Notices", PS_PGSQL(sess_notice)); if (with_error_message) { PGresult *pg_result; @@ -1059,6 +1128,10 @@ char *escaped; int len; + if (PS_PGSQL(sess_error_message)) { + add_assoc_string(return_value, "Error Message", PS_PGSQL(sess_error_message), 1); + return; + } len = strlen(PS(id)); escaped = (char *)emalloc(len*2+1); len = PQescapeString(escaped, PS(id), len); @@ -1070,12 +1143,15 @@ smart_str_free(&buf); efree(escaped); if (PQntuples(pg_result) == 1) { - add_assoc_string(return_value, "error message", PQgetvalue(pg_result, 0, 0), 1); + char *tmp; + tmp = safe_estrdup(PQgetvalue(pg_result, 0, 0)); + add_assoc_string(return_value, "Error Message", tmp, 0); } else { /* new session */ - add_assoc_string(return_value, "error message", empty_string, 0); + add_assoc_string(return_value, "Error Message", empty_string, 0); } + PQclear(pg_result); } } /* }}} */ Index: pear/PECL/session_pgsql/session_pgsql.h diff -u pear/PECL/session_pgsql/session_pgsql.h:1.16 pear/PECL/session_pgsql/session_pgsql.h:1.17 --- pear/PECL/session_pgsql/session_pgsql.h:1.16 Mon Jan 13 19:45:44 2003 +++ pear/PECL/session_pgsql/session_pgsql.h Tue Jan 14 05:46:35 2003 @@ -45,7 +45,7 @@ PGconn *pgsql_link[MAX_PGSQL_SERVERS]; PGconn *current_db; int current_id; - char *connstr[MAX_PGSQL_SERVERS]; + char *connstr[MAX_PGSQL_SERVERS]; /* malloc/free should be used */ int servers; /* better to use prim number. i.e. 2,3,5,7,11... session db servers */ int sess_new; int sess_del; @@ -53,9 +53,9 @@ int sess_error; int sess_warning; int sess_notice; - char *sess_custom; + char *sess_custom; /* malloc/free should be used */ + char *sess_error_message; /* malloc/free should be used */ int maxlifetime; - char *sess_error_message; int app_modified; int app_new;
« previous php.pear.cvs (#8238) next »