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