X Tutup
Skip to content

Commit 256ce2e

Browse files
authored
Merge pull request systemd#21838 from lnussel/logind-refactor
Logind shutdown refactor
2 parents e3759ac + 48f3bc5 commit 256ce2e

File tree

16 files changed

+539
-364
lines changed

16 files changed

+539
-364
lines changed

src/login/logind-action.c

Lines changed: 111 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <unistd.h>
44

5+
#include "sd-messages.h"
6+
57
#include "alloc-util.h"
68
#include "bus-error.h"
79
#include "bus-util.h"
@@ -11,29 +13,119 @@
1113
#include "logind-dbus.h"
1214
#include "logind-session-dbus.h"
1315
#include "process-util.h"
14-
#include "sleep-config.h"
1516
#include "special.h"
1617
#include "string-table.h"
1718
#include "terminal-util.h"
1819
#include "user-util.h"
1920

21+
static const ActionTableItem action_table[_HANDLE_ACTION_MAX] = {
22+
[HANDLE_POWEROFF] = {
23+
SPECIAL_POWEROFF_TARGET,
24+
INHIBIT_SHUTDOWN,
25+
"org.freedesktop.login1.power-off",
26+
"org.freedesktop.login1.power-off-multiple-sessions",
27+
"org.freedesktop.login1.power-off-ignore-inhibit",
28+
_SLEEP_OPERATION_INVALID,
29+
SD_MESSAGE_SHUTDOWN_STR,
30+
"System is powering down",
31+
"power-off",
32+
},
33+
[HANDLE_REBOOT] = {
34+
SPECIAL_REBOOT_TARGET,
35+
INHIBIT_SHUTDOWN,
36+
"org.freedesktop.login1.reboot",
37+
"org.freedesktop.login1.reboot-multiple-sessions",
38+
"org.freedesktop.login1.reboot-ignore-inhibit",
39+
_SLEEP_OPERATION_INVALID,
40+
SD_MESSAGE_SHUTDOWN_STR,
41+
"System is rebooting",
42+
"reboot",
43+
},
44+
[HANDLE_HALT] = {
45+
SPECIAL_HALT_TARGET,
46+
INHIBIT_SHUTDOWN,
47+
"org.freedesktop.login1.halt",
48+
"org.freedesktop.login1.halt-multiple-sessions",
49+
"org.freedesktop.login1.halt-ignore-inhibit",
50+
_SLEEP_OPERATION_INVALID,
51+
SD_MESSAGE_SHUTDOWN_STR,
52+
"System is halting",
53+
"halt",
54+
},
55+
[HANDLE_KEXEC] = {
56+
SPECIAL_KEXEC_TARGET,
57+
INHIBIT_SHUTDOWN,
58+
"org.freedesktop.login1.reboot",
59+
"org.freedesktop.login1.reboot-multiple-sessions",
60+
"org.freedesktop.login1.reboot-ignore-inhibit",
61+
_SLEEP_OPERATION_INVALID,
62+
SD_MESSAGE_SHUTDOWN_STR,
63+
"System is rebooting with kexec",
64+
"kexec",
65+
},
66+
[HANDLE_SUSPEND] = {
67+
SPECIAL_SUSPEND_TARGET,
68+
INHIBIT_SLEEP,
69+
"org.freedesktop.login1.suspend",
70+
"org.freedesktop.login1.suspend-multiple-sessions",
71+
"org.freedesktop.login1.suspend-ignore-inhibit",
72+
SLEEP_SUSPEND,
73+
},
74+
[HANDLE_HIBERNATE] = {
75+
SPECIAL_HIBERNATE_TARGET,
76+
INHIBIT_SLEEP,
77+
"org.freedesktop.login1.hibernate",
78+
"org.freedesktop.login1.hibernate-multiple-sessions",
79+
"org.freedesktop.login1.hibernate-ignore-inhibit",
80+
SLEEP_HIBERNATE,
81+
},
82+
[HANDLE_HYBRID_SLEEP] = {
83+
SPECIAL_HYBRID_SLEEP_TARGET,
84+
INHIBIT_SLEEP,
85+
"org.freedesktop.login1.hibernate",
86+
"org.freedesktop.login1.hibernate-multiple-sessions",
87+
"org.freedesktop.login1.hibernate-ignore-inhibit",
88+
SLEEP_HYBRID_SLEEP,
89+
},
90+
[HANDLE_SUSPEND_THEN_HIBERNATE] = {
91+
SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET,
92+
INHIBIT_SLEEP,
93+
"org.freedesktop.login1.hibernate",
94+
"org.freedesktop.login1.hibernate-multiple-sessions",
95+
"org.freedesktop.login1.hibernate-ignore-inhibit",
96+
SLEEP_SUSPEND_THEN_HIBERNATE,
97+
},
98+
[HANDLE_FACTORY_RESET] = {
99+
SPECIAL_FACTORY_RESET_TARGET,
100+
_INHIBIT_WHAT_INVALID,
101+
NULL,
102+
NULL,
103+
NULL,
104+
_SLEEP_OPERATION_INVALID,
105+
SD_MESSAGE_FACTORY_RESET_STR,
106+
"System is performing factory reset",
107+
NULL
108+
},
109+
};
110+
20111
const char* manager_target_for_action(HandleAction handle) {
21-
static const char * const target_table[_HANDLE_ACTION_MAX] = {
22-
[HANDLE_POWEROFF] = SPECIAL_POWEROFF_TARGET,
23-
[HANDLE_REBOOT] = SPECIAL_REBOOT_TARGET,
24-
[HANDLE_HALT] = SPECIAL_HALT_TARGET,
25-
[HANDLE_KEXEC] = SPECIAL_KEXEC_TARGET,
26-
[HANDLE_SUSPEND] = SPECIAL_SUSPEND_TARGET,
27-
[HANDLE_HIBERNATE] = SPECIAL_HIBERNATE_TARGET,
28-
[HANDLE_HYBRID_SLEEP] = SPECIAL_HYBRID_SLEEP_TARGET,
29-
[HANDLE_SUSPEND_THEN_HIBERNATE] = SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET,
30-
[HANDLE_FACTORY_RESET] = SPECIAL_FACTORY_RESET_TARGET,
31-
};
112+
assert(handle >= 0);
113+
assert(handle < (ssize_t) ELEMENTSOF(action_table));
32114

115+
return action_table[handle].target;
116+
}
117+
118+
const ActionTableItem* manager_item_for_handle(HandleAction handle) {
33119
assert(handle >= 0);
34-
if (handle < (ssize_t) ELEMENTSOF(target_table))
35-
return target_table[handle];
36-
return NULL;
120+
assert(handle < (ssize_t) ELEMENTSOF(action_table));
121+
122+
return &action_table[handle];
123+
}
124+
125+
HandleAction manager_handle_for_item(const ActionTableItem* a) {
126+
if (a && a < action_table + ELEMENTSOF(action_table))
127+
return a - action_table;
128+
return _HANDLE_ACTION_INVALID;
37129
}
38130

39131
int manager_handle_action(
@@ -59,7 +151,6 @@ int manager_handle_action(
59151
InhibitWhat inhibit_operation;
60152
Inhibitor *offending = NULL;
61153
bool supported;
62-
const char *target;
63154
int r;
64155

65156
assert(m);
@@ -129,17 +220,13 @@ int manager_handle_action(
129220
return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
130221
"Requested %s operation not supported, ignoring.", handle_action_to_string(handle));
131222

132-
if (m->action_what > 0)
223+
if (m->delayed_action)
133224
return log_debug_errno(SYNTHETIC_ERRNO(EALREADY),
134225
"Action already in progress (%s), ignoring requested %s operation.",
135-
inhibit_what_to_string(m->action_what),
226+
inhibit_what_to_string(m->delayed_action->inhibit_what),
136227
handle_action_to_string(handle));
137228

138-
assert_se(target = manager_target_for_action(handle));
139-
140-
inhibit_operation = IN_SET(handle, HANDLE_SUSPEND, HANDLE_HIBERNATE,
141-
HANDLE_HYBRID_SLEEP,
142-
HANDLE_SUSPEND_THEN_HIBERNATE) ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN;
229+
inhibit_operation = manager_item_for_handle(handle)->inhibit_what;
143230

144231
/* If the actual operation is inhibited, warn and fail */
145232
if (!ignore_inhibited &&
@@ -162,7 +249,7 @@ int manager_handle_action(
162249

163250
log_info("%s", message_table[handle]);
164251

165-
r = bus_manager_shutdown_or_sleep_now_or_later(m, target, inhibit_operation, &error);
252+
r = bus_manager_shutdown_or_sleep_now_or_later(m, manager_item_for_handle(handle), &error);
166253
if (r < 0)
167254
return log_error_errno(r, "Failed to execute %s operation: %s",
168255
handle_action_to_string(handle),

src/login/logind-action.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,26 @@ typedef enum HandleAction {
1919
_HANDLE_ACTION_INVALID = -EINVAL,
2020
} HandleAction;
2121

22+
typedef struct ActionTableItem ActionTableItem;
23+
24+
#define handle_action_valid(x) (x && (x < _HANDLE_ACTION_MAX))
25+
2226
#include "logind-inhibit.h"
2327
#include "logind.h"
28+
#include "sleep-config.h"
29+
30+
struct ActionTableItem {
31+
const char *target;
32+
InhibitWhat inhibit_what;
33+
const char *polkit_action;
34+
const char *polkit_action_multiple_sessions;
35+
const char *polkit_action_ignore_inhibit;
36+
SleepOperation sleep_operation;
37+
const char* message_id;
38+
const char* message;
39+
const char* log_str;
40+
41+
};
2442

2543
int manager_handle_action(
2644
Manager *m,
@@ -33,5 +51,7 @@ const char* handle_action_to_string(HandleAction h) _const_;
3351
HandleAction handle_action_from_string(const char *s) _pure_;
3452

3553
const char* manager_target_for_action(HandleAction handle);
54+
const ActionTableItem* manager_item_for_handle(HandleAction handle);
55+
HandleAction manager_handle_for_item(const ActionTableItem* a);
3656

3757
CONFIG_PARSER_PROTOTYPE(config_parse_handle_action);

src/login/logind-button.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ static void button_lid_switch_handle_action(Manager *manager, bool is_edge) {
8484
* differently */
8585
if (manager_is_docked_or_external_displays(manager))
8686
handle_action = manager->handle_lid_switch_docked;
87-
else if (manager->handle_lid_switch_ep != _HANDLE_ACTION_INVALID &&
88-
manager_is_on_external_power())
87+
else if (!handle_action_valid(manager->handle_lid_switch_ep) && manager_is_on_external_power())
8988
handle_action = manager->handle_lid_switch_ep;
9089
else
9190
handle_action = manager->handle_lid_switch;

0 commit comments

Comments
 (0)
X Tutup