2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
19 #include "BionicDeathTest.h"
20 #include "ScopedSignalHandler.h"
21 #include "TemporaryFile.h"
29 #include <sys/capability.h>
30 #include <sys/param.h>
31 #include <sys/syscall.h>
32 #include <sys/types.h>
33 #include <sys/utsname.h>
37 #include <android-base/file.h>
38 #include <android-base/strings.h>
40 #include "private/get_cpu_count_from_string.h"
42 #if defined(NOFORTIFY)
43 #define UNISTD_TEST unistd_nofortify
44 #define UNISTD_DEATHTEST unistd_nofortify_DeathTest
46 #define UNISTD_TEST unistd
47 #define UNISTD_DEATHTEST unistd_DeathTest
50 static void* get_brk() {
54 static void* page_align(uintptr_t addr) {
55 uintptr_t mask = sysconf(_SC_PAGE_SIZE) - 1;
56 return reinterpret_cast<void*>((addr + mask) & ~mask);
59 TEST(UNISTD_TEST, brk) {
60 void* initial_break = get_brk();
62 void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 1);
63 int ret = brk(new_break);
65 ASSERT_EQ(errno, ENOMEM);
68 ASSERT_GE(get_brk(), new_break);
71 // Expand by a full page to force the mapping to expand
72 new_break = page_align(reinterpret_cast<uintptr_t>(initial_break) + sysconf(_SC_PAGE_SIZE));
75 ASSERT_EQ(errno, ENOMEM);
78 ASSERT_EQ(get_brk(), new_break);
82 TEST(UNISTD_TEST, brk_ENOMEM) {
83 ASSERT_EQ(-1, brk(reinterpret_cast<void*>(-1)));
84 ASSERT_EQ(ENOMEM, errno);
87 #if defined(__GLIBC__)
88 #define SBRK_MIN INTPTR_MIN
89 #define SBRK_MAX INTPTR_MAX
91 #define SBRK_MIN PTRDIFF_MIN
92 #define SBRK_MAX PTRDIFF_MAX
95 TEST(UNISTD_TEST, sbrk_ENOMEM) {
96 #if defined(__BIONIC__) && !defined(__LP64__)
97 // There is no way to guarantee that all overflow conditions can be tested
98 // without manipulating the underlying values of the current break.
99 extern void* __bionic_brk;
103 ScopedBrk() : saved_brk_(__bionic_brk) {}
104 virtual ~ScopedBrk() { __bionic_brk = saved_brk_; }
112 // Set the current break to a point that will cause an overflow.
113 __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX) + 2);
115 // Can't increase by so much that we'd overflow.
116 ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MAX));
117 ASSERT_EQ(ENOMEM, errno);
119 // Set the current break to a point that will cause an overflow.
120 __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX));
122 ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MIN));
123 ASSERT_EQ(ENOMEM, errno);
125 __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX) - 1);
127 ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MIN + 1));
128 ASSERT_EQ(ENOMEM, errno);
132 ScopedBrk() : saved_brk_(get_brk()) {}
133 virtual ~ScopedBrk() { brk(saved_brk_); }
141 uintptr_t cur_brk = reinterpret_cast<uintptr_t>(get_brk());
142 if (cur_brk < static_cast<uintptr_t>(-(SBRK_MIN+1))) {
143 // Do the overflow test for a max negative increment.
144 ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(SBRK_MIN));
145 #if defined(__BIONIC__)
146 // GLIBC does not set errno in overflow case.
147 ASSERT_EQ(ENOMEM, errno);
151 uintptr_t overflow_brk = static_cast<uintptr_t>(SBRK_MAX) + 2;
152 if (cur_brk < overflow_brk) {
153 // Try and move the value to PTRDIFF_MAX + 2.
154 cur_brk = reinterpret_cast<uintptr_t>(sbrk(overflow_brk));
156 if (cur_brk >= overflow_brk) {
157 ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(SBRK_MAX));
158 #if defined(__BIONIC__)
159 // GLIBC does not set errno in overflow case.
160 ASSERT_EQ(ENOMEM, errno);
166 TEST(UNISTD_TEST, truncate) {
168 ASSERT_EQ(0, close(tf.fd));
169 ASSERT_EQ(0, truncate(tf.filename, 123));
172 ASSERT_EQ(0, stat(tf.filename, &sb));
173 ASSERT_EQ(123, sb.st_size);
176 TEST(UNISTD_TEST, truncate64) {
178 ASSERT_EQ(0, close(tf.fd));
179 ASSERT_EQ(0, truncate64(tf.filename, 123));
182 ASSERT_EQ(0, stat(tf.filename, &sb));
183 ASSERT_EQ(123, sb.st_size);
186 TEST(UNISTD_TEST, ftruncate) {
188 ASSERT_EQ(0, ftruncate(tf.fd, 123));
189 ASSERT_EQ(0, close(tf.fd));
192 ASSERT_EQ(0, stat(tf.filename, &sb));
193 ASSERT_EQ(123, sb.st_size);
196 TEST(UNISTD_TEST, ftruncate64) {
198 ASSERT_EQ(0, ftruncate64(tf.fd, 123));
199 ASSERT_EQ(0, close(tf.fd));
202 ASSERT_EQ(0, stat(tf.filename, &sb));
203 ASSERT_EQ(123, sb.st_size);
206 TEST(UNISTD_TEST, ftruncate_negative) {
209 ASSERT_EQ(-1, ftruncate(tf.fd, -123));
210 ASSERT_EQ(EINVAL, errno);
213 static bool g_pause_test_flag = false;
214 static void PauseTestSignalHandler(int) {
215 g_pause_test_flag = true;
218 TEST(UNISTD_TEST, pause) {
219 ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler);
222 ASSERT_FALSE(g_pause_test_flag);
223 ASSERT_EQ(-1, pause());
224 ASSERT_TRUE(g_pause_test_flag);
227 TEST(UNISTD_TEST, read) {
228 int fd = open("/proc/version", O_RDONLY);
229 ASSERT_TRUE(fd != -1);
232 ASSERT_EQ(5, read(fd, buf, 5));
233 ASSERT_EQ(buf[0], 'L');
234 ASSERT_EQ(buf[1], 'i');
235 ASSERT_EQ(buf[2], 'n');
236 ASSERT_EQ(buf[3], 'u');
237 ASSERT_EQ(buf[4], 'x');
241 TEST(UNISTD_TEST, read_EBADF) {
242 // read returns ssize_t which is 64-bits on LP64, so it's worth explicitly checking that
243 // our syscall stubs correctly return a 64-bit -1.
245 ASSERT_EQ(-1, read(-1, buf, sizeof(buf)));
246 ASSERT_EQ(EBADF, errno);
249 TEST(UNISTD_TEST, syscall_long) {
250 // Check that syscall(3) correctly returns long results.
251 // https://code.google.com/p/android/issues/detail?id=73952
252 // We assume that the break is > 4GiB, but this is potentially flaky.
253 uintptr_t p = reinterpret_cast<uintptr_t>(sbrk(0));
254 ASSERT_EQ(p, static_cast<uintptr_t>(syscall(__NR_brk, 0)));
257 TEST(UNISTD_TEST, alarm) {
258 ASSERT_EQ(0U, alarm(0));
261 TEST(UNISTD_TEST, _exit) {
263 ASSERT_NE(-1, pid) << strerror(errno);
269 AssertChildExited(pid, 99);
272 TEST(UNISTD_TEST, getenv_unsetenv) {
273 ASSERT_EQ(0, setenv("test-variable", "hello", 1));
274 ASSERT_STREQ("hello", getenv("test-variable"));
275 ASSERT_EQ(0, unsetenv("test-variable"));
276 ASSERT_TRUE(getenv("test-variable") == NULL);
279 TEST(UNISTD_TEST, unsetenv_EINVAL) {
280 EXPECT_EQ(-1, unsetenv(""));
281 EXPECT_EQ(EINVAL, errno);
282 EXPECT_EQ(-1, unsetenv("a=b"));
283 EXPECT_EQ(EINVAL, errno);
286 TEST(UNISTD_TEST, setenv_EINVAL) {
287 EXPECT_EQ(-1, setenv(NULL, "value", 0));
288 EXPECT_EQ(EINVAL, errno);
289 EXPECT_EQ(-1, setenv(NULL, "value", 1));
290 EXPECT_EQ(EINVAL, errno);
291 EXPECT_EQ(-1, setenv("", "value", 0));
292 EXPECT_EQ(EINVAL, errno);
293 EXPECT_EQ(-1, setenv("", "value", 1));
294 EXPECT_EQ(EINVAL, errno);
295 EXPECT_EQ(-1, setenv("a=b", "value", 0));
296 EXPECT_EQ(EINVAL, errno);
297 EXPECT_EQ(-1, setenv("a=b", "value", 1));
298 EXPECT_EQ(EINVAL, errno);
301 TEST(UNISTD_TEST, setenv) {
302 ASSERT_EQ(0, unsetenv("test-variable"));
309 EXPECT_EQ(0, setenv("test-variable", a, 0));
310 EXPECT_STREQ(a, getenv("test-variable"));
312 // Existing value, no overwrite.
313 EXPECT_EQ(0, setenv("test-variable", b, 0));
314 EXPECT_STREQ(a, getenv("test-variable"));
316 // Existing value, overwrite.
317 EXPECT_EQ(0, setenv("test-variable", c, 1));
318 EXPECT_STREQ(c, getenv("test-variable"));
319 // But the arrays backing the values are unchanged.
320 EXPECT_EQ('a', a[0]);
321 EXPECT_EQ('b', b[0]);
322 EXPECT_EQ('c', c[0]);
324 ASSERT_EQ(0, unsetenv("test-variable"));
327 TEST(UNISTD_TEST, putenv) {
328 ASSERT_EQ(0, unsetenv("a"));
330 char* s1 = strdup("a=b");
331 ASSERT_EQ(0, putenv(s1));
333 ASSERT_STREQ("b", getenv("a"));
335 ASSERT_STREQ("c", getenv("a"));
337 char* s2 = strdup("a=b");
338 ASSERT_EQ(0, putenv(s2));
340 ASSERT_STREQ("b", getenv("a"));
341 ASSERT_EQ('c', s1[2]);
343 ASSERT_EQ(0, unsetenv("a"));
348 TEST(UNISTD_TEST, clearenv) {
349 extern char** environ;
351 // Guarantee that environ is not initially empty...
352 ASSERT_EQ(0, setenv("test-variable", "a", 1));
355 std::vector<char*> old_environ;
356 for (size_t i = 0; environ[i] != NULL; ++i) {
357 old_environ.push_back(strdup(environ[i]));
360 ASSERT_EQ(0, clearenv());
362 EXPECT_TRUE(environ == NULL || environ[0] == NULL);
363 EXPECT_EQ(NULL, getenv("test-variable"));
364 EXPECT_EQ(0, setenv("test-variable", "post-clear", 1));
365 EXPECT_STREQ("post-clear", getenv("test-variable"));
367 // Put the old environment back.
368 for (size_t i = 0; i < old_environ.size(); ++i) {
369 EXPECT_EQ(0, putenv(old_environ[i]));
372 // Check it wasn't overwritten.
373 EXPECT_STREQ("a", getenv("test-variable"));
375 EXPECT_EQ(0, unsetenv("test-variable"));
378 static void TestFsyncFunction(int (*fn)(int)) {
381 // Can't sync an invalid fd.
383 EXPECT_EQ(-1, fn(-1));
384 EXPECT_EQ(EBADF, errno);
386 // It doesn't matter whether you've opened a file for write or not.
388 ASSERT_NE(-1, tf.fd);
390 EXPECT_EQ(0, fn(tf.fd));
392 ASSERT_NE(-1, fd = open(tf.filename, O_RDONLY));
393 EXPECT_EQ(0, fn(fd));
396 ASSERT_NE(-1, fd = open(tf.filename, O_RDWR));
397 EXPECT_EQ(0, fn(fd));
400 // The fd can even be a directory.
401 ASSERT_NE(-1, fd = open("/data/local/tmp", O_RDONLY));
402 EXPECT_EQ(0, fn(fd));
405 // But some file systems may choose to be fussy...
407 ASSERT_NE(-1, fd = open("/proc/version", O_RDONLY));
408 EXPECT_EQ(-1, fn(fd));
409 EXPECT_EQ(EINVAL, errno);
413 TEST(UNISTD_TEST, fdatasync) {
414 TestFsyncFunction(fdatasync);
417 TEST(UNISTD_TEST, fsync) {
418 TestFsyncFunction(fsync);
421 static void AssertGetPidCorrect() {
422 // The loop is just to make manual testing/debugging with strace easier.
423 pid_t getpid_syscall_result = syscall(__NR_getpid);
424 for (size_t i = 0; i < 128; ++i) {
425 ASSERT_EQ(getpid_syscall_result, getpid());
429 static void TestGetPidCachingWithFork(int (*fork_fn)(), void (*exit_fn)(int)) {
430 pid_t parent_pid = getpid();
431 ASSERT_EQ(syscall(__NR_getpid), parent_pid);
433 pid_t fork_result = fork_fn();
434 ASSERT_NE(fork_result, -1);
435 if (fork_result == 0) {
437 ASSERT_NO_FATAL_FAILURE(AssertGetPidCorrect());
438 ASSERT_EQ(parent_pid, getppid());
442 ASSERT_EQ(parent_pid, getpid());
443 AssertChildExited(fork_result, 123);
447 // gettid() is marked as __attribute_const__, which will have the compiler
448 // optimize out multiple calls to gettid in the same function. This wrapper
449 // defeats that optimization.
450 static __attribute__((__noinline__)) pid_t GetTidForTest() {
455 static void AssertGetTidCorrect() {
456 // The loop is just to make manual testing/debugging with strace easier.
457 pid_t gettid_syscall_result = syscall(__NR_gettid);
458 for (size_t i = 0; i < 128; ++i) {
459 ASSERT_EQ(gettid_syscall_result, GetTidForTest());
463 static void TestGetTidCachingWithFork(int (*fork_fn)(), void (*exit_fn)(int)) {
464 pid_t parent_tid = GetTidForTest();
465 ASSERT_EQ(syscall(__NR_gettid), parent_tid);
467 pid_t fork_result = fork_fn();
468 ASSERT_NE(fork_result, -1);
469 if (fork_result == 0) {
471 EXPECT_EQ(syscall(__NR_getpid), syscall(__NR_gettid));
472 EXPECT_EQ(getpid(), GetTidForTest()) << "real tid is " << syscall(__NR_gettid)
473 << ", pid is " << syscall(__NR_getpid);
474 ASSERT_NO_FATAL_FAILURE(AssertGetTidCorrect());
478 ASSERT_EQ(parent_tid, GetTidForTest());
479 AssertChildExited(fork_result, 123);
483 TEST(UNISTD_TEST, getpid_caching_and_fork) {
484 TestGetPidCachingWithFork(fork, exit);
487 TEST(UNISTD_TEST, gettid_caching_and_fork) {
488 TestGetTidCachingWithFork(fork, exit);
491 TEST(UNISTD_TEST, getpid_caching_and_vfork) {
492 TestGetPidCachingWithFork(vfork, _exit);
495 static int CloneLikeFork() {
496 return clone(nullptr, nullptr, SIGCHLD, nullptr);
499 TEST(UNISTD_TEST, getpid_caching_and_clone_process) {
500 TestGetPidCachingWithFork(CloneLikeFork, exit);
503 TEST(UNISTD_TEST, gettid_caching_and_clone_process) {
504 TestGetTidCachingWithFork(CloneLikeFork, exit);
507 static int CloneAndSetTid() {
509 pid_t parent_tid = GetTidForTest();
511 int rv = clone(nullptr, nullptr, CLONE_CHILD_SETTID | SIGCHLD, nullptr, nullptr, nullptr, &child_tid);
516 EXPECT_EQ(child_tid, GetTidForTest());
517 EXPECT_NE(child_tid, parent_tid);
519 EXPECT_NE(child_tid, GetTidForTest());
520 EXPECT_NE(child_tid, parent_tid);
521 EXPECT_EQ(GetTidForTest(), parent_tid);
527 TEST(UNISTD_TEST, gettid_caching_and_clone_process_settid) {
528 TestGetTidCachingWithFork(CloneAndSetTid, exit);
531 static int CloneStartRoutine(int (*start_routine)(void*)) {
532 void* child_stack[1024];
533 return clone(start_routine, &child_stack[1024], SIGCHLD, NULL);
536 static int GetPidCachingCloneStartRoutine(void*) {
537 AssertGetPidCorrect();
541 TEST(UNISTD_TEST, getpid_caching_and_clone) {
542 pid_t parent_pid = getpid();
543 ASSERT_EQ(syscall(__NR_getpid), parent_pid);
545 int clone_result = CloneStartRoutine(GetPidCachingCloneStartRoutine);
546 ASSERT_NE(clone_result, -1);
548 ASSERT_EQ(parent_pid, getpid());
550 AssertChildExited(clone_result, 123);
553 static int GetTidCachingCloneStartRoutine(void*) {
554 AssertGetTidCorrect();
558 TEST(UNISTD_TEST, gettid_caching_and_clone) {
559 pid_t parent_tid = GetTidForTest();
560 ASSERT_EQ(syscall(__NR_gettid), parent_tid);
562 int clone_result = CloneStartRoutine(GetTidCachingCloneStartRoutine);
563 ASSERT_NE(clone_result, -1);
565 ASSERT_EQ(parent_tid, GetTidForTest());
567 AssertChildExited(clone_result, 123);
570 static int CloneChildExit(void*) {
571 AssertGetPidCorrect();
572 AssertGetTidCorrect();
576 TEST(UNISTD_TEST, clone_fn_and_exit) {
577 int clone_result = CloneStartRoutine(CloneChildExit);
578 ASSERT_NE(-1, clone_result);
580 AssertGetPidCorrect();
581 AssertGetTidCorrect();
583 AssertChildExited(clone_result, 33);
586 static void* GetPidCachingPthreadStartRoutine(void*) {
587 AssertGetPidCorrect();
591 TEST(UNISTD_TEST, getpid_caching_and_pthread_create) {
592 pid_t parent_pid = getpid();
595 ASSERT_EQ(0, pthread_create(&t, NULL, GetPidCachingPthreadStartRoutine, NULL));
597 ASSERT_EQ(parent_pid, getpid());
600 ASSERT_EQ(0, pthread_join(t, &result));
601 ASSERT_EQ(NULL, result);
604 static void* GetTidCachingPthreadStartRoutine(void*) {
605 AssertGetTidCorrect();
606 uint64_t tid = GetTidForTest();
607 return reinterpret_cast<void*>(tid);
610 TEST(UNISTD_TEST, gettid_caching_and_pthread_create) {
611 pid_t parent_tid = GetTidForTest();
614 ASSERT_EQ(0, pthread_create(&t, NULL, GetTidCachingPthreadStartRoutine, &parent_tid));
616 ASSERT_EQ(parent_tid, GetTidForTest());
619 ASSERT_EQ(0, pthread_join(t, &result));
620 ASSERT_NE(static_cast<uint64_t>(parent_tid), reinterpret_cast<uint64_t>(result));
623 class UNISTD_DEATHTEST : public BionicDeathTest {};
625 TEST_F(UNISTD_DEATHTEST, abort) {
626 ASSERT_EXIT(abort(), testing::KilledBySignal(SIGABRT), "");
629 TEST(UNISTD_TEST, sethostname) {
630 // The permissions check happens before the argument check, so this will
631 // fail for a different reason if you're running as root than if you're
632 // not, but it'll fail either way. Checking that we have the symbol is about
633 // all we can do for sethostname(2).
634 ASSERT_EQ(-1, sethostname("", -1));
637 TEST(UNISTD_TEST, gethostname) {
638 char hostname[HOST_NAME_MAX + 1];
639 memset(hostname, 0, sizeof(hostname));
641 // Can we get the hostname with a big buffer?
642 ASSERT_EQ(0, gethostname(hostname, HOST_NAME_MAX));
644 // Can we get the hostname with a right-sized buffer?
646 ASSERT_EQ(0, gethostname(hostname, strlen(hostname) + 1));
648 // Does uname(2) agree?
650 ASSERT_EQ(0, uname(&buf));
651 ASSERT_EQ(0, strncmp(hostname, buf.nodename, SYS_NMLN));
652 ASSERT_GT(strlen(hostname), 0U);
654 // Do we correctly detect truncation?
656 ASSERT_EQ(-1, gethostname(hostname, strlen(hostname)));
657 ASSERT_EQ(ENAMETOOLONG, errno);
660 TEST(UNISTD_TEST, pathconf_fpathconf) {
663 // As a file system's block size is always power of 2, the configure values
664 // for ALLOC and XFER should be power of 2 as well.
665 rc = pathconf(tf.filename, _PC_ALLOC_SIZE_MIN);
666 ASSERT_TRUE(rc > 0 && powerof2(rc));
667 rc = pathconf(tf.filename, _PC_REC_MIN_XFER_SIZE);
668 ASSERT_TRUE(rc > 0 && powerof2(rc));
669 rc = pathconf(tf.filename, _PC_REC_XFER_ALIGN);
670 ASSERT_TRUE(rc > 0 && powerof2(rc));
672 rc = fpathconf(tf.fd, _PC_ALLOC_SIZE_MIN);
673 ASSERT_TRUE(rc > 0 && powerof2(rc));
674 rc = fpathconf(tf.fd, _PC_REC_MIN_XFER_SIZE);
675 ASSERT_TRUE(rc > 0 && powerof2(rc));
676 rc = fpathconf(tf.fd, _PC_REC_XFER_ALIGN);
677 ASSERT_TRUE(rc > 0 && powerof2(rc));
681 TEST(UNISTD_TEST, _POSIX_macros_smoke) {
682 // Make a tight verification of _POSIX_* / _POSIX2_* / _XOPEN_* macros, to prevent change by mistake.
683 // Verify according to POSIX.1-2008.
684 EXPECT_EQ(200809L, _POSIX_VERSION);
686 EXPECT_EQ(_POSIX_VERSION, _POSIX_ADVISORY_INFO);
687 EXPECT_GT(_POSIX_AIO_LISTIO_MAX, 0);
688 EXPECT_GT(_POSIX_AIO_MAX, 0);
689 EXPECT_GT(_POSIX_ARG_MAX, 0);
690 EXPECT_GT(_POSIX_BARRIERS, 0);
691 EXPECT_GT(_POSIX_SPIN_LOCKS, 0);
692 EXPECT_GT(_POSIX_CHILD_MAX, 0);
693 EXPECT_NE(_POSIX_CHOWN_RESTRICTED, -1);
694 EXPECT_EQ(_POSIX_VERSION, _POSIX_CLOCK_SELECTION);
695 EXPECT_EQ(0, _POSIX_CPUTIME); // Use sysconf to detect support at runtime.
696 EXPECT_GT(_POSIX_DELAYTIMER_MAX, 0);
697 EXPECT_EQ(_POSIX_VERSION, _POSIX_FSYNC);
698 EXPECT_GT(_POSIX_HOST_NAME_MAX, 0);
699 EXPECT_EQ(_POSIX_VERSION, _POSIX_IPV6);
700 EXPECT_GT(_POSIX_JOB_CONTROL, 0);
701 EXPECT_GT(_POSIX_LINK_MAX, 0);
702 EXPECT_GT(_POSIX_LOGIN_NAME_MAX, 0);
703 EXPECT_EQ(_POSIX_VERSION, _POSIX_MAPPED_FILES);
704 EXPECT_GT(_POSIX_MAX_CANON, 0);
705 EXPECT_GT(_POSIX_MAX_INPUT, 0);
706 EXPECT_EQ(_POSIX_VERSION, _POSIX_MEMLOCK);
707 EXPECT_EQ(_POSIX_VERSION, _POSIX_MEMLOCK_RANGE);
708 EXPECT_EQ(_POSIX_VERSION, _POSIX_MEMORY_PROTECTION);
709 EXPECT_EQ(0, _POSIX_MONOTONIC_CLOCK);
710 EXPECT_GT(_POSIX_MQ_OPEN_MAX, 0);
711 EXPECT_GT(_POSIX_MQ_PRIO_MAX, 0);
712 EXPECT_GT(_POSIX_NAME_MAX, 0);
713 EXPECT_GT(_POSIX_NGROUPS_MAX, 0);
714 EXPECT_GT(_POSIX_NO_TRUNC, 0);
715 EXPECT_GT(_POSIX_OPEN_MAX, 0);
716 EXPECT_GT(_POSIX_PATH_MAX, 0);
717 EXPECT_GT(_POSIX_PIPE_BUF, 0);
718 EXPECT_EQ(_POSIX_VERSION, _POSIX_PRIORITY_SCHEDULING);
719 EXPECT_EQ(_POSIX_VERSION, _POSIX_RAW_SOCKETS);
720 EXPECT_EQ(_POSIX_VERSION, _POSIX_READER_WRITER_LOCKS);
721 EXPECT_EQ(_POSIX_VERSION, _POSIX_REALTIME_SIGNALS);
722 EXPECT_GT(_POSIX_REGEXP, 0);
723 EXPECT_GT(_POSIX_RE_DUP_MAX, 0);
724 EXPECT_GT(_POSIX_SAVED_IDS, 0);
725 EXPECT_EQ(_POSIX_VERSION, _POSIX_SEMAPHORES);
726 EXPECT_GT(_POSIX_SEM_NSEMS_MAX, 0);
727 EXPECT_GT(_POSIX_SEM_VALUE_MAX, 0);
728 EXPECT_GT(_POSIX_SHELL, 0);
729 EXPECT_GT(_POSIX_SIGQUEUE_MAX, 0);
730 EXPECT_EQ(-1, _POSIX_SPORADIC_SERVER);
731 EXPECT_GT(_POSIX_SSIZE_MAX, 0);
732 EXPECT_GT(_POSIX_STREAM_MAX, 0);
733 EXPECT_GT(_POSIX_SYMLINK_MAX, 0);
734 EXPECT_GT(_POSIX_SYMLOOP_MAX, 0);
735 EXPECT_EQ(_POSIX_VERSION, _POSIX_SYNCHRONIZED_IO);
736 EXPECT_EQ(_POSIX_VERSION, _POSIX_THREADS);
737 EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_ATTR_STACKADDR);
738 EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_ATTR_STACKSIZE);
739 EXPECT_EQ(0, _POSIX_THREAD_CPUTIME); // Use sysconf to detect support at runtime.
740 EXPECT_GT(_POSIX_THREAD_DESTRUCTOR_ITERATIONS, 0);
741 EXPECT_EQ(_POSIX_THREAD_KEYS_MAX, 128);
742 EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_PRIORITY_SCHEDULING);
743 EXPECT_EQ(-1, _POSIX_THREAD_PRIO_INHERIT);
744 EXPECT_EQ(-1, _POSIX_THREAD_PRIO_PROTECT);
745 EXPECT_EQ(-1, _POSIX_THREAD_ROBUST_PRIO_PROTECT);
746 EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_SAFE_FUNCTIONS);
747 EXPECT_EQ(-1, _POSIX_THREAD_SPORADIC_SERVER);
748 EXPECT_GT(_POSIX_THREAD_THREADS_MAX, 0);
749 EXPECT_EQ(_POSIX_VERSION, _POSIX_TIMEOUTS);
750 EXPECT_EQ(_POSIX_VERSION, _POSIX_TIMERS);
751 EXPECT_GT(_POSIX_TIMER_MAX, 0);
752 EXPECT_EQ(-1, _POSIX_TRACE);
753 EXPECT_EQ(-1, _POSIX_TRACE_EVENT_FILTER);
754 EXPECT_EQ(-1, _POSIX_TRACE_INHERIT);
755 EXPECT_EQ(-1, _POSIX_TRACE_LOG);
756 EXPECT_GT(_POSIX_TTY_NAME_MAX, 0);
757 EXPECT_EQ(-1, _POSIX_TYPED_MEMORY_OBJECTS);
758 EXPECT_GT(_POSIX_TZNAME_MAX, 0);
759 EXPECT_NE(-1, _POSIX_VDISABLE);
761 EXPECT_GT(_POSIX2_BC_BASE_MAX, 0);
762 EXPECT_GT(_POSIX2_BC_DIM_MAX, 0);
763 EXPECT_GT(_POSIX2_BC_SCALE_MAX, 0);
764 EXPECT_GT(_POSIX2_BC_STRING_MAX, 0);
765 EXPECT_GT(_POSIX2_CHARCLASS_NAME_MAX, 0);
766 EXPECT_GT(_POSIX2_COLL_WEIGHTS_MAX, 0);
767 EXPECT_EQ(_POSIX_VERSION, _POSIX2_C_BIND);
768 EXPECT_GT(_POSIX2_EXPR_NEST_MAX, 0);
769 EXPECT_GT(_POSIX2_LINE_MAX, 0);
770 EXPECT_GT(_POSIX2_RE_DUP_MAX, 0);
772 EXPECT_EQ(700, _XOPEN_VERSION);
773 EXPECT_GT(_XOPEN_IOV_MAX, 0);
774 EXPECT_GT(_XOPEN_UNIX, 0);
776 #if defined(__BIONIC__)
777 // These tests only pass on bionic, as bionic and glibc has different support on these macros.
778 // Macros like _POSIX_ASYNCHRONOUS_IO are not supported on bionic yet.
779 EXPECT_EQ(-1, _POSIX_ASYNCHRONOUS_IO);
780 EXPECT_EQ(-1, _POSIX_MESSAGE_PASSING);
781 EXPECT_EQ(-1, _POSIX_PRIORITIZED_IO);
782 EXPECT_EQ(-1, _POSIX_SHARED_MEMORY_OBJECTS);
783 EXPECT_EQ(-1, _POSIX_SPAWN);
784 EXPECT_EQ(-1, _POSIX_THREAD_PROCESS_SHARED);
785 EXPECT_EQ(-1, _POSIX_THREAD_ROBUST_PRIO_INHERIT);
787 EXPECT_EQ(-1, _POSIX2_VERSION);
788 EXPECT_EQ(-1, _POSIX2_CHAR_TERM);
789 EXPECT_EQ(-1, _POSIX2_C_DEV);
790 EXPECT_EQ(-1, _POSIX2_LOCALEDEF);
791 EXPECT_EQ(-1, _POSIX2_SW_DEV);
792 EXPECT_EQ(-1, _POSIX2_UPE);
794 EXPECT_EQ(-1, _XOPEN_ENH_I18N);
795 EXPECT_EQ(-1, _XOPEN_CRYPT);
796 EXPECT_EQ(-1, _XOPEN_LEGACY);
797 EXPECT_EQ(-1, _XOPEN_REALTIME);
798 EXPECT_EQ(-1, _XOPEN_REALTIME_THREADS);
799 EXPECT_EQ(-1, _XOPEN_SHM);
801 #endif // defined(__BIONIC__)
804 #define VERIFY_SYSCONF_UNSUPPORTED(name) VerifySysconf(name, #name, [](long v){return v == -1;})
806 // sysconf() means unlimited when it returns -1 with errno unchanged.
807 #define VERIFY_SYSCONF_POSITIVE(name) \
808 VerifySysconf(name, #name, [](long v){return (v > 0 || v == -1);})
810 #define VERIFY_SYSCONF_POSIX_VERSION(name) \
811 VerifySysconf(name, #name, [](long v){return v == _POSIX_VERSION;})
813 static void VerifySysconf(int option, const char *option_name, bool (*verify)(long)) {
815 long ret = sysconf(option);
816 EXPECT_TRUE(0 == errno && verify(ret)) << "name = " << option_name << ", ret = "
817 << ret <<", Error Message: " << strerror(errno);
820 TEST(UNISTD_TEST, sysconf) {
821 VERIFY_SYSCONF_POSIX_VERSION(_SC_ADVISORY_INFO);
822 VERIFY_SYSCONF_POSITIVE(_SC_ARG_MAX);
823 VERIFY_SYSCONF_POSIX_VERSION(_SC_BARRIERS);
824 VERIFY_SYSCONF_POSITIVE(_SC_BC_BASE_MAX);
825 VERIFY_SYSCONF_POSITIVE(_SC_BC_DIM_MAX);
826 VERIFY_SYSCONF_POSITIVE(_SC_BC_SCALE_MAX);
827 VERIFY_SYSCONF_POSITIVE(_SC_CHILD_MAX);
828 VERIFY_SYSCONF_POSITIVE(_SC_CLK_TCK);
829 VERIFY_SYSCONF_POSITIVE(_SC_COLL_WEIGHTS_MAX);
830 VERIFY_SYSCONF_POSIX_VERSION(_SC_CPUTIME);
831 VERIFY_SYSCONF_POSITIVE(_SC_EXPR_NEST_MAX);
832 VERIFY_SYSCONF_POSITIVE(_SC_LINE_MAX);
833 VERIFY_SYSCONF_POSITIVE(_SC_NGROUPS_MAX);
834 VERIFY_SYSCONF_POSITIVE(_SC_OPEN_MAX);
835 VERIFY_SYSCONF_POSITIVE(_SC_PASS_MAX);
836 VERIFY_SYSCONF_POSIX_VERSION(_SC_2_C_BIND);
837 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_FORT_DEV);
838 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_FORT_RUN);
839 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_UPE);
840 VERIFY_SYSCONF_POSITIVE(_SC_JOB_CONTROL);
841 VERIFY_SYSCONF_POSITIVE(_SC_SAVED_IDS);
842 VERIFY_SYSCONF_POSIX_VERSION(_SC_VERSION);
843 VERIFY_SYSCONF_POSITIVE(_SC_RE_DUP_MAX);
844 VERIFY_SYSCONF_POSITIVE(_SC_STREAM_MAX);
845 VERIFY_SYSCONF_POSITIVE(_SC_TZNAME_MAX);
846 VerifySysconf(_SC_XOPEN_VERSION, "_SC_XOPEN_VERSION", [](long v){return v == _XOPEN_VERSION;});
847 VERIFY_SYSCONF_POSITIVE(_SC_ATEXIT_MAX);
848 VERIFY_SYSCONF_POSITIVE(_SC_IOV_MAX);
849 VERIFY_SYSCONF_POSITIVE(_SC_PAGESIZE);
850 VERIFY_SYSCONF_POSITIVE(_SC_PAGE_SIZE);
851 VerifySysconf(_SC_PAGE_SIZE, "_SC_PAGE_SIZE",
852 [](long v){return v == sysconf(_SC_PAGESIZE) && v == getpagesize();});
853 VERIFY_SYSCONF_POSITIVE(_SC_XOPEN_UNIX);
854 VERIFY_SYSCONF_POSITIVE(_SC_AIO_LISTIO_MAX);
855 VERIFY_SYSCONF_POSITIVE(_SC_AIO_MAX);
856 VerifySysconf(_SC_AIO_PRIO_DELTA_MAX, "_SC_AIO_PRIO_DELTA_MAX", [](long v){return v >= 0;});
857 VERIFY_SYSCONF_POSITIVE(_SC_DELAYTIMER_MAX);
858 VERIFY_SYSCONF_POSITIVE(_SC_MQ_OPEN_MAX);
859 VERIFY_SYSCONF_POSITIVE(_SC_MQ_PRIO_MAX);
860 VERIFY_SYSCONF_POSITIVE(_SC_RTSIG_MAX);
861 VERIFY_SYSCONF_POSITIVE(_SC_SEM_NSEMS_MAX);
862 VERIFY_SYSCONF_POSITIVE(_SC_SEM_VALUE_MAX);
863 VERIFY_SYSCONF_POSIX_VERSION(_SC_SPIN_LOCKS);
864 VERIFY_SYSCONF_POSITIVE(_SC_TIMER_MAX);
865 VERIFY_SYSCONF_POSIX_VERSION(_SC_FSYNC);
866 VERIFY_SYSCONF_POSIX_VERSION(_SC_MAPPED_FILES);
867 VERIFY_SYSCONF_POSIX_VERSION(_SC_MEMLOCK);
868 VERIFY_SYSCONF_POSIX_VERSION(_SC_MEMLOCK_RANGE);
869 VERIFY_SYSCONF_POSIX_VERSION(_SC_MEMORY_PROTECTION);
870 VERIFY_SYSCONF_POSIX_VERSION(_SC_PRIORITY_SCHEDULING);
871 VERIFY_SYSCONF_POSIX_VERSION(_SC_REALTIME_SIGNALS);
872 VERIFY_SYSCONF_POSIX_VERSION(_SC_SEMAPHORES);
873 VERIFY_SYSCONF_POSIX_VERSION(_SC_SYNCHRONIZED_IO);
874 VERIFY_SYSCONF_POSIX_VERSION(_SC_TIMERS);
875 VERIFY_SYSCONF_POSITIVE(_SC_GETGR_R_SIZE_MAX);
876 VERIFY_SYSCONF_POSITIVE(_SC_GETPW_R_SIZE_MAX);
877 VERIFY_SYSCONF_POSITIVE(_SC_LOGIN_NAME_MAX);
878 VERIFY_SYSCONF_POSITIVE(_SC_THREAD_DESTRUCTOR_ITERATIONS);
879 VERIFY_SYSCONF_POSITIVE(_SC_THREAD_KEYS_MAX);
880 VERIFY_SYSCONF_POSITIVE(_SC_THREAD_STACK_MIN);
881 VERIFY_SYSCONF_POSITIVE(_SC_THREAD_THREADS_MAX);
882 VERIFY_SYSCONF_POSITIVE(_SC_TTY_NAME_MAX);
883 VERIFY_SYSCONF_POSIX_VERSION(_SC_THREADS);
884 VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_ATTR_STACKADDR);
885 VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_ATTR_STACKSIZE);
886 VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_PRIORITY_SCHEDULING);
887 VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_PRIO_INHERIT);
888 VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_PRIO_PROTECT);
889 VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_SAFE_FUNCTIONS);
890 VERIFY_SYSCONF_POSITIVE(_SC_NPROCESSORS_CONF);
891 VERIFY_SYSCONF_POSITIVE(_SC_NPROCESSORS_ONLN);
892 VERIFY_SYSCONF_POSITIVE(_SC_PHYS_PAGES);
893 VERIFY_SYSCONF_POSITIVE(_SC_AVPHYS_PAGES);
894 VERIFY_SYSCONF_POSIX_VERSION(_SC_MONOTONIC_CLOCK);
895 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS);
896 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS_ACCOUNTING);
897 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS_CHECKPOINT);
898 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS_LOCATE);
899 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS_MESSAGE);
900 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS_TRACK);
901 VERIFY_SYSCONF_POSIX_VERSION(_SC_CLOCK_SELECTION);
902 VERIFY_SYSCONF_POSITIVE(_SC_HOST_NAME_MAX);
903 VERIFY_SYSCONF_POSIX_VERSION(_SC_IPV6);
904 VERIFY_SYSCONF_POSIX_VERSION(_SC_RAW_SOCKETS);
905 VERIFY_SYSCONF_POSIX_VERSION(_SC_READER_WRITER_LOCKS);
906 VERIFY_SYSCONF_POSITIVE(_SC_REGEXP);
907 VERIFY_SYSCONF_POSITIVE(_SC_SHELL);
908 VERIFY_SYSCONF_UNSUPPORTED(_SC_SPORADIC_SERVER);
909 VERIFY_SYSCONF_POSITIVE(_SC_SYMLOOP_MAX);
910 VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_CPUTIME);
911 VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_SPORADIC_SERVER);
912 VERIFY_SYSCONF_POSIX_VERSION(_SC_TIMEOUTS);
913 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE);
914 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_EVENT_FILTER);
915 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_EVENT_NAME_MAX);
916 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_INHERIT);
917 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_LOG);
918 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_NAME_MAX);
919 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_SYS_MAX);
920 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_USER_EVENT_MAX);
921 VERIFY_SYSCONF_UNSUPPORTED(_SC_TYPED_MEMORY_OBJECTS);
922 VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_STREAMS);
924 #if defined(__LP64__)
925 VERIFY_SYSCONF_UNSUPPORTED(_SC_V7_ILP32_OFF32);
926 VERIFY_SYSCONF_UNSUPPORTED(_SC_V7_ILP32_OFFBIG);
927 VERIFY_SYSCONF_POSITIVE(_SC_V7_LP64_OFF64);
928 VERIFY_SYSCONF_POSITIVE(_SC_V7_LPBIG_OFFBIG);
930 VERIFY_SYSCONF_POSITIVE(_SC_V7_ILP32_OFF32);
931 #if defined(__BIONIC__)
932 // bionic does not support 64 bits off_t type on 32bit machine.
933 VERIFY_SYSCONF_UNSUPPORTED(_SC_V7_ILP32_OFFBIG);
935 VERIFY_SYSCONF_UNSUPPORTED(_SC_V7_LP64_OFF64);
936 VERIFY_SYSCONF_UNSUPPORTED(_SC_V7_LPBIG_OFFBIG);
939 #if defined(__BIONIC__)
940 // Tests can only run on bionic, as bionic and glibc have different support for these options.
941 // Below options are not supported on bionic yet.
942 VERIFY_SYSCONF_UNSUPPORTED(_SC_ASYNCHRONOUS_IO);
943 VERIFY_SYSCONF_UNSUPPORTED(_SC_MESSAGE_PASSING);
944 VERIFY_SYSCONF_UNSUPPORTED(_SC_PRIORITIZED_IO);
945 VERIFY_SYSCONF_UNSUPPORTED(_SC_SHARED_MEMORY_OBJECTS);
946 VERIFY_SYSCONF_UNSUPPORTED(_SC_SPAWN);
947 VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_PROCESS_SHARED);
948 VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_ROBUST_PRIO_INHERIT);
949 VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_ROBUST_PRIO_PROTECT);
951 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_C_DEV);
952 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_CHAR_TERM);
953 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_LOCALEDEF);
954 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_SW_DEV);
955 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_VERSION);
957 VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_CRYPT);
958 VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_ENH_I18N);
959 VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_LEGACY);
960 VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_REALTIME);
961 VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_REALTIME_THREADS);
962 VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_SHM);
963 VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_UUCP);
964 #endif // defined(__BIONIC__)
967 TEST(UNISTD_TEST, get_cpu_count_from_string) {
968 ASSERT_EQ(0, GetCpuCountFromString(" "));
969 ASSERT_EQ(1, GetCpuCountFromString("0"));
970 ASSERT_EQ(40, GetCpuCountFromString("0-39"));
971 ASSERT_EQ(4, GetCpuCountFromString("0, 1-2, 4\n"));
974 TEST(UNISTD_TEST, sysconf_SC_NPROCESSORS_ONLN) {
976 ASSERT_TRUE(android::base::ReadFileToString("/sys/devices/system/cpu/online", &line));
977 long online_cpus = 0;
978 for (const std::string& s : android::base::Split(line, ",")) {
979 std::vector<std::string> numbers = android::base::Split(s, "-");
980 if (numbers.size() == 1u) {
983 online_cpus += atoi(numbers[1].c_str()) - atoi(numbers[0].c_str()) + 1;
986 ASSERT_EQ(online_cpus, sysconf(_SC_NPROCESSORS_ONLN));
989 TEST(UNISTD_TEST, sysconf_SC_ARG_MAX) {
990 // Since Linux 2.6.23, ARG_MAX isn't a constant and depends on RLIMIT_STACK.
992 // Get our current limit, and set things up so we restore the limit.
994 ASSERT_EQ(0, getrlimit(RLIMIT_STACK, &rl));
995 uint64_t original_rlim_cur = rl.rlim_cur;
996 if (rl.rlim_cur == RLIM_INFINITY) {
997 rl.rlim_cur = 8 * 1024 * 1024; // Bionic reports unlimited stacks as 8MiB.
999 auto guard = make_scope_guard([&rl, original_rlim_cur]() {
1000 rl.rlim_cur = original_rlim_cur;
1001 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1004 // _SC_ARG_MAX should be 1/4 the stack size.
1005 EXPECT_EQ(static_cast<long>(rl.rlim_cur / 4), sysconf(_SC_ARG_MAX));
1007 // If you have a really small stack, the kernel still guarantees "32 pages" (fs/exec.c).
1009 rl.rlim_max = RLIM_INFINITY;
1010 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1012 EXPECT_EQ(static_cast<long>(32 * sysconf(_SC_PAGE_SIZE)), sysconf(_SC_ARG_MAX));
1014 // With a 128-page stack limit, we know exactly what _SC_ARG_MAX should be...
1015 rl.rlim_cur = 128 * sysconf(_SC_PAGE_SIZE);
1016 rl.rlim_max = RLIM_INFINITY;
1017 ASSERT_EQ(0, setrlimit(RLIMIT_STACK, &rl));
1019 EXPECT_EQ(static_cast<long>((128 * sysconf(_SC_PAGE_SIZE)) / 4), sysconf(_SC_ARG_MAX));
1022 TEST(UNISTD_TEST, dup2_same) {
1023 // POSIX says of dup2:
1024 // If fildes2 is already a valid open file descriptor ...
1025 // [and] fildes is equal to fildes2 ... dup2() shall return
1026 // fildes2 without closing it.
1027 // This isn't true of dup3(2), so we need to manually implement that.
1030 int fd = open("/proc/version", O_RDONLY);
1031 ASSERT_TRUE(fd != -1);
1032 ASSERT_EQ(fd, dup2(fd, fd));
1033 ASSERT_EQ(0, close(fd)); // Check that dup2 didn't close fd.
1035 // Equal, but invalid.
1037 ASSERT_EQ(-1, dup2(fd, fd));
1038 ASSERT_EQ(EBADF, errno);
1041 TEST(UNISTD_TEST, lockf_smoke) {
1042 constexpr off64_t file_size = 32*1024LL;
1045 ASSERT_EQ(0, ftruncate(tf.fd, file_size));
1048 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1049 ASSERT_EQ(0, lockf64(tf.fd, F_LOCK, file_size));
1051 // Try-lock everything, this should succeed too.
1052 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1053 ASSERT_EQ(0, lockf64(tf.fd, F_TLOCK, file_size));
1056 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1057 ASSERT_EQ(0, lockf64(tf.fd, F_TEST, file_size));
1060 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1061 ASSERT_EQ(0, lockf64(tf.fd, F_ULOCK, file_size));
1064 TEST(UNISTD_TEST, lockf_zero) {
1065 constexpr off64_t file_size = 32*1024LL;
1068 ASSERT_EQ(0, ftruncate(tf.fd, file_size));
1070 // Lock everything by specifying a size of 0 (meaning "to the end, even if it changes").
1071 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1072 ASSERT_EQ(0, lockf64(tf.fd, F_LOCK, 0));
1074 // Check that it's locked.
1075 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1076 ASSERT_EQ(0, lockf64(tf.fd, F_TEST, file_size));
1079 ASSERT_EQ(0, ftruncate(tf.fd, 2*file_size));
1081 // Check that the new section is locked too.
1082 ASSERT_EQ(file_size, lseek64(tf.fd, file_size, SEEK_SET));
1083 ASSERT_EQ(0, lockf64(tf.fd, F_TEST, 2*file_size));
1086 TEST(UNISTD_TEST, lockf_negative) {
1087 constexpr off64_t file_size = 32*1024LL;
1090 ASSERT_EQ(0, ftruncate(tf.fd, file_size));
1092 // Lock everything, but specifying the range in reverse.
1093 ASSERT_EQ(file_size, lseek64(tf.fd, file_size, SEEK_SET));
1094 ASSERT_EQ(0, lockf64(tf.fd, F_LOCK, -file_size));
1096 // Check that it's locked.
1097 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1098 ASSERT_EQ(0, lockf64(tf.fd, F_TEST, file_size));
1101 TEST(UNISTD_TEST, lockf_with_child) {
1102 constexpr off64_t file_size = 32*1024LL;
1105 ASSERT_EQ(0, ftruncate(tf.fd, file_size));
1108 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1109 ASSERT_EQ(0, lockf64(tf.fd, F_LOCK, file_size));
1111 // Fork a child process
1115 // Check that the child cannot lock the file.
1116 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1117 ASSERT_EQ(-1, lockf64(tf.fd, F_TLOCK, file_size));
1118 ASSERT_EQ(EAGAIN, errno);
1119 // Check also that it reports itself as locked.
1120 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1121 ASSERT_EQ(-1, lockf64(tf.fd, F_TEST, file_size));
1122 ASSERT_EQ(EACCES, errno);
1125 AssertChildExited(pid, 0);
1128 TEST(UNISTD_TEST, lockf_partial_with_child) {
1129 constexpr off64_t file_size = 32*1024LL;
1132 ASSERT_EQ(0, ftruncate(tf.fd, file_size));
1134 // Lock the first half of the file.
1135 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1136 ASSERT_EQ(0, lockf64(tf.fd, F_LOCK, file_size/2));
1138 // Fork a child process.
1142 // Check that the child can lock the other half.
1143 ASSERT_EQ(file_size/2, lseek64(tf.fd, file_size/2, SEEK_SET));
1144 ASSERT_EQ(0, lockf64(tf.fd, F_TLOCK, file_size/2));
1145 // Check that the child cannot lock the first half.
1146 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1147 ASSERT_EQ(-1, lockf64(tf.fd, F_TEST, file_size/2));
1148 ASSERT_EQ(EACCES, errno);
1149 // Check also that it reports itself as locked.
1150 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1151 ASSERT_EQ(-1, lockf64(tf.fd, F_TEST, file_size/2));
1152 ASSERT_EQ(EACCES, errno);
1155 AssertChildExited(pid, 0);
1157 // The second half was locked by the child, but the lock disappeared
1158 // when the process exited, so check it can be locked now.
1159 ASSERT_EQ(file_size/2, lseek64(tf.fd, file_size/2, SEEK_SET));
1160 ASSERT_EQ(0, lockf64(tf.fd, F_TLOCK, file_size/2));
1163 TEST(UNISTD_TEST, getdomainname) {
1165 ASSERT_EQ(0, uname(&u));
1167 char buf[sizeof(u.domainname)];
1168 ASSERT_EQ(0, getdomainname(buf, sizeof(buf)));
1169 EXPECT_STREQ(u.domainname, buf);
1171 #if defined(__BIONIC__)
1172 // bionic and glibc have different behaviors when len is too small
1173 ASSERT_EQ(-1, getdomainname(buf, strlen(u.domainname)));
1174 EXPECT_EQ(EINVAL, errno);
1178 TEST(UNISTD_TEST, setdomainname) {
1179 __user_cap_header_struct header;
1180 memset(&header, 0, sizeof(header));
1181 header.version = _LINUX_CAPABILITY_VERSION_3;
1183 __user_cap_data_struct old_caps[_LINUX_CAPABILITY_U32S_3];
1184 ASSERT_EQ(0, capget(&header, &old_caps[0]));
1186 auto admin_idx = CAP_TO_INDEX(CAP_SYS_ADMIN);
1187 auto admin_mask = CAP_TO_MASK(CAP_SYS_ADMIN);
1188 bool has_admin = old_caps[admin_idx].effective & admin_mask;
1190 __user_cap_data_struct new_caps[_LINUX_CAPABILITY_U32S_3];
1191 memcpy(new_caps, old_caps, sizeof(new_caps));
1192 new_caps[admin_idx].effective &= ~admin_mask;
1194 ASSERT_EQ(0, capset(&header, &new_caps[0])) << "failed to drop admin privileges";
1197 const char* name = "newdomainname";
1198 ASSERT_EQ(-1, setdomainname(name, strlen(name)));
1199 ASSERT_EQ(EPERM, errno);
1202 ASSERT_EQ(0, capset(&header, &old_caps[0])) << "failed to restore admin privileges";
1206 #if defined(__GLIBC__)
1207 #define BIN_DIR "/bin/"
1209 #define BIN_DIR "/system/bin/"
1212 TEST(UNISTD_TEST, execve_failure) {
1215 ASSERT_EQ(-1, execve("/", eth.GetArgs(), eth.GetEnv()));
1216 ASSERT_EQ(EACCES, errno);
1219 TEST(UNISTD_TEST, execve_args) {
1220 // int execve(const char* path, char* argv[], char* envp[]);
1222 // Test basic argument passing.
1224 eth.SetArgs({"echo", "hello", "world", nullptr});
1225 eth.Run([&]() { execve(BIN_DIR "echo", eth.GetArgs(), eth.GetEnv()); }, 0, "hello world\n");
1227 // Test environment variable setting too.
1228 eth.SetArgs({"printenv", nullptr});
1229 eth.SetEnv({"A=B", nullptr});
1230 eth.Run([&]() { execve(BIN_DIR "printenv", eth.GetArgs(), eth.GetEnv()); }, 0, "A=B\n");
1233 TEST(UNISTD_TEST, execl_failure) {
1235 ASSERT_EQ(-1, execl("/", "/", nullptr));
1236 ASSERT_EQ(EACCES, errno);
1239 TEST(UNISTD_TEST, execl) {
1241 // int execl(const char* path, const char* arg, ...);
1242 eth.Run([&]() { execl(BIN_DIR "echo", "echo", "hello", "world", nullptr); }, 0, "hello world\n");
1245 TEST(UNISTD_TEST, execle_failure) {
1248 ASSERT_EQ(-1, execle("/", "/", nullptr, eth.GetEnv()));
1249 ASSERT_EQ(EACCES, errno);
1252 TEST(UNISTD_TEST, execle) {
1254 eth.SetEnv({"A=B", nullptr});
1255 // int execle(const char* path, const char* arg, ..., char* envp[]);
1256 eth.Run([&]() { execle(BIN_DIR "printenv", "printenv", nullptr, eth.GetEnv()); }, 0, "A=B\n");
1259 TEST(UNISTD_TEST, execv_failure) {
1262 ASSERT_EQ(-1, execv("/", eth.GetArgs()));
1263 ASSERT_EQ(EACCES, errno);
1266 TEST(UNISTD_TEST, execv) {
1268 eth.SetArgs({"echo", "hello", "world", nullptr});
1269 // int execv(const char* path, char* argv[]);
1270 eth.Run([&]() { execv(BIN_DIR "echo", eth.GetArgs()); }, 0, "hello world\n");
1273 TEST(UNISTD_TEST, execlp_failure) {
1275 ASSERT_EQ(-1, execlp("/", "/", nullptr));
1276 ASSERT_EQ(EACCES, errno);
1279 TEST(UNISTD_TEST, execlp) {
1281 // int execlp(const char* file, const char* arg, ...);
1282 eth.Run([&]() { execlp("echo", "echo", "hello", "world", nullptr); }, 0, "hello world\n");
1285 TEST(UNISTD_TEST, execvp_failure) {
1287 eth.SetArgs({nullptr});
1289 ASSERT_EQ(-1, execvp("/", eth.GetArgs()));
1290 ASSERT_EQ(EACCES, errno);
1293 TEST(UNISTD_TEST, execvp) {
1295 eth.SetArgs({"echo", "hello", "world", nullptr});
1296 // int execvp(const char* file, char* argv[]);
1297 eth.Run([&]() { execvp("echo", eth.GetArgs()); }, 0, "hello world\n");
1300 TEST(UNISTD_TEST, execvpe_failure) {
1303 ASSERT_EQ(-1, execvpe("this-does-not-exist", eth.GetArgs(), eth.GetEnv()));
1304 // Running in CTS we might not even be able to search all directories in $PATH.
1305 ASSERT_TRUE(errno == ENOENT || errno == EACCES);
1308 TEST(UNISTD_TEST, execvpe) {
1309 // int execvpe(const char* file, char* argv[], char* envp[]);
1311 // Test basic argument passing.
1313 eth.SetArgs({"echo", "hello", "world", nullptr});
1314 eth.Run([&]() { execvpe("echo", eth.GetArgs(), eth.GetEnv()); }, 0, "hello world\n");
1316 // Test environment variable setting too.
1317 eth.SetArgs({"printenv", nullptr});
1318 eth.SetEnv({"A=B", nullptr});
1319 eth.Run([&]() { execvpe("printenv", eth.GetArgs(), eth.GetEnv()); }, 0, "A=B\n");
1322 TEST(UNISTD_TEST, execvpe_ENOEXEC) {
1323 // Create a shell script with #!.
1325 ASSERT_TRUE(android::base::WriteStringToFile("#!" BIN_DIR "sh\necho script\n", tf.filename));
1327 // Set $PATH so we can find it.
1328 setenv("PATH", dirname(tf.filename), 1);
1331 eth.SetArgs({basename(tf.filename), nullptr});
1333 // It's not inherently executable.
1335 ASSERT_EQ(-1, execvpe(basename(tf.filename), eth.GetArgs(), eth.GetEnv()));
1336 ASSERT_EQ(EACCES, errno);
1338 // Make it executable (and keep it writable because we're going to rewrite it below).
1339 ASSERT_EQ(0, chmod(tf.filename, 0777));
1341 // TemporaryFile will have a writable fd, so we can test ETXTBSY while we're here...
1343 ASSERT_EQ(-1, execvpe(basename(tf.filename), eth.GetArgs(), eth.GetEnv()));
1344 ASSERT_EQ(ETXTBSY, errno);
1346 // 1. The simplest test: the kernel should handle this.
1347 ASSERT_EQ(0, close(tf.fd));
1348 eth.Run([&]() { execvpe(basename(tf.filename), eth.GetArgs(), eth.GetEnv()); }, 0, "script\n");
1350 // 2. Try again without a #!. We should have to handle this ourselves.
1351 ASSERT_TRUE(android::base::WriteStringToFile("echo script\n", tf.filename));
1352 eth.Run([&]() { execvpe(basename(tf.filename), eth.GetArgs(), eth.GetEnv()); }, 0, "script\n");
1354 // 3. Again without a #!, but also with a leading '/', since that's a special case in the
1356 eth.Run([&]() { execvpe(tf.filename, eth.GetArgs(), eth.GetEnv()); }, 0, "script\n");
1359 TEST(UNISTD_TEST, execvp_libcore_test_55017) {
1361 eth.SetArgs({"/system/bin/does-not-exist", nullptr});
1364 ASSERT_EQ(-1, execvp("/system/bin/does-not-exist", eth.GetArgs()));
1365 ASSERT_EQ(ENOENT, errno);
1368 TEST(UNISTD_TEST, exec_argv0_null) {
1369 // http://b/33276926
1370 char* args[] = {nullptr};
1371 char* envs[] = {nullptr};
1372 ASSERT_EXIT(execve("/system/bin/run-as", args, envs), testing::ExitedWithCode(1),
1373 "<unknown>: usage: run-as");