]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
3f49d45a | 2 | |
a185c5aa | 3 | #include <errno.h> |
ca78ad1d | 4 | #include <sys/stat.h> |
98a28fef | 5 | #include <unistd.h> |
a185c5aa | 6 | |
4f209af7 | 7 | #include "sd-device.h" |
cc377381 | 8 | #include "sd-messages.h" |
4f5dd394 | 9 | |
b5efdb8a | 10 | #include "alloc-util.h" |
430f0182 | 11 | #include "audit-util.h" |
31b221cf | 12 | #include "bootspec.h" |
96aad8d1 | 13 | #include "bus-common-errors.h" |
4f5dd394 | 14 | #include "bus-error.h" |
40af3d02 | 15 | #include "bus-get-properties.h" |
9b71e4ab | 16 | #include "bus-locator.h" |
269e4d2d | 17 | #include "bus-polkit.h" |
c8c8ee85 | 18 | #include "bus-unit-util.h" |
4f5dd394 | 19 | #include "bus-util.h" |
75100aeb | 20 | #include "cgroup-util.h" |
8437c059 | 21 | #include "device-util.h" |
a0956174 | 22 | #include "dirent-util.h" |
0bb2f0f1 | 23 | #include "efi-loader.h" |
269e4d2d | 24 | #include "efivars.h" |
e667266a | 25 | #include "env-util.h" |
4f5dd394 | 26 | #include "escape.h" |
3ffd4af2 | 27 | #include "fd-util.h" |
4f5dd394 | 28 | #include "fileio-label.h" |
ee228be1 | 29 | #include "fileio.h" |
f97b34a6 | 30 | #include "format-util.h" |
f4f15635 | 31 | #include "fs-util.h" |
6ecda0fb LP |
32 | #include "logind-dbus.h" |
33 | #include "logind-seat-dbus.h" | |
34 | #include "logind-session-dbus.h" | |
35 | #include "logind-user-dbus.h" | |
4f5dd394 | 36 | #include "logind.h" |
36dd5ffd | 37 | #include "missing_capability.h" |
4f5dd394 | 38 | #include "mkdir.h" |
31b221cf | 39 | #include "parse-util.h" |
4f5dd394 | 40 | #include "path-util.h" |
0b452006 | 41 | #include "process-util.h" |
428b296a | 42 | #include "reboot-util.h" |
4f5dd394 LP |
43 | #include "selinux-util.h" |
44 | #include "sleep-config.h" | |
45 | #include "special.h" | |
31b221cf | 46 | #include "stdio-util.h" |
4f5dd394 | 47 | #include "strv.h" |
288a74cc | 48 | #include "terminal-util.h" |
e4de7287 | 49 | #include "tmpfile-util.h" |
4f5dd394 | 50 | #include "unit-name.h" |
b1d4f8e1 | 51 | #include "user-util.h" |
e2fa5721 | 52 | #include "utmp-wtmp.h" |
428b296a | 53 | #include "virt.h" |
3f49d45a | 54 | |
3b92c086 LP |
55 | static int get_sender_session( |
56 | Manager *m, | |
57 | sd_bus_message *message, | |
58 | bool consult_display, | |
59 | sd_bus_error *error, | |
60 | Session **ret) { | |
aeb07570 | 61 | |
4afd3348 | 62 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
3b92c086 | 63 | Session *session = NULL; |
aeb07570 | 64 | const char *name; |
309a29df LP |
65 | int r; |
66 | ||
3b92c086 LP |
67 | /* Acquire the sender's session. This first checks if the sending process is inside a session itself, |
68 | * and returns that. If not and 'consult_display' is true, this returns the display session of the | |
69 | * owning user of the caller. */ | |
70 | ||
71 | r = sd_bus_query_sender_creds(message, | |
72 | SD_BUS_CREDS_SESSION|SD_BUS_CREDS_AUGMENT| | |
73 | (consult_display ? SD_BUS_CREDS_OWNER_UID : 0), &creds); | |
aeb07570 AJ |
74 | if (r < 0) |
75 | return r; | |
76 | ||
77 | r = sd_bus_creds_get_session(creds, &name); | |
3b92c086 LP |
78 | if (r < 0) { |
79 | if (r != -ENXIO) | |
80 | return r; | |
81 | ||
82 | if (consult_display) { | |
83 | uid_t uid; | |
84 | ||
85 | r = sd_bus_creds_get_owner_uid(creds, &uid); | |
86 | if (r < 0) { | |
87 | if (r != -ENXIO) | |
88 | return r; | |
89 | } else { | |
90 | User *user; | |
91 | ||
92 | user = hashmap_get(m->users, UID_TO_PTR(uid)); | |
93 | if (user) | |
94 | session = user->display; | |
95 | } | |
96 | } | |
97 | } else | |
98 | session = hashmap_get(m->sessions, name); | |
aeb07570 | 99 | |
aeb07570 | 100 | if (!session) |
3b92c086 LP |
101 | return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, |
102 | consult_display ? | |
103 | "Caller does not belong to any known session and doesn't own any suitable session." : | |
104 | "Caller does not belong to any known session."); | |
aeb07570 AJ |
105 | |
106 | *ret = session; | |
107 | return 0; | |
aeb07570 AJ |
108 | } |
109 | ||
3b92c086 LP |
110 | int manager_get_session_from_creds( |
111 | Manager *m, | |
112 | sd_bus_message *message, | |
113 | const char *name, | |
114 | sd_bus_error *error, | |
115 | Session **ret) { | |
116 | ||
aeb07570 AJ |
117 | Session *session; |
118 | ||
309a29df | 119 | assert(m); |
309a29df LP |
120 | assert(ret); |
121 | ||
3b92c086 LP |
122 | if (SEAT_IS_SELF(name)) /* the caller's own session */ |
123 | return get_sender_session(m, message, false, error, ret); | |
124 | if (SEAT_IS_AUTO(name)) /* The caller's own session if they have one, otherwise their user's display session */ | |
125 | return get_sender_session(m, message, true, error, ret); | |
309a29df LP |
126 | |
127 | session = hashmap_get(m->sessions, name); | |
128 | if (!session) | |
129 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name); | |
130 | ||
131 | *ret = session; | |
132 | return 0; | |
133 | } | |
134 | ||
aeb07570 | 135 | static int get_sender_user(Manager *m, sd_bus_message *message, sd_bus_error *error, User **ret) { |
aeb07570 AJ |
136 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
137 | uid_t uid; | |
309a29df LP |
138 | User *user; |
139 | int r; | |
140 | ||
aeb07570 AJ |
141 | /* Note that we get the owner UID of the session, not the actual client UID here! */ |
142 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds); | |
143 | if (r < 0) | |
144 | return r; | |
145 | ||
146 | r = sd_bus_creds_get_owner_uid(creds, &uid); | |
3b92c086 LP |
147 | if (r < 0) { |
148 | if (r != -ENXIO) | |
149 | return r; | |
150 | ||
151 | user = NULL; | |
152 | } else | |
153 | user = hashmap_get(m->users, UID_TO_PTR(uid)); | |
aeb07570 | 154 | |
aeb07570 | 155 | if (!user) |
3b92c086 LP |
156 | return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, |
157 | "Caller does not belong to any logged in or lingering user"); | |
aeb07570 AJ |
158 | |
159 | *ret = user; | |
160 | return 0; | |
aeb07570 AJ |
161 | } |
162 | ||
163 | int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret) { | |
164 | User *user; | |
165 | ||
309a29df | 166 | assert(m); |
309a29df LP |
167 | assert(ret); |
168 | ||
aeb07570 AJ |
169 | if (!uid_is_valid(uid)) |
170 | return get_sender_user(m, message, error, ret); | |
309a29df | 171 | |
8cb4ab00 | 172 | user = hashmap_get(m->users, UID_TO_PTR(uid)); |
309a29df | 173 | if (!user) |
124d7cb2 ZJS |
174 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, |
175 | "User ID "UID_FMT" is not logged in or lingering", uid); | |
309a29df LP |
176 | |
177 | *ret = user; | |
178 | return 0; | |
179 | } | |
180 | ||
3b92c086 LP |
181 | int manager_get_seat_from_creds( |
182 | Manager *m, | |
183 | sd_bus_message *message, | |
184 | const char *name, | |
185 | sd_bus_error *error, | |
186 | Seat **ret) { | |
187 | ||
309a29df LP |
188 | Seat *seat; |
189 | int r; | |
190 | ||
191 | assert(m); | |
309a29df LP |
192 | assert(ret); |
193 | ||
3b92c086 | 194 | if (SEAT_IS_SELF(name) || SEAT_IS_AUTO(name)) { |
309a29df LP |
195 | Session *session; |
196 | ||
3b92c086 LP |
197 | /* Use these special seat names as session names */ |
198 | r = manager_get_session_from_creds(m, message, name, error, &session); | |
309a29df LP |
199 | if (r < 0) |
200 | return r; | |
201 | ||
202 | seat = session->seat; | |
309a29df | 203 | if (!seat) |
3b92c086 | 204 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "Session '%s' has no seat.", session->id); |
309a29df LP |
205 | } else { |
206 | seat = hashmap_get(m->seats, name); | |
207 | if (!seat) | |
208 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name); | |
209 | } | |
210 | ||
211 | *ret = seat; | |
212 | return 0; | |
213 | } | |
214 | ||
ce7f1070 VJ |
215 | static int return_test_polkit( |
216 | sd_bus_message *message, | |
217 | int capability, | |
218 | const char *action, | |
219 | const char **details, | |
220 | uid_t good_user, | |
221 | sd_bus_error *e) { | |
222 | ||
223 | const char *result; | |
224 | bool challenge; | |
225 | int r; | |
226 | ||
227 | r = bus_test_polkit(message, capability, action, details, good_user, &challenge, e); | |
228 | if (r < 0) | |
229 | return r; | |
230 | ||
231 | if (r > 0) | |
232 | result = "yes"; | |
233 | else if (challenge) | |
234 | result = "challenge"; | |
235 | else | |
236 | result = "no"; | |
237 | ||
238 | return sd_bus_reply_method_return(message, "s", result); | |
239 | } | |
240 | ||
cc377381 LP |
241 | static int property_get_idle_hint( |
242 | sd_bus *bus, | |
243 | const char *path, | |
244 | const char *interface, | |
245 | const char *property, | |
246 | sd_bus_message *reply, | |
ebcf1f97 LP |
247 | void *userdata, |
248 | sd_bus_error *error) { | |
a185c5aa | 249 | |
cc377381 | 250 | Manager *m = userdata; |
a185c5aa | 251 | |
cc377381 LP |
252 | assert(bus); |
253 | assert(reply); | |
254 | assert(m); | |
255 | ||
256 | return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0); | |
a185c5aa LP |
257 | } |
258 | ||
cc377381 LP |
259 | static int property_get_idle_since_hint( |
260 | sd_bus *bus, | |
261 | const char *path, | |
262 | const char *interface, | |
263 | const char *property, | |
264 | sd_bus_message *reply, | |
ebcf1f97 LP |
265 | void *userdata, |
266 | sd_bus_error *error) { | |
cc377381 LP |
267 | |
268 | Manager *m = userdata; | |
5cb14b37 | 269 | dual_timestamp t = DUAL_TIMESTAMP_NULL; |
a185c5aa | 270 | |
cc377381 LP |
271 | assert(bus); |
272 | assert(reply); | |
a185c5aa LP |
273 | assert(m); |
274 | ||
275 | manager_get_idle_hint(m, &t); | |
a185c5aa | 276 | |
cc377381 | 277 | return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic); |
a185c5aa LP |
278 | } |
279 | ||
cc377381 LP |
280 | static int property_get_inhibited( |
281 | sd_bus *bus, | |
282 | const char *path, | |
283 | const char *interface, | |
284 | const char *property, | |
285 | sd_bus_message *reply, | |
ebcf1f97 LP |
286 | void *userdata, |
287 | sd_bus_error *error) { | |
cc377381 LP |
288 | |
289 | Manager *m = userdata; | |
f8e2fb7b | 290 | InhibitWhat w; |
f8e2fb7b | 291 | |
cc377381 LP |
292 | assert(bus); |
293 | assert(reply); | |
294 | assert(m); | |
f8e2fb7b | 295 | |
cc377381 | 296 | w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY); |
f8e2fb7b | 297 | |
cc377381 | 298 | return sd_bus_message_append(reply, "s", inhibit_what_to_string(w)); |
f8e2fb7b LP |
299 | } |
300 | ||
cc377381 LP |
301 | static int property_get_preparing( |
302 | sd_bus *bus, | |
303 | const char *path, | |
304 | const char *interface, | |
305 | const char *property, | |
306 | sd_bus_message *reply, | |
ebcf1f97 LP |
307 | void *userdata, |
308 | sd_bus_error *error) { | |
cc377381 LP |
309 | |
310 | Manager *m = userdata; | |
311 | bool b; | |
5e4a79da | 312 | |
cc377381 LP |
313 | assert(bus); |
314 | assert(reply); | |
315 | assert(m); | |
5e4a79da LP |
316 | |
317 | if (streq(property, "PreparingForShutdown")) | |
5d904a6a | 318 | b = m->action_what & INHIBIT_SHUTDOWN; |
5e4a79da | 319 | else |
5d904a6a | 320 | b = m->action_what & INHIBIT_SLEEP; |
5e4a79da | 321 | |
cc377381 | 322 | return sd_bus_message_append(reply, "b", b); |
5e4a79da LP |
323 | } |
324 | ||
8aaa023a DM |
325 | static int property_get_scheduled_shutdown( |
326 | sd_bus *bus, | |
327 | const char *path, | |
328 | const char *interface, | |
329 | const char *property, | |
330 | sd_bus_message *reply, | |
331 | void *userdata, | |
332 | sd_bus_error *error) { | |
333 | ||
334 | Manager *m = userdata; | |
335 | int r; | |
336 | ||
337 | assert(bus); | |
338 | assert(reply); | |
339 | assert(m); | |
340 | ||
341 | r = sd_bus_message_open_container(reply, 'r', "st"); | |
342 | if (r < 0) | |
343 | return r; | |
344 | ||
345 | r = sd_bus_message_append(reply, "st", m->scheduled_shutdown_type, m->scheduled_shutdown_timeout); | |
346 | if (r < 0) | |
347 | return r; | |
348 | ||
349 | return sd_bus_message_close_container(reply); | |
350 | } | |
351 | ||
cc377381 | 352 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction); |
01adcd69 | 353 | static BUS_DEFINE_PROPERTY_GET(property_get_docked, "b", Manager, manager_is_docked_or_external_displays); |
9b9c23da | 354 | static BUS_DEFINE_PROPERTY_GET(property_get_lid_closed, "b", Manager, manager_is_lid_closed); |
4e96eb68 | 355 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_on_external_power, "b", manager_is_on_external_power); |
01adcd69 YW |
356 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_compat_user_tasks_max, "t", CGROUP_LIMIT_MAX); |
357 | static BUS_DEFINE_PROPERTY_GET_REF(property_get_hashmap_size, "t", Hashmap *, (uint64_t) hashmap_size); | |
28414939 | 358 | |
19070062 | 359 | static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
360 | _cleanup_free_ char *p = NULL; |
361 | Manager *m = userdata; | |
362 | const char *name; | |
363 | Session *session; | |
364 | int r; | |
365 | ||
cc377381 LP |
366 | assert(message); |
367 | assert(m); | |
368 | ||
369 | r = sd_bus_message_read(message, "s", &name); | |
370 | if (r < 0) | |
ebcf1f97 | 371 | return r; |
cc377381 | 372 | |
309a29df LP |
373 | r = manager_get_session_from_creds(m, message, name, error, &session); |
374 | if (r < 0) | |
375 | return r; | |
cc377381 LP |
376 | |
377 | p = session_bus_path(session); | |
378 | if (!p) | |
ebcf1f97 | 379 | return -ENOMEM; |
cc377381 | 380 | |
df2d202e | 381 | return sd_bus_reply_method_return(message, "o", p); |
cc377381 LP |
382 | } |
383 | ||
7b33c622 AJ |
384 | /* Get login session of a process. This is not what you are looking for these days, |
385 | * as apps may instead belong to a user service unit. This includes terminal | |
386 | * emulators and hence command-line apps. */ | |
19070062 | 387 | static int method_get_session_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 388 | _cleanup_free_ char *p = NULL; |
954449b8 | 389 | Session *session = NULL; |
cc377381 | 390 | Manager *m = userdata; |
4e724d9c | 391 | pid_t pid; |
cc377381 LP |
392 | int r; |
393 | ||
cc377381 LP |
394 | assert(message); |
395 | assert(m); | |
396 | ||
4e724d9c LP |
397 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); |
398 | ||
cc377381 LP |
399 | r = sd_bus_message_read(message, "u", &pid); |
400 | if (r < 0) | |
ebcf1f97 | 401 | return r; |
07b38ba5 | 402 | if (pid < 0) |
06820eaf | 403 | return -EINVAL; |
cc377381 | 404 | |
06820eaf | 405 | if (pid == 0) { |
309a29df | 406 | r = manager_get_session_from_creds(m, message, NULL, error, &session); |
5b12334d LP |
407 | if (r < 0) |
408 | return r; | |
309a29df LP |
409 | } else { |
410 | r = manager_get_session_by_pid(m, pid, &session); | |
4e724d9c | 411 | if (r < 0) |
ebcf1f97 | 412 | return r; |
4e724d9c | 413 | |
309a29df | 414 | if (!session) |
124d7cb2 ZJS |
415 | return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, |
416 | "PID "PID_FMT" does not belong to any known session", pid); | |
309a29df | 417 | } |
cc377381 LP |
418 | |
419 | p = session_bus_path(session); | |
420 | if (!p) | |
ebcf1f97 | 421 | return -ENOMEM; |
cc377381 | 422 | |
df2d202e | 423 | return sd_bus_reply_method_return(message, "o", p); |
cc377381 LP |
424 | } |
425 | ||
19070062 | 426 | static int method_get_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
427 | _cleanup_free_ char *p = NULL; |
428 | Manager *m = userdata; | |
429 | uint32_t uid; | |
430 | User *user; | |
431 | int r; | |
432 | ||
cc377381 LP |
433 | assert(message); |
434 | assert(m); | |
435 | ||
436 | r = sd_bus_message_read(message, "u", &uid); | |
437 | if (r < 0) | |
ebcf1f97 | 438 | return r; |
cc377381 | 439 | |
309a29df LP |
440 | r = manager_get_user_from_creds(m, message, uid, error, &user); |
441 | if (r < 0) | |
442 | return r; | |
cc377381 LP |
443 | |
444 | p = user_bus_path(user); | |
445 | if (!p) | |
ebcf1f97 | 446 | return -ENOMEM; |
cc377381 | 447 | |
df2d202e | 448 | return sd_bus_reply_method_return(message, "o", p); |
cc377381 LP |
449 | } |
450 | ||
19070062 | 451 | static int method_get_user_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
452 | _cleanup_free_ char *p = NULL; |
453 | Manager *m = userdata; | |
954449b8 | 454 | User *user = NULL; |
4e724d9c | 455 | pid_t pid; |
fb6becb4 | 456 | int r; |
98a28fef | 457 | |
cc377381 | 458 | assert(message); |
98a28fef | 459 | assert(m); |
cc377381 | 460 | |
4e724d9c LP |
461 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); |
462 | ||
cc377381 LP |
463 | r = sd_bus_message_read(message, "u", &pid); |
464 | if (r < 0) | |
ebcf1f97 | 465 | return r; |
07b38ba5 | 466 | if (pid < 0) |
06820eaf | 467 | return -EINVAL; |
cc377381 | 468 | |
06820eaf | 469 | if (pid == 0) { |
309a29df | 470 | r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user); |
5b12334d LP |
471 | if (r < 0) |
472 | return r; | |
309a29df LP |
473 | } else { |
474 | r = manager_get_user_by_pid(m, pid, &user); | |
4e724d9c | 475 | if (r < 0) |
ebcf1f97 | 476 | return r; |
309a29df | 477 | if (!user) |
095b8833 AJ |
478 | return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, |
479 | "PID "PID_FMT" does not belong to any logged in user or lingering user", | |
480 | pid); | |
4e724d9c LP |
481 | } |
482 | ||
cc377381 LP |
483 | p = user_bus_path(user); |
484 | if (!p) | |
ebcf1f97 | 485 | return -ENOMEM; |
cc377381 | 486 | |
df2d202e | 487 | return sd_bus_reply_method_return(message, "o", p); |
cc377381 LP |
488 | } |
489 | ||
19070062 | 490 | static int method_get_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
491 | _cleanup_free_ char *p = NULL; |
492 | Manager *m = userdata; | |
493 | const char *name; | |
494 | Seat *seat; | |
495 | int r; | |
496 | ||
98a28fef | 497 | assert(message); |
cc377381 | 498 | assert(m); |
98a28fef | 499 | |
cc377381 LP |
500 | r = sd_bus_message_read(message, "s", &name); |
501 | if (r < 0) | |
ebcf1f97 | 502 | return r; |
98a28fef | 503 | |
309a29df LP |
504 | r = manager_get_seat_from_creds(m, message, name, error, &seat); |
505 | if (r < 0) | |
506 | return r; | |
98a28fef | 507 | |
cc377381 LP |
508 | p = seat_bus_path(seat); |
509 | if (!p) | |
ebcf1f97 | 510 | return -ENOMEM; |
98a28fef | 511 | |
df2d202e | 512 | return sd_bus_reply_method_return(message, "o", p); |
cc377381 | 513 | } |
98a28fef | 514 | |
19070062 | 515 | static int method_list_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 516 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
cc377381 LP |
517 | Manager *m = userdata; |
518 | Session *session; | |
519 | Iterator i; | |
520 | int r; | |
521 | ||
cc377381 LP |
522 | assert(message); |
523 | assert(m); | |
98a28fef | 524 | |
df2d202e | 525 | r = sd_bus_message_new_method_return(message, &reply); |
cc377381 | 526 | if (r < 0) |
ebcf1f97 | 527 | return r; |
98a28fef | 528 | |
cc377381 LP |
529 | r = sd_bus_message_open_container(reply, 'a', "(susso)"); |
530 | if (r < 0) | |
ebcf1f97 | 531 | return r; |
cc377381 LP |
532 | |
533 | HASHMAP_FOREACH(session, m->sessions, i) { | |
534 | _cleanup_free_ char *p = NULL; | |
535 | ||
536 | p = session_bus_path(session); | |
537 | if (!p) | |
ebcf1f97 | 538 | return -ENOMEM; |
cc377381 LP |
539 | |
540 | r = sd_bus_message_append(reply, "(susso)", | |
541 | session->id, | |
22c902fa LP |
542 | (uint32_t) session->user->user_record->uid, |
543 | session->user->user_record->user_name, | |
cc377381 LP |
544 | session->seat ? session->seat->id : "", |
545 | p); | |
546 | if (r < 0) | |
ebcf1f97 | 547 | return r; |
cc377381 LP |
548 | } |
549 | ||
550 | r = sd_bus_message_close_container(reply); | |
551 | if (r < 0) | |
ebcf1f97 | 552 | return r; |
cc377381 | 553 | |
9030ca46 | 554 | return sd_bus_send(NULL, reply, NULL); |
cc377381 LP |
555 | } |
556 | ||
19070062 | 557 | static int method_list_users(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 558 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
cc377381 LP |
559 | Manager *m = userdata; |
560 | User *user; | |
561 | Iterator i; | |
562 | int r; | |
563 | ||
cc377381 LP |
564 | assert(message); |
565 | assert(m); | |
566 | ||
df2d202e | 567 | r = sd_bus_message_new_method_return(message, &reply); |
cc377381 | 568 | if (r < 0) |
ebcf1f97 | 569 | return r; |
cc377381 LP |
570 | |
571 | r = sd_bus_message_open_container(reply, 'a', "(uso)"); | |
572 | if (r < 0) | |
ebcf1f97 | 573 | return r; |
cc377381 LP |
574 | |
575 | HASHMAP_FOREACH(user, m->users, i) { | |
576 | _cleanup_free_ char *p = NULL; | |
577 | ||
578 | p = user_bus_path(user); | |
579 | if (!p) | |
ebcf1f97 | 580 | return -ENOMEM; |
cc377381 LP |
581 | |
582 | r = sd_bus_message_append(reply, "(uso)", | |
22c902fa LP |
583 | (uint32_t) user->user_record->uid, |
584 | user->user_record->user_name, | |
cc377381 LP |
585 | p); |
586 | if (r < 0) | |
ebcf1f97 | 587 | return r; |
cc377381 LP |
588 | } |
589 | ||
590 | r = sd_bus_message_close_container(reply); | |
591 | if (r < 0) | |
ebcf1f97 | 592 | return r; |
cc377381 | 593 | |
9030ca46 | 594 | return sd_bus_send(NULL, reply, NULL); |
cc377381 LP |
595 | } |
596 | ||
19070062 | 597 | static int method_list_seats(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 598 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
cc377381 LP |
599 | Manager *m = userdata; |
600 | Seat *seat; | |
601 | Iterator i; | |
602 | int r; | |
603 | ||
cc377381 LP |
604 | assert(message); |
605 | assert(m); | |
606 | ||
df2d202e | 607 | r = sd_bus_message_new_method_return(message, &reply); |
cc377381 | 608 | if (r < 0) |
ebcf1f97 | 609 | return r; |
cc377381 LP |
610 | |
611 | r = sd_bus_message_open_container(reply, 'a', "(so)"); | |
612 | if (r < 0) | |
ebcf1f97 | 613 | return r; |
cc377381 LP |
614 | |
615 | HASHMAP_FOREACH(seat, m->seats, i) { | |
616 | _cleanup_free_ char *p = NULL; | |
617 | ||
618 | p = seat_bus_path(seat); | |
619 | if (!p) | |
ebcf1f97 | 620 | return -ENOMEM; |
cc377381 | 621 | |
b8358bce | 622 | r = sd_bus_message_append(reply, "(so)", seat->id, p); |
cc377381 | 623 | if (r < 0) |
ebcf1f97 | 624 | return r; |
cc377381 LP |
625 | } |
626 | ||
627 | r = sd_bus_message_close_container(reply); | |
628 | if (r < 0) | |
ebcf1f97 | 629 | return r; |
cc377381 | 630 | |
9030ca46 | 631 | return sd_bus_send(NULL, reply, NULL); |
cc377381 LP |
632 | } |
633 | ||
19070062 | 634 | static int method_list_inhibitors(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 635 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
cc377381 LP |
636 | Manager *m = userdata; |
637 | Inhibitor *inhibitor; | |
638 | Iterator i; | |
639 | int r; | |
640 | ||
19070062 LP |
641 | assert(message); |
642 | assert(m); | |
643 | ||
df2d202e | 644 | r = sd_bus_message_new_method_return(message, &reply); |
cc377381 | 645 | if (r < 0) |
ebcf1f97 | 646 | return r; |
cc377381 LP |
647 | |
648 | r = sd_bus_message_open_container(reply, 'a', "(ssssuu)"); | |
649 | if (r < 0) | |
ebcf1f97 | 650 | return r; |
cc377381 LP |
651 | |
652 | HASHMAP_FOREACH(inhibitor, m->inhibitors, i) { | |
653 | ||
dbfa3fbb | 654 | r = sd_bus_message_append(reply, "(ssssuu)", |
cc377381 LP |
655 | strempty(inhibit_what_to_string(inhibitor->what)), |
656 | strempty(inhibitor->who), | |
657 | strempty(inhibitor->why), | |
658 | strempty(inhibit_mode_to_string(inhibitor->mode)), | |
659 | (uint32_t) inhibitor->uid, | |
660 | (uint32_t) inhibitor->pid); | |
661 | if (r < 0) | |
ebcf1f97 | 662 | return r; |
cc377381 LP |
663 | } |
664 | ||
665 | r = sd_bus_message_close_container(reply); | |
666 | if (r < 0) | |
ebcf1f97 | 667 | return r; |
cc377381 | 668 | |
9030ca46 | 669 | return sd_bus_send(NULL, reply, NULL); |
cc377381 LP |
670 | } |
671 | ||
19070062 | 672 | static int method_create_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
a4cd87e9 | 673 | const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop; |
c7543606 | 674 | _cleanup_free_ char *id = NULL; |
cc377381 | 675 | Session *session = NULL; |
bc2b6332 | 676 | uint32_t audit_id = 0; |
cc377381 LP |
677 | Manager *m = userdata; |
678 | User *user = NULL; | |
679 | Seat *seat = NULL; | |
06820eaf LP |
680 | pid_t leader; |
681 | uid_t uid; | |
cc377381 LP |
682 | int remote; |
683 | uint32_t vtnr = 0; | |
684 | SessionType t; | |
685 | SessionClass c; | |
686 | int r; | |
687 | ||
cc377381 LP |
688 | assert(message); |
689 | assert(m); | |
690 | ||
06820eaf LP |
691 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); |
692 | assert_cc(sizeof(uid_t) == sizeof(uint32_t)); | |
693 | ||
124d7cb2 ZJS |
694 | r = sd_bus_message_read(message, "uusssssussbss", |
695 | &uid, &leader, &service, &type, &class, &desktop, &cseat, | |
696 | &vtnr, &tty, &display, &remote, &remote_user, &remote_host); | |
cc377381 | 697 | if (r < 0) |
ebcf1f97 | 698 | return r; |
cc377381 | 699 | |
06820eaf LP |
700 | if (!uid_is_valid(uid)) |
701 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid UID"); | |
bc2b6332 | 702 | if (leader < 0 || leader == 1 || leader == getpid_cached()) |
ebcf1f97 | 703 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID"); |
98a28fef | 704 | |
e2acb67b LP |
705 | if (isempty(type)) |
706 | t = _SESSION_TYPE_INVALID; | |
707 | else { | |
708 | t = session_type_from_string(type); | |
709 | if (t < 0) | |
124d7cb2 ZJS |
710 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
711 | "Invalid session type %s", type); | |
e2acb67b | 712 | } |
98a28fef | 713 | |
55efac6c | 714 | if (isempty(class)) |
e2acb67b LP |
715 | c = _SESSION_CLASS_INVALID; |
716 | else { | |
55efac6c | 717 | c = session_class_from_string(class); |
e2acb67b | 718 | if (c < 0) |
124d7cb2 ZJS |
719 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
720 | "Invalid session class %s", class); | |
e2acb67b | 721 | } |
55efac6c | 722 | |
a4cd87e9 LP |
723 | if (isempty(desktop)) |
724 | desktop = NULL; | |
725 | else { | |
726 | if (!string_is_safe(desktop)) | |
124d7cb2 ZJS |
727 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
728 | "Invalid desktop string %s", desktop); | |
a4cd87e9 LP |
729 | } |
730 | ||
954449b8 LP |
731 | if (isempty(cseat)) |
732 | seat = NULL; | |
98a28fef | 733 | else { |
954449b8 LP |
734 | seat = hashmap_get(m->seats, cseat); |
735 | if (!seat) | |
124d7cb2 ZJS |
736 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, |
737 | "No seat '%s' known", cseat); | |
98a28fef LP |
738 | } |
739 | ||
98a28fef | 740 | if (tty_is_vc(tty)) { |
4d6d6518 | 741 | int v; |
98a28fef | 742 | |
954449b8 | 743 | if (!seat) |
92432fcc DH |
744 | seat = m->seat0; |
745 | else if (seat != m->seat0) | |
124d7cb2 ZJS |
746 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
747 | "TTY %s is virtual console but seat %s is not seat0", tty, seat->id); | |
98a28fef | 748 | |
4d6d6518 | 749 | v = vtnr_from_tty(tty); |
4d6d6518 | 750 | if (v <= 0) |
124d7cb2 ZJS |
751 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
752 | "Cannot determine VT number from virtual console TTY %s", tty); | |
98a28fef | 753 | |
bc2b6332 | 754 | if (vtnr == 0) |
4d6d6518 LP |
755 | vtnr = (uint32_t) v; |
756 | else if (vtnr != (uint32_t) v) | |
124d7cb2 ZJS |
757 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
758 | "Specified TTY and VT number do not match"); | |
cc377381 | 759 | |
d1122ad5 LP |
760 | } else if (tty_is_console(tty)) { |
761 | ||
954449b8 | 762 | if (!seat) |
92432fcc DH |
763 | seat = m->seat0; |
764 | else if (seat != m->seat0) | |
124d7cb2 ZJS |
765 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
766 | "Console TTY specified but seat is not seat0"); | |
d1122ad5 LP |
767 | |
768 | if (vtnr != 0) | |
124d7cb2 ZJS |
769 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
770 | "Console TTY specified but VT number is not 0"); | |
978cf3c7 | 771 | } |
98a28fef | 772 | |
954449b8 | 773 | if (seat) { |
bf7825ae | 774 | if (seat_has_vts(seat)) { |
bc2b6332 | 775 | if (vtnr <= 0 || vtnr > 63) |
124d7cb2 ZJS |
776 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
777 | "VT number out of range"); | |
4d6d6518 | 778 | } else { |
d1122ad5 | 779 | if (vtnr != 0) |
124d7cb2 ZJS |
780 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
781 | "Seat has no VTs but VT number not 0"); | |
4d6d6518 LP |
782 | } |
783 | } | |
784 | ||
e2acb67b LP |
785 | if (t == _SESSION_TYPE_INVALID) { |
786 | if (!isempty(display)) | |
787 | t = SESSION_X11; | |
788 | else if (!isempty(tty)) | |
789 | t = SESSION_TTY; | |
790 | else | |
791 | t = SESSION_UNSPECIFIED; | |
792 | } | |
793 | ||
794 | if (c == _SESSION_CLASS_INVALID) { | |
a4cd87e9 | 795 | if (t == SESSION_UNSPECIFIED) |
e2acb67b | 796 | c = SESSION_BACKGROUND; |
a4cd87e9 LP |
797 | else |
798 | c = SESSION_USER; | |
e2acb67b LP |
799 | } |
800 | ||
06820eaf | 801 | if (leader == 0) { |
4afd3348 | 802 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
5b12334d LP |
803 | |
804 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); | |
805 | if (r < 0) | |
806 | return r; | |
807 | ||
5b12334d | 808 | r = sd_bus_creds_get_pid(creds, (pid_t*) &leader); |
cc377381 | 809 | if (r < 0) |
ebcf1f97 | 810 | return r; |
9444b1f2 LP |
811 | } |
812 | ||
124d7cb2 ZJS |
813 | /* Check if we are already in a logind session. Or if we are in user@.service |
814 | * which is a special PAM session that avoids creating a logind session. */ | |
bc2b6332 | 815 | r = manager_get_user_by_pid(m, leader, NULL); |
e8a3144e AJ |
816 | if (r < 0) |
817 | return r; | |
bc2b6332 | 818 | if (r > 0) |
124d7cb2 ZJS |
819 | return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, |
820 | "Already running in a session or user slice"); | |
b80120c4 DH |
821 | |
822 | /* | |
823 | * Old gdm and lightdm start the user-session on the same VT as | |
824 | * the greeter session. But they destroy the greeter session | |
825 | * after the user-session and want the user-session to take | |
826 | * over the VT. We need to support this for | |
827 | * backwards-compatibility, so make sure we allow new sessions | |
cc85d562 DH |
828 | * on a VT that a greeter is running on. Furthermore, to allow |
829 | * re-logins, we have to allow a greeter to take over a used VT for | |
830 | * the exact same reasons. | |
b80120c4 | 831 | */ |
cc85d562 DH |
832 | if (c != SESSION_GREETER && |
833 | vtnr > 0 && | |
b80120c4 DH |
834 | vtnr < m->seat0->position_count && |
835 | m->seat0->positions[vtnr] && | |
836 | m->seat0->positions[vtnr]->class != SESSION_GREETER) | |
837 | return sd_bus_error_setf(error, BUS_ERROR_SESSION_BUSY, "Already occupied by a session"); | |
21c390cc | 838 | |
183e0738 | 839 | if (hashmap_size(m->sessions) >= m->sessions_max) |
124d7cb2 ZJS |
840 | return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, |
841 | "Maximum number of sessions (%" PRIu64 ") reached, refusing further sessions.", | |
842 | m->sessions_max); | |
183e0738 | 843 | |
3a87a86e LP |
844 | (void) audit_session_from_pid(leader, &audit_id); |
845 | if (audit_session_is_valid(audit_id)) { | |
954449b8 | 846 | /* Keep our session IDs and the audit session IDs in sync */ |
21c390cc | 847 | |
de0671ee | 848 | if (asprintf(&id, "%"PRIu32, audit_id) < 0) |
ebcf1f97 | 849 | return -ENOMEM; |
21c390cc | 850 | |
b0bd159f LP |
851 | /* Wut? There's already a session by this name and we didn't find it above? Weird, then let's |
852 | * not trust the audit data and let's better register a new ID */ | |
853 | if (hashmap_contains(m->sessions, id)) { | |
bc2b6332 | 854 | log_warning("Existing logind session ID %s used by new audit session, ignoring.", id); |
3a87a86e | 855 | audit_id = AUDIT_SESSION_INVALID; |
97b11eed | 856 | id = mfree(id); |
07714753 | 857 | } |
954449b8 | 858 | } |
07714753 | 859 | |
954449b8 | 860 | if (!id) { |
07714753 | 861 | do { |
97b11eed | 862 | id = mfree(id); |
07714753 | 863 | |
cc377381 | 864 | if (asprintf(&id, "c%lu", ++m->session_counter) < 0) |
ebcf1f97 | 865 | return -ENOMEM; |
07714753 | 866 | |
b0bd159f | 867 | } while (hashmap_contains(m->sessions, id)); |
98a28fef LP |
868 | } |
869 | ||
3b92c086 LP |
870 | /* The generated names should not clash with 'auto' or 'self' */ |
871 | assert(!SESSION_IS_SELF(id)); | |
872 | assert(!SESSION_IS_AUTO(id)); | |
873 | ||
5238e957 | 874 | /* If we are not watching utmp already, try again */ |
3d0ef5c7 LP |
875 | manager_reconnect_utmp(m); |
876 | ||
954449b8 | 877 | r = manager_add_user_by_uid(m, uid, &user); |
ebcf1f97 | 878 | if (r < 0) |
954449b8 LP |
879 | goto fail; |
880 | ||
9444b1f2 | 881 | r = manager_add_session(m, id, &session); |
ebcf1f97 | 882 | if (r < 0) |
98a28fef LP |
883 | goto fail; |
884 | ||
9444b1f2 | 885 | session_set_user(session, user); |
fe3ab845 YW |
886 | r = session_set_leader(session, leader); |
887 | if (r < 0) | |
888 | goto fail; | |
9444b1f2 | 889 | |
db72aea4 | 890 | session->original_type = session->type = t; |
55efac6c | 891 | session->class = c; |
98a28fef | 892 | session->remote = remote; |
98a28fef LP |
893 | session->vtnr = vtnr; |
894 | ||
98a28fef LP |
895 | if (!isempty(tty)) { |
896 | session->tty = strdup(tty); | |
897 | if (!session->tty) { | |
ebcf1f97 | 898 | r = -ENOMEM; |
98a28fef LP |
899 | goto fail; |
900 | } | |
3d0ef5c7 LP |
901 | |
902 | session->tty_validity = TTY_FROM_PAM; | |
98a28fef LP |
903 | } |
904 | ||
905 | if (!isempty(display)) { | |
906 | session->display = strdup(display); | |
907 | if (!session->display) { | |
ebcf1f97 | 908 | r = -ENOMEM; |
98a28fef LP |
909 | goto fail; |
910 | } | |
911 | } | |
912 | ||
913 | if (!isempty(remote_user)) { | |
914 | session->remote_user = strdup(remote_user); | |
915 | if (!session->remote_user) { | |
ebcf1f97 | 916 | r = -ENOMEM; |
98a28fef LP |
917 | goto fail; |
918 | } | |
919 | } | |
920 | ||
921 | if (!isempty(remote_host)) { | |
922 | session->remote_host = strdup(remote_host); | |
923 | if (!session->remote_host) { | |
ebcf1f97 | 924 | r = -ENOMEM; |
98a28fef LP |
925 | goto fail; |
926 | } | |
927 | } | |
928 | ||
929 | if (!isempty(service)) { | |
930 | session->service = strdup(service); | |
931 | if (!session->service) { | |
ebcf1f97 | 932 | r = -ENOMEM; |
98a28fef LP |
933 | goto fail; |
934 | } | |
935 | } | |
936 | ||
a4cd87e9 LP |
937 | if (!isempty(desktop)) { |
938 | session->desktop = strdup(desktop); | |
939 | if (!session->desktop) { | |
940 | r = -ENOMEM; | |
941 | goto fail; | |
942 | } | |
943 | } | |
944 | ||
954449b8 LP |
945 | if (seat) { |
946 | r = seat_attach_session(seat, session); | |
ebcf1f97 | 947 | if (r < 0) |
98a28fef LP |
948 | goto fail; |
949 | } | |
950 | ||
22f93314 JS |
951 | r = sd_bus_message_enter_container(message, 'a', "(sv)"); |
952 | if (r < 0) | |
d88ffeee | 953 | goto fail; |
22f93314 | 954 | |
25a1ab4e | 955 | r = session_start(session, message, error); |
22f93314 JS |
956 | if (r < 0) |
957 | goto fail; | |
958 | ||
959 | r = sd_bus_message_exit_container(message); | |
ebcf1f97 | 960 | if (r < 0) |
98a28fef LP |
961 | goto fail; |
962 | ||
cc377381 | 963 | session->create_message = sd_bus_message_ref(message); |
98a28fef | 964 | |
bc2b6332 | 965 | /* Now, let's wait until the slice unit and stuff got created. We send the reply back from |
f7340ab2 | 966 | * session_send_create_reply(). */ |
cba38758 | 967 | |
cc377381 | 968 | return 1; |
98a28fef LP |
969 | |
970 | fail: | |
98a28fef LP |
971 | if (session) |
972 | session_add_to_gc_queue(session); | |
973 | ||
974 | if (user) | |
975 | user_add_to_gc_queue(user); | |
976 | ||
98a28fef LP |
977 | return r; |
978 | } | |
979 | ||
19070062 | 980 | static int method_release_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
981 | Manager *m = userdata; |
982 | Session *session; | |
983 | const char *name; | |
984 | int r; | |
314b4b0a | 985 | |
cc377381 LP |
986 | assert(message); |
987 | assert(m); | |
988 | ||
989 | r = sd_bus_message_read(message, "s", &name); | |
990 | if (r < 0) | |
ebcf1f97 | 991 | return r; |
cc377381 | 992 | |
309a29df LP |
993 | r = manager_get_session_from_creds(m, message, name, error, &session); |
994 | if (r < 0) | |
995 | return r; | |
cc377381 | 996 | |
ad8780c9 ZJS |
997 | r = session_release(session); |
998 | if (r < 0) | |
999 | return r; | |
cc377381 | 1000 | |
df2d202e | 1001 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
1002 | } |
1003 | ||
19070062 | 1004 | static int method_activate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
1005 | Manager *m = userdata; |
1006 | Session *session; | |
1007 | const char *name; | |
1008 | int r; | |
f8e2fb7b | 1009 | |
cc377381 | 1010 | assert(message); |
f8e2fb7b | 1011 | assert(m); |
cc377381 LP |
1012 | |
1013 | r = sd_bus_message_read(message, "s", &name); | |
1014 | if (r < 0) | |
ebcf1f97 | 1015 | return r; |
cc377381 | 1016 | |
309a29df LP |
1017 | r = manager_get_session_from_creds(m, message, name, error, &session); |
1018 | if (r < 0) | |
1019 | return r; | |
cc377381 | 1020 | |
4acf0cfd LP |
1021 | /* PolicyKit is done by bus_session_method_activate() */ |
1022 | ||
19070062 | 1023 | return bus_session_method_activate(message, session, error); |
cc377381 LP |
1024 | } |
1025 | ||
19070062 | 1026 | static int method_activate_session_on_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
1027 | const char *session_name, *seat_name; |
1028 | Manager *m = userdata; | |
1029 | Session *session; | |
1030 | Seat *seat; | |
1031 | int r; | |
1032 | ||
f8e2fb7b | 1033 | assert(message); |
cc377381 | 1034 | assert(m); |
f8e2fb7b | 1035 | |
3b92c086 | 1036 | /* Same as ActivateSession() but refuses to work if the seat doesn't match */ |
f8e2fb7b | 1037 | |
cc377381 LP |
1038 | r = sd_bus_message_read(message, "ss", &session_name, &seat_name); |
1039 | if (r < 0) | |
ebcf1f97 | 1040 | return r; |
eecd1362 | 1041 | |
309a29df LP |
1042 | r = manager_get_session_from_creds(m, message, session_name, error, &session); |
1043 | if (r < 0) | |
1044 | return r; | |
beaafb2e | 1045 | |
309a29df LP |
1046 | r = manager_get_seat_from_creds(m, message, seat_name, error, &seat); |
1047 | if (r < 0) | |
1048 | return r; | |
314b4b0a | 1049 | |
cc377381 | 1050 | if (session->seat != seat) |
124d7cb2 ZJS |
1051 | return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, |
1052 | "Session %s not on seat %s", session_name, seat_name); | |
cc377381 | 1053 | |
4acf0cfd LP |
1054 | r = bus_verify_polkit_async( |
1055 | message, | |
1056 | CAP_SYS_ADMIN, | |
1057 | "org.freedesktop.login1.chvt", | |
1058 | NULL, | |
1059 | false, | |
1060 | UID_INVALID, | |
1061 | &m->polkit_registry, | |
1062 | error); | |
1063 | if (r < 0) | |
1064 | return r; | |
1065 | if (r == 0) | |
1066 | return 1; /* Will call us back */ | |
1067 | ||
cc377381 | 1068 | r = session_activate(session); |
f8e2fb7b | 1069 | if (r < 0) |
ebcf1f97 | 1070 | return r; |
f8e2fb7b | 1071 | |
df2d202e | 1072 | return sd_bus_reply_method_return(message, NULL); |
cc377381 | 1073 | } |
f8e2fb7b | 1074 | |
19070062 | 1075 | static int method_lock_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
1076 | Manager *m = userdata; |
1077 | Session *session; | |
1078 | const char *name; | |
1079 | int r; | |
f8e2fb7b | 1080 | |
cc377381 LP |
1081 | assert(message); |
1082 | assert(m); | |
f8e2fb7b | 1083 | |
cc377381 LP |
1084 | r = sd_bus_message_read(message, "s", &name); |
1085 | if (r < 0) | |
ebcf1f97 | 1086 | return r; |
f8e2fb7b | 1087 | |
309a29df LP |
1088 | r = manager_get_session_from_creds(m, message, name, error, &session); |
1089 | if (r < 0) | |
1090 | return r; | |
f8e2fb7b | 1091 | |
19070062 | 1092 | return bus_session_method_lock(message, session, error); |
cc377381 | 1093 | } |
f8e2fb7b | 1094 | |
19070062 | 1095 | static int method_lock_sessions(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
1096 | Manager *m = userdata; |
1097 | int r; | |
f8e2fb7b | 1098 | |
cc377381 LP |
1099 | assert(message); |
1100 | assert(m); | |
f8e2fb7b | 1101 | |
c529695e LP |
1102 | r = bus_verify_polkit_async( |
1103 | message, | |
1104 | CAP_SYS_ADMIN, | |
1105 | "org.freedesktop.login1.lock-sessions", | |
403ed0e5 | 1106 | NULL, |
c529695e LP |
1107 | false, |
1108 | UID_INVALID, | |
1109 | &m->polkit_registry, | |
1110 | error); | |
1111 | if (r < 0) | |
1112 | return r; | |
1113 | if (r == 0) | |
1114 | return 1; /* Will call us back */ | |
1115 | ||
cc377381 LP |
1116 | r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions")); |
1117 | if (r < 0) | |
ebcf1f97 | 1118 | return r; |
f8e2fb7b | 1119 | |
df2d202e | 1120 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
1121 | } |
1122 | ||
19070062 | 1123 | static int method_kill_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c529695e | 1124 | const char *name; |
cc377381 LP |
1125 | Manager *m = userdata; |
1126 | Session *session; | |
cc377381 LP |
1127 | int r; |
1128 | ||
cc377381 LP |
1129 | assert(message); |
1130 | assert(m); | |
1131 | ||
c529695e | 1132 | r = sd_bus_message_read(message, "s", &name); |
cc377381 | 1133 | if (r < 0) |
ebcf1f97 | 1134 | return r; |
cc377381 | 1135 | |
309a29df LP |
1136 | r = manager_get_session_from_creds(m, message, name, error, &session); |
1137 | if (r < 0) | |
1138 | return r; | |
f8e2fb7b | 1139 | |
19070062 | 1140 | return bus_session_method_kill(message, session, error); |
cc377381 | 1141 | } |
f8e2fb7b | 1142 | |
19070062 | 1143 | static int method_kill_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
1144 | Manager *m = userdata; |
1145 | uint32_t uid; | |
cc377381 LP |
1146 | User *user; |
1147 | int r; | |
f8e2fb7b | 1148 | |
cc377381 LP |
1149 | assert(message); |
1150 | assert(m); | |
1151 | ||
c529695e | 1152 | r = sd_bus_message_read(message, "u", &uid); |
cc377381 | 1153 | if (r < 0) |
ebcf1f97 | 1154 | return r; |
cc377381 | 1155 | |
309a29df LP |
1156 | r = manager_get_user_from_creds(m, message, uid, error, &user); |
1157 | if (r < 0) | |
1158 | return r; | |
cc377381 | 1159 | |
19070062 | 1160 | return bus_user_method_kill(message, user, error); |
cc377381 LP |
1161 | } |
1162 | ||
19070062 | 1163 | static int method_terminate_session(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
1164 | Manager *m = userdata; |
1165 | const char *name; | |
1166 | Session *session; | |
1167 | int r; | |
1168 | ||
cc377381 LP |
1169 | assert(message); |
1170 | assert(m); | |
1171 | ||
1172 | r = sd_bus_message_read(message, "s", &name); | |
1173 | if (r < 0) | |
ebcf1f97 | 1174 | return r; |
cc377381 | 1175 | |
309a29df LP |
1176 | r = manager_get_session_from_creds(m, message, name, error, &session); |
1177 | if (r < 0) | |
1178 | return r; | |
cc377381 | 1179 | |
19070062 | 1180 | return bus_session_method_terminate(message, session, error); |
cc377381 LP |
1181 | } |
1182 | ||
19070062 | 1183 | static int method_terminate_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
1184 | Manager *m = userdata; |
1185 | uint32_t uid; | |
1186 | User *user; | |
1187 | int r; | |
1188 | ||
cc377381 LP |
1189 | assert(message); |
1190 | assert(m); | |
1191 | ||
1192 | r = sd_bus_message_read(message, "u", &uid); | |
1193 | if (r < 0) | |
ebcf1f97 | 1194 | return r; |
cc377381 | 1195 | |
309a29df LP |
1196 | r = manager_get_user_from_creds(m, message, uid, error, &user); |
1197 | if (r < 0) | |
1198 | return r; | |
cc377381 | 1199 | |
19070062 | 1200 | return bus_user_method_terminate(message, user, error); |
cc377381 LP |
1201 | } |
1202 | ||
19070062 | 1203 | static int method_terminate_seat(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
1204 | Manager *m = userdata; |
1205 | const char *name; | |
1206 | Seat *seat; | |
1207 | int r; | |
1208 | ||
cc377381 LP |
1209 | assert(message); |
1210 | assert(m); | |
1211 | ||
1212 | r = sd_bus_message_read(message, "s", &name); | |
1213 | if (r < 0) | |
ebcf1f97 | 1214 | return r; |
cc377381 | 1215 | |
309a29df LP |
1216 | r = manager_get_seat_from_creds(m, message, name, error, &seat); |
1217 | if (r < 0) | |
1218 | return r; | |
cc377381 | 1219 | |
19070062 | 1220 | return bus_seat_method_terminate(message, seat, error); |
cc377381 LP |
1221 | } |
1222 | ||
19070062 | 1223 | static int method_set_user_linger(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
34160d91 | 1224 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
cc377381 LP |
1225 | _cleanup_free_ char *cc = NULL; |
1226 | Manager *m = userdata; | |
152199f2 | 1227 | int r, b, interactive; |
cc377381 LP |
1228 | struct passwd *pw; |
1229 | const char *path; | |
34160d91 | 1230 | uint32_t uid, auth_uid; |
cc377381 | 1231 | |
cc377381 LP |
1232 | assert(message); |
1233 | assert(m); | |
1234 | ||
1235 | r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive); | |
1236 | if (r < 0) | |
ebcf1f97 | 1237 | return r; |
cc377381 | 1238 | |
34160d91 AJ |
1239 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID | |
1240 | SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds); | |
1241 | if (r < 0) | |
1242 | return r; | |
309a29df | 1243 | |
34160d91 AJ |
1244 | if (!uid_is_valid(uid)) { |
1245 | /* Note that we get the owner UID of the session or user unit, | |
1246 | * not the actual client UID here! */ | |
309a29df LP |
1247 | r = sd_bus_creds_get_owner_uid(creds, &uid); |
1248 | if (r < 0) | |
1249 | return r; | |
34160d91 | 1250 | } |
06820eaf | 1251 | |
34160d91 AJ |
1252 | /* owner_uid is racy, so for authorization we must use euid */ |
1253 | r = sd_bus_creds_get_euid(creds, &auth_uid); | |
1254 | if (r < 0) | |
1255 | return r; | |
309a29df | 1256 | |
cc377381 LP |
1257 | errno = 0; |
1258 | pw = getpwuid(uid); | |
1259 | if (!pw) | |
66855de7 | 1260 | return errno_or_else(ENOENT); |
cc377381 | 1261 | |
f3885791 LP |
1262 | r = bus_verify_polkit_async( |
1263 | message, | |
1264 | CAP_SYS_ADMIN, | |
34160d91 AJ |
1265 | uid == auth_uid ? "org.freedesktop.login1.set-self-linger" : |
1266 | "org.freedesktop.login1.set-user-linger", | |
403ed0e5 | 1267 | NULL, |
f3885791 | 1268 | interactive, |
c529695e | 1269 | UID_INVALID, |
f3885791 LP |
1270 | &m->polkit_registry, |
1271 | error); | |
cc377381 | 1272 | if (r < 0) |
ebcf1f97 | 1273 | return r; |
cc377381 LP |
1274 | if (r == 0) |
1275 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1276 | ||
6e5dcce4 | 1277 | (void) mkdir_p_label("/var/lib/systemd", 0755); |
37c1d5e9 | 1278 | r = mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0, MKDIR_WARN_MODE); |
cc377381 | 1279 | if (r < 0) |
ebcf1f97 | 1280 | return r; |
cc377381 LP |
1281 | |
1282 | cc = cescape(pw->pw_name); | |
1283 | if (!cc) | |
ebcf1f97 | 1284 | return -ENOMEM; |
cc377381 | 1285 | |
63c372cb | 1286 | path = strjoina("/var/lib/systemd/linger/", cc); |
cc377381 LP |
1287 | if (b) { |
1288 | User *u; | |
1289 | ||
1290 | r = touch(path); | |
1291 | if (r < 0) | |
ebcf1f97 | 1292 | return r; |
cc377381 LP |
1293 | |
1294 | if (manager_add_user_by_uid(m, uid, &u) >= 0) | |
1295 | user_start(u); | |
1296 | ||
1297 | } else { | |
1298 | User *u; | |
1299 | ||
1300 | r = unlink(path); | |
1301 | if (r < 0 && errno != ENOENT) | |
ebcf1f97 | 1302 | return -errno; |
cc377381 | 1303 | |
8cb4ab00 | 1304 | u = hashmap_get(m->users, UID_TO_PTR(uid)); |
cc377381 LP |
1305 | if (u) |
1306 | user_add_to_gc_queue(u); | |
1307 | } | |
1308 | ||
df2d202e | 1309 | return sd_bus_reply_method_return(message, NULL); |
f8e2fb7b LP |
1310 | } |
1311 | ||
4f209af7 YW |
1312 | static int trigger_device(Manager *m, sd_device *d) { |
1313 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; | |
b668e064 LP |
1314 | int r; |
1315 | ||
1316 | assert(m); | |
1317 | ||
4f209af7 YW |
1318 | r = sd_device_enumerator_new(&e); |
1319 | if (r < 0) | |
1320 | return r; | |
1321 | ||
1322 | r = sd_device_enumerator_allow_uninitialized(e); | |
1323 | if (r < 0) | |
1324 | return r; | |
b668e064 | 1325 | |
2eb916cd | 1326 | if (d) { |
4f209af7 | 1327 | r = sd_device_enumerator_add_match_parent(e, d); |
06acf2d4 LP |
1328 | if (r < 0) |
1329 | return r; | |
2eb916cd LP |
1330 | } |
1331 | ||
8437c059 | 1332 | FOREACH_DEVICE(e, d) { |
cc377381 | 1333 | _cleanup_free_ char *t = NULL; |
b668e064 LP |
1334 | const char *p; |
1335 | ||
4f209af7 YW |
1336 | r = sd_device_get_syspath(d, &p); |
1337 | if (r < 0) | |
1338 | return r; | |
b668e064 | 1339 | |
b910cc72 | 1340 | t = path_join(p, "uevent"); |
06acf2d4 LP |
1341 | if (!t) |
1342 | return -ENOMEM; | |
b668e064 | 1343 | |
57512c89 | 1344 | (void) write_string_file(t, "change", WRITE_STRING_FILE_DISABLE_BUFFER); |
b668e064 LP |
1345 | } |
1346 | ||
06acf2d4 | 1347 | return 0; |
b668e064 LP |
1348 | } |
1349 | ||
47a26690 | 1350 | static int attach_device(Manager *m, const char *seat, const char *sysfs) { |
4f209af7 | 1351 | _cleanup_(sd_device_unrefp) sd_device *d = NULL; |
7fd1b19b | 1352 | _cleanup_free_ char *rule = NULL, *file = NULL; |
c28fa3d3 | 1353 | const char *id_for_seat; |
47a26690 LP |
1354 | int r; |
1355 | ||
1356 | assert(m); | |
1357 | assert(seat); | |
1358 | assert(sysfs); | |
1359 | ||
4f209af7 YW |
1360 | r = sd_device_new_from_syspath(&d, sysfs); |
1361 | if (r < 0) | |
1362 | return r; | |
47a26690 | 1363 | |
4f209af7 | 1364 | if (sd_device_has_tag(d, "seat") <= 0) |
06acf2d4 | 1365 | return -ENODEV; |
47a26690 | 1366 | |
4f209af7 | 1367 | if (sd_device_get_property_value(d, "ID_FOR_SEAT", &id_for_seat) < 0) |
06acf2d4 | 1368 | return -ENODEV; |
47a26690 | 1369 | |
06acf2d4 LP |
1370 | if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) |
1371 | return -ENOMEM; | |
47a26690 | 1372 | |
06acf2d4 LP |
1373 | if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) |
1374 | return -ENOMEM; | |
47a26690 | 1375 | |
4f209af7 | 1376 | (void) mkdir_p_label("/etc/udev/rules.d", 0755); |
574d5f2d | 1377 | r = write_string_file_atomic_label(file, rule); |
a0a0c7f1 | 1378 | if (r < 0) |
06acf2d4 | 1379 | return r; |
47a26690 | 1380 | |
06acf2d4 | 1381 | return trigger_device(m, d); |
47a26690 LP |
1382 | } |
1383 | ||
b668e064 | 1384 | static int flush_devices(Manager *m) { |
7fd1b19b | 1385 | _cleanup_closedir_ DIR *d; |
b668e064 LP |
1386 | |
1387 | assert(m); | |
1388 | ||
1389 | d = opendir("/etc/udev/rules.d"); | |
1390 | if (!d) { | |
1391 | if (errno != ENOENT) | |
56f64d95 | 1392 | log_warning_errno(errno, "Failed to open /etc/udev/rules.d: %m"); |
b668e064 LP |
1393 | } else { |
1394 | struct dirent *de; | |
1395 | ||
8fb3f009 | 1396 | FOREACH_DIRENT_ALL(de, d, break) { |
28e68bb2 | 1397 | dirent_ensure_type(d, de); |
b668e064 LP |
1398 | if (!dirent_is_file(de)) |
1399 | continue; | |
1400 | ||
1401 | if (!startswith(de->d_name, "72-seat-")) | |
1402 | continue; | |
1403 | ||
1404 | if (!endswith(de->d_name, ".rules")) | |
1405 | continue; | |
1406 | ||
1407 | if (unlinkat(dirfd(d), de->d_name, 0) < 0) | |
56f64d95 | 1408 | log_warning_errno(errno, "Failed to unlink %s: %m", de->d_name); |
b668e064 | 1409 | } |
b668e064 LP |
1410 | } |
1411 | ||
1412 | return trigger_device(m, NULL); | |
1413 | } | |
1414 | ||
19070062 | 1415 | static int method_attach_device(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
1416 | const char *sysfs, *seat; |
1417 | Manager *m = userdata; | |
1418 | int interactive, r; | |
1419 | ||
cc377381 LP |
1420 | assert(message); |
1421 | assert(m); | |
1422 | ||
1423 | r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive); | |
1424 | if (r < 0) | |
ebcf1f97 | 1425 | return r; |
cc377381 | 1426 | |
bda06130 LP |
1427 | if (!path_is_normalized(sysfs)) |
1428 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not normalized", sysfs); | |
cc377381 | 1429 | if (!path_startswith(sysfs, "/sys")) |
ebcf1f97 | 1430 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs); |
cc377381 | 1431 | |
ed179418 LP |
1432 | if (SEAT_IS_SELF(seat) || SEAT_IS_AUTO(seat)) { |
1433 | Seat *found; | |
1434 | ||
1435 | r = manager_get_seat_from_creds(m, message, seat, error, &found); | |
1436 | if (r < 0) | |
1437 | return r; | |
1438 | ||
1439 | seat = found->id; | |
1440 | ||
1441 | } else if (!seat_name_is_valid(seat)) /* Note that a seat does not have to exist yet for this operation to succeed */ | |
1442 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat name %s is not valid", seat); | |
cc377381 | 1443 | |
f3885791 LP |
1444 | r = bus_verify_polkit_async( |
1445 | message, | |
1446 | CAP_SYS_ADMIN, | |
1447 | "org.freedesktop.login1.attach-device", | |
403ed0e5 | 1448 | NULL, |
f3885791 | 1449 | interactive, |
c529695e | 1450 | UID_INVALID, |
f3885791 LP |
1451 | &m->polkit_registry, |
1452 | error); | |
cc377381 | 1453 | if (r < 0) |
ebcf1f97 | 1454 | return r; |
cc377381 LP |
1455 | if (r == 0) |
1456 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1457 | ||
1458 | r = attach_device(m, seat, sysfs); | |
1459 | if (r < 0) | |
ebcf1f97 | 1460 | return r; |
cc377381 | 1461 | |
df2d202e | 1462 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
1463 | } |
1464 | ||
19070062 | 1465 | static int method_flush_devices(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
1466 | Manager *m = userdata; |
1467 | int interactive, r; | |
1468 | ||
cc377381 LP |
1469 | assert(message); |
1470 | assert(m); | |
1471 | ||
1472 | r = sd_bus_message_read(message, "b", &interactive); | |
1473 | if (r < 0) | |
ebcf1f97 | 1474 | return r; |
cc377381 | 1475 | |
f3885791 LP |
1476 | r = bus_verify_polkit_async( |
1477 | message, | |
1478 | CAP_SYS_ADMIN, | |
1479 | "org.freedesktop.login1.flush-devices", | |
403ed0e5 | 1480 | NULL, |
f3885791 | 1481 | interactive, |
c529695e | 1482 | UID_INVALID, |
f3885791 LP |
1483 | &m->polkit_registry, |
1484 | error); | |
cc377381 | 1485 | if (r < 0) |
ebcf1f97 | 1486 | return r; |
cc377381 LP |
1487 | if (r == 0) |
1488 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1489 | ||
1490 | r = flush_devices(m); | |
1491 | if (r < 0) | |
ebcf1f97 | 1492 | return r; |
cc377381 | 1493 | |
df2d202e | 1494 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
1495 | } |
1496 | ||
89f13440 | 1497 | static int have_multiple_sessions( |
89f13440 | 1498 | Manager *m, |
409133be | 1499 | uid_t uid) { |
89f13440 | 1500 | |
2154761f MS |
1501 | Session *session; |
1502 | Iterator i; | |
89f13440 LP |
1503 | |
1504 | assert(m); | |
1505 | ||
1ca04b87 LP |
1506 | /* Check for other users' sessions. Greeter sessions do not |
1507 | * count, and non-login sessions do not count either. */ | |
2154761f | 1508 | HASHMAP_FOREACH(session, m->sessions, i) |
1ca04b87 | 1509 | if (session->class == SESSION_USER && |
22c902fa | 1510 | session->user->user_record->uid != uid) |
2154761f | 1511 | return true; |
89f13440 LP |
1512 | |
1513 | return false; | |
1514 | } | |
1515 | ||
314b4b0a LP |
1516 | static int bus_manager_log_shutdown( |
1517 | Manager *m, | |
314b4b0a LP |
1518 | const char *unit_name) { |
1519 | ||
5744f59a | 1520 | const char *p, *q; |
314b4b0a LP |
1521 | |
1522 | assert(m); | |
1523 | assert(unit_name); | |
1524 | ||
314b4b0a | 1525 | if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) { |
f868cb58 | 1526 | p = "MESSAGE=System is powering down"; |
314b4b0a | 1527 | q = "SHUTDOWN=power-off"; |
314b4b0a | 1528 | } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) { |
f868cb58 | 1529 | p = "MESSAGE=System is rebooting"; |
314b4b0a | 1530 | q = "SHUTDOWN=reboot"; |
36b69c31 LP |
1531 | } else if (streq(unit_name, SPECIAL_HALT_TARGET)) { |
1532 | p = "MESSAGE=System is halting"; | |
1533 | q = "SHUTDOWN=halt"; | |
314b4b0a | 1534 | } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) { |
f868cb58 | 1535 | p = "MESSAGE=System is rebooting with kexec"; |
314b4b0a LP |
1536 | q = "SHUTDOWN=kexec"; |
1537 | } else { | |
f868cb58 | 1538 | p = "MESSAGE=System is shutting down"; |
314b4b0a LP |
1539 | q = NULL; |
1540 | } | |
1541 | ||
f868cb58 ZJS |
1542 | if (isempty(m->wall_message)) |
1543 | p = strjoina(p, "."); | |
1544 | else | |
1545 | p = strjoina(p, " (", m->wall_message, ")."); | |
9ef15026 | 1546 | |
e2cc6eca | 1547 | return log_struct(LOG_NOTICE, |
2b044526 | 1548 | "MESSAGE_ID=" SD_MESSAGE_SHUTDOWN_STR, |
314b4b0a | 1549 | p, |
a1230ff9 | 1550 | q); |
314b4b0a LP |
1551 | } |
1552 | ||
b5d3e168 KS |
1553 | static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) { |
1554 | Manager *m = userdata; | |
1555 | ||
1556 | assert(e); | |
1557 | assert(m); | |
1558 | ||
1559 | m->lid_switch_ignore_event_source = sd_event_source_unref(m->lid_switch_ignore_event_source); | |
1560 | return 0; | |
1561 | } | |
1562 | ||
1563 | int manager_set_lid_switch_ignore(Manager *m, usec_t until) { | |
1564 | int r; | |
1565 | ||
1566 | assert(m); | |
1567 | ||
1568 | if (until <= now(CLOCK_MONOTONIC)) | |
1569 | return 0; | |
1570 | ||
1571 | /* We want to ignore the lid switch for a while after each | |
1572 | * suspend, and after boot-up. Hence let's install a timer for | |
1573 | * this. As long as the event source exists we ignore the lid | |
1574 | * switch. */ | |
1575 | ||
1576 | if (m->lid_switch_ignore_event_source) { | |
1577 | usec_t u; | |
1578 | ||
1579 | r = sd_event_source_get_time(m->lid_switch_ignore_event_source, &u); | |
1580 | if (r < 0) | |
1581 | return r; | |
1582 | ||
1583 | if (until <= u) | |
1584 | return 0; | |
1585 | ||
1586 | r = sd_event_source_set_time(m->lid_switch_ignore_event_source, until); | |
1587 | } else | |
6a0f1f6d LP |
1588 | r = sd_event_add_time( |
1589 | m->event, | |
1590 | &m->lid_switch_ignore_event_source, | |
1591 | CLOCK_MONOTONIC, | |
1592 | until, 0, | |
1593 | lid_switch_ignore_handler, m); | |
b5d3e168 KS |
1594 | |
1595 | return r; | |
1596 | } | |
1597 | ||
6d7f7fd4 | 1598 | static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) { |
6d7f7fd4 AJ |
1599 | int active = _active; |
1600 | ||
1601 | assert(m); | |
06c2f0a8 | 1602 | assert(IN_SET(w, INHIBIT_SHUTDOWN, INHIBIT_SLEEP)); |
6d7f7fd4 AJ |
1603 | |
1604 | return sd_bus_emit_signal(m->bus, | |
1605 | "/org/freedesktop/login1", | |
1606 | "org.freedesktop.login1.Manager", | |
06c2f0a8 | 1607 | w == INHIBIT_SHUTDOWN ? "PrepareForShutdown" : "PrepareForSleep", |
6d7f7fd4 AJ |
1608 | "b", |
1609 | active); | |
1610 | } | |
1611 | ||
314b4b0a LP |
1612 | static int execute_shutdown_or_sleep( |
1613 | Manager *m, | |
1614 | InhibitWhat w, | |
1615 | const char *unit_name, | |
cc377381 | 1616 | sd_bus_error *error) { |
314b4b0a | 1617 | |
4afd3348 | 1618 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
cc377381 | 1619 | const char *p; |
cc377381 | 1620 | int r; |
eecd1362 | 1621 | |
af9792ac | 1622 | assert(m); |
b61fa4e0 | 1623 | assert(w > 0); |
314b4b0a | 1624 | assert(w < _INHIBIT_WHAT_MAX); |
d889a206 | 1625 | assert(unit_name); |
eecd1362 | 1626 | |
df75a1a8 AJ |
1627 | if (w == INHIBIT_SHUTDOWN) |
1628 | bus_manager_log_shutdown(m, unit_name); | |
314b4b0a | 1629 | |
5d990cc5 | 1630 | r = bus_call_method( |
df75a1a8 | 1631 | m->bus, |
5d990cc5 | 1632 | bus_systemd_mgr, |
df75a1a8 AJ |
1633 | "StartUnit", |
1634 | error, | |
1635 | &reply, | |
1636 | "ss", unit_name, "replace-irreversibly"); | |
1637 | if (r < 0) | |
6d7f7fd4 | 1638 | goto error; |
af9792ac | 1639 | |
df75a1a8 AJ |
1640 | r = sd_bus_message_read(reply, "o", &p); |
1641 | if (r < 0) | |
6d7f7fd4 | 1642 | goto error; |
af9792ac | 1643 | |
6b9f8b71 YW |
1644 | r = free_and_strdup(&m->action_job, p); |
1645 | if (r < 0) | |
6d7f7fd4 | 1646 | goto error; |
af9792ac | 1647 | |
314b4b0a | 1648 | m->action_unit = unit_name; |
314b4b0a | 1649 | m->action_what = w; |
af9792ac | 1650 | |
f9cd6be1 | 1651 | /* Make sure the lid switch is ignored for a while */ |
9d10cbee | 1652 | manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec); |
f9cd6be1 | 1653 | |
af9792ac | 1654 | return 0; |
6d7f7fd4 AJ |
1655 | |
1656 | error: | |
1657 | /* Tell people that they now may take a lock again */ | |
401e33ed | 1658 | (void) send_prepare_for(m, w, false); |
6d7f7fd4 AJ |
1659 | |
1660 | return r; | |
eecd1362 LP |
1661 | } |
1662 | ||
418b22b8 | 1663 | int manager_dispatch_delayed(Manager *manager, bool timeout) { |
c0f32805 | 1664 | |
4afd3348 | 1665 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
c0f32805 | 1666 | Inhibitor *offending = NULL; |
c0f32805 DM |
1667 | int r; |
1668 | ||
1669 | assert(manager); | |
c0f32805 DM |
1670 | |
1671 | if (manager->action_what == 0 || manager->action_job) | |
1672 | return 0; | |
1673 | ||
1674 | if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) { | |
1675 | _cleanup_free_ char *comm = NULL, *u = NULL; | |
1676 | ||
418b22b8 DM |
1677 | if (!timeout) |
1678 | return 0; | |
1679 | ||
c0f32805 DM |
1680 | (void) get_process_comm(offending->pid, &comm); |
1681 | u = uid_to_name(offending->uid); | |
1682 | ||
1683 | log_notice("Delay lock is active (UID "UID_FMT"/%s, PID "PID_FMT"/%s) but inhibitor timeout is reached.", | |
1684 | offending->uid, strna(u), | |
1685 | offending->pid, strna(comm)); | |
1686 | } | |
1687 | ||
1688 | /* Actually do the operation */ | |
1689 | r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error); | |
1690 | if (r < 0) { | |
6d7f7fd4 AJ |
1691 | log_warning("Error during inhibitor-delayed operation (already returned success to client): %s", |
1692 | bus_error_message(&error, r)); | |
c0f32805 DM |
1693 | |
1694 | manager->action_unit = NULL; | |
1695 | manager->action_what = 0; | |
418b22b8 | 1696 | return r; |
c0f32805 DM |
1697 | } |
1698 | ||
418b22b8 DM |
1699 | return 1; |
1700 | } | |
1701 | ||
1702 | static int manager_inhibit_timeout_handler( | |
1703 | sd_event_source *s, | |
1704 | uint64_t usec, | |
1705 | void *userdata) { | |
1706 | ||
1707 | Manager *manager = userdata; | |
1708 | int r; | |
1709 | ||
1710 | assert(manager); | |
1711 | assert(manager->inhibit_timeout_source == s); | |
1712 | ||
1713 | r = manager_dispatch_delayed(manager, true); | |
1714 | return (r < 0) ? r : 0; | |
c0f32805 DM |
1715 | } |
1716 | ||
314b4b0a LP |
1717 | static int delay_shutdown_or_sleep( |
1718 | Manager *m, | |
1719 | InhibitWhat w, | |
1720 | const char *unit_name) { | |
eecd1362 | 1721 | |
c0f32805 DM |
1722 | int r; |
1723 | usec_t timeout_val; | |
1724 | ||
eecd1362 | 1725 | assert(m); |
d889a206 LP |
1726 | assert(w >= 0); |
1727 | assert(w < _INHIBIT_WHAT_MAX); | |
314b4b0a | 1728 | assert(unit_name); |
eecd1362 | 1729 | |
c0f32805 DM |
1730 | timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max; |
1731 | ||
1732 | if (m->inhibit_timeout_source) { | |
1733 | r = sd_event_source_set_time(m->inhibit_timeout_source, timeout_val); | |
1734 | if (r < 0) | |
c2a23db0 | 1735 | return log_error_errno(r, "sd_event_source_set_time() failed: %m"); |
c0f32805 DM |
1736 | |
1737 | r = sd_event_source_set_enabled(m->inhibit_timeout_source, SD_EVENT_ONESHOT); | |
1738 | if (r < 0) | |
c2a23db0 | 1739 | return log_error_errno(r, "sd_event_source_set_enabled() failed: %m"); |
c0f32805 DM |
1740 | } else { |
1741 | r = sd_event_add_time(m->event, &m->inhibit_timeout_source, CLOCK_MONOTONIC, | |
1742 | timeout_val, 0, manager_inhibit_timeout_handler, m); | |
1743 | if (r < 0) | |
1744 | return r; | |
1745 | } | |
1746 | ||
314b4b0a LP |
1747 | m->action_unit = unit_name; |
1748 | m->action_what = w; | |
d889a206 LP |
1749 | |
1750 | return 0; | |
1751 | } | |
1752 | ||
069cfc85 LP |
1753 | int bus_manager_shutdown_or_sleep_now_or_later( |
1754 | Manager *m, | |
1755 | const char *unit_name, | |
1756 | InhibitWhat w, | |
cc377381 | 1757 | sd_bus_error *error) { |
069cfc85 | 1758 | |
c8c8ee85 | 1759 | _cleanup_free_ char *load_state = NULL; |
069cfc85 LP |
1760 | bool delayed; |
1761 | int r; | |
1762 | ||
1763 | assert(m); | |
1764 | assert(unit_name); | |
b61fa4e0 | 1765 | assert(w > 0); |
543680f4 | 1766 | assert(w < _INHIBIT_WHAT_MAX); |
af9792ac | 1767 | assert(!m->action_job); |
069cfc85 | 1768 | |
c8c8ee85 ZJS |
1769 | r = unit_load_state(m->bus, unit_name, &load_state); |
1770 | if (r < 0) | |
1771 | return r; | |
1772 | ||
baaa35ad ZJS |
1773 | if (!streq(load_state, "loaded")) |
1774 | return log_notice_errno(SYNTHETIC_ERRNO(EACCES), | |
1775 | "Unit %s is %s, refusing operation.", | |
1776 | unit_name, load_state); | |
c8c8ee85 | 1777 | |
314b4b0a | 1778 | /* Tell everybody to prepare for shutdown/sleep */ |
401e33ed | 1779 | (void) send_prepare_for(m, w, true); |
314b4b0a | 1780 | |
069cfc85 LP |
1781 | delayed = |
1782 | m->inhibit_delay_max > 0 && | |
85a428c6 | 1783 | manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL); |
069cfc85 LP |
1784 | |
1785 | if (delayed) | |
1786 | /* Shutdown is delayed, keep in mind what we | |
1787 | * want to do, and start a timeout */ | |
1788 | r = delay_shutdown_or_sleep(m, w, unit_name); | |
314b4b0a | 1789 | else |
069cfc85 LP |
1790 | /* Shutdown is not delayed, execute it |
1791 | * immediately */ | |
314b4b0a | 1792 | r = execute_shutdown_or_sleep(m, w, unit_name, error); |
069cfc85 LP |
1793 | |
1794 | return r; | |
1795 | } | |
1796 | ||
b7aa9589 | 1797 | static int verify_shutdown_creds( |
d889a206 | 1798 | Manager *m, |
cc377381 | 1799 | sd_bus_message *message, |
d889a206 | 1800 | InhibitWhat w, |
b7aa9589 | 1801 | bool interactive, |
d889a206 LP |
1802 | const char *action, |
1803 | const char *action_multiple_sessions, | |
1804 | const char *action_ignore_inhibit, | |
ebcf1f97 | 1805 | sd_bus_error *error) { |
d889a206 | 1806 | |
4afd3348 | 1807 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
069cfc85 | 1808 | bool multiple_sessions, blocked; |
cc377381 | 1809 | uid_t uid; |
b7aa9589 | 1810 | int r; |
d889a206 LP |
1811 | |
1812 | assert(m); | |
d889a206 | 1813 | assert(message); |
d889a206 LP |
1814 | assert(w >= 0); |
1815 | assert(w <= _INHIBIT_WHAT_MAX); | |
6524990f | 1816 | |
05bae4a6 | 1817 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); |
5b12334d LP |
1818 | if (r < 0) |
1819 | return r; | |
1820 | ||
05bae4a6 | 1821 | r = sd_bus_creds_get_euid(creds, &uid); |
cc377381 | 1822 | if (r < 0) |
ebcf1f97 | 1823 | return r; |
409133be | 1824 | |
cc377381 | 1825 | r = have_multiple_sessions(m, uid); |
d889a206 | 1826 | if (r < 0) |
ebcf1f97 | 1827 | return r; |
d889a206 LP |
1828 | |
1829 | multiple_sessions = r > 0; | |
85a428c6 | 1830 | blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL); |
d889a206 | 1831 | |
b7aa9589 | 1832 | if (multiple_sessions && action_multiple_sessions) { |
403ed0e5 | 1833 | r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, interactive, UID_INVALID, &m->polkit_registry, error); |
d889a206 | 1834 | if (r < 0) |
ebcf1f97 | 1835 | return r; |
055d4066 ZJS |
1836 | if (r == 0) |
1837 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
d889a206 LP |
1838 | } |
1839 | ||
b7aa9589 | 1840 | if (blocked && action_ignore_inhibit) { |
403ed0e5 | 1841 | r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, interactive, UID_INVALID, &m->polkit_registry, error); |
d889a206 | 1842 | if (r < 0) |
ebcf1f97 | 1843 | return r; |
055d4066 ZJS |
1844 | if (r == 0) |
1845 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
d889a206 LP |
1846 | } |
1847 | ||
b7aa9589 | 1848 | if (!multiple_sessions && !blocked && action) { |
403ed0e5 | 1849 | r = bus_verify_polkit_async(message, CAP_SYS_BOOT, action, NULL, interactive, UID_INVALID, &m->polkit_registry, error); |
d889a206 | 1850 | if (r < 0) |
ebcf1f97 | 1851 | return r; |
055d4066 ZJS |
1852 | if (r == 0) |
1853 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
d889a206 LP |
1854 | } |
1855 | ||
b7aa9589 DM |
1856 | return 0; |
1857 | } | |
1858 | ||
1859 | static int method_do_shutdown_or_sleep( | |
1860 | Manager *m, | |
1861 | sd_bus_message *message, | |
1862 | const char *unit_name, | |
1863 | InhibitWhat w, | |
1864 | const char *action, | |
1865 | const char *action_multiple_sessions, | |
1866 | const char *action_ignore_inhibit, | |
1867 | const char *sleep_verb, | |
1868 | sd_bus_error *error) { | |
1869 | ||
1870 | int interactive, r; | |
1871 | ||
1872 | assert(m); | |
1873 | assert(message); | |
1874 | assert(unit_name); | |
1875 | assert(w >= 0); | |
1876 | assert(w <= _INHIBIT_WHAT_MAX); | |
1877 | ||
1878 | r = sd_bus_message_read(message, "b", &interactive); | |
1879 | if (r < 0) | |
1880 | return r; | |
1881 | ||
1882 | /* Don't allow multiple jobs being executed at the same time */ | |
f8bfa318 | 1883 | if (m->action_what > 0) |
124d7cb2 ZJS |
1884 | return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, |
1885 | "There's already a shutdown or sleep operation in progress"); | |
b7aa9589 DM |
1886 | |
1887 | if (sleep_verb) { | |
1888 | r = can_sleep(sleep_verb); | |
b71c9758 | 1889 | if (r == -ENOSPC) |
8340b762 ZJS |
1890 | return sd_bus_error_set(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, |
1891 | "Not enough swap space for hibernation"); | |
b71c9758 | 1892 | if (r == 0) |
8340b762 ZJS |
1893 | return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, |
1894 | "Sleep verb \"%s\" not supported", sleep_verb); | |
b7aa9589 DM |
1895 | if (r < 0) |
1896 | return r; | |
b7aa9589 DM |
1897 | } |
1898 | ||
1899 | r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions, | |
1900 | action_ignore_inhibit, error); | |
1901 | if (r != 0) | |
1902 | return r; | |
1903 | ||
ebcf1f97 | 1904 | r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error); |
d889a206 | 1905 | if (r < 0) |
ebcf1f97 | 1906 | return r; |
d889a206 | 1907 | |
df2d202e | 1908 | return sd_bus_reply_method_return(message, NULL); |
eecd1362 LP |
1909 | } |
1910 | ||
19070062 | 1911 | static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
3f49d45a LP |
1912 | Manager *m = userdata; |
1913 | ||
cc377381 LP |
1914 | return method_do_shutdown_or_sleep( |
1915 | m, message, | |
1916 | SPECIAL_POWEROFF_TARGET, | |
1917 | INHIBIT_SHUTDOWN, | |
1918 | "org.freedesktop.login1.power-off", | |
1919 | "org.freedesktop.login1.power-off-multiple-sessions", | |
1920 | "org.freedesktop.login1.power-off-ignore-inhibit", | |
1921 | NULL, | |
ebcf1f97 | 1922 | error); |
cc377381 | 1923 | } |
88e3dc90 | 1924 | |
19070062 | 1925 | static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 1926 | Manager *m = userdata; |
88e3dc90 | 1927 | |
cc377381 LP |
1928 | return method_do_shutdown_or_sleep( |
1929 | m, message, | |
1930 | SPECIAL_REBOOT_TARGET, | |
1931 | INHIBIT_SHUTDOWN, | |
1932 | "org.freedesktop.login1.reboot", | |
1933 | "org.freedesktop.login1.reboot-multiple-sessions", | |
1934 | "org.freedesktop.login1.reboot-ignore-inhibit", | |
1935 | NULL, | |
ebcf1f97 | 1936 | error); |
cc377381 | 1937 | } |
88e3dc90 | 1938 | |
36b69c31 LP |
1939 | static int method_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1940 | Manager *m = userdata; | |
1941 | ||
1942 | return method_do_shutdown_or_sleep( | |
1943 | m, message, | |
1944 | SPECIAL_HALT_TARGET, | |
1945 | INHIBIT_SHUTDOWN, | |
1946 | "org.freedesktop.login1.halt", | |
1947 | "org.freedesktop.login1.halt-multiple-sessions", | |
1948 | "org.freedesktop.login1.halt-ignore-inhibit", | |
1949 | NULL, | |
1950 | error); | |
1951 | } | |
1952 | ||
19070062 | 1953 | static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 1954 | Manager *m = userdata; |
88e3dc90 | 1955 | |
cc377381 LP |
1956 | return method_do_shutdown_or_sleep( |
1957 | m, message, | |
1958 | SPECIAL_SUSPEND_TARGET, | |
1959 | INHIBIT_SLEEP, | |
1960 | "org.freedesktop.login1.suspend", | |
1961 | "org.freedesktop.login1.suspend-multiple-sessions", | |
1962 | "org.freedesktop.login1.suspend-ignore-inhibit", | |
1963 | "suspend", | |
ebcf1f97 | 1964 | error); |
cc377381 | 1965 | } |
88e3dc90 | 1966 | |
15e99a43 LP |
1967 | static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
1968 | Manager *m = userdata; | |
1969 | ||
1970 | return method_do_shutdown_or_sleep( | |
1971 | m, message, | |
1972 | SPECIAL_HIBERNATE_TARGET, | |
1973 | INHIBIT_SLEEP, | |
1974 | "org.freedesktop.login1.hibernate", | |
1975 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
1976 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
1977 | "hibernate", | |
1978 | error); | |
1979 | } | |
1980 | ||
1981 | static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
1982 | Manager *m = userdata; | |
1983 | ||
1984 | return method_do_shutdown_or_sleep( | |
1985 | m, message, | |
1986 | SPECIAL_HYBRID_SLEEP_TARGET, | |
1987 | INHIBIT_SLEEP, | |
1988 | "org.freedesktop.login1.hibernate", | |
1989 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
1990 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
1991 | "hybrid-sleep", | |
1992 | error); | |
1993 | } | |
1994 | ||
e68c79db | 1995 | static int method_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c58493c0 ML |
1996 | Manager *m = userdata; |
1997 | ||
1998 | return method_do_shutdown_or_sleep( | |
1999 | m, message, | |
e68c79db | 2000 | SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET, |
c58493c0 ML |
2001 | INHIBIT_SLEEP, |
2002 | "org.freedesktop.login1.hibernate", | |
2003 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
2004 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
2005 | "hybrid-sleep", | |
2006 | error); | |
2007 | } | |
2008 | ||
867c37f6 DM |
2009 | static int nologin_timeout_handler( |
2010 | sd_event_source *s, | |
2011 | uint64_t usec, | |
2012 | void *userdata) { | |
2013 | ||
2014 | Manager *m = userdata; | |
867c37f6 DM |
2015 | |
2016 | log_info("Creating /run/nologin, blocking further logins..."); | |
2017 | ||
6e11e7e6 LP |
2018 | m->unlink_nologin = |
2019 | create_shutdown_run_nologin_or_warn() >= 0; | |
867c37f6 DM |
2020 | |
2021 | return 0; | |
2022 | } | |
2023 | ||
2024 | static int update_schedule_file(Manager *m) { | |
91b3e7fb | 2025 | _cleanup_free_ char *temp_path = NULL; |
867c37f6 | 2026 | _cleanup_fclose_ FILE *f = NULL; |
91b3e7fb | 2027 | int r; |
867c37f6 DM |
2028 | |
2029 | assert(m); | |
2030 | ||
37c1d5e9 | 2031 | r = mkdir_safe_label("/run/systemd/shutdown", 0755, 0, 0, MKDIR_WARN_MODE); |
867c37f6 DM |
2032 | if (r < 0) |
2033 | return log_error_errno(r, "Failed to create shutdown subdirectory: %m"); | |
2034 | ||
867c37f6 DM |
2035 | r = fopen_temporary("/run/systemd/shutdown/scheduled", &f, &temp_path); |
2036 | if (r < 0) | |
2037 | return log_error_errno(r, "Failed to save information about scheduled shutdowns: %m"); | |
2038 | ||
2039 | (void) fchmod(fileno(f), 0644); | |
2040 | ||
2041 | fprintf(f, | |
2042 | "USEC="USEC_FMT"\n" | |
2043 | "WARN_WALL=%i\n" | |
2044 | "MODE=%s\n", | |
2045 | m->scheduled_shutdown_timeout, | |
2046 | m->enable_wall_messages, | |
2047 | m->scheduled_shutdown_type); | |
2048 | ||
91b3e7fb LP |
2049 | if (!isempty(m->wall_message)) { |
2050 | _cleanup_free_ char *t; | |
2051 | ||
2052 | t = cescape(m->wall_message); | |
2053 | if (!t) { | |
2054 | r = -ENOMEM; | |
2055 | goto fail; | |
2056 | } | |
2057 | ||
867c37f6 | 2058 | fprintf(f, "WALL_MESSAGE=%s\n", t); |
91b3e7fb | 2059 | } |
867c37f6 | 2060 | |
dacd6cee LP |
2061 | r = fflush_and_check(f); |
2062 | if (r < 0) | |
2063 | goto fail; | |
867c37f6 | 2064 | |
dacd6cee | 2065 | if (rename(temp_path, "/run/systemd/shutdown/scheduled") < 0) { |
867c37f6 | 2066 | r = -errno; |
dacd6cee | 2067 | goto fail; |
867c37f6 DM |
2068 | } |
2069 | ||
dacd6cee LP |
2070 | return 0; |
2071 | ||
2072 | fail: | |
2073 | (void) unlink(temp_path); | |
2074 | (void) unlink("/run/systemd/shutdown/scheduled"); | |
2075 | ||
2076 | return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m"); | |
867c37f6 DM |
2077 | } |
2078 | ||
df75a1a8 | 2079 | static void reset_scheduled_shutdown(Manager *m) { |
36b69c31 LP |
2080 | assert(m); |
2081 | ||
df75a1a8 AJ |
2082 | m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source); |
2083 | m->wall_message_timeout_source = sd_event_source_unref(m->wall_message_timeout_source); | |
2084 | m->nologin_timeout_source = sd_event_source_unref(m->nologin_timeout_source); | |
36b69c31 | 2085 | |
df75a1a8 AJ |
2086 | m->scheduled_shutdown_type = mfree(m->scheduled_shutdown_type); |
2087 | m->scheduled_shutdown_timeout = 0; | |
2088 | m->shutdown_dry_run = false; | |
2089 | ||
2090 | if (m->unlink_nologin) { | |
af229d7a | 2091 | (void) unlink_or_warn("/run/nologin"); |
df75a1a8 AJ |
2092 | m->unlink_nologin = false; |
2093 | } | |
36b69c31 | 2094 | |
df75a1a8 AJ |
2095 | (void) unlink("/run/systemd/shutdown/scheduled"); |
2096 | } | |
2097 | ||
8aaa023a DM |
2098 | static int manager_scheduled_shutdown_handler( |
2099 | sd_event_source *s, | |
2100 | uint64_t usec, | |
2101 | void *userdata) { | |
2102 | ||
4afd3348 | 2103 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
8aaa023a DM |
2104 | Manager *m = userdata; |
2105 | const char *target; | |
2106 | int r; | |
2107 | ||
2108 | assert(m); | |
2109 | ||
2110 | if (isempty(m->scheduled_shutdown_type)) | |
2111 | return 0; | |
2112 | ||
36b69c31 | 2113 | if (streq(m->scheduled_shutdown_type, "poweroff")) |
8aaa023a | 2114 | target = SPECIAL_POWEROFF_TARGET; |
36b69c31 | 2115 | else if (streq(m->scheduled_shutdown_type, "reboot")) |
8aaa023a | 2116 | target = SPECIAL_REBOOT_TARGET; |
36b69c31 LP |
2117 | else if (streq(m->scheduled_shutdown_type, "halt")) |
2118 | target = SPECIAL_HALT_TARGET; | |
2119 | else | |
2120 | assert_not_reached("unexpected shutdown type"); | |
8aaa023a | 2121 | |
b498d6ea | 2122 | /* Don't allow multiple jobs being executed at the same time */ |
f8bfa318 | 2123 | if (m->action_what > 0) { |
6d7f7fd4 | 2124 | r = -EALREADY; |
b498d6ea | 2125 | log_error("Scheduled shutdown to %s failed: shutdown or sleep operation already in progress", target); |
6d7f7fd4 | 2126 | goto error; |
b498d6ea AJ |
2127 | } |
2128 | ||
df75a1a8 AJ |
2129 | if (m->shutdown_dry_run) { |
2130 | /* We do not process delay inhibitors here. Otherwise, we | |
2131 | * would have to be considered "in progress" (like the check | |
2132 | * above) for some seconds after our admin has seen the final | |
2133 | * wall message. */ | |
2134 | ||
2135 | bus_manager_log_shutdown(m, target); | |
2136 | log_info("Running in dry run, suppressing action."); | |
2137 | reset_scheduled_shutdown(m); | |
2138 | ||
2139 | return 0; | |
2140 | } | |
2141 | ||
2142 | r = bus_manager_shutdown_or_sleep_now_or_later(m, target, INHIBIT_SHUTDOWN, &error); | |
6d7f7fd4 AJ |
2143 | if (r < 0) { |
2144 | log_error_errno(r, "Scheduled shutdown to %s failed: %m", target); | |
2145 | goto error; | |
2146 | } | |
8aaa023a DM |
2147 | |
2148 | return 0; | |
6d7f7fd4 AJ |
2149 | |
2150 | error: | |
2151 | reset_scheduled_shutdown(m); | |
2152 | return r; | |
8aaa023a DM |
2153 | } |
2154 | ||
19070062 | 2155 | static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
8aaa023a | 2156 | Manager *m = userdata; |
4afd3348 | 2157 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
8aaa023a DM |
2158 | const char *action_multiple_sessions = NULL; |
2159 | const char *action_ignore_inhibit = NULL; | |
2160 | const char *action = NULL; | |
2161 | uint64_t elapse; | |
2162 | char *type; | |
2163 | int r; | |
d13f5e16 | 2164 | bool dry_run = false; |
8aaa023a DM |
2165 | |
2166 | assert(m); | |
2167 | assert(message); | |
2168 | ||
2169 | r = sd_bus_message_read(message, "st", &type, &elapse); | |
2170 | if (r < 0) | |
2171 | return r; | |
2172 | ||
1389f4b9 DM |
2173 | if (startswith(type, "dry-")) { |
2174 | type += 4; | |
d13f5e16 | 2175 | dry_run = true; |
1389f4b9 DM |
2176 | } |
2177 | ||
36b69c31 LP |
2178 | if (streq(type, "poweroff")) { |
2179 | action = "org.freedesktop.login1.power-off"; | |
2180 | action_multiple_sessions = "org.freedesktop.login1.power-off-multiple-sessions"; | |
2181 | action_ignore_inhibit = "org.freedesktop.login1.power-off-ignore-inhibit"; | |
2182 | } else if (streq(type, "reboot")) { | |
8aaa023a DM |
2183 | action = "org.freedesktop.login1.reboot"; |
2184 | action_multiple_sessions = "org.freedesktop.login1.reboot-multiple-sessions"; | |
2185 | action_ignore_inhibit = "org.freedesktop.login1.reboot-ignore-inhibit"; | |
2186 | } else if (streq(type, "halt")) { | |
2187 | action = "org.freedesktop.login1.halt"; | |
2188 | action_multiple_sessions = "org.freedesktop.login1.halt-multiple-sessions"; | |
2189 | action_ignore_inhibit = "org.freedesktop.login1.halt-ignore-inhibit"; | |
8aaa023a DM |
2190 | } else |
2191 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type"); | |
2192 | ||
2193 | r = verify_shutdown_creds(m, message, INHIBIT_SHUTDOWN, false, | |
2194 | action, action_multiple_sessions, action_ignore_inhibit, error); | |
2195 | if (r != 0) | |
2196 | return r; | |
2197 | ||
2198 | if (m->scheduled_shutdown_timeout_source) { | |
2199 | r = sd_event_source_set_time(m->scheduled_shutdown_timeout_source, elapse); | |
2200 | if (r < 0) | |
c2a23db0 | 2201 | return log_error_errno(r, "sd_event_source_set_time() failed: %m"); |
8aaa023a DM |
2202 | |
2203 | r = sd_event_source_set_enabled(m->scheduled_shutdown_timeout_source, SD_EVENT_ONESHOT); | |
2204 | if (r < 0) | |
c2a23db0 | 2205 | return log_error_errno(r, "sd_event_source_set_enabled() failed: %m"); |
8aaa023a DM |
2206 | } else { |
2207 | r = sd_event_add_time(m->event, &m->scheduled_shutdown_timeout_source, | |
2208 | CLOCK_REALTIME, elapse, 0, manager_scheduled_shutdown_handler, m); | |
2209 | if (r < 0) | |
c2a23db0 | 2210 | return log_error_errno(r, "sd_event_add_time() failed: %m"); |
8aaa023a DM |
2211 | } |
2212 | ||
2213 | r = free_and_strdup(&m->scheduled_shutdown_type, type); | |
2214 | if (r < 0) { | |
2215 | m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source); | |
2216 | return log_oom(); | |
2217 | } | |
2218 | ||
d13f5e16 LP |
2219 | m->shutdown_dry_run = dry_run; |
2220 | ||
867c37f6 DM |
2221 | if (m->nologin_timeout_source) { |
2222 | r = sd_event_source_set_time(m->nologin_timeout_source, elapse); | |
2223 | if (r < 0) | |
c2a23db0 | 2224 | return log_error_errno(r, "sd_event_source_set_time() failed: %m"); |
867c37f6 DM |
2225 | |
2226 | r = sd_event_source_set_enabled(m->nologin_timeout_source, SD_EVENT_ONESHOT); | |
2227 | if (r < 0) | |
c2a23db0 | 2228 | return log_error_errno(r, "sd_event_source_set_enabled() failed: %m"); |
867c37f6 DM |
2229 | } else { |
2230 | r = sd_event_add_time(m->event, &m->nologin_timeout_source, | |
2231 | CLOCK_REALTIME, elapse - 5 * USEC_PER_MINUTE, 0, nologin_timeout_handler, m); | |
2232 | if (r < 0) | |
c2a23db0 | 2233 | return log_error_errno(r, "sd_event_add_time() failed: %m"); |
867c37f6 DM |
2234 | } |
2235 | ||
8aaa023a DM |
2236 | m->scheduled_shutdown_timeout = elapse; |
2237 | ||
e2fa5721 DM |
2238 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds); |
2239 | if (r >= 0) { | |
011062f3 | 2240 | const char *tty = NULL; |
e2fa5721 DM |
2241 | |
2242 | (void) sd_bus_creds_get_uid(creds, &m->scheduled_shutdown_uid); | |
2243 | (void) sd_bus_creds_get_tty(creds, &tty); | |
2244 | ||
2245 | r = free_and_strdup(&m->scheduled_shutdown_tty, tty); | |
2246 | if (r < 0) { | |
2247 | m->scheduled_shutdown_timeout_source = sd_event_source_unref(m->scheduled_shutdown_timeout_source); | |
2248 | return log_oom(); | |
2249 | } | |
2250 | } | |
2251 | ||
2252 | r = manager_setup_wall_message_timer(m); | |
2253 | if (r < 0) | |
2254 | return r; | |
2255 | ||
f8169e62 AJ |
2256 | r = update_schedule_file(m); |
2257 | if (r < 0) | |
2258 | return r; | |
867c37f6 | 2259 | |
8aaa023a DM |
2260 | return sd_bus_reply_method_return(message, NULL); |
2261 | } | |
2262 | ||
19070062 | 2263 | static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
8aaa023a DM |
2264 | Manager *m = userdata; |
2265 | bool cancelled; | |
2266 | ||
2267 | assert(m); | |
2268 | assert(message); | |
2269 | ||
2270 | cancelled = m->scheduled_shutdown_type != NULL; | |
1389f4b9 | 2271 | reset_scheduled_shutdown(m); |
fb91034c | 2272 | |
6e78fa4a | 2273 | if (cancelled && m->enable_wall_messages) { |
4afd3348 | 2274 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
e99742ef | 2275 | _cleanup_free_ char *username = NULL; |
e2fa5721 DM |
2276 | const char *tty = NULL; |
2277 | uid_t uid = 0; | |
2278 | int r; | |
2279 | ||
2280 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds); | |
2281 | if (r >= 0) { | |
2282 | (void) sd_bus_creds_get_uid(creds, &uid); | |
2283 | (void) sd_bus_creds_get_tty(creds, &tty); | |
2284 | } | |
2285 | ||
e99742ef | 2286 | username = uid_to_name(uid); |
e2fa5721 | 2287 | utmp_wall("The system shutdown has been cancelled", |
e99742ef | 2288 | username, tty, logind_wall_tty_filter, m); |
e2fa5721 DM |
2289 | } |
2290 | ||
8aaa023a DM |
2291 | return sd_bus_reply_method_return(message, "b", cancelled); |
2292 | } | |
2293 | ||
cc377381 LP |
2294 | static int method_can_shutdown_or_sleep( |
2295 | Manager *m, | |
2296 | sd_bus_message *message, | |
2297 | InhibitWhat w, | |
2298 | const char *action, | |
2299 | const char *action_multiple_sessions, | |
2300 | const char *action_ignore_inhibit, | |
ebcf1f97 LP |
2301 | const char *sleep_verb, |
2302 | sd_bus_error *error) { | |
de07ab16 | 2303 | |
4afd3348 | 2304 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
c8c8ee85 | 2305 | HandleAction handle; |
cc377381 LP |
2306 | bool multiple_sessions, challenge, blocked; |
2307 | const char *result = NULL; | |
2308 | uid_t uid; | |
2309 | int r; | |
de07ab16 | 2310 | |
cc377381 LP |
2311 | assert(m); |
2312 | assert(message); | |
2313 | assert(w >= 0); | |
2314 | assert(w <= _INHIBIT_WHAT_MAX); | |
2315 | assert(action); | |
2316 | assert(action_multiple_sessions); | |
2317 | assert(action_ignore_inhibit); | |
de07ab16 | 2318 | |
cc377381 LP |
2319 | if (sleep_verb) { |
2320 | r = can_sleep(sleep_verb); | |
8340b762 | 2321 | if (IN_SET(r, 0, -ENOSPC)) |
b71c9758 | 2322 | return sd_bus_reply_method_return(message, "s", "na"); |
de07ab16 | 2323 | if (r < 0) |
ebcf1f97 | 2324 | return r; |
cc377381 | 2325 | } |
de07ab16 | 2326 | |
05bae4a6 | 2327 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); |
5b12334d LP |
2328 | if (r < 0) |
2329 | return r; | |
2330 | ||
05bae4a6 | 2331 | r = sd_bus_creds_get_euid(creds, &uid); |
cc377381 | 2332 | if (r < 0) |
ebcf1f97 | 2333 | return r; |
de07ab16 | 2334 | |
cc377381 LP |
2335 | r = have_multiple_sessions(m, uid); |
2336 | if (r < 0) | |
ebcf1f97 | 2337 | return r; |
de07ab16 | 2338 | |
cc377381 | 2339 | multiple_sessions = r > 0; |
85a428c6 | 2340 | blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL); |
de07ab16 | 2341 | |
c8c8ee85 ZJS |
2342 | handle = handle_action_from_string(sleep_verb); |
2343 | if (handle >= 0) { | |
2344 | const char *target; | |
2345 | ||
2346 | target = manager_target_for_action(handle); | |
2347 | if (target) { | |
2348 | _cleanup_free_ char *load_state = NULL; | |
2349 | ||
2350 | r = unit_load_state(m->bus, target, &load_state); | |
2351 | if (r < 0) | |
2352 | return r; | |
2353 | ||
2354 | if (!streq(load_state, "loaded")) { | |
2355 | result = "no"; | |
2356 | goto finish; | |
2357 | } | |
2358 | } | |
2359 | } | |
2360 | ||
cc377381 | 2361 | if (multiple_sessions) { |
403ed0e5 | 2362 | r = bus_test_polkit(message, CAP_SYS_BOOT, action_multiple_sessions, NULL, UID_INVALID, &challenge, error); |
de07ab16 | 2363 | if (r < 0) |
ebcf1f97 | 2364 | return r; |
bef422ae | 2365 | |
cc377381 LP |
2366 | if (r > 0) |
2367 | result = "yes"; | |
2368 | else if (challenge) | |
2369 | result = "challenge"; | |
2370 | else | |
2371 | result = "no"; | |
2372 | } | |
bef422ae | 2373 | |
cc377381 | 2374 | if (blocked) { |
403ed0e5 | 2375 | r = bus_test_polkit(message, CAP_SYS_BOOT, action_ignore_inhibit, NULL, UID_INVALID, &challenge, error); |
bef422ae | 2376 | if (r < 0) |
ebcf1f97 | 2377 | return r; |
bef422ae | 2378 | |
0c093a62 HT |
2379 | if (r > 0) { |
2380 | if (!result) | |
2381 | result = "yes"; | |
2382 | } else if (challenge) { | |
2383 | if (!result || streq(result, "yes")) | |
2384 | result = "challenge"; | |
2385 | } else | |
cc377381 LP |
2386 | result = "no"; |
2387 | } | |
bef422ae | 2388 | |
cc377381 LP |
2389 | if (!multiple_sessions && !blocked) { |
2390 | /* If neither inhibit nor multiple sessions | |
2391 | * apply then just check the normal policy */ | |
bef422ae | 2392 | |
403ed0e5 | 2393 | r = bus_test_polkit(message, CAP_SYS_BOOT, action, NULL, UID_INVALID, &challenge, error); |
bef422ae | 2394 | if (r < 0) |
ebcf1f97 | 2395 | return r; |
bef422ae | 2396 | |
cc377381 LP |
2397 | if (r > 0) |
2398 | result = "yes"; | |
2399 | else if (challenge) | |
2400 | result = "challenge"; | |
2401 | else | |
2402 | result = "no"; | |
2403 | } | |
bef422ae | 2404 | |
c8c8ee85 | 2405 | finish: |
df2d202e | 2406 | return sd_bus_reply_method_return(message, "s", result); |
cc377381 | 2407 | } |
bef422ae | 2408 | |
19070062 | 2409 | static int method_can_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 2410 | Manager *m = userdata; |
bef422ae | 2411 | |
cc377381 LP |
2412 | return method_can_shutdown_or_sleep( |
2413 | m, message, | |
2414 | INHIBIT_SHUTDOWN, | |
2415 | "org.freedesktop.login1.power-off", | |
2416 | "org.freedesktop.login1.power-off-multiple-sessions", | |
2417 | "org.freedesktop.login1.power-off-ignore-inhibit", | |
ebcf1f97 LP |
2418 | NULL, |
2419 | error); | |
cc377381 | 2420 | } |
bef422ae | 2421 | |
19070062 | 2422 | static int method_can_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 2423 | Manager *m = userdata; |
bef422ae | 2424 | |
cc377381 LP |
2425 | return method_can_shutdown_or_sleep( |
2426 | m, message, | |
2427 | INHIBIT_SHUTDOWN, | |
2428 | "org.freedesktop.login1.reboot", | |
2429 | "org.freedesktop.login1.reboot-multiple-sessions", | |
2430 | "org.freedesktop.login1.reboot-ignore-inhibit", | |
ebcf1f97 LP |
2431 | NULL, |
2432 | error); | |
cc377381 | 2433 | } |
bef422ae | 2434 | |
36b69c31 LP |
2435 | static int method_can_halt(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
2436 | Manager *m = userdata; | |
2437 | ||
2438 | return method_can_shutdown_or_sleep( | |
2439 | m, message, | |
2440 | INHIBIT_SHUTDOWN, | |
2441 | "org.freedesktop.login1.halt", | |
2442 | "org.freedesktop.login1.halt-multiple-sessions", | |
2443 | "org.freedesktop.login1.halt-ignore-inhibit", | |
2444 | NULL, | |
2445 | error); | |
2446 | } | |
2447 | ||
19070062 | 2448 | static int method_can_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 2449 | Manager *m = userdata; |
7f7bb946 | 2450 | |
cc377381 LP |
2451 | return method_can_shutdown_or_sleep( |
2452 | m, message, | |
2453 | INHIBIT_SLEEP, | |
2454 | "org.freedesktop.login1.suspend", | |
2455 | "org.freedesktop.login1.suspend-multiple-sessions", | |
2456 | "org.freedesktop.login1.suspend-ignore-inhibit", | |
ebcf1f97 LP |
2457 | "suspend", |
2458 | error); | |
cc377381 | 2459 | } |
7f7bb946 | 2460 | |
19070062 | 2461 | static int method_can_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 2462 | Manager *m = userdata; |
02b16a19 | 2463 | |
cc377381 LP |
2464 | return method_can_shutdown_or_sleep( |
2465 | m, message, | |
2466 | INHIBIT_SLEEP, | |
2467 | "org.freedesktop.login1.hibernate", | |
2468 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
2469 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
ebcf1f97 LP |
2470 | "hibernate", |
2471 | error); | |
cc377381 | 2472 | } |
7f7bb946 | 2473 | |
19070062 | 2474 | static int method_can_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 2475 | Manager *m = userdata; |
7f7bb946 | 2476 | |
cc377381 LP |
2477 | return method_can_shutdown_or_sleep( |
2478 | m, message, | |
2479 | INHIBIT_SLEEP, | |
2480 | "org.freedesktop.login1.hibernate", | |
2481 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
2482 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
ebcf1f97 LP |
2483 | "hybrid-sleep", |
2484 | error); | |
cc377381 | 2485 | } |
38f3fc7d | 2486 | |
e68c79db | 2487 | static int method_can_suspend_then_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c58493c0 ML |
2488 | Manager *m = userdata; |
2489 | ||
2490 | return method_can_shutdown_or_sleep( | |
2491 | m, message, | |
2492 | INHIBIT_SLEEP, | |
2493 | "org.freedesktop.login1.hibernate", | |
2494 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
2495 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
e68c79db | 2496 | "suspend-then-hibernate", |
c58493c0 ML |
2497 | error); |
2498 | } | |
2499 | ||
428b296a VJ |
2500 | static int property_get_reboot_parameter( |
2501 | sd_bus *bus, | |
2502 | const char *path, | |
2503 | const char *interface, | |
2504 | const char *property, | |
2505 | sd_bus_message *reply, | |
2506 | void *userdata, | |
2507 | sd_bus_error *error) { | |
2508 | _cleanup_free_ char *parameter = NULL; | |
2509 | int r; | |
2510 | ||
2511 | assert(bus); | |
2512 | assert(reply); | |
2513 | assert(userdata); | |
2514 | ||
2515 | r = read_reboot_parameter(¶meter); | |
2516 | if (r < 0) | |
2517 | return r; | |
2518 | ||
2519 | return sd_bus_message_append(reply, "s", parameter); | |
2520 | } | |
2521 | ||
2522 | static int method_set_reboot_parameter( | |
2523 | sd_bus_message *message, | |
2524 | void *userdata, | |
2525 | sd_bus_error *error) { | |
2526 | ||
2527 | Manager *m = userdata; | |
2528 | const char *arg; | |
2529 | int r; | |
2530 | ||
2531 | assert(message); | |
2532 | assert(m); | |
2533 | ||
2534 | r = sd_bus_message_read(message, "s", &arg); | |
2535 | if (r < 0) | |
2536 | return r; | |
2537 | ||
2538 | r = detect_container(); | |
2539 | if (r < 0) | |
2540 | return r; | |
2541 | if (r > 0) | |
2542 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, | |
2543 | "Reboot parameter not supported in containers, refusing."); | |
2544 | ||
2545 | r = bus_verify_polkit_async(message, | |
2546 | CAP_SYS_ADMIN, | |
2547 | "org.freedesktop.login1.set-reboot-parameter", | |
2548 | NULL, | |
2549 | false, | |
2550 | UID_INVALID, | |
2551 | &m->polkit_registry, | |
2552 | error); | |
2553 | if (r < 0) | |
2554 | return r; | |
2555 | if (r == 0) | |
2556 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2557 | ||
2558 | r = update_reboot_parameter_and_warn(arg, false); | |
2559 | if (r < 0) | |
2560 | return r; | |
2561 | ||
2562 | return sd_bus_reply_method_return(message, NULL); | |
2563 | } | |
2564 | ||
2565 | static int method_can_reboot_parameter( | |
2566 | sd_bus_message *message, | |
2567 | void *userdata, | |
2568 | sd_bus_error *error) { | |
2569 | ||
0a0e594a | 2570 | _unused_ Manager *m = userdata; |
428b296a VJ |
2571 | int r; |
2572 | ||
2573 | assert(message); | |
2574 | assert(m); | |
2575 | ||
2576 | r = detect_container(); | |
2577 | if (r < 0) | |
2578 | return r; | |
2579 | if (r > 0) /* Inside containers, specifying a reboot parameter, doesn't make much sense */ | |
2580 | return sd_bus_reply_method_return(message, "s", "na"); | |
2581 | ||
2582 | return return_test_polkit( | |
2583 | message, | |
2584 | CAP_SYS_ADMIN, | |
2585 | "org.freedesktop.login1.set-reboot-parameter", | |
2586 | NULL, | |
2587 | UID_INVALID, | |
2588 | error); | |
2589 | } | |
2590 | ||
5bdf2243 JJ |
2591 | static int property_get_reboot_to_firmware_setup( |
2592 | sd_bus *bus, | |
2593 | const char *path, | |
2594 | const char *interface, | |
2595 | const char *property, | |
2596 | sd_bus_message *reply, | |
2597 | void *userdata, | |
2598 | sd_bus_error *error) { | |
2599 | int r; | |
2600 | ||
2601 | assert(bus); | |
2602 | assert(reply); | |
2603 | assert(userdata); | |
2604 | ||
e667266a LP |
2605 | r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP"); |
2606 | if (r == -ENXIO) { | |
2607 | /* EFI case: let's see what is currently configured in the EFI variables */ | |
2608 | r = efi_get_reboot_to_firmware(); | |
2609 | if (r < 0 && r != -EOPNOTSUPP) | |
2610 | log_warning_errno(r, "Failed to determine reboot-to-firmware-setup state: %m"); | |
2611 | } else if (r < 0) | |
2612 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m"); | |
2613 | else if (r > 0) { | |
2614 | /* Non-EFI case: let's see whether /run/systemd/reboot-to-firmware-setup exists. */ | |
2615 | if (access("/run/systemd/reboot-to-firmware-setup", F_OK) < 0) { | |
2616 | if (errno != ENOENT) | |
2617 | log_warning_errno(errno, "Failed to check whether /run/systemd/reboot-to-firmware-setup exists: %m"); | |
2618 | ||
2619 | r = false; | |
2620 | } else | |
2621 | r = true; | |
2622 | } | |
5bdf2243 JJ |
2623 | |
2624 | return sd_bus_message_append(reply, "b", r > 0); | |
2625 | } | |
2626 | ||
2627 | static int method_set_reboot_to_firmware_setup( | |
5bdf2243 JJ |
2628 | sd_bus_message *message, |
2629 | void *userdata, | |
2630 | sd_bus_error *error) { | |
2631 | ||
5bdf2243 | 2632 | Manager *m = userdata; |
e667266a LP |
2633 | bool use_efi; |
2634 | int b, r; | |
5bdf2243 | 2635 | |
5bdf2243 JJ |
2636 | assert(message); |
2637 | assert(m); | |
2638 | ||
889f25b2 | 2639 | r = sd_bus_message_read(message, "b", &b); |
5bdf2243 JJ |
2640 | if (r < 0) |
2641 | return r; | |
2642 | ||
e667266a LP |
2643 | r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP"); |
2644 | if (r == -ENXIO) { | |
2645 | /* EFI case: let's see what the firmware supports */ | |
2646 | ||
2647 | r = efi_reboot_to_firmware_supported(); | |
2648 | if (r == -EOPNOTSUPP) | |
2649 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Firmware does not support boot into firmware."); | |
2650 | if (r < 0) | |
2651 | return r; | |
2652 | ||
2653 | use_efi = true; | |
2654 | ||
2655 | } else if (r <= 0) { | |
2656 | /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to off */ | |
2657 | ||
2658 | if (r < 0) | |
2659 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m"); | |
2660 | ||
2661 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Firmware does not support boot into firmware."); | |
2662 | } else | |
2663 | /* non-EFI case: $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP is set to on */ | |
2664 | use_efi = false; | |
2665 | ||
5bdf2243 JJ |
2666 | r = bus_verify_polkit_async(message, |
2667 | CAP_SYS_ADMIN, | |
2668 | "org.freedesktop.login1.set-reboot-to-firmware-setup", | |
403ed0e5 | 2669 | NULL, |
889f25b2 | 2670 | false, |
5bdf2243 JJ |
2671 | UID_INVALID, |
2672 | &m->polkit_registry, | |
2673 | error); | |
2674 | if (r < 0) | |
2675 | return r; | |
2676 | if (r == 0) | |
2677 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2678 | ||
e667266a LP |
2679 | if (use_efi) { |
2680 | r = efi_set_reboot_to_firmware(b); | |
2681 | if (r < 0) | |
2682 | return r; | |
2683 | } else { | |
2684 | if (b) { | |
2685 | r = touch("/run/systemd/reboot-to-firmware-setup"); | |
2686 | if (r < 0) | |
2687 | return r; | |
2688 | } else { | |
2689 | if (unlink("/run/systemd/reboot-to-firmware-setup") < 0 && errno != ENOENT) | |
2690 | return -errno; | |
2691 | } | |
2692 | } | |
5bdf2243 JJ |
2693 | |
2694 | return sd_bus_reply_method_return(message, NULL); | |
2695 | } | |
2696 | ||
2697 | static int method_can_reboot_to_firmware_setup( | |
5bdf2243 JJ |
2698 | sd_bus_message *message, |
2699 | void *userdata, | |
2700 | sd_bus_error *error) { | |
2701 | ||
0a0e594a | 2702 | _unused_ Manager *m = userdata; |
e667266a | 2703 | int r; |
5bdf2243 | 2704 | |
5bdf2243 JJ |
2705 | assert(message); |
2706 | assert(m); | |
2707 | ||
e667266a LP |
2708 | r = getenv_bool("SYSTEMD_REBOOT_TO_FIRMWARE_SETUP"); |
2709 | if (r == -ENXIO) { | |
2710 | /* EFI case: let's see what the firmware supports */ | |
2711 | ||
2712 | r = efi_reboot_to_firmware_supported(); | |
2713 | if (r < 0) { | |
2714 | if (r != -EOPNOTSUPP) | |
2715 | log_warning_errno(r, "Failed to determine whether reboot to firmware is supported: %m"); | |
2716 | ||
350f9518 | 2717 | return sd_bus_reply_method_return(message, "s", "na"); |
e667266a | 2718 | } |
6f302ce6 | 2719 | |
e667266a LP |
2720 | } else if (r <= 0) { |
2721 | /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP */ | |
2722 | ||
2723 | if (r < 0) | |
2724 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_FIRMWARE_SETUP: %m"); | |
2725 | ||
350f9518 | 2726 | return sd_bus_reply_method_return(message, "s", "na"); |
6f302ce6 | 2727 | } |
5bdf2243 | 2728 | |
350f9518 LP |
2729 | return return_test_polkit( |
2730 | message, | |
2731 | CAP_SYS_ADMIN, | |
2732 | "org.freedesktop.login1.set-reboot-to-firmware-setup", | |
2733 | NULL, | |
2734 | UID_INVALID, | |
2735 | error); | |
5bdf2243 JJ |
2736 | } |
2737 | ||
31b221cf LP |
2738 | static int property_get_reboot_to_boot_loader_menu( |
2739 | sd_bus *bus, | |
2740 | const char *path, | |
2741 | const char *interface, | |
2742 | const char *property, | |
2743 | sd_bus_message *reply, | |
2744 | void *userdata, | |
2745 | sd_bus_error *error) { | |
2746 | ||
2747 | uint64_t x = UINT64_MAX; | |
2748 | int r; | |
2749 | ||
2750 | assert(bus); | |
2751 | assert(reply); | |
2752 | assert(userdata); | |
2753 | ||
2754 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU"); | |
2755 | if (r == -ENXIO) { | |
31b221cf LP |
2756 | /* EFI case: returns the current value of LoaderConfigTimeoutOneShot. Three cases are distuingished: |
2757 | * | |
2758 | * 1. Variable not set, boot into boot loader menu is not enabled (we return UINT64_MAX to the user) | |
2759 | * 2. Variable set to "0", boot into boot loader menu is enabled with no timeout (we return 0 to the user) | |
2760 | * 3. Variable set to numeric value formatted in ASCII, boot into boot loader menu with the specified timeout in seconds | |
2761 | */ | |
2762 | ||
e8df4eee | 2763 | r = efi_loader_get_config_timeout_one_shot(&x); |
31b221cf LP |
2764 | if (r < 0) { |
2765 | if (r != -ENOENT) | |
e8df4eee | 2766 | log_warning_errno(r, "Failed to read LoaderConfigTimeoutOneShot variable, ignoring: %m"); |
31b221cf LP |
2767 | } |
2768 | ||
2769 | } else if (r < 0) | |
2770 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m"); | |
2771 | else if (r > 0) { | |
2772 | _cleanup_free_ char *v = NULL; | |
2773 | ||
2774 | /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-menu. */ | |
2775 | ||
2776 | r = read_one_line_file("/run/systemd/reboot-to-boot-loader-menu", &v); | |
2777 | if (r < 0) { | |
2778 | if (r != -ENOENT) | |
2779 | log_warning_errno(r, "Failed to read /run/systemd/reboot-to-boot-loader-menu: %m"); | |
2780 | } else { | |
2781 | r = safe_atou64(v, &x); | |
2782 | if (r < 0) | |
2783 | log_warning_errno(r, "Failed to parse /run/systemd/reboot-to-boot-loader-menu: %m"); | |
2784 | } | |
2785 | } | |
2786 | ||
2787 | return sd_bus_message_append(reply, "t", x); | |
2788 | } | |
2789 | ||
2790 | static int method_set_reboot_to_boot_loader_menu( | |
2791 | sd_bus_message *message, | |
2792 | void *userdata, | |
2793 | sd_bus_error *error) { | |
2794 | ||
2795 | Manager *m = userdata; | |
2796 | bool use_efi; | |
2797 | uint64_t x; | |
2798 | int r; | |
2799 | ||
2800 | assert(message); | |
2801 | assert(m); | |
2802 | ||
2803 | r = sd_bus_message_read(message, "t", &x); | |
2804 | if (r < 0) | |
2805 | return r; | |
2806 | ||
2807 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU"); | |
2808 | if (r == -ENXIO) { | |
2809 | uint64_t features; | |
2810 | ||
2811 | /* EFI case: let's see if booting into boot loader menu is supported. */ | |
2812 | ||
2813 | r = efi_loader_get_features(&features); | |
2814 | if (r < 0) | |
2815 | log_warning_errno(r, "Failed to determine whether reboot to boot loader menu is supported: %m"); | |
2816 | if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT)) | |
2817 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader does not support boot into boot loader menu."); | |
2818 | ||
2819 | use_efi = true; | |
2820 | ||
2821 | } else if (r <= 0) { | |
2822 | /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to off */ | |
2823 | ||
2824 | if (r < 0) | |
2825 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m"); | |
2826 | ||
2827 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader does not support boot into boot loader menu."); | |
2828 | } else | |
2829 | /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU is set to on */ | |
2830 | use_efi = false; | |
2831 | ||
2832 | r = bus_verify_polkit_async(message, | |
2833 | CAP_SYS_ADMIN, | |
2834 | "org.freedesktop.login1.set-reboot-to-boot-loader-menu", | |
2835 | NULL, | |
2836 | false, | |
2837 | UID_INVALID, | |
2838 | &m->polkit_registry, | |
2839 | error); | |
2840 | if (r < 0) | |
2841 | return r; | |
2842 | if (r == 0) | |
2843 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
2844 | ||
2845 | if (use_efi) { | |
2846 | if (x == UINT64_MAX) | |
2847 | r = efi_set_variable(EFI_VENDOR_LOADER, "LoaderConfigTimeoutOneShot", NULL, 0); | |
2848 | else { | |
2849 | char buf[DECIMAL_STR_MAX(uint64_t) + 1]; | |
2850 | xsprintf(buf, "%" PRIu64, DIV_ROUND_UP(x, USEC_PER_SEC)); /* second granularity */ | |
2851 | ||
2852 | r = efi_set_variable_string(EFI_VENDOR_LOADER, "LoaderConfigTimeoutOneShot", buf); | |
2853 | } | |
2854 | if (r < 0) | |
2855 | return r; | |
2856 | } else { | |
2857 | if (x == UINT64_MAX) { | |
2858 | if (unlink("/run/systemd/reboot-to-loader-menu") < 0 && errno != ENOENT) | |
2859 | return -errno; | |
2860 | } else { | |
2861 | char buf[DECIMAL_STR_MAX(uint64_t) + 1]; | |
2862 | ||
2863 | xsprintf(buf, "%" PRIu64, x); /* µs granularity */ | |
2864 | ||
2865 | r = write_string_file_atomic_label("/run/systemd/reboot-to-loader-menu", buf); | |
2866 | if (r < 0) | |
2867 | return r; | |
2868 | } | |
2869 | } | |
2870 | ||
2871 | return sd_bus_reply_method_return(message, NULL); | |
2872 | } | |
2873 | ||
2874 | static int method_can_reboot_to_boot_loader_menu( | |
2875 | sd_bus_message *message, | |
2876 | void *userdata, | |
2877 | sd_bus_error *error) { | |
2878 | ||
0a0e594a | 2879 | _unused_ Manager *m = userdata; |
31b221cf LP |
2880 | int r; |
2881 | ||
31b221cf LP |
2882 | assert(message); |
2883 | assert(m); | |
2884 | ||
2885 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU"); | |
2886 | if (r == -ENXIO) { | |
2887 | uint64_t features = 0; | |
2888 | ||
2889 | /* EFI case, let's see if booting into boot loader menu is supported. */ | |
2890 | ||
2891 | r = efi_loader_get_features(&features); | |
2892 | if (r < 0) | |
2893 | log_warning_errno(r, "Failed to determine whether reboot to boot loader menu is supported: %m"); | |
2894 | if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_CONFIG_TIMEOUT_ONE_SHOT)) | |
350f9518 | 2895 | return sd_bus_reply_method_return(message, "s", "na"); |
31b221cf LP |
2896 | |
2897 | } else if (r <= 0) { | |
2898 | /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU */ | |
2899 | ||
2900 | if (r < 0) | |
2901 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_MENU: %m"); | |
2902 | ||
350f9518 | 2903 | return sd_bus_reply_method_return(message, "s", "na"); |
31b221cf LP |
2904 | } |
2905 | ||
350f9518 LP |
2906 | return return_test_polkit( |
2907 | message, | |
2908 | CAP_SYS_ADMIN, | |
2909 | "org.freedesktop.login1.set-reboot-to-boot-loader-menu", | |
2910 | NULL, | |
2911 | UID_INVALID, | |
2912 | error); | |
31b221cf LP |
2913 | } |
2914 | ||
2915 | static int property_get_reboot_to_boot_loader_entry( | |
2916 | sd_bus *bus, | |
2917 | const char *path, | |
2918 | const char *interface, | |
2919 | const char *property, | |
2920 | sd_bus_message *reply, | |
2921 | void *userdata, | |
2922 | sd_bus_error *error) { | |
2923 | ||
2924 | _cleanup_free_ char *v = NULL; | |
af2697e8 LP |
2925 | Manager *m = userdata; |
2926 | const char *x = NULL; | |
31b221cf LP |
2927 | int r; |
2928 | ||
2929 | assert(bus); | |
2930 | assert(reply); | |
af2697e8 | 2931 | assert(m); |
31b221cf LP |
2932 | |
2933 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY"); | |
2934 | if (r == -ENXIO) { | |
2935 | /* EFI case: let's read the LoaderEntryOneShot variable */ | |
2936 | ||
af2697e8 | 2937 | r = efi_loader_update_entry_one_shot_cache(&m->efi_loader_entry_one_shot, &m->efi_loader_entry_one_shot_stat); |
31b221cf LP |
2938 | if (r < 0) { |
2939 | if (r != -ENOENT) | |
af2697e8 LP |
2940 | log_warning_errno(r, "Failed to read LoaderEntryOneShot variable, ignoring: %m"); |
2941 | } else | |
2942 | x = m->efi_loader_entry_one_shot; | |
2943 | ||
31b221cf LP |
2944 | } else if (r < 0) |
2945 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m"); | |
2946 | else if (r > 0) { | |
2947 | ||
2948 | /* Non-EFI case, let's process /run/systemd/reboot-to-boot-loader-entry. */ | |
2949 | ||
2950 | r = read_one_line_file("/run/systemd/reboot-to-boot-loader-entry", &v); | |
2951 | if (r < 0) { | |
2952 | if (r != -ENOENT) | |
af2697e8 LP |
2953 | log_warning_errno(r, "Failed to read /run/systemd/reboot-to-boot-loader-entry, ignoring: %m"); |
2954 | } else if (!efi_loader_entry_name_valid(v)) | |
31b221cf | 2955 | log_warning("/run/systemd/reboot-to-boot-loader-entry is not valid, ignoring."); |
af2697e8 LP |
2956 | else |
2957 | x = v; | |
31b221cf LP |
2958 | } |
2959 | ||
af2697e8 | 2960 | return sd_bus_message_append(reply, "s", x); |
31b221cf LP |
2961 | } |
2962 | ||
d4bd786d | 2963 | static int boot_loader_entry_exists(Manager *m, const char *id) { |
31b221cf LP |
2964 | _cleanup_(boot_config_free) BootConfig config = {}; |
2965 | int r; | |
2966 | ||
d4bd786d | 2967 | assert(m); |
31b221cf LP |
2968 | assert(id); |
2969 | ||
2970 | r = boot_entries_load_config_auto(NULL, NULL, &config); | |
cc5957dc | 2971 | if (r < 0 && r != -ENOKEY) /* don't complain if no GPT is found, hence skip ENOKEY */ |
31b221cf LP |
2972 | return r; |
2973 | ||
d4bd786d LP |
2974 | r = manager_read_efi_boot_loader_entries(m); |
2975 | if (r >= 0) | |
2976 | (void) boot_entries_augment_from_loader(&config, m->efi_boot_loader_entries, true); | |
31b221cf LP |
2977 | |
2978 | return boot_config_has_entry(&config, id); | |
2979 | } | |
2980 | ||
2981 | static int method_set_reboot_to_boot_loader_entry( | |
2982 | sd_bus_message *message, | |
2983 | void *userdata, | |
2984 | sd_bus_error *error) { | |
2985 | ||
2986 | Manager *m = userdata; | |
2987 | bool use_efi; | |
2988 | const char *v; | |
2989 | int r; | |
2990 | ||
2991 | assert(message); | |
2992 | assert(m); | |
2993 | ||
2994 | r = sd_bus_message_read(message, "s", &v); | |
2995 | if (r < 0) | |
2996 | return r; | |
2997 | ||
2998 | if (isempty(v)) | |
2999 | v = NULL; | |
3000 | else if (efi_loader_entry_name_valid(v)) { | |
d4bd786d | 3001 | r = boot_loader_entry_exists(m, v); |
31b221cf LP |
3002 | if (r < 0) |
3003 | return r; | |
3004 | if (r == 0) | |
3005 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Boot loader entry '%s' is not known.", v); | |
3006 | } else | |
3007 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Boot loader entry name '%s' is not valid, refusing.", v); | |
3008 | ||
3009 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY"); | |
3010 | if (r == -ENXIO) { | |
3011 | uint64_t features; | |
3012 | ||
3013 | /* EFI case: let's see if booting into boot loader entry is supported. */ | |
3014 | ||
3015 | r = efi_loader_get_features(&features); | |
3016 | if (r < 0) | |
3017 | log_warning_errno(r, "Failed to determine whether reboot into boot loader entry is supported: %m"); | |
3018 | if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_ENTRY_ONESHOT)) | |
3019 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Loader does not support boot into boot loader entry."); | |
3020 | ||
3021 | use_efi = true; | |
3022 | ||
3023 | } else if (r <= 0) { | |
3024 | /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to off */ | |
3025 | ||
3026 | if (r < 0) | |
3027 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m"); | |
3028 | ||
3029 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Loader does not support boot into boot loader entry."); | |
3030 | } else | |
3031 | /* non-EFI case: $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY is set to on */ | |
3032 | use_efi = false; | |
3033 | ||
3034 | r = bus_verify_polkit_async(message, | |
3035 | CAP_SYS_ADMIN, | |
3036 | "org.freedesktop.login1.set-reboot-to-boot-loader-entry", | |
3037 | NULL, | |
3038 | false, | |
3039 | UID_INVALID, | |
3040 | &m->polkit_registry, | |
3041 | error); | |
3042 | if (r < 0) | |
3043 | return r; | |
3044 | if (r == 0) | |
3045 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
3046 | ||
3047 | if (use_efi) { | |
3048 | if (isempty(v)) | |
3049 | /* Delete item */ | |
3050 | r = efi_set_variable(EFI_VENDOR_LOADER, "LoaderEntryOneShot", NULL, 0); | |
3051 | else | |
3052 | r = efi_set_variable_string(EFI_VENDOR_LOADER, "LoaderEntryOneShot", v); | |
3053 | if (r < 0) | |
3054 | return r; | |
3055 | } else { | |
3056 | if (isempty(v)) { | |
3057 | if (unlink("/run/systemd/reboot-to-boot-loader-entry") < 0 && errno != ENOENT) | |
3058 | return -errno; | |
3059 | } else { | |
3060 | r = write_string_file_atomic_label("/run/systemd/reboot-boot-to-loader-entry", v); | |
3061 | if (r < 0) | |
3062 | return r; | |
3063 | } | |
3064 | } | |
3065 | ||
3066 | return sd_bus_reply_method_return(message, NULL); | |
3067 | } | |
3068 | ||
3069 | static int method_can_reboot_to_boot_loader_entry( | |
3070 | sd_bus_message *message, | |
3071 | void *userdata, | |
3072 | sd_bus_error *error) { | |
3073 | ||
0a0e594a | 3074 | _unused_ Manager *m = userdata; |
31b221cf LP |
3075 | int r; |
3076 | ||
3077 | assert(message); | |
3078 | assert(m); | |
3079 | ||
3080 | r = getenv_bool("SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY"); | |
3081 | if (r == -ENXIO) { | |
3082 | uint64_t features = 0; | |
3083 | ||
3084 | /* EFI case, let's see if booting into boot loader entry is supported. */ | |
3085 | ||
3086 | r = efi_loader_get_features(&features); | |
3087 | if (r < 0) | |
3088 | log_warning_errno(r, "Failed to determine whether reboot to boot loader entry is supported: %m"); | |
3089 | if (r < 0 || !FLAGS_SET(features, EFI_LOADER_FEATURE_ENTRY_ONESHOT)) | |
350f9518 | 3090 | return sd_bus_reply_method_return(message, "s", "na"); |
31b221cf LP |
3091 | |
3092 | } else if (r <= 0) { | |
3093 | /* Non-EFI case: let's trust $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY */ | |
3094 | ||
3095 | if (r < 0) | |
3096 | log_warning_errno(r, "Failed to parse $SYSTEMD_REBOOT_TO_BOOT_LOADER_ENTRY: %m"); | |
3097 | ||
350f9518 | 3098 | return sd_bus_reply_method_return(message, "s", "na"); |
31b221cf LP |
3099 | } |
3100 | ||
350f9518 LP |
3101 | return return_test_polkit( |
3102 | message, | |
3103 | CAP_SYS_ADMIN, | |
3104 | "org.freedesktop.login1.set-reboot-to-boot-loader-entry", | |
3105 | NULL, | |
3106 | UID_INVALID, | |
3107 | error); | |
31b221cf LP |
3108 | } |
3109 | ||
3110 | static int property_get_boot_loader_entries( | |
3111 | sd_bus *bus, | |
3112 | const char *path, | |
3113 | const char *interface, | |
3114 | const char *property, | |
3115 | sd_bus_message *reply, | |
3116 | void *userdata, | |
3117 | sd_bus_error *error) { | |
3118 | ||
3119 | _cleanup_(boot_config_free) BootConfig config = {}; | |
d4bd786d | 3120 | Manager *m = userdata; |
31b221cf LP |
3121 | size_t i; |
3122 | int r; | |
3123 | ||
3124 | assert(bus); | |
3125 | assert(reply); | |
d4bd786d | 3126 | assert(m); |
31b221cf LP |
3127 | |
3128 | r = boot_entries_load_config_auto(NULL, NULL, &config); | |
cc5957dc | 3129 | if (r < 0 && r != -ENOKEY) /* don't complain if there's no GPT found */ |
31b221cf LP |
3130 | return r; |
3131 | ||
d4bd786d LP |
3132 | r = manager_read_efi_boot_loader_entries(m); |
3133 | if (r >= 0) | |
3134 | (void) boot_entries_augment_from_loader(&config, m->efi_boot_loader_entries, true); | |
31b221cf LP |
3135 | |
3136 | r = sd_bus_message_open_container(reply, 'a', "s"); | |
3137 | if (r < 0) | |
3138 | return r; | |
3139 | ||
3140 | for (i = 0; i < config.n_entries; i++) { | |
3141 | BootEntry *e = config.entries + i; | |
3142 | ||
3143 | r = sd_bus_message_append(reply, "s", e->id); | |
3144 | if (r < 0) | |
3145 | return r; | |
3146 | } | |
3147 | ||
3148 | return sd_bus_message_close_container(reply); | |
3149 | } | |
3150 | ||
9ef15026 JS |
3151 | static int method_set_wall_message( |
3152 | sd_bus_message *message, | |
3153 | void *userdata, | |
3154 | sd_bus_error *error) { | |
3155 | ||
3156 | int r; | |
3157 | Manager *m = userdata; | |
3158 | char *wall_message; | |
c9482b88 | 3159 | unsigned enable_wall_messages; |
9ef15026 JS |
3160 | |
3161 | assert(message); | |
3162 | assert(m); | |
3163 | ||
3164 | r = sd_bus_message_read(message, "sb", &wall_message, &enable_wall_messages); | |
3165 | if (r < 0) | |
3166 | return r; | |
3167 | ||
5644d47b ZJS |
3168 | /* Short-circuit the operation if the desired state is already in place, to |
3169 | * avoid an unnecessary polkit permission check. */ | |
3170 | if (streq_ptr(m->wall_message, empty_to_null(wall_message)) && | |
3171 | m->enable_wall_messages == enable_wall_messages) | |
3172 | goto done; | |
3173 | ||
9ef15026 JS |
3174 | r = bus_verify_polkit_async(message, |
3175 | CAP_SYS_ADMIN, | |
3176 | "org.freedesktop.login1.set-wall-message", | |
403ed0e5 | 3177 | NULL, |
9ef15026 JS |
3178 | false, |
3179 | UID_INVALID, | |
3180 | &m->polkit_registry, | |
3181 | error); | |
9ef15026 JS |
3182 | if (r < 0) |
3183 | return r; | |
3184 | if (r == 0) | |
3185 | return 1; /* Will call us back */ | |
3186 | ||
28a9ec44 ZJS |
3187 | r = free_and_strdup(&m->wall_message, empty_to_null(wall_message)); |
3188 | if (r < 0) | |
3189 | return log_oom(); | |
5744f59a | 3190 | |
9ef15026 JS |
3191 | m->enable_wall_messages = enable_wall_messages; |
3192 | ||
5644d47b | 3193 | done: |
9ef15026 JS |
3194 | return sd_bus_reply_method_return(message, NULL); |
3195 | } | |
3196 | ||
19070062 | 3197 | static int method_inhibit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 3198 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
cc377381 LP |
3199 | const char *who, *why, *what, *mode; |
3200 | _cleanup_free_ char *id = NULL; | |
3201 | _cleanup_close_ int fifo_fd = -1; | |
3202 | Manager *m = userdata; | |
3203 | Inhibitor *i = NULL; | |
3204 | InhibitMode mm; | |
3205 | InhibitWhat w; | |
3206 | pid_t pid; | |
3207 | uid_t uid; | |
3208 | int r; | |
7f7bb946 | 3209 | |
cc377381 LP |
3210 | assert(message); |
3211 | assert(m); | |
38f3fc7d | 3212 | |
cc377381 LP |
3213 | r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode); |
3214 | if (r < 0) | |
ebcf1f97 | 3215 | return r; |
38f3fc7d | 3216 | |
cc377381 LP |
3217 | w = inhibit_what_from_string(what); |
3218 | if (w <= 0) | |
124d7cb2 ZJS |
3219 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
3220 | "Invalid what specification %s", what); | |
38f3fc7d | 3221 | |
cc377381 LP |
3222 | mm = inhibit_mode_from_string(mode); |
3223 | if (mm < 0) | |
124d7cb2 ZJS |
3224 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
3225 | "Invalid mode specification %s", mode); | |
7f7bb946 | 3226 | |
cc377381 LP |
3227 | /* Delay is only supported for shutdown/sleep */ |
3228 | if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP))) | |
124d7cb2 ZJS |
3229 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, |
3230 | "Delay inhibitors only supported for shutdown and sleep"); | |
38f3fc7d | 3231 | |
cc377381 LP |
3232 | /* Don't allow taking delay locks while we are already |
3233 | * executing the operation. We shouldn't create the impression | |
3234 | * that the lock was successful if the machine is about to go | |
3235 | * down/suspend any moment. */ | |
3236 | if (m->action_what & w) | |
124d7cb2 ZJS |
3237 | return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, |
3238 | "The operation inhibition has been requested for is already running"); | |
cc377381 | 3239 | |
c529695e LP |
3240 | r = bus_verify_polkit_async( |
3241 | message, | |
3242 | CAP_SYS_BOOT, | |
3243 | w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") : | |
3244 | w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") : | |
3245 | w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" : | |
3246 | w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" : | |
3247 | w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" : | |
3248 | w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" : | |
3249 | "org.freedesktop.login1.inhibit-handle-lid-switch", | |
403ed0e5 | 3250 | NULL, |
c529695e LP |
3251 | false, |
3252 | UID_INVALID, | |
3253 | &m->polkit_registry, | |
3254 | error); | |
cc377381 | 3255 | if (r < 0) |
ebcf1f97 | 3256 | return r; |
cc377381 LP |
3257 | if (r == 0) |
3258 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
7f7bb946 | 3259 | |
05bae4a6 | 3260 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID, &creds); |
5b12334d LP |
3261 | if (r < 0) |
3262 | return r; | |
3263 | ||
05bae4a6 | 3264 | r = sd_bus_creds_get_euid(creds, &uid); |
cc377381 | 3265 | if (r < 0) |
ebcf1f97 | 3266 | return r; |
7f7bb946 | 3267 | |
5b12334d | 3268 | r = sd_bus_creds_get_pid(creds, &pid); |
cc377381 | 3269 | if (r < 0) |
ebcf1f97 | 3270 | return r; |
47a26690 | 3271 | |
c5a11ae2 | 3272 | if (hashmap_size(m->inhibitors) >= m->inhibitors_max) |
124d7cb2 ZJS |
3273 | return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, |
3274 | "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", | |
3275 | m->inhibitors_max); | |
c5a11ae2 | 3276 | |
cc377381 | 3277 | do { |
97b11eed | 3278 | id = mfree(id); |
47a26690 | 3279 | |
cc377381 | 3280 | if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0) |
ebcf1f97 | 3281 | return -ENOMEM; |
47a26690 | 3282 | |
cc377381 | 3283 | } while (hashmap_get(m->inhibitors, id)); |
47a26690 | 3284 | |
cc377381 LP |
3285 | r = manager_add_inhibitor(m, id, &i); |
3286 | if (r < 0) | |
ebcf1f97 | 3287 | return r; |
47a26690 | 3288 | |
cc377381 LP |
3289 | i->what = w; |
3290 | i->mode = mm; | |
3291 | i->pid = pid; | |
3292 | i->uid = uid; | |
3293 | i->why = strdup(why); | |
3294 | i->who = strdup(who); | |
7f7bb946 | 3295 | |
cc377381 | 3296 | if (!i->why || !i->who) { |
ebcf1f97 | 3297 | r = -ENOMEM; |
cc377381 LP |
3298 | goto fail; |
3299 | } | |
b668e064 | 3300 | |
cc377381 LP |
3301 | fifo_fd = inhibitor_create_fifo(i); |
3302 | if (fifo_fd < 0) { | |
ebcf1f97 | 3303 | r = fifo_fd; |
cc377381 LP |
3304 | goto fail; |
3305 | } | |
b668e064 | 3306 | |
2b695039 LP |
3307 | r = inhibitor_start(i); |
3308 | if (r < 0) | |
3309 | goto fail; | |
b668e064 | 3310 | |
df2d202e | 3311 | return sd_bus_reply_method_return(message, "h", fifo_fd); |
b668e064 | 3312 | |
cc377381 LP |
3313 | fail: |
3314 | if (i) | |
3315 | inhibitor_free(i); | |
89f13440 | 3316 | |
cc377381 LP |
3317 | return r; |
3318 | } | |
3f49d45a | 3319 | |
c2b178d3 | 3320 | static const sd_bus_vtable manager_vtable[] = { |
cc377381 LP |
3321 | SD_BUS_VTABLE_START(0), |
3322 | ||
e2fa5721 DM |
3323 | SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0), |
3324 | SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0), | |
3325 | ||
556089dc LP |
3326 | SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST), |
3327 | SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST), | |
3328 | SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST), | |
3329 | SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST), | |
428b296a | 3330 | SD_BUS_PROPERTY("RebootParameter", "s", property_get_reboot_parameter, 0, 0), |
c30e0d7b | 3331 | SD_BUS_PROPERTY("RebootToFirmwareSetup", "b", property_get_reboot_to_firmware_setup, 0, 0), |
31b221cf LP |
3332 | SD_BUS_PROPERTY("RebootToBootLoaderMenu", "t", property_get_reboot_to_boot_loader_menu, 0, 0), |
3333 | SD_BUS_PROPERTY("RebootToBootLoaderEntry", "s", property_get_reboot_to_boot_loader_entry, 0, 0), | |
3334 | SD_BUS_PROPERTY("BootLoaderEntries", "as", property_get_boot_loader_entries, 0, SD_BUS_VTABLE_PROPERTY_CONST), | |
cc377381 LP |
3335 | SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
3336 | SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
3337 | SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
3338 | SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
3339 | SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
556089dc | 3340 | SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST), |
9afe9efb | 3341 | SD_BUS_PROPERTY("UserStopDelayUSec", "t", NULL, offsetof(Manager, user_stop_delay), SD_BUS_VTABLE_PROPERTY_CONST), |
556089dc LP |
3342 | SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST), |
3343 | SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST), | |
3344 | SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST), | |
3345 | SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST), | |
e25937a3 | 3346 | SD_BUS_PROPERTY("HandleLidSwitchExternalPower", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_ep), SD_BUS_VTABLE_PROPERTY_CONST), |
3c56cab4 | 3347 | SD_BUS_PROPERTY("HandleLidSwitchDocked", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch_docked), SD_BUS_VTABLE_PROPERTY_CONST), |
9d10cbee | 3348 | SD_BUS_PROPERTY("HoldoffTimeoutUSec", "t", NULL, offsetof(Manager, holdoff_timeout_usec), SD_BUS_VTABLE_PROPERTY_CONST), |
556089dc LP |
3349 | SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST), |
3350 | SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST), | |
cc377381 LP |
3351 | SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0), |
3352 | SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0), | |
8aaa023a | 3353 | SD_BUS_PROPERTY("ScheduledShutdown", "(st)", property_get_scheduled_shutdown, 0, 0), |
14856079 | 3354 | SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0), |
9b9c23da | 3355 | SD_BUS_PROPERTY("LidClosed", "b", property_get_lid_closed, 0, 0), |
4e96eb68 | 3356 | SD_BUS_PROPERTY("OnExternalPower", "b", property_get_on_external_power, 0, 0), |
6d97d3c6 | 3357 | SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST), |
a7b46b7d | 3358 | SD_BUS_PROPERTY("RuntimeDirectorySize", "t", NULL, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST), |
cc1c85fb | 3359 | SD_BUS_PROPERTY("RuntimeDirectoryInodesMax", "t", NULL, offsetof(Manager, runtime_dir_inodes), SD_BUS_VTABLE_PROPERTY_CONST), |
c5a11ae2 | 3360 | SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST), |
01adcd69 | 3361 | SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_hashmap_size, offsetof(Manager, inhibitors), 0), |
183e0738 | 3362 | SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST), |
01adcd69 | 3363 | SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_hashmap_size, offsetof(Manager, sessions), 0), |
28414939 | 3364 | SD_BUS_PROPERTY("UserTasksMax", "t", property_get_compat_user_tasks_max, 0, SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_HIDDEN), |
cc377381 | 3365 | |
4cb5fd0d ZJS |
3366 | SD_BUS_METHOD_WITH_NAMES("GetSession", |
3367 | "s", | |
3368 | SD_BUS_PARAM(session_id), | |
3369 | "o", | |
3370 | SD_BUS_PARAM(object_path), | |
3371 | method_get_session, | |
3372 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3373 | SD_BUS_METHOD_WITH_NAMES("GetSessionByPID", | |
3374 | "u", | |
3375 | SD_BUS_PARAM(pid), | |
3376 | "o", | |
3377 | SD_BUS_PARAM(object_path), | |
3378 | method_get_session_by_pid, | |
3379 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3380 | SD_BUS_METHOD_WITH_NAMES("GetUser", | |
3381 | "u", | |
3382 | SD_BUS_PARAM(uid), | |
3383 | "o", | |
3384 | SD_BUS_PARAM(object_path), | |
3385 | method_get_user, | |
3386 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3387 | SD_BUS_METHOD_WITH_NAMES("GetUserByPID", | |
3388 | "u", | |
3389 | SD_BUS_PARAM(pid), | |
3390 | "o", | |
3391 | SD_BUS_PARAM(object_path), | |
3392 | method_get_user_by_pid, | |
3393 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3394 | SD_BUS_METHOD_WITH_NAMES("GetSeat", | |
3395 | "s", | |
3396 | SD_BUS_PARAM(seat_id), | |
3397 | "o", | |
3398 | SD_BUS_PARAM(object_path), | |
3399 | method_get_seat, | |
3400 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3401 | SD_BUS_METHOD_WITH_NAMES("ListSessions", | |
3402 | NULL,, | |
3403 | "a(susso)", | |
3404 | SD_BUS_PARAM(sessions), | |
3405 | method_list_sessions, | |
3406 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3407 | SD_BUS_METHOD_WITH_NAMES("ListUsers", | |
3408 | NULL,, | |
3409 | "a(uso)", | |
3410 | SD_BUS_PARAM(users), | |
3411 | method_list_users, | |
3412 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3413 | SD_BUS_METHOD_WITH_NAMES("ListSeats", | |
3414 | NULL,, | |
3415 | "a(so)", | |
3416 | SD_BUS_PARAM(seats), | |
3417 | method_list_seats, | |
3418 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3419 | SD_BUS_METHOD_WITH_NAMES("ListInhibitors", | |
3420 | NULL,, | |
3421 | "a(ssssuu)", | |
3422 | SD_BUS_PARAM(inhibitors), | |
3423 | method_list_inhibitors, | |
3424 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3425 | SD_BUS_METHOD_WITH_NAMES("CreateSession", | |
3426 | "uusssssussbssa(sv)", | |
3427 | SD_BUS_PARAM(uid) | |
3428 | SD_BUS_PARAM(pid) | |
3429 | SD_BUS_PARAM(service) | |
3430 | SD_BUS_PARAM(type) | |
3431 | SD_BUS_PARAM(class) | |
3432 | SD_BUS_PARAM(desktop) | |
3433 | SD_BUS_PARAM(seat_id) | |
3434 | SD_BUS_PARAM(vtnr) | |
3435 | SD_BUS_PARAM(tty) | |
3436 | SD_BUS_PARAM(display) | |
3437 | SD_BUS_PARAM(remote) | |
3438 | SD_BUS_PARAM(remote_user) | |
3439 | SD_BUS_PARAM(remote_host) | |
3440 | SD_BUS_PARAM(properties), | |
3441 | "soshusub", | |
3442 | SD_BUS_PARAM(session_id) | |
3443 | SD_BUS_PARAM(object_path) | |
3444 | SD_BUS_PARAM(runtime_path) | |
3445 | SD_BUS_PARAM(fifo_fd) | |
3446 | SD_BUS_PARAM(uid) | |
3447 | SD_BUS_PARAM(seat_id) | |
3448 | SD_BUS_PARAM(vtnr) | |
3449 | SD_BUS_PARAM(existing), | |
3450 | method_create_session, | |
3451 | 0), | |
3452 | SD_BUS_METHOD_WITH_NAMES("ReleaseSession", | |
3453 | "s", | |
3454 | SD_BUS_PARAM(session_id), | |
3455 | NULL,, | |
3456 | method_release_session, | |
3457 | 0), | |
3458 | SD_BUS_METHOD_WITH_NAMES("ActivateSession", | |
3459 | "s", | |
3460 | SD_BUS_PARAM(session_id), | |
3461 | NULL,, | |
3462 | method_activate_session, | |
3463 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3464 | SD_BUS_METHOD_WITH_NAMES("ActivateSessionOnSeat", | |
3465 | "ss", | |
3466 | SD_BUS_PARAM(session_id) | |
3467 | SD_BUS_PARAM(seat_id), | |
3468 | NULL,, | |
3469 | method_activate_session_on_seat, | |
3470 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3471 | SD_BUS_METHOD_WITH_NAMES("LockSession", | |
3472 | "s", | |
3473 | SD_BUS_PARAM(session_id), | |
3474 | NULL,, | |
3475 | method_lock_session, | |
3476 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3477 | SD_BUS_METHOD_WITH_NAMES("UnlockSession", | |
3478 | "s", | |
3479 | SD_BUS_PARAM(session_id), | |
3480 | NULL,, | |
3481 | method_lock_session, | |
3482 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3483 | SD_BUS_METHOD("LockSessions", | |
3484 | NULL, | |
3485 | NULL, | |
3486 | method_lock_sessions, | |
3487 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3488 | SD_BUS_METHOD("UnlockSessions", | |
3489 | NULL, | |
3490 | NULL, | |
3491 | method_lock_sessions, | |
3492 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3493 | SD_BUS_METHOD_WITH_NAMES("KillSession", | |
3494 | "ssi", | |
3495 | SD_BUS_PARAM(session_id) | |
3496 | SD_BUS_PARAM(who) | |
3497 | SD_BUS_PARAM(signal_number), | |
3498 | NULL,, | |
3499 | method_kill_session, | |
3500 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3501 | SD_BUS_METHOD_WITH_NAMES("KillUser", | |
3502 | "ui", | |
3503 | SD_BUS_PARAM(uid) | |
3504 | SD_BUS_PARAM(signal_number), | |
3505 | NULL,, | |
3506 | method_kill_user, | |
3507 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3508 | SD_BUS_METHOD_WITH_NAMES("TerminateSession", | |
3509 | "s", | |
3510 | SD_BUS_PARAM(session_id), | |
3511 | NULL,, | |
3512 | method_terminate_session, | |
3513 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3514 | SD_BUS_METHOD_WITH_NAMES("TerminateUser", | |
3515 | "u", | |
3516 | SD_BUS_PARAM(uid), | |
3517 | NULL,, | |
3518 | method_terminate_user, | |
3519 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3520 | SD_BUS_METHOD_WITH_NAMES("TerminateSeat", | |
3521 | "s", | |
3522 | SD_BUS_PARAM(seat_id), | |
3523 | NULL,, | |
3524 | method_terminate_seat, | |
3525 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3526 | SD_BUS_METHOD_WITH_NAMES("SetUserLinger", | |
3527 | "ubb", | |
3528 | SD_BUS_PARAM(uid) | |
3529 | SD_BUS_PARAM(enable) | |
3530 | SD_BUS_PARAM(interactive), | |
3531 | NULL,, | |
3532 | method_set_user_linger, | |
3533 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3534 | SD_BUS_METHOD_WITH_NAMES("AttachDevice", | |
3535 | "ssb", | |
3536 | SD_BUS_PARAM(seat_id) | |
3537 | SD_BUS_PARAM(sysfs_path) | |
3538 | SD_BUS_PARAM(interactive), | |
3539 | NULL,, | |
3540 | method_attach_device, | |
3541 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3542 | SD_BUS_METHOD_WITH_NAMES("FlushDevices", | |
3543 | "b", | |
3544 | SD_BUS_PARAM(interactive), | |
3545 | NULL,, | |
3546 | method_flush_devices, | |
3547 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3548 | SD_BUS_METHOD_WITH_NAMES("PowerOff", | |
3549 | "b", | |
3550 | SD_BUS_PARAM(interactive), | |
3551 | NULL,, | |
3552 | method_poweroff, | |
3553 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3554 | SD_BUS_METHOD_WITH_NAMES("Reboot", | |
3555 | "b", | |
3556 | SD_BUS_PARAM(interactive), | |
3557 | NULL,, | |
3558 | method_reboot, | |
3559 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3560 | SD_BUS_METHOD_WITH_NAMES("Halt", | |
3561 | "b", | |
3562 | SD_BUS_PARAM(interactive), | |
3563 | NULL,, | |
3564 | method_halt, | |
3565 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3566 | SD_BUS_METHOD_WITH_NAMES("Suspend", | |
3567 | "b", | |
3568 | SD_BUS_PARAM(interactive), | |
3569 | NULL,, | |
3570 | method_suspend, | |
3571 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3572 | SD_BUS_METHOD_WITH_NAMES("Hibernate", | |
3573 | "b", | |
3574 | SD_BUS_PARAM(interactive), | |
3575 | NULL,, | |
3576 | method_hibernate, | |
3577 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3578 | SD_BUS_METHOD_WITH_NAMES("HybridSleep", | |
3579 | "b", | |
3580 | SD_BUS_PARAM(interactive), | |
3581 | NULL,, | |
3582 | method_hybrid_sleep, | |
3583 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3584 | SD_BUS_METHOD_WITH_NAMES("SuspendThenHibernate", | |
3585 | "b", | |
3586 | SD_BUS_PARAM(interactive), | |
3587 | NULL,, | |
3588 | method_suspend_then_hibernate, | |
3589 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3590 | SD_BUS_METHOD_WITH_NAMES("CanPowerOff", | |
3591 | NULL,, | |
3592 | "s", | |
3593 | SD_BUS_PARAM(result), | |
3594 | method_can_poweroff, | |
3595 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3596 | SD_BUS_METHOD_WITH_NAMES("CanReboot", | |
3597 | NULL,, | |
3598 | "s", | |
3599 | SD_BUS_PARAM(result), | |
3600 | method_can_reboot, | |
3601 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3602 | SD_BUS_METHOD_WITH_NAMES("CanHalt", | |
3603 | NULL,, | |
3604 | "s", | |
3605 | SD_BUS_PARAM(result), | |
3606 | method_can_halt, | |
3607 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3608 | SD_BUS_METHOD_WITH_NAMES("CanSuspend", | |
3609 | NULL,, | |
3610 | "s", | |
3611 | SD_BUS_PARAM(result), | |
3612 | method_can_suspend, | |
3613 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3614 | SD_BUS_METHOD_WITH_NAMES("CanHibernate", | |
3615 | NULL,, | |
3616 | "s", | |
3617 | SD_BUS_PARAM(result), | |
3618 | method_can_hibernate, | |
3619 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3620 | SD_BUS_METHOD_WITH_NAMES("CanHybridSleep", | |
3621 | NULL,, | |
3622 | "s", | |
3623 | SD_BUS_PARAM(result), | |
3624 | method_can_hybrid_sleep, | |
3625 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3626 | SD_BUS_METHOD_WITH_NAMES("CanSuspendThenHibernate", | |
3627 | NULL,, | |
3628 | "s", | |
3629 | SD_BUS_PARAM(result), | |
3630 | method_can_suspend_then_hibernate, | |
3631 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3632 | SD_BUS_METHOD_WITH_NAMES("ScheduleShutdown", | |
3633 | "st", | |
3634 | SD_BUS_PARAM(type) | |
3635 | SD_BUS_PARAM(usec), | |
3636 | NULL,, | |
3637 | method_schedule_shutdown, | |
3638 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3639 | SD_BUS_METHOD_WITH_NAMES("CancelScheduledShutdown", | |
3640 | NULL,, | |
3641 | "b", | |
3642 | SD_BUS_PARAM(cancelled), | |
3643 | method_cancel_scheduled_shutdown, | |
3644 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3645 | SD_BUS_METHOD_WITH_NAMES("Inhibit", | |
3646 | "ssss", | |
3647 | SD_BUS_PARAM(what) | |
3648 | SD_BUS_PARAM(who) | |
3649 | SD_BUS_PARAM(why) | |
3650 | SD_BUS_PARAM(mode), | |
3651 | "h", | |
3652 | SD_BUS_PARAM(pipe_fd), | |
3653 | method_inhibit, | |
3654 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3655 | SD_BUS_METHOD_WITH_NAMES("CanRebootParameter", | |
3656 | NULL,, | |
3657 | "s", | |
3658 | SD_BUS_PARAM(result), | |
3659 | method_can_reboot_parameter, | |
3660 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3661 | SD_BUS_METHOD_WITH_NAMES("SetRebootParameter", | |
3662 | "s", | |
3663 | SD_BUS_PARAM(parameter), | |
3664 | NULL,, | |
3665 | method_set_reboot_parameter, | |
3666 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3667 | SD_BUS_METHOD_WITH_NAMES("CanRebootToFirmwareSetup", | |
3668 | NULL,, | |
3669 | "s", | |
3670 | SD_BUS_PARAM(result), | |
3671 | method_can_reboot_to_firmware_setup, | |
3672 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3673 | SD_BUS_METHOD_WITH_NAMES("SetRebootToFirmwareSetup", | |
3674 | "b", | |
3675 | SD_BUS_PARAM(enable), | |
3676 | NULL,, | |
3677 | method_set_reboot_to_firmware_setup, | |
3678 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3679 | SD_BUS_METHOD_WITH_NAMES("CanRebootToBootLoaderMenu", | |
3680 | NULL,, | |
3681 | "s", | |
3682 | SD_BUS_PARAM(result), | |
3683 | method_can_reboot_to_boot_loader_menu, | |
3684 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3685 | SD_BUS_METHOD_WITH_NAMES("SetRebootToBootLoaderMenu", | |
3686 | "t", | |
3687 | SD_BUS_PARAM(timeout), | |
3688 | NULL,, | |
3689 | method_set_reboot_to_boot_loader_menu, | |
3690 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3691 | SD_BUS_METHOD_WITH_NAMES("CanRebootToBootLoaderEntry", | |
3692 | NULL,, | |
3693 | "s", | |
3694 | SD_BUS_PARAM(result), | |
3695 | method_can_reboot_to_boot_loader_entry, | |
3696 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3697 | SD_BUS_METHOD_WITH_NAMES("SetRebootToBootLoaderEntry", | |
3698 | "s", | |
3699 | SD_BUS_PARAM(boot_loader_entry), | |
3700 | NULL,, | |
3701 | method_set_reboot_to_boot_loader_entry, | |
3702 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3703 | SD_BUS_METHOD_WITH_NAMES("SetWallMessage", | |
3704 | "sb", | |
3705 | SD_BUS_PARAM(wall_message) | |
3706 | SD_BUS_PARAM(enable), | |
3707 | NULL,, | |
3708 | method_set_wall_message, | |
3709 | SD_BUS_VTABLE_UNPRIVILEGED), | |
3710 | ||
3711 | SD_BUS_SIGNAL_WITH_NAMES("SessionNew", | |
3712 | "so", | |
3713 | SD_BUS_PARAM(session_id) | |
3714 | SD_BUS_PARAM(object_path), | |
3715 | 0), | |
3716 | SD_BUS_SIGNAL_WITH_NAMES("SessionRemoved", | |
3717 | "so", | |
3718 | SD_BUS_PARAM(session_id) | |
3719 | SD_BUS_PARAM(object_path), | |
3720 | 0), | |
3721 | SD_BUS_SIGNAL_WITH_NAMES("UserNew", | |
3722 | "uo", | |
3723 | SD_BUS_PARAM(uid) | |
3724 | SD_BUS_PARAM(object_path), | |
3725 | 0), | |
3726 | SD_BUS_SIGNAL_WITH_NAMES("UserRemoved", | |
3727 | "uo", | |
3728 | SD_BUS_PARAM(uid) | |
3729 | SD_BUS_PARAM(object_path), | |
3730 | 0), | |
3731 | SD_BUS_SIGNAL_WITH_NAMES("SeatNew", | |
3732 | "so", | |
3733 | SD_BUS_PARAM(seat_id) | |
3734 | SD_BUS_PARAM(object_path), | |
3735 | 0), | |
3736 | SD_BUS_SIGNAL_WITH_NAMES("SeatRemoved", | |
3737 | "so", | |
3738 | SD_BUS_PARAM(seat_id) | |
3739 | SD_BUS_PARAM(object_path), | |
3740 | 0), | |
3741 | SD_BUS_SIGNAL_WITH_NAMES("PrepareForShutdown", | |
3742 | "b", | |
3743 | SD_BUS_PARAM(start), | |
3744 | 0), | |
3745 | SD_BUS_SIGNAL_WITH_NAMES("PrepareForSleep", | |
3746 | "b", | |
3747 | SD_BUS_PARAM(start), | |
3748 | 0), | |
cc377381 LP |
3749 | |
3750 | SD_BUS_VTABLE_END | |
3751 | }; | |
3f49d45a | 3752 | |
c2b178d3 ZJS |
3753 | const BusObjectImplementation manager_object = { |
3754 | "/org/freedesktop/login1", | |
3755 | "org.freedesktop.login1.Manager", | |
3756 | .vtables = BUS_VTABLES(manager_vtable), | |
3757 | .children = BUS_IMPLEMENTATIONS(&seat_object, | |
3758 | &session_object, | |
3759 | &user_object), | |
3760 | }; | |
3761 | ||
b39648ed | 3762 | static int session_jobs_reply(Session *s, uint32_t jid, const char *unit, const char *result) { |
99e7e392 DH |
3763 | assert(s); |
3764 | assert(unit); | |
3765 | ||
3766 | if (!s->started) | |
25a1ab4e | 3767 | return 0; |
99e7e392 | 3768 | |
25a1ab4e | 3769 | if (result && !streq(result, "done")) { |
4afd3348 | 3770 | _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; |
99e7e392 | 3771 | |
124d7cb2 | 3772 | sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, |
b39648ed | 3773 | "Job %u for unit '%s' failed with '%s'", jid, unit, result); |
25a1ab4e | 3774 | return session_send_create_reply(s, &e); |
99e7e392 DH |
3775 | } |
3776 | ||
25a1ab4e | 3777 | return session_send_create_reply(s, NULL); |
99e7e392 DH |
3778 | } |
3779 | ||
19070062 | 3780 | int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
3781 | const char *path, *result, *unit; |
3782 | Manager *m = userdata; | |
3783 | Session *session; | |
3784 | uint32_t id; | |
3785 | User *user; | |
3786 | int r; | |
3f49d45a | 3787 | |
cc377381 LP |
3788 | assert(message); |
3789 | assert(m); | |
3f49d45a | 3790 | |
cc377381 LP |
3791 | r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result); |
3792 | if (r < 0) { | |
ebcf1f97 | 3793 | bus_log_parse_error(r); |
65d73cf0 | 3794 | return 0; |
cc377381 | 3795 | } |
3f49d45a | 3796 | |
cc377381 | 3797 | if (m->action_job && streq(m->action_job, path)) { |
af4efb51 | 3798 | log_info("Operation '%s' finished.", inhibit_what_to_string(m->action_what)); |
3f49d45a | 3799 | |
cc377381 | 3800 | /* Tell people that they now may take a lock again */ |
401e33ed | 3801 | (void) send_prepare_for(m, m->action_what, false); |
3f49d45a | 3802 | |
491ac9f2 | 3803 | m->action_job = mfree(m->action_job); |
cc377381 LP |
3804 | m->action_unit = NULL; |
3805 | m->action_what = 0; | |
3806 | return 0; | |
3807 | } | |
3f49d45a | 3808 | |
cc377381 | 3809 | session = hashmap_get(m->session_units, unit); |
25a1ab4e LP |
3810 | if (session) { |
3811 | if (streq_ptr(path, session->scope_job)) { | |
3812 | session->scope_job = mfree(session->scope_job); | |
b39648ed | 3813 | (void) session_jobs_reply(session, id, unit, result); |
25a1ab4e LP |
3814 | |
3815 | session_save(session); | |
3816 | user_save(session->user); | |
3817 | } | |
3f49d45a | 3818 | |
cc377381 LP |
3819 | session_add_to_gc_queue(session); |
3820 | } | |
3f49d45a | 3821 | |
cc377381 | 3822 | user = hashmap_get(m->user_units, unit); |
25a1ab4e LP |
3823 | if (user) { |
3824 | if (streq_ptr(path, user->service_job)) { | |
491ac9f2 | 3825 | user->service_job = mfree(user->service_job); |
3f49d45a | 3826 | |
25a1ab4e | 3827 | LIST_FOREACH(sessions_by_user, session, user->sessions) |
b39648ed | 3828 | (void) session_jobs_reply(session, id, unit, NULL /* don't propagate user service failures to the client */); |
3f49d45a | 3829 | |
25a1ab4e LP |
3830 | user_save(user); |
3831 | } | |
dd9b67aa | 3832 | |
cc377381 | 3833 | user_add_to_gc_queue(user); |
3f49d45a LP |
3834 | } |
3835 | ||
cc377381 | 3836 | return 0; |
3f49d45a LP |
3837 | } |
3838 | ||
19070062 | 3839 | int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 3840 | const char *path, *unit; |
1713813d | 3841 | Manager *m = userdata; |
cc377381 LP |
3842 | Session *session; |
3843 | User *user; | |
3844 | int r; | |
1713813d | 3845 | |
1713813d | 3846 | assert(message); |
cc377381 | 3847 | assert(m); |
1713813d | 3848 | |
cc377381 LP |
3849 | r = sd_bus_message_read(message, "so", &unit, &path); |
3850 | if (r < 0) { | |
ebcf1f97 | 3851 | bus_log_parse_error(r); |
65d73cf0 | 3852 | return 0; |
cc377381 | 3853 | } |
fb6becb4 | 3854 | |
cc377381 LP |
3855 | session = hashmap_get(m->session_units, unit); |
3856 | if (session) | |
3857 | session_add_to_gc_queue(session); | |
fb6becb4 | 3858 | |
cc377381 LP |
3859 | user = hashmap_get(m->user_units, unit); |
3860 | if (user) | |
3861 | user_add_to_gc_queue(user); | |
fb6becb4 | 3862 | |
cc377381 LP |
3863 | return 0; |
3864 | } | |
fb6becb4 | 3865 | |
19070062 | 3866 | int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
3867 | _cleanup_free_ char *unit = NULL; |
3868 | Manager *m = userdata; | |
3869 | const char *path; | |
3870 | Session *session; | |
3871 | User *user; | |
ebcf1f97 | 3872 | int r; |
fb6becb4 | 3873 | |
cc377381 LP |
3874 | assert(message); |
3875 | assert(m); | |
fb6becb4 | 3876 | |
cc377381 LP |
3877 | path = sd_bus_message_get_path(message); |
3878 | if (!path) | |
3879 | return 0; | |
fb6becb4 | 3880 | |
ebcf1f97 | 3881 | r = unit_name_from_dbus_path(path, &unit); |
e5f5b5b9 LP |
3882 | if (r == -EINVAL) /* not a unit */ |
3883 | return 0; | |
65d73cf0 LP |
3884 | if (r < 0) { |
3885 | log_oom(); | |
3886 | return 0; | |
3887 | } | |
fb6becb4 | 3888 | |
cc377381 LP |
3889 | session = hashmap_get(m->session_units, unit); |
3890 | if (session) | |
3891 | session_add_to_gc_queue(session); | |
fb6becb4 | 3892 | |
cc377381 LP |
3893 | user = hashmap_get(m->user_units, unit); |
3894 | if (user) | |
3895 | user_add_to_gc_queue(user); | |
fb6becb4 | 3896 | |
cc377381 LP |
3897 | return 0; |
3898 | } | |
6fa48533 | 3899 | |
19070062 | 3900 | int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
3901 | Manager *m = userdata; |
3902 | Session *session; | |
3903 | Iterator i; | |
3904 | int b, r; | |
943aca8e | 3905 | |
19070062 LP |
3906 | assert(message); |
3907 | assert(m); | |
943aca8e | 3908 | |
cc377381 LP |
3909 | r = sd_bus_message_read(message, "b", &b); |
3910 | if (r < 0) { | |
ebcf1f97 | 3911 | bus_log_parse_error(r); |
65d73cf0 | 3912 | return 0; |
cc377381 | 3913 | } |
943aca8e | 3914 | |
cc377381 LP |
3915 | if (b) |
3916 | return 0; | |
943aca8e | 3917 | |
cc377381 LP |
3918 | /* systemd finished reloading, let's recheck all our sessions */ |
3919 | log_debug("System manager has been reloaded, rechecking sessions..."); | |
6797c324 | 3920 | |
cc377381 LP |
3921 | HASHMAP_FOREACH(session, m->sessions, i) |
3922 | session_add_to_gc_queue(session); | |
6797c324 | 3923 | |
cc377381 LP |
3924 | return 0; |
3925 | } | |
943aca8e | 3926 | |
cc377381 LP |
3927 | int manager_send_changed(Manager *manager, const char *property, ...) { |
3928 | char **l; | |
9418f147 LP |
3929 | |
3930 | assert(manager); | |
3931 | ||
cc377381 | 3932 | l = strv_from_stdarg_alloca(property); |
9418f147 | 3933 | |
cc377381 LP |
3934 | return sd_bus_emit_properties_changed_strv( |
3935 | manager->bus, | |
3936 | "/org/freedesktop/login1", | |
3937 | "org.freedesktop.login1.Manager", | |
3938 | l); | |
9418f147 | 3939 | } |
eecd1362 | 3940 | |
2adae5ac ZJS |
3941 | static int strdup_job(sd_bus_message *reply, char **job) { |
3942 | const char *j; | |
3943 | char *copy; | |
3944 | int r; | |
3945 | ||
3946 | r = sd_bus_message_read(reply, "o", &j); | |
3947 | if (r < 0) | |
3948 | return r; | |
3949 | ||
3950 | copy = strdup(j); | |
3951 | if (!copy) | |
3952 | return -ENOMEM; | |
3953 | ||
3954 | *job = copy; | |
3955 | return 1; | |
3956 | } | |
3957 | ||
fb6becb4 LP |
3958 | int manager_start_scope( |
3959 | Manager *manager, | |
3960 | const char *scope, | |
3961 | pid_t pid, | |
3962 | const char *slice, | |
3963 | const char *description, | |
25a1ab4e LP |
3964 | char **wants, |
3965 | char **after, | |
d5ac9d06 | 3966 | const char *requires_mounts_for, |
22f93314 | 3967 | sd_bus_message *more_properties, |
cc377381 | 3968 | sd_bus_error *error, |
fb6becb4 LP |
3969 | char **job) { |
3970 | ||
4afd3348 | 3971 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
25a1ab4e | 3972 | char **i; |
cc377381 | 3973 | int r; |
fb6becb4 LP |
3974 | |
3975 | assert(manager); | |
3976 | assert(scope); | |
3977 | assert(pid > 1); | |
2adae5ac | 3978 | assert(job); |
fb6becb4 | 3979 | |
5d990cc5 | 3980 | r = bus_message_new_method_call(manager->bus, &m, bus_systemd_mgr, "StartTransientUnit"); |
cc377381 LP |
3981 | if (r < 0) |
3982 | return r; | |
fb6becb4 | 3983 | |
cc377381 LP |
3984 | r = sd_bus_message_append(m, "ss", strempty(scope), "fail"); |
3985 | if (r < 0) | |
3986 | return r; | |
fb6becb4 | 3987 | |
cc377381 LP |
3988 | r = sd_bus_message_open_container(m, 'a', "(sv)"); |
3989 | if (r < 0) | |
3990 | return r; | |
fb6becb4 LP |
3991 | |
3992 | if (!isempty(slice)) { | |
cc377381 LP |
3993 | r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice); |
3994 | if (r < 0) | |
3995 | return r; | |
fb6becb4 LP |
3996 | } |
3997 | ||
3998 | if (!isempty(description)) { | |
cc377381 LP |
3999 | r = sd_bus_message_append(m, "(sv)", "Description", "s", description); |
4000 | if (r < 0) | |
4001 | return r; | |
fb6becb4 LP |
4002 | } |
4003 | ||
25a1ab4e LP |
4004 | STRV_FOREACH(i, wants) { |
4005 | r = sd_bus_message_append(m, "(sv)", "Wants", "as", 1, *i); | |
cc377381 LP |
4006 | if (r < 0) |
4007 | return r; | |
7fb3ee51 LP |
4008 | } |
4009 | ||
25a1ab4e LP |
4010 | STRV_FOREACH(i, after) { |
4011 | r = sd_bus_message_append(m, "(sv)", "After", "as", 1, *i); | |
ba4c5d93 LP |
4012 | if (r < 0) |
4013 | return r; | |
4014 | } | |
4015 | ||
d5ac9d06 LP |
4016 | if (!empty_or_root(requires_mounts_for)) { |
4017 | r = sd_bus_message_append(m, "(sv)", "RequiresMountsFor", "as", 1, requires_mounts_for); | |
4018 | if (r < 0) | |
4019 | return r; | |
4020 | } | |
4021 | ||
e743bca2 LP |
4022 | /* Make sure that the session shells are terminated with SIGHUP since bash and friends tend to ignore |
4023 | * SIGTERM */ | |
cc377381 LP |
4024 | r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true); |
4025 | if (r < 0) | |
4026 | return r; | |
4027 | ||
4028 | r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid); | |
4029 | if (r < 0) | |
4030 | return r; | |
4031 | ||
22f93314 JS |
4032 | /* disable TasksMax= for the session scope, rely on the slice setting for it */ |
4033 | r = sd_bus_message_append(m, "(sv)", "TasksMax", "t", (uint64_t)-1); | |
90558f31 | 4034 | if (r < 0) |
22f93314 JS |
4035 | return bus_log_create_error(r); |
4036 | ||
4037 | if (more_properties) { | |
4038 | /* If TasksMax also appears here, it will overwrite the default value set above */ | |
4039 | r = sd_bus_message_copy(m, more_properties, true); | |
4040 | if (r < 0) | |
4041 | return r; | |
4042 | } | |
90558f31 | 4043 | |
cc377381 LP |
4044 | r = sd_bus_message_close_container(m); |
4045 | if (r < 0) | |
4046 | return r; | |
86b8d289 LP |
4047 | |
4048 | r = sd_bus_message_append(m, "a(sa(sv))", 0); | |
4049 | if (r < 0) | |
4050 | return r; | |
cc377381 | 4051 | |
c49b30a2 | 4052 | r = sd_bus_call(manager->bus, m, 0, error, &reply); |
cc377381 LP |
4053 | if (r < 0) |
4054 | return r; | |
fb6becb4 | 4055 | |
2adae5ac | 4056 | return strdup_job(reply, job); |
fb6becb4 LP |
4057 | } |
4058 | ||
cc377381 | 4059 | int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) { |
4afd3348 | 4060 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
fb6becb4 LP |
4061 | int r; |
4062 | ||
4063 | assert(manager); | |
4064 | assert(unit); | |
2adae5ac | 4065 | assert(job); |
fb6becb4 | 4066 | |
5d990cc5 | 4067 | r = bus_call_method( |
fb6becb4 | 4068 | manager->bus, |
5d990cc5 | 4069 | bus_systemd_mgr, |
fb6becb4 | 4070 | "StartUnit", |
fb6becb4 | 4071 | error, |
cc377381 | 4072 | &reply, |
79ee4ad1 | 4073 | "ss", unit, "replace"); |
cc377381 | 4074 | if (r < 0) |
fb6becb4 | 4075 | return r; |
fb6becb4 | 4076 | |
2adae5ac | 4077 | return strdup_job(reply, job); |
fb6becb4 LP |
4078 | } |
4079 | ||
cc377381 | 4080 | int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) { |
4afd3348 | 4081 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
fb6becb4 LP |
4082 | int r; |
4083 | ||
4084 | assert(manager); | |
4085 | assert(unit); | |
2adae5ac | 4086 | assert(job); |
fb6becb4 | 4087 | |
5d990cc5 | 4088 | r = bus_call_method( |
fb6becb4 | 4089 | manager->bus, |
5d990cc5 | 4090 | bus_systemd_mgr, |
fb6becb4 | 4091 | "StopUnit", |
fb6becb4 | 4092 | error, |
cc377381 LP |
4093 | &reply, |
4094 | "ss", unit, "fail"); | |
fb6becb4 | 4095 | if (r < 0) { |
cc377381 LP |
4096 | if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) || |
4097 | sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) { | |
6797c324 | 4098 | |
2adae5ac | 4099 | *job = NULL; |
cc377381 | 4100 | sd_bus_error_free(error); |
6797c324 LP |
4101 | return 0; |
4102 | } | |
4103 | ||
fb6becb4 LP |
4104 | return r; |
4105 | } | |
4106 | ||
2adae5ac | 4107 | return strdup_job(reply, job); |
fb6becb4 LP |
4108 | } |
4109 | ||
ea3a7cf6 LP |
4110 | int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *ret_error) { |
4111 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
5f41d1f1 LP |
4112 | _cleanup_free_ char *path = NULL; |
4113 | int r; | |
4114 | ||
4115 | assert(manager); | |
4116 | assert(scope); | |
4117 | ||
4118 | path = unit_dbus_path_from_name(scope); | |
4119 | if (!path) | |
4120 | return -ENOMEM; | |
4121 | ||
4122 | r = sd_bus_call_method( | |
4123 | manager->bus, | |
4124 | "org.freedesktop.systemd1", | |
4125 | path, | |
4126 | "org.freedesktop.systemd1.Scope", | |
4127 | "Abandon", | |
ea3a7cf6 | 4128 | &error, |
5f41d1f1 LP |
4129 | NULL, |
4130 | NULL); | |
4131 | if (r < 0) { | |
ea3a7cf6 LP |
4132 | if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) || |
4133 | sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED) || | |
4134 | sd_bus_error_has_name(&error, BUS_ERROR_SCOPE_NOT_RUNNING)) | |
5f41d1f1 | 4135 | return 0; |
5f41d1f1 | 4136 | |
ea3a7cf6 | 4137 | sd_bus_error_move(ret_error, &error); |
5f41d1f1 LP |
4138 | return r; |
4139 | } | |
4140 | ||
4141 | return 1; | |
4142 | } | |
4143 | ||
cc377381 | 4144 | int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) { |
fb6becb4 LP |
4145 | assert(manager); |
4146 | assert(unit); | |
4147 | ||
5d990cc5 | 4148 | return bus_call_method( |
fb6becb4 | 4149 | manager->bus, |
5d990cc5 | 4150 | bus_systemd_mgr, |
fb6becb4 | 4151 | "KillUnit", |
fb6becb4 | 4152 | error, |
cc377381 LP |
4153 | NULL, |
4154 | "ssi", unit, who == KILL_LEADER ? "main" : "all", signo); | |
fb6becb4 LP |
4155 | } |
4156 | ||
bd26aee1 | 4157 | int manager_unit_is_active(Manager *manager, const char *unit, sd_bus_error *ret_error) { |
4afd3348 LP |
4158 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4159 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
fb6becb4 | 4160 | _cleanup_free_ char *path = NULL; |
fb6becb4 | 4161 | const char *state; |
fb6becb4 LP |
4162 | int r; |
4163 | ||
4164 | assert(manager); | |
4165 | assert(unit); | |
4166 | ||
fb6becb4 LP |
4167 | path = unit_dbus_path_from_name(unit); |
4168 | if (!path) | |
4169 | return -ENOMEM; | |
4170 | ||
cc377381 | 4171 | r = sd_bus_get_property( |
fb6becb4 LP |
4172 | manager->bus, |
4173 | "org.freedesktop.systemd1", | |
4174 | path, | |
cc377381 LP |
4175 | "org.freedesktop.systemd1.Unit", |
4176 | "ActiveState", | |
fb6becb4 | 4177 | &error, |
cc377381 LP |
4178 | &reply, |
4179 | "s"); | |
fb6becb4 | 4180 | if (r < 0) { |
ebcf6976 | 4181 | /* systemd might have dropped off momentarily, let's |
cc377381 LP |
4182 | * not make this an error */ |
4183 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) || | |
4184 | sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED)) | |
6797c324 | 4185 | return true; |
6797c324 | 4186 | |
cc377381 LP |
4187 | /* If the unit is already unloaded then it's not |
4188 | * active */ | |
4189 | if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) || | |
4190 | sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED)) | |
6797c324 | 4191 | return false; |
6797c324 | 4192 | |
bd26aee1 | 4193 | sd_bus_error_move(ret_error, &error); |
fb6becb4 LP |
4194 | return r; |
4195 | } | |
4196 | ||
cc377381 LP |
4197 | r = sd_bus_message_read(reply, "s", &state); |
4198 | if (r < 0) | |
bd26aee1 | 4199 | return r; |
fb6becb4 | 4200 | |
bd26aee1 | 4201 | return !STR_IN_SET(state, "inactive", "failed"); |
cc377381 LP |
4202 | } |
4203 | ||
bd26aee1 | 4204 | int manager_job_is_active(Manager *manager, const char *path, sd_bus_error *ret_error) { |
4afd3348 LP |
4205 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
4206 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
cc377381 LP |
4207 | int r; |
4208 | ||
4209 | assert(manager); | |
4210 | assert(path); | |
4211 | ||
4212 | r = sd_bus_get_property( | |
4213 | manager->bus, | |
4214 | "org.freedesktop.systemd1", | |
4215 | path, | |
4216 | "org.freedesktop.systemd1.Job", | |
4217 | "State", | |
4218 | &error, | |
4219 | &reply, | |
4220 | "s"); | |
4221 | if (r < 0) { | |
4222 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) || | |
4223 | sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED)) | |
4224 | return true; | |
4225 | ||
4226 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT)) | |
4227 | return false; | |
4228 | ||
bd26aee1 | 4229 | sd_bus_error_move(ret_error, &error); |
cc377381 | 4230 | return r; |
fb6becb4 LP |
4231 | } |
4232 | ||
cc377381 LP |
4233 | /* We don't actually care about the state really. The fact |
4234 | * that we could read the job state is enough for us */ | |
fb6becb4 | 4235 | |
cc377381 | 4236 | return true; |
fb6becb4 | 4237 | } |