]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
metrics/report: use _cleanup_, avoid goto
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 4 Feb 2026 16:57:24 +0000 (17:57 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 4 Feb 2026 16:57:24 +0000 (17:57 +0100)
src/report/report.c

index 591efa8da8ffeb319dd846eb8ac69cbaad7eb3c0..42819ae29b87e5648098ac46cd040d5c7501f2e9 100644 (file)
@@ -79,16 +79,13 @@ static int metrics_on_query_reply(
                         log_info("Varlink timed out");
                 else
                         log_error("Varlink error: %s", error_id);
-
-                goto finish;
+        } else {
+                /* Collect metrics for later sorting */
+                if (!GREEDY_REALLOC(context->metrics, context->n_metrics + 1))
+                        return log_oom();
+                context->metrics[context->n_metrics++] = sd_json_variant_ref(parameters);
         }
 
-        /* Collect metrics for later sorting */
-        if (!GREEDY_REALLOC(context->metrics, context->n_metrics + 1))
-                return log_oom();
-        context->metrics[context->n_metrics++] = sd_json_variant_ref(parameters);
-
-finish:
         if (!FLAGS_SET(flags, SD_VARLINK_REPLY_CONTINUES)) {
                 assert(context->n_open_connections > 0);
                 context->n_open_connections--;
@@ -156,9 +153,6 @@ static void context_done(Context *context) {
 static void metrics_output_sorted(Context *context) {
         assert(context);
 
-        if (context->n_metrics == 0)
-                return;
-
         typesafe_qsort(context->metrics, context->n_metrics, metric_compare);
 
         FOREACH_ARRAY(m, context->metrics, context->n_metrics)
@@ -170,16 +164,14 @@ static void metrics_output_sorted(Context *context) {
 }
 
 static int metrics_query(void) {
-        _cleanup_closedir_ DIR *d = NULL;
-        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
-        _cleanup_free_ char *metrics_path = NULL;
         int r;
 
+        _cleanup_free_ char *metrics_path = NULL;
         r = runtime_directory_generic(arg_runtime_scope, "systemd/report", &metrics_path);
         if (r < 0)
                 return log_error_errno(r, "Failed to determine metrics directory path: %m");
 
-        d = opendir(metrics_path);
+        _cleanup_closedir_ DIR *d = opendir(metrics_path);
         if (!d) {
                 if (errno == ENOENT)
                         return 0;
@@ -187,6 +179,7 @@ static int metrics_query(void) {
                 return log_error_errno(errno, "Failed to open directory %s: %m", metrics_path);
         }
 
+        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
         r = sd_event_default(&event);
         if (r < 0)
                 return log_error_errno(r, "Failed to get event loop: %m");
@@ -202,15 +195,14 @@ static int metrics_query(void) {
 
         CLEANUP_ARRAY(varlinks, n_varlinks, sd_varlink_unref_many);
 
-        Context context = {};
+        _cleanup_(context_done) Context context = {};
 
         FOREACH_DIRENT(de, d, return -errno) {
-                _cleanup_free_ char *p = NULL;
 
                 if (!IN_SET(de->d_type, DT_SOCK, DT_UNKNOWN))
                         continue;
 
-                p = path_join(metrics_path, de->d_name);
+                _cleanup_free_ char *p = path_join(metrics_path, de->d_name);
                 if (!p)
                         return log_oom();
 
@@ -227,15 +219,11 @@ static int metrics_query(void) {
         }
 
         r = sd_event_loop(event);
-        if (r < 0) {
-                context_done(&context);
+        if (r < 0)
                 return log_error_errno(r, "Failed to run event loop: %m");
-        }
 
         metrics_output_sorted(&context);
 
-        context_done(&context);
-
         return 0;
 }
 
@@ -318,11 +306,7 @@ static int run(int argc, char *argv[]) {
         if (r <= 0)
                 return r;
 
-        r = metrics_query();
-        if (r < 0)
-                return r;
-
-        return 0;
+        return metrics_query();
 }
 
 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);