1 /*
2  * Copyright (C) 2019 The Android Open Source 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 #include "adbconnection/server.h"
18 
19 #include <sys/epoll.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <unistd.h>
23 
24 #include <algorithm>
25 #include <array>
26 #include <vector>
27 
28 #include <android-base/logging.h>
29 #include <android-base/unique_fd.h>
30 
31 #include "adbconnection/common.h"
32 #include "app_processes.pb.h"
33 
34 using android::base::unique_fd;
35 
readProcessInfoFromSocket(int socket)36 std::optional<ProcessInfo> readProcessInfoFromSocket(int socket) {
37   std::string proto;
38   proto.resize(MAX_APP_MESSAGE_LENGTH);
39   ssize_t rc = TEMP_FAILURE_RETRY(recv(socket, proto.data(), proto.length(), MSG_PEEK));
40 
41   if (rc == 0) {
42     LOG(INFO) << "Remote process closed the socket (on MSG_PEEK)";
43     return {};
44   }
45 
46   if (rc == -1) {
47     PLOG(ERROR) << "adbconnection_server: Unable to MSG_PEEK ProcessInfo recv";
48     return {};
49   }
50 
51   ssize_t message_size = rc;
52   proto.resize(message_size);
53   rc = TEMP_FAILURE_RETRY(recv(socket, proto.data(), message_size, 0));
54 
55   if (rc == 0) {
56     LOG(INFO) << "Remote process closed the socket (on recv)";
57     return {};
58   }
59 
60   if (rc == -1) {
61     PLOG(ERROR) << "adbconnection_server: Unable to recv ProcessInfo " << message_size << " bytes";
62     return {};
63   }
64 
65   if (rc != message_size) {
66     LOG(ERROR) << "adbconnection_server: Unexpected ProcessInfo size " << message_size
67                << " bytes but got " << rc;
68     return {};
69   }
70 
71   return ProcessInfo::parseProtobufString(proto);
72 }
73 
74 // Listen for incoming jdwp clients forever.
adbconnection_listen(void (* callback)(int fd,ProcessInfo process))75 void adbconnection_listen(void (*callback)(int fd, ProcessInfo process)) {
76   unique_fd s(socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
77   if (s < 0) {
78     PLOG(ERROR) << "failed to create JDWP control socket";
79     return;
80   }
81 
82   auto [addr, addr_len] = get_control_socket_addr();
83   if (bind(s.get(), reinterpret_cast<sockaddr*>(&addr), addr_len) < 0) {
84     PLOG(ERROR) << "failed to bind JDWP control socket";
85     return;
86   }
87 
88   if (listen(s.get(), 4) < 0) {
89     PLOG(ERROR) << "failed to listen on JDWP control socket";
90     return;
91   }
92 
93   std::vector<unique_fd> pending_connections;
94 
95   unique_fd epfd(epoll_create1(EPOLL_CLOEXEC));
96   std::array<epoll_event, 16> events;
97 
98   events[0].events = EPOLLIN;
99   events[0].data.fd = -1;
100   if (epoll_ctl(epfd.get(), EPOLL_CTL_ADD, s.get(), &events[0]) != 0) {
101     PLOG(FATAL) << "failed to register socket " << s.get() << " with epoll fd";
102   }
103 
104   while (true) {
105     int epoll_rc = TEMP_FAILURE_RETRY(epoll_wait(epfd.get(), events.data(), events.size(), -1));
106     if (epoll_rc == -1) {
107       PLOG(FATAL) << "epoll_wait failed";
108     }
109 
110     for (int i = 0; i < epoll_rc; ++i) {
111       const epoll_event& event = events[i];
112       if (event.data.fd == -1) {
113         unique_fd client(
114             TEMP_FAILURE_RETRY(accept4(s.get(), nullptr, nullptr, SOCK_NONBLOCK | SOCK_CLOEXEC)));
115 
116         if (client == -1) {
117           PLOG(WARNING) << "failed to accept client on JDWP control socket";
118           continue;
119         }
120 
121         epoll_event register_event;
122         register_event.events = EPOLLIN;
123         register_event.data.fd = client.get();
124 
125         if (epoll_ctl(epfd.get(), EPOLL_CTL_ADD, client.get(), &register_event) != 0) {
126           PLOG(FATAL) << "failed to register JDWP client " << client.get() << " with epoll";
127         }
128 
129         pending_connections.emplace_back(std::move(client));
130       } else {
131         // n^2, but the backlog should be short.
132         auto it = std::find_if(pending_connections.begin(), pending_connections.end(),
133                                [&](const unique_fd& fd) { return fd.get() == event.data.fd; });
134 
135         if (it == pending_connections.end()) {
136           LOG(FATAL) << "failed to find JDWP client (" << event.data.fd
137                      << ") in pending connections";
138         }
139 
140         auto process_info = readProcessInfoFromSocket(it->get());
141         if (process_info) {
142           callback(it->release(), *process_info);
143         } else {
144           LOG(ERROR) << "Unable to read ProcessInfo from app startup";
145         }
146 
147         if (epoll_ctl(epfd.get(), EPOLL_CTL_DEL, event.data.fd, nullptr) != 0) {
148           PLOG(FATAL) << "failed to delete fd " << event.data.fd << " from JDWP epoll fd";
149         }
150 
151         pending_connections.erase(it);
152       }
153     }
154   }
155 }
156