From: Daniel Lezcano Date: Sun, 7 Jun 2009 19:48:46 +0000 (+0200) Subject: save/restore the tty X-Git-Tag: lxc_0_6_3~51 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f8e09a0b769d185e66dc488796fc027abd46266d;p=thirdparty%2Flxc.git save/restore the tty Save the tty configuration before calling lxc_start and restore it right after it has been changed. Signed-off-by: Daniel Lezcano --- diff --git a/src/lxc/lxc_start.c b/src/lxc/lxc_start.c index cfdb17a37..8db3bd3c7 100644 --- a/src/lxc/lxc_start.c +++ b/src/lxc/lxc_start.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -58,6 +59,46 @@ Options :\n\ .checker = NULL, }; +static int save_tty(struct termios *tios) +{ + if (!isatty(0)) + return 0; + + if (tcgetattr(0, tios)) + WARN("failed to get current terminal settings : %s", + strerror(errno)); + + return 0; +} + +static int restore_tty(struct termios *tios) +{ + struct termios current_tios; + void (*oldhandler)(int); + int ret; + + if (!isatty(0)) + return 0; + + if (tcgetattr(0, ¤t_tios)) { + ERROR("failed to get current terminal settings : %s", + strerror(errno)); + return -1; + } + + if (!memcmp(tios, ¤t_tios, sizeof(*tios))) + return 0; + + + oldhandler = signal(SIGTTOU, SIG_IGN); + ret = tcsetattr(0, TCSADRAIN, tios); + if (ret) + ERROR("failed to restore terminal attributes"); + signal(SIGTTOU, oldhandler); + + return ret; +} + int main(int argc, char *argv[]) { char *const *args; @@ -81,17 +122,11 @@ int main(int argc, char *argv[]) my_args.progname, my_args.quiet)) return err; - if (tcgetattr(0, &tios)) { - ERROR("failed to get current terminal settings : %s", - strerror(errno)); - return err; - } + save_tty(&tios); err = lxc_start(my_args.name, args); - if (tcsetattr(0, TCSAFLUSH, &tios)) - ERROR("failed to restore terminal settings : %s", - strerror(errno)); + restore_tty(&tios); return err; }