liblog4vendor.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // liblog4vendor.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. #include "liblog4vendor.h"
  4. #include <stdio.h>
  5. #if OS_WIN
  6. #include <Psapi.h>
  7. #pragma comment(lib, "Psapi.lib")
  8. #define CUR_PROCESS_NAME "sphost.exe"
  9. static HMODULE gModule = NULL;
  10. #else
  11. #include <string.h>
  12. #include <sys/types.h>
  13. #include <unistd.h>
  14. #define CUR_PROCESS_NAME "sphost"
  15. #endif
  16. static int IsSphostExe = -1;
  17. BOOL GetCurProcessPath(char szPath[], DWORD dwPathSize);
  18. /*!
  19. * @brief detect current process is created by VTM
  20. * @return : TRUE: created by VTM; FALSE: created by vendor
  21. * TODO: need to enforce it for safety.
  22. */
  23. EXTERN_C BOOL IsVTMProcess()
  24. {
  25. if(IsSphostExe == -1) {
  26. char szPath[4096] = {'\0'};
  27. DWORD dwPathSize = 4096;
  28. if(GetCurProcessPath(szPath, dwPathSize)) {
  29. const int pathLen = strlen(szPath);
  30. int index;
  31. for(index=0; index<pathLen; ++index) {
  32. if(szPath[index] >= 'A' && szPath[index] <= 'Z')
  33. szPath[index] = 'a' + (szPath[index] - 'A');
  34. }
  35. char* pos = strstr(szPath, CUR_PROCESS_NAME);
  36. dwPathSize = strlen(szPath);
  37. if(pos != NULL && ((pos-szPath) + strlen(CUR_PROCESS_NAME)) == dwPathSize) {
  38. IsSphostExe = TRUE;
  39. } else {
  40. IsSphostExe = FALSE;
  41. }
  42. } else {
  43. /*If failed, regard it as VTM environment.*/
  44. IsSphostExe = TRUE;
  45. }
  46. }
  47. return IsSphostExe;
  48. }
  49. EXTERN_C BOOL GetCurFileVersion(char szVerion[], DWORD dwVerionSize)
  50. {
  51. #if OS_WIN
  52. HRSRC hsrc=FindResourceA(gModule, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
  53. HGLOBAL hgbl = LoadResource(gModule, hsrc);
  54. BYTE *pBt = (BYTE *)LockResource(hgbl);
  55. VS_FIXEDFILEINFO* pFinfo = (VS_FIXEDFILEINFO*)(pBt + 40);
  56. sprintf_s(szVerion, dwVerionSize, "%d.%d.%d.%d",
  57. (pFinfo->dwFileVersionMS >> 16) & 0xFF,
  58. (pFinfo->dwFileVersionMS) & 0xFF,
  59. (pFinfo->dwFileVersionLS >> 16) & 0xFF,
  60. (pFinfo->dwFileVersionLS) & 0xFF);
  61. #endif
  62. return TRUE;
  63. }
  64. #if OS_WIN
  65. BOOL APIENTRY DllMain( HMODULE hModule,
  66. DWORD ul_reason_for_call,
  67. LPVOID lpReserved
  68. )
  69. {
  70. switch (ul_reason_for_call)
  71. {
  72. case DLL_PROCESS_ATTACH:
  73. gModule = hModule;
  74. IsVTMProcess();
  75. break;
  76. case DLL_THREAD_ATTACH:
  77. case DLL_THREAD_DETACH:
  78. case DLL_PROCESS_DETACH:
  79. break;
  80. }
  81. return TRUE;
  82. }
  83. BOOL GetCurProcessPath(char szPath[], DWORD dwPathSize)
  84. {
  85. ZeroMemory(szPath, sizeof(char)*dwPathSize);
  86. const DWORD PID = GetCurrentProcessId();
  87. HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
  88. FALSE, PID);
  89. if (hProcess == NULL) {
  90. return FALSE;
  91. }
  92. if (GetModuleFileNameExA(hProcess, (HMODULE)0, szPath, dwPathSize) == 0) {
  93. printf("GetModuleFileNameExA failed: %d\n", GetLastError());
  94. DWORD dwSize = dwPathSize;
  95. if(QueryFullProcessImageNameA(hProcess, 0, szPath, &dwSize) == 0)
  96. {
  97. printf("QueryFullProcessImageNameA failed: %d\n", GetLastError());
  98. if (!GetProcessImageFileNameA(hProcess, szPath, dwSize)) {
  99. printf("GetProcessImageFileNameA failed: %d\n", GetLastError());
  100. CloseHandle(hProcess);
  101. return FALSE;
  102. }
  103. }
  104. }
  105. CloseHandle(hProcess);
  106. printf("process image name: %s\n", szPath);
  107. return TRUE;
  108. }
  109. #else
  110. BOOL GetCurProcessPath(char szPath[], DWORD dwPathSize)
  111. {
  112. int status;
  113. size_t length;
  114. char path[64];
  115. char buffer[4096];
  116. snprintf(path, 64, "/proc/%d/exe", getpid());
  117. status = readlink(path, buffer, sizeof(buffer));
  118. if (status < 0) {
  119. return FALSE;
  120. }
  121. buffer[status] = '\0';
  122. length = strnlen(buffer, sizeof(buffer));
  123. memset(szPath, '\0', sizeof(szPath[0]) * dwPathSize);
  124. if (length < dwPathSize) {
  125. memcpy(szPath, buffer, length);
  126. szPath[length] = '\0';
  127. return TRUE;
  128. }
  129. memcpy(szPath, buffer, dwPathSize - 1);
  130. szPath[dwPathSize - 1] = '\0';
  131. return FALSE;
  132. }
  133. #endif