Blame SOURCES/0058-rescue-Implement-escape-sequences.patch

151578
From 50e044064b4388f11912d96d2eb52aea1b292734 Mon Sep 17 00:00:00 2001
151578
From: "Richard W.M. Jones" <rjones@redhat.com>
151578
Date: Sat, 4 Mar 2017 11:47:59 +0000
151578
Subject: [PATCH] rescue: Implement escape sequences.
151578
151578
This implements a few useful escape sequences:
151578
151578
><rescue> ^]?
151578
virt-rescue escape sequences:
151578
 ^] ? - print this message
151578
 ^] h - print this message
151578
 ^] q - quit virt-rescue
151578
 ^] s - sync the filesystems
151578
 ^] u - unmount filesystems
151578
 ^] x - quit virt-rescue
151578
 ^] z - suspend virt-rescue
151578
to pass the escape key through to the rescue shell, type it twice
151578
151578
^]i
151578
151578
root device: /dev/sda3
151578
  product name: Fedora 25 (Twenty Five)
151578
  type: linux
151578
  distro: fedora
151578
151578
^]z
151578
[3]+  Stopped                 ./run virt-rescue --scratch
151578
$ fg
151578
151578
><rescue> ^]u
151578
151578
unmounting filesystems ...
151578
[   21.158558] XFS (sda3): Unmounting Filesystem
151578
151578
(cherry picked from commit 3637c42f4e521eb647d7dfae7f48eb1689d0af54)
151578
---
151578
 rescue/Makefile.am     |   4 +-
151578
 rescue/escape.c        | 302 +++++++++++++++++++++++++++++++++++++++++++++++++
151578
 rescue/rescue.c        |  30 ++++-
151578
 rescue/rescue.h        |  47 ++++++++
151578
 rescue/virt-rescue.pod |  74 ++++++++++++
151578
 5 files changed, 454 insertions(+), 3 deletions(-)
151578
 create mode 100644 rescue/escape.c
151578
 create mode 100644 rescue/rescue.h
151578
151578
diff --git a/rescue/Makefile.am b/rescue/Makefile.am
151578
index c83c43458..eb60bafa4 100644
151578
--- a/rescue/Makefile.am
151578
+++ b/rescue/Makefile.am
151578
@@ -26,7 +26,9 @@ EXTRA_DIST = \
151578
 bin_PROGRAMS = virt-rescue
151578
 
151578
 virt_rescue_SOURCES = \
151578
-	rescue.c
151578
+	escape.c \
151578
+	rescue.c \
151578
+	rescue.h
151578
 
151578
 virt_rescue_CPPFLAGS = \
151578
 	-DGUESTFS_WARN_DEPRECATED=1 \
151578
diff --git a/rescue/escape.c b/rescue/escape.c
151578
new file mode 100644
151578
index 000000000..f7f7d84c4
151578
--- /dev/null
151578
+++ b/rescue/escape.c
151578
@@ -0,0 +1,302 @@
151578
+/* virt-rescue
151578
+ * Copyright (C) 2010-2017 Red Hat Inc.
151578
+ *
151578
+ * This program is free software; you can redistribute it and/or modify
151578
+ * it under the terms of the GNU General Public License as published by
151578
+ * the Free Software Foundation; either version 2 of the License, or
151578
+ * (at your option) any later version.
151578
+ *
151578
+ * This program is distributed in the hope that it will be useful,
151578
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
151578
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
151578
+ * GNU General Public License for more details.
151578
+ *
151578
+ * You should have received a copy of the GNU General Public License
151578
+ * along with this program; if not, write to the Free Software
151578
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
151578
+ */
151578
+
151578
+#include <config.h>
151578
+
151578
+#include <stdio.h>
151578
+#include <stdlib.h>
151578
+#include <stdbool.h>
151578
+#include <string.h>
151578
+#include <unistd.h>
151578
+#include <signal.h>
151578
+#include <locale.h>
151578
+#include <libintl.h>
151578
+
151578
+#include "c-ctype.h"
151578
+
151578
+#include "guestfs.h"
151578
+#include "guestfs-internal-frontend.h"
151578
+
151578
+#include "rescue.h"
151578
+
151578
+static void print_help (void);
151578
+static void print_inspector (void);
151578
+static void crlf (void);
151578
+static void print_escape_key (void);
151578
+
151578
+/* Parse the -e parameter from the command line. */
151578
+int
151578
+parse_escape_key (const char *arg)
151578
+{
151578
+  size_t len;
151578
+
151578
+  if (STREQ (arg, "none"))
151578
+    return 0;
151578
+
151578
+  len = strlen (arg);
151578
+  if (arg == 0)
151578
+    return -1;
151578
+
151578
+  switch (arg[0]) {
151578
+  case '^':
151578
+    if (len == 2 &&
151578
+        ((arg[1] >= 'a' && arg[1] <= 'z') ||
151578
+         (arg[1] >= 'A' && arg[1] <= '_'))) {
151578
+      return c_toupper (arg[1]) - '@';
151578
+    }
151578
+    else
151578
+      return -1;
151578
+    break;
151578
+  }
151578
+
151578
+  return -1;
151578
+}
151578
+
151578
+/* Print one-line end user description of the escape key.
151578
+ *
151578
+ * This is printed when virt-rescue starts.
151578
+ */
151578
+void
151578
+print_escape_key_help (void)
151578
+{
151578
+  crlf ();
151578
+  /* Difficult to translate this string. XXX */
151578
+  printf ("The virt-rescue escape key is ‘");
151578
+  print_escape_key ();
151578
+  printf ("’.  Type ‘");
151578
+  print_escape_key ();
151578
+  printf (" h’ for help.");
151578
+  crlf ();
151578
+}
151578
+
151578
+void
151578
+init_escape_state (struct escape_state *state)
151578
+{
151578
+  state->in_escape = false;
151578
+}
151578
+
151578
+/* Process escapes in the tty input buffer.
151578
+ *
151578
+ * This function has a state parameter so that we can handle an escape
151578
+ * sequence split over the end of the buffer.
151578
+ *
151578
+ * Escape sequences are removed from the buffer.
151578
+ *
151578
+ * Returns true iff virt-rescue should exit.
151578
+ */
151578
+bool
151578
+process_escapes (struct escape_state *state, char *buf, size_t *len)
151578
+{
151578
+  size_t i;
151578
+
151578
+  for (i = 0; i < *len; ++i) {
151578
+#define DROP_CURRENT_CHAR() \
151578
+    memmove (&buf[i], &buf[i+1], --(*len))
151578
+#define PRINT_ESC() \
151578
+    do { print_escape_key (); putchar (buf[i]); crlf (); } while (0)
151578
+
151578
+    if (!state->in_escape) {
151578
+      if (buf[i] == escape_key) {
151578
+        /* Drop the escape key from the buffer and go to escape mode. */
151578
+        DROP_CURRENT_CHAR ();
151578
+        state->in_escape = true;
151578
+      }
151578
+    }
151578
+    else /* in escape sequence */ {
151578
+      if (buf[i] == escape_key) /* ^] ^] means send ^] to rescue shell */
151578
+        state->in_escape = false;
151578
+      else {
151578
+        switch (buf[i]) {
151578
+        case '?': case 'h':
151578
+          PRINT_ESC ();
151578
+          print_help ();
151578
+          break;
151578
+
151578
+        case 'i':
151578
+          PRINT_ESC ();
151578
+          print_inspector ();
151578
+          break;
151578
+
151578
+        case 'q': case 'x':
151578
+          PRINT_ESC ();
151578
+          return true /* exit virt-rescue at once */;
151578
+
151578
+        case 's':
151578
+          PRINT_ESC ();
151578
+          printf (_("attempting to sync filesystems ..."));
151578
+          crlf ();
151578
+          guestfs_sync (g);
151578
+          break;
151578
+
151578
+        case 'u':
151578
+          PRINT_ESC ();
151578
+          printf (_("unmounting filesystems ..."));
151578
+          crlf ();
151578
+          guestfs_umount_all (g);
151578
+          break;
151578
+
151578
+        case 'z':
151578
+          PRINT_ESC ();
151578
+          raise (SIGTSTP);
151578
+          break;
151578
+
151578
+        default:
151578
+          /* Any unrecognized escape sequence will be dropped.  We
151578
+           * could be obnoxious and ring the bell, but I hate it when
151578
+           * programs do that.
151578
+           */
151578
+          break;
151578
+        }
151578
+
151578
+        /* Drop the escape key and return to non-escape mode. */
151578
+        DROP_CURRENT_CHAR ();
151578
+        state->in_escape = false;
151578
+
151578
+        /* The output is line buffered, this is just to make sure
151578
+         * everything gets written to stdout before we continue
151578
+         * writing to STDOUT_FILENO.
151578
+         */
151578
+        fflush (stdout);
151578
+      }
151578
+    } /* in escape sequence */
151578
+  } /* for */
151578
+
151578
+  return false /* don't exit */;
151578
+}
151578
+
151578
+/* This is called when the user types ^] h */
151578
+static void
151578
+print_help (void)
151578
+{
151578
+  printf (_("virt-rescue escape sequences:"));
151578
+  crlf ();
151578
+
151578
+  putchar (' ');
151578
+  print_escape_key ();
151578
+  printf (_(" ? - print this message"));
151578
+  crlf ();
151578
+
151578
+  putchar (' ');
151578
+  print_escape_key ();
151578
+  printf (_(" h - print this message"));
151578
+  crlf ();
151578
+
151578
+  if (inspector) {
151578
+    putchar (' ');
151578
+    print_escape_key ();
151578
+    printf (_(" i - print inspection data"));
151578
+    crlf ();
151578
+  }
151578
+
151578
+  putchar (' ');
151578
+  print_escape_key ();
151578
+  printf (_(" q - quit virt-rescue"));
151578
+  crlf ();
151578
+
151578
+  putchar (' ');
151578
+  print_escape_key ();
151578
+  printf (_(" s - sync the filesystems"));
151578
+  crlf ();
151578
+
151578
+  putchar (' ');
151578
+  print_escape_key ();
151578
+  printf (_(" u - unmount filesystems"));
151578
+  crlf ();
151578
+
151578
+  putchar (' ');
151578
+  print_escape_key ();
151578
+  printf (_(" x - quit virt-rescue"));
151578
+  crlf ();
151578
+
151578
+  putchar (' ');
151578
+  print_escape_key ();
151578
+  printf (_(" z - suspend virt-rescue"));
151578
+  crlf ();
151578
+
151578
+  printf (_("to pass the escape key through to the rescue shell, type it twice"));
151578
+  crlf ();
151578
+}
151578
+
151578
+/* This is called when the user types ^] i */
151578
+static void
151578
+print_inspector (void)
151578
+{
151578
+  CLEANUP_FREE_STRING_LIST char **roots;
151578
+  size_t i;
151578
+  const char *root;
151578
+  char *str;
151578
+
151578
+  if (inspector) {
151578
+    roots = guestfs_inspect_get_roots (g);
151578
+    if (roots) {
151578
+      crlf ();
151578
+      for (i = 0; roots[i] != NULL; ++i) {
151578
+        root = roots[i];
151578
+        printf (_("root device: %s"), root);
151578
+        crlf ();
151578
+
151578
+        str = guestfs_inspect_get_product_name (g, root);
151578
+        if (str) {
151578
+          printf (_("  product name: %s"), str);
151578
+          crlf ();
151578
+        }
151578
+        free (str);
151578
+
151578
+        str = guestfs_inspect_get_type (g, root);
151578
+        if (str) {
151578
+          printf (_("  type: %s"), str);
151578
+          crlf ();
151578
+        }
151578
+        free (str);
151578
+
151578
+        str = guestfs_inspect_get_distro (g, root);
151578
+        if (str) {
151578
+          printf (_("  distro: %s"), str);
151578
+          crlf ();
151578
+        }
151578
+        free (str);
151578
+      }
151578
+    }
151578
+  }
151578
+}
151578
+
151578
+/* Because the terminal is in raw mode, we have to send CR LF instead
151578
+ * of printing just \n.
151578
+ */
151578
+static void
151578
+crlf (void)
151578
+{
151578
+  putchar ('\r');
151578
+  putchar ('\n');
151578
+}
151578
+
151578
+static void
151578
+print_escape_key (void)
151578
+{
151578
+  switch (escape_key) {
151578
+  case 0:
151578
+    printf ("none");
151578
+    break;
151578
+  case '\x1'...'\x1f':
151578
+    putchar ('^');
151578
+    putchar (escape_key + '@');
151578
+    break;
151578
+  default:
151578
+    abort ();
151578
+  }
151578
+}
151578
diff --git a/rescue/rescue.c b/rescue/rescue.c
151578
index 2b461378d..5281b1161 100644
151578
--- a/rescue/rescue.c
151578
+++ b/rescue/rescue.c
151578
@@ -1,5 +1,5 @@
151578
 /* virt-rescue
151578
- * Copyright (C) 2010-2012 Red Hat Inc.
151578
+ * Copyright (C) 2010-2017 Red Hat Inc.
151578
  *
151578
  * This program is free software; you can redistribute it and/or modify
151578
  * it under the terms of the GNU General Public License as published by
151578
@@ -40,10 +40,14 @@
151578
 #include "xvasprintf.h"
151578
 
151578
 #include "guestfs.h"
151578
+#include "guestfs-internal-frontend.h"
151578
+
151578
 #include "windows.h"
151578
 #include "options.h"
151578
 #include "display-options.h"
151578
 
151578
+#include "rescue.h"
151578
+
151578
 static void log_message_callback (guestfs_h *g, void *opaque, uint64_t event, int event_handle, int flags, const char *buf, size_t buf_len, const uint64_t *array, size_t array_len);
151578
 static void do_rescue (int sock);
151578
 static void raw_tty (void);
151578
@@ -65,6 +69,7 @@ const char *libvirt_uri = NULL;
151578
 int inspector = 0;
151578
 int in_guestfish = 0;
151578
 int in_virt_rescue = 1;
151578
+int escape_key = '\x1d';        /* ^] */
151578
 
151578
 /* Old terminal settings. */
151578
 static struct termios old_termios;
151578
@@ -86,6 +91,7 @@ usage (int status)
151578
               "  --append kernelopts  Append kernel options\n"
151578
               "  -c|--connect uri     Specify libvirt URI for -d option\n"
151578
               "  -d|--domain guest    Add disks from libvirt guest\n"
151578
+              "  -e ^x|none           Set or disable escape key (default ^])\n"
151578
               "  --format[=raw|..]    Force disk format for -a option\n"
151578
               "  --help               Display brief help\n"
151578
               "  -i|--inspector       Automatically mount filesystems\n"
151578
@@ -119,7 +125,7 @@ main (int argc, char *argv[])
151578
 
151578
   enum { HELP_OPTION = CHAR_MAX + 1 };
151578
 
151578
-  static const char options[] = "a:c:d:im:rvVwx";
151578
+  static const char options[] = "a:c:d:e:im:rvVwx";
151578
   static const struct option long_options[] = {
151578
     { "add", 1, 0, 'a' },
151578
     { "append", 1, 0, 0 },
151578
@@ -226,6 +232,12 @@ main (int argc, char *argv[])
151578
       OPTION_d;
151578
       break;
151578
 
151578
+    case 'e':
151578
+      escape_key = parse_escape_key (optarg);
151578
+      if (escape_key == -1)
151578
+        error (EXIT_FAILURE, 0, _("unrecognized escape key: %s"), optarg);
151578
+      break;
151578
+
151578
     case 'i':
151578
       OPTION_i;
151578
       break;
151578
@@ -428,6 +440,10 @@ main (int argc, char *argv[])
151578
   signal (SIGTSTP, tstp_handler);
151578
   signal (SIGCONT, cont_handler);
151578
 
151578
+  /* Print the escape key if set. */
151578
+  if (escape_key > 0)
151578
+    print_escape_key_help ();
151578
+
151578
   do_rescue (sock);
151578
 
151578
   restore_tty ();
151578
@@ -478,6 +494,9 @@ do_rescue (int sock)
151578
 {
151578
   size_t rlen = 0;
151578
   size_t wlen = 0;
151578
+  struct escape_state escape_state;
151578
+
151578
+  init_escape_state (&escape_state);
151578
 
151578
   while (sock >= 0 || rlen > 0) {
151578
     struct pollfd fds[3];
151578
@@ -534,6 +553,13 @@ do_rescue (int sock)
151578
       }
151578
       if (n > 0)
151578
         wlen += n;
151578
+
151578
+      /* Process escape sequences in the tty input.  If the function
151578
+       * returns true, then we exit the loop causing virt-rescue to
151578
+       * exit.
151578
+       */
151578
+      if (escape_key > 0 && process_escapes (&escape_state, wbuf, &wlen))
151578
+        return;
151578
     }
151578
 
151578
     /* Log message from appliance. */
151578
diff --git a/rescue/rescue.h b/rescue/rescue.h
151578
new file mode 100644
151578
index 000000000..ccffb5eb3
151578
--- /dev/null
151578
+++ b/rescue/rescue.h
151578
@@ -0,0 +1,47 @@
151578
+/* virt-rescue
151578
+ * Copyright (C) 2010-2017 Red Hat Inc.
151578
+ *
151578
+ * This program is free software; you can redistribute it and/or modify
151578
+ * it under the terms of the GNU General Public License as published by
151578
+ * the Free Software Foundation; either version 2 of the License, or
151578
+ * (at your option) any later version.
151578
+ *
151578
+ * This program is distributed in the hope that it will be useful,
151578
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
151578
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
151578
+ * GNU General Public License for more details.
151578
+ *
151578
+ * You should have received a copy of the GNU General Public License
151578
+ * along with this program; if not, write to the Free Software
151578
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
151578
+ */
151578
+
151578
+#ifndef RESCUE_H
151578
+#define RESCUE_H
151578
+
151578
+#include <stdbool.h>
151578
+
151578
+#include "guestfs.h"
151578
+
151578
+extern guestfs_h *g;
151578
+extern int read_only;
151578
+extern int live;
151578
+extern int verbose;
151578
+extern int keys_from_stdin;
151578
+extern int echo_keys;
151578
+extern const char *libvirt_uri;
151578
+extern int inspector;
151578
+extern int in_guestfish;
151578
+extern int in_virt_rescue;
151578
+extern int escape_key;
151578
+
151578
+/* escape.c */
151578
+struct escape_state {
151578
+  bool in_escape;
151578
+};
151578
+extern void init_escape_state (struct escape_state *state);
151578
+extern bool process_escapes (struct escape_state *state, char *buf, size_t *len);
151578
+extern int parse_escape_key (const char *);
151578
+extern void print_escape_key_help (void);
151578
+
151578
+#endif /* RESCUE_H */
151578
diff --git a/rescue/virt-rescue.pod b/rescue/virt-rescue.pod
151578
index b651f84e7..bd6f954e9 100644
151578
--- a/rescue/virt-rescue.pod
151578
+++ b/rescue/virt-rescue.pod
151578
@@ -128,6 +128,29 @@ not used at all.
151578
 Add all the disks from the named libvirt guest.  Domain UUIDs can be
151578
 used instead of names.
151578
 
151578
+=item B<-e none>
151578
+
151578
+Disable the escape key.
151578
+
151578
+=item B<-e> KEY
151578
+
151578
+Set the escape key to the given key sequence.  The default is C<^]>.
151578
+To specify the escape key you can use:
151578
+
151578
+=over 4
151578
+
151578
+=item C<^x>
151578
+
151578
+Control key + C<x> key.
151578
+
151578
+=item C<none>
151578
+
151578
+I<-e none> means there is no escape key, escapes are disabled.
151578
+
151578
+=back
151578
+
151578
+See L</ESCAPE KEY> below for further information.
151578
+
151578
 =item B<--format=raw|qcow2|..>
151578
 
151578
 =item B<--format>
151578
@@ -321,6 +344,57 @@ See L<bash(1)> for more details.
151578
 
151578
 =back
151578
 
151578
+=head1 ESCAPE KEY
151578
+
151578
+Virt-rescue supports various keyboard escape sequences which are
151578
+entered by pressing C<^]> (Control key + C<]> key).
151578
+
151578
+You can change the escape key using the I<-e> option on the command
151578
+line (see above), and you can disable escapes completely using
151578
+I<-e none>.  The rest of this section assumes the default escape key.
151578
+
151578
+The following escapes can be used:
151578
+
151578
+=over 4
151578
+
151578
+=item C<^] ?>
151578
+
151578
+=item C<^] h>
151578
+
151578
+Prints a brief help text about escape sequences.
151578
+
151578
+=item C<^] i>
151578
+
151578
+Prints brief libguestfs inspection information for the guest.  This
151578
+only works if you used I<-i> on the virt-rescue command line.
151578
+
151578
+=item C<^] q>
151578
+
151578
+=item C<^] x>
151578
+
151578
+Quits virt-rescue immediately.
151578
+
151578
+=item C<^] s>
151578
+
151578
+Synchronize the filesystems (sync).
151578
+
151578
+=item C<^] u>
151578
+
151578
+Unmounts all the filesystems, except for the root (appliance)
151578
+filesystems.
151578
+
151578
+=item C<^] z>
151578
+
151578
+Suspend virt-rescue (like pressing C<^Z> except that it affects
151578
+virt-rescue rather than the program inside the rescue shell).
151578
+
151578
+=item C<^] ^]>
151578
+
151578
+Sends the literal character C<^]> (ASCII 0x1d) through to the rescue
151578
+shell.
151578
+
151578
+=back
151578
+
151578
 =head1 CAPTURING CORE DUMPS
151578
 
151578
 If you are testing a tool inside virt-rescue and the tool (B<not>
151578
-- 
151578
2.14.3
151578