libutil: Support getSelfExe on FreeBSD

getSelfExe is used in a few places re-execute nix.
Current code in this file uses ifdefs to support several
platforms, just keep doing that

Change-Id: Iecc2ada0101aea0c30524e3a1218594f919d74bf
This commit is contained in:
Artemis Tosini 2024-07-23 02:26:41 +00:00
parent 53f3e39815
commit 3b96b51cf4
Signed by untrusted user: artemist
GPG key ID: EE5227935FE3FF18

View file

@ -15,6 +15,11 @@
# include <sys/resource.h>
#endif
#if __FreeBSD__
# include <sys/param.h>
# include <sys/sysctl.h>
#endif
#include <sys/mount.h>
#include <cgroup.hh>
@ -102,6 +107,24 @@ std::optional<Path> getSelfExe()
return buf;
else
return std::nullopt;
#elif __FreeBSD__
int sysctlName[] = {
CTL_KERN,
KERN_PROC,
KERN_PROC_PATHNAME,
-1,
};
size_t pathLen = 0;
if (sysctl(sysctlName, sizeof(sysctlName) / sizeof(sysctlName[0]), nullptr, &pathLen, nullptr, 0) < 0) {
return std::nullopt;
}
std::vector<char> path(pathLen);
if (sysctl(sysctlName, sizeof(sysctlName) / sizeof(sysctlName[0]), path.data(), &pathLen, nullptr, 0) < 0) {
return std::nullopt;
}
return Path(path.begin(), path.end());
#else
return std::nullopt;
#endif