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.
This commit is contained in:
Grimmauld 2024-12-09 10:57:58 +01:00
parent 5fb91616e3
commit 4f006a660c
Signed by: Grimmauld
SSH key fingerprint: SHA256:Q8IL6Y7sSKqzkyFdV1L0O/EflEh1fFV3tBtwxpapRH4

View file

@ -541,7 +541,12 @@ static int compare_processes_by_executable(const void *a, const void *b) {
static void json_header(FILE *outf) 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) 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); free_profiles(filtered, nfiltered);
} }
if (json) if (json)
fprintf(outf, "}, "); fprintf(outf, "}");
return AA_EXIT_ENABLED; return AA_EXIT_ENABLED;
} }
@ -702,7 +707,7 @@ static int detailed_processes(FILE *outf, filters_t *filters, bool json,
fprintf(outf, "]"); fprintf(outf, "]");
} }
fprintf(outf, "}\n"); fprintf(outf, "}");
} }
exit: exit:
@ -1030,6 +1035,8 @@ int main(int argc, char **argv)
if (opt_json) if (opt_json)
json_header(outf); json_header(outf);
if (opt_show & SHOW_PROFILES) { if (opt_show & SHOW_PROFILES) {
if (opt_json)
json_seperator(outf);
if (opt_count) { if (opt_count) {
ret = simple_filtered_count(outf, &filters, ret = simple_filtered_count(outf, &filters,
profiles, nprofiles); profiles, nprofiles);
@ -1042,6 +1049,9 @@ int main(int argc, char **argv)
} }
if (opt_show & SHOW_PROCESSES) { if (opt_show & SHOW_PROCESSES) {
if (opt_json)
json_seperator(outf);
struct process *processes = NULL; struct process *processes = NULL;
size_t nprocesses = 0; size_t nprocesses = 0;