Skip to content

Exporting state and events

ebenali edited this page Dec 4, 2026 · 3 revisions

Exporting state and events: sysctl, drvctl, sysfs, uevents

Part III of the system development guide.

Drivers tell user space about themselves in two ways: state it can read or set (settings, statistics, identity), and events it can react to (a device appeared, a monitor was plugged in). NetBSD and Linux built very different machinery for both.

NetBSD

State: sysctl(9). A tree of named nodes (hw., kern., net., ...), each an integer, string, boolean or structure, readable and optionally writable with sysctl(8). A driver creates its nodes at attach with sysctl_createv() (keeping a log to tear them down) and destroys them at detach (sysctl_teardown()). Per-device tunables usually live under hw.<device>.

Names programs parse. Some sysctl strings are read by programs, not just people, and their format is an interface. hw.model is the machine's name for the CPU: system-information tools and libraries (fastfetch, neofetch, libuv's uv_cpu_info) read it or machdep.cpu_brand, and FreeBSD, OpenBSD and the other NetBSD ports put the CPU's name there. On x86 the CPU reports its own name, the 48-byte brand string (CPUID leaves 0x80000002–0x80000004, padded with spaces), which identcpu.c keeps, trimmed at both ends, as machdep.cpu_brand. On amd64, hw.model is that brand string (or the CPU class when the CPU has no brand string). On i386 it stays "<vendor> <n>86-class", because programs parse the class from it there (OpenSSL's Configure picks its i<n>86 target from it). A test (tests/sys/x86/t_hw_model) holds both rules and the trimming.

Device properties. A proplib dictionary per device (device_properties()), mostly for passing data between kernel parts (firmware tables to drivers, a framebuffer's geometry to the console), also readable from user space through drvctl(8).

Events: drvctl(4) and devpubd(8). Autoconf reports every attach and detach (devmon_report_device() in subr_autoconf.c); a driver reports other changes of a device with devmon_report_change(dev, props) (aiBSD), event device-change with keys of its own saying what changed. devmon_insert() in kern_drvctl.c queues each event, a proplib dictionary with event, device, parent and maybe location, for readers of /dev/drvctl (DRVGETEVENT). devpubd(8) reads them and runs the hook scripts in /libexec/devpubd-hooks with the event and device name as arguments (for device-change, the event's upper-case string keys also as DEVPUBD_<key> environment variables) — NetBSD's counterpart of udev, used to make device nodes and permissions or start services.

Several listeners. Upstream keeps one queue and a read takes the event for everyone, so two listeners (devpubd and, say, a compositor through libudev) would each see only part of the events. aiBSD gives each open of /dev/drvctl its own queue (struct drvctl_listener, the open file's f_data): devmon_insert() puts a reference to the same dictionary on every queue, and the dictionaries are not changed once queued, so sharing them is safe. A queue holds 64 events and drops the oldest; a listener receives events from the time it opened. Every open counts, including short ones like drvctl -l; their queues go away on close.

Sensors and switches: sysmon. Environmental sensors (sysmon_envsys, read with envstat(8)), power buttons, lid switches, AC adapters (sysmon_pswitch, types PSWITCH_TYPE_POWER, _SLEEP, _LID, _ACADAPTER, ... in <sys/power.h>) delivered to powerd(8) and its scripts; watchdogs.

Linux

State: sysfs. Every kobject is a directory under /sys (/sys/devices/pci0000:00/.../drm/card0, with classes linking to them from /sys/class/drm/), and every attribute a file whose show and store callbacks format and parse text: DEVICE_ATTR_RO(name) defines one, attribute groups add several at once, sysfs_emit() formats into the page-sized buffer. GPU drivers export hundreds (clocks, power profiles, VRAM usage, firmware versions). Module parameters appear under /sys/module/<module>/parameters/.

Events: uevents. kobject_uevent_env(kobj, KOBJ_CHANGE, envp) sends an event with key=value pairs over netlink; udev applies rules (device nodes, permissions, symlinks, running programs). DRM sends a change event with HOTPLUG=1 on the card when connectors change, which compositors listen for to reconfigure displays.

aiBSD

  • kobjects without sysfs: kobjects exist for their lifetime rules only (see devices); creating attributes, groups and links succeeds and does nothing, so drivers' sysfs code compiles and runs harmlessly. sysfs_emit() works, into page-aligned show buffers only, as on Linux.

  • uevents in general go nowhere: kobject_uevent*() return success.

  • DRM's output-change uevents become drvctl events. Linux sends a change uevent on the card with HOTPLUG=1 (plus CONNECTOR=<id>, and PROPERTY=<id> for a property change) or LEASE=1; compositors watch for exactly these keys through libudev. aiBSD's drm/drm_sysfs.c (drm_sysfs_hotplug_event() and friends) sends them as device-change on the DRM driver's device, with the same keys and string values and DEVNAME=dri/card<n> naming the node:

    device-change bochsdrm0 HOTPLUG=1 CONNECTOR=39 DEVNAME=dri/card1
    

    For compositors to see them, a libudev for NetBSD (libudev-bsd in pkgsrc reads drvctl) must map device-change to udev's change action and pass the keys through as properties; it knows only attach and detach so far.

  • Module parameters appear as sysctl nodes under hw.drm2 (drm_sysctl.c), created from each module's parameter link set.

  • What Linux exposes in sysfs that users need (power profiles, backlight) needs a sysctl or ioctl route chosen case by case.

Try it

  • sysctl hw.drm2; drvctl -l; run devpubd -f and plug in a USB device to watch events; envstat for sensors.
  • A hook that logs display changes: an executable /libexec/devpubd-hooks/50-display with [ "$1" = device-change ] && [ -n "$DEVPUBD_HOTPLUG" ] && logger "display change on $2 ($DEVPUBD_DEVNAME)".

Further reading

sysctl(8), sysctl(9), drvctl(4), drvctl(8), devpubd(8), envstat(8), powerd(8), proplib(3); sys/kern/kern_drvctl.c, sys/kern/subr_autoconf.c, sys/dev/sysmon/; sys/external/bsd/drm2/drm/drm_sysctl.c.

Clone this wiki locally