All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jiri Olsa <olsajiri@gmail.com>
To: Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com>
Cc: Jiri Olsa <olsajiri@gmail.com>,
	Quentin Monnet <quentin@isovalent.com>,
	Namhyung Kim <namhyung@kernel.org>,
	Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Andrii Nakryiko <andrii.nakryiko@gmail.com>,
	bpf@vger.kernel.org
Subject: Re: [PATCH] bpftool: Add missing libgen.h for basename()
Date: Sat, 6 Jan 2024 21:02:26 +0100	[thread overview]
Message-ID: <ZZmx0rFDyIJxnPAi@krava> (raw)
In-Reply-To: <ZZhsPs00TI75RdAr@kernel.org>

On Fri, Jan 05, 2024 at 05:53:18PM -0300, Arnaldo Carvalho de Melo wrote:
> Em Fri, Jan 05, 2024 at 04:01:37PM +0100, Jiri Olsa escreveu:
> > On Fri, Jan 05, 2024 at 09:48:31AM +0100, Jiri Olsa wrote:
> > > On Thu, Jan 04, 2024 at 10:01:35AM -0300, Arnaldo Carvalho de Melo wrote:
> > > 
> > > SNIP
> > > 
> > > >    9    51.66 amazonlinux:2                 : Ok   gcc (GCC) 7.3.1 20180712 (Red Hat 7.3.1-17) , clang version 11.1.0 (Amazon Linux 2 11.1.0-1.amzn2.0.2) flex 2.5.37
> > > >   10    60.77 amazonlinux:2023              : Ok   gcc (GCC) 11.4.1 20230605 (Red Hat 11.4.1-2) , clang version 15.0.7 (Amazon Linux 15.0.7-3.amzn2023.0.1) flex 2.6.4
> > > >   11    61.29 amazonlinux:devel             : Ok   gcc (GCC) 11.3.1 20221121 (Red Hat 11.3.1-4) , clang version 15.0.6 (Amazon Linux 15.0.6-3.amzn2023.0.2) flex 2.6.4
> > > >   12    74.72 archlinux:base                : Ok   gcc (GCC) 13.2.1 20230801 , clang version 16.0.6 flex 2.6.4
> > > > 
> > > > / $ grep -B8 -A2 -w basename /usr/include/string.h
> > > > #ifdef _GNU_SOURCE
> > > > #define	strdupa(x)	strcpy(alloca(strlen(x)+1),x)
> > > > int strverscmp (const char *, const char *);
> > > > char *strchrnul(const char *, int);
> > > > char *strcasestr(const char *, const char *);
> > > > void *memrchr(const void *, int, size_t);
> > > > void *mempcpy(void *, const void *, size_t);
> > > > #ifndef __cplusplus
> > > > char *basename();
> > > > #endif
> > > > #endif
> > > > / $ cat /etc/os-release
> > > > NAME="Alpine Linux"
> > > > ID=alpine
> > > > VERSION_ID=3.19.0
> > > > PRETTY_NAME="Alpine Linux v3.19"
> > > > HOME_URL="https://alpinelinux.org/"
> > > > BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues"
> > > > / $
> > > > 
> > > > Weird, they had it and now removed the _GNU_SOURCE bits (edge is their
> > > > devel distro, like rawhide is for fedora, tumbleweed for opensuse, etc).
> > > 
> > > let's see, I asked them in here: https://gitlab.alpinelinux.org/alpine/aports/-/issues/15643
> > 
> > it got removed in musl libc recently:
> >   https://git.musl-libc.org/cgit/musl/commit/?id=725e17ed6dff4d0cd22487bb64470881e86a92e7
> > 
> > so perhaps switching to POSIX version of basename is the easiest way out?
> 
> I think so, in all of perf we use the POSIX one, strdup'ing the arg,
> etc.
> 
> Something like the patch below?
> 
> - Arnaldo
> 
> diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
> index ee3ce2b8000d75d2..a5cc5938c3d7951e 100644
> --- a/tools/bpf/bpftool/gen.c
> +++ b/tools/bpf/bpftool/gen.c
> @@ -7,6 +7,7 @@
>  #include <ctype.h>
>  #include <errno.h>
>  #include <fcntl.h>
> +#include <libgen.h>
>  #include <linux/err.h>
>  #include <stdbool.h>
>  #include <stdio.h>
> @@ -56,9 +57,10 @@ static bool str_has_suffix(const char *str, const char *suffix)
>  
>  static void get_obj_name(char *name, const char *file)
>  {
> -	/* Using basename() GNU version which doesn't modify arg. */
> -	strncpy(name, basename(file), MAX_OBJ_NAME_LEN - 1);
> -	name[MAX_OBJ_NAME_LEN - 1] = '\0';
> +	char file_copy[PATH_MAX];

ok, probably better then checking for strdup error

> +	/* Using basename() POSIX version to be more portable. */
> +	strncpy(file_copy, file, PATH_MAX - 1)[PATH_MAX - 1] = '\0';
> +	strncpy(name, basename(file_copy), MAX_OBJ_NAME_LEN - 1)[MAX_OBJ_NAME_LEN - 1] = '\0';

I've never used it like that.. had to zoom in twice ;-)
but extra line with that might be more readable

jirka

>  	if (str_has_suffix(name, ".o"))
>  		name[strlen(name) - 2] = '\0';
>  	sanitize_identifier(name);

  reply	other threads:[~2024-01-06 20:02 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-04  3:04 [PATCH] bpftool: Add missing libgen.h for basename() Arnaldo Carvalho de Melo
2024-01-04  9:33 ` Jiri Olsa
2024-01-04 12:27   ` Arnaldo Carvalho de Melo
2024-01-04 13:01     ` Arnaldo Carvalho de Melo
2024-01-05  8:48       ` Jiri Olsa
2024-01-05 15:01         ` Jiri Olsa
2024-01-05 20:53           ` Arnaldo Carvalho de Melo
2024-01-06 20:02             ` Jiri Olsa [this message]
2024-01-26 15:53             ` Arnaldo Carvalho de Melo
2024-01-29 10:07               ` Quentin Monnet
2024-01-29 11:23               ` Jiri Olsa
2024-01-12  2:52 ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZZmx0rFDyIJxnPAi@krava \
    --to=olsajiri@gmail.com \
    --cc=adrian.hunter@intel.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=arnaldo.melo@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=irogers@google.com \
    --cc=namhyung@kernel.org \
    --cc=quentin@isovalent.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.