Skip to content

Commit

Permalink
Read configuration files from /etc and /usr
Browse files Browse the repository at this point in the history
Add support for the so called "stateless" configuration pattern (read
from /etc, fall back to /usr), giving system administrators a way to
define local configuration without changing any distro-provided files.

In practice this means that each configuration file FOO is loaded
from /usr/lib/iproute2/FOO unless /etc/iproute2/FOO exists.

Signed-off-by: Gioele Barabucci <gioele@svario.it>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
  • Loading branch information
gioele authored and shemminger committed Jul 26, 2023
1 parent 02ea021 commit 0a0a8f1
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 134 deletions.
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ endif

PREFIX?=/usr
SBINDIR?=/sbin
CONFDIR?=/etc/iproute2
CONF_ETC_DIR?=/etc/iproute2
CONF_USR_DIR?=$(PREFIX)/lib/iproute2
NETNS_RUN_DIR?=/var/run/netns
NETNS_ETC_DIR?=/etc/netns
DATADIR?=$(PREFIX)/share
Expand All @@ -37,7 +38,8 @@ ifneq ($(SHARED_LIBS),y)
DEFINES+= -DNO_SHARED_LIBS
endif

DEFINES+=-DCONFDIR=\"$(CONFDIR)\" \
DEFINES+=-DCONF_USR_DIR=\"$(CONF_USR_DIR)\" \
-DCONF_ETC_DIR=\"$(CONF_ETC_DIR)\" \
-DNETNS_RUN_DIR=\"$(NETNS_RUN_DIR)\" \
-DNETNS_ETC_DIR=\"$(NETNS_ETC_DIR)\"

Expand Down Expand Up @@ -100,11 +102,11 @@ config.mk:

install: all
install -m 0755 -d $(DESTDIR)$(SBINDIR)
install -m 0755 -d $(DESTDIR)$(CONFDIR)
install -m 0755 -d $(DESTDIR)$(CONF_USR_DIR)
install -m 0755 -d $(DESTDIR)$(ARPDDIR)
install -m 0755 -d $(DESTDIR)$(HDRDIR)
@for i in $(SUBDIRS); do $(MAKE) -C $$i install; done
install -m 0644 $(shell find etc/iproute2 -maxdepth 1 -type f) $(DESTDIR)$(CONFDIR)
install -m 0644 $(shell find etc/iproute2 -maxdepth 1 -type f) $(DESTDIR)$(CONF_USR_DIR)
install -m 0755 -d $(DESTDIR)$(BASH_COMPDIR)
install -m 0644 bash-completion/tc $(DESTDIR)$(BASH_COMPDIR)
install -m 0644 bash-completion/devlink $(DESTDIR)$(BASH_COMPDIR)
Expand Down
7 changes: 5 additions & 2 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ extern int numeric;
extern bool do_all;
extern int echo_request;

#ifndef CONFDIR
#define CONFDIR "/etc/iproute2"
#ifndef CONF_USR_DIR
#define CONF_USR_DIR "/usr/lib/iproute2"
#endif
#ifndef CONF_ETC_DIR
#define CONF_ETC_DIR "/etc/iproute2"
#endif

#define SPRINT_BSIZE 64
Expand Down
12 changes: 8 additions & 4 deletions lib/bpf_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -2751,7 +2751,7 @@ static bool bpf_pinning_reserved(uint32_t pinning)
}
}

static void bpf_hash_init(struct bpf_elf_ctx *ctx, const char *db_file)
static int bpf_hash_init(struct bpf_elf_ctx *ctx, const char *db_file)
{
struct bpf_hash_entry *entry;
char subpath[PATH_MAX] = {};
Expand All @@ -2761,14 +2761,14 @@ static void bpf_hash_init(struct bpf_elf_ctx *ctx, const char *db_file)

fp = fopen(db_file, "r");
if (!fp)
return;
return -errno;

while ((ret = bpf_read_pin_mapping(fp, &pinning, subpath))) {
if (ret == -1) {
fprintf(stderr, "Database %s is corrupted at: %s\n",
db_file, subpath);
fclose(fp);
return;
return -EINVAL;
}

if (bpf_pinning_reserved(pinning)) {
Expand Down Expand Up @@ -2796,6 +2796,8 @@ static void bpf_hash_init(struct bpf_elf_ctx *ctx, const char *db_file)
}

fclose(fp);

return 0;
}

static void bpf_hash_destroy(struct bpf_elf_ctx *ctx)
Expand Down Expand Up @@ -2924,7 +2926,9 @@ static int bpf_elf_ctx_init(struct bpf_elf_ctx *ctx, const char *pathname,
}

bpf_save_finfo(ctx);
bpf_hash_init(ctx, CONFDIR "/bpf_pinning");
bpf_hash_init(ctx, CONF_ETC_DIR "/bpf_pinning");
if (ret == -ENOENT)
ret = bpf_hash_init(ctx, CONF_USR_DIR "/bpf_pinning");

return 0;
out_free:
Expand Down

0 comments on commit 0a0a8f1

Please sign in to comment.