summaryrefslogtreecommitdiffstats
path: root/liblog
diff options
context:
space:
mode:
authorMark Salyzyn2015-07-28 11:30:47 -0500
committerMark Salyzyn2015-07-28 11:37:07 -0500
commit514243d5bd3f51e5696243799a072db66a962e4f (patch)
treeb9222f6a549e0ded32d60fa06b0fa871fccdbb4c /liblog
parentcb41e3673b81f246a45ac1ab38ba0c8490ceb0a1 (diff)
downloadplatform-system-core-514243d5bd3f51e5696243799a072db66a962e4f.tar.gz
platform-system-core-514243d5bd3f51e5696243799a072db66a962e4f.tar.xz
platform-system-core-514243d5bd3f51e5696243799a072db66a962e4f.zip
liblog: deprecate TARGET_USES_LOGD
This is not the kernel logger you are loiking for Bug: 22787659 Change-Id: Id65ed2e8e7ffe4b2be1bdeed65fa8db23bd66b51
Diffstat (limited to 'liblog')
-rw-r--r--liblog/Android.mk8
-rw-r--r--liblog/log_read_kern.c742
-rw-r--r--liblog/logd_write_kern.c295
-rw-r--r--liblog/tests/Android.mk4
-rw-r--r--liblog/tests/libc_test.cpp2
5 files changed, 0 insertions, 1051 deletions
diff --git a/liblog/Android.mk b/liblog/Android.mk
index ce282bd4a..930dcf729 100644
--- a/liblog/Android.mk
+++ b/liblog/Android.mk
@@ -24,11 +24,7 @@ include $(CLEAR_VARS)
24# so make sure we do not regret hard-coding it as follows: 24# so make sure we do not regret hard-coding it as follows:
25liblog_cflags := -DLIBLOG_LOG_TAG=1005 25liblog_cflags := -DLIBLOG_LOG_TAG=1005
26 26
27ifneq ($(TARGET_USES_LOGD),false)
28liblog_sources := logd_write.c 27liblog_sources := logd_write.c
29else
30liblog_sources := logd_write_kern.c
31endif
32 28
33# some files must not be compiled when building against Mingw 29# some files must not be compiled when building against Mingw
34# they correspond to features not used by our host development tools 30# they correspond to features not used by our host development tools
@@ -47,11 +43,7 @@ liblog_target_sources := $(liblog_sources) log_time.cpp log_is_loggable.c
47ifeq ($(strip $(USE_MINGW)),) 43ifeq ($(strip $(USE_MINGW)),)
48liblog_target_sources += logprint.c 44liblog_target_sources += logprint.c
49endif 45endif
50ifneq ($(TARGET_USES_LOGD),false)
51liblog_target_sources += log_read.c 46liblog_target_sources += log_read.c
52else
53liblog_target_sources += log_read_kern.c
54endif
55 47
56# Shared and static library for host 48# Shared and static library for host
57# ======================================================== 49# ========================================================
diff --git a/liblog/log_read_kern.c b/liblog/log_read_kern.c
deleted file mode 100644
index 69b405ca1..000000000
--- a/liblog/log_read_kern.c
+++ /dev/null
@@ -1,742 +0,0 @@
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#define _GNU_SOURCE /* asprintf for x86 host */
18#include <errno.h>
19#include <fcntl.h>
20#include <poll.h>
21#include <string.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <sys/cdefs.h>
25#include <sys/ioctl.h>
26
27#include <cutils/list.h>
28#include <log/log.h>
29#include <log/logger.h>
30
31#define __LOGGERIO 0xAE
32
33#define LOGGER_GET_LOG_BUF_SIZE _IO(__LOGGERIO, 1) /* size of log */
34#define LOGGER_GET_LOG_LEN _IO(__LOGGERIO, 2) /* used log len */
35#define LOGGER_GET_NEXT_ENTRY_LEN _IO(__LOGGERIO, 3) /* next entry len */
36#define LOGGER_FLUSH_LOG _IO(__LOGGERIO, 4) /* flush log */
37#define LOGGER_GET_VERSION _IO(__LOGGERIO, 5) /* abi version */
38#define LOGGER_SET_VERSION _IO(__LOGGERIO, 6) /* abi version */
39
40typedef char bool;
41#define false (const bool)0
42#define true (const bool)1
43
44#define LOG_FILE_DIR "/dev/log/"
45
46/* timeout in milliseconds */
47#define LOG_TIMEOUT_FLUSH 5
48#define LOG_TIMEOUT_NEVER -1
49
50#define logger_for_each(logger, logger_list) \
51 for (logger = node_to_item((logger_list)->node.next, struct logger, node); \
52 logger != node_to_item(&(logger_list)->node, struct logger, node); \
53 logger = node_to_item((logger)->node.next, struct logger, node))
54
55#ifndef __unused
56#define __unused __attribute__((unused))
57#endif
58
59/* In the future, we would like to make this list extensible */
60static const char *LOG_NAME[LOG_ID_MAX] = {
61 [LOG_ID_MAIN] = "main",
62 [LOG_ID_RADIO] = "radio",
63 [LOG_ID_EVENTS] = "events",
64 [LOG_ID_SYSTEM] = "system",
65 [LOG_ID_CRASH] = "crash",
66 [LOG_ID_KERNEL] = "kernel",
67};
68
69const char *android_log_id_to_name(log_id_t log_id)
70{
71 if (log_id >= LOG_ID_MAX) {
72 log_id = LOG_ID_MAIN;
73 }
74 return LOG_NAME[log_id];
75}
76
77static int accessmode(int mode)
78{
79 if ((mode & ANDROID_LOG_ACCMODE) == ANDROID_LOG_WRONLY) {
80 return W_OK;
81 }
82 if ((mode & ANDROID_LOG_ACCMODE) == ANDROID_LOG_RDWR) {
83 return R_OK | W_OK;
84 }
85 return R_OK;
86}
87
88/* repeated fragment */
89static int check_allocate_accessible(char **n, const char *b, int mode)
90{
91 *n = NULL;
92
93 if (!b) {
94 return -EINVAL;
95 }
96
97 asprintf(n, LOG_FILE_DIR "%s", b);
98 if (!*n) {
99 return -1;
100 }
101
102 return access(*n, accessmode(mode));
103}
104
105log_id_t android_name_to_log_id(const char *logName)
106{
107 const char *b;
108 char *n;
109 int ret;
110
111 if (!logName) {
112 return -1; /* NB: log_id_t is unsigned */
113 }
114 b = strrchr(logName, '/');
115 if (!b) {
116 b = logName;
117 } else {
118 ++b;
119 }
120
121 ret = check_allocate_accessible(&n, b, ANDROID_LOG_RDONLY);
122 free(n);
123 if (ret) {
124 return ret;
125 }
126
127 for(ret = LOG_ID_MIN; ret < LOG_ID_MAX; ++ret) {
128 const char *l = LOG_NAME[ret];
129 if (l && !strcmp(b, l)) {
130 return ret;
131 }
132 }
133 return -1; /* should never happen */
134}
135
136struct logger_list {
137 struct listnode node;
138 int mode;
139 unsigned int tail;
140 pid_t pid;
141 unsigned int queued_lines;
142 int timeout_ms;
143 int error;
144 bool flush;
145 bool valid_entry; /* valiant(?) effort to deal with memory starvation */
146 struct log_msg entry;
147};
148
149struct log_list {
150 struct listnode node;
151 struct log_msg entry; /* Truncated to event->len() + 1 to save space */
152};
153
154struct logger {
155 struct listnode node;
156 struct logger_list *top;
157 int fd;
158 log_id_t id;
159 short *revents;
160 struct listnode log_list;
161};
162
163/* android_logger_alloc unimplemented, no use case */
164/* android_logger_free not exported */
165static void android_logger_free(struct logger *logger)
166{
167 if (!logger) {
168 return;
169 }
170
171 while (!list_empty(&logger->log_list)) {
172 struct log_list *entry = node_to_item(
173 list_head(&logger->log_list), struct log_list, node);
174 list_remove(&entry->node);
175 free(entry);
176 if (logger->top->queued_lines) {
177 logger->top->queued_lines--;
178 }
179 }
180
181 if (logger->fd >= 0) {
182 close(logger->fd);
183 }
184
185 list_remove(&logger->node);
186
187 free(logger);
188}
189
190log_id_t android_logger_get_id(struct logger *logger)
191{
192 return logger->id;
193}
194
195/* worker for sending the command to the logger */
196static int logger_ioctl(struct logger *logger, int cmd, int mode)
197{
198 char *n;
199 int f, ret;
200
201 if (!logger || !logger->top) {
202 return -EFAULT;
203 }
204
205 if (((mode & ANDROID_LOG_ACCMODE) == ANDROID_LOG_RDWR)
206 || (((mode ^ logger->top->mode) & ANDROID_LOG_ACCMODE) == 0)) {
207 return ioctl(logger->fd, cmd);
208 }
209
210 /* We go here if android_logger_list_open got mode wrong for this ioctl */
211 ret = check_allocate_accessible(&n, android_log_id_to_name(logger->id), mode);
212 if (ret) {
213 free(n);
214 return ret;
215 }
216
217 f = open(n, mode);
218 free(n);
219 if (f < 0) {
220 return f;
221 }
222
223 ret = ioctl(f, cmd);
224 close (f);
225
226 return ret;
227}
228
229int android_logger_clear(struct logger *logger)
230{
231 return logger_ioctl(logger, LOGGER_FLUSH_LOG, ANDROID_LOG_WRONLY);
232}
233
234/* returns the total size of the log's ring buffer */
235long android_logger_get_log_size(struct logger *logger)
236{
237 return logger_ioctl(logger, LOGGER_GET_LOG_BUF_SIZE, ANDROID_LOG_RDWR);
238}
239
240int android_logger_set_log_size(struct logger *logger __unused,
241 unsigned long size __unused)
242{
243 return -ENOTSUP;
244}
245
246/*
247 * returns the readable size of the log's ring buffer (that is, amount of the
248 * log consumed)
249 */
250long android_logger_get_log_readable_size(struct logger *logger)
251{
252 return logger_ioctl(logger, LOGGER_GET_LOG_LEN, ANDROID_LOG_RDONLY);
253}
254
255/*
256 * returns the logger version
257 */
258int android_logger_get_log_version(struct logger *logger)
259{
260 int ret = logger_ioctl(logger, LOGGER_GET_VERSION, ANDROID_LOG_RDWR);
261 return (ret < 0) ? 1 : ret;
262}
263
264/*
265 * returns statistics
266 */
267static const char unsupported[] = "18\nNot Supported\n\f";
268
269ssize_t android_logger_get_statistics(struct logger_list *logger_list __unused,
270 char *buf, size_t len)
271{
272 strncpy(buf, unsupported, len);
273 return -ENOTSUP;
274}
275
276ssize_t android_logger_get_prune_list(struct logger_list *logger_list __unused,
277 char *buf, size_t len)
278{
279 strncpy(buf, unsupported, len);
280 return -ENOTSUP;
281}
282
283int android_logger_set_prune_list(struct logger_list *logger_list __unused,
284 char *buf, size_t len)
285{
286 static const char unsupported_error[] = "Unsupported";
287 strncpy(buf, unsupported, len);
288 return -ENOTSUP;
289}
290
291struct logger_list *android_logger_list_alloc(int mode,
292 unsigned int tail,
293 pid_t pid)
294{
295 struct logger_list *logger_list;
296
297 logger_list = calloc(1, sizeof(*logger_list));
298 if (!logger_list) {
299 return NULL;
300 }
301 list_init(&logger_list->node);
302 logger_list->mode = mode;
303 logger_list->tail = tail;
304 logger_list->pid = pid;
305 return logger_list;
306}
307
308struct logger_list *android_logger_list_alloc_time(int mode,
309 log_time start __unused,
310 pid_t pid)
311{
312 return android_logger_list_alloc(mode, 0, pid);
313}
314
315/* android_logger_list_register unimplemented, no use case */
316/* android_logger_list_unregister unimplemented, no use case */
317
318/* Open the named log and add it to the logger list */
319struct logger *android_logger_open(struct logger_list *logger_list,
320 log_id_t id)
321{
322 struct listnode *node;
323 struct logger *logger;
324 char *n;
325
326 if (!logger_list || (id >= LOG_ID_MAX)) {
327 goto err;
328 }
329
330 logger_for_each(logger, logger_list) {
331 if (logger->id == id) {
332 goto ok;
333 }
334 }
335
336 logger = calloc(1, sizeof(*logger));
337 if (!logger) {
338 goto err;
339 }
340
341 if (check_allocate_accessible(&n, android_log_id_to_name(id),
342 logger_list->mode)) {
343 goto err_name;
344 }
345
346 logger->fd = open(n, logger_list->mode & (ANDROID_LOG_ACCMODE | ANDROID_LOG_NONBLOCK));
347 if (logger->fd < 0) {
348 goto err_name;
349 }
350
351 free(n);
352 logger->id = id;
353 list_init(&logger->log_list);
354 list_add_tail(&logger_list->node, &logger->node);
355 logger->top = logger_list;
356 logger_list->timeout_ms = LOG_TIMEOUT_FLUSH;
357 goto ok;
358
359err_name:
360 free(n);
361err_logger:
362 free(logger);
363err:
364 logger = NULL;
365ok:
366 return logger;
367}
368
369/* Open the single named log and make it part of a new logger list */
370struct logger_list *android_logger_list_open(log_id_t id,
371 int mode,
372 unsigned int tail,
373 pid_t pid)
374{
375 struct logger_list *logger_list = android_logger_list_alloc(mode, tail, pid);
376 if (!logger_list) {
377 return NULL;
378 }
379
380 if (!android_logger_open(logger_list, id)) {
381 android_logger_list_free(logger_list);
382 return NULL;
383 }
384
385 return logger_list;
386}
387
388/* prevent memory starvation when backfilling */
389static unsigned int queue_threshold(struct logger_list *logger_list)
390{
391 return (logger_list->tail < 64) ? 64 : logger_list->tail;
392}
393
394static bool low_queue(struct listnode *node)
395{
396 /* low is considered less than 2 */
397 return list_head(node) == list_tail(node);
398}
399
400/* Flush queues in sequential order, one at a time */
401static int android_logger_list_flush(struct logger_list *logger_list,
402 struct log_msg *log_msg)
403{
404 int ret = 0;
405 struct log_list *firstentry = NULL;
406
407 while ((ret == 0)
408 && (logger_list->flush
409 || (logger_list->queued_lines > logger_list->tail))) {
410 struct logger *logger;
411
412 /* Merge sort */
413 bool at_least_one_is_low = false;
414 struct logger *firstlogger = NULL;
415 firstentry = NULL;
416
417 logger_for_each(logger, logger_list) {
418 struct listnode *node;
419 struct log_list *oldest = NULL;
420
421 /* kernel logger channels not necessarily time-sort order */
422 list_for_each(node, &logger->log_list) {
423 struct log_list *entry = node_to_item(node,
424 struct log_list, node);
425 if (!oldest
426 || (entry->entry.entry.sec < oldest->entry.entry.sec)
427 || ((entry->entry.entry.sec == oldest->entry.entry.sec)
428 && (entry->entry.entry.nsec < oldest->entry.entry.nsec))) {
429 oldest = entry;
430 }
431 }
432
433 if (!oldest) {
434 at_least_one_is_low = true;
435 continue;
436 } else if (low_queue(&logger->log_list)) {
437 at_least_one_is_low = true;
438 }
439
440 if (!firstentry
441 || (oldest->entry.entry.sec < firstentry->entry.entry.sec)
442 || ((oldest->entry.entry.sec == firstentry->entry.entry.sec)
443 && (oldest->entry.entry.nsec < firstentry->entry.entry.nsec))) {
444 firstentry = oldest;
445 firstlogger = logger;
446 }
447 }
448
449 if (!firstentry) {
450 break;
451 }
452
453 /* when trimming list, tries to keep one entry behind in each bucket */
454 if (!logger_list->flush
455 && at_least_one_is_low
456 && (logger_list->queued_lines < queue_threshold(logger_list))) {
457 break;
458 }
459
460 /* within tail?, send! */
461 if ((logger_list->tail == 0)
462 || (logger_list->queued_lines <= logger_list->tail)) {
463 int diff;
464 ret = firstentry->entry.entry.hdr_size;
465 if (!ret) {
466 ret = sizeof(firstentry->entry.entry_v1);
467 }
468
469 /* Promote entry to v3 format */
470 memcpy(log_msg->buf, firstentry->entry.buf, ret);
471 diff = sizeof(firstentry->entry.entry_v3) - ret;
472 if (diff < 0) {
473 diff = 0;
474 } else if (diff > 0) {
475 memset(log_msg->buf + ret, 0, diff);
476 }
477 memcpy(log_msg->buf + ret + diff, firstentry->entry.buf + ret,
478 firstentry->entry.entry.len + 1);
479 ret += diff;
480 log_msg->entry.hdr_size = ret;
481 log_msg->entry.lid = firstlogger->id;
482
483 ret += firstentry->entry.entry.len;
484 }
485
486 /* next entry */
487 list_remove(&firstentry->node);
488 free(firstentry);
489 if (logger_list->queued_lines) {
490 logger_list->queued_lines--;
491 }
492 }
493
494 /* Flushed the list, no longer in tail mode for continuing content */
495 if (logger_list->flush && !firstentry) {
496 logger_list->tail = 0;
497 }
498 return ret;
499}
500
501/* Read from the selected logs */
502int android_logger_list_read(struct logger_list *logger_list,
503 struct log_msg *log_msg)
504{
505 struct logger *logger;
506 nfds_t nfds;
507 struct pollfd *p, *pollfds = NULL;
508 int error = 0, ret = 0;
509
510 memset(log_msg, 0, sizeof(struct log_msg));
511
512 if (!logger_list) {
513 return -ENODEV;
514 }
515
516 if (!(accessmode(logger_list->mode) & R_OK)) {
517 logger_list->error = EPERM;
518 goto done;
519 }
520
521 nfds = 0;
522 logger_for_each(logger, logger_list) {
523 ++nfds;
524 }
525 if (nfds <= 0) {
526 error = ENODEV;
527 goto done;
528 }
529
530 /* Do we have anything to offer from the buffer or state? */
531 if (logger_list->valid_entry) { /* implies we are also in a flush state */
532 goto flush;
533 }
534
535 ret = android_logger_list_flush(logger_list, log_msg);
536 if (ret) {
537 goto done;
538 }
539
540 if (logger_list->error) { /* implies we are also in a flush state */
541 goto done;
542 }
543
544 /* Lets start grinding on metal */
545 pollfds = calloc(nfds, sizeof(struct pollfd));
546 if (!pollfds) {
547 error = ENOMEM;
548 goto flush;
549 }
550
551 p = pollfds;
552 logger_for_each(logger, logger_list) {
553 p->fd = logger->fd;
554 p->events = POLLIN;
555 logger->revents = &p->revents;
556 ++p;
557 }
558
559 while (!ret && !error) {
560 int result;
561
562 /* If we oversleep it's ok, i.e. ignore EINTR. */
563 result = TEMP_FAILURE_RETRY(
564 poll(pollfds, nfds, logger_list->timeout_ms));
565
566 if (result <= 0) {
567 if (result) {
568 error = errno;
569 } else if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
570 error = EAGAIN;
571 } else {
572 logger_list->timeout_ms = LOG_TIMEOUT_NEVER;
573 }
574
575 logger_list->flush = true;
576 goto try_flush;
577 }
578
579 logger_list->timeout_ms = LOG_TIMEOUT_FLUSH;
580
581 /* Anti starvation */
582 if (!logger_list->flush
583 && (logger_list->queued_lines > (queue_threshold(logger_list) / 2))) {
584 /* Any queues with input pending that is low? */
585 bool starving = false;
586 logger_for_each(logger, logger_list) {
587 if ((*(logger->revents) & POLLIN)
588 && low_queue(&logger->log_list)) {
589 starving = true;
590 break;
591 }
592 }
593
594 /* pushback on any queues that are not low */
595 if (starving) {
596 logger_for_each(logger, logger_list) {
597 if ((*(logger->revents) & POLLIN)
598 && !low_queue(&logger->log_list)) {
599 *(logger->revents) &= ~POLLIN;
600 }
601 }
602 }
603 }
604
605 logger_for_each(logger, logger_list) {
606 unsigned int hdr_size;
607 struct log_list *entry;
608 int diff;
609
610 if (!(*(logger->revents) & POLLIN)) {
611 continue;
612 }
613
614 memset(logger_list->entry.buf, 0, sizeof(struct log_msg));
615 /* NOTE: driver guarantees we read exactly one full entry */
616 result = read(logger->fd, logger_list->entry.buf,
617 LOGGER_ENTRY_MAX_LEN);
618 if (result <= 0) {
619 if (!result) {
620 error = EIO;
621 } else if (errno != EINTR) {
622 error = errno;
623 }
624 continue;
625 }
626
627 if (logger_list->pid
628 && (logger_list->pid != logger_list->entry.entry.pid)) {
629 continue;
630 }
631
632 hdr_size = logger_list->entry.entry.hdr_size;
633 if (!hdr_size) {
634 hdr_size = sizeof(logger_list->entry.entry_v1);
635 }
636
637 if ((hdr_size > sizeof(struct log_msg))
638 || (logger_list->entry.entry.len
639 > sizeof(logger_list->entry.buf) - hdr_size)
640 || (logger_list->entry.entry.len != result - hdr_size)) {
641 error = EINVAL;
642 continue;
643 }
644
645 /* Promote entry to v3 format */
646 diff = sizeof(logger_list->entry.entry_v3) - hdr_size;
647 if (diff > 0) {
648 if (logger_list->entry.entry.len
649 > sizeof(logger_list->entry.buf) - hdr_size - diff) {
650 error = EINVAL;
651 continue;
652 }
653 result += diff;
654 memmove(logger_list->entry.buf + hdr_size + diff,
655 logger_list->entry.buf + hdr_size,
656 logger_list->entry.entry.len + 1);
657 memset(logger_list->entry.buf + hdr_size, 0, diff);
658 logger_list->entry.entry.hdr_size = hdr_size + diff;
659 }
660 logger_list->entry.entry.lid = logger->id;
661
662 /* speedup: If not tail, and only one list, send directly */
663 if (!logger_list->tail
664 && (list_head(&logger_list->node)
665 == list_tail(&logger_list->node))) {
666 ret = result;
667 memcpy(log_msg->buf, logger_list->entry.buf, result + 1);
668 break;
669 }
670
671 entry = malloc(sizeof(*entry) - sizeof(entry->entry) + result + 1);
672
673 if (!entry) {
674 logger_list->valid_entry = true;
675 error = ENOMEM;
676 break;
677 }
678
679 logger_list->queued_lines++;
680
681 memcpy(entry->entry.buf, logger_list->entry.buf, result);
682 entry->entry.buf[result] = '\0';
683 list_add_tail(&logger->log_list, &entry->node);
684 }
685
686 if (ret <= 0) {
687try_flush:
688 ret = android_logger_list_flush(logger_list, log_msg);
689 }
690 }
691
692 free(pollfds);
693
694flush:
695 if (error) {
696 logger_list->flush = true;
697 }
698
699 if (ret <= 0) {
700 ret = android_logger_list_flush(logger_list, log_msg);
701
702 if (!ret && logger_list->valid_entry) {
703 ret = logger_list->entry.entry.hdr_size;
704 if (!ret) {
705 ret = sizeof(logger_list->entry.entry_v1);
706 }
707 ret += logger_list->entry.entry.len;
708
709 memcpy(log_msg->buf, logger_list->entry.buf,
710 sizeof(struct log_msg));
711 logger_list->valid_entry = false;
712 }
713 }
714
715done:
716 if (logger_list->error) {
717 error = logger_list->error;
718 }
719 if (error) {
720 logger_list->error = error;
721 if (!ret) {
722 ret = -error;
723 }
724 }
725 return ret;
726}
727
728/* Close all the logs */
729void android_logger_list_free(struct logger_list *logger_list)
730{
731 if (logger_list == NULL) {
732 return;
733 }
734
735 while (!list_empty(&logger_list->node)) {
736 struct listnode *node = list_head(&logger_list->node);
737 struct logger *logger = node_to_item(node, struct logger, node);
738 android_logger_free(logger);
739 }
740
741 free(logger_list);
742}
diff --git a/liblog/logd_write_kern.c b/liblog/logd_write_kern.c
deleted file mode 100644
index 8742b34ef..000000000
--- a/liblog/logd_write_kern.c
+++ /dev/null
@@ -1,295 +0,0 @@
1/*
2 * Copyright (C) 2007-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 <errno.h>
18#include <fcntl.h>
19#include <pthread.h>
20#include <stdarg.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/stat.h>
25#include <sys/types.h>
26#include <time.h>
27#include <unistd.h>
28
29#include <android/set_abort_message.h>
30
31#include <log/log.h>
32#include <log/logd.h>
33#include <log/logger.h>
34
35#define LOGGER_LOG_MAIN "log/main"
36#define LOGGER_LOG_RADIO "log/radio"
37#define LOGGER_LOG_EVENTS "log/events"
38#define LOGGER_LOG_SYSTEM "log/system"
39
40#define LOG_BUF_SIZE 1024
41
42#define log_open(pathname, flags) open(pathname, (flags) | O_CLOEXEC)
43#define log_writev(filedes, vector, count) writev(filedes, vector, count)
44#define log_close(filedes) close(filedes)
45
46static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
47static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;
48
49static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
50
51#ifndef __unused
52#define __unused __attribute__((__unused__))
53#endif
54
55static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 };
56
57/*
58 * This is used by the C++ code to decide if it should write logs through
59 * the C code. Basically, if /dev/log/... is available, we're running in
60 * the simulator rather than a desktop tool and want to use the device.
61 */
62static enum {
63 kLogUninitialized, kLogNotAvailable, kLogAvailable
64} g_log_status = kLogUninitialized;
65int __android_log_dev_available(void)
66{
67 if (g_log_status == kLogUninitialized) {
68 if (access("/dev/"LOGGER_LOG_MAIN, W_OK) == 0)
69 g_log_status = kLogAvailable;
70 else
71 g_log_status = kLogNotAvailable;
72 }
73
74 return (g_log_status == kLogAvailable);
75}
76
77static int __write_to_log_null(log_id_t log_fd __unused, struct iovec *vec __unused,
78 size_t nr __unused)
79{
80 return -1;
81}
82
83static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
84{
85 ssize_t ret;
86 int log_fd;
87
88 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
89 if (log_id == LOG_ID_CRASH) {
90 log_id = LOG_ID_MAIN;
91 }
92 log_fd = log_fds[(int)log_id];
93 } else {
94 return -EBADF;
95 }
96
97 do {
98 ret = log_writev(log_fd, vec, nr);
99 if (ret < 0) {
100 ret = -errno;
101 }
102 } while (ret == -EINTR);
103
104 return ret;
105}
106
107static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
108{
109 pthread_mutex_lock(&log_init_lock);
110
111 if (write_to_log == __write_to_log_init) {
112 log_fds[LOG_ID_MAIN] = log_open("/dev/"LOGGER_LOG_MAIN, O_WRONLY);
113 log_fds[LOG_ID_RADIO] = log_open("/dev/"LOGGER_LOG_RADIO, O_WRONLY);
114 log_fds[LOG_ID_EVENTS] = log_open("/dev/"LOGGER_LOG_EVENTS, O_WRONLY);
115 log_fds[LOG_ID_SYSTEM] = log_open("/dev/"LOGGER_LOG_SYSTEM, O_WRONLY);
116
117 write_to_log = __write_to_log_kernel;
118
119 if (log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 ||
120 log_fds[LOG_ID_EVENTS] < 0) {
121 log_close(log_fds[LOG_ID_MAIN]);
122 log_close(log_fds[LOG_ID_RADIO]);
123 log_close(log_fds[LOG_ID_EVENTS]);
124 log_fds[LOG_ID_MAIN] = -1;
125 log_fds[LOG_ID_RADIO] = -1;
126 log_fds[LOG_ID_EVENTS] = -1;
127 write_to_log = __write_to_log_null;
128 }
129
130 if (log_fds[LOG_ID_SYSTEM] < 0) {
131 log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN];
132 }
133 }
134
135 pthread_mutex_unlock(&log_init_lock);
136
137 return write_to_log(log_id, vec, nr);
138}
139
140int __android_log_write(int prio, const char *tag, const char *msg)
141{
142 return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
143}
144
145int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
146{
147 struct iovec vec[3];
148 char tmp_tag[32];
149
150 if (!tag)
151 tag = "";
152
153 /* XXX: This needs to go! */
154 if ((bufID != LOG_ID_RADIO) &&
155 (!strcmp(tag, "HTC_RIL") ||
156 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
157 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
158 !strcmp(tag, "AT") ||
159 !strcmp(tag, "GSM") ||
160 !strcmp(tag, "STK") ||
161 !strcmp(tag, "CDMA") ||
162 !strcmp(tag, "PHONE") ||
163 !strcmp(tag, "SMS"))) {
164 bufID = LOG_ID_RADIO;
165 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
166 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
167 tag = tmp_tag;
168 }
169
170 if (prio == ANDROID_LOG_FATAL) {
171 android_set_abort_message(msg);
172 }
173
174 vec[0].iov_base = (unsigned char *) &prio;
175 vec[0].iov_len = 1;
176 vec[1].iov_base = (void *) tag;
177 vec[1].iov_len = strlen(tag) + 1;
178 vec[2].iov_base = (void *) msg;
179 vec[2].iov_len = strlen(msg) + 1;
180
181 return write_to_log(bufID, vec, 3);
182}
183
184int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
185{
186 char buf[LOG_BUF_SIZE];
187
188 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
189
190 return __android_log_write(prio, tag, buf);
191}
192
193int __android_log_print(int prio, const char *tag, const char *fmt, ...)
194{
195 va_list ap;
196 char buf[LOG_BUF_SIZE];
197
198 va_start(ap, fmt);
199 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
200 va_end(ap);
201
202 return __android_log_write(prio, tag, buf);
203}
204
205int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
206{
207 va_list ap;
208 char buf[LOG_BUF_SIZE];
209
210 va_start(ap, fmt);
211 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
212 va_end(ap);
213
214 return __android_log_buf_write(bufID, prio, tag, buf);
215}
216
217void __android_log_assert(const char *cond, const char *tag,
218 const char *fmt, ...)
219{
220 char buf[LOG_BUF_SIZE];
221
222 if (fmt) {
223 va_list ap;
224 va_start(ap, fmt);
225 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
226 va_end(ap);
227 } else {
228 /* Msg not provided, log condition. N.B. Do not use cond directly as
229 * format string as it could contain spurious '%' syntax (e.g.
230 * "%d" in "blocks%devs == 0").
231 */
232 if (cond)
233 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
234 else
235 strcpy(buf, "Unspecified assertion failed");
236 }
237
238 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
239 abort(); /* abort so we have a chance to debug the situation */
240 /* NOTREACHED */
241}
242
243int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
244{
245 struct iovec vec[2];
246
247 vec[0].iov_base = &tag;
248 vec[0].iov_len = sizeof(tag);
249 vec[1].iov_base = (void*)payload;
250 vec[1].iov_len = len;
251
252 return write_to_log(LOG_ID_EVENTS, vec, 2);
253}
254
255/*
256 * Like __android_log_bwrite, but takes the type as well. Doesn't work
257 * for the general case where we're generating lists of stuff, but very
258 * handy if we just want to dump an integer into the log.
259 */
260int __android_log_btwrite(int32_t tag, char type, const void *payload,
261 size_t len)
262{
263 struct iovec vec[3];
264
265 vec[0].iov_base = &tag;
266 vec[0].iov_len = sizeof(tag);
267 vec[1].iov_base = &type;
268 vec[1].iov_len = sizeof(type);
269 vec[2].iov_base = (void*)payload;
270 vec[2].iov_len = len;
271
272 return write_to_log(LOG_ID_EVENTS, vec, 3);
273}
274
275/*
276 * Like __android_log_bwrite, but used for writing strings to the
277 * event log.
278 */
279int __android_log_bswrite(int32_t tag, const char *payload)
280{
281 struct iovec vec[4];
282 char type = EVENT_TYPE_STRING;
283 uint32_t len = strlen(payload);
284
285 vec[0].iov_base = &tag;
286 vec[0].iov_len = sizeof(tag);
287 vec[1].iov_base = &type;
288 vec[1].iov_len = sizeof(type);
289 vec[2].iov_base = &len;
290 vec[2].iov_len = sizeof(len);
291 vec[3].iov_base = (void*)payload;
292 vec[3].iov_len = len;
293
294 return write_to_log(LOG_ID_EVENTS, vec, 4);
295}
diff --git a/liblog/tests/Android.mk b/liblog/tests/Android.mk
index a407c50a5..8229859a2 100644
--- a/liblog/tests/Android.mk
+++ b/liblog/tests/Android.mk
@@ -65,10 +65,6 @@ ifneq ($(wildcard $(LOCAL_PATH)/../../../../bionic/libc/bionic/libc_logging.cpp)
65test_src_files += \ 65test_src_files += \
66 libc_test.cpp 66 libc_test.cpp
67 67
68ifneq ($(TARGET_USES_LOGD),false)
69test_c_flags += -DTARGET_USES_LOGD
70endif
71
72endif 68endif
73 69
74# Build tests for the device (with .so). Run with: 70# Build tests for the device (with .so). Run with:
diff --git a/liblog/tests/libc_test.cpp b/liblog/tests/libc_test.cpp
index 0e84f4eb1..9dd6f0340 100644
--- a/liblog/tests/libc_test.cpp
+++ b/liblog/tests/libc_test.cpp
@@ -25,9 +25,7 @@
25#define _ANDROID_LOG_H // Priorities redefined 25#define _ANDROID_LOG_H // Priorities redefined
26#define _LIBS_LOG_LOG_H // log ids redefined 26#define _LIBS_LOG_LOG_H // log ids redefined
27typedef unsigned char log_id_t; // log_id_t missing as a result 27typedef unsigned char log_id_t; // log_id_t missing as a result
28#ifdef TARGET_USES_LOGD
29#define _LIBS_LOG_LOG_READ_H // log_time redefined 28#define _LIBS_LOG_LOG_READ_H // log_time redefined
30#endif
31 29
32#include <log/log.h> 30#include <log/log.h>
33#include <log/logger.h> 31#include <log/logger.h>