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