#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <dirent.h>
#include <port.h>
#include <err.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/poll.h>
#define FILE_SRC "/opt/os-tests/tests"
#define FILE_COUNT 10
#define TEST_REPEAT 10000
static uint_t
find_test_files(const char *dir, uint_t count, int *result_fds)
{
assert(count > 0);
DIR *dirp;
dirp = opendir(dir);
if (dirp == NULL) {
return (0);
}
dirent_t *de;
uint_t nvalid = 0;
while ((de = readdir(dirp)) != NULL) {
char path[MAXPATHLEN];
struct stat st;
(void) snprintf(path, sizeof (path), "%s/%s", dir, de->d_name);
if (lstat(path, &st) != 0 || (st.st_mode & S_IFREG) == 0) {
continue;
}
result_fds[nvalid] = open(path, O_RDONLY, 0);
if (result_fds[nvalid] < 0) {
continue;
}
nvalid++;
if (nvalid == count) {
break;
}
}
(void) closedir(dirp);
return (nvalid);
}
int
main(int argc, char *argv[])
{
int poll_fds[FILE_COUNT];
if (find_test_files(FILE_SRC, FILE_COUNT, poll_fds) != FILE_COUNT) {
errx(EXIT_FAILURE, "FAIL - count not open test files to poll");
}
for (uint_t i = 0; i < TEST_REPEAT; i++) {
int port_fds[FILE_COUNT];
for (uint_t j = 0; j < FILE_COUNT; j++) {
port_fds[j] = port_create();
if (port_fds[j] < 0) {
err(EXIT_FAILURE, "FAIL - port_create()");
}
int res = port_associate(port_fds[j], PORT_SOURCE_FD,
(uintptr_t)poll_fds[j], POLLIN, NULL);
if (res != 0) {
err(EXIT_FAILURE, "FAIL - port_associate()");
}
}
for (uint_t j = 0; j < FILE_COUNT; j++) {
int res = port_dissociate(port_fds[j], PORT_SOURCE_FD,
(uintptr_t)poll_fds[j]);
if (res != 0) {
err(EXIT_FAILURE, "FAIL - port_dissociate()");
}
(void) close(port_fds[j]);
}
}
(void) printf("PASS\n");
return (EXIT_SUCCESS);
}