]> git.ipfire.org Git - telemetry.git/commitdiff
file: Add a function to read a single integer
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 8 Oct 2025 09:57:46 +0000 (09:57 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 8 Oct 2025 09:57:46 +0000 (09:57 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/daemon/file.c
src/daemon/file.h
src/daemon/sources/conntrack.c
src/daemon/util.c
src/daemon/util.h

index ff69356f175087b07df93a871a42a3497888b4d7..c012ddb9f77d0cc79ed510d5140b256c464cd7e8 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -103,6 +104,23 @@ collecty_file* collecty_file_unref(collecty_file* self) {
        return NULL;
 }
 
+int collecty_file_read_uint64(collecty_file* self, uint64_t* number) {
+       int r;
+
+       // Read the number
+       r = fscanf(self->f, "%lu", number);
+
+       // Handle errors
+       if (r < 0)
+               return -errno;
+
+       // Exactly one field must have been parsed
+       if (r < 1)
+               return -EBADMSG;
+
+       return 0;
+}
+
 int collecty_file_walk(collecty_file* self,
                collecty_file_walk_callback callback, void* data) {
        unsigned long lineno = 0;
@@ -146,3 +164,24 @@ int collecty_file_walk(collecty_file* self,
 
        return r;
 }
+
+int collecty_read_uint64(collecty_ctx* ctx, const char* path, uint64_t* number) {
+       collecty_file* file = NULL;
+       int r;
+
+       // Open the file
+       r = collecty_file_open_path(&file, ctx, path);
+       if (r < 0)
+               goto ERROR;
+
+       // Read the number
+       r = collecty_file_read_uint64(file, number);
+       if (r < 0)
+               goto ERROR;
+
+ERROR:
+       if (file)
+               collecty_file_unref(file);
+
+       return r;
+}
index 4b50431d77ff6d95483e53c0a73bb1a19d63a397..284237cfff0fff5d021aaf5a7bfaedc645b0dc58 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef COLLECTY_FILE_H
 #define COLLECTY_FILE_H
 
+#include <stdint.h>
+
 typedef struct collecty_file collecty_file;
 
 #include "ctx.h"
@@ -30,10 +32,16 @@ int collecty_file_open_path(collecty_file** file, collecty_ctx* ctx, const char*
 collecty_file* collecty_file_ref(collecty_file* self);
 collecty_file* collecty_file_unref(collecty_file* self);
 
+int collecty_file_read_uint64(collecty_file* self, uint64_t* number);
+
 typedef int (*collecty_file_walk_callback)
        (collecty_ctx* ctx, unsigned long lineno, char* line, size_t length, void* data);
 
 int collecty_file_walk(collecty_file* self,
        collecty_file_walk_callback callback, void* data);
 
+// Shorthands
+
+int collecty_read_uint64(collecty_ctx* ctx, const char* path, uint64_t* number);
+
 #endif /* COLLECTY_FILE_H */
index 033b46b0ee80db4e3935c19970da6498b39cf90d..9818fefa73ac96d2b4c3c558e2c43532d9dbab8b 100644 (file)
@@ -21,8 +21,8 @@
 #include <string.h>
 
 #include "../ctx.h"
+#include "../file.h"
 #include "../source.h"
-#include "../util.h"
 #include "conntrack.h"
 
 static int conntrack_heartbeat(collecty_ctx* ctx, collecty_source* source) {
@@ -31,7 +31,7 @@ static int conntrack_heartbeat(collecty_ctx* ctx, collecty_source* source) {
        int r;
 
        // Read the total number of connections
-       r = collecty_file_read_uint64("/proc/sys/net/netfilter/nf_conntrack_count", &count);
+       r = collecty_read_uint64(ctx, "/proc/sys/net/netfilter/nf_conntrack_count", &count);
        if (r < 0) {
                ERROR(ctx, "Failed to read %s: %s\n",
                                "/proc/sys/net/netfilter/nf_conntrack_count", strerror(-r));
@@ -39,7 +39,7 @@ static int conntrack_heartbeat(collecty_ctx* ctx, collecty_source* source) {
        }
 
        // Read the maximum number of connections
-       r = collecty_file_read_uint64("/proc/sys/net/netfilter/nf_conntrack_max", &max);
+       r = collecty_read_uint64(ctx, "/proc/sys/net/netfilter/nf_conntrack_max", &max);
        if (r < 0) {
                ERROR(ctx, "Failed to read %s: %s\n",
                                "/proc/sys/net/netfilter/nf_conntrack_max", strerror(-r));
index fcdda7b9ff6a7c477a89997ebc038a50d03d296d..48224b69e5ea3305f4d92ed9ec8fe6e5f2519a86 100644 (file)
@@ -35,34 +35,6 @@ int __collecty_format_number(char* buffer, size_t length, int number) {
        return __collecty_string_set(buffer, length, "U");
 }
 
-int collecty_file_read_uint64(const char* path, uint64_t* number) {
-       unsigned long n = 0;
-       FILE* f = NULL;
-       int r;
-
-       // Open the file
-       f = fopen(path, "r");
-       if (!f)
-               return -errno;
-
-       // Read the number
-       r = fscanf(f, "%lu", &n);
-       if (r != 1) {
-               r = -errno;
-               goto ERROR;
-       }
-
-       // Return the number
-       *number = n;
-       r = 0;
-
-ERROR:
-       if (f)
-               fclose(f);
-
-       return r;
-}
-
 // Helper function to set the title
 int collecty_format_title(char** title, const char* format, ...) {
        va_list args;
index e9ffa9134e32c5de602e88a7952e71ae10152403..aea5718dffea938dd27fe18c501975f5e0a7d79c 100644 (file)
@@ -46,8 +46,6 @@
 
 int __collecty_format_number(char* buffer, size_t length, int number);
 
-int collecty_file_read_uint64(const char* path, uint64_t* number);
-
 // Helper function to set the title
 int collecty_format_title(char** title, const char* format, ...)
        __attribute__((format(printf, 2, 3)));