mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-12-17 21:26:13 -07:00
util: move read_password method out of qemu-img into osdep/oslib
The qemu-img.c file has a read_password() method impl that is used to prompt for passwords on the console, with impls for POSIX and Windows. This will be needed by qemu-io.c too, so move it into the QEMU osdep/oslib files where it can be shared without code duplication Signed-off-by: Daniel P. Berrange <berrange@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
8336aafae1
commit
d57e4e482e
4 changed files with 93 additions and 92 deletions
|
|
@ -50,6 +50,7 @@ extern int daemon(int, int);
|
|||
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <termios.h>
|
||||
|
||||
#include <glib/gprintf.h>
|
||||
|
||||
|
|
@ -415,3 +416,68 @@ void os_mem_prealloc(int fd, char *area, size_t memory)
|
|||
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static struct termios oldtty;
|
||||
|
||||
static void term_exit(void)
|
||||
{
|
||||
tcsetattr(0, TCSANOW, &oldtty);
|
||||
}
|
||||
|
||||
static void term_init(void)
|
||||
{
|
||||
struct termios tty;
|
||||
|
||||
tcgetattr(0, &tty);
|
||||
oldtty = tty;
|
||||
|
||||
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
|
||||
|INLCR|IGNCR|ICRNL|IXON);
|
||||
tty.c_oflag |= OPOST;
|
||||
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
|
||||
tty.c_cflag &= ~(CSIZE|PARENB);
|
||||
tty.c_cflag |= CS8;
|
||||
tty.c_cc[VMIN] = 1;
|
||||
tty.c_cc[VTIME] = 0;
|
||||
|
||||
tcsetattr(0, TCSANOW, &tty);
|
||||
|
||||
atexit(term_exit);
|
||||
}
|
||||
|
||||
int qemu_read_password(char *buf, int buf_size)
|
||||
{
|
||||
uint8_t ch;
|
||||
int i, ret;
|
||||
|
||||
printf("password: ");
|
||||
fflush(stdout);
|
||||
term_init();
|
||||
i = 0;
|
||||
for (;;) {
|
||||
ret = read(0, &ch, 1);
|
||||
if (ret == -1) {
|
||||
if (errno == EAGAIN || errno == EINTR) {
|
||||
continue;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else if (ret == 0) {
|
||||
ret = -1;
|
||||
break;
|
||||
} else {
|
||||
if (ch == '\r') {
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
if (i < (buf_size - 1)) {
|
||||
buf[i++] = ch;
|
||||
}
|
||||
}
|
||||
}
|
||||
term_exit();
|
||||
buf[i] = '\0';
|
||||
printf("\n");
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue