Commit 395fc4fa authored by Joe Burton's avatar Joe Burton Committed by Andrii Nakryiko

libbpf: Add bpf_obj_get_opts()

Add an extensible variant of bpf_obj_get() capable of setting the
`file_flags` parameter.

This parameter is needed to enable unprivileged access to BPF maps.
Without a method like this, users must manually make the syscall.
Signed-off-by: default avatarJoe Burton <jevburton@google.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220729202727.3311806-1-jevburton.kernel@gmail.com
parent d0b80a9e
...@@ -578,12 +578,21 @@ int bpf_obj_pin(int fd, const char *pathname) ...@@ -578,12 +578,21 @@ int bpf_obj_pin(int fd, const char *pathname)
} }
int bpf_obj_get(const char *pathname) int bpf_obj_get(const char *pathname)
{
return bpf_obj_get_opts(pathname, NULL);
}
int bpf_obj_get_opts(const char *pathname, const struct bpf_obj_get_opts *opts)
{ {
union bpf_attr attr; union bpf_attr attr;
int fd; int fd;
if (!OPTS_VALID(opts, bpf_obj_get_opts))
return libbpf_err(-EINVAL);
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, sizeof(attr));
attr.pathname = ptr_to_u64((void *)pathname); attr.pathname = ptr_to_u64((void *)pathname);
attr.file_flags = OPTS_GET(opts, file_flags, 0);
fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr)); fd = sys_bpf_fd(BPF_OBJ_GET, &attr, sizeof(attr));
return libbpf_err_errno(fd); return libbpf_err_errno(fd);
......
...@@ -270,8 +270,19 @@ LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values ...@@ -270,8 +270,19 @@ LIBBPF_API int bpf_map_update_batch(int fd, const void *keys, const void *values
__u32 *count, __u32 *count,
const struct bpf_map_batch_opts *opts); const struct bpf_map_batch_opts *opts);
struct bpf_obj_get_opts {
size_t sz; /* size of this struct for forward/backward compatibility */
__u32 file_flags;
size_t :0;
};
#define bpf_obj_get_opts__last_field file_flags
LIBBPF_API int bpf_obj_pin(int fd, const char *pathname); LIBBPF_API int bpf_obj_pin(int fd, const char *pathname);
LIBBPF_API int bpf_obj_get(const char *pathname); LIBBPF_API int bpf_obj_get(const char *pathname);
LIBBPF_API int bpf_obj_get_opts(const char *pathname,
const struct bpf_obj_get_opts *opts);
struct bpf_prog_attach_opts { struct bpf_prog_attach_opts {
size_t sz; /* size of this struct for forward/backward compatibility */ size_t sz; /* size of this struct for forward/backward compatibility */
......
...@@ -355,6 +355,7 @@ LIBBPF_0.8.0 { ...@@ -355,6 +355,7 @@ LIBBPF_0.8.0 {
LIBBPF_1.0.0 { LIBBPF_1.0.0 {
global: global:
bpf_obj_get_opts;
bpf_prog_query_opts; bpf_prog_query_opts;
bpf_program__attach_ksyscall; bpf_program__attach_ksyscall;
btf__add_enum64; btf__add_enum64;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment