tools: bpftool: provide JSON output for all possible commands

As all commands can now return JSON output (possibly just a "null"
value), output of `bpftool --json batch file FILE` should also be fully
JSON compliant.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Quentin Monnet 2017-10-23 09:24:14 -07:00 committed by David S. Miller
parent 9a5ab8bf1d
commit 004b45c0e5
3 changed files with 47 additions and 6 deletions

View File

@ -64,6 +64,11 @@ void usage(void)
static int do_help(int argc, char **argv) static int do_help(int argc, char **argv)
{ {
if (json_output) {
jsonw_null(json_wtr);
return 0;
}
fprintf(stderr, fprintf(stderr,
"Usage: %s OBJECT { COMMAND | help }\n" "Usage: %s OBJECT { COMMAND | help }\n"
" %s batch file FILE\n" " %s batch file FILE\n"
@ -77,10 +82,22 @@ static int do_help(int argc, char **argv)
static int do_version(int argc, char **argv) static int do_version(int argc, char **argv)
{ {
printf("%s v%d.%d.%d\n", bin_name, unsigned int version[3];
LINUX_VERSION_CODE >> 16,
LINUX_VERSION_CODE >> 8 & 0xf, version[0] = LINUX_VERSION_CODE >> 16;
LINUX_VERSION_CODE & 0xf); version[1] = LINUX_VERSION_CODE >> 8 & 0xf;
version[2] = LINUX_VERSION_CODE & 0xf;
if (json_output) {
jsonw_start_object(json_wtr);
jsonw_name(json_wtr, "version");
jsonw_printf(json_wtr, "\"%u.%u.%u\"",
version[0], version[1], version[2]);
jsonw_end_object(json_wtr);
} else {
printf("%s v%u.%u.%u\n", bin_name,
version[0], version[1], version[2]);
}
return 0; return 0;
} }

View File

@ -651,6 +651,8 @@ exit_free:
free(value); free(value);
close(fd); close(fd);
if (!err && json_output)
jsonw_null(json_wtr);
return err; return err;
} }
@ -812,16 +814,28 @@ exit_free:
free(key); free(key);
close(fd); close(fd);
if (!err && json_output)
jsonw_null(json_wtr);
return err; return err;
} }
static int do_pin(int argc, char **argv) static int do_pin(int argc, char **argv)
{ {
return do_pin_any(argc, argv, bpf_map_get_fd_by_id); int err;
err = do_pin_any(argc, argv, bpf_map_get_fd_by_id);
if (!err && json_output)
jsonw_null(json_wtr);
return err;
} }
static int do_help(int argc, char **argv) static int do_help(int argc, char **argv)
{ {
if (json_output) {
jsonw_null(json_wtr);
return 0;
}
fprintf(stderr, fprintf(stderr,
"Usage: %s %s show [MAP]\n" "Usage: %s %s show [MAP]\n"
" %s %s dump MAP\n" " %s %s dump MAP\n"

View File

@ -602,11 +602,21 @@ err_free:
static int do_pin(int argc, char **argv) static int do_pin(int argc, char **argv)
{ {
return do_pin_any(argc, argv, bpf_prog_get_fd_by_id); int err;
err = do_pin_any(argc, argv, bpf_prog_get_fd_by_id);
if (!err && json_output)
jsonw_null(json_wtr);
return err;
} }
static int do_help(int argc, char **argv) static int do_help(int argc, char **argv)
{ {
if (json_output) {
jsonw_null(json_wtr);
return 0;
}
fprintf(stderr, fprintf(stderr,
"Usage: %s %s show [PROG]\n" "Usage: %s %s show [PROG]\n"
" %s %s dump xlated PROG [{ file FILE | opcodes }]\n" " %s %s dump xlated PROG [{ file FILE | opcodes }]\n"