Skip to content

Commit d775a41

Browse files
committed
Merge branch 'bpftool-misc-fixes'
Quentin Monnet says: ==================== First commit in this series fixes a crash that occurs when incorrect arguments are passed to bpftool after the `--json` option. It comes from the usage() function trying to use the JSON writer, although the latter has not been created yet at that point. Other patches add destruction of the writer in case the program exits in usage(), fix error messages handling when an unrecognized option is encountered, remove a spurious new-line character in an error message. Last patches are related to the Makefiles. They fix the installation directory prefix and .PHONY targets. ==================== Signed-off-by: Daniel Borkmann <[email protected]>
2 parents a39e17b + ad3cda0 commit d775a41

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

tools/bpf/bpftool/Documentation/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ RM ?= rm -f
66

77
# Make the path relative to DESTDIR, not prefix
88
ifndef DESTDIR
9-
prefix?=$(HOME)
9+
prefix ?= /usr/local
1010
endif
1111
mandir ?= $(prefix)/share/man
1212
man8dir = $(mandir)/man8

tools/bpf/bpftool/Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ $(LIBBPF)-clean:
4545
$(call QUIET_CLEAN, libbpf)
4646
$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(OUTPUT) clean >/dev/null
4747

48-
prefix = /usr
49-
bash_compdir ?= $(prefix)/share/bash-completion/completions
48+
prefix = /usr/local
49+
bash_compdir ?= /usr/share/bash-completion/completions
5050

5151
CC = gcc
5252

@@ -76,6 +76,7 @@ clean: $(LIBBPF)-clean
7676
$(Q)rm -rf $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
7777

7878
install:
79+
install -m 0755 -d $(prefix)/sbin
7980
install $(OUTPUT)bpftool $(prefix)/sbin/bpftool
8081
install -m 0755 -d $(bash_compdir)
8182
install -m 0644 bash-completion/bpftool $(bash_compdir)
@@ -88,5 +89,5 @@ doc-install:
8889

8990
FORCE:
9091

91-
.PHONY: all clean FORCE
92+
.PHONY: all clean FORCE install doc doc-install
9293
.DEFAULT_GOAL := all

tools/bpf/bpftool/main.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,19 @@ bool show_pinned;
5858
struct pinned_obj_table prog_table;
5959
struct pinned_obj_table map_table;
6060

61+
static void __noreturn clean_and_exit(int i)
62+
{
63+
if (json_output)
64+
jsonw_destroy(&json_wtr);
65+
66+
exit(i);
67+
}
68+
6169
void usage(void)
6270
{
6371
last_do_help(last_argc - 1, last_argv + 1);
6472

65-
exit(-1);
73+
clean_and_exit(-1);
6674
}
6775

6876
static int do_help(int argc, char **argv)
@@ -280,6 +288,7 @@ int main(int argc, char **argv)
280288
hash_init(prog_table.table);
281289
hash_init(map_table.table);
282290

291+
opterr = 0;
283292
while ((opt = getopt_long(argc, argv, "Vhpjf",
284293
options, NULL)) >= 0) {
285294
switch (opt) {
@@ -291,13 +300,25 @@ int main(int argc, char **argv)
291300
pretty_output = true;
292301
/* fall through */
293302
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);
295312
break;
296313
case 'f':
297314
show_pinned = true;
298315
break;
299316
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();
301322
}
302323
}
303324

@@ -306,15 +327,6 @@ int main(int argc, char **argv)
306327
if (argc < 0)
307328
usage();
308329

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-
318330
bfd_init();
319331

320332
ret = cmd_select(cmds, argc, argv, do_help);

tools/bpf/bpftool/main.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include <stdbool.h>
4242
#include <stdio.h>
4343
#include <linux/bpf.h>
44+
#include <linux/compiler.h>
4445
#include <linux/kernel.h>
4546
#include <linux/hashtable.h>
4647

@@ -50,7 +51,7 @@
5051

5152
#define NEXT_ARG() ({ argc--; argv++; if (argc < 0) usage(); })
5253
#define NEXT_ARGP() ({ (*argc)--; (*argv)++; if (*argc < 0) usage(); })
53-
#define BAD_ARG() ({ p_err("what is '%s'?\n", *argv); -1; })
54+
#define BAD_ARG() ({ p_err("what is '%s'?", *argv); -1; })
5455

5556
#define ERR_MAX_LEN 1024
5657

@@ -80,7 +81,7 @@ void p_info(const char *fmt, ...);
8081

8182
bool is_prefix(const char *pfx, const char *str);
8283
void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
83-
void usage(void) __attribute__((noreturn));
84+
void usage(void) __noreturn;
8485

8586
struct pinned_obj_table {
8687
DECLARE_HASHTABLE(table, 16);

0 commit comments

Comments
 (0)