From ca3d41cba455fc3a46d9eb03f7a32bd0f2c78532 Mon Sep 17 00:00:00 2001
Message-Id: <ca3d41cba455fc3a46d9eb03f7a32bd0f2c78532.1427300678.git.jen@redhat.com>
In-Reply-To: <cd1e5c640073fe9f6f79125f2cbb3f434f1c7897.1427300678.git.jen@redhat.com>
References: <cd1e5c640073fe9f6f79125f2cbb3f434f1c7897.1427300678.git.jen@redhat.com>
From: Amos Kong <akong@redhat.com>
Date: Tue, 24 Mar 2015 23:00:02 -0500
Subject: [CHANGE 31/33] rng: initialize file descriptor to -1
To: rhvirt-patches@redhat.com,
    jen@redhat.com

RH-Author: Amos Kong <akong@redhat.com>
Message-id: <1427238004-11205-2-git-send-email-akong@redhat.com>
Patchwork-id: 64579
O-Subject: [RHEL-6.7 qemu-kvm PATCH 1/3] rng: initialize file descriptor to -1
Bugzilla: 1119575
RH-Acked-by: Marcel Apfelbaum <marcel@redhat.com>
RH-Acked-by: Amit Shah <amit.shah@redhat.com>
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>

From: Paolo Bonzini <pbonzini@redhat.com>

The file descriptor is never initialized to -1, which makes rng-random
close stdin if an object is created and immediately destroyed.  If we
change it to -1, we also need to protect qemu_set_fd_handler from
receiving a bogus file descriptor.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Igor Mammedov <imammedo@redhat.com>
Tested-by: Igor Mammedov <imammedo@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
(cherry picked from commit 513b8c74c4c899cd940a79131e76c5568aa11381)

Signed-off-by: Amos Kong <akong@redhat.com>
---
 backends/rng-random.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

Signed-off-by: Jeff E. Nelson <jen@redhat.com>
---
 backends/rng-random.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/backends/rng-random.c b/backends/rng-random.c
index a1b92ca..6f617df 100644
--- a/backends/rng-random.c
+++ b/backends/rng-random.c
@@ -125,15 +125,15 @@ static void rng_random_init(Object *obj)
                             NULL);
 
     s->filename = g_strdup("/dev/random");
+    s->fd = -1;
 }
 
 static void rng_random_finalize(Object *obj)
 {
     RndRandom *s = RNG_RANDOM(obj);
 
-    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
-
     if (s->fd != -1) {
+        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
         qemu_close(s->fd);
     }
 
-- 
2.1.0

