Skip to content

Commit 3168a1e

Browse files
committed
Handle Ctrl-a x sequence to exit
Originally, the guest will be terminated when ctrl-c is pressed. This causes the guest to exit unexpectedly. This change addresses this issue by removing ISIG flag in c_lflag in termios and adds a key sequence ctrl-a x to terminate the guest. Close #25
1 parent 706cada commit 3168a1e

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ containing concatenated `bootsect.o + setup.o + misc.o + piggy.o`. `initrd` is t
4242
initial RAM disk image, which is an optional argument.
4343
`disk-image` is the path to disk image which can be mounted as a block device via virtio. For the reference Linux guest, ext4 filesystem is used for disk image.
4444

45+
To exit kvm-host, press "Ctrl-A", release both keys, and then press "x".
46+
4547
## License
4648

4749
`kvm-host` is released under the BSD 2 clause license. Use of this source code is governed by

src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static void set_input_mode(void)
4242
atexit(reset_input_mode);
4343

4444
tattr = saved_attributes;
45-
tattr.c_lflag &= ~(ICANON | ECHO);
45+
tattr.c_lflag &= ~(ICANON | ECHO | ISIG);
4646
tcsetattr(STDIN_FILENO, TCSANOW, &tattr);
4747
}
4848

src/serial.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,28 @@ static void *serial_thread(serial_dev_t *s)
9797
return NULL;
9898
}
9999

100+
#define TERMINAL_ESCAPE_CHAR 0x01
101+
#define TERMINAL_EXIT_CHAR 'x'
102+
100103
void serial_console(serial_dev_t *s)
101104
{
102105
struct serial_dev_priv *priv = (struct serial_dev_priv *) s->priv;
106+
static bool escaped = false;
103107

104108
while (!fifo_is_full(&priv->rx_buf) && serial_readable(s, 0)) {
105109
char c;
106110
if (read(s->infd, &c, 1) == -1)
107111
break;
112+
if (escaped && c == TERMINAL_EXIT_CHAR) {
113+
/* Terminate */
114+
fprintf(stderr, "\n");
115+
exit(0);
116+
}
117+
if (!escaped && c == TERMINAL_ESCAPE_CHAR) {
118+
escaped = true;
119+
continue;
120+
}
121+
escaped = false;
108122
if (!fifo_put(&priv->rx_buf, c))
109123
break;
110124
__atomic_store_n(&priv->lsr, priv->lsr | UART_LSR_DR, __ATOMIC_RELEASE);

0 commit comments

Comments
 (0)