blob: a230749e1f6da8a3a7af7ecf4fb5e15df272bf74 [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
Mark Salyzyn018a96d2016-03-01 13:45:42 -080035#include <private/android_filesystem_config.h>
36#include <private/android_logger.h>
37
Mark Salyzyn018a96d2016-03-01 13:45:42 -080038#include "logger.h"
Tom Cherry6f6ef392019-01-16 14:17:08 -080039#include "uio.h"
Mark Salyzyn018a96d2016-03-01 13:45:42 -080040
Tom Cherry7acfba22020-04-24 17:15:26 -070041static atomic_int logd_socket;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080042
Tom Cherry7acfba22020-04-24 17:15:26 -070043// Note that it is safe to call connect() multiple times on DGRAM Unix domain sockets, so this
44// function is used to reconnect to logd without requiring a new socket.
45static void LogdConnect() {
Tom Cherry2a6811b2019-12-11 12:56:01 -080046 sockaddr_un un = {};
47 un.sun_family = AF_UNIX;
48 strcpy(un.sun_path, "/dev/socket/logdw");
Tom Cherry7acfba22020-04-24 17:15:26 -070049 TEMP_FAILURE_RETRY(connect(logd_socket, reinterpret_cast<sockaddr*>(&un), sizeof(sockaddr_un)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080050}
51
Tom Cherry7acfba22020-04-24 17:15:26 -070052// logd_socket should only be opened once. If we see that logd_socket is uninitialized, we create a
53// new socket and attempt to exchange it into the atomic logd_socket. If the compare/exchange was
54// successful, then that will be the socket used for the duration of the program, otherwise a
55// different thread has already opened and written the socket to the atomic, so close the new socket
56// and return.
57static void GetSocket() {
58 if (logd_socket != 0) {
Tom Cherry2a6811b2019-12-11 12:56:01 -080059 return;
60 }
61
Tom Cherry7acfba22020-04-24 17:15:26 -070062 int new_socket =
63 TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
64 if (new_socket <= 0) {
Tom Cherry2a6811b2019-12-11 12:56:01 -080065 return;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080066 }
Tom Cherry7acfba22020-04-24 17:15:26 -070067
68 int uninitialized_value = 0;
69 if (!logd_socket.compare_exchange_strong(uninitialized_value, new_socket)) {
70 close(new_socket);
71 return;
72 }
73
74 LogdConnect();
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080075}
76
Tom Cherry7acfba22020-04-24 17:15:26 -070077// This is the one exception to the above. Zygote uses this to clean up open FD's after fork() and
78// before specialization. It is single threaded at this point and therefore this function is
79// explicitly not thread safe. It sets logd_socket to 0, so future logs will be safely initialized
80// whenever they happen.
Tom Cherry21bb36c2020-01-08 15:18:26 -080081void LogdClose() {
Tom Cherry2a6811b2019-12-11 12:56:01 -080082 if (logd_socket > 0) {
83 close(logd_socket);
84 }
85 logd_socket = 0;
86}
87
Tom Cherry21bb36c2020-01-08 15:18:26 -080088int LogdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080089 ssize_t ret;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080090 static const unsigned headerLength = 1;
91 struct iovec newVec[nr + headerLength];
92 android_log_header_t header;
93 size_t i, payloadSize;
Tom Cherry71ba1642019-01-10 10:37:36 -080094 static atomic_int dropped;
95 static atomic_int droppedSecurity;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080096
Tom Cherry7acfba22020-04-24 17:15:26 -070097 GetSocket();
Tom Cherry2a6811b2019-12-11 12:56:01 -080098
99 if (logd_socket <= 0) {
100 return -EBADF;
101 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800102
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800103 /* logd, after initialization and priv drop */
Tom Cherryb47aa2a2020-01-08 15:34:14 -0800104 if (getuid() == AID_LOGD) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800105 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800106 * ignore log messages we send to ourself (logd).
107 * Such log messages are often generated by libraries we depend on
108 * which use standard Android logging.
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800109 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800110 return 0;
111 }
112
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800113 header.tid = gettid();
114 header.realtime.tv_sec = ts->tv_sec;
115 header.realtime.tv_nsec = ts->tv_nsec;
116
117 newVec[0].iov_base = (unsigned char*)&header;
118 newVec[0].iov_len = sizeof(header);
119
Tom Cherry2a6811b2019-12-11 12:56:01 -0800120 int32_t snapshot = atomic_exchange_explicit(&droppedSecurity, 0, memory_order_relaxed);
121 if (snapshot) {
122 android_log_event_int_t buffer;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800123
Tom Cherry2a6811b2019-12-11 12:56:01 -0800124 header.id = LOG_ID_SECURITY;
125 buffer.header.tag = LIBLOG_LOG_TAG;
126 buffer.payload.type = EVENT_TYPE_INT;
127 buffer.payload.data = snapshot;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800128
Tom Cherry2a6811b2019-12-11 12:56:01 -0800129 newVec[headerLength].iov_base = &buffer;
130 newVec[headerLength].iov_len = sizeof(buffer);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800131
Tom Cherry2a6811b2019-12-11 12:56:01 -0800132 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, 2));
133 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
134 atomic_fetch_add_explicit(&droppedSecurity, snapshot, memory_order_relaxed);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800135 }
Tom Cherry2a6811b2019-12-11 12:56:01 -0800136 }
137 snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
138 if (snapshot && __android_log_is_loggable_len(ANDROID_LOG_INFO, "liblog", strlen("liblog"),
139 ANDROID_LOG_VERBOSE)) {
140 android_log_event_int_t buffer;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800141
Tom Cherry2a6811b2019-12-11 12:56:01 -0800142 header.id = LOG_ID_EVENTS;
143 buffer.header.tag = LIBLOG_LOG_TAG;
144 buffer.payload.type = EVENT_TYPE_INT;
145 buffer.payload.data = snapshot;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800146
Tom Cherry2a6811b2019-12-11 12:56:01 -0800147 newVec[headerLength].iov_base = &buffer;
148 newVec[headerLength].iov_len = sizeof(buffer);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800149
Tom Cherry2a6811b2019-12-11 12:56:01 -0800150 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, 2));
151 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
152 atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800153 }
154 }
155
156 header.id = logId;
157
158 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
159 newVec[i].iov_base = vec[i - headerLength].iov_base;
160 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
161
162 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
163 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
164 if (newVec[i].iov_len) {
165 ++i;
166 }
167 break;
168 }
169 }
170
Tom Cherry2a6811b2019-12-11 12:56:01 -0800171 // The write below could be lost, but will never block.
172 // EAGAIN occurs if logd is overloaded, other errors indicate that something went wrong with
173 // the connection, so we reset it and try again.
174 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, i));
175 if (ret < 0 && errno != EAGAIN) {
Tom Cherry7acfba22020-04-24 17:15:26 -0700176 LogdConnect();
Tom Cherry2a6811b2019-12-11 12:56:01 -0800177
178 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, i));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800179 }
Mark Salyzyn65e1e622017-01-03 13:28:18 -0800180
Tom Cherry2a6811b2019-12-11 12:56:01 -0800181 if (ret < 0) {
182 ret = -errno;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800183 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800184
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800185 if (ret > (ssize_t)sizeof(header)) {
186 ret -= sizeof(header);
Tom Cherry2a6811b2019-12-11 12:56:01 -0800187 } else if (ret < 0) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800188 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
189 if (logId == LOG_ID_SECURITY) {
190 atomic_fetch_add_explicit(&droppedSecurity, 1, memory_order_relaxed);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800191 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800192 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800193
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800194 return ret;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800195}