3
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
#include <unistd.h>
|
|
21 |
|
|
22 |
#include "talon_process.h"
|
|
23 |
#include "buffer.h"
|
|
24 |
#include <errno.h>
|
|
25 |
|
|
26 |
#include <windows.h>
|
|
27 |
#include <tchar.h>
|
|
28 |
#include <stdio.h>
|
|
29 |
//#include <strsafe.h>
|
|
30 |
|
|
31 |
|
|
32 |
#include "log.h"
|
|
33 |
|
|
34 |
#define RETURN(x) { retval=x; goto cleanup; }
|
|
35 |
#define CLEANUP() cleanup:
|
|
36 |
|
|
37 |
#define READSIZE 4096
|
|
38 |
|
|
39 |
typedef struct ReadOpStruct {
|
|
40 |
HANDLE semaphore;
|
|
41 |
HANDLE thread;
|
|
42 |
DWORD timeout;
|
|
43 |
DWORD error;
|
|
44 |
HANDLE file;
|
|
45 |
BOOL success;
|
|
46 |
char *space;
|
|
47 |
DWORD nbytes;
|
|
48 |
int id;
|
|
49 |
struct ReadOpStruct *next;
|
|
50 |
} ReadOp;
|
|
51 |
|
|
52 |
typedef struct {
|
|
53 |
ReadOp *first;
|
|
54 |
ReadOp *last;
|
|
55 |
HANDLE semaphore;
|
|
56 |
} ReadOpQ;
|
|
57 |
|
|
58 |
proc *process_new(void)
|
|
59 |
{
|
|
60 |
proc *p = malloc(sizeof(proc));
|
|
61 |
p->output = buffer_new();
|
|
62 |
if (!p->output)
|
|
63 |
{
|
|
64 |
free(p);
|
|
65 |
return NULL;
|
|
66 |
}
|
|
67 |
p->starttime = 0;
|
|
68 |
p->endtime = 0;
|
|
69 |
p->returncode = 1;
|
|
70 |
p->pid = 0;
|
|
71 |
p->causeofdeath = PROC_NORMALDEATH;
|
|
72 |
|
|
73 |
return p;
|
|
74 |
}
|
|
75 |
|
|
76 |
#define TALONMAXERRSTR 1024
|
|
77 |
|
|
78 |
void printlasterror(void)
|
|
79 |
{
|
|
80 |
LPTSTR msg;
|
|
81 |
DWORD err = GetLastError();
|
|
82 |
char buf[1024];
|
|
83 |
|
|
84 |
msg=buf;
|
|
85 |
|
|
86 |
DEBUG(("error %d\n",err));
|
|
87 |
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
88 |
FORMAT_MESSAGE_FROM_SYSTEM |
|
|
89 |
FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
90 |
NULL, // lpSource
|
|
91 |
err, // dwMessageId,
|
|
92 |
0,
|
|
93 |
//MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // dwLanguageId,
|
|
94 |
msg,
|
|
95 |
0,
|
|
96 |
NULL
|
|
97 |
);
|
|
98 |
|
|
99 |
DEBUG(("%s\n",msg));
|
|
100 |
//LocalFree(msg);
|
|
101 |
}
|
|
102 |
|
|
103 |
typedef struct
|
|
104 |
{
|
|
105 |
HANDLE read, write;
|
|
106 |
} tl_stream;
|
|
107 |
|
|
108 |
|
|
109 |
/* Because windows is d**b, there is no way to avoid blocking on an anonymous
|
|
110 |
* pipe. We can't use CancelIO to stop reads since that's only in newer
|
|
111 |
* versions of Win***ws. So what we are left with is putting the read operation
|
|
112 |
* into a thread, timing out in the main body and ignoring this thread if we
|
|
113 |
* feel we have to.
|
|
114 |
* */
|
|
115 |
|
|
116 |
|
|
117 |
DWORD readpipe_thread(void *param)
|
|
118 |
{
|
|
119 |
ReadOpQ *io_ops = (ReadOpQ *)param;
|
|
120 |
ReadOp *iopipe_op;
|
|
121 |
/* have our own buffer since we don't want to risk that the
|
|
122 |
* caller's buffer might have disappeared by the time
|
|
123 |
* our readfile unblocks.
|
|
124 |
*/
|
|
125 |
|
|
126 |
while (1)
|
|
127 |
{
|
|
128 |
DWORD waitres = WaitForSingleObject(io_ops->semaphore, INFINITE);
|
|
129 |
iopipe_op = io_ops->last;
|
|
130 |
|
|
131 |
DEBUG(("readpipe_thread: pre-ReadFile%d: %d \n", iopipe_op->id, iopipe_op->nbytes));
|
|
132 |
iopipe_op->success = ReadFile(iopipe_op->file, iopipe_op->space, iopipe_op->nbytes, &iopipe_op->nbytes, NULL);
|
|
133 |
iopipe_op->error = GetLastError();
|
|
134 |
|
|
135 |
DEBUG(("readpipe_thread: post-ReadFile%d: %d read, err %d\n", iopipe_op->id, iopipe_op->nbytes,iopipe_op->error));
|
|
136 |
ReleaseSemaphore(iopipe_op->semaphore, 1, NULL);
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
proc *process_run(char executable[], char *args[], int timeout)
|
|
141 |
{
|
|
142 |
proc *retval = NULL;
|
|
143 |
char *text;
|
|
144 |
int status;
|
|
145 |
tl_stream stdout_p;
|
|
146 |
tl_stream stdin_p;
|
|
147 |
SECURITY_ATTRIBUTES saAttr;
|
|
148 |
PROCESS_INFORMATION pi;
|
|
149 |
STARTUPINFO si;
|
|
150 |
BOOL createproc_success = FALSE;
|
|
151 |
BOOL timedout = FALSE;
|
|
152 |
TCHAR *commandline = NULL;
|
|
153 |
|
|
154 |
proc *p = process_new();
|
|
155 |
|
|
156 |
if (p == NULL)
|
|
157 |
return NULL;
|
|
158 |
|
|
159 |
/* Make sure pipe handles are inherited */
|
|
160 |
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
161 |
saAttr.bInheritHandle = TRUE;
|
|
162 |
saAttr.lpSecurityDescriptor = NULL;
|
|
163 |
|
|
164 |
p->causeofdeath = PROC_PIPECREATE;
|
|
165 |
|
|
166 |
DEBUG(("making pipes \n"));
|
|
167 |
/* Child's Stdout */
|
|
168 |
if ( ! CreatePipe(&stdout_p.read, &stdout_p.write, &saAttr, 1) )
|
|
169 |
{
|
|
170 |
printlasterror();
|
|
171 |
RETURN(p);
|
|
172 |
}
|
|
173 |
DEBUG(("stdout done \n"));
|
|
174 |
|
|
175 |
/* read handle to the pipe for STDOUT is not inherited */
|
|
176 |
if ( ! SetHandleInformation(stdout_p.read, HANDLE_FLAG_INHERIT, 0) )
|
|
177 |
{
|
|
178 |
printlasterror();
|
|
179 |
RETURN(p);
|
|
180 |
}
|
|
181 |
DEBUG(("stdout noinherit \n"));
|
|
182 |
|
|
183 |
/* a pipe for the child process's STDIN */
|
|
184 |
if ( ! CreatePipe(&stdin_p.read, &stdin_p.write, &saAttr, 0) )
|
|
185 |
{
|
|
186 |
printlasterror();
|
|
187 |
RETURN(p);
|
|
188 |
}
|
|
189 |
DEBUG(("stdin done \n"));
|
|
190 |
|
|
191 |
/* write handle to the pipe for STDIN not inherited */
|
|
192 |
if ( ! SetHandleInformation(stdin_p.read, HANDLE_FLAG_INHERIT, 0) )
|
|
193 |
{
|
|
194 |
printlasterror();
|
|
195 |
RETURN(p);
|
|
196 |
}
|
|
197 |
DEBUG(("pipes done \n"));
|
|
198 |
|
|
199 |
|
|
200 |
p->causeofdeath = PROC_START;
|
|
201 |
|
|
202 |
ZeroMemory( &si, sizeof(STARTUPINFO) );
|
|
203 |
ZeroMemory( &pi, sizeof(PROCESS_INFORMATION) );
|
|
204 |
|
|
205 |
si.cb = sizeof(STARTUPINFO);
|
|
206 |
si.hStdError = stdout_p.write;
|
|
207 |
si.hStdOutput = stdout_p.write;
|
|
208 |
/*
|
|
209 |
Rather than use the stdin pipe, which would be
|
|
210 |
si.hStdInput = stdin_p.read;
|
|
211 |
Pass on talon's own standard input to the child process
|
|
212 |
This helps with programs like xcopy which demand that
|
|
213 |
they are attached to a console and not just any type of
|
|
214 |
input file.
|
|
215 |
*/
|
|
216 |
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
|
|
217 |
si.dwFlags |= STARTF_USESTDHANDLES;
|
|
218 |
|
|
219 |
|
|
220 |
DEBUG(("pre commandline \n"));
|
|
221 |
/* create the commandline string */
|
|
222 |
int len = 0;
|
|
223 |
int i = 0;
|
|
224 |
while (args[i] != NULL)
|
|
225 |
{
|
|
226 |
len += strlen(args[i++]) + 1;
|
|
227 |
}
|
|
228 |
len+=2;
|
|
229 |
|
|
230 |
commandline = malloc(len*2);
|
|
231 |
if (! commandline)
|
|
232 |
RETURN(p);
|
|
233 |
commandline[0] = '\0';
|
|
234 |
|
|
235 |
i = 0;
|
|
236 |
while (args[i] != NULL)
|
|
237 |
{
|
|
238 |
strcat(commandline, args[i]);
|
|
239 |
strcat(commandline, " ");
|
|
240 |
i++;
|
|
241 |
}
|
|
242 |
|
|
243 |
|
|
244 |
/* Get the read thread ready to go before creating
|
|
245 |
* the process.
|
|
246 |
*/
|
|
247 |
ReadOpQ *ropq = malloc(sizeof(ReadOpQ));
|
|
248 |
|
|
249 |
ropq->first=NULL;
|
|
250 |
ropq->last=NULL;
|
|
251 |
ropq->semaphore = CreateSemaphore(NULL, 0, 1, NULL);
|
|
252 |
DEBUG(("Creating read thread. \n"));
|
|
253 |
|
|
254 |
DWORD readpipe_threadid;
|
|
255 |
HANDLE h_readpipe_thread = CreateThread(NULL, 8192, (LPTHREAD_START_ROUTINE) readpipe_thread, (void*)ropq, 0, &readpipe_threadid);
|
|
256 |
|
|
257 |
/* ready to run the process */
|
|
258 |
|
|
259 |
DEBUG(("process commandline:\n%s \n", commandline));
|
|
260 |
DEBUG(("\n"));
|
|
261 |
createproc_success = CreateProcess(executable,
|
|
262 |
commandline, // command line
|
|
263 |
NULL, // process security attributes
|
|
264 |
NULL, // primary thread security attributes
|
|
265 |
TRUE, // handles are inherited
|
|
266 |
0, // creation flags
|
|
267 |
NULL, // use parent's environment
|
|
268 |
NULL, // use parent's current directory
|
|
269 |
&si, // STARTUPINFO pointer
|
|
270 |
&pi); // receives PROCESS_INFORMATION
|
|
271 |
|
|
272 |
if (! createproc_success)
|
|
273 |
{
|
|
274 |
DEBUG(("Createprocess failed. \n"));
|
|
275 |
p->causeofdeath = PROC_SOMEODDDEATH;
|
|
276 |
RETURN(p);
|
|
277 |
}
|
|
278 |
|
|
279 |
int have_status = 0;
|
|
280 |
|
|
281 |
|
|
282 |
DEBUG(("Closing Handles. \n"));
|
|
283 |
if (!CloseHandle(stdout_p.write))
|
|
284 |
RETURN(p);
|
|
285 |
if (!CloseHandle(stdin_p.read))
|
|
286 |
RETURN(p);
|
|
287 |
|
|
288 |
DEBUG(("Closed Handles. \n"));
|
|
289 |
|
|
290 |
|
|
291 |
static int id=0;
|
|
292 |
do
|
|
293 |
{
|
|
294 |
char *space = buffer_makespace(p->output, READSIZE);
|
|
295 |
|
|
296 |
DWORD waitres;
|
|
297 |
ReadOp *iopipe_op = malloc(sizeof(ReadOp));
|
|
298 |
iopipe_op->semaphore = CreateSemaphore(NULL, 0, 1, NULL);
|
|
299 |
iopipe_op->thread = h_readpipe_thread;
|
|
300 |
iopipe_op->timeout = timeout;
|
|
301 |
iopipe_op->file = stdout_p.read;
|
|
302 |
iopipe_op->space = malloc(READSIZE);
|
|
303 |
iopipe_op->id = id++;
|
|
304 |
iopipe_op->nbytes = READSIZE;
|
|
305 |
iopipe_op->next = NULL;
|
|
306 |
|
|
307 |
if (!ropq->first)
|
|
308 |
{
|
|
309 |
ropq->first = iopipe_op;
|
|
310 |
ropq->last = iopipe_op;
|
|
311 |
} else {
|
|
312 |
ropq->last->next = iopipe_op;
|
|
313 |
ropq->last = iopipe_op;
|
|
314 |
}
|
|
315 |
|
|
316 |
ReleaseSemaphore(ropq->semaphore, 1, NULL);
|
|
317 |
|
|
318 |
DEBUG(("waiting for read %d\n", timeout));
|
|
319 |
waitres = WaitForSingleObject(iopipe_op->semaphore, timeout);
|
|
320 |
DEBUG(("read wait finished result= %d\n", waitres));
|
|
321 |
|
|
322 |
if (waitres != WAIT_OBJECT_0)
|
|
323 |
{
|
|
324 |
DEBUG(("timeout \n"));
|
|
325 |
timedout = TRUE;
|
|
326 |
break;
|
|
327 |
}
|
|
328 |
else
|
|
329 |
{
|
|
330 |
DEBUG(("read signalled: nbytes: %d \n", iopipe_op->nbytes));
|
|
331 |
if (iopipe_op->nbytes <= 0)
|
|
332 |
{
|
|
333 |
break;
|
|
334 |
}
|
|
335 |
memcpy(space, iopipe_op->space, iopipe_op->nbytes);
|
|
336 |
buffer_usespace(p->output, iopipe_op->nbytes);
|
|
337 |
DEBUG(("buffer took on nbytes: %d \n", iopipe_op->nbytes));
|
|
338 |
}
|
|
339 |
}
|
|
340 |
while (1);
|
|
341 |
|
|
342 |
if (timedout == FALSE)
|
|
343 |
{
|
|
344 |
DEBUG(("Wait for process exit\n"));
|
|
345 |
// Wait until child process exits.
|
|
346 |
WaitForSingleObject(pi.hProcess, INFINITE);
|
|
347 |
DEBUG(("Process exited\n"));
|
|
348 |
|
|
349 |
DWORD exitcode;
|
|
350 |
|
|
351 |
if (GetExitCodeProcess(pi.hProcess, &exitcode))
|
|
352 |
{
|
|
353 |
p->causeofdeath = PROC_NORMALDEATH;
|
|
354 |
p->returncode = exitcode;
|
|
355 |
DEBUG(("process exited normally = %d:\n", p->returncode));
|
|
356 |
RETURN(p);
|
|
357 |
} else {
|
|
358 |
p->causeofdeath = PROC_SOMEODDDEATH;
|
|
359 |
p->returncode = 128;
|
|
360 |
DEBUG(("process terminated \n"));
|
|
361 |
RETURN(p);
|
|
362 |
}
|
|
363 |
} else {
|
|
364 |
TerminateProcess(pi.hProcess,1);
|
|
365 |
p->causeofdeath = PROC_TIMEOUTDEATH;
|
|
366 |
p->returncode = 128;
|
|
367 |
DEBUG(("process timedout \n"));
|
|
368 |
RETURN(p);
|
|
369 |
}
|
|
370 |
|
|
371 |
/* Clean up the read operation queue
|
|
372 |
ReadOp *r = ropq.first;
|
|
373 |
do
|
|
374 |
{
|
|
375 |
CloseHandle(r->semaphore);
|
|
376 |
free(r->space);
|
|
377 |
free(r);
|
|
378 |
r = r->next;
|
|
379 |
} while (r != NULL); */
|
|
380 |
|
|
381 |
CLEANUP();
|
|
382 |
if (retval == NULL)
|
|
383 |
{
|
|
384 |
if (p)
|
|
385 |
process_free(&p);
|
|
386 |
}
|
|
387 |
if (commandline)
|
|
388 |
free(commandline);
|
|
389 |
|
|
390 |
return retval;
|
|
391 |
}
|
|
392 |
|
|
393 |
void process_free(proc **pp)
|
|
394 |
{
|
|
395 |
if (!pp)
|
|
396 |
return;
|
|
397 |
if (! *pp)
|
|
398 |
return;
|
|
399 |
|
|
400 |
if ((*pp)->output)
|
|
401 |
buffer_free(&((*pp)->output));
|
|
402 |
|
|
403 |
free(*pp);
|
|
404 |
|
|
405 |
*pp = NULL;
|
|
406 |
}
|