Browse Source

Improve on error return.

Brian Aker 12 years ago
parent
commit
4631d8c107

+ 1 - 2
libgearman-server/connection.c

@@ -95,8 +95,7 @@ static gearman_server_con_st * _server_con_create(gearman_server_thread_st *thre
     con= build_gearman_server_con_st();
     if (con == NULL)
     {
-      gearmand_perror("new() build_gearman_server_con_st");
-      *ret= GEARMAN_MEMORY_ALLOCATION_FAILURE;
+      *ret= gearmand_perror(errno, "new() build_gearman_server_con_st");
       return NULL;
     }
   }

+ 30 - 28
libgearman-server/gearmand.cc

@@ -317,7 +317,7 @@ gearmand_error_t gearmand_port_add(gearmand_st *gearmand, const char *port,
                                          sizeof(gearmand_port_st) * (gearmand->port_count + 1));
   if (port_list == NULL)
   {
-    gearmand_perror("realloc");
+    gearmand_perror(errno, "realloc");
     return GEARMAN_MEMORY_ALLOCATION_FAILURE;
   }
 
@@ -432,7 +432,7 @@ void gearmand_wakeup(gearmand_st *gearmand, gearmand_wakeup_t wakeup)
   {
     if (written < 0)
     {
-      gearmand_perror(gearmand_strwakeup(wakeup));
+      gearmand_perror(errno, gearmand_strwakeup(wakeup));
     }
     else
     {
@@ -454,7 +454,7 @@ gearmand_error_t set_socket(int& fd, struct addrinfo *addrinfo_next)
              addrinfo_next->ai_protocol);
   if (fd == -1)
   {
-    return gearmand_perror("socket()");
+    return gearmand_perror(errno, "socket()");
   }
 
 #ifdef IPV6_V6ONLY
@@ -465,7 +465,7 @@ gearmand_error_t set_socket(int& fd, struct addrinfo *addrinfo_next)
       flags= 1;
       if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &flags, sizeof(flags)) == -1)
       {
-        return gearmand_perror("setsockopt(IPV6_V6ONLY)");
+        return gearmand_perror(errno, "setsockopt(IPV6_V6ONLY)");
       }
     }
   }
@@ -497,7 +497,7 @@ gearmand_error_t set_socket(int& fd, struct addrinfo *addrinfo_next)
     int flags= 1;
     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(flags)) == -1)
     {
-      return gearmand_perror("setsockopt(SO_REUSEADDR)");
+      return gearmand_perror(errno, "setsockopt(SO_REUSEADDR)");
     }
   }
 
@@ -505,7 +505,7 @@ gearmand_error_t set_socket(int& fd, struct addrinfo *addrinfo_next)
     int flags= 1;
     if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &flags, sizeof(flags)) == -1)
     {
-      return gearmand_perror("setsockopt(SO_KEEPALIVE)");
+      return gearmand_perror(errno, "setsockopt(SO_KEEPALIVE)");
     }
   }
 
@@ -513,7 +513,7 @@ gearmand_error_t set_socket(int& fd, struct addrinfo *addrinfo_next)
     struct linger ling= {0, 0};
     if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling)) == -1)
     {
-      return gearmand_perror("setsockopt(SO_LINGER)");
+      return gearmand_perror(errno, "setsockopt(SO_LINGER)");
     }
   }
 
@@ -521,7 +521,7 @@ gearmand_error_t set_socket(int& fd, struct addrinfo *addrinfo_next)
     int flags= 1;
     if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags)) == -1)
     {
-      return gearmand_perror("setsockopt(TCP_NODELAY)");
+      return gearmand_perror(errno, "setsockopt(TCP_NODELAY)");
     }
   }
 
@@ -633,8 +633,7 @@ static gearmand_error_t _listen_init(gearmand_st *gearmand)
 
         if (ret != EADDRINUSE)
         {
-          errno= ret;
-          return gearmand_perror("bind");
+          return gearmand_perror(ret, "bind");
         }
 
         this_wait= retry * retry / 3 + 1;
@@ -650,7 +649,7 @@ static gearmand_error_t _listen_init(gearmand_st *gearmand)
 
       if (listen(fd, gearmand->backlog) == -1)
       {
-        gearmand_perror("listen");
+        gearmand_perror(errno, "listen");
 
         gearmand_sockfd_close(fd);
 
@@ -662,7 +661,7 @@ static gearmand_error_t _listen_init(gearmand_st *gearmand)
         int* fd_list= (int *)realloc(port->listen_fd, sizeof(int) * (port->listen_count + 1));
         if (fd_list == NULL)
         {
-          gearmand_perror("realloc");
+          gearmand_perror(errno, "realloc");
 
           gearmand_sockfd_close(fd);
 
@@ -738,7 +737,7 @@ static gearmand_error_t _listen_watch(gearmand_st *gearmand)
 
       if (event_add(&(gearmand->port_list[x].listen_event[y]), NULL) < 0)
       {
-        gearmand_perror("event_add");
+        gearmand_perror(errno, "event_add");
         return GEARMAN_EVENT;
       }
     }
@@ -762,7 +761,7 @@ static void _listen_clear(gearmand_st *gearmand)
 
         if (event_del(&(gearmand->port_list[x].listen_event[y])) < 0)
         {
-          gearmand_perror("We tried to event_del() an event which no longer existed");
+          gearmand_perror(errno, "We tried to event_del() an event which no longer existed");
           assert(! "We tried to event_del() an event which no longer existed");
         }
       }
@@ -781,23 +780,25 @@ static void _listen_event(int event_fd, short events __attribute__ ((unused)), v
   int fd= accept(event_fd, &sa, &sa_len);
   if (fd == -1)
   {
-    if (errno == EINTR)
+    int local_error= errno;
+
+    if (local_error == EINTR)
     {
       return;
     }
-    else if (errno == ECONNABORTED)
+    else if (local_error == ECONNABORTED)
     {
-      gearmand_perror("accept");
+      gearmand_perror(local_error, "accept");
       return;
     }
-    else if (errno == EMFILE)
+    else if (local_error == EMFILE)
     {
-      gearmand_perror("accept");
+      gearmand_perror(local_error, "accept");
       return;
     }
 
     _clear_events(Gearmand());
-    gearmand_perror("accept");
+    gearmand_perror(local_error, "accept");
     Gearmand()->ret= GEARMAN_ERRNO;
     return;
   }
@@ -838,20 +839,20 @@ static gearmand_error_t _wakeup_init(gearmand_st *gearmand)
 
   if (pipe(gearmand->wakeup_fd) < 0)
   {
-    gearmand_perror("pipe");
+    gearmand_perror(errno, "pipe");
     return GEARMAN_ERRNO;
   }
 
   int returned_flags;
   if ((returned_flags= fcntl(gearmand->wakeup_fd[0], F_GETFL, 0)) < 0)
   {
-    gearmand_perror("fcntl:F_GETFL");
+    gearmand_perror(errno, "fcntl:F_GETFL");
     return GEARMAN_ERRNO;
   }
 
   if (fcntl(gearmand->wakeup_fd[0], F_SETFL, returned_flags | O_NONBLOCK) < 0)
   {
-    gearmand_perror("F_SETFL");
+    gearmand_perror(errno, "F_SETFL");
     return GEARMAN_ERRNO;
   }
 
@@ -887,7 +888,7 @@ static gearmand_error_t _wakeup_watch(gearmand_st *gearmand)
 
   if (event_add(&(gearmand->wakeup_event), NULL) < 0)
   {
-    gearmand_perror("event_add");
+    gearmand_perror(errno, "event_add");
     return GEARMAN_EVENT;
   }
 
@@ -902,7 +903,7 @@ static void _wakeup_clear(gearmand_st *gearmand)
     gearmand_debug("Clearing event for wakeup pipe");
     if (event_del(&(gearmand->wakeup_event)) < 0)
     {
-      gearmand_perror("We tried to event_del() an event which no longer existed");
+      gearmand_perror(errno, "We tried to event_del() an event which no longer existed");
       assert(! "We tried to event_del() an event which no longer existed");
     }
     gearmand->is_wakeup_event= false;
@@ -926,18 +927,19 @@ static void _wakeup_event(int fd, short, void *arg)
     }
     else if (ret == -1)
     {
-      if (errno == EINTR)
+      int local_error= errno;
+      if (local_error == EINTR)
       {
         continue;
       }
 
-      if (errno == EAGAIN)
+      if (local_error == EAGAIN)
       {
         break;
       }
 
       _clear_events(gearmand);
-      gearmand_perror("_wakeup_event:read");
+      gearmand_perror(local_error, "_wakeup_event:read");
       gearmand->ret= GEARMAN_ERRNO;
       return;
     }

+ 8 - 12
libgearman-server/gearmand_con.c

@@ -116,7 +116,7 @@ gearmand_error_t gearmand_con_create(gearmand_st *gearmand, int fd,
     dcon= build_gearmand_con_st(); 
     if (dcon == NULL)
     {
-      gearmand_perror("new build_gearmand_con_st");
+      gearmand_perror(errno, "new build_gearmand_con_st");
       gearmand_sockfd_close(fd);
 
       return GEARMAN_MEMORY_ALLOCATION_FAILURE;
@@ -167,8 +167,7 @@ gearmand_error_t gearmand_con_create(gearmand_st *gearmand, int fd,
     int error;
     if ((error= pthread_mutex_lock(&(dcon->thread->lock))) != 0)
     {
-      errno= error;
-      gearmand_perror("pthread_mutex_lock");
+      gearmand_perror(error, "pthread_mutex_lock");
       gearmand_fatal("Lock could not be taken on dcon->thread->, shutdown to occur");
       gearmand_wakeup(Gearmand(), GEARMAND_WAKEUP_SHUTDOWN);
     }
@@ -211,7 +210,7 @@ void gearmand_con_free(gearmand_con_st *dcon)
   {
     if (event_del(&(dcon->event)) < 0)
     {
-      gearmand_perror("event_del");
+      gearmand_perror(errno, "event_del");
     }
     else
     {
@@ -221,13 +220,13 @@ void gearmand_con_free(gearmand_con_st *dcon)
 
       if (event_add(&(dcon->event), NULL) < 0)
       {
-        gearmand_perror("event_add");
+        gearmand_perror(errno, "event_add");
       }
       else
       {
         if (event_del(&(dcon->event)) < 0)
         {
-          gearmand_perror("event_del");
+          gearmand_perror(errno, "event_del");
         }
       }
     }
@@ -261,8 +260,7 @@ void gearmand_con_free(gearmand_con_st *dcon)
       }
       else
       {
-        errno= error;
-        gearmand_perror("pthread_mutex_lock");
+        gearmand_perror(error, "pthread_mutex_lock");
       }
     }
   }
@@ -292,8 +290,7 @@ void gearmand_con_check_queue(gearmand_thread_st *thread)
 
       if ((error= pthread_mutex_unlock(&(thread->lock))) != 0)
       {
-        errno= error;
-        gearmand_perror("pthread_mutex_unlock");
+        gearmand_perror(error, "pthread_mutex_unlock");
         gearmand_fatal("Error in locking forcing a shutdown");
         gearmand_wakeup(Gearmand(), GEARMAND_WAKEUP_SHUTDOWN);
       }
@@ -307,8 +304,7 @@ void gearmand_con_check_queue(gearmand_thread_st *thread)
     }
     else
     {
-      errno= error;
-      gearmand_perror("pthread_mutex_lock");
+      gearmand_perror(error, "pthread_mutex_lock");
       gearmand_fatal("Lock could not be taken on thread->, shutdown to occur");
       gearmand_wakeup(Gearmand(), GEARMAND_WAKEUP_SHUTDOWN);
     }

+ 3 - 2
libgearman-server/gearmand_con_plus.cc

@@ -39,6 +39,7 @@
 #include "libgearman-server/common.h"
 
 #include <cassert>
+#include <cerrno>
 #include <memory>
 
 gearmand_con_st* build_gearmand_con_st(void)
@@ -104,7 +105,7 @@ gearmand_error_t gearmand_connection_watch(gearmand_io_st *con, short events, vo
     {
       if (event_del(&(dcon->event)) < 0)
       {
-        gearmand_perror("event_del");
+        gearmand_perror(errno, "event_del");
         assert(! "event_del");
       }
     }
@@ -113,7 +114,7 @@ gearmand_error_t gearmand_connection_watch(gearmand_io_st *con, short events, vo
 
     if (event_add(&(dcon->event), NULL) < 0)
     {
-      gearmand_perror("event_add");
+      gearmand_perror(errno, "event_add");
       return GEARMAN_EVENT;
     }
 

+ 22 - 26
libgearman-server/gearmand_thread.cc

@@ -66,7 +66,7 @@ bool fill_timespec(struct timespec& ts)
   {
     if (clock_gettime(CLOCK_REALTIME, &ts) == -1) 
     {
-      gearmand_perror("clock_gettime(CLOCK_REALTIME)");
+      gearmand_perror(errno, "clock_gettime(CLOCK_REALTIME)");
       return false;
     }
   }
@@ -75,7 +75,7 @@ bool fill_timespec(struct timespec& ts)
     struct timeval tv;
     if (gettimeofday(&tv, NULL) == -1) 
     {
-      gearmand_perror("gettimeofday()");
+      gearmand_perror(errno, "gettimeofday()");
       return false;
     }
 
@@ -182,9 +182,7 @@ gearmand_error_t gearmand_thread_create(gearmand_st *gearmand)
     thread->count= 0;
     gearmand_thread_free(thread);
 
-    errno= pthread_ret;
-    gearmand_fatal_perror("pthread_mutex_init");
-    return GEARMAN_ERRNO;
+    return gearmand_fatal_perror(pthread_ret, "pthread_mutex_init");
   }
 
   thread->is_thread_lock= true;
@@ -197,10 +195,7 @@ gearmand_error_t gearmand_thread_create(gearmand_st *gearmand)
     thread->count= 0;
     gearmand_thread_free(thread);
 
-    errno= pthread_ret;
-    gearmand_perror("pthread_create");
-
-    return GEARMAN_ERRNO;
+    return gearmand_perror(pthread_ret, "pthread_create");
   }
 
   gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "Thread %u created", thread->count);
@@ -226,8 +221,7 @@ void gearmand_thread_free(gearmand_thread_st *thread)
         pthread_error= pthread_timedjoin_np(thread->id, NULL, &ts);
         if (pthread_error)
         {
-          errno= pthread_error;
-          gearmand_perror("pthread_join");
+          gearmand_perror(pthread_error, "pthread_timedjoin_np");
         }
       }
 
@@ -236,8 +230,7 @@ void gearmand_thread_free(gearmand_thread_st *thread)
         pthread_error= pthread_kill(thread->id, SIGQUIT);
         if (pthread_error)
         {
-          errno= pthread_error;
-          gearmand_perror("pthread_join");
+          gearmand_perror(pthread_error, "pthread_kill(, SIGQUIT)");
         }
         pthread_error= pthread_join(thread->id, NULL);
       }
@@ -248,8 +241,7 @@ void gearmand_thread_free(gearmand_thread_st *thread)
 
     if (pthread_error)
     {
-      errno= pthread_error;
-      gearmand_perror("pthread_join");
+      gearmand_perror(pthread_error, "pthread_join");
     }
   }
 
@@ -258,8 +250,7 @@ void gearmand_thread_free(gearmand_thread_st *thread)
     int pthread_error;
     if ((pthread_error= pthread_mutex_destroy(&(thread->lock))))
     {
-      errno= pthread_error;
-      gearmand_perror("pthread_mutex_destroy");
+      gearmand_perror(pthread_error, "pthread_mutex_destroy");
     }
   }
 
@@ -312,7 +303,7 @@ void gearmand_thread_wakeup(gearmand_thread_st *thread,
      though if the thread is still active. */
   if (write(thread->wakeup_fd[1], &buffer, 1) != 1)
   {
-    gearmand_perror("write");
+    gearmand_perror(errno, "write");
   }
 }
 
@@ -401,21 +392,21 @@ static gearmand_error_t _wakeup_init(gearmand_thread_st *thread)
   ret= pipe(thread->wakeup_fd);
   if (ret == -1)
   {
-    gearmand_perror("pipe");
+    gearmand_perror(ret, "pipe");
     return GEARMAN_ERRNO;
   }
 
   ret= fcntl(thread->wakeup_fd[0], F_GETFL, 0);
   if (ret == -1)
   {
-    gearmand_perror("fcntl(F_GETFL)");
+    gearmand_perror(ret, "fcntl(F_GETFL)");
     return GEARMAN_ERRNO;
   }
 
   ret= fcntl(thread->wakeup_fd[0], F_SETFL, ret | O_NONBLOCK);
   if (ret == -1)
   {
-    gearmand_perror("fcntl(F_SETFL)");
+    gearmand_perror(ret, "fcntl(F_SETFL)");
     return GEARMAN_ERRNO;
   }
 
@@ -425,7 +416,7 @@ static gearmand_error_t _wakeup_init(gearmand_thread_st *thread)
 
   if (event_add(&(thread->wakeup_event), NULL) < 0)
   {
-    gearmand_perror("event_add");
+    gearmand_perror(errno, "event_add");
     return GEARMAN_EVENT;
   }
 
@@ -455,7 +446,7 @@ static void _wakeup_clear(gearmand_thread_st *thread)
     gearmand_log_debug(GEARMAN_DEFAULT_LOG_PARAM, "Clearing event for IO thread wakeup pipe %u", thread->count);
     if (event_del(&(thread->wakeup_event)) < 0)
     {
-      gearmand_perror("event_del() failure, shutdown may hang");
+      gearmand_perror(errno, "event_del() failure, shutdown may hang");
     }
     thread->is_wakeup_event= false;
   }
@@ -479,14 +470,19 @@ static void _wakeup_event(int fd, short events __attribute__ ((unused)), void *a
     }
     else if (ret == -1)
     {
-      if (errno == EINTR)
+      int local_errno= errno;
+      if (local_errno == EINTR)
+      {
         continue;
+      }
 
-      if (errno == EAGAIN)
+      if (local_errno == EAGAIN)
+      {
         break;
+      }
 
       _clear_events(thread);
-      gearmand_perror("_wakeup_event:read");
+      gearmand_perror(local_errno, "_wakeup_event:read");
       Gearmand()->ret= GEARMAN_ERRNO;
       return;
     }

+ 20 - 27
libgearman-server/io.cc

@@ -109,13 +109,14 @@ static size_t _connection_read(gearman_server_con_st *con, void *data, size_t da
     }
     else if (read_size == -1)
     {
-      switch (errno)
+      int local_errno= errno;
+      switch (local_errno)
       {
       case EAGAIN:
         ret= gearmand_io_set_events(con, POLLIN);
         if (gearmand_failed(ret))
         {
-          gearmand_perror("recv");
+          gearmand_perror(local_errno, "recv");
           return 0;
         }
 
@@ -140,11 +141,10 @@ static size_t _connection_read(gearman_server_con_st *con, void *data, size_t da
         break;
 
       default:
-        gearmand_perror("recv");
         ret= GEARMAN_ERRNO;
       }
 
-      gearmand_error("closing connection due to previous errno error");
+      gearmand_perror(local_errno, "closing connection due to previous errno error");
       _connection_close(connection);
       return 0;
     }
@@ -236,7 +236,8 @@ static gearmand_error_t _connection_flush(gearman_server_con_st *con)
         }
         else if (write_size == -1)
         {
-          switch (errno)
+          int local_errno= errno;
+          switch (local_errno)
           {
           case EAGAIN:
             {
@@ -254,7 +255,7 @@ static gearmand_error_t _connection_flush(gearman_server_con_st *con)
           case EPIPE:
           case ECONNRESET:
           case EHOSTDOWN:
-            gearmand_perror("lost connection to client during send(EPIPE || ECONNRESET || EHOSTDOWN)");
+            gearmand_perror(local_errno, "lost connection to client during send(EPIPE || ECONNRESET || EHOSTDOWN)");
             _connection_close(connection);
             return GEARMAN_LOST_CONNECTION;
 
@@ -262,7 +263,7 @@ static gearmand_error_t _connection_flush(gearman_server_con_st *con)
             break;
           }
 
-          gearmand_perror("send() failed, closing connection");
+          gearmand_perror(local_errno, "send() failed, closing connection");
           _connection_close(connection);
           return GEARMAN_ERRNO;
         }
@@ -790,8 +791,7 @@ static gearmand_error_t _io_setsockopt(gearmand_io_st &connection)
     int setting= 1;
     if (setsockopt(connection.fd, IPPROTO_TCP, TCP_NODELAY, &setting, (socklen_t)sizeof(int)) and errno != EOPNOTSUPP)
     {
-      gearmand_perror("setsockopt(TCP_NODELAY)");
-      return GEARMAN_ERRNO;
+      return gearmand_perror(errno, "setsockopt(TCP_NODELAY)");
     }
   }
 
@@ -801,8 +801,7 @@ static gearmand_error_t _io_setsockopt(gearmand_io_st &connection)
     linger.l_linger= GEARMAN_DEFAULT_SOCKET_TIMEOUT;
     if (setsockopt(connection.fd, SOL_SOCKET, SO_LINGER, &linger, (socklen_t)sizeof(struct linger)))
     {
-      gearmand_perror("setsockopt(SO_LINGER)");
-      return GEARMAN_ERRNO;
+      return gearmand_perror(errno, "setsockopt(SO_LINGER)");
     }
   }
 
@@ -813,7 +812,7 @@ static gearmand_error_t _io_setsockopt(gearmand_io_st &connection)
     // This is not considered a fatal error 
     if (setsockopt(connection.fd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&setting, sizeof(int)))
     {
-      gearmand_perror("setsockopt(SO_NOSIGPIPE)");
+      gearmand_perror(errno, "setsockopt(SO_NOSIGPIPE)");
     }
   }
 #endif
@@ -825,14 +824,12 @@ static gearmand_error_t _io_setsockopt(gearmand_io_st &connection)
     waittime.tv_usec= 0;
     if (setsockopt(connection.fd, SOL_SOCKET, SO_SNDTIMEO, &waittime, (socklen_t)sizeof(struct timeval)) and errno != ENOPROTOOPT)
     {
-      gearmand_perror("setsockopt(SO_SNDTIMEO)");
-      return GEARMAN_ERRNO;
+      return gearmand_perror(errno, "setsockopt(SO_SNDTIMEO)");
     }
 
     if (setsockopt(connection.fd, SOL_SOCKET, SO_RCVTIMEO, &waittime, (socklen_t)sizeof(struct timeval)) and errno != ENOPROTOOPT)
     {
-      gearmand_error("setsockopt(SO_RCVTIMEO)");
-      return GEARMAN_ERRNO;
+      return gearmand_perror(errno, "setsockopt(SO_RCVTIMEO)");
     }
   }
 
@@ -841,15 +838,13 @@ static gearmand_error_t _io_setsockopt(gearmand_io_st &connection)
     int setting= GEARMAN_DEFAULT_SOCKET_SEND_SIZE;
     if (setsockopt(connection.fd, SOL_SOCKET, SO_SNDBUF, &setting, (socklen_t)sizeof(int)))
     {
-      gearmand_perror("setsockopt(SO_SNDBUF)");
-      return GEARMAN_ERRNO;
+      return gearmand_perror(errno, "setsockopr(SO_SNDBUF)");
     }
 
     setting= GEARMAN_DEFAULT_SOCKET_RECV_SIZE;
     if (setsockopt(connection.fd, SOL_SOCKET, SO_RCVBUF, &setting, (socklen_t)sizeof(int)))
     {
-      gearmand_perror("setsockopt(SO_RCVBUF)");
-      return GEARMAN_ERRNO;
+      return gearmand_perror(errno, "setsockopt(SO_RCVBUF)");
     }
   }
 
@@ -857,14 +852,12 @@ static gearmand_error_t _io_setsockopt(gearmand_io_st &connection)
     int fcntl_flags;
     if ((fcntl_flags= fcntl(connection.fd, F_GETFL, 0)) == -1)
     {
-      gearmand_perror("fcntl(F_GETFL)");
-      return GEARMAN_ERRNO;
+      return gearmand_perror(errno, "fcntl(F_GETFL)");
     }
 
     if ((fcntl(connection.fd, F_SETFL, fcntl_flags | O_NONBLOCK) == -1))
     {
-      gearmand_perror("fcntl(F_SETFL)");
-      return GEARMAN_ERRNO;
+      return gearmand_perror(errno, "fcntl(F_SETFL)");
     }
   }
 
@@ -882,14 +875,14 @@ void gearmand_sockfd_close(int sockfd)
   /* in case of death shutdown to avoid blocking at close() */
   if (shutdown(sockfd, SHUT_RDWR) == SOCKET_ERROR && get_socket_errno() != ENOTCONN)
   {
-    gearmand_perror("shutdown");
+    gearmand_perror(errno, "shutdown");
     assert(errno != ENOTSOCK);
     return;
   }
 
   if (closesocket(sockfd) == SOCKET_ERROR)
   {
-    gearmand_perror("close");
+    gearmand_perror(errno, "close");
   }
 }
 
@@ -903,6 +896,6 @@ void gearmand_pipe_close(int pipefd)
 
   if (closesocket(pipefd) == SOCKET_ERROR)
   {
-    gearmand_perror("close");
+    gearmand_perror(errno, "close");
   }
 }

+ 35 - 7
libgearman-server/log.cc

@@ -219,7 +219,7 @@ void gearmand_log_fatal(const char *position, const char *func, const char *form
   }
 }
 
-void gearmand_log_fatal_perror(const char *position, const char *function, const char *message)
+gearmand_error_t gearmand_log_fatal_perror(const char *position, const char *function, int local_errno,  const char *message)
 {
   if (not Gearmand() || Gearmand()->verbose >= GEARMAND_VERBOSE_FATAL)
   {
@@ -228,14 +228,29 @@ void gearmand_log_fatal_perror(const char *position, const char *function, const
     errmsg[0]= 0; 
 
 #ifdef STRERROR_R_CHAR_P
-    errmsg_ptr= strerror_r(errno, errmsg, sizeof(errmsg));
+    errmsg_ptr= strerror_r(local_errno, errmsg, sizeof(errmsg));
 #else
-    strerror_r(errno, errmsg, sizeof(errmsg));
+    strerror_r(local_errno, errmsg, sizeof(errmsg));
     errmsg_ptr= errmsg;
 #endif
 
     gearmand_log_fatal(position, function, "%s(%s)", message, errmsg_ptr);
   }
+
+  switch (local_errno)
+  {
+  case ENOMEM:
+    return GEARMAN_MEMORY_ALLOCATION_FAILURE;
+
+  case ECONNRESET:
+  case EHOSTDOWN:
+    return GEARMAN_LOST_CONNECTION;
+
+  default:
+    break;
+  }
+
+  return GEARMAN_ERRNO;
 }
 
 gearmand_error_t gearmand_log_error(const char *position, const char *function, const char *format, ...)
@@ -301,7 +316,7 @@ void gearmand_log_debug(const char *position, const char *function, const char *
   }
 }
 
-gearmand_error_t gearmand_log_perror(const char *position, const char *function, const char *message)
+gearmand_error_t gearmand_log_perror(const char *position, const char *function, int local_errno, const char *message)
 {
   if (not Gearmand() or (Gearmand()->verbose >= GEARMAND_VERBOSE_ERROR))
   {
@@ -310,14 +325,27 @@ gearmand_error_t gearmand_log_perror(const char *position, const char *function,
     errmsg[0]= 0; 
 
 #ifdef STRERROR_R_CHAR_P
-    errmsg_ptr= strerror_r(errno, errmsg, sizeof(errmsg));
+    errmsg_ptr= strerror_r(local_errno, errmsg, sizeof(errmsg));
 #else
-    strerror_r(errno, errmsg, sizeof(errmsg));
+    strerror_r(local_errno, errmsg, sizeof(errmsg));
     errmsg_ptr= errmsg;
 #endif
     gearmand_log_error(position, function, "%s(%s)", message, errmsg_ptr);
   }
 
+  switch (local_errno)
+  {
+  case ENOMEM:
+    return GEARMAN_MEMORY_ALLOCATION_FAILURE;
+
+  case ECONNRESET:
+  case EHOSTDOWN:
+    return GEARMAN_LOST_CONNECTION;
+
+  default:
+    break;
+  }
+
   return GEARMAN_ERRNO;
 }
 
@@ -363,7 +391,7 @@ gearmand_error_t gearmand_log_gai_error(const char *position, const char *functi
 {
   if (rc == EAI_SYSTEM)
   {
-    return gearmand_log_perror(position, function, message);
+    return gearmand_log_perror(position, function, errno, message);
   }
 
   gearmand_log_error(position, function, "%s getaddrinfo(%s)", message, gai_strerror(rc));

+ 4 - 6
libgearman-server/log.h

@@ -71,8 +71,8 @@ void gearmand_log_fatal(const char *position, const char *func, const char *form
 #define gearmand_fatal(_mesg) gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, (_mesg))
 
 GEARMAN_INTERNAL_API
-void gearmand_log_fatal_perror(const char *position, const char *function, const char *message);
-#define gearmand_fatal_perror(_mesg) gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, (_mesg))
+gearmand_error_t gearmand_log_fatal_perror(const char *position, const char *function, int local_errno, const char *message);
+#define gearmand_fatal_perror(__local_errno, __mesg) gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, (__local_errno), (__mesg))
 
 
 /**
@@ -82,11 +82,9 @@ GEARMAN_INTERNAL_API
 gearmand_error_t gearmand_log_error(const char *position, const char *function, const char *format, ...);
 #define gearmand_error(_mesg) gearmand_log_error(GEARMAN_DEFAULT_LOG_PARAM, (_mesg))
 
-GEARMAN_INTERNAL_API
-gearmand_error_t gearmand_log_perror(const char *position, const char *function, const char *message);
-#define gearmand_perror(_mesg) gearmand_log_perror(GEARMAN_DEFAULT_LOG_PARAM,  (_mesg))
+gearmand_error_t gearmand_log_perror(const char *position, const char *function, int local_errno, const char *message);
+#define gearmand_perror(__local_errno, __mesg) gearmand_log_perror(GEARMAN_DEFAULT_LOG_PARAM, (__local_errno), (__mesg))
 
-GEARMAN_INTERNAL_API
 gearmand_error_t gearmand_log_gerror(const char *position, const char *function, const gearmand_error_t rc, const char *format, ...);
 #define gearmand_gerror(_mesg, _gearmand_errot_t) gearmand_log_gerror(GEARMAN_DEFAULT_LOG_PARAM, (_gearmand_errot_t), (_mesg))
 

+ 3 - 3
libgearman-server/packet.cc

@@ -48,6 +48,7 @@
 
 #include <libgearman-server/fifo.h>
 #include <cassert>
+#include <cerrno>
 #include <cstring>
 #include <memory>
 
@@ -89,7 +90,7 @@ gearman_server_packet_create(gearman_server_thread_st *thread,
     server_packet= new (std::nothrow) gearman_server_packet_st;
     if (server_packet == NULL)
     {
-      gearmand_perror("new() gearman_server_packet_st");
+      gearmand_perror(errno, "new() gearman_server_packet_st");
       return NULL;
     }
   }
@@ -319,8 +320,7 @@ inline static gearmand_error_t packet_create_arg(gearmand_packet_st *packet,
       char *new_args= (char *)realloc(packet->args, packet->args_size + arg_size);
       if (new_args == NULL)
       {
-        gearmand_perror("realloc");
-        return GEARMAN_MEMORY_ALLOCATION_FAILURE;
+        return gearmand_perror(errno, "realloc");
       }
       packet->args= new_args;
     }

+ 5 - 1
libgearman-server/plugins/queue/libmemcached/queue.cc

@@ -49,6 +49,10 @@
 #include <libgearman-server/plugins/queue/libmemcached/queue.h>
 #include <libmemcached/memcached.h>
 
+#include "libgearman-server/log.h"
+
+#include <cerrno>
+
 #pragma GCC diagnostic ignored "-Wold-style-cast"
 
 using namespace gearmand;
@@ -317,7 +321,7 @@ static memcached_return callback_loader(const memcached_st*,
   char* data= (char*)malloc(memcached_result_length(result));
   if (data == NULL)
   {
-    gearmand_perror("malloc");
+    gearmand_perror(errno, "malloc");
     return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
   } 
   memcpy(data, memcached_result_value(result), memcached_result_length(result));

Some files were not shown because too many files changed in this diff