Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "socket_spec.h" |
| 18 | |
Daniel Colascione | 3e12469 | 2019-11-13 17:49:37 -0800 | [diff] [blame^] | 19 | #include <limits> |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 20 | #include <string> |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 21 | #include <string_view> |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 22 | #include <unordered_map> |
| 23 | #include <vector> |
| 24 | |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 25 | #include <android-base/parseint.h> |
| 26 | #include <android-base/parsenetaddress.h> |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> |
| 28 | #include <android-base/strings.h> |
| 29 | #include <cutils/sockets.h> |
| 30 | |
| 31 | #include "adb.h" |
Daniel Colascione | 3e12469 | 2019-11-13 17:49:37 -0800 | [diff] [blame^] | 32 | #include "adb_utils.h" |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 33 | #include "sysdeps.h" |
| 34 | |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 35 | using namespace std::string_literals; |
| 36 | |
Daniel Colascione | 3e12469 | 2019-11-13 17:49:37 -0800 | [diff] [blame^] | 37 | using android::base::ConsumePrefix; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 38 | using android::base::StringPrintf; |
| 39 | |
| 40 | #if defined(__linux__) |
| 41 | #define ADB_LINUX 1 |
| 42 | #else |
| 43 | #define ADB_LINUX 0 |
| 44 | #endif |
| 45 | |
| 46 | #if defined(_WIN32) |
| 47 | #define ADB_WINDOWS 1 |
| 48 | #else |
| 49 | #define ADB_WINDOWS 0 |
| 50 | #endif |
| 51 | |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 52 | #if ADB_LINUX |
| 53 | #include <sys/socket.h> |
| 54 | #include "sysdeps/vm_sockets.h" |
| 55 | #endif |
| 56 | |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 57 | // Not static because it is used in commandline.c. |
| 58 | int gListenAll = 0; |
| 59 | |
| 60 | struct LocalSocketType { |
| 61 | int socket_namespace; |
| 62 | bool available; |
| 63 | }; |
| 64 | |
| 65 | static auto& kLocalSocketTypes = *new std::unordered_map<std::string, LocalSocketType>({ |
| 66 | #if ADB_HOST |
| 67 | { "local", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } }, |
| 68 | #else |
| 69 | { "local", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_WINDOWS } }, |
| 70 | #endif |
| 71 | |
| 72 | { "localreserved", { ANDROID_SOCKET_NAMESPACE_RESERVED, !ADB_HOST } }, |
| 73 | { "localabstract", { ANDROID_SOCKET_NAMESPACE_ABSTRACT, ADB_LINUX } }, |
| 74 | { "localfilesystem", { ANDROID_SOCKET_NAMESPACE_FILESYSTEM, !ADB_WINDOWS } }, |
| 75 | }); |
| 76 | |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 77 | bool parse_tcp_socket_spec(std::string_view spec, std::string* hostname, int* port, |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 78 | std::string* serial, std::string* error) { |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 79 | if (!spec.starts_with("tcp:")) { |
| 80 | *error = "specification is not tcp: "; |
| 81 | *error += spec; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 85 | std::string hostname_value; |
| 86 | int port_value; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 87 | |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 88 | // If the spec is tcp:<port>, parse it ourselves. |
| 89 | // Otherwise, delegate to android::base::ParseNetAddress. |
| 90 | if (android::base::ParseInt(&spec[4], &port_value)) { |
| 91 | // Do the range checking ourselves, because ParseInt rejects 'tcp:65536' and 'tcp:foo:1234' |
| 92 | // identically. |
| 93 | if (port_value < 0 || port_value > 65535) { |
| 94 | *error = StringPrintf("bad port number '%d'", port_value); |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 95 | return false; |
| 96 | } |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 97 | } else { |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 98 | std::string addr(spec.substr(4)); |
Elliott Hughes | 14d673e | 2019-07-30 13:51:03 -0700 | [diff] [blame] | 99 | port_value = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 100 | |
| 101 | // FIXME: ParseNetAddress rejects port 0. This currently doesn't hurt, because listening |
| 102 | // on an address that isn't 'localhost' is unsupported. |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 103 | if (!android::base::ParseNetAddress(addr, &hostname_value, &port_value, serial, error)) { |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 104 | return false; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 105 | } |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 106 | |
| 107 | if (port_value == -1) { |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 108 | *error = "missing port in specification: "; |
| 109 | *error += spec; |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 110 | return false; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (hostname) { |
| 115 | *hostname = std::move(hostname_value); |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | if (port) { |
Josh Gao | 3fb517c | 2016-09-19 17:31:55 -0700 | [diff] [blame] | 119 | *port = port_value; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 125 | static bool tcp_host_is_local(std::string_view hostname) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 126 | // FIXME |
| 127 | return hostname.empty() || hostname == "localhost"; |
| 128 | } |
| 129 | |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 130 | bool is_socket_spec(std::string_view spec) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 131 | for (const auto& it : kLocalSocketTypes) { |
| 132 | std::string prefix = it.first + ":"; |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 133 | if (spec.starts_with(prefix)) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 134 | return true; |
| 135 | } |
| 136 | } |
Daniel Colascione | 3e12469 | 2019-11-13 17:49:37 -0800 | [diff] [blame^] | 137 | return spec.starts_with("tcp:") || spec.starts_with("acceptfd:"); |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 140 | bool is_local_socket_spec(std::string_view spec) { |
Josh Gao | bb4f860 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 141 | for (const auto& it : kLocalSocketTypes) { |
| 142 | std::string prefix = it.first + ":"; |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 143 | if (spec.starts_with(prefix)) { |
Josh Gao | bb4f860 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | std::string error; |
| 149 | std::string hostname; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 150 | if (!parse_tcp_socket_spec(spec, &hostname, nullptr, nullptr, &error)) { |
Josh Gao | bb4f860 | 2016-08-25 16:00:22 -0700 | [diff] [blame] | 151 | return false; |
| 152 | } |
| 153 | return tcp_host_is_local(hostname); |
| 154 | } |
| 155 | |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 156 | bool socket_spec_connect(unique_fd* fd, std::string_view address, int* port, std::string* serial, |
| 157 | std::string* error) { |
| 158 | if (address.starts_with("tcp:")) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 159 | std::string hostname; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 160 | int port_value = port ? *port : 0; |
| 161 | if (!parse_tcp_socket_spec(address, &hostname, &port_value, serial, error)) { |
| 162 | return false; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 165 | if (tcp_host_is_local(hostname)) { |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 166 | fd->reset(network_loopback_client(port_value, SOCK_STREAM, error)); |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 167 | } else { |
| 168 | #if ADB_HOST |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 169 | fd->reset(network_connect(hostname, port_value, SOCK_STREAM, 0, error)); |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 170 | #else |
| 171 | // Disallow arbitrary connections in adbd. |
| 172 | *error = "adbd does not support arbitrary tcp connections"; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 173 | return false; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 174 | #endif |
| 175 | } |
| 176 | |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 177 | if (fd->get() > 0) { |
| 178 | disable_tcp_nagle(fd->get()); |
| 179 | if (port) { |
| 180 | *port = port_value; |
| 181 | } |
| 182 | return true; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 183 | } |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 184 | return false; |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 185 | } else if (address.starts_with("vsock:")) { |
| 186 | #if ADB_LINUX |
| 187 | std::string spec_str(address); |
| 188 | std::vector<std::string> fragments = android::base::Split(spec_str, ":"); |
| 189 | unsigned int port_value = port ? *port : 0; |
| 190 | if (fragments.size() != 2 && fragments.size() != 3) { |
| 191 | *error = android::base::StringPrintf("expected vsock:cid or vsock:port:cid in '%s'", |
| 192 | spec_str.c_str()); |
| 193 | errno = EINVAL; |
| 194 | return false; |
| 195 | } |
| 196 | unsigned int cid = 0; |
| 197 | if (!android::base::ParseUint(fragments[1], &cid)) { |
| 198 | *error = android::base::StringPrintf("could not parse vsock cid in '%s'", |
| 199 | spec_str.c_str()); |
| 200 | errno = EINVAL; |
| 201 | return false; |
| 202 | } |
| 203 | if (fragments.size() == 3 && !android::base::ParseUint(fragments[2], &port_value)) { |
| 204 | *error = android::base::StringPrintf("could not parse vsock port in '%s'", |
| 205 | spec_str.c_str()); |
| 206 | errno = EINVAL; |
| 207 | return false; |
| 208 | } |
| 209 | if (port_value == 0) { |
| 210 | *error = android::base::StringPrintf("vsock port was not provided."); |
| 211 | errno = EINVAL; |
| 212 | return false; |
| 213 | } |
| 214 | fd->reset(socket(AF_VSOCK, SOCK_STREAM, 0)); |
| 215 | if (fd->get() == -1) { |
| 216 | *error = "could not open vsock socket"; |
| 217 | return false; |
| 218 | } |
| 219 | sockaddr_vm addr{}; |
| 220 | addr.svm_family = AF_VSOCK; |
| 221 | addr.svm_port = port_value; |
| 222 | addr.svm_cid = cid; |
| 223 | if (serial) { |
| 224 | *serial = android::base::StringPrintf("vsock:%u:%d", cid, port_value); |
| 225 | } |
| 226 | if (connect(fd->get(), reinterpret_cast<sockaddr*>(&addr), sizeof(addr))) { |
| 227 | int error_num = errno; |
| 228 | *error = android::base::StringPrintf("could not connect to vsock address '%s'", |
| 229 | spec_str.c_str()); |
| 230 | errno = error_num; |
| 231 | return false; |
| 232 | } |
| 233 | if (port) { |
| 234 | *port = port_value; |
| 235 | } |
| 236 | return true; |
| 237 | #else // ADB_LINUX |
| 238 | *error = "vsock is only supported on linux"; |
| 239 | return false; |
| 240 | #endif // ADB_LINUX |
Daniel Colascione | 3e12469 | 2019-11-13 17:49:37 -0800 | [diff] [blame^] | 241 | } else if (address.starts_with("acceptfd:")) { |
| 242 | *error = "cannot connect to acceptfd"; |
| 243 | return false; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | for (const auto& it : kLocalSocketTypes) { |
| 247 | std::string prefix = it.first + ":"; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 248 | if (address.starts_with(prefix)) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 249 | if (!it.second.available) { |
| 250 | *error = StringPrintf("socket type %s is unavailable on this platform", |
| 251 | it.first.c_str()); |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 252 | return false; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 255 | fd->reset(network_local_client(&address[prefix.length()], it.second.socket_namespace, |
| 256 | SOCK_STREAM, error)); |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 257 | if (serial) { |
| 258 | *serial = address; |
| 259 | } |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 260 | return true; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 264 | *error = "unknown socket specification: "; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 265 | *error += address; |
| 266 | return false; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 269 | int socket_spec_listen(std::string_view spec, std::string* error, int* resolved_port) { |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 270 | if (spec.starts_with("tcp:")) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 271 | std::string hostname; |
| 272 | int port; |
Cody Schuffelen | 331a908 | 2019-01-02 14:17:29 -0800 | [diff] [blame] | 273 | if (!parse_tcp_socket_spec(spec, &hostname, &port, nullptr, error)) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | int result; |
| 278 | if (hostname.empty() && gListenAll) { |
| 279 | result = network_inaddr_any_server(port, SOCK_STREAM, error); |
| 280 | } else if (tcp_host_is_local(hostname)) { |
Callum Ryan | 04efea3 | 2019-10-31 07:21:42 -0700 | [diff] [blame] | 281 | result = network_loopback_server(port, SOCK_STREAM, error, true); |
| 282 | } else if (hostname == "::1") { |
| 283 | result = network_loopback_server(port, SOCK_STREAM, error, false); |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 284 | } else { |
| 285 | // TODO: Implement me. |
| 286 | *error = "listening on specified hostname currently unsupported"; |
| 287 | return -1; |
| 288 | } |
| 289 | |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 290 | if (result >= 0 && resolved_port) { |
| 291 | *resolved_port = adb_socket_get_local_port(result); |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 292 | } |
| 293 | return result; |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 294 | } else if (spec.starts_with("vsock:")) { |
| 295 | #if ADB_LINUX |
| 296 | std::string spec_str(spec); |
| 297 | std::vector<std::string> fragments = android::base::Split(spec_str, ":"); |
| 298 | if (fragments.size() != 2) { |
| 299 | *error = "given vsock server socket string was invalid"; |
| 300 | return -1; |
| 301 | } |
| 302 | int port; |
| 303 | if (!android::base::ParseInt(fragments[1], &port)) { |
| 304 | *error = "could not parse vsock port"; |
| 305 | errno = EINVAL; |
| 306 | return -1; |
| 307 | } else if (port < 0) { |
| 308 | *error = "vsock port was negative."; |
| 309 | errno = EINVAL; |
| 310 | return -1; |
| 311 | } |
| 312 | unique_fd serverfd(socket(AF_VSOCK, SOCK_STREAM, 0)); |
| 313 | if (serverfd == -1) { |
| 314 | int error_num = errno; |
| 315 | *error = android::base::StringPrintf("could not create vsock server: '%s'", |
| 316 | strerror(error_num)); |
| 317 | errno = error_num; |
| 318 | return -1; |
| 319 | } |
| 320 | sockaddr_vm addr{}; |
| 321 | addr.svm_family = AF_VSOCK; |
| 322 | addr.svm_port = port == 0 ? VMADDR_PORT_ANY : port; |
| 323 | addr.svm_cid = VMADDR_CID_ANY; |
| 324 | socklen_t addr_len = sizeof(addr); |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 325 | if (bind(serverfd.get(), reinterpret_cast<struct sockaddr*>(&addr), addr_len)) { |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 326 | return -1; |
| 327 | } |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 328 | if (listen(serverfd.get(), 4)) { |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 329 | return -1; |
| 330 | } |
| 331 | if (serverfd >= 0 && resolved_port) { |
Josh Gao | 90228a6 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 332 | if (getsockname(serverfd.get(), reinterpret_cast<sockaddr*>(&addr), &addr_len) == 0) { |
Cody Schuffelen | 637aaf5 | 2019-01-04 18:51:11 -0800 | [diff] [blame] | 333 | *resolved_port = addr.svm_port; |
| 334 | } else { |
| 335 | return -1; |
| 336 | } |
| 337 | } |
| 338 | return serverfd.release(); |
| 339 | #else // ADB_LINUX |
| 340 | *error = "vsock is only supported on linux"; |
| 341 | return -1; |
| 342 | #endif // ADB_LINUX |
Daniel Colascione | 3e12469 | 2019-11-13 17:49:37 -0800 | [diff] [blame^] | 343 | } else if (ConsumePrefix(&spec, "acceptfd:")) { |
| 344 | #if ADB_WINDOWS |
| 345 | *error = "socket activation not supported under Windows"; |
| 346 | return -1; |
| 347 | #else |
| 348 | // We inherited the socket from some kind of launcher. It's already bound and |
| 349 | // listening. Return a copy of the FD instead of the FD itself so we implement the |
| 350 | // normal "listen" contract and can succeed more than once. |
| 351 | unsigned int fd_u; |
| 352 | if (!ParseUint(&fd_u, spec) || fd_u > std::numeric_limits<int>::max()) { |
| 353 | *error = "invalid fd"; |
| 354 | return -1; |
| 355 | } |
| 356 | int fd = static_cast<int>(fd_u); |
| 357 | int flags = get_fd_flags(fd); |
| 358 | if (flags < 0) { |
| 359 | *error = android::base::StringPrintf("could not get flags of inherited fd %d: '%s'", fd, |
| 360 | strerror(errno)); |
| 361 | return -1; |
| 362 | } |
| 363 | if (flags & FD_CLOEXEC) { |
| 364 | *error = android::base::StringPrintf("fd %d was not inherited from parent", fd); |
| 365 | return -1; |
| 366 | } |
| 367 | |
| 368 | int dummy_sock_type; |
| 369 | socklen_t dummy_sock_type_size = sizeof(dummy_sock_type); |
| 370 | if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &dummy_sock_type, &dummy_sock_type_size)) { |
| 371 | *error = android::base::StringPrintf("fd %d does not refer to a socket", fd); |
| 372 | return -1; |
| 373 | } |
| 374 | |
| 375 | int new_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
| 376 | if (new_fd < 0) { |
| 377 | *error = android::base::StringPrintf("could not dup inherited fd %d: '%s'", fd, |
| 378 | strerror(errno)); |
| 379 | return -1; |
| 380 | } |
| 381 | return new_fd; |
| 382 | #endif |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | for (const auto& it : kLocalSocketTypes) { |
| 386 | std::string prefix = it.first + ":"; |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 387 | if (spec.starts_with(prefix)) { |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 388 | if (!it.second.available) { |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 389 | *error = "attempted to listen on unavailable socket type: "; |
| 390 | *error += spec; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | return network_local_server(&spec[prefix.length()], it.second.socket_namespace, |
| 395 | SOCK_STREAM, error); |
| 396 | } |
| 397 | } |
| 398 | |
Josh Gao | ab9958e | 2018-12-13 14:04:04 -0800 | [diff] [blame] | 399 | *error = "unknown socket specification:"; |
| 400 | *error += spec; |
Josh Gao | 4a5a95d | 2016-08-24 18:38:44 -0700 | [diff] [blame] | 401 | return -1; |
| 402 | } |