summaryrefslogtreecommitdiff
path: root/regress/sys/fifofs
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2004-05-10 17:03:24 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2004-05-10 17:03:24 +0000
commit8f3888266670aaad335ba6ca159d39488fe48fc4 (patch)
tree6c3770117cfbd66d8dc854e2274f2fc2deeb0ad7 /regress/sys/fifofs
parent5b56f599e03f5fa7dffdc519a7acfe191c11e124 (diff)
Check select in addition to poll (they should produce the same results).
Run tests 4 times: with and without blocking and with and without a timeout for poll/select. Results match Solaris.
Diffstat (limited to 'regress/sys/fifofs')
-rw-r--r--regress/sys/fifofs/fifotest.c124
-rw-r--r--regress/sys/fifofs/fifotest.out120
2 files changed, 213 insertions, 31 deletions
diff --git a/regress/sys/fifofs/fifotest.c b/regress/sys/fifofs/fifotest.c
index 01872bfd8e5..3826bbfaa57 100644
--- a/regress/sys/fifofs/fifotest.c
+++ b/regress/sys/fifofs/fifotest.c
@@ -14,7 +14,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include <sys/types.h>
+#include <sys/param.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <errno.h>
@@ -25,9 +25,15 @@
#include <string.h>
#include <unistd.h>
+#ifndef INFTIM
+#define INFTIM -1
+#endif
+
void usage(void);
void sigalrm(int);
-void dopoll(int, int, char *);
+void dopoll(int, int, char *, int);
+void doselect(int, int, int);
+void runtest(char *, int, int);
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
defined(__linux__)
@@ -39,21 +45,32 @@ char *__progname;
/*
* Test FIFOs and poll(2) both with an emtpy and full FIFO.
*/
-int main(int argc, char **argv)
+int
+main(int argc, char **argv)
{
- struct sigaction sa;
- ssize_t nread;
- int fd;
- char *fifo, buf[BUFSIZ];
-
-#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
- defined(__linux__)
+#if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
+ !defined(__linux__)
__progname = argv[0];
#endif
if (argc != 2)
usage();
- fifo = argv[1];
+ runtest(argv[1], 0, 0);
+ runtest(argv[1], 0, INFTIM);
+ runtest(argv[1], O_NONBLOCK, 0);
+ runtest(argv[1], O_NONBLOCK, INFTIM);
+
+ exit(0);
+}
+
+void
+runtest(char *fifo, int flags, int timeout)
+{
+ struct sigaction sa;
+ ssize_t nread;
+ int fd;
+ char buf[BUFSIZ];
+
(void)unlink(fifo);
if (mkfifo(fifo, 0644) != 0) {
printf("mkfifo %s: %s\n", fifo, strerror(errno));
@@ -66,18 +83,22 @@ int main(int argc, char **argv)
sigaction(SIGALRM, &sa, NULL);
alarm(2);
- if ((fd = open(fifo, O_RDWR, 0644)) == -1) {
+ if ((fd = open(fifo, O_RDWR | flags, 0644)) == -1) {
printf("open %s: %s\n", fifo, strerror(errno));
exit(1);
}
alarm(0);
(void)unlink(fifo);
- printf("sucessfully opened fifo\n");
+ printf("\nOpened fifo %s%s\n", fifo,
+ (flags & O_NONBLOCK) ? " (nonblocking)" : "");
printf("\nTesting empty FIFO:\n");
- dopoll(fd, POLLIN|POLLOUT, "POLLIN|POLLOUT");
- dopoll(fd, POLLIN, "POLLIN");
- dopoll(fd, POLLOUT, "POLLOUT");
+ dopoll(fd, POLLIN|POLLOUT, "POLLIN|POLLOUT", timeout);
+ dopoll(fd, POLLIN, "POLLIN", timeout);
+ dopoll(fd, POLLOUT, "POLLOUT", timeout);
+ doselect(fd, fd, timeout);
+ doselect(fd, -1, timeout);
+ doselect(-1, fd, timeout);
if (write(fd, "test", 4) != 4) {
printf("write error: %s\n", strerror(errno));
@@ -85,9 +106,12 @@ int main(int argc, char **argv)
}
printf("\nTesting full FIFO:\n");
- dopoll(fd, POLLIN|POLLOUT, "POLLIN|POLLOUT");
- dopoll(fd, POLLIN, "POLLIN");
- dopoll(fd, POLLOUT, "POLLOUT");
+ dopoll(fd, POLLIN|POLLOUT, "POLLIN|POLLOUT", timeout);
+ dopoll(fd, POLLIN, "POLLIN", timeout);
+ dopoll(fd, POLLOUT, "POLLOUT", timeout);
+ doselect(fd, fd, timeout);
+ doselect(fd, -1, timeout);
+ doselect(-1, fd, timeout);
if ((nread = read(fd, buf, sizeof(buf))) <= 0) {
printf("read error: %s\n", (nread == 0) ? "EOF" : strerror(errno));
@@ -95,12 +119,10 @@ int main(int argc, char **argv)
}
buf[nread] = '\0';
printf("\treceived '%s' from FIFO\n", buf);
-
- exit(0);
}
void
-dopoll(int fd, int events, char *str)
+dopoll(int fd, int events, char *str, int timeout)
{
struct pollfd pfd;
int nready;
@@ -108,10 +130,10 @@ dopoll(int fd, int events, char *str)
pfd.fd = fd;
pfd.events = events;
- printf("\ttesting %s\n", str);
+ printf("\tpoll %s, timeout=%d\n", str, timeout);
pfd.events = events;
alarm(2);
- nready = poll(&pfd, 1, 0);
+ nready = poll(&pfd, 1, timeout);
alarm(0);
if (nready < 0) {
printf("poll: %s\n", strerror(errno));
@@ -132,6 +154,60 @@ dopoll(int fd, int events, char *str)
}
void
+doselect(int rfd, int wfd, int timeout)
+{
+ struct timeval tv, *tvp;
+ fd_set *rfds = NULL, *wfds = NULL;
+ int nready, maxfd;
+
+ if (timeout == INFTIM)
+ tvp = NULL;
+ else {
+ tv.tv_sec = timeout / 1000;
+ tv.tv_usec = (timeout % 1000) * 1000;
+ tvp = &tv;
+ }
+ tv.tv_sec = tv.tv_usec = 0;
+ maxfd = rfd > wfd ? rfd : wfd;
+ if (rfd != -1) {
+ rfds = calloc(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask));
+ if (rfds == NULL) {
+ printf("unable to allocate memory\n");
+ exit(1);
+ }
+ FD_SET(rfd, rfds);
+ }
+ if (wfd != -1) {
+ wfds = calloc(howmany(maxfd + 1, NFDBITS), sizeof(fd_mask));
+ if (wfds == NULL) {
+ printf("unable to allocate memory\n");
+ exit(1);
+ }
+ FD_SET(wfd, wfds);
+ }
+
+ printf("\tselect%s%s\n", rfds ? " read" : "",
+ wfds ? " write" : "");
+
+ alarm(2);
+ nready = select(maxfd + 1, rfds, wfds, NULL, &tv);
+ alarm(0);
+ if (nready < 0) {
+ printf("select: %s\n", strerror(errno));
+ goto cleanup;
+ }
+ printf("\t\t%d fd(s) ready", nready);
+ if (rfds != NULL && FD_ISSET(rfd, rfds))
+ printf(", readable");
+ if (wfds != NULL && FD_ISSET(wfd, wfds))
+ printf(", writeable");
+ printf("\n");
+cleanup:
+ free(rfds);
+ free(wfds);
+}
+
+void
sigalrm(int dummy)
{
/* Just cause EINTR */
diff --git a/regress/sys/fifofs/fifotest.out b/regress/sys/fifofs/fifotest.out
index 717f2e0a4fa..98c477031e7 100644
--- a/regress/sys/fifofs/fifotest.out
+++ b/regress/sys/fifofs/fifotest.out
@@ -1,18 +1,124 @@
-sucessfully opened fifo
+
+Opened fifo fi.fo.fum
+
+Testing empty FIFO:
+ poll POLLIN|POLLOUT, timeout=0
+ 1 fd(s) ready, revents == POLLOUT
+ poll POLLIN, timeout=0
+ 0 fd(s) ready
+ poll POLLOUT, timeout=0
+ 1 fd(s) ready, revents == POLLOUT
+ select read write
+ 1 fd(s) ready, writeable
+ select read
+ 0 fd(s) ready
+ select write
+ 1 fd(s) ready, writeable
+
+Testing full FIFO:
+ poll POLLIN|POLLOUT, timeout=0
+ 1 fd(s) ready, revents == POLLIN POLLOUT
+ poll POLLIN, timeout=0
+ 1 fd(s) ready, revents == POLLIN
+ poll POLLOUT, timeout=0
+ 1 fd(s) ready, revents == POLLOUT
+ select read write
+ 2 fd(s) ready, readable, writeable
+ select read
+ 1 fd(s) ready, readable
+ select write
+ 1 fd(s) ready, writeable
+ received 'test' from FIFO
+
+Opened fifo fi.fo.fum
Testing empty FIFO:
- testing POLLIN|POLLOUT
+ poll POLLIN|POLLOUT, timeout=-1
+ 1 fd(s) ready, revents == POLLOUT
+ poll POLLIN, timeout=-1
+poll: Interrupted system call
+ poll POLLOUT, timeout=-1
1 fd(s) ready, revents == POLLOUT
- testing POLLIN
+ select read write
+ 1 fd(s) ready, writeable
+ select read
0 fd(s) ready
- testing POLLOUT
+ select write
+ 1 fd(s) ready, writeable
+
+Testing full FIFO:
+ poll POLLIN|POLLOUT, timeout=-1
+ 1 fd(s) ready, revents == POLLIN POLLOUT
+ poll POLLIN, timeout=-1
+ 1 fd(s) ready, revents == POLLIN
+ poll POLLOUT, timeout=-1
1 fd(s) ready, revents == POLLOUT
+ select read write
+ 2 fd(s) ready, readable, writeable
+ select read
+ 1 fd(s) ready, readable
+ select write
+ 1 fd(s) ready, writeable
+ received 'test' from FIFO
+
+Opened fifo fi.fo.fum (nonblocking)
+
+Testing empty FIFO:
+ poll POLLIN|POLLOUT, timeout=0
+ 1 fd(s) ready, revents == POLLOUT
+ poll POLLIN, timeout=0
+ 0 fd(s) ready
+ poll POLLOUT, timeout=0
+ 1 fd(s) ready, revents == POLLOUT
+ select read write
+ 1 fd(s) ready, writeable
+ select read
+ 0 fd(s) ready
+ select write
+ 1 fd(s) ready, writeable
+
+Testing full FIFO:
+ poll POLLIN|POLLOUT, timeout=0
+ 1 fd(s) ready, revents == POLLIN POLLOUT
+ poll POLLIN, timeout=0
+ 1 fd(s) ready, revents == POLLIN
+ poll POLLOUT, timeout=0
+ 1 fd(s) ready, revents == POLLOUT
+ select read write
+ 2 fd(s) ready, readable, writeable
+ select read
+ 1 fd(s) ready, readable
+ select write
+ 1 fd(s) ready, writeable
+ received 'test' from FIFO
+
+Opened fifo fi.fo.fum (nonblocking)
+
+Testing empty FIFO:
+ poll POLLIN|POLLOUT, timeout=-1
+ 1 fd(s) ready, revents == POLLOUT
+ poll POLLIN, timeout=-1
+poll: Interrupted system call
+ poll POLLOUT, timeout=-1
+ 1 fd(s) ready, revents == POLLOUT
+ select read write
+ 1 fd(s) ready, writeable
+ select read
+ 0 fd(s) ready
+ select write
+ 1 fd(s) ready, writeable
Testing full FIFO:
- testing POLLIN|POLLOUT
+ poll POLLIN|POLLOUT, timeout=-1
1 fd(s) ready, revents == POLLIN POLLOUT
- testing POLLIN
+ poll POLLIN, timeout=-1
1 fd(s) ready, revents == POLLIN
- testing POLLOUT
+ poll POLLOUT, timeout=-1
1 fd(s) ready, revents == POLLOUT
+ select read write
+ 2 fd(s) ready, readable, writeable
+ select read
+ 1 fd(s) ready, readable
+ select write
+ 1 fd(s) ready, writeable
received 'test' from FIFO