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>
16 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
18 xbt_dynar_t bg_jobs = NULL;
19 rctx_t armageddon_initiator = NULL;
20 xbt_os_mutex_t armageddon_mutex = NULL;
26 static void kill_it(void*r) {
27 rctx_t rctx = *(rctx_t*)r;
29 VERB2("Join thread %p which were running background cmd <%s>",rctx->runner,rctx->filepos);
30 xbt_os_thread_join(rctx->runner,NULL);
34 void rctx_init(void) {
35 bg_jobs = xbt_dynar_new_sync(sizeof(rctx_t),kill_it);
36 armageddon_mutex = xbt_os_mutex_init();
37 armageddon_initiator = NULL;
40 void rctx_exit(void) {
42 /* Do not use xbt_dynar_free or it will lock the dynar, preventing armageddon from working */
43 while (xbt_dynar_length(bg_jobs)) {
45 xbt_dynar_pop(bg_jobs,&rctx);
48 xbt_dynar_free(&bg_jobs);
50 xbt_os_mutex_destroy(armageddon_mutex);
53 void rctx_wait_bg(void) {
55 /* Do not use xbt_dynar_free or it will lock the dynar, preventing armageddon from working */
56 while (xbt_dynar_length(bg_jobs)) {
58 xbt_dynar_pop(bg_jobs,&rctx);
61 xbt_dynar_free(&bg_jobs);
63 bg_jobs = xbt_dynar_new_sync(sizeof(rctx_t),kill_it);
66 void rctx_armageddon(rctx_t initiator, int exitcode) {
69 DEBUG2("Armageddon request by <%s> (exit=%d)",initiator->filepos,exitcode);
70 xbt_os_mutex_acquire(armageddon_mutex);
71 if (armageddon_initiator != NULL) {
72 VERB0("Armageddon already started. Let it go");
73 xbt_os_mutex_release(initiator->interruption);
74 xbt_os_mutex_release(armageddon_mutex);
77 DEBUG1("Armageddon request by <%s> got the lock. Let's go amok",initiator->filepos);
78 armageddon_initiator = initiator;
79 xbt_os_mutex_release(armageddon_mutex);
81 /* Kill any background commands */
82 while (xbt_dynar_length(bg_jobs)) {
83 xbt_dynar_pop(bg_jobs,&rctx);
84 if (rctx != initiator) {
85 INFO2("Kill <%s> because <%s> failed",rctx->filepos,initiator->filepos);
86 xbt_os_mutex_acquire(rctx->interruption);
87 rctx->interrupted = 1;
88 xbt_os_mutex_release(rctx->interruption);
89 if (!rctx->reader_done) {
90 kill(rctx->pid,SIGTERM);
92 kill(rctx->pid,SIGKILL);
97 VERB0("Shut everything down!");
106 /* under darwin, the environment gets added to the process at startup time. So, it's not defined at library link time, forcing us to extra tricks */
107 # include <crt_externs.h>
108 # define environ (*_NSGetEnviron())
110 /* the environment, as specified by the opengroup, used to initialize the process properties */
111 extern char **environ;
114 void rctx_empty(rctx_t rc) {
116 char **env_it=environ;
126 for (i=0;*env_it;i++,env_it++);
129 rc->env = malloc(i*sizeof(char*));
130 memcpy(rc->env,environ,i*sizeof(char*));
134 rc->is_background = 0;
135 rc->is_stoppable = 0;
136 rc->output = e_output_check;
140 xbt_strbuff_empty(rc->input);
141 xbt_strbuff_empty(rc->output_wanted);
142 xbt_strbuff_empty(rc->output_got);
147 rctx_t res = xbt_new0(s_rctx_t,1);
149 res->input=xbt_strbuff_new();
150 res->output_wanted=xbt_strbuff_new();
151 res->output_got=xbt_strbuff_new();
152 res->interruption = xbt_os_mutex_init();
157 void rctx_free(rctx_t rctx) {
158 DEBUG1("RCTX: Free %p", rctx);
159 rctx_dump(rctx,"free");
169 xbt_os_mutex_destroy(rctx->interruption);
170 xbt_strbuff_free(rctx->input);
171 xbt_strbuff_free(rctx->output_got);
172 xbt_strbuff_free(rctx->output_wanted);
176 void rctx_dump(rctx_t rctx, const char *str) {
177 DEBUG9("%s RCTX %p={in%p={%d,%10s}, want={%d,%10s}, out={%d,%10s}}",
179 rctx->input, rctx->input->used, rctx->input->data,
180 rctx->output_wanted->used,rctx->output_wanted->data,
181 rctx->output_got->used, rctx->output_got->data);
182 DEBUG5("%s RCTX %p=[cmd%p=%10s, pid=%d]",
183 str,rctx,rctx->cmd,rctx->cmd,rctx->pid);
188 * Getting instructions from the file
191 void rctx_pushline(const char* filepos, char kind, char *line) {
197 if (!rctx->is_empty) {
198 ERROR2("[%s] More than one command in this chunk of lines (previous: %s).\n"
199 " Dunno which input/output belongs to which command.",
201 ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name);
202 rctx_armageddon(rctx,1);
206 VERB1("[%s] More than one command in this chunk of lines",filepos);
209 rctx->is_background = 1;
211 rctx->is_background = 0;
213 rctx->cmd = xbt_strdup(line);
214 rctx->filepos = xbt_strdup(filepos);
215 INFO3("[%s] %s%s",filepos,rctx->cmd,
216 ((rctx->is_background)?" (background command)":""));
222 xbt_strbuff_append(rctx->input,line);
223 xbt_strbuff_append(rctx->input,"\n");
228 xbt_strbuff_append(rctx->output_wanted,line);
229 xbt_strbuff_append(rctx->output_wanted,"\n");
236 if (!strncmp(line,"timeout no",strlen("timeout no"))) {
237 VERB1("[%s] (disable timeout)", filepos);
239 } else if (!strncmp(line,"timeout ",strlen("timeout "))) {
240 timeout_value=atoi(line+strlen("timeout"));
241 VERB2("[%s] (new timeout value: %d)",
242 filepos,timeout_value);
244 } else if (!strncmp(line,"expect signal ",strlen("expect signal "))) {
245 rctx->expected_signal = strdup(line + strlen("expect signal "));
246 xbt_str_trim(rctx->expected_signal," \n");
247 VERB2("[%s] (next command must raise signal %s)",
248 filepos, rctx->expected_signal);
250 } else if (!strncmp(line,"expect return ",strlen("expect return "))) {
251 rctx->expected_return = atoi(line+strlen("expect return "));
252 VERB2("[%s] (next command must return code %d)",
253 filepos, rctx->expected_return);
255 } else if (!strncmp(line,"output ignore",strlen("output ignore"))) {
256 rctx->output = e_output_ignore;
257 VERB1("[%s] (ignore output of next command)", filepos);
259 } else if (!strncmp(line,"output display",strlen("output display"))) {
260 rctx->output = e_output_display;
261 VERB1("[%s] (ignore output of next command)", filepos);
263 } else if (!strncmp(line,"setenv ",strlen("setenv "))) {
264 rctx->env = realloc(rctx->env,++(rctx->env_size)*sizeof(char*));
265 rctx->env[rctx->env_size-2] = xbt_strdup(line+strlen("setenv "));
266 rctx->env[rctx->env_size-1] = NULL;
267 VERB1("[%s] (ignore output of next command)", filepos);
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 /* Start a new child, plug the pipes as expected and fire up the
352 helping threads. Is also waits for the child to end if this is a
353 foreground job, or fire up a thread to wait otherwise. */
355 void rctx_start(void) {
359 VERB2("Start %s %s",rctx->cmd,(rctx->is_background?"(background job)":""));
360 if (pipe(child_in) || pipe(child_out)) {
361 perror("Cannot open the pipes");
362 ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
363 rctx_armageddon(rctx,4);
368 perror("Cannot fork the command");
369 ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
370 rctx_armageddon(rctx,4);
374 if (rctx->pid) { /* father */
376 rctx->child_to = child_in[1];
379 rctx->child_from = child_out[0];
381 if (timeout_value > 0)
382 rctx->end_time = time(NULL) + timeout_value;
386 rctx->reader_done = 0;
387 rctx->reader = xbt_os_thread_create("reader",thread_reader,(void*)rctx);
388 rctx->writer = xbt_os_thread_create("writer",thread_writer,(void*)rctx);
397 dup2(child_out[1],1);
398 dup2(child_out[1],2);
401 execle ("/bin/sh", "sh", "-c", rctx->cmd, NULL, rctx->env);
404 rctx->is_stoppable = 1;
406 if (!rctx->is_background) {
409 /* Damn. Copy the rctx and launch a thread to handle it */
411 xbt_os_thread_t runner;
414 DEBUG2("RCTX: new bg=%p, new fg=%p",old,rctx);
416 DEBUG2("Launch a thread to wait for %s %d",old->cmd,old->pid);
417 runner = xbt_os_thread_create(old->cmd,rctx_wait,(void*)old);
418 old->runner = runner;
419 VERB3("Launched thread %p to wait for %s %d",
420 runner,old->cmd, old->pid);
421 xbt_dynar_push(bg_jobs,&old);
425 /* Waits for the child to end (or to timeout), and check its
426 ending conditions. This is launched from rctx_start but either in main
427 thread (for foreground jobs) or in a separate one for background jobs.
428 That explains the prototype, forced by xbt_os_thread_create. */
430 void *rctx_wait(void* r) {
431 rctx_t rctx = (rctx_t)r;
433 int now = time(NULL);
435 rctx_dump(rctx,"wait");
437 if (!rctx->is_stoppable)
438 THROW1(unknown_error,0,"Cmd '%s' not started yet. Cannot wait it",
441 /* Wait for the child to die or the timeout to happen (or an armageddon to happen) */
442 while (!rctx->interrupted && !rctx->reader_done && (rctx->end_time <0 ||rctx->end_time >= now)) {
447 xbt_os_mutex_acquire(rctx->interruption);
448 if (!rctx->interrupted && rctx->end_time > 0 && rctx->end_time < now) {
449 INFO1("<%s> timeouted. Kill the process.",rctx->filepos);
451 kill(rctx->pid,SIGTERM);
453 kill(rctx->pid,SIGKILL);
454 rctx->reader_done = 1;
457 /* Make sure helper threads die.
458 Cannot block since they wait for the child we just killed
459 if not already dead. */
460 xbt_os_thread_join(rctx->writer,NULL);
461 xbt_os_thread_join(rctx->reader,NULL);
463 /* xbt_os_mutex_release(rctx->interruption);
464 if (rctx->interrupted)
466 xbt_os_mutex_acquire(rctx->interruption);*/
468 xbt_strbuff_chomp(rctx->output_got);
469 xbt_strbuff_chomp(rctx->output_wanted);
470 xbt_strbuff_trim(rctx->output_got);
471 xbt_strbuff_trim(rctx->output_wanted);
473 /* Check for broken pipe */
474 if (rctx->brokenpipe)
475 VERB0("Warning: Child did not consume all its input (I got broken pipe)");
477 /* Check for timeouts */
479 if (rctx->output_got->data[0])
480 INFO2("<%s> Output on timeout:\n%s",
481 rctx->filepos,rctx->output_got->data);
483 INFO1("<%s> No output before timeout",
485 ERROR3("Test suite `%s': NOK (<%s> timeout after %d sec)",
486 testsuite_name,rctx->filepos,timeout_value);
487 DEBUG2("<%s> Interrupted = %d", rctx->filepos, rctx->interrupted);
488 if (!rctx->interrupted) {
489 rctx_armageddon(rctx, 3);
494 DEBUG2("RCTX=%p (pid=%d)",rctx,rctx->pid);
495 DEBUG3("Status(%s|%d)=%d",rctx->cmd,rctx->pid,rctx->status);
497 if (!rctx->interrupted) {
498 if (WIFSIGNALED(rctx->status) && !rctx->expected_signal) {
499 ERROR3("Test suite `%s': NOK (<%s> got signal %s)",
500 testsuite_name, rctx->filepos,
501 signal_name(WTERMSIG(rctx->status),NULL));
502 errcode = WTERMSIG(rctx->status)+4;
505 if (WIFSIGNALED(rctx->status) && rctx->expected_signal &&
506 strcmp(signal_name(WTERMSIG(rctx->status),rctx->expected_signal),
507 rctx->expected_signal)) {
508 ERROR4("Test suite `%s': NOK (%s got signal %s instead of %s)",
509 testsuite_name, rctx->filepos,
510 signal_name(WTERMSIG(rctx->status),rctx->expected_signal),
511 rctx->expected_signal);
512 errcode = WTERMSIG(rctx->status)+4;
515 if (!WIFSIGNALED(rctx->status) && rctx->expected_signal) {
516 ERROR3("Test suite `%s': NOK (child %s expected signal %s)",
517 testsuite_name, rctx->filepos,
518 rctx->expected_signal);
522 if (WIFEXITED(rctx->status) && WEXITSTATUS(rctx->status) != rctx->expected_return ) {
523 if (rctx->expected_return)
524 ERROR4("Test suite `%s': NOK (<%s> returned code %d instead of %d)",
525 testsuite_name, rctx->filepos,
526 WEXITSTATUS(rctx->status), rctx->expected_return);
528 ERROR3("Test suite `%s': NOK (<%s> returned code %d)",
529 testsuite_name, rctx->filepos, WEXITSTATUS(rctx->status));
530 errcode = 40+WEXITSTATUS(rctx->status);
533 rctx->expected_return = 0;
535 if(rctx->expected_signal){
536 free(rctx->expected_signal);
537 rctx->expected_signal = NULL;
541 if ( rctx->output == e_output_check
542 && ( rctx->output_got->used != rctx->output_wanted->used
543 || strcmp(rctx->output_got->data, rctx->output_wanted->data))) {
544 if (XBT_LOG_ISENABLED(tesh,xbt_log_priority_info)) {
545 char *diff= xbt_str_diff(rctx->output_wanted->data,rctx->output_got->data);
546 ERROR2("Output of <%s> mismatch:\n%s",rctx->filepos,diff);
549 ERROR2("Test suite `%s': NOK (<%s> output mismatch)",
550 testsuite_name,rctx->filepos);
553 } else if (rctx->output == e_output_ignore) {
554 INFO1("(ignoring the output of <%s> as requested)",rctx->filepos);
555 } else if (rctx->output == e_output_display) {
556 xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
557 char *out = xbt_str_join(a,"\n||");
559 INFO1("Here is the (ignored) command output: \n||%s",out);
561 } else if (errcode || rctx->interrupted) {
562 /* checking output, and matching */
563 xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
564 char *out = xbt_str_join(a,"\n||");
566 INFO2("Output of <%s> so far: \n||%s",rctx->filepos,out);
570 if (!rctx->is_background) {
574 if (!rctx->interrupted) {
575 rctx_armageddon(rctx, errcode);
580 xbt_os_mutex_release(rctx->interruption);