osutil.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #else
  73. #include <dirent.h>
  74. #include <unistd.h>
  75. #include <strings.h>
  76. #include <winpr/wtypes.h>
  77. static inline int is_num(const char* chars)
  78. {
  79. for (; *chars; chars++)
  80. if (*chars < '0' || *chars > '9')
  81. return 0; // false
  82. return 1; // true
  83. }
  84. TOOLKIT_API int osutil_detect_unique_app(char** pNames, int nNum)
  85. {
  86. int rc = TRUE;
  87. DIR* dir_proc = NULL;
  88. struct dirent* dir_entity = NULL;
  89. int i = 0;
  90. char cmdLinePath[512] = {0};
  91. char processName[128] = {0};
  92. char processPath[256] = {0};
  93. pid_t pid = (pid_t)-1;
  94. const pid_t cur_pid = getpid();
  95. dir_proc = opendir("/proc/");
  96. if (dir_proc == NULL) {
  97. return FALSE;
  98. }
  99. while ((dir_entity = readdir(dir_proc))) {
  100. if (dir_entity->d_type == DT_DIR) {
  101. if (is_num(dir_entity->d_name)) {
  102. pid = (pid_t)atoi(dir_entity->d_name);
  103. if (pid == getpid()) {
  104. continue;
  105. }
  106. strcpy(cmdLinePath, "/proc/");
  107. strcat(cmdLinePath, dir_entity->d_name);
  108. strcat(cmdLinePath, "/cmdline");
  109. FILE* fd_cmdline = fopen(cmdLinePath, "rt");
  110. if (fd_cmdline) {
  111. fscanf(fd_cmdline, "%s", processPath);
  112. fclose(fd_cmdline);
  113. if (strrchr(processPath, '/')) {
  114. strcpy(processName, strrchr(processPath, '/') + 1);
  115. }
  116. else {
  117. strcpy(processName, processPath);
  118. }
  119. for (i = 0; i < nNum; i++) {
  120. if (strcasecmp(processName, pNames[i]) == 0) {
  121. printf("%s, %s, %d, %d\n", processPath, processName, pid, cur_pid);
  122. rc = FALSE;
  123. break;
  124. }
  125. }
  126. }
  127. }
  128. }
  129. if (!rc) {
  130. break;
  131. }
  132. }
  133. closedir(dir_proc);
  134. return rc;
  135. }
  136. TOOLKIT_API int osutil_restart_system()
  137. {
  138. //TODO: implement it.
  139. return 0;
  140. }
  141. #endif