[PATCH v2 05/17] virfdstream: Use VIR_AUTOCLOSE()

Michal Privoznik posted 17 patches 5 years, 7 months ago
There is a newer version of this series
[PATCH v2 05/17] virfdstream: Use VIR_AUTOCLOSE()
Posted by Michal Privoznik 5 years, 7 months ago
Again, instead of closing FDs explicitly, we can automatically
close them when they go out of their respective scopes.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/util/virfdstream.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/util/virfdstream.c b/src/util/virfdstream.c
index c85dee05c3..bac1c95c0a 100644
--- a/src/util/virfdstream.c
+++ b/src/util/virfdstream.c
@@ -571,9 +571,9 @@ virFDStreamThread(void *opaque)
     virStreamPtr st = data->st;
     size_t length = data->length;
     bool sparse = data->sparse;
-    int fdin = data->fdin;
+    VIR_AUTOCLOSE fdin = data->fdin;
     char *fdinname = data->fdinname;
-    int fdout = data->fdout;
+    VIR_AUTOCLOSE fdout = data->fdout;
     char *fdoutname = data->fdoutname;
     virFDStreamDataPtr fdst = st->privateData;
     bool doRead = fdst->threadDoRead;
@@ -633,8 +633,6 @@ virFDStreamThread(void *opaque)
     virObjectUnref(fdst);
     if (virFDStreamDataDisposed)
         st->privateData = NULL;
-    VIR_FORCE_CLOSE(fdin);
-    VIR_FORCE_CLOSE(fdout);
     virFDStreamThreadDataFree(data);
     return;
 
@@ -1160,9 +1158,10 @@ int virFDStreamConnectUNIX(virStreamPtr st,
 {
     struct sockaddr_un sa;
     virTimeBackOffVar timeout;
+    VIR_AUTOCLOSE fd = -1;
     int ret;
 
-    int fd = socket(AF_UNIX, SOCK_STREAM, 0);
+    fd = socket(AF_UNIX, SOCK_STREAM, 0);
     if (fd < 0) {
         virReportSystemError(errno, "%s", _("Unable to open UNIX socket"));
         goto error;
@@ -1197,10 +1196,11 @@ int virFDStreamConnectUNIX(virStreamPtr st,
 
     if (virFDStreamOpenInternal(st, fd, NULL, 0) < 0)
         goto error;
+
+    fd = -1;
     return 0;
 
  error:
-    VIR_FORCE_CLOSE(fd);
     return -1;
 }
 
-- 
2.26.2

Re: [PATCH v2 05/17] virfdstream: Use VIR_AUTOCLOSE()
Posted by Peter Krempa 5 years, 5 months ago
On Tue, Jul 07, 2020 at 21:46:23 +0200, Michal Privoznik wrote:
> Again, instead of closing FDs explicitly, we can automatically
> close them when they go out of their respective scopes.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/util/virfdstream.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)

[...]

> @@ -1160,9 +1158,10 @@ int virFDStreamConnectUNIX(virStreamPtr st,
>  {
>      struct sockaddr_un sa;
>      virTimeBackOffVar timeout;
> +    VIR_AUTOCLOSE fd = -1;
>      int ret;
>  
> -    int fd = socket(AF_UNIX, SOCK_STREAM, 0);
> +    fd = socket(AF_UNIX, SOCK_STREAM, 0);
>      if (fd < 0) {
>          virReportSystemError(errno, "%s", _("Unable to open UNIX socket"));
>          goto error;
> @@ -1197,10 +1196,11 @@ int virFDStreamConnectUNIX(virStreamPtr st,
>  
>      if (virFDStreamOpenInternal(st, fd, NULL, 0) < 0)
>          goto error;
> +
> +    fd = -1;
>      return 0;

Please move the clearing of 'fd' closer towards the call to
'virFDStreamOpenInternal' which consumes it than to the return statement
so that it's visualy clearer that it's consumed by the call.


Reviewed-by: Peter Krempa <pkrempa@redhat.com>