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 | 094f316 | 2016-07-19 15:20:24 -0700 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <sys/utsname.h> |
Matthias Hausner | 47a5210 | 2017-06-02 10:09:19 -0700 | [diff] [blame] | 23 | #include <sys/vfs.h> |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 24 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 25 | #include <android-base/file.h> |
Elliott Hughes | dfe67d2 | 2022-02-17 12:55:42 -0800 | [diff] [blame] | 26 | #include <android-base/silent_death_test.h> |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> |
| 28 | |
Nick Desaulniers | 094f316 | 2016-07-19 15:20:24 -0700 | [diff] [blame] | 29 | // Glibc v2.19 doesn't include these in fcntl.h so host builds will fail without. |
| 30 | #if !defined(FALLOC_FL_PUNCH_HOLE) || !defined(FALLOC_FL_KEEP_SIZE) |
| 31 | #include <linux/falloc.h> |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 32 | #endif |
| 33 | #if !defined(EXT4_SUPER_MAGIC) |
Matthias Hausner | 47a5210 | 2017-06-02 10:09:19 -0700 | [diff] [blame] | 34 | #include <linux/magic.h> |
Nick Desaulniers | 094f316 | 2016-07-19 15:20:24 -0700 | [diff] [blame] | 35 | #endif |
| 36 | |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 37 | #include "utils.h" |
| 38 | |
Elliott Hughes | dfe67d2 | 2022-02-17 12:55:42 -0800 | [diff] [blame] | 39 | using fcntl_DeathTest = SilentDeathTest; |
| 40 | |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 41 | TEST(fcntl, fcntl_smoke) { |
| 42 | int fd = open("/proc/version", O_RDONLY); |
| 43 | ASSERT_TRUE(fd != -1); |
| 44 | |
| 45 | int flags = fcntl(fd, F_GETFD); |
| 46 | ASSERT_TRUE(flags != -1); |
| 47 | ASSERT_EQ(0, flags & FD_CLOEXEC); |
| 48 | |
| 49 | int rc = fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 50 | ASSERT_EQ(0, rc); |
| 51 | |
| 52 | flags = fcntl(fd, F_GETFD); |
| 53 | ASSERT_TRUE(flags != -1); |
| 54 | ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC); |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 55 | |
| 56 | close(fd); |
| 57 | } |
| 58 | |
| 59 | TEST(fcntl, open_open64) { |
| 60 | int fd; |
| 61 | |
| 62 | fd = open("/proc/version", O_RDONLY); |
| 63 | ASSERT_TRUE(fd != -1); |
| 64 | close(fd); |
| 65 | |
| 66 | fd = open64("/proc/version", O_RDONLY); |
| 67 | ASSERT_TRUE(fd != -1); |
| 68 | close(fd); |
| 69 | } |
| 70 | |
| 71 | TEST(fcntl, openat_openat64) { |
| 72 | int fd; |
| 73 | |
| 74 | fd = openat(AT_FDCWD, "/proc/version", O_RDONLY); |
| 75 | ASSERT_TRUE(fd != -1); |
| 76 | close(fd); |
| 77 | |
| 78 | fd = openat64(AT_FDCWD, "/proc/version", O_RDONLY); |
| 79 | ASSERT_TRUE(fd != -1); |
| 80 | close(fd); |
| 81 | } |
| 82 | |
| 83 | TEST(fcntl, creat_creat64) { |
| 84 | ASSERT_EQ(-1, creat("", 0666)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 85 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | db1ea34 | 2014-01-17 18:42:49 -0800 | [diff] [blame] | 86 | ASSERT_EQ(-1, creat64("", 0666)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 87 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 88 | } |
Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 89 | |
Elliott Hughes | b587f33 | 2014-09-10 17:39:00 -0700 | [diff] [blame] | 90 | TEST(fcntl, posix_fadvise) { |
| 91 | TemporaryFile tf; |
| 92 | errno = 0; |
| 93 | |
| 94 | EXPECT_EQ(EBADF, posix_fadvise(-1, 0, 0, POSIX_FADV_NORMAL)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 95 | EXPECT_ERRNO(0); |
Elliott Hughes | b587f33 | 2014-09-10 17:39:00 -0700 | [diff] [blame] | 96 | |
| 97 | EXPECT_EQ(EBADF, posix_fadvise64(-1, 0, 0, POSIX_FADV_NORMAL)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 98 | EXPECT_ERRNO(0); |
Elliott Hughes | b587f33 | 2014-09-10 17:39:00 -0700 | [diff] [blame] | 99 | |
| 100 | EXPECT_EQ(EINVAL, posix_fadvise(tf.fd, 0, 0, -1)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 101 | EXPECT_ERRNO(0); |
Elliott Hughes | b587f33 | 2014-09-10 17:39:00 -0700 | [diff] [blame] | 102 | |
| 103 | EXPECT_EQ(EINVAL, posix_fadvise64(tf.fd, 0, 0, -1)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 104 | EXPECT_ERRNO(0); |
Elliott Hughes | b587f33 | 2014-09-10 17:39:00 -0700 | [diff] [blame] | 105 | |
| 106 | EXPECT_EQ(0, posix_fadvise(tf.fd, 0, 0, POSIX_FADV_NORMAL)); |
| 107 | EXPECT_EQ(0, posix_fadvise64(tf.fd, 0, 0, POSIX_FADV_NORMAL)); |
| 108 | } |
| 109 | |
Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 110 | TEST(fcntl, fallocate_EINVAL) { |
| 111 | TemporaryFile tf; |
| 112 | |
Elliott Hughes | b587f33 | 2014-09-10 17:39:00 -0700 | [diff] [blame] | 113 | // fallocate/fallocate64 set errno. |
| 114 | // posix_fallocate/posix_fallocate64 return an errno value. |
| 115 | |
Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 116 | errno = 0; |
| 117 | ASSERT_EQ(-1, fallocate(tf.fd, 0, 0, -1)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 118 | ASSERT_ERRNO(EINVAL); |
Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 119 | |
| 120 | errno = 0; |
| 121 | ASSERT_EQ(-1, fallocate64(tf.fd, 0, 0, -1)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 122 | ASSERT_ERRNO(EINVAL); |
Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 123 | |
| 124 | errno = 0; |
| 125 | ASSERT_EQ(EINVAL, posix_fallocate(tf.fd, 0, -1)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 126 | ASSERT_ERRNO(0); |
Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 127 | |
| 128 | errno = 0; |
| 129 | ASSERT_EQ(EINVAL, posix_fallocate64(tf.fd, 0, -1)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 130 | ASSERT_ERRNO(0); |
Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | TEST(fcntl, fallocate) { |
| 134 | TemporaryFile tf; |
| 135 | struct stat sb; |
| 136 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 137 | ASSERT_EQ(0, sb.st_size); |
| 138 | |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 139 | #if defined(__BIONIC__) |
Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 140 | ASSERT_EQ(0, fallocate(tf.fd, 0, 0, 1)); |
| 141 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 142 | ASSERT_EQ(1, sb.st_size); |
| 143 | |
| 144 | ASSERT_EQ(0, fallocate64(tf.fd, 0, 0, 2)); |
| 145 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 146 | ASSERT_EQ(2, sb.st_size); |
| 147 | #endif |
| 148 | |
| 149 | ASSERT_EQ(0, posix_fallocate(tf.fd, 0, 3)); |
| 150 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 151 | ASSERT_EQ(3, sb.st_size); |
| 152 | |
| 153 | ASSERT_EQ(0, posix_fallocate64(tf.fd, 0, 4)); |
| 154 | ASSERT_EQ(0, fstat(tf.fd, &sb)); |
| 155 | ASSERT_EQ(4, sb.st_size); |
| 156 | } |
Serban Constantinescu | 48501af | 2014-03-14 13:16:25 +0000 | [diff] [blame] | 157 | |
Elliott Hughes | 09e77f3 | 2020-01-29 19:20:45 -0800 | [diff] [blame] | 158 | TEST(fcntl, f_getlk) { |
| 159 | int fd = open("/proc/version", O_RDONLY); |
| 160 | ASSERT_TRUE(fd != -1); |
| 161 | |
| 162 | struct flock check_lock; |
| 163 | check_lock.l_type = F_WRLCK; |
| 164 | check_lock.l_start = 0; |
| 165 | check_lock.l_whence = SEEK_SET; |
| 166 | check_lock.l_len = 0; |
| 167 | |
| 168 | ASSERT_EQ(0, fcntl(fd, F_GETLK, &check_lock)); |
| 169 | close(fd); |
| 170 | } |
| 171 | |
Serban Constantinescu | 48501af | 2014-03-14 13:16:25 +0000 | [diff] [blame] | 172 | TEST(fcntl, f_getlk64) { |
| 173 | int fd = open64("/proc/version", O_RDONLY); |
| 174 | ASSERT_TRUE(fd != -1); |
| 175 | |
| 176 | struct flock64 check_lock; |
| 177 | check_lock.l_type = F_WRLCK; |
| 178 | check_lock.l_start = 0; |
| 179 | check_lock.l_whence = SEEK_SET; |
| 180 | check_lock.l_len = 0; |
| 181 | |
Elliott Hughes | 09e77f3 | 2020-01-29 19:20:45 -0800 | [diff] [blame] | 182 | ASSERT_EQ(0, fcntl(fd, F_GETLK64, &check_lock)); |
Serban Constantinescu | 48501af | 2014-03-14 13:16:25 +0000 | [diff] [blame] | 183 | close(fd); |
| 184 | } |
Elliott Hughes | 3f525d4 | 2014-06-24 16:32:01 -0700 | [diff] [blame] | 185 | |
| 186 | TEST(fcntl, splice) { |
| 187 | int pipe_fds[2]; |
| 188 | ASSERT_EQ(0, pipe(pipe_fds)); |
| 189 | |
| 190 | int in = open("/proc/cpuinfo", O_RDONLY); |
| 191 | ASSERT_NE(in, -1); |
| 192 | |
| 193 | TemporaryFile tf; |
| 194 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 195 | ssize_t bytes_read = splice(in, nullptr, pipe_fds[1], nullptr, 8*1024, SPLICE_F_MORE | SPLICE_F_MOVE); |
Elliott Hughes | 3f525d4 | 2014-06-24 16:32:01 -0700 | [diff] [blame] | 196 | ASSERT_NE(bytes_read, -1); |
| 197 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 198 | ssize_t bytes_written = splice(pipe_fds[0], nullptr, tf.fd, nullptr, bytes_read, SPLICE_F_MORE | SPLICE_F_MOVE); |
Elliott Hughes | 3f525d4 | 2014-06-24 16:32:01 -0700 | [diff] [blame] | 199 | ASSERT_EQ(bytes_read, bytes_written); |
| 200 | |
| 201 | close(pipe_fds[0]); |
| 202 | close(pipe_fds[1]); |
| 203 | close(in); |
| 204 | } |
| 205 | |
| 206 | TEST(fcntl, vmsplice) { |
| 207 | int pipe_fds[2]; |
| 208 | ASSERT_EQ(0, pipe(pipe_fds)); |
| 209 | |
| 210 | iovec v[2]; |
| 211 | v[0].iov_base = const_cast<char*>("hello "); |
| 212 | v[0].iov_len = 6; |
| 213 | v[1].iov_base = const_cast<char*>("world\n"); |
| 214 | v[1].iov_len = 6; |
| 215 | ssize_t bytes_written = vmsplice(pipe_fds[1], v, sizeof(v)/sizeof(iovec), 0); |
| 216 | ASSERT_EQ(v[0].iov_len + v[1].iov_len, static_cast<size_t>(bytes_written)); |
| 217 | close(pipe_fds[1]); |
| 218 | |
| 219 | char buf[BUFSIZ]; |
| 220 | FILE* fp = fdopen(pipe_fds[0], "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 221 | ASSERT_TRUE(fp != nullptr); |
| 222 | ASSERT_TRUE(fgets(buf, sizeof(buf), fp) != nullptr); |
Elliott Hughes | 3f525d4 | 2014-06-24 16:32:01 -0700 | [diff] [blame] | 223 | fclose(fp); |
| 224 | ASSERT_STREQ("hello world\n", buf); |
| 225 | } |
| 226 | |
| 227 | TEST(fcntl, tee) { |
Kazuhiro Inaba | f7a8e95 | 2017-09-25 15:18:29 +0900 | [diff] [blame] | 228 | char expected[BUFSIZ]; |
Elliott Hughes | 3f525d4 | 2014-06-24 16:32:01 -0700 | [diff] [blame] | 229 | FILE* expected_fp = fopen("/proc/version", "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 230 | ASSERT_TRUE(expected_fp != nullptr); |
| 231 | ASSERT_TRUE(fgets(expected, sizeof(expected), expected_fp) != nullptr); |
Elliott Hughes | 3f525d4 | 2014-06-24 16:32:01 -0700 | [diff] [blame] | 232 | fclose(expected_fp); |
| 233 | |
| 234 | int pipe1[2]; |
| 235 | ASSERT_EQ(0, pipe(pipe1)); |
| 236 | |
| 237 | int pipe2[2]; |
| 238 | ASSERT_EQ(0, pipe(pipe2)); |
| 239 | |
| 240 | int in = open("/proc/version", O_RDONLY); |
| 241 | ASSERT_NE(in, -1); |
| 242 | |
| 243 | // Write /proc/version into pipe1. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 244 | ssize_t bytes_read = splice(in, nullptr, pipe1[1], nullptr, 8*1024, SPLICE_F_MORE | SPLICE_F_MOVE); |
Elliott Hughes | 3f525d4 | 2014-06-24 16:32:01 -0700 | [diff] [blame] | 245 | ASSERT_NE(bytes_read, -1); |
| 246 | close(pipe1[1]); |
| 247 | |
| 248 | // Tee /proc/version from pipe1 into pipe2. |
| 249 | ssize_t bytes_teed = tee(pipe1[0], pipe2[1], SIZE_MAX, 0); |
| 250 | ASSERT_EQ(bytes_read, bytes_teed); |
| 251 | close(pipe2[1]); |
| 252 | |
| 253 | // The out fds of both pipe1 and pipe2 should now contain /proc/version. |
| 254 | char buf1[BUFSIZ]; |
| 255 | FILE* fp1 = fdopen(pipe1[0], "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 256 | ASSERT_TRUE(fp1 != nullptr); |
| 257 | ASSERT_TRUE(fgets(buf1, sizeof(buf1), fp1) != nullptr); |
Elliott Hughes | 3f525d4 | 2014-06-24 16:32:01 -0700 | [diff] [blame] | 258 | fclose(fp1); |
| 259 | |
| 260 | char buf2[BUFSIZ]; |
| 261 | FILE* fp2 = fdopen(pipe2[0], "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 262 | ASSERT_TRUE(fp2 != nullptr); |
| 263 | ASSERT_TRUE(fgets(buf2, sizeof(buf2), fp2) != nullptr); |
Elliott Hughes | 3f525d4 | 2014-06-24 16:32:01 -0700 | [diff] [blame] | 264 | fclose(fp2); |
| 265 | |
| 266 | ASSERT_STREQ(expected, buf1); |
| 267 | ASSERT_STREQ(expected, buf2); |
| 268 | } |
Elliott Hughes | 55147ad | 2016-04-05 11:06:02 -0700 | [diff] [blame] | 269 | |
| 270 | TEST(fcntl, readahead) { |
| 271 | // Just check that the function is available. |
| 272 | errno = 0; |
| 273 | ASSERT_EQ(-1, readahead(-1, 0, 123)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 274 | ASSERT_ERRNO(EBADF); |
Elliott Hughes | 55147ad | 2016-04-05 11:06:02 -0700 | [diff] [blame] | 275 | } |
Elliott Hughes | 7f72ad4 | 2016-04-05 11:56:03 -0700 | [diff] [blame] | 276 | |
| 277 | TEST(fcntl, sync_file_range) { |
| 278 | // Just check that the function is available. |
| 279 | errno = 0; |
| 280 | ASSERT_EQ(-1, sync_file_range(-1, 0, 0, 0)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 281 | ASSERT_ERRNO(EBADF); |
Elliott Hughes | 7f72ad4 | 2016-04-05 11:56:03 -0700 | [diff] [blame] | 282 | |
| 283 | TemporaryFile tf; |
| 284 | ASSERT_EQ(0, sync_file_range(tf.fd, 0, 0, 0)); |
| 285 | |
| 286 | // The arguments to the underlying system call are in a different order on 32-bit ARM. |
| 287 | // Check that the `flags` argument gets passed to the kernel correctly. |
| 288 | errno = 0; |
| 289 | ASSERT_EQ(-1, sync_file_range(tf.fd, 0, 0, ~0)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 290 | ASSERT_ERRNO(EINVAL); |
Elliott Hughes | 7f72ad4 | 2016-04-05 11:56:03 -0700 | [diff] [blame] | 291 | } |
Nick Desaulniers | 094f316 | 2016-07-19 15:20:24 -0700 | [diff] [blame] | 292 | |
| 293 | static bool parse_kernel_release(long* const major, long* const minor) { |
| 294 | struct utsname buf; |
| 295 | if (uname(&buf) == -1) { |
| 296 | return false; |
| 297 | } |
| 298 | return sscanf(buf.release, "%ld.%ld", major, minor) == 2; |
| 299 | } |
| 300 | |
| 301 | /* |
Josh Gao | 24ed8b5 | 2017-06-02 14:57:49 -0700 | [diff] [blame] | 302 | * b/28760453: |
| 303 | * Kernels older than 4.1 should have ext4 FALLOC_FL_PUNCH_HOLE disabled due to CVE-2015-8839. |
| 304 | * Devices that fail this test should cherry-pick the following commit: |
| 305 | * https://android.googlesource.com/kernel/msm/+/bdba352e898cbf57c8620ad68c8abf749c784d1f |
Nick Desaulniers | 094f316 | 2016-07-19 15:20:24 -0700 | [diff] [blame] | 306 | */ |
| 307 | TEST(fcntl, falloc_punch) { |
| 308 | long major = 0, minor = 0; |
| 309 | ASSERT_TRUE(parse_kernel_release(&major, &minor)); |
| 310 | |
| 311 | if (major < 4 || (major == 4 && minor < 1)) { |
| 312 | TemporaryFile tf; |
Matthias Hausner | 47a5210 | 2017-06-02 10:09:19 -0700 | [diff] [blame] | 313 | struct statfs sfs; |
| 314 | ASSERT_EQ(0, fstatfs(tf.fd, &sfs)); |
| 315 | if (sfs.f_type == EXT4_SUPER_MAGIC) { |
| 316 | ASSERT_EQ(-1, fallocate(tf.fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 1)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 317 | ASSERT_ERRNO(EOPNOTSUPP); |
Matthias Hausner | 47a5210 | 2017-06-02 10:09:19 -0700 | [diff] [blame] | 318 | } |
Nick Desaulniers | 094f316 | 2016-07-19 15:20:24 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 321 | |
| 322 | TEST(fcntl, open_O_TMPFILE_mode) { |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 323 | TemporaryDir dir; |
| 324 | // Without O_EXCL, we're allowed to give this a name later. |
| 325 | // (This is unrelated to the O_CREAT interaction with O_EXCL.) |
| 326 | const mode_t perms = S_IRUSR | S_IWUSR; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 327 | int fd = open(dir.path, O_TMPFILE | O_RDWR, perms); |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 328 | |
| 329 | // Ignore kernels without O_TMPFILE support (< 3.11). |
| 330 | if (fd == -1 && (errno == EISDIR || errno == EINVAL || errno == EOPNOTSUPP)) return; |
| 331 | |
| 332 | ASSERT_TRUE(fd != -1) << strerror(errno); |
| 333 | |
| 334 | // Does the fd claim to have the mode we set? |
| 335 | struct stat sb = {}; |
| 336 | ASSERT_EQ(0, fstat(fd, &sb)); |
| 337 | ASSERT_EQ(perms, (sb.st_mode & ~S_IFMT)); |
| 338 | |
Elliott Hughes | fc30bfe | 2017-10-24 22:54:34 -0700 | [diff] [blame] | 339 | // On Android if we're not root, we won't be able to create links anyway... |
| 340 | if (getuid() != 0) return; |
| 341 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 342 | std::string final_path = android::base::StringPrintf("%s/named_now", dir.path); |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 343 | ASSERT_EQ(0, linkat(AT_FDCWD, android::base::StringPrintf("/proc/self/fd/%d", fd).c_str(), |
| 344 | AT_FDCWD, final_path.c_str(), |
| 345 | AT_SYMLINK_FOLLOW)); |
| 346 | ASSERT_EQ(0, close(fd)); |
| 347 | |
| 348 | // Does the resulting file claim to have the mode we set? |
| 349 | ASSERT_EQ(0, stat(final_path.c_str(), &sb)); |
| 350 | ASSERT_EQ(perms, (sb.st_mode & ~S_IFMT)); |
| 351 | |
| 352 | // With O_EXCL, you're not allowed to add a name later. |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 353 | fd = open(dir.path, O_TMPFILE | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR); |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 354 | ASSERT_TRUE(fd != -1) << strerror(errno); |
| 355 | errno = 0; |
| 356 | ASSERT_EQ(-1, linkat(AT_FDCWD, android::base::StringPrintf("/proc/self/fd/%d", fd).c_str(), |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 357 | AT_FDCWD, android::base::StringPrintf("%s/no_chance", dir.path).c_str(), |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 358 | AT_SYMLINK_FOLLOW)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 359 | ASSERT_ERRNO(ENOENT); |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 360 | ASSERT_EQ(0, close(fd)); |
Elliott Hughes | b115aef | 2017-08-04 09:34:19 -0700 | [diff] [blame] | 361 | } |
Elliott Hughes | dfe67d2 | 2022-02-17 12:55:42 -0800 | [diff] [blame] | 362 | |
Christopher Ferris | c833fab | 2023-07-24 13:55:01 -0700 | [diff] [blame] | 363 | TEST_F(fcntl_DeathTest, fcntl_F_SETFD) { |
Elliott Hughes | 25af17c | 2023-10-11 23:02:37 +0000 | [diff] [blame] | 364 | EXPECT_DEATH(fcntl(0, F_SETFD, O_NONBLOCK), "only supports FD_CLOEXEC"); |
Elliott Hughes | dfe67d2 | 2022-02-17 12:55:42 -0800 | [diff] [blame] | 365 | } |