changeset 2762:8a508291ac35

Refactored FD close calls into a single new dClose() dlib function [p37sitdu]
author Jorge Arellano Cid <jcid@dillo.org>
date Wed, 09 Jan 2013 12:47:23 -0300
parents 5216b6fc7681
children 35fb41bd0cdb
files dlib/dlib.c dlib/dlib.h dpi/downloads.cc dpi/https.c dpid/dpid.c dpid/dpidc.c dpid/main.c dpid/misc_new.c dpid/misc_new.h dpip/dpip.c src/IO/IO.c src/IO/dpi.c src/IO/http.c src/cookies.c src/dns.c
diffstat 15 files changed, 54 insertions(+), 84 deletions(-) [+]
line wrap: on
line diff
--- a/dlib/dlib.c	Wed Jan 09 14:04:59 2013 +0100
+++ b/dlib/dlib.c	Wed Jan 09 12:47:23 2013 -0300
@@ -931,3 +931,15 @@
    return line;
 }
 
+/*
+ * Close a FD handling EINTR.
+ */
+int dClose(int fd)
+{
+   int st;
+
+   do
+      st = close(fd);
+   while (st == -1 && errno == EINTR);
+   return st;
+}
--- a/dlib/dlib.h	Wed Jan 09 14:04:59 2013 +0100
+++ b/dlib/dlib.h	Wed Jan 09 12:47:23 2013 -0300
@@ -175,10 +175,10 @@
 /*
  *- Misc utility functions ----------------------------------------------------
  */
-char *dGetcwd ();
-char *dGethomedir ();
-char *dGetline (FILE *stream);
-
+char *dGetcwd();
+char *dGethomedir();
+char *dGetline(FILE *stream);
+int dClose(int fd);
 
 #ifdef __cplusplus
 }
--- a/dpi/downloads.cc	Wed Jan 09 14:04:59 2013 +0100
+++ b/dpi/downloads.cc	Wed Jan 09 12:47:23 2013 -0300
@@ -441,7 +441,7 @@
 void DLItem::abort_dl()
 {
    if (!log_done()) {
-      close(LogPipe[0]);
+      dClose(LogPipe[0]);
       Fl::remove_fd(LogPipe[0]);
       log_done(1);
       // Stop wget
@@ -459,9 +459,9 @@
 
 void DLItem::child_init()
 {
-   close(0); // stdin
-   close(1); // stdout
-   close(LogPipe[0]);
+   dClose(0); // stdin
+   dClose(1); // stdout
+   dClose(LogPipe[0]);
    dup2(LogPipe[1], 2); // stderr
    // set the locale to C for log parsing
    setenv("LC_ALL", "C", 1);
@@ -618,7 +618,7 @@
          perror("read, ");
          break;
       } else if (st == 0) {
-         close(fd_in);
+         dClose(fd_in);
          Fl::remove_fd(fd_in, 1);
          dl_item->log_done(1);
          break;
@@ -630,7 +630,7 @@
 
 void DLItem::father_init()
 {
-   close(LogPipe[1]);
+   dClose(LogPipe[1]);
    Fl::add_fd(LogPipe[0], 1, read_log_cb, this); // Read
 
    // Start the timer after the child is running.
--- a/dpi/https.c	Wed Jan 09 14:04:59 2013 +0100
+++ b/dpi/https.c	Wed Jan 09 12:47:23 2013 -0300
@@ -330,7 +330,7 @@
    dFree(check_cert);
 
    if (network_socket != -1){
-      close(network_socket);
+      dClose(network_socket);
       network_socket = -1;
    }
    if (ssl_connection != NULL){
@@ -402,7 +402,7 @@
 
    s = socket(hp->h_addrtype, SOCK_STREAM, 0);
    if (connect(s, (struct sockaddr *)&address, sizeof(address)) != 0){
-      close(s);
+      dClose(s);
       s = -1;
       MSG("errno: %i\n", errno);
    }
--- a/dpid/dpid.c	Wed Jan 09 14:04:59 2013 +0100
+++ b/dpid/dpid.c	Wed Jan 09 12:47:23 2013 -0300
@@ -762,7 +762,7 @@
       } else if (CKD_WRITE(sock_fd, bye_cmd) == -1) {
          ERRMSG("stop_active_dpis", "write", errno);
       }
-      a_Misc_close_fd(sock_fd);
+      dClose(sock_fd);
    }
 
    dFree(auth_cmd);
@@ -782,7 +782,7 @@
 
    for (i = 0; i < numdpis; i++) {
       FD_CLR(dpi_attr_list[i].sock_fd, &sock_set);
-      a_Misc_close_fd(dpi_attr_list[i].sock_fd);
+      dClose(dpi_attr_list[i].sock_fd);
    }
 }
 
--- a/dpid/dpidc.c	Wed Jan 09 14:04:59 2013 +0100
+++ b/dpid/dpidc.c	Wed Jan 09 12:47:23 2013 -0300
@@ -9,6 +9,7 @@
 #include <netdb.h>
 #include <errno.h>
 
+#include "../dlib/dlib.h"
 #include "../dpip/dpip.h"
 
 #define MSG_ERR(...) printf("** ERROR **: " __VA_ARGS__);
@@ -116,6 +117,6 @@
          error("ERROR reading from socket");
     printf("%s\n",buffer);
 */
-    close(sockfd);
+    dClose(sockfd);
     return 0;
 }
--- a/dpid/main.c	Wed Jan 09 14:04:59 2013 +0100
+++ b/dpid/main.c	Wed Jan 09 12:47:23 2013 -0300
@@ -26,6 +26,8 @@
 #include "dpi.h"
 #include "dpi_socket_dir.h"
 #include "misc_new.h"
+
+#include "../dlib/dlib.h"
 #include "../dpip/dpip.h"
 
 sigset_t mask_sigchld;
@@ -76,13 +78,13 @@
    }
 
    /* Parent, Close sockets fix stdio and return pid */
-   if (a_Misc_close_fd(newsock) == -1) {
+   if (dClose(newsock) == -1) {
       ERRMSG("start_plugin", "close", errno);
       MSG_ERR("ERROR in child proc for %s\n", dpi_attr.path);
       exit(1);
    }
-   a_Misc_close_fd(STDIN_FILENO);
-   a_Misc_close_fd(STDOUT_FILENO);
+   dClose(STDIN_FILENO);
+   dClose(STDOUT_FILENO);
    dup2(old_stdin, STDIN_FILENO);
    dup2(old_stdout, STDOUT_FILENO);
    return pid;
@@ -95,7 +97,7 @@
       MSG_ERR("ERROR in child proc for %s\n", dpi_attr.path);
       exit(1);
    }
-   if (a_Misc_close_fd(dpi_attr.sock_fd) == -1) {
+   if (dClose(dpi_attr.sock_fd) == -1) {
       ERRMSG("start_plugin", "close", errno);
       MSG_ERR("ERROR in child proc for %s\n", dpi_attr.path);
       exit(1);
@@ -224,7 +226,7 @@
    /* close inherited file descriptors */
    open_max = get_open_max();
    for (i = 3; i < open_max; i++)
-      a_Misc_close_fd(i);
+      dClose(i);
 
    /* this sleep used to unmask a race condition */
    // sleep(2);
--- a/dpid/misc_new.c	Wed Jan 09 14:04:59 2013 +0100
+++ b/dpid/misc_new.c	Wed Jan 09 12:47:23 2013 -0300
@@ -19,20 +19,6 @@
 #include "dpid_common.h"
 #include "misc_new.h"   /* for function prototypes */
 
-
-/*
- * Close a FD handling EINTR.
- */
-int a_Misc_close_fd(int fd)
-{
-   int st;
-
-   do {
-      st = close(fd);
-   } while (st < 0 && errno == EINTR);
-   return st;
-}
-
 /*! Reads a dpi tag from a socket
  * \li Continues after a signal interrupt
  * \Return
--- a/dpid/misc_new.h	Wed Jan 09 14:04:59 2013 +0100
+++ b/dpid/misc_new.h	Wed Jan 09 12:47:23 2013 -0300
@@ -1,8 +1,6 @@
 #ifndef MISC_NEW_H
 #define MISC_NEW_H
 
-
-int a_Misc_close_fd(int fd);
 Dstr *a_Misc_rdtag(int socket);
 char *a_Misc_readtag(int sock);
 char *a_Misc_mkdtemp(char *template);
--- a/dpip/dpip.c	Wed Jan 09 14:04:59 2013 +0100
+++ b/dpip/dpip.c	Wed Jan 09 12:47:23 2013 -0300
@@ -489,11 +489,11 @@
    a_Dpip_dsh_write(dsh, 1, "", 0);
 
    /* close fds */
-   while((st = close(dsh->fd_in)) < 0 && errno == EINTR) ;
+   st = dClose(dsh->fd_in);
    if (st < 0)
       MSG_ERR("[a_Dpip_dsh_close] close: %s\n", dStrerror(errno));
    if (dsh->fd_out != dsh->fd_in) {
-      while((st = close(dsh->fd_out)) < 0 && errno == EINTR) ;
+      st = dClose(dsh->fd_out);
       if (st < 0)
          MSG_ERR("[a_Dpip_dsh_close] close: %s\n", dStrerror(errno));
    }
--- a/src/IO/IO.c	Wed Jan 09 14:04:59 2013 +0100
+++ b/src/IO/IO.c	Wed Jan 09 12:47:23 2013 -0300
@@ -125,7 +125,6 @@
  */
 static void IO_close_fd(IOData_t *io, int CloseCode)
 {
-   int st;
    int events = 0;
 
    _MSG("====> begin IO_close_fd (%d) Key=%d CloseCode=%d Flags=%d ",
@@ -135,9 +134,7 @@
     * closed! (other clients may set 'IOFlag_ForceClose') */
    if (((io->Flags & IOFlag_ForceClose) || (CloseCode == IO_StopRdWr)) &&
        io->FD != -1) {
-      do
-         st = close(io->FD);
-      while (st < 0 && errno == EINTR);
+      dClose(io->FD);
    } else {
       _MSG(" NOT CLOSING ");
    }
--- a/src/IO/dpi.c	Wed Jan 09 14:04:59 2013 +0100
+++ b/src/IO/dpi.c	Wed Jan 09 12:47:23 2013 -0300
@@ -86,19 +86,6 @@
 }
 
 /*
- * Close a FD handling EINTR
- */
-static void Dpi_close_fd(int fd)
-{
-   int st;
-
-   dReturn_if (fd < 0);
-   do
-      st = close(fd);
-   while (st < 0 && errno == EINTR);
-}
-
-/*
  * Create a new connection data structure
  */
 static dpi_conn_t *Dpi_conn_new(ChainLink *Info)
@@ -362,7 +349,7 @@
    if (pid == 0) {
       /* This is the child process.  Execute the command. */
       char *path1 = dStrconcat(dGethomedir(), "/.dillo/dpid", NULL);
-      Dpi_close_fd(st_pipe[0]);
+      dClose(st_pipe[0]);
       if (execl(path1, "dpid", (char*)NULL) == -1) {
          dFree(path1);
          path1 = dStrconcat(DILLO_BINDIR, "dpid", NULL);
@@ -373,7 +360,7 @@
                if (Dpi_blocking_write(st_pipe[1], "ERROR", 5) == -1) {
                   MSG("Dpi_start_dpid (child): can't write to pipe.\n");
                }
-               Dpi_close_fd(st_pipe[1]);
+               dClose(st_pipe[1]);
                _exit (EXIT_FAILURE);
             }
          }
@@ -382,19 +369,18 @@
       /* The fork failed.  Report failure.  */
       MSG("Dpi_start_dpid: %s\n", dStrerror(errno));
       /* close the unused pipe */
-      Dpi_close_fd(st_pipe[0]);
-      Dpi_close_fd(st_pipe[1]);
-
+      dClose(st_pipe[0]);
+      dClose(st_pipe[1]);
    } else {
       /* This is the parent process, check our child status... */
-      Dpi_close_fd(st_pipe[1]);
+      dClose(st_pipe[1]);
       if ((answer = Dpi_blocking_read(st_pipe[0])) != NULL) {
          MSG("Dpi_start_dpid: can't start dpid\n");
          dFree(answer);
       } else {
          ret = 0;
       }
-      Dpi_close_fd(st_pipe[0]);
+      dClose(st_pipe[0]);
    }
 
    return ret;
@@ -467,7 +453,7 @@
       } else if (connect(sock_fd, (struct sockaddr *)&sin, sin_sz) == -1) {
          MSG("Dpi_check_dpid_ids: %s\n", dStrerror(errno));
       } else {
-         Dpi_close_fd(sock_fd);
+         dClose(sock_fd);
          ret = 1;
       }
    }
@@ -603,7 +589,7 @@
       dFree(cmd);
    }
    dFree(rply);
-   Dpi_close_fd(sock_fd);
+   dClose(sock_fd);
 
    return ok ? dpi_port : -1;
 }
@@ -648,7 +634,7 @@
    }
    dFree(cmd);
    if (sock_fd != -1 && ret == -1) /* can't send cmd? */
-      Dpi_close_fd(sock_fd);
+      dClose(sock_fd);
 
    return ret;
 }
@@ -797,7 +783,7 @@
    } if ((ret = Dpi_blocking_read(sock_fd)) == NULL) {
       MSG_ERR("[a_Dpi_send_blocking_cmd] Can't read message.\n");
    }
-   Dpi_close_fd(sock_fd);
+   dClose(sock_fd);
 
    return ret;
 }
--- a/src/IO/http.c	Wed Jan 09 14:04:59 2013 +0100
+++ b/src/IO/http.c	Wed Jan 09 12:47:23 2013 -0300
@@ -210,17 +210,6 @@
 }
 
 /*
- * Close the socket's FD
- */
-static void Http_socket_close(SocketData_t *S)
-{
-   int st;
-   do
-      st = close(S->SockFD);
-   while (st < 0 && errno == EINTR);
-}
-
-/*
  * Make the HTTP header's Referer line according to preferences
  * (default is "host" i.e. "scheme://hostname/" )
  */
@@ -448,7 +437,7 @@
       status = connect(S->SockFD, (struct sockaddr *)&name, socket_len);
       if (status == -1 && errno != EINPROGRESS) {
          S->Err = errno;
-         Http_socket_close(S);
+         dClose(S->SockFD);
          MSG("Http_connect_socket ERROR: %s\n", dStrerror(S->Err));
       } else {
          a_Chain_bcb(OpSend, Info, &S->SockFD, "FD");
--- a/src/cookies.c	Wed Jan 09 14:04:59 2013 +0100
+++ b/src/cookies.c	Wed Jan 09 12:47:23 2013 -0300
@@ -90,7 +90,7 @@
                    filename, dStrerror(errno));
             }
          }
-         close(fd);
+         dClose(fd);
 
          MSG("Cookies: Created file: %s\n", filename);
          F_in = fopen(filename, "r");
--- a/src/dns.c	Wed Jan 09 14:04:59 2013 +0100
+++ b/src/dns.c	Wed Jan 09 12:47:23 2013 -0300
@@ -217,9 +217,8 @@
       /* If the IPv6 address family is not available there is no point
          wasting time trying to connect to v6 addresses. */
       int fd = socket(AF_INET6, SOCK_STREAM, 0);
-      if (fd >= 0) {
-         close(fd);
-      }
+      if (fd >= 0)
+         dClose(fd);
    }
 #endif
 }
@@ -510,8 +509,8 @@
       dList_free(dns_cache[i].addr_list);
    }
    a_IOwatch_remove_fd(dns_notify_pipe[0], DIO_READ);
-   close(dns_notify_pipe[0]);
-   close(dns_notify_pipe[1]);
+   dClose(dns_notify_pipe[0]);
+   dClose(dns_notify_pipe[1]);
    dFree(dns_cache);
 }