read bootstrap file relative to exe path

This commit is contained in:
Josh Holtrop 2018-02-06 21:15:17 -05:00
parent b7b587fb8f
commit 1ac247400d

View File

@ -6,6 +6,8 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#define SHARE_BOOTSTRAP "/share/svi/bootstrap.rb"
VALUE svi_ruby_protect_eval_string_rescue(VALUE exception, VALUE exception_object) VALUE svi_ruby_protect_eval_string_rescue(VALUE exception, VALUE exception_object)
{ {
*(bool *)exception = true; *(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[]) int main(int argc, char * argv[])
{ {
static const char * ruby_startup_file = SHARE_DIR "/svi.rb";
bool exception = false; bool exception = false;
char * ruby_startup = read_file(ruby_startup_file); const char * ruby_startup = read_bootstrap_file();
if (ruby_startup == nullptr) if (ruby_startup == nullptr)
{ {
fprintf(stderr, "Error opening %s\n", ruby_startup_file); fprintf(stderr, "Could not read Ruby startup file\n");
return 1; return 1;
} }
{ {