bpftool: use only nftw for file tree parsing
Subsystems (4)
A message that is not in the archive (sent off-list or not yet received): 20200715051214.28099-1-Tony.Ambardar () gmail ! com
PATCH v2 1/1 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. Clean up related function warnings by using "const char *" arguments and fixing an unsafe call to dirname().
These changes help in building bpftool against musl for OpenWrt.
---
V2:
* use _GNU_SOURCE to pull in getpagesize(), getline(), nftw() definitions
* use "const char *" in open_obj_pinned() and open_obj_pinned_any()
* make dirname() safely act on a string copy
--- tools/bpf/bpftool/common.c | 129 +++++++++++++++++++++---------------- tools/bpf/bpftool/main.h | 4 +- 2 files changed, 76 insertions(+), 57 deletions(-)
tools/bpf/bpftool/common.c +74 -55
@@ -1,10 +1,11 @@// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ +#define _GNU_SOURCE#include <ctype.h> #include <errno.h> #include <fcntl.h> -#include <fts.h> +#include <ftw.h> #include <libgen.h> #include <mntent.h> #include <stdbool.h>
@@ -160,24 +161,36 @@ int mount_tracefs(const char *target)return err; } -int open_obj_pinned (char *path,bool quiet) +int open_obj_pinned (const char *path,bool quiet) { -int fd; +char *pname; +int fd =-1 ; + + pname =strdup (path); +if (pname ==NULL ) { +if (!quiet) +p_err ("bpf obj get (%s): %s" , path,strerror (errno)); + goto out_ret; + } + - fd =bpf_obj_get (path); + fd =bpf_obj_get (pname);if (fd <0 ) {if (!quiet) -p_err ("bpf obj get (%s): %s" , path, - errno ==EACCES && !is_bpffs (dirname (path)) ? +p_err ("bpf obj get (%s): %s" , pname, + errno ==EACCES && !is_bpffs (dirname (pname)) ?"directory not in bpf file system (bpffs)" :strerror (errno)); -return -1 ; + goto out_free; } +out_free: +free (pname); +out_ret:return fd; } -int open_obj_pinned_any (char *path,enum bpf_obj_type exp_type) +int open_obj_pinned_any (const char *path,enum bpf_obj_type exp_type) {enum bpf_obj_type type;int fd;
@@ -367,68 +380,74 @@ void print_hex_data_json(uint8_t *data, size_t len)jsonw_end_array (json_wtr); } -int build_pinned_obj_table (struct pinned_obj_table *tab, -enum bpf_obj_type type) +static struct pinned_obj_table *build_fn_table;/* params for nftw cb*/ +static enum bpf_obj_type build_fn_type; + +static int do_build_table_cb (const char *fpath,const struct stat *sb, +int typeflag,struct FTW *ftwbuf) {struct bpf_prog_info pinned_info = {};struct pinned_obj *obj_node =NULL ;__u32 len =sizeof (pinned_info); -struct mntent *mntent =NULL ;enum bpf_obj_type objtype; +int fd, err =0 ; + +if (typeflag !=FTW_F ) + goto out_ret; + fd =open_obj_pinned (fpath, true); +if (fd <0 ) + goto out_ret; + + objtype =get_fd_type (fd); +if (objtype != build_fn_type) + goto out_close; + +memset (&pinned_info,0 ,sizeof (pinned_info)); +if (bpf_obj_get_info_by_fd (fd, &pinned_info, &len)) { +p_err ("can't get obj info: %s" ,strerror (errno)); + goto out_close; + } + + obj_node =malloc (sizeof (*obj_node)); +if (!obj_node) { +p_err ("mem alloc failed" ); + err =-1 ; + goto out_close; + } + +memset (obj_node,0 ,sizeof (*obj_node)); + obj_node->id = pinned_info.id; + obj_node->path =strdup (fpath); +hash_add (build_fn_table->table, &obj_node->hash, obj_node->id); + +out_close: +close (fd); +out_ret: +return err; +} + +int build_pinned_obj_table (struct pinned_obj_table *tab, +enum bpf_obj_type type) +{ +struct mntent *mntent =NULL ;FILE *mntfile =NULL ; -FTSENT *ftse =NULL ; -FTS *fts =NULL ; -int fd, err; +int flags =FTW_PHYS ; +int nopenfd =16 ; mntfile =setmntent ("/proc/mounts" ,"r" );if (!mntfile)return -1 ; + build_fn_table = tab; + build_fn_type = type; +while ((mntent =getmntent (mntfile))) { -char *path[] = { mntent->mnt_dir,NULL }; +char *path = mntent->mnt_dir;if (strncmp (mntent->mnt_type,"bpf" ,3 ) !=0 )continue ; - - fts =fts_open (path,0 ,NULL ); -if (!fts) -continue ; - -while ((ftse =fts_read (fts))) { -if (!(ftse->fts_info &FTS_F )) -continue ; - fd =open_obj_pinned (ftse->fts_path, true); -if (fd <0 ) -continue ; - - objtype =get_fd_type (fd); -if (objtype != type) { -close (fd); -continue ; - } -memset (&pinned_info,0 ,sizeof (pinned_info)); - err =bpf_obj_get_info_by_fd (fd, &pinned_info, &len); -if (err) { -close (fd); -continue ; - } - - obj_node =malloc (sizeof (*obj_node)); -if (!obj_node) { -close (fd); -fts_close (fts); -fclose (mntfile); -return -1 ; - } - -memset (obj_node,0 ,sizeof (*obj_node)); - obj_node->id = pinned_info.id; - obj_node->path =strdup (ftse->fts_path); -hash_add (tab->table, &obj_node->hash, obj_node->id); - -close (fd); - } -fts_close (fts); +if (nftw (path, do_build_table_cb, nopenfd, flags) ==-1 ) +break ; }fclose (mntfile);return 0 ;
tools/bpf/bpftool/main.h +2 -2
@@ -152,8 +152,8 @@ int cmd_select(const struct cmd *cmds, int argc, char **argv,int get_fd_type (int fd);const char *get_fd_type_name (enum bpf_obj_type type);char *get_fdinfo (int fd,const char *key); -int open_obj_pinned (char *path,bool quiet); -int open_obj_pinned_any (char *path,enum bpf_obj_type exp_type); +int open_obj_pinned (const char *path,bool quiet); +int open_obj_pinned_any (const char *path,enum bpf_obj_type exp_type);int mount_bpffs_for_pin (const char *name);int do_pin_any (int argc,char **argv,int (*get_fd_by_id)(int *,char ***));int do_pin_fd (int fd,const char *name);
Signature · 1 line
2.17.1
2020-07-15 22:29 UTC-0700 ~ Tony Ambardar <[email protected]>
Quoted 11 lines
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. Clean up related function warnings by using "const char *" arguments and fixing an unsafe call to
dirname().These changes help in building bpftool against musl for OpenWrt.
Could you please add a line to the log about the reason for the path copy in open_obj_pinned()?
Quoted 34 lines
Signed-off-by Tony Ambardar <[email protected]>---
V2:
* use_GNU_SOURCEto pull ingetpagesize(),getline(),nftw()definitions
* use "const char *" inopen_obj_pinned()andopen_obj_pinned_any()
* makedirname()safely act on a string copy--- tools/bpf/bpftool/common.c | 129 +++++++++++++++++++++---------------- tools/bpf/bpftool/main.h | 4 +- 2 files changed, 76 insertions(+), 57 deletions(-)tools/bpf/bpftool/common.c +2
-1@@ -1,10 +1,11 @@// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ +#define _GNU_SOURCE#include <ctype.h> #include <errno.h> #include <fcntl.h> -#include <fts.h> +#include <ftw.h> #include <libgen.h> #include <mntent.h> #include <stdbool.h> @@ -160,24 +161,36 @@ int mount_tracefs(const char *target)return err; }
tools/bpf/bpftool/common.c int mount_tracefs(const char *target) · line 170
Quoted 10 lines
tools/bpf/bpftool/common.c +6
-2-int open_obj_pinned (char *path,bool quiet) +int open_obj_pinned (const char *path,bool quiet) { -int fd; +char *pname; +int fd =-1 ; + + pname =strdup (path); +if (pname ==NULL ) {
Simply "if (!pname) {"
tools/bpf/bpftool/common.c int mount_tracefs(const char *target) · line 172
Quoted 2 lines
tools/bpf/bpftool/common.c +2
-0+if (!quiet) +p_err ("bpf obj get (%s): %s" , path,strerror (errno));
Please update the error message, this one was for a failure on bpf_obj_get().
tools/bpf/bpftool/common.c int mount_tracefs(const char *target) · line 175
Quoted 3 lines
tools/bpf/bpftool/common.c +3
-0+ goto out_ret; + } +
You're adding a second blank line here, please fix.
Quoted 27 lines
tools/bpf/bpftool/common.c +8
-5- fd =bpf_obj_get (path); + fd =bpf_obj_get (pname);if (fd <0 ) {if (!quiet) -p_err ("bpf obj get (%s): %s" , path, - errno ==EACCES && !is_bpffs (dirname (path)) ? +p_err ("bpf obj get (%s): %s" , pname, + errno ==EACCES && !is_bpffs (dirname (pname)) ?"directory not in bpf file system (bpffs)" :strerror (errno)); -return -1 ; + goto out_free; } +out_free: +free (pname); +out_ret:return fd; } -int open_obj_pinned_any (char *path,enum bpf_obj_type exp_type) +int open_obj_pinned_any (const char *path,enum bpf_obj_type exp_type) {enum bpf_obj_type type;int fd;
tools/bpf/bpftool/common.c void print_hex_data_json(uint8_t *data, size_t len) · line 384
Quoted 7 lines
tools/bpf/bpftool/common.c +2
-2@@ -367,68 +380,74 @@ void print_hex_data_json(uint8_t *data, size_t len)jsonw_end_array (json_wtr); } -int build_pinned_obj_table (struct pinned_obj_table *tab, -enum bpf_obj_type type) +static struct pinned_obj_table *build_fn_table;/* params for nftw cb*/ +static enum bpf_obj_type build_fn_type;
I would move the comments above the lines, since it applies to both of them.
tools/bpf/bpftool/common.c void print_hex_data_json(uint8_t *data, size_t len) · line 387
Quoted 3 lines
tools/bpf/bpftool/common.c +3
-0+ +static int do_build_table_cb (const char *fpath,const struct stat *sb, +int typeflag,struct FTW *ftwbuf)
Please align the second line on the open parenthesis.
tools/bpf/bpftool/common.c void print_hex_data_json(uint8_t *data, size_t len) · line 389
Quoted 2 lines
tools/bpf/bpftool/common.c +0
-0{struct bpf_prog_info pinned_info = {};
A few suggestions on this code, even though I realise you simply moved some parts. We can skip zero-initialising here (" = {}") because we memset() it below before using it anyway.
Quoted 9 lines
tools/bpf/bpftool/common.c +5
-1struct pinned_obj *obj_node =NULL ;__u32 len =sizeof (pinned_info); -struct mntent *mntent =NULL ;enum bpf_obj_type objtype; +int fd, err =0 ; + +if (typeflag !=FTW_F ) + goto out_ret; + fd =open_obj_pinned (fpath, true);
tools/bpf/bpftool/common.c void print_hex_data_json(uint8_t *data, size_t len) · line 407
Quoted 10 lines
tools/bpf/bpftool/common.c +10
-0+if (fd <0 ) + goto out_ret; + + objtype =get_fd_type (fd); +if (objtype != build_fn_type) + goto out_close; + +memset (&pinned_info,0 ,sizeof (pinned_info)); +if (bpf_obj_get_info_by_fd (fd, &pinned_info, &len)) { +p_err ("can't get obj info: %s" ,strerror (errno));
We are simply building a table here to show the paths where objects are pinned when listing progs/maps/etc., and I don't believe we want to print an error in that case. And with such a message I would expect the function to return and bpftool to stop, but again I don't believe this is necessary here and we can just go on listing objects, even if we fail to show their pinned paths.
tools/bpf/bpftool/common.c void print_hex_data_json(uint8_t *data, size_t len) · line 413
Quoted 6 lines
tools/bpf/bpftool/common.c +6
-0+ goto out_close; + } + + obj_node =malloc (sizeof (*obj_node)); +if (!obj_node) { +p_err ("mem alloc failed" );
Same here, let's not add an error message.
tools/bpf/bpftool/common.c void print_hex_data_json(uint8_t *data, size_t len) · line 418
Quoted 5 lines
tools/bpf/bpftool/common.c +5
-0+ err =-1 ; + goto out_close; + } + +memset (obj_node,0 ,sizeof (*obj_node));
Instead of malloc() + memset(), we could simply use calloc().
tools/bpf/bpftool/common.c void print_hex_data_json(uint8_t *data, size_t len) · line 431
Quoted 13 lines
tools/bpf/bpftool/common.c +13
-0+ obj_node->id = pinned_info.id; + obj_node->path =strdup (fpath); +hash_add (build_fn_table->table, &obj_node->hash, obj_node->id); + +out_close: +close (fd); +out_ret: +return err; +} + +int build_pinned_obj_table (struct pinned_obj_table *tab, +enum bpf_obj_type type) +{
The end looks good.
Apart from the minor points mentioned above, the patch is good, copying the string in open_obj_pinned() looks like the right approach. It does compile (with no warnings) and works as intended on my machine :).
Thanks,
Quentin
Quoted 8 lines · Quentin Monnet
2020-07-15 22:29 UTC-0700 ~ Tony Ambardar <[email protected]>
Quoted 6 lines
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.
Quoted 8 lines
Quoted 5 lines
Standardize code usage by rewriting one fts-based function to use nftw. Clean up related function warnings by using "const char *" arguments and fixing an unsafe call to
dirname().These changes help in building bpftool against musl for OpenWrt.
Could you please add a line to the log about the reason for the path copy in
open_obj_pinned()?
Good point, will do.
Quoted 136 lines
Quoted 34 lines
Signed-off-by Tony Ambardar <[email protected]>---
V2:
* use_GNU_SOURCEto pull ingetpagesize(),getline(),nftw()definitions
* use "const char *" inopen_obj_pinned()andopen_obj_pinned_any()
* makedirname()safely act on a string copy--- tools/bpf/bpftool/common.c | 129 +++++++++++++++++++++---------------- tools/bpf/bpftool/main.h | 4 +- 2 files changed, 76 insertions(+), 57 deletions(-)tools/bpf/bpftool/common.c +2
-1@@ -1,10 +1,11 @@// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) /* Copyright (C) 2017-2018 Netronome Systems, Inc. */ +#define _GNU_SOURCE#include <ctype.h> #include <errno.h> #include <fcntl.h> -#include <fts.h> +#include <ftw.h> #include <libgen.h> #include <mntent.h> #include <stdbool.h> @@ -160,24 +161,36 @@ int mount_tracefs(const char *target)return err; }tools/bpf/bpftool/common.c int mount_tracefs(const char *target) · line 170
Quoted 10 lines
tools/bpf/bpftool/common.c +6
-2-int open_obj_pinned (char *path,bool quiet) +int open_obj_pinned (const char *path,bool quiet) { -int fd; +char *pname; +int fd =-1 ; + + pname =strdup (path); +if (pname ==NULL ) {Simply "if (!pname) {"
tools/bpf/bpftool/common.c int mount_tracefs(const char *target) · line 172
Quoted 2 lines
tools/bpf/bpftool/common.c +2
-0+if (!quiet) +p_err ("bpf obj get (%s): %s" , path,strerror (errno));Please update the error message, this one was for a failure on
bpf_obj_get().tools/bpf/bpftool/common.c int mount_tracefs(const char *target) · line 175
Quoted 3 lines
tools/bpf/bpftool/common.c +3
-0+ goto out_ret; + } +You're adding a second blank line here, please fix.
Quoted 27 lines
tools/bpf/bpftool/common.c +8
-5- fd =bpf_obj_get (path); + fd =bpf_obj_get (pname);if (fd <0 ) {if (!quiet) -p_err ("bpf obj get (%s): %s" , path, - errno ==EACCES && !is_bpffs (dirname (path)) ? +p_err ("bpf obj get (%s): %s" , pname, + errno ==EACCES && !is_bpffs (dirname (pname)) ?"directory not in bpf file system (bpffs)" :strerror (errno)); -return -1 ; + goto out_free; } +out_free: +free (pname); +out_ret:return fd; } -int open_obj_pinned_any (char *path,enum bpf_obj_type exp_type) +int open_obj_pinned_any (const char *path,enum bpf_obj_type exp_type) {enum bpf_obj_type type;int fd;tools/bpf/bpftool/common.c void print_hex_data_json(uint8_t *data, size_t len) · line 384
Quoted 7 lines
tools/bpf/bpftool/common.c +2
-2@@ -367,68 +380,74 @@ void print_hex_data_json(uint8_t *data, size_t len)jsonw_end_array (json_wtr); } -int build_pinned_obj_table (struct pinned_obj_table *tab, -enum bpf_obj_type type) +static struct pinned_obj_table *build_fn_table;/* params for nftw cb*/ +static enum bpf_obj_type build_fn_type;I would move the comments above the lines, since it applies to both of them.
tools/bpf/bpftool/common.c void print_hex_data_json(uint8_t *data, size_t len) · line 387
Quoted 3 lines
tools/bpf/bpftool/common.c +3
-0+ +static int do_build_table_cb (const char *fpath,const struct stat *sb, +int typeflag,struct FTW *ftwbuf)Please align the second line on the open parenthesis.
tools/bpf/bpftool/common.c void print_hex_data_json(uint8_t *data, size_t len) · line 389
Quoted 2 lines
tools/bpf/bpftool/common.c +0
-0{struct bpf_prog_info pinned_info = {};A few suggestions on this code, even though I realise you simply moved some parts. We can skip zero-initialising here (" = {}") because we
memset()it below before using it anyway.Quoted 9 lines
tools/bpf/bpftool/common.c +5
-1struct pinned_obj *obj_node =NULL ;__u32 len =sizeof (pinned_info); -struct mntent *mntent =NULL ;enum bpf_obj_type objtype; +int fd, err =0 ; + +if (typeflag !=FTW_F ) + goto out_ret; + fd =open_obj_pinned (fpath, true);tools/bpf/bpftool/common.c void print_hex_data_json(uint8_t *data, size_t len) · line 407
Quoted 10 lines
tools/bpf/bpftool/common.c +10
-0+if (fd <0 ) + goto out_ret; + + objtype =get_fd_type (fd); +if (objtype != build_fn_type) + goto out_close; + +memset (&pinned_info,0 ,sizeof (pinned_info)); +if (bpf_obj_get_info_by_fd (fd, &pinned_info, &len)) { +p_err ("can't get obj info: %s" ,strerror (errno));We are simply building a table here to show the paths where objects are pinned when listing progs/maps/etc., and I don't believe we want to print an error in that case. And with such a message I would expect the function to return and bpftool to stop, but again I don't believe this is necessary here and we can just go on listing objects, even if we fail to show their pinned paths.
Quoted 8 lines
tools/bpf/bpftool/common.c void print_hex_data_json(uint8_t *data, size_t len) · line 413
Quoted 6 lines
tools/bpf/bpftool/common.c +6
-0+ goto out_close; + } + + obj_node =malloc (sizeof (*obj_node)); +if (!obj_node) { +p_err ("mem alloc failed" );Same here, let's not add an error message.
You're right. I just realized those aren't pre-existing messages; I added them but then forgot.
Quoted 10 lines
tools/bpf/bpftool/common.c void print_hex_data_json(uint8_t *data, size_t len) · line 418
Quoted 5 lines
tools/bpf/bpftool/common.c +5
-0+ err =-1 ; + goto out_close; + } + +memset (obj_node,0 ,sizeof (*obj_node));Instead of
malloc()+memset(), we could simply usecalloc().Quoted 2 lines
tools/bpf/bpftool/common.c +2
-0+ obj_node->id = pinned_info.id; + obj_node->path =strdup (fpath);
I should check for failed allocation here too.
Quoted 14 lines
tools/bpf/bpftool/common.c void print_hex_data_json(uint8_t *data, size_t len) · line 431
Quoted 11 lines
tools/bpf/bpftool/common.c +11
-0+hash_add (build_fn_table->table, &obj_node->hash, obj_node->id); + +out_close: +close (fd); +out_ret: +return err; +} + +int build_pinned_obj_table (struct pinned_obj_table *tab, +enum bpf_obj_type type) +{The end looks good.
Quoted 3 lines
Apart from the minor points mentioned above, the patch is good, copying the string in
open_obj_pinned()looks like the right approach. It does compile (with no warnings) and works as intended on my machine :).
Glad to hear it! I'll update the patch based on your comments above and resend shortly. Thanks again for the testing and feedback.
Regards,
Tony
Quoted 2 lines
Thanks,
Quentin