chiark / gitweb /
Prep v229: Add missing fixes from upstream [4/6] src/login
[elogind.git] / src / login / logind-action.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2012 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <unistd.h>
21
22 #include "alloc-util.h"
23 #include "bus-error.h"
24 #include "bus-util.h"
25 #include "conf-parser.h"
26 #include "formats-util.h"
27 #include "logind-action.h"
28 #include "process-util.h"
29 #include "sleep-config.h"
30 //#include "special.h"
31 #include "string-table.h"
32 #include "terminal-util.h"
33 #include "user-util.h"
34
35 // Additional includes needed by elogind
36 #include "fd-util.h"
37 #include "fileio.h"
38 #include "sd-messages.h"
39 #include "strv.h"
40
41
42 int manager_handle_action(
43                 Manager *m,
44                 InhibitWhat inhibit_key,
45                 HandleAction handle,
46                 bool ignore_inhibited,
47                 bool is_edge) {
48
49         static const char * const message_table[_HANDLE_ACTION_MAX] = {
50                 [HANDLE_POWEROFF] = "Powering Off...",
51                 [HANDLE_REBOOT] = "Rebooting...",
52                 [HANDLE_HALT] = "Halting...",
53                 [HANDLE_KEXEC] = "Rebooting via kexec...",
54                 [HANDLE_SUSPEND] = "Suspending...",
55                 [HANDLE_HIBERNATE] = "Hibernating...",
56                 [HANDLE_HYBRID_SLEEP] = "Hibernating and suspending..."
57         };
58
59 #if 0 /// elogind does this itself. No target table required
60         static const char * const target_table[_HANDLE_ACTION_MAX] = {
61                 [HANDLE_POWEROFF] = SPECIAL_POWEROFF_TARGET,
62                 [HANDLE_REBOOT] = SPECIAL_REBOOT_TARGET,
63                 [HANDLE_HALT] = SPECIAL_HALT_TARGET,
64                 [HANDLE_KEXEC] = SPECIAL_KEXEC_TARGET,
65                 [HANDLE_SUSPEND] = SPECIAL_SUSPEND_TARGET,
66                 [HANDLE_HIBERNATE] = SPECIAL_HIBERNATE_TARGET,
67                 [HANDLE_HYBRID_SLEEP] = SPECIAL_HYBRID_SLEEP_TARGET
68         };
69 #endif // 0
70
71         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
72         InhibitWhat inhibit_operation;
73         Inhibitor *offending = NULL;
74         bool supported;
75         int r;
76
77         assert(m);
78
79         /* If the key handling is turned off, don't do anything */
80         if (handle == HANDLE_IGNORE) {
81                 log_debug("Refusing operation, as it is turned off.");
82                 return 0;
83         }
84
85         if (inhibit_key == INHIBIT_HANDLE_LID_SWITCH) {
86                 /* If the last system suspend or startup is too close,
87                  * let's not suspend for now, to give USB docking
88                  * stations some time to settle so that we can
89                  * properly watch its displays. */
90                 if (m->lid_switch_ignore_event_source) {
91                         log_debug("Ignoring lid switch request, system startup or resume too close.");
92                         return 0;
93                 }
94         }
95
96         /* If the key handling is inhibited, don't do anything */
97         if (inhibit_key > 0) {
98                 if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0, NULL)) {
99                         log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_key));
100                         return 0;
101                 }
102         }
103
104         /* Locking is handled differently from the rest. */
105         if (handle == HANDLE_LOCK) {
106
107                 if (!is_edge)
108                         return 0;
109
110                 log_info("Locking sessions...");
111                 session_send_lock_all(m, true);
112                 return 1;
113         }
114
115         if (handle == HANDLE_SUSPEND)
116                 supported = can_sleep("suspend") > 0;
117         else if (handle == HANDLE_HIBERNATE)
118                 supported = can_sleep("hibernate") > 0;
119         else if (handle == HANDLE_HYBRID_SLEEP)
120                 supported = can_sleep("hybrid-sleep") > 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 = handle == HANDLE_SUSPEND || handle == HANDLE_HIBERNATE || handle == HANDLE_HYBRID_SLEEP ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN;
137
138         /* If the actual operation is inhibited, warn and fail */
139         if (!ignore_inhibited &&
140             manager_is_inhibited(m, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0, &offending)) {
141                 _cleanup_free_ char *comm = NULL, *u = NULL;
142
143                 get_process_comm(offending->pid, &comm);
144                 u = uid_to_name(offending->uid);
145
146                 /* If this is just a recheck of the lid switch then don't warn about anything */
147                 if (!is_edge) {
148                         log_debug("Refusing operation, %s is inhibited by UID "UID_FMT"/%s, PID "PID_FMT"/%s.",
149                                   inhibit_what_to_string(inhibit_operation),
150                                   offending->uid, strna(u),
151                                   offending->pid, strna(comm));
152                         return 0;
153                 }
154
155                 log_error("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
160                 return -EPERM;
161         }
162
163         log_info("%s", message_table[handle]);
164
165 #if 0 /// elogind uses its own variant, which can use the handle directly.
166         r = bus_manager_shutdown_or_sleep_now_or_later(m, target_table[handle], inhibit_operation, &error);
167 #else
168         r = bus_manager_shutdown_or_sleep_now_or_later(m, handle, inhibit_operation, &error);
169 #endif // 0
170         if (r < 0) {
171                 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
172                 return r;
173         }
174
175         return 1;
176 }
177
178 static int run_helper(const char *helper) {
179         int pid = fork();
180         if (pid < 0) {
181                 return log_error_errno(errno, "Failed to fork: %m");
182         }
183
184         if (pid == 0) {
185                 /* Child */
186
187                 close_all_fds(NULL, 0);
188
189                 execlp(helper, helper, NULL);
190                 log_error_errno(errno, "Failed to execute %s: %m", helper);
191                 _exit(EXIT_FAILURE);
192         }
193
194         return wait_for_terminate_and_warn(helper, pid, true);
195 }
196
197 static int write_mode(char **modes) {
198         int r = 0;
199         char **mode;
200
201         STRV_FOREACH(mode, modes) {
202                 int k;
203
204                 k = write_string_file("/sys/power/disk", *mode, 0);
205                 if (k == 0)
206                         return 0;
207
208                 log_debug_errno(k, "Failed to write '%s' to /sys/power/disk: %m",
209                                 *mode);
210                 if (r == 0)
211                         r = k;
212         }
213
214         if (r < 0)
215                 log_error_errno(r, "Failed to write mode to /sys/power/disk: %m");
216
217         return r;
218 }
219
220 static int write_state(FILE **f, char **states) {
221         char **state;
222         int r = 0;
223
224         STRV_FOREACH(state, states) {
225                 int k;
226
227                 k = write_string_stream(*f, *state, true);
228                 if (k == 0)
229                         return 0;
230                 log_debug_errno(k, "Failed to write '%s' to /sys/power/state: %m",
231                                 *state);
232                 if (r == 0)
233                         r = k;
234
235                 fclose(*f);
236                 *f = fopen("/sys/power/state", "we");
237                 if (!*f)
238                         return log_error_errno(errno, "Failed to open /sys/power/state: %m");
239         }
240
241         return r;
242 }
243
244 static int do_sleep(const char *arg_verb, char **modes, char **states) {
245         char *arguments[] = {
246                 NULL,
247                 (char*) "pre",
248                 (char*) arg_verb,
249                 NULL
250         };
251         static const char* const dirs[] = {SYSTEM_SLEEP_PATH, NULL};
252
253         int r;
254         _cleanup_fclose_ FILE *f = NULL;
255
256         /* This file is opened first, so that if we hit an error,
257          * we can abort before modifying any state. */
258         f = fopen("/sys/power/state", "we");
259         if (!f)
260                 return log_error_errno(errno, "Failed to open /sys/power/state: %m");
261
262         /* Configure the hibernation mode */
263         r = write_mode(modes);
264         if (r < 0)
265                 return r;
266
267         execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments);
268
269         log_struct(LOG_INFO,
270                    LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_START),
271                    LOG_MESSAGE("Suspending system..."),
272                    "SLEEP=%s", arg_verb,
273                    NULL);
274
275         r = write_state(&f, states);
276         if (r < 0)
277                 return r;
278
279         log_struct(LOG_INFO,
280                    LOG_MESSAGE_ID(SD_MESSAGE_SLEEP_STOP),
281                    LOG_MESSAGE("System resumed."),
282                    "SLEEP=%s", arg_verb,
283                    NULL);
284
285         arguments[1] = (char*) "post";
286         execute_directories(dirs, DEFAULT_TIMEOUT_USEC, arguments);
287
288         return r;
289 }
290
291 int shutdown_or_sleep(Manager *m, HandleAction action) {
292
293         assert(m);
294
295         switch (action) {
296         case HANDLE_POWEROFF:
297                 return run_helper(HALT);
298         case HANDLE_REBOOT:
299                 return run_helper(REBOOT);
300         case HANDLE_HALT:
301                 return run_helper(HALT);
302         case HANDLE_KEXEC:
303                 return run_helper(KEXEC);
304         case HANDLE_SUSPEND:
305                 return do_sleep("suspend", m->suspend_mode, m->suspend_state);
306         case HANDLE_HIBERNATE:
307                 return do_sleep("hibernate", m->hibernate_mode, m->hibernate_state);
308         case HANDLE_HYBRID_SLEEP:
309                 return do_sleep("hybrid-sleep", m->hybrid_sleep_mode, m->hybrid_sleep_state);
310         default:
311                 return -EINVAL;
312         }
313 }
314
315 static const char* const handle_action_table[_HANDLE_ACTION_MAX] = {
316         [HANDLE_IGNORE] = "ignore",
317         [HANDLE_POWEROFF] = "poweroff",
318         [HANDLE_REBOOT] = "reboot",
319         [HANDLE_HALT] = "halt",
320         [HANDLE_KEXEC] = "kexec",
321         [HANDLE_SUSPEND] = "suspend",
322         [HANDLE_HIBERNATE] = "hibernate",
323         [HANDLE_HYBRID_SLEEP] = "hybrid-sleep",
324         [HANDLE_LOCK] = "lock"
325 };
326
327 DEFINE_STRING_TABLE_LOOKUP(handle_action, HandleAction);
328 DEFINE_CONFIG_PARSE_ENUM(config_parse_handle_action, handle_action, HandleAction, "Failed to parse handle action setting");