File ee414b5d-fix-qemu-hotplug-usb-hostdev.patch of Package libvirt.4601

commit ee414b5d6d1601bde8440a9de050c02447bbd3bf
Author: Cole Robinson <crobinso@redhat.com>
Date:   Thu Dec 5 14:54:41 2013 -0500

    qemu: hotplug: Only label hostdev after checking device conflicts
    
    Similar to what Jiri did for cgroup setup/teardown in 05e149f94, push
    it all into the device handler functions so we can do the necessary prep
    work before claiming the device.
    
    This also fixes hotplugging USB devices by product/vendor (virt-manager's
    default behavior):
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1016511

Index: libvirt-1.1.2/src/qemu/qemu_hotplug.c
===================================================================
--- libvirt-1.1.2.orig/src/qemu/qemu_hotplug.c
+++ libvirt-1.1.2/src/qemu/qemu_hotplug.c
@@ -1135,6 +1135,7 @@ int qemuDomainAttachHostPciDevice(virQEM
     char *configfd_name = NULL;
     bool releaseaddr = false;
     bool teardowncgroup = false;
+    bool teardownlabel = false;
 
     if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs + 1) < 0)
         return -1;
@@ -1169,6 +1170,11 @@ int qemuDomainAttachHostPciDevice(virQEM
         goto error;
     teardowncgroup = true;
 
+    if (virSecurityManagerSetHostdevLabel(driver->securityManager,
+                                          vm->def, hostdev, NULL) < 0)
+        goto error;
+    teardownlabel = true;
+
     if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
         if (qemuAssignDeviceHostdevAlias(vm->def, hostdev, -1) < 0)
             goto error;
@@ -1227,6 +1233,10 @@ int qemuDomainAttachHostPciDevice(virQEM
 error:
     if (teardowncgroup && qemuTeardownHostdevCgroup(vm, hostdev) < 0)
         VIR_WARN("Unable to remove host device cgroup ACL on hotplug fail");
+    if (teardownlabel &&
+        virSecurityManagerRestoreHostdevLabel(driver->securityManager,
+                                              vm->def, hostdev, NULL) < 0)
+        VIR_WARN("Unable to restore host device labelling on hotplug fail");
 
     if (releaseaddr)
         qemuDomainReleaseDeviceAddress(vm, hostdev->info, NULL);
@@ -1421,6 +1431,7 @@ int qemuDomainAttachHostUsbDevice(virQEM
     char *devstr = NULL;
     bool added = false;
     bool teardowncgroup = false;
+    bool teardownlabel = false;
     int ret = -1;
 
     if (qemuFindHostdevUSBDevice(hostdev, true, &usb) < 0)
@@ -1442,6 +1453,11 @@ int qemuDomainAttachHostUsbDevice(virQEM
         goto cleanup;
     teardowncgroup = true;
 
+    if (virSecurityManagerSetHostdevLabel(driver->securityManager,
+                                          vm->def, hostdev, NULL) < 0)
+        goto cleanup;
+    teardownlabel = true;
+
     if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) {
         if (qemuAssignDeviceHostdevAlias(vm->def, hostdev, -1) < 0)
             goto cleanup;
@@ -1468,10 +1484,14 @@ int qemuDomainAttachHostUsbDevice(virQEM
 
     ret = 0;
 cleanup:
-    if (ret < 0 &&
-        teardowncgroup &&
-        qemuTeardownHostdevCgroup(vm, hostdev) < 0)
-        VIR_WARN("Unable to remove host device cgroup ACL on hotplug fail");
+    if (ret < 0) {
+        if (teardowncgroup && qemuTeardownHostdevCgroup(vm, hostdev) < 0)
+            VIR_WARN("Unable to remove host device cgroup ACL on hotplug fail");
+        if (teardownlabel &&
+            virSecurityManagerRestoreHostdevLabel(driver->securityManager,
+                                                  vm->def, hostdev, NULL) < 0)
+            VIR_WARN("Unable to restore host device labelling on hotplug fail");
+    }
     if (added)
         virUSBDeviceListSteal(driver->activeUsbHostdevs, usb);
     virUSBDeviceFree(usb);
@@ -1490,6 +1510,7 @@ qemuDomainAttachHostScsiDevice(virQEMUDr
     char *devstr = NULL;
     char *drvstr = NULL;
     bool teardowncgroup = false;
+    bool teardownlabel = false;
 
     if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_DRIVE) ||
         !virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE) ||
@@ -1514,6 +1535,11 @@ qemuDomainAttachHostScsiDevice(virQEMUDr
         goto cleanup;
     teardowncgroup = true;
 
+    if (virSecurityManagerSetHostdevLabel(driver->securityManager,
+                                          vm->def, hostdev, NULL) < 0)
+        goto cleanup;
+    teardownlabel = true;
+
     if (qemuAssignDeviceHostdevAlias(vm->def, hostdev, -1) < 0)
         goto cleanup;
 
@@ -1555,6 +1581,10 @@ cleanup:
         qemuDomainReAttachHostScsiDevices(driver, vm->def->name, &hostdev, 1);
         if (teardowncgroup && qemuTeardownHostdevCgroup(vm, hostdev) < 0)
             VIR_WARN("Unable to remove host device cgroup ACL on hotplug fail");
+        if (teardownlabel &&
+            virSecurityManagerRestoreHostdevLabel(driver->securityManager,
+                                                  vm->def, hostdev, NULL) < 0)
+            VIR_WARN("Unable to restore host device labelling on hotplug fail");
     }
     VIR_FREE(drvstr);
     VIR_FREE(devstr);
@@ -1572,10 +1602,6 @@ int qemuDomainAttachHostDevice(virQEMUDr
         return -1;
     }
 
-    if (virSecurityManagerSetHostdevLabel(driver->securityManager,
-                                          vm->def, hostdev, NULL) < 0)
-        return -1;
-
     switch (hostdev->source.subsys.type) {
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
         if (qemuDomainAttachHostPciDevice(driver, vm,
openSUSE Build Service is sponsored by