Skip to content

Commit 379ee99

Browse files
authored
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.
1 parent 4036e35 commit 379ee99

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/desktop/Window.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
#include <hyprutils/animation/AnimatedVariable.hpp>
44
#include <re2/re2.h>
55

6+
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
7+
#include <sys/types.h>
8+
#include <sys/sysctl.h>
9+
#endif
10+
611
#include <any>
712
#include <bit>
813
#include <string_view>
@@ -1203,20 +1208,36 @@ std::unordered_map<std::string, std::string> CWindow::getEnv() {
12031208

12041209
std::unordered_map<std::string, std::string> results;
12051210

1211+
std::vector<char> buffer;
1212+
size_t needle = 0;
1213+
1214+
#if defined(__linux__)
12061215
//
12071216
std::string environFile = "/proc/" + std::to_string(PID) + "/environ";
12081217
std::ifstream ifs(environFile, std::ios::binary);
12091218

12101219
if (!ifs.good())
12111220
return {};
12121221

1213-
std::vector<char> buffer;
1214-
size_t needle = 0;
12151222
buffer.resize(512, '\0');
12161223
while (ifs.read(buffer.data() + needle, 512)) {
12171224
buffer.resize(buffer.size() + 512, '\0');
12181225
needle += 512;
12191226
}
1227+
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
1228+
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ENV, static_cast<int>(PID)};
1229+
size_t len = 0;
1230+
1231+
if (sysctl(mib, 4, nullptr, &len, nullptr, 0) < 0 || len == 0)
1232+
return {};
1233+
1234+
buffer.resize(len, '\0');
1235+
1236+
if (sysctl(mib, 4, buffer.data(), &len, nullptr, 0) < 0)
1237+
return {};
1238+
1239+
needle = len;
1240+
#endif
12201241

12211242
if (needle <= 1)
12221243
return {};

0 commit comments

Comments
 (0)