Logo AND Algorithmique Numérique Distribuée

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