OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
heavyai_path.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifdef __APPLE__
20 #include <libproc.h>
21 #else
22 #include <linux/limits.h>
23 #endif
24 #include <unistd.h>
25 
26 #include <boost/filesystem/operations.hpp>
27 #include <boost/filesystem/path.hpp>
28 
29 #include "Logger/Logger.h"
30 #ifdef ENABLE_EMBEDDED_DATABASE
31 #ifndef _GNU_SOURCE
32 #define _GNU_SOURCE
33 #endif
34 #include <dlfcn.h>
35 #include <link.h>
36 #endif
37 
38 namespace heavyai {
39 
40 std::string get_root_abs_path() {
41 #ifdef ENABLE_EMBEDDED_DATABASE
42  void* const handle = dlopen(DBEngine_LIBNAME, RTLD_LAZY | RTLD_NOLOAD);
43  if (handle) {
44  /* Non-zero handle means that libDBEngine.so has been loaded and
45  the omnisci root path will be determined with respect to the
46  location of the shared library rather than the appliction
47  `/proc/self/exe` path. */
48  const struct link_map* link_map = 0;
49  const int ret = dlinfo(handle, RTLD_DI_LINKMAP, &link_map);
50  CHECK_EQ(ret, 0);
51  CHECK(link_map);
52  /* Despite the dlinfo man page claim that l_name is absolute path,
53  it is so only when the location path to the library is absolute,
54  say, as specified in LD_LIBRARY_PATH. */
55  boost::filesystem::path abs_exe_dir(boost::filesystem::absolute(
56  boost::filesystem::canonical(std::string(link_map->l_name))));
57  abs_exe_dir.remove_filename();
58 #ifdef XCODE
59  const auto mapd_root = abs_exe_dir.parent_path().parent_path();
60 #else
61  const auto mapd_root = abs_exe_dir.parent_path();
62 #endif
63  return mapd_root.string();
64  }
65 #endif
66 #ifdef __APPLE__
67  char abs_exe_path[PROC_PIDPATHINFO_MAXSIZE] = {0};
68  auto path_len = proc_pidpath(getpid(), abs_exe_path, sizeof(abs_exe_path));
69 #else
70  char abs_exe_path[PATH_MAX] = {0};
71  auto path_len = readlink("/proc/self/exe", abs_exe_path, sizeof(abs_exe_path));
72 #endif
73  CHECK_GT(path_len, 0);
74  CHECK_LT(static_cast<size_t>(path_len), sizeof(abs_exe_path));
75  boost::filesystem::path abs_exe_dir(std::string(abs_exe_path, path_len));
76  abs_exe_dir.remove_filename();
77 #ifdef XCODE
78  const auto mapd_root = abs_exe_dir.parent_path().parent_path();
79 #else
80  const auto mapd_root = abs_exe_dir.parent_path();
81 #endif
82  return mapd_root.string();
83 }
84 
85 } // namespace heavyai
#define CHECK_EQ(x, y)
Definition: Logger.h:301
std::string get_root_abs_path()
#define CHECK_GT(x, y)
Definition: Logger.h:305
#define CHECK_LT(x, y)
Definition: Logger.h:303
#define CHECK(condition)
Definition: Logger.h:291