Blame SOURCES/0131-p2v-ssh-Improve-consistency-of-error-messages.patch

e76f14
From af87d247df5ebe6b94e04624c35b59155ae99eb5 Mon Sep 17 00:00:00 2001
e76f14
From: "Richard W.M. Jones" <rjones@redhat.com>
e76f14
Date: Thu, 23 Jun 2016 14:25:23 +0100
e76f14
Subject: [PATCH] p2v: ssh: Improve consistency of error messages.
e76f14
e76f14
(cherry picked from commit 8f8a651e591064d87974bd322a995519347ebae7)
e76f14
---
e76f14
 p2v/ssh.c | 133 +++++++++++++++++++++++++++++++++++---------------------------
e76f14
 1 file changed, 75 insertions(+), 58 deletions(-)
e76f14
e76f14
diff --git a/p2v/ssh.c b/p2v/ssh.c
e76f14
index 083e04e..93c4c55 100644
e76f14
--- a/p2v/ssh.c
e76f14
+++ b/p2v/ssh.c
e76f14
@@ -93,6 +93,21 @@ get_ssh_error (void)
e76f14
   return ssh_error;
e76f14
 }
e76f14
 
e76f14
+/* Like set_ssh_error, but for errors that aren't supposed to happen. */
e76f14
+#define set_ssh_internal_error(fs, ...) \
e76f14
+  set_ssh_error ("internal error: " fs, ##__VA_ARGS__)
e76f14
+#define set_ssh_mexp_error(fn) \
e76f14
+  set_ssh_internal_error ("%s: %m", fn)
e76f14
+#define set_ssh_pcre_error() \
e76f14
+  set_ssh_internal_error ("pcre error: %d\n", mexp_get_pcre_error (h))
e76f14
+
e76f14
+#define set_ssh_unexpected_eof(fs, ...)                               \
e76f14
+  set_ssh_error ("remote server closed the connection unexpectedly, " \
e76f14
+                 "waiting for: " fs, ##__VA_ARGS__)
e76f14
+#define set_ssh_unexpected_timeout(fs, ...)               \
e76f14
+  set_ssh_error ("remote server timed out unexpectedly, " \
e76f14
+                 "waiting for: " fs, ##__VA_ARGS__)
e76f14
+
e76f14
 static void compile_regexps (void) __attribute__((constructor));
e76f14
 static void free_regexps (void) __attribute__((destructor));
e76f14
 
e76f14
@@ -241,7 +256,7 @@ curl_download (const char *url, const char *local_file)
e76f14
     return -1;
e76f14
   }
e76f14
   else if (!WIFEXITED (r)) {
e76f14
-    set_ssh_error ("curl subprocess got a signal (%d)", r);
e76f14
+    set_ssh_internal_error ("curl subprocess got a signal (%d)", r);
e76f14
     unlink (error_file);
e76f14
     return -1;
e76f14
   }
e76f14
@@ -353,7 +368,7 @@ start_ssh (struct config *config, char **extra_args, int wait_prompt)
e76f14
 
e76f14
   h = mexp_spawnv ("ssh", (char **) args);
e76f14
   if (h == NULL) {
e76f14
-    set_ssh_error ("internal error: ssh: mexp_spawnv: %m");
e76f14
+    set_ssh_internal_error ("ssh: mexp_spawnv: %m");
e76f14
     return NULL;
e76f14
   }
e76f14
 
e76f14
@@ -371,7 +386,7 @@ start_ssh (struct config *config, char **extra_args, int wait_prompt)
e76f14
                          }, ovector, ovecsize)) {
e76f14
     case 100:                   /* Got password prompt. */
e76f14
       if (mexp_printf (h, "%s\n", config->password) == -1) {
e76f14
-        set_ssh_error ("mexp_printf: %m");
e76f14
+        set_ssh_mexp_error ("mexp_printf");
e76f14
         mexp_close (h);
e76f14
         return NULL;
e76f14
       }
e76f14
@@ -383,7 +398,6 @@ start_ssh (struct config *config, char **extra_args, int wait_prompt)
e76f14
       goto wait_password_again;
e76f14
 
e76f14
     case MEXP_EOF:
e76f14
-      mexp_close (h);
e76f14
       /* This is where we get to if the user enters an incorrect or
e76f14
        * impossible hostname or port number.  Hopefully ssh printed an
e76f14
        * error message, and we picked it up and put it in
e76f14
@@ -393,21 +407,22 @@ start_ssh (struct config *config, char **extra_args, int wait_prompt)
e76f14
       if (ssh_message)
e76f14
         set_ssh_error ("%s", ssh_message);
e76f14
       else
e76f14
-        set_ssh_error ("unknown ssh error");
e76f14
+        set_ssh_error ("ssh closed the connection without printing an error.");
e76f14
+      mexp_close (h);
e76f14
       return NULL;
e76f14
 
e76f14
     case MEXP_TIMEOUT:
e76f14
+      set_ssh_unexpected_timeout ("password prompt");
e76f14
       mexp_close (h);
e76f14
-      set_ssh_error ("timeout waiting for password prompt");
e76f14
       return NULL;
e76f14
 
e76f14
     case MEXP_ERROR:
e76f14
-      set_ssh_error ("mexp_expect: %m");
e76f14
+      set_ssh_mexp_error ("mexp_expect");
e76f14
       mexp_close (h);
e76f14
       return NULL;
e76f14
 
e76f14
     case MEXP_PCRE_ERROR:
e76f14
-      set_ssh_error ("PCRE error: %d\n", mexp_get_pcre_error (h));
e76f14
+      set_ssh_pcre_error ();
e76f14
       mexp_close (h);
e76f14
       return NULL;
e76f14
     }
e76f14
@@ -435,7 +450,7 @@ start_ssh (struct config *config, char **extra_args, int wait_prompt)
e76f14
    * (https://bugzilla.redhat.com/1314244#c9).
e76f14
    */
e76f14
   if (mexp_printf (h, "exec bash --noediting --noprofile\n") == -1) {
e76f14
-    set_ssh_error ("setting bash as remote shell: %m");
e76f14
+    set_ssh_mexp_error ("mexp_printf");
e76f14
     mexp_close (h);
e76f14
     return NULL;
e76f14
   }
e76f14
@@ -449,7 +464,7 @@ start_ssh (struct config *config, char **extra_args, int wait_prompt)
e76f14
     int r;
e76f14
 
e76f14
     if (guestfs_int_random_string (magic, 8) == -1) {
e76f14
-      set_ssh_error ("random_string: %m");
e76f14
+      set_ssh_internal_error ("random_string: %m");
e76f14
       mexp_close (h);
e76f14
       return NULL;
e76f14
     }
e76f14
@@ -458,7 +473,7 @@ start_ssh (struct config *config, char **extra_args, int wait_prompt)
e76f14
      * mistake the command echo for the prompt.
e76f14
      */
e76f14
     if (mexp_printf (h, "export LANG=C PS1='###''%s''### '\n", magic) == -1) {
e76f14
-      set_ssh_error ("random_string: %m");
e76f14
+      set_ssh_mexp_error ("mexp_printf");
e76f14
       mexp_close (h);
e76f14
       return NULL;
e76f14
     }
e76f14
@@ -472,8 +487,8 @@ start_ssh (struct config *config, char **extra_args, int wait_prompt)
e76f14
                            { 0 }
e76f14
                          }, ovector, ovecsize)) {
e76f14
     case 100:                    /* Got password prompt unexpectedly. */
e76f14
+      set_ssh_error ("Login failed.  Probably the username and/or password is wrong.");
e76f14
       mexp_close (h);
e76f14
-      set_ssh_error ("login failed - probably the username and/or password is wrong");
e76f14
       return NULL;
e76f14
 
e76f14
     case 101:
e76f14
@@ -483,7 +498,7 @@ start_ssh (struct config *config, char **extra_args, int wait_prompt)
e76f14
       r = pcre_get_substring (h->buffer, ovector,
e76f14
                               mexp_get_pcre_error (h), 1, &matched);
e76f14
       if (r < 0) {
e76f14
-        fprintf (stderr, "error: PCRE error reading substring (%d)\n", r);
e76f14
+        fprintf (stderr, "error: pcre error reading substring (%d)\n", r);
e76f14
         exit (EXIT_FAILURE);
e76f14
       }
e76f14
       r = STREQ (magic, matched);
e76f14
@@ -493,8 +508,8 @@ start_ssh (struct config *config, char **extra_args, int wait_prompt)
e76f14
       goto got_prompt;
e76f14
 
e76f14
     case MEXP_EOF:
e76f14
+      set_ssh_unexpected_eof ("the command prompt");
e76f14
       mexp_close (h);
e76f14
-      set_ssh_error ("unexpected end of file waiting for command prompt");
e76f14
       return NULL;
e76f14
 
e76f14
     case MEXP_TIMEOUT:
e76f14
@@ -504,19 +519,19 @@ start_ssh (struct config *config, char **extra_args, int wait_prompt)
e76f14
       break;
e76f14
 
e76f14
     case MEXP_ERROR:
e76f14
-      set_ssh_error ("mexp_expect: %m");
e76f14
+      set_ssh_mexp_error ("mexp_expect");
e76f14
       mexp_close (h);
e76f14
       return NULL;
e76f14
 
e76f14
     case MEXP_PCRE_ERROR:
e76f14
-      set_ssh_error ("PCRE error: %d\n", mexp_get_pcre_error (h));
e76f14
+      set_ssh_pcre_error ();
e76f14
       mexp_close (h);
e76f14
       return NULL;
e76f14
     }
e76f14
   }
e76f14
 
e76f14
+  set_ssh_error ("Failed to synchronize with remote shell after 60 seconds.");
e76f14
   mexp_close (h);
e76f14
-  set_ssh_error ("failed to synchronize with remote shell after 60 seconds");
e76f14
   return NULL;
e76f14
 
e76f14
  got_prompt:
e76f14
@@ -556,7 +571,7 @@ test_connection (struct config *config)
e76f14
   if (mexp_printf (h,
e76f14
                    "%svirt-v2v --version\n",
e76f14
                    config->sudo ? "sudo -n " : "") == -1) {
e76f14
-    set_ssh_error ("mexp_printf: %m");
e76f14
+    set_ssh_mexp_error ("mexp_printf");
e76f14
     mexp_close (h);
e76f14
     return -1;
e76f14
   }
e76f14
@@ -582,28 +597,28 @@ test_connection (struct config *config)
e76f14
       goto end_of_version;
e76f14
 
e76f14
     case 102:
e76f14
-      mexp_close (h);
e76f14
       set_ssh_error ("sudo for user \"%s\" requires a password.  Edit /etc/sudoers on the conversion server to ensure the \"NOPASSWD:\" option is set for this user.",
e76f14
                      config->username);
e76f14
+      mexp_close (h);
e76f14
       return -1;
e76f14
 
e76f14
     case MEXP_EOF:
e76f14
+      set_ssh_unexpected_eof ("\"virt-v2v --version\" output");
e76f14
       mexp_close (h);
e76f14
-      set_ssh_error ("unexpected end of file waiting virt-v2v --version output");
e76f14
       return -1;
e76f14
 
e76f14
     case MEXP_TIMEOUT:
e76f14
+      set_ssh_unexpected_timeout ("\"virt-v2v --version\" output");
e76f14
       mexp_close (h);
e76f14
-      set_ssh_error ("timeout waiting for virt-v2v --version output");
e76f14
       return -1;
e76f14
 
e76f14
     case MEXP_ERROR:
e76f14
-      set_ssh_error ("mexp_expect: %m");
e76f14
+      set_ssh_mexp_error ("mexp_expect");
e76f14
       mexp_close (h);
e76f14
       return -1;
e76f14
 
e76f14
     case MEXP_PCRE_ERROR:
e76f14
-      set_ssh_error ("PCRE error: %d\n", mexp_get_pcre_error (h));
e76f14
+      set_ssh_pcre_error ();
e76f14
       mexp_close (h);
e76f14
       return -1;
e76f14
     }
e76f14
@@ -612,9 +627,9 @@ test_connection (struct config *config)
e76f14
 
e76f14
   /* Got the prompt but no version number. */
e76f14
   if (v2v_version == NULL) {
e76f14
-    mexp_close (h);
e76f14
     set_ssh_error ("virt-v2v is not installed on the conversion server, "
e76f14
-                   "or it might be a too old version");
e76f14
+                   "or it might be a too old version.");
e76f14
+    mexp_close (h);
e76f14
     return -1;
e76f14
   }
e76f14
 
e76f14
@@ -634,7 +649,7 @@ test_connection (struct config *config)
e76f14
   /* Get virt-v2v features.  See: v2v/cmdline.ml */
e76f14
   if (mexp_printf (h, "%svirt-v2v --machine-readable\n",
e76f14
                    config->sudo ? "sudo -n " : "") == -1) {
e76f14
-    set_ssh_error ("mexp_printf: %m");
e76f14
+    set_ssh_mexp_error ("mexp_printf");
e76f14
     mexp_close (h);
e76f14
     return -1;
e76f14
   }
e76f14
@@ -677,22 +692,22 @@ test_connection (struct config *config)
e76f14
       goto end_of_machine_readable;
e76f14
 
e76f14
     case MEXP_EOF:
e76f14
+      set_ssh_unexpected_eof ("\"virt-v2v --machine-readable\" output");
e76f14
       mexp_close (h);
e76f14
-      set_ssh_error ("unexpected end of file waiting virt-v2v --machine-readable output");
e76f14
       return -1;
e76f14
 
e76f14
     case MEXP_TIMEOUT:
e76f14
+      set_ssh_unexpected_timeout ("\"virt-v2v --machine-readable\" output");
e76f14
       mexp_close (h);
e76f14
-      set_ssh_error ("timeout waiting virt-v2v --machine-readable output");
e76f14
       return -1;
e76f14
 
e76f14
     case MEXP_ERROR:
e76f14
-      set_ssh_error ("mexp_expect: %m");
e76f14
+      set_ssh_mexp_error ("mexp_expect");
e76f14
       mexp_close (h);
e76f14
       return -1;
e76f14
 
e76f14
     case MEXP_PCRE_ERROR:
e76f14
-      set_ssh_error ("PCRE error: %d\n", mexp_get_pcre_error (h));
e76f14
+      set_ssh_pcre_error ();
e76f14
       mexp_close (h);
e76f14
       return -1;
e76f14
     }
e76f14
@@ -700,14 +715,14 @@ test_connection (struct config *config)
e76f14
  end_of_machine_readable:
e76f14
 
e76f14
   if (!feature_libguestfs_rewrite) {
e76f14
+    set_ssh_error ("Invalid output of \"virt-v2v --machine-readable\" command.");
e76f14
     mexp_close (h);
e76f14
-    set_ssh_error ("invalid output of virt-v2v --machine-readable command");
e76f14
     return -1;
e76f14
   }
e76f14
 
e76f14
   /* Test finished, shut down ssh. */
e76f14
   if (mexp_printf (h, "exit\n") == -1) {
e76f14
-    set_ssh_error ("mexp_printf: %m");
e76f14
+    set_ssh_mexp_error ("mexp_printf");
e76f14
     mexp_close (h);
e76f14
     return -1;
e76f14
   }
e76f14
@@ -717,30 +732,31 @@ test_connection (struct config *config)
e76f14
     break;
e76f14
 
e76f14
   case MEXP_TIMEOUT:
e76f14
+    set_ssh_unexpected_timeout ("end of ssh session");
e76f14
     mexp_close (h);
e76f14
-    set_ssh_error ("timeout waiting for end of ssh session");
e76f14
     return -1;
e76f14
 
e76f14
   case MEXP_ERROR:
e76f14
-    set_ssh_error ("mexp_expect: %m");
e76f14
+    set_ssh_mexp_error ("mexp_expect");
e76f14
     mexp_close (h);
e76f14
     return -1;
e76f14
 
e76f14
   case MEXP_PCRE_ERROR:
e76f14
-    set_ssh_error ("PCRE error: %d\n", mexp_get_pcre_error (h));
e76f14
+    set_ssh_pcre_error ();
e76f14
     mexp_close (h);
e76f14
     return -1;
e76f14
   }
e76f14
 
e76f14
   status = mexp_close (h);
e76f14
   if (status == -1) {
e76f14
-    set_ssh_error ("mexp_close: %m");
e76f14
+    set_ssh_internal_error ("mexp_close: %m");
e76f14
     return -1;
e76f14
   }
e76f14
   if (WIFSIGNALED (status) && WTERMSIG (status) == SIGHUP)
e76f14
     return 0; /* not an error */
e76f14
   if (!WIFEXITED (status) || WEXITSTATUS (status) != 0) {
e76f14
-    set_ssh_error ("unexpected close status from ssh subprocess (%d)", status);
e76f14
+    set_ssh_internal_error ("unexpected close status from ssh subprocess (%d)",
e76f14
+                            status);
e76f14
     return -1;
e76f14
   }
e76f14
   return 0;
e76f14
@@ -799,7 +815,7 @@ compatible_version (const char *v2v_version)
e76f14
   /* The major version must always be 1. */
e76f14
   if (!STRPREFIX (v2v_version, "1.")) {
e76f14
     set_ssh_error ("virt-v2v major version is not 1 (\"%s\"), "
e76f14
-                   "this version of virt-p2v is not compatible",
e76f14
+                   "this version of virt-p2v is not compatible.",
e76f14
                    v2v_version);
e76f14
     return 0;
e76f14
   }
e76f14
@@ -810,15 +826,15 @@ compatible_version (const char *v2v_version)
e76f14
    * We should remain compatible with any virt-v2v after 1.28.
e76f14
    */
e76f14
   if (sscanf (v2v_version, "1.%u", &v2v_minor) != 1) {
e76f14
-    set_ssh_error ("cannot parse virt-v2v version string (\"%s\")",
e76f14
-                   v2v_version);
e76f14
+    set_ssh_internal_error ("cannot parse virt-v2v version string (\"%s\")",
e76f14
+                            v2v_version);
e76f14
     return 0;
e76f14
   }
e76f14
 
e76f14
   if (v2v_minor < 28) {
e76f14
     set_ssh_error ("virt-v2v version is < 1.28 (\"%s\"), "
e76f14
                    "you must upgrade to virt-v2v >= 1.28 on "
e76f14
-                   "the conversion server", v2v_version);
e76f14
+                   "the conversion server.", v2v_version);
e76f14
     return 0;
e76f14
   }
e76f14
 
e76f14
@@ -858,34 +874,35 @@ open_data_connection (struct config *config, int *local_port, int *remote_port)
e76f14
   case 100:                     /* Ephemeral port. */
e76f14
     port_str = strndup (&h->buffer[ovector[2]], ovector[3]-ovector[2]);
e76f14
     if (port_str == NULL) {
e76f14
-      set_ssh_error ("not enough memory for strndup");
e76f14
+      set_ssh_internal_error ("strndup: %m");
e76f14
       mexp_close (h);
e76f14
       return NULL;
e76f14
     }
e76f14
     if (sscanf (port_str, "%d", remote_port) != 1) {
e76f14
-      set_ssh_error ("cannot extract the port number from '%s'", port_str);
e76f14
+      set_ssh_internal_error ("cannot extract the port number from '%s'",
e76f14
+                              port_str);
e76f14
       mexp_close (h);
e76f14
       return NULL;
e76f14
     }
e76f14
     break;
e76f14
 
e76f14
   case MEXP_EOF:
e76f14
+    set_ssh_unexpected_eof ("\"ssh -R\" output");
e76f14
     mexp_close (h);
e76f14
-    set_ssh_error ("unexpected end of file waiting ssh -R output");
e76f14
     return NULL;
e76f14
 
e76f14
   case MEXP_TIMEOUT:
e76f14
+    set_ssh_unexpected_timeout ("\"ssh -R\" output");
e76f14
     mexp_close (h);
e76f14
-    set_ssh_error ("timeout waiting for ssh -R output");
e76f14
     return NULL;
e76f14
 
e76f14
   case MEXP_ERROR:
e76f14
-    set_ssh_error ("mexp_expect: %m");
e76f14
+    set_ssh_mexp_error ("mexp_expect");
e76f14
     mexp_close (h);
e76f14
     return NULL;
e76f14
 
e76f14
   case MEXP_PCRE_ERROR:
e76f14
-    set_ssh_error ("PCRE error: %d\n", mexp_get_pcre_error (h));
e76f14
+    set_ssh_pcre_error ();
e76f14
     mexp_close (h);
e76f14
     return NULL;
e76f14
   }
e76f14
@@ -909,19 +926,19 @@ wait_for_prompt (mexp_h *h)
e76f14
     return 0;
e76f14
 
e76f14
   case MEXP_EOF:
e76f14
-    set_ssh_error ("unexpected end of file waiting for prompt");
e76f14
+    set_ssh_unexpected_eof ("command prompt");
e76f14
     return -1;
e76f14
 
e76f14
   case MEXP_TIMEOUT:
e76f14
-    set_ssh_error ("timeout waiting for prompt");
e76f14
+    set_ssh_unexpected_timeout ("command prompt");
e76f14
     return -1;
e76f14
 
e76f14
   case MEXP_ERROR:
e76f14
-    set_ssh_error ("mexp_expect: %m");
e76f14
+    set_ssh_mexp_error ("mexp_expect");
e76f14
     return -1;
e76f14
 
e76f14
   case MEXP_PCRE_ERROR:
e76f14
-    set_ssh_error ("PCRE error: %d\n", mexp_get_pcre_error (h));
e76f14
+    set_ssh_pcre_error ();
e76f14
     return -1;
e76f14
   }
e76f14
 
e76f14
@@ -947,7 +964,7 @@ start_remote_connection (struct config *config,
e76f14
 
e76f14
   /* Create the remote directory. */
e76f14
   if (mexp_printf (h, "mkdir %s\n", remote_dir) == -1) {
e76f14
-    set_ssh_error ("mexp_printf: %m");
e76f14
+    set_ssh_mexp_error ("mexp_printf");
e76f14
     goto error;
e76f14
   }
e76f14
 
e76f14
@@ -957,7 +974,7 @@ start_remote_connection (struct config *config,
e76f14
   /* Write some useful config information to files in the remote directory. */
e76f14
   if (mexp_printf (h, "echo '%s' > %s/name\n",
e76f14
                    config->guestname, remote_dir) == -1) {
e76f14
-    set_ssh_error ("mexp_printf: %m");
e76f14
+    set_ssh_mexp_error ("mexp_printf");
e76f14
     goto error;
e76f14
   }
e76f14
 
e76f14
@@ -965,7 +982,7 @@ start_remote_connection (struct config *config,
e76f14
     goto error;
e76f14
 
e76f14
   if (mexp_printf (h, "date > %s/time\n", remote_dir) == -1) {
e76f14
-    set_ssh_error ("mexp_printf: %m");
e76f14
+    set_ssh_mexp_error ("mexp_printf");
e76f14
     goto error;
e76f14
   }
e76f14
 
e76f14
@@ -980,7 +997,7 @@ start_remote_connection (struct config *config,
e76f14
                    remote_dir, magic,
e76f14
                    libvirt_xml,
e76f14
                    magic) == -1) {
e76f14
-    set_ssh_error ("mexp_printf: %m");
e76f14
+    set_ssh_mexp_error ("mexp_printf");
e76f14
     goto error;
e76f14
   }
e76f14
 
e76f14
@@ -995,7 +1012,7 @@ start_remote_connection (struct config *config,
e76f14
                    remote_dir, magic,
e76f14
                    wrapper_script,
e76f14
                    magic) == -1) {
e76f14
-    set_ssh_error ("mexp_printf: %m");
e76f14
+    set_ssh_mexp_error ("mexp_printf");
e76f14
     goto error;
e76f14
   }
e76f14
 
e76f14
@@ -1003,7 +1020,7 @@ start_remote_connection (struct config *config,
e76f14
     goto error;
e76f14
 
e76f14
   if (mexp_printf (h, "chmod +x %s/virt-v2v-wrapper.sh\n", remote_dir) == -1) {
e76f14
-    set_ssh_error ("mexp_printf: %m");
e76f14
+    set_ssh_mexp_error ("mexp_printf");
e76f14
     goto error;
e76f14
   }
e76f14
 
e76f14
@@ -1020,7 +1037,7 @@ start_remote_connection (struct config *config,
e76f14
                      remote_dir, magic,
e76f14
                      dmesg,
e76f14
                      magic) == -1) {
e76f14
-      set_ssh_error ("mexp_printf: %m");
e76f14
+      set_ssh_mexp_error ("mexp_printf");
e76f14
       goto error;
e76f14
     }
e76f14
 
e76f14
-- 
aa0300
2.7.4
e76f14