The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/6990

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
Closes #6972.
Closes #6986.
Signed-off-by: Christian Brauner <christian.brau...@ubuntu.com>
From fbb11cc79276a927990ce205895cf07fe2839c60 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brau...@ubuntu.com>
Date: Fri, 6 Mar 2020 09:42:24 +0100
Subject: [PATCH] unix-hotplug: fix device removal and zero padding

Closes #6972.
Closes #6986.
Signed-off-by: Christian Brauner <christian.brau...@ubuntu.com>
---
 lxd/device/unix_hotplug.go |  8 +++----
 lxd/devices.go             | 46 ++++++++++++++++++++++++++------------
 2 files changed, 36 insertions(+), 18 deletions(-)

diff --git a/lxd/device/unix_hotplug.go b/lxd/device/unix_hotplug.go
index 632fb81a4c..2c9adeea10 100644
--- a/lxd/device/unix_hotplug.go
+++ b/lxd/device/unix_hotplug.go
@@ -78,13 +78,13 @@ func (d *unixHotplug) Register() error {
 
        // Handler for when a UnixHotplug event occurs.
        f := func(e UnixHotplugEvent) (*deviceConfig.RunConfig, error) {
-               if !unixHotplugIsOurDevice(devConfig, &e) {
-                       return nil, nil
-               }
-
                runConf := deviceConfig.RunConfig{}
 
                if e.Action == "add" {
+                       if !unixHotplugIsOurDevice(devConfig, &e) {
+                               return nil, nil
+                       }
+
                        if e.Subsystem == "block" {
                                err := unixDeviceSetupBlockNum(state, 
devicesPath, "unix", deviceName, devConfig, e.Major, e.Minor, e.Path, false, 
&runConf)
                                if err != nil {
diff --git a/lxd/devices.go b/lxd/devices.go
index dd7c523bab..5060c1788c 100644
--- a/lxd/devices.go
+++ b/lxd/devices.go
@@ -227,17 +227,17 @@ func deviceNetlinkListener() (chan []string, chan 
[]string, chan device.USBEvent
 
                        // unix hotplug device events rely on information added 
by udev
                        if udevEvent {
-                               subsystem, ok := props["SUBSYSTEM"]
-                               if !ok {
+                               action := props["ACTION"]
+                               if action != "add" && action != "remove" {
                                        continue
                                }
 
-                               devname, ok := props["DEVNAME"]
+                               subsystem, ok := props["SUBSYSTEM"]
                                if !ok {
                                        continue
                                }
 
-                               vendor, product, ok := 
ueventParseVendorProduct(props, subsystem, devname)
+                               devname, ok := props["DEVNAME"]
                                if !ok {
                                        continue
                                }
@@ -252,18 +252,36 @@ func deviceNetlinkListener() (chan []string, chan 
[]string, chan device.USBEvent
                                        continue
                                }
 
+                               vendor := ""
+                               product := ""
+                               if action == "add" {
+                                       vendor, product, ok = 
ueventParseVendorProduct(props, subsystem, devname)
+                                       if !ok {
+                                               continue
+                                       }
+                               }
+
                                zeroPad := func(s string, l int) string {
                                        return strings.Repeat("0", l-len(s)) + s
                                }
 
+                               // zeropad
+                               if len(vendor) < 4 {
+                                       vendor = zeroPad(vendor, 4)
+                               }
+
+                               if len(product) < 4 {
+                                       product = zeroPad(product, 4)
+                               }
+
                                unix, err := device.UnixHotplugNewEvent(
-                                       props["ACTION"],
+                                       action,
                                        /* udev doesn't zero pad these, while
                                         * everything else does, so let's zero 
pad them
                                         * for consistency
                                         */
-                                       zeroPad(vendor, 4),
-                                       zeroPad(product, 4),
+                                       vendor,
+                                       product,
                                        major,
                                        minor,
                                        subsystem,
@@ -584,14 +602,14 @@ func getHidrawDevInfo(fd int) (string, string, error) {
 }
 
 func ueventParseVendorProduct(props map[string]string, subsystem string, 
devname string) (string, string, bool) {
-       if subsystem != "hidraw" {
-               vendor, vendorOk := props["ID_VENDOR_ID"]
-               product, productOk := props["ID_MODEL_ID"]
+       vendor, vendorOk := props["ID_VENDOR_ID"]
+       product, productOk := props["ID_MODEL_ID"]
 
-               if vendorOk && productOk {
-                       return vendor, product, true
-               }
+       if vendorOk && productOk {
+               return vendor, product, true
+       }
 
+       if subsystem != "hidraw" {
                return "", "", false
        }
 
@@ -606,7 +624,7 @@ func ueventParseVendorProduct(props map[string]string, 
subsystem string, devname
 
        defer file.Close()
 
-       vendor, product, err := getHidrawDevInfo(int(file.Fd()))
+       vendor, product, err = getHidrawDevInfo(int(file.Fd()))
        if err != nil {
                logger.Debugf("Failed to retrieve device info from hidraw 
device \"%s\"", devname)
                return "", "", false
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to