-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows.cpp
349 lines (309 loc) · 8.85 KB
/
windows.cpp
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "util.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <io.h>
#include <direct.h>
#include <errno.h>
#include <string>
#include <map>
#pragma warning (disable:4996)
using namespace std;
static const char* tmpdir() {
const char* r=getenv("TMP");
if(!r) r=getenv("TEMP");
if(!r) {
r="c:\\";
}
return r;
}
#define REGISTRY_KEY "Software\\LOCAL\\ExecServer"
const struct configuration *cfg(void) {
static struct configuration* _cfg=NULL;
if(_cfg) return _cfg;
_cfg=new struct configuration;
_cfg->start_dir=tmpdir();
_cfg->listen_port=DEFAULT_PORT;
/* read registry */
HKEY hkey;
if(RegOpenKey(HKEY_LOCAL_MACHINE, REGISTRY_KEY, &hkey) == ERROR_SUCCESS) {
char value[256];
LONG len;
len=sizeof(value);
if(RegQueryValue(hkey,"StartDir",value,&len)==ERROR_SUCCESS) {
if(len>0 && len<sizeof(value)) _cfg->start_dir=strdup(value);
}
len=sizeof(value);
if(RegQueryValue(hkey,"ListenPort",value,&len)==ERROR_SUCCESS) {
value[sizeof(value)-1]='\0'; // just for sanity
int port=atoi(value);
if(port) _cfg->listen_port=port;
}
RegCloseKey(hkey);
}
return _cfg;
}
void clear_error() {
::SetLastError(0);
errno=0;
}
int get_os_error(const char** pdesc) {
// If we have posix-like error, return it
*pdesc=NULL;
int e=errno;
if(e) {
*pdesc=strerror(e);
return e;
}
// Otherwise deal with GetLastError()
DWORD we=::GetLastError();
if(we) {
static char errbuf[512];
if(::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL,
we, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
errbuf, sizeof(errbuf), NULL) == 0) {
sprintf(errbuf,"Unknown error %08x",(unsigned)we);
}
*pdesc=errbuf;
return we;
}
// No error
return 0;
}
static int rmfile(char const* fname) {
struct stat st;
if(::stat(fname, &st)<0) return -1;
if(st.st_mode & _S_IFDIR) {
return rmdir_recursive(fname);
} else {
return unlink(fname);
}
}
int rmdir_recursive(char const* dirname) {
struct _finddata_t d;
char wildcard[_MAX_PATH];
_snprintf(wildcard, sizeof(wildcard), "%s/*", dirname);
intptr_t p=_findfirst(wildcard, &d);
if(p==-1) {
if(errno==ENOENT) { /* empty dir */
errno=0;
goto done;
} else {
return -1;
}
}
do {
char fname[_MAX_PATH];
if(strcmp(d.name,".")==0 || strcmp(d.name,"..")==0) continue;
_snprintf(fname,sizeof(fname), "%s/%s",dirname, d.name);
if(rmfile(fname)<0) {
_findclose(p);
return -1;
}
} while(_findnext(p, &d)==0);
done:
if(p!=-1) _findclose(p);
clear_error();
return rmdir(dirname);
}
map<int, HANDLE> pid_table;
static HANDLE get_process_handle(int pid) {
if(pid_table.find(pid) != pid_table.end())
return pid_table[pid];
HANDLE h=::OpenProcess(
PROCESS_QUERY_INFORMATION|PROCESS_TERMINATE|SYNCHRONIZE,
FALSE, (DWORD)pid);
if(!h)
return h;
pid_table[pid]=h;
return h;
}
int pkill(int pid) {
HANDLE h=get_process_handle(pid);
if(!h)
return -1;
::TerminateProcess(h, 254);
DWORD rc;
if(!::GetExitCodeProcess(h,&rc)) {
rc=(DWORD)(-1);
}
// pid_table.erase(pid);
// ::CloseHandle(h);
return (int)rc;
}
int pwait(int pid, unsigned seconds) {
HANDLE h=get_process_handle(pid);
if(!h)
return -2;
DWORD wr=::WaitForSingleObject(h, seconds*1000);
if(wr==WAIT_TIMEOUT) {
return -1;
}
DWORD rc;
if(::GetExitCodeProcess(h,&rc)) {
pid_table.erase(pid);
::CloseHandle(h);
return (int)rc;
} else {
return -1;
}
}
static string quote_arg(const string& arg) {
bool q=false;
if(arg.empty())
q=true;
if(arg.find_first_of(" \t\"") != string::npos)
q=true;
if(q) {
string r("\"");
for(string::const_iterator p=arg.begin(), e=arg.end(); p!=e; ++p) {
if(*p=='\\')
r.append("\\\\");
else if(*p=='"')
r.append("\\\"");
else
r.push_back(*p);
}
r.push_back('"');
return r;
} else {
return arg;
}
}
static HANDLE open_read(const char* fname) {
SECURITY_ATTRIBUTES sa;
SECURITY_DESCRIPTOR sd;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = &sd;
if(::InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION) == FALSE)
return INVALID_HANDLE_VALUE;
if(::SetSecurityDescriptorDacl(&sd, TRUE, (PACL)NULL, FALSE) == FALSE)
return INVALID_HANDLE_VALUE;
return ::CreateFile(fname, FILE_READ_DATA, FILE_SHARE_READ,
&sa, OPEN_EXISTING, 0, NULL);
}
static HANDLE open_write(const char* fname) {
SECURITY_ATTRIBUTES sa;
SECURITY_DESCRIPTOR sd;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = &sd;
if(::InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION) == FALSE)
return INVALID_HANDLE_VALUE;
if(::SetSecurityDescriptorDacl(&sd, TRUE, (PACL)NULL, FALSE) == FALSE)
return INVALID_HANDLE_VALUE;
return ::CreateFile(fname, FILE_WRITE_DATA|FILE_APPEND_DATA,
FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
&sa, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
}
#define NULFILE "nul"
int pspawn(const char* const* argv, const char* const* envp, const char* cwd,
const char* fstdin, const char* fstdout, const char* fstderr) {
int res=-1;
char* newenv=NULL;
STARTUPINFO si;
memset(&si,0,sizeof(si));
si.cb=sizeof(si);
si.dwFlags=STARTF_USESTDHANDLES;
PROCESS_INFORMATION pi;
memset(&pi,0,sizeof(pi));
/* make command line */
char* cmd=NULL;
{
std::string cmdline;
for(const char* const* a=argv; *a; ++a) {
if(!cmdline.empty())
cmdline += " ";
cmdline += quote_arg(*a);
}
cmd=new char[cmdline.size()+1];
strcpy(cmd, cmdline.c_str());
}
if(envp) {
/* prepare environment */
int esize=0;
for(const char* const* e=envp; *e; ++e)
esize += strlen(*e)+1;
esize++;
newenv=new char[esize];
char* s=newenv;
for(const char* const* e=envp; *e; ++e) {
strcpy(s, *e);
s += strlen(*e);
*s++ = 0;
}
*s++ = 0;
}
/* open files */
if(!fstdin)
fstdin=NULFILE;
si.hStdInput=open_read(fstdin);
if(si.hStdInput==INVALID_HANDLE_VALUE)
goto cleanup;
if(!fstdout)
fstdout=NULFILE;
si.hStdOutput=open_write(fstdout);
if(si.hStdOutput==INVALID_HANDLE_VALUE)
goto cleanup;
if(!fstderr)
fstderr=NULFILE;
si.hStdError=open_write(fstderr);
if(si.hStdError==INVALID_HANDLE_VALUE)
goto cleanup;
/* run */
if(::CreateProcess(NULL, cmd, NULL, NULL,
TRUE, 0,
newenv, cwd, &si, &pi)) {
/* we don't need handles */
res=(int)pi.dwProcessId;
pid_table[res]=pi.hProcess;
::CloseHandle(pi.hThread);
}
cleanup:
if(cmd) {
delete[] cmd;
cmd=NULL;
}
if(newenv) {
delete[] newenv;
newenv=NULL;
}
if(si.hStdInput != 0 && si.hStdInput != INVALID_HANDLE_VALUE)
::CloseHandle(si.hStdInput);
if(si.hStdOutput != 0 && si.hStdOutput != INVALID_HANDLE_VALUE)
::CloseHandle(si.hStdOutput);
if(si.hStdError != 0 && si.hStdError != INVALID_HANDLE_VALUE)
::CloseHandle(si.hStdError);
return res;
}
int uname(struct utsname *name) {
OSVERSIONINFO ovi;
SYSTEM_INFO si;
memset(name,0,sizeof(*name));
ovi.dwOSVersionInfoSize=sizeof(ovi);
GetVersionEx(&ovi);
const char* sysn="Windows?";
switch (ovi.dwPlatformId) {
case VER_PLATFORM_WIN32_NT: sysn="WinNT"; break;
case VER_PLATFORM_WIN32_WINDOWS: sysn="Win9x"; break;
case VER_PLATFORM_WIN32s: sysn="Win32s"; break;
}
strncpy(name->sysname, sysn, sizeof(name->sysname));
DWORD nn=sizeof(name->nodename);
GetComputerNameExA(ComputerNameDnsFullyQualified, name->nodename, &nn);
sprintf(name->release, "%u.%u.%u",
ovi.dwMajorVersion, ovi.dwMinorVersion, ovi.dwBuildNumber);
strncpy(name->version, ovi.szCSDVersion, sizeof(name->version));
GetSystemInfo(&si);
const char* arch="Unknown";
switch (si.wProcessorArchitecture) {
case PROCESSOR_ARCHITECTURE_INTEL: arch="i386"; break;
case PROCESSOR_ARCHITECTURE_IA64: arch="ia64"; break;
case PROCESSOR_ARCHITECTURE_AMD64: arch="x86_64"; break;
}
strncpy(name->machine, arch, sizeof(name->machine));
return 0;
}