summaryrefslogtreecommitdiffstats
blob: 4b839443d761771d14e8770f7d750015784682cf (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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
/*
** 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 <inttypes.h>
#include <poll.h>
#include <signal.h>
#include <stdbool.h>
#include <stddef.h>
#define NOMINMAX /* for windows to suppress definition of min in stdlib.h */
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <cutils/list.h>
#include <cutils/sockets.h>
#include <log/log.h>
#include <log/logger.h>
#include <private/android_filesystem_config.h>
#include <private/android_logger.h>

#include "log_cdefs.h"

/* branchless on many architectures. */
#define min(x,y) ((y) ^ (((x) ^ (y)) & -((x) < (y))))

/* Private copy of ../libcutils/socket_local_client.c prevent library loops */

#if defined(_WIN32)

LIBLOG_WEAK int socket_local_client(const char *name, int namespaceId, int type)
{
    errno = ENOSYS;
    return -ENOSYS;
}

#else /* !_WIN32 */

#include <sys/socket.h>
#include <sys/un.h>
#include <sys/select.h>
#include <sys/types.h>

/* Private copy of ../libcutils/socket_local.h prevent library loops */
#define FILESYSTEM_SOCKET_PREFIX "/tmp/"
#define ANDROID_RESERVED_SOCKET_PREFIX "/dev/socket/"
/* End of ../libcutils/socket_local.h */

#define LISTEN_BACKLOG 4

/* Documented in header file. */
LIBLOG_WEAK int socket_make_sockaddr_un(const char *name, int namespaceId,
                                        struct sockaddr_un *p_addr,
                                        socklen_t *alen)
{
    memset (p_addr, 0, sizeof (*p_addr));
    size_t namelen;

    switch (namespaceId) {
    case ANDROID_SOCKET_NAMESPACE_ABSTRACT:
#if defined(__linux__)
        namelen  = strlen(name);

        /* Test with length +1 for the *initial* '\0'. */
        if ((namelen + 1) > sizeof(p_addr->sun_path)) {
            goto error;
        }

        /*
         * Note: The path in this case is *not* supposed to be
         * '\0'-terminated. ("man 7 unix" for the gory details.)
         */

        p_addr->sun_path[0] = 0;
        memcpy(p_addr->sun_path + 1, name, namelen);
#else
        /* this OS doesn't have the Linux abstract namespace */

        namelen = strlen(name) + strlen(FILESYSTEM_SOCKET_PREFIX);
        /* unix_path_max appears to be missing on linux */
        if (namelen > sizeof(*p_addr)
                - offsetof(struct sockaddr_un, sun_path) - 1) {
            goto error;
        }

        strcpy(p_addr->sun_path, FILESYSTEM_SOCKET_PREFIX);
        strcat(p_addr->sun_path, name);
#endif
        break;

    case ANDROID_SOCKET_NAMESPACE_RESERVED:
        namelen = strlen(name) + strlen(ANDROID_RESERVED_SOCKET_PREFIX);
        /* unix_path_max appears to be missing on linux */
        if (namelen > sizeof(*p_addr)
                - offsetof(struct sockaddr_un, sun_path) - 1) {
            goto error;
        }

        strcpy(p_addr->sun_path, ANDROID_RESERVED_SOCKET_PREFIX);
        strcat(p_addr->sun_path, name);
        break;

    case ANDROID_SOCKET_NAMESPACE_FILESYSTEM:
        namelen = strlen(name);
        /* unix_path_max appears to be missing on linux */
        if (namelen > sizeof(*p_addr)
                - offsetof(struct sockaddr_un, sun_path) - 1) {
            goto error;
        }

        strcpy(p_addr->sun_path, name);
        break;

    default:
        /* invalid namespace id */
        return -1;
    }

    p_addr->sun_family = AF_LOCAL;
    *alen = namelen + offsetof(struct sockaddr_un, sun_path) + 1;
    return 0;
error:
    return -1;
}

/**
 * connect to peer named "name" on fd
 * returns same fd or -1 on error.
 * fd is not closed on error. that's your job.
 *
 * Used by AndroidSocketImpl
 */
LIBLOG_WEAK int socket_local_client_connect(int fd, const char *name,
                                            int namespaceId, int type __unused)
{
    struct sockaddr_un addr;
    socklen_t alen;
    int err;

    err = socket_make_sockaddr_un(name, namespaceId, &addr, &alen);

    if (err < 0) {
        goto error;
    }

    if(connect(fd, (struct sockaddr *) &addr, alen) < 0) {
        goto error;
    }

    return fd;

error:
    return -1;
}

/**
 * connect to peer named "name"
 * returns fd or -1 on error
 */
LIBLOG_WEAK int socket_local_client(const char *name, int namespaceId, int type)
{
    int s;

    s = socket(AF_LOCAL, type, 0);
    if(s < 0) return -1;

    if ( 0 > socket_local_client_connect(s, name, namespaceId, type)) {
        close(s);
        return -1;
    }

    return s;
}

#endif /* !_WIN32 */
/* End of ../libcutils/socket_local_client.c */

#define logger_for_each(logger, logger_list) \
    for (logger = node_to_item((logger_list)->node.next, struct logger, node); \
         logger != node_to_item(&(logger_list)->node, struct logger, node); \
         logger = node_to_item((logger)->node.next, struct logger, node))

/* In the future, we would like to make this list extensible */
static const char *LOG_NAME[LOG_ID_MAX] = {
    [LOG_ID_MAIN] = "main",
    [LOG_ID_RADIO] = "radio",
    [LOG_ID_EVENTS] = "events",
    [LOG_ID_SYSTEM] = "system",
    [LOG_ID_CRASH] = "crash",
    [LOG_ID_SECURITY] = "security",
    [LOG_ID_KERNEL] = "kernel",
};

LIBLOG_ABI_PUBLIC const char *android_log_id_to_name(log_id_t log_id)
{
    if (log_id >= LOG_ID_MAX) {
        log_id = LOG_ID_MAIN;
    }
    return LOG_NAME[log_id];
}

LIBLOG_ABI_PUBLIC log_id_t android_name_to_log_id(const char *logName)
{
    const char *b;
    int ret;

    if (!logName) {
        return -1; /* NB: log_id_t is unsigned */
    }
    b = strrchr(logName, '/');
    if (!b) {
        b = logName;
    } else {
        ++b;
    }

    for(ret = LOG_ID_MIN; ret < LOG_ID_MAX; ++ret) {
        const char *l = LOG_NAME[ret];
        if (l && !strcmp(b, l)) {
            return ret;
        }
    }
    return -1;   /* should never happen */
}

struct logger_list {
    struct listnode node;
    int mode;
    unsigned int tail;
    log_time start;
    pid_t pid;
    int sock;
};

struct logger {
    struct listnode node;
    struct logger_list *top;
    log_id_t id;
};

/* android_logger_alloc unimplemented, no use case */
/* android_logger_free not exported */
static void android_logger_free(struct logger *logger)
{
    if (!logger) {
        return;
    }

    list_remove(&logger->node);

    free(logger);
}

/* 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 logger->id;
}

/* worker for sending the command to the logger */
static ssize_t send_log_msg(struct logger *logger,
                            const char *msg, char *buf, size_t buf_size)
{
    ssize_t ret;
    size_t len;
    char *cp;
    int errno_save = 0;
    int sock = socket_local_client("logd", ANDROID_SOCKET_NAMESPACE_RESERVED,
                                   SOCK_STREAM);
    if (sock < 0) {
        return sock;
    }

    if (msg) {
        snprintf(buf, buf_size, msg, logger ? logger->id : (unsigned) -1);
    }

    len = strlen(buf) + 1;
    ret = TEMP_FAILURE_RETRY(write(sock, buf, len));
    if (ret <= 0) {
        goto done;
    }

    len = buf_size;
    cp = buf;
    while ((ret = TEMP_FAILURE_RETRY(read(sock, cp, len))) > 0) {
        struct pollfd p;

        if (((size_t)ret == len) || (buf_size < PAGE_SIZE)) {
            break;
        }

        len -= ret;
        cp += ret;

        memset(&p, 0, sizeof(p));
        p.fd = sock;
        p.events = POLLIN;

        /* Give other side 20ms to refill pipe */
        ret = TEMP_FAILURE_RETRY(poll(&p, 1, 20));

        if (ret <= 0) {
            break;
        }

        if (!(p.revents & POLLIN)) {
            ret = 0;
            break;
        }
    }

    if (ret >= 0) {
        ret += buf_size - len;
    }

done:
    if ((ret == -1) && errno) {
        errno_save = errno;
    }
    close(sock);
    if (errno_save) {
        errno = errno_save;
    }
    return ret;
}

static int check_log_success(char *buf, ssize_t ret)
{
    if (ret < 0) {
        return ret;
    }

    if (strncmp(buf, "success", 7)) {
        errno = EINVAL;
        return -1;
    }

    return 0;
}

/* Determine the credentials of the caller */
static bool uid_has_log_permission(uid_t uid)
{
    return (uid == AID_SYSTEM) || (uid == AID_LOG) || (uid == AID_ROOT);
}

static uid_t get_best_effective_uid()
{
    uid_t euid;
    uid_t uid;
    gid_t gid;
    ssize_t i;
    static uid_t last_uid = (uid_t) -1;

    if (last_uid != (uid_t) -1) {
        return last_uid;
    }
    uid = getuid();
    if (uid_has_log_permission(uid)) {
        return last_uid = uid;
    }
    euid = geteuid();
    if (uid_has_log_permission(euid)) {
        return last_uid = euid;
    }
    gid = getgid();
    if (uid_has_log_permission(gid)) {
        return last_uid = gid;
    }
    gid = getegid();
    if (uid_has_log_permission(gid)) {
        return last_uid = gid;
    }
    i = getgroups((size_t) 0, NULL);
    if (i > 0) {
        gid_t list[i];

        getgroups(i, list);
        while (--i >= 0) {
            if (uid_has_log_permission(list[i])) {
                return last_uid = list[i];
            }
        }
    }
    return last_uid = uid;
}

LIBLOG_ABI_PUBLIC int android_logger_clear(struct logger *logger)
{
    char buf[512];

    if (logger->top->mode & ANDROID_LOG_PSTORE) {
        if (uid_has_log_permission(get_best_effective_uid())) {
            return unlink("/sys/fs/pstore/pmsg-ramoops-0");
        }
        errno = EPERM;
        return -1;
    }
    return check_log_success(buf,
        send_log_msg(logger, "clear %d", buf, sizeof(buf)));
}

/* returns the total size of the log's ring buffer */
LIBLOG_ABI_PUBLIC long android_logger_get_log_size(struct logger *logger)
{
    char buf[512];

    ssize_t ret = send_log_msg(logger, "getLogSize %d", buf, sizeof(buf));
    if (ret < 0) {
        return ret;
    }

    if ((buf[0] < '0') || ('9' < buf[0])) {
        return -1;
    }

    return atol(buf);
}

LIBLOG_ABI_PUBLIC int android_logger_set_log_size(struct logger *logger,
                                                  unsigned long size)
{
    char buf[512];

    snprintf(buf, sizeof(buf), "setLogSize %d %lu",
        logger ? logger->id : (unsigned) -1, size);

    return check_log_success(buf, send_log_msg(NULL, NULL, buf, sizeof(buf)));
}

/*
 * 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)
{
    char buf[512];

    ssize_t ret = send_log_msg(logger, "getLogSizeUsed %d", buf, sizeof(buf));
    if (ret < 0) {
        return ret;
    }

    if ((buf[0] < '0') || ('9' < buf[0])) {
        return -1;
    }

    return atol(buf);
}

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

/*
 * returns statistics
 */
LIBLOG_ABI_PUBLIC ssize_t android_logger_get_statistics(
        struct logger_list *logger_list,
        char *buf, size_t len)
{
    struct logger *logger;
    char *cp = buf;
    size_t remaining = len;
    size_t n;

    n = snprintf(cp, remaining, "getStatistics");
    n = min(n, remaining);
    remaining -= n;
    cp += n;

    logger_for_each(logger, logger_list) {
        n = snprintf(cp, remaining, " %d", logger->id);
        n = min(n, remaining);
        remaining -= n;
        cp += n;
    }

    if (logger_list->pid) {
        snprintf(cp, remaining, " pid=%u", logger_list->pid);
    }

    return send_log_msg(NULL, NULL, buf, len);
}

LIBLOG_ABI_PUBLIC ssize_t android_logger_get_prune_list(
        struct logger_list *logger_list __unused,
        char *buf, size_t len)
{
    return send_log_msg(NULL, "getPruneList", buf, len);
}

LIBLOG_ABI_PUBLIC int android_logger_set_prune_list(
        struct logger_list *logger_list __unused,
        char *buf, size_t len)
{
    const char cmd[] = "setPruneList ";
    const size_t cmdlen = sizeof(cmd) - 1;

    if (strlen(buf) > (len - cmdlen)) {
        return -ENOMEM; /* KISS */
    }
    memmove(buf + cmdlen, buf, len - cmdlen);
    buf[len - 1] = '\0';
    memcpy(buf, cmd, cmdlen);

    return check_log_success(buf, send_log_msg(NULL, NULL, buf, len));
}

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

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

    list_init(&logger_list->node);
    logger_list->mode = mode;
    logger_list->start.tv_sec = 0;
    logger_list->start.tv_nsec = 0;
    logger_list->tail = tail;
    logger_list->pid = pid;
    logger_list->sock = -1;

    return logger_list;
}

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

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

    list_init(&logger_list->node);
    logger_list->mode = mode;
    logger_list->start = start;
    logger_list->tail = 0;
    logger_list->pid = pid;
    logger_list->sock = -1;

    return 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 id)
{
    struct logger *logger;

    if (!logger_list || (id >= LOG_ID_MAX)) {
        goto err;
    }

    logger_for_each(logger, logger_list) {
        if (logger->id == id) {
            goto ok;
        }
    }

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

    logger->id = id;
    list_add_tail(&logger_list->node, &logger->node);
    logger->top = logger_list;
    goto ok;

err:
    logger = NULL;
ok:
    return 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 id,
        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, id)) {
        android_logger_list_free(logger_list);
        return NULL;
    }

    return logger_list;
}

static int android_logger_list_read_pstore(struct logger_list *logger_list,
                                           struct log_msg *log_msg)
{
    ssize_t ret;
    off_t current, next;
    uid_t uid;
    struct logger *logger;
    struct __attribute__((__packed__)) {
        android_pmsg_log_header_t p;
        android_log_header_t l;
    } buf;
    static uint8_t preread_count;
    bool is_system;

    memset(log_msg, 0, sizeof(*log_msg));

    if (logger_list->sock < 0) {
        int fd = open("/sys/fs/pstore/pmsg-ramoops-0", O_RDONLY);

        if (fd < 0) {
            return -errno;
        }
        logger_list->sock = fd;
        preread_count = 0;
    }

    while(1) {
        if (preread_count < sizeof(buf)) {
            ret = TEMP_FAILURE_RETRY(read(logger_list->sock,
                                          &buf.p.magic + preread_count,
                                          sizeof(buf) - preread_count));
            if (ret < 0) {
                return -errno;
            }
            preread_count += ret;
        }
        if (preread_count != sizeof(buf)) {
            return preread_count ? -EIO : -EAGAIN;
        }
        if ((buf.p.magic != LOGGER_MAGIC)
         || (buf.p.len <= sizeof(buf))
         || (buf.p.len > (sizeof(buf) + LOGGER_ENTRY_MAX_PAYLOAD))
         || (buf.l.id >= LOG_ID_MAX)
         || (buf.l.realtime.tv_nsec >= NS_PER_SEC)) {
            do {
                memmove(&buf.p.magic, &buf.p.magic + 1, --preread_count);
            } while (preread_count && (buf.p.magic != LOGGER_MAGIC));
            continue;
        }
        preread_count = 0;

        logger_for_each(logger, logger_list) {
            if (buf.l.id != logger->id) {
                continue;
            }

            if ((logger_list->start.tv_sec || logger_list->start.tv_nsec)
             && ((logger_list->start.tv_sec > buf.l.realtime.tv_sec)
              || ((logger_list->start.tv_sec == buf.l.realtime.tv_sec)
               && (logger_list->start.tv_nsec > buf.l.realtime.tv_nsec)))) {
                break;
            }

            if (logger_list->pid && (logger_list->pid != buf.p.pid)) {
                break;
            }

            uid = get_best_effective_uid();
            is_system = uid_has_log_permission(uid);
            if (!is_system && (uid != buf.p.uid)) {
                break;
            }

            ret = TEMP_FAILURE_RETRY(read(logger_list->sock,
                                          is_system ?
                                              log_msg->entry_v4.msg :
                                              log_msg->entry_v3.msg,
                                          buf.p.len - sizeof(buf)));
            if (ret < 0) {
                return -errno;
            }
            if (ret != (ssize_t)(buf.p.len - sizeof(buf))) {
                return -EIO;
            }

            log_msg->entry_v4.len = buf.p.len - sizeof(buf);
            log_msg->entry_v4.hdr_size = is_system ?
                sizeof(log_msg->entry_v4) :
                sizeof(log_msg->entry_v3);
            log_msg->entry_v4.pid = buf.p.pid;
            log_msg->entry_v4.tid = buf.l.tid;
            log_msg->entry_v4.sec = buf.l.realtime.tv_sec;
            log_msg->entry_v4.nsec = buf.l.realtime.tv_nsec;
            log_msg->entry_v4.lid = buf.l.id;
            if (is_system) {
                log_msg->entry_v4.uid = buf.p.uid;
            }

            return ret;
        }

        current = TEMP_FAILURE_RETRY(lseek(logger_list->sock,
                                           (off_t)0, SEEK_CUR));
        if (current < 0) {
            return -errno;
        }
        next = TEMP_FAILURE_RETRY(lseek(logger_list->sock,
                                        (off_t)(buf.p.len - sizeof(buf)),
                                        SEEK_CUR));
        if (next < 0) {
            return -errno;
        }
        if ((next - current) != (ssize_t)(buf.p.len - sizeof(buf))) {
            return -EIO;
        }
    }
}

static void caught_signal(int signum __unused)
{
}

/* Read from the selected logs */
LIBLOG_ABI_PUBLIC int android_logger_list_read(
        struct logger_list *logger_list,
        struct log_msg *log_msg)
{
    int ret, e;
    struct logger *logger;
    struct sigaction ignore;
    struct sigaction old_sigaction;
    unsigned int old_alarm = 0;

    if (!logger_list) {
        return -EINVAL;
    }

    if (logger_list->mode & ANDROID_LOG_PSTORE) {
        return android_logger_list_read_pstore(logger_list, log_msg);
    }

    if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
        memset(&ignore, 0, sizeof(ignore));
        ignore.sa_handler = caught_signal;
        sigemptyset(&ignore.sa_mask);
    }

    if (logger_list->sock < 0) {
        char buffer[256], *cp, c;

        int sock = socket_local_client("logdr",
                                       ANDROID_SOCKET_NAMESPACE_RESERVED,
                                       SOCK_SEQPACKET);
        if (sock < 0) {
            if ((sock == -1) && errno) {
                return -errno;
            }
            return sock;
        }

        strcpy(buffer,
               (logger_list->mode & ANDROID_LOG_NONBLOCK) ? "dumpAndClose" : "stream");
        cp = buffer + strlen(buffer);

        strcpy(cp, " lids");
        cp += 5;
        c = '=';
        int remaining = sizeof(buffer) - (cp - buffer);
        logger_for_each(logger, logger_list) {
            ret = snprintf(cp, remaining, "%c%u", c, logger->id);
            ret = min(ret, remaining);
            remaining -= ret;
            cp += ret;
            c = ',';
        }

        if (logger_list->tail) {
            ret = snprintf(cp, remaining, " tail=%u", logger_list->tail);
            ret = min(ret, remaining);
            remaining -= ret;
            cp += ret;
        }

        if (logger_list->start.tv_sec || logger_list->start.tv_nsec) {
            if (logger_list->mode & ANDROID_LOG_WRAP) {
                // ToDo: alternate API to allow timeout to be adjusted.
                ret = snprintf(cp, remaining, " timeout=%u",
                               ANDROID_LOG_WRAP_DEFAULT_TIMEOUT);
                ret = min(ret, remaining);
                remaining -= ret;
                cp += ret;
            }
            ret = snprintf(cp, remaining, " start=%" PRIu32 ".%09" PRIu32,
                           logger_list->start.tv_sec,
                           logger_list->start.tv_nsec);
            ret = min(ret, remaining);
            remaining -= ret;
            cp += ret;
        }

        if (logger_list->pid) {
            ret = snprintf(cp, remaining, " pid=%u", logger_list->pid);
            ret = min(ret, remaining);
            cp += ret;
        }

        if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
            /* Deal with an unresponsive logd */
            sigaction(SIGALRM, &ignore, &old_sigaction);
            old_alarm = alarm(30);
        }
        ret = write(sock, buffer, cp - buffer);
        e = errno;
        if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
            if (e == EINTR) {
                e = ETIMEDOUT;
            }
            alarm(old_alarm);
            sigaction(SIGALRM, &old_sigaction, NULL);
        }

        if (ret <= 0) {
            close(sock);
            if ((ret == -1) && e) {
                return -e;
            }
            if (ret == 0) {
                return -EIO;
            }
            return ret;
        }

        logger_list->sock = sock;
    }

    while(1) {
        memset(log_msg, 0, sizeof(*log_msg));

        if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
            /* particularily useful if tombstone is reporting for logd */
            sigaction(SIGALRM, &ignore, &old_sigaction);
            old_alarm = alarm(30);
        }
        /* NOTE: SOCK_SEQPACKET guarantees we read exactly one full entry */
        ret = recv(logger_list->sock, log_msg, LOGGER_ENTRY_MAX_LEN, 0);
        e = errno;
        if (logger_list->mode & ANDROID_LOG_NONBLOCK) {
            if ((ret == 0) || (e == EINTR)) {
                e = EAGAIN;
                ret = -1;
            }
            alarm(old_alarm);
            sigaction(SIGALRM, &old_sigaction, NULL);
        }

        if ((ret == -1) && e) {
            return -e;
        }
        return ret;
    }
    /* NOTREACH */
    return ret;
}

/* Close all the logs */
LIBLOG_ABI_PUBLIC void android_logger_list_free(
        struct logger_list *logger_list)
{
    if (logger_list == NULL) {
        return;
    }

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

    if (logger_list->sock >= 0) {
        close (logger_list->sock);
    }

    free(logger_list);
}