1 /* run_context -- stuff in which TESH runs a command */
3 /* Copyright (c) 2007-2014. The SimGrid Team.
4 * All rights reserved. */
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. */
12 #include <sys/types.h>
16 #include <math.h> /* floor */
18 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
21 xbt_dynar_t bg_jobs = NULL;
22 rctx_t armageddon_initiator = NULL;
23 xbt_os_mutex_t armageddon_mutex = NULL;
27 } oldact[3]; /* SIGINT, SIGQUIT, SIGTERM */
29 xbt_os_thread_t sigwaiter_thread;
30 xbt_os_mutex_t sigwaiter_mutex;
31 xbt_os_cond_t sigwaiter_cond;
32 int armageddon_requested = 0;
33 int caught_signum = 0;
39 static void armageddon_sighandler(int signum)
41 xbt_os_mutex_acquire(sigwaiter_mutex);
42 caught_signum = signum;
43 armageddon_requested = 1;
44 xbt_os_cond_signal(sigwaiter_cond);
45 xbt_os_mutex_release(sigwaiter_mutex);
48 static void *armageddon_sigwaiter(_XBT_GNUC_UNUSED void *arg)
50 xbt_os_mutex_acquire(sigwaiter_mutex);
51 /* Inform main thread that it started. */
52 xbt_os_cond_signal(sigwaiter_cond);
53 /* Wait for ending signal... */
54 xbt_os_cond_wait(sigwaiter_cond, sigwaiter_mutex);
55 if (armageddon_requested) {
56 XBT_ERROR("Test suite `%s': caught signal %d", testsuite_name, caught_signum);
57 rctx_armageddon(rctx, 3);
59 xbt_os_mutex_release(sigwaiter_mutex);
63 static void wait_it(rctx_t rctx)
65 XBT_VERB("Join thread %p which were running background cmd <%s>",
66 rctx->runner, rctx->filepos);
67 xbt_os_thread_join(rctx->runner, NULL);
70 static void kill_it(void *r)
72 rctx_t rctx = *(rctx_t *) r;
79 struct sigaction newact;
82 bg_jobs = xbt_dynar_new(sizeof(rctx_t), kill_it);
83 armageddon_mutex = xbt_os_mutex_init();
84 armageddon_initiator = NULL;
85 sigwaiter_mutex = xbt_os_mutex_init();
86 sigwaiter_cond = xbt_os_cond_init();
87 xbt_os_mutex_acquire(sigwaiter_mutex);
88 sigwaiter_thread = xbt_os_thread_create("Armageddon request waiter",
89 armageddon_sigwaiter, NULL, NULL);
90 /* Wait for thread to start... */
91 xbt_os_cond_wait(sigwaiter_cond, sigwaiter_mutex);
92 xbt_os_mutex_release(sigwaiter_mutex);
93 memset(&newact, 0, sizeof(newact));
94 newact.sa_handler = armageddon_sighandler;
95 oldact[0].num = SIGINT;
96 oldact[1].num = SIGQUIT;
97 oldact[2].num = SIGTERM;
98 for (i = 0; i < 3; i++)
99 sigaction(oldact[i].num, &newact, &oldact[i].act);
105 for (i = 0; i < 3; i++)
106 sigaction(oldact[i].num, &oldact[i].act, NULL);
107 xbt_os_cond_signal(sigwaiter_cond);
108 xbt_os_thread_join(sigwaiter_thread, NULL);
109 xbt_dynar_free(&bg_jobs);
110 xbt_os_cond_destroy(sigwaiter_cond);
111 xbt_os_mutex_destroy(sigwaiter_mutex);
112 xbt_os_mutex_destroy(armageddon_mutex);
115 void rctx_wait_bg(void)
117 /* Do not use xbt_dynar_free or it will lock the dynar, preventing armageddon
119 while (!xbt_dynar_is_empty(bg_jobs)) {
120 rctx_t rctx = xbt_dynar_getlast_as(bg_jobs, rctx_t);
122 xbt_dynar_pop(bg_jobs, &rctx);
125 xbt_dynar_reset(bg_jobs);
128 static void rctx_armageddon_kill_one(rctx_t initiator, const char *filepos,
131 if (rctx != initiator) {
132 XBT_INFO("Kill <%s> because <%s> failed", rctx->filepos, filepos);
133 xbt_os_mutex_acquire(rctx->interruption);
134 if (!rctx->reader_done) {
135 rctx->interrupted = 1;
136 kill(rctx->pid, SIGTERM);
139 ts.tv_nsec = (100e-6 - floor(100e-6)) * 1e9;
140 nanosleep (&ts, NULL);
141 kill(rctx->pid, SIGKILL);
143 xbt_os_mutex_release(rctx->interruption);
147 void rctx_armageddon(rctx_t initiator, int exitcode)
151 const char *filepos = initiator && initiator->filepos ?
152 initiator->filepos : "(master)";
154 XBT_DEBUG("Armageddon request by <%s> (exit=%d)", filepos, exitcode);
155 xbt_os_mutex_acquire(armageddon_mutex);
156 if (armageddon_initiator != NULL) {
157 XBT_VERB("Armageddon already started. Let it go");
158 xbt_os_mutex_release(armageddon_mutex);
161 XBT_DEBUG("Armageddon request by <%s> got the lock. Let's go amok",
163 armageddon_initiator = initiator;
164 xbt_os_mutex_release(armageddon_mutex);
166 /* Kill foreground command */
168 rctx_armageddon_kill_one(initiator, filepos, rctx);
170 /* Kill any background commands */
171 xbt_dynar_foreach(bg_jobs, cursor, job) {
172 rctx_armageddon_kill_one(initiator, filepos, job);
175 /* Give runner threads a chance to acknowledge the processes deaths */
178 ts.tv_nsec = (10000e-6 - floor(10000e-6)) * 1e9;
179 nanosleep (&ts, NULL);
180 /* Ensure that nobody is running rctx_wait on exit */
182 xbt_os_mutex_acquire(rctx->interruption);
183 xbt_dynar_foreach(bg_jobs, cursor, job)
184 xbt_os_mutex_acquire(job->interruption);
185 XBT_VERB("Shut everything down!");
193 void rctx_empty(rctx_t rc)
201 /* avoid race with rctx_armageddon log messages */
202 filepos = rc->filepos;
205 for (i = 0, env_it = environ; *env_it; i++, env_it++);
207 for (env_it = rctx->env + i; *env_it; env_it++)
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 *));
216 rc->is_background = 0;
217 rc->is_stoppable = 0;
218 rc->output = e_output_check;
223 xbt_strbuff_empty(rc->input);
224 xbt_strbuff_empty(rc->output_wanted);
225 xbt_strbuff_empty(rc->output_got);
231 rctx_t res = xbt_new0(s_rctx_t, 1);
233 res->input = xbt_strbuff_new();
234 res->output_sort = 0;
235 res->output_wanted = xbt_strbuff_new();
236 res->output_got = xbt_strbuff_new();
237 res->interruption = xbt_os_mutex_init();
242 void rctx_free(rctx_t rctx)
244 XBT_DEBUG("RCTX: Free %p", rctx);
245 rctx_dump(rctx, "free");
254 for (i = 0, env_it = environ; *env_it; i++, env_it++);
255 for (env_it = rctx->env + i; *env_it; env_it++)
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);
266 void rctx_dump(rctx_t rctx, const char *str)
268 XBT_DEBUG("%s RCTX %p={in%p={%d,%10s}, want={%d,%10s}, out={%d,%10s}}",
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 XBT_DEBUG("%s RCTX %p=[cmd%p=%10s, pid=%d]",
274 str, rctx, rctx->cmd, rctx->cmd, rctx->pid);
279 * Getting instructions from the file
282 void rctx_pushline(const char *filepos, char kind, char *line)
289 if (!rctx->is_empty) {
291 ("[%s] More than one command in this chunk of lines (previous: %s).\n"
292 " Cannot guess which input/output belongs to which command.",
294 XBT_ERROR("Test suite `%s': NOK (syntax error)", testsuite_name);
295 rctx_armageddon(rctx, 1);
299 XBT_VERB("[%s] More than one command in this chunk of lines", filepos);
302 rctx->is_background = 1;
304 rctx->is_background = 0;
306 rctx->cmd = xbt_strdup(line);
307 rctx->filepos = xbt_strdup(filepos);
309 char *newcmd = bprintf("%s %s", rctx->cmd, option);
313 XBT_INFO("[%s] %s%s", filepos, rctx->cmd,
314 ((rctx->is_background) ? " (background command)" : ""));
320 xbt_strbuff_append(rctx->input, line);
321 xbt_strbuff_append(rctx->input, "\n");
326 xbt_strbuff_append(rctx->output_wanted, line);
327 xbt_strbuff_append(rctx->output_wanted, "\n");
328 XBT_DEBUG("wanted:%s",rctx->output_wanted->data);
335 if (!strncmp(line, "timeout no", strlen("timeout no"))) {
336 XBT_VERB("[%s] (disable timeout)", filepos);
338 } else if (!strncmp(line, "timeout ", strlen("timeout "))) {
339 timeout_value = atoi(line + strlen("timeout"));
340 XBT_VERB("[%s] (new timeout value: %d)", filepos, timeout_value);
342 } else if (!strncmp(line, "expect signal ", strlen("expect signal "))) {
343 rctx->expected_signal = strdup(line + strlen("expect signal "));
344 xbt_str_trim(rctx->expected_signal, " \n");
345 XBT_VERB("[%s] (next command must raise signal %s)",
346 filepos, rctx->expected_signal);
348 } else if (!strncmp(line, "expect return ", strlen("expect return "))) {
349 rctx->expected_return = atoi(line + strlen("expect return "));
350 XBT_VERB("[%s] (next command must return code %d)",
351 filepos, rctx->expected_return);
353 } else if (!strncmp(line, "output sort", strlen("output sort"))) {
354 sort_len = atoi(line + strlen("output sort"));
356 sort_len=SORT_LEN_DEFAULT;
357 rctx->output_sort = 1;
358 XBT_VERB("[%s] (sort output of next command)", filepos);
360 } else if (!strncmp(line, "output ignore", strlen("output ignore"))) {
361 rctx->output = e_output_ignore;
362 XBT_VERB("[%s] (ignore output of next command)", filepos);
364 } else if (!strncmp(line, "output display", strlen("output display"))) {
365 rctx->output = e_output_display;
366 XBT_VERB("[%s] (ignore output of next command)", filepos);
368 } else if (!strncmp(line, "setenv ", strlen("setenv "))) {
369 int len = strlen("setenv ");
370 char *eq = strchr(line + len, '=');
371 char *key = bprintf("%.*s", (int) (eq - line - len), line + len);
372 xbt_dict_set(env, key, xbt_strdup(eq + 1), NULL);
375 rctx->env = realloc(rctx->env, ++(rctx->env_size) * sizeof(char *));
376 rctx->env[rctx->env_size - 2] = xbt_strdup(line + len);
377 rctx->env[rctx->env_size - 1] = NULL;
378 XBT_VERB("[%s] setenv %s", filepos, line + len);
381 XBT_ERROR("%s: Malformed metacommand: %s", filepos, line);
382 XBT_ERROR("Test suite `%s': NOK (syntax error)", testsuite_name);
383 rctx_armageddon(rctx, 1);
391 * Actually doing the job
394 /* The IO of the childs are handled by the two following threads
395 (one pair per child) */
397 static void *thread_writer(void *r)
400 rctx_t rctx = (rctx_t) r;
401 for (posw = 0; posw < rctx->input->used && !rctx->brokenpipe;) {
403 XBT_DEBUG("Still %d chars to write", rctx->input->used - posw);
405 write(rctx->child_to, rctx->input->data + posw,
406 rctx->input->used - posw);
410 if (errno == EPIPE) {
411 rctx->brokenpipe = 1;
412 } else if (errno != EINTR && errno != EAGAIN && errno != EPIPE) {
413 perror("Error while writing input to child");
414 XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name);
415 rctx_armageddon(rctx, 4);
419 XBT_DEBUG("written %d chars so far", posw);
424 ts.tv_nsec = (100e-6 - floor(100e-6)) * 1e9;
425 nanosleep (&ts, NULL);
428 rctx->input->data[0] = '\0';
429 rctx->input->used = 0;
430 close(rctx->child_to);
435 static void *thread_reader(void *r)
437 rctx_t rctx = (rctx_t) r;
438 char *buffout = malloc(4096);
442 posr = read(rctx->child_from, buffout, 4095);
443 if (posr < 0 && errno != EINTR && errno != EAGAIN) {
444 perror("Error while reading output of child");
445 XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name);
446 rctx_armageddon(rctx, 4);
450 buffout[posr] = '\0';
451 xbt_strbuff_append(rctx->output_got, buffout);
455 ts.tv_nsec = (100e-6 - floor(100e-6)) * 1e9;
456 nanosleep (&ts, NULL);
458 } while (!rctx->timeout && posr != 0);
461 /* let this thread wait for the child so that the main thread can detect the timeout without blocking on the wait */
462 got_pid = waitpid(rctx->pid, &rctx->status, 0);
463 if (got_pid != rctx->pid) {
465 ("(%s) Cannot wait for the child %s (got pid %d where pid %d were expected;status=%d)",
466 xbt_thread_self_name(), rctx->cmd, (int) got_pid,
467 (int) rctx->pid, rctx->status));
468 XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name);
469 rctx_armageddon(rctx, 4);
473 rctx->reader_done = 1;
477 /* Special command: mkfile is a built-in creating a file with the input data as content */
478 static void rctx_mkfile(void)
480 char *filename = xbt_strdup(rctx->cmd + strlen("mkfile "));
483 xbt_str_trim(filename, NULL);
484 OUT = fopen(filename, "w");
486 THROWF(system_error, errno, "%s: Cannot create file %s: %s",
487 rctx->filepos, filename, strerror(errno));
489 err = (fprintf(OUT, "%s", rctx->input->data) < 0);
490 err = (fclose(OUT) == -1) || err;
492 THROWF(system_error, errno, "%s: Cannot write file %s: %s",
493 rctx->filepos, filename, strerror(errno));
498 /* function to be called from the child to start the actual process */
499 static void start_command(rctx_t rctx)
502 char *binary_name = NULL;
508 if (!strncmp(rctx->cmd, "mkfile ", strlen("mkfile "))) {
510 /* Valgrind detects memory leaks here.
511 * To correct those leaks, we must free objects allocated in main() or in
512 * handle_suite(), but we have no more reference to them at this point.
513 * A quick and dirty hack to make valgrind happy it to uncomment the
516 /* execlp("true", "true", (const char *)0); */
517 exit(0); /* end the working child */
520 cmd = xbt_str_split_quoted(rctx->cmd);
521 xbt_dynar_get_cpy(cmd, 0, &binary_name);
522 args = xbt_new(char *, xbt_dynar_length(cmd) + 1);
523 xbt_dynar_foreach(cmd, it, str) {
524 args[it] = xbt_strdup(str);
527 xbt_dynar_free_container(&cmd);
529 /* To search for the right executable path when not trivial */
530 struct stat stat_buf;
532 /* build the command line */
533 if (stat(binary_name, &stat_buf)) {
534 /* Damn. binary not in current dir. We'll have to dig the PATH to find it */
537 for (i = 0; environ[i]; i++) {
538 if (!strncmp("PATH=", environ[i], 5)) {
539 xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
541 xbt_dynar_foreach(path, it, str) {
543 binary_name = bprintf("%s/%s", str, args[0]);
544 if (!stat(binary_name, &stat_buf)) {
546 XBT_DEBUG("Looked in the PATH for the binary. Found %s",
548 xbt_dynar_free(&path);
552 xbt_dynar_free(&path);
553 if (stat(binary_name, &stat_buf)) {
555 printf("TESH_ERROR Command %s not found\n", args[0]);
562 binary_name = xbt_strdup(args[0]);
565 errcode = execve(binary_name, args, rctx->env);
566 printf("TESH_ERROR %s: Cannot start %s: %s\n", rctx->filepos, rctx->cmd,
571 /* Start a new child, plug the pipes as expected and fire up the
572 helping threads. Is also waits for the child to end if this is a
573 foreground job, or fire up a thread to wait otherwise. */
574 void rctx_start(void)
579 XBT_DEBUG("Cmd before rewriting %s", rctx->cmd);
580 char *newcmd = xbt_str_varsubst(rctx->cmd, env);
583 XBT_VERB("Start %s %s", rctx->cmd,
584 (rctx->is_background ? "(background job)" : ""));
585 xbt_os_mutex_acquire(armageddon_mutex);
586 if (armageddon_initiator) {
587 XBT_VERB("Armageddon in progress. Do not start job.");
588 xbt_os_mutex_release(armageddon_mutex);
591 if (pipe(child_in) || pipe(child_out)) {
592 perror("Cannot open the pipes");
593 XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name);
594 xbt_os_mutex_release(armageddon_mutex);
595 rctx_armageddon(rctx, 4);
600 perror("Cannot fork the command");
601 XBT_ERROR("Test suite `%s': NOK (system error)", testsuite_name);
602 xbt_os_mutex_release(armageddon_mutex);
603 rctx_armageddon(rctx, 4);
607 if (rctx->pid) { /* father */
609 rctx->child_to = child_in[1];
612 rctx->child_from = child_out[0];
614 if (timeout_value > 0)
615 rctx->end_time = time(NULL) + timeout_value;
619 rctx->reader_done = 0;
621 xbt_os_thread_create("reader", thread_reader, (void *) rctx, NULL);
623 xbt_os_thread_create("writer", thread_writer, (void *) rctx, NULL);
627 dup2(child_in[0], 0);
631 dup2(child_out[1], 1);
632 dup2(child_out[1], 2);
638 rctx->is_stoppable = 1;
640 if (!rctx->is_background) {
642 xbt_os_mutex_release(armageddon_mutex);
646 /* Damn. Copy the rctx and launch a thread to handle it */
648 xbt_os_thread_t runner;
651 XBT_DEBUG("RCTX: new bg=%p, new fg=%p", old, rctx);
653 XBT_DEBUG("Launch a thread to wait for %s %d", old->cmd, old->pid);
654 runner = xbt_os_thread_create(old->cmd, rctx_wait, (void *) old, NULL);
655 old->runner = runner;
656 XBT_VERB("Launched thread %p to wait for %s %d", runner, old->cmd,
658 xbt_dynar_push(bg_jobs, &old);
659 xbt_os_mutex_release(armageddon_mutex);
663 /* Helper function to sort the output */
664 static int cmpstringp(const void *p1, const void *p2) {
665 /* Sort only using the sort_len first chars
666 * If they are the same, then, sort using pointer address
667 * (be stable wrt output of each process)
669 const char **s1 = *(const char***)p1;
670 const char **s2 = *(const char***)p2;
672 XBT_DEBUG("Compare strings '%s' and '%s'", *s1, *s2);
674 int res = strncmp(*s1, *s2, sort_len);
676 res = s1 > s2 ? 1 : (s1 < s2 ? -1 : 0);
680 static void stable_sort(xbt_dynar_t a)
682 unsigned long len = xbt_dynar_length(a);
683 void **b = xbt_new(void*, len);
685 for (i = 0 ; i < len ; i++) /* fill the array b with pointers to strings */
686 b[i] = xbt_dynar_get_ptr(a, i);
687 qsort(b, len, sizeof *b, cmpstringp); /* sort it */
688 for (i = 0 ; i < len ; i++) /* dereference the pointers to get the strings */
689 b[i] = *(char**)b[i];
690 for (i = 0 ; i < len ; i++) /* put everything in place */
691 xbt_dynar_set_as(a, i, char*, b[i]);
695 /* Waits for the child to end (or to timeout), and check its
696 ending conditions. This is launched from rctx_start but either in main
697 thread (for foreground jobs) or in a separate one for background jobs.
698 That explains the prototype, forced by xbt_os_thread_create. */
700 void *rctx_wait(void *r)
702 rctx_t rctx = (rctx_t) r;
704 int now = time(NULL);
706 rctx_dump(rctx, "wait");
708 if (!rctx->is_stoppable)
709 THROWF(unknown_error, 0, "Cmd '%s' not started yet. Cannot wait it",
712 /* Wait for the child to die or the timeout to happen (or an armageddon to happen) */
713 while (!rctx->reader_done
714 && (rctx->end_time < 0 || rctx->end_time >= now)) {
717 ts.tv_nsec = (100e-6 - floor(100e-6)) * 1e9;
718 nanosleep (&ts, NULL);
722 xbt_os_mutex_acquire(rctx->interruption);
723 if (!rctx->interrupted && rctx->end_time > 0 && rctx->end_time < now) {
724 XBT_INFO("<%s> timeouted. Kill the process.", rctx->filepos);
726 kill(rctx->pid, SIGTERM);
729 ts.tv_nsec = (100e-6 - floor(100e-6)) * 1e9;
730 nanosleep (&ts, NULL);
731 kill(rctx->pid, SIGKILL);
734 /* Make sure helper threads die.
735 Cannot block since they wait for the child we just killed
736 if not already dead. */
737 xbt_os_thread_join(rctx->writer, NULL);
738 xbt_os_thread_join(rctx->reader, NULL);
740 /* xbt_os_mutex_release(rctx->interruption);
741 if (rctx->interrupted)
743 xbt_os_mutex_acquire(rctx->interruption); */
745 { // Sorting output got
746 xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
747 xbt_dynar_t b = xbt_dynar_new(sizeof(char *), NULL);
750 xbt_dynar_foreach(a, cpt, str) {
751 if (strncmp(str, "TESH_ERROR ", (sizeof "TESH_ERROR ") - 1) == 0) {
752 XBT_CRITICAL("%s", str);
754 } else if (coverage &&
755 strncmp(str, "profiling:", (sizeof "profiling:") - 1) == 0) {
756 XBT_DEBUG("Remove line [%u]: '%s'", cpt, str);
758 xbt_dynar_push_as(b, char *, str);
762 if (rctx->output_sort) {
764 /* If empty lines moved in first position, remove them */
765 while (!xbt_dynar_is_empty(b) && *xbt_dynar_getfirst_as(b, char*) == '\0')
766 xbt_dynar_shift(b, NULL);
769 if (rctx->output_sort || xbt_dynar_length(b) != xbt_dynar_length(a)) {
770 char *newbuf = xbt_str_join(b, "\n");
771 strcpy(rctx->output_got->data, newbuf);
772 rctx->output_got->used = strlen(newbuf);
780 if (rctx->output_sort) { // Sorting output wanted
782 xbt_dynar_t a = xbt_str_split(rctx->output_wanted->data, "\n");
785 /* If empty lines moved in first position, remove them */
786 while (!xbt_dynar_is_empty(a) && *xbt_dynar_getfirst_as(a, char*) == '\0')
787 xbt_dynar_shift(a, NULL);
789 newbuf = xbt_str_join(a, "\n");
790 strcpy(rctx->output_wanted->data, newbuf);
791 rctx->output_wanted->used = strlen(newbuf);
796 xbt_strbuff_chomp(rctx->output_got);
797 xbt_strbuff_chomp(rctx->output_wanted);
799 /* Check for broken pipe */
800 if (rctx->brokenpipe)
802 ("Warning: Child did not consume all its input (I got broken pipe)");
804 /* Check for timeouts */
806 if (rctx->output_got->data[0])
807 XBT_INFO("<%s> Output on timeout:\n%s",
808 rctx->filepos, rctx->output_got->data);
810 XBT_INFO("<%s> No output before timeout", rctx->filepos);
811 XBT_ERROR("Test suite `%s': NOK (<%s> timeout after %d sec)",
812 testsuite_name, rctx->filepos, timeout_value);
813 XBT_DEBUG("<%s> Interrupted = %d", rctx->filepos, (int)rctx->interrupted);
814 if (!rctx->interrupted) {
815 xbt_os_mutex_release(rctx->interruption);
816 rctx_armageddon(rctx, 3);
821 XBT_DEBUG("RCTX=%p (pid=%d)", rctx, rctx->pid);
822 XBT_DEBUG("Status(%s|%d)=%d", rctx->cmd, rctx->pid, rctx->status);
824 if (!rctx->interrupted) {
825 if (WIFSIGNALED(rctx->status) && !rctx->expected_signal) {
826 XBT_ERROR("Test suite `%s': NOK (<%s> got signal %s)",
827 testsuite_name, rctx->filepos,
828 signal_name(WTERMSIG(rctx->status), NULL));
829 errcode = WTERMSIG(rctx->status) + 4;
832 if (WIFSIGNALED(rctx->status) && rctx->expected_signal &&
833 strcmp(signal_name(WTERMSIG(rctx->status), rctx->expected_signal),
834 rctx->expected_signal)) {
835 XBT_ERROR("Test suite `%s': NOK (%s got signal %s instead of %s)",
836 testsuite_name, rctx->filepos,
837 signal_name(WTERMSIG(rctx->status), rctx->expected_signal),
838 rctx->expected_signal);
839 errcode = WTERMSIG(rctx->status) + 4;
842 if (!WIFSIGNALED(rctx->status) && rctx->expected_signal) {
843 XBT_ERROR("Test suite `%s': NOK (child %s expected signal %s)",
844 testsuite_name, rctx->filepos, rctx->expected_signal);
848 if (WIFEXITED(rctx->status)
849 && WEXITSTATUS(rctx->status) != rctx->expected_return) {
850 if (rctx->expected_return)
852 ("Test suite `%s': NOK (<%s> returned code %d instead of %d)",
853 testsuite_name, rctx->filepos, WEXITSTATUS(rctx->status),
854 rctx->expected_return);
856 XBT_ERROR("Test suite `%s': NOK (<%s> returned code %d)",
857 testsuite_name, rctx->filepos, WEXITSTATUS(rctx->status));
858 errcode = 40 + WEXITSTATUS(rctx->status);
861 rctx->expected_return = 0;
863 free(rctx->expected_signal);
864 rctx->expected_signal = NULL;
867 if ((errcode && errcode != 1) || rctx->interrupted) {
868 /* checking output, and matching */
869 xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
870 char *out = xbt_str_join(a, "\n||");
872 XBT_INFO("Output of <%s> so far:\n||%s", rctx->filepos, out);
874 } else if (rctx->output == e_output_check
875 && (rctx->output_got->used != rctx->output_wanted->used
876 || strcmp(rctx->output_got->data,
877 rctx->output_wanted->data))) {
878 if (XBT_LOG_ISENABLED(tesh, xbt_log_priority_info)) {
880 xbt_str_diff(rctx->output_wanted->data, rctx->output_got->data);
881 XBT_ERROR("Output of <%s> mismatch:\n%s", rctx->filepos, diff);
884 XBT_ERROR("Test suite `%s': NOK (<%s> output mismatch)",
885 testsuite_name, rctx->filepos);
888 } else if (rctx->output == e_output_ignore) {
889 XBT_INFO("(ignoring the output of <%s> as requested)", rctx->filepos);
890 } else if (rctx->output == e_output_display) {
891 xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
892 char *out = xbt_str_join(a, "\n||");
894 XBT_INFO("Here is the (ignored) command output:\n||%s", out);
898 if (!rctx->is_background) {
899 xbt_os_mutex_acquire(armageddon_mutex);
900 /* Don't touch rctx if armageddon is in progress. */
901 if (!armageddon_initiator)
903 xbt_os_mutex_release(armageddon_mutex);
906 if (!rctx->interrupted) {
907 xbt_os_mutex_release(rctx->interruption);
908 rctx_armageddon(rctx, errcode);
913 xbt_os_mutex_release(rctx->interruption);