f0aeb09c95eba1b3ed8d6a0037bb3c509d54183b
[android-sdk/platform-bionic.git] / tests / unistd_test.cpp
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  */
17 #include <gtest/gtest.h>
18 #include "BionicDeathTest.h"
19 #include "ScopedSignalHandler.h"
20 #include "TemporaryFile.h"
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <limits.h>
25 #include <stdint.h>
26 #include <sys/param.h>
27 #include <sys/syscall.h>
28 #include <sys/types.h>
29 #include <sys/utsname.h>
30 #include <sys/wait.h>
31 #include <unistd.h>
33 static void* get_brk() {
34   return sbrk(0);
35 }
37 static void* page_align(uintptr_t addr) {
38   uintptr_t mask = sysconf(_SC_PAGE_SIZE) - 1;
39   return reinterpret_cast<void*>((addr + mask) & ~mask);
40 }
42 TEST(unistd, brk) {
43   void* initial_break = get_brk();
45   // The kernel aligns the break to a page.
46   void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 1);
47   ASSERT_EQ(0, brk(new_break));
48   ASSERT_GE(get_brk(), new_break);
50   new_break = page_align(reinterpret_cast<uintptr_t>(initial_break) + sysconf(_SC_PAGE_SIZE));
51   ASSERT_EQ(0, brk(new_break));
52   ASSERT_EQ(get_brk(), new_break);
53 }
55 TEST(unistd, brk_ENOMEM) {
56   ASSERT_EQ(-1, brk(reinterpret_cast<void*>(-1)));
57   ASSERT_EQ(ENOMEM, errno);
58 }
60 #if defined(__GLIBC__)
61 #define SBRK_MIN INTPTR_MIN
62 #define SBRK_MAX INTPTR_MAX
63 #else
64 #define SBRK_MIN PTRDIFF_MIN
65 #define SBRK_MAX PTRDIFF_MAX
66 #endif
68 TEST(unistd, sbrk_ENOMEM) {
69 #if defined(__BIONIC__) && !defined(__LP64__)
70   // There is no way to guarantee that all overflow conditions can be tested
71   // without manipulating the underlying values of the current break.
72   extern void* __bionic_brk;
74   class ScopedBrk {
75   public:
76     ScopedBrk() : saved_brk_(__bionic_brk) {}
77     virtual ~ScopedBrk() { __bionic_brk = saved_brk_; }
79   private:
80     void* saved_brk_;
81   };
83   ScopedBrk scope_brk;
85   // Set the current break to a point that will cause an overflow.
86   __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX) + 2);
88   // Can't increase by so much that we'd overflow.
89   ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MAX));
90   ASSERT_EQ(ENOMEM, errno);
92   // Set the current break to a point that will cause an overflow.
93   __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX));
95   ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MIN));
96   ASSERT_EQ(ENOMEM, errno);
98   __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX) - 1);
100   ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MIN + 1));
101   ASSERT_EQ(ENOMEM, errno);
102 #else
103   class ScopedBrk {
104   public:
105     ScopedBrk() : saved_brk_(get_brk()) {}
106     virtual ~ScopedBrk() { brk(saved_brk_); }
108   private:
109     void* saved_brk_;
110   };
112   ScopedBrk scope_brk;
114   uintptr_t cur_brk = reinterpret_cast<uintptr_t>(get_brk());
115   if (cur_brk < static_cast<uintptr_t>(-(SBRK_MIN+1))) {
116     // Do the overflow test for a max negative increment.
117     ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(SBRK_MIN));
118 #if defined(__BIONIC__)
119     // GLIBC does not set errno in overflow case.
120     ASSERT_EQ(ENOMEM, errno);
121 #endif
122   }
124   uintptr_t overflow_brk = static_cast<uintptr_t>(SBRK_MAX) + 2;
125   if (cur_brk < overflow_brk) {
126     // Try and move the value to PTRDIFF_MAX + 2.
127     cur_brk = reinterpret_cast<uintptr_t>(sbrk(overflow_brk));
128   }
129   if (cur_brk >= overflow_brk) {
130     ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(SBRK_MAX));
131 #if defined(__BIONIC__)
132     // GLIBC does not set errno in overflow case.
133     ASSERT_EQ(ENOMEM, errno);
134 #endif
135   }
136 #endif
139 TEST(unistd, truncate) {
140   TemporaryFile tf;
141   ASSERT_EQ(0, close(tf.fd));
142   ASSERT_EQ(0, truncate(tf.filename, 123));
144   struct stat sb;
145   ASSERT_EQ(0, stat(tf.filename, &sb));
146   ASSERT_EQ(123, sb.st_size);
149 TEST(unistd, truncate64) {
150   TemporaryFile tf;
151   ASSERT_EQ(0, close(tf.fd));
152   ASSERT_EQ(0, truncate64(tf.filename, 123));
154   struct stat sb;
155   ASSERT_EQ(0, stat(tf.filename, &sb));
156   ASSERT_EQ(123, sb.st_size);
159 TEST(unistd, ftruncate) {
160   TemporaryFile tf;
161   ASSERT_EQ(0, ftruncate(tf.fd, 123));
162   ASSERT_EQ(0, close(tf.fd));
164   struct stat sb;
165   ASSERT_EQ(0, stat(tf.filename, &sb));
166   ASSERT_EQ(123, sb.st_size);
169 TEST(unistd, ftruncate64) {
170   TemporaryFile tf;
171   ASSERT_EQ(0, ftruncate64(tf.fd, 123));
172   ASSERT_EQ(0, close(tf.fd));
174   struct stat sb;
175   ASSERT_EQ(0, stat(tf.filename, &sb));
176   ASSERT_EQ(123, sb.st_size);
179 static bool g_pause_test_flag = false;
180 static void PauseTestSignalHandler(int) {
181   g_pause_test_flag = true;
184 TEST(unistd, pause) {
185   ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler);
187   alarm(1);
188   ASSERT_FALSE(g_pause_test_flag);
189   ASSERT_EQ(-1, pause());
190   ASSERT_TRUE(g_pause_test_flag);
193 TEST(unistd, read) {
194   int fd = open("/proc/version", O_RDONLY);
195   ASSERT_TRUE(fd != -1);
197   char buf[5];
198   ASSERT_EQ(5, read(fd, buf, 5));
199   ASSERT_EQ(buf[0], 'L');
200   ASSERT_EQ(buf[1], 'i');
201   ASSERT_EQ(buf[2], 'n');
202   ASSERT_EQ(buf[3], 'u');
203   ASSERT_EQ(buf[4], 'x');
204   close(fd);
207 TEST(unistd, read_EBADF) {
208   // read returns ssize_t which is 64-bits on LP64, so it's worth explicitly checking that
209   // our syscall stubs correctly return a 64-bit -1.
210   char buf[1];
211   ASSERT_EQ(-1, read(-1, buf, sizeof(buf)));
212   ASSERT_EQ(EBADF, errno);
215 TEST(unistd, syscall_long) {
216   // Check that syscall(3) correctly returns long results.
217   // https://code.google.com/p/android/issues/detail?id=73952
218   // We assume that the break is > 4GiB, but this is potentially flaky.
219   uintptr_t p = reinterpret_cast<uintptr_t>(sbrk(0));
220   ASSERT_EQ(p, static_cast<uintptr_t>(syscall(__NR_brk, 0)));
223 TEST(unistd, alarm) {
224   ASSERT_EQ(0U, alarm(0));
227 TEST(unistd, _exit) {
228   int pid = fork();
229   ASSERT_NE(-1, pid) << strerror(errno);
231   if (pid == 0) {
232     _exit(99);
233   }
235   int status;
236   ASSERT_EQ(pid, waitpid(pid, &status, 0));
237   ASSERT_TRUE(WIFEXITED(status));
238   ASSERT_EQ(99, WEXITSTATUS(status));
241 TEST(unistd, getenv_unsetenv) {
242   ASSERT_EQ(0, setenv("test-variable", "hello", 1));
243   ASSERT_STREQ("hello", getenv("test-variable"));
244   ASSERT_EQ(0, unsetenv("test-variable"));
245   ASSERT_TRUE(getenv("test-variable") == NULL);
248 TEST(unistd, unsetenv_EINVAL) {
249   EXPECT_EQ(-1, unsetenv(""));
250   EXPECT_EQ(EINVAL, errno);
251   EXPECT_EQ(-1, unsetenv("a=b"));
252   EXPECT_EQ(EINVAL, errno);
255 TEST(unistd, setenv_EINVAL) {
256   EXPECT_EQ(-1, setenv(NULL, "value", 0));
257   EXPECT_EQ(EINVAL, errno);
258   EXPECT_EQ(-1, setenv(NULL, "value", 1));
259   EXPECT_EQ(EINVAL, errno);
260   EXPECT_EQ(-1, setenv("", "value", 0));
261   EXPECT_EQ(EINVAL, errno);
262   EXPECT_EQ(-1, setenv("", "value", 1));
263   EXPECT_EQ(EINVAL, errno);
264   EXPECT_EQ(-1, setenv("a=b", "value", 0));
265   EXPECT_EQ(EINVAL, errno);
266   EXPECT_EQ(-1, setenv("a=b", "value", 1));
267   EXPECT_EQ(EINVAL, errno);
270 TEST(unistd, setenv) {
271   ASSERT_EQ(0, unsetenv("test-variable"));
273   char a[] = "a";
274   char b[] = "b";
275   char c[] = "c";
277   // New value.
278   EXPECT_EQ(0, setenv("test-variable", a, 0));
279   EXPECT_STREQ(a, getenv("test-variable"));
281   // Existing value, no overwrite.
282   EXPECT_EQ(0, setenv("test-variable", b, 0));
283   EXPECT_STREQ(a, getenv("test-variable"));
285   // Existing value, overwrite.
286   EXPECT_EQ(0, setenv("test-variable", c, 1));
287   EXPECT_STREQ(c, getenv("test-variable"));
288   // But the arrays backing the values are unchanged.
289   EXPECT_EQ('a', a[0]);
290   EXPECT_EQ('b', b[0]);
291   EXPECT_EQ('c', c[0]);
293   ASSERT_EQ(0, unsetenv("test-variable"));
296 TEST(unistd, putenv) {
297   ASSERT_EQ(0, unsetenv("a"));
299   char* s1 = strdup("a=b");
300   ASSERT_EQ(0, putenv(s1));
302   ASSERT_STREQ("b", getenv("a"));
303   s1[2] = 'c';
304   ASSERT_STREQ("c", getenv("a"));
306   char* s2 = strdup("a=b");
307   ASSERT_EQ(0, putenv(s2));
309   ASSERT_STREQ("b", getenv("a"));
310   ASSERT_EQ('c', s1[2]);
312   ASSERT_EQ(0, unsetenv("a"));
313   free(s1);
314   free(s2);
317 TEST(unistd, clearenv) {
318   extern char** environ;
320   // Guarantee that environ is not initially empty...
321   ASSERT_EQ(0, setenv("test-variable", "a", 1));
323   // Stash a copy.
324   std::vector<char*> old_environ;
325   for (size_t i = 0; environ[i] != NULL; ++i) {
326     old_environ.push_back(strdup(environ[i]));
327   }
329   ASSERT_EQ(0, clearenv());
331   EXPECT_TRUE(environ == NULL || environ[0] == NULL);
332   EXPECT_EQ(NULL, getenv("test-variable"));
333   EXPECT_EQ(0, setenv("test-variable", "post-clear", 1));
334   EXPECT_STREQ("post-clear", getenv("test-variable"));
336   // Put the old environment back.
337   for (size_t i = 0; i < old_environ.size(); ++i) {
338     EXPECT_EQ(0, putenv(old_environ[i]));
339   }
341   // Check it wasn't overwritten.
342   EXPECT_STREQ("a", getenv("test-variable"));
344   EXPECT_EQ(0, unsetenv("test-variable"));
347 static void TestFsyncFunction(int (*fn)(int)) {
348   int fd;
350   // Can't sync an invalid fd.
351   errno = 0;
352   EXPECT_EQ(-1, fn(-1));
353   EXPECT_EQ(EBADF, errno);
355   // It doesn't matter whether you've opened a file for write or not.
356   TemporaryFile tf;
357   ASSERT_NE(-1, tf.fd);
359   EXPECT_EQ(0, fn(tf.fd));
361   ASSERT_NE(-1, fd = open(tf.filename, O_RDONLY));
362   EXPECT_EQ(0, fn(fd));
363   close(fd);
365   ASSERT_NE(-1, fd = open(tf.filename, O_RDWR));
366   EXPECT_EQ(0, fn(fd));
367   close(fd);
369   // The fd can even be a directory.
370   ASSERT_NE(-1, fd = open("/", O_RDONLY));
371   EXPECT_EQ(0, fn(fd));
372   close(fd);
374   // But some file systems may choose to be fussy...
375   errno = 0;
376   ASSERT_NE(-1, fd = open("/proc/version", O_RDONLY));
377   EXPECT_EQ(-1, fn(fd));
378   EXPECT_EQ(EINVAL, errno);
379   close(fd);
382 TEST(unistd, fdatasync) {
383   TestFsyncFunction(fdatasync);
386 TEST(unistd, fsync) {
387   TestFsyncFunction(fsync);
390 static void AssertGetPidCorrect() {
391   // The loop is just to make manual testing/debugging with strace easier.
392   pid_t getpid_syscall_result = syscall(__NR_getpid);
393   for (size_t i = 0; i < 128; ++i) {
394     ASSERT_EQ(getpid_syscall_result, getpid());
395   }
398 TEST(unistd, getpid_caching_and_fork) {
399   pid_t parent_pid = getpid();
400   ASSERT_EQ(syscall(__NR_getpid), parent_pid);
402   pid_t fork_result = fork();
403   ASSERT_NE(fork_result, -1);
404   if (fork_result == 0) {
405     // We're the child.
406     AssertGetPidCorrect();
407     ASSERT_EQ(parent_pid, getppid());
408     _exit(123);
409   } else {
410     // We're the parent.
411     ASSERT_EQ(parent_pid, getpid());
413     int status;
414     ASSERT_EQ(fork_result, waitpid(fork_result, &status, 0));
415     ASSERT_TRUE(WIFEXITED(status));
416     ASSERT_EQ(123, WEXITSTATUS(status));
417   }
420 static int GetPidCachingCloneStartRoutine(void*) {
421   AssertGetPidCorrect();
422   return 123;
425 TEST(unistd, getpid_caching_and_clone) {
426   pid_t parent_pid = getpid();
427   ASSERT_EQ(syscall(__NR_getpid), parent_pid);
429   void* child_stack[1024];
430   int clone_result = clone(GetPidCachingCloneStartRoutine, &child_stack[1024], CLONE_NEWNS | SIGCHLD, NULL);
431   if (clone_result == -1 && errno == EPERM && getuid() != 0) {
432     GTEST_LOG_(INFO) << "This test only works if you have permission to CLONE_NEWNS; try running as root.\n";
433     return;
434   }
435   ASSERT_NE(clone_result, -1);
437   ASSERT_EQ(parent_pid, getpid());
439   int status;
440   ASSERT_EQ(clone_result, waitpid(clone_result, &status, 0));
441   ASSERT_TRUE(WIFEXITED(status));
442   ASSERT_EQ(123, WEXITSTATUS(status));
445 static void* GetPidCachingPthreadStartRoutine(void*) {
446   AssertGetPidCorrect();
447   return NULL;
450 TEST(unistd, getpid_caching_and_pthread_create) {
451   pid_t parent_pid = getpid();
453   pthread_t t;
454   ASSERT_EQ(0, pthread_create(&t, NULL, GetPidCachingPthreadStartRoutine, NULL));
456   ASSERT_EQ(parent_pid, getpid());
458   void* result;
459   ASSERT_EQ(0, pthread_join(t, &result));
460   ASSERT_EQ(NULL, result);
463 class unistd_DeathTest : public BionicDeathTest {};
465 TEST_F(unistd_DeathTest, abort) {
466   ASSERT_EXIT(abort(), testing::KilledBySignal(SIGABRT), "");
469 TEST(unistd, sethostname) {
470   // The permissions check happens before the argument check, so this will
471   // fail for a different reason if you're running as root than if you're
472   // not, but it'll fail either way. Checking that we have the symbol is about
473   // all we can do for sethostname(2).
474   ASSERT_EQ(-1, sethostname("", -1));
477 TEST(unistd, gethostname) {
478   char hostname[HOST_NAME_MAX + 1];
479   memset(hostname, 0, sizeof(hostname));
481   // Can we get the hostname with a big buffer?
482   ASSERT_EQ(0, gethostname(hostname, HOST_NAME_MAX));
484   // Can we get the hostname with a right-sized buffer?
485   errno = 0;
486   ASSERT_EQ(0, gethostname(hostname, strlen(hostname) + 1));
488   // Does uname(2) agree?
489   utsname buf;
490   ASSERT_EQ(0, uname(&buf));
491   ASSERT_EQ(0, strncmp(hostname, buf.nodename, SYS_NMLN));
492   ASSERT_GT(strlen(hostname), 0U);
494   // Do we correctly detect truncation?
495   errno = 0;
496   ASSERT_EQ(-1, gethostname(hostname, strlen(hostname)));
497   ASSERT_EQ(ENAMETOOLONG, errno);
500 TEST(unistd, pathconf_fpathconf) {
501   TemporaryFile tf;
502   long rc = 0L;
503   // As a file system's block size is always power of 2, the configure values
504   // for ALLOC and XFER should be power of 2 as well.
505   rc = pathconf(tf.filename, _PC_ALLOC_SIZE_MIN);
506   ASSERT_TRUE(rc > 0 && powerof2(rc));
507   rc = pathconf(tf.filename, _PC_REC_MIN_XFER_SIZE);
508   ASSERT_TRUE(rc > 0 && powerof2(rc));
509   rc = pathconf(tf.filename, _PC_REC_XFER_ALIGN);
510   ASSERT_TRUE(rc > 0 && powerof2(rc));
512   rc = fpathconf(tf.fd, _PC_ALLOC_SIZE_MIN);
513   ASSERT_TRUE(rc > 0 && powerof2(rc));
514   rc = fpathconf(tf.fd, _PC_REC_MIN_XFER_SIZE);
515   ASSERT_TRUE(rc > 0 && powerof2(rc));
516   rc = fpathconf(tf.fd, _PC_REC_XFER_ALIGN);
517   ASSERT_TRUE(rc > 0 && powerof2(rc));
521 TEST(unistd, _POSIX_macros_smoke) {
522   // Make a tight verification of _POSIX_* / _POSIX2_* / _XOPEN_* macros, to prevent change by mistake.
523   // Verify according to POSIX.1-2008.
524   EXPECT_EQ(200809L, _POSIX_VERSION);
526   EXPECT_GT(_POSIX_AIO_LISTIO_MAX, 0);
527   EXPECT_GT(_POSIX_AIO_MAX, 0);
528   EXPECT_GT(_POSIX_ARG_MAX, 0);
529   EXPECT_GT(_POSIX_CHILD_MAX, 0);
530   EXPECT_NE(_POSIX_CHOWN_RESTRICTED, -1);
531   EXPECT_EQ(_POSIX_VERSION, _POSIX_CLOCK_SELECTION);
532   EXPECT_GT(_POSIX_DELAYTIMER_MAX, 0);
533   EXPECT_EQ(_POSIX_VERSION, _POSIX_FSYNC);
534   EXPECT_GT(_POSIX_HOST_NAME_MAX, 0);
535   EXPECT_EQ(_POSIX_VERSION, _POSIX_IPV6);
536   EXPECT_GT(_POSIX_JOB_CONTROL, 0);
537   EXPECT_GT(_POSIX_LINK_MAX, 0);
538   EXPECT_GT(_POSIX_LOGIN_NAME_MAX, 0);
539   EXPECT_EQ(_POSIX_VERSION, _POSIX_MAPPED_FILES);
540   EXPECT_GT(_POSIX_MAX_CANON, 0);
541   EXPECT_GT(_POSIX_MAX_INPUT, 0);
542   EXPECT_EQ(_POSIX_VERSION, _POSIX_MEMLOCK);
543   EXPECT_EQ(_POSIX_VERSION, _POSIX_MEMLOCK_RANGE);
544   EXPECT_EQ(_POSIX_VERSION, _POSIX_MEMORY_PROTECTION);
545   EXPECT_EQ(0, _POSIX_MONOTONIC_CLOCK);
546   EXPECT_GT(_POSIX_MQ_OPEN_MAX, 0);
547   EXPECT_GT(_POSIX_MQ_PRIO_MAX, 0);
548   EXPECT_GT(_POSIX_NAME_MAX, 0);
549   EXPECT_GT(_POSIX_NGROUPS_MAX, 0);
550   EXPECT_GT(_POSIX_NO_TRUNC, 0);
551   EXPECT_GT(_POSIX_OPEN_MAX, 0);
552   EXPECT_GT(_POSIX_PATH_MAX, 0);
553   EXPECT_GT(_POSIX_PIPE_BUF, 0);
554   EXPECT_EQ(_POSIX_VERSION, _POSIX_PRIORITY_SCHEDULING);
555   EXPECT_EQ(_POSIX_VERSION, _POSIX_RAW_SOCKETS);
556   EXPECT_EQ(_POSIX_VERSION, _POSIX_READER_WRITER_LOCKS);
557   EXPECT_GT(_POSIX_REGEXP, 0);
558   EXPECT_GT(_POSIX_RE_DUP_MAX, 0);
559   EXPECT_GT(_POSIX_SAVED_IDS, 0);
560   EXPECT_EQ(_POSIX_VERSION, _POSIX_SEMAPHORES);
561   EXPECT_GT(_POSIX_SEM_NSEMS_MAX, 0);
562   EXPECT_GT(_POSIX_SEM_VALUE_MAX, 0);
563   EXPECT_GT(_POSIX_SHELL, 0);
564   EXPECT_GT(_POSIX_SIGQUEUE_MAX, 0);
565   EXPECT_EQ(-1, _POSIX_SPORADIC_SERVER);
566   EXPECT_GT(_POSIX_SSIZE_MAX, 0);
567   EXPECT_GT(_POSIX_STREAM_MAX, 0);
568   EXPECT_GT(_POSIX_SYMLINK_MAX, 0);
569   EXPECT_GT(_POSIX_SYMLOOP_MAX, 0);
570   EXPECT_EQ(_POSIX_VERSION, _POSIX_SYNCHRONIZED_IO);
571   EXPECT_EQ(_POSIX_VERSION, _POSIX_THREADS);
572   EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_ATTR_STACKADDR);
573   EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_ATTR_STACKSIZE);
574   EXPECT_TRUE(_POSIX_VERSION ==  _POSIX_THREAD_CPUTIME || 0 == _POSIX_THREAD_CPUTIME);
575   EXPECT_GT(_POSIX_THREAD_DESTRUCTOR_ITERATIONS, 0);
576   EXPECT_GT(_POSIX_THREAD_KEYS_MAX, 0);
577   EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_PRIORITY_SCHEDULING);
578   EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_PRIO_INHERIT);
579   EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_PRIO_PROTECT);
580   EXPECT_EQ(-1, _POSIX_THREAD_ROBUST_PRIO_PROTECT);
581   EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_SAFE_FUNCTIONS);
582   EXPECT_EQ(-1, _POSIX_THREAD_SPORADIC_SERVER);
583   EXPECT_GT(_POSIX_THREAD_THREADS_MAX, 0);
584   EXPECT_EQ(_POSIX_VERSION, _POSIX_TIMEOUTS);
585   EXPECT_EQ(_POSIX_VERSION, _POSIX_TIMERS);
586   EXPECT_GT(_POSIX_TIMER_MAX, 0);
587   EXPECT_EQ(-1, _POSIX_TRACE);
588   EXPECT_EQ(-1, _POSIX_TRACE_EVENT_FILTER);
589   EXPECT_EQ(-1, _POSIX_TRACE_INHERIT);
590   EXPECT_EQ(-1, _POSIX_TRACE_LOG);
591   EXPECT_GT(_POSIX_TTY_NAME_MAX, 0);
592   EXPECT_EQ(-1, _POSIX_TYPED_MEMORY_OBJECTS);
593   EXPECT_GT(_POSIX_TZNAME_MAX, 0);
594   EXPECT_NE(-1, _POSIX_VDISABLE);
596   EXPECT_GT(_POSIX2_BC_BASE_MAX, 0);
597   EXPECT_GT(_POSIX2_BC_DIM_MAX, 0);
598   EXPECT_GT(_POSIX2_BC_SCALE_MAX, 0);
599   EXPECT_GT(_POSIX2_BC_STRING_MAX, 0);
600   EXPECT_GT(_POSIX2_CHARCLASS_NAME_MAX, 0);
601   EXPECT_GT(_POSIX2_COLL_WEIGHTS_MAX, 0);
602   EXPECT_EQ(_POSIX_VERSION, _POSIX2_C_BIND);
603   EXPECT_GT(_POSIX2_EXPR_NEST_MAX, 0);
604   EXPECT_GT(_POSIX2_LINE_MAX, 0);
605   EXPECT_GT(_POSIX2_RE_DUP_MAX, 0);
607   EXPECT_EQ(700, _XOPEN_VERSION);
608   EXPECT_GT(_XOPEN_IOV_MAX, 0);
609   EXPECT_GT(_XOPEN_UNIX, 0);
611 #if defined(__BIONIC__)
612   // These tests only pass on bionic, as bionic and glibc has different support on these macros.
613   // Macros like _POSIX_ADVISORY_INFO are not supported on bionic yet.
614   EXPECT_EQ(-1, _POSIX_ADVISORY_INFO);
615   EXPECT_EQ(-1, _POSIX_ASYNCHRONOUS_IO);
616   EXPECT_EQ(-1, _POSIX_BARRIERS);
617   EXPECT_EQ(-1, _POSIX_CPUTIME);
618   EXPECT_EQ(-1, _POSIX_MESSAGE_PASSING);
619   EXPECT_EQ(-1, _POSIX_PRIORITIZED_IO);
620   EXPECT_EQ(-1, _POSIX_REALTIME_SIGNALS);
621   EXPECT_EQ(-1, _POSIX_SHARED_MEMORY_OBJECTS);
622   EXPECT_EQ(-1, _POSIX_SPAWN);
623   EXPECT_EQ(-1, _POSIX_SPIN_LOCKS);
624   EXPECT_EQ(-1, _POSIX_THREAD_PROCESS_SHARED);
625   EXPECT_EQ(-1, _POSIX_THREAD_ROBUST_PRIO_INHERIT);
627   EXPECT_EQ(-1, _POSIX2_VERSION);
628   EXPECT_EQ(-1, _POSIX2_CHAR_TERM);
629   EXPECT_EQ(-1, _POSIX2_C_DEV);
630   EXPECT_EQ(-1, _POSIX2_LOCALEDEF);
631   EXPECT_EQ(-1, _POSIX2_SW_DEV);
632   EXPECT_EQ(-1, _POSIX2_UPE);
634   EXPECT_EQ(-1, _XOPEN_ENH_I18N);
635   EXPECT_EQ(-1, _XOPEN_CRYPT);
636   EXPECT_EQ(-1, _XOPEN_LEGACY);
637   EXPECT_EQ(-1, _XOPEN_REALTIME);
638   EXPECT_EQ(-1, _XOPEN_REALTIME_THREADS);
639   EXPECT_EQ(-1, _XOPEN_SHM);
641 #endif // defined(__BIONIC__)
644 #define VERIFY_SYSCONF_NOT_SUPPORT(name) VerifySysconf(name, #name, [](long v){return v == -1;})
646 // sysconf() means unlimited when it returns -1 with errno unchanged.
647 #define VERIFY_SYSCONF_POSITIVE(name) \
648   VerifySysconf(name, #name, [](long v){return (v > 0 || v == -1);})
650 #define VERIFY_SYSCONF_POSIX_VERSION(name) \
651   VerifySysconf(name, #name, [](long v){return v == _POSIX_VERSION;})
653 static void VerifySysconf(int option, const char *option_name, bool (*verify)(long)) {
654   errno = 0;
655   long ret = sysconf(option);
656   EXPECT_TRUE(0 == errno && verify(ret)) << "name = " << option_name << ", ret = "
657       << ret <<", Error Message: " << strerror(errno);
660 TEST(unistd, sysconf) {
661   VERIFY_SYSCONF_POSITIVE(_SC_ARG_MAX);
662   VERIFY_SYSCONF_POSITIVE(_SC_BC_BASE_MAX);
663   VERIFY_SYSCONF_POSITIVE(_SC_BC_DIM_MAX);
664   VERIFY_SYSCONF_POSITIVE(_SC_BC_SCALE_MAX);
665   VERIFY_SYSCONF_POSITIVE(_SC_CHILD_MAX);
666   VERIFY_SYSCONF_POSITIVE(_SC_CLK_TCK);
667   VERIFY_SYSCONF_POSITIVE(_SC_COLL_WEIGHTS_MAX);
668   VERIFY_SYSCONF_POSITIVE(_SC_EXPR_NEST_MAX);
669   VERIFY_SYSCONF_POSITIVE(_SC_LINE_MAX);
670   VERIFY_SYSCONF_POSITIVE(_SC_NGROUPS_MAX);
671   VERIFY_SYSCONF_POSITIVE(_SC_OPEN_MAX);
672   VERIFY_SYSCONF_POSITIVE(_SC_PASS_MAX);
673   VERIFY_SYSCONF_POSIX_VERSION(_SC_2_C_BIND);
674   VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_FORT_DEV);
675   VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_FORT_RUN);
676   VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_UPE);
677   VERIFY_SYSCONF_POSITIVE(_SC_JOB_CONTROL);
678   VERIFY_SYSCONF_POSITIVE(_SC_SAVED_IDS);
679   VERIFY_SYSCONF_POSIX_VERSION(_SC_VERSION);
680   VERIFY_SYSCONF_POSITIVE(_SC_RE_DUP_MAX);
681   VERIFY_SYSCONF_POSITIVE(_SC_STREAM_MAX);
682   VERIFY_SYSCONF_POSITIVE(_SC_TZNAME_MAX);
683   VerifySysconf(_SC_XOPEN_VERSION, "_SC_XOPEN_VERSION", [](long v){return v == _XOPEN_VERSION;});
684   VERIFY_SYSCONF_POSITIVE(_SC_ATEXIT_MAX);
685   VERIFY_SYSCONF_POSITIVE(_SC_IOV_MAX);
686   VERIFY_SYSCONF_POSITIVE(_SC_PAGESIZE);
687   VERIFY_SYSCONF_POSITIVE(_SC_PAGE_SIZE);
688   VERIFY_SYSCONF_POSITIVE(_SC_XOPEN_UNIX);
689   VERIFY_SYSCONF_POSITIVE(_SC_AIO_LISTIO_MAX);
690   VERIFY_SYSCONF_POSITIVE(_SC_AIO_MAX);
691   VerifySysconf(_SC_AIO_PRIO_DELTA_MAX, "_SC_AIO_PRIO_DELTA_MAX", [](long v){return v >= 0;});
692   VERIFY_SYSCONF_POSITIVE(_SC_DELAYTIMER_MAX);
693   VERIFY_SYSCONF_POSITIVE(_SC_MQ_OPEN_MAX);
694   VERIFY_SYSCONF_POSITIVE(_SC_MQ_PRIO_MAX);
695   VERIFY_SYSCONF_POSITIVE(_SC_RTSIG_MAX);
696   VERIFY_SYSCONF_POSITIVE(_SC_SEM_NSEMS_MAX);
697   VERIFY_SYSCONF_POSITIVE(_SC_SEM_VALUE_MAX);
698   VERIFY_SYSCONF_POSITIVE(_SC_TIMER_MAX);
699   VERIFY_SYSCONF_POSIX_VERSION(_SC_FSYNC);
700   VERIFY_SYSCONF_POSIX_VERSION(_SC_MAPPED_FILES);
701   VERIFY_SYSCONF_POSIX_VERSION(_SC_MEMLOCK);
702   VERIFY_SYSCONF_POSIX_VERSION(_SC_MEMLOCK_RANGE);
703   VERIFY_SYSCONF_POSIX_VERSION(_SC_MEMORY_PROTECTION);
704   VERIFY_SYSCONF_POSIX_VERSION(_SC_PRIORITY_SCHEDULING);
705   VERIFY_SYSCONF_POSIX_VERSION(_SC_SEMAPHORES);
706   VERIFY_SYSCONF_POSIX_VERSION(_SC_SYNCHRONIZED_IO);
707   VERIFY_SYSCONF_POSIX_VERSION(_SC_TIMERS);
708   VERIFY_SYSCONF_POSITIVE(_SC_GETGR_R_SIZE_MAX);
709   VERIFY_SYSCONF_POSITIVE(_SC_GETPW_R_SIZE_MAX);
710   VERIFY_SYSCONF_POSITIVE(_SC_LOGIN_NAME_MAX);
711   VERIFY_SYSCONF_POSITIVE(_SC_THREAD_DESTRUCTOR_ITERATIONS);
712   VERIFY_SYSCONF_POSITIVE(_SC_THREAD_KEYS_MAX);
713   VERIFY_SYSCONF_POSITIVE(_SC_THREAD_STACK_MIN);
714   VERIFY_SYSCONF_POSITIVE(_SC_THREAD_THREADS_MAX);
715   VERIFY_SYSCONF_POSITIVE(_SC_TTY_NAME_MAX);
716   VERIFY_SYSCONF_POSIX_VERSION(_SC_THREADS);
717   VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_ATTR_STACKADDR);
718   VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_ATTR_STACKSIZE);
719   VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_PRIORITY_SCHEDULING);
720   VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_PRIO_INHERIT);
721   VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_PRIO_PROTECT);
722   VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_SAFE_FUNCTIONS);
723   VERIFY_SYSCONF_POSITIVE(_SC_NPROCESSORS_CONF);
724   VERIFY_SYSCONF_POSITIVE(_SC_NPROCESSORS_ONLN);
725   VERIFY_SYSCONF_POSITIVE(_SC_PHYS_PAGES);
726   VERIFY_SYSCONF_POSITIVE(_SC_AVPHYS_PAGES);
727   VERIFY_SYSCONF_POSIX_VERSION(_SC_MONOTONIC_CLOCK);
728   VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_PBS);
729   VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_PBS_ACCOUNTING);
730   VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_PBS_CHECKPOINT);
731   VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_PBS_LOCATE);
732   VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_PBS_MESSAGE);
733   VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_PBS_TRACK);
734   VERIFY_SYSCONF_POSIX_VERSION(_SC_CLOCK_SELECTION);
735   VERIFY_SYSCONF_POSITIVE(_SC_HOST_NAME_MAX);
736   VERIFY_SYSCONF_POSIX_VERSION(_SC_IPV6);
737   VERIFY_SYSCONF_POSIX_VERSION(_SC_RAW_SOCKETS);
738   VERIFY_SYSCONF_POSIX_VERSION(_SC_READER_WRITER_LOCKS);
739   VERIFY_SYSCONF_POSITIVE(_SC_REGEXP);
740   VERIFY_SYSCONF_POSITIVE(_SC_SHELL);
741   VERIFY_SYSCONF_NOT_SUPPORT(_SC_SPORADIC_SERVER);
742   VERIFY_SYSCONF_POSITIVE(_SC_SYMLOOP_MAX);
743   VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_CPUTIME);
744   VERIFY_SYSCONF_NOT_SUPPORT(_SC_THREAD_SPORADIC_SERVER);
745   VERIFY_SYSCONF_POSIX_VERSION(_SC_TIMEOUTS);
746   VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE);
747   VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE_EVENT_FILTER);
748   VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE_EVENT_NAME_MAX);
749   VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE_INHERIT);
750   VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE_LOG);
751   VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE_NAME_MAX);
752   VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE_SYS_MAX);
753   VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE_USER_EVENT_MAX);
754   VERIFY_SYSCONF_NOT_SUPPORT(_SC_TYPED_MEMORY_OBJECTS);
755   VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_STREAMS);
757 #if defined(__LP64__)
758   VERIFY_SYSCONF_NOT_SUPPORT(_SC_V7_ILP32_OFF32);
759   VERIFY_SYSCONF_NOT_SUPPORT(_SC_V7_ILP32_OFFBIG);
760   VERIFY_SYSCONF_POSITIVE(_SC_V7_LP64_OFF64);
761   VERIFY_SYSCONF_POSITIVE(_SC_V7_LPBIG_OFFBIG);
762 #else
763   VERIFY_SYSCONF_POSITIVE(_SC_V7_ILP32_OFF32);
764 #if defined(__BIONIC__)
765   // bionic does not support 64 bits off_t type on 32bit machine.
766   VERIFY_SYSCONF_NOT_SUPPORT(_SC_V7_ILP32_OFFBIG);
767 #endif
768   VERIFY_SYSCONF_NOT_SUPPORT(_SC_V7_LP64_OFF64);
769   VERIFY_SYSCONF_NOT_SUPPORT(_SC_V7_LPBIG_OFFBIG);
770 #endif
772 #if defined(__BIONIC__)
773   // Tests can only run on bionic, as bionic and glibc have different support for these options.
774   // Below options are not supported on bionic yet.
775   VERIFY_SYSCONF_NOT_SUPPORT(_SC_ADVISORY_INFO);
776   VERIFY_SYSCONF_NOT_SUPPORT(_SC_ASYNCHRONOUS_IO);
777   VERIFY_SYSCONF_NOT_SUPPORT(_SC_BARRIERS);
778   VERIFY_SYSCONF_NOT_SUPPORT(_SC_CPUTIME);
779   VERIFY_SYSCONF_NOT_SUPPORT(_SC_MESSAGE_PASSING);
780   VERIFY_SYSCONF_NOT_SUPPORT(_SC_PRIORITIZED_IO);
781   VERIFY_SYSCONF_NOT_SUPPORT(_SC_REALTIME_SIGNALS);
782   VERIFY_SYSCONF_NOT_SUPPORT(_SC_SHARED_MEMORY_OBJECTS);
783   VERIFY_SYSCONF_NOT_SUPPORT(_SC_SPAWN);
784   VERIFY_SYSCONF_NOT_SUPPORT(_SC_SPIN_LOCKS);
785   VERIFY_SYSCONF_NOT_SUPPORT(_SC_THREAD_PROCESS_SHARED);
786   VERIFY_SYSCONF_NOT_SUPPORT(_SC_THREAD_ROBUST_PRIO_INHERIT);
787   VERIFY_SYSCONF_NOT_SUPPORT(_SC_THREAD_ROBUST_PRIO_PROTECT);
789   VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_C_DEV);
790   VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_CHAR_TERM);
791   VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_LOCALEDEF);
792   VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_SW_DEV);
793   VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_VERSION);
795   VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_CRYPT);
796   VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_ENH_I18N);
797   VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_LEGACY);
798   VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_REALTIME);
799   VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_REALTIME_THREADS);
800   VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_SHM);
801   VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_UUCP);
802 #endif // defined(__BIONIC__)