From 6c8c429d4997e4d197f95e9aeb42d863c7bd32dd Mon Sep 17 00:00:00 2001 From: Vaclav Dolezal Date: Tue, 15 Aug 2017 15:25:32 +0200 Subject: fdisk: handle SIGINT in dialogs as cancel [kzak@redhat.com: - use sig_atomic_t] Signed-off-by: Vaclav Dolezal Signed-off-by: Karel Zak --- disk-utils/fdisk.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'disk-utils') 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 #include #include +#include #include #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, -- cgit v1.2.3-55-g7522