blob: f49c59efe9e764708248dbf6374f238fd8a8787c [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
Tom Cherry21bb36c2020-01-08 15:18:26 -080017#include "logd_writer.h"
18
Mark Salyzyn018a96d2016-03-01 13:45:42 -080019#include <errno.h>
20#include <fcntl.h>
21#include <inttypes.h>
22#include <poll.h>
23#include <stdarg.h>
24#include <stdatomic.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080028#include <sys/socket.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080029#include <sys/stat.h>
30#include <sys/types.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080031#include <sys/un.h>
32#include <time.h>
33#include <unistd.h>
34
Tom Cherry2a6811b2019-12-11 12:56:01 -080035#include <shared_mutex>
36
Mark Salyzyn018a96d2016-03-01 13:45:42 -080037#include <private/android_filesystem_config.h>
38#include <private/android_logger.h>
39
Mark Salyzyn018a96d2016-03-01 13:45:42 -080040#include "log_portability.h"
41#include "logger.h"
Tom Cherry2a6811b2019-12-11 12:56:01 -080042#include "rwlock.h"
Tom Cherry6f6ef392019-01-16 14:17:08 -080043#include "uio.h"
Mark Salyzyn018a96d2016-03-01 13:45:42 -080044
Tom Cherry2a6811b2019-12-11 12:56:01 -080045static int logd_socket;
46static RwLock logd_socket_lock;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080047
Tom Cherry2a6811b2019-12-11 12:56:01 -080048static void OpenSocketLocked() {
49 logd_socket = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
50 if (logd_socket <= 0) {
51 return;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080052 }
53
Tom Cherry2a6811b2019-12-11 12:56:01 -080054 sockaddr_un un = {};
55 un.sun_family = AF_UNIX;
56 strcpy(un.sun_path, "/dev/socket/logdw");
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080057
Tom Cherry2a6811b2019-12-11 12:56:01 -080058 if (TEMP_FAILURE_RETRY(
59 connect(logd_socket, reinterpret_cast<sockaddr*>(&un), sizeof(sockaddr_un))) < 0) {
60 close(logd_socket);
61 logd_socket = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080062 }
63}
64
Tom Cherry2a6811b2019-12-11 12:56:01 -080065static void OpenSocket() {
66 auto lock = std::unique_lock{logd_socket_lock};
67 if (logd_socket > 0) {
68 // Someone raced us and opened the socket already.
69 return;
70 }
71
72 OpenSocketLocked();
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080073}
74
Tom Cherry2a6811b2019-12-11 12:56:01 -080075static void ResetSocket(int old_socket) {
76 auto lock = std::unique_lock{logd_socket_lock};
77 if (old_socket != logd_socket) {
78 // Someone raced us and reset the socket already.
79 return;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080080 }
Tom Cherry2a6811b2019-12-11 12:56:01 -080081 close(logd_socket);
82 logd_socket = 0;
83 OpenSocketLocked();
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080084}
85
Tom Cherry21bb36c2020-01-08 15:18:26 -080086void LogdClose() {
Tom Cherry2a6811b2019-12-11 12:56:01 -080087 auto lock = std::unique_lock{logd_socket_lock};
88 if (logd_socket > 0) {
89 close(logd_socket);
90 }
91 logd_socket = 0;
92}
93
Tom Cherry21bb36c2020-01-08 15:18:26 -080094int LogdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080095 ssize_t ret;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080096 static const unsigned headerLength = 1;
97 struct iovec newVec[nr + headerLength];
98 android_log_header_t header;
99 size_t i, payloadSize;
Tom Cherry71ba1642019-01-10 10:37:36 -0800100 static atomic_int dropped;
101 static atomic_int droppedSecurity;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800102
Tom Cherry2a6811b2019-12-11 12:56:01 -0800103 auto lock = std::shared_lock{logd_socket_lock};
104 if (logd_socket <= 0) {
105 lock.unlock();
106 OpenSocket();
107 lock.lock();
108 }
109
110 if (logd_socket <= 0) {
111 return -EBADF;
112 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800113
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800114 /* logd, after initialization and priv drop */
Tom Cherryb47aa2a2020-01-08 15:34:14 -0800115 if (getuid() == AID_LOGD) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800116 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800117 * ignore log messages we send to ourself (logd).
118 * Such log messages are often generated by libraries we depend on
119 * which use standard Android logging.
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800120 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800121 return 0;
122 }
123
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800124 header.tid = gettid();
125 header.realtime.tv_sec = ts->tv_sec;
126 header.realtime.tv_nsec = ts->tv_nsec;
127
128 newVec[0].iov_base = (unsigned char*)&header;
129 newVec[0].iov_len = sizeof(header);
130
Tom Cherry2a6811b2019-12-11 12:56:01 -0800131 int32_t snapshot = atomic_exchange_explicit(&droppedSecurity, 0, memory_order_relaxed);
132 if (snapshot) {
133 android_log_event_int_t buffer;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800134
Tom Cherry2a6811b2019-12-11 12:56:01 -0800135 header.id = LOG_ID_SECURITY;
136 buffer.header.tag = LIBLOG_LOG_TAG;
137 buffer.payload.type = EVENT_TYPE_INT;
138 buffer.payload.data = snapshot;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800139
Tom Cherry2a6811b2019-12-11 12:56:01 -0800140 newVec[headerLength].iov_base = &buffer;
141 newVec[headerLength].iov_len = sizeof(buffer);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800142
Tom Cherry2a6811b2019-12-11 12:56:01 -0800143 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, 2));
144 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
145 atomic_fetch_add_explicit(&droppedSecurity, snapshot, memory_order_relaxed);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800146 }
Tom Cherry2a6811b2019-12-11 12:56:01 -0800147 }
148 snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
149 if (snapshot && __android_log_is_loggable_len(ANDROID_LOG_INFO, "liblog", strlen("liblog"),
150 ANDROID_LOG_VERBOSE)) {
151 android_log_event_int_t buffer;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800152
Tom Cherry2a6811b2019-12-11 12:56:01 -0800153 header.id = LOG_ID_EVENTS;
154 buffer.header.tag = LIBLOG_LOG_TAG;
155 buffer.payload.type = EVENT_TYPE_INT;
156 buffer.payload.data = snapshot;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800157
Tom Cherry2a6811b2019-12-11 12:56:01 -0800158 newVec[headerLength].iov_base = &buffer;
159 newVec[headerLength].iov_len = sizeof(buffer);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800160
Tom Cherry2a6811b2019-12-11 12:56:01 -0800161 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, 2));
162 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
163 atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800164 }
165 }
166
167 header.id = logId;
168
169 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
170 newVec[i].iov_base = vec[i - headerLength].iov_base;
171 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
172
173 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
174 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
175 if (newVec[i].iov_len) {
176 ++i;
177 }
178 break;
179 }
180 }
181
Tom Cherry2a6811b2019-12-11 12:56:01 -0800182 // The write below could be lost, but will never block.
183 // EAGAIN occurs if logd is overloaded, other errors indicate that something went wrong with
184 // the connection, so we reset it and try again.
185 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, i));
186 if (ret < 0 && errno != EAGAIN) {
187 int old_socket = logd_socket;
188 lock.unlock();
189 ResetSocket(old_socket);
190 lock.lock();
191
192 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, i));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800193 }
Mark Salyzyn65e1e622017-01-03 13:28:18 -0800194
Tom Cherry2a6811b2019-12-11 12:56:01 -0800195 if (ret < 0) {
196 ret = -errno;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800197 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800198
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800199 if (ret > (ssize_t)sizeof(header)) {
200 ret -= sizeof(header);
Tom Cherry2a6811b2019-12-11 12:56:01 -0800201 } else if (ret < 0) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800202 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
203 if (logId == LOG_ID_SECURITY) {
204 atomic_fetch_add_explicit(&droppedSecurity, 1, memory_order_relaxed);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800205 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800206 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800207
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800208 return ret;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800209}