chiark / gitweb /
Prep v229.6: Send wakeup call to suspended processes.
[elogind.git] / src / login / eloginctl.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-error.h"
22 #include "bus-util.h"
23 #include "eloginctl.h"
24 #include "logind-action.h"
25 #include "parse-util.h"
26 #include "process-util.h"
27 #include "sd-login.h"
28 #include "spawn-polkit-agent.h"
29 #include "string-util.h"
30 #include "strv.h"
31 #include "terminal-util.h"
32 #include "user-util.h"
33 #include "virt.h"
34
35
36 elogind_action arg_action            = _ACTION_INVALID;
37 bool           arg_ask_password      = true;
38 bool           arg_ignore_inhibitors = false;
39 bool           arg_no_wall           = false;
40 BusTransport   arg_transport         = BUS_TRANSPORT_LOCAL;
41 char**         arg_wall              = NULL;
42 usec_t         arg_when              = 0;
43
44 static const struct {
45         HandleAction action;
46         const char*  verb;
47 } action_table[_ACTION_MAX] = {
48         [ACTION_POWEROFF]     = { HANDLE_POWEROFF,     "poweroff",    },
49         [ACTION_REBOOT]       = { HANDLE_REBOOT,       "reboot",      },
50         [ACTION_SUSPEND]      = { HANDLE_SUSPEND,      "suspend",     },
51         [ACTION_HIBERNATE]    = { HANDLE_HIBERNATE,    "hibernate",   },
52         [ACTION_HYBRID_SLEEP] = { HANDLE_HYBRID_SLEEP, "hybrid-sleep" },
53 };
54
55 static int elogind_set_wall_message(sd_bus* bus, const char* msg);
56
57 static enum elogind_action verb_to_action(const char *verb) {
58         enum elogind_action i;
59
60         for (i = _ACTION_INVALID; i < _ACTION_MAX; i++)
61                 if (streq_ptr(action_table[i].verb, verb))
62                         return i;
63
64         return _ACTION_INVALID;
65 }
66
67 static int check_inhibitors(sd_bus* bus, enum elogind_action a) {
68         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
69         _cleanup_strv_free_ char **sessions = NULL;
70         const char *what, *who, *why, *mode;
71         uint32_t uid, pid;
72         unsigned c = 0;
73         char **s;
74         int r;
75
76         if (arg_ignore_inhibitors)
77                 return 0;
78
79         if (arg_when > 0)
80                 return 0;
81
82         if (geteuid() == 0)
83                 return 0;
84
85         if (!on_tty())
86                 return 0;
87
88         r = sd_bus_call_method(
89                         bus,
90                         "org.freedesktop.login1",
91                         "/org/freedesktop/login1",
92                         "org.freedesktop.login1.Manager",
93                         "ListInhibitors",
94                         NULL,
95                         &reply,
96                         NULL);
97         if (r < 0)
98                 /* If logind is not around, then there are no inhibitors... */
99                 return 0;
100
101         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
102         if (r < 0)
103                 return bus_log_parse_error(r);
104
105         while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
106                 _cleanup_free_ char *comm = NULL, *user = NULL;
107                 _cleanup_strv_free_ char **sv = NULL;
108
109                 if (!streq(mode, "block"))
110                         continue;
111
112                 sv = strv_split(what, ":");
113                 if (!sv)
114                         return log_oom();
115
116                 if ((pid_t) pid < 0)
117                         return log_error_errno(ERANGE, "Bad PID %"PRIu32": %m", pid);
118
119                 if (!strv_contains(sv,
120                                    IN_SET(a,
121                                           ACTION_HALT,
122                                           ACTION_POWEROFF,
123                                           ACTION_REBOOT,
124                                           ACTION_KEXEC) ? "shutdown" : "sleep"))
125                         continue;
126
127                 get_process_comm(pid, &comm);
128                 user = uid_to_name(uid);
129
130                 log_warning("Operation inhibited by \"%s\" (PID "PID_FMT" \"%s\", user %s), reason is \"%s\".",
131                             who, (pid_t) pid, strna(comm), strna(user), why);
132
133                 c++;
134         }
135         if (r < 0)
136                 return bus_log_parse_error(r);
137
138         r = sd_bus_message_exit_container(reply);
139         if (r < 0)
140                 return bus_log_parse_error(r);
141
142         /* Check for current sessions */
143         sd_get_sessions(&sessions);
144         STRV_FOREACH(s, sessions) {
145                 _cleanup_free_ char *type = NULL, *tty = NULL, *seat = NULL, *user = NULL, *service = NULL, *class = NULL;
146
147                 if (sd_session_get_uid(*s, &uid) < 0 || uid == getuid())
148                         continue;
149
150                 if (sd_session_get_class(*s, &class) < 0 || !streq(class, "user"))
151                         continue;
152
153                 if (sd_session_get_type(*s, &type) < 0 || (!streq(type, "x11") && !streq(type, "tty")))
154                         continue;
155
156                 sd_session_get_tty(*s, &tty);
157                 sd_session_get_seat(*s, &seat);
158                 sd_session_get_service(*s, &service);
159                 user = uid_to_name(uid);
160
161                 log_warning("User %s is logged in on %s.", strna(user), isempty(tty) ? (isempty(seat) ? strna(service) : seat) : tty);
162                 c++;
163         }
164
165         if (c <= 0)
166                 return 0;
167
168         log_error("Please retry operation after closing inhibitors and logging out other users.\nAlternatively, ignore inhibitors and users with 'loginctl -i %s'.",
169                   action_table[a].verb);
170
171         return -EPERM;
172 }
173
174 int elogind_cancel_shutdown(sd_bus *bus) {
175         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
176         int r;
177
178         r = sd_bus_call_method(
179                         bus,
180                         "org.freedesktop.login1",
181                         "/org/freedesktop/login1",
182                         "org.freedesktop.login1.Manager",
183                         "CancelScheduledShutdown",
184                         &error,
185                         NULL, NULL);
186         if (r < 0)
187                 return log_warning_errno(r, "Failed to talk to elogind, shutdown hasn't been cancelled: %s", bus_error_message(&error, r));
188
189         return 0;
190 }
191
192 void elogind_cleanup(void) {
193         polkit_agent_close();
194         strv_free(arg_wall);
195 }
196
197 static int elogind_reboot(sd_bus *bus, enum elogind_action a) {
198         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
199         const char *method  = NULL;
200         int r;
201         static const char *table[_ACTION_MAX] = {
202                 [ACTION_REBOOT]          = "The system is going down for reboot NOW!",
203                 [ACTION_POWEROFF]        = "The system is going down for power-off NOW!"
204         };
205
206         if (!bus)
207                 return -EIO;
208
209         polkit_agent_open_if_enabled();
210
211         switch (a) {
212
213         case ACTION_POWEROFF:
214                 method = "PowerOff";
215                 break;
216
217         case ACTION_REBOOT:
218                 method = "Reboot";
219                 break;
220
221         case ACTION_SUSPEND:
222                 method = "Suspend";
223                 break;
224
225         case ACTION_HIBERNATE:
226                 method = "Hibernate";
227                 break;
228
229         case ACTION_HYBRID_SLEEP:
230                 method = "HybridSleep";
231                 break;
232
233         default:
234                 return -EINVAL;
235         }
236
237         r = elogind_set_wall_message(bus, table[a]);
238
239         if (r < 0) {
240                 log_warning_errno(r, "Failed to set wall message, ignoring: %s",
241                                   bus_error_message(&error, r));
242                 sd_bus_error_free(&error);
243         }
244
245         /* Now call elogind itself to request the operation */
246         r = sd_bus_call_method(
247                         bus,
248                         "org.freedesktop.login1",
249                         "/org/freedesktop/login1",
250                         "org.freedesktop.login1.Manager",
251                         method,
252                         &error,
253                         NULL,
254                         "b",
255                         arg_ask_password);
256
257         if (r < 0)
258                 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
259
260         return r;
261 }
262
263 static int elogind_schedule_shutdown(sd_bus *bus, enum elogind_action a) {
264         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
265         const char *method  = NULL;
266         int r;
267
268         if (!bus)
269                 return -EIO;
270
271         polkit_agent_open_if_enabled();
272
273         switch (a) {
274
275         case ACTION_POWEROFF:
276                 method = "poweroff";
277                 break;
278
279         case ACTION_REBOOT:
280                 method = "reboot";
281                 break;
282
283         default:
284                 return -EINVAL;
285         }
286
287         r = elogind_set_wall_message(bus, NULL);
288
289         if (r < 0) {
290                 log_warning_errno(r, "Failed to set wall message, ignoring: %s",
291                                   bus_error_message(&error, r));
292                 sd_bus_error_free(&error);
293         }
294
295         r = sd_bus_call_method(
296                         bus,
297                         "org.freedesktop.login1",
298                         "/org/freedesktop/login1",
299                         "org.freedesktop.login1.Manager",
300                         "ScheduleShutdown",
301                         &error,
302                         NULL,
303                         "st",
304                         method,
305                         arg_when);
306
307         if (r < 0)
308                 log_error("Failed to execute operation: %s", bus_error_message(&error, r));
309
310         return r;
311 }
312
313 static int elogind_set_wall_message(sd_bus* bus, const char* msg) {
314         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
315         _cleanup_free_ char *m = NULL;
316         int r;
317
318         if (strv_extend(&arg_wall, msg) < 0)
319                 return log_oom();
320
321         m = strv_join(arg_wall, " ");
322         if (!m)
323                 return log_oom();
324
325         r = sd_bus_call_method(
326                         bus,
327                         "org.freedesktop.login1",
328                         "/org/freedesktop/login1",
329                         "org.freedesktop.login1.Manager",
330                         "SetWallMessage",
331                         &error,
332                         NULL,
333                         "sb",
334                         m,
335                         !arg_no_wall);
336
337         if (r < 0)
338                 return log_warning_errno(r, "Failed to set wall message, ignoring: %s", bus_error_message(&error, r));
339
340         return 0;
341 }
342
343 static int parse_shutdown_time_spec(const char *t, usec_t *_u) {
344         assert(t);
345         assert(_u);
346
347         if (streq(t, "now"))
348                 *_u = 0;
349         else if (!strchr(t, ':')) {
350                 uint64_t u;
351
352                 if (safe_atou64(t, &u) < 0)
353                         return -EINVAL;
354
355                 *_u = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
356         } else {
357                 char *e = NULL;
358                 long hour, minute;
359                 struct tm tm = {};
360                 time_t s;
361                 usec_t n;
362
363                 errno = 0;
364                 hour = strtol(t, &e, 10);
365                 if (errno > 0 || *e != ':' || hour < 0 || hour > 23)
366                         return -EINVAL;
367
368                 minute = strtol(e+1, &e, 10);
369                 if (errno > 0 || *e != 0 || minute < 0 || minute > 59)
370                         return -EINVAL;
371
372                 n = now(CLOCK_REALTIME);
373                 s = (time_t) (n / USEC_PER_SEC);
374
375                 assert_se(localtime_r(&s, &tm));
376
377                 tm.tm_hour = (int) hour;
378                 tm.tm_min = (int) minute;
379                 tm.tm_sec = 0;
380
381                 assert_se(s = mktime(&tm));
382
383                 *_u = (usec_t) s * USEC_PER_SEC;
384
385                 while (*_u <= n)
386                         *_u += USEC_PER_DAY;
387         }
388
389         return 0;
390 }
391
392 void polkit_agent_open_if_enabled(void) {
393
394         /* Open the polkit agent as a child process if necessary */
395
396         if (!arg_ask_password)
397                 return;
398
399         if (arg_transport != BUS_TRANSPORT_LOCAL)
400                 return;
401
402         polkit_agent_open();
403 }
404
405 int start_special(int argc, char *argv[], void *userdata) {
406         sd_bus *bus = userdata;
407         enum elogind_action a;
408         int r;
409         char** wall = NULL;
410
411         assert(argv);
412
413         a = verb_to_action(argv[0]);
414
415         /* Switch to cancel shutdown, if a shutdown action was requested,
416            and the option to cancel it was set: */
417         if ( IN_SET(a, ACTION_POWEROFF, ACTION_REBOOT)
418           && (arg_action == ACTION_CANCEL_SHUTDOWN) )
419                 return elogind_cancel_shutdown(bus);
420
421         r = check_inhibitors(bus, a);
422         if (r < 0)
423                 return r;
424
425         /* No power off actions in chroot environments */
426         if ((a == ACTION_POWEROFF ||
427              a == ACTION_REBOOT) &&
428             (running_in_chroot() > 0) ) {
429                 log_info("Running in chroot, ignoring request.");
430                 return 0;
431         }
432
433         /* Check time arguments */
434         if ( IN_SET(a, ACTION_POWEROFF, ACTION_REBOOT)
435           && (argc > 1)
436           && (arg_action != ACTION_CANCEL_SHUTDOWN) ) {
437                 r = parse_shutdown_time_spec(argv[1], &arg_when);
438                 if (r < 0) {
439                         log_error("Failed to parse time specification: %s", argv[optind]);
440                         return r;
441                 }
442         } else
443                 arg_when = now(CLOCK_REALTIME) + USEC_PER_MINUTE;
444
445         /* The optional user wall message must be set */
446         if ((argc > 1) && (arg_action == ACTION_CANCEL_SHUTDOWN) )
447                 /* No time argument for shutdown cancel */
448                 wall = argv + 1;
449         else if (argc > 2)
450                 /* We skip the time argument */
451                 wall = argv + 2;
452
453         if (wall) {
454                 arg_wall = strv_copy(wall);
455                 if (!arg_wall)
456                         return log_oom();
457         }
458
459         /* Perform requested action */
460         if (IN_SET(a,
461                    ACTION_POWEROFF,
462                    ACTION_REBOOT,
463                    ACTION_SUSPEND,
464                    ACTION_HIBERNATE,
465                    ACTION_HYBRID_SLEEP)) {
466                 if (arg_when > 0)
467                         return elogind_schedule_shutdown(bus, a);
468                 else
469                         return elogind_reboot(bus, a);
470         }
471
472         return -EOPNOTSUPP;
473 }
474