3 /* run_context -- stuff in which TESH runs a command */
5 /* Copyright (c) 2007 Martin Quinson. */
6 /* All rights reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
13 #include <sys/types.h>
18 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;
28 static void kill_it(void*r) {
29 rctx_t rctx = *(rctx_t*)r;
31 VERB2("Join thread %p which were running background cmd <%s>",rctx->runner,rctx->filepos);
32 xbt_os_thread_join(rctx->runner,NULL);
36 void rctx_init(void) {
37 bg_jobs = xbt_dynar_new_sync(sizeof(rctx_t),kill_it);
38 armageddon_mutex = xbt_os_mutex_init();
39 armageddon_initiator = NULL;
42 void rctx_exit(void) {
44 /* Do not use xbt_dynar_free or it will lock the dynar, preventing armageddon from working */
45 while (xbt_dynar_length(bg_jobs)) {
47 xbt_dynar_pop(bg_jobs,&rctx);
50 xbt_dynar_free(&bg_jobs);
52 xbt_os_mutex_destroy(armageddon_mutex);
55 void rctx_wait_bg(void) {
57 /* Do not use xbt_dynar_free or it will lock the dynar, preventing armageddon from working */
58 while (xbt_dynar_length(bg_jobs)) {
60 xbt_dynar_pop(bg_jobs,&rctx);
63 xbt_dynar_free(&bg_jobs);
65 bg_jobs = xbt_dynar_new_sync(sizeof(rctx_t),kill_it);
68 void rctx_armageddon(rctx_t initiator, int exitcode) {
71 DEBUG2("Armageddon request by <%s> (exit=%d)",initiator->filepos,exitcode);
72 xbt_os_mutex_acquire(armageddon_mutex);
73 if (armageddon_initiator != NULL) {
74 VERB0("Armageddon already started. Let it go");
75 xbt_os_mutex_release(initiator->interruption);
76 xbt_os_mutex_release(armageddon_mutex);
79 DEBUG1("Armageddon request by <%s> got the lock. Let's go amok",initiator->filepos);
80 armageddon_initiator = initiator;
81 xbt_os_mutex_release(armageddon_mutex);
83 /* Kill any background commands */
84 while (xbt_dynar_length(bg_jobs)) {
85 xbt_dynar_pop(bg_jobs,&rctx);
86 if (rctx != initiator) {
87 INFO2("Kill <%s> because <%s> failed",rctx->filepos,initiator->filepos);
88 xbt_os_mutex_acquire(rctx->interruption);
89 rctx->interrupted = 1;
90 xbt_os_mutex_release(rctx->interruption);
91 if (!rctx->reader_done) {
92 kill(rctx->pid,SIGTERM);
94 kill(rctx->pid,SIGKILL);
99 VERB0("Shut everything down!");
107 void rctx_empty(rctx_t rc) {
109 char **env_it=environ;
119 for (i=0;*env_it;i++,env_it++);
122 rc->env = malloc(i*sizeof(char*));
123 memcpy(rc->env,environ,i*sizeof(char*));
127 rc->is_background = 0;
128 rc->is_stoppable = 0;
129 rc->output = e_output_check;
133 xbt_strbuff_empty(rc->input);
134 xbt_strbuff_empty(rc->output_wanted);
135 xbt_strbuff_empty(rc->output_got);
140 rctx_t res = xbt_new0(s_rctx_t,1);
142 res->input=xbt_strbuff_new();
143 res->output_wanted=xbt_strbuff_new();
144 res->output_got=xbt_strbuff_new();
145 res->interruption = xbt_os_mutex_init();
150 void rctx_free(rctx_t rctx) {
151 DEBUG1("RCTX: Free %p", rctx);
152 rctx_dump(rctx,"free");
162 xbt_os_mutex_destroy(rctx->interruption);
163 xbt_strbuff_free(rctx->input);
164 xbt_strbuff_free(rctx->output_got);
165 xbt_strbuff_free(rctx->output_wanted);
169 void rctx_dump(rctx_t rctx, const char *str) {
170 DEBUG9("%s RCTX %p={in%p={%d,%10s}, want={%d,%10s}, out={%d,%10s}}",
172 rctx->input, rctx->input->used, rctx->input->data,
173 rctx->output_wanted->used,rctx->output_wanted->data,
174 rctx->output_got->used, rctx->output_got->data);
175 DEBUG5("%s RCTX %p=[cmd%p=%10s, pid=%d]",
176 str,rctx,rctx->cmd,rctx->cmd,rctx->pid);
181 * Getting instructions from the file
184 void rctx_pushline(const char* filepos, char kind, char *line) {
190 if (!rctx->is_empty) {
191 ERROR2("[%s] More than one command in this chunk of lines (previous: %s).\n"
192 " Dunno which input/output belongs to which command.",
194 ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name);
195 rctx_armageddon(rctx,1);
199 VERB1("[%s] More than one command in this chunk of lines",filepos);
202 rctx->is_background = 1;
204 rctx->is_background = 0;
206 rctx->cmd = xbt_strdup(line);
207 rctx->filepos = xbt_strdup(filepos);
208 INFO3("[%s] %s%s",filepos,rctx->cmd,
209 ((rctx->is_background)?" (background command)":""));
215 xbt_strbuff_append(rctx->input,line);
216 xbt_strbuff_append(rctx->input,"\n");
221 xbt_strbuff_append(rctx->output_wanted,line);
222 xbt_strbuff_append(rctx->output_wanted,"\n");
229 if (!strncmp(line,"timeout no",strlen("timeout no"))) {
230 VERB1("[%s] (disable timeout)", filepos);
232 } else if (!strncmp(line,"timeout ",strlen("timeout "))) {
233 timeout_value=atoi(line+strlen("timeout"));
234 VERB2("[%s] (new timeout value: %d)",
235 filepos,timeout_value);
237 } else if (!strncmp(line,"expect signal ",strlen("expect signal "))) {
238 rctx->expected_signal = strdup(line + strlen("expect signal "));
239 xbt_str_trim(rctx->expected_signal," \n");
240 VERB2("[%s] (next command must raise signal %s)",
241 filepos, rctx->expected_signal);
243 } else if (!strncmp(line,"expect return ",strlen("expect return "))) {
244 rctx->expected_return = atoi(line+strlen("expect return "));
245 VERB2("[%s] (next command must return code %d)",
246 filepos, rctx->expected_return);
248 } else if (!strncmp(line,"output ignore",strlen("output ignore"))) {
249 rctx->output = e_output_ignore;
250 VERB1("[%s] (ignore output of next command)", filepos);
252 } else if (!strncmp(line,"output display",strlen("output display"))) {
253 rctx->output = e_output_display;
254 VERB1("[%s] (ignore output of next command)", filepos);
256 } else if (!strncmp(line,"setenv ",strlen("setenv "))) {
257 int len = strlen("setenv ");
258 char *eq = strchr(line+len,'=');
259 char *key = bprintf("%.*s",(int)(eq-line-len),line+len);
260 xbt_dict_set(env,key,xbt_strdup(eq+1),xbt_free_f);
262 rctx->env = realloc(rctx->env,++(rctx->env_size)*sizeof(char*));
263 rctx->env[rctx->env_size-2] = xbt_strdup(line+len);
264 rctx->env[rctx->env_size-1] = NULL;
265 VERB2("[%s] setenv %s", filepos,line+len);
267 // } else if (!strncmp(line,"file ",strlen("file "))) {
270 ERROR2("%s: Malformed metacommand: %s",filepos,line);
271 ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name);
272 rctx_armageddon(rctx,1);
280 * Actually doing the job
283 /* The IO of the childs are handled by the two following threads
284 (one pair per child) */
286 static void* thread_writer(void *r) {
288 rctx_t rctx = (rctx_t)r;
289 for (posw=0; posw<rctx->input->used && !rctx->brokenpipe; ) {
291 DEBUG1("Still %d chars to write",rctx->input->used-posw);
292 got=write(rctx->child_to,rctx->input->data+posw,rctx->input->used-posw);
296 if (errno == EPIPE) {
297 rctx->brokenpipe = 1;
298 } else if (errno!=EINTR && errno!=EAGAIN && errno!=EPIPE) {
299 perror("Error while writing input to child");
300 ERROR1("Test suite `%s': NOK (system error)",testsuite_name);
301 rctx_armageddon(rctx,4);
305 DEBUG1("written %d chars so far",posw);
310 rctx->input->data[0]='\0';
312 close(rctx->child_to);
316 static void *thread_reader(void *r) {
317 rctx_t rctx = (rctx_t)r;
318 char *buffout=malloc(4096);
322 posr=read(rctx->child_from,buffout,4095);
323 if (posr<0 && errno!=EINTR && errno!=EAGAIN) {
324 perror("Error while reading output of child");
325 ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
326 rctx_armageddon(rctx,4);
331 xbt_strbuff_append(rctx->output_got,buffout);
335 } while (!rctx->timeout && posr!=0);
338 /* let this thread wait for the child so that the main thread can detect the timeout without blocking on the wait */
339 got_pid = waitpid(rctx->pid,&rctx->status,0);
340 if (got_pid != rctx->pid) {
341 perror(bprintf("Cannot wait for the child %s",rctx->cmd));
342 ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
343 rctx_armageddon(rctx,4);
347 rctx->reader_done = 1;
351 /* function to be called from the child to start the actual process */
352 static void start_command(rctx_t rctx){
353 xbt_dynar_t cmd = xbt_str_split_quoted(rctx->cmd);
354 char *binary_name = NULL;
357 xbt_dynar_get_cpy(cmd,0,&binary_name);
358 char **args = xbt_new(char*,xbt_dynar_length(cmd)+1);
360 xbt_dynar_foreach(cmd,it,str) {
361 args[it] = xbt_strdup(str);
365 /* To search for the right executable path when not trivial */
366 struct stat stat_buf;
368 /* build the command line */
369 if (stat(binary_name, &stat_buf)) {
370 /* Damn. binary not in current dir. We'll have to dig the PATH to find it */
373 for (i = 0; environ[i]; i++) {
374 if (!strncmp("PATH=", environ[i], 5)) {
375 xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
377 xbt_dynar_foreach(path, it, str) {
380 binary_name = bprintf("%s/%s", str, args[0]);
381 if (!stat(binary_name, &stat_buf)) {
383 DEBUG1("Looked in the PATH for the binary. Found %s",
385 xbt_dynar_free(&path);
389 xbt_dynar_free(&path);
390 if (stat(binary_name, &stat_buf)) {
392 ERROR1("Command %s not found",args[0]);
399 binary_name = xbt_strdup(args[0]);
402 execve(binary_name, args, rctx->env);
405 /* Start a new child, plug the pipes as expected and fire up the
406 helping threads. Is also waits for the child to end if this is a
407 foreground job, or fire up a thread to wait otherwise. */
408 void rctx_start(void) {
412 DEBUG1("Cmd before rewriting %s",rctx->cmd);
413 rctx->cmd = xbt_str_varsubst(rctx->cmd,env);
414 VERB2("Start %s %s",rctx->cmd,(rctx->is_background?"(background job)":""));
415 if (pipe(child_in) || pipe(child_out)) {
416 perror("Cannot open the pipes");
417 ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
418 rctx_armageddon(rctx,4);
423 perror("Cannot fork the command");
424 ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
425 rctx_armageddon(rctx,4);
429 if (rctx->pid) { /* father */
431 rctx->child_to = child_in[1];
434 rctx->child_from = child_out[0];
436 if (timeout_value > 0)
437 rctx->end_time = time(NULL) + timeout_value;
441 rctx->reader_done = 0;
442 rctx->reader = xbt_os_thread_create("reader",thread_reader,(void*)rctx);
443 rctx->writer = xbt_os_thread_create("writer",thread_writer,(void*)rctx);
452 dup2(child_out[1],1);
453 dup2(child_out[1],2);
459 rctx->is_stoppable = 1;
461 if (!rctx->is_background) {
464 /* Damn. Copy the rctx and launch a thread to handle it */
466 xbt_os_thread_t runner;
469 DEBUG2("RCTX: new bg=%p, new fg=%p",old,rctx);
471 DEBUG2("Launch a thread to wait for %s %d",old->cmd,old->pid);
472 runner = xbt_os_thread_create(old->cmd,rctx_wait,(void*)old);
473 old->runner = runner;
474 VERB3("Launched thread %p to wait for %s %d",
475 runner,old->cmd, old->pid);
476 xbt_dynar_push(bg_jobs,&old);
480 /* Waits for the child to end (or to timeout), and check its
481 ending conditions. This is launched from rctx_start but either in main
482 thread (for foreground jobs) or in a separate one for background jobs.
483 That explains the prototype, forced by xbt_os_thread_create. */
485 void *rctx_wait(void* r) {
486 rctx_t rctx = (rctx_t)r;
488 int now = time(NULL);
490 rctx_dump(rctx,"wait");
492 if (!rctx->is_stoppable)
493 THROW1(unknown_error,0,"Cmd '%s' not started yet. Cannot wait it",
496 /* Wait for the child to die or the timeout to happen (or an armageddon to happen) */
497 while (!rctx->interrupted && !rctx->reader_done && (rctx->end_time <0 ||rctx->end_time >= now)) {
502 xbt_os_mutex_acquire(rctx->interruption);
503 if (!rctx->interrupted && rctx->end_time > 0 && rctx->end_time < now) {
504 INFO1("<%s> timeouted. Kill the process.",rctx->filepos);
506 kill(rctx->pid,SIGTERM);
508 kill(rctx->pid,SIGKILL);
509 rctx->reader_done = 1;
512 /* Make sure helper threads die.
513 Cannot block since they wait for the child we just killed
514 if not already dead. */
515 xbt_os_thread_join(rctx->writer,NULL);
516 xbt_os_thread_join(rctx->reader,NULL);
518 /* xbt_os_mutex_release(rctx->interruption);
519 if (rctx->interrupted)
521 xbt_os_mutex_acquire(rctx->interruption);*/
523 xbt_strbuff_chomp(rctx->output_got);
524 xbt_strbuff_chomp(rctx->output_wanted);
525 xbt_strbuff_trim(rctx->output_got);
526 xbt_strbuff_trim(rctx->output_wanted);
528 /* Check for broken pipe */
529 if (rctx->brokenpipe)
530 VERB0("Warning: Child did not consume all its input (I got broken pipe)");
532 /* Check for timeouts */
534 if (rctx->output_got->data[0])
535 INFO2("<%s> Output on timeout:\n%s",
536 rctx->filepos,rctx->output_got->data);
538 INFO1("<%s> No output before timeout",
540 ERROR3("Test suite `%s': NOK (<%s> timeout after %d sec)",
541 testsuite_name,rctx->filepos,timeout_value);
542 DEBUG2("<%s> Interrupted = %d", rctx->filepos, rctx->interrupted);
543 if (!rctx->interrupted) {
544 rctx_armageddon(rctx, 3);
549 DEBUG2("RCTX=%p (pid=%d)",rctx,rctx->pid);
550 DEBUG3("Status(%s|%d)=%d",rctx->cmd,rctx->pid,rctx->status);
552 if (!rctx->interrupted) {
553 if (WIFSIGNALED(rctx->status) && !rctx->expected_signal) {
554 ERROR3("Test suite `%s': NOK (<%s> got signal %s)",
555 testsuite_name, rctx->filepos,
556 signal_name(WTERMSIG(rctx->status),NULL));
557 errcode = WTERMSIG(rctx->status)+4;
560 if (WIFSIGNALED(rctx->status) && rctx->expected_signal &&
561 strcmp(signal_name(WTERMSIG(rctx->status),rctx->expected_signal),
562 rctx->expected_signal)) {
563 ERROR4("Test suite `%s': NOK (%s got signal %s instead of %s)",
564 testsuite_name, rctx->filepos,
565 signal_name(WTERMSIG(rctx->status),rctx->expected_signal),
566 rctx->expected_signal);
567 errcode = WTERMSIG(rctx->status)+4;
570 if (!WIFSIGNALED(rctx->status) && rctx->expected_signal) {
571 ERROR3("Test suite `%s': NOK (child %s expected signal %s)",
572 testsuite_name, rctx->filepos,
573 rctx->expected_signal);
577 if (WIFEXITED(rctx->status) && WEXITSTATUS(rctx->status) != rctx->expected_return ) {
578 if (rctx->expected_return)
579 ERROR4("Test suite `%s': NOK (<%s> returned code %d instead of %d)",
580 testsuite_name, rctx->filepos,
581 WEXITSTATUS(rctx->status), rctx->expected_return);
583 ERROR3("Test suite `%s': NOK (<%s> returned code %d)",
584 testsuite_name, rctx->filepos, WEXITSTATUS(rctx->status));
585 errcode = 40+WEXITSTATUS(rctx->status);
588 rctx->expected_return = 0;
590 if(rctx->expected_signal){
591 free(rctx->expected_signal);
592 rctx->expected_signal = NULL;
596 if ( rctx->output == e_output_check
597 && ( rctx->output_got->used != rctx->output_wanted->used
598 || strcmp(rctx->output_got->data, rctx->output_wanted->data))) {
599 if (XBT_LOG_ISENABLED(tesh,xbt_log_priority_info)) {
600 char *diff= xbt_str_diff(rctx->output_wanted->data,rctx->output_got->data);
601 ERROR2("Output of <%s> mismatch:\n%s",rctx->filepos,diff);
604 ERROR2("Test suite `%s': NOK (<%s> output mismatch)",
605 testsuite_name,rctx->filepos);
608 } else if (rctx->output == e_output_ignore) {
609 INFO1("(ignoring the output of <%s> as requested)",rctx->filepos);
610 } else if (rctx->output == e_output_display) {
611 xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
612 char *out = xbt_str_join(a,"\n||");
614 INFO1("Here is the (ignored) command output: \n||%s",out);
616 } else if (errcode || rctx->interrupted) {
617 /* checking output, and matching */
618 xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
619 char *out = xbt_str_join(a,"\n||");
621 INFO2("Output of <%s> so far: \n||%s",rctx->filepos,out);
625 if (!rctx->is_background) {
629 if (!rctx->interrupted) {
630 rctx_armageddon(rctx, errcode);
635 xbt_os_mutex_release(rctx->interruption);