blob: d2f0b0acf07357ae6594806b5f6c87ba2629bb3d [file] [log] [blame]
Tri Voa8919a22021-03-11 17:34:02 -08001/*
2 * Copyright (C) 2021 The Android Open Sourete 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 LOG_TAG "metrics"
18
19#include <android-base/logging.h>
20#include <fcntl.h>
21#include <poll.h>
22#include <trusty/metrics/metrics.h>
23#include <trusty/metrics/tipc.h>
24#include <trusty/tipc.h>
25#include <unistd.h>
26
27namespace android {
28namespace trusty {
29namespace metrics {
30
31using android::base::ErrnoError;
32using android::base::Error;
33
34Result<void> TrustyMetrics::Open() {
35 int fd = tipc_connect(tipc_dev_.c_str(), METRICS_PORT);
36 if (fd < 0) {
37 return ErrnoError() << "failed to connect to Trusty metrics TA";
38 }
39
40 int flags = fcntl(fd, F_GETFL, 0);
41 if (flags < 0) {
42 return ErrnoError() << "failed F_GETFL";
43 }
44
45 int rc = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
46 if (rc < 0) {
47 return ErrnoError() << "failed F_SETFL";
48 }
49
50 metrics_fd_.reset(fd);
51 return {};
52}
53
54Result<void> TrustyMetrics::WaitForEvent(int timeout_ms) {
55 if (!metrics_fd_.ok()) {
56 return Error() << "connection to Metrics TA has not been initialized yet";
57 }
58
59 struct pollfd pfd = {
60 .fd = metrics_fd_,
61 .events = POLLIN,
62 };
63
64 int rc = poll(&pfd, 1, timeout_ms);
65 if (rc != 1) {
66 return ErrnoError() << "failed poll()";
67 }
68
69 if (!(pfd.revents & POLLIN)) {
70 return ErrnoError() << "channel not ready";
71 }
72
73 return {};
74}
75
76Result<void> TrustyMetrics::HandleEvent() {
77 if (!metrics_fd_.ok()) {
78 return Error() << "connection to Metrics TA has not been initialized yet";
79 }
80
Snehaldfd84432024-02-28 18:08:09 +000081 struct metrics_msg metrics_msg;
82 int rc = read(metrics_fd_, &metrics_msg, sizeof(metrics_msg));
Tri Voa8919a22021-03-11 17:34:02 -080083 if (rc < 0) {
84 return ErrnoError() << "failed to read metrics message";
85 }
86 size_t msg_len = rc;
87
88 if (msg_len < sizeof(metrics_req)) {
89 return Error() << "message too small: " << rc;
90 }
Snehaldfd84432024-02-28 18:08:09 +000091 uint32_t cmd = metrics_msg.req.cmd;
Tri Voa8919a22021-03-11 17:34:02 -080092 uint32_t status = METRICS_NO_ERROR;
93
Snehaldfd84432024-02-28 18:08:09 +000094 switch (cmd) {
Tri Voa8919a22021-03-11 17:34:02 -080095 case METRICS_CMD_REPORT_CRASH: {
Snehaldfd84432024-02-28 18:08:09 +000096 struct metrics_report_crash_req crash_args = metrics_msg.crash_args;
97 auto app_id_ptr = crash_args.app_id;
98 std::string app_id(app_id_ptr, UUID_STR_SIZE);
Tri Voa8919a22021-03-11 17:34:02 -080099
100 HandleCrash(app_id);
101 break;
102 }
103
104 case METRICS_CMD_REPORT_EVENT_DROP:
105 HandleEventDrop();
106 break;
107
108 default:
109 status = METRICS_ERR_UNKNOWN_CMD;
110 break;
111 }
112
113 metrics_resp resp = {
Snehaldfd84432024-02-28 18:08:09 +0000114 .cmd = cmd | METRICS_CMD_RESP_BIT,
Tri Voa8919a22021-03-11 17:34:02 -0800115 .status = status,
116 };
117
118 rc = write(metrics_fd_, &resp, sizeof(resp));
119 if (rc < 0) {
120 return ErrnoError() << "failed to request next metrics event";
121 }
122
123 if (rc != (int)sizeof(resp)) {
124 return Error() << "unexpected number of bytes sent event: " << rc;
125 }
126
127 return {};
128}
129
130} // namespace metrics
131} // namespace trusty
132} // namespace android