Ejemplo n.º 1
0
/* The Execute function. Note that we use vfork() rather than fork. Vfork has
 a special semantic in that the child process runs in the parent address space
 until exec is called in the child. The child also run first and suspend the
 parent process until exec or exit is called */
Process_T Command_execute(T C) {
        assert(C);
        volatile int exec_error = 0;
        Process_T P = Process_new();
        P->env = (char**)List_toArray(C->env);
        P->args = (char**)List_toArray(C->args);
        createPipes(P);
        if ((P->pid = vfork()) < 0) {
                ERROR("Command: fork failed -- %s\n", System_getLastError());
                Process_free(&P);
                return NULL;
        }
        // Child
        else if (P->pid == 0) { 
                if (C->working_directory) {
                        if (! Dir_chdir(C->working_directory)) {
                                exec_error = errno;
                                ERROR("Command: sub-process cannot change working directory to '%s' -- %s\n", C->working_directory, System_getLastError());
                                _exit(errno);
                        }
                }
                if (C->uid)
                        P->uid = (setuid(C->uid) != 0) ? ERROR("Command: Cannot change process uid to '%d' -- %s\n", C->uid, System_getLastError()), getuid() : C->uid;
                else
                        P->uid = getuid();
                if (C->gid)
                        P->gid = (setgid(C->gid) != 0) ? ERROR("Command: Cannot change process gid to '%d' -- %s\n", C->gid, System_getLastError()), getgid() : C->gid;
                else
                        P->gid = getgid();
                setsid(); // Loose controlling terminal
                setupChildPipes(P);
                // Close all descriptors except stdio
                int descriptors = getdtablesize();
                for (int i = 3; i < descriptors; i++)
                        close(i);
                // Unblock any signals and reset signal handlers
                sigset_t mask;
                sigemptyset(&mask);
                pthread_sigmask(SIG_SETMASK, &mask, NULL);
                signal(SIGINT, SIG_DFL);
                signal(SIGQUIT, SIG_DFL);
                signal(SIGABRT, SIG_DFL);
                signal(SIGTERM, SIG_DFL);
                signal(SIGPIPE, SIG_DFL);
                signal(SIGCHLD, SIG_DFL); 
                signal(SIGUSR1, SIG_DFL);
                signal(SIGHUP, SIG_IGN);  // Ensure future opens won't allocate controlling TTYs
                // Execute the program
                execve(P->args[0], P->args, P->env);
                exec_error = errno;
                _exit(errno);
        }
        // Parent
        if (exec_error != 0)
                Process_free(&P);
        else 
                setupParentPipes(P);
        errno = exec_error;
        return P;
}
Ejemplo n.º 2
0
/* all is an Oec_AList of DataObject entries */
oe_id Oed_Dispatcher_reg_group(T _this_,
                               DataObjectList all,
                               oe_time dur,
                               bool consume,
                               user_callback *match_handler,
                               user_callback *timeout_handler,
                               user_callback_arg args) {

    oe_id sid = 0;

    assert(all);
    List_T group = List_list(NULL);
    for (Iterator iter = DataObjectList_iterator(all, true);
        Iterator_hasMore(iter);) {
        DataObject o = Iterator_next(iter);
        oe_scalar *templ_p = DataObject_toArray(o);
        item_ *item = _create_reg_item(_this_, templ_p, dur, consume, match_handler, timeout_handler, args);
        sid = item->sid;
        group = List_append(group, List_list(item, NULL));
    }
    item_ **groupitems = (item_**) List_toArray(group, NULL);
    for (int i = 0; i < List_length(group); i++) {
        groupitems[i]->group = group;

        _schedule_item(_this_, groupitems[i]);
    }

    Mem_free(groupitems, __FILE__, __LINE__);

    return sid; //you can cancel the whole group by unreg'ing this one sid
}
Ejemplo n.º 3
0
static void _remove_group(Table_T items, List_T group, bool timeout) {
    if (group == NULL) return;
    item_ **gitems = (item_ **) List_toArray(group, NULL);
    if (gitems == NULL) return;
    size_t sz = List_length(group);
    for (int i = 0; i < sz; i++) {
        item_ *gitem = gitems[i];
        gitem->group = NULL; 
        if (i > 0) {
            //don't run 'em twice
            gitem->match_handler = NULL;
            gitem->timeout_handler = NULL;
        }
        _remove_item(items, gitem, timeout);
    }
    Mem_free(gitems, __FILE__, __LINE__);
    List_free(&group);
}
Ejemplo n.º 4
0
void print_fgroups(T* table)
{
    char ** arr = (char **)Table_toArray(*table, NULL);
    int table_length = Table_length(*table) * 2;
    for (int i=0; i < table_length; i+=2) {
        L list_ptr = ((L)arr[i+1]);
        int list_size = List_length(list_ptr);
        if (list_size >= 2) {
            char **names = (char**) List_toArray(list_ptr, NULL);
            for (int j = 0; names[j]; j++)
                printf("%s\n", (char*)(names[j]));
            if (2*i+2 < table_length)
                printf("\n");
            free(names);
        }
        List_free(&list_ptr);
    }
    free(arr);
    Table_free(table);
}
Ejemplo n.º 5
0
void store_name(char* fprint_buffer, char* name_buf, T* table, L* name_list)
{
    const char* fprint = Atom_string(fprint_buffer);
    const char* name = Atom_string(name_buf);

    char **name_arr = (char**) List_toArray(*name_list, NULL);
    for(int z = 0; name_arr[z]; z++) {
        if(name_arr[z] == name) {
            fprintf(stderr, "Duplicate name detected: Ignoring input\n");
            free (name_arr);
            return;
        }
    }
    free(name_arr);
    *name_list = List_push(*name_list, (void*) name);
    L list = (L) Table_get(*table, fprint);
    if (list == NULL) {
        list = List_list((char*) name, NULL);
    } else {
        list = List_push(list, (void*) name);

    }
    Table_put(*table, fprint, list);
}
Ejemplo n.º 6
0
int main(void) {
        List_T L = NULL;

        Bootstrap(); // Need to initialize library

        printf("============> Start List Tests\n\n");

        printf("=> Test0: create\n");
        {
                L = List_new();
                assert(L);
                List_free(&L);
        }
        printf("=> Test0: OK\n\n");

        printf("=> Test1: List_push() & List_length()\n");
        {
                L = List_new();
                List_push(L, "1");
                List_push(L, "2");
                List_push(L, "3");
                List_push(L, "4");
                List_push(L, "5");
                List_push(L, "6");
                List_push(L, "7");
                List_push(L, "8");
                List_push(L, "9");
                List_push(L, "10");
                List_push(L, "11");
                List_push(L, "12");
                List_push(L, "13");
                List_push(L, "14");
                List_push(L, "15");
                assert(Str_isEqual(L->tail->e, "1"));
                assert(Str_isEqual(L->head->e, "15"));
                assert(List_length(L) == 15);
        }
        printf("=> Test1: OK\n\n");

        printf("=> Test2: List_pop()\n");
        {
                int i= 0;
                list_t p;
                while (List_pop(L)) ;
                assert(List_length(L) == 0);
                // Ensure that nodes are retained in the freelist
                for (p= L->freelist; p; p= p->next) i++;
                assert(i == 15);
                List_free(&L);
        }
        printf("=> Test2: OK\n\n");

        printf("=> Test3: List_append()\n");
        {
                L = List_new();
                List_append(L, "1");
                List_append(L, "2");
                List_append(L, "3");
                List_append(L, "4");
                List_append(L, "5");
                List_append(L, "6");
                List_append(L, "7");
                List_append(L, "8");
                List_append(L, "9");
                List_append(L, "10");
                List_append(L, "11");
                List_append(L, "12");
                List_append(L, "13");
                List_append(L, "14");
                List_append(L, "15");
                assert(Str_isEqual(L->tail->e, "15"));
                assert(Str_isEqual(L->head->e, "1"));
                assert(List_length(L) == 15);
        }
        printf("=> Test3: OK\n\n");

        printf("=> Test4: List_cat()\n");
        {
                List_T t= List_new();
                List_append(t, "a");
                List_append(t, "b");
                List_append(t, "c");
                List_append(t, "d");
                List_cat(L, t);
                assert(Str_isEqual(L->tail->e, "d"));
                assert(Str_isEqual(L->head->e, "1"));
                assert(List_length(L) == 19);
        }
        printf("=> Test4: OK\n\n");

        printf("=> Test5: List_reverse()\n");
        {
                list_t p;
                List_T l= List_new();
                List_append(l, "a");
                List_append(l, "b");
                List_append(l, "c");
                List_append(l, "d");
                printf("\tList before reverse: ");
                for (p= l->head; p; p= p->next)
                        printf("%s%s", (char*)p->e, p->next?"->":"\n");
                assert(Str_isEqual(l->head->e, "a"));
                assert(Str_isEqual(l->tail->e, "d"));
                List_reverse(l);
                printf("\tList after reverse: ");
                for (p= l->head; p; p= p->next)
                        printf("%s%s", (char*)p->e, p->next?"->":"\n");
                assert(Str_isEqual(l->head->e, "d"));
                assert(Str_isEqual(l->tail->e, "a"));
                List_free(&l);
        }
        printf("=> Test5: OK\n\n");

        printf("=> Test6: List_map()\n");
        {
                int i = 0;
                List_map(L, apply, &i);
                assert(i == 19);
        }
        printf("=> Test6: OK\n\n");

        printf("=> Test7: List_clear()\n");
        {
                List_clear(L);
                assert(List_length(L) == 0);
                assert(L->freelist);
        }
        printf("=> Test7: OK\n\n");

        List_free(&L);

        printf("=> Test8: List malloc\n");
        {
                L = List_new();
                List_push(L, "1");
                List_push(L, "2");
                List_push(L, "3");
                List_push(L, "4");
                List_push(L, "5");
                List_push(L, "6");
                List_push(L, "7");
                List_push(L, "8");
                List_push(L, "9");
                List_push(L, "10");
                List_push(L, "11");
                List_push(L, "12");
                List_push(L, "13");
                List_push(L, "14");
                List_push(L, "15");
                assert(Str_isEqual(L->tail->e, "1"));
                assert(Str_isEqual(L->head->e, "15"));
                assert(List_length(L) == 15);
                List_clear(L);
                List_append(L, "1");
                List_append(L, "2");
                List_append(L, "3");
                List_append(L, "4");
                List_append(L, "5");
                List_append(L, "6");
                List_append(L, "7");
                List_append(L, "8");
                List_append(L, "9");
                List_append(L, "10");
                List_append(L, "11");
                List_append(L, "12");
                List_append(L, "13");
                List_append(L, "14");
                List_append(L, "15");
                assert(Str_isEqual(L->tail->e, "15"));
                assert(Str_isEqual(L->head->e, "1"));
                assert(List_length(L) == 15);
                List_free(&L);
        }
        printf("=> Test8: OK\n\n");

        printf("=> Test9: List remove\n");
        {
                char *one = "1";
                char *two = "2";
                L = List_new();
                printf("\tRemove from empty list.. ");
                assert(List_remove(L, "1") == NULL);
                printf("OK\n");
                List_push(L, one);
                printf("\tRemove from 1 element list.. ");
                assert(List_remove(L, one) == one);
                assert(List_length(L) == 0);
                printf("OK\n");
                List_push(L, one);
                List_push(L, two);
                printf("\tRemove last from list.. ");
                assert(List_remove(L, two) == two);
                assert(List_length(L) == 1);
                printf("OK\n");
                List_append(L, two);
                List_append(L, two);
                List_append(L, two);
                List_append(L, "5");
                printf("\tRemove first occurrence.. ");
                assert(List_remove(L, two) == two);
                assert(List_length(L) == 4);
                printf("OK\n");
                List_free(&L);
        }
        printf("=> Test9: OK\n\n");

        printf("=> Test10: check pointers\n");
        {
                L = List_new();
                printf("\tCheck pop.. ");
                List_push(L, "1");
                List_push(L, "2");
                List_pop(L);
                List_pop(L);
                List_push(L, "1");
                assert(L->head == L->tail);
                List_pop(L);
                List_append(L, "1");
                assert(L->head == L->tail);
                printf("OK\n");
                printf("\tCheck remove.. ");
                List_push(L, "1");
                List_append(L, "2");
                List_remove(L, "2");
                List_remove(L, "1");
                assert(L->head == L->tail);
                printf("OK\n");
                List_free(&L);
        }
        printf("=> Test10: OK\n\n");

        printf("=> Test11: List_toArray()\n");
        {
                List_T l = List_new();
                List_append(l, "a");
                List_append(l, "b");
                List_append(l, "c");
                List_append(l, "d");
                char **array = (char**)List_toArray(l);
                assert(Str_isEqual(array[0], "a"));
                assert(Str_isEqual(array[1], "b"));
                assert(Str_isEqual(array[2], "c"));
                assert(Str_isEqual(array[3], "d"));
                assert(array[4] == NULL);
                FREE(array);
                List_free(&l);
        }
        printf("=> Test11: OK\n\n");

        printf("============> List Tests: OK\n\n");

        return 0;
}