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>
This commit is contained in:
Daniel Borkmann 2017-11-30 02:09:30 +01:00
commit d775a418ac
4 changed files with 32 additions and 18 deletions

View File

@ -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

View File

@ -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
@ -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)
@ -88,5 +89,5 @@ doc-install:
FORCE:
.PHONY: all clean FORCE
.PHONY: all clean FORCE install doc doc-install
.DEFAULT_GOAL := all

View File

@ -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)
@ -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) {
@ -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();
}
}
@ -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);

View File

@ -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>
@ -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
@ -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);