blob: 4f6fc7f7ec3c29b0b4266926c7f7bf8521744339 [file] [log] [blame]
Elliott Hughes06209252013-11-06 16:20:54 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <errno.h>
20#include <fcntl.h>
Nick Desaulniers094f3162016-07-19 15:20:24 -070021#include <string.h>
22#include <sys/utsname.h>
Matthias Hausner47a52102017-06-02 10:09:19 -070023#include <sys/vfs.h>
Elliott Hughes06209252013-11-06 16:20:54 -080024
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080025#include <android-base/file.h>
Elliott Hughesdfe67d22022-02-17 12:55:42 -080026#include <android-base/silent_death_test.h>
Elliott Hughesb115aef2017-08-04 09:34:19 -070027#include <android-base/stringprintf.h>
28
Colin Cross7da20342021-07-28 11:18:11 -070029#if !defined(EXT4_SUPER_MAGIC)
Matthias Hausner47a52102017-06-02 10:09:19 -070030#include <linux/magic.h>
Nick Desaulniers094f3162016-07-19 15:20:24 -070031#endif
32
Elliott Hughes95646e62023-09-21 14:11:19 -070033#include "utils.h"
34
Elliott Hughesdfe67d22022-02-17 12:55:42 -080035using fcntl_DeathTest = SilentDeathTest;
36
Elliott Hughes06209252013-11-06 16:20:54 -080037TEST(fcntl, fcntl_smoke) {
38 int fd = open("/proc/version", O_RDONLY);
39 ASSERT_TRUE(fd != -1);
40
41 int flags = fcntl(fd, F_GETFD);
42 ASSERT_TRUE(flags != -1);
43 ASSERT_EQ(0, flags & FD_CLOEXEC);
44
45 int rc = fcntl(fd, F_SETFD, FD_CLOEXEC);
46 ASSERT_EQ(0, rc);
47
48 flags = fcntl(fd, F_GETFD);
49 ASSERT_TRUE(flags != -1);
50 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
Elliott Hughesdb1ea342014-01-17 18:42:49 -080051
52 close(fd);
53}
54
55TEST(fcntl, open_open64) {
56 int fd;
57
58 fd = open("/proc/version", O_RDONLY);
59 ASSERT_TRUE(fd != -1);
60 close(fd);
61
62 fd = open64("/proc/version", O_RDONLY);
63 ASSERT_TRUE(fd != -1);
64 close(fd);
65}
66
67TEST(fcntl, openat_openat64) {
68 int fd;
69
70 fd = openat(AT_FDCWD, "/proc/version", O_RDONLY);
71 ASSERT_TRUE(fd != -1);
72 close(fd);
73
74 fd = openat64(AT_FDCWD, "/proc/version", O_RDONLY);
75 ASSERT_TRUE(fd != -1);
76 close(fd);
77}
78
79TEST(fcntl, creat_creat64) {
80 ASSERT_EQ(-1, creat("", 0666));
Elliott Hughes95646e62023-09-21 14:11:19 -070081 ASSERT_ERRNO(ENOENT);
Elliott Hughesdb1ea342014-01-17 18:42:49 -080082 ASSERT_EQ(-1, creat64("", 0666));
Elliott Hughes95646e62023-09-21 14:11:19 -070083 ASSERT_ERRNO(ENOENT);
Elliott Hughes06209252013-11-06 16:20:54 -080084}
Elliott Hughesf64b8ea2014-02-03 16:20:46 -080085
Elliott Hughesb587f332014-09-10 17:39:00 -070086TEST(fcntl, posix_fadvise) {
87 TemporaryFile tf;
88 errno = 0;
89
90 EXPECT_EQ(EBADF, posix_fadvise(-1, 0, 0, POSIX_FADV_NORMAL));
Elliott Hughes95646e62023-09-21 14:11:19 -070091 EXPECT_ERRNO(0);
Elliott Hughesb587f332014-09-10 17:39:00 -070092
93 EXPECT_EQ(EBADF, posix_fadvise64(-1, 0, 0, POSIX_FADV_NORMAL));
Elliott Hughes95646e62023-09-21 14:11:19 -070094 EXPECT_ERRNO(0);
Elliott Hughesb587f332014-09-10 17:39:00 -070095
96 EXPECT_EQ(EINVAL, posix_fadvise(tf.fd, 0, 0, -1));
Elliott Hughes95646e62023-09-21 14:11:19 -070097 EXPECT_ERRNO(0);
Elliott Hughesb587f332014-09-10 17:39:00 -070098
99 EXPECT_EQ(EINVAL, posix_fadvise64(tf.fd, 0, 0, -1));
Elliott Hughes95646e62023-09-21 14:11:19 -0700100 EXPECT_ERRNO(0);
Elliott Hughesb587f332014-09-10 17:39:00 -0700101
102 EXPECT_EQ(0, posix_fadvise(tf.fd, 0, 0, POSIX_FADV_NORMAL));
103 EXPECT_EQ(0, posix_fadvise64(tf.fd, 0, 0, POSIX_FADV_NORMAL));
104}
105
Elliott Hughesf64b8ea2014-02-03 16:20:46 -0800106TEST(fcntl, fallocate_EINVAL) {
107 TemporaryFile tf;
108
Elliott Hughesb587f332014-09-10 17:39:00 -0700109 // fallocate/fallocate64 set errno.
110 // posix_fallocate/posix_fallocate64 return an errno value.
111
Elliott Hughesf64b8ea2014-02-03 16:20:46 -0800112 errno = 0;
113 ASSERT_EQ(-1, fallocate(tf.fd, 0, 0, -1));
Elliott Hughes95646e62023-09-21 14:11:19 -0700114 ASSERT_ERRNO(EINVAL);
Elliott Hughesf64b8ea2014-02-03 16:20:46 -0800115
116 errno = 0;
117 ASSERT_EQ(-1, fallocate64(tf.fd, 0, 0, -1));
Elliott Hughes95646e62023-09-21 14:11:19 -0700118 ASSERT_ERRNO(EINVAL);
Elliott Hughesf64b8ea2014-02-03 16:20:46 -0800119
120 errno = 0;
121 ASSERT_EQ(EINVAL, posix_fallocate(tf.fd, 0, -1));
Elliott Hughes95646e62023-09-21 14:11:19 -0700122 ASSERT_ERRNO(0);
Elliott Hughesf64b8ea2014-02-03 16:20:46 -0800123
124 errno = 0;
125 ASSERT_EQ(EINVAL, posix_fallocate64(tf.fd, 0, -1));
Elliott Hughes95646e62023-09-21 14:11:19 -0700126 ASSERT_ERRNO(0);
Elliott Hughesf64b8ea2014-02-03 16:20:46 -0800127}
128
129TEST(fcntl, fallocate) {
130 TemporaryFile tf;
131 struct stat sb;
132 ASSERT_EQ(0, fstat(tf.fd, &sb));
133 ASSERT_EQ(0, sb.st_size);
134
Elliott Hughes063525c2014-05-13 11:19:57 -0700135#if defined(__BIONIC__)
Elliott Hughesf64b8ea2014-02-03 16:20:46 -0800136 ASSERT_EQ(0, fallocate(tf.fd, 0, 0, 1));
137 ASSERT_EQ(0, fstat(tf.fd, &sb));
138 ASSERT_EQ(1, sb.st_size);
139
140 ASSERT_EQ(0, fallocate64(tf.fd, 0, 0, 2));
141 ASSERT_EQ(0, fstat(tf.fd, &sb));
142 ASSERT_EQ(2, sb.st_size);
143#endif
144
145 ASSERT_EQ(0, posix_fallocate(tf.fd, 0, 3));
146 ASSERT_EQ(0, fstat(tf.fd, &sb));
147 ASSERT_EQ(3, sb.st_size);
148
149 ASSERT_EQ(0, posix_fallocate64(tf.fd, 0, 4));
150 ASSERT_EQ(0, fstat(tf.fd, &sb));
151 ASSERT_EQ(4, sb.st_size);
152}
Serban Constantinescu48501af2014-03-14 13:16:25 +0000153
Elliott Hughes09e77f32020-01-29 19:20:45 -0800154TEST(fcntl, f_getlk) {
155 int fd = open("/proc/version", O_RDONLY);
156 ASSERT_TRUE(fd != -1);
157
158 struct flock check_lock;
159 check_lock.l_type = F_WRLCK;
160 check_lock.l_start = 0;
161 check_lock.l_whence = SEEK_SET;
162 check_lock.l_len = 0;
163
164 ASSERT_EQ(0, fcntl(fd, F_GETLK, &check_lock));
165 close(fd);
166}
167
Serban Constantinescu48501af2014-03-14 13:16:25 +0000168TEST(fcntl, f_getlk64) {
169 int fd = open64("/proc/version", O_RDONLY);
170 ASSERT_TRUE(fd != -1);
171
172 struct flock64 check_lock;
173 check_lock.l_type = F_WRLCK;
174 check_lock.l_start = 0;
175 check_lock.l_whence = SEEK_SET;
176 check_lock.l_len = 0;
177
Elliott Hughes09e77f32020-01-29 19:20:45 -0800178 ASSERT_EQ(0, fcntl(fd, F_GETLK64, &check_lock));
Serban Constantinescu48501af2014-03-14 13:16:25 +0000179 close(fd);
180}
Elliott Hughes3f525d42014-06-24 16:32:01 -0700181
182TEST(fcntl, splice) {
183 int pipe_fds[2];
184 ASSERT_EQ(0, pipe(pipe_fds));
185
186 int in = open("/proc/cpuinfo", O_RDONLY);
187 ASSERT_NE(in, -1);
188
189 TemporaryFile tf;
190
Yi Kong32bc0fc2018-08-02 17:31:13 -0700191 ssize_t bytes_read = splice(in, nullptr, pipe_fds[1], nullptr, 8*1024, SPLICE_F_MORE | SPLICE_F_MOVE);
Elliott Hughes3f525d42014-06-24 16:32:01 -0700192 ASSERT_NE(bytes_read, -1);
193
Yi Kong32bc0fc2018-08-02 17:31:13 -0700194 ssize_t bytes_written = splice(pipe_fds[0], nullptr, tf.fd, nullptr, bytes_read, SPLICE_F_MORE | SPLICE_F_MOVE);
Elliott Hughes3f525d42014-06-24 16:32:01 -0700195 ASSERT_EQ(bytes_read, bytes_written);
196
197 close(pipe_fds[0]);
198 close(pipe_fds[1]);
199 close(in);
200}
201
202TEST(fcntl, vmsplice) {
203 int pipe_fds[2];
204 ASSERT_EQ(0, pipe(pipe_fds));
205
206 iovec v[2];
207 v[0].iov_base = const_cast<char*>("hello ");
208 v[0].iov_len = 6;
209 v[1].iov_base = const_cast<char*>("world\n");
210 v[1].iov_len = 6;
211 ssize_t bytes_written = vmsplice(pipe_fds[1], v, sizeof(v)/sizeof(iovec), 0);
212 ASSERT_EQ(v[0].iov_len + v[1].iov_len, static_cast<size_t>(bytes_written));
213 close(pipe_fds[1]);
214
215 char buf[BUFSIZ];
216 FILE* fp = fdopen(pipe_fds[0], "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700217 ASSERT_TRUE(fp != nullptr);
218 ASSERT_TRUE(fgets(buf, sizeof(buf), fp) != nullptr);
Elliott Hughes3f525d42014-06-24 16:32:01 -0700219 fclose(fp);
220 ASSERT_STREQ("hello world\n", buf);
221}
222
223TEST(fcntl, tee) {
Kazuhiro Inabaf7a8e952017-09-25 15:18:29 +0900224 char expected[BUFSIZ];
Elliott Hughes3f525d42014-06-24 16:32:01 -0700225 FILE* expected_fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700226 ASSERT_TRUE(expected_fp != nullptr);
227 ASSERT_TRUE(fgets(expected, sizeof(expected), expected_fp) != nullptr);
Elliott Hughes3f525d42014-06-24 16:32:01 -0700228 fclose(expected_fp);
229
230 int pipe1[2];
231 ASSERT_EQ(0, pipe(pipe1));
232
233 int pipe2[2];
234 ASSERT_EQ(0, pipe(pipe2));
235
236 int in = open("/proc/version", O_RDONLY);
237 ASSERT_NE(in, -1);
238
239 // Write /proc/version into pipe1.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700240 ssize_t bytes_read = splice(in, nullptr, pipe1[1], nullptr, 8*1024, SPLICE_F_MORE | SPLICE_F_MOVE);
Elliott Hughes3f525d42014-06-24 16:32:01 -0700241 ASSERT_NE(bytes_read, -1);
242 close(pipe1[1]);
243
244 // Tee /proc/version from pipe1 into pipe2.
245 ssize_t bytes_teed = tee(pipe1[0], pipe2[1], SIZE_MAX, 0);
246 ASSERT_EQ(bytes_read, bytes_teed);
247 close(pipe2[1]);
248
249 // The out fds of both pipe1 and pipe2 should now contain /proc/version.
250 char buf1[BUFSIZ];
251 FILE* fp1 = fdopen(pipe1[0], "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700252 ASSERT_TRUE(fp1 != nullptr);
253 ASSERT_TRUE(fgets(buf1, sizeof(buf1), fp1) != nullptr);
Elliott Hughes3f525d42014-06-24 16:32:01 -0700254 fclose(fp1);
255
256 char buf2[BUFSIZ];
257 FILE* fp2 = fdopen(pipe2[0], "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700258 ASSERT_TRUE(fp2 != nullptr);
259 ASSERT_TRUE(fgets(buf2, sizeof(buf2), fp2) != nullptr);
Elliott Hughes3f525d42014-06-24 16:32:01 -0700260 fclose(fp2);
261
262 ASSERT_STREQ(expected, buf1);
263 ASSERT_STREQ(expected, buf2);
264}
Elliott Hughes55147ad2016-04-05 11:06:02 -0700265
266TEST(fcntl, readahead) {
267 // Just check that the function is available.
268 errno = 0;
269 ASSERT_EQ(-1, readahead(-1, 0, 123));
Elliott Hughes95646e62023-09-21 14:11:19 -0700270 ASSERT_ERRNO(EBADF);
Elliott Hughes55147ad2016-04-05 11:06:02 -0700271}
Elliott Hughes7f72ad42016-04-05 11:56:03 -0700272
273TEST(fcntl, sync_file_range) {
274 // Just check that the function is available.
275 errno = 0;
276 ASSERT_EQ(-1, sync_file_range(-1, 0, 0, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700277 ASSERT_ERRNO(EBADF);
Elliott Hughes7f72ad42016-04-05 11:56:03 -0700278
279 TemporaryFile tf;
280 ASSERT_EQ(0, sync_file_range(tf.fd, 0, 0, 0));
281
282 // The arguments to the underlying system call are in a different order on 32-bit ARM.
283 // Check that the `flags` argument gets passed to the kernel correctly.
284 errno = 0;
285 ASSERT_EQ(-1, sync_file_range(tf.fd, 0, 0, ~0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700286 ASSERT_ERRNO(EINVAL);
Elliott Hughes7f72ad42016-04-05 11:56:03 -0700287}
Nick Desaulniers094f3162016-07-19 15:20:24 -0700288
289static bool parse_kernel_release(long* const major, long* const minor) {
290 struct utsname buf;
291 if (uname(&buf) == -1) {
292 return false;
293 }
294 return sscanf(buf.release, "%ld.%ld", major, minor) == 2;
295}
296
297/*
Josh Gao24ed8b52017-06-02 14:57:49 -0700298 * b/28760453:
299 * Kernels older than 4.1 should have ext4 FALLOC_FL_PUNCH_HOLE disabled due to CVE-2015-8839.
300 * Devices that fail this test should cherry-pick the following commit:
301 * https://android.googlesource.com/kernel/msm/+/bdba352e898cbf57c8620ad68c8abf749c784d1f
Nick Desaulniers094f3162016-07-19 15:20:24 -0700302 */
303TEST(fcntl, falloc_punch) {
304 long major = 0, minor = 0;
305 ASSERT_TRUE(parse_kernel_release(&major, &minor));
306
307 if (major < 4 || (major == 4 && minor < 1)) {
308 TemporaryFile tf;
Matthias Hausner47a52102017-06-02 10:09:19 -0700309 struct statfs sfs;
310 ASSERT_EQ(0, fstatfs(tf.fd, &sfs));
311 if (sfs.f_type == EXT4_SUPER_MAGIC) {
312 ASSERT_EQ(-1, fallocate(tf.fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 1));
Elliott Hughes95646e62023-09-21 14:11:19 -0700313 ASSERT_ERRNO(EOPNOTSUPP);
Matthias Hausner47a52102017-06-02 10:09:19 -0700314 }
Nick Desaulniers094f3162016-07-19 15:20:24 -0700315 }
316}
Elliott Hughesb115aef2017-08-04 09:34:19 -0700317
318TEST(fcntl, open_O_TMPFILE_mode) {
Elliott Hughesb115aef2017-08-04 09:34:19 -0700319 TemporaryDir dir;
320 // Without O_EXCL, we're allowed to give this a name later.
321 // (This is unrelated to the O_CREAT interaction with O_EXCL.)
322 const mode_t perms = S_IRUSR | S_IWUSR;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800323 int fd = open(dir.path, O_TMPFILE | O_RDWR, perms);
Elliott Hughesb115aef2017-08-04 09:34:19 -0700324
325 // Ignore kernels without O_TMPFILE support (< 3.11).
326 if (fd == -1 && (errno == EISDIR || errno == EINVAL || errno == EOPNOTSUPP)) return;
327
328 ASSERT_TRUE(fd != -1) << strerror(errno);
329
330 // Does the fd claim to have the mode we set?
331 struct stat sb = {};
332 ASSERT_EQ(0, fstat(fd, &sb));
333 ASSERT_EQ(perms, (sb.st_mode & ~S_IFMT));
334
Elliott Hughesfc30bfe2017-10-24 22:54:34 -0700335 // On Android if we're not root, we won't be able to create links anyway...
336 if (getuid() != 0) return;
337
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800338 std::string final_path = android::base::StringPrintf("%s/named_now", dir.path);
Elliott Hughesb115aef2017-08-04 09:34:19 -0700339 ASSERT_EQ(0, linkat(AT_FDCWD, android::base::StringPrintf("/proc/self/fd/%d", fd).c_str(),
340 AT_FDCWD, final_path.c_str(),
341 AT_SYMLINK_FOLLOW));
342 ASSERT_EQ(0, close(fd));
343
344 // Does the resulting file claim to have the mode we set?
345 ASSERT_EQ(0, stat(final_path.c_str(), &sb));
346 ASSERT_EQ(perms, (sb.st_mode & ~S_IFMT));
347
348 // With O_EXCL, you're not allowed to add a name later.
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800349 fd = open(dir.path, O_TMPFILE | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
Elliott Hughesb115aef2017-08-04 09:34:19 -0700350 ASSERT_TRUE(fd != -1) << strerror(errno);
351 errno = 0;
352 ASSERT_EQ(-1, linkat(AT_FDCWD, android::base::StringPrintf("/proc/self/fd/%d", fd).c_str(),
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -0800353 AT_FDCWD, android::base::StringPrintf("%s/no_chance", dir.path).c_str(),
Elliott Hughesb115aef2017-08-04 09:34:19 -0700354 AT_SYMLINK_FOLLOW));
Elliott Hughes95646e62023-09-21 14:11:19 -0700355 ASSERT_ERRNO(ENOENT);
Elliott Hughesb115aef2017-08-04 09:34:19 -0700356 ASSERT_EQ(0, close(fd));
Elliott Hughesb115aef2017-08-04 09:34:19 -0700357}
Elliott Hughesdfe67d22022-02-17 12:55:42 -0800358
Christopher Ferrisc833fab2023-07-24 13:55:01 -0700359TEST_F(fcntl_DeathTest, fcntl_F_SETFD) {
Elliott Hughes25af17c2023-10-11 23:02:37 +0000360 EXPECT_DEATH(fcntl(0, F_SETFD, O_NONBLOCK), "only supports FD_CLOEXEC");
Elliott Hughesdfe67d22022-02-17 12:55:42 -0800361}