Esempio n. 1
0
/**
 * Initialize the dirwatch package
 *
 * @exceptions Aborts if already initialized or on failure to initialize inotify
 */
void
sxe_dirwatch_init(void)
{
    SXEE60("()");
    if (sxe_dirwatch_inotify_fd != -1) {
        SXEL60("sxe_dirwatch_init: Already initialized");
        goto SXE_EARLY_OUT;
    }

    SXEA11((sxe_dirwatch_inotify_fd = inotify_init()) != -1, "sxe_dirwatch_init: inotify_init() failed with %s", strerror(errno));
    SXEL61("inotify_init() returned fd: %d", sxe_dirwatch_inotify_fd);
    SXE_LIST_CONSTRUCT(&sxe_dirwatch_list, 0, SXE_DIRWATCH, node);
    ev_io_init(&sxe_dirwatch_watcher, sxe_dirwatch_event, sxe_dirwatch_inotify_fd, EV_READ);

SXE_EARLY_OUT:
    SXER60("return");
}
Esempio n. 2
0
/* Note that the decoded_name will be NUL terminated, and therefore the
 * maximum_name_length_maximum should probably be 254 and not 253 (though, with
 * pointers, the decoded name could well be *longer* than 253 characters!
 */
static SXE_RETURN
sxe_dns_decode_name(
    const unsigned char * dns_packet                 ,
    unsigned                dns_packet_length          ,
    unsigned              name_offset                ,
    char                * decoded_name               ,
    unsigned                decoded_name_length_maximum,
    unsigned              * decoded_name_length        )
{
    SXE_RETURN result = SXE_RETURN_ERROR_INTERNAL;
    unsigned   decoded_name_offset = 0;

    SXEE86("sxe_dns_decode_name(dns_packet=%p, dns_packet_length=%u, name_offset=%u, decoded_name=%p, decoded_name_length_maximum=%u, decoded_name_length=%p)", dns_packet, dns_packet_length, name_offset, decoded_name, decoded_name_length_maximum, decoded_name_length);
    SXEA10(decoded_name_length != 0,                                 "A value for decoded_name_length must be provided");
    SXEA10(decoded_name == NULL || decoded_name_length_maximum != 0, "NAME can not be decoded into an empty buffer");

    while (name_offset < dns_packet_length) {
        unsigned char len = dns_packet[name_offset];

        if (len == '\0') {
            SXEL60("NAME is terminated normally");
            if (decoded_name_offset > 0) {
                --decoded_name_offset;
            }

            if (decoded_name != NULL) {
                decoded_name[decoded_name_offset] = '\0';
            }
            *decoded_name_length = decoded_name_offset;
            result = SXE_RETURN_OK;
            goto SXE_EARLY_OUT;
        }
        else if ((len & SXE_DNS_LABEL_LENGTH_POINTER) == 0) {
            SXEL73("Normal label of length %u '%.*s'", len, len, &dns_packet[name_offset + 1]);

            /* Don't allow the NAME to exceed the known packet length */
            if ((decoded_name != NULL) && (decoded_name_offset + len + 1 >= decoded_name_length_maximum)) {
                SXEL51("sxe_dns_decode_name(): NAME is invalid; Decoded NAME is longer than the provided buffer length %u", decoded_name_length_maximum);
                goto SXE_ERROR_OUT;
            }

            /* Copy the label */
            if (decoded_name != NULL) {
                memcpy((unsigned char*)(decoded_name + decoded_name_offset), dns_packet + name_offset + 1, len);
                decoded_name[decoded_name_offset + len] = '.';
            }

            decoded_name_offset += len + 1;
        }
        else if ((len & SXE_DNS_LABEL_LENGTH_POINTER) == SXE_DNS_LABEL_LENGTH_POINTER) {
            SXEL60("NAME includes a pointer");
            name_offset = (len & ~SXE_DNS_LABEL_LENGTH_POINTER) + dns_packet[name_offset + 1];
            SXEL61("New NAME offset is %u", name_offset);

            /* Prevent looping forever - a pointer should never point to a pointer
             */
            if ((dns_packet[name_offset] & SXE_DNS_LABEL_LENGTH_POINTER) != 0) {
                SXEL51("sxe_dns_decode_name(): NAME contains a pointer which points to another pointer at packet offset %u",
                       name_offset);
                goto SXE_ERROR_OUT;
            }

            continue;
        }
        else {
            SXEL51("sxe_dns_decode_name(): NAME is invalid; NAME contains an invalid length/pointer value at packet offset %u", name_offset);
            goto SXE_ERROR_OUT;
        }

        name_offset += len + 1;
    }

    SXEL60("NAME is invalid because it extends outside the packet");

SXE_ERROR_OUT:
SXE_EARLY_OUT:
    SXER81("return // result=%s", sxe_return_to_string(result));
    return result;
}
Esempio n. 3
0
int
main(int argc, char ** argv)
{
#ifdef WINDOWS_NT
    SXEL10("WARNING: Need to implement sxe_spawn() on Windows to run this test file!");
#else
    int           fd;
    double        start_time;
    unsigned      count;
    unsigned      id;
    unsigned    * pool;
    unsigned    * shared;
    size_t        size;
    SXE_MMAP      memmap;
    SXE_RETURN    result;
    SXE_SPAWN     spawn[TEST_CLIENT_INSTANCES];

    if (argc > 1) {
        count = atoi(argv[1]);
        sxe_mmap_open(&memmap, "memmap");
        shared  = (unsigned *)(unsigned long)SXE_MMAP_ADDR(&memmap);
        pool    = sxe_pool_from_base(shared);
        SXEL63("Instance %u mapped to shared pool // base=%p, pool=%p", count, shared, pool);
        do {
            usleep(10000 * count);
            id = sxe_pool_set_oldest_element_state_locked(pool, TEST_STATE_FREE, TEST_STATE_CLIENT_TAKE);
            SXEA10(id != SXE_POOL_LOCK_NEVER_TAKEN, "Got SXE_POOL_LOCK_NEVER_TAKEN");;
        } while (id == SXE_POOL_NO_INDEX);

        SXEL62("Instance %u got pool element %u", count, id);
        pool[id] = count;
        sxe_pool_set_indexed_element_state_locked(pool, id, TEST_STATE_CLIENT_TAKE, TEST_STATE_CLIENT_DONE);
        sxe_mmap_close(&memmap);
        SXEL61("Instance %u exiting", count);
        return 0;
    }

    plan_tests(5);
    ok((size = sxe_pool_size(TEST_CLIENT_INSTANCES/2, sizeof(*pool), TEST_STATE_NUMBER_OF_STATES)) >= TEST_CLIENT_INSTANCES * sizeof(*pool),
       "Expect pool size %u to be at least the size of the array %u", size, TEST_CLIENT_INSTANCES * sizeof(*pool));

    SXEA11((fd = open("memmap", O_CREAT | O_TRUNC | O_WRONLY, 0666)) >= 0, "Failed to create file 'memmap': %s",         strerror(errno));
    SXEA12(ftruncate(fd, size)                                       >= 0, "Failed to extend the file to %lu bytes: %s", size, strerror(errno));
    close(fd);
    sxe_mmap_open(&memmap, "memmap");
    shared = (unsigned *)(unsigned long)SXE_MMAP_ADDR(&memmap);

    pool = sxe_pool_construct(shared, "shared-pool", TEST_CLIENT_INSTANCES/2, sizeof(*pool), TEST_STATE_NUMBER_OF_STATES, SXE_POOL_LOCKS_ENABLED);

    sxe_register(TEST_CLIENT_INSTANCES + 1, 0);
    SXEA11((result = sxe_init()) == SXE_RETURN_OK,  "Failed to initialize the SXE package: %s",  sxe_return_to_string(result));

    for (count = 1; count <= TEST_CLIENT_INSTANCES; count++) {
        char buffer[12];

        snprintf(buffer, sizeof(buffer), "%u", count);
        result = sxe_spawn(NULL, &spawn[count - 1], argv[0], buffer, NULL, NULL, NULL, NULL);
        SXEA13(result == SXE_RETURN_OK, "Failed to spawn '%s %s': %s", argv[0], buffer, sxe_return_to_string(result));
    }

    start_time = sxe_get_time_in_seconds();
    for (count = 0; (count < TEST_CLIENT_INSTANCES); ) {
        SXEA10((TEST_WAIT + start_time ) > sxe_get_time_in_seconds(), "Unexpected timeout... is the hardware too slow?");
        usleep(10000);
        id = sxe_pool_set_oldest_element_state_locked(pool, TEST_STATE_CLIENT_DONE, TEST_STATE_FREE);

        /* Assert here in  the test. The actual service would take specific action here */
        SXEA12(id != SXE_POOL_LOCK_NEVER_TAKEN, "Parent: Failed to acqure lock .. yield limit reached. id %u vs %u", id, SXE_POOL_LOCK_NEVER_TAKEN);

        if (id != SXE_POOL_NO_INDEX) {
            SXEL62("Looks like instance %u got element %u", pool[id], id);
            count++;
        }
    }
    ok(count == TEST_CLIENT_INSTANCES, "All clients got an element in the pool");

    start_time = sxe_get_time_in_seconds();
    for (count = 0; (count < TEST_CLIENT_INSTANCES); count++) {
        SXEA10((TEST_WAIT + start_time ) > sxe_get_time_in_seconds(), "Unexpected timeout... is the hardware too slow?");
        waitpid(spawn[count].pid, NULL, 0);
    }

    ok(SXE_POOL_LOCK_NEVER_TAKEN != sxe_pool_lock(pool), "Forced lock to be always locked!");
    id = sxe_pool_set_oldest_element_state_locked(pool, TEST_STATE_FREE, TEST_STATE_FREE);
    ok(id == SXE_POOL_LOCK_NEVER_TAKEN,   "sxe_pool_set_oldest_element_state_locked() Failed to acquire lock");
    id = sxe_pool_set_indexed_element_state_locked(pool, 0, TEST_STATE_FREE, TEST_STATE_FREE);
    ok(id == SXE_POOL_LOCK_NEVER_TAKEN,   "sxe_pool_set_indexed_element_state_locked() Failed to acquire lock");
    sxe_pool_unlock(pool);
    sxe_pool_override_locked(pool); /* for coverage */

    sxe_mmap_close(&memmap);
    return exit_status();
#endif
}