]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include <errno.h> | |
4 | #include <pwd.h> | |
5 | #include <string.h> | |
6 | #include <unistd.h> | |
7 | ||
8 | #include "sd-device.h" | |
9 | #include "sd-messages.h" | |
10 | ||
11 | #include "alloc-util.h" | |
12 | #include "audit-util.h" | |
13 | #include "bus-common-errors.h" | |
14 | #include "bus-error.h" | |
15 | #include "bus-unit-util.h" | |
16 | #include "bus-util.h" | |
17 | #include "device-util.h" | |
18 | #include "dirent-util.h" | |
19 | #include "efivars.h" | |
20 | #include "escape.h" | |
21 | #include "fd-util.h" | |
22 | #include "fileio-label.h" | |
23 | #include "format-util.h" | |
24 | #include "fs-util.h" | |
25 | #include "logind.h" | |
26 | #include "mkdir.h" | |
27 | #include "path-util.h" | |
28 | #include "process-util.h" | |
29 | #include "cgroup-util.h" | |
30 | #include "selinux-util.h" | |
31 | #include "sleep-config.h" | |
32 | #include "special.h" | |
33 | #include "strv.h" | |
34 | #include "terminal-util.h" | |
35 | #include "unit-name.h" | |
36 | #include "user-util.h" | |
37 | #include "utmp-wtmp.h" | |
38 | ||
39 | static int get_sender_session(Manager *m, sd_bus_message *message, sd_bus_error *error, Session **ret) { | |
40 | ||
41 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
42 | const char *name; | |
43 | Session *session; | |
44 | int r; | |
45 | ||
46 | /* Get client login session. This is not what you are looking for these days, | |
47 | * as apps may instead belong to a user service unit. This includes terminal | |
48 | * emulators and hence command-line apps. */ | |
49 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT, &creds); | |
50 | if (r < 0) | |
51 | return r; | |
52 | ||
53 | r = sd_bus_creds_get_session(creds, &name); | |
54 | if (r == -ENXIO) | |
55 | goto err_no_session; | |
56 | if (r < 0) | |
57 | return r; | |
58 | ||
59 | session = hashmap_get(m->sessions, name); | |
60 | if (!session) | |
61 | goto err_no_session; | |
62 | ||
63 | *ret = session; | |
64 | return 0; | |
65 | ||
66 | err_no_session: | |
67 | return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, | |
68 | "Caller does not belong to any known session"); | |
69 | } | |
70 | ||
71 | int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret) { | |
72 | Session *session; | |
73 | ||
74 | assert(m); | |
75 | assert(message); | |
76 | assert(ret); | |
77 | ||
78 | if (isempty(name)) | |
79 | return get_sender_session(m, message, error, ret); | |
80 | ||
81 | session = hashmap_get(m->sessions, name); | |
82 | if (!session) | |
83 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name); | |
84 | ||
85 | *ret = session; | |
86 | return 0; | |
87 | } | |
88 | ||
89 | static int get_sender_user(Manager *m, sd_bus_message *message, sd_bus_error *error, User **ret) { | |
90 | ||
91 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
92 | uid_t uid; | |
93 | User *user; | |
94 | int r; | |
95 | ||
96 | /* Note that we get the owner UID of the session, not the actual client UID here! */ | |
97 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds); | |
98 | if (r < 0) | |
99 | return r; | |
100 | ||
101 | r = sd_bus_creds_get_owner_uid(creds, &uid); | |
102 | if (r == -ENXIO) | |
103 | goto err_no_user; | |
104 | if (r < 0) | |
105 | return r; | |
106 | ||
107 | user = hashmap_get(m->users, UID_TO_PTR(uid)); | |
108 | if (!user) | |
109 | goto err_no_user; | |
110 | ||
111 | *ret = user; | |
112 | return 0; | |
113 | ||
114 | err_no_user: | |
115 | return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, "Caller does not belong to any logged in user or lingering user"); | |
116 | } | |
117 | ||
118 | int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret) { | |
119 | User *user; | |
120 | ||
121 | assert(m); | |
122 | assert(message); | |
123 | assert(ret); | |
124 | ||
125 | if (!uid_is_valid(uid)) | |
126 | return get_sender_user(m, message, error, ret); | |
127 | ||
128 | user = hashmap_get(m->users, UID_TO_PTR(uid)); | |
129 | if (!user) | |
130 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "User ID "UID_FMT" is not logged in or lingering", uid); | |
131 | ||
132 | *ret = user; | |
133 | return 0; | |
134 | } | |
135 | ||
136 | int manager_get_seat_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Seat **ret) { | |
137 | Seat *seat; | |
138 | int r; | |
139 | ||
140 | assert(m); | |
141 | assert(message); | |
142 | assert(ret); | |
143 | ||
144 | if (isempty(name)) { | |
145 | Session *session; | |
146 | ||
147 | r = manager_get_session_from_creds(m, message, NULL, error, &session); | |
148 | if (r < 0) | |
149 | return r; | |
150 | ||
151 | seat = session->seat; | |
152 | if (!seat) | |
153 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "Session has no seat."); | |
154 | } else { | |
155 | seat = hashmap_get(m->seats, name); | |
156 | if (!seat) | |
157 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name); | |
158 | } | |
159 | ||
160 | *ret = seat; | |
161 | return 0; | |
162 | } | |
163 | ||
164 | static int property_get_idle_hint( | |
165 | sd_bus *bus, | |
166 | const char *path, | |
167 | const char *interface, | |
168 | const char *property, | |
169 | sd_bus_message *reply, | |
170 | void *userdata, | |
171 | sd_bus_error *error) { | |
172 | ||
173 | Manager *m = userdata; | |
174 | ||
175 | assert(bus); | |
176 | assert(reply); | |
177 | assert(m); | |
178 | ||
179 | return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0); | |
180 | } | |
181 | ||
182 | static int property_get_idle_since_hint( | |
183 | sd_bus *bus, | |
184 | const char *path, | |
185 | const char *interface, | |
186 | const char *property, | |
187 | sd_bus_message *reply, | |
188 | void *userdata, | |
189 | sd_bus_error *error) { | |
190 | ||
191 | Manager *m = userdata; | |
192 | dual_timestamp t = DUAL_TIMESTAMP_NULL; | |
193 | ||
194 | assert(bus); | |
195 | assert(reply); | |
196 | assert(m); | |
197 | ||
198 | manager_get_idle_hint(m, &t); | |
199 | ||
200 | return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic); | |
201 | } | |
202 | ||
203 | static int property_get_inhibited( | |
204 | sd_bus *bus, | |
205 | const char *path, | |
206 | const char *interface, | |
207 | const char *property, | |
208 | sd_bus_message *reply, | |
209 | void *userdata, | |
210 | sd_bus_error *error) { | |
211 | ||
212 | Manager *m = userdata; | |
213 | InhibitWhat w; | |
214 | ||
215 | assert(bus); | |
216 | assert(reply); | |
217 | assert(m); | |
218 | ||
219 | w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY); | |
220 | ||
221 | return sd_bus_message_append(reply, "s", inhibit_what_to_string(w)); | |
222 | } | |
223 | ||
224 | static int property_get_preparing( | |
225 | sd_bus *bus, | |
226 | const char *path, | |
227 | const char *interface, | |
228 | const char *property, | |
229 | sd_bus_message *reply, | |
230 | void *userdata, | |
231 | sd_bus_error *error) { | |
232 | ||
233 | Manager *m = userdata; | |
234 | bool b; | |
235 | ||
236 | assert(bus); | |
237 | assert(reply); | |
238 | assert(m); | |
239 | ||
240 | if (streq(property, "PreparingForShutdown")) | |
241 | b = m->action_what & INHIBIT_SHUTDOWN; | |
242 | else | |
243 | b = m->action_what & INHIBIT_SLEEP; | |
244 | ||
245 | return sd_bus_message_append(reply, "b", b); | |
246 | } | |
247 | ||
248 | static int property_get_scheduled_shutdown( | |
249 | sd_bus *bus, | |
250 | const char *path, | |
251 | const char *interface, | |
252 | const char *property, | |
253 | sd_bus_message *reply, | |
254 | void *userdata, | |
255 | sd_bus_error *error) { | |
256 | ||
257 | Manager *m = userdata; | |
258 | int r; | |
259 | ||
260 | assert(bus); | |
261 | assert(reply); | |
262 | assert(m); | |
263 | ||
264 | r = sd_bus_message_open_container(reply, 'r', "st"); | |
265 | if (r < 0) | |
266 | return r; | |
267 | ||
268 | r = sd_bus_message_append(reply, "st", m->scheduled_shutdown_type, m->scheduled_shutdown_timeout); | |
269 | if (r < 0) | |
270 | return r; | |
271 | ||
272 | return sd_bus_message_close_container(reply); | |
273 | } | |
274 | ||
275 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction); | |
276 | static BUS_DEFINE_PROPERTY_GET(property_get_docked, "b", Manager, manager_is_docked_or_external_displays); | |
277 | static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed, "b", Manager, manager_is_lid_closed); | |
278 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max, "t", CGROUP_LIMIT_MAX); | |
279 | static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size, "t", Hashmap *, (uint64_t) hashmap_size); | |
280 | ||
281 | static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
282 | _cleanup_free_ char *p = NULL; | |
283 | Manager *m = userdata; | |
284 | const char *name; | |
285 | Session *session; | |
286 | int r; | |
287 | ||
288 | assert(message); | |
289 | assert(m); | |
290 | ||
291 | r = sd_bus_message_read(message, "s", &name); | |
292 | if (r < 0) | |
293 | return r; | |
294 | ||
295 | r = manager_get_session_from_creds(m, message, name, error, &session); | |
296 | if (r < 0) | |
297 | return r; | |
298 | ||
299 | p = session_bus_path(session); | |
300 | if (!p) | |
301 | return -ENOMEM; | |
302 | ||
303 | return sd_bus_reply_method_return(message, "o", p); | |
304 | } | |
305 | ||
306 | /* Get login session of a process. This is not what you are looking for these days, | |
307 | * as apps may instead belong to a user service unit. This includes terminal | |
308 | * emulators and hence command-line apps. */ | |
309 | static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
310 | _cleanup_free_ char *p = NULL; | |
311 | Session *session = NULL; | |
312 | Manager *m = userdata; | |
313 | pid_t pid; | |
314 | int r; | |
315 | ||
316 | assert(message); | |
317 | assert(m); | |
318 | ||
319 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); | |
320 | ||
321 | r = sd_bus_message_read(message, "u", &pid); | |
322 | if (r < 0) | |
323 | return r; | |
324 | if (pid < 0) | |
325 | return -EINVAL; | |
326 | ||
327 | if (pid == 0) { | |
328 | r = manager_get_session_from_creds(m, message, NULL, error, &session); | |
329 | if (r < 0) | |
330 | return r; | |
331 | } else { | |
332 | r = manager_get_session_by_pid(m, pid, &session); | |
333 | if (r < 0) | |
334 | return r; | |
335 | ||
336 | if (!session) | |
337 | return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, "PID "PID_FMT" does not belong to any known session", pid); | |
338 | } | |
339 | ||
340 | p = session_bus_path(session); | |
341 | if (!p) | |
342 | return -ENOMEM; | |
343 | ||
344 | return sd_bus_reply_method_return(message, "o", p); | |
345 | } | |
346 | ||
347 | static int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
348 | _cleanup_free_ char *p = NULL; | |
349 | Manager *m = userdata; | |
350 | uint32_t uid; | |
351 | User *user; | |
352 | int r; | |
353 | ||
354 | assert(message); | |
355 | assert(m); | |
356 | ||
357 | r = sd_bus_message_read(message, "u", &uid); | |
358 | if (r < 0) | |
359 | return r; | |
360 | ||
361 | r = manager_get_user_from_creds(m, message, uid, error, &user); | |
362 | if (r < 0) | |
363 | return r; | |
364 | ||
365 | p = user_bus_path(user); | |
366 | if (!p) | |
367 | return -ENOMEM; | |
368 | ||
369 | return sd_bus_reply_method_return(message, "o", p); | |
370 | } | |
371 | ||
372 | static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
373 | _cleanup_free_ char *p = NULL; | |
374 | Manager *m = userdata; | |
375 | User *user = NULL; | |
376 | pid_t pid; | |
377 | int r; | |
378 | ||
379 | assert(message); | |
380 | assert(m); | |
381 | ||
382 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); | |
383 | ||
384 | r = sd_bus_message_read(message, "u", &pid); | |
385 | if (r < 0) | |
386 | return r; | |
387 | if (pid < 0) | |
388 | return -EINVAL; | |
389 | ||
390 | if (pid == 0) { | |
391 | r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user); | |
392 | if (r < 0) | |
393 | return r; | |
394 | } else { | |
395 | r = manager_get_user_by_pid(m, pid, &user); | |
396 | if (r < 0) | |
397 | return r; | |
398 | if (!user) | |
399 | return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, | |
400 | "PID "PID_FMT" does not belong to any logged in user or lingering user", | |
401 | pid); | |
402 | } | |
403 | ||
404 | p = user_bus_path(user); | |
405 | if (!p) | |
406 | return -ENOMEM; | |
407 | ||
408 | return sd_bus_reply_method_return(message, "o", p); | |
409 | } | |
410 | ||
411 | static int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
412 | _cleanup_free_ char *p = NULL; | |
413 | Manager *m = userdata; | |
414 | const char *name; | |
415 | Seat *seat; | |
416 | int r; | |
417 | ||
418 | assert(message); | |
419 | assert(m); | |
420 | ||
421 | r = sd_bus_message_read(message, "s", &name); | |
422 | if (r < 0) | |
423 | return r; | |
424 | ||
425 | r = manager_get_seat_from_creds(m, message, name, error, &seat); | |
426 | if (r < 0) | |
427 | return r; | |
428 | ||
429 | p = seat_bus_path(seat); | |
430 | if (!p) | |
431 | return -ENOMEM; | |
432 | ||
433 | return sd_bus_reply_method_return(message, "o", p); | |
434 | } | |
435 | ||
436 | static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
437 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
438 | Manager *m = userdata; | |
439 | Session *session; | |
440 | Iterator i; | |
441 | int r; | |
442 | ||
443 | assert(message); | |
444 | assert(m); | |
445 | ||
446 | r = sd_bus_message_new_method_return(message, &reply); | |
447 | if (r < 0) | |
448 | return r; | |
449 | ||
450 | r = sd_bus_message_open_container(reply, 'a', "(susso)"); | |
451 | if (r < 0) | |
452 | return r; | |
453 | ||
454 | HASHMAP_FOREACH(session, m->sessions, i) { | |
455 | _cleanup_free_ char *p = NULL; | |
456 | ||
457 | p = session_bus_path(session); | |
458 | if (!p) | |
459 | return -ENOMEM; | |
460 | ||
461 | r = sd_bus_message_append(reply, "(susso)", | |
462 | session->id, | |
463 | (uint32_t) session->user->uid, | |
464 | session->user->name, | |
465 | session->seat ? session->seat->id : "", | |
466 | p); | |
467 | if (r < 0) | |
468 | return r; | |
469 | } | |
470 | ||
471 | r = sd_bus_message_close_container(reply); | |
472 | if (r < 0) | |
473 | return r; | |
474 | ||
475 | return sd_bus_send(NULL, reply, NULL); | |
476 | } | |
477 | ||
478 | static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
479 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
480 | Manager *m = userdata; | |
481 | User *user; | |
482 | Iterator i; | |
483 | int r; | |
484 | ||
485 | assert(message); | |
486 | assert(m); | |
487 | ||
488 | r = sd_bus_message_new_method_return(message, &reply); | |
489 | if (r < 0) | |
490 | return r; | |
491 | ||
492 | r = sd_bus_message_open_container(reply, 'a', "(uso)"); | |
493 | if (r < 0) | |
494 | return r; | |
495 | ||
496 | HASHMAP_FOREACH(user, m->users, i) { | |
497 | _cleanup_free_ char *p = NULL; | |
498 | ||
499 | p = user_bus_path(user); | |
500 | if (!p) | |
501 | return -ENOMEM; | |
502 | ||
503 | r = sd_bus_message_append(reply, "(uso)", | |
504 | (uint32_t) user->uid, | |
505 | user->name, | |
506 | p); | |
507 | if (r < 0) | |
508 | return r; | |
509 | } | |
510 | ||
511 | r = sd_bus_message_close_container(reply); | |
512 | if (r < 0) | |
513 | return r; | |
514 | ||
515 | return sd_bus_send(NULL, reply, NULL); | |
516 | } | |
517 | ||
518 | static int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
519 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
520 | Manager *m = userdata; | |
521 | Seat *seat; | |
522 | Iterator i; | |
523 | int r; | |
524 | ||
525 | assert(message); | |
526 | assert(m); | |
527 | ||
528 | r = sd_bus_message_new_method_return(message, &reply); | |
529 | if (r < 0) | |
530 | return r; | |
531 | ||
532 | r = sd_bus_message_open_container(reply, 'a', "(so)"); | |
533 | if (r < 0) | |
534 | return r; | |
535 | ||
536 | HASHMAP_FOREACH(seat, m->seats, i) { | |
537 | _cleanup_free_ char *p = NULL; | |
538 | ||
539 | p = seat_bus_path(seat); | |
540 | if (!p) | |
541 | return -ENOMEM; | |
542 | ||
543 | r = sd_bus_message_append(reply, "(so)", seat->id, p); | |
544 | if (r < 0) | |
545 | return r; | |
546 | } | |
547 | ||
548 | r = sd_bus_message_close_container(reply); | |
549 | if (r < 0) | |
550 | return r; | |
551 | ||
552 | return sd_bus_send(NULL, reply, NULL); | |
553 | } | |
554 | ||
555 | static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
556 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
557 | Manager *m = userdata; | |
558 | Inhibitor *inhibitor; | |
559 | Iterator i; | |
560 | int r; | |
561 | ||
562 | assert(message); | |
563 | assert(m); | |
564 | ||
565 | r = sd_bus_message_new_method_return(message, &reply); | |
566 | if (r < 0) | |
567 | return r; | |
568 | ||
569 | r = sd_bus_message_open_container(reply, 'a', "(ssssuu)"); | |
570 | if (r < 0) | |
571 | return r; | |
572 | ||
573 | HASHMAP_FOREACH(inhibitor, m->inhibitors, i) { | |
574 | ||
575 | r = sd_bus_message_append(reply, "(ssssuu)", | |
576 | strempty(inhibit_what_to_string(inhibitor->what)), | |
577 | strempty(inhibitor->who), | |
578 | strempty(inhibitor->why), | |
579 | strempty(inhibit_mode_to_string(inhibitor->mode)), | |
580 | (uint32_t) inhibitor->uid, | |
581 | (uint32_t) inhibitor->pid); | |
582 | if (r < 0) | |
583 | return r; | |
584 | } | |
585 | ||
586 | r = sd_bus_message_close_container(reply); | |
587 | if (r < 0) | |
588 | return r; | |
589 | ||
590 | return sd_bus_send(NULL, reply, NULL); | |
591 | } | |
592 | ||
593 | static int method_create_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
594 | const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop; | |
595 | _cleanup_free_ char *id = NULL; | |
596 | Session *session = NULL; | |
597 | uint32_t audit_id = 0; | |
598 | Manager *m = userdata; | |
599 | User *user = NULL; | |
600 | Seat *seat = NULL; | |
601 | pid_t leader; | |
602 | uid_t uid; | |
603 | int remote; | |
604 | uint32_t vtnr = 0; | |
605 | SessionType t; | |
606 | SessionClass c; | |
607 | int r; | |
608 | ||
609 | assert(message); | |
610 | assert(m); | |
611 | ||
612 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); | |
613 | assert_cc(sizeof(uid_t) == sizeof(uint32_t)); | |
614 | ||
615 | r = sd_bus_message_read(message, "uusssssussbss", &uid, &leader, &service, &type, &class, &desktop, &cseat, &vtnr, &tty, &display, &remote, &remote_user, &remote_host); | |
616 | if (r < 0) | |
617 | return r; | |
618 | ||
619 | if (!uid_is_valid(uid)) | |
620 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UID"); | |
621 | if (leader < 0 || leader == 1 || leader == getpid_cached()) | |
622 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID"); | |
623 | ||
624 | if (isempty(type)) | |
625 | t = _SESSION_TYPE_INVALID; | |
626 | else { | |
627 | t = session_type_from_string(type); | |
628 | if (t < 0) | |
629 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session type %s", type); | |
630 | } | |
631 | ||
632 | if (isempty(class)) | |
633 | c = _SESSION_CLASS_INVALID; | |
634 | else { | |
635 | c = session_class_from_string(class); | |
636 | if (c < 0) | |
637 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session class %s", class); | |
638 | } | |
639 | ||
640 | if (isempty(desktop)) | |
641 | desktop = NULL; | |
642 | else { | |
643 | if (!string_is_safe(desktop)) | |
644 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid desktop string %s", desktop); | |
645 | } | |
646 | ||
647 | if (isempty(cseat)) | |
648 | seat = NULL; | |
649 | else { | |
650 | seat = hashmap_get(m->seats, cseat); | |
651 | if (!seat) | |
652 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", cseat); | |
653 | } | |
654 | ||
655 | if (tty_is_vc(tty)) { | |
656 | int v; | |
657 | ||
658 | if (!seat) | |
659 | seat = m->seat0; | |
660 | else if (seat != m->seat0) | |
661 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "TTY %s is virtual console but seat %s is not seat0", tty, seat->id); | |
662 | ||
663 | v = vtnr_from_tty(tty); | |
664 | if (v <= 0) | |
665 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty); | |
666 | ||
667 | if (vtnr == 0) | |
668 | vtnr = (uint32_t) v; | |
669 | else if (vtnr != (uint32_t) v) | |
670 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match"); | |
671 | ||
672 | } else if (tty_is_console(tty)) { | |
673 | ||
674 | if (!seat) | |
675 | seat = m->seat0; | |
676 | else if (seat != m->seat0) | |
677 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but seat is not seat0"); | |
678 | ||
679 | if (vtnr != 0) | |
680 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but VT number is not 0"); | |
681 | } | |
682 | ||
683 | if (seat) { | |
684 | if (seat_has_vts(seat)) { | |
685 | if (vtnr <= 0 || vtnr > 63) | |
686 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "VT number out of range"); | |
687 | } else { | |
688 | if (vtnr != 0) | |
689 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat has no VTs but VT number not 0"); | |
690 | } | |
691 | } | |
692 | ||
693 | if (t == _SESSION_TYPE_INVALID) { | |
694 | if (!isempty(display)) | |
695 | t = SESSION_X11; | |
696 | else if (!isempty(tty)) | |
697 | t = SESSION_TTY; | |
698 | else | |
699 | t = SESSION_UNSPECIFIED; | |
700 | } | |
701 | ||
702 | if (c == _SESSION_CLASS_INVALID) { | |
703 | if (t == SESSION_UNSPECIFIED) | |
704 | c = SESSION_BACKGROUND; | |
705 | else | |
706 | c = SESSION_USER; | |
707 | } | |
708 | ||
709 | if (leader == 0) { | |
710 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
711 | ||
712 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); | |
713 | if (r < 0) | |
714 | return r; | |
715 | ||
716 | r = sd_bus_creds_get_pid(creds, (pid_t*) &leader); | |
717 | if (r < 0) | |
718 | return r; | |
719 | } | |
720 | ||
721 | /* Check if we are already in a logind session. Or if we are in user@.service which is a special PAM session | |
722 | * that avoids creating a logind session. */ | |
723 | r = manager_get_user_by_pid(m, leader, NULL); | |
724 | if (r < 0) | |
725 | return r; | |
726 | if (r > 0) | |
727 | return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already running in a session or user slice"); | |
728 | ||
729 | /* | |
730 | * Old gdm and lightdm start the user-session on the same VT as | |
731 | * the greeter session. But they destroy the greeter session | |
732 | * after the user-session and want the user-session to take | |
733 | * over the VT. We need to support this for | |
734 | * backwards-compatibility, so make sure we allow new sessions | |
735 | * on a VT that a greeter is running on. Furthermore, to allow | |
736 | * re-logins, we have to allow a greeter to take over a used VT for | |
737 | * the exact same reasons. | |
738 | */ | |
739 | if (c != SESSION_GREETER && | |
740 | vtnr > 0 && | |
741 | vtnr < m->seat0->position_count && | |
742 | m->seat0->positions[vtnr] && | |
743 | m->seat0->positions[vtnr]->class != SESSION_GREETER) | |
744 | return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session"); | |
745 | ||
746 | if (hashmap_size(m->sessions) >= m->sessions_max) | |
747 | return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", m->sessions_max); | |
748 | ||
749 | (void) audit_session_from_pid(leader, &audit_id); | |
750 | if (audit_session_is_valid(audit_id)) { | |
751 | /* Keep our session IDs and the audit session IDs in sync */ | |
752 | ||
753 | if (asprintf(&id, "%"PRIu32, audit_id) < 0) | |
754 | return -ENOMEM; | |
755 | ||
756 | /* Wut? There's already a session by this name and we | |
757 | * didn't find it above? Weird, then let's not trust | |
758 | * the audit data and let's better register a new | |
759 | * ID */ | |
760 | if (hashmap_get(m->sessions, id)) { | |
761 | log_warning("Existing logind session ID %s used by new audit session, ignoring.", id); | |
762 | audit_id = AUDIT_SESSION_INVALID; | |
763 | id = mfree(id); | |
764 | } | |
765 | } | |
766 | ||
767 | if (!id) { | |
768 | do { | |
769 | id = mfree(id); | |
770 | ||
771 | if (asprintf(&id, "c%lu", ++m->session_counter) < 0) | |
772 | return -ENOMEM; | |
773 | ||
774 | } while (hashmap_get(m->sessions, id)); | |
775 | } | |
776 | ||
777 | /* If we are not watching utmp aleady, try again */ | |
778 | manager_reconnect_utmp(m); | |
779 | ||
780 | r = manager_add_user_by_uid(m, uid, &user); | |
781 | if (r < 0) | |
782 | goto fail; | |
783 | ||
784 | r = manager_add_session(m, id, &session); | |
785 | if (r < 0) | |
786 | goto fail; | |
787 | ||
788 | session_set_user(session, user); | |
789 | session_set_leader(session, leader); | |
790 | ||
791 | session->type = t; | |
792 | session->class = c; | |
793 | session->remote = remote; | |
794 | session->vtnr = vtnr; | |
795 | ||
796 | if (!isempty(tty)) { | |
797 | session->tty = strdup(tty); | |
798 | if (!session->tty) { | |
799 | r = -ENOMEM; | |
800 | goto fail; | |
801 | } | |
802 | ||
803 | session->tty_validity = TTY_FROM_PAM; | |
804 | } | |
805 | ||
806 | if (!isempty(display)) { | |
807 | session->display = strdup(display); | |
808 | if (!session->display) { | |
809 | r = -ENOMEM; | |
810 | goto fail; | |
811 | } | |
812 | } | |
813 | ||
814 | if (!isempty(remote_user)) { | |
815 | session->remote_user = strdup(remote_user); | |
816 | if (!session->remote_user) { | |
817 | r = -ENOMEM; | |
818 | goto fail; | |
819 | } | |
820 | } | |
821 | ||
822 | if (!isempty(remote_host)) { | |
823 | session->remote_host = strdup(remote_host); | |
824 | if (!session->remote_host) { | |
825 | r = -ENOMEM; | |
826 | goto fail; | |
827 | } | |
828 | } | |
829 | ||
830 | if (!isempty(service)) { | |
831 | session->service = strdup(service); | |
832 | if (!session->service) { | |
833 | r = -ENOMEM; | |
834 | goto fail; | |
835 | } | |
836 | } | |
837 | ||
838 | if (!isempty(desktop)) { | |
839 | session->desktop = strdup(desktop); | |
840 | if (!session->desktop) { | |
841 | r = -ENOMEM; | |
842 | goto fail; | |
843 | } | |
844 | } | |
845 | ||
846 | if (seat) { | |
847 | r = seat_attach_session(seat, session); | |
848 | if (r < 0) | |
849 | goto fail; | |
850 | } | |
851 | ||
852 | r = sd_bus_message_enter_container(message, 'a', "(sv)"); | |
853 | if (r < 0) | |
854 | goto fail; | |
855 | ||
856 | r = session_start(session, message, error); | |
857 | if (r < 0) | |
858 | goto fail; | |
859 | ||
860 | r = sd_bus_message_exit_container(message); | |
861 | if (r < 0) | |
862 | goto fail; | |
863 | ||
864 | session->create_message = sd_bus_message_ref(message); | |
865 | ||
866 | /* Now, let's wait until the slice unit and stuff got created. We send the reply back from | |
867 | * session_send_create_reply(). */ | |
868 | ||
869 | return 1; | |
870 | ||
871 | fail: | |
872 | if (session) | |
873 | session_add_to_gc_queue(session); | |
874 | ||
875 | if (user) | |
876 | user_add_to_gc_queue(user); | |
877 | ||
878 | return r; | |
879 | } | |
880 | ||
881 | static int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
882 | Manager *m = userdata; | |
883 | Session *session; | |
884 | const char *name; | |
885 | int r; | |
886 | ||
887 | assert(message); | |
888 | assert(m); | |
889 | ||
890 | r = sd_bus_message_read(message, "s", &name); | |
891 | if (r < 0) | |
892 | return r; | |
893 | ||
894 | r = manager_get_session_from_creds(m, message, name, error, &session); | |
895 | if (r < 0) | |
896 | return r; | |
897 | ||
898 | r = session_release(session); | |
899 | if (r < 0) | |
900 | return r; | |
901 | ||
902 | return sd_bus_reply_method_return(message, NULL); | |
903 | } | |
904 | ||
905 | static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
906 | Manager *m = userdata; | |
907 | Session *session; | |
908 | const char *name; | |
909 | int r; | |
910 | ||
911 | assert(message); | |
912 | assert(m); | |
913 | ||
914 | r = sd_bus_message_read(message, "s", &name); | |
915 | if (r < 0) | |
916 | return r; | |
917 | ||
918 | r = manager_get_session_from_creds(m, message, name, error, &session); | |
919 | if (r < 0) | |
920 | return r; | |
921 | ||
922 | return bus_session_method_activate(message, session, error); | |
923 | } | |
924 | ||
925 | static int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
926 | const char *session_name, *seat_name; | |
927 | Manager *m = userdata; | |
928 | Session *session; | |
929 | Seat *seat; | |
930 | int r; | |
931 | ||
932 | assert(message); | |
933 | assert(m); | |
934 | ||
935 | /* Same as ActivateSession() but refuses to work if | |
936 | * the seat doesn't match */ | |
937 | ||
938 | r = sd_bus_message_read(message, "ss", &session_name, &seat_name); | |
939 | if (r < 0) | |
940 | return r; | |
941 | ||
942 | r = manager_get_session_from_creds(m, message, session_name, error, &session); | |
943 | if (r < 0) | |
944 | return r; | |
945 | ||
946 | r = manager_get_seat_from_creds(m, message, seat_name, error, &seat); | |
947 | if (r < 0) | |
948 | return r; | |
949 | ||
950 | if (session->seat != seat) | |
951 | return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name); | |
952 | ||
953 | r = session_activate(session); | |
954 | if (r < 0) | |
955 | return r; | |
956 | ||
957 | return sd_bus_reply_method_return(message, NULL); | |
958 | } | |
959 | ||
960 | static int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
961 | Manager *m = userdata; | |
962 | Session *session; | |
963 | const char *name; | |
964 | int r; | |
965 | ||
966 | assert(message); | |
967 | assert(m); | |
968 | ||
969 | r = sd_bus_message_read(message, "s", &name); | |
970 | if (r < 0) | |
971 | return r; | |
972 | ||
973 | r = manager_get_session_from_creds(m, message, name, error, &session); | |
974 | if (r < 0) | |
975 | return r; | |
976 | ||
977 | return bus_session_method_lock(message, session, error); | |
978 | } | |
979 | ||
980 | static int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
981 | Manager *m = userdata; | |
982 | int r; | |
983 | ||
984 | assert(message); | |
985 | assert(m); | |
986 | ||
987 | r = bus_verify_polkit_async( | |
988 | message, | |
989 | CAP_SYS_ADMIN, | |
990 | "org.freedesktop.login1.lock-sessions", | |
991 | NULL, | |
992 | false, | |
993 | UID_INVALID, | |
994 | &m->polkit_registry, | |
995 | error); | |
996 | if (r < 0) | |
997 | return r; | |
998 | if (r == 0) | |
999 | return 1; /* Will call us back */ | |
1000 | ||
1001 | r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions")); | |
1002 | if (r < 0) | |
1003 | return r; | |
1004 | ||
1005 | return sd_bus_reply_method_return(message, NULL); | |
1006 | } | |
1007 | ||
1008 | static int method_kill_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1009 | const char *name; | |
1010 | Manager *m = userdata; | |
1011 | Session *session; | |
1012 | int r; | |
1013 | ||
1014 | assert(message); | |
1015 | assert(m); | |
1016 | ||
1017 | r = sd_bus_message_read(message, "s", &name); | |
1018 | if (r < 0) | |
1019 | return r; | |
1020 | ||
1021 | r = manager_get_session_from_creds(m, message, name, error, &session); | |
1022 | if (r < 0) | |
1023 | return r; | |
1024 | ||
1025 | return bus_session_method_kill(message, session, error); | |
1026 | } | |
1027 | ||
1028 | static int method_kill_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1029 | Manager *m = userdata; | |
1030 | uint32_t uid; | |
1031 | User *user; | |
1032 | int r; | |
1033 | ||
1034 | assert(message); | |
1035 | assert(m); | |
1036 | ||
1037 | r = sd_bus_message_read(message, "u", &uid); | |
1038 | if (r < 0) | |
1039 | return r; | |
1040 | ||
1041 | r = manager_get_user_from_creds(m, message, uid, error, &user); | |
1042 | if (r < 0) | |
1043 | return r; | |
1044 | ||
1045 | return bus_user_method_kill(message, user, error); | |
1046 | } | |
1047 | ||
1048 | static int method_terminate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1049 | Manager *m = userdata; | |
1050 | const char *name; | |
1051 | Session *session; | |
1052 | int r; | |
1053 | ||
1054 | assert(message); | |
1055 | assert(m); | |
1056 | ||
1057 | r = sd_bus_message_read(message, "s", &name); | |
1058 | if (r < 0) | |
1059 | return r; | |
1060 | ||
1061 | r = manager_get_session_from_creds(m, message, name, error, &session); | |
1062 | if (r < 0) | |
1063 | return r; | |
1064 | ||
1065 | return bus_session_method_terminate(message, session, error); | |
1066 | } | |
1067 | ||
1068 | static int method_terminate_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1069 | Manager *m = userdata; | |
1070 | uint32_t uid; | |
1071 | User *user; | |
1072 | int r; | |
1073 | ||
1074 | assert(message); | |
1075 | assert(m); | |
1076 | ||
1077 | r = sd_bus_message_read(message, "u", &uid); | |
1078 | if (r < 0) | |
1079 | return r; | |
1080 | ||
1081 | r = manager_get_user_from_creds(m, message, uid, error, &user); | |
1082 | if (r < 0) | |
1083 | return r; | |
1084 | ||
1085 | return bus_user_method_terminate(message, user, error); | |
1086 | } | |
1087 | ||
1088 | static int method_terminate_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1089 | Manager *m = userdata; | |
1090 | const char *name; | |
1091 | Seat *seat; | |
1092 | int r; | |
1093 | ||
1094 | assert(message); | |
1095 | assert(m); | |
1096 | ||
1097 | r = sd_bus_message_read(message, "s", &name); | |
1098 | if (r < 0) | |
1099 | return r; | |
1100 | ||
1101 | r = manager_get_seat_from_creds(m, message, name, error, &seat); | |
1102 | if (r < 0) | |
1103 | return r; | |
1104 | ||
1105 | return bus_seat_method_terminate(message, seat, error); | |
1106 | } | |
1107 | ||
1108 | static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1109 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
1110 | _cleanup_free_ char *cc = NULL; | |
1111 | Manager *m = userdata; | |
1112 | int r, b, interactive; | |
1113 | struct passwd *pw; | |
1114 | const char *path; | |
1115 | uint32_t uid, auth_uid; | |
1116 | ||
1117 | assert(message); | |
1118 | assert(m); | |
1119 | ||
1120 | r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive); | |
1121 | if (r < 0) | |
1122 | return r; | |
1123 | ||
1124 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID | | |
1125 | SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds); | |
1126 | if (r < 0) | |
1127 | return r; | |
1128 | ||
1129 | if (!uid_is_valid(uid)) { | |
1130 | /* Note that we get the owner UID of the session or user unit, | |
1131 | * not the actual client UID here! */ | |
1132 | r = sd_bus_creds_get_owner_uid(creds, &uid); | |
1133 | if (r < 0) | |
1134 | return r; | |
1135 | } | |
1136 | ||
1137 | /* owner_uid is racy, so for authorization we must use euid */ | |
1138 | r = sd_bus_creds_get_euid(creds, &auth_uid); | |
1139 | if (r < 0) | |
1140 | return r; | |
1141 | ||
1142 | errno = 0; | |
1143 | pw = getpwuid(uid); | |
1144 | if (!pw) | |
1145 | return errno > 0 ? -errno : -ENOENT; | |
1146 | ||
1147 | r = bus_verify_polkit_async( | |
1148 | message, | |
1149 | CAP_SYS_ADMIN, | |
1150 | uid == auth_uid ? "org.freedesktop.login1.set-self-linger" : | |
1151 | "org.freedesktop.login1.set-user-linger", | |
1152 | NULL, | |
1153 | interactive, | |
1154 | UID_INVALID, | |
1155 | &m->polkit_registry, | |
1156 | error); | |
1157 | if (r < 0) | |
1158 | return r; | |
1159 | if (r == 0) | |
1160 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1161 | ||
1162 | mkdir_p_label("/var/lib/systemd", 0755); | |
1163 | ||
1164 | r = mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE); | |
1165 | if (r < 0) | |
1166 | return r; | |
1167 | ||
1168 | cc = cescape(pw->pw_name); | |
1169 | if (!cc) | |
1170 | return -ENOMEM; | |
1171 | ||
1172 | path = strjoina("/var/lib/systemd/linger/", cc); | |
1173 | if (b) { | |
1174 | User *u; | |
1175 | ||
1176 | r = touch(path); | |
1177 | if (r < 0) | |
1178 | return r; | |
1179 | ||
1180 | if (manager_add_user_by_uid(m, uid, &u) >= 0) | |
1181 | user_start(u); | |
1182 | ||
1183 | } else { | |
1184 | User *u; | |
1185 | ||
1186 | r = unlink(path); | |
1187 | if (r < 0 && errno != ENOENT) | |
1188 | return -errno; | |
1189 | ||
1190 | u = hashmap_get(m->users, UID_TO_PTR(uid)); | |
1191 | if (u) | |
1192 | user_add_to_gc_queue(u); | |
1193 | } | |
1194 | ||
1195 | return sd_bus_reply_method_return(message, NULL); | |
1196 | } | |
1197 | ||
1198 | static int trigger_device(Manager *m, sd_device *d) { | |
1199 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
1200 | int r; | |
1201 | ||
1202 | assert(m); | |
1203 | ||
1204 | r = sd_device_enumerator_new(&e); | |
1205 | if (r < 0) | |
1206 | return r; | |
1207 | ||
1208 | r = sd_device_enumerator_allow_uninitialized(e); | |
1209 | if (r < 0) | |
1210 | return r; | |
1211 | ||
1212 | if (d) { | |
1213 | r = sd_device_enumerator_add_match_parent(e, d); | |
1214 | if (r < 0) | |
1215 | return r; | |
1216 | } | |
1217 | ||
1218 | FOREACH_DEVICE(e, d) { | |
1219 | _cleanup_free_ char *t = NULL; | |
1220 | const char *p; | |
1221 | ||
1222 | r = sd_device_get_syspath(d, &p); | |
1223 | if (r < 0) | |
1224 | return r; | |
1225 | ||
1226 | t = strappend(p, "/uevent"); | |
1227 | if (!t) | |
1228 | return -ENOMEM; | |
1229 | ||
1230 | (void) write_string_file(t, "change", WRITE_STRING_FILE_DISABLE_BUFFER); | |
1231 | } | |
1232 | ||
1233 | return 0; | |
1234 | } | |
1235 | ||
1236 | static int attach_device(Manager *m, const char *seat, const char *sysfs) { | |
1237 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; | |
1238 | _cleanup_free_ char *rule = NULL, *file = NULL; | |
1239 | const char *id_for_seat; | |
1240 | int r; | |
1241 | ||
1242 | assert(m); | |
1243 | assert(seat); | |
1244 | assert(sysfs); | |
1245 | ||
1246 | r = sd_device_new_from_syspath(&d, sysfs); | |
1247 | if (r < 0) | |
1248 | return r; | |
1249 | ||
1250 | if (sd_device_has_tag(d, "seat") <= 0) | |
1251 | return -ENODEV; | |
1252 | ||
1253 | if (sd_device_get_property_value(d, "ID_FOR_SEAT", &id_for_seat) < 0) | |
1254 | return -ENODEV; | |
1255 | ||
1256 | if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) | |
1257 | return -ENOMEM; | |
1258 | ||
1259 | if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) | |
1260 | return -ENOMEM; | |
1261 | ||
1262 | (void) mkdir_p_label("/etc/udev/rules.d", 0755); | |
1263 | r = write_string_file_atomic_label(file, rule); | |
1264 | if (r < 0) | |
1265 | return r; | |
1266 | ||
1267 | return trigger_device(m, d); | |
1268 | } | |
1269 | ||
1270 | static int flush_devices(Manager *m) { | |
1271 | _cleanup_closedir_ DIR *d; | |
1272 | ||
1273 | assert(m); | |
1274 | ||
1275 | d = opendir("/etc/udev/rules.d"); | |
1276 | if (!d) { | |
1277 | if (errno != ENOENT) | |
1278 | log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m"); | |
1279 | } else { | |
1280 | struct dirent *de; | |
1281 | ||
1282 | FOREACH_DIRENT_ALL(de, d, break) { | |
1283 | if (!dirent_is_file(de)) | |
1284 | continue; | |
1285 | ||
1286 | if (!startswith(de->d_name, "72-seat-")) | |
1287 | continue; | |
1288 | ||
1289 | if (!endswith(de->d_name, ".rules")) | |
1290 | continue; | |
1291 | ||
1292 | if (unlinkat(dirfd(d), de->d_name, 0) < 0) | |
1293 | log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name); | |
1294 | } | |
1295 | } | |
1296 | ||
1297 | return trigger_device(m, NULL); | |
1298 | } | |
1299 | ||
1300 | static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1301 | const char *sysfs, *seat; | |
1302 | Manager *m = userdata; | |
1303 | int interactive, r; | |
1304 | ||
1305 | assert(message); | |
1306 | assert(m); | |
1307 | ||
1308 | r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive); | |
1309 | if (r < 0) | |
1310 | return r; | |
1311 | ||
1312 | if (!path_startswith(sysfs, "/sys")) | |
1313 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs); | |
1314 | ||
1315 | if (!seat_name_is_valid(seat)) | |
1316 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat %s is not valid", seat); | |
1317 | ||
1318 | r = bus_verify_polkit_async( | |
1319 | message, | |
1320 | CAP_SYS_ADMIN, | |
1321 | "org.freedesktop.login1.attach-device", | |
1322 | NULL, | |
1323 | interactive, | |
1324 | UID_INVALID, | |
1325 | &m->polkit_registry, | |
1326 | error); | |
1327 | if (r < 0) | |
1328 | return r; | |
1329 | if (r == 0) | |
1330 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1331 | ||
1332 | r = attach_device(m, seat, sysfs); | |
1333 | if (r < 0) | |
1334 | return r; | |
1335 | ||
1336 | return sd_bus_reply_method_return(message, NULL); | |
1337 | } | |
1338 | ||
1339 | static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1340 | Manager *m = userdata; | |
1341 | int interactive, r; | |
1342 | ||
1343 | assert(message); | |
1344 | assert(m); | |
1345 | ||
1346 | r = sd_bus_message_read(message, "b", &interactive); | |
1347 | if (r < 0) | |
1348 | return r; | |
1349 | ||
1350 | r = bus_verify_polkit_async( | |
1351 | message, | |
1352 | CAP_SYS_ADMIN, | |
1353 | "org.freedesktop.login1.flush-devices", | |
1354 | NULL, | |
1355 | interactive, | |
1356 | UID_INVALID, | |
1357 | &m->polkit_registry, | |
1358 | error); | |
1359 | if (r < 0) | |
1360 | return r; | |
1361 | if (r == 0) | |
1362 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1363 | ||
1364 | r = flush_devices(m); | |
1365 | if (r < 0) | |
1366 | return r; | |
1367 | ||
1368 | return sd_bus_reply_method_return(message, NULL); | |
1369 | } | |
1370 | ||
1371 | static int have_multiple_sessions( | |
1372 | Manager *m, | |
1373 | uid_t uid) { | |
1374 | ||
1375 | Session *session; | |
1376 | Iterator i; | |
1377 | ||
1378 | assert(m); | |
1379 | ||
1380 | /* Check for other users' sessions. Greeter sessions do not | |
1381 | * count, and non-login sessions do not count either. */ | |
1382 | HASHMAP_FOREACH(session, m->sessions, i) | |
1383 | if (session->class == SESSION_USER && | |
1384 | session->user->uid != uid) | |
1385 | return true; | |
1386 | ||
1387 | return false; | |
1388 | } | |
1389 | ||
1390 | static int bus_manager_log_shutdown( | |
1391 | Manager *m, | |
1392 | const char *unit_name) { | |
1393 | ||
1394 | const char *p, *q; | |
1395 | ||
1396 | assert(m); | |
1397 | assert(unit_name); | |
1398 | ||
1399 | if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) { | |
1400 | p = "MESSAGE=System is powering down"; | |
1401 | q = "SHUTDOWN=power-off"; | |
1402 | } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) { | |
1403 | p = "MESSAGE=System is rebooting"; | |
1404 | q = "SHUTDOWN=reboot"; | |
1405 | } else if (streq(unit_name, SPECIAL_HALT_TARGET)) { | |
1406 | p = "MESSAGE=System is halting"; | |
1407 | q = "SHUTDOWN=halt"; | |
1408 | } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) { | |
1409 | p = "MESSAGE=System is rebooting with kexec"; | |
1410 | q = "SHUTDOWN=kexec"; | |
1411 | } else { | |
1412 | p = "MESSAGE=System is shutting down"; | |
1413 | q = NULL; | |
1414 | } | |
1415 | ||
1416 | if (isempty(m->wall_message)) | |
1417 | p = strjoina(p, "."); | |
1418 | else | |
1419 | p = strjoina(p, " (", m->wall_message, ")."); | |
1420 | ||
1421 | return log_struct(LOG_NOTICE, | |
1422 | "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR, | |
1423 | p, | |
1424 | q); | |
1425 | } | |
1426 | ||
1427 | static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) { | |
1428 | Manager *m = userdata; | |
1429 | ||
1430 | assert(e); | |
1431 | assert(m); | |
1432 | ||
1433 | m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source); | |
1434 | return 0; | |
1435 | } | |
1436 | ||
1437 | int manager_set_lid_switch_ignore(Manager *m, usec_t until) { | |
1438 | int r; | |
1439 | ||
1440 | assert(m); | |
1441 | ||
1442 | if (until <= now(CLOCK_MONOTONIC)) | |
1443 | return 0; | |
1444 | ||
1445 | /* We want to ignore the lid switch for a while after each | |
1446 | * suspend, and after boot-up. Hence let's install a timer for | |
1447 | * this. As long as the event source exists we ignore the lid | |
1448 | * switch. */ | |
1449 | ||
1450 | if (m->lid_switch_ignore_event_source) { | |
1451 | usec_t u; | |
1452 | ||
1453 | r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u); | |
1454 | if (r < 0) | |
1455 | return r; | |
1456 | ||
1457 | if (until <= u) | |
1458 | return 0; | |
1459 | ||
1460 | r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until); | |
1461 | } else | |
1462 | r = sd_event_add_time( | |
1463 | m->event, | |
1464 | &m->lid_switch_ignore_event_source, | |
1465 | CLOCK_MONOTONIC, | |
1466 | until, 0, | |
1467 | lid_switch_ignore_handler, m); | |
1468 | ||
1469 | return r; | |
1470 | } | |
1471 | ||
1472 | static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) { | |
1473 | ||
1474 | static const char * const signal_name[_INHIBIT_WHAT_MAX] = { | |
1475 | [INHIBIT_SHUTDOWN] = "PrepareForShutdown", | |
1476 | [INHIBIT_SLEEP] = "PrepareForSleep" | |
1477 | }; | |
1478 | ||
1479 | int active = _active; | |
1480 | ||
1481 | assert(m); | |
1482 | assert(w >= 0); | |
1483 | assert(w < _INHIBIT_WHAT_MAX); | |
1484 | assert(signal_name[w]); | |
1485 | ||
1486 | return sd_bus_emit_signal(m->bus, | |
1487 | "/org/freedesktop/login1", | |
1488 | "org.freedesktop.login1.Manager", | |
1489 | signal_name[w], | |
1490 | "b", | |
1491 | active); | |
1492 | } | |
1493 | ||
1494 | static int execute_shutdown_or_sleep( | |
1495 | Manager *m, | |
1496 | InhibitWhat w, | |
1497 | const char *unit_name, | |
1498 | sd_bus_error *error) { | |
1499 | ||
1500 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1501 | char *c = NULL; | |
1502 | const char *p; | |
1503 | int r; | |
1504 | ||
1505 | assert(m); | |
1506 | assert(w > 0); | |
1507 | assert(w < _INHIBIT_WHAT_MAX); | |
1508 | assert(unit_name); | |
1509 | ||
1510 | if (w == INHIBIT_SHUTDOWN) | |
1511 | bus_manager_log_shutdown(m, unit_name); | |
1512 | ||
1513 | r = sd_bus_call_method( | |
1514 | m->bus, | |
1515 | "org.freedesktop.systemd1", | |
1516 | "/org/freedesktop/systemd1", | |
1517 | "org.freedesktop.systemd1.Manager", | |
1518 | "StartUnit", | |
1519 | error, | |
1520 | &reply, | |
1521 | "ss", unit_name, "replace-irreversibly"); | |
1522 | if (r < 0) | |
1523 | goto error; | |
1524 | ||
1525 | r = sd_bus_message_read(reply, "o", &p); | |
1526 | if (r < 0) | |
1527 | goto error; | |
1528 | ||
1529 | c = strdup(p); | |
1530 | if (!c) { | |
1531 | r = -ENOMEM; | |
1532 | goto error; | |
1533 | } | |
1534 | ||
1535 | m->action_unit = unit_name; | |
1536 | free(m->action_job); | |
1537 | m->action_job = c; | |
1538 | m->action_what = w; | |
1539 | ||
1540 | /* Make sure the lid switch is ignored for a while */ | |
1541 | manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec); | |
1542 | ||
1543 | return 0; | |
1544 | ||
1545 | error: | |
1546 | /* Tell people that they now may take a lock again */ | |
1547 | (void) send_prepare_for(m, w, false); | |
1548 | ||
1549 | return r; | |
1550 | } | |
1551 | ||
1552 | int manager_dispatch_delayed(Manager *manager, bool timeout) { | |
1553 | ||
1554 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1555 | Inhibitor *offending = NULL; | |
1556 | int r; | |
1557 | ||
1558 | assert(manager); | |
1559 | ||
1560 | if (manager->action_what == 0 || manager->action_job) | |
1561 | return 0; | |
1562 | ||
1563 | if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) { | |
1564 | _cleanup_free_ char *comm = NULL, *u = NULL; | |
1565 | ||
1566 | if (!timeout) | |
1567 | return 0; | |
1568 | ||
1569 | (void) get_process_comm(offending->pid, &comm); | |
1570 | u = uid_to_name(offending->uid); | |
1571 | ||
1572 | log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.", | |
1573 | offending->uid, strna(u), | |
1574 | offending->pid, strna(comm)); | |
1575 | } | |
1576 | ||
1577 | /* Actually do the operation */ | |
1578 | r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error); | |
1579 | if (r < 0) { | |
1580 | log_warning("Error during inhibitor-delayed operation (already returned success to client): %s", | |
1581 | bus_error_message(&error, r)); | |
1582 | ||
1583 | manager->action_unit = NULL; | |
1584 | manager->action_what = 0; | |
1585 | return r; | |
1586 | } | |
1587 | ||
1588 | return 1; | |
1589 | } | |
1590 | ||
1591 | static int manager_inhibit_timeout_handler( | |
1592 | sd_event_source *s, | |
1593 | uint64_t usec, | |
1594 | void *userdata) { | |
1595 | ||
1596 | Manager *manager = userdata; | |
1597 | int r; | |
1598 | ||
1599 | assert(manager); | |
1600 | assert(manager->inhibit_timeout_source == s); | |
1601 | ||
1602 | r = manager_dispatch_delayed(manager, true); | |
1603 | return (r < 0) ? r : 0; | |
1604 | } | |
1605 | ||
1606 | static int delay_shutdown_or_sleep( | |
1607 | Manager *m, | |
1608 | InhibitWhat w, | |
1609 | const char *unit_name) { | |
1610 | ||
1611 | int r; | |
1612 | usec_t timeout_val; | |
1613 | ||
1614 | assert(m); | |
1615 | assert(w >= 0); | |
1616 | assert(w < _INHIBIT_WHAT_MAX); | |
1617 | assert(unit_name); | |
1618 | ||
1619 | timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max; | |
1620 | ||
1621 | if (m->inhibit_timeout_source) { | |
1622 | r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val); | |
1623 | if (r < 0) | |
1624 | return log_error_errno(r, "sd_event_source_set_time() failed: %m"); | |
1625 | ||
1626 | r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT); | |
1627 | if (r < 0) | |
1628 | return log_error_errno(r, "sd_event_source_set_enabled() failed: %m"); | |
1629 | } else { | |
1630 | r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC, | |
1631 | timeout_val, 0, manager_inhibit_timeout_handler, m); | |
1632 | if (r < 0) | |
1633 | return r; | |
1634 | } | |
1635 | ||
1636 | m->action_unit = unit_name; | |
1637 | m->action_what = w; | |
1638 | ||
1639 | return 0; | |
1640 | } | |
1641 | ||
1642 | int bus_manager_shutdown_or_sleep_now_or_later( | |
1643 | Manager *m, | |
1644 | const char *unit_name, | |
1645 | InhibitWhat w, | |
1646 | sd_bus_error *error) { | |
1647 | ||
1648 | _cleanup_free_ char *load_state = NULL; | |
1649 | bool delayed; | |
1650 | int r; | |
1651 | ||
1652 | assert(m); | |
1653 | assert(unit_name); | |
1654 | assert(w > 0); | |
1655 | assert(w <= _INHIBIT_WHAT_MAX); | |
1656 | assert(!m->action_job); | |
1657 | ||
1658 | r = unit_load_state(m->bus, unit_name, &load_state); | |
1659 | if (r < 0) | |
1660 | return r; | |
1661 | ||
1662 | if (!streq(load_state, "loaded")) { | |
1663 | log_notice("Unit %s is %s, refusing operation.", unit_name, load_state); | |
1664 | return -EACCES; | |
1665 | } | |
1666 | ||
1667 | /* Tell everybody to prepare for shutdown/sleep */ | |
1668 | (void) send_prepare_for(m, w, true); | |
1669 | ||
1670 | delayed = | |
1671 | m->inhibit_delay_max > 0 && | |
1672 | manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL); | |
1673 | ||
1674 | if (delayed) | |
1675 | /* Shutdown is delayed, keep in mind what we | |
1676 | * want to do, and start a timeout */ | |
1677 | r = delay_shutdown_or_sleep(m, w, unit_name); | |
1678 | else | |
1679 | /* Shutdown is not delayed, execute it | |
1680 | * immediately */ | |
1681 | r = execute_shutdown_or_sleep(m, w, unit_name, error); | |
1682 | ||
1683 | return r; | |
1684 | } | |
1685 | ||
1686 | static int verify_shutdown_creds( | |
1687 | Manager *m, | |
1688 | sd_bus_message *message, | |
1689 | InhibitWhat w, | |
1690 | bool interactive, | |
1691 | const char *action, | |
1692 | const char *action_multiple_sessions, | |
1693 | const char *action_ignore_inhibit, | |
1694 | sd_bus_error *error) { | |
1695 | ||
1696 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
1697 | bool multiple_sessions, blocked; | |
1698 | uid_t uid; | |
1699 | int r; | |
1700 | ||
1701 | assert(m); | |
1702 | assert(message); | |
1703 | assert(w >= 0); | |
1704 | assert(w <= _INHIBIT_WHAT_MAX); | |
1705 | ||
1706 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); | |
1707 | if (r < 0) | |
1708 | return r; | |
1709 | ||
1710 | r = sd_bus_creds_get_euid(creds, &uid); | |
1711 | if (r < 0) | |
1712 | return r; | |
1713 | ||
1714 | r = have_multiple_sessions(m, uid); | |
1715 | if (r < 0) | |
1716 | return r; | |
1717 | ||
1718 | multiple_sessions = r > 0; | |
1719 | blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL); | |
1720 | ||
1721 | if (multiple_sessions && action_multiple_sessions) { | |
1722 | r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error); | |
1723 | if (r < 0) | |
1724 | return r; | |
1725 | if (r == 0) | |
1726 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1727 | } | |
1728 | ||
1729 | if (blocked && action_ignore_inhibit) { | |
1730 | r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error); | |
1731 | if (r < 0) | |
1732 | return r; | |
1733 | if (r == 0) | |
1734 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1735 | } | |
1736 | ||
1737 | if (!multiple_sessions && !blocked && action) { | |
1738 | r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error); | |
1739 | if (r < 0) | |
1740 | return r; | |
1741 | if (r == 0) | |
1742 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1743 | } | |
1744 | ||
1745 | return 0; | |
1746 | } | |
1747 | ||
1748 | static int method_do_shutdown_or_sleep( | |
1749 | Manager *m, | |
1750 | sd_bus_message *message, | |
1751 | const char *unit_name, | |
1752 | InhibitWhat w, | |
1753 | const char *action, | |
1754 | const char *action_multiple_sessions, | |
1755 | const char *action_ignore_inhibit, | |
1756 | const char *sleep_verb, | |
1757 | sd_bus_error *error) { | |
1758 | ||
1759 | int interactive, r; | |
1760 | ||
1761 | assert(m); | |
1762 | assert(message); | |
1763 | assert(unit_name); | |
1764 | assert(w >= 0); | |
1765 | assert(w <= _INHIBIT_WHAT_MAX); | |
1766 | ||
1767 | r = sd_bus_message_read(message, "b", &interactive); | |
1768 | if (r < 0) | |
1769 | return r; | |
1770 | ||
1771 | /* Don't allow multiple jobs being executed at the same time */ | |
1772 | if (m->action_what) | |
1773 | return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress"); | |
1774 | ||
1775 | if (sleep_verb) { | |
1776 | r = can_sleep(sleep_verb); | |
1777 | if (r == -ENOSPC) | |
1778 | return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Not enough swap space for hibernation"); | |
1779 | if (r == -ENOMEDIUM) | |
1780 | return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Kernel image has been removed, can't hibernate"); | |
1781 | if (r == -EADV) | |
1782 | return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Resume not configured, can't hibernate"); | |
1783 | if (r == 0) | |
1784 | return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb \"%s\" not supported", sleep_verb); | |
1785 | if (r < 0) | |
1786 | return r; | |
1787 | } | |
1788 | ||
1789 | r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions, | |
1790 | action_ignore_inhibit, error); | |
1791 | if (r != 0) | |
1792 | return r; | |
1793 | ||
1794 | r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error); | |
1795 | if (r < 0) | |
1796 | return r; | |
1797 | ||
1798 | return sd_bus_reply_method_return(message, NULL); | |
1799 | } | |
1800 | ||
1801 | static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1802 | Manager *m = userdata; | |
1803 | ||
1804 | return method_do_shutdown_or_sleep( | |
1805 | m, message, | |
1806 | SPECIAL_POWEROFF_TARGET, | |
1807 | INHIBIT_SHUTDOWN, | |
1808 | "org.freedesktop.login1.power-off", | |
1809 | "org.freedesktop.login1.power-off-multiple-sessions", | |
1810 | "org.freedesktop.login1.power-off-ignore-inhibit", | |
1811 | NULL, | |
1812 | error); | |
1813 | } | |
1814 | ||
1815 | static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1816 | Manager *m = userdata; | |
1817 | ||
1818 | return method_do_shutdown_or_sleep( | |
1819 | m, message, | |
1820 | SPECIAL_REBOOT_TARGET, | |
1821 | INHIBIT_SHUTDOWN, | |
1822 | "org.freedesktop.login1.reboot", | |
1823 | "org.freedesktop.login1.reboot-multiple-sessions", | |
1824 | "org.freedesktop.login1.reboot-ignore-inhibit", | |
1825 | NULL, | |
1826 | error); | |
1827 | } | |
1828 | ||
1829 | static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1830 | Manager *m = userdata; | |
1831 | ||
1832 | return method_do_shutdown_or_sleep( | |
1833 | m, message, | |
1834 | SPECIAL_HALT_TARGET, | |
1835 | INHIBIT_SHUTDOWN, | |
1836 | "org.freedesktop.login1.halt", | |
1837 | "org.freedesktop.login1.halt-multiple-sessions", | |
1838 | "org.freedesktop.login1.halt-ignore-inhibit", | |
1839 | NULL, | |
1840 | error); | |
1841 | } | |
1842 | ||
1843 | static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1844 | Manager *m = userdata; | |
1845 | ||
1846 | return method_do_shutdown_or_sleep( | |
1847 | m, message, | |
1848 | SPECIAL_SUSPEND_TARGET, | |
1849 | INHIBIT_SLEEP, | |
1850 | "org.freedesktop.login1.suspend", | |
1851 | "org.freedesktop.login1.suspend-multiple-sessions", | |
1852 | "org.freedesktop.login1.suspend-ignore-inhibit", | |
1853 | "suspend", | |
1854 | error); | |
1855 | } | |
1856 | ||
1857 | static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1858 | Manager *m = userdata; | |
1859 | ||
1860 | return method_do_shutdown_or_sleep( | |
1861 | m, message, | |
1862 | SPECIAL_HIBERNATE_TARGET, | |
1863 | INHIBIT_SLEEP, | |
1864 | "org.freedesktop.login1.hibernate", | |
1865 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
1866 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
1867 | "hibernate", | |
1868 | error); | |
1869 | } | |
1870 | ||
1871 | static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1872 | Manager *m = userdata; | |
1873 | ||
1874 | return method_do_shutdown_or_sleep( | |
1875 | m, message, | |
1876 | SPECIAL_HYBRID_SLEEP_TARGET, | |
1877 | INHIBIT_SLEEP, | |
1878 | "org.freedesktop.login1.hibernate", | |
1879 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
1880 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
1881 | "hybrid-sleep", | |
1882 | error); | |
1883 | } | |
1884 | ||
1885 | static int method_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1886 | Manager *m = userdata; | |
1887 | ||
1888 | return method_do_shutdown_or_sleep( | |
1889 | m, message, | |
1890 | SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET, | |
1891 | INHIBIT_SLEEP, | |
1892 | "org.freedesktop.login1.hibernate", | |
1893 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
1894 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
1895 | "hybrid-sleep", | |
1896 | error); | |
1897 | } | |
1898 | ||
1899 | static int nologin_timeout_handler( | |
1900 | sd_event_source *s, | |
1901 | uint64_t usec, | |
1902 | void *userdata) { | |
1903 | ||
1904 | Manager *m = userdata; | |
1905 | ||
1906 | log_info("Creating /run/nologin, blocking further logins..."); | |
1907 | ||
1908 | m->unlink_nologin = | |
1909 | create_shutdown_run_nologin_or_warn() >= 0; | |
1910 | ||
1911 | return 0; | |
1912 | } | |
1913 | ||
1914 | static int update_schedule_file(Manager *m) { | |
1915 | _cleanup_free_ char *temp_path = NULL; | |
1916 | _cleanup_fclose_ FILE *f = NULL; | |
1917 | int r; | |
1918 | ||
1919 | assert(m); | |
1920 | ||
1921 | r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE); | |
1922 | if (r < 0) | |
1923 | return log_error_errno(r, "Failed to create shutdown subdirectory: %m"); | |
1924 | ||
1925 | r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path); | |
1926 | if (r < 0) | |
1927 | return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m"); | |
1928 | ||
1929 | (void) fchmod(fileno(f), 0644); | |
1930 | ||
1931 | fprintf(f, | |
1932 | "USEC="USEC_FMT"\n" | |
1933 | "WARN_WALL=%i\n" | |
1934 | "MODE=%s\n", | |
1935 | m->scheduled_shutdown_timeout, | |
1936 | m->enable_wall_messages, | |
1937 | m->scheduled_shutdown_type); | |
1938 | ||
1939 | if (!isempty(m->wall_message)) { | |
1940 | _cleanup_free_ char *t; | |
1941 | ||
1942 | t = cescape(m->wall_message); | |
1943 | if (!t) { | |
1944 | r = -ENOMEM; | |
1945 | goto fail; | |
1946 | } | |
1947 | ||
1948 | fprintf(f, "WALL_MESSAGE=%s\n", t); | |
1949 | } | |
1950 | ||
1951 | r = fflush_and_check(f); | |
1952 | if (r < 0) | |
1953 | goto fail; | |
1954 | ||
1955 | if (rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) { | |
1956 | r = -errno; | |
1957 | goto fail; | |
1958 | } | |
1959 | ||
1960 | return 0; | |
1961 | ||
1962 | fail: | |
1963 | (void) unlink(temp_path); | |
1964 | (void) unlink("/run/systemd/shutdown/scheduled"); | |
1965 | ||
1966 | return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m"); | |
1967 | } | |
1968 | ||
1969 | static void reset_scheduled_shutdown(Manager *m) { | |
1970 | assert(m); | |
1971 | ||
1972 | m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source); | |
1973 | m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source); | |
1974 | m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source); | |
1975 | ||
1976 | m->scheduled_shutdown_type = mfree(m->scheduled_shutdown_type); | |
1977 | m->scheduled_shutdown_timeout = 0; | |
1978 | m->shutdown_dry_run = false; | |
1979 | ||
1980 | if (m->unlink_nologin) { | |
1981 | (void) unlink_or_warn("/run/nologin"); | |
1982 | m->unlink_nologin = false; | |
1983 | } | |
1984 | ||
1985 | (void) unlink("/run/systemd/shutdown/scheduled"); | |
1986 | } | |
1987 | ||
1988 | static int manager_scheduled_shutdown_handler( | |
1989 | sd_event_source *s, | |
1990 | uint64_t usec, | |
1991 | void *userdata) { | |
1992 | ||
1993 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1994 | Manager *m = userdata; | |
1995 | const char *target; | |
1996 | int r; | |
1997 | ||
1998 | assert(m); | |
1999 | ||
2000 | if (isempty(m->scheduled_shutdown_type)) | |
2001 | return 0; | |
2002 | ||
2003 | if (streq(m->scheduled_shutdown_type, "poweroff")) | |
2004 | target = SPECIAL_POWEROFF_TARGET; | |
2005 | else if (streq(m->scheduled_shutdown_type, "reboot")) | |
2006 | target = SPECIAL_REBOOT_TARGET; | |
2007 | else if (streq(m->scheduled_shutdown_type, "halt")) | |
2008 | target = SPECIAL_HALT_TARGET; | |
2009 | else | |
2010 | assert_not_reached("unexpected shutdown type"); | |
2011 | ||
2012 | /* Don't allow multiple jobs being executed at the same time */ | |
2013 | if (m->action_what) { | |
2014 | r = -EALREADY; | |
2015 | log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target); | |
2016 | goto error; | |
2017 | } | |
2018 | ||
2019 | if (m->shutdown_dry_run) { | |
2020 | /* We do not process delay inhibitors here. Otherwise, we | |
2021 | * would have to be considered "in progress" (like the check | |
2022 | * above) for some seconds after our admin has seen the final | |
2023 | * wall message. */ | |
2024 | ||
2025 | bus_manager_log_shutdown(m, target); | |
2026 | log_info("Running in dry run, suppressing action."); | |
2027 | reset_scheduled_shutdown(m); | |
2028 | ||
2029 | return 0; | |
2030 | } | |
2031 | ||
2032 | r = bus_manager_shutdown_or_sleep_now_or_later(m, target, INHIBIT_SHUTDOWN, &error); | |
2033 | if (r < 0) { | |
2034 | log_error_errno(r, "Scheduled shutdown to %s failed: %m", target); | |
2035 | goto error; | |
2036 | } | |
2037 | ||
2038 | return 0; | |
2039 | ||
2040 | error: | |
2041 | reset_scheduled_shutdown(m); | |
2042 | return r; | |
2043 | } | |
2044 | ||
2045 | static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2046 | Manager *m = userdata; | |
2047 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
2048 | const char *action_multiple_sessions = NULL; | |
2049 | const char *action_ignore_inhibit = NULL; | |
2050 | const char *action = NULL; | |
2051 | uint64_t elapse; | |
2052 | char *type; | |
2053 | int r; | |
2054 | bool dry_run = false; | |
2055 | ||
2056 | assert(m); | |
2057 | assert(message); | |
2058 | ||
2059 | r = sd_bus_message_read(message, "st", &type, &elapse); | |
2060 | if (r < 0) | |
2061 | return r; | |
2062 | ||
2063 | if (startswith(type, "dry-")) { | |
2064 | type += 4; | |
2065 | dry_run = true; | |
2066 | } | |
2067 | ||
2068 | if (streq(type, "poweroff")) { | |
2069 | action = "org.freedesktop.login1.power-off"; | |
2070 | action_multiple_sessions = "org.freedesktop.login1.power-off-multiple-sessions"; | |
2071 | action_ignore_inhibit = "org.freedesktop.login1.power-off-ignore-inhibit"; | |
2072 | } else if (streq(type, "reboot")) { | |
2073 | action = "org.freedesktop.login1.reboot"; | |
2074 | action_multiple_sessions = "org.freedesktop.login1.reboot-multiple-sessions"; | |
2075 | action_ignore_inhibit = "org.freedesktop.login1.reboot-ignore-inhibit"; | |
2076 | } else if (streq(type, "halt")) { | |
2077 | action = "org.freedesktop.login1.halt"; | |
2078 | action_multiple_sessions = "org.freedesktop.login1.halt-multiple-sessions"; | |
2079 | action_ignore_inhibit = "org.freedesktop.login1.halt-ignore-inhibit"; | |
2080 | } else | |
2081 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type"); | |
2082 | ||
2083 | r = verify_shutdown_creds(m, message, INHIBIT_SHUTDOWN, false, | |
2084 | action, action_multiple_sessions, action_ignore_inhibit, error); | |
2085 | if (r != 0) | |
2086 | return r; | |
2087 | ||
2088 | if (m->scheduled_shutdown_timeout_source) { | |
2089 | r = sd_event_source_set_time(m->scheduled_shutdown_timeout_source, elapse); | |
2090 | if (r < 0) | |
2091 | return log_error_errno(r, "sd_event_source_set_time() failed: %m"); | |
2092 | ||
2093 | r = sd_event_source_set_enabled(m->scheduled_shutdown_timeout_source, SD_EVENT_ONESHOT); | |
2094 | if (r < 0) | |
2095 | return log_error_errno(r, "sd_event_source_set_enabled() failed: %m"); | |
2096 | } else { | |
2097 | r = sd_event_add_time(m->event, &m->scheduled_shutdown_timeout_source, | |
2098 | CLOCK_REALTIME, elapse, 0, manager_scheduled_shutdown_handler, m); | |
2099 | if (r < 0) | |
2100 | return log_error_errno(r, "sd_event_add_time() failed: %m"); | |
2101 | } | |
2102 | ||
2103 | r = free_and_strdup(&m->scheduled_shutdown_type, type); | |
2104 | if (r < 0) { | |
2105 | m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source); | |
2106 | return log_oom(); | |
2107 | } | |
2108 | ||
2109 | m->shutdown_dry_run = dry_run; | |
2110 | ||
2111 | if (m->nologin_timeout_source) { | |
2112 | r = sd_event_source_set_time(m->nologin_timeout_source, elapse); | |
2113 | if (r < 0) | |
2114 | return log_error_errno(r, "sd_event_source_set_time() failed: %m"); | |
2115 | ||
2116 | r = sd_event_source_set_enabled(m->nologin_timeout_source, SD_EVENT_ONESHOT); | |
2117 | if (r < 0) | |
2118 | return log_error_errno(r, "sd_event_source_set_enabled() failed: %m"); | |
2119 | } else { | |
2120 | r = sd_event_add_time(m->event, &m->nologin_timeout_source, | |
2121 | CLOCK_REALTIME, elapse - 5 * USEC_PER_MINUTE, 0, nologin_timeout_handler, m); | |
2122 | if (r < 0) | |
2123 | return log_error_errno(r, "sd_event_add_time() failed: %m"); | |
2124 | } | |
2125 | ||
2126 | m->scheduled_shutdown_timeout = elapse; | |
2127 | ||
2128 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds); | |
2129 | if (r >= 0) { | |
2130 | const char *tty = NULL; | |
2131 | ||
2132 | (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid); | |
2133 | (void) sd_bus_creds_get_tty(creds, &tty); | |
2134 | ||
2135 | r = free_and_strdup(&m->scheduled_shutdown_tty, tty); | |
2136 | if (r < 0) { | |
2137 | m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source); | |
2138 | return log_oom(); | |
2139 | } | |
2140 | } | |
2141 | ||
2142 | r = manager_setup_wall_message_timer(m); | |
2143 | if (r < 0) | |
2144 | return r; | |
2145 | ||
2146 | r = update_schedule_file(m); | |
2147 | if (r < 0) | |
2148 | return r; | |
2149 | ||
2150 | return sd_bus_reply_method_return(message, NULL); | |
2151 | } | |
2152 | ||
2153 | static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2154 | Manager *m = userdata; | |
2155 | bool cancelled; | |
2156 | ||
2157 | assert(m); | |
2158 | assert(message); | |
2159 | ||
2160 | cancelled = m->scheduled_shutdown_type != NULL; | |
2161 | reset_scheduled_shutdown(m); | |
2162 | ||
2163 | if (cancelled && m->enable_wall_messages) { | |
2164 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
2165 | _cleanup_free_ char *username = NULL; | |
2166 | const char *tty = NULL; | |
2167 | uid_t uid = 0; | |
2168 | int r; | |
2169 | ||
2170 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds); | |
2171 | if (r >= 0) { | |
2172 | (void) sd_bus_creds_get_uid(creds, &uid); | |
2173 | (void) sd_bus_creds_get_tty(creds, &tty); | |
2174 | } | |
2175 | ||
2176 | username = uid_to_name(uid); | |
2177 | utmp_wall("The system shutdown has been cancelled", | |
2178 | username, tty, logind_wall_tty_filter, m); | |
2179 | } | |
2180 | ||
2181 | return sd_bus_reply_method_return(message, "b", cancelled); | |
2182 | } | |
2183 | ||
2184 | static int method_can_shutdown_or_sleep( | |
2185 | Manager *m, | |
2186 | sd_bus_message *message, | |
2187 | InhibitWhat w, | |
2188 | const char *action, | |
2189 | const char *action_multiple_sessions, | |
2190 | const char *action_ignore_inhibit, | |
2191 | const char *sleep_verb, | |
2192 | sd_bus_error *error) { | |
2193 | ||
2194 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
2195 | HandleAction handle; | |
2196 | bool multiple_sessions, challenge, blocked; | |
2197 | const char *result = NULL; | |
2198 | uid_t uid; | |
2199 | int r; | |
2200 | ||
2201 | assert(m); | |
2202 | assert(message); | |
2203 | assert(w >= 0); | |
2204 | assert(w <= _INHIBIT_WHAT_MAX); | |
2205 | assert(action); | |
2206 | assert(action_multiple_sessions); | |
2207 | assert(action_ignore_inhibit); | |
2208 | ||
2209 | if (sleep_verb) { | |
2210 | r = can_sleep(sleep_verb); | |
2211 | if (IN_SET(r, 0, -ENOSPC, -ENOMEDIUM, -EADV)) | |
2212 | return sd_bus_reply_method_return(message, "s", "na"); | |
2213 | if (r < 0) | |
2214 | return r; | |
2215 | } | |
2216 | ||
2217 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); | |
2218 | if (r < 0) | |
2219 | return r; | |
2220 | ||
2221 | r = sd_bus_creds_get_euid(creds, &uid); | |
2222 | if (r < 0) | |
2223 | return r; | |
2224 | ||
2225 | r = have_multiple_sessions(m, uid); | |
2226 | if (r < 0) | |
2227 | return r; | |
2228 | ||
2229 | multiple_sessions = r > 0; | |
2230 | blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL); | |
2231 | ||
2232 | handle = handle_action_from_string(sleep_verb); | |
2233 | if (handle >= 0) { | |
2234 | const char *target; | |
2235 | ||
2236 | target = manager_target_for_action(handle); | |
2237 | if (target) { | |
2238 | _cleanup_free_ char *load_state = NULL; | |
2239 | ||
2240 | r = unit_load_state(m->bus, target, &load_state); | |
2241 | if (r < 0) | |
2242 | return r; | |
2243 | ||
2244 | if (!streq(load_state, "loaded")) { | |
2245 | result = "no"; | |
2246 | goto finish; | |
2247 | } | |
2248 | } | |
2249 | } | |
2250 | ||
2251 | if (multiple_sessions) { | |
2252 | r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error); | |
2253 | if (r < 0) | |
2254 | return r; | |
2255 | ||
2256 | if (r > 0) | |
2257 | result = "yes"; | |
2258 | else if (challenge) | |
2259 | result = "challenge"; | |
2260 | else | |
2261 | result = "no"; | |
2262 | } | |
2263 | ||
2264 | if (blocked) { | |
2265 | r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error); | |
2266 | if (r < 0) | |
2267 | return r; | |
2268 | ||
2269 | if (r > 0 && !result) | |
2270 | result = "yes"; | |
2271 | else if (challenge && (!result || streq(result, "yes"))) | |
2272 | result = "challenge"; | |
2273 | else | |
2274 | result = "no"; | |
2275 | } | |
2276 | ||
2277 | if (!multiple_sessions && !blocked) { | |
2278 | /* If neither inhibit nor multiple sessions | |
2279 | * apply then just check the normal policy */ | |
2280 | ||
2281 | r = bus_test_polkit(message, CAP_SYS_BOOT, action, NULL, UID_INVALID, &challenge, error); | |
2282 | if (r < 0) | |
2283 | return r; | |
2284 | ||
2285 | if (r > 0) | |
2286 | result = "yes"; | |
2287 | else if (challenge) | |
2288 | result = "challenge"; | |
2289 | else | |
2290 | result = "no"; | |
2291 | } | |
2292 | ||
2293 | finish: | |
2294 | return sd_bus_reply_method_return(message, "s", result); | |
2295 | } | |
2296 | ||
2297 | static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2298 | Manager *m = userdata; | |
2299 | ||
2300 | return method_can_shutdown_or_sleep( | |
2301 | m, message, | |
2302 | INHIBIT_SHUTDOWN, | |
2303 | "org.freedesktop.login1.power-off", | |
2304 | "org.freedesktop.login1.power-off-multiple-sessions", | |
2305 | "org.freedesktop.login1.power-off-ignore-inhibit", | |
2306 | NULL, | |
2307 | error); | |
2308 | } | |
2309 | ||
2310 | static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2311 | Manager *m = userdata; | |
2312 | ||
2313 | return method_can_shutdown_or_sleep( | |
2314 | m, message, | |
2315 | INHIBIT_SHUTDOWN, | |
2316 | "org.freedesktop.login1.reboot", | |
2317 | "org.freedesktop.login1.reboot-multiple-sessions", | |
2318 | "org.freedesktop.login1.reboot-ignore-inhibit", | |
2319 | NULL, | |
2320 | error); | |
2321 | } | |
2322 | ||
2323 | static int method_can_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2324 | Manager *m = userdata; | |
2325 | ||
2326 | return method_can_shutdown_or_sleep( | |
2327 | m, message, | |
2328 | INHIBIT_SHUTDOWN, | |
2329 | "org.freedesktop.login1.halt", | |
2330 | "org.freedesktop.login1.halt-multiple-sessions", | |
2331 | "org.freedesktop.login1.halt-ignore-inhibit", | |
2332 | NULL, | |
2333 | error); | |
2334 | } | |
2335 | ||
2336 | static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2337 | Manager *m = userdata; | |
2338 | ||
2339 | return method_can_shutdown_or_sleep( | |
2340 | m, message, | |
2341 | INHIBIT_SLEEP, | |
2342 | "org.freedesktop.login1.suspend", | |
2343 | "org.freedesktop.login1.suspend-multiple-sessions", | |
2344 | "org.freedesktop.login1.suspend-ignore-inhibit", | |
2345 | "suspend", | |
2346 | error); | |
2347 | } | |
2348 | ||
2349 | static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2350 | Manager *m = userdata; | |
2351 | ||
2352 | return method_can_shutdown_or_sleep( | |
2353 | m, message, | |
2354 | INHIBIT_SLEEP, | |
2355 | "org.freedesktop.login1.hibernate", | |
2356 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
2357 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
2358 | "hibernate", | |
2359 | error); | |
2360 | } | |
2361 | ||
2362 | static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2363 | Manager *m = userdata; | |
2364 | ||
2365 | return method_can_shutdown_or_sleep( | |
2366 | m, message, | |
2367 | INHIBIT_SLEEP, | |
2368 | "org.freedesktop.login1.hibernate", | |
2369 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
2370 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
2371 | "hybrid-sleep", | |
2372 | error); | |
2373 | } | |
2374 | ||
2375 | static int method_can_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2376 | Manager *m = userdata; | |
2377 | ||
2378 | return method_can_shutdown_or_sleep( | |
2379 | m, message, | |
2380 | INHIBIT_SLEEP, | |
2381 | "org.freedesktop.login1.hibernate", | |
2382 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
2383 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
2384 | "suspend-then-hibernate", | |
2385 | error); | |
2386 | } | |
2387 | ||
2388 | static int property_get_reboot_to_firmware_setup( | |
2389 | sd_bus *bus, | |
2390 | const char *path, | |
2391 | const char *interface, | |
2392 | const char *property, | |
2393 | sd_bus_message *reply, | |
2394 | void *userdata, | |
2395 | sd_bus_error *error) { | |
2396 | int r; | |
2397 | ||
2398 | assert(bus); | |
2399 | assert(reply); | |
2400 | assert(userdata); | |
2401 | ||
2402 | r = efi_get_reboot_to_firmware(); | |
2403 | if (r < 0 && r != -EOPNOTSUPP) | |
2404 | log_warning_errno(r, "Failed to determine reboot-to-firmware state: %m"); | |
2405 | ||
2406 | return sd_bus_message_append(reply, "b", r > 0); | |
2407 | } | |
2408 | ||
2409 | static int method_set_reboot_to_firmware_setup( | |
2410 | sd_bus_message *message, | |
2411 | void *userdata, | |
2412 | sd_bus_error *error) { | |
2413 | ||
2414 | int b, r; | |
2415 | Manager *m = userdata; | |
2416 | ||
2417 | assert(message); | |
2418 | assert(m); | |
2419 | ||
2420 | r = sd_bus_message_read(message, "b", &b); | |
2421 | if (r < 0) | |
2422 | return r; | |
2423 | ||
2424 | r = bus_verify_polkit_async(message, | |
2425 | CAP_SYS_ADMIN, | |
2426 | "org.freedesktop.login1.set-reboot-to-firmware-setup", | |
2427 | NULL, | |
2428 | false, | |
2429 | UID_INVALID, | |
2430 | &m->polkit_registry, | |
2431 | error); | |
2432 | if (r < 0) | |
2433 | return r; | |
2434 | if (r == 0) | |
2435 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2436 | ||
2437 | r = efi_set_reboot_to_firmware(b); | |
2438 | if (r < 0) | |
2439 | return r; | |
2440 | ||
2441 | return sd_bus_reply_method_return(message, NULL); | |
2442 | } | |
2443 | ||
2444 | static int method_can_reboot_to_firmware_setup( | |
2445 | sd_bus_message *message, | |
2446 | void *userdata, | |
2447 | sd_bus_error *error) { | |
2448 | ||
2449 | int r; | |
2450 | bool challenge; | |
2451 | const char *result; | |
2452 | Manager *m = userdata; | |
2453 | ||
2454 | assert(message); | |
2455 | assert(m); | |
2456 | ||
2457 | r = efi_reboot_to_firmware_supported(); | |
2458 | if (r < 0) { | |
2459 | if (r != -EOPNOTSUPP) | |
2460 | log_warning_errno(errno, "Failed to determine whether reboot to firmware is supported: %m"); | |
2461 | ||
2462 | return sd_bus_reply_method_return(message, "s", "na"); | |
2463 | } | |
2464 | ||
2465 | r = bus_test_polkit(message, | |
2466 | CAP_SYS_ADMIN, | |
2467 | "org.freedesktop.login1.set-reboot-to-firmware-setup", | |
2468 | NULL, | |
2469 | UID_INVALID, | |
2470 | &challenge, | |
2471 | error); | |
2472 | if (r < 0) | |
2473 | return r; | |
2474 | ||
2475 | if (r > 0) | |
2476 | result = "yes"; | |
2477 | else if (challenge) | |
2478 | result = "challenge"; | |
2479 | else | |
2480 | result = "no"; | |
2481 | ||
2482 | return sd_bus_reply_method_return(message, "s", result); | |
2483 | } | |
2484 | ||
2485 | static int method_set_wall_message( | |
2486 | sd_bus_message *message, | |
2487 | void *userdata, | |
2488 | sd_bus_error *error) { | |
2489 | ||
2490 | int r; | |
2491 | Manager *m = userdata; | |
2492 | char *wall_message; | |
2493 | unsigned enable_wall_messages; | |
2494 | ||
2495 | assert(message); | |
2496 | assert(m); | |
2497 | ||
2498 | r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages); | |
2499 | if (r < 0) | |
2500 | return r; | |
2501 | ||
2502 | r = bus_verify_polkit_async(message, | |
2503 | CAP_SYS_ADMIN, | |
2504 | "org.freedesktop.login1.set-wall-message", | |
2505 | NULL, | |
2506 | false, | |
2507 | UID_INVALID, | |
2508 | &m->polkit_registry, | |
2509 | error); | |
2510 | if (r < 0) | |
2511 | return r; | |
2512 | if (r == 0) | |
2513 | return 1; /* Will call us back */ | |
2514 | ||
2515 | r = free_and_strdup(&m->wall_message, empty_to_null(wall_message)); | |
2516 | if (r < 0) | |
2517 | return log_oom(); | |
2518 | ||
2519 | m->enable_wall_messages = enable_wall_messages; | |
2520 | ||
2521 | return sd_bus_reply_method_return(message, NULL); | |
2522 | } | |
2523 | ||
2524 | static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2525 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
2526 | const char *who, *why, *what, *mode; | |
2527 | _cleanup_free_ char *id = NULL; | |
2528 | _cleanup_close_ int fifo_fd = -1; | |
2529 | Manager *m = userdata; | |
2530 | Inhibitor *i = NULL; | |
2531 | InhibitMode mm; | |
2532 | InhibitWhat w; | |
2533 | pid_t pid; | |
2534 | uid_t uid; | |
2535 | int r; | |
2536 | ||
2537 | assert(message); | |
2538 | assert(m); | |
2539 | ||
2540 | r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode); | |
2541 | if (r < 0) | |
2542 | return r; | |
2543 | ||
2544 | w = inhibit_what_from_string(what); | |
2545 | if (w <= 0) | |
2546 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid what specification %s", what); | |
2547 | ||
2548 | mm = inhibit_mode_from_string(mode); | |
2549 | if (mm < 0) | |
2550 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid mode specification %s", mode); | |
2551 | ||
2552 | /* Delay is only supported for shutdown/sleep */ | |
2553 | if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP))) | |
2554 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep"); | |
2555 | ||
2556 | /* Don't allow taking delay locks while we are already | |
2557 | * executing the operation. We shouldn't create the impression | |
2558 | * that the lock was successful if the machine is about to go | |
2559 | * down/suspend any moment. */ | |
2560 | if (m->action_what & w) | |
2561 | return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "The operation inhibition has been requested for is already running"); | |
2562 | ||
2563 | r = bus_verify_polkit_async( | |
2564 | message, | |
2565 | CAP_SYS_BOOT, | |
2566 | w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") : | |
2567 | w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") : | |
2568 | w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" : | |
2569 | w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" : | |
2570 | w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" : | |
2571 | w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" : | |
2572 | "org.freedesktop.login1.inhibit-handle-lid-switch", | |
2573 | NULL, | |
2574 | false, | |
2575 | UID_INVALID, | |
2576 | &m->polkit_registry, | |
2577 | error); | |
2578 | if (r < 0) | |
2579 | return r; | |
2580 | if (r == 0) | |
2581 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2582 | ||
2583 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds); | |
2584 | if (r < 0) | |
2585 | return r; | |
2586 | ||
2587 | r = sd_bus_creds_get_euid(creds, &uid); | |
2588 | if (r < 0) | |
2589 | return r; | |
2590 | ||
2591 | r = sd_bus_creds_get_pid(creds, &pid); | |
2592 | if (r < 0) | |
2593 | return r; | |
2594 | ||
2595 | if (hashmap_size(m->inhibitors) >= m->inhibitors_max) | |
2596 | return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", m->inhibitors_max); | |
2597 | ||
2598 | do { | |
2599 | id = mfree(id); | |
2600 | ||
2601 | if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0) | |
2602 | return -ENOMEM; | |
2603 | ||
2604 | } while (hashmap_get(m->inhibitors, id)); | |
2605 | ||
2606 | r = manager_add_inhibitor(m, id, &i); | |
2607 | if (r < 0) | |
2608 | return r; | |
2609 | ||
2610 | i->what = w; | |
2611 | i->mode = mm; | |
2612 | i->pid = pid; | |
2613 | i->uid = uid; | |
2614 | i->why = strdup(why); | |
2615 | i->who = strdup(who); | |
2616 | ||
2617 | if (!i->why || !i->who) { | |
2618 | r = -ENOMEM; | |
2619 | goto fail; | |
2620 | } | |
2621 | ||
2622 | fifo_fd = inhibitor_create_fifo(i); | |
2623 | if (fifo_fd < 0) { | |
2624 | r = fifo_fd; | |
2625 | goto fail; | |
2626 | } | |
2627 | ||
2628 | inhibitor_start(i); | |
2629 | ||
2630 | return sd_bus_reply_method_return(message, "h", fifo_fd); | |
2631 | ||
2632 | fail: | |
2633 | if (i) | |
2634 | inhibitor_free(i); | |
2635 | ||
2636 | return r; | |
2637 | } | |
2638 | ||
2639 | const sd_bus_vtable manager_vtable[] = { | |
2640 | SD_BUS_VTABLE_START(0), | |
2641 | ||
2642 | SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0), | |
2643 | SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0), | |
2644 | ||
2645 | SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST), | |
2646 | SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST), | |
2647 | SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST), | |
2648 | SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST), | |
2649 | SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
2650 | SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
2651 | SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
2652 | SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
2653 | SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
2654 | SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
2655 | SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST), | |
2656 | SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL, offsetof(Manager, user_stop_delay), SD_BUS_VTABLE_PROPERTY_CONST), | |
2657 | SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST), | |
2658 | SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST), | |
2659 | SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST), | |
2660 | SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST), | |
2661 | SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_ep), SD_BUS_VTABLE_PROPERTY_CONST), | |
2662 | SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST), | |
2663 | SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST), | |
2664 | SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST), | |
2665 | SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST), | |
2666 | SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0), | |
2667 | SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0), | |
2668 | SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0), | |
2669 | SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0), | |
2670 | SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed, 0, 0), | |
2671 | SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST), | |
2672 | SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST), | |
2673 | SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST), | |
2674 | SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size, offsetof(Manager, inhibitors), 0), | |
2675 | SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST), | |
2676 | SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size, offsetof(Manager, sessions), 0), | |
2677 | SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), | |
2678 | ||
2679 | SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED), | |
2680 | SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED), | |
2681 | SD_BUS_METHOD("GetUser", "u", "o", method_get_user, SD_BUS_VTABLE_UNPRIVILEGED), | |
2682 | SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid, SD_BUS_VTABLE_UNPRIVILEGED), | |
2683 | SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat, SD_BUS_VTABLE_UNPRIVILEGED), | |
2684 | SD_BUS_METHOD("ListSessions", NULL, "a(susso)", method_list_sessions, SD_BUS_VTABLE_UNPRIVILEGED), | |
2685 | SD_BUS_METHOD("ListUsers", NULL, "a(uso)", method_list_users, SD_BUS_VTABLE_UNPRIVILEGED), | |
2686 | SD_BUS_METHOD("ListSeats", NULL, "a(so)", method_list_seats, SD_BUS_VTABLE_UNPRIVILEGED), | |
2687 | SD_BUS_METHOD("ListInhibitors", NULL, "a(ssssuu)", method_list_inhibitors, SD_BUS_VTABLE_UNPRIVILEGED), | |
2688 | SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session, 0), | |
2689 | SD_BUS_METHOD("ReleaseSession", "s", NULL, method_release_session, 0), | |
2690 | SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, SD_BUS_VTABLE_UNPRIVILEGED), | |
2691 | SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL, method_activate_session_on_seat, SD_BUS_VTABLE_UNPRIVILEGED), | |
2692 | SD_BUS_METHOD("LockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED), | |
2693 | SD_BUS_METHOD("UnlockSession", "s", NULL, method_lock_session, SD_BUS_VTABLE_UNPRIVILEGED), | |
2694 | SD_BUS_METHOD("LockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED), | |
2695 | SD_BUS_METHOD("UnlockSessions", NULL, NULL, method_lock_sessions, SD_BUS_VTABLE_UNPRIVILEGED), | |
2696 | SD_BUS_METHOD("KillSession", "ssi", NULL, method_kill_session, SD_BUS_VTABLE_UNPRIVILEGED), | |
2697 | SD_BUS_METHOD("KillUser", "ui", NULL, method_kill_user, SD_BUS_VTABLE_UNPRIVILEGED), | |
2698 | SD_BUS_METHOD("TerminateSession", "s", NULL, method_terminate_session, SD_BUS_VTABLE_UNPRIVILEGED), | |
2699 | SD_BUS_METHOD("TerminateUser", "u", NULL, method_terminate_user, SD_BUS_VTABLE_UNPRIVILEGED), | |
2700 | SD_BUS_METHOD("TerminateSeat", "s", NULL, method_terminate_seat, SD_BUS_VTABLE_UNPRIVILEGED), | |
2701 | SD_BUS_METHOD("SetUserLinger", "ubb", NULL, method_set_user_linger, SD_BUS_VTABLE_UNPRIVILEGED), | |
2702 | SD_BUS_METHOD("AttachDevice", "ssb", NULL, method_attach_device, SD_BUS_VTABLE_UNPRIVILEGED), | |
2703 | SD_BUS_METHOD("FlushDevices", "b", NULL, method_flush_devices, SD_BUS_VTABLE_UNPRIVILEGED), | |
2704 | SD_BUS_METHOD("PowerOff", "b", NULL, method_poweroff, SD_BUS_VTABLE_UNPRIVILEGED), | |
2705 | SD_BUS_METHOD("Reboot", "b", NULL, method_reboot, SD_BUS_VTABLE_UNPRIVILEGED), | |
2706 | SD_BUS_METHOD("Halt", "b", NULL, method_halt, SD_BUS_VTABLE_UNPRIVILEGED), | |
2707 | SD_BUS_METHOD("Suspend", "b", NULL, method_suspend, SD_BUS_VTABLE_UNPRIVILEGED), | |
2708 | SD_BUS_METHOD("Hibernate", "b", NULL, method_hibernate, SD_BUS_VTABLE_UNPRIVILEGED), | |
2709 | SD_BUS_METHOD("HybridSleep", "b", NULL, method_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED), | |
2710 | SD_BUS_METHOD("SuspendThenHibernate", "b", NULL, method_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED), | |
2711 | SD_BUS_METHOD("CanPowerOff", NULL, "s", method_can_poweroff, SD_BUS_VTABLE_UNPRIVILEGED), | |
2712 | SD_BUS_METHOD("CanReboot", NULL, "s", method_can_reboot, SD_BUS_VTABLE_UNPRIVILEGED), | |
2713 | SD_BUS_METHOD("CanHalt", NULL, "s", method_can_halt, SD_BUS_VTABLE_UNPRIVILEGED), | |
2714 | SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, SD_BUS_VTABLE_UNPRIVILEGED), | |
2715 | SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, SD_BUS_VTABLE_UNPRIVILEGED), | |
2716 | SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED), | |
2717 | SD_BUS_METHOD("CanSuspendThenHibernate", NULL, "s", method_can_suspend_then_hibernate, SD_BUS_VTABLE_UNPRIVILEGED), | |
2718 | SD_BUS_METHOD("ScheduleShutdown", "st", NULL, method_schedule_shutdown, SD_BUS_VTABLE_UNPRIVILEGED), | |
2719 | SD_BUS_METHOD("CancelScheduledShutdown", NULL, "b", method_cancel_scheduled_shutdown, SD_BUS_VTABLE_UNPRIVILEGED), | |
2720 | SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, SD_BUS_VTABLE_UNPRIVILEGED), | |
2721 | SD_BUS_METHOD("CanRebootToFirmwareSetup", NULL, "s", method_can_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED), | |
2722 | SD_BUS_METHOD("SetRebootToFirmwareSetup", "b", NULL, method_set_reboot_to_firmware_setup, SD_BUS_VTABLE_UNPRIVILEGED), | |
2723 | SD_BUS_METHOD("SetWallMessage", "sb", NULL, method_set_wall_message, SD_BUS_VTABLE_UNPRIVILEGED), | |
2724 | ||
2725 | SD_BUS_SIGNAL("SessionNew", "so", 0), | |
2726 | SD_BUS_SIGNAL("SessionRemoved", "so", 0), | |
2727 | SD_BUS_SIGNAL("UserNew", "uo", 0), | |
2728 | SD_BUS_SIGNAL("UserRemoved", "uo", 0), | |
2729 | SD_BUS_SIGNAL("SeatNew", "so", 0), | |
2730 | SD_BUS_SIGNAL("SeatRemoved", "so", 0), | |
2731 | SD_BUS_SIGNAL("PrepareForShutdown", "b", 0), | |
2732 | SD_BUS_SIGNAL("PrepareForSleep", "b", 0), | |
2733 | ||
2734 | SD_BUS_VTABLE_END | |
2735 | }; | |
2736 | ||
2737 | static int session_jobs_reply(Session *s, const char *unit, const char *result) { | |
2738 | assert(s); | |
2739 | assert(unit); | |
2740 | ||
2741 | if (!s->started) | |
2742 | return 0; | |
2743 | ||
2744 | if (result && !streq(result, "done")) { | |
2745 | _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; | |
2746 | ||
2747 | sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit '%s' failed with '%s'", unit, result); | |
2748 | return session_send_create_reply(s, &e); | |
2749 | } | |
2750 | ||
2751 | return session_send_create_reply(s, NULL); | |
2752 | } | |
2753 | ||
2754 | int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2755 | const char *path, *result, *unit; | |
2756 | Manager *m = userdata; | |
2757 | Session *session; | |
2758 | uint32_t id; | |
2759 | User *user; | |
2760 | int r; | |
2761 | ||
2762 | assert(message); | |
2763 | assert(m); | |
2764 | ||
2765 | r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result); | |
2766 | if (r < 0) { | |
2767 | bus_log_parse_error(r); | |
2768 | return 0; | |
2769 | } | |
2770 | ||
2771 | if (m->action_job && streq(m->action_job, path)) { | |
2772 | log_info("Operation '%s' finished.", inhibit_what_to_string(m->action_what)); | |
2773 | ||
2774 | /* Tell people that they now may take a lock again */ | |
2775 | (void) send_prepare_for(m, m->action_what, false); | |
2776 | ||
2777 | m->action_job = mfree(m->action_job); | |
2778 | m->action_unit = NULL; | |
2779 | m->action_what = 0; | |
2780 | return 0; | |
2781 | } | |
2782 | ||
2783 | session = hashmap_get(m->session_units, unit); | |
2784 | if (session) { | |
2785 | if (streq_ptr(path, session->scope_job)) { | |
2786 | session->scope_job = mfree(session->scope_job); | |
2787 | (void) session_jobs_reply(session, unit, result); | |
2788 | ||
2789 | session_save(session); | |
2790 | user_save(session->user); | |
2791 | } | |
2792 | ||
2793 | session_add_to_gc_queue(session); | |
2794 | } | |
2795 | ||
2796 | user = hashmap_get(m->user_units, unit); | |
2797 | if (user) { | |
2798 | if (streq_ptr(path, user->service_job)) { | |
2799 | user->service_job = mfree(user->service_job); | |
2800 | ||
2801 | LIST_FOREACH(sessions_by_user, session, user->sessions) | |
2802 | (void) session_jobs_reply(session, unit, NULL /* don't propagate user service failures to the client */); | |
2803 | ||
2804 | user_save(user); | |
2805 | } | |
2806 | ||
2807 | user_add_to_gc_queue(user); | |
2808 | } | |
2809 | ||
2810 | return 0; | |
2811 | } | |
2812 | ||
2813 | int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2814 | const char *path, *unit; | |
2815 | Manager *m = userdata; | |
2816 | Session *session; | |
2817 | User *user; | |
2818 | int r; | |
2819 | ||
2820 | assert(message); | |
2821 | assert(m); | |
2822 | ||
2823 | r = sd_bus_message_read(message, "so", &unit, &path); | |
2824 | if (r < 0) { | |
2825 | bus_log_parse_error(r); | |
2826 | return 0; | |
2827 | } | |
2828 | ||
2829 | session = hashmap_get(m->session_units, unit); | |
2830 | if (session) | |
2831 | session_add_to_gc_queue(session); | |
2832 | ||
2833 | user = hashmap_get(m->user_units, unit); | |
2834 | if (user) | |
2835 | user_add_to_gc_queue(user); | |
2836 | ||
2837 | return 0; | |
2838 | } | |
2839 | ||
2840 | int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2841 | _cleanup_free_ char *unit = NULL; | |
2842 | Manager *m = userdata; | |
2843 | const char *path; | |
2844 | Session *session; | |
2845 | User *user; | |
2846 | int r; | |
2847 | ||
2848 | assert(message); | |
2849 | assert(m); | |
2850 | ||
2851 | path = sd_bus_message_get_path(message); | |
2852 | if (!path) | |
2853 | return 0; | |
2854 | ||
2855 | r = unit_name_from_dbus_path(path, &unit); | |
2856 | if (r == -EINVAL) /* not a unit */ | |
2857 | return 0; | |
2858 | if (r < 0) { | |
2859 | log_oom(); | |
2860 | return 0; | |
2861 | } | |
2862 | ||
2863 | session = hashmap_get(m->session_units, unit); | |
2864 | if (session) | |
2865 | session_add_to_gc_queue(session); | |
2866 | ||
2867 | user = hashmap_get(m->user_units, unit); | |
2868 | if (user) | |
2869 | user_add_to_gc_queue(user); | |
2870 | ||
2871 | return 0; | |
2872 | } | |
2873 | ||
2874 | int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
2875 | Manager *m = userdata; | |
2876 | Session *session; | |
2877 | Iterator i; | |
2878 | int b, r; | |
2879 | ||
2880 | assert(message); | |
2881 | assert(m); | |
2882 | ||
2883 | r = sd_bus_message_read(message, "b", &b); | |
2884 | if (r < 0) { | |
2885 | bus_log_parse_error(r); | |
2886 | return 0; | |
2887 | } | |
2888 | ||
2889 | if (b) | |
2890 | return 0; | |
2891 | ||
2892 | /* systemd finished reloading, let's recheck all our sessions */ | |
2893 | log_debug("System manager has been reloaded, rechecking sessions..."); | |
2894 | ||
2895 | HASHMAP_FOREACH(session, m->sessions, i) | |
2896 | session_add_to_gc_queue(session); | |
2897 | ||
2898 | return 0; | |
2899 | } | |
2900 | ||
2901 | int manager_send_changed(Manager *manager, const char *property, ...) { | |
2902 | char **l; | |
2903 | ||
2904 | assert(manager); | |
2905 | ||
2906 | l = strv_from_stdarg_alloca(property); | |
2907 | ||
2908 | return sd_bus_emit_properties_changed_strv( | |
2909 | manager->bus, | |
2910 | "/org/freedesktop/login1", | |
2911 | "org.freedesktop.login1.Manager", | |
2912 | l); | |
2913 | } | |
2914 | ||
2915 | static int strdup_job(sd_bus_message *reply, char **job) { | |
2916 | const char *j; | |
2917 | char *copy; | |
2918 | int r; | |
2919 | ||
2920 | r = sd_bus_message_read(reply, "o", &j); | |
2921 | if (r < 0) | |
2922 | return r; | |
2923 | ||
2924 | copy = strdup(j); | |
2925 | if (!copy) | |
2926 | return -ENOMEM; | |
2927 | ||
2928 | *job = copy; | |
2929 | return 1; | |
2930 | } | |
2931 | ||
2932 | int manager_start_scope( | |
2933 | Manager *manager, | |
2934 | const char *scope, | |
2935 | pid_t pid, | |
2936 | const char *slice, | |
2937 | const char *description, | |
2938 | char **wants, | |
2939 | char **after, | |
2940 | const char *requires_mounts_for, | |
2941 | sd_bus_message *more_properties, | |
2942 | sd_bus_error *error, | |
2943 | char **job) { | |
2944 | ||
2945 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; | |
2946 | char **i; | |
2947 | int r; | |
2948 | ||
2949 | assert(manager); | |
2950 | assert(scope); | |
2951 | assert(pid > 1); | |
2952 | assert(job); | |
2953 | ||
2954 | r = sd_bus_message_new_method_call( | |
2955 | manager->bus, | |
2956 | &m, | |
2957 | "org.freedesktop.systemd1", | |
2958 | "/org/freedesktop/systemd1", | |
2959 | "org.freedesktop.systemd1.Manager", | |
2960 | "StartTransientUnit"); | |
2961 | if (r < 0) | |
2962 | return r; | |
2963 | ||
2964 | r = sd_bus_message_append(m, "ss", strempty(scope), "fail"); | |
2965 | if (r < 0) | |
2966 | return r; | |
2967 | ||
2968 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
2969 | if (r < 0) | |
2970 | return r; | |
2971 | ||
2972 | if (!isempty(slice)) { | |
2973 | r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice); | |
2974 | if (r < 0) | |
2975 | return r; | |
2976 | } | |
2977 | ||
2978 | if (!isempty(description)) { | |
2979 | r = sd_bus_message_append(m, "(sv)", "Description", "s", description); | |
2980 | if (r < 0) | |
2981 | return r; | |
2982 | } | |
2983 | ||
2984 | STRV_FOREACH(i, wants) { | |
2985 | r = sd_bus_message_append(m, "(sv)", "Wants", "as", 1, *i); | |
2986 | if (r < 0) | |
2987 | return r; | |
2988 | } | |
2989 | ||
2990 | STRV_FOREACH(i, after) { | |
2991 | r = sd_bus_message_append(m, "(sv)", "After", "as", 1, *i); | |
2992 | if (r < 0) | |
2993 | return r; | |
2994 | } | |
2995 | ||
2996 | if (!empty_or_root(requires_mounts_for)) { | |
2997 | r = sd_bus_message_append(m, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for); | |
2998 | if (r < 0) | |
2999 | return r; | |
3000 | } | |
3001 | ||
3002 | /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore | |
3003 | * SIGTERM */ | |
3004 | r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true); | |
3005 | if (r < 0) | |
3006 | return r; | |
3007 | ||
3008 | r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid); | |
3009 | if (r < 0) | |
3010 | return r; | |
3011 | ||
3012 | /* disable TasksMax= for the session scope, rely on the slice setting for it */ | |
3013 | r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", (uint64_t)-1); | |
3014 | if (r < 0) | |
3015 | return bus_log_create_error(r); | |
3016 | ||
3017 | if (more_properties) { | |
3018 | /* If TasksMax also appears here, it will overwrite the default value set above */ | |
3019 | r = sd_bus_message_copy(m, more_properties, true); | |
3020 | if (r < 0) | |
3021 | return r; | |
3022 | } | |
3023 | ||
3024 | r = sd_bus_message_close_container(m); | |
3025 | if (r < 0) | |
3026 | return r; | |
3027 | ||
3028 | r = sd_bus_message_append(m, "a(sa(sv))", 0); | |
3029 | if (r < 0) | |
3030 | return r; | |
3031 | ||
3032 | r = sd_bus_call(manager->bus, m, 0, error, &reply); | |
3033 | if (r < 0) | |
3034 | return r; | |
3035 | ||
3036 | return strdup_job(reply, job); | |
3037 | } | |
3038 | ||
3039 | int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) { | |
3040 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
3041 | int r; | |
3042 | ||
3043 | assert(manager); | |
3044 | assert(unit); | |
3045 | assert(job); | |
3046 | ||
3047 | r = sd_bus_call_method( | |
3048 | manager->bus, | |
3049 | "org.freedesktop.systemd1", | |
3050 | "/org/freedesktop/systemd1", | |
3051 | "org.freedesktop.systemd1.Manager", | |
3052 | "StartUnit", | |
3053 | error, | |
3054 | &reply, | |
3055 | "ss", unit, "replace"); | |
3056 | if (r < 0) | |
3057 | return r; | |
3058 | ||
3059 | return strdup_job(reply, job); | |
3060 | } | |
3061 | ||
3062 | int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) { | |
3063 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
3064 | int r; | |
3065 | ||
3066 | assert(manager); | |
3067 | assert(unit); | |
3068 | assert(job); | |
3069 | ||
3070 | r = sd_bus_call_method( | |
3071 | manager->bus, | |
3072 | "org.freedesktop.systemd1", | |
3073 | "/org/freedesktop/systemd1", | |
3074 | "org.freedesktop.systemd1.Manager", | |
3075 | "StopUnit", | |
3076 | error, | |
3077 | &reply, | |
3078 | "ss", unit, "fail"); | |
3079 | if (r < 0) { | |
3080 | if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) || | |
3081 | sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) { | |
3082 | ||
3083 | *job = NULL; | |
3084 | sd_bus_error_free(error); | |
3085 | return 0; | |
3086 | } | |
3087 | ||
3088 | return r; | |
3089 | } | |
3090 | ||
3091 | return strdup_job(reply, job); | |
3092 | } | |
3093 | ||
3094 | int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *ret_error) { | |
3095 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3096 | _cleanup_free_ char *path = NULL; | |
3097 | int r; | |
3098 | ||
3099 | assert(manager); | |
3100 | assert(scope); | |
3101 | ||
3102 | path = unit_dbus_path_from_name(scope); | |
3103 | if (!path) | |
3104 | return -ENOMEM; | |
3105 | ||
3106 | r = sd_bus_call_method( | |
3107 | manager->bus, | |
3108 | "org.freedesktop.systemd1", | |
3109 | path, | |
3110 | "org.freedesktop.systemd1.Scope", | |
3111 | "Abandon", | |
3112 | &error, | |
3113 | NULL, | |
3114 | NULL); | |
3115 | if (r < 0) { | |
3116 | if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) || | |
3117 | sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED) || | |
3118 | sd_bus_error_has_name(&error, BUS_ERROR_SCOPE_NOT_RUNNING)) | |
3119 | return 0; | |
3120 | ||
3121 | sd_bus_error_move(ret_error, &error); | |
3122 | return r; | |
3123 | } | |
3124 | ||
3125 | return 1; | |
3126 | } | |
3127 | ||
3128 | int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) { | |
3129 | assert(manager); | |
3130 | assert(unit); | |
3131 | ||
3132 | return sd_bus_call_method( | |
3133 | manager->bus, | |
3134 | "org.freedesktop.systemd1", | |
3135 | "/org/freedesktop/systemd1", | |
3136 | "org.freedesktop.systemd1.Manager", | |
3137 | "KillUnit", | |
3138 | error, | |
3139 | NULL, | |
3140 | "ssi", unit, who == KILL_LEADER ? "main" : "all", signo); | |
3141 | } | |
3142 | ||
3143 | int manager_unit_is_active(Manager *manager, const char *unit, sd_bus_error *ret_error) { | |
3144 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3145 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
3146 | _cleanup_free_ char *path = NULL; | |
3147 | const char *state; | |
3148 | int r; | |
3149 | ||
3150 | assert(manager); | |
3151 | assert(unit); | |
3152 | ||
3153 | path = unit_dbus_path_from_name(unit); | |
3154 | if (!path) | |
3155 | return -ENOMEM; | |
3156 | ||
3157 | r = sd_bus_get_property( | |
3158 | manager->bus, | |
3159 | "org.freedesktop.systemd1", | |
3160 | path, | |
3161 | "org.freedesktop.systemd1.Unit", | |
3162 | "ActiveState", | |
3163 | &error, | |
3164 | &reply, | |
3165 | "s"); | |
3166 | if (r < 0) { | |
3167 | /* systemd might have droppped off momentarily, let's | |
3168 | * not make this an error */ | |
3169 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) || | |
3170 | sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED)) | |
3171 | return true; | |
3172 | ||
3173 | /* If the unit is already unloaded then it's not | |
3174 | * active */ | |
3175 | if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) || | |
3176 | sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED)) | |
3177 | return false; | |
3178 | ||
3179 | sd_bus_error_move(ret_error, &error); | |
3180 | return r; | |
3181 | } | |
3182 | ||
3183 | r = sd_bus_message_read(reply, "s", &state); | |
3184 | if (r < 0) | |
3185 | return r; | |
3186 | ||
3187 | return !STR_IN_SET(state, "inactive", "failed"); | |
3188 | } | |
3189 | ||
3190 | int manager_job_is_active(Manager *manager, const char *path, sd_bus_error *ret_error) { | |
3191 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3192 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
3193 | int r; | |
3194 | ||
3195 | assert(manager); | |
3196 | assert(path); | |
3197 | ||
3198 | r = sd_bus_get_property( | |
3199 | manager->bus, | |
3200 | "org.freedesktop.systemd1", | |
3201 | path, | |
3202 | "org.freedesktop.systemd1.Job", | |
3203 | "State", | |
3204 | &error, | |
3205 | &reply, | |
3206 | "s"); | |
3207 | if (r < 0) { | |
3208 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) || | |
3209 | sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED)) | |
3210 | return true; | |
3211 | ||
3212 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT)) | |
3213 | return false; | |
3214 | ||
3215 | sd_bus_error_move(ret_error, &error); | |
3216 | return r; | |
3217 | } | |
3218 | ||
3219 | /* We don't actually care about the state really. The fact | |
3220 | * that we could read the job state is enough for us */ | |
3221 | ||
3222 | return true; | |
3223 | } |