diff options
author | Artemis Tosini <lix@artem.ist> | 2024-07-25 23:10:30 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@localhost> | 2024-07-25 23:10:30 +0000 |
commit | 60a48311e84c228e664a44c8d049ea3080879a40 (patch) | |
tree | da9d9a52c229b5ba57b66f7e4acf2fde568c1f0c /src/libutil | |
parent | c4c7cb7613a859faee3933a7f24f3e496e5548e9 (diff) | |
parent | 3b96b51cf493353c59fede5d4c9a6af4265c0d56 (diff) |
Merge "libutil: Support getSelfExe on FreeBSD" into main
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/current-process.cc | 23 |
1 files changed, 23 insertions, 0 deletions
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 <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 |