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