-
-
Save zao/4c877240f9ebf59633b715363bfb01c1 to your computer and use it in GitHub Desktop.
libprocstat replacement for environ on FreeBSD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/param.h> | |
#include <sys/queue.h> | |
#include <sys/socket.h> | |
#include <sys/sysctl.h> | |
#include <libprocstat.h> | |
#include <unistd.h> | |
#include <iostream> | |
int main() { | |
struct procstat* procstat = procstat_open_sysctl(); | |
unsigned cnt{}; | |
struct kinfo_proc* kipp = procstat_getprocs(procstat, KERN_PROC_PID, getpid(), &cnt); | |
char** envs = procstat_getenvv(procstat, kipp, 0); | |
for (size_t i = 0; envs[i]; ++i) { | |
std::cout << "\"" << envs[i] << "\"" << std::endl; | |
} | |
procstat_freeenvv(procstat); | |
procstat_freeprocs(procstat, kipp); | |
procstat_close(procstat); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[zao@freja ~/pstest]$ c++ -o foo foo.cc -lprocstat | |
[zao@freja ~/pstest]$ ./foo | |
"SHELL=/usr/local/bin/bash" | |
"EDITOR=vi" | |
"ENV=/home/zao/.shrc" | |
"PWD=/home/zao/pstest" | |
"HOME=/home/zao" | |
"TERM=screen" | |
"USER=zao" | |
"SHLVL=1" | |
"PAGER=less" | |
"PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/home/zao/bin" | |
"BLOCKSIZE=K" | |
"MAIL=/var/mail/zao" | |
"_=./foo" | |
"OLDPWD=/home/zao" | |
[zao@freja ~/pstest]$ ldd ./foo | |
./foo: | |
libprocstat.so.1 => /usr/lib/libprocstat.so.1 (0x80024e000) | |
libc++.so.1 => /usr/lib/libc++.so.1 (0x80025b000) | |
libcxxrt.so.1 => /lib/libcxxrt.so.1 (0x800328000) | |
libm.so.5 => /lib/libm.so.5 (0x80034a000) | |
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x80037c000) | |
libc.so.7 => /lib/libc.so.7 (0x800396000) | |
libelf.so.2 => /lib/libelf.so.2 (0x80078c000) | |
libkvm.so.7 => /lib/libkvm.so.7 (0x8007a7000) | |
libutil.so.9 => /lib/libutil.so.9 (0x8007ba000) | |
[zao@freja ~/pstest]$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment