From 9b77d73744852adddbb0871dd2a5691738886f10 Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Fri, 7 Aug 2020 10:27:37 -0700 Subject: [PATCH] logd: add a test that logd ignores SIGPIPE Now that libsysutils sockets do not ignore SIGPIPE, logd itself must ignore it and we therefore add a test that it is ignored properly. Test: this test passes Test: this test fails if logd doesn't ignore SIGPIPE Change-Id: I65474967f40291a7abd11cfd279c8cde2d2bca14 --- logd/logd_test.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/logd/logd_test.cpp b/logd/logd_test.cpp index 5a0585a73..202ab062b 100644 --- a/logd/logd_test.cpp +++ b/logd/logd_test.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -41,6 +42,8 @@ #include "LogReader.h" // pickup LOGD_SNDTIMEO +using android::base::unique_fd; + #ifdef __ANDROID__ static void send_to_control(char* buf, size_t len) { int sock = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_RESERVED, @@ -836,3 +839,34 @@ TEST(logd, getEventTag_newentry) { GTEST_LOG_(INFO) << "This test does nothing.\n"; #endif } + +TEST(logd, no_epipe) { +#ifdef __ANDROID__ + // Actually generating SIGPIPE in logd is racy, since we need to close the socket quicker than + // logd finishes writing the data to it, so we try 10 times, which should be enough to trigger + // SIGPIPE if logd isn't ignoring SIGPIPE + for (int i = 0; i < 10; ++i) { + unique_fd sock1( + socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM)); + ASSERT_GT(sock1, 0); + unique_fd sock2( + socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM)); + ASSERT_GT(sock2, 0); + + std::string message = "getStatistics 0 1 2 3 4 5 6 7"; + + ASSERT_GT(write(sock1, message.c_str(), message.length()), 0); + sock1.reset(); + ASSERT_GT(write(sock2, message.c_str(), message.length()), 0); + + struct pollfd p = {.fd = sock2, .events = POLLIN, .revents = 0}; + + int ret = poll(&p, 1, 20); + EXPECT_EQ(ret, 1); + EXPECT_TRUE(p.revents & POLLIN); + EXPECT_FALSE(p.revents & POLL_ERR); + } +#else + GTEST_LOG_(INFO) << "This test does nothing.\n"; +#endif +}