chiark / gitweb /
Move manager_dispatch_delayed() back to logind-dbus.c
[elogind.git] / src / login / elogind-dbus.c
1 /***
2   This file is part of elogind.
3
4   Copyright 2017 Sven Eden
5
6   elogind 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   elogind 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 elogind; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20
21 #include "bus-common-errors.h"
22 #include "bus-error.h"
23 #include "bus-util.h"
24 #include "elogind-dbus.h"
25 #include "fd-util.h"
26 #include "process-util.h"
27 #include "sd-messages.h"
28 #include "sleep.h"
29 #include "sleep-config.h"
30 #include "string-util.h"
31 #include "strv.h"
32 #include "update-utmp.h"
33 #include "user-util.h"
34
35
36 static int bus_manager_log_shutdown(
37                 Manager *m,
38                 InhibitWhat w,
39                 HandleAction action) {
40
41          const char *p, *q;
42
43          assert(m);
44
45          if (w != INHIBIT_SHUTDOWN)
46                  return 0;
47
48          switch (action) {
49          case HANDLE_POWEROFF:
50                  p = "MESSAGE=System is powering down.";
51                  q = "SHUTDOWN=power-off";
52                  break;
53          case HANDLE_HALT:
54                  p = "MESSAGE=System is halting.";
55                  q = "SHUTDOWN=halt";
56                  break;
57          case HANDLE_REBOOT:
58                  p = "MESSAGE=System is rebooting.";
59                  q = "SHUTDOWN=reboot";
60                  break;
61          case HANDLE_KEXEC:
62                  p = "MESSAGE=System is rebooting with kexec.";
63                  q = "SHUTDOWN=kexec";
64                  break;
65          default:
66                  p = "MESSAGE=System is shutting down.";
67                  q = NULL;
68          }
69
70         if (isempty(m->wall_message))
71                 p = strjoina(p, ".");
72         else
73                 p = strjoina(p, " (", m->wall_message, ").");
74
75         return log_struct(LOG_NOTICE,
76                           "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR,
77                           p,
78                           q,
79                           NULL);
80 }
81
82 /* elogind specific helper to make HALT and REBOOT possible. */
83 static int run_helper(const char *helper) {
84         pid_t pid = 0;
85         int   r   = 0;
86
87         r = safe_fork_full(helper, NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS|FORK_WAIT, &pid);
88
89         if (r < 0)
90                 return log_error_errno(errno, "Failed to fork run %s: %m", helper);
91
92         if (pid == 0) {
93                 /* Child */
94
95                 execlp(helper, helper, NULL);
96                 log_error_errno(errno, "Failed to execute %s: %m", helper);
97                 _exit(EXIT_FAILURE);
98         }
99
100         return r;
101 }
102
103 /* elogind specific executor */
104 static int shutdown_or_sleep(Manager *m, HandleAction action) {
105
106         assert(m);
107
108         switch (action) {
109         case HANDLE_POWEROFF:
110                 return run_helper(POWEROFF);
111         case HANDLE_REBOOT:
112                 return run_helper(REBOOT);
113         case HANDLE_HALT:
114                 return run_helper(HALT);
115         case HANDLE_KEXEC:
116                 return run_helper(KEXEC);
117         case HANDLE_SUSPEND:
118                 return do_sleep("suspend", m->suspend_mode, m->suspend_state);
119         case HANDLE_HIBERNATE:
120                 return do_sleep("hibernate", m->hibernate_mode, m->hibernate_state);
121         case HANDLE_HYBRID_SLEEP:
122                 return do_sleep("hybrid-sleep", m->hybrid_sleep_mode, m->hybrid_sleep_state);
123         default:
124                 return -EINVAL;
125         }
126 }
127
128 int execute_shutdown_or_sleep(
129                 Manager *m,
130                 InhibitWhat w,
131                 HandleAction action,
132                 sd_bus_error *error) {
133
134         char** argv_utmp = NULL;
135         int r;
136
137         assert(m);
138         assert(w >= 0);
139         assert(w < _INHIBIT_WHAT_MAX);
140
141         bus_manager_log_shutdown(m, w, action);
142
143         if (IN_SET(action, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
144
145                 /* As we have no systemd update-utmp daemon running, we have to
146                  * set the relevant utmp/wtmp entries ourselves.
147                  */
148
149                 if (strv_extend(&argv_utmp, "elogind") < 0)
150                         return log_oom();
151
152                 if (HANDLE_REBOOT == action) {
153                         if (strv_extend(&argv_utmp, "reboot") < 0)
154                                 return log_oom();
155                 } else {
156                         if (strv_extend(&argv_utmp, "shutdown") < 0)
157                                 return log_oom();
158                 }
159
160                  /* This comes from our patched update-utmp/update-utmp.c */
161                 update_utmp(2, argv_utmp, m->bus);
162                 strv_free(argv_utmp);
163         }
164
165         /* Now perform the requested action */
166         r = shutdown_or_sleep(m, action);
167
168         /* no more pending actions, whether this failed or not */
169         m->pending_action = HANDLE_IGNORE;
170
171         /* As elogind can not rely on a systemd manager to call all
172          * sleeping processes to wake up, we have to tell them all
173          * by ourselves. */
174         if (w == INHIBIT_SLEEP) {
175                 (void) send_prepare_for(m, w, false);
176                 m->action_what = 0;
177         } else
178                 m->action_what = w;
179
180         if (r < 0)
181                 return r;
182
183         /* Make sure the lid switch is ignored for a while */
184         manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
185
186         return 0;
187 }
188
189 static int delay_shutdown_or_sleep(
190                 Manager *m,
191                 InhibitWhat w,
192                 HandleAction action) {
193
194         int r;
195         usec_t timeout_val;
196
197         assert(m);
198         assert(w >= 0);
199         assert(w < _INHIBIT_WHAT_MAX);
200
201         timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
202
203         if (m->inhibit_timeout_source) {
204                 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
205                 if (r < 0)
206                         return log_error_errno(r, "sd_event_source_set_time() failed: %m");
207
208                 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
209                 if (r < 0)
210                         return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
211         } else {
212                 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
213                                       timeout_val, 0, manager_inhibit_timeout_handler, m);
214                 if (r < 0)
215                         return r;
216         }
217
218         m->pending_action = action;
219         m->action_what = w;
220
221         return 0;
222 }
223
224 int bus_manager_shutdown_or_sleep_now_or_later(
225                 Manager *m,
226                 HandleAction action,
227                 InhibitWhat w,
228                 sd_bus_error *error) {
229
230         bool delayed;
231         int r;
232
233         assert(m);
234         assert(w >= 0);
235         assert(w <= _INHIBIT_WHAT_MAX);
236
237         /* Tell everybody to prepare for shutdown/sleep */
238         (void) send_prepare_for(m, w, true);
239
240         delayed =
241                 m->inhibit_delay_max > 0 &&
242                 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
243
244         log_debug_elogind("%s called for %s (%sdelayed)", __FUNCTION__,
245                           handle_action_to_string(action),
246                           delayed ? "" : "NOT ");
247
248         if (delayed)
249                 /* Shutdown is delayed, keep in mind what we
250                  * want to do, and start a timeout */
251                 r = delay_shutdown_or_sleep(m, w, action);
252         else
253                 /* Shutdown is not delayed, execute it
254                  * immediately */
255                 r = execute_shutdown_or_sleep(m, w, action, error);
256
257         return r;
258 }
259
260 static int method_do_shutdown_or_sleep(
261                 Manager *m,
262                 sd_bus_message *message,
263                 HandleAction sleep_action,
264                 InhibitWhat w,
265                 const char *action,
266                 const char *action_multiple_sessions,
267                 const char *action_ignore_inhibit,
268                 const char *sleep_verb,
269                 sd_bus_error *error) {
270
271         int interactive, r;
272
273         assert(m);
274         assert(message);
275         assert(w >= 0);
276         assert(w <= _INHIBIT_WHAT_MAX);
277
278         r = sd_bus_message_read(message, "b", &interactive);
279         if (r < 0)
280                 return r;
281
282         log_debug_elogind("%s called with action '%s', sleep '%s' (%sinteractive)",
283                           __FUNCTION__, action, sleep_verb,
284                           interactive ? "" : "NOT ");
285
286         /* Don't allow multiple jobs being executed at the same time */
287         if (m->action_what)
288                 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
289
290         if (sleep_verb) {
291                 r = can_sleep(m, sleep_verb);
292                 if (r < 0)
293                         return r;
294
295                 if (r == 0)
296                         return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
297         }
298
299         if (IN_SET(sleep_action, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
300                 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
301                                           action_ignore_inhibit, error);
302                 log_debug_elogind("verify_shutdown_creds() returned %d", r);
303                 if (r != 0)
304                         return r;
305         }
306
307         r = bus_manager_shutdown_or_sleep_now_or_later(m, sleep_action, w, error);
308         if (r < 0)
309                 return r;
310
311         return sd_bus_reply_method_return(message, NULL);
312 }
313
314 int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
315         Manager *m = userdata;
316
317         log_debug_elogind("%s called", __FUNCTION__);
318
319         return method_do_shutdown_or_sleep(
320                         m, message,
321                         HANDLE_POWEROFF,
322                         INHIBIT_SHUTDOWN,
323                         "org.freedesktop.login1.power-off",
324                         "org.freedesktop.login1.power-off-multiple-sessions",
325                         "org.freedesktop.login1.power-off-ignore-inhibit",
326                         NULL,
327                         error);
328 }
329
330 int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
331         Manager *m = userdata;
332
333         log_debug_elogind("%s called", __FUNCTION__);
334
335         return method_do_shutdown_or_sleep(
336                         m, message,
337                         HANDLE_REBOOT,
338                         INHIBIT_SHUTDOWN,
339                         "org.freedesktop.login1.reboot",
340                         "org.freedesktop.login1.reboot-multiple-sessions",
341                         "org.freedesktop.login1.reboot-ignore-inhibit",
342                         NULL,
343                         error);
344 }
345
346 int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
347         Manager *m = userdata;
348
349         log_debug_elogind("%s called", __FUNCTION__);
350
351         return method_do_shutdown_or_sleep(
352                         m, message,
353                         HANDLE_HALT,
354                         INHIBIT_SHUTDOWN,
355                         "org.freedesktop.login1.halt",
356                         "org.freedesktop.login1.halt-multiple-sessions",
357                         "org.freedesktop.login1.halt-ignore-inhibit",
358                         NULL,
359                         error);
360 }
361
362 int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
363         Manager *m = userdata;
364
365         log_debug_elogind("%s called", __FUNCTION__);
366
367         return method_do_shutdown_or_sleep(
368                         m, message,
369                         HANDLE_SUSPEND,
370                         INHIBIT_SLEEP,
371                         "org.freedesktop.login1.suspend",
372                         "org.freedesktop.login1.suspend-multiple-sessions",
373                         "org.freedesktop.login1.suspend-ignore-inhibit",
374                         "suspend",
375                         error);
376 }
377
378 int manager_scheduled_shutdown_handler(
379                         sd_event_source *s,
380                         uint64_t usec,
381                         void *userdata) {
382
383         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
384         Manager *m = userdata;
385         HandleAction action;
386         int r;
387
388         assert(m);
389
390         if (isempty(m->scheduled_shutdown_type))
391                 return 0;
392
393         if (streq(m->scheduled_shutdown_type, "halt"))
394                 action = HANDLE_HALT;
395         else if (streq(m->scheduled_shutdown_type, "poweroff"))
396                 action = HANDLE_POWEROFF;
397         else
398                 action = HANDLE_REBOOT;
399
400         /* Don't allow multiple jobs being executed at the same time */
401         if (m->action_what) {
402                 r = -EALREADY;
403                 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress",
404                           m->scheduled_shutdown_type);
405                 goto error;
406         }
407
408         if (m->shutdown_dry_run) {
409                 /* We do not process delay inhibitors here.  Otherwise, we
410                  * would have to be considered "in progress" (like the check
411                  * above) for some seconds after our admin has seen the final
412                  * wall message. */
413
414                 bus_manager_log_shutdown(m, INHIBIT_SHUTDOWN, action);
415                 log_info("Running in dry run, suppressing action.");
416                 reset_scheduled_shutdown(m);
417
418                 return 0;
419         }
420
421         r = execute_shutdown_or_sleep(m, 0, action, &error);
422         if (r < 0) {
423                 log_error_errno(r, "Scheduled shutdown to %s failed: %m",
424                                        m->scheduled_shutdown_type);
425                 goto error;
426         }
427
428         return 0;
429
430 error:
431         reset_scheduled_shutdown(m);
432         return r;
433 }
434
435 int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
436         Manager *m = userdata;
437
438         log_debug_elogind("%s called", __FUNCTION__);
439
440         return method_do_shutdown_or_sleep(
441                         m, message,
442                         HANDLE_HIBERNATE,
443                         INHIBIT_SLEEP,
444                         "org.freedesktop.login1.hibernate",
445                         "org.freedesktop.login1.hibernate-multiple-sessions",
446                         "org.freedesktop.login1.hibernate-ignore-inhibit",
447                         "hibernate",
448                         error);
449 }
450
451 int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
452         Manager *m = userdata;
453
454         log_debug_elogind("%s called", __FUNCTION__);
455
456         return method_do_shutdown_or_sleep(
457                         m, message,
458                         HANDLE_HYBRID_SLEEP,
459                         INHIBIT_SLEEP,
460                         "org.freedesktop.login1.hibernate",
461                         "org.freedesktop.login1.hibernate-multiple-sessions",
462                         "org.freedesktop.login1.hibernate-ignore-inhibit",
463                         "hybrid-sleep",
464                         error);
465 }