Skip to content

Commit

Permalink
Merge branch 'bpftool-misc-fixes'
Browse files Browse the repository at this point in the history
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 <daniel@iogearbox.net>
  • Loading branch information
Daniel Borkmann committed Nov 30, 2017
2 parents a39e17b + ad3cda0 commit d775a41
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion tools/bpf/bpftool/Documentation/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RM ?= rm -f

# Make the path relative to DESTDIR, not prefix
ifndef DESTDIR
prefix?=$(HOME)
prefix ?= /usr/local
endif
mandir ?= $(prefix)/share/man
man8dir = $(mandir)/man8
Expand Down
7 changes: 4 additions & 3 deletions tools/bpf/bpftool/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ $(LIBBPF)-clean:
$(call QUIET_CLEAN, libbpf)
$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(OUTPUT) clean >/dev/null

prefix = /usr
bash_compdir ?= $(prefix)/share/bash-completion/completions
prefix = /usr/local
bash_compdir ?= /usr/share/bash-completion/completions

CC = gcc

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

install:
install -m 0755 -d $(prefix)/sbin
install $(OUTPUT)bpftool $(prefix)/sbin/bpftool
install -m 0755 -d $(bash_compdir)
install -m 0644 bash-completion/bpftool $(bash_compdir)
Expand All @@ -88,5 +89,5 @@ doc-install:

FORCE:

.PHONY: all clean FORCE
.PHONY: all clean FORCE install doc doc-install
.DEFAULT_GOAL := all
36 changes: 24 additions & 12 deletions tools/bpf/bpftool/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,19 @@ bool show_pinned;
struct pinned_obj_table prog_table;
struct pinned_obj_table map_table;

static void __noreturn clean_and_exit(int i)
{
if (json_output)
jsonw_destroy(&json_wtr);

exit(i);
}

void usage(void)
{
last_do_help(last_argc - 1, last_argv + 1);

exit(-1);
clean_and_exit(-1);
}

static int do_help(int argc, char **argv)
Expand Down Expand Up @@ -280,6 +288,7 @@ int main(int argc, char **argv)
hash_init(prog_table.table);
hash_init(map_table.table);

opterr = 0;
while ((opt = getopt_long(argc, argv, "Vhpjf",
options, NULL)) >= 0) {
switch (opt) {
Expand All @@ -291,13 +300,25 @@ int main(int argc, char **argv)
pretty_output = true;
/* fall through */
case 'j':
json_output = true;
if (!json_output) {
json_wtr = jsonw_new(stdout);
if (!json_wtr) {
p_err("failed to create JSON writer");
return -1;
}
json_output = true;
}
jsonw_pretty(json_wtr, pretty_output);
break;
case 'f':
show_pinned = true;
break;
default:
usage();
p_err("unrecognized option '%s'", argv[optind - 1]);
if (json_output)
clean_and_exit(-1);
else
usage();
}
}

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

if (json_output) {
json_wtr = jsonw_new(stdout);
if (!json_wtr) {
p_err("failed to create JSON writer");
return -1;
}
jsonw_pretty(json_wtr, pretty_output);
}

bfd_init();

ret = cmd_select(cmds, argc, argv, do_help);
Expand Down
5 changes: 3 additions & 2 deletions tools/bpf/bpftool/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <linux/bpf.h>
#include <linux/compiler.h>
#include <linux/kernel.h>
#include <linux/hashtable.h>

Expand All @@ -50,7 +51,7 @@

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

#define ERR_MAX_LEN 1024

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

bool is_prefix(const char *pfx, const char *str);
void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
void usage(void) __attribute__((noreturn));
void usage(void) __noreturn;

struct pinned_obj_table {
DECLARE_HASHTABLE(table, 16);
Expand Down

0 comments on commit d775a41

Please sign in to comment.