diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-19 21:00:37 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-19 21:00:37 +0000 |
commit | 94ac2ab3fff96814d7460a27a0e9d004abbd4128 (patch) | |
tree | 9a4eb8cc234b540b0f4b93363109cdd37a20540b /tools/bpf/bpftool/common.c | |
parent | Adding debian version 6.8.12-1. (diff) | |
download | linux-94ac2ab3fff96814d7460a27a0e9d004abbd4128.tar.xz linux-94ac2ab3fff96814d7460a27a0e9d004abbd4128.zip |
Merging upstream version 6.9.2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/bpf/bpftool/common.c')
-rw-r--r-- | tools/bpf/bpftool/common.c | 96 |
1 files changed, 12 insertions, 84 deletions
diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c index 958e92acca..cc6e6aae24 100644 --- a/tools/bpf/bpftool/common.c +++ b/tools/bpf/bpftool/common.c @@ -244,101 +244,29 @@ int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type) return fd; } -int create_and_mount_bpffs_dir(const char *dir_name) +int mount_bpffs_for_pin(const char *name, bool is_dir) { char err_str[ERR_MAX_LEN]; - bool dir_exists; - int err = 0; - - if (is_bpffs(dir_name)) - return err; - - dir_exists = access(dir_name, F_OK) == 0; - - if (!dir_exists) { - char *temp_name; - char *parent_name; - - temp_name = strdup(dir_name); - if (!temp_name) { - p_err("mem alloc failed"); - return -1; - } - - parent_name = dirname(temp_name); - - if (is_bpffs(parent_name)) { - /* nothing to do if already mounted */ - free(temp_name); - return err; - } - - if (access(parent_name, F_OK) == -1) { - p_err("can't create dir '%s' to pin BPF object: parent dir '%s' doesn't exist", - dir_name, parent_name); - free(temp_name); - return -1; - } - - free(temp_name); - } - - if (block_mount) { - p_err("no BPF file system found, not mounting it due to --nomount option"); - return -1; - } - - if (!dir_exists) { - err = mkdir(dir_name, S_IRWXU); - if (err) { - p_err("failed to create dir '%s': %s", dir_name, strerror(errno)); - return err; - } - } - - err = mnt_fs(dir_name, "bpf", err_str, ERR_MAX_LEN); - if (err) { - err_str[ERR_MAX_LEN - 1] = '\0'; - p_err("can't mount BPF file system on given dir '%s': %s", - dir_name, err_str); - - if (!dir_exists) - rmdir(dir_name); - } - - return err; -} - -int mount_bpffs_for_file(const char *file_name) -{ - char err_str[ERR_MAX_LEN]; - char *temp_name; + char *file; char *dir; int err = 0; - if (access(file_name, F_OK) != -1) { - p_err("can't pin BPF object: path '%s' already exists", file_name); - return -1; - } + if (is_dir && is_bpffs(name)) + return err; - temp_name = strdup(file_name); - if (!temp_name) { + file = malloc(strlen(name) + 1); + if (!file) { p_err("mem alloc failed"); return -1; } - dir = dirname(temp_name); + strcpy(file, name); + dir = dirname(file); if (is_bpffs(dir)) /* nothing to do if already mounted */ goto out_free; - if (access(dir, F_OK) == -1) { - p_err("can't pin BPF object: dir '%s' doesn't exist", dir); - err = -1; - goto out_free; - } - if (block_mount) { p_err("no BPF file system found, not mounting it due to --nomount option"); err = -1; @@ -348,12 +276,12 @@ int mount_bpffs_for_file(const char *file_name) err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN); if (err) { err_str[ERR_MAX_LEN - 1] = '\0'; - p_err("can't mount BPF file system to pin the object '%s': %s", - file_name, err_str); + p_err("can't mount BPF file system to pin the object (%s): %s", + name, err_str); } out_free: - free(temp_name); + free(file); return err; } @@ -361,7 +289,7 @@ int do_pin_fd(int fd, const char *name) { int err; - err = mount_bpffs_for_file(name); + err = mount_bpffs_for_pin(name, false); if (err) return err; |