#############################################################################*/
#include <stdlib.h>
-#include <string.h>
#include <systemd/sd-bus.h>
#include <systemd/sd-event.h>
#include "bus.h"
#include "daemon.h"
+#include "daemon-bus.h"
#include "logging.h"
-static int nw_bus_daemon_reload(sd_bus_message* m, void* data, sd_bus_error* error) {
- struct nw_daemon* daemon = (struct nw_daemon*)data;
-
- // Reload the daemon
- nw_daemon_reload(daemon);
-
- // Respond with an empty message
- return sd_bus_reply_method_return(m, NULL);
-}
-
static int nw_bus_on_connect(sd_bus_message* m, void* data, sd_bus_error* error) {
- struct nw_daemon* daemon = (struct nw_daemon*)data;
+ nw_daemon* daemon = (nw_daemon*)data;
DEBUG("Connected to D-Bus\n");
return 0;
}
-static const sd_bus_vtable daemon_vtable[] = {
- SD_BUS_VTABLE_START(0),
- SD_BUS_METHOD("Reload", SD_BUS_NO_ARGS, SD_BUS_NO_RESULT,
- nw_bus_daemon_reload, SD_BUS_VTABLE_UNPRIVILEGED),
- SD_BUS_VTABLE_END,
-};
-
-int nw_bus_connect(sd_bus* bus, sd_event* loop) {
+int nw_bus_connect(sd_bus* bus, sd_event* loop, nw_daemon* daemon) {
int r;
// Create a bus object
return 1;
}
- // Register any functions
- r = sd_bus_add_object_vtable(bus, NULL,
- NETWORKD_BUS_OBJECT_PATH, NETWORKD_BUS_INTERFACE_NAME, daemon_vtable, NULL);
- if (r < 0) {
- ERROR("Could not add object vtable: %s\n", strerror(-r));
- return 1;
- }
+ // Register the implementation
+ r = nw_bus_register_implementation(bus, &daemon_bus_impl, daemon);
+ if (r)
+ return r;
// Request interface name
- r = sd_bus_request_name_async(bus, NULL, NETWORKD_BUS_INTERFACE_NAME, 0, NULL, NULL);
+ r = sd_bus_request_name_async(bus, NULL, "org.ipfire.network1", 0, NULL, NULL);
if (r < 0) {
ERROR("Could not request bus name: %s\n", strerror(-r));
return 1;
return 0;
}
+
+int nw_bus_register_implementation(sd_bus* bus,
+ const struct nw_bus_implementation* impl, void* data) {
+ int r;
+
+ DEBUG("Registering bus object implementation for path=%s iface=%s\n",
+ impl->path, impl->interface);
+
+ // Register vtables
+ for (const sd_bus_vtable** vtable = impl->vtables; vtable && *vtable; vtable++) {
+ r = sd_bus_add_object_vtable(bus, NULL, impl->path, impl->interface, *vtable, data);
+ if (r < 0) {
+ ERROR("Could not register bus path %s with interface %s: %m\n",
+ impl->path, impl->interface);
+ return 1;
+ }
+ }
+
+ // Register fallback vtables
+ for (const struct nw_bus_vtable_pair* p = impl->fallback_vtables; p && p->vtable; p++) {
+ r = sd_bus_add_fallback_vtable(bus, NULL, impl->path, impl->interface,
+ p->vtable, p->object_find, data);
+ if (r < 0) {
+ ERROR("Could not register bus path %s with interface %s: %m\n",
+ impl->path, impl->interface);
+ return 1;
+ }
+ }
+
+ // Register the node enumerator
+ if (impl->node_enumerator) {
+ r = sd_bus_add_node_enumerator(bus, NULL, impl->path, impl->node_enumerator, data);
+ if (r < 0) {
+ ERROR("Could not add the node enumerator for %s: %m\n", impl->path);
+ return 1;
+ }
+ }
+
+ // Register any child implementations
+ for (int i = 0; impl->children && impl->children[i]; i++) {
+ r = nw_bus_register_implementation(bus, impl->children[i], data);
+ if (r)
+ return r;
+ }
+
+ return 0;
+}