Skip to content

Commit

Permalink
Reset the terminal when exiting normally
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed May 16, 2020
1 parent f63b888 commit f7d9d82
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
22 changes: 17 additions & 5 deletions fs/tty-real.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// Only /dev/tty1 will be connected, the rest will go to a black hole.
#define REAL_TTY_NUM 1

void real_tty_reset_term(void);

static void real_tty_read_thread(struct tty *tty) {
char ch;
for (;;) {
Expand All @@ -25,6 +27,7 @@ static void real_tty_read_thread(struct tty *tty) {
}
if (ch == '\x1c') {
// ^\ (so ^C still works for emulated SIGINT)
real_tty_reset_term();
raise(SIGINT);
}
tty_input(tty, &ch, 1, 0);
Expand Down Expand Up @@ -76,7 +79,8 @@ static struct termios_ termios_from_real(struct termios real) {
}

static struct termios old_termios;
int real_tty_init(struct tty *tty) {
static bool real_tty_is_open;
static int real_tty_init(struct tty *tty) {
if (tty->num != REAL_TTY_NUM)
return 0;

Expand Down Expand Up @@ -109,20 +113,28 @@ int real_tty_init(struct tty *tty) {
// ok if this actually happened it would be weird AF
return _EIO;
pthread_detach(tty->thread);
real_tty_is_open = true;
return 0;
}

int real_tty_write(struct tty *tty, const void *buf, size_t len, bool UNUSED(blocking)) {
static int real_tty_write(struct tty *tty, const void *buf, size_t len, bool UNUSED(blocking)) {
if (tty->num != REAL_TTY_NUM)
return len;
return write(STDOUT_FILENO, buf, len);
}

void real_tty_cleanup(struct tty *tty) {
void real_tty_reset_term() {
if (!real_tty_is_open) return;
if (tcsetattr(STDIN_FILENO, TCSANOW, &old_termios) < 0 && errno != ENOTTY) {
printk("failed to reset terminal: %s\n", strerror(errno));
abort();
}
}

static void real_tty_cleanup(struct tty *tty) {
if (tty->num != REAL_TTY_NUM)
return;
if (tcsetattr(STDIN_FILENO, TCSANOW, &old_termios) < 0 && errno != ENOTTY)
ERRNO_DIE("failed to reset terminal");
real_tty_reset_term();
pthread_cancel(tty->thread);
}

Expand Down
4 changes: 3 additions & 1 deletion tests/e2e/shell/test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh
mknod /dev/null c 1 3 # shell uses this internally
if [ ! -e /dev/null ]; then
mknod /dev/null c 1 3 # shell uses this internally
fi

echo builtin echo
/bin/echo real echo
Expand Down
6 changes: 4 additions & 2 deletions xX_main_Xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
#define IOPOL_VFS_HFS_CASE_SENSITIVITY_FORCE_CASE_SENSITIVE 1
#endif

void real_tty_reset_term(void);

static void exit_handler(struct task *task, int code) {
if (task->parent != NULL)
return;
real_tty_reset_term();
if (code & 0xff)
raise(code & 0xff);
else
exit(code >> 8);
exit(code >> 8);
}

// this function parses command line arguments and initializes global
Expand Down

0 comments on commit f7d9d82

Please sign in to comment.