[PATCH v2 3/9] bpftool: Add flags parameter to open_obj_pinned_any() and open_obj_pinned()

From: Roberto Sassu
Date: Thu Jun 02 2022 - 10:38:45 EST


Add the flags parameter to open_obj_pinned_any() and open_obj_pinned(), so
that it is possible to specify the right permissions when obtaining a file
descriptor from a pinned object.

By default, the value passed to those functions is zero, so that there is
no change in the current behavior.

Signed-off-by: Roberto Sassu <roberto.sassu@xxxxxxxxxx>
---
tools/bpf/bpftool/common.c | 15 ++++++++-------
tools/bpf/bpftool/link.c | 2 +-
tools/bpf/bpftool/main.h | 5 +++--
3 files changed, 12 insertions(+), 10 deletions(-)

diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c
index a45b42ee8ab0..88e5e1900270 100644
--- a/tools/bpf/bpftool/common.c
+++ b/tools/bpf/bpftool/common.c
@@ -118,7 +118,7 @@ int mount_tracefs(const char *target)
return err;
}

-int open_obj_pinned(const char *path, bool quiet)
+int open_obj_pinned(const char *path, bool quiet, __u32 flags)
{
char *pname;
int fd = -1;
@@ -130,7 +130,7 @@ int open_obj_pinned(const char *path, bool quiet)
goto out_ret;
}

- fd = bpf_obj_get(pname);
+ fd = bpf_obj_get_flags(pname, flags);
if (fd < 0) {
if (!quiet)
p_err("bpf obj get (%s): %s", pname,
@@ -146,12 +146,13 @@ int open_obj_pinned(const char *path, bool quiet)
return fd;
}

-int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type)
+int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type,
+ __u32 flags)
{
enum bpf_obj_type type;
int fd;

- fd = open_obj_pinned(path, false);
+ fd = open_obj_pinned(path, false, flags);
if (fd < 0)
return -1;

@@ -400,7 +401,7 @@ static int do_build_table_cb(const char *fpath, const struct stat *sb,
if (typeflag != FTW_F)
goto out_ret;

- fd = open_obj_pinned(fpath, true);
+ fd = open_obj_pinned(fpath, true, 0);
if (fd < 0)
goto out_ret;

@@ -761,7 +762,7 @@ int prog_parse_fds(int *argc, char ***argv, int **fds)
path = **argv;
NEXT_ARGP();

- (*fds)[0] = open_obj_pinned_any(path, BPF_OBJ_PROG);
+ (*fds)[0] = open_obj_pinned_any(path, BPF_OBJ_PROG, 0);
if ((*fds)[0] < 0)
return -1;
return 1;
@@ -898,7 +899,7 @@ int map_parse_fds(int *argc, char ***argv, int **fds)
path = **argv;
NEXT_ARGP();

- (*fds)[0] = open_obj_pinned_any(path, BPF_OBJ_MAP);
+ (*fds)[0] = open_obj_pinned_any(path, BPF_OBJ_MAP, 0);
if ((*fds)[0] < 0)
return -1;
return 1;
diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c
index 7a20931c3250..04447ad9b3b3 100644
--- a/tools/bpf/bpftool/link.c
+++ b/tools/bpf/bpftool/link.c
@@ -44,7 +44,7 @@ static int link_parse_fd(int *argc, char ***argv)
path = **argv;
NEXT_ARGP();

- return open_obj_pinned_any(path, BPF_OBJ_LINK);
+ return open_obj_pinned_any(path, BPF_OBJ_LINK, 0);
}

p_err("expected 'id' or 'pinned', got: '%s'?", **argv);
diff --git a/tools/bpf/bpftool/main.h b/tools/bpf/bpftool/main.h
index 6c311f47147e..3f6c03afb2f8 100644
--- a/tools/bpf/bpftool/main.h
+++ b/tools/bpf/bpftool/main.h
@@ -141,8 +141,9 @@ void get_prog_full_name(const struct bpf_prog_info *prog_info, int prog_fd,
int get_fd_type(int fd);
const char *get_fd_type_name(enum bpf_obj_type type);
char *get_fdinfo(int fd, const char *key);
-int open_obj_pinned(const char *path, bool quiet);
-int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type);
+int open_obj_pinned(const char *path, bool quiet, __u32 flags);
+int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type,
+ __u32 flags);
int mount_bpffs_for_pin(const char *name);
int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(int *, char ***));
int do_pin_fd(int fd, const char *name);
--
2.25.1