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