[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 16/21] Locking of the qemuMonitorPtr object



In preparation of the monitor I/O process becoming fully asynchronous,
it is neccessary to ensure all access to internals of the qemuMonitorPtr
object is protected by a mutex lock.

* src/qemu/qemu_monitor.c: Acquire & release a lock for all monitor
  commands
---
 src/qemu/qemu_monitor.c |  222 +++++++++++++++++++++++++++++++++++++---------
 1 files changed, 178 insertions(+), 44 deletions(-)

diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index b0e0cd5..33dbe1b 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -39,6 +39,8 @@
 #define VIR_FROM_THIS VIR_FROM_QEMU
 
 struct _qemuMonitor {
+    virMutex lock;
+
     int fd;
     int watch;
     int hasSendFD;
@@ -49,6 +51,16 @@ struct _qemuMonitor {
     qemuMonitorDiskSecretLookup secretCB;
 };
 
+static void qemuMonitorLock(qemuMonitorPtr mon)
+{
+    virMutexLock(&mon->lock);
+}
+
+static void qemuMonitorUnlock(qemuMonitorPtr mon)
+{
+    virMutexUnlock(&mon->lock);
+}
+
 /* Return -1 for error, 1 to continue reading and 0 for success */
 typedef int qemuMonitorHandleOutput(virDomainObjPtr vm,
                                     const char *output);
@@ -292,6 +304,12 @@ qemuMonitorOpen(virDomainObjPtr vm,
         return NULL;
     }
 
+    if (virMutexInit(&mon->lock) < 0) {
+        qemudReportError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, "%s",
+                         _("cannot initialize monitor mutex"));
+        VIR_FREE(mon);
+        return NULL;
+    }
     mon->fd = -1;
     mon->vm = vm;
     mon->eofCB = eofCB;
@@ -343,10 +361,10 @@ void qemuMonitorClose(qemuMonitorPtr mon)
 
     if (mon->fd != -1)
         close(mon->fd);
+    virMutexDestroy(&mon->lock);
     VIR_FREE(mon);
 }
 
-
 void qemuMonitorRegisterDiskSecretLookup(qemuMonitorPtr mon,
                                          qemuMonitorDiskSecretLookup secretCB)
 {
@@ -440,41 +458,61 @@ int qemuMonitorGetDiskSecret(qemuMonitorPtr mon,
 int
 qemuMonitorStartCPUs(qemuMonitorPtr mon,
                      virConnectPtr conn) {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d", mon, mon->fd);
 
-    return qemuMonitorTextStartCPUs(mon, conn);
+    ret = qemuMonitorTextStartCPUs(mon, conn);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
 int
 qemuMonitorStopCPUs(qemuMonitorPtr mon) {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d", mon, mon->fd);
 
-    return qemuMonitorTextStopCPUs(mon);
+    ret = qemuMonitorTextStopCPUs(mon);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
 int qemuMonitorSystemPowerdown(qemuMonitorPtr mon) {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d", mon, mon->fd);
 
-    return qemuMonitorTextSystemPowerdown(mon);
+    ret = qemuMonitorTextSystemPowerdown(mon);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
 int qemuMonitorGetCPUInfo(qemuMonitorPtr mon,
                           int **pids)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d", mon, mon->fd);
 
-    return qemuMonitorTextGetCPUInfo(mon, pids);
+    ret = qemuMonitorTextGetCPUInfo(mon, pids);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 int qemuMonitorGetBalloonInfo(qemuMonitorPtr mon,
                               unsigned long *currmem)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d", mon, mon->fd);
 
-    return qemuMonitorTextGetBalloonInfo(mon, currmem);
+    ret = qemuMonitorTextGetBalloonInfo(mon, currmem);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
@@ -486,38 +524,54 @@ int qemuMonitorGetBlockStatsInfo(qemuMonitorPtr mon,
                                  long long *wr_bytes,
                                  long long *errs)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d dev=%s", mon, mon->fd, devname);
 
-    return qemuMonitorTextGetBlockStatsInfo(mon, devname,
-                                            rd_req, rd_bytes,
-                                            wr_req, wr_bytes,
-                                            errs);
+    ret = qemuMonitorTextGetBlockStatsInfo(mon, devname,
+                                           rd_req, rd_bytes,
+                                           wr_req, wr_bytes,
+                                           errs);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
 int qemuMonitorSetVNCPassword(qemuMonitorPtr mon,
                               const char *password)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d", mon, mon->fd);
 
-    return qemuMonitorTextSetVNCPassword(mon, password);
+    ret = qemuMonitorTextSetVNCPassword(mon, password);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
 int qemuMonitorSetBalloon(qemuMonitorPtr mon,
                           unsigned long newmem)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d newmem=%lu", mon, mon->fd, newmem);
 
-    return qemuMonitorTextSetBalloon(mon, newmem);
+    ret = qemuMonitorTextSetBalloon(mon, newmem);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 int qemuMonitorEjectMedia(qemuMonitorPtr mon,
                           const char *devname)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d devname=%s", mon, mon->fd, devname);
 
-    return qemuMonitorTextEjectMedia(mon, devname);
+    ret = qemuMonitorTextEjectMedia(mon, devname);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
@@ -525,10 +579,14 @@ int qemuMonitorChangeMedia(qemuMonitorPtr mon,
                            const char *devname,
                            const char *newmedia)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d devname=%s newmedia=%s",
           mon, mon->fd, devname, newmedia);
 
-    return qemuMonitorTextChangeMedia(mon, devname, newmedia);
+    ret = qemuMonitorTextChangeMedia(mon, devname, newmedia);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
@@ -537,10 +595,14 @@ int qemuMonitorSaveVirtualMemory(qemuMonitorPtr mon,
                                  size_t length,
                                  const char *path)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d offset=%llu length=%zu path=%s",
           mon, mon->fd, offset, length, path);
 
-    return qemuMonitorTextSaveVirtualMemory(mon, offset, length, path);
+    ret = qemuMonitorTextSaveVirtualMemory(mon, offset, length, path);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 int qemuMonitorSavePhysicalMemory(qemuMonitorPtr mon,
@@ -548,19 +610,27 @@ int qemuMonitorSavePhysicalMemory(qemuMonitorPtr mon,
                                   size_t length,
                                   const char *path)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d offset=%llu length=%zu path=%s",
           mon, mon->fd, offset, length, path);
 
-    return qemuMonitorTextSavePhysicalMemory(mon, offset, length, path);
+    ret = qemuMonitorTextSavePhysicalMemory(mon, offset, length, path);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
 int qemuMonitorSetMigrationSpeed(qemuMonitorPtr mon,
                                  unsigned long bandwidth)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d bandwidth=%lu", mon, mon->fd, bandwidth);
 
-    return qemuMonitorTextSetMigrationSpeed(mon, bandwidth);
+    ret = qemuMonitorTextSetMigrationSpeed(mon, bandwidth);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 int qemuMonitorGetMigrationStatus(qemuMonitorPtr mon,
@@ -569,12 +639,16 @@ int qemuMonitorGetMigrationStatus(qemuMonitorPtr mon,
                                   unsigned long long *remaining,
                                   unsigned long long *total)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d", mon, mon->fd);
 
-    return qemuMonitorTextGetMigrationStatus(mon, status,
-                                             transferred,
-                                             remaining,
-                                             total);
+    ret = qemuMonitorTextGetMigrationStatus(mon, status,
+                                            transferred,
+                                            remaining,
+                                            total);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
@@ -583,10 +657,14 @@ int qemuMonitorMigrateToHost(qemuMonitorPtr mon,
                              const char *hostname,
                              int port)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d hostname=%s port=%d",
           mon, mon->fd, hostname, port);
 
-    return qemuMonitorTextMigrateToHost(mon, background, hostname, port);
+    ret = qemuMonitorTextMigrateToHost(mon, background, hostname, port);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
@@ -595,35 +673,51 @@ int qemuMonitorMigrateToCommand(qemuMonitorPtr mon,
                                 const char * const *argv,
                                 const char *target)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d argv=%p target=%s",
           mon, mon->fd, argv, target);
 
-    return qemuMonitorTextMigrateToCommand(mon, background, argv, target);
+    ret = qemuMonitorTextMigrateToCommand(mon, background, argv, target);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 int qemuMonitorMigrateToUnix(qemuMonitorPtr mon,
                              int background,
                              const char *unixfile)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p fd=%d unixfile=%s",
           mon, mon->fd, unixfile);
 
-    return qemuMonitorTextMigrateToUnix(mon, background, unixfile);
+    ret = qemuMonitorTextMigrateToUnix(mon, background, unixfile);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 int qemuMonitorMigrateCancel(qemuMonitorPtr mon)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p fd=%d", mon, mon->fd);
 
-    return qemuMonitorTextMigrateCancel(mon);
+    ret = qemuMonitorTextMigrateCancel(mon);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 int qemuMonitorAddUSBDisk(qemuMonitorPtr mon,
                           const char *path)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d path=%s", mon, mon->fd, path);
 
-    return qemuMonitorTextAddUSBDisk(mon, path);
+    ret = qemuMonitorTextAddUSBDisk(mon, path);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
@@ -631,19 +725,27 @@ int qemuMonitorAddUSBDeviceExact(qemuMonitorPtr mon,
                                  int bus,
                                  int dev)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d bus=%d dev=%d", mon, mon->fd, bus, dev);
 
-    return qemuMonitorTextAddUSBDeviceExact(mon, bus, dev);
+    ret = qemuMonitorTextAddUSBDeviceExact(mon, bus, dev);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 int qemuMonitorAddUSBDeviceMatch(qemuMonitorPtr mon,
                                  int vendor,
                                  int product)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d vendor=%d product=%d",
           mon, mon->fd, vendor, product);
 
-    return qemuMonitorTextAddUSBDeviceMatch(mon, vendor, product);
+    ret = qemuMonitorTextAddUSBDeviceMatch(mon, vendor, product);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
@@ -656,16 +758,20 @@ int qemuMonitorAddPCIHostDevice(qemuMonitorPtr mon,
                                 unsigned *guestBus,
                                 unsigned *guestSlot)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d domain=%d bus=%d slot=%d function=%d",
           mon, mon->fd,
           hostDomain, hostBus, hostSlot, hostFunction);
 
-    return qemuMonitorTextAddPCIHostDevice(mon, hostDomain,
-                                           hostBus, hostSlot,
-                                           hostFunction,
-                                           guestDomain,
-                                           guestBus,
-                                           guestSlot);
+    ret = qemuMonitorTextAddPCIHostDevice(mon, hostDomain,
+                                          hostBus, hostSlot,
+                                          hostFunction,
+                                          guestDomain,
+                                          guestBus,
+                                          guestSlot);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
@@ -676,11 +782,15 @@ int qemuMonitorAddPCIDisk(qemuMonitorPtr mon,
                           unsigned *guestBus,
                           unsigned *guestSlot)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d path=%s bus=%s",
           mon, mon->fd, path, bus);
 
-    return qemuMonitorTextAddPCIDisk(mon, path, bus,
-                                     guestDomain, guestBus, guestSlot);
+    ret = qemuMonitorTextAddPCIDisk(mon, path, bus,
+                                    guestDomain, guestBus, guestSlot);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
@@ -690,10 +800,14 @@ int qemuMonitorAddPCINetwork(qemuMonitorPtr mon,
                              unsigned *guestBus,
                              unsigned *guestSlot)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d nicstr=%s", mon, mon->fd, nicstr);
 
-    return qemuMonitorTextAddPCINetwork(mon, nicstr, guestDomain,
-                                        guestBus, guestSlot);
+    ret = qemuMonitorTextAddPCINetwork(mon, nicstr, guestDomain,
+                                       guestBus, guestSlot);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
@@ -702,11 +816,15 @@ int qemuMonitorRemovePCIDevice(qemuMonitorPtr mon,
                                unsigned guestBus,
                                unsigned guestSlot)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d domain=%d bus=%d slot=%d",
           mon, mon->fd, guestDomain, guestBus, guestSlot);
 
-    return qemuMonitorTextRemovePCIDevice(mon, guestDomain,
-                                          guestBus, guestSlot);
+    ret = qemuMonitorTextRemovePCIDevice(mon, guestDomain,
+                                         guestBus, guestSlot);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
@@ -714,30 +832,42 @@ int qemuMonitorSendFileHandle(qemuMonitorPtr mon,
                               const char *fdname,
                               int fd)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d fdname=%s fd=%d",
           mon, mon->fd, fdname, fd);
 
-    return qemuMonitorTextSendFileHandle(mon, fdname, fd);
+    ret = qemuMonitorTextSendFileHandle(mon, fdname, fd);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
 int qemuMonitorCloseFileHandle(qemuMonitorPtr mon,
                                const char *fdname)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d fdname=%s",
           mon, mon->fd, fdname);
 
-    return qemuMonitorTextCloseFileHandle(mon, fdname);
+    ret = qemuMonitorTextCloseFileHandle(mon, fdname);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
 int qemuMonitorAddHostNetwork(qemuMonitorPtr mon,
                               const char *netstr)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d netstr=%s",
           mon, mon->fd, netstr);
 
-    return qemuMonitorTextAddHostNetwork(mon, netstr);
+    ret = qemuMonitorTextAddHostNetwork(mon, netstr);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
 
 
@@ -745,8 +875,12 @@ int qemuMonitorRemoveHostNetwork(qemuMonitorPtr mon,
                                  int vlan,
                                  const char *netname)
 {
+    int ret;
+    qemuMonitorLock(mon);
     DEBUG("mon=%p, fd=%d netname=%s",
           mon, mon->fd, netname);
 
-    return qemuMonitorTextRemoveHostNetwork(mon, vlan, netname);
+    ret = qemuMonitorTextRemoveHostNetwork(mon, vlan, netname);
+    qemuMonitorUnlock(mon);
+    return ret;
 }
-- 
1.6.2.5


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]