xref: /openbsd-src/regress/lib/libc/sys/t_kevent.c (revision d89ec533011f513df1010f142a111086a0785f09)
1 /*	$OpenBSD: t_kevent.c,v 1.2 2021/12/13 16:56:48 deraadt 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/types.h>
35 #include <sys/event.h>
36 
37 #include "atf-c.h"
38 #include <errno.h>
39 #include <time.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <err.h>
46 #ifndef __OpenBSD__
47 #include <sys/drvctlio.h>
48 #endif
49 #include <sys/event.h>
50 #include <sys/time.h>
51 #include <sys/socket.h>
52 #include <sys/wait.h>
53 
54 ATF_TC(kevent_zerotimer);
55 ATF_TC_HEAD(kevent_zerotimer, tc)
56 {
57 	atf_tc_set_md_var(tc, "descr", "Checks that kevent with a 0 timer "
58 	    "does not crash the system (PR lib/45618)");
59 }
60 
61 ATF_TC_BODY(kevent_zerotimer, tc)
62 {
63 	struct kevent ev;
64 	int kq;
65 
66 	ATF_REQUIRE((kq = kqueue()) != -1);
67 	EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 1, 0);
68 	ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) != -1);
69 	ATF_REQUIRE(kevent(kq, NULL, 0, &ev, 1, NULL) == 1);
70 }
71 
72 ATF_TC(kqueue_desc_passing);
73 ATF_TC_HEAD(kqueue_desc_passing, tc)
74 {
75 	atf_tc_set_md_var(tc, "descr", "Checks that passing a kqueue to "
76 		"another process does not crash the kernel (PR 46463)");
77 }
78 
79 ATF_TC_BODY(kqueue_desc_passing, tc)
80 {
81 	pid_t child;
82 	int s[2], storage, status, kq;
83 	struct cmsghdr *msg;
84 	struct iovec iov;
85 	struct msghdr m;
86 	struct kevent ev;
87 
88 	ATF_REQUIRE((kq = kqueue()) != -1);
89 
90 	// atf_tc_skip("crashes kernel (PR kern/46463)");
91 
92 	ATF_REQUIRE(socketpair(AF_LOCAL, SOCK_STREAM, 0, s) != -1);
93 	msg = malloc(CMSG_SPACE(sizeof(int)));
94 	m.msg_iov = &iov;
95 	m.msg_iovlen = 1;
96 	m.msg_name = NULL;
97 	m.msg_namelen = 0;
98 	m.msg_control = msg;
99 	m.msg_controllen = CMSG_SPACE(sizeof(int));
100 
101 	child = fork();
102 	if (child == 0) {
103 #ifdef __OpenBSD__
104 		sleep(1);
105 		exit(0);
106 #endif
107 		close(s[0]);
108 
109 		iov.iov_base = &storage;
110 		iov.iov_len = sizeof(int);
111 		m.msg_iov = &iov;
112 		m.msg_iovlen = 1;
113 
114 		if (recvmsg(s[1], &m, 0) == -1)
115 			err(1, "child: could not recvmsg");
116 
117 		kq = *(int *)CMSG_DATA(msg);
118 		printf("child (pid %d): received kq fd %d\n", getpid(), kq);
119 		exit(0);
120 	}
121 
122 	close(s[1]);
123 
124 	iov.iov_base = &storage;
125 	iov.iov_len = sizeof(int);
126 
127 	msg->cmsg_level = SOL_SOCKET;
128 	msg->cmsg_type = SCM_RIGHTS;
129 	msg->cmsg_len = CMSG_LEN(sizeof(int));
130 
131 	*(int *)CMSG_DATA(msg) = kq;
132 
133 	EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 1, 0);
134 	ATF_CHECK(kevent(kq, &ev, 1, NULL, 0, NULL) != -1);
135 
136 	printf("parent (pid %d): sending kq fd %d\n", getpid(), kq);
137 	if (sendmsg(s[0], &m, 0) == -1) {
138 #ifdef __OpenBSD__
139 		ATF_REQUIRE_EQ_MSG(errno, EINVAL, "errno is %d", errno);
140 #else
141 		ATF_REQUIRE_EQ_MSG(errno, EBADF, "errno is %d", errno);
142 		atf_tc_skip("PR kern/46523");
143 #endif
144 	}
145 
146 	close(kq);
147 
148 	waitpid(child, &status, 0);
149 	ATF_CHECK(WIFEXITED(status) && WEXITSTATUS(status)==0);
150 }
151 
152 #ifndef __OpenBSD__
153 ATF_TC(kqueue_unsupported_fd);
154 ATF_TC_HEAD(kqueue_unsupported_fd, tc)
155 {
156 	atf_tc_set_md_var(tc, "descr", "Checks that watching an fd whose"
157 	    " type is not supported does not crash the kernel");
158 }
159 
160 ATF_TC_BODY(kqueue_unsupported_fd, tc)
161 {
162 	/* mqueue and semaphore use fnullop_kqueue also */
163 	int fd, kq;
164 	struct kevent ev;
165 
166 	fd = open(DRVCTLDEV, O_RDONLY);
167 	if (fd == -1) {
168 		switch (errno) {
169 		case ENOENT:
170 		case ENXIO:
171 			atf_tc_skip("no " DRVCTLDEV " available for testing");
172 			break;
173 		}
174 	}
175 	ATF_REQUIRE(fd != -1);
176 	ATF_REQUIRE((kq = kqueue()) != -1);
177 
178 	EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
179 	   NOTE_DELETE|NOTE_WRITE|NOTE_EXTEND|NOTE_ATTRIB|NOTE_LINK|
180 	   NOTE_RENAME|NOTE_REVOKE, 0, 0);
181 
182 	ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == -1);
183 	ATF_REQUIRE_ERRNO(EOPNOTSUPP, true);
184 
185 	(void)close(fd);
186 	(void)close(kq);
187 }
188 
189 ATF_TC(kqueue_EVFILT_USER);
190 ATF_TC_HEAD(kqueue_EVFILT_USER, tc)
191 {
192 	atf_tc_set_md_var(tc, "descr", "Checks usability of EVFILT_USER");
193 }
194 
195 ATF_TC_BODY(kqueue_EVFILT_USER, tc)
196 {
197 	/* mqueue and semaphore use fnullop_kqueue also */
198 	int kq;
199 	struct kevent ev, rev;
200 
201 	ATF_REQUIRE((kq = kqueue()) != -1);
202 
203 	EV_SET(&ev, 666, EVFILT_USER, EV_ADD | EV_ENABLE, 0, 0, 0);
204 	ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == 0);
205 	EV_SET(&ev, 666, EVFILT_USER, 0, NOTE_FFCOPY | NOTE_TRIGGER | 8, 0, 0);
206 	ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == 0);
207 	const struct timespec timeout = {
208 		.tv_sec = 1,
209 		.tv_nsec = 0,
210 	};
211 
212 	ATF_REQUIRE(kevent(kq, NULL, 0, &rev, 1, &timeout) == 1);
213 	ATF_REQUIRE(rev.ident == 666);
214 	ATF_REQUIRE(rev.filter == EVFILT_USER);
215 	ATF_REQUIRE(rev.fflags == 8);
216 	(void)close(kq);
217 }
218 #endif
219 
220 
221 
222 ATF_TP_ADD_TCS(tp)
223 {
224 
225 	ATF_TP_ADD_TC(tp, kevent_zerotimer);
226 	ATF_TP_ADD_TC(tp, kqueue_desc_passing);
227 #ifndef __OpenBSD__
228 	ATF_TP_ADD_TC(tp, kqueue_unsupported_fd);
229 	ATF_TP_ADD_TC(tp, kqueue_EVFILT_USER);
230 #endif
231 
232 	return atf_no_error();
233 }
234