]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/log/ModDaemon.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / log / ModDaemon.cc
index d1f0fcf5e56ed6a314390875e33a92534ce9732f..fe2907d140bc72222f45abfb82d075c00d85b6d4 100644 (file)
@@ -1,54 +1,39 @@
 /*
- * DEBUG: section 50    Log file handling
- * AUTHOR: Adrian Chadd <adrian@squid-cache.org>
- *
- * 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-2021 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.
  */
 
+/* DEBUG: section 50    Log file handling */
+
 #include "squid.h"
 #include "cbdata.h"
+#include "comm/Loops.h"
+#include "fatal.h"
 #include "fde.h"
+#include "globals.h"
 #include "log/Config.h"
 #include "log/File.h"
 #include "log/ModDaemon.h"
+#include "SquidConfig.h"
+#include "SquidIpc.h"
 #include "SquidTime.h"
 
+#include <cerrno>
+
 /* How many buffers to keep before we say we've buffered too much */
-#define        LOGFILE_MAXBUFS         128
+#define LOGFILE_MAXBUFS     128
 
 /* Size of the logfile buffer */
 /*
  * For optimal performance this should match LOGFILE_BUFSIZ in logfile-daemon.c
  */
-#define        LOGFILE_BUFSZ           32768
+#define LOGFILE_BUFSZ       32768
 
 /* How many seconds between warnings */
-#define        LOGFILE_WARN_TIME       30
+#define LOGFILE_WARN_TIME   30
 
 static LOGWRITE logfile_mod_daemon_writeline;
 static LOGLINESTART logfile_mod_daemon_linestart;
@@ -88,7 +73,7 @@ logfileNewBuffer(Logfile * lf)
     b->written_len = 0;
     b->len = 0;
     dlinkAddTail(b, &b->node, &ll->bufs);
-    ll->nbufs++;
+    ++ ll->nbufs;
 }
 
 static void
@@ -97,36 +82,40 @@ logfileFreeBuffer(Logfile * lf, logfile_buffer_t * b)
     l_daemon_t *ll = (l_daemon_t *) lf->data;
     assert(b != NULL);
     dlinkDelete(&b->node, &ll->bufs);
-    ll->nbufs--;
+    -- ll->nbufs;
     xfree(b->buf);
     xfree(b);
 }
 
 static void
-logfileHandleWrite(int fd, void *data)
+logfileHandleWrite(int, void *data)
 {
-    Logfile *lf = (Logfile *) data;
-    l_daemon_t *ll = (l_daemon_t *) lf->data;
-    int ret;
-    logfile_buffer_t *b;
+    Logfile *lf = static_cast<Logfile *>(data);
+    l_daemon_t *ll = static_cast<l_daemon_t *>(lf->data);
 
     /*
      * We'll try writing the first entry until its done - if we
      * get a partial write then we'll re-schedule until its completed.
      * Its naive but it'll do for now.
      */
-    b = static_cast<logfile_buffer_t*>(ll->bufs.head->data);
+    if (!ll->bufs.head) // abort if there is nothing pending right now.
+        return;
+
+    logfile_buffer_t *b = static_cast<logfile_buffer_t*>(ll->bufs.head->data);
     assert(b != NULL);
     ll->flush_pending = 0;
 
-    ret = FD_WRITE_METHOD(ll->wfd, b->buf + b->written_len, b->len - b->written_len);
-    debugs(50, 3, "logfileHandleWrite: " << lf->path << ": write returned " << ret);
+    int ret = FD_WRITE_METHOD(ll->wfd, b->buf + b->written_len, b->len - b->written_len);
+    int xerrno = errno;
+    debugs(50, 3, lf->path << ": write returned " << ret);
     if (ret < 0) {
-        if (ignoreErrno(errno)) {
+        if (ignoreErrno(xerrno)) {
             /* something temporary */
-            goto reschedule;
+            Comm::SetSelect(ll->wfd, COMM_SELECT_WRITE, logfileHandleWrite, lf, 0);
+            ll->flush_pending = 1;
+            return;
         }
-        debugs(50, DBG_IMPORTANT,"logfileHandleWrite: " << lf->path << ": error writing (" << xstrerror() << ")");
+        debugs(50, DBG_IMPORTANT,"logfileHandleWrite: " << lf->path << ": error writing (" << xstrerr(xerrno) << ")");
         /* XXX should handle this better */
         fatal("I don't handle this error well!");
     }
@@ -145,15 +134,12 @@ logfileHandleWrite(int fd, void *data)
         b = NULL;
     }
     /* Is there more to write? */
-    if (ll->bufs.head == NULL) {
-        goto finish;
-    }
+    if (!ll->bufs.head)
+        return;
     /* there is, so schedule more */
 
-reschedule:
-    commSetSelect(ll->wfd, COMM_SELECT_WRITE, logfileHandleWrite, lf, 0);
+    Comm::SetSelect(ll->wfd, COMM_SELECT_WRITE, logfileHandleWrite, lf, 0);
     ll->flush_pending = 1;
-finish:
     return;
 }
 
@@ -171,7 +157,7 @@ logfileQueueWrite(Logfile * lf)
             logfile_mod_daemon_append(lf, "F\n", 2);
     }
     /* Ok, schedule a write-event */
-    commSetSelect(ll->wfd, COMM_SELECT_WRITE, logfileHandleWrite, lf, 0);
+    Comm::SetSelect(ll->wfd, COMM_SELECT_WRITE, logfileHandleWrite, lf, 0);
 }
 
 static void
@@ -219,11 +205,10 @@ logfileFlushEvent(void *data)
     eventAdd("logfileFlush", logfileFlushEvent, lf, 1.0, 1);
 }
 
-
 /* External code */
 
 int
-logfile_mod_daemon_open(Logfile * lf, const char *path, size_t bufsz, int fatal_flag)
+logfile_mod_daemon_open(Logfile * lf, const char *path, size_t, int)
 {
     const char *args[5];
     char *tmpbuf;
@@ -237,7 +222,7 @@ logfile_mod_daemon_open(Logfile * lf, const char *path, size_t bufsz, int fatal_
     lf->f_rotate = logfile_mod_daemon_rotate;
 
     cbdataInternalLock(lf); // WTF?
-    debugs(50, 1, "Logfile Daemon: opening log " << path);
+    debugs(50, DBG_IMPORTANT, "Logfile Daemon: opening log " << path);
     ll = static_cast<l_daemon_t*>(xcalloc(1, sizeof(*ll)));
     lf->data = ll;
     ll->eol = 1;
@@ -246,7 +231,7 @@ logfile_mod_daemon_open(Logfile * lf, const char *path, size_t bufsz, int fatal_
         args[0] = "(logfile-daemon)";
         args[1] = path;
         args[2] = NULL;
-        localhost.SetLocalhost();
+        localhost.setLocalhost();
         ll->pid = ipcCreate(IPC_STREAM, Log::TheConfig.logfile_daemon, args, "logfile-daemon", localhost, &ll->rfd, &ll->wfd, NULL);
         if (ll->pid < 0)
             fatal("Couldn't start logfile helper");
@@ -269,7 +254,7 @@ static void
 logfile_mod_daemon_close(Logfile * lf)
 {
     l_daemon_t *ll = static_cast<l_daemon_t *>(lf->data);
-    debugs(50, 1, "Logfile Daemon: closing log " << lf->path);
+    debugs(50, DBG_IMPORTANT, "Logfile Daemon: closing log " << lf->path);
     logfileFlush(lf);
     if (ll->rfd == ll->wfd)
         comm_close(ll->rfd);
@@ -285,10 +270,10 @@ logfile_mod_daemon_close(Logfile * lf)
 }
 
 static void
-logfile_mod_daemon_rotate(Logfile * lf)
+logfile_mod_daemon_rotate(Logfile * lf, const int16_t)
 {
     char tb[3];
-    debugs(50, 1, "logfileRotate: " << lf->path);
+    debugs(50, DBG_IMPORTANT, "logfileRotate: " << lf->path);
     tb[0] = 'R';
     tb[1] = '\n';
     tb[2] = '\0';
@@ -313,8 +298,14 @@ logfile_mod_daemon_writeline(Logfile * lf, const char *buf, size_t len)
         }
         return;
     }
-    /* Append this data to the end buffer; create a new one if needed */
+
     /* Are we eol? If so, prefix with our logfile command byte */
+    if (ll->eol == 1) {
+        logfile_mod_daemon_append(lf, "L", 1);
+        ll->eol = 0;
+    }
+
+    /* Append this data to the end buffer; create a new one if needed */
     logfile_mod_daemon_append(lf, buf, len);
 }
 
@@ -322,12 +313,8 @@ static void
 logfile_mod_daemon_linestart(Logfile * lf)
 {
     l_daemon_t *ll = static_cast<l_daemon_t *>(lf->data);
-    char tb[2];
     assert(ll->eol == 1);
-    ll->eol = 0;
-    tb[0] = 'L';
-    tb[1] = '\0';
-    logfile_mod_daemon_append(lf, tb, 1);
+    // logfile_mod_daemon_writeline() sends the starting command
 }
 
 static void
@@ -335,7 +322,8 @@ logfile_mod_daemon_lineend(Logfile * lf)
 {
     l_daemon_t *ll = static_cast<l_daemon_t *>(lf->data);
     logfile_buffer_t *b;
-    assert(ll->eol == 0);
+    if (ll->eol == 1) // logfile_mod_daemon_writeline() wrote nothing
+        return;
     ll->eol = 1;
     /* Kick a write off if the head buffer is -full- */
     if (ll->bufs.head != NULL) {
@@ -361,3 +349,4 @@ logfile_mod_daemon_flush(Logfile * lf)
         return;
     }
 }
+