From: Michael Tremer Date: Wed, 8 Oct 2025 09:57:46 +0000 (+0000) Subject: file: Add a function to read a single integer X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dc8471887c3edaa84ec9fd861b1ba60ca1f4f405;p=telemetry.git file: Add a function to read a single integer Signed-off-by: Michael Tremer --- diff --git a/src/daemon/file.c b/src/daemon/file.c index ff69356..c012ddb 100644 --- a/src/daemon/file.c +++ b/src/daemon/file.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include #include @@ -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; +} diff --git a/src/daemon/file.h b/src/daemon/file.h index 4b50431..284237c 100644 --- a/src/daemon/file.h +++ b/src/daemon/file.h @@ -21,6 +21,8 @@ #ifndef COLLECTY_FILE_H #define COLLECTY_FILE_H +#include + 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 */ diff --git a/src/daemon/sources/conntrack.c b/src/daemon/sources/conntrack.c index 033b46b..9818fef 100644 --- a/src/daemon/sources/conntrack.c +++ b/src/daemon/sources/conntrack.c @@ -21,8 +21,8 @@ #include #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)); diff --git a/src/daemon/util.c b/src/daemon/util.c index fcdda7b..48224b6 100644 --- a/src/daemon/util.c +++ b/src/daemon/util.c @@ -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; diff --git a/src/daemon/util.h b/src/daemon/util.h index e9ffa91..aea5718 100644 --- a/src/daemon/util.h +++ b/src/daemon/util.h @@ -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)));