1 /*
2 ** Copyright 2013-2014, 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 "log/log_read.h"
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <pthread.h>
22 #include <sched.h>
23 #include <stddef.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include <android/log.h>
29 #include <cutils/list.h>
30 #include <private/android_filesystem_config.h>
31
32 #include "config_read.h"
33 #include "log_portability.h"
34 #include "logger.h"
35
36 /* android_logger_alloc unimplemented, no use case */
37 /* android_logger_free not exported */
android_logger_free(struct logger * logger)38 static void android_logger_free(struct logger* logger) {
39 struct android_log_logger* logger_internal = (struct android_log_logger*)logger;
40
41 if (!logger_internal) {
42 return;
43 }
44
45 list_remove(&logger_internal->node);
46
47 free(logger_internal);
48 }
49
50 /* android_logger_alloc unimplemented, no use case */
51
52 /* method for getting the associated sublog id */
android_logger_get_id(struct logger * logger)53 log_id_t android_logger_get_id(struct logger* logger) {
54 return ((struct android_log_logger*)logger)->logId;
55 }
56
init_transport_context(struct android_log_logger_list * logger_list)57 static int init_transport_context(struct android_log_logger_list* logger_list) {
58 struct android_log_transport_read* transport;
59 struct listnode* node;
60
61 if (!logger_list) {
62 return -EINVAL;
63 }
64
65 if (list_empty(&logger_list->logger)) {
66 return -EINVAL;
67 }
68
69 if (!list_empty(&logger_list->transport)) {
70 return 0;
71 }
72
73 __android_log_lock();
74 /* mini __write_to_log_initialize() to populate transports */
75 if (list_empty(&__android_log_transport_read) && list_empty(&__android_log_persist_read)) {
76 __android_log_config_read();
77 }
78 __android_log_unlock();
79
80 node = (logger_list->mode & ANDROID_LOG_PSTORE) ? &__android_log_persist_read
81 : &__android_log_transport_read;
82
83 read_transport_for_each(transport, node) {
84 struct android_log_transport_context* transp;
85 struct android_log_logger* logger;
86 unsigned logMask = 0;
87
88 logger_for_each(logger, logger_list) {
89 log_id_t logId = logger->logId;
90
91 if ((logId == LOG_ID_SECURITY) && (__android_log_uid() != AID_SYSTEM)) {
92 continue;
93 }
94 if (transport->read && (!transport->available || (transport->available(logId) >= 0))) {
95 logMask |= 1 << logId;
96 }
97 }
98 if (!logMask) {
99 continue;
100 }
101 transp = static_cast<android_log_transport_context*>(calloc(1, sizeof(*transp)));
102 if (!transp) {
103 return -ENOMEM;
104 }
105 transp->parent = logger_list;
106 transp->transport = transport;
107 transp->logMask = logMask;
108 transp->ret = 1;
109 list_add_tail(&logger_list->transport, &transp->node);
110 }
111 if (list_empty(&logger_list->transport)) {
112 return -ENODEV;
113 }
114 return 0;
115 }
116
117 #define LOGGER_FUNCTION(logger, def, func, args...) \
118 ssize_t ret = -EINVAL; \
119 struct android_log_transport_context* transp; \
120 struct android_log_logger* logger_internal = (struct android_log_logger*)(logger); \
121 \
122 if (!logger_internal) { \
123 return ret; \
124 } \
125 ret = init_transport_context(logger_internal->parent); \
126 if (ret < 0) { \
127 return ret; \
128 } \
129 \
130 ret = (def); \
131 transport_context_for_each(transp, logger_internal->parent) { \
132 if ((transp->logMask & (1 << logger_internal->logId)) && transp->transport && \
133 transp->transport->func) { \
134 ssize_t retval = (transp->transport->func)(logger_internal, transp, ##args); \
135 if ((ret >= 0) || (ret == (def))) { \
136 ret = retval; \
137 } \
138 } \
139 } \
140 return ret
141
android_logger_clear(struct logger * logger)142 int android_logger_clear(struct logger* logger) {
143 LOGGER_FUNCTION(logger, -ENODEV, clear);
144 }
145
146 /* returns the total size of the log's ring buffer */
android_logger_get_log_size(struct logger * logger)147 long android_logger_get_log_size(struct logger* logger) {
148 LOGGER_FUNCTION(logger, -ENODEV, getSize);
149 }
150
android_logger_set_log_size(struct logger * logger,unsigned long size)151 int android_logger_set_log_size(struct logger* logger, unsigned long size) {
152 LOGGER_FUNCTION(logger, -ENODEV, setSize, size);
153 }
154
155 /*
156 * returns the readable size of the log's ring buffer (that is, amount of the
157 * log consumed)
158 */
android_logger_get_log_readable_size(struct logger * logger)159 long android_logger_get_log_readable_size(struct logger* logger) {
160 LOGGER_FUNCTION(logger, -ENODEV, getReadableSize);
161 }
162
163 /*
164 * returns the logger version
165 */
android_logger_get_log_version(struct logger * logger)166 int android_logger_get_log_version(struct logger* logger) {
167 LOGGER_FUNCTION(logger, 4, version);
168 }
169
170 #define LOGGER_LIST_FUNCTION(logger_list, def, func, args...) \
171 struct android_log_transport_context* transp; \
172 struct android_log_logger_list* logger_list_internal = \
173 (struct android_log_logger_list*)(logger_list); \
174 \
175 ssize_t ret = init_transport_context(logger_list_internal); \
176 if (ret < 0) { \
177 return ret; \
178 } \
179 \
180 ret = (def); \
181 transport_context_for_each(transp, logger_list_internal) { \
182 if (transp->transport && (transp->transport->func)) { \
183 ssize_t retval = (transp->transport->func)(logger_list_internal, transp, ##args); \
184 if ((ret >= 0) || (ret == (def))) { \
185 ret = retval; \
186 } \
187 } \
188 } \
189 return ret
190
191 /*
192 * returns statistics
193 */
android_logger_get_statistics(struct logger_list * logger_list,char * buf,size_t len)194 ssize_t android_logger_get_statistics(struct logger_list* logger_list, char* buf, size_t len) {
195 LOGGER_LIST_FUNCTION(logger_list, -ENODEV, getStats, buf, len);
196 }
197
android_logger_get_prune_list(struct logger_list * logger_list,char * buf,size_t len)198 ssize_t android_logger_get_prune_list(struct logger_list* logger_list, char* buf, size_t len) {
199 LOGGER_LIST_FUNCTION(logger_list, -ENODEV, getPrune, buf, len);
200 }
201
android_logger_set_prune_list(struct logger_list * logger_list,char * buf,size_t len)202 int android_logger_set_prune_list(struct logger_list* logger_list, char* buf, size_t len) {
203 LOGGER_LIST_FUNCTION(logger_list, -ENODEV, setPrune, buf, len);
204 }
205
android_logger_list_alloc(int mode,unsigned int tail,pid_t pid)206 struct logger_list* android_logger_list_alloc(int mode, unsigned int tail, pid_t pid) {
207 struct android_log_logger_list* logger_list;
208
209 logger_list = static_cast<android_log_logger_list*>(calloc(1, sizeof(*logger_list)));
210 if (!logger_list) {
211 return NULL;
212 }
213
214 list_init(&logger_list->logger);
215 list_init(&logger_list->transport);
216 logger_list->mode = mode;
217 logger_list->tail = tail;
218 logger_list->pid = pid;
219
220 return (struct logger_list*)logger_list;
221 }
222
android_logger_list_alloc_time(int mode,log_time start,pid_t pid)223 struct logger_list* android_logger_list_alloc_time(int mode, log_time start, pid_t pid) {
224 struct android_log_logger_list* logger_list;
225
226 logger_list = static_cast<android_log_logger_list*>(calloc(1, sizeof(*logger_list)));
227 if (!logger_list) {
228 return NULL;
229 }
230
231 list_init(&logger_list->logger);
232 list_init(&logger_list->transport);
233 logger_list->mode = mode;
234 logger_list->start = start;
235 logger_list->pid = pid;
236
237 return (struct logger_list*)logger_list;
238 }
239
240 /* android_logger_list_register unimplemented, no use case */
241 /* android_logger_list_unregister unimplemented, no use case */
242
243 /* Open the named log and add it to the logger list */
android_logger_open(struct logger_list * logger_list,log_id_t logId)244 struct logger* android_logger_open(struct logger_list* logger_list, log_id_t logId) {
245 struct android_log_logger_list* logger_list_internal =
246 (struct android_log_logger_list*)logger_list;
247 struct android_log_logger* logger;
248
249 if (!logger_list_internal || (logId >= LOG_ID_MAX)) {
250 goto err;
251 }
252
253 logger_for_each(logger, logger_list_internal) {
254 if (logger->logId == logId) {
255 goto ok;
256 }
257 }
258
259 logger = static_cast<android_log_logger*>(calloc(1, sizeof(*logger)));
260 if (!logger) {
261 goto err;
262 }
263
264 logger->logId = logId;
265 list_add_tail(&logger_list_internal->logger, &logger->node);
266 logger->parent = logger_list_internal;
267
268 /* Reset known transports to re-evaluate, we just added one */
269 while (!list_empty(&logger_list_internal->transport)) {
270 struct listnode* node = list_head(&logger_list_internal->transport);
271 struct android_log_transport_context* transp =
272 node_to_item(node, struct android_log_transport_context, node);
273
274 list_remove(&transp->node);
275 free(transp);
276 }
277 goto ok;
278
279 err:
280 logger = NULL;
281 ok:
282 return (struct logger*)logger;
283 }
284
285 /* Open the single named log and make it part of a new logger list */
android_logger_list_open(log_id_t logId,int mode,unsigned int tail,pid_t pid)286 struct logger_list* android_logger_list_open(log_id_t logId, int mode, unsigned int tail,
287 pid_t pid) {
288 struct logger_list* logger_list = android_logger_list_alloc(mode, tail, pid);
289
290 if (!logger_list) {
291 return NULL;
292 }
293
294 if (!android_logger_open(logger_list, logId)) {
295 android_logger_list_free(logger_list);
296 return NULL;
297 }
298
299 return logger_list;
300 }
301
302 /* Validate log_msg packet, read function has already been null checked */
android_transport_read(struct android_log_logger_list * logger_list,struct android_log_transport_context * transp,struct log_msg * log_msg)303 static int android_transport_read(struct android_log_logger_list* logger_list,
304 struct android_log_transport_context* transp,
305 struct log_msg* log_msg) {
306 int ret = (*transp->transport->read)(logger_list, transp, log_msg);
307
308 if (ret > (int)sizeof(*log_msg)) {
309 ret = sizeof(*log_msg);
310 }
311
312 transp->ret = ret;
313
314 /* propagate errors, or make sure len & hdr_size members visible */
315 if (ret < (int)(sizeof(log_msg->entry.len) + sizeof(log_msg->entry.hdr_size))) {
316 if (ret >= (int)sizeof(log_msg->entry.len)) {
317 log_msg->entry.len = 0;
318 }
319 return ret;
320 }
321
322 /* hdr_size correction (logger_entry -> logger_entry_v2+ conversion) */
323 if (log_msg->entry_v2.hdr_size == 0) {
324 log_msg->entry_v2.hdr_size = sizeof(struct logger_entry);
325 }
326 if ((log_msg->entry_v2.hdr_size < sizeof(log_msg->entry_v1)) ||
327 (log_msg->entry_v2.hdr_size > sizeof(log_msg->entry))) {
328 return -EINVAL;
329 }
330
331 /* len validation */
332 if (ret <= log_msg->entry_v2.hdr_size) {
333 log_msg->entry.len = 0;
334 } else {
335 log_msg->entry.len = ret - log_msg->entry_v2.hdr_size;
336 }
337
338 return ret;
339 }
340
341 /* Read from the selected logs */
android_logger_list_read(struct logger_list * logger_list,struct log_msg * log_msg)342 int android_logger_list_read(struct logger_list* logger_list, struct log_msg* log_msg) {
343 struct android_log_transport_context* transp;
344 struct android_log_logger_list* logger_list_internal =
345 (struct android_log_logger_list*)logger_list;
346
347 int ret = init_transport_context(logger_list_internal);
348 if (ret < 0) {
349 return ret;
350 }
351
352 /* at least one transport */
353 transp = node_to_item(logger_list_internal->transport.next, struct android_log_transport_context,
354 node);
355
356 /* more than one transport? */
357 if (transp->node.next != &logger_list_internal->transport) {
358 /* Poll and merge sort the entries if from multiple transports */
359 struct android_log_transport_context* oldest = NULL;
360 int ret;
361 int polled = 0;
362 do {
363 if (polled) {
364 sched_yield();
365 }
366 ret = -1000;
367 polled = 0;
368 do {
369 int retval = transp->ret;
370 if ((retval > 0) && !transp->logMsg.entry.len) {
371 if (!transp->transport->read) {
372 retval = transp->ret = 0;
373 } else if ((logger_list_internal->mode & ANDROID_LOG_NONBLOCK) ||
374 !transp->transport->poll) {
375 retval = android_transport_read(logger_list_internal, transp, &transp->logMsg);
376 } else {
377 int pollval = (*transp->transport->poll)(logger_list_internal, transp);
378 if (pollval <= 0) {
379 sched_yield();
380 pollval = (*transp->transport->poll)(logger_list_internal, transp);
381 }
382 polled = 1;
383 if (pollval < 0) {
384 if ((pollval == -EINTR) || (pollval == -EAGAIN)) {
385 return -EAGAIN;
386 }
387 retval = transp->ret = pollval;
388 } else if (pollval > 0) {
389 retval = android_transport_read(logger_list_internal, transp, &transp->logMsg);
390 }
391 }
392 }
393 if (ret < retval) {
394 ret = retval;
395 }
396 if ((transp->ret > 0) && transp->logMsg.entry.len &&
397 (!oldest || (oldest->logMsg.entry.sec > transp->logMsg.entry.sec) ||
398 ((oldest->logMsg.entry.sec == transp->logMsg.entry.sec) &&
399 (oldest->logMsg.entry.nsec > transp->logMsg.entry.nsec)))) {
400 oldest = transp;
401 }
402 transp = node_to_item(transp->node.next, struct android_log_transport_context, node);
403 } while (transp != node_to_item(&logger_list_internal->transport,
404 struct android_log_transport_context, node));
405 if (!oldest && (logger_list_internal->mode & ANDROID_LOG_NONBLOCK)) {
406 return (ret < 0) ? ret : -EAGAIN;
407 }
408 transp = node_to_item(logger_list_internal->transport.next,
409 struct android_log_transport_context, node);
410 } while (!oldest && (ret > 0));
411 if (!oldest) {
412 return ret;
413 }
414 // ret is a positive value less than sizeof(struct log_msg)
415 ret = oldest->ret;
416 if (ret < oldest->logMsg.entry.hdr_size) {
417 // zero truncated header fields.
418 memset(
419 log_msg, 0,
420 (oldest->logMsg.entry.hdr_size > sizeof(oldest->logMsg) ? sizeof(oldest->logMsg)
421 : oldest->logMsg.entry.hdr_size));
422 }
423 memcpy(log_msg, &oldest->logMsg, ret);
424 oldest->logMsg.entry.len = 0; /* Mark it as copied */
425 return ret;
426 }
427
428 /* if only one, no need to copy into transport_context and merge-sort */
429 return android_transport_read(logger_list_internal, transp, log_msg);
430 }
431
432 /* Close all the logs */
android_logger_list_free(struct logger_list * logger_list)433 void android_logger_list_free(struct logger_list* logger_list) {
434 struct android_log_logger_list* logger_list_internal =
435 (struct android_log_logger_list*)logger_list;
436
437 if (logger_list_internal == NULL) {
438 return;
439 }
440
441 while (!list_empty(&logger_list_internal->transport)) {
442 struct listnode* node = list_head(&logger_list_internal->transport);
443 struct android_log_transport_context* transp =
444 node_to_item(node, struct android_log_transport_context, node);
445
446 if (transp->transport && transp->transport->close) {
447 (*transp->transport->close)(logger_list_internal, transp);
448 }
449 list_remove(&transp->node);
450 free(transp);
451 }
452
453 while (!list_empty(&logger_list_internal->logger)) {
454 struct listnode* node = list_head(&logger_list_internal->logger);
455 struct android_log_logger* logger = node_to_item(node, struct android_log_logger, node);
456 android_logger_free((struct logger*)logger);
457 }
458
459 free(logger_list_internal);
460 }
461