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