File: metrics.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 (142 lines) | stat: -rw-r--r-- 3,894 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2021 The Android Open Sourete 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.
 */

#define LOG_TAG "metrics"

#include <android-base/logging.h>
#include <fcntl.h>
#include <poll.h>
#include <trusty/metrics/metrics.h>
#include <trusty/metrics/tipc.h>
#include <trusty/tipc.h>
#include <unistd.h>

namespace android {
namespace trusty {
namespace metrics {

using android::base::ErrnoError;
using android::base::Error;

Result<void> TrustyMetrics::Open() {
    int fd = tipc_connect(tipc_dev_.c_str(), METRICS_PORT);
    if (fd < 0) {
        return ErrnoError() << "failed to connect to Trusty metrics TA";
    }

    int flags = fcntl(fd, F_GETFL, 0);
    if (flags < 0) {
        return ErrnoError() << "failed F_GETFL";
    }

    int rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
    if (rc < 0) {
        return ErrnoError() << "failed F_SETFL";
    }

    metrics_fd_.reset(fd);
    return {};
}

Result<void> TrustyMetrics::WaitForEvent(int timeout_ms) {
    if (!metrics_fd_.ok()) {
        return Error() << "connection to Metrics TA has not been initialized yet";
    }

    struct pollfd pfd = {
            .fd = metrics_fd_,
            .events = POLLIN,
    };

    int rc = poll(&pfd, 1, timeout_ms);
    if (rc != 1) {
        return ErrnoError() << "failed poll()";
    }

    if (!(pfd.revents & POLLIN)) {
        return ErrnoError() << "channel not ready";
    }

    return {};
}

Result<void> TrustyMetrics::HandleEvent() {
    if (!metrics_fd_.ok()) {
        return Error() << "connection to Metrics TA has not been initialized yet";
    }

    uint8_t msg[METRICS_MAX_MSG_SIZE];

    auto rc = read(metrics_fd_, msg, sizeof(msg));
    if (rc < 0) {
        return ErrnoError() << "failed to read metrics message";
    }
    size_t msg_len = rc;

    if (msg_len < sizeof(metrics_req)) {
        return Error() << "message too small: " << rc;
    }
    auto req = reinterpret_cast<metrics_req*>(msg);
    size_t offset = sizeof(metrics_req);
    uint32_t status = METRICS_NO_ERROR;

    switch (req->cmd) {
        case METRICS_CMD_REPORT_CRASH: {
            if (msg_len < offset + sizeof(metrics_report_crash_req)) {
                return Error() << "message too small: " << rc;
            }
            auto crash_args = reinterpret_cast<metrics_report_crash_req*>(msg + offset);
            offset += sizeof(metrics_report_crash_req);

            if (msg_len < offset + crash_args->app_id_len) {
                return Error() << "message too small: " << rc;
            }
            auto app_id_ptr = reinterpret_cast<char*>(msg + offset);
            std::string app_id(app_id_ptr, crash_args->app_id_len);

            HandleCrash(app_id);
            break;
        }

        case METRICS_CMD_REPORT_EVENT_DROP:
            HandleEventDrop();
            break;

        default:
            status = METRICS_ERR_UNKNOWN_CMD;
            break;
    }

    metrics_resp resp = {
            .cmd = req->cmd | METRICS_CMD_RESP_BIT,
            .status = status,
    };

    rc = write(metrics_fd_, &resp, sizeof(resp));
    if (rc < 0) {
        return ErrnoError() << "failed to request next metrics event";
    }

    if (rc != (int)sizeof(resp)) {
        return Error() << "unexpected number of bytes sent event: " << rc;
    }

    return {};
}

}  // namespace metrics
}  // namespace trusty
}  // namespace android