2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
22 #include <sys/utsname.h>
24 #include "TemporaryFile.h"
26 // Glibc v2.19 doesn't include these in fcntl.h so host builds will fail without.
27 #if !defined(FALLOC_FL_PUNCH_HOLE) || !defined(FALLOC_FL_KEEP_SIZE)
28 #include <linux/falloc.h>
31 TEST(fcntl, fcntl_smoke) {
32 int fd = open("/proc/version", O_RDONLY);
33 ASSERT_TRUE(fd != -1);
35 int flags = fcntl(fd, F_GETFD);
36 ASSERT_TRUE(flags != -1);
37 ASSERT_EQ(0, flags & FD_CLOEXEC);
39 int rc = fcntl(fd, F_SETFD, FD_CLOEXEC);
42 flags = fcntl(fd, F_GETFD);
43 ASSERT_TRUE(flags != -1);
44 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
49 TEST(fcntl, open_open64) {
52 fd = open("/proc/version", O_RDONLY);
53 ASSERT_TRUE(fd != -1);
56 fd = open64("/proc/version", O_RDONLY);
57 ASSERT_TRUE(fd != -1);
61 TEST(fcntl, openat_openat64) {
64 fd = openat(AT_FDCWD, "/proc/version", O_RDONLY);
65 ASSERT_TRUE(fd != -1);
68 fd = openat64(AT_FDCWD, "/proc/version", O_RDONLY);
69 ASSERT_TRUE(fd != -1);
73 TEST(fcntl, creat_creat64) {
74 ASSERT_EQ(-1, creat("", 0666));
75 ASSERT_EQ(ENOENT, errno);
76 ASSERT_EQ(-1, creat64("", 0666));
77 ASSERT_EQ(ENOENT, errno);
80 TEST(fcntl, posix_fadvise) {
84 EXPECT_EQ(EBADF, posix_fadvise(-1, 0, 0, POSIX_FADV_NORMAL));
87 EXPECT_EQ(EBADF, posix_fadvise64(-1, 0, 0, POSIX_FADV_NORMAL));
90 EXPECT_EQ(EINVAL, posix_fadvise(tf.fd, 0, 0, -1));
93 EXPECT_EQ(EINVAL, posix_fadvise64(tf.fd, 0, 0, -1));
96 EXPECT_EQ(0, posix_fadvise(tf.fd, 0, 0, POSIX_FADV_NORMAL));
97 EXPECT_EQ(0, posix_fadvise64(tf.fd, 0, 0, POSIX_FADV_NORMAL));
100 TEST(fcntl, fallocate_EINVAL) {
103 // fallocate/fallocate64 set errno.
104 // posix_fallocate/posix_fallocate64 return an errno value.
107 ASSERT_EQ(-1, fallocate(tf.fd, 0, 0, -1));
108 ASSERT_EQ(EINVAL, errno);
111 ASSERT_EQ(-1, fallocate64(tf.fd, 0, 0, -1));
112 ASSERT_EQ(EINVAL, errno);
115 ASSERT_EQ(EINVAL, posix_fallocate(tf.fd, 0, -1));
119 ASSERT_EQ(EINVAL, posix_fallocate64(tf.fd, 0, -1));
123 TEST(fcntl, fallocate) {
126 ASSERT_EQ(0, fstat(tf.fd, &sb));
127 ASSERT_EQ(0, sb.st_size);
129 #if defined(__BIONIC__)
130 ASSERT_EQ(0, fallocate(tf.fd, 0, 0, 1));
131 ASSERT_EQ(0, fstat(tf.fd, &sb));
132 ASSERT_EQ(1, sb.st_size);
134 ASSERT_EQ(0, fallocate64(tf.fd, 0, 0, 2));
135 ASSERT_EQ(0, fstat(tf.fd, &sb));
136 ASSERT_EQ(2, sb.st_size);
139 ASSERT_EQ(0, posix_fallocate(tf.fd, 0, 3));
140 ASSERT_EQ(0, fstat(tf.fd, &sb));
141 ASSERT_EQ(3, sb.st_size);
143 ASSERT_EQ(0, posix_fallocate64(tf.fd, 0, 4));
144 ASSERT_EQ(0, fstat(tf.fd, &sb));
145 ASSERT_EQ(4, sb.st_size);
148 TEST(fcntl, f_getlk64) {
149 int fd = open64("/proc/version", O_RDONLY);
150 ASSERT_TRUE(fd != -1);
152 struct flock64 check_lock;
153 check_lock.l_type = F_WRLCK;
154 check_lock.l_start = 0;
155 check_lock.l_whence = SEEK_SET;
156 check_lock.l_len = 0;
158 int rc = fcntl(fd, F_GETLK64, &check_lock);
164 TEST(fcntl, splice) {
166 ASSERT_EQ(0, pipe(pipe_fds));
168 int in = open("/proc/cpuinfo", O_RDONLY);
173 ssize_t bytes_read = splice(in, 0, pipe_fds[1], NULL, 8*1024, SPLICE_F_MORE | SPLICE_F_MOVE);
174 ASSERT_NE(bytes_read, -1);
176 ssize_t bytes_written = splice(pipe_fds[0], NULL, tf.fd, 0, bytes_read, SPLICE_F_MORE | SPLICE_F_MOVE);
177 ASSERT_EQ(bytes_read, bytes_written);
184 TEST(fcntl, vmsplice) {
186 ASSERT_EQ(0, pipe(pipe_fds));
189 v[0].iov_base = const_cast<char*>("hello ");
191 v[1].iov_base = const_cast<char*>("world\n");
193 ssize_t bytes_written = vmsplice(pipe_fds[1], v, sizeof(v)/sizeof(iovec), 0);
194 ASSERT_EQ(v[0].iov_len + v[1].iov_len, static_cast<size_t>(bytes_written));
198 FILE* fp = fdopen(pipe_fds[0], "r");
199 ASSERT_TRUE(fp != NULL);
200 ASSERT_TRUE(fgets(buf, sizeof(buf), fp) != NULL);
202 ASSERT_STREQ("hello world\n", buf);
207 FILE* expected_fp = fopen("/proc/version", "r");
208 ASSERT_TRUE(expected_fp != NULL);
209 ASSERT_TRUE(fgets(expected, sizeof(expected), expected_fp) != NULL);
213 ASSERT_EQ(0, pipe(pipe1));
216 ASSERT_EQ(0, pipe(pipe2));
218 int in = open("/proc/version", O_RDONLY);
221 // Write /proc/version into pipe1.
222 ssize_t bytes_read = splice(in, 0, pipe1[1], NULL, 8*1024, SPLICE_F_MORE | SPLICE_F_MOVE);
223 ASSERT_NE(bytes_read, -1);
226 // Tee /proc/version from pipe1 into pipe2.
227 ssize_t bytes_teed = tee(pipe1[0], pipe2[1], SIZE_MAX, 0);
228 ASSERT_EQ(bytes_read, bytes_teed);
231 // The out fds of both pipe1 and pipe2 should now contain /proc/version.
233 FILE* fp1 = fdopen(pipe1[0], "r");
234 ASSERT_TRUE(fp1 != NULL);
235 ASSERT_TRUE(fgets(buf1, sizeof(buf1), fp1) != NULL);
239 FILE* fp2 = fdopen(pipe2[0], "r");
240 ASSERT_TRUE(fp2 != NULL);
241 ASSERT_TRUE(fgets(buf2, sizeof(buf2), fp2) != NULL);
244 ASSERT_STREQ(expected, buf1);
245 ASSERT_STREQ(expected, buf2);
248 TEST(fcntl, readahead) {
249 // Just check that the function is available.
251 ASSERT_EQ(-1, readahead(-1, 0, 123));
252 ASSERT_EQ(EBADF, errno);
255 TEST(fcntl, sync_file_range) {
256 // Just check that the function is available.
258 ASSERT_EQ(-1, sync_file_range(-1, 0, 0, 0));
259 ASSERT_EQ(EBADF, errno);
262 ASSERT_EQ(0, sync_file_range(tf.fd, 0, 0, 0));
264 // The arguments to the underlying system call are in a different order on 32-bit ARM.
265 // Check that the `flags` argument gets passed to the kernel correctly.
267 ASSERT_EQ(-1, sync_file_range(tf.fd, 0, 0, ~0));
268 ASSERT_EQ(EINVAL, errno);
271 static bool parse_kernel_release(long* const major, long* const minor) {
273 if (uname(&buf) == -1) {
276 return sscanf(buf.release, "%ld.%ld", major, minor) == 2;
280 * Kernels less than 4.1 are affected.
281 * Devices that fail this test should include change id from Nexus:
282 * Commit: 9b431291a1fadbdbcca1485711b5bab145112293
284 TEST(fcntl, falloc_punch) {
285 long major = 0, minor = 0;
286 ASSERT_TRUE(parse_kernel_release(&major, &minor));
288 if (major < 4 || (major == 4 && minor < 1)) {
290 ASSERT_EQ(-1, fallocate(tf.fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 1));
291 ASSERT_EQ(errno, EOPNOTSUPP);