forked from engor/Jentos.Code
-
Notifications
You must be signed in to change notification settings - Fork 2
/
proc.cpp
executable file
·433 lines (333 loc) · 9.33 KB
/
proc.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
Ted, a simple text editor/IDE.
Copyright 2012, Blitz Research Ltd.
See LICENSE.TXT for licensing terms.
*/
#include "proc.h"
extern void cdebug( const QString &q );
#ifdef Q_OS_WIN
#define CLOSE CloseHandle
#include <tlhelp32.h>
//kill 'em all!
//
static bool killpg( HANDLE proc ){
QMap<DWORD,DWORD> procs;
QMap<DWORD,QString> procExes;
HANDLE snapshot=CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS,0 );
if( snapshot!=INVALID_HANDLE_VALUE ){
PROCESSENTRY32 pentry={sizeof(pentry)};
int more=Process32First( snapshot,&pentry );
while( more ){
procs.insert( pentry.th32ProcessID,pentry.th32ParentProcessID );
procExes.insert( pentry.th32ProcessID,QString::fromStdWString( std::wstring(pentry.szExeFile) ) );
more=Process32Next( snapshot,&pentry );
}
DWORD procid=GetProcessId( proc );
QSet<DWORD> pset;
QMapIterator<DWORD,DWORD> it( procs );
while( it.hasNext() ){
it.next();
DWORD pid=it.key();
DWORD ppid=it.value();
pset.clear();
while( ppid && ppid!=procid && procs.contains( ppid ) && !pset.contains( ppid ) ){
ppid=procs.value( ppid );
pset.insert( ppid );
}
if( ppid==procid ){
HANDLE child=OpenProcess( PROCESS_TERMINATE,0,pid );
if( child ){
if( TerminateProcess( child,-1 ) ){
WaitForSingleObject( child,INFINITE );
}else{
qDebug()<<"Failed to terminate child process"<<procExes.value(pid);
}
CloseHandle( child );
}else{
qDebug()<<"Failed to open child process"<<procExes.value(pid);
}
}
}
CloseHandle( snapshot );
}else{
qDebug()<<"Failed to create process snapshot";
}
if( !TerminateProcess( proc,-1 ) ){
qDebug()<<"Failed to terminate process";
return false;
}
return true;
}
#else
#define CLOSE close
#include <signal.h>
#include <sys/wait.h>
#define PIPE_READ 0
#define PIPE_WRITE 1
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
static char **makeargv( const char *cmd ){
int n,c;
char *p;
static char *args,**argv;
if( args ) free( args );
if( argv ) free( argv );
args=(char*)malloc( strlen(cmd)+1 );
strcpy( args,cmd );
n=0;
p=args;
while( (c=*p++) ){
if( c==' ' ){
continue;
}else if( c=='\"' ){
while( *p && *p!='\"' ) ++p;
}else{
while( *p && *p!=' ' ) ++p;
}
if( *p ) ++p;
++n;
}
argv=(char**)malloc( (n+1)*sizeof(char*) );
n=0;
p=args;
while( (c=*p++) ){
if( c==' ' ){
continue;
}else if( c=='\"' ){
argv[n]=p;
while( *p && *p!='\"' ) ++p;
}else{
argv[n]=p-1;
while( *p && *p!=' ' ) ++p;
}
if( *p ) *p++=0;
++n;
}
argv[n]=0;
return argv;
}
#endif
#define INIT 0
#define RUNNING 1
#define FINISHED 2
#define DELETING 3
Process::Process( QObject *parent ):QObject( parent ),_state( INIT ){
}
bool Process::start( const QString &cmd ){
if( _state!=INIT ) return false;
#ifdef Q_OS_WIN
HANDLE in[2],out[2],err[2];
SECURITY_ATTRIBUTES sa={sizeof(sa),0,1};
CreatePipe( &in[0],&in[1],&sa,0 );
CreatePipe( &out[0],&out[1],&sa,0 );
CreatePipe( &err[0],&err[1],&sa,0 );
STARTUPINFOW si={sizeof(si)};
si.dwFlags=STARTF_USESTDHANDLES|STARTF_USESHOWWINDOW;
si.hStdInput=in[0];
si.hStdOutput=out[1];
si.hStdError=err[1];
si.wShowWindow=SW_HIDE;
PROCESS_INFORMATION pi={0};
int res=CreateProcessW( 0,(LPWSTR)cmd.toStdWString().c_str(),0,0,-1,CREATE_NEW_PROCESS_GROUP,0,0,&si,&pi );
CloseHandle( in[0] );
CloseHandle( out[1] );
CloseHandle( err[1] );
if( !res ){
CloseHandle( in[1] );
CloseHandle( out[0] );
CloseHandle( err[0] );
return false;
}
CloseHandle( pi.hThread );
_pid=pi.hProcess;
_in=in[1];
_out=out[0];
_err=err[0];
#else
int in[2],out[2],err[2];
pipe( in );
pipe( out );
pipe( err );
_pid=vfork();
if( !_pid ){
#if __linux
setsid();
#else
setpgid(0,0);
#endif
dup2( in[PIPE_READ],STDIN_FILENO );
dup2( out[PIPE_WRITE],STDOUT_FILENO );
dup2( err[PIPE_WRITE],STDERR_FILENO );
close( in[PIPE_READ] );
close( out[PIPE_WRITE] );
close( err[PIPE_WRITE] );
close( in[PIPE_WRITE] );
close( out[PIPE_READ] );
close( err[PIPE_READ] );
char **argv=makeargv( cmd.toStdString().c_str() );
execvp( argv[0],argv );
_exit( -1 );
return false;
}
close( in[PIPE_READ] );
close( out[PIPE_WRITE] );
close( err[PIPE_WRITE] );
if( _pid==-1 ){
close( in[PIPE_WRITE] );
close( out[PIPE_READ] );
close( err[PIPE_READ] );
return false;
}
_in=in[PIPE_WRITE];
_out=out[PIPE_READ];
_err=err[PIPE_READ];
#endif
_procwaiter=new ProcWaiter( _pid );
connect( _procwaiter,SIGNAL(finished()),SLOT(onFinished()) );
_linereaders[0]=new LineReader( _out );
connect( _linereaders[0],SIGNAL(finished()),SLOT(onFinished()) );
_linereaders[1]=new LineReader( _err );
connect( _linereaders[1],SIGNAL(finished()),SLOT(onFinished()) );
_procwaiter->start();
_linereaders[0]->start();
_linereaders[1]->start();
_state=RUNNING;
return true;
}
Process::~Process(){
if( _state!=RUNNING && _state!=FINISHED ) return;
_state=DELETING;
if( _procwaiter->wait( 10000 ) ) delete _procwaiter; else cdebug( "Timeout waiting for process to finish" );
if( _linereaders[0]->wait( 1000 ) ) delete _linereaders[0]; else cdebug( "Timeout waiting for stdout reader to finish" );
if( _linereaders[1]->wait( 1000 ) ) delete _linereaders[1]; else cdebug( "Timeout waiting for stderr reader to finish" );
CLOSE( _in );
CLOSE( _out );
CLOSE( _err );
}
bool Process::wait(){
if( _state!=RUNNING ) return _state==FINISHED;
return _procwaiter->wait( 30000 );
}
bool Process::kill(){
qDebug()<<"Killing proc";
if( _state!=RUNNING ) return _state==FINISHED;
_linereaders[0]->kill();
_linereaders[1]->kill();
#ifdef Q_OS_WIN
if( killpg( _pid ) ) return true;
#else
if( killpg( _pid,SIGTERM )>=0 ) return true;
#endif
qDebug()<<"proc killed";
return false;
}
bool Process::writeLine( const QString &line ){
QString buf=line+'\n';
#ifdef Q_OS_WIN
DWORD n=0;
return WriteFile( _in,buf.toStdString().c_str(),buf.length(),&n,0 ) && n==buf.length();
#else
return write( _in,buf.toStdString().c_str(),buf.length() )==buf.length();
#endif
}
bool Process::isEof( int channel ){
return _linereaders[channel]->isEof();
}
bool Process::isLineAvailable( int channel ){
return _linereaders[channel]->isLineAvailable();
}
bool Process::waitLineAvailable( int channel,int millis ){
return _linereaders[channel]->waitLineAvailable( millis );
}
QString Process::readLine( int channel ){
return _linereaders[channel]->readLine();
}
void Process::onFinished(){
if( _state!=RUNNING ) return;
if( sender()==_procwaiter ){
_state=FINISHED;
emit finished();
return;
}
for( int channel=0;channel<2;++channel ){
if( sender()==_linereaders[channel] ){
if( _linereaders[channel]->isLineAvailable() ){
emit lineAvailable( channel );
}
return;
}
}
}
// ***** Proc waiter *****
ProcWaiter::ProcWaiter( pid_t2 pid ):_pid(pid){
}
void ProcWaiter::run(){
#ifdef Q_OS_WIN
WaitForSingleObject( _pid,INFINITE );
CloseHandle( _pid );
#else
int status=0;
waitpid( _pid,&status,0 );
#endif
}
// ***** Line reader *****
LineReader::LineReader( fd_t fd ):_fd(fd),_eof(false),_avail(false){
}
void LineReader::kill(){
_eof=true;
terminate();
}
bool LineReader::isEof(){
return _eof;
}
bool LineReader::isLineAvailable(){
return _avail;
}
bool LineReader::waitLineAvailable( int millis ){
if( !_avail && !_eof ){
if( !wait( millis ) ) cdebug( "Timeout waiting for process output" );
}
return _avail;
}
QString LineReader::readLine(){
if( !waitLineAvailable( 10000 ) ) return "";
QString line=_line;
_avail=false;
if( !_eof ) start();
return line;
}
int LineReader::readChar(){
unsigned char c;
#ifdef Q_OS_WIN
DWORD n=0;
if( ReadFile( _fd,&c,1,&n,0 ) && n==1 ) return c;
#else
if( read( _fd,&c,1 )==1 ) return c;
#endif
return -1;
}
void LineReader::run(){
_buf.clear();
for(;;){
int c=readChar();
if( c==-1 ){
if( _buf.count() ){
_buf.push_back( 0 );
_line=QString( _buf.constData() );
_avail=true;
}else{
_line="";
}
_eof=true;
return;
}else if( c=='\n' ){
_buf.push_back( 0 );
_line=QString( _buf.constData() );
_avail=true;
return;
}else if( c!='\r' ){
_buf.push_back( c );
}
}
}