From fb19c2ffa2a4987c95921b7735effe4aca3b6fa4 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Fri, 7 Aug 2026 23:07:40 +0900 Subject: [PATCH] sysctl: introduce --revert= option to remove runtime sysctl config This may be useful for removing files created by the --save= option. --- src/sysctl/sysctl.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index 3a7f64fc14..1e94643cad 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include +#include #include "sd-json.h" @@ -33,9 +34,11 @@ 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", @@ -496,6 +499,13 @@ static int parse_argv(int argc, char *argv[], char ***remaining_args) { 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); } @@ -529,6 +539,15 @@ 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;