1 // roughly based on the code for Firefox class BinaryPath
2 // https://searchfox.org/mozilla-central/source/xpcom/build/BinaryPath.h#185
6 #include <sys/sysctl.h>
16 int main(int argc
, char **argv
)
18 int mib
[] = { CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1};
21 if (sysctl(mib
, 4, NULL
, &len
, NULL
, 0) != 0) {
22 cout
<< "sysctl failed to get path length: " << std::strerror(errno
) << '\n';
26 std::unique_ptr
<char[]> aResult(new char[len
]);
28 if (sysctl(mib
, 4, aResult
.get(), &len
, NULL
, 0) != 0) {
29 cout
<< "sysctl failed to get path: " << strerror(errno
) << '\n';
33 if (string(aResult
.get()) == argv
[1]) {
36 cout
<< "Not OK aResult " << aResult
.get() << " argv[1] " << argv
[1] << '\n';
39 if (sysctl(mib
, 4, NULL
, NULL
, NULL
, 0) != -1) {
40 cout
<< "OK syscall failed\n";
43 cout
<< "sysctl succeeded when it should have failed\n";