metrics/report: use _cleanup_, avoid goto
This commit is contained in:
+12
-28
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user