liblog4vendor.cpp 3.4 KB

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