liblog: remove alarm in logd_reader.cpp
There is an alarm() call that provides a 30 second timeout in case
logd is unavailable. The main reason for this is in the case that logd
is crashing, debuggerd is ptrace'ing logd, and tombstoned is
attempting to dump log messages. In this case, with no other checks
and without this alarm, tombstoned will deadlock when dumping log
messages.
However, tombstoned already has two mechanisms to prevent the above
situation from happening:
1) It checks that the thread name that is is dumping is either logd or
starts with "logd." and skips dumping logs in this case.
2) It does not dump logs if it is running in process for any process.
Calling alarm() or modifying signal handlers from general purpose
libraries is not recommended either, so without a strong reason to
keep this, this change removes it.
This also shortens the liblog.wrap_mode_blocks test time, since the 30
second issue that it ensures does not happen has been fundamentally
removed.
Test: `kill -8 `pidof logd`` succeeds without delay
Test: liblog, logd unit tests
Change-Id: Id8a40544645d220e49f7ba299201af80a0c44de9
diff --git a/liblog/logd_reader.cpp b/liblog/logd_reader.cpp
index 916a428..0d304bb 100644
--- a/liblog/logd_reader.cpp
+++ b/liblog/logd_reader.cpp
@@ -316,16 +316,11 @@
return check_log_success(buf, send_log_msg(NULL, NULL, buf, len));
}
-static void caught_signal(int signum __unused) {}
-
static int logdOpen(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp) {
struct android_log_logger* logger;
- struct sigaction ignore;
- struct sigaction old_sigaction;
- unsigned int old_alarm = 0;
char buffer[256], *cp, c;
- int e, ret, remaining, sock;
+ int ret, remaining, sock;
if (!logger_list) {
return -EINVAL;
@@ -393,29 +388,13 @@
cp += ret;
}
- if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
- /* Deal with an unresponsive logd */
- memset(&ignore, 0, sizeof(ignore));
- ignore.sa_handler = caught_signal;
- sigemptyset(&ignore.sa_mask);
- /* particularily useful if tombstone is reporting for logd */
- sigaction(SIGALRM, &ignore, &old_sigaction);
- old_alarm = alarm(30);
- }
- ret = write(sock, buffer, cp - buffer);
- e = errno;
- if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
- if (e == EINTR) {
- e = ETIMEDOUT;
- }
- alarm(old_alarm);
- sigaction(SIGALRM, &old_sigaction, NULL);
- }
+ ret = TEMP_FAILURE_RETRY(write(sock, buffer, cp - buffer));
+ int write_errno = errno;
if (ret <= 0) {
close(sock);
- if ((ret == -1) && e) {
- return -e;
+ if (ret == -1) {
+ return -write_errno;
}
if (ret == 0) {
return -EIO;
@@ -433,52 +412,21 @@
/* Read from the selected logs */
static int logdRead(struct android_log_logger_list* logger_list,
struct android_log_transport_context* transp, struct log_msg* log_msg) {
- int ret, e;
- struct sigaction ignore;
- struct sigaction old_sigaction;
- unsigned int old_alarm = 0;
-
- ret = logdOpen(logger_list, transp);
+ int ret = logdOpen(logger_list, transp);
if (ret < 0) {
return ret;
}
memset(log_msg, 0, sizeof(*log_msg));
- unsigned int new_alarm = 0;
- if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
- if ((logger_list->mode & ANDROID_LOG_WRAP) &&
- (logger_list->start.tv_sec || logger_list->start.tv_nsec)) {
- /* b/64143705 */
- new_alarm = (ANDROID_LOG_WRAP_DEFAULT_TIMEOUT * 11) / 10 + 10;
- logger_list->mode &= ~ANDROID_LOG_WRAP;
- } else {
- new_alarm = 30;
- }
-
- memset(&ignore, 0, sizeof(ignore));
- ignore.sa_handler = caught_signal;
- sigemptyset(&ignore.sa_mask);
- /* particularily useful if tombstone is reporting for logd */
- sigaction(SIGALRM, &ignore, &old_sigaction);
- old_alarm = alarm(new_alarm);
- }
-
/* NOTE: SOCK_SEQPACKET guarantees we read exactly one full entry */
- ret = recv(ret, log_msg, LOGGER_ENTRY_MAX_LEN, 0);
- e = errno;
-
- if (new_alarm) {
- if ((ret == 0) || (e == EINTR)) {
- e = EAGAIN;
- ret = -1;
- }
- alarm(old_alarm);
- sigaction(SIGALRM, &old_sigaction, NULL);
+ ret = TEMP_FAILURE_RETRY(recv(ret, log_msg, LOGGER_ENTRY_MAX_LEN, 0));
+ if ((logger_list->mode & ANDROID_LOG_NONBLOCK) && ret == 0) {
+ return -EAGAIN;
}
- if ((ret == -1) && e) {
- return -e;
+ if (ret == -1) {
+ return -errno;
}
return ret;
}