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