process.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "toolkit.h"
  2. #include <winpr/windows.h>
  3. #include "memutil.h"
  4. #ifdef _WIN32
  5. #include <tlhelp32.h> //CreateToolhelp32Snapshot
  6. #endif //_WIN32
  7. #define TAG TOOLKIT_TAG("process")
  8. TOOLKIT_API int process_init(tk_process_t* proc)
  9. {
  10. proc->handle = NULL;
  11. proc->pid = 0;
  12. return 0;
  13. }
  14. TOOLKIT_API int process_spawn(const tk_process_option_t* option, tk_process_t** proc)
  15. {
  16. tk_process_t* new_process = NULL;
  17. int ret = 0;
  18. LPVOID pEnv = NULL;
  19. STARTUPINFOA si = { sizeof(STARTUPINFOA) };
  20. PROCESS_INFORMATION pi;
  21. HANDLE hProcess = NULL;
  22. #if _WIN32
  23. DWORD dwSessionId;
  24. HANDLE hUserTokenDup, hThisToken;
  25. new_process = MALLOC_T(tk_process_t);
  26. if (new_process == NULL) {
  27. return -1;
  28. }
  29. dwSessionId = WTSGetActiveConsoleSessionId();
  30. if (OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hThisToken)) {
  31. LUID luid;
  32. TOKEN_PRIVILEGES tp;
  33. LookupPrivilegeValueA(NULL, SE_DEBUG_NAME, &luid);
  34. tp.PrivilegeCount = 1;
  35. tp.Privileges[0].Luid = luid;
  36. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  37. DuplicateTokenEx(hThisToken, MAXIMUM_ALLOWED, NULL,
  38. SecurityIdentification, TokenPrimary, &hUserTokenDup);
  39. SetTokenInformation(hUserTokenDup,
  40. TokenSessionId, (void*)dwSessionId, sizeof(DWORD));
  41. AdjustTokenPrivileges(hUserTokenDup, FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
  42. (PTOKEN_PRIVILEGES)NULL, NULL);
  43. if (CreateProcessAsUserA(
  44. hUserTokenDup
  45. , option->file
  46. , option->params
  47. , NULL
  48. , NULL
  49. , FALSE
  50. , 0
  51. , pEnv
  52. , NULL
  53. , &si
  54. , &pi)) {
  55. CloseHandle(pi.hThread);
  56. new_process->pid = pi.dwProcessId;
  57. new_process->handle = pi.hProcess;
  58. }
  59. else {
  60. WLog_ERR(TAG, "create process as user failed.");
  61. FREE(new_process);
  62. ret = -1;
  63. }
  64. CloseHandle(hUserTokenDup);
  65. CloseHandle(hThisToken);
  66. }
  67. else {
  68. WLog_ERR(TAG, "open process token failed.");
  69. FREE(new_process);
  70. ret = -1;
  71. }
  72. #else
  73. new_process = MALLOC_T(tk_process_t);
  74. if (new_process == NULL) {
  75. return -1;
  76. }
  77. if (CreateProcessA(
  78. option->file
  79. , option->params
  80. , NULL
  81. , NULL
  82. , FALSE
  83. , 0
  84. , pEnv
  85. , NULL
  86. , &si
  87. , &pi)) {
  88. CloseHandle(pi.hThread);
  89. new_process->pid = pi.dwProcessId;
  90. new_process->handle = pi.hProcess;
  91. }
  92. else {
  93. WLog_ERR(TAG, "create process failed, GetLastError(%d).", GetLastError());
  94. FREE(new_process);
  95. ret = -1;
  96. }
  97. #endif
  98. if (ret == 0) {
  99. *proc = new_process;
  100. }
  101. return ret;
  102. }
  103. TOOLKIT_API int process_compare(const tk_process_t* proc1, const tk_process_t* proc2)
  104. {
  105. if(proc1 == NULL && proc1 == proc2) {
  106. return 0;
  107. }
  108. if(proc1 == NULL || proc2 == NULL) {
  109. return -1;
  110. }
  111. if(proc1->handle == proc2->handle) {
  112. return 0;
  113. }
  114. if (proc1->pid == proc2->pid) {
  115. return 0;
  116. }
  117. return -1;
  118. }
  119. TOOLKIT_API void process_close(tk_process_t* proc)
  120. {
  121. if(proc->handle) {
  122. CloseHandle(proc->handle);
  123. proc->handle = NULL;
  124. }
  125. proc->pid = 0;
  126. }
  127. TOOLKIT_API bool process_exist_or_not(int pid)
  128. {
  129. bool bRet = false;
  130. #ifdef _WIN32
  131. HANDLE hProcessSnap;
  132. PROCESSENTRY32 pe32;
  133. // Take a snapshot of all processes in the system.
  134. hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  135. if (hProcessSnap == INVALID_HANDLE_VALUE) {
  136. return(FALSE);
  137. }
  138. pe32.dwSize = sizeof(PROCESSENTRY32);
  139. if (!Process32First(hProcessSnap, &pe32)) {
  140. CloseHandle(hProcessSnap); // clean the snapshot object
  141. return(false);
  142. }
  143. do {
  144. if (pid == pe32.th32ProcessID) {
  145. bRet = true;
  146. break;
  147. }
  148. } while (Process32Next(hProcessSnap, &pe32));
  149. CloseHandle(hProcessSnap);
  150. #endif //_WIN32
  151. return bRet;
  152. }