From 9955b35f5eaab57fd902f9184d0223a55200821d Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Thu, 8 Nov 2001 18:15:23 +0000 Subject: Sicket leak avoidance. svn path=/trunk/; revision=3545 --- socket.c | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'socket.c') diff --git a/socket.c b/socket.c index 93a2be2f..8c2ce7bb 100644 --- a/socket.c +++ b/socket.c @@ -36,6 +36,7 @@ #else #include #endif +#include #include "socket.h" #include "fetchmail.h" #include "i18n.h" @@ -218,7 +219,11 @@ int SockCheckOpen(int fd) int UnixOpen(const char *path) { - int sock = -1; +#ifdef HAVE_SIGPROCMASK + sigset_t allsigs; +#endif /* HAVE_SIGPROCMASK */ + + int stat, sock = -1; struct sockaddr_un ad; memset(&ad, 0, sizeof(ad)); ad.sun_family = AF_UNIX; @@ -230,14 +235,26 @@ int UnixOpen(const char *path) h_errno = 0; return -1; } + +#ifdef HAVE_SIGPROCMASK + /* avoid socket leak on alarm signal during connect(2) */ + sigfillset(&allsigs); + sigprocmask(SIG_BLOCK, &allsigs, NULL); +#endif /* HAVE_SIGPROCMASK */ + if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0) { int olderr = errno; fm_close(sock); /* don't use SockClose, no traffic yet */ h_errno = 0; errno = olderr; - return -1; + sock = -1; } + +#ifdef HAVE_SIGPROCMASK + sigprocmask(SIG_UNBLOCK, &allsigs, NULL); +#endif /* HAVE_SIGPROCMASK */ + return sock; } @@ -245,6 +262,10 @@ int UnixOpen(const char *path) int SockOpen(const char *host, const char *service, const char *options, const char *plugin) { +#ifdef HAVE_SIGPROCMASK + sigset_t allsigs; +#endif /* HAVE_SIGPROCMASK */ + struct addrinfo *ai, *ai0, req; int i; #if NET_SECURITY @@ -282,6 +303,13 @@ int SockOpen(const char *host, const char *service, const char *options, if (i >= 0) break; #else + +#ifdef HAVE_SIGPROCMASK + /* avoid socket leak on alarm signal during connect(2) */ + sigfillset(&allsigs); + sigprocmask(SIG_BLOCK, &allsigs, NULL); +#endif /* HAVE_SIGPROCMASK */ + i = -1; for (ai = ai0; ai; ai = ai->ai_next) { i = socket(ai->ai_family, ai->ai_socktype, 0); @@ -294,6 +322,11 @@ int SockOpen(const char *host, const char *service, const char *options, } break; } + +#ifdef HAVE_SIGPROCMASK + sigprocmask(SIG_UNBLOCK, &allsigs, NULL); +#endif /* HAVE_SIGPROCMASK */ + #endif #endif /* NET_SECURITY */ -- cgit v1.2.3