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>
36 static pthread_t loggingThread;
38 bool iCompare(const std::string& a, const std::string& b)
40 return a.length() == b.length() &&
41 std::equal(b.begin(), b.end(), a.begin(),
42 [](unsigned char a, unsigned char b)
43 { return std::tolower(a) == std::tolower(b); });
46 bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
48 return static_cast<int>(a.length()) - length >= aOffset &&
49 static_cast<int>(b.length()) - length >= bOffset &&
50 std::equal(b.begin() + bOffset, b.begin() + bOffset + length, a.begin() + aOffset,
51 [](unsigned char a, unsigned char b)
52 { return std::tolower(a) == std::tolower(b); });
55 bool isManagedAssembly(const std::string& fileName)
57 return iCompare(fileName, fileName.size()-4, ".dll", 0, 4) ||
58 iCompare(fileName, fileName.size()-4, ".exe", 0, 4);
61 bool isNativeImage(const std::string& fileName)
63 return iCompare(fileName, fileName.size()-7, ".ni", 0, 3);
66 std::string readSelfPath()
69 ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
72 return std::string(buff);
78 std::string concatPath(const std::string& path1, const std::string& path2)
80 std::string path(path1);
81 if (path.back() == PATH_SEPARATOR) {
84 path += PATH_SEPARATOR;
91 void appendPath(std::string& path1, const std::string& path2)
93 if (path1.back() == PATH_SEPARATOR) {
96 path1 += PATH_SEPARATOR;
101 std::string absolutePath(const std::string& path)
104 char realPath[PATH_MAX];
105 if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
106 absPath.assign(realPath);
111 std::string baseName(const std::string& path)
113 auto pos = path.find_last_of(PATH_SEPARATOR);
114 if (pos != std::string::npos)
115 return path.substr(0, pos);
117 return std::string(".");
121 bool endWithIgnoreCase(const std::string& str1, const std::string& str2, std::string& fileName)
123 std::string::size_type len1 = str1.length();
124 std::string::size_type len2 = str2.length();
129 bool result = std::all_of(str1.cend() - len2, str1.end(),
130 [&i, &str2] (char x) {
131 return std::tolower(x) == std::tolower(str2[i++]);
134 fileName = str1.substr(0, len1 - len2);
139 bool fileNotExist(const std::string& path)
142 return stat(path.c_str(), &sb) != 0;
145 #ifdef NOT_USE_FUNCTION
146 static bool extCheckAndGetFileNameIfExist(const std::string& dir, const std::string& ext, struct dirent* entry, std::string& fileName)
148 std::string fName(entry->d_name);
149 if (fName.length() < ext.length() ||
150 fHame.compare(fName.length() - ext.length(), ext.length(), ext) != 0) {
154 std::string fullName = concatPath(dir, entry->d_name);
155 switch (entry->d_type) {
159 if (fileNotExist(fullName))
171 std::string stripNiDLL(const std::string& path)
173 std::string niPath(path);
174 if (path.size() < 5) return niPath;
175 if (!strncasecmp(path.c_str() + path.size() - 4, ".dll", 4))
176 niPath = path.substr(0, path.size()-4);
177 else if (!strncasecmp(path.c_str() + path.size() - 4, ".exe", 4))
178 niPath = path.substr(0, path.size()-4);
180 if (!strncasecmp(niPath.c_str() + niPath.size() - 3, ".ni", 3))
181 return niPath.substr(0, niPath.size()-3);
186 std::string joinStrings(const std::vector<std::string>& strings, const char* const delimeter)
188 switch (strings.size()) {
194 std::ostringstream os;
195 std::copy(strings.begin(), strings.end()-1, std::ostream_iterator<std::string>(os, delimeter));
196 os << *strings.rbegin();
201 struct AssemblyFile {
206 bool operator == (const AssemblyFile& lhs, const AssemblyFile& rhs)
208 return lhs.noExt == rhs.noExt && lhs.ext == rhs.ext;
213 struct hash<AssemblyFile> {
214 std::size_t operator () (const AssemblyFile& f) const {
215 const std::size_t h1 = std::hash<std::string>{}(f.noExt);
216 const std::size_t h2 = std::hash<std::string>{}(f.ext);
218 return h1 ^ (h2 << 1);
223 void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
225 std::map<std::string, std::string> assemblyList;
226 std::map<std::string, std::string> tmpList;
228 auto reader = [&assemblyList, &tmpList] (const char* path, const char* name) {
229 std::string pathStr(path);
230 if (isManagedAssembly(pathStr)) {
231 std::string dllName = stripNiDLL(name);
232 std::pair<std::map<std::string, std::string>::iterator, bool> ret;
233 ret = tmpList.insert(std::pair<std::string, std::string>(dllName, pathStr));
234 if (ret.second == false) {
235 if (isNativeImage(pathStr))
236 tmpList[dllName] = pathStr;
241 for (auto directory : directories) {
242 scanFilesInDir(directory.c_str(), reader, 1);
243 // merge scaned dll list to tpa list.
244 // if the dll is already exist in the list, that is skipped.
245 assemblyList.insert(tmpList.begin(), tmpList.end());
248 std::map<std::string, std::string>::iterator it;
249 for (it = assemblyList.begin(); it != assemblyList.end(); it++)
250 tpaList += it->second + ':';
252 if (tpaList.back() == ':')
256 void scanFilesInDir(const char* directory, FileReader reader, unsigned int depth)
259 struct dirent* entry;
262 dir = opendir(directory);
267 std::vector<std::string> innerDirectories;
269 while ((entry = readdir(dir)) != nullptr) {
271 std::string path = concatPath(directory, entry->d_name);
272 switch (entry->d_type) {
280 if (stat(path.c_str(), &sb) == -1)
283 if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
289 reader(path.c_str(), entry->d_name);
290 else if (depth > 1 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
291 innerDirectories.push_back(path);
295 for (auto& d : innerDirectories)
296 scanFilesInDir(d.c_str(), reader, depth-1);
301 static void *stdlog(void*)
307 if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) {
308 _DBG("fail to make stdout line-buffered");
312 if (setvbuf(stderr, NULL, _IONBF, 0) < 0) {
313 _DBG("make stderr unbuffered");
317 /* create the pipe and redirect stdout and stderr */
319 _DBG("fail to create pipe for logging");
323 if (dup2(pfd[1], fileno(stdout)) == -1) {
324 _DBG("fail to duplicate fd to stdout");
328 if (dup2(pfd[1], fileno(stderr)) == -1) {
329 _DBG("fail to duplicate fd to stderr");
335 while ((readSize = read(pfd[0], buf, sizeof buf - 1)) > 0) {
336 if (buf[readSize - 1] == '\n') {
350 int runLoggingThread() {
351 /* spawn the logging thread */
352 if (pthread_create(&loggingThread, 0, stdlog, 0) != 0) {
353 _DBG("fail to create pthread");
357 if (pthread_detach(loggingThread) != 0) {
358 _DBG("fail to detach pthread");