From cbba835b154ba31e7a07ba1b15a06b8953eb7e72 Mon Sep 17 00:00:00 2001 From: Jorge Arellano Cid Date: Wed, 9 Jan 2013 12:47:23 -0300 Subject: Refactored FD close calls into a single new dClose() dlib function [p37sitdu] --- dlib/dlib.c | 12 ++++++++++++ dlib/dlib.h | 8 ++++---- dpi/downloads.cc | 12 ++++++------ dpi/https.c | 4 ++-- dpid/dpid.c | 4 ++-- dpid/dpidc.c | 3 ++- dpid/main.c | 12 +++++++----- dpid/misc_new.c | 14 -------------- dpid/misc_new.h | 2 -- dpip/dpip.c | 4 ++-- src/IO/IO.c | 5 +---- src/IO/dpi.c | 34 ++++++++++------------------------ src/IO/http.c | 13 +------------ src/cookies.c | 2 +- src/dns.c | 9 ++++----- 15 files changed, 54 insertions(+), 84 deletions(-) diff --git a/dlib/dlib.c b/dlib/dlib.c index ebdaf0cd..d60f1bc6 100644 --- a/dlib/dlib.c +++ b/dlib/dlib.c @@ -931,3 +931,15 @@ char *dGetline (FILE *stream) return line; } +/* + * Close a FD handling EINTR. + */ +int dClose(int fd) +{ + int st; + + do + st = close(fd); + while (st == -1 && errno == EINTR); + return st; +} diff --git a/dlib/dlib.h b/dlib/dlib.h index 8dc324a7..0d194dbe 100644 --- a/dlib/dlib.h +++ b/dlib/dlib.h @@ -175,10 +175,10 @@ void dLib_show_messages(bool_t show); /* *- 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 } diff --git a/dpi/downloads.cc b/dpi/downloads.cc index 6f36afe9..70acaa8a 100644 --- a/dpi/downloads.cc +++ b/dpi/downloads.cc @@ -441,7 +441,7 @@ DLItem::~DLItem() 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::prButton_cb() 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 @@ static void read_log_cb(int fd_in, void *data) 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 @@ static void read_log_cb(int fd_in, void *data) 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. diff --git a/dpi/https.c b/dpi/https.c index 389c608d..9aea31e5 100644 --- a/dpi/https.c +++ b/dpi/https.c @@ -330,7 +330,7 @@ static void yes_ssl_support(void) dFree(check_cert); if (network_socket != -1){ - close(network_socket); + dClose(network_socket); network_socket = -1; } if (ssl_connection != NULL){ @@ -402,7 +402,7 @@ static int get_network_connection(char * url) 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); } diff --git a/dpid/dpid.c b/dpid/dpid.c index 28cfabba..ebabb9a2 100644 --- a/dpid/dpid.c +++ b/dpid/dpid.c @@ -762,7 +762,7 @@ void stop_active_dpis(struct dp *dpi_attr_list, int numdpis) } 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 @@ void ignore_dpi_sockets(struct dp *dpi_attr_list, int numdpis) 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); } } diff --git a/dpid/dpidc.c b/dpid/dpidc.c index 61a91275..58db9c3c 100644 --- a/dpid/dpidc.c +++ b/dpid/dpidc.c @@ -9,6 +9,7 @@ #include #include +#include "../dlib/dlib.h" #include "../dpip/dpip.h" #define MSG_ERR(...) printf("** ERROR **: " __VA_ARGS__); @@ -116,6 +117,6 @@ int main(int argc, char *argv[]) error("ERROR reading from socket"); printf("%s\n",buffer); */ - close(sockfd); + dClose(sockfd); return 0; } diff --git a/dpid/main.c b/dpid/main.c index 5f512245..6350a2b6 100644 --- a/dpid/main.c +++ b/dpid/main.c @@ -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 @@ static int start_filter_plugin(struct dp dpi_attr) } /* 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 @@ static void start_server_plugin(struct dp dpi_attr) 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 @@ int main(void) /* 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); diff --git a/dpid/misc_new.c b/dpid/misc_new.c index 8c07a4eb..50867bc3 100644 --- a/dpid/misc_new.c +++ b/dpid/misc_new.c @@ -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 diff --git a/dpid/misc_new.h b/dpid/misc_new.h index 325451a1..b015a77a 100644 --- a/dpid/misc_new.h +++ b/dpid/misc_new.h @@ -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); diff --git a/dpip/dpip.c b/dpip/dpip.c index dd97ec4a..e1908317 100644 --- a/dpip/dpip.c +++ b/dpip/dpip.c @@ -489,11 +489,11 @@ void a_Dpip_dsh_close(Dsh *dsh) 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)); } diff --git a/src/IO/IO.c b/src/IO/IO.c index 1243c70a..a0a8bba5 100644 --- a/src/IO/IO.c +++ b/src/IO/IO.c @@ -125,7 +125,6 @@ static void IO_free(IOData_t *io) */ 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 @@ static void IO_close_fd(IOData_t *io, int CloseCode) * 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 "); } diff --git a/src/IO/dpi.c b/src/IO/dpi.c index 24f1fce8..8e71f8ca 100644 --- a/src/IO/dpi.c +++ b/src/IO/dpi.c @@ -85,19 +85,6 @@ void a_Dpi_init(void) /* empty */ } -/* - * 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 */ @@ -362,7 +349,7 @@ static int Dpi_start_dpid(void) 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 @@ static int Dpi_start_dpid(void) 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 @@ static int Dpi_start_dpid(void) /* 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 @@ static int Dpi_check_dpid_ids() } 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 @@ static int Dpi_get_server_port(const char *server_name) dFree(cmd); } dFree(rply); - Dpi_close_fd(sock_fd); + dClose(sock_fd); return ok ? dpi_port : -1; } @@ -648,7 +634,7 @@ static int Dpi_connect_socket(const char *server_name) } 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 @@ char *a_Dpi_send_blocking_cmd(const char *server_name, const char *cmd) } 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; } diff --git a/src/IO/http.c b/src/IO/http.c index 3e87f912..bb4bca15 100644 --- a/src/IO/http.c +++ b/src/IO/http.c @@ -209,17 +209,6 @@ static void Http_socket_free(int SKey) } } -/* - * 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 @@ static int Http_connect_socket(ChainLink *Info) 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"); diff --git a/src/cookies.c b/src/cookies.c index e43a74a2..deaae2ab 100644 --- a/src/cookies.c +++ b/src/cookies.c @@ -90,7 +90,7 @@ static FILE *Cookies_fopen(const char *filename, char *init_str) filename, dStrerror(errno)); } } - close(fd); + dClose(fd); MSG("Cookies: Created file: %s\n", filename); F_in = fopen(filename, "r"); diff --git a/src/dns.c b/src/dns.c index 7ae19fe4..e288e8cc 100644 --- a/src/dns.c +++ b/src/dns.c @@ -217,9 +217,8 @@ void a_Dns_init(void) /* 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 @@ void a_Dns_freeall(void) 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); } -- cgit v1.2.3