]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/fd.cc
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / fd.cc
index 4f3fc0780d285fcf92cc327a13e9ebc4765fa495..421f7a3cbabe80b70bc0d724fb137df745af2b07 100644 (file)
--- a/src/fd.cc
+++ b/src/fd.cc
@@ -1,55 +1,33 @@
-
 /*
- * $Id$
- *
- * DEBUG: section 51    Filedescriptor Functions
- * AUTHOR: Duane Wessels
- *
- * SQUID Web Proxy Cache          http://www.squid-cache.org/
- * ----------------------------------------------------------
- *
- *  Squid is the result of efforts by numerous individuals from
- *  the Internet community; see the CONTRIBUTORS file for full
- *  details.   Many organizations have provided support for Squid's
- *  development; see the SPONSORS file for full details.  Squid is
- *  Copyrighted (C) 2001 by the Regents of the University of
- *  California; see the COPYRIGHT file for full details.  Squid
- *  incorporates software developed and/or copyrighted by other
- *  sources; see the CREDITS file for full details.
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
+ * Copyright (C) 1996-2020 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-old.h"
+/* DEBUG: section 51    Filedescriptor Functions */
+
+#include "squid.h"
 #include "comm/Loops.h"
+#include "Debug.h"
+#include "fatal.h"
+#include "fd.h"
 #include "fde.h"
+#include "globals.h"
+#include "profiler/Profiler.h"
 #include "SquidTime.h"
-#include "Debug.h"
-
 
 // Solaris and possibly others lack MSG_NOSIGNAL optimization
 // TODO: move this into compat/? Use a dedicated compat file to avoid dragging
-// sys/types.h and sys/socket.h into the rest of Squid??
+// 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_MSWIN_
+#if _SQUID_WINDOWS_
 int socket_read_method(int, char *, int);
 int socket_write_method(int, const char *, int);
 int file_read_method(int, char *, int);
@@ -106,7 +84,7 @@ fd_close(int fd)
     fde *F = &fd_table[fd];
 
     assert(fd >= 0);
-    assert(F->flags.open == 1);
+    assert(F->flags.open);
 
     if (F->type == FD_FILE) {
         assert(F->read_handler == NULL);
@@ -114,15 +92,14 @@ fd_close(int fd)
     }
 
     debugs(51, 3, "fd_close FD " << fd << " " << F->desc);
-    Comm::SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
-    Comm::SetSelect(fd, COMM_SELECT_WRITE, NULL, NULL, 0);
-    F->flags.open = 0;
+    Comm::ResetSelect(fd);
+    F->flags.open = false;
     fdUpdateBiggest(fd, 0);
     --Number_FD;
-    *F = fde();
+    F->clear();
 }
 
-#if _SQUID_MSWIN_
+#if _SQUID_WINDOWS_
 
 int
 socket_read_method(int fd, char *buf, int len)
@@ -186,7 +163,7 @@ default_write_method(int fd, const char *buf, int len)
 }
 
 int
-msghdr_read_method(int fd, char *buf, int len)
+msghdr_read_method(int fd, char *buf, int)
 {
     PROF_start(read);
     const int i = recvmsg(fd, reinterpret_cast<msghdr*>(buf), MSG_DONTWAIT);
@@ -220,9 +197,9 @@ fd_open(int fd, unsigned int type, const char *desc)
     assert(!F->flags.open);
     debugs(51, 3, "fd_open() FD " << fd << " " << desc);
     F->type = type;
-    F->flags.open = 1;
+    F->flags.open = true;
     F->epoll_state = 0;
-#if _SQUID_MSWIN_
+#if _SQUID_WINDOWS_
 
     F->win32.handle = _get_osfhandle(fd);
 
@@ -231,15 +208,13 @@ fd_open(int fd, unsigned int type, const char *desc)
     case FD_SOCKET:
 
     case FD_PIPE:
-        F->read_method = &socket_read_method;
-        F->write_method = &socket_write_method;
+        F->setIo(&socket_read_method, &socket_write_method);
         break;
 
     case FD_FILE:
 
     case FD_LOG:
-        F->read_method = &file_read_method;
-        F->write_method = &file_write_method;
+        F->setIo(&file_read_method, &file_write_method);
         break;
 
     default:
@@ -250,13 +225,11 @@ fd_open(int fd, unsigned int type, const char *desc)
     switch (type) {
 
     case FD_MSGHDR:
-        F->read_method = &msghdr_read_method;
-        F->write_method = &msghdr_write_method;
+        F->setIo(&msghdr_read_method, &msghdr_write_method);
         break;
 
     default:
-        F->read_method = &default_read_method;
-        F->write_method = &default_write_method;
+        F->setIo(&default_read_method, &default_write_method);
         break;
     }
 
@@ -264,8 +237,7 @@ fd_open(int fd, unsigned int type, const char *desc)
 
     fdUpdateBiggest(fd, 1);
 
-    if (desc)
-        xstrncpy(F->desc, desc, FD_DESC_SZ);
+    fd_note(fd, desc);
 
     ++Number_FD;
 }
@@ -274,7 +246,10 @@ void
 fd_note(int fd, const char *s)
 {
     fde *F = &fd_table[fd];
-    xstrncpy(F->desc, s, FD_DESC_SZ);
+    if (s)
+        xstrncpy(F->desc, s, FD_DESC_SZ);
+    else
+        *(F->desc) = 0; // ""-string
 }
 
 void
@@ -362,13 +337,15 @@ fdAdjustReserved(void)
 
     if (newReserve > x) {
         /* perhaps this should be fatal()? -DW */
-        debugs(51, 0, "WARNING: This machine has a serious shortage of filedescriptors.");
+        debugs(51, DBG_CRITICAL, "WARNING: This machine has a serious shortage of filedescriptors.");
         newReserve = x;
     }
 
     if (Squid_MaxFD - newReserve < min(256, Squid_MaxFD / 2))
         fatalf("Too few filedescriptors available in the system (%d usable of %d).\n", Squid_MaxFD - newReserve, Squid_MaxFD);
 
-    debugs(51, 0, "Reserved FD adjusted from " << RESERVED_FD << " to " << newReserve << " due to failures");
+    debugs(51, DBG_CRITICAL, "Reserved FD adjusted from " << RESERVED_FD << " to " << newReserve <<
+           " due to failures (" << (Squid_MaxFD - newReserve) << "/" << Squid_MaxFD << " file descriptors available)");
     RESERVED_FD = newReserve;
 }
+