Skip to content

Commit

Permalink
Disable canonical mode on host
Browse files Browse the repository at this point in the history
The commit configures the standard input in raw mode, so we can pass
unmodified user input to the guest operating system.
  • Loading branch information
RinHizakura committed Feb 14, 2022
1 parent a872c4b commit 0a3c225
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <getopt.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>

#include "err.h"
#include "vm.h"
Expand All @@ -17,6 +19,31 @@ static void usage(const char *execpath)
print_option("-i, --initrd initrd", "Initial RAM disk image\n");
}

static struct termios saved_attributes;

static void reset_input_mode(void)
{
tcsetattr(STDIN_FILENO, TCSANOW, &saved_attributes);
}

static void set_input_mode(void)
{
struct termios tattr;
/* Make sure stdin is a terminal. */
if (!isatty(STDIN_FILENO)) {
fprintf(stderr, "Not a terminal.\n");
exit(EXIT_FAILURE);
}

/* Save the terminal attributes so we can restore them later. */
tcgetattr(STDIN_FILENO, &saved_attributes);
atexit(reset_input_mode);

tattr = saved_attributes;
tattr.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &tattr);
}

int main(int argc, char *argv[])
{
int option_index = 0;
Expand All @@ -43,6 +70,8 @@ int main(int argc, char *argv[])
}
}

set_input_mode();

vm_t vm;
if (vm_init(&vm) < 0)
return throw_err("Failed to initialize guest vm");
Expand All @@ -58,5 +87,8 @@ int main(int argc, char *argv[])

vm_run(&vm);
vm_exit(&vm);

reset_input_mode();

return 0;
}

0 comments on commit 0a3c225

Please sign in to comment.