]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sysctl: also process sysctl requests via the "sysctl.extra" credential
authorLennart Poettering <lennart@poettering.net>
Thu, 14 Jul 2022 11:41:37 +0000 (13:41 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 14 Jul 2022 16:02:58 +0000 (18:02 +0200)
man/systemd-sysctl.service.xml
src/sysctl/sysctl.c
test/TEST-54-CREDS/test.sh
test/units/testsuite-54.sh
units/systemd-sysctl.service.in

index 751aa2b09e07563579dd9e24156d37c51ed0ffe1..98533b451fdacc96d0d1e3531f10caeb67977e76 100644 (file)
     </variablelist>
   </refsect1>
 
+  <refsect1>
+    <title>Credentials</title>
+
+    <para><command>systemd-sysctl</command> supports the service credentials logic as implemented by
+    <varname>LoadCredential=</varname>/<varname>SetCredential=</varname> (see
+    <citerefentry><refentrytitle>systemd.exec</refentrytitle><manvolnum>1</manvolnum></citerefentry> for
+    details). The following credentials are used when passed in:</para>
+
+    <variablelist>
+      <varlistentry>
+        <term><literal>sysctl.extra</literal></term>
+
+        <listitem><para>The contents of this credential may contain additional lines to operate on. The
+        credential contents should follow the same format as any other <filename>sysctl.d/</filename>
+        drop-in. If this credential is passed it is processed after all of the drop-in files read from the
+        file system. The settings configured in the credential hence take precedence over those in the file
+        system.</para></listitem>
+      </varlistentry>
+    </variablelist>
+
+    <para>Note that by default the <filename>systemd-sysctl.service</filename> unit file is set up to inherit
+    the <literal>sysctl.extra</literal> credential from the service manager.</para>
+  </refsect1>
+
   <refsect1>
     <title>Examples</title>
 
index 2b854a73709b42aa4333947ced1d6b7ec40fda6b..e92640d9489e9fb840ef2d9bbc996ad9cb74d4ff 100644 (file)
@@ -10,6 +10,7 @@
 #include <sys/types.h>
 
 #include "conf-files.h"
+#include "creds-util.h"
 #include "def.h"
 #include "errno-util.h"
 #include "fd-util.h"
@@ -277,6 +278,25 @@ static int parse_file(OrderedHashmap **sysctl_options, const char *path, bool ig
         return r;
 }
 
+static int read_credential_lines(OrderedHashmap **sysctl_options) {
+        _cleanup_free_ char *j = NULL;
+        const char *d;
+        int r;
+
+        r = get_credentials_dir(&d);
+        if (r == -ENXIO)
+                return 0;
+        if (r < 0)
+                return log_error_errno(r, "Failed to get credentials directory: %m");
+
+        j = path_join(d, "sysctl.extra");
+        if (!j)
+                return log_oom();
+
+        (void) parse_file(sysctl_options, j, /* ignore_enoent= */ true);
+        return 0;
+}
+
 static int help(void) {
         _cleanup_free_ char *link = NULL;
         int r;
@@ -416,6 +436,10 @@ static int run(int argc, char *argv[]) {
                         if (k < 0 && r == 0)
                                 r = k;
                 }
+
+                k = read_credential_lines(&sysctl_options);
+                if (k < 0 && r == 0)
+                        r = k;
         }
 
         k = apply_all(sysctl_options);
index 3b2c24132326a83c94ccd23cf39bfbe088a9f670..8d5d796cc8b99828dd276046619e209565f69676 100755 (executable)
@@ -5,7 +5,7 @@ set -e
 TEST_DESCRIPTION="test credentials"
 NSPAWN_ARGUMENTS="${NSPAWN_ARGUMENTS:-} --set-credential=mynspawncredential:strangevalue"
 QEMU_OPTIONS="${QEMU_OPTIONS:-} -fw_cfg  name=opt/io.systemd.credentials/myqemucredential,string=othervalue"
-KERNEL_APPEND="${KERNEL_APPEND:-} systemd.set_credential=kernelcmdlinecred:uff rd.systemd.import_credentials=no"
+KERNEL_APPEND="${KERNEL_APPEND:-} systemd.set_credential=kernelcmdlinecred:uff systemd.set_credential=sysctl.extra:kernel.domainname=sysctltest rd.systemd.import_credentials=no"
 
 # shellcheck source=test/test-functions
 . "${TEST_BASE_DIR:?}/test-functions"
index c5347e351ec74326062324afbf96f92870a8c7ce..06f3beb287d6d3ab2d78b87879a134d0404f3d8c 100755 (executable)
@@ -33,6 +33,9 @@ elif [ -d /sys/firmware/qemu_fw_cfg/by_name ]; then
     systemd-detect-virt -q -v
     expected_credential=myqemucredential
     expected_value=othervalue
+
+    # Verify that writing a sysctl via the kernel cmdline worked
+    [ "$(cat /proc/sys/kernel/domainname)" = "sysctltest" ]
 else
     echo "qemu_fw_cfg support missing in kernel. Sniff!"
     expected_credential=""
index 44b885352142844ca84c39f84edf11b7d83b62dc..77793f38948fdfe015da08a73b7eb70227ec82c3 100644 (file)
@@ -21,3 +21,4 @@ Type=oneshot
 RemainAfterExit=yes
 ExecStart={{ROOTLIBEXECDIR}}/systemd-sysctl
 TimeoutSec=90s
+LoadCredential=sysctl.extra