global variable and pconnect trouble in extension
| From: | Chesneau Serge | Date: | Fri, 23 Jun 2000 12:09:21 +0000 |
| Subject: | global variable and pconnect trouble in extension | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-22051@lists.php.net to get a copy of this message | ||
Hi,
Some trouble with global.
I wrote a test module called ats. In this module, i need a mechanism
like persistent database connexion witch use a global variable
(num_persistent) that count the number of new permanent connexion. It
seem that this counter (num_persistent) is not refreshing (or storing)
correctly. My extension has two fonction. One for register user in the
hash list (ats_register), the second for showing num_persistent and the
hash content. When i call two php script (ats2.php and ats3.php) with
two browser window and refreshing (5/6 time), the result is strange.
I think that a correctly write my module but, if the global variable
(num_persistent) can't correctly be set, i think that allocation storing
in the hash table can be wrong, ... but i'am a newbie.
This module was written based on the sybase ext model, i try the write
another based on the ext_skel script, the result is the same.
I have tried to use php4 (official release) and a recent set a source
(20000619) on a linux box (redhat 6.2) with apache 1.3.12-2
The browser is netscape 4.73 with cache disable (check everytime).
-------------------------------------------------------------------------------------------------------------
The ./configure parameters :
./configure i386-redhat-linux --prefix=/usr --disable-debug --enable-pic
--with-apxs=/usr/sbin/apxs --with-config-file-path=/etc/httpd
--with-exec-dir=/usr/bin --with-regex=system --with-gettext --with-gd
--with-jpeg-dir=/usr --with-png --with-zlib --with-db2 --with-db3
--with-gdbm --enable-debugger --enable-magic-quotes --enable-safe-mode
--enable-sysvsem --enable-sysvshm --enable-track-vars --enable-yp
--enable-ftp --without-mysql --with-xml --enable-ats
--with-swf=/usr/lib/ > vueconfig
-------------------------------------------------------------------------------------------------------------
php_ats.h
#ifndef _PHP_ATS_H
#define _PHP_ATS_H
#if HAVE_ATS
extern zend_module_entry ats_module_entry;
#define ats_module_ptr &ats_module_entry
PHP_MINIT_FUNCTION(ats);
PHP_MSHUTDOWN_FUNCTION(ats);
PHP_RINIT_FUNCTION(ats);
PHP_RSHUTDOWN_FUNCTION(ats);
PHP_MINFO_FUNCTION(ats);
PHP_FUNCTION(ats_register);
PHP_FUNCTION(ats_show);
#define ATS_HOSTSIZE 64
#define ATS_USERSIZE 32
#define ATS_PASSWDSIZE 32
typedef struct ats_link_struct
{
int test; /* Cnt */
char host[ATS_HOSTSIZE+1];
char user[ATS_USERSIZE+1];
char passwd[ATS_PASSWDSIZE+1];
}ats_link;
typedef struct {
long num_persistent;
int le_plink;
} ats_module;
extern ats_module php_ats_module;
#else
#define ats_module_ptr NULL
#endif
#define phpext_ats_ptr ats_module_ptr
#endif /* _PHP_ATS_H */
-------------------------------------------------------------------------------------------------------------
ats.c
#include "php.h"
#include "php_ats.h"
#include "php_ini.h"
#include "php_globals.h"
#if HAVE_ATS
function_entry ats_functions[] =
{
PHP_FE(ats_register, NULL)
PHP_FE(ats_show, NULL)
{NULL, NULL, NULL}
};
zend_module_entry ats_module_entry =
{
"ats",
ats_functions,
PHP_MINIT(ats),
PHP_MSHUTDOWN(ats),
PHP_RINIT(ats),
PHP_RSHUTDOWN(ats),
PHP_MINFO(ats),
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_ATS
ZEND_GET_MODULE(ats)
#endif
THREAD_LS ats_module php_ats_module;
static void _close_ats_plink (ats_link *ats_ptr)
{
free(ats_ptr);
php_ats_module.num_persistent--;
}
PHP_MINIT_FUNCTION(ats)
{
php_ats_module.num_persistent=0;
php_ats_module.le_plink =
register_list_destructors(NULL,_close_ats_plink);
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(ats)
{
return SUCCESS;
}
PHP_RINIT_FUNCTION(ats)
{
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(ats)
{
return SUCCESS;
}
static void php_ats_do_connect(INTERNAL_FUNCTION_PARAMETERS)
{
char *user,*passwd,*host;
char *hashed_details;
int hashed_details_length;
ats_link ats, *ats_ptr;
pval *yyhost,*yyuser,*yypasswd;
list_entry *le;
list_entry new_le;
if (ARG_COUNT(ht) != 3)
{
WRONG_PARAM_COUNT;
}
if (getParameters(ht,3, &yyhost, &yyuser, &yypasswd) == FAILURE)
{
RETURN_FALSE;
}
convert_to_string(yyhost);
convert_to_string(yyuser);
convert_to_string(yypasswd);
host = yyhost->value.str.val;
user = yyuser->value.str.val;
passwd = yypasswd->value.str.val;
hashed_details_length = yyhost->value.str.len +
yyuser->value.str.len + yypasswd->value.str.len+3+3;
hashed_details = (char *)emalloc(hashed_details_length+1);
sprintf
(hashed_details,"ats_%s_%s_%s",yyhost->value.str.val,yyuser->value.str.val,yypasswd->value.str.val);
if (zend_hash_find(&EG(persistent_list), hashed_details,
hashed_details_length+1,(void **) &le) == FAILURE)
{
sprintf (ats.host,"%s",host);
sprintf (ats.user,"%s",user);
sprintf (ats.passwd,"%s",passwd);
ats.test = php_ats_module.num_persistent+1;
ats_ptr = (ats_link *)malloc(sizeof(ats_link));
memcpy (ats_ptr,&ats,sizeof(ats_link));
new_le.type = php_ats_module.le_plink;
new_le.ptr = ats_ptr;
if
(zend_hash_update(&EG(persistent_list),hashed_details,hashed_details_length+1,(void
*) &new_le,sizeof(list_entry),NULL) == FAILURE)
{
free(ats_ptr);
efree(hashed_details);
RETURN_FALSE;
}
php_ats_module.num_persistent++;
}
else
{
if (le->type != php_ats_module.le_plink)
{
php_error(E_WARNING,"ats : not an ats link");
RETURN_FALSE;
}
ats_ptr = (ats_link *)le->ptr;
}
return_value->value.lval =
zend_list_insert(ats_ptr,php_ats_module.le_plink);
return_value->type = IS_LONG;
efree(hashed_details);
}
PHP_MINFO_FUNCTION(ats)
{
php_info_print_table_start();
php_info_print_table_header(2, "ats support", "enabled");
php_info_print_table_end();
}
PHP_FUNCTION(ats_register)
{
php_ats_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
PHP_FUNCTION(ats_show)
{
pval *ats_link_index;
char string[512];
int id,type,len;
ats_link *ats_ptr;
if (ARG_COUNT(ht) != 1)
{
WRONG_PARAM_COUNT;
}
if (getParameters(ht,1,&ats_link_index) == FAILURE)
{
RETURN_FALSE;
}
convert_to_long(ats_link_index);
id = ats_link_index->value.lval;
ats_ptr = (ats_link *)zend_list_find(id,&type);
if (type != php_ats_module.le_plink)
{
php_error(E_WARNING,"%d not a ats link index",id);
RETURN_FALSE;
}
len = sprintf (string, "NumPers %d / Link ID %d - H=%s U=%s P=%s
T=%d",php_ats_module.num_persistent,id,ats_ptr->host,ats_ptr->user,ats_ptr->passwd,ats_ptr->test);
RETURN_STRINGL(string, len, 1);
}
#endif /* HAVE_ATS */
-------------------------------------------------------------------------------------------------------------
The first and second script (juste change user name and password for
example).
<?
echo "<strong>ats2</strong>\n";
echo "<P>\n";
echo "login=user2<BR>\n";
echo "</P>\n";
$r1=ats_register('host.domain.com','user1','pass1');
if (!$r1)
{
echo "wrong <BR>\n";
exit;
}
$numl = ats_show($r1);
echo "Param " . $numl . " <BR>\n";
echo "<P>\n";
echo "<strong>End</strong>\n";
?>
-------------------------------------------------------------------------------------------------------------------------------------------
If anyone have an idea ?
Thanks.
Chesneau Serge.