1 /* $NetBSD: t_write.c,v 1.7 2019/07/16 17:29:18 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __COPYRIGHT("@(#) Copyright (c) 2008\
31 The NetBSD Foundation, inc. All rights reserved.");
32 __RCSID("$NetBSD: t_write.c,v 1.7 2019/07/16 17:29:18 martin Exp $");
33
34 #include <sys/uio.h>
35 #include <sys/mman.h>
36
37 #include <atf-c.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <signal.h>
41 #include <limits.h>
42 #include <stdio.h>
43 #include <stdint.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <paths.h>
47
48 static void sighandler(int);
49
50 static bool fail = false;
51 static const char *path = "write";
52
53 static void
sighandler(int signo __unused)54 sighandler(int signo __unused)
55 {
56 fail = false;
57 }
58
59 ATF_TC_WITH_CLEANUP(write_err);
ATF_TC_HEAD(write_err,tc)60 ATF_TC_HEAD(write_err, tc)
61 {
62 atf_tc_set_md_var(tc, "descr", "Checks errors from write(2)");
63 }
64
ATF_TC_BODY(write_err,tc)65 ATF_TC_BODY(write_err, tc)
66 {
67 char rbuf[3] = { 'a', 'b', 'c' };
68 char wbuf[3] = { 'x', 'y', 'z' };
69 int fd;
70
71 errno = 0;
72 ATF_REQUIRE_ERRNO(EBADF, write(-1, wbuf, sizeof(wbuf)) == -1);
73
74 fd = open(path, O_RDWR | O_CREAT, 0600);
75
76 if (fd >= 0) {
77
78 errno = 0;
79 ATF_REQUIRE_ERRNO(0, write(fd, wbuf, 3) == 3);
80
81 errno = 0;
82 ATF_REQUIRE_ERRNO(EINVAL, write(fd, wbuf, SIZE_MAX) == -1);
83
84 errno = 0;
85 ATF_REQUIRE_ERRNO(EFAULT, write(fd, (void *)-1, 1) == -1);
86
87 /*
88 * Check that the above bogus write(2)
89 * calls did not corrupt the file.
90 */
91 ATF_REQUIRE(lseek(fd, 0, SEEK_SET) == 0);
92 ATF_REQUIRE(read(fd, rbuf, 3) == 3);
93 ATF_REQUIRE(memcmp(rbuf, wbuf, 3) == 0);
94
95 (void)close(fd);
96 (void)unlink(path);
97 }
98 }
99
ATF_TC_CLEANUP(write_err,tc)100 ATF_TC_CLEANUP(write_err, tc)
101 {
102 (void)unlink(path);
103 }
104
105 ATF_TC(write_pipe);
ATF_TC_HEAD(write_pipe,tc)106 ATF_TC_HEAD(write_pipe, tc)
107 {
108 atf_tc_set_md_var(tc, "descr", "Checks for EPIPE from write(2)");
109 }
110
ATF_TC_BODY(write_pipe,tc)111 ATF_TC_BODY(write_pipe, tc)
112 {
113 int fds[2];
114
115 ATF_REQUIRE(pipe(fds) == 0);
116 ATF_REQUIRE(signal(SIGPIPE, sighandler) == 0);
117
118 ATF_REQUIRE(write(fds[1], "x", 1) != -1);
119 ATF_REQUIRE(close(fds[0]) == 0);
120
121 errno = 0;
122 fail = true;
123
124 if (write(fds[1], "x", 1) != -1 || errno != EPIPE)
125 atf_tc_fail_nonfatal("expected EPIPE but write(2) succeeded");
126
127 ATF_REQUIRE(close(fds[1]) == 0);
128
129 if (fail != false)
130 atf_tc_fail_nonfatal("SIGPIPE was not raised");
131 }
132
133 ATF_TC_WITH_CLEANUP(write_pos);
ATF_TC_HEAD(write_pos,tc)134 ATF_TC_HEAD(write_pos, tc)
135 {
136 atf_tc_set_md_var(tc, "descr", "Checks that write(2) "
137 "updates the file position");
138 }
139
ATF_TC_BODY(write_pos,tc)140 ATF_TC_BODY(write_pos, tc)
141 {
142 const size_t n = 123;
143 size_t i;
144 int fd;
145
146 fd = open(path, O_RDWR | O_CREAT, 0600);
147 ATF_REQUIRE(fd >= 0);
148
149 for (i = 0; i < n; i++) {
150 ATF_REQUIRE(write(fd, "x", 1) == 1);
151 ATF_REQUIRE(lseek(fd, 0, SEEK_CUR) == (off_t)(i + 1));
152 }
153
154 ATF_REQUIRE(close(fd) == 0);
155 ATF_REQUIRE(unlink(path) == 0);
156 }
157
ATF_TC_CLEANUP(write_pos,tc)158 ATF_TC_CLEANUP(write_pos, tc)
159 {
160 (void)unlink(path);
161 }
162
163 ATF_TC_WITH_CLEANUP(write_ret);
ATF_TC_HEAD(write_ret,tc)164 ATF_TC_HEAD(write_ret, tc)
165 {
166 atf_tc_set_md_var(tc, "descr", "Checks return values from write(2)");
167 }
168
ATF_TC_BODY(write_ret,tc)169 ATF_TC_BODY(write_ret, tc)
170 {
171 const size_t n = 99;
172 char buf[123];
173 size_t i, j;
174 int fd;
175
176 fd = open(path, O_WRONLY | O_CREAT, 0600);
177 ATF_REQUIRE(fd >= 0);
178
179 (void)memset(buf, 'x', sizeof(buf));
180
181 for (i = j = 0; i < n; i++)
182 j += write(fd, buf, sizeof(buf));
183
184 if (j != n * 123)
185 atf_tc_fail("inconsistent return values from write(2)");
186
187 (void)close(fd);
188 (void)unlink(path);
189 }
190
ATF_TC_CLEANUP(write_ret,tc)191 ATF_TC_CLEANUP(write_ret, tc)
192 {
193 (void)unlink(path);
194 }
195
196 ATF_TC(writev_iovmax);
ATF_TC_HEAD(writev_iovmax,tc)197 ATF_TC_HEAD(writev_iovmax, tc)
198 {
199 atf_tc_set_md_var(tc, "timeout", "10");
200 atf_tc_set_md_var(tc, "descr",
201 "Checks that file descriptor is properly FILE_UNUSE()d "
202 "when iovcnt is greater than IOV_MAX");
203 }
204
ATF_TC_BODY(writev_iovmax,tc)205 ATF_TC_BODY(writev_iovmax, tc)
206 {
207 ssize_t retval;
208
209 (void)printf("Calling writev(2, NULL, IOV_MAX + 1)...\n");
210
211 errno = 0;
212 retval = writev(2, NULL, IOV_MAX + 1);
213
214 ATF_REQUIRE_EQ_MSG(retval, -1, "got: %zd", retval);
215 ATF_REQUIRE_EQ_MSG(errno, EINVAL, "got: %s", strerror(errno));
216 }
217
218 ATF_TC(write_fault);
219
ATF_TC_HEAD(write_fault,tc)220 ATF_TC_HEAD(write_fault, tc)
221 {
222 atf_tc_set_md_var(tc, "descr",
223 "Check that writing to non-permitted space returns EFAULT");
224 }
225
226 #define SIZE 8192
227
ATF_TC_BODY(write_fault,tc)228 ATF_TC_BODY(write_fault, tc)
229 {
230 int fd[2];
231 ATF_REQUIRE(pipe(fd) != -1);
232 // Can't use /dev/null cause it doesn't access the buffer.
233
234 void *map = mmap(NULL, SIZE, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
235 ATF_REQUIRE(map != MAP_FAILED);
236
237 ssize_t retval = write(fd[1], map, SIZE);
238
239 ATF_REQUIRE_EQ_MSG(retval, -1, "got: %zd", retval);
240 ATF_REQUIRE_EQ_MSG(errno, EFAULT, "got: %s", strerror(errno));
241
242 munmap(map, SIZE);
243 close(fd[0]);
244 close(fd[1]);
245 }
246
247 ATF_TC(read_fault);
248
ATF_TC_HEAD(read_fault,tc)249 ATF_TC_HEAD(read_fault, tc)
250 {
251 atf_tc_set_md_var(tc, "descr",
252 "Check that reading from non-permitted space returns EFAULT");
253 }
254
ATF_TC_BODY(read_fault,tc)255 ATF_TC_BODY(read_fault, tc)
256 {
257 int fd = open(_PATH_DEVZERO, O_RDONLY);
258 ATF_REQUIRE(fd != -1);
259
260 void *map = mmap(NULL, SIZE, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
261 ATF_REQUIRE(map != MAP_FAILED);
262
263 ssize_t retval = read(fd, map, SIZE);
264
265 ATF_REQUIRE_EQ_MSG(retval, -1, "got: %zd", retval);
266 ATF_REQUIRE_EQ_MSG(errno, EFAULT, "got: %s", strerror(errno));
267
268 munmap(map, SIZE);
269 close(fd);
270 }
271
ATF_TP_ADD_TCS(tp)272 ATF_TP_ADD_TCS(tp)
273 {
274
275 ATF_TP_ADD_TC(tp, write_err);
276 ATF_TP_ADD_TC(tp, write_pipe);
277 ATF_TP_ADD_TC(tp, write_pos);
278 ATF_TP_ADD_TC(tp, write_ret);
279 ATF_TP_ADD_TC(tp, writev_iovmax);
280 ATF_TP_ADD_TC(tp, write_fault);
281 ATF_TP_ADD_TC(tp, read_fault);
282
283 return atf_no_error();
284 }
285