window: implement CWindow::getEnv() for BSDs (#12462)

Some BSDs provide procfs to access kernel information. However, BSDs'
procfs does not provide information on a process' environment
variables. Instead sysctl(3) function is usually used for system
information retrieval on BSDs.
This commit is contained in:
Hiroki Tagato 2025-11-27 07:12:50 +09:00 committed by GitHub
parent 4036e35e73
commit 379ee99c68
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,6 +3,11 @@
#include <hyprutils/animation/AnimatedVariable.hpp>
#include <re2/re2.h>
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
#include <any>
#include <bit>
#include <string_view>
@ -1203,6 +1208,10 @@ std::unordered_map<std::string, std::string> CWindow::getEnv() {
std::unordered_map<std::string, std::string> results;
std::vector<char> buffer;
size_t needle = 0;
#if defined(__linux__)
//
std::string environFile = "/proc/" + std::to_string(PID) + "/environ";
std::ifstream ifs(environFile, std::ios::binary);
@ -1210,13 +1219,25 @@ std::unordered_map<std::string, std::string> CWindow::getEnv() {
if (!ifs.good())
return {};
std::vector<char> buffer;
size_t needle = 0;
buffer.resize(512, '\0');
while (ifs.read(buffer.data() + needle, 512)) {
buffer.resize(buffer.size() + 512, '\0');
needle += 512;
}
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ENV, static_cast<int>(PID)};
size_t len = 0;
if (sysctl(mib, 4, nullptr, &len, nullptr, 0) < 0 || len == 0)
return {};
buffer.resize(len, '\0');
if (sysctl(mib, 4, buffer.data(), &len, nullptr, 0) < 0)
return {};
needle = len;
#endif
if (needle <= 1)
return {};