[libvirt] [PATCH 1/3] rpc: socket: Add possibility to suppress errors on read hangup

Peter Krempa posted 3 patches 8 years, 10 months ago
[libvirt] [PATCH 1/3] rpc: socket: Add possibility to suppress errors on read hangup
Posted by Peter Krempa 8 years, 10 months ago
In some cases a read error due to connection hangup is expected. This
patch adds a flag that removes the logging of a virError in such case.
---
 src/rpc/virnetsocket.c | 33 +++++++++++++++++++++++++++------
 src/rpc/virnetsocket.h |  2 ++
 2 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c
index 325a7c7cf..4d1dc6446 100644
--- a/src/rpc/virnetsocket.c
+++ b/src/rpc/virnetsocket.c
@@ -82,6 +82,7 @@ struct _virNetSocket {
     int errfd;
     bool client;
     bool ownsFd;
+    bool quietEOF;

     /* Event callback fields */
     virNetSocketIOFunc func;
@@ -1792,12 +1793,18 @@ static ssize_t virNetSocketReadWire(virNetSocketPtr sock, char *buf, size_t len)
                                  _("Cannot recv data"));
         ret = -1;
     } else if (ret == 0) {
-        if (errout)
-            virReportSystemError(EIO,
-                                 _("End of file while reading data: %s"), errout);
-        else
-            virReportSystemError(EIO, "%s",
-                                 _("End of file while reading data"));
+        if (sock->quietEOF) {
+            VIR_DEBUG("socket='%p' EOF while reading: errout='%s'",
+                      socket, NULLSTR(errout));
+        } else {
+            if (errout)
+                virReportSystemError(EIO,
+                                     _("End of file while reading data: %s"),
+                                     errout);
+            else
+                virReportSystemError(EIO, "%s",
+                                     _("End of file while reading data"));
+        }
         ret = -1;
     }

@@ -2233,3 +2240,17 @@ void virNetSocketClose(virNetSocketPtr sock)

     virObjectUnlock(sock);
 }
+
+
+/**
+ * virNetSocketSetQuietEOF:
+ * @sock: socket object pointer
+ *
+ * Disables reporting I/O errors as a virError when @socket is closed while
+ * reading data.
+ */
+void
+virNetSocketSetQuietEOF(virNetSocketPtr sock)
+{
+    sock->quietEOF = true;
+}
diff --git a/src/rpc/virnetsocket.h b/src/rpc/virnetsocket.h
index 56c75c030..1e75ee62b 100644
--- a/src/rpc/virnetsocket.h
+++ b/src/rpc/virnetsocket.h
@@ -143,6 +143,8 @@ int virNetSocketGetSELinuxContext(virNetSocketPtr sock,
 int virNetSocketSetBlocking(virNetSocketPtr sock,
                             bool blocking);

+void virNetSocketSetQuietEOF(virNetSocketPtr sock);
+
 ssize_t virNetSocketRead(virNetSocketPtr sock, char *buf, size_t len);
 ssize_t virNetSocketWrite(virNetSocketPtr sock, const char *buf, size_t len);

-- 
2.12.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 1/3] rpc: socket: Add possibility to suppress errors on read hangup
Posted by Daniel P. Berrange 8 years, 10 months ago
On Fri, Mar 17, 2017 at 04:48:49PM +0100, Peter Krempa wrote:
> In some cases a read error due to connection hangup is expected. This
> patch adds a flag that removes the logging of a virError in such case.
> ---
>  src/rpc/virnetsocket.c | 33 +++++++++++++++++++++++++++------
>  src/rpc/virnetsocket.h |  2 ++
>  2 files changed, 29 insertions(+), 6 deletions(-)
> 
> diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c
> index 325a7c7cf..4d1dc6446 100644
> --- a/src/rpc/virnetsocket.c
> +++ b/src/rpc/virnetsocket.c
> @@ -82,6 +82,7 @@ struct _virNetSocket {
>      int errfd;
>      bool client;
>      bool ownsFd;
> +    bool quietEOF;
> 
>      /* Event callback fields */
>      virNetSocketIOFunc func;
> @@ -1792,12 +1793,18 @@ static ssize_t virNetSocketReadWire(virNetSocketPtr sock, char *buf, size_t len)
>                                   _("Cannot recv data"));
>          ret = -1;
>      } else if (ret == 0) {
> -        if (errout)
> -            virReportSystemError(EIO,
> -                                 _("End of file while reading data: %s"), errout);
> -        else
> -            virReportSystemError(EIO, "%s",
> -                                 _("End of file while reading data"));
> +        if (sock->quietEOF) {
> +            VIR_DEBUG("socket='%p' EOF while reading: errout='%s'",
> +                      socket, NULLSTR(errout));
> +        } else {
> +            if (errout)
> +                virReportSystemError(EIO,
> +                                     _("End of file while reading data: %s"),
> +                                     errout);
> +            else
> +                virReportSystemError(EIO, "%s",
> +                                     _("End of file while reading data"));
> +        }
>          ret = -1;

I'm a little uncomfortable with the idea of returning '-1' without reporting
an error message. I would suggest returning 0, but we used that to indicate
EAGAIN condition. Can we at least make it return '-2' as a distinct code
when we don't report errors.


Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://entangle-photo.org       -o-    http://search.cpan.org/~danberr/ :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 1/3] rpc: socket: Add possibility to suppress errors on read hangup
Posted by Peter Krempa 8 years, 10 months ago
On Fri, Mar 17, 2017 at 16:10:35 +0000, Daniel Berrange wrote:
> On Fri, Mar 17, 2017 at 04:48:49PM +0100, Peter Krempa wrote:
> > In some cases a read error due to connection hangup is expected. This
> > patch adds a flag that removes the logging of a virError in such case.
> > ---
> >  src/rpc/virnetsocket.c | 33 +++++++++++++++++++++++++++------
> >  src/rpc/virnetsocket.h |  2 ++
> >  2 files changed, 29 insertions(+), 6 deletions(-)

[...]

> > +        } else {
> > +            if (errout)
> > +                virReportSystemError(EIO,
> > +                                     _("End of file while reading data: %s"),
> > +                                     errout);
> > +            else
> > +                virReportSystemError(EIO, "%s",
> > +                                     _("End of file while reading data"));
> > +        }
> >          ret = -1;
> 
> I'm a little uncomfortable with the idea of returning '-1' without reporting
> an error message. I would suggest returning 0, but we used that to indicate
> EAGAIN condition. Can we at least make it return '-2' as a distinct code
> when we don't report errors.

That is fine by me. The callers that will ignore the error don't really
care why the read failed currently.
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list