Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpftool: Wrap struct_ops dump in an array

When dumping a struct_ops, 2 dictionaries are emitted.

When using `name`, they were already wrapped in an array, but not when
using `id`. Causing `jq` to fail at parsing the payload as it reached
the comma following the first dict.

This change wraps those dictionaries in an array so valid json is emitted.

Before, jq fails to parse the output:
```
$ sudo bpftool struct_ops dump id 1523612 | jq . > /dev/null
parse error: Expected value before ',' at line 19, column 2
```

After, no error parsing the output:
```
sudo ./bpftool struct_ops dump id 1523612 | jq . > /dev/null
```

Signed-off-by: Manu Bretelle <chantr4@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Tested-by: Eduard Zingerman <eddyz87@gmail.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Acked-by: Quentin Monnet <quentin@isovalent.com>
Link: https://lore.kernel.org/bpf/20231018230133.1593152-3-chantr4@gmail.com

authored by

Manu Bretelle and committed by
Daniel Borkmann
6bd5e167 90704b4b

+6
+6
tools/bpf/bpftool/struct_ops.c
··· 276 276 277 277 res.nr_maps++; 278 278 279 + if (wtr) 280 + jsonw_start_array(wtr); 281 + 279 282 if (func(fd, info, data, wtr)) 280 283 res.nr_errs++; 281 284 else if (!wtr && json_output) ··· 290 287 * other cmds. 291 288 */ 292 289 jsonw_null(json_wtr); 290 + 291 + if (wtr) 292 + jsonw_end_array(wtr); 293 293 294 294 done: 295 295 free(info);