Fix typo
[platform/upstream/cryptsetup.git] / src / utils_tools.c
index 01ded12..ca39339 100644 (file)
@@ -4,10 +4,12 @@
  * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
  * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2009-2012, Milan Broz
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
- * version 2 as published by the Free Software Foundation.
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  */
 
 #include "cryptsetup.h"
+#include <signal.h>
 
 int opt_verbose = 0;
 int opt_debug = 0;
 int opt_batch_mode = 0;
 
+/* interrupt handling */
+volatile int quit = 0;
+static int signals_blocked = 0;
+
+static void int_handler(int sig __attribute__((__unused__)))
+{
+       quit++;
+}
+
+int tools_signals_blocked(void)
+{
+       return signals_blocked;
+}
+
+void set_int_block(int block)
+{
+       sigset_t signals_open;
+
+       log_dbg("%slocking interruption on signal.", block ? "B" : "Unb");
+
+       sigemptyset(&signals_open);
+       sigaddset(&signals_open, SIGINT);
+       sigaddset(&signals_open, SIGTERM);
+       sigprocmask(block ? SIG_SETMASK : SIG_UNBLOCK, &signals_open, NULL);
+       signals_blocked = block;
+       quit = 0;
+}
+
+void set_int_handler(int block)
+{
+       struct sigaction sigaction_open;
+
+       log_dbg("Installing SIGINT/SIGTERM handler.");
+       memset(&sigaction_open, 0, sizeof(struct sigaction));
+       sigaction_open.sa_handler = int_handler;
+       sigaction(SIGINT, &sigaction_open, 0);
+       sigaction(SIGTERM, &sigaction_open, 0);
+       set_int_block(block);
+}
+
+void check_signal(int *r)
+{
+       if (quit && !*r)
+               *r = -EINTR;
+}
+
 __attribute__((format(printf, 5, 6)))
 void clogger(struct crypt_device *cd, int level, const char *file, int line,
             const char *format, ...)
@@ -85,21 +134,30 @@ int yesDialog(const char *msg, void *usrptr __attribute__((unused)))
 {
        char *answer = NULL;
        size_t size = 0;
-       int r = 1;
+       int r = 1, block;
+
+       block = tools_signals_blocked();
+       if (block)
+               set_int_block(0);
 
        if(isatty(STDIN_FILENO) && !opt_batch_mode) {
                log_std("\nWARNING!\n========\n");
                log_std("%s\n\nAre you sure? (Type uppercase yes): ", msg);
                if(getline(&answer, &size, stdin) == -1) {
-                       perror("getline");
-                       free(answer);
-                       return 0;
-               }
-               if(strcmp(answer, "YES\n"))
                        r = 0;
-               free(answer);
+                       /* Aborted by signal */
+                       if (!quit)
+                               log_err(_("Error reading response from terminal.\n"));
+                       else
+                               log_dbg("Query interrupted on signal.");
+               } else if(strcmp(answer, "YES\n"))
+                       r = 0;
        }
 
+       if (block && !quit)
+               set_int_block(1);
+
+       free(answer);
        return r;
 }