blob: e31d2fd3f30f1780553d4596f9d5a2a1f567c982 [file] [log] [blame]
Dan Albert9a894b72015-02-18 18:36:08 -08001/*
2 * Copyright (C) 2015 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 <errno.h>
xunchang34690ce2019-04-05 16:16:07 -070018#include <fcntl.h>
Dan Albert9a894b72015-02-18 18:36:08 -080019#include <signal.h>
20#include <stdio.h>
21#include <stdlib.h>
xunchang34690ce2019-04-05 16:16:07 -070022#include <strings.h>
23
24#include <android-base/logging.h>
25#include <android-base/parseint.h>
Yumi Yukimura8e7c1632024-12-19 21:41:28 +080026#include <android-base/properties.h>
27#include <android-base/strings.h>
Dan Albert9a894b72015-02-18 18:36:08 -080028
Dan Albert8f6eb5c2015-02-24 22:07:18 -080029#include "adb.h"
Elliott Hughes9813f5b2015-06-23 11:12:58 -070030#include "adb_auth.h"
Dan Albert8f6eb5c2015-02-24 22:07:18 -080031#include "transport.h"
32
Tao Bao3305d482019-09-26 00:02:29 -070033#include "minadbd/types.h"
xunchang34690ce2019-04-05 16:16:07 -070034#include "minadbd_services.h"
Dan Albertf3a57262015-02-19 13:21:14 -080035
Tao Baoc6dc3252019-04-16 14:22:25 -070036using namespace std::string_literals;
37
Yumi Yukimura8e7c1632024-12-19 21:41:28 +080038static void minadbd_net_init() {
39 std::string prop_port = android::base::GetProperty("service.adb.tcp.port", "");
40
41 int port;
42 if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) {
43 LOG(DEBUG) << "using tcp port=" << std::to_string(port);
44 // Listen on TCP and VSOCK port specified by service.adb.tcp.port property.
45 local_init(android::base::StringPrintf("tcp:%d", port));
46 local_init(android::base::StringPrintf("vsock:%d", port));
47 } else {
48 // Listen on default port.
49 local_init(android::base::StringPrintf("tcp:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
50 local_init(android::base::StringPrintf("vsock:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT));
51 }
52}
53
xunchang34690ce2019-04-05 16:16:07 -070054int main(int argc, char** argv) {
55 android::base::InitLogging(argv, &android::base::StderrLogger);
56 // TODO(xunchang) implement a command parser
Tao Baoc6dc3252019-04-16 14:22:25 -070057 if ((argc != 3 && argc != 4) || argv[1] != "--socket_fd"s ||
58 (argc == 4 && argv[3] != "--rescue"s)) {
xunchang34690ce2019-04-05 16:16:07 -070059 LOG(ERROR) << "minadbd has invalid arguments, argc: " << argc;
60 exit(kMinadbdArgumentsParsingError);
61 }
Dan Albert9a894b72015-02-18 18:36:08 -080062
xunchang34690ce2019-04-05 16:16:07 -070063 int socket_fd;
64 if (!android::base::ParseInt(argv[2], &socket_fd)) {
65 LOG(ERROR) << "Failed to parse int in " << argv[2];
66 exit(kMinadbdArgumentsParsingError);
67 }
68 if (fcntl(socket_fd, F_GETFD, 0) == -1) {
69 PLOG(ERROR) << "Failed to get minadbd socket";
70 exit(kMinadbdSocketIOError);
71 }
72 SetMinadbdSocketFd(socket_fd);
Elliott Hughes9813f5b2015-06-23 11:12:58 -070073
Tao Baoc6dc3252019-04-16 14:22:25 -070074 if (argc == 4) {
75 SetMinadbdRescueMode(true);
76 adb_device_banner = "rescue";
77 } else {
78 adb_device_banner = "sideload";
79 }
Dan Albert9a894b72015-02-18 18:36:08 -080080
xunchang34690ce2019-04-05 16:16:07 -070081 signal(SIGPIPE, SIG_IGN);
Dan Albert9a894b72015-02-18 18:36:08 -080082
xunchang34690ce2019-04-05 16:16:07 -070083 // We can't require authentication for sideloading. http://b/22025550.
84 auth_required = false;
Shaju Mathew1b3dcb82021-11-28 19:31:16 -080085 socket_access_allowed = false;
xunchang34690ce2019-04-05 16:16:07 -070086
xunchang34690ce2019-04-05 16:16:07 -070087 usb_init();
88
Yumi Yukimura8e7c1632024-12-19 21:41:28 +080089 minadbd_net_init();
90
xunchang34690ce2019-04-05 16:16:07 -070091 VLOG(ADB) << "Event loop starting";
92 fdevent_loop();
93
94 return 0;
Dan Albert9a894b72015-02-18 18:36:08 -080095}