2 * Copyright (c) 2005 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/event.h>
31 #include <sys/filio.h>
45 * Regression test for piddling details of fifos.
49 * All activity occurs within a temporary directory created early in the
52 char temp_dir[PATH_MAX];
62 makefifo(const char *fifoname, const char *testname)
65 if (mkfifo(fifoname, 0700) < 0)
66 err(-1, "%s: makefifo: mkfifo: %s", testname, fifoname);
70 cleanfifo(const char *fifoname, int fd1, int fd2)
77 (void)unlink(fifoname);
81 openfifo(const char *fifoname, const char *testname, int *reader_fdp,
86 fd1 = open(fifoname, O_RDONLY | O_NONBLOCK);
89 fd2 = open(fifoname, O_WRONLY | O_NONBLOCK);
103 * POSIX does not allow lseek(2) on fifos, so we expect ESPIPE as a result.
108 int reader_fd, writer_fd;
110 makefifo("testfifo", __func__);
112 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd) < 0) {
113 warn("%s: openfifo", __func__);
114 cleanfifo("testfifo", -1, -1);
118 if (lseek(reader_fd, SEEK_CUR, 1) >= 0) {
119 warnx("%s: lseek succeeded instead of returning ESPIPE",
121 cleanfifo("testfifo", reader_fd, writer_fd);
124 if (errno != ESPIPE) {
125 warn("%s: lseek returned instead of ESPIPE", __func__);
126 cleanfifo("testfifo", reader_fd, writer_fd);
130 cleanfifo("testfifo", reader_fd, writer_fd);
134 * truncate(2) on FIFO should silently return success.
140 makefifo("testfifo", __func__);
142 if (truncate("testfifo", 1024) != 0) {
143 warn("%s: truncate", __func__);
144 cleanfifo("testfifo", -1, -1);
148 cleanfifo("testfifo", -1, -1);
151 struct filter_entry {
155 const char *fe_errorname;
158 static const struct filter_entry good_filter_types[] = {
159 { EVFILT_READ, "EVFILT_READ", 0, "0" },
160 { EVFILT_WRITE, "EVFILT_WRITE", 0, "0" },
161 #if WORKING_EVFILT_VNODE_ON_FIFOS
162 { EVFILT_VNODE, "EVFILT_VNODE", EINVAL, "EINVAL" },
165 static const int good_filter_types_len = sizeof(good_filter_types) /
166 sizeof(good_filter_types[0]);
168 static const struct filter_entry bad_filter_types[] = {
169 { EVFILT_NETDEV, "EVFILT_NETDEV", EINVAL, "EINVAL" },
171 static const int bad_filter_types_len = sizeof(bad_filter_types) /
172 sizeof(bad_filter_types[0]);
175 * kqueue event-related tests are in fifo_io.c; however, that tests only
176 * valid invocations of kqueue. Check to make sure that some invalid filters
177 * that are generally allowed on file descriptors are not allowed to be
178 * registered with kqueue, and that if attempts are made, we get the right
184 int kqueue_fd, reader_fd, writer_fd;
185 struct kevent kev_set;
186 struct timespec timeout;
189 makefifo("testfifo", __func__);
191 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd) < 0) {
192 warn("%s: openfifo", __func__);
193 cleanfifo("testfifo", -1, -1);
197 kqueue_fd = kqueue();
199 warn("%s: kqueue", __func__);
200 cleanfifo("testfifo", reader_fd, writer_fd);
207 for (i = 0; i < good_filter_types_len; i++) {
208 bzero(&kev_set, sizeof(kev_set));
209 EV_SET(&kev_set, reader_fd, good_filter_types[i].fe_filter,
211 ret = kevent(kqueue_fd, &kev_set, 1, NULL, 0, &timeout);
213 warn("%s: kevent: adding good filter %s", __func__,
214 good_filter_types[i].fe_name);
216 cleanfifo("testfifo", reader_fd, writer_fd);
219 bzero(&kev_set, sizeof(kev_set));
220 EV_SET(&kev_set, reader_fd, good_filter_types[i].fe_filter,
222 ret = kevent(kqueue_fd, &kev_set, 1, NULL, 0, &timeout);
224 warn("%s: kevent: deleting good filter %s", __func__,
225 good_filter_types[i].fe_name);
227 cleanfifo("testfifo", reader_fd, writer_fd);
232 for (i = 0; i < bad_filter_types_len; i++) {
233 bzero(&kev_set, sizeof(kev_set));
234 EV_SET(&kev_set, reader_fd, bad_filter_types[i].fe_filter,
236 ret = kevent(kqueue_fd, &kev_set, 1, NULL, 0, &timeout);
238 warnx("%s: kevent: bad filter %s succeeded, expected "
239 "EINVAL", __func__, bad_filter_types[i].fe_name);
241 cleanfifo("testfifo", reader_fd, writer_fd);
244 if (errno != bad_filter_types[i].fe_error) {
245 warn("%s: kevent: bad filter %s failed with error "
247 bad_filter_types[i].fe_name,
248 bad_filter_types[i].fe_errorname);
250 cleanfifo("testfifo", reader_fd, writer_fd);
256 cleanfifo("testfifo", reader_fd, writer_fd);
260 test_ioctl_setclearflag(int fd, int flag, const char *testname,
261 const char *fdname, const char *flagname)
266 if (ioctl(fd, flag, &i) < 0) {
267 warn("%s:%s: ioctl(%s, %s, 1)", testname, __func__, fdname,
269 cleanfifo("testfifo", -1, -1);
274 if (ioctl(fd, flag, &i) < 0) {
275 warn("%s:%s: ioctl(%s, %s, 0)", testname, __func__, fdname,
277 cleanfifo("testfifo", -1, -1);
285 * Test that various ioctls can be issued against the file descriptor. We
286 * don't currently test the semantics of these changes here.
291 int reader_fd, writer_fd;
293 makefifo("testfifo", __func__);
295 if (openfifo("testfifo", __func__, &reader_fd, &writer_fd) < 0) {
296 warn("%s: openfifo", __func__);
297 cleanfifo("testfifo", -1, -1);
302 * Set and remove the non-blocking I/O flag.
304 if (test_ioctl_setclearflag(reader_fd, FIONBIO, __func__,
305 "reader_fd", "FIONBIO") < 0) {
306 cleanfifo("testfifo", reader_fd, writer_fd);
310 if (test_ioctl_setclearflag(writer_fd, FIONBIO, __func__,
311 "writer_fd", "FIONBIO") < 0) {
312 cleanfifo("testfifo", reader_fd, writer_fd);
317 * Set and remove the async I/O flag.
319 if (test_ioctl_setclearflag(reader_fd, FIOASYNC, __func__,
320 "reader_fd", "FIOASYNC") < 0) {
321 cleanfifo("testfifo", reader_fd, writer_fd);
325 if (test_ioctl_setclearflag(writer_fd, FIOASYNC, __func__,
326 "writer_fd", "FIONASYNC") < 0) {
327 cleanfifo("testfifo", reader_fd, writer_fd);
331 cleanfifo("testfifo", reader_fd, writer_fd);
335 main(int argc, char *argv[])
338 strcpy(temp_dir, "/tmp/fifo_misc.XXXXXXXXXXX");
339 if (mkdtemp(temp_dir) == NULL)
341 atexit(atexit_temp_dir);
343 if (chdir(temp_dir) < 0)
344 err(-1, "chdir %s", temp_dir);