Hi,

The appended patch allows the users of suspend to abort the image saving by
pressing Ctrl+c.

Comments welcome.

Greetings,
Rafael


---
 suspend.c |   48 +++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 45 insertions(+), 3 deletions(-)

Index: suspend/suspend.c
===================================================================
--- suspend.orig/suspend.c      2006-11-01 20:55:15.000000000 +0100
+++ suspend/suspend.c   2006-11-02 07:44:53.000000000 +0100
@@ -29,6 +29,7 @@
 #include <string.h>
 #include <errno.h>
 #include <signal.h>
+#include <termios.h>
 #ifdef CONFIG_COMPRESS
 #include <lzf.h>
 #else
@@ -433,17 +434,41 @@ static int save_image(struct swap_map_ha
                       unsigned int nr_pages)
 {
        unsigned int m, writeout_rate;
-       int ret;
+       int ret, abort_possible;
+       struct termios newtrm, savedtrm;
+       char c = 0;
        int error = 0;
 
-       printf("suspend: Saving image data pages (%u pages) ...     ", 
nr_pages);
+       /* Switch the state of the terminal so that we can read the keyboard
+        * without blocking and with no echo.
+        *
+        * stdin must be attached to the terminal now.
+        */
+       abort_possible = !tcgetattr(0 , &savedtrm);
+       if (abort_possible) {
+               newtrm = savedtrm;
+               newtrm.c_cc[VMIN] = 0;
+               newtrm.c_cc[VTIME] = 1;
+               newtrm.c_iflag = IGNBRK | IGNPAR | ICRNL | IMAXBEL;
+               newtrm.c_lflag = 0;
+               abort_possible = !tcsetattr(0, TCSANOW, &newtrm);
+       }
+       if (abort_possible)
+               printf("suspend: Saving %u image data pages "
+                       "(press Ctrl+c to abort) ...     ", nr_pages);
+       else
+               printf("suspend: Saving image data pages (%u pages) ...     ",
+                       nr_pages);
+
        m = nr_pages / 100;
        if (!m)
                m = 1;
+
        if (early_writeout)
                writeout_rate = m;
        else
                writeout_rate = nr_pages;
+
        nr_pages = 0;
        do {
                ret = read(handle->dev, handle->page_buffer, page_size);
@@ -451,20 +476,34 @@ static int save_image(struct swap_map_ha
                        error = swap_write_page(handle);
                        if (error)
                                break;
+
                        if (!(nr_pages % m)) {
                                printf("\b\b\b\b%3d%%", nr_pages / m);
                                splash.progress(20 + (nr_pages / m) * 0.75);
+                               if (abort_possible) {
+                                       ret = read(0, &c, 1);
+                                       if (ret > 0 && c == 3) {
+                                               printf(" aborted!\n");
+                                               return -EINTR;
+                                       }
+                                       ret = 1;
+                               }
                        }
                        if (!(nr_pages % writeout_rate))
                                start_writeout(handle->fd);
+
                        nr_pages++;
                }
        } while (ret > 0);
        if (ret < 0)
                error = -errno;
+
        if (!error)
                printf(" done (%u pages)\n", nr_pages);
 
+       if (abort_possible)
+               tcsetattr(0, TCSANOW, &savedtrm);
+
        return error;
 }
 
@@ -1295,8 +1334,11 @@ int main(int argc, char *argv[])
 
        splash_prepare(&splash, splash_param);
 
-       if (lock_vt() < 0)
+       if (lock_vt() < 0) {
+               ret = errno;
+               fprintf(stderr, "suspend: Could not lock the terminal\n");
                goto Restore_console;
+       }
 
        splash.progress(5);
 

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Suspend-devel mailing list
Suspend-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/suspend-devel

Reply via email to