From f43d7a27f661f937c5f1c8610f7f72f368279421 Mon Sep 17 00:00:00 2001 From: Christoph Ostarek Date: Sat, 10 Aug 2024 16:55:49 +0200 Subject: [PATCH] usbmanager: do not panic replace all panics with warnings sometimes the kernel f.e. cannot do a readlink of a file in /sys, because it is not a symlink (e.g. bonding_masters) Signed-off-by: Christoph Ostarek (cherry picked from commit 06ed3189bbbc5501e5aa8d46d57194cf7048cee5) --- pkg/pillar/cmd/usbmanager/rules.go | 19 ++++++++++++++----- pkg/pillar/cmd/usbmanager/scanusb.go | 6 ++++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pkg/pillar/cmd/usbmanager/rules.go b/pkg/pillar/cmd/usbmanager/rules.go index 38ec3f0685..18b81560a5 100644 --- a/pkg/pillar/cmd/usbmanager/rules.go +++ b/pkg/pillar/cmd/usbmanager/rules.go @@ -3,6 +3,7 @@ package usbmanager import ( + "errors" "fmt" "math" "os" @@ -221,7 +222,8 @@ func (cpr *compositionORPassthroughRule) String() string { func (cpr *compositionORPassthroughRule) evaluate(ud usbdevice) (passthroughAction, uint8) { if len(cpr.rules) == 0 { - panic("there has to be at least one rule") + log.Warnf("assertion failed, there has to be at least one rule") + return passthroughForbid, 0 } var ret passthroughAction @@ -326,7 +328,8 @@ func (*usbNetworkAdapterForbidPassthroughRule) netDevPathsImpl() []string { netDir := filepath.Join(sysFSPath, "class", "net") netDevfiles, err := os.ReadDir(netDir) if err != nil { - panic(err) + log.Warnf("readdir of %s failed: %v", netDir, err) + return []string{} } netDevPaths := make([]string, 0) @@ -334,14 +337,20 @@ func (*usbNetworkAdapterForbidPassthroughRule) netDevPathsImpl() []string { for _, file := range netDevfiles { // e.g. ../../devices/pci0000:00/0000:00:14.0/usb4/4-2/4-2.1/4-2.1:1.0/net/enp0s20f0u2u1/ relPath, err := os.Readlink(filepath.Join(netDir, file.Name())) + if errors.Is(err, os.ErrInvalid) { + continue + } if err != nil { - panic(err) + log.Warnf("readlink of %s failed: %v", relPath, err) + continue } // remove net/enp0s20f0u2u1/ and prefix with sysfs dir - absPath, err := filepath.Abs(filepath.Join(netDir, relPath, "..", "..")) + netDirPath := filepath.Join(netDir, relPath, "..", "..") + absPath, err := filepath.Abs(netDirPath) if err != nil { - panic(err) + log.Warnf("creating absolute filepath of %s failed: %v", netDirPath, err) + continue } netDevPaths = append(netDevPaths, absPath) diff --git a/pkg/pillar/cmd/usbmanager/scanusb.go b/pkg/pillar/cmd/usbmanager/scanusb.go index 56de86e506..5d8e78b2ab 100644 --- a/pkg/pillar/cmd/usbmanager/scanusb.go +++ b/pkg/pillar/cmd/usbmanager/scanusb.go @@ -128,7 +128,8 @@ func ueventFile2usbDeviceImpl(ueventFilePath string, ueventFp io.Reader) *usbdev if vals[0] == "BUSNUM" { val64, err := strconv.ParseUint(vals[1], 10, 16) if err != nil { - panic(err) + log.Warnf("could not parse BUSNUM %+v", vals) + return nil } busnum = uint16(val64) busnumSet = true @@ -136,7 +137,8 @@ func ueventFile2usbDeviceImpl(ueventFilePath string, ueventFp io.Reader) *usbdev if vals[0] == "DEVNUM" { val64, err := strconv.ParseUint(vals[1], 10, 16) if err != nil { - panic(err) + log.Warnf("could not parse DEVNUM %+v", vals) + return nil } devnum = uint16(val64) devnumSet = true