diff options
author | Stefan Metzmacher <metze@samba.org> | 2007-10-17 14:00:23 +0200 |
---|---|---|
committer | Stefan Metzmacher <metze@samba.org> | 2007-12-21 05:43:11 +0100 |
commit | 2627603506452debf018e2ed8ae0b7912d64e58e (patch) | |
tree | d4caa7530a169d408e8d424b93adc93425c3cdbe /source4/lib/replace/getpass.c | |
parent | 090015a6fb40cf3dd925bc7db9015f91567c475e (diff) | |
download | samba-2627603506452debf018e2ed8ae0b7912d64e58e.tar.gz samba-2627603506452debf018e2ed8ae0b7912d64e58e.tar.bz2 samba-2627603506452debf018e2ed8ae0b7912d64e58e.zip |
r25679: reapply: Allow ^C to interrupt smbpasswd if using our getpass. from Jeremy
metze
(cherry picked from commit d4ae42b1b2982dd786d6da16d7fa964d25fd3356)
(This used to be commit a11d21790fc2ee33998e042195ccbad73631bad8)
Diffstat (limited to 'source4/lib/replace/getpass.c')
-rw-r--r-- | source4/lib/replace/getpass.c | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/source4/lib/replace/getpass.c b/source4/lib/replace/getpass.c index eb69e68f19..1cabf0bd87 100644 --- a/source4/lib/replace/getpass.c +++ b/source4/lib/replace/getpass.c @@ -138,6 +138,21 @@ static void catch_signal(int signum,void (*handler)(int )) #endif } +static sig_atomic_t gotintr; +static int in_fd = -1; + +/*************************************************************** + Signal function to tell us were ^C'ed. +****************************************************************/ + +static void gotintr_sig(void) +{ + gotintr = 1; + if (in_fd != -1) + close(in_fd); /* Safe way to force a return. */ + in_fd = -1; +} + char *getsmbpass(const char *prompt) { FILE *in, *out; @@ -147,7 +162,7 @@ char *getsmbpass(const char *prompt) size_t nread; /* Catch problematic signals */ - catch_signal(SIGINT, SIGNAL_CAST SIG_IGN); + catch_signal(SIGINT, SIGNAL_CAST gotintr_sig); /* Try to write to and read from the terminal if we can. If we can't open the terminal, use stderr and stdin. */ @@ -182,14 +197,21 @@ char *getsmbpass(const char *prompt) /* Read the password. */ buf[0] = 0; - fgets(buf, bufsize, in); + if (!gotintr) { + in_fd = fileno(in); + fgets(buf, bufsize, in); + } nread = strlen(buf); if (buf[nread - 1] == '\n') buf[nread - 1] = '\0'; /* Restore echoing. */ - if (echo_off) - tcsetattr (fileno (in), TCSANOW, &t); + if (echo_off) { + if (gotintr && in_fd == -1) + in = fopen ("/dev/tty", "w+"); + if (in != NULL) + tcsetattr (fileno (in), TCSANOW, &t); + } if (in != stdin) /* We opened the terminal; now close it. */ fclose(in); @@ -198,6 +220,12 @@ char *getsmbpass(const char *prompt) catch_signal(SIGINT, SIGNAL_CAST SIG_DFL); printf("\n"); + + if (gotintr) { + printf("Interupted by signal.\n"); + fflush(stdout); + exit(1); + } return buf; } |