package/busybox: add init script for sysctl
authorCarlos Santos <unixmania@gmail.com>
Wed, 1 May 2019 23:11:40 +0000 (20:11 -0300)
committerPeter Korsgaard <peter@korsgaard.com>
Sat, 3 Aug 2019 16:48:09 +0000 (18:48 +0200)
Add a simple init script that invokes sysctl early in the initialization
process to configure kernel parameters. This is already performed by
systemd (systemd-sysctl) but there is no sysvinit/busybox counterpart.

Files are read from directories in the following list in the given order
from top to bottom:

    /run/sysctl.d/*.conf
    /etc/sysctl.d/*.conf
    /usr/local/lib/sysctl.d/*.conf
    /usr/lib/sysctl.d/*.conf
    /lib/sysctl.d/*.conf
    /etc/sysctl.conf

A file may be used more than once, since there can be multiple symlinks
to it. No attempt is made to prevent this.

Signed-off-by: Carlos Santos <unixmania@gmail.com>
Reviewed-by: Matthew Weber <matthew.weber@rockwellcollins.com>
Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
package/busybox/S02sysctl [new file with mode: 0644]
package/busybox/busybox.mk

diff --git a/package/busybox/S02sysctl b/package/busybox/S02sysctl
new file mode 100644 (file)
index 0000000..6bb2fa1
--- /dev/null
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+PROGRAM="sysctl"
+
+SYSCTL_ARGS=""
+
+# shellcheck source=/dev/null
+[ -r "/etc/default/$PROGRAM" ] && . "/etc/default/$PROGRAM"
+
+# Files are read from directories in the SYSCTL_SOURCES list, in the given
+# order. A file may be used more than once, since there can be multiple
+# symlinks to it. No attempt is made to prevent this.
+SYSCTL_SOURCES="/etc/sysctl.d/ /usr/local/lib/sysctl.d/ /usr/lib/sysctl.d/ /lib/sysctl.d/ /etc/sysctl.conf"
+
+# Use some scripting to mimic the --system option of the sysctl provided by
+# procps-ng but still reporting errors. Users not interested on error report
+# can put "-e" in SYSCTL_ARGS.
+#
+# The file redirections do the following:
+#
+# - stdout is redirected to syslog with facility.level "kern.info"
+# - stderr is redirected to syslog with facility.level "kern.err"
+# - file dscriptor 4 is used to pass the result to the "start" function.
+#
+# Testing the sysctl exit code is fruitless, as at the moment, since it ends
+# with status zero even if errors happen. Hopefully this will be fixed in a
+# future version of Busybox.
+#
+run_program() {
+       # shellcheck disable=SC2086 # we need the word splitting
+       find $SYSCTL_SOURCES -maxdepth 1 -name '*.conf' -print0 2> /dev/null | \
+       xargs -0 -r -n 1 readlink -f | {
+               prog_status="OK"
+               while :; do
+                       read -r file
+                       if [ -z "$file" ]; then
+                               echo "$prog_status" >&4
+                               break
+                       fi
+                       echo "* Applying $file ..."
+                       /sbin/sysctl -p "$file" $SYSCTL_ARGS || prog_status="FAIL"
+               done 2>&1 >&3 | /usr/bin/logger -t sysctl -p kern.err
+       } 3>&1 | /usr/bin/logger -t sysctl -p kern.info
+}
+
+start() {
+       printf '%s %s: ' "$1" "$PROGRAM"
+       status=$(run_program 4>&1)
+       echo "$status"
+       if [ "$status" = "OK" ]; then
+               return 0
+       fi
+       return 1
+}
+
+case "$1" in
+       start)
+               start "Running";;
+       restart|reload)
+               start "Rerunning";;
+       stop)
+               :;;
+       *)
+               echo "Usage: $0 {start|stop|restart|reload}"
+               exit 1
+esac
index 7a5a37a05da06df4f48c98cb2f802e91087dea7e..a154584592c98b93cc9df039f761ef920b915e08 100644 (file)
@@ -260,6 +260,17 @@ define BUSYBOX_INSTALL_LOGGING_SCRIPT
 endef
 endif
 
+# Only install our sysctl scripts if no other package does it.
+ifeq ($(BR2_PACKAGE_PROCPS_NG),)
+define BUSYBOX_INSTALL_SYSCTL_SCRIPT
+       if grep -q CONFIG_BB_SYSCTL=y $(@D)/.config; \
+       then \
+               $(INSTALL) -m 0755 -D package/busybox/S02sysctl \
+                       $(TARGET_DIR)/etc/init.d/S02sysctl ; \
+       fi
+endef
+endif
+
 ifeq ($(BR2_INIT_BUSYBOX),y)
 define BUSYBOX_INSTALL_INITTAB
        $(INSTALL) -D -m 0644 package/busybox/inittab $(TARGET_DIR)/etc/inittab
@@ -341,6 +352,7 @@ define BUSYBOX_INSTALL_INIT_SYSV
        $(BUSYBOX_INSTALL_MDEV_SCRIPT)
        $(BUSYBOX_INSTALL_LOGGING_SCRIPT)
        $(BUSYBOX_INSTALL_WATCHDOG_SCRIPT)
+       $(BUSYBOX_INSTALL_SYSCTL_SCRIPT)
        $(BUSYBOX_INSTALL_TELNET_SCRIPT)
        $(BUSYBOX_INSTALL_INDIVIDUAL_BINARIES)
 endef