Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update script for pipol
[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   VERB0("Shut everything down!");
184   exit(exitcode);
185 }
186
187 /*
188  * Memory management
189  */
190
191 void rctx_empty(rctx_t rc)
192 {
193   int i;
194   char **env_it;
195   void *filepos;
196
197   if (rc->cmd)
198     free(rc->cmd);
199   rc->cmd = NULL;
200   /* avoid race with rctx_armageddon log messages */
201   filepos = rc->filepos;
202   rc->filepos = NULL;
203   if (filepos)
204     free(filepos);
205   for (i = 0, env_it = environ; *env_it; i++, env_it++);
206   if (rc->env) {
207     for (env_it = rctx->env + i; *env_it; env_it++)
208       free(*env_it);
209     free(rc->env);
210   }
211   rc->env_size = i + 1;
212   rc->env = malloc(rc->env_size * sizeof(char *));
213   memcpy(rc->env, environ, rc->env_size * sizeof(char *));
214
215   rc->is_empty = 1;
216   rc->is_background = 0;
217   rc->is_stoppable = 0;
218   rc->output = e_output_check;
219   rc->brokenpipe = 0;
220   rc->timeout = 0;
221   rc->interrupted = 0;
222   xbt_strbuff_empty(rc->input);
223   xbt_strbuff_empty(rc->output_wanted);
224   xbt_strbuff_empty(rc->output_got);
225 }
226
227
228 rctx_t rctx_new()
229 {
230   rctx_t res = xbt_new0(s_rctx_t, 1);
231
232   res->input = xbt_strbuff_new();
233   res->output_wanted = xbt_strbuff_new();
234   res->output_got = xbt_strbuff_new();
235   res->interruption = xbt_os_mutex_init();
236   rctx_empty(res);
237   return res;
238 }
239
240 void rctx_free(rctx_t rctx)
241 {
242   DEBUG1("RCTX: Free %p", rctx);
243   rctx_dump(rctx, "free");
244   if (!rctx)
245     return;
246
247   if (rctx->cmd)
248     free(rctx->cmd);
249   if (rctx->filepos)
250     free(rctx->filepos);
251   if (rctx->env) {
252     int i;
253     char **env_it;
254     for (i = 0, env_it = environ; *env_it; i++, env_it++);
255     for (env_it = rctx->env + i; *env_it; env_it++)
256       free(*env_it);
257     free(rctx->env);
258   }
259   xbt_os_mutex_destroy(rctx->interruption);
260   xbt_strbuff_free(rctx->input);
261   xbt_strbuff_free(rctx->output_got);
262   xbt_strbuff_free(rctx->output_wanted);
263   free(rctx);
264 }
265
266 void rctx_dump(rctx_t rctx, const char *str)
267 {
268   DEBUG9("%s RCTX %p={in%p={%d,%10s}, want={%d,%10s}, out={%d,%10s}}",
269          str, rctx,
270          rctx->input, rctx->input->used, rctx->input->data,
271          rctx->output_wanted->used, rctx->output_wanted->data,
272          rctx->output_got->used, rctx->output_got->data);
273   DEBUG5("%s RCTX %p=[cmd%p=%10s, pid=%d]",
274          str, rctx, rctx->cmd, rctx->cmd, rctx->pid);
275
276 }
277
278 /*
279  * Getting instructions from the file
280  */
281
282 void rctx_pushline(const char *filepos, char kind, char *line)
283 {
284
285   switch (kind) {
286   case '$':
287   case '&':
288     if (rctx->cmd) {
289       if (!rctx->is_empty) {
290         ERROR2
291             ("[%s] More than one command in this chunk of lines (previous: %s).\n"
292              " Cannot guess which input/output belongs to which command.",
293              filepos, rctx->cmd);
294         ERROR1("Test suite `%s': NOK (syntax error)", testsuite_name);
295         rctx_armageddon(rctx, 1);
296         return;
297       }
298       rctx_start();
299       VERB1("[%s] More than one command in this chunk of lines", filepos);
300     }
301     if (kind == '&')
302       rctx->is_background = 1;
303     else
304       rctx->is_background = 0;
305
306     rctx->cmd = xbt_strdup(line);
307     rctx->filepos = xbt_strdup(filepos);
308     INFO3("[%s] %s%s", filepos, rctx->cmd,
309           ((rctx->is_background) ? " (background command)" : ""));
310
311     break;
312
313   case '<':
314     rctx->is_empty = 0;
315     xbt_strbuff_append(rctx->input, line);
316     xbt_strbuff_append(rctx->input, "\n");
317     break;
318
319   case '>':
320     rctx->is_empty = 0;
321     xbt_strbuff_append(rctx->output_wanted, line);
322     xbt_strbuff_append(rctx->output_wanted, "\n");
323     break;
324
325   case '!':
326     if (rctx->cmd)
327       rctx_start();
328
329     if (!strncmp(line, "timeout no", strlen("timeout no"))) {
330       VERB1("[%s] (disable timeout)", filepos);
331       timeout_value = -1;
332     } else if (!strncmp(line, "timeout ", strlen("timeout "))) {
333       timeout_value = atoi(line + strlen("timeout"));
334       VERB2("[%s] (new timeout value: %d)", filepos, timeout_value);
335
336     } else if (!strncmp(line, "expect signal ", strlen("expect signal "))) {
337       rctx->expected_signal = strdup(line + strlen("expect signal "));
338       xbt_str_trim(rctx->expected_signal, " \n");
339       VERB2("[%s] (next command must raise signal %s)",
340             filepos, rctx->expected_signal);
341
342     } else if (!strncmp(line, "expect return ", strlen("expect return "))) {
343       rctx->expected_return = atoi(line + strlen("expect return "));
344       VERB2("[%s] (next command must return code %d)",
345             filepos, rctx->expected_return);
346
347     } else if (!strncmp(line, "output ignore", strlen("output ignore"))) {
348       rctx->output = e_output_ignore;
349       VERB1("[%s] (ignore output of next command)", filepos);
350
351     } else if (!strncmp(line, "output display", strlen("output display"))) {
352       rctx->output = e_output_display;
353       VERB1("[%s] (ignore output of next command)", filepos);
354
355     } else if (!strncmp(line, "setenv ", strlen("setenv "))) {
356       int len = strlen("setenv ");
357       char *eq = strchr(line + len, '=');
358       char *key = bprintf("%.*s", (int) (eq - line - len), line + len);
359       xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f);
360       free(key);
361
362       rctx->env = realloc(rctx->env, ++(rctx->env_size) * sizeof(char *));
363       rctx->env[rctx->env_size - 2] = xbt_strdup(line + len);
364       rctx->env[rctx->env_size - 1] = NULL;
365       VERB2("[%s] setenv %s", filepos, line + len);
366
367     } else {
368       ERROR2("%s: Malformed metacommand: %s", filepos, line);
369       ERROR1("Test suite `%s': NOK (syntax error)", testsuite_name);
370       rctx_armageddon(rctx, 1);
371       return;
372     }
373     break;
374   }
375 }
376
377 /*
378  * Actually doing the job
379  */
380
381 /* The IO of the childs are handled by the two following threads
382    (one pair per child) */
383
384 static void *thread_writer(void *r)
385 {
386   int posw;
387   rctx_t rctx = (rctx_t) r;
388   for (posw = 0; posw < rctx->input->used && !rctx->brokenpipe;) {
389     int got;
390     DEBUG1("Still %d chars to write", rctx->input->used - posw);
391     got =
392         write(rctx->child_to, rctx->input->data + posw,
393               rctx->input->used - posw);
394     if (got > 0)
395       posw += got;
396     if (got < 0) {
397       if (errno == EPIPE) {
398         rctx->brokenpipe = 1;
399       } else if (errno != EINTR && errno != EAGAIN && errno != EPIPE) {
400         perror("Error while writing input to child");
401         ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
402         rctx_armageddon(rctx, 4);
403         return NULL;
404       }
405     }
406     DEBUG1("written %d chars so far", posw);
407
408     if (got <= 0)
409       usleep(100);
410   }
411   rctx->input->data[0] = '\0';
412   rctx->input->used = 0;
413   close(rctx->child_to);
414
415   return NULL;
416 }
417
418 static void *thread_reader(void *r)
419 {
420   rctx_t rctx = (rctx_t) r;
421   char *buffout = malloc(4096);
422   int posr, got_pid;
423
424   do {
425     posr = read(rctx->child_from, buffout, 4095);
426     if (posr < 0 && errno != EINTR && errno != EAGAIN) {
427       perror("Error while reading output of child");
428       ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
429       rctx_armageddon(rctx, 4);
430       return NULL;
431     }
432     if (posr > 0) {
433       buffout[posr] = '\0';
434       xbt_strbuff_append(rctx->output_got, buffout);
435     } else {
436       usleep(100);
437     }
438   } while (!rctx->timeout && posr != 0);
439   free(buffout);
440
441   /* let this thread wait for the child so that the main thread can detect the timeout without blocking on the wait */
442   got_pid = waitpid(rctx->pid, &rctx->status, 0);
443   if (got_pid != rctx->pid) {
444     perror(bprintf
445            ("(%s) Cannot wait for the child %s (got pid %d where pid %d were expected;status=%d)",
446             xbt_thread_self_name(), rctx->cmd, (int) got_pid,
447             (int) rctx->pid, rctx->status));
448     ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
449     rctx_armageddon(rctx, 4);
450     return NULL;
451   }
452
453   rctx->reader_done = 1;
454   return NULL;
455 }
456
457 /* Special command: mkfile is a built-in creating a file with the input data as content */
458 static void rctx_mkfile(void)
459 {
460   char *filename = xbt_strdup(rctx->cmd + strlen("mkfile "));
461   FILE *OUT;
462   int err;
463   xbt_str_trim(filename, NULL);
464   OUT = fopen(filename, "w");
465   if (!OUT) {
466     THROW3(system_error, errno, "%s: Cannot create file %s: %s",
467            rctx->filepos, filename, strerror(errno));
468   }
469   err = (fprintf(OUT, "%s", rctx->input->data) < 0);
470   err = (fclose(OUT) == -1) || err;
471   if (err) {
472     THROW3(system_error, errno, "%s: Cannot write file %s: %s",
473            rctx->filepos, filename, strerror(errno));
474   }
475   free(filename);
476 }
477
478 /* function to be called from the child to start the actual process */
479 static void start_command(rctx_t rctx)
480 {
481   xbt_dynar_t cmd;
482   char *binary_name = NULL;
483   unsigned int it;
484   char *str;
485   char **args;
486   int errcode;
487
488   if (!strncmp(rctx->cmd, "mkfile ", strlen("mkfile "))) {
489     rctx_mkfile();
490     /* Valgrind detects memory leaks here.
491      * To correct those leaks, we must free objects allocated in main() or in
492      * handle_suite(), but we have no more reference to them at this point.
493      * A quick and dirty hack to make valgrind happy it to uncomment the
494      * following line.
495      */
496     /* execlp("true", "true", (const char *)0); */
497     exit(0);                    /* end the working child */
498   }
499
500   cmd = xbt_str_split_quoted(rctx->cmd);
501   xbt_dynar_get_cpy(cmd, 0, &binary_name);
502   args = xbt_new(char *, xbt_dynar_length(cmd) + 1);
503   xbt_dynar_foreach(cmd, it, str) {
504     args[it] = xbt_strdup(str);
505   }
506   args[it] = NULL;
507   xbt_dynar_free_container(&cmd);
508
509   /* To search for the right executable path when not trivial */
510   struct stat stat_buf;
511
512   /* build the command line */
513   if (stat(binary_name, &stat_buf)) {
514     /* Damn. binary not in current dir. We'll have to dig the PATH to find it */
515     int i;
516
517     for (i = 0; environ[i]; i++) {
518       if (!strncmp("PATH=", environ[i], 5)) {
519         xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
520
521         xbt_dynar_foreach(path, it, str) {
522           if (binary_name)
523             free(binary_name);
524           binary_name = bprintf("%s/%s", str, args[0]);
525           if (!stat(binary_name, &stat_buf)) {
526             /* Found. */
527             DEBUG1("Looked in the PATH for the binary. Found %s",
528                    binary_name);
529             xbt_dynar_free(&path);
530             break;
531           }
532         }
533         xbt_dynar_free(&path);
534         if (stat(binary_name, &stat_buf)) {
535           /* not found */
536           printf("TESH_ERROR Command %s not found\n", args[0]);
537           exit(127);
538         }
539         break;
540       }
541     }
542   } else {
543     binary_name = xbt_strdup(args[0]);
544   }
545
546   errcode = execve(binary_name, args, rctx->env);
547   printf("TESH_ERROR %s: Cannot start %s: %s\n", rctx->filepos, rctx->cmd,
548          strerror(errcode));
549   exit(127);
550 }
551
552 /* Start a new child, plug the pipes as expected and fire up the
553    helping threads. Is also waits for the child to end if this is a
554    foreground job, or fire up a thread to wait otherwise. */
555 void rctx_start(void)
556 {
557   int child_in[2];
558   int child_out[2];
559
560   DEBUG1("Cmd before rewriting %s", rctx->cmd);
561   rctx->cmd = xbt_str_varsubst(rctx->cmd, env);
562   VERB2("Start %s %s", rctx->cmd,
563         (rctx->is_background ? "(background job)" : ""));
564   xbt_os_mutex_acquire(armageddon_mutex);
565   if (armageddon_initiator) {
566     VERB0("Armageddon in progress. Do not start job.");
567     xbt_os_mutex_release(armageddon_mutex);
568     return;
569   }
570   if (pipe(child_in) || pipe(child_out)) {
571     perror("Cannot open the pipes");
572     ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
573     xbt_os_mutex_release(armageddon_mutex);
574     rctx_armageddon(rctx, 4);
575   }
576
577   rctx->pid = fork();
578   if (rctx->pid < 0) {
579     perror("Cannot fork the command");
580     ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
581     xbt_os_mutex_release(armageddon_mutex);
582     rctx_armageddon(rctx, 4);
583     return;
584   }
585
586   if (rctx->pid) {              /* father */
587     close(child_in[0]);
588     rctx->child_to = child_in[1];
589
590     close(child_out[1]);
591     rctx->child_from = child_out[0];
592
593     if (timeout_value > 0)
594       rctx->end_time = time(NULL) + timeout_value;
595     else
596       rctx->end_time = -1;
597
598     rctx->reader_done = 0;
599     rctx->reader =
600         xbt_os_thread_create("reader", thread_reader, (void *) rctx);
601     rctx->writer =
602         xbt_os_thread_create("writer", thread_writer, (void *) rctx);
603
604   } else {                      /* child */
605     close(child_in[1]);
606     dup2(child_in[0], 0);
607     close(child_in[0]);
608
609     close(child_out[0]);
610     dup2(child_out[1], 1);
611     dup2(child_out[1], 2);
612     close(child_out[1]);
613
614     start_command(rctx);
615   }
616
617   rctx->is_stoppable = 1;
618
619   if (!rctx->is_background) {
620     fg_job = 1;
621     xbt_os_mutex_release(armageddon_mutex);
622     rctx_wait(rctx);
623     fg_job = 0;
624   } else {
625     /* Damn. Copy the rctx and launch a thread to handle it */
626     rctx_t old = rctx;
627     xbt_os_thread_t runner;
628
629     rctx = rctx_new();
630     DEBUG2("RCTX: new bg=%p, new fg=%p", old, rctx);
631
632     DEBUG2("Launch a thread to wait for %s %d", old->cmd, old->pid);
633     runner = xbt_os_thread_create(old->cmd, rctx_wait, (void *) old);
634     old->runner = runner;
635     VERB3("Launched thread %p to wait for %s %d", runner, old->cmd,
636           old->pid);
637     xbt_dynar_push(bg_jobs, &old);
638     xbt_os_mutex_release(armageddon_mutex);
639   }
640 }
641
642 /* Waits for the child to end (or to timeout), and check its
643    ending conditions. This is launched from rctx_start but either in main
644    thread (for foreground jobs) or in a separate one for background jobs.
645    That explains the prototype, forced by xbt_os_thread_create. */
646
647 void *rctx_wait(void *r)
648 {
649   rctx_t rctx = (rctx_t) r;
650   int errcode = 0;
651   int now = time(NULL);
652
653   rctx_dump(rctx, "wait");
654
655   if (!rctx->is_stoppable)
656     THROW1(unknown_error, 0, "Cmd '%s' not started yet. Cannot wait it",
657            rctx->cmd);
658
659   /* Wait for the child to die or the timeout to happen (or an armageddon to happen) */
660   while (!rctx->reader_done
661          && (rctx->end_time < 0 || rctx->end_time >= now)) {
662     usleep(100);
663     now = time(NULL);
664   }
665
666   xbt_os_mutex_acquire(rctx->interruption);
667   if (!rctx->interrupted && rctx->end_time > 0 && rctx->end_time < now) {
668     INFO1("<%s> timeouted. Kill the process.", rctx->filepos);
669     rctx->timeout = 1;
670     kill(rctx->pid, SIGTERM);
671     usleep(100);
672     kill(rctx->pid, SIGKILL);
673   }
674
675   /* Make sure helper threads die.
676      Cannot block since they wait for the child we just killed
677      if not already dead. */
678   xbt_os_thread_join(rctx->writer, NULL);
679   xbt_os_thread_join(rctx->reader, NULL);
680
681   /*  xbt_os_mutex_release(rctx->interruption);
682      if (rctx->interrupted)
683      return NULL;
684      xbt_os_mutex_acquire(rctx->interruption); */
685
686   xbt_strbuff_chomp(rctx->output_got);
687   xbt_strbuff_chomp(rctx->output_wanted);
688   xbt_strbuff_trim(rctx->output_got);
689   xbt_strbuff_trim(rctx->output_wanted);
690
691   /* Check for broken pipe */
692   if (rctx->brokenpipe)
693     VERB0
694         ("Warning: Child did not consume all its input (I got broken pipe)");
695
696   /* Check for timeouts */
697   if (rctx->timeout) {
698     if (rctx->output_got->data[0])
699       INFO2("<%s> Output on timeout:\n%s",
700             rctx->filepos, rctx->output_got->data);
701     else
702       INFO1("<%s> No output before timeout", rctx->filepos);
703     ERROR3("Test suite `%s': NOK (<%s> timeout after %d sec)",
704            testsuite_name, rctx->filepos, timeout_value);
705     DEBUG2("<%s> Interrupted = %d", rctx->filepos, rctx->interrupted);
706     if (!rctx->interrupted) {
707       xbt_os_mutex_release(rctx->interruption);
708       rctx_armageddon(rctx, 3);
709       return NULL;
710     }
711   }
712
713   DEBUG2("RCTX=%p (pid=%d)", rctx, rctx->pid);
714   DEBUG3("Status(%s|%d)=%d", rctx->cmd, rctx->pid, rctx->status);
715
716   if (!rctx->interrupted) {
717     if (WIFSIGNALED(rctx->status) && !rctx->expected_signal) {
718       ERROR3("Test suite `%s': NOK (<%s> got signal %s)",
719              testsuite_name, rctx->filepos,
720              signal_name(WTERMSIG(rctx->status), NULL));
721       errcode = WTERMSIG(rctx->status) + 4;
722     }
723
724     if (WIFSIGNALED(rctx->status) && rctx->expected_signal &&
725         strcmp(signal_name(WTERMSIG(rctx->status), rctx->expected_signal),
726                rctx->expected_signal)) {
727       ERROR4("Test suite `%s': NOK (%s got signal %s instead of %s)",
728              testsuite_name, rctx->filepos,
729              signal_name(WTERMSIG(rctx->status), rctx->expected_signal),
730              rctx->expected_signal);
731       errcode = WTERMSIG(rctx->status) + 4;
732     }
733
734     if (!WIFSIGNALED(rctx->status) && rctx->expected_signal) {
735       ERROR3("Test suite `%s': NOK (child %s expected signal %s)",
736              testsuite_name, rctx->filepos, rctx->expected_signal);
737       errcode = 5;
738     }
739
740     if (WIFEXITED(rctx->status)
741         && WEXITSTATUS(rctx->status) != rctx->expected_return) {
742       if (rctx->expected_return)
743         ERROR4
744             ("Test suite `%s': NOK (<%s> returned code %d instead of %d)",
745              testsuite_name, rctx->filepos, WEXITSTATUS(rctx->status),
746              rctx->expected_return);
747       else
748         ERROR3("Test suite `%s': NOK (<%s> returned code %d)",
749                testsuite_name, rctx->filepos, WEXITSTATUS(rctx->status));
750       errcode = 40 + WEXITSTATUS(rctx->status);
751
752     }
753     rctx->expected_return = 0;
754
755     if (rctx->expected_signal) {
756       free(rctx->expected_signal);
757       rctx->expected_signal = NULL;
758     }
759   }
760   while (rctx->output_got->used
761          && !strncmp(rctx->output_got->data, "TESH_ERROR ",
762                      strlen("TESH_ERROR "))) {
763     int marklen = strlen("TESH_ERROR ");
764     char *endline = strchr(rctx->output_got->data, '\n');
765
766     CRITICAL2("%.*s", (int) (endline - rctx->output_got->data - marklen),
767               rctx->output_got->data + marklen);
768     memmove(rctx->output_got->data, rctx->output_got->data + marklen,
769             rctx->output_got->used - marklen);
770     rctx->output_got->used -= endline - rctx->output_got->data + 1;
771     rctx->output_got->data[rctx->output_got->used] = '\0';
772     errcode = 1;
773   }
774
775   if ((errcode && errcode != 1) || rctx->interrupted) {
776     /* checking output, and matching */
777     xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
778     char *out = xbt_str_join(a, "\n||");
779     xbt_dynar_free(&a);
780     INFO2("Output of <%s> so far: \n||%s", rctx->filepos, out);
781     free(out);
782   } else if (rctx->output == e_output_check
783              && (rctx->output_got->used != rctx->output_wanted->used
784                  || strcmp(rctx->output_got->data,
785                            rctx->output_wanted->data))) {
786     if (XBT_LOG_ISENABLED(tesh, xbt_log_priority_info)) {
787       char *diff =
788           xbt_str_diff(rctx->output_wanted->data, rctx->output_got->data);
789       ERROR2("Output of <%s> mismatch:\n%s", rctx->filepos, diff);
790       free(diff);
791     }
792     ERROR2("Test suite `%s': NOK (<%s> output mismatch)",
793            testsuite_name, rctx->filepos);
794
795     errcode = 2;
796   } else if (rctx->output == e_output_ignore) {
797     INFO1("(ignoring the output of <%s> as requested)", rctx->filepos);
798   } else if (rctx->output == e_output_display) {
799     xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
800     char *out = xbt_str_join(a, "\n||");
801     xbt_dynar_free(&a);
802     INFO1("Here is the (ignored) command output: \n||%s", out);
803     free(out);
804   }
805
806   if (!rctx->is_background) {
807     xbt_os_mutex_acquire(armageddon_mutex);
808     /* Don't touch rctx if armageddon is in progress. */
809     if (!armageddon_initiator)
810       rctx_empty(rctx);
811     xbt_os_mutex_release(armageddon_mutex);
812   }
813   if (errcode) {
814     if (!rctx->interrupted) {
815       xbt_os_mutex_release(rctx->interruption);
816       rctx_armageddon(rctx, errcode);
817       return NULL;
818     }
819   }
820
821   xbt_os_mutex_release(rctx->interruption);
822   return NULL;
823 }