2 * Copyright (c) 2005 Robert N. M. Watson
3 * Copyright (c) 2012 Jilles Tjoelker
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/types.h>
31 #include <sys/event.h>
32 #include <sys/filio.h>
33 #include <sys/ioctl.h>
47 * Regression test for piddling details of fifos.
51 * All activity occurs within a temporary directory created early in the
54 static char temp_dir[PATH_MAX];
64 makefifo(const char *fifoname, const char *testname)
67 if (mkfifo(fifoname, 0700) < 0)
68 err(-1, "%s: makefifo: mkfifo: %s", testname, fifoname);
72 cleanfifo(const char *fifoname, int fd1, int fd2)
79 (void)unlink(fifoname);
83 openfifo(const char *fifoname, int *reader_fdp, int *writer_fdp)
87 fd1 = open(fifoname, O_RDONLY | O_NONBLOCK);
90 fd2 = open(fifoname, O_WRONLY | O_NONBLOCK);
104 * POSIX does not allow lseek(2) on fifos, so we expect ESPIPE as a result.
109 int reader_fd, writer_fd;
111 makefifo("testfifo", __func__);
113 if (openfifo("testfifo", &reader_fd, &writer_fd) < 0) {
114 warn("%s: openfifo", __func__);
115 cleanfifo("testfifo", -1, -1);
119 if (lseek(reader_fd, 1, SEEK_CUR) >= 0) {
120 warnx("%s: lseek succeeded instead of returning ESPIPE",
122 cleanfifo("testfifo", reader_fd, writer_fd);
125 if (errno != ESPIPE) {
126 warn("%s: lseek returned instead of ESPIPE", __func__);
127 cleanfifo("testfifo", reader_fd, writer_fd);
131 cleanfifo("testfifo", reader_fd, writer_fd);
135 * truncate(2) on FIFO should silently return success.
141 makefifo("testfifo", __func__);
143 if (truncate("testfifo", 1024) != 0) {
144 warn("%s: truncate", __func__);
145 cleanfifo("testfifo", -1, -1);
149 cleanfifo("testfifo", -1, -1);
153 test_ioctl_setclearflag(int fd, unsigned long flag, const char *testname,
154 const char *fdname, const char *flagname)
159 if (ioctl(fd, flag, &i) < 0) {
160 warn("%s:%s: ioctl(%s, %s, 1)", testname, __func__, fdname,
162 cleanfifo("testfifo", -1, -1);
167 if (ioctl(fd, flag, &i) < 0) {
168 warn("%s:%s: ioctl(%s, %s, 0)", testname, __func__, fdname,
170 cleanfifo("testfifo", -1, -1);
178 * Test that various ioctls can be issued against the file descriptor. We
179 * don't currently test the semantics of these changes here.
184 int reader_fd, writer_fd;
186 makefifo("testfifo", __func__);
188 if (openfifo("testfifo", &reader_fd, &writer_fd) < 0) {
189 warn("%s: openfifo", __func__);
190 cleanfifo("testfifo", -1, -1);
195 * Set and remove the non-blocking I/O flag.
197 if (test_ioctl_setclearflag(reader_fd, FIONBIO, __func__,
198 "reader_fd", "FIONBIO") < 0) {
199 cleanfifo("testfifo", reader_fd, writer_fd);
203 if (test_ioctl_setclearflag(writer_fd, FIONBIO, __func__,
204 "writer_fd", "FIONBIO") < 0) {
205 cleanfifo("testfifo", reader_fd, writer_fd);
210 * Set and remove the async I/O flag.
212 if (test_ioctl_setclearflag(reader_fd, FIOASYNC, __func__,
213 "reader_fd", "FIOASYNC") < 0) {
214 cleanfifo("testfifo", reader_fd, writer_fd);
218 if (test_ioctl_setclearflag(writer_fd, FIOASYNC, __func__,
219 "writer_fd", "FIONASYNC") < 0) {
220 cleanfifo("testfifo", reader_fd, writer_fd);
224 cleanfifo("testfifo", reader_fd, writer_fd);
228 * fchmod(2)/fchown(2) on FIFO should work.
231 test_chmodchown(void)
234 int reader_fd, writer_fd;
238 makefifo("testfifo", __func__);
240 if (openfifo("testfifo", &reader_fd, &writer_fd) < 0) {
241 warn("%s: openfifo", __func__);
242 cleanfifo("testfifo", -1, -1);
246 if (fchmod(reader_fd, 0666) != 0) {
247 warn("%s: fchmod", __func__);
248 cleanfifo("testfifo", reader_fd, writer_fd);
252 if (stat("testfifo", &sb) != 0) {
253 warn("%s: stat", __func__);
254 cleanfifo("testfifo", reader_fd, writer_fd);
258 if ((sb.st_mode & 0777) != 0666) {
259 warnx("%s: stat chmod result", __func__);
260 cleanfifo("testfifo", reader_fd, writer_fd);
264 if (fstat(writer_fd, &sb) != 0) {
265 warn("%s: fstat", __func__);
266 cleanfifo("testfifo", reader_fd, writer_fd);
270 if ((sb.st_mode & 0777) != 0666) {
271 warnx("%s: fstat chmod result", __func__);
272 cleanfifo("testfifo", reader_fd, writer_fd);
276 if (fchown(reader_fd, -1, -1) != 0) {
277 warn("%s: fchown 1", __func__);
278 cleanfifo("testfifo", reader_fd, writer_fd);
286 if (fchown(reader_fd, u, g) != 0) {
287 warn("%s: fchown 2", __func__);
288 cleanfifo("testfifo", reader_fd, writer_fd);
291 if (stat("testfifo", &sb) != 0) {
292 warn("%s: stat", __func__);
293 cleanfifo("testfifo", reader_fd, writer_fd);
297 if (sb.st_uid != u || sb.st_gid != g) {
298 warnx("%s: stat chown result", __func__);
299 cleanfifo("testfifo", reader_fd, writer_fd);
303 if (fstat(writer_fd, &sb) != 0) {
304 warn("%s: fstat", __func__);
305 cleanfifo("testfifo", reader_fd, writer_fd);
309 if (sb.st_uid != u || sb.st_gid != g) {
310 warnx("%s: fstat chown result", __func__);
311 cleanfifo("testfifo", reader_fd, writer_fd);
315 cleanfifo("testfifo", -1, -1);
322 strcpy(temp_dir, "fifo_misc.XXXXXXXXXXX");
323 if (mkdtemp(temp_dir) == NULL)
325 atexit(atexit_temp_dir);
327 if (chdir(temp_dir) < 0)
328 err(-1, "chdir %s", temp_dir);