Skip to content

Commit

Permalink
usbmanager: do not panic
Browse files Browse the repository at this point in the history
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 <[email protected]>
(cherry picked from commit 06ed318)
  • Loading branch information
christoph-zededa authored and eriknordmark committed Aug 16, 2024
1 parent 2556a01 commit f43d7a2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
19 changes: 14 additions & 5 deletions pkg/pillar/cmd/usbmanager/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package usbmanager

import (
"errors"
"fmt"
"math"
"os"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -326,22 +328,29 @@ 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)

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)
Expand Down
6 changes: 4 additions & 2 deletions pkg/pillar/cmd/usbmanager/scanusb.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,17 @@ 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
}
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
Expand Down

0 comments on commit f43d7a2

Please sign in to comment.