From e3631d1c809e368a5116d01e4fb9b9c7d2dddcca Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 21 Feb 2018 17:54:35 +0100 Subject: [PATCH] basic: split out update_reboot_parameter_and_warn() into its own .c/.h files This is primarily preparation for a follow-up commit that adds a common implementation of the other side of the reboot parameter file, i.e. the code that reads the file and issues reboot() for it. --- src/basic/meson.build | 2 ++ src/basic/reboot-util.c | 34 ++++++++++++++++++++++++++++++++++ src/basic/reboot-util.h | 4 ++++ src/basic/util.c | 23 ----------------------- src/basic/util.h | 2 -- src/core/emergency-action.c | 1 + src/systemctl/systemctl.c | 1 + 7 files changed, 42 insertions(+), 25 deletions(-) create mode 100644 src/basic/reboot-util.c create mode 100644 src/basic/reboot-util.h diff --git a/src/basic/meson.build b/src/basic/meson.build index 811085eec1..c71599db7b 100644 --- a/src/basic/meson.build +++ b/src/basic/meson.build @@ -158,6 +158,8 @@ basic_sources = files(''' ratelimit.h raw-clone.h raw-reboot.h + reboot-util.c + reboot-util.h refcnt.h replace-var.c replace-var.h diff --git a/src/basic/reboot-util.c b/src/basic/reboot-util.c new file mode 100644 index 0000000000..68910a6a34 --- /dev/null +++ b/src/basic/reboot-util.c @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +#include +#include + +#include "fileio.h" +#include "log.h" +#include "reboot-util.h" +#include "string-util.h" +#include "umask-util.h" + +int update_reboot_parameter_and_warn(const char *parameter) { + int r; + + if (isempty(parameter)) { + if (unlink("/run/systemd/reboot-param") < 0) { + if (errno == ENOENT) + return 0; + + return log_warning_errno(errno, "Failed to unlink reboot parameter file: %m"); + } + + return 0; + } + + RUN_WITH_UMASK(0022) { + r = write_string_file("/run/systemd/reboot-param", parameter, + WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC); + if (r < 0) + return log_warning_errno(r, "Failed to write reboot parameter file: %m"); + } + + return 0; +} diff --git a/src/basic/reboot-util.h b/src/basic/reboot-util.h new file mode 100644 index 0000000000..6f1d24c1f5 --- /dev/null +++ b/src/basic/reboot-util.h @@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +#pragma once + +int update_reboot_parameter_and_warn(const char *parameter); diff --git a/src/basic/util.c b/src/basic/util.c index c7f1513f3e..7863a14fb2 100644 --- a/src/basic/util.c +++ b/src/basic/util.c @@ -518,29 +518,6 @@ uint64_t system_tasks_max_scale(uint64_t v, uint64_t max) { return m / max; } -int update_reboot_parameter_and_warn(const char *param) { - int r; - - if (isempty(param)) { - if (unlink("/run/systemd/reboot-param") < 0) { - if (errno == ENOENT) - return 0; - - return log_warning_errno(errno, "Failed to unlink reboot parameter file: %m"); - } - - return 0; - } - - RUN_WITH_UMASK(0022) { - r = write_string_file("/run/systemd/reboot-param", param, WRITE_STRING_FILE_CREATE); - if (r < 0) - return log_warning_errno(r, "Failed to write reboot parameter file: %m"); - } - - return 0; -} - int version(void) { puts(PACKAGE_STRING "\n" SYSTEMD_FEATURES); diff --git a/src/basic/util.h b/src/basic/util.h index 9d1b10756b..6f8d8bef34 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -186,8 +186,6 @@ uint64_t physical_memory_scale(uint64_t v, uint64_t max); uint64_t system_tasks_max(void); uint64_t system_tasks_max_scale(uint64_t v, uint64_t max); -int update_reboot_parameter_and_warn(const char *param); - int version(void); int str_verscmp(const char *s1, const char *s2); diff --git a/src/core/emergency-action.c b/src/core/emergency-action.c index be3ca8f20d..3d37a986bc 100644 --- a/src/core/emergency-action.c +++ b/src/core/emergency-action.c @@ -25,6 +25,7 @@ #include "bus-util.h" #include "emergency-action.h" #include "raw-reboot.h" +#include "reboot-util.h" #include "special.h" #include "string-table.h" #include "terminal-util.h" diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 800f862938..0d3d0d0daa 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -73,6 +73,7 @@ #include "path-util.h" #include "process-util.h" #include "raw-reboot.h" +#include "reboot-util.h" #include "rlimit-util.h" #include "set.h" #include "sigbus.h"