factor/vm/os-linux.cpp

33 lines
787 B
C++
Raw Normal View History

2009-05-02 05:04:19 -04:00
#include "master.hpp"
2013-05-11 22:24:31 -04:00
namespace factor {
2009-05-04 02:46:13 -04:00
2013-05-11 22:24:31 -04:00
const char* vm_executable_path() {
ssize_t bufsiz = 4096;
2009-05-02 05:04:19 -04:00
// readlink is called in a loop with increasing buffer sizes in case
// someone tries to run Factor from a incredibly deeply nested
// path.
while (true) {
char* buf = new char[bufsiz + 1];
ssize_t size= readlink("/proc/self/exe", buf, bufsiz);
if (size < 0) {
fatal_error("Cannot read /proc/self/exe", errno);
} else {
if (size < bufsiz) {
// Buffer was large enough, return string.
buf[size] = '\0';
const char* ret = safe_strdup(buf);
delete[] buf;
return ret;
} else {
// Buffer wasn't big enough, double it and try again.
delete[] buf;
bufsiz *= 2;
}
}
2013-05-11 22:24:31 -04:00
}
2009-05-02 05:04:19 -04:00
}
2009-05-04 02:46:13 -04:00
}