Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add --setenv command line options to tesh
[simgrid.git] / tools / tesh / run_context.c
1 /* run_context -- stuff in which TESH runs a command                        */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "tesh.h"
10
11 #include <signal.h>
12 #include <sys/types.h>
13 #include <sys/wait.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
18
19 int fg_job = 0;
20 xbt_dynar_t bg_jobs = NULL;
21 rctx_t armageddon_initiator = NULL;
22 xbt_os_mutex_t armageddon_mutex = NULL;
23 struct {
24   int num;
25   struct sigaction act;
26 } oldact[3];                    /* SIGINT, SIGQUIT, SIGTERM */
27
28 xbt_os_thread_t sigwaiter_thread;
29 xbt_os_mutex_t sigwaiter_mutex;
30 xbt_os_cond_t sigwaiter_cond;
31 int armageddon_requested = 0;
32 int caught_signum = 0;
33
34 /*
35  * Module management
36  */
37
38 static void armageddon_sighandler(int signum)
39 {
40   xbt_os_mutex_acquire(sigwaiter_mutex);
41   caught_signum = signum;
42   armageddon_requested = 1;
43   xbt_os_cond_signal(sigwaiter_cond);
44   xbt_os_mutex_release(sigwaiter_mutex);
45 }
46
47 static void *armageddon_sigwaiter(_XBT_GNUC_UNUSED void *arg)
48 {
49   xbt_os_mutex_acquire(sigwaiter_mutex);
50   /* Inform main thread that it started. */
51   xbt_os_cond_signal(sigwaiter_cond);
52   /* Wait for ending signal... */
53   xbt_os_cond_wait(sigwaiter_cond, sigwaiter_mutex);
54   if (armageddon_requested) {
55     ERROR2("Test suite `%s': caught signal %d", testsuite_name, caught_signum);
56     rctx_armageddon(rctx, 3);
57   }
58   xbt_os_mutex_release(sigwaiter_mutex);
59   return NULL;
60 }
61
62 static void wait_it(rctx_t rctx)
63 {
64   VERB2("Join thread %p which were running background cmd <%s>",
65         rctx->runner, rctx->filepos);
66   xbt_os_thread_join(rctx->runner, NULL);
67 }
68
69 static void kill_it(void *r)
70 {
71   rctx_t rctx = *(rctx_t *) r;
72   wait_it(rctx);
73   rctx_free(rctx);
74 }
75
76 void rctx_init(void)
77 {
78   struct sigaction newact;
79   int i;
80   fg_job = 0;
81   bg_jobs = xbt_dynar_new_sync(sizeof(rctx_t), kill_it);
82   armageddon_mutex = xbt_os_mutex_init();
83   armageddon_initiator = NULL;
84   sigwaiter_mutex = xbt_os_mutex_init();
85   sigwaiter_cond = xbt_os_cond_init();
86   xbt_os_mutex_acquire(sigwaiter_mutex);
87   sigwaiter_thread = xbt_os_thread_create("Armaggedon request waiter",
88                                           armageddon_sigwaiter, NULL);
89   /* Wait for thread to start... */
90   xbt_os_cond_wait(sigwaiter_cond, sigwaiter_mutex);
91   xbt_os_mutex_release(sigwaiter_mutex);
92   memset(&newact, 0, sizeof(newact));
93   newact.sa_handler = armageddon_sighandler;
94   oldact[0].num = SIGINT;
95   oldact[1].num = SIGQUIT;
96   oldact[2].num = SIGTERM;
97   for (i = 0; i < 3; i++)
98     sigaction(oldact[i].num, &newact, &oldact[i].act);
99 }
100
101 void rctx_exit(void)
102 {
103   int i;
104   if (bg_jobs) {
105     /* Do not use xbt_dynar_free or it will lock the dynar, preventing armageddon from working */
106     while (xbt_dynar_length(bg_jobs)) {
107       rctx_t rctx = xbt_dynar_getlast_as(bg_jobs, rctx_t);
108       wait_it(rctx);
109       xbt_dynar_pop(bg_jobs, &rctx);
110       rctx_free(rctx);
111     }
112   }
113   for (i = 0; i < 3; i++)
114     sigaction(oldact[i].num, &oldact[i].act, NULL);
115   xbt_os_cond_signal(sigwaiter_cond);
116   xbt_os_thread_join(sigwaiter_thread, NULL);
117   if (bg_jobs)
118     xbt_dynar_free(&bg_jobs);
119   xbt_os_cond_destroy(sigwaiter_cond);
120   xbt_os_mutex_destroy(sigwaiter_mutex);
121   xbt_os_mutex_destroy(armageddon_mutex);
122 }
123
124 void rctx_wait_bg(void)
125 {
126   if (bg_jobs) {
127     /* Do not use xbt_dynar_free or it will lock the dynar, preventing armageddon from working */
128     while (xbt_dynar_length(bg_jobs)) {
129       rctx_t rctx = xbt_dynar_getlast_as(bg_jobs, rctx_t);
130       wait_it(rctx);
131       xbt_dynar_pop(bg_jobs, &rctx);
132       rctx_free(rctx);
133     }
134     xbt_dynar_free(&bg_jobs);
135   }
136   bg_jobs = xbt_dynar_new_sync(sizeof(rctx_t), kill_it);
137 }
138
139 static void rctx_armageddon_kill_one(rctx_t initiator, const char *filepos,
140                                      rctx_t rctx)
141 {
142   if (rctx != initiator) {
143     INFO2("Kill <%s> because <%s> failed", rctx->filepos, filepos);
144     xbt_os_mutex_acquire(rctx->interruption);
145     if (!rctx->reader_done) {
146       rctx->interrupted = 1;
147       kill(rctx->pid, SIGTERM);
148       usleep(100);
149       kill(rctx->pid, SIGKILL);
150     }
151     xbt_os_mutex_release(rctx->interruption);
152   }
153 }
154
155 void rctx_armageddon(rctx_t initiator, int exitcode)
156 {
157   unsigned int cursor;
158   rctx_t job;
159   const char *filepos = initiator && initiator->filepos ?
160       initiator->filepos : "(master)";
161
162   DEBUG2("Armageddon request by <%s> (exit=%d)", filepos, exitcode);
163   xbt_os_mutex_acquire(armageddon_mutex);
164   if (armageddon_initiator != NULL) {
165     VERB0("Armageddon already started. Let it go");
166     xbt_os_mutex_release(armageddon_mutex);
167     return;
168   }
169   DEBUG1("Armageddon request by <%s> got the lock. Let's go amok",
170          filepos);
171   armageddon_initiator = initiator;
172   xbt_os_mutex_release(armageddon_mutex);
173
174   /* Kill foreground command */
175   if (fg_job)
176     rctx_armageddon_kill_one(initiator, filepos, rctx);
177
178   /* Kill any background commands */
179   xbt_dynar_foreach(bg_jobs, cursor, job) {
180     rctx_armageddon_kill_one(initiator, filepos, job);
181   }
182
183   /* Give runner threads a chance to acknowledge the processes deaths */
184   usleep(10000);
185   /* Ensure that nobody is running rctx_wait on exit */
186   if (fg_job)
187     xbt_os_mutex_acquire(rctx->interruption);
188   xbt_dynar_foreach(bg_jobs, cursor, job)
189     xbt_os_mutex_acquire(job->interruption);
190   VERB0("Shut everything down!");
191   exit(exitcode);
192 }
193
194 /*
195  * Memory management
196  */
197
198 void rctx_empty(rctx_t rc)
199 {
200   int i;
201   char **env_it;
202   void *filepos;
203
204   if (rc->cmd)
205     free(rc->cmd);
206   rc->cmd = NULL;
207   /* avoid race with rctx_armageddon log messages */
208   filepos = rc->filepos;
209   rc->filepos = NULL;
210   if (filepos)
211     free(filepos);
212   for (i = 0, env_it = environ; *env_it; i++, env_it++);
213   if (rc->env) {
214     for (env_it = rctx->env + i; *env_it; env_it++)
215       free(*env_it);
216     free(rc->env);
217   }
218   rc->env_size = i + 1;
219   rc->env = malloc(rc->env_size * sizeof(char *));
220   memcpy(rc->env, environ, rc->env_size * sizeof(char *));
221
222   rc->is_empty = 1;
223   rc->is_background = 0;
224   rc->is_stoppable = 0;
225   rc->output = e_output_check;
226   rc->brokenpipe = 0;
227   rc->timeout = 0;
228   rc->interrupted = 0;
229   xbt_strbuff_empty(rc->input);
230   xbt_strbuff_empty(rc->output_wanted);
231   xbt_strbuff_empty(rc->output_got);
232 }
233
234
235 rctx_t rctx_new()
236 {
237   rctx_t res = xbt_new0(s_rctx_t, 1);
238
239   res->input = xbt_strbuff_new();
240   res->output_wanted = xbt_strbuff_new();
241   res->output_got = xbt_strbuff_new();
242   res->interruption = xbt_os_mutex_init();
243   rctx_empty(res);
244   return res;
245 }
246
247 void rctx_free(rctx_t rctx)
248 {
249   DEBUG1("RCTX: Free %p", rctx);
250   rctx_dump(rctx, "free");
251   if (!rctx)
252     return;
253
254   if (rctx->cmd)
255     free(rctx->cmd);
256   if (rctx->filepos)
257     free(rctx->filepos);
258   if (rctx->env) {
259     int i;
260     char **env_it;
261     for (i = 0, env_it = environ; *env_it; i++, env_it++);
262     for (env_it = rctx->env + i; *env_it; env_it++)
263       free(*env_it);
264     free(rctx->env);
265   }
266   xbt_os_mutex_destroy(rctx->interruption);
267   xbt_strbuff_free(rctx->input);
268   xbt_strbuff_free(rctx->output_got);
269   xbt_strbuff_free(rctx->output_wanted);
270   free(rctx);
271 }
272
273 void rctx_dump(rctx_t rctx, const char *str)
274 {
275   DEBUG9("%s RCTX %p={in%p={%d,%10s}, want={%d,%10s}, out={%d,%10s}}",
276          str, rctx,
277          rctx->input, rctx->input->used, rctx->input->data,
278          rctx->output_wanted->used, rctx->output_wanted->data,
279          rctx->output_got->used, rctx->output_got->data);
280   DEBUG5("%s RCTX %p=[cmd%p=%10s, pid=%d]",
281          str, rctx, rctx->cmd, rctx->cmd, rctx->pid);
282
283 }
284
285 /*
286  * Getting instructions from the file
287  */
288
289 void rctx_pushline(const char *filepos, char kind, char *line)
290 {
291
292   switch (kind) {
293   case '$':
294   case '&':
295     if (rctx->cmd) {
296       if (!rctx->is_empty) {
297         ERROR2
298             ("[%s] More than one command in this chunk of lines (previous: %s).\n"
299              " Cannot guess which input/output belongs to which command.",
300              filepos, rctx->cmd);
301         ERROR1("Test suite `%s': NOK (syntax error)", testsuite_name);
302         rctx_armageddon(rctx, 1);
303         return;
304       }
305       rctx_start();
306       VERB1("[%s] More than one command in this chunk of lines", filepos);
307     }
308     if (kind == '&')
309       rctx->is_background = 1;
310     else
311       rctx->is_background = 0;
312
313     rctx->cmd = xbt_strdup(line);
314     rctx->filepos = xbt_strdup(filepos);
315     INFO3("[%s] %s%s", filepos, rctx->cmd,
316           ((rctx->is_background) ? " (background command)" : ""));
317
318     break;
319
320   case '<':
321     rctx->is_empty = 0;
322     xbt_strbuff_append(rctx->input, line);
323     xbt_strbuff_append(rctx->input, "\n");
324     break;
325
326   case '>':
327     rctx->is_empty = 0;
328     xbt_strbuff_append(rctx->output_wanted, line);
329     xbt_strbuff_append(rctx->output_wanted, "\n");
330     break;
331
332   case '!':
333     if (rctx->cmd)
334       rctx_start();
335
336     if (!strncmp(line, "timeout no", strlen("timeout no"))) {
337       VERB1("[%s] (disable timeout)", filepos);
338       timeout_value = -1;
339     } else if (!strncmp(line, "timeout ", strlen("timeout "))) {
340       timeout_value = atoi(line + strlen("timeout"));
341       VERB2("[%s] (new timeout value: %d)", filepos, timeout_value);
342
343     } else if (!strncmp(line, "expect signal ", strlen("expect signal "))) {
344       rctx->expected_signal = strdup(line + strlen("expect signal "));
345       xbt_str_trim(rctx->expected_signal, " \n");
346       VERB2("[%s] (next command must raise signal %s)",
347             filepos, rctx->expected_signal);
348
349     } else if (!strncmp(line, "expect return ", strlen("expect return "))) {
350       rctx->expected_return = atoi(line + strlen("expect return "));
351       VERB2("[%s] (next command must return code %d)",
352             filepos, rctx->expected_return);
353
354     } else if (!strncmp(line, "output ignore", strlen("output ignore"))) {
355       rctx->output = e_output_ignore;
356       VERB1("[%s] (ignore output of next command)", filepos);
357
358     } else if (!strncmp(line, "output display", strlen("output display"))) {
359       rctx->output = e_output_display;
360       VERB1("[%s] (ignore output of next command)", filepos);
361
362     } else if (!strncmp(line, "setenv ", strlen("setenv "))) {
363       int len = strlen("setenv ");
364       char *eq = strchr(line + len, '=');
365       char *key = bprintf("%.*s", (int) (eq - line - len), line + len);
366       xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f);
367       free(key);
368
369       rctx->env = realloc(rctx->env, ++(rctx->env_size) * sizeof(char *));
370       rctx->env[rctx->env_size - 2] = xbt_strdup(line + len);
371       rctx->env[rctx->env_size - 1] = NULL;
372       VERB2("[%s] setenv %s", filepos, line + len);
373
374     } else {
375       ERROR2("%s: Malformed metacommand: %s", filepos, line);
376       ERROR1("Test suite `%s': NOK (syntax error)", testsuite_name);
377       rctx_armageddon(rctx, 1);
378       return;
379     }
380     break;
381   }
382 }
383
384 /*
385  * Actually doing the job
386  */
387
388 /* The IO of the childs are handled by the two following threads
389    (one pair per child) */
390
391 static void *thread_writer(void *r)
392 {
393   int posw;
394   rctx_t rctx = (rctx_t) r;
395   for (posw = 0; posw < rctx->input->used && !rctx->brokenpipe;) {
396     int got;
397     DEBUG1("Still %d chars to write", rctx->input->used - posw);
398     got =
399         write(rctx->child_to, rctx->input->data + posw,
400               rctx->input->used - posw);
401     if (got > 0)
402       posw += got;
403     if (got < 0) {
404       if (errno == EPIPE) {
405         rctx->brokenpipe = 1;
406       } else if (errno != EINTR && errno != EAGAIN && errno != EPIPE) {
407         perror("Error while writing input to child");
408         ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
409         rctx_armageddon(rctx, 4);
410         return NULL;
411       }
412     }
413     DEBUG1("written %d chars so far", posw);
414
415     if (got <= 0)
416       usleep(100);
417   }
418   rctx->input->data[0] = '\0';
419   rctx->input->used = 0;
420   close(rctx->child_to);
421
422   return NULL;
423 }
424
425 static void *thread_reader(void *r)
426 {
427   rctx_t rctx = (rctx_t) r;
428   char *buffout = malloc(4096);
429   int posr, got_pid;
430
431   do {
432     posr = read(rctx->child_from, buffout, 4095);
433     if (posr < 0 && errno != EINTR && errno != EAGAIN) {
434       perror("Error while reading output of child");
435       ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
436       rctx_armageddon(rctx, 4);
437       return NULL;
438     }
439     if (posr > 0) {
440       buffout[posr] = '\0';
441       xbt_strbuff_append(rctx->output_got, buffout);
442     } else {
443       usleep(100);
444     }
445   } while (!rctx->timeout && posr != 0);
446   free(buffout);
447
448   /* let this thread wait for the child so that the main thread can detect the timeout without blocking on the wait */
449   got_pid = waitpid(rctx->pid, &rctx->status, 0);
450   if (got_pid != rctx->pid) {
451     perror(bprintf
452            ("(%s) Cannot wait for the child %s (got pid %d where pid %d were expected;status=%d)",
453             xbt_thread_self_name(), rctx->cmd, (int) got_pid,
454             (int) rctx->pid, rctx->status));
455     ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
456     rctx_armageddon(rctx, 4);
457     return NULL;
458   }
459
460   rctx->reader_done = 1;
461   return NULL;
462 }
463
464 /* Special command: mkfile is a built-in creating a file with the input data as content */
465 static void rctx_mkfile(void)
466 {
467   char *filename = xbt_strdup(rctx->cmd + strlen("mkfile "));
468   FILE *OUT;
469   int err;
470   xbt_str_trim(filename, NULL);
471   OUT = fopen(filename, "w");
472   if (!OUT) {
473     THROW3(system_error, errno, "%s: Cannot create file %s: %s",
474            rctx->filepos, filename, strerror(errno));
475   }
476   err = (fprintf(OUT, "%s", rctx->input->data) < 0);
477   err = (fclose(OUT) == -1) || err;
478   if (err) {
479     THROW3(system_error, errno, "%s: Cannot write file %s: %s",
480            rctx->filepos, filename, strerror(errno));
481   }
482   free(filename);
483 }
484
485 /* function to be called from the child to start the actual process */
486 static void start_command(rctx_t rctx)
487 {
488   xbt_dynar_t cmd;
489   char *binary_name = NULL;
490   unsigned int it;
491   char *str;
492   char **args;
493   int errcode;
494
495   if (!strncmp(rctx->cmd, "mkfile ", strlen("mkfile "))) {
496     rctx_mkfile();
497     /* Valgrind detects memory leaks here.
498      * To correct those leaks, we must free objects allocated in main() or in
499      * handle_suite(), but we have no more reference to them at this point.
500      * A quick and dirty hack to make valgrind happy it to uncomment the
501      * following line.
502      */
503     /* execlp("true", "true", (const char *)0); */
504     exit(0);                    /* end the working child */
505   }
506
507   cmd = xbt_str_split_quoted(rctx->cmd);
508   xbt_dynar_get_cpy(cmd, 0, &binary_name);
509   args = xbt_new(char *, xbt_dynar_length(cmd) + 1);
510   xbt_dynar_foreach(cmd, it, str) {
511     args[it] = xbt_strdup(str);
512   }
513   args[it] = NULL;
514   xbt_dynar_free_container(&cmd);
515
516   /* To search for the right executable path when not trivial */
517   struct stat stat_buf;
518
519   /* build the command line */
520   if (stat(binary_name, &stat_buf)) {
521     /* Damn. binary not in current dir. We'll have to dig the PATH to find it */
522     int i;
523
524     for (i = 0; environ[i]; i++) {
525       if (!strncmp("PATH=", environ[i], 5)) {
526         xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
527
528         xbt_dynar_foreach(path, it, str) {
529           if (binary_name)
530             free(binary_name);
531           binary_name = bprintf("%s/%s", str, args[0]);
532           if (!stat(binary_name, &stat_buf)) {
533             /* Found. */
534             DEBUG1("Looked in the PATH for the binary. Found %s",
535                    binary_name);
536             xbt_dynar_free(&path);
537             break;
538           }
539         }
540         xbt_dynar_free(&path);
541         if (stat(binary_name, &stat_buf)) {
542           /* not found */
543           printf("TESH_ERROR Command %s not found\n", args[0]);
544           exit(127);
545         }
546         break;
547       }
548     }
549   } else {
550     binary_name = xbt_strdup(args[0]);
551   }
552
553   errcode = execve(binary_name, args, rctx->env);
554   printf("TESH_ERROR %s: Cannot start %s: %s\n", rctx->filepos, rctx->cmd,
555          strerror(errcode));
556   exit(127);
557 }
558
559 /* Start a new child, plug the pipes as expected and fire up the
560    helping threads. Is also waits for the child to end if this is a
561    foreground job, or fire up a thread to wait otherwise. */
562 void rctx_start(void)
563 {
564   int child_in[2];
565   int child_out[2];
566
567   DEBUG1("Cmd before rewriting %s", rctx->cmd);
568   rctx->cmd = xbt_str_varsubst(rctx->cmd, env);
569   VERB2("Start %s %s", rctx->cmd,
570         (rctx->is_background ? "(background job)" : ""));
571   xbt_os_mutex_acquire(armageddon_mutex);
572   if (armageddon_initiator) {
573     VERB0("Armageddon in progress. Do not start job.");
574     xbt_os_mutex_release(armageddon_mutex);
575     return;
576   }
577   if (pipe(child_in) || pipe(child_out)) {
578     perror("Cannot open the pipes");
579     ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
580     xbt_os_mutex_release(armageddon_mutex);
581     rctx_armageddon(rctx, 4);
582   }
583
584   rctx->pid = fork();
585   if (rctx->pid < 0) {
586     perror("Cannot fork the command");
587     ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
588     xbt_os_mutex_release(armageddon_mutex);
589     rctx_armageddon(rctx, 4);
590     return;
591   }
592
593   if (rctx->pid) {              /* father */
594     close(child_in[0]);
595     rctx->child_to = child_in[1];
596
597     close(child_out[1]);
598     rctx->child_from = child_out[0];
599
600     if (timeout_value > 0)
601       rctx->end_time = time(NULL) + timeout_value;
602     else
603       rctx->end_time = -1;
604
605     rctx->reader_done = 0;
606     rctx->reader =
607         xbt_os_thread_create("reader", thread_reader, (void *) rctx);
608     rctx->writer =
609         xbt_os_thread_create("writer", thread_writer, (void *) rctx);
610
611   } else {                      /* child */
612     close(child_in[1]);
613     dup2(child_in[0], 0);
614     close(child_in[0]);
615
616     close(child_out[0]);
617     dup2(child_out[1], 1);
618     dup2(child_out[1], 2);
619     close(child_out[1]);
620
621     start_command(rctx);
622   }
623
624   rctx->is_stoppable = 1;
625
626   if (!rctx->is_background) {
627     fg_job = 1;
628     xbt_os_mutex_release(armageddon_mutex);
629     rctx_wait(rctx);
630     fg_job = 0;
631   } else {
632     /* Damn. Copy the rctx and launch a thread to handle it */
633     rctx_t old = rctx;
634     xbt_os_thread_t runner;
635
636     rctx = rctx_new();
637     DEBUG2("RCTX: new bg=%p, new fg=%p", old, rctx);
638
639     DEBUG2("Launch a thread to wait for %s %d", old->cmd, old->pid);
640     runner = xbt_os_thread_create(old->cmd, rctx_wait, (void *) old);
641     old->runner = runner;
642     VERB3("Launched thread %p to wait for %s %d", runner, old->cmd,
643           old->pid);
644     xbt_dynar_push(bg_jobs, &old);
645     xbt_os_mutex_release(armageddon_mutex);
646   }
647 }
648
649 /* Waits for the child to end (or to timeout), and check its
650    ending conditions. This is launched from rctx_start but either in main
651    thread (for foreground jobs) or in a separate one for background jobs.
652    That explains the prototype, forced by xbt_os_thread_create. */
653
654 void *rctx_wait(void *r)
655 {
656   rctx_t rctx = (rctx_t) r;
657   int errcode = 0;
658   int now = time(NULL);
659
660   rctx_dump(rctx, "wait");
661
662   if (!rctx->is_stoppable)
663     THROW1(unknown_error, 0, "Cmd '%s' not started yet. Cannot wait it",
664            rctx->cmd);
665
666   /* Wait for the child to die or the timeout to happen (or an armageddon to happen) */
667   while (!rctx->reader_done
668          && (rctx->end_time < 0 || rctx->end_time >= now)) {
669     usleep(100);
670     now = time(NULL);
671   }
672
673   xbt_os_mutex_acquire(rctx->interruption);
674   if (!rctx->interrupted && rctx->end_time > 0 && rctx->end_time < now) {
675     INFO1("<%s> timeouted. Kill the process.", rctx->filepos);
676     rctx->timeout = 1;
677     kill(rctx->pid, SIGTERM);
678     usleep(100);
679     kill(rctx->pid, SIGKILL);
680   }
681
682   /* Make sure helper threads die.
683      Cannot block since they wait for the child we just killed
684      if not already dead. */
685   xbt_os_thread_join(rctx->writer, NULL);
686   xbt_os_thread_join(rctx->reader, NULL);
687
688   /*  xbt_os_mutex_release(rctx->interruption);
689      if (rctx->interrupted)
690      return NULL;
691      xbt_os_mutex_acquire(rctx->interruption); */
692
693   xbt_strbuff_chomp(rctx->output_got);
694   xbt_strbuff_chomp(rctx->output_wanted);
695   xbt_strbuff_trim(rctx->output_got);
696   xbt_strbuff_trim(rctx->output_wanted);
697
698   /* Check for broken pipe */
699   if (rctx->brokenpipe)
700     VERB0
701         ("Warning: Child did not consume all its input (I got broken pipe)");
702
703   /* Check for timeouts */
704   if (rctx->timeout) {
705     if (rctx->output_got->data[0])
706       INFO2("<%s> Output on timeout:\n%s",
707             rctx->filepos, rctx->output_got->data);
708     else
709       INFO1("<%s> No output before timeout", rctx->filepos);
710     ERROR3("Test suite `%s': NOK (<%s> timeout after %d sec)",
711            testsuite_name, rctx->filepos, timeout_value);
712     DEBUG2("<%s> Interrupted = %d", rctx->filepos, rctx->interrupted);
713     if (!rctx->interrupted) {
714       xbt_os_mutex_release(rctx->interruption);
715       rctx_armageddon(rctx, 3);
716       return NULL;
717     }
718   }
719
720   DEBUG2("RCTX=%p (pid=%d)", rctx, rctx->pid);
721   DEBUG3("Status(%s|%d)=%d", rctx->cmd, rctx->pid, rctx->status);
722
723   if (!rctx->interrupted) {
724     if (WIFSIGNALED(rctx->status) && !rctx->expected_signal) {
725       ERROR3("Test suite `%s': NOK (<%s> got signal %s)",
726              testsuite_name, rctx->filepos,
727              signal_name(WTERMSIG(rctx->status), NULL));
728       errcode = WTERMSIG(rctx->status) + 4;
729     }
730
731     if (WIFSIGNALED(rctx->status) && rctx->expected_signal &&
732         strcmp(signal_name(WTERMSIG(rctx->status), rctx->expected_signal),
733                rctx->expected_signal)) {
734       ERROR4("Test suite `%s': NOK (%s got signal %s instead of %s)",
735              testsuite_name, rctx->filepos,
736              signal_name(WTERMSIG(rctx->status), rctx->expected_signal),
737              rctx->expected_signal);
738       errcode = WTERMSIG(rctx->status) + 4;
739     }
740
741     if (!WIFSIGNALED(rctx->status) && rctx->expected_signal) {
742       ERROR3("Test suite `%s': NOK (child %s expected signal %s)",
743              testsuite_name, rctx->filepos, rctx->expected_signal);
744       errcode = 5;
745     }
746
747     if (WIFEXITED(rctx->status)
748         && WEXITSTATUS(rctx->status) != rctx->expected_return) {
749       if (rctx->expected_return)
750         ERROR4
751             ("Test suite `%s': NOK (<%s> returned code %d instead of %d)",
752              testsuite_name, rctx->filepos, WEXITSTATUS(rctx->status),
753              rctx->expected_return);
754       else
755         ERROR3("Test suite `%s': NOK (<%s> returned code %d)",
756                testsuite_name, rctx->filepos, WEXITSTATUS(rctx->status));
757       errcode = 40 + WEXITSTATUS(rctx->status);
758
759     }
760     rctx->expected_return = 0;
761
762     if (rctx->expected_signal) {
763       free(rctx->expected_signal);
764       rctx->expected_signal = NULL;
765     }
766   }
767   while (rctx->output_got->used
768          && !strncmp(rctx->output_got->data, "TESH_ERROR ",
769                      strlen("TESH_ERROR "))) {
770     int marklen = strlen("TESH_ERROR ");
771     char *endline = strchr(rctx->output_got->data, '\n');
772
773     CRITICAL2("%.*s", (int) (endline - rctx->output_got->data - marklen),
774               rctx->output_got->data + marklen);
775     memmove(rctx->output_got->data, rctx->output_got->data + marklen,
776             rctx->output_got->used - marklen);
777     rctx->output_got->used -= endline - rctx->output_got->data + 1;
778     rctx->output_got->data[rctx->output_got->used] = '\0';
779     errcode = 1;
780   }
781
782   if ((errcode && errcode != 1) || rctx->interrupted) {
783     /* checking output, and matching */
784     xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
785     char *out = xbt_str_join(a, "\n||");
786     xbt_dynar_free(&a);
787     INFO2("Output of <%s> so far: \n||%s", rctx->filepos, out);
788     free(out);
789   } else if (rctx->output == e_output_check
790              && (rctx->output_got->used != rctx->output_wanted->used
791                  || strcmp(rctx->output_got->data,
792                            rctx->output_wanted->data))) {
793     if (XBT_LOG_ISENABLED(tesh, xbt_log_priority_info)) {
794       char *diff =
795           xbt_str_diff(rctx->output_wanted->data, rctx->output_got->data);
796       ERROR2("Output of <%s> mismatch:\n%s", rctx->filepos, diff);
797       free(diff);
798     }
799     ERROR2("Test suite `%s': NOK (<%s> output mismatch)",
800            testsuite_name, rctx->filepos);
801
802     errcode = 2;
803   } else if (rctx->output == e_output_ignore) {
804     INFO1("(ignoring the output of <%s> as requested)", rctx->filepos);
805   } else if (rctx->output == e_output_display) {
806     xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
807     char *out = xbt_str_join(a, "\n||");
808     xbt_dynar_free(&a);
809     INFO1("Here is the (ignored) command output: \n||%s", out);
810     free(out);
811   }
812
813   if (!rctx->is_background) {
814     xbt_os_mutex_acquire(armageddon_mutex);
815     /* Don't touch rctx if armageddon is in progress. */
816     if (!armageddon_initiator)
817       rctx_empty(rctx);
818     xbt_os_mutex_release(armageddon_mutex);
819   }
820   if (errcode) {
821     if (!rctx->interrupted) {
822       xbt_os_mutex_release(rctx->interruption);
823       rctx_armageddon(rctx, errcode);
824       return NULL;
825     }
826   }
827
828   xbt_os_mutex_release(rctx->interruption);
829   return NULL;
830 }