Re: [PHP-DEV] socket_create() doesn't expose error if one occurs [CVS HEAD]

2002-04-29 Thread Markus Fischer

Hi,

attached is the patch I intend to commit later this day:

- Stores last errno in a module global implicitely
- Sets global last error explicitely for socket_select() and
  socket_create()
- Modified socket_last_error() to return global modules last
  error if no socket resource is given
- Added a couple of more E_WARNING messages in case something
  goes foobar so the user isn't left alone in the dark.

regards,
- Markus

On Sun, Apr 28, 2002 at 10:53:42PM -0500, Jason Greene wrote : 
 This was something I was planning on adding, but it was not applied to
 the 4.2.0 branch, because it introduces a feature enhancement (storing a
 global last error) and not a bug fix. It is a very simple enhance, I
 just have not had the time to work on it yet.
 
 -Jason

-- 
Please always Cc to me when replying to me on the lists.
GnuPG Key: http://guru.josefine.at/~mfischer/C2272BD0.asc
Mind if I MFH ? What QA did you do on it? the usual? ah... none :)


? sockets.diff
Index: php_sockets.h
===
RCS file: /repository/php4/ext/sockets/php_sockets.h,v
retrieving revision 1.22
diff -u -r1.22 php_sockets.h
--- php_sockets.h   6 Mar 2002 20:19:09 -   1.22
+++ php_sockets.h   29 Apr 2002 08:56:25 -
 -102,12 +102,14 
 int accept_connect(php_socket *in_sock, php_socket **new_sock, struct sockaddr *la 
TSRMLS_DC);
 int php_read(int bsd_socket, void *buf, int maxlen);
 
+ZEND_BEGIN_MODULE_GLOBALS(sockets)
+int last_error;
+ZEND_END_MODULE_GLOBALS(sockets)
+
 #ifdef ZTS
-#define SOCKETSG(v) (sockets_globals-v)
-#define SOCKETSLS_FETCH() php_sockets_globals *sockets_globals = 
ts_resource(sockets_globals_id)
+#define SOCKETS_G(v) TSRMG(sockets_globals_id, zend_sockets_globals *, v)
 #else
-#define SOCKETSG(v) (sockets_globals.v)
-#define SOCKETSLS_FETCH()
+#define SOCKETS_G(v) (sockets_globals.v)
 #endif
 
 #else
Index: sockets.c
===
RCS file: /repository/php4/ext/sockets/sockets.c,v
retrieving revision 1.98
diff -u -r1.98 sockets.c
--- sockets.c   11 Mar 2002 01:24:42 -  1.98
+++ sockets.c   29 Apr 2002 08:56:25 -
 -62,12 +62,7 
 # define IS_INVALID_SOCKET(a)  (a-bsd_socket == INVALID_SOCKET)
 #endif
 
-#ifdef ZTS
-int sockets_globals_id;
-#else
-php_sockets_globals sockets_globals;
-#endif
-
+ZEND_DECLARE_MODULE_GLOBALS(sockets)
 
 #ifndef MSG_WAITALL
 #ifdef LINUX
 -90,7 +85,9 
 #define PHP_BINARY_READ 0x0002
 
 #define PHP_SOCKET_ERROR(socket,msg,errn)  socket-error = errn;   \
-  
 php_error(E_WARNING, %s() %s [%d]: %s, get_active_function_name(TSRMLS_C), msg, 
errn, php_strerror(errn))
+  
+ SOCKETS_G(last_error) = errn; \
+  
+ php_error(E_WARNING, %s() %s [%d]: %s, \
+  
+   get_active_function_name(TSRMLS_C), msg, errn, php_strerror(errn))
 
 static int le_iov;
 #define le_iov_name Socket I/O vector
 -374,12 +371,20 
return 1;
 }

+
+static void php_sockets_init_globals(zend_sockets_globals *sockets_globals TSRMLS_DC)
+{
+   sockets_globals-last_error = 0;
+}
+
 /* {{{ PHP_MINIT_FUNCTION
  */
 PHP_MINIT_FUNCTION(sockets)
 {
struct protoent *pe;
 
+   ZEND_INIT_MODULE_GLOBALS(sockets, php_sockets_init_globals, NULL);
+
le_socket   = zend_register_list_destructors_ex(destroy_socket, NULL, 
le_socket_name, module_number);
le_iov  = zend_register_list_destructors_ex(destroy_iovec,  NULL, 
le_iov_name, module_number);
 
 -526,6 +531,7 
retval = select(max_fd+1, rfds, wfds, efds, tv_p);
 
if (retval == -1) {
+   SOCKETS_G(last_error) = errno;
php_error(E_WARNING, %s() %s [%d]: %s, 
get_active_function_name(TSRMLS_C), unable to select, errno, php_strerror(errno));
RETURN_FALSE;
}
 -570,6 +576,8 
ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, 
le_socket);

if (!accept_connect(php_sock, new_sock, (struct sockaddr *) sa TSRMLS_CC)) {
+   php_error(E_WARNING, %s() unable to accept socket connection [%d]: 
+%s,
+ get_active_function_name(TSRMLS_C), errno, 
+php_strerror(errno));
RETURN_FALSE;
}

 -798,6 +806,8 
RETURN_TRUE;
 
default:
+   php_error(E_WARNING, %s() Unsupported address family %d,
+ get_active_function_name(TSRMLS_C), 
+sa-sa_family);
RETURN_FALSE;
}
 }
 -854,6 +864,8 
  

Re: [PHP-DEV] socket_create() doesn't expose error if one occurs[CVS HEAD]

2002-04-29 Thread Jason Greene

The patch looks good to me,  go ahead and apply it.


Thanks,
-Jason

On Mon, 2002-04-29 at 04:02, Markus Fischer wrote:
 Hi,
 
 attached is the patch I intend to commit later this day:
 
 - Stores last errno in a module global implicitely
 - Sets global last error explicitely for socket_select() and
   socket_create()
 - Modified socket_last_error() to return global modules last
   error if no socket resource is given
 - Added a couple of more E_WARNING messages in case something
   goes foobar so the user isn't left alone in the dark.
 
 regards,
 - Markus
 
 On Sun, Apr 28, 2002 at 10:53:42PM -0500, Jason Greene wrote : 
  This was something I was planning on adding, but it was not applied to
  the 4.2.0 branch, because it introduces a feature enhancement (storing a
  global last error) and not a bug fix. It is a very simple enhance, I
  just have not had the time to work on it yet.
  
  -Jason
 
 -- 
 Please always Cc to me when replying to me on the lists.
 GnuPG Key: http://guru.josefine.at/~mfischer/C2272BD0.asc
 Mind if I MFH ? What QA did you do on it? the usual? ah... none :)
 
 

 ? sockets.diff
 Index: php_sockets.h
 ===
 RCS file: /repository/php4/ext/sockets/php_sockets.h,v
 retrieving revision 1.22
 diff -u -r1.22 php_sockets.h
 --- php_sockets.h 6 Mar 2002 20:19:09 -   1.22
 +++ php_sockets.h 29 Apr 2002 08:56:25 -
  -102,12 +102,14 
  int accept_connect(php_socket *in_sock, php_socket **new_sock, struct sockaddr *la 
TSRMLS_DC);
  int php_read(int bsd_socket, void *buf, int maxlen);
  
 +ZEND_BEGIN_MODULE_GLOBALS(sockets)
 +int last_error;
 +ZEND_END_MODULE_GLOBALS(sockets)
 +
  #ifdef ZTS
 -#define SOCKETSG(v) (sockets_globals-v)
 -#define SOCKETSLS_FETCH() php_sockets_globals *sockets_globals = 
ts_resource(sockets_globals_id)
 +#define SOCKETS_G(v) TSRMG(sockets_globals_id, zend_sockets_globals *, v)
  #else
 -#define SOCKETSG(v) (sockets_globals.v)
 -#define SOCKETSLS_FETCH()
 +#define SOCKETS_G(v) (sockets_globals.v)
  #endif
  
  #else
 Index: sockets.c
 ===
 RCS file: /repository/php4/ext/sockets/sockets.c,v
 retrieving revision 1.98
 diff -u -r1.98 sockets.c
 --- sockets.c 11 Mar 2002 01:24:42 -  1.98
 +++ sockets.c 29 Apr 2002 08:56:25 -
  -62,12 +62,7 
  # define IS_INVALID_SOCKET(a)(a-bsd_socket == INVALID_SOCKET)
  #endif
  
 -#ifdef ZTS
 -int sockets_globals_id;
 -#else
 -php_sockets_globals sockets_globals;
 -#endif
 -
 +ZEND_DECLARE_MODULE_GLOBALS(sockets)
  
  #ifndef MSG_WAITALL
  #ifdef LINUX
  -90,7 +85,9 
  #define PHP_BINARY_READ 0x0002
  
  #define PHP_SOCKET_ERROR(socket,msg,errn)socket-error = errn;   \
 -
 php_error(E_WARNING, %s() %s [%d]: %s, get_active_function_name(TSRMLS_C), msg, 
errn, php_strerror(errn))
 +
 SOCKETS_G(last_error) = errn; \
 +
 php_error(E_WARNING, %s() %s [%d]: %s, \
 +
   get_active_function_name(TSRMLS_C), msg, errn, php_strerror(errn))
  
  static int le_iov;
  #define le_iov_name Socket I/O vector
  -374,12 +371,20 
   return 1;
  }
   
 +
 +static void php_sockets_init_globals(zend_sockets_globals *sockets_globals 
TSRMLS_DC)
 +{
 + sockets_globals-last_error = 0;
 +}
 +
  /* {{{ PHP_MINIT_FUNCTION
   */
  PHP_MINIT_FUNCTION(sockets)
  {
   struct protoent *pe;
  
 + ZEND_INIT_MODULE_GLOBALS(sockets, php_sockets_init_globals, NULL);
 +
   le_socket   = zend_register_list_destructors_ex(destroy_socket, NULL, 
le_socket_name, module_number);
   le_iov  = zend_register_list_destructors_ex(destroy_iovec,  NULL, 
le_iov_name, module_number);
  
  -526,6 +531,7 
   retval = select(max_fd+1, rfds, wfds, efds, tv_p);
  
   if (retval == -1) {
 + SOCKETS_G(last_error) = errno;
   php_error(E_WARNING, %s() %s [%d]: %s, 
get_active_function_name(TSRMLS_C), unable to select, errno, php_strerror(errno));
   RETURN_FALSE;
   }
  -570,6 +576,8 
   ZEND_FETCH_RESOURCE(php_sock, php_socket *, arg1, -1, le_socket_name, 
le_socket);
   
   if (!accept_connect(php_sock, new_sock, (struct sockaddr *) sa TSRMLS_CC)) {
 + php_error(E_WARNING, %s() unable to accept socket connection [%d]: 
%s,
 +   get_active_function_name(TSRMLS_C), errno, 
php_strerror(errno));
   RETURN_FALSE;
   }
   
  -798,6 +806,8 
   RETURN_TRUE;
  
   default:
 + php_error(E_WARNING, %s() 

Re: [PHP-DEV] socket_create() doesn't expose error if one occurs[CVS HEAD]

2002-04-28 Thread Jason Greene

This was something I was planning on adding, but it was not applied to
the 4.2.0 branch, because it introduces a feature enhancement (storing a
global last error) and not a bug fix. It is a very simple enhance, I
just have not had the time to work on it yet.

-Jason



On Sat, 2002-04-27 at 07:22, Markus Fischer wrote:
 Hi,
 
 socket_create() doesn't expose an error if one occurs when
 creating a new socket with socket(). The current code is:
 
 [...]
 php_sock-bsd_socket = socket(arg1, arg2, arg3);
 php_sock-type = arg1;
 
 if (IS_INVALID_SOCKET(php_sock)) {
 efree(php_sock);
 RETURN_FALSE;
 }
 
 ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket);
 }
 
 This logic silently hides the error if we encounter one
 during socket creating.
 
 
 Since the current socket error reporting facility only
 operates with a valid socket resource context we have a
 limitation here because obviously there's no valid socket
 resource context here (false is returned and we don't have
 the socket resource yet).
 
 
 Suggestion:
 Introduce a (per thread) global variable which always stores
 the error message of the last socket function which failed
 and teach socket_last_error() to return this value if no
 resource is passed to it, e.g.:
 
 if (false == (socket_create(...))) {
 echo Failed, reason:, socket_strerror(socket_last_error()), \n;
 [...]
 }
 
 To be consistent with the other socket_*() functions I also
 suggest emitting an E_WARNING message if it fails (basically
 all other functions have been rewritten this way).
 
 I haven't included a patch because it's rather trivial.
 
 Any objections or better suggestions how to handle this?
 
 - Markus
 
 -- 
 Please always Cc to me when replying to me on the lists.
 GnuPG Key: http://guru.josefine.at/~mfischer/C2272BD0.asc
 Mind if I MFH ? What QA did you do on it? the usual? ah... none :)
 
 -- 
 PHP Development Mailing List http://www.php.net/
 To unsubscribe, visit: http://www.php.net/unsub.php
 



-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DEV] socket_create() doesn't expose error if one occurs [CVS HEAD]

2002-04-27 Thread Markus Fischer

Hi,

socket_create() doesn't expose an error if one occurs when
creating a new socket with socket(). The current code is:

[...]
php_sock-bsd_socket = socket(arg1, arg2, arg3);
php_sock-type = arg1;

if (IS_INVALID_SOCKET(php_sock)) {
efree(php_sock);
RETURN_FALSE;
}

ZEND_REGISTER_RESOURCE(return_value, php_sock, le_socket);
}

This logic silently hides the error if we encounter one
during socket creating.


Since the current socket error reporting facility only
operates with a valid socket resource context we have a
limitation here because obviously there's no valid socket
resource context here (false is returned and we don't have
the socket resource yet).


Suggestion:
Introduce a (per thread) global variable which always stores
the error message of the last socket function which failed
and teach socket_last_error() to return this value if no
resource is passed to it, e.g.:

if (false == (socket_create(...))) {
echo Failed, reason:, socket_strerror(socket_last_error()), \n;
[...]
}

To be consistent with the other socket_*() functions I also
suggest emitting an E_WARNING message if it fails (basically
all other functions have been rewritten this way).

I haven't included a patch because it's rather trivial.

Any objections or better suggestions how to handle this?

- Markus

-- 
Please always Cc to me when replying to me on the lists.
GnuPG Key: http://guru.josefine.at/~mfischer/C2272BD0.asc
Mind if I MFH ? What QA did you do on it? the usual? ah... none :)

-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, visit: http://www.php.net/unsub.php