1 /* run_context -- stuff in which TESH runs a command */
3 /* Copyright (c) 2007, 2008, 2009, 2010. 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>
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
20 xbt_dynar_t bg_jobs = NULL;
21 rctx_t armageddon_initiator = NULL;
22 xbt_os_mutex_t armageddon_mutex = NULL;
27 } oldact[3]; /* SIGINT, SIGQUIT, SIGTERM */
33 static void armageddon_sighandler(int signum)
35 if (getpid() == father_pid) {
36 ERROR2("Test suite `%s': caught signal %d", testsuite_name, signum);
37 rctx_armageddon(rctx, 3);
41 static void wait_it(rctx_t rctx)
43 VERB2("Join thread %p which were running background cmd <%s>", rctx->runner,
45 xbt_os_thread_join(rctx->runner, NULL);
48 static void kill_it(void *r)
50 rctx_t rctx = *(rctx_t *) r;
57 struct sigaction newact;
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);
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);
81 xbt_dynar_pop(bg_jobs, &rctx);
85 for (i = 0; i < 3; i++)
86 sigaction(oldact[i].num, &oldact[i].act, NULL);
88 xbt_dynar_free(&bg_jobs);
89 xbt_os_mutex_destroy(armageddon_mutex);
92 void rctx_wait_bg(void)
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);
99 xbt_dynar_pop(bg_jobs, &rctx);
102 xbt_dynar_free(&bg_jobs);
104 bg_jobs = xbt_dynar_new_sync(sizeof(rctx_t), kill_it);
107 static void rctx_armageddon_kill_one(rctx_t initiator, const char *filepos,
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);
117 kill(rctx->pid, SIGKILL);
119 xbt_os_mutex_release(rctx->interruption);
123 void rctx_armageddon(rctx_t initiator, int exitcode)
127 const char *filepos = initiator && initiator->filepos ?
128 initiator->filepos : "(master)";
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);
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);
142 /* Kill foreground command */
144 rctx_armageddon_kill_one(initiator, filepos, rctx);
146 /* Kill any background commands */
147 xbt_dynar_foreach(bg_jobs, cursor, job) {
148 rctx_armageddon_kill_one(initiator, filepos, job);
151 VERB0("Shut everything down!");
159 void rctx_empty(rctx_t rc)
162 char **env_it = environ;
168 /* avoid race with rctx_armageddon log messages */
169 filepos = rc->filepos;
176 for (i = 0; *env_it; i++, env_it++);
179 rc->env = malloc(i * sizeof(char *));
180 memcpy(rc->env, environ, i * sizeof(char *));
183 rc->is_background = 0;
184 rc->is_stoppable = 0;
185 rc->output = e_output_check;
189 xbt_strbuff_empty(rc->input);
190 xbt_strbuff_empty(rc->output_wanted);
191 xbt_strbuff_empty(rc->output_got);
197 rctx_t res = xbt_new0(s_rctx_t, 1);
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();
207 void rctx_free(rctx_t rctx)
209 DEBUG1("RCTX: Free %p", rctx);
210 rctx_dump(rctx, "free");
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);
227 void rctx_dump(rctx_t rctx, const char *str)
229 DEBUG9("%s RCTX %p={in%p={%d,%10s}, want={%d,%10s}, out={%d,%10s}}",
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);
240 * Getting instructions from the file
243 void rctx_pushline(const char *filepos, char kind, char *line)
250 if (!rctx->is_empty) {
252 ("[%s] More than one command in this chunk of lines (previous: %s).\n"
253 " Cannot guess which input/output belongs to which command.",
255 ERROR1("Test suite `%s': NOK (syntax error)", testsuite_name);
256 rctx_armageddon(rctx, 1);
260 VERB1("[%s] More than one command in this chunk of lines", filepos);
263 rctx->is_background = 1;
265 rctx->is_background = 0;
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)" : ""));
276 xbt_strbuff_append(rctx->input, line);
277 xbt_strbuff_append(rctx->input, "\n");
282 xbt_strbuff_append(rctx->output_wanted, line);
283 xbt_strbuff_append(rctx->output_wanted, "\n");
290 if (!strncmp(line, "timeout no", strlen("timeout no"))) {
291 VERB1("[%s] (disable timeout)", filepos);
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);
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);
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);
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);
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);
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);
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);
328 ERROR2("%s: Malformed metacommand: %s", filepos, line);
329 ERROR1("Test suite `%s': NOK (syntax error)", testsuite_name);
330 rctx_armageddon(rctx, 1);
338 * Actually doing the job
341 /* The IO of the childs are handled by the two following threads
342 (one pair per child) */
344 static void *thread_writer(void *r)
347 rctx_t rctx = (rctx_t) r;
348 for (posw = 0; posw < rctx->input->used && !rctx->brokenpipe;) {
350 DEBUG1("Still %d chars to write", rctx->input->used - posw);
352 write(rctx->child_to, rctx->input->data + posw,
353 rctx->input->used - posw);
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);
366 DEBUG1("written %d chars so far", posw);
371 rctx->input->data[0] = '\0';
372 rctx->input->used = 0;
373 close(rctx->child_to);
378 static void *thread_reader(void *r)
380 rctx_t rctx = (rctx_t) r;
381 char *buffout = malloc(4096);
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);
393 buffout[posr] = '\0';
394 xbt_strbuff_append(rctx->output_got, buffout);
398 } while (!rctx->timeout && posr != 0);
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);
411 rctx->reader_done = 1;
415 /* Special command: mkfile is a building creating a file with the input data as content */
416 static void rctx_mkfile(void)
418 char *filename = xbt_strdup(rctx->cmd + strlen("mkfile "));
420 xbt_str_trim(filename, NULL);
421 OUT = fopen(filename, "w");
424 THROW3(system_error, errno, "%s: Cannot create file %s: %s",
425 rctx->filepos, filename, strerror(errno));
427 fprintf(OUT, "%s", rctx->input->data);
431 /* function to be called from the child to start the actual process */
432 static void start_command(rctx_t rctx)
434 xbt_dynar_t cmd = xbt_str_split_quoted(rctx->cmd);
435 char *binary_name = NULL;
438 xbt_dynar_get_cpy(cmd, 0, &binary_name);
439 char **args = xbt_new(char *, xbt_dynar_length(cmd) + 1);
442 if (!strncmp(rctx->cmd, "mkfile ", strlen("mkfile "))) {
444 exit(0); /* end the working child */
447 xbt_dynar_foreach(cmd, it, str) {
448 args[it] = xbt_strdup(str);
452 /* To search for the right executable path when not trivial */
453 struct stat stat_buf;
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 */
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, ":");
464 xbt_dynar_foreach(path, it, str) {
467 binary_name = bprintf("%s/%s", str, args[0]);
468 if (!stat(binary_name, &stat_buf)) {
470 DEBUG1("Looked in the PATH for the binary. Found %s",
472 xbt_dynar_free(&path);
476 xbt_dynar_free(&path);
477 if (stat(binary_name, &stat_buf)) {
479 printf("TESH_ERROR Command %s not found\n", args[0]);
486 binary_name = xbt_strdup(args[0]);
489 errcode = execve(binary_name, args, rctx->env);
490 printf("TESH_ERROR %s: Cannot start %s: %s\n", rctx->filepos, rctx->cmd,
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)
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);
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);
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);
529 if (rctx->pid) { /* father */
531 rctx->child_to = child_in[1];
534 rctx->child_from = child_out[0];
536 if (timeout_value > 0)
537 rctx->end_time = time(NULL) + timeout_value;
541 rctx->reader_done = 0;
543 xbt_os_thread_create("reader", thread_reader, (void *) rctx);
545 xbt_os_thread_create("writer", thread_writer, (void *) rctx);
548 xbt_os_mutex_release(armageddon_mutex);
551 dup2(child_in[0], 0);
555 dup2(child_out[1], 1);
556 dup2(child_out[1], 2);
562 rctx->is_stoppable = 1;
564 if (!rctx->is_background) {
566 xbt_os_mutex_release(armageddon_mutex);
570 /* Damn. Copy the rctx and launch a thread to handle it */
572 xbt_os_thread_t runner;
575 DEBUG2("RCTX: new bg=%p, new fg=%p", old, rctx);
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);
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. */
591 void *rctx_wait(void *r)
593 rctx_t rctx = (rctx_t) r;
595 int now = time(NULL);
597 rctx_dump(rctx, "wait");
599 if (!rctx->is_stoppable)
600 THROW1(unknown_error, 0, "Cmd '%s' not started yet. Cannot wait it",
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)) {
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);
614 kill(rctx->pid, SIGTERM);
616 kill(rctx->pid, SIGKILL);
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);
625 /* xbt_os_mutex_release(rctx->interruption);
626 if (rctx->interrupted)
628 xbt_os_mutex_acquire(rctx->interruption); */
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);
635 /* Check for broken pipe */
636 if (rctx->brokenpipe)
637 VERB0("Warning: Child did not consume all its input (I got broken pipe)");
639 /* Check for timeouts */
641 if (rctx->output_got->data[0])
642 INFO2("<%s> Output on timeout:\n%s",
643 rctx->filepos, rctx->output_got->data);
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);
655 DEBUG2("RCTX=%p (pid=%d)", rctx, rctx->pid);
656 DEBUG3("Status(%s|%d)=%d", rctx->cmd, rctx->pid, rctx->status);
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;
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;
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);
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);
689 ERROR3("Test suite `%s': NOK (<%s> returned code %d)",
690 testsuite_name, rctx->filepos, WEXITSTATUS(rctx->status));
691 errcode = 40 + WEXITSTATUS(rctx->status);
694 rctx->expected_return = 0;
696 if (rctx->expected_signal) {
697 free(rctx->expected_signal);
698 rctx->expected_signal = NULL;
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');
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';
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||");
721 INFO2("Output of <%s> so far: \n||%s", rctx->filepos, 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)) {
728 xbt_str_diff(rctx->output_wanted->data, rctx->output_got->data);
729 ERROR2("Output of <%s> mismatch:\n%s", rctx->filepos, diff);
732 ERROR2("Test suite `%s': NOK (<%s> output mismatch)",
733 testsuite_name, rctx->filepos);
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||");
742 INFO1("Here is the (ignored) command output: \n||%s", out);
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)
751 xbt_os_mutex_release(armageddon_mutex);
754 if (!rctx->interrupted) {
755 rctx_armageddon(rctx, errcode);
756 xbt_os_mutex_release(rctx->interruption);
761 xbt_os_mutex_release(rctx->interruption);