From 4f006a660c57a61950e59172c511fef32ee1da63 Mon Sep 17 00:00:00 2001 From: Grimmauld Date: Mon, 9 Dec 2024 10:57:58 +0100 Subject: [PATCH 1/2] aa-status: fix json generation - previously, aa-status --json --show profiles would return non-standard json - adding the --pretty flag would crash completely - closes #470 Things done: - removed trailing ", " in json generation - generate json seperator (", ") for each new json field (profiles/processes) after the header if json is enabled Tested on NixOS and apparmor 4.0.3 base, but should work on any version the patch applies on. --- binutils/aa_status.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/binutils/aa_status.c b/binutils/aa_status.c index 5c05eede2..0943943a7 100644 --- a/binutils/aa_status.c +++ b/binutils/aa_status.c @@ -541,7 +541,12 @@ static int compare_processes_by_executable(const void *a, const void *b) { static void json_header(FILE *outf) { - fprintf(outf, "{\"version\": \"%s\", ", aa_status_json_version); + fprintf(outf, "{\"version\": \"%s\"", aa_status_json_version); +} + +static void json_seperator(FILE *outf) +{ + fprintf(outf, ", "); } static void json_footer(FILE *outf) @@ -609,7 +614,7 @@ static int detailed_profiles(FILE *outf, filters_t *filters, bool json, free_profiles(filtered, nfiltered); } if (json) - fprintf(outf, "}, "); + fprintf(outf, "}"); return AA_EXIT_ENABLED; } @@ -702,7 +707,7 @@ static int detailed_processes(FILE *outf, filters_t *filters, bool json, fprintf(outf, "]"); } - fprintf(outf, "}\n"); + fprintf(outf, "}"); } exit: @@ -1030,6 +1035,8 @@ int main(int argc, char **argv) if (opt_json) json_header(outf); if (opt_show & SHOW_PROFILES) { + if (opt_json) + json_seperator(outf); if (opt_count) { ret = simple_filtered_count(outf, &filters, profiles, nprofiles); @@ -1042,6 +1049,9 @@ int main(int argc, char **argv) } if (opt_show & SHOW_PROCESSES) { + if (opt_json) + json_seperator(outf); + struct process *processes = NULL; size_t nprocesses = 0; From 9967ba98734e5d498b83421ed8be9ea7ca620976 Mon Sep 17 00:00:00 2001 From: Grimmauld Date: Mon, 9 Dec 2024 23:58:04 +0100 Subject: [PATCH 2/2] aa-status: fix json output with --count flag --- binutils/aa_status.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/binutils/aa_status.c b/binutils/aa_status.c index 0943943a7..ee0bb9086 100644 --- a/binutils/aa_status.c +++ b/binutils/aa_status.c @@ -490,7 +490,7 @@ static int filter_processes(struct process *processes, * * Return: 0 on success, else shell error code */ -static int simple_filtered_count(FILE *outf, filters_t *filters, +static int simple_filtered_count(FILE *outf, filters_t *filters, bool json, struct profile *profiles, size_t nprofiles) { struct profile *filtered = NULL; @@ -499,7 +499,13 @@ static int simple_filtered_count(FILE *outf, filters_t *filters, ret = filter_profiles(profiles, nprofiles, filters, &filtered, &nfiltered); - fprintf(outf, "%zd\n", nfiltered); + + if (!json) { + fprintf(outf, "%zd\n", nfiltered); + } else { + fprintf(outf, "\"profile_count\": %zd", nfiltered); + } + free_profiles(filtered, nfiltered); return ret; @@ -514,7 +520,7 @@ static int simple_filtered_count(FILE *outf, filters_t *filters, * * Return: 0 on success, else shell error code */ -static int simple_filtered_process_count(FILE *outf, filters_t *filters, +static int simple_filtered_process_count(FILE *outf, filters_t *filters, bool json, struct process *processes, size_t nprocesses) { struct process *filtered = NULL; size_t nfiltered; @@ -522,7 +528,12 @@ static int simple_filtered_process_count(FILE *outf, filters_t *filters, ret = filter_processes(processes, nprocesses, filters, &filtered, &nfiltered); - fprintf(outf, "%zd\n", nfiltered); + if (!json) { + fprintf(outf, "%zd\n", nfiltered); + } else { + fprintf(outf, "\"process_count\": %zd", nfiltered); + } + free_processes(filtered, nfiltered); return ret; @@ -1038,7 +1049,7 @@ int main(int argc, char **argv) if (opt_json) json_seperator(outf); if (opt_count) { - ret = simple_filtered_count(outf, &filters, + ret = simple_filtered_count(outf, &filters, opt_json, profiles, nprofiles); } else { ret = detailed_profiles(outf, &filters, opt_json, @@ -1059,7 +1070,7 @@ int main(int argc, char **argv) if (ret != 0) { eprintf(_("Failed to get processes: %d....\n"), ret); } else if (opt_count) { - ret = simple_filtered_process_count(outf, &filters, + ret = simple_filtered_process_count(outf, &filters, opt_json, processes, nprocesses); } else { ret = detailed_processes(outf, &filters, opt_json,