xref: /openbsd-src/regress/lib/libc/sys/t_kevent.c (revision 824adb5411e4389b29bae28eba5c2c2bbd147f34)
1 /*	$OpenBSD: t_kevent.c,v 1.1 2021/09/02 12:40:44 mbuhl Exp $	*/
2 /*	$NetBSD: t_kevent.c,v 1.9 2020/10/31 01:08:32 christos Exp $ */
3 
4 /*-
5  * Copyright (c) 2011 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundatiom
9  * by Christos Zoulas.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 #include "macros.h"
33 
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: t_kevent.c,v 1.9 2020/10/31 01:08:32 christos Exp $");
36 
37 #include <sys/types.h>
38 #include <sys/event.h>
39 
40 #include "atf-c.h"
41 #include <errno.h>
42 #include <time.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <err.h>
49 #ifndef __OpenBSD__
50 #include <sys/drvctlio.h>
51 #endif
52 #include <sys/event.h>
53 #include <sys/time.h>
54 #include <sys/socket.h>
55 #include <sys/wait.h>
56 
57 ATF_TC(kevent_zerotimer);
58 ATF_TC_HEAD(kevent_zerotimer, tc)
59 {
60 	atf_tc_set_md_var(tc, "descr", "Checks that kevent with a 0 timer "
61 	    "does not crash the system (PR lib/45618)");
62 }
63 
64 ATF_TC_BODY(kevent_zerotimer, tc)
65 {
66 	struct kevent ev;
67 	int kq;
68 
69 	ATF_REQUIRE((kq = kqueue()) != -1);
70 	EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 1, 0);
71 	ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) != -1);
72 	ATF_REQUIRE(kevent(kq, NULL, 0, &ev, 1, NULL) == 1);
73 }
74 
75 ATF_TC(kqueue_desc_passing);
76 ATF_TC_HEAD(kqueue_desc_passing, tc)
77 {
78 	atf_tc_set_md_var(tc, "descr", "Checks that passing a kqueue to "
79 		"another process does not crash the kernel (PR 46463)");
80 }
81 
82 ATF_TC_BODY(kqueue_desc_passing, tc)
83 {
84 	pid_t child;
85 	int s[2], storage, status, kq;
86 	struct cmsghdr *msg;
87 	struct iovec iov;
88 	struct msghdr m;
89 	struct kevent ev;
90 
91 	ATF_REQUIRE((kq = kqueue()) != -1);
92 
93 	// atf_tc_skip("crashes kernel (PR kern/46463)");
94 
95 	ATF_REQUIRE(socketpair(AF_LOCAL, SOCK_STREAM, 0, s) != -1);
96 	msg = malloc(CMSG_SPACE(sizeof(int)));
97 	m.msg_iov = &iov;
98 	m.msg_iovlen = 1;
99 	m.msg_name = NULL;
100 	m.msg_namelen = 0;
101 	m.msg_control = msg;
102 	m.msg_controllen = CMSG_SPACE(sizeof(int));
103 
104 	child = fork();
105 	if (child == 0) {
106 #ifdef __OpenBSD__
107 		sleep(1);
108 		exit(0);
109 #endif
110 		close(s[0]);
111 
112 		iov.iov_base = &storage;
113 		iov.iov_len = sizeof(int);
114 		m.msg_iov = &iov;
115 		m.msg_iovlen = 1;
116 
117 		if (recvmsg(s[1], &m, 0) == -1)
118 			err(1, "child: could not recvmsg");
119 
120 		kq = *(int *)CMSG_DATA(msg);
121 		printf("child (pid %d): received kq fd %d\n", getpid(), kq);
122 		exit(0);
123 	}
124 
125 	close(s[1]);
126 
127 	iov.iov_base = &storage;
128 	iov.iov_len = sizeof(int);
129 
130 	msg->cmsg_level = SOL_SOCKET;
131 	msg->cmsg_type = SCM_RIGHTS;
132 	msg->cmsg_len = CMSG_LEN(sizeof(int));
133 
134 	*(int *)CMSG_DATA(msg) = kq;
135 
136 	EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 1, 0);
137 	ATF_CHECK(kevent(kq, &ev, 1, NULL, 0, NULL) != -1);
138 
139 	printf("parent (pid %d): sending kq fd %d\n", getpid(), kq);
140 	if (sendmsg(s[0], &m, 0) == -1) {
141 #ifdef __OpenBSD__
142 		ATF_REQUIRE_EQ_MSG(errno, EINVAL, "errno is %d", errno);
143 #else
144 		ATF_REQUIRE_EQ_MSG(errno, EBADF, "errno is %d", errno);
145 		atf_tc_skip("PR kern/46523");
146 #endif
147 	}
148 
149 	close(kq);
150 
151 	waitpid(child, &status, 0);
152 	ATF_CHECK(WIFEXITED(status) && WEXITSTATUS(status)==0);
153 }
154 
155 #ifndef __OpenBSD__
156 ATF_TC(kqueue_unsupported_fd);
157 ATF_TC_HEAD(kqueue_unsupported_fd, tc)
158 {
159 	atf_tc_set_md_var(tc, "descr", "Checks that watching an fd whose"
160 	    " type is not supported does not crash the kernel");
161 }
162 
163 ATF_TC_BODY(kqueue_unsupported_fd, tc)
164 {
165 	/* mqueue and semaphore use fnullop_kqueue also */
166 	int fd, kq;
167 	struct kevent ev;
168 
169 	fd = open(DRVCTLDEV, O_RDONLY);
170 	if (fd == -1) {
171 		switch (errno) {
172 		case ENOENT:
173 		case ENXIO:
174 			atf_tc_skip("no " DRVCTLDEV " available for testing");
175 			break;
176 		}
177 	}
178 	ATF_REQUIRE(fd != -1);
179 	ATF_REQUIRE((kq = kqueue()) != -1);
180 
181 	EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
182 	   NOTE_DELETE|NOTE_WRITE|NOTE_EXTEND|NOTE_ATTRIB|NOTE_LINK|
183 	   NOTE_RENAME|NOTE_REVOKE, 0, 0);
184 
185 	ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == -1);
186 	ATF_REQUIRE_ERRNO(EOPNOTSUPP, true);
187 
188 	(void)close(fd);
189 	(void)close(kq);
190 }
191 
192 ATF_TC(kqueue_EVFILT_USER);
193 ATF_TC_HEAD(kqueue_EVFILT_USER, tc)
194 {
195 	atf_tc_set_md_var(tc, "descr", "Checks usability of EVFILT_USER");
196 }
197 
198 ATF_TC_BODY(kqueue_EVFILT_USER, tc)
199 {
200 	/* mqueue and semaphore use fnullop_kqueue also */
201 	int kq;
202 	struct kevent ev, rev;
203 
204 	ATF_REQUIRE((kq = kqueue()) != -1);
205 
206 	EV_SET(&ev, 666, EVFILT_USER, EV_ADD | EV_ENABLE, 0, 0, 0);
207 	ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == 0);
208 	EV_SET(&ev, 666, EVFILT_USER, 0, NOTE_FFCOPY | NOTE_TRIGGER | 8, 0, 0);
209 	ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == 0);
210 	const struct timespec timeout = {
211 		.tv_sec = 1,
212 		.tv_nsec = 0,
213 	};
214 
215 	ATF_REQUIRE(kevent(kq, NULL, 0, &rev, 1, &timeout) == 1);
216 	ATF_REQUIRE(rev.ident == 666);
217 	ATF_REQUIRE(rev.filter == EVFILT_USER);
218 	ATF_REQUIRE(rev.fflags == 8);
219 	(void)close(kq);
220 }
221 #endif
222 
223 
224 
225 ATF_TP_ADD_TCS(tp)
226 {
227 
228 	ATF_TP_ADD_TC(tp, kevent_zerotimer);
229 	ATF_TP_ADD_TC(tp, kqueue_desc_passing);
230 #ifndef __OpenBSD__
231 	ATF_TP_ADD_TC(tp, kqueue_unsupported_fd);
232 	ATF_TP_ADD_TC(tp, kqueue_EVFILT_USER);
233 #endif
234 
235 	return atf_no_error();
236 }
237