1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
6 #include <sys/timerfd.h>
14 static void bus_dispatch_status(DBusConnection
*bus
, DBusDispatchStatus status
, void *data
) {
20 m
->request_bus_dispatch
= status
!= DBUS_DISPATCH_COMPLETE
;
23 static uint32_t bus_flags_to_events(DBusWatch
*bus_watch
) {
29 /* no watch flags for disabled watches */
30 if (!dbus_watch_get_enabled(bus_watch
))
33 flags
= dbus_watch_get_flags(bus_watch
);
35 if (flags
& DBUS_WATCH_READABLE
)
37 if (flags
& DBUS_WATCH_WRITABLE
)
40 return events
| EPOLLHUP
| EPOLLERR
;
43 static unsigned events_to_bus_flags(uint32_t events
) {
47 flags
|= DBUS_WATCH_READABLE
;
48 if (events
& EPOLLOUT
)
49 flags
|= DBUS_WATCH_WRITABLE
;
50 if (events
& EPOLLHUP
)
51 flags
|= DBUS_WATCH_HANGUP
;
52 if (events
& EPOLLERR
)
53 flags
|= DBUS_WATCH_ERROR
;
58 void bus_watch_event(Manager
*m
, Watch
*w
, int events
) {
62 /* This is called by the event loop whenever there is
63 * something happening on D-Bus' file handles. */
65 if (!(dbus_watch_get_enabled(w
->data
.bus_watch
)))
68 dbus_watch_handle(w
->data
.bus_watch
, events_to_bus_flags(events
));
71 static dbus_bool_t
bus_add_watch(DBusWatch
*bus_watch
, void *data
) {
74 struct epoll_event ev
;
79 if (!(w
= new0(Watch
, 1)))
82 w
->fd
= dbus_watch_get_unix_fd(bus_watch
);
83 w
->type
= WATCH_DBUS_WATCH
;
84 w
->data
.bus_watch
= bus_watch
;
87 ev
.events
= bus_flags_to_events(bus_watch
);
90 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, w
->fd
, &ev
) < 0) {
92 if (errno
!= EEXIST
) {
97 /* Hmm, bloody D-Bus creates multiple watches on the
98 * same fd. epoll() does not like that. As a dirty
99 * hack we simply dup() the fd and hence get a second
100 * one we can safely add to the epoll(). */
102 if ((w
->fd
= dup(w
->fd
)) < 0) {
107 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, w
->fd
, &ev
) < 0) {
109 close_nointr_nofail(w
->fd
);
113 w
->fd_is_dupped
= true;
116 dbus_watch_set_data(bus_watch
, w
, NULL
);
121 static void bus_remove_watch(DBusWatch
*bus_watch
, void *data
) {
128 if (!(w
= dbus_watch_get_data(bus_watch
)))
131 assert(w
->type
== WATCH_DBUS_WATCH
);
132 assert_se(epoll_ctl(m
->epoll_fd
, EPOLL_CTL_DEL
, w
->fd
, NULL
) >= 0);
135 close_nointr_nofail(w
->fd
);
140 static void bus_toggle_watch(DBusWatch
*bus_watch
, void *data
) {
143 struct epoll_event ev
;
148 assert_se(w
= dbus_watch_get_data(bus_watch
));
149 assert(w
->type
== WATCH_DBUS_WATCH
);
152 ev
.events
= bus_flags_to_events(bus_watch
);
155 assert_se(epoll_ctl(m
->epoll_fd
, EPOLL_CTL_MOD
, w
->fd
, &ev
) == 0);
158 static int bus_timeout_arm(Manager
*m
, Watch
*w
) {
159 struct itimerspec its
;
166 if (dbus_timeout_get_enabled(w
->data
.bus_timeout
)) {
167 timespec_store(&its
.it_value
, dbus_timeout_get_interval(w
->data
.bus_timeout
) * USEC_PER_MSEC
);
168 its
.it_interval
= its
.it_interval
;
171 if (timerfd_settime(w
->fd
, 0, &its
, NULL
) < 0)
177 void bus_timeout_event(Manager
*m
, Watch
*w
, int events
) {
181 /* This is called by the event loop whenever there is
182 * something happening on D-Bus' file handles. */
184 if (!(dbus_timeout_get_enabled(w
->data
.bus_timeout
)))
187 dbus_timeout_handle(w
->data
.bus_timeout
);
190 static dbus_bool_t
bus_add_timeout(DBusTimeout
*timeout
, void *data
) {
193 struct epoll_event ev
;
198 if (!(w
= new0(Watch
, 1)))
201 if (!(w
->fd
= timerfd_create(CLOCK_MONOTONIC
, TFD_NONBLOCK
|TFD_CLOEXEC
)) < 0)
204 w
->type
= WATCH_DBUS_TIMEOUT
;
205 w
->data
.bus_timeout
= timeout
;
207 if (bus_timeout_arm(m
, w
) < 0)
214 if (epoll_ctl(m
->epoll_fd
, EPOLL_CTL_ADD
, w
->fd
, &ev
) < 0)
217 dbus_timeout_set_data(timeout
, w
, NULL
);
223 close_nointr_nofail(w
->fd
);
229 static void bus_remove_timeout(DBusTimeout
*timeout
, void *data
) {
236 if (!(w
= dbus_timeout_get_data(timeout
)))
239 assert(w
->type
== WATCH_DBUS_TIMEOUT
);
240 assert_se(epoll_ctl(m
->epoll_fd
, EPOLL_CTL_DEL
, w
->fd
, NULL
) >= 0);
241 close_nointr_nofail(w
->fd
);
245 static void bus_toggle_timeout(DBusTimeout
*timeout
, void *data
) {
253 assert_se(w
= dbus_timeout_get_data(timeout
));
254 assert(w
->type
== WATCH_DBUS_TIMEOUT
);
256 if ((r
= bus_timeout_arm(m
, w
)) < 0)
257 log_error("Failed to rearm timer: %s", strerror(-r
));
260 void bus_dispatch(Manager
*m
) {
263 if (dbus_connection_dispatch(m
->bus
) == DBUS_DISPATCH_COMPLETE
)
264 m
->request_bus_dispatch
= false;
267 static int request_name(Manager
*m
) {
268 DBusMessage
*message
;
269 const char *name
= "org.freedesktop.systemd1";
272 if (!(message
= dbus_message_new_method_call(
279 if (!dbus_message_append_args(
281 DBUS_TYPE_STRING
, &name
,
282 DBUS_TYPE_UINT32
, &flags
,
283 DBUS_TYPE_INVALID
)) {
284 dbus_message_unref(message
);
288 if (!dbus_connection_send(m
->bus
, message
, NULL
)) {
289 dbus_message_unref(message
);
293 /* We simple ask for the name and don't wait for it. Sooner or
294 * later we'll have it, and we wouldn't know what to do on
297 dbus_message_unref(message
);
302 int bus_init(Manager
*m
) {
312 dbus_connection_set_change_sigpipe(FALSE
);
314 dbus_error_init(&error
);
315 if (!(m
->bus
= dbus_bus_get_private(m
->is_init
? DBUS_BUS_SYSTEM
: DBUS_BUS_SESSION
, &error
))) {
316 log_error("Failed to get D-Bus connection: %s", error
.message
);
317 dbus_error_free(&error
);
318 return -ECONNREFUSED
;
321 dbus_connection_set_exit_on_disconnect(m
->bus
, FALSE
);
322 dbus_connection_set_dispatch_status_function(m
->bus
, bus_dispatch_status
, m
, NULL
);
323 if (!dbus_connection_set_watch_functions(m
->bus
, bus_add_watch
, bus_remove_watch
, bus_toggle_watch
, m
, NULL
) ||
324 !dbus_connection_set_timeout_functions(m
->bus
, bus_add_timeout
, bus_remove_timeout
, bus_toggle_timeout
, m
, NULL
) ||
325 !dbus_connection_register_object_path(m
->bus
, "/org/freedesktop/systemd1", &bus_manager_vtable
, m
) ||
326 !dbus_connection_register_fallback(m
->bus
, "/org/freedesktop/systemd1/unit", &bus_unit_vtable
, m
) ||
327 !dbus_connection_register_fallback(m
->bus
, "/org/freedesktop/systemd1/job", &bus_job_vtable
, m
)) {
332 if ((r
= request_name(m
)) < 0) {
337 log_debug("Successfully connected to D-Bus bus %s as %s",
338 strnull((id
= dbus_connection_get_server_id(m
->bus
))),
339 strnull(dbus_bus_get_unique_name(m
->bus
)));
342 m
->request_bus_dispatch
= true;
347 void bus_done(Manager
*m
) {
351 dbus_connection_close(m
->bus
);
352 dbus_connection_unref(m
->bus
);
357 DBusHandlerResult
bus_default_message_handler(Manager
*m
, DBusMessage
*message
, const char*introspection
, const BusProperty
*properties
) {
359 DBusMessage
*reply
= NULL
;
365 dbus_error_init(&error
);
367 if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Introspectable", "Introspect") && introspection
) {
369 if (!(reply
= dbus_message_new_method_return(message
)))
372 if (!dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection
, DBUS_TYPE_INVALID
))
375 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Properties", "Get") && properties
) {
376 const char *interface
, *property
;
377 const BusProperty
*p
;
379 if (!dbus_message_get_args(
382 DBUS_TYPE_STRING
, &interface
,
383 DBUS_TYPE_STRING
, &property
,
385 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
387 for (p
= properties
; p
->property
; p
++)
388 if (streq(p
->interface
, interface
) && streq(p
->property
, property
))
392 DBusMessageIter iter
, sub
;
394 if (!(reply
= dbus_message_new_method_return(message
)))
397 dbus_message_iter_init_append(reply
, &iter
);
399 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_VARIANT
, p
->signature
, &sub
))
402 if ((r
= p
->append(m
, &sub
, property
, p
->data
)) < 0) {
407 dbus_message_unref(reply
);
408 return bus_send_error_reply(m
, message
, NULL
, r
);
411 if (!dbus_message_iter_close_container(&iter
, &sub
))
414 } else if (dbus_message_is_method_call(message
, "org.freedesktop.DBus.Properties", "GetAll") && properties
) {
415 const char *interface
;
416 const BusProperty
*p
;
417 DBusMessageIter iter
, sub
, sub2
, sub3
;
420 if (!dbus_message_get_args(
423 DBUS_TYPE_STRING
, &interface
,
425 return bus_send_error_reply(m
, message
, &error
, -EINVAL
);
427 if (!(reply
= dbus_message_new_method_return(message
)))
430 dbus_message_iter_init_append(reply
, &iter
);
432 if (!dbus_message_iter_open_container(&iter
, DBUS_TYPE_ARRAY
, "{sv}", &sub
))
435 for (p
= properties
; p
->property
; p
++) {
436 if (!streq(p
->interface
, interface
))
439 if (!dbus_message_iter_open_container(&sub
, DBUS_TYPE_DICT_ENTRY
, "sv" , &sub2
) ||
440 !dbus_message_iter_append_basic(&sub2
, DBUS_TYPE_STRING
, &p
->property
) ||
441 !dbus_message_iter_open_container(&sub2
, DBUS_TYPE_VARIANT
, p
->signature
, &sub3
))
444 if ((r
= p
->append(m
, &sub3
, p
->property
, p
->data
)) < 0) {
449 dbus_message_unref(reply
);
450 return bus_send_error_reply(m
, message
, NULL
, r
);
453 if (!dbus_message_iter_close_container(&sub2
, &sub3
) ||
454 !dbus_message_iter_close_container(&sub
, &sub2
))
460 if (!dbus_message_iter_close_container(&iter
, &sub
))
465 if (!dbus_connection_send(m
->bus
, reply
, NULL
))
468 dbus_message_unref(reply
);
469 return DBUS_HANDLER_RESULT_HANDLED
;
472 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
476 dbus_message_unref(reply
);
478 dbus_error_free(&error
);
480 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
485 static const char *error_to_dbus(int error
) {
490 return DBUS_ERROR_INVALID_ARGS
;
493 return DBUS_ERROR_NO_MEMORY
;
497 return DBUS_ERROR_ACCESS_DENIED
;
500 return DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN
;
503 return DBUS_ERROR_FILE_NOT_FOUND
;
506 return DBUS_ERROR_FILE_EXISTS
;
509 return DBUS_ERROR_TIMEOUT
;
512 return DBUS_ERROR_IO_ERROR
;
517 return DBUS_ERROR_DISCONNECTED
;
520 return DBUS_ERROR_FAILED
;
523 DBusHandlerResult
bus_send_error_reply(Manager
*m
, DBusMessage
*message
, DBusError
*bus_error
, int error
) {
524 DBusMessage
*reply
= NULL
;
525 const char *name
, *text
;
527 if (bus_error
&& dbus_error_is_set(bus_error
)) {
528 name
= bus_error
->name
;
529 text
= bus_error
->message
;
531 name
= error_to_dbus(error
);
532 text
= strerror(-error
);
535 if (!(reply
= dbus_message_new_error(message
, name
, text
)))
538 if (!dbus_connection_send(m
->bus
, reply
, NULL
))
541 dbus_message_unref(reply
);
544 dbus_error_free(bus_error
);
546 return DBUS_HANDLER_RESULT_HANDLED
;
550 dbus_message_unref(reply
);
553 dbus_error_free(bus_error
);
555 return DBUS_HANDLER_RESULT_NEED_MEMORY
;
558 int bus_property_append_string(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
559 const char *t
= data
;
566 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_STRING
, &t
))
572 int bus_property_append_strv(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
580 if (!dbus_message_iter_open_container(i
, DBUS_TYPE_ARRAY
, "s", &sub
))
584 if (!dbus_message_iter_append_basic(&sub
, DBUS_TYPE_STRING
, t
))
587 if (!dbus_message_iter_close_container(i
, &sub
))
593 int bus_property_append_bool(Manager
*m
, DBusMessageIter
*i
, const char *property
, void *data
) {
604 if (!dbus_message_iter_append_basic(i
, DBUS_TYPE_BOOLEAN
, &db
))