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.
This commit is contained in:
Lennart Poettering 2018-02-21 17:54:35 +01:00
parent 118cf9523b
commit e3631d1c80
7 changed files with 42 additions and 25 deletions

View File

@ -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

34
src/basic/reboot-util.c Normal file
View File

@ -0,0 +1,34 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include <errno.h>
#include <unistd.h>
#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;
}

4
src/basic/reboot-util.h Normal file
View File

@ -0,0 +1,4 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
int update_reboot_parameter_and_warn(const char *parameter);

View File

@ -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);

View File

@ -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);

View File

@ -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"

View File

@ -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"