]> gitweb.factorcode.org Git - factor.git/blob - vm/os-linux.cpp
Put brackets around ipv6 addresses in `inet6 present`
[factor.git] / vm / os-linux.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 const char* vm_executable_path() {
6   ssize_t bufsiz = 4096;
7
8   // readlink is called in a loop with increasing buffer sizes in case
9   // someone tries to run Factor from a incredibly deeply nested
10   // path.
11   while (true) {
12     char* buf = new char[bufsiz + 1];
13     ssize_t size= readlink("/proc/self/exe", buf, bufsiz);
14     if (size < 0) {
15       fatal_error("Cannot read /proc/self/exe", errno);
16     } else {
17       if (size < bufsiz) {
18         // Buffer was large enough, return string.
19         buf[size] = '\0';
20         const char* ret = safe_strdup(buf);
21         delete[] buf;
22         return ret;
23       } else {
24         // Buffer wasn't big enough, double it and try again.
25         delete[] buf;
26         bufsiz *= 2;
27       }
28     }
29   }
30 }
31
32 }