| // Copyright 2021 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #include "src/developer/debug/shared/message_loop_linux.h" | |
| #include <lib/fit/defer.h> | |
| #include <signal.h> | |
| #include <sys/wait.h> | |
| #include <unistd.h> | |
| #include <memory> | |
| #include <optional> | |
| #include <gtest/gtest.h> | |
| #include "src/lib/files/eintr_wrapper.h" | |
| namespace debug { | |
| namespace { | |
| class ScopedChild { | |
| public: | |
| explicit ScopedChild(pid_t pid) : pid_(pid) {} | |
| ~ScopedChild() { | |
| if (pid_ > 0) { | |
| kill(pid_, SIGKILL); | |
| HANDLE_EINTR(waitpid(pid_, nullptr, 0)); | |
| } | |
| } | |
| pid_t pid() const { return pid_; } | |
| void MarkReaped() { pid_ = -1; } | |
| private: | |
| pid_t pid_ = -1; | |
| }; | |
| pid_t ForkPipeChild(int read_fd, int write_fd, int exit_code) { | |
| pid_t pid = fork(); | |
| if (pid == 0) { | |
| close(write_fd); | |
| char buf; | |
| if (HANDLE_EINTR(read(read_fd, &buf, 1)) < 0) { | |
| _exit(99); | |
| } | |
| close(read_fd); | |
| _exit(exit_code); | |
| } | |
| return pid; | |
| } | |
| struct TimerState { | |
| bool active = true; | |
| bool timed_out = false; | |
| }; | |
| std::shared_ptr<TimerState> PostQuitTimeout(MessageLoopLinux& loop, uint64_t timeout_ms) { | |
| auto state = std::make_shared<TimerState>(); | |
| loop.PostTimer(FROM_HERE, timeout_ms, [&loop, state]() { | |
| if (!state->active) { | |
| return; | |
| } | |
| state->timed_out = true; | |
| loop.QuitNow(); | |
| }); | |
| return state; | |
| } | |
| } // namespace | |
| // Tests that we can get SIGCHLD signals for a single exiting child. | |
| TEST(MessageLoopLinux, OneChildExits) { | |
| MessageLoopLinux loop; | |
| std::string error_message; | |
| ASSERT_TRUE(loop.Init(&error_message)) << error_message; | |
| auto cleanup = fit::defer([&] { loop.Cleanup(); }); | |
| { | |
| pid_t child_pid = fork(); | |
| ASSERT_GE(child_pid, 0); | |
| if (child_pid == 0) { | |
| _exit(1); | |
| } | |
| std::optional<int> result_status; | |
| MessageLoop::WatchHandle watch_handle = loop.WatchChildSignals( | |
| child_pid, [child_pid, &result_status, &loop](pid_t pid, int status) { | |
| EXPECT_EQ(child_pid, pid); | |
| result_status = status; | |
| loop.QuitNow(); | |
| }); | |
| loop.Run(); | |
| ASSERT_TRUE(result_status); | |
| EXPECT_TRUE(WIFEXITED(*result_status)); | |
| EXPECT_EQ(WEXITSTATUS(*result_status), 1); | |
| } | |
| } | |
| TEST(MessageLoopLinux, TwoChildrenExitInOneSigchld) { | |
| MessageLoopLinux loop; | |
| std::string error_message; | |
| ASSERT_TRUE(loop.Init(&error_message)) << error_message; | |
| auto cleanup = fit::defer([&] { loop.Cleanup(); }); | |
| int sync_pipe[2] = {-1, -1}; | |
| ASSERT_EQ(pipe(sync_pipe), 0); | |
| auto close_sync_pipe = fit::defer([&] { | |
| if (sync_pipe[0] >= 0) { | |
| close(sync_pipe[0]); | |
| } | |
| if (sync_pipe[1] >= 0) { | |
| close(sync_pipe[1]); | |
| } | |
| }); | |
| ScopedChild child1(ForkPipeChild(sync_pipe[0], sync_pipe[1], 11)); | |
| ASSERT_GE(child1.pid(), 0); | |
| ScopedChild child2(ForkPipeChild(sync_pipe[0], sync_pipe[1], 22)); | |
| ASSERT_GE(child2.pid(), 0); | |
| close(sync_pipe[0]); | |
| sync_pipe[0] = -1; | |
| std::optional<int> status1; | |
| std::optional<int> status2; | |
| { | |
| auto check_done = [&]() { | |
| if (status1 && status2) { | |
| loop.QuitNow(); | |
| } | |
| }; | |
| MessageLoop::WatchHandle handle1 = | |
| loop.WatchChildSignals(child1.pid(), [&](pid_t pid, int status) { | |
| EXPECT_EQ(pid, child1.pid()); | |
| status1 = status; | |
| child1.MarkReaped(); | |
| check_done(); | |
| }); | |
| MessageLoop::WatchHandle handle2 = | |
| loop.WatchChildSignals(child2.pid(), [&](pid_t pid, int status) { | |
| EXPECT_EQ(pid, child2.pid()); | |
| status2 = status; | |
| child2.MarkReaped(); | |
| check_done(); | |
| }); | |
| // Release both children and wait without reaping so their SIGCHLD events coalesce before | |
| // the loop runs. | |
| close(sync_pipe[1]); | |
| sync_pipe[1] = -1; | |
| siginfo_t si = {}; | |
| ASSERT_EQ(HANDLE_EINTR(waitid(P_PID, child1.pid(), &si, WEXITED | WNOWAIT)), 0); | |
| ASSERT_EQ(HANDLE_EINTR(waitid(P_PID, child2.pid(), &si, WEXITED | WNOWAIT)), 0); | |
| auto timeout_state = PostQuitTimeout(loop, 3000); | |
| loop.Run(); | |
| timeout_state->active = false; | |
| EXPECT_FALSE(timeout_state->timed_out); | |
| } | |
| ASSERT_TRUE(status1); | |
| EXPECT_TRUE(WIFEXITED(*status1)); | |
| EXPECT_EQ(WEXITSTATUS(*status1), 11); | |
| ASSERT_TRUE(status2); | |
| EXPECT_TRUE(WIFEXITED(*status2)); | |
| EXPECT_EQ(WEXITSTATUS(*status2), 22); | |
| } | |
| TEST(MessageLoopLinux, WatchAddedInCallbackIsPolledInSamePass) { | |
| MessageLoopLinux loop; | |
| std::string error_message; | |
| ASSERT_TRUE(loop.Init(&error_message)) << error_message; | |
| auto cleanup = fit::defer([&] { loop.Cleanup(); }); | |
| int sync_pipe[2] = {-1, -1}; | |
| ASSERT_EQ(pipe(sync_pipe), 0); | |
| auto close_sync_pipe = fit::defer([&] { | |
| if (sync_pipe[0] >= 0) { | |
| close(sync_pipe[0]); | |
| } | |
| if (sync_pipe[1] >= 0) { | |
| close(sync_pipe[1]); | |
| } | |
| }); | |
| ScopedChild child1(ForkPipeChild(sync_pipe[0], sync_pipe[1], 11)); | |
| ASSERT_GE(child1.pid(), 0); | |
| ScopedChild child2(ForkPipeChild(sync_pipe[0], sync_pipe[1], 22)); | |
| ASSERT_GE(child2.pid(), 0); | |
| close(sync_pipe[0]); | |
| sync_pipe[0] = -1; | |
| // Release both children and wait without reaping so only one SIGCHLD is pending when the loop | |
| // starts, ensuring child2's watch added inside child1's callback must run in the same pass. | |
| close(sync_pipe[1]); | |
| sync_pipe[1] = -1; | |
| siginfo_t si = {}; | |
| ASSERT_EQ(HANDLE_EINTR(waitid(P_PID, child1.pid(), &si, WEXITED | WNOWAIT)), 0); | |
| ASSERT_EQ(HANDLE_EINTR(waitid(P_PID, child2.pid(), &si, WEXITED | WNOWAIT)), 0); | |
| std::optional<int> status1; | |
| std::optional<int> status2; | |
| { | |
| MessageLoop::WatchHandle handle1; | |
| MessageLoop::WatchHandle handle2; | |
| handle1 = loop.WatchChildSignals(child1.pid(), [&](pid_t pid, int status) { | |
| EXPECT_EQ(pid, child1.pid()); | |
| status1 = status; | |
| child1.MarkReaped(); | |
| handle2 = loop.WatchChildSignals(child2.pid(), [&](pid_t pid2, int status_val2) { | |
| EXPECT_EQ(pid2, child2.pid()); | |
| status2 = status_val2; | |
| child2.MarkReaped(); | |
| loop.QuitNow(); | |
| }); | |
| }); | |
| auto timeout_state = PostQuitTimeout(loop, 3000); | |
| loop.Run(); | |
| timeout_state->active = false; | |
| EXPECT_FALSE(timeout_state->timed_out); | |
| } | |
| ASSERT_TRUE(status1); | |
| EXPECT_TRUE(WIFEXITED(*status1)); | |
| EXPECT_EQ(WEXITSTATUS(*status1), 11); | |
| ASSERT_TRUE(status2); | |
| EXPECT_TRUE(WIFEXITED(*status2)); | |
| EXPECT_EQ(WEXITSTATUS(*status2), 22); | |
| } | |
| TEST(MessageLoopLinux, StopWatchingInCallbackPreservesSiblingWatchesAndSkipsRunningChild) { | |
| MessageLoopLinux loop; | |
| std::string error_message; | |
| ASSERT_TRUE(loop.Init(&error_message)) << error_message; | |
| auto cleanup = fit::defer([&] { loop.Cleanup(); }); | |
| pid_t raw_running = fork(); | |
| ASSERT_GE(raw_running, 0); | |
| if (raw_running == 0) { | |
| pause(); | |
| _exit(0); | |
| } | |
| ScopedChild running_child(raw_running); | |
| int exit_pipe[2] = {-1, -1}; | |
| ASSERT_EQ(pipe(exit_pipe), 0); | |
| auto close_exit_pipe = fit::defer([&] { | |
| if (exit_pipe[0] >= 0) { | |
| close(exit_pipe[0]); | |
| } | |
| if (exit_pipe[1] >= 0) { | |
| close(exit_pipe[1]); | |
| } | |
| }); | |
| ScopedChild child1(ForkPipeChild(exit_pipe[0], exit_pipe[1], 11)); | |
| ASSERT_GE(child1.pid(), 0); | |
| ScopedChild child2(ForkPipeChild(exit_pipe[0], exit_pipe[1], 22)); | |
| ASSERT_GE(child2.pid(), 0); | |
| close(exit_pipe[0]); | |
| exit_pipe[0] = -1; | |
| std::optional<int> status1; | |
| std::optional<int> status2; | |
| bool running_called = false; | |
| bool handle1_closure_destroyed = false; | |
| { | |
| MessageLoop::WatchHandle handle1; | |
| MessageLoop::WatchHandle running_handle; | |
| MessageLoop::WatchHandle handle2; | |
| auto check_done = [&]() { | |
| if (status1 && status2) { | |
| loop.QuitNow(); | |
| } | |
| }; | |
| handle1 = loop.WatchChildSignals(child1.pid(), [&, watcher_guard = fit::defer([&] { | |
| handle1_closure_destroyed = true; | |
| })](pid_t pid, int status) { | |
| EXPECT_EQ(pid, child1.pid()); | |
| status1 = status; | |
| child1.MarkReaped(); | |
| handle1.StopWatching(); | |
| EXPECT_FALSE(handle1_closure_destroyed); | |
| check_done(); | |
| }); | |
| running_handle = loop.WatchChildSignals(running_child.pid(), | |
| [&](pid_t pid, int status) { running_called = true; }); | |
| handle2 = loop.WatchChildSignals(child2.pid(), [&](pid_t pid, int status) { | |
| EXPECT_EQ(pid, child2.pid()); | |
| status2 = status; | |
| child2.MarkReaped(); | |
| handle2.StopWatching(); | |
| check_done(); | |
| }); | |
| // Release both exiting children and wait without reaping so their SIGCHLD events coalesce | |
| // while keeping running_child blocked so its waitpid returns 0. | |
| close(exit_pipe[1]); | |
| exit_pipe[1] = -1; | |
| siginfo_t si = {}; | |
| ASSERT_EQ(HANDLE_EINTR(waitid(P_PID, child1.pid(), &si, WEXITED | WNOWAIT)), 0); | |
| ASSERT_EQ(HANDLE_EINTR(waitid(P_PID, child2.pid(), &si, WEXITED | WNOWAIT)), 0); | |
| auto timeout_state = PostQuitTimeout(loop, 3000); | |
| loop.Run(); | |
| timeout_state->active = false; | |
| EXPECT_FALSE(timeout_state->timed_out); | |
| } | |
| EXPECT_TRUE(handle1_closure_destroyed); | |
| EXPECT_FALSE(running_called); | |
| ASSERT_TRUE(status1); | |
| EXPECT_TRUE(WIFEXITED(*status1)); | |
| EXPECT_EQ(WEXITSTATUS(*status1), 11); | |
| ASSERT_TRUE(status2); | |
| EXPECT_TRUE(WIFEXITED(*status2)); | |
| EXPECT_EQ(WEXITSTATUS(*status2), 22); | |
| } | |
| TEST(MessageLoopLinux, QuitNowInFirstCallbackStillPollsRemainingChild) { | |
| MessageLoopLinux loop; | |
| std::string error_message; | |
| ASSERT_TRUE(loop.Init(&error_message)) << error_message; | |
| auto cleanup = fit::defer([&] { loop.Cleanup(); }); | |
| int sync_pipe[2] = {-1, -1}; | |
| ASSERT_EQ(pipe(sync_pipe), 0); | |
| auto close_sync_pipe = fit::defer([&] { | |
| if (sync_pipe[0] >= 0) { | |
| close(sync_pipe[0]); | |
| } | |
| if (sync_pipe[1] >= 0) { | |
| close(sync_pipe[1]); | |
| } | |
| }); | |
| ScopedChild child1(ForkPipeChild(sync_pipe[0], sync_pipe[1], 11)); | |
| ASSERT_GE(child1.pid(), 0); | |
| ScopedChild child2(ForkPipeChild(sync_pipe[0], sync_pipe[1], 22)); | |
| ASSERT_GE(child2.pid(), 0); | |
| close(sync_pipe[0]); | |
| sync_pipe[0] = -1; | |
| std::optional<int> status1; | |
| std::optional<int> status2; | |
| bool quit_requested = false; | |
| { | |
| MessageLoop::WatchHandle handle1 = | |
| loop.WatchChildSignals(child1.pid(), [&](pid_t pid, int status) { | |
| EXPECT_EQ(pid, child1.pid()); | |
| status1 = status; | |
| child1.MarkReaped(); | |
| quit_requested = true; | |
| loop.QuitNow(); | |
| }); | |
| MessageLoop::WatchHandle handle2 = | |
| loop.WatchChildSignals(child2.pid(), [&](pid_t pid, int status) { | |
| EXPECT_TRUE(quit_requested); | |
| EXPECT_EQ(pid, child2.pid()); | |
| status2 = status; | |
| child2.MarkReaped(); | |
| }); | |
| // Release both children and wait without reaping so their SIGCHLD events coalesce before | |
| // the loop runs. | |
| close(sync_pipe[1]); | |
| sync_pipe[1] = -1; | |
| siginfo_t si = {}; | |
| ASSERT_EQ(HANDLE_EINTR(waitid(P_PID, child1.pid(), &si, WEXITED | WNOWAIT)), 0); | |
| ASSERT_EQ(HANDLE_EINTR(waitid(P_PID, child2.pid(), &si, WEXITED | WNOWAIT)), 0); | |
| auto timeout_state = PostQuitTimeout(loop, 3000); | |
| loop.Run(); | |
| timeout_state->active = false; | |
| EXPECT_FALSE(timeout_state->timed_out); | |
| } | |
| EXPECT_TRUE(quit_requested); | |
| ASSERT_TRUE(status1); | |
| EXPECT_TRUE(WIFEXITED(*status1)); | |
| EXPECT_EQ(WEXITSTATUS(*status1), 11); | |
| ASSERT_TRUE(status2); | |
| EXPECT_TRUE(WIFEXITED(*status2)); | |
| EXPECT_EQ(WEXITSTATUS(*status2), 22); | |
| } | |
| } // namespace debug |