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