1 /*
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 
27 namespace android {
28 namespace trusty {
29 namespace metrics {
30 
31 using android::base::ErrnoError;
32 using android::base::Error;
33 
Open()34 Result<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 
WaitForEvent(int timeout_ms)54 Result<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 
HandleEvent()76 Result<void> TrustyMetrics::HandleEvent() {
77     if (!metrics_fd_.ok()) {
78         return Error() << "connection to Metrics TA has not been initialized yet";
79     }
80 
81     uint8_t msg[METRICS_MAX_MSG_SIZE];
82 
83     auto rc = read(metrics_fd_, msg, sizeof(msg));
84     if (rc < 0) {
85         return ErrnoError() << "failed to read metrics message";
86     }
87     size_t msg_len = rc;
88 
89     if (msg_len < sizeof(metrics_req)) {
90         return Error() << "message too small: " << rc;
91     }
92     auto req = reinterpret_cast<metrics_req*>(msg);
93     size_t offset = sizeof(metrics_req);
94     uint32_t status = METRICS_NO_ERROR;
95 
96     switch (req->cmd) {
97         case METRICS_CMD_REPORT_CRASH: {
98             if (msg_len < offset + sizeof(metrics_report_crash_req)) {
99                 return Error() << "message too small: " << rc;
100             }
101             auto crash_args = reinterpret_cast<metrics_report_crash_req*>(msg + offset);
102             offset += sizeof(metrics_report_crash_req);
103 
104             if (msg_len < offset + crash_args->app_id_len) {
105                 return Error() << "message too small: " << rc;
106             }
107             auto app_id_ptr = reinterpret_cast<char*>(msg + offset);
108             std::string app_id(app_id_ptr, crash_args->app_id_len);
109 
110             HandleCrash(app_id);
111             break;
112         }
113 
114         case METRICS_CMD_REPORT_EVENT_DROP:
115             HandleEventDrop();
116             break;
117 
118         default:
119             status = METRICS_ERR_UNKNOWN_CMD;
120             break;
121     }
122 
123     metrics_resp resp = {
124             .cmd = req->cmd | METRICS_CMD_RESP_BIT,
125             .status = status,
126     };
127 
128     rc = write(metrics_fd_, &resp, sizeof(resp));
129     if (rc < 0) {
130         return ErrnoError() << "failed to request next metrics event";
131     }
132 
133     if (rc != (int)sizeof(resp)) {
134         return Error() << "unexpected number of bytes sent event: " << rc;
135     }
136 
137     return {};
138 }
139 
140 }  // namespace metrics
141 }  // namespace trusty
142 }  // namespace android
143