之前我们分析了如何修改版本使log机制变成kernel的log机制。这篇博客我们继续修改可以动态切换,这样方便平时log丢失时调试。
我们先来看liblog库的编译mk文件,这个文件中主要修改了对使用使用logd 使用不同文件进行编译这块做了修改,增加了log_write_common.c 和log_read_common.c两个通用文件。
- #
- # Copyright (C) 2008-2014 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.
- #
- LOCAL_PATH := $(my-dir)
- include $(CLEAR_VARS)
- # This is what we want to do:
- # liblog_cflags := $(shell \
- # sed -n \
- # 's/^ [0−9]∗ [ \t]*liblog[ \t].*/-DLIBLOG_LOG_TAG=\1/p' \
- # $(LOCAL_PATH)/event.logtags)
- # so make sure we do not regret hard-coding it as follows:
- liblog_cflags := -DLIBLOG_LOG_TAG=1005
- #ifneq ($(TARGET_USES_LOGD),false)//注释了 不需要区分使用使用logd
- #liblog_sources := logd_write.c log_event_write.c
- #else
- #liblog_sources := logd_write_kern.c log_event_write.c
- #endif
- liblog_sources := log_write_common.c log_event_write.c//增加log_write_common.c
- # some files must not be compiled when building against Mingw
- # they correspond to features not used by our host development tools
- # which are also hard or even impossible to port to native Win32
- ifeq ($(strip $(USE_MINGW)),)
- liblog_sources += \
- event_tag_map.c
- else
- liblog_sources += \
- uio.c
- endif
- liblog_host_sources := $(liblog_sources) fake_log_device.c event.logtags
- liblog_target_sources := $(liblog_sources) log_time.cpp log_is_loggable.c
- ifeq ($(strip $(USE_MINGW)),)
- liblog_target_sources += logprint.c
- endif
- #ifneq ($(TARGET_USES_LOGD),false)//注释了,不用区分是否使用logd
- #liblog_target_sources += log_read.c
- #else
- #liblog_target_sources += log_read_kern.c
- #endif
- liblog_target_sources += log_read_common.c//增加log_read_common.c
- # Shared and static library for host
- # ========================================================
- LOCAL_MODULE := liblog
- LOCAL_SRC_FILES := $(liblog_host_sources)
- LOCAL_CFLAGS := -DFAKE_LOG_DEVICE=1 -Werror $(liblog_cflags)
- LOCAL_MULTILIB := both
- include $(BUILD_HOST_STATIC_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := liblog
- LOCAL_WHOLE_STATIC_LIBRARIES := liblog
- ifeq ($(strip $(HOST_OS)),linux)
- LOCAL_LDLIBS := -lrt
- endif
- LOCAL_MULTILIB := both
- include $(BUILD_HOST_SHARED_LIBRARY)
- # Shared and static library for target
- # ========================================================
- include $(CLEAR_VARS)
- LOCAL_MODULE := liblog
- LOCAL_SRC_FILES := $(liblog_target_sources)
- LOCAL_CFLAGS := -Werror $(liblog_cflags)
- include $(BUILD_STATIC_LIBRARY)
- include $(CLEAR_VARS)
- LOCAL_MODULE := liblog
- LOCAL_WHOLE_STATIC_LIBRARIES := liblog
- LOCAL_CFLAGS := -Werror $(liblog_cflags)
- # TODO: This is to work around b/19059885. Remove after root cause is fixed
- LOCAL_LDFLAGS_arm := -Wl,--hash-style=both
- include $(BUILD_SHARED_LIBRARY)
- include $(call first-makefiles-under,$(LOCAL_PATH))
我们先来看log_read_common.c文件,这样主要就是将原来的logd_write.c 和logd_write_kern.c 文件进行合并。一些对外的接口,统一使用use_logd这个全局变量来控制,选择logd的函数,还是kernel的函数(logd的函数和kernel的函数就是static函数了,不用对外)。另外在__write_to_log_init函数中,第一次初始化的使用,先看system/etc/disable_logd是否有这样一个文件,有就将use_logd为0,代表使用kernel机制。
- /*
- * Copyright (C) 2007-2014 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.
- */
- #if (FAKE_LOG_DEVICE == 0)
- #include <endian.h>
- #endif
- #include <errno.h>
- #include <fcntl.h>
- #if !defined(_WIN32)
- #include <pthread.h>
- #endif
- #include <stdarg.h>
- #include <stdatomic.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #if (FAKE_LOG_DEVICE == 0)
- #include <sys/socket.h>
- #include <sys/un.h>
- #endif
- #include <time.h>
- #include <unistd.h>
- #ifdef __BIONIC__
- #include <android/set_abort_message.h>
- #endif
- #include <log/log.h>
- #include <log/logd.h>
- #include <log/logger.h>
- #include <log/log_read.h>
- #include <private/android_filesystem_config.h>
- #include <private/android_logger.h>
- #define LOGGER_LOG_MAIN "log/main"
- #define LOGGER_LOG_RADIO "log/radio"
- #define LOGGER_LOG_EVENTS "log/events"
- #define LOGGER_LOG_SYSTEM "log/system"
- #define LOG_BUF_SIZE 1024
- #if FAKE_LOG_DEVICE
- /* This will be defined when building for the host. */
- #include "fake_log_device.h"
- #endif
- #define log_open(pathname, flags) open(pathname, (flags) | O_CLOEXEC)
- #define log_writev(filedes, vector, count) writev(filedes, vector, count)
- #define log_close(filedes) close(filedes)
- static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
- static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;
- #if !defined(_WIN32)
- static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
- #endif
- #ifndef __unused
- #define __unused __attribute__((__unused__))
- #endif
- static int8_t use_logd = 1;//做区分
- #if FAKE_LOG_DEVICE
- static int log_fds_fake[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1 };
- #else
- static int logd_fd = -1;
- static int pstore_fd = -1;
- #endif
- static int log_fds_kernel[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
- /*
- * This is used by the C++ code to decide if it should write logs through
- * the C code. Basically, if /dev/socket/logd is available, we're running in
- * the simulator rather than a desktop tool and want to use the device.
- */
- static enum {
- kLogUninitialized, kLogNotAvailable, kLogAvailable
- } g_log_status = kLogUninitialized;
- static int __android_log_dev_available_logd(void)
- {
- if (g_log_status == kLogUninitialized) {
- if (access("/dev/socket/logdw", W_OK) == 0)
- g_log_status = kLogAvailable;
- else
- g_log_status = kLogNotAvailable;
- }
- return (g_log_status == kLogAvailable);
- }
- static int __android_log_dev_available_kernel(void)
- {
- if (g_log_status == kLogUninitialized) {
- if (access("/dev/"LOGGER_LOG_MAIN, W_OK) == 0)
- g_log_status = kLogAvailable;
- else
- g_log_status = kLogNotAvailable;
- }
- return (g_log_status == kLogAvailable);
- }
- int __android_log_dev_available(void)
- {
- if (use_logd) {
- return __android_log_dev_available_logd();
- } else {
- return __android_log_dev_available_kernel();
- }
- }
- /* log_init_lock assumed */
- static int __write_to_log_initialize()
- {
- int i, ret = 0;
- #if FAKE_LOG_DEVICE
- for (i = 0; i < LOG_ID_MAX; i++) {
- char buf[sizeof("/dev/log_system")];
- snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
- log_fds_fake[i] = fakeLogOpen(buf, O_WRONLY);
- }
- #else
- if (pstore_fd < 0) {
- pstore_fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY));
- }
- if (logd_fd < 0) {
- i = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0));
- if (i < 0) {
- ret = -errno;
- } else if (TEMP_FAILURE_RETRY(fcntl(i, F_SETFL, O_NONBLOCK)) < 0) {
- ret = -errno;
- close(i);
- } else {
- struct sockaddr_un un;
- memset(&un, 0, sizeof(struct sockaddr_un));
- un.sun_family = AF_UNIX;
- strcpy(un.sun_path, "/dev/socket/logdw");
- if (TEMP_FAILURE_RETRY(connect(i, (struct sockaddr *)&un,
- sizeof(struct sockaddr_un))) < 0) {
- ret = -errno;
- close(i);
- } else {
- logd_fd = i;
- }
- }
- }
- #endif
- return ret;
- }
- static int __write_to_log_daemon(log_id_t log_id, struct iovec *vec, size_t nr)
- {
- ssize_t ret;
- #if FAKE_LOG_DEVICE
- int log_fd;
- if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
- log_fd = log_fds_fake[(int)log_id];
- } else {
- return -EBADF;
- }
- do {
- ret = fakeLogWritev(log_fd, vec, nr);
- if (ret < 0) {
- ret = -errno;
- }
- } while (ret == -EINTR);
- #else
- static const unsigned header_length = 2;
- struct iovec newVec[nr + header_length];
- android_log_header_t header;
- android_pmsg_log_header_t pmsg_header;
- struct timespec ts;
- size_t i, payload_size;
- static uid_t last_uid = AID_ROOT; /* logd *always* starts up as AID_ROOT */
- static pid_t last_pid = (pid_t) -1;
- static atomic_int_fast32_t dropped;
- if (!nr) {
- return -EINVAL;
- }
- if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */
- last_uid = getuid();
- }
- if (last_pid == (pid_t) -1) {
- last_pid = getpid();
- }
- /*
- * struct {
- * // what we provide to pstore
- * android_pmsg_log_header_t pmsg_header;
- * // what we provide to socket
- * android_log_header_t header;
- * // caller provides
- * union {
- * struct {
- * char prio;
- * char payload[];
- * } string;
- * struct {
- * uint32_t tag
- * char payload[];
- * } binary;
- * };
- * };
- */
- clock_gettime(CLOCK_REALTIME, &ts);
- pmsg_header.magic = LOGGER_MAGIC;
- pmsg_header.len = sizeof(pmsg_header) + sizeof(header);
- pmsg_header.uid = last_uid;
- pmsg_header.pid = last_pid;
- header.tid = gettid();
- header.realtime.tv_sec = ts.tv_sec;
- header.realtime.tv_nsec = ts.tv_nsec;
- newVec[0].iov_base = (unsigned char *) &pmsg_header;
- newVec[0].iov_len = sizeof(pmsg_header);
- newVec[1].iov_base = (unsigned char *) &header;
- newVec[1].iov_len = sizeof(header);
- if (logd_fd > 0) {
- int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
- if (snapshot) {
- android_log_event_int_t buffer;
- header.id = LOG_ID_EVENTS;
- buffer.header.tag = htole32(LIBLOG_LOG_TAG);
- buffer.payload.type = EVENT_TYPE_INT;
- buffer.payload.data = htole32(snapshot);
- newVec[2].iov_base = &buffer;
- newVec[2].iov_len = sizeof(buffer);
- ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2));
- if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
- atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
- }
- }
- }
- header.id = log_id;
- for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
- newVec[i].iov_base = vec[i - header_length].iov_base;
- payload_size += newVec[i].iov_len = vec[i - header_length].iov_len;
- if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) {
- newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD;
- if (newVec[i].iov_len) {
- ++i;
- }
- payload_size = LOGGER_ENTRY_MAX_PAYLOAD;
- break;
- }
- }
- pmsg_header.len += payload_size;
- if (pstore_fd >= 0) {
- TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i));
- }
- if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
- /*
- * ignore log messages we send to ourself (logd).
- * Such log messages are often generated by libraries we depend on
- * which use standard Android logging.
- */
- return 0;
- }
- if (logd_fd < 0) {
- return -EBADF;
- }
- /*
- * The write below could be lost, but will never block.
- *
- * To logd, we drop the pmsg_header
- *
- * ENOTCONN occurs if logd dies.
- * EAGAIN occurs if logd is overloaded.
- */
- ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
- if (ret < 0) {
- ret = -errno;
- if (ret == -ENOTCONN) {
- #if !defined(_WIN32)
- pthread_mutex_lock(&log_init_lock);
- #endif
- close(logd_fd);
- logd_fd = -1;
- ret = __write_to_log_initialize();
- #if !defined(_WIN32)
- pthread_mutex_unlock(&log_init_lock);
- #endif
- if (ret < 0) {
- return ret;
- }
- ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
- if (ret < 0) {
- ret = -errno;
- }
- }
- }
- if (ret > (ssize_t)sizeof(header)) {
- ret -= sizeof(header);
- } else if (ret == -EAGAIN) {
- atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
- }
- #endif
- return ret;
- }
- #if FAKE_LOG_DEVICE
- static const char *LOG_NAME[LOG_ID_MAX] = {
- [LOG_ID_MAIN] = "main",
- [LOG_ID_RADIO] = "radio",
- [LOG_ID_EVENTS] = "events",
- [LOG_ID_SYSTEM] = "system",
- [LOG_ID_CRASH] = "crash",
- [LOG_ID_KERNEL] = "kernel",
- };
- const char *android_log_id_to_name(log_id_t log_id)
- {
- if (log_id >= LOG_ID_MAX) {
- log_id = LOG_ID_MAIN;
- }
- return LOG_NAME[log_id];
- }
- #endif
- static int __write_to_log_init_logd(log_id_t log_id, struct iovec *vec, size_t nr)
- {
- #if !defined(_WIN32)
- pthread_mutex_lock(&log_init_lock);
- #endif
- if (write_to_log == __write_to_log_init) {
- int ret;
- ret = __write_to_log_initialize();
- if (ret < 0) {
- #if !defined(_WIN32)
- pthread_mutex_unlock(&log_init_lock);
- #endif
- #if (FAKE_LOG_DEVICE == 0)
- if (pstore_fd >= 0) {
- __write_to_log_daemon(log_id, vec, nr);
- }
- #endif
- return ret;
- }
- write_to_log = __write_to_log_daemon;
- }
- #if !defined(_WIN32)
- pthread_mutex_unlock(&log_init_lock);
- #endif
- return write_to_log(log_id, vec, nr);
- }
- static int __write_to_log_null(log_id_t log_fd __unused, struct iovec *vec __unused,
- size_t nr __unused)
- {
- return -1;
- }
- static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
- {
- ssize_t ret;
- int log_fd;
- if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
- if (log_id == LOG_ID_CRASH) {
- log_id = LOG_ID_MAIN;
- }
- log_fd = log_fds_kernel[(int)log_id];
- } else {
- return -EBADF;
- }
- do {
- ret = log_writev(log_fd, vec, nr);
- if (ret < 0) {
- ret = -errno;
- }
- } while (ret == -EINTR);
- return ret;
- }
- static int __write_to_log_init_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
- {
- pthread_mutex_lock(&log_init_lock);
- if (write_to_log == __write_to_log_init) {
- log_fds_kernel[LOG_ID_MAIN] = log_open("/dev/"LOGGER_LOG_MAIN, O_WRONLY);
- log_fds_kernel[LOG_ID_RADIO] = log_open("/dev/"LOGGER_LOG_RADIO, O_WRONLY);
- log_fds_kernel[LOG_ID_EVENTS] = log_open("/dev/"LOGGER_LOG_EVENTS, O_WRONLY);
- log_fds_kernel[LOG_ID_SYSTEM] = log_open("/dev/"LOGGER_LOG_SYSTEM, O_WRONLY);
- write_to_log = __write_to_log_kernel;
- if (log_fds_kernel[LOG_ID_MAIN] < 0 || log_fds_kernel[LOG_ID_RADIO] < 0 ||
- log_fds_kernel[LOG_ID_EVENTS] < 0) {
- log_close(log_fds_kernel[LOG_ID_MAIN]);
- log_close(log_fds_kernel[LOG_ID_RADIO]);
- log_close(log_fds_kernel[LOG_ID_EVENTS]);
- log_fds_kernel[LOG_ID_MAIN] = -1;
- log_fds_kernel[LOG_ID_RADIO] = -1;
- log_fds_kernel[LOG_ID_EVENTS] = -1;
- write_to_log = __write_to_log_null;
- }
- if (log_fds_kernel[LOG_ID_SYSTEM] < 0) {
- log_fds_kernel[LOG_ID_SYSTEM] = log_fds_kernel[LOG_ID_MAIN];
- }
- }
- pthread_mutex_unlock(&log_init_lock);
- return write_to_log(log_id, vec, nr);
- }
- static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr) {
- #if (FAKE_LOG_DEVICE == 0)
- int fd;
- fd = open("system/etc/disable_logd", O_RDONLY);//是否有disable_logd文件
- if (fd > 0) {
- use_logd = 0;
- close(fd);
- }
- #endif
- if(use_logd) {
- return __write_to_log_init_logd(log_id, vec, nr);
- } else {
- return __write_to_log_init_kernel(log_id, vec, nr);
- }
- }
- int __android_log_write(int prio, const char *tag, const char *msg)
- {
- return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
- }
- int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
- {
- struct iovec vec[3];
- char tmp_tag[32];
- if (!tag)
- tag = "";
- /* XXX: This needs to go! */
- if ((bufID != LOG_ID_RADIO) &&
- (!strcmp(tag, "HTC_RIL") ||
- !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
- !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
- !strcmp(tag, "AT") ||
- !strcmp(tag, "GSM") ||
- !strcmp(tag, "STK") ||
- !strcmp(tag, "CDMA") ||
- !strcmp(tag, "PHONE") ||
- !strcmp(tag, "SMS"))) {
- bufID = LOG_ID_RADIO;
- /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
- snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
- tag = tmp_tag;
- }
- #if __BIONIC__
- if (prio == ANDROID_LOG_FATAL) {
- android_set_abort_message(msg);
- }
- #endif
- vec[0].iov_base = (unsigned char *) &prio;
- vec[0].iov_len = 1;
- vec[1].iov_base = (void *) tag;
- vec[1].iov_len = strlen(tag) + 1;
- vec[2].iov_base = (void *) msg;
- vec[2].iov_len = strlen(msg) + 1;
- return write_to_log(bufID, vec, 3);
- }
- int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
- {
- char buf[LOG_BUF_SIZE];
- vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
- return __android_log_write(prio, tag, buf);
- }
- int __android_log_print(int prio, const char *tag, const char *fmt, ...)
- {
- va_list ap;
- char buf[LOG_BUF_SIZE];
- va_start(ap, fmt);
- vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
- va_end(ap);
- return __android_log_write(prio, tag, buf);
- }
- int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
- {
- va_list ap;
- char buf[LOG_BUF_SIZE];
- va_start(ap, fmt);
- vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
- va_end(ap);
- return __android_log_buf_write(bufID, prio, tag, buf);
- }
- void __android_log_assert(const char *cond, const char *tag,
- const char *fmt, ...)
- {
- char buf[LOG_BUF_SIZE];
- if (fmt) {
- va_list ap;
- va_start(ap, fmt);
- vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
- va_end(ap);
- } else {
- /* Msg not provided, log condition. N.B. Do not use cond directly as
- * format string as it could contain spurious '%' syntax (e.g.
- * "%d" in "blocks%devs == 0").
- */
- if (cond)
- snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
- else
- strcpy(buf, "Unspecified assertion failed");
- }
- __android_log_write(ANDROID_LOG_FATAL, tag, buf);
- abort(); /* abort so we have a chance to debug the situation */
- /* NOTREACHED */
- }
- int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
- {
- struct iovec vec[2];
- vec[0].iov_base = &tag;
- vec[0].iov_len = sizeof(tag);
- vec[1].iov_base = (void*)payload;
- vec[1].iov_len = len;
- return write_to_log(LOG_ID_EVENTS, vec, 2);
- }
- /*
- * Like __android_log_bwrite, but takes the type as well. Doesn't work
- * for the general case where we're generating lists of stuff, but very
- * 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)
- {
- struct iovec vec[3];
- vec[0].iov_base = &tag;
- vec[0].iov_len = sizeof(tag);
- vec[1].iov_base = &type;
- vec[1].iov_len = sizeof(type);
- vec[2].iov_base = (void*)payload;
- vec[2].iov_len = len;
- return write_to_log(LOG_ID_EVENTS, vec, 3);
- }
- /*
- * Like __android_log_bwrite, but used for writing strings to the
- * event log.
- */
- int __android_log_bswrite(int32_t tag, const char *payload)
- {
- struct iovec vec[4];
- char type = EVENT_TYPE_STRING;
- uint32_t len = strlen(payload);
- vec[0].iov_base = &tag;
- vec[0].iov_len = sizeof(tag);
- vec[1].iov_base = &type;
- vec[1].iov_len = sizeof(type);
- vec[2].iov_base = &len;
- vec[2].iov_len = sizeof(len);
- vec[3].iov_base = (void*)payload;
- vec[3].iov_len = len;
- return write_to_log(LOG_ID_EVENTS, vec, 4);
- }
另外一个就是log_read_common.c文件,将log_read.c和log_read_kern.c合并。这样文件稍微复杂一点,因为在log_read.c和log_read_kern.c中都使用了struct logger_list 和struct logger结构体而且不一样,这样我们将共同部分做了一个基类的struct logger_list 和struct logger结构体。而在合并文件中定义了struct logger_list_logd struct logger_list_kernel结构体,将其基类放在第一个变量base中。而使用指针时,对外返回的指针强制转成基类,进入的指针强制转到相关的子类。因为基类的数据结构在子类结构体中是第一个,因此子类指针强转成父类,也是能正确调用到父类的变量的。
还有我们也是用use_logd这个全局变量来控制log机制的,并且定义了一个对外的函数set_uselogd. 因为使用read这块是在logcat进程中使用的,我们需要在logcat中调用。
- /*
- ** Copyright 2013-2014, 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.
- */
- #define _GNU_SOURCE /* asprintf for x86 host */
- #include <errno.h>
- #include <fcntl.h>
- #include <inttypes.h>
- #include <poll.h>
- #include <signal.h>
- #include <stdbool.h>
- #include <stddef.h>
- #define NOMINMAX /* for windows to suppress definition of min in stdlib.h */
- #include <stdlib.h>
- #include <string.h>
- #include <sys/cdefs.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdio.h>
- #include <cutils/list.h>
- #include <cutils/sockets.h>
- #include <log/log.h>
- #include <log/logger.h>
- #include <private/android_filesystem_config.h>
- #include <private/android_logger.h>
- #include <cutils/sockets.h>
- #define __LOGGERIO 0xAE
- #define LOGGER_GET_LOG_BUF_SIZE _IO(__LOGGERIO, 1) /* size of log */
- #define LOGGER_GET_LOG_LEN _IO(__LOGGERIO, 2) /* used log len */
- #define LOGGER_GET_NEXT_ENTRY_LEN _IO(__LOGGERIO, 3) /* next entry len */
- #define LOGGER_FLUSH_LOG _IO(__LOGGERIO, 4) /* flush log */
- #define LOGGER_GET_VERSION _IO(__LOGGERIO, 5) /* abi version */
- #define LOGGER_SET_VERSION _IO(__LOGGERIO, 6) /* abi version */
- #define LOG_FILE_DIR "/dev/log/"
- /* timeout in milliseconds */
- #define LOG_TIMEOUT_FLUSH 5
- #define LOG_TIMEOUT_NEVER -1
- /* branchless on many architectures. */
- #define min(x,y) ((y) ^ (((x) ^ (y)) & -((x) < (y))))
- #if (defined(USE_MINGW) || defined(HAVE_WINSOCK))
- #define WEAK static
- #else
- #define WEAK __attribute__((weak))
- #endif
- #ifndef __unused
- #define __unused __attribute__((unused))
- #endif
- static int8_t use_logd = 1;//版本控制
- void set_uselogd(int8_t uselogd) {//对外函数
- use_logd = uselogd;
- }
- /* Private copy of ../libcutils/socket_local_client.c prevent library loops */
- #ifdef HAVE_WINSOCK
- int WEAK socket_local_client(const char *name, int namespaceId, int type)
- {
- errno = ENOSYS;
- return -ENOSYS;
- }
- #else /* !HAVE_WINSOCK */
- #include <sys/socket.h>
- #include <sys/un.h>
- #include <sys/select.h>
- #include <sys/types.h>
- /* Private copy of ../libcutils/socket_local.h prevent library loops */
- #define FILESYSTEM_SOCKET_PREFIX "/tmp/"
- #define ANDROID_RESERVED_SOCKET_PREFIX "/dev/socket/"
- /* End of ../libcutils/socket_local.h */
- #define LISTEN_BACKLOG 4
- /* Documented in header file. */
- int WEAK socket_make_sockaddr_un(const char *name, int namespaceId,
- struct sockaddr_un *p_addr, socklen_t *alen)
- {
- memset (p_addr, 0, sizeof (*p_addr));
- size_t namelen;
- switch (namespaceId) {
- case ANDROID_SOCKET_NAMESPACE_ABSTRACT:
- #if defined(__linux__)
- namelen = strlen(name);
- /* Test with length +1 for the *initial* '\0'. */
- if ((namelen + 1) > sizeof(p_addr->sun_path)) {
- goto error;
- }
- /*
- * Note: The path in this case is *not* supposed to be
- * '\0'-terminated. ("man 7 unix" for the gory details.)
- */
- p_addr->sun_path[0] = 0;
- memcpy(p_addr->sun_path + 1, name, namelen);
- #else
- /* this OS doesn't have the Linux abstract namespace */
- namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX);
- /* unix_path_max appears to be missing on linux */
- if (namelen > sizeof(*p_addr)
- - offsetof(struct sockaddr_un, sun_path) - 1) {
- goto error;
- }
- strcpy(p_addr->sun_path, FILESYSTEM_SOCKET_PREFIX);
- strcat(p_addr->sun_path, name);
- #endif
- break;
- case ANDROID_SOCKET_NAMESPACE_RESERVED:
- namelen = strlen(name) + strlen(ANDROID_RESERVED_SOCKET_PREFIX);
- /* unix_path_max appears to be missing on linux */
- if (namelen > sizeof(*p_addr)
- - offsetof(struct sockaddr_un, sun_path) - 1) {
- goto error;
- }
- strcpy(p_addr->sun_path, ANDROID_RESERVED_SOCKET_PREFIX);
- strcat(p_addr->sun_path, name);
- break;
- case ANDROID_SOCKET_NAMESPACE_FILESYSTEM:
- namelen = strlen(name);
- /* unix_path_max appears to be missing on linux */
- if (namelen > sizeof(*p_addr)
- - offsetof(struct sockaddr_un, sun_path) - 1) {
- goto error;
- }
- strcpy(p_addr->sun_path, name);
- break;
- default:
- /* invalid namespace id */
- return -1;
- }
- p_addr->sun_family = AF_LOCAL;
- *alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
- return 0;
- error:
- return -1;
- }
- /**
- * connect to peer named "name" on fd
- * returns same fd or -1 on error.
- * fd is not closed on error. that's your job.
- *
- * Used by AndroidSocketImpl
- */
- int WEAK socket_local_client_connect(int fd, const char *name, int namespaceId,
- int type __unused)
- {
- struct sockaddr_un addr;
- socklen_t alen;
- int err;
- err = socket_make_sockaddr_un(name, namespaceId, &addr, &alen);
- if (err < 0) {
- goto error;
- }
- if(connect(fd, (struct sockaddr *) &addr, alen) < 0) {
- goto error;
- }
- return fd;
- error:
- return -1;
- }
- /**
- * connect to peer named "name"
- * returns fd or -1 on error
- */
- int WEAK socket_local_client(const char *name, int namespaceId, int type)
- {
- int s;
- s = socket(AF_LOCAL, type, 0);
- if(s < 0) return -1;
- if ( 0 > socket_local_client_connect(s, name, namespaceId, type)) {
- close(s);
- return -1;
- }
- return s;
- }
- #endif /* !HAVE_WINSOCK */
- /* End of ../libcutils/socket_local_client.c */
- #define logger_for_each(logger, logger_list) \
- for (logger = node_to_item((logger_list)->node.next, struct logger, node); \
- logger != node_to_item(&(logger_list)->node, struct logger, node); \
- logger = node_to_item((logger)->node.next, struct logger, node))
- #ifndef __unused
- #define __unused __attribute__((unused))
- #endif
- /* In the future, we would like to make this list extensible */
- static const char *LOG_NAME[LOG_ID_MAX] = {
- [LOG_ID_MAIN] = "main",
- [LOG_ID_RADIO] = "radio",
- [LOG_ID_EVENTS] = "events",
- [LOG_ID_SYSTEM] = "system",
- [LOG_ID_CRASH] = "crash",
- [LOG_ID_KERNEL] = "kernel",
- };
- const char *android_log_id_to_name(log_id_t log_id)
- {
- if (log_id >= LOG_ID_MAX) {
- log_id = LOG_ID_MAIN;
- }
- return LOG_NAME[log_id];
- }
- static int accessmode(int mode)
- {
- if ((mode & ANDROID_LOG_ACCMODE) == ANDROID_LOG_WRONLY) {
- return W_OK;
- }
- if ((mode & ANDROID_LOG_ACCMODE) == ANDROID_LOG_RDWR) {
- return R_OK | W_OK;
- }
- return R_OK;
- }
- /* repeated fragment */
- static int check_allocate_accessible(char **n, const char *b, int mode)
- {
- *n = NULL;
- if (!b) {
- return -EINVAL;
- }
- asprintf(n, LOG_FILE_DIR "%s", b);
- if (!*n) {
- return -1;
- }
- return access(*n, accessmode(mode));
- }
- log_id_t android_name_to_log_id_logd(const char *logName)
- {
- const char *b;
- int ret;
- if (!logName) {
- return -1; /* NB: log_id_t is unsigned */
- }
- b = strrchr(logName, '/');
- if (!b) {
- b = logName;
- } else {
- ++b;
- }
- for(ret = LOG_ID_MIN; ret < LOG_ID_MAX; ++ret) {
- const char *l = LOG_NAME[ret];
- if (l && !strcmp(b, l)) {
- return ret;
- }
- }
- return -1; /* should never happen */
- }
- log_id_t android_name_to_log_id_kernel(const char *logName)
- {
- const char *b;
- char *n;
- int ret;
- if (!logName) {
- return -1; /* NB: log_id_t is unsigned */
- }
- b = strrchr(logName, '/');
- if (!b) {
- b = logName;
- } else {
- ++b;
- }
- ret = check_allocate_accessible(&n, b, ANDROID_LOG_RDONLY);
- free(n);
- if (ret) {
- return ret;
- }
- for(ret = LOG_ID_MIN; ret < LOG_ID_MAX; ++ret) {
- const char *l = LOG_NAME[ret];
- if (l && !strcmp(b, l)) {
- return ret;
- }
- }
- return -1; /* should never happen */
- }
- log_id_t android_name_to_log_id(const char *logName) {
- if (use_logd) {
- return android_name_to_log_id_logd(logName);
- } else {
- return android_name_to_log_id_kernel(logName);
- }
- }
- struct logger_list {//基类结构体
- struct listnode node;
- int mode;
- unsigned int tail;
- pid_t pid;
- };
- struct logger_list_logd {//logd机制结构体
- struct logger_list base;
- log_time start;
- int sock;
- };
- struct logger_list_kernel {//kernel机制结构体
- struct logger_list base;
- unsigned int queued_lines;
- int timeout_ms;
- int error;
- bool flush;
- bool valid_entry; /* valiant(?) effort to deal with memory starvation */
- struct log_msg entry;
- };
- struct logger {
- struct listnode node;
- struct logger_list *top;
- log_id_t id;
- };
- struct log_list {
- struct listnode node;
- struct log_msg entry; /* Truncated to event->len() + 1 to save space */
- };
- struct logger_kernel {
- struct logger base;
- int fd;
- short *revents;
- struct listnode log_list;
- };
- /* android_logger_alloc unimplemented, no use case */
- /* android_logger_free not exported */
- static void android_logger_free_logd(struct logger *logger)
- {
- if (!logger) {
- return;
- }
- list_remove(&logger->node);
- free(logger);
- }
- /* android_logger_alloc unimplemented, no use case */
- /* android_logger_free not exported */
- static void android_logger_free_kernel(struct logger *logger)
- {
- if (!logger) {
- return;
- }
- struct logger_kernel *logger_kernel = (struct logger_kernel*)logger;
- struct logger_list_kernel *logger_list_kernel;
- while (!list_empty(&logger_kernel->log_list)) {
- struct log_list *entry = node_to_item(
- list_head(&logger_kernel->log_list), struct log_list, node);
- list_remove(&entry->node);
- free(entry);
- logger_list_kernel = (struct logger_list_kernel *)(logger_kernel->base.top);
- if (logger_list_kernel->queued_lines) {
- logger_list_kernel->queued_lines--;
- }
- }
- if (logger_kernel->fd >= 0) {
- close(logger_kernel->fd);
- }
- list_remove(&logger_kernel->base.node);
- free(logger_kernel);
- }
- /* android_logger_alloc unimplemented, no use case */
- /* android_logger_free not exported */
- void android_logger_free(struct logger *logger)
- {
- if (use_logd) {
- android_logger_free_logd(logger);
- } else {
- android_logger_free_kernel(logger);
- }
- }
- /* android_logger_alloc unimplemented, no use case */
- /* method for getting the associated sublog id */
- log_id_t android_logger_get_id(struct logger *logger)
- {
- return logger->id;
- }
- /* worker for sending the command to the logger */
- static ssize_t send_log_msg(struct logger *logger,
- const char *msg, char *buf, size_t buf_size)
- {
- ssize_t ret;
- size_t len;
- char *cp;
- int errno_save = 0;
- int sock = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_RESERVED,
- SOCK_STREAM);
- if (sock < 0) {
- return sock;
- }
- if (msg) {
- snprintf(buf, buf_size, msg, logger ? logger->id : (unsigned) -1);
- }
- len = strlen(buf) + 1;
- ret = TEMP_FAILURE_RETRY(write(sock, buf, len));
- if (ret <= 0) {
- goto done;
- }
- len = buf_size;
- cp = buf;
- while ((ret = TEMP_FAILURE_RETRY(read(sock, cp, len))) > 0) {
- struct pollfd p;
- if (((size_t)ret == len) || (buf_size < PAGE_SIZE)) {
- break;
- }
- len -= ret;
- cp += ret;
- memset(&p, 0, sizeof(p));
- p.fd = sock;
- p.events = POLLIN;
- /* Give other side 20ms to refill pipe */
- ret = TEMP_FAILURE_RETRY(poll(&p, 1, 20));
- if (ret <= 0) {
- break;
- }
- if (!(p.revents & POLLIN)) {
- ret = 0;
- break;
- }
- }
- if (ret >= 0) {
- ret += buf_size - len;
- }
- done:
- if ((ret == -1) && errno) {
- errno_save = errno;
- }
- close(sock);
- if (errno_save) {
- errno = errno_save;
- }
- return ret;
- }
- /* worker for sending the command to the logger */
- static int logger_ioctl(struct logger *logger, int cmd, int mode)
- {
- char *n;
- int f, ret;
- struct logger_kernel* logger_kernel = (struct logger_kernel*)logger;
- if (!logger_kernel || !logger_kernel->base.top) {
- return -EFAULT;
- }
- if (((mode & ANDROID_LOG_ACCMODE) == ANDROID_LOG_RDWR)
- || (((mode ^ logger_kernel->base.top->mode) & ANDROID_LOG_ACCMODE) == 0)) {
- return ioctl(logger_kernel->fd, cmd);
- }
- /* We go here if android_logger_list_open got mode wrong for this ioctl */
- ret = check_allocate_accessible(&n, android_log_id_to_name(logger_kernel->base.id), mode);
- if (ret) {
- free(n);
- return ret;
- }
- f = open(n, mode);
- free(n);
- if (f < 0) {
- return f;
- }
- ret = ioctl(f, cmd);
- close (f);
- return ret;
- }
- static int check_log_success(char *buf, ssize_t ret)
- {
- if (ret < 0) {
- return ret;
- }
- if (strncmp(buf, "success", 7)) {
- errno = EINVAL;
- return -1;
- }
- return 0;
- }
- /* Determine the credentials of the caller */
- static bool uid_has_log_permission(uid_t uid)
- {
- return (uid == AID_SYSTEM) || (uid == AID_LOG) || (uid == AID_ROOT);
- }
- static uid_t get_best_effective_uid()
- {
- uid_t euid;
- uid_t uid;
- gid_t gid;
- ssize_t i;
- static uid_t last_uid = (uid_t) -1;
- if (last_uid != (uid_t) -1) {
- return last_uid;
- }
- uid = getuid();
- if (uid_has_log_permission(uid)) {
- return last_uid = uid;
- }
- euid = geteuid();
- if (uid_has_log_permission(euid)) {
- return last_uid = euid;
- }
- gid = getgid();
- if (uid_has_log_permission(gid)) {
- return last_uid = gid;
- }
- gid = getegid();
- if (uid_has_log_permission(gid)) {
- return last_uid = gid;
- }
- i = getgroups((size_t) 0, NULL);
- if (i > 0) {
- gid_t list[i];
- getgroups(i, list);
- while (--i >= 0) {
- if (uid_has_log_permission(list[i])) {
- return last_uid = list[i];
- }
- }
- }
- return last_uid = uid;
- }
- static int android_logger_clear_logd(struct logger *logger)
- {
- char buf[512];
- if (logger->top->mode & ANDROID_LOG_PSTORE) {
- if (uid_has_log_permission(get_best_effective_uid())) {
- return unlink("/sys/fs/pstore/pmsg-ramoops-0");
- }
- errno = EPERM;
- return -1;
- }
- return check_log_success(buf,
- send_log_msg(logger, "clear %d", buf, sizeof(buf)));
- }
- static int android_logger_clear_kernel(struct logger *logger)
- {
- return logger_ioctl(logger, LOGGER_FLUSH_LOG, ANDROID_LOG_WRONLY);
- }
- int android_logger_clear(struct logger *logger)
- {
- if (use_logd) {
- return android_logger_clear_logd(logger);
- } else {
- return android_logger_clear_kernel(logger);
- }
- }
- /* returns the total size of the log's ring buffer */
- static long android_logger_get_log_size_logd(struct logger *logger)
- {
- char buf[512];
- ssize_t ret = send_log_msg(logger, "getLogSize %d", buf, sizeof(buf));
- if (ret < 0) {
- return ret;
- }
- if ((buf[0] < '0') || ('9' < buf[0])) {
- return -1;
- }
- return atol(buf);
- }
- /* returns the total size of the log's ring buffer */
- static long android_logger_get_log_size_kernel(struct logger *logger)
- {
- return logger_ioctl(logger, LOGGER_GET_LOG_BUF_SIZE, ANDROID_LOG_RDWR);
- }
- /* returns the total size of the log's ring buffer */
- long android_logger_get_log_size(struct logger *logger)
- {
- if (use_logd) {
- return android_logger_get_log_size_logd(logger);
- } else {
- return android_logger_get_log_size_kernel(logger);
- }
- }
- int android_logger_set_log_size(struct logger *logger, unsigned long size)
- {
- if (use_logd) {
- char buf[512];
- snprintf(buf, sizeof(buf), "setLogSize %d %lu",
- logger ? logger->id : (unsigned) -1, size);
- return check_log_success(buf, send_log_msg(NULL, NULL, buf, sizeof(buf)));
- } else {
- return -ENOTSUP;
- }
- }
- /*
- * returns the readable size of the log's ring buffer (that is, amount of the
- * log consumed)
- */
- static long android_logger_get_log_readable_size_logd(struct logger *logger)
- {
- char buf[512];
- ssize_t ret = send_log_msg(logger, "getLogSizeUsed %d", buf, sizeof(buf));
- if (ret < 0) {
- return ret;
- }
- if ((buf[0] < '0') || ('9' < buf[0])) {
- return -1;
- }
- return atol(buf);
- }
- /*
- * returns the readable size of the log's ring buffer (that is, amount of the
- * log consumed)
- */
- static long android_logger_get_log_readable_size_kernel(struct logger *logger)
- {
- return logger_ioctl(logger, LOGGER_GET_LOG_LEN, ANDROID_LOG_RDONLY);
- }
- /*
- * returns the readable size of the log's ring buffer (that is, amount of the
- * log consumed)
- */
- long android_logger_get_log_readable_size(struct logger *logger)
- {
- if (use_logd) {
- return android_logger_get_log_readable_size_logd(logger);
- } else {
- return android_logger_get_log_readable_size_kernel(logger);
- }
- }
- /*
- * returns the logger version
- */
- int android_logger_get_log_version(struct logger *logger __unused)
- {
- if (use_logd) {
- return 3;
- } else {
- int ret = logger_ioctl(logger, LOGGER_GET_VERSION, ANDROID_LOG_RDWR);
- return (ret < 0) ? 1 : ret;
- }
- }
- /*
- * returns statistics
- */
- static const char unsupported[] = "18\nNot Supported\n\f";
- ssize_t android_logger_get_statistics(struct logger_list *logger_list,
- char *buf, size_t len)
- {
- if (use_logd) {
- struct logger *logger;
- char *cp = buf;
- size_t remaining = len;
- size_t n;
- n = snprintf(cp, remaining, "getStatistics");
- n = min(n, remaining);
- remaining -= n;
- cp += n;
- logger_for_each(logger, logger_list) {
- n = snprintf(cp, remaining, " %d", logger->id);
- n = min(n, remaining);
- remaining -= n;
- cp += n;
- }
- return send_log_msg(NULL, NULL, buf, len);
- } else {
- strncpy(buf, unsupported, len);
- return -ENOTSUP;
- }
- }
- ssize_t android_logger_get_prune_list(struct logger_list *logger_list __unused,
- char *buf, size_t len)
- {
- if (use_logd) {
- return send_log_msg(NULL, "getPruneList", buf, len);
- } else {
- strncpy(buf, unsupported, len);
- return -ENOTSUP;
- }
- }
- int android_logger_set_prune_list(struct logger_list *logger_list __unused,
- char *buf, size_t len)
- {
- if (use_logd) {
- const char cmd[] = "setPruneList ";
- const size_t cmdlen = sizeof(cmd) - 1;
- if (strlen(buf) > (len - cmdlen)) {
- return -ENOMEM; /* KISS */
- }
- memmove(buf + cmdlen, buf, len - cmdlen);
- buf[len - 1] = '\0';
- memcpy(buf, cmd, cmdlen);
- return check_log_success(buf, send_log_msg(NULL, NULL, buf, len));
- } else {
- static const char unsupported_error[] = "Unsupported";
- strncpy(buf, unsupported, len);
- return -ENOTSUP;
- }
- }
- static struct logger_list *android_logger_list_alloc_logd(int mode,
- unsigned int tail,
- pid_t pid)
- {
- struct logger_list_logd *logger_list_logd;
- logger_list_logd = calloc(1, sizeof(*logger_list_logd));
- if (!logger_list_logd) {
- return NULL;
- }
- list_init(&logger_list_logd->base.node);
- logger_list_logd->base.mode = mode;
- logger_list_logd->start.tv_sec = 0;
- logger_list_logd->start.tv_nsec = 0;
- logger_list_logd->base.tail = tail;
- logger_list_logd->base.pid = pid;
- logger_list_logd->sock = -1;
- return (struct logger_list *)logger_list_logd;
- }
- static struct logger_list *android_logger_list_alloc_kernel(int mode,
- unsigned int tail,
- pid_t pid)
- {
- struct logger_list_kernel *logger_list_kernel;
- logger_list_kernel = calloc(1, sizeof(*logger_list_kernel));
- if (!logger_list_kernel) {
- return NULL;
- }
- list_init(&logger_list_kernel->base.node);
- logger_list_kernel->base.mode = mode;
- logger_list_kernel->base.tail = tail;
- logger_list_kernel->base.pid = pid;
- return (struct logger_list *)logger_list_kernel;
- }
- struct logger_list *android_logger_list_alloc(int mode,
- unsigned int tail,
- pid_t pid)
- {
- if (use_logd) {
- return android_logger_list_alloc_logd(mode, tail, pid);
- } else {
- return android_logger_list_alloc_kernel(mode, tail, pid);
- }
- }
- static struct logger_list *android_logger_list_alloc_time_logd(int mode,
- log_time start,
- pid_t pid)
- {
- struct logger_list_logd *logger_list_logd;
- logger_list_logd = calloc(1, sizeof(*logger_list_logd));
- if (!logger_list_logd) {
- return NULL;
- }
- list_init(&logger_list_logd->base.node);
- logger_list_logd->base.mode = mode;
- logger_list_logd->start = start;
- logger_list_logd->base.tail = 0;
- logger_list_logd->base.pid = pid;
- logger_list_logd->sock = -1;
- return (struct logger_list *)logger_list_logd;
- }
- static struct logger_list *android_logger_list_alloc_time_kernel(int mode,
- log_time start __unused,
- pid_t pid)
- {
- return android_logger_list_alloc(mode, 0, pid);
- }
- struct logger_list *android_logger_list_alloc_time(int mode,
- log_time start,
- pid_t pid)
- {
- if(use_logd){
- return android_logger_list_alloc_time_logd(mode, start, pid);
- } else {
- return android_logger_list_alloc_time_kernel(mode, start, pid);
- }
- }
- /* android_logger_list_register unimplemented, no use case */
- /* android_logger_list_unregister unimplemented, no use case */
- /* Open the named log and add it to the logger list */
- static struct logger *android_logger_open_logd(struct logger_list *logger_list,
- log_id_t id)
- {
- struct logger *logger;
- if (!logger_list || (id >= LOG_ID_MAX)) {
- goto err;
- }
- logger_for_each(logger, logger_list) {
- if (logger->id == id) {
- goto ok;
- }
- }
- logger = calloc(1, sizeof(*logger));
- if (!logger) {
- goto err;
- }
- logger->id = id;
- list_add_tail(&logger_list->node, &logger->node);
- logger->top = logger_list;
- goto ok;
- err:
- logger = NULL;
- ok:
- return logger;
- }
- /* android_logger_list_register unimplemented, no use case */
- /* android_logger_list_unregister unimplemented, no use case */
- /* Open the named log and add it to the logger list */
- static struct logger *android_logger_open_kernel(struct logger_list *logger_list,
- log_id_t id)
- {
- struct listnode *node;
- struct logger_kernel *logger_kernel = NULL;
- struct logger *logger;
- char *n;
- struct logger_list_kernel* logger_list_kernel = (struct logger_list_kernel*)logger_list;
- if (!logger_list || (id >= LOG_ID_MAX)) {
- goto err;
- }
- logger_for_each(logger, logger_list) {
- if (logger->id == id) {
- goto ok;
- }
- }
- logger_kernel = calloc(1, sizeof(struct logger_kernel));
- if (!logger_kernel) {
- goto err;
- }
- if (check_allocate_accessible(&n, android_log_id_to_name(id),
- logger_list_kernel->base.mode)) {
- goto err_name;
- }
- logger_kernel->fd = open(n, logger_list_kernel->base.mode & (ANDROID_LOG_ACCMODE | ANDROID_LOG_NONBLOCK));
- if (logger_kernel->fd < 0) {
- goto err_name;
- }
- free(n);
- logger_kernel->base.id = id;
- list_init(&logger_kernel->log_list);
- list_add_tail(&logger_list_kernel->base.node, &logger_kernel->base.node);
- logger_kernel->base.top = (struct logger_list *)logger_list_kernel;
- logger_list_kernel->timeout_ms = LOG_TIMEOUT_FLUSH;
- goto ok;
- err_name:
- free(n);
- err_logger:
- free(logger_kernel);
- err:
- logger_kernel = NULL;
- ok:
- return (struct logger *)logger_kernel;
- }
- struct logger *android_logger_open(struct logger_list *logger_list,
- log_id_t id)
- {
- if (use_logd) {
- return android_logger_open_logd(logger_list, id);
- } else {
- return android_logger_open_kernel(logger_list, id);
- }
- }
- /* Open the single named log and make it part of a new logger list */
- struct logger_list *android_logger_list_open(log_id_t id,
- int mode,
- unsigned int tail,
- pid_t pid)
- {
- struct logger_list *logger_list = android_logger_list_alloc(mode, tail, pid);
- if (!logger_list) {
- return NULL;
- }
- if (!android_logger_open(logger_list, id)) {
- android_logger_list_free(logger_list);
- return NULL;
- }
- return logger_list;
- }
- /* prevent memory starvation when backfilling */
- static unsigned int queue_threshold(struct logger_list *logger_list)
- {
- return (logger_list->tail < 64) ? 64 : logger_list->tail;
- }
- static bool low_queue(struct listnode *node)
- {
- /* low is considered less than 2 */
- return list_head(node) == list_tail(node);
- }
- static int android_logger_list_read_pstore(struct logger_list *logger_list,
- struct log_msg *log_msg)
- {
- ssize_t ret;
- off_t current, next;
- uid_t uid;
- struct logger *logger;
- struct __attribute__((__packed__)) {
- android_pmsg_log_header_t p;
- android_log_header_t l;
- } buf;
- static uint8_t preread_count;
- struct logger_list_logd* logger_list_logd = (struct logger_list_logd*)logger_list;
- memset(log_msg, 0, sizeof(*log_msg));
- if (logger_list_logd->sock < 0) {
- int fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY);
- if (fd < 0) {
- return -errno;
- }
- logger_list_logd->sock = fd;
- preread_count = 0;
- }
- ret = 0;
- while(1) {
- if (preread_count < sizeof(buf)) {
- ret = TEMP_FAILURE_RETRY(read(logger_list_logd->sock,
- &buf.p.magic + preread_count,
- sizeof(buf) - preread_count));
- if (ret < 0) {
- return -errno;
- }
- preread_count += ret;
- }
- if (preread_count != sizeof(buf)) {
- return preread_count ? -EIO : -EAGAIN;
- }
- if ((buf.p.magic != LOGGER_MAGIC)
- || (buf.p.len <= sizeof(buf))
- || (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD))
- || (buf.l.id >= LOG_ID_MAX)
- || (buf.l.realtime.tv_nsec >= NS_PER_SEC)) {
- do {
- memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
- } while (preread_count && (buf.p.magic != LOGGER_MAGIC));
- continue;
- }
- preread_count = 0;
- logger_for_each(logger, logger_list) {
- if (buf.l.id != logger->id) {
- continue;
- }
- if ((logger_list_logd->start.tv_sec || logger_list_logd->start.tv_nsec)
- && ((logger_list_logd->start.tv_sec > buf.l.realtime.tv_sec)
- || ((logger_list_logd->start.tv_sec == buf.l.realtime.tv_sec)
- && (logger_list_logd->start.tv_nsec > buf.l.realtime.tv_nsec)))) {
- break;
- }
- if (logger_list_logd->base.pid && (logger_list_logd->base.pid != buf.p.pid)) {
- break;
- }
- uid = get_best_effective_uid();
- if (!uid_has_log_permission(uid) && (uid != buf.p.uid)) {
- break;
- }
- ret = TEMP_FAILURE_RETRY(read(logger_list_logd->sock,
- log_msg->entry_v3.msg,
- buf.p.len - sizeof(buf)));
- if (ret < 0) {
- return -errno;
- }
- if (ret != (ssize_t)(buf.p.len - sizeof(buf))) {
- return -EIO;
- }
- log_msg->entry_v3.len = buf.p.len - sizeof(buf);
- log_msg->entry_v3.hdr_size = sizeof(log_msg->entry_v3);
- log_msg->entry_v3.pid = buf.p.pid;
- log_msg->entry_v3.tid = buf.l.tid;
- log_msg->entry_v3.sec = buf.l.realtime.tv_sec;
- log_msg->entry_v3.nsec = buf.l.realtime.tv_nsec;
- log_msg->entry_v3.lid = buf.l.id;
- return ret;
- }
- current = TEMP_FAILURE_RETRY(lseek(logger_list_logd->sock,
- (off_t)0, SEEK_CUR));
- if (current < 0) {
- return -errno;
- }
- next = TEMP_FAILURE_RETRY(lseek(logger_list_logd->sock,
- (off_t)(buf.p.len - sizeof(buf)),
- SEEK_CUR));
- if (next < 0) {
- return -errno;
- }
- if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) {
- return -EIO;
- }
- }
- }
- /* Flush queues in sequential order, one at a time */
- static int android_logger_list_flush(struct logger_list *logger_list,
- struct log_msg *log_msg)
- {
- int ret = 0;
- struct log_list *firstentry = NULL;
- struct logger_list_kernel* logger_list_kernel = (struct logger_list_kernel*)logger_list;
- while ((ret == 0)
- && (logger_list_kernel->flush
- || (logger_list_kernel->queued_lines > logger_list_kernel->base.tail))) {
- struct logger *logger;
- struct logger_kernel *logger_kernel;
- /* Merge sort */
- bool at_least_one_is_low = false;
- struct logger *firstlogger = NULL;
- firstentry = NULL;
- logger_for_each(logger, logger_list) {
- struct listnode *node;
- struct log_list *oldest = NULL;
- /* kernel logger channels not necessarily time-sort order */
- logger_kernel = (struct logger_kernel *)logger;
- list_for_each(node, &logger_kernel->log_list) {
- struct log_list *entry = node_to_item(node,
- struct log_list, node);
- if (!oldest
- || (entry->entry.entry.sec < oldest->entry.entry.sec)
- || ((entry->entry.entry.sec == oldest->entry.entry.sec)
- && (entry->entry.entry.nsec < oldest->entry.entry.nsec))) {
- oldest = entry;
- }
- }
- if (!oldest) {
- at_least_one_is_low = true;
- continue;
- } else if (low_queue(&logger_kernel->log_list)) {
- at_least_one_is_low = true;
- }
- if (!firstentry
- || (oldest->entry.entry.sec < firstentry->entry.entry.sec)
- || ((oldest->entry.entry.sec == firstentry->entry.entry.sec)
- && (oldest->entry.entry.nsec < firstentry->entry.entry.nsec))) {
- firstentry = oldest;
- firstlogger = logger;
- }
- }
- if (!firstentry) {
- break;
- }
- /* when trimming list, tries to keep one entry behind in each bucket */
- if (!logger_list_kernel->flush
- && at_least_one_is_low
- && (logger_list_kernel->queued_lines < queue_threshold(logger_list))) {
- break;
- }
- /* within tail?, send! */
- if ((logger_list_kernel->base.tail == 0)
- || (logger_list_kernel->queued_lines <= logger_list_kernel->base.tail)) {
- int diff;
- ret = firstentry->entry.entry.hdr_size;
- if (!ret) {
- ret = sizeof(firstentry->entry.entry_v1);
- }
- /* Promote entry to v3 format */
- memcpy(log_msg->buf, firstentry->entry.buf, ret);
- diff = sizeof(firstentry->entry.entry_v3) - ret;
- if (diff < 0) {
- diff = 0;
- } else if (diff > 0) {
- memset(log_msg->buf + ret, 0, diff);
- }
- memcpy(log_msg->buf + ret + diff, firstentry->entry.buf + ret,
- firstentry->entry.entry.len + 1);
- ret += diff;
- log_msg->entry.hdr_size = ret;
- log_msg->entry.lid = firstlogger->id;
- ret += firstentry->entry.entry.len;
- }
- /* next entry */
- list_remove(&firstentry->node);
- free(firstentry);
- if (logger_list_kernel->queued_lines) {
- logger_list_kernel->queued_lines--;
- }
- }
- /* Flushed the list, no longer in tail mode for continuing content */
- if (logger_list_kernel->flush && !firstentry) {
- logger_list_kernel->base.tail = 0;
- }
- return ret;
- }
- static void caught_signal(int signum __unused)
- {
- }
- /* Read from the selected logs */
- static int android_logger_list_read_logd(struct logger_list *logger_list,
- struct log_msg *log_msg)
- {
- int ret, e;
- struct logger *logger;
- struct sigaction ignore;
- struct sigaction old_sigaction;
- struct logger_list_logd *logger_list_logd = (struct logger_list_logd *)logger_list;
- unsigned int old_alarm = 0;
- if (!logger_list) {
- return -EINVAL;
- }
- if (logger_list->mode & ANDROID_LOG_PSTORE) {
- return android_logger_list_read_pstore(logger_list, log_msg);
- }
- if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
- memset(&ignore, 0, sizeof(ignore));
- ignore.sa_handler = caught_signal;
- sigemptyset(&ignore.sa_mask);
- }
- if (logger_list_logd->sock < 0) {
- char buffer[256], *cp, c;
- int sock = socket_local_client("logdr",
- ANDROID_SOCKET_NAMESPACE_RESERVED,
- SOCK_SEQPACKET);
- if (sock < 0) {
- if ((sock == -1) && errno) {
- return -errno;
- }
- return sock;
- }
- strcpy(buffer,
- (logger_list->mode & ANDROID_LOG_NONBLOCK) ? "dumpAndClose" : "stream");
- cp = buffer + strlen(buffer);
- strcpy(cp, " lids");
- cp += 5;
- c = '=';
- int remaining = sizeof(buffer) - (cp - buffer);
- logger_for_each(logger, logger_list) {
- ret = snprintf(cp, remaining, "%c%u", c, logger->id);
- ret = min(ret, remaining);
- remaining -= ret;
- cp += ret;
- c = ',';
- }
- if (logger_list->tail) {
- ret = snprintf(cp, remaining, " tail=%u", logger_list->tail);
- ret = min(ret, remaining);
- remaining -= ret;
- cp += ret;
- }
- if (logger_list_logd->start.tv_sec || logger_list_logd->start.tv_nsec) {
- ret = snprintf(cp, remaining, " start=%" PRIu32 ".%09" PRIu32,
- logger_list_logd->start.tv_sec,
- logger_list_logd->start.tv_nsec);
- ret = min(ret, remaining);
- remaining -= ret;
- cp += ret;
- }
- if (logger_list->pid) {
- ret = snprintf(cp, remaining, " pid=%u", logger_list->pid);
- ret = min(ret, remaining);
- remaining -= ret;
- cp += ret;
- }
- if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
- /* Deal with an unresponsive 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);
- }
- if (ret <= 0) {
- close(sock);
- if ((ret == -1) && e) {
- return -e;
- }
- if (ret == 0) {
- return -EIO;
- }
- return ret;
- }
- logger_list_logd->sock = sock;
- }
- ret = 0;
- while(1) {
- memset(log_msg, 0, sizeof(*log_msg));
- if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
- /* particularily useful if tombstone is reporting for logd */
- sigaction(SIGALRM, &ignore, &old_sigaction);
- old_alarm = alarm(30);
- }
- /* NOTE: SOCK_SEQPACKET guarantees we read exactly one full entry */
- ret = recv(logger_list_logd->sock, log_msg, LOGGER_ENTRY_MAX_LEN, 0);
- e = errno;
- if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
- if ((ret == 0) || (e == EINTR)) {
- e = EAGAIN;
- ret = -1;
- }
- alarm(old_alarm);
- sigaction(SIGALRM, &old_sigaction, NULL);
- }
- if (ret <= 0) {
- if ((ret == -1) && e) {
- return -e;
- }
- return ret;
- }
- logger_for_each(logger, logger_list) {
- if (log_msg->entry.lid == logger->id) {
- return ret;
- }
- }
- }
- /* NOTREACH */
- return ret;
- }
- /* Read from the selected logs */
- static int android_logger_list_read_kernel(struct logger_list *logger_list,
- struct log_msg *log_msg)
- {
- struct logger *logger;
- struct logger_list_kernel *logger_list_kernel = (struct logger_list_kernel *)logger_list;
- nfds_t nfds;
- struct pollfd *p, *pollfds = NULL;
- int error = 0, ret = 0;
- memset(log_msg, 0, sizeof(struct log_msg));
- if (!logger_list) {
- return -ENODEV;
- }
- if (!(accessmode(logger_list->mode) & R_OK)) {
- logger_list_kernel->error = EPERM;
- goto done;
- }
- nfds = 0;
- logger_for_each(logger, logger_list) {
- ++nfds;
- }
- if (nfds <= 0) {
- error = ENODEV;
- goto done;
- }
- /* Do we have anything to offer from the buffer or state? */
- if (logger_list_kernel->valid_entry) { /* implies we are also in a flush state */
- goto flush;
- }
- ret = android_logger_list_flush(logger_list, log_msg);
- if (ret) {
- goto done;
- }
- if (logger_list_kernel->error) { /* implies we are also in a flush state */
- goto done;
- }
- /* Lets start grinding on metal */
- pollfds = calloc(nfds, sizeof(struct pollfd));
- if (!pollfds) {
- error = ENOMEM;
- goto flush;
- }
- p = pollfds;
- struct logger_kernel *logger_kernel;
- logger_for_each(logger, logger_list) {
- logger_kernel = (struct logger_kernel *)logger;
- p->fd = logger_kernel->fd;
- p->events = POLLIN;
- logger_kernel->revents = &p->revents;
- ++p;
- }
- while (!ret && !error) {
- int result;
- /* If we oversleep it's ok, i.e. ignore EINTR. */
- result = TEMP_FAILURE_RETRY(
- poll(pollfds, nfds, logger_list_kernel->timeout_ms));
- if (result <= 0) {
- if (result) {
- error = errno;
- } else if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
- error = EAGAIN;
- } else {
- logger_list_kernel->timeout_ms = LOG_TIMEOUT_NEVER;
- }
- logger_list_kernel->flush = true;
- goto try_flush;
- }
- logger_list_kernel->timeout_ms = LOG_TIMEOUT_FLUSH;
- /* Anti starvation */
- if (!logger_list_kernel->flush
- && (logger_list_kernel->queued_lines > (queue_threshold(logger_list) / 2))) {
- /* Any queues with input pending that is low? */
- bool starving = false;
- logger_for_each(logger, logger_list) {
- logger_kernel = (struct logger_kernel *)logger;
- if ((*(logger_kernel->revents) & POLLIN)
- && low_queue(&logger_kernel->log_list)) {
- starving = true;
- break;
- }
- }
- /* pushback on any queues that are not low */
- if (starving) {
- logger_for_each(logger, logger_list) {
- logger_kernel = (struct logger_kernel *)logger;
- if ((*(logger_kernel->revents) & POLLIN)
- && !low_queue(&logger_kernel->log_list)) {
- *(logger_kernel->revents) &= ~POLLIN;
- }
- }
- }
- }
- logger_for_each(logger, logger_list) {
- unsigned int hdr_size;
- struct log_list *entry;
- int diff;
- logger_kernel = (struct logger_kernel *)logger;
- if (!(*(logger_kernel->revents) & POLLIN)) {
- continue;
- }
- memset(logger_list_kernel->entry.buf, 0, sizeof(struct log_msg));
- /* NOTE: driver guarantees we read exactly one full entry */
- result = read(logger_kernel->fd, logger_list_kernel->entry.buf,
- LOGGER_ENTRY_MAX_LEN);
- if (result <= 0) {
- if (!result) {
- error = EIO;
- } else if (errno != EINTR) {
- error = errno;
- }
- continue;
- }
- if (logger_list->pid
- && (logger_list->pid != logger_list_kernel->entry.entry.pid)) {
- continue;
- }
- hdr_size = logger_list_kernel->entry.entry.hdr_size;
- if (!hdr_size) {
- hdr_size = sizeof(logger_list_kernel->entry.entry_v1);
- }
- if ((hdr_size > sizeof(struct log_msg))
- || (logger_list_kernel->entry.entry.len
- > sizeof(logger_list_kernel->entry.buf) - hdr_size)
- || (logger_list_kernel->entry.entry.len != result - hdr_size)) {
- error = EINVAL;
- continue;
- }
- /* Promote entry to v3 format */
- diff = sizeof(logger_list_kernel->entry.entry_v3) - hdr_size;
- if (diff > 0) {
- if (logger_list_kernel->entry.entry.len
- > sizeof(logger_list_kernel->entry.buf) - hdr_size - diff) {
- error = EINVAL;
- continue;
- }
- result += diff;
- memmove(logger_list_kernel->entry.buf + hdr_size + diff,
- logger_list_kernel->entry.buf + hdr_size,
- logger_list_kernel->entry.entry.len + 1);
- memset(logger_list_kernel->entry.buf + hdr_size, 0, diff);
- logger_list_kernel->entry.entry.hdr_size = hdr_size + diff;
- }
- logger_list_kernel->entry.entry.lid = logger->id;
- /* speedup: If not tail, and only one list, send directly */
- if (!logger_list->tail
- && (list_head(&logger_list->node)
- == list_tail(&logger_list->node))) {
- ret = result;
- memcpy(log_msg->buf, logger_list_kernel->entry.buf, result + 1);
- break;
- }
- entry = malloc(sizeof(*entry) - sizeof(entry->entry) + result + 1);
- if (!entry) {
- logger_list_kernel->valid_entry = true;
- error = ENOMEM;
- break;
- }
- logger_list_kernel->queued_lines++;
- memcpy(entry->entry.buf, logger_list_kernel->entry.buf, result);
- entry->entry.buf[result] = '\0';
- list_add_tail(&logger_kernel->log_list, &entry->node);
- }
- if (ret <= 0) {
- try_flush:
- ret = android_logger_list_flush(logger_list, log_msg);
- }
- }
- free(pollfds);
- flush:
- if (error) {
- logger_list_kernel->flush = true;
- }
- if (ret <= 0) {
- ret = android_logger_list_flush(logger_list, log_msg);
- if (!ret && logger_list_kernel->valid_entry) {
- ret = logger_list_kernel->entry.entry.hdr_size;
- if (!ret) {
- ret = sizeof(logger_list_kernel->entry.entry_v1);
- }
- ret += logger_list_kernel->entry.entry.len;
- memcpy(log_msg->buf, logger_list_kernel->entry.buf,
- sizeof(struct log_msg));
- logger_list_kernel->valid_entry = false;
- }
- }
- done:
- if (logger_list_kernel->error) {
- error = logger_list_kernel->error;
- }
- if (error) {
- logger_list_kernel->error = error;
- if (!ret) {
- ret = -error;
- }
- }
- return ret;
- }
- int android_logger_list_read(struct logger_list *logger_list,
- struct log_msg *log_msg)
- {
- if (use_logd) {
- return android_logger_list_read_logd(logger_list, log_msg);
- } else {
- return android_logger_list_read_kernel(logger_list, log_msg);
- }
- }
- /* Close all the logs */
- static void android_logger_list_free_logd(struct logger_list *logger_list)
- {
- if (logger_list == NULL) {
- return;
- }
- while (!list_empty(&logger_list->node)) {
- struct listnode *node = list_head(&logger_list->node);
- struct logger *logger = node_to_item(node, struct logger, node);
- android_logger_free(logger);
- }
- struct logger_list_logd *logger_list_logd = (struct logger_list_logd *)logger_list;
- if (use_logd) {
- if (logger_list_logd->sock >= 0) {
- close (logger_list_logd->sock);
- }
- }
- free(logger_list_logd);
- }
- /* Close all the logs */
- static void android_logger_list_free_kernel(struct logger_list *logger_list)
- {
- if (logger_list == NULL) {
- return;
- }
- while (!list_empty(&logger_list->node)) {
- struct listnode *node = list_head(&logger_list->node);
- struct logger *logger = node_to_item(node, struct logger, node);
- android_logger_free(logger);
- }
- struct logger_list_kernel *logger_list_kernel = (struct logger_list_kernel *)logger_list;
- free(logger_list_kernel);
- }
- void android_logger_list_free(struct logger_list *logger_list) {
- if (use_logd) {
- android_logger_list_free_logd(logger_list);
- } else {
- android_logger_list_free_kernel(logger_list);
- }
- }
在logger.h中定义对外函数,FAKE_LOG_DEVICE==0 代表是手机版本,不是host。必须加这个宏,因为在编译host的使用也会引用到这个头文件,而在编译HOST的静态库时,log_read_common.c是不会参与编译的这样会造成set_uselogd这样函数没有定义。所以必须加这个宏。
- ......
- #if (FAKE_LOG_DEVICE == 0)
- void set_uselogd(int8_t uselogd);
- #endif
- ......
最后我们来看logcat的main函数中,调用这个函数来控制logcat read log时使用logd机制还是kernel机制。下面这段代码是在logcat的main函数在定义完变量之后,和write相同也是看system/etc/disable_logd这个文件是否存在,存在就是kernel机制,不存在就是logd机制。
- ......
- int fd;
- int8_t use_logd = 1;
- fd = open("system/etc/disable_logd", O_RDONLY);
- if (fd > 0) {
- LOGCAT_INFO("logcat disable logd.\n ");
- use_logd = 0;
- close(fd);
- }
- set_uselogd(use_logd);
- ......
write是在liblog库中log函数初始化的时候来判断使用哪个机制,也就是每个进程调用这个函数时都会初始化下。而read只有在logcat进程中使用,每次logcat进程启动的时候判断使用哪个机制,并且调用set_uselogd来通知read那块。
原文地址: http://blog.csdn.net/kc58236582/article/details/72279744