]> git.ipfire.org Git - pakfire.git/commitdiff
client: Move sending a crash report here
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 26 Jun 2025 13:12:22 +0000 (13:12 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 26 Jun 2025 13:12:22 +0000 (13:12 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/client.c
src/pakfire/client.h
src/pakfire/job.c

index 7a1bc52a3276881802ae78eb47d5e6178aa1eb20..0bc8847bd6db35c2c30fd187f4a2da08c0ceb928 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <systemd/sd-event.h>
 
+#include <pakfire/buffer.h>
 #include <pakfire/client.h>
 #include <pakfire/config.h>
 #include <pakfire/ctx.h>
@@ -1413,3 +1414,54 @@ ERROR:
 
        return r;
 }
+
+/*
+       This is called when a job has crashed.
+*/
+int pakfire_client_job_crashed(struct pakfire_client* self,
+               const char* job_id, int signo, struct pakfire_buffer* log) {
+       struct json_object* request = NULL;
+       struct pakfire_xfer* xfer = NULL;
+       int r;
+
+       // Create a new request object
+       r = pakfire_json_new_object(&request);
+       if (r < 0)
+               goto ERROR;
+
+       // Add signal number
+       r = pakfire_json_add_int64(request, "signo", signo);
+       if (r < 0)
+               goto ERROR;
+
+       // Optionally append the log
+       if (!pakfire_buffer_is_empty(log)) {
+               r = pakfire_json_add_bytes(request, "log", log->data, log->length);
+               if (r < 0)
+                       goto ERROR;
+       }
+
+       // Create a new transfer
+       r = pakfire_client_xfer_create(&xfer, self, "/api/v1/jobs/%s/crashed", job_id);
+       if (r < 0)
+               goto ERROR;
+
+       // Enable authentication
+       r = pakfire_client_xfer_auth(self, xfer);
+       if (r < 0)
+               goto ERROR;
+
+       // Set the request body
+       r = pakfire_xfer_set_json_payload(xfer, request);
+       if (r < 0)
+               goto ERROR;
+
+       // Enqueue the transfer
+       r = pakfire_httpclient_enqueue(self->httpclient, xfer);
+
+ERROR:
+       if (xfer)
+               pakfire_xfer_unref(xfer);
+
+       return r;
+}
index 2823a52872d9ccd8d433ce094bd87e95ae0ae26d..2ebd4cdcd9f85ae289f25e5f1172557e9e088b7c 100644 (file)
@@ -25,6 +25,7 @@ struct pakfire_client;
 
 #include <json.h>
 
+#include <pakfire/buffer.h>
 #include <pakfire/builder.h>
 #include <pakfire/ctx.h>
 #include <pakfire/xfer.h>
@@ -109,4 +110,9 @@ int pakfire_client_create_repo(struct pakfire_client* client,
 int pakfire_client_delete_repo(struct pakfire_client* client,
        const char* distro, const char* name);
 
+// Jobs
+
+int pakfire_client_job_crashed(struct pakfire_client* self,
+       const char* job_id, int signo, struct pakfire_buffer* log);
+
 #endif /* PAKFIRE_CLIENT_H */
index d9b4edaae54aac9dc875e3e9eff9347f9f4a54dd..9f4e541d371dc8fc7b6def0f14fa6f00570254dc 100644 (file)
@@ -375,56 +375,22 @@ ERROR:
        return r;
 }
 
-static int pakfire_job_crashed(struct pakfire_job* job, const siginfo_t* si) {
-       struct pakfire_xfer* xfer = NULL;
+static int pakfire_job_crashed(struct pakfire_job* self, const siginfo_t* si) {
+       struct pakfire_buffer log = {};
        int r;
 
-       struct pakfire_job_log {
-               char* data;
-               size_t length;
-       } log = {};
-
-       DEBUG(job->ctx, "Sending crash report...\n");
+       DEBUG(self->ctx, "Sending crash report...\n");
 
        // Dump the log
-       r = pakfire_log_buffer_dump(job->log.buffer, &log.data, &log.length);
-       if (r < 0)
-               goto ERROR;
-
-       // Create a new transfer
-       r = pakfire_job_xfer_create(&xfer, job, "/api/v1/jobs/%s/crashed", job->id);
-       if (r < 0)
-               goto ERROR;
-
-       // Enable authentication
-       r = pakfire_xfer_auth(xfer);
-       if (r)
-               goto ERROR;
-
-       // Add the exit code
-       r = pakfire_xfer_add_param(xfer, "signo", "%d", si->si_status);
+       r = pakfire_log_buffer_dump(self->log.buffer, &log.data, &log.length);
        if (r < 0)
                goto ERROR;
 
-       // If we have a log, let's send the log, too
-       // XXX THIS IS NOT QUITE IDEAL BECAUSE THE LOG COULD CONTAIN NULL-CHARACTERS
-       // WE SHOULD IMPLEMENT THIS WITH CURL'S MIME CALLBACK FUNCTIONS
-       if (log.data) {
-               r = pakfire_xfer_add_param(xfer, "log", "%.*s", (int)log.length, log.data);
-               if (r < 0)
-                       goto ERROR;
-       }
-
-       // Send the request
-       r = pakfire_xfer_run_api_request(xfer, NULL, NULL);
-       if (r < 0)
-               goto ERROR;
+       // Send the report
+       r = pakfire_client_job_crashed(self->client, self->id, si->si_status, &log);
 
 ERROR:
-       if (xfer)
-               pakfire_xfer_unref(xfer);
-       if (log.data)
-               free(log.data);
+       pakfire_buffer_free(&log);
 
        return r;
 }