[Toybox] notes on mdev (avoiding code duplication in hotplug/-s/-i)

Isaac Dunham ibid.ag at gmail.com
Sat Mar 21 12:12:29 PDT 2015


This is more-or-less notes for Rob or whoever cleans up mdev; if
you're not working on it, feel free to just file this somewhere.

One of the more-often requested features for busybox mdev is a pipe mode,
wherein something writes events to a pipe and mdev reads from the pipe,
treating a given record as a hotplug event.
The theory is that you can have a netlink listener write to this, creating
a netlink-based hotplug daemon.
There are already at least two of these netlink listeners:
nldev ( git.r-36.net/nldev/ ) and s6-devd, which I believe you mention
as "not sure what this is" in your roadmap.

I've been working on adding this pipe mode (mdev -i) to busybox, and would
like to provide some details for doing so and avoiding code duplication.
In essence, the approach I used:
- (Important preliminary detail: the parser state should be global)
- have a function equivalent to the hotplug code;
- in that function, replace "getenv(...)" with a generic function that
returns a pointer to a value that follows an '=' sign (from the
environment, or from a buffer, whether created from uevent or read from
a pipe);
- on hotplug ("mdev" or "mdev usb"), call that function once.
- on mdev -s (change not yet made): scan /sys for a uevent file, read it,
replace \n with \0, and call the hotplug function with that as the buffer
- on mdev -i, read a message from the pipe, and call the hotplug function
with that as the buffer

At the request of the developer of s6-devd, the events read from the
pipe on mdev -i are expected to follow this general format:

KEY1=VALUE1\0KEY2=VALUE2\0KEY3=VALUE3\0\0

(in other words, uevent with \0 for \n and \0 at the end of file)


By coincidence, this means that "mdev -s" is equivalent to this script:

cat <<=== >catuevent.sh
#!/bin/sh
while test -n "$@"
  do
    printf 'ACTION=add\nDEVPATH='
    echo "$1" |sed -e 's:^/sys::;s:/uevent$::'
    cat "$1"
    printf '\n'
    shift
  done
===
chmod +x catuevent.sh

find /sys -name uevent -exec ./catuevent.sh '{}' + |tr '\n' '\0' | mdev -i

You could probably use add the buffer and buffer length to GLOBALS();
this will greatly simplify replacing getenv, and avoid a lot of bloat.

HTH,
Isaac Dunham

 1426965149.0


More information about the Toybox mailing list