aboutsummaryrefslogtreecommitdiffstats
path: root/tools/bpf/bpftool/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/bpf/bpftool/main.c')
-rw-r--r--tools/bpf/bpftool/main.c36
1 files changed, 24 insertions, 12 deletions
diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index d6e4762170a4..d294bc8168be 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -58,11 +58,19 @@ bool show_pinned;
58struct pinned_obj_table prog_table; 58struct pinned_obj_table prog_table;
59struct pinned_obj_table map_table; 59struct pinned_obj_table map_table;
60 60
61static void __noreturn clean_and_exit(int i)
62{
63 if (json_output)
64 jsonw_destroy(&json_wtr);
65
66 exit(i);
67}
68
61void usage(void) 69void usage(void)
62{ 70{
63 last_do_help(last_argc - 1, last_argv + 1); 71 last_do_help(last_argc - 1, last_argv + 1);
64 72
65 exit(-1); 73 clean_and_exit(-1);
66} 74}
67 75
68static int do_help(int argc, char **argv) 76static int do_help(int argc, char **argv)
@@ -280,6 +288,7 @@ int main(int argc, char **argv)
280 hash_init(prog_table.table); 288 hash_init(prog_table.table);
281 hash_init(map_table.table); 289 hash_init(map_table.table);
282 290
291 opterr = 0;
283 while ((opt = getopt_long(argc, argv, "Vhpjf", 292 while ((opt = getopt_long(argc, argv, "Vhpjf",
284 options, NULL)) >= 0) { 293 options, NULL)) >= 0) {
285 switch (opt) { 294 switch (opt) {
@@ -291,13 +300,25 @@ int main(int argc, char **argv)
291 pretty_output = true; 300 pretty_output = true;
292 /* fall through */ 301 /* fall through */
293 case 'j': 302 case 'j':
294 json_output = true; 303 if (!json_output) {
304 json_wtr = jsonw_new(stdout);
305 if (!json_wtr) {
306 p_err("failed to create JSON writer");
307 return -1;
308 }
309 json_output = true;
310 }
311 jsonw_pretty(json_wtr, pretty_output);
295 break; 312 break;
296 case 'f': 313 case 'f':
297 show_pinned = true; 314 show_pinned = true;
298 break; 315 break;
299 default: 316 default:
300 usage(); 317 p_err("unrecognized option '%s'", argv[optind - 1]);
318 if (json_output)
319 clean_and_exit(-1);
320 else
321 usage();
301 } 322 }
302 } 323 }
303 324
@@ -306,15 +327,6 @@ int main(int argc, char **argv)
306 if (argc < 0) 327 if (argc < 0)
307 usage(); 328 usage();
308 329
309 if (json_output) {
310 json_wtr = jsonw_new(stdout);
311 if (!json_wtr) {
312 p_err("failed to create JSON writer");
313 return -1;
314 }
315 jsonw_pretty(json_wtr, pretty_output);
316 }
317
318 bfd_init(); 330 bfd_init();
319 331
320 ret = cmd_select(cmds, argc, argv, do_help); 332 ret = cmd_select(cmds, argc, argv, do_help);