Explorar o código

Renamed 'MHD_pipe_close' -> 'MHD_itc_destroy_'

Evgeny Grin (Karlson2k) %!s(int64=9) %!d(string=hai) anos
pai
achega
f6f92fa688
Modificáronse 2 ficheiros con 43 adicións e 26 borrados
  1. 6 6
      src/microhttpd/daemon.c
  2. 37 20
      src/microhttpd/mhd_itc.h

+ 6 - 6
src/microhttpd/daemon.c

@@ -4473,7 +4473,7 @@ MHD_start_daemon_va (unsigned int flags,
 		  _("Failed to make read side of inter-thread control channel non-blocking: %s\n"),
 		  MHD_itc_last_strerror_ ());
 #endif
-        MHD_pipe_close_ (daemon->itc);
+        MHD_itc_destroy_chk_ (daemon->itc);
         free (daemon);
         return NULL;
       }
@@ -4487,7 +4487,7 @@ MHD_start_daemon_va (unsigned int flags,
       MHD_DLOG (daemon,
 		_("file descriptor for control pipe exceeds maximum value\n"));
 #endif
-      MHD_pipe_close_ (daemon->itc);
+      MHD_itc_destroy_chk_ (daemon->itc);
       free (daemon);
       return NULL;
     }
@@ -5005,7 +5005,7 @@ MHD_start_daemon_va (unsigned int flags,
               MHD_DLOG (daemon,
                         _("File descriptor for worker control pipe exceeds maximum value\n"));
 #endif
-              MHD_pipe_close_ (d->itc);
+              MHD_itc_destroy_chk_ (d->itc);
               goto thread_failed;
             }
 
@@ -5112,7 +5112,7 @@ thread_failed:
     gnutls_priority_deinit (daemon->priority_cache);
 #endif
   if (! MHD_INVALID_PIPE_(daemon->itc))
-    MHD_pipe_close_ (daemon->itc);
+    MHD_itc_destroy_chk_ (daemon->itc);
   free (daemon);
   return NULL;
 }
@@ -5339,7 +5339,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon)
             {
               if (! MHD_INVALID_PIPE_ (daemon->worker_pool[i].itc) )
                 {
-                  MHD_pipe_close_ (daemon->worker_pool[i].itc);
+                  MHD_itc_destroy_chk_ (daemon->worker_pool[i].itc);
                 }
 	    }
 	}
@@ -5395,7 +5395,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon)
   MHD_mutex_destroy_chk_ (&daemon->cleanup_connection_mutex);
 
   if (! MHD_INVALID_PIPE_(daemon->itc))
-    MHD_pipe_close_ (daemon->itc);
+    MHD_itc_destroy_chk_ (daemon->itc);
   free (daemon);
 }
 

+ 37 - 20
src/microhttpd/mhd_itc.h

@@ -36,6 +36,15 @@
 
 #include <fcntl.h>
 
+#ifndef MHD_PANIC
+#  include <stdio.h>
+#  include <stdlib.h>
+/* Simple implementation of MHD_PANIC, to be used outside lib */
+#  define MHD_PANIC(msg) do { fprintf (stderr,           \
+     "Abnormal termination at %d line in file %s: %s\n", \
+     (int)__LINE__, __FILE__, msg); abort();} while(0)
+#endif /* ! MHD_PANIC */
+
 #if defined(_MHD_ITC_EVENTFD)
 
 /* **************** Optimized GNU/Linux ITC implementation by eventfd ********** */
@@ -99,13 +108,11 @@ static const uint64_t _MHD_itc_wr_data = 1;
        (void)__r; } while(0)
 
 /**
- * Close any FDs of the pipe (non-W32)
+ * Destroy previously initialised ITC
+ * @param itc the itc to destroy
+ * @return non-zero if succeeded, zero otherwise
  */
-#define MHD_pipe_close_(pip) do { \
-    if ( (0 != close (pip)) && \
-         (EBADF == errno) )             \
-      MHD_PANIC (_("close failed"));    \
-  } while (0)
+#define MHD_itc_destroy_(itc) ((0 != close (itc)) || (EBADF != errno))
 
 /**
  * Check if we have an uninitialized pipe
@@ -188,16 +195,14 @@ static const uint64_t _MHD_itc_wr_data = 1;
     {} } while(0)
 
 /**
- * Close any FDs of the pipe (non-W32)
+ * Destroy previously initialised ITC
+ * @param itc the itc to destroy
+ * @return non-zero if succeeded, zero otherwise
  */
-#define MHD_pipe_close_(pip) do { \
-    if ( (0 != close ((pip).fd[0])) && \
-         (EBADF == errno) )             \
-      MHD_PANIC (_("close failed"));    \
-    if ( (0 != close ((pip).fd[1])) && \
-         (EBADF == errno) )             \
-      MHD_PANIC (_("close failed"));    \
-  } while (0)
+#define MHD_itc_destroy_(itc)      \
+  ( (0 == close ((itc).fd[0])) ?   \
+      (0 == close ((itc).fd[1])) : \
+      ((close ((itc).fd[1])), 0) )
 
 /**
  * Check if we have an uninitialized pipe
@@ -276,12 +281,14 @@ MHD_itc_nonblocking_ (MHD_itc_ itc);
     {} } while(0)
 
 /**
- * Close emulated pipe FDs
+ * Destroy previously initialised ITC
+ * @param itc the itc to destroy
+ * @return non-zero if succeeded, zero otherwise
  */
-#define MHD_pipe_close_(pip) do { \
-   MHD_socket_close_chk_ ((pip).sk[0]); \
-   MHD_socket_close_chk_ ((pip).sk[1]); \
-} while (0)
+#define MHD_itc_destroy_(itc)          \
+  ( MHD_socket_close_((itc).sk[0]) ?   \
+      MHD_socket_close_((itc).sk[1]) : \
+      ((void)MHD_socket_close_((itc).sk[1]), 0) )
 
 /**
  * Check for uninitialized pipe @a pip
@@ -300,4 +307,14 @@ MHD_itc_nonblocking_ (MHD_itc_ itc);
 
 #endif /* _MHD_ITC_SOCKETPAIR */
 
+/**
+ * Destroy previously initialised ITC and abort execution
+ * if error is detected.
+ * @param itc the itc to destroy
+ */
+#define MHD_itc_destroy_chk_(itc) do {          \
+    if (!MHD_itc_destroy_(itc))                 \
+      MHD_PANIC(_("Failed to destroy ITC.\n")); \
+  } while(0)
+
 #endif /* MHD_ITC_H */