[PATCH] lib: Use virReportSystemError() more

Michal Privoznik posted 1 patch 2 years ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/e93676592f568c1702f2417ea7606a576ec3433f.1649083930.git.mprivozn@redhat.com
Test syntax-check passed
src/libxl/libxl_conf.c    |  7 +++----
src/libxl/libxl_driver.c  | 35 +++++++++++++++--------------------
src/qemu/qemu_interface.c |  8 ++++----
3 files changed, 22 insertions(+), 28 deletions(-)
[PATCH] lib: Use virReportSystemError() more
Posted by Michal Privoznik 2 years ago
Instead of reporting virReportError(..., g_strerror(), ...) let's
use proper virReportSystemError(). Generated with help of cocci:

  @@
  expression c;
  @@
      <...
  -   virReportError(c,
  +   virReportSystemError(errno,
                         ...,
  -                      g_strerror(errno),
                         ...);
      ...>

But then I had to hand fix format strings, because I'm not sure
if cocci even knows how to do that. And even if it did, I surely
don't.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/libxl/libxl_conf.c    |  7 +++----
 src/libxl/libxl_driver.c  | 35 +++++++++++++++--------------------
 src/qemu/qemu_interface.c |  8 ++++----
 3 files changed, 22 insertions(+), 28 deletions(-)

diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c
index a0fc51c74a..6398129195 100644
--- a/src/libxl/libxl_conf.c
+++ b/src/libxl/libxl_conf.c
@@ -1775,10 +1775,9 @@ libxlDriverConfigInit(libxlDriverConfig *cfg)
     uint64_t free_mem;
 
     if (g_mkdir_with_parents(cfg->logDir, 0777) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("failed to create log dir '%s': %s"),
-                       cfg->logDir,
-                       g_strerror(errno));
+        virReportSystemError(errno,
+                             _("failed to create log dir '%s'"),
+                             cfg->logDir);
         return -1;
     }
 
diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c
index 01f281d0a5..5d76eb9752 100644
--- a/src/libxl/libxl_driver.c
+++ b/src/libxl/libxl_driver.c
@@ -721,38 +721,33 @@ libxlStateInitialize(bool privileged,
 
     libxl_driver->config = cfg;
     if (g_mkdir_with_parents(cfg->stateDir, 0777) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("failed to create state dir '%s': %s"),
-                       cfg->stateDir,
-                       g_strerror(errno));
+        virReportSystemError(errno,
+                             _("failed to create state dir '%s'"),
+                             cfg->stateDir);
         goto error;
     }
     if (g_mkdir_with_parents(cfg->libDir, 0777) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("failed to create lib dir '%s': %s"),
-                       cfg->libDir,
-                       g_strerror(errno));
+        virReportSystemError(errno,
+                             _("failed to create lib dir '%s'"),
+                             cfg->libDir);
         goto error;
     }
     if (g_mkdir_with_parents(cfg->saveDir, 0777) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("failed to create save dir '%s': %s"),
-                       cfg->saveDir,
-                       g_strerror(errno));
+        virReportSystemError(errno,
+                             _("failed to create save dir '%s'"),
+                             cfg->saveDir);
         goto error;
     }
     if (g_mkdir_with_parents(cfg->autoDumpDir, 0777) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("failed to create dump dir '%s': %s"),
-                       cfg->autoDumpDir,
-                       g_strerror(errno));
+        virReportSystemError(errno,
+                             _("failed to create dump dir '%s'"),
+                             cfg->autoDumpDir);
         goto error;
     }
     if (g_mkdir_with_parents(cfg->channelDir, 0777) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("failed to create channel dir '%s': %s"),
-                       cfg->channelDir,
-                       g_strerror(errno));
+        virReportSystemError(errno,
+                             _("failed to create channel dir '%s'"),
+                             cfg->channelDir);
         goto error;
     }
 
diff --git a/src/qemu/qemu_interface.c b/src/qemu/qemu_interface.c
index 676bc896d6..d0dcce5690 100644
--- a/src/qemu/qemu_interface.c
+++ b/src/qemu/qemu_interface.c
@@ -370,10 +370,10 @@ qemuCreateInBridgePortWithHelper(virQEMUDriverConfig *cfg,
         if (errbuf && *errbuf)
             errstr = g_strdup_printf("\nstderr=%s", errbuf);
 
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-            _("%s: failed to communicate with bridge helper: %s%s"),
-            cmdstr, g_strerror(errno),
-            NULLSTR_EMPTY(errstr));
+        virReportSystemError(errno,
+                             _("%s: failed to communicate with bridge helper: %s"),
+                             cmdstr,
+                             NULLSTR_EMPTY(errstr));
         VIR_FREE(errstr);
         goto cleanup;
     }
-- 
2.35.1
Re: [PATCH] lib: Use virReportSystemError() more
Posted by Ján Tomko 2 years ago
On a Monday in 2022, Michal Privoznik wrote:
>Instead of reporting virReportError(..., g_strerror(), ...) let's
>use proper virReportSystemError(). Generated with help of cocci:
>
>  @@
>  expression c;
>  @@
>      <...
>  -   virReportError(c,
>  +   virReportSystemError(errno,
>                         ...,
>  -                      g_strerror(errno),
>                         ...);
>      ...>
>
>But then I had to hand fix format strings, because I'm not sure
>if cocci even knows how to do that. And even if it did, I surely
>don't.
>
>Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
>---
> src/libxl/libxl_conf.c    |  7 +++----
> src/libxl/libxl_driver.c  | 35 +++++++++++++++--------------------
> src/qemu/qemu_interface.c |  8 ++++----
> 3 files changed, 22 insertions(+), 28 deletions(-)
>
>diff --git a/src/qemu/qemu_interface.c b/src/qemu/qemu_interface.c
>index 676bc896d6..d0dcce5690 100644
>--- a/src/qemu/qemu_interface.c
>+++ b/src/qemu/qemu_interface.c
>@@ -370,10 +370,10 @@ qemuCreateInBridgePortWithHelper(virQEMUDriverConfig *cfg,
>         if (errbuf && *errbuf)
>             errstr = g_strdup_printf("\nstderr=%s", errbuf);
>
>-        virReportError(VIR_ERR_INTERNAL_ERROR,
>-            _("%s: failed to communicate with bridge helper: %s%s"),
>-            cmdstr, g_strerror(errno),
>-            NULLSTR_EMPTY(errstr));
>+        virReportSystemError(errno,
>+                             _("%s: failed to communicate with bridge helper: %s"),
>+                             cmdstr,
>+                             NULLSTR_EMPTY(errstr));

So, before we had:
helper: <errno>\n<errbuf>
now we'll get:
helper: \n<errbuf>: <errno>

I'd rather leave the code as-is, or at least remove the newline from the
g_strdup_printf call above.

Either way:

Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano

>         VIR_FREE(errstr);
>         goto cleanup;
>     }
>-- 
>2.35.1
>