Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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> |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 18 | #include "BionicDeathTest.h" |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 19 | #include "ScopedSignalHandler.h" |
Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 20 | #include "TemporaryFile.h" |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 21 | |
Elliott Hughes | 915fefb | 2014-02-18 12:34:51 -0800 | [diff] [blame] | 22 | #include <errno.h> |
Colin Cross | 3d19a83 | 2014-02-14 18:56:23 -0800 | [diff] [blame] | 23 | #include <fcntl.h> |
Elliott Hughes | 21972b6 | 2014-07-28 12:24:22 -0700 | [diff] [blame] | 24 | #include <limits.h> |
Elliott Hughes | 428f556 | 2013-02-05 16:10:59 -0800 | [diff] [blame] | 25 | #include <stdint.h> |
Elliott Hughes | 7086ad6 | 2014-06-19 16:39:01 -0700 | [diff] [blame] | 26 | #include <sys/syscall.h> |
Elliott Hughes | 764a993 | 2014-04-08 19:44:36 -0700 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | #include <sys/wait.h> |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | |
Elliott Hughes | a55f630 | 2013-01-02 14:23:43 -0800 | [diff] [blame] | 31 | |
| 32 | TEST(unistd, sysconf_SC_MONOTONIC_CLOCK) { |
| 33 | ASSERT_GT(sysconf(_SC_MONOTONIC_CLOCK), 0); |
| 34 | } |
Elliott Hughes | 428f556 | 2013-02-05 16:10:59 -0800 | [diff] [blame] | 35 | |
Elliott Hughes | 533dde4 | 2014-04-25 18:27:38 -0700 | [diff] [blame] | 36 | static void* get_brk() { |
| 37 | return sbrk(0); |
| 38 | } |
Elliott Hughes | 428f556 | 2013-02-05 16:10:59 -0800 | [diff] [blame] | 39 | |
Elliott Hughes | 533dde4 | 2014-04-25 18:27:38 -0700 | [diff] [blame] | 40 | static void* page_align(uintptr_t addr) { |
| 41 | uintptr_t mask = sysconf(_SC_PAGE_SIZE) - 1; |
| 42 | return reinterpret_cast<void*>((addr + mask) & ~mask); |
| 43 | } |
| 44 | |
| 45 | TEST(unistd, brk) { |
| 46 | void* initial_break = get_brk(); |
| 47 | |
| 48 | // The kernel aligns the break to a page. |
| 49 | void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 1); |
Elliott Hughes | 428f556 | 2013-02-05 16:10:59 -0800 | [diff] [blame] | 50 | ASSERT_EQ(0, brk(new_break)); |
Elliott Hughes | 533dde4 | 2014-04-25 18:27:38 -0700 | [diff] [blame] | 51 | ASSERT_GE(get_brk(), new_break); |
Elliott Hughes | 428f556 | 2013-02-05 16:10:59 -0800 | [diff] [blame] | 52 | |
Elliott Hughes | 533dde4 | 2014-04-25 18:27:38 -0700 | [diff] [blame] | 53 | new_break = page_align(reinterpret_cast<uintptr_t>(initial_break) + sysconf(_SC_PAGE_SIZE)); |
| 54 | ASSERT_EQ(0, brk(new_break)); |
| 55 | ASSERT_EQ(get_brk(), new_break); |
| 56 | } |
| 57 | |
| 58 | TEST(unistd, brk_ENOMEM) { |
| 59 | ASSERT_EQ(-1, brk(reinterpret_cast<void*>(-1))); |
| 60 | ASSERT_EQ(ENOMEM, errno); |
| 61 | } |
| 62 | |
Christopher Ferris | 738b0cc | 2014-05-21 19:03:34 -0700 | [diff] [blame] | 63 | #if defined(__GLIBC__) |
| 64 | #define SBRK_MIN INTPTR_MIN |
| 65 | #define SBRK_MAX INTPTR_MAX |
| 66 | #else |
| 67 | #define SBRK_MIN PTRDIFF_MIN |
| 68 | #define SBRK_MAX PTRDIFF_MAX |
| 69 | #endif |
| 70 | |
Elliott Hughes | 533dde4 | 2014-04-25 18:27:38 -0700 | [diff] [blame] | 71 | TEST(unistd, sbrk_ENOMEM) { |
Christopher Ferris | 738b0cc | 2014-05-21 19:03:34 -0700 | [diff] [blame] | 72 | #if defined(__BIONIC__) && !defined(__LP64__) |
| 73 | // There is no way to guarantee that all overflow conditions can be tested |
| 74 | // without manipulating the underlying values of the current break. |
| 75 | extern void* __bionic_brk; |
| 76 | |
| 77 | class ScopedBrk { |
| 78 | public: |
| 79 | ScopedBrk() : saved_brk_(__bionic_brk) {} |
| 80 | virtual ~ScopedBrk() { __bionic_brk = saved_brk_; } |
| 81 | |
| 82 | private: |
| 83 | void* saved_brk_; |
| 84 | }; |
| 85 | |
| 86 | ScopedBrk scope_brk; |
| 87 | |
| 88 | // Set the current break to a point that will cause an overflow. |
| 89 | __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX) + 2); |
Elliott Hughes | 533dde4 | 2014-04-25 18:27:38 -0700 | [diff] [blame] | 90 | |
| 91 | // Can't increase by so much that we'd overflow. |
| 92 | ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MAX)); |
| 93 | ASSERT_EQ(ENOMEM, errno); |
| 94 | |
Christopher Ferris | 738b0cc | 2014-05-21 19:03:34 -0700 | [diff] [blame] | 95 | // Set the current break to a point that will cause an overflow. |
| 96 | __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX)); |
Elliott Hughes | 533dde4 | 2014-04-25 18:27:38 -0700 | [diff] [blame] | 97 | |
Elliott Hughes | 533dde4 | 2014-04-25 18:27:38 -0700 | [diff] [blame] | 98 | ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MIN)); |
| 99 | ASSERT_EQ(ENOMEM, errno); |
Christopher Ferris | 738b0cc | 2014-05-21 19:03:34 -0700 | [diff] [blame] | 100 | |
| 101 | __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX) - 1); |
| 102 | |
| 103 | ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MIN + 1)); |
| 104 | ASSERT_EQ(ENOMEM, errno); |
| 105 | #else |
| 106 | class ScopedBrk { |
| 107 | public: |
| 108 | ScopedBrk() : saved_brk_(get_brk()) {} |
| 109 | virtual ~ScopedBrk() { brk(saved_brk_); } |
| 110 | |
| 111 | private: |
| 112 | void* saved_brk_; |
| 113 | }; |
| 114 | |
| 115 | ScopedBrk scope_brk; |
| 116 | |
| 117 | uintptr_t cur_brk = reinterpret_cast<uintptr_t>(get_brk()); |
| 118 | if (cur_brk < static_cast<uintptr_t>(-(SBRK_MIN+1))) { |
| 119 | // Do the overflow test for a max negative increment. |
| 120 | ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(SBRK_MIN)); |
| 121 | #if defined(__BIONIC__) |
| 122 | // GLIBC does not set errno in overflow case. |
| 123 | ASSERT_EQ(ENOMEM, errno); |
| 124 | #endif |
| 125 | } |
| 126 | |
| 127 | uintptr_t overflow_brk = static_cast<uintptr_t>(SBRK_MAX) + 2; |
| 128 | if (cur_brk < overflow_brk) { |
| 129 | // Try and move the value to PTRDIFF_MAX + 2. |
| 130 | cur_brk = reinterpret_cast<uintptr_t>(sbrk(overflow_brk)); |
| 131 | } |
| 132 | if (cur_brk >= overflow_brk) { |
| 133 | ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(SBRK_MAX)); |
| 134 | #if defined(__BIONIC__) |
| 135 | // GLIBC does not set errno in overflow case. |
| 136 | ASSERT_EQ(ENOMEM, errno); |
| 137 | #endif |
| 138 | } |
Elliott Hughes | 533dde4 | 2014-04-25 18:27:38 -0700 | [diff] [blame] | 139 | #endif |
Elliott Hughes | 428f556 | 2013-02-05 16:10:59 -0800 | [diff] [blame] | 140 | } |
Elliott Hughes | b4f7616 | 2013-09-19 16:27:24 -0700 | [diff] [blame] | 141 | |
| 142 | TEST(unistd, truncate) { |
| 143 | TemporaryFile tf; |
| 144 | ASSERT_EQ(0, close(tf.fd)); |
| 145 | ASSERT_EQ(0, truncate(tf.filename, 123)); |
| 146 | |
| 147 | struct stat sb; |
| 148 | ASSERT_EQ(0, stat(tf.filename, &sb)); |
| 149 | ASSERT_EQ(123, sb.st_size); |
| 150 | } |
| 151 | |
| 152 | TEST(unistd, truncate64) { |
| 153 | TemporaryFile tf; |
| 154 | ASSERT_EQ(0, close(tf.fd)); |
| 155 | ASSERT_EQ(0, truncate64(tf.filename, 123)); |
| 156 | |
| 157 | struct stat sb; |
| 158 | ASSERT_EQ(0, stat(tf.filename, &sb)); |
| 159 | ASSERT_EQ(123, sb.st_size); |
| 160 | } |
| 161 | |
| 162 | TEST(unistd, ftruncate) { |
| 163 | TemporaryFile tf; |
| 164 | ASSERT_EQ(0, ftruncate(tf.fd, 123)); |
| 165 | ASSERT_EQ(0, close(tf.fd)); |
| 166 | |
| 167 | struct stat sb; |
| 168 | ASSERT_EQ(0, stat(tf.filename, &sb)); |
| 169 | ASSERT_EQ(123, sb.st_size); |
| 170 | } |
| 171 | |
| 172 | TEST(unistd, ftruncate64) { |
| 173 | TemporaryFile tf; |
| 174 | ASSERT_EQ(0, ftruncate64(tf.fd, 123)); |
| 175 | ASSERT_EQ(0, close(tf.fd)); |
| 176 | |
| 177 | struct stat sb; |
| 178 | ASSERT_EQ(0, stat(tf.filename, &sb)); |
| 179 | ASSERT_EQ(123, sb.st_size); |
| 180 | } |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 181 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 182 | static bool g_pause_test_flag = false; |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 183 | static void PauseTestSignalHandler(int) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 184 | g_pause_test_flag = true; |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | TEST(unistd, pause) { |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 188 | ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler); |
| 189 | |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 190 | alarm(1); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 191 | ASSERT_FALSE(g_pause_test_flag); |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 192 | ASSERT_EQ(-1, pause()); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 193 | ASSERT_TRUE(g_pause_test_flag); |
Elliott Hughes | 1195207 | 2013-10-24 15:15:14 -0700 | [diff] [blame] | 194 | } |
Colin Cross | 3d19a83 | 2014-02-14 18:56:23 -0800 | [diff] [blame] | 195 | |
| 196 | TEST(unistd, read) { |
| 197 | int fd = open("/proc/version", O_RDONLY); |
| 198 | ASSERT_TRUE(fd != -1); |
| 199 | |
| 200 | char buf[5]; |
| 201 | ASSERT_EQ(5, read(fd, buf, 5)); |
| 202 | ASSERT_EQ(buf[0], 'L'); |
| 203 | ASSERT_EQ(buf[1], 'i'); |
| 204 | ASSERT_EQ(buf[2], 'n'); |
| 205 | ASSERT_EQ(buf[3], 'u'); |
| 206 | ASSERT_EQ(buf[4], 'x'); |
| 207 | close(fd); |
| 208 | } |
| 209 | |
| 210 | TEST(unistd, read_EBADF) { |
| 211 | // read returns ssize_t which is 64-bits on LP64, so it's worth explicitly checking that |
| 212 | // our syscall stubs correctly return a 64-bit -1. |
| 213 | char buf[1]; |
| 214 | ASSERT_EQ(-1, read(-1, buf, sizeof(buf))); |
| 215 | ASSERT_EQ(EBADF, errno); |
| 216 | } |
Elliott Hughes | aedb00d | 2014-03-03 14:38:20 -0800 | [diff] [blame] | 217 | |
Elliott Hughes | 21972b6 | 2014-07-28 12:24:22 -0700 | [diff] [blame] | 218 | TEST(unistd, syscall_long) { |
| 219 | // Check that syscall(3) correctly returns long results. |
| 220 | // https://code.google.com/p/android/issues/detail?id=73952 |
| 221 | // We assume that the break is > 4GiB, but this is potentially flaky. |
| 222 | uintptr_t p = reinterpret_cast<uintptr_t>(sbrk(0)); |
| 223 | ASSERT_EQ(p, static_cast<uintptr_t>(syscall(__NR_brk, 0))); |
| 224 | } |
| 225 | |
Elliott Hughes | aedb00d | 2014-03-03 14:38:20 -0800 | [diff] [blame] | 226 | TEST(unistd, alarm) { |
| 227 | ASSERT_EQ(0U, alarm(0)); |
| 228 | } |
Elliott Hughes | 9f52564 | 2014-04-08 17:14:01 -0700 | [diff] [blame] | 229 | |
| 230 | TEST(unistd, _exit) { |
| 231 | int pid = fork(); |
| 232 | ASSERT_NE(-1, pid) << strerror(errno); |
| 233 | |
| 234 | if (pid == 0) { |
| 235 | _exit(99); |
| 236 | } |
| 237 | |
| 238 | int status; |
| 239 | ASSERT_EQ(pid, waitpid(pid, &status, 0)); |
| 240 | ASSERT_TRUE(WIFEXITED(status)); |
| 241 | ASSERT_EQ(99, WEXITSTATUS(status)); |
| 242 | } |
Grigoriy Kraynov | cbf6df0 | 2013-09-17 15:44:22 +0400 | [diff] [blame] | 243 | |
| 244 | TEST(unistd, getenv_unsetenv) { |
| 245 | ASSERT_EQ(0, setenv("test-variable", "hello", 1)); |
| 246 | ASSERT_STREQ("hello", getenv("test-variable")); |
| 247 | ASSERT_EQ(0, unsetenv("test-variable")); |
| 248 | ASSERT_TRUE(getenv("test-variable") == NULL); |
| 249 | } |
| 250 | |
| 251 | TEST(unistd, unsetenv_EINVAL) { |
| 252 | EXPECT_EQ(-1, unsetenv(NULL)); |
| 253 | EXPECT_EQ(EINVAL, errno); |
| 254 | EXPECT_EQ(-1, unsetenv("")); |
| 255 | EXPECT_EQ(EINVAL, errno); |
| 256 | EXPECT_EQ(-1, unsetenv("a=b")); |
| 257 | EXPECT_EQ(EINVAL, errno); |
| 258 | } |
| 259 | |
| 260 | TEST(unistd, setenv_EINVAL) { |
| 261 | EXPECT_EQ(-1, setenv(NULL, "value", 0)); |
| 262 | EXPECT_EQ(EINVAL, errno); |
| 263 | EXPECT_EQ(-1, setenv(NULL, "value", 1)); |
| 264 | EXPECT_EQ(EINVAL, errno); |
| 265 | EXPECT_EQ(-1, setenv("", "value", 0)); |
| 266 | EXPECT_EQ(EINVAL, errno); |
| 267 | EXPECT_EQ(-1, setenv("", "value", 1)); |
| 268 | EXPECT_EQ(EINVAL, errno); |
| 269 | EXPECT_EQ(-1, setenv("a=b", "value", 0)); |
| 270 | EXPECT_EQ(EINVAL, errno); |
| 271 | EXPECT_EQ(-1, setenv("a=b", "value", 1)); |
| 272 | EXPECT_EQ(EINVAL, errno); |
| 273 | } |
| 274 | |
| 275 | TEST(unistd, setenv) { |
| 276 | ASSERT_EQ(0, unsetenv("test-variable")); |
| 277 | |
| 278 | char a[] = "a"; |
| 279 | char b[] = "b"; |
| 280 | char c[] = "c"; |
| 281 | |
| 282 | // New value. |
| 283 | EXPECT_EQ(0, setenv("test-variable", a, 0)); |
| 284 | EXPECT_STREQ(a, getenv("test-variable")); |
| 285 | |
| 286 | // Existing value, no overwrite. |
| 287 | EXPECT_EQ(0, setenv("test-variable", b, 0)); |
| 288 | EXPECT_STREQ(a, getenv("test-variable")); |
| 289 | |
| 290 | // Existing value, overwrite. |
| 291 | EXPECT_EQ(0, setenv("test-variable", c, 1)); |
| 292 | EXPECT_STREQ(c, getenv("test-variable")); |
| 293 | // But the arrays backing the values are unchanged. |
| 294 | EXPECT_EQ('a', a[0]); |
| 295 | EXPECT_EQ('b', b[0]); |
| 296 | EXPECT_EQ('c', c[0]); |
| 297 | |
| 298 | ASSERT_EQ(0, unsetenv("test-variable")); |
| 299 | } |
| 300 | |
| 301 | TEST(unistd, putenv) { |
| 302 | ASSERT_EQ(0, unsetenv("a")); |
| 303 | |
| 304 | char* s1 = strdup("a=b"); |
| 305 | ASSERT_EQ(0, putenv(s1)); |
| 306 | |
| 307 | ASSERT_STREQ("b", getenv("a")); |
| 308 | s1[2] = 'c'; |
| 309 | ASSERT_STREQ("c", getenv("a")); |
| 310 | |
| 311 | char* s2 = strdup("a=b"); |
| 312 | ASSERT_EQ(0, putenv(s2)); |
| 313 | |
| 314 | ASSERT_STREQ("b", getenv("a")); |
| 315 | ASSERT_EQ('c', s1[2]); |
| 316 | |
| 317 | ASSERT_EQ(0, unsetenv("a")); |
| 318 | free(s1); |
| 319 | free(s2); |
| 320 | } |
| 321 | |
| 322 | TEST(unistd, clearenv) { |
| 323 | extern char** environ; |
| 324 | |
| 325 | // Guarantee that environ is not initially empty... |
| 326 | ASSERT_EQ(0, setenv("test-variable", "a", 1)); |
| 327 | |
| 328 | // Stash a copy. |
| 329 | std::vector<char*> old_environ; |
| 330 | for (size_t i = 0; environ[i] != NULL; ++i) { |
| 331 | old_environ.push_back(strdup(environ[i])); |
| 332 | } |
| 333 | |
| 334 | ASSERT_EQ(0, clearenv()); |
| 335 | |
| 336 | EXPECT_TRUE(environ == NULL || environ[0] == NULL); |
| 337 | EXPECT_EQ(NULL, getenv("test-variable")); |
| 338 | EXPECT_EQ(0, setenv("test-variable", "post-clear", 1)); |
| 339 | EXPECT_STREQ("post-clear", getenv("test-variable")); |
| 340 | |
| 341 | // Put the old environment back. |
| 342 | for (size_t i = 0; i < old_environ.size(); ++i) { |
| 343 | EXPECT_EQ(0, putenv(old_environ[i])); |
| 344 | } |
| 345 | |
| 346 | // Check it wasn't overwritten. |
| 347 | EXPECT_STREQ("a", getenv("test-variable")); |
| 348 | |
| 349 | EXPECT_EQ(0, unsetenv("test-variable")); |
| 350 | } |
Elliott Hughes | a62a28d | 2014-05-07 14:30:33 -0700 | [diff] [blame] | 351 | |
| 352 | static void TestFsyncFunction(int (*fn)(int)) { |
| 353 | int fd; |
| 354 | |
| 355 | // Can't sync an invalid fd. |
| 356 | errno = 0; |
| 357 | EXPECT_EQ(-1, fn(-1)); |
| 358 | EXPECT_EQ(EBADF, errno); |
| 359 | |
| 360 | // It doesn't matter whether you've opened a file for write or not. |
| 361 | TemporaryFile tf; |
| 362 | ASSERT_NE(-1, tf.fd); |
| 363 | |
| 364 | EXPECT_EQ(0, fn(tf.fd)); |
| 365 | |
| 366 | ASSERT_NE(-1, fd = open(tf.filename, O_RDONLY)); |
| 367 | EXPECT_EQ(0, fn(fd)); |
| 368 | close(fd); |
| 369 | |
| 370 | ASSERT_NE(-1, fd = open(tf.filename, O_RDWR)); |
| 371 | EXPECT_EQ(0, fn(fd)); |
| 372 | close(fd); |
| 373 | |
| 374 | // The fd can even be a directory. |
| 375 | ASSERT_NE(-1, fd = open("/", O_RDONLY)); |
| 376 | EXPECT_EQ(0, fn(fd)); |
| 377 | close(fd); |
| 378 | |
| 379 | // But some file systems may choose to be fussy... |
| 380 | errno = 0; |
| 381 | ASSERT_NE(-1, fd = open("/proc/version", O_RDONLY)); |
| 382 | EXPECT_EQ(-1, fn(fd)); |
| 383 | EXPECT_EQ(EINVAL, errno); |
| 384 | close(fd); |
| 385 | } |
| 386 | |
| 387 | TEST(unistd, fdatasync) { |
| 388 | TestFsyncFunction(fdatasync); |
| 389 | } |
| 390 | |
| 391 | TEST(unistd, fsync) { |
| 392 | TestFsyncFunction(fsync); |
| 393 | } |
Elliott Hughes | 7086ad6 | 2014-06-19 16:39:01 -0700 | [diff] [blame] | 394 | |
Elliott Hughes | fa9e16e | 2014-06-23 17:49:45 -0700 | [diff] [blame] | 395 | static void AssertGetPidCorrect() { |
| 396 | // The loop is just to make manual testing/debugging with strace easier. |
| 397 | pid_t getpid_syscall_result = syscall(__NR_getpid); |
| 398 | for (size_t i = 0; i < 128; ++i) { |
| 399 | ASSERT_EQ(getpid_syscall_result, getpid()); |
| 400 | } |
| 401 | } |
| 402 | |
Elliott Hughes | 7086ad6 | 2014-06-19 16:39:01 -0700 | [diff] [blame] | 403 | TEST(unistd, getpid_caching_and_fork) { |
| 404 | pid_t parent_pid = getpid(); |
| 405 | ASSERT_EQ(syscall(__NR_getpid), parent_pid); |
| 406 | |
| 407 | pid_t fork_result = fork(); |
| 408 | ASSERT_NE(fork_result, -1); |
| 409 | if (fork_result == 0) { |
| 410 | // We're the child. |
Elliott Hughes | fa9e16e | 2014-06-23 17:49:45 -0700 | [diff] [blame] | 411 | AssertGetPidCorrect(); |
Elliott Hughes | 7086ad6 | 2014-06-19 16:39:01 -0700 | [diff] [blame] | 412 | ASSERT_EQ(parent_pid, getppid()); |
| 413 | _exit(123); |
| 414 | } else { |
| 415 | // We're the parent. |
| 416 | ASSERT_EQ(parent_pid, getpid()); |
| 417 | |
| 418 | int status; |
| 419 | ASSERT_EQ(fork_result, waitpid(fork_result, &status, 0)); |
| 420 | ASSERT_TRUE(WIFEXITED(status)); |
| 421 | ASSERT_EQ(123, WEXITSTATUS(status)); |
| 422 | } |
| 423 | } |
| 424 | |
Elliott Hughes | fa9e16e | 2014-06-23 17:49:45 -0700 | [diff] [blame] | 425 | static int GetPidCachingCloneStartRoutine(void*) { |
| 426 | AssertGetPidCorrect(); |
| 427 | return 123; |
Elliott Hughes | 7086ad6 | 2014-06-19 16:39:01 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Elliott Hughes | fa9e16e | 2014-06-23 17:49:45 -0700 | [diff] [blame] | 430 | TEST(unistd, getpid_caching_and_clone) { |
| 431 | pid_t parent_pid = getpid(); |
| 432 | ASSERT_EQ(syscall(__NR_getpid), parent_pid); |
| 433 | |
| 434 | void* child_stack[1024]; |
| 435 | int clone_result = clone(GetPidCachingCloneStartRoutine, &child_stack[1024], CLONE_NEWNS | SIGCHLD, NULL); |
Elliott Hughes | 2b3b2ec | 2014-08-21 19:23:53 -0700 | [diff] [blame] | 436 | if (clone_result == -1 && errno == EPERM && getuid() != 0) { |
| 437 | GTEST_LOG_(INFO) << "This test only works if you have permission to CLONE_NEWNS; try running as root.\n"; |
| 438 | return; |
| 439 | } |
Elliott Hughes | fa9e16e | 2014-06-23 17:49:45 -0700 | [diff] [blame] | 440 | ASSERT_NE(clone_result, -1); |
| 441 | |
| 442 | ASSERT_EQ(parent_pid, getpid()); |
| 443 | |
| 444 | int status; |
| 445 | ASSERT_EQ(clone_result, waitpid(clone_result, &status, 0)); |
| 446 | ASSERT_TRUE(WIFEXITED(status)); |
| 447 | ASSERT_EQ(123, WEXITSTATUS(status)); |
| 448 | } |
| 449 | |
| 450 | static void* GetPidCachingPthreadStartRoutine(void*) { |
| 451 | AssertGetPidCorrect(); |
Elliott Hughes | 7086ad6 | 2014-06-19 16:39:01 -0700 | [diff] [blame] | 452 | return NULL; |
| 453 | } |
| 454 | |
| 455 | TEST(unistd, getpid_caching_and_pthread_create) { |
| 456 | pid_t parent_pid = getpid(); |
| 457 | |
| 458 | pthread_t t; |
Elliott Hughes | fa9e16e | 2014-06-23 17:49:45 -0700 | [diff] [blame] | 459 | ASSERT_EQ(0, pthread_create(&t, NULL, GetPidCachingPthreadStartRoutine, NULL)); |
Elliott Hughes | 7086ad6 | 2014-06-19 16:39:01 -0700 | [diff] [blame] | 460 | |
| 461 | ASSERT_EQ(parent_pid, getpid()); |
| 462 | |
| 463 | void* result; |
| 464 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 465 | ASSERT_EQ(NULL, result); |
| 466 | } |
Elliott Hughes | 60452a2 | 2014-09-22 14:41:30 -0700 | [diff] [blame] | 467 | |
Yabin Cui | 9df7040 | 2014-11-05 18:01:01 -0800 | [diff] [blame] | 468 | class unistd_DeathTest : public BionicDeathTest {}; |
| 469 | |
| 470 | TEST_F(unistd_DeathTest, abort) { |
Elliott Hughes | 60452a2 | 2014-09-22 14:41:30 -0700 | [diff] [blame] | 471 | ASSERT_EXIT(abort(), testing::KilledBySignal(SIGABRT), ""); |
| 472 | } |
Elliott Hughes | b86a4c7 | 2014-11-07 16:07:13 -0800 | [diff] [blame] | 473 | |
| 474 | TEST(unistd, sethostname) { |
| 475 | // The permissions check happens before the argument check, so this will |
| 476 | // fail for a different reason if you're running as root than if you're |
| 477 | // not, but it'll fail either way. Checking that we have the symbol is about |
| 478 | // all we can do for sethostname(2). |
| 479 | ASSERT_EQ(-1, sethostname("", -1)); |
| 480 | } |