|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#include "opentelemetry/resource_detectors/detail/process_detector_utils.h" |
| 5 | + |
| 6 | +#include <fstream> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +#ifdef _MSC_VER |
| 10 | +// clang-format off |
| 11 | +# include <windows.h> |
| 12 | +# include <psapi.h> |
| 13 | +// clang-format on |
| 14 | +#else |
| 15 | +# include <sys/types.h> |
| 16 | +# include <unistd.h> |
| 17 | +# include <cstdio> |
| 18 | +#endif |
| 19 | + |
| 20 | +#include "opentelemetry/version.h" |
| 21 | + |
| 22 | +OPENTELEMETRY_BEGIN_NAMESPACE |
| 23 | +namespace resource_detector |
| 24 | +{ |
| 25 | +namespace detail |
| 26 | +{ |
| 27 | + |
| 28 | +constexpr const char *kExecutableName = "exe"; |
| 29 | +constexpr const char *kCmdlineName = "cmdline"; |
| 30 | + |
| 31 | +std::string GetExecutablePath(const int32_t &pid) |
| 32 | +{ |
| 33 | +#ifdef _MSC_VER |
| 34 | + HANDLE hProcess = |
| 35 | + OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, static_cast<DWORD>(pid)); |
| 36 | + if (!hProcess) |
| 37 | + { |
| 38 | + return std::string(); |
| 39 | + } |
| 40 | + |
| 41 | + WCHAR wbuffer[MAX_PATH]; |
| 42 | + DWORD len = GetProcessImageFileNameW(hProcess, wbuffer, MAX_PATH); |
| 43 | + CloseHandle(hProcess); |
| 44 | + |
| 45 | + if (len == 0) |
| 46 | + { |
| 47 | + return std::string(); |
| 48 | + } |
| 49 | + |
| 50 | + // Convert UTF-16 to UTF-8 |
| 51 | + int size_needed = WideCharToMultiByte(CP_UTF8, 0, wbuffer, len, NULL, 0, NULL, NULL); |
| 52 | + std::string utf8_path(size_needed, 0); |
| 53 | + WideCharToMultiByte(CP_UTF8, 0, wbuffer, len, &utf8_path[0], size_needed, NULL, NULL); |
| 54 | + |
| 55 | + return utf8_path; |
| 56 | +#else |
| 57 | + std::string path = FormFilePath(pid, kExecutableName); |
| 58 | + char buffer[4096]; |
| 59 | + |
| 60 | + ssize_t len = readlink(path.c_str(), buffer, sizeof(buffer) - 1); |
| 61 | + if (len != -1) |
| 62 | + { |
| 63 | + buffer[len] = '\0'; |
| 64 | + return std::string(buffer); |
| 65 | + } |
| 66 | + |
| 67 | + return std::string(); |
| 68 | +#endif |
| 69 | +} |
| 70 | + |
| 71 | +std::string GetCommand(const int32_t &pid) |
| 72 | +{ |
| 73 | +#ifdef _MSC_VER |
| 74 | + // On Windows, GetCommandLineW only works for the CURRENT process, |
| 75 | + // so we ignore `pid` and just return the current process's command line. |
| 76 | + LPCWSTR wcmd = GetCommandLineW(); |
| 77 | + if (!wcmd) |
| 78 | + { |
| 79 | + return std::string(); |
| 80 | + } |
| 81 | + |
| 82 | + // Convert UTF-16 to UTF-8 |
| 83 | + int size_needed = WideCharToMultiByte(CP_UTF8, 0, wcmd, -1, NULL, 0, NULL, NULL); |
| 84 | + if (size_needed <= 0) |
| 85 | + { |
| 86 | + return std::string(); |
| 87 | + } |
| 88 | + |
| 89 | + std::string utf8_command(size_needed - 1, 0); // exclude null terminator |
| 90 | + WideCharToMultiByte(CP_UTF8, 0, wcmd, -1, &utf8_command[0], size_needed, NULL, NULL); |
| 91 | + |
| 92 | + return utf8_command; |
| 93 | +#else |
| 94 | + // This is the path to get the command that was used to start the process |
| 95 | + std::string command_line_path = FormFilePath(pid, kCmdlineName); |
| 96 | + return ExtractCommand(command_line_path); |
| 97 | +#endif |
| 98 | +} |
| 99 | + |
| 100 | +std::string ExtractCommand(const std::string &command_line_path) |
| 101 | +{ |
| 102 | + std::string command; |
| 103 | + std::ifstream command_line_file(command_line_path, std::ios::in | std::ios::binary); |
| 104 | + std::getline(command_line_file, command, '\0'); |
| 105 | + return command; |
| 106 | +} |
| 107 | + |
| 108 | +std::string FormFilePath(const int32_t &pid, const char *process_type) |
| 109 | +{ |
| 110 | + char buff[64]; |
| 111 | + int len = std::snprintf(buff, sizeof(buff), "/proc/%d/%s", pid, process_type); |
| 112 | + if (len < 0) |
| 113 | + { |
| 114 | + // in case snprintf fails |
| 115 | + return std::string(); |
| 116 | + } |
| 117 | + if (len >= static_cast<int>(sizeof(buff))) |
| 118 | + { |
| 119 | + return std::string(buff, sizeof(buff) - 1); |
| 120 | + } |
| 121 | + return std::string(buff, len); |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace detail |
| 125 | +} // namespace resource_detector |
| 126 | +OPENTELEMETRY_END_NAMESPACE |
0 commit comments