Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
Yabin Cui | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG ADB |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 21 | #if defined(__BIONIC__) |
Josh Gao | 41da100 | 2018-07-25 18:13:44 -0700 | [diff] [blame] | 22 | #include <android/fdsan.h> |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 23 | #endif |
| 24 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 25 | #include <errno.h> |
Josh Gao | 358aca2 | 2018-04-04 17:53:54 -0700 | [diff] [blame] | 26 | #include <getopt.h> |
| 27 | #include <malloc.h> |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 28 | #include <signal.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
Luis Hector Chavez | e4528e1 | 2018-04-04 15:59:59 -0700 | [diff] [blame] | 31 | #include <sys/capability.h> |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 32 | #include <sys/prctl.h> |
| 33 | |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 34 | #include <memory> |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 35 | #include <vector> |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 36 | |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 37 | #include <android-base/logging.h> |
Jorge Lucangeli Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 38 | #include <android-base/macros.h> |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 39 | #include <android-base/properties.h> |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 40 | #include <android-base/stringprintf.h> |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 41 | #include <android-base/strings.h> |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 42 | |
| 43 | #if defined(__ANDROID__) |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 44 | #include <libminijail.h> |
Steven Moreland | b087d30 | 2017-04-13 23:48:57 -0700 | [diff] [blame] | 45 | #include <log/log_properties.h> |
Jorge Lucangeli Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 46 | #include <scoped_minijail.h> |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 47 | |
Mark Salyzyn | c75f65f | 2016-03-28 15:52:13 -0700 | [diff] [blame] | 48 | #include <private/android_filesystem_config.h> |
Tom Cherry | 4684522 | 2015-12-11 14:28:09 -0800 | [diff] [blame] | 49 | #include "selinux/android.h" |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 50 | #endif |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 51 | |
| 52 | #include "adb.h" |
| 53 | #include "adb_auth.h" |
| 54 | #include "adb_listeners.h" |
Josh Gao | 1eef478 | 2015-11-20 15:37:31 -0800 | [diff] [blame] | 55 | #include "adb_utils.h" |
Joshua Duong | 290ccb5 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 56 | #include "adb_wifi.h" |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 57 | #include "socket_spec.h" |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 58 | #include "transport.h" |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 59 | |
Josh Gao | 2dc6196 | 2021-02-03 22:11:45 -0800 | [diff] [blame] | 60 | #include "daemon/mdns.h" |
| 61 | #include "daemon/watchdog.h" |
Casey Dahlin | 10ad15f | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 62 | |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 63 | #if defined(__ANDROID__) |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 64 | static const char* root_seclabel = nullptr; |
| 65 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 66 | static bool should_drop_privileges() { |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 67 | // The properties that affect `adb root` and `adb unroot` are ro.secure and |
| 68 | // ro.debuggable. In this context the names don't make the expected behavior |
| 69 | // particularly obvious. |
| 70 | // |
| 71 | // ro.debuggable: |
| 72 | // Allowed to become root, but not necessarily the default. Set to 1 on |
| 73 | // eng and userdebug builds. |
| 74 | // |
| 75 | // ro.secure: |
| 76 | // Drop privileges by default. Set to 1 on userdebug and user builds. |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 77 | bool ro_secure = android::base::GetBoolProperty("ro.secure", true); |
Mark Salyzyn | c75f65f | 2016-03-28 15:52:13 -0700 | [diff] [blame] | 78 | bool ro_debuggable = __android_log_is_debuggable(); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 79 | |
| 80 | // Drop privileges if ro.secure is set... |
| 81 | bool drop = ro_secure; |
| 82 | |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 83 | // ... except "adb root" lets you keep privileges in a debuggable build. |
Elliott Hughes | 8b249d2 | 2016-09-23 15:40:03 -0700 | [diff] [blame] | 84 | std::string prop = android::base::GetProperty("service.adb.root", ""); |
| 85 | bool adb_root = (prop == "1"); |
| 86 | bool adb_unroot = (prop == "0"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 87 | if (ro_debuggable && adb_root) { |
| 88 | drop = false; |
| 89 | } |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 90 | // ... and "adb unroot" lets you explicitly drop privileges. |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 91 | if (adb_unroot) { |
| 92 | drop = true; |
| 93 | } |
| 94 | |
| 95 | return drop; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 98 | static void drop_privileges(int server_port) { |
Jorge Lucangeli Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 99 | ScopedMinijail jail(minijail_new()); |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 100 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 101 | // Add extra groups: |
| 102 | // AID_ADB to access the USB driver |
| 103 | // AID_LOG to read system logs (adb logcat) |
| 104 | // AID_INPUT to diagnose input issues (getevent) |
| 105 | // AID_INET to diagnose network issues (ping) |
| 106 | // AID_NET_BT and AID_NET_BT_ADMIN to diagnose bluetooth (hcidump) |
| 107 | // AID_SDCARD_R to allow reading from the SD card |
| 108 | // AID_SDCARD_RW to allow writing to the SD card |
| 109 | // AID_NET_BW_STATS to read out qtaguid statistics |
Nick Kralevich | 8a1b4b3 | 2015-11-07 16:52:17 -0800 | [diff] [blame] | 110 | // AID_READPROC for reading /proc entries across UID boundaries |
Siarhei Vishniakou | 209c27b | 2017-05-08 15:50:55 -0700 | [diff] [blame] | 111 | // AID_UHID for using 'hid' command to read/write to /dev/uhid |
Martijn Coenen | 223bcc7 | 2020-08-18 15:34:15 +0200 | [diff] [blame] | 112 | // AID_EXT_DATA_RW for writing to /sdcard/Android/data (devices without sdcardfs) |
| 113 | // AID_EXT_OBB_RW for writing to /sdcard/Android/obb (devices without sdcardfs) |
Kalesh Singh | 7cb519d | 2021-12-06 16:19:58 -0800 | [diff] [blame] | 114 | // AID_READTRACEFS for reading tracefs entries |
Siarhei Vishniakou | 209c27b | 2017-05-08 15:50:55 -0700 | [diff] [blame] | 115 | gid_t groups[] = {AID_ADB, AID_LOG, AID_INPUT, AID_INET, |
| 116 | AID_NET_BT, AID_NET_BT_ADMIN, AID_SDCARD_R, AID_SDCARD_RW, |
Martijn Coenen | 223bcc7 | 2020-08-18 15:34:15 +0200 | [diff] [blame] | 117 | AID_NET_BW_STATS, AID_READPROC, AID_UHID, AID_EXT_DATA_RW, |
Kalesh Singh | 7cb519d | 2021-12-06 16:19:58 -0800 | [diff] [blame] | 118 | AID_EXT_OBB_RW, AID_READTRACEFS}; |
Jorge Lucangeli Obes | bd75c67 | 2016-07-18 13:46:42 -0400 | [diff] [blame] | 119 | minijail_set_supplementary_gids(jail.get(), arraysize(groups), groups); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 120 | |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 121 | // Don't listen on a port (default 5037) if running in secure mode. |
| 122 | // Don't run as root if running in secure mode. |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 123 | if (should_drop_privileges()) { |
Josh Gao | c5ca638 | 2020-06-04 17:58:48 -0700 | [diff] [blame] | 124 | const bool should_drop_caps = !__android_log_is_debuggable(); |
Luis Hector Chavez | e4528e1 | 2018-04-04 15:59:59 -0700 | [diff] [blame] | 125 | |
| 126 | if (should_drop_caps) { |
| 127 | minijail_use_caps(jail.get(), CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID)); |
| 128 | } |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 129 | |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 130 | minijail_change_gid(jail.get(), AID_SHELL); |
| 131 | minijail_change_uid(jail.get(), AID_SHELL); |
| 132 | // minijail_enter() will abort if any priv-dropping step fails. |
| 133 | minijail_enter(jail.get()); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 134 | |
Luis Hector Chavez | e4528e1 | 2018-04-04 15:59:59 -0700 | [diff] [blame] | 135 | // Whenever ambient capabilities are being used, minijail cannot |
| 136 | // simultaneously drop the bounding capability set to just |
| 137 | // CAP_SETUID|CAP_SETGID while clearing the inheritable, effective, |
| 138 | // and permitted sets. So we need to do that in two steps. |
| 139 | using ScopedCaps = |
| 140 | std::unique_ptr<std::remove_pointer<cap_t>::type, std::function<void(cap_t)>>; |
| 141 | ScopedCaps caps(cap_get_proc(), &cap_free); |
| 142 | if (cap_clear_flag(caps.get(), CAP_INHERITABLE) == -1) { |
| 143 | PLOG(FATAL) << "cap_clear_flag(INHERITABLE) failed"; |
| 144 | } |
| 145 | if (cap_clear_flag(caps.get(), CAP_EFFECTIVE) == -1) { |
| 146 | PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed"; |
| 147 | } |
| 148 | if (cap_clear_flag(caps.get(), CAP_PERMITTED) == -1) { |
| 149 | PLOG(FATAL) << "cap_clear_flag(PEMITTED) failed"; |
| 150 | } |
| 151 | if (cap_set_proc(caps.get()) != 0) { |
| 152 | PLOG(FATAL) << "cap_set_proc() failed"; |
| 153 | } |
| 154 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 155 | D("Local port disabled"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 156 | } else { |
Jorge Lucangeli Obes | 83e2851 | 2015-12-14 13:18:57 -0800 | [diff] [blame] | 157 | // minijail_enter() will abort if any priv-dropping step fails. |
| 158 | minijail_enter(jail.get()); |
| 159 | |
Nick Kralevich | 11f73a1 | 2015-06-12 22:03:50 -0700 | [diff] [blame] | 160 | if (root_seclabel != nullptr) { |
Tom Cherry | 4684522 | 2015-12-11 14:28:09 -0800 | [diff] [blame] | 161 | if (selinux_android_setcon(root_seclabel) < 0) { |
Jorge Lucangeli Obes | 116b8b9 | 2015-11-11 11:33:19 -0800 | [diff] [blame] | 162 | LOG(FATAL) << "Could not set SELinux context"; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 165 | } |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 166 | } |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 167 | #endif |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 168 | |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 169 | static void setup_adb(const std::vector<std::string>& addrs) { |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 170 | #if defined(__ANDROID__) |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 171 | // Get the first valid port from addrs and setup mDNS. |
| 172 | int port = -1; |
| 173 | std::string error; |
| 174 | for (const auto& addr : addrs) { |
| 175 | port = get_host_socket_spec_port(addr, &error); |
| 176 | if (port != -1) { |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | if (port == -1) { |
| 181 | port = DEFAULT_ADB_LOCAL_TRANSPORT_PORT; |
| 182 | } |
Joshua Duong | 290ccb5 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 183 | LOG(INFO) << "Setup mdns on port= " << port; |
Casey Dahlin | 10ad15f | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 184 | setup_mdns(port); |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 185 | #endif |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 186 | for (const auto& addr : addrs) { |
| 187 | LOG(INFO) << "adbd listening on " << addr; |
| 188 | local_init(addr); |
| 189 | } |
Casey Dahlin | 10ad15f | 2016-05-06 16:19:13 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 192 | int adbd_main(int server_port) { |
| 193 | umask(0); |
| 194 | |
| 195 | signal(SIGPIPE, SIG_IGN); |
| 196 | |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 197 | #if defined(__BIONIC__) |
Josh Gao | 41da100 | 2018-07-25 18:13:44 -0700 | [diff] [blame] | 198 | auto fdsan_level = android_fdsan_get_error_level(); |
| 199 | if (fdsan_level == ANDROID_FDSAN_ERROR_LEVEL_DISABLED) { |
| 200 | android_fdsan_set_error_level(ANDROID_FDSAN_ERROR_LEVEL_WARN_ONCE); |
| 201 | } |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 202 | #endif |
Josh Gao | 41da100 | 2018-07-25 18:13:44 -0700 | [diff] [blame] | 203 | |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 204 | init_transport_registration(); |
| 205 | |
| 206 | // We need to call this even if auth isn't enabled because the file |
| 207 | // descriptor will always be open. |
| 208 | adbd_cloexec_auth_socket(); |
| 209 | |
Josh Gao | c5ca638 | 2020-06-04 17:58:48 -0700 | [diff] [blame] | 210 | #if defined(__ANDROID__) |
Shaju Mathew | 390e67a | 2021-10-15 12:01:49 -0700 | [diff] [blame] | 211 | bool device_unlocked = android::base::GetProperty("ro.boot.verifiedbootstate", "") == "orange"; |
| 212 | if (device_unlocked || __android_log_is_debuggable()) { |
Shaju Mathew | 390e67a | 2021-10-15 12:01:49 -0700 | [diff] [blame] | 213 | // If we're on userdebug/eng or the device is unlocked, permit no-authentication. |
Bowgo Tsai | 35b817b | 2019-03-12 04:25:33 +0800 | [diff] [blame] | 214 | auth_required = android::base::GetBoolProperty("ro.adb.secure", false); |
LuK1337 | 71002e9 | 2020-04-12 19:04:41 +0200 | [diff] [blame] | 215 | #if defined(__ANDROID_RECOVERY__) |
| 216 | auth_required &= android::base::GetBoolProperty("ro.adb.secure.recovery", true); |
Shaju Mathew | 390e67a | 2021-10-15 12:01:49 -0700 | [diff] [blame] | 217 | #endif |
Bowgo Tsai | 35b817b | 2019-03-12 04:25:33 +0800 | [diff] [blame] | 218 | } |
Josh Gao | 361148b | 2018-01-02 12:01:43 -0800 | [diff] [blame] | 219 | #endif |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 220 | |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 221 | // Our external storage path may be different than apps, since |
| 222 | // we aren't able to bind mount after dropping root. |
| 223 | const char* adb_external_storage = getenv("ADB_EXTERNAL_STORAGE"); |
| 224 | if (adb_external_storage != nullptr) { |
| 225 | setenv("EXTERNAL_STORAGE", adb_external_storage, 1); |
| 226 | } else { |
| 227 | D("Warning: ADB_EXTERNAL_STORAGE is not set. Leaving EXTERNAL_STORAGE" |
| 228 | " unchanged.\n"); |
| 229 | } |
| 230 | |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 231 | #if defined(__ANDROID__) |
Mike Frysinger | 2e11407 | 2015-12-08 18:57:47 -0500 | [diff] [blame] | 232 | drop_privileges(server_port); |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 233 | #endif |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 234 | |
Josh Gao | 2dc6196 | 2021-02-03 22:11:45 -0800 | [diff] [blame] | 235 | #if defined(__ANDROID__) |
| 236 | // A thread gets spawned as a side-effect of initializing the watchdog, so it needs to happen |
| 237 | // after we drop privileges. |
| 238 | watchdog::Initialize(); |
| 239 | #endif |
| 240 | |
Josh Gao | 7cac88a | 2019-10-22 12:30:39 -0700 | [diff] [blame] | 241 | // adbd_auth_init will spawn a thread, so we need to defer it until after selinux transitions. |
| 242 | adbd_auth_init(); |
| 243 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 244 | bool is_usb = false; |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 245 | |
| 246 | #if defined(__ANDROID__) |
Josh Gao | 0dde9c8 | 2017-01-11 14:39:19 -0800 | [diff] [blame] | 247 | if (access(USB_FFS_ADB_EP0, F_OK) == 0) { |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 248 | // Listen on USB. |
| 249 | usb_init(); |
| 250 | is_usb = true; |
| 251 | } |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 252 | #endif |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 253 | |
| 254 | // If one of these properties is set, also listen on that port. |
| 255 | // If one of the properties isn't set and we couldn't listen on usb, listen |
| 256 | // on the default port. |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 257 | std::vector<std::string> addrs; |
| 258 | std::string prop_addr = android::base::GetProperty("service.adb.listen_addrs", ""); |
| 259 | if (prop_addr.empty()) { |
| 260 | std::string prop_port = android::base::GetProperty("service.adb.tcp.port", ""); |
| 261 | if (prop_port.empty()) { |
| 262 | prop_port = android::base::GetProperty("persist.adb.tcp.port", ""); |
| 263 | } |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 264 | |
Josh Gao | 79d122a | 2019-09-17 17:34:41 +0800 | [diff] [blame] | 265 | #if !defined(__ANDROID__) |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 266 | if (prop_port.empty() && getenv("ADBD_PORT")) { |
| 267 | prop_port = getenv("ADBD_PORT"); |
| 268 | } |
Josh Gao | 79d122a | 2019-09-17 17:34:41 +0800 | [diff] [blame] | 269 | #endif |
| 270 | |
Jason Jeremy Iman | 8461387 | 2019-07-19 12:44:39 +0900 | [diff] [blame] | 271 | int port; |
| 272 | if (sscanf(prop_port.c_str(), "%d", &port) == 1 && port > 0) { |
| 273 | D("using tcp port=%d", port); |
| 274 | // Listen on TCP and VSOCK port specified by service.adb.tcp.port property. |
| 275 | addrs.push_back(android::base::StringPrintf("tcp:%d", port)); |
| 276 | addrs.push_back(android::base::StringPrintf("vsock:%d", port)); |
| 277 | setup_adb(addrs); |
| 278 | } else if (!is_usb) { |
| 279 | // Listen on default port. |
| 280 | addrs.push_back( |
| 281 | android::base::StringPrintf("tcp:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT)); |
| 282 | addrs.push_back( |
| 283 | android::base::StringPrintf("vsock:%d", DEFAULT_ADB_LOCAL_TRANSPORT_PORT)); |
| 284 | setup_adb(addrs); |
| 285 | } |
| 286 | } else { |
| 287 | addrs = android::base::Split(prop_addr, ","); |
| 288 | setup_adb(addrs); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Josh Gao | b2bcd75 | 2020-04-20 16:45:55 -0700 | [diff] [blame] | 291 | LOG(INFO) << "adbd started"; |
| 292 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 293 | D("adbd_main(): pre init_jdwp()"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 294 | init_jdwp(); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 295 | D("adbd_main(): post init_jdwp()"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 296 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 297 | D("Event loop starting"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 298 | fdevent_loop(); |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 303 | int main(int argc, char** argv) { |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 304 | #if defined(__BIONIC__) |
Josh Gao | 358aca2 | 2018-04-04 17:53:54 -0700 | [diff] [blame] | 305 | // Set M_DECAY_TIME so that our allocations aren't immediately purged on free. |
| 306 | mallopt(M_DECAY_TIME, 1); |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 307 | #endif |
Josh Gao | 358aca2 | 2018-04-04 17:53:54 -0700 | [diff] [blame] | 308 | |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 309 | while (true) { |
| 310 | static struct option opts[] = { |
Joshua Duong | 290ccb5 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 311 | {"root_seclabel", required_argument, nullptr, 's'}, |
| 312 | {"device_banner", required_argument, nullptr, 'b'}, |
| 313 | {"version", no_argument, nullptr, 'v'}, |
| 314 | {"logpostfsdata", no_argument, nullptr, 'l'}, |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 315 | }; |
| 316 | |
| 317 | int option_index = 0; |
| 318 | int c = getopt_long(argc, argv, "", opts, &option_index); |
| 319 | if (c == -1) { |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | switch (c) { |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 324 | #if defined(__ANDROID__) |
| 325 | case 's': |
| 326 | root_seclabel = optarg; |
| 327 | break; |
| 328 | #endif |
| 329 | case 'b': |
| 330 | adb_device_banner = optarg; |
| 331 | break; |
| 332 | case 'v': |
| 333 | printf("Android Debug Bridge Daemon version %d.%d.%d\n", ADB_VERSION_MAJOR, |
| 334 | ADB_VERSION_MINOR, ADB_SERVER_VERSION); |
| 335 | return 0; |
Joshua Duong | 290ccb5 | 2019-11-20 14:18:43 -0800 | [diff] [blame] | 336 | case 'l': |
| 337 | LOG(ERROR) << "post-fs-data triggered"; |
| 338 | return 0; |
Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 339 | default: |
| 340 | // getopt already prints "adbd: invalid option -- %c" for us. |
| 341 | return 1; |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | close_stdin(); |
| 346 | |
Dan Albert | 08d552b | 2015-05-21 13:58:50 -0700 | [diff] [blame] | 347 | adb_trace_init(argv); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 348 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 349 | D("Handling main()"); |
Dan Albert | 53a3744 | 2015-05-08 16:13:53 -0700 | [diff] [blame] | 350 | return adbd_main(DEFAULT_ADB_PORT); |
| 351 | } |