Style cleanup
[m6w6/libmemcached] / libmemcached / memcached_connect.c
index cee75e4c7c24412752155464c4f5e263f2810d26..24328f4c4bb7c244b2bc358faeef953317af7207 100644 (file)
@@ -3,7 +3,7 @@
 #include <poll.h>
 #include <sys/time.h>
 
-static memcached_return set_hostinfo(memcached_server_st *server)
+static memcached_return_t set_hostinfo(memcached_server_st *server)
 {
   struct addrinfo *ai;
   struct addrinfo hints;
@@ -44,7 +44,7 @@ static memcached_return set_hostinfo(memcached_server_st *server)
   return MEMCACHED_SUCCESS;
 }
 
-static memcached_return set_socket_options(memcached_server_st *ptr)
+static memcached_return_t set_socket_options(memcached_server_st *ptr)
 {
   WATCHPOINT_ASSERT(ptr->fd != -1);
 
@@ -81,7 +81,7 @@ static memcached_return set_socket_options(memcached_server_st *ptr)
   }
 #endif
 
-  if (ptr->root->flags & MEM_NO_BLOCK)
+  if (ptr->root->flags.no_block)
   {
     int error;
     struct linger linger;
@@ -93,7 +93,7 @@ static memcached_return set_socket_options(memcached_server_st *ptr)
     WATCHPOINT_ASSERT(error == 0);
   }
 
-  if (ptr->root->flags & MEM_TCP_NODELAY)
+  if (ptr->root->flags.tcp_nodelay)
   {
     int flag= 1;
     int error;
@@ -121,22 +121,31 @@ static memcached_return set_socket_options(memcached_server_st *ptr)
     WATCHPOINT_ASSERT(error == 0);
   }
 
-  /* For the moment, not getting a nonblocking mode will not be fatal */
-  if ((ptr->root->flags & MEM_NO_BLOCK) || ptr->root->connect_timeout)
-  {
-    int flags;
+  /* libmemcached will always use nonblocking IO to avoid write deadlocks */
+  int flags;
 
+  do
     flags= fcntl(ptr->fd, F_GETFL, 0);
-    unlikely (flags != -1)
-    {
-      (void)fcntl(ptr->fd, F_SETFL, flags | O_NONBLOCK);
-    }
+  while (flags == -1 && (errno == EINTR || errno == EAGAIN));
+
+  unlikely (flags == -1)
+    return MEMCACHED_CONNECTION_FAILURE;
+  else if ((flags & O_NONBLOCK) == 0)
+  {
+    int rval;
+
+    do
+      rval= fcntl(ptr->fd, F_SETFL, flags | O_NONBLOCK);
+    while (rval == -1 && (errno == EINTR || errno == EAGAIN));
+
+    unlikely (rval == -1)
+      return MEMCACHED_CONNECTION_FAILURE;
   }
 
   return MEMCACHED_SUCCESS;
 }
 
-static memcached_return unix_socket_connect(memcached_server_st *ptr)
+static memcached_return_t unix_socket_connect(memcached_server_st *ptr)
 {
   struct sockaddr_un servAddr;
   socklen_t addrlen;
@@ -180,16 +189,16 @@ test_connect:
   return MEMCACHED_SUCCESS;
 }
 
-static memcached_return network_connect(memcached_server_st *ptr)
+static memcached_return_t network_connect(memcached_server_st *ptr)
 {
   if (ptr->fd == -1)
   {
     struct addrinfo *use;
 
     if (!ptr->sockaddr_inited ||
-        (!(ptr->root->flags & MEM_USE_CACHE_LOOKUPS)))
+        (!(ptr->root->flags.use_cache_lookups)))
     {
-      memcached_return rc;
+      memcached_return_t rc;
 
       rc= set_hostinfo(ptr);
       if (rc != MEMCACHED_SUCCESS)
@@ -219,14 +228,6 @@ static memcached_return network_connect(memcached_server_st *ptr)
 
       (void)set_socket_options(ptr);
 
-      int flags= 0;
-      if (ptr->root->connect_timeout)
-      {
-        flags= fcntl(ptr->fd, F_GETFL, 0);
-        if (flags != -1 && !(flags & O_NONBLOCK))
-          (void)fcntl(ptr->fd, F_SETFL, flags | O_NONBLOCK);
-      }
-
       /* connect to server */
       while (ptr->fd != -1 &&
              connect(ptr->fd, use->ai_addr, use->ai_addrlen) < 0)
@@ -268,10 +269,6 @@ static memcached_return network_connect(memcached_server_st *ptr)
 
       if (ptr->fd != -1)
       {
-        /* restore flags */
-        if (ptr->root->connect_timeout && (ptr->root->flags & MEM_NO_BLOCK) == 0)
-          (void)fcntl(ptr->fd, F_SETFL, flags & ~O_NONBLOCK);
-
         WATCHPOINT_ASSERT(ptr->cursor_active == 0);
         ptr->server_failure_counter= 0;
         return MEMCACHED_SUCCESS;
@@ -290,9 +287,10 @@ static memcached_return network_connect(memcached_server_st *ptr)
       if (gettimeofday(&next_time, NULL) == 0)
         ptr->next_retry= next_time.tv_sec + ptr->root->retry_timeout;
     }
-    ptr->server_failure_counter+= 1;
+    ptr->server_failure_counter++;
     if (ptr->cached_errno == 0)
       return MEMCACHED_TIMEOUT;
+
     return MEMCACHED_ERRNO; /* The last error should be from connect() */
   }
 
@@ -301,9 +299,9 @@ static memcached_return network_connect(memcached_server_st *ptr)
 }
 
 
-memcached_return memcached_connect(memcached_server_st *ptr)
+memcached_return_t memcached_connect(memcached_server_st *ptr)
 {
-  memcached_return rc= MEMCACHED_NO_SERVERS;
+  memcached_return_t rc= MEMCACHED_NO_SERVERS;
   LIBMEMCACHED_MEMCACHED_CONNECT_START();
 
   /* both retry_timeout and server_failure_limit must be set in order to delay retrying a server on error. */