Skip to content
This repository was archived by the owner on Mar 28, 2018. It is now read-only.

Commit e78743e

Browse files
committed
shim: Restore terminal before exiting.
Register the function to restore terminal at atexit. Signed-off-by: Archana Shinde <archana.m.shinde@intel.com>
1 parent 1099acc commit e78743e

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

shim/shim.c

+28-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <stdbool.h>
3232
#include <limits.h>
3333
#include <fcntl.h>
34+
#include <termios.h>
3435

3536
#include "utils.h"
3637
#include "log.h"
@@ -43,6 +44,8 @@ int signal_pipe_fd[2] = { -1, -1 };
4344

4445
static char *program_name;
4546

47+
struct termios *saved_term_settings;
48+
4649
/*!
4750
* Add file descriptor to the array of polled descriptors
4851
*
@@ -107,6 +110,17 @@ assign_all_signals(struct sigaction *sa)
107110
return true;
108111
}
109112

113+
void restore_terminal(void) {
114+
if ( isatty(STDIN_FILENO) && saved_term_settings) {
115+
if (tcsetattr (STDIN_FILENO, TCSANOW, saved_term_settings)) {
116+
shim_warning("Unable to restore terminal: %s\n",
117+
strerror(errno));
118+
}
119+
free(saved_term_settings);
120+
saved_term_settings = NULL;
121+
}
122+
}
123+
110124
/*!
111125
* Print formatted message to stderr and exit with EXIT_FAILURE
112126
*
@@ -125,6 +139,7 @@ err_exit(const char *format, ...)
125139
va_start(args, format);
126140
vfprintf(stderr, format, args);
127141
va_end(args);
142+
restore_terminal();
128143
exit(EXIT_FAILURE);
129144
}
130145

@@ -432,6 +447,7 @@ handle_proxy_output(struct cc_shim *shim)
432447
code = *(buf + STREAM_HEADER_SIZE); // hyperstart has sent the exit status
433448
shim_debug("Exit status for container: %d\n", code);
434449
free(buf);
450+
restore_terminal();
435451
exit(code);
436452
}
437453

@@ -470,11 +486,9 @@ handle_proxy_ctl(struct cc_shim *shim)
470486

471487
ret = read(shim->proxy_sock_fd, buf, LINE_MAX-1);
472488
if (ret == -1) {
473-
shim_warning("Error reading from the proxy ctl socket: %s\n", strerror(errno));
474-
exit(EXIT_FAILURE);
489+
err_exit("Error reading from the proxy ctl socket: %s\n", strerror(errno));
475490
} else if (ret == 0) {
476-
shim_warning("EOF received on proxy ctl socket. Proxy has exited\n");
477-
exit(EXIT_FAILURE);
491+
err_exit("EOF received on proxy ctl socket. Proxy has exited\n");
478492
}
479493

480494
//TODO: Parse the json and log error responses explicitly
@@ -665,12 +679,22 @@ main(int argc, char **argv)
665679
struct termios term_settings;
666680

667681
tcgetattr(STDIN_FILENO, &term_settings);
682+
saved_term_settings = calloc(1, sizeof(struct termios));
683+
if ( !saved_term_settings) {
684+
abort();
685+
}
686+
*saved_term_settings = term_settings;
668687
cfmakeraw(&term_settings);
669688
tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_settings);
670689

671690
add_pollfd(poll_fds, &nfds, STDIN_FILENO, POLLIN | POLLPRI);
672691
}
673692

693+
ret = atexit(restore_terminal);
694+
if ( !ret) {
695+
shim_debug("Could not register function for atexit");
696+
}
697+
674698
/* 0 =>signal_pipe_fd[0]
675699
1 =>proxy_io_fd
676700
2 =>sockfd

0 commit comments

Comments
 (0)