From 2b3ebaf5c03acaffa1607d6385a2508f4bdd320e Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Fri, 31 Jul 2020 15:21:54 -0700 Subject: SocketClient: don't ignore SIGPIPE 1) All current users are better off ignoring SIGPIPE at the beginning of their process instead of ignoring it just for SocketClient 2) This isn't thread safe if users did want it, since sigaction() ignores SIGPIPE for the entire process 3) This costs 5-10% of logd CPU time when logcat is reading logs Also clean up the error handling in SocketClient::sendDataLockedv(). Test: kill logcat and see that logd doesn't crash Test: run simpleperf and see that no cycles are going to sigaction Change-Id: I6532c8a0d71338e534411707b9a9bd785145c730 --- libsysutils/src/SocketClient.cpp | 51 +++++++++++++--------------------------- 1 file changed, 16 insertions(+), 35 deletions(-) (limited to 'libsysutils/src/SocketClient.cpp') diff --git a/libsysutils/src/SocketClient.cpp b/libsysutils/src/SocketClient.cpp index fe2f3d668..e90afcd18 100644 --- a/libsysutils/src/SocketClient.cpp +++ b/libsysutils/src/SocketClient.cpp @@ -201,50 +201,31 @@ int SocketClient::sendDataLockedv(struct iovec *iov, int iovcnt) { return 0; } - int ret = 0; - int e = 0; // SLOGW and sigaction are not inert regarding errno int current = 0; - struct sigaction new_action, old_action; - memset(&new_action, 0, sizeof(new_action)); - new_action.sa_handler = SIG_IGN; - sigaction(SIGPIPE, &new_action, &old_action); - for (;;) { - ssize_t rc = TEMP_FAILURE_RETRY( - writev(mSocket, iov + current, iovcnt - current)); - - if (rc > 0) { - size_t written = rc; - while ((current < iovcnt) && (written >= iov[current].iov_len)) { - written -= iov[current].iov_len; - current++; - } - if (current == iovcnt) { - break; - } - iov[current].iov_base = (char *)iov[current].iov_base + written; - iov[current].iov_len -= written; - continue; - } + ssize_t rc = TEMP_FAILURE_RETRY(writev(mSocket, iov + current, iovcnt - current)); if (rc == 0) { - e = EIO; + errno = EIO; SLOGW("0 length write :("); - } else { - e = errno; - SLOGW("write error (%s)", strerror(e)); + return -1; + } else if (rc < 0) { + SLOGW("write error (%s)", strerror(errno)); + return -1; } - ret = -1; - break; - } - sigaction(SIGPIPE, &old_action, &new_action); - - if (e != 0) { - errno = e; + size_t written = rc; + while (current < iovcnt && written >= iov[current].iov_len) { + written -= iov[current].iov_len; + current++; + } + if (current == iovcnt) { + return 0; + } + iov[current].iov_base = (char*)iov[current].iov_base + written; + iov[current].iov_len -= written; } - return ret; } void SocketClient::incRef() { -- cgit v1.2.3