blob: 8fdfb92d3e46059ecb0be11743f4c0f23f34767c [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
17#include <endian.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <inttypes.h>
21#include <poll.h>
22#include <stdarg.h>
23#include <stdatomic.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29#include <sys/socket.h>
30#include <sys/un.h>
31#include <time.h>
32#include <unistd.h>
33
34#include <cutils/sockets.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080035#include <private/android_filesystem_config.h>
36#include <private/android_logger.h>
37
38#include "config_write.h"
39#include "log_portability.h"
40#include "logger.h"
41
42/* branchless on many architectures. */
43#define min(x,y) ((y) ^ (((x) ^ (y)) & -((x) < (y))))
44
45static int logdAvailable(log_id_t LogId);
46static int logdOpen();
47static void logdClose();
48static int logdWrite(log_id_t logId, struct timespec *ts,
49 struct iovec *vec, size_t nr);
50
51LIBLOG_HIDDEN struct android_log_transport_write logdLoggerWrite = {
52 .node = { &logdLoggerWrite.node, &logdLoggerWrite.node },
53 .context.sock = -1,
54 .name = "logd",
55 .available = logdAvailable,
56 .open = logdOpen,
57 .close = logdClose,
58 .write = logdWrite,
59};
60
61/* log_init_lock assumed */
62static int logdOpen()
63{
64 int i, ret = 0;
65
Mark Salyzyndb8a2662016-10-10 07:27:42 -070066 i = atomic_load(&logdLoggerWrite.context.sock);
67 if (i < 0) {
Elliott Hughes5ba30032016-08-22 14:17:58 -070068 i = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
Mark Salyzyn018a96d2016-03-01 13:45:42 -080069 if (i < 0) {
70 ret = -errno;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080071 } else {
72 struct sockaddr_un un;
73 memset(&un, 0, sizeof(struct sockaddr_un));
74 un.sun_family = AF_UNIX;
75 strcpy(un.sun_path, "/dev/socket/logdw");
76
77 if (TEMP_FAILURE_RETRY(connect(i, (struct sockaddr *)&un,
78 sizeof(struct sockaddr_un))) < 0) {
79 ret = -errno;
80 close(i);
81 } else {
Mark Salyzyndb8a2662016-10-10 07:27:42 -070082 ret = atomic_exchange(&logdLoggerWrite.context.sock, i);
83 if ((ret >= 0) && (ret != i)) {
84 close(ret);
85 }
86 ret = 0;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080087 }
88 }
89 }
90
91 return ret;
92}
93
94static void logdClose()
95{
Mark Salyzyndb8a2662016-10-10 07:27:42 -070096 int sock = atomic_exchange(&logdLoggerWrite.context.sock, -1);
97 if (sock >= 0) {
98 close(sock);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080099 }
100}
101
102static int logdAvailable(log_id_t logId)
103{
104 if (logId > LOG_ID_SECURITY) {
105 return -EINVAL;
106 }
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700107 if (atomic_load(&logdLoggerWrite.context.sock) < 0) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800108 if (access("/dev/socket/logdw", W_OK) == 0) {
109 return 0;
110 }
111 return -EBADF;
112 }
113 return 1;
114}
115
116static int logdWrite(log_id_t logId, struct timespec *ts,
117 struct iovec *vec, size_t nr)
118{
119 ssize_t ret;
120 static const unsigned headerLength = 1;
121 struct iovec newVec[nr + headerLength];
122 android_log_header_t header;
123 size_t i, payloadSize;
124 static atomic_int_fast32_t dropped;
125 static atomic_int_fast32_t droppedSecurity;
126
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700127 if (atomic_load(&logdLoggerWrite.context.sock) < 0) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800128 return -EBADF;
129 }
130
131 /* logd, after initialization and priv drop */
132 if (__android_log_uid() == AID_LOGD) {
133 /*
134 * ignore log messages we send to ourself (logd).
135 * Such log messages are often generated by libraries we depend on
136 * which use standard Android logging.
137 */
138 return 0;
139 }
140
141 /*
142 * struct {
143 * // what we provide to socket
144 * android_log_header_t header;
145 * // caller provides
146 * union {
147 * struct {
148 * char prio;
149 * char payload[];
150 * } string;
151 * struct {
152 * uint32_t tag
153 * char payload[];
154 * } binary;
155 * };
156 * };
157 */
158
159 header.tid = gettid();
160 header.realtime.tv_sec = ts->tv_sec;
161 header.realtime.tv_nsec = ts->tv_nsec;
162
163 newVec[0].iov_base = (unsigned char *)&header;
164 newVec[0].iov_len = sizeof(header);
165
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700166 if (atomic_load(&logdLoggerWrite.context.sock) > 0) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800167 int32_t snapshot = atomic_exchange_explicit(&droppedSecurity, 0,
168 memory_order_relaxed);
169 if (snapshot) {
170 android_log_event_int_t buffer;
171
172 header.id = LOG_ID_SECURITY;
173 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
174 buffer.payload.type = EVENT_TYPE_INT;
175 buffer.payload.data = htole32(snapshot);
176
177 newVec[headerLength].iov_base = &buffer;
178 newVec[headerLength].iov_len = sizeof(buffer);
179
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700180 ret = TEMP_FAILURE_RETRY(writev(
181 atomic_load(&logdLoggerWrite.context.sock), newVec, 2));
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800182 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
183 atomic_fetch_add_explicit(&droppedSecurity, snapshot,
184 memory_order_relaxed);
185 }
186 }
187 snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
Mark Salyzyn807e40e2016-09-22 09:56:51 -0700188 if (snapshot && __android_log_is_loggable_len(ANDROID_LOG_INFO,
189 "liblog", strlen("liblog"),
190 ANDROID_LOG_VERBOSE)) {
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800191 android_log_event_int_t buffer;
192
193 header.id = LOG_ID_EVENTS;
194 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
195 buffer.payload.type = EVENT_TYPE_INT;
196 buffer.payload.data = htole32(snapshot);
197
198 newVec[headerLength].iov_base = &buffer;
199 newVec[headerLength].iov_len = sizeof(buffer);
200
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700201 ret = TEMP_FAILURE_RETRY(writev(
202 atomic_load(&logdLoggerWrite.context.sock), newVec, 2));
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800203 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
204 atomic_fetch_add_explicit(&dropped, snapshot,
205 memory_order_relaxed);
206 }
207 }
208 }
209
210 header.id = logId;
211
212 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
213 newVec[i].iov_base = vec[i - headerLength].iov_base;
214 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
215
216 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
217 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
218 if (newVec[i].iov_len) {
219 ++i;
220 }
221 break;
222 }
223 }
224
225 /*
226 * The write below could be lost, but will never block.
227 *
228 * ENOTCONN occurs if logd dies.
229 * EAGAIN occurs if logd is overloaded.
230 */
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700231 ret = TEMP_FAILURE_RETRY(writev(
232 atomic_load(&logdLoggerWrite.context.sock), newVec, i));
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800233 if (ret < 0) {
234 ret = -errno;
235 if (ret == -ENOTCONN) {
236 __android_log_lock();
237 logdClose();
238 ret = logdOpen();
239 __android_log_unlock();
240
241 if (ret < 0) {
242 return ret;
243 }
244
Mark Salyzyndb8a2662016-10-10 07:27:42 -0700245 ret = TEMP_FAILURE_RETRY(writev(
246 atomic_load(&logdLoggerWrite.context.sock), newVec, i));
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800247 if (ret < 0) {
248 ret = -errno;
249 }
250 }
251 }
252
253 if (ret > (ssize_t)sizeof(header)) {
254 ret -= sizeof(header);
255 } else if (ret == -EAGAIN) {
256 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
257 if (logId == LOG_ID_SECURITY) {
258 atomic_fetch_add_explicit(&droppedSecurity, 1,
259 memory_order_relaxed);
260 }
261 }
262
263 return ret;
264}