]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Enhance and use POSIX socket compatibility layer (#2046) auto master
authorFrancesco Chemolli <5175948+kinkie@users.noreply.github.com>
Thu, 31 Jul 2025 18:01:47 +0000 (18:01 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Thu, 31 Jul 2025 22:09:30 +0000 (22:09 +0000)
Implement portable wrappers around most socket-related functions, named
x[function], and use them in all callsites.

winsock's socket-related functions are very similar but not
identical to the POSIX standard. Make the mswindows
compatibility layer available to MinGW, and modernize it.

Error highlighting the issue:
```
TcpAcceptor.cc: In member function
  'bool Comm::TcpAcceptor::acceptInto(Comm::ConnectionPointer&)':
TcpAcceptor.cc:352:17: error:
  comparison of unsigned expression in '< 0'
  is always false [-Werror=type-limits]
  352 |     if (rawSock < 0) {
```

65 files changed:
compat/Makefile.am
compat/mswindows.cc
compat/netdb.cc [new file with mode: 0644]
compat/netdb.h [new file with mode: 0644]
compat/os/mswindows.h
compat/select.cc [new file with mode: 0644]
compat/select.h [new file with mode: 0644]
compat/socket.cc [new file with mode: 0644]
compat/socket.h [new file with mode: 0644]
compat/unistd.cc [new file with mode: 0644]
compat/unistd.h [new file with mode: 0644]
compat/wserrno.cc [new file with mode: 0644]
compat/wserrno.h [new file with mode: 0644]
src/DiskIO/DiskDaemon/DiskdIOStrategy.cc
src/DiskIO/DiskDaemon/diskd.cc
src/DiskIO/DiskThreads/CommIO.cc
src/DiskIO/DiskThreads/aiops.cc
src/DiskIO/DiskThreads/aiops_win32.cc
src/adaptation/icap/ServiceRep.cc
src/auth/basic/RADIUS/basic_radius_auth.cc
src/auth/basic/RADIUS/radius-util.cc
src/auth/negotiate/kerberos/negotiate_kerberos_auth.cc
src/auth/negotiate/wrapper/negotiate_wrapper.cc
src/base/File.cc
src/cache_cf.cc
src/client_side.cc
src/clients/FtpGateway.cc
src/comm.cc
src/comm/ConnOpener.cc
src/comm/ModDevPoll.cc
src/comm/ModSelect.cc
src/comm/Tcp.cc
src/comm/TcpAcceptor.cc
src/eui/Eui48.cc
src/fd.cc
src/fs/rock/RockRebuild.cc
src/fs/rock/RockSwapDir.cc
src/fs_io.cc
src/icmp/Icmp.cc
src/icmp/Icmp4.cc
src/icmp/Icmp6.cc
src/icmp/IcmpPinger.cc
src/icmp/IcmpSquid.cc
src/icmp/pinger.cc
src/ip/Intercept.cc
src/ip/QosConfig.cc
src/ip/tools.cc
src/ipc.cc
src/ipc/Coordinator.cc
src/ipc/UdsOp.cc
src/ipc/mem/Segment.cc
src/ipc_win32.cc
src/log/ModUdp.cc
src/log/file/log_file_daemon.cc
src/main.cc
src/mgr/Action.cc
src/multicast.cc
src/neighbors.cc
src/security/cert_generators/file/certificate_db.cc
src/tests/testIpAddress.cc
src/tools.cc
src/unlinkd.cc
src/unlinkd_daemon.cc
src/wccp.cc
src/wccp2.cc

index 2581e81a3ab6964ca1ed8ea1cc4e4be8b11b64b5..f7875117d0c889f448aae7afaea69312fc13d214 100644 (file)
@@ -45,6 +45,8 @@ libcompatsquid_la_SOURCES = \
        memrchr.cc \
        memrchr.h \
        mswindows.cc \
+       netdb.cc \
+       netdb.h \
        os/aix.h \
        os/android.h \
        os/dragonfly.h \
@@ -63,8 +65,12 @@ libcompatsquid_la_SOURCES = \
        os/sunos.h \
        osdetect.h \
        pipe.h \
+       select.cc \
+       select.h \
        shm.cc \
        shm.h \
+       socket.cc \
+       socket.h \
        statvfs.cc \
        statvfs.h \
        stdio.h \
@@ -72,9 +78,13 @@ libcompatsquid_la_SOURCES = \
        strtoll.h \
        tempnam.h \
        types.h \
+       unistd.cc \
+       unistd.h \
        valgrind.h \
        win32_maperror.cc \
        win32_maperror.h \
+       wserrno.cc \
+       wserrno.h \
        xalloc.cc \
        xalloc.h \
        xis.h \
index 4d04023e65d45d09dd17a82f27c0782c4ddf34c1..5d24b04a96726a192cefe2518db25b6d56dd7be5 100644 (file)
@@ -11,6 +11,8 @@
 
 #include "squid.h"
 
+#include "compat/unistd.h"
+
 // The following code section is part of an EXPERIMENTAL native Windows NT/2000 Squid port.
 // Compiles only on MS Visual C++
 // CygWin appears not to need any of these
@@ -156,10 +158,9 @@ WIN32_ftruncate(int fd, off_t size)
 int
 WIN32_truncate(const char *pathname, off_t length)
 {
-    int fd;
     int res = -1;
 
-    fd = open(pathname, O_RDWR);
+    const auto fd = xopen(pathname, O_RDWR);
 
     if (fd == -1)
         errno = EBADF;
diff --git a/compat/netdb.cc b/compat/netdb.cc
new file mode 100644 (file)
index 0000000..9f0aa28
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
+#include "squid.h"
+
+#if _SQUID_WINDOWS_ || _SQUID_MINGW_
+
+#include "compat/netdb.h"
+#include "compat/wserrno.h"
+
+struct hostent *
+xgethostbyname(const char * const name)
+{
+    const auto result = gethostbyname(name);
+    if (!result)
+        SetErrnoFromWsaError();
+    return result;
+}
+
+struct servent *
+xgetservbyname(const char * const name, const char * const proto)
+{
+    const auto result = getservbyname(name, proto);
+    if (!result)
+        SetErrnoFromWsaError();
+    return result;
+}
+
+#endif /* _SQUID_WINDOWS_ || _SQUID_MINGW_ */
diff --git a/compat/netdb.h b/compat/netdb.h
new file mode 100644 (file)
index 0000000..fa912d5
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
+#ifndef SQUID_COMPAT_NETDB_H
+#define SQUID_COMPAT_NETDB_H
+
+#if HAVE_NETDB_H
+#include <netdb.h>
+#endif
+
+/// POSIX gethostbyname(3) equivalent
+struct hostent * xgethostbyname(const char * name);
+
+/// POSIX getservbyname(3) equivalent
+struct servent * xgetservbyname(const char * name, const char * proto);
+
+#if !(_SQUID_WINDOWS_ || _SQUID_MINGW_)
+
+inline struct hostent *
+xgethostbyname(const char *name)
+{
+    return gethostbyname(name);
+}
+
+inline struct servent *
+xgetservbyname(const char *name, const char *proto)
+{
+    return getservbyname(name, proto);
+}
+
+#endif /* !(_SQUID_WINDOWS_ || _SQUID_MINGW_) */
+#endif /* SQUID_COMPAT_NETDB_H */
index 1c584855bd636fbec445df650e378d88eabda6d4..9770d9bad0672dbc0cbcc45be6b6d347fbe37b57 100644 (file)
@@ -280,10 +280,6 @@ struct timezone {
 
 #include <io.h>
 
-#ifndef _PATH_DEVNULL
-#define _PATH_DEVNULL "NUL"
-#endif
-
 #ifndef EISCONN
 #define EISCONN WSAEISCONN
 #endif
@@ -392,69 +388,10 @@ SQUIDCEXTERN THREADLOCAL int ws32_result;
 
 #if defined(__cplusplus)
 
-inline int
-close(int fd)
-{
-    char l_so_type[sizeof(int)];
-    int l_so_type_siz = sizeof(l_so_type);
-    SOCKET sock = _get_osfhandle(fd);
-
-    if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0) {
-        int result = 0;
-        if (closesocket(sock) == SOCKET_ERROR) {
-            errno = WSAGetLastError();
-            result = 1;
-        }
-        _free_osfhnd(fd);
-        _osfile(fd) = 0;
-        return result;
-    } else
-        return _close(fd);
-}
-
 #if defined(_MSC_VER) /* Microsoft C Compiler ONLY */
 
-#ifndef _S_IREAD
-#define _S_IREAD 0x0100
 #endif
 
-#ifndef _S_IWRITE
-#define _S_IWRITE 0x0080
-#endif
-
-inline int
-open(const char *filename, int oflag, int pmode = 0)
-{
-    return _open(filename, oflag, pmode & (_S_IREAD | _S_IWRITE));
-}
-#endif
-
-inline int
-read(int fd, void * buf, size_t siz)
-{
-    char l_so_type[sizeof(int)];
-    int l_so_type_siz = sizeof(l_so_type);
-    SOCKET sock = _get_osfhandle(fd);
-
-    if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0)
-        return ::recv(sock, (char FAR *) buf, (int)siz, 0);
-    else
-        return _read(fd, buf, (unsigned int)siz);
-}
-
-inline int
-write(int fd, const void * buf, size_t siz)
-{
-    char l_so_type[sizeof(int)];
-    int l_so_type_siz = sizeof(l_so_type);
-    SOCKET sock = _get_osfhandle(fd);
-
-    if (::getsockopt(sock, SOL_SOCKET, SO_TYPE, l_so_type, &l_so_type_siz) == 0)
-        return ::send(sock, (char FAR *) buf, siz, 0);
-    else
-        return _write(fd, buf, siz);
-}
-
 // stdlib <functional> definitions are required before std API redefinitions.
 #include <functional>
 
@@ -475,107 +412,6 @@ namespace Squid
  * - map the FD value used by Squid to the socket handes used by Windows.
  */
 
-inline int
-accept(int s, struct sockaddr * a, socklen_t * l)
-{
-    SOCKET result;
-    if ((result = ::accept(_get_osfhandle(s), a, l)) == INVALID_SOCKET) {
-        if (WSAEMFILE == (errno = WSAGetLastError()))
-            errno = EMFILE;
-        return -1;
-    } else
-        return _open_osfhandle(result, 0);
-}
-#define accept(s,a,l) Squid::accept(s,a,reinterpret_cast<socklen_t*>(l))
-
-inline int
-bind(int s, const struct sockaddr * n, socklen_t l)
-{
-    if (::bind(_get_osfhandle(s),n,l) == SOCKET_ERROR) {
-        errno = WSAGetLastError();
-        return -1;
-    } else
-        return 0;
-}
-#define bind(s,n,l) Squid::bind(s,n,l)
-
-inline int
-connect(int s, const struct sockaddr * n, socklen_t l)
-{
-    if (::connect(_get_osfhandle(s),n,l) == SOCKET_ERROR) {
-        if (WSAEMFILE == (errno = WSAGetLastError()))
-            errno = EMFILE;
-        return -1;
-    } else
-        return 0;
-}
-#define connect(s,n,l) Squid::connect(s,n,l)
-
-inline struct hostent *
-gethostbyname(const char *n) {
-    HOSTENT FAR * result;
-    if ((result = ::gethostbyname(n)) == NULL)
-        errno = WSAGetLastError();
-    return result;
-}
-#define gethostbyname(n) Squid::gethostbyname(n)
-
-inline SERVENT FAR *
-getservbyname(const char * n, const char * p)
-{
-    SERVENT FAR * result;
-    if ((result = ::getservbyname(n, p)) == NULL)
-        errno = WSAGetLastError();
-    return result;
-}
-#define getservbyname(n,p) Squid::getservbyname(n,p)
-
-inline HOSTENT FAR *
-gethostbyaddr(const void * a, size_t l, int t)
-{
-    HOSTENT FAR * result;
-    if ((result = ::gethostbyaddr((const char*)a, l, t)) == NULL)
-        errno = WSAGetLastError();
-    return result;
-}
-#define gethostbyaddr(a,l,t) Squid::gethostbyaddr(a,l,t)
-
-inline int
-getsockname(int s, struct sockaddr * n, socklen_t * l)
-{
-    int i=*l;
-    if (::getsockname(_get_osfhandle(s), n, &i) == SOCKET_ERROR) {
-        errno = WSAGetLastError();
-        return -1;
-    } else
-        return 0;
-}
-#define getsockname(s,a,l) Squid::getsockname(s,a,reinterpret_cast<socklen_t*>(l))
-
-inline int
-gethostname(char * n, size_t l)
-{
-    if ((::gethostname(n, l)) == SOCKET_ERROR) {
-        errno = WSAGetLastError();
-        return -1;
-    } else
-        return 0;
-}
-#define gethostname(n,l) Squid::gethostname(n,l)
-
-inline int
-getsockopt(int s, int l, int o, void * v, socklen_t * n)
-{
-    Sleep(1);
-    if ((::getsockopt(_get_osfhandle(s), l, o,(char *) v, n)) == SOCKET_ERROR) {
-        errno = WSAGetLastError();
-        return -1;
-    } else
-        return 0;
-}
-#define getsockopt(s,l,o,v,n) Squid::getsockopt(s,l,o,v,n)
-
-/* Simple ioctl() emulation */
 inline int
 ioctl(int s, int c, void * a)
 {
@@ -598,94 +434,6 @@ ioctlsocket(int s, long c, u_long FAR * a)
 }
 #define ioctlsocket(s,c,a) Squid::ioctlsocket(s,c,a)
 
-inline int
-listen(int s, int b)
-{
-    if (::listen(_get_osfhandle(s), b) == SOCKET_ERROR) {
-        if (WSAEMFILE == (errno = WSAGetLastError()))
-            errno = EMFILE;
-        return -1;
-    } else
-        return 0;
-}
-#define listen(s,b) Squid::listen(s,b)
-
-inline ssize_t
-recv(int s, void * b, size_t l, int f)
-{
-    ssize_t result;
-    if ((result = ::recv(_get_osfhandle(s), (char *)b, l, f)) == SOCKET_ERROR) {
-        errno = WSAGetLastError();
-        return -1;
-    } else
-        return result;
-}
-#define recv(s,b,l,f) Squid::recv(s,b,l,f)
-
-inline ssize_t
-recvfrom(int s, void * b, size_t l, int f, struct sockaddr * fr, socklen_t * fl)
-{
-    ssize_t result;
-    int ifl=*fl;
-    if ((result = ::recvfrom(_get_osfhandle(s), (char *)b, l, f, fr, &ifl)) == SOCKET_ERROR) {
-        errno = WSAGetLastError();
-        return -1;
-    } else
-        return result;
-}
-#define recvfrom(s,b,l,f,r,n) Squid::recvfrom(s,b,l,f,r,reinterpret_cast<socklen_t*>(n))
-
-inline int
-select(int n, fd_set * r, fd_set * w, fd_set * e, struct timeval * t)
-{
-    int result;
-    if ((result = ::select(n,r,w,e,t)) == SOCKET_ERROR) {
-        errno = WSAGetLastError();
-        return -1;
-    } else
-        return result;
-}
-#define select(n,r,w,e,t) Squid::select(n,r,w,e,t)
-
-inline ssize_t
-send(int s, const char * b, size_t l, int f)
-{
-    ssize_t result;
-    if ((result = ::send(_get_osfhandle(s), b, l, f)) == SOCKET_ERROR) {
-        errno = WSAGetLastError();
-        return -1;
-    } else
-        return result;
-}
-#define send(s,b,l,f) Squid::send(s,reinterpret_cast<const char*>(b),l,f)
-
-inline ssize_t
-sendto(int s, const void * b, size_t l, int f, const struct sockaddr * t, socklen_t tl)
-{
-    ssize_t result;
-    if ((result = ::sendto(_get_osfhandle(s), (char *)b, l, f, t, tl)) == SOCKET_ERROR) {
-        errno = WSAGetLastError();
-        return -1;
-    } else
-        return result;
-}
-#define sendto(a,b,l,f,t,n) Squid::sendto(a,b,l,f,t,n)
-
-inline int
-setsockopt(SOCKET s, int l, int o, const void * v, socklen_t n)
-{
-    SOCKET socket;
-
-    socket = ((s == INVALID_SOCKET) ? s : (SOCKET)_get_osfhandle((int)s));
-
-    if (::setsockopt(socket, l, o, (const char *)v, n) == SOCKET_ERROR) {
-        errno = WSAGetLastError();
-        return -1;
-    } else
-        return 0;
-}
-#define setsockopt(s,l,o,v,n) Squid::setsockopt(s,l,o,v,n)
-
 inline int
 shutdown(int s, int h)
 {
@@ -697,30 +445,6 @@ shutdown(int s, int h)
 }
 #define shutdown(s,h) Squid::shutdown(s,h)
 
-inline int
-socket(int f, int t, int p)
-{
-    SOCKET result;
-    if ((result = ::socket(f, t, p)) == INVALID_SOCKET) {
-        if (WSAEMFILE == (errno = WSAGetLastError()))
-            errno = EMFILE;
-        return -1;
-    } else
-        return _open_osfhandle(result, 0);
-}
-#define socket(f,t,p) Squid::socket(f,t,p)
-
-inline int
-WSAAsyncSelect(int s, HWND h, unsigned int w, long e)
-{
-    if (::WSAAsyncSelect(_get_osfhandle(s), h, w, e) == SOCKET_ERROR) {
-        errno = WSAGetLastError();
-        return -1;
-    } else
-        return 0;
-}
-#define WSAAsyncSelect(s,h,w,e) Squid::WSAAsyncSelect(s,h,w,e)
-
 #undef WSADuplicateSocket
 inline int
 WSADuplicateSocket(int s, DWORD n, LPWSAPROTOCOL_INFO l)
@@ -758,28 +482,6 @@ WSASocket(int a, int t, int p, LPWSAPROTOCOL_INFO i, GROUP g, DWORD f)
 } /* namespace Squid */
 
 #else /* #ifdef __cplusplus */
-#define connect(s,n,l) \
-    (SOCKET_ERROR == connect(_get_osfhandle(s),n,l) ? \
-    (WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1, -1) : 0)
-#define gethostbyname(n) \
-    (NULL == ((HOSTENT FAR*)(ws32_result = (int)gethostbyname(n))) ? \
-    (errno = WSAGetLastError()), (HOSTENT FAR*)NULL : (HOSTENT FAR*)ws32_result)
-#define gethostname(n,l) \
-    (SOCKET_ERROR == gethostname(n,l) ? \
-    (errno = WSAGetLastError()), -1 : 0)
-#define recv(s,b,l,f) \
-    (SOCKET_ERROR == (ws32_result = recv(_get_osfhandle(s),b,l,f)) ? \
-    (errno = WSAGetLastError()), -1 : ws32_result)
-#define sendto(s,b,l,f,t,tl) \
-    (SOCKET_ERROR == (ws32_result = sendto(_get_osfhandle(s),b,l,f,t,tl)) ? \
-    (errno = WSAGetLastError()), -1 : ws32_result)
-#define select(n,r,w,e,t) \
-    (SOCKET_ERROR == (ws32_result = select(n,r,w,e,t)) ? \
-    (errno = WSAGetLastError()), -1 : ws32_result)
-#define socket(f,t,p) \
-    (INVALID_SOCKET == ((SOCKET)(ws32_result = (int)socket(f,t,p))) ? \
-    ((WSAEMFILE == (errno = WSAGetLastError()) ? errno = EMFILE : -1), -1) : \
-    (SOCKET)_open_osfhandle(ws32_result,0))
 #define write      _write /* Needed in util.c */
 #define open       _open /* Needed in win32lib.c */
 #endif /* #ifdef __cplusplus */
diff --git a/compat/select.cc b/compat/select.cc
new file mode 100644 (file)
index 0000000..f2db6db
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
+#include "squid.h"
+#include "compat/select.h"
+#include "compat/wserrno.h"
+
+#if _SQUID_WINDOWS_ || _SQUID_MINGW_
+
+int
+xselect(int nfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, struct timeval * timeout)
+{
+    const auto result = select(nfds, readfds, writefds, exceptfds, timeout);
+    if (result == SOCKET_ERROR)
+        SetErrnoFromWsaError();
+    return result;
+}
+
+#endif /* _SQUID_WINDOWS_ || _SQUID_MINGW_ */
diff --git a/compat/select.h b/compat/select.h
new file mode 100644 (file)
index 0000000..a6ef510
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
+#ifndef SQUID_COMPAT_SELECT_H
+#define SQUID_COMPAT_SELECT_H
+
+#if HAVE_SYS_SELECT_H
+#include <sys/select.h>
+#endif
+
+/// POSIX select(2) equivalent
+int xselect(int nfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, struct timeval * timeout);
+
+#if !(_SQUID_WINDOWS_ || _SQUID_MINGW_)
+
+inline int
+xselect(int nfds, fd_set * readfds, fd_set * writefds, fd_set * exceptfds, struct timeval * timeout)
+{
+    return select(nfds, readfds, writefds, exceptfds, timeout);
+}
+
+#endif /* !(_SQUID_WINDOWS_ || _SQUID_MINGW_) */
+
+#endif /* SQUID_COMPAT_SELECT_H */
diff --git a/compat/socket.cc b/compat/socket.cc
new file mode 100644 (file)
index 0000000..16bca10
--- /dev/null
@@ -0,0 +1,233 @@
+/*
+ * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
+#include "squid.h"
+#include "compat/socket.h"
+#include "compat/wserrno.h"
+
+#if _SQUID_WINDOWS_ || _SQUID_MINGW_
+#include <unordered_map>
+
+int
+xaccept(int socketFd, struct sockaddr *sa, socklen_t *saLength)
+{
+    const auto handle = _get_osfhandle(socketFd);
+    if (!isValidSocketHandle(handle)) {
+        // errno is already set by _get_osfhandle()
+        return SOCKET_ERROR;
+    }
+    int al = 0;
+    int *alp = nullptr;
+    if (saLength) {
+        assert(*saLength <= INT_MAX);
+        al = static_cast<int>(*saLength);
+        alp = &al;
+    }
+    const auto result = accept(handle, sa, alp);
+    if (result == INVALID_SOCKET) {
+        SetErrnoFromWsaError();
+        return SOCKET_ERROR;
+    }
+    const auto rv = _open_osfhandle(result, 0);
+    if (rv == -1)
+        errno = EBADF;
+    if (saLength)
+        *saLength = static_cast<socklen_t>(al);
+    return rv;
+}
+
+int
+xbind(int socketFd, const struct sockaddr *sa, socklen_t saLength)
+{
+    const auto handle = _get_osfhandle(socketFd);
+    if (!isValidSocketHandle(handle)) {
+        // errno is already set by _get_osfhandle()
+        return SOCKET_ERROR;
+    }
+    assert(saLength <= INT_MAX);
+    const auto result = bind(handle, sa, static_cast<int>(saLength));
+    if (result == SOCKET_ERROR)
+        SetErrnoFromWsaError();
+    return result;
+}
+
+int
+xconnect(int socketFd, const struct sockaddr *sa, socklen_t saLength)
+{
+    const auto handle = _get_osfhandle(socketFd);
+    if (!isValidSocketHandle(handle)) {
+        // errno is already set by _get_osfhandle()
+        return SOCKET_ERROR;
+    }
+    assert(saLength <= INT_MAX);
+    const auto result = connect(handle, sa, static_cast<int>(saLength));
+    if (result == SOCKET_ERROR)
+        SetErrnoFromWsaError();
+    return result;
+}
+
+int
+xgetsockname(int socketFd, struct sockaddr * sa, socklen_t * saLength)
+{
+    const auto handle = _get_osfhandle(socketFd);
+    if (!isValidSocketHandle(handle)) {
+        // errno is already set by _get_osfhandle()
+        return SOCKET_ERROR;
+    }
+    int al = 0;
+    int *alp = nullptr;
+    if (saLength) {
+        assert(*saLength <= INT_MAX);
+        al = static_cast<int>(*saLength);
+        alp = &al;
+    }
+    const auto result = getsockname(handle, sa, alp);
+    if (result == SOCKET_ERROR)
+        SetErrnoFromWsaError();
+    if (saLength)
+        *saLength = static_cast<socklen_t>(al);
+    return result;
+}
+
+int
+xgetsockopt(int socketFd, int level, int optionName, void * optionValue, socklen_t * optionLength)
+{
+    const auto handle = _get_osfhandle(socketFd);
+    if (!isValidSocketHandle(handle)) {
+        // errno is already set by _get_osfhandle()
+        return SOCKET_ERROR;
+    }
+    int ol = 0;
+    int *olp = nullptr;
+    if (optionLength) {
+        assert(*optionLength <= INT_MAX);
+        ol = static_cast<int>(*optionLength);
+        olp = &ol;
+    }
+    const auto result = getsockopt(handle, level, optionName, static_cast<char *>(optionValue), olp);
+    if (result == SOCKET_ERROR)
+        SetErrnoFromWsaError();
+    if (optionLength)
+        *optionLength = static_cast<socklen_t>(ol);
+    return result;
+}
+
+int
+xlisten(int socketFd, int backlog)
+{
+    const auto handle = _get_osfhandle(socketFd);
+    if (!isValidSocketHandle(handle)) {
+        // errno is already set by _get_osfhandle()
+        return SOCKET_ERROR;
+    }
+    const auto result = listen(handle, backlog);
+    if (result == SOCKET_ERROR)
+        SetErrnoFromWsaError();
+    return result;
+}
+
+ssize_t
+xrecv(int socketFd, void * buf, size_t bufLength, int flags)
+{
+    const auto handle = _get_osfhandle(socketFd);
+    if (!isValidSocketHandle(handle)) {
+        // errno is already set by _get_osfhandle()
+        return SOCKET_ERROR;
+    }
+    assert(bufLength <= INT_MAX);
+    const auto result = recv(handle, static_cast<char *>(buf), static_cast<int>(bufLength), flags);
+    if (result == SOCKET_ERROR)
+        SetErrnoFromWsaError();
+    return result;
+}
+
+ssize_t
+xrecvfrom(int socketFd, void * buf, size_t bufLength, int flags, struct sockaddr * from, socklen_t * fromLength)
+{
+    const auto handle = _get_osfhandle(socketFd);
+    if (!isValidSocketHandle(handle)) {
+        // errno is already set by _get_osfhandle()
+        return SOCKET_ERROR;
+    }
+    assert(bufLength <= INT_MAX);
+    int fl = 0;
+    int *flp = nullptr;
+    if (fromLength) {
+        assert(*fromLength <= INT_MAX);
+        fl = static_cast<int>(*fromLength);
+        flp = &fl;
+    }
+    const auto result = recvfrom(handle, static_cast<char *>(buf), static_cast<int>(bufLength), flags, from, flp);
+    if (result == SOCKET_ERROR)
+        SetErrnoFromWsaError();
+    if (fromLength)
+        *fromLength = static_cast<socklen_t>(fl);
+    return result;
+}
+
+ssize_t
+xsend(int socketFd, const void * buf, size_t bufLength, int flags)
+{
+    const auto handle = _get_osfhandle(socketFd);
+    if (!isValidSocketHandle(handle)) {
+        // errno is already set by _get_osfhandle()
+        return SOCKET_ERROR;
+    }
+    assert(bufLength <= INT_MAX);
+    const auto result = send(handle, static_cast<const char *>(buf), static_cast<int>(bufLength), flags);
+    if (result == SOCKET_ERROR)
+        SetErrnoFromWsaError();
+    return result;
+}
+
+ssize_t
+xsendto(int socketFd, const void * buf, size_t bufLength, int flags, const struct sockaddr * to, socklen_t toLength)
+{
+    const auto handle = _get_osfhandle(socketFd);
+    if (!isValidSocketHandle(handle)) {
+        // errno is already set by _get_osfhandle()
+        return SOCKET_ERROR;
+    }
+    assert(bufLength <= INT_MAX);
+    assert(toLength <= INT_MAX);
+    const auto result = sendto(handle, static_cast<const char *>(buf), static_cast<int>(bufLength), flags, to, static_cast<int>(toLength));
+    if (result == SOCKET_ERROR)
+        SetErrnoFromWsaError();
+    return result;
+}
+
+int
+xsetsockopt(int socketFd, int level, int option, const void *value, socklen_t valueLength)
+{
+    const auto handle = _get_osfhandle(socketFd);
+    if (!isValidSocketHandle(handle)) {
+        // errno is already set by _get_osfhandle()
+        return SOCKET_ERROR;
+    }
+    assert(option <= INT_MAX);
+    const auto result = setsockopt(handle, level, option, static_cast<const char *>(value), static_cast<int>(valueLength));
+    if (result == SOCKET_ERROR)
+        SetErrnoFromWsaError();
+    return result;
+}
+
+int
+xsocket(int domain, int type, int protocol)
+{
+    const auto result = socket(domain, type, protocol);
+    if (result == INVALID_SOCKET) {
+        SetErrnoFromWsaError();
+        return SOCKET_ERROR;
+    }
+    const auto rv = _open_osfhandle(result, 0);
+    if (rv == -1)
+        errno = EBADF;
+    return rv;
+}
+
+#endif /* _SQUID_WINDOWS_ || _SQUID_MINGW_ */
diff --git a/compat/socket.h b/compat/socket.h
new file mode 100644 (file)
index 0000000..0dbc377
--- /dev/null
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
+#ifndef SQUID_COMPAT_SOCKET_H
+#define SQUID_COMPAT_SOCKET_H
+
+#if HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+
+/// POSIX accept(2) equivalent
+int xaccept(int socketFd, struct sockaddr *sa, socklen_t *saLength);
+
+/// POSIX bind(2) equivalent
+int xbind(int socketFd, const struct sockaddr *sa, socklen_t saLength);
+
+/// POSIX connect(2) equivalent
+int xconnect(int socketFd, const struct sockaddr *sa, socklen_t saLength);
+
+/// POSIX getsockopt(2) equivalent
+int xgetsockopt(int socketFd, int level, int optionName, void * optionValue, socklen_t * optionLength);
+
+/// POSIX getsockname(2) equivalent
+int xgetsockname(int socketFd, struct sockaddr * sa, socklen_t * saLength);
+
+/// POSIX listen(2) equivalent
+int xlisten(int socketFd, int backlog);
+
+/// POSIX recv(2) equivalent
+ssize_t xrecv(int socketFd, void * buf, size_t bufLength, int flags);
+
+/// POSIX recvfrom(2) equivalent
+ssize_t xrecvfrom(int socketFd, void * buf, size_t bufLength, int flags, struct sockaddr * from, socklen_t * fromLength);
+
+/// POSIX send(2) equivalent
+ssize_t xsend(int socketFd, const void * buf, size_t bufLength, int flags);
+
+/// POSIX sendto(2) equivalent
+ssize_t xsendto(int socketFd, const void * buf, size_t bufLength, int flags, const struct sockaddr * to, socklen_t toLength);
+
+/// POSIX setsockopt(2) equivalent
+int xsetsockopt(int socketFd, int level, int option, const void * value, socklen_t valueLength);
+
+/// POSIX socket(2) equivalent
+int xsocket(int domain, int type, int protocol);
+
+// Solaris and possibly others lack MSG_NOSIGNAL optimization
+// TODO: move this into compat/? Use a dedicated compat file to avoid dragging
+// sys/socket.h into the rest of Squid??
+#ifndef MSG_NOSIGNAL
+#define MSG_NOSIGNAL 0
+#endif
+
+#if !(_SQUID_WINDOWS_ || _SQUID_MINGW_)
+
+inline int
+xaccept(int socketFd, struct sockaddr *sa, socklen_t *saLength)
+{
+    return accept(socketFd, sa, saLength);
+}
+
+inline int
+xbind(int socketFd, const struct sockaddr *sa, socklen_t saLength)
+{
+    return bind(socketFd, sa, saLength);
+}
+
+inline int
+xconnect(int socketFd, const struct sockaddr *sa, socklen_t saLength)
+{
+    return connect(socketFd, sa, saLength);
+}
+
+inline int
+xgetsockname(int socketFd, struct sockaddr * sa, socklen_t * saLength)
+{
+    return getsockname(socketFd, sa, saLength);
+}
+
+inline int
+xlisten(int socketFd, int backlog)
+{
+    return listen(socketFd, backlog);
+}
+
+inline int
+xgetsockopt(int socketFd, int level, int optionName, void * optionValue, socklen_t * optionLength)
+{
+    return getsockopt(socketFd, level, optionName, optionValue, optionLength);
+}
+
+inline ssize_t
+xrecv(int socketFd, void * buf, size_t bufLength, int flags)
+{
+    return recv(socketFd, buf, bufLength, flags);
+}
+
+inline ssize_t
+xrecvfrom(int socketFd, void * buf, size_t bufLength, int flags, struct sockaddr * from, socklen_t * fromLength)
+{
+    return recvfrom(socketFd, buf, bufLength, flags, from, fromLength);
+}
+
+inline ssize_t
+xsend(int socketFd, const void * buf, size_t bufLength, int flags)
+{
+    return send(socketFd, buf, bufLength, flags);
+}
+
+inline ssize_t
+xsendto(int socketFd, const void * buf, size_t bufLength, int flags, const struct sockaddr * to, socklen_t l)
+{
+    return sendto(socketFd, buf, bufLength, flags, to, l);
+}
+
+inline int
+xsetsockopt(int socketFd, int level, int option, const void *value, socklen_t valueLength)
+{
+    return setsockopt(socketFd, level, option, value, valueLength);
+}
+
+inline int
+xsocket(int domain, int type, int protocol)
+{
+    return socket(domain, type, protocol);
+}
+
+#else
+
+static_assert(SOCKET_ERROR == -1);
+
+inline bool
+isValidSocketHandle(intptr_t handle)
+{
+    return handle != intptr_t(INVALID_HANDLE_VALUE);
+}
+
+#endif /* !(_SQUID_WINDOWS_ || _SQUID_MINGW_) */
+
+#endif /* SQUID_COMPAT_SOCKET_H */
diff --git a/compat/unistd.cc b/compat/unistd.cc
new file mode 100644 (file)
index 0000000..f884fa0
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
+#include "squid.h"
+
+#if _SQUID_WINDOWS_ || _SQUID_MINGW_
+
+#include "compat/socket.h"
+#include "compat/unistd.h"
+#include "compat/wserrno.h"
+
+// 2025 MinGW and pre-2022 MSVC do not define these
+#ifndef _S_IREAD
+#define _S_IREAD 0x0100
+#endif
+
+#ifndef _S_IWRITE
+#define _S_IWRITE 0x0080
+#endif
+
+/// returns true if handle is a valid socket. Preserves errno.
+static bool
+isSocket(intptr_t handle)
+{
+    if (!isValidSocketHandle(handle)) {
+        // isValidSocketHandle does not touch errno
+        return false;
+    }
+
+    int value = 0;
+    int valueSize = sizeof(value);
+    const auto savedErrno = errno;
+    // use Windows API directly
+    const auto result = (getsockopt(handle, SOL_SOCKET, SO_TYPE, reinterpret_cast<char *>(&value), &valueSize) == 0);
+    errno = savedErrno;
+    return result;
+}
+
+int
+xclose(int fd)
+{
+    const auto sock = _get_osfhandle(fd);
+    if (sock == intptr_t(INVALID_HANDLE_VALUE)) {
+        // errno is already set by _get_osfhandle()
+        return -1;
+    }
+
+    if (isSocket(sock)) {
+        const auto result = closesocket(sock);
+        if (result == SOCKET_ERROR)
+            SetErrnoFromWsaError();
+        return result;
+    } else {
+        const auto result = _close(fd);
+        if (result)
+            SetErrnoFromWsaError();
+        return result;
+    }
+}
+
+int
+xgethostname(char *name, size_t nameLength)
+{
+    assert(nameLength <= INT_MAX);
+    const auto result = gethostname(name, static_cast<int>(nameLength));
+    if (result == SOCKET_ERROR)
+        SetErrnoFromWsaError();
+    return result;
+}
+
+int
+xopen(const char *filename, int oflag, int pmode)
+{
+    return _open(filename, oflag, pmode & (_S_IREAD | _S_IWRITE));
+}
+
+int
+xread(int fd, void * buf, size_t bufSize)
+{
+    const auto sock = _get_osfhandle(fd);
+    if (sock == intptr_t(INVALID_HANDLE_VALUE)) {
+        // errno is already set by _get_osfhandle()
+        return -1;
+    }
+
+    assert(bufSize <= UINT_MAX);
+    if (isSocket(sock))
+        return xrecv(sock, buf, bufSize, 0);
+    else
+        return _read(fd, buf, static_cast<unsigned int>(bufSize));
+}
+
+int
+xwrite(int fd, const void * buf, size_t bufSize)
+{
+    const auto sock = _get_osfhandle(fd);
+    if (sock == intptr_t(INVALID_HANDLE_VALUE)) {
+        // errno is already set by _get_osfhandle()
+        return -1;
+    }
+
+    assert(bufSize <= UINT_MAX);
+    if (isSocket(sock))
+        return xsend(sock, buf, bufSize, 0);
+    else
+        return _write(fd, buf, static_cast<unsigned int>(bufSize));
+}
+
+#endif /* _SQUID_WINDOWS_ || _SQUID_MINGW_ */
diff --git a/compat/unistd.h b/compat/unistd.h
new file mode 100644 (file)
index 0000000..5e39da4
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
+#ifndef SQUID_COMPAT_UNISTD_H
+#define SQUID_COMPAT_UNISTD_H
+
+#if HAVE_PATHS_H
+#include <paths.h>
+#endif
+#if HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+/// POSIX close(2) equivalent
+int xclose(int fd);
+
+/// POSIX gethostname(2) equivalent
+int xgethostname(char *name, size_t nameLength);
+
+/// POSIX open(2) equivalent
+int xopen(const char *filename, int oflag, int pmode = 0);
+
+/// POSIX read(2) equivalent
+int xread(int fd, void * buf, size_t bufSize);
+
+/// POSIX write(2) equivalent
+int xwrite(int fd, const void * buf, size_t bufSize);
+
+#if _SQUID_WINDOWS_ || _SQUID_MINGW_
+
+#if !defined(_PATH_DEVNULL)
+#define _PATH_DEVNULL "NUL"
+#endif
+
+#else /* _SQUID_WINDOWS_ || _SQUID_MINGW_ */
+
+inline int
+xclose(int fd)
+{
+    return close(fd);
+}
+
+inline int
+xgethostname(char *name, size_t nameLength)
+{
+    return gethostname(name, nameLength);
+}
+
+inline int
+xopen(const char *filename, int oflag, int pmode)
+{
+    return open(filename, oflag, pmode);
+}
+
+inline int
+xread(int fd, void * buf, size_t bufSize)
+{
+    return read(fd, buf, bufSize);
+}
+
+inline int
+xwrite(int fd, const void * buf, size_t bufSize)
+{
+    return write(fd, buf, bufSize);
+}
+
+#endif /* _SQUID_WINDOWS_ || _SQUID_MINGW_ */
+#endif /* SQUID_COMPAT_UNISTD_H */
diff --git a/compat/wserrno.cc b/compat/wserrno.cc
new file mode 100644 (file)
index 0000000..47222cf
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
+#include "squid.h"
+#include "compat/wserrno.h"
+
+#if _SQUID_WINDOWS_ || _SQUID_MINGW_
+
+#include <unordered_map>
+
+void
+SetErrnoFromWsaError()
+{
+    // POSIX codes which socket API users may care about
+    static const auto *CodeMap = new std::unordered_map<int, int> {
+        { WSAECONNABORTED, ECONNABORTED },
+        { WSAEINPROGRESS, EINPROGRESS },
+        { WSAEAFNOSUPPORT, EAFNOSUPPORT },
+        { WSAEINVAL, EINVAL },
+        { WSAEISCONN, EISCONN },
+        { WSAEWOULDBLOCK, EWOULDBLOCK },
+        // no Windows error code maps to EAGAIN
+        { WSAEALREADY, EALREADY },
+        { WSAEINTR, EINTR },
+        // no Windows error code maps to ERESTART
+        { WSAEMFILE, EMFILE },
+        // no Windows error code maps to ENFILE
+        { WSAECONNRESET, ECONNRESET }
+    };
+
+    const auto wsa = WSAGetLastError();
+    const auto itr = CodeMap->find(wsa);
+    if (itr != CodeMap->cend())
+        errno = itr->second;
+    else
+        errno = wsa;
+}
+
+#endif /* _SQUID_WINDOWS_ || _SQUID_MINGW_ */
diff --git a/compat/wserrno.h b/compat/wserrno.h
new file mode 100644 (file)
index 0000000..b473586
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 1996-2025 The Squid Software Foundation and contributors
+ *
+ * Squid software is distributed under GPLv2+ license and includes
+ * contributions from numerous individuals and organizations.
+ * Please see the COPYING and CONTRIBUTORS files for details.
+ */
+
+#ifndef SQUID_COMPAT_WSERRNO_H
+#define SQUID_COMPAT_WSERRNO_H
+
+#if _SQUID_WINDOWS_ || _SQUID_MINGW_
+
+/**
+ * Squid socket code is written to handle POSIX errno codes.
+ * Set errno to the relevant POSIX or WSA code.
+ */
+void SetErrnoFromWsaError();
+
+#endif /* _SQUID_WINDOWS_ || _SQUID_MINGW_ */
+
+#endif /* SQUID_COMPAT_WSERRNO_H */
index 4eef0bf3df51c2b686236423aeba4376c9f6b7e8..dcc81107f569fc298caed0a3b6d1e25efb94d7f5 100644 (file)
@@ -11,6 +11,7 @@
 #include "squid.h"
 #include "comm.h"
 #include "comm/Loops.h"
+#include "compat/select.h"
 #include "ConfigOption.h"
 #include "diomsg.h"
 #include "DiskdFile.h"
@@ -409,7 +410,7 @@ DiskdIOStrategy::SEND(diomsg *M, int mtype, int id, size_t size, off_t offset, s
     struct timeval delay = {0, 1};
 
     while (away > magic2) {
-        select(0, nullptr, nullptr, nullptr, &delay);
+        xselect(0, nullptr, nullptr, nullptr, &delay);
         Store::Root().callback();
 
         if (delay.tv_usec < 1000000)
index cd2dee0c9176c8f13e03ef0755c88a5ba3fe9133..e63bcba802a15af7d1d6191314ad28ff04f36ec1 100644 (file)
@@ -9,6 +9,8 @@
 /* DEBUG: section --    External DISKD process implementation. */
 
 #include "squid.h"
+#include "compat/socket.h"
+#include "compat/unistd.h"
 #include "DiskIO/DiskDaemon/diomsg.h"
 #include "hash.h"
 
@@ -53,12 +55,11 @@ static int DebugLevel = 0;
 static int
 do_open(diomsg * r, int, const char *buf)
 {
-    int fd;
     file_state *fs;
     /*
      * note r->offset holds open() flags
      */
-    fd = open(buf, r->offset, 0600);
+    const auto fd = xopen(buf, r->offset, 0600);
 
     if (fd < 0) {
         DEBUG(1) {
@@ -111,13 +112,12 @@ do_close(diomsg * r, int)
                 fs);
     }
     xfree(fs);
-    return close(fd);
+    return xclose(fd);
 }
 
 static int
 do_read(diomsg * r, int, char *buf)
 {
-    int x;
     int readlen = r->size;
     file_state *fs;
     fs = (file_state *) hash_lookup(hash, &r->id);
@@ -145,7 +145,7 @@ do_read(diomsg * r, int, char *buf)
         }
     }
 
-    x = read(fs->fd, buf, readlen);
+    const auto x = xread(fs->fd, buf, readlen);
     DEBUG(2) {
         fprintf(stderr, "%d READ %d,%d,%" PRId64 " ret %d\n", (int) mypid,
                 fs->fd, readlen, (int64_t)r->offset, x);
@@ -168,7 +168,6 @@ static int
 do_write(diomsg * r, int, const char *buf)
 {
     int wrtlen = r->size;
-    int x;
     file_state *fs;
     fs = (file_state *) hash_lookup(hash, &r->id);
 
@@ -195,7 +194,7 @@ do_write(diomsg * r, int, const char *buf)
         fprintf(stderr, "%d WRITE %d,%d,%" PRId64 "\n", (int) mypid,
                 fs->fd, wrtlen, (int64_t)r->offset);
     }
-    x = write(fs->fd, buf, wrtlen);
+    const auto x = xwrite(fs->fd, buf, wrtlen);
 
     if (x < 0) {
         DEBUG(1) {
@@ -370,7 +369,7 @@ main(int argc, char *argv[])
 
         if (rlen < 0) {
             if (EINTR == errno) {
-                if (read(0, rbuf, 512) <= 0) {
+                if (xread(0, rbuf, 512) <= 0) {
                     if (EWOULDBLOCK == errno)
                         (void) 0;
                     else if (EAGAIN == errno)
index b3a5d6746bd1024d3f43f24832a1a13bfb7be203..c0558ab37b4d11818b0205df161fde9dc93f200c 100644 (file)
@@ -11,6 +11,7 @@
 #include "squid.h"
 #include "comm/Loops.h"
 #include "compat/pipe.h"
+#include "compat/unistd.h"
 #include "DiskIO/DiskThreads/CommIO.h"
 #include "fd.h"
 #include "globals.h"
@@ -40,8 +41,8 @@ CommIO::NotifyIOClose()
 {
     /* Close done pipe signal */
     FlushPipe();
-    close(DoneFD);
-    close(DoneReadFD);
+    xclose(DoneFD);
+    xclose(DoneReadFD);
     fd_close(DoneFD);
     fd_close(DoneReadFD);
     Initialized = false;
index 5be893a77a8e312553852279bda3a6785c4a3f66..b50be32f6dd48a4ed0b3f4b7bc59588ac0b43cec 100644 (file)
@@ -13,6 +13,8 @@
 #endif
 
 #include "squid.h"
+#include "compat/socket.h"
+#include "compat/unistd.h"
 #include "DiskIO/DiskThreads/CommIO.h"
 #include "DiskThreads.h"
 #include "SquidConfig.h"
@@ -578,7 +580,7 @@ squidaio_cleanup_request(squidaio_request_t * requestp)
     case _AIO_OP_OPEN:
         if (cancelled && requestp->ret >= 0)
             /* The open() was cancelled but completed */
-            close(requestp->ret);
+            xclose(requestp->ret);
 
         squidaio_xstrfree(requestp->path);
 
@@ -587,7 +589,7 @@ squidaio_cleanup_request(squidaio_request_t * requestp)
     case _AIO_OP_CLOSE:
         if (cancelled && requestp->ret < 0)
             /* The close() was cancelled and never got executed */
-            close(requestp->fd);
+            xclose(requestp->fd);
 
         break;
 
@@ -701,7 +703,7 @@ static void
 squidaio_do_read(squidaio_request_t * requestp)
 {
     if (lseek(requestp->fd, requestp->offset, requestp->whence) >= 0)
-        requestp->ret = read(requestp->fd, requestp->bufferp, requestp->buflen);
+        requestp->ret = xread(requestp->fd, requestp->bufferp, requestp->buflen);
     else
         requestp->ret = -1;
     requestp->err = errno;
@@ -740,7 +742,7 @@ squidaio_write(int fd, char *bufp, size_t bufs, off_t offset, int whence, squida
 static void
 squidaio_do_write(squidaio_request_t * requestp)
 {
-    requestp->ret = write(requestp->fd, requestp->bufferp, requestp->buflen);
+    requestp->ret = xwrite(requestp->fd, requestp->bufferp, requestp->buflen);
     requestp->err = errno;
 }
 
@@ -769,7 +771,7 @@ squidaio_close(int fd, squidaio_result_t * resultp)
 static void
 squidaio_do_close(squidaio_request_t * requestp)
 {
-    requestp->ret = close(requestp->fd);
+    requestp->ret = xclose(requestp->fd);
     requestp->err = errno;
 }
 
index 7c3a2070974fd95e621233c23cd378080ca626ea..8ce649574dbb41c4cc4025976e3a8e8cec712498 100644 (file)
@@ -9,6 +9,7 @@
 /* DEBUG: section 43    Windows AIOPS */
 
 #include "squid.h"
+#include "compat/unistd.h"
 #include "compat/win32_maperror.h"
 #include "DiskIO/DiskThreads/CommIO.h"
 #include "DiskThreads.h"
@@ -653,7 +654,7 @@ squidaio_cleanup_request(squidaio_request_t * requestp)
     case _AIO_OP_OPEN:
         if (cancelled && requestp->ret >= 0)
             /* The open() was cancelled but completed */
-            close(requestp->ret);
+            xclose(requestp->ret);
 
         squidaio_xstrfree(requestp->path);
 
@@ -662,7 +663,7 @@ squidaio_cleanup_request(squidaio_request_t * requestp)
     case _AIO_OP_CLOSE:
         if (cancelled && requestp->ret < 0)
             /* The close() was cancelled and never got executed */
-            close(requestp->fd);
+            xclose(requestp->fd);
 
         break;
 
@@ -853,9 +854,9 @@ squidaio_close(int fd, squidaio_result_t * resultp)
 static void
 squidaio_do_close(squidaio_request_t * requestp)
 {
-    if ((requestp->ret = close(requestp->fd)) < 0) {
+    if ((requestp->ret = xclose(requestp->fd)) < 0) {
         debugs(43, DBG_CRITICAL, "squidaio_do_close: FD " << requestp->fd << ", errno " << errno);
-        close(requestp->fd);
+        xclose(requestp->fd);
     }
 
     requestp->err = errno;
index a6e2bd0359f2ba1500f5671e911fef1f7db49cc9..2c63aa92540340d339f984ccf7389dd978560d09 100644 (file)
@@ -17,6 +17,7 @@
 #include "adaptation/icap/ServiceRep.h"
 #include "base/TextException.h"
 #include "comm/Connection.h"
+#include "compat/netdb.h"
 #include "ConfigParser.h"
 #include "debug/Stream.h"
 #include "fde.h"
@@ -66,9 +67,9 @@ Adaptation::Icap::ServiceRep::finalize()
     if (!have_port) {
         struct servent *serv;
         if (cfg().protocol.caseCmp("icaps") == 0)
-            serv = getservbyname("icaps", "tcp");
+            serv = xgetservbyname("icaps", "tcp");
         else
-            serv = getservbyname("icap", "tcp");
+            serv = xgetservbyname("icap", "tcp");
 
         if (serv) {
             writeableCfg().port = htons(serv->s_port);
index 72daa072f5a065b9890228ae77449f330c6acdf8..340faee37b95e19d8e8a673c58d3feb6a4fba46a 100644 (file)
 #include "auth/basic/RADIUS/radius-util.h"
 #include "auth/basic/RADIUS/radius.h"
 #include "base/Random.h"
+#include "compat/netdb.h"
+#include "compat/select.h"
+#include "compat/socket.h"
+#include "compat/unistd.h"
 #include "helper/protocol_defines.h"
 #include "md5.h"
 
 #include <cerrno>
 #include <cstring>
 #include <ctime>
-#if HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
 #if HAVE_NETINET_IN_H
 #include <netinet/in.h>
 #endif
-#if HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 #if HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
 #if _SQUID_WINDOWS_
 #include <io.h>
 #endif
-#if HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#if HAVE_NETDB_H
-#include <netdb.h>
-#endif
 #if HAVE_PWD_H
 #include <pwd.h>
 #endif
@@ -417,7 +409,7 @@ authenticate(int socket_fd, const char *username, const char *passwd)
          *    Send the request we've built.
          */
         gettimeofday(&sent, nullptr);
-        if (send(socket_fd, (char *) auth, total_length, 0) < 0) {
+        if (xsend(socket_fd, auth, total_length, 0) < 0) {
             int xerrno = errno;
             // EAGAIN is expected at high traffic, just retry
             // TODO: block/sleep a few ms to let the apparently full buffer drain ?
@@ -437,11 +429,11 @@ authenticate(int socket_fd, const char *username, const char *passwd)
             }
             FD_ZERO(&readfds);
             FD_SET(socket_fd, &readfds);
-            if (select(socket_fd + 1, &readfds, nullptr, nullptr, &tv) == 0)  /* Select timeout */
+            if (xselect(socket_fd + 1, &readfds, nullptr, nullptr, &tv) == 0)  /* Select timeout */
                 break;
             salen = sizeof(saremote);
-            len = recvfrom(socket_fd, recv_buffer, sizeof(i_recv_buffer),
-                           0, (struct sockaddr *) &saremote, &salen);
+            len = xrecvfrom(socket_fd, recv_buffer, sizeof(i_recv_buffer),
+                            0, (struct sockaddr *) &saremote, &salen);
 
             if (len < 0)
                 continue;
@@ -468,7 +460,6 @@ main(int argc, char **argv)
 {
     struct sockaddr_in salocal;
     struct sockaddr_in saremote;
-    struct servent *svp;
     unsigned short svc_port;
     char username[MAXPWNAM];
     char passwd[MAXPASS];
@@ -536,7 +527,7 @@ main(int argc, char **argv)
     /*
      *    Open a connection to the server.
      */
-    svp = getservbyname(svc_name, "udp");
+    const auto svp = xgetservbyname(svc_name, "udp");
     if (svp != nullptr)
         svc_port = ntohs((unsigned short) svp->s_port);
     else
@@ -549,7 +540,7 @@ main(int argc, char **argv)
         fprintf(stderr, "FATAL: %s: Couldn't find host %s\n", argv[0], server);
         exit(EXIT_FAILURE);
     }
-    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+    sockfd = xsocket(AF_INET, SOCK_DGRAM, 0);
     if (sockfd < 0) {
         perror("socket");
         exit(EXIT_FAILURE);
@@ -559,12 +550,12 @@ main(int argc, char **argv)
     saremote.sin_addr.s_addr = htonl(auth_ipaddr);
     saremote.sin_port = htons(svc_port);
 
-    if (connect(sockfd, (struct sockaddr *) &saremote, sizeof(saremote)) < 0) {
+    if (xconnect(sockfd, (struct sockaddr *) &saremote, sizeof(saremote)) < 0) {
         perror("connect");
         exit(EXIT_FAILURE);
     }
     salen = sizeof(salocal);
-    if (getsockname(sockfd, (struct sockaddr *) &salocal, &salen) < 0) {
+    if (xgetsockname(sockfd, (struct sockaddr *) &salocal, &salen) < 0) {
         perror("getsockname");
         exit(EXIT_FAILURE);
     }
@@ -612,7 +603,7 @@ main(int argc, char **argv)
 
         authenticate(sockfd, username, passwd);
     }
-    close(sockfd);
+    xclose(sockfd);
     return EXIT_SUCCESS;
 }
 
index 52bac5b3eee979043f66224b2fbb127ec134f486..67ac67540f553421db86c885c2af651634b1b088 100644 (file)
@@ -46,20 +46,16 @@ char util_sccsid[] =
 
 #include "squid.h"
 #include "auth/basic/RADIUS/radius-util.h"
+#include "compat/netdb.h"
+#include "compat/socket.h"
 #include "md5.h"
 
 #include <cctype>
 #include <csignal>
 #include <ctime>
-#if HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
 #if HAVE_NETINET_IN_H
 #include <netinet/in.h>
 #endif
-#if HAVE_NETDB_H
-#include <netdb.h>
-#endif
 #if HAVE_PWD_H
 #include <pwd.h>
 #endif
@@ -146,7 +142,7 @@ uint32_t get_ipaddr(char *host)
 
     if (good_ipaddr(host) == 0) {
         return(ipstr2long(host));
-    } else if ((hp = gethostbyname(host)) == (struct hostent *)nullptr) {
+    } else if (!(hp = xgethostbyname(host))) {
         return((uint32_t)0);
     }
     return(ntohl(*(uint32_t *)hp->h_addr));
index 1ac5a502a7718cba6ccda51e40f135308a2c096d..262f3498098666a3567cfdef1941379e69072fcc 100644 (file)
 
 #if HAVE_GSSAPI
 
+#include "compat/unistd.h"
 #include "negotiate_kerberos.h"
 
 #if HAVE_SYS_STAT_H
 #include "sys/stat.h"
 #endif
-#if HAVE_UNISTD_H
-#include "unistd.h"
-#endif
 
 #if HAVE_KRB5_MEMORY_KEYTAB
 typedef struct _krb5_kt_list {
@@ -97,7 +95,7 @@ gethost_name(void)
     struct addrinfo *hres = nullptr, *hres_list;
     int rc;
 
-    rc = gethostname(hostname, sizeof(hostname)-1);
+    rc = xgethostname(hostname, sizeof(hostname)-1);
     if (rc) {
         debug((char *) "%s| %s: ERROR: resolving hostname '%s' failed\n", LogTime(), PROGRAM, hostname);
         fprintf(stderr, "%s| %s: ERROR: resolving hostname '%s' failed\n",
index 3525aab9f8980afc4a918c7c0b9def64c5c24eda..2ac4e58201a0f16ce09f3ca1ed25b6485b500fae 100644 (file)
 #include "squid.h"
 #include "base64.h"
 #include "compat/pipe.h"
+#include "compat/unistd.h"
 
 #include <cerrno>
 #include <cstring>
 #include <cstdlib>
 #include <ctime>
-#if HAVE_NETDB_H
-#include <netdb.h>
-#endif
-#if HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 
 #if !defined(HAVE_DECL_XMALLOC) || !HAVE_DECL_XMALLOC
 #define xmalloc malloc
@@ -376,13 +371,13 @@ main(int argc, char *const argv[])
     if ( fpid == 0 ) {
         /* First Child for Kerberos helper */
 
-        close(pkin[1]);
+        xclose(pkin[1]);
         dup2(pkin[0],STDIN_FILENO);
-        close(pkin[0]);
+        xclose(pkin[0]);
 
-        close(pkout[0]);
+        xclose(pkout[0]);
         dup2(pkout[1],STDOUT_FILENO);
-        close(pkout[1]);
+        xclose(pkout[1]);
 
         setbuf(stdin, nullptr);
         setbuf(stdout, nullptr);
@@ -392,8 +387,8 @@ main(int argc, char *const argv[])
         exit(EXIT_FAILURE);
     }
 
-    close(pkin[0]);
-    close(pkout[1]);
+    xclose(pkin[0]);
+    xclose(pkout[1]);
 
     if (pipe(pnin) < 0) {
         fprintf(stderr, "%s| %s: Could not assign streams for pnin\n", LogTime(), PROGRAM);
@@ -412,13 +407,13 @@ main(int argc, char *const argv[])
     if ( fpid == 0 ) {
         /* Second Child for NTLM helper */
 
-        close(pnin[1]);
+        xclose(pnin[1]);
         dup2(pnin[0],STDIN_FILENO);
-        close(pnin[0]);
+        xclose(pnin[0]);
 
-        close(pnout[0]);
+        xclose(pnout[0]);
         dup2(pnout[1],STDOUT_FILENO);
-        close(pnout[1]);
+        xclose(pnout[1]);
 
         setbuf(stdin, nullptr);
         setbuf(stdout, nullptr);
@@ -428,8 +423,8 @@ main(int argc, char *const argv[])
         exit(EXIT_FAILURE);
     }
 
-    close(pnin[0]);
-    close(pnout[1]);
+    xclose(pnin[0]);
+    xclose(pnout[1]);
 
     FILE *FDKIN=fdopen(pkin[1],"w");
     FILE *FDKOUT=fdopen(pkout[0],"r");
index b2f638e978db33d2d971e39d4b520537579e26af..dc518be435b15996fd6c3e72b4555ceac9114dc4 100644 (file)
@@ -8,6 +8,8 @@
 
 #include "squid.h"
 #include "base/File.h"
+#include "compat/socket.h"
+#include "compat/unistd.h"
 #include "debug/Stream.h"
 #include "sbuf/Stream.h"
 #include "tools.h"
@@ -22,9 +24,6 @@
 #if HAVE_SYS_STAT_H
 #include <sys/stat.h>
 #endif
-#if HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 
 /* FileOpeningConfig */
 
@@ -182,7 +181,7 @@ File::open(const FileOpeningConfig &cfg)
     enter_suid();
     if (cfg.creationMask)
         oldCreationMask = umask(cfg.creationMask); // XXX: Why here? Should not this be set for the whole Squid?
-    fd_ = ::open(filename, cfg.openFlags, cfg.openMode);
+    fd_ = xopen(filename, cfg.openFlags, cfg.openMode);
     const auto savedErrno = errno;
     if (cfg.creationMask)
         umask(oldCreationMask);
@@ -203,7 +202,7 @@ File::close()
         debugs(54, DBG_IMPORTANT, sysCallFailure("CloseHandle", WindowsErrorMessage(savedError)));
     }
 #else
-    if (::close(fd_) != 0) {
+    if (xclose(fd_) != 0) {
         const auto savedErrno = errno;
         debugs(54, DBG_IMPORTANT, sysCallError("close", savedErrno));
     }
@@ -250,7 +249,7 @@ File::readSmall(const SBuf::size_type minBytes, const SBuf::size_type maxBytes)
         throw TexcHere(sysCallFailure("ReadFile", WindowsErrorMessage(savedError)));
     }
 #else
-    const auto result = ::read(fd_, rawBuf, readLimit);
+    const auto result = xread(fd_, rawBuf, readLimit);
     if (result < 0) {
         const auto savedErrno = errno;
         throw TexcHere(sysCallError("read", savedErrno));
@@ -287,7 +286,7 @@ File::writeAll(const SBuf &data)
     }
     const auto bytesWritten = static_cast<size_t>(nBytesWritten);
 #else
-    const auto result = ::write(fd_, data.rawContent(), data.length());
+    const auto result = xwrite(fd_, data.rawContent(), data.length());
     if (result < 0) {
         const auto savedErrno = errno;
         throw TexcHere(sysCallError("write", savedErrno));
index cc3631403d02ccfa2589dfa5cf36cacd515ad96d..da219101891f5825a708286e30cb01cd0c6a19f1 100644 (file)
@@ -28,6 +28,8 @@
 #include "cache_cf.h"
 #include "CachePeer.h"
 #include "CachePeers.h"
+#include "compat/netdb.h"
+#include "compat/socket.h"
 #include "ConfigOption.h"
 #include "ConfigParser.h"
 #include "CpuAffinityMap.h"
 #if HAVE_GRP_H
 #include <grp.h>
 #endif
-#if HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
 #if HAVE_SYS_STAT_H
 #include <sys/stat.h>
 #endif
@@ -2130,7 +2129,7 @@ GetService(const char *proto)
     }
     /** Returns either the service port number from /etc/services */
     if ( !isUnsignedNumeric(token, strlen(token)) )
-        port = getservbyname(token, proto);
+        port = xgetservbyname(token, proto);
     if (port != nullptr) {
         return ntohs((unsigned short)port->s_port);
     }
index 17d377dfefda771644703d1ea776e14584276858..d8507bd5e992d8c22e4072b9212d8c4e963a56c5 100644 (file)
@@ -78,6 +78,7 @@
 #include "comm/TcpAcceptor.h"
 #include "comm/Write.h"
 #include "CommCalls.h"
+#include "compat/socket.h"
 #include "debug/Messages.h"
 #include "error/ExceptionErrorDetail.h"
 #include "errorpage.h"
@@ -2134,7 +2135,7 @@ ConnStateData::start()
             (transparent() || port->disable_pmtu_discovery == DISABLE_PMTU_ALWAYS)) {
 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
         int i = IP_PMTUDISC_DONT;
-        if (setsockopt(clientConnection->fd, SOL_IP, IP_MTU_DISCOVER, &i, sizeof(i)) < 0) {
+        if (xsetsockopt(clientConnection->fd, SOL_IP, IP_MTU_DISCOVER, &i, sizeof(i)) < 0) {
             int xerrno = errno;
             debugs(33, 2, "WARNING: Path MTU discovery disabling failed on " << clientConnection << " : " << xstrerr(xerrno));
         }
index ddf96160293c9523da4f895b44380300e9432be6..4c4b59df4660e12e7ae39fc6b0bf2e90e84e582e 100644 (file)
@@ -18,6 +18,7 @@
 #include "comm/Read.h"
 #include "comm/TcpAcceptor.h"
 #include "CommCalls.h"
+#include "compat/socket.h"
 #include "compat/strtoll.h"
 #include "errorpage.h"
 #include "fd.h"
@@ -1777,8 +1778,8 @@ ftpOpenListenSocket(Ftp::Gateway * ftpState, int fallback)
     if (fallback) {
         int on = 1;
         errno = 0;
-        if (setsockopt(ftpState->ctrl.conn->fd, SOL_SOCKET, SO_REUSEADDR,
-                       (char *) &on, sizeof(on)) == -1) {
+        if (xsetsockopt(ftpState->ctrl.conn->fd, SOL_SOCKET, SO_REUSEADDR,
+                        &on, sizeof(on)) == -1) {
             int xerrno = errno;
             // SO_REUSEADDR is only an optimization, no need to be verbose about error
             debugs(9, 4, "setsockopt failed: " << xstrerr(xerrno));
index 4b88933c0150f2b7e119f0d7daa0b37fba221714..034c17760307084d4d72dc085035c9a1e972b69b 100644 (file)
@@ -21,6 +21,8 @@
 #include "comm/TcpAcceptor.h"
 #include "comm/Write.h"
 #include "compat/cmsg.h"
+#include "compat/socket.h"
+#include "compat/unistd.h"
 #include "DescriptorSet.h"
 #include "event.h"
 #include "fd.h"
@@ -129,7 +131,7 @@ comm_udp_recvfrom(int fd, void *buf, size_t len, int flags, Ip::Address &from)
     debugs(5,8, "comm_udp_recvfrom: FD " << fd << " from " << from);
     struct addrinfo *AI = nullptr;
     Ip::Address::InitAddr(AI);
-    int x = recvfrom(fd, buf, len, flags, AI->ai_addr, &AI->ai_addrlen);
+    int x = xrecvfrom(fd, buf, len, flags, AI->ai_addr, &AI->ai_addrlen);
     from = *AI;
     Ip::Address::FreeAddr(AI);
     return x;
@@ -145,7 +147,7 @@ comm_udp_recv(int fd, void *buf, size_t len, int flags)
 ssize_t
 comm_udp_send(int s, const void *buf, size_t len, int flags)
 {
-    return send(s, buf, len, flags);
+    return xsend(s, buf, len, flags);
 }
 
 bool
@@ -183,7 +185,7 @@ comm_local_port(int fd)
 
     Ip::Address::InitAddr(addr);
 
-    if (getsockname(fd, addr->ai_addr, &(addr->ai_addrlen)) ) {
+    if (xgetsockname(fd, addr->ai_addr, &(addr->ai_addrlen)) ) {
         int xerrno = errno;
         debugs(50, DBG_IMPORTANT, "ERROR: " << MYNAME << "Failed to retrieve TCP/UDP port number for socket: FD " << fd << ": " << xstrerr(xerrno));
         Ip::Address::FreeAddr(addr);
@@ -211,7 +213,7 @@ commSetBindAddressNoPort(const int fd)
 {
 #if defined(IP_BIND_ADDRESS_NO_PORT)
     int flag = 1;
-    if (setsockopt(fd, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, reinterpret_cast<char*>(&flag), sizeof(flag)) < 0) {
+    if (xsetsockopt(fd, IPPROTO_IP, IP_BIND_ADDRESS_NO_PORT, &flag, sizeof(flag)) < 0) {
         const auto savedErrno = errno;
         debugs(50, DBG_IMPORTANT, "ERROR: setsockopt(IP_BIND_ADDRESS_NO_PORT) failure: " << xstrerr(savedErrno));
     }
@@ -225,7 +227,7 @@ commBind(int s, struct addrinfo &inaddr)
 {
     ++ statCounter.syscalls.sock.binds;
 
-    if (bind(s, inaddr.ai_addr, inaddr.ai_addrlen) == 0) {
+    if (xbind(s, inaddr.ai_addr, inaddr.ai_addrlen) == 0) {
         debugs(50, 6, "bind socket FD " << s << " to " << fd_table[s].local_addr);
         return Comm::OK;
     }
@@ -294,7 +296,7 @@ static void
 comm_set_v6only(int fd, int tos)
 {
 #ifdef IPV6_V6ONLY
-    if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &tos, sizeof(int)) < 0) {
+    if (xsetsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &tos, sizeof(int)) < 0) {
         int xerrno = errno;
         debugs(50, DBG_IMPORTANT, MYNAME << "setsockopt(IPV6_V6ONLY) " << (tos?"ON":"OFF") << " for FD " << fd << ": " << xstrerr(xerrno));
     }
@@ -336,7 +338,7 @@ comm_set_transparent(int fd)
 
 #if defined(soLevel) && defined(soFlag)
     int tos = 1;
-    if (setsockopt(fd, soLevel, soFlag, (char *) &tos, sizeof(int)) < 0) {
+    if (xsetsockopt(fd, soLevel, soFlag, &tos, sizeof(int)) < 0) {
         int xerrno = errno;
         debugs(50, DBG_IMPORTANT, MYNAME << "setsockopt(TPROXY) on FD " << fd << ": " << xstrerr(xerrno));
     } else {
@@ -378,7 +380,7 @@ comm_openex(int sock_type,
 
     debugs(50, 3, "comm_openex: Attempt open socket for: " << addr );
 
-    new_socket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
+    new_socket = xsocket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
     const auto firstErrNo = errno;
 
     /* under IPv6 there is the possibility IPv6 is present but disabled. */
@@ -391,7 +393,7 @@ comm_openex(int sock_type,
         AI->ai_socktype = sock_type;
         AI->ai_protocol = proto;
         debugs(50, 3, "Attempt fallback open socket for: " << addr );
-        new_socket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
+        new_socket = xsocket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
         // TODO: Report failures of this second socket() call.
         // if both socket() calls fail, we use firstErrNo
         debugs(50, 2, "attempt open " << note << " socket on: " << addr);
@@ -506,7 +508,7 @@ comm_apply_flags(int new_socket,
 #if defined(SO_REUSEPORT)
         if (flags & COMM_REUSEPORT) {
             int on = 1;
-            if (setsockopt(new_socket, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<char*>(&on), sizeof(on)) < 0) {
+            if (xsetsockopt(new_socket, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on)) < 0) {
                 const auto savedErrno = errno;
                 const auto errorMessage = ToSBuf("cannot enable SO_REUSEPORT socket option when binding to ",
                                                  addr, ": ", xstrerr(savedErrno));
@@ -671,7 +673,8 @@ comm_connect_addr(int sock, const Ip::Address &address)
         ++ statCounter.syscalls.sock.connects;
 
         errno = 0;
-        if ((x = connect(sock, AI->ai_addr, AI->ai_addrlen)) < 0) {
+        x = xconnect(sock, AI->ai_addr, AI->ai_addrlen);
+        if (x < 0) {
             xerrno = errno;
             debugs(5,5, "sock=" << sock << ", addrinfo(" <<
                    " flags=" << AI->ai_flags <<
@@ -693,7 +696,7 @@ comm_connect_addr(int sock, const Ip::Address &address)
     } else {
         errno = 0;
         errlen = sizeof(err);
-        x = getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen);
+        x = xgetsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &errlen);
         if (x == 0)
             xerrno = err;
 
@@ -770,7 +773,7 @@ commConfigureLinger(const int fd, const OnOff enabled)
 
     fd_table[fd].flags.harshClosureRequested = (l.l_onoff && !l.l_linger); // close(2) sends TCP RST if true
 
-    if (setsockopt(fd, SOL_SOCKET, SO_LINGER, reinterpret_cast<char*>(&l), sizeof(l)) < 0) {
+    if (xsetsockopt(fd, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) < 0) {
         const auto xerrno = errno;
         debugs(50, DBG_CRITICAL, "ERROR: Failed to set closure behavior (SO_LINGER) for FD " << fd << ": " << xstrerr(xerrno));
     }
@@ -813,7 +816,7 @@ comm_close_complete(const int fd)
     F->ssl.reset();
     F->dynamicTlsContext.reset();
     fd_close(fd); /* update fdstat */
-    close(fd);
+    xclose(fd);
 
     ++ statCounter.syscalls.sock.closes;
 
@@ -917,7 +920,7 @@ comm_udp_sendto(int fd,
 
     struct addrinfo *AI = nullptr;
     to_addr.getAddrInfo(AI, fd_table[fd].sock_family);
-    int x = sendto(fd, buf, len, 0, AI->ai_addr, AI->ai_addrlen);
+    int x = xsendto(fd, buf, len, 0, AI->ai_addr, AI->ai_addrlen);
     int xerrno = errno;
     Ip::Address::FreeAddr(AI);
 
@@ -1012,7 +1015,7 @@ static void
 commSetReuseAddr(int fd)
 {
     int on = 1;
-    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)) < 0) {
+    if (xsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
         int xerrno = errno;
         debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
     }
@@ -1021,16 +1024,16 @@ commSetReuseAddr(int fd)
 static void
 commSetTcpRcvbuf(int fd, int size)
 {
-    if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &size, sizeof(size)) < 0) {
+    if (xsetsockopt(fd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)) < 0) {
         int xerrno = errno;
         debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno));
     }
-    if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *) &size, sizeof(size)) < 0) {
+    if (xsetsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) < 0) {
         int xerrno = errno;
         debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno));
     }
 #ifdef TCP_WINDOW_CLAMP
-    if (setsockopt(fd, SOL_TCP, TCP_WINDOW_CLAMP, (char *) &size, sizeof(size)) < 0) {
+    if (xsetsockopt(fd, SOL_TCP, TCP_WINDOW_CLAMP, &size, sizeof(size)) < 0) {
         int xerrno = errno;
         debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ", SIZE " << size << ": " << xstrerr(xerrno));
     }
@@ -1124,7 +1127,7 @@ commSetTcpNoDelay(int fd)
 {
     int on = 1;
 
-    if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &on, sizeof(on)) < 0) {
+    if (xsetsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) {
         int xerrno = errno;
         debugs(50, DBG_IMPORTANT, MYNAME << "FD " << fd << ": " << xstrerr(xerrno));
     }
@@ -1696,7 +1699,7 @@ comm_open_uds(int sock_type,
 
     debugs(50, 3, "Attempt open socket for: " << addr->sun_path);
 
-    if ((new_socket = socket(AI.ai_family, AI.ai_socktype, AI.ai_protocol)) < 0) {
+    if ((new_socket = xsocket(AI.ai_family, AI.ai_socktype, AI.ai_protocol)) < 0) {
         int xerrno = errno;
         /* Increase the number of reserved fd's if calls to socket()
          * are failing because the open file table is full.  This
index 25aad6bb4b71f9cd14b295e846cd7bbdeaa6a15a..44838865a13cba96eb8322d4f2957908da85209e 100644 (file)
@@ -14,6 +14,7 @@
 #include "comm/Connection.h"
 #include "comm/ConnOpener.h"
 #include "comm/Loops.h"
+#include "compat/socket.h"
 #include "fd.h"
 #include "fde.h"
 #include "globals.h"
@@ -431,7 +432,7 @@ Comm::ConnOpener::lookupLocalAddress()
 {
     struct sockaddr_storage addr = {};
     socklen_t len = sizeof(addr);
-    if (getsockname(conn_->fd, reinterpret_cast<struct sockaddr *>(&addr), &len) != 0) {
+    if (xgetsockname(conn_->fd, reinterpret_cast<struct sockaddr *>(&addr), &len) != 0) {
         int xerrno = errno;
         debugs(50, DBG_IMPORTANT, "ERROR: Failed to retrieve TCP/UDP details for socket: " << conn_ << ": " << xstrerr(xerrno));
         return;
index e049fb578e8580ad820e7dc5fa36bcbc4698a620..b2f08f17b60aa56fe4eac101d27f33b609941d53 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "base/IoManip.h"
 #include "comm/Loops.h"
+#include "compat/unistd.h"
 #include "fd.h"
 #include "fde.h"
 #include "mgr/Registration.h"
@@ -92,7 +93,6 @@ static void commDevPollRegisterWithCacheManager(void);
 static void
 comm_flush_updates(void)
 {
-    int i;
     if (devpoll_update.cur == -1)
         return; /* array of changes to make is empty */
 
@@ -102,11 +102,11 @@ comm_flush_updates(void)
         (devpoll_update.cur + 1) << " fds queued"
     );
 
-    i = write(
-            devpoll_fd, /* open handle to /dev/poll */
-            devpoll_update.pfds, /* pointer to array of struct pollfd */
-            (devpoll_update.cur + 1) * sizeof(struct pollfd) /* bytes to process */
-        );
+    const auto i = xwrite(
+                       devpoll_fd, /* open handle to /dev/poll */
+                       devpoll_update.pfds, /* pointer to array of struct pollfd */
+                       (devpoll_update.cur + 1) * sizeof(struct pollfd) /* bytes to process */
+                   );
     assert(i > 0);
     assert(static_cast<size_t>(i) == (sizeof(struct pollfd) * (devpoll_update.cur + 1)));
     devpoll_update.cur = -1; /* reset size of array, no elements remain */
@@ -191,7 +191,7 @@ Comm::SelectLoopInit(void)
                           );
 
     /* attempt to open /dev/poll device */
-    devpoll_fd = open("/dev/poll", O_RDWR);
+    devpoll_fd = xopen("/dev/poll", O_RDWR);
     if (devpoll_fd < 0) {
         int xerrno = errno;
         fatalf("comm_select_init: can't open /dev/poll: %s\n", xstrerr(xerrno));
index 6f3a58d3ef0ccc2036b2882b13069a30190e2c1e..79e91fc430b78232bae449b4d4ab7739c49a2a00 100644 (file)
@@ -15,6 +15,7 @@
 #include "anyp/PortCfg.h"
 #include "comm/Connection.h"
 #include "comm/Loops.h"
+#include "compat/select.h"
 #include "fde.h"
 #include "globals.h"
 #include "ICP.h"
@@ -156,7 +157,7 @@ comm_check_incoming_select_handlers(int nfds, int *fds)
 
     ++ statCounter.syscalls.selects;
 
-    if (select(maxfd, &read_mask, &write_mask, nullptr, &zero_tv) < 1)
+    if (xselect(maxfd, &read_mask, &write_mask, nullptr, &zero_tv) < 1)
         return incoming_sockets_accepted;
 
     for (i = 0; i < nfds; ++i) {
@@ -320,7 +321,7 @@ Comm::DoSelect(int msec)
             poll_time.tv_sec = msec / 1000;
             poll_time.tv_usec = (msec % 1000) * 1000;
             ++ statCounter.syscalls.selects;
-            num = select(maxfd, &readfds, &writefds, nullptr, &poll_time);
+            num = xselect(maxfd, &readfds, &writefds, nullptr, &poll_time);
             int xerrno = errno;
             ++ statCounter.select_loops;
 
index d42c29082134cb388bb4a5bd7bcc47171d878328..ee41d00a85d9768f193f1948a8ea75f306fa650e 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "squid.h"
 #include "comm/Tcp.h"
+#include "compat/socket.h"
 #include "debug/Stream.h"
 
 #if HAVE_NETINET_TCP_H
 #if HAVE_NETINET_IN_H
 #include <netinet/in.h>
 #endif
-#if HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
 #include <type_traits>
 
-/// setsockopt(2) wrapper
+/// xsetsockopt(2) wrapper
 template <typename Option>
 static bool
 SetSocketOption(const int fd, const int level, const int optName, const Option &optValue)
 {
     static_assert(std::is_trivially_copyable<Option>::value, "setsockopt() expects POD-like options");
     static_assert(!std::is_same<Option, bool>::value, "setsockopt() uses int to represent boolean options");
-    if (setsockopt(fd, level, optName, reinterpret_cast<const char *>(&optValue), sizeof(optValue)) < 0) {
+    if (xsetsockopt(fd, level, optName, &optValue, sizeof(optValue)) < 0) {
         const auto xerrno = errno;
         debugs(5, DBG_IMPORTANT, "ERROR: setsockopt(2) failure: " << xstrerr(xerrno));
         // TODO: Generalize to throw on errors when some callers need that.
index 094fa3a52de0ba7f6af5cd3da2098efca1e0ee7f..bdf3b04b3683c4f81565fefc751f7ac100ce42ea 100644 (file)
@@ -20,6 +20,7 @@
 #include "comm/Loops.h"
 #include "comm/TcpAcceptor.h"
 #include "CommCalls.h"
+#include "compat/socket.h"
 #include "eui/Config.h"
 #include "fd.h"
 #include "fde.h"
@@ -150,7 +151,7 @@ void
 Comm::TcpAcceptor::setListen()
 {
     errcode = errno = 0;
-    if (listen(conn->fd, Squid_MaxFD >> 2) < 0) {
+    if (xlisten(conn->fd, Squid_MaxFD >> 2) < 0) {
         errcode = errno;
         debugs(50, DBG_CRITICAL, "ERROR: listen(..., " << (Squid_MaxFD >> 2) << ") system call failed: " << xstrerr(errcode));
         return;
@@ -162,7 +163,7 @@ Comm::TcpAcceptor::setListen()
         bzero(&afa, sizeof(afa));
         debugs(5, DBG_IMPORTANT, "Installing accept filter '" << Config.accept_filter << "' on " << conn);
         xstrncpy(afa.af_name, Config.accept_filter, sizeof(afa.af_name));
-        if (setsockopt(conn->fd, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa)) < 0) {
+        if (xsetsockopt(conn->fd, SOL_SOCKET, SO_ACCEPTFILTER, &afa, sizeof(afa)) < 0) {
             int xerrno = errno;
             debugs(5, DBG_CRITICAL, "WARNING: SO_ACCEPTFILTER '" << Config.accept_filter << "': '" << xstrerr(xerrno));
         }
@@ -170,7 +171,7 @@ Comm::TcpAcceptor::setListen()
         int seconds = 30;
         if (strncmp(Config.accept_filter, "data=", 5) == 0)
             seconds = atoi(Config.accept_filter + 5);
-        if (setsockopt(conn->fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &seconds, sizeof(seconds)) < 0) {
+        if (xsetsockopt(conn->fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &seconds, sizeof(seconds)) < 0) {
             int xerrno = errno;
             debugs(5, DBG_CRITICAL, "WARNING: TCP_DEFER_ACCEPT '" << Config.accept_filter << "': '" << xstrerr(xerrno));
         }
@@ -348,7 +349,7 @@ Comm::TcpAcceptor::acceptInto(Comm::ConnectionPointer &details)
     errcode = 0; // reset local errno copy.
     struct sockaddr_storage remoteAddress = {};
     socklen_t remoteAddressSize = sizeof(remoteAddress);
-    const auto rawSock = accept(conn->fd, reinterpret_cast<struct sockaddr *>(&remoteAddress), &remoteAddressSize);
+    const auto rawSock = xaccept(conn->fd, reinterpret_cast<struct sockaddr *>(&remoteAddress), &remoteAddressSize);
     if (rawSock < 0) {
         errcode = errno; // store last accept errno locally.
         if (ignoreErrno(errcode) || errcode == ECONNABORTED) {
@@ -375,7 +376,7 @@ Comm::TcpAcceptor::acceptInto(Comm::ConnectionPointer &details)
     // lookup the local-end details of this new connection
     struct sockaddr_storage localAddress = {};
     socklen_t localAddressSize = sizeof(localAddress);
-    if (getsockname(sock, reinterpret_cast<struct sockaddr *>(&localAddress), &localAddressSize) != 0) {
+    if (xgetsockname(sock, reinterpret_cast<struct sockaddr *>(&localAddress), &localAddressSize) != 0) {
         int xerrno = errno;
         debugs(50, DBG_IMPORTANT, "ERROR: Closing accepted TCP connection after failing to obtain its local IP address" <<
                Debug::Extra << "accepted connection: " << details <<
index ea2068bfead01e197dbdc1e0d145d5ff35268a0d..392ff31501eeb6e0b9b639cc627762ad25a6aa63 100644 (file)
@@ -13,6 +13,8 @@
 #if USE_SQUID_EUI
 
 #include "base/IoManip.h"
+#include "compat/socket.h"
+#include "compat/unistd.h"
 #include "debug/Stream.h"
 #include "eui/Eui48.h"
 #include "globals.h"
@@ -171,7 +173,7 @@ Eui::Eui48::lookup(const Ip::Address &c)
     int offset;
 
     /* IPv6 builds do not provide the first http_port as an IPv4 socket for ARP */
-    int tmpSocket = socket(AF_INET,SOCK_STREAM,0);
+    auto tmpSocket = xsocket(AF_INET,SOCK_STREAM,0);
     if (tmpSocket < 0) {
         int xerrno = errno;
         debugs(28, DBG_IMPORTANT, "ERROR: Attempt to open socket for EUI retrieval failed: " << xstrerr(xerrno));
@@ -204,7 +206,7 @@ Eui::Eui48::lookup(const Ip::Address &c)
     debugs(28, 4, "id=" << (void*)this << " query ARP table");
     if (ioctl(tmpSocket, SIOCGARP, &arpReq) != -1) {
         /* Skip non-ethernet interfaces */
-        close(tmpSocket);
+        xclose(tmpSocket);
 
         if (arpReq.arp_ha.sa_family != ARPHRD_ETHER) {
             debugs(28, 4, "id=" << (void*)this << " ... not an Ethernet interface: " << arpReq.arp_ha.sa_data);
@@ -227,14 +229,14 @@ Eui::Eui48::lookup(const Ip::Address &c)
         int xerrno = errno;
         debugs(28, DBG_IMPORTANT, "ERROR: Attempt to retrieve interface list failed: " << xstrerr(xerrno));
         clear();
-        close(tmpSocket);
+        xclose(tmpSocket);
         return false;
     }
 
     if (ifc.ifc_len > (int)sizeof(ifbuffer)) {
         debugs(28, DBG_IMPORTANT, "Interface list too long - " << ifc.ifc_len);
         clear();
-        close(tmpSocket);
+        xclose(tmpSocket);
         return false;
     }
 
@@ -295,16 +297,16 @@ Eui::Eui48::lookup(const Ip::Address &c)
          */
 
         /* AYJ: 2009-10-06: for now we have to. We can only store one EUI at a time. */
-        close(tmpSocket);
+        xclose(tmpSocket);
         return true;
     }
 
-    close(tmpSocket);
+    xclose(tmpSocket);
 
 #elif _SQUID_SOLARIS_
 
     /* IPv6 builds do not provide the first http_port as an IPv4 socket for ARP */
-    int tmpSocket = socket(AF_INET,SOCK_STREAM,0);
+    auto tmpSocket = xsocket(AF_INET,SOCK_STREAM,0);
     if (tmpSocket < 0) {
         int xerrno = errno;
         debugs(28, DBG_IMPORTANT, "ERROR: Attempt to open socket for EUI retrieval failed: " << xstrerr(xerrno));
@@ -325,7 +327,7 @@ Eui::Eui48::lookup(const Ip::Address &c)
         *  Solaris (at least 2.6/x86) does not use arp_ha.sa_family -
         * it returns 00:00:00:00:00:00 for non-ethernet media
         */
-        close(tmpSocket);
+        xclose(tmpSocket);
 
         if (arpReq.arp_ha.sa_data[0] == 0 &&
                 arpReq.arp_ha.sa_data[1] == 0 &&
@@ -341,7 +343,7 @@ Eui::Eui48::lookup(const Ip::Address &c)
         set(arpReq.arp_ha.sa_data, 6);
         return true;
     } else {
-        close(tmpSocket);
+        xclose(tmpSocket);
     }
 
 #elif _SQUID_FREEBSD_ || _SQUID_NETBSD_ || _SQUID_OPENBSD_ || _SQUID_DRAGONFLY_ || _SQUID_KFREEBSD_
index 12142502d7bab953c64efbfafee5529c51fbd47b..111e00382f3781e0769f03b6e199217708c8e771 100644 (file)
--- a/src/fd.cc
+++ b/src/fd.cc
@@ -10,6 +10,8 @@
 
 #include "squid.h"
 #include "comm/Loops.h"
+#include "compat/socket.h"
+#include "compat/unistd.h"
 #include "debug/Messages.h"
 #include "debug/Stream.h"
 #include "fatal.h"
 #include "fde.h"
 #include "globals.h"
 
-// Solaris and possibly others lack MSG_NOSIGNAL optimization
-// TODO: move this into compat/? Use a dedicated compat file to avoid dragging
-// sys/socket.h into the rest of Squid??
-#ifndef MSG_NOSIGNAL
-#define MSG_NOSIGNAL 0
-#endif
-
 int default_read_method(int, char *, int);
 int default_write_method(int, const char *, int);
 #if _SQUID_WINDOWS_
@@ -103,7 +98,7 @@ fd_close(int fd)
 int
 socket_read_method(int fd, char *buf, int len)
 {
-    return recv(fd, (void *) buf, len, 0);
+    return xrecv(fd, (void *) buf, len, 0);
 }
 
 int
@@ -115,7 +110,7 @@ file_read_method(int fd, char *buf, int len)
 int
 socket_write_method(int fd, const char *buf, int len)
 {
-    return send(fd, (const void *) buf, len, 0);
+    return xsend(fd, buf, len, 0);
 }
 
 int
@@ -128,13 +123,13 @@ file_write_method(int fd, const char *buf, int len)
 int
 default_read_method(int fd, char *buf, int len)
 {
-    return read(fd, buf, len);
+    return xread(fd, buf, len);
 }
 
 int
 default_write_method(int fd, const char *buf, int len)
 {
-    return write(fd, buf, len);
+    return xwrite(fd, buf, len);
 }
 
 int
index 3eca180969adc87f4f5ea22b2378a98110a58666..9391d9bf1b4efc1ceaa3abb92dfaf01b899b36bb 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "squid.h"
 #include "base/AsyncJobCalls.h"
+#include "compat/unistd.h"
 #include "debug/Messages.h"
 #include "fs/rock/RockDbCell.h"
 #include "fs/rock/RockRebuild.h"
@@ -354,7 +355,7 @@ Rock::Rebuild::start()
         failure("cannot open db", errno);
 
     char hdrBuf[SwapDir::HeaderSize];
-    if (read(fd, hdrBuf, sizeof(hdrBuf)) != SwapDir::HeaderSize)
+    if (xread(fd, hdrBuf, sizeof(hdrBuf)) != SwapDir::HeaderSize)
         failure("cannot read db header", errno);
 
     // slot prefix of SM_PAGE_SIZE should fit both core entry header and ours
index 29c8dc97bb6a16eb17bbb1e9c4c4f7bd8689fd88..3e85598594ba58f09348255a8a217fae3bff6e4d 100644 (file)
@@ -12,6 +12,8 @@
 #include "base/IoManip.h"
 #include "cache_cf.h"
 #include "CollapsedForwarding.h"
+#include "compat/socket.h"
+#include "compat/unistd.h"
 #include "ConfigOption.h"
 #include "DiskIO/DiskIOModule.h"
 #include "DiskIO/DiskIOStrategy.h"
@@ -239,7 +241,7 @@ Rock::SwapDir::create()
     }
 
     debugs (47, DBG_IMPORTANT, "Creating Rock db: " << filePath);
-    const int swap = open(filePath, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600);
+    const auto swap = xopen(filePath, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0600);
     if (swap < 0)
         createError("create");
 
@@ -249,7 +251,7 @@ Rock::SwapDir::create()
     memset(block, '\0', sizeof(block));
 
     for (off_t offset = 0; offset < maxSize(); offset += sizeof(block)) {
-        if (write(swap, block, sizeof(block)) != sizeof(block))
+        if (xwrite(swap, block, sizeof(block)) != sizeof(block))
             createError("write");
     }
 #else
@@ -258,11 +260,11 @@ Rock::SwapDir::create()
 
     char header[HeaderSize];
     memset(header, '\0', sizeof(header));
-    if (write(swap, header, sizeof(header)) != sizeof(header))
+    if (xwrite(swap, header, sizeof(header)) != sizeof(header))
         createError("write");
 #endif
 
-    close(swap);
+    xclose(swap);
 }
 
 // report Rock DB creation error and exit
index 68b6f759d157897c24103577663878fac4ec1eab..d593cd3d7377d2db6c8d459826ab62b5a413f17e 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "squid.h"
 #include "comm/Loops.h"
+#include "compat/unistd.h"
 #include "fd.h"
 #include "fde.h"
 #include "fs_io.h"
@@ -64,14 +65,12 @@ dwrite_q::~dwrite_q()
 int
 file_open(const char *path, int mode)
 {
-    int fd;
-
     if (FILE_MODE(mode) == O_WRONLY)
         mode |= O_APPEND;
 
     errno = 0;
 
-    fd = open(path, mode, 0644);
+    auto fd = xopen(path, mode, 0644);
 
     ++ statCounter.syscalls.disk.opens;
 
@@ -124,7 +123,7 @@ file_close(int fd)
      */
     assert(F->write_handler == nullptr);
 
-    close(fd);
+    xclose(fd);
 
     debugs(6, F->flags.close_request ? 2 : 5, "file_close: FD " << fd << " really closing");
 
index 5bf7d48cb07575185c4185cbcfc8c94795017d2f..31e3883a652b45e5ccd0538006c7fc32fb1b3209 100644 (file)
@@ -9,6 +9,7 @@
 /* DEBUG: section 37    ICMP Routines */
 
 #include "squid.h"
+#include "compat/unistd.h"
 #include "debug/Stream.h"
 #include "Icmp.h"
 #include "time/gadgets.h"
@@ -26,7 +27,7 @@ Icmp::Close()
 {
 #if USE_ICMP
     if (icmp_sock > 0)
-        close(icmp_sock);
+        xclose(icmp_sock);
     icmp_sock = -1;
     icmp_ident = 0;
 #endif
index 889b3638b90167ee18c9717ee2f1da17dd916e4b..a753a17e99373217bf8adcaf4396f6bd0031a5a9 100644 (file)
@@ -14,6 +14,7 @@
 
 #if USE_ICMP
 
+#include "compat/socket.h"
 #include "debug/Stream.h"
 #include "Icmp4.h"
 #include "IcmpPinger.h"
@@ -65,7 +66,7 @@ Icmp4::~Icmp4()
 int
 Icmp4::Open(void)
 {
-    icmp_sock = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
+    icmp_sock = xsocket(PF_INET, SOCK_RAW, IPPROTO_ICMP);
 
     if (icmp_sock < 0) {
         int xerrno = errno;
@@ -136,12 +137,12 @@ Icmp4::SendEcho(Ip::Address &to, int opcode, const char *payload, int len)
 
     debugs(42, 5, "Send ICMP packet to " << to << ".");
 
-    x = sendto(icmp_sock,
-               (const void *) pkt,
-               icmp_pktsize,
-               0,
-               S->ai_addr,
-               S->ai_addrlen);
+    x = xsendto(icmp_sock,
+                pkt,
+                icmp_pktsize,
+                0,
+                S->ai_addr,
+                S->ai_addrlen);
 
     if (x < 0) {
         int xerrno = errno;
@@ -174,12 +175,12 @@ Icmp4::Recv(void)
         pkt = (char *)xmalloc(MAX_PKT4_SZ);
 
     Ip::Address::InitAddr(from);
-    n = recvfrom(icmp_sock,
-                 (void *)pkt,
-                 MAX_PKT4_SZ,
-                 0,
-                 from->ai_addr,
-                 &from->ai_addrlen);
+    n = xrecvfrom(icmp_sock,
+                  pkt,
+                  MAX_PKT4_SZ,
+                  0,
+                  from->ai_addr,
+                  &from->ai_addrlen);
 
     if (n <= 0) {
         debugs(42, DBG_CRITICAL, "ERROR: when calling recvfrom() on ICMP socket.");
index ca94b1c7ea094ccbca9e2f9d35e19528cc7dc1b1..93a64472d776087bc7f0914a48b329edfb127058 100644 (file)
@@ -14,6 +14,7 @@
 
 #if USE_ICMP
 
+#include "compat/socket.h"
 #include "debug/Stream.h"
 #include "Icmp6.h"
 #include "IcmpPinger.h"
@@ -97,7 +98,7 @@ Icmp6::~Icmp6()
 int
 Icmp6::Open(void)
 {
-    icmp_sock = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
+    icmp_sock = xsocket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
 
     if (icmp_sock < 0) {
         int xerrno = errno;
@@ -172,12 +173,12 @@ Icmp6::SendEcho(Ip::Address &to, int opcode, const char *payload, int len)
 
     debugs(42, 5, "Send Icmp6 packet to " << to << ".");
 
-    x = sendto(icmp_sock,
-               (const void *) pkt,
-               icmp6_pktsize,
-               0,
-               S->ai_addr,
-               S->ai_addrlen);
+    x = xsendto(icmp_sock,
+                pkt,
+                icmp6_pktsize,
+                0,
+                S->ai_addr,
+                S->ai_addrlen);
 
     if (x < 0) {
         int xerrno = errno;
@@ -215,12 +216,12 @@ Icmp6::Recv(void)
 
     Ip::Address::InitAddr(from);
 
-    n = recvfrom(icmp_sock,
-                 (void *)pkt,
-                 MAX_PKT6_SZ,
-                 0,
-                 from->ai_addr,
-                 &from->ai_addrlen);
+    n = xrecvfrom(icmp_sock,
+                  pkt,
+                  MAX_PKT6_SZ,
+                  0,
+                  from->ai_addr,
+                  &from->ai_addrlen);
 
     if (n <= 0) {
         debugs(42, DBG_CRITICAL, "ERROR: when calling recvfrom() on ICMPv6 socket.");
index d74ab735ec1d617c2893500a26e992efeb82fdd6..de5b05072d2f77974b30fe9c0c506eff15ac57a0 100644 (file)
@@ -14,6 +14,8 @@
 
 #if USE_ICMP
 
+#include "compat/socket.h"
+#include "compat/unistd.h"
 #include "debug/Stream.h"
 #include "Icmp4.h"
 #include "Icmp6.h"
@@ -65,26 +67,26 @@ IcmpPinger::Open(void)
 
     setmode(0, O_BINARY);
     setmode(1, O_BINARY);
-    x = read(0, buf, sizeof(wpi));
+    x = xread(0, buf, sizeof(wpi));
 
     if (x < (int)sizeof(wpi)) {
         xerrno = errno;
         getCurrentTime();
         debugs(42, DBG_CRITICAL, MYNAME << " read: FD 0: " << xstrerr(xerrno));
-        write(1, "ERR\n", 4);
+        xwrite(1, "ERR\n", 4);
         return -1;
     }
 
     memcpy(&wpi, buf, sizeof(wpi));
 
-    write(1, "OK\n", 3);
-    x = read(0, buf, sizeof(PS));
+    xwrite(1, "OK\n", 3);
+    x = xread(0, buf, sizeof(PS));
 
     if (x < (int)sizeof(PS)) {
         xerrno = errno;
         getCurrentTime();
         debugs(42, DBG_CRITICAL, MYNAME << " read: FD 0: " << xstrerr(xerrno));
-        write(1, "ERR\n", 4);
+        xwrite(1, "ERR\n", 4);
         return -1;
     }
 
@@ -96,23 +98,23 @@ IcmpPinger::Open(void)
         xerrno = errno;
         getCurrentTime();
         debugs(42, DBG_CRITICAL, MYNAME << "WSASocket: " << xstrerr(xerrno));
-        write(1, "ERR\n", 4);
+        xwrite(1, "ERR\n", 4);
         return -1;
     }
 
-    x = connect(icmp_sock, (struct sockaddr *) &PS, sizeof(PS));
+    x = xconnect(icmp_sock, (struct sockaddr *) &PS, sizeof(PS));
 
-    if (SOCKET_ERROR == x) {
+    if (x != 0) {
         xerrno = errno;
         getCurrentTime();
         debugs(42, DBG_CRITICAL, MYNAME << "connect: " << xstrerr(xerrno));
-        write(1, "ERR\n", 4);
+        xwrite(1, "ERR\n", 4);
         return -1;
     }
 
-    write(1, "OK\n", 3);
+    xwrite(1, "OK\n", 3);
     memset(buf, 0, sizeof(buf));
-    x = recv(icmp_sock, (void *) buf, sizeof(buf), 0);
+    x = xrecv(icmp_sock, buf, sizeof(buf), 0);
 
     if (x < 3) {
         xerrno = errno;
@@ -120,7 +122,7 @@ IcmpPinger::Open(void)
         return -1;
     }
 
-    x = send(icmp_sock, (const void *) buf, strlen(buf), 0);
+    x = xsend(icmp_sock, buf, strlen(buf), 0);
     xerrno = errno;
 
     if (x < 3 || strncmp("OK\n", buf, 3)) {
@@ -152,7 +154,7 @@ IcmpPinger::Close(void)
 #if _SQUID_WINDOWS_
 
     shutdown(icmp_sock, SD_BOTH);
-    close(icmp_sock);
+    xclose(icmp_sock);
     icmp_sock = -1;
 #endif
 
@@ -169,7 +171,7 @@ IcmpPinger::Recv(void)
     int guess_size;
 
     pecho = pingerEchoData();
-    n = recv(socket_from_squid, &pecho, sizeof(pecho), 0);
+    n = xrecv(socket_from_squid, &pecho, sizeof(pecho), 0);
 
     if (n < 0) {
         debugs(42, DBG_IMPORTANT, "Pinger exiting.");
@@ -219,7 +221,7 @@ IcmpPinger::SendResult(pingerReplyData &preply, int len)
 {
     debugs(42, 2, "return result to squid. len=" << len);
 
-    if (send(socket_to_squid, &preply, len, 0) < 0) {
+    if (xsend(socket_to_squid, &preply, len, 0) < 0) {
         int xerrno = errno;
         debugs(42, DBG_CRITICAL, "FATAL: send failure: " << xstrerr(xerrno));
         Close();
index 8505b2611191a48f99f8f179d1241d0724aa827f..8d1c94f42ada989dcd9bce61c8a511d87e94b444 100644 (file)
@@ -11,6 +11,7 @@
 #include "squid.h"
 #include "comm.h"
 #include "comm/Loops.h"
+#include "compat/socket.h"
 #include "defines.h"
 #include "fd.h"
 #include "icmp/IcmpConfig.h"
@@ -262,7 +263,7 @@ IcmpSquid::Close(void)
 
 #if _SQUID_WINDOWS_
 
-    send(icmp_sock, (const void *) "$shutdown\n", 10, 0);
+    xsend(icmp_sock, (const void *) "$shutdown\n", 10, 0);
 
 #endif
 
index 126d84e827b92843a92fbcb00e4708ba2ad237bd..1a3f1c0df6831c8184be9a62dfffdc6ce3432e58 100644 (file)
@@ -45,6 +45,8 @@
 #if USE_ICMP
 
 #include "base/Stopwatch.h"
+#include "compat/select.h"
+#include "compat/socket.h"
 #include "Icmp4.h"
 #include "Icmp6.h"
 #include "IcmpPinger.h"
@@ -101,7 +103,6 @@ int
 main(int, char **)
 {
     fd_set R;
-    int x;
     int max_fd = 0;
 
     /*
@@ -205,7 +206,7 @@ main(int, char **)
         FD_SET(squid_link, &R);
         Stopwatch timer;
         timer.resume();
-        x = select(max_fd+1, &R, nullptr, nullptr, &tv);
+        const auto x = xselect(max_fd+1, &R, nullptr, nullptr, &tv);
         getCurrentTime();
 
         if (x < 0) {
@@ -228,7 +229,7 @@ main(int, char **)
 
         const auto delay = std::chrono::duration_cast<std::chrono::seconds>(timer.total());
         if (delay >= PingerTimeout) {
-            if (send(LINK_TO_SQUID, &tv, 0, 0) < 0) {
+            if (xsend(LINK_TO_SQUID, &tv, 0, 0) < 0) {
                 debugs(42, DBG_CRITICAL, "Closing. No requests in last " << delay.count() << " seconds.");
                 control.Close();
                 exit(EXIT_FAILURE);
@@ -244,7 +245,7 @@ main(int, char **)
 
 #include <ostream>
 int
-main(int argc, char *argv[])
+main(int, char *argv[])
 {
     std::cerr << argv[0] << ": ICMP support not compiled in." << std::endl;
     return EXIT_FAILURE;
index ded4649fc63bb5c4bac9e1a859b4ec801d884c2f..6500d55aab4975e3aa352b0ef138bd4656609e84 100644 (file)
@@ -13,6 +13,8 @@
 
 #include "squid.h"
 #include "comm/Connection.h"
+#include "compat/socket.h"
+#include "compat/unistd.h"
 #include "fde.h"
 #include "ip/Intercept.h"
 #include "ip/tools.h"
@@ -75,7 +77,6 @@
 #endif /* IPF_TRANSPARENT required headers */
 
 #if PF_TRANSPARENT
-#include <sys/socket.h>
 #include <sys/ioctl.h>
 #include <sys/fcntl.h>
 #include <net/if.h>
@@ -130,11 +131,11 @@ Ip::Intercept::NetfilterInterception(const Comm::ConnectionPointer &newConn)
 
     /** \par
      * Try NAT lookup for REDIRECT or DNAT targets. */
-    if ( getsockopt(newConn->fd,
-                    newConn->local.isIPv6() ? IPPROTO_IPV6 : IPPROTO_IP,
-                    newConn->local.isIPv6() ? IP6T_SO_ORIGINAL_DST : SO_ORIGINAL_DST,
-                    &lookup,
-                    &len) != 0) {
+    if ( xgetsockopt(newConn->fd,
+                     newConn->local.isIPv6() ? IPPROTO_IPV6 : IPPROTO_IP,
+                     newConn->local.isIPv6() ? IP6T_SO_ORIGINAL_DST : SO_ORIGINAL_DST,
+                     &lookup,
+                     &len) != 0) {
         const auto xerrno = errno;
         debugs(89, DBG_IMPORTANT, "ERROR: NF getsockopt(ORIGINAL_DST) failed on " << newConn << ": " << xstrerr(xerrno));
         return false;
@@ -250,9 +251,9 @@ Ip::Intercept::IpfInterception(const Comm::ConnectionPointer &newConn)
         int save_errno;
         enter_suid();
 #ifdef IPNAT_NAME
-        natfd = open(IPNAT_NAME, O_RDONLY, 0);
+        natfd = xopen(IPNAT_NAME, O_RDONLY, 0);
 #else
-        natfd = open(IPL_NAT, O_RDONLY, 0);
+        natfd = xopen(IPL_NAT, O_RDONLY, 0);
 #endif
         save_errno = errno;
         leave_suid();
@@ -295,7 +296,7 @@ Ip::Intercept::IpfInterception(const Comm::ConnectionPointer &newConn)
         const auto xerrno = errno;
         if (xerrno != ESRCH) {
             debugs(89, DBG_IMPORTANT, "ERROR: IPF (IPFilter) NAT lookup failed: ioctl(SIOCGNATL) (v=" << IPFILTER_VERSION << "): " << xstrerr(xerrno));
-            close(natfd);
+            xclose(natfd);
             natfd = -1;
         }
 
@@ -335,7 +336,7 @@ Ip::Intercept::PfInterception(const Comm::ConnectionPointer &newConn)
     static int pffd = -1;
 
     if (pffd < 0)
-        pffd = open("/dev/pf", O_RDONLY);
+        pffd = xopen("/dev/pf", O_RDONLY);
 
     if (pffd < 0) {
         const auto xerrno = errno;
@@ -365,7 +366,7 @@ Ip::Intercept::PfInterception(const Comm::ConnectionPointer &newConn)
         const auto xerrno = errno;
         if (xerrno != ENOENT) {
             debugs(89, DBG_IMPORTANT, "ERROR: PF lookup failed: ioctl(DIOCNATLOOK): " << xstrerr(xerrno));
-            close(pffd);
+            xclose(pffd);
             pffd = -1;
         }
         debugs(89, 9, "address: " << newConn);
@@ -443,18 +444,18 @@ Ip::Intercept::ProbeForTproxy(Ip::Address &test)
         tmp.port(0);
         tmp.getSockAddr(tmp_ip6);
 
-        if ( (tmp_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP)) >= 0 &&
-                setsockopt(tmp_sock, soLevel, soFlag, (char *)&tos, sizeof(int)) == 0 &&
-                bind(tmp_sock, (struct sockaddr*)&tmp_ip6, sizeof(struct sockaddr_in6)) == 0 ) {
+        if ( (tmp_sock = xsocket(PF_INET6, SOCK_STREAM, IPPROTO_TCP)) >= 0 &&
+                xsetsockopt(tmp_sock, soLevel, soFlag, &tos, sizeof(int)) == 0 &&
+                xbind(tmp_sock, (struct sockaddr*)&tmp_ip6, sizeof(struct sockaddr_in6)) == 0 ) {
 
             debugs(3, 3, "IPv6 TPROXY support detected. Using.");
-            close(tmp_sock);
+            xclose(tmp_sock);
             if (doneSuid)
                 leave_suid();
             return true;
         }
         if (tmp_sock >= 0) {
-            close(tmp_sock);
+            xclose(tmp_sock);
             tmp_sock = -1;
         }
     }
@@ -475,18 +476,18 @@ Ip::Intercept::ProbeForTproxy(Ip::Address &test)
         tmp.port(0);
         tmp.getSockAddr(tmp_ip4);
 
-        if ( (tmp_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) >= 0 &&
-                setsockopt(tmp_sock, soLevel, soFlag, (char *)&tos, sizeof(int)) == 0 &&
-                bind(tmp_sock, (struct sockaddr*)&tmp_ip4, sizeof(struct sockaddr_in)) == 0 ) {
+        if ( (tmp_sock = xsocket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) >= 0 &&
+                xsetsockopt(tmp_sock, soLevel, soFlag, &tos, sizeof(int)) == 0 &&
+                xbind(tmp_sock, (struct sockaddr*)&tmp_ip4, sizeof(struct sockaddr_in)) == 0 ) {
 
             debugs(3, 3, "IPv4 TPROXY support detected. Using.");
-            close(tmp_sock);
+            xclose(tmp_sock);
             if (doneSuid)
                 leave_suid();
             return true;
         }
         if (tmp_sock >= 0) {
-            close(tmp_sock);
+            xclose(tmp_sock);
         }
     }
 
index d4416c6776fdc50568082acd0e49827efe680135..9b77b756f3d298dc53882377f5420b7fe71121d4 100644 (file)
@@ -14,6 +14,7 @@
 #include "cache_cf.h"
 #include "comm/Connection.h"
 #include "compat/cmsg.h"
+#include "compat/socket.h"
 #include "ConfigParser.h"
 #include "fde.h"
 #include "globals.h"
@@ -51,10 +52,10 @@ Ip::Qos::getTosFromServer(const Comm::ConnectionPointer &server, fde *clientFde)
     tos_t tos = 1;
     int tos_len = sizeof(tos);
     clientFde->tosFromServer = 0;
-    if (setsockopt(server->fd,SOL_IP,IP_RECVTOS,&tos,tos_len)==0) {
+    if (xsetsockopt(server->fd,SOL_IP,IP_RECVTOS,&tos,tos_len)==0) {
         unsigned char buf[512];
         int len = 512;
-        if (getsockopt(server->fd,SOL_IP,IP_PKTOPTIONS,buf,(socklen_t*)&len) == 0) {
+        if (xgetsockopt(server->fd,SOL_IP,IP_PKTOPTIONS,buf,(socklen_t*)&len) == 0) {
             /* Parse the PKTOPTIONS structure to locate the TOS data message
              * prepared in the kernel by the ZPH incoming TCP TOS preserving
              * patch.
@@ -526,7 +527,7 @@ Ip::Qos::setSockTos(const int fd, tos_t tos, int type)
 
     if (type == AF_INET) {
 #if defined(IP_TOS)
-        const int x = setsockopt(fd, IPPROTO_IP, IP_TOS, &bTos, sizeof(bTos));
+        const int x = xsetsockopt(fd, IPPROTO_IP, IP_TOS, &bTos, sizeof(bTos));
         if (x < 0) {
             int xerrno = errno;
             debugs(50, 2, "setsockopt(IP_TOS) on " << fd << ": " << xstrerr(xerrno));
@@ -538,7 +539,7 @@ Ip::Qos::setSockTos(const int fd, tos_t tos, int type)
 #endif
     } else { // type == AF_INET6
 #if defined(IPV6_TCLASS)
-        const int x = setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &bTos, sizeof(bTos));
+        const int x = xsetsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &bTos, sizeof(bTos));
         if (x < 0) {
             int xerrno = errno;
             debugs(50, 2, "setsockopt(IPV6_TCLASS) on " << fd << ": " << xstrerr(xerrno));
@@ -566,7 +567,7 @@ Ip::Qos::setSockNfmark(const int fd, nfmark_t mark)
 {
 #if HAVE_LIBCAP && SO_MARK
     debugs(50, 3, "for FD " << fd << " to " << mark);
-    const int x = setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(nfmark_t));
+    const int x = xsetsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(nfmark_t));
     if (x < 0) {
         int xerrno = errno;
         debugs(50, 2, "setsockopt(SO_MARK) on " << fd << ": " << xstrerr(xerrno));
index 1f17d0fec74a37dc4d7eaa5b2c2f956e576ad597..3e2f0bf58a30c37107f2fdc40ac161bd30f6fda4 100644 (file)
@@ -9,16 +9,12 @@
 /* DEBUG: section 21    Misc Functions */
 
 #include "squid.h"
+#include "compat/socket.h"
+#include "compat/unistd.h"
 #include "debug/Messages.h"
 #include "ip/Address.h"
 #include "ip/tools.h"
 
-#if HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-#if HAVE_SYS_SOCKET_H
-#include <sys/socket.h>
-#endif
 #if HAVE_NETINET_IN_H
 #include <netinet/in.h>
 #endif
@@ -32,7 +28,7 @@ void
 Ip::ProbeTransport()
 {
     // check for usable IPv6 sockets
-    int s = socket(PF_INET6, SOCK_STREAM, 0);
+    auto s = xsocket(PF_INET6, SOCK_STREAM, 0);
     if (s < 0) {
         debugs(3, 2, "IPv6 not supported on this machine. Auto-Disabled.");
         EnableIpv6 = IPV6_OFF;
@@ -43,7 +39,7 @@ Ip::ProbeTransport()
     // (AKA. the operating system supports RFC 3493 section 5.3)
 #if defined(IPV6_V6ONLY)
     int tos = 0;
-    if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &tos, sizeof(int)) == 0) {
+    if (xsetsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &tos, sizeof(int)) == 0) {
         debugs(3, 2, "Detected IPv6 hybrid or v4-mapping stack...");
         EnableIpv6 |= IPV6_SPECIAL_V4MAPPING;
     } else {
@@ -63,7 +59,7 @@ Ip::ProbeTransport()
     if (ip.isIPv6()) { // paranoid; always succeeds if we got this far
         struct sockaddr_in6 sin;
         ip.getSockAddr(sin);
-        if (bind(s, reinterpret_cast<struct sockaddr *>(&sin), sizeof(sin)) != 0) {
+        if (xbind(s, reinterpret_cast<struct sockaddr *>(&sin), sizeof(sin)) != 0) {
             debugs(3, Critical(66), "WARNING: BCP 177 violation. Detected non-functional IPv6 loopback.");
             EnableIpv6 = IPV6_OFF;
         } else {
@@ -71,7 +67,7 @@ Ip::ProbeTransport()
         }
     }
 
-    close(s);
+    xclose(s);
 
 #if USE_IPV6
     debugs(3, 2, "IPv6 transport " << (EnableIpv6?"Enabled":"Disabled"));
index 9e6571c519f7631bd3fcf64371d22623ce4a59d2..ec462cae3b59e5eacb96f04e9121b39978a677e5 100644 (file)
@@ -11,6 +11,8 @@
 #include "squid.h"
 #include "comm/Connection.h"
 #include "compat/pipe.h"
+#include "compat/socket.h"
+#include "compat/unistd.h"
 #include "fd.h"
 #include "fde.h"
 #include "globals.h"
 #include <thread>
 #include <cstdlib>
 
-#if HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
 static const char *hello_string = "hi there\n";
 #ifndef HELLO_BUF_SZ
 #define HELLO_BUF_SZ 32
@@ -165,22 +163,22 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
         }
 
         errno = 0;
-        if (setsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, (void *) &buflen, sizeof(buflen)) == -1)  {
+        if (xsetsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, &buflen, sizeof(buflen)) == -1)  {
             xerrno = errno;
             debugs(54, DBG_IMPORTANT, "ERROR: setsockopt failed: " << xstrerr(xerrno));
             errno = 0;
         }
-        if (setsockopt(fds[0], SOL_SOCKET, SO_RCVBUF, (void *) &buflen, sizeof(buflen)) == -1) {
+        if (xsetsockopt(fds[0], SOL_SOCKET, SO_RCVBUF, &buflen, sizeof(buflen)) == -1) {
             xerrno = errno;
             debugs(54, DBG_IMPORTANT, "ERROR: setsockopt failed: " << xstrerr(xerrno));
             errno = 0;
         }
-        if (setsockopt(fds[1], SOL_SOCKET, SO_SNDBUF, (void *) &buflen, sizeof(buflen)) == -1) {
+        if (xsetsockopt(fds[1], SOL_SOCKET, SO_SNDBUF, &buflen, sizeof(buflen)) == -1) {
             xerrno = errno;
             debugs(54, DBG_IMPORTANT, "ERROR: setsockopt failed: " << xstrerr(xerrno));
             errno = 0;
         }
-        if (setsockopt(fds[1], SOL_SOCKET, SO_RCVBUF, (void *) &buflen, sizeof(buflen)) == -1) {
+        if (xsetsockopt(fds[1], SOL_SOCKET, SO_RCVBUF, &buflen, sizeof(buflen)) == -1) {
             xerrno = errno;
             debugs(54, DBG_IMPORTANT, "ERROR: setsockopt failed: " << xstrerr(xerrno));
             errno = 0;
@@ -218,7 +216,7 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
     if (type == IPC_TCP_SOCKET || type == IPC_UDP_SOCKET) {
         Ip::Address::InitAddr(AI);
 
-        if (getsockname(pwfd, AI->ai_addr, &AI->ai_addrlen) < 0) {
+        if (xgetsockname(pwfd, AI->ai_addr, &AI->ai_addrlen) < 0) {
             xerrno = errno;
             Ip::Address::FreeAddr(AI);
             debugs(54, DBG_CRITICAL, "ipcCreate: getsockname: " << xstrerr(xerrno));
@@ -233,7 +231,7 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
 
         Ip::Address::InitAddr(AI);
 
-        if (getsockname(crfd, AI->ai_addr, &AI->ai_addrlen) < 0) {
+        if (xgetsockname(crfd, AI->ai_addr, &AI->ai_addrlen) < 0) {
             xerrno = errno;
             Ip::Address::FreeAddr(AI);
             debugs(54, DBG_CRITICAL, "ipcCreate: getsockname: " << xstrerr(xerrno));
@@ -249,7 +247,7 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
     }
 
     if (type == IPC_TCP_SOCKET) {
-        if (listen(crfd, 1) < 0) {
+        if (xlisten(crfd, 1) < 0) {
             xerrno = errno;
             debugs(54, DBG_IMPORTANT, "ipcCreate: listen FD " << crfd << ": " << xstrerr(xerrno));
             return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
@@ -284,7 +282,7 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
         if (type == IPC_UDP_SOCKET)
             x = comm_udp_recv(prfd, hello_buf, sizeof(hello_buf)-1, 0);
         else
-            x = read(prfd, hello_buf, sizeof(hello_buf)-1);
+            x = xread(prfd, hello_buf, sizeof(hello_buf)-1);
         xerrno = errno;
         if (x >= 0)
             hello_buf[x] = '\0';
@@ -325,24 +323,24 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
     no_suid();          /* give up extra privileges */
 
     /* close shared socket with parent */
-    close(prfd);
+    xclose(prfd);
 
     if (pwfd != prfd)
-        close(pwfd);
+        xclose(pwfd);
 
     pwfd = prfd = -1;
 
     if (type == IPC_TCP_SOCKET) {
         debugs(54, 3, "ipcCreate: calling accept on FD " << crfd);
 
-        if ((fd = accept(crfd, nullptr, nullptr)) < 0) {
+        if ((fd = xaccept(crfd, nullptr, nullptr)) < 0) {
             xerrno = errno;
             debugs(54, DBG_CRITICAL, "ipcCreate: FD " << crfd << " accept: " << xstrerr(xerrno));
             _exit(1);
         }
 
         debugs(54, 3, "ipcCreate: CHILD accepted new FD " << fd);
-        close(crfd);
+        xclose(crfd);
         cwfd = crfd = fd;
     } else if (type == IPC_UDP_SOCKET) {
         if (comm_connect_addr(crfd, PaS) == Comm::COMM_ERROR)
@@ -359,7 +357,7 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
             _exit(1);
         }
     } else {
-        if (write(cwfd, hello_string, strlen(hello_string) + 1) < 0) {
+        if (xwrite(cwfd, hello_string, strlen(hello_string) + 1) < 0) {
             xerrno = errno;
             debugs(54, DBG_CRITICAL, "write FD " << cwfd << ": " << xstrerr(xerrno));
             debugs(54, DBG_CRITICAL, "ERROR: ipcCreate: CHILD: hello write test failed");
@@ -394,7 +392,7 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
         x = dupOrExit(crfd);
     } while (x < 3);
 
-    close(x);
+    xclose(x);
 
     t1 = dupOrExit(crfd);
 
@@ -404,11 +402,11 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
 
     assert(t1 > 2 && t2 > 2 && t3 > 2);
 
-    close(crfd);
+    xclose(crfd);
 
-    close(cwfd);
+    xclose(cwfd);
 
-    close(fileno(DebugStream()));
+    xclose(fileno(DebugStream()));
 
     dup2(t1, 0);
 
@@ -416,15 +414,15 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
 
     dup2(t3, 2);
 
-    close(t1);
+    xclose(t1);
 
-    close(t2);
+    xclose(t2);
 
-    close(t3);
+    xclose(t3);
 
     /* Make sure all other filedescriptors are closed */
     for (x = 3; x < SQUID_MAXFD; ++x)
-        close(x);
+        xclose(x);
 
 #if HAVE_SETSID
     if (opt_no_daemon)
index e9cbf502293a5c6107a563f7672053c83b84ee90..7f5aba55ae923a2650b5ae064030ff3460f08445 100644 (file)
@@ -14,6 +14,7 @@
 #include "CacheManager.h"
 #include "comm.h"
 #include "comm/Connection.h"
+#include "compat/unistd.h"
 #include "ipc/Coordinator.h"
 #include "ipc/SharedListen.h"
 #include "mgr/Inquirer.h"
 
 #include <cerrno>
 
+#if HAVE_SYS_UNISTD_H
+#include <sys/unistd.h>
+#endif
+
 CBDATA_NAMESPACED_CLASS_INIT(Ipc, Coordinator);
 Ipc::Coordinator* Ipc::Coordinator::TheInstance = nullptr;
 
@@ -176,7 +181,7 @@ Ipc::Coordinator::handleCacheMgrRequest(const Mgr::Request& request)
         debugs(54, DBG_IMPORTANT, "ERROR: Squid BUG: cannot aggregate mgr:" <<
                request.params.actionName << ": " << ex.what());
         // TODO: Avoid half-baked Connections or teach them how to close.
-        ::close(request.conn->fd);
+        xclose(request.conn->fd);
         request.conn->fd = -1;
         return; // the worker will timeout and close
     }
index ef16dd6e3094a9ca3de1220e4f6e153e3677cb27..41ca5a266c29ddbf734c1c7cf1a0fe0413b27eaf 100644 (file)
@@ -14,6 +14,7 @@
 #include "comm/Connection.h"
 #include "comm/Write.h"
 #include "CommCalls.h"
+#include "compat/socket.h"
 #include "ipc/UdsOp.h"
 
 Ipc::UdsOp::UdsOp(const String& pathAddr):
@@ -195,7 +196,7 @@ Ipc::ImportFdIntoComm(const Comm::ConnectionPointer &conn, int socktype, int pro
 {
     struct sockaddr_storage addr;
     socklen_t len = sizeof(addr);
-    if (getsockname(conn->fd, reinterpret_cast<sockaddr*>(&addr), &len) == 0) {
+    if (xgetsockname(conn->fd, reinterpret_cast<sockaddr*>(&addr), &len) == 0) {
         conn->remote = addr;
         struct addrinfo* addr_info = nullptr;
         conn->remote.getAddrInfo(addr_info);
index 075572483937de998f20425f65147ade558ee3b5..9eed23d928229d02076147df92f4bd540798724b 100644 (file)
@@ -11,6 +11,7 @@
 #include "squid.h"
 #include "base/TextException.h"
 #include "compat/shm.h"
+#include "compat/unistd.h"
 #include "debug/Stream.h"
 #include "fatal.h"
 #include "Instance.h"
@@ -28,9 +29,6 @@
 #if HAVE_SYS_STAT_H
 #include <sys/stat.h>
 #endif
-#if HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 
 // test cases change this
 const char *Ipc::Mem::Segment::BasePath = DEFAULT_STATEDIR;
@@ -70,7 +68,7 @@ Ipc::Mem::Segment::~Segment()
 {
     if (theFD >= 0) {
         detach();
-        if (close(theFD) != 0) {
+        if (xclose(theFD) != 0) {
             int xerrno = errno;
             debugs(54, 5, "close " << theName << ": " << xstrerr(xerrno));
         }
index 31c382e42c01626082ac6ffc8a8448549084c4a6..4047e4ca504c7eee2326989ee95424e778af6b65 100644 (file)
@@ -12,6 +12,8 @@
 #include "cache_cf.h"
 #include "comm.h"
 #include "comm/Connection.h"
+#include "compat/socket.h"
+#include "compat/unistd.h"
 #include "fd.h"
 #include "fde.h"
 #include "globals.h"
@@ -184,7 +186,7 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
 
         Ip::Address::InitAddr(aiPS);
 
-        if (getsockname(pwfd, aiPS->ai_addr, &(aiPS->ai_addrlen) ) < 0) {
+        if (xgetsockname(pwfd, aiPS->ai_addr, &(aiPS->ai_addrlen) ) < 0) {
             int xerrno = errno;
             debugs(54, DBG_CRITICAL, "ipcCreate: getsockname: " << xstrerr(xerrno));
             Ip::Address::FreeAddr(aiPS);
@@ -198,7 +200,7 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
 
         Ip::Address::InitAddr(aiCS);
 
-        if (getsockname(crfd, aiCS->ai_addr, &(aiCS->ai_addrlen) ) < 0) {
+        if (xgetsockname(crfd, aiCS->ai_addr, &(aiCS->ai_addrlen) ) < 0) {
             int xerrno = errno;
             debugs(54, DBG_CRITICAL, "ipcCreate: getsockname: " << xstrerr(xerrno));
             Ip::Address::FreeAddr(aiCS);
@@ -213,7 +215,7 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
     }
 
     if (type == IPC_TCP_SOCKET) {
-        if (listen(crfd, 1) < 0) {
+        if (xlisten(crfd, 1) < 0) {
             int xerrno = errno;
             debugs(54, DBG_IMPORTANT, "ipcCreate: listen FD " << crfd << ": " << xstrerr(xerrno));
             return ipcCloseAllFD(prfd, pwfd, crfd, cwfd);
@@ -254,7 +256,7 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
     }
 
     memset(hello_buf, '\0', HELLO_BUF_SZ);
-    x = recv(prfd, (void *)hello_buf, HELLO_BUF_SZ - 1, 0);
+    x = xrecv(prfd, (void *)hello_buf, HELLO_BUF_SZ - 1, 0);
 
     if (x < 0) {
         int xerrno = errno;
@@ -270,7 +272,7 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
         return ipcCloseAllFD(prfd, pwfd, -1, -1);
     }
 
-    x = send(pwfd, (const void *)ok_string, strlen(ok_string), 0);
+    x = xsend(pwfd, ok_string, strlen(ok_string), 0);
 
     if (x < 0) {
         int xerrno = errno;
@@ -281,7 +283,7 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
     }
 
     memset(hello_buf, '\0', HELLO_BUF_SZ);
-    x = recv(prfd, (void *)hello_buf, HELLO_BUF_SZ - 1, 0);
+    x = xrecv(prfd, (void *)hello_buf, HELLO_BUF_SZ - 1, 0);
 
     if (x < 0) {
         int xerrno = errno;
@@ -333,7 +335,7 @@ ipcCreate(int type, const char *prog, const char *const args[], const char *name
 static int
 ipcSend(int cwfd, const char *buf, int len)
 {
-    int x = send(cwfd, (const void *)buf, len, 0);
+    const auto x = xsend(cwfd, buf, len, 0);
 
     if (x < 0) {
         int xerrno = errno;
@@ -393,7 +395,7 @@ ipc_thread_1(void *in_params)
     if (type == IPC_TCP_SOCKET) {
         debugs(54, 3, "ipcCreate: calling accept on FD " << crfd);
 
-        if ((fd = accept(crfd, nullptr, nullptr)) < 0) {
+        if ((fd = xaccept(crfd, nullptr, nullptr)) < 0) {
             int xerrno = errno;
             debugs(54, DBG_CRITICAL, "ipcCreate: FD " << crfd << " accept: " << xstrerr(xerrno));
             goto cleanup;
@@ -410,7 +412,7 @@ ipc_thread_1(void *in_params)
             goto cleanup;
     }
 
-    x = send(cwfd, (const void *)hello_string, strlen(hello_string) + 1, 0);
+    x = xsend(cwfd, hello_string, strlen(hello_string) + 1, 0);
 
     if (x < 0) {
         int xerrno = errno;
@@ -421,7 +423,7 @@ ipc_thread_1(void *in_params)
 
     PutEnvironment();
     memset(buf1, '\0', bufSz);
-    x = recv(crfd, (void *)buf1, bufSz-1, 0);
+    x = xrecv(crfd, (void *)buf1, bufSz-1, 0);
 
     if (x < 0) {
         int xerrno = errno;
@@ -471,7 +473,7 @@ ipc_thread_1(void *in_params)
 
         Ip::Address::InitAddr(aiPS_ipc);
 
-        if (getsockname(pwfd_ipc, aiPS_ipc->ai_addr, &(aiPS_ipc->ai_addrlen)) < 0) {
+        if (xgetsockname(pwfd_ipc, aiPS_ipc->ai_addr, &(aiPS_ipc->ai_addrlen)) < 0) {
             int xerrno = errno;
             debugs(54, DBG_CRITICAL, "ipcCreate: getsockname: " << xstrerr(xerrno));
             ipcSend(cwfd, err_string, strlen(err_string));
@@ -486,7 +488,7 @@ ipc_thread_1(void *in_params)
 
         Ip::Address::InitAddr(aiCS_ipc);
 
-        if (getsockname(crfd_ipc, aiCS_ipc->ai_addr, &(aiCS_ipc->ai_addrlen)) < 0) {
+        if (xgetsockname(crfd_ipc, aiCS_ipc->ai_addr, &(aiCS_ipc->ai_addrlen)) < 0) {
             int xerrno = errno;
             debugs(54, DBG_CRITICAL, "ipcCreate: getsockname: " << xstrerr(xerrno));
             ipcSend(cwfd, err_string, strlen(err_string));
@@ -602,7 +604,7 @@ ipc_thread_1(void *in_params)
             goto cleanup;
         }
 
-        x = write(c2p[1], (const char *) &wpi, sizeof(wpi));
+        x = xwrite(c2p[1], (const char *) &wpi, sizeof(wpi));
 
         if (x < (ssize_t)sizeof(wpi)) {
             int xerrno = errno;
@@ -612,7 +614,7 @@ ipc_thread_1(void *in_params)
             goto cleanup;
         }
 
-        x = read(p2c[0], buf1, bufSz-1);
+        x = xread(p2c[0], buf1, bufSz-1);
 
         if (x < 0) {
             int xerrno = errno;
@@ -629,7 +631,7 @@ ipc_thread_1(void *in_params)
             goto cleanup;
         }
 
-        x = write(c2p[1], (const char *) &PS_ipc, sizeof(PS_ipc));
+        x = xwrite(c2p[1], (const char *) &PS_ipc, sizeof(PS_ipc));
 
         if (x < (ssize_t)sizeof(PS_ipc)) {
             int xerrno = errno;
@@ -639,7 +641,7 @@ ipc_thread_1(void *in_params)
             goto cleanup;
         }
 
-        x = read(p2c[0], buf1, bufSz-1);
+        x = xread(p2c[0], buf1, bufSz-1);
 
         if (x < 0) {
             int xerrno = errno;
@@ -656,8 +658,8 @@ ipc_thread_1(void *in_params)
             goto cleanup;
         }
 
-        x = send(pwfd_ipc, (const void *)ok_string, strlen(ok_string), 0);
-        x = recv(prfd_ipc, (void *)(buf1 + 200), bufSz -1 - 200, 0);
+        x = xsend(pwfd_ipc, ok_string, strlen(ok_string), 0);
+        x = xrecv(prfd_ipc, (void *)(buf1 + 200), bufSz -1 - 200, 0);
         assert((size_t) x == strlen(ok_string)
                && !strncmp(ok_string, buf1 + 200, strlen(ok_string)));
     }               /* IPC_UDP_SOCKET */
@@ -705,7 +707,7 @@ ipc_thread_1(void *in_params)
 
     /* cycle */
     for (;;) {
-        x = recv(crfd, (void *)buf1, bufSz-1, 0);
+        x = xrecv(crfd, (void *)buf1, bufSz-1, 0);
 
         if (x <= 0) {
             debugs(54, 3, "ipc(" << prog << "," << pid << "): " << x << " bytes received from parent. Exiting...");
@@ -724,9 +726,9 @@ ipc_thread_1(void *in_params)
         debugs(54, 5, "ipc(" << prog << "," << pid << "): received from parent: " << rfc1738_escape_unescaped(buf1));
 
         if (type == IPC_TCP_SOCKET)
-            x = write(c2p[1], buf1, x);
+            x = xwrite(c2p[1], buf1, x);
         else
-            x = send(pwfd_ipc, (const void *)buf1, x, 0);
+            x = xsend(pwfd_ipc, buf1, x, 0);
 
         if (x <= 0) {
             debugs(54, 3, "ipc(" << prog << "," << pid << "): " << x << " bytes written to " << prog << ". Exiting...");
@@ -746,7 +748,7 @@ cleanup:
         ipcCloseAllFD(-1, -1, crfd, cwfd);
 
     if (prfd_ipc != -1) {
-        send(crfd_ipc, (const void *)shutdown_string, strlen(shutdown_string), 0);
+        xsend(crfd_ipc, shutdown_string, strlen(shutdown_string), 0);
         shutdown(crfd_ipc, SD_BOTH);
         shutdown(prfd_ipc, SD_BOTH);
     }
@@ -804,9 +806,9 @@ ipc_thread_2(void *in_params)
 
     for (;;) {
         if (type == IPC_TCP_SOCKET)
-            x = read(rfd, buf2, bufSz-1);
+            x = xread(rfd, buf2, bufSz-1);
         else
-            x = recv(rfd, (void *)buf2, bufSz-1, 0);
+            x = xrecv(rfd, (void *)buf2, bufSz-1, 0);
 
         if ((x <= 0 && type == IPC_TCP_SOCKET) ||
                 (x < 0 && type == IPC_UDP_SOCKET)) {
@@ -832,7 +834,7 @@ ipc_thread_2(void *in_params)
 
         debugs(54, 5, "ipc(" << prog << "," << pid << "): received from child : " << rfc1738_escape_unescaped(buf2));
 
-        x = send(send_fd, (const void *)buf2, x, 0);
+        x = xsend(send_fd, buf2, x, 0);
 
         if ((x <= 0 && type == IPC_TCP_SOCKET) ||
                 (x < 0 && type == IPC_UDP_SOCKET)) {
index ba44937241a26188432934fbd5b9a833859b9fbc..d4ad05611aa3ea9cb8f5cbeec7461f6c0409cc32 100644 (file)
@@ -11,6 +11,7 @@
 #include "squid.h"
 #include "comm.h"
 #include "comm/Connection.h"
+#include "compat/unistd.h"
 #include "fatal.h"
 #include "fd.h"
 #include "fs_io.h"
@@ -40,8 +41,7 @@ static void
 logfile_mod_udp_write(Logfile * lf, const char *buf, size_t len)
 {
     l_udp_t *ll = (l_udp_t *) lf->data;
-    ssize_t s;
-    s = write(ll->fd, (char const *) buf, len);
+    const auto s = xwrite(ll->fd, buf, len);
     fd_bytes(ll->fd, s, IoDirection::Write);
 #if 0
     // TODO: Enable after polishing to properly log these errors.
index 97b6d6c6422ba9d5736d0ef30ea611230187c47c..9d1ef06a94880ec0b9bad2cbaaf577aa752c9794 100644 (file)
@@ -8,13 +8,12 @@
 
 #include "squid.h"
 
+#include "compat/unistd.h"
+
 #include <cassert>
 #include <cerrno>
 #include <csignal>
 #include <cstring>
-#if HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 #if HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
@@ -112,8 +111,8 @@ main(int argc, char *argv[])
     /* XXX stderr should not be closed, but in order to support squid must be
      * able to collect and manage modules' stderr first.
      */
-    close(2);
-    t = open(_PATH_DEVNULL, O_RDWR);
+    xclose(2);
+    t = xopen(_PATH_DEVNULL, O_RDWR);
     assert(t > -1);
     dup2(t, 2);
 
index 3e676752ae55ae1a28ff999d655f34315b0f3e53..f3177fe61b3323b893ae4d368a2b9a8f2e85e553 100644 (file)
@@ -25,6 +25,7 @@
 #include "client_side.h"
 #include "comm.h"
 #include "CommandLine.h"
+#include "compat/unistd.h"
 #include "ConfigParser.h"
 #include "CpuAffinity.h"
 #include "debug/Messages.h"
@@ -1857,11 +1858,8 @@ watch_child(const CommandLine &masterCommand)
     pid_t pid;
 #ifdef TIOCNOTTY
 
-    int i;
 #endif
 
-    int nullfd;
-
     // TODO: zero values are not supported because they result in
     // misconfigured SMP Squid instances running forever, endlessly
     // restarting each dying kid.
@@ -1879,7 +1877,7 @@ watch_child(const CommandLine &masterCommand)
 
 #ifdef TIOCNOTTY
 
-    if ((i = open("/dev/tty", O_RDWR | O_TEXT)) >= 0) {
+    if ((const auto i = xopen("/dev/tty", O_RDWR | O_TEXT)) >= 0) {
         ioctl(i, TIOCNOTTY, nullptr);
         close(i);
     }
@@ -1892,7 +1890,7 @@ watch_child(const CommandLine &masterCommand)
      * 1.1.3.  execvp had a bit overflow error in a loop..
      */
     /* Connect stdio to /dev/null in daemon mode */
-    nullfd = open(_PATH_DEVNULL, O_RDWR | O_TEXT);
+    const auto nullfd = xopen(_PATH_DEVNULL, O_RDWR | O_TEXT);
 
     if (nullfd < 0) {
         int xerrno = errno;
index cddf4ed039ef002350e4cdaae819ea60be3fc61d..d44b01be6f25c23fb5a54a5d54e406127c95f3a5 100644 (file)
@@ -11,6 +11,7 @@
 #include "squid.h"
 #include "CacheManager.h"
 #include "comm/Connection.h"
+#include "compat/unistd.h"
 #include "HttpReply.h"
 #include "ipc/Port.h"
 #include "mgr/Action.h"
@@ -91,7 +92,7 @@ Mgr::Action::respond(const Request &request)
     // Assume most kid classes are fully aggregatable (i.e., they do not dump
     // local info at all). Do not import the remote HTTP fd into our Comm
     // space; collect and send an IPC msg with collected info to Coordinator.
-    ::close(request.conn->fd);
+    xclose(request.conn->fd);
     request.conn->fd = -1;
     collect();
     sendResponse(request.requestId);
index 48366948555804c9776ec603ba2e2ba243a8dc5c..7601ca956b49ed7677e84c1dd330717e59bbe01b 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "squid.h"
 #include "comm/Connection.h"
+#include "compat/socket.h"
 #include "debug/Stream.h"
 // XXX: for icpIncomingConn - need to pass it as a generic parameter.
 #include "ICP.h"
@@ -22,7 +23,7 @@ mcastSetTtl(int fd, int mcast_ttl)
 #ifdef IP_MULTICAST_TTL
     char ttl = (char) mcast_ttl;
 
-    if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, 1) < 0) {
+    if (xsetsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, 1) < 0) {
         int xerrno = errno;
         debugs(50, DBG_IMPORTANT, "mcastSetTtl: FD " << fd << ", TTL: " << mcast_ttl << ": " << xstrerr(xerrno));
     }
@@ -54,11 +55,11 @@ mcastJoinGroups(const ipcache_addrs *ia, const Dns::LookupDetails &, void *)
 
         mr.imr_interface.s_addr = INADDR_ANY;
 
-        if (setsockopt(icpIncomingConn->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mr, sizeof(struct ip_mreq)) < 0)
+        if (xsetsockopt(icpIncomingConn->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mr, sizeof(struct ip_mreq)) < 0)
             debugs(7, DBG_IMPORTANT, "ERROR: Join failed for " << icpIncomingConn << ", Multicast IP=" << ip);
 
         char c = 0;
-        if (setsockopt(icpIncomingConn->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &c, 1) < 0) {
+        if (xsetsockopt(icpIncomingConn->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &c, 1) < 0) {
             int xerrno = errno;
             debugs(7, DBG_IMPORTANT, "ERROR: " << icpIncomingConn << " can't disable multicast loopback: " << xstrerr(xerrno));
         }
index a6d13aba25180ec9dfb4f8ec55347bd29eee0948..e09461f1850e722cf4aa25d1fe77dcdcbbbd236c 100644 (file)
@@ -20,6 +20,7 @@
 #include "CachePeers.h"
 #include "comm/Connection.h"
 #include "comm/ConnOpener.h"
+#include "compat/netdb.h"
 #include "debug/Messages.h"
 #include "event.h"
 #include "FwdState.h"
@@ -503,7 +504,6 @@ neighborsRegisterWithCacheManager()
 void
 neighbors_init(void)
 {
-    struct servent *sep = nullptr;
     const char *me = getMyHostname();
 
     neighborsRegisterWithCacheManager();
@@ -536,7 +536,7 @@ neighbors_init(void)
 
     peerDnsRefreshStart();
 
-    sep = getservbyname("echo", "udp");
+    const auto sep = xgetservbyname("echo", "udp");
     echo_port = sep ? ntohs((unsigned short) sep->s_port) : 7;
 }
 
index e6eb011a0a21f7bd8bc7daaf5429cbc069f27391..df293ca2c953718e26ae019678a0f18a53704f5a 100644 (file)
@@ -9,6 +9,7 @@
 #include "squid.h"
 #include "base/HardFun.h"
 #include "base/TextException.h"
+#include "compat/unistd.h"
 #include "sbuf/Stream.h"
 #include "security/cert_generators/file/certificate_db.h"
 
@@ -52,7 +53,7 @@ void Ssl::Lock::lock()
     hFile = CreateFile(TEXT(filename.c_str()), GENERIC_READ, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
     if (hFile == INVALID_HANDLE_VALUE)
 #else
-    fd = open(filename.c_str(), O_RDWR);
+    fd = xopen(filename.c_str(), O_RDWR);
     if (fd == -1)
 #endif
         throw TextException(ToSBuf("Failed to open file ", filename), Here());
@@ -82,7 +83,7 @@ void Ssl::Lock::unlock()
 #else
         flock(fd, LOCK_UN);
 #endif
-        close(fd);
+        xclose(fd);
         fd = -1;
     }
 #endif
index 505a70381d608c1edf025a5a163f255bfa6a9473..4dc29ae11e5ca64e2f579acef7e3fe608b14ee49 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "squid.h"
 #include "compat/cppunit.h"
+#include "compat/netdb.h"
 #include "ip/Address.h"
 #include "ip/tools.h"
 #include "unitTestMain.h"
@@ -21,9 +22,6 @@
 #if HAVE_ARPA_INET_H
 #include <arpa/inet.h>
 #endif
-#if HAVE_NETDB_H
-#include <netdb.h>
-#endif
 
 /*
  * test the IP storage type
@@ -235,13 +233,12 @@ TestIpAddress::testCopyConstructor()
 void
 TestIpAddress::testHostentConstructor()
 {
-    struct hostent *hp = nullptr;
     struct in_addr outval;
     struct in_addr expectval;
 
     expectval.s_addr = htonl(0xC0A8640C);
 
-    hp = gethostbyname("192.168.100.12");
+    const auto hp = xgethostbyname("192.168.100.12");
     CPPUNIT_ASSERT( hp != nullptr /* gethostbyname failure.*/ );
 
     Ip::Address anIPA(*hp);
index cbc74d05c9ecc588a145f5f2b0588e17a68ff5b1..989f55636e6e42a7968296a10c797ae460d7c0eb 100644 (file)
@@ -12,6 +12,7 @@
 #include "anyp/PortCfg.h"
 #include "base/Subscription.h"
 #include "client_side.h"
+#include "compat/unistd.h"
 #include "fatal.h"
 #include "fde.h"
 #include "fqdncache.h"
@@ -508,7 +509,7 @@ getMyHostname(void)
     }
 
     // still no host. fallback to gethostname()
-    if (gethostname(host, SQUIDHOSTNAMELEN) < 0) {
+    if (xgethostname(host, SQUIDHOSTNAMELEN) < 0) {
         int xerrno = errno;
         debugs(50, DBG_IMPORTANT, "WARNING: gethostname failed: " << xstrerr(xerrno));
     } else {
index 4d730be96180e08cd7be1031fbec18bcce2b2564..57ccebf5744d739d9f544165dff6f9b008d6c1dd 100644 (file)
@@ -11,6 +11,8 @@
 #include "squid.h"
 
 #if USE_UNLINKD
+#include "compat/select.h"
+#include "compat/unistd.h"
 #include "fd.h"
 #include "fde.h"
 #include "fs_io.h"
@@ -40,7 +42,6 @@ unlinkdUnlink(const char *path)
 {
     char buf[MAXPATHLEN];
     int l;
-    int bytes_written;
     static int queuelen = 0;
 
     if (unlinkd_wfd < 0) {
@@ -74,7 +75,7 @@ unlinkdUnlink(const char *path)
         FD_SET(unlinkd_rfd, &R);
         to.tv_sec = 0;
         to.tv_usec = 100000;
-        select(unlinkd_rfd + 1, &R, nullptr, nullptr, &to);
+        xselect(unlinkd_rfd + 1, &R, nullptr, nullptr, &to);
 #endif
     }
 
@@ -85,10 +86,9 @@ unlinkdUnlink(const char *path)
     * decrement the queue size by the number of newlines read.
     */
     if (queuelen > 0) {
-        int bytes_read;
         int i;
         char rbuf[512];
-        bytes_read = read(unlinkd_rfd, rbuf, 511);
+        const auto bytes_read = xread(unlinkd_rfd, rbuf, 511);
 
         if (bytes_read > 0) {
             rbuf[bytes_read] = '\0';
@@ -106,7 +106,7 @@ unlinkdUnlink(const char *path)
     xstrncpy(buf, path, MAXPATHLEN);
     buf[l] = '\n';
     ++l;
-    bytes_written = write(unlinkd_wfd, buf, l);
+    const auto bytes_written = xwrite(unlinkd_wfd, buf, l);
 
     if (bytes_written < 0) {
         int xerrno = errno;
index 810e78d3dc131052baf9f46688614242adaddc47..50cd776916ff0fbfaa45f5fd6e53170facbf7f88 100644 (file)
@@ -12,6 +12,8 @@
 
 #include "squid.h"
 
+#include "compat/unistd.h"
+
 #include <iostream>
 #include <cstdio>
 #if HAVE_PATHS_H
@@ -53,7 +55,7 @@ main(int, char *[])
 {
     std::string sbuf;
     close(2);
-    if (open(_PATH_DEVNULL, O_RDWR) < 0) {
+    if (xopen(_PATH_DEVNULL, O_RDWR) < 0) {
         ; // the irony of having to close(2) earlier is that we cannot report this failure.
     }
     while (getline(std::cin, sbuf)) {
index 44792d10f70fe54780065270911d8f6a073dfb46..e5a902ef3a409bcd3e8bd828863129c2f25e711a 100644 (file)
@@ -15,6 +15,7 @@
 #include "comm.h"
 #include "comm/Connection.h"
 #include "comm/Loops.h"
+#include "compat/socket.h"
 #include "event.h"
 #include "fatal.h"
 #include "SquidConfig.h"
@@ -144,13 +145,13 @@ wccpConnectionOpen(void)
 
     struct sockaddr_in router;
     Config.Wccp.router.getSockAddr(router);
-    if (connect(theWccpConnection, (struct sockaddr*)&router, sizeof(router)))
+    if (xconnect(theWccpConnection, (struct sockaddr*)&router, sizeof(router)))
         fatal("Unable to connect WCCP out socket");
 
     struct sockaddr_in local;
     memset(&local, '\0', sizeof(local));
     socklen_t slen = sizeof(local);
-    if (getsockname(theWccpConnection, (struct sockaddr*)&local, &slen))
+    if (xgetsockname(theWccpConnection, (struct sockaddr*)&local, &slen))
         fatal("Unable to getsockname on WCCP out socket");
 
     local_ip = local;
index 38fc2d4ece3bdabca767fcce4fbc9a8c5ccb9c71..92e10efd587f1c15ca8ff217d4e4dbc82b64e170 100644 (file)
@@ -16,6 +16,7 @@
 #include "comm.h"
 #include "comm/Connection.h"
 #include "comm/Loops.h"
+#include "compat/socket.h"
 #include "ConfigParser.h"
 #include "event.h"
 #include "ip/Address.h"
 #include "tools.h"
 #include "wccp2.h"
 
-#if HAVE_NETDB_H
-#include <netdb.h>
-#endif
-
 #define WCCP_PORT 2048
 #define WCCP_RESPONSE_SIZE 12448
 #define WCCP_BUCKETS 256
@@ -990,7 +987,7 @@ wccp2ConnectionOpen(void)
 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
     {
         int i = IP_PMTUDISC_DONT;
-        if (setsockopt(theWccp2Connection, SOL_IP, IP_MTU_DISCOVER, &i, sizeof i) < 0) {
+        if (xsetsockopt(theWccp2Connection, SOL_IP, IP_MTU_DISCOVER, &i, sizeof i) < 0) {
             int xerrno = errno;
             debugs(80, 2, "WARNING: Path MTU discovery could not be disabled on FD " << theWccp2Connection << ": " << xstrerr(xerrno));
         }
@@ -1017,14 +1014,14 @@ wccp2ConnectionOpen(void)
             router.sin_port = htons(WCCP_PORT);
             router.sin_addr = router_list_ptr->router_sendto_address;
 
-            if (connect(theWccp2Connection, (struct sockaddr *) &router, router_len))
+            if (xconnect(theWccp2Connection, (struct sockaddr *) &router, router_len))
                 fatal("Unable to connect WCCP out socket");
 
             local_len = sizeof(local);
 
             memset(&local, '\0', local_len);
 
-            if (getsockname(theWccp2Connection, (struct sockaddr *) &local, &local_len))
+            if (xgetsockname(theWccp2Connection, (struct sockaddr *) &local, &local_len))
                 fatal("Unable to getsockname on WCCP out socket");
 
             router_list_ptr->local_ip = local.sin_addr;
@@ -1033,7 +1030,7 @@ wccp2ConnectionOpen(void)
              * but disconnects anyway so we have to just assume it worked
              */
             if (wccp2_numrouters > 1) {
-                (void)connect(theWccp2Connection, (struct sockaddr *) &null, router_len);
+                (void)xconnect(theWccp2Connection, (struct sockaddr *) &null, router_len);
             }
         }
 
@@ -1660,7 +1657,7 @@ wccp2HereIam(void *)
                                 &service_list_ptr->wccp_packet,
                                 service_list_ptr->wccp_packet_size);
             } else {
-                if (send(theWccp2Connection, &service_list_ptr->wccp_packet, service_list_ptr->wccp_packet_size, 0) < static_cast<int>(service_list_ptr->wccp_packet_size)) {
+                if (xsend(theWccp2Connection, &service_list_ptr->wccp_packet, service_list_ptr->wccp_packet_size, 0) < static_cast<int>(service_list_ptr->wccp_packet_size)) {
                     int xerrno = errno;
                     debugs(80, 2, "ERROR: failed to send WCCPv2 HERE_I_AM packet to " << router << " : " << xstrerr(xerrno));
                 }
@@ -2044,7 +2041,7 @@ wccp2AssignBuckets(void *)
                                     &wccp_packet,
                                     offset);
                 } else {
-                    if (send(theWccp2Connection, &wccp_packet, offset, 0) < static_cast<int>(offset)) {
+                    if (xsend(theWccp2Connection, &wccp_packet, offset, 0) < offset) {
                         int xerrno = errno;
                         debugs(80, 2, "ERROR: failed to send WCCPv2 HERE_I_AM packet to " << tmp_rtr << " : " << xstrerr(xerrno));
                     }