Merge "liblog: always restore errno in logging functions"
This commit is contained in:
commit
077839ab23
3 changed files with 69 additions and 29 deletions
42
base/include/android-base/errno_restorer.h
Normal file
42
base/include/android-base/errno_restorer.h
Normal file
|
|
@ -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
|
||||
|
|
@ -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 @@ void SetLogger(LogFunction&& logger);
|
|||
// 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.
|
||||
|
|
|
|||
|
|
@ -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 @@ void __android_log_call_aborter(const char* abort_message) {
|
|||
|
||||
#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 @@ static int write_to_log(log_id_t log_id, struct iovec* vec, size_t nr) {
|
|||
|
||||
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 @@ static int write_to_log(log_id_t log_id, struct iovec* vec, size_t nr) {
|
|||
ret = LogdWrite(log_id, &ts, vec, nr);
|
||||
PmsgWrite(log_id, &ts, vec, nr);
|
||||
|
||||
errno = save_errno;
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
|
|
@ -313,6 +309,8 @@ int __android_log_write(int prio, const char* tag, const char* msg) {
|
|||
}
|
||||
|
||||
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 @@ void __android_log_write_logger_data(__android_logger_data* logger_data, const c
|
|||
}
|
||||
|
||||
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_buf_write(int bufID, int prio, const char* tag, const char* ms
|
|||
}
|
||||
|
||||
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_vprint(int prio, const char* tag, const char* fmt, va_list ap)
|
|||
}
|
||||
|
||||
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_print(int prio, const char* tag, const char* fmt, ...) {
|
|||
}
|
||||
|
||||
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 @@ void __android_log_assert(const char* cond, const char* tag, const char* fmt, ..
|
|||
}
|
||||
|
||||
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_bwrite(int32_t tag, const void* payload, size_t len) {
|
|||
}
|
||||
|
||||
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_stats_bwrite(int32_t tag, const void* payload, size_t len) {
|
|||
}
|
||||
|
||||
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 @@ int __android_log_security_bwrite(int32_t tag, const void* payload, size_t len)
|
|||
* 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 @@ int __android_log_btwrite(int32_t tag, char type, const void* payload, size_t le
|
|||
* 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 @@ int __android_log_bswrite(int32_t tag, const char* payload) {
|
|||
* 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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue