blob: 283a979b780cb50ca0c3b231ea0a20883015925c [file] [log] [blame]
Mark Salyzyn018a96d2016-03-01 13:45:42 -08001/*
2 * Copyright (C) 2007-2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn018a96d2016-03-01 13:45:42 -080017#include <errno.h>
18#include <fcntl.h>
19#include <inttypes.h>
20#include <poll.h>
21#include <stdarg.h>
22#include <stdatomic.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080026#include <sys/socket.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080027#include <sys/stat.h>
28#include <sys/types.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080029#include <sys/un.h>
30#include <time.h>
31#include <unistd.h>
32
Tom Cherry2a6811b2019-12-11 12:56:01 -080033#include <shared_mutex>
34
Mark Salyzyn018a96d2016-03-01 13:45:42 -080035#include <cutils/sockets.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080036#include <private/android_filesystem_config.h>
37#include <private/android_logger.h>
38
Mark Salyzyn018a96d2016-03-01 13:45:42 -080039#include "log_portability.h"
40#include "logger.h"
Tom Cherry2a6811b2019-12-11 12:56:01 -080041#include "rwlock.h"
Tom Cherry6f6ef392019-01-16 14:17:08 -080042#include "uio.h"
Mark Salyzyn018a96d2016-03-01 13:45:42 -080043
Tom Cherry2a6811b2019-12-11 12:56:01 -080044static int LogdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr);
45static void LogdClose();
Mark Salyzyn018a96d2016-03-01 13:45:42 -080046
Tom Cherry2d9779e2019-02-08 11:46:19 -080047struct android_log_transport_write logdLoggerWrite = {
Tom Cherry71ba1642019-01-10 10:37:36 -080048 .name = "logd",
Tom Cherry2beabe52019-10-01 13:05:58 -070049 .logMask = 0,
Tom Cherry2a6811b2019-12-11 12:56:01 -080050 .available = [](log_id_t) { return 0; },
51 .open = [] { return 0; },
52 .close = LogdClose,
53 .write = LogdWrite,
Mark Salyzyn018a96d2016-03-01 13:45:42 -080054};
55
Tom Cherry2a6811b2019-12-11 12:56:01 -080056static int logd_socket;
57static RwLock logd_socket_lock;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080058
Tom Cherry2a6811b2019-12-11 12:56:01 -080059static void OpenSocketLocked() {
60 logd_socket = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
61 if (logd_socket <= 0) {
62 return;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080063 }
64
Tom Cherry2a6811b2019-12-11 12:56:01 -080065 sockaddr_un un = {};
66 un.sun_family = AF_UNIX;
67 strcpy(un.sun_path, "/dev/socket/logdw");
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080068
Tom Cherry2a6811b2019-12-11 12:56:01 -080069 if (TEMP_FAILURE_RETRY(
70 connect(logd_socket, reinterpret_cast<sockaddr*>(&un), sizeof(sockaddr_un))) < 0) {
71 close(logd_socket);
72 logd_socket = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080073 }
74}
75
Tom Cherry2a6811b2019-12-11 12:56:01 -080076static void OpenSocket() {
77 auto lock = std::unique_lock{logd_socket_lock};
78 if (logd_socket > 0) {
79 // Someone raced us and opened the socket already.
80 return;
81 }
82
83 OpenSocketLocked();
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080084}
85
Tom Cherry2a6811b2019-12-11 12:56:01 -080086static void ResetSocket(int old_socket) {
87 auto lock = std::unique_lock{logd_socket_lock};
88 if (old_socket != logd_socket) {
89 // Someone raced us and reset the socket already.
90 return;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080091 }
Tom Cherry2a6811b2019-12-11 12:56:01 -080092 close(logd_socket);
93 logd_socket = 0;
94 OpenSocketLocked();
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080095}
96
Tom Cherry2a6811b2019-12-11 12:56:01 -080097static void LogdClose() {
98 auto lock = std::unique_lock{logd_socket_lock};
99 if (logd_socket > 0) {
100 close(logd_socket);
101 }
102 logd_socket = 0;
103}
104
105static int LogdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800106 ssize_t ret;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800107 static const unsigned headerLength = 1;
108 struct iovec newVec[nr + headerLength];
109 android_log_header_t header;
110 size_t i, payloadSize;
Tom Cherry71ba1642019-01-10 10:37:36 -0800111 static atomic_int dropped;
112 static atomic_int droppedSecurity;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800113
Tom Cherry2a6811b2019-12-11 12:56:01 -0800114 auto lock = std::shared_lock{logd_socket_lock};
115 if (logd_socket <= 0) {
116 lock.unlock();
117 OpenSocket();
118 lock.lock();
119 }
120
121 if (logd_socket <= 0) {
122 return -EBADF;
123 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800124
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800125 /* logd, after initialization and priv drop */
126 if (__android_log_uid() == AID_LOGD) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800127 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800128 * ignore log messages we send to ourself (logd).
129 * Such log messages are often generated by libraries we depend on
130 * which use standard Android logging.
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800131 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800132 return 0;
133 }
134
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800135 header.tid = gettid();
136 header.realtime.tv_sec = ts->tv_sec;
137 header.realtime.tv_nsec = ts->tv_nsec;
138
139 newVec[0].iov_base = (unsigned char*)&header;
140 newVec[0].iov_len = sizeof(header);
141
Tom Cherry2a6811b2019-12-11 12:56:01 -0800142 int32_t snapshot = atomic_exchange_explicit(&droppedSecurity, 0, memory_order_relaxed);
143 if (snapshot) {
144 android_log_event_int_t buffer;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800145
Tom Cherry2a6811b2019-12-11 12:56:01 -0800146 header.id = LOG_ID_SECURITY;
147 buffer.header.tag = LIBLOG_LOG_TAG;
148 buffer.payload.type = EVENT_TYPE_INT;
149 buffer.payload.data = snapshot;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800150
Tom Cherry2a6811b2019-12-11 12:56:01 -0800151 newVec[headerLength].iov_base = &buffer;
152 newVec[headerLength].iov_len = sizeof(buffer);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800153
Tom Cherry2a6811b2019-12-11 12:56:01 -0800154 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, 2));
155 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
156 atomic_fetch_add_explicit(&droppedSecurity, snapshot, memory_order_relaxed);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800157 }
Tom Cherry2a6811b2019-12-11 12:56:01 -0800158 }
159 snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
160 if (snapshot && __android_log_is_loggable_len(ANDROID_LOG_INFO, "liblog", strlen("liblog"),
161 ANDROID_LOG_VERBOSE)) {
162 android_log_event_int_t buffer;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800163
Tom Cherry2a6811b2019-12-11 12:56:01 -0800164 header.id = LOG_ID_EVENTS;
165 buffer.header.tag = LIBLOG_LOG_TAG;
166 buffer.payload.type = EVENT_TYPE_INT;
167 buffer.payload.data = snapshot;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800168
Tom Cherry2a6811b2019-12-11 12:56:01 -0800169 newVec[headerLength].iov_base = &buffer;
170 newVec[headerLength].iov_len = sizeof(buffer);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800171
Tom Cherry2a6811b2019-12-11 12:56:01 -0800172 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, 2));
173 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
174 atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800175 }
176 }
177
178 header.id = logId;
179
180 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
181 newVec[i].iov_base = vec[i - headerLength].iov_base;
182 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
183
184 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
185 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
186 if (newVec[i].iov_len) {
187 ++i;
188 }
189 break;
190 }
191 }
192
Tom Cherry2a6811b2019-12-11 12:56:01 -0800193 // The write below could be lost, but will never block.
194 // EAGAIN occurs if logd is overloaded, other errors indicate that something went wrong with
195 // the connection, so we reset it and try again.
196 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, i));
197 if (ret < 0 && errno != EAGAIN) {
198 int old_socket = logd_socket;
199 lock.unlock();
200 ResetSocket(old_socket);
201 lock.lock();
202
203 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, i));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800204 }
Mark Salyzyn65e1e622017-01-03 13:28:18 -0800205
Tom Cherry2a6811b2019-12-11 12:56:01 -0800206 if (ret < 0) {
207 ret = -errno;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800208 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800209
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800210 if (ret > (ssize_t)sizeof(header)) {
211 ret -= sizeof(header);
Tom Cherry2a6811b2019-12-11 12:56:01 -0800212 } else if (ret < 0) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800213 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
214 if (logId == LOG_ID_SECURITY) {
215 atomic_fetch_add_explicit(&droppedSecurity, 1, memory_order_relaxed);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800216 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800217 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800218
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800219 return ret;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800220}