[Libguestfs] [PATCH 5/8] fish: Register ^C handler to cancel long transfers.

Richard W.M. Jones rjones at redhat.com
Fri Jul 15 14:24:02 UTC 2011


-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-df lists disk usage of guests without needing to install any
software inside the virtual machine.  Supports Linux and Windows.
http://et.redhat.com/~rjones/virt-df/
-------------- next part --------------
>From 50c54d2814b13d7d51f659600e93565dc6921b28 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones at redhat.com>
Date: Fri, 15 Jul 2011 11:01:23 +0100
Subject: [PATCH 5/8] fish: Register ^C handler to cancel long transfers.

---
 fish/fish.c   |   22 +++++++++++++++++++++-
 fish/reopen.c |    4 ++++
 2 files changed, 25 insertions(+), 1 deletions(-)

diff --git a/fish/fish.c b/fish/fish.c
index 750f50d..2dbcdf0 100644
--- a/fish/fish.c
+++ b/fish/fish.c
@@ -53,6 +53,7 @@ struct parsed_command {
   char *argv[64];
 };
 
+static void user_cancel (int);
 static void set_up_terminal (void);
 static void prepare_drives (struct drv *drv);
 static int launch (void);
@@ -72,7 +73,7 @@ static void add_history_line (const char *);
 static int override_progress_bars = -1;
 
 /* Currently open libguestfs handle. */
-guestfs_h *g;
+guestfs_h *g = NULL;
 
 int read_only = 0;
 int live = 0;
@@ -392,6 +393,18 @@ main (int argc, char *argv[])
    */
   is_interactive = !file && isatty (0);
 
+  /* Register a ^C handler.  We have to do this before launch could
+   * possibly be called below.
+   */
+  if (is_interactive) {
+    memset (&sa, 0, sizeof sa);
+    sa.sa_handler = user_cancel;
+    sa.sa_flags = SA_RESTART;
+    sigaction (SIGINT, &sa, NULL);
+
+    guestfs_set_pgroup (g, 1);
+  }
+
   /* Old-style -i syntax?  Since -a/-d/-N and -i was disallowed
    * previously, if we have -i without any drives but with something
    * on the command line, it must be old-style syntax.
@@ -515,6 +528,13 @@ main (int argc, char *argv[])
   exit (EXIT_SUCCESS);
 }
 
+static void
+user_cancel (int sig)
+{
+  if (g)
+    guestfs_user_cancel (g);
+}
+
 /* The <term.h> header file which defines this has "issues". */
 extern int tgetent (char *, const char *);
 
diff --git a/fish/reopen.c b/fish/reopen.c
index 67a845c..959cd2c 100644
--- a/fish/reopen.c
+++ b/fish/reopen.c
@@ -66,6 +66,10 @@ run_reopen (const char *cmd, size_t argc, char *argv[])
   if (p)
     guestfs_set_path (g2, p);
 
+  r = guestfs_get_pgroup (g);
+  if (r >= 0)
+    guestfs_set_pgroup (g2, r);
+
   if (progress_bars)
     guestfs_set_event_callback (g2, progress_callback,
                                 GUESTFS_EVENT_PROGRESS, 0, NULL);
-- 
1.7.5.2



More information about the Libguestfs mailing list