Skip to content

Commit 9165e1d

Browse files
guidosarducciborkmann
authored andcommitted
bpftool: Use only nftw for file tree parsing
The bpftool sources include code to walk file trees, but use multiple frameworks to do so: nftw and fts. While nftw conforms to POSIX/SUSv3 and is widely available, fts is not conformant and less common, especially on non-glibc systems. The inconsistent framework usage hampers maintenance and portability of bpftool, in particular for embedded systems. Standardize code usage by rewriting one fts-based function to use nftw and clean up some related function warnings by extending use of "const char *" arguments. This change helps in building bpftool against musl for OpenWrt. Also fix an unsafe call to dirname() by duplicating the string to pass, since some implementations may directly alter it. The same approach is used in libbpf.c. Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: Quentin Monnet <quentin@isovalent.com> Link: https://lore.kernel.org/bpf/20200721024817.13701-1-Tony.Ambardar@gmail.com
1 parent 8fca4f9 commit 9165e1d

2 files changed

Lines changed: 82 additions & 59 deletions

File tree

‎tools/bpf/bpftool/common.c‎

Lines changed: 80 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
22
/* Copyright (C) 2017-2018 Netronome Systems, Inc. */
33

4+
#define _GNU_SOURCE
45
#include <ctype.h>
56
#include <errno.h>
67
#include <fcntl.h>
7-
#include <fts.h>
8+
#include <ftw.h>
89
#include <libgen.h>
910
#include <mntent.h>
1011
#include <stdbool.h>
@@ -161,24 +162,35 @@ int mount_tracefs(const char *target)
161162
return err;
162163
}
163164

164-
int open_obj_pinned(char *path, bool quiet)
165+
int open_obj_pinned(const char *path, bool quiet)
165166
{
166-
int fd;
167+
char *pname;
168+
int fd = -1;
169+
170+
pname = strdup(path);
171+
if (!pname) {
172+
if (!quiet)
173+
p_err("mem alloc failed");
174+
goto out_ret;
175+
}
167176

168-
fd = bpf_obj_get(path);
177+
fd = bpf_obj_get(pname);
169178
if (fd < 0) {
170179
if (!quiet)
171-
p_err("bpf obj get (%s): %s", path,
172-
errno == EACCES && !is_bpffs(dirname(path)) ?
180+
p_err("bpf obj get (%s): %s", pname,
181+
errno == EACCES && !is_bpffs(dirname(pname)) ?
173182
"directory not in bpf file system (bpffs)" :
174183
strerror(errno));
175-
return -1;
184+
goto out_free;
176185
}
177186

187+
out_free:
188+
free(pname);
189+
out_ret:
178190
return fd;
179191
}
180192

181-
int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
193+
int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type)
182194
{
183195
enum bpf_obj_type type;
184196
int fd;
@@ -368,71 +380,82 @@ void print_hex_data_json(uint8_t *data, size_t len)
368380
jsonw_end_array(json_wtr);
369381
}
370382

383+
/* extra params for nftw cb */
384+
static struct pinned_obj_table *build_fn_table;
385+
static enum bpf_obj_type build_fn_type;
386+
387+
static int do_build_table_cb(const char *fpath, const struct stat *sb,
388+
int typeflag, struct FTW *ftwbuf)
389+
{
390+
struct bpf_prog_info pinned_info;
391+
__u32 len = sizeof(pinned_info);
392+
struct pinned_obj *obj_node;
393+
enum bpf_obj_type objtype;
394+
int fd, err = 0;
395+
396+
if (typeflag != FTW_F)
397+
goto out_ret;
398+
399+
fd = open_obj_pinned(fpath, true);
400+
if (fd < 0)
401+
goto out_ret;
402+
403+
objtype = get_fd_type(fd);
404+
if (objtype != build_fn_type)
405+
goto out_close;
406+
407+
memset(&pinned_info, 0, sizeof(pinned_info));
408+
if (bpf_obj_get_info_by_fd(fd, &pinned_info, &len))
409+
goto out_close;
410+
411+
obj_node = calloc(1, sizeof(*obj_node));
412+
if (!obj_node) {
413+
err = -1;
414+
goto out_close;
415+
}
416+
417+
obj_node->id = pinned_info.id;
418+
obj_node->path = strdup(fpath);
419+
if (!obj_node->path) {
420+
err = -1;
421+
free(obj_node);
422+
goto out_close;
423+
}
424+
425+
hash_add(build_fn_table->table, &obj_node->hash, obj_node->id);
426+
out_close:
427+
close(fd);
428+
out_ret:
429+
return err;
430+
}
431+
371432
int build_pinned_obj_table(struct pinned_obj_table *tab,
372433
enum bpf_obj_type type)
373434
{
374-
struct bpf_prog_info pinned_info = {};
375-
struct pinned_obj *obj_node = NULL;
376-
__u32 len = sizeof(pinned_info);
377435
struct mntent *mntent = NULL;
378-
enum bpf_obj_type objtype;
379436
FILE *mntfile = NULL;
380-
FTSENT *ftse = NULL;
381-
FTS *fts = NULL;
382-
int fd, err;
437+
int flags = FTW_PHYS;
438+
int nopenfd = 16;
439+
int err = 0;
383440

384441
mntfile = setmntent("/proc/mounts", "r");
385442
if (!mntfile)
386443
return -1;
387444

445+
build_fn_table = tab;
446+
build_fn_type = type;
447+
388448
while ((mntent = getmntent(mntfile))) {
389-
char *path[] = { mntent->mnt_dir, NULL };
449+
char *path = mntent->mnt_dir;
390450

391451
if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
392452
continue;
393-
394-
fts = fts_open(path, 0, NULL);
395-
if (!fts)
396-
continue;
397-
398-
while ((ftse = fts_read(fts))) {
399-
if (!(ftse->fts_info & FTS_F))
400-
continue;
401-
fd = open_obj_pinned(ftse->fts_path, true);
402-
if (fd < 0)
403-
continue;
404-
405-
objtype = get_fd_type(fd);
406-
if (objtype != type) {
407-
close(fd);
408-
continue;
409-
}
410-
memset(&pinned_info, 0, sizeof(pinned_info));
411-
err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len);
412-
if (err) {
413-
close(fd);
414-
continue;
415-
}
416-
417-
obj_node = malloc(sizeof(*obj_node));
418-
if (!obj_node) {
419-
close(fd);
420-
fts_close(fts);
421-
fclose(mntfile);
422-
return -1;
423-
}
424-
425-
memset(obj_node, 0, sizeof(*obj_node));
426-
obj_node->id = pinned_info.id;
427-
obj_node->path = strdup(ftse->fts_path);
428-
hash_add(tab->table, &obj_node->hash, obj_node->id);
429-
430-
close(fd);
431-
}
432-
fts_close(fts);
453+
err = nftw(path, do_build_table_cb, nopenfd, flags);
454+
if (err)
455+
break;
433456
}
434457
fclose(mntfile);
435-
return 0;
458+
return err;
436459
}
437460

438461
void delete_pinned_obj_table(struct pinned_obj_table *tab)

‎tools/bpf/bpftool/main.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ int cmd_select(const struct cmd *cmds, int argc, char **argv,
152152
int get_fd_type(int fd);
153153
const char *get_fd_type_name(enum bpf_obj_type type);
154154
char *get_fdinfo(int fd, const char *key);
155-
int open_obj_pinned(char *path, bool quiet);
156-
int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type);
155+
int open_obj_pinned(const char *path, bool quiet);
156+
int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type);
157157
int mount_bpffs_for_pin(const char *name);
158158
int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(int *, char ***));
159159
int do_pin_fd(int fd, const char *name);

0 commit comments

Comments
 (0)