Logo AND Algorithmique Numérique Distribuée

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