1 Commits

Author SHA1 Message Date
648079f42c internal/netlink: switch to recvmsg/sendmsg
Some checks are pending
Test / Hakurei (race detector) (push) Waiting to run
Test / Flake checks (push) Blocked by required conditions
Test / Create distribution (push) Successful in 1m15s
Test / Sandbox (push) Successful in 3m6s
Test / Hakurei (push) Successful in 4m13s
Test / ShareFS (push) Successful in 4m20s
Test / Sandbox (race detector) (push) Successful in 5m35s
These are more flexible than recvfrom/sendto.

Signed-off-by: Ophestra <cat@gensokyo.uk>
2026-03-29 23:36:00 +09:00
7 changed files with 16 additions and 156 deletions

View File

@@ -101,13 +101,13 @@ func (c *Conn) Close() error {
return c.f.Close()
}
// Recvfrom wraps recv(2) with nonblocking behaviour via the runtime network poller.
// Recvmsg wraps recv(2) with nonblocking behaviour via the runtime network poller.
//
// The returned slice is valid until the next call to Recvfrom.
func (c *Conn) Recvfrom(
// The returned slice is valid until the next call to Recvmsg.
func (c *Conn) Recvmsg(
ctx context.Context,
flags int,
) (data []byte, from syscall.Sockaddr, err error) {
) (data []byte, recvflags int, from syscall.Sockaddr, err error) {
if err = c.f.SetReadDeadline(time.Time{}); err != nil {
return
}
@@ -117,7 +117,7 @@ func (c *Conn) Recvfrom(
done := make(chan error, 1)
go func() {
rcErr := c.raw.Read(func(fd uintptr) (done bool) {
n, from, err = syscall.Recvfrom(int(fd), data, flags)
n, _, recvflags, from, err = syscall.Recvmsg(int(fd), data, nil, flags)
return err != syscall.EWOULDBLOCK
})
if n >= 0 {
@@ -129,7 +129,7 @@ func (c *Conn) Recvfrom(
select {
case rcErr := <-done:
if err != nil {
err = os.NewSyscallError("recvfrom", err)
err = os.NewSyscallError("recvmsg", err)
} else {
err = rcErr
}
@@ -147,12 +147,12 @@ func (c *Conn) Recvfrom(
}
}
// Sendto wraps send(2) with nonblocking behaviour via the runtime network poller.
func (c *Conn) Sendto(
// Sendmsg wraps send(2) with nonblocking behaviour via the runtime network poller.
func (c *Conn) Sendmsg(
ctx context.Context,
p []byte,
flags int,
to syscall.Sockaddr,
flags int,
) (err error) {
if err = c.f.SetWriteDeadline(time.Time{}); err != nil {
return
@@ -161,7 +161,7 @@ func (c *Conn) Sendto(
done := make(chan error, 1)
go func() {
done <- c.raw.Write(func(fd uintptr) (done bool) {
err = syscall.Sendto(int(fd), p, flags, to)
err = syscall.Sendmsg(int(fd), p, nil, to, flags)
return err != syscall.EWOULDBLOCK
})
}()
@@ -169,7 +169,7 @@ func (c *Conn) Sendto(
select {
case rcErr := <-done:
if err != nil {
err = os.NewSyscallError("sendto", err)
err = os.NewSyscallError("sendmsg", err)
} else {
err = rcErr
}
@@ -278,7 +278,7 @@ type HandlerFunc func(resp []syscall.NetlinkMessage) error
func (c *Conn) receive(ctx context.Context, f HandlerFunc, flags int) error {
for {
var resp []syscall.NetlinkMessage
if data, _, err := c.Recvfrom(ctx, flags); err != nil {
if data, _, _, err := c.Recvmsg(ctx, flags); err != nil {
return err
} else if len(data) < syscall.NLMSG_HDRLEN {
return syscall.EBADE
@@ -302,9 +302,9 @@ func (c *Conn) Roundtrip(ctx context.Context, f HandlerFunc) error {
}
defer func() { c.seq++ }()
if err := c.Sendto(ctx, c.pending(), 0, &syscall.SockaddrNetlink{
if err := c.Sendmsg(ctx, c.pending(), &syscall.SockaddrNetlink{
Family: syscall.AF_NETLINK,
}); err != nil {
}, 0); err != nil {
return err
}

View File

@@ -19,10 +19,6 @@ const (
KOBJ_OFFLINE
KOBJ_BIND
KOBJ_UNBIND
// Synthetic denotes a [Message] that originates from outside the kernel. It
// is not valid in the wire format and is only meaningful within this package.
Synthetic KobjectAction = 0xfeed
)
// lib/kobject_uevent.c
@@ -42,10 +38,6 @@ func (act KobjectAction) Valid() bool { return int(act) < len(kobject_actions) }
// String returns the corresponding string sent over netlink.
func (act KobjectAction) String() string {
if act == Synthetic {
return "synthetic"
}
if !act.Valid() {
return "unsupported kobject_action " + strconv.Itoa(int(act))
}
@@ -53,7 +45,7 @@ func (act KobjectAction) String() string {
}
func (act KobjectAction) AppendText(b []byte) ([]byte, error) {
if !act.Valid() && act != Synthetic {
if !act.Valid() {
return b, syscall.EINVAL
}
return append(b, act.String()...), nil

View File

@@ -29,15 +29,4 @@ func TestKobjectAction(t *testing.T) {
t.Errorf("String: %q, want %q", got, want)
}
})
adeT(t, "synthetic", uevent.Synthetic, "synthetic",
uevent.UnsupportedActionError("synthetic"), nil)
t.Run("validate synthetic", func(t *testing.T) {
t.Parallel()
if uevent.Synthetic.Valid() {
t.Errorf("Valid unexpectedly succeeded")
}
})
}

View File

@@ -110,12 +110,6 @@ SEQNUM=780`},
}, "move", uevent.MissingHeaderError(
"move",
), syscall.EINVAL, "unsupported kobject_action 2989 event:"},
{"synthetic", uevent.Message{
Action: uevent.Synthetic,
}, "synthetic@\x00", uevent.UnsupportedActionError(
"synthetic",
), nil, "synthetic event:"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

View File

@@ -1,87 +0,0 @@
package uevent
import (
"bytes"
"errors"
"io/fs"
"log"
"path/filepath"
"unsafe"
)
// Enumerate scans sysfs and emits [Synthetic] events. It returns the first
// error it encounters.
//
// The specified filesystem must present the sysfs root.
func Enumerate(
sysfs fs.FS,
handleWalkErr func(error) error,
events chan<- *Message,
) error {
if handleWalkErr == nil {
handleWalkErr = func(err error) error {
if errors.Is(err, fs.ErrNotExist) {
log.Println("enumerate", err)
return nil
}
return err
}
}
return fs.WalkDir(sysfs, "devices", func(
path string,
d fs.DirEntry,
err error,
) error {
if err != nil {
return handleWalkErr(err)
}
if d.IsDir() || d.Name() != "uevent" {
return nil
}
msg := Message{
Action: Synthetic,
// cleans path, appears to be compatible with kernel behaviour
DevPath: filepath.Dir(path),
}
var target string
if target, err = fs.ReadLink(
sysfs,
filepath.Join(msg.DevPath, "subsystem"),
); err != nil {
if err = handleWalkErr(err); err != nil {
return err
}
} else {
msg.Env = append(msg.Env, "SUBSYSTEM="+filepath.Base(target))
}
// read entire file: slicing does not copy
var env []byte
if env, err = fs.ReadFile(sysfs, path); err != nil {
return handleWalkErr(err)
}
for _, s := range bytes.Split(env, []byte{'\n'}) {
if len(s) == 0 {
continue
}
msg.Env = append(msg.Env, unsafe.String(unsafe.SliceData(s), len(s)))
}
if len(msg.Env) == 0 {
// this implies absent subsystem, its error is already handled
return nil
}
if msg.DevPath != "" && msg.DevPath[0] != '/' {
msg.DevPath = "/" + msg.DevPath
}
events <- &msg
return nil
})
}

View File

@@ -1,28 +0,0 @@
package uevent_test
import (
"os"
"sync"
"testing"
"hakurei.app/internal/uevent"
)
func TestEnumerate(t *testing.T) {
t.Parallel()
var wg sync.WaitGroup
defer wg.Wait()
events := make(chan *uevent.Message, 1<<10)
wg.Go(func() {
for msg := range events {
t.Log(msg)
}
})
if err := uevent.Enumerate(os.DirFS("/sys"), nil, events); err != nil {
t.Fatalf("Enumerate: error = %v", err)
}
close(events)
}

View File

@@ -82,7 +82,7 @@ func (c *Conn) Consume(ctx context.Context, events chan<- *Message) error {
defer c.exitExcl()
for {
data, from, err := c.conn.Recvfrom(ctx, 0)
data, _, from, err := c.conn.Recvmsg(ctx, 0)
if err != nil {
return err
}