[lxc-devel] [lxd/master] unix-hotplug: fix device removal and zero padding

brauner on Github lxc-bot at linuxcontainers.org
Fri Mar 6 08:43:38 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 394 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200306/355306ec/attachment.bin>
-------------- next part --------------
From fbb11cc79276a927990ce205895cf07fe2839c60 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at 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.brauner at 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


More information about the lxc-devel mailing list