🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: muc.lists.netbsd.tech.userlevel
7 messages
7 total messages Started by Brad Smith Sun, 25 Oct 2026 08:20
Re: Proposal: getexecpath(3)
#35
Author: Brad Smith
Date: Sun, 25 Oct 2026 08:20
70 lines
4973 bytes
On Mon, Dec 16, 2024, Taylor R Campbell wrote:
 > PROPOSAL: New libc function
 >
 >     const char *getexecpath(void);
 >
 > returns the pathname that was passed to execve(2), unmodified.

I know this is not quite what you are looking for, but FYI, OpenBSD
has since added a getexecpath(3) to libc, first appearing in
OpenBSD 8.0:

   https://man.openbsd.org/getexecpath.3

Unlike NetBSD, OpenBSD did not even have a sysctl like
KERN_PROC_PATHNAME to fall back on, so software was left guessing
from argv[0]. That said, after looking through many open source
projects, I see a lot of them have not been updated to use NetBSD's
sysctl either.

   #include <unistd.h>

   int
   getexecpath(char *buf, size_t bufsize);

It copies the absolute pathname of the executable into buf,
canonicalized the same way as realpath(3), and returns 0 on success
or -1 with errno set on failure (e.g. the executable was started in
a directory no longer linked into the filesystem, or the buffer is
too small; PATH_MAX is the recommended size).

The path is computed by the kernel in execve(2) and placed on the
new process's stack as an ELF auxiliary vector entry, which libc
picks up at startup. The relevant commits:

https://github.com/openbsd/src/commit/4dde6567f895c6faa36acbacd4cdf62954a70399
     In execve(2), attempt to create a realpath buffer for the
     executable and pass it to userland as an auxval

https://github.com/openbsd/src/commit/026846f111232f45cd1f4883414c190c8ff722a7
     Add getexecpath(3) to libc

https://github.com/openbsd/src/commit/1d46e5b94d0221c062218de40c6d15f9f5cacb31
     Make getexecpath() return the ELF executable that is running
     (for a script run via a shebang line, the canonicalized path of
     the interpreter actually executed)

So the name is the same, but both the signature and the semantics
differ from what is proposed here: a canonical absolute path rather
than the verbatim execve(2) argument. If NetBSD goes ahead with
this, it would be good to either match OpenBSD's interface or pick a
different name. Portable software is already starting to probe for
getexecpath() (HAVE_GETEXECPATH checks in configure/meson/CMake), and
two incompatible functions under the same name would be an
unpleasant trap.

On the consumer side, I have been adding getexecpath() support for
OpenBSD to quite a few common open source packages, Racket among
them, which uses the same kind of self-path logic that tripped up
lang/racket in your postscript. A compatible interface on NetBSD
would let the same code paths be reused.


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Re: Proposal: getexecpath(3)
#41
Author: Taylor R Campbell
Date: Mon, 26 Oct 2026 07:29
37 lines
3522 bytes
> Date: Sun, 25 Oct 2026 04:20:57 -0400
> From: Brad Smith <brad@comstyle.com>
> 
> So the name is the same, but both the signature and the semantics
> differ from what is proposed here: a canonical absolute path rather
> than the verbatim execve(2) argument. If NetBSD goes ahead with
> this, it would be good to either match OpenBSD's interface or pick a
> different name.

Why do you want the kernel to compute some notion of a canonical
absolute path?  Of what use is this for applications?  Isn't this
strictly less useful than the original verbatim exec path?

You can always pass the original verbatim exec path to realpath(3) if
you want to canonicalize it, but you can't undo realpath(3) (or the
kernel's equivalent) if the program wants to use the same symlinks for
to (dl)open supporting data and libraries as the caller used to
execute the program.

I think it is a mistake for the kernel to expose anything other than
the original verbatim exec path.

(I don't have strong feelings about char *getexecpath(void) vs void
getexecpath(char *, size_t); the first one is a little simpler, but
whatever.)

> Portable software is already starting to probe for getexecpath()
> (HAVE_GETEXECPATH checks in configure/meson/CMake), and two
> incompatible functions under the same name would be an unpleasant
> trap.

Sounds like the `portable' software needs #ifdef __OpenBSD__, since
API this isn't portable to any platform except OpenBSD anyway?

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Re: Proposal: getexecpath(3)
#42
Author: Mouse
Date: Mon, 26 Oct 2026 11:04
25 lines
2427 bytes
> Why do you want the kernel to compute some notion of a canonical
> absolute path?  Of what use is this for applications?  Isn't this
> strictly less useful than the original verbatim exec path?

It also can leak information.  If the path passed to execve is relative
and one of the containing directories is inaccessible to the process, a
kernel-created absolute path will leak a pathname the program shouldn't
be able to obtain.

> You can always pass the original verbatim exec path to realpath(3) if
> you want to canonicalize it,

True, though that - at best - returns the hard path at the time of
realpath (or more precisely with pieces fetched at various times
scattered throughout the execution of realpath), which may not be the
same as the one at the time of exec.

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse@rodents-montreal.org
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Re: Proposal: getexecpath(3)
#43
Author: Taylor R Campbell
Date: Mon, 26 Oct 2026 12:17
22 lines
2865 bytes
> Date: Mon, 26 Oct 2026 07:04:08 -0400 (EDT)
> From: Mouse <mouse@Rodents-Montreal.ORG>
> 
> > You can always pass the original verbatim exec path to realpath(3) if
> > you want to canonicalize it,
> 
> True, though that - at best - returns the hard path at the time of
> realpath (or more precisely with pieces fetched at various times
> scattered throughout the execution of realpath), which may not be the
> same as the one at the time of exec.

If the path resolution may have concurrently changed between the
execve call and the realpath call, it may as well have concurrently
changed between the execve call and the open call.

In other words, this race condition is inherent to the nature of
passing a path around; it cannot be avoided by asking the kernel to do
(the equivalent of) realpath.

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Re: Proposal: getexecpath(3)
#44
Author: Mouse
Date: Mon, 26 Oct 2026 23:12
27 lines
2675 bytes
>>> You can always pass the original verbatim exec path to realpath(3)
>>> if you want to canonicalize it,
>> True, though that - at best - returns the hard path at the time of
>> realpath (or more precisely with pieces fetched at various times
>> scattered throughout the execution of realpath), which may not be
>> the same as the one at the time of exec.
> If the path resolution may have concurrently changed between the
> execve call and the realpath call, it may as well have concurrently
> changed between the execve call and the open call.

Good point.  Since (at least as far as I recall) we don't know why
anyone (putatively) wants the original path, we don't know whether this
matters.  But for most likely purposes I would expect it would.

Maybe what this wants is a way to flag the executable such that it
automatically gets something like a descriptor open on the directory
containing the executable?  I'm not sure that's quite the right
semantic, but maybe it's close enough to be a useful starting point.

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse@rodents-montreal.org
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Re: Proposal: getexecpath(3)
#45
Author: Jason Thorpe
Date: Tue, 27 Oct 2026 12:59
19 lines
4928 bytes
> On Oct 26, 2026, at 7:12 PM, Mouse <mouse@rodents-montreal.org> wrote:
> 
> Good point.  Since (at least as far as I recall) we don't know why
> anyone (putatively) wants the original path, we don't know whether this
> matters.  But for most likely purposes I would expect it would.

It’s useful for having “relocatable” (in the file system) applications, so that a program could find its resources without having to bake in path prefixes as compile time.

-- thorpej


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Re: Proposal: getexecpath(3)
#67
Author: David Holland
Date: Sun, 01 Nov 2026 19:34
39 lines
3641 bytes
On Mon, Oct 26, 2026 at 12:17:00PM +0000, Taylor R Campbell wrote:
 > > > You can always pass the original verbatim exec path to realpath(3) if
 > > > you want to canonicalize it,
 > > 
 > > True, though that - at best - returns the hard path at the time of
 > > realpath (or more precisely with pieces fetched at various times
 > > scattered throughout the execution of realpath), which may not be the
 > > same as the one at the time of exec.
 > 
 > If the path resolution may have concurrently changed between the
 > execve call and the realpath call, it may as well have concurrently
 > changed between the execve call and the open call.
 > 
 > In other words, this race condition is inherent to the nature of
 > passing a path around; it cannot be avoided by asking the kernel to do
 > (the equivalent of) realpath.

If the realpathing is done as part of namei, then there's only one
lookup sequence and the resulting realpath points to the same file as
the program gets loaded from, as of the time the lookup in exec is
completed. Mostly. If it's an absolute path. Relative paths create
further issues.

Nothing prevents elements of that path from changing later before it's
looked up again. But I suspect the intent is to canonicalize it into
something the invoking user has no write access to.

Note that our namei does this and what gets dumped into the ELF
auxiliary vector has been handled this way. I don't know if OpenBSD
merged my code or reimplemented it (and if they reimplemented it,
whether they got it right) but that's their problem :-)

-- 
David A. Holland
dholland@netbsd.org

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads