bug fix for recursive decent into quit()
[awesomized/libmemcached] / libmemcached / io.c
index bca5ea4bb2f0d1a86b900634182789bf5c092705..ef46a0d9e460b93f819e8c70d368ce3c2c107b72 100644 (file)
@@ -53,16 +53,34 @@ static memcached_return_t io_wait(memcached_server_instance_st *ptr,
   if (ptr->root->flags.no_block == false)
     timeout= -1;
 
-  error= poll(&fds, 1, timeout);
+  size_t loop_max= 5;
+  while (--loop_max)
+  {
+    error= poll(&fds, 1, timeout);
 
-  if (error == 1)
-    return MEMCACHED_SUCCESS;
-  else if (error == 0)
-    return MEMCACHED_TIMEOUT;
+    switch (error)
+    {
+    case 1:
+      return MEMCACHED_SUCCESS;
+    case 0:
+      return MEMCACHED_TIMEOUT;
+#ifdef TARGET_OS_LINUX
+    case ERESTART:
+#endif
+    case EINTR:
+      continue;
+    default:
+      ptr->cached_errno= error;
+      memcached_quit_server(ptr, true);
+
+      return MEMCACHED_FAILURE;
+    }
+  }
 
   /* Imposssible for anything other then -1 */
   WATCHPOINT_ASSERT(error == -1);
-  memcached_quit_server(ptr, 1);
+  ptr->cached_errno= error;
+  memcached_quit_server(ptr, true);
 
   return MEMCACHED_FAILURE;
 }
@@ -157,6 +175,7 @@ static bool process_input_buffer(memcached_server_instance_st *ptr)
 
 static inline void memcached_io_cork_push(memcached_server_st *ptr)
 {
+  (void)ptr;
 #ifdef CORK
   if (ptr->root->flags.cork == false || ptr->state.is_corked)
     return;
@@ -168,13 +187,12 @@ static inline void memcached_io_cork_push(memcached_server_st *ptr)
     ptr->state.is_corked= true;
 
   WATCHPOINT_ASSERT(ptr->state.is_corked == true);
-#else
-  (void)ptr;
 #endif
 }
 
 static inline void memcached_io_cork_pop(memcached_server_st *ptr)
 {
+  (void)ptr;
 #ifdef CORK
   if (ptr->root->flags.cork == false || ptr->state.is_corked == false)
     return;
@@ -186,12 +204,10 @@ static inline void memcached_io_cork_pop(memcached_server_st *ptr)
     ptr->state.is_corked= false;
 
   WATCHPOINT_ASSERT(ptr->state.is_corked == false);
-#else
-  (void)ptr;
 #endif
 }
 
-#ifdef UNUSED
+#if 0 // Dead code, this should be removed.
 void memcached_io_preread(memcached_st *ptr)
 {
   unsigned int x;
@@ -244,13 +260,16 @@ memcached_return_t memcached_io_read(memcached_server_instance_st *ptr,
           {
           case EAGAIN:
           case EINTR:
+#ifdef TARGET_OS_LINUX
+          case ERESTART:
+#endif
             if ((rc= io_wait(ptr, MEM_READ)) == MEMCACHED_SUCCESS)
               continue;
             /* fall through */
 
           default:
             {
-              memcached_quit_server(ptr, 1);
+              memcached_quit_server(ptr, true);
               *nread= -1;
               return rc;
             }
@@ -267,7 +286,7 @@ memcached_return_t memcached_io_read(memcached_server_instance_st *ptr,
             for blocking I/O we do not return 0 and for non-blocking case
             it will return EGAIN if data is not immediatly available.
           */
-          memcached_quit_server(ptr, 1);
+          memcached_quit_server(ptr, true);
           *nread= -1;
           return MEMCACHED_UNKNOWN_READ_FAILURE;
         }
@@ -307,7 +326,7 @@ memcached_return_t memcached_io_read(memcached_server_instance_st *ptr,
 }
 
 ssize_t memcached_io_write(memcached_server_instance_st *ptr,
-                           const void *buffer, size_t length, char with_flush)
+                           const void *buffer, size_t length, bool with_flush)
 {
   size_t original_length;
   const char* buffer_ptr;
@@ -550,11 +569,11 @@ static ssize_t io_flush(memcached_server_instance_st *ptr,
           if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_TIMEOUT)
             continue;
 
-          memcached_quit_server(ptr, 1);
+          memcached_quit_server(ptr, true);
           return -1;
         }
       default:
-        memcached_quit_server(ptr, 1);
+        memcached_quit_server(ptr, true);
         *error= MEMCACHED_ERRNO;
         return -1;
       }
@@ -563,7 +582,7 @@ static ssize_t io_flush(memcached_server_instance_st *ptr,
     if (ptr->type == MEMCACHED_CONNECTION_UDP &&
         (size_t)sent_length != write_length)
     {
-      memcached_quit_server(ptr, 1);
+      memcached_quit_server(ptr, true);
       return -1;
     }
 
@@ -593,7 +612,7 @@ static ssize_t io_flush(memcached_server_instance_st *ptr,
 */
 void memcached_io_reset(memcached_server_instance_st *ptr)
 {
-  memcached_quit_server(ptr, 1);
+  memcached_quit_server(ptr, true);
 }
 
 /**