xref: /freebsd/tests/sys/file/fcntlflags_test.c (revision 046c625e9382e17da953767b881aaa782fa73af8)
1 /*-
2  * Copyright (c) 2013 Jilles Tjoelker
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 
27 #include <sys/filio.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 
33 #include <atf-c.h>
34 
35 /*
36  * O_ACCMODE is currently defined incorrectly. This is what it should be.
37  * Various code depends on the incorrect value.
38  */
39 #define CORRECT_O_ACCMODE (O_ACCMODE | O_EXEC)
40 
41 static void
42 basic_tests(const char *path, int omode, const char *omodetext)
43 {
44 	int fd, flags1, flags2, flags3;
45 
46 	fd = open(path, omode);
47 	ATF_REQUIRE_MSG(fd != -1, "open(\"%s\", %s) failed: %s", path,
48 	    omodetext, strerror(errno));
49 
50 	flags1 = fcntl(fd, F_GETFL);
51 	ATF_REQUIRE_MSG(flags1 != -1, "fcntl(F_GETFL) (1) failed: %s",
52 	    strerror(errno));
53 	ATF_REQUIRE_INTEQ(omode, flags1 & CORRECT_O_ACCMODE);
54 	ATF_REQUIRE((flags1 & O_NONBLOCK) == 0);
55 
56 	ATF_REQUIRE_MSG(fcntl(fd, F_SETFL, flags1) != -1,
57 	    "fcntl(F_SETFL) same flags failed: %s", strerror(errno));
58 
59 	flags2 = fcntl(fd, F_GETFL);
60 	ATF_REQUIRE_MSG(flags2 != -1, "fcntl(F_GETFL) (2) failed: %s",
61 	    strerror(errno));
62 	ATF_REQUIRE_INTEQ(flags1, flags2);
63 
64 	ATF_REQUIRE_MSG(fcntl(fd, F_SETFL, flags2 | O_NONBLOCK) != -1,
65 	    "fcntl(F_SETFL) O_NONBLOCK failed: %s", strerror(errno));
66 
67 	flags3 = fcntl(fd, F_GETFL);
68 	ATF_REQUIRE_MSG(flags3 != -1, "fcntl(F_GETFL) (3) failed: %s",
69 	    strerror(errno));
70 	ATF_REQUIRE_INTEQ(flags2 | O_NONBLOCK, flags3);
71 
72 	(void)close(fd);
73 }
74 
75 ATF_TC_WITHOUT_HEAD(read_only_null);
76 ATF_TC_BODY(read_only_null, tc)
77 {
78 	basic_tests("/dev/null", O_RDONLY, "O_RDONLY");
79 }
80 
81 ATF_TC_WITHOUT_HEAD(write_only_null);
82 ATF_TC_BODY(write_only_null, tc)
83 {
84 	basic_tests("/dev/null", O_WRONLY, "O_WRONLY");
85 }
86 
87 ATF_TC_WITHOUT_HEAD(read_write_null);
88 ATF_TC_BODY(read_write_null, tc)
89 {
90 	basic_tests("/dev/null", O_RDWR, "O_RDWR");
91 }
92 
93 ATF_TC_WITHOUT_HEAD(exec_only_sh);
94 ATF_TC_BODY(exec_only_sh, tc)
95 {
96 	basic_tests("/bin/sh", O_EXEC, "O_EXEC");
97 }
98 
99 ATF_TC_WITHOUT_HEAD(fioasync_dev_null);
100 ATF_TC_BODY(fioasync_dev_null, tc)
101 {
102 	int fd, flags1, flags2, val;
103 
104 	fd = open("/dev/null", O_RDONLY);
105 	ATF_REQUIRE_MSG(fd != -1, "open(\"/dev/null\") failed: %s",
106 	    strerror(errno));
107 
108 	flags1 = fcntl(fd, F_GETFL);
109 	ATF_REQUIRE_MSG(flags1 != -1, "fcntl(F_GETFL) (1) failed: %s",
110 	    strerror(errno));
111 	ATF_REQUIRE((flags1 & O_ASYNC) == 0);
112 
113 	val = 1;
114 	ATF_REQUIRE_ERRNO(EINVAL, ioctl(fd, FIOASYNC, &val) == -1);
115 
116 	flags2 = fcntl(fd, F_GETFL);
117 	ATF_REQUIRE_MSG(flags2 != -1, "fcntl(F_GETFL) (2) failed: %s",
118 	    strerror(errno));
119 	ATF_REQUIRE_INTEQ(flags1, flags2);
120 
121 	(void)close(fd);
122 }
123 
124 ATF_TP_ADD_TCS(tp)
125 {
126 	ATF_TP_ADD_TC(tp, read_only_null);
127 	ATF_TP_ADD_TC(tp, write_only_null);
128 	ATF_TP_ADD_TC(tp, read_write_null);
129 	ATF_TP_ADD_TC(tp, exec_only_sh);
130 	ATF_TP_ADD_TC(tp, fioasync_dev_null);
131 
132 	return (atf_no_error());
133 }
134