X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/4c04d8355923f8323a1a3195fe3b73ac08b984ea..22c8f349c3475e4bee6df083cc1871f45da2aecb:/tools/tesh/run_context.c diff --git a/tools/tesh/run_context.c b/tools/tesh/run_context.c index 59f787e187..d8e21f3afd 100644 --- a/tools/tesh/run_context.c +++ b/tools/tesh/run_context.c @@ -8,6 +8,7 @@ #include "tesh.h" +#include #include #include #include @@ -15,94 +16,165 @@ XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh); +int fg_job = 0; xbt_dynar_t bg_jobs = NULL; rctx_t armageddon_initiator = NULL; xbt_os_mutex_t armageddon_mutex = NULL; +struct { + int num; + struct sigaction act; +} oldact[3]; /* SIGINT, SIGQUIT, SIGTERM */ + +xbt_os_thread_t sigwaiter_thread; +xbt_os_mutex_t sigwaiter_mutex; +xbt_os_cond_t sigwaiter_cond; +int armageddon_requested = 0; +int caught_signum = 0; /* * Module management */ -static void kill_it(void *r) +static void armageddon_sighandler(int signum) { - rctx_t rctx = *(rctx_t *) r; + xbt_os_mutex_acquire(sigwaiter_mutex); + caught_signum = signum; + armageddon_requested = 1; + xbt_os_cond_signal(sigwaiter_cond); + xbt_os_mutex_release(sigwaiter_mutex); +} + +static void *armageddon_sigwaiter(_XBT_GNUC_UNUSED void *arg) +{ + xbt_os_mutex_acquire(sigwaiter_mutex); + /* Inform main thread that it started. */ + xbt_os_cond_signal(sigwaiter_cond); + /* Wait for ending signal... */ + xbt_os_cond_wait(sigwaiter_cond, sigwaiter_mutex); + if (armageddon_requested) { + ERROR2("Test suite `%s': caught signal %d", testsuite_name, caught_signum); + rctx_armageddon(rctx, 3); + } + xbt_os_mutex_release(sigwaiter_mutex); + return NULL; +} - VERB2("Join thread %p which were running background cmd <%s>", rctx->runner, - rctx->filepos); +static void wait_it(rctx_t rctx) +{ + VERB2("Join thread %p which were running background cmd <%s>", + rctx->runner, rctx->filepos); xbt_os_thread_join(rctx->runner, NULL); +} + +static void kill_it(void *r) +{ + rctx_t rctx = *(rctx_t *) r; + wait_it(rctx); rctx_free(rctx); } void rctx_init(void) { + struct sigaction newact; + int i; + fg_job = 0; bg_jobs = xbt_dynar_new_sync(sizeof(rctx_t), kill_it); armageddon_mutex = xbt_os_mutex_init(); armageddon_initiator = NULL; + sigwaiter_mutex = xbt_os_mutex_init(); + sigwaiter_cond = xbt_os_cond_init(); + xbt_os_mutex_acquire(sigwaiter_mutex); + sigwaiter_thread = xbt_os_thread_create("Armaggedon request waiter", + armageddon_sigwaiter, NULL); + /* Wait for thread to start... */ + xbt_os_cond_wait(sigwaiter_cond, sigwaiter_mutex); + xbt_os_mutex_release(sigwaiter_mutex); + memset(&newact, 0, sizeof(newact)); + newact.sa_handler = armageddon_sighandler; + oldact[0].num = SIGINT; + oldact[1].num = SIGQUIT; + oldact[2].num = SIGTERM; + for (i = 0; i < 3; i++) + sigaction(oldact[i].num, &newact, &oldact[i].act); } void rctx_exit(void) { - if (bg_jobs) { - /* Do not use xbt_dynar_free or it will lock the dynar, preventing armageddon from working */ - while (xbt_dynar_length(bg_jobs)) { - rctx_t rctx; - xbt_dynar_pop(bg_jobs, &rctx); - kill_it(&rctx); - } - xbt_dynar_free(&bg_jobs); - } + int i; + for (i = 0; i < 3; i++) + sigaction(oldact[i].num, &oldact[i].act, NULL); + xbt_os_cond_signal(sigwaiter_cond); + xbt_os_thread_join(sigwaiter_thread, NULL); + xbt_dynar_free(&bg_jobs); + xbt_os_cond_destroy(sigwaiter_cond); + xbt_os_mutex_destroy(sigwaiter_mutex); xbt_os_mutex_destroy(armageddon_mutex); } void rctx_wait_bg(void) { - if (bg_jobs) { - /* Do not use xbt_dynar_free or it will lock the dynar, preventing armageddon from working */ - while (xbt_dynar_length(bg_jobs)) { - rctx_t rctx; - xbt_dynar_pop(bg_jobs, &rctx); - kill_it(&rctx); + /* Do not use xbt_dynar_free or it will lock the dynar, preventing armageddon + * from working */ + while (xbt_dynar_length(bg_jobs)) { + rctx_t rctx = xbt_dynar_getlast_as(bg_jobs, rctx_t); + wait_it(rctx); + xbt_dynar_pop(bg_jobs, &rctx); + rctx_free(rctx); + } + xbt_dynar_reset(bg_jobs); +} + +static void rctx_armageddon_kill_one(rctx_t initiator, const char *filepos, + rctx_t rctx) +{ + if (rctx != initiator) { + INFO2("Kill <%s> because <%s> failed", rctx->filepos, filepos); + xbt_os_mutex_acquire(rctx->interruption); + if (!rctx->reader_done) { + rctx->interrupted = 1; + kill(rctx->pid, SIGTERM); + usleep(100); + kill(rctx->pid, SIGKILL); } - xbt_dynar_free(&bg_jobs); + xbt_os_mutex_release(rctx->interruption); } - bg_jobs = xbt_dynar_new_sync(sizeof(rctx_t), kill_it); } void rctx_armageddon(rctx_t initiator, int exitcode) { - rctx_t rctx; + unsigned int cursor; + rctx_t job; + const char *filepos = initiator && initiator->filepos ? + initiator->filepos : "(master)"; - DEBUG2("Armageddon request by <%s> (exit=%d)", initiator->filepos, - exitcode); + DEBUG2("Armageddon request by <%s> (exit=%d)", filepos, exitcode); xbt_os_mutex_acquire(armageddon_mutex); if (armageddon_initiator != NULL) { VERB0("Armageddon already started. Let it go"); - xbt_os_mutex_release(initiator->interruption); xbt_os_mutex_release(armageddon_mutex); return; } DEBUG1("Armageddon request by <%s> got the lock. Let's go amok", - initiator->filepos); + filepos); armageddon_initiator = initiator; xbt_os_mutex_release(armageddon_mutex); + /* Kill foreground command */ + if (fg_job) + rctx_armageddon_kill_one(initiator, filepos, rctx); + /* Kill any background commands */ - while (xbt_dynar_length(bg_jobs)) { - xbt_dynar_pop(bg_jobs, &rctx); - if (rctx != initiator) { - INFO2("Kill <%s> because <%s> failed", rctx->filepos, - initiator->filepos); - xbt_os_mutex_acquire(rctx->interruption); - rctx->interrupted = 1; - xbt_os_mutex_release(rctx->interruption); - if (!rctx->reader_done) { - kill(rctx->pid, SIGTERM); - usleep(100); - kill(rctx->pid, SIGKILL); - } - } + xbt_dynar_foreach(bg_jobs, cursor, job) { + rctx_armageddon_kill_one(initiator, filepos, job); } + /* Give runner threads a chance to acknowledge the processes deaths */ + usleep(10000); + /* Ensure that nobody is running rctx_wait on exit */ + if (fg_job) + xbt_os_mutex_acquire(rctx->interruption); + xbt_dynar_foreach(bg_jobs, cursor, job) + xbt_os_mutex_acquire(job->interruption); VERB0("Shut everything down!"); exit(exitcode); } @@ -114,23 +186,27 @@ void rctx_armageddon(rctx_t initiator, int exitcode) void rctx_empty(rctx_t rc) { int i; - char **env_it = environ; + char **env_it; + void *filepos; if (rc->cmd) free(rc->cmd); rc->cmd = NULL; - if (rc->filepos) - free(rc->filepos); - if (rc->env) + /* avoid race with rctx_armageddon log messages */ + filepos = rc->filepos; + rc->filepos = NULL; + if (filepos) + free(filepos); + for (i = 0, env_it = environ; *env_it; i++, env_it++); + if (rc->env) { + for (env_it = rctx->env + i; *env_it; env_it++) + free(*env_it); free(rc->env); + } + rc->env_size = i + 1; + rc->env = malloc(rc->env_size * sizeof(char *)); + memcpy(rc->env, environ, rc->env_size * sizeof(char *)); - for (i = 0; *env_it; i++, env_it++); - i++; - rc->env_size = i; - rc->env = malloc(i * sizeof(char *)); - memcpy(rc->env, environ, i * sizeof(char *)); - - rc->filepos = NULL; rc->is_empty = 1; rc->is_background = 0; rc->is_stoppable = 0; @@ -167,8 +243,14 @@ void rctx_free(rctx_t rctx) free(rctx->cmd); if (rctx->filepos) free(rctx->filepos); - if (rctx->env) + if (rctx->env) { + int i; + char **env_it; + for (i = 0, env_it = environ; *env_it; i++, env_it++); + for (env_it = rctx->env + i; *env_it; env_it++) + free(*env_it); free(rctx->env); + } xbt_os_mutex_destroy(rctx->interruption); xbt_strbuff_free(rctx->input); xbt_strbuff_free(rctx->output_got); @@ -201,9 +283,9 @@ void rctx_pushline(const char *filepos, char kind, char *line) if (rctx->cmd) { if (!rctx->is_empty) { ERROR2 - ("[%s] More than one command in this chunk of lines (previous: %s).\n" - " Cannot guess which input/output belongs to which command.", - filepos, rctx->cmd); + ("[%s] More than one command in this chunk of lines (previous: %s).\n" + " Cannot guess which input/output belongs to which command.", + filepos, rctx->cmd); ERROR1("Test suite `%s': NOK (syntax error)", testsuite_name); rctx_armageddon(rctx, 1); return; @@ -270,6 +352,7 @@ void rctx_pushline(const char *filepos, char kind, char *line) char *eq = strchr(line + len, '='); char *key = bprintf("%.*s", (int) (eq - line - len), line + len); xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f); + free(key); rctx->env = realloc(rctx->env, ++(rctx->env_size) * sizeof(char *)); rctx->env[rctx->env_size - 2] = xbt_strdup(line + len); @@ -301,8 +384,8 @@ static void *thread_writer(void *r) int got; DEBUG1("Still %d chars to write", rctx->input->used - posw); got = - write(rctx->child_to, rctx->input->data + posw, - rctx->input->used - posw); + write(rctx->child_to, rctx->input->data + posw, + rctx->input->used - posw); if (got > 0) posw += got; if (got < 0) { @@ -353,8 +436,10 @@ static void *thread_reader(void *r) /* let this thread wait for the child so that the main thread can detect the timeout without blocking on the wait */ got_pid = waitpid(rctx->pid, &rctx->status, 0); if (got_pid != rctx->pid) { - perror(bprintf("(%s) Cannot wait for the child %s (got pid %d where pid %d were expected;rctx=%p;status=%d)", - xbt_thread_self_name(), rctx->cmd, (int)got_pid, (int)rctx->pid,rctx,rctx->status)); + perror(bprintf + ("(%s) Cannot wait for the child %s (got pid %d where pid %d were expected;status=%d)", + xbt_thread_self_name(), rctx->cmd, (int) got_pid, + (int) rctx->pid, rctx->status)); ERROR1("Test suite `%s': NOK (system error)", testsuite_name); rctx_armageddon(rctx, 4); return NULL; @@ -364,42 +449,57 @@ static void *thread_reader(void *r) return NULL; } -/* Special command: mkfile is a building creating a file with the input data as content */ +/* Special command: mkfile is a built-in creating a file with the input data as content */ static void rctx_mkfile(void) { char *filename = xbt_strdup(rctx->cmd + strlen("mkfile ")); FILE *OUT; + int err; xbt_str_trim(filename, NULL); OUT = fopen(filename, "w"); if (!OUT) { - free(filename); THROW3(system_error, errno, "%s: Cannot create file %s: %s", rctx->filepos, filename, strerror(errno)); } - fprintf(OUT, "%s", rctx->input->data); - fclose(OUT); + err = (fprintf(OUT, "%s", rctx->input->data) < 0); + err = (fclose(OUT) == -1) || err; + if (err) { + THROW3(system_error, errno, "%s: Cannot write file %s: %s", + rctx->filepos, filename, strerror(errno)); + } + free(filename); } /* function to be called from the child to start the actual process */ static void start_command(rctx_t rctx) { - xbt_dynar_t cmd = xbt_str_split_quoted(rctx->cmd); + xbt_dynar_t cmd; char *binary_name = NULL; unsigned int it; char *str; - xbt_dynar_get_cpy(cmd, 0, &binary_name); - char **args = xbt_new(char *, xbt_dynar_length(cmd) + 1); + char **args; int errcode; if (!strncmp(rctx->cmd, "mkfile ", strlen("mkfile "))) { rctx_mkfile(); + /* Valgrind detects memory leaks here. + * To correct those leaks, we must free objects allocated in main() or in + * handle_suite(), but we have no more reference to them at this point. + * A quick and dirty hack to make valgrind happy it to uncomment the + * following line. + */ + /* execlp("true", "true", (const char *)0); */ exit(0); /* end the working child */ } + cmd = xbt_str_split_quoted(rctx->cmd); + xbt_dynar_get_cpy(cmd, 0, &binary_name); + args = xbt_new(char *, xbt_dynar_length(cmd) + 1); xbt_dynar_foreach(cmd, it, str) { args[it] = xbt_strdup(str); } args[it] = NULL; + xbt_dynar_free_container(&cmd); /* To search for the right executable path when not trivial */ struct stat stat_buf; @@ -456,9 +556,16 @@ void rctx_start(void) rctx->cmd = xbt_str_varsubst(rctx->cmd, env); VERB2("Start %s %s", rctx->cmd, (rctx->is_background ? "(background job)" : "")); + xbt_os_mutex_acquire(armageddon_mutex); + if (armageddon_initiator) { + VERB0("Armageddon in progress. Do not start job."); + xbt_os_mutex_release(armageddon_mutex); + return; + } if (pipe(child_in) || pipe(child_out)) { perror("Cannot open the pipes"); ERROR1("Test suite `%s': NOK (system error)", testsuite_name); + xbt_os_mutex_release(armageddon_mutex); rctx_armageddon(rctx, 4); } @@ -466,6 +573,7 @@ void rctx_start(void) if (rctx->pid < 0) { perror("Cannot fork the command"); ERROR1("Test suite `%s': NOK (system error)", testsuite_name); + xbt_os_mutex_release(armageddon_mutex); rctx_armageddon(rctx, 4); return; } @@ -484,12 +592,11 @@ void rctx_start(void) rctx->reader_done = 0; rctx->reader = - xbt_os_thread_create("reader", thread_reader, (void *) rctx); + xbt_os_thread_create("reader", thread_reader, (void *) rctx); rctx->writer = - xbt_os_thread_create("writer", thread_writer, (void *) rctx); + xbt_os_thread_create("writer", thread_writer, (void *) rctx); } else { /* child */ - close(child_in[1]); dup2(child_in[0], 0); close(child_in[0]); @@ -505,7 +612,10 @@ void rctx_start(void) rctx->is_stoppable = 1; if (!rctx->is_background) { + fg_job = 1; + xbt_os_mutex_release(armageddon_mutex); rctx_wait(rctx); + fg_job = 0; } else { /* Damn. Copy the rctx and launch a thread to handle it */ rctx_t old = rctx; @@ -517,8 +627,10 @@ void rctx_start(void) DEBUG2("Launch a thread to wait for %s %d", old->cmd, old->pid); runner = xbt_os_thread_create(old->cmd, rctx_wait, (void *) old); old->runner = runner; - VERB3("Launched thread %p to wait for %s %d", runner, old->cmd, old->pid); + VERB3("Launched thread %p to wait for %s %d", runner, old->cmd, + old->pid); xbt_dynar_push(bg_jobs, &old); + xbt_os_mutex_release(armageddon_mutex); } } @@ -540,7 +652,7 @@ void *rctx_wait(void *r) rctx->cmd); /* Wait for the child to die or the timeout to happen (or an armageddon to happen) */ - while (!rctx->interrupted && !rctx->reader_done + while (!rctx->reader_done && (rctx->end_time < 0 || rctx->end_time >= now)) { usleep(100); now = time(NULL); @@ -553,7 +665,6 @@ void *rctx_wait(void *r) kill(rctx->pid, SIGTERM); usleep(100); kill(rctx->pid, SIGKILL); - rctx->reader_done = 1; } /* Make sure helper threads die. @@ -574,7 +685,8 @@ void *rctx_wait(void *r) /* Check for broken pipe */ if (rctx->brokenpipe) - VERB0("Warning: Child did not consume all its input (I got broken pipe)"); + VERB0 + ("Warning: Child did not consume all its input (I got broken pipe)"); /* Check for timeouts */ if (rctx->timeout) { @@ -587,6 +699,7 @@ void *rctx_wait(void *r) testsuite_name, rctx->filepos, timeout_value); DEBUG2("<%s> Interrupted = %d", rctx->filepos, rctx->interrupted); if (!rctx->interrupted) { + xbt_os_mutex_release(rctx->interruption); rctx_armageddon(rctx, 3); return NULL; } @@ -622,9 +735,10 @@ void *rctx_wait(void *r) if (WIFEXITED(rctx->status) && WEXITSTATUS(rctx->status) != rctx->expected_return) { if (rctx->expected_return) - ERROR4("Test suite `%s': NOK (<%s> returned code %d instead of %d)", - testsuite_name, rctx->filepos, - WEXITSTATUS(rctx->status), rctx->expected_return); + ERROR4 + ("Test suite `%s': NOK (<%s> returned code %d instead of %d)", + testsuite_name, rctx->filepos, WEXITSTATUS(rctx->status), + rctx->expected_return); else ERROR3("Test suite `%s': NOK (<%s> returned code %d)", testsuite_name, rctx->filepos, WEXITSTATUS(rctx->status)); @@ -653,12 +767,20 @@ void *rctx_wait(void *r) errcode = 1; } - if (rctx->output == e_output_check - && (rctx->output_got->used != rctx->output_wanted->used - || strcmp(rctx->output_got->data, rctx->output_wanted->data))) { + if ((errcode && errcode != 1) || rctx->interrupted) { + /* checking output, and matching */ + xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n"); + char *out = xbt_str_join(a, "\n||"); + xbt_dynar_free(&a); + INFO2("Output of <%s> so far: \n||%s", rctx->filepos, out); + free(out); + } else if (rctx->output == e_output_check + && (rctx->output_got->used != rctx->output_wanted->used + || strcmp(rctx->output_got->data, + rctx->output_wanted->data))) { if (XBT_LOG_ISENABLED(tesh, xbt_log_priority_info)) { char *diff = - xbt_str_diff(rctx->output_wanted->data, rctx->output_got->data); + xbt_str_diff(rctx->output_wanted->data, rctx->output_got->data); ERROR2("Output of <%s> mismatch:\n%s", rctx->filepos, diff); free(diff); } @@ -674,20 +796,18 @@ void *rctx_wait(void *r) xbt_dynar_free(&a); INFO1("Here is the (ignored) command output: \n||%s", out); free(out); - } else if ((errcode && errcode != 1) || rctx->interrupted) { - /* checking output, and matching */ - xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n"); - char *out = xbt_str_join(a, "\n||"); - xbt_dynar_free(&a); - INFO2("Output of <%s> so far: \n||%s", rctx->filepos, out); - free(out); } if (!rctx->is_background) { - rctx_empty(rctx); + xbt_os_mutex_acquire(armageddon_mutex); + /* Don't touch rctx if armageddon is in progress. */ + if (!armageddon_initiator) + rctx_empty(rctx); + xbt_os_mutex_release(armageddon_mutex); } if (errcode) { if (!rctx->interrupted) { + xbt_os_mutex_release(rctx->interruption); rctx_armageddon(rctx, errcode); return NULL; }