diff --git a/configure.ac b/configure.ac index 88d8dfbc..465f75d9 100644 --- a/configure.ac +++ b/configure.ac @@ -101,6 +101,7 @@ AC_ARG_ENABLE(contrib, enable_all_plugins=auto AC_PLUGIN([alsa-utils], [no], [Save and restore ALSA sound settings using alsactl]) AC_PLUGIN([dbus], [no], [Setup and start system message bus, D-Bus]) +AC_PLUGIN([hotplug], [yes], [Setup and start udev or mdev hotplug daemon]) AC_PLUGIN([inetd-echo], [no], [Inetd plugin: echo server, RFC862]) AC_PLUGIN([inetd-chargen], [no], [Inetd plugin: character generator, RFC864]) AC_PLUGIN([inetd-daytime], [no], [Inetd plugin: daytime server, RFC867]) diff --git a/doc/plugins.md b/doc/plugins.md index bdc96b4d..6a546a6a 100644 --- a/doc/plugins.md +++ b/doc/plugins.md @@ -42,6 +42,9 @@ For your convenience a set of *optional* plugins are available: * *discard.so*: RFC 863 plugin. Start as inetd service, like time below. +* *hotplug.so*: Setup and start either udev or mdev hotplug daemon, if + available. + * *rtc.so*: Restore and save system clock from/to RTC on boot/halt. * *initctl.so*: Extends finit with a traditional `initctl` functionality. diff --git a/plugins/Makefile.am b/plugins/Makefile.am index c26acf01..cb0022ee 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -17,6 +17,10 @@ if BUILD_DBUS_PLUGIN libplug_la_SOURCES += dbus.c endif +if BUILD_HOTPLUG_PLUGIN +libplug_la_SOURCES += hotplug.c +endif + if INETD if BUILD_INETD_ECHO_PLUGIN libplug_la_SOURCES += echo.c @@ -62,6 +66,10 @@ if BUILD_DBUS_PLUGIN pkglib_LTLIBRARIES += dbus.la endif +if BUILD_HOTPLUG_PLUGIN +pkglib_LTLIBRARIES += hotplug.la +endif + if INETD if BUILD_INETD_ECHO_PLUGIN pkglib_LTLIBRARIES += echo.la diff --git a/plugins/hotplug.c b/plugins/hotplug.c new file mode 100644 index 00000000..5ec91f67 --- /dev/null +++ b/plugins/hotplug.c @@ -0,0 +1,110 @@ +/* Heuristically find and initialize a suitable hotplug daemon + * + * Copyright (c) 2012-2020 Joachim Wiberg + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include + +#include "config.h" +#include "conf.h" +#include "finit.h" +#include "helpers.h" +#include "plugin.h" +#include "service.h" + +static void setup(void *arg) +{ + char cmd[256]; + char *path; + + /* + * Populate /dev and prepare for runtime events from kernel. + * Prefer udev if mdev is also available on the system. + */ + path = which("udevd"); + if (!path) + path = which("/lib/systemd/systemd-udevd"); + if (path) { + /* Register udevd as a monitored service */ + snprintf(cmd, sizeof(cmd), "[S12345789] pid:udevd %s " + "-- Device event managing daemon", path); + if (service_register(SVC_TYPE_SERVICE, cmd, global_rlimit, NULL)) { + _pe("Failed registering %s", path); + } else { + snprintf(cmd, sizeof(cmd), ":1 [S] " + "udevadm trigger -c add -t devices " + "-- Requesting device events", path); + service_register(SVC_TYPE_RUN, cmd, global_rlimit, NULL); + + snprintf(cmd, sizeof(cmd), ":2 [S] " + "udevadm trigger -c add -t subsystems " + "-- Requesting subsystem events", path); + service_register(SVC_TYPE_RUN, cmd, global_rlimit, NULL); + } + + free(path); + + /* Debian has this little script to copy generated + * rules while the system was read-only. TODO: When + * this functionality was hardcoded in finit.c, this + * call was made in crank_worker(). Now that + * filesystems are mounted earlier we should be able + * to make this call directly after the triggers have + * run, but this has not been tested AT ALL. */ + if (fexist("/lib/udev/udev-finish")) + run_interactive("/lib/udev/udev-finish", "Finalizing udev"); + } else { + path = which("mdev"); + if (path) { + /* Embedded Linux systems usually have BusyBox mdev */ + if (log_is_debug()) + touch("/dev/mdev.log"); + + snprintf(cmd, sizeof(cmd), "%s -s", path); + free(path); + + run_interactive(cmd, "Populating device tree"); + } + } +} + +static plugin_t plugin = { + .name = __FILE__, + .hook[HOOK_BASEFS_UP] = { + .cb = setup + }, +}; + +PLUGIN_INIT(plugin_init) +{ + plugin_register(&plugin); +} + +PLUGIN_EXIT(plugin_exit) +{ + plugin_unregister(&plugin); +} + +/** + * Local Variables: + * indent-tabs-mode: t + * c-file-style: "linux" + * End: + */