xref: /freebsd/tests/sys/fifo/fifo_misc.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * Copyright (c) 2005 Robert N. M. Watson
3  * Copyright (c) 2012 Jilles Tjoelker
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/types.h>
29 #include <sys/event.h>
30 #include <sys/filio.h>
31 #include <sys/ioctl.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 static 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, int *reader_fdp, int *writer_fdp)
82 {
83 	int error, fd1, fd2;
84 
85 	fd1 = open(fifoname, O_RDONLY | O_NONBLOCK);
86 	if (fd1 < 0)
87 		return (-1);
88 	fd2 = open(fifoname, O_WRONLY | O_NONBLOCK);
89 	if (fd2 < 0) {
90 		error = errno;
91 		close(fd1);
92 		errno = error;
93 		return (-1);
94 	}
95 	*reader_fdp = fd1;
96 	*writer_fdp = fd2;
97 
98 	return (0);
99 }
100 
101 /*
102  * POSIX does not allow lseek(2) on fifos, so we expect ESPIPE as a result.
103  */
104 static void
105 test_lseek(void)
106 {
107 	int reader_fd, writer_fd;
108 
109 	makefifo("testfifo", __func__);
110 
111 	if (openfifo("testfifo", &reader_fd, &writer_fd) < 0) {
112 		warn("%s: openfifo", __func__);
113 		cleanfifo("testfifo", -1, -1);
114 		exit(-1);
115 	}
116 
117 	if (lseek(reader_fd, 1, SEEK_CUR) >= 0) {
118 		warnx("%s: lseek succeeded instead of returning ESPIPE",
119 		    __func__);
120 		cleanfifo("testfifo", reader_fd, writer_fd);
121 		exit(-1);
122 	}
123 	if (errno != ESPIPE) {
124 		warn("%s: lseek returned instead of ESPIPE", __func__);
125 		cleanfifo("testfifo", reader_fd, writer_fd);
126 		exit(-1);
127 	}
128 
129 	cleanfifo("testfifo", reader_fd, writer_fd);
130 }
131 
132 /*
133  * truncate(2) on FIFO should silently return success.
134  */
135 static void
136 test_truncate(void)
137 {
138 
139 	makefifo("testfifo", __func__);
140 
141 	if (truncate("testfifo", 1024) != 0) {
142 		warn("%s: truncate", __func__);
143 		cleanfifo("testfifo", -1, -1);
144 		exit(-1);
145 	}
146 
147 	cleanfifo("testfifo", -1, -1);
148 }
149 
150 static int
151 test_ioctl_setclearflag(int fd, unsigned long flag, const char *testname,
152     const char *fdname, const char *flagname)
153 {
154 	int i;
155 
156 	i = 1;
157 	if (ioctl(fd, flag, &i) < 0) {
158 		warn("%s:%s: ioctl(%s, %s, 1)", testname, __func__, fdname,
159 		    flagname);
160 		cleanfifo("testfifo", -1, -1);
161 		exit(-1);
162 	}
163 
164 	i = 0;
165 	if (ioctl(fd, flag, &i) < 0) {
166 		warn("%s:%s: ioctl(%s, %s, 0)", testname, __func__, fdname,
167 		    flagname);
168 		cleanfifo("testfifo", -1, -1);
169 		exit(-1);
170 	}
171 
172 	return (0);
173 }
174 
175 /*
176  * Test that various ioctls can be issued against the file descriptor.  We
177  * don't currently test the semantics of these changes here.
178  */
179 static void
180 test_ioctl(void)
181 {
182 	int reader_fd, writer_fd;
183 
184 	makefifo("testfifo", __func__);
185 
186 	if (openfifo("testfifo", &reader_fd, &writer_fd) < 0) {
187 		warn("%s: openfifo", __func__);
188 		cleanfifo("testfifo", -1, -1);
189 		exit(-1);
190 	}
191 
192 	/*
193 	 * Set and remove the non-blocking I/O flag.
194 	 */
195 	if (test_ioctl_setclearflag(reader_fd, FIONBIO, __func__,
196 	    "reader_fd", "FIONBIO") < 0) {
197 		cleanfifo("testfifo", reader_fd, writer_fd);
198 		exit(-1);
199 	}
200 
201 	if (test_ioctl_setclearflag(writer_fd, FIONBIO, __func__,
202 	    "writer_fd", "FIONBIO") < 0) {
203 		cleanfifo("testfifo", reader_fd, writer_fd);
204 		exit(-1);
205 	}
206 
207 	/*
208 	 * Set and remove the async I/O flag.
209 	 */
210 	if (test_ioctl_setclearflag(reader_fd, FIOASYNC, __func__,
211 	    "reader_fd", "FIOASYNC") < 0) {
212 		cleanfifo("testfifo", reader_fd, writer_fd);
213 		exit(-1);
214 	}
215 
216 	if (test_ioctl_setclearflag(writer_fd, FIOASYNC, __func__,
217 	    "writer_fd", "FIONASYNC") < 0) {
218 		cleanfifo("testfifo", reader_fd, writer_fd);
219 		exit(-1);
220 	}
221 
222 	cleanfifo("testfifo", reader_fd, writer_fd);
223 }
224 
225 /*
226  * fchmod(2)/fchown(2) on FIFO should work.
227  */
228 static void
229 test_chmodchown(void)
230 {
231 	struct stat sb;
232 	int reader_fd, writer_fd;
233 	uid_t u;
234 	gid_t g;
235 
236 	makefifo("testfifo", __func__);
237 
238 	if (openfifo("testfifo", &reader_fd, &writer_fd) < 0) {
239 		warn("%s: openfifo", __func__);
240 		cleanfifo("testfifo", -1, -1);
241 		exit(-1);
242 	}
243 
244 	if (fchmod(reader_fd, 0666) != 0) {
245 		warn("%s: fchmod", __func__);
246 		cleanfifo("testfifo", reader_fd, writer_fd);
247 		exit(-1);
248 	}
249 
250 	if (stat("testfifo", &sb) != 0) {
251 		warn("%s: stat", __func__);
252 		cleanfifo("testfifo", reader_fd, writer_fd);
253 		exit(-1);
254 	}
255 
256 	if ((sb.st_mode & 0777) != 0666) {
257 		warnx("%s: stat chmod result", __func__);
258 		cleanfifo("testfifo", reader_fd, writer_fd);
259 		exit(-1);
260 	}
261 
262 	if (fstat(writer_fd, &sb) != 0) {
263 		warn("%s: fstat", __func__);
264 		cleanfifo("testfifo", reader_fd, writer_fd);
265 		exit(-1);
266 	}
267 
268 	if ((sb.st_mode & 0777) != 0666) {
269 		warnx("%s: fstat chmod result", __func__);
270 		cleanfifo("testfifo", reader_fd, writer_fd);
271 		exit(-1);
272 	}
273 
274 	if (fchown(reader_fd, -1, -1) != 0) {
275 		warn("%s: fchown 1", __func__);
276 		cleanfifo("testfifo", reader_fd, writer_fd);
277 		exit(-1);
278 	}
279 
280 	u = geteuid();
281 	if (u == 0)
282 		u = 1;
283 	g = getegid();
284 	if (fchown(reader_fd, u, g) != 0) {
285 		warn("%s: fchown 2", __func__);
286 		cleanfifo("testfifo", reader_fd, writer_fd);
287 		exit(-1);
288 	}
289 	if (stat("testfifo", &sb) != 0) {
290 		warn("%s: stat", __func__);
291 		cleanfifo("testfifo", reader_fd, writer_fd);
292 		exit(-1);
293 	}
294 
295 	if (sb.st_uid != u || sb.st_gid != g) {
296 		warnx("%s: stat chown result", __func__);
297 		cleanfifo("testfifo", reader_fd, writer_fd);
298 		exit(-1);
299 	}
300 
301 	if (fstat(writer_fd, &sb) != 0) {
302 		warn("%s: fstat", __func__);
303 		cleanfifo("testfifo", reader_fd, writer_fd);
304 		exit(-1);
305 	}
306 
307 	if (sb.st_uid != u || sb.st_gid != g) {
308 		warnx("%s: fstat chown result", __func__);
309 		cleanfifo("testfifo", reader_fd, writer_fd);
310 		exit(-1);
311 	}
312 
313 	cleanfifo("testfifo", -1, -1);
314 }
315 
316 int
317 main(void)
318 {
319 
320 	strcpy(temp_dir, "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_truncate();
330 	test_ioctl();
331 	test_chmodchown();
332 
333 	return (0);
334 }
335