X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/86051ed1b66342bae0e21fd6643e66331ac2c07d..4b70e717ae88822e3985f35e1c8f37bf330976a2:/tools/tesh/run_context.c diff --git a/tools/tesh/run_context.c b/tools/tesh/run_context.c index 3f65821eb4..b234a084ac 100644 --- a/tools/tesh/run_context.c +++ b/tools/tesh/run_context.c @@ -13,32 +13,81 @@ #include #include - XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh); xbt_dynar_t bg_jobs = NULL; +rctx_t armageddon_initiator = NULL; +xbt_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; + + VERB1("Join thread %p which were running a background cmd",rctx->runner); + xbt_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(sizeof(rctx_t),kill_it); + armageddon_mutex = xbt_mutex_init(); + armageddon_initiator = NULL; } void rctx_exit(void) { - xbt_dynar_free(&bg_jobs); + if (bg_jobs) + xbt_dynar_free(&bg_jobs); + xbt_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); + bg_jobs = xbt_dynar_new(sizeof(rctx_t),kill_it); +} + +void rctx_armageddon(rctx_t initiator, int exitcode) { + rctx_t rctx; + int cpt; + + xbt_mutex_lock(armageddon_mutex); + if (armageddon_initiator != NULL) { + VERB0("Armageddon already started. Let it go"); + return; + } + armageddon_initiator = initiator; + xbt_mutex_unlock(armageddon_mutex); + + + /* Kill any background commands */ + xbt_dynar_foreach(bg_jobs,cpt,rctx) { + if (rctx != initiator) { + xbt_mutex_lock(rctx->interruption); + rctx->interrupted = 1; + INFO2("Kill <%s> because <%s> failed",rctx->filepos,initiator->filepos); + if (!rctx->reader_done) { + kill(rctx->pid,SIGTERM); + usleep(100); + kill(rctx->pid,SIGKILL); + } + xbt_mutex_unlock(rctx->interruption); + } + } + + /* Remove myself from the tasks */ + if (xbt_dynar_member(bg_jobs, &initiator)) { + int mypos = xbt_dynar_search(bg_jobs, &initiator); + rctx_t myself; + xbt_dynar_remove_at(bg_jobs,mypos,&myself); + // rctx_free(myself); + } + + /* Cleanup the place */ + xbt_dynar_free(&bg_jobs); + + exit(exitcode); } /* @@ -49,12 +98,16 @@ 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->check_output = 1; + rc->output = e_output_check; rc->brokenpipe = 0; rc->timeout = 0; + rc->interrupted = 0; buff_empty(rc->input); buff_empty(rc->output_wanted); buff_empty(rc->output_got); @@ -66,6 +119,7 @@ rctx_t rctx_new() { res->input=buff_new(); res->output_wanted=buff_new(); res->output_got=buff_new(); + res->interruption = xbt_mutex_init(); rctx_empty(res); return res; } @@ -78,6 +132,9 @@ void rctx_free(rctx_t rctx) { if (rctx->cmd) free(rctx->cmd); + if (rctx->filepos) + free(rctx->filepos); + xbt_mutex_destroy(rctx->interruption); buff_free(rctx->input); buff_free(rctx->output_got); buff_free(rctx->output_wanted); @@ -109,7 +166,8 @@ void rctx_pushline(const char* filepos, char kind, char *line) { ERROR2("[%s] More than one command in this chunk of lines (previous: %s).\n" " Dunno which input/output belongs to which command.", filepos,rctx->cmd); - exit(1); + ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name); + rctx_armageddon(rctx,1); } rctx_start(); VERB1("[%s] More than one command in this chunk of lines",filepos); @@ -120,7 +178,8 @@ 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; @@ -157,13 +216,18 @@ void rctx_pushline(const char* filepos, char kind, char *line) { VERB2("[%s] (next command must return code %d)", filepos, rctx->expected_return); - } else if (!strncmp(line,"ignore output",strlen("ignore output"))) { - rctx->check_output = 0; + } else if (!strncmp(line,"output ignore",strlen("output ignore"))) { + rctx->output = e_output_ignore; + VERB1("[%s] (ignore output of next command)", filepos); + + } else if (!strncmp(line,"output display",strlen("output display"))) { + rctx->output = e_output_display; VERB1("[%s] (ignore output of next command)", filepos); } else { ERROR2("%s: Malformed metacommand: %s",filepos,line); - exit(1); + ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name); + rctx_armageddon(rctx,1); } break; } @@ -190,7 +254,8 @@ static void* thread_writer(void *r) { rctx->brokenpipe = 1; } else if (errno!=EINTR && errno!=EAGAIN && errno!=EPIPE) { perror("Error while writing input to child"); - exit(4); + ERROR1("Test suite `%s': NOK (system error)",testsuite_name); + rctx_armageddon(rctx,4); } } DEBUG1("written %d chars so far",posw); @@ -207,13 +272,14 @@ static void* thread_writer(void *r) { static void *thread_reader(void *r) { rctx_t rctx = (rctx_t)r; char *buffout=malloc(4096); - int posr; + int posr, got_pid; do { posr=read(rctx->child_from,buffout,4095); if (posr<0 && errno!=EINTR && errno!=EAGAIN) { perror("Error while reading output of child"); - exit(4); + ERROR1("Test suite `%s': NOK (system error)", testsuite_name); + rctx_armageddon(rctx,4); } if (posr>0) { buffout[posr]='\0'; @@ -223,6 +289,15 @@ static void *thread_reader(void *r) { } } while (!rctx->timeout && posr!=0); free(buffout); + + /* 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("Cannot wait for the child %s",rctx->cmd)); + ERROR1("Test suite `%s': NOK (system error)", testsuite_name); + rctx_armageddon(rctx,4); + } + rctx->reader_done = 1; return NULL; } @@ -238,13 +313,15 @@ void rctx_start(void) { VERB2("Start %s %s",rctx->cmd,(rctx->is_background?"(background job)":"")); if (pipe(child_in) || pipe(child_out)) { perror("Cannot open the pipes"); - exit(4); + ERROR1("Test suite `%s': NOK (system error)", testsuite_name); + rctx_armageddon(rctx,4); } rctx->pid=fork(); if (rctx->pid<0) { perror("Cannot fork the command"); - exit(4); + ERROR1("Test suite `%s': NOK (system error)", testsuite_name); + rctx_armageddon(rctx,4); } if (rctx->pid) { /* father */ @@ -256,6 +333,7 @@ void rctx_start(void) { rctx->end_time = time(NULL) + timeout_value; + rctx->reader_done = 0; rctx->reader = xbt_thread_create(thread_reader,(void*)rctx); rctx->writer = xbt_thread_create(thread_writer,(void*)rctx); @@ -287,9 +365,10 @@ void rctx_start(void) { DEBUG2("Launch a thread to wait for %s %d",old->cmd,old->pid); runner = xbt_thread_create(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); } } @@ -301,8 +380,7 @@ void rctx_start(void) { void *rctx_wait(void* r) { rctx_t rctx = (rctx_t)r; int errcode = 0; - int res; - int status; + int now = time(NULL); rctx_dump(rctx,"wait"); @@ -310,100 +388,142 @@ void *rctx_wait(void* r) { THROW1(unknown_error,0,"Cmd '%s' not started yet. Cannot wait it", rctx->cmd); - /* Wait for the child to die or the timeout to happen */ - while (!rctx->reader_done && rctx->end_time > time(NULL)) { + /* 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 >= now) { usleep(100); + now = time(NULL); } + + xbt_mutex_lock(rctx->interruption); - if (!rctx->reader_done) { - INFO1("Child '%s' timeouted. Kill it",rctx->cmd); + if (!rctx->interrupted && rctx->end_time < now) { + INFO1("<%s> timeouted. Kill the process.",rctx->filepos); rctx->timeout = 1; - kill(rctx->pid,SIGKILL); + 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_mutex_unlock(rctx->interruption); + if (rctx->interrupted) + return NULL; + xbt_mutex_lock(rctx->interruption);*/ + + buff_chomp(rctx->output_got); + buff_chomp(rctx->output_wanted); + buff_trim(rctx->output_got); + buff_trim(rctx->output_wanted); + /* Check for broken pipe */ if (rctx->brokenpipe) VERB0("Warning: Child did not consume all its input (I got broken pipe)"); /* Check for timeouts */ if (rctx->timeout) { - ERROR1("Child timeouted (waited %d sec)",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); + if (!rctx->interrupted) + rctx_armageddon(rctx, 3); } - DEBUG2("Wait for %s (%d)",rctx->cmd,rctx->pid); - res = waitpid(rctx->pid,&status,0); - if (res != rctx->pid) { - perror(bprintf("Cannot wait for the child %s",rctx->cmd)); - exit(1); - } DEBUG2("RCTX=%p (pid=%d)",rctx,rctx->pid); - DEBUG3("Status(%s|%d)=%d",rctx->cmd,rctx->pid,status); - - if (WIFSIGNALED(status) && !rctx->expected_signal) { - ERROR2("Child \"%s\" got signal %s.", rctx->cmd, - signal_name(WTERMSIG(status),NULL)); - errcode = WTERMSIG(status)+4; - } - - if (WIFSIGNALED(status) && rctx->expected_signal && - strcmp(signal_name(WTERMSIG(status),rctx->expected_signal), - rctx->expected_signal)) { - ERROR3("Child \"%s\" got signal %s instead of signal %s", rctx->cmd, - signal_name(WTERMSIG(status),rctx->expected_signal), - rctx->expected_signal); - errcode = WTERMSIG(status)+4; - } - - if (!WIFSIGNALED(status) && rctx->expected_signal) { - ERROR2("Child \"%s\" didn't got expected signal %s", - rctx->cmd, rctx->expected_signal); - errcode = 5; - } - - if (WIFEXITED(status) && WEXITSTATUS(status) != rctx->expected_return ) { - if (rctx->expected_return) - ERROR3("Child \"%s\" returned code %d instead of %d", rctx->cmd, - WEXITSTATUS(status), rctx->expected_return); - else - ERROR2("Child \"%s\" returned code %d", rctx->cmd, WEXITSTATUS(status)); - errcode = 40+WEXITSTATUS(status); - } - rctx->expected_return = 0; + DEBUG3("Status(%s|%d)=%d",rctx->cmd,rctx->pid,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; + } + + 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->check_output + if ( rctx->output == e_output_check && ( rctx->output_got->used != rctx->output_wanted->used || strcmp(rctx->output_got->data, rctx->output_wanted->data))) { - char *diff= xbt_str_diff(rctx->output_wanted->data,rctx->output_got->data); - ERROR2("Output of child \"%s\" don't match expectations. Here is a diff between expected and got output:\n%s", - rctx->cmd,diff); - free(diff); + 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); + free(diff); + } + ERROR2("Test suite `%s': NOK (<%s> output mismatch)", + testsuite_name,rctx->filepos); + errcode=2; - } else if (!rctx->check_output) { + } else if (rctx->output == e_output_ignore) { INFO0("(ignoring the output as requested)"); + } 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) - exit (errcode); + } + if (errcode) { + if (!rctx->interrupted) + rctx_armageddon(rctx, errcode); + } + xbt_mutex_unlock(rctx->interruption); return NULL; }