Browse Source

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 <daniel@iogearbox.net>
Daniel Borkmann 8 years ago
parent
commit
d775a418ac

+ 1 - 1
tools/bpf/bpftool/Documentation/Makefile

@@ -6,7 +6,7 @@ RM ?= rm -f
 
 
 # Make the path relative to DESTDIR, not prefix
 # Make the path relative to DESTDIR, not prefix
 ifndef DESTDIR
 ifndef DESTDIR
-prefix?=$(HOME)
+prefix ?= /usr/local
 endif
 endif
 mandir ?= $(prefix)/share/man
 mandir ?= $(prefix)/share/man
 man8dir = $(mandir)/man8
 man8dir = $(mandir)/man8

+ 4 - 3
tools/bpf/bpftool/Makefile

@@ -45,8 +45,8 @@ $(LIBBPF)-clean:
 	$(call QUIET_CLEAN, libbpf)
 	$(call QUIET_CLEAN, libbpf)
 	$(Q)$(MAKE) -C $(BPF_DIR) OUTPUT=$(OUTPUT) clean >/dev/null
 	$(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
 CC = gcc
 
 
@@ -76,6 +76,7 @@ clean: $(LIBBPF)-clean
 	$(Q)rm -rf $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
 	$(Q)rm -rf $(OUTPUT)bpftool $(OUTPUT)*.o $(OUTPUT)*.d
 
 
 install:
 install:
+	install -m 0755 -d $(prefix)/sbin
 	install $(OUTPUT)bpftool $(prefix)/sbin/bpftool
 	install $(OUTPUT)bpftool $(prefix)/sbin/bpftool
 	install -m 0755 -d $(bash_compdir)
 	install -m 0755 -d $(bash_compdir)
 	install -m 0644 bash-completion/bpftool $(bash_compdir)
 	install -m 0644 bash-completion/bpftool $(bash_compdir)
@@ -88,5 +89,5 @@ doc-install:
 
 
 FORCE:
 FORCE:
 
 
-.PHONY: all clean FORCE
+.PHONY: all clean FORCE install doc doc-install
 .DEFAULT_GOAL := all
 .DEFAULT_GOAL := all

+ 24 - 12
tools/bpf/bpftool/main.c

@@ -58,11 +58,19 @@ bool show_pinned;
 struct pinned_obj_table prog_table;
 struct pinned_obj_table prog_table;
 struct pinned_obj_table map_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)
 void usage(void)
 {
 {
 	last_do_help(last_argc - 1, last_argv + 1);
 	last_do_help(last_argc - 1, last_argv + 1);
 
 
-	exit(-1);
+	clean_and_exit(-1);
 }
 }
 
 
 static int do_help(int argc, char **argv)
 static int do_help(int argc, char **argv)
@@ -280,6 +288,7 @@ int main(int argc, char **argv)
 	hash_init(prog_table.table);
 	hash_init(prog_table.table);
 	hash_init(map_table.table);
 	hash_init(map_table.table);
 
 
+	opterr = 0;
 	while ((opt = getopt_long(argc, argv, "Vhpjf",
 	while ((opt = getopt_long(argc, argv, "Vhpjf",
 				  options, NULL)) >= 0) {
 				  options, NULL)) >= 0) {
 		switch (opt) {
 		switch (opt) {
@@ -291,13 +300,25 @@ int main(int argc, char **argv)
 			pretty_output = true;
 			pretty_output = true;
 			/* fall through */
 			/* fall through */
 		case 'j':
 		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;
 			break;
 		case 'f':
 		case 'f':
 			show_pinned = true;
 			show_pinned = true;
 			break;
 			break;
 		default:
 		default:
-			usage();
+			p_err("unrecognized option '%s'", argv[optind - 1]);
+			if (json_output)
+				clean_and_exit(-1);
+			else
+				usage();
 		}
 		}
 	}
 	}
 
 
@@ -306,15 +327,6 @@ int main(int argc, char **argv)
 	if (argc < 0)
 	if (argc < 0)
 		usage();
 		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();
 	bfd_init();
 
 
 	ret = cmd_select(cmds, argc, argv, do_help);
 	ret = cmd_select(cmds, argc, argv, do_help);

+ 3 - 2
tools/bpf/bpftool/main.h

@@ -41,6 +41,7 @@
 #include <stdbool.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdio.h>
 #include <linux/bpf.h>
 #include <linux/bpf.h>
+#include <linux/compiler.h>
 #include <linux/kernel.h>
 #include <linux/kernel.h>
 #include <linux/hashtable.h>
 #include <linux/hashtable.h>
 
 
@@ -50,7 +51,7 @@
 
 
 #define NEXT_ARG()	({ argc--; argv++; if (argc < 0) usage(); })
 #define NEXT_ARG()	({ argc--; argv++; if (argc < 0) usage(); })
 #define NEXT_ARGP()	({ (*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
 #define ERR_MAX_LEN	1024
 
 
@@ -80,7 +81,7 @@ void p_info(const char *fmt, ...);
 
 
 bool is_prefix(const char *pfx, const char *str);
 bool is_prefix(const char *pfx, const char *str);
 void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
 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 {
 struct pinned_obj_table {
 	DECLARE_HASHTABLE(table, 16);
 	DECLARE_HASHTABLE(table, 16);