/** * Construct a watched directory and add it to the list of watched directories * * @param dirwatch Pointer to the directory watcher * @param directory Name of directory to watch * @param flags Add one or more of: SXE_DIRWATCH_CREATED, SXE_DIRWATCH_MODIFIED, SXE_DIRWATCH_DELETED * @param notify Function to be called when directory changes * @param user_data Data to pass to the notify callback * * @exception Aborts if the dirwatch cannot be added to inotify */ void sxe_dirwatch_add(SXE_DIRWATCH * dirwatch, const char * directory, unsigned flags, void(*notify)(EV_P_ const char * file, int revents,void * user_data), void * user_data) { char buffer[PATH_MAX]; char * cwd; int inotify_flags = IN_MASK_ADD | IN_ONLYDIR; SXEE65("(dirwatch=%p, directory=%s, flags=0x%x, notify=%p, user_data=%p)", dirwatch, directory, flags, notify, user_data); if (flags & SXE_DIRWATCH_CREATED) { inotify_flags |= IN_CREATE | IN_MOVED_TO; } if (flags & SXE_DIRWATCH_MODIFIED) { inotify_flags |= IN_MODIFY; } if (flags & SXE_DIRWATCH_DELETED) { inotify_flags |= IN_DELETE | IN_MOVED_FROM; } dirwatch->notify = notify; dirwatch->user_data = user_data; dirwatch->fd = inotify_add_watch(sxe_dirwatch_watcher.fd, directory, inotify_flags); SXEA13(dirwatch->fd != -1, "Error watching directory '%s' (pwd = '%s'): %s", directory, (cwd = getcwd(buffer, sizeof(buffer))), strerror(errno)); sxe_list_push(&sxe_dirwatch_list, dirwatch); SXEL62("added directory %s as watch id %d", directory, dirwatch->fd); SXER60("return"); }
SXE_RETURN md5_to_hex(SOPHOS_MD5 * md5, char * md5_in_hex, unsigned md5_in_hex_length) { SXE_RETURN result = SXE_RETURN_OK; SXEE86("(md5=%08x%08x%08x%08x,md5_in_hex='%p',md5_in_hex_length='%u'", md5->word[3], md5->word[2], md5->word[1], md5->word[0], md5_in_hex, md5_in_hex_length); SXEA11(md5_in_hex_length == (MD5_IN_HEX_LENGTH + 1), "Incorrect length of char * for md5_to_hex(): '%u'", md5_in_hex_length); snprintf(md5_in_hex , 9, "%08x", htonl(md5->word[0])); snprintf(md5_in_hex + 8, 9, "%08x", htonl(md5->word[1])); snprintf(md5_in_hex + 16, 9, "%08x", htonl(md5->word[2])); snprintf(md5_in_hex + 24, 9, "%08x", htonl(md5->word[3])); SXEL62("md5_in_hex: '%.*s'", MD5_IN_HEX_LENGTH, md5_in_hex); SXER81("return %s", sxe_return_to_string(result)); return result; }
SXE_RETURN sha1_to_hex(SOPHOS_SHA1 * sha1, char * sha1_in_hex, unsigned sha1_in_hex_length) { SXE_RETURN result = SXE_RETURN_OK; SXEE87("sxe_sha1_to_hex(sha1=%08x%08x%08x%08x%08x,sha1_in_hex='%p',sha1_in_hex_length='%u'", sha1->word[4], sha1->word[3], sha1->word[2], sha1->word[1], sha1->word[0], sha1_in_hex, sha1_in_hex_length); SXEA11(sha1_in_hex_length == (SHA1_IN_HEX_LENGTH + 1), "Incorrect length of char * for sha1_to_hex(): '%u'", sha1_in_hex_length); snprintf(sha1_in_hex , 9, "%08x", htonl(sha1->word[0])); snprintf(sha1_in_hex + 8, 9, "%08x", htonl(sha1->word[1])); snprintf(sha1_in_hex + 16, 9, "%08x", htonl(sha1->word[2])); snprintf(sha1_in_hex + 24, 9, "%08x", htonl(sha1->word[3])); snprintf(sha1_in_hex + 32, 9, "%08x", htonl(sha1->word[4])); SXEL62("sha1_in_hex: '%.*s'", 40, sha1_in_hex); SXER81("return %s", sxe_return_to_string(result)); return result; }
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 }