]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/logind-action.c
Merge pull request #12753 from jrouleau/fix/hibernate-resume-timeout
[thirdparty/systemd.git] / src / login / logind-action.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
23406ce5 2
dc3a1b76
LP
3#include <unistd.h>
4
b5efdb8a 5#include "alloc-util.h"
cc377381 6#include "bus-error.h"
8b43440b
LP
7#include "bus-util.h"
8#include "conf-parser.h"
f97b34a6 9#include "format-util.h"
8b43440b 10#include "logind-action.h"
6ecda0fb
LP
11#include "logind-dbus.h"
12#include "logind-session-dbus.h"
0b452006 13#include "process-util.h"
8b43440b
LP
14#include "sleep-config.h"
15#include "special.h"
16#include "string-table.h"
288a74cc 17#include "terminal-util.h"
b1d4f8e1 18#include "user-util.h"
23406ce5 19
c8c8ee85
ZJS
20const 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 };
31
32 assert(handle >= 0);
33 if (handle < (ssize_t) ELEMENTSOF(target_table))
34 return target_table[handle];
35 return NULL;
36}
37
23406ce5
LP
38int manager_handle_action(
39 Manager *m,
40 InhibitWhat inhibit_key,
41 HandleAction handle,
42 bool ignore_inhibited,
43 bool is_edge) {
44
45 static const char * const message_table[_HANDLE_ACTION_MAX] = {
46 [HANDLE_POWEROFF] = "Powering Off...",
47 [HANDLE_REBOOT] = "Rebooting...",
48 [HANDLE_HALT] = "Halting...",
49 [HANDLE_KEXEC] = "Rebooting via kexec...",
50 [HANDLE_SUSPEND] = "Suspending...",
51 [HANDLE_HIBERNATE] = "Hibernating...",
c58493c0 52 [HANDLE_HYBRID_SLEEP] = "Hibernating and suspending...",
e68c79db 53 [HANDLE_SUSPEND_THEN_HIBERNATE] = "Suspending, then hibernating...",
23406ce5
LP
54 };
55
4afd3348 56 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
23406ce5 57 InhibitWhat inhibit_operation;
85a428c6 58 Inhibitor *offending = NULL;
af9792ac 59 bool supported;
c8c8ee85 60 const char *target;
cc377381 61 int r;
23406ce5
LP
62
63 assert(m);
64
65 /* If the key handling is turned off, don't do anything */
66 if (handle == HANDLE_IGNORE) {
67 log_debug("Refusing operation, as it is turned off.");
68 return 0;
69 }
70
2d62c530 71 if (inhibit_key == INHIBIT_HANDLE_LID_SWITCH) {
f9cd6be1
LP
72 /* If the last system suspend or startup is too close,
73 * let's not suspend for now, to give USB docking
74 * stations some time to settle so that we can
75 * properly watch its displays. */
76 if (m->lid_switch_ignore_event_source) {
77 log_debug("Ignoring lid switch request, system startup or resume too close.");
78 return 0;
79 }
2d62c530
LP
80 }
81
cc377381 82 /* If the key handling is inhibited, don't do anything */
06a70b91 83 if (inhibit_key > 0) {
85a428c6 84 if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0, NULL)) {
cc377381
LP
85 log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_key));
86 return 0;
87 }
88 }
89
90 /* Locking is handled differently from the rest. */
91 if (handle == HANDLE_LOCK) {
ed4ba7e4
LP
92 if (!is_edge)
93 return 0;
94
cc377381
LP
95 log_info("Locking sessions...");
96 session_send_lock_all(m, true);
97 return 1;
98 }
99
dc3a1b76 100 if (handle == HANDLE_SUSPEND)
19adb8a3 101 supported = can_sleep("suspend") > 0;
dc3a1b76 102 else if (handle == HANDLE_HIBERNATE)
19adb8a3 103 supported = can_sleep("hibernate") > 0;
dc3a1b76 104 else if (handle == HANDLE_HYBRID_SLEEP)
19adb8a3 105 supported = can_sleep("hybrid-sleep") > 0;
e68c79db
ML
106 else if (handle == HANDLE_SUSPEND_THEN_HIBERNATE)
107 supported = can_sleep("suspend-then-hibernate") > 0;
dc3a1b76 108 else if (handle == HANDLE_KEXEC)
78013564 109 supported = access(KEXEC, X_OK) >= 0;
af9792ac
LP
110 else
111 supported = true;
dc3a1b76 112
c282daed
LP
113 if (!supported && IN_SET(handle, HANDLE_HIBERNATE, HANDLE_HYBRID_SLEEP, HANDLE_SUSPEND_THEN_HIBERNATE)) {
114 supported = can_sleep("suspend") > 0;
115 if (supported) {
116 log_notice("Operation '%s' requested but not supported, using regular suspend instead.", handle_action_to_string(handle));
117 handle = HANDLE_SUSPEND;
118 }
119 }
120
dc3a1b76
LP
121 if (!supported) {
122 log_warning("Requested operation not supported, ignoring.");
15411c0c 123 return -EOPNOTSUPP;
dc3a1b76
LP
124 }
125
f8bfa318 126 if (m->action_what > 0) {
cc377381
LP
127 log_debug("Action already in progress, ignoring.");
128 return -EALREADY;
23406ce5
LP
129 }
130
c8c8ee85
ZJS
131 assert_se(target = manager_target_for_action(handle));
132
c58493c0
ML
133 inhibit_operation = IN_SET(handle, HANDLE_SUSPEND, HANDLE_HIBERNATE,
134 HANDLE_HYBRID_SLEEP,
e68c79db 135 HANDLE_SUSPEND_THEN_HIBERNATE) ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN;
23406ce5
LP
136
137 /* If the actual operation is inhibited, warn and fail */
138 if (!ignore_inhibited &&
85a428c6
LP
139 manager_is_inhibited(m, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0, &offending)) {
140 _cleanup_free_ char *comm = NULL, *u = NULL;
141
07be8669 142 (void) get_process_comm(offending->pid, &comm);
85a428c6 143 u = uid_to_name(offending->uid);
23406ce5
LP
144
145 /* If this is just a recheck of the lid switch then don't warn about anything */
146 if (!is_edge) {
de0671ee 147 log_debug("Refusing operation, %s is inhibited by UID "UID_FMT"/%s, PID "PID_FMT"/%s.",
85a428c6 148 inhibit_what_to_string(inhibit_operation),
de0671ee
ZJS
149 offending->uid, strna(u),
150 offending->pid, strna(comm));
23406ce5
LP
151 return 0;
152 }
153
de0671ee 154 log_error("Refusing operation, %s is inhibited by UID "UID_FMT"/%s, PID "PID_FMT"/%s.",
85a428c6 155 inhibit_what_to_string(inhibit_operation),
de0671ee
ZJS
156 offending->uid, strna(u),
157 offending->pid, strna(comm));
85a428c6 158
23406ce5
LP
159 return -EPERM;
160 }
161
162 log_info("%s", message_table[handle]);
163
c8c8ee85 164 r = bus_manager_shutdown_or_sleep_now_or_later(m, target, inhibit_operation, &error);
4ae25393
YW
165 if (r < 0)
166 return log_error_errno(r, "Failed to execute operation: %s", bus_error_message(&error, r));
23406ce5
LP
167
168 return 1;
169}
170
171static const char* const handle_action_table[_HANDLE_ACTION_MAX] = {
172 [HANDLE_IGNORE] = "ignore",
173 [HANDLE_POWEROFF] = "poweroff",
174 [HANDLE_REBOOT] = "reboot",
175 [HANDLE_HALT] = "halt",
176 [HANDLE_KEXEC] = "kexec",
177 [HANDLE_SUSPEND] = "suspend",
178 [HANDLE_HIBERNATE] = "hibernate",
179 [HANDLE_HYBRID_SLEEP] = "hybrid-sleep",
e68c79db 180 [HANDLE_SUSPEND_THEN_HIBERNATE] = "suspend-then-hibernate",
c8c8ee85 181 [HANDLE_LOCK] = "lock",
23406ce5
LP
182};
183
184DEFINE_STRING_TABLE_LOOKUP(handle_action, HandleAction);
185DEFINE_CONFIG_PARSE_ENUM(config_parse_handle_action, handle_action, HandleAction, "Failed to parse handle action setting");