diff options
| author | Ophestra <cat@gensokyo.uk> | 2026-03-27 22:37:36 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2026-03-27 22:39:43 +0900 |
| commit | ee22847dde5c02027a4f180f1f32a69adc3aaf70 (patch) | |
| tree | c1cdd5a0b546cfe2903e197c4658098960a67254 /internal/uevent/action.go | |
| parent | c61188649b757ba81c813ff2f7351a3d393cdce1 (diff) | |
internal/uevent: kobject_action lookup
This is encoded as part of kobject uevent message headers.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/uevent/action.go')
| -rw-r--r-- | internal/uevent/action.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/internal/uevent/action.go b/internal/uevent/action.go new file mode 100644 index 00000000..4f1f4f79 --- /dev/null +++ b/internal/uevent/action.go @@ -0,0 +1,74 @@ +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 +) + +// 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.Valid() { + return "unsupported kobject_action " + strconv.Itoa(int(act)) + } + return kobject_actions[act] +} + +func (act KobjectAction) AppendText(b []byte) ([]byte, error) { + if !act.Valid() { + 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 + +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) +} |
