blob: c942c1f230235d4fcea493f79e73082d4de7320e [file] [log] [blame]
Josh Gao0560feb2019-01-22 19:36:15 -08001/*
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
LuK133768fc4872020-09-16 19:35:05 +020019#if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__)
20#include <aidl/android/adbroot/IADBRootService.h>
21#include <android/binder_manager.h>
22#endif
23
Josh Gao0560feb2019-01-22 19:36:15 -080024#include "sysdeps.h"
25
26#include <unistd.h>
27
Josh Gao76730d22019-02-15 15:14:11 -080028#include <android-base/logging.h>
Josh Gao0560feb2019-01-22 19:36:15 -080029#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
36void restart_root_service(unique_fd fd) {
37 if (getuid() == 0) {
38 WriteFdExactly(fd.get(), "adbd is already running as root\n");
39 return;
40 }
LuK133768fc4872020-09-16 19:35:05 +020041
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 Gao0560feb2019-01-22 19:36:15 -080056 if (!__android_log_is_debuggable()) {
57 WriteFdExactly(fd.get(), "adbd cannot run as root in production builds\n");
58 return;
59 }
LuK133768fc4872020-09-16 19:35:05 +020060#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 Gao0560feb2019-01-22 19:36:15 -080068
Josh Gao76730d22019-02-15 15:14:11 -080069 LOG(INFO) << "adbd restarting as root";
Josh Gao0560feb2019-01-22 19:36:15 -080070 android::base::SetProperty("service.adb.root", "1");
71 WriteFdExactly(fd.get(), "restarting adbd as root\n");
72}
73
74void restart_unroot_service(unique_fd fd) {
75 if (getuid() != 0) {
76 WriteFdExactly(fd.get(), "adbd not running as root\n");
77 return;
78 }
Josh Gao76730d22019-02-15 15:14:11 -080079
80 LOG(INFO) << "adbd restarting as nonroot";
Josh Gao0560feb2019-01-22 19:36:15 -080081 android::base::SetProperty("service.adb.root", "0");
82 WriteFdExactly(fd.get(), "restarting adbd as non root\n");
83}
84
85void 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 Gao76730d22019-02-15 15:14:11 -080091 LOG(INFO) << "adbd restarting in TCP mode (port = " << port << ")";
Josh Gao0560feb2019-01-22 19:36:15 -080092 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
96void restart_usb_service(unique_fd fd) {
Josh Gao76730d22019-02-15 15:14:11 -080097 LOG(INFO) << "adbd restarting in USB mode";
Josh Gao0560feb2019-01-22 19:36:15 -080098 android::base::SetProperty("service.adb.tcp.port", "0");
99 WriteFdExactly(fd.get(), "restarting in USB mode\n");
100}