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