Skip to content

Commit c024780

Browse files
guidosarduccianakryiko
authored andcommitted
selftests/bpf: Use portable POSIX basename()
Use the POSIX version of basename() to allow compilation against non-gnu libc (e.g. musl). Include <libgen.h> ahead of <string.h> to enable using functions from the latter while preferring POSIX over GNU basename(). In veristat.c, rely on strdupa() to avoid basename() altering the passed "const char" argument. This is not needed in xskxceiver.c since the arg is mutable and the program exits immediately after usage. Signed-off-by: Tony Ambardar <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/0fd3c9f3c605e6cba33504213c9df287817ade04.1722244708.git.tony.ambardar@gmail.com
1 parent 781f0bb commit c024780

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

tools/testing/selftests/bpf/veristat.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
33
#define _GNU_SOURCE
44
#include <argp.h>
5+
#include <libgen.h>
56
#include <string.h>
67
#include <stdlib.h>
78
#include <sched.h>
@@ -988,8 +989,8 @@ static void fixup_obj(struct bpf_object *obj, struct bpf_program *prog, const ch
988989

989990
static int process_prog(const char *filename, struct bpf_object *obj, struct bpf_program *prog)
990991
{
992+
const char *base_filename = basename(strdupa(filename));
991993
const char *prog_name = bpf_program__name(prog);
992-
const char *base_filename = basename(filename);
993994
char *buf;
994995
int buf_sz, log_level;
995996
struct verif_stats *stats;
@@ -1056,13 +1057,14 @@ static int process_prog(const char *filename, struct bpf_object *obj, struct bpf
10561057

10571058
static int process_obj(const char *filename)
10581059
{
1060+
const char *base_filename = basename(strdupa(filename));
10591061
struct bpf_object *obj = NULL, *tobj;
10601062
struct bpf_program *prog, *tprog, *lprog;
10611063
libbpf_print_fn_t old_libbpf_print_fn;
10621064
LIBBPF_OPTS(bpf_object_open_opts, opts);
10631065
int err = 0, prog_cnt = 0;
10641066

1065-
if (!should_process_file_prog(basename(filename), NULL)) {
1067+
if (!should_process_file_prog(base_filename, NULL)) {
10661068
if (env.verbose)
10671069
printf("Skipping '%s' due to filters...\n", filename);
10681070
env.files_skipped++;
@@ -1076,7 +1078,7 @@ static int process_obj(const char *filename)
10761078
}
10771079

10781080
if (!env.quiet && env.out_fmt == RESFMT_TABLE)
1079-
printf("Processing '%s'...\n", basename(filename));
1081+
printf("Processing '%s'...\n", base_filename);
10801082

10811083
old_libbpf_print_fn = libbpf_set_print(libbpf_print_fn);
10821084
obj = bpf_object__open_file(filename, &opts);

tools/testing/selftests/bpf/xskxceiver.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
#include <signal.h>
9191
#include <stdio.h>
9292
#include <stdlib.h>
93+
#include <libgen.h>
9394
#include <string.h>
9495
#include <stddef.h>
9596
#include <sys/mman.h>

0 commit comments

Comments
 (0)