Skip to content

Commit

Permalink
libbpf: Move directory creation into _pin() functions
Browse files Browse the repository at this point in the history
The existing pin_*() functions all try to create the parent directory
before pinning. Move this check into the per-object _pin() functions
instead. This ensures consistent behaviour when auto-pinning is
added (which doesn't go through the top-level pin_maps() function), at the
cost of a few more calls to mkdir().

Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/157269297985.394725.5882630952992598610.stgit@toke.dk
  • Loading branch information
Toke Høiland-Jørgensen authored and Alexei Starovoitov committed Nov 2, 2019
1 parent 4580b25 commit 196f848
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions tools/lib/bpf/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3816,6 +3816,28 @@ int bpf_object__load(struct bpf_object *obj)
return bpf_object__load_xattr(&attr);
}

static int make_parent_dir(const char *path)
{
char *cp, errmsg[STRERR_BUFSIZE];
char *dname, *dir;
int err = 0;

dname = strdup(path);
if (dname == NULL)
return -ENOMEM;

dir = dirname(dname);
if (mkdir(dir, 0700) && errno != EEXIST)
err = -errno;

free(dname);
if (err) {
cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
pr_warn("failed to mkdir %s: %s\n", path, cp);
}
return err;
}

static int check_path(const char *path)
{
char *cp, errmsg[STRERR_BUFSIZE];
Expand Down Expand Up @@ -3852,6 +3874,10 @@ int bpf_program__pin_instance(struct bpf_program *prog, const char *path,
char *cp, errmsg[STRERR_BUFSIZE];
int err;

err = make_parent_dir(path);
if (err)
return err;

err = check_path(path);
if (err)
return err;
Expand Down Expand Up @@ -3905,25 +3931,14 @@ int bpf_program__unpin_instance(struct bpf_program *prog, const char *path,
return 0;
}

static int make_dir(const char *path)
{
char *cp, errmsg[STRERR_BUFSIZE];
int err = 0;

if (mkdir(path, 0700) && errno != EEXIST)
err = -errno;

if (err) {
cp = libbpf_strerror_r(-err, errmsg, sizeof(errmsg));
pr_warn("failed to mkdir %s: %s\n", path, cp);
}
return err;
}

int bpf_program__pin(struct bpf_program *prog, const char *path)
{
int i, err;

err = make_parent_dir(path);
if (err)
return err;

err = check_path(path);
if (err)
return err;
Expand All @@ -3944,10 +3959,6 @@ int bpf_program__pin(struct bpf_program *prog, const char *path)
return bpf_program__pin_instance(prog, path, 0);
}

err = make_dir(path);
if (err)
return err;

for (i = 0; i < prog->instances.nr; i++) {
char buf[PATH_MAX];
int len;
Expand Down Expand Up @@ -4070,6 +4081,10 @@ int bpf_map__pin(struct bpf_map *map, const char *path)
}
}

err = make_parent_dir(map->pin_path);
if (err)
return err;

err = check_path(map->pin_path);
if (err)
return err;
Expand Down Expand Up @@ -4164,10 +4179,6 @@ int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
return -ENOENT;
}

err = make_dir(path);
if (err)
return err;

bpf_object__for_each_map(map, obj) {
char *pin_path = NULL;
char buf[PATH_MAX];
Expand Down Expand Up @@ -4254,10 +4265,6 @@ int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
return -ENOENT;
}

err = make_dir(path);
if (err)
return err;

bpf_object__for_each_program(prog, obj) {
char buf[PATH_MAX];
int len;
Expand Down

0 comments on commit 196f848

Please sign in to comment.