- Fix exec() bug

- Merge fsock and file globals
This commit is contained in:
Zeev Suraski 2001-01-13 13:59:22 +00:00
parent 93f4fa8d73
commit 86624dfad9
5 changed files with 75 additions and 82 deletions

View file

@ -44,11 +44,12 @@ static int _Exec(int type, char *cmd, pval *array, pval *return_value)
FILE *fp;
char *buf, *tmp=NULL;
int buflen = 0;
int t, l, ret, output=1;
int t, l, output=1;
int overflow_limit, lcmd, ldir;
int rsrc_id;
char *b, *c, *d=NULL;
PLS_FETCH();
FLS_FETCH();
buf = (char*) emalloc(EXEC_INPUT_BUF);
if (!buf) {
@ -123,7 +124,7 @@ static int _Exec(int type, char *cmd, pval *array, pval *return_value)
* fd gets pclosed
*/
rsrc_id = ZEND_REGISTER_RESOURCE(NULL, fp, php_file_le_fopen());
rsrc_id = ZEND_REGISTER_RESOURCE(NULL, fp, php_file_le_popen());
if (type != 3) {
l=0;
@ -197,14 +198,16 @@ static int _Exec(int type, char *cmd, pval *array, pval *return_value)
zend_list_delete(rsrc_id);
#if HAVE_SYS_WAIT_H
if (WIFEXITED(ret)) {
ret = WEXITSTATUS(ret);
if (WIFEXITED(FG(pclose_ret))) {
ret = WEXITSTATUS(FG(pclose_ret));
}
#endif
if (d) efree(d);
if (d) {
efree(d);
}
efree(buf);
return ret;
return FG(pclose_ret);
}
/* {{{ proto int exec(string command [, array output [, int return_value]])

View file

@ -89,25 +89,15 @@ extern int fclose(FILE *);
#include "scanf.h"
#include "zend_API.h"
#ifdef ZTS
int file_globals_id;
#else
php_file_globals file_globals;
#endif
/* }}} */
/* {{{ ZTS-stuff / Globals / Prototypes */
typedef struct {
int fgetss_state;
int pclose_ret;
} php_file_globals;
#ifdef ZTS
#define FIL(v) (file_globals->v)
#define FIL_FETCH() php_file_globals *file_globals = ts_resource(file_globals_id)
int file_globals_id;
#else
#define FIL(v) (file_globals.v)
#define FIL_FETCH()
php_file_globals file_globals;
#endif
/* sharing globals is *evil* */
static int le_fopen, le_popen, le_socket;
@ -118,8 +108,9 @@ static int le_fopen, le_popen, le_socket;
static void _file_popen_dtor(zend_rsrc_list_entry *rsrc)
{
FILE *pipe = (FILE *)rsrc->ptr;
FIL_FETCH();
FIL(pclose_ret) = pclose(pipe);
FLS_FETCH();
FG(pclose_ret) = pclose(pipe);
}
@ -159,10 +150,20 @@ PHPAPI int php_file_le_socket(void) /* XXX doe we really want this???? */
#ifdef ZTS
static void php_file_init_globals(php_file_globals *file_globals)
static void file_globals_ctor(FLS_D)
{
FIL(fgetss_state) = 0;
FIL(pclose_ret) = 0;
zend_hash_init(&FG(ht_fsock_keys), 0, NULL, NULL, 1);
zend_hash_init(&FG(ht_fsock_socks), 0, NULL, (void (*)(void *))php_msock_destroy, 1);
FG(def_chunk_size) = PHP_FSOCK_CHUNK_SIZE;
FG(phpsockbuf) = NULL;
FG(fgetss_state) = 0;
FG(pclose_ret) = 0;
}
static void file_globals_dtor(FLS_D)
{
zend_hash_destroy(&FG(ht_fsock_socks));
zend_hash_destroy(&FG(ht_fsock_keys));
php_cleanup_sockbuf(1 FLS_CC);
}
#endif
@ -173,10 +174,9 @@ PHP_MINIT_FUNCTION(file)
le_socket = zend_register_list_destructors_ex(_file_socket_dtor, NULL, "socket", module_number);
#ifdef ZTS
file_globals_id = ts_allocate_id(sizeof(php_file_globals), (ts_allocate_ctor) php_file_init_globals, NULL);
file_globals_id = ts_allocate_id(sizeof(php_file_globals), (ts_allocate_ctor) file_globals_ctor, (ts_allocate_dtor) file_globals_dtor);
#else
FIL(fgetss_state) = 0;
FIL(pclose_ret) = 0;
file_globals_ctor(FLS_C);
#endif
REGISTER_LONG_CONSTANT("SEEK_SET", SEEK_SET, CONST_CS | CONST_PERSISTENT);
@ -526,7 +526,7 @@ PHP_NAMED_FUNCTION(php_if_fopen)
int *sock;
int use_include_path = 0;
int issock=0, socketd=0;
FIL_FETCH();
FLS_FETCH();
switch(ARG_COUNT(ht)) {
case 2:
@ -565,7 +565,7 @@ PHP_NAMED_FUNCTION(php_if_fopen)
}
efree(p);
FIL(fgetss_state)=0;
FG(fgetss_state)=0;
if (issock) {
sock=emalloc(sizeof(int));
@ -664,7 +664,7 @@ PHP_FUNCTION(pclose)
{
pval **arg1;
void *what;
FIL_FETCH();
FLS_FETCH();
if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1, &arg1) == FAILURE) {
WRONG_PARAM_COUNT;
@ -674,7 +674,7 @@ PHP_FUNCTION(pclose)
ZEND_VERIFY_RESOURCE(what);
zend_list_delete((*arg1)->value.lval);
RETURN_LONG(FIL(pclose_ret));
RETURN_LONG(FG(pclose_ret));
}
/* }}} */
@ -949,7 +949,7 @@ PHP_FUNCTION(fgetss)
void *what;
char *allowed_tags=NULL;
int allowed_tags_len=0;
FIL_FETCH();
FLS_FETCH();
switch(ARG_COUNT(ht)) {
case 2:
@ -995,7 +995,7 @@ PHP_FUNCTION(fgetss)
}
/* strlen() can be used here since we are doing it on the return of an fgets() anyway */
php_strip_tags(buf, strlen(buf), FIL(fgetss_state), allowed_tags, allowed_tags_len);
php_strip_tags(buf, strlen(buf), FG(fgetss_state), allowed_tags, allowed_tags_len);
RETURN_STRING(buf, 0);
}

View file

@ -72,4 +72,32 @@ PHPAPI int php_file_le_popen(void);
PHPAPI int php_file_le_socket(void);
PHPAPI int php_copy_file(char *src, char *dest);
typedef struct {
int fgetss_state;
int pclose_ret;
HashTable ht_fsock_keys;
HashTable ht_fsock_socks;
struct php_sockbuf *phpsockbuf;
size_t def_chunk_size;
} php_file_globals;
#ifdef ZTS
#define FLS_D php_file_globals *file_globals
#define FLS_DC , FLS_D
#define FLS_C file_globals
#define FLS_CC , FLS_C
#define FG(v) (file_globals->v)
#define FLS_FETCH() php_file_globals *file_globals = ts_resource(file_globals_id)
extern int file_globals_id;
#else
#define FLS_D void
#define FLS_DC
#define FLS_C
#define FLS_CC
#define FG(v) (file_globals.v)
#define FLS_FETCH()
extern php_file_globals file_globals;
#endif
#endif /* FILE_H */

View file

@ -357,7 +357,6 @@ PHP_FUNCTION(pfsockopen)
}
/* }}} */
#define CHUNK_SIZE 8192
#define SOCK_DESTROY(sock) \
if(sock->readbuf) pefree(sock->readbuf, sock->persistent); \
if(sock->prev) sock->prev->next = sock->next; \
@ -366,7 +365,7 @@ PHP_FUNCTION(pfsockopen)
FG(phpsockbuf) = sock->next; \
pefree(sock, sock->persistent)
static void php_cleanup_sockbuf(int persistent FLS_DC)
void php_cleanup_sockbuf(int persistent FLS_DC)
{
php_sockbuf *now, *next;
@ -431,7 +430,7 @@ size_t php_sock_set_def_chunk_size(size_t size)
old = FG(def_chunk_size);
if(size <= CHUNK_SIZE || size > 0)
if(size <= PHP_FSOCK_CHUNK_SIZE || size > 0)
FG(def_chunk_size) = size;
return old;
@ -516,7 +515,7 @@ static void php_sockwait_for_data(php_sockbuf *sock)
static size_t php_sockread_internal(php_sockbuf *sock)
{
char buf[CHUNK_SIZE];
char buf[PHP_FSOCK_CHUNK_SIZE];
int nr_bytes;
size_t nr_read = 0;
@ -716,28 +715,9 @@ void php_msock_destroy(int *data)
}
/* }}} */
static void fsock_globals_ctor(FLS_D)
{
zend_hash_init(&FG(ht_fsock_keys), 0, NULL, NULL, 1);
zend_hash_init(&FG(ht_fsock_socks), 0, NULL, (void (*)(void *))php_msock_destroy, 1);
FG(def_chunk_size) = CHUNK_SIZE;
FG(phpsockbuf) = NULL;
}
static void fsock_globals_dtor(FLS_D)
{
zend_hash_destroy(&FG(ht_fsock_socks));
zend_hash_destroy(&FG(ht_fsock_keys));
php_cleanup_sockbuf(1 FLS_CC);
}
PHP_MINIT_FUNCTION(fsock)
{
#ifdef ZTS
fsock_globals_id = ts_allocate_id(sizeof(php_fsock_globals), (ts_allocate_ctor) fsock_globals_ctor, (ts_allocate_dtor) fsock_globals_dtor);
#else
fsock_globals_ctor(FLS_C);
#endif
return SUCCESS;
}

View file

@ -24,6 +24,8 @@
#ifndef FSOCK_H
#define FSOCK_H
#include "file.h"
#ifdef PHP_WIN32
# ifndef WINNT
# define WINNT 1
@ -45,6 +47,8 @@
#include <sys/time.h>
#endif
#define PHP_FSOCK_CHUNK_SIZE 8192
struct php_sockbuf {
int socket;
unsigned char *readbuf;
@ -78,6 +82,7 @@ int php_sockdestroy(int socket);
int php_sock_close(int socket);
size_t php_sock_set_def_chunk_size(size_t size);
void php_msock_destroy(int *data);
void php_cleanup_sockbuf(int persistent FLS_DC);
PHPAPI int connect_nonb(int sockfd, struct sockaddr *addr, socklen_t addrlen, struct timeval *timeout);
PHPAPI struct php_sockbuf *php_get_socket(int socket);
@ -86,27 +91,4 @@ PHP_MINIT_FUNCTION(fsock);
PHP_MSHUTDOWN_FUNCTION(fsock);
PHP_RSHUTDOWN_FUNCTION(fsock);
typedef struct {
HashTable ht_fsock_keys;
HashTable ht_fsock_socks;
struct php_sockbuf *phpsockbuf;
size_t def_chunk_size;
} php_fsock_globals;
#ifdef ZTS
#define FLS_D php_fsock_globals *fsock_globals
#define FLS_DC , FLS_D
#define FLS_C fsock_globals
#define FLS_CC , FLS_C
#define FG(v) (fsock_globals->v)
#define FLS_FETCH() php_fsock_globals *fsock_globals = ts_resource(fsock_globals_id)
#else
#define FLS_D void
#define FLS_DC
#define FLS_C
#define FLS_CC
#define FG(v) (fsock_globals.v)
#define FLS_FETCH()
#endif
#endif /* FSOCK_H */