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