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