[libvirt] [PATCH] virsh: Fix job watching when STDIN is not a tty

Peter Krempa pkrempa at redhat.com
Tue Oct 22 14:05:05 UTC 2013


In commit b46c4787dde79b015dad67dedda4ccf6ff1a3082 I changed the code to
watch long running jobs in virsh. Unfortunately I didn't take into
account that poll may get a hangup if the terminal is not a TTY and will
be closed.

This patch avoids polling the STDIN fd when there's no TTY.
---
 tools/virsh-domain.c | 7 ++++++-
 tools/virsh.c        | 7 +++++++
 tools/virsh.h        | 2 ++
 3 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index b75f331..5aabccd 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -3529,6 +3529,7 @@ vshWatchJob(vshControl *ctl,
     bool functionReturn = false;
     sigset_t sigmask, oldsigmask;
     bool jobStarted = false;
+    nfds_t npollfd = 2;

     sigemptyset(&sigmask);
     sigaddset(&sigmask, SIGINT);
@@ -3539,9 +3540,13 @@ vshWatchJob(vshControl *ctl,
     sigemptyset(&sig_action.sa_mask);
     sigaction(SIGINT, &sig_action, &old_sig_action);

+    /* don't poll on STDIN if we are not using a terminal */
+    if (!vshTTYAvailable(ctl))
+        npollfd = 1;
+
     GETTIMEOFDAY(&start);
     while (1) {
-        ret = poll((struct pollfd *)&pollfd, 2, 500);
+        ret = poll((struct pollfd *)&pollfd, npollfd, 500);
         if (ret > 0) {
             if (pollfd[1].revents & POLLIN &&
                 saferead(STDIN_FILENO, &retchar, sizeof(retchar)) > 0) {
diff --git a/tools/virsh.c b/tools/virsh.c
index a76229a..8425f53 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -2226,6 +2226,13 @@ vshTTYIsInterruptCharacter(vshControl *ctl ATTRIBUTE_UNUSED,
 }


+bool
+vshTTYAvailable(vshControl *ctl)
+{
+    return ctl->istty;
+}
+
+
 int
 vshTTYDisableInterrupt(vshControl *ctl ATTRIBUTE_UNUSED)
 {
diff --git a/tools/virsh.h b/tools/virsh.h
index f978d94..b843788 100644
--- a/tools/virsh.h
+++ b/tools/virsh.h
@@ -365,6 +365,8 @@ bool vshTTYIsInterruptCharacter(vshControl *ctl, const char chr);
 int vshTTYDisableInterrupt(vshControl *ctl);
 int vshTTYRestore(vshControl *ctl);
 int vshTTYMakeRaw(vshControl *ctl, bool report_errors);
+bool vshTTYAvailable(vshControl *ctl);
+

 /* allocation wrappers */
 void *_vshMalloc(vshControl *ctl, size_t sz, const char *filename, int line);
-- 
1.8.3.2




More information about the libvir-list mailing list