Tweaking the program name for <err.h> functions

Alejandro Colomar alx at kernel.org
Thu Mar 7 22:24:32 UTC 2024


Hi,

I'm interested in knowing if the <err.h> functions have a supported way
of tweaking the prefix they print.

GNU's similar <error.h> functions support changing

	extern char *program_invocation_name;

for that, as per the error(3) manual page:

     The  program  name  printed by error() is the value of the global
     variable   program_invocation_name(3).    program_invocation_name
     initially  has  the same value as main()’s argv[0].  The value of
     this variable can be modified to change the output of error().


I have experimented with err(3), and it seems you can tweak it by
modifying 'program_invocation_short_name', but I'd like to know if
that's supported and/or portable.  I guess that since the pointer is a
GNU extension, while the functions come from the BSDs, that's not
portable, and thus not supported.

Maybe it would be interesting to get the BSDs to support these pointers?

The rationale is that the user controls argv[0], which might be
problematic in some cases --think of setuid programs-- where a user
could write arbitrary text to fd 2, which might be opened as a
privileged file.

In such programs, you likely want to hardcode that prefix, with
something like

	program_invocation_short_name = "su";

Have a lovely day!
Alex

-- 
<https://www.alejandro-colomar.es/>
Looking for a remote C programming job at the moment.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://lists.freedesktop.org/archives/libbsd/attachments/20240307/087b5397/attachment.sig>


More information about the libbsd mailing list