OmniSciDB  8fa3bf436f
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
omnisci_path.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, 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/path.hpp>
27 
28 #include "Logger/Logger.h"
29 #ifdef ENABLE_EMBEDDED_DATABASE
30 #ifndef _GNU_SOURCE
31 #define _GNU_SOURCE
32 #endif
33 #include <dlfcn.h>
34 #include <link.h>
35 #endif
36 
37 namespace omnisci {
38 
39 std::string get_root_abs_path() {
40 #ifdef ENABLE_EMBEDDED_DATABASE
41  void* const handle = dlopen(DBEngine_LIBNAME, RTLD_LAZY | RTLD_NOLOAD);
42  if (handle) {
43  /* Non-zero handle means that libDBEngine.so has been loaded and
44  the omnisci root path will be determined with respect to the
45  location of the shared library rather than the appliction
46  `/proc/self/exe` path. */
47  const struct link_map* link_map = 0;
48  const int ret = dlinfo(handle, RTLD_DI_LINKMAP, &link_map);
49  CHECK_EQ(ret, 0);
50  CHECK(link_map);
51  /* Despite the dlinfo man page claim that l_name is absolute path,
52  it is so only when the location path to the library is absolute,
53  say, as specified in LD_LIBRARY_PATH. */
54  boost::filesystem::path abs_exe_dir(boost::filesystem::absolute(
55  boost::filesystem::canonical(std::string(link_map->l_name))));
56  abs_exe_dir.remove_filename();
57 #ifdef XCODE
58  const auto mapd_root = abs_exe_dir.parent_path().parent_path();
59 #else
60  const auto mapd_root = abs_exe_dir.parent_path();
61 #endif
62  return mapd_root.string();
63  }
64 #endif
65 #ifdef __APPLE__
66  char abs_exe_path[PROC_PIDPATHINFO_MAXSIZE] = {0};
67  auto path_len = proc_pidpath(getpid(), abs_exe_path, sizeof(abs_exe_path));
68 #else
69  char abs_exe_path[PATH_MAX] = {0};
70  auto path_len = readlink("/proc/self/exe", abs_exe_path, sizeof(abs_exe_path));
71 #endif
72  CHECK_GT(path_len, 0);
73  CHECK_LT(static_cast<size_t>(path_len), sizeof(abs_exe_path));
74  boost::filesystem::path abs_exe_dir(std::string(abs_exe_path, path_len));
75  abs_exe_dir.remove_filename();
76 #ifdef XCODE
77  const auto mapd_root = abs_exe_dir.parent_path().parent_path();
78 #else
79  const auto mapd_root = abs_exe_dir.parent_path();
80 #endif
81  return mapd_root.string();
82 }
83 
84 } // namespace omnisci
#define CHECK_EQ(x, y)
Definition: Logger.h:211
#define CHECK_GT(x, y)
Definition: Logger.h:215
#define CHECK_LT(x, y)
Definition: Logger.h:213
#define CHECK(condition)
Definition: Logger.h:203
std::string get_root_abs_path()