From 1ac247400dbc3013e317a3b2b07158d0b1bedcc8 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 6 Feb 2018 21:15:17 -0500 Subject: [PATCH] read bootstrap file relative to exe path --- src/main.cc | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/main.cc b/src/main.cc index cbf542b..892fa4d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -6,6 +6,8 @@ #include #include +#define SHARE_BOOTSTRAP "/share/svi/bootstrap.rb" + VALUE svi_ruby_protect_eval_string_rescue(VALUE exception, VALUE exception_object) { *(bool *)exception = true; @@ -57,14 +59,47 @@ static char * read_file(const char * filename) } } +std::string get_exe_path() +{ + pid_t pid = getpid(); + char proc_path[25]; + sprintf(proc_path, "/proc/%d/exe", pid); + char exe_path[100]; + ssize_t n = readlink(proc_path, exe_path, sizeof(exe_path)); + if ((n > 0) && (n < (ssize_t)sizeof(exe_path))) + { + return exe_path; + } + return ""; +} + +const char * read_bootstrap_file() +{ + auto exe_path = get_exe_path(); + size_t index = exe_path.rfind('/'); + if ((index != std::string::npos) && (index > 0u)) + { + index = exe_path.rfind('/', index - 1u); + if (index != std::string::npos) + { + std::string path = std::string(exe_path, 0u, index) + SHARE_BOOTSTRAP; + char * bootstrap = read_file(path.c_str()); + if (bootstrap != nullptr) + { + return bootstrap; + } + } + } + return read_file(SHARE_DIR SHARE_BOOTSTRAP); +} + int main(int argc, char * argv[]) { - static const char * ruby_startup_file = SHARE_DIR "/svi.rb"; bool exception = false; - char * ruby_startup = read_file(ruby_startup_file); + const char * ruby_startup = read_bootstrap_file(); if (ruby_startup == nullptr) { - fprintf(stderr, "Error opening %s\n", ruby_startup_file); + fprintf(stderr, "Could not read Ruby startup file\n"); return 1; } {