1 | /* $Id: CmdQd.c,v 1.7 2001-09-30 05:26:52 bird Exp $
|
---|
2 | *
|
---|
3 | * Command Queue Daemon / Client.
|
---|
4 | *
|
---|
5 | * Designed to execute commands asyncronus using multiple workers,
|
---|
6 | * and when all commands are submitted wait for them to complete.
|
---|
7 | *
|
---|
8 | * Copyright (c) 2001 knut st. osmundsen (kosmunds@csc.com)
|
---|
9 | *
|
---|
10 | * GPL
|
---|
11 | *
|
---|
12 | */
|
---|
13 |
|
---|
14 |
|
---|
15 | /** @design Command Queue Daemon.
|
---|
16 | *
|
---|
17 | * This command daemon orginated as tool to exploit SMP and UNI systems better
|
---|
18 | * when building large programs, but also when building one specific component of
|
---|
19 | * that program. It is gonna work just like the gnu make -j option.
|
---|
20 | *
|
---|
21 | * @subsection Work flow
|
---|
22 | *
|
---|
23 | * 1. Init daemon process. Creates a daemon process with a given number of
|
---|
24 | * workers. This is a detached process.
|
---|
25 | * 2. Submit jobs to the daemon. The daemon will queue the jobs and the
|
---|
26 | * workers will start working at once there is a job for them.
|
---|
27 | * 3. The nmake script will issue a wait command. We will now wait for all
|
---|
28 | * jobs to finish and in the mean time we'll display output from the jobs.
|
---|
29 | * Failing jobs will be queued up and show when all jobs are finished.
|
---|
30 | * 4. Two options: kill the daemon or start at step 2 again.
|
---|
31 | *
|
---|
32 | *
|
---|
33 | * @subsection Client <-> Daemon communication
|
---|
34 | *
|
---|
35 | * Client and daemon is one and the same executable. This has some advantages
|
---|
36 | * like implicit preloading of the client, fewer source files and fewer programs
|
---|
37 | * to install.
|
---|
38 | *
|
---|
39 | * The communication between the client and the daemon will use shared memory
|
---|
40 | * with an mutex semaphore and two event sems to direct the conversation. The
|
---|
41 | * shared memory block is allocated by the daemon and will have a quite simple
|
---|
42 | * layout:
|
---|
43 | * Mutex Semaphore.
|
---|
44 | * Message Type.
|
---|
45 | * Message specific data:
|
---|
46 | * - Submit job:
|
---|
47 | * Returcode ignore. (4 bytes)
|
---|
48 | * Command to execute. (1 Kb)
|
---|
49 | * Current directory. (260 bytes)
|
---|
50 | * Environment block. (about 62KB)
|
---|
51 | * - Submit job response:
|
---|
52 | * Success/failure indicator.
|
---|
53 | *
|
---|
54 | * - Wait:
|
---|
55 | * Nothing.
|
---|
56 | * - Wait response:
|
---|
57 | * More output indicator.
|
---|
58 | * Success/failure indicator.
|
---|
59 | * Job output (about 63KB)
|
---|
60 | *
|
---|
61 | * - Kill:
|
---|
62 | * Nothing.
|
---|
63 | * - Kill response:
|
---|
64 | * Success/failure indicator.
|
---|
65 | *
|
---|
66 | * - Dies:
|
---|
67 | * Nothing. This is a message to the client saying that the
|
---|
68 | * daemon is dying or allready dead.
|
---|
69 | *
|
---|
70 | * The shared memory block is 64KB.
|
---|
71 | *
|
---|
72 | *
|
---|
73 | * @subsection The Workers
|
---|
74 | *
|
---|
75 | * The workers is individual threads which waits for a job to be submitted to
|
---|
76 | * execution. If the job only contains a single executable program to execute
|
---|
77 | * (no & or &&) it will be executed using DosExecPgm. If it's a multi program
|
---|
78 | * or command job it will be executed by CMD.EXE.
|
---|
79 | *
|
---|
80 | * Output will be read using unamed pipe and buffered. When the job is
|
---|
81 | * completed we'll put the output into either the success queue or the failure
|
---|
82 | * queue depending on the result.
|
---|
83 | *
|
---|
84 | * Note. Process startup needs to be serialized in order to be able to redirect
|
---|
85 | * stdout. We're using a mutex for this.
|
---|
86 | *
|
---|
87 | */
|
---|
88 |
|
---|
89 |
|
---|
90 | /*******************************************************************************
|
---|
91 | * Header Files *
|
---|
92 | *******************************************************************************/
|
---|
93 | #include <stdio.h>
|
---|
94 | #include <string.h>
|
---|
95 | #include <stdlib.h>
|
---|
96 | #include <stdarg.h>
|
---|
97 | #include <assert.h>
|
---|
98 | #include <direct.h>
|
---|
99 | #include <signal.h>
|
---|
100 | #include <process.h>
|
---|
101 |
|
---|
102 | #define INCL_BASE
|
---|
103 | #include <os2.h>
|
---|
104 |
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Memory debugging.
|
---|
108 | */
|
---|
109 | #ifdef DEBUGMEMORY
|
---|
110 | void my_free(void *);
|
---|
111 | void *my_malloc(size_t);
|
---|
112 |
|
---|
113 | #undef free
|
---|
114 | #undef malloc
|
---|
115 |
|
---|
116 | #define free(pv) my_free(pv)
|
---|
117 | #define malloc(cb) my_malloc(cb)
|
---|
118 |
|
---|
119 | #endif
|
---|
120 |
|
---|
121 | /*******************************************************************************
|
---|
122 | * Defined Constants And Macros *
|
---|
123 | *******************************************************************************/
|
---|
124 | #define SHARED_MEM_NAME "\\SHAREMEM\\CmdQd"
|
---|
125 | #define SHARED_MEM_SIZE 65536
|
---|
126 | #define IDLE_TIMEOUT_MS -1 //(60*1000*3)
|
---|
127 | #define OUTPUT_CHUNK (8192-8)
|
---|
128 |
|
---|
129 | #define HF_STDIN 0
|
---|
130 | #define HF_STDOUT 1
|
---|
131 | #define HF_STDERR 2
|
---|
132 |
|
---|
133 | /*******************************************************************************
|
---|
134 | * Structures and Typedefs *
|
---|
135 | *******************************************************************************/
|
---|
136 | typedef struct SharedMem
|
---|
137 | {
|
---|
138 | HEV hevClient; /* Client will wait on this. */
|
---|
139 | HEV hevDaemon; /* Daemon will wait on this. */
|
---|
140 | HMTX hmtx; /* Owner of the shared memory. */
|
---|
141 | HMTX hmtxClient; /* Take and release this for each */
|
---|
142 | /* client -> server -> client talk. */
|
---|
143 | enum
|
---|
144 | {
|
---|
145 | msgUnknown = 0,
|
---|
146 | msgSubmit = 1,
|
---|
147 | msgSubmitResponse = 2,
|
---|
148 | msgWait = 3,
|
---|
149 | msgWaitResponse = 4,
|
---|
150 | msgKill = 5,
|
---|
151 | msgKillResponse = 6,
|
---|
152 | msgDying = 0xff
|
---|
153 | } enmMsgType;
|
---|
154 |
|
---|
155 | union
|
---|
156 | {
|
---|
157 | struct Submit
|
---|
158 | {
|
---|
159 | unsigned rcIgnore; /* max return code to accept as good. */
|
---|
160 | char szCommand[1024]; /* job command. */
|
---|
161 | char szCurrentDir[CCHMAXPATH]; /* current directory. */
|
---|
162 | int cchEnv; /* Size of the environment block */
|
---|
163 | char szzEnv[SHARED_MEM_SIZE - CCHMAXPATH - 1024 - 4 - 4 - 4 - 4 - 4 - 4];
|
---|
164 | /* Environment block. */
|
---|
165 | } Submit;
|
---|
166 |
|
---|
167 | struct SubmitResponse
|
---|
168 | {
|
---|
169 | BOOL fRc; /* Success idicator. */
|
---|
170 | } SubmitResponse;
|
---|
171 |
|
---|
172 |
|
---|
173 | struct Wait
|
---|
174 | {
|
---|
175 | int iNothing; /* Dummy. */
|
---|
176 | } Wait;
|
---|
177 |
|
---|
178 | struct WaitResponse
|
---|
179 | {
|
---|
180 | BOOL fMore; /* More data. */
|
---|
181 | int rc; /* return code of first failing job. */
|
---|
182 | /* only valid if fMore == FALSE. */
|
---|
183 | char szOutput[SHARED_MEM_SIZE- 4 - 4 - 4 - 4 - 4 - 4 - 4];
|
---|
184 | /* The output of one or more jobs. */
|
---|
185 | } WaitResponse;
|
---|
186 |
|
---|
187 |
|
---|
188 | struct Kill
|
---|
189 | {
|
---|
190 | int iNothing; /* dummy. */
|
---|
191 | } Kill;
|
---|
192 |
|
---|
193 | struct KillResponse
|
---|
194 | {
|
---|
195 | BOOL fRc; /* Success idicator. */
|
---|
196 | } KillResponse;
|
---|
197 |
|
---|
198 | } u1;
|
---|
199 |
|
---|
200 | } SHAREDMEM, *PSHAREDMEM;
|
---|
201 |
|
---|
202 |
|
---|
203 | typedef struct JobOutput
|
---|
204 | {
|
---|
205 | struct JobOutput * pNext; /* Pointer to next output chunk. */
|
---|
206 | int cchOutput; /* Bytes used of the szOutput member. */
|
---|
207 | char szOutput[OUTPUT_CHUNK]; /* Output. */
|
---|
208 | } JOBOUTPUT, *PJOBOUTPUT;
|
---|
209 |
|
---|
210 |
|
---|
211 | typedef struct Job
|
---|
212 | {
|
---|
213 | struct Job * pNext; /* Pointer to next job. */
|
---|
214 | int rc; /* Result. */
|
---|
215 | PJOBOUTPUT pJobOutput; /* Output. */
|
---|
216 | struct Submit JobInfo; /* Job. */
|
---|
217 | } JOB, *PJOB;
|
---|
218 |
|
---|
219 |
|
---|
220 | typedef struct PathCache
|
---|
221 | {
|
---|
222 | char szPath[4096 - CCHMAXPATH * 3]; /* The path which this is valid for. */
|
---|
223 | char szCurDir[CCHMAXPATH]; /* The current dir this is valid for. */
|
---|
224 | char szProgram[CCHMAXPATH]; /* The program. */
|
---|
225 | char szResult[CCHMAXPATH]; /* The result. */
|
---|
226 | } PATHCACHE, *PPATHCACHE;
|
---|
227 |
|
---|
228 |
|
---|
229 | /*******************************************************************************
|
---|
230 | * Global Variables *
|
---|
231 | *******************************************************************************/
|
---|
232 | PSHAREDMEM pShrMem; /* Pointer to the shared memory. */
|
---|
233 |
|
---|
234 | PJOB pJobQueue; /* Linked list of jobs. */
|
---|
235 | PJOB pJobQueueEnd; /* Last job entry. */
|
---|
236 | HMTX hmtxJobQueue; /* Read/Write mutex. */
|
---|
237 | HEV hevJobQueue; /* Incomming job event sem. */
|
---|
238 | ULONG cJobs; /* Count of jobs submitted. */
|
---|
239 |
|
---|
240 | HMTX hmtxJobQueueFine; /* Read/Write mutex for the next two queues. */
|
---|
241 | HEV hevJobQueueFine; /* Posted when there is more output. */
|
---|
242 | PJOB pJobCompleted; /* Linked list of successful jobs. */
|
---|
243 | PJOB pJobCompletedLast; /* Last successful job entry. */
|
---|
244 | PJOB pJobFailed; /* Linked list of failed jobs. */
|
---|
245 | PJOB pJobFailedLast; /* Last failed job entry. */
|
---|
246 | ULONG cJobsFinished; /* Count of jobs finished (failed or completed). */
|
---|
247 |
|
---|
248 | HMTX hmtxExec; /* Execute childs mutex sem. Required */
|
---|
249 | /* since we redirect standard files handles */
|
---|
250 | /* and changes the currentdirectory. */
|
---|
251 |
|
---|
252 | /*******************************************************************************
|
---|
253 | * Internal Functions *
|
---|
254 | *******************************************************************************/
|
---|
255 | void syntax(void);
|
---|
256 |
|
---|
257 | /* operations */
|
---|
258 | int Init(const char *arg0, int cWorkers);
|
---|
259 | int Daemon(int cWorkers);
|
---|
260 | int DaemonInit(int cWorkers);
|
---|
261 | void signalhandlerDaemon(int sig);
|
---|
262 | void signalhandlerClient(int sig);
|
---|
263 | void Worker(void * iWorkerId);
|
---|
264 | char*WorkerArguments(char *pszArg, const char *pszzEnv, const char *pszCommand, char *pszCurDir, PPATHCACHE pPathCache);
|
---|
265 | char*fileNormalize(char *pszFilename, char *pszCurDir);
|
---|
266 | APIRET fileExist(const char *pszFilename);
|
---|
267 | int Submit(int rcIgnore);
|
---|
268 | int Wait(void);
|
---|
269 | int QueryRunning(void);
|
---|
270 | int Kill(void);
|
---|
271 | /* shared memory helpers */
|
---|
272 | int shrmemCreate(void);
|
---|
273 | int shrmemOpen(void);
|
---|
274 | void shrmemFree(void);
|
---|
275 | int shrmemSendDaemon(BOOL fWait);
|
---|
276 | int shrmemSendClient(int enmMsgTypeResponse);
|
---|
277 |
|
---|
278 | /* error handling */
|
---|
279 | void _Optlink Error(const char *pszFormat, ...);
|
---|
280 |
|
---|
281 |
|
---|
282 | int main(int argc, char **argv)
|
---|
283 | {
|
---|
284 |
|
---|
285 | /*
|
---|
286 | * Display help.
|
---|
287 | */
|
---|
288 | if (argc < 2 || (argv[1][0] == '-'))
|
---|
289 | {
|
---|
290 | syntax();
|
---|
291 | if (argc < 2)
|
---|
292 | {
|
---|
293 | printf("\n!syntax error!");
|
---|
294 | return -1;
|
---|
295 | }
|
---|
296 | return 0;
|
---|
297 | }
|
---|
298 |
|
---|
299 | /*
|
---|
300 | * String switch on command.
|
---|
301 | */
|
---|
302 | if (!stricmp(argv[1], "submit"))
|
---|
303 | {
|
---|
304 | int rcIgnore = 0;
|
---|
305 | if (argc == 2)
|
---|
306 | {
|
---|
307 | printf("fatal error: There is no job to submit...\n");
|
---|
308 | return -1;
|
---|
309 | }
|
---|
310 | if (argv[2][0] == '-' && (rcIgnore = atoi(argv[2]+1)) <= 0)
|
---|
311 | {
|
---|
312 | printf("syntax error: Invalid ignore return code number...\n");
|
---|
313 | return -1;
|
---|
314 | }
|
---|
315 | return Submit(rcIgnore);
|
---|
316 | }
|
---|
317 | else if (!stricmp(argv[1], "wait"))
|
---|
318 | {
|
---|
319 | return Wait();
|
---|
320 | }
|
---|
321 | else if (!strcmp(argv[1], "queryrunning"))
|
---|
322 | {
|
---|
323 | return QueryRunning();
|
---|
324 | }
|
---|
325 | else if (!strcmp(argv[1], "kill"))
|
---|
326 | {
|
---|
327 | return Kill();
|
---|
328 | }
|
---|
329 | else if (!strcmp(argv[1], "init"))
|
---|
330 | {
|
---|
331 | if (argc != 3 || atoi(argv[2]) <= 0 || atoi(argv[2]) >= 256)
|
---|
332 | {
|
---|
333 | printf("fatal error: invalid/missing number of workers.\n");
|
---|
334 | return -1;
|
---|
335 | }
|
---|
336 | return Init(argv[0], atoi(argv[2]));
|
---|
337 | }
|
---|
338 | else if (!strcmp(argv[1], "!Daemon!"))
|
---|
339 | {
|
---|
340 | if (argc != 3 || atoi(argv[2]) <= 0)
|
---|
341 | {
|
---|
342 | printf("fatal error: no worker count specified or to many parameters.\n");
|
---|
343 | return -2;
|
---|
344 | }
|
---|
345 |
|
---|
346 | return Daemon(atoi(argv[2]));
|
---|
347 | }
|
---|
348 | else
|
---|
349 | {
|
---|
350 | syntax();
|
---|
351 | printf("\n!invalid command '%s'.\n", argv[1]);
|
---|
352 | return -1;
|
---|
353 | }
|
---|
354 |
|
---|
355 | //return 0;
|
---|
356 | }
|
---|
357 |
|
---|
358 |
|
---|
359 | /**
|
---|
360 | * Display syntax.
|
---|
361 | */
|
---|
362 | void syntax(void)
|
---|
363 | {
|
---|
364 | printf(
|
---|
365 | "Command Queue Daemon v0.0.1\n"
|
---|
366 | "---------------------------\n"
|
---|
367 | "syntax: CmdQd.exe <command> [args]\n"
|
---|
368 | "\n"
|
---|
369 | "commands:\n"
|
---|
370 | " init <workers>\n"
|
---|
371 | " Initiates the command queue daemon with the given number of workers.\n"
|
---|
372 | "\n"
|
---|
373 | " submit [-<n>] <command> [args]\n"
|
---|
374 | " Submits a command to the daemon.\n"
|
---|
375 | " Use '-<n>' to tell use to ignore return code 0-n.\n"
|
---|
376 | " \n"
|
---|
377 | " wait\n"
|
---|
378 | " Wait for all commands which are queued up to complete.\n"
|
---|
379 | " rc = count of failing commands.\n"
|
---|
380 | " \n"
|
---|
381 | " kill\n"
|
---|
382 | " Kills the daemon. Daemon will automatically die after\n"
|
---|
383 | " idling for some time.\n"
|
---|
384 | " queryrunning\n"
|
---|
385 | " Checks if the daemon is running.\n"
|
---|
386 | " rc = 0 if running; rc != 0 if not running.\n"
|
---|
387 | "\n"
|
---|
388 | "Copyright (c) 2001 knut st. osmundsen (kosmunds@csc.com)\n"
|
---|
389 | );
|
---|
390 | }
|
---|
391 |
|
---|
392 |
|
---|
393 | /**
|
---|
394 | * Starts a daemon process.
|
---|
395 | * @returns 0 on success.
|
---|
396 | * -4 on error.
|
---|
397 | * @param arg0 Executable filename.
|
---|
398 | * @param cWorkers Number of workers to start.
|
---|
399 | */
|
---|
400 | int Init(const char *arg0, int cWorkers)
|
---|
401 | {
|
---|
402 | int rc;
|
---|
403 | RESULTCODES Res; /* dummy, unused */
|
---|
404 | char szArg[CCHMAXPATH + 32];
|
---|
405 |
|
---|
406 | sprintf(&szArg[0], "%s\t!Daemon! %d", arg0, cWorkers);
|
---|
407 | szArg[strlen(arg0)] = '\0';
|
---|
408 | rc = DosExecPgm(NULL, 0, EXEC_BACKGROUND, &szArg[0], NULL, &Res, &szArg[0]);
|
---|
409 | if (rc)
|
---|
410 | Error("Fatal error: Failed to start daemon. rc=%d\n");
|
---|
411 | return rc;
|
---|
412 | }
|
---|
413 |
|
---|
414 |
|
---|
415 | /**
|
---|
416 | * This process is to be a daemon with a given number of works.
|
---|
417 | * @returns 0 on success.
|
---|
418 | * -4 on error.
|
---|
419 | * @param cWorkers Number of workers to start.
|
---|
420 | * @sketch
|
---|
421 | */
|
---|
422 | int Daemon(int cWorkers)
|
---|
423 | {
|
---|
424 | int rc;
|
---|
425 |
|
---|
426 | /*
|
---|
427 | * Init Shared memory
|
---|
428 | */
|
---|
429 | rc = shrmemCreate();
|
---|
430 | if (rc)
|
---|
431 | return rc;
|
---|
432 |
|
---|
433 | /*
|
---|
434 | * Init queues and semaphores.
|
---|
435 | */
|
---|
436 | rc = DaemonInit(cWorkers);
|
---|
437 | if (rc)
|
---|
438 | {
|
---|
439 | shrmemFree();
|
---|
440 | return rc;
|
---|
441 | }
|
---|
442 |
|
---|
443 | /*
|
---|
444 | * Do work!
|
---|
445 | */
|
---|
446 | rc = shrmemSendDaemon(TRUE);
|
---|
447 | while (!rc)
|
---|
448 | {
|
---|
449 | switch (pShrMem->enmMsgType)
|
---|
450 | {
|
---|
451 | case msgSubmit:
|
---|
452 | {
|
---|
453 | PJOB pJob;
|
---|
454 |
|
---|
455 | /*
|
---|
456 | * Make job entry.
|
---|
457 | */
|
---|
458 | _heap_check();
|
---|
459 | pJob = malloc((int)&((PJOB)0)->JobInfo.szzEnv[pShrMem->u1.Submit.cchEnv]);
|
---|
460 | if (pJob)
|
---|
461 | {
|
---|
462 | _heap_check();
|
---|
463 | memcpy(&pJob->JobInfo, &pShrMem->u1.Submit,
|
---|
464 | (int)&((struct Submit *)0)->szzEnv[pShrMem->u1.Submit.cchEnv]);
|
---|
465 | _heap_check();
|
---|
466 | pJob->rc = -1;
|
---|
467 | pJob->pNext = NULL;
|
---|
468 | pJob->pJobOutput = NULL;
|
---|
469 |
|
---|
470 | /*
|
---|
471 | * Insert the entry.
|
---|
472 | */
|
---|
473 | rc = DosRequestMutexSem(hmtxJobQueue, SEM_INDEFINITE_WAIT);
|
---|
474 | if (rc)
|
---|
475 | break;
|
---|
476 | if (!pJobQueue)
|
---|
477 | pJobQueueEnd = pJobQueue = pJob;
|
---|
478 | else
|
---|
479 | {
|
---|
480 | pJobQueueEnd->pNext = pJob;
|
---|
481 | pJobQueueEnd = pJob;
|
---|
482 | }
|
---|
483 | cJobs++;
|
---|
484 | DosReleaseMutexSem(hmtxJobQueue);
|
---|
485 |
|
---|
486 | /*
|
---|
487 | * Post the queue to wake up workers.
|
---|
488 | */
|
---|
489 | DosPostEventSem(hevJobQueue);
|
---|
490 | pShrMem->u1.SubmitResponse.fRc = TRUE;
|
---|
491 | }
|
---|
492 | else
|
---|
493 | {
|
---|
494 | Error("Internal Error: Out of memory (line=%d)\n", __LINE__);
|
---|
495 | pShrMem->u1.SubmitResponse.fRc = FALSE;
|
---|
496 | }
|
---|
497 | pShrMem->enmMsgType = msgSubmitResponse;
|
---|
498 | rc = shrmemSendDaemon(TRUE);
|
---|
499 | break;
|
---|
500 | }
|
---|
501 |
|
---|
502 |
|
---|
503 | case msgWait:
|
---|
504 | {
|
---|
505 | PJOB pJob = NULL;
|
---|
506 | PJOBOUTPUT pJobOutput = NULL;
|
---|
507 | char * psz;
|
---|
508 | int cch = 0;
|
---|
509 | char * pszOutput;
|
---|
510 | int cchOutput;
|
---|
511 | int rcFailure = 0;
|
---|
512 | BOOL fMore = TRUE;
|
---|
513 | ULONG ulIgnore;
|
---|
514 | void * pv;
|
---|
515 |
|
---|
516 | DosPostEventSem(hevJobQueueFine); /* just so we don't get stuck in the loop... */
|
---|
517 | do
|
---|
518 | {
|
---|
519 | /* init response message */
|
---|
520 | pShrMem->enmMsgType = msgWaitResponse;
|
---|
521 | pShrMem->u1.WaitResponse.szOutput[0] = '\0';
|
---|
522 | pszOutput = &pShrMem->u1.WaitResponse.szOutput[0];
|
---|
523 | cchOutput = sizeof(pShrMem->u1.WaitResponse.szOutput) - 1;
|
---|
524 |
|
---|
525 | /*
|
---|
526 | * Wait for output.
|
---|
527 | */
|
---|
528 | /*rc = DosWaitEventSem(hevJobQueueFine, SEM_INDEFINITE_WAIT); - there is some timing problem here, */
|
---|
529 | rc = DosWaitEventSem(hevJobQueueFine, 1000); /* timeout after 1 second. */
|
---|
530 | if (rc && rc != ERROR_TIMEOUT)
|
---|
531 | break;
|
---|
532 | rc = NO_ERROR; /* in case of TIMEOUT */
|
---|
533 |
|
---|
534 | /*
|
---|
535 | * Copy output - Optimized so we don't cause to many context switches.
|
---|
536 | */
|
---|
537 | do
|
---|
538 | {
|
---|
539 | /*
|
---|
540 | * Next job.
|
---|
541 | */
|
---|
542 | if (!pJobOutput)
|
---|
543 | {
|
---|
544 | rc = DosRequestMutexSem(hmtxJobQueueFine, SEM_INDEFINITE_WAIT);
|
---|
545 | if (rc)
|
---|
546 | break;
|
---|
547 | pv = pJob;
|
---|
548 | pJob = pJobCompleted;
|
---|
549 | if (pJob)
|
---|
550 | {
|
---|
551 | pJobCompleted = pJob->pNext;
|
---|
552 | if (!pJobCompleted)
|
---|
553 | pJobCompletedLast = NULL;
|
---|
554 | }
|
---|
555 |
|
---|
556 | if (!pJob && cJobs == cJobsFinished)
|
---|
557 | { /* all jobs finished, we may start output failures. */
|
---|
558 | pJob = pJobFailed;
|
---|
559 | if (pJob)
|
---|
560 | {
|
---|
561 | if (rcFailure == 0)
|
---|
562 | rcFailure = pJob->rc;
|
---|
563 |
|
---|
564 | pJobFailed = pJob->pNext;
|
---|
565 | if (!pJobFailed)
|
---|
566 | pJobFailedLast = NULL;
|
---|
567 | }
|
---|
568 | else
|
---|
569 | fMore = FALSE;
|
---|
570 | }
|
---|
571 | else
|
---|
572 | DosResetEventSem(hevJobQueueFine, &ulIgnore); /* No more output, prepare wait. */
|
---|
573 | DosReleaseMutexSem(hmtxJobQueueFine);
|
---|
574 |
|
---|
575 | if (pJob && pJob->pJobOutput)
|
---|
576 | {
|
---|
577 | pJobOutput = pJob->pJobOutput;
|
---|
578 | psz = pJobOutput->szOutput;
|
---|
579 | cch = pJobOutput->cchOutput;
|
---|
580 | }
|
---|
581 | if (pv)
|
---|
582 | free(pv);
|
---|
583 | }
|
---|
584 |
|
---|
585 | /*
|
---|
586 | * Anything to output?
|
---|
587 | */
|
---|
588 | if (pJobOutput)
|
---|
589 | {
|
---|
590 | /*
|
---|
591 | * Copy output.
|
---|
592 | */
|
---|
593 | do
|
---|
594 | {
|
---|
595 | if (cch)
|
---|
596 | { /* copy */
|
---|
597 | int cchCopy = min(cch, cchOutput);
|
---|
598 | memcpy(pszOutput, psz, cchCopy);
|
---|
599 | psz += cchCopy; cch -= cchCopy;
|
---|
600 | pszOutput += cchCopy; cchOutput -= cchCopy;
|
---|
601 | }
|
---|
602 | if (!cch)
|
---|
603 | { /* next chunk */
|
---|
604 | pv = pJobOutput;
|
---|
605 | pJobOutput = pJobOutput->pNext;
|
---|
606 | if (pJobOutput)
|
---|
607 | {
|
---|
608 | psz = &pJobOutput->szOutput[0];
|
---|
609 | cch = pJobOutput->cchOutput;
|
---|
610 | }
|
---|
611 | free(pv);
|
---|
612 | }
|
---|
613 | } while (cch && cchOutput);
|
---|
614 | }
|
---|
615 | else
|
---|
616 | break; /* no more output, let's send what we got. */
|
---|
617 |
|
---|
618 | } while (!rc && fMore && cchOutput);
|
---|
619 |
|
---|
620 | /*
|
---|
621 | * We've got a message to send.
|
---|
622 | */
|
---|
623 | if (rc)
|
---|
624 | break;
|
---|
625 | *pszOutput = '\0';
|
---|
626 | pShrMem->u1.WaitResponse.rc = rcFailure;
|
---|
627 | pShrMem->u1.WaitResponse.fMore = fMore;
|
---|
628 | rc = shrmemSendDaemon(TRUE);
|
---|
629 | } while (!rc && fMore);
|
---|
630 | break;
|
---|
631 | }
|
---|
632 |
|
---|
633 |
|
---|
634 | case msgKill:
|
---|
635 | {
|
---|
636 | pShrMem->enmMsgType = msgKillResponse;
|
---|
637 | pShrMem->u1.KillResponse.fRc = TRUE;
|
---|
638 | shrmemSendDaemon(FALSE);
|
---|
639 | rc = -1;
|
---|
640 | break;
|
---|
641 | }
|
---|
642 |
|
---|
643 | default:
|
---|
644 | Error("Internal error: Invalid message id %d\n", pShrMem->enmMsgType);
|
---|
645 | pShrMem->enmMsgType = msgUnknown;
|
---|
646 | rc = shrmemSendDaemon(TRUE);
|
---|
647 | }
|
---|
648 | }
|
---|
649 |
|
---|
650 | /*
|
---|
651 | * Set dying msg type. shrmemFree posts the hevClient so clients
|
---|
652 | * waiting for the daemon to respond will quit.
|
---|
653 | */
|
---|
654 | pShrMem->enmMsgType = msgDying;
|
---|
655 |
|
---|
656 | /*
|
---|
657 | * Cleanup.
|
---|
658 | */
|
---|
659 | shrmemFree();
|
---|
660 | DosCloseMutexSem(hmtxJobQueue);
|
---|
661 | DosCloseMutexSem(hmtxJobQueueFine);
|
---|
662 | DosCloseEventSem(hevJobQueueFine);
|
---|
663 | DosCloseMutexSem(hmtxExec);
|
---|
664 | DosCloseEventSem(hevJobQueue);
|
---|
665 |
|
---|
666 | _dump_allocated(16);
|
---|
667 |
|
---|
668 | return 0;
|
---|
669 | }
|
---|
670 |
|
---|
671 |
|
---|
672 | /**
|
---|
673 | * Help which does most of the daemon init stuff.
|
---|
674 | * @returns 0 on success.
|
---|
675 | * @param cWorkers Number of worker threads to start.
|
---|
676 | */
|
---|
677 | int DaemonInit(int cWorkers)
|
---|
678 | {
|
---|
679 | int rc;
|
---|
680 | int i;
|
---|
681 |
|
---|
682 | /*
|
---|
683 | * Init queues and semaphores.
|
---|
684 | */
|
---|
685 | rc = DosCreateEventSem(NULL, &hevJobQueue, 0, FALSE);
|
---|
686 | if (!rc)
|
---|
687 | {
|
---|
688 | rc = DosCreateMutexSem(NULL, &hmtxJobQueue, 0, FALSE);
|
---|
689 | if (!rc)
|
---|
690 | {
|
---|
691 | rc = DosCreateMutexSem(NULL, &hmtxJobQueueFine, 0, FALSE);
|
---|
692 | if (!rc)
|
---|
693 | {
|
---|
694 | rc = DosCreateEventSem(NULL, &hevJobQueueFine, 0, FALSE);
|
---|
695 | if (!rc)
|
---|
696 | {
|
---|
697 | rc = DosCreateMutexSem(NULL, &hmtxExec, 0, FALSE);
|
---|
698 | if (!rc)
|
---|
699 | {
|
---|
700 | /*
|
---|
701 | * Start workers.
|
---|
702 | */
|
---|
703 | rc = 0;
|
---|
704 | for (i = 0; i < cWorkers; i++)
|
---|
705 | if (_beginthread(Worker, NULL, 64*1024, (void*)i) == -1)
|
---|
706 | {
|
---|
707 | Error("Fatal error: failed to create worker thread no. %d\n", i);
|
---|
708 | rc = -1;
|
---|
709 | break;
|
---|
710 | }
|
---|
711 | if (!rc)
|
---|
712 | {
|
---|
713 | DosSetMaxFH(cWorkers * 6 + 20);
|
---|
714 | return 0; /* success! */
|
---|
715 | }
|
---|
716 |
|
---|
717 | /* failure */
|
---|
718 | DosCloseMutexSem(hmtxExec);
|
---|
719 | }
|
---|
720 | else
|
---|
721 | Error("Fatal error: failed to create exec mutex. rc=%d", rc);
|
---|
722 | DosCloseEventSem(hevJobQueueFine);
|
---|
723 | }
|
---|
724 | else
|
---|
725 | Error("Fatal error: failed to create job queue fine event sem. rc=%d", rc);
|
---|
726 | DosCloseMutexSem(hmtxJobQueueFine);
|
---|
727 | }
|
---|
728 | else
|
---|
729 | Error("Fatal error: failed to create job queue fine mutex. rc=%d", rc);
|
---|
730 | DosCloseMutexSem(hmtxJobQueue);
|
---|
731 | }
|
---|
732 | else
|
---|
733 | Error("Fatal error: failed to create job queue mutex. rc=%d", rc);
|
---|
734 | DosCloseEventSem(hevJobQueue);
|
---|
735 | }
|
---|
736 | else
|
---|
737 | Error("Fatal error: failed to create job queue event sem. rc=%d", rc);
|
---|
738 |
|
---|
739 | return rc;
|
---|
740 | }
|
---|
741 |
|
---|
742 |
|
---|
743 | /**
|
---|
744 | * Daemon signal handler.
|
---|
745 | */
|
---|
746 | void signalhandlerDaemon(int sig)
|
---|
747 | {
|
---|
748 | /*
|
---|
749 | * Set dying msg type. shrmemFree posts the hevClient so clients
|
---|
750 | * waiting for the daemon to respond will quit.
|
---|
751 | */
|
---|
752 | pShrMem->enmMsgType = msgDying;
|
---|
753 |
|
---|
754 | /*
|
---|
755 | * Free and exit.
|
---|
756 | */
|
---|
757 | shrmemFree();
|
---|
758 | exit(-42);
|
---|
759 | sig = sig;
|
---|
760 | }
|
---|
761 |
|
---|
762 |
|
---|
763 | /**
|
---|
764 | * Client signal handler.
|
---|
765 | */
|
---|
766 | void signalhandlerClient(int sig)
|
---|
767 | {
|
---|
768 | shrmemFree();
|
---|
769 | exit(-42);
|
---|
770 | sig = sig;
|
---|
771 | }
|
---|
772 |
|
---|
773 |
|
---|
774 |
|
---|
775 | /**
|
---|
776 | * Worker thread.
|
---|
777 | * @param iWorkerId The worker process id.
|
---|
778 | * @sketch
|
---|
779 | */
|
---|
780 | void Worker(void * iWorkerId)
|
---|
781 | {
|
---|
782 | PATHCACHE PathCache;
|
---|
783 | memset(&PathCache, 0, sizeof(PathCache));
|
---|
784 |
|
---|
785 | while (!DosWaitEventSem(hevJobQueue, SEM_INDEFINITE_WAIT))
|
---|
786 | {
|
---|
787 | PJOB pJob;
|
---|
788 |
|
---|
789 | /*
|
---|
790 | * Get job.
|
---|
791 | */
|
---|
792 | if (DosRequestMutexSem(hmtxJobQueue, SEM_INDEFINITE_WAIT))
|
---|
793 | return;
|
---|
794 | pJob = pJobQueue;
|
---|
795 | if (pJob)
|
---|
796 | {
|
---|
797 | if (pJob != pJobQueueEnd)
|
---|
798 | pJobQueue = pJob->pNext;
|
---|
799 | else
|
---|
800 | {
|
---|
801 | ULONG ulIgnore;
|
---|
802 | pJobQueue = pJobQueueEnd = NULL;
|
---|
803 | DosResetEventSem(hevJobQueue, &ulIgnore);
|
---|
804 | }
|
---|
805 | }
|
---|
806 | DosReleaseMutexSem(hmtxJobQueue);
|
---|
807 |
|
---|
808 | /*
|
---|
809 | * Execute job.
|
---|
810 | */
|
---|
811 | if (pJob)
|
---|
812 | {
|
---|
813 | int rc;
|
---|
814 | char szArg[4096];
|
---|
815 | char szObj[256];
|
---|
816 | PJOBOUTPUT pJobOutput = NULL;
|
---|
817 | PJOBOUTPUT pJobOutputLast = NULL;
|
---|
818 | RESULTCODES Res;
|
---|
819 | PID pid;
|
---|
820 | HFILE hStdOut = HF_STDOUT;
|
---|
821 | HFILE hStdErr = HF_STDERR;
|
---|
822 | HFILE hStdOutSave = -1;
|
---|
823 | HFILE hStdErrSave = -1;
|
---|
824 | HPIPE hPipeR = NULLHANDLE;
|
---|
825 | HPIPE hPipeW = NULLHANDLE;
|
---|
826 |
|
---|
827 | //printf("debug-%d: start %s\n", iWorkerId, pJob->JobInfo.szCommand);
|
---|
828 |
|
---|
829 | /*
|
---|
830 | * Redirect output and start process.
|
---|
831 | */
|
---|
832 | WorkerArguments(&szArg[0], &pJob->JobInfo.szzEnv[0], &pJob->JobInfo.szCommand[0],
|
---|
833 | &pJob->JobInfo.szCurrentDir[0], &PathCache);
|
---|
834 | rc = DosCreatePipe(&hPipeR, &hPipeW, sizeof(pJobOutput->szOutput) - 1);
|
---|
835 | if (rc)
|
---|
836 | {
|
---|
837 | Error("Internal Error: Failed to create pipe! rc=%d\n", rc);
|
---|
838 | return;
|
---|
839 | }
|
---|
840 |
|
---|
841 | if (DosRequestMutexSem(hmtxExec, SEM_INDEFINITE_WAIT))
|
---|
842 | {
|
---|
843 | DosClose(hPipeR);
|
---|
844 | DosClose(hPipeW);
|
---|
845 | return;
|
---|
846 | }
|
---|
847 |
|
---|
848 | pJob->pJobOutput = pJobOutput = pJobOutputLast = malloc(sizeof(JOBOUTPUT));
|
---|
849 | pJobOutput->pNext = NULL;
|
---|
850 | pJobOutput->cchOutput = sprintf(pJobOutput->szOutput, "Job: %s\n", pJob->JobInfo.szCommand);
|
---|
851 |
|
---|
852 | rc = DosSetDefaultDisk( pJob->JobInfo.szCurrentDir[0] >= 'a'
|
---|
853 | ? pJob->JobInfo.szCurrentDir[0] - 'a' + 1
|
---|
854 | : pJob->JobInfo.szCurrentDir[0] - 'A' + 1);
|
---|
855 | rc += DosSetCurrentDir(pJob->JobInfo.szCurrentDir);
|
---|
856 | if (!rc)
|
---|
857 | {
|
---|
858 | assert( pJob->JobInfo.szzEnv[pJob->JobInfo.cchEnv-1] == '\0'
|
---|
859 | && pJob->JobInfo.szzEnv[pJob->JobInfo.cchEnv-2] == '\0');
|
---|
860 | DosDupHandle(HF_STDOUT, &hStdOutSave);
|
---|
861 | DosDupHandle(HF_STDERR, &hStdErrSave);
|
---|
862 | DosDupHandle(hPipeW, &hStdOut);
|
---|
863 | DosDupHandle(hPipeW, &hStdErr);
|
---|
864 | rc = DosExecPgm(szObj, sizeof(szObj), EXEC_ASYNCRESULT,
|
---|
865 | szArg, pJob->JobInfo.szzEnv, &Res, szArg);
|
---|
866 | DosClose(hStdOut); hStdOut = HF_STDOUT;
|
---|
867 | DosClose(hStdErr); hStdErr = HF_STDERR;
|
---|
868 | DosDupHandle(hStdOutSave, &hStdOut);
|
---|
869 | DosDupHandle(hStdErrSave, &hStdErr);
|
---|
870 | DosClose(hStdOutSave);
|
---|
871 | DosClose(hStdErrSave);
|
---|
872 | DosReleaseMutexSem(hmtxExec);
|
---|
873 | DosClose(hPipeW);
|
---|
874 |
|
---|
875 |
|
---|
876 | /*
|
---|
877 | * Read Output.
|
---|
878 | */
|
---|
879 | if (!rc)
|
---|
880 | {
|
---|
881 | ULONG cchRead;
|
---|
882 | ULONG cchRead2 = 0;
|
---|
883 |
|
---|
884 | cchRead = sizeof(pJobOutput->szOutput) - pJobOutput->cchOutput - 1;
|
---|
885 | while (((rc = DosRead(hPipeR,
|
---|
886 | &pJobOutput->szOutput[pJobOutput->cchOutput],
|
---|
887 | cchRead, &cchRead2)) == NO_ERROR
|
---|
888 | || rc == ERROR_MORE_DATA)
|
---|
889 | && cchRead2 != 0)
|
---|
890 | {
|
---|
891 | pJobOutput->cchOutput += cchRead2;
|
---|
892 | pJobOutput->szOutput[pJobOutput->cchOutput] = '\0';
|
---|
893 |
|
---|
894 | /* prepare next read */
|
---|
895 | cchRead = sizeof(pJobOutput->szOutput) - pJobOutput->cchOutput - 1;
|
---|
896 | if (cchRead < 16)
|
---|
897 | {
|
---|
898 | pJobOutput = pJobOutput->pNext = malloc(sizeof(JOBOUTPUT));
|
---|
899 | pJobOutput->pNext = NULL;
|
---|
900 | pJobOutput->cchOutput = 0;
|
---|
901 | cchRead = sizeof(pJobOutput->szOutput) - 1;
|
---|
902 | }
|
---|
903 | cchRead2 = 0;
|
---|
904 | }
|
---|
905 | rc = 0;
|
---|
906 | }
|
---|
907 |
|
---|
908 | /* finished reading */
|
---|
909 | DosClose(hPipeR);
|
---|
910 |
|
---|
911 | /*
|
---|
912 | * Get result.
|
---|
913 | */
|
---|
914 | if (!rc)
|
---|
915 | {
|
---|
916 | DosWaitChild(DCWA_PROCESS, DCWW_WAIT, &Res, &pid, Res.codeTerminate);
|
---|
917 | if ( Res.codeResult <= pJob->JobInfo.rcIgnore
|
---|
918 | && Res.codeTerminate == TC_EXIT)
|
---|
919 | pJob->rc = 0;
|
---|
920 | else
|
---|
921 | {
|
---|
922 | pJob->rc = -1;
|
---|
923 | rc = sprintf(szArg, "failed with rc=%d term=%d\n", Res.codeResult, Res.codeTerminate);
|
---|
924 | if (rc + pJobOutput->cchOutput + 1 >= sizeof(pJobOutput->szOutput))
|
---|
925 | {
|
---|
926 | pJobOutput = pJobOutput->pNext = malloc(sizeof(JOBOUTPUT));
|
---|
927 | pJobOutput->pNext = NULL;
|
---|
928 | pJobOutput->cchOutput = 0;
|
---|
929 | }
|
---|
930 | strcpy(&pJobOutput->szOutput[pJobOutput->cchOutput], szArg);
|
---|
931 | pJobOutput->cchOutput += rc;
|
---|
932 | }
|
---|
933 | }
|
---|
934 | else
|
---|
935 | {
|
---|
936 | pJobOutput->cchOutput += sprintf(&pJobOutput->szOutput[pJobOutput->cchOutput],
|
---|
937 | "DosExecPgm failed with rc=%d for command %s %s\n"
|
---|
938 | " obj=%s\n",
|
---|
939 | rc, szArg, pJob->JobInfo.szCommand, szObj);
|
---|
940 | pJob->rc = -1;
|
---|
941 | }
|
---|
942 | }
|
---|
943 | else
|
---|
944 | {
|
---|
945 | /*
|
---|
946 | * ChDir failed.
|
---|
947 | */
|
---|
948 | DosReleaseMutexSem(hmtxExec);
|
---|
949 | pJobOutput->cchOutput += sprintf(&pJobOutput->szOutput[pJobOutput->cchOutput ],
|
---|
950 | "Failed to set current directory to: %s\n",
|
---|
951 | pJob->JobInfo.szCurrentDir);
|
---|
952 | pJob->rc = -1;
|
---|
953 | DosClose(hPipeR);
|
---|
954 | }
|
---|
955 |
|
---|
956 |
|
---|
957 | /*
|
---|
958 | * Insert result in result queue.
|
---|
959 | */
|
---|
960 | if (DosRequestMutexSem(hmtxJobQueueFine, SEM_INDEFINITE_WAIT))
|
---|
961 | return;
|
---|
962 | pJob->pNext = NULL;
|
---|
963 | if (!pJob->rc) /* 0 on success. */
|
---|
964 | {
|
---|
965 | if (pJobCompletedLast)
|
---|
966 | pJobCompletedLast->pNext = pJob;
|
---|
967 | else
|
---|
968 | pJobCompleted = pJob;
|
---|
969 | pJobCompletedLast = pJob;
|
---|
970 | }
|
---|
971 | else
|
---|
972 | {
|
---|
973 | if (pJobFailedLast)
|
---|
974 | pJobFailedLast->pNext = pJob;
|
---|
975 | else
|
---|
976 | pJobFailed = pJob;
|
---|
977 | pJobFailedLast = pJob;
|
---|
978 | }
|
---|
979 | cJobsFinished++;
|
---|
980 | DosReleaseMutexSem(hmtxJobQueueFine);
|
---|
981 | /* wake up Wait. */
|
---|
982 | DosPostEventSem(hevJobQueueFine);
|
---|
983 | //printf("debug-%d: fine\n", iWorkerId);
|
---|
984 | }
|
---|
985 | }
|
---|
986 | iWorkerId = iWorkerId;
|
---|
987 | }
|
---|
988 |
|
---|
989 |
|
---|
990 | /**
|
---|
991 | * Builds the input to DosExecPgm.
|
---|
992 | * Will execute programs directly and command thru the shell.
|
---|
993 | *
|
---|
994 | * @returns pszArg.
|
---|
995 | * @param pszArg Arguments to DosExecPgm.(output)
|
---|
996 | * Assumes that the buffer is large enought.
|
---|
997 | * @param pszzEnv Pointer to environment block.
|
---|
998 | * @param pszCommand Command to execute.
|
---|
999 | * @param pszCurDir From where the command is to executed.
|
---|
1000 | * @param pPathCache Used to cache the last path, executable, and the search result.
|
---|
1001 | */
|
---|
1002 | char *WorkerArguments(char *pszArg, const char *pszzEnv, const char *pszCommand, char *pszCurDir, PPATHCACHE pPathCache)
|
---|
1003 | {
|
---|
1004 | BOOL fCMD = FALSE;
|
---|
1005 | const char *psz;
|
---|
1006 | const char *psz2;
|
---|
1007 | char * pszW;
|
---|
1008 | char ch;
|
---|
1009 | int cch;
|
---|
1010 | APIRET rc;
|
---|
1011 |
|
---|
1012 | /*
|
---|
1013 | * Check if this is multiple command separated by either &, && or |.
|
---|
1014 | * Currently ignoring quotes for this test.
|
---|
1015 | */
|
---|
1016 | if ( strchr(pszCommand, '&')
|
---|
1017 | || strchr(pszCommand, '|'))
|
---|
1018 | {
|
---|
1019 | strcpy(pszArg, "cmd.exe"); /* doesn't use comspec, just defaults to cmd.exe in all cases. */
|
---|
1020 | fCMD = TRUE;
|
---|
1021 | psz2 = pszCommand; /* start of arguments. */
|
---|
1022 | }
|
---|
1023 | else
|
---|
1024 | {
|
---|
1025 | char chEnd = ' ';
|
---|
1026 |
|
---|
1027 | /*
|
---|
1028 | * Parse out the first name.
|
---|
1029 | */
|
---|
1030 | for (psz = pszCommand; *psz == '\t' || *psz == ' ';) //strip(,'L');
|
---|
1031 | psz++;
|
---|
1032 | if (*psz == '"' || *psz == '\'')
|
---|
1033 | chEnd = *psz++;
|
---|
1034 | psz2 = psz;
|
---|
1035 | if (chEnd == ' ')
|
---|
1036 | {
|
---|
1037 | while ((ch = *psz) != '\0' && ch != ' ' && ch != '\t')
|
---|
1038 | psz++;
|
---|
1039 | }
|
---|
1040 | else
|
---|
1041 | {
|
---|
1042 | while ((ch = *psz) != '\0' && ch != chEnd)
|
---|
1043 | psz++;
|
---|
1044 | }
|
---|
1045 | *pszArg = '\0';
|
---|
1046 | strncat(pszArg, psz2, psz - psz2);
|
---|
1047 | psz2 = psz+1; /* start of arguments. */
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 |
|
---|
1051 | /*
|
---|
1052 | * Resolve the executable name if not qualified.
|
---|
1053 | * NB! We doesn't fully support references to other driveletters yet. (TODO/BUGBUG)
|
---|
1054 | */
|
---|
1055 | /* correct slashes */
|
---|
1056 | pszW = pszArg;
|
---|
1057 | while ((pszW = strchr(pszW, '//')) != NULL)
|
---|
1058 | *pszW++ = '\\';
|
---|
1059 |
|
---|
1060 | /* make sure it ends with .exe */
|
---|
1061 | pszW = pszArg + strlen(pszArg) - 1;
|
---|
1062 | while (pszW > pszArg && *pszW != '.' && *pszW != '\\')
|
---|
1063 | pszW--;
|
---|
1064 | if (*pszW != '.')
|
---|
1065 | strcat(pszArg, ".exe");
|
---|
1066 |
|
---|
1067 | if (pszArg[1] != ':' || *pszArg == *pszCurDir)
|
---|
1068 | {
|
---|
1069 | rc = -1; /* indicate that we've not found the file. */
|
---|
1070 |
|
---|
1071 | /* relative path? - expand it */
|
---|
1072 | if (strchr(pszArg, '\\') || pszArg[1] == ':')
|
---|
1073 | { /* relative path - expand it and check for file existence */
|
---|
1074 | fileNormalize(pszArg, pszCurDir);
|
---|
1075 | rc = fileExist(pszArg);
|
---|
1076 | }
|
---|
1077 | else
|
---|
1078 | { /* Search path. */
|
---|
1079 | const char *pszPath = pszzEnv;
|
---|
1080 | while (*pszPath != '\0' && strncmp(pszPath, "PATH=", 5))
|
---|
1081 | pszPath += strlen(pszPath) + 1;
|
---|
1082 |
|
---|
1083 | if (pszPath && *pszPath != '\0')
|
---|
1084 | {
|
---|
1085 | /* check cache */
|
---|
1086 | if ( !strcmp(pPathCache->szProgram, pszArg)
|
---|
1087 | && !strcmp(pPathCache->szPath, pszPath)
|
---|
1088 | && !strcmp(pPathCache->szCurDir, pszCurDir)
|
---|
1089 | )
|
---|
1090 | {
|
---|
1091 | strcpy(pszArg, pPathCache->szResult);
|
---|
1092 | rc = fileExist(pszArg);
|
---|
1093 | }
|
---|
1094 |
|
---|
1095 | if (rc)
|
---|
1096 | { /* search path */
|
---|
1097 | char szResult[CCHMAXPATH];
|
---|
1098 | rc = DosSearchPath(SEARCH_IGNORENETERRS, pszPath, pszArg, &szResult[0] , sizeof(szResult));
|
---|
1099 | if (!rc)
|
---|
1100 | {
|
---|
1101 | strcpy(pszArg, szResult);
|
---|
1102 |
|
---|
1103 | /* update cache */
|
---|
1104 | strcpy(pPathCache->szProgram, pszArg);
|
---|
1105 | strcpy(pPathCache->szPath, pszPath);
|
---|
1106 | strcpy(pPathCache->szCurDir, pszCurDir);
|
---|
1107 | strcpy(pPathCache->szResult, szResult);
|
---|
1108 | }
|
---|
1109 | }
|
---|
1110 | }
|
---|
1111 | }
|
---|
1112 | }
|
---|
1113 | /* else nothing to do - assume full path (btw. we don't have the current dir for other drives anyway :-) */
|
---|
1114 | else
|
---|
1115 | rc = !fCMD ? fileExist(pszArg) : NO_ERROR;
|
---|
1116 |
|
---|
1117 | /* In case of error use CMD */
|
---|
1118 | if (rc && !fCMD)
|
---|
1119 | {
|
---|
1120 | strcpy(pszArg, "cmd.exe"); /* doesn't use comspec, just defaults to cmd.exe in all cases. */
|
---|
1121 | fCMD = TRUE;
|
---|
1122 | psz2 = pszCommand; /* start of arguments. */
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 |
|
---|
1126 | /*
|
---|
1127 | * Complete the argument string.
|
---|
1128 | * ---
|
---|
1129 | * szArg current holds the command.
|
---|
1130 | * psz2 points to the first parameter. (needs strip(,'L'))
|
---|
1131 | */
|
---|
1132 | while ((ch = *psz2) != '\0' && (ch == '\t' || ch == ' '))
|
---|
1133 | psz2++;
|
---|
1134 |
|
---|
1135 | pszW = pszArg + strlen(pszArg) + 1;
|
---|
1136 | cch = strlen(psz2);
|
---|
1137 | if (!fCMD)
|
---|
1138 | {
|
---|
1139 | memcpy(pszW, psz2, ++cch);
|
---|
1140 | pszW[cch] = '\0';
|
---|
1141 | }
|
---|
1142 | else
|
---|
1143 | {
|
---|
1144 | strcpy(pszW, "/C \"");
|
---|
1145 | pszW += strlen(pszW);
|
---|
1146 | memcpy(pszW, psz2, cch);
|
---|
1147 | memcpy(pszW + cch, "\"\0", 3);
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | return pszArg;
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 |
|
---|
1154 |
|
---|
1155 | /**
|
---|
1156 | * Normalizes the path slashes for the filename. It will partially expand paths too.
|
---|
1157 | * @returns pszFilename
|
---|
1158 | * @param pszFilename Pointer to filename string. Not empty string!
|
---|
1159 | * Much space to play with.
|
---|
1160 | * @remark (From fastdep.)
|
---|
1161 | */
|
---|
1162 | char *fileNormalize(char *pszFilename, char *pszCurDir)
|
---|
1163 | {
|
---|
1164 | char * psz;
|
---|
1165 | int aiSlashes[CCHMAXPATH/2];
|
---|
1166 | int cSlashes;
|
---|
1167 | int i;
|
---|
1168 |
|
---|
1169 | /*
|
---|
1170 | * Init stuff.
|
---|
1171 | */
|
---|
1172 | for (i = 1, cSlashes; pszCurDir[i] != '\0'; i++)
|
---|
1173 | {
|
---|
1174 | if (pszCurDir[i] == '/')
|
---|
1175 | pszCurDir[i] = '\\';
|
---|
1176 | if (pszCurDir[i] == '\\')
|
---|
1177 | aiSlashes[cSlashes++] = i;
|
---|
1178 | }
|
---|
1179 | if (pszCurDir[i-1] != '\\')
|
---|
1180 | {
|
---|
1181 | aiSlashes[cSlashes] = i;
|
---|
1182 | pszCurDir[i++] = '\\';
|
---|
1183 | pszCurDir[i] = '\0';
|
---|
1184 | }
|
---|
1185 |
|
---|
1186 |
|
---|
1187 | /* expand path? */
|
---|
1188 | pszFilename = psz;
|
---|
1189 | if (pszFilename[1] != ':')
|
---|
1190 | { /* relative path */
|
---|
1191 | int iSlash;
|
---|
1192 | char szFile[CCHMAXPATH];
|
---|
1193 | char * psz = szFile;
|
---|
1194 |
|
---|
1195 | strcpy(szFile, pszFilename);
|
---|
1196 | iSlash = *psz == '\\' ? 1 : cSlashes;
|
---|
1197 | while (*psz != '\0')
|
---|
1198 | {
|
---|
1199 | if (*psz == '.' && psz[1] == '.' && psz[2] == '\\')
|
---|
1200 | { /* up one directory */
|
---|
1201 | if (iSlash > 0)
|
---|
1202 | iSlash--;
|
---|
1203 | psz += 3;
|
---|
1204 | }
|
---|
1205 | else if (*psz == '.' && psz[1] == '\\')
|
---|
1206 | { /* no change */
|
---|
1207 | psz += 2;
|
---|
1208 | }
|
---|
1209 | else
|
---|
1210 | { /* completed expantion! */
|
---|
1211 | strncpy(pszFilename, pszCurDir, aiSlashes[iSlash]+1);
|
---|
1212 | strcpy(pszFilename + aiSlashes[iSlash]+1, psz);
|
---|
1213 | break;
|
---|
1214 | }
|
---|
1215 | }
|
---|
1216 | }
|
---|
1217 | /* else: assume full path */
|
---|
1218 |
|
---|
1219 | return psz;
|
---|
1220 | }
|
---|
1221 |
|
---|
1222 | /**
|
---|
1223 | * Checks if a given file exist.
|
---|
1224 | * @returns 0 if the file exists. (NO_ERROR)
|
---|
1225 | * 2 if the file doesn't exist. (ERROR_FILE_NOT_FOUND)
|
---|
1226 | * @param pszFilename Name of the file to check existance for.
|
---|
1227 | */
|
---|
1228 | APIRET fileExist(const char *pszFilename)
|
---|
1229 | {
|
---|
1230 | FILESTATUS3 fsts3;
|
---|
1231 | return DosQueryPathInfo(pszFilename, FIL_STANDARD, &fsts3, sizeof(fsts3));
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 |
|
---|
1235 | /**
|
---|
1236 | * Submits a command to the daemon.
|
---|
1237 | * @returns 0 on success.
|
---|
1238 | * -3 on failure.
|
---|
1239 | * @param rcIgnore Ignores returcodes ranging from 0 to rcIgnore.
|
---|
1240 | */
|
---|
1241 | int Submit(int rcIgnore)
|
---|
1242 | {
|
---|
1243 | int cch;
|
---|
1244 | int rc;
|
---|
1245 | char * psz;
|
---|
1246 | PPIB ppib;
|
---|
1247 | PTIB ptib;
|
---|
1248 |
|
---|
1249 | DosGetInfoBlocks(&ptib, &ppib);
|
---|
1250 | rc = shrmemOpen();
|
---|
1251 | if (rc)
|
---|
1252 | return rc;
|
---|
1253 |
|
---|
1254 | /*
|
---|
1255 | * Build message.
|
---|
1256 | */
|
---|
1257 | pShrMem->enmMsgType = msgSubmit;
|
---|
1258 | pShrMem->u1.Submit.rcIgnore = rcIgnore;
|
---|
1259 | _getcwd(pShrMem->u1.Submit.szCurrentDir, sizeof(pShrMem->u1.Submit.szCurrentDir));
|
---|
1260 |
|
---|
1261 | /* command */
|
---|
1262 | psz = ppib->pib_pchcmd;
|
---|
1263 | psz += strlen(psz) + 1 + 7; /* 7 = strlen("submit ")*/
|
---|
1264 | while (*psz == ' ' || *psz == '\t')
|
---|
1265 | psz++;
|
---|
1266 | if (*psz == '-')
|
---|
1267 | {
|
---|
1268 | while (*psz != ' ' && *psz != '\t')
|
---|
1269 | psz++;
|
---|
1270 | while (*psz == ' ' || *psz == '\t')
|
---|
1271 | psz++;
|
---|
1272 | }
|
---|
1273 | cch = strlen(psz) + 1;
|
---|
1274 | if (cch > sizeof(pShrMem->u1.Submit.szCommand))
|
---|
1275 | {
|
---|
1276 | Error("Fatal error: Command too long.\n");
|
---|
1277 | shrmemFree();
|
---|
1278 | return -1;
|
---|
1279 | }
|
---|
1280 | if (*psz == '"' && psz[cch-2] == '"') /* remove start & end quotes if any */
|
---|
1281 | {
|
---|
1282 | cch--;
|
---|
1283 | psz++;
|
---|
1284 | }
|
---|
1285 | memcpy(&pShrMem->u1.Submit.szCommand[0], psz, cch);
|
---|
1286 |
|
---|
1287 | /* environment */
|
---|
1288 | for (cch = 1, psz = ppib->pib_pchenv; *psz != '\0';)
|
---|
1289 | {
|
---|
1290 | int cchVar = strlen(psz) + 1;
|
---|
1291 | cch += cchVar;
|
---|
1292 | psz += cchVar;
|
---|
1293 | }
|
---|
1294 | if ( ppib->pib_pchenv[cch-2] != '\0'
|
---|
1295 | || ppib->pib_pchenv[cch-1] != '\0')
|
---|
1296 | {
|
---|
1297 | Error("internal error\n");
|
---|
1298 | return -1;
|
---|
1299 | }
|
---|
1300 | if (cch > sizeof(pShrMem->u1.Submit.szzEnv))
|
---|
1301 | {
|
---|
1302 | Error("Fatal error: environment is to bit, cchEnv=%d\n", cch);
|
---|
1303 | shrmemFree();
|
---|
1304 | return -ERROR_BAD_ENVIRONMENT;
|
---|
1305 | }
|
---|
1306 | pShrMem->u1.Submit.cchEnv = cch;
|
---|
1307 | memcpy(&pShrMem->u1.Submit.szzEnv[0], ppib->pib_pchenv, cch);
|
---|
1308 |
|
---|
1309 |
|
---|
1310 | /*
|
---|
1311 | * Send message and get respons.
|
---|
1312 | */
|
---|
1313 | rc = shrmemSendClient(msgSubmitResponse);
|
---|
1314 | if (rc)
|
---|
1315 | {
|
---|
1316 | shrmemFree();
|
---|
1317 | return rc;
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 | rc = !pShrMem->u1.SubmitResponse.fRc;
|
---|
1321 | shrmemFree();
|
---|
1322 | return rc;
|
---|
1323 | }
|
---|
1324 |
|
---|
1325 |
|
---|
1326 | /**
|
---|
1327 | * Waits for the commands to complete.
|
---|
1328 | * Will write all output from completed command to stdout.
|
---|
1329 | * Will write failing commands last.
|
---|
1330 | * @returns Count of failing commands.
|
---|
1331 | */
|
---|
1332 | int Wait(void)
|
---|
1333 | {
|
---|
1334 | int rc;
|
---|
1335 |
|
---|
1336 | rc = shrmemOpen();
|
---|
1337 | if (rc)
|
---|
1338 | return rc;
|
---|
1339 | do
|
---|
1340 | {
|
---|
1341 | pShrMem->enmMsgType = msgWait;
|
---|
1342 | pShrMem->u1.Wait.iNothing = 0;
|
---|
1343 | rc = shrmemSendClient(msgWaitResponse);
|
---|
1344 | if (rc)
|
---|
1345 | {
|
---|
1346 | shrmemFree();
|
---|
1347 | return -1;
|
---|
1348 | }
|
---|
1349 | printf("%s", pShrMem->u1.WaitResponse.szOutput);
|
---|
1350 | /*
|
---|
1351 | * Release the client mutex if more data and yield the CPU.
|
---|
1352 | * So we can submit more work. (Odin nmake lib...)
|
---|
1353 | */
|
---|
1354 | if (pShrMem->u1.WaitResponse.fMore)
|
---|
1355 | {
|
---|
1356 | DosReleaseMutexSem(pShrMem->hmtxClient);
|
---|
1357 | DosSleep(0);
|
---|
1358 | rc = DosRequestMutexSem(pShrMem->hmtxClient, SEM_INDEFINITE_WAIT);
|
---|
1359 | if (rc)
|
---|
1360 | {
|
---|
1361 | Error("Fatal error: failed to get client mutex. rc=%d\n", rc);
|
---|
1362 | shrmemFree();
|
---|
1363 | return -1;
|
---|
1364 | }
|
---|
1365 | }
|
---|
1366 | } while (pShrMem->u1.WaitResponse.fMore);
|
---|
1367 |
|
---|
1368 | rc = pShrMem->u1.WaitResponse.rc;
|
---|
1369 | shrmemFree();
|
---|
1370 | return rc;
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 |
|
---|
1374 | /**
|
---|
1375 | * Checks if the daemon is running.
|
---|
1376 | */
|
---|
1377 | int QueryRunning(void)
|
---|
1378 | {
|
---|
1379 | APIRET rc;
|
---|
1380 | rc = DosGetNamedSharedMem((PPVOID)(PVOID)&pShrMem,
|
---|
1381 | SHARED_MEM_NAME,
|
---|
1382 | PAG_READ | PAG_WRITE);
|
---|
1383 | if (!rc)
|
---|
1384 | DosFreeMem(pShrMem);
|
---|
1385 |
|
---|
1386 | return rc;
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 |
|
---|
1390 | /**
|
---|
1391 | * Sends a kill command to the daemon to kill it and its workers.
|
---|
1392 | * @returns 0.
|
---|
1393 | */
|
---|
1394 | int Kill(void)
|
---|
1395 | {
|
---|
1396 | int rc;
|
---|
1397 |
|
---|
1398 | rc = shrmemOpen();
|
---|
1399 | if (rc)
|
---|
1400 | return rc;
|
---|
1401 |
|
---|
1402 | pShrMem->enmMsgType = msgKill;
|
---|
1403 | pShrMem->u1.Kill.iNothing = 0;
|
---|
1404 | rc = shrmemSendClient(msgKillResponse);
|
---|
1405 | if (!rc)
|
---|
1406 | rc = !pShrMem->u1.KillResponse.fRc;
|
---|
1407 |
|
---|
1408 | shrmemFree();
|
---|
1409 | return rc;
|
---|
1410 | }
|
---|
1411 |
|
---|
1412 |
|
---|
1413 | /**
|
---|
1414 | * Creates the shared memory area.
|
---|
1415 | * The creator owns the memory when created.
|
---|
1416 | * @returns 0 on success. Error code on error.
|
---|
1417 | */
|
---|
1418 | int shrmemCreate(void)
|
---|
1419 | {
|
---|
1420 | int rc;
|
---|
1421 | rc = DosAllocSharedMem((PPVOID)(PVOID)&pShrMem,
|
---|
1422 | SHARED_MEM_NAME,
|
---|
1423 | SHARED_MEM_SIZE,
|
---|
1424 | PAG_COMMIT | PAG_READ | PAG_WRITE);
|
---|
1425 | if (rc)
|
---|
1426 | {
|
---|
1427 | Error("Fatal error: Failed to create shared memory object. rc=%d\n", rc);
|
---|
1428 | return rc;
|
---|
1429 | }
|
---|
1430 |
|
---|
1431 | rc = DosCreateEventSem(NULL, &pShrMem->hevDaemon, DC_SEM_SHARED, FALSE);
|
---|
1432 | if (rc)
|
---|
1433 | {
|
---|
1434 | Error("Fatal error: Failed to create daemon event semaphore. rc=%d\n", rc);
|
---|
1435 | DosFreeMem(pShrMem);
|
---|
1436 | return rc;
|
---|
1437 | }
|
---|
1438 |
|
---|
1439 | rc = DosCreateEventSem(NULL, &pShrMem->hevClient, DC_SEM_SHARED, FALSE);
|
---|
1440 | if (rc)
|
---|
1441 | {
|
---|
1442 | Error("Fatal error: Failed to create client event semaphore. rc=%d\n", rc);
|
---|
1443 | DosCloseEventSem(pShrMem->hevDaemon);
|
---|
1444 | DosFreeMem(pShrMem);
|
---|
1445 | return rc;
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | rc = DosCreateMutexSem(NULL, &pShrMem->hmtx, DC_SEM_SHARED, TRUE);
|
---|
1449 | if (rc)
|
---|
1450 | {
|
---|
1451 | Error("Fatal error: Failed to create mutex semaphore. rc=%d\n", rc);
|
---|
1452 | DosCloseEventSem(pShrMem->hevClient);
|
---|
1453 | DosCloseEventSem(pShrMem->hevDaemon);
|
---|
1454 | DosFreeMem(pShrMem);
|
---|
1455 | return rc;
|
---|
1456 | }
|
---|
1457 |
|
---|
1458 | rc = DosCreateMutexSem(NULL, &pShrMem->hmtxClient, DC_SEM_SHARED, FALSE);
|
---|
1459 | if (rc)
|
---|
1460 | {
|
---|
1461 | Error("Fatal error: Failed to create client mutex semaphore. rc=%d\n", rc);
|
---|
1462 | DosCloseEventSem(pShrMem->hevClient);
|
---|
1463 | DosCloseEventSem(pShrMem->hevClient);
|
---|
1464 | DosCloseEventSem(pShrMem->hevDaemon);
|
---|
1465 | DosFreeMem(pShrMem);
|
---|
1466 | return rc;
|
---|
1467 | }
|
---|
1468 |
|
---|
1469 |
|
---|
1470 | /*
|
---|
1471 | * Install signal handlers.
|
---|
1472 | */
|
---|
1473 | signal(SIGSEGV, signalhandlerDaemon);
|
---|
1474 | signal(SIGTERM, signalhandlerDaemon);
|
---|
1475 | signal(SIGABRT, signalhandlerDaemon);
|
---|
1476 | signal(SIGINT, signalhandlerDaemon);
|
---|
1477 | signal(SIGBREAK,signalhandlerDaemon);
|
---|
1478 |
|
---|
1479 | return rc;
|
---|
1480 | }
|
---|
1481 |
|
---|
1482 |
|
---|
1483 | /**
|
---|
1484 | * Opens the shared memory and the semaphores.
|
---|
1485 | * The caller is owner of the memory upon successful return.
|
---|
1486 | * @returns 0 on success. Error code on error.
|
---|
1487 | */
|
---|
1488 | int shrmemOpen(void)
|
---|
1489 | {
|
---|
1490 | int rc;
|
---|
1491 |
|
---|
1492 | rc = DosGetNamedSharedMem((PPVOID)(PVOID)&pShrMem,
|
---|
1493 | SHARED_MEM_NAME,
|
---|
1494 | PAG_READ | PAG_WRITE);
|
---|
1495 | if (rc)
|
---|
1496 | {
|
---|
1497 | Error("Fatal error: Failed to open shared memory. rc=%d\n");
|
---|
1498 | return rc;
|
---|
1499 | }
|
---|
1500 |
|
---|
1501 | rc = DosOpenEventSem(NULL, &pShrMem->hevClient);
|
---|
1502 | if (rc)
|
---|
1503 | {
|
---|
1504 | Error("Fatal error: Failed to open client event semaphore. rc=%d\n");
|
---|
1505 | DosFreeMem(pShrMem);
|
---|
1506 | return rc;
|
---|
1507 | }
|
---|
1508 |
|
---|
1509 | rc = DosOpenEventSem(NULL, &pShrMem->hevDaemon);
|
---|
1510 | if (rc)
|
---|
1511 | {
|
---|
1512 | Error("Fatal error: Failed to open daemon event semaphore. rc=%d\n");
|
---|
1513 | DosCloseEventSem(pShrMem->hevClient);
|
---|
1514 | DosFreeMem(pShrMem);
|
---|
1515 | return rc;
|
---|
1516 | }
|
---|
1517 |
|
---|
1518 | rc = DosOpenMutexSem(NULL, &pShrMem->hmtx);
|
---|
1519 | if (rc)
|
---|
1520 | {
|
---|
1521 | Error("Fatal error: Failed to open mutex semaphore. rc=%d\n");
|
---|
1522 | DosCloseEventSem(pShrMem->hevClient);
|
---|
1523 | DosCloseEventSem(pShrMem->hevDaemon);
|
---|
1524 | DosFreeMem(pShrMem);
|
---|
1525 | return rc;
|
---|
1526 | }
|
---|
1527 |
|
---|
1528 | rc = DosOpenMutexSem(NULL, &pShrMem->hmtxClient);
|
---|
1529 | if (rc)
|
---|
1530 | {
|
---|
1531 | Error("Fatal error: Failed to open client mutex semaphore. rc=%d\n");
|
---|
1532 | DosCloseEventSem(pShrMem->hevClient);
|
---|
1533 | DosCloseEventSem(pShrMem->hevDaemon);
|
---|
1534 | DosCloseMutexSem(pShrMem->hmtx);
|
---|
1535 | DosFreeMem(pShrMem);
|
---|
1536 | return rc;
|
---|
1537 | }
|
---|
1538 |
|
---|
1539 | rc = DosRequestMutexSem(pShrMem->hmtxClient, SEM_INDEFINITE_WAIT);
|
---|
1540 | if (rc)
|
---|
1541 | {
|
---|
1542 | Error("Fatal error: Failed to open take ownership of client mutex semaphore. rc=%d\n");
|
---|
1543 | shrmemFree();
|
---|
1544 | return rc;
|
---|
1545 | }
|
---|
1546 |
|
---|
1547 | rc = DosRequestMutexSem(pShrMem->hmtx, SEM_INDEFINITE_WAIT);
|
---|
1548 | if (rc)
|
---|
1549 | {
|
---|
1550 | Error("Fatal error: Failed to open take ownership of mutex semaphore. rc=%d\n");
|
---|
1551 | shrmemFree();
|
---|
1552 | return rc;
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 |
|
---|
1556 | /*
|
---|
1557 | * Install signal handlers.
|
---|
1558 | */
|
---|
1559 | signal(SIGSEGV, signalhandlerClient);
|
---|
1560 | signal(SIGTERM, signalhandlerClient);
|
---|
1561 | signal(SIGABRT, signalhandlerClient);
|
---|
1562 | signal(SIGINT, signalhandlerClient);
|
---|
1563 | signal(SIGBREAK,signalhandlerClient);
|
---|
1564 |
|
---|
1565 | return rc;
|
---|
1566 | }
|
---|
1567 |
|
---|
1568 |
|
---|
1569 | /**
|
---|
1570 | * Frees the shared memory and the associated semaphores.
|
---|
1571 | */
|
---|
1572 | void shrmemFree(void)
|
---|
1573 | {
|
---|
1574 | if (!pShrMem)
|
---|
1575 | return;
|
---|
1576 | /* wakeup any clients */
|
---|
1577 | DosPostEventSem(pShrMem->hevClient);
|
---|
1578 | /* free stuff */
|
---|
1579 | DosReleaseMutexSem(pShrMem->hmtxClient);
|
---|
1580 | DosReleaseMutexSem(pShrMem->hmtx);
|
---|
1581 | DosCloseMutexSem(pShrMem->hmtxClient);
|
---|
1582 | DosCloseMutexSem(pShrMem->hmtx);
|
---|
1583 | DosCloseEventSem(pShrMem->hevClient);
|
---|
1584 | DosCloseEventSem(pShrMem->hevDaemon);
|
---|
1585 | DosFreeMem(pShrMem);
|
---|
1586 | pShrMem = NULL;
|
---|
1587 | }
|
---|
1588 |
|
---|
1589 |
|
---|
1590 | /**
|
---|
1591 | * Daemon sends a message.
|
---|
1592 | * Upon we don't own the shared memory any longer.
|
---|
1593 | * @returns 0 on success. Error code on error.
|
---|
1594 | * -1 on timeout.
|
---|
1595 | * @param fWait Wait for new message.
|
---|
1596 | */
|
---|
1597 | int shrmemSendDaemon(BOOL fWait)
|
---|
1598 | {
|
---|
1599 | ULONG ulDummy;
|
---|
1600 | int rc;
|
---|
1601 |
|
---|
1602 | /* send message */
|
---|
1603 | DosResetEventSem(pShrMem->hevDaemon, &ulDummy);
|
---|
1604 | rc = DosReleaseMutexSem(pShrMem->hmtx);
|
---|
1605 | if (!rc)
|
---|
1606 | rc = DosPostEventSem(pShrMem->hevClient);
|
---|
1607 |
|
---|
1608 | /* wait for next message */
|
---|
1609 | if (!rc && fWait)
|
---|
1610 | {
|
---|
1611 | do
|
---|
1612 | {
|
---|
1613 | rc = DosWaitEventSem(pShrMem->hevDaemon, IDLE_TIMEOUT_MS);
|
---|
1614 | } while (rc == ERROR_TIMEOUT && pJobQueue);
|
---|
1615 |
|
---|
1616 | if (rc == ERROR_TIMEOUT)
|
---|
1617 | {
|
---|
1618 | DosRequestMutexSem(pShrMem->hmtx, SEM_INDEFINITE_WAIT);
|
---|
1619 | shrmemFree();
|
---|
1620 | return -1;
|
---|
1621 | }
|
---|
1622 |
|
---|
1623 | if (!rc)
|
---|
1624 | {
|
---|
1625 | rc = DosRequestMutexSem(pShrMem->hmtx, SEM_INDEFINITE_WAIT);
|
---|
1626 | if (rc == ERROR_SEM_OWNER_DIED)
|
---|
1627 | {
|
---|
1628 | DosCloseMutexSem(pShrMem->hmtx);
|
---|
1629 | pShrMem->hmtx = NULLHANDLE;
|
---|
1630 | rc = DosCreateMutexSem(NULL, &pShrMem->hmtx, DC_SEM_SHARED, TRUE);
|
---|
1631 | }
|
---|
1632 | }
|
---|
1633 |
|
---|
1634 | if (rc && rc != ERROR_INTERRUPT)
|
---|
1635 | Error("Internal error: failed to get next message from daemon, rc=%d\n", rc);
|
---|
1636 | }
|
---|
1637 | else
|
---|
1638 | Error("Internal error: failed to send message from daemon, rc=%d\n", rc);
|
---|
1639 | return rc;
|
---|
1640 | }
|
---|
1641 |
|
---|
1642 |
|
---|
1643 | /**
|
---|
1644 | * Client sends a message.
|
---|
1645 | * Upon we don't own the shared memory any longer.
|
---|
1646 | * @returns 0 on success. Error code on error.
|
---|
1647 | * @param enmMsgTypeResponse The expected response on this message.
|
---|
1648 | */
|
---|
1649 | int shrmemSendClient(int enmMsgTypeResponse)
|
---|
1650 | {
|
---|
1651 | ULONG ulDummy;
|
---|
1652 | int rc;
|
---|
1653 |
|
---|
1654 | /* send message */
|
---|
1655 | DosResetEventSem(pShrMem->hevClient, &ulDummy);
|
---|
1656 | rc = DosReleaseMutexSem(pShrMem->hmtx);
|
---|
1657 | if (!rc)
|
---|
1658 | rc = DosPostEventSem(pShrMem->hevDaemon);
|
---|
1659 |
|
---|
1660 | /* wait for response */
|
---|
1661 | if (!rc)
|
---|
1662 | {
|
---|
1663 | rc = DosWaitEventSem(pShrMem->hevClient, SEM_INDEFINITE_WAIT);
|
---|
1664 | if (!rc)
|
---|
1665 | {
|
---|
1666 | rc = DosRequestMutexSem(pShrMem->hmtx, SEM_INDEFINITE_WAIT);
|
---|
1667 | if (rc == ERROR_SEM_OWNER_DIED)
|
---|
1668 | {
|
---|
1669 | Error("Internal error: shared mem mutex owner died.\n");
|
---|
1670 | return -1;
|
---|
1671 | }
|
---|
1672 |
|
---|
1673 | if (!rc && pShrMem->enmMsgType != enmMsgTypeResponse)
|
---|
1674 | {
|
---|
1675 | if (pShrMem->enmMsgType != msgDying)
|
---|
1676 | Error("Internal error: Invalid response message. response=%d expected=%d\n",
|
---|
1677 | pShrMem->enmMsgType, enmMsgTypeResponse);
|
---|
1678 | else
|
---|
1679 | Error("Fatal error: daemon just died!\n");
|
---|
1680 | return -1;
|
---|
1681 | }
|
---|
1682 | }
|
---|
1683 | if (rc && rc != ERROR_INTERRUPT)
|
---|
1684 | Error("Internal error: failed to get response message from daemon, rc=%d\n", rc);
|
---|
1685 | }
|
---|
1686 | else
|
---|
1687 | Error("Internal error: failed to send message to daemon, rc=%d\n", rc);
|
---|
1688 |
|
---|
1689 | return rc;
|
---|
1690 | }
|
---|
1691 |
|
---|
1692 |
|
---|
1693 | /**
|
---|
1694 | * printf lookalike used to print all run-tim errors.
|
---|
1695 | * @param pszFormat Format string.
|
---|
1696 | * @param ... Arguments (optional).
|
---|
1697 | */
|
---|
1698 | void Error(const char *pszFormat, ...)
|
---|
1699 | {
|
---|
1700 | va_list arg;
|
---|
1701 |
|
---|
1702 | va_start(arg, pszFormat);
|
---|
1703 | vfprintf(stdout, pszFormat, arg);
|
---|
1704 | va_end(arg);
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 |
|
---|
1708 | #ifdef DEBUGMEMORY
|
---|
1709 | void my_free(void *pv)
|
---|
1710 | {
|
---|
1711 | DosFreeMem((PVOID)((unsigned)pv & 0xffff0000));
|
---|
1712 | }
|
---|
1713 |
|
---|
1714 | void *my_malloc(size_t cb)
|
---|
1715 | {
|
---|
1716 | APIRET rc;
|
---|
1717 | PVOID pv;
|
---|
1718 | ULONG cbAlloc;
|
---|
1719 | char szMsg[200];
|
---|
1720 |
|
---|
1721 | cbAlloc = (cb + 0x1fff) & (~0x0fff);
|
---|
1722 |
|
---|
1723 | rc = DosAllocMem(&pv, cbAlloc, PAG_READ | PAG_WRITE);
|
---|
1724 | if (!rc)
|
---|
1725 | {
|
---|
1726 | rc = DosSetMem(pv, cbAlloc - 0x1000, PAG_READ | PAG_WRITE | PAG_COMMIT);
|
---|
1727 | if (rc)
|
---|
1728 | __interrupt(3);
|
---|
1729 | if (cb & 0xfff)
|
---|
1730 | pv = (PVOID)((unsigned)pv + 0x1000 - (cb & 0x0fff));
|
---|
1731 | }
|
---|
1732 |
|
---|
1733 | strcpy(szMsg, "malloc(");
|
---|
1734 | _itoa(cb, szMsg + strlen(szMsg), 16);
|
---|
1735 | strcat(szMsg, ") -> ");
|
---|
1736 | _itoa(pv, szMsg + strlen(szMsg), 16);
|
---|
1737 | strcat(szMsg, "\r\n");
|
---|
1738 |
|
---|
1739 | DosPutMessage(1, strlen(szMsg), szMsg);
|
---|
1740 |
|
---|
1741 | return rc ? NULL : pv;
|
---|
1742 | }
|
---|
1743 | #endif
|
---|