]>
Commit | Line | Data |
---|---|---|
ea430986 LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
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 | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
ea430986 LP |
22 | #include <sys/epoll.h> |
23 | #include <sys/timerfd.h> | |
24 | #include <errno.h> | |
25 | #include <unistd.h> | |
4139c1b2 | 26 | #include <dbus/dbus.h> |
ea430986 LP |
27 | |
28 | #include "dbus.h" | |
29 | #include "log.h" | |
30 | #include "strv.h" | |
8e274523 | 31 | #include "cgroup.h" |
4139c1b2 LP |
32 | #include "dbus-unit.h" |
33 | #include "dbus-job.h" | |
34 | #include "dbus-manager.h" | |
4288f619 LP |
35 | #include "dbus-service.h" |
36 | #include "dbus-socket.h" | |
37 | #include "dbus-target.h" | |
38 | #include "dbus-device.h" | |
39 | #include "dbus-mount.h" | |
40 | #include "dbus-automount.h" | |
41 | #include "dbus-snapshot.h" | |
42 | #include "dbus-swap.h" | |
871d7de4 | 43 | #include "dbus-timer.h" |
01f78473 | 44 | #include "dbus-path.h" |
4288f619 LP |
45 | |
46 | static const char bus_properties_interface[] = BUS_PROPERTIES_INTERFACE; | |
47 | static const char bus_introspectable_interface[] = BUS_INTROSPECTABLE_INTERFACE; | |
48 | ||
49 | const char *const bus_interface_table[] = { | |
50 | "org.freedesktop.DBus.Properties", bus_properties_interface, | |
51 | "org.freedesktop.DBus.Introspectable", bus_introspectable_interface, | |
52 | "org.freedesktop.systemd1.Manager", bus_manager_interface, | |
53 | "org.freedesktop.systemd1.Job", bus_job_interface, | |
54 | "org.freedesktop.systemd1.Unit", bus_unit_interface, | |
55 | "org.freedesktop.systemd1.Service", bus_service_interface, | |
56 | "org.freedesktop.systemd1.Socket", bus_socket_interface, | |
57 | "org.freedesktop.systemd1.Target", bus_target_interface, | |
58 | "org.freedesktop.systemd1.Device", bus_device_interface, | |
59 | "org.freedesktop.systemd1.Mount", bus_mount_interface, | |
60 | "org.freedesktop.systemd1.Automount", bus_automount_interface, | |
61 | "org.freedesktop.systemd1.Snapshot", bus_snapshot_interface, | |
62 | "org.freedesktop.systemd1.Swap", bus_swap_interface, | |
871d7de4 | 63 | "org.freedesktop.systemd1.Timer", bus_timer_interface, |
01f78473 | 64 | "org.freedesktop.systemd1.Path", bus_path_interface, |
4288f619 LP |
65 | NULL |
66 | }; | |
ea430986 | 67 | |
0034c15c LP |
68 | static const char *error_to_dbus(int error); |
69 | ||
f278026d | 70 | static void api_bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) { |
ea430986 LP |
71 | Manager *m = data; |
72 | ||
73 | assert(bus); | |
74 | assert(m); | |
2e317f52 LP |
75 | |
76 | if (!m->api_bus) | |
77 | return; | |
78 | ||
f278026d | 79 | assert(m->api_bus == bus); |
ea430986 | 80 | |
f278026d | 81 | m->request_api_bus_dispatch = status != DBUS_DISPATCH_COMPLETE; |
ea430986 LP |
82 | } |
83 | ||
8e274523 LP |
84 | static void system_bus_dispatch_status(DBusConnection *bus, DBusDispatchStatus status, void *data) { |
85 | Manager *m = data; | |
86 | ||
87 | assert(bus); | |
88 | assert(m); | |
2e317f52 LP |
89 | |
90 | if (!m->system_bus) | |
91 | return; | |
92 | ||
8e274523 LP |
93 | assert(m->system_bus == bus); |
94 | ||
95 | m->request_system_bus_dispatch = status != DBUS_DISPATCH_COMPLETE; | |
96 | } | |
97 | ||
ea430986 LP |
98 | static uint32_t bus_flags_to_events(DBusWatch *bus_watch) { |
99 | unsigned flags; | |
100 | uint32_t events = 0; | |
101 | ||
102 | assert(bus_watch); | |
103 | ||
104 | /* no watch flags for disabled watches */ | |
105 | if (!dbus_watch_get_enabled(bus_watch)) | |
106 | return 0; | |
107 | ||
108 | flags = dbus_watch_get_flags(bus_watch); | |
109 | ||
110 | if (flags & DBUS_WATCH_READABLE) | |
111 | events |= EPOLLIN; | |
112 | if (flags & DBUS_WATCH_WRITABLE) | |
113 | events |= EPOLLOUT; | |
114 | ||
115 | return events | EPOLLHUP | EPOLLERR; | |
116 | } | |
117 | ||
118 | static unsigned events_to_bus_flags(uint32_t events) { | |
119 | unsigned flags = 0; | |
120 | ||
121 | if (events & EPOLLIN) | |
122 | flags |= DBUS_WATCH_READABLE; | |
123 | if (events & EPOLLOUT) | |
124 | flags |= DBUS_WATCH_WRITABLE; | |
125 | if (events & EPOLLHUP) | |
126 | flags |= DBUS_WATCH_HANGUP; | |
127 | if (events & EPOLLERR) | |
128 | flags |= DBUS_WATCH_ERROR; | |
129 | ||
130 | return flags; | |
131 | } | |
132 | ||
133 | void bus_watch_event(Manager *m, Watch *w, int events) { | |
134 | assert(m); | |
135 | assert(w); | |
136 | ||
137 | /* This is called by the event loop whenever there is | |
138 | * something happening on D-Bus' file handles. */ | |
139 | ||
8e274523 | 140 | if (!dbus_watch_get_enabled(w->data.bus_watch)) |
ea430986 LP |
141 | return; |
142 | ||
143 | dbus_watch_handle(w->data.bus_watch, events_to_bus_flags(events)); | |
144 | } | |
145 | ||
146 | static dbus_bool_t bus_add_watch(DBusWatch *bus_watch, void *data) { | |
147 | Manager *m = data; | |
148 | Watch *w; | |
149 | struct epoll_event ev; | |
150 | ||
151 | assert(bus_watch); | |
152 | assert(m); | |
153 | ||
154 | if (!(w = new0(Watch, 1))) | |
155 | return FALSE; | |
156 | ||
157 | w->fd = dbus_watch_get_unix_fd(bus_watch); | |
158 | w->type = WATCH_DBUS_WATCH; | |
159 | w->data.bus_watch = bus_watch; | |
160 | ||
161 | zero(ev); | |
162 | ev.events = bus_flags_to_events(bus_watch); | |
163 | ev.data.ptr = w; | |
164 | ||
165 | if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) { | |
166 | ||
167 | if (errno != EEXIST) { | |
168 | free(w); | |
169 | return FALSE; | |
170 | } | |
171 | ||
172 | /* Hmm, bloody D-Bus creates multiple watches on the | |
173 | * same fd. epoll() does not like that. As a dirty | |
174 | * hack we simply dup() the fd and hence get a second | |
175 | * one we can safely add to the epoll(). */ | |
176 | ||
177 | if ((w->fd = dup(w->fd)) < 0) { | |
178 | free(w); | |
179 | return FALSE; | |
180 | } | |
181 | ||
182 | if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) { | |
183 | free(w); | |
184 | close_nointr_nofail(w->fd); | |
185 | return FALSE; | |
186 | } | |
187 | ||
188 | w->fd_is_dupped = true; | |
189 | } | |
190 | ||
191 | dbus_watch_set_data(bus_watch, w, NULL); | |
192 | ||
193 | return TRUE; | |
194 | } | |
195 | ||
196 | static void bus_remove_watch(DBusWatch *bus_watch, void *data) { | |
197 | Manager *m = data; | |
198 | Watch *w; | |
199 | ||
200 | assert(bus_watch); | |
201 | assert(m); | |
202 | ||
203 | if (!(w = dbus_watch_get_data(bus_watch))) | |
204 | return; | |
205 | ||
206 | assert(w->type == WATCH_DBUS_WATCH); | |
207 | assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0); | |
208 | ||
209 | if (w->fd_is_dupped) | |
210 | close_nointr_nofail(w->fd); | |
211 | ||
212 | free(w); | |
213 | } | |
214 | ||
215 | static void bus_toggle_watch(DBusWatch *bus_watch, void *data) { | |
216 | Manager *m = data; | |
217 | Watch *w; | |
218 | struct epoll_event ev; | |
219 | ||
220 | assert(bus_watch); | |
221 | assert(m); | |
222 | ||
223 | assert_se(w = dbus_watch_get_data(bus_watch)); | |
224 | assert(w->type == WATCH_DBUS_WATCH); | |
225 | ||
226 | zero(ev); | |
227 | ev.events = bus_flags_to_events(bus_watch); | |
228 | ev.data.ptr = w; | |
229 | ||
230 | assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_MOD, w->fd, &ev) == 0); | |
231 | } | |
232 | ||
233 | static int bus_timeout_arm(Manager *m, Watch *w) { | |
234 | struct itimerspec its; | |
235 | ||
236 | assert(m); | |
237 | assert(w); | |
238 | ||
239 | zero(its); | |
240 | ||
241 | if (dbus_timeout_get_enabled(w->data.bus_timeout)) { | |
242 | timespec_store(&its.it_value, dbus_timeout_get_interval(w->data.bus_timeout) * USEC_PER_MSEC); | |
243 | its.it_interval = its.it_interval; | |
244 | } | |
245 | ||
246 | if (timerfd_settime(w->fd, 0, &its, NULL) < 0) | |
247 | return -errno; | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
252 | void bus_timeout_event(Manager *m, Watch *w, int events) { | |
253 | assert(m); | |
254 | assert(w); | |
255 | ||
256 | /* This is called by the event loop whenever there is | |
257 | * something happening on D-Bus' file handles. */ | |
258 | ||
259 | if (!(dbus_timeout_get_enabled(w->data.bus_timeout))) | |
260 | return; | |
261 | ||
262 | dbus_timeout_handle(w->data.bus_timeout); | |
263 | } | |
264 | ||
265 | static dbus_bool_t bus_add_timeout(DBusTimeout *timeout, void *data) { | |
266 | Manager *m = data; | |
267 | Watch *w; | |
268 | struct epoll_event ev; | |
269 | ||
270 | assert(timeout); | |
271 | assert(m); | |
272 | ||
273 | if (!(w = new0(Watch, 1))) | |
274 | return FALSE; | |
275 | ||
276 | if (!(w->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0) | |
277 | goto fail; | |
278 | ||
279 | w->type = WATCH_DBUS_TIMEOUT; | |
280 | w->data.bus_timeout = timeout; | |
281 | ||
282 | if (bus_timeout_arm(m, w) < 0) | |
283 | goto fail; | |
284 | ||
285 | zero(ev); | |
286 | ev.events = EPOLLIN; | |
287 | ev.data.ptr = w; | |
288 | ||
289 | if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, w->fd, &ev) < 0) | |
290 | goto fail; | |
291 | ||
292 | dbus_timeout_set_data(timeout, w, NULL); | |
293 | ||
294 | return TRUE; | |
295 | ||
296 | fail: | |
297 | if (w->fd >= 0) | |
298 | close_nointr_nofail(w->fd); | |
299 | ||
300 | free(w); | |
301 | return FALSE; | |
302 | } | |
303 | ||
304 | static void bus_remove_timeout(DBusTimeout *timeout, void *data) { | |
305 | Manager *m = data; | |
306 | Watch *w; | |
307 | ||
308 | assert(timeout); | |
309 | assert(m); | |
310 | ||
311 | if (!(w = dbus_timeout_get_data(timeout))) | |
312 | return; | |
313 | ||
314 | assert(w->type == WATCH_DBUS_TIMEOUT); | |
315 | assert_se(epoll_ctl(m->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0); | |
316 | close_nointr_nofail(w->fd); | |
317 | free(w); | |
318 | } | |
319 | ||
320 | static void bus_toggle_timeout(DBusTimeout *timeout, void *data) { | |
321 | Manager *m = data; | |
322 | Watch *w; | |
323 | int r; | |
324 | ||
325 | assert(timeout); | |
326 | assert(m); | |
327 | ||
328 | assert_se(w = dbus_timeout_get_data(timeout)); | |
329 | assert(w->type == WATCH_DBUS_TIMEOUT); | |
330 | ||
331 | if ((r = bus_timeout_arm(m, w)) < 0) | |
332 | log_error("Failed to rearm timer: %s", strerror(-r)); | |
333 | } | |
334 | ||
f278026d | 335 | static DBusHandlerResult api_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) { |
c1e1601e LP |
336 | Manager *m = data; |
337 | DBusError error; | |
0034c15c | 338 | DBusMessage *reply = NULL; |
c1e1601e LP |
339 | |
340 | assert(connection); | |
341 | assert(message); | |
342 | assert(m); | |
343 | ||
344 | dbus_error_init(&error); | |
345 | ||
a5478cd4 LP |
346 | /* log_debug("Got D-Bus request: %s.%s() on %s", */ |
347 | /* dbus_message_get_interface(message), */ | |
348 | /* dbus_message_get_member(message), */ | |
349 | /* dbus_message_get_path(message)); */ | |
c1e1601e LP |
350 | |
351 | if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) { | |
2e317f52 | 352 | log_error("Warning! API D-Bus connection terminated."); |
f278026d | 353 | bus_done_api(m); |
c1e1601e LP |
354 | |
355 | } else if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) { | |
05e343b7 | 356 | const char *name, *old_owner, *new_owner; |
c1e1601e LP |
357 | |
358 | if (!dbus_message_get_args(message, &error, | |
359 | DBUS_TYPE_STRING, &name, | |
05e343b7 LP |
360 | DBUS_TYPE_STRING, &old_owner, |
361 | DBUS_TYPE_STRING, &new_owner, | |
c1e1601e LP |
362 | DBUS_TYPE_INVALID)) |
363 | log_error("Failed to parse NameOwnerChanged message: %s", error.message); | |
364 | else { | |
365 | if (set_remove(m->subscribed, (char*) name)) | |
366 | log_debug("Subscription client vanished: %s (left: %u)", name, set_size(m->subscribed)); | |
05e343b7 LP |
367 | |
368 | if (old_owner[0] == 0) | |
369 | old_owner = NULL; | |
370 | ||
371 | if (new_owner[0] == 0) | |
372 | new_owner = NULL; | |
373 | ||
374 | manager_dispatch_bus_name_owner_changed(m, name, old_owner, new_owner); | |
c1e1601e | 375 | } |
0034c15c LP |
376 | } else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Activator", "ActivationRequest")) { |
377 | const char *name; | |
378 | ||
379 | if (!dbus_message_get_args(message, &error, | |
380 | DBUS_TYPE_STRING, &name, | |
381 | DBUS_TYPE_INVALID)) | |
382 | log_error("Failed to parse ActivationRequest message: %s", error.message); | |
383 | else { | |
384 | int r; | |
385 | Unit *u; | |
386 | ||
387 | r = manager_load_unit(m, name, NULL, &u); | |
388 | ||
389 | if (r >= 0 && u->meta.only_by_dependency) | |
390 | r = -EPERM; | |
391 | ||
392 | if (r >= 0) | |
393 | r = manager_add_job(m, JOB_START, u, JOB_REPLACE, true, NULL); | |
394 | ||
395 | if (r < 0) { | |
396 | const char *id, *text; | |
397 | ||
398 | if (!(reply = dbus_message_new_signal("/org/freedesktop/systemd1", "org.freedesktop.systemd1.Activator", "ActivationFailure"))) | |
399 | goto oom; | |
400 | ||
401 | id = error_to_dbus(r); | |
402 | text = strerror(-r); | |
403 | ||
404 | if (!dbus_message_set_destination(reply, DBUS_SERVICE_DBUS) || | |
405 | !dbus_message_append_args(reply, | |
406 | DBUS_TYPE_STRING, &name, | |
407 | DBUS_TYPE_STRING, &id, | |
408 | DBUS_TYPE_STRING, &text, | |
409 | DBUS_TYPE_INVALID)) | |
410 | goto oom; | |
411 | } | |
412 | ||
413 | /* On success we don't do anything, the service will be spwaned now */ | |
414 | } | |
c1e1601e LP |
415 | } |
416 | ||
417 | dbus_error_free(&error); | |
0034c15c LP |
418 | |
419 | if (reply) { | |
420 | if (!dbus_connection_send(connection, reply, NULL)) | |
421 | goto oom; | |
422 | ||
423 | dbus_message_unref(reply); | |
424 | } | |
425 | ||
c1e1601e | 426 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
0034c15c LP |
427 | |
428 | oom: | |
429 | if (reply) | |
430 | dbus_message_unref(reply); | |
431 | ||
432 | dbus_error_free(&error); | |
433 | ||
434 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
c1e1601e LP |
435 | } |
436 | ||
8e274523 LP |
437 | static DBusHandlerResult system_bus_message_filter(DBusConnection *connection, DBusMessage *message, void *data) { |
438 | Manager *m = data; | |
439 | DBusError error; | |
440 | ||
441 | assert(connection); | |
442 | assert(message); | |
443 | assert(m); | |
444 | ||
445 | dbus_error_init(&error); | |
446 | ||
447 | /* log_debug("Got D-Bus request: %s.%s() on %s", */ | |
448 | /* dbus_message_get_interface(message), */ | |
449 | /* dbus_message_get_member(message), */ | |
450 | /* dbus_message_get_path(message)); */ | |
451 | ||
452 | if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) { | |
2e317f52 | 453 | log_error("Warning! System D-Bus connection terminated."); |
f278026d | 454 | bus_done_system(m); |
8e274523 LP |
455 | |
456 | } if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Agent", "Released")) { | |
457 | const char *cgroup; | |
458 | ||
459 | if (!dbus_message_get_args(message, &error, | |
460 | DBUS_TYPE_STRING, &cgroup, | |
461 | DBUS_TYPE_INVALID)) | |
462 | log_error("Failed to parse Released message: %s", error.message); | |
463 | else | |
464 | cgroup_notify_empty(m, cgroup); | |
465 | } | |
466 | ||
467 | dbus_error_free(&error); | |
468 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
469 | } | |
470 | ||
c1e1601e | 471 | unsigned bus_dispatch(Manager *m) { |
ea430986 LP |
472 | assert(m); |
473 | ||
a16e1123 LP |
474 | if (m->queued_message) { |
475 | /* If we cannot get rid of this message we won't | |
476 | * dispatch any D-Bus messages, so that we won't end | |
477 | * up wanting to queue another message. */ | |
478 | ||
479 | if (!dbus_connection_send(m->api_bus, m->queued_message, NULL)) | |
480 | return 0; | |
481 | ||
482 | dbus_message_unref(m->queued_message); | |
483 | m->queued_message = NULL; | |
484 | } | |
485 | ||
fd18e1f4 LP |
486 | if (m->request_api_bus_dispatch) { |
487 | if (dbus_connection_dispatch(m->api_bus) == DBUS_DISPATCH_COMPLETE) | |
f278026d | 488 | m->request_api_bus_dispatch = false; |
c1e1601e | 489 | |
fd18e1f4 LP |
490 | return 1; |
491 | } | |
492 | ||
493 | if (m->request_system_bus_dispatch) { | |
494 | if (dbus_connection_dispatch(m->system_bus) == DBUS_DISPATCH_COMPLETE) | |
8e274523 | 495 | m->request_system_bus_dispatch = false; |
fd18e1f4 LP |
496 | |
497 | return 1; | |
498 | } | |
c1e1601e | 499 | |
8e274523 | 500 | return 0; |
ea430986 LP |
501 | } |
502 | ||
05e343b7 | 503 | static void request_name_pending_cb(DBusPendingCall *pending, void *userdata) { |
61902ea3 LP |
504 | DBusMessage *reply; |
505 | DBusError error; | |
506 | ||
507 | dbus_error_init(&error); | |
508 | ||
509 | assert_se(reply = dbus_pending_call_steal_reply(pending)); | |
510 | ||
511 | switch (dbus_message_get_type(reply)) { | |
512 | ||
513 | case DBUS_MESSAGE_TYPE_ERROR: | |
514 | ||
515 | assert_se(dbus_set_error_from_message(&error, reply)); | |
516 | log_warning("RequestName() failed: %s", error.message); | |
517 | break; | |
518 | ||
519 | case DBUS_MESSAGE_TYPE_METHOD_RETURN: { | |
520 | uint32_t r; | |
521 | ||
522 | if (!dbus_message_get_args(reply, | |
523 | &error, | |
524 | DBUS_TYPE_UINT32, &r, | |
525 | DBUS_TYPE_INVALID)) { | |
526 | log_error("Failed to parse RequestName() reply: %s", error.message); | |
527 | break; | |
528 | } | |
529 | ||
530 | if (r == 1) | |
531 | log_debug("Successfully acquired name."); | |
532 | else | |
533 | log_error("Name already owned."); | |
534 | ||
535 | break; | |
536 | } | |
537 | ||
538 | default: | |
539 | assert_not_reached("Invalid reply message"); | |
540 | } | |
541 | ||
542 | dbus_message_unref(reply); | |
543 | dbus_error_free(&error); | |
544 | } | |
545 | ||
ea430986 | 546 | static int request_name(Manager *m) { |
ea430986 LP |
547 | const char *name = "org.freedesktop.systemd1"; |
548 | uint32_t flags = 0; | |
05e343b7 LP |
549 | DBusMessage *message = NULL; |
550 | DBusPendingCall *pending = NULL; | |
ea430986 LP |
551 | |
552 | if (!(message = dbus_message_new_method_call( | |
553 | DBUS_SERVICE_DBUS, | |
554 | DBUS_PATH_DBUS, | |
555 | DBUS_INTERFACE_DBUS, | |
556 | "RequestName"))) | |
05e343b7 | 557 | goto oom; |
ea430986 LP |
558 | |
559 | if (!dbus_message_append_args( | |
560 | message, | |
561 | DBUS_TYPE_STRING, &name, | |
562 | DBUS_TYPE_UINT32, &flags, | |
05e343b7 LP |
563 | DBUS_TYPE_INVALID)) |
564 | goto oom; | |
ea430986 | 565 | |
05e343b7 LP |
566 | if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1)) |
567 | goto oom; | |
ea430986 | 568 | |
05e343b7 LP |
569 | if (!dbus_pending_call_set_notify(pending, request_name_pending_cb, m, NULL)) |
570 | goto oom; | |
ea430986 LP |
571 | |
572 | dbus_message_unref(message); | |
61902ea3 LP |
573 | dbus_pending_call_unref(pending); |
574 | ||
575 | /* We simple ask for the name and don't wait for it. Sooner or | |
576 | * later we'll have it. */ | |
577 | ||
ea430986 | 578 | return 0; |
05e343b7 LP |
579 | |
580 | oom: | |
581 | if (pending) { | |
582 | dbus_pending_call_cancel(pending); | |
583 | dbus_pending_call_unref(pending); | |
584 | } | |
585 | ||
586 | if (message) | |
587 | dbus_message_unref(message); | |
588 | ||
589 | return -ENOMEM; | |
ea430986 LP |
590 | } |
591 | ||
8e274523 LP |
592 | static int bus_setup_loop(Manager *m, DBusConnection *bus) { |
593 | assert(m); | |
594 | assert(bus); | |
595 | ||
596 | dbus_connection_set_exit_on_disconnect(bus, FALSE); | |
fd18e1f4 | 597 | |
8e274523 LP |
598 | if (!dbus_connection_set_watch_functions(bus, bus_add_watch, bus_remove_watch, bus_toggle_watch, m, NULL) || |
599 | !dbus_connection_set_timeout_functions(bus, bus_add_timeout, bus_remove_timeout, bus_toggle_timeout, m, NULL)) | |
600 | return -ENOMEM; | |
601 | ||
602 | return 0; | |
603 | } | |
604 | ||
f278026d | 605 | int bus_init_system(Manager *m) { |
ea430986 LP |
606 | DBusError error; |
607 | char *id; | |
608 | int r; | |
609 | ||
610 | assert(m); | |
611 | ||
ea430986 | 612 | dbus_error_init(&error); |
ea430986 | 613 | |
f278026d LP |
614 | if (m->system_bus) |
615 | return 0; | |
8e274523 | 616 | |
f278026d LP |
617 | if (m->running_as != MANAGER_SESSION && m->api_bus) |
618 | m->system_bus = m->api_bus; | |
619 | else { | |
8e274523 | 620 | if (!(m->system_bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error))) { |
266e525c | 621 | log_debug("Failed to get system D-Bus connection, retrying later: %s", error.message); |
8e274523 | 622 | dbus_error_free(&error); |
f278026d | 623 | return 0; |
8e274523 LP |
624 | } |
625 | ||
fd18e1f4 LP |
626 | dbus_connection_set_dispatch_status_function(m->system_bus, system_bus_dispatch_status, m, NULL); |
627 | m->request_system_bus_dispatch = true; | |
628 | ||
8e274523 | 629 | if ((r = bus_setup_loop(m, m->system_bus)) < 0) { |
f278026d | 630 | bus_done_system(m); |
8e274523 LP |
631 | return r; |
632 | } | |
f278026d LP |
633 | } |
634 | ||
635 | if (!dbus_connection_add_filter(m->system_bus, system_bus_message_filter, m, NULL)) { | |
636 | bus_done_system(m); | |
c1e1601e LP |
637 | return -ENOMEM; |
638 | } | |
639 | ||
f278026d | 640 | dbus_bus_add_match(m->system_bus, |
c1e1601e | 641 | "type='signal'," |
f278026d LP |
642 | "interface='org.freedesktop.systemd1.Agent'," |
643 | "path='/org/freedesktop/systemd1/agent'", | |
c1e1601e LP |
644 | &error); |
645 | ||
646 | if (dbus_error_is_set(&error)) { | |
647 | log_error("Failed to register match: %s", error.message); | |
648 | dbus_error_free(&error); | |
f278026d | 649 | bus_done_system(m); |
ea430986 LP |
650 | return -ENOMEM; |
651 | } | |
652 | ||
f278026d LP |
653 | log_debug("Successfully connected to system D-Bus bus %s as %s", |
654 | strnull((id = dbus_connection_get_server_id(m->system_bus))), | |
655 | strnull(dbus_bus_get_unique_name(m->system_bus))); | |
656 | dbus_free(id); | |
657 | ||
f278026d LP |
658 | return 0; |
659 | } | |
660 | ||
661 | int bus_init_api(Manager *m) { | |
662 | DBusError error; | |
663 | char *id; | |
664 | int r; | |
665 | ||
666 | assert(m); | |
667 | ||
668 | dbus_error_init(&error); | |
669 | ||
670 | if (m->api_bus) | |
671 | return 0; | |
672 | ||
05e343b7 LP |
673 | if (m->name_data_slot < 0) |
674 | if (!dbus_pending_call_allocate_data_slot(&m->name_data_slot)) | |
675 | return -ENOMEM; | |
676 | ||
f278026d LP |
677 | if (m->running_as != MANAGER_SESSION && m->system_bus) |
678 | m->api_bus = m->system_bus; | |
679 | else { | |
680 | if (!(m->api_bus = dbus_bus_get_private(m->running_as == MANAGER_SESSION ? DBUS_BUS_SESSION : DBUS_BUS_SYSTEM, &error))) { | |
266e525c | 681 | log_debug("Failed to get API D-Bus connection, retrying later: %s", error.message); |
f278026d LP |
682 | dbus_error_free(&error); |
683 | return 0; | |
684 | } | |
685 | ||
fd18e1f4 LP |
686 | dbus_connection_set_dispatch_status_function(m->api_bus, api_bus_dispatch_status, m, NULL); |
687 | m->request_api_bus_dispatch = true; | |
688 | ||
f278026d LP |
689 | if ((r = bus_setup_loop(m, m->api_bus)) < 0) { |
690 | bus_done_api(m); | |
691 | return r; | |
692 | } | |
ea430986 LP |
693 | } |
694 | ||
f278026d LP |
695 | if (!dbus_connection_register_object_path(m->api_bus, "/org/freedesktop/systemd1", &bus_manager_vtable, m) || |
696 | !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/unit", &bus_unit_vtable, m) || | |
697 | !dbus_connection_register_fallback(m->api_bus, "/org/freedesktop/systemd1/job", &bus_job_vtable, m) || | |
698 | !dbus_connection_add_filter(m->api_bus, api_bus_message_filter, m, NULL)) { | |
699 | bus_done_api(m); | |
700 | return -ENOMEM; | |
701 | } | |
702 | ||
0034c15c | 703 | /* Get NameOwnerChange messages */ |
f278026d | 704 | dbus_bus_add_match(m->api_bus, |
8e274523 | 705 | "type='signal'," |
f278026d LP |
706 | "sender='"DBUS_SERVICE_DBUS"'," |
707 | "interface='"DBUS_INTERFACE_DBUS"'," | |
708 | "path='"DBUS_PATH_DBUS"'", | |
8e274523 LP |
709 | &error); |
710 | ||
711 | if (dbus_error_is_set(&error)) { | |
712 | log_error("Failed to register match: %s", error.message); | |
713 | dbus_error_free(&error); | |
f278026d | 714 | bus_done_api(m); |
8e274523 LP |
715 | return -ENOMEM; |
716 | } | |
717 | ||
0034c15c LP |
718 | /* Get activation requests */ |
719 | dbus_bus_add_match(m->api_bus, | |
720 | "type='signal'," | |
721 | "sender='"DBUS_SERVICE_DBUS"'," | |
722 | "interface='org.freedesktop.systemd1.Activator'," | |
723 | "path='"DBUS_PATH_DBUS"'", | |
724 | &error); | |
725 | ||
726 | if (dbus_error_is_set(&error)) { | |
727 | log_error("Failed to register match: %s", error.message); | |
728 | dbus_error_free(&error); | |
729 | bus_done_api(m); | |
730 | return -ENOMEM; | |
731 | } | |
732 | ||
f278026d LP |
733 | if ((r = request_name(m)) < 0) { |
734 | bus_done_api(m); | |
735 | return r; | |
736 | } | |
ea430986 | 737 | |
f278026d LP |
738 | log_debug("Successfully connected to API D-Bus bus %s as %s", |
739 | strnull((id = dbus_connection_get_server_id(m->api_bus))), | |
740 | strnull(dbus_bus_get_unique_name(m->api_bus))); | |
8e274523 LP |
741 | dbus_free(id); |
742 | ||
f278026d LP |
743 | if (!m->subscribed) |
744 | if (!(m->subscribed = set_new(string_hash_func, string_compare_func))) | |
745 | return -ENOMEM; | |
746 | ||
ea430986 LP |
747 | return 0; |
748 | } | |
749 | ||
f278026d | 750 | void bus_done_api(Manager *m) { |
ea430986 LP |
751 | assert(m); |
752 | ||
f278026d LP |
753 | if (m->api_bus) { |
754 | if (m->system_bus == m->api_bus) | |
755 | m->system_bus = NULL; | |
756 | ||
90d3f30b | 757 | dbus_connection_set_dispatch_status_function(m->api_bus, NULL, NULL, NULL); |
8e2f43b3 | 758 | dbus_connection_flush(m->api_bus); |
2e317f52 | 759 | dbus_connection_close(m->api_bus); |
f278026d LP |
760 | dbus_connection_unref(m->api_bus); |
761 | m->api_bus = NULL; | |
ea430986 | 762 | } |
c1e1601e LP |
763 | |
764 | if (m->subscribed) { | |
765 | char *c; | |
766 | ||
767 | while ((c = set_steal_first(m->subscribed))) | |
768 | free(c); | |
769 | ||
770 | set_free(m->subscribed); | |
771 | m->subscribed = NULL; | |
772 | } | |
05e343b7 LP |
773 | |
774 | if (m->name_data_slot >= 0) | |
775 | dbus_pending_call_free_data_slot(&m->name_data_slot); | |
a16e1123 LP |
776 | |
777 | if (m->queued_message) { | |
778 | dbus_message_unref(m->queued_message); | |
779 | m->queued_message = NULL; | |
780 | } | |
ea430986 LP |
781 | } |
782 | ||
f278026d LP |
783 | void bus_done_system(Manager *m) { |
784 | assert(m); | |
785 | ||
786 | if (m->system_bus == m->api_bus) | |
787 | bus_done_api(m); | |
788 | ||
789 | if (m->system_bus) { | |
90d3f30b | 790 | dbus_connection_set_dispatch_status_function(m->system_bus, NULL, NULL, NULL); |
8e2f43b3 | 791 | dbus_connection_flush(m->system_bus); |
2e317f52 | 792 | dbus_connection_close(m->system_bus); |
f278026d LP |
793 | dbus_connection_unref(m->system_bus); |
794 | m->system_bus = NULL; | |
795 | } | |
796 | } | |
797 | ||
05e343b7 LP |
798 | static void query_pid_pending_cb(DBusPendingCall *pending, void *userdata) { |
799 | Manager *m = userdata; | |
800 | DBusMessage *reply; | |
801 | DBusError error; | |
802 | const char *name; | |
803 | ||
804 | dbus_error_init(&error); | |
805 | ||
806 | assert_se(name = dbus_pending_call_get_data(pending, m->name_data_slot)); | |
807 | assert_se(reply = dbus_pending_call_steal_reply(pending)); | |
808 | ||
809 | switch (dbus_message_get_type(reply)) { | |
810 | ||
811 | case DBUS_MESSAGE_TYPE_ERROR: | |
812 | ||
813 | assert_se(dbus_set_error_from_message(&error, reply)); | |
814 | log_warning("GetConnectionUnixProcessID() failed: %s", error.message); | |
815 | break; | |
816 | ||
817 | case DBUS_MESSAGE_TYPE_METHOD_RETURN: { | |
818 | uint32_t r; | |
819 | ||
820 | if (!dbus_message_get_args(reply, | |
821 | &error, | |
822 | DBUS_TYPE_UINT32, &r, | |
823 | DBUS_TYPE_INVALID)) { | |
824 | log_error("Failed to parse GetConnectionUnixProcessID() reply: %s", error.message); | |
825 | break; | |
826 | } | |
827 | ||
828 | manager_dispatch_bus_query_pid_done(m, name, (pid_t) r); | |
829 | break; | |
830 | } | |
831 | ||
832 | default: | |
833 | assert_not_reached("Invalid reply message"); | |
834 | } | |
835 | ||
836 | dbus_message_unref(reply); | |
837 | dbus_error_free(&error); | |
838 | } | |
839 | ||
840 | int bus_query_pid(Manager *m, const char *name) { | |
841 | DBusMessage *message = NULL; | |
842 | DBusPendingCall *pending = NULL; | |
843 | char *n = NULL; | |
844 | ||
845 | assert(m); | |
846 | assert(name); | |
847 | ||
848 | if (!(message = dbus_message_new_method_call( | |
849 | DBUS_SERVICE_DBUS, | |
850 | DBUS_PATH_DBUS, | |
851 | DBUS_INTERFACE_DBUS, | |
852 | "GetConnectionUnixProcessID"))) | |
853 | goto oom; | |
854 | ||
855 | if (!(dbus_message_append_args( | |
856 | message, | |
857 | DBUS_TYPE_STRING, &name, | |
858 | DBUS_TYPE_INVALID))) | |
859 | goto oom; | |
860 | ||
861 | if (!dbus_connection_send_with_reply(m->api_bus, message, &pending, -1)) | |
862 | goto oom; | |
863 | ||
864 | if (!(n = strdup(name))) | |
865 | goto oom; | |
866 | ||
867 | if (!dbus_pending_call_set_data(pending, m->name_data_slot, n, free)) | |
868 | goto oom; | |
869 | ||
870 | n = NULL; | |
871 | ||
872 | if (!dbus_pending_call_set_notify(pending, query_pid_pending_cb, m, NULL)) | |
873 | goto oom; | |
874 | ||
875 | dbus_message_unref(message); | |
876 | dbus_pending_call_unref(pending); | |
877 | ||
878 | return 0; | |
879 | ||
880 | oom: | |
881 | free(n); | |
882 | ||
883 | if (pending) { | |
884 | dbus_pending_call_cancel(pending); | |
885 | dbus_pending_call_unref(pending); | |
886 | } | |
887 | ||
888 | if (message) | |
889 | dbus_message_unref(message); | |
890 | ||
891 | return -ENOMEM; | |
892 | } | |
893 | ||
ea430986 LP |
894 | DBusHandlerResult bus_default_message_handler(Manager *m, DBusMessage *message, const char*introspection, const BusProperty *properties) { |
895 | DBusError error; | |
896 | DBusMessage *reply = NULL; | |
897 | int r; | |
898 | ||
899 | assert(m); | |
900 | assert(message); | |
901 | ||
902 | dbus_error_init(&error); | |
903 | ||
904 | if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection) { | |
905 | ||
906 | if (!(reply = dbus_message_new_method_return(message))) | |
907 | goto oom; | |
908 | ||
909 | if (!dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection, DBUS_TYPE_INVALID)) | |
910 | goto oom; | |
911 | ||
912 | } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "Get") && properties) { | |
913 | const char *interface, *property; | |
914 | const BusProperty *p; | |
915 | ||
916 | if (!dbus_message_get_args( | |
917 | message, | |
918 | &error, | |
919 | DBUS_TYPE_STRING, &interface, | |
920 | DBUS_TYPE_STRING, &property, | |
921 | DBUS_TYPE_INVALID)) | |
922 | return bus_send_error_reply(m, message, &error, -EINVAL); | |
923 | ||
924 | for (p = properties; p->property; p++) | |
925 | if (streq(p->interface, interface) && streq(p->property, property)) | |
926 | break; | |
927 | ||
928 | if (p->property) { | |
929 | DBusMessageIter iter, sub; | |
930 | ||
931 | if (!(reply = dbus_message_new_method_return(message))) | |
932 | goto oom; | |
933 | ||
934 | dbus_message_iter_init_append(reply, &iter); | |
935 | ||
936 | if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->signature, &sub)) | |
937 | goto oom; | |
938 | ||
b9f49ee4 | 939 | if ((r = p->append(m, &sub, property, (void*) p->data)) < 0) { |
ea430986 LP |
940 | |
941 | if (r == -ENOMEM) | |
942 | goto oom; | |
943 | ||
944 | dbus_message_unref(reply); | |
945 | return bus_send_error_reply(m, message, NULL, r); | |
946 | } | |
947 | ||
948 | if (!dbus_message_iter_close_container(&iter, &sub)) | |
949 | goto oom; | |
950 | } | |
951 | } else if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Properties", "GetAll") && properties) { | |
952 | const char *interface; | |
953 | const BusProperty *p; | |
954 | DBusMessageIter iter, sub, sub2, sub3; | |
955 | bool any = false; | |
956 | ||
957 | if (!dbus_message_get_args( | |
958 | message, | |
959 | &error, | |
960 | DBUS_TYPE_STRING, &interface, | |
961 | DBUS_TYPE_INVALID)) | |
962 | return bus_send_error_reply(m, message, &error, -EINVAL); | |
963 | ||
964 | if (!(reply = dbus_message_new_method_return(message))) | |
965 | goto oom; | |
966 | ||
967 | dbus_message_iter_init_append(reply, &iter); | |
968 | ||
969 | if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub)) | |
970 | goto oom; | |
971 | ||
972 | for (p = properties; p->property; p++) { | |
973 | if (!streq(p->interface, interface)) | |
974 | continue; | |
975 | ||
c401a1e0 | 976 | if (!dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2) || |
ea430986 LP |
977 | !dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &p->property) || |
978 | !dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, p->signature, &sub3)) | |
979 | goto oom; | |
980 | ||
b9f49ee4 | 981 | if ((r = p->append(m, &sub3, p->property, (void*) p->data)) < 0) { |
ea430986 LP |
982 | |
983 | if (r == -ENOMEM) | |
984 | goto oom; | |
985 | ||
986 | dbus_message_unref(reply); | |
987 | return bus_send_error_reply(m, message, NULL, r); | |
988 | } | |
989 | ||
990 | if (!dbus_message_iter_close_container(&sub2, &sub3) || | |
991 | !dbus_message_iter_close_container(&sub, &sub2)) | |
992 | goto oom; | |
993 | ||
994 | any = true; | |
995 | } | |
996 | ||
997 | if (!dbus_message_iter_close_container(&iter, &sub)) | |
998 | goto oom; | |
999 | } | |
1000 | ||
1001 | if (reply) { | |
f278026d | 1002 | if (!dbus_connection_send(m->api_bus, reply, NULL)) |
ea430986 LP |
1003 | goto oom; |
1004 | ||
1005 | dbus_message_unref(reply); | |
1006 | return DBUS_HANDLER_RESULT_HANDLED; | |
1007 | } | |
1008 | ||
1009 | return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
1010 | ||
1011 | oom: | |
1012 | if (reply) | |
1013 | dbus_message_unref(reply); | |
1014 | ||
1015 | dbus_error_free(&error); | |
1016 | ||
1017 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
1018 | } | |
1019 | ||
ea430986 LP |
1020 | static const char *error_to_dbus(int error) { |
1021 | ||
1022 | switch(error) { | |
1023 | ||
1024 | case -EINVAL: | |
1025 | return DBUS_ERROR_INVALID_ARGS; | |
1026 | ||
1027 | case -ENOMEM: | |
1028 | return DBUS_ERROR_NO_MEMORY; | |
1029 | ||
1030 | case -EPERM: | |
1031 | case -EACCES: | |
1032 | return DBUS_ERROR_ACCESS_DENIED; | |
1033 | ||
1034 | case -ESRCH: | |
1035 | return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN; | |
1036 | ||
1037 | case -ENOENT: | |
1038 | return DBUS_ERROR_FILE_NOT_FOUND; | |
1039 | ||
1040 | case -EEXIST: | |
1041 | return DBUS_ERROR_FILE_EXISTS; | |
1042 | ||
1043 | case -ETIMEDOUT: | |
1044 | return DBUS_ERROR_TIMEOUT; | |
1045 | ||
1046 | case -EIO: | |
1047 | return DBUS_ERROR_IO_ERROR; | |
1048 | ||
1049 | case -ENETRESET: | |
1050 | case -ECONNABORTED: | |
1051 | case -ECONNRESET: | |
1052 | return DBUS_ERROR_DISCONNECTED; | |
1053 | } | |
1054 | ||
1055 | return DBUS_ERROR_FAILED; | |
1056 | } | |
1057 | ||
1058 | DBusHandlerResult bus_send_error_reply(Manager *m, DBusMessage *message, DBusError *bus_error, int error) { | |
1059 | DBusMessage *reply = NULL; | |
1060 | const char *name, *text; | |
1061 | ||
1062 | if (bus_error && dbus_error_is_set(bus_error)) { | |
1063 | name = bus_error->name; | |
1064 | text = bus_error->message; | |
1065 | } else { | |
1066 | name = error_to_dbus(error); | |
1067 | text = strerror(-error); | |
1068 | } | |
1069 | ||
1070 | if (!(reply = dbus_message_new_error(message, name, text))) | |
1071 | goto oom; | |
1072 | ||
f278026d | 1073 | if (!dbus_connection_send(m->api_bus, reply, NULL)) |
ea430986 LP |
1074 | goto oom; |
1075 | ||
1076 | dbus_message_unref(reply); | |
1077 | ||
1078 | if (bus_error) | |
1079 | dbus_error_free(bus_error); | |
1080 | ||
1081 | return DBUS_HANDLER_RESULT_HANDLED; | |
1082 | ||
1083 | oom: | |
1084 | if (reply) | |
1085 | dbus_message_unref(reply); | |
1086 | ||
1087 | if (bus_error) | |
1088 | dbus_error_free(bus_error); | |
1089 | ||
1090 | return DBUS_HANDLER_RESULT_NEED_MEMORY; | |
1091 | } | |
1092 | ||
1093 | int bus_property_append_string(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
1094 | const char *t = data; | |
1095 | ||
1096 | assert(m); | |
1097 | assert(i); | |
1098 | assert(property); | |
38131695 LP |
1099 | |
1100 | if (!t) | |
1101 | t = ""; | |
ea430986 LP |
1102 | |
1103 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_STRING, &t)) | |
1104 | return -ENOMEM; | |
1105 | ||
1106 | return 0; | |
1107 | } | |
1108 | ||
1109 | int bus_property_append_strv(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
1110 | DBusMessageIter sub; | |
1111 | char **t = data; | |
1112 | ||
1113 | assert(m); | |
1114 | assert(i); | |
1115 | assert(property); | |
1116 | ||
1117 | if (!dbus_message_iter_open_container(i, DBUS_TYPE_ARRAY, "s", &sub)) | |
1118 | return -ENOMEM; | |
1119 | ||
1120 | STRV_FOREACH(t, t) | |
1121 | if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, t)) | |
1122 | return -ENOMEM; | |
1123 | ||
1124 | if (!dbus_message_iter_close_container(i, &sub)) | |
1125 | return -ENOMEM; | |
1126 | ||
1127 | return 0; | |
1128 | } | |
1129 | ||
1130 | int bus_property_append_bool(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
1131 | bool *b = data; | |
1132 | dbus_bool_t db; | |
1133 | ||
1134 | assert(m); | |
1135 | assert(i); | |
1136 | assert(property); | |
1137 | assert(b); | |
1138 | ||
1139 | db = *b; | |
1140 | ||
1141 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db)) | |
1142 | return -ENOMEM; | |
1143 | ||
1144 | return 0; | |
1145 | } | |
38131695 LP |
1146 | |
1147 | int bus_property_append_uint64(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
1148 | assert(m); | |
1149 | assert(i); | |
1150 | assert(property); | |
1151 | assert(data); | |
1152 | ||
4139c1b2 LP |
1153 | /* Let's ensure that pid_t is actually 64bit, and hence this |
1154 | * function can be used for usec_t */ | |
1155 | assert_cc(sizeof(uint64_t) == sizeof(usec_t)); | |
1156 | ||
38131695 LP |
1157 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT64, data)) |
1158 | return -ENOMEM; | |
1159 | ||
1160 | return 0; | |
1161 | } | |
1162 | ||
1163 | int bus_property_append_uint32(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
1164 | assert(m); | |
1165 | assert(i); | |
1166 | assert(property); | |
1167 | assert(data); | |
1168 | ||
4139c1b2 LP |
1169 | /* Let's ensure that pid_t and mode_t is actually 32bit, and |
1170 | * hence this function can be used for pid_t/mode_t */ | |
1171 | assert_cc(sizeof(uint32_t) == sizeof(pid_t)); | |
1172 | assert_cc(sizeof(uint32_t) == sizeof(mode_t)); | |
1173 | assert_cc(sizeof(uint32_t) == sizeof(unsigned)); | |
1174 | ||
38131695 LP |
1175 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_UINT32, data)) |
1176 | return -ENOMEM; | |
1177 | ||
1178 | return 0; | |
1179 | } | |
4139c1b2 LP |
1180 | |
1181 | int bus_property_append_int32(Manager *m, DBusMessageIter *i, const char *property, void *data) { | |
1182 | assert(m); | |
1183 | assert(i); | |
1184 | assert(property); | |
1185 | assert(data); | |
1186 | ||
1187 | assert_cc(sizeof(int32_t) == sizeof(int)); | |
1188 | ||
1189 | if (!dbus_message_iter_append_basic(i, DBUS_TYPE_INT32, data)) | |
1190 | return -ENOMEM; | |
1191 | ||
1192 | return 0; | |
1193 | } | |
1137a57c LP |
1194 | |
1195 | int bus_parse_strv(DBusMessage *m, char ***_l) { | |
1196 | DBusMessageIter iter, sub; | |
1197 | unsigned n = 0, i = 0; | |
1198 | char **l; | |
1199 | ||
1200 | assert(m); | |
1201 | assert(_l); | |
1202 | ||
1203 | if (!dbus_message_iter_init(m, &iter) || | |
1204 | dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY || | |
1205 | dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_STRING) | |
1206 | return -EINVAL; | |
1207 | ||
1208 | dbus_message_iter_recurse(&iter, &sub); | |
1209 | ||
1210 | while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { | |
1211 | n++; | |
1212 | dbus_message_iter_next(&sub); | |
1213 | } | |
1214 | ||
1215 | if (!(l = new(char*, n+1))) | |
1216 | return -ENOMEM; | |
1217 | ||
1218 | assert_se(dbus_message_iter_init(m, &iter)); | |
1219 | dbus_message_iter_recurse(&iter, &sub); | |
1220 | ||
1221 | while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { | |
1222 | const char *s; | |
1223 | ||
1224 | assert_se(dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING); | |
1225 | dbus_message_iter_get_basic(&sub, &s); | |
1226 | ||
1227 | if (!(l[i++] = strdup(s))) { | |
1228 | strv_free(l); | |
1229 | return -ENOMEM; | |
1230 | } | |
1231 | ||
1232 | dbus_message_iter_next(&sub); | |
1233 | } | |
1234 | ||
1235 | assert(i == n); | |
1236 | l[i] = NULL; | |
1237 | ||
1238 | if (_l) | |
1239 | *_l = l; | |
1240 | ||
1241 | return 0; | |
1242 | } |