1 Star 0 Fork 48

杨枝甘露瑞纳冰/systemd

forked from src-anolis-os/systemd 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0445-systemctl-replace-switch-statement-by-table-of-struc.patch 4.42 KB
一键复制 编辑 原始数据 按行查看 历史
geliwei 提交于 2021-06-16 16:46 . update to systemd-239-45.el8.src.rpm
From 8b34041ee97069bee8bf03ae5ba651b34b1b8460 Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Tue, 26 Mar 2019 15:20:26 +0100
Subject: [PATCH] systemctl: replace switch statement by table of structures
(cherry picked from commit c45e5fb877033c9e3f9b79121644ed71032af379)
Related: #846319
---
src/systemctl/systemctl.c | 68 ++++++++++++---------------------------
1 file changed, 21 insertions(+), 47 deletions(-)
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index e963f19b0a..04e24691d8 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -3179,64 +3179,38 @@ static int logind_set_wall_message(void) {
}
#endif
-/* Ask systemd-logind, which might grant access to unprivileged users
- * through PolicyKit */
+/* Ask systemd-logind, which might grant access to unprivileged users through polkit */
static int logind_reboot(enum action a) {
#if ENABLE_LOGIND
+ static const struct {
+ const char *method;
+ const char *description;
+ } actions[_ACTION_MAX] = {
+ [ACTION_POWEROFF] = { "PowerOff", "power off system" },
+ [ACTION_REBOOT] = { "Reboot", "reboot system" },
+ [ACTION_HALT] = { "Halt", "halt system" },
+ [ACTION_SUSPEND] = { "Suspend", "suspend system" },
+ [ACTION_HIBERNATE] = { "Hibernate", "hibernate system" },
+ [ACTION_HYBRID_SLEEP] = { "HybridSleep", "put system into hybrid sleep" },
+ [ACTION_SUSPEND_THEN_HIBERNATE] = { "SuspendThenHibernate", "suspend system, hibernate later" },
+ };
+
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
- const char *method, *description;
sd_bus *bus;
int r;
+ if (a < 0 || a >= _ACTION_MAX || !actions[a].method)
+ return -EINVAL;
+
r = acquire_bus(BUS_FULL, &bus);
if (r < 0)
return r;
- switch (a) {
-
- case ACTION_POWEROFF:
- method = "PowerOff";
- description = "power off system";
- break;
-
- case ACTION_REBOOT:
- method = "Reboot";
- description = "reboot system";
- break;
-
- case ACTION_HALT:
- method = "Halt";
- description = "halt system";
- break;
-
- case ACTION_SUSPEND:
- method = "Suspend";
- description = "suspend system";
- break;
-
- case ACTION_HIBERNATE:
- method = "Hibernate";
- description = "hibernate system";
- break;
-
- case ACTION_HYBRID_SLEEP:
- method = "HybridSleep";
- description = "put system into hybrid sleep";
- break;
-
- case ACTION_SUSPEND_THEN_HIBERNATE:
- method = "SuspendThenHibernate";
- description = "put system into suspend followed by hibernate";
- break;
-
- default:
- return -EINVAL;
- }
-
polkit_agent_open_maybe();
(void) logind_set_wall_message();
- log_debug("%s org.freedesktop.login1.Manager %s dbus call.", arg_dry_run ? "Would execute" : "Executing", method);
+ log_debug("%s org.freedesktop.login1.Manager %s dbus call.", arg_dry_run ? "Would execute" : "Executing", actions[a].method);
+
if (arg_dry_run)
return 0;
@@ -3245,12 +3219,12 @@ static int logind_reboot(enum action a) {
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
- method,
+ actions[a].method,
&error,
NULL,
"b", arg_ask_password);
if (r < 0)
- return log_error_errno(r, "Failed to %s via logind: %s", description, bus_error_message(&error, r));
+ return log_error_errno(r, "Failed to %s via logind: %s", actions[a].description, bus_error_message(&error, r));
return 0;
#else
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yummypeng/systemd.git
git@gitee.com:yummypeng/systemd.git
yummypeng
systemd
systemd
a8

搜索帮助