]> git.ipfire.org Git - telemetry.git/commitdiff
daemon: Allow to configure the database path
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Jun 2026 14:48:01 +0000 (14:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Jun 2026 14:48:01 +0000 (14:48 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/daemon.c
src/daemon/daemon.h
src/daemon/main.c

index 21eaa2f275cb9031b3383f831d97f525a5d6fcae..283608fcacf3cb07515590fb9205e2d0a2915118 100644 (file)
@@ -21,6 +21,7 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
 
 #include <libudev.h>
 #include <systemd/sd-daemon.h>
 #include "netlink.h"
 #include "queue.h"
 #include "sensors.h"
+#include "string.h"
 #include "source.h"
 #include "sources.h"
+#include "util.h"
 
 struct td_daemon {
        td_ctx* ctx;
@@ -76,6 +79,9 @@ struct td_daemon {
 
        // Netlink
        td_netlink* netlink;
+
+       // Path
+       char path[PATH_MAX];
 };
 
 static int td_daemon_init(sd_event_source* source, void* data) {
@@ -311,7 +317,7 @@ static void td_daemon_free(td_daemon* self) {
        free(self);
 }
 
-int td_daemon_create(td_daemon** daemon, td_ctx* ctx, char** enabled_sources) {
+int td_daemon_create(td_daemon** daemon, td_ctx* ctx, const char* path, char** enabled_sources) {
        td_daemon* self = NULL;
        int r;
 
@@ -326,6 +332,11 @@ int td_daemon_create(td_daemon** daemon, td_ctx* ctx, char** enabled_sources) {
        // Store a reference to the context
        self->ctx = td_ctx_ref(ctx);
 
+       // Set the path
+       r = td_daemon_set_path(self, path);
+       if (r < 0)
+               goto ERROR;
+
        // Store the enabled sources
        self->enabled_sources = enabled_sources;
 
@@ -378,6 +389,74 @@ td_daemon* td_daemon_unref(td_daemon* daemon) {
        return NULL;
 }
 
+const char* td_daemon_get_path(td_daemon* self) {
+       if (unlikely(!*self->path))
+               return NULL;
+
+       return self->path;
+}
+
+static int td_daemon_check_path(td_daemon* self) {
+       struct stat st;
+       int fd = -EBADF;
+       int r;
+
+       // Stat the directory
+       r = stat(self->path, &st);
+       if (r < 0) {
+               switch (errno) {
+                       case ENOENT:
+                               ERROR(self->ctx, "%s does not exist\n", self->path);
+                               r = -ENOENT;
+                               break;
+
+                       default:
+                               ERROR(self->ctx, "Failed to stat %s: %m\n", self->path);
+                               r = -errno;
+                               break;
+               }
+
+               goto ERROR;
+       }
+
+       // Check if this is a directory
+       if (!S_ISDIR(st.st_mode)) {
+               ERROR(self->ctx, "%s is not a directory\n", self->path);
+               r = -ENOTDIR;
+               goto ERROR;
+       }
+
+       // Check if we have permission to write
+       fd = open(self->path, O_WRONLY|O_TMPFILE, 0600);
+       if (fd < 0) {
+               ERROR(self->ctx, "Cannot write to %s: %m\n", self->path);
+               r = -errno;
+               goto ERROR;
+       }
+
+ERROR:
+       if (fd >= 0)
+               close(fd);
+
+       return r;
+}
+
+int td_daemon_set_path(td_daemon* self, const char* path) {
+       int r;
+
+       // Use a default path if nothing has been set
+       if (!path)
+               path = DATABASE_PATH;
+
+       // Store the path
+       r = td_string_set(self->path, path);
+       if (r < 0)
+               return r;
+
+       // Check if the path exists
+       return td_daemon_check_path(self);
+}
+
 sd_event* td_daemon_loop(td_daemon* self) {
        return sd_event_ref(self->loop);
 }
index 5d8006cecf6a8d913c8fad2001f5b3e613925b83..35857fbb4a8a88abbc1108be4ef801420c1776e9 100644 (file)
@@ -34,11 +34,15 @@ typedef struct td_daemon td_daemon;
 #include "source.h"
 #include "sources.h"
 
-int td_daemon_create(td_daemon** daemon, td_ctx* ctx, char** enabled_sources);
+int td_daemon_create(td_daemon** daemon, td_ctx* ctx,
+       const char* path, char** enabled_sources);
 
 td_daemon* td_daemon_ref(td_daemon* daemon);
 td_daemon* td_daemon_unref(td_daemon* daemon);
 
+const char* td_daemon_get_path(td_daemon* self);
+int td_daemon_set_path(td_daemon* self, const char* path);
+
 sd_event* td_daemon_loop(td_daemon* self);
 struct udev* td_daemon_get_udev(td_daemon* self);
 td_sources* td_daemon_get_sources(td_daemon* self);
index bd645cfb83a66ec0c4e009b686182df5cd0ee3e3..964d1b408283b0902997f2bb0d461b219e438b1b 100644 (file)
@@ -105,7 +105,7 @@ int main(int argc, char* argv[]) {
                goto ERROR;
 
        // Create a daemon
-       r = td_daemon_create(&daemon, ctx, sources);
+       r = td_daemon_create(&daemon, ctx, NULL, sources);
        if (r < 0) {
                ERROR(ctx, "Failed to initialize the daemon: %s\n", strerror(-r));
                goto ERROR;