Merge "liblog: always restore errno in logging functions"
diff --git a/base/include/android-base/errno_restorer.h b/base/include/android-base/errno_restorer.h
new file mode 100644
index 0000000..1c8597c
--- /dev/null
+++ b/base/include/android-base/errno_restorer.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "errno.h"
+
+#include "android-base/macros.h"
+
+namespace android {
+namespace base {
+
+class ErrnoRestorer {
+ public:
+ ErrnoRestorer() : saved_errno_(errno) {}
+
+ ~ErrnoRestorer() { errno = saved_errno_; }
+
+ // Allow this object to be used as part of && operation.
+ operator bool() const { return true; }
+
+ private:
+ const int saved_errno_;
+
+ DISALLOW_COPY_AND_ASSIGN(ErrnoRestorer);
+};
+
+} // namespace base
+} // namespace android
diff --git a/base/include/android-base/logging.h b/base/include/android-base/logging.h
index cc162cd..accc225 100644
--- a/base/include/android-base/logging.h
+++ b/base/include/android-base/logging.h
@@ -64,6 +64,7 @@
#include <memory>
#include <ostream>
+#include "android-base/errno_restorer.h"
#include "android-base/macros.h"
// Note: DO NOT USE DIRECTLY. Use LOG_TAG instead.
@@ -154,27 +155,6 @@
// Replace the current aborter.
void SetAborter(AbortFunction&& aborter);
-class ErrnoRestorer {
- public:
- ErrnoRestorer()
- : saved_errno_(errno) {
- }
-
- ~ErrnoRestorer() {
- errno = saved_errno_;
- }
-
- // Allow this object to be used as part of && operation.
- operator bool() const {
- return true;
- }
-
- private:
- const int saved_errno_;
-
- DISALLOW_COPY_AND_ASSIGN(ErrnoRestorer);
-};
-
// A helper macro that produces an expression that accepts both a qualified name and an
// unqualified name for a LogSeverity, and returns a LogSeverity value.
// Note: DO NOT USE DIRECTLY. This is an implementation detail.
diff --git a/liblog/logger_write.cpp b/liblog/logger_write.cpp
index 3733357..ca68296 100644
--- a/liblog/logger_write.cpp
+++ b/liblog/logger_write.cpp
@@ -29,6 +29,7 @@
#include <shared_mutex>
+#include <android-base/errno_restorer.h>
#include <android-base/macros.h>
#include <private/android_filesystem_config.h>
#include <private/android_logger.h>
@@ -54,6 +55,8 @@
#include <windows.h>
#endif
+using android::base::ErrnoRestorer;
+
#define LOG_BUF_SIZE 1024
#if defined(__ANDROID__)
@@ -196,11 +199,9 @@
#ifdef __ANDROID__
static int write_to_log(log_id_t log_id, struct iovec* vec, size_t nr) {
- int ret, save_errno;
+ int ret;
struct timespec ts;
- save_errno = errno;
-
if (log_id == LOG_ID_KERNEL) {
return -EINVAL;
}
@@ -209,23 +210,19 @@
if (log_id == LOG_ID_SECURITY) {
if (vec[0].iov_len < 4) {
- errno = save_errno;
return -EINVAL;
}
ret = check_log_uid_permissions();
if (ret < 0) {
- errno = save_errno;
return ret;
}
if (!__android_log_security()) {
/* If only we could reset downstream logd counter */
- errno = save_errno;
return -EPERM;
}
} else if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
if (vec[0].iov_len < 4) {
- errno = save_errno;
return -EINVAL;
}
}
@@ -233,7 +230,6 @@
ret = LogdWrite(log_id, &ts, vec, nr);
PmsgWrite(log_id, &ts, vec, nr);
- errno = save_errno;
return ret;
}
#else
@@ -313,6 +309,8 @@
}
void __android_log_write_logger_data(__android_logger_data* logger_data, const char* msg) {
+ ErrnoRestorer errno_restorer;
+
auto tag_lock = std::shared_lock{default_tag_lock, std::defer_lock};
if (logger_data->tag == nullptr) {
tag_lock.lock();
@@ -330,6 +328,8 @@
}
int __android_log_buf_write(int bufID, int prio, const char* tag, const char* msg) {
+ ErrnoRestorer errno_restorer;
+
if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
return 0;
}
@@ -340,6 +340,8 @@
}
int __android_log_vprint(int prio, const char* tag, const char* fmt, va_list ap) {
+ ErrnoRestorer errno_restorer;
+
if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
return 0;
}
@@ -355,6 +357,8 @@
}
int __android_log_print(int prio, const char* tag, const char* fmt, ...) {
+ ErrnoRestorer errno_restorer;
+
if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
return 0;
}
@@ -373,6 +377,8 @@
}
int __android_log_buf_print(int bufID, int prio, const char* tag, const char* fmt, ...) {
+ ErrnoRestorer errno_restorer;
+
if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
return 0;
}
@@ -419,6 +425,8 @@
}
int __android_log_bwrite(int32_t tag, const void* payload, size_t len) {
+ ErrnoRestorer errno_restorer;
+
struct iovec vec[2];
vec[0].iov_base = &tag;
@@ -430,6 +438,8 @@
}
int __android_log_stats_bwrite(int32_t tag, const void* payload, size_t len) {
+ ErrnoRestorer errno_restorer;
+
struct iovec vec[2];
vec[0].iov_base = &tag;
@@ -441,6 +451,8 @@
}
int __android_log_security_bwrite(int32_t tag, const void* payload, size_t len) {
+ ErrnoRestorer errno_restorer;
+
struct iovec vec[2];
vec[0].iov_base = &tag;
@@ -457,6 +469,8 @@
* handy if we just want to dump an integer into the log.
*/
int __android_log_btwrite(int32_t tag, char type, const void* payload, size_t len) {
+ ErrnoRestorer errno_restorer;
+
struct iovec vec[3];
vec[0].iov_base = &tag;
@@ -474,6 +488,8 @@
* event log.
*/
int __android_log_bswrite(int32_t tag, const char* payload) {
+ ErrnoRestorer errno_restorer;
+
struct iovec vec[4];
char type = EVENT_TYPE_STRING;
uint32_t len = strlen(payload);
@@ -495,6 +511,8 @@
* security log.
*/
int __android_log_security_bswrite(int32_t tag, const char* payload) {
+ ErrnoRestorer errno_restorer;
+
struct iovec vec[4];
char type = EVENT_TYPE_STRING;
uint32_t len = strlen(payload);