--- /dev/null
+/*#############################################################################
+# #
+# collecty - A system statistics collection daemon for IPFire #
+# Copyright (C) 2025 IPFire Development Team #
+# #
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <http://www.gnu.org/licenses/>. #
+# #
+#############################################################################*/
+
+#include <errno.h>
+#include <stdlib.h>
+
+#include <systemd/sd-bus.h>
+#include <systemd/sd-event.h>
+
+#include "bus.h"
+#include "ctx.h"
+#include "daemon.h"
+#include "util.h"
+
+#define DEFAULT_SYSTEM_BUS_ADDRESS "unix:path=/run/dbus/system_bus_socket"
+
+static int collecty_bus_on_connect(sd_bus_message* m, void* data, sd_bus_error* error) {
+ collecty_ctx* ctx = data;
+
+ // Log action
+ DEBUG(ctx, "Connected to D-Bus\n");
+
+ return 0;
+}
+
+static int collecty_bus_register_implementation(collecty_ctx* ctx,
+ sd_bus* bus, const struct collecty_bus_implementation* impl, void* data) {
+ int r;
+
+ DEBUG(ctx, "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(ctx, "Could not register bus path %s with interface %s: %m\n",
+ impl->path, impl->interface);
+ return r;
+ }
+ }
+
+ // Register fallback vtables
+ for (const struct collecty_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(ctx, "Could not register bus path %s with interface %s: %m\n",
+ impl->path, impl->interface);
+ return r;
+ }
+ }
+
+ // 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(ctx, "Could not add the node enumerator for %s: %m\n", impl->path);
+ return r;
+ }
+ }
+
+ // Register any child implementations
+ for (int i = 0; impl->children && impl->children[i]; i++) {
+ r = collecty_bus_register_implementation(ctx, bus, impl->children[i], data);
+ if (r)
+ return r;
+ }
+
+ return 0;
+}
+
+int collecty_bus_connect(collecty_ctx* ctx, sd_bus** bus, sd_event* loop, collecty_daemon* daemon) {
+ const char* address = NULL;
+ sd_bus* b = NULL;
+ int r;
+
+ // Create a bus object
+ r = sd_bus_new(&b);
+ if (r < 0) {
+ ERROR(ctx, "Could not allocate a bus object: %s\n", strerror(-r));
+ goto ERROR;
+ }
+
+ // Set description
+ r = sd_bus_set_description(b, "collectyd");
+ if (r < 0) {
+ ERROR(ctx, "Could not set bus description: %s\n", strerror(-r));
+ goto ERROR;
+ }
+
+ // Fetch the bus address
+ address = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
+ if (!address)
+ address = DEFAULT_SYSTEM_BUS_ADDRESS;
+
+ // Set bus address
+ r = sd_bus_set_address(b, address);
+ if (r < 0) {
+ ERROR(ctx, "Could not set bus address: %s\n", strerror(-r));
+ goto ERROR;
+ }
+
+ // Set bus client
+ r = sd_bus_set_bus_client(b, 1);
+ if (r < 0) {
+ ERROR(ctx, "Could not set bus client: %s\n", strerror(-r));
+ goto ERROR;
+ }
+
+ // Request some credentials for all messages
+ r = sd_bus_negotiate_creds(b, 1,
+ SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS);
+ if (r < 0) {
+ ERROR(ctx, "Could not negotiate creds: %s\n", strerror(-r));
+ goto ERROR;
+ }
+
+ // Automatically bind when the socket is available
+ r = sd_bus_set_watch_bind(b, 1);
+ if (r < 0) {
+ ERROR(ctx, "Could not watch socket: %s\n", strerror(-r));
+ goto ERROR;
+ }
+
+ // Emit a connected signal when we are connected
+ r = sd_bus_set_connected_signal(b, 1);
+ if (r < 0) {
+ ERROR(ctx, "Could not enable sending a connect signal: %s\n", strerror(-r));
+ goto ERROR;
+ }
+
+ // Connect to the bus
+ r = sd_bus_start(b);
+ if (r < 0) {
+ ERROR(ctx, "Could not connect to bus: %s\n", strerror(-r));
+ goto ERROR;
+ }
+
+ // Register the implementation
+ r = collecty_bus_register_implementation(ctx, b, &daemon_bus_impl, daemon);
+ if (r < 0)
+ goto ERROR;
+
+ // Request interface name
+ r = sd_bus_request_name_async(b, NULL, "org.ipfire.collecty1", 0, NULL, NULL);
+ if (r < 0) {
+ ERROR(ctx, "Could not request bus name: %s\n", strerror(-r));
+ goto ERROR;
+ }
+
+ // Attach the event loop
+ r = sd_bus_attach_event(b, loop, 0);
+ if (r < 0) {
+ ERROR(ctx, "Could not attach bus to event loop: %s\n", strerror(-r));
+ goto ERROR;
+ }
+
+ // Request receiving a connect signal
+ r = sd_bus_match_signal_async(b, NULL, "org.freedesktop.DBus.Local", NULL,
+ "org.freedesktop.DBus.Local", "Connected", collecty_bus_on_connect, NULL, ctx);
+ if (r < 0) {
+ ERROR(ctx, "Could not request match on Connected signal: %s\n", strerror(-r));
+ goto ERROR;
+ }
+
+ // Return reference
+ *bus = b;
+ return 0;
+
+ERROR:
+ if (b)
+ sd_bus_unref(b);
+
+ return r;
+}
--- /dev/null
+/*#############################################################################
+# #
+# collecty - A system statistics collection daemon for IPFire #
+# Copyright (C) 2025 IPFire Development Team #
+# #
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <http://www.gnu.org/licenses/>. #
+# #
+#############################################################################*/
+
+#ifndef COLLECTY_BUS_H
+#define COLLECTY_BUS_H
+
+#include <systemd/sd-bus.h>
+#include <systemd/sd-event.h>
+
+struct collecty_bus_vtable_pair {
+ const sd_bus_vtable* vtable;
+ sd_bus_object_find_t object_find;
+};
+
+typedef struct collecty_bus_implementation {
+ const char* path;
+ const char* interface;
+ const sd_bus_vtable** vtables;
+ const struct collecty_bus_vtable_pair* fallback_vtables;
+ sd_bus_node_enumerator_t node_enumerator;
+ const struct collecty_bus_implementation** children;
+} collecty_bus_implementation;
+
+#include "ctx.h"
+#include "daemon.h"
+
+#define BUS_FALLBACK_VTABLES(...) ((const struct collecty_bus_vtable_pair[]) { __VA_ARGS__, {} })
+#define BUS_IMPLEMENTATIONS(...) ((const collecty_bus_implementation* []) { __VA_ARGS__, NULL })
+#define BUS_VTABLES(...) ((const sd_bus_vtable* []){ __VA_ARGS__, NULL })
+
+int collecty_bus_connect(collecty_ctx* ctx,
+ sd_bus** bus, sd_event* loop, collecty_daemon* daemon);
+
+#endif /* COLLECTY_BUS_H */