summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Salyzyn2016-03-01 15:45:42 -0600
committerDan Willemsen2016-03-22 15:01:15 -0500
commit018a96d03f0d452bf078084eedcd5693da42308d (patch)
tree28a998736c3637944985ccacb20539527e19d3e5 /liblog/logd_writer.c
parent80b1b188281b0c89e94cd9c3c3f2b04f007d6b28 (diff)
downloadplatform-system-core-018a96d03f0d452bf078084eedcd5693da42308d.tar.gz
platform-system-core-018a96d03f0d452bf078084eedcd5693da42308d.tar.xz
platform-system-core-018a96d03f0d452bf078084eedcd5693da42308d.zip
liblog: split out transports into separate files
Create config_logger, logger and logger_read to house the log interfaces. Add fake_logger, logd_logger and pmsg_logger to house the write and read transports. Allows for an easier and direct path to add new transports to the library. SideEffects: None, logger benchmark performance unaffected Bug: 27176738 Bug: 27405083 Change-Id: I01b38637334a5242905c8c89f6ab0a92e2540008
Diffstat (limited to 'liblog/logd_writer.c')
-rw-r--r--liblog/logd_writer.c267
1 files changed, 267 insertions, 0 deletions
diff --git a/liblog/logd_writer.c b/liblog/logd_writer.c
new file mode 100644
index 000000000..696237d34
--- /dev/null
+++ b/liblog/logd_writer.c
@@ -0,0 +1,267 @@
1/*
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) {
70 i = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0));
71 if (i < 0) {
72 ret = -errno;
73 } else if (TEMP_FAILURE_RETRY(fcntl(i, F_SETFL, O_NONBLOCK)) < 0) {
74 ret = -errno;
75 close(i);
76 } else {
77 struct sockaddr_un un;
78 memset(&un, 0, sizeof(struct sockaddr_un));
79 un.sun_family = AF_UNIX;
80 strcpy(un.sun_path, "/dev/socket/logdw");
81
82 if (TEMP_FAILURE_RETRY(connect(i, (struct sockaddr *)&un,
83 sizeof(struct sockaddr_un))) < 0) {
84 ret = -errno;
85 close(i);
86 } else {
87 logdLoggerWrite.context.sock = i;
88 }
89 }
90 }
91
92 return ret;
93}
94
95static void logdClose()
96{
97 if (logdLoggerWrite.context.sock >= 0) {
98 close(logdLoggerWrite.context.sock);
99 logdLoggerWrite.context.sock = -1;
100 }
101}
102
103static int logdAvailable(log_id_t logId)
104{
105 if (logId > LOG_ID_SECURITY) {
106 return -EINVAL;
107 }
108 if (logId == LOG_ID_SECURITY) {
109 uid_t uid = __android_log_uid();
110 if ((uid != AID_LOG) && (uid != AID_ROOT) && (uid != AID_SYSTEM)) {
111 return -EPERM;
112 }
113 }
114 if (logdLoggerWrite.context.sock < 0) {
115 if (access("/dev/socket/logdw", W_OK) == 0) {
116 return 0;
117 }
118 return -EBADF;
119 }
120 return 1;
121}
122
123static int logdWrite(log_id_t logId, struct timespec *ts,
124 struct iovec *vec, size_t nr)
125{
126 ssize_t ret;
127 static const unsigned headerLength = 1;
128 struct iovec newVec[nr + headerLength];
129 android_log_header_t header;
130 size_t i, payloadSize;
131 static atomic_int_fast32_t dropped;
132 static atomic_int_fast32_t droppedSecurity;
133
134 if (logdLoggerWrite.context.sock < 0) {
135 return -EBADF;
136 }
137
138 /* logd, after initialization and priv drop */
139 if (__android_log_uid() == AID_LOGD) {
140 /*
141 * ignore log messages we send to ourself (logd).
142 * Such log messages are often generated by libraries we depend on
143 * which use standard Android logging.
144 */
145 return 0;
146 }
147
148 /*
149 * struct {
150 * // what we provide to socket
151 * android_log_header_t header;
152 * // caller provides
153 * union {
154 * struct {
155 * char prio;
156 * char payload[];
157 * } string;
158 * struct {
159 * uint32_t tag
160 * char payload[];
161 * } binary;
162 * };
163 * };
164 */
165
166 header.tid = gettid();
167 header.realtime.tv_sec = ts->tv_sec;
168 header.realtime.tv_nsec = ts->tv_nsec;
169
170 newVec[0].iov_base = (unsigned char *)&header;
171 newVec[0].iov_len = sizeof(header);
172
173 if (logdLoggerWrite.context.sock > 0) {
174 int32_t snapshot = atomic_exchange_explicit(&droppedSecurity, 0,
175 memory_order_relaxed);
176 if (snapshot) {
177 android_log_event_int_t buffer;
178
179 header.id = LOG_ID_SECURITY;
180 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
181 buffer.payload.type = EVENT_TYPE_INT;
182 buffer.payload.data = htole32(snapshot);
183
184 newVec[headerLength].iov_base = &buffer;
185 newVec[headerLength].iov_len = sizeof(buffer);
186
187 ret = TEMP_FAILURE_RETRY(writev(logdLoggerWrite.context.sock, newVec, 2));
188 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
189 atomic_fetch_add_explicit(&droppedSecurity, snapshot,
190 memory_order_relaxed);
191 }
192 }
193 snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
194 if (snapshot && __android_log_is_loggable(ANDROID_LOG_INFO,
195 "liblog",
196 ANDROID_LOG_VERBOSE)) {
197 android_log_event_int_t buffer;
198
199 header.id = LOG_ID_EVENTS;
200 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
201 buffer.payload.type = EVENT_TYPE_INT;
202 buffer.payload.data = htole32(snapshot);
203
204 newVec[headerLength].iov_base = &buffer;
205 newVec[headerLength].iov_len = sizeof(buffer);
206
207 ret = TEMP_FAILURE_RETRY(writev(logdLoggerWrite.context.sock, newVec, 2));
208 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
209 atomic_fetch_add_explicit(&dropped, snapshot,
210 memory_order_relaxed);
211 }
212 }
213 }
214
215 header.id = logId;
216
217 for (payloadSize = 0, i = headerLength; i < nr + headerLength; i++) {
218 newVec[i].iov_base = vec[i - headerLength].iov_base;
219 payloadSize += newVec[i].iov_len = vec[i - headerLength].iov_len;
220
221 if (payloadSize > LOGGER_ENTRY_MAX_PAYLOAD) {
222 newVec[i].iov_len -= payloadSize - LOGGER_ENTRY_MAX_PAYLOAD;
223 if (newVec[i].iov_len) {
224 ++i;
225 }
226 break;
227 }
228 }
229
230 /*
231 * The write below could be lost, but will never block.
232 *
233 * ENOTCONN occurs if logd dies.
234 * EAGAIN occurs if logd is overloaded.
235 */
236 ret = TEMP_FAILURE_RETRY(writev(logdLoggerWrite.context.sock, newVec, i));
237 if (ret < 0) {
238 ret = -errno;
239 if (ret == -ENOTCONN) {
240 __android_log_lock();
241 logdClose();
242 ret = logdOpen();
243 __android_log_unlock();
244
245 if (ret < 0) {
246 return ret;
247 }
248
249 ret = TEMP_FAILURE_RETRY(writev(logdLoggerWrite.context.sock, newVec, i));
250 if (ret < 0) {
251 ret = -errno;
252 }
253 }
254 }
255
256 if (ret > (ssize_t)sizeof(header)) {
257 ret -= sizeof(header);
258 } else if (ret == -EAGAIN) {
259 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
260 if (logId == LOG_ID_SECURITY) {
261 atomic_fetch_add_explicit(&droppedSecurity, 1,
262 memory_order_relaxed);
263 }
264 }
265
266 return ret;
267}