]>
Commit | Line | Data |
---|---|---|
3f49d45a LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2011 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
9 | under the terms of the GNU Lesser General Public License as published by |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
3f49d45a LP |
11 | (at your option) any later version. |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 16 | Lesser General Public License for more details. |
3f49d45a | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
3f49d45a LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
a185c5aa LP |
22 | #include <errno.h> |
23 | #include <string.h> | |
98a28fef | 24 | #include <unistd.h> |
7f7bb946 | 25 | #include <pwd.h> |
adacb957 | 26 | #include <sys/capability.h> |
a185c5aa | 27 | |
cc377381 LP |
28 | #include "sd-id128.h" |
29 | #include "sd-messages.h" | |
98a28fef | 30 | #include "strv.h" |
49e942b2 | 31 | #include "mkdir.h" |
9eb977db | 32 | #include "path-util.h" |
55af3897 | 33 | #include "special.h" |
19adb8a3 | 34 | #include "sleep-config.h" |
a5c32cff HH |
35 | #include "fileio-label.h" |
36 | #include "label.h" | |
9444b1f2 LP |
37 | #include "utf8.h" |
38 | #include "unit-name.h" | |
fb6becb4 | 39 | #include "virt.h" |
cc377381 LP |
40 | #include "audit.h" |
41 | #include "bus-util.h" | |
42 | #include "bus-error.h" | |
43 | #include "logind.h" | |
718db961 | 44 | #include "bus-errors.h" |
06acf2d4 | 45 | #include "udev-util.h" |
3f49d45a | 46 | |
cc377381 LP |
47 | static int property_get_idle_hint( |
48 | sd_bus *bus, | |
49 | const char *path, | |
50 | const char *interface, | |
51 | const char *property, | |
52 | sd_bus_message *reply, | |
ebcf1f97 LP |
53 | void *userdata, |
54 | sd_bus_error *error) { | |
a185c5aa | 55 | |
cc377381 | 56 | Manager *m = userdata; |
a185c5aa | 57 | |
cc377381 LP |
58 | assert(bus); |
59 | assert(reply); | |
60 | assert(m); | |
61 | ||
62 | return sd_bus_message_append(reply, "b", manager_get_idle_hint(m, NULL) > 0); | |
a185c5aa LP |
63 | } |
64 | ||
cc377381 LP |
65 | static int property_get_idle_since_hint( |
66 | sd_bus *bus, | |
67 | const char *path, | |
68 | const char *interface, | |
69 | const char *property, | |
70 | sd_bus_message *reply, | |
ebcf1f97 LP |
71 | void *userdata, |
72 | sd_bus_error *error) { | |
cc377381 LP |
73 | |
74 | Manager *m = userdata; | |
a185c5aa | 75 | dual_timestamp t; |
a185c5aa | 76 | |
cc377381 LP |
77 | assert(bus); |
78 | assert(reply); | |
a185c5aa LP |
79 | assert(m); |
80 | ||
81 | manager_get_idle_hint(m, &t); | |
a185c5aa | 82 | |
cc377381 | 83 | return sd_bus_message_append(reply, "t", streq(property, "IdleSinceHint") ? t.realtime : t.monotonic); |
a185c5aa LP |
84 | } |
85 | ||
cc377381 LP |
86 | static int property_get_inhibited( |
87 | sd_bus *bus, | |
88 | const char *path, | |
89 | const char *interface, | |
90 | const char *property, | |
91 | sd_bus_message *reply, | |
ebcf1f97 LP |
92 | void *userdata, |
93 | sd_bus_error *error) { | |
cc377381 LP |
94 | |
95 | Manager *m = userdata; | |
f8e2fb7b | 96 | InhibitWhat w; |
f8e2fb7b | 97 | |
cc377381 LP |
98 | assert(bus); |
99 | assert(reply); | |
100 | assert(m); | |
f8e2fb7b | 101 | |
cc377381 | 102 | w = manager_inhibit_what(m, streq(property, "BlockInhibited") ? INHIBIT_BLOCK : INHIBIT_DELAY); |
f8e2fb7b | 103 | |
cc377381 | 104 | return sd_bus_message_append(reply, "s", inhibit_what_to_string(w)); |
f8e2fb7b LP |
105 | } |
106 | ||
cc377381 LP |
107 | static int property_get_preparing( |
108 | sd_bus *bus, | |
109 | const char *path, | |
110 | const char *interface, | |
111 | const char *property, | |
112 | sd_bus_message *reply, | |
ebcf1f97 LP |
113 | void *userdata, |
114 | sd_bus_error *error) { | |
cc377381 LP |
115 | |
116 | Manager *m = userdata; | |
117 | bool b; | |
5e4a79da | 118 | |
cc377381 LP |
119 | assert(bus); |
120 | assert(reply); | |
121 | assert(m); | |
5e4a79da LP |
122 | |
123 | if (streq(property, "PreparingForShutdown")) | |
314b4b0a | 124 | b = !!(m->action_what & INHIBIT_SHUTDOWN); |
5e4a79da | 125 | else |
314b4b0a | 126 | b = !!(m->action_what & INHIBIT_SLEEP); |
5e4a79da | 127 | |
cc377381 | 128 | return sd_bus_message_append(reply, "b", b); |
5e4a79da LP |
129 | } |
130 | ||
cc377381 | 131 | static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_handle_action, handle_action, HandleAction); |
fb6becb4 | 132 | |
ebcf1f97 | 133 | static int method_get_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
134 | _cleanup_free_ char *p = NULL; |
135 | Manager *m = userdata; | |
136 | const char *name; | |
137 | Session *session; | |
138 | int r; | |
139 | ||
140 | assert(bus); | |
141 | assert(message); | |
142 | assert(m); | |
143 | ||
144 | r = sd_bus_message_read(message, "s", &name); | |
145 | if (r < 0) | |
ebcf1f97 | 146 | return r; |
cc377381 LP |
147 | |
148 | session = hashmap_get(m->sessions, name); | |
149 | if (!session) | |
ebcf1f97 | 150 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name); |
cc377381 LP |
151 | |
152 | p = session_bus_path(session); | |
153 | if (!p) | |
ebcf1f97 | 154 | return -ENOMEM; |
cc377381 | 155 | |
df2d202e | 156 | return sd_bus_reply_method_return(message, "o", p); |
cc377381 LP |
157 | } |
158 | ||
ebcf1f97 | 159 | static int method_get_session_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 160 | _cleanup_free_ char *p = NULL; |
954449b8 | 161 | Session *session = NULL; |
cc377381 | 162 | Manager *m = userdata; |
4e724d9c | 163 | pid_t pid; |
cc377381 LP |
164 | int r; |
165 | ||
166 | assert(bus); | |
167 | assert(message); | |
168 | assert(m); | |
169 | ||
4e724d9c LP |
170 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); |
171 | ||
cc377381 LP |
172 | r = sd_bus_message_read(message, "u", &pid); |
173 | if (r < 0) | |
ebcf1f97 | 174 | return r; |
cc377381 | 175 | |
4e724d9c | 176 | if (pid == 0) { |
5b12334d LP |
177 | _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; |
178 | ||
179 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); | |
180 | if (r < 0) | |
181 | return r; | |
182 | ||
183 | r = sd_bus_creds_get_pid(creds, &pid); | |
4e724d9c | 184 | if (r < 0) |
ebcf1f97 | 185 | return r; |
4e724d9c LP |
186 | } |
187 | ||
cc377381 LP |
188 | r = manager_get_session_by_pid(m, pid, &session); |
189 | if (r < 0) | |
ebcf1f97 | 190 | return r; |
cc377381 | 191 | if (!session) |
ebcf1f97 | 192 | return sd_bus_error_setf(error, BUS_ERROR_NO_SESSION_FOR_PID, "PID %lu does not belong to any known session", (unsigned long) pid); |
cc377381 LP |
193 | |
194 | p = session_bus_path(session); | |
195 | if (!p) | |
ebcf1f97 | 196 | return -ENOMEM; |
cc377381 | 197 | |
df2d202e | 198 | return sd_bus_reply_method_return(message, "o", p); |
cc377381 LP |
199 | } |
200 | ||
ebcf1f97 | 201 | static int method_get_user(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
202 | _cleanup_free_ char *p = NULL; |
203 | Manager *m = userdata; | |
204 | uint32_t uid; | |
205 | User *user; | |
206 | int r; | |
207 | ||
208 | assert(bus); | |
209 | assert(message); | |
210 | assert(m); | |
211 | ||
212 | r = sd_bus_message_read(message, "u", &uid); | |
213 | if (r < 0) | |
ebcf1f97 | 214 | return r; |
cc377381 LP |
215 | |
216 | user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid)); | |
217 | if (!user) | |
ebcf1f97 | 218 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "No user '%lu' known or logged in", (unsigned long) uid); |
cc377381 LP |
219 | |
220 | p = user_bus_path(user); | |
221 | if (!p) | |
ebcf1f97 | 222 | return -ENOMEM; |
cc377381 | 223 | |
df2d202e | 224 | return sd_bus_reply_method_return(message, "o", p); |
cc377381 LP |
225 | } |
226 | ||
ebcf1f97 | 227 | static int method_get_user_by_pid(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
228 | _cleanup_free_ char *p = NULL; |
229 | Manager *m = userdata; | |
954449b8 | 230 | User *user = NULL; |
4e724d9c | 231 | pid_t pid; |
fb6becb4 | 232 | int r; |
98a28fef | 233 | |
cc377381 LP |
234 | assert(bus); |
235 | assert(message); | |
98a28fef | 236 | assert(m); |
cc377381 | 237 | |
4e724d9c LP |
238 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); |
239 | ||
cc377381 LP |
240 | r = sd_bus_message_read(message, "u", &pid); |
241 | if (r < 0) | |
ebcf1f97 | 242 | return r; |
cc377381 | 243 | |
4e724d9c | 244 | if (pid == 0) { |
5b12334d LP |
245 | _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; |
246 | ||
247 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); | |
248 | if (r < 0) | |
249 | return r; | |
250 | ||
251 | r = sd_bus_creds_get_pid(creds, &pid); | |
4e724d9c | 252 | if (r < 0) |
ebcf1f97 | 253 | return r; |
4e724d9c LP |
254 | } |
255 | ||
cc377381 LP |
256 | r = manager_get_user_by_pid(m, pid, &user); |
257 | if (r < 0) | |
ebcf1f97 | 258 | return r; |
cc377381 | 259 | if (!user) |
ebcf1f97 | 260 | return sd_bus_error_setf(error, BUS_ERROR_NO_USER_FOR_PID, "PID %lu does not belong to any known or logged in user", (unsigned long) pid); |
cc377381 LP |
261 | |
262 | p = user_bus_path(user); | |
263 | if (!p) | |
ebcf1f97 | 264 | return -ENOMEM; |
cc377381 | 265 | |
df2d202e | 266 | return sd_bus_reply_method_return(message, "o", p); |
cc377381 LP |
267 | } |
268 | ||
ebcf1f97 | 269 | static int method_get_seat(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
270 | _cleanup_free_ char *p = NULL; |
271 | Manager *m = userdata; | |
272 | const char *name; | |
273 | Seat *seat; | |
274 | int r; | |
275 | ||
276 | assert(bus); | |
98a28fef | 277 | assert(message); |
cc377381 | 278 | assert(m); |
98a28fef | 279 | |
cc377381 LP |
280 | r = sd_bus_message_read(message, "s", &name); |
281 | if (r < 0) | |
ebcf1f97 | 282 | return r; |
98a28fef | 283 | |
cc377381 LP |
284 | seat = hashmap_get(m->seats, name); |
285 | if (!seat) | |
ebcf1f97 | 286 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name); |
98a28fef | 287 | |
cc377381 LP |
288 | p = seat_bus_path(seat); |
289 | if (!p) | |
ebcf1f97 | 290 | return -ENOMEM; |
98a28fef | 291 | |
df2d202e | 292 | return sd_bus_reply_method_return(message, "o", p); |
cc377381 | 293 | } |
98a28fef | 294 | |
ebcf1f97 | 295 | static int method_list_sessions(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
296 | _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; |
297 | Manager *m = userdata; | |
298 | Session *session; | |
299 | Iterator i; | |
300 | int r; | |
301 | ||
302 | assert(bus); | |
303 | assert(message); | |
304 | assert(m); | |
98a28fef | 305 | |
df2d202e | 306 | r = sd_bus_message_new_method_return(message, &reply); |
cc377381 | 307 | if (r < 0) |
ebcf1f97 | 308 | return r; |
98a28fef | 309 | |
cc377381 LP |
310 | r = sd_bus_message_open_container(reply, 'a', "(susso)"); |
311 | if (r < 0) | |
ebcf1f97 | 312 | return r; |
cc377381 LP |
313 | |
314 | HASHMAP_FOREACH(session, m->sessions, i) { | |
315 | _cleanup_free_ char *p = NULL; | |
316 | ||
317 | p = session_bus_path(session); | |
318 | if (!p) | |
ebcf1f97 | 319 | return -ENOMEM; |
cc377381 LP |
320 | |
321 | r = sd_bus_message_append(reply, "(susso)", | |
322 | session->id, | |
323 | (uint32_t) session->user->uid, | |
324 | session->user->name, | |
325 | session->seat ? session->seat->id : "", | |
326 | p); | |
327 | if (r < 0) | |
ebcf1f97 | 328 | return r; |
cc377381 LP |
329 | } |
330 | ||
331 | r = sd_bus_message_close_container(reply); | |
332 | if (r < 0) | |
ebcf1f97 | 333 | return r; |
cc377381 LP |
334 | |
335 | return sd_bus_send(bus, reply, NULL); | |
336 | } | |
337 | ||
ebcf1f97 | 338 | static int method_list_users(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
339 | _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; |
340 | Manager *m = userdata; | |
341 | User *user; | |
342 | Iterator i; | |
343 | int r; | |
344 | ||
345 | assert(bus); | |
346 | assert(message); | |
347 | assert(m); | |
348 | ||
df2d202e | 349 | r = sd_bus_message_new_method_return(message, &reply); |
cc377381 | 350 | if (r < 0) |
ebcf1f97 | 351 | return r; |
cc377381 LP |
352 | |
353 | r = sd_bus_message_open_container(reply, 'a', "(uso)"); | |
354 | if (r < 0) | |
ebcf1f97 | 355 | return r; |
cc377381 LP |
356 | |
357 | HASHMAP_FOREACH(user, m->users, i) { | |
358 | _cleanup_free_ char *p = NULL; | |
359 | ||
360 | p = user_bus_path(user); | |
361 | if (!p) | |
ebcf1f97 | 362 | return -ENOMEM; |
cc377381 LP |
363 | |
364 | r = sd_bus_message_append(reply, "(uso)", | |
365 | (uint32_t) user->uid, | |
366 | user->name, | |
367 | p); | |
368 | if (r < 0) | |
ebcf1f97 | 369 | return r; |
cc377381 LP |
370 | } |
371 | ||
372 | r = sd_bus_message_close_container(reply); | |
373 | if (r < 0) | |
ebcf1f97 | 374 | return r; |
cc377381 LP |
375 | |
376 | return sd_bus_send(bus, reply, NULL); | |
377 | } | |
378 | ||
ebcf1f97 | 379 | static int method_list_seats(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
380 | _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; |
381 | Manager *m = userdata; | |
382 | Seat *seat; | |
383 | Iterator i; | |
384 | int r; | |
385 | ||
386 | assert(bus); | |
387 | assert(message); | |
388 | assert(m); | |
389 | ||
df2d202e | 390 | r = sd_bus_message_new_method_return(message, &reply); |
cc377381 | 391 | if (r < 0) |
ebcf1f97 | 392 | return r; |
cc377381 LP |
393 | |
394 | r = sd_bus_message_open_container(reply, 'a', "(so)"); | |
395 | if (r < 0) | |
ebcf1f97 | 396 | return r; |
cc377381 LP |
397 | |
398 | HASHMAP_FOREACH(seat, m->seats, i) { | |
399 | _cleanup_free_ char *p = NULL; | |
400 | ||
401 | p = seat_bus_path(seat); | |
402 | if (!p) | |
ebcf1f97 | 403 | return -ENOMEM; |
cc377381 | 404 | |
b8358bce | 405 | r = sd_bus_message_append(reply, "(so)", seat->id, p); |
cc377381 | 406 | if (r < 0) |
ebcf1f97 | 407 | return r; |
cc377381 LP |
408 | } |
409 | ||
410 | r = sd_bus_message_close_container(reply); | |
411 | if (r < 0) | |
ebcf1f97 | 412 | return r; |
cc377381 LP |
413 | |
414 | return sd_bus_send(bus, reply, NULL); | |
415 | } | |
416 | ||
ebcf1f97 | 417 | static int method_list_inhibitors(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
418 | _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; |
419 | Manager *m = userdata; | |
420 | Inhibitor *inhibitor; | |
421 | Iterator i; | |
422 | int r; | |
423 | ||
df2d202e | 424 | r = sd_bus_message_new_method_return(message, &reply); |
cc377381 | 425 | if (r < 0) |
ebcf1f97 | 426 | return r; |
cc377381 LP |
427 | |
428 | r = sd_bus_message_open_container(reply, 'a', "(ssssuu)"); | |
429 | if (r < 0) | |
ebcf1f97 | 430 | return r; |
cc377381 LP |
431 | |
432 | HASHMAP_FOREACH(inhibitor, m->inhibitors, i) { | |
433 | ||
dbfa3fbb | 434 | r = sd_bus_message_append(reply, "(ssssuu)", |
cc377381 LP |
435 | strempty(inhibit_what_to_string(inhibitor->what)), |
436 | strempty(inhibitor->who), | |
437 | strempty(inhibitor->why), | |
438 | strempty(inhibit_mode_to_string(inhibitor->mode)), | |
439 | (uint32_t) inhibitor->uid, | |
440 | (uint32_t) inhibitor->pid); | |
441 | if (r < 0) | |
ebcf1f97 | 442 | return r; |
cc377381 LP |
443 | } |
444 | ||
445 | r = sd_bus_message_close_container(reply); | |
446 | if (r < 0) | |
ebcf1f97 | 447 | return r; |
cc377381 LP |
448 | |
449 | return sd_bus_send(bus, reply, NULL); | |
450 | } | |
451 | ||
ebcf1f97 | 452 | static int method_create_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
a4cd87e9 | 453 | const char *service, *type, *class, *cseat, *tty, *display, *remote_user, *remote_host, *desktop; |
cc377381 LP |
454 | uint32_t uid, leader, audit_id = 0; |
455 | _cleanup_free_ char *id = NULL; | |
456 | Session *session = NULL; | |
457 | Manager *m = userdata; | |
458 | User *user = NULL; | |
459 | Seat *seat = NULL; | |
460 | int remote; | |
461 | uint32_t vtnr = 0; | |
462 | SessionType t; | |
463 | SessionClass c; | |
464 | int r; | |
465 | ||
466 | assert(bus); | |
467 | assert(message); | |
468 | assert(m); | |
469 | ||
a4cd87e9 | 470 | r = sd_bus_message_read(message, "uusssssussbss", &uid, &leader, &service, &type, &class, &desktop, &cseat, &vtnr, &tty, &display, &remote, &remote_user, &remote_host); |
cc377381 | 471 | if (r < 0) |
ebcf1f97 | 472 | return r; |
cc377381 LP |
473 | |
474 | if (leader == 1) | |
ebcf1f97 | 475 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID"); |
98a28fef | 476 | |
e2acb67b LP |
477 | if (isempty(type)) |
478 | t = _SESSION_TYPE_INVALID; | |
479 | else { | |
480 | t = session_type_from_string(type); | |
481 | if (t < 0) | |
ebcf1f97 | 482 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session type %s", type); |
e2acb67b | 483 | } |
98a28fef | 484 | |
55efac6c | 485 | if (isempty(class)) |
e2acb67b LP |
486 | c = _SESSION_CLASS_INVALID; |
487 | else { | |
55efac6c | 488 | c = session_class_from_string(class); |
e2acb67b | 489 | if (c < 0) |
ebcf1f97 | 490 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid session class %s", class); |
e2acb67b | 491 | } |
55efac6c | 492 | |
a4cd87e9 LP |
493 | if (isempty(desktop)) |
494 | desktop = NULL; | |
495 | else { | |
496 | if (!string_is_safe(desktop)) | |
497 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid desktop string %s", desktop); | |
498 | } | |
499 | ||
954449b8 LP |
500 | if (isempty(cseat)) |
501 | seat = NULL; | |
98a28fef | 502 | else { |
954449b8 LP |
503 | seat = hashmap_get(m->seats, cseat); |
504 | if (!seat) | |
d14ab08b | 505 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", cseat); |
98a28fef LP |
506 | } |
507 | ||
98a28fef | 508 | if (tty_is_vc(tty)) { |
4d6d6518 | 509 | int v; |
98a28fef | 510 | |
954449b8 | 511 | if (!seat) |
92432fcc DH |
512 | seat = m->seat0; |
513 | else if (seat != m->seat0) | |
d14ab08b | 514 | 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 | 515 | |
4d6d6518 | 516 | v = vtnr_from_tty(tty); |
4d6d6518 | 517 | if (v <= 0) |
ebcf1f97 | 518 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Cannot determine VT number from virtual console TTY %s", tty); |
98a28fef | 519 | |
92bd5ff3 | 520 | if (!vtnr) |
4d6d6518 LP |
521 | vtnr = (uint32_t) v; |
522 | else if (vtnr != (uint32_t) v) | |
ebcf1f97 | 523 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Specified TTY and VT number do not match"); |
cc377381 | 524 | |
d1122ad5 LP |
525 | } else if (tty_is_console(tty)) { |
526 | ||
954449b8 | 527 | if (!seat) |
92432fcc DH |
528 | seat = m->seat0; |
529 | else if (seat != m->seat0) | |
ebcf1f97 | 530 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but seat is not seat0"); |
d1122ad5 LP |
531 | |
532 | if (vtnr != 0) | |
ebcf1f97 | 533 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Console TTY specified but VT number is not 0"); |
978cf3c7 | 534 | } |
98a28fef | 535 | |
954449b8 | 536 | if (seat) { |
bf7825ae | 537 | if (seat_has_vts(seat)) { |
c506027a | 538 | if (!vtnr || vtnr > 63) |
ebcf1f97 | 539 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "VT number out of range"); |
4d6d6518 | 540 | } else { |
d1122ad5 | 541 | if (vtnr != 0) |
ebcf1f97 | 542 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat has no VTs but VT number not 0"); |
4d6d6518 LP |
543 | } |
544 | } | |
545 | ||
cc377381 LP |
546 | r = sd_bus_message_enter_container(message, 'a', "(sv)"); |
547 | if (r < 0) | |
ebcf1f97 | 548 | return r; |
98a28fef | 549 | |
e2acb67b LP |
550 | if (t == _SESSION_TYPE_INVALID) { |
551 | if (!isempty(display)) | |
552 | t = SESSION_X11; | |
553 | else if (!isempty(tty)) | |
554 | t = SESSION_TTY; | |
555 | else | |
556 | t = SESSION_UNSPECIFIED; | |
557 | } | |
558 | ||
559 | if (c == _SESSION_CLASS_INVALID) { | |
a4cd87e9 | 560 | if (t == SESSION_UNSPECIFIED) |
e2acb67b | 561 | c = SESSION_BACKGROUND; |
a4cd87e9 LP |
562 | else |
563 | c = SESSION_USER; | |
e2acb67b LP |
564 | } |
565 | ||
9444b1f2 | 566 | if (leader <= 0) { |
5b12334d LP |
567 | _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; |
568 | ||
569 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); | |
570 | if (r < 0) | |
571 | return r; | |
572 | ||
cc377381 LP |
573 | assert_cc(sizeof(uint32_t) == sizeof(pid_t)); |
574 | ||
5b12334d | 575 | r = sd_bus_creds_get_pid(creds, (pid_t*) &leader); |
cc377381 | 576 | if (r < 0) |
ebcf1f97 | 577 | return r; |
9444b1f2 LP |
578 | } |
579 | ||
872c8faa | 580 | manager_get_session_by_pid(m, leader, &session); |
fb6becb4 | 581 | if (session) { |
fb6becb4 LP |
582 | _cleanup_free_ char *path = NULL; |
583 | _cleanup_close_ int fifo_fd = -1; | |
98a28fef | 584 | |
fb6becb4 LP |
585 | /* Session already exists, client is probably |
586 | * something like "su" which changes uid but is still | |
587 | * the same session */ | |
98a28fef | 588 | |
954449b8 | 589 | fifo_fd = session_create_fifo(session); |
cc377381 | 590 | if (fifo_fd < 0) |
ebcf1f97 | 591 | return fifo_fd; |
98a28fef | 592 | |
fb6becb4 | 593 | path = session_bus_path(session); |
cc377381 | 594 | if (!path) |
ebcf1f97 | 595 | return -ENOMEM; |
21c390cc | 596 | |
cc377381 | 597 | return sd_bus_reply_method_return( |
baae0358 | 598 | message, "soshusub", |
cc377381 LP |
599 | session->id, |
600 | path, | |
601 | session->user->runtime_path, | |
602 | fifo_fd, | |
baae0358 | 603 | (uint32_t) session->user->uid, |
cc377381 LP |
604 | session->seat ? session->seat->id : "", |
605 | (uint32_t) session->vtnr, | |
606 | true); | |
954449b8 | 607 | } |
21c390cc | 608 | |
954449b8 LP |
609 | audit_session_from_pid(leader, &audit_id); |
610 | if (audit_id > 0) { | |
611 | /* Keep our session IDs and the audit session IDs in sync */ | |
21c390cc | 612 | |
cc377381 | 613 | if (asprintf(&id, "%lu", (unsigned long) audit_id) < 0) |
ebcf1f97 | 614 | return -ENOMEM; |
21c390cc | 615 | |
954449b8 LP |
616 | /* Wut? There's already a session by this name and we |
617 | * didn't find it above? Weird, then let's not trust | |
618 | * the audit data and let's better register a new | |
619 | * ID */ | |
620 | if (hashmap_get(m->sessions, id)) { | |
4b549144 | 621 | log_warning("Existing logind session ID %s used by new audit session, ignoring", id); |
954449b8 | 622 | audit_id = 0; |
8ea913b2 | 623 | |
954449b8 LP |
624 | free(id); |
625 | id = NULL; | |
07714753 | 626 | } |
954449b8 | 627 | } |
07714753 | 628 | |
954449b8 | 629 | if (!id) { |
07714753 LP |
630 | do { |
631 | free(id); | |
f8e2fb7b | 632 | id = NULL; |
07714753 | 633 | |
cc377381 | 634 | if (asprintf(&id, "c%lu", ++m->session_counter) < 0) |
ebcf1f97 | 635 | return -ENOMEM; |
07714753 LP |
636 | |
637 | } while (hashmap_get(m->sessions, id)); | |
98a28fef LP |
638 | } |
639 | ||
954449b8 | 640 | r = manager_add_user_by_uid(m, uid, &user); |
ebcf1f97 | 641 | if (r < 0) |
954449b8 LP |
642 | goto fail; |
643 | ||
9444b1f2 | 644 | r = manager_add_session(m, id, &session); |
ebcf1f97 | 645 | if (r < 0) |
98a28fef LP |
646 | goto fail; |
647 | ||
9444b1f2 LP |
648 | session_set_user(session, user); |
649 | ||
98a28fef LP |
650 | session->leader = leader; |
651 | session->audit_id = audit_id; | |
652 | session->type = t; | |
55efac6c | 653 | session->class = c; |
98a28fef | 654 | session->remote = remote; |
98a28fef LP |
655 | session->vtnr = vtnr; |
656 | ||
98a28fef LP |
657 | if (!isempty(tty)) { |
658 | session->tty = strdup(tty); | |
659 | if (!session->tty) { | |
ebcf1f97 | 660 | r = -ENOMEM; |
98a28fef LP |
661 | goto fail; |
662 | } | |
663 | } | |
664 | ||
665 | if (!isempty(display)) { | |
666 | session->display = strdup(display); | |
667 | if (!session->display) { | |
ebcf1f97 | 668 | r = -ENOMEM; |
98a28fef LP |
669 | goto fail; |
670 | } | |
671 | } | |
672 | ||
673 | if (!isempty(remote_user)) { | |
674 | session->remote_user = strdup(remote_user); | |
675 | if (!session->remote_user) { | |
ebcf1f97 | 676 | r = -ENOMEM; |
98a28fef LP |
677 | goto fail; |
678 | } | |
679 | } | |
680 | ||
681 | if (!isempty(remote_host)) { | |
682 | session->remote_host = strdup(remote_host); | |
683 | if (!session->remote_host) { | |
ebcf1f97 | 684 | r = -ENOMEM; |
98a28fef LP |
685 | goto fail; |
686 | } | |
687 | } | |
688 | ||
689 | if (!isempty(service)) { | |
690 | session->service = strdup(service); | |
691 | if (!session->service) { | |
ebcf1f97 | 692 | r = -ENOMEM; |
98a28fef LP |
693 | goto fail; |
694 | } | |
695 | } | |
696 | ||
a4cd87e9 LP |
697 | if (!isempty(desktop)) { |
698 | session->desktop = strdup(desktop); | |
699 | if (!session->desktop) { | |
700 | r = -ENOMEM; | |
701 | goto fail; | |
702 | } | |
703 | } | |
704 | ||
954449b8 LP |
705 | if (seat) { |
706 | r = seat_attach_session(seat, session); | |
ebcf1f97 | 707 | if (r < 0) |
98a28fef LP |
708 | goto fail; |
709 | } | |
710 | ||
711 | r = session_start(session); | |
ebcf1f97 | 712 | if (r < 0) |
98a28fef LP |
713 | goto fail; |
714 | ||
cc377381 | 715 | session->create_message = sd_bus_message_ref(message); |
98a28fef | 716 | |
cba38758 LP |
717 | /* Now, let's wait until the slice unit and stuff got |
718 | * created. We send the reply back from | |
719 | * session_send_create_reply().*/ | |
720 | ||
cc377381 | 721 | return 1; |
98a28fef LP |
722 | |
723 | fail: | |
98a28fef LP |
724 | if (session) |
725 | session_add_to_gc_queue(session); | |
726 | ||
727 | if (user) | |
728 | user_add_to_gc_queue(user); | |
729 | ||
98a28fef LP |
730 | return r; |
731 | } | |
732 | ||
ebcf1f97 | 733 | static int method_release_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
734 | Manager *m = userdata; |
735 | Session *session; | |
736 | const char *name; | |
737 | int r; | |
314b4b0a | 738 | |
cc377381 LP |
739 | assert(bus); |
740 | assert(message); | |
741 | assert(m); | |
742 | ||
743 | r = sd_bus_message_read(message, "s", &name); | |
744 | if (r < 0) | |
ebcf1f97 | 745 | return r; |
cc377381 LP |
746 | |
747 | session = hashmap_get(m->sessions, name); | |
748 | if (!session) | |
ebcf1f97 | 749 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name); |
cc377381 | 750 | |
5f41d1f1 | 751 | session_release(session); |
cc377381 | 752 | |
df2d202e | 753 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
754 | } |
755 | ||
ebcf1f97 | 756 | static int method_activate_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
757 | Manager *m = userdata; |
758 | Session *session; | |
759 | const char *name; | |
760 | int r; | |
f8e2fb7b | 761 | |
cc377381 LP |
762 | assert(bus); |
763 | assert(message); | |
f8e2fb7b | 764 | assert(m); |
cc377381 LP |
765 | |
766 | r = sd_bus_message_read(message, "s", &name); | |
767 | if (r < 0) | |
ebcf1f97 | 768 | return r; |
cc377381 LP |
769 | |
770 | session = hashmap_get(m->sessions, name); | |
771 | if (!session) | |
ebcf1f97 | 772 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name); |
cc377381 LP |
773 | |
774 | r = session_activate(session); | |
775 | if (r < 0) | |
ebcf1f97 | 776 | return r; |
cc377381 | 777 | |
df2d202e | 778 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
779 | } |
780 | ||
ebcf1f97 | 781 | static int method_activate_session_on_seat(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
782 | const char *session_name, *seat_name; |
783 | Manager *m = userdata; | |
784 | Session *session; | |
785 | Seat *seat; | |
786 | int r; | |
787 | ||
788 | assert(bus); | |
f8e2fb7b | 789 | assert(message); |
cc377381 | 790 | assert(m); |
f8e2fb7b | 791 | |
cc377381 LP |
792 | /* Same as ActivateSession() but refuses to work if |
793 | * the seat doesn't match */ | |
f8e2fb7b | 794 | |
cc377381 LP |
795 | r = sd_bus_message_read(message, "ss", &session_name, &seat_name); |
796 | if (r < 0) | |
ebcf1f97 | 797 | return r; |
eecd1362 | 798 | |
cc377381 LP |
799 | session = hashmap_get(m->sessions, session_name); |
800 | if (!session) | |
ebcf1f97 | 801 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", session_name); |
beaafb2e | 802 | |
cc377381 LP |
803 | seat = hashmap_get(m->seats, seat_name); |
804 | if (!seat) | |
ebcf1f97 | 805 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", seat_name); |
314b4b0a | 806 | |
cc377381 | 807 | if (session->seat != seat) |
ebcf1f97 | 808 | return sd_bus_error_setf(error, BUS_ERROR_SESSION_NOT_ON_SEAT, "Session %s not on seat %s", session_name, seat_name); |
cc377381 LP |
809 | |
810 | r = session_activate(session); | |
f8e2fb7b | 811 | if (r < 0) |
ebcf1f97 | 812 | return r; |
f8e2fb7b | 813 | |
df2d202e | 814 | return sd_bus_reply_method_return(message, NULL); |
cc377381 | 815 | } |
f8e2fb7b | 816 | |
ebcf1f97 | 817 | static int method_lock_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
818 | Manager *m = userdata; |
819 | Session *session; | |
820 | const char *name; | |
821 | int r; | |
f8e2fb7b | 822 | |
cc377381 LP |
823 | assert(bus); |
824 | assert(message); | |
825 | assert(m); | |
f8e2fb7b | 826 | |
cc377381 LP |
827 | r = sd_bus_message_read(message, "s", &name); |
828 | if (r < 0) | |
ebcf1f97 | 829 | return r; |
f8e2fb7b | 830 | |
cc377381 LP |
831 | session = hashmap_get(m->sessions, name); |
832 | if (!session) | |
ebcf1f97 | 833 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name); |
f8e2fb7b | 834 | |
cc377381 | 835 | r = session_send_lock(session, streq(sd_bus_message_get_member(message), "LockSession")); |
f8e2fb7b | 836 | if (r < 0) |
ebcf1f97 | 837 | return r; |
f8e2fb7b | 838 | |
df2d202e | 839 | return sd_bus_reply_method_return(message, NULL); |
cc377381 | 840 | } |
f8e2fb7b | 841 | |
ebcf1f97 | 842 | static int method_lock_sessions(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
843 | Manager *m = userdata; |
844 | int r; | |
f8e2fb7b | 845 | |
cc377381 LP |
846 | assert(bus); |
847 | assert(message); | |
848 | assert(m); | |
f8e2fb7b | 849 | |
cc377381 LP |
850 | r = session_send_lock_all(m, streq(sd_bus_message_get_member(message), "LockSessions")); |
851 | if (r < 0) | |
ebcf1f97 | 852 | return r; |
f8e2fb7b | 853 | |
df2d202e | 854 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
855 | } |
856 | ||
ebcf1f97 | 857 | static int method_kill_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
858 | const char *name, *swho; |
859 | Manager *m = userdata; | |
860 | Session *session; | |
861 | int32_t signo; | |
862 | KillWho who; | |
863 | int r; | |
864 | ||
865 | assert(bus); | |
866 | assert(message); | |
867 | assert(m); | |
868 | ||
869 | r = sd_bus_message_read(message, "ssi", &name, &swho, &signo); | |
870 | if (r < 0) | |
ebcf1f97 | 871 | return r; |
cc377381 LP |
872 | |
873 | if (isempty(swho)) | |
874 | who = KILL_ALL; | |
875 | else { | |
876 | who = kill_who_from_string(swho); | |
877 | if (who < 0) | |
ebcf1f97 | 878 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho); |
f8e2fb7b LP |
879 | } |
880 | ||
cc377381 | 881 | if (signo <= 0 || signo >= _NSIG) |
ebcf1f97 | 882 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo); |
f8e2fb7b | 883 | |
cc377381 LP |
884 | session = hashmap_get(m->sessions, name); |
885 | if (!session) | |
ebcf1f97 | 886 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name); |
f8e2fb7b | 887 | |
cc377381 LP |
888 | r = session_kill(session, who, signo); |
889 | if (r < 0) | |
ebcf1f97 | 890 | return r; |
f8e2fb7b | 891 | |
df2d202e | 892 | return sd_bus_reply_method_return(message, NULL); |
cc377381 | 893 | } |
f8e2fb7b | 894 | |
ebcf1f97 | 895 | static int method_kill_user(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
896 | Manager *m = userdata; |
897 | uint32_t uid; | |
898 | int32_t signo; | |
899 | User *user; | |
900 | int r; | |
f8e2fb7b | 901 | |
cc377381 LP |
902 | assert(bus); |
903 | assert(message); | |
904 | assert(m); | |
905 | ||
906 | r = sd_bus_message_read(message, "ui", &uid, &signo); | |
907 | if (r < 0) | |
ebcf1f97 | 908 | return r; |
cc377381 LP |
909 | |
910 | if (signo <= 0 || signo >= _NSIG) | |
ebcf1f97 | 911 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo); |
cc377381 LP |
912 | |
913 | user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid)); | |
914 | if (!user) | |
ebcf1f97 | 915 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "No user '%lu' known or logged in", (unsigned long) uid); |
cc377381 LP |
916 | |
917 | r = user_kill(user, signo); | |
918 | if (r < 0) | |
ebcf1f97 | 919 | return r; |
cc377381 | 920 | |
df2d202e | 921 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
922 | } |
923 | ||
ebcf1f97 | 924 | static int method_terminate_session(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
925 | Manager *m = userdata; |
926 | const char *name; | |
927 | Session *session; | |
928 | int r; | |
929 | ||
930 | assert(bus); | |
931 | assert(message); | |
932 | assert(m); | |
933 | ||
934 | r = sd_bus_message_read(message, "s", &name); | |
935 | if (r < 0) | |
ebcf1f97 | 936 | return r; |
cc377381 LP |
937 | |
938 | session = hashmap_get(m->sessions, name); | |
939 | if (!session) | |
ebcf1f97 | 940 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SESSION, "No session '%s' known", name); |
cc377381 LP |
941 | |
942 | r = session_stop(session); | |
943 | if (r < 0) | |
ebcf1f97 | 944 | return r; |
cc377381 | 945 | |
df2d202e | 946 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
947 | } |
948 | ||
ebcf1f97 | 949 | static int method_terminate_user(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
950 | Manager *m = userdata; |
951 | uint32_t uid; | |
952 | User *user; | |
953 | int r; | |
954 | ||
955 | assert(bus); | |
956 | assert(message); | |
957 | assert(m); | |
958 | ||
959 | r = sd_bus_message_read(message, "u", &uid); | |
960 | if (r < 0) | |
ebcf1f97 | 961 | return r; |
cc377381 LP |
962 | |
963 | user = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid)); | |
964 | if (!user) | |
ebcf1f97 | 965 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER, "No user '%lu' known or logged in", (unsigned long) uid); |
cc377381 LP |
966 | |
967 | r = user_stop(user); | |
968 | if (r < 0) | |
ebcf1f97 | 969 | return r; |
cc377381 | 970 | |
df2d202e | 971 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
972 | } |
973 | ||
ebcf1f97 | 974 | static int method_terminate_seat(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
975 | Manager *m = userdata; |
976 | const char *name; | |
977 | Seat *seat; | |
978 | int r; | |
979 | ||
980 | assert(bus); | |
981 | assert(message); | |
982 | assert(m); | |
983 | ||
984 | r = sd_bus_message_read(message, "s", &name); | |
985 | if (r < 0) | |
ebcf1f97 | 986 | return r; |
cc377381 LP |
987 | |
988 | seat = hashmap_get(m->seats, name); | |
989 | if (!seat) | |
ebcf1f97 | 990 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_SEAT, "No seat '%s' known", name); |
cc377381 LP |
991 | |
992 | r = seat_stop_sessions(seat); | |
993 | if (r < 0) | |
ebcf1f97 | 994 | return r; |
cc377381 | 995 | |
df2d202e | 996 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
997 | } |
998 | ||
ebcf1f97 | 999 | static int method_set_user_linger(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
1000 | _cleanup_free_ char *cc = NULL; |
1001 | Manager *m = userdata; | |
1002 | int b, r; | |
1003 | struct passwd *pw; | |
1004 | const char *path; | |
1005 | uint32_t uid; | |
1006 | int interactive; | |
1007 | ||
1008 | assert(bus); | |
1009 | assert(message); | |
1010 | assert(m); | |
1011 | ||
1012 | r = sd_bus_message_read(message, "ubb", &uid, &b, &interactive); | |
1013 | if (r < 0) | |
ebcf1f97 | 1014 | return r; |
cc377381 LP |
1015 | |
1016 | errno = 0; | |
1017 | pw = getpwuid(uid); | |
1018 | if (!pw) | |
ebcf1f97 | 1019 | return errno ? -errno : -ENOENT; |
cc377381 LP |
1020 | |
1021 | r = bus_verify_polkit_async(bus, | |
1022 | &m->polkit_registry, | |
1023 | message, | |
1024 | "org.freedesktop.login1.set-user-linger", | |
1025 | interactive, | |
ebcf1f97 | 1026 | error, |
cc377381 LP |
1027 | method_set_user_linger, m); |
1028 | if (r < 0) | |
ebcf1f97 | 1029 | return r; |
cc377381 LP |
1030 | if (r == 0) |
1031 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1032 | ||
1033 | mkdir_p_label("/var/lib/systemd", 0755); | |
1034 | ||
1035 | r = mkdir_safe_label("/var/lib/systemd/linger", 0755, 0, 0); | |
1036 | if (r < 0) | |
ebcf1f97 | 1037 | return r; |
cc377381 LP |
1038 | |
1039 | cc = cescape(pw->pw_name); | |
1040 | if (!cc) | |
ebcf1f97 | 1041 | return -ENOMEM; |
cc377381 LP |
1042 | |
1043 | path = strappenda("/var/lib/systemd/linger/", cc); | |
1044 | if (b) { | |
1045 | User *u; | |
1046 | ||
1047 | r = touch(path); | |
1048 | if (r < 0) | |
ebcf1f97 | 1049 | return r; |
cc377381 LP |
1050 | |
1051 | if (manager_add_user_by_uid(m, uid, &u) >= 0) | |
1052 | user_start(u); | |
1053 | ||
1054 | } else { | |
1055 | User *u; | |
1056 | ||
1057 | r = unlink(path); | |
1058 | if (r < 0 && errno != ENOENT) | |
ebcf1f97 | 1059 | return -errno; |
cc377381 LP |
1060 | |
1061 | u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid)); | |
1062 | if (u) | |
1063 | user_add_to_gc_queue(u); | |
1064 | } | |
1065 | ||
df2d202e | 1066 | return sd_bus_reply_method_return(message, NULL); |
f8e2fb7b LP |
1067 | } |
1068 | ||
2eb916cd | 1069 | static int trigger_device(Manager *m, struct udev_device *d) { |
06acf2d4 | 1070 | _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL; |
b668e064 LP |
1071 | struct udev_list_entry *first, *item; |
1072 | int r; | |
1073 | ||
1074 | assert(m); | |
1075 | ||
1076 | e = udev_enumerate_new(m->udev); | |
06acf2d4 LP |
1077 | if (!e) |
1078 | return -ENOMEM; | |
b668e064 | 1079 | |
2eb916cd | 1080 | if (d) { |
06acf2d4 LP |
1081 | r = udev_enumerate_add_match_parent(e, d); |
1082 | if (r < 0) | |
1083 | return r; | |
2eb916cd LP |
1084 | } |
1085 | ||
06acf2d4 LP |
1086 | r = udev_enumerate_scan_devices(e); |
1087 | if (r < 0) | |
1088 | return r; | |
b668e064 LP |
1089 | |
1090 | first = udev_enumerate_get_list_entry(e); | |
1091 | udev_list_entry_foreach(item, first) { | |
cc377381 | 1092 | _cleanup_free_ char *t = NULL; |
b668e064 LP |
1093 | const char *p; |
1094 | ||
1095 | p = udev_list_entry_get_name(item); | |
1096 | ||
b668e064 | 1097 | t = strappend(p, "/uevent"); |
06acf2d4 LP |
1098 | if (!t) |
1099 | return -ENOMEM; | |
b668e064 | 1100 | |
574d5f2d | 1101 | write_string_file(t, "change"); |
b668e064 LP |
1102 | } |
1103 | ||
06acf2d4 | 1104 | return 0; |
b668e064 LP |
1105 | } |
1106 | ||
47a26690 | 1107 | static int attach_device(Manager *m, const char *seat, const char *sysfs) { |
06acf2d4 | 1108 | _cleanup_udev_device_unref_ struct udev_device *d = NULL; |
7fd1b19b | 1109 | _cleanup_free_ char *rule = NULL, *file = NULL; |
c28fa3d3 | 1110 | const char *id_for_seat; |
47a26690 LP |
1111 | int r; |
1112 | ||
1113 | assert(m); | |
1114 | assert(seat); | |
1115 | assert(sysfs); | |
1116 | ||
1117 | d = udev_device_new_from_syspath(m->udev, sysfs); | |
1118 | if (!d) | |
1119 | return -ENODEV; | |
1120 | ||
06acf2d4 LP |
1121 | if (!udev_device_has_tag(d, "seat")) |
1122 | return -ENODEV; | |
47a26690 | 1123 | |
c28fa3d3 | 1124 | id_for_seat = udev_device_get_property_value(d, "ID_FOR_SEAT"); |
06acf2d4 LP |
1125 | if (!id_for_seat) |
1126 | return -ENODEV; | |
47a26690 | 1127 | |
06acf2d4 LP |
1128 | if (asprintf(&file, "/etc/udev/rules.d/72-seat-%s.rules", id_for_seat) < 0) |
1129 | return -ENOMEM; | |
47a26690 | 1130 | |
06acf2d4 LP |
1131 | if (asprintf(&rule, "TAG==\"seat\", ENV{ID_FOR_SEAT}==\"%s\", ENV{ID_SEAT}=\"%s\"", id_for_seat, seat) < 0) |
1132 | return -ENOMEM; | |
47a26690 | 1133 | |
d2e54fae | 1134 | mkdir_p_label("/etc/udev/rules.d", 0755); |
a5c32cff | 1135 | label_init("/etc"); |
574d5f2d | 1136 | r = write_string_file_atomic_label(file, rule); |
a0a0c7f1 | 1137 | if (r < 0) |
06acf2d4 | 1138 | return r; |
47a26690 | 1139 | |
06acf2d4 | 1140 | return trigger_device(m, d); |
47a26690 LP |
1141 | } |
1142 | ||
b668e064 | 1143 | static int flush_devices(Manager *m) { |
7fd1b19b | 1144 | _cleanup_closedir_ DIR *d; |
b668e064 LP |
1145 | |
1146 | assert(m); | |
1147 | ||
1148 | d = opendir("/etc/udev/rules.d"); | |
1149 | if (!d) { | |
1150 | if (errno != ENOENT) | |
1151 | log_warning("Failed to open /etc/udev/rules.d: %m"); | |
1152 | } else { | |
1153 | struct dirent *de; | |
1154 | ||
1155 | while ((de = readdir(d))) { | |
1156 | ||
1157 | if (!dirent_is_file(de)) | |
1158 | continue; | |
1159 | ||
1160 | if (!startswith(de->d_name, "72-seat-")) | |
1161 | continue; | |
1162 | ||
1163 | if (!endswith(de->d_name, ".rules")) | |
1164 | continue; | |
1165 | ||
1166 | if (unlinkat(dirfd(d), de->d_name, 0) < 0) | |
1167 | log_warning("Failed to unlink %s: %m", de->d_name); | |
1168 | } | |
b668e064 LP |
1169 | } |
1170 | ||
1171 | return trigger_device(m, NULL); | |
1172 | } | |
1173 | ||
ebcf1f97 | 1174 | static int method_attach_device(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
1175 | const char *sysfs, *seat; |
1176 | Manager *m = userdata; | |
1177 | int interactive, r; | |
1178 | ||
1179 | assert(bus); | |
1180 | assert(message); | |
1181 | assert(m); | |
1182 | ||
1183 | r = sd_bus_message_read(message, "ssb", &seat, &sysfs, &interactive); | |
1184 | if (r < 0) | |
ebcf1f97 | 1185 | return r; |
cc377381 LP |
1186 | |
1187 | if (!path_startswith(sysfs, "/sys")) | |
ebcf1f97 | 1188 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path %s is not in /sys", sysfs); |
cc377381 LP |
1189 | |
1190 | if (!seat_name_is_valid(seat)) | |
ebcf1f97 | 1191 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Seat %s is not valid", seat); |
cc377381 LP |
1192 | |
1193 | r = bus_verify_polkit_async(bus, | |
1194 | &m->polkit_registry, | |
1195 | message, | |
1196 | "org.freedesktop.login1.attach-device", | |
1197 | interactive, | |
ebcf1f97 | 1198 | error, |
cc377381 LP |
1199 | method_attach_device, m); |
1200 | if (r < 0) | |
ebcf1f97 | 1201 | return r; |
cc377381 LP |
1202 | if (r == 0) |
1203 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1204 | ||
1205 | r = attach_device(m, seat, sysfs); | |
1206 | if (r < 0) | |
ebcf1f97 | 1207 | return r; |
cc377381 | 1208 | |
df2d202e | 1209 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
1210 | } |
1211 | ||
ebcf1f97 | 1212 | static int method_flush_devices(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
1213 | Manager *m = userdata; |
1214 | int interactive, r; | |
1215 | ||
1216 | assert(bus); | |
1217 | assert(message); | |
1218 | assert(m); | |
1219 | ||
1220 | r = sd_bus_message_read(message, "b", &interactive); | |
1221 | if (r < 0) | |
ebcf1f97 | 1222 | return r; |
cc377381 LP |
1223 | |
1224 | r = bus_verify_polkit_async(bus, | |
1225 | &m->polkit_registry, | |
1226 | message, | |
1227 | "org.freedesktop.login1.flush-devices", | |
1228 | interactive, | |
ebcf1f97 | 1229 | error, |
cc377381 LP |
1230 | method_flush_devices, m); |
1231 | if (r < 0) | |
ebcf1f97 | 1232 | return r; |
cc377381 LP |
1233 | if (r == 0) |
1234 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
1235 | ||
1236 | r = flush_devices(m); | |
1237 | if (r < 0) | |
ebcf1f97 | 1238 | return r; |
cc377381 | 1239 | |
df2d202e | 1240 | return sd_bus_reply_method_return(message, NULL); |
cc377381 LP |
1241 | } |
1242 | ||
89f13440 | 1243 | static int have_multiple_sessions( |
89f13440 | 1244 | Manager *m, |
409133be | 1245 | uid_t uid) { |
89f13440 | 1246 | |
2154761f MS |
1247 | Session *session; |
1248 | Iterator i; | |
89f13440 LP |
1249 | |
1250 | assert(m); | |
1251 | ||
1ca04b87 LP |
1252 | /* Check for other users' sessions. Greeter sessions do not |
1253 | * count, and non-login sessions do not count either. */ | |
2154761f | 1254 | HASHMAP_FOREACH(session, m->sessions, i) |
1ca04b87 | 1255 | if (session->class == SESSION_USER && |
1ca04b87 | 1256 | session->user->uid != uid) |
2154761f | 1257 | return true; |
89f13440 LP |
1258 | |
1259 | return false; | |
1260 | } | |
1261 | ||
314b4b0a LP |
1262 | static int bus_manager_log_shutdown( |
1263 | Manager *m, | |
1264 | InhibitWhat w, | |
1265 | const char *unit_name) { | |
1266 | ||
1267 | const char *p, *q; | |
1268 | ||
1269 | assert(m); | |
1270 | assert(unit_name); | |
1271 | ||
1272 | if (w != INHIBIT_SHUTDOWN) | |
1273 | return 0; | |
1274 | ||
1275 | if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) { | |
1276 | p = "MESSAGE=System is powering down."; | |
1277 | q = "SHUTDOWN=power-off"; | |
1278 | } else if (streq(unit_name, SPECIAL_HALT_TARGET)) { | |
1279 | p = "MESSAGE=System is halting."; | |
1280 | q = "SHUTDOWN=halt"; | |
1281 | } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) { | |
1282 | p = "MESSAGE=System is rebooting."; | |
1283 | q = "SHUTDOWN=reboot"; | |
1284 | } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) { | |
1285 | p = "MESSAGE=System is rebooting with kexec."; | |
1286 | q = "SHUTDOWN=kexec"; | |
1287 | } else { | |
1288 | p = "MESSAGE=System is shutting down."; | |
1289 | q = NULL; | |
1290 | } | |
1291 | ||
1292 | return log_struct(LOG_NOTICE, MESSAGE_ID(SD_MESSAGE_SHUTDOWN), | |
1293 | p, | |
1294 | q, NULL); | |
1295 | } | |
1296 | ||
1297 | static int execute_shutdown_or_sleep( | |
1298 | Manager *m, | |
1299 | InhibitWhat w, | |
1300 | const char *unit_name, | |
cc377381 | 1301 | sd_bus_error *error) { |
314b4b0a | 1302 | |
cc377381 LP |
1303 | _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; |
1304 | const char *p; | |
af9792ac | 1305 | char *c; |
cc377381 | 1306 | int r; |
eecd1362 | 1307 | |
af9792ac | 1308 | assert(m); |
314b4b0a LP |
1309 | assert(w >= 0); |
1310 | assert(w < _INHIBIT_WHAT_MAX); | |
d889a206 | 1311 | assert(unit_name); |
eecd1362 | 1312 | |
314b4b0a LP |
1313 | bus_manager_log_shutdown(m, w, unit_name); |
1314 | ||
cc377381 | 1315 | r = sd_bus_call_method( |
af9792ac | 1316 | m->bus, |
eecd1362 LP |
1317 | "org.freedesktop.systemd1", |
1318 | "/org/freedesktop/systemd1", | |
1319 | "org.freedesktop.systemd1.Manager", | |
b9c26b41 | 1320 | "StartUnit", |
af9792ac | 1321 | error, |
cc377381 LP |
1322 | &reply, |
1323 | "ss", unit_name, "replace-irreversibly"); | |
af9792ac LP |
1324 | if (r < 0) |
1325 | return r; | |
1326 | ||
cc377381 LP |
1327 | r = sd_bus_message_read(reply, "o", &p); |
1328 | if (r < 0) | |
1329 | return r; | |
af9792ac LP |
1330 | |
1331 | c = strdup(p); | |
1332 | if (!c) | |
1333 | return -ENOMEM; | |
1334 | ||
314b4b0a | 1335 | m->action_unit = unit_name; |
af9792ac LP |
1336 | free(m->action_job); |
1337 | m->action_job = c; | |
314b4b0a | 1338 | m->action_what = w; |
af9792ac LP |
1339 | |
1340 | return 0; | |
eecd1362 LP |
1341 | } |
1342 | ||
314b4b0a LP |
1343 | static int delay_shutdown_or_sleep( |
1344 | Manager *m, | |
1345 | InhibitWhat w, | |
1346 | const char *unit_name) { | |
eecd1362 | 1347 | |
eecd1362 | 1348 | assert(m); |
d889a206 LP |
1349 | assert(w >= 0); |
1350 | assert(w < _INHIBIT_WHAT_MAX); | |
314b4b0a | 1351 | assert(unit_name); |
eecd1362 | 1352 | |
314b4b0a LP |
1353 | m->action_timestamp = now(CLOCK_MONOTONIC); |
1354 | m->action_unit = unit_name; | |
1355 | m->action_what = w; | |
d889a206 LP |
1356 | |
1357 | return 0; | |
1358 | } | |
1359 | ||
cc377381 | 1360 | static int send_prepare_for(Manager *m, InhibitWhat w, bool _active) { |
d889a206 | 1361 | |
cc377381 LP |
1362 | static const char * const signal_name[_INHIBIT_WHAT_MAX] = { |
1363 | [INHIBIT_SHUTDOWN] = "PrepareForShutdown", | |
1364 | [INHIBIT_SLEEP] = "PrepareForSleep" | |
1365 | }; | |
1366 | ||
1367 | int active = _active; | |
877d54e9 LP |
1368 | |
1369 | assert(m); | |
314b4b0a LP |
1370 | assert(w >= 0); |
1371 | assert(w < _INHIBIT_WHAT_MAX); | |
1372 | assert(signal_name[w]); | |
877d54e9 | 1373 | |
cc377381 LP |
1374 | return sd_bus_emit_signal(m->bus, |
1375 | "/org/freedesktop/login1", | |
1376 | "org.freedesktop.login1.Manager", | |
1377 | signal_name[w], | |
1378 | "b", | |
dd9f0525 | 1379 | active); |
877d54e9 LP |
1380 | } |
1381 | ||
069cfc85 LP |
1382 | int bus_manager_shutdown_or_sleep_now_or_later( |
1383 | Manager *m, | |
1384 | const char *unit_name, | |
1385 | InhibitWhat w, | |
cc377381 | 1386 | sd_bus_error *error) { |
069cfc85 LP |
1387 | |
1388 | bool delayed; | |
1389 | int r; | |
1390 | ||
1391 | assert(m); | |
1392 | assert(unit_name); | |
1393 | assert(w >= 0); | |
1394 | assert(w <= _INHIBIT_WHAT_MAX); | |
af9792ac | 1395 | assert(!m->action_job); |
069cfc85 | 1396 | |
314b4b0a LP |
1397 | /* Tell everybody to prepare for shutdown/sleep */ |
1398 | send_prepare_for(m, w, true); | |
1399 | ||
069cfc85 LP |
1400 | delayed = |
1401 | m->inhibit_delay_max > 0 && | |
85a428c6 | 1402 | manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL); |
069cfc85 LP |
1403 | |
1404 | if (delayed) | |
1405 | /* Shutdown is delayed, keep in mind what we | |
1406 | * want to do, and start a timeout */ | |
1407 | r = delay_shutdown_or_sleep(m, w, unit_name); | |
314b4b0a | 1408 | else |
069cfc85 LP |
1409 | /* Shutdown is not delayed, execute it |
1410 | * immediately */ | |
314b4b0a | 1411 | r = execute_shutdown_or_sleep(m, w, unit_name, error); |
069cfc85 LP |
1412 | |
1413 | return r; | |
1414 | } | |
1415 | ||
cc377381 | 1416 | static int method_do_shutdown_or_sleep( |
d889a206 | 1417 | Manager *m, |
cc377381 | 1418 | sd_bus_message *message, |
d889a206 LP |
1419 | const char *unit_name, |
1420 | InhibitWhat w, | |
1421 | const char *action, | |
1422 | const char *action_multiple_sessions, | |
1423 | const char *action_ignore_inhibit, | |
19adb8a3 | 1424 | const char *sleep_verb, |
ebcf1f97 LP |
1425 | sd_bus_message_handler_t method, |
1426 | sd_bus_error *error) { | |
d889a206 | 1427 | |
5b12334d | 1428 | _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; |
069cfc85 | 1429 | bool multiple_sessions, blocked; |
cc377381 LP |
1430 | int interactive, r; |
1431 | uid_t uid; | |
d889a206 LP |
1432 | |
1433 | assert(m); | |
d889a206 LP |
1434 | assert(message); |
1435 | assert(unit_name); | |
1436 | assert(w >= 0); | |
1437 | assert(w <= _INHIBIT_WHAT_MAX); | |
1438 | assert(action); | |
1439 | assert(action_multiple_sessions); | |
1440 | assert(action_ignore_inhibit); | |
cc377381 LP |
1441 | assert(method); |
1442 | ||
1443 | r = sd_bus_message_read(message, "b", &interactive); | |
1444 | if (r < 0) | |
ebcf1f97 | 1445 | return r; |
d889a206 | 1446 | |
314b4b0a LP |
1447 | /* Don't allow multiple jobs being executed at the same time */ |
1448 | if (m->action_what) | |
ebcf1f97 | 1449 | return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress"); |
d889a206 | 1450 | |
19adb8a3 ZJS |
1451 | if (sleep_verb) { |
1452 | r = can_sleep(sleep_verb); | |
6524990f | 1453 | if (r < 0) |
ebcf1f97 | 1454 | return r; |
6524990f LP |
1455 | |
1456 | if (r == 0) | |
ebcf1f97 | 1457 | return sd_bus_error_setf(error, BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, "Sleep verb not supported"); |
6524990f LP |
1458 | } |
1459 | ||
5b12334d LP |
1460 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_UID, &creds); |
1461 | if (r < 0) | |
1462 | return r; | |
1463 | ||
1464 | r = sd_bus_creds_get_uid(creds, &uid); | |
cc377381 | 1465 | if (r < 0) |
ebcf1f97 | 1466 | return r; |
409133be | 1467 | |
cc377381 | 1468 | r = have_multiple_sessions(m, uid); |
d889a206 | 1469 | if (r < 0) |
ebcf1f97 | 1470 | return r; |
d889a206 LP |
1471 | |
1472 | multiple_sessions = r > 0; | |
85a428c6 | 1473 | blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL); |
d889a206 LP |
1474 | |
1475 | if (multiple_sessions) { | |
cc377381 | 1476 | r = bus_verify_polkit_async(m->bus, &m->polkit_registry, message, |
ebcf1f97 | 1477 | action_multiple_sessions, interactive, error, method, m); |
d889a206 | 1478 | if (r < 0) |
ebcf1f97 | 1479 | return r; |
d889a206 LP |
1480 | } |
1481 | ||
1482 | if (blocked) { | |
cc377381 | 1483 | r = bus_verify_polkit_async(m->bus, &m->polkit_registry, message, |
ebcf1f97 | 1484 | action_ignore_inhibit, interactive, error, method, m); |
d889a206 | 1485 | if (r < 0) |
ebcf1f97 | 1486 | return r; |
d889a206 LP |
1487 | } |
1488 | ||
1489 | if (!multiple_sessions && !blocked) { | |
cc377381 | 1490 | r = bus_verify_polkit_async(m->bus, &m->polkit_registry, message, |
ebcf1f97 | 1491 | action, interactive, error, method, m); |
d889a206 | 1492 | if (r < 0) |
ebcf1f97 | 1493 | return r; |
d889a206 LP |
1494 | } |
1495 | ||
ebcf1f97 | 1496 | r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error); |
d889a206 | 1497 | if (r < 0) |
ebcf1f97 | 1498 | return r; |
d889a206 | 1499 | |
df2d202e | 1500 | return sd_bus_reply_method_return(message, NULL); |
eecd1362 LP |
1501 | } |
1502 | ||
ebcf1f97 | 1503 | static int method_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
3f49d45a LP |
1504 | Manager *m = userdata; |
1505 | ||
cc377381 LP |
1506 | return method_do_shutdown_or_sleep( |
1507 | m, message, | |
1508 | SPECIAL_POWEROFF_TARGET, | |
1509 | INHIBIT_SHUTDOWN, | |
1510 | "org.freedesktop.login1.power-off", | |
1511 | "org.freedesktop.login1.power-off-multiple-sessions", | |
1512 | "org.freedesktop.login1.power-off-ignore-inhibit", | |
1513 | NULL, | |
ebcf1f97 LP |
1514 | method_poweroff, |
1515 | error); | |
cc377381 | 1516 | } |
88e3dc90 | 1517 | |
ebcf1f97 | 1518 | static int method_reboot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 1519 | Manager *m = userdata; |
88e3dc90 | 1520 | |
cc377381 LP |
1521 | return method_do_shutdown_or_sleep( |
1522 | m, message, | |
1523 | SPECIAL_REBOOT_TARGET, | |
1524 | INHIBIT_SHUTDOWN, | |
1525 | "org.freedesktop.login1.reboot", | |
1526 | "org.freedesktop.login1.reboot-multiple-sessions", | |
1527 | "org.freedesktop.login1.reboot-ignore-inhibit", | |
1528 | NULL, | |
ebcf1f97 LP |
1529 | method_reboot, |
1530 | error); | |
cc377381 | 1531 | } |
88e3dc90 | 1532 | |
ebcf1f97 | 1533 | static int method_suspend(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 1534 | Manager *m = userdata; |
88e3dc90 | 1535 | |
cc377381 LP |
1536 | return method_do_shutdown_or_sleep( |
1537 | m, message, | |
1538 | SPECIAL_SUSPEND_TARGET, | |
1539 | INHIBIT_SLEEP, | |
1540 | "org.freedesktop.login1.suspend", | |
1541 | "org.freedesktop.login1.suspend-multiple-sessions", | |
1542 | "org.freedesktop.login1.suspend-ignore-inhibit", | |
1543 | "suspend", | |
ebcf1f97 LP |
1544 | method_suspend, |
1545 | error); | |
cc377381 | 1546 | } |
88e3dc90 | 1547 | |
ebcf1f97 | 1548 | static int method_hibernate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 1549 | Manager *m = userdata; |
b6160029 | 1550 | |
cc377381 LP |
1551 | return method_do_shutdown_or_sleep( |
1552 | m, message, | |
1553 | SPECIAL_HIBERNATE_TARGET, | |
1554 | INHIBIT_SLEEP, | |
1555 | "org.freedesktop.login1.hibernate", | |
1556 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
1557 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
1558 | "hibernate", | |
ebcf1f97 LP |
1559 | method_hibernate, |
1560 | error); | |
cc377381 | 1561 | } |
fa2b196d | 1562 | |
ebcf1f97 | 1563 | static int method_hybrid_sleep(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 1564 | Manager *m = userdata; |
fa2b196d | 1565 | |
cc377381 LP |
1566 | return method_do_shutdown_or_sleep( |
1567 | m, message, | |
1568 | SPECIAL_HYBRID_SLEEP_TARGET, | |
1569 | INHIBIT_SLEEP, | |
1570 | "org.freedesktop.login1.hibernate", | |
1571 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
1572 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
1573 | "hybrid-sleep", | |
ebcf1f97 LP |
1574 | method_hybrid_sleep, |
1575 | error); | |
cc377381 | 1576 | } |
de07ab16 | 1577 | |
cc377381 LP |
1578 | static int method_can_shutdown_or_sleep( |
1579 | Manager *m, | |
1580 | sd_bus_message *message, | |
1581 | InhibitWhat w, | |
1582 | const char *action, | |
1583 | const char *action_multiple_sessions, | |
1584 | const char *action_ignore_inhibit, | |
ebcf1f97 LP |
1585 | const char *sleep_verb, |
1586 | sd_bus_error *error) { | |
de07ab16 | 1587 | |
5b12334d | 1588 | _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; |
cc377381 LP |
1589 | bool multiple_sessions, challenge, blocked; |
1590 | const char *result = NULL; | |
1591 | uid_t uid; | |
1592 | int r; | |
de07ab16 | 1593 | |
cc377381 LP |
1594 | assert(m); |
1595 | assert(message); | |
1596 | assert(w >= 0); | |
1597 | assert(w <= _INHIBIT_WHAT_MAX); | |
1598 | assert(action); | |
1599 | assert(action_multiple_sessions); | |
1600 | assert(action_ignore_inhibit); | |
de07ab16 | 1601 | |
cc377381 LP |
1602 | if (sleep_verb) { |
1603 | r = can_sleep(sleep_verb); | |
de07ab16 | 1604 | if (r < 0) |
ebcf1f97 | 1605 | return r; |
cc377381 | 1606 | if (r == 0) |
df2d202e | 1607 | return sd_bus_reply_method_return(message, "s", "na"); |
cc377381 | 1608 | } |
de07ab16 | 1609 | |
5b12334d LP |
1610 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_UID, &creds); |
1611 | if (r < 0) | |
1612 | return r; | |
1613 | ||
1614 | r = sd_bus_creds_get_uid(creds, &uid); | |
cc377381 | 1615 | if (r < 0) |
ebcf1f97 | 1616 | return r; |
de07ab16 | 1617 | |
cc377381 LP |
1618 | r = have_multiple_sessions(m, uid); |
1619 | if (r < 0) | |
ebcf1f97 | 1620 | return r; |
de07ab16 | 1621 | |
cc377381 | 1622 | multiple_sessions = r > 0; |
85a428c6 | 1623 | blocked = manager_is_inhibited(m, w, INHIBIT_BLOCK, NULL, false, true, uid, NULL); |
de07ab16 | 1624 | |
cc377381 | 1625 | if (multiple_sessions) { |
ebcf1f97 | 1626 | r = bus_verify_polkit(m->bus, message, action_multiple_sessions, false, &challenge, error); |
de07ab16 | 1627 | if (r < 0) |
ebcf1f97 | 1628 | return r; |
bef422ae | 1629 | |
cc377381 LP |
1630 | if (r > 0) |
1631 | result = "yes"; | |
1632 | else if (challenge) | |
1633 | result = "challenge"; | |
1634 | else | |
1635 | result = "no"; | |
1636 | } | |
bef422ae | 1637 | |
cc377381 | 1638 | if (blocked) { |
ebcf1f97 | 1639 | r = bus_verify_polkit(m->bus, message, action_ignore_inhibit, false, &challenge, error); |
bef422ae | 1640 | if (r < 0) |
ebcf1f97 | 1641 | return r; |
bef422ae | 1642 | |
cc377381 LP |
1643 | if (r > 0 && !result) |
1644 | result = "yes"; | |
1645 | else if (challenge && (!result || streq(result, "yes"))) | |
1646 | result = "challenge"; | |
1647 | else | |
1648 | result = "no"; | |
1649 | } | |
bef422ae | 1650 | |
cc377381 LP |
1651 | if (!multiple_sessions && !blocked) { |
1652 | /* If neither inhibit nor multiple sessions | |
1653 | * apply then just check the normal policy */ | |
bef422ae | 1654 | |
ebcf1f97 | 1655 | r = bus_verify_polkit(m->bus, message, action, false, &challenge, error); |
bef422ae | 1656 | if (r < 0) |
ebcf1f97 | 1657 | return r; |
bef422ae | 1658 | |
cc377381 LP |
1659 | if (r > 0) |
1660 | result = "yes"; | |
1661 | else if (challenge) | |
1662 | result = "challenge"; | |
1663 | else | |
1664 | result = "no"; | |
1665 | } | |
bef422ae | 1666 | |
df2d202e | 1667 | return sd_bus_reply_method_return(message, "s", result); |
cc377381 | 1668 | } |
bef422ae | 1669 | |
ebcf1f97 | 1670 | static int method_can_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 1671 | Manager *m = userdata; |
bef422ae | 1672 | |
cc377381 LP |
1673 | return method_can_shutdown_or_sleep( |
1674 | m, message, | |
1675 | INHIBIT_SHUTDOWN, | |
1676 | "org.freedesktop.login1.power-off", | |
1677 | "org.freedesktop.login1.power-off-multiple-sessions", | |
1678 | "org.freedesktop.login1.power-off-ignore-inhibit", | |
ebcf1f97 LP |
1679 | NULL, |
1680 | error); | |
cc377381 | 1681 | } |
bef422ae | 1682 | |
ebcf1f97 | 1683 | static int method_can_reboot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 1684 | Manager *m = userdata; |
bef422ae | 1685 | |
cc377381 LP |
1686 | return method_can_shutdown_or_sleep( |
1687 | m, message, | |
1688 | INHIBIT_SHUTDOWN, | |
1689 | "org.freedesktop.login1.reboot", | |
1690 | "org.freedesktop.login1.reboot-multiple-sessions", | |
1691 | "org.freedesktop.login1.reboot-ignore-inhibit", | |
ebcf1f97 LP |
1692 | NULL, |
1693 | error); | |
cc377381 | 1694 | } |
bef422ae | 1695 | |
ebcf1f97 | 1696 | static int method_can_suspend(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 1697 | Manager *m = userdata; |
7f7bb946 | 1698 | |
cc377381 LP |
1699 | return method_can_shutdown_or_sleep( |
1700 | m, message, | |
1701 | INHIBIT_SLEEP, | |
1702 | "org.freedesktop.login1.suspend", | |
1703 | "org.freedesktop.login1.suspend-multiple-sessions", | |
1704 | "org.freedesktop.login1.suspend-ignore-inhibit", | |
ebcf1f97 LP |
1705 | "suspend", |
1706 | error); | |
cc377381 | 1707 | } |
7f7bb946 | 1708 | |
ebcf1f97 | 1709 | static int method_can_hibernate(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 1710 | Manager *m = userdata; |
02b16a19 | 1711 | |
cc377381 LP |
1712 | return method_can_shutdown_or_sleep( |
1713 | m, message, | |
1714 | INHIBIT_SLEEP, | |
1715 | "org.freedesktop.login1.hibernate", | |
1716 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
1717 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
ebcf1f97 LP |
1718 | "hibernate", |
1719 | error); | |
cc377381 | 1720 | } |
7f7bb946 | 1721 | |
ebcf1f97 | 1722 | static int method_can_hybrid_sleep(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 1723 | Manager *m = userdata; |
7f7bb946 | 1724 | |
cc377381 LP |
1725 | return method_can_shutdown_or_sleep( |
1726 | m, message, | |
1727 | INHIBIT_SLEEP, | |
1728 | "org.freedesktop.login1.hibernate", | |
1729 | "org.freedesktop.login1.hibernate-multiple-sessions", | |
1730 | "org.freedesktop.login1.hibernate-ignore-inhibit", | |
ebcf1f97 LP |
1731 | "hybrid-sleep", |
1732 | error); | |
cc377381 | 1733 | } |
38f3fc7d | 1734 | |
ebcf1f97 | 1735 | static int method_inhibit(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
5b12334d | 1736 | _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; |
cc377381 LP |
1737 | const char *who, *why, *what, *mode; |
1738 | _cleanup_free_ char *id = NULL; | |
1739 | _cleanup_close_ int fifo_fd = -1; | |
1740 | Manager *m = userdata; | |
1741 | Inhibitor *i = NULL; | |
1742 | InhibitMode mm; | |
1743 | InhibitWhat w; | |
1744 | pid_t pid; | |
1745 | uid_t uid; | |
1746 | int r; | |
7f7bb946 | 1747 | |
cc377381 LP |
1748 | assert(bus); |
1749 | assert(message); | |
1750 | assert(m); | |
38f3fc7d | 1751 | |
cc377381 LP |
1752 | r = sd_bus_message_read(message, "ssss", &what, &who, &why, &mode); |
1753 | if (r < 0) | |
ebcf1f97 | 1754 | return r; |
38f3fc7d | 1755 | |
cc377381 LP |
1756 | w = inhibit_what_from_string(what); |
1757 | if (w <= 0) | |
ebcf1f97 | 1758 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid what specification %s", what); |
38f3fc7d | 1759 | |
cc377381 LP |
1760 | mm = inhibit_mode_from_string(mode); |
1761 | if (mm < 0) | |
ebcf1f97 | 1762 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid mode specification %s", mode); |
7f7bb946 | 1763 | |
cc377381 LP |
1764 | /* Delay is only supported for shutdown/sleep */ |
1765 | if (mm == INHIBIT_DELAY && (w & ~(INHIBIT_SHUTDOWN|INHIBIT_SLEEP))) | |
ebcf1f97 | 1766 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Delay inhibitors only supported for shutdown and sleep"); |
38f3fc7d | 1767 | |
cc377381 LP |
1768 | /* Don't allow taking delay locks while we are already |
1769 | * executing the operation. We shouldn't create the impression | |
1770 | * that the lock was successful if the machine is about to go | |
1771 | * down/suspend any moment. */ | |
1772 | if (m->action_what & w) | |
ebcf1f97 | 1773 | return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "The operation inhibition has been requested for is already running"); |
cc377381 LP |
1774 | |
1775 | r = bus_verify_polkit_async(bus, &m->polkit_registry, message, | |
1776 | w == INHIBIT_SHUTDOWN ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-shutdown" : "org.freedesktop.login1.inhibit-delay-shutdown") : | |
1777 | w == INHIBIT_SLEEP ? (mm == INHIBIT_BLOCK ? "org.freedesktop.login1.inhibit-block-sleep" : "org.freedesktop.login1.inhibit-delay-sleep") : | |
1778 | w == INHIBIT_IDLE ? "org.freedesktop.login1.inhibit-block-idle" : | |
1779 | w == INHIBIT_HANDLE_POWER_KEY ? "org.freedesktop.login1.inhibit-handle-power-key" : | |
1780 | w == INHIBIT_HANDLE_SUSPEND_KEY ? "org.freedesktop.login1.inhibit-handle-suspend-key" : | |
1781 | w == INHIBIT_HANDLE_HIBERNATE_KEY ? "org.freedesktop.login1.inhibit-handle-hibernate-key" : | |
1782 | "org.freedesktop.login1.inhibit-handle-lid-switch", | |
ebcf1f97 | 1783 | false, error, method_inhibit, m); |
cc377381 | 1784 | if (r < 0) |
ebcf1f97 | 1785 | return r; |
cc377381 LP |
1786 | if (r == 0) |
1787 | return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */ | |
7f7bb946 | 1788 | |
5b12334d LP |
1789 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_UID|SD_BUS_CREDS_PID, &creds); |
1790 | if (r < 0) | |
1791 | return r; | |
1792 | ||
1793 | r = sd_bus_creds_get_uid(creds, &uid); | |
cc377381 | 1794 | if (r < 0) |
ebcf1f97 | 1795 | return r; |
7f7bb946 | 1796 | |
5b12334d | 1797 | r = sd_bus_creds_get_pid(creds, &pid); |
cc377381 | 1798 | if (r < 0) |
ebcf1f97 | 1799 | return r; |
47a26690 | 1800 | |
cc377381 LP |
1801 | do { |
1802 | free(id); | |
1803 | id = NULL; | |
47a26690 | 1804 | |
cc377381 | 1805 | if (asprintf(&id, "%lu", ++m->inhibit_counter) < 0) |
ebcf1f97 | 1806 | return -ENOMEM; |
47a26690 | 1807 | |
cc377381 | 1808 | } while (hashmap_get(m->inhibitors, id)); |
47a26690 | 1809 | |
cc377381 LP |
1810 | r = manager_add_inhibitor(m, id, &i); |
1811 | if (r < 0) | |
ebcf1f97 | 1812 | return r; |
47a26690 | 1813 | |
cc377381 LP |
1814 | i->what = w; |
1815 | i->mode = mm; | |
1816 | i->pid = pid; | |
1817 | i->uid = uid; | |
1818 | i->why = strdup(why); | |
1819 | i->who = strdup(who); | |
7f7bb946 | 1820 | |
cc377381 | 1821 | if (!i->why || !i->who) { |
ebcf1f97 | 1822 | r = -ENOMEM; |
cc377381 LP |
1823 | goto fail; |
1824 | } | |
b668e064 | 1825 | |
cc377381 LP |
1826 | fifo_fd = inhibitor_create_fifo(i); |
1827 | if (fifo_fd < 0) { | |
ebcf1f97 | 1828 | r = fifo_fd; |
cc377381 LP |
1829 | goto fail; |
1830 | } | |
b668e064 | 1831 | |
cc377381 | 1832 | inhibitor_start(i); |
b668e064 | 1833 | |
df2d202e | 1834 | return sd_bus_reply_method_return(message, "h", fifo_fd); |
b668e064 | 1835 | |
cc377381 LP |
1836 | fail: |
1837 | if (i) | |
1838 | inhibitor_free(i); | |
89f13440 | 1839 | |
cc377381 LP |
1840 | return r; |
1841 | } | |
3f49d45a | 1842 | |
cc377381 LP |
1843 | const sd_bus_vtable manager_vtable[] = { |
1844 | SD_BUS_VTABLE_START(0), | |
1845 | ||
556089dc LP |
1846 | SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST), |
1847 | SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST), | |
1848 | SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST), | |
1849 | SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST), | |
cc377381 LP |
1850 | SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
1851 | SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
1852 | SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
1853 | SD_BUS_PROPERTY("BlockInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
1854 | SD_BUS_PROPERTY("DelayInhibited", "s", property_get_inhibited, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), | |
556089dc LP |
1855 | SD_BUS_PROPERTY("InhibitDelayMaxUSec", "t", NULL, offsetof(Manager, inhibit_delay_max), SD_BUS_VTABLE_PROPERTY_CONST), |
1856 | SD_BUS_PROPERTY("HandlePowerKey", "s", property_get_handle_action, offsetof(Manager, handle_power_key), SD_BUS_VTABLE_PROPERTY_CONST), | |
1857 | SD_BUS_PROPERTY("HandleSuspendKey", "s", property_get_handle_action, offsetof(Manager, handle_suspend_key), SD_BUS_VTABLE_PROPERTY_CONST), | |
1858 | SD_BUS_PROPERTY("HandleHibernateKey", "s", property_get_handle_action, offsetof(Manager, handle_hibernate_key), SD_BUS_VTABLE_PROPERTY_CONST), | |
1859 | SD_BUS_PROPERTY("HandleLidSwitch", "s", property_get_handle_action, offsetof(Manager, handle_lid_switch), SD_BUS_VTABLE_PROPERTY_CONST), | |
1860 | SD_BUS_PROPERTY("IdleAction", "s", property_get_handle_action, offsetof(Manager, idle_action), SD_BUS_VTABLE_PROPERTY_CONST), | |
1861 | SD_BUS_PROPERTY("IdleActionUSec", "t", NULL, offsetof(Manager, idle_action_usec), SD_BUS_VTABLE_PROPERTY_CONST), | |
cc377381 LP |
1862 | SD_BUS_PROPERTY("PreparingForShutdown", "b", property_get_preparing, 0, 0), |
1863 | SD_BUS_PROPERTY("PreparingForSleep", "b", property_get_preparing, 0, 0), | |
1864 | ||
adacb957 LP |
1865 | SD_BUS_METHOD("GetSession", "s", "o", method_get_session, SD_BUS_VTABLE_UNPRIVILEGED), |
1866 | SD_BUS_METHOD("GetSessionByPID", "u", "o", method_get_session_by_pid, SD_BUS_VTABLE_UNPRIVILEGED), | |
1867 | SD_BUS_METHOD("GetUser", "u", "o", method_get_user, SD_BUS_VTABLE_UNPRIVILEGED), | |
1868 | SD_BUS_METHOD("GetUserByPID", "u", "o", method_get_user_by_pid, SD_BUS_VTABLE_UNPRIVILEGED), | |
1869 | SD_BUS_METHOD("GetSeat", "s", "o", method_get_seat, SD_BUS_VTABLE_UNPRIVILEGED), | |
1870 | SD_BUS_METHOD("ListSessions", NULL, "a(susso)", method_list_sessions, SD_BUS_VTABLE_UNPRIVILEGED), | |
1871 | SD_BUS_METHOD("ListUsers", NULL, "a(uso)", method_list_users, SD_BUS_VTABLE_UNPRIVILEGED), | |
1872 | SD_BUS_METHOD("ListSeats", NULL, "a(so)", method_list_seats, SD_BUS_VTABLE_UNPRIVILEGED), | |
1873 | SD_BUS_METHOD("ListInhibitors", NULL, "a(ssssuu)", method_list_inhibitors, SD_BUS_VTABLE_UNPRIVILEGED), | |
a4cd87e9 | 1874 | SD_BUS_METHOD("CreateSession", "uusssssussbssa(sv)", "soshusub", method_create_session, 0), |
cc377381 | 1875 | SD_BUS_METHOD("ReleaseSession", "s", NULL, method_release_session, 0), |
adacb957 LP |
1876 | SD_BUS_METHOD("ActivateSession", "s", NULL, method_activate_session, SD_BUS_VTABLE_UNPRIVILEGED), |
1877 | SD_BUS_METHOD("ActivateSessionOnSeat", "ss", NULL, method_activate_session_on_seat, SD_BUS_VTABLE_UNPRIVILEGED), | |
cc377381 LP |
1878 | SD_BUS_METHOD("LockSession", "s", NULL, method_lock_session, 0), |
1879 | SD_BUS_METHOD("UnlockSession", "s", NULL, method_lock_session, 0), | |
1880 | SD_BUS_METHOD("LockSessions", NULL, NULL, method_lock_sessions, 0), | |
1881 | SD_BUS_METHOD("UnlockSessions", NULL, NULL, method_lock_sessions, 0), | |
adacb957 LP |
1882 | SD_BUS_METHOD("KillSession", "ssi", NULL, method_kill_session, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)), |
1883 | SD_BUS_METHOD("KillUser", "ui", NULL, method_kill_user, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)), | |
1884 | SD_BUS_METHOD("TerminateSession", "s", NULL, method_terminate_session, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)), | |
1885 | SD_BUS_METHOD("TerminateUser", "u", NULL, method_terminate_user, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)), | |
1886 | SD_BUS_METHOD("TerminateSeat", "s", NULL, method_terminate_seat, SD_BUS_VTABLE_CAPABILITY(CAP_KILL)), | |
1887 | SD_BUS_METHOD("SetUserLinger", "ubb", NULL, method_set_user_linger, SD_BUS_VTABLE_UNPRIVILEGED), | |
1888 | SD_BUS_METHOD("AttachDevice", "ssb", NULL, method_attach_device, SD_BUS_VTABLE_UNPRIVILEGED), | |
1889 | SD_BUS_METHOD("FlushDevices", "b", NULL, method_flush_devices, SD_BUS_VTABLE_UNPRIVILEGED), | |
1890 | SD_BUS_METHOD("PowerOff", "b", NULL, method_poweroff, SD_BUS_VTABLE_UNPRIVILEGED), | |
1891 | SD_BUS_METHOD("Reboot", "b", NULL, method_reboot, SD_BUS_VTABLE_UNPRIVILEGED), | |
1892 | SD_BUS_METHOD("Suspend", "b", NULL, method_suspend, SD_BUS_VTABLE_UNPRIVILEGED), | |
1893 | SD_BUS_METHOD("Hibernate", "b", NULL, method_hibernate, SD_BUS_VTABLE_UNPRIVILEGED), | |
1894 | SD_BUS_METHOD("HybridSleep", "b", NULL, method_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED), | |
1895 | SD_BUS_METHOD("CanPowerOff", NULL, "s", method_can_poweroff, SD_BUS_VTABLE_UNPRIVILEGED), | |
1896 | SD_BUS_METHOD("CanReboot", NULL, "s", method_can_reboot, SD_BUS_VTABLE_UNPRIVILEGED), | |
1897 | SD_BUS_METHOD("CanSuspend", NULL, "s", method_can_suspend, SD_BUS_VTABLE_UNPRIVILEGED), | |
1898 | SD_BUS_METHOD("CanHibernate", NULL, "s", method_can_hibernate, SD_BUS_VTABLE_UNPRIVILEGED), | |
1899 | SD_BUS_METHOD("CanHybridSleep", NULL, "s", method_can_hybrid_sleep, SD_BUS_VTABLE_UNPRIVILEGED), | |
1900 | SD_BUS_METHOD("Inhibit", "ssss", "h", method_inhibit, SD_BUS_VTABLE_UNPRIVILEGED), | |
cc377381 LP |
1901 | |
1902 | SD_BUS_SIGNAL("SessionNew", "so", 0), | |
1903 | SD_BUS_SIGNAL("SessionRemoved", "so", 0), | |
1904 | SD_BUS_SIGNAL("UserNew", "uo", 0), | |
1905 | SD_BUS_SIGNAL("UserRemoved", "uo", 0), | |
1906 | SD_BUS_SIGNAL("SeatNew", "so", 0), | |
1907 | SD_BUS_SIGNAL("SeatRemoved", "so", 0), | |
1908 | SD_BUS_SIGNAL("PrepareForShutdown", "b", 0), | |
1909 | SD_BUS_SIGNAL("PrepareForSleep", "b", 0), | |
1910 | ||
1911 | SD_BUS_VTABLE_END | |
1912 | }; | |
3f49d45a | 1913 | |
99e7e392 DH |
1914 | static int session_jobs_reply(Session *s, const char *unit, const char *result) { |
1915 | int r = 0; | |
1916 | ||
1917 | assert(s); | |
1918 | assert(unit); | |
1919 | ||
1920 | if (!s->started) | |
1921 | return r; | |
1922 | ||
1923 | if (streq(result, "done")) | |
1924 | r = session_send_create_reply(s, NULL); | |
1925 | else { | |
1926 | _cleanup_bus_error_free_ sd_bus_error e = SD_BUS_ERROR_NULL; | |
1927 | ||
1928 | sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result); | |
1929 | r = session_send_create_reply(s, &e); | |
1930 | } | |
1931 | ||
1932 | return r; | |
1933 | } | |
1934 | ||
ebcf1f97 | 1935 | int match_job_removed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
1936 | const char *path, *result, *unit; |
1937 | Manager *m = userdata; | |
1938 | Session *session; | |
1939 | uint32_t id; | |
1940 | User *user; | |
1941 | int r; | |
3f49d45a | 1942 | |
cc377381 LP |
1943 | assert(bus); |
1944 | assert(message); | |
1945 | assert(m); | |
3f49d45a | 1946 | |
cc377381 LP |
1947 | r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result); |
1948 | if (r < 0) { | |
ebcf1f97 LP |
1949 | bus_log_parse_error(r); |
1950 | return r; | |
cc377381 | 1951 | } |
3f49d45a | 1952 | |
cc377381 LP |
1953 | if (m->action_job && streq(m->action_job, path)) { |
1954 | log_info("Operation finished."); | |
3f49d45a | 1955 | |
cc377381 LP |
1956 | /* Tell people that they now may take a lock again */ |
1957 | send_prepare_for(m, m->action_what, false); | |
3f49d45a | 1958 | |
cc377381 LP |
1959 | free(m->action_job); |
1960 | m->action_job = NULL; | |
1961 | m->action_unit = NULL; | |
1962 | m->action_what = 0; | |
1963 | return 0; | |
1964 | } | |
3f49d45a | 1965 | |
cc377381 LP |
1966 | session = hashmap_get(m->session_units, unit); |
1967 | if (session) { | |
3f49d45a | 1968 | |
cc377381 LP |
1969 | if (streq_ptr(path, session->scope_job)) { |
1970 | free(session->scope_job); | |
1971 | session->scope_job = NULL; | |
3f49d45a LP |
1972 | } |
1973 | ||
99e7e392 | 1974 | session_jobs_reply(session, unit, result); |
3f49d45a | 1975 | |
99e7e392 | 1976 | session_save(session); |
cc377381 LP |
1977 | session_add_to_gc_queue(session); |
1978 | } | |
3f49d45a | 1979 | |
cc377381 LP |
1980 | user = hashmap_get(m->user_units, unit); |
1981 | if (user) { | |
3f49d45a | 1982 | |
cc377381 LP |
1983 | if (streq_ptr(path, user->service_job)) { |
1984 | free(user->service_job); | |
1985 | user->service_job = NULL; | |
3f49d45a LP |
1986 | } |
1987 | ||
cc377381 LP |
1988 | if (streq_ptr(path, user->slice_job)) { |
1989 | free(user->slice_job); | |
1990 | user->slice_job = NULL; | |
1991 | } | |
3f49d45a | 1992 | |
dd9b67aa | 1993 | LIST_FOREACH(sessions_by_user, session, user->sessions) { |
99e7e392 | 1994 | session_jobs_reply(session, unit, result); |
dd9b67aa LP |
1995 | } |
1996 | ||
cc377381 LP |
1997 | user_save(user); |
1998 | user_add_to_gc_queue(user); | |
3f49d45a LP |
1999 | } |
2000 | ||
cc377381 | 2001 | return 0; |
3f49d45a LP |
2002 | } |
2003 | ||
ebcf1f97 | 2004 | int match_unit_removed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 | 2005 | const char *path, *unit; |
1713813d | 2006 | Manager *m = userdata; |
cc377381 LP |
2007 | Session *session; |
2008 | User *user; | |
2009 | int r; | |
1713813d | 2010 | |
cc377381 | 2011 | assert(bus); |
1713813d | 2012 | assert(message); |
cc377381 | 2013 | assert(m); |
1713813d | 2014 | |
cc377381 LP |
2015 | r = sd_bus_message_read(message, "so", &unit, &path); |
2016 | if (r < 0) { | |
ebcf1f97 LP |
2017 | bus_log_parse_error(r); |
2018 | return r; | |
cc377381 | 2019 | } |
fb6becb4 | 2020 | |
cc377381 LP |
2021 | session = hashmap_get(m->session_units, unit); |
2022 | if (session) | |
2023 | session_add_to_gc_queue(session); | |
fb6becb4 | 2024 | |
cc377381 LP |
2025 | user = hashmap_get(m->user_units, unit); |
2026 | if (user) | |
2027 | user_add_to_gc_queue(user); | |
fb6becb4 | 2028 | |
cc377381 LP |
2029 | return 0; |
2030 | } | |
fb6becb4 | 2031 | |
ebcf1f97 | 2032 | int match_properties_changed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
2033 | _cleanup_free_ char *unit = NULL; |
2034 | Manager *m = userdata; | |
2035 | const char *path; | |
2036 | Session *session; | |
2037 | User *user; | |
ebcf1f97 | 2038 | int r; |
fb6becb4 | 2039 | |
cc377381 LP |
2040 | assert(bus); |
2041 | assert(message); | |
2042 | assert(m); | |
fb6becb4 | 2043 | |
cc377381 LP |
2044 | path = sd_bus_message_get_path(message); |
2045 | if (!path) | |
2046 | return 0; | |
fb6becb4 | 2047 | |
ebcf1f97 LP |
2048 | r = unit_name_from_dbus_path(path, &unit); |
2049 | if (r < 0) | |
2050 | return r; | |
fb6becb4 | 2051 | |
cc377381 LP |
2052 | session = hashmap_get(m->session_units, unit); |
2053 | if (session) | |
2054 | session_add_to_gc_queue(session); | |
fb6becb4 | 2055 | |
cc377381 LP |
2056 | user = hashmap_get(m->user_units, unit); |
2057 | if (user) | |
2058 | user_add_to_gc_queue(user); | |
fb6becb4 | 2059 | |
cc377381 LP |
2060 | return 0; |
2061 | } | |
6fa48533 | 2062 | |
ebcf1f97 | 2063 | int match_reloading(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
2064 | Manager *m = userdata; |
2065 | Session *session; | |
2066 | Iterator i; | |
2067 | int b, r; | |
943aca8e | 2068 | |
cc377381 | 2069 | assert(bus); |
943aca8e | 2070 | |
cc377381 LP |
2071 | r = sd_bus_message_read(message, "b", &b); |
2072 | if (r < 0) { | |
ebcf1f97 LP |
2073 | bus_log_parse_error(r); |
2074 | return r; | |
cc377381 | 2075 | } |
943aca8e | 2076 | |
cc377381 LP |
2077 | if (b) |
2078 | return 0; | |
943aca8e | 2079 | |
cc377381 LP |
2080 | /* systemd finished reloading, let's recheck all our sessions */ |
2081 | log_debug("System manager has been reloaded, rechecking sessions..."); | |
6797c324 | 2082 | |
cc377381 LP |
2083 | HASHMAP_FOREACH(session, m->sessions, i) |
2084 | session_add_to_gc_queue(session); | |
6797c324 | 2085 | |
cc377381 LP |
2086 | return 0; |
2087 | } | |
943aca8e | 2088 | |
ebcf1f97 | 2089 | int match_name_owner_changed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { |
cc377381 LP |
2090 | const char *name, *old, *new; |
2091 | Manager *m = userdata; | |
2092 | Session *session; | |
2093 | Iterator i; | |
2094 | int r; | |
943aca8e | 2095 | |
6797c324 | 2096 | |
cc377381 | 2097 | char *key; |
e8b212fe | 2098 | |
cc377381 LP |
2099 | r = sd_bus_message_read(message, "sss", &name, &old, &new); |
2100 | if (r < 0) { | |
ebcf1f97 LP |
2101 | bus_log_parse_error(r); |
2102 | return r; | |
cc377381 | 2103 | } |
e8b212fe | 2104 | |
cc377381 LP |
2105 | if (isempty(old) || !isempty(new)) |
2106 | return 0; | |
e8b212fe | 2107 | |
cc377381 LP |
2108 | key = set_remove(m->busnames, (char*) old); |
2109 | if (!key) | |
2110 | return 0; | |
ae5e06bd | 2111 | |
cc377381 | 2112 | /* Drop all controllers owned by this name */ |
ae5e06bd | 2113 | |
cc377381 | 2114 | free(key); |
1713813d | 2115 | |
cc377381 LP |
2116 | HASHMAP_FOREACH(session, m->sessions, i) |
2117 | if (session_is_controller(session, old)) | |
2118 | session_drop_controller(session); | |
1713813d | 2119 | |
cc377381 | 2120 | return 0; |
1713813d LP |
2121 | } |
2122 | ||
cc377381 LP |
2123 | int manager_send_changed(Manager *manager, const char *property, ...) { |
2124 | char **l; | |
9418f147 LP |
2125 | |
2126 | assert(manager); | |
2127 | ||
cc377381 | 2128 | l = strv_from_stdarg_alloca(property); |
9418f147 | 2129 | |
cc377381 LP |
2130 | return sd_bus_emit_properties_changed_strv( |
2131 | manager->bus, | |
2132 | "/org/freedesktop/login1", | |
2133 | "org.freedesktop.login1.Manager", | |
2134 | l); | |
9418f147 | 2135 | } |
eecd1362 | 2136 | |
d889a206 | 2137 | int manager_dispatch_delayed(Manager *manager) { |
cc377381 | 2138 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; |
85a428c6 | 2139 | Inhibitor *offending = NULL; |
eecd1362 LP |
2140 | int r; |
2141 | ||
2142 | assert(manager); | |
2143 | ||
84286536 | 2144 | if (manager->action_what == 0 || manager->action_job) |
eecd1362 LP |
2145 | return 0; |
2146 | ||
2147 | /* Continue delay? */ | |
85a428c6 LP |
2148 | if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) { |
2149 | _cleanup_free_ char *comm = NULL, *u = NULL; | |
2150 | ||
2151 | get_process_comm(offending->pid, &comm); | |
2152 | u = uid_to_name(offending->uid); | |
eecd1362 | 2153 | |
314b4b0a LP |
2154 | if (manager->action_timestamp + manager->inhibit_delay_max > now(CLOCK_MONOTONIC)) |
2155 | return 0; | |
af9792ac | 2156 | |
85a428c6 LP |
2157 | log_info("Delay lock is active (UID %lu/%s, PID %lu/%s) but inhibitor timeout is reached.", |
2158 | (unsigned long) offending->uid, strna(u), | |
2159 | (unsigned long) offending->pid, strna(comm)); | |
314b4b0a | 2160 | } |
eecd1362 | 2161 | |
314b4b0a | 2162 | /* Actually do the operation */ |
314b4b0a | 2163 | r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error); |
eecd1362 | 2164 | if (r < 0) { |
cc377381 | 2165 | log_warning("Failed to send delayed message: %s", bus_error_message(&error, r)); |
314b4b0a LP |
2166 | |
2167 | manager->action_unit = NULL; | |
2168 | manager->action_what = 0; | |
eecd1362 LP |
2169 | return r; |
2170 | } | |
2171 | ||
eecd1362 LP |
2172 | return 1; |
2173 | } | |
fb6becb4 LP |
2174 | |
2175 | int manager_start_scope( | |
2176 | Manager *manager, | |
2177 | const char *scope, | |
2178 | pid_t pid, | |
2179 | const char *slice, | |
2180 | const char *description, | |
ba4c5d93 | 2181 | const char *after, const char *after2, |
cc377381 | 2182 | sd_bus_error *error, |
fb6becb4 LP |
2183 | char **job) { |
2184 | ||
cc377381 LP |
2185 | _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL; |
2186 | int r; | |
fb6becb4 LP |
2187 | |
2188 | assert(manager); | |
2189 | assert(scope); | |
2190 | assert(pid > 1); | |
2191 | ||
cc377381 LP |
2192 | r = sd_bus_message_new_method_call( |
2193 | manager->bus, | |
fb6becb4 LP |
2194 | "org.freedesktop.systemd1", |
2195 | "/org/freedesktop/systemd1", | |
2196 | "org.freedesktop.systemd1.Manager", | |
cc377381 LP |
2197 | "StartTransientUnit", |
2198 | &m); | |
2199 | if (r < 0) | |
2200 | return r; | |
fb6becb4 | 2201 | |
cc377381 LP |
2202 | r = sd_bus_message_append(m, "ss", strempty(scope), "fail"); |
2203 | if (r < 0) | |
2204 | return r; | |
fb6becb4 | 2205 | |
cc377381 LP |
2206 | r = sd_bus_message_open_container(m, 'a', "(sv)"); |
2207 | if (r < 0) | |
2208 | return r; | |
fb6becb4 LP |
2209 | |
2210 | if (!isempty(slice)) { | |
cc377381 LP |
2211 | r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice); |
2212 | if (r < 0) | |
2213 | return r; | |
fb6becb4 LP |
2214 | } |
2215 | ||
2216 | if (!isempty(description)) { | |
cc377381 LP |
2217 | r = sd_bus_message_append(m, "(sv)", "Description", "s", description); |
2218 | if (r < 0) | |
2219 | return r; | |
fb6becb4 LP |
2220 | } |
2221 | ||
ba4c5d93 | 2222 | if (!isempty(after)) { |
cc377381 LP |
2223 | r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after); |
2224 | if (r < 0) | |
2225 | return r; | |
7fb3ee51 LP |
2226 | } |
2227 | ||
ba4c5d93 LP |
2228 | if (!isempty(after2)) { |
2229 | r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2); | |
2230 | if (r < 0) | |
2231 | return r; | |
2232 | } | |
2233 | ||
fb6becb4 LP |
2234 | /* cgroup empty notification is not available in containers |
2235 | * currently. To make this less problematic, let's shorten the | |
2236 | * stop timeout for sessions, so that we don't wait | |
2237 | * forever. */ | |
2238 | ||
743e8945 LP |
2239 | /* Make sure that the session shells are terminated with |
2240 | * SIGHUP since bash and friends tend to ignore SIGTERM */ | |
cc377381 LP |
2241 | r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true); |
2242 | if (r < 0) | |
2243 | return r; | |
2244 | ||
2245 | r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid); | |
2246 | if (r < 0) | |
2247 | return r; | |
2248 | ||
2249 | r = sd_bus_message_close_container(m); | |
2250 | if (r < 0) | |
2251 | return r; | |
86b8d289 LP |
2252 | |
2253 | r = sd_bus_message_append(m, "a(sa(sv))", 0); | |
2254 | if (r < 0) | |
2255 | return r; | |
cc377381 | 2256 | |
c49b30a2 | 2257 | r = sd_bus_call(manager->bus, m, 0, error, &reply); |
cc377381 LP |
2258 | if (r < 0) |
2259 | return r; | |
fb6becb4 LP |
2260 | |
2261 | if (job) { | |
2262 | const char *j; | |
2263 | char *copy; | |
2264 | ||
cc377381 LP |
2265 | r = sd_bus_message_read(reply, "o", &j); |
2266 | if (r < 0) | |
2267 | return r; | |
fb6becb4 LP |
2268 | |
2269 | copy = strdup(j); | |
2270 | if (!copy) | |
2271 | return -ENOMEM; | |
2272 | ||
2273 | *job = copy; | |
2274 | } | |
2275 | ||
cc377381 | 2276 | return 1; |
fb6becb4 LP |
2277 | } |
2278 | ||
cc377381 LP |
2279 | int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) { |
2280 | _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; | |
fb6becb4 LP |
2281 | int r; |
2282 | ||
2283 | assert(manager); | |
2284 | assert(unit); | |
2285 | ||
cc377381 | 2286 | r = sd_bus_call_method( |
fb6becb4 LP |
2287 | manager->bus, |
2288 | "org.freedesktop.systemd1", | |
2289 | "/org/freedesktop/systemd1", | |
2290 | "org.freedesktop.systemd1.Manager", | |
2291 | "StartUnit", | |
fb6becb4 | 2292 | error, |
cc377381 LP |
2293 | &reply, |
2294 | "ss", unit, "fail"); | |
2295 | if (r < 0) | |
fb6becb4 | 2296 | return r; |
fb6becb4 LP |
2297 | |
2298 | if (job) { | |
2299 | const char *j; | |
2300 | char *copy; | |
2301 | ||
cc377381 LP |
2302 | r = sd_bus_message_read(reply, "o", &j); |
2303 | if (r < 0) | |
2304 | return r; | |
fb6becb4 LP |
2305 | |
2306 | copy = strdup(j); | |
2307 | if (!copy) | |
2308 | return -ENOMEM; | |
2309 | ||
2310 | *job = copy; | |
2311 | } | |
2312 | ||
cc377381 | 2313 | return 1; |
fb6becb4 LP |
2314 | } |
2315 | ||
cc377381 LP |
2316 | int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) { |
2317 | _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; | |
fb6becb4 LP |
2318 | int r; |
2319 | ||
2320 | assert(manager); | |
2321 | assert(unit); | |
2322 | ||
cc377381 | 2323 | r = sd_bus_call_method( |
fb6becb4 LP |
2324 | manager->bus, |
2325 | "org.freedesktop.systemd1", | |
2326 | "/org/freedesktop/systemd1", | |
2327 | "org.freedesktop.systemd1.Manager", | |
2328 | "StopUnit", | |
fb6becb4 | 2329 | error, |
cc377381 LP |
2330 | &reply, |
2331 | "ss", unit, "fail"); | |
fb6becb4 | 2332 | if (r < 0) { |
cc377381 LP |
2333 | if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) || |
2334 | sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) { | |
6797c324 LP |
2335 | |
2336 | if (job) | |
2337 | *job = NULL; | |
2338 | ||
cc377381 | 2339 | sd_bus_error_free(error); |
6797c324 LP |
2340 | return 0; |
2341 | } | |
2342 | ||
fb6becb4 LP |
2343 | return r; |
2344 | } | |
2345 | ||
2346 | if (job) { | |
2347 | const char *j; | |
2348 | char *copy; | |
2349 | ||
cc377381 LP |
2350 | r = sd_bus_message_read(reply, "o", &j); |
2351 | if (r < 0) | |
2352 | return r; | |
fb6becb4 LP |
2353 | |
2354 | copy = strdup(j); | |
2355 | if (!copy) | |
2356 | return -ENOMEM; | |
2357 | ||
2358 | *job = copy; | |
2359 | } | |
2360 | ||
6797c324 | 2361 | return 1; |
fb6becb4 LP |
2362 | } |
2363 | ||
5f41d1f1 | 2364 | int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error) { |
5f41d1f1 LP |
2365 | _cleanup_free_ char *path = NULL; |
2366 | int r; | |
2367 | ||
2368 | assert(manager); | |
2369 | assert(scope); | |
2370 | ||
2371 | path = unit_dbus_path_from_name(scope); | |
2372 | if (!path) | |
2373 | return -ENOMEM; | |
2374 | ||
2375 | r = sd_bus_call_method( | |
2376 | manager->bus, | |
2377 | "org.freedesktop.systemd1", | |
2378 | path, | |
2379 | "org.freedesktop.systemd1.Scope", | |
2380 | "Abandon", | |
2381 | error, | |
2382 | NULL, | |
2383 | NULL); | |
2384 | if (r < 0) { | |
2385 | if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) || | |
4e2f8d27 LP |
2386 | sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED) || |
2387 | sd_bus_error_has_name(error, BUS_ERROR_SCOPE_NOT_RUNNING)) { | |
5f41d1f1 LP |
2388 | sd_bus_error_free(error); |
2389 | return 0; | |
2390 | } | |
2391 | ||
2392 | return r; | |
2393 | } | |
2394 | ||
2395 | return 1; | |
2396 | } | |
2397 | ||
cc377381 | 2398 | int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) { |
fb6becb4 LP |
2399 | assert(manager); |
2400 | assert(unit); | |
2401 | ||
cc377381 | 2402 | return sd_bus_call_method( |
fb6becb4 LP |
2403 | manager->bus, |
2404 | "org.freedesktop.systemd1", | |
2405 | "/org/freedesktop/systemd1", | |
2406 | "org.freedesktop.systemd1.Manager", | |
2407 | "KillUnit", | |
fb6becb4 | 2408 | error, |
cc377381 LP |
2409 | NULL, |
2410 | "ssi", unit, who == KILL_LEADER ? "main" : "all", signo); | |
fb6becb4 LP |
2411 | } |
2412 | ||
2413 | int manager_unit_is_active(Manager *manager, const char *unit) { | |
cc377381 LP |
2414 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; |
2415 | _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; | |
fb6becb4 | 2416 | _cleanup_free_ char *path = NULL; |
fb6becb4 | 2417 | const char *state; |
fb6becb4 LP |
2418 | int r; |
2419 | ||
2420 | assert(manager); | |
2421 | assert(unit); | |
2422 | ||
fb6becb4 LP |
2423 | path = unit_dbus_path_from_name(unit); |
2424 | if (!path) | |
2425 | return -ENOMEM; | |
2426 | ||
cc377381 | 2427 | r = sd_bus_get_property( |
fb6becb4 LP |
2428 | manager->bus, |
2429 | "org.freedesktop.systemd1", | |
2430 | path, | |
cc377381 LP |
2431 | "org.freedesktop.systemd1.Unit", |
2432 | "ActiveState", | |
fb6becb4 | 2433 | &error, |
cc377381 LP |
2434 | &reply, |
2435 | "s"); | |
fb6becb4 | 2436 | if (r < 0) { |
cc377381 LP |
2437 | /* systemd might have droppped off momentarily, let's |
2438 | * not make this an error */ | |
2439 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) || | |
2440 | sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED)) | |
6797c324 | 2441 | return true; |
6797c324 | 2442 | |
cc377381 LP |
2443 | /* If the unit is already unloaded then it's not |
2444 | * active */ | |
2445 | if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) || | |
2446 | sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED)) | |
6797c324 | 2447 | return false; |
6797c324 | 2448 | |
fb6becb4 LP |
2449 | return r; |
2450 | } | |
2451 | ||
cc377381 LP |
2452 | r = sd_bus_message_read(reply, "s", &state); |
2453 | if (r < 0) | |
fb6becb4 | 2454 | return -EINVAL; |
fb6becb4 | 2455 | |
cc377381 LP |
2456 | return !streq(state, "inactive") && !streq(state, "failed"); |
2457 | } | |
2458 | ||
2459 | int manager_job_is_active(Manager *manager, const char *path) { | |
2460 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; | |
2461 | _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; | |
2462 | int r; | |
2463 | ||
2464 | assert(manager); | |
2465 | assert(path); | |
2466 | ||
2467 | r = sd_bus_get_property( | |
2468 | manager->bus, | |
2469 | "org.freedesktop.systemd1", | |
2470 | path, | |
2471 | "org.freedesktop.systemd1.Job", | |
2472 | "State", | |
2473 | &error, | |
2474 | &reply, | |
2475 | "s"); | |
2476 | if (r < 0) { | |
2477 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) || | |
2478 | sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED)) | |
2479 | return true; | |
2480 | ||
2481 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT)) | |
2482 | return false; | |
2483 | ||
2484 | return r; | |
fb6becb4 LP |
2485 | } |
2486 | ||
cc377381 LP |
2487 | /* We don't actually care about the state really. The fact |
2488 | * that we could read the job state is enough for us */ | |
fb6becb4 | 2489 | |
cc377381 | 2490 | return true; |
fb6becb4 | 2491 | } |