liblog: __android_log_pmsg_file_write() cleanup
__android_log_pmsg_file_write() will open /dev/pmsg0 if not
already, and will close it if we opened it.
Added atomic access to the android_log_context as insurance.
Fortify and correct pmsg tests.
Test: gTest liblog-unit-tests --gtest_filter=liblog.__android_log_pmsg_file_*
Bug: 31958686
Change-Id: I2cf6f971b6968938f471fda67367efe20dae3004
diff --git a/liblog/pmsg_reader.c b/liblog/pmsg_reader.c
index 679c159..a0a69c1 100644
--- a/liblog/pmsg_reader.c
+++ b/liblog/pmsg_reader.c
@@ -151,8 +151,8 @@
memset(log_msg, 0, sizeof(*log_msg));
- if (transp->context.fd <= 0) {
- int fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
+ if (atomic_load(&transp->context.fd) <= 0) {
+ int i, fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY | O_CLOEXEC);
if (fd < 0) {
return -errno;
@@ -164,13 +164,22 @@
return -errno;
}
}
- transp->context.fd = fd;
+ i = atomic_exchange(&transp->context.fd, fd);
+ if ((i > 0) && (i != fd)) {
+ close(i);
+ }
preread_count = 0;
}
while(1) {
+ int fd;
+
if (preread_count < sizeof(buf)) {
- ret = TEMP_FAILURE_RETRY(read(transp->context.fd,
+ fd = atomic_load(&transp->context.fd);
+ if (fd <= 0) {
+ return -EBADF;
+ }
+ ret = TEMP_FAILURE_RETRY(read(fd,
&buf.p.magic + preread_count,
sizeof(buf) - preread_count));
if (ret < 0) {
@@ -212,9 +221,13 @@
log_msg->entry_v4.msg :
log_msg->entry_v3.msg;
*msg = buf.prio;
- ret = TEMP_FAILURE_RETRY(read(transp->context.fd,
- msg + sizeof(buf.prio),
- buf.p.len - sizeof(buf)));
+ fd = atomic_load(&transp->context.fd);
+ if (fd <= 0) {
+ return -EBADF;
+ }
+ ret = TEMP_FAILURE_RETRY(read(fd,
+ msg + sizeof(buf.prio),
+ buf.p.len - sizeof(buf)));
if (ret < 0) {
return -errno;
}
@@ -239,12 +252,19 @@
}
}
- current = TEMP_FAILURE_RETRY(lseek(transp->context.fd,
- (off_t)0, SEEK_CUR));
+ fd = atomic_load(&transp->context.fd);
+ if (fd <= 0) {
+ return -EBADF;
+ }
+ current = TEMP_FAILURE_RETRY(lseek(fd, (off_t)0, SEEK_CUR));
if (current < 0) {
return -errno;
}
- next = TEMP_FAILURE_RETRY(lseek(transp->context.fd,
+ fd = atomic_load(&transp->context.fd);
+ if (fd <= 0) {
+ return -EBADF;
+ }
+ next = TEMP_FAILURE_RETRY(lseek(fd,
(off_t)(buf.p.len - sizeof(buf)),
SEEK_CUR));
if (next < 0) {
@@ -258,10 +278,10 @@
static void pmsgClose(struct android_log_logger_list *logger_list __unused,
struct android_log_transport_context *transp) {
- if (transp->context.fd > 0) {
- close (transp->context.fd);
+ int fd = atomic_exchange(&transp->context.fd, 0);
+ if (fd > 0) {
+ close (fd);
}
- transp->context.fd = 0;
}
LIBLOG_ABI_PRIVATE ssize_t __android_log_pmsg_file_read(