31
31
#include <stdbool.h>
32
32
#include <limits.h>
33
33
#include <fcntl.h>
34
+ #include <termios.h>
34
35
35
36
#include "utils.h"
36
37
#include "log.h"
@@ -43,6 +44,8 @@ int signal_pipe_fd[2] = { -1, -1 };
43
44
44
45
static char * program_name ;
45
46
47
+ struct termios * saved_term_settings ;
48
+
46
49
/*!
47
50
* Add file descriptor to the array of polled descriptors
48
51
*
@@ -107,6 +110,17 @@ assign_all_signals(struct sigaction *sa)
107
110
return true;
108
111
}
109
112
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
+
110
124
/*!
111
125
* Print formatted message to stderr and exit with EXIT_FAILURE
112
126
*
@@ -125,6 +139,7 @@ err_exit(const char *format, ...)
125
139
va_start (args , format );
126
140
vfprintf (stderr , format , args );
127
141
va_end (args );
142
+ restore_terminal ();
128
143
exit (EXIT_FAILURE );
129
144
}
130
145
@@ -432,6 +447,7 @@ handle_proxy_output(struct cc_shim *shim)
432
447
code = * (buf + STREAM_HEADER_SIZE ); // hyperstart has sent the exit status
433
448
shim_debug ("Exit status for container: %d\n" , code );
434
449
free (buf );
450
+ restore_terminal ();
435
451
exit (code );
436
452
}
437
453
@@ -470,11 +486,9 @@ handle_proxy_ctl(struct cc_shim *shim)
470
486
471
487
ret = read (shim -> proxy_sock_fd , buf , LINE_MAX - 1 );
472
488
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 ));
475
490
} 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" );
478
492
}
479
493
480
494
//TODO: Parse the json and log error responses explicitly
@@ -665,12 +679,22 @@ main(int argc, char **argv)
665
679
struct termios term_settings ;
666
680
667
681
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 ;
668
687
cfmakeraw (& term_settings );
669
688
tcsetattr (STDIN_FILENO , TCSAFLUSH , & term_settings );
670
689
671
690
add_pollfd (poll_fds , & nfds , STDIN_FILENO , POLLIN | POLLPRI );
672
691
}
673
692
693
+ ret = atexit (restore_terminal );
694
+ if ( !ret ) {
695
+ shim_debug ("Could not register function for atexit" );
696
+ }
697
+
674
698
/* 0 =>signal_pipe_fd[0]
675
699
1 =>proxy_io_fd
676
700
2 =>sockfd
0 commit comments