sysctl: introduce --save= and --revert= options (#43325)

This commit is contained in:
Yu Watanabe
2026-08-21 04:57:57 +09:00
committed by GitHub
3 changed files with 331 additions and 8 deletions
+152 -6
View File
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <sys/stat.h>
#include <unistd.h>
#include "sd-json.h"
@@ -10,16 +11,20 @@
#include "constants.h"
#include "creds-util.h"
#include "errno-util.h"
#include "fs-util.h"
#include "glob-util.h"
#include "hashmap.h"
#include "log.h"
#include "main-func.h"
#include "mkdir.h"
#include "options.h"
#include "pager.h"
#include "path-util.h"
#include "pretty-print.h"
#include "string-util.h"
#include "strv.h"
#include "sysctl-util.h"
#include "tmpfile-util.h"
#include "verbs.h"
static char **arg_prefixes = NULL;
@@ -28,8 +33,12 @@ static bool arg_strict = false;
static bool arg_verify = false;
static bool arg_inline = false;
static PagerFlags arg_pager_flags = 0;
static char *arg_save_file = NULL;
static char *arg_revert_file = NULL;
STATIC_DESTRUCTOR_REGISTER(arg_prefixes, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_save_file, freep);
STATIC_DESTRUCTOR_REGISTER(arg_revert_file, freep);
COMMAND(
"systemd-sysctl\0",
@@ -226,6 +235,13 @@ static int apply_all(OrderedHashmap *sysctl_options) {
return r;
}
static void filter_sysctl_options(OrderedHashmap *sysctl_options) {
SysctlOption *option;
ORDERED_HASHMAP_FOREACH(option, sysctl_options)
if (!string_is_glob(option->key) && !test_prefix(option->key))
sysctl_option_free(ordered_hashmap_remove(sysctl_options, option->key));
}
static int parse_line(const char *fname, unsigned line, const char *buffer, bool *invalid_config, void *userdata) {
OrderedHashmap **sysctl_options = ASSERT_PTR(userdata);
_cleanup_free_ char *k = NULL, *v = NULL;
@@ -262,9 +278,19 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, bool
const char *key = sysctl_normalize(strstrip(k)), *value = strstrip(v);
/* We can't filter out globs at this point, we'll need to do that later. */
if (!string_is_glob(key) && !test_prefix(key))
return 0;
if (arg_save_file) {
/* When saving the loaded rules, refuse unsafe characters, especially newlines, in keys
* and values. Otherwise, the saved file may not be parsed correctly. */
if (!string_is_safe(key, STRING_ALLOW_BACKSLASHES | STRING_ALLOW_QUOTES | STRING_ALLOW_GLOBS) ||
(value && !string_is_safe(value, STRING_ALLOW_EMPTY | STRING_ALLOW_BACKSLASHES | STRING_ALLOW_QUOTES | STRING_ALLOW_GLOBS)))
return log_syntax(NULL, LOG_WARNING, fname, line, SYNTHETIC_ERRNO(EINVAL),
"Ignoring unsafe key and/or value for '%s'.", key);
} else {
/* We can't filter out globs at this point, we'll need to do that later.
* Keep them when saving the loaded rules, and filter them after writing. */
if (!string_is_glob(key) && !test_prefix(key))
return 0;
}
SysctlOption *existing = ordered_hashmap_get(*sysctl_options, key);
if (existing) {
@@ -325,7 +351,82 @@ static int cat_config(char **files) {
return cat_files(NULL, files, arg_cat_flags);
}
static int save_sysctl_options(OrderedHashmap *sysctl_options, const char *filename) {
int r;
assert(filename);
if (ordered_hashmap_isempty(sysctl_options)) {
log_info("No sysctl settings loaded, not creating '%s'.", filename);
return 0;
}
r = mkdir_p("/run/sysctl.d/", 0755);
if (r < 0)
return log_error_errno(r, "Failed to create directory '/run/sysctl.d/': %m");
_cleanup_free_ char *path = path_join("/run/sysctl.d/", filename);
if (!path)
return log_oom();
_cleanup_fclose_ FILE *f = NULL;
_cleanup_(unlink_and_freep) char *temp_path = NULL;
r = fopen_temporary(path, &f, &temp_path);
if (r < 0)
return log_error_errno(r, "Failed to open a temporary file for '%s': %m", path);
if (fchmod(fileno(f), 0644) < 0)
return log_error_errno(errno, "Failed to chmod '%s': %m", temp_path);
fputs("# This is generated by systemd-sysctl.\n", f);
SysctlOption *option;
ORDERED_HASHMAP_FOREACH(option, sysctl_options)
fprintf(f, "%s%s%s%s\n",
option->ignore_failure || !option->value ? "-" : "",
option->key,
option->value ? " = " : "",
strempty(option->value));
r = fflush_and_check(f);
if (r < 0)
return log_error_errno(r, "Failed to flush '%s': %m", temp_path);
r = conservative_rename(temp_path, path);
if (r < 0)
return log_error_errno(r, "Failed to rename '%s' to '%s': %m", temp_path, path);
temp_path = mfree(temp_path);
return 0;
}
static int parse_conf_filename(const char *src, const char *option, char **dest) {
assert(option);
assert(dest);
if (isempty(src))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot use an empty filename for %s.", option);
if (startswith(src, "."))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Dot-prefixed filenames are not allowed for %s.", option);
_cleanup_free_ char *name = NULL;
if (!endswith(src, ".conf"))
name = strjoin(src, ".conf");
else
name = strdup(src);
if (!name)
return log_oom();
if (!filename_is_valid(name))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid file name '%s' specified for %s.", name, option);
return free_and_replace(*dest, name);
}
static int parse_argv(int argc, char *argv[], char ***remaining_args) {
int r;
assert(argc >= 0);
assert(argv);
assert(remaining_args);
@@ -391,6 +492,20 @@ static int parse_argv(int argc, char *argv[], char ***remaining_args) {
arg_inline = true;
break;
OPTION_LONG("save", "FILENAME",
"Write loaded settings to the specified file"):
r = parse_conf_filename(opts.arg, "--save=", &arg_save_file);
if (r < 0)
return r;
break;
OPTION_LONG("revert", "FILENAME",
"Remove the specified file from /run/sysctl.d/"):
r = parse_conf_filename(opts.arg, "--revert=", &arg_revert_file);
if (r < 0)
return r;
break;
OPTION_COMMON_INTROSPECT_CLI:
return introspect_cli(SD_JSON_FORMAT_OFF);
}
@@ -401,6 +516,14 @@ static int parse_argv(int argc, char *argv[], char ***remaining_args) {
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Positional arguments are not allowed with --cat-config/--tldr.");
if (arg_inline && strv_isempty(*remaining_args))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"--inline requires positional arguments.");
if (arg_save_file && strv_isempty(*remaining_args))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"--save= requires positional arguments.");
return 1;
}
@@ -416,17 +539,40 @@ static int run(int argc, char *argv[]) {
umask(0022);
if (arg_revert_file) {
_cleanup_free_ char *path = path_join("/run/sysctl.d/", arg_revert_file);
if (!path)
return log_oom();
if (unlink(path) < 0 && errno != ENOENT)
return log_error_errno(errno, "Failed to remove '%s': %m", path);
}
_cleanup_ordered_hashmap_free_ OrderedHashmap *sysctl_options = NULL;
if (!strv_isempty(args)) {
unsigned pos = 0;
STRV_FOREACH(arg, args) {
if (arg_inline)
if (arg_inline) {
pos++;
const char *s = skip_leading_chars(*arg, /* bad= */ NULL);
if (IN_SET(*s, '\0', '#'))
continue;
/* Use (argument):n, where n==1 for the first positional arg */
RET_GATHER(r, parse_line("(argument)", ++pos, *arg, /* invalid_config= */ NULL, &sysctl_options));
else
RET_GATHER(r, parse_line("(argument)", pos, s, /* invalid_config= */ NULL, &sysctl_options));
} else
RET_GATHER(r, parse_file(&sysctl_options, *arg, false));
}
if (arg_save_file) {
r = save_sysctl_options(sysctl_options, arg_save_file);
if (r < 0)
return r;
filter_sysctl_options(sysctl_options);
}
} else {
_cleanup_strv_free_ char **files = NULL;