]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/netbsd-tests/lib/libc/sys/t_kevent.c
MFC r305358,r305449,r305451,r306367,r306397,r309474:
[FreeBSD/stable/10.git] / contrib / netbsd-tests / lib / libc / sys / t_kevent.c
1 /*      $NetBSD: t_kevent.c,v 1.7 2015/02/05 13:55:37 isaki 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.7 2015/02/05 13:55:37 isaki 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 #ifdef __NetBSD__
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 #ifdef __FreeBSD__
55 #define DRVCTLDEV "/nonexistent"
56 #endif
57
58 ATF_TC(kevent_zerotimer);
59 ATF_TC_HEAD(kevent_zerotimer, tc)
60 {
61         atf_tc_set_md_var(tc, "descr", "Checks that kevent with a 0 timer "
62             "does not crash the system (PR lib/45618)");
63 }
64
65 ATF_TC_BODY(kevent_zerotimer, tc)
66 {
67         struct kevent ev;
68         int kq;
69
70         ATF_REQUIRE((kq = kqueue()) != -1);
71         EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 1, 0);
72         ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) != -1);
73         ATF_REQUIRE(kevent(kq, NULL, 0, &ev, 1, NULL) == 1);
74 }
75
76 ATF_TC(kqueue_desc_passing);
77 ATF_TC_HEAD(kqueue_desc_passing, tc)
78 {
79         atf_tc_set_md_var(tc, "descr", "Checks that passing a kqueue to "
80                 "another process does not crash the kernel (PR 46463)");
81 }
82
83 ATF_TC_BODY(kqueue_desc_passing, tc)
84 {
85         pid_t child;
86         int s[2], storage, status, kq;
87         struct cmsghdr *msg;
88         struct iovec iov;
89         struct msghdr m;
90         struct kevent ev;
91
92         ATF_REQUIRE((kq = kqueue()) != -1);
93
94         // atf_tc_skip("crashes kernel (PR 46463)");
95
96         ATF_REQUIRE(socketpair(AF_LOCAL, SOCK_STREAM, 0, s) != -1);
97         msg = malloc(CMSG_SPACE(sizeof(int)));
98         m.msg_iov = &iov;
99         m.msg_iovlen = 1;
100         m.msg_name = NULL;
101         m.msg_namelen = 0;
102         m.msg_control = msg;
103         m.msg_controllen = CMSG_SPACE(sizeof(int));
104 #ifdef __FreeBSD__
105         m.msg_flags = 0;
106 #endif
107
108         child = fork();
109         if (child == 0) {
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 #ifdef __FreeBSD__
121                 bcopy(CMSG_DATA(msg), &kq, sizeof(kq));
122                 printf("child (pid %d): received kq fd %d\n", getpid(), kq);
123                 _exit(0);
124 #else
125                 kq = *(int *)CMSG_DATA(msg);
126                 printf("child (pid %d): received kq fd %d\n", getpid(), kq);
127                 exit(0);
128 #endif
129         }
130
131         close(s[1]);
132
133         iov.iov_base = &storage;
134         iov.iov_len = sizeof(int);
135
136         msg->cmsg_level = SOL_SOCKET;
137         msg->cmsg_type = SCM_RIGHTS;
138         msg->cmsg_len = CMSG_LEN(sizeof(int));
139
140 #ifdef __FreeBSD__
141         /* 
142          * What is should have been
143          *   bcopy(&s[0], CMSG_DATA(msg), sizeof(kq));
144          */
145         bcopy(&kq, CMSG_DATA(msg), sizeof(kq));
146 #else
147         *(int *)CMSG_DATA(msg) = kq;
148 #endif
149
150         EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 1, 0);
151         ATF_CHECK(kevent(kq, &ev, 1, NULL, 0, NULL) != -1);
152
153         printf("parent (pid %d): sending kq fd %d\n", getpid(), kq);
154         if (sendmsg(s[0], &m, 0) == -1) {
155 #ifdef __NetBSD__
156                 ATF_REQUIRE_EQ_MSG(errno, EBADF, "errno is %d", errno);
157                 atf_tc_skip("PR kern/46523");
158 #endif
159 #ifdef __FreeBSD__
160                 ATF_REQUIRE_EQ_MSG(errno, EOPNOTSUPP, "errno is %d", errno);
161                 close(s[0]);
162 #endif
163         }
164
165         close(kq);
166
167         waitpid(child, &status, 0);
168         ATF_CHECK(WIFEXITED(status) && WEXITSTATUS(status)==0);
169 }
170
171 ATF_TC(kqueue_unsupported_fd);
172 ATF_TC_HEAD(kqueue_unsupported_fd, tc)
173 {
174         atf_tc_set_md_var(tc, "descr", "Checks that watching an fd whose"
175             " type is not supported does not crash the kernel");
176 }
177
178 ATF_TC_BODY(kqueue_unsupported_fd, tc)
179 {
180         /* mqueue and semaphore use fnullop_kqueue also */
181         int fd, kq;
182         struct kevent ev;
183
184         fd = open(DRVCTLDEV, O_RDONLY);
185         if (fd == -1) {
186                 switch (errno) {
187                 case ENOENT:
188                 case ENXIO:
189                         atf_tc_skip("no " DRVCTLDEV " available for testing");
190                         break;
191                 }
192         }
193         ATF_REQUIRE(fd != -1);
194         ATF_REQUIRE((kq = kqueue()) != -1);
195
196         EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
197            NOTE_DELETE|NOTE_WRITE|NOTE_EXTEND|NOTE_ATTRIB|NOTE_LINK|
198            NOTE_RENAME|NOTE_REVOKE, 0, 0);
199
200         ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == -1);
201         ATF_REQUIRE_ERRNO(EOPNOTSUPP, true);
202
203         (void)close(fd);
204 }
205
206
207 ATF_TP_ADD_TCS(tp)
208 {
209
210         ATF_TP_ADD_TC(tp, kevent_zerotimer);
211         ATF_TP_ADD_TC(tp, kqueue_desc_passing);
212         ATF_TP_ADD_TC(tp, kqueue_unsupported_fd);
213
214         return atf_no_error();
215 }