blob: 67376f41117ae4212ca75222993da24f80a83c98 [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 "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 logd_socket;
45static RwLock logd_socket_lock;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080046
Tom Cherry2a6811b2019-12-11 12:56:01 -080047static void OpenSocketLocked() {
48 logd_socket = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
49 if (logd_socket <= 0) {
50 return;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080051 }
52
Tom Cherry2a6811b2019-12-11 12:56:01 -080053 sockaddr_un un = {};
54 un.sun_family = AF_UNIX;
55 strcpy(un.sun_path, "/dev/socket/logdw");
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080056
Tom Cherry2a6811b2019-12-11 12:56:01 -080057 if (TEMP_FAILURE_RETRY(
58 connect(logd_socket, reinterpret_cast<sockaddr*>(&un), sizeof(sockaddr_un))) < 0) {
59 close(logd_socket);
60 logd_socket = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080061 }
62}
63
Tom Cherry2a6811b2019-12-11 12:56:01 -080064static void OpenSocket() {
65 auto lock = std::unique_lock{logd_socket_lock};
66 if (logd_socket > 0) {
67 // Someone raced us and opened the socket already.
68 return;
69 }
70
71 OpenSocketLocked();
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080072}
73
Tom Cherry2a6811b2019-12-11 12:56:01 -080074static void ResetSocket(int old_socket) {
75 auto lock = std::unique_lock{logd_socket_lock};
76 if (old_socket != logd_socket) {
77 // Someone raced us and reset the socket already.
78 return;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080079 }
Tom Cherry2a6811b2019-12-11 12:56:01 -080080 close(logd_socket);
81 logd_socket = 0;
82 OpenSocketLocked();
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080083}
84
Tom Cherry21bb36c2020-01-08 15:18:26 -080085void LogdClose() {
Tom Cherry2a6811b2019-12-11 12:56:01 -080086 auto lock = std::unique_lock{logd_socket_lock};
87 if (logd_socket > 0) {
88 close(logd_socket);
89 }
90 logd_socket = 0;
91}
92
Tom Cherry21bb36c2020-01-08 15:18:26 -080093int LogdWrite(log_id_t logId, struct timespec* ts, struct iovec* vec, size_t nr) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080094 ssize_t ret;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080095 static const unsigned headerLength = 1;
96 struct iovec newVec[nr + headerLength];
97 android_log_header_t header;
98 size_t i, payloadSize;
Tom Cherry71ba1642019-01-10 10:37:36 -080099 static atomic_int dropped;
100 static atomic_int droppedSecurity;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800101
Tom Cherry2a6811b2019-12-11 12:56:01 -0800102 auto lock = std::shared_lock{logd_socket_lock};
103 if (logd_socket <= 0) {
104 lock.unlock();
105 OpenSocket();
106 lock.lock();
107 }
108
109 if (logd_socket <= 0) {
110 return -EBADF;
111 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800112
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800113 /* logd, after initialization and priv drop */
Tom Cherryb47aa2a2020-01-08 15:34:14 -0800114 if (getuid() == AID_LOGD) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800115 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800116 * ignore log messages we send to ourself (logd).
117 * Such log messages are often generated by libraries we depend on
118 * which use standard Android logging.
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800119 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800120 return 0;
121 }
122
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800123 header.tid = gettid();
124 header.realtime.tv_sec = ts->tv_sec;
125 header.realtime.tv_nsec = ts->tv_nsec;
126
127 newVec[0].iov_base = (unsigned char*)&header;
128 newVec[0].iov_len = sizeof(header);
129
Tom Cherry2a6811b2019-12-11 12:56:01 -0800130 int32_t snapshot = atomic_exchange_explicit(&droppedSecurity, 0, memory_order_relaxed);
131 if (snapshot) {
132 android_log_event_int_t buffer;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800133
Tom Cherry2a6811b2019-12-11 12:56:01 -0800134 header.id = LOG_ID_SECURITY;
135 buffer.header.tag = LIBLOG_LOG_TAG;
136 buffer.payload.type = EVENT_TYPE_INT;
137 buffer.payload.data = snapshot;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800138
Tom Cherry2a6811b2019-12-11 12:56:01 -0800139 newVec[headerLength].iov_base = &buffer;
140 newVec[headerLength].iov_len = sizeof(buffer);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800141
Tom Cherry2a6811b2019-12-11 12:56:01 -0800142 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, 2));
143 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
144 atomic_fetch_add_explicit(&droppedSecurity, snapshot, memory_order_relaxed);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800145 }
Tom Cherry2a6811b2019-12-11 12:56:01 -0800146 }
147 snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
148 if (snapshot && __android_log_is_loggable_len(ANDROID_LOG_INFO, "liblog", strlen("liblog"),
149 ANDROID_LOG_VERBOSE)) {
150 android_log_event_int_t buffer;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800151
Tom Cherry2a6811b2019-12-11 12:56:01 -0800152 header.id = LOG_ID_EVENTS;
153 buffer.header.tag = LIBLOG_LOG_TAG;
154 buffer.payload.type = EVENT_TYPE_INT;
155 buffer.payload.data = snapshot;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800156
Tom Cherry2a6811b2019-12-11 12:56:01 -0800157 newVec[headerLength].iov_base = &buffer;
158 newVec[headerLength].iov_len = sizeof(buffer);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800159
Tom Cherry2a6811b2019-12-11 12:56:01 -0800160 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, 2));
161 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
162 atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800163 }
164 }
165
166 header.id = logId;
167
168 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
169 newVec[i].iov_base = vec[i - headerLength].iov_base;
170 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
171
172 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
173 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
174 if (newVec[i].iov_len) {
175 ++i;
176 }
177 break;
178 }
179 }
180
Tom Cherry2a6811b2019-12-11 12:56:01 -0800181 // The write below could be lost, but will never block.
182 // EAGAIN occurs if logd is overloaded, other errors indicate that something went wrong with
183 // the connection, so we reset it and try again.
184 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, i));
185 if (ret < 0 && errno != EAGAIN) {
186 int old_socket = logd_socket;
187 lock.unlock();
188 ResetSocket(old_socket);
189 lock.lock();
190
191 ret = TEMP_FAILURE_RETRY(writev(logd_socket, newVec, i));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800192 }
Mark Salyzyn65e1e622017-01-03 13:28:18 -0800193
Tom Cherry2a6811b2019-12-11 12:56:01 -0800194 if (ret < 0) {
195 ret = -errno;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800196 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800197
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800198 if (ret > (ssize_t)sizeof(header)) {
199 ret -= sizeof(header);
Tom Cherry2a6811b2019-12-11 12:56:01 -0800200 } else if (ret < 0) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800201 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
202 if (logId == LOG_ID_SECURITY) {
203 atomic_fetch_add_explicit(&droppedSecurity, 1, memory_order_relaxed);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800204 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800205 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800206
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800207 return ret;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800208}