| Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #define TRACE_TAG SERVICES |
| 18 | |
| LuK1337 | 68fc487 | 2020-09-16 19:35:05 +0200 | [diff] [blame] | 19 | #if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__) |
| 20 | #include <aidl/android/adbroot/IADBRootService.h> |
| 21 | #include <android/binder_manager.h> |
| 22 | #endif |
| 23 | |
| Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 24 | #include "sysdeps.h" |
| 25 | |
| 26 | #include <unistd.h> |
| 27 | |
| Josh Gao | 76730d2 | 2019-02-15 15:14:11 -0800 | [diff] [blame] | 28 | #include <android-base/logging.h> |
| Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 29 | #include <android-base/properties.h> |
| 30 | #include <android-base/stringprintf.h> |
| 31 | #include <log/log_properties.h> |
| 32 | |
| 33 | #include "adb_io.h" |
| 34 | #include "adb_unique_fd.h" |
| 35 | |
| 36 | void restart_root_service(unique_fd fd) { |
| 37 | if (getuid() == 0) { |
| 38 | WriteFdExactly(fd.get(), "adbd is already running as root\n"); |
| 39 | return; |
| 40 | } |
| LuK1337 | 68fc487 | 2020-09-16 19:35:05 +0200 | [diff] [blame] | 41 | |
| 42 | #if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__) |
| 43 | ndk::SpAIBinder binder = ndk::SpAIBinder(AServiceManager_getService("adbroot_service")); |
| 44 | std::shared_ptr<aidl::android::adbroot::IADBRootService> service = |
| 45 | aidl::android::adbroot::IADBRootService::fromBinder(binder); |
| 46 | if (!service) { |
| 47 | LOG(ERROR) << "Failed to get adbroot_service interface"; |
| 48 | return; |
| 49 | } |
| 50 | #endif |
| 51 | |
| 52 | #if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__) |
| 53 | bool enabled = false; |
| 54 | if (auto status = service->getEnabled(&enabled); !status.isOk()) { |
| 55 | #endif |
| Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 56 | if (!__android_log_is_debuggable()) { |
| 57 | WriteFdExactly(fd.get(), "adbd cannot run as root in production builds\n"); |
| 58 | return; |
| 59 | } |
| LuK1337 | 68fc487 | 2020-09-16 19:35:05 +0200 | [diff] [blame] | 60 | #if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__) |
| 61 | } |
| 62 | if (!enabled) { |
| 63 | WriteFdExactly(fd, "ADB Root access is disabled by system setting - " |
| 64 | "enable in Settings -> System -> Developer options\n"); |
| 65 | return; |
| 66 | } |
| 67 | #endif |
| Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 68 | |
| Josh Gao | 76730d2 | 2019-02-15 15:14:11 -0800 | [diff] [blame] | 69 | LOG(INFO) << "adbd restarting as root"; |
| Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 70 | android::base::SetProperty("service.adb.root", "1"); |
| 71 | WriteFdExactly(fd.get(), "restarting adbd as root\n"); |
| 72 | } |
| 73 | |
| 74 | void restart_unroot_service(unique_fd fd) { |
| 75 | if (getuid() != 0) { |
| 76 | WriteFdExactly(fd.get(), "adbd not running as root\n"); |
| 77 | return; |
| 78 | } |
| Josh Gao | 76730d2 | 2019-02-15 15:14:11 -0800 | [diff] [blame] | 79 | |
| 80 | LOG(INFO) << "adbd restarting as nonroot"; |
| Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 81 | android::base::SetProperty("service.adb.root", "0"); |
| 82 | WriteFdExactly(fd.get(), "restarting adbd as non root\n"); |
| 83 | } |
| 84 | |
| 85 | void restart_tcp_service(unique_fd fd, int port) { |
| 86 | if (port <= 0) { |
| 87 | WriteFdFmt(fd.get(), "invalid port %d\n", port); |
| 88 | return; |
| 89 | } |
| 90 | |
| Josh Gao | 76730d2 | 2019-02-15 15:14:11 -0800 | [diff] [blame] | 91 | LOG(INFO) << "adbd restarting in TCP mode (port = " << port << ")"; |
| Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 92 | android::base::SetProperty("service.adb.tcp.port", android::base::StringPrintf("%d", port)); |
| 93 | WriteFdFmt(fd.get(), "restarting in TCP mode port: %d\n", port); |
| 94 | } |
| 95 | |
| 96 | void restart_usb_service(unique_fd fd) { |
| Josh Gao | 76730d2 | 2019-02-15 15:14:11 -0800 | [diff] [blame] | 97 | LOG(INFO) << "adbd restarting in USB mode"; |
| Josh Gao | 0560feb | 2019-01-22 19:36:15 -0800 | [diff] [blame] | 98 | android::base::SetProperty("service.adb.tcp.port", "0"); |
| 99 | WriteFdExactly(fd.get(), "restarting in USB mode\n"); |
| 100 | } |