Ejemplo n.º 1
0
void taskmain(int argc, char *argv[])
{
    LOG_FILE = stderr;
    glob_t profile_glob;
    int rc = 0;
    int i = 0;
    Action *action = NULL;
    tst_t *targets = NULL;
    bstring pid_file = NULL;

    check(argc == 3, "USAGE: procer <profile_dir> <procer_pid_file>");
    pid_file = bfromcstr(argv[2]);

    rc = Unixy_remove_dead_pidfile(pid_file);
    check(rc == 0, "Failed to remove %s, procer is probably already running.", bdata(pid_file));

    rc = Unixy_daemonize();
    check(rc == 0, "Couldn't daemonize, that's not good.");

    rc = chdir(argv[1]);
    check(rc == 0, "Couldn't change to %s profile dir.", argv[1]);

    rc = Unixy_pid_file(pid_file);
    check(rc == 0, "Failed to make the PID file: %s", bdata(pid_file));

    FILE *log = fopen("error.log", "a+");
    check(log, "Couldn't open error.log");
    setbuf(log, NULL);
    LOG_FILE = log;

    bstring dir_glob = bformat("%s/[A-Za-z0-9]*", argv[1]);
    check(dir_glob, "Couldn't make the directory glob.");

    rc = glob(bdata(dir_glob), GLOB_ERR | GLOB_ONLYDIR, NULL, &profile_glob);
    check(rc == 0, "Failed to find directories in the profiles.");

    debug("Loading %zu actions.", profile_glob.gl_pathc);
    for(i = 0; i < profile_glob.gl_pathc; i++) {
        action = Action_create(profile_glob.gl_pathv[i]);
        targets = tst_insert(targets, bdata(action->name), blength(action->name), action);
    }

    // now we setup the dependencies from the settings they've got
    tst_traverse(targets, Action_dependency_assign, targets);
    tst_traverse(targets, Action_start_all, NULL);

    taskexit(0);

error:
    taskexitall(1);
}
Ejemplo n.º 2
0
int AST_walk_hash(tst_t *settings, Value *data, ast_hash_walk_cb cb)
{
    struct ASTScanData scan = {.settings = settings, .cb = cb, .error = 0};
    tst_traverse(data->as.hash, ast_hash_traverse_cb, &scan);
    return scan.error;
}


Value *AST_get(tst_t *settings, tst_t *fr, bstring name, ValueType type)
{
    Pair *pair = tst_search(fr, bdata(name), blength(name));
    check_debug(pair, "Couldn't find variable %s of type %s", bdata(name), Value_type_name(type));

    Value *val = Pair_value(pair);

    if(Value_is(val, REF)) {
        val = Value_resolve(settings, val);
        check(val, "Couldn't find variable %s of type %s",
            bdata(name), Value_type_name(type));
    }

    check(val->type == type, "Invalid type for %s, should be %s not %s",
            bdata(name), Value_type_name(type), Value_type_name(val->type));

    return val;

error:
    return NULL;
}
Ejemplo n.º 3
0
static inline int Server_copy_active_handlers(Server *srv, Server *copy_from)
{
    int i = 0;
    for(i = 0; i < darray_end(copy_from->handlers); i++) {
        Handler *from = darray_get(copy_from->handlers, i);

        int j = 0;
        for(j = 0; j < darray_end(srv->handlers); j++) {
            Handler *to = darray_get(srv->handlers, j);

            if(same_handler(from, to))
            {
                debug("Swapping %p original for %p replacement", to, from);
                RouteUpdater update = {.original = to, .replacement = from};
                tst_traverse(srv->hosts->routes, update_host_routes, &update);

                darray_set(srv->handlers, j, from);
                // swap them around so that the darrays stay full
                darray_set(copy_from->handlers, i, to);
                to->running = 0;
                break;
            }
        }
    }

    return 0;
}
Ejemplo n.º 4
0
// returned object belongs to caller.
dict_array*
dict_to_array(dict *d)
{
    dict_array *a = pdf_malloc(sizeof(dict_array));
    if (!a)
	    return a;
    a->cur = 0;
    a->items = pdf_malloc((sizeof(struct dict_item))* (d->n));
    if (!a->items)
	    return NULL;
    if (d && d->dict)
    {
#if defined (HASHMAP)
        fprintf(stderr, "%s is not implemented", __FUNCTION__);
#elif defined (TSTC)
	    tstc_call(d->dict, 0, (tstc_f)dict_tstc_array_append, (void*)a);

#else
	    tst_print_reset(1);
	    tst_traverse(d->dict, (tst_hook)dict_array_append, a);
	    tst_print_reset(-1);
#endif
    }
    return a;
}
Ejemplo n.º 5
0
void MIME_destroy()
{
    if(MIME_MAP) {
        tst_traverse(MIME_MAP, MIME_traverse_destroy, NULL);
        tst_destroy(MIME_MAP);
        MIME_MAP = NULL;
    }
}
Ejemplo n.º 6
0
void Setting_destroy()
{
    if(SETTINGS_MAP) {
        tst_traverse(SETTINGS_MAP, Setting_traverse_destroy, NULL);
        tst_destroy(SETTINGS_MAP);
        SETTINGS_MAP = NULL;
    }
}
Ejemplo n.º 7
0
char *test_tst_traverse()
{
    traverse_count = 0;
    tst_traverse(node, tst_traverse_test_cb, valueA);
    debug("traverse count is: %d", traverse_count);
    mu_assert(traverse_count == 4, "Didn't find 4 keys.");

    return NULL;
}
Ejemplo n.º 8
0
void
dict_each(dict *d, void (*call()), void *a)
{
    if (d && d->dict)
    {
#ifdef TSTC
	    tstc_call(d->dict, 0, call, a);
#elif defined (HASHMAP)
#else
	    tst_traverse(d->dict, call, a);
#endif
    }
}
Ejemplo n.º 9
0
void run_repl(Module *state, const char *in_file) {
    printf("EaRing.  Copyright 2008 Zed A. Shaw.\n");
    printf("Done compiling %s.  Enter ? to get the function list.\n", in_file);
    char *cmd = NULL;
    current_module = state;
    repl_init(NULL);

    while((cmd = repl_prompt())) {
        if(cmd[0] == '?') {
            tst_traverse(state->functions, (tst_traverse_cb)query_functions, NULL);
        } else {
            run_function(state, cmd);
        }
    }
}
Ejemplo n.º 10
0
void dict_dump(dict* d)
{
    printf("%s", "\n");
    if (d)
    {
        if (d->dict)
        {
#ifdef TSTC
            tstc_print(d->dict);
#elif defined (HASHMAP)
#else
            tst_print_reset(1);
            tst_traverse(d->dict, dict_print_keyval, NULL);
            tst_print_reset(-1);
#endif
        }
    }
}
Ejemplo n.º 11
0
void  dict_free(dict* d)
{
    if (d)
    {
        if (d->dict)
        {
#ifdef TSTC
            tstc_call(d->dict, 0, dict_entry_free, 0);
            tstc_free(d->dict);
#elif defined (HASHMAP)
            extern void pdf_obj_delete(void *);
            hash_map_entry *e;
            hash_map_iterator *i = hash_map_front(d->dict);

            while (!hash_map_iterator_at_end(i))
            {
                e = hash_map_iterator_get(i);
                pdf_obj_delete(e->v);
                pdf_free(e->v);
                hash_map_iterator_next(i);
            }
            hash_map_iterator_free(i);
            hash_map_free(d->dict);
#else
            tst_print_reset(1);
            tst_traverse(d->dict, dict_free_val, NULL);
            tst_cleanup(d->dict);
            tst_print_reset(-1);
#endif
        }
	    if (d->stream)
	    {
            pdf_free(d->stream);
	    }
        pdf_free(d);
    }
    return;
}
Ejemplo n.º 12
0
// returned object belongs to caller.
// Experimental: using list as target by imitating "LISP" style.
dict_list*
dict_to_list(dict *d)
{
    dict_list *l = pdf_malloc(sizeof(dict_list));
    l->next = l->last = NULL;
    l->val.t = eLimit;
    if (d && d->dict)
    {
#ifdef TSTC
	    char buf[1024];
	    tstc_call(d->dict, buf, dict_list_append, l);
#elif defined (HASHMAP)
        hash_map_iterator *i = hash_map_front(d->dict);
        hash_map_entry *e;

        while (!hash_map_iterator_at_end(i))
        {
            e = hash_map_iterator_get(i);

            dict_list_append((char*)e->k, e->v, l);

            hash_map_iterator_next(i);
        }
        hash_map_iterator_free(i);
#else
	    tst_print_reset(1);
	    tst_traverse(d->dict, (tst_hook)dict_list_append, l);
	    tst_print_reset(-1);
#endif
    }
    if (l->val.t == eLimit)
    {
	    pdf_free(l);
	    l = NULL;
    }
    return l;
}
Ejemplo n.º 13
0
void AST_destroy(tst_t *settings)
{
    tst_traverse(settings, AST_destroy_cb, NULL);
    tst_destroy(settings);
}
Ejemplo n.º 14
0
static void update_host_routes(void *value, void *data)
{
    Host *host = ((Route *)value)->data;
    tst_traverse(host->routes->routes, update_routes, data);
}
Ejemplo n.º 15
0
char *cmd_help(void *self, char *argv[], int argc) {
  char *ret = malloc(2048);
  memset(ret, '\0', sizeof(2048));
  tst_traverse(commands, _cmd_help_i, (void *)ret);
  return ret;
}
Ejemplo n.º 16
0
void taskmain(int argc, char *argv[])
{
    dbg_set_log(stderr);
    glob_t profile_glob;
    int rc = 0;
    int i = 0;
    Action *action = NULL;
    tst_t *targets = NULL;
    bstring pid_file = NULL;
    char *procer_error_log = NULL;

    m2program = procer_program;

    check(argc == 3, "USAGE: %s <profile_dir> <procer_pid_file>", m2program);
    pid_file = bfromcstr(argv[2]);

    srand(time(NULL)); // simple randomness

    rc = Unixy_remove_dead_pidfile(pid_file);
    check(rc == 0, "Failed to remove %s, %s is probably already running.", bdata(pid_file), m2program);

    rc = Unixy_daemonize(1);
    check(rc == 0, "Couldn't daemonize, that's not good.");

    rc = chdir(argv[1]);
    check(rc == 0, "Couldn't change to %s profile dir.", argv[1]);

    rc = Unixy_pid_file(pid_file);
    check(rc == 0, "Failed to make the PID file: %s", bdata(pid_file));

    if( (procer_error_log = getenv("PROCER_ERROR_LOG")) == NULL)
        procer_error_log = "error.log";
    FILE *log = fopen(procer_error_log, "a+");
    check(log, "Couldn't open error.log");
    setbuf(log, NULL);
    dbg_set_log(log);

    bstring dir_glob = bformat("%s/[A-Za-z0-9]*", argv[1]);
    check(dir_glob, "Couldn't make the directory glob.");

    rc = glob(bdata(dir_glob), GLOB_ERR, NULL, &profile_glob);
    check(rc == 0, "Failed to find directories in the profiles.");

    struct stat sb;
    debug("Loading %zu actions.", profile_glob.gl_pathc);

    start_terminator();

    while(RUNNING) {
        for(i = 0; i < profile_glob.gl_pathc; i++) {
            rc = lstat(profile_glob.gl_pathv[i], &sb);
            check(rc == 0, "Failed to stat file or directory: %s", profile_glob.gl_pathv[i]);

            if (sb.st_mode & S_IFDIR) {
                action = Action_create(profile_glob.gl_pathv[i]);
                targets = tst_insert(targets, bdata(action->name), blength(action->name), action);
            }
        }

        // now we setup the dependencies from the settings they've got
        tst_traverse(targets, Action_dependency_assign, targets);
        tst_traverse(targets, Action_start_all, NULL);

        while(RUNNING) {
            Action_sleep(1);
        }
    }

    log_warn("Exiting as requested from procer.");

    taskexit(0);

error:
    taskexitall(1);
}
Ejemplo n.º 17
0
int main(int argc, char *argv[])
{
    CORD input = NULL;
    const char *in_file_name = NULL;
    FILE *in_file = NULL;
    Module *state = NULL;
    int opt;
    int disassemble = 0;
    const char *func = "main";
    int interactive = 0;
    int listing = 0;

    while((opt = getopt(argc, argv, "lhdf:i")) != -1) {
        switch(opt) {
            case 'd': disassemble = 1; break;
            case 'f': func = optarg; break;
            case 'i': interactive = 1; break;
            case 'l': listing = 1; break;
            case 'h': /// fall through!
            default:
                die(NULL, "USAGE: earing [-d | -i] [-f function] <file.asm>\n");
                return 1;
        }
    }

    if(optind >= argc) {
        die(NULL, "You have to give a file.  Use -h to see the usage.");
        return 1;
    }

    GC_INIT();

    in_file_name = argv[optind];
    in_file = fopen(in_file_name, "r");

    if(!in_file) {
        die(NULL, "Failed to open the input file %s", in_file_name);
    }

    input = CORD_from_file(in_file);

    state = Module_create(in_file_name, 1024);
    Module_register_default_directives(state);

    if(!Module_compile(state, CORD_to_const_char_star(input), CORD_len(input))) {
        die(state, "Parsing failed with %d errors.\n", state->errors);
        return 1;
    } else {
        if(listing) {
            tst_traverse(state->functions, (tst_traverse_cb)query_functions, NULL);
        } else if(disassemble) {
            dis_functions(state);
        } else if(interactive) {
            // go into interactive mode with the repl
            run_repl(state, in_file_name);
        } else {
            // run the given function or the "main" default
            run_function(state, func);
        }
    }

    return 0;
}