[patch 3/4] io: Add timeout to io_t

Simon Horman <[email protected]>
Newsgroups gmane.mail.perdition.user
Message-ID <[email protected]>
io_t-timeout.patch (text/plain, 12.3 KB)
Cc: Christian Balzer <[email protected]>
Signed-off-by: Simon Horman <[email protected]>

Index: perdition/perdition/io.c
===================================================================
--- perdition.orig/perdition/io.c	2009-09-05 15:48:22.000000000 +1000
+++ perdition/perdition/io.c	2009-09-05 15:48:37.000000000 +1000
@@ -31,6 +31,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <vanessa_socket.h>
+#include <limits.h>
 
 #include "io.h"
 #include "io_select.h"
@@ -70,6 +71,7 @@ struct io_t_struct {
   __io_data_t data;
   char *name;
   enum io_err err;
+  long timeout;
 };
 
 
@@ -290,7 +292,6 @@ err:
  * pre: io: io_t to read from
  *      buf: buffer to read
  *      count: maximum number of bytes to read
- *      timeout: idle timeout in seconds, 0 for no timeout
  * post: up to count bytes are read from io into buf
  * return: Number of bytes read
  *         -1 on error
@@ -330,7 +331,7 @@ err:
 }
 
 
-ssize_t io_read(io_t *io, void *buf, size_t count, long timeout)
+ssize_t io_read(io_t *io, void *buf, size_t count)
 {
 	io_select_t *s = NULL;
 	struct timeval tv;
@@ -365,11 +366,12 @@ ssize_t io_read(io_t *io, void *buf, siz
 		FD_SET(rfd, &read_template);
 		FD_ZERO(&except_template);
 		FD_SET(rfd, &except_template);
-		tv.tv_sec = timeout;
+		tv.tv_sec = io->timeout;
 		tv.tv_usec = 0;
 
 		status = io_select(FD_SETSIZE, &read_template, NULL,
-                                   &except_template, timeout ? &tv : NULL, s);
+                                   &except_template, io->timeout ? &tv : NULL,
+                                   s);
 		if (status < 0) {
 			if (errno != EINTR) {
 				VANESSA_LOGGER_DEBUG_ERRNO("select");
@@ -423,6 +425,35 @@ void io_set_err(io_t *io, enum io_err er
 
 
 /**********************************************************************
+ * io_get_timoeut
+ * Get the idle timeout of an io
+ * pre: io: io_t to get the idle timeout of
+ * post: none
+ * return: timeout in seconds
+ **********************************************************************/
+
+long io_get_timeout(io_t *io)
+{
+        return io->timeout;
+}
+
+
+/**********************************************************************
+ * io_set_timoeut
+ * Set the idle timeout of an io
+ * pre: io: io_t to set the idle timeout of
+ *      timeout: timeout in seconds, 0 for no timeout
+ * post: idle timeout of the io_t is set
+ * return: none
+ **********************************************************************/
+
+void io_set_timeout(io_t *io, long timeout)
+{
+        io->timeout = timeout;
+}
+
+
+/**********************************************************************
  * io_get_rfd
  * Get the file descriptor that is being used for reading
  * pre: io: io_t to get read file descriptor of
@@ -615,17 +646,13 @@ err:
  *      io_b: the other io_t
  *      buffer:   allocated buffer to read data into
  *      buffer_length: size of buffer in bytes
- *      idle_timeout:  timeout in seconds to wait for input
- *                     timeout of 0 = infinite timeout
  *      return_a_read_bytes: Pointer to int where number
  *                           of bytes read from a will be recorded.
  *      return_b_read_bytes: Pointer to int where number
  *                           of bytes read from b will be recorded.
  * post: bytes are read from io_a and written to io_b and vice versa
- * return: -1 on error
- *         1 on idle timeout
+ * return: -1 on error, including idle timeout
  *         0 otherwise (one of io_a or io_b closes gracefully)
- *
  **********************************************************************/
 
 static int __io_pipe_read(int fd, void *buf, size_t count, void *data){
@@ -683,10 +710,26 @@ static int __io_pipe_write(int fd, const
 
          
 ssize_t io_pipe(io_t *io_a, io_t *io_b, unsigned char *buffer,
-      int buffer_length, int idle_timeout, int *return_a_read_bytes,
+      int buffer_length, int *return_a_read_bytes,
       int *return_b_read_bytes, timed_log_t *log){
   int bytes;
   io_select_t *s;
+  long timeout;
+
+  if (io_a->timeout && io_b->timeout)
+        timeout = io_a->timeout < io_b->timeout ? io_a->timeout :
+                io_b->timeout;
+  else if (io_a->timeout)
+        timeout = io_a->timeout;
+  else if (io_b->timeout)
+        timeout = io_b->timeout;
+  else
+        timeout = 0;
+
+  /* Cap timeout at INT_MAX, as vanessa_socket_pipe_func() takes
+   * an integer as the timeout argument */
+  if (timeout > INT_MAX)
+        timeout = INT_MAX;
 
   s = io_select_create();
   if(s == NULL) {
@@ -704,7 +747,7 @@ ssize_t io_pipe(io_t *io_a, io_t *io_b, 
 
   if((bytes=vanessa_socket_pipe_func(io_get_rfd(io_a), io_get_wfd(io_a),
       io_get_rfd(io_b), io_get_wfd(io_b), buffer, buffer_length,
-      idle_timeout, return_a_read_bytes, return_b_read_bytes,
+      timeout, return_a_read_bytes, return_b_read_bytes,
       __io_pipe_read, __io_pipe_write, io_select, (void *)s))<0){
 	VANESSA_LOGGER_DEBUG("vanessa_socket_pipe_func");
         if (bytes == 1) {
Index: perdition/perdition/io.h
===================================================================
--- perdition.orig/perdition/io.h	2009-09-05 15:48:01.000000000 +1000
+++ perdition/perdition/io.h	2009-09-05 15:48:37.000000000 +1000
@@ -117,13 +117,12 @@ ssize_t io_write(io_t *io, const void *b
  * pre: io: io_t to read from
  *      buf: buffer to read
  *      count: maximum number of bytes to read
- *      timeout: idle timeout in seconds, 0 for no timeout
  * post: up to count bytes are read from io into buf
  * return: Number of bytes read
  *         -1 on error
  **********************************************************************/
 
-ssize_t io_read(io_t *io, void *buf, size_t count, long timeout);
+ssize_t io_read(io_t *io, void *buf, size_t count);
 
 
 /**********************************************************************
@@ -183,6 +182,41 @@ const char *io_get_name(io_t *io);
 enum io_err io_get_err(io_t *io);
 
 
+/**********************************************************************
+ * io_get_timoeut
+ * Get the idle timeout of an io
+ * pre: io: io_t to get the idle timeout of
+ * post: none
+ * return: timeout in seconds
+ **********************************************************************/
+
+long io_get_timeout(io_t *io);
+
+
+/**********************************************************************
+ * io_set_timoeut
+ * Set the idle timeout of an io
+ * pre: io: io_t to set the idle timeout of
+ *      timeout: timeout in seconds, 0 for no timeout
+ * post: idle timeout of the io_t is set
+ * return: none
+ **********************************************************************/
+
+void io_set_timeout(io_t *io, long timeout);
+
+
+/**********************************************************************
+ * io_set_timoeut
+ * Set the idle timeout of io
+ * pre: io: io_t to set the idle timeout of
+ *      timeout: timeout in seconds, 0 for no timeout
+ * post: idle timeout of the io_t is set
+ * return: none
+ **********************************************************************/
+
+void io_set_timeout(io_t *io, long timeout);
+
+
 #ifdef WITH_SSL_SUPPORT
 /**********************************************************************
  * io_get_ssl
@@ -218,21 +252,17 @@ int io_close(io_t *io);
  *      io_b: the other io_t
  *      buffer:   allocated buffer to read data into
  *      buffer_length: size of buffer in bytes
- *      idle_timeout:  timeout in seconds to wait for input
- *                     timeout of 0 = infinite timeout
  *      return_a_read_bytes: Pointer to int where number
  *                           of bytes read from a will be recorded.
  *      return_b_read_bytes: Pointer to int where number
  *                           of bytes read from b will be recorded.
  * post: bytes are read from io_a and written to io_b and vice versa
- * return: -1 on error
- *         1 on idle timeout
+ * return: -1 on error, including idle timeout
  *         0 otherwise (one of io_a or io_b closes gracefully)
- *
  **********************************************************************/
 
 ssize_t io_pipe( io_t *io_a, io_t *io_b, unsigned char *buffer,
-  int buffer_length, int idle_timeout, int *return_a_read_bytes,
+  int buffer_length, int *return_a_read_bytes,
   int *return_b_read_bytes, timed_log_t *log);
 
 #endif /* _PERDITION_IO_H */
Index: perdition/perdition/perdition.c
===================================================================
--- perdition.orig/perdition/perdition.c	2009-09-05 15:47:46.000000000 +1000
+++ perdition/perdition/perdition.c	2009-09-05 15:48:37.000000000 +1000
@@ -577,6 +577,7 @@ int main (int argc, char **argv, char **
       VANESSA_LOGGER_ERR("Fatal error setting IO. Exiting.");
       perdition_exit_cleanly(-1);
     }
+    io_set_timeout(client_io, opt.timeout);
 
     namelen = sizeof(*peername);
     if (getpeername(0, (struct sockaddr *)peername, &namelen)) {
@@ -611,6 +612,7 @@ int main (int argc, char **argv, char **
       VANESSA_LOGGER_ERR("Fatal error setting IO. Exiting.");
       perdition_exit_cleanly(-1);
     }
+    io_set_timeout(client_io, opt.timeout);
   }
 
   /* A child process, or process handling an inetd connection
@@ -698,9 +700,15 @@ int main (int argc, char **argv, char **
     token_flush();
     if(status<0){
       VANESSA_LOGGER_DEBUG("protocol->in_get_pw");
-      VANESSA_LOGGER_ERR_UNSAFE("Fatal Error reading authentication "
-				"information from client \"%s\": "
-				"Exiting child", from_to_host_str);
+      if (io_get_err(client_io) == io_err_timeout)
+	VANESSA_LOGGER_ERR_UNSAFE("Fatal Error: Timeout reading "
+				  "authentication information from "
+				  "client \"%s\": Exiting child",
+				  from_to_host_str);
+      else
+	VANESSA_LOGGER_ERR_UNSAFE("Fatal Error reading authentication "
+				  "information from client \"%s\": "
+				  "Exiting child", from_to_host_str);
       perdition_exit_cleanly(-1);
     }
     else if(status == 1){
@@ -841,6 +849,7 @@ int main (int argc, char **argv, char **
       VANESSA_LOGGER_ERR("Fatal error setting IO. Exiting.");
       perdition_exit_cleanly(-1);
     }
+    io_set_timeout(server_io, opt.timeout);
 
 #ifdef WITH_SSL_SUPPORT
     if(opt.ssl_mode & SSL_MODE_SSL_OUTGOING) {
@@ -932,7 +941,11 @@ int main (int argc, char **argv, char **
     }
     else if(status<0){
       VANESSA_LOGGER_DEBUG_UNSAFE("protocol->out_authenticate %d", status);
-      VANESSA_LOGGER_ERR("Fatal error authenticating user. Exiting child.");
+      if (io_get_err(client_io) == io_err_timeout)
+	VANESSA_LOGGER_ERR("Fatal error: Timeout authenticating user. "
+			   "Exiting child.");
+      else
+        VANESSA_LOGGER_ERR("Fatal error authenticating user. Exiting child.");
       perdition_exit_cleanly(-1);
     }
 
@@ -971,10 +984,14 @@ int main (int argc, char **argv, char **
   }
 
   /*Let the client talk to the real server*/
-  if(io_pipe(server_io, client_io, buffer, BUFFER_SIZE, opt.timeout,
+  if(io_pipe(server_io, client_io, buffer, BUFFER_SIZE,
         &bytes_written, &bytes_read, &auth_log)<0){
     VANESSA_LOGGER_DEBUG("vanessa_socket_pipe");
-    VANESSA_LOGGER_ERR("Fatal error piping data. Exiting child.");
+    if (io_get_err(client_io) == io_err_timeout)
+      VANESSA_LOGGER_ERR("Fatal error: Timeout piping data. "
+			 "Exiting child.");
+    else
+      VANESSA_LOGGER_ERR("Fatal error piping data. Exiting child.");
     perdition_exit_cleanly(-1);
   }
 
Index: perdition/perdition/token.c
===================================================================
--- perdition.orig/perdition/token.c	2009-09-05 15:48:01.000000000 +1000
+++ perdition/perdition/token.c	2009-09-05 15:48:37.000000000 +1000
@@ -218,8 +218,7 @@ static int __token_fill_buffer(io_t *io,
 	int bytes_read;
 	char *dump_buf;
 
-	bytes_read = io_read(io, token_read_buffer, MAX_LINE_LENGTH-1,
-			     opt->timeout);
+	bytes_read = io_read(io, token_read_buffer, MAX_LINE_LENGTH-1);
 	if (bytes_read < 0) {
 		VANESSA_LOGGER_DEBUG_ERRNO("error reading input");
 		return -1;
Index: perdition/perdition/ssl.c
===================================================================
--- perdition.orig/perdition/ssl.c	2009-09-05 15:47:46.000000000 +1000
+++ perdition/perdition/ssl.c	2009-09-05 15:48:37.000000000 +1000
@@ -926,6 +926,7 @@ static io_t *__perdition_ssl_connection(
 		goto bail;
 	}
 
+	io_set_timeout(new_io, io_get_timeout(io));
 	io_destroy(io);
 	io = NULL;
 

______________________________________________
Perdition-users mailing list
[email protected]
http://lists.vergenet.net/listinfo/perdition-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.