]> git.ipfire.org Git - pakfire.git/commitdiff
daemon: Add a common function to send messages into the control socket
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 4 Feb 2025 11:31:49 +0000 (11:31 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 4 Feb 2025 11:31:49 +0000 (11:31 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/daemon.c
src/pakfire/daemon.h

index 3b55e4ecb828ac9b3a02d8f384b233305b76fac9..ea2e9c273880ebab88a03dc2f0c96ac7cfa0ec70 100644 (file)
@@ -23,6 +23,8 @@
 #include <errno.h>
 #include <stdlib.h>
 
+#include <json.h>
+
 #include <krb5/krb5.h>
 
 #include <systemd/sd-bus.h>
@@ -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 */
index 2bbffc57c89bca60d30658cf9a410df996968c2b..f345d3c77e573a5de893e0f3caa7aac4203c5469 100644 (file)
@@ -29,6 +29,8 @@ struct pakfire_daemon;
 #include <pakfire/ctx.h>
 #include <pakfire/job.h>
 
+#include <json.h>
+
 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 */