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