Factor out the waiting for children in bionic tests.
Change-Id: I4a1e51b6920b33dc892d447f5bd6d10f1cb2704a
diff --git a/tests/dlext_test.cpp b/tests/dlext_test.cpp
index c221402..c64ec15 100644
--- a/tests/dlext_test.cpp
+++ b/tests/dlext_test.cpp
@@ -442,10 +442,7 @@
// continuing in parent
ASSERT_NOERROR(close(relro_fd));
ASSERT_NOERROR(pid);
- int status;
- ASSERT_EQ(pid, waitpid(pid, &status, 0));
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(0, WEXITSTATUS(status));
+ AssertChildExited(pid, 0);
// reopen file for reading so it can be used
relro_fd = open(relro_file, O_RDONLY);
@@ -554,7 +551,7 @@
const int CHILDREN = 20;
// Create children
- pid_t childpid[CHILDREN];
+ pid_t child_pids[CHILDREN];
int childpipe[CHILDREN];
for (int i=0; i<CHILDREN; ++i) {
char read_buf;
@@ -599,7 +596,7 @@
close(child_done_pipe[0]);
// save the child's pid and the parent_done_pipe
- childpid[i] = child;
+ child_pids[i] = child;
childpipe[i] = parent_done_pipe[1];
}
@@ -607,7 +604,7 @@
size_t total_pss = 0;
for (int i=0; i<CHILDREN; ++i) {
size_t child_pss;
- ASSERT_NO_FATAL_FAILURE(getPss(childpid[i], &child_pss));
+ ASSERT_NO_FATAL_FAILURE(getPss(child_pids[i], &child_pss));
total_pss += child_pss;
}
*pss_out = total_pss;
@@ -616,11 +613,8 @@
for (int i=0; i<CHILDREN; ++i) {
ASSERT_NOERROR(close(childpipe[i]));
}
- for (int i=0; i<CHILDREN; ++i) {
- int status;
- ASSERT_EQ(childpid[i], waitpid(childpid[i], &status, 0));
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(0, WEXITSTATUS(status));
+ for (int i = 0; i < CHILDREN; ++i) {
+ AssertChildExited(child_pids[i], 0);
}
}
diff --git a/tests/pthread_dlfcn_test.cpp b/tests/pthread_dlfcn_test.cpp
index 5e8b206..64423da 100644
--- a/tests/pthread_dlfcn_test.cpp
+++ b/tests/pthread_dlfcn_test.cpp
@@ -17,6 +17,8 @@
#include <dlfcn.h>
+#include "utils.h"
+
static int g_atfork_prepare_calls = 0;
static void AtForkPrepare1() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 1; }
static void AtForkPrepare2() { g_atfork_prepare_calls = (g_atfork_prepare_calls * 10) + 2; }
@@ -49,7 +51,7 @@
ASSERT_EQ(0, pthread_atfork(AtForkPrepare4, AtForkParent4, AtForkChild4));
- int pid = fork();
+ pid_t pid = fork();
ASSERT_NE(-1, pid) << strerror(errno);
@@ -64,8 +66,7 @@
EXPECT_EQ(0, dlclose(handle));
g_atfork_prepare_calls = g_atfork_parent_calls = g_atfork_child_calls = 0;
- int status;
- ASSERT_EQ(pid, waitpid(pid, &status, 0));
+ AssertChildExited(pid, 0);
pid = fork();
@@ -79,5 +80,5 @@
ASSERT_EQ(14, g_atfork_parent_calls);
ASSERT_EQ(41, g_atfork_prepare_calls);
- ASSERT_EQ(pid, waitpid(pid, &status, 0));
+ AssertChildExited(pid, 0);
}
diff --git a/tests/pthread_test.cpp b/tests/pthread_test.cpp
index 78dbd39..d11ea3f 100755
--- a/tests/pthread_test.cpp
+++ b/tests/pthread_test.cpp
@@ -37,7 +37,6 @@
#include "private/ScopeGuard.h"
#include "BionicDeathTest.h"
#include "ScopedSignalHandler.h"
-
#include "utils.h"
TEST(pthread, pthread_key_create) {
@@ -142,10 +141,7 @@
_exit(99);
}
- int status;
- ASSERT_EQ(pid, waitpid(pid, &status, 0));
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(99, WEXITSTATUS(status));
+ AssertChildExited(pid, 99);
ASSERT_EQ(expected, pthread_getspecific(key));
ASSERT_EQ(0, pthread_key_delete(key));
@@ -1038,7 +1034,7 @@
ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
- int pid = fork();
+ pid_t pid = fork();
ASSERT_NE(-1, pid) << strerror(errno);
// Child and parent calls are made in the order they were registered.
@@ -1050,8 +1046,7 @@
// Prepare calls are made in the reverse order.
ASSERT_EQ(21, g_atfork_prepare_calls);
- int status;
- ASSERT_EQ(pid, waitpid(pid, &status, 0));
+ AssertChildExited(pid, 0);
}
TEST(pthread, pthread_attr_getscope) {
diff --git a/tests/pty_test.cpp b/tests/pty_test.cpp
index 7fe97e9..91d1f5e 100644
--- a/tests/pty_test.cpp
+++ b/tests/pty_test.cpp
@@ -19,6 +19,8 @@
#include <pty.h>
#include <sys/ioctl.h>
+#include "utils.h"
+
TEST(pty, openpty) {
int master, slave;
char name[32];
@@ -58,10 +60,7 @@
ASSERT_EQ(sid, getsid(0));
- int status;
- ASSERT_EQ(pid, waitpid(pid, &status, 0));
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(0, WEXITSTATUS(status));
+ AssertChildExited(pid, 0);
close(master);
}
diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp
index 050f5a7..6ae6cda 100644
--- a/tests/stdlib_test.cpp
+++ b/tests/stdlib_test.cpp
@@ -15,8 +15,10 @@
*/
#include <gtest/gtest.h>
+
#include "BionicDeathTest.h"
#include "TemporaryFile.h"
+#include "utils.h"
#include <errno.h>
#include <libgen.h>
@@ -323,10 +325,7 @@
quick_exit(99);
}
- int status;
- ASSERT_EQ(pid, waitpid(pid, &status, 0));
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(99, WEXITSTATUS(status));
+ AssertChildExited(pid, 99);
}
static int quick_exit_status = 0;
@@ -355,24 +354,18 @@
quick_exit(99);
}
- int status;
- ASSERT_EQ(pid, waitpid(pid, &status, 0));
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(99, WEXITSTATUS(status));
+ AssertChildExited(pid, 99);
}
TEST(unistd, _Exit) {
- int pid = fork();
+ pid_t pid = fork();
ASSERT_NE(-1, pid) << strerror(errno);
if (pid == 0) {
_Exit(99);
}
- int status;
- ASSERT_EQ(pid, waitpid(pid, &status, 0));
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(99, WEXITSTATUS(status));
+ AssertChildExited(pid, 99);
}
TEST(stdlib, pty_smoke) {
diff --git a/tests/sys_select_test.cpp b/tests/sys_select_test.cpp
index d4ac333..4ad77f0 100644
--- a/tests/sys_select_test.cpp
+++ b/tests/sys_select_test.cpp
@@ -23,6 +23,8 @@
#include <sys/types.h>
#include <sys/wait.h>
+#include "utils.h"
+
TEST(sys_select, fd_set_smoke) {
fd_set fds;
FD_ZERO(&fds);
@@ -68,7 +70,8 @@
char buf[sizeof(DELAY_MSG)];
ASSERT_EQ(static_cast<ssize_t>(sizeof(DELAY_MSG)), read(fd, buf, sizeof(DELAY_MSG)));
ASSERT_STREQ(DELAY_MSG, buf);
- ASSERT_EQ(pid, waitpid(pid, NULL, 0));
+
+ AssertChildExited(pid, 0);
}
TEST(sys_select, select_smoke) {
diff --git a/tests/time_test.cpp b/tests/time_test.cpp
index a04c449..ec1b549 100644
--- a/tests/time_test.cpp
+++ b/tests/time_test.cpp
@@ -27,6 +27,7 @@
#include <atomic>
#include "ScopedSignalHandler.h"
+#include "utils.h"
#include "private/bionic_constants.h"
@@ -218,7 +219,7 @@
timer_t timer_id;
ASSERT_EQ(0, timer_create(CLOCK_MONOTONIC, &se, &timer_id));
- int pid = fork();
+ pid_t pid = fork();
ASSERT_NE(-1, pid) << strerror(errno);
if (pid == 0) {
@@ -228,10 +229,7 @@
_exit(0);
}
- int status;
- ASSERT_EQ(pid, waitpid(pid, &status, 0));
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(0, WEXITSTATUS(status));
+ AssertChildExited(pid, 0);
ASSERT_EQ(0, timer_delete(timer_id));
}
diff --git a/tests/unistd_test.cpp b/tests/unistd_test.cpp
index 5ef54a4..62e39fd 100644
--- a/tests/unistd_test.cpp
+++ b/tests/unistd_test.cpp
@@ -15,9 +15,11 @@
*/
#include <gtest/gtest.h>
+
#include "BionicDeathTest.h"
#include "ScopedSignalHandler.h"
#include "TemporaryFile.h"
+#include "utils.h"
#include <errno.h>
#include <fcntl.h>
@@ -245,17 +247,14 @@
}
TEST(UNISTD_TEST, _exit) {
- int pid = fork();
+ pid_t pid = fork();
ASSERT_NE(-1, pid) << strerror(errno);
if (pid == 0) {
_exit(99);
}
- int status;
- ASSERT_EQ(pid, waitpid(pid, &status, 0));
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(99, WEXITSTATUS(status));
+ AssertChildExited(pid, 99);
}
TEST(UNISTD_TEST, getenv_unsetenv) {
@@ -429,11 +428,7 @@
} else {
// We're the parent.
ASSERT_EQ(parent_pid, getpid());
-
- int status;
- ASSERT_EQ(fork_result, waitpid(fork_result, &status, 0));
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(123, WEXITSTATUS(status));
+ AssertChildExited(fork_result, 123);
}
}
@@ -464,10 +459,7 @@
ASSERT_EQ(parent_pid, getpid());
- int status;
- ASSERT_EQ(clone_result, waitpid(clone_result, &status, 0));
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(123, WEXITSTATUS(status));
+ AssertChildExited(clone_result, 123);
}
static void* GetPidCachingPthreadStartRoutine(void*) {
@@ -873,13 +865,6 @@
ASSERT_EQ(EBADF, errno);
}
-static void WaitForChildExit() {
- int status;
- wait(&status);
- ASSERT_TRUE(WIFEXITED(status));
- ASSERT_EQ(0, WEXITSTATUS(status));
-}
-
TEST(UNISTD_TEST, lockf_smoke) {
constexpr off64_t file_size = 32*1024LL;
@@ -964,7 +949,7 @@
ASSERT_EQ(EACCES, errno);
_exit(0);
}
- WaitForChildExit();
+ AssertChildExited(pid, 0);
}
TEST(UNISTD_TEST, lockf_partial_with_child) {
@@ -994,7 +979,7 @@
ASSERT_EQ(EACCES, errno);
_exit(0);
}
- WaitForChildExit();
+ AssertChildExited(pid, 0);
// The second half was locked by the child, but the lock disappeared
// when the process exited, so check it can be locked now.
diff --git a/tests/utils.h b/tests/utils.h
index a8f3441..828c8d0 100644
--- a/tests/utils.h
+++ b/tests/utils.h
@@ -16,8 +16,11 @@
#ifndef __TEST_UTILS_H
#define __TEST_UTILS_H
+
#include <inttypes.h>
#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/wait.h>
#include <unistd.h>
#include <atomic>
@@ -109,4 +112,11 @@
}
}
+static inline void AssertChildExited(int pid, int expected_exit_status) {
+ int status;
+ ASSERT_EQ(pid, waitpid(pid, &status, 0));
+ ASSERT_TRUE(WIFEXITED(status));
+ ASSERT_EQ(expected_exit_status, WEXITSTATUS(status));
+}
+
#endif