]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/regression/fifo/fifo_misc/fifo_misc.c
This commit was generated by cvs2svn to compensate for changes in r153758,
[FreeBSD/FreeBSD.git] / tools / regression / fifo / fifo_misc / fifo_misc.c
1 /*-
2  * Copyright (c) 2005 Robert N. M. Watson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/types.h>
30 #include <sys/event.h>
31 #include <sys/filio.h>
32 #include <sys/stat.h>
33 #include <sys/time.h>
34
35 #include <err.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <limits.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 /*
45  * Regression test for piddling details of fifos.
46  */
47
48 /*
49  * All activity occurs within a temporary directory created early in the
50  * test.
51  */
52 char    temp_dir[PATH_MAX];
53
54 static void __unused
55 atexit_temp_dir(void)
56 {
57
58         rmdir(temp_dir);
59 }
60
61 static void
62 makefifo(const char *fifoname, const char *testname)
63 {
64
65         if (mkfifo(fifoname, 0700) < 0)
66                 err(-1, "%s: makefifo: mkfifo: %s", testname, fifoname);
67 }
68
69 static void
70 cleanfifo(const char *fifoname, int fd1, int fd2)
71 {
72
73         if (fd1 != -1)
74                 close(fd1);
75         if (fd2 != -1)
76                 close(fd2);
77         (void)unlink(fifoname);
78 }
79
80 static int
81 openfifo(const char *fifoname, const char *testname, int *reader_fdp,
82     int *writer_fdp)
83 {
84         int error, fd1, fd2;
85
86         fd1 = open(fifoname, O_RDONLY | O_NONBLOCK);
87         if (fd1 < 0)
88                 return (-1);
89         fd2 = open(fifoname, O_WRONLY | O_NONBLOCK);
90         if (fd2 < 0) {
91                 error = errno;
92                 close(fd1);
93                 errno = error;
94                 return (-1);
95         }
96         *reader_fdp = fd1;
97         *writer_fdp = fd2;
98
99         return (0);
100 }
101
102 /*
103  * POSIX does not allow lseek(2) on fifos, so we expect ESPIPE as a result.
104  */
105 static void
106 test_lseek(void)
107 {
108         int reader_fd, writer_fd;
109
110         makefifo("testfifo", __func__);
111
112         if (openfifo("testfifo", __func__, &reader_fd, &writer_fd) < 0) {
113                 warn("%s: openfifo", __func__);
114                 cleanfifo("testfifo", -1, -1);
115                 exit(-1);
116         }
117
118         if (lseek(reader_fd, SEEK_CUR, 1) >= 0) {
119                 warnx("%s: lseek succeeded instead of returning ESPIPE",
120                     __func__);
121                 cleanfifo("testfifo", reader_fd, writer_fd);
122                 exit(-1);
123         }
124         if (errno != ESPIPE) {
125                 warn("%s: lseek returned instead of ESPIPE", __func__);
126                 cleanfifo("testfifo", reader_fd, writer_fd);
127                 exit(-1);
128         }
129
130         cleanfifo("testfifo", reader_fd, writer_fd);
131 }
132
133 struct filter_entry {
134         int              fe_filter;
135         const char      *fe_name;
136         int              fe_error;
137         const char      *fe_errorname;
138 };
139
140 static const struct filter_entry good_filter_types[] = {
141         { EVFILT_READ, "EVFILT_READ", 0, "0" },
142         { EVFILT_WRITE, "EVFILT_WRITE", 0, "0" },
143 #if WORKING_EVFILT_VNODE_ON_FIFOS
144         { EVFILT_VNODE, "EVFILT_VNODE", EINVAL, "EINVAL" },
145 #endif
146 };
147 static const int good_filter_types_len = sizeof(good_filter_types) /
148     sizeof(good_filter_types[0]);
149
150 static const struct filter_entry bad_filter_types[] = {
151         { EVFILT_NETDEV, "EVFILT_NETDEV", EINVAL, "EINVAL" },
152 };
153 static const int bad_filter_types_len = sizeof(bad_filter_types) /
154     sizeof(bad_filter_types[0]);
155
156 /*
157  * kqueue event-related tests are in fifo_io.c; however, that tests only
158  * valid invocations of kqueue.  Check to make sure that some invalid filters
159  * that are generally allowed on file descriptors are not allowed to be
160  * registered with kqueue, and that if attempts are made, we get the right
161  * error.
162  */
163 static void
164 test_kqueue(void)
165 {
166         int kqueue_fd, reader_fd, writer_fd;
167         struct kevent kev_set;
168         struct timespec timeout;
169         int i, ret;
170
171         makefifo("testfifo", __func__);
172
173         if (openfifo("testfifo", __func__, &reader_fd, &writer_fd) < 0) {
174                 warn("%s: openfifo", __func__);
175                 cleanfifo("testfifo", -1, -1);
176                 exit(-1);
177         }
178
179         kqueue_fd = kqueue();
180         if (kqueue_fd < 0) {
181                 warn("%s: kqueue", __func__);
182                 cleanfifo("testfifo", reader_fd, writer_fd);
183                 exit(-1);
184         }
185
186         timeout.tv_sec = 0;
187         timeout.tv_nsec = 0;
188
189         for (i = 0; i < good_filter_types_len; i++) {
190                 bzero(&kev_set, sizeof(kev_set));
191                 EV_SET(&kev_set, reader_fd, good_filter_types[i].fe_filter,
192                     EV_ADD, 0, 0, 0);
193                 ret = kevent(kqueue_fd, &kev_set, 1, NULL, 0, &timeout);
194                 if (ret < 0) {
195                         warn("%s: kevent: adding good filter %s", __func__,
196                             good_filter_types[i].fe_name);
197                         close(kqueue_fd);
198                         cleanfifo("testfifo", reader_fd, writer_fd);
199                         exit(-1);
200                 }
201                 bzero(&kev_set, sizeof(kev_set));
202                 EV_SET(&kev_set, reader_fd, good_filter_types[i].fe_filter,
203                     EV_DELETE, 0, 0, 0);
204                 ret = kevent(kqueue_fd, &kev_set, 1, NULL, 0, &timeout);
205                 if (ret < 0) {
206                         warn("%s: kevent: deleting good filter %s", __func__,
207                             good_filter_types[i].fe_name);
208                         close(kqueue_fd);
209                         cleanfifo("testfifo", reader_fd, writer_fd);
210                         exit(-1);
211                 }
212         }
213
214         for (i = 0; i < bad_filter_types_len; i++) {
215                 bzero(&kev_set, sizeof(kev_set));
216                 EV_SET(&kev_set, reader_fd, bad_filter_types[i].fe_filter,
217                     EV_ADD, 0, 0, 0);
218                 ret = kevent(kqueue_fd, &kev_set, 1, NULL, 0, &timeout);
219                 if (ret >= 0) {
220                         warnx("%s: kevent: bad filter %s succeeded, expected "
221                             "EINVAL", __func__, bad_filter_types[i].fe_name);
222                         close(kqueue_fd);
223                         cleanfifo("testfifo", reader_fd, writer_fd);
224                         exit(-1);
225                 }
226                 if (errno != bad_filter_types[i].fe_error) {
227                         warn("%s: kevent: bad filter %s failed with error "
228                             "not %s", __func__,
229                             bad_filter_types[i].fe_name,
230                             bad_filter_types[i].fe_errorname);
231                         close(kqueue_fd);
232                         cleanfifo("testfifo", reader_fd, writer_fd);
233                         exit(-1);
234                 }
235         }
236
237         close(kqueue_fd);
238         cleanfifo("testfifo", reader_fd, writer_fd);
239 }
240
241 static int
242 test_ioctl_setclearflag(int fd, int flag, const char *testname,
243     const char *fdname, const char *flagname)
244 {
245         int i;
246
247         i = 1;
248         if (ioctl(fd, flag, &i) < 0) {
249                 warn("%s:%s: ioctl(%s, %s, 1)", testname, __func__, fdname,
250                     flagname);
251                 cleanfifo("testfifo", -1, -1);
252                 exit(-1);
253         }
254
255         i = 0;
256         if (ioctl(fd, flag, &i) < 0) {
257                 warn("%s:%s: ioctl(%s, %s, 0)", testname, __func__, fdname,
258                     flagname);
259                 cleanfifo("testfifo", -1, -1);
260                 exit(-1);
261         }
262
263         return (0);
264 }
265
266 /*
267  * Test that various ioctls can be issued against the file descriptor.  We
268  * don't currently test the semantics of these changes here.
269  */
270 static void
271 test_ioctl(void)
272 {
273         int reader_fd, writer_fd;
274
275         makefifo("testfifo", __func__);
276
277         if (openfifo("testfifo", __func__, &reader_fd, &writer_fd) < 0) {
278                 warn("%s: openfifo", __func__);
279                 cleanfifo("testfifo", -1, -1);
280                 exit(-1);
281         }
282
283         /*
284          * Set and remove the non-blocking I/O flag.
285          */
286         if (test_ioctl_setclearflag(reader_fd, FIONBIO, __func__,
287             "reader_fd", "FIONBIO") < 0) {
288                 cleanfifo("testfifo", reader_fd, writer_fd);
289                 exit(-1);
290         }
291
292         if (test_ioctl_setclearflag(writer_fd, FIONBIO, __func__,
293             "writer_fd", "FIONBIO") < 0) {
294                 cleanfifo("testfifo", reader_fd, writer_fd);
295                 exit(-1);
296         }
297
298         /*
299          * Set and remove the async I/O flag.
300          */
301         if (test_ioctl_setclearflag(reader_fd, FIOASYNC, __func__,
302             "reader_fd", "FIOASYNC") < 0) {
303                 cleanfifo("testfifo", reader_fd, writer_fd);
304                 exit(-1);
305         }
306
307         if (test_ioctl_setclearflag(writer_fd, FIOASYNC, __func__,
308             "writer_fd", "FIONASYNC") < 0) {
309                 cleanfifo("testfifo", reader_fd, writer_fd);
310                 exit(-1);
311         }
312
313         cleanfifo("testfifo", reader_fd, writer_fd);
314 }
315
316 int
317 main(int argc, char *argv[])
318 {
319
320         strcpy(temp_dir, "/tmp/fifo_misc.XXXXXXXXXXX");
321         if (mkdtemp(temp_dir) == NULL)
322                 err(-1, "mkdtemp");
323         atexit(atexit_temp_dir);
324
325         if (chdir(temp_dir) < 0)
326                 err(-1, "chdir %s", temp_dir);
327
328         test_lseek();
329         test_kqueue();
330         test_ioctl();
331
332         return (0);
333 }