osutil.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "precompile.h"
  2. #include "osutil.h"
  3. #ifdef _WIN32
  4. #include <TlHelp32.h>
  5. TOOLKIT_API int osutil_detect_unique_app(char** pNames, int nNum)
  6. {
  7. //const WCHAR *exe;
  8. HANDLE hSnapshot;
  9. int rc = TRUE;
  10. /*{
  11. WCHAR *t = (WCHAR*)_alloca(MAX_PATH);
  12. GetModuleFileNameW(NULL, t, MAX_PATH);
  13. exe = wcsrchr(t, '\\') + 1;
  14. }*/
  15. DWORD dwCurProcID = GetCurrentProcessId();
  16. hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  17. if (hSnapshot) {
  18. PROCESSENTRY32 pe;
  19. pe.dwSize = sizeof(pe);
  20. if (Process32First(hSnapshot, &pe)) {
  21. do {
  22. int i;
  23. for (i = 0; i < nNum; i++) {
  24. if (stricmp(&pe.szExeFile[0], pNames[i]) == 0 && pe.th32ProcessID != dwCurProcID) {
  25. rc = FALSE;
  26. break;
  27. }
  28. }
  29. } while (Process32Next(hSnapshot, &pe));
  30. }
  31. CloseHandle(hSnapshot);
  32. }
  33. return rc;
  34. }
  35. TOOLKIT_API int osutil_restart_system()
  36. {
  37. HANDLE hToken;
  38. TOKEN_PRIVILEGES tkp;
  39. BOOL fResult;
  40. // Get the current process token handle so we can get shutdown privilege.
  41. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
  42. fprintf(stderr, "get proc token fail");
  43. return -1;
  44. }
  45. // Get the LUID for shutdown privilege.
  46. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
  47. tkp.PrivilegeCount = 1; // one privilege to set
  48. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  49. // Get shutdown privilege for this process.
  50. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
  51. // Cannot test the return value of AdjustTokenPrivileges.
  52. if (GetLastError() != ERROR_SUCCESS) {
  53. fprintf(stderr, "adjust proc token privilege fail");
  54. return -1;
  55. }
  56. // Display the shutdown dialog box and start the countdown.
  57. fResult = InitiateSystemShutdown(
  58. NULL, // shut down local computer
  59. NULL, // message for user
  60. 0, // time-out period, in seconds
  61. FALSE, // ask user to close apps
  62. TRUE); // reboot after shutdown
  63. if (!fResult) {
  64. fprintf(stderr, "request windows reboot fail");
  65. return -1;
  66. }
  67. // Disable shutdown privilege.
  68. tkp.Privileges[0].Attributes = 0;
  69. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
  70. return 0;
  71. }
  72. TOOLKIT_API void osutil_terminate_related_process(char** process_array, const int array_size)
  73. {
  74. const DWORD dwCurProcessID = GetCurrentProcessId();
  75. DWORD relateProcessIDs[256];
  76. DWORD dwIdx = 0;
  77. char szCmd[256];
  78. DWORD relateProcessNum = 0;
  79. HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  80. memset(relateProcessIDs, 0, sizeof(relateProcessIDs));
  81. if (hSnapshot)
  82. {
  83. PROCESSENTRY32 pe;
  84. pe.dwSize = sizeof(pe);
  85. if (Process32First(hSnapshot, &pe)) {
  86. do {
  87. int i;
  88. for (i = 0; i < array_size; i++) {
  89. if (stricmp(&pe.szExeFile[0], process_array[i]) == 0 && pe.th32ProcessID != dwCurProcessID) {
  90. relateProcessIDs[relateProcessNum++] = pe.th32ProcessID;
  91. break;
  92. }
  93. }
  94. } while (Process32Next(hSnapshot, &pe));
  95. }
  96. CloseHandle(hSnapshot);
  97. }
  98. for (dwIdx = 0; dwIdx < relateProcessNum; ++dwIdx) {
  99. sprintf_s(szCmd, 256, "TASKKILL /PID %d /F", relateProcessIDs[dwIdx]);
  100. WinExec(szCmd, SW_HIDE);
  101. }
  102. }
  103. #else
  104. #include <dirent.h>
  105. #include <unistd.h>
  106. #include <strings.h>
  107. #include <winpr/wtypes.h>
  108. static inline int is_num(const char* chars)
  109. {
  110. for (; *chars; chars++)
  111. if (*chars < '0' || *chars > '9')
  112. return 0; // false
  113. return 1; // true
  114. }
  115. TOOLKIT_API int osutil_detect_unique_app(char** pNames, int nNum)
  116. {
  117. int rc = TRUE;
  118. DIR* dir_proc = NULL;
  119. struct dirent* dir_entity = NULL;
  120. int i = 0;
  121. char cmdLinePath[512] = {0};
  122. char processName[128] = {0};
  123. char processPath[256] = {0};
  124. pid_t pid = (pid_t)-1;
  125. const pid_t cur_pid = getpid();
  126. dir_proc = opendir("/proc/");
  127. if (dir_proc == NULL) {
  128. return FALSE;
  129. }
  130. while ((dir_entity = readdir(dir_proc))) {
  131. if (dir_entity->d_type == DT_DIR) {
  132. if (is_num(dir_entity->d_name)) {
  133. pid = (pid_t)atoi(dir_entity->d_name);
  134. if (pid == getpid()) {
  135. continue;
  136. }
  137. strcpy(cmdLinePath, "/proc/");
  138. strcat(cmdLinePath, dir_entity->d_name);
  139. strcat(cmdLinePath, "/cmdline");
  140. FILE* fd_cmdline = fopen(cmdLinePath, "rt");
  141. if (fd_cmdline) {
  142. fscanf(fd_cmdline, "%s", processPath);
  143. fclose(fd_cmdline);
  144. if (strrchr(processPath, '/')) {
  145. strcpy(processName, strrchr(processPath, '/') + 1);
  146. }
  147. else {
  148. strcpy(processName, processPath);
  149. }
  150. for (i = 0; i < nNum; i++) {
  151. if (strcasecmp(processName, pNames[i]) == 0) {
  152. printf("%s, %s, %d, %d\n", processPath, processName, pid, cur_pid);
  153. rc = FALSE;
  154. break;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. if (!rc) {
  161. break;
  162. }
  163. }
  164. closedir(dir_proc);
  165. return rc;
  166. }
  167. TOOLKIT_API int osutil_restart_system()
  168. {
  169. //TODO: implement it.
  170. return 0;
  171. }
  172. TOOLKIT_API void osutil_terminate_related_process(char** process_array, const int array_size)
  173. {
  174. }
  175. #endif