Commit bb180fb2 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

libbpf: Factor out common logic of testing and closing FD

Factor out common piece of logic that detects support for a feature based on
successfully created FD. Also take care of closing FD, if it was created.
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200818213356.2629020-4-andriin@fb.com
parent 47b6cb4d
...@@ -3431,6 +3431,13 @@ bpf_object__probe_loading(struct bpf_object *obj) ...@@ -3431,6 +3431,13 @@ bpf_object__probe_loading(struct bpf_object *obj)
return 0; return 0;
} }
static int probe_fd(int fd)
{
if (fd >= 0)
close(fd);
return fd >= 0;
}
static int probe_kern_prog_name(void) static int probe_kern_prog_name(void)
{ {
struct bpf_load_program_attr attr; struct bpf_load_program_attr attr;
...@@ -3449,12 +3456,7 @@ static int probe_kern_prog_name(void) ...@@ -3449,12 +3456,7 @@ static int probe_kern_prog_name(void)
attr.license = "GPL"; attr.license = "GPL";
attr.name = "test"; attr.name = "test";
ret = bpf_load_program_xattr(&attr, NULL, 0); ret = bpf_load_program_xattr(&attr, NULL, 0);
if (ret >= 0) { return probe_fd(ret);
close(ret);
return 1;
}
return 0;
} }
static int probe_kern_global_data(void) static int probe_kern_global_data(void)
...@@ -3495,12 +3497,7 @@ static int probe_kern_global_data(void) ...@@ -3495,12 +3497,7 @@ static int probe_kern_global_data(void)
ret = bpf_load_program_xattr(&prg_attr, NULL, 0); ret = bpf_load_program_xattr(&prg_attr, NULL, 0);
close(map); close(map);
if (ret >= 0) { return probe_fd(ret);
close(ret);
return 1;
}
return 0;
} }
static int probe_kern_btf_func(void) static int probe_kern_btf_func(void)
...@@ -3516,16 +3513,9 @@ static int probe_kern_btf_func(void) ...@@ -3516,16 +3513,9 @@ static int probe_kern_btf_func(void)
/* FUNC x */ /* [3] */ /* FUNC x */ /* [3] */
BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2), BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, 0), 2),
}; };
int btf_fd;
btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types), return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
strs, sizeof(strs)); strs, sizeof(strs)));
if (btf_fd >= 0) {
close(btf_fd);
return 1;
}
return 0;
} }
static int probe_kern_btf_func_global(void) static int probe_kern_btf_func_global(void)
...@@ -3541,16 +3531,9 @@ static int probe_kern_btf_func_global(void) ...@@ -3541,16 +3531,9 @@ static int probe_kern_btf_func_global(void)
/* FUNC x BTF_FUNC_GLOBAL */ /* [3] */ /* FUNC x BTF_FUNC_GLOBAL */ /* [3] */
BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 2), BTF_TYPE_ENC(5, BTF_INFO_ENC(BTF_KIND_FUNC, 0, BTF_FUNC_GLOBAL), 2),
}; };
int btf_fd;
btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types), return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
strs, sizeof(strs)); strs, sizeof(strs)));
if (btf_fd >= 0) {
close(btf_fd);
return 1;
}
return 0;
} }
static int probe_kern_btf_datasec(void) static int probe_kern_btf_datasec(void)
...@@ -3567,16 +3550,9 @@ static int probe_kern_btf_datasec(void) ...@@ -3567,16 +3550,9 @@ static int probe_kern_btf_datasec(void)
BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4), BTF_TYPE_ENC(3, BTF_INFO_ENC(BTF_KIND_DATASEC, 0, 1), 4),
BTF_VAR_SECINFO_ENC(2, 0, 4), BTF_VAR_SECINFO_ENC(2, 0, 4),
}; };
int btf_fd;
btf_fd = libbpf__load_raw_btf((char *)types, sizeof(types),
strs, sizeof(strs));
if (btf_fd >= 0) {
close(btf_fd);
return 1;
}
return 0; return probe_fd(libbpf__load_raw_btf((char *)types, sizeof(types),
strs, sizeof(strs)));
} }
static int probe_kern_array_mmap(void) static int probe_kern_array_mmap(void)
...@@ -3588,14 +3564,8 @@ static int probe_kern_array_mmap(void) ...@@ -3588,14 +3564,8 @@ static int probe_kern_array_mmap(void)
.value_size = sizeof(int), .value_size = sizeof(int),
.max_entries = 1, .max_entries = 1,
}; };
int fd;
fd = bpf_create_map_xattr(&attr); return probe_fd(bpf_create_map_xattr(&attr));
if (fd >= 0) {
close(fd);
return 1;
}
return 0;
} }
static int probe_kern_exp_attach_type(void) static int probe_kern_exp_attach_type(void)
...@@ -3605,7 +3575,6 @@ static int probe_kern_exp_attach_type(void) ...@@ -3605,7 +3575,6 @@ static int probe_kern_exp_attach_type(void)
BPF_MOV64_IMM(BPF_REG_0, 0), BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
int fd;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, sizeof(attr));
/* use any valid combination of program type and (optional) /* use any valid combination of program type and (optional)
...@@ -3619,12 +3588,7 @@ static int probe_kern_exp_attach_type(void) ...@@ -3619,12 +3588,7 @@ static int probe_kern_exp_attach_type(void)
attr.insns_cnt = ARRAY_SIZE(insns); attr.insns_cnt = ARRAY_SIZE(insns);
attr.license = "GPL"; attr.license = "GPL";
fd = bpf_load_program_xattr(&attr, NULL, 0); return probe_fd(bpf_load_program_xattr(&attr, NULL, 0));
if (fd >= 0) {
close(fd);
return 1;
}
return 0;
} }
enum kern_feature_result { enum kern_feature_result {
......
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