2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include <unordered_map>
34 #include "path_manager.h"
37 static pthread_t loggingThread;
39 static bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
41 return static_cast<int>(a.length()) - length >= aOffset &&
42 static_cast<int>(b.length()) - length >= bOffset &&
43 std::equal(b.begin() + bOffset, b.begin() + bOffset + length, a.begin() + aOffset,
44 [](unsigned char a, unsigned char b)
45 { return std::tolower(a) == std::tolower(b); });
48 bool isManagedAssembly(const std::string& fileName)
50 return (iCompare(fileName, fileName.size()-4, ".dll", 0, 4) ||
51 iCompare(fileName, fileName.size()-4, ".exe", 0, 4)) &&
52 !isNativeImage(fileName);
55 bool isNativeImage(const std::string& fileName)
57 return iCompare(fileName, fileName.size()-7, ".ni", 0, 3);
60 std::string readSelfPath()
63 ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
66 return std::string(buff);
72 std::string concatPath(const std::string& path1, const std::string& path2)
74 std::string path(path1);
75 if (path.back() == PATH_SEPARATOR) {
78 path += PATH_SEPARATOR;
85 void splitPath(const std::string& path, std::vector<std::string>& out)
87 std::istringstream ss(path);
90 while (std::getline(ss, token, ':')) {
95 std::string absolutePath(const std::string& path)
98 char realPath[PATH_MAX];
99 if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
100 absPath.assign(realPath);
105 std::string baseName(const std::string& path)
107 auto pos = path.find_last_of(PATH_SEPARATOR);
108 if (pos != std::string::npos)
109 return path.substr(0, pos);
111 return std::string(".");
115 bool isFileExist(const std::string& path)
118 return stat(path.c_str(), &sb) == 0;
121 std::string stripNiDLL(const std::string& path)
123 std::string niPath(path);
124 if (path.size() < 5) return niPath;
125 if (!strncasecmp(path.c_str() + path.size() - 4, ".dll", 4))
126 niPath = path.substr(0, path.size()-4);
127 else if (!strncasecmp(path.c_str() + path.size() - 4, ".exe", 4))
128 niPath = path.substr(0, path.size()-4);
130 if (!strncasecmp(niPath.c_str() + niPath.size() - 3, ".ni", 3))
131 return niPath.substr(0, niPath.size()-3);
136 void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
138 std::map<std::string, std::string> assemblyList;
139 std::map<std::string, std::string> tmpList;
141 auto reader = [&assemblyList, &tmpList] (const std::string& path, const char* name) {
142 if (isManagedAssembly(path) || isNativeImage(path)) {
143 std::string dllName = stripNiDLL(name);
144 std::pair<std::map<std::string, std::string>::iterator, bool> ret;
145 ret = tmpList.insert(std::pair<std::string, std::string>(dllName, path));
146 if (ret.second == false) {
147 if (isNativeImage(path))
148 tmpList[dllName] = path;
153 for (auto directory : directories) {
154 scanFilesInDir(directory.c_str(), reader, 1);
155 // merge scaned dll list to tpa list.
156 // if the dll is already exist in the list, that is skipped.
157 assemblyList.insert(tmpList.begin(), tmpList.end());
160 std::map<std::string, std::string>::iterator it;
161 for (it = assemblyList.begin(); it != assemblyList.end(); it++)
162 tpaList += it->second + ':';
164 if (tpaList.back() == ':')
168 void scanFilesInDir(const std::string& directory, FileReader reader, unsigned int depth)
171 struct dirent* entry;
174 dir = opendir(directory.c_str());
179 std::vector<std::string> innerDirectories;
181 while ((entry = readdir(dir)) != nullptr) {
183 std::string path = concatPath(directory, entry->d_name);
184 switch (entry->d_type) {
192 if (stat(path.c_str(), &sb) == -1)
195 if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
201 reader(path, entry->d_name);
202 else if (depth > 1 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
203 innerDirectories.push_back(path);
207 for (auto& d : innerDirectories)
208 scanFilesInDir(d.c_str(), reader, depth-1);
215 static void *stdlog(void*)
220 while ((readSize = read(__pfd[0], buf, sizeof buf - 1)) > 0) {
221 if (buf[readSize - 1] == '\n') {
235 int runLoggingThread()
237 if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) {
238 _DBG("fail to make stdout line-buffered");
242 if (setvbuf(stderr, NULL, _IONBF, 0) < 0) {
243 _DBG("make stderr unbuffered");
247 /* create the pipe and redirect stdout and stderr */
248 if (pipe(__pfd) < 0) {
249 _DBG("fail to create pipe for logging");
254 if (dup2(__pfd[1], 1) == -1) {
255 _DBG("fail to duplicate fd to stdout");
260 if (dup2(__pfd[1], 2) == -1) {
261 _DBG("fail to duplicate fd to stderr");
267 /* spawn the logging thread */
268 if (pthread_create(&loggingThread, 0, stdlog, 0) != 0) {
269 _DBG("fail to create pthread");
273 if (pthread_detach(loggingThread) != 0) {
274 _DBG("fail to detach pthread");