From 7f1027325b7a928d006810975f9d1e8bb0ccf502 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 4 Feb 2025 11:31:49 +0000 Subject: [PATCH] daemon: Add a common function to send messages into the control socket Signed-off-by: Michael Tremer --- src/pakfire/daemon.c | 48 ++++++++++++++++++++++++++++++++++++++------ src/pakfire/daemon.h | 5 +++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/pakfire/daemon.c b/src/pakfire/daemon.c index 3b55e4ec..ea2e9c27 100644 --- a/src/pakfire/daemon.c +++ b/src/pakfire/daemon.c @@ -23,6 +23,8 @@ #include #include +#include + #include #include @@ -148,7 +150,6 @@ static int pakfire_daemon_submit_stats(sd_event_source* s, uint64_t usec, void* struct pakfire_meminfo meminfo = {}; struct json_object* stats = NULL; struct json_object* data = NULL; - size_t length = 0; int r; // If we have any jobs running, we will submit our stats @@ -341,12 +342,20 @@ static int pakfire_daemon_submit_stats(sd_event_source* s, uint64_t usec, void* // Dereference data so it won't be double-freed later data = NULL; - // Serialize to string - const char* buffer = json_object_to_json_string_length(stats, - JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY, &length); - // Send the message - r = pakfire_xfer_send_message(daemon->control, buffer, length); + r = pakfire_daemon_send_message(daemon, stats); + if (r < 0) { + switch (-r) { + // Ignore if we are not connected + case ENOTCONN: + r = 0; + break; + + // Raise anything else + default: + goto ERROR; + } + } ERROR: if (stats) @@ -1273,4 +1282,31 @@ int pakfire_daemon_job_finished(struct pakfire_daemon* daemon, struct pakfire_jo return 0; } +int pakfire_daemon_send_message(struct pakfire_daemon* self, struct json_object* message) { + const char* m = NULL; + size_t length = 0; + int r; + + // Return an error if we are not connected + if (!self->control) + return -ENOTCONN; + + // Serialize to string + m = json_object_to_json_string_length(message, + JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY, &length); + if (!m) { + ERROR(self->ctx, "Failed to serialize message: %m\n"); + return -errno; + } + + // Send the message + r = pakfire_xfer_send_message(self->control, m, length); + if (r < 0) { + ERROR(self->ctx, "Failed to send message: %s\n", strerror(-r)); + return r; + } + + return 0; +} + #endif /* CURL_HAS_WEBSOCKETS */ diff --git a/src/pakfire/daemon.h b/src/pakfire/daemon.h index 2bbffc57..f345d3c7 100644 --- a/src/pakfire/daemon.h +++ b/src/pakfire/daemon.h @@ -29,6 +29,8 @@ struct pakfire_daemon; #include #include +#include + int pakfire_daemon_create(struct pakfire_daemon** daemon, struct pakfire_ctx* ctx); struct pakfire_daemon* pakfire_daemon_ref(struct pakfire_daemon* daemon); @@ -44,5 +46,8 @@ int pakfire_daemon_main(struct pakfire_daemon* daemon); int pakfire_daemon_job_finished(struct pakfire_daemon* daemon, struct pakfire_job* job); +// Send message +int pakfire_daemon_send_message(struct pakfire_daemon* self, struct json_object* message); + #endif /* CURL_HAS_WEBSOCKETS */ #endif /* PAKFIRE_DAEMON_H */ -- 2.39.5