Files
hakurei/internal/uevent/action.go
Ophestra 121fcfa406
All checks were successful
Test / Create distribution (push) Successful in 1m15s
Test / Sandbox (push) Successful in 3m8s
Test / Hakurei (push) Successful in 4m17s
Test / ShareFS (push) Successful in 4m20s
Test / Sandbox (race detector) (push) Successful in 5m34s
Test / Hakurei (race detector) (push) Successful in 6m40s
Test / Flake checks (push) Successful in 1m26s
internal/uevent: enumerate objects via sysfs
This is not a great way to implement cold boot, but I already have the implementation lying around.

Signed-off-by: Ophestra <cat@gensokyo.uk>
2026-03-29 19:50:20 +09:00

86 lines
2.0 KiB
Go

package uevent
import (
"strconv"
"syscall"
)
// KobjectAction represents enum kobject_action found in include/linux/kobject.h
// and their corresponding string representations in lib/kobject_uevent.c.
type KobjectAction uint32
// include/linux/kobject.h
const (
KOBJ_ADD KobjectAction = iota
KOBJ_REMOVE
KOBJ_CHANGE
KOBJ_MOVE
KOBJ_ONLINE
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
var kobject_actions = [...]string{
KOBJ_ADD: "add",
KOBJ_REMOVE: "remove",
KOBJ_CHANGE: "change",
KOBJ_MOVE: "move",
KOBJ_ONLINE: "online",
KOBJ_OFFLINE: "offline",
KOBJ_BIND: "bind",
KOBJ_UNBIND: "unbind",
}
// Valid returns whether the value of act is defined.
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))
}
return kobject_actions[act]
}
func (act KobjectAction) AppendText(b []byte) ([]byte, error) {
if !act.Valid() && act != Synthetic {
return b, syscall.EINVAL
}
return append(b, act.String()...), nil
}
func (act KobjectAction) MarshalText() ([]byte, error) {
return act.AppendText(nil)
}
// An UnsupportedActionError describes a string representation of [KobjectAction]
// not yet supported by this package.
type UnsupportedActionError string
var _ Recoverable = UnsupportedActionError("")
func (UnsupportedActionError) recoverable() {}
func (e UnsupportedActionError) Error() string {
return "unsupported kobject_action " + strconv.Quote(string(e))
}
func (act *KobjectAction) UnmarshalText(data []byte) error {
for v, s := range kobject_actions {
if string(data) == s {
*act = KobjectAction(v)
return nil
}
}
return UnsupportedActionError(data)
}