When the agent code was first introduced back in
commit c160ce3316852a797d7b06b4ee101233866e69a9
Author: Daniel P. Berrange <berrange(a)redhat.com>
Date: Wed Oct 5 18:31:54 2011 +0100
QEMU guest agent support
there was code that would loop and retry the connection when opening
the agent socket. At this time, the only thing done in between the
opening of the monitor socket & opening of the agent socket was a
call to set the monitor capabilities. This was a no-op on non-QMP
versions, so in theory there could be a race which let us connect
to the monitor while the agent socket was still not created by QEMU.
In the modern world, however, we long ago mandated the use of QMP
for managing QEMU, so we're guaranteed to have a set capabilities
QMP call. Once we've seen a reply to this, we're guaranteed that
QEMU has fully initialized all backends and is in its event loop.
We can thus be sure the QEMU agent socket is present and don't need
to retry connections to it, even without having the chardev FD passing
feature.
Signed-off-by: Daniel P. Berrangé <berrange(a)redhat.com>
---
src/qemu/qemu_agent.c | 84 +++++----------------------------------------------
1 file changed, 7 insertions(+), 77 deletions(-)
diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c
index 85af53d194..a68c1e3897 100644
--- a/src/qemu/qemu_agent.c
+++ b/src/qemu/qemu_agent.c
@@ -106,7 +106,6 @@ struct _qemuAgent {
int fd;
int watch;
- bool connectPending;
bool running;
virDomainObjPtr vm;
@@ -183,15 +182,12 @@ static void qemuAgentDispose(void *obj)
}
static int
-qemuAgentOpenUnix(const char *monitor, pid_t cpid, bool *inProgress)
+qemuAgentOpenUnix(const char *monitor)
{
struct sockaddr_un addr;
int monfd;
- virTimeBackOffVar timeout;
int ret = -1;
- *inProgress = false;
-
if ((monfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
virReportSystemError(errno,
"%s", _("failed to create socket"));
@@ -220,39 +216,11 @@ qemuAgentOpenUnix(const char *monitor, pid_t cpid, bool
*inProgress)
goto error;
}
- if (virTimeBackOffStart(&timeout, 1, 3*1000 /* ms */) < 0)
- goto error;
- while (virTimeBackOffWait(&timeout)) {
- ret = connect(monfd, (struct sockaddr *) &addr, sizeof(addr));
-
- if (ret == 0)
- break;
-
- if ((errno == ENOENT || errno == ECONNREFUSED) &&
- virProcessKill(cpid, 0) == 0) {
- /* ENOENT : Socket may not have shown up yet
- * ECONNREFUSED : Leftover socket hasn't been removed yet */
- continue;
- }
-
- if ((errno == EINPROGRESS) ||
- (errno == EAGAIN)) {
- VIR_DEBUG("Connection attempt continuing in background");
- *inProgress = true;
- ret = 0;
- break;
- }
-
+ ret = connect(monfd, (struct sockaddr *) &addr, sizeof(addr));
+ if (ret < 0) {
virReportSystemError(errno, "%s",
_("failed to connect to monitor socket"));
goto error;
-
- }
-
- if (ret != 0) {
- virReportSystemError(errno, "%s",
- _("monitor socket did not show up"));
- goto error;
}
return monfd;
@@ -473,35 +441,6 @@ qemuAgentIOProcess(qemuAgentPtr mon)
}
-static int
-qemuAgentIOConnect(qemuAgentPtr mon)
-{
- int optval;
- socklen_t optlen;
-
- VIR_DEBUG("Checking on background connection status");
-
- mon->connectPending = false;
-
- optlen = sizeof(optval);
-
- if (getsockopt(mon->fd, SOL_SOCKET, SO_ERROR,
- &optval, &optlen) < 0) {
- virReportSystemError(errno, "%s",
- _("Cannot check socket connection status"));
- return -1;
- }
-
- if (optval != 0) {
- virReportSystemError(optval, "%s",
- _("Cannot connect to agent socket"));
- return -1;
- }
-
- VIR_DEBUG("Agent is now connected");
- return 0;
-}
-
/*
* Called when the monitor is able to write data
* Call this function while holding the monitor lock.
@@ -633,13 +572,8 @@ qemuAgentIO(int watch, int fd, int events, void *opaque)
error = true;
} else {
if (events & VIR_EVENT_HANDLE_WRITABLE) {
- if (mon->connectPending) {
- if (qemuAgentIOConnect(mon) < 0)
- error = true;
- } else {
- if (qemuAgentIOWrite(mon) < 0)
- error = true;
- }
+ if (qemuAgentIOWrite(mon) < 0)
+ error = true;
events &= ~VIR_EVENT_HANDLE_WRITABLE;
}
@@ -771,8 +705,7 @@ qemuAgentOpen(virDomainObjPtr vm,
switch (config->type) {
case VIR_DOMAIN_CHR_TYPE_UNIX:
- mon->fd = qemuAgentOpenUnix(config->data.nix.path, vm->pid,
- &mon->connectPending);
+ mon->fd = qemuAgentOpenUnix(config->data.nix.path);
break;
case VIR_DOMAIN_CHR_TYPE_PTY:
@@ -793,10 +726,7 @@ qemuAgentOpen(virDomainObjPtr vm,
if ((mon->watch = virEventAddHandle(mon->fd,
VIR_EVENT_HANDLE_HANGUP |
VIR_EVENT_HANDLE_ERROR |
- VIR_EVENT_HANDLE_READABLE |
- (mon->connectPending ?
- VIR_EVENT_HANDLE_WRITABLE :
- 0),
+ VIR_EVENT_HANDLE_READABLE,
qemuAgentIO,
mon,
virObjectFreeCallback)) < 0) {
--
2.14.3