2016-03-01 22:45:42 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007-2016 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <endian.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdatomic.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2017-03-09 17:09:43 +01:00
|
|
|
#include <sys/socket.h>
|
2016-03-01 22:45:42 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <cutils/sockets.h>
|
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
#include <private/android_logger.h>
|
|
|
|
|
|
|
|
#include "config_write.h"
|
|
|
|
#include "log_portability.h"
|
|
|
|
#include "logger.h"
|
2019-01-16 23:17:08 +01:00
|
|
|
#include "uio.h"
|
2016-03-01 22:45:42 +01:00
|
|
|
|
|
|
|
/* branchless on many architectures. */
|
2017-03-09 17:09:43 +01:00
|
|
|
#define min(x, y) ((y) ^ (((x) ^ (y)) & -((x) < (y))))
|
2016-03-01 22:45:42 +01:00
|
|
|
|
|
|
|
static int logdAvailable(log_id_t LogId);
|
|
|
|
static int logdOpen();
|
|
|
|
static void logdClose();
|
2019-01-10 19:37:36 +01:00
|
|
|
static int logdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr);
|
2016-03-01 22:45:42 +01:00
|
|
|
|
|
|
|
LIBLOG_HIDDEN struct android_log_transport_write logdLoggerWrite = {
|
2019-01-10 19:37:36 +01:00
|
|
|
.node = {&logdLoggerWrite.node, &logdLoggerWrite.node},
|
|
|
|
.context.sock = -EBADF,
|
|
|
|
.name = "logd",
|
|
|
|
.available = logdAvailable,
|
|
|
|
.open = logdOpen,
|
|
|
|
.close = logdClose,
|
|
|
|
.write = logdWrite,
|
2016-03-01 22:45:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* log_init_lock assumed */
|
2017-03-09 17:09:43 +01:00
|
|
|
static int logdOpen() {
|
|
|
|
int i, ret = 0;
|
|
|
|
|
|
|
|
i = atomic_load(&logdLoggerWrite.context.sock);
|
|
|
|
if (i < 0) {
|
2019-01-10 19:37:36 +01:00
|
|
|
int sock = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
|
2017-03-09 17:09:43 +01:00
|
|
|
if (sock < 0) {
|
|
|
|
ret = -errno;
|
|
|
|
} else {
|
|
|
|
struct sockaddr_un un;
|
|
|
|
memset(&un, 0, sizeof(struct sockaddr_un));
|
|
|
|
un.sun_family = AF_UNIX;
|
|
|
|
strcpy(un.sun_path, "/dev/socket/logdw");
|
|
|
|
|
2019-01-10 19:37:36 +01:00
|
|
|
if (TEMP_FAILURE_RETRY(connect(sock, (struct sockaddr*)&un, sizeof(struct sockaddr_un))) <
|
|
|
|
0) {
|
2017-03-09 17:09:43 +01:00
|
|
|
ret = -errno;
|
|
|
|
switch (ret) {
|
|
|
|
case -ENOTCONN:
|
|
|
|
case -ECONNREFUSED:
|
|
|
|
case -ENOENT:
|
|
|
|
i = atomic_exchange(&logdLoggerWrite.context.sock, ret);
|
2019-01-10 19:37:36 +01:00
|
|
|
[[fallthrough]];
|
2017-03-09 17:09:43 +01:00
|
|
|
default:
|
|
|
|
break;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
close(sock);
|
|
|
|
} else {
|
|
|
|
ret = atomic_exchange(&logdLoggerWrite.context.sock, sock);
|
|
|
|
if ((ret >= 0) && (ret != sock)) {
|
|
|
|
close(ret);
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
}
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
2016-03-01 22:45:42 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
return ret;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
static void __logdClose(int negative_errno) {
|
|
|
|
int sock = atomic_exchange(&logdLoggerWrite.context.sock, negative_errno);
|
|
|
|
if (sock >= 0) {
|
|
|
|
close(sock);
|
|
|
|
}
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
static void logdClose() {
|
|
|
|
__logdClose(-EBADF);
|
2017-01-03 22:28:18 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
static int logdAvailable(log_id_t logId) {
|
|
|
|
if (logId >= LOG_ID_MAX || logId == LOG_ID_KERNEL) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (atomic_load(&logdLoggerWrite.context.sock) < 0) {
|
|
|
|
if (access("/dev/socket/logdw", W_OK) == 0) {
|
|
|
|
return 0;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
return 1;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
|
|
|
|
2019-01-10 19:37:36 +01:00
|
|
|
static int logdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) {
|
2017-03-09 17:09:43 +01:00
|
|
|
ssize_t ret;
|
|
|
|
int sock;
|
|
|
|
static const unsigned headerLength = 1;
|
|
|
|
struct iovec newVec[nr + headerLength];
|
|
|
|
android_log_header_t header;
|
|
|
|
size_t i, payloadSize;
|
2019-01-10 19:37:36 +01:00
|
|
|
static atomic_int dropped;
|
|
|
|
static atomic_int droppedSecurity;
|
2017-03-09 17:09:43 +01:00
|
|
|
|
|
|
|
sock = atomic_load(&logdLoggerWrite.context.sock);
|
|
|
|
if (sock < 0) switch (sock) {
|
|
|
|
case -ENOTCONN:
|
|
|
|
case -ECONNREFUSED:
|
|
|
|
case -ENOENT:
|
2017-01-03 22:28:18 +01:00
|
|
|
break;
|
2017-03-09 17:09:43 +01:00
|
|
|
default:
|
2016-03-01 22:45:42 +01:00
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
/* logd, after initialization and priv drop */
|
|
|
|
if (__android_log_uid() == AID_LOGD) {
|
2016-03-01 22:45:42 +01:00
|
|
|
/*
|
2017-03-09 17:09:43 +01:00
|
|
|
* ignore log messages we send to ourself (logd).
|
|
|
|
* Such log messages are often generated by libraries we depend on
|
|
|
|
* which use standard Android logging.
|
2016-03-01 22:45:42 +01:00
|
|
|
*/
|
2017-03-09 17:09:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* struct {
|
|
|
|
* // 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;
|
|
|
|
* };
|
|
|
|
* };
|
|
|
|
*/
|
|
|
|
|
|
|
|
header.tid = gettid();
|
|
|
|
header.realtime.tv_sec = ts->tv_sec;
|
|
|
|
header.realtime.tv_nsec = ts->tv_nsec;
|
|
|
|
|
|
|
|
newVec[0].iov_base = (unsigned char*)&header;
|
|
|
|
newVec[0].iov_len = sizeof(header);
|
|
|
|
|
|
|
|
if (sock >= 0) {
|
2019-01-10 19:37:36 +01:00
|
|
|
int32_t snapshot = atomic_exchange_explicit(&droppedSecurity, 0, memory_order_relaxed);
|
2017-03-09 17:09:43 +01:00
|
|
|
if (snapshot) {
|
|
|
|
android_log_event_int_t buffer;
|
|
|
|
|
|
|
|
header.id = LOG_ID_SECURITY;
|
|
|
|
buffer.header.tag = htole32(LIBLOG_LOG_TAG);
|
|
|
|
buffer.payload.type = EVENT_TYPE_INT;
|
|
|
|
buffer.payload.data = htole32(snapshot);
|
|
|
|
|
|
|
|
newVec[headerLength].iov_base = &buffer;
|
|
|
|
newVec[headerLength].iov_len = sizeof(buffer);
|
|
|
|
|
|
|
|
ret = TEMP_FAILURE_RETRY(writev(sock, newVec, 2));
|
|
|
|
if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
|
2019-01-10 19:37:36 +01:00
|
|
|
atomic_fetch_add_explicit(&droppedSecurity, snapshot, memory_order_relaxed);
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
|
2019-01-10 19:37:36 +01:00
|
|
|
if (snapshot && __android_log_is_loggable_len(ANDROID_LOG_INFO, "liblog", strlen("liblog"),
|
|
|
|
ANDROID_LOG_VERBOSE)) {
|
2017-03-09 17:09:43 +01:00
|
|
|
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[headerLength].iov_base = &buffer;
|
|
|
|
newVec[headerLength].iov_len = sizeof(buffer);
|
|
|
|
|
|
|
|
ret = TEMP_FAILURE_RETRY(writev(sock, newVec, 2));
|
|
|
|
if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
|
|
|
|
atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
|
|
|
|
}
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
2016-03-01 22:45:42 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
header.id = logId;
|
2016-03-01 22:45:42 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
|
|
|
|
newVec[i].iov_base = vec[i - headerLength].iov_base;
|
|
|
|
payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
|
2016-03-01 22:45:42 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
|
|
|
|
newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
|
|
|
|
if (newVec[i].iov_len) {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
break;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The write below could be lost, but will never block.
|
|
|
|
*
|
|
|
|
* ENOTCONN occurs if logd has died.
|
|
|
|
* ENOENT occurs if logd is not running and socket is missing.
|
|
|
|
* ECONNREFUSED occurs if we can not reconnect to logd.
|
|
|
|
* EAGAIN occurs if logd is overloaded.
|
|
|
|
*/
|
|
|
|
if (sock < 0) {
|
|
|
|
ret = sock;
|
|
|
|
} else {
|
|
|
|
ret = TEMP_FAILURE_RETRY(writev(sock, newVec, i));
|
|
|
|
if (ret < 0) {
|
|
|
|
ret = -errno;
|
2017-01-03 22:28:18 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
|
|
|
switch (ret) {
|
2017-01-03 22:28:18 +01:00
|
|
|
case -ENOTCONN:
|
|
|
|
case -ECONNREFUSED:
|
|
|
|
case -ENOENT:
|
2017-03-09 17:09:43 +01:00
|
|
|
if (__android_log_trylock()) {
|
|
|
|
return ret; /* in a signal handler? try again when less stressed */
|
|
|
|
}
|
|
|
|
__logdClose(ret);
|
|
|
|
ret = logdOpen();
|
|
|
|
__android_log_unlock();
|
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-10 19:37:36 +01:00
|
|
|
ret = TEMP_FAILURE_RETRY(writev(atomic_load(&logdLoggerWrite.context.sock), newVec, i));
|
2017-03-09 17:09:43 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
ret = -errno;
|
|
|
|
}
|
2019-01-10 19:37:36 +01:00
|
|
|
[[fallthrough]];
|
2017-01-03 22:28:18 +01:00
|
|
|
default:
|
2017-03-09 17:09:43 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret > (ssize_t)sizeof(header)) {
|
|
|
|
ret -= sizeof(header);
|
|
|
|
} else if (ret == -EAGAIN) {
|
|
|
|
atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
|
|
|
|
if (logId == LOG_ID_SECURITY) {
|
|
|
|
atomic_fetch_add_explicit(&droppedSecurity, 1, memory_order_relaxed);
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|
2017-03-09 17:09:43 +01:00
|
|
|
}
|
2016-03-01 22:45:42 +01:00
|
|
|
|
2017-03-09 17:09:43 +01:00
|
|
|
return ret;
|
2016-03-01 22:45:42 +01:00
|
|
|
}
|