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