]>
Commit | Line | Data |
---|---|---|
a7334b09 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
7 | under the terms of the GNU Lesser General Public License as published by |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
9 | (at your option) any later version. |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 14 | Lesser General Public License for more details. |
a7334b09 | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
ea430986 | 20 | #include <errno.h> |
07630cea | 21 | #include <sys/epoll.h> |
ea430986 LP |
22 | #include <unistd.h> |
23 | ||
718db961 | 24 | #include "sd-bus.h" |
07630cea | 25 | |
b5efdb8a | 26 | #include "alloc-util.h" |
07630cea LP |
27 | #include "bus-common-errors.h" |
28 | #include "bus-error.h" | |
29 | #include "bus-internal.h" | |
30 | #include "bus-util.h" | |
31 | #include "dbus-cgroup.h" | |
718db961 | 32 | #include "dbus-execute.h" |
07630cea | 33 | #include "dbus-job.h" |
718db961 | 34 | #include "dbus-kill.h" |
07630cea LP |
35 | #include "dbus-manager.h" |
36 | #include "dbus-unit.h" | |
3ffd4af2 LP |
37 | #include "dbus.h" |
38 | #include "fd-util.h" | |
07630cea LP |
39 | #include "log.h" |
40 | #include "missing.h" | |
41 | #include "mkdir.h" | |
42 | #include "selinux-access.h" | |
8f6df3fa | 43 | #include "special.h" |
07630cea LP |
44 | #include "string-util.h" |
45 | #include "strv.h" | |
718db961 | 46 | #include "strxcpyx.h" |
ee104e11 | 47 | #include "user-util.h" |
4288f619 | 48 | |
cbecf9bf | 49 | #define CONNECTIONS_MAX 4096 |
5e8d1c9a | 50 | |
718db961 LP |
51 | static void destroy_bus(Manager *m, sd_bus **bus); |
52 | ||
53 | int bus_send_queued_message(Manager *m) { | |
54 | int r; | |
8e274523 | 55 | |
8e274523 | 56 | assert(m); |
2e317f52 | 57 | |
718db961 LP |
58 | if (!m->queued_message) |
59 | return 0; | |
8e274523 | 60 | |
718db961 LP |
61 | /* If we cannot get rid of this message we won't dispatch any |
62 | * D-Bus messages, so that we won't end up wanting to queue | |
63 | * another message. */ | |
ea430986 | 64 | |
8f88ecf6 | 65 | r = sd_bus_send(NULL, m->queued_message, NULL); |
718db961 | 66 | if (r < 0) |
da927ba9 | 67 | log_warning_errno(r, "Failed to send queued message: %m"); |
ea430986 | 68 | |
718db961 | 69 | m->queued_message = sd_bus_message_unref(m->queued_message); |
ea430986 | 70 | |
718db961 | 71 | return 0; |
ea430986 LP |
72 | } |
73 | ||
19070062 | 74 | static int signal_agent_released(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 75 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
0a069ce6 | 76 | const char *cgroup, *me; |
718db961 | 77 | Manager *m = userdata; |
0a069ce6 DH |
78 | uid_t sender_uid; |
79 | sd_bus *bus; | |
718db961 | 80 | int r; |
ea430986 | 81 | |
718db961 | 82 | assert(message); |
ea430986 LP |
83 | assert(m); |
84 | ||
0a069ce6 DH |
85 | /* ignore recursive events sent by us on the system/user bus */ |
86 | bus = sd_bus_message_get_bus(message); | |
87 | if (!sd_bus_is_server(bus)) { | |
88 | r = sd_bus_get_unique_name(bus, &me); | |
89 | if (r < 0) | |
90 | return r; | |
91 | ||
92 | if (streq_ptr(sd_bus_message_get_sender(message), me)) | |
93 | return 0; | |
94 | } | |
95 | ||
96 | /* only accept org.freedesktop.systemd1.Agent from UID=0 */ | |
97 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); | |
98 | if (r < 0) | |
99 | return r; | |
100 | ||
101 | r = sd_bus_creds_get_euid(creds, &sender_uid); | |
102 | if (r < 0 || sender_uid != 0) | |
103 | return 0; | |
104 | ||
105 | /* parse 'cgroup-empty' notification */ | |
718db961 LP |
106 | r = sd_bus_message_read(message, "s", &cgroup); |
107 | if (r < 0) { | |
108 | bus_log_parse_error(r); | |
109 | return 0; | |
110 | } | |
ea430986 | 111 | |
718db961 | 112 | manager_notify_cgroup_empty(m, cgroup); |
ea430986 | 113 | |
a0c8526a DH |
114 | /* if running as system-instance, forward under our name */ |
115 | if (m->running_as == MANAGER_SYSTEM && m->system_bus) { | |
116 | r = sd_bus_message_rewind(message, 1); | |
117 | if (r >= 0) | |
118 | r = sd_bus_send(m->system_bus, message, NULL); | |
119 | if (r < 0) | |
120 | log_warning_errno(r, "Failed to forward Released message: %m"); | |
121 | } | |
39881ed2 | 122 | |
718db961 LP |
123 | return 0; |
124 | } | |
ea430986 | 125 | |
19070062 | 126 | static int signal_disconnected(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
718db961 | 127 | Manager *m = userdata; |
19070062 | 128 | sd_bus *bus; |
ea430986 | 129 | |
718db961 LP |
130 | assert(message); |
131 | assert(m); | |
19070062 | 132 | assert_se(bus = sd_bus_message_get_bus(message)); |
ea430986 | 133 | |
718db961 LP |
134 | if (bus == m->api_bus) |
135 | destroy_bus(m, &m->api_bus); | |
136 | if (bus == m->system_bus) | |
137 | destroy_bus(m, &m->system_bus); | |
138 | if (set_remove(m->private_buses, bus)) { | |
139 | log_debug("Got disconnect on private connection."); | |
140 | destroy_bus(m, &bus); | |
ea430986 LP |
141 | } |
142 | ||
718db961 | 143 | return 0; |
ea430986 LP |
144 | } |
145 | ||
19070062 | 146 | static int signal_activation_request(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) { |
4afd3348 LP |
147 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
148 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
718db961 LP |
149 | Manager *m = userdata; |
150 | const char *name; | |
151 | Unit *u; | |
152 | int r; | |
ea430986 | 153 | |
718db961 | 154 | assert(message); |
ea430986 LP |
155 | assert(m); |
156 | ||
718db961 LP |
157 | r = sd_bus_message_read(message, "s", &name); |
158 | if (r < 0) { | |
159 | bus_log_parse_error(r); | |
160 | return 0; | |
161 | } | |
ea430986 | 162 | |
718db961 LP |
163 | if (manager_unit_inactive_or_pending(m, SPECIAL_DBUS_SERVICE) || |
164 | manager_unit_inactive_or_pending(m, SPECIAL_DBUS_SOCKET)) { | |
165 | r = sd_bus_error_setf(&error, BUS_ERROR_SHUTTING_DOWN, "Refusing activation, D-Bus is shutting down."); | |
166 | goto failed; | |
167 | } | |
ea430986 | 168 | |
718db961 LP |
169 | r = manager_load_unit(m, name, NULL, &error, &u); |
170 | if (r < 0) | |
171 | goto failed; | |
ea430986 | 172 | |
718db961 | 173 | if (u->refuse_manual_start) { |
d14ab08b | 174 | r = sd_bus_error_setf(&error, BUS_ERROR_ONLY_BY_DEPENDENCY, "Operation refused, %s may be requested by dependency only.", u->id); |
718db961 | 175 | goto failed; |
ea430986 LP |
176 | } |
177 | ||
4bd29fe5 | 178 | r = manager_add_job(m, JOB_START, u, JOB_REPLACE, &error, NULL); |
718db961 LP |
179 | if (r < 0) |
180 | goto failed; | |
ea430986 | 181 | |
718db961 | 182 | /* Successfully queued, that's it for us */ |
ea430986 | 183 | return 0; |
ea430986 | 184 | |
718db961 LP |
185 | failed: |
186 | if (!sd_bus_error_is_set(&error)) | |
187 | sd_bus_error_set_errno(&error, r); | |
ea430986 | 188 | |
718db961 | 189 | log_debug("D-Bus activation failed for %s: %s", name, bus_error_message(&error, r)); |
ea430986 | 190 | |
966c66e3 | 191 | r = sd_bus_message_new_signal(sd_bus_message_get_bus(message), &reply, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure"); |
718db961 LP |
192 | if (r < 0) { |
193 | bus_log_create_error(r); | |
194 | return 0; | |
195 | } | |
196 | ||
fa0fed49 | 197 | r = sd_bus_message_append(reply, "sss", name, error.name, error.message); |
718db961 LP |
198 | if (r < 0) { |
199 | bus_log_create_error(r); | |
200 | return 0; | |
201 | } | |
202 | ||
966c66e3 | 203 | r = sd_bus_send_to(NULL, reply, "org.freedesktop.DBus", NULL); |
23bbb0de MS |
204 | if (r < 0) |
205 | return log_error_errno(r, "Failed to respond with to bus activation request: %m"); | |
ea430986 | 206 | |
718db961 | 207 | return 0; |
ea430986 LP |
208 | } |
209 | ||
ad5b215d | 210 | #ifdef HAVE_SELINUX |
19070062 | 211 | static int mac_selinux_filter(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
969987ea LP |
212 | Manager *m = userdata; |
213 | const char *verb, *path; | |
214 | Unit *u = NULL; | |
215 | Job *j; | |
216 | int r; | |
217 | ||
969987ea LP |
218 | assert(message); |
219 | ||
220 | /* Our own method calls are all protected individually with | |
221 | * selinux checks, but the built-in interfaces need to be | |
222 | * protected too. */ | |
223 | ||
224 | if (sd_bus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Set")) | |
225 | verb = "reload"; | |
226 | else if (sd_bus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", NULL) || | |
227 | sd_bus_message_is_method_call(message, "org.freedesktop.DBus.Properties", NULL) || | |
228 | sd_bus_message_is_method_call(message, "org.freedesktop.DBus.ObjectManager", NULL) || | |
229 | sd_bus_message_is_method_call(message, "org.freedesktop.DBus.Peer", NULL)) | |
230 | verb = "status"; | |
231 | else | |
232 | return 0; | |
233 | ||
234 | path = sd_bus_message_get_path(message); | |
235 | ||
236 | if (object_path_startswith("/org/freedesktop/systemd1", path)) { | |
237 | ||
8a188de9 | 238 | r = mac_selinux_access_check(message, verb, error); |
969987ea LP |
239 | if (r < 0) |
240 | return r; | |
241 | ||
242 | return 0; | |
243 | } | |
244 | ||
245 | if (streq_ptr(path, "/org/freedesktop/systemd1/unit/self")) { | |
4afd3348 | 246 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
969987ea LP |
247 | pid_t pid; |
248 | ||
5b12334d LP |
249 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); |
250 | if (r < 0) | |
251 | return 0; | |
252 | ||
253 | r = sd_bus_creds_get_pid(creds, &pid); | |
969987ea LP |
254 | if (r < 0) |
255 | return 0; | |
256 | ||
257 | u = manager_get_unit_by_pid(m, pid); | |
258 | } else { | |
259 | r = manager_get_job_from_dbus_path(m, path, &j); | |
260 | if (r >= 0) | |
261 | u = j->unit; | |
262 | else | |
263 | manager_load_unit_from_dbus_path(m, path, NULL, &u); | |
264 | } | |
265 | ||
266 | if (!u) | |
267 | return 0; | |
268 | ||
8a188de9 | 269 | r = mac_selinux_unit_access_check(u, message, verb, error); |
969987ea LP |
270 | if (r < 0) |
271 | return r; | |
272 | ||
273 | return 0; | |
274 | } | |
ad5b215d | 275 | #endif |
969987ea | 276 | |
f00c3121 | 277 | static int bus_job_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { |
718db961 LP |
278 | Manager *m = userdata; |
279 | Job *j; | |
280 | int r; | |
ea430986 | 281 | |
718db961 LP |
282 | assert(bus); |
283 | assert(path); | |
284 | assert(interface); | |
285 | assert(found); | |
ea430986 LP |
286 | assert(m); |
287 | ||
718db961 LP |
288 | r = manager_get_job_from_dbus_path(m, path, &j); |
289 | if (r < 0) | |
290 | return 0; | |
ea430986 | 291 | |
718db961 LP |
292 | *found = j; |
293 | return 1; | |
294 | } | |
ea430986 | 295 | |
f00c3121 | 296 | static int find_unit(Manager *m, sd_bus *bus, const char *path, Unit **unit, sd_bus_error *error) { |
718db961 LP |
297 | Unit *u; |
298 | int r; | |
ea430986 | 299 | |
718db961 LP |
300 | assert(m); |
301 | assert(bus); | |
302 | assert(path); | |
ea430986 | 303 | |
718db961 | 304 | if (streq_ptr(path, "/org/freedesktop/systemd1/unit/self")) { |
4afd3348 | 305 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
718db961 LP |
306 | sd_bus_message *message; |
307 | pid_t pid; | |
ea430986 | 308 | |
19befb2d | 309 | message = sd_bus_get_current_message(bus); |
718db961 | 310 | if (!message) |
f00c3121 | 311 | return 0; |
ea430986 | 312 | |
5b12334d | 313 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); |
718db961 | 314 | if (r < 0) |
5b12334d LP |
315 | return r; |
316 | ||
317 | r = sd_bus_creds_get_pid(creds, &pid); | |
318 | if (r < 0) | |
319 | return r; | |
ea430986 | 320 | |
718db961 LP |
321 | u = manager_get_unit_by_pid(m, pid); |
322 | } else { | |
f00c3121 | 323 | r = manager_load_unit_from_dbus_path(m, path, error, &u); |
718db961 | 324 | if (r < 0) |
f00c3121 | 325 | return 0; |
718db961 | 326 | } |
ea430986 | 327 | |
f00c3121 LP |
328 | if (!u) |
329 | return 0; | |
330 | ||
331 | *unit = u; | |
332 | return 1; | |
ea430986 LP |
333 | } |
334 | ||
f00c3121 | 335 | static int bus_unit_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { |
718db961 | 336 | Manager *m = userdata; |
ea430986 | 337 | |
718db961 LP |
338 | assert(bus); |
339 | assert(path); | |
340 | assert(interface); | |
341 | assert(found); | |
ea430986 LP |
342 | assert(m); |
343 | ||
f00c3121 | 344 | return find_unit(m, bus, path, (Unit**) found, error); |
ea430986 LP |
345 | } |
346 | ||
f00c3121 | 347 | static int bus_unit_interface_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { |
718db961 LP |
348 | Manager *m = userdata; |
349 | Unit *u; | |
f00c3121 | 350 | int r; |
ea430986 | 351 | |
718db961 LP |
352 | assert(bus); |
353 | assert(path); | |
354 | assert(interface); | |
355 | assert(found); | |
ea430986 LP |
356 | assert(m); |
357 | ||
f00c3121 LP |
358 | r = find_unit(m, bus, path, &u, error); |
359 | if (r <= 0) | |
360 | return r; | |
3df5bf61 | 361 | |
21b735e7 | 362 | if (!streq_ptr(interface, unit_dbus_interface_from_type(u->type))) |
718db961 | 363 | return 0; |
ea430986 | 364 | |
718db961 LP |
365 | *found = u; |
366 | return 1; | |
ea430986 LP |
367 | } |
368 | ||
f00c3121 | 369 | static int bus_unit_cgroup_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { |
718db961 LP |
370 | Manager *m = userdata; |
371 | Unit *u; | |
f00c3121 | 372 | int r; |
c1e1601e | 373 | |
718db961 LP |
374 | assert(bus); |
375 | assert(path); | |
376 | assert(interface); | |
377 | assert(found); | |
c1e1601e LP |
378 | assert(m); |
379 | ||
f00c3121 LP |
380 | r = find_unit(m, bus, path, &u, error); |
381 | if (r <= 0) | |
382 | return r; | |
05e343b7 | 383 | |
21b735e7 | 384 | if (!streq_ptr(interface, unit_dbus_interface_from_type(u->type))) |
718db961 | 385 | return 0; |
05e343b7 | 386 | |
35b7ff80 | 387 | if (!UNIT_HAS_CGROUP_CONTEXT(u)) |
718db961 | 388 | return 0; |
0034c15c | 389 | |
718db961 LP |
390 | *found = u; |
391 | return 1; | |
392 | } | |
0034c15c | 393 | |
f00c3121 | 394 | static int bus_cgroup_context_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { |
718db961 LP |
395 | Manager *m = userdata; |
396 | CGroupContext *c; | |
397 | Unit *u; | |
f00c3121 | 398 | int r; |
af25ec12 | 399 | |
718db961 LP |
400 | assert(bus); |
401 | assert(path); | |
402 | assert(interface); | |
403 | assert(found); | |
404 | assert(m); | |
0034c15c | 405 | |
f00c3121 LP |
406 | r = find_unit(m, bus, path, &u, error); |
407 | if (r <= 0) | |
408 | return r; | |
0034c15c | 409 | |
21b735e7 | 410 | if (!streq_ptr(interface, unit_dbus_interface_from_type(u->type))) |
718db961 | 411 | return 0; |
0034c15c | 412 | |
718db961 LP |
413 | c = unit_get_cgroup_context(u); |
414 | if (!c) | |
415 | return 0; | |
c1e1601e | 416 | |
718db961 LP |
417 | *found = c; |
418 | return 1; | |
419 | } | |
0034c15c | 420 | |
f00c3121 | 421 | static int bus_exec_context_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { |
718db961 LP |
422 | Manager *m = userdata; |
423 | ExecContext *c; | |
424 | Unit *u; | |
f00c3121 | 425 | int r; |
0034c15c | 426 | |
718db961 LP |
427 | assert(bus); |
428 | assert(path); | |
429 | assert(interface); | |
430 | assert(found); | |
431 | assert(m); | |
0034c15c | 432 | |
f00c3121 LP |
433 | r = find_unit(m, bus, path, &u, error); |
434 | if (r <= 0) | |
435 | return r; | |
0034c15c | 436 | |
21b735e7 | 437 | if (!streq_ptr(interface, unit_dbus_interface_from_type(u->type))) |
718db961 | 438 | return 0; |
0034c15c | 439 | |
718db961 LP |
440 | c = unit_get_exec_context(u); |
441 | if (!c) | |
442 | return 0; | |
0034c15c | 443 | |
718db961 LP |
444 | *found = c; |
445 | return 1; | |
c1e1601e LP |
446 | } |
447 | ||
f00c3121 | 448 | static int bus_kill_context_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { |
718db961 LP |
449 | Manager *m = userdata; |
450 | KillContext *c; | |
451 | Unit *u; | |
f00c3121 | 452 | int r; |
8e274523 | 453 | |
718db961 LP |
454 | assert(bus); |
455 | assert(path); | |
456 | assert(interface); | |
457 | assert(found); | |
8e274523 LP |
458 | assert(m); |
459 | ||
f00c3121 LP |
460 | r = find_unit(m, bus, path, &u, error); |
461 | if (r <= 0) | |
462 | return r; | |
8e274523 | 463 | |
21b735e7 | 464 | if (!streq_ptr(interface, unit_dbus_interface_from_type(u->type))) |
718db961 | 465 | return 0; |
8e274523 | 466 | |
718db961 LP |
467 | c = unit_get_kill_context(u); |
468 | if (!c) | |
469 | return 0; | |
53c6a358 | 470 | |
718db961 LP |
471 | *found = c; |
472 | return 1; | |
473 | } | |
8e274523 | 474 | |
f00c3121 | 475 | static int bus_job_enumerate(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { |
718db961 LP |
476 | _cleanup_free_ char **l = NULL; |
477 | Manager *m = userdata; | |
478 | unsigned k = 0; | |
479 | Iterator i; | |
480 | Job *j; | |
8e274523 | 481 | |
718db961 LP |
482 | l = new0(char*, hashmap_size(m->jobs)+1); |
483 | if (!l) | |
484 | return -ENOMEM; | |
8e274523 | 485 | |
718db961 LP |
486 | HASHMAP_FOREACH(j, m->jobs, i) { |
487 | l[k] = job_dbus_path(j); | |
488 | if (!l[k]) | |
489 | return -ENOMEM; | |
5e8d1c9a | 490 | |
718db961 LP |
491 | k++; |
492 | } | |
5e8d1c9a | 493 | |
718db961 | 494 | assert(hashmap_size(m->jobs) == k); |
3c661fad | 495 | |
718db961 LP |
496 | *nodes = l; |
497 | l = NULL; | |
5e8d1c9a | 498 | |
718db961 LP |
499 | return k; |
500 | } | |
53c6a358 | 501 | |
f00c3121 | 502 | static int bus_unit_enumerate(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { |
718db961 LP |
503 | _cleanup_free_ char **l = NULL; |
504 | Manager *m = userdata; | |
505 | unsigned k = 0; | |
506 | Iterator i; | |
507 | Unit *u; | |
3c661fad | 508 | |
718db961 LP |
509 | l = new0(char*, hashmap_size(m->units)+1); |
510 | if (!l) | |
511 | return -ENOMEM; | |
53c6a358 | 512 | |
718db961 LP |
513 | HASHMAP_FOREACH(u, m->units, i) { |
514 | l[k] = unit_dbus_path(u); | |
515 | if (!l[k]) | |
516 | return -ENOMEM; | |
53c6a358 | 517 | |
718db961 | 518 | k++; |
3c661fad LP |
519 | } |
520 | ||
718db961 LP |
521 | *nodes = l; |
522 | l = NULL; | |
5e8d1c9a | 523 | |
718db961 | 524 | return k; |
5e8d1c9a LP |
525 | } |
526 | ||
718db961 LP |
527 | static int bus_setup_api_vtables(Manager *m, sd_bus *bus) { |
528 | UnitType t; | |
529 | int r; | |
5e8d1c9a | 530 | |
ea430986 | 531 | assert(m); |
718db961 | 532 | assert(bus); |
ea430986 | 533 | |
ad5b215d | 534 | #ifdef HAVE_SELINUX |
8a188de9 | 535 | r = sd_bus_add_filter(bus, NULL, mac_selinux_filter, m); |
23bbb0de MS |
536 | if (r < 0) |
537 | return log_error_errno(r, "Failed to add SELinux access filter: %m"); | |
ad5b215d | 538 | #endif |
969987ea | 539 | |
19befb2d | 540 | r = sd_bus_add_object_vtable(bus, NULL, "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", bus_manager_vtable, m); |
23bbb0de MS |
541 | if (r < 0) |
542 | return log_error_errno(r, "Failed to register Manager vtable: %m"); | |
a16e1123 | 543 | |
19befb2d | 544 | r = sd_bus_add_fallback_vtable(bus, NULL, "/org/freedesktop/systemd1/job", "org.freedesktop.systemd1.Job", bus_job_vtable, bus_job_find, m); |
23bbb0de MS |
545 | if (r < 0) |
546 | return log_error_errno(r, "Failed to register Job vtable: %m"); | |
c1e1601e | 547 | |
19befb2d | 548 | r = sd_bus_add_node_enumerator(bus, NULL, "/org/freedesktop/systemd1/job", bus_job_enumerate, m); |
23bbb0de MS |
549 | if (r < 0) |
550 | return log_error_errno(r, "Failed to add job enumerator: %m"); | |
61902ea3 | 551 | |
19befb2d | 552 | r = sd_bus_add_fallback_vtable(bus, NULL, "/org/freedesktop/systemd1/unit", "org.freedesktop.systemd1.Unit", bus_unit_vtable, bus_unit_find, m); |
23bbb0de MS |
553 | if (r < 0) |
554 | return log_error_errno(r, "Failed to register Unit vtable: %m"); | |
61902ea3 | 555 | |
19befb2d | 556 | r = sd_bus_add_node_enumerator(bus, NULL, "/org/freedesktop/systemd1/unit", bus_unit_enumerate, m); |
23bbb0de MS |
557 | if (r < 0) |
558 | return log_error_errno(r, "Failed to add job enumerator: %m"); | |
61902ea3 | 559 | |
718db961 | 560 | for (t = 0; t < _UNIT_TYPE_MAX; t++) { |
21b735e7 LP |
561 | const char *interface; |
562 | ||
563 | assert_se(interface = unit_dbus_interface_from_type(t)); | |
564 | ||
565 | r = sd_bus_add_fallback_vtable(bus, NULL, "/org/freedesktop/systemd1/unit", interface, unit_vtable[t]->bus_vtable, bus_unit_interface_find, m); | |
23bbb0de | 566 | if (r < 0) |
21b735e7 | 567 | return log_error_errno(r, "Failed to register type specific vtable for %s: %m", interface); |
61902ea3 | 568 | |
718db961 | 569 | if (unit_vtable[t]->cgroup_context_offset > 0) { |
21b735e7 | 570 | r = sd_bus_add_fallback_vtable(bus, NULL, "/org/freedesktop/systemd1/unit", interface, bus_unit_cgroup_vtable, bus_unit_cgroup_find, m); |
23bbb0de | 571 | if (r < 0) |
21b735e7 | 572 | return log_error_errno(r, "Failed to register control group unit vtable for %s: %m", interface); |
61902ea3 | 573 | |
21b735e7 | 574 | r = sd_bus_add_fallback_vtable(bus, NULL, "/org/freedesktop/systemd1/unit", interface, bus_cgroup_vtable, bus_cgroup_context_find, m); |
23bbb0de | 575 | if (r < 0) |
21b735e7 | 576 | return log_error_errno(r, "Failed to register control group vtable for %s: %m", interface); |
61902ea3 LP |
577 | } |
578 | ||
718db961 | 579 | if (unit_vtable[t]->exec_context_offset > 0) { |
21b735e7 | 580 | r = sd_bus_add_fallback_vtable(bus, NULL, "/org/freedesktop/systemd1/unit", interface, bus_exec_vtable, bus_exec_context_find, m); |
23bbb0de | 581 | if (r < 0) |
21b735e7 | 582 | return log_error_errno(r, "Failed to register execute vtable for %s: %m", interface); |
718db961 | 583 | } |
61902ea3 | 584 | |
718db961 | 585 | if (unit_vtable[t]->kill_context_offset > 0) { |
21b735e7 | 586 | r = sd_bus_add_fallback_vtable(bus, NULL, "/org/freedesktop/systemd1/unit", interface, bus_kill_vtable, bus_kill_context_find, m); |
23bbb0de | 587 | if (r < 0) |
21b735e7 | 588 | return log_error_errno(r, "Failed to register kill vtable for %s: %m", interface); |
718db961 | 589 | } |
61902ea3 LP |
590 | } |
591 | ||
718db961 | 592 | return 0; |
61902ea3 LP |
593 | } |
594 | ||
718db961 LP |
595 | static int bus_setup_disconnected_match(Manager *m, sd_bus *bus) { |
596 | int r; | |
61902ea3 | 597 | |
718db961 LP |
598 | assert(m); |
599 | assert(bus); | |
61902ea3 | 600 | |
718db961 LP |
601 | r = sd_bus_add_match( |
602 | bus, | |
19befb2d | 603 | NULL, |
27fc65e1 | 604 | "sender='org.freedesktop.DBus.Local'," |
718db961 LP |
605 | "type='signal'," |
606 | "path='/org/freedesktop/DBus/Local'," | |
607 | "interface='org.freedesktop.DBus.Local'," | |
608 | "member='Disconnected'", | |
609 | signal_disconnected, m); | |
05e343b7 | 610 | |
23bbb0de MS |
611 | if (r < 0) |
612 | return log_error_errno(r, "Failed to register match for Disconnected message: %m"); | |
05e343b7 | 613 | |
718db961 | 614 | return 0; |
ea430986 LP |
615 | } |
616 | ||
718db961 | 617 | static int bus_on_connection(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
4afd3348 | 618 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; |
718db961 | 619 | _cleanup_close_ int nfd = -1; |
6dded4c7 | 620 | Manager *m = userdata; |
718db961 LP |
621 | sd_id128_t id; |
622 | int r; | |
6dded4c7 | 623 | |
718db961 | 624 | assert(s); |
6dded4c7 LP |
625 | assert(m); |
626 | ||
718db961 LP |
627 | nfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC); |
628 | if (nfd < 0) { | |
56f64d95 | 629 | log_warning_errno(errno, "Failed to accept private connection, ignoring: %m"); |
718db961 LP |
630 | return 0; |
631 | } | |
6dded4c7 | 632 | |
718db961 LP |
633 | if (set_size(m->private_buses) >= CONNECTIONS_MAX) { |
634 | log_warning("Too many concurrent connections, refusing"); | |
635 | return 0; | |
636 | } | |
6dded4c7 | 637 | |
d5099efc | 638 | r = set_ensure_allocated(&m->private_buses, NULL); |
718db961 LP |
639 | if (r < 0) { |
640 | log_oom(); | |
641 | return 0; | |
642 | } | |
6dded4c7 | 643 | |
718db961 LP |
644 | r = sd_bus_new(&bus); |
645 | if (r < 0) { | |
da927ba9 | 646 | log_warning_errno(r, "Failed to allocate new private connection bus: %m"); |
718db961 LP |
647 | return 0; |
648 | } | |
6dded4c7 | 649 | |
718db961 LP |
650 | r = sd_bus_set_fd(bus, nfd, nfd); |
651 | if (r < 0) { | |
da927ba9 | 652 | log_warning_errno(r, "Failed to set fd on new connection bus: %m"); |
718db961 LP |
653 | return 0; |
654 | } | |
6dded4c7 | 655 | |
718db961 | 656 | nfd = -1; |
6dded4c7 | 657 | |
718db961 LP |
658 | r = bus_check_peercred(bus); |
659 | if (r < 0) { | |
da927ba9 | 660 | log_warning_errno(r, "Incoming private connection from unprivileged client, refusing: %m"); |
718db961 LP |
661 | return 0; |
662 | } | |
6dded4c7 | 663 | |
718db961 | 664 | assert_se(sd_id128_randomize(&id) >= 0); |
6dded4c7 | 665 | |
718db961 LP |
666 | r = sd_bus_set_server(bus, 1, id); |
667 | if (r < 0) { | |
da927ba9 | 668 | log_warning_errno(r, "Failed to enable server support for new connection bus: %m"); |
718db961 LP |
669 | return 0; |
670 | } | |
6dded4c7 | 671 | |
0674bbea LP |
672 | r = sd_bus_negotiate_creds(bus, 1, |
673 | SD_BUS_CREDS_PID|SD_BUS_CREDS_UID| | |
674 | SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS| | |
675 | SD_BUS_CREDS_SELINUX_CONTEXT); | |
676 | if (r < 0) { | |
677 | log_warning_errno(r, "Failed to enable credentials for new connection: %m"); | |
678 | return 0; | |
679 | } | |
680 | ||
718db961 LP |
681 | r = sd_bus_start(bus); |
682 | if (r < 0) { | |
da927ba9 | 683 | log_warning_errno(r, "Failed to start new connection bus: %m"); |
718db961 | 684 | return 0; |
6dded4c7 LP |
685 | } |
686 | ||
718db961 LP |
687 | r = sd_bus_attach_event(bus, m->event, SD_EVENT_PRIORITY_NORMAL); |
688 | if (r < 0) { | |
da927ba9 | 689 | log_warning_errno(r, "Failed to attach new connection bus to event loop: %m"); |
718db961 | 690 | return 0; |
6dded4c7 LP |
691 | } |
692 | ||
b2c23da8 | 693 | if (m->running_as == MANAGER_SYSTEM) { |
718db961 LP |
694 | /* When we run as system instance we get the Released |
695 | * signal via a direct connection */ | |
696 | ||
697 | r = sd_bus_add_match( | |
698 | bus, | |
19befb2d | 699 | NULL, |
718db961 LP |
700 | "type='signal'," |
701 | "interface='org.freedesktop.systemd1.Agent'," | |
702 | "member='Released'," | |
703 | "path='/org/freedesktop/systemd1/agent'", | |
704 | signal_agent_released, m); | |
705 | ||
706 | if (r < 0) { | |
da927ba9 | 707 | log_warning_errno(r, "Failed to register Released match on new connection bus: %m"); |
718db961 LP |
708 | return 0; |
709 | } | |
710 | } | |
6dded4c7 | 711 | |
718db961 LP |
712 | r = bus_setup_disconnected_match(m, bus); |
713 | if (r < 0) | |
714 | return 0; | |
6dded4c7 | 715 | |
718db961 LP |
716 | r = bus_setup_api_vtables(m, bus); |
717 | if (r < 0) { | |
da927ba9 | 718 | log_warning_errno(r, "Failed to set up API vtables on new connection bus: %m"); |
718db961 LP |
719 | return 0; |
720 | } | |
6dded4c7 | 721 | |
718db961 LP |
722 | r = set_put(m->private_buses, bus); |
723 | if (r < 0) { | |
05a08cb6 | 724 | log_warning_errno(r, "Failed to add new connection bus to set: %m"); |
718db961 | 725 | return 0; |
6dded4c7 LP |
726 | } |
727 | ||
718db961 | 728 | bus = NULL; |
6dded4c7 | 729 | |
718db961 | 730 | log_debug("Accepted new private connection."); |
6dded4c7 | 731 | |
8e274523 LP |
732 | return 0; |
733 | } | |
734 | ||
8936a5e3 | 735 | int manager_sync_bus_names(Manager *m, sd_bus *bus) { |
0e7be129 | 736 | _cleanup_strv_free_ char **names = NULL; |
d8ccf5fd DM |
737 | const char *name; |
738 | Iterator i; | |
739 | Unit *u; | |
718db961 | 740 | int r; |
5e8d1c9a LP |
741 | |
742 | assert(m); | |
718db961 | 743 | assert(bus); |
5e8d1c9a | 744 | |
0e7be129 | 745 | r = sd_bus_list_names(bus, &names, NULL); |
23bbb0de MS |
746 | if (r < 0) |
747 | return log_error_errno(r, "Failed to get initial list of names: %m"); | |
5e8d1c9a | 748 | |
d8ccf5fd DM |
749 | /* We have to synchronize the current bus names with the |
750 | * list of active services. To do this, walk the list of | |
751 | * all units with bus names. */ | |
752 | HASHMAP_FOREACH_KEY(u, name, m->watch_bus, i) { | |
753 | Service *s = SERVICE(u); | |
754 | ||
755 | assert(s); | |
bbc29086 | 756 | |
d8ccf5fd DM |
757 | if (!streq_ptr(s->bus_name, name)) { |
758 | log_unit_warning(u, "Bus name has changed from %s → %s, ignoring.", s->bus_name, name); | |
759 | continue; | |
760 | } | |
761 | ||
762 | /* Check if a service's bus name is in the list of currently | |
763 | * active names */ | |
764 | if (strv_contains(names, name)) { | |
765 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
766 | const char *unique; | |
767 | ||
768 | /* If it is, determine its current owner */ | |
769 | r = sd_bus_get_name_creds(bus, name, SD_BUS_CREDS_UNIQUE_NAME, &creds); | |
770 | if (r < 0) { | |
771 | log_error_errno(r, "Failed to get bus name owner %s: %m", name); | |
772 | continue; | |
773 | } | |
774 | ||
775 | r = sd_bus_creds_get_unique_name(creds, &unique); | |
776 | if (r < 0) { | |
777 | log_error_errno(r, "Failed to get unique name for %s: %m", name); | |
778 | continue; | |
779 | } | |
780 | ||
781 | /* Now, let's compare that to the previous bus owner, and | |
782 | * if it's still the same, all is fine, so just don't | |
783 | * bother the service. Otherwise, the name has apparently | |
784 | * changed, so synthesize a name owner changed signal. */ | |
785 | ||
786 | if (!streq_ptr(unique, s->bus_name_owner)) | |
787 | UNIT_VTABLE(u)->bus_name_owner_change(u, name, s->bus_name_owner, unique); | |
788 | } else { | |
789 | /* So, the name we're watching is not on the bus. | |
790 | * This either means it simply hasn't appeared yet, | |
791 | * or it was lost during the daemon reload. | |
792 | * Check if the service has a stored name owner, | |
793 | * and synthesize a name loss signal in this case. */ | |
794 | ||
795 | if (s->bus_name_owner) | |
796 | UNIT_VTABLE(u)->bus_name_owner_change(u, name, s->bus_name_owner, NULL); | |
797 | } | |
bbc29086 | 798 | } |
5e8d1c9a | 799 | |
718db961 | 800 | return 0; |
5e8d1c9a LP |
801 | } |
802 | ||
718db961 | 803 | static int bus_setup_api(Manager *m, sd_bus *bus) { |
bbc29086 DM |
804 | Iterator i; |
805 | char *name; | |
806 | Unit *u; | |
718db961 | 807 | int r; |
f278026d | 808 | |
718db961 LP |
809 | assert(m); |
810 | assert(bus); | |
c1e1601e | 811 | |
8fd00193 LP |
812 | /* Let's make sure we have enough credential bits so that we can make security and selinux decisions */ |
813 | r = sd_bus_negotiate_creds(bus, 1, | |
814 | SD_BUS_CREDS_PID|SD_BUS_CREDS_UID| | |
815 | SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS| | |
816 | SD_BUS_CREDS_SELINUX_CONTEXT); | |
817 | if (r < 0) | |
da927ba9 | 818 | log_warning_errno(r, "Failed to enable credential passing, ignoring: %m"); |
8fd00193 | 819 | |
718db961 LP |
820 | r = bus_setup_api_vtables(m, bus); |
821 | if (r < 0) | |
822 | return r; | |
cbd37330 | 823 | |
bbc29086 | 824 | HASHMAP_FOREACH_KEY(u, name, m->watch_bus, i) { |
9806e87d | 825 | r = unit_install_bus_match(u, bus, name); |
bbc29086 | 826 | if (r < 0) |
8ea823b6 | 827 | log_error_errno(r, "Failed to subscribe to NameOwnerChanged signal for '%s': %m", name); |
bbc29086 | 828 | } |
718db961 LP |
829 | |
830 | r = sd_bus_add_match( | |
831 | bus, | |
19befb2d | 832 | NULL, |
718db961 LP |
833 | "type='signal'," |
834 | "sender='org.freedesktop.DBus'," | |
835 | "path='/org/freedesktop/DBus'," | |
836 | "interface='org.freedesktop.systemd1.Activator'," | |
837 | "member='ActivationRequest'", | |
838 | signal_activation_request, m); | |
839 | if (r < 0) | |
da927ba9 | 840 | log_warning_errno(r, "Failed to subscribe to activation signal: %m"); |
cbd37330 | 841 | |
718db961 LP |
842 | /* Allow replacing of our name, to ease implementation of |
843 | * reexecution, where we keep the old connection open until | |
844 | * after the new connection is set up and the name installed | |
845 | * to allow clients to synchronously wait for reexecution to | |
846 | * finish */ | |
29a07cdb | 847 | r = sd_bus_request_name(bus,"org.freedesktop.systemd1", SD_BUS_NAME_REPLACE_EXISTING|SD_BUS_NAME_ALLOW_REPLACEMENT); |
23bbb0de MS |
848 | if (r < 0) |
849 | return log_error_errno(r, "Failed to register name: %m"); | |
53c6a358 | 850 | |
8936a5e3 | 851 | r = manager_sync_bus_names(m, bus); |
d8ccf5fd DM |
852 | if (r < 0) |
853 | return r; | |
f278026d | 854 | |
718db961 | 855 | log_debug("Successfully connected to API bus."); |
f278026d LP |
856 | return 0; |
857 | } | |
858 | ||
718db961 | 859 | static int bus_init_api(Manager *m) { |
4afd3348 | 860 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; |
f278026d LP |
861 | int r; |
862 | ||
718db961 LP |
863 | if (m->api_bus) |
864 | return 0; | |
cbd37330 | 865 | |
718db961 | 866 | /* The API and system bus is the same if we are running in system mode */ |
b2c23da8 | 867 | if (m->running_as == MANAGER_SYSTEM && m->system_bus) |
718db961 LP |
868 | bus = sd_bus_ref(m->system_bus); |
869 | else { | |
b2c23da8 | 870 | if (m->running_as == MANAGER_SYSTEM) |
718db961 LP |
871 | r = sd_bus_open_system(&bus); |
872 | else | |
873 | r = sd_bus_open_user(&bus); | |
cbd37330 | 874 | |
718db961 LP |
875 | if (r < 0) { |
876 | log_debug("Failed to connect to API bus, retrying later..."); | |
877 | return 0; | |
cbd37330 MS |
878 | } |
879 | ||
718db961 LP |
880 | r = sd_bus_attach_event(bus, m->event, SD_EVENT_PRIORITY_NORMAL); |
881 | if (r < 0) { | |
da927ba9 | 882 | log_error_errno(r, "Failed to attach API bus to event loop: %m"); |
718db961 | 883 | return 0; |
cbd37330 MS |
884 | } |
885 | ||
718db961 LP |
886 | r = bus_setup_disconnected_match(m, bus); |
887 | if (r < 0) | |
888 | return 0; | |
cbd37330 MS |
889 | } |
890 | ||
718db961 | 891 | r = bus_setup_api(m, bus); |
cbd37330 | 892 | if (r < 0) { |
da927ba9 | 893 | log_error_errno(r, "Failed to set up API bus: %m"); |
718db961 | 894 | return 0; |
cbd37330 | 895 | } |
cbd37330 | 896 | |
718db961 LP |
897 | m->api_bus = bus; |
898 | bus = NULL; | |
cbd37330 MS |
899 | |
900 | return 0; | |
cbd37330 MS |
901 | } |
902 | ||
718db961 LP |
903 | static int bus_setup_system(Manager *m, sd_bus *bus) { |
904 | int r; | |
cbd37330 | 905 | |
718db961 LP |
906 | assert(m); |
907 | assert(bus); | |
cbd37330 | 908 | |
f5b51ea7 | 909 | /* On kdbus or if we are a user instance we get the Released message via the system bus */ |
b2c23da8 | 910 | if (m->running_as == MANAGER_USER || m->kdbus_fd >= 0) { |
f5b51ea7 DH |
911 | r = sd_bus_add_match( |
912 | bus, | |
913 | NULL, | |
914 | "type='signal'," | |
915 | "interface='org.freedesktop.systemd1.Agent'," | |
916 | "member='Released'," | |
917 | "path='/org/freedesktop/systemd1/agent'", | |
918 | signal_agent_released, m); | |
919 | if (r < 0) | |
920 | log_warning_errno(r, "Failed to register Released match on system bus: %m"); | |
921 | } | |
718db961 LP |
922 | |
923 | log_debug("Successfully connected to system bus."); | |
924 | return 0; | |
cbd37330 MS |
925 | } |
926 | ||
927 | static int bus_init_system(Manager *m) { | |
4afd3348 | 928 | _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; |
cbd37330 MS |
929 | int r; |
930 | ||
931 | if (m->system_bus) | |
932 | return 0; | |
933 | ||
718db961 | 934 | /* The API and system bus is the same if we are running in system mode */ |
b2c23da8 | 935 | if (m->running_as == MANAGER_SYSTEM && m->api_bus) { |
718db961 LP |
936 | m->system_bus = sd_bus_ref(m->api_bus); |
937 | return 0; | |
9014a8bd | 938 | } |
8e274523 | 939 | |
718db961 LP |
940 | r = sd_bus_open_system(&bus); |
941 | if (r < 0) { | |
942 | log_debug("Failed to connect to system bus, retrying later..."); | |
943 | return 0; | |
944 | } | |
cbd37330 | 945 | |
718db961 | 946 | r = bus_setup_disconnected_match(m, bus); |
cbd37330 | 947 | if (r < 0) |
cbd37330 MS |
948 | return 0; |
949 | ||
718db961 LP |
950 | r = sd_bus_attach_event(bus, m->event, SD_EVENT_PRIORITY_NORMAL); |
951 | if (r < 0) { | |
da927ba9 | 952 | log_error_errno(r, "Failed to attach system bus to event loop: %m"); |
cbd37330 MS |
953 | return 0; |
954 | } | |
955 | ||
718db961 LP |
956 | r = bus_setup_system(m, bus); |
957 | if (r < 0) { | |
da927ba9 | 958 | log_error_errno(r, "Failed to set up system bus: %m"); |
718db961 | 959 | return 0; |
cbd37330 MS |
960 | } |
961 | ||
718db961 LP |
962 | m->system_bus = bus; |
963 | bus = NULL; | |
cbd37330 MS |
964 | |
965 | return 0; | |
5e8d1c9a LP |
966 | } |
967 | ||
968 | static int bus_init_private(Manager *m) { | |
718db961 LP |
969 | _cleanup_close_ int fd = -1; |
970 | union sockaddr_union sa = { | |
971 | .un.sun_family = AF_UNIX | |
5e8d1c9a | 972 | }; |
718db961 LP |
973 | sd_event_source *s; |
974 | socklen_t salen; | |
975 | int r; | |
5e8d1c9a LP |
976 | |
977 | assert(m); | |
978 | ||
718db961 | 979 | if (m->private_listen_fd >= 0) |
5e8d1c9a LP |
980 | return 0; |
981 | ||
a6aa8912 LP |
982 | /* We don't need the private socket if we have kdbus */ |
983 | if (m->kdbus_fd >= 0) | |
984 | return 0; | |
985 | ||
b2c23da8 | 986 | if (m->running_as == MANAGER_SYSTEM) { |
be81bfc4 LP |
987 | |
988 | /* We want the private bus only when running as init */ | |
989 | if (getpid() != 1) | |
990 | return 0; | |
991 | ||
718db961 | 992 | strcpy(sa.un.sun_path, "/run/systemd/private"); |
f8294e41 | 993 | salen = offsetof(union sockaddr_union, un.sun_path) + strlen("/run/systemd/private"); |
be81bfc4 | 994 | } else { |
718db961 LP |
995 | size_t left = sizeof(sa.un.sun_path); |
996 | char *p = sa.un.sun_path; | |
be81bfc4 | 997 | const char *e; |
be81bfc4 | 998 | |
4db17f29 | 999 | e = secure_getenv("XDG_RUNTIME_DIR"); |
718db961 LP |
1000 | if (!e) { |
1001 | log_error("Failed to determine XDG_RUNTIME_DIR"); | |
1002 | return -EHOSTDOWN; | |
be81bfc4 LP |
1003 | } |
1004 | ||
718db961 LP |
1005 | left = strpcpy(&p, left, e); |
1006 | left = strpcpy(&p, left, "/systemd/private"); | |
92f30349 | 1007 | |
718db961 | 1008 | salen = sizeof(sa.un) - left; |
be81bfc4 | 1009 | } |
5e8d1c9a | 1010 | |
f0e62e89 LP |
1011 | (void) mkdir_parents_label(sa.un.sun_path, 0755); |
1012 | (void) unlink(sa.un.sun_path); | |
718db961 LP |
1013 | |
1014 | fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); | |
4a62c710 MS |
1015 | if (fd < 0) |
1016 | return log_error_errno(errno, "Failed to allocate private socket: %m"); | |
5e8d1c9a | 1017 | |
718db961 | 1018 | r = bind(fd, &sa.sa, salen); |
4a62c710 MS |
1019 | if (r < 0) |
1020 | return log_error_errno(errno, "Failed to bind private socket: %m"); | |
5e8d1c9a | 1021 | |
718db961 | 1022 | r = listen(fd, SOMAXCONN); |
4a62c710 MS |
1023 | if (r < 0) |
1024 | return log_error_errno(errno, "Failed to make private socket listening: %m"); | |
5e8d1c9a | 1025 | |
151b9b96 | 1026 | r = sd_event_add_io(m->event, &s, fd, EPOLLIN, bus_on_connection, m); |
23bbb0de MS |
1027 | if (r < 0) |
1028 | return log_error_errno(r, "Failed to allocate event source: %m"); | |
5e8d1c9a | 1029 | |
7dfbe2e3 TG |
1030 | (void) sd_event_source_set_description(s, "bus-connection"); |
1031 | ||
718db961 LP |
1032 | m->private_listen_fd = fd; |
1033 | m->private_listen_event_source = s; | |
1034 | fd = -1; | |
5e8d1c9a | 1035 | |
718db961 | 1036 | log_debug("Successfully created private D-Bus server."); |
5e8d1c9a | 1037 | |
718db961 | 1038 | return 0; |
5e8d1c9a LP |
1039 | } |
1040 | ||
3996fbe2 | 1041 | int bus_init(Manager *m, bool try_bus_connect) { |
5e8d1c9a LP |
1042 | int r; |
1043 | ||
3996fbe2 | 1044 | if (try_bus_connect) { |
718db961 LP |
1045 | r = bus_init_system(m); |
1046 | if (r < 0) | |
1047 | return r; | |
1048 | ||
1049 | r = bus_init_api(m); | |
1050 | if (r < 0) | |
3996fbe2 LP |
1051 | return r; |
1052 | } | |
1053 | ||
6fa48533 LP |
1054 | r = bus_init_private(m); |
1055 | if (r < 0) | |
5e8d1c9a | 1056 | return r; |
f278026d | 1057 | |
ea430986 LP |
1058 | return 0; |
1059 | } | |
1060 | ||
718db961 | 1061 | static void destroy_bus(Manager *m, sd_bus **bus) { |
a567261a | 1062 | Iterator i; |
718db961 | 1063 | Job *j; |
a567261a | 1064 | |
718db961 LP |
1065 | assert(m); |
1066 | assert(bus); | |
5e8d1c9a | 1067 | |
718db961 | 1068 | if (!*bus) |
cbd37330 | 1069 | return; |
f278026d | 1070 | |
718db961 | 1071 | /* Get rid of tracked clients on this bus */ |
8f8f05a9 LP |
1072 | if (m->subscribed && sd_bus_track_get_bus(m->subscribed) == *bus) |
1073 | m->subscribed = sd_bus_track_unref(m->subscribed); | |
1074 | ||
718db961 | 1075 | HASHMAP_FOREACH(j, m->jobs, i) |
b39a2770 SW |
1076 | if (j->clients && sd_bus_track_get_bus(j->clients) == *bus) |
1077 | j->clients = sd_bus_track_unref(j->clients); | |
c1e1601e | 1078 | |
718db961 | 1079 | /* Get rid of queued message on this bus */ |
8f88ecf6 LP |
1080 | if (m->queued_message && sd_bus_message_get_bus(m->queued_message) == *bus) |
1081 | m->queued_message = sd_bus_message_unref(m->queued_message); | |
f278026d | 1082 | |
718db961 LP |
1083 | /* Possibly flush unwritten data, but only if we are |
1084 | * unprivileged, since we don't want to sync here */ | |
b2c23da8 | 1085 | if (m->running_as != MANAGER_SYSTEM) |
718db961 | 1086 | sd_bus_flush(*bus); |
5e8d1c9a | 1087 | |
718db961 LP |
1088 | /* And destroy the object */ |
1089 | sd_bus_close(*bus); | |
1090 | *bus = sd_bus_unref(*bus); | |
5e8d1c9a LP |
1091 | } |
1092 | ||
1093 | void bus_done(Manager *m) { | |
718db961 | 1094 | sd_bus *b; |
05e343b7 LP |
1095 | |
1096 | assert(m); | |
05e343b7 | 1097 | |
718db961 LP |
1098 | if (m->api_bus) |
1099 | destroy_bus(m, &m->api_bus); | |
1100 | if (m->system_bus) | |
1101 | destroy_bus(m, &m->system_bus); | |
1102 | while ((b = set_steal_first(m->private_buses))) | |
1103 | destroy_bus(m, &b); | |
05e343b7 | 1104 | |
525d3cc7 | 1105 | m->private_buses = set_free(m->private_buses); |
8f8f05a9 LP |
1106 | |
1107 | m->subscribed = sd_bus_track_unref(m->subscribed); | |
6796073e | 1108 | m->deserialized_subscribed = strv_free(m->deserialized_subscribed); |
05e343b7 | 1109 | |
718db961 LP |
1110 | if (m->private_listen_event_source) |
1111 | m->private_listen_event_source = sd_event_source_unref(m->private_listen_event_source); | |
05e343b7 | 1112 | |
03e334a1 | 1113 | m->private_listen_fd = safe_close(m->private_listen_fd); |
283868e1 SW |
1114 | |
1115 | bus_verify_polkit_async_registry_free(m->polkit_registry); | |
a567261a | 1116 | } |
b23de6af LP |
1117 | |
1118 | int bus_fdset_add_all(Manager *m, FDSet *fds) { | |
1119 | Iterator i; | |
718db961 LP |
1120 | sd_bus *b; |
1121 | int fd; | |
b23de6af LP |
1122 | |
1123 | assert(m); | |
1124 | assert(fds); | |
1125 | ||
1126 | /* When we are about to reexecute we add all D-Bus fds to the | |
1127 | * set to pass over to the newly executed systemd. They won't | |
718db961 | 1128 | * be used there however, except thatt they are closed at the |
b23de6af | 1129 | * very end of deserialization, those making it possible for |
44143309 | 1130 | * clients to synchronously wait for systemd to reexec by |
b23de6af LP |
1131 | * simply waiting for disconnection */ |
1132 | ||
718db961 LP |
1133 | if (m->api_bus) { |
1134 | fd = sd_bus_get_fd(m->api_bus); | |
1135 | if (fd >= 0) { | |
b23de6af | 1136 | fd = fdset_put_dup(fds, fd); |
b23de6af LP |
1137 | if (fd < 0) |
1138 | return fd; | |
1139 | } | |
1140 | } | |
1141 | ||
718db961 LP |
1142 | SET_FOREACH(b, m->private_buses, i) { |
1143 | fd = sd_bus_get_fd(b); | |
1144 | if (fd >= 0) { | |
b23de6af | 1145 | fd = fdset_put_dup(fds, fd); |
b23de6af LP |
1146 | if (fd < 0) |
1147 | return fd; | |
1148 | } | |
1149 | } | |
1150 | ||
718db961 LP |
1151 | /* We don't offer any APIs on the system bus (well, unless it |
1152 | * is the same as the API bus) hence we don't bother with it | |
1153 | * here */ | |
6fa48533 | 1154 | |
718db961 | 1155 | return 0; |
6fa48533 LP |
1156 | } |
1157 | ||
8f8f05a9 LP |
1158 | int bus_foreach_bus( |
1159 | Manager *m, | |
1160 | sd_bus_track *subscribed2, | |
1161 | int (*send_message)(sd_bus *bus, void *userdata), | |
1162 | void *userdata) { | |
1163 | ||
1164 | Iterator i; | |
1165 | sd_bus *b; | |
1166 | int r, ret = 0; | |
1167 | ||
ff9b60f3 | 1168 | /* Send to all direct buses, unconditionally */ |
8f8f05a9 LP |
1169 | SET_FOREACH(b, m->private_buses, i) { |
1170 | r = send_message(b, userdata); | |
1171 | if (r < 0) | |
1172 | ret = r; | |
1173 | } | |
1174 | ||
1175 | /* Send to API bus, but only if somebody is subscribed */ | |
1176 | if (sd_bus_track_count(m->subscribed) > 0 || | |
1177 | sd_bus_track_count(subscribed2) > 0) { | |
1178 | r = send_message(m->api_bus, userdata); | |
1179 | if (r < 0) | |
1180 | ret = r; | |
1181 | } | |
1182 | ||
1183 | return ret; | |
1184 | } | |
1185 | ||
1186 | void bus_track_serialize(sd_bus_track *t, FILE *f) { | |
1187 | const char *n; | |
1188 | ||
6fa48533 LP |
1189 | assert(f); |
1190 | ||
8f8f05a9 LP |
1191 | for (n = sd_bus_track_first(t); n; n = sd_bus_track_next(t)) |
1192 | fprintf(f, "subscribed=%s\n", n); | |
6fa48533 LP |
1193 | } |
1194 | ||
8f8f05a9 LP |
1195 | int bus_track_deserialize_item(char ***l, const char *line) { |
1196 | const char *e; | |
230314d7 | 1197 | int r; |
8f8f05a9 LP |
1198 | |
1199 | assert(l); | |
6fa48533 LP |
1200 | assert(line); |
1201 | ||
8f8f05a9 LP |
1202 | e = startswith(line, "subscribed="); |
1203 | if (!e) | |
230314d7 LP |
1204 | return 0; |
1205 | ||
1206 | r = strv_extend(l, e); | |
1207 | if (r < 0) | |
1208 | return r; | |
8f8f05a9 | 1209 | |
230314d7 | 1210 | return 1; |
8f8f05a9 LP |
1211 | } |
1212 | ||
1213 | int bus_track_coldplug(Manager *m, sd_bus_track **t, char ***l) { | |
1214 | int r = 0; | |
1215 | ||
1216 | assert(m); | |
1217 | assert(t); | |
1218 | assert(l); | |
1219 | ||
1220 | if (!strv_isempty(*l) && m->api_bus) { | |
1221 | char **i; | |
1222 | ||
1223 | if (!*t) { | |
1224 | r = sd_bus_track_new(m->api_bus, t, NULL, NULL); | |
1225 | if (r < 0) | |
1226 | return r; | |
1227 | } | |
1228 | ||
1229 | r = 0; | |
1230 | STRV_FOREACH(i, *l) { | |
1231 | int k; | |
1232 | ||
1233 | k = sd_bus_track_add_name(*t, *i); | |
1234 | if (k < 0) | |
1235 | r = k; | |
1236 | } | |
1237 | } | |
1238 | ||
6796073e | 1239 | *l = strv_free(*l); |
8f8f05a9 LP |
1240 | |
1241 | return r; | |
6fa48533 | 1242 | } |
283868e1 | 1243 | |
1d22e906 | 1244 | int bus_verify_manage_units_async(Manager *m, sd_bus_message *call, sd_bus_error *error) { |
403ed0e5 | 1245 | return bus_verify_polkit_async(call, CAP_SYS_ADMIN, "org.freedesktop.systemd1.manage-units", NULL, false, UID_INVALID, &m->polkit_registry, error); |
283868e1 SW |
1246 | } |
1247 | ||
283868e1 | 1248 | int bus_verify_manage_unit_files_async(Manager *m, sd_bus_message *call, sd_bus_error *error) { |
403ed0e5 | 1249 | return bus_verify_polkit_async(call, CAP_SYS_ADMIN, "org.freedesktop.systemd1.manage-unit-files", NULL, false, UID_INVALID, &m->polkit_registry, error); |
283868e1 SW |
1250 | } |
1251 | ||
1252 | int bus_verify_reload_daemon_async(Manager *m, sd_bus_message *call, sd_bus_error *error) { | |
403ed0e5 | 1253 | return bus_verify_polkit_async(call, CAP_SYS_ADMIN, "org.freedesktop.systemd1.reload-daemon", NULL, false, UID_INVALID, &m->polkit_registry, error); |
283868e1 | 1254 | } |
1d22e906 LP |
1255 | |
1256 | int bus_verify_set_environment_async(Manager *m, sd_bus_message *call, sd_bus_error *error) { | |
403ed0e5 | 1257 | return bus_verify_polkit_async(call, CAP_SYS_ADMIN, "org.freedesktop.systemd1.set-environment", NULL, false, UID_INVALID, &m->polkit_registry, error); |
1d22e906 | 1258 | } |