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>
18#include "BionicDeathTest.h"
19#include "ScopedSignalHandler.h"
20#include "TemporaryFile.h"
21
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>
32
33static void* get_brk() {
34  return sbrk(0);
35}
36
37static void* page_align(uintptr_t addr) {
38  uintptr_t mask = sysconf(_SC_PAGE_SIZE) - 1;
39  return reinterpret_cast<void*>((addr + mask) & ~mask);
40}
41
42TEST(unistd, brk) {
43  void* initial_break = get_brk();
44
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);
49
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}
54
55TEST(unistd, brk_ENOMEM) {
56  ASSERT_EQ(-1, brk(reinterpret_cast<void*>(-1)));
57  ASSERT_EQ(ENOMEM, errno);
58}
59
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
67
68TEST(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;
73
74  class ScopedBrk {
75  public:
76    ScopedBrk() : saved_brk_(__bionic_brk) {}
77    virtual ~ScopedBrk() { __bionic_brk = saved_brk_; }
78
79  private:
80    void* saved_brk_;
81  };
82
83  ScopedBrk scope_brk;
84
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);
87
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);
91
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));
94
95  ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MIN));
96  ASSERT_EQ(ENOMEM, errno);
97
98  __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX) - 1);
99
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_); }
107
108  private:
109    void* saved_brk_;
110  };
111
112  ScopedBrk scope_brk;
113
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  }
123
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
137}
138
139TEST(unistd, truncate) {
140  TemporaryFile tf;
141  ASSERT_EQ(0, close(tf.fd));
142  ASSERT_EQ(0, truncate(tf.filename, 123));
143
144  struct stat sb;
145  ASSERT_EQ(0, stat(tf.filename, &sb));
146  ASSERT_EQ(123, sb.st_size);
147}
148
149TEST(unistd, truncate64) {
150  TemporaryFile tf;
151  ASSERT_EQ(0, close(tf.fd));
152  ASSERT_EQ(0, truncate64(tf.filename, 123));
153
154  struct stat sb;
155  ASSERT_EQ(0, stat(tf.filename, &sb));
156  ASSERT_EQ(123, sb.st_size);
157}
158
159TEST(unistd, ftruncate) {
160  TemporaryFile tf;
161  ASSERT_EQ(0, ftruncate(tf.fd, 123));
162  ASSERT_EQ(0, close(tf.fd));
163
164  struct stat sb;
165  ASSERT_EQ(0, stat(tf.filename, &sb));
166  ASSERT_EQ(123, sb.st_size);
167}
168
169TEST(unistd, ftruncate64) {
170  TemporaryFile tf;
171  ASSERT_EQ(0, ftruncate64(tf.fd, 123));
172  ASSERT_EQ(0, close(tf.fd));
173
174  struct stat sb;
175  ASSERT_EQ(0, stat(tf.filename, &sb));
176  ASSERT_EQ(123, sb.st_size);
177}
178
179TEST(unistd, ftruncate_negative) {
180  TemporaryFile tf;
181  errno = 0;
182  ASSERT_EQ(-1, ftruncate(tf.fd, -123));
183  ASSERT_EQ(EINVAL, errno);
184}
185
186static bool g_pause_test_flag = false;
187static void PauseTestSignalHandler(int) {
188  g_pause_test_flag = true;
189}
190
191TEST(unistd, pause) {
192  ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler);
193
194  alarm(1);
195  ASSERT_FALSE(g_pause_test_flag);
196  ASSERT_EQ(-1, pause());
197  ASSERT_TRUE(g_pause_test_flag);
198}
199
200TEST(unistd, read) {
201  int fd = open("/proc/version", O_RDONLY);
202  ASSERT_TRUE(fd != -1);
203
204  char buf[5];
205  ASSERT_EQ(5, read(fd, buf, 5));
206  ASSERT_EQ(buf[0], 'L');
207  ASSERT_EQ(buf[1], 'i');
208  ASSERT_EQ(buf[2], 'n');
209  ASSERT_EQ(buf[3], 'u');
210  ASSERT_EQ(buf[4], 'x');
211  close(fd);
212}
213
214TEST(unistd, read_EBADF) {
215  // read returns ssize_t which is 64-bits on LP64, so it's worth explicitly checking that
216  // our syscall stubs correctly return a 64-bit -1.
217  char buf[1];
218  ASSERT_EQ(-1, read(-1, buf, sizeof(buf)));
219  ASSERT_EQ(EBADF, errno);
220}
221
222TEST(unistd, syscall_long) {
223  // Check that syscall(3) correctly returns long results.
224  // https://code.google.com/p/android/issues/detail?id=73952
225  // We assume that the break is > 4GiB, but this is potentially flaky.
226  uintptr_t p = reinterpret_cast<uintptr_t>(sbrk(0));
227  ASSERT_EQ(p, static_cast<uintptr_t>(syscall(__NR_brk, 0)));
228}
229
230TEST(unistd, alarm) {
231  ASSERT_EQ(0U, alarm(0));
232}
233
234TEST(unistd, _exit) {
235  int pid = fork();
236  ASSERT_NE(-1, pid) << strerror(errno);
237
238  if (pid == 0) {
239    _exit(99);
240  }
241
242  int status;
243  ASSERT_EQ(pid, waitpid(pid, &status, 0));
244  ASSERT_TRUE(WIFEXITED(status));
245  ASSERT_EQ(99, WEXITSTATUS(status));
246}
247
248TEST(unistd, getenv_unsetenv) {
249  ASSERT_EQ(0, setenv("test-variable", "hello", 1));
250  ASSERT_STREQ("hello", getenv("test-variable"));
251  ASSERT_EQ(0, unsetenv("test-variable"));
252  ASSERT_TRUE(getenv("test-variable") == NULL);
253}
254
255TEST(unistd, unsetenv_EINVAL) {
256  EXPECT_EQ(-1, unsetenv(""));
257  EXPECT_EQ(EINVAL, errno);
258  EXPECT_EQ(-1, unsetenv("a=b"));
259  EXPECT_EQ(EINVAL, errno);
260}
261
262TEST(unistd, setenv_EINVAL) {
263  EXPECT_EQ(-1, setenv(NULL, "value", 0));
264  EXPECT_EQ(EINVAL, errno);
265  EXPECT_EQ(-1, setenv(NULL, "value", 1));
266  EXPECT_EQ(EINVAL, errno);
267  EXPECT_EQ(-1, setenv("", "value", 0));
268  EXPECT_EQ(EINVAL, errno);
269  EXPECT_EQ(-1, setenv("", "value", 1));
270  EXPECT_EQ(EINVAL, errno);
271  EXPECT_EQ(-1, setenv("a=b", "value", 0));
272  EXPECT_EQ(EINVAL, errno);
273  EXPECT_EQ(-1, setenv("a=b", "value", 1));
274  EXPECT_EQ(EINVAL, errno);
275}
276
277TEST(unistd, setenv) {
278  ASSERT_EQ(0, unsetenv("test-variable"));
279
280  char a[] = "a";
281  char b[] = "b";
282  char c[] = "c";
283
284  // New value.
285  EXPECT_EQ(0, setenv("test-variable", a, 0));
286  EXPECT_STREQ(a, getenv("test-variable"));
287
288  // Existing value, no overwrite.
289  EXPECT_EQ(0, setenv("test-variable", b, 0));
290  EXPECT_STREQ(a, getenv("test-variable"));
291
292  // Existing value, overwrite.
293  EXPECT_EQ(0, setenv("test-variable", c, 1));
294  EXPECT_STREQ(c, getenv("test-variable"));
295  // But the arrays backing the values are unchanged.
296  EXPECT_EQ('a', a[0]);
297  EXPECT_EQ('b', b[0]);
298  EXPECT_EQ('c', c[0]);
299
300  ASSERT_EQ(0, unsetenv("test-variable"));
301}
302
303TEST(unistd, putenv) {
304  ASSERT_EQ(0, unsetenv("a"));
305
306  char* s1 = strdup("a=b");
307  ASSERT_EQ(0, putenv(s1));
308
309  ASSERT_STREQ("b", getenv("a"));
310  s1[2] = 'c';
311  ASSERT_STREQ("c", getenv("a"));
312
313  char* s2 = strdup("a=b");
314  ASSERT_EQ(0, putenv(s2));
315
316  ASSERT_STREQ("b", getenv("a"));
317  ASSERT_EQ('c', s1[2]);
318
319  ASSERT_EQ(0, unsetenv("a"));
320  free(s1);
321  free(s2);
322}
323
324TEST(unistd, clearenv) {
325  extern char** environ;
326
327  // Guarantee that environ is not initially empty...
328  ASSERT_EQ(0, setenv("test-variable", "a", 1));
329
330  // Stash a copy.
331  std::vector<char*> old_environ;
332  for (size_t i = 0; environ[i] != NULL; ++i) {
333    old_environ.push_back(strdup(environ[i]));
334  }
335
336  ASSERT_EQ(0, clearenv());
337
338  EXPECT_TRUE(environ == NULL || environ[0] == NULL);
339  EXPECT_EQ(NULL, getenv("test-variable"));
340  EXPECT_EQ(0, setenv("test-variable", "post-clear", 1));
341  EXPECT_STREQ("post-clear", getenv("test-variable"));
342
343  // Put the old environment back.
344  for (size_t i = 0; i < old_environ.size(); ++i) {
345    EXPECT_EQ(0, putenv(old_environ[i]));
346  }
347
348  // Check it wasn't overwritten.
349  EXPECT_STREQ("a", getenv("test-variable"));
350
351  EXPECT_EQ(0, unsetenv("test-variable"));
352}
353
354static void TestFsyncFunction(int (*fn)(int)) {
355  int fd;
356
357  // Can't sync an invalid fd.
358  errno = 0;
359  EXPECT_EQ(-1, fn(-1));
360  EXPECT_EQ(EBADF, errno);
361
362  // It doesn't matter whether you've opened a file for write or not.
363  TemporaryFile tf;
364  ASSERT_NE(-1, tf.fd);
365
366  EXPECT_EQ(0, fn(tf.fd));
367
368  ASSERT_NE(-1, fd = open(tf.filename, O_RDONLY));
369  EXPECT_EQ(0, fn(fd));
370  close(fd);
371
372  ASSERT_NE(-1, fd = open(tf.filename, O_RDWR));
373  EXPECT_EQ(0, fn(fd));
374  close(fd);
375
376  // The fd can even be a directory.
377  ASSERT_NE(-1, fd = open("/", O_RDONLY));
378  EXPECT_EQ(0, fn(fd));
379  close(fd);
380
381  // But some file systems may choose to be fussy...
382  errno = 0;
383  ASSERT_NE(-1, fd = open("/proc/version", O_RDONLY));
384  EXPECT_EQ(-1, fn(fd));
385  EXPECT_EQ(EINVAL, errno);
386  close(fd);
387}
388
389TEST(unistd, fdatasync) {
390  TestFsyncFunction(fdatasync);
391}
392
393TEST(unistd, fsync) {
394  TestFsyncFunction(fsync);
395}
396
397static void AssertGetPidCorrect() {
398  // The loop is just to make manual testing/debugging with strace easier.
399  pid_t getpid_syscall_result = syscall(__NR_getpid);
400  for (size_t i = 0; i < 128; ++i) {
401    ASSERT_EQ(getpid_syscall_result, getpid());
402  }
403}
404
405TEST(unistd, getpid_caching_and_fork) {
406  pid_t parent_pid = getpid();
407  ASSERT_EQ(syscall(__NR_getpid), parent_pid);
408
409  pid_t fork_result = fork();
410  ASSERT_NE(fork_result, -1);
411  if (fork_result == 0) {
412    // We're the child.
413    AssertGetPidCorrect();
414    ASSERT_EQ(parent_pid, getppid());
415    _exit(123);
416  } else {
417    // We're the parent.
418    ASSERT_EQ(parent_pid, getpid());
419
420    int status;
421    ASSERT_EQ(fork_result, waitpid(fork_result, &status, 0));
422    ASSERT_TRUE(WIFEXITED(status));
423    ASSERT_EQ(123, WEXITSTATUS(status));
424  }
425}
426
427static int GetPidCachingCloneStartRoutine(void*) {
428  AssertGetPidCorrect();
429  return 123;
430}
431
432TEST(unistd, getpid_caching_and_clone) {
433  pid_t parent_pid = getpid();
434  ASSERT_EQ(syscall(__NR_getpid), parent_pid);
435
436  void* child_stack[1024];
437  int clone_result = clone(GetPidCachingCloneStartRoutine, &child_stack[1024], CLONE_NEWNS | SIGCHLD, NULL);
438  if (clone_result == -1 && errno == EPERM && getuid() != 0) {
439    GTEST_LOG_(INFO) << "This test only works if you have permission to CLONE_NEWNS; try running as root.\n";
440    return;
441  }
442  ASSERT_NE(clone_result, -1);
443
444  ASSERT_EQ(parent_pid, getpid());
445
446  int status;
447  ASSERT_EQ(clone_result, waitpid(clone_result, &status, 0));
448  ASSERT_TRUE(WIFEXITED(status));
449  ASSERT_EQ(123, WEXITSTATUS(status));
450}
451
452static void* GetPidCachingPthreadStartRoutine(void*) {
453  AssertGetPidCorrect();
454  return NULL;
455}
456
457TEST(unistd, getpid_caching_and_pthread_create) {
458  pid_t parent_pid = getpid();
459
460  pthread_t t;
461  ASSERT_EQ(0, pthread_create(&t, NULL, GetPidCachingPthreadStartRoutine, NULL));
462
463  ASSERT_EQ(parent_pid, getpid());
464
465  void* result;
466  ASSERT_EQ(0, pthread_join(t, &result));
467  ASSERT_EQ(NULL, result);
468}
469
470class unistd_DeathTest : public BionicDeathTest {};
471
472TEST_F(unistd_DeathTest, abort) {
473  ASSERT_EXIT(abort(), testing::KilledBySignal(SIGABRT), "");
474}
475
476TEST(unistd, sethostname) {
477  // The permissions check happens before the argument check, so this will
478  // fail for a different reason if you're running as root than if you're
479  // not, but it'll fail either way. Checking that we have the symbol is about
480  // all we can do for sethostname(2).
481  ASSERT_EQ(-1, sethostname("", -1));
482}
483
484TEST(unistd, gethostname) {
485  char hostname[HOST_NAME_MAX + 1];
486  memset(hostname, 0, sizeof(hostname));
487
488  // Can we get the hostname with a big buffer?
489  ASSERT_EQ(0, gethostname(hostname, HOST_NAME_MAX));
490
491  // Can we get the hostname with a right-sized buffer?
492  errno = 0;
493  ASSERT_EQ(0, gethostname(hostname, strlen(hostname) + 1));
494
495  // Does uname(2) agree?
496  utsname buf;
497  ASSERT_EQ(0, uname(&buf));
498  ASSERT_EQ(0, strncmp(hostname, buf.nodename, SYS_NMLN));
499  ASSERT_GT(strlen(hostname), 0U);
500
501  // Do we correctly detect truncation?
502  errno = 0;
503  ASSERT_EQ(-1, gethostname(hostname, strlen(hostname)));
504  ASSERT_EQ(ENAMETOOLONG, errno);
505}
506
507TEST(unistd, pathconf_fpathconf) {
508  TemporaryFile tf;
509  long rc = 0L;
510  // As a file system's block size is always power of 2, the configure values
511  // for ALLOC and XFER should be power of 2 as well.
512  rc = pathconf(tf.filename, _PC_ALLOC_SIZE_MIN);
513  ASSERT_TRUE(rc > 0 && powerof2(rc));
514  rc = pathconf(tf.filename, _PC_REC_MIN_XFER_SIZE);
515  ASSERT_TRUE(rc > 0 && powerof2(rc));
516  rc = pathconf(tf.filename, _PC_REC_XFER_ALIGN);
517  ASSERT_TRUE(rc > 0 && powerof2(rc));
518
519  rc = fpathconf(tf.fd, _PC_ALLOC_SIZE_MIN);
520  ASSERT_TRUE(rc > 0 && powerof2(rc));
521  rc = fpathconf(tf.fd, _PC_REC_MIN_XFER_SIZE);
522  ASSERT_TRUE(rc > 0 && powerof2(rc));
523  rc = fpathconf(tf.fd, _PC_REC_XFER_ALIGN);
524  ASSERT_TRUE(rc > 0 && powerof2(rc));
525}
526
527
528TEST(unistd, _POSIX_macros_smoke) {
529  // Make a tight verification of _POSIX_* / _POSIX2_* / _XOPEN_* macros, to prevent change by mistake.
530  // Verify according to POSIX.1-2008.
531  EXPECT_EQ(200809L, _POSIX_VERSION);
532
533  EXPECT_EQ(_POSIX_VERSION, _POSIX_ADVISORY_INFO);
534  EXPECT_GT(_POSIX_AIO_LISTIO_MAX, 0);
535  EXPECT_GT(_POSIX_AIO_MAX, 0);
536  EXPECT_GT(_POSIX_ARG_MAX, 0);
537  EXPECT_GT(_POSIX_CHILD_MAX, 0);
538  EXPECT_NE(_POSIX_CHOWN_RESTRICTED, -1);
539  EXPECT_EQ(_POSIX_VERSION, _POSIX_CLOCK_SELECTION);
540  EXPECT_EQ(0, _POSIX_CPUTIME);             // Use sysconf to detect support at runtime.
541  EXPECT_GT(_POSIX_DELAYTIMER_MAX, 0);
542  EXPECT_EQ(_POSIX_VERSION, _POSIX_FSYNC);
543  EXPECT_GT(_POSIX_HOST_NAME_MAX, 0);
544  EXPECT_EQ(_POSIX_VERSION, _POSIX_IPV6);
545  EXPECT_GT(_POSIX_JOB_CONTROL, 0);
546  EXPECT_GT(_POSIX_LINK_MAX, 0);
547  EXPECT_GT(_POSIX_LOGIN_NAME_MAX, 0);
548  EXPECT_EQ(_POSIX_VERSION, _POSIX_MAPPED_FILES);
549  EXPECT_GT(_POSIX_MAX_CANON, 0);
550  EXPECT_GT(_POSIX_MAX_INPUT, 0);
551  EXPECT_EQ(_POSIX_VERSION, _POSIX_MEMLOCK);
552  EXPECT_EQ(_POSIX_VERSION, _POSIX_MEMLOCK_RANGE);
553  EXPECT_EQ(_POSIX_VERSION, _POSIX_MEMORY_PROTECTION);
554  EXPECT_EQ(0, _POSIX_MONOTONIC_CLOCK);
555  EXPECT_GT(_POSIX_MQ_OPEN_MAX, 0);
556  EXPECT_GT(_POSIX_MQ_PRIO_MAX, 0);
557  EXPECT_GT(_POSIX_NAME_MAX, 0);
558  EXPECT_GT(_POSIX_NGROUPS_MAX, 0);
559  EXPECT_GT(_POSIX_NO_TRUNC, 0);
560  EXPECT_GT(_POSIX_OPEN_MAX, 0);
561  EXPECT_GT(_POSIX_PATH_MAX, 0);
562  EXPECT_GT(_POSIX_PIPE_BUF, 0);
563  EXPECT_EQ(_POSIX_VERSION, _POSIX_PRIORITY_SCHEDULING);
564  EXPECT_EQ(_POSIX_VERSION, _POSIX_RAW_SOCKETS);
565  EXPECT_EQ(_POSIX_VERSION, _POSIX_READER_WRITER_LOCKS);
566  EXPECT_EQ(_POSIX_VERSION, _POSIX_REALTIME_SIGNALS);
567  EXPECT_GT(_POSIX_REGEXP, 0);
568  EXPECT_GT(_POSIX_RE_DUP_MAX, 0);
569  EXPECT_GT(_POSIX_SAVED_IDS, 0);
570  EXPECT_EQ(_POSIX_VERSION, _POSIX_SEMAPHORES);
571  EXPECT_GT(_POSIX_SEM_NSEMS_MAX, 0);
572  EXPECT_GT(_POSIX_SEM_VALUE_MAX, 0);
573  EXPECT_GT(_POSIX_SHELL, 0);
574  EXPECT_GT(_POSIX_SIGQUEUE_MAX, 0);
575  EXPECT_EQ(-1, _POSIX_SPORADIC_SERVER);
576  EXPECT_GT(_POSIX_SSIZE_MAX, 0);
577  EXPECT_GT(_POSIX_STREAM_MAX, 0);
578  EXPECT_GT(_POSIX_SYMLINK_MAX, 0);
579  EXPECT_GT(_POSIX_SYMLOOP_MAX, 0);
580  EXPECT_EQ(_POSIX_VERSION, _POSIX_SYNCHRONIZED_IO);
581  EXPECT_EQ(_POSIX_VERSION, _POSIX_THREADS);
582  EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_ATTR_STACKADDR);
583  EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_ATTR_STACKSIZE);
584  EXPECT_EQ(0, _POSIX_THREAD_CPUTIME);       // Use sysconf to detect support at runtime.
585  EXPECT_GT(_POSIX_THREAD_DESTRUCTOR_ITERATIONS, 0);
586  EXPECT_EQ(_POSIX_THREAD_KEYS_MAX, 128);
587  EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_PRIORITY_SCHEDULING);
588  EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_PRIO_INHERIT);
589  EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_PRIO_PROTECT);
590  EXPECT_EQ(-1, _POSIX_THREAD_ROBUST_PRIO_PROTECT);
591  EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_SAFE_FUNCTIONS);
592  EXPECT_EQ(-1, _POSIX_THREAD_SPORADIC_SERVER);
593  EXPECT_GT(_POSIX_THREAD_THREADS_MAX, 0);
594  EXPECT_EQ(_POSIX_VERSION, _POSIX_TIMEOUTS);
595  EXPECT_EQ(_POSIX_VERSION, _POSIX_TIMERS);
596  EXPECT_GT(_POSIX_TIMER_MAX, 0);
597  EXPECT_EQ(-1, _POSIX_TRACE);
598  EXPECT_EQ(-1, _POSIX_TRACE_EVENT_FILTER);
599  EXPECT_EQ(-1, _POSIX_TRACE_INHERIT);
600  EXPECT_EQ(-1, _POSIX_TRACE_LOG);
601  EXPECT_GT(_POSIX_TTY_NAME_MAX, 0);
602  EXPECT_EQ(-1, _POSIX_TYPED_MEMORY_OBJECTS);
603  EXPECT_GT(_POSIX_TZNAME_MAX, 0);
604  EXPECT_NE(-1, _POSIX_VDISABLE);
605
606  EXPECT_GT(_POSIX2_BC_BASE_MAX, 0);
607  EXPECT_GT(_POSIX2_BC_DIM_MAX, 0);
608  EXPECT_GT(_POSIX2_BC_SCALE_MAX, 0);
609  EXPECT_GT(_POSIX2_BC_STRING_MAX, 0);
610  EXPECT_GT(_POSIX2_CHARCLASS_NAME_MAX, 0);
611  EXPECT_GT(_POSIX2_COLL_WEIGHTS_MAX, 0);
612  EXPECT_EQ(_POSIX_VERSION, _POSIX2_C_BIND);
613  EXPECT_GT(_POSIX2_EXPR_NEST_MAX, 0);
614  EXPECT_GT(_POSIX2_LINE_MAX, 0);
615  EXPECT_GT(_POSIX2_RE_DUP_MAX, 0);
616
617  EXPECT_EQ(700, _XOPEN_VERSION);
618  EXPECT_GT(_XOPEN_IOV_MAX, 0);
619  EXPECT_GT(_XOPEN_UNIX, 0);
620
621#if defined(__BIONIC__)
622  // These tests only pass on bionic, as bionic and glibc has different support on these macros.
623  // Macros like _POSIX_ASYNCHRONOUS_IO are not supported on bionic yet.
624  EXPECT_EQ(-1, _POSIX_ASYNCHRONOUS_IO);
625  EXPECT_EQ(-1, _POSIX_BARRIERS);
626  EXPECT_EQ(-1, _POSIX_MESSAGE_PASSING);
627  EXPECT_EQ(-1, _POSIX_PRIORITIZED_IO);
628  EXPECT_EQ(-1, _POSIX_SHARED_MEMORY_OBJECTS);
629  EXPECT_EQ(-1, _POSIX_SPAWN);
630  EXPECT_EQ(-1, _POSIX_SPIN_LOCKS);
631  EXPECT_EQ(-1, _POSIX_THREAD_PROCESS_SHARED);
632  EXPECT_EQ(-1, _POSIX_THREAD_ROBUST_PRIO_INHERIT);
633
634  EXPECT_EQ(-1, _POSIX2_VERSION);
635  EXPECT_EQ(-1, _POSIX2_CHAR_TERM);
636  EXPECT_EQ(-1, _POSIX2_C_DEV);
637  EXPECT_EQ(-1, _POSIX2_LOCALEDEF);
638  EXPECT_EQ(-1, _POSIX2_SW_DEV);
639  EXPECT_EQ(-1, _POSIX2_UPE);
640
641  EXPECT_EQ(-1, _XOPEN_ENH_I18N);
642  EXPECT_EQ(-1, _XOPEN_CRYPT);
643  EXPECT_EQ(-1, _XOPEN_LEGACY);
644  EXPECT_EQ(-1, _XOPEN_REALTIME);
645  EXPECT_EQ(-1, _XOPEN_REALTIME_THREADS);
646  EXPECT_EQ(-1, _XOPEN_SHM);
647
648#endif // defined(__BIONIC__)
649}
650
651#define VERIFY_SYSCONF_NOT_SUPPORT(name) VerifySysconf(name, #name, [](long v){return v == -1;})
652
653// sysconf() means unlimited when it returns -1 with errno unchanged.
654#define VERIFY_SYSCONF_POSITIVE(name) \
655  VerifySysconf(name, #name, [](long v){return (v > 0 || v == -1);})
656
657#define VERIFY_SYSCONF_POSIX_VERSION(name) \
658  VerifySysconf(name, #name, [](long v){return v == _POSIX_VERSION;})
659
660static void VerifySysconf(int option, const char *option_name, bool (*verify)(long)) {
661  errno = 0;
662  long ret = sysconf(option);
663  EXPECT_TRUE(0 == errno && verify(ret)) << "name = " << option_name << ", ret = "
664      << ret <<", Error Message: " << strerror(errno);
665}
666
667TEST(unistd, sysconf) {
668  VERIFY_SYSCONF_POSIX_VERSION(_SC_ADVISORY_INFO);
669  VERIFY_SYSCONF_POSITIVE(_SC_ARG_MAX);
670  VERIFY_SYSCONF_POSITIVE(_SC_BC_BASE_MAX);
671  VERIFY_SYSCONF_POSITIVE(_SC_BC_DIM_MAX);
672  VERIFY_SYSCONF_POSITIVE(_SC_BC_SCALE_MAX);
673  VERIFY_SYSCONF_POSITIVE(_SC_CHILD_MAX);
674  VERIFY_SYSCONF_POSITIVE(_SC_CLK_TCK);
675  VERIFY_SYSCONF_POSITIVE(_SC_COLL_WEIGHTS_MAX);
676  VERIFY_SYSCONF_POSIX_VERSION(_SC_CPUTIME);
677  VERIFY_SYSCONF_POSITIVE(_SC_EXPR_NEST_MAX);
678  VERIFY_SYSCONF_POSITIVE(_SC_LINE_MAX);
679  VERIFY_SYSCONF_POSITIVE(_SC_NGROUPS_MAX);
680  VERIFY_SYSCONF_POSITIVE(_SC_OPEN_MAX);
681  VERIFY_SYSCONF_POSITIVE(_SC_PASS_MAX);
682  VERIFY_SYSCONF_POSIX_VERSION(_SC_2_C_BIND);
683  VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_FORT_DEV);
684  VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_FORT_RUN);
685  VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_UPE);
686  VERIFY_SYSCONF_POSITIVE(_SC_JOB_CONTROL);
687  VERIFY_SYSCONF_POSITIVE(_SC_SAVED_IDS);
688  VERIFY_SYSCONF_POSIX_VERSION(_SC_VERSION);
689  VERIFY_SYSCONF_POSITIVE(_SC_RE_DUP_MAX);
690  VERIFY_SYSCONF_POSITIVE(_SC_STREAM_MAX);
691  VERIFY_SYSCONF_POSITIVE(_SC_TZNAME_MAX);
692  VerifySysconf(_SC_XOPEN_VERSION, "_SC_XOPEN_VERSION", [](long v){return v == _XOPEN_VERSION;});
693  VERIFY_SYSCONF_POSITIVE(_SC_ATEXIT_MAX);
694  VERIFY_SYSCONF_POSITIVE(_SC_IOV_MAX);
695  VERIFY_SYSCONF_POSITIVE(_SC_PAGESIZE);
696  VERIFY_SYSCONF_POSITIVE(_SC_PAGE_SIZE);
697  VERIFY_SYSCONF_POSITIVE(_SC_XOPEN_UNIX);
698  VERIFY_SYSCONF_POSITIVE(_SC_AIO_LISTIO_MAX);
699  VERIFY_SYSCONF_POSITIVE(_SC_AIO_MAX);
700  VerifySysconf(_SC_AIO_PRIO_DELTA_MAX, "_SC_AIO_PRIO_DELTA_MAX", [](long v){return v >= 0;});
701  VERIFY_SYSCONF_POSITIVE(_SC_DELAYTIMER_MAX);
702  VERIFY_SYSCONF_POSITIVE(_SC_MQ_OPEN_MAX);
703  VERIFY_SYSCONF_POSITIVE(_SC_MQ_PRIO_MAX);
704  VERIFY_SYSCONF_POSITIVE(_SC_RTSIG_MAX);
705  VERIFY_SYSCONF_POSITIVE(_SC_SEM_NSEMS_MAX);
706  VERIFY_SYSCONF_POSITIVE(_SC_SEM_VALUE_MAX);
707  VERIFY_SYSCONF_POSITIVE(_SC_TIMER_MAX);
708  VERIFY_SYSCONF_POSIX_VERSION(_SC_FSYNC);
709  VERIFY_SYSCONF_POSIX_VERSION(_SC_MAPPED_FILES);
710  VERIFY_SYSCONF_POSIX_VERSION(_SC_MEMLOCK);
711  VERIFY_SYSCONF_POSIX_VERSION(_SC_MEMLOCK_RANGE);
712  VERIFY_SYSCONF_POSIX_VERSION(_SC_MEMORY_PROTECTION);
713  VERIFY_SYSCONF_POSIX_VERSION(_SC_PRIORITY_SCHEDULING);
714  VERIFY_SYSCONF_POSIX_VERSION(_SC_REALTIME_SIGNALS);
715  VERIFY_SYSCONF_POSIX_VERSION(_SC_SEMAPHORES);
716  VERIFY_SYSCONF_POSIX_VERSION(_SC_SYNCHRONIZED_IO);
717  VERIFY_SYSCONF_POSIX_VERSION(_SC_TIMERS);
718  VERIFY_SYSCONF_POSITIVE(_SC_GETGR_R_SIZE_MAX);
719  VERIFY_SYSCONF_POSITIVE(_SC_GETPW_R_SIZE_MAX);
720  VERIFY_SYSCONF_POSITIVE(_SC_LOGIN_NAME_MAX);
721  VERIFY_SYSCONF_POSITIVE(_SC_THREAD_DESTRUCTOR_ITERATIONS);
722  VERIFY_SYSCONF_POSITIVE(_SC_THREAD_KEYS_MAX);
723  VERIFY_SYSCONF_POSITIVE(_SC_THREAD_STACK_MIN);
724  VERIFY_SYSCONF_POSITIVE(_SC_THREAD_THREADS_MAX);
725  VERIFY_SYSCONF_POSITIVE(_SC_TTY_NAME_MAX);
726  VERIFY_SYSCONF_POSIX_VERSION(_SC_THREADS);
727  VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_ATTR_STACKADDR);
728  VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_ATTR_STACKSIZE);
729  VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_PRIORITY_SCHEDULING);
730  VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_PRIO_INHERIT);
731  VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_PRIO_PROTECT);
732  VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_SAFE_FUNCTIONS);
733  VERIFY_SYSCONF_POSITIVE(_SC_NPROCESSORS_CONF);
734  VERIFY_SYSCONF_POSITIVE(_SC_NPROCESSORS_ONLN);
735  VERIFY_SYSCONF_POSITIVE(_SC_PHYS_PAGES);
736  VERIFY_SYSCONF_POSITIVE(_SC_AVPHYS_PAGES);
737  VERIFY_SYSCONF_POSIX_VERSION(_SC_MONOTONIC_CLOCK);
738  VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_PBS);
739  VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_PBS_ACCOUNTING);
740  VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_PBS_CHECKPOINT);
741  VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_PBS_LOCATE);
742  VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_PBS_MESSAGE);
743  VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_PBS_TRACK);
744  VERIFY_SYSCONF_POSIX_VERSION(_SC_CLOCK_SELECTION);
745  VERIFY_SYSCONF_POSITIVE(_SC_HOST_NAME_MAX);
746  VERIFY_SYSCONF_POSIX_VERSION(_SC_IPV6);
747  VERIFY_SYSCONF_POSIX_VERSION(_SC_RAW_SOCKETS);
748  VERIFY_SYSCONF_POSIX_VERSION(_SC_READER_WRITER_LOCKS);
749  VERIFY_SYSCONF_POSITIVE(_SC_REGEXP);
750  VERIFY_SYSCONF_POSITIVE(_SC_SHELL);
751  VERIFY_SYSCONF_NOT_SUPPORT(_SC_SPORADIC_SERVER);
752  VERIFY_SYSCONF_POSITIVE(_SC_SYMLOOP_MAX);
753  VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_CPUTIME);
754  VERIFY_SYSCONF_NOT_SUPPORT(_SC_THREAD_SPORADIC_SERVER);
755  VERIFY_SYSCONF_POSIX_VERSION(_SC_TIMEOUTS);
756  VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE);
757  VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE_EVENT_FILTER);
758  VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE_EVENT_NAME_MAX);
759  VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE_INHERIT);
760  VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE_LOG);
761  VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE_NAME_MAX);
762  VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE_SYS_MAX);
763  VERIFY_SYSCONF_NOT_SUPPORT(_SC_TRACE_USER_EVENT_MAX);
764  VERIFY_SYSCONF_NOT_SUPPORT(_SC_TYPED_MEMORY_OBJECTS);
765  VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_STREAMS);
766
767#if defined(__LP64__)
768  VERIFY_SYSCONF_NOT_SUPPORT(_SC_V7_ILP32_OFF32);
769  VERIFY_SYSCONF_NOT_SUPPORT(_SC_V7_ILP32_OFFBIG);
770  VERIFY_SYSCONF_POSITIVE(_SC_V7_LP64_OFF64);
771  VERIFY_SYSCONF_POSITIVE(_SC_V7_LPBIG_OFFBIG);
772#else
773  VERIFY_SYSCONF_POSITIVE(_SC_V7_ILP32_OFF32);
774#if defined(__BIONIC__)
775  // bionic does not support 64 bits off_t type on 32bit machine.
776  VERIFY_SYSCONF_NOT_SUPPORT(_SC_V7_ILP32_OFFBIG);
777#endif
778  VERIFY_SYSCONF_NOT_SUPPORT(_SC_V7_LP64_OFF64);
779  VERIFY_SYSCONF_NOT_SUPPORT(_SC_V7_LPBIG_OFFBIG);
780#endif
781
782#if defined(__BIONIC__)
783  // Tests can only run on bionic, as bionic and glibc have different support for these options.
784  // Below options are not supported on bionic yet.
785  VERIFY_SYSCONF_NOT_SUPPORT(_SC_ASYNCHRONOUS_IO);
786  VERIFY_SYSCONF_NOT_SUPPORT(_SC_BARRIERS);
787  VERIFY_SYSCONF_NOT_SUPPORT(_SC_MESSAGE_PASSING);
788  VERIFY_SYSCONF_NOT_SUPPORT(_SC_PRIORITIZED_IO);
789  VERIFY_SYSCONF_NOT_SUPPORT(_SC_SHARED_MEMORY_OBJECTS);
790  VERIFY_SYSCONF_NOT_SUPPORT(_SC_SPAWN);
791  VERIFY_SYSCONF_NOT_SUPPORT(_SC_SPIN_LOCKS);
792  VERIFY_SYSCONF_NOT_SUPPORT(_SC_THREAD_PROCESS_SHARED);
793  VERIFY_SYSCONF_NOT_SUPPORT(_SC_THREAD_ROBUST_PRIO_INHERIT);
794  VERIFY_SYSCONF_NOT_SUPPORT(_SC_THREAD_ROBUST_PRIO_PROTECT);
795
796  VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_C_DEV);
797  VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_CHAR_TERM);
798  VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_LOCALEDEF);
799  VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_SW_DEV);
800  VERIFY_SYSCONF_NOT_SUPPORT(_SC_2_VERSION);
801
802  VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_CRYPT);
803  VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_ENH_I18N);
804  VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_LEGACY);
805  VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_REALTIME);
806  VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_REALTIME_THREADS);
807  VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_SHM);
808  VERIFY_SYSCONF_NOT_SUPPORT(_SC_XOPEN_UUCP);
809#endif // defined(__BIONIC__)
810}
811
812TEST(unistd, dup2_same) {
813  // POSIX says of dup2:
814  // If fildes2 is already a valid open file descriptor ...
815  // [and] fildes is equal to fildes2 ... dup2() shall return
816  // fildes2 without closing it.
817  // This isn't true of dup3(2), so we need to manually implement that.
818
819  // Equal and valid.
820  int fd = open("/proc/version", O_RDONLY);
821  ASSERT_TRUE(fd != -1);
822  ASSERT_EQ(fd, dup2(fd, fd));
823  ASSERT_EQ(0, close(fd)); // Check that dup2 didn't close fd.
824
825  // Equal, but invalid.
826  errno = 0;
827  ASSERT_EQ(-1, dup2(fd, fd));
828  ASSERT_EQ(EBADF, errno);
829}
830