summaryrefslogtreecommitdiff
path: root/regress/sys/kern/kqueue
diff options
context:
space:
mode:
authorPhilip Guenthe <guenther@cvs.openbsd.org>2011-07-07 02:00:52 +0000
committerPhilip Guenthe <guenther@cvs.openbsd.org>2011-07-07 02:00:52 +0000
commit378901b3e27a2741599c22ef8364af96214d7fc0 (patch)
treef2c82b44099595b8d7cb1f0d6ed8666930c59300 /regress/sys/kern/kqueue
parent7c33bed1adf5b744113a1098c89f33acafced56f (diff)
Add tests for kqueue EVFILT_SIGNAL and that they can't be passed over
sockets
Diffstat (limited to 'regress/sys/kern/kqueue')
-rw-r--r--regress/sys/kern/kqueue/Makefile11
-rw-r--r--regress/sys/kern/kqueue/kqueue-fdpass.c80
-rw-r--r--regress/sys/kern/kqueue/kqueue-signal.c99
-rw-r--r--regress/sys/kern/kqueue/main.c18
4 files changed, 200 insertions, 8 deletions
diff --git a/regress/sys/kern/kqueue/Makefile b/regress/sys/kern/kqueue/Makefile
index 4a14ae54331..b1ea724f570 100644
--- a/regress/sys/kern/kqueue/Makefile
+++ b/regress/sys/kern/kqueue/Makefile
@@ -1,9 +1,9 @@
-# $OpenBSD: Makefile,v 1.14 2010/08/04 05:57:21 guenther Exp $
+# $OpenBSD: Makefile,v 1.15 2011/07/07 02:00:51 guenther Exp $
PROG= kqueue-test
CFLAGS+=-Wall
SRCS= kqueue-pipe.c kqueue-fork.c main.c kqueue-process.c kqueue-random.c \
- kqueue-pty.c kqueue-tun.c
+ kqueue-pty.c kqueue-tun.c kqueue-signal.c kqueue-fdpass.c
LDADD= -levent -lutil
DPADD= ${LIBEVENT} ${LIBUTIL}
@@ -23,8 +23,13 @@ kq-tun: ${PROG}
@-${SUDO} rm -f /dev/tun98 /dev/tun99
kq-pty: ${PROG}
${SUDO} ./${PROG} -T
+kq-signal: ${PROG}
+ ./${PROG} -s
+kq-fdpass: ${PROG}
+ ./${PROG} -F
-REGRESS_TARGETS=kq-pipe kq-fork kq-process kq-random kq-tun kq-pty
+REGRESS_TARGETS=kq-pipe kq-fork kq-process kq-random kq-tun kq-pty kq-signal \
+ kq-fdpass
REGRESS_ROOT_TARGETS=${REGRESS_TARGETS}
.PHONY: ${REGRESS_TARGETS}
diff --git a/regress/sys/kern/kqueue/kqueue-fdpass.c b/regress/sys/kern/kqueue/kqueue-fdpass.c
new file mode 100644
index 00000000000..d801ec063a5
--- /dev/null
+++ b/regress/sys/kern/kqueue/kqueue-fdpass.c
@@ -0,0 +1,80 @@
+/* $OpenBSD: kqueue-fdpass.c,v 1.1 2011/07/07 02:00:51 guenther Exp $ */
+/*
+ * Written by Philip Guenther <guenther@openbsd.org> 2011 Public Domain
+ */
+
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <err.h>
+#include <errno.h>
+#include <unistd.h>
+#include <signal.h>
+
+#define ASS(cond, mess) do { if (!(cond)) { mess; return 1; } } while (0)
+
+#define ASSX(cond) ASS(cond, warnx("assertion " #cond " failed on line %d", __LINE__))
+
+
+int do_fdpass(void);
+
+int
+do_fdpass(void)
+{
+ struct msghdr msg;
+ union {
+ struct cmsghdr hdr;
+ char buf[CMSG_SPACE(sizeof(int))];
+ } cmsgbuf;
+ struct kevent ke;
+ struct cmsghdr *cmp;
+ pid_t pid;
+ int pfd[2], fd, status;
+
+ ASS(socketpair(PF_LOCAL, SOCK_STREAM, 0, pfd) == 0,
+ warn("socketpair"));
+
+ pid = fork();
+ if (pid == -1)
+ err(1, "fork");
+ if (pid == 0) {
+ close(pfd[0]);
+
+ /* a kqueue with event to pass */
+ fd = kqueue();
+ EV_SET(&ke, SIGHUP, EVFILT_SIGNAL, EV_ADD|EV_ENABLE,
+ 0, 0, NULL);
+ if (kevent(fd, &ke, 1, NULL, 0, NULL) != 0)
+ err(1, "can't register events on kqueue");
+
+ memset(&msg, 0, sizeof msg);
+ msg.msg_control = &cmsgbuf.buf;
+ msg.msg_controllen = sizeof(cmsgbuf);
+
+ cmp = CMSG_FIRSTHDR(&msg);
+ cmp->cmsg_len = CMSG_LEN(sizeof(int));
+ cmp->cmsg_level = SOL_SOCKET;
+ cmp->cmsg_type = SCM_RIGHTS;
+
+ *(int *)CMSG_DATA(cmp) = fd;
+
+ if (sendmsg(pfd[1], &msg, 0) == 0)
+ errx(1, "sendmsg succeeded when it shouldn't");
+ if (errno != EINVAL)
+ err(1, "child sendmsg");
+ printf("sendmsg failed with EINVAL as expected\n");
+ exit(0);
+ }
+
+ close(pfd[1]);
+ wait(&status);
+
+ return (0);
+}
+
diff --git a/regress/sys/kern/kqueue/kqueue-signal.c b/regress/sys/kern/kqueue/kqueue-signal.c
new file mode 100644
index 00000000000..0ad7b701303
--- /dev/null
+++ b/regress/sys/kern/kqueue/kqueue-signal.c
@@ -0,0 +1,99 @@
+/* $OpenBSD: kqueue-signal.c,v 1.1 2011/07/07 02:00:51 guenther Exp $ */
+/*
+ * Written by Philip Guenther <guenther@openbsd.org> 2011 Public Domain
+ */
+
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <err.h>
+#include <errno.h>
+#include <unistd.h>
+#include <signal.h>
+
+#define ASS(cond, mess) do { if (!(cond)) { mess; return 1; } } while (0)
+
+#define ASSX(cond) ASS(cond, warnx("assertion " #cond " failed on line %d", __LINE__))
+
+volatile sig_atomic_t saw_usr1 = 0;
+volatile sig_atomic_t result = 0;
+int kq;
+
+int
+sigtest(int signum, int catch)
+{
+ struct kevent ke;
+ struct timespec ts;
+
+ ts.tv_sec = 10;
+ ts.tv_nsec = 0;
+
+ ASS(kevent(kq, NULL, 0, &ke, 1, &ts) == 1,
+ warn("can't fetch event on kqueue"));
+ ASSX(ke.filter == EVFILT_SIGNAL);
+ ASSX(ke.ident == signum);
+ ASSX(ke.data == catch);
+ return (0);
+}
+
+void
+usr1handler(int signum)
+{
+ saw_usr1 = 1;
+ result = sigtest(SIGUSR1, 1);
+}
+
+
+int do_signal(void);
+
+int
+do_signal(void)
+{
+ struct kevent ke;
+ pid_t pid = getpid();
+ sigset_t mask;
+
+ ASS((kq = kqueue()) >= 0, warn("kqueue"));
+
+ signal(SIGUSR1, usr1handler);
+ signal(SIGUSR2, SIG_IGN);
+
+ EV_SET(&ke, SIGUSR1, EVFILT_SIGNAL, EV_ADD|EV_ENABLE, 0, 0, NULL);
+ ASS(kevent(kq, &ke, 1, NULL, 0, NULL) == 0,
+ warn("can't register events on kqueue"));
+ EV_SET(&ke, SIGUSR2, EVFILT_SIGNAL, EV_ADD|EV_ENABLE, 0, 0, NULL);
+ ASS(kevent(kq, &ke, 1, NULL, 0, NULL) == 0,
+ warn("can't register events on kqueue"));
+
+ ASSX(saw_usr1 == 0);
+ kill(pid, SIGUSR1);
+ ASSX(saw_usr1 == 1);
+
+ kill(pid, SIGUSR2);
+ if (sigtest(SIGUSR2, 1))
+ return (1);
+ kill(pid, SIGUSR2);
+ kill(pid, SIGUSR2);
+ if (sigtest(SIGUSR2, 2))
+ return (1);
+
+ sigemptyset(&mask);
+ sigaddset(&mask, SIGUSR1);
+ sigaddset(&mask, SIGUSR2);
+ sigprocmask(SIG_BLOCK, &mask, NULL);
+
+ signal(SIGUSR1, SIG_DFL);
+ kill(pid, SIGUSR1);
+ kill(pid, SIGUSR2);
+
+ close(kq);
+
+ return (0);
+}
+
diff --git a/regress/sys/kern/kqueue/main.c b/regress/sys/kern/kqueue/main.c
index d51ebb00304..26f1d50a2c1 100644
--- a/regress/sys/kern/kqueue/main.c
+++ b/regress/sys/kern/kqueue/main.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: main.c,v 1.5 2003/12/02 05:48:48 mickey Exp $ */
+/* $OpenBSD: main.c,v 1.6 2011/07/07 02:00:51 guenther Exp $ */
/*
* Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain
*/
@@ -10,9 +10,11 @@
int do_pipe(void);
int check_inheritance(void);
int do_process(void);
+int do_signal(void);
int do_random(void);
int do_pty(void);
int do_tun(void);
+int do_fdpass(void);
int
main(int argc, char **argv)
@@ -21,20 +23,26 @@ main(int argc, char **argv)
int ret, c;
ret = 0;
- while ((c = getopt(argc, argv, "fPprTt")) != -1) {
+ while ((c = getopt(argc, argv, "fFpPrstT")) != -1) {
switch (c) {
- case 'p':
- ret |= do_pipe();
- break;
case 'f':
ret |= check_inheritance();
break;
+ case 'F':
+ ret |= do_fdpass();
+ break;
+ case 'p':
+ ret |= do_pipe();
+ break;
case 'P':
ret |= do_process();
break;
case 'r':
ret |= do_random();
break;
+ case 's':
+ ret |= do_signal();
+ break;
case 't':
ret |= do_tun();
break;