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