• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 using android::base::unique_fd;
32 
33 #define JDWP_CONTROL_NAME "\0jdwp-control"
34 #define JDWP_CONTROL_NAME_LEN (sizeof(JDWP_CONTROL_NAME) - 1)
35 
36 static_assert(JDWP_CONTROL_NAME_LEN <= sizeof(reinterpret_cast<sockaddr_un*>(0)->sun_path));
37 
38 // Listen for incoming jdwp clients forever.
adbconnection_listen(void (* callback)(int fd,pid_t pid))39 void adbconnection_listen(void (*callback)(int fd, pid_t pid)) {
40   sockaddr_un addr = {};
41   socklen_t addrlen = JDWP_CONTROL_NAME_LEN + sizeof(addr.sun_family);
42 
43   addr.sun_family = AF_UNIX;
44   memcpy(addr.sun_path, JDWP_CONTROL_NAME, JDWP_CONTROL_NAME_LEN);
45 
46   unique_fd s(socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
47   if (s < 0) {
48     PLOG(ERROR) << "failed to create JDWP control socket";
49     return;
50   }
51 
52   if (bind(s.get(), reinterpret_cast<sockaddr*>(&addr), addrlen) < 0) {
53     PLOG(ERROR) << "failed to bind JDWP control socket";
54     return;
55   }
56 
57   if (listen(s.get(), 4) < 0) {
58     PLOG(ERROR) << "failed to listen on JDWP control socket";
59     return;
60   }
61 
62   std::vector<unique_fd> pending_connections;
63 
64   unique_fd epfd(epoll_create1(EPOLL_CLOEXEC));
65   std::array<epoll_event, 16> events;
66 
67   events[0].events = EPOLLIN;
68   events[0].data.fd = -1;
69   if (epoll_ctl(epfd.get(), EPOLL_CTL_ADD, s.get(), &events[0]) != 0) {
70     LOG(FATAL) << "failed to register event with epoll fd";
71   }
72 
73   while (true) {
74     int epoll_rc = TEMP_FAILURE_RETRY(epoll_wait(epfd.get(), events.data(), events.size(), -1));
75     if (epoll_rc == -1) {
76       PLOG(FATAL) << "epoll_wait failed";
77     }
78 
79     for (int i = 0; i < epoll_rc; ++i) {
80       const epoll_event& event = events[i];
81       if (event.data.fd == -1) {
82         unique_fd client(
83             TEMP_FAILURE_RETRY(accept4(s.get(), nullptr, nullptr, SOCK_NONBLOCK | SOCK_CLOEXEC)));
84 
85         if (client == -1) {
86           PLOG(WARNING) << "failed to accept client on JDWP control socket";
87           continue;
88         }
89 
90         epoll_event register_event;
91         register_event.events = EPOLLIN;
92         register_event.data.fd = client.get();
93 
94         if (epoll_ctl(epfd.get(), EPOLL_CTL_ADD, client.get(), &register_event) != 0) {
95           PLOG(FATAL) << "failed to register JDWP client with epoll";
96         }
97 
98         pending_connections.emplace_back(std::move(client));
99       } else {
100         // n^2, but the backlog should be short.
101         auto it = std::find_if(pending_connections.begin(), pending_connections.end(),
102                                [&](const unique_fd& fd) { return fd.get() == event.data.fd; });
103 
104         if (it == pending_connections.end()) {
105           LOG(FATAL) << "failed to find JDWP client (" << event.data.fd
106                      << ") in pending connections";
107         }
108 
109         // Massively oversized buffer: we're expecting an int32_t from the other end.
110         char buf[32];
111         int rc = TEMP_FAILURE_RETRY(recv(it->get(), buf, sizeof(buf), MSG_DONTWAIT));
112         if (rc != 4) {
113           LOG(ERROR) << "received data of incorrect size from JDWP client: read " << rc
114                      << ", expected 4";
115         } else {
116           int32_t pid;
117           memcpy(&pid, buf, sizeof(pid));
118           callback(it->release(), static_cast<pid_t>(pid));
119         }
120 
121         if (epoll_ctl(epfd.get(), EPOLL_CTL_DEL, event.data.fd, nullptr) != 0) {
122           LOG(FATAL) << "failed to delete fd from JDWP epoll fd";
123         }
124 
125         pending_connections.erase(it);
126       }
127     }
128   }
129 }
130