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
19 #include "BionicDeathTest.h"
20 #include "ScopedSignalHandler.h"
21 #include "TemporaryFile.h"
22 #include "utils.h"
23
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <libgen.h>
27 #include <limits.h>
28 #include <stdint.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>
34 #include <sys/wait.h>
35 #include <unistd.h>
36
37 #include <android-base/file.h>
38 #include <android-base/strings.h>
39
40 #include "private/get_cpu_count_from_string.h"
41
42 #if defined(NOFORTIFY)
43 #define UNISTD_TEST unistd_nofortify
44 #define UNISTD_DEATHTEST unistd_nofortify_DeathTest
45 #else
46 #define UNISTD_TEST unistd
47 #define UNISTD_DEATHTEST unistd_DeathTest
48 #endif
49
get_brk()50 static void* get_brk() {
51 return sbrk(0);
52 }
53
page_align(uintptr_t addr)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);
57 }
58
TEST(UNISTD_TEST,brk)59 TEST(UNISTD_TEST, brk) {
60 void* initial_break = get_brk();
61
62 void* new_break = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(initial_break) + 1);
63 int ret = brk(new_break);
64 if (ret == -1) {
65 ASSERT_EQ(errno, ENOMEM);
66 } else {
67 ASSERT_EQ(0, ret);
68 ASSERT_GE(get_brk(), new_break);
69 }
70
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));
73 ret = brk(new_break);
74 if (ret == -1) {
75 ASSERT_EQ(errno, ENOMEM);
76 } else {
77 ASSERT_EQ(0, ret);
78 ASSERT_EQ(get_brk(), new_break);
79 }
80 }
81
TEST(UNISTD_TEST,brk_ENOMEM)82 TEST(UNISTD_TEST, brk_ENOMEM) {
83 ASSERT_EQ(-1, brk(reinterpret_cast<void*>(-1)));
84 ASSERT_EQ(ENOMEM, errno);
85 }
86
87 #if defined(__GLIBC__)
88 #define SBRK_MIN INTPTR_MIN
89 #define SBRK_MAX INTPTR_MAX
90 #else
91 #define SBRK_MIN PTRDIFF_MIN
92 #define SBRK_MAX PTRDIFF_MAX
93 #endif
94
TEST(UNISTD_TEST,sbrk_ENOMEM)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;
100
101 class ScopedBrk {
102 public:
103 ScopedBrk() : saved_brk_(__bionic_brk) {}
104 virtual ~ScopedBrk() { __bionic_brk = saved_brk_; }
105
106 private:
107 void* saved_brk_;
108 };
109
110 ScopedBrk scope_brk;
111
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);
114
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);
118
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));
121
122 ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MIN));
123 ASSERT_EQ(ENOMEM, errno);
124
125 __bionic_brk = reinterpret_cast<void*>(static_cast<uintptr_t>(PTRDIFF_MAX) - 1);
126
127 ASSERT_EQ(reinterpret_cast<void*>(-1), sbrk(PTRDIFF_MIN + 1));
128 ASSERT_EQ(ENOMEM, errno);
129 #else
130 class ScopedBrk {
131 public:
132 ScopedBrk() : saved_brk_(get_brk()) {}
133 virtual ~ScopedBrk() { brk(saved_brk_); }
134
135 private:
136 void* saved_brk_;
137 };
138
139 ScopedBrk scope_brk;
140
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);
148 #endif
149 }
150
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));
155 }
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);
161 #endif
162 }
163 #endif
164 }
165
TEST(UNISTD_TEST,truncate)166 TEST(UNISTD_TEST, truncate) {
167 TemporaryFile tf;
168 ASSERT_EQ(0, close(tf.fd));
169 ASSERT_EQ(0, truncate(tf.filename, 123));
170
171 struct stat sb;
172 ASSERT_EQ(0, stat(tf.filename, &sb));
173 ASSERT_EQ(123, sb.st_size);
174 }
175
TEST(UNISTD_TEST,truncate64)176 TEST(UNISTD_TEST, truncate64) {
177 TemporaryFile tf;
178 ASSERT_EQ(0, close(tf.fd));
179 ASSERT_EQ(0, truncate64(tf.filename, 123));
180
181 struct stat sb;
182 ASSERT_EQ(0, stat(tf.filename, &sb));
183 ASSERT_EQ(123, sb.st_size);
184 }
185
TEST(UNISTD_TEST,ftruncate)186 TEST(UNISTD_TEST, ftruncate) {
187 TemporaryFile tf;
188 ASSERT_EQ(0, ftruncate(tf.fd, 123));
189 ASSERT_EQ(0, close(tf.fd));
190
191 struct stat sb;
192 ASSERT_EQ(0, stat(tf.filename, &sb));
193 ASSERT_EQ(123, sb.st_size);
194 }
195
TEST(UNISTD_TEST,ftruncate64)196 TEST(UNISTD_TEST, ftruncate64) {
197 TemporaryFile tf;
198 ASSERT_EQ(0, ftruncate64(tf.fd, 123));
199 ASSERT_EQ(0, close(tf.fd));
200
201 struct stat sb;
202 ASSERT_EQ(0, stat(tf.filename, &sb));
203 ASSERT_EQ(123, sb.st_size);
204 }
205
TEST(UNISTD_TEST,ftruncate_negative)206 TEST(UNISTD_TEST, ftruncate_negative) {
207 TemporaryFile tf;
208 errno = 0;
209 ASSERT_EQ(-1, ftruncate(tf.fd, -123));
210 ASSERT_EQ(EINVAL, errno);
211 }
212
213 static bool g_pause_test_flag = false;
PauseTestSignalHandler(int)214 static void PauseTestSignalHandler(int) {
215 g_pause_test_flag = true;
216 }
217
TEST(UNISTD_TEST,pause)218 TEST(UNISTD_TEST, pause) {
219 ScopedSignalHandler handler(SIGALRM, PauseTestSignalHandler);
220
221 alarm(1);
222 ASSERT_FALSE(g_pause_test_flag);
223 ASSERT_EQ(-1, pause());
224 ASSERT_TRUE(g_pause_test_flag);
225 }
226
TEST(UNISTD_TEST,read)227 TEST(UNISTD_TEST, read) {
228 int fd = open("/proc/version", O_RDONLY);
229 ASSERT_TRUE(fd != -1);
230
231 char buf[5];
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');
238 close(fd);
239 }
240
TEST(UNISTD_TEST,read_EBADF)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.
244 char buf[1];
245 ASSERT_EQ(-1, read(-1, buf, sizeof(buf)));
246 ASSERT_EQ(EBADF, errno);
247 }
248
TEST(UNISTD_TEST,syscall_long)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)));
255 }
256
TEST(UNISTD_TEST,alarm)257 TEST(UNISTD_TEST, alarm) {
258 ASSERT_EQ(0U, alarm(0));
259 }
260
TEST(UNISTD_TEST,_exit)261 TEST(UNISTD_TEST, _exit) {
262 pid_t pid = fork();
263 ASSERT_NE(-1, pid) << strerror(errno);
264
265 if (pid == 0) {
266 _exit(99);
267 }
268
269 AssertChildExited(pid, 99);
270 }
271
TEST(UNISTD_TEST,getenv_unsetenv)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);
277 }
278
TEST(UNISTD_TEST,unsetenv_EINVAL)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);
284 }
285
TEST(UNISTD_TEST,setenv_EINVAL)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);
299 }
300
TEST(UNISTD_TEST,setenv)301 TEST(UNISTD_TEST, setenv) {
302 ASSERT_EQ(0, unsetenv("test-variable"));
303
304 char a[] = "a";
305 char b[] = "b";
306 char c[] = "c";
307
308 // New value.
309 EXPECT_EQ(0, setenv("test-variable", a, 0));
310 EXPECT_STREQ(a, getenv("test-variable"));
311
312 // Existing value, no overwrite.
313 EXPECT_EQ(0, setenv("test-variable", b, 0));
314 EXPECT_STREQ(a, getenv("test-variable"));
315
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]);
323
324 ASSERT_EQ(0, unsetenv("test-variable"));
325 }
326
TEST(UNISTD_TEST,putenv)327 TEST(UNISTD_TEST, putenv) {
328 ASSERT_EQ(0, unsetenv("a"));
329
330 char* s1 = strdup("a=b");
331 ASSERT_EQ(0, putenv(s1));
332
333 ASSERT_STREQ("b", getenv("a"));
334 s1[2] = 'c';
335 ASSERT_STREQ("c", getenv("a"));
336
337 char* s2 = strdup("a=b");
338 ASSERT_EQ(0, putenv(s2));
339
340 ASSERT_STREQ("b", getenv("a"));
341 ASSERT_EQ('c', s1[2]);
342
343 ASSERT_EQ(0, unsetenv("a"));
344 free(s1);
345 free(s2);
346 }
347
TEST(UNISTD_TEST,clearenv)348 TEST(UNISTD_TEST, clearenv) {
349 extern char** environ;
350
351 // Guarantee that environ is not initially empty...
352 ASSERT_EQ(0, setenv("test-variable", "a", 1));
353
354 // Stash a copy.
355 std::vector<char*> old_environ;
356 for (size_t i = 0; environ[i] != NULL; ++i) {
357 old_environ.push_back(strdup(environ[i]));
358 }
359
360 ASSERT_EQ(0, clearenv());
361
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"));
366
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]));
370 }
371
372 // Check it wasn't overwritten.
373 EXPECT_STREQ("a", getenv("test-variable"));
374
375 EXPECT_EQ(0, unsetenv("test-variable"));
376 }
377
TestSyncFunction(int (* fn)(int))378 static void TestSyncFunction(int (*fn)(int)) {
379 int fd;
380
381 // Can't sync an invalid fd.
382 errno = 0;
383 EXPECT_EQ(-1, fn(-1));
384 EXPECT_EQ(EBADF, errno);
385
386 // It doesn't matter whether you've opened a file for write or not.
387 TemporaryFile tf;
388 ASSERT_NE(-1, tf.fd);
389
390 EXPECT_EQ(0, fn(tf.fd));
391
392 ASSERT_NE(-1, fd = open(tf.filename, O_RDONLY));
393 EXPECT_EQ(0, fn(fd));
394 close(fd);
395
396 ASSERT_NE(-1, fd = open(tf.filename, O_RDWR));
397 EXPECT_EQ(0, fn(fd));
398 close(fd);
399
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));
403 close(fd);
404 }
405
TestFsyncFunction(int (* fn)(int))406 static void TestFsyncFunction(int (*fn)(int)) {
407 TestSyncFunction(fn);
408
409 // But some file systems are fussy about fsync/fdatasync...
410 errno = 0;
411 int fd = open("/proc/version", O_RDONLY);
412 ASSERT_NE(-1, fd);
413 EXPECT_EQ(-1, fn(fd));
414 EXPECT_EQ(EINVAL, errno);
415 close(fd);
416 }
417
TEST(UNISTD_TEST,fdatasync)418 TEST(UNISTD_TEST, fdatasync) {
419 TestFsyncFunction(fdatasync);
420 }
421
TEST(UNISTD_TEST,fsync)422 TEST(UNISTD_TEST, fsync) {
423 TestFsyncFunction(fsync);
424 }
425
TEST(UNISTD_TEST,syncfs)426 TEST(UNISTD_TEST, syncfs) {
427 TestSyncFunction(syncfs);
428 }
429
AssertGetPidCorrect()430 static void AssertGetPidCorrect() {
431 // The loop is just to make manual testing/debugging with strace easier.
432 pid_t getpid_syscall_result = syscall(__NR_getpid);
433 for (size_t i = 0; i < 128; ++i) {
434 ASSERT_EQ(getpid_syscall_result, getpid());
435 }
436 }
437
TestGetPidCachingWithFork(int (* fork_fn)(),void (* exit_fn)(int))438 static void TestGetPidCachingWithFork(int (*fork_fn)(), void (*exit_fn)(int)) {
439 pid_t parent_pid = getpid();
440 ASSERT_EQ(syscall(__NR_getpid), parent_pid);
441
442 pid_t fork_result = fork_fn();
443 ASSERT_NE(fork_result, -1);
444 if (fork_result == 0) {
445 // We're the child.
446 ASSERT_NO_FATAL_FAILURE(AssertGetPidCorrect());
447 ASSERT_EQ(parent_pid, getppid());
448 exit_fn(123);
449 } else {
450 // We're the parent.
451 ASSERT_EQ(parent_pid, getpid());
452 AssertChildExited(fork_result, 123);
453 }
454 }
455
456 // gettid() is marked as __attribute_const__, which will have the compiler
457 // optimize out multiple calls to gettid in the same function. This wrapper
458 // defeats that optimization.
GetTidForTest()459 static __attribute__((__noinline__)) pid_t GetTidForTest() {
460 __asm__("");
461 return gettid();
462 }
463
AssertGetTidCorrect()464 static void AssertGetTidCorrect() {
465 // The loop is just to make manual testing/debugging with strace easier.
466 pid_t gettid_syscall_result = syscall(__NR_gettid);
467 for (size_t i = 0; i < 128; ++i) {
468 ASSERT_EQ(gettid_syscall_result, GetTidForTest());
469 }
470 }
471
TestGetTidCachingWithFork(int (* fork_fn)(),void (* exit_fn)(int))472 static void TestGetTidCachingWithFork(int (*fork_fn)(), void (*exit_fn)(int)) {
473 pid_t parent_tid = GetTidForTest();
474 ASSERT_EQ(syscall(__NR_gettid), parent_tid);
475
476 pid_t fork_result = fork_fn();
477 ASSERT_NE(fork_result, -1);
478 if (fork_result == 0) {
479 // We're the child.
480 EXPECT_EQ(syscall(__NR_getpid), syscall(__NR_gettid));
481 EXPECT_EQ(getpid(), GetTidForTest()) << "real tid is " << syscall(__NR_gettid)
482 << ", pid is " << syscall(__NR_getpid);
483 ASSERT_NO_FATAL_FAILURE(AssertGetTidCorrect());
484 exit_fn(123);
485 } else {
486 // We're the parent.
487 ASSERT_EQ(parent_tid, GetTidForTest());
488 AssertChildExited(fork_result, 123);
489 }
490 }
491
TEST(UNISTD_TEST,getpid_caching_and_fork)492 TEST(UNISTD_TEST, getpid_caching_and_fork) {
493 TestGetPidCachingWithFork(fork, exit);
494 }
495
TEST(UNISTD_TEST,gettid_caching_and_fork)496 TEST(UNISTD_TEST, gettid_caching_and_fork) {
497 TestGetTidCachingWithFork(fork, exit);
498 }
499
TEST(UNISTD_TEST,getpid_caching_and_vfork)500 TEST(UNISTD_TEST, getpid_caching_and_vfork) {
501 TestGetPidCachingWithFork(vfork, _exit);
502 }
503
CloneLikeFork()504 static int CloneLikeFork() {
505 return clone(nullptr, nullptr, SIGCHLD, nullptr);
506 }
507
TEST(UNISTD_TEST,getpid_caching_and_clone_process)508 TEST(UNISTD_TEST, getpid_caching_and_clone_process) {
509 TestGetPidCachingWithFork(CloneLikeFork, exit);
510 }
511
TEST(UNISTD_TEST,gettid_caching_and_clone_process)512 TEST(UNISTD_TEST, gettid_caching_and_clone_process) {
513 TestGetTidCachingWithFork(CloneLikeFork, exit);
514 }
515
CloneAndSetTid()516 static int CloneAndSetTid() {
517 pid_t child_tid = 0;
518 pid_t parent_tid = GetTidForTest();
519
520 int rv = clone(nullptr, nullptr, CLONE_CHILD_SETTID | SIGCHLD, nullptr, nullptr, nullptr, &child_tid);
521 EXPECT_NE(-1, rv);
522
523 if (rv == 0) {
524 // Child.
525 EXPECT_EQ(child_tid, GetTidForTest());
526 EXPECT_NE(child_tid, parent_tid);
527 } else {
528 EXPECT_NE(child_tid, GetTidForTest());
529 EXPECT_NE(child_tid, parent_tid);
530 EXPECT_EQ(GetTidForTest(), parent_tid);
531 }
532
533 return rv;
534 }
535
TEST(UNISTD_TEST,gettid_caching_and_clone_process_settid)536 TEST(UNISTD_TEST, gettid_caching_and_clone_process_settid) {
537 TestGetTidCachingWithFork(CloneAndSetTid, exit);
538 }
539
CloneStartRoutine(int (* start_routine)(void *))540 static int CloneStartRoutine(int (*start_routine)(void*)) {
541 void* child_stack[1024];
542 return clone(start_routine, &child_stack[1024], SIGCHLD, NULL);
543 }
544
GetPidCachingCloneStartRoutine(void *)545 static int GetPidCachingCloneStartRoutine(void*) {
546 AssertGetPidCorrect();
547 return 123;
548 }
549
TEST(UNISTD_TEST,getpid_caching_and_clone)550 TEST(UNISTD_TEST, getpid_caching_and_clone) {
551 pid_t parent_pid = getpid();
552 ASSERT_EQ(syscall(__NR_getpid), parent_pid);
553
554 int clone_result = CloneStartRoutine(GetPidCachingCloneStartRoutine);
555 ASSERT_NE(clone_result, -1);
556
557 ASSERT_EQ(parent_pid, getpid());
558
559 AssertChildExited(clone_result, 123);
560 }
561
GetTidCachingCloneStartRoutine(void *)562 static int GetTidCachingCloneStartRoutine(void*) {
563 AssertGetTidCorrect();
564 return 123;
565 }
566
TEST(UNISTD_TEST,gettid_caching_and_clone)567 TEST(UNISTD_TEST, gettid_caching_and_clone) {
568 pid_t parent_tid = GetTidForTest();
569 ASSERT_EQ(syscall(__NR_gettid), parent_tid);
570
571 int clone_result = CloneStartRoutine(GetTidCachingCloneStartRoutine);
572 ASSERT_NE(clone_result, -1);
573
574 ASSERT_EQ(parent_tid, GetTidForTest());
575
576 AssertChildExited(clone_result, 123);
577 }
578
CloneChildExit(void *)579 static int CloneChildExit(void*) {
580 AssertGetPidCorrect();
581 AssertGetTidCorrect();
582 exit(33);
583 }
584
TEST(UNISTD_TEST,clone_fn_and_exit)585 TEST(UNISTD_TEST, clone_fn_and_exit) {
586 int clone_result = CloneStartRoutine(CloneChildExit);
587 ASSERT_NE(-1, clone_result);
588
589 AssertGetPidCorrect();
590 AssertGetTidCorrect();
591
592 AssertChildExited(clone_result, 33);
593 }
594
GetPidCachingPthreadStartRoutine(void *)595 static void* GetPidCachingPthreadStartRoutine(void*) {
596 AssertGetPidCorrect();
597 return NULL;
598 }
599
TEST(UNISTD_TEST,getpid_caching_and_pthread_create)600 TEST(UNISTD_TEST, getpid_caching_and_pthread_create) {
601 pid_t parent_pid = getpid();
602
603 pthread_t t;
604 ASSERT_EQ(0, pthread_create(&t, NULL, GetPidCachingPthreadStartRoutine, NULL));
605
606 ASSERT_EQ(parent_pid, getpid());
607
608 void* result;
609 ASSERT_EQ(0, pthread_join(t, &result));
610 ASSERT_EQ(NULL, result);
611 }
612
GetTidCachingPthreadStartRoutine(void *)613 static void* GetTidCachingPthreadStartRoutine(void*) {
614 AssertGetTidCorrect();
615 uint64_t tid = GetTidForTest();
616 return reinterpret_cast<void*>(tid);
617 }
618
TEST(UNISTD_TEST,gettid_caching_and_pthread_create)619 TEST(UNISTD_TEST, gettid_caching_and_pthread_create) {
620 pid_t parent_tid = GetTidForTest();
621
622 pthread_t t;
623 ASSERT_EQ(0, pthread_create(&t, NULL, GetTidCachingPthreadStartRoutine, &parent_tid));
624
625 ASSERT_EQ(parent_tid, GetTidForTest());
626
627 void* result;
628 ASSERT_EQ(0, pthread_join(t, &result));
629 ASSERT_NE(static_cast<uint64_t>(parent_tid), reinterpret_cast<uint64_t>(result));
630 }
631
632 class UNISTD_DEATHTEST : public BionicDeathTest {};
633
TEST_F(UNISTD_DEATHTEST,abort)634 TEST_F(UNISTD_DEATHTEST, abort) {
635 ASSERT_EXIT(abort(), testing::KilledBySignal(SIGABRT), "");
636 }
637
TEST(UNISTD_TEST,sethostname)638 TEST(UNISTD_TEST, sethostname) {
639 // The permissions check happens before the argument check, so this will
640 // fail for a different reason if you're running as root than if you're
641 // not, but it'll fail either way. Checking that we have the symbol is about
642 // all we can do for sethostname(2).
643 ASSERT_EQ(-1, sethostname("", -1));
644 }
645
TEST(UNISTD_TEST,gethostname)646 TEST(UNISTD_TEST, gethostname) {
647 char hostname[HOST_NAME_MAX + 1];
648 memset(hostname, 0, sizeof(hostname));
649
650 // Can we get the hostname with a big buffer?
651 ASSERT_EQ(0, gethostname(hostname, HOST_NAME_MAX));
652
653 // Can we get the hostname with a right-sized buffer?
654 errno = 0;
655 ASSERT_EQ(0, gethostname(hostname, strlen(hostname) + 1));
656
657 // Does uname(2) agree?
658 utsname buf;
659 ASSERT_EQ(0, uname(&buf));
660 ASSERT_EQ(0, strncmp(hostname, buf.nodename, SYS_NMLN));
661 ASSERT_GT(strlen(hostname), 0U);
662
663 // Do we correctly detect truncation?
664 errno = 0;
665 ASSERT_EQ(-1, gethostname(hostname, strlen(hostname)));
666 ASSERT_EQ(ENAMETOOLONG, errno);
667 }
668
TEST(UNISTD_TEST,pathconf_fpathconf)669 TEST(UNISTD_TEST, pathconf_fpathconf) {
670 TemporaryFile tf;
671 long rc = 0L;
672 // As a file system's block size is always power of 2, the configure values
673 // for ALLOC and XFER should be power of 2 as well.
674 rc = pathconf(tf.filename, _PC_ALLOC_SIZE_MIN);
675 ASSERT_TRUE(rc > 0 && powerof2(rc));
676 rc = pathconf(tf.filename, _PC_REC_MIN_XFER_SIZE);
677 ASSERT_TRUE(rc > 0 && powerof2(rc));
678 rc = pathconf(tf.filename, _PC_REC_XFER_ALIGN);
679 ASSERT_TRUE(rc > 0 && powerof2(rc));
680
681 rc = fpathconf(tf.fd, _PC_ALLOC_SIZE_MIN);
682 ASSERT_TRUE(rc > 0 && powerof2(rc));
683 rc = fpathconf(tf.fd, _PC_REC_MIN_XFER_SIZE);
684 ASSERT_TRUE(rc > 0 && powerof2(rc));
685 rc = fpathconf(tf.fd, _PC_REC_XFER_ALIGN);
686 ASSERT_TRUE(rc > 0 && powerof2(rc));
687 }
688
TEST(UNISTD_TEST,_POSIX_constants)689 TEST(UNISTD_TEST, _POSIX_constants) {
690 // Make a tight verification of _POSIX_* / _POSIX2_* / _XOPEN_* macros, to prevent change by mistake.
691 // Verify according to POSIX.1-2008.
692 EXPECT_EQ(200809L, _POSIX_VERSION);
693
694 EXPECT_EQ(2, _POSIX_AIO_LISTIO_MAX);
695 EXPECT_EQ(1, _POSIX_AIO_MAX);
696 EXPECT_EQ(4096, _POSIX_ARG_MAX);
697 EXPECT_EQ(25, _POSIX_CHILD_MAX);
698 EXPECT_EQ(20000000, _POSIX_CLOCKRES_MIN);
699 EXPECT_EQ(32, _POSIX_DELAYTIMER_MAX);
700 EXPECT_EQ(255, _POSIX_HOST_NAME_MAX);
701 EXPECT_EQ(8, _POSIX_LINK_MAX);
702 EXPECT_EQ(9, _POSIX_LOGIN_NAME_MAX);
703 EXPECT_EQ(255, _POSIX_MAX_CANON);
704 EXPECT_EQ(255, _POSIX_MAX_INPUT);
705 EXPECT_EQ(8, _POSIX_MQ_OPEN_MAX);
706 EXPECT_EQ(32, _POSIX_MQ_PRIO_MAX);
707 EXPECT_EQ(14, _POSIX_NAME_MAX);
708 EXPECT_EQ(8, _POSIX_NGROUPS_MAX);
709 EXPECT_EQ(20, _POSIX_OPEN_MAX);
710 EXPECT_EQ(256, _POSIX_PATH_MAX);
711 EXPECT_EQ(512, _POSIX_PIPE_BUF);
712 EXPECT_EQ(255, _POSIX_RE_DUP_MAX);
713 EXPECT_EQ(8, _POSIX_RTSIG_MAX);
714 EXPECT_EQ(256, _POSIX_SEM_NSEMS_MAX);
715 EXPECT_EQ(32767, _POSIX_SEM_VALUE_MAX);
716 EXPECT_EQ(32, _POSIX_SIGQUEUE_MAX);
717 EXPECT_EQ(32767, _POSIX_SSIZE_MAX);
718 EXPECT_EQ(8, _POSIX_STREAM_MAX);
719 #if !defined(__GLIBC__)
720 EXPECT_EQ(4, _POSIX_SS_REPL_MAX);
721 #endif
722 EXPECT_EQ(255, _POSIX_SYMLINK_MAX);
723 EXPECT_EQ(8, _POSIX_SYMLOOP_MAX);
724 EXPECT_EQ(4, _POSIX_THREAD_DESTRUCTOR_ITERATIONS);
725 EXPECT_EQ(128, _POSIX_THREAD_KEYS_MAX);
726 EXPECT_EQ(64, _POSIX_THREAD_THREADS_MAX);
727 EXPECT_EQ(32, _POSIX_TIMER_MAX);
728 #if !defined(__GLIBC__)
729 EXPECT_EQ(30, _POSIX_TRACE_EVENT_NAME_MAX);
730 EXPECT_EQ(8, _POSIX_TRACE_NAME_MAX);
731 EXPECT_EQ(8, _POSIX_TRACE_SYS_MAX);
732 EXPECT_EQ(32, _POSIX_TRACE_USER_EVENT_MAX);
733 #endif
734 EXPECT_EQ(9, _POSIX_TTY_NAME_MAX);
735 EXPECT_EQ(6, _POSIX_TZNAME_MAX);
736 EXPECT_EQ(99, _POSIX2_BC_BASE_MAX);
737 EXPECT_EQ(2048, _POSIX2_BC_DIM_MAX);
738 EXPECT_EQ(99, _POSIX2_BC_SCALE_MAX);
739 EXPECT_EQ(1000, _POSIX2_BC_STRING_MAX);
740 EXPECT_EQ(14, _POSIX2_CHARCLASS_NAME_MAX);
741 EXPECT_EQ(2, _POSIX2_COLL_WEIGHTS_MAX);
742 EXPECT_EQ(32, _POSIX2_EXPR_NEST_MAX);
743 EXPECT_EQ(2048, _POSIX2_LINE_MAX);
744 EXPECT_EQ(255, _POSIX2_RE_DUP_MAX);
745
746 EXPECT_EQ(16, _XOPEN_IOV_MAX);
747 #if !defined(__GLIBC__)
748 EXPECT_EQ(255, _XOPEN_NAME_MAX);
749 EXPECT_EQ(1024, _XOPEN_PATH_MAX);
750 #endif
751 }
752
TEST(UNISTD_TEST,_POSIX_options)753 TEST(UNISTD_TEST, _POSIX_options) {
754 EXPECT_EQ(_POSIX_VERSION, _POSIX_ADVISORY_INFO);
755 EXPECT_GT(_POSIX_BARRIERS, 0);
756 EXPECT_GT(_POSIX_SPIN_LOCKS, 0);
757 EXPECT_NE(_POSIX_CHOWN_RESTRICTED, -1);
758 EXPECT_EQ(_POSIX_VERSION, _POSIX_CLOCK_SELECTION);
759 #if !defined(__GLIBC__) // glibc supports ancient kernels.
760 EXPECT_EQ(_POSIX_VERSION, _POSIX_CPUTIME);
761 #endif
762 EXPECT_EQ(_POSIX_VERSION, _POSIX_FSYNC);
763 EXPECT_EQ(_POSIX_VERSION, _POSIX_IPV6);
764 EXPECT_GT(_POSIX_JOB_CONTROL, 0);
765 EXPECT_EQ(_POSIX_VERSION, _POSIX_MAPPED_FILES);
766 EXPECT_EQ(_POSIX_VERSION, _POSIX_MEMLOCK);
767 EXPECT_EQ(_POSIX_VERSION, _POSIX_MEMLOCK_RANGE);
768 EXPECT_EQ(_POSIX_VERSION, _POSIX_MEMORY_PROTECTION);
769 #if !defined(__GLIBC__) // glibc supports ancient kernels.
770 EXPECT_EQ(_POSIX_VERSION, _POSIX_MONOTONIC_CLOCK);
771 #endif
772 EXPECT_GT(_POSIX_NO_TRUNC, 0);
773 EXPECT_EQ(_POSIX_VERSION, _POSIX_PRIORITY_SCHEDULING);
774 EXPECT_EQ(_POSIX_VERSION, _POSIX_RAW_SOCKETS);
775 EXPECT_EQ(_POSIX_VERSION, _POSIX_READER_WRITER_LOCKS);
776 EXPECT_EQ(_POSIX_VERSION, _POSIX_REALTIME_SIGNALS);
777 EXPECT_GT(_POSIX_REGEXP, 0);
778 EXPECT_GT(_POSIX_SAVED_IDS, 0);
779 EXPECT_EQ(_POSIX_VERSION, _POSIX_SEMAPHORES);
780 EXPECT_GT(_POSIX_SHELL, 0);
781 EXPECT_EQ(_POSIX_VERSION, _POSIX_SPAWN);
782 EXPECT_EQ(-1, _POSIX_SPORADIC_SERVER);
783 EXPECT_EQ(_POSIX_VERSION, _POSIX_SYNCHRONIZED_IO);
784 EXPECT_EQ(_POSIX_VERSION, _POSIX_THREADS);
785 EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_ATTR_STACKADDR);
786 EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_ATTR_STACKSIZE);
787 #if !defined(__GLIBC__) // glibc supports ancient kernels.
788 EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_CPUTIME);
789 #endif
790 EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_PRIORITY_SCHEDULING);
791 EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_PROCESS_SHARED);
792 EXPECT_EQ(-1, _POSIX_THREAD_ROBUST_PRIO_PROTECT);
793 EXPECT_EQ(_POSIX_VERSION, _POSIX_THREAD_SAFE_FUNCTIONS);
794 EXPECT_EQ(-1, _POSIX_THREAD_SPORADIC_SERVER);
795 EXPECT_EQ(_POSIX_VERSION, _POSIX_TIMEOUTS);
796 EXPECT_EQ(_POSIX_VERSION, _POSIX_TIMERS);
797 EXPECT_EQ(-1, _POSIX_TRACE);
798 EXPECT_EQ(-1, _POSIX_TRACE_EVENT_FILTER);
799 EXPECT_EQ(-1, _POSIX_TRACE_INHERIT);
800 EXPECT_EQ(-1, _POSIX_TRACE_LOG);
801 EXPECT_EQ(-1, _POSIX_TYPED_MEMORY_OBJECTS);
802 EXPECT_NE(-1, _POSIX_VDISABLE);
803
804 EXPECT_EQ(_POSIX_VERSION, _POSIX2_VERSION);
805 EXPECT_EQ(_POSIX_VERSION, _POSIX2_C_BIND);
806 EXPECT_EQ(_POSIX_VERSION, _POSIX2_CHAR_TERM);
807
808 EXPECT_EQ(700, _XOPEN_VERSION);
809 EXPECT_EQ(1, _XOPEN_ENH_I18N);
810 EXPECT_EQ(1, _XOPEN_REALTIME);
811 EXPECT_EQ(1, _XOPEN_REALTIME_THREADS);
812 EXPECT_EQ(1, _XOPEN_SHM);
813 EXPECT_EQ(1, _XOPEN_UNIX);
814
815 #if defined(__BIONIC__)
816 // These tests only pass on bionic, as bionic and glibc has different support on these macros.
817 // Macros like _POSIX_ASYNCHRONOUS_IO are not supported on bionic yet.
818 EXPECT_EQ(-1, _POSIX_ASYNCHRONOUS_IO);
819 EXPECT_EQ(-1, _POSIX_MESSAGE_PASSING);
820 EXPECT_EQ(-1, _POSIX_PRIORITIZED_IO);
821 EXPECT_EQ(-1, _POSIX_SHARED_MEMORY_OBJECTS);
822 EXPECT_EQ(-1, _POSIX_THREAD_PRIO_INHERIT);
823 EXPECT_EQ(-1, _POSIX_THREAD_PRIO_PROTECT);
824 EXPECT_EQ(-1, _POSIX_THREAD_ROBUST_PRIO_INHERIT);
825
826 EXPECT_EQ(-1, _POSIX2_C_DEV);
827 EXPECT_EQ(-1, _POSIX2_FORT_DEV);
828 EXPECT_EQ(-1, _POSIX2_FORT_RUN);
829 EXPECT_EQ(-1, _POSIX2_LOCALEDEF);
830 EXPECT_EQ(-1, _POSIX2_SW_DEV);
831 EXPECT_EQ(-1, _POSIX2_UPE);
832
833 EXPECT_EQ(-1, _XOPEN_CRYPT);
834 EXPECT_EQ(-1, _XOPEN_LEGACY);
835 EXPECT_EQ(-1, _XOPEN_STREAMS);
836 #endif // defined(__BIONIC__)
837 }
838
839 #define VERIFY_SYSCONF_UNSUPPORTED(name) VerifySysconf(name, #name, [](long v){return v == -1;})
840
841 // sysconf() means unlimited when it returns -1 with errno unchanged.
842 #define VERIFY_SYSCONF_POSITIVE(name) \
843 VerifySysconf(name, #name, [](long v){return (v > 0 || v == -1);})
844
845 #define VERIFY_SYSCONF_POSIX_VERSION(name) \
846 VerifySysconf(name, #name, [](long v){return v == _POSIX_VERSION;})
847
VerifySysconf(int option,const char * option_name,bool (* verify)(long))848 static void VerifySysconf(int option, const char *option_name, bool (*verify)(long)) {
849 errno = 0;
850 long ret = sysconf(option);
851 EXPECT_TRUE(0 == errno && verify(ret)) << "name = " << option_name << ", ret = "
852 << ret <<", Error Message: " << strerror(errno);
853 }
854
TEST(UNISTD_TEST,sysconf)855 TEST(UNISTD_TEST, sysconf) {
856 VERIFY_SYSCONF_POSIX_VERSION(_SC_ADVISORY_INFO);
857 VERIFY_SYSCONF_POSITIVE(_SC_ARG_MAX);
858 VERIFY_SYSCONF_POSIX_VERSION(_SC_BARRIERS);
859 VERIFY_SYSCONF_POSITIVE(_SC_BC_BASE_MAX);
860 VERIFY_SYSCONF_POSITIVE(_SC_BC_DIM_MAX);
861 VERIFY_SYSCONF_POSITIVE(_SC_BC_SCALE_MAX);
862 VERIFY_SYSCONF_POSITIVE(_SC_CHILD_MAX);
863 VERIFY_SYSCONF_POSITIVE(_SC_CLK_TCK);
864 VERIFY_SYSCONF_POSITIVE(_SC_COLL_WEIGHTS_MAX);
865 VERIFY_SYSCONF_POSIX_VERSION(_SC_CPUTIME);
866 VERIFY_SYSCONF_POSITIVE(_SC_EXPR_NEST_MAX);
867 VERIFY_SYSCONF_POSITIVE(_SC_LINE_MAX);
868 VERIFY_SYSCONF_POSITIVE(_SC_NGROUPS_MAX);
869 VERIFY_SYSCONF_POSITIVE(_SC_OPEN_MAX);
870 VERIFY_SYSCONF_POSITIVE(_SC_PASS_MAX);
871 VERIFY_SYSCONF_POSIX_VERSION(_SC_2_C_BIND);
872 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_FORT_DEV);
873 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_FORT_RUN);
874 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_UPE);
875 VERIFY_SYSCONF_POSIX_VERSION(_SC_2_VERSION);
876 VERIFY_SYSCONF_POSITIVE(_SC_JOB_CONTROL);
877 VERIFY_SYSCONF_POSITIVE(_SC_SAVED_IDS);
878 VERIFY_SYSCONF_POSIX_VERSION(_SC_VERSION);
879 VERIFY_SYSCONF_POSITIVE(_SC_RE_DUP_MAX);
880 VERIFY_SYSCONF_POSITIVE(_SC_STREAM_MAX);
881 VERIFY_SYSCONF_POSITIVE(_SC_TZNAME_MAX);
882 VerifySysconf(_SC_XOPEN_VERSION, "_SC_XOPEN_VERSION", [](long v){return v == _XOPEN_VERSION;});
883 VERIFY_SYSCONF_POSITIVE(_SC_ATEXIT_MAX);
884 VERIFY_SYSCONF_POSITIVE(_SC_IOV_MAX);
885 VERIFY_SYSCONF_POSITIVE(_SC_PAGESIZE);
886 VERIFY_SYSCONF_POSITIVE(_SC_PAGE_SIZE);
887 VerifySysconf(_SC_PAGE_SIZE, "_SC_PAGE_SIZE",
888 [](long v){return v == sysconf(_SC_PAGESIZE) && v == getpagesize();});
889 VERIFY_SYSCONF_POSITIVE(_SC_XOPEN_UNIX);
890 VERIFY_SYSCONF_POSITIVE(_SC_AIO_LISTIO_MAX);
891 VERIFY_SYSCONF_POSITIVE(_SC_AIO_MAX);
892 VerifySysconf(_SC_AIO_PRIO_DELTA_MAX, "_SC_AIO_PRIO_DELTA_MAX", [](long v){return v >= 0;});
893 VERIFY_SYSCONF_POSITIVE(_SC_DELAYTIMER_MAX);
894 VERIFY_SYSCONF_POSITIVE(_SC_MQ_OPEN_MAX);
895 VERIFY_SYSCONF_POSITIVE(_SC_MQ_PRIO_MAX);
896 VERIFY_SYSCONF_POSITIVE(_SC_RTSIG_MAX);
897 VERIFY_SYSCONF_POSITIVE(_SC_SEM_NSEMS_MAX);
898 VERIFY_SYSCONF_POSITIVE(_SC_SEM_VALUE_MAX);
899 VERIFY_SYSCONF_POSIX_VERSION(_SC_SPIN_LOCKS);
900 VERIFY_SYSCONF_POSITIVE(_SC_TIMER_MAX);
901 VERIFY_SYSCONF_POSIX_VERSION(_SC_FSYNC);
902 VERIFY_SYSCONF_POSIX_VERSION(_SC_MAPPED_FILES);
903 VERIFY_SYSCONF_POSIX_VERSION(_SC_MEMLOCK);
904 VERIFY_SYSCONF_POSIX_VERSION(_SC_MEMLOCK_RANGE);
905 VERIFY_SYSCONF_POSIX_VERSION(_SC_MEMORY_PROTECTION);
906 VERIFY_SYSCONF_POSIX_VERSION(_SC_PRIORITY_SCHEDULING);
907 VERIFY_SYSCONF_POSIX_VERSION(_SC_REALTIME_SIGNALS);
908 VERIFY_SYSCONF_POSIX_VERSION(_SC_SEMAPHORES);
909 VERIFY_SYSCONF_POSIX_VERSION(_SC_SYNCHRONIZED_IO);
910 VERIFY_SYSCONF_POSIX_VERSION(_SC_TIMERS);
911 VERIFY_SYSCONF_POSITIVE(_SC_GETGR_R_SIZE_MAX);
912 VERIFY_SYSCONF_POSITIVE(_SC_GETPW_R_SIZE_MAX);
913 VERIFY_SYSCONF_POSITIVE(_SC_LOGIN_NAME_MAX);
914 VERIFY_SYSCONF_POSITIVE(_SC_THREAD_DESTRUCTOR_ITERATIONS);
915 VERIFY_SYSCONF_POSITIVE(_SC_THREAD_KEYS_MAX);
916 VERIFY_SYSCONF_POSITIVE(_SC_THREAD_STACK_MIN);
917 VERIFY_SYSCONF_POSITIVE(_SC_THREAD_THREADS_MAX);
918 VERIFY_SYSCONF_POSITIVE(_SC_TTY_NAME_MAX);
919 VERIFY_SYSCONF_POSIX_VERSION(_SC_THREADS);
920 VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_ATTR_STACKADDR);
921 VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_ATTR_STACKSIZE);
922 VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_PRIORITY_SCHEDULING);
923 VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_PRIO_INHERIT);
924 VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_PRIO_PROTECT);
925 VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_SAFE_FUNCTIONS);
926 VERIFY_SYSCONF_POSITIVE(_SC_NPROCESSORS_CONF);
927 VERIFY_SYSCONF_POSITIVE(_SC_NPROCESSORS_ONLN);
928 VERIFY_SYSCONF_POSITIVE(_SC_PHYS_PAGES);
929 VERIFY_SYSCONF_POSITIVE(_SC_AVPHYS_PAGES);
930 VERIFY_SYSCONF_POSIX_VERSION(_SC_MONOTONIC_CLOCK);
931 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS);
932 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS_ACCOUNTING);
933 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS_CHECKPOINT);
934 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS_LOCATE);
935 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS_MESSAGE);
936 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_PBS_TRACK);
937 VERIFY_SYSCONF_POSIX_VERSION(_SC_CLOCK_SELECTION);
938 VERIFY_SYSCONF_POSITIVE(_SC_HOST_NAME_MAX);
939 VERIFY_SYSCONF_POSIX_VERSION(_SC_IPV6);
940 VERIFY_SYSCONF_POSIX_VERSION(_SC_RAW_SOCKETS);
941 VERIFY_SYSCONF_POSIX_VERSION(_SC_READER_WRITER_LOCKS);
942 VERIFY_SYSCONF_POSITIVE(_SC_REGEXP);
943 VERIFY_SYSCONF_POSITIVE(_SC_SHELL);
944 VERIFY_SYSCONF_POSIX_VERSION(_SC_SPAWN);
945 VERIFY_SYSCONF_UNSUPPORTED(_SC_SPORADIC_SERVER);
946 VERIFY_SYSCONF_POSITIVE(_SC_SYMLOOP_MAX);
947 VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_CPUTIME);
948 VERIFY_SYSCONF_POSIX_VERSION(_SC_THREAD_PROCESS_SHARED);
949 VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_SPORADIC_SERVER);
950 VERIFY_SYSCONF_POSIX_VERSION(_SC_TIMEOUTS);
951 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE);
952 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_EVENT_FILTER);
953 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_EVENT_NAME_MAX);
954 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_INHERIT);
955 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_LOG);
956 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_NAME_MAX);
957 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_SYS_MAX);
958 VERIFY_SYSCONF_UNSUPPORTED(_SC_TRACE_USER_EVENT_MAX);
959 VERIFY_SYSCONF_UNSUPPORTED(_SC_TYPED_MEMORY_OBJECTS);
960 VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_STREAMS);
961
962 #if defined(__LP64__)
963 VERIFY_SYSCONF_UNSUPPORTED(_SC_V7_ILP32_OFF32);
964 VERIFY_SYSCONF_UNSUPPORTED(_SC_V7_ILP32_OFFBIG);
965 VERIFY_SYSCONF_POSITIVE(_SC_V7_LP64_OFF64);
966 VERIFY_SYSCONF_POSITIVE(_SC_V7_LPBIG_OFFBIG);
967 #else
968 VERIFY_SYSCONF_POSITIVE(_SC_V7_ILP32_OFF32);
969 #if defined(__BIONIC__)
970 // bionic does not support 64 bits off_t type on 32bit machine.
971 VERIFY_SYSCONF_UNSUPPORTED(_SC_V7_ILP32_OFFBIG);
972 #endif
973 VERIFY_SYSCONF_UNSUPPORTED(_SC_V7_LP64_OFF64);
974 VERIFY_SYSCONF_UNSUPPORTED(_SC_V7_LPBIG_OFFBIG);
975 #endif
976
977 #if defined(__BIONIC__)
978 // Tests can only run on bionic, as bionic and glibc have different support for these options.
979 // Below options are not supported on bionic yet.
980 VERIFY_SYSCONF_UNSUPPORTED(_SC_ASYNCHRONOUS_IO);
981 VERIFY_SYSCONF_UNSUPPORTED(_SC_MESSAGE_PASSING);
982 VERIFY_SYSCONF_UNSUPPORTED(_SC_PRIORITIZED_IO);
983 VERIFY_SYSCONF_UNSUPPORTED(_SC_SHARED_MEMORY_OBJECTS);
984 VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_ROBUST_PRIO_INHERIT);
985 VERIFY_SYSCONF_UNSUPPORTED(_SC_THREAD_ROBUST_PRIO_PROTECT);
986
987 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_C_DEV);
988 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_LOCALEDEF);
989 VERIFY_SYSCONF_UNSUPPORTED(_SC_2_SW_DEV);
990
991 VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_CRYPT);
992 VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_LEGACY);
993 VERIFY_SYSCONF_UNSUPPORTED(_SC_XOPEN_UUCP);
994 #endif // defined(__BIONIC__)
995 }
996
TEST(UNISTD_TEST,get_cpu_count_from_string)997 TEST(UNISTD_TEST, get_cpu_count_from_string) {
998 ASSERT_EQ(0, GetCpuCountFromString(" "));
999 ASSERT_EQ(1, GetCpuCountFromString("0"));
1000 ASSERT_EQ(40, GetCpuCountFromString("0-39"));
1001 ASSERT_EQ(4, GetCpuCountFromString("0, 1-2, 4\n"));
1002 }
1003
TEST(UNISTD_TEST,sysconf_SC_NPROCESSORS_ONLN)1004 TEST(UNISTD_TEST, sysconf_SC_NPROCESSORS_ONLN) {
1005 std::string line;
1006 ASSERT_TRUE(android::base::ReadFileToString("/sys/devices/system/cpu/online", &line));
1007 long online_cpus = 0;
1008 for (const std::string& s : android::base::Split(line, ",")) {
1009 std::vector<std::string> numbers = android::base::Split(s, "-");
1010 if (numbers.size() == 1u) {
1011 online_cpus++;
1012 } else {
1013 online_cpus += atoi(numbers[1].c_str()) - atoi(numbers[0].c_str()) + 1;
1014 }
1015 }
1016 ASSERT_EQ(online_cpus, sysconf(_SC_NPROCESSORS_ONLN));
1017 }
1018
TEST(UNISTD_TEST,sysconf_SC_ARG_MAX)1019 TEST(UNISTD_TEST, sysconf_SC_ARG_MAX) {
1020 // https://lkml.org/lkml/2017/11/15/813.
1021 #if !defined(ARG_MAX)
1022 #define ARG_MAX 131072
1023 #endif
1024 ASSERT_EQ(ARG_MAX, sysconf(_SC_ARG_MAX));
1025 }
1026
TEST(UNISTD_TEST,dup2_same)1027 TEST(UNISTD_TEST, dup2_same) {
1028 // POSIX says of dup2:
1029 // If fildes2 is already a valid open file descriptor ...
1030 // [and] fildes is equal to fildes2 ... dup2() shall return
1031 // fildes2 without closing it.
1032 // This isn't true of dup3(2), so we need to manually implement that.
1033
1034 // Equal and valid.
1035 int fd = open("/proc/version", O_RDONLY);
1036 ASSERT_TRUE(fd != -1);
1037 ASSERT_EQ(fd, dup2(fd, fd));
1038 ASSERT_EQ(0, close(fd)); // Check that dup2 didn't close fd.
1039
1040 // Equal, but invalid.
1041 errno = 0;
1042 ASSERT_EQ(-1, dup2(fd, fd));
1043 ASSERT_EQ(EBADF, errno);
1044 }
1045
TEST(UNISTD_TEST,dup3)1046 TEST(UNISTD_TEST, dup3) {
1047 int fd = open("/proc/version", O_RDONLY);
1048 ASSERT_EQ(666, dup3(fd, 666, 0));
1049 AssertCloseOnExec(666, false);
1050 close(666);
1051 ASSERT_EQ(667, dup3(fd, 667, O_CLOEXEC));
1052 AssertCloseOnExec(667, true);
1053 close(667);
1054 close(fd);
1055 }
1056
TEST(UNISTD_TEST,lockf_smoke)1057 TEST(UNISTD_TEST, lockf_smoke) {
1058 constexpr off64_t file_size = 32*1024LL;
1059
1060 TemporaryFile tf;
1061 ASSERT_EQ(0, ftruncate(tf.fd, file_size));
1062
1063 // Lock everything.
1064 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1065 ASSERT_EQ(0, lockf64(tf.fd, F_LOCK, file_size));
1066
1067 // Try-lock everything, this should succeed too.
1068 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1069 ASSERT_EQ(0, lockf64(tf.fd, F_TLOCK, file_size));
1070
1071 // Check status.
1072 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1073 ASSERT_EQ(0, lockf64(tf.fd, F_TEST, file_size));
1074
1075 // Unlock file.
1076 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1077 ASSERT_EQ(0, lockf64(tf.fd, F_ULOCK, file_size));
1078 }
1079
TEST(UNISTD_TEST,lockf_zero)1080 TEST(UNISTD_TEST, lockf_zero) {
1081 constexpr off64_t file_size = 32*1024LL;
1082
1083 TemporaryFile tf;
1084 ASSERT_EQ(0, ftruncate(tf.fd, file_size));
1085
1086 // Lock everything by specifying a size of 0 (meaning "to the end, even if it changes").
1087 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1088 ASSERT_EQ(0, lockf64(tf.fd, F_LOCK, 0));
1089
1090 // Check that it's locked.
1091 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1092 ASSERT_EQ(0, lockf64(tf.fd, F_TEST, file_size));
1093
1094 // Move the end.
1095 ASSERT_EQ(0, ftruncate(tf.fd, 2*file_size));
1096
1097 // Check that the new section is locked too.
1098 ASSERT_EQ(file_size, lseek64(tf.fd, file_size, SEEK_SET));
1099 ASSERT_EQ(0, lockf64(tf.fd, F_TEST, 2*file_size));
1100 }
1101
TEST(UNISTD_TEST,lockf_negative)1102 TEST(UNISTD_TEST, lockf_negative) {
1103 constexpr off64_t file_size = 32*1024LL;
1104
1105 TemporaryFile tf;
1106 ASSERT_EQ(0, ftruncate(tf.fd, file_size));
1107
1108 // Lock everything, but specifying the range in reverse.
1109 ASSERT_EQ(file_size, lseek64(tf.fd, file_size, SEEK_SET));
1110 ASSERT_EQ(0, lockf64(tf.fd, F_LOCK, -file_size));
1111
1112 // Check that it's locked.
1113 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1114 ASSERT_EQ(0, lockf64(tf.fd, F_TEST, file_size));
1115 }
1116
TEST(UNISTD_TEST,lockf_with_child)1117 TEST(UNISTD_TEST, lockf_with_child) {
1118 constexpr off64_t file_size = 32*1024LL;
1119
1120 TemporaryFile tf;
1121 ASSERT_EQ(0, ftruncate(tf.fd, file_size));
1122
1123 // Lock everything.
1124 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1125 ASSERT_EQ(0, lockf64(tf.fd, F_LOCK, file_size));
1126
1127 // Fork a child process
1128 pid_t pid = fork();
1129 ASSERT_NE(-1, pid);
1130 if (pid == 0) {
1131 // Check that the child cannot lock the file.
1132 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1133 ASSERT_EQ(-1, lockf64(tf.fd, F_TLOCK, file_size));
1134 ASSERT_EQ(EAGAIN, errno);
1135 // Check also that it reports itself as locked.
1136 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1137 ASSERT_EQ(-1, lockf64(tf.fd, F_TEST, file_size));
1138 ASSERT_EQ(EACCES, errno);
1139 _exit(0);
1140 }
1141 AssertChildExited(pid, 0);
1142 }
1143
TEST(UNISTD_TEST,lockf_partial_with_child)1144 TEST(UNISTD_TEST, lockf_partial_with_child) {
1145 constexpr off64_t file_size = 32*1024LL;
1146
1147 TemporaryFile tf;
1148 ASSERT_EQ(0, ftruncate(tf.fd, file_size));
1149
1150 // Lock the first half of the file.
1151 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1152 ASSERT_EQ(0, lockf64(tf.fd, F_LOCK, file_size/2));
1153
1154 // Fork a child process.
1155 pid_t pid = fork();
1156 ASSERT_NE(-1, pid);
1157 if (pid == 0) {
1158 // Check that the child can lock the other half.
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));
1161 // Check that the child cannot lock the first half.
1162 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1163 ASSERT_EQ(-1, lockf64(tf.fd, F_TEST, file_size/2));
1164 ASSERT_EQ(EACCES, errno);
1165 // Check also that it reports itself as locked.
1166 ASSERT_EQ(0, lseek64(tf.fd, 0, SEEK_SET));
1167 ASSERT_EQ(-1, lockf64(tf.fd, F_TEST, file_size/2));
1168 ASSERT_EQ(EACCES, errno);
1169 _exit(0);
1170 }
1171 AssertChildExited(pid, 0);
1172
1173 // The second half was locked by the child, but the lock disappeared
1174 // when the process exited, so check it can be locked now.
1175 ASSERT_EQ(file_size/2, lseek64(tf.fd, file_size/2, SEEK_SET));
1176 ASSERT_EQ(0, lockf64(tf.fd, F_TLOCK, file_size/2));
1177 }
1178
TEST(UNISTD_TEST,getdomainname)1179 TEST(UNISTD_TEST, getdomainname) {
1180 struct utsname u;
1181 ASSERT_EQ(0, uname(&u));
1182
1183 char buf[sizeof(u.domainname)];
1184 ASSERT_EQ(0, getdomainname(buf, sizeof(buf)));
1185 EXPECT_STREQ(u.domainname, buf);
1186
1187 #if defined(__BIONIC__)
1188 // bionic and glibc have different behaviors when len is too small
1189 ASSERT_EQ(-1, getdomainname(buf, strlen(u.domainname)));
1190 EXPECT_EQ(EINVAL, errno);
1191 #endif
1192 }
1193
TEST(UNISTD_TEST,setdomainname)1194 TEST(UNISTD_TEST, setdomainname) {
1195 __user_cap_header_struct header;
1196 memset(&header, 0, sizeof(header));
1197 header.version = _LINUX_CAPABILITY_VERSION_3;
1198
1199 __user_cap_data_struct old_caps[_LINUX_CAPABILITY_U32S_3];
1200 ASSERT_EQ(0, capget(&header, &old_caps[0]));
1201
1202 auto admin_idx = CAP_TO_INDEX(CAP_SYS_ADMIN);
1203 auto admin_mask = CAP_TO_MASK(CAP_SYS_ADMIN);
1204 bool has_admin = old_caps[admin_idx].effective & admin_mask;
1205 if (has_admin) {
1206 __user_cap_data_struct new_caps[_LINUX_CAPABILITY_U32S_3];
1207 memcpy(new_caps, old_caps, sizeof(new_caps));
1208 new_caps[admin_idx].effective &= ~admin_mask;
1209
1210 ASSERT_EQ(0, capset(&header, &new_caps[0])) << "failed to drop admin privileges";
1211 }
1212
1213 const char* name = "newdomainname";
1214 ASSERT_EQ(-1, setdomainname(name, strlen(name)));
1215 ASSERT_EQ(EPERM, errno);
1216
1217 if (has_admin) {
1218 ASSERT_EQ(0, capset(&header, &old_caps[0])) << "failed to restore admin privileges";
1219 }
1220 }
1221
TEST(UNISTD_TEST,execve_failure)1222 TEST(UNISTD_TEST, execve_failure) {
1223 ExecTestHelper eth;
1224 errno = 0;
1225 ASSERT_EQ(-1, execve("/", eth.GetArgs(), eth.GetEnv()));
1226 ASSERT_EQ(EACCES, errno);
1227 }
1228
TEST(UNISTD_TEST,execve_args)1229 TEST(UNISTD_TEST, execve_args) {
1230 // int execve(const char* path, char* argv[], char* envp[]);
1231
1232 // Test basic argument passing.
1233 ExecTestHelper eth;
1234 eth.SetArgs({"echo", "hello", "world", nullptr});
1235 eth.Run([&]() { execve(BIN_DIR "echo", eth.GetArgs(), eth.GetEnv()); }, 0, "hello world\n");
1236
1237 // Test environment variable setting too.
1238 eth.SetArgs({"printenv", nullptr});
1239 eth.SetEnv({"A=B", nullptr});
1240 eth.Run([&]() { execve(BIN_DIR "printenv", eth.GetArgs(), eth.GetEnv()); }, 0, "A=B\n");
1241 }
1242
TEST(UNISTD_TEST,execl_failure)1243 TEST(UNISTD_TEST, execl_failure) {
1244 errno = 0;
1245 ASSERT_EQ(-1, execl("/", "/", nullptr));
1246 ASSERT_EQ(EACCES, errno);
1247 }
1248
TEST(UNISTD_TEST,execl)1249 TEST(UNISTD_TEST, execl) {
1250 ExecTestHelper eth;
1251 // int execl(const char* path, const char* arg, ...);
1252 eth.Run([&]() { execl(BIN_DIR "echo", "echo", "hello", "world", nullptr); }, 0, "hello world\n");
1253 }
1254
TEST(UNISTD_TEST,execle_failure)1255 TEST(UNISTD_TEST, execle_failure) {
1256 ExecTestHelper eth;
1257 errno = 0;
1258 ASSERT_EQ(-1, execle("/", "/", nullptr, eth.GetEnv()));
1259 ASSERT_EQ(EACCES, errno);
1260 }
1261
TEST(UNISTD_TEST,execle)1262 TEST(UNISTD_TEST, execle) {
1263 ExecTestHelper eth;
1264 eth.SetEnv({"A=B", nullptr});
1265 // int execle(const char* path, const char* arg, ..., char* envp[]);
1266 eth.Run([&]() { execle(BIN_DIR "printenv", "printenv", nullptr, eth.GetEnv()); }, 0, "A=B\n");
1267 }
1268
TEST(UNISTD_TEST,execv_failure)1269 TEST(UNISTD_TEST, execv_failure) {
1270 ExecTestHelper eth;
1271 errno = 0;
1272 ASSERT_EQ(-1, execv("/", eth.GetArgs()));
1273 ASSERT_EQ(EACCES, errno);
1274 }
1275
TEST(UNISTD_TEST,execv)1276 TEST(UNISTD_TEST, execv) {
1277 ExecTestHelper eth;
1278 eth.SetArgs({"echo", "hello", "world", nullptr});
1279 // int execv(const char* path, char* argv[]);
1280 eth.Run([&]() { execv(BIN_DIR "echo", eth.GetArgs()); }, 0, "hello world\n");
1281 }
1282
TEST(UNISTD_TEST,execlp_failure)1283 TEST(UNISTD_TEST, execlp_failure) {
1284 errno = 0;
1285 ASSERT_EQ(-1, execlp("/", "/", nullptr));
1286 ASSERT_EQ(EACCES, errno);
1287 }
1288
TEST(UNISTD_TEST,execlp)1289 TEST(UNISTD_TEST, execlp) {
1290 ExecTestHelper eth;
1291 // int execlp(const char* file, const char* arg, ...);
1292 eth.Run([&]() { execlp("echo", "echo", "hello", "world", nullptr); }, 0, "hello world\n");
1293 }
1294
TEST(UNISTD_TEST,execvp_failure)1295 TEST(UNISTD_TEST, execvp_failure) {
1296 ExecTestHelper eth;
1297 eth.SetArgs({nullptr});
1298 errno = 0;
1299 ASSERT_EQ(-1, execvp("/", eth.GetArgs()));
1300 ASSERT_EQ(EACCES, errno);
1301 }
1302
TEST(UNISTD_TEST,execvp)1303 TEST(UNISTD_TEST, execvp) {
1304 ExecTestHelper eth;
1305 eth.SetArgs({"echo", "hello", "world", nullptr});
1306 // int execvp(const char* file, char* argv[]);
1307 eth.Run([&]() { execvp("echo", eth.GetArgs()); }, 0, "hello world\n");
1308 }
1309
TEST(UNISTD_TEST,execvpe_failure)1310 TEST(UNISTD_TEST, execvpe_failure) {
1311 ExecTestHelper eth;
1312 errno = 0;
1313 ASSERT_EQ(-1, execvpe("this-does-not-exist", eth.GetArgs(), eth.GetEnv()));
1314 // Running in CTS we might not even be able to search all directories in $PATH.
1315 ASSERT_TRUE(errno == ENOENT || errno == EACCES);
1316 }
1317
TEST(UNISTD_TEST,execvpe)1318 TEST(UNISTD_TEST, execvpe) {
1319 // int execvpe(const char* file, char* argv[], char* envp[]);
1320
1321 // Test basic argument passing.
1322 ExecTestHelper eth;
1323 eth.SetArgs({"echo", "hello", "world", nullptr});
1324 eth.Run([&]() { execvpe("echo", eth.GetArgs(), eth.GetEnv()); }, 0, "hello world\n");
1325
1326 // Test environment variable setting too.
1327 eth.SetArgs({"printenv", nullptr});
1328 eth.SetEnv({"A=B", nullptr});
1329 eth.Run([&]() { execvpe("printenv", eth.GetArgs(), eth.GetEnv()); }, 0, "A=B\n");
1330 }
1331
TEST(UNISTD_TEST,execvpe_ENOEXEC)1332 TEST(UNISTD_TEST, execvpe_ENOEXEC) {
1333 // Create a shell script with #!.
1334 TemporaryFile tf;
1335 ASSERT_TRUE(android::base::WriteStringToFile("#!" BIN_DIR "sh\necho script\n", tf.filename));
1336
1337 // Set $PATH so we can find it.
1338 setenv("PATH", dirname(tf.filename), 1);
1339
1340 ExecTestHelper eth;
1341 eth.SetArgs({basename(tf.filename), nullptr});
1342
1343 // It's not inherently executable.
1344 errno = 0;
1345 ASSERT_EQ(-1, execvpe(basename(tf.filename), eth.GetArgs(), eth.GetEnv()));
1346 ASSERT_EQ(EACCES, errno);
1347
1348 // Make it executable (and keep it writable because we're going to rewrite it below).
1349 ASSERT_EQ(0, chmod(tf.filename, 0777));
1350
1351 // TemporaryFile will have a writable fd, so we can test ETXTBSY while we're here...
1352 errno = 0;
1353 ASSERT_EQ(-1, execvpe(basename(tf.filename), eth.GetArgs(), eth.GetEnv()));
1354 ASSERT_EQ(ETXTBSY, errno);
1355
1356 // 1. The simplest test: the kernel should handle this.
1357 ASSERT_EQ(0, close(tf.fd));
1358 eth.Run([&]() { execvpe(basename(tf.filename), eth.GetArgs(), eth.GetEnv()); }, 0, "script\n");
1359
1360 // 2. Try again without a #!. We should have to handle this ourselves.
1361 ASSERT_TRUE(android::base::WriteStringToFile("echo script\n", tf.filename));
1362 eth.Run([&]() { execvpe(basename(tf.filename), eth.GetArgs(), eth.GetEnv()); }, 0, "script\n");
1363
1364 // 3. Again without a #!, but also with a leading '/', since that's a special case in the
1365 // implementation.
1366 eth.Run([&]() { execvpe(tf.filename, eth.GetArgs(), eth.GetEnv()); }, 0, "script\n");
1367 }
1368
TEST(UNISTD_TEST,execvp_libcore_test_55017)1369 TEST(UNISTD_TEST, execvp_libcore_test_55017) {
1370 ExecTestHelper eth;
1371 eth.SetArgs({"/system/bin/does-not-exist", nullptr});
1372
1373 errno = 0;
1374 ASSERT_EQ(-1, execvp("/system/bin/does-not-exist", eth.GetArgs()));
1375 ASSERT_EQ(ENOENT, errno);
1376 }
1377
TEST(UNISTD_TEST,exec_argv0_null)1378 TEST(UNISTD_TEST, exec_argv0_null) {
1379 // http://b/33276926
1380 char* args[] = {nullptr};
1381 char* envs[] = {nullptr};
1382 ASSERT_EXIT(execve("/system/bin/run-as", args, envs), testing::ExitedWithCode(1),
1383 "<unknown>: usage: run-as");
1384 }
1385
TEST(UNISTD_TEST,fexecve_failure)1386 TEST(UNISTD_TEST, fexecve_failure) {
1387 ExecTestHelper eth;
1388 errno = 0;
1389 int fd = open("/", O_RDONLY);
1390 ASSERT_NE(-1, fd);
1391 ASSERT_EQ(-1, fexecve(fd, eth.GetArgs(), eth.GetEnv()));
1392 ASSERT_EQ(EACCES, errno);
1393 close(fd);
1394 }
1395
TEST(UNISTD_TEST,fexecve_bad_fd)1396 TEST(UNISTD_TEST, fexecve_bad_fd) {
1397 ExecTestHelper eth;
1398 errno = 0;
1399 ASSERT_EQ(-1, fexecve(-1, eth.GetArgs(), eth.GetEnv()));
1400 ASSERT_EQ(EBADF, errno);
1401 }
1402
TEST(UNISTD_TEST,fexecve_args)1403 TEST(UNISTD_TEST, fexecve_args) {
1404 // Test basic argument passing.
1405 int echo_fd = open(BIN_DIR "echo", O_RDONLY | O_CLOEXEC);
1406 ASSERT_NE(-1, echo_fd);
1407 ExecTestHelper eth;
1408 eth.SetArgs({"echo", "hello", "world", nullptr});
1409 eth.Run([&]() { fexecve(echo_fd, eth.GetArgs(), eth.GetEnv()); }, 0, "hello world\n");
1410 close(echo_fd);
1411
1412 // Test environment variable setting too.
1413 int printenv_fd = open(BIN_DIR "printenv", O_RDONLY | O_CLOEXEC);
1414 ASSERT_NE(-1, printenv_fd);
1415 eth.SetArgs({"printenv", nullptr});
1416 eth.SetEnv({"A=B", nullptr});
1417 eth.Run([&]() { fexecve(printenv_fd, eth.GetArgs(), eth.GetEnv()); }, 0, "A=B\n");
1418 close(printenv_fd);
1419 }
1420
TEST(UNISTD_TEST,getlogin_r)1421 TEST(UNISTD_TEST, getlogin_r) {
1422 char buf[LOGIN_NAME_MAX] = {};
1423 EXPECT_EQ(ERANGE, getlogin_r(buf, 0));
1424 EXPECT_EQ(0, getlogin_r(buf, sizeof(buf)));
1425 EXPECT_STREQ(getlogin(), buf);
1426 }
1427
TEST(UNISTD_TEST,swab)1428 TEST(UNISTD_TEST, swab) {
1429 // POSIX: "The swab() function shall copy nbytes bytes, which are pointed to by src,
1430 // to the object pointed to by dest, exchanging adjacent bytes."
1431 char buf[BUFSIZ];
1432 memset(buf, 'x', sizeof(buf));
1433 swab("ehll oowlr\0d", buf, 12);
1434 ASSERT_STREQ("hello world", buf);
1435 }
1436
TEST(UNISTD_TEST,swab_odd_byte_count)1437 TEST(UNISTD_TEST, swab_odd_byte_count) {
1438 // POSIX: "If nbytes is odd, swab() copies and exchanges nbytes-1 bytes and the disposition
1439 // of the last byte is unspecified."
1440 // ...but it seems unreasonable to not just leave the last byte alone.
1441 char buf[BUFSIZ];
1442 memset(buf, 'x', sizeof(buf));
1443 swab("012345", buf, 3);
1444 ASSERT_EQ('1', buf[0]);
1445 ASSERT_EQ('0', buf[1]);
1446 ASSERT_EQ('x', buf[2]);
1447 }
1448
TEST(UNISTD_TEST,swab_overlap)1449 TEST(UNISTD_TEST, swab_overlap) {
1450 // POSIX: "If copying takes place between objects that overlap, the behavior is undefined."
1451 // ...but it seems unreasonable to not just do the right thing.
1452 char buf[] = "012345";
1453 swab(buf, buf, 4);
1454 ASSERT_EQ('1', buf[0]);
1455 ASSERT_EQ('0', buf[1]);
1456 ASSERT_EQ('3', buf[2]);
1457 ASSERT_EQ('2', buf[3]);
1458 ASSERT_EQ('4', buf[4]);
1459 ASSERT_EQ('5', buf[5]);
1460 ASSERT_EQ(0, buf[6]);
1461 }
1462
TEST(UNISTD_TEST,swab_negative_byte_count)1463 TEST(UNISTD_TEST, swab_negative_byte_count) {
1464 // POSIX: "If nbytes is negative, swab() does nothing."
1465 char buf[BUFSIZ];
1466 memset(buf, 'x', sizeof(buf));
1467 swab("hello", buf, -1);
1468 ASSERT_EQ('x', buf[0]);
1469 }
1470