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