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