X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/49db0fab32b65bf2224aa5d0334320e64a276787..e9b0dc96439383147b02491962e8ccf8cfdeff6b:/tools/tesh/run_context.c diff --git a/tools/tesh/run_context.c b/tools/tesh/run_context.c index b73b5fb1b5..36547ccd11 100644 --- a/tools/tesh/run_context.c +++ b/tools/tesh/run_context.c @@ -13,32 +13,89 @@ #include #include - XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh); xbt_dynar_t bg_jobs = NULL; +rctx_t armageddon_initiator = NULL; +xbt_os_mutex_t armageddon_mutex = NULL; /* * Module management */ -static void join_it(void*t) { - xbt_thread_t th = *(xbt_thread_t*)t; - VERB1("Join thread %p which were running a background cmd",th); - xbt_thread_join(th,NULL); +static void kill_it(void*r) { + rctx_t rctx = *(rctx_t*)r; + + VERB2("Join thread %p which were running background cmd <%s>",rctx->runner,rctx->filepos); + xbt_os_thread_join(rctx->runner,NULL); + rctx_free(rctx); } void rctx_init(void) { - bg_jobs = xbt_dynar_new(sizeof(xbt_thread_t),join_it); + bg_jobs = xbt_dynar_new_sync(sizeof(rctx_t),kill_it); + armageddon_mutex = xbt_os_mutex_init(); + armageddon_initiator = NULL; } void rctx_exit(void) { - xbt_dynar_free(&bg_jobs); + 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); + } + xbt_os_mutex_destroy(armageddon_mutex); } void rctx_wait_bg(void) { - xbt_dynar_free(&bg_jobs); - bg_jobs = xbt_dynar_new(sizeof(xbt_thread_t),join_it); + 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); + } + bg_jobs = xbt_dynar_new_sync(sizeof(rctx_t),kill_it); +} + +void rctx_armageddon(rctx_t initiator, int exitcode) { + rctx_t rctx; + + DEBUG2("Armageddon request by <%s> (exit=%d)",initiator->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); + armageddon_initiator = initiator; + xbt_os_mutex_release(armageddon_mutex); + + /* 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); + } + } + } + + VERB0("Shut everything down!"); + exit(exitcode); } /* @@ -49,23 +106,28 @@ void rctx_empty(rctx_t rc) { if (rc->cmd) free(rc->cmd); rc->cmd = NULL; + if (rc->filepos) + free(rc->filepos); + rc->filepos = NULL; rc->is_empty = 1; rc->is_background = 0; rc->is_stoppable = 0; rc->output = e_output_check; rc->brokenpipe = 0; rc->timeout = 0; - buff_empty(rc->input); - buff_empty(rc->output_wanted); - buff_empty(rc->output_got); + rc->interrupted = 0; + xbt_strbuff_empty(rc->input); + xbt_strbuff_empty(rc->output_wanted); + xbt_strbuff_empty(rc->output_got); } rctx_t rctx_new() { rctx_t res = xbt_new0(s_rctx_t,1); - res->input=buff_new(); - res->output_wanted=buff_new(); - res->output_got=buff_new(); + res->input=xbt_strbuff_new(); + res->output_wanted=xbt_strbuff_new(); + res->output_got=xbt_strbuff_new(); + res->interruption = xbt_os_mutex_init(); rctx_empty(res); return res; } @@ -78,9 +140,12 @@ void rctx_free(rctx_t rctx) { if (rctx->cmd) free(rctx->cmd); - buff_free(rctx->input); - buff_free(rctx->output_got); - buff_free(rctx->output_wanted); + if (rctx->filepos) + free(rctx->filepos); + xbt_os_mutex_destroy(rctx->interruption); + xbt_strbuff_free(rctx->input); + xbt_strbuff_free(rctx->output_got); + xbt_strbuff_free(rctx->output_wanted); free(rctx); } @@ -110,7 +175,8 @@ void rctx_pushline(const char* filepos, char kind, char *line) { " Dunno which input/output belongs to which command.", filepos,rctx->cmd); ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name); - exit(1); + rctx_armageddon(rctx,1); + return; } rctx_start(); VERB1("[%s] More than one command in this chunk of lines",filepos); @@ -121,29 +187,33 @@ void rctx_pushline(const char* filepos, char kind, char *line) { rctx->is_background = 0; rctx->cmd = xbt_strdup(line); - INFO3("[%s] %s%s",filepos,line, + rctx->filepos = xbt_strdup(filepos); + INFO3("[%s] %s%s",filepos,rctx->cmd, ((rctx->is_background)?" (background command)":"")); break; case '<': rctx->is_empty = 0; - buff_append(rctx->input,line); - buff_append(rctx->input,"\n"); + xbt_strbuff_append(rctx->input,line); + xbt_strbuff_append(rctx->input,"\n"); break; case '>': rctx->is_empty = 0; - buff_append(rctx->output_wanted,line); - buff_append(rctx->output_wanted,"\n"); + xbt_strbuff_append(rctx->output_wanted,line); + xbt_strbuff_append(rctx->output_wanted,"\n"); break; case '!': if (rctx->cmd) rctx_start(); - if (!strncmp(line,"set timeout ",strlen("set timeout "))) { - timeout_value=atoi(line+strlen("set timeout")); + if (!strncmp(line,"timeout no",strlen("timeout no"))) { + VERB1("[%s] (disable timeout)", filepos); + timeout_value = -1; + } else if (!strncmp(line,"timeout ",strlen("timeout "))) { + timeout_value=atoi(line+strlen("timeout")); VERB2("[%s] (new timeout value: %d)", filepos,timeout_value); @@ -169,7 +239,8 @@ void rctx_pushline(const char* filepos, char kind, char *line) { } else { ERROR2("%s: Malformed metacommand: %s",filepos,line); ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name); - exit(1); + rctx_armageddon(rctx,1); + return; } break; } @@ -197,7 +268,8 @@ static void* thread_writer(void *r) { } else if (errno!=EINTR && errno!=EAGAIN && errno!=EPIPE) { perror("Error while writing input to child"); ERROR1("Test suite `%s': NOK (system error)",testsuite_name); - exit(4); + rctx_armageddon(rctx,4); + return NULL; } } DEBUG1("written %d chars so far",posw); @@ -221,11 +293,12 @@ static void *thread_reader(void *r) { if (posr<0 && errno!=EINTR && errno!=EAGAIN) { perror("Error while reading output of child"); ERROR1("Test suite `%s': NOK (system error)", testsuite_name); - exit(4); + rctx_armageddon(rctx,4); + return NULL; } if (posr>0) { buffout[posr]='\0'; - buff_append(rctx->output_got,buffout); + xbt_strbuff_append(rctx->output_got,buffout); } else { usleep(100); } @@ -237,7 +310,8 @@ static void *thread_reader(void *r) { if (got_pid != rctx->pid) { perror(bprintf("Cannot wait for the child %s",rctx->cmd)); ERROR1("Test suite `%s': NOK (system error)", testsuite_name); - exit(4); + rctx_armageddon(rctx,4); + return NULL; } rctx->reader_done = 1; @@ -256,14 +330,15 @@ void rctx_start(void) { if (pipe(child_in) || pipe(child_out)) { perror("Cannot open the pipes"); ERROR1("Test suite `%s': NOK (system error)", testsuite_name); - exit(4); + rctx_armageddon(rctx,4); } rctx->pid=fork(); if (rctx->pid<0) { perror("Cannot fork the command"); ERROR1("Test suite `%s': NOK (system error)", testsuite_name); - exit(4); + rctx_armageddon(rctx,4); + return; } if (rctx->pid) { /* father */ @@ -273,11 +348,14 @@ void rctx_start(void) { close(child_out[1]); rctx->child_from = child_out[0]; - rctx->end_time = time(NULL) + timeout_value; + if (timeout_value > 0) + rctx->end_time = time(NULL) + timeout_value; + else + rctx->end_time = -1; rctx->reader_done = 0; - rctx->reader = xbt_thread_create(thread_reader,(void*)rctx); - rctx->writer = xbt_thread_create(thread_writer,(void*)rctx); + rctx->reader = xbt_os_thread_create("reader",thread_reader,(void*)rctx); + rctx->writer = xbt_os_thread_create("writer",thread_writer,(void*)rctx); } else { /* child */ @@ -300,23 +378,24 @@ void rctx_start(void) { } else { /* Damn. Copy the rctx and launch a thread to handle it */ rctx_t old = rctx; - xbt_thread_t runner; + xbt_os_thread_t runner; rctx = rctx_new(); DEBUG2("RCTX: new bg=%p, new fg=%p",old,rctx); DEBUG2("Launch a thread to wait for %s %d",old->cmd,old->pid); - runner = xbt_thread_create(rctx_wait,(void*)old); + 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); - xbt_dynar_push(bg_jobs,&runner); + xbt_dynar_push(bg_jobs,&old); } } /* Waits for the child to end (or to timeout), and check its ending conditions. This is launched from rctx_start but either in main thread (for foreground jobs) or in a separate one for background jobs. - That explains the prototype, forced by xbt_thread_create. */ + That explains the prototype, forced by xbt_os_thread_create. */ void *rctx_wait(void* r) { rctx_t rctx = (rctx_t)r; @@ -329,26 +408,37 @@ void *rctx_wait(void* r) { THROW1(unknown_error,0,"Cmd '%s' not started yet. Cannot wait it", rctx->cmd); - usleep(100); - /* Wait for the child to die or the timeout to happen */ - while (!rctx->reader_done && rctx->end_time >= now) { + /* Wait for the child to die or the timeout to happen (or an armageddon to happen) */ + while (!rctx->interrupted && !rctx->reader_done && (rctx->end_time <0 ||rctx->end_time >= now)) { usleep(100); now = time(NULL); } - if (rctx->end_time < now) { - INFO1("Child '%s' timeouted. Kill it",rctx->cmd); + xbt_os_mutex_acquire(rctx->interruption); + if (!rctx->interrupted && rctx->end_time > 0 && rctx->end_time < now) { + INFO1("<%s> timeouted. Kill the process.",rctx->filepos); rctx->timeout = 1; kill(rctx->pid,SIGTERM); usleep(100); kill(rctx->pid,SIGKILL); + rctx->reader_done = 1; } /* Make sure helper threads die. Cannot block since they wait for the child we just killed if not already dead. */ - xbt_thread_join(rctx->writer,NULL); - xbt_thread_join(rctx->reader,NULL); + xbt_os_thread_join(rctx->writer,NULL); + xbt_os_thread_join(rctx->reader,NULL); + + /* xbt_os_mutex_release(rctx->interruption); + if (rctx->interrupted) + return NULL; + xbt_os_mutex_acquire(rctx->interruption);*/ + + xbt_strbuff_chomp(rctx->output_got); + xbt_strbuff_chomp(rctx->output_wanted); + xbt_strbuff_trim(rctx->output_got); + xbt_strbuff_trim(rctx->output_wanted); /* Check for broken pipe */ if (rctx->brokenpipe) @@ -356,88 +446,108 @@ void *rctx_wait(void* r) { /* Check for timeouts */ if (rctx->timeout) { - ERROR2("Test suite `%s': NOK (timeout after %d sec)", testsuite_name,timeout_value); - exit(3); + if (rctx->output_got->data[0]) + INFO2("<%s> Output on timeout:\n%s", + rctx->filepos,rctx->output_got->data); + else + INFO1("<%s> No output before timeout", + rctx->filepos); + ERROR3("Test suite `%s': NOK (<%s> timeout after %d sec)", + testsuite_name,rctx->filepos,timeout_value); + DEBUG2("<%s> Interrupted = %d", rctx->filepos, rctx->interrupted); + if (!rctx->interrupted) { + rctx_armageddon(rctx, 3); + return NULL; + } } DEBUG2("RCTX=%p (pid=%d)",rctx,rctx->pid); DEBUG3("Status(%s|%d)=%d",rctx->cmd,rctx->pid,rctx->status); - if (WIFSIGNALED(rctx->status) && !rctx->expected_signal) { - ERROR2("Test suite `%s': NOK (child got signal %s)", - testsuite_name, - signal_name(WTERMSIG(rctx->status),NULL)); - errcode = WTERMSIG(rctx->status)+4; - } - - if (WIFSIGNALED(rctx->status) && rctx->expected_signal && - strcmp(signal_name(WTERMSIG(rctx->status),rctx->expected_signal), - rctx->expected_signal)) { - ERROR3("Test suite `%s': NOK (child got signal %s instead of %s)", testsuite_name, - signal_name(WTERMSIG(rctx->status),rctx->expected_signal), - rctx->expected_signal); - errcode = WTERMSIG(rctx->status)+4; - } - - if (!WIFSIGNALED(rctx->status) && rctx->expected_signal) { - ERROR2("Test suite `%s': NOK (child expected signal %s)", testsuite_name, - rctx->expected_signal); - errcode = 5; - } - - if (WIFEXITED(rctx->status) && WEXITSTATUS(rctx->status) != rctx->expected_return ) { - if (rctx->expected_return) - ERROR3("Test suite `%s': NOK (child returned code %d instead of %d)", testsuite_name, - WEXITSTATUS(rctx->status), rctx->expected_return); - else - ERROR2("Test suite `%s': NOK (child returned code %d)", testsuite_name, WEXITSTATUS(rctx->status)); - errcode = 40+WEXITSTATUS(rctx->status); + if (!rctx->interrupted) { + if (WIFSIGNALED(rctx->status) && !rctx->expected_signal) { + ERROR3("Test suite `%s': NOK (<%s> got signal %s)", + testsuite_name, rctx->filepos, + signal_name(WTERMSIG(rctx->status),NULL)); + errcode = WTERMSIG(rctx->status)+4; + } - } - rctx->expected_return = 0; + if (WIFSIGNALED(rctx->status) && rctx->expected_signal && + strcmp(signal_name(WTERMSIG(rctx->status),rctx->expected_signal), + rctx->expected_signal)) { + ERROR4("Test suite `%s': NOK (%s got signal %s instead of %s)", + testsuite_name, rctx->filepos, + signal_name(WTERMSIG(rctx->status),rctx->expected_signal), + rctx->expected_signal); + errcode = WTERMSIG(rctx->status)+4; + } + + if (!WIFSIGNALED(rctx->status) && rctx->expected_signal) { + ERROR3("Test suite `%s': NOK (child %s expected signal %s)", + testsuite_name, rctx->filepos, + rctx->expected_signal); + errcode = 5; + } + + 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); + else + ERROR3("Test suite `%s': NOK (<%s> returned code %d)", + testsuite_name, rctx->filepos, WEXITSTATUS(rctx->status)); + errcode = 40+WEXITSTATUS(rctx->status); + + } + rctx->expected_return = 0; - if(rctx->expected_signal){ - free(rctx->expected_signal); - rctx->expected_signal = NULL; + if(rctx->expected_signal){ + free(rctx->expected_signal); + rctx->expected_signal = NULL; + } } - buff_chomp(rctx->output_got); - buff_chomp(rctx->output_wanted); - buff_trim(rctx->output_got); - buff_trim(rctx->output_wanted); - 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); - ERROR1("Output mismatch:\n%s", - diff); + ERROR2("Output of <%s> mismatch:\n%s",rctx->filepos,diff); free(diff); } - ERROR1("Test suite `%s': NOK (output mismatch)", testsuite_name); + ERROR2("Test suite `%s': NOK (<%s> output mismatch)", + testsuite_name,rctx->filepos); errcode=2; } else if (rctx->output == e_output_ignore) { - INFO0("(ignoring the output as requested)"); + INFO1("(ignoring the output of <%s> as requested)",rctx->filepos); } else if (rctx->output == e_output_display) { xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n"); char *out = xbt_str_join(a,"\n||"); xbt_dynar_free(&a); INFO1("Here is the (ignored) command output: \n||%s",out); free(out); + } else if (errcode || 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_free(rctx); - else + if (!rctx->is_background) { rctx_empty(rctx); + } if (errcode) { - if (rctx->output == e_output_check) - INFO1("Here is the child's output:\n%s",rctx->output_got->data); - exit (errcode); + if (!rctx->interrupted) { + rctx_armageddon(rctx, errcode); + return NULL; + } } + xbt_os_mutex_release(rctx->interruption); return NULL; }