File: TrustyLog.cpp

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (125 lines) | stat: -rw-r--r-- 4,348 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2021 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 "TrustyLog.h"
#include <private/android_logger.h>
#include "LogBuffer.h"

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#define TRUSTY_LINE_BUFFER_SIZE 256
static const char trustyprefix[] = "trusty";

TrustyLog::TrustyLog(LogBuffer* buf, int fdRead) : SocketListener(fdRead, false), logbuf(buf) {}

void TrustyLog::create(LogBuffer* buf) {
    if (access("/sys/module/trusty_log/parameters/log_size", F_OK)) {
        /* this device has the old driver which doesn't support poll() */
        return;
    }

    int fd = TEMP_FAILURE_RETRY(open("/dev/trusty-log0", O_RDONLY | O_NDELAY | O_CLOEXEC));
    if (fd >= 0) {
        TrustyLog* tl = new TrustyLog(buf, fd);
        if (tl->startListener()) {
            delete tl;
        }
    }
}

/*
 * Log a message, breaking it into smaller chunks if needed
 */
void TrustyLog::LogMsg(const char* msg, size_t len) {
    char linebuffer[TRUSTY_LINE_BUFFER_SIZE + sizeof(trustyprefix) + 1];

    while (len) {
        size_t sublen = len;
        if (sublen > TRUSTY_LINE_BUFFER_SIZE) {
            sublen = TRUSTY_LINE_BUFFER_SIZE;
        }

        *linebuffer = ANDROID_LOG_INFO;
        strcpy(linebuffer + 1, trustyprefix);
        strncpy(linebuffer + 1 + sizeof(trustyprefix), msg, sublen);
        timespec tp;
        clock_gettime(CLOCK_REALTIME, &tp);
        log_time now = log_time(tp.tv_sec, tp.tv_nsec);
        // The Log() API appears to want a length that is 1 greater than what's
        // actually being logged.
        logbuf->Log(LOG_ID_KERNEL, now, AID_ROOT, 0 /*pid*/, 0 /*tid*/, linebuffer,
                    sizeof(trustyprefix) + sublen + 2);
        msg += sublen;
        len -= sublen;
    }
}

bool TrustyLog::onDataAvailable(SocketClient* cli) {
    char buffer[4096];
    ssize_t len = 0;
    bool need_newline = false;
    for (;;) {
        ssize_t retval = 0;
        if (len < (ssize_t)(sizeof(buffer) - 1)) {
            retval = TEMP_FAILURE_RETRY(
                    read(cli->getSocket(), buffer + len, sizeof(buffer) - 1 - len));
        }
        if (retval > 0) {
            len += retval;
        }
        if ((retval <= 0) && (len <= 0)) {
            // nothing read and nothing to read
            break;
        }

        // log the complete lines we have so far
        char* linestart = buffer;
        for (;;) {
            char* lineend = static_cast<char*>(memchr(linestart, '\n', len));
            if (lineend) {
                // print one newline-terminated line
                size_t linelen = lineend - linestart;
                LogMsg(linestart, linelen);
                linestart += (linelen + 1);  // next line, skipping the newline
                len -= (linelen + 1);
            } else if (len >= TRUSTY_LINE_BUFFER_SIZE) {
                // there was no newline, but there's enough data to print
                LogMsg(linestart, TRUSTY_LINE_BUFFER_SIZE);
                linestart += TRUSTY_LINE_BUFFER_SIZE;
                len -= TRUSTY_LINE_BUFFER_SIZE;
            } else {
                if (len) {
                    if (need_newline) {
                        // still no newline after reading more, log what we have
                        // and return
                        LogMsg(linestart, len);
                        return true;
                    }
                    // there's some unterminated data left at the end of the
                    // buffer. Move it to the front and try to append more in
                    // the outer loop.
                    memmove(buffer, linestart, len);
                    need_newline = true;
                }
                break;
            }
        }
    }
    return true;
}