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!");
105 void rctx_empty(rctx_t rc) {
107 char **env_it=environ;
117 for (i=0;*env_it;i++,env_it++);
120 rc->env = malloc(i*sizeof(char*));
121 memcpy(rc->env,environ,i*sizeof(char*));
125 rc->is_background = 0;
126 rc->is_stoppable = 0;
127 rc->output = e_output_check;
131 xbt_strbuff_empty(rc->input);
132 xbt_strbuff_empty(rc->output_wanted);
133 xbt_strbuff_empty(rc->output_got);
136 /* the environment, as specified by the opengroup */
137 extern char **environ;
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 rctx->env = realloc(rctx->env,++(rctx->env_size)*sizeof(char*));
258 rctx->env[rctx->env_size-2] = xbt_strdup(line+strlen("setenv "));
259 rctx->env[rctx->env_size-1] = NULL;
260 VERB1("[%s] (ignore output of next command)", filepos);
263 ERROR2("%s: Malformed metacommand: %s",filepos,line);
264 ERROR1("Test suite `%s': NOK (syntax error)",testsuite_name);
265 rctx_armageddon(rctx,1);
273 * Actually doing the job
276 /* The IO of the childs are handled by the two following threads
277 (one pair per child) */
279 static void* thread_writer(void *r) {
281 rctx_t rctx = (rctx_t)r;
282 for (posw=0; posw<rctx->input->used && !rctx->brokenpipe; ) {
284 DEBUG1("Still %d chars to write",rctx->input->used-posw);
285 got=write(rctx->child_to,rctx->input->data+posw,rctx->input->used-posw);
289 if (errno == EPIPE) {
290 rctx->brokenpipe = 1;
291 } else if (errno!=EINTR && errno!=EAGAIN && errno!=EPIPE) {
292 perror("Error while writing input to child");
293 ERROR1("Test suite `%s': NOK (system error)",testsuite_name);
294 rctx_armageddon(rctx,4);
298 DEBUG1("written %d chars so far",posw);
303 rctx->input->data[0]='\0';
305 close(rctx->child_to);
309 static void *thread_reader(void *r) {
310 rctx_t rctx = (rctx_t)r;
311 char *buffout=malloc(4096);
315 posr=read(rctx->child_from,buffout,4095);
316 if (posr<0 && errno!=EINTR && errno!=EAGAIN) {
317 perror("Error while reading output of child");
318 ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
319 rctx_armageddon(rctx,4);
324 xbt_strbuff_append(rctx->output_got,buffout);
328 } while (!rctx->timeout && posr!=0);
331 /* let this thread wait for the child so that the main thread can detect the timeout without blocking on the wait */
332 got_pid = waitpid(rctx->pid,&rctx->status,0);
333 if (got_pid != rctx->pid) {
334 perror(bprintf("Cannot wait for the child %s",rctx->cmd));
335 ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
336 rctx_armageddon(rctx,4);
340 rctx->reader_done = 1;
344 /* Start a new child, plug the pipes as expected and fire up the
345 helping threads. Is also waits for the child to end if this is a
346 foreground job, or fire up a thread to wait otherwise. */
348 void rctx_start(void) {
352 VERB2("Start %s %s",rctx->cmd,(rctx->is_background?"(background job)":""));
353 if (pipe(child_in) || pipe(child_out)) {
354 perror("Cannot open the pipes");
355 ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
356 rctx_armageddon(rctx,4);
361 perror("Cannot fork the command");
362 ERROR1("Test suite `%s': NOK (system error)", testsuite_name);
363 rctx_armageddon(rctx,4);
367 if (rctx->pid) { /* father */
369 rctx->child_to = child_in[1];
372 rctx->child_from = child_out[0];
374 if (timeout_value > 0)
375 rctx->end_time = time(NULL) + timeout_value;
379 rctx->reader_done = 0;
380 rctx->reader = xbt_os_thread_create("reader",thread_reader,(void*)rctx);
381 rctx->writer = xbt_os_thread_create("writer",thread_writer,(void*)rctx);
390 dup2(child_out[1],1);
391 dup2(child_out[1],2);
394 execle ("/bin/sh", "sh", "-c", rctx->cmd, NULL, rctx->env);
397 rctx->is_stoppable = 1;
399 if (!rctx->is_background) {
402 /* Damn. Copy the rctx and launch a thread to handle it */
404 xbt_os_thread_t runner;
407 DEBUG2("RCTX: new bg=%p, new fg=%p",old,rctx);
409 DEBUG2("Launch a thread to wait for %s %d",old->cmd,old->pid);
410 runner = xbt_os_thread_create(old->cmd,rctx_wait,(void*)old);
411 old->runner = runner;
412 VERB3("Launched thread %p to wait for %s %d",
413 runner,old->cmd, old->pid);
414 xbt_dynar_push(bg_jobs,&old);
418 /* Waits for the child to end (or to timeout), and check its
419 ending conditions. This is launched from rctx_start but either in main
420 thread (for foreground jobs) or in a separate one for background jobs.
421 That explains the prototype, forced by xbt_os_thread_create. */
423 void *rctx_wait(void* r) {
424 rctx_t rctx = (rctx_t)r;
426 int now = time(NULL);
428 rctx_dump(rctx,"wait");
430 if (!rctx->is_stoppable)
431 THROW1(unknown_error,0,"Cmd '%s' not started yet. Cannot wait it",
434 /* Wait for the child to die or the timeout to happen (or an armageddon to happen) */
435 while (!rctx->interrupted && !rctx->reader_done && (rctx->end_time <0 ||rctx->end_time >= now)) {
440 xbt_os_mutex_acquire(rctx->interruption);
441 if (!rctx->interrupted && rctx->end_time > 0 && rctx->end_time < now) {
442 INFO1("<%s> timeouted. Kill the process.",rctx->filepos);
444 kill(rctx->pid,SIGTERM);
446 kill(rctx->pid,SIGKILL);
447 rctx->reader_done = 1;
450 /* Make sure helper threads die.
451 Cannot block since they wait for the child we just killed
452 if not already dead. */
453 xbt_os_thread_join(rctx->writer,NULL);
454 xbt_os_thread_join(rctx->reader,NULL);
456 /* xbt_os_mutex_release(rctx->interruption);
457 if (rctx->interrupted)
459 xbt_os_mutex_acquire(rctx->interruption);*/
461 xbt_strbuff_chomp(rctx->output_got);
462 xbt_strbuff_chomp(rctx->output_wanted);
463 xbt_strbuff_trim(rctx->output_got);
464 xbt_strbuff_trim(rctx->output_wanted);
466 /* Check for broken pipe */
467 if (rctx->brokenpipe)
468 VERB0("Warning: Child did not consume all its input (I got broken pipe)");
470 /* Check for timeouts */
472 if (rctx->output_got->data[0])
473 INFO2("<%s> Output on timeout:\n%s",
474 rctx->filepos,rctx->output_got->data);
476 INFO1("<%s> No output before timeout",
478 ERROR3("Test suite `%s': NOK (<%s> timeout after %d sec)",
479 testsuite_name,rctx->filepos,timeout_value);
480 DEBUG2("<%s> Interrupted = %d", rctx->filepos, rctx->interrupted);
481 if (!rctx->interrupted) {
482 rctx_armageddon(rctx, 3);
487 DEBUG2("RCTX=%p (pid=%d)",rctx,rctx->pid);
488 DEBUG3("Status(%s|%d)=%d",rctx->cmd,rctx->pid,rctx->status);
490 if (!rctx->interrupted) {
491 if (WIFSIGNALED(rctx->status) && !rctx->expected_signal) {
492 ERROR3("Test suite `%s': NOK (<%s> got signal %s)",
493 testsuite_name, rctx->filepos,
494 signal_name(WTERMSIG(rctx->status),NULL));
495 errcode = WTERMSIG(rctx->status)+4;
498 if (WIFSIGNALED(rctx->status) && rctx->expected_signal &&
499 strcmp(signal_name(WTERMSIG(rctx->status),rctx->expected_signal),
500 rctx->expected_signal)) {
501 ERROR4("Test suite `%s': NOK (%s got signal %s instead of %s)",
502 testsuite_name, rctx->filepos,
503 signal_name(WTERMSIG(rctx->status),rctx->expected_signal),
504 rctx->expected_signal);
505 errcode = WTERMSIG(rctx->status)+4;
508 if (!WIFSIGNALED(rctx->status) && rctx->expected_signal) {
509 ERROR3("Test suite `%s': NOK (child %s expected signal %s)",
510 testsuite_name, rctx->filepos,
511 rctx->expected_signal);
515 if (WIFEXITED(rctx->status) && WEXITSTATUS(rctx->status) != rctx->expected_return ) {
516 if (rctx->expected_return)
517 ERROR4("Test suite `%s': NOK (<%s> returned code %d instead of %d)",
518 testsuite_name, rctx->filepos,
519 WEXITSTATUS(rctx->status), rctx->expected_return);
521 ERROR3("Test suite `%s': NOK (<%s> returned code %d)",
522 testsuite_name, rctx->filepos, WEXITSTATUS(rctx->status));
523 errcode = 40+WEXITSTATUS(rctx->status);
526 rctx->expected_return = 0;
528 if(rctx->expected_signal){
529 free(rctx->expected_signal);
530 rctx->expected_signal = NULL;
534 if ( rctx->output == e_output_check
535 && ( rctx->output_got->used != rctx->output_wanted->used
536 || strcmp(rctx->output_got->data, rctx->output_wanted->data))) {
537 if (XBT_LOG_ISENABLED(tesh,xbt_log_priority_info)) {
538 char *diff= xbt_str_diff(rctx->output_wanted->data,rctx->output_got->data);
539 ERROR2("Output of <%s> mismatch:\n%s",rctx->filepos,diff);
542 ERROR2("Test suite `%s': NOK (<%s> output mismatch)",
543 testsuite_name,rctx->filepos);
546 } else if (rctx->output == e_output_ignore) {
547 INFO1("(ignoring the output of <%s> as requested)",rctx->filepos);
548 } else if (rctx->output == e_output_display) {
549 xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
550 char *out = xbt_str_join(a,"\n||");
552 INFO1("Here is the (ignored) command output: \n||%s",out);
554 } else if (errcode || rctx->interrupted) {
555 /* checking output, and matching */
556 xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
557 char *out = xbt_str_join(a,"\n||");
559 INFO2("Output of <%s> so far: \n||%s",rctx->filepos,out);
563 if (!rctx->is_background) {
567 if (!rctx->interrupted) {
568 rctx_armageddon(rctx, errcode);
573 xbt_os_mutex_release(rctx->interruption);