Skip to content

Get OpenBSD executable path #9086

Closed
Closed

Description

The code in master doesn't allow for this, in OpenBSD to get the executable path, and mounting the procfs is no longer possible.

Feel free to add me under people assigned.

I just want to know in advance if this code will be accepted or if it's too hacky to be taken seriously:
https://github.com/time-killer-games/libfilesystem/blob/2f773e866f99f7ea2228f970dbf43ed3128df821/filesystem.cpp#L541-L587

Of course, I will rewrite this code to be C compliant once the pr will be opened. OpenBSD has no official API for getting the current exe path, and I don't know if it ever did, it very well may have when the procfs was available, but i wasn't an OpenBSD user when that was still available to the OS. This is pretty much the best we can do without an official API.

Activity

  1. faho commented on Jul 24, 2022

    @faho
    Member

    You'll want to look through our codebase, because we do of course have helpers to find an executable in $PATH, or to add $PWD to a path.

    That should make this quite a bit simpler.

  2. added this to the fish-future milestone on Jul 24, 2022
  3. faho commented on Jul 24, 2022

    @faho
    Member

    Tbh I hate the complexities of that code, the hoops we'd have to jump through.

    Because it relies on $PATH and the working directory, you need to jump through hoops to get the original values of those. But even that isn't actually guaranteed to be correct!

    Because what you need is the $PATH and cwd of fish's parent process, at the time it started fish. And that is iff it decided to not play games with argv[0] - like call us as -fish.

    So I'm tempted to just declare that we'll be printing "fish" on OpenBSD until they grow a feature that allows us to actually be correct.

  4. floam commented on Jul 25, 2022

    @floam
    Member

    I'm surprised the OpenBSD kernel developers do not provide a way to do this, considering their security stance and repute.

  5. floam commented on Jul 25, 2022

    @floam
    Member

    I read this a bit: ziglang/zig#6718

    Honestly IMO if we need to do any more work than take advantage of the platform-specific API to get the executable path, I don't think we should go so far as trying to derive it by heroic means.

    I admittedly don't fully understand where the problem is.

  6. faho commented on Jul 25, 2022

    @faho
    Member

    Another option instead of using argv[0] would be to use getprogname() although that would break with a relative path to fish in some cases. Not many people would use a relative path to fish, most people would would run based on the filename spec without the directory spec. Thus, the using $PATH + getprogname might be good enough.

    It would not. In fact that would barely be better than what we have now.

    the compare a few stat structure members with the stat structure returned by opening the path the function guessed points to the current executable

    Please don't. That just adds more guesswork and complication, for unclear gain.


    Here's what we can do:

    1. We can resolve the executable path once, at startup
    2. If we do that at startup, we can check for it in $PWD and/or $PATH at that time - just do it before we read our config and it's fine
    3. If it doesn't resolve, return "fish" so people using status fish-path could conceivably find fish via $PATH at that time
    4. (optional) As one special case, handle -fish like fish.
  7. floam commented on Jul 25, 2022

    @floam
    Member

    I'm like a few days late on this, but I'm happy with how the NetBSD one went. It just needed different sysctl parameters to get the executable path for the current process. My understanding was there was some discussion of trying to do a normalization step to remove redundancies like symlinks, or '..' appearing in the middle - and this was (IMO) correctly abandoned because we still got an actual path to the executable. What is the specific issue on OpenBSD?

    Sorry for asking this all dumbed up but I tried to check the commits at one point a couple days ago on one of these and the branch was deleted, and I'm seeing these links to other projects - please sum it up.

  8. floam commented on Jul 25, 2022

    @floam
    Member

    I'm probably going to need to install OpenBSD again to figure this out then, because I just don't know what you mean. How do PWD, PATH, argv[0] relate to what the kernel can tell us?

  9. floam commented on Jul 25, 2022

    @floam
    Member

    We can get perhaps the parent directory or something?

  10. 12 remaining items

  11. added this to the fish-future milestone on Jul 25, 2022
  12. faho commented on Jul 26, 2022

    @faho
    Member

    I read the docs for none() and it only returns true of all bits are zero.

    "none()" is part of our maybe_t implementation - basically a backport of std::optional.

    In essence you'd use it like

    auto path = path_get_path(wargv[0], nullptr);
    if (path) {
        wcstring actual_path = *path;
    }

    This is because maybe_t overloads operator bool(), so you can use it like it's a null pointer - with the exception that maybe_t<wcstring> doesn't work in place of a wcstring like a NULL would in place of a wchar_t*.

  13. faho commented on Jul 27, 2022

    @faho
    Member

    In terms of complexity that seems acceptable. If it works, we can use it.

  14. brad0 commented on Sep 4, 2026

    @brad0

    Just FYI OpenBSD now has getexecpath() to deal with this.

  15. Darkshadow2 commented on Sep 4, 2026

    @Darkshadow2
    Contributor

    According to the HISTORY section on the manpage getexecpath is being added to OpenBSD 8.0, which isn't out yet

  16. brad0 commented on Sep 13, 2026

    @brad0

    Snapshots are available. 8.0 final will be out in a few weeks.

  17. added a commit that references this issue on Sep 14, 2026
    25cb92d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions