From: Daniel Lezcano Date: Wed, 24 Feb 2010 09:57:42 +0000 (+0100) Subject: factor-out-console code X-Git-Tag: lxc-0.7.0~136 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6dae68151518a26f87169cf0d3bb2fd39624b75c;p=thirdparty%2Flxc.git factor-out-console code Factor out the console code and encapsulate the code in functions. Signed-off-by: Daniel Lezcano --- diff --git a/src/lxc/lxc_console.c b/src/lxc/lxc_console.c index 7f0e83e1d..1e00a8d2c 100644 --- a/src/lxc/lxc_console.c +++ b/src/lxc/lxc_console.c @@ -100,11 +100,43 @@ static void sigwinch(int sig) winsz(); } +static int setup_tios(int fd, struct termios *newtios, struct termios *oldtios) +{ + if (isatty(fd)) { + ERROR("'%d' is not a tty", fd); + return -1; + } + + /* Get current termios */ + if (tcgetattr(0, oldtios)) { + SYSERROR("failed to get current terminal settings"); + return -1; + } + + *newtios = *oldtios; + + /* Remove the echo characters and signal reception, the echo + * will be done below with master proxying */ + newtios->c_iflag &= ~IGNBRK; + newtios->c_iflag &= BRKINT; + newtios->c_lflag &= ~(ECHO|ICANON|ISIG); + newtios->c_cc[VMIN] = 1; + newtios->c_cc[VTIME] = 0; + + /* Set new attributes */ + if (tcsetattr(0, TCSAFLUSH, newtios)) { + ERROR("failed to set new terminal settings"); + return -1; + } + + return 0; +} + int main(int argc, char *argv[]) { int wait4q = 0; int err; - struct termios tios, oldtios; + struct termios newtios, oldtios; err = lxc_arguments_parse(&my_args, argc, argv); if (err) @@ -114,27 +146,8 @@ int main(int argc, char *argv[]) my_args.progname, my_args.quiet)) return -1; - /* Get current termios */ - if (tcgetattr(0, &tios)) { - ERROR("failed to get current terminal settings : %s", - strerror(errno)); - return -1; - } - - oldtios = tios; - - /* Remove the echo characters and signal reception, the echo - * will be done below with master proxying */ - tios.c_iflag &= ~IGNBRK; - tios.c_iflag &= BRKINT; - tios.c_lflag &= ~(ECHO|ICANON|ISIG); - tios.c_cc[VMIN] = 1; - tios.c_cc[VTIME] = 0; - - /* Set new attributes */ - if (tcsetattr(0, TCSAFLUSH, &tios)) { - ERROR("failed to set new terminal settings : %s", - strerror(errno)); + if (setup_tios(0, &newtios, &oldtios)) { + ERROR("failed to setup tios"); return -1; } @@ -145,8 +158,14 @@ int main(int argc, char *argv[]) fprintf(stderr, "\nType to exit the console\n", 'a' + my_args.escape - 1); - setsid(); - signal(SIGWINCH, sigwinch); + if (setsid()) + INFO("already group leader"); + + if (signal(SIGWINCH, sigwinch) == SIG_ERR) { + SYSERROR("failed to set SIGWINCH handler"); + return -1; + } + winsz(); err = 0;