unrarsrc  6.1.7
About: unrar extracts, views and tests the contents of archives created with the RAR archiver.
  Fossies Dox: unrarsrc-6.1.7.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

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