diff options
author | Vaclav Dolezal | 2017-08-15 15:25:32 +0200 |
---|---|---|
committer | Karel Zak | 2017-08-24 13:08:22 +0200 |
commit | 6c8c429d4997e4d197f95e9aeb42d863c7bd32dd (patch) | |
tree | f637ca163ee40c745982b18641b4a1c3d55aec3b /disk-utils | |
parent | fdisk: add wrap_fgets() for getting user input (diff) | |
download | kernel-qcow2-util-linux-6c8c429d4997e4d197f95e9aeb42d863c7bd32dd.tar.gz kernel-qcow2-util-linux-6c8c429d4997e4d197f95e9aeb42d863c7bd32dd.tar.xz kernel-qcow2-util-linux-6c8c429d4997e4d197f95e9aeb42d863c7bd32dd.zip |
fdisk: handle SIGINT in dialogs as cancel
[kzak@redhat.com: - use sig_atomic_t]
Signed-off-by: Vaclav Dolezal <vdolezal@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'disk-utils')
-rw-r--r-- | disk-utils/fdisk.c | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/disk-utils/fdisk.c b/disk-utils/fdisk.c index d0d58fd67..e08acb783 100644 --- a/disk-utils/fdisk.c +++ b/disk-utils/fdisk.c @@ -21,6 +21,7 @@ #include <sys/time.h> #include <time.h> #include <limits.h> +#include <signal.h> #include <libsmartcols.h> #ifdef HAVE_LIBREADLINE # define _FUNCTION_DEF @@ -68,6 +69,12 @@ static void fdiskprog_init_debug(void) __UL_INIT_DEBUG(fdisk, FDISKPROG_DEBUG_, 0, FDISK_DEBUG); } +static sig_atomic_t volatile got_sigint = 0; +static void int_handler(int sig __attribute__((unused))) +{ + got_sigint = 1; +} + #ifdef HAVE_LIBREADLINE static char *rl_fgets(char *s, int n, FILE *stream, const char *prompt) { @@ -105,9 +112,25 @@ int get_user_reply(struct fdisk_context *cxt, const char *prompt, { char *p; size_t sz; + int ret = 0; + struct sigaction oldact, act = { + .sa_handler = int_handler, + }; + + sigemptyset(&act.sa_mask); + + got_sigint = 0; + sigaction(SIGINT, &act, &oldact); do { - if (!wrap_fgets(buf, bufsz, stdout, prompt)) { + char *tmp = wrap_fgets(buf, bufsz, stdout, prompt); + + if (got_sigint) { + ret = -ECANCELED; + goto end; + } + + if (!tmp) { if (fdisk_label_is_changed(fdisk_get_label(cxt, NULL))) { if (wrap_fgets(buf, bufsz, stderr, _("\nDo you really want to quit? ")) @@ -129,7 +152,9 @@ int get_user_reply(struct fdisk_context *cxt, const char *prompt, *(buf + sz - 1) = '\0'; DBG(ASK, ul_debug("user's reply: >>>%s<<<", buf)); - return 0; +end: + sigaction(SIGINT, &oldact, NULL); + return ret; } static int ask_menu(struct fdisk_context *cxt, struct fdisk_ask *ask, |