[Libguestfs] [PATCH] daemon: initrd: print return value from failing process

Pino Toscano ptoscano at redhat.com
Tue Sep 15 08:20:11 UTC 2015


If either zcat or cpio fails when spawned in initrd-list, pclose will
return the actual return value of it, but reply_with_perror still uses
errno regardless; thus, the reported error is:

  libguestfs: error: initrd_list: pclose: Success

which is not much helpful.

Instead, when pclose returns > 0, extract the actual return value of the
subprocess, and print that. Thus now we get for example:

  libguestfs: error: initrd_list: pclose: command failed with return code 1
---
 daemon/initrd.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/daemon/initrd.c b/daemon/initrd.c
index ac14826..c9fc2dd 100644
--- a/daemon/initrd.c
+++ b/daemon/initrd.c
@@ -43,6 +43,7 @@ do_initrd_list (const char *path)
   CLEANUP_FREE char *filename = NULL;
   size_t allocsize;
   ssize_t len;
+  int ret;
 
   /* "zcat /sysroot/<path> | cpio --quiet -it", but path must be quoted. */
   if (asprintf_nowarn (&cmd, "%s %R | %s --quiet -it", str_zcat, path, str_cpio) == -1) {
@@ -74,8 +75,15 @@ do_initrd_list (const char *path)
     return NULL;
   }
 
-  if (pclose (fp) != 0) {
-    reply_with_perror ("pclose");
+  ret = pclose (fp);
+  if (ret != 0) {
+    if (ret == -1)
+      reply_with_perror ("pclose");
+    else {
+      if (WEXITSTATUS (ret) != 0)
+        ret = WEXITSTATUS (ret);
+      reply_with_error ("pclose: command failed with return code %d", ret);
+    }
     free_stringslen (filenames.argv, filenames.size);
     return NULL;
   }
-- 
2.1.0




More information about the Libguestfs mailing list