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