chiark / gitweb /
login/elogind-dbus.c: Sync the following methods from systemctl.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 static 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 int manager_dispatch_delayed(Manager *manager, bool timeout) {
190
191         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
192         Inhibitor *offending = NULL;
193         int r;
194
195         assert(manager);
196
197         if ( (0 == manager->action_what) || (HANDLE_IGNORE == manager->pending_action) )
198                 return 0;
199
200         if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
201                 _cleanup_free_ char *comm = NULL, *u = NULL;
202
203                 if (!timeout)
204                         return 0;
205
206                 (void) get_process_comm(offending->pid, &comm);
207                 u = uid_to_name(offending->uid);
208
209                 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
210                            offending->uid, strna(u),
211                            offending->pid, strna(comm));
212         }
213
214         /* Actually do the operation */
215         r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
216         if (r < 0) {
217                 log_warning("Failed to send delayed message: %s", bus_error_message(&error, r));
218
219                 manager->pending_action = HANDLE_IGNORE;
220                 manager->action_what    = 0;
221                 /* It is not a critical error for elogind if suspending fails */
222         }
223
224         return 1;
225 }
226
227 static int delay_shutdown_or_sleep(
228                 Manager *m,
229                 InhibitWhat w,
230                 HandleAction action) {
231
232         int r;
233         usec_t timeout_val;
234
235         assert(m);
236         assert(w >= 0);
237         assert(w < _INHIBIT_WHAT_MAX);
238
239         timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
240
241         if (m->inhibit_timeout_source) {
242                 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
243                 if (r < 0)
244                         return log_error_errno(r, "sd_event_source_set_time() failed: %m");
245
246                 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
247                 if (r < 0)
248                         return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
249         } else {
250                 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
251                                       timeout_val, 0, manager_inhibit_timeout_handler, m);
252                 if (r < 0)
253                         return r;
254         }
255
256         m->pending_action = action;
257         m->action_what = w;
258
259         return 0;
260 }
261
262 int bus_manager_shutdown_or_sleep_now_or_later(
263                 Manager *m,
264                 HandleAction action,
265                 InhibitWhat w,
266                 sd_bus_error *error) {
267
268         bool delayed;
269         int r;
270
271         assert(m);
272         assert(w >= 0);
273         assert(w <= _INHIBIT_WHAT_MAX);
274
275         /* Tell everybody to prepare for shutdown/sleep */
276         (void) send_prepare_for(m, w, true);
277
278         delayed =
279                 m->inhibit_delay_max > 0 &&
280                 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
281
282         log_debug_elogind("%s called for %s (%sdelayed)", __FUNCTION__,
283                           handle_action_to_string(action),
284                           delayed ? "" : "NOT ");
285
286         if (delayed)
287                 /* Shutdown is delayed, keep in mind what we
288                  * want to do, and start a timeout */
289                 r = delay_shutdown_or_sleep(m, w, action);
290         else
291                 /* Shutdown is not delayed, execute it
292                  * immediately */
293                 r = execute_shutdown_or_sleep(m, w, action, error);
294
295         return r;
296 }
297
298 static int method_do_shutdown_or_sleep(
299                 Manager *m,
300                 sd_bus_message *message,
301                 HandleAction sleep_action,
302                 InhibitWhat w,
303                 const char *action,
304                 const char *action_multiple_sessions,
305                 const char *action_ignore_inhibit,
306                 const char *sleep_verb,
307                 sd_bus_error *error) {
308
309         int interactive, r;
310
311         assert(m);
312         assert(message);
313         assert(w >= 0);
314         assert(w <= _INHIBIT_WHAT_MAX);
315
316         r = sd_bus_message_read(message, "b", &interactive);
317         if (r < 0)
318                 return r;
319
320         log_debug_elogind("%s called with action '%s', sleep '%s' (%sinteractive)",
321                           __FUNCTION__, action, sleep_verb,
322                           interactive ? "" : "NOT ");
323
324         /* Don't allow multiple jobs being executed at the same time */
325         if (m->action_what)
326                 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
327
328         if (sleep_verb) {
329                 r = can_sleep(m, sleep_verb);
330                 if (r < 0)
331                         return r;
332
333                 if (r == 0)
334                         return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
335         }
336
337         if (IN_SET(sleep_action, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
338                 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
339                                           action_ignore_inhibit, error);
340                 log_debug_elogind("verify_shutdown_creds() returned %d", r);
341                 if (r != 0)
342                         return r;
343         }
344
345         r = bus_manager_shutdown_or_sleep_now_or_later(m, sleep_action, w, error);
346         if (r < 0)
347                 return r;
348
349         return sd_bus_reply_method_return(message, NULL);
350 }
351
352 int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
353         Manager *m = userdata;
354
355         log_debug_elogind("%s called", __FUNCTION__);
356
357         return method_do_shutdown_or_sleep(
358                         m, message,
359                         HANDLE_POWEROFF,
360                         INHIBIT_SHUTDOWN,
361                         "org.freedesktop.login1.power-off",
362                         "org.freedesktop.login1.power-off-multiple-sessions",
363                         "org.freedesktop.login1.power-off-ignore-inhibit",
364                         NULL,
365                         error);
366 }
367
368 int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
369         Manager *m = userdata;
370
371         log_debug_elogind("%s called", __FUNCTION__);
372
373         return method_do_shutdown_or_sleep(
374                         m, message,
375                         HANDLE_REBOOT,
376                         INHIBIT_SHUTDOWN,
377                         "org.freedesktop.login1.reboot",
378                         "org.freedesktop.login1.reboot-multiple-sessions",
379                         "org.freedesktop.login1.reboot-ignore-inhibit",
380                         NULL,
381                         error);
382 }
383
384 int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) {
385         Manager *m = userdata;
386
387         log_debug_elogind("%s called", __FUNCTION__);
388
389         return method_do_shutdown_or_sleep(
390                         m, message,
391                         HANDLE_HALT,
392                         INHIBIT_SHUTDOWN,
393                         "org.freedesktop.login1.halt",
394                         "org.freedesktop.login1.halt-multiple-sessions",
395                         "org.freedesktop.login1.halt-ignore-inhibit",
396                         NULL,
397                         error);
398 }
399
400 int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
401         Manager *m = userdata;
402
403         log_debug_elogind("%s called", __FUNCTION__);
404
405         return method_do_shutdown_or_sleep(
406                         m, message,
407                         HANDLE_SUSPEND,
408                         INHIBIT_SLEEP,
409                         "org.freedesktop.login1.suspend",
410                         "org.freedesktop.login1.suspend-multiple-sessions",
411                         "org.freedesktop.login1.suspend-ignore-inhibit",
412                         "suspend",
413                         error);
414 }
415
416 int manager_scheduled_shutdown_handler(
417                         sd_event_source *s,
418                         uint64_t usec,
419                         void *userdata) {
420
421         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
422         Manager *m = userdata;
423         HandleAction action;
424         int r;
425
426         assert(m);
427
428         if (isempty(m->scheduled_shutdown_type))
429                 return 0;
430
431         if (streq(m->scheduled_shutdown_type, "halt"))
432                 action = HANDLE_HALT;
433         else if (streq(m->scheduled_shutdown_type, "poweroff"))
434                 action = HANDLE_POWEROFF;
435         else
436                 action = HANDLE_REBOOT;
437
438         /* Don't allow multiple jobs being executed at the same time */
439         if (m->action_what) {
440                 r = -EALREADY;
441                 log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress",
442                           m->scheduled_shutdown_type);
443                 goto error;
444         }
445
446         if (m->shutdown_dry_run) {
447                 /* We do not process delay inhibitors here.  Otherwise, we
448                  * would have to be considered "in progress" (like the check
449                  * above) for some seconds after our admin has seen the final
450                  * wall message. */
451
452                 bus_manager_log_shutdown(m, target);
453                 log_info("Running in dry run, suppressing action.");
454                 reset_scheduled_shutdown(m);
455
456                 return 0;
457         }
458
459         r = execute_shutdown_or_sleep(m, 0, action, &error);
460         if (r < 0) {
461                 log_error_errno(r, "Scheduled shutdown to %s failed: %m",
462                                        m->scheduled_shutdown_type);
463                 goto error;
464         }
465
466         return 0;
467
468 error:
469         reset_scheduled_shutdown(m);
470         return r;
471 }
472
473 int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
474         Manager *m = userdata;
475
476         log_debug_elogind("%s called", __FUNCTION__);
477
478         return method_do_shutdown_or_sleep(
479                         m, message,
480                         HANDLE_HIBERNATE,
481                         INHIBIT_SLEEP,
482                         "org.freedesktop.login1.hibernate",
483                         "org.freedesktop.login1.hibernate-multiple-sessions",
484                         "org.freedesktop.login1.hibernate-ignore-inhibit",
485                         "hibernate",
486                         error);
487 }
488
489 int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
490         Manager *m = userdata;
491
492         log_debug_elogind("%s called", __FUNCTION__);
493
494         return method_do_shutdown_or_sleep(
495                         m, message,
496                         HANDLE_HYBRID_SLEEP,
497                         INHIBIT_SLEEP,
498                         "org.freedesktop.login1.hibernate",
499                         "org.freedesktop.login1.hibernate-multiple-sessions",
500                         "org.freedesktop.login1.hibernate-ignore-inhibit",
501                         "hybrid-sleep",
502                         error);
503 }