diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2004-01-29 20:11:10 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2004-01-29 20:11:10 +0000 |
commit | 9e362e633109e886c4093cad2f14fd36c59e8ee8 (patch) | |
tree | da7e7f518de1b586ec7dd8b64d246406ded05d35 /regress/sys/fifofs | |
parent | 24ef46d92fe2b95c24faedf4d5fddd4a308cfaad (diff) |
Simple FIFO regress for O_RDWR using poll, read and write.
Diffstat (limited to 'regress/sys/fifofs')
-rw-r--r-- | regress/sys/fifofs/Makefile | 13 | ||||
-rw-r--r-- | regress/sys/fifofs/fifotest.c | 135 | ||||
-rw-r--r-- | regress/sys/fifofs/fifotest.out | 18 |
3 files changed, 166 insertions, 0 deletions
diff --git a/regress/sys/fifofs/Makefile b/regress/sys/fifofs/Makefile new file mode 100644 index 00000000000..930fcfb1bde --- /dev/null +++ b/regress/sys/fifofs/Makefile @@ -0,0 +1,13 @@ +# $OpenBSD: Makefile,v 1.1 2004/01/29 20:11:09 millert Exp $ + +PROG=fifotest +FIFO=fi.fo.fum +CLEANFILES+=${FIFO} +CFLAGS+= -Wall + +REGRESS_TARGETS=runtest + +runtest: ${PROG} + ./${PROG} ${FIFO} | diff - ${.CURDIR}/${PROG}.out + +.include <bsd.regress.mk> diff --git a/regress/sys/fifofs/fifotest.c b/regress/sys/fifofs/fifotest.c new file mode 100644 index 00000000000..a786fde9467 --- /dev/null +++ b/regress/sys/fifofs/fifotest.c @@ -0,0 +1,135 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/poll.h> +#include <errno.h> +#include <fcntl.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +void usage(void); +void sigalrm(int); +void dopoll(int, int, char *); + +#if defined(__OpenBSD__) || defined(__linux__) +extern char *__progname; +#else +char *__progname; +#endif + +/* + * Test FIFOs and poll(2) + * Expected results: + * testing POLLIN|POLLOUT + * 1 fd(s) ready, revents == POLLOUT + * testing POLLIN + * 0 fd(s) ready + * testing POLLOUT + * 1 fd(s) ready, revents == POLLOUT + */ +int main(int argc, char **argv) +{ + struct sigaction sa; + ssize_t nread; + int fd; + char *fifo, buf[BUFSIZ]; + +#if !defined(__OpenBSD__) && !defined(__linux__) + __progname = argv[0]; +#endif + if (argc != 2) + usage(); + + fifo = argv[1]; + (void)unlink(fifo); + if (mkfifo(fifo, 0644) != 0) { + printf("mkfifo %s: %s\n", fifo, strerror(errno)); + exit(1); + } + + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sa.sa_handler = sigalrm; + sigaction(SIGALRM, &sa, NULL); + + alarm(2); + if ((fd = open(fifo, O_RDWR, 0644)) == -1) { + printf("open %s: %s\n", fifo, strerror(errno)); + exit(1); + } + alarm(0); + (void)unlink(fifo); + printf("sucessfully opened fifo\n"); + + printf("\nTesting empty FIFO:\n"); + dopoll(fd, POLLIN|POLLOUT, "POLLIN|POLLOUT"); + dopoll(fd, POLLIN, "POLLIN"); + dopoll(fd, POLLOUT, "POLLOUT"); + + if (write(fd, "test", 4) != 4) { + printf("write error: %s\n", strerror(errno)); + exit(1); + } + + printf("\nTesting full FIFO:\n"); + dopoll(fd, POLLIN|POLLOUT, "POLLIN|POLLOUT"); + dopoll(fd, POLLIN, "POLLIN"); + dopoll(fd, POLLOUT, "POLLOUT"); + + if ((nread = read(fd, buf, sizeof(buf))) <= 0) { + printf("read error: %s\n", (nread == 0) ? "EOF" : strerror(errno)); + exit(1); + } + buf[nread] = '\0'; + printf("\treceived '%s' from FIFO\n", buf); + + exit(0); +} + +void +dopoll(int fd, int events, char *str) +{ + struct pollfd pfd; + int nready; + + pfd.fd = fd; + pfd.events = events; + + printf("\ttesting %s\n", str); + pfd.events = events; + alarm(2); + nready = poll(&pfd, 1, 0); + alarm(0); + if (nready < 0) { + printf("poll: %s\n", strerror(errno)); + return; + } + printf("\t\t%d fd(s) ready%s", nready, nready ? ", revents ==" : ""); + if (pfd.revents & POLLIN) + printf(" POLLIN"); + if (pfd.revents & POLLOUT) + printf(" POLLOUT"); + if (pfd.revents & POLLERR) + printf(" POLLERR"); + if (pfd.revents & POLLHUP) + printf(" POLLHUP"); + if (pfd.revents & POLLNVAL) + printf(" POLLNVAL"); + printf("\n"); +} + +void +sigalrm(int dummy) +{ + /* Just cause EINTR */ + return; +} + +void +usage(void) +{ + fprintf(stderr, "usage: %s fifoname\n", __progname); + exit(1); +} diff --git a/regress/sys/fifofs/fifotest.out b/regress/sys/fifofs/fifotest.out new file mode 100644 index 00000000000..717f2e0a4fa --- /dev/null +++ b/regress/sys/fifofs/fifotest.out @@ -0,0 +1,18 @@ +sucessfully opened fifo + +Testing empty FIFO: + testing POLLIN|POLLOUT + 1 fd(s) ready, revents == POLLOUT + testing POLLIN + 0 fd(s) ready + testing POLLOUT + 1 fd(s) ready, revents == POLLOUT + +Testing full FIFO: + testing POLLIN|POLLOUT + 1 fd(s) ready, revents == POLLIN POLLOUT + testing POLLIN + 1 fd(s) ready, revents == POLLIN + testing POLLOUT + 1 fd(s) ready, revents == POLLOUT + received 'test' from FIFO |