]> gitweb.factorcode.org Git - factor.git/blobdiff - vm/os-linux.cpp
Put brackets around ipv6 addresses in `inet6 present`
[factor.git] / vm / os-linux.cpp
index f5814d7f184372ce4fcfcc61a2799ce3487c6e10..0261942462c6bd8b778f23b944862681cbb71ab7 100644 (file)
@@ -1,63 +1,32 @@
 #include "master.hpp"
 
-namespace factor
-{
-
-/* Snarfed from SBCL linux-so.c. You must free() this yourself. */
-const char *vm_executable_path()
-{
-       char *path = (char *)safe_malloc(PATH_MAX + 1);
-
-       int size = readlink("/proc/self/exe", path, PATH_MAX);
-       if (size < 0)
-       {
-               fatal_error("Cannot read /proc/self/exe",0);
-               return NULL;
-       }
-       else
-       {
-               path[size] = '\0';
-               return safe_strdup(path);
-       }
-}
-
-#ifdef SYS_inotify_init
-
-int inotify_init()
-{
-       return syscall(SYS_inotify_init);
-}
-
-int inotify_add_watch(int fd, const char *name, u32 mask)
-{
-       return syscall(SYS_inotify_add_watch, fd, name, mask);
-}
-
-int inotify_rm_watch(int fd, u32 wd)
-{
-       return syscall(SYS_inotify_rm_watch, fd, wd);
+namespace factor {
+
+const char* vm_executable_path() {
+  ssize_t bufsiz = 4096;
+
+  // 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;
+      }
+    }
+  }
 }
 
-#else
-
-int inotify_init()
-{
-       not_implemented_error();
-       return -1;
-}
-
-int inotify_add_watch(int fd, const char *name, u32 mask)
-{
-       not_implemented_error();
-       return -1;
-}
-
-int inotify_rm_watch(int fd, u32 wd)
-{
-       not_implemented_error();
-       return -1;
-}
-
-#endif
-
 }