From 7af257cccf707925b6268a920a6ba6a7f81c12d7 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Mon, 27 Feb 2023 23:17:21 +0100 Subject: [PATCH] Migrate hotplug plugin to a conditional system/hotplug.conf file This is the first example of the just minted advanced stanza syntax with if-statements, conflict handling, and nowarn flags. Signed-off-by: Joachim Wiberg --- plugins/Makefile.am | 8 ---- plugins/hotplug.c | 113 -------------------------------------------- system/Makefile.am | 4 ++ system/hotplug.conf | 16 +++++++ 4 files changed, 20 insertions(+), 121 deletions(-) delete mode 100644 plugins/hotplug.c create mode 100644 system/hotplug.conf diff --git a/plugins/Makefile.am b/plugins/Makefile.am index a2b69c53..3c5bcda7 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -17,10 +17,6 @@ if BUILD_DBUS_PLUGIN libplug_la_SOURCES += dbus.c endif -if BUILD_HOTPLUG_PLUGIN -libplug_la_SOURCES += hotplug.c -endif - if BUILD_MODULES_LOAD_PLUGIN libplug_la_SOURCES += modules-load.c endif @@ -68,10 +64,6 @@ if BUILD_HOOK_SCRIPTS_PLUGIN pkglib_LTLIBRARIES += hook-scripts.la endif -if BUILD_HOTPLUG_PLUGIN -pkglib_LTLIBRARIES += hotplug.la -endif - if BUILD_MODULES_LOAD_PLUGIN pkglib_LTLIBRARIES += modules-load.la endif diff --git a/plugins/hotplug.c b/plugins/hotplug.c deleted file mode 100644 index 701c33bc..00000000 --- a/plugins/hotplug.c +++ /dev/null @@ -1,113 +0,0 @@ -/* Heuristically find and initialize a suitable hotplug daemon - * - * Copyright (c) 2012-2022 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; - - if (rescue) { - dbg("Skipping %s plugin in rescue mode.", __FILE__); - return; - } - - /* - * 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] cgroup.system pid:udevd name:udevd log %s " - "-- Device event managing daemon", path); - if (service_register(SVC_TYPE_SERVICE, cmd, global_rlimit, NULL)) { - err(1, "Failed registering %s", path); - } else { - snprintf(cmd, sizeof(cmd), "cgroup.init :1 [S] log " - "udevadm trigger -c add -t devices " - "-- Requesting device events"); - service_register(SVC_TYPE_RUN, cmd, global_rlimit, NULL); - - snprintf(cmd, sizeof(cmd), "cgroup.init :2 [S] log " - "udevadm trigger -c add -t subsystems " - "-- Requesting subsystem events"); - 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 (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_SVC_PLUGIN] = { .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: - */ diff --git a/system/Makefile.am b/system/Makefile.am index b9b82043..d99836c8 100644 --- a/system/Makefile.am +++ b/system/Makefile.am @@ -1,6 +1,10 @@ systemdir = $(system_path) dist_system_DATA = +if BUILD_HOTPLUG_PLUGIN +dist_system_DATA += hotplug.conf +endif + if BUILD_TESTSERV_PLUGIN dist_system_DATA += testserv.conf endif diff --git a/system/hotplug.conf b/system/hotplug.conf new file mode 100644 index 00000000..46e2117f --- /dev/null +++ b/system/hotplug.conf @@ -0,0 +1,16 @@ +# System hotplug can be handled by udev or mdev + +# Either we have systemd-udevd or eudev, on the rare occasion we find +# both, we opt for the latter. Remember, you can override this by a +# custom hotplug.conf in /etc/finit.d/ or /etc/finit.d/enabled/. +service nowarn [S12345789] cgroup.system name:udevd pid:udevd log /lib/systemd/systemd-udevd -- Device event managing daemon +service nowarn [S12345789] cgroup.system name:udevd pid:udevd log udevd -- Device event managing daemon + +# Wait for udevd to start, then wait a couple of seconds for it to become ready +run if:udevd cgroup.init [S] sleep 2 -- +run if:udevd cgroup.init :1 [S] log udevadm trigger -c add -t devices -- Requesting device events +run if:udevd cgroup.init :2 [S] log udevadm trigger -c add -t subsystems -- Requesting subsystem events +task if:udevd [S] /lib/udev/udev-finish -- Finalizing udev + +# If udevd is not available, try mdev +run conflict:udevd [S] mdev -s -- Populating device tree