summaryrefslogtreecommitdiff
path: root/src/desktop/Window.cpp
diff options
context:
space:
mode:
authorHiroki Tagato <tagattie@gmail.com>2025-11-27 07:12:50 +0900
committerGitHub <noreply@github.com>2025-11-26 22:12:50 +0000
commit379ee99c681d45626604ad0253527438960ed374 (patch)
tree6de311e55cca24a92d1f1cf4380359511cc4f7cb /src/desktop/Window.cpp
parent4036e35e733b57d45df503813a6bd423d9a9e2a7 (diff)
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.
Diffstat (limited to 'src/desktop/Window.cpp')
-rw-r--r--src/desktop/Window.cpp25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/desktop/Window.cpp b/src/desktop/Window.cpp
index 706d21cc..8a0e37a6 100644
--- a/src/desktop/Window.cpp
+++ b/src/desktop/Window.cpp
@@ -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 {};