xref: /minix/tests/lib/libc/sys/t_kevent.c (revision ebfedea0)
1 /*	$NetBSD: t_kevent.c,v 1.6 2012/11/29 09:13:44 martin Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundatiom
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_kevent.c,v 1.6 2012/11/29 09:13:44 martin Exp $");
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 #include <sys/drvctlio.h>
47 #include <sys/event.h>
48 #include <sys/time.h>
49 #include <sys/socket.h>
50 #include <sys/wait.h>
51 
52 ATF_TC(kevent_zerotimer);
53 ATF_TC_HEAD(kevent_zerotimer, tc)
54 {
55 	atf_tc_set_md_var(tc, "descr", "Checks that kevent with a 0 timer "
56 	    "does not crash the system (PR lib/45618)");
57 }
58 
59 ATF_TC_BODY(kevent_zerotimer, tc)
60 {
61 	struct kevent ev;
62 	int kq;
63 
64 	ATF_REQUIRE((kq = kqueue()) != -1);
65 	EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 1, 0);
66 	ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) != -1);
67 	ATF_REQUIRE(kevent(kq, NULL, 0, &ev, 1, NULL) == 1);
68 }
69 
70 ATF_TC(kqueue_desc_passing);
71 ATF_TC_HEAD(kqueue_desc_passing, tc)
72 {
73 	atf_tc_set_md_var(tc, "descr", "Checks that passing a kqueue to "
74 		"another process does not crash the kernel (PR 46463)");
75 }
76 
77 ATF_TC_BODY(kqueue_desc_passing, tc)
78 {
79 	pid_t child;
80 	int s[2], storage, status, kq;
81 	struct cmsghdr *msg;
82 	struct iovec iov;
83 	struct msghdr m;
84 	struct kevent ev;
85 
86 	ATF_REQUIRE((kq = kqueue()) != -1);
87 
88 	// atf_tc_skip("crashes kernel (PR 46463)");
89 
90 	ATF_REQUIRE(socketpair(AF_LOCAL, SOCK_STREAM, 0, s) != -1);
91 	msg = malloc(CMSG_SPACE(sizeof(int)));
92 	m.msg_iov = &iov;
93 	m.msg_iovlen = 1;
94 	m.msg_name = NULL;
95 	m.msg_namelen = 0;
96 	m.msg_control = msg;
97 	m.msg_controllen = CMSG_SPACE(sizeof(int));
98 
99 	child = fork();
100 	if (child == 0) {
101 		close(s[0]);
102 
103 		iov.iov_base = &storage;
104 		iov.iov_len = sizeof(int);
105 		m.msg_iov = &iov;
106 		m.msg_iovlen = 1;
107 
108 		if (recvmsg(s[1], &m, 0) == -1)
109 			err(1, "child: could not recvmsg");
110 
111 		kq = *(int *)CMSG_DATA(msg);
112 		printf("child (pid %d): received kq fd %d\n", getpid(), kq);
113 		exit(0);
114 	}
115 
116 	close(s[1]);
117 
118 	iov.iov_base = &storage;
119 	iov.iov_len = sizeof(int);
120 
121 	msg->cmsg_level = SOL_SOCKET;
122 	msg->cmsg_type = SCM_RIGHTS;
123 	msg->cmsg_len = CMSG_LEN(sizeof(int));
124 
125 	*(int *)CMSG_DATA(msg) = kq;
126 
127 	EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 1, 0);
128 	ATF_CHECK(kevent(kq, &ev, 1, NULL, 0, NULL) != -1);
129 
130 	printf("parent (pid %d): sending kq fd %d\n", getpid(), kq);
131 	if (sendmsg(s[0], &m, 0) == -1) {
132 		ATF_REQUIRE_EQ_MSG(errno, EBADF, "errno is %d", errno);
133 		atf_tc_skip("PR kern/46523");
134 	}
135 
136 	close(kq);
137 
138 	waitpid(child, &status, 0);
139 	ATF_CHECK(WIFEXITED(status) && WEXITSTATUS(status)==0);
140 }
141 
142 ATF_TC(kqueue_unsupported_fd);
143 ATF_TC_HEAD(kqueue_unsupported_fd, tc)
144 {
145 	atf_tc_set_md_var(tc, "descr", "Checks that watching an fd whose"
146 	    " type is not supported does not crash the kernel");
147 }
148 
149 ATF_TC_BODY(kqueue_unsupported_fd, tc)
150 {
151 	/* mqueue and semaphore use fnullop_kqueue also */
152 	int fd, kq;
153 	struct kevent ev;
154 
155 	fd = open(DRVCTLDEV, O_RDONLY);
156 	if (fd == -1 && errno == ENOENT)
157 		atf_tc_skip("no " DRVCTLDEV " available for testing");
158 	ATF_REQUIRE(fd != -1);
159 	ATF_REQUIRE((kq = kqueue()) != -1);
160 
161 	EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
162 	   NOTE_DELETE|NOTE_WRITE|NOTE_EXTEND|NOTE_ATTRIB|NOTE_LINK|
163 	   NOTE_RENAME|NOTE_REVOKE, 0, 0);
164 
165 	ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == -1);
166 	ATF_REQUIRE_ERRNO(EOPNOTSUPP, true);
167 
168 	(void)close(fd);
169 }
170 
171 
172 ATF_TP_ADD_TCS(tp)
173 {
174 
175 	ATF_TP_ADD_TC(tp, kevent_zerotimer);
176 	ATF_TP_ADD_TC(tp, kqueue_desc_passing);
177 	ATF_TP_ADD_TC(tp, kqueue_unsupported_fd);
178 
179 	return atf_no_error();
180 }
181