From 33fd6f004969b2f3a3756c9b26186f5ab532e411 Mon Sep 17 00:00:00 2001 From: Robert Andersson Date: Thu, 16 Aug 2018 09:28:57 +0200 Subject: [PATCH] Add modules-load.d plugin This plugin scans /etc/modules-load.d for modules to modprobe. Signed-off-by: Robert Andersson --- configure.ac | 1 + docs/plugins.md | 2 + plugins/Makefile.am | 8 ++++ plugins/modules-load.c | 105 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 plugins/modules-load.c diff --git a/configure.ac b/configure.ac index 3896a00f..bae45ceb 100644 --- a/configure.ac +++ b/configure.ac @@ -95,6 +95,7 @@ AC_PLUGIN([inetd-chargen], [no], [Inetd plugin: character generator, RFC864]) AC_PLUGIN([inetd-daytime], [no], [Inetd plugin: daytime server, RFC867]) AC_PLUGIN([inetd-discard], [no], [Inetd plugin: discard server, RFC863]) AC_PLUGIN([inetd-time], [no], [Inetd plugin: time (rdate) server, RFC868]) +AC_PLUGIN([modules-load], [no], [Scans /etc/modules-load.d for modules to load]) AC_PLUGIN([resolvconf], [no], [Setup necessary files for resolvconf]) AC_PLUGIN([x11-common], [no], [Console setup (for X)]) AC_PLUGIN([netlink], [yes], [Basic netlink plugin for IFUP/IFDN and GW events. Can be replaced with externally built plugin that links with libnl or similar.]) diff --git a/docs/plugins.md b/docs/plugins.md index 8c2d8eb8..3fc6b438 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -47,6 +47,8 @@ For your convenience a set of *optional* plugins are available: * *initctl.so*: Extends finit with a traditional `initctl` functionality. +* *modules-load.so*: Scans /etc/modules-load.d for modules to modprobe. + * *netlink.so*: Listens to Linux kernel Netlink events for gateway and interfaces. These events are then sent to the Finit service monitor for services that may want to be SIGHUP'ed on new default route or diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 17a67cae..73d7c7c9 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -39,6 +39,10 @@ libplug_la_SOURCES += time.c endif endif +if BUILD_MODULES_LOAD_PLUGIN +libplug_la_SOURCES += modules-load.c +endif + if BUILD_NETLINK_PLUGIN libplug_la_SOURCES += netlink.c endif @@ -80,6 +84,10 @@ pkglib_LTLIBRARIES += time.la endif endif +if BUILD_MODULES_LOAD_PLUGIN +pkglib_LTLIBRARIES += modules-load.la +endif + if BUILD_NETLINK_PLUGIN pkglib_LTLIBRARIES += netlink.la endif diff --git a/plugins/modules-load.c b/plugins/modules-load.c new file mode 100644 index 00000000..9e03201d --- /dev/null +++ b/plugins/modules-load.c @@ -0,0 +1,105 @@ +/* Scans /etc/modules-load.d for modules to load. + * + * Copyright (c) 2018 Robert Andersson + * + * 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 +#include +#include + +#include "finit.h" +#include "helpers.h" +#include "plugin.h" +#include "service.h" +#include "conf.h" + +#define MODULES_LOAD_PATH "/etc/modules-load.d" +#define SERVICE_LINE ":%d [2345] /sbin/modprobe %s %s -- Kernel module: %s" + +static void load(void *arg) +{ + FILE *fp; + DIR *dirp; + struct dirent *direntp; + char line[256]; + int index = 1; + + _d("Scanning " MODULES_LOAD_PATH " for config files ..."); + + dirp = opendir(MODULES_LOAD_PATH); + if (!dirp) return; + + while ((direntp = readdir(dirp))) + { + char module_path[PATH_MAX]; + strlcpy(module_path, MODULES_LOAD_PATH "/", sizeof(module_path)); + strlcat(module_path, direntp->d_name, sizeof(module_path)); + + fp = fopen(module_path, "r"); + if (!fp) continue; + + while (fgets(line, sizeof(line), fp)) { + char cmd[CMD_SIZE], *mod, *args; + + mod = strip_line(line); + if (!*mod) continue; + + mod = chomp(mod); + if (!mod || !*mod) continue; + + mod = strtok_r(mod, " ", &args); + + snprintf(cmd, sizeof(cmd), SERVICE_LINE, index, mod, args, mod); + service_register(SVC_TYPE_TASK, cmd, global_rlimit, NULL); + + index++; + } + + fclose(fp); + } + + closedir(dirp); +} + +static plugin_t plugin = { + .name = __FILE__, + .hook[HOOK_BASEFS_UP] = { + .cb = load + }, +}; + +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: + */