This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runcpu.c
261 lines (234 loc) · 7.16 KB
/
runcpu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* runcpu.c : run a process given its commandline without showing its window
* and set its affinity mask to the one given as a parameter of the program. */
#include <windows.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <strsafe.h>
#define ErrorMsgBox(msg,title) \
do \
{ \
MessageBox(NULL,msg,title,MB_OK | MB_ICONSTOP); \
} while(0)
/* per http://alter.org.ua/en/docs/win/args/, since winapi is too cool to have
* its own CommandLineToArgvA despite having CommandLineToArgvW. modified
* to return when it recognizes the start of the process commandline, e.g.
* --. the location of the process commandline is written to *cmdLineStart.
* this avoids unnecessary copying of a string that only gets passed directly
* into CreateProcess() anyway. also, it's not necessary to append the extra
* quotation marks, which are essential for proper process execution. */
PCHAR* CommandLineToArgvA(PCHAR CmdLine, int* _argc, PCHAR *cmdLineStart)
{
PCHAR* argv;
PCHAR _argv;
ULONG len;
ULONG argc;
CHAR a;
ULONG i, j;
BOOLEAN in_QM;
BOOLEAN in_TEXT;
BOOLEAN in_SPACE;
len = strlen(CmdLine);
i = ((len+2)/2)*sizeof(PVOID) + sizeof(PVOID);
argv = (PCHAR*)HeapAlloc(GetProcessHeap(),0,i + (len+2)*sizeof(CHAR));
_argv = (PCHAR)(((PUCHAR)argv)+i);
argc = 0;
argv[argc] = _argv;
in_QM = FALSE;
in_TEXT = FALSE;
in_SPACE = TRUE;
i = 0;
j = 0;
while( a = CmdLine[i] )
{
if(in_QM)
{
if(a == '\"')
{
in_QM = FALSE;
}
else
{
_argv[j] = a;
j++;
}
}
else
{
/* check for the "--" sequence. if it starts here, skip the white
* space and write the beginning of the actual commandline to
* *cmdLineStart. */
if(a == '-' && CmdLine[i+1] == '-')
{
++i;
while((a = CmdLine[++i]) == ' ' || a == '\t' || a == '\n'
|| a == '\r'); /* skip whitespace */
*cmdLineStart = CmdLine + i;
break;
}
switch(a)
{
case '\"':
in_QM = TRUE;
in_TEXT = TRUE;
if(in_SPACE)
{
argv[argc] = _argv+j;
argc++;
}
in_SPACE = FALSE;
break;
case ' ':
case '\t':
case '\n':
case '\r':
if(in_TEXT)
{
_argv[j] = '\0';
j++;
}
in_TEXT = FALSE;
in_SPACE = TRUE;
break;
default:
in_TEXT = TRUE;
if(in_SPACE)
{
argv[argc] = _argv+j;
argc++;
}
_argv[j] = a;
j++;
in_SPACE = FALSE;
break;
}
}
i++;
}
_argv[j] = '\0';
argv[argc] = NULL;
(*_argc) = argc;
return argv;
}
/* return a human-readable explanation of GetLastError() made with
* FormatMessage() and formatted according to WIN_STRERROR_TEMPLATE defined
* above. the returned value must be freed with HeapFree() and GetProcessHeap()
* when it's no longer needed. */
char* win_strerror(const char* function)
{
LPTSTR lpMsgBuf, lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
lpDisplayBuf = (LPTSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
(lstrlen((LPCSTR)lpMsgBuf) + lstrlen((LPCTSTR)function) + 40)
* sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) /
sizeof(TCHAR), "%s failed with %d : %s", function, dw, lpMsgBuf);
LocalFree(lpMsgBuf);
return (char*)lpDisplayBuf;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpszCmd,
int nCmd)
{
STARTUPINFO sProcStartupInfo;
PROCESS_INFORMATION sProcInformation;
DWORD dwNewProcessAffinity;
int argc, i, rv = 1;
char **argv, *szProcCmdLine, *szLastErrorMsg;
LPCTSTR lpProcWorkingDir = NULL;
argv = CommandLineToArgvA(GetCommandLine(),&argc,&szProcCmdLine);
if(argc < 2)
{
ErrorMsgBox("Usage : [-a affinity] [-d working_dir] -- <cmdline>",
"Improper parameters");
goto beach;
}
for(i=1; i<argc; ++i)
{
if(strcmp(argv[i],"-a") == 0)
{
++i;
if(i >= argc)
{
ErrorMsgBox("No data after -a.","Improper parameters");
goto beach;
}
else if(sscanf_s(argv[i],"%8x",&dwNewProcessAffinity) != 1)
{
ErrorMsgBox("Improper affinity specification",
"Improper parameters");
goto beach;
}
}
else if(strcmp(argv[i],"-d") == 0)
{
++i;
if(i >= argc)
{
ErrorMsgBox("No data after -d.","Improper parameters");
goto beach;
}
else lpProcWorkingDir = argv[i];
}
}
/* prepare the STARTUPINFO structure. in our case, it is desired to hide
* the window of the launched process. */
ZeroMemory(&sProcStartupInfo, sizeof(sProcStartupInfo));
sProcStartupInfo.cb = sizeof(sProcStartupInfo);
sProcStartupInfo.dwFlags = STARTF_USESHOWWINDOW;
sProcStartupInfo.wShowWindow = SW_HIDE;
ZeroMemory(&sProcInformation, sizeof(sProcInformation));
/* create the process by specifying its commandline. the process is created
* as a suspended one. it is later resumed with ResumeThread() after setting
* its affinity. therefore, the process will not start its execution until
* the desired affinity is set. */
if(CreateProcess(NULL, szProcCmdLine, NULL, NULL, FALSE,
CREATE_SUSPENDED, NULL, lpProcWorkingDir, &sProcStartupInfo,
&sProcInformation) == 0)
{
szLastErrorMsg = win_strerror("CreateProcess()");
ErrorMsgBox(szLastErrorMsg, "WinAPI call failed");
HeapFree(GetProcessHeap(),0,szLastErrorMsg);
return 1;
}
/* set the affinity mask for the process. */
if(SetProcessAffinityMask(sProcInformation.hProcess, dwNewProcessAffinity)
== 0)
{
szLastErrorMsg = win_strerror("SetProcessAffinityMask()");
ErrorMsgBox(szLastErrorMsg, "WinAPI call failed");
HeapFree(GetProcessHeap(),0,szLastErrorMsg);
if(TerminateProcess(sProcInformation.hProcess, 255) == 0)
{
szLastErrorMsg = win_strerror("TerminateProcess()");
ErrorMsgBox(szLastErrorMsg, "WinAPI call failed");
HeapFree(GetProcessHeap(),0,szLastErrorMsg);
}
goto beach2;
}
/* and resume (actually, start) its execution. */
if(ResumeThread(sProcInformation.hThread) == -1)
{
szLastErrorMsg = win_strerror("ResumeThread()");
ErrorMsgBox(szLastErrorMsg, "WinAPI call failed");
HeapFree(GetProcessHeap(),0,szLastErrorMsg);
if(TerminateProcess(sProcInformation.hProcess, 255) == 0)
{
szLastErrorMsg = win_strerror("TerminateProcess()");
ErrorMsgBox(szLastErrorMsg, "WinAPI call failed");
HeapFree(GetProcessHeap(),0,szLastErrorMsg);
}
goto beach2;
}
/* process launched successfully. */
rv = 0;
beach2:
/* close handles to the created process and its main thread, and exit. */
CloseHandle(sProcInformation.hProcess);
CloseHandle(sProcInformation.hThread);
beach:
HeapFree(GetProcessHeap(),0,argv);
return rv;
}