1 /* $NetBSD: t_kevent.c,v 1.7 2015/02/05 13:55:37 isaki Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundatiom
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_kevent.c,v 1.7 2015/02/05 13:55:37 isaki Exp $");
34 #include <sys/types.h>
35 #include <sys/event.h>
47 #include <sys/drvctlio.h>
49 #define DRVCTLDEV "/nonexistent"
51 #include <sys/event.h>
53 #include <sys/socket.h>
56 ATF_TC(kevent_zerotimer);
57 ATF_TC_HEAD(kevent_zerotimer, tc)
59 atf_tc_set_md_var(tc, "descr", "Checks that kevent with a 0 timer "
60 "does not crash the system (PR lib/45618)");
63 ATF_TC_BODY(kevent_zerotimer, tc)
68 ATF_REQUIRE((kq = kqueue()) != -1);
69 EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 1, 0);
70 ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) != -1);
71 ATF_REQUIRE(kevent(kq, NULL, 0, &ev, 1, NULL) == 1);
74 ATF_TC(kqueue_desc_passing);
75 ATF_TC_HEAD(kqueue_desc_passing, tc)
77 atf_tc_set_md_var(tc, "descr", "Checks that passing a kqueue to "
78 "another process does not crash the kernel (PR 46463)");
81 ATF_TC_BODY(kqueue_desc_passing, tc)
84 int s[2], storage, status, kq;
90 ATF_REQUIRE((kq = kqueue()) != -1);
92 // atf_tc_skip("crashes kernel (PR 46463)");
94 ATF_REQUIRE(socketpair(AF_LOCAL, SOCK_STREAM, 0, s) != -1);
95 msg = malloc(CMSG_SPACE(sizeof(int)));
101 m.msg_controllen = CMSG_SPACE(sizeof(int));
110 iov.iov_base = &storage;
111 iov.iov_len = sizeof(int);
115 if (recvmsg(s[1], &m, 0) == -1)
116 err(1, "child: could not recvmsg");
118 kq = *(int *)CMSG_DATA(msg);
119 printf("child (pid %d): received kq fd %d\n", getpid(), kq);
125 iov.iov_base = &storage;
126 iov.iov_len = sizeof(int);
129 msg = CMSG_FIRSTHDR(&m);
131 msg->cmsg_level = SOL_SOCKET;
132 msg->cmsg_type = SCM_RIGHTS;
133 msg->cmsg_len = CMSG_LEN(sizeof(int));
136 *(int *)CMSG_DATA(msg) = kq;
139 EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 1, 0);
140 ATF_CHECK(kevent(kq, &ev, 1, NULL, 0, NULL) != -1);
142 printf("parent (pid %d): sending kq fd %d\n", getpid(), kq);
143 if (sendmsg(s[0], &m, 0) == -1) {
144 ATF_REQUIRE_EQ_MSG(errno, EBADF, "errno is %d", errno);
145 atf_tc_skip("PR kern/46523");
150 waitpid(child, &status, 0);
151 ATF_CHECK(WIFEXITED(status) && WEXITSTATUS(status)==0);
154 ATF_TC(kqueue_unsupported_fd);
155 ATF_TC_HEAD(kqueue_unsupported_fd, tc)
157 atf_tc_set_md_var(tc, "descr", "Checks that watching an fd whose"
158 " type is not supported does not crash the kernel");
161 ATF_TC_BODY(kqueue_unsupported_fd, tc)
163 /* mqueue and semaphore use fnullop_kqueue also */
167 fd = open(DRVCTLDEV, O_RDONLY);
172 atf_tc_skip("no " DRVCTLDEV " available for testing");
176 ATF_REQUIRE(fd != -1);
177 ATF_REQUIRE((kq = kqueue()) != -1);
179 EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
180 NOTE_DELETE|NOTE_WRITE|NOTE_EXTEND|NOTE_ATTRIB|NOTE_LINK|
181 NOTE_RENAME|NOTE_REVOKE, 0, 0);
183 ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == -1);
184 ATF_REQUIRE_ERRNO(EOPNOTSUPP, true);
193 ATF_TP_ADD_TC(tp, kevent_zerotimer);
194 ATF_TP_ADD_TC(tp, kqueue_desc_passing);
195 ATF_TP_ADD_TC(tp, kqueue_unsupported_fd);
197 return atf_no_error();