mirror of
https://github.com/netdata/libbpf.git
synced 2026-04-05 16:19:06 +08:00
libbpf: Add pathname_concat() helper
Move snprintf and len check to common helper pathname_concat() to make the code simpler. Signed-off-by: Wang Yufen <wangyufen@huawei.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/1663828124-10437-1-git-send-email-wangyufen@huawei.com
This commit is contained in:
committed by
Andrii Nakryiko
parent
0420f75dbc
commit
fc2577c54c
76
src/libbpf.c
76
src/libbpf.c
@@ -2097,19 +2097,30 @@ static bool get_map_field_int(const char *map_name, const struct btf *btf,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int pathname_concat(char *buf, size_t buf_sz, const char *path, const char *name)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
|
||||||
|
len = snprintf(buf, buf_sz, "%s/%s", path, name);
|
||||||
|
if (len < 0)
|
||||||
|
return -EINVAL;
|
||||||
|
if (len >= buf_sz)
|
||||||
|
return -ENAMETOOLONG;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int build_map_pin_path(struct bpf_map *map, const char *path)
|
static int build_map_pin_path(struct bpf_map *map, const char *path)
|
||||||
{
|
{
|
||||||
char buf[PATH_MAX];
|
char buf[PATH_MAX];
|
||||||
int len;
|
int err;
|
||||||
|
|
||||||
if (!path)
|
if (!path)
|
||||||
path = "/sys/fs/bpf";
|
path = "/sys/fs/bpf";
|
||||||
|
|
||||||
len = snprintf(buf, PATH_MAX, "%s/%s", path, bpf_map__name(map));
|
err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
|
||||||
if (len < 0)
|
if (err)
|
||||||
return -EINVAL;
|
return err;
|
||||||
else if (len >= PATH_MAX)
|
|
||||||
return -ENAMETOOLONG;
|
|
||||||
|
|
||||||
return bpf_map__set_pin_path(map, buf);
|
return bpf_map__set_pin_path(map, buf);
|
||||||
}
|
}
|
||||||
@@ -7968,17 +7979,9 @@ int bpf_object__pin_maps(struct bpf_object *obj, const char *path)
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (path) {
|
if (path) {
|
||||||
int len;
|
err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
|
||||||
|
if (err)
|
||||||
len = snprintf(buf, PATH_MAX, "%s/%s", path,
|
|
||||||
bpf_map__name(map));
|
|
||||||
if (len < 0) {
|
|
||||||
err = -EINVAL;
|
|
||||||
goto err_unpin_maps;
|
goto err_unpin_maps;
|
||||||
} else if (len >= PATH_MAX) {
|
|
||||||
err = -ENAMETOOLONG;
|
|
||||||
goto err_unpin_maps;
|
|
||||||
}
|
|
||||||
sanitize_pin_path(buf);
|
sanitize_pin_path(buf);
|
||||||
pin_path = buf;
|
pin_path = buf;
|
||||||
} else if (!map->pin_path) {
|
} else if (!map->pin_path) {
|
||||||
@@ -8016,14 +8019,9 @@ int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
|
|||||||
char buf[PATH_MAX];
|
char buf[PATH_MAX];
|
||||||
|
|
||||||
if (path) {
|
if (path) {
|
||||||
int len;
|
err = pathname_concat(buf, sizeof(buf), path, bpf_map__name(map));
|
||||||
|
if (err)
|
||||||
len = snprintf(buf, PATH_MAX, "%s/%s", path,
|
return libbpf_err(err);
|
||||||
bpf_map__name(map));
|
|
||||||
if (len < 0)
|
|
||||||
return libbpf_err(-EINVAL);
|
|
||||||
else if (len >= PATH_MAX)
|
|
||||||
return libbpf_err(-ENAMETOOLONG);
|
|
||||||
sanitize_pin_path(buf);
|
sanitize_pin_path(buf);
|
||||||
pin_path = buf;
|
pin_path = buf;
|
||||||
} else if (!map->pin_path) {
|
} else if (!map->pin_path) {
|
||||||
@@ -8041,6 +8039,7 @@ int bpf_object__unpin_maps(struct bpf_object *obj, const char *path)
|
|||||||
int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
|
int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
|
||||||
{
|
{
|
||||||
struct bpf_program *prog;
|
struct bpf_program *prog;
|
||||||
|
char buf[PATH_MAX];
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
if (!obj)
|
if (!obj)
|
||||||
@@ -8052,17 +8051,9 @@ int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
|
|||||||
}
|
}
|
||||||
|
|
||||||
bpf_object__for_each_program(prog, obj) {
|
bpf_object__for_each_program(prog, obj) {
|
||||||
char buf[PATH_MAX];
|
err = pathname_concat(buf, sizeof(buf), path, prog->name);
|
||||||
int len;
|
if (err)
|
||||||
|
|
||||||
len = snprintf(buf, PATH_MAX, "%s/%s", path, prog->name);
|
|
||||||
if (len < 0) {
|
|
||||||
err = -EINVAL;
|
|
||||||
goto err_unpin_programs;
|
goto err_unpin_programs;
|
||||||
} else if (len >= PATH_MAX) {
|
|
||||||
err = -ENAMETOOLONG;
|
|
||||||
goto err_unpin_programs;
|
|
||||||
}
|
|
||||||
|
|
||||||
err = bpf_program__pin(prog, buf);
|
err = bpf_program__pin(prog, buf);
|
||||||
if (err)
|
if (err)
|
||||||
@@ -8073,13 +8064,7 @@ int bpf_object__pin_programs(struct bpf_object *obj, const char *path)
|
|||||||
|
|
||||||
err_unpin_programs:
|
err_unpin_programs:
|
||||||
while ((prog = bpf_object__prev_program(obj, prog))) {
|
while ((prog = bpf_object__prev_program(obj, prog))) {
|
||||||
char buf[PATH_MAX];
|
if (pathname_concat(buf, sizeof(buf), path, prog->name))
|
||||||
int len;
|
|
||||||
|
|
||||||
len = snprintf(buf, PATH_MAX, "%s/%s", path, prog->name);
|
|
||||||
if (len < 0)
|
|
||||||
continue;
|
|
||||||
else if (len >= PATH_MAX)
|
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
bpf_program__unpin(prog, buf);
|
bpf_program__unpin(prog, buf);
|
||||||
@@ -8098,13 +8083,10 @@ int bpf_object__unpin_programs(struct bpf_object *obj, const char *path)
|
|||||||
|
|
||||||
bpf_object__for_each_program(prog, obj) {
|
bpf_object__for_each_program(prog, obj) {
|
||||||
char buf[PATH_MAX];
|
char buf[PATH_MAX];
|
||||||
int len;
|
|
||||||
|
|
||||||
len = snprintf(buf, PATH_MAX, "%s/%s", path, prog->name);
|
err = pathname_concat(buf, sizeof(buf), path, prog->name);
|
||||||
if (len < 0)
|
if (err)
|
||||||
return libbpf_err(-EINVAL);
|
return libbpf_err(err);
|
||||||
else if (len >= PATH_MAX)
|
|
||||||
return libbpf_err(-ENAMETOOLONG);
|
|
||||||
|
|
||||||
err = bpf_program__unpin(prog, buf);
|
err = bpf_program__unpin(prog, buf);
|
||||||
if (err)
|
if (err)
|
||||||
|
|||||||
Reference in New Issue
Block a user