summaryrefslogtreecommitdiffstats
blob: f15c7cd6235501a5c8b02f420a3844c90e60d25a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
** Copyright 2013-2014, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <sched.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <cutils/list.h>
#include <log/log.h>
#include <log/logger.h>

#include "config_read.h"
#include "log_portability.h"
#include "logger.h"

/* android_logger_alloc unimplemented, no use case */
/* android_logger_free not exported */
static void android_logger_free(struct logger *logger)
{
    struct android_log_logger *logger_internal =
            (struct android_log_logger *)logger;

    if (!logger_internal) {
        return;
    }

    list_remove(&logger_internal->node);

    free(logger_internal);
}

/* android_logger_alloc unimplemented, no use case */

/* method for getting the associated sublog id */
LIBLOG_ABI_PUBLIC log_id_t android_logger_get_id(struct logger *logger)
{
    return ((struct android_log_logger *)logger)->logId;
}

static int init_transport_context(struct android_log_logger_list *logger_list)
{
    struct android_log_transport_read *transport;
    struct listnode *node;

    if (!logger_list) {
        return -EINVAL;
    }

    if (list_empty(&logger_list->logger)) {
        return -EINVAL;
    }

    if (!list_empty(&logger_list->transport)) {
        return 0;
    }

    __android_log_lock();
    /* mini __write_to_log_initialize() to populate transports */
    if (list_empty(&__android_log_transport_read) &&
            list_empty(&__android_log_persist_read)) {
        __android_log_config_read();
    }
    __android_log_unlock();

    node = (logger_list->mode & ANDROID_LOG_PSTORE) ?
            &__android_log_persist_read : &__android_log_transport_read;

    read_transport_for_each(transport, node) {
        struct android_log_transport_context *transp;
        struct android_log_logger *logger;
        unsigned logMask = 0;

        logger_for_each(logger, logger_list) {
            log_id_t logId = logger->logId;

            if (transport->read &&
                    (!transport->available ||
                        (transport->available(logId) >= 0))) {
                logMask |= 1 << logId;
            }
        }
        if (!logMask) {
            continue;
        }
        transp = calloc(1, sizeof(*transp));
        if (!transp) {
            return -ENOMEM;
        }
        transp->parent = logger_list;
        transp->transport = transport;
        transp->logMask = logMask;
        transp->ret = 1;
        list_add_tail(&logger_list->transport, &transp->node);
    }
    if (list_empty(&logger_list->transport)) {
        return -ENODEV;
    }
    return 0;
}

#define LOGGER_FUNCTION(logger, def, func, args...)                           \
    ssize_t ret = -EINVAL;                                                    \
    struct android_log_transport_context *transp;                             \
    struct android_log_logger *logger_internal =                              \
            (struct android_log_logger *)logger;                              \
                                                                              \
    if (!logger_internal) {                                                   \
        return ret;                                                           \
    }                                                                         \
    ret = init_transport_context(logger_internal->parent);                    \
    if (ret < 0) {                                                            \
        return ret;                                                           \
    }                                                                         \
                                                                              \
    ret = (def);                                                              \
    transport_context_for_each(transp, logger_internal->parent) {             \
        if ((transp->logMask & (1 << logger_internal->logId)) &&              \
                transp->transport && transp->transport->func) {               \
            ssize_t retval = (transp->transport->func)(logger_internal,       \
                                                       transp, ## args);      \
            if ((ret >= 0) || (ret == (def))) {                               \
                ret = retval;                                                 \
            }                                                                 \
        }                                                                     \
    }                                                                         \
    return ret

LIBLOG_ABI_PUBLIC int android_logger_clear(struct logger *logger)
{
    LOGGER_FUNCTION(logger, -ENODEV, clear);
}

/* returns the total size of the log's ring buffer */
LIBLOG_ABI_PUBLIC long android_logger_get_log_size(struct logger *logger)
{
    LOGGER_FUNCTION(logger, -ENODEV, getSize);
}

LIBLOG_ABI_PUBLIC int android_logger_set_log_size(struct logger *logger,
                                                  unsigned long size)
{
    LOGGER_FUNCTION(logger, -ENODEV, setSize, size);
}

/*
 * returns the readable size of the log's ring buffer (that is, amount of the
 * log consumed)
 */
LIBLOG_ABI_PUBLIC long android_logger_get_log_readable_size(
        struct logger *logger)
{
    LOGGER_FUNCTION(logger, -ENODEV, getReadableSize);
}

/*
 * returns the logger version
 */
LIBLOG_ABI_PUBLIC int android_logger_get_log_version(struct logger *logger)
{
    LOGGER_FUNCTION(logger, 4, version);
}

#define LOGGER_LIST_FUNCTION(logger_list, def, func, args...)                 \
    struct android_log_transport_context *transp;                             \
    struct android_log_logger_list *logger_list_internal =                    \
            (struct android_log_logger_list *)logger_list;                    \
                                                                              \
    ssize_t ret = init_transport_context(logger_list_internal);               \
    if (ret < 0) {                                                            \
        return ret;                                                           \
    }                                                                         \
                                                                              \
    ret = (def);                                                              \
    transport_context_for_each(transp, logger_list_internal) {                \
        if (transp->transport && (transp->transport->func)) {                 \
            ssize_t retval = (transp->transport->func)(logger_list_internal,  \
                                                       transp, ## args);      \
            if ((ret >= 0) || (ret == (def))) {                               \
                ret = retval;                                                 \
            }                                                                 \
        }                                                                     \
    }                                                                         \
    return ret

/*
 * returns statistics
 */
LIBLOG_ABI_PUBLIC ssize_t android_logger_get_statistics(
        struct logger_list *logger_list,
        char *buf, size_t len)
{
    LOGGER_LIST_FUNCTION(logger_list, -ENODEV, getStats, buf, len);
}

LIBLOG_ABI_PUBLIC ssize_t android_logger_get_prune_list(
        struct logger_list *logger_list,
        char *buf, size_t len)
{
    LOGGER_LIST_FUNCTION(logger_list, -ENODEV, getPrune, buf, len);
}

LIBLOG_ABI_PUBLIC int android_logger_set_prune_list(
        struct logger_list *logger_list,
        char *buf, size_t len)
{
    LOGGER_LIST_FUNCTION(logger_list, -ENODEV, setPrune, buf, len);
}

LIBLOG_ABI_PUBLIC struct logger_list *android_logger_list_alloc(
        int mode,
        unsigned int tail,
        pid_t pid)
{
    struct android_log_logger_list *logger_list;

    logger_list = calloc(1, sizeof(*logger_list));
    if (!logger_list) {
        return NULL;
    }

    list_init(&logger_list->logger);
    list_init(&logger_list->transport);
    logger_list->mode = mode;
    logger_list->tail = tail;
    logger_list->pid = pid;

    return (struct logger_list *)logger_list;
}

LIBLOG_ABI_PUBLIC struct logger_list *android_logger_list_alloc_time(
        int mode,
        log_time start,
        pid_t pid)
{
    struct android_log_logger_list *logger_list;

    logger_list = calloc(1, sizeof(*logger_list));
    if (!logger_list) {
        return NULL;
    }

    list_init(&logger_list->logger);
    list_init(&logger_list->transport);
    logger_list->mode = mode;
    logger_list->start = start;
    logger_list->pid = pid;

    return (struct logger_list *)logger_list;
}

/* android_logger_list_register unimplemented, no use case */
/* android_logger_list_unregister unimplemented, no use case */

/* Open the named log and add it to the logger list */
LIBLOG_ABI_PUBLIC struct logger *android_logger_open(
        struct logger_list *logger_list,
        log_id_t logId)
{
    struct android_log_logger_list *logger_list_internal =
            (struct android_log_logger_list *)logger_list;
    struct android_log_logger *logger;

    if (!logger_list_internal || (logId >= LOG_ID_MAX)) {
        goto err;
    }

    logger_for_each(logger, logger_list_internal) {
        if (logger->logId == logId) {
            goto ok;
        }
    }

    logger = calloc(1, sizeof(*logger));
    if (!logger) {
        goto err;
    }

    logger->logId = logId;
    list_add_tail(&logger_list_internal->logger, &logger->node);
    logger->parent = logger_list_internal;

    /* Reset known transports to re-evaluate, we just added one */
    while (!list_empty(&logger_list_internal->transport)) {
        struct listnode *node = list_head(&logger_list_internal->transport);
        struct android_log_transport_context *transp =
                node_to_item(node, struct android_log_transport_context, node);

        list_remove(&transp->node);
        free(transp);
    }
    goto ok;

err:
    logger = NULL;
ok:
    return (struct logger *)logger;
}

/* Open the single named log and make it part of a new logger list */
LIBLOG_ABI_PUBLIC struct logger_list *android_logger_list_open(
        log_id_t logId,
        int mode,
        unsigned int tail,
        pid_t pid)
{
    struct logger_list *logger_list =
            android_logger_list_alloc(mode, tail, pid);

    if (!logger_list) {
        return NULL;
    }

    if (!android_logger_open(logger_list, logId)) {
        android_logger_list_free(logger_list);
        return NULL;
    }

    return logger_list;
}

/* Read from the selected logs */
LIBLOG_ABI_PUBLIC int android_logger_list_read(struct logger_list *logger_list,
                                               struct log_msg *log_msg)
{
    struct android_log_transport_context *transp;
    struct android_log_logger_list *logger_list_internal =
            (struct android_log_logger_list *)logger_list;

    int ret = init_transport_context(logger_list_internal);
    if (ret < 0) {
        return ret;
    }

    /* at least one transport */
    transp = node_to_item(logger_list_internal->transport.next,
                          struct android_log_transport_context, node);

    /* more than one transport? */
    if (transp->node.next != &logger_list_internal->transport) {
        /* Poll and merge sort the entries if from multiple transports */
        struct android_log_transport_context *oldest = NULL;
        int ret;
        int polled = 0;
        do {
            if (polled) {
                sched_yield();
            }
            ret = -1000;
            polled = 0;
            do {
                int retval = transp->ret;
                if ((retval > 0) && !transp->logMsg.entry.len) {
                    if (!transp->transport->read) {
                        retval = transp->ret = 0;
                    } else if ((logger_list_internal->mode &
                                ANDROID_LOG_NONBLOCK) ||
                            !transp->transport->poll) {
                        retval = transp->ret = (*transp->transport->read)(
                                logger_list_internal,
                                transp,
                                &transp->logMsg);
                    } else {
                        int pollval = (*transp->transport->poll)(
                                logger_list_internal, transp);
                        if (pollval <= 0) {
                            sched_yield();
                            pollval = (*transp->transport->poll)(
                                    logger_list_internal, transp);
                        }
                        polled = 1;
                        if (pollval < 0) {
                            if ((pollval == -EINTR) || (pollval == -EAGAIN)) {
                                return -EAGAIN;
                            }
                            retval = transp->ret = pollval;
                        } else if (pollval > 0) {
                            retval = transp->ret = (*transp->transport->read)(
                                    logger_list_internal,
                                    transp,
                                    &transp->logMsg);
                        }
                    }
                }
                if (ret < retval) {
                    ret = retval;
                }
                if ((transp->ret > 0) && transp->logMsg.entry.len &&
                        (!oldest ||
                            (oldest->logMsg.entry.sec >
                                transp->logMsg.entry.sec) ||
                            ((oldest->logMsg.entry.sec ==
                                    transp->logMsg.entry.sec) &&
                                (oldest->logMsg.entry.nsec >
                                    transp->logMsg.entry.nsec)))) {
                    oldest = transp;
                }
                transp = node_to_item(transp->node.next,
                                      struct android_log_transport_context,
                                      node);
            } while (transp != node_to_item(
                    &logger_list_internal->transport,
                    struct android_log_transport_context,
                    node));
            if (!oldest &&
                    (logger_list_internal->mode & ANDROID_LOG_NONBLOCK)) {
                return (ret < 0) ? ret : -EAGAIN;
            }
            transp = node_to_item(logger_list_internal->transport.next,
                                  struct android_log_transport_context, node);
        } while (!oldest && (ret > 0));
        if (!oldest) {
            return ret;
        }
        memcpy(log_msg, &oldest->logMsg, oldest->logMsg.entry.len +
                    (oldest->logMsg.entry.hdr_size ?
                        oldest->logMsg.entry.hdr_size :
                        sizeof(struct logger_entry)));
        oldest->logMsg.entry.len = 0; /* Mark it as copied */
        return oldest->ret;
    }

    /* if only one, no need to copy into transport_context and merge-sort */
    return (transp->transport->read)(logger_list_internal, transp, log_msg);
}

/* Close all the logs */
LIBLOG_ABI_PUBLIC void android_logger_list_free(struct logger_list *logger_list)
{
    struct android_log_logger_list *logger_list_internal =
            (struct android_log_logger_list *)logger_list;

    if (logger_list_internal == NULL) {
        return;
    }

    while (!list_empty(&logger_list_internal->transport)) {
        struct listnode *node = list_head(&logger_list_internal->transport);
        struct android_log_transport_context *transp =
                node_to_item(node, struct android_log_transport_context, node);

        if (transp->transport && transp->transport->close) {
            (*transp->transport->close)(logger_list_internal, transp);
        }
        list_remove(&transp->node);
        free(transp);
    }

    while (!list_empty(&logger_list_internal->logger)) {
        struct listnode *node = list_head(&logger_list_internal->logger);
        struct android_log_logger *logger =
                node_to_item(node, struct android_log_logger, node);
        android_logger_free((struct logger *)logger);
    }

    free(logger_list_internal);
}