]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/comm/Read.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / comm / Read.cc
index 2587d80a899db8bf35f484a8f651deae18c68076..9946efb2676051a5b1452e9b21af5f9d5997f759 100644 (file)
@@ -1,6 +1,13 @@
 /*
- * DEBUG: section 05    Socket Functions
+ * Copyright (C) 1996-2017 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 05    Socket Functions */
+
 #include "squid.h"
 #include "comm.h"
 #include "comm/IoCallback.h"
@@ -11,9 +18,8 @@
 #include "Debug.h"
 #include "fd.h"
 #include "fde.h"
-#include "SBuf.h"
+#include "sbuf/SBuf.h"
 #include "StatCounters.h"
-//#include "tools.h"
 
 // Does comm check this fd for read readiness?
 // Note that when comm is not monitoring, there can be a pending callback
@@ -21,7 +27,7 @@
 bool
 Comm::MonitorsRead(int fd)
 {
-    assert(isOpen(fd) && COMMIO_FD_READCB(fd));
+    assert(isOpen(fd) && COMMIO_FD_READCB(fd) != NULL);
     // Being active is usually the same as monitoring because we always
     // start monitoring the FD when we configure Comm::IoCallback for I/O
     // and we usually configure Comm::IoCallback for I/O when we starting
@@ -75,7 +81,9 @@ Comm::ReadNow(CommIoCbParams &params, SBuf &buf)
 {
     /* Attempt a read */
     ++ statCounter.syscalls.sock.reads;
-    const SBuf::size_type sz = buf.spaceSize();
+    SBuf::size_type sz = buf.spaceSize();
+    if (params.size > 0 && params.size < sz)
+        sz = params.size;
     char *inbuf = buf.rawSpace(sz);
     errno = 0;
     const int retval = FD_READ_METHOD(params.conn->fd, inbuf, sz);
@@ -131,22 +139,22 @@ Comm::HandleRead(int fd, void *data)
     /* For legacy callers : Attempt a read */
     // Keep in sync with Comm::ReadNow()!
     ++ statCounter.syscalls.sock.reads;
-    errno = 0;
+    int xerrno = errno = 0;
     int retval = FD_READ_METHOD(fd, ccb->buf, ccb->size);
-    debugs(5, 3, "FD " << fd << ", size " << ccb->size << ", retval " << retval << ", errno " << errno);
+    xerrno = errno;
+    debugs(5, 3, "FD " << fd << ", size " << ccb->size << ", retval " << retval << ", errno " << xerrno);
 
     /* See if we read anything */
     /* Note - read 0 == socket EOF, which is a valid read */
     if (retval >= 0) {
         fd_bytes(fd, retval, FD_READ);
         ccb->offset = retval;
-        ccb->finish(Comm::OK, errno);
+        ccb->finish(Comm::OK, 0);
         return;
-
-    } else if (retval < 0 && !ignoreErrno(errno)) {
+    } else if (retval < 0 && !ignoreErrno(xerrno)) {
         debugs(5, 3, "comm_read_try: scheduling Comm::COMM_ERROR");
         ccb->offset = 0;
-        ccb->finish(Comm::COMM_ERROR, errno);
+        ccb->finish(Comm::COMM_ERROR, xerrno);
         return;
     };
 
@@ -232,3 +240,4 @@ Comm::ReadCancel(int fd, AsyncCall::Pointer &callback)
     /* And the IO event */
     Comm::SetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
 }
+