mirror of
git://github.com/lxc/lxc
synced 2025-09-01 19:09:29 +00:00
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 <dlezcano@fr.ibm.com>
This commit is contained in:
committed by
Daniel Lezcano
parent
d4fca29f01
commit
f8e09a0b76
@@ -26,6 +26,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
#include <sys/utsname.h>
|
#include <sys/utsname.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
@@ -58,6 +59,46 @@ Options :\n\
|
|||||||
.checker = NULL,
|
.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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
char *const *args;
|
char *const *args;
|
||||||
@@ -81,17 +122,11 @@ int main(int argc, char *argv[])
|
|||||||
my_args.progname, my_args.quiet))
|
my_args.progname, my_args.quiet))
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
if (tcgetattr(0, &tios)) {
|
save_tty(&tios);
|
||||||
ERROR("failed to get current terminal settings : %s",
|
|
||||||
strerror(errno));
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
err = lxc_start(my_args.name, args);
|
err = lxc_start(my_args.name, args);
|
||||||
|
|
||||||
if (tcsetattr(0, TCSAFLUSH, &tios))
|
restore_tty(&tios);
|
||||||
ERROR("failed to restore terminal settings : %s",
|
|
||||||
strerror(errno));
|
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user