From d6f35903d6b10a477ae25589b739c62de0977b02 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Tue, 16 Aug 2022 13:14:08 +0200 Subject: [PATCH] Fix #195: add mdevd plugin as complement to hotplug.sh This patch adds the mdevd[1] plugin from Andy's fork of Finit, only minor whitespace adjustment and group fix introduced, along with updates to configure.ac and the documentation. Currently the hotplug.sh calls mdevd-coldplug (instad of mdev -s), but since mdevd is not guaranteed to have started and be ready, it is also started with mdevd -C. A better solution is to use the s6 notify code to synchronize the start of mdevd-coldplug. Support for this is coming soon to Finit. [1]: https://github.com/hongkongkiwi/finit/blob/master/plugins/mdevd.c Signed-off-by: Joachim Wiberg --- configure.ac | 1 + doc/plugins.md | 3 ++ plugins/Makefile.am | 8 +++ plugins/hotplug.c | 16 +++++- plugins/mdevd.c | 123 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 plugins/mdevd.c diff --git a/configure.ac b/configure.ac index 3f38b832..2d8424cf 100644 --- a/configure.ac +++ b/configure.ac @@ -85,6 +85,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([mdevd], [no], [Setup and start system hotplug helper mdevd]) AC_PLUGIN([modules-load], [no], [Scans /etc/modules-load.d for modules to load]) AC_PLUGIN([modprobe], [yes], [Coldplug modules using modalias magic]) AC_PLUGIN([resolvconf], [no], [Setup necessary files for resolvconf]) diff --git a/doc/plugins.md b/doc/plugins.md index 641e4240..5dd00b24 100644 --- a/doc/plugins.md +++ b/doc/plugins.md @@ -43,6 +43,9 @@ For your convenience a set of *optional* plugins are available: * *hotplug.so*: Setup and start either udev or mdev hotplug daemon, if available. Enabled by default. +* *mdevd.so*: Set up and start mdevd (mdev replacement). Enabling this + also changes how `hotplug.so` plugin operates. _Optional plugin._ + * *rtc.so*: Restore and save system clock from/to RTC on boot/halt. Enabled by default. diff --git a/plugins/Makefile.am b/plugins/Makefile.am index b9b6b1d2..d4b878c5 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -21,6 +21,10 @@ if BUILD_HOTPLUG_PLUGIN libplug_la_SOURCES += hotplug.c endif +if BUILD_MDEVD_PLUGIN +libplug_la_SOURCES += mdevd.c +endif + if BUILD_MODULES_LOAD_PLUGIN libplug_la_SOURCES += modules-load.c endif @@ -72,6 +76,10 @@ if BUILD_HOTPLUG_PLUGIN pkglib_LTLIBRARIES += hotplug.la endif +if BUILD_MDEVD_PLUGIN +pkglib_LTLIBRARIES += mdevd.la +endif + if BUILD_MODULES_LOAD_PLUGIN pkglib_LTLIBRARIES += modules-load.la endif diff --git a/plugins/hotplug.c b/plugins/hotplug.c index 9c157510..6b998240 100644 --- a/plugins/hotplug.c +++ b/plugins/hotplug.c @@ -39,6 +39,19 @@ static void setup(void *arg) return; } + /* + * If the mdevd plugin is enabled and loaded we assume this + * system is mdevd based, skipping below udev/mdev checks. + */ + if (plugin_find("mdevd")) { + path = which("mdevd-coldplug"); + if (path) { + run_interactive(path, "Populating device tree"); + free(path); + } + return; + } + /* * Populate /dev and prepare for runtime events from kernel. * Prefer udev if mdev is also available on the system. @@ -90,12 +103,13 @@ static void setup(void *arg) } } +/* mdevd optional, if installed & loaded changes behavior of setup() */ static plugin_t plugin = { .name = __FILE__, .hook[HOOK_BASEFS_UP] = { .cb = setup }, - .depends = { "bootmisc", "modprobe" }, + .depends = { "bootmisc", "mdevd", "modprobe" }, }; PLUGIN_INIT(plugin_init) diff --git a/plugins/mdevd.c b/plugins/mdevd.c new file mode 100644 index 00000000..1c1430a3 --- /dev/null +++ b/plugins/mdevd.c @@ -0,0 +1,123 @@ +/* Setup and start system hotplug helper mdevd + * + * Portions Copyright (c) 2012-2022 Joachim Wiberg + * Portions Copyright (c) 2022 Andy Savage + * + * 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 +#ifdef _LIBITE_LITE +# include +#else +# include +#endif + +#include "finit.h" +#include "config.h" +#include "helpers.h" +#include "plugin.h" +#include "service.h" +#include "conf.h" + +#define MDEVD_DAEMON "mdevd" +/* Rebroadcast events onto nlgroup 4 */ +#define MDEVD_ARGS "-C -O 4" +#define MDEVD_DESC "MDEVD Extended Hotplug Daemon" + +#ifndef MDEVD_DAEMONUSER +#define MDEVD_DAEMONUSER "root" +#endif + +#ifndef MDEVD_DAEMONGROUP +#define MDEVD_DAEMONGROUP "root" +#endif + +#ifndef MDEVD_DAEMONPIDFILE +#define MDEVD_DAEMONPIDFILE "/var/run/mdevd.pid" +#endif + +static void setup(void *arg) +{ + char line[256]; + mode_t prev; + char *cmd; + FILE *fp; + + if (rescue) { + _d("Skipping %s plugin in rescue mode.", __FILE__); + return; + } + + cmd = which(MDEVD_DAEMON); + if (!cmd) { + _d("Skipping plugin, %s is not installed.", MDEVD_DAEMON); + return; + } + + prev =umask(0); + + /* Clean up from any previous pre-bootstrap run */ + remove(MDEVD_DAEMONPIDFILE); + + /* Set hotplug helper */ + fp = fopen("/proc/sys/kernel/hotplug", "w"); + if (fp) { + fputc('\n', fp); + fclose(fp); + } + + /* Register service with Finit */ + if (debug) + snprintf(line, sizeof(line), "[S12345789] cgroup.system pid:!%s @%s:%s %s -v3 %s -- %s", + MDEVD_DAEMONPIDFILE, MDEVD_DAEMONUSER, MDEVD_DAEMONGROUP, cmd, MDEVD_ARGS, MDEVD_DESC); + else + snprintf(line, sizeof(line), "[S12345789] cgroup.system pid:!%s @%s:%s %s %s -- %s", + MDEVD_DAEMONPIDFILE, MDEVD_DAEMONUSER, MDEVD_DAEMONGROUP, cmd, MDEVD_ARGS, MDEVD_DESC); + if (service_register(SVC_TYPE_SERVICE, line, global_rlimit, NULL)) + _pe("Failed registering %s", MDEVD_DAEMON); + free(cmd); + + umask(prev); +} + +static plugin_t plugin = { + .name = __FILE__, + .hook[HOOK_BASEFS_UP] = { + .cb = setup + }, + .depends = { "bootmisc", }, +}; + +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: + */