mirror of
https://github.com/php/php-src.git
synced 2025-08-16 05:58:45 +02:00
port "liveness" checks and a couple of other things from the 4.3 branch.
Liveness checks are used to validate persistent socket connects when they are re-used.
This commit is contained in:
parent
43698d6dbb
commit
3ee72aa5be
3 changed files with 33 additions and 10 deletions
|
@ -358,6 +358,10 @@ PHPAPI int _php_stream_truncate_set_size(php_stream *stream, size_t newsize TSRM
|
||||||
#define PHP_STREAM_OPTION_META_DATA_API 11 /* ptrparam is a zval* to which to add meta data information */
|
#define PHP_STREAM_OPTION_META_DATA_API 11 /* ptrparam is a zval* to which to add meta data information */
|
||||||
#define php_stream_populate_meta_data(stream, zv) (_php_stream_set_option((stream), PHP_STREAM_OPTION_META_DATA_API, 0, zv TSRMLS_CC) == PHP_STREAM_OPTION_RETURN_OK ? 1 : 0)
|
#define php_stream_populate_meta_data(stream, zv) (_php_stream_set_option((stream), PHP_STREAM_OPTION_META_DATA_API, 0, zv TSRMLS_CC) == PHP_STREAM_OPTION_RETURN_OK ? 1 : 0)
|
||||||
|
|
||||||
|
/* Check if the stream is still "live"; for sockets/pipes this means the socket
|
||||||
|
* is still connected; for files, this does not really have meaning */
|
||||||
|
#define PHP_STREAM_OPTION_CHECK_LIVENESS 12 /* no parameters */
|
||||||
|
|
||||||
#define PHP_STREAM_OPTION_RETURN_OK 0 /* option set OK */
|
#define PHP_STREAM_OPTION_RETURN_OK 0 /* option set OK */
|
||||||
#define PHP_STREAM_OPTION_RETURN_ERR -1 /* problem setting option */
|
#define PHP_STREAM_OPTION_RETURN_ERR -1 /* problem setting option */
|
||||||
#define PHP_STREAM_OPTION_RETURN_NOTIMPL -2 /* underlying stream does not implement; streams can handle it instead */
|
#define PHP_STREAM_OPTION_RETURN_NOTIMPL -2 /* underlying stream does not implement; streams can handle it instead */
|
||||||
|
|
|
@ -74,8 +74,10 @@ PHPAPI php_stream *_php_stream_xport_create(const char *name, long namelen, int
|
||||||
if (persistent_id) {
|
if (persistent_id) {
|
||||||
switch(php_stream_from_persistent_id(persistent_id, &stream TSRMLS_CC)) {
|
switch(php_stream_from_persistent_id(persistent_id, &stream TSRMLS_CC)) {
|
||||||
case PHP_STREAM_PERSISTENT_SUCCESS:
|
case PHP_STREAM_PERSISTENT_SUCCESS:
|
||||||
/* TODO: check if the socket is still live */
|
if (PHP_STREAM_OPTION_RETURN_OK == php_stream_set_option(stream, PHP_STREAM_OPTION_CHECK_LIVENESS, 0, NULL)) {
|
||||||
return stream;
|
return stream;
|
||||||
|
}
|
||||||
|
/* fall through */
|
||||||
|
|
||||||
case PHP_STREAM_PERSISTENT_FAILURE:
|
case PHP_STREAM_PERSISTENT_FAILURE:
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -148,15 +148,9 @@ static int php_sockop_close(php_stream *stream, int close_handle TSRMLS_DC)
|
||||||
if (close_handle) {
|
if (close_handle) {
|
||||||
|
|
||||||
if (sock->socket != -1) {
|
if (sock->socket != -1) {
|
||||||
|
#ifdef PHP_WIN32
|
||||||
/* prevent more data from coming in */
|
/* prevent more data from coming in */
|
||||||
|
|
||||||
#ifdef AF_UNIX
|
|
||||||
if (stream->ops != &php_stream_unix_socket_ops && stream->ops != &php_stream_unixdg_socket_ops) {
|
|
||||||
#endif
|
|
||||||
shutdown(sock->socket, SHUT_RD);
|
shutdown(sock->socket, SHUT_RD);
|
||||||
#ifdef AF_UNIX
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* try to make sure that the OS sends all data before we close the connection.
|
/* try to make sure that the OS sends all data before we close the connection.
|
||||||
* Essentially, we are waiting for the socket to become writeable, which means
|
* Essentially, we are waiting for the socket to become writeable, which means
|
||||||
|
@ -174,6 +168,7 @@ static int php_sockop_close(php_stream *stream, int close_handle TSRMLS_DC)
|
||||||
|
|
||||||
n = select(sock->socket + 1, NULL, &wrfds, &efds, &timeout);
|
n = select(sock->socket + 1, NULL, &wrfds, &efds, &timeout);
|
||||||
} while (n == -1 && php_socket_errno() == EINTR);
|
} while (n == -1 && php_socket_errno() == EINTR);
|
||||||
|
#endif
|
||||||
|
|
||||||
closesocket(sock->socket);
|
closesocket(sock->socket);
|
||||||
sock->socket = -1;
|
sock->socket = -1;
|
||||||
|
@ -205,6 +200,28 @@ static int php_sockop_set_option(php_stream *stream, int option, int value, void
|
||||||
php_stream_xport_param *xparam;
|
php_stream_xport_param *xparam;
|
||||||
|
|
||||||
switch(option) {
|
switch(option) {
|
||||||
|
case PHP_STREAM_OPTION_CHECK_LIVENESS:
|
||||||
|
{
|
||||||
|
fd_set rfds;
|
||||||
|
struct timeval tv = {0,0};
|
||||||
|
char buf;
|
||||||
|
int alive = 1;
|
||||||
|
|
||||||
|
if (sock->socket == -1) {
|
||||||
|
alive = 0;
|
||||||
|
} else {
|
||||||
|
FD_ZERO(&rfds);
|
||||||
|
FD_SET(sock->socket, &rfds);
|
||||||
|
|
||||||
|
if (select(sock->socket + 1, &rfds, NULL, NULL, &tv) > 0 && FD_ISSET(sock->socket, &rfds)) {
|
||||||
|
if (0 == recv(sock->socket, &buf, sizeof(buf), MSG_PEEK) && php_socket_errno() != EAGAIN) {
|
||||||
|
alive = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return alive ? PHP_STREAM_OPTION_RETURN_OK : PHP_STREAM_OPTION_RETURN_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
case PHP_STREAM_OPTION_BLOCKING:
|
case PHP_STREAM_OPTION_BLOCKING:
|
||||||
|
|
||||||
oldmode = sock->is_blocked;
|
oldmode = sock->is_blocked;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue