/* run_context -- stuff in which TESH runs a command */
-/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
+/* Copyright (c) 2007-2014. The SimGrid Team.
* All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
#include <sys/wait.h>
#include <sys/stat.h>
#include <unistd.h>
+#include <math.h> /* floor */
XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
struct sigaction newact;
int i;
fg_job = 0;
- bg_jobs = xbt_dynar_new_sync(sizeof(rctx_t), kill_it);
+ bg_jobs = xbt_dynar_new(sizeof(rctx_t), kill_it);
armageddon_mutex = xbt_os_mutex_init();
armageddon_initiator = NULL;
sigwaiter_mutex = xbt_os_mutex_init();
sigwaiter_cond = xbt_os_cond_init();
xbt_os_mutex_acquire(sigwaiter_mutex);
- sigwaiter_thread = xbt_os_thread_create("Armaggedon request waiter",
+ sigwaiter_thread = xbt_os_thread_create("Armageddon request waiter",
armageddon_sigwaiter, NULL, NULL);
/* Wait for thread to start... */
xbt_os_cond_wait(sigwaiter_cond, sigwaiter_mutex);
{
/* Do not use xbt_dynar_free or it will lock the dynar, preventing armageddon
* from working */
- while (xbt_dynar_length(bg_jobs)) {
+ while (!xbt_dynar_is_empty(bg_jobs)) {
rctx_t rctx = xbt_dynar_getlast_as(bg_jobs, rctx_t);
wait_it(rctx);
xbt_dynar_pop(bg_jobs, &rctx);
if (!rctx->reader_done) {
rctx->interrupted = 1;
kill(rctx->pid, SIGTERM);
- usleep(100);
+ struct timespec ts;
+ ts.tv_sec = 0;
+ ts.tv_nsec = (100e-6 - floor(100e-6)) * 1e9;
+ nanosleep (&ts, NULL);
kill(rctx->pid, SIGKILL);
}
xbt_os_mutex_release(rctx->interruption);
}
/* Give runner threads a chance to acknowledge the processes deaths */
- usleep(10000);
+ struct timespec ts;
+ ts.tv_sec = 0;
+ ts.tv_nsec = (10000e-6 - floor(10000e-6)) * 1e9;
+ nanosleep (&ts, NULL);
/* Ensure that nobody is running rctx_wait on exit */
if (fg_job)
xbt_os_mutex_acquire(rctx->interruption);
char **env_it;
void *filepos;
- if (rc->cmd)
- free(rc->cmd);
+ free(rc->cmd);
rc->cmd = NULL;
/* avoid race with rctx_armageddon log messages */
filepos = rc->filepos;
rc->filepos = NULL;
- if (filepos)
- free(filepos);
+ free(filepos);
for (i = 0, env_it = environ; *env_it; i++, env_it++);
if (rc->env) {
for (env_it = rctx->env + i; *env_it; env_it++)
if (!rctx)
return;
- if (rctx->cmd)
- free(rctx->cmd);
- if (rctx->filepos)
- free(rctx->filepos);
+ free(rctx->cmd);
+ free(rctx->filepos);
if (rctx->env) {
int i;
char **env_it;
rctx->cmd = xbt_strdup(line);
rctx->filepos = xbt_strdup(filepos);
- if(option){
- rctx->cmd = bprintf("%s %s",rctx->cmd,option);
+ if (option){
+ char *newcmd = bprintf("%s %s", rctx->cmd, option);
+ free(rctx->cmd);
+ rctx->cmd = newcmd;
}
XBT_INFO("[%s] %s%s", filepos, rctx->cmd,
((rctx->is_background) ? " (background command)" : ""));
rctx->is_empty = 0;
xbt_strbuff_append(rctx->output_wanted, line);
xbt_strbuff_append(rctx->output_wanted, "\n");
+ XBT_DEBUG("wanted:%s",rctx->output_wanted->data);
break;
case '!':
int len = strlen("setenv ");
char *eq = strchr(line + len, '=');
char *key = bprintf("%.*s", (int) (eq - line - len), line + len);
- xbt_dict_set(env, key, xbt_strdup(eq + 1), xbt_free_f);
+ xbt_dict_set(env, key, xbt_strdup(eq + 1), NULL);
free(key);
rctx->env = realloc(rctx->env, ++(rctx->env_size) * sizeof(char *));
}
XBT_DEBUG("written %d chars so far", posw);
- if (got <= 0)
- usleep(100);
+ if (got <= 0){
+ struct timespec ts;
+ ts.tv_sec = 0;
+ ts.tv_nsec = (100e-6 - floor(100e-6)) * 1e9;
+ nanosleep (&ts, NULL);
+ }
}
rctx->input->data[0] = '\0';
rctx->input->used = 0;
buffout[posr] = '\0';
xbt_strbuff_append(rctx->output_got, buffout);
} else {
- usleep(100);
+ struct timespec ts;
+ ts.tv_sec = 0;
+ ts.tv_nsec = (100e-6 - floor(100e-6)) * 1e9;
+ nanosleep (&ts, NULL);
}
} while (!rctx->timeout && posr != 0);
free(buffout);
xbt_dynar_t path = xbt_str_split(environ[i] + 5, ":");
xbt_dynar_foreach(path, it, str) {
- if (binary_name)
- free(binary_name);
+ free(binary_name);
binary_name = bprintf("%s/%s", str, args[0]);
if (!stat(binary_name, &stat_buf)) {
/* Found. */
int child_out[2];
XBT_DEBUG("Cmd before rewriting %s", rctx->cmd);
- rctx->cmd = xbt_str_varsubst(rctx->cmd, env);
+ char *newcmd = xbt_str_varsubst(rctx->cmd, env);
+ free(rctx->cmd);
+ rctx->cmd = newcmd;
XBT_VERB("Start %s %s", rctx->cmd,
(rctx->is_background ? "(background job)" : ""));
xbt_os_mutex_acquire(armageddon_mutex);
/* Helper function to sort the output */
static int cmpstringp(const void *p1, const void *p2) {
- /* Sort only using the 19 first chars (date+pid)
- * If the dates are the same, then, sort using pointer address (be stable wrt output of each process)
+ /* Sort only using the sort_len first chars
+ * If they are the same, then, sort using pointer address
+ * (be stable wrt output of each process)
*/
- const char *s1 = *((const char**) p1);
- const char *s2 = *((const char**) p2);
+ const char **s1 = *(const char***)p1;
+ const char **s2 = *(const char***)p2;
- XBT_DEBUG("Compare strings '%s' and '%s'", s1, s2);
+ XBT_DEBUG("Compare strings '%s' and '%s'", *s1, *s2);
- int res = strncmp(s1, s2, sort_len);
+ int res = strncmp(*s1, *s2, sort_len);
if (res == 0)
- return p1>p2;
+ res = s1 > s2 ? 1 : (s1 < s2 ? -1 : 0);
return res;
}
+static void stable_sort(xbt_dynar_t a)
+{
+ unsigned long len = xbt_dynar_length(a);
+ void **b = xbt_new(void*, len);
+ unsigned long i;
+ for (i = 0 ; i < len ; i++) /* fill the array b with pointers to strings */
+ b[i] = xbt_dynar_get_ptr(a, i);
+ qsort(b, len, sizeof *b, cmpstringp); /* sort it */
+ for (i = 0 ; i < len ; i++) /* dereference the pointers to get the strings */
+ b[i] = *(char**)b[i];
+ for (i = 0 ; i < len ; i++) /* put everything in place */
+ xbt_dynar_set_as(a, i, char*, b[i]);
+ xbt_free(b);
+}
/* Waits for the child to end (or to timeout), and check its
ending conditions. This is launched from rctx_start but either in main
/* Wait for the child to die or the timeout to happen (or an armageddon to happen) */
while (!rctx->reader_done
&& (rctx->end_time < 0 || rctx->end_time >= now)) {
- usleep(100);
+ struct timespec ts;
+ ts.tv_sec = 0;
+ ts.tv_nsec = (100e-6 - floor(100e-6)) * 1e9;
+ nanosleep (&ts, NULL);
now = time(NULL);
}
XBT_INFO("<%s> timeouted. Kill the process.", rctx->filepos);
rctx->timeout = 1;
kill(rctx->pid, SIGTERM);
- usleep(100);
+ struct timespec ts;
+ ts.tv_sec = 0;
+ ts.tv_nsec = (100e-6 - floor(100e-6)) * 1e9;
+ nanosleep (&ts, NULL);
kill(rctx->pid, SIGKILL);
}
return NULL;
xbt_os_mutex_acquire(rctx->interruption); */
+ { // Sorting output got
+ xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
+ xbt_dynar_t b = xbt_dynar_new(sizeof(char *), NULL);
+ unsigned cpt;
+ char *str;
+ xbt_dynar_foreach(a, cpt, str) {
+ if (strncmp(str, "TESH_ERROR ", (sizeof "TESH_ERROR ") - 1) == 0) {
+ XBT_CRITICAL("%s", str);
+ errcode = 1;
+ } else if (coverage &&
+ strncmp(str, "profiling:", (sizeof "profiling:") - 1) == 0) {
+ XBT_DEBUG("Remove line [%u]: '%s'", cpt, str);
+ } else {
+ xbt_dynar_push_as(b, char *, str);
+ }
+ }
+
+ if (rctx->output_sort) {
+ stable_sort(b);
+ /* If empty lines moved in first position, remove them */
+ while (!xbt_dynar_is_empty(b) && *xbt_dynar_getfirst_as(b, char*) == '\0')
+ xbt_dynar_shift(b, NULL);
+ }
+
+ if (rctx->output_sort || xbt_dynar_length(b) != xbt_dynar_length(a)) {
+ char *newbuf = xbt_str_join(b, "\n");
+ strcpy(rctx->output_got->data, newbuf);
+ rctx->output_got->used = strlen(newbuf);
+ xbt_free(newbuf);
+ }
+
+ xbt_dynar_free(&b);
+ xbt_dynar_free(&a);
+ }
+
+ if (rctx->output_sort) { // Sorting output wanted
+ char *newbuf;
+ xbt_dynar_t a = xbt_str_split(rctx->output_wanted->data, "\n");
+
+ stable_sort(a);
+ /* If empty lines moved in first position, remove them */
+ while (!xbt_dynar_is_empty(a) && *xbt_dynar_getfirst_as(a, char*) == '\0')
+ xbt_dynar_shift(a, NULL);
+
+ newbuf = xbt_str_join(a, "\n");
+ strcpy(rctx->output_wanted->data, newbuf);
+ rctx->output_wanted->used = strlen(newbuf);
+ xbt_free(newbuf);
+
+ xbt_dynar_free(&a);
+ }
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)
XBT_INFO("<%s> No output before timeout", rctx->filepos);
XBT_ERROR("Test suite `%s': NOK (<%s> timeout after %d sec)",
testsuite_name, rctx->filepos, timeout_value);
- XBT_DEBUG("<%s> Interrupted = %d", rctx->filepos, rctx->interrupted);
+ XBT_DEBUG("<%s> Interrupted = %d", rctx->filepos, (int)rctx->interrupted);
if (!rctx->interrupted) {
xbt_os_mutex_release(rctx->interruption);
rctx_armageddon(rctx, 3);
}
rctx->expected_return = 0;
- if (rctx->expected_signal) {
- free(rctx->expected_signal);
- rctx->expected_signal = NULL;
- }
- }
- while (rctx->output_got->used
- && !strncmp(rctx->output_got->data, "TESH_ERROR ",
- strlen("TESH_ERROR "))) {
- int marklen = strlen("TESH_ERROR ");
- char *endline = strchr(rctx->output_got->data, '\n');
-
- XBT_CRITICAL("%.*s", (int) (endline - rctx->output_got->data - marklen),
- rctx->output_got->data + marklen);
- memmove(rctx->output_got->data, rctx->output_got->data + marklen,
- rctx->output_got->used - marklen);
- rctx->output_got->used -= endline - rctx->output_got->data + 1;
- rctx->output_got->data[rctx->output_got->used] = '\0';
- errcode = 1;
+ free(rctx->expected_signal);
+ rctx->expected_signal = NULL;
}
- if (rctx->output_sort) {
- xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
- xbt_dynar_sort(a,cmpstringp);
- char *sorted_output = xbt_str_join(a, "\n");
- strcpy(rctx->output_got->data, sorted_output);
- xbt_free(sorted_output);
- xbt_dynar_free(&a);
- /* If an empty line moved in first position, move it back to the end */
- if (rctx->output_got->data[0]=='\n') {
- memmove(rctx->output_got->data,rctx->output_got->data+1,rctx->output_got->used-1);
- rctx->output_got->data[rctx->output_got->used-1] = '\n';
- }
- }
if ((errcode && errcode != 1) || 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);
- XBT_INFO("Output of <%s> so far: \n||%s", rctx->filepos, out);
+ XBT_INFO("Output of <%s> so far:\n||%s", rctx->filepos, out);
free(out);
} else if (rctx->output == e_output_check
&& (rctx->output_got->used != rctx->output_wanted->used
xbt_dynar_t a = xbt_str_split(rctx->output_got->data, "\n");
char *out = xbt_str_join(a, "\n||");
xbt_dynar_free(&a);
- XBT_INFO("Here is the (ignored) command output: \n||%s", out);
+ XBT_INFO("Here is the (ignored) command output:\n||%s", out);
free(out);
}