adb: split daemon services out of service_to_fd.
Let minadbd override daemon services by pulling out the daemon services
into a separate translation unit, that isn't compiled into libadbd.
Bug: http://b/111831478
Test: mma
Change-Id: Iecfebea371e03864108eca9a4d2e9cfd5db34749
diff --git a/services.cpp b/services.cpp
index 639bb46..4b033bd 100644
--- a/services.cpp
+++ b/services.cpp
@@ -24,39 +24,15 @@
#include <stdlib.h>
#include <string.h>
-#ifndef _WIN32
-#include <netdb.h>
-#include <netinet/in.h>
-#include <sys/ioctl.h>
-#include <unistd.h>
-#endif
-
#include <thread>
-
-#include <android-base/file.h>
-#include <android-base/parsenetaddress.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <cutils/sockets.h>
-#if !ADB_HOST
-#include <android-base/properties.h>
-#include <bootloader_message/bootloader_message.h>
-#include <cutils/android_reboot.h>
-#include <log/log_properties.h>
-#endif
-
#include "adb.h"
#include "adb_io.h"
#include "adb_unique_fd.h"
#include "adb_utils.h"
-#if !ADB_HOST
-#include "daemon/file_sync_service.h"
-#include "daemon/framebuffer_service.h"
-#include "daemon/remount_service.h"
-#include "daemon/set_verity_enable_state_service.h"
-#include "daemon/shell_service.h"
-#endif
#include "services.h"
#include "socket_spec.h"
#include "sysdeps.h"
@@ -70,157 +46,7 @@
func(std::move(fd));
}
-#if !ADB_HOST
-
-void restart_root_service(unique_fd fd) {
- if (getuid() == 0) {
- WriteFdExactly(fd.get(), "adbd is already running as root\n");
- return;
- }
- if (!__android_log_is_debuggable()) {
- WriteFdExactly(fd.get(), "adbd cannot run as root in production builds\n");
- return;
- }
-
- android::base::SetProperty("service.adb.root", "1");
- WriteFdExactly(fd.get(), "restarting adbd as root\n");
-}
-
-void restart_unroot_service(unique_fd fd) {
- if (getuid() != 0) {
- WriteFdExactly(fd.get(), "adbd not running as root\n");
- return;
- }
- android::base::SetProperty("service.adb.root", "0");
- WriteFdExactly(fd.get(), "restarting adbd as non root\n");
-}
-
-void restart_tcp_service(unique_fd fd, int port) {
- if (port <= 0) {
- WriteFdFmt(fd.get(), "invalid port %d\n", port);
- return;
- }
-
- android::base::SetProperty("service.adb.tcp.port", android::base::StringPrintf("%d", port));
- WriteFdFmt(fd.get(), "restarting in TCP mode port: %d\n", port);
-}
-
-void restart_usb_service(unique_fd fd) {
- android::base::SetProperty("service.adb.tcp.port", "0");
- WriteFdExactly(fd.get(), "restarting in USB mode\n");
-}
-
-bool reboot_service_impl(unique_fd fd, const std::string& arg) {
- std::string reboot_arg = arg;
- bool auto_reboot = false;
-
- if (reboot_arg == "sideload-auto-reboot") {
- auto_reboot = true;
- reboot_arg = "sideload";
- }
-
- // It reboots into sideload mode by setting "--sideload" or "--sideload_auto_reboot"
- // in the command file.
- if (reboot_arg == "sideload") {
- if (getuid() != 0) {
- WriteFdExactly(fd, "'adb root' is required for 'adb reboot sideload'.\n");
- return false;
- }
-
- const std::vector<std::string> options = {
- auto_reboot ? "--sideload_auto_reboot" : "--sideload"
- };
- std::string err;
- if (!write_bootloader_message(options, &err)) {
- D("Failed to set bootloader message: %s", err.c_str());
- return false;
- }
-
- reboot_arg = "recovery";
- }
-
- sync();
-
- if (reboot_arg.empty()) reboot_arg = "adb";
- std::string reboot_string = android::base::StringPrintf("reboot,%s", reboot_arg.c_str());
- if (!android::base::SetProperty(ANDROID_RB_PROPERTY, reboot_string)) {
- WriteFdFmt(fd, "reboot (%s) failed\n", reboot_string.c_str());
- return false;
- }
-
- return true;
-}
-
-void reboot_service(unique_fd fd, const std::string& arg) {
- if (!reboot_service_impl(std::move(fd), arg)) {
- return;
- }
- // Don't return early. Give the reboot command time to take effect
- // to avoid messing up scripts which do "adb reboot && adb wait-for-device"
- while (true) {
- pause();
- }
-}
-
-void reconnect_service(unique_fd fd, atransport* t) {
- WriteFdExactly(fd, "done");
- kick_transport(t);
-}
-
-int reverse_service(const char* command, atransport* transport) {
- int s[2];
- if (adb_socketpair(s)) {
- PLOG(ERROR) << "cannot create service socket pair.";
- return -1;
- }
- VLOG(SERVICES) << "service socketpair: " << s[0] << ", " << s[1];
- if (handle_forward_request(command, transport, s[1]) < 0) {
- SendFail(s[1], "not a reverse forwarding command");
- }
- adb_close(s[1]);
- return s[0];
-}
-
-// Shell service string can look like:
-// shell[,arg1,arg2,...]:[command]
-int ShellService(const std::string& args, const atransport* transport) {
- size_t delimiter_index = args.find(':');
- if (delimiter_index == std::string::npos) {
- LOG(ERROR) << "No ':' found in shell service arguments: " << args;
- return -1;
- }
-
- const std::string service_args = args.substr(0, delimiter_index);
- const std::string command = args.substr(delimiter_index + 1);
-
- // Defaults:
- // PTY for interactive, raw for non-interactive.
- // No protocol.
- // $TERM set to "dumb".
- SubprocessType type(command.empty() ? SubprocessType::kPty
- : SubprocessType::kRaw);
- SubprocessProtocol protocol = SubprocessProtocol::kNone;
- std::string terminal_type = "dumb";
-
- for (const std::string& arg : android::base::Split(service_args, ",")) {
- if (arg == kShellServiceArgRaw) {
- type = SubprocessType::kRaw;
- } else if (arg == kShellServiceArgPty) {
- type = SubprocessType::kPty;
- } else if (arg == kShellServiceArgShellProtocol) {
- protocol = SubprocessProtocol::kShell;
- } else if (android::base::StartsWith(arg, "TERM=")) {
- terminal_type = arg.substr(5);
- } else if (!arg.empty()) {
- // This is not an error to allow for future expansion.
- LOG(WARNING) << "Ignoring unknown shell service argument: " << arg;
- }
- }
-
- return StartSubprocess(command.c_str(), terminal_type.c_str(), type, protocol);
-}
-
-#endif // !ADB_HOST
+} // namespace
unique_fd create_service_thread(const char* service_name, std::function<void(unique_fd)> func) {
int s[2];
@@ -245,8 +71,6 @@
return unique_fd(s[0]);
}
-} // namespace
-
int service_to_fd(const char* name, atransport* transport) {
int ret = -1;
@@ -256,65 +80,12 @@
if (ret < 0) {
LOG(ERROR) << "failed to connect to socket '" << name << "': " << error;
}
+ } else {
#if !ADB_HOST
- } else if(!strncmp("dev:", name, 4)) {
- ret = unix_open(name + 4, O_RDWR | O_CLOEXEC);
- } else if (!strncmp(name, "framebuffer:", 12)) {
- ret = create_service_thread("fb", framebuffer_service).release();
- } else if (!strncmp(name, "jdwp:", 5)) {
- ret = create_jdwp_connection_fd(atoi(name + 5));
- } else if (!strncmp(name, "shell", 5)) {
- ret = ShellService(name + 5, transport);
- } else if (!strncmp(name, "exec:", 5)) {
- ret = StartSubprocess(name + 5, nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
- } else if (!strncmp(name, "sync:", 5)) {
- ret = create_service_thread("sync", file_sync_service).release();
- } else if (!strncmp(name, "remount:", 8)) {
- std::string options(name + strlen("remount:"));
- ret = create_service_thread("remount",
- std::bind(remount_service, std::placeholders::_1, options))
- .release();
- } else if (!strncmp(name, "reboot:", 7)) {
- std::string arg(name + strlen("reboot:"));
- ret = create_service_thread("reboot", std::bind(reboot_service, std::placeholders::_1, arg))
- .release();
- } else if (!strncmp(name, "root:", 5)) {
- ret = create_service_thread("root", restart_root_service).release();
- } else if (!strncmp(name, "unroot:", 7)) {
- ret = create_service_thread("unroot", restart_unroot_service).release();
- } else if (!strncmp(name, "backup:", 7)) {
- ret = StartSubprocess(
- android::base::StringPrintf("/system/bin/bu backup %s", (name + 7)).c_str(),
- nullptr, SubprocessType::kRaw, SubprocessProtocol::kNone);
- } else if (!strncmp(name, "restore:", 8)) {
- ret = StartSubprocess("/system/bin/bu restore", nullptr, SubprocessType::kRaw,
- SubprocessProtocol::kNone);
- } else if (!strncmp(name, "tcpip:", 6)) {
- int port;
- if (sscanf(name + 6, "%d", &port) != 1) {
- return -1;
- }
- ret = create_service_thread("tcp",
- std::bind(restart_tcp_service, std::placeholders::_1, port))
- .release();
- } else if (!strncmp(name, "usb:", 4)) {
- ret = create_service_thread("usb", restart_usb_service).release();
- } else if (!strncmp(name, "reverse:", 8)) {
- ret = reverse_service(name + 8, transport);
- } else if (!strncmp(name, "disable-verity:", 15)) {
- ret = create_service_thread("verity-on", std::bind(set_verity_enabled_state_service,
- std::placeholders::_1, false))
- .release();
- } else if (!strncmp(name, "enable-verity:", 15)) {
- ret = create_service_thread("verity-off", std::bind(set_verity_enabled_state_service,
- std::placeholders::_1, true))
- .release();
- } else if (!strcmp(name, "reconnect")) {
- ret = create_service_thread("reconnect",
- std::bind(reconnect_service, std::placeholders::_1, transport))
- .release();
+ ret = daemon_service_to_fd(name, transport).release();
#endif
}
+
if (ret >= 0) {
close_on_exec(ret);
}