chiark / gitweb /
util-lib: make sure fd_check_fstype() opens files with O_CLOEXEC
[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 "process-util.h"
26 #include "sd-messages.h"
27 #include "sleep-config.h"
28 #include "string-util.h"
29 #include "strv.h"
30 #include "update-utmp.h"
31 #include "user-util.h"
32
33
34 int have_multiple_sessions(
35                 Manager *m,
36                 uid_t uid) {
37
38         Session *session;
39         Iterator i;
40
41         assert(m);
42
43         /* Check for other users' sessions. Greeter sessions do not
44          * count, and non-login sessions do not count either. */
45         HASHMAP_FOREACH(session, m->sessions, i)
46                 if (session->class == SESSION_USER &&
47                     session->user->uid != uid)
48                         return true;
49
50         return false;
51 }
52
53 static int bus_manager_log_shutdown(
54                 Manager *m,
55                 InhibitWhat w,
56                 HandleAction action) {
57
58          const char *p, *q;
59
60          assert(m);
61
62          if (w != INHIBIT_SHUTDOWN)
63                  return 0;
64
65          switch (action) {
66          case HANDLE_POWEROFF:
67                  p = "MESSAGE=System is powering down.";
68                  q = "SHUTDOWN=power-off";
69                  break;
70          case HANDLE_HALT:
71                  p = "MESSAGE=System is halting.";
72                  q = "SHUTDOWN=halt";
73                  break;
74          case HANDLE_REBOOT:
75                  p = "MESSAGE=System is rebooting.";
76                  q = "SHUTDOWN=reboot";
77                  break;
78          case HANDLE_KEXEC:
79                  p = "MESSAGE=System is rebooting with kexec.";
80                  q = "SHUTDOWN=kexec";
81                  break;
82          default:
83                  p = "MESSAGE=System is shutting down.";
84                  q = NULL;
85          }
86
87         if (isempty(m->wall_message))
88                 p = strjoina(p, ".");
89         else
90                 p = strjoina(p, " (", m->wall_message, ").");
91
92         return log_struct(LOG_NOTICE,
93                           LOG_MESSAGE_ID(SD_MESSAGE_SHUTDOWN),
94                           p,
95                           q,
96                           NULL);
97 }
98
99 static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) {
100
101         static const char * const signal_name[_INHIBIT_WHAT_MAX] = {
102                 [INHIBIT_SHUTDOWN] = "PrepareForShutdown",
103                 [INHIBIT_SLEEP] = "PrepareForSleep"
104         };
105
106         int active = _active;
107
108         assert(m);
109         assert(w >= 0);
110         assert(w < _INHIBIT_WHAT_MAX);
111         assert(signal_name[w]);
112
113         return sd_bus_emit_signal(m->bus,
114                                   "/org/freedesktop/login1",
115                                   "org.freedesktop.login1.Manager",
116                                   signal_name[w],
117                                   "b",
118                                   active);
119 }
120
121 static int execute_shutdown_or_sleep(
122                 Manager *m,
123                 InhibitWhat w,
124                 HandleAction action,
125                 sd_bus_error *error) {
126
127         char** argv_utmp = NULL;
128         int r;
129
130         assert(m);
131         assert(w >= 0);
132         assert(w < _INHIBIT_WHAT_MAX);
133
134         bus_manager_log_shutdown(m, w, action);
135
136         if (IN_SET(action, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
137
138                 /* As we have no systemd update-utmp daemon running, we have to
139                  * set the relevant utmp/wtmp entries ourselves.
140                  */
141
142                 if (strv_extend(&argv_utmp, "elogind") < 0)
143                         return log_oom();
144
145                 if (HANDLE_REBOOT == action) {
146                         if (strv_extend(&argv_utmp, "reboot") < 0)
147                                 return log_oom();
148                 } else {
149                         if (strv_extend(&argv_utmp, "shutdown") < 0)
150                                 return log_oom();
151                 }
152
153                  /* This comes from our patched update-utmp/update-utmp.c */
154                 update_utmp(2, argv_utmp, m->bus);
155                 strv_free(argv_utmp);
156         }
157
158         /* Now perform the requested action */
159         r = shutdown_or_sleep(m, action);
160
161         /* no more pending actions, whether this failed or not */
162         m->pending_action = HANDLE_IGNORE;
163
164         /* As elogind can not rely on a systemd manager to call all
165          * sleeping processes to wake up, we have to tell them all
166          * by ourselves. */
167         if (w == INHIBIT_SLEEP) {
168                 send_prepare_for(m, w, false);
169                 m->action_what = 0;
170         } else
171                 m->action_what = w;
172
173         if (r < 0)
174                 return r;
175
176         /* Make sure the lid switch is ignored for a while */
177         manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
178
179         return 0;
180 }
181
182 int manager_dispatch_delayed(Manager *manager, bool timeout) {
183
184         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
185         Inhibitor *offending = NULL;
186         int r;
187
188         assert(manager);
189
190         if ( (0 == manager->action_what) || (HANDLE_IGNORE == manager->pending_action) )
191                 return 0;
192
193         if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
194                 _cleanup_free_ char *comm = NULL, *u = NULL;
195
196                 if (!timeout)
197                         return 0;
198
199                 (void) get_process_comm(offending->pid, &comm);
200                 u = uid_to_name(offending->uid);
201
202                 log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.",
203                            offending->uid, strna(u),
204                            offending->pid, strna(comm));
205         }
206
207         /* Actually do the operation */
208         r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
209         if (r < 0) {
210                 log_warning("Failed to send delayed message: %s", bus_error_message(&error, r));
211
212                 manager->pending_action = HANDLE_IGNORE;
213                 manager->action_what    = 0;
214                 /* It is not a critical error for elogind if suspending fails */
215         }
216
217         return 1;
218 }
219
220 static int manager_inhibit_timeout_handler(
221                         sd_event_source *s,
222                         uint64_t usec,
223                         void *userdata) {
224
225         Manager *manager = userdata;
226         int r;
227
228         assert(manager);
229         assert(manager->inhibit_timeout_source == s);
230
231         r = manager_dispatch_delayed(manager, true);
232         return (r < 0) ? r : 0;
233 }
234
235 static int delay_shutdown_or_sleep(
236                 Manager *m,
237                 InhibitWhat w,
238                 HandleAction action) {
239
240         int r;
241         usec_t timeout_val;
242
243         assert(m);
244         assert(w >= 0);
245         assert(w < _INHIBIT_WHAT_MAX);
246
247         timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
248
249         if (m->inhibit_timeout_source) {
250                 r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val);
251                 if (r < 0)
252                         return log_error_errno(r, "sd_event_source_set_time() failed: %m");
253
254                 r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT);
255                 if (r < 0)
256                         return log_error_errno(r, "sd_event_source_set_enabled() failed: %m");
257         } else {
258                 r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC,
259                                       timeout_val, 0, manager_inhibit_timeout_handler, m);
260                 if (r < 0)
261                         return r;
262         }
263
264         m->pending_action = action;
265         m->action_what = w;
266
267         return 0;
268 }
269
270 int bus_manager_shutdown_or_sleep_now_or_later(
271                 Manager *m,
272                 HandleAction action,
273                 InhibitWhat w,
274                 sd_bus_error *error) {
275
276         bool delayed;
277         int r;
278
279         assert(m);
280         assert(w >= 0);
281         assert(w <= _INHIBIT_WHAT_MAX);
282
283         /* Tell everybody to prepare for shutdown/sleep */
284         send_prepare_for(m, w, true);
285
286         delayed =
287                 m->inhibit_delay_max > 0 &&
288                 manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
289
290         log_debug_elogind("%s called for %s (%sdelayed)", __FUNCTION__,
291                           handle_action_to_string(action),
292                           delayed ? "" : "NOT ");
293
294         if (delayed)
295                 /* Shutdown is delayed, keep in mind what we
296                  * want to do, and start a timeout */
297                 r = delay_shutdown_or_sleep(m, w, action);
298         else
299                 /* Shutdown is not delayed, execute it
300                  * immediately */
301                 r = execute_shutdown_or_sleep(m, w, action, error);
302
303         return r;
304 }
305
306 int verify_shutdown_creds(
307                 Manager *m,
308                 sd_bus_message *message,
309                 InhibitWhat w,
310                 bool interactive,
311                 const char *action,
312                 const char *action_multiple_sessions,
313                 const char *action_ignore_inhibit,
314                 sd_bus_error *error) {
315
316         _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
317         bool multiple_sessions, blocked;
318         uid_t uid;
319         int r;
320
321         assert(m);
322         assert(message);
323         assert(w >= 0);
324         assert(w <= _INHIBIT_WHAT_MAX);
325
326         r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds);
327         if (r < 0)
328                 return r;
329
330         r = sd_bus_creds_get_euid(creds, &uid);
331         if (r < 0)
332                 return r;
333
334         r = have_multiple_sessions(m, uid);
335         if (r < 0)
336                 return r;
337
338         multiple_sessions = r > 0;
339         blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL);
340
341         if (multiple_sessions && action_multiple_sessions) {
342                 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
343                 if (r < 0)
344                         return r;
345                 if (r == 0)
346                         return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
347         }
348
349         if (blocked && action_ignore_inhibit) {
350                 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
351                 if (r < 0)
352                         return r;
353                 if (r == 0)
354                         return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
355         }
356
357         if (!multiple_sessions && !blocked && action) {
358                 r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error);
359                 if (r < 0)
360                         return r;
361                 if (r == 0)
362                         return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
363         }
364
365         return 0;
366 }
367
368 static int method_do_shutdown_or_sleep(
369                 Manager *m,
370                 sd_bus_message *message,
371                 HandleAction sleep_action,
372                 InhibitWhat w,
373                 const char *action,
374                 const char *action_multiple_sessions,
375                 const char *action_ignore_inhibit,
376                 const char *sleep_verb,
377                 sd_bus_error *error) {
378
379         int interactive, r;
380
381         assert(m);
382         assert(message);
383         assert(w >= 0);
384         assert(w <= _INHIBIT_WHAT_MAX);
385
386         r = sd_bus_message_read(message, "b", &interactive);
387         if (r < 0)
388                 return r;
389
390         log_debug_elogind("%s called with action '%s', sleep '%s' (%sinteractive)",
391                           __FUNCTION__, action, sleep_verb,
392                           interactive ? "" : "NOT ");
393
394         /* Don't allow multiple jobs being executed at the same time */
395         if (m->action_what)
396                 return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
397
398         if (sleep_verb) {
399                 r = can_sleep(m, sleep_verb);
400                 if (r < 0)
401                         return r;
402
403                 if (r == 0)
404                         return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported");
405         }
406
407         if (IN_SET(sleep_action, HANDLE_HALT, HANDLE_POWEROFF, HANDLE_REBOOT)) {
408                 r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
409                                           action_ignore_inhibit, error);
410                 log_debug_elogind("verify_shutdown_creds() returned %d", r);
411                 if (r != 0)
412                         return r;
413         }
414
415         r = bus_manager_shutdown_or_sleep_now_or_later(m, sleep_action, w, error);
416         if (r < 0)
417                 return r;
418
419         return sd_bus_reply_method_return(message, NULL);
420 }
421
422 int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
423         Manager *m = userdata;
424
425         log_debug_elogind("%s called", __FUNCTION__);
426
427         return method_do_shutdown_or_sleep(
428                         m, message,
429                         HANDLE_POWEROFF,
430                         INHIBIT_SHUTDOWN,
431                         "org.freedesktop.login1.power-off",
432                         "org.freedesktop.login1.power-off-multiple-sessions",
433                         "org.freedesktop.login1.power-off-ignore-inhibit",
434                         NULL,
435                         error);
436 }
437
438 int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
439         Manager *m = userdata;
440
441         log_debug_elogind("%s called", __FUNCTION__);
442
443         return method_do_shutdown_or_sleep(
444                         m, message,
445                         HANDLE_REBOOT,
446                         INHIBIT_SHUTDOWN,
447                         "org.freedesktop.login1.reboot",
448                         "org.freedesktop.login1.reboot-multiple-sessions",
449                         "org.freedesktop.login1.reboot-ignore-inhibit",
450                         NULL,
451                         error);
452 }
453
454 int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
455         Manager *m = userdata;
456
457         log_debug_elogind("%s called", __FUNCTION__);
458
459         return method_do_shutdown_or_sleep(
460                         m, message,
461                         HANDLE_SUSPEND,
462                         INHIBIT_SLEEP,
463                         "org.freedesktop.login1.suspend",
464                         "org.freedesktop.login1.suspend-multiple-sessions",
465                         "org.freedesktop.login1.suspend-ignore-inhibit",
466                         "suspend",
467                         error);
468 }
469
470 int manager_scheduled_shutdown_handler(
471                         sd_event_source *s,
472                         uint64_t usec,
473                         void *userdata) {
474
475         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
476         Manager *m = userdata;
477         HandleAction action;
478         int r;
479
480         assert(m);
481
482         if (isempty(m->scheduled_shutdown_type))
483                 return 0;
484
485         if (streq(m->scheduled_shutdown_type, "halt"))
486                 action = HANDLE_HALT;
487         else if (streq(m->scheduled_shutdown_type, "poweroff"))
488                 action = HANDLE_POWEROFF;
489         else
490                 action = HANDLE_REBOOT;
491
492         r = execute_shutdown_or_sleep(m, 0, action, &error);
493         if (r < 0)
494                 return log_error_errno(r, "Unable to execute transition to %s: %m", m->scheduled_shutdown_type);
495
496         return 0;
497 }
498
499 int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
500         Manager *m = userdata;
501
502         log_debug_elogind("%s called", __FUNCTION__);
503
504         return method_do_shutdown_or_sleep(
505                         m, message,
506                         HANDLE_HIBERNATE,
507                         INHIBIT_SLEEP,
508                         "org.freedesktop.login1.hibernate",
509                         "org.freedesktop.login1.hibernate-multiple-sessions",
510                         "org.freedesktop.login1.hibernate-ignore-inhibit",
511                         "hibernate",
512                         error);
513 }
514
515 int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
516         Manager *m = userdata;
517
518         log_debug_elogind("%s called", __FUNCTION__);
519
520         return method_do_shutdown_or_sleep(
521                         m, message,
522                         HANDLE_HYBRID_SLEEP,
523                         INHIBIT_SLEEP,
524                         "org.freedesktop.login1.hibernate",
525                         "org.freedesktop.login1.hibernate-multiple-sessions",
526                         "org.freedesktop.login1.hibernate-ignore-inhibit",
527                         "hybrid-sleep",
528                         error);
529 }