blob: ed82902b450f8f578fd232989c4b2cabb54d2ad5 [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>
35#include <log/logd.h>
36#include <log/logger.h>
37#include <log/log_read.h>
38#include <private/android_filesystem_config.h>
39#include <private/android_logger.h>
40
41#include "config_write.h"
42#include "log_portability.h"
43#include "logger.h"
44
45/* branchless on many architectures. */
46#define min(x,y) ((y) ^ (((x) ^ (y)) & -((x) < (y))))
47
48static int logdAvailable(log_id_t LogId);
49static int logdOpen();
50static void logdClose();
51static int logdWrite(log_id_t logId, struct timespec *ts,
52 struct iovec *vec, size_t nr);
53
54LIBLOG_HIDDEN struct android_log_transport_write logdLoggerWrite = {
55 .node = { &logdLoggerWrite.node, &logdLoggerWrite.node },
56 .context.sock = -1,
57 .name = "logd",
58 .available = logdAvailable,
59 .open = logdOpen,
60 .close = logdClose,
61 .write = logdWrite,
62};
63
64/* log_init_lock assumed */
65static int logdOpen()
66{
67 int i, ret = 0;
68
69 if (logdLoggerWrite.context.sock < 0) {
Elliott Hughes5ba30032016-08-22 14:17:58 -070070 i = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
Mark Salyzyn018a96d2016-03-01 13:45:42 -080071 if (i < 0) {
72 ret = -errno;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080073 } else {
74 struct sockaddr_un un;
75 memset(&un, 0, sizeof(struct sockaddr_un));
76 un.sun_family = AF_UNIX;
77 strcpy(un.sun_path, "/dev/socket/logdw");
78
79 if (TEMP_FAILURE_RETRY(connect(i, (struct sockaddr *)&un,
80 sizeof(struct sockaddr_un))) < 0) {
81 ret = -errno;
82 close(i);
83 } else {
84 logdLoggerWrite.context.sock = i;
85 }
86 }
87 }
88
89 return ret;
90}
91
92static void logdClose()
93{
94 if (logdLoggerWrite.context.sock >= 0) {
95 close(logdLoggerWrite.context.sock);
96 logdLoggerWrite.context.sock = -1;
97 }
98}
99
100static int logdAvailable(log_id_t logId)
101{
102 if (logId > LOG_ID_SECURITY) {
103 return -EINVAL;
104 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800105 if (logdLoggerWrite.context.sock < 0) {
106 if (access("/dev/socket/logdw", W_OK) == 0) {
107 return 0;
108 }
109 return -EBADF;
110 }
111 return 1;
112}
113
114static int logdWrite(log_id_t logId, struct timespec *ts,
115 struct iovec *vec, size_t nr)
116{
117 ssize_t ret;
118 static const unsigned headerLength = 1;
119 struct iovec newVec[nr + headerLength];
120 android_log_header_t header;
121 size_t i, payloadSize;
122 static atomic_int_fast32_t dropped;
123 static atomic_int_fast32_t droppedSecurity;
124
125 if (logdLoggerWrite.context.sock < 0) {
126 return -EBADF;
127 }
128
129 /* logd, after initialization and priv drop */
130 if (__android_log_uid() == AID_LOGD) {
131 /*
132 * ignore log messages we send to ourself (logd).
133 * Such log messages are often generated by libraries we depend on
134 * which use standard Android logging.
135 */
136 return 0;
137 }
138
139 /*
140 * struct {
141 * // what we provide to socket
142 * android_log_header_t header;
143 * // caller provides
144 * union {
145 * struct {
146 * char prio;
147 * char payload[];
148 * } string;
149 * struct {
150 * uint32_t tag
151 * char payload[];
152 * } binary;
153 * };
154 * };
155 */
156
157 header.tid = gettid();
158 header.realtime.tv_sec = ts->tv_sec;
159 header.realtime.tv_nsec = ts->tv_nsec;
160
161 newVec[0].iov_base = (unsigned char *)&header;
162 newVec[0].iov_len = sizeof(header);
163
164 if (logdLoggerWrite.context.sock > 0) {
165 int32_t snapshot = atomic_exchange_explicit(&droppedSecurity, 0,
166 memory_order_relaxed);
167 if (snapshot) {
168 android_log_event_int_t buffer;
169
170 header.id = LOG_ID_SECURITY;
171 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
172 buffer.payload.type = EVENT_TYPE_INT;
173 buffer.payload.data = htole32(snapshot);
174
175 newVec[headerLength].iov_base = &buffer;
176 newVec[headerLength].iov_len = sizeof(buffer);
177
178 ret = TEMP_FAILURE_RETRY(writev(logdLoggerWrite.context.sock, newVec, 2));
179 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
180 atomic_fetch_add_explicit(&droppedSecurity, snapshot,
181 memory_order_relaxed);
182 }
183 }
184 snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
185 if (snapshot && __android_log_is_loggable(ANDROID_LOG_INFO,
186 "liblog",
187 ANDROID_LOG_VERBOSE)) {
188 android_log_event_int_t buffer;
189
190 header.id = LOG_ID_EVENTS;
191 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
192 buffer.payload.type = EVENT_TYPE_INT;
193 buffer.payload.data = htole32(snapshot);
194
195 newVec[headerLength].iov_base = &buffer;
196 newVec[headerLength].iov_len = sizeof(buffer);
197
198 ret = TEMP_FAILURE_RETRY(writev(logdLoggerWrite.context.sock, newVec, 2));
199 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
200 atomic_fetch_add_explicit(&dropped, snapshot,
201 memory_order_relaxed);
202 }
203 }
204 }
205
206 header.id = logId;
207
208 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
209 newVec[i].iov_base = vec[i - headerLength].iov_base;
210 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
211
212 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
213 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
214 if (newVec[i].iov_len) {
215 ++i;
216 }
217 break;
218 }
219 }
220
221 /*
222 * The write below could be lost, but will never block.
223 *
224 * ENOTCONN occurs if logd dies.
225 * EAGAIN occurs if logd is overloaded.
226 */
227 ret = TEMP_FAILURE_RETRY(writev(logdLoggerWrite.context.sock, newVec, i));
228 if (ret < 0) {
229 ret = -errno;
230 if (ret == -ENOTCONN) {
231 __android_log_lock();
232 logdClose();
233 ret = logdOpen();
234 __android_log_unlock();
235
236 if (ret < 0) {
237 return ret;
238 }
239
240 ret = TEMP_FAILURE_RETRY(writev(logdLoggerWrite.context.sock, newVec, i));
241 if (ret < 0) {
242 ret = -errno;
243 }
244 }
245 }
246
247 if (ret > (ssize_t)sizeof(header)) {
248 ret -= sizeof(header);
249 } else if (ret == -EAGAIN) {
250 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
251 if (logId == LOG_ID_SECURITY) {
252 atomic_fetch_add_explicit(&droppedSecurity, 1,
253 memory_order_relaxed);
254 }
255 }
256
257 return ret;
258}