| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 #ifndef WEBRTC_MEDIA_BASE_EXECUTABLEHELPERS_H_ | |
| 12 #define WEBRTC_MEDIA_BASE_EXECUTABLEHELPERS_H_ | |
| 13 | |
| 14 #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) | |
| 15 #include <mach-o/dyld.h> | |
| 16 #endif | |
| 17 | |
| 18 #include <string> | |
| 19 | |
| 20 #include "webrtc/base/logging.h" | |
| 21 #include "webrtc/base/pathutils.h" | |
| 22 | |
| 23 namespace rtc { | |
| 24 | |
| 25 // Returns the path to the running executable or an empty path. | |
| 26 // TODO(thorcarpenter): Consolidate with FluteClient::get_executable_dir. | |
| 27 inline Pathname GetExecutablePath() { | |
| 28 const int32_t kMaxExePathSize = 255; | |
| 29 #ifdef WIN32 | |
| 30 TCHAR exe_path_buffer[kMaxExePathSize]; | |
| 31 DWORD copied_length = GetModuleFileName(NULL, // NULL = Current process | |
| 32 exe_path_buffer, kMaxExePathSize); | |
| 33 if (0 == copied_length) { | |
| 34 LOG(LS_ERROR) << "Copied length is zero"; | |
| 35 return rtc::Pathname(); | |
| 36 } | |
| 37 if (kMaxExePathSize == copied_length) { | |
| 38 LOG(LS_ERROR) << "Buffer too small"; | |
| 39 return rtc::Pathname(); | |
| 40 } | |
| 41 #ifdef UNICODE | |
| 42 std::wstring wdir(exe_path_buffer); | |
| 43 std::string dir_tmp(wdir.begin(), wdir.end()); | |
| 44 rtc::Pathname path(dir_tmp); | |
| 45 #else // UNICODE | |
| 46 rtc::Pathname path(exe_path_buffer); | |
| 47 #endif // UNICODE | |
| 48 #elif (defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)) || defined(WEBRTC_LINUX) | |
| 49 char exe_path_buffer[kMaxExePathSize]; | |
| 50 #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) | |
| 51 uint32_t copied_length = kMaxExePathSize - 1; | |
| 52 if (_NSGetExecutablePath(exe_path_buffer, &copied_length) == -1) { | |
| 53 LOG(LS_ERROR) << "Buffer too small"; | |
| 54 return rtc::Pathname(); | |
| 55 } | |
| 56 #elif defined WEBRTC_LINUX | |
| 57 int32_t copied_length = kMaxExePathSize - 1; | |
| 58 const char* kProcExeFmt = "/proc/%d/exe"; | |
| 59 char proc_exe_link[40]; | |
| 60 snprintf(proc_exe_link, sizeof(proc_exe_link), kProcExeFmt, getpid()); | |
| 61 copied_length = readlink(proc_exe_link, exe_path_buffer, copied_length); | |
| 62 if (copied_length == -1) { | |
| 63 LOG_ERR(LS_ERROR) << "Error reading link " << proc_exe_link; | |
| 64 return rtc::Pathname(); | |
| 65 } | |
| 66 if (copied_length == kMaxExePathSize - 1) { | |
| 67 LOG(LS_ERROR) << "Probably truncated result when reading link " | |
| 68 << proc_exe_link; | |
| 69 return rtc::Pathname(); | |
| 70 } | |
| 71 exe_path_buffer[copied_length] = '\0'; | |
| 72 #endif // WEBRTC_LINUX | |
| 73 rtc::Pathname path(exe_path_buffer); | |
| 74 #else // Android || iOS | |
| 75 rtc::Pathname path; | |
| 76 #endif // Mac || Linux | |
| 77 return path; | |
| 78 } | |
| 79 | |
| 80 } // namespace rtc | |
| 81 | |
| 82 #endif // WEBRTC_MEDIA_BASE_EXECUTABLEHELPERS_H_ | |
| 83 | |
| OLD | NEW |