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