| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Desaulniers | 09d900d | 2016-07-19 15:20:24 -0700 | [diff] [blame] | 21 | #include <string.h> | 
|  | 22 | #include <sys/utsname.h> | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 23 |  | 
| Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 24 | #include "TemporaryFile.h" | 
|  | 25 |  | 
| Nick Desaulniers | 09d900d | 2016-07-19 15:20:24 -0700 | [diff] [blame] | 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> | 
|  | 29 | #endif | 
|  | 30 |  | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 31 | TEST(fcntl, fcntl_smoke) { | 
|  | 32 | int fd = open("/proc/version", O_RDONLY); | 
|  | 33 | ASSERT_TRUE(fd != -1); | 
|  | 34 |  | 
|  | 35 | int flags = fcntl(fd, F_GETFD); | 
|  | 36 | ASSERT_TRUE(flags != -1); | 
|  | 37 | ASSERT_EQ(0, flags & FD_CLOEXEC); | 
|  | 38 |  | 
|  | 39 | int rc = fcntl(fd, F_SETFD, FD_CLOEXEC); | 
|  | 40 | ASSERT_EQ(0, rc); | 
|  | 41 |  | 
|  | 42 | flags = fcntl(fd, F_GETFD); | 
|  | 43 | ASSERT_TRUE(flags != -1); | 
|  | 44 | ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC); | 
| Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 45 |  | 
|  | 46 | close(fd); | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | TEST(fcntl, open_open64) { | 
|  | 50 | int fd; | 
|  | 51 |  | 
|  | 52 | fd = open("/proc/version", O_RDONLY); | 
|  | 53 | ASSERT_TRUE(fd != -1); | 
|  | 54 | close(fd); | 
|  | 55 |  | 
|  | 56 | fd = open64("/proc/version", O_RDONLY); | 
|  | 57 | ASSERT_TRUE(fd != -1); | 
|  | 58 | close(fd); | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | TEST(fcntl, openat_openat64) { | 
|  | 62 | int fd; | 
|  | 63 |  | 
|  | 64 | fd = openat(AT_FDCWD, "/proc/version", O_RDONLY); | 
|  | 65 | ASSERT_TRUE(fd != -1); | 
|  | 66 | close(fd); | 
|  | 67 |  | 
|  | 68 | fd = openat64(AT_FDCWD, "/proc/version", O_RDONLY); | 
|  | 69 | ASSERT_TRUE(fd != -1); | 
|  | 70 | close(fd); | 
|  | 71 | } | 
|  | 72 |  | 
|  | 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); | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 78 | } | 
| Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 79 |  | 
| Elliott Hughes | b587f33 | 2014-09-10 17:39:00 -0700 | [diff] [blame] | 80 | TEST(fcntl, posix_fadvise) { | 
|  | 81 | TemporaryFile tf; | 
|  | 82 | errno = 0; | 
|  | 83 |  | 
|  | 84 | EXPECT_EQ(EBADF, posix_fadvise(-1, 0, 0, POSIX_FADV_NORMAL)); | 
|  | 85 | EXPECT_EQ(0, errno); | 
|  | 86 |  | 
|  | 87 | EXPECT_EQ(EBADF, posix_fadvise64(-1, 0, 0, POSIX_FADV_NORMAL)); | 
|  | 88 | EXPECT_EQ(0, errno); | 
|  | 89 |  | 
|  | 90 | EXPECT_EQ(EINVAL, posix_fadvise(tf.fd, 0, 0, -1)); | 
|  | 91 | EXPECT_EQ(0, errno); | 
|  | 92 |  | 
|  | 93 | EXPECT_EQ(EINVAL, posix_fadvise64(tf.fd, 0, 0, -1)); | 
|  | 94 | EXPECT_EQ(0, errno); | 
|  | 95 |  | 
|  | 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)); | 
|  | 98 | } | 
|  | 99 |  | 
| Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 100 | TEST(fcntl, fallocate_EINVAL) { | 
|  | 101 | TemporaryFile tf; | 
|  | 102 |  | 
| Elliott Hughes | b587f33 | 2014-09-10 17:39:00 -0700 | [diff] [blame] | 103 | // fallocate/fallocate64 set errno. | 
|  | 104 | // posix_fallocate/posix_fallocate64 return an errno value. | 
|  | 105 |  | 
| Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 106 | errno = 0; | 
|  | 107 | ASSERT_EQ(-1, fallocate(tf.fd, 0, 0, -1)); | 
|  | 108 | ASSERT_EQ(EINVAL, errno); | 
|  | 109 |  | 
|  | 110 | errno = 0; | 
|  | 111 | ASSERT_EQ(-1, fallocate64(tf.fd, 0, 0, -1)); | 
|  | 112 | ASSERT_EQ(EINVAL, errno); | 
| Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 113 |  | 
|  | 114 | errno = 0; | 
|  | 115 | ASSERT_EQ(EINVAL, posix_fallocate(tf.fd, 0, -1)); | 
|  | 116 | ASSERT_EQ(0, errno); | 
|  | 117 |  | 
|  | 118 | errno = 0; | 
|  | 119 | ASSERT_EQ(EINVAL, posix_fallocate64(tf.fd, 0, -1)); | 
|  | 120 | ASSERT_EQ(0, errno); | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | TEST(fcntl, fallocate) { | 
|  | 124 | TemporaryFile tf; | 
|  | 125 | struct stat sb; | 
|  | 126 | ASSERT_EQ(0, fstat(tf.fd, &sb)); | 
|  | 127 | ASSERT_EQ(0, sb.st_size); | 
|  | 128 |  | 
| Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 129 | #if defined(__BIONIC__) | 
| Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 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); | 
|  | 133 |  | 
|  | 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); | 
|  | 137 | #endif | 
|  | 138 |  | 
|  | 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); | 
|  | 142 |  | 
|  | 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); | 
|  | 146 | } | 
| Serban Constantinescu | 48501af | 2014-03-14 13:16:25 +0000 | [diff] [blame] | 147 |  | 
|  | 148 | TEST(fcntl, f_getlk64) { | 
|  | 149 | int fd = open64("/proc/version", O_RDONLY); | 
|  | 150 | ASSERT_TRUE(fd != -1); | 
|  | 151 |  | 
|  | 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; | 
|  | 157 |  | 
|  | 158 | int rc = fcntl(fd, F_GETLK64, &check_lock); | 
|  | 159 | ASSERT_EQ(0, rc); | 
|  | 160 |  | 
|  | 161 | close(fd); | 
|  | 162 | } | 
| Elliott Hughes | 3f525d4 | 2014-06-24 16:32:01 -0700 | [diff] [blame] | 163 |  | 
|  | 164 | TEST(fcntl, splice) { | 
|  | 165 | int pipe_fds[2]; | 
|  | 166 | ASSERT_EQ(0, pipe(pipe_fds)); | 
|  | 167 |  | 
|  | 168 | int in = open("/proc/cpuinfo", O_RDONLY); | 
|  | 169 | ASSERT_NE(in, -1); | 
|  | 170 |  | 
|  | 171 | TemporaryFile tf; | 
|  | 172 |  | 
|  | 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); | 
|  | 175 |  | 
|  | 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); | 
|  | 178 |  | 
|  | 179 | close(pipe_fds[0]); | 
|  | 180 | close(pipe_fds[1]); | 
|  | 181 | close(in); | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | TEST(fcntl, vmsplice) { | 
|  | 185 | int pipe_fds[2]; | 
|  | 186 | ASSERT_EQ(0, pipe(pipe_fds)); | 
|  | 187 |  | 
|  | 188 | iovec v[2]; | 
|  | 189 | v[0].iov_base = const_cast<char*>("hello "); | 
|  | 190 | v[0].iov_len = 6; | 
|  | 191 | v[1].iov_base = const_cast<char*>("world\n"); | 
|  | 192 | v[1].iov_len = 6; | 
|  | 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)); | 
|  | 195 | close(pipe_fds[1]); | 
|  | 196 |  | 
|  | 197 | char buf[BUFSIZ]; | 
|  | 198 | FILE* fp = fdopen(pipe_fds[0], "r"); | 
|  | 199 | ASSERT_TRUE(fp != NULL); | 
|  | 200 | ASSERT_TRUE(fgets(buf, sizeof(buf), fp) != NULL); | 
|  | 201 | fclose(fp); | 
|  | 202 | ASSERT_STREQ("hello world\n", buf); | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | TEST(fcntl, tee) { | 
|  | 206 | char expected[256]; | 
|  | 207 | FILE* expected_fp = fopen("/proc/version", "r"); | 
|  | 208 | ASSERT_TRUE(expected_fp != NULL); | 
|  | 209 | ASSERT_TRUE(fgets(expected, sizeof(expected), expected_fp) != NULL); | 
|  | 210 | fclose(expected_fp); | 
|  | 211 |  | 
|  | 212 | int pipe1[2]; | 
|  | 213 | ASSERT_EQ(0, pipe(pipe1)); | 
|  | 214 |  | 
|  | 215 | int pipe2[2]; | 
|  | 216 | ASSERT_EQ(0, pipe(pipe2)); | 
|  | 217 |  | 
|  | 218 | int in = open("/proc/version", O_RDONLY); | 
|  | 219 | ASSERT_NE(in, -1); | 
|  | 220 |  | 
|  | 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); | 
|  | 224 | close(pipe1[1]); | 
|  | 225 |  | 
|  | 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); | 
|  | 229 | close(pipe2[1]); | 
|  | 230 |  | 
|  | 231 | // The out fds of both pipe1 and pipe2 should now contain /proc/version. | 
|  | 232 | char buf1[BUFSIZ]; | 
|  | 233 | FILE* fp1 = fdopen(pipe1[0], "r"); | 
|  | 234 | ASSERT_TRUE(fp1 != NULL); | 
|  | 235 | ASSERT_TRUE(fgets(buf1, sizeof(buf1), fp1) != NULL); | 
|  | 236 | fclose(fp1); | 
|  | 237 |  | 
|  | 238 | char buf2[BUFSIZ]; | 
|  | 239 | FILE* fp2 = fdopen(pipe2[0], "r"); | 
|  | 240 | ASSERT_TRUE(fp2 != NULL); | 
|  | 241 | ASSERT_TRUE(fgets(buf2, sizeof(buf2), fp2) != NULL); | 
|  | 242 | fclose(fp2); | 
|  | 243 |  | 
|  | 244 | ASSERT_STREQ(expected, buf1); | 
|  | 245 | ASSERT_STREQ(expected, buf2); | 
|  | 246 | } | 
| Elliott Hughes | 55147ad | 2016-04-05 11:06:02 -0700 | [diff] [blame] | 247 |  | 
|  | 248 | TEST(fcntl, readahead) { | 
|  | 249 | // Just check that the function is available. | 
|  | 250 | errno = 0; | 
|  | 251 | ASSERT_EQ(-1, readahead(-1, 0, 123)); | 
|  | 252 | ASSERT_EQ(EBADF, errno); | 
|  | 253 | } | 
| Elliott Hughes | 7f72ad4 | 2016-04-05 11:56:03 -0700 | [diff] [blame] | 254 |  | 
|  | 255 | TEST(fcntl, sync_file_range) { | 
|  | 256 | // Just check that the function is available. | 
|  | 257 | errno = 0; | 
|  | 258 | ASSERT_EQ(-1, sync_file_range(-1, 0, 0, 0)); | 
|  | 259 | ASSERT_EQ(EBADF, errno); | 
|  | 260 |  | 
|  | 261 | TemporaryFile tf; | 
|  | 262 | ASSERT_EQ(0, sync_file_range(tf.fd, 0, 0, 0)); | 
|  | 263 |  | 
|  | 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. | 
|  | 266 | errno = 0; | 
|  | 267 | ASSERT_EQ(-1, sync_file_range(tf.fd, 0, 0, ~0)); | 
|  | 268 | ASSERT_EQ(EINVAL, errno); | 
|  | 269 | } | 
| Nick Desaulniers | 09d900d | 2016-07-19 15:20:24 -0700 | [diff] [blame] | 270 |  | 
|  | 271 | static bool parse_kernel_release(long* const major, long* const minor) { | 
|  | 272 | struct utsname buf; | 
|  | 273 | if (uname(&buf) == -1) { | 
|  | 274 | return false; | 
|  | 275 | } | 
|  | 276 | return sscanf(buf.release, "%ld.%ld", major, minor) == 2; | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | /* | 
|  | 280 | * Kernels less than 4.1 are affected. | 
|  | 281 | * Devices that fail this test should include change id from Nexus: | 
|  | 282 | * Commit: 9b431291a1fadbdbcca1485711b5bab145112293 | 
|  | 283 | */ | 
|  | 284 | TEST(fcntl, falloc_punch) { | 
|  | 285 | long major = 0, minor = 0; | 
|  | 286 | ASSERT_TRUE(parse_kernel_release(&major, &minor)); | 
|  | 287 |  | 
|  | 288 | if (major < 4 || (major == 4 && minor < 1)) { | 
|  | 289 | TemporaryFile tf; | 
|  | 290 | ASSERT_EQ(-1, fallocate(tf.fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 1)); | 
|  | 291 | ASSERT_EQ(errno, EOPNOTSUPP); | 
|  | 292 | } | 
|  | 293 | } |