Skip to content

Linter and link fixes, allow handling of "abort error" #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions crawler/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand All @@ -22,6 +21,8 @@ type Device struct {
Env map[string]string
}

var ErrAbortReceived = errors.New("abort signal received")

// ExistingDevices return all plugged devices matched by the matcher
// All uevent files inside /sys/devices is crawled to match right env values
func ExistingDevices(queue chan Device, errs chan error, matcher netlink.Matcher) chan struct{} {
Expand All @@ -40,7 +41,7 @@ func ExistingDevices(queue chan Device, errs chan error, matcher netlink.Matcher
err := filepath.Walk(BASE_DEVPATH, func(path string, info os.FileInfo, err error) error {
select {
case <-quit:
return errors.New("abort signal receive")
return ErrAbortReceived
default:
if err != nil {
return err
Expand Down Expand Up @@ -80,34 +81,25 @@ func ExistingDevices(queue chan Device, errs chan error, matcher netlink.Matcher
return quit
}

// getEventFromUEventFile return all env var define in file
// syntax: name=value for each line
// Fonction use for /sys/.../uevent files
// getEventFromUEventFile return all variables defined in /sys/**/uevent files
func getEventFromUEventFile(path string) (map[string]string, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer func() {
_ = f.Close()
}()

data, err := io.ReadAll(f)
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return getEventFromUEventData(data), nil
return getEventFromUEventData(data)
}

func getEventFromUEventData(data []byte) map[string]string {
// syntax: name=value for each line
func getEventFromUEventData(data []byte) (map[string]string, error) {
rv := make(map[string]string)
buf := bufio.NewScanner(bytes.NewBuffer(data))
for buf.Scan() {
field := strings.SplitN(buf.Text(), "=", 2)
if len(field) != 2 {
return rv // TODO: return error ?
return rv, fmt.Errorf("cannot parse uevent data: did not find '=' in '%s'", buf.Text())
}
rv[field[0]] = field[1]
}
return rv
return rv, nil
}
4 changes: 0 additions & 4 deletions crawler/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ DEVNAME=vcs3`,
t.Fatalf("Test %d failed, unable to get event dfrom uevent file", k)
}

if !reflect.DeepEqual(evt, getEventFromUEventData([]byte(tcase.got))) {
t.Fatalf("Test %d failed, uevent from file or data must be equals", k)
}

if !reflect.DeepEqual(evt, tcase.expected) {
t.Fatalf("Test %d failed (got: %v, expected: %v)", k, evt, tcase.expected)
}
Expand Down
4 changes: 2 additions & 2 deletions netlink/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type UEventConn struct {
// Connect allow to connect to system socket AF_NETLINK with family NETLINK_KOBJECT_UEVENT to
// catch events about block/char device
// see:
// - http://elixir.free-electrons.com/linux/v3.12/source/include/uapi/linux/netlink.h#L23
// - http://elixir.free-electrons.com/linux/v3.12/source/include/uapi/linux/socket.h#L11
// - https://elixir.bootlin.com/linux/v6.14.1/source/include/uapi/linux/netlink.h#L23
// - https://elixir.bootlin.com/linux/v6.14.1/source/include/uapi/linux/socket.h#L11
func (c *UEventConn) Connect(mode Mode) (err error) {
if c.Fd, err = syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_RAW, syscall.NETLINK_KOBJECT_UEVENT); err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion netlink/uevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"unsafe"
)

// See: http://elixir.free-electrons.com/linux/v3.12/source/lib/kobject_uevent.c#L45
// See: https://elixir.bootlin.com/linux/v6.14.1/source/lib/kobject_uevent.c#L50

const (
ADD KObjAction = "add"
Expand Down