All of lore.kernel.org
 help / color / mirror / Atom feed
From: Willy Tarreau <w@1wt.eu>
To: "Thomas Weißschuh" <linux@weissschuh.net>
Cc: Shuah Khan <shuah@kernel.org>,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 1/2] tools/nolibc: add support for program_invocation_{,short_}name
Date: Sat, 14 Mar 2026 05:59:06 +0100	[thread overview]
Message-ID: <abTrGufSlaVqbl18@1wt.eu> (raw)
In-Reply-To: <20260313-nolibc-err-h-v2-1-f4481053b07a@weissschuh.net>

Hi Thomas,

On Fri, Mar 13, 2026 at 09:26:28PM +0100, Thomas Weißschuh wrote:
> Add support for the GNU extensions 'program_invocation_name' and
> 'program_invocation_short_name'. These are useful to print error
> messages, which by convention include the program name.
> 
> As these are global variables which take up memory even if not used,
> similar to 'errno', gate them behind NOLIBC_IGNORE_ERRNO.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>  tools/include/nolibc/crt.h                   | 26 ++++++++++++++++++++++
>  tools/include/nolibc/errno.h                 |  2 ++
>  tools/testing/selftests/nolibc/nolibc-test.c | 33 ++++++++++++++++++++++++++++
>  3 files changed, 61 insertions(+)
> 
> diff --git a/tools/include/nolibc/crt.h b/tools/include/nolibc/crt.h
> index d9262998dae9..842f86e41f2f 100644
> --- a/tools/include/nolibc/crt.h
> +++ b/tools/include/nolibc/crt.h
> @@ -17,6 +17,7 @@ const unsigned long *_auxv __attribute__((weak));
>  void _start(void);
>  static void __stack_chk_init(void);
>  static void exit(int);
> +static char *strrchr(const char *s, int c);
>  
>  extern void (*const __preinit_array_start[])(int, char **, char**) __attribute__((weak));
>  extern void (*const __preinit_array_end[])(int, char **, char**) __attribute__((weak));
> @@ -27,6 +28,24 @@ extern void (*const __init_array_end[])(int, char **, char**) __attribute__((wea
>  extern void (*const __fini_array_start[])(void) __attribute__((weak));
>  extern void (*const __fini_array_end[])(void) __attribute__((weak));
>  
> +extern char *program_invocation_name __attribute__((weak));
> +extern char *program_invocation_short_name __attribute__((weak));
> +
> +static __inline__
> +char *__nolibc_program_invocation_short_name(char *long_name)
> +{
> +	char *short_name;
> +
> +	if (!long_name)
> +		return NULL;
> +
> +	short_name = strrchr(long_name, '/');
> +	if (!short_name || !short_name[0])
> +		return NULL;

Here it should return long_name, not NULL, since you want a valid name
to use later. I'm seeing it passed to strcmp() for example.

Also because of this, I'm wondering for the first test about !long_name.
Either we consider that it's not possible to have a NULL long_name and
we don't need to test for it, or we consider it is valid, and we should
return a non-null string (e.g. "") so that the rest of the program
survives it. Both approaches are fine to me, but IMHO testing for NULL
to return a NULL that will crash the program later instead of just now
is not very useful. Maybe returning the empty string is still slightly
better because it allows well-designed programs that check their argc
before using argv[0] to survive.

Cheers,
Willy

  reply	other threads:[~2026-03-14  4:59 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-13 20:26 [PATCH v2 0/2] tools/nolibc: add err.h Thomas Weißschuh
2026-03-13 20:26 ` [PATCH v2 1/2] tools/nolibc: add support for program_invocation_{,short_}name Thomas Weißschuh
2026-03-14  4:59   ` Willy Tarreau [this message]
2026-03-13 20:26 ` [PATCH v2 2/2] tools/nolibc: add err.h Thomas Weißschuh

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=abTrGufSlaVqbl18@1wt.eu \
    --to=w@1wt.eu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=shuah@kernel.org \
    /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.