From 3b96b51cf493353c59fede5d4c9a6af4265c0d56 Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Tue, 23 Jul 2024 02:26:41 +0000 Subject: [PATCH] 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 --- src/libutil/current-process.cc | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libutil/current-process.cc b/src/libutil/current-process.cc index 3c037c33f..33cda211b 100644 --- a/src/libutil/current-process.cc +++ b/src/libutil/current-process.cc @@ -15,6 +15,11 @@ # include #endif +#if __FreeBSD__ +# include +# include +#endif + #include #include @@ -102,6 +107,24 @@ std::optional 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 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