summaryrefslogtreecommitdiff
path: root/vio
diff options
context:
space:
mode:
authorKonstantin Osipov <kostja@sun.com>2009-11-20 17:18:37 +0300
committerKonstantin Osipov <kostja@sun.com>2009-11-20 17:18:37 +0300
commitd51a4fae328044f6b796ad8405dbb0dc3c653762 (patch)
tree126efa7bce0c3c70a1d3c3cb0099c6615f951e39 /vio
parentca595821498d6a547fa1780df86cdf127b0019b3 (diff)
parentecb6228c623cc2b239bf297e107225eaa4455d19 (diff)
downloadmariadb-git-d51a4fae328044f6b796ad8405dbb0dc3c653762.tar.gz
Merge with next-mr
Diffstat (limited to 'vio')
-rw-r--r--vio/vio.c32
-rw-r--r--vio/vio_priv.h14
-rw-r--r--vio/viosocket.c166
3 files changed, 204 insertions, 8 deletions
diff --git a/vio/vio.c b/vio/vio.c
index fd8e2e5a402..34e02adae3d 100644
--- a/vio/vio.c
+++ b/vio/vio.c
@@ -22,6 +22,28 @@
#include "vio_priv.h"
+#if defined(__WIN__) || defined(HAVE_SMEM)
+
+/**
+ Stub poll_read method that defaults to indicate that there
+ is data to read.
+
+ Used for named pipe and shared memory VIO types.
+
+ @param vio Unused.
+ @param timeout Unused.
+
+ @retval FALSE There is data to read.
+*/
+
+static my_bool no_poll_read(Vio *vio __attribute__((unused)),
+ uint timeout __attribute__((unused)))
+{
+ return FALSE;
+}
+
+#endif
+
/*
* Helper to fill most of the Vio* with defaults.
*/
@@ -60,6 +82,9 @@ static void vio_init(Vio* vio, enum enum_vio_type type,
vio->vioblocking =vio_blocking;
vio->is_blocking =vio_is_blocking;
+ vio->poll_read =no_poll_read;
+ vio->is_connected =vio_is_connected_pipe;
+
vio->timeout=vio_win32_timeout;
/* Set default timeout */
vio->read_timeout_millis = INFINITE;
@@ -87,6 +112,9 @@ static void vio_init(Vio* vio, enum enum_vio_type type,
vio->vioblocking =vio_blocking;
vio->is_blocking =vio_is_blocking;
+ vio->poll_read =no_poll_read;
+ vio->is_connected =vio_is_connected_shared_memory;
+
/* Currently, shared memory is on Windows only, hence the below is ok*/
vio->timeout= vio_win32_timeout;
/* Set default timeout */
@@ -112,6 +140,8 @@ static void vio_init(Vio* vio, enum enum_vio_type type,
vio->vioblocking =vio_ssl_blocking;
vio->is_blocking =vio_is_blocking;
vio->timeout =vio_timeout;
+ vio->poll_read =vio_poll_read;
+ vio->is_connected =vio_is_connected;
DBUG_VOID_RETURN;
}
#endif /* HAVE_OPENSSL */
@@ -130,6 +160,8 @@ static void vio_init(Vio* vio, enum enum_vio_type type,
vio->vioblocking =vio_blocking;
vio->is_blocking =vio_is_blocking;
vio->timeout =vio_timeout;
+ vio->poll_read =vio_poll_read;
+ vio->is_connected =vio_is_connected;
}
DBUG_VOID_RETURN;
}
diff --git a/vio/vio_priv.h b/vio/vio_priv.h
index 21fa80143cd..c1ac2ab8365 100644
--- a/vio/vio_priv.h
+++ b/vio/vio_priv.h
@@ -29,6 +29,20 @@
void vio_win32_timeout(Vio *vio, uint which, uint timeout);
#endif
+#ifdef __WIN__
+size_t vio_read_pipe(Vio *vio, uchar * buf, size_t size);
+size_t vio_write_pipe(Vio *vio, const uchar * buf, size_t size);
+my_bool vio_is_connected_pipe(Vio *vio);
+int vio_close_pipe(Vio * vio);
+#endif
+
+#ifdef HAVE_SMEM
+size_t vio_read_shared_memory(Vio *vio, uchar * buf, size_t size);
+size_t vio_write_shared_memory(Vio *vio, const uchar * buf, size_t size);
+my_bool vio_is_connected_shared_memory(Vio *vio);
+int vio_close_shared_memory(Vio * vio);
+#endif
+
void vio_timeout(Vio *vio,uint which, uint timeout);
#ifdef HAVE_OPENSSL
diff --git a/vio/viosocket.c b/vio/viosocket.c
index 89b3e36eb20..86268795e60 100644
--- a/vio/viosocket.c
+++ b/vio/viosocket.c
@@ -350,28 +350,163 @@ void vio_in_addr(Vio *vio, struct in_addr *in)
}
-/* Return 0 if there is data to be read */
+/**
+ Indicate whether there is data to read on a given socket.
+
+ @note An exceptional condition event and/or errors are
+ interpreted as if there is data to read.
+
+ @param sd A connected socket.
+ @param timeout Maximum time in seconds to poll.
+
+ @retval FALSE There is data to read.
+ @retval TRUE There is no data to read.
+*/
-my_bool vio_poll_read(Vio *vio,uint timeout)
+static my_bool socket_poll_read(my_socket sd, uint timeout)
{
-#ifndef HAVE_POLL
- return 0;
-#else
+#ifdef __WIN__
+ int res;
+ my_socket fd= sd;
+ fd_set readfds, errorfds;
+ struct timeval tm;
+ DBUG_ENTER("socket_poll_read");
+ tm.tv_sec= timeout;
+ tm.tv_usec= 0;
+ FD_ZERO(&readfds);
+ FD_ZERO(&errorfds);
+ FD_SET(fd, &readfds);
+ FD_SET(fd, &errorfds);
+ if ((res= select(fd, &readfds, NULL, &errorfds, &tm) <= 0))
+ {
+ DBUG_RETURN(res < 0 ? 0 : 1);
+ }
+ res= FD_ISSET(fd, &readfds) || FD_ISSET(fd, &errorfds);
+ DBUG_RETURN(!res);
+#elif defined(HAVE_POLL)
struct pollfd fds;
int res;
- DBUG_ENTER("vio_poll");
- fds.fd=vio->sd;
+ DBUG_ENTER("socket_poll_read");
+ fds.fd=sd;
fds.events=POLLIN;
fds.revents=0;
if ((res=poll(&fds,1,(int) timeout*1000)) <= 0)
{
DBUG_RETURN(res < 0 ? 0 : 1); /* Don't return 1 on errors */
}
- DBUG_RETURN(fds.revents & POLLIN ? 0 : 1);
+ DBUG_RETURN(fds.revents & (POLLIN | POLLERR | POLLHUP) ? 0 : 1);
+#else
+ return 0;
#endif
}
+/**
+ Retrieve the amount of data that can be read from a socket.
+
+ @param vio A VIO object.
+ @param bytes[out] The amount of bytes available.
+
+ @retval FALSE Success.
+ @retval TRUE Failure.
+*/
+
+static my_bool socket_peek_read(Vio *vio, uint *bytes)
+{
+#ifdef __WIN__
+ int len;
+ if (ioctlsocket(vio->sd, FIONREAD, &len))
+ return TRUE;
+ *bytes= len;
+ return FALSE;
+#elif FIONREAD_IN_SYS_IOCTL
+ int len;
+ if (ioctl(vio->sd, FIONREAD, &len) < 0)
+ return TRUE;
+ *bytes= len;
+ return FALSE;
+#else
+ char buf[1024];
+ ssize_t res= recv(vio->sd, &buf, sizeof(buf), MSG_PEEK);
+ if (res < 0)
+ return TRUE;
+ *bytes= res;
+ return FALSE;
+#endif
+}
+
+
+/**
+ Indicate whether there is data to read on a given socket.
+
+ @remark Errors are interpreted as if there is data to read.
+
+ @param sd A connected socket.
+ @param timeout Maximum time in seconds to wait.
+
+ @retval FALSE There is data (or EOF) to read. Also FALSE if error.
+ @retval TRUE There is _NO_ data to read or timed out.
+*/
+
+my_bool vio_poll_read(Vio *vio, uint timeout)
+{
+ my_socket sd= vio->sd;
+ DBUG_ENTER("vio_poll_read");
+#ifdef HAVE_OPENSSL
+ if (vio->type == VIO_TYPE_SSL)
+ sd= SSL_get_fd((SSL*) vio->ssl_arg);
+#endif
+ DBUG_RETURN(socket_poll_read(sd, timeout));
+}
+
+
+/**
+ Determine if the endpoint of a connection is still available.
+
+ @remark The socket is assumed to be disconnected if an EOF
+ condition is encountered.
+
+ @param vio The VIO object.
+
+ @retval TRUE EOF condition not found.
+ @retval FALSE EOF condition is signaled.
+*/
+
+my_bool vio_is_connected(Vio *vio)
+{
+ uint bytes= 0;
+ DBUG_ENTER("vio_is_connected");
+
+ /* In the presence of errors the socket is assumed to be connected. */
+
+ /*
+ The first step of detecting a EOF condition is veryfing
+ whether there is data to read. Data in this case would
+ be the EOF.
+ */
+ if (vio_poll_read(vio, 0))
+ DBUG_RETURN(TRUE);
+
+ /*
+ The second step is read() or recv() from the socket returning
+ 0 (EOF). Unfortunelly, it's not possible to call read directly
+ as we could inadvertently read meaningful connection data.
+ Simulate a read by retrieving the number of bytes available to
+ read -- 0 meaning EOF.
+ */
+ if (socket_peek_read(vio, &bytes))
+ DBUG_RETURN(TRUE);
+
+#ifdef HAVE_OPENSSL
+ /* There might be buffered data at the SSL layer. */
+ if (!bytes && vio->type == VIO_TYPE_SSL)
+ bytes= SSL_pending((SSL*) vio->ssl_arg);
+#endif
+
+ DBUG_RETURN(bytes ? TRUE : FALSE);
+}
+
+
void vio_timeout(Vio *vio, uint which, uint timeout)
{
#if defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)
@@ -494,6 +629,15 @@ size_t vio_write_pipe(Vio * vio, const uchar* buf, size_t size)
}
+my_bool vio_is_connected_pipe(Vio *vio)
+{
+ if (PeekNamedPipe(vio->hPipe, NULL, 0, NULL, NULL, NULL))
+ return TRUE;
+ else
+ return (GetLastError() != ERROR_BROKEN_PIPE);
+}
+
+
int vio_close_pipe(Vio * vio)
{
int r;
@@ -645,6 +789,12 @@ size_t vio_write_shared_memory(Vio * vio, const uchar* buf, size_t size)
}
+my_bool vio_is_connected_shared_memory(Vio *vio)
+{
+ return (WaitForSingleObject(vio->event_conn_closed, 0) != WAIT_OBJECT_0);
+}
+
+
/**
Close shared memory and DBUG_PRINT any errors that happen on closing.
@return Zero if all closing functions succeed, and nonzero otherwise.