3
|
1 |
/*
|
|
2 |
* Copyright (c) 2008-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 |
descramble.cpp:
|
|
21 |
|
|
22 |
Description
|
|
23 |
-----------
|
|
24 |
"descramble" is a program that buffers standard input until end of file
|
|
25 |
and then copies the buffers to the standard output in such a way that
|
|
26 |
if there are many copies of descramble running, each will be given an
|
|
27 |
opportunity to write the complete contents of its buffers while the
|
|
28 |
others wait.
|
|
29 |
|
|
30 |
Purpose
|
|
31 |
-------
|
|
32 |
descramble is used to ensure that output from multiple concurrent processes
|
|
33 |
is not interleaved. It is useful in build systems such as GNU make with
|
|
34 |
the -j switch, although it requires that makefiles are changed before it can
|
|
35 |
work.
|
|
36 |
|
|
37 |
Design
|
|
38 |
------
|
|
39 |
This program may be built on Linux or on Windows. On both platforms the
|
|
40 |
basic behavior is similar:
|
|
41 |
1) Read standard input into buffers. Allocate these dynamically
|
|
42 |
so that there is no limit.
|
|
43 |
2) Wait on a named or system-wide semaphore.
|
|
44 |
3) Output all the buffers to stdout.
|
|
45 |
|
|
46 |
The name of the semaphore is a parameter and should be chosen so that multiple
|
|
47 |
instances of the build system (or whatever process is running many tasks with
|
|
48 |
descramble) cannot block each other.
|
|
49 |
|
|
50 |
|
|
51 |
Special Considerations for Linux
|
|
52 |
--------------------------------
|
|
53 |
A named semaphore is a file in the filesystem. It is not automatically removed
|
|
54 |
when a process exits. descramble provides a "stop" parameter to be run at the
|
|
55 |
"end" of the build system (or other process) that will clean away this semaphore.
|
|
56 |
|
|
57 |
|
|
58 |
Special Considerations for Windows
|
|
59 |
----------------------------------
|
|
60 |
The windows implementation is built with the MINGW toolchain. On windows
|
|
61 |
problems have been noticed that require a fairly complex timeout capability
|
|
62 |
such that descramble will not wait "forever" for output from stdin.
|
|
63 |
This solution currently uses a "kill thread" that will stop descramble if
|
|
64 |
it is blocked in a read on stdio for more than the timeout period.
|
|
65 |
|
|
66 |
The "kill thread" attempts to print as much of the input from stdin as has
|
|
67 |
been read so far. It scans this for XML characters and escapes them, finally
|
|
68 |
printing its own XML-formatted error message with the escaped version of the
|
|
69 |
input between <descramble> tags.
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
*/
|
|
75 |
|
|
76 |
#include <stdio.h>
|
|
77 |
#include <vector>
|
|
78 |
#include <ctype.h>
|
|
79 |
|
|
80 |
// what size chunks to allocate/read
|
|
81 |
const int BufferSize = 4096;
|
|
82 |
unsigned int globalreadcounter = 0;
|
|
83 |
|
|
84 |
// OS specific headers
|
|
85 |
#ifdef WIN32
|
|
86 |
#include <windows.h>
|
|
87 |
#include <tlhelp32.h>
|
|
88 |
#include <fcntl.h> /* _O_BINARY */
|
|
89 |
#else
|
|
90 |
#include <stdlib.h>
|
|
91 |
#include <fcntl.h>
|
|
92 |
#include <semaphore.h>
|
|
93 |
#include <sys/types.h>
|
|
94 |
#include <signal.h>
|
|
95 |
#include <string.h>
|
|
96 |
#endif
|
|
97 |
#include <unistd.h>
|
|
98 |
|
|
99 |
#define DEFAULT_TIMEOUT (600000) // 10 minutes
|
|
100 |
|
|
101 |
#define GOOD_OUTPUT 1
|
|
102 |
#define BAD_OUTPUT 0
|
|
103 |
|
|
104 |
typedef struct
|
|
105 |
{
|
|
106 |
char *name;
|
|
107 |
#ifdef WIN32
|
|
108 |
HANDLE handle;
|
|
109 |
#else
|
|
110 |
sem_t *handle;
|
|
111 |
#endif
|
|
112 |
|
|
113 |
unsigned int timeout;
|
|
114 |
} sbs_semaphore;
|
|
115 |
|
|
116 |
|
|
117 |
// readstate is a flag to indicate whether descramble is reading
|
|
118 |
// it's standard input.
|
|
119 |
// This allows the timeout thread on Windows to only cause the
|
|
120 |
// process to exit if there is an overdue read operation on the
|
|
121 |
// standard input.
|
|
122 |
int readstate=1;
|
|
123 |
int timeoutstate=0;
|
|
124 |
|
|
125 |
// The output semaphore.
|
|
126 |
sbs_semaphore sem;
|
|
127 |
#ifdef WIN32
|
|
128 |
HANDLE bufferMutex;
|
|
129 |
|
|
130 |
|
|
131 |
// Make all output handling binary
|
|
132 |
unsigned int _CRT_fmode = _O_BINARY;
|
|
133 |
|
|
134 |
|
|
135 |
DWORD killPIDTree = 0;
|
|
136 |
#else
|
|
137 |
pid_t killPIDTree = 0;
|
|
138 |
#endif
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
// Where we store all the standard input.
|
|
143 |
std::vector<char*> buffers;
|
|
144 |
std::vector<int> bytesIn;
|
|
145 |
|
|
146 |
|
|
147 |
void error(const char *reason, char *SEM_NAME)
|
|
148 |
{
|
|
149 |
fprintf(stderr, "<descrambler reason='%s' semaphore='%s' />\n", reason, SEM_NAME);
|
|
150 |
exit(1);
|
|
151 |
}
|
|
152 |
|
|
153 |
#ifdef WIN32
|
|
154 |
|
|
155 |
void killProcess(DWORD pid)
|
|
156 |
{
|
|
157 |
HANDLE proc = OpenProcess(PROCESS_TERMINATE,0,pid);
|
|
158 |
if (proc)
|
|
159 |
{
|
|
160 |
TerminateProcess(proc, 1);
|
|
161 |
//fprintf(stdout,"sent terminate to process=%d\n", pid);
|
|
162 |
CloseHandle(proc);
|
|
163 |
}
|
|
164 |
else
|
|
165 |
{
|
|
166 |
//fprintf(stderr,"Can't open process=%d\n", pid);
|
|
167 |
}
|
|
168 |
}
|
|
169 |
|
|
170 |
typedef struct {
|
|
171 |
DWORD PID;
|
|
172 |
DWORD PPID;
|
|
173 |
} proc;
|
|
174 |
|
|
175 |
void killProcessTreeRecursively(DWORD PPID, DWORD thisPID, std::vector<proc *> &processtree)
|
|
176 |
{
|
|
177 |
int idx;
|
|
178 |
|
|
179 |
for (idx=0; idx < processtree.size(); idx++)
|
|
180 |
{
|
|
181 |
if (processtree[idx]->PID != thisPID && processtree[idx]->PPID == PPID)
|
|
182 |
{
|
|
183 |
killProcessTreeRecursively(processtree[idx]->PID, thisPID, processtree);
|
|
184 |
//fprintf(stderr,"Found descendant =%d\n",processtree[idx]->PID );
|
|
185 |
}
|
|
186 |
}
|
|
187 |
|
|
188 |
killProcess(PPID);
|
|
189 |
}
|
|
190 |
|
|
191 |
int killProcessTree(DWORD PPID)
|
|
192 |
{
|
|
193 |
HANDLE hSnapShot;
|
|
194 |
DWORD thisProcID=0;
|
|
195 |
BOOL ok;
|
|
196 |
PROCESSENTRY32 pe;
|
|
197 |
std::vector<proc *> processtree;
|
|
198 |
|
|
199 |
thisProcID = GetCurrentProcessId();
|
|
200 |
|
|
201 |
hSnapShot=CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
|
|
202 |
|
|
203 |
// Put all this process information into an array;
|
|
204 |
ok = Process32First(hSnapShot, &pe);
|
|
205 |
while (ok)
|
|
206 |
{
|
|
207 |
if ( pe.th32ProcessID != thisProcID)
|
|
208 |
{
|
|
209 |
proc *p = new proc;
|
|
210 |
p->PID = pe.th32ProcessID;
|
|
211 |
p->PPID = pe.th32ParentProcessID;
|
|
212 |
processtree.push_back(p);
|
|
213 |
//fprintf(stderr,"Found process =%d\n", pe.th32ProcessID );
|
|
214 |
}
|
|
215 |
|
|
216 |
ok = Process32Next(hSnapShot, &pe);
|
|
217 |
}
|
|
218 |
|
|
219 |
killProcessTreeRecursively(PPID, thisProcID, processtree);
|
|
220 |
|
|
221 |
CloseHandle(hSnapShot);
|
|
222 |
|
|
223 |
//fprintf(stderr,"Ending killproc\n", PPID);
|
|
224 |
|
|
225 |
return 0;
|
|
226 |
}
|
|
227 |
|
|
228 |
#endif
|
|
229 |
|
|
230 |
void createDescrambleSemaphore(sbs_semaphore *s)
|
|
231 |
{
|
|
232 |
#ifdef WIN32
|
|
233 |
s->handle = CreateSemaphore(NULL, 1, 1, s->name);
|
|
234 |
if (s->handle)
|
|
235 |
CloseHandle(s->handle);
|
|
236 |
else
|
|
237 |
error("unable to create semaphore", s->name);
|
|
238 |
#else
|
|
239 |
s->handle = sem_open(s->name, O_CREAT | O_EXCL, 0644, 1);
|
|
240 |
|
|
241 |
if (s->handle == SEM_FAILED)
|
|
242 |
{
|
|
243 |
sem_close(s->handle);
|
|
244 |
error("unable to create semaphore", s->name);
|
|
245 |
}
|
|
246 |
sem_close(s->handle);
|
|
247 |
#endif
|
|
248 |
}
|
|
249 |
|
|
250 |
void destroyDescrambleSemaphore(sbs_semaphore *s)
|
|
251 |
{
|
|
252 |
#ifdef WIN32
|
|
253 |
/* can't destroy a windows semaphore... */
|
|
254 |
#else
|
|
255 |
if (sem_unlink(s->name) != 0)
|
|
256 |
error("unable to unlink semaphore", s->name);
|
|
257 |
#endif
|
|
258 |
}
|
|
259 |
|
|
260 |
|
|
261 |
int waitForOutput(sbs_semaphore *s)
|
|
262 |
{
|
|
263 |
/* try and open the semaphore now */
|
|
264 |
#ifdef WIN32
|
|
265 |
s->handle = CreateSemaphore(NULL, 1, 1, s->name);
|
|
266 |
if (!s->handle)
|
|
267 |
error("unable to open semaphore", s->name);
|
|
268 |
#else
|
|
269 |
s->handle = sem_open(s->name, 0);
|
|
270 |
|
|
271 |
if (s->handle == SEM_FAILED)
|
|
272 |
{
|
|
273 |
sem_close(s->handle);
|
|
274 |
error("unable to open semaphore", s->name);
|
|
275 |
}
|
|
276 |
#endif
|
|
277 |
|
|
278 |
/* wait for the semaphore to be free [timeout after 60 seconds] */
|
|
279 |
int timedOutFlag = 0;
|
|
280 |
#ifdef WIN32
|
|
281 |
timedOutFlag = (WaitForSingleObject(s->handle, s->timeout) != WAIT_OBJECT_0);
|
|
282 |
#else
|
|
283 |
sem_wait(s->handle);
|
|
284 |
#endif
|
|
285 |
|
|
286 |
return timedOutFlag;
|
|
287 |
}
|
|
288 |
|
|
289 |
|
|
290 |
void releaseOutput(sbs_semaphore *s)
|
|
291 |
{
|
|
292 |
/* release the semaphore */
|
|
293 |
#ifdef WIN32
|
|
294 |
ReleaseSemaphore(s->handle, 1, NULL);
|
|
295 |
#else
|
|
296 |
sem_post(s->handle);
|
|
297 |
#endif
|
|
298 |
|
|
299 |
/* clean up */
|
|
300 |
#ifdef WIN32
|
|
301 |
CloseHandle(s->handle);
|
|
302 |
#else
|
|
303 |
sem_close(s->handle);
|
|
304 |
#endif
|
|
305 |
}
|
|
306 |
|
|
307 |
void writeBuffers(int goodoutput)
|
|
308 |
{
|
|
309 |
/* write stdin buffers to stdout. If the output comes
|
|
310 |
from a partially-complete command then make sure that there
|
|
311 |
is no malformed xml inside by escaping it. */
|
|
312 |
char *escaped_output=NULL;
|
|
313 |
|
|
314 |
#ifdef WIN32
|
|
315 |
DWORD dwWaitResult = WaitForSingleObject(
|
|
316 |
bufferMutex,
|
|
317 |
INFINITE);
|
|
318 |
#endif
|
|
319 |
|
|
320 |
for (int i = 0; i < buffers.size(); i++)
|
|
321 |
{
|
|
322 |
int bytes_out;
|
|
323 |
char *outbuf;
|
|
324 |
|
|
325 |
if (goodoutput != GOOD_OUTPUT)
|
|
326 |
{
|
|
327 |
if (!escaped_output)
|
|
328 |
escaped_output = new char[BufferSize*4];
|
|
329 |
|
|
330 |
if (!escaped_output)
|
|
331 |
error("No memory for escaped outputbuffer.",sem.name);
|
|
332 |
|
|
333 |
char *buf = buffers[i];
|
|
334 |
bytes_out = 0;
|
|
335 |
for (int idx=0; idx < bytesIn[i]; idx++)
|
|
336 |
{
|
|
337 |
switch (buf[idx])
|
|
338 |
{
|
|
339 |
case '&':
|
|
340 |
escaped_output[bytes_out++]='&';
|
|
341 |
escaped_output[bytes_out++]='a';
|
|
342 |
escaped_output[bytes_out++]='m';
|
|
343 |
escaped_output[bytes_out++]='p';
|
|
344 |
escaped_output[bytes_out++]=';';
|
|
345 |
break;
|
|
346 |
case '<':
|
|
347 |
escaped_output[bytes_out++]='&';
|
|
348 |
escaped_output[bytes_out++]='l';
|
|
349 |
escaped_output[bytes_out++]='t';
|
|
350 |
escaped_output[bytes_out++]=';';
|
|
351 |
break;
|
|
352 |
case '>':
|
|
353 |
escaped_output[bytes_out++]='&';
|
|
354 |
escaped_output[bytes_out++]='g';
|
|
355 |
escaped_output[bytes_out++]='t';
|
|
356 |
escaped_output[bytes_out++]=';';
|
|
357 |
break;
|
|
358 |
default:
|
|
359 |
if (!iscntrl(buf[idx]) || buf[idx] == '\n' || buf[idx] == '\r')
|
|
360 |
escaped_output[bytes_out++]=buf[idx];
|
|
361 |
break;
|
|
362 |
}
|
|
363 |
|
|
364 |
}
|
|
365 |
|
|
366 |
outbuf = escaped_output;
|
|
367 |
|
|
368 |
} else {
|
|
369 |
outbuf = buffers[i];
|
|
370 |
bytes_out = bytesIn[i];
|
|
371 |
}
|
|
372 |
fwrite(outbuf, 1, bytes_out, stdout);
|
|
373 |
}
|
|
374 |
#ifdef WIN32
|
|
375 |
ReleaseMutex(bufferMutex);
|
|
376 |
#endif
|
|
377 |
|
|
378 |
if (escaped_output)
|
|
379 |
delete escaped_output;
|
|
380 |
fflush(stdout);
|
|
381 |
}
|
|
382 |
|
|
383 |
#ifdef WIN32
|
|
384 |
/*
|
|
385 |
A Thread that kills this process if it is "stuck" in a read operation
|
|
386 |
for longer than the timeout period.
|
|
387 |
|
|
388 |
There are some race conditions here that don't matter. e.g. globalreadcounter
|
|
389 |
is not protected. This might result in an "unfair" timeout but we don't care
|
|
390 |
because the timeout should be pretty long and anything that's even nearly
|
|
391 |
a timeout deserves to be timed out.
|
|
392 |
|
|
393 |
Additionally, if the timeout is so quick that this function starts before the first
|
|
394 |
ever read has happened then there would be a premature timeout. This is not likely
|
|
395 |
so we also dont' care - the timeout has a minimum value which is more than long
|
|
396 |
enough (500msec) to deal with that.
|
|
397 |
|
|
398 |
*/
|
|
399 |
DWORD descrambleKillerThread(void * param)
|
|
400 |
{
|
|
401 |
|
|
402 |
sbs_semaphore *s;
|
|
403 |
unsigned int stored_globalreadcounter;
|
|
404 |
s = (sbs_semaphore *)param;
|
|
405 |
|
|
406 |
fflush(stderr);
|
|
407 |
//fprintf(stdout, " timeout=%u sem_name='%s' \n", s->timeout, s->name);
|
|
408 |
|
|
409 |
do
|
|
410 |
{
|
|
411 |
stored_globalreadcounter = globalreadcounter;
|
|
412 |
Sleep(s->timeout);
|
|
413 |
}
|
|
414 |
while (globalreadcounter != stored_globalreadcounter);
|
|
415 |
|
|
416 |
if (waitForOutput(s) != 0)
|
|
417 |
{
|
|
418 |
fprintf(stdout, "<descrambler reason='semaphore wait exceeded %ums timeout' semaphore='%s' />\n", s->timeout, s->name);
|
|
419 |
ExitProcess(3);
|
|
420 |
}
|
|
421 |
|
|
422 |
if (readstate)
|
|
423 |
{
|
|
424 |
fprintf(stdout, "<descrambler reason='command output read exceeded %ums timeout' semaphore='%s'>\n", s->timeout, s->name);
|
|
425 |
writeBuffers(BAD_OUTPUT);
|
|
426 |
fprintf(stdout, "</descrambler>\n");
|
|
427 |
fflush(stdout);
|
|
428 |
if (killPIDTree != 0)
|
|
429 |
killProcessTree(killPIDTree); // Make sure peers and parents all die. Nasty
|
|
430 |
ExitProcess(2);
|
|
431 |
}
|
|
432 |
else
|
|
433 |
{
|
|
434 |
writeBuffers(GOOD_OUTPUT);
|
|
435 |
}
|
|
436 |
|
|
437 |
// Don't release the semaphore in case the main thread
|
|
438 |
// gets it and tries to write the output.
|
|
439 |
|
|
440 |
// Input process finished while we were waiting
|
|
441 |
// for the semaphore so a false alarm.
|
|
442 |
fflush(stdout);
|
|
443 |
ExitProcess(0);
|
|
444 |
}
|
|
445 |
#endif
|
|
446 |
|
|
447 |
|
|
448 |
int main(int argc, char *argv[])
|
|
449 |
{
|
|
450 |
char usage[]="usage: %s [-t timeout_millisecs] [ -k kill_PID_tree_on_fail ] buildID [start | stop]\nwhere timeout_millisecs >= 500\n";
|
|
451 |
int opt_res;
|
|
452 |
char options[]="t:k:";
|
|
453 |
|
|
454 |
sem.timeout = DEFAULT_TIMEOUT;
|
|
455 |
|
|
456 |
|
|
457 |
opt_res = getopt(argc, argv, options);
|
|
458 |
|
|
459 |
while (opt_res != -1 )
|
|
460 |
{
|
|
461 |
switch (opt_res)
|
|
462 |
{
|
|
463 |
case 'k':
|
|
464 |
if (!optarg)
|
|
465 |
{
|
|
466 |
fprintf(stderr, "PID argument required for 'kill PID tree on fail' option\n");
|
|
467 |
fprintf(stderr, usage, argv[0]);
|
|
468 |
exit(1);
|
|
469 |
}
|
|
470 |
|
|
471 |
killPIDTree = atol(optarg);
|
|
472 |
if (killPIDTree == 0)
|
|
473 |
{
|
|
474 |
fprintf(stderr, usage, argv[0]);
|
|
475 |
fprintf(stderr, "kill PID tree on fail must be > 0: %u\n", killPIDTree);
|
|
476 |
exit(1);
|
|
477 |
}
|
|
478 |
break;
|
|
479 |
case 't':
|
|
480 |
if (!optarg)
|
|
481 |
{
|
|
482 |
fprintf(stderr, "timeout argument required for timeout option\n");
|
|
483 |
fprintf(stderr, usage, argv[0]);
|
|
484 |
exit(1);
|
|
485 |
}
|
|
486 |
|
|
487 |
sem.timeout = atoi(optarg);
|
|
488 |
if (sem.timeout < 500)
|
|
489 |
{
|
|
490 |
fprintf(stderr, usage, argv[0]);
|
|
491 |
fprintf(stderr, "timeout was too low: %u\n", sem.timeout);
|
|
492 |
exit(1);
|
|
493 |
}
|
|
494 |
break;
|
|
495 |
case '?':
|
|
496 |
default:
|
|
497 |
fprintf(stderr, usage, argv[0]);
|
|
498 |
fprintf(stderr, "Unknown option. %s\n", opterr);
|
|
499 |
exit(1);
|
|
500 |
break;
|
|
501 |
}
|
|
502 |
|
|
503 |
opt_res = getopt(argc, argv, options);
|
|
504 |
}
|
|
505 |
|
|
506 |
if (optind >= argc)
|
|
507 |
{
|
|
508 |
fprintf(stderr, usage, argv[0]);
|
|
509 |
fprintf(stderr, "Missing buildID\n");
|
|
510 |
exit(1);
|
|
511 |
}
|
|
512 |
|
|
513 |
sem.name = argv[optind];
|
|
514 |
|
|
515 |
if (optind + 1 < argc)
|
|
516 |
{
|
|
517 |
optind++;
|
|
518 |
|
|
519 |
if (strncmp(argv[optind], "stop",4) == 0)
|
|
520 |
destroyDescrambleSemaphore(&sem);
|
|
521 |
else if (strncmp(argv[optind],"start",5) == 0)
|
|
522 |
createDescrambleSemaphore(&sem);
|
|
523 |
else
|
|
524 |
{
|
|
525 |
fprintf(stderr, usage, argv[0]);
|
|
526 |
fprintf(stderr, "Unknown argument:: %s\n", argv[optind]);
|
|
527 |
exit(1);
|
|
528 |
}
|
|
529 |
|
|
530 |
exit(0);
|
|
531 |
}
|
|
532 |
|
|
533 |
#ifdef WIN32
|
|
534 |
|
|
535 |
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
|
|
536 |
|
|
537 |
bufferMutex = CreateMutex(NULL, FALSE, NULL);
|
|
538 |
|
|
539 |
/*
|
|
540 |
HANDLE WINAPI CreateThread(
|
|
541 |
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
|
|
542 |
__in SIZE_T dwStackSize,
|
|
543 |
__in LPTHREAD_START_ROUTINE lpStartAddress,
|
|
544 |
__in_opt LPVOID lpParameter,
|
|
545 |
__in DWORD dwCreationFlags,
|
|
546 |
__out_opt LPDWORD lpThreadId
|
|
547 |
); */
|
|
548 |
|
|
549 |
DWORD killerThreadId;
|
|
550 |
HANDLE hKillerThread;
|
|
551 |
|
|
552 |
hKillerThread = CreateThread(NULL, 4096, (LPTHREAD_START_ROUTINE) descrambleKillerThread, (void*)&sem, 0, &killerThreadId);
|
|
553 |
#endif
|
|
554 |
|
|
555 |
/* read all of my stdin into buffers */
|
|
556 |
int bytesRead = 0;
|
|
557 |
int bufferIndex = 0;
|
|
558 |
do
|
|
559 |
{
|
|
560 |
char *buffer = new char[BufferSize];
|
|
561 |
if (buffer == NULL)
|
|
562 |
error("not enough memory for buffer", sem.name);
|
|
563 |
|
|
564 |
|
|
565 |
// Add an empty buffer in advance so that if there is a timeout
|
|
566 |
// the partial command result can be gathered.
|
|
567 |
#ifdef WIN32
|
|
568 |
DWORD dwWaitResult = WaitForSingleObject(
|
|
569 |
bufferMutex,
|
|
570 |
INFINITE);
|
|
571 |
#endif
|
|
572 |
|
|
573 |
buffers.push_back(buffer);
|
|
574 |
bytesIn.push_back(0);
|
|
575 |
int *counter = &bytesIn[bufferIndex];
|
|
576 |
|
|
577 |
|
|
578 |
#ifdef WIN32
|
|
579 |
ReleaseMutex(bufferMutex);
|
|
580 |
#endif
|
|
581 |
// Empty buffer added.
|
|
582 |
|
|
583 |
char c = fgetc(stdin);
|
|
584 |
|
|
585 |
//fprintf(stderr, "counter %d buffersize %d\n", *counter, BufferSize);
|
|
586 |
do
|
|
587 |
{
|
|
588 |
if (c == EOF)
|
|
589 |
break;
|
|
590 |
// escape unprintable characters that might make logs unparsable.
|
|
591 |
if (iscntrl(c) && !isspace(c))
|
|
592 |
c = '_';
|
|
593 |
|
|
594 |
buffer[*counter] = c;
|
|
595 |
|
|
596 |
*counter += 1;
|
|
597 |
if (*counter >= BufferSize)
|
|
598 |
break;
|
|
599 |
|
|
600 |
c = fgetc(stdin);
|
|
601 |
globalreadcounter = ++globalreadcounter % 65535*4;
|
|
602 |
}
|
|
603 |
while (c != EOF);
|
|
604 |
|
|
605 |
//fprintf(stderr, "## %d bytesin %d\n", bufferIndex, *counter);
|
|
606 |
bufferIndex++;
|
|
607 |
}
|
|
608 |
while (!feof(stdin) && !timeoutstate);
|
|
609 |
readstate = 0; // Tell the killerthread that it can back off.
|
|
610 |
|
|
611 |
int timedout;
|
|
612 |
|
|
613 |
timedout = waitForOutput(&sem);
|
|
614 |
|
|
615 |
|
|
616 |
if (timedout)
|
|
617 |
error("timed out waiting for semaphore", sem.name);
|
|
618 |
else
|
|
619 |
{
|
|
620 |
writeBuffers(1);
|
|
621 |
}
|
|
622 |
|
|
623 |
releaseOutput(&sem);
|
|
624 |
|
|
625 |
/* let the OS free the buffer memory */
|
|
626 |
exit(0);
|
|
627 |
}
|
|
628 |
|
|
629 |
|