system.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "rar.hpp"
  2. static int SleepTime=0;
  3. void InitSystemOptions(int SleepTime)
  4. {
  5. ::SleepTime=SleepTime;
  6. }
  7. #if !defined(SFX_MODULE)
  8. void SetPriority(int Priority)
  9. {
  10. #ifdef _WIN_ALL
  11. uint PriorityClass;
  12. int PriorityLevel;
  13. if (Priority<1 || Priority>15)
  14. return;
  15. if (Priority==1)
  16. {
  17. PriorityClass=IDLE_PRIORITY_CLASS;
  18. PriorityLevel=THREAD_PRIORITY_IDLE;
  19. // Background mode for Vista, can be slow for many small files.
  20. // if (WinNT()>=WNT_VISTA)
  21. // SetPriorityClass(GetCurrentProcess(),PROCESS_MODE_BACKGROUND_BEGIN);
  22. }
  23. else
  24. if (Priority<7)
  25. {
  26. PriorityClass=IDLE_PRIORITY_CLASS;
  27. PriorityLevel=Priority-4;
  28. }
  29. else
  30. if (Priority==7)
  31. {
  32. PriorityClass=BELOW_NORMAL_PRIORITY_CLASS;
  33. PriorityLevel=THREAD_PRIORITY_ABOVE_NORMAL;
  34. }
  35. else
  36. if (Priority<10)
  37. {
  38. PriorityClass=NORMAL_PRIORITY_CLASS;
  39. PriorityLevel=Priority-7;
  40. }
  41. else
  42. if (Priority==10)
  43. {
  44. PriorityClass=ABOVE_NORMAL_PRIORITY_CLASS;
  45. PriorityLevel=THREAD_PRIORITY_NORMAL;
  46. }
  47. else
  48. {
  49. PriorityClass=HIGH_PRIORITY_CLASS;
  50. PriorityLevel=Priority-13;
  51. }
  52. SetPriorityClass(GetCurrentProcess(),PriorityClass);
  53. SetThreadPriority(GetCurrentThread(),PriorityLevel);
  54. #ifdef RAR_SMP
  55. ThreadPool::SetPriority(PriorityLevel);
  56. #endif
  57. #endif
  58. }
  59. #endif
  60. // Monotonic clock. Like clock(), returns time passed in CLOCKS_PER_SEC items.
  61. // In Android 5+ and Unix usual clock() returns time spent by all threads
  62. // together, so we cannot use it to measure time intervals anymore.
  63. clock_t MonoClock()
  64. {
  65. return clock();
  66. }
  67. void Wait()
  68. {
  69. return; // OPENMPT ADDITION
  70. if (ErrHandler.UserBreak)
  71. ErrHandler.Exit(RARX_USERBREAK);
  72. #if defined(_WIN_ALL) && !defined(SFX_MODULE)
  73. if (SleepTime!=0)
  74. {
  75. static clock_t LastTime=MonoClock();
  76. if (MonoClock()-LastTime>10*CLOCKS_PER_SEC/1000)
  77. {
  78. Sleep(SleepTime);
  79. LastTime=MonoClock();
  80. }
  81. }
  82. #endif
  83. #if defined(_WIN_ALL)
  84. // Reset system sleep timer to prevent system going sleep.
  85. SetThreadExecutionState(ES_SYSTEM_REQUIRED);
  86. #endif
  87. }
  88. #if defined(_WIN_ALL) && !defined(SFX_MODULE)
  89. void Shutdown(POWER_MODE Mode)
  90. {
  91. return; // OPENMPT ADDITION
  92. HANDLE hToken;
  93. TOKEN_PRIVILEGES tkp;
  94. if (OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
  95. {
  96. LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
  97. tkp.PrivilegeCount = 1;
  98. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  99. AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0);
  100. }
  101. if (Mode==POWERMODE_OFF)
  102. ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE,SHTDN_REASON_FLAG_PLANNED);
  103. if (Mode==POWERMODE_SLEEP)
  104. SetSuspendState(FALSE,FALSE,FALSE);
  105. if (Mode==POWERMODE_HIBERNATE)
  106. SetSuspendState(TRUE,FALSE,FALSE);
  107. if (Mode==POWERMODE_RESTART)
  108. ExitWindowsEx(EWX_REBOOT|EWX_FORCE,SHTDN_REASON_FLAG_PLANNED);
  109. }
  110. bool ShutdownCheckAnother(bool Open)
  111. {
  112. const wchar *EventName=L"rar -ioff";
  113. static HANDLE hEvent=NULL;
  114. bool Result=false; // Return false if no other RAR -ioff are running.
  115. if (Open) // Create or open the event.
  116. hEvent=CreateEvent(NULL,FALSE,FALSE,EventName);
  117. else
  118. {
  119. if (hEvent!=NULL)
  120. CloseHandle(hEvent); // Close our event.
  121. // Check if other copies still own the event. While race conditions
  122. // are possible, they are improbable and their harm is minimal.
  123. hEvent=CreateEvent(NULL,FALSE,FALSE,EventName);
  124. Result=GetLastError()==ERROR_ALREADY_EXISTS;
  125. if (hEvent!=NULL)
  126. CloseHandle(hEvent);
  127. }
  128. return Result;
  129. }
  130. #endif
  131. #if defined(_WIN_ALL)
  132. // Load library from Windows System32 folder. Use this function to prevent
  133. // loading a malicious code from current folder or same folder as exe.
  134. HMODULE WINAPI LoadSysLibrary(const wchar *Name)
  135. {
  136. wchar SysDir[NM];
  137. if (GetSystemDirectory(SysDir,ASIZE(SysDir))==0)
  138. return NULL;
  139. MakeName(SysDir,Name,SysDir,ASIZE(SysDir));
  140. return LoadLibrary(SysDir);
  141. }
  142. bool IsUserAdmin()
  143. {
  144. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  145. PSID AdministratorsGroup;
  146. BOOL b = AllocateAndInitializeSid(&NtAuthority,2,SECURITY_BUILTIN_DOMAIN_RID,
  147. DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup);
  148. if (b)
  149. {
  150. if (!CheckTokenMembership( NULL, AdministratorsGroup, &b))
  151. b = FALSE;
  152. FreeSid(AdministratorsGroup);
  153. }
  154. return b!=FALSE;
  155. }
  156. #endif
  157. #ifdef USE_SSE
  158. SSE_VERSION _SSE_Version=GetSSEVersion();
  159. SSE_VERSION GetSSEVersion()
  160. {
  161. int CPUInfo[4];
  162. __cpuid(CPUInfo, 0x80000000);
  163. // Maximum supported cpuid function. For example, Pentium M 755 returns 4 here.
  164. uint MaxSupported=CPUInfo[0] & 0x7fffffff;
  165. if (MaxSupported>=7)
  166. {
  167. __cpuid(CPUInfo, 7);
  168. if ((CPUInfo[1] & 0x20)!=0)
  169. return SSE_AVX2;
  170. }
  171. if (MaxSupported>=1)
  172. {
  173. __cpuid(CPUInfo, 1);
  174. if ((CPUInfo[2] & 0x80000)!=0)
  175. return SSE_SSE41;
  176. if ((CPUInfo[2] & 0x200)!=0)
  177. return SSE_SSSE3;
  178. if ((CPUInfo[3] & 0x4000000)!=0)
  179. return SSE_SSE2;
  180. if ((CPUInfo[3] & 0x2000000)!=0)
  181. return SSE_SSE;
  182. }
  183. return SSE_NONE;
  184. }
  185. #endif