]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/DiskIO/DiskDaemon/diskd.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / DiskIO / DiskDaemon / diskd.cc
index 7bfd07702ef141c8e396e477d05c8cbd37eb815e..d4e93b18824551da68908e676725dbfa6efdebbe 100644 (file)
@@ -1,57 +1,43 @@
 /*
- * $Id: diskd.cc,v 1.1 2004/12/20 16:30:38 robertc Exp $
- *
- * DEBUG: section --    External DISKD process implementation.
- * AUTHOR: Harvest Derived
- *
- * 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-2015 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 "config.h"
+/* DEBUG: section --    External DISKD process implementation. */
+
 #include "squid.h"
+#include "DiskIO/DiskDaemon/diomsg.h"
+#include "hash.h"
 
+#include <cerrno>
+#include <iostream>
+#if HAVE_SYS_IPC_H
 #include <sys/ipc.h>
+#endif
+#if HAVE_SYS_MSG_H
 #include <sys/msg.h>
+#endif
+#if HAVE_SYS_SHM_H
 #include <sys/shm.h>
+#endif
 
-#include "DiskIO/DiskDaemon/diomsg.h"
-
-#undef assert
-#include <assert.h>
+void
+xassert(const char *msg, const char *file, int line)
+{
+    fprintf(stderr,"assertion failed: %s:%d: \"%s\"\n", file, line, msg);
 
+    abort();
+}
 
 const int diomsg::msg_snd_rcv_sz = sizeof(diomsg) - sizeof(mtyp_t);
 #define DEBUG(LEVEL) if ((LEVEL) <= DebugLevel)
 
 typedef struct _file_state file_state;
 
-struct _file_state
-{
+struct _file_state {
     void *key;
     file_state *next;
     int id;
@@ -62,10 +48,10 @@ struct _file_state
 static hash_table *hash = NULL;
 static pid_t mypid;
 static char *shmbuf;
-static int DebugLevel = 1;
+static int DebugLevel = 0;
 
 static int
-do_open(diomsg * r, int len, const char *buf)
+do_open(diomsg * r, int, const char *buf)
 {
     int fd;
     file_state *fs;
@@ -85,20 +71,21 @@ do_open(diomsg * r, int len, const char *buf)
 
     fs = (file_state *)xcalloc(1, sizeof(*fs));
     fs->id = r->id;
-    fs->key = &fs->id;         /* gack */
+    fs->key = &fs->id;          /* gack */
     fs->fd = fd;
     hash_join(hash, (hash_link *) fs);
-    DEBUG(2)
-    fprintf(stderr, "%d OPEN  id %d, FD %d, fs %p\n",
-            (int) mypid,
-            fs->id,
-            fs->fd,
-            fs);
+    DEBUG(2) {
+        fprintf(stderr, "%d OPEN  id %d, FD %d, fs %p\n",
+                (int) mypid,
+                fs->id,
+                fs->fd,
+                fs);
+    }
     return fd;
 }
 
 static int
-do_close(diomsg * r, int len)
+do_close(diomsg * r, int)
 {
     int fd;
     file_state *fs;
@@ -116,18 +103,19 @@ do_close(diomsg * r, int len)
 
     fd = fs->fd;
     hash_remove_link(hash, (hash_link *) fs);
-    DEBUG(2)
-    fprintf(stderr, "%d CLOSE id %d, FD %d, fs %p\n",
-            (int) mypid,
-            r->id,
-            fs->fd,
-            fs);
+    DEBUG(2) {
+        fprintf(stderr, "%d CLOSE id %d, FD %d, fs %p\n",
+                (int) mypid,
+                r->id,
+                fs->fd,
+                fs);
+    }
     xfree(fs);
     return close(fd);
 }
 
 static int
-do_read(diomsg * r, int len, char *buf)
+do_read(diomsg * r, int, char *buf)
 {
     int x;
     int readlen = r->size;
@@ -145,21 +133,23 @@ do_read(diomsg * r, int len, char *buf)
     }
 
     if (r->offset > -1 && r->offset != fs->offset) {
-        DEBUG(2)
-        fprintf(stderr, "seeking to %d\n", r->offset);
+        DEBUG(2) {
+            fprintf(stderr, "seeking to %" PRId64 "\n", (int64_t)r->offset);
+        }
 
         if (lseek(fs->fd, r->offset, SEEK_SET) < 0) {
             DEBUG(1) {
-                fprintf(stderr, "%d FD %d, offset %d: ", (int) mypid, fs->fd, r->offset);
+                fprintf(stderr, "%d FD %d, offset %" PRId64 ": ", (int) mypid, fs->fd, (int64_t)r->offset);
                 perror("lseek");
             }
         }
     }
 
     x = read(fs->fd, buf, readlen);
-    DEBUG(2)
-    fprintf(stderr, "%d READ %d,%d,%d ret %d\n", (int) mypid,
-            fs->fd, readlen, r->offset, x);
+    DEBUG(2) {
+        fprintf(stderr, "%d READ %d,%d,%" PRId64 " ret %d\n", (int) mypid,
+                fs->fd, readlen, (int64_t)r->offset, x);
+    }
 
     if (x < 0) {
         DEBUG(1) {
@@ -175,7 +165,7 @@ do_read(diomsg * r, int len, char *buf)
 }
 
 static int
-do_write(diomsg * r, int len, const char *buf)
+do_write(diomsg * r, int, const char *buf)
 {
     int wrtlen = r->size;
     int x;
@@ -195,15 +185,16 @@ do_write(diomsg * r, int len, const char *buf)
     if (r->offset > -1 && r->offset != fs->offset) {
         if (lseek(fs->fd, r->offset, SEEK_SET) < 0) {
             DEBUG(1) {
-                fprintf(stderr, "%d FD %d, offset %d: ", (int) mypid, fs->fd, r->offset);
+                fprintf(stderr, "%d FD %d, offset %" PRId64 ": ", (int) mypid, fs->fd, (int64_t)r->offset);
                 perror("lseek");
             }
         }
     }
 
-    DEBUG(2)
-    fprintf(stderr, "%d WRITE %d,%d,%d\n", (int) mypid,
-            fs->fd, wrtlen, r->offset);
+    DEBUG(2) {
+        fprintf(stderr, "%d WRITE %d,%d,%" PRId64 "\n", (int) mypid,
+                fs->fd, wrtlen, (int64_t)r->offset);
+    }
     x = write(fs->fd, buf, wrtlen);
 
     if (x < 0) {
@@ -220,27 +211,20 @@ do_write(diomsg * r, int len, const char *buf)
 }
 
 static int
-do_unlink(diomsg * r, int len, const char *buf)
+do_unlink(diomsg * r, int, const char *buf)
 {
-#if USE_TRUNCATE
-
-    if (truncate(buf, 0) < 0)
-#else
-
-    if (unlink(buf) < 0)
-#endif
-
-    {
+    if (unlink(buf) < 0) {
         DEBUG(1) {
             fprintf(stderr, "%d UNLNK id %d %s: ", (int) mypid, r->id, buf);
-            perror("truncate");
+            perror("unlink");
         }
 
         return -errno;
     }
 
-    DEBUG(2)
-    fprintf(stderr, "%d UNLNK %s\n", (int) mypid, buf);
+    DEBUG(2) {
+        fprintf(stderr, "%d UNLNK %s\n", (int) mypid, buf);
+    }
     return 0;
 }
 
@@ -249,14 +233,21 @@ msg_handle(diomsg * r, int rl, diomsg * s)
 {
     char *buf = NULL;
     s->mtype = r->mtype;
+    s->id = r->id;
+    s->seq_no = r->seq_no;      /* optional, debugging */
     s->callback_data = r->callback_data;
     s->requestor = r->requestor;
+    s->size = 0;                /* optional, debugging */
+    s->offset = 0;              /* optional, debugging */
     s->shm_offset = r->shm_offset;
-    s->id = r->id;
     s->newstyle = r->newstyle;
 
     if (s->shm_offset > -1)
         buf = shmbuf + s->shm_offset;
+    else if (r->mtype != _MQD_CLOSE) {
+        fprintf(stderr, "%d UNLNK id(%u) Error: no filename in shm buffer\n", (int) mypid, s->id);
+        return;
+    }
 
     switch (r->mtype) {
 
@@ -304,11 +295,9 @@ fsHash(const void *key, unsigned int n)
     return (*k & (--n));
 }
 
-static void
-alarm_handler(int sig)
-{
-    (void) 0;
-}
+extern "C" {
+    static void alarm_handler(int) {}
+};
 
 int
 main(int argc, char *argv[])
@@ -360,7 +349,10 @@ main(int argc, char *argv[])
 
     hash = hash_create(fsCmp, 1 << 4, fsHash);
     assert(hash);
-    fcntl(0, F_SETFL, SQUID_NONBLOCK);
+    if (fcntl(0, F_SETFL, SQUID_NONBLOCK) < 0) {
+        perror(xstrerror());
+        return 1;
+    }
     memset(&sa, '\0', sizeof(sa));
     sa.sa_handler = alarm_handler;
     sa.sa_flags = SA_RESTART;
@@ -369,9 +361,11 @@ main(int argc, char *argv[])
     for (;;) {
         alarm(1);
         memset(&rmsg, '\0', sizeof(rmsg));
-        DEBUG(2)
-        fprintf(stderr, "msgrcv: %ld, %p, %u, %ld, %d \n",
-                rmsgid, &rmsg, diomsg::msg_snd_rcv_sz, 0, 0);
+        DEBUG(2) {
+            std::cerr << "msgrcv: " << rmsgid << ", "
+                      << &rmsg << ", " << diomsg::msg_snd_rcv_sz
+                      << ", " << 0 << ", " << 0 << std::endl;
+        }
         rlen = msgrcv(rmsgid, &rmsg, diomsg::msg_snd_rcv_sz, 0, 0);
 
         if (rlen < 0) {
@@ -403,8 +397,9 @@ main(int argc, char *argv[])
         }
     }
 
-    DEBUG(2)
-    fprintf(stderr, "%d diskd exiting\n", (int) mypid);
+    DEBUG(2) {
+        fprintf(stderr, "%d diskd exiting\n", (int) mypid);
+    }
 
     if (msgctl(rmsgid, IPC_RMID, 0) < 0)
         perror("msgctl IPC_RMID");
@@ -420,3 +415,4 @@ main(int argc, char *argv[])
 
     return 0;
 }
+