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