virsh: Remember terminal state when starting and add helpers

This patch adds instrumentation to allow modification of config of the
terminal in virsh and successful reset of the state afterwards.

The added helpers allow to disable receiving of SIGINT when pressing the
key sequence (Ctrl+C usualy). This normally sends SIGINT to the
foreground process group which kills ssh processes used for transport of
the data.
This commit is contained in:
Peter Krempa 2013-08-29 10:36:00 +02:00
parent f8456e5a18
commit ebef689363
2 changed files with 63 additions and 0 deletions

View File

@ -2213,6 +2213,53 @@ vshPrintExtra(vshControl *ctl, const char *format, ...)
}
bool
vshTTYIsInterruptCharacter(vshControl *ctl,
const char chr)
{
if (ctl->istty &&
ctl->termattr.c_cc[VINTR] == chr)
return true;
return false;
}
int
vshTTYDisableInterrupt(vshControl *ctl)
{
struct termios termset = ctl->termattr;
if (!ctl->istty)
return -1;
/* check if we need to set the terminal */
if (termset.c_cc[VINTR] == _POSIX_VDISABLE)
return 0;
termset.c_cc[VINTR] = _POSIX_VDISABLE;
termset.c_lflag &= ~ICANON;
if (tcsetattr(STDIN_FILENO, TCSANOW, &termset) < 0)
return -1;
return 0;
}
int
vshTTYRestore(vshControl *ctl)
{
if (!ctl->istty)
return 0;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &ctl->termattr) < 0)
return -1;
return 0;
}
void
vshError(vshControl *ctl, const char *format, ...)
{
@ -3157,6 +3204,13 @@ main(int argc, char **argv)
return EXIT_FAILURE;
}
if (isatty(STDIN_FILENO)) {
ctl->istty = true;
if (tcgetattr(STDIN_FILENO, &ctl->termattr) < 0)
ctl->istty = false;
}
if (virMutexInit(&ctl->lock) < 0) {
vshError(ctl, "%s", _("Failed to initialize mutex"));
return EXIT_FAILURE;

View File

@ -32,6 +32,7 @@
# include <unistd.h>
# include <sys/stat.h>
# include <inttypes.h>
# include <termios.h>
# include "internal.h"
# include "virerror.h"
@ -240,6 +241,9 @@ struct _vshControl {
const char *escapeChar; /* String representation of
console escape character */
struct termios termattr; /* settings of the tty terminal */
bool istty; /* is the terminal a tty */
};
struct _vshCmdGrp {
@ -350,6 +354,11 @@ void vshReportError(vshControl *ctl);
void vshResetLibvirtError(void);
void vshSaveLibvirtError(void);
/* terminal modifications */
bool vshTTYIsInterruptCharacter(vshControl *ctl, const char chr);
int vshTTYDisableInterrupt(vshControl *ctl);
int vshTTYRestore(vshControl *ctl);
/* allocation wrappers */
void *_vshMalloc(vshControl *ctl, size_t sz, const char *filename, int line);
# define vshMalloc(_ctl, _sz) _vshMalloc(_ctl, _sz, __FILE__, __LINE__)