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