From d7c73bf766843d09fdad51ab5a2306ecc38730e3 Mon Sep 17 00:00:00 2001 From: Simon Fels Date: Tue, 28 Jun 2016 19:46:35 +0200 Subject: [PATCH] Implement RPC commands on the Android side --- Android.mk | 9 +++- android/CMakeLists.txt | 5 ++ android/service/server.cpp | 54 ++++++++++++++++++- android/service/server.h | 5 ++ .../include/core/posix/child_process.h | 2 + .../src/core/posix/child_process.cpp | 20 ++++++- .../src/core/posix/fork.cpp | 4 ++ 7 files changed, 94 insertions(+), 5 deletions(-) diff --git a/Android.mk b/Android.mk index d8cedb8f..6480c360 100644 --- a/Android.mk +++ b/Android.mk @@ -9,8 +9,12 @@ LOCAL_SRC_FILES := \ external/process-cpp-minimal/src/core/posix/signal.cpp \ external/process-cpp-minimal/src/core/posix/signalable.cpp \ external/process-cpp-minimal/src/core/posix/standard_stream.cpp \ - external/process-cpp-minimal/src/core/posix/wait.cpp + external/process-cpp-minimal/src/core/posix/wait.cpp \ + external/process-cpp-minimal/src/core/posix/fork.cpp \ + external/process-cpp-minimal/src/core/posix/exec.cpp \ + external/process-cpp-minimal/src/core/posix/child_process.cpp LOCAL_CFLAGS := \ + -DANDROID \ -fexceptions LOCAL_C_INCLUDES += \ $(LOCAL_PATH)/external/process-cpp-minimal/include @@ -26,11 +30,12 @@ LOCAL_SRC_FILES := \ android/service/host_connector.cpp \ android/service/local_socket_connection.cpp \ android/service/message_processor.cpp \ + android/service/server.cpp \ src/anbox/common/fd.cpp \ src/anbox/bridge/message_processor.cpp \ src/anbox/bridge/pending_call_cache.cpp \ src/anbox/bridge/rpc_channel.cpp \ - src/anbox/protobuf/bridge.proto + src/anbox/protobuf/anbox_bridge.proto proto_header_dir := $(call local-generated-sources-dir)/proto/$(LOCAL_PATH)/src/anbox/protobuf LOCAL_C_INCLUDES += \ $(proto_header_dir) \ diff --git a/android/CMakeLists.txt b/android/CMakeLists.txt index 15b84a19..744b73fd 100644 --- a/android/CMakeLists.txt +++ b/android/CMakeLists.txt @@ -19,3 +19,8 @@ target_link_libraries(anboxd pthread process-cpp anbox-protobuf) +# As we're adding Android specific bits in this project we can't +# build this safely within default build anymore. We keep this +# for easy integration into used IDEs. +set_target_properties(anboxd + PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1) diff --git a/android/service/server.cpp b/android/service/server.cpp index 4e93ad42..8b58cded 100644 --- a/android/service/server.cpp +++ b/android/service/server.cpp @@ -19,6 +19,16 @@ #include "anbox_bridge.pb.h" +#include +#include + +namespace { +std::map common_env = { + {"ANDROID_DATA", "/data"}, + {"ANDROID_ROOT", "/system"}, +}; +} + namespace anbox { namespace android { Server::Server() { @@ -30,20 +40,60 @@ Server::~Server() { void Server::install_application(anbox::protobuf::bridge::InstallApplication const *request, anbox::protobuf::bridge::Void *response, google::protobuf::Closure *done) { - (void) request; (void) response; + std::vector argv = { + "/system/bin/pm", + "install", + request->path(), + }; + + auto process = core::posix::exec("/system/bin/sh", argv, common_env, core::posix::StandardStream::empty); + process.wait_for(core::posix::wait::Flags::untraced); + done->Run(); } void Server::launch_application(anbox::protobuf::bridge::LaunchApplication const *request, anbox::protobuf::bridge::Void *response, google::protobuf::Closure *done) { - (void) request; (void) response; + std::string intent = request->package_name(); + intent += "/"; + intent += request->activity(); + + std::vector argv = { + "/system/bin/am", + "start", + intent, + }; + + auto process = core::posix::exec("/system/bin/sh", argv, common_env, core::posix::StandardStream::empty); + process.wait_for(core::posix::wait::Flags::untraced); + done->Run(); } +void Server::set_dns_servers(anbox::protobuf::bridge::SetDnsServers const *request, + anbox::protobuf::bridge::Void *response, + google::protobuf::Closure *done) { + (void) response; + + std::vector argv = { + "resolver", + "setnetdns", + "0", + request->domain(), + }; + + for (int n = 0; n < request->servers_size(); n++) + argv.push_back(request->servers(n).address()); + + auto process = core::posix::exec("/system/bin/ndc", argv, common_env, core::posix::StandardStream::empty); + process.wait_for(core::posix::wait::Flags::untraced); + + done->Run(); +} } // namespace anbox } // namespace network diff --git a/android/service/server.h b/android/service/server.h index 69e868c6..94079820 100644 --- a/android/service/server.h +++ b/android/service/server.h @@ -29,6 +29,7 @@ namespace protobuf { namespace bridge { class InstallApplication; class LaunchApplication; +class SetDnsServers; class Void; } // namespace bridge } // namespace protobuf @@ -45,6 +46,10 @@ public: void launch_application(anbox::protobuf::bridge::LaunchApplication const *request, anbox::protobuf::bridge::Void *response, google::protobuf::Closure *done); + + void set_dns_servers(anbox::protobuf::bridge::SetDnsServers const *request, + anbox::protobuf::bridge::Void *response, + google::protobuf::Closure *done); }; } // namespace android } // namespace anbox diff --git a/external/process-cpp-minimal/include/core/posix/child_process.h b/external/process-cpp-minimal/include/core/posix/child_process.h index edbd6d3d..d6b3b31d 100644 --- a/external/process-cpp-minimal/include/core/posix/child_process.h +++ b/external/process-cpp-minimal/include/core/posix/child_process.h @@ -114,6 +114,7 @@ public: */ wait::Result wait_for(const wait::Flags& flags); +#ifndef ANDROID /** * @brief Access this process's stderr. */ @@ -128,6 +129,7 @@ public: * @brief Access this process's stdout. */ std::istream& cout(); +#endif private: friend ChildProcess fork(const std::function&, const StandardStream&); diff --git a/external/process-cpp-minimal/src/core/posix/child_process.cpp b/external/process-cpp-minimal/src/core/posix/child_process.cpp index 93d1cba1..b121b89e 100644 --- a/external/process-cpp-minimal/src/core/posix/child_process.cpp +++ b/external/process-cpp-minimal/src/core/posix/child_process.cpp @@ -18,8 +18,10 @@ #include +#ifndef ANDROID #include #include +#endif #include #include @@ -33,7 +35,13 @@ #include #include +#include + +#include + +#ifndef ANDROID namespace io = boost::iostreams; +#endif namespace { @@ -279,12 +287,14 @@ struct ChildProcess::Private const ChildProcess::Pipe& stdin, const ChildProcess::Pipe& stdout) : pipes{stderr, stdin, stdout}, +#ifndef ANDROID serr(pipes.stderr.read_fd(), io::never_close_handle), sin(pipes.stdin.write_fd(), io::never_close_handle), sout(pipes.stdout.read_fd(), io::never_close_handle), cerr(&serr), cin(&sin), cout(&sout), +#endif original_parent_pid(::getpid()), original_child_pid(pid) { @@ -308,12 +318,15 @@ struct ChildProcess::Private ChildProcess::Pipe stdout; ChildProcess::Pipe stderr; } pipes; + +#ifndef ANDROID io::stream_buffer serr; io::stream_buffer sin; io::stream_buffer sout; std::istream cerr; std::ostream cin; std::istream cout; +#endif // We need to store the original parent pid as we might have been forked // and with our automatic cleanup in place, it might happen that the d'tor @@ -371,14 +384,18 @@ wait::Result ChildProcess::wait_for(const wait::Flags& flags) { result.status = wait::Result::Status::stopped; result.detail.if_stopped.signal = static_cast(WSTOPSIG(status)); - } else if (WIFCONTINUED(status)) + } +#ifndef ANDROID + else if (WIFCONTINUED(status)) { result.status = wait::Result::Status::continued; } +#endif return result; } +#ifndef ANDROID std::istream& ChildProcess::cerr() { return d->cerr; @@ -393,5 +410,6 @@ std::istream& ChildProcess::cout() { return d->cout; } +#endif } } diff --git a/external/process-cpp-minimal/src/core/posix/fork.cpp b/external/process-cpp-minimal/src/core/posix/fork.cpp index 1c9c1f30..82a225c7 100644 --- a/external/process-cpp-minimal/src/core/posix/fork.cpp +++ b/external/process-cpp-minimal/src/core/posix/fork.cpp @@ -19,7 +19,9 @@ #include #include +#ifndef ANDROID #include "backtrace.h" +#endif #include #include @@ -38,12 +40,14 @@ void redirect_stream_to_fd(int fd, int stream) void print_backtrace(std::ostream& out, const std::string& line_prefix) { +#ifndef ANDROID core::posix::backtrace::visit_with_handler([&out, line_prefix](const core::posix::backtrace::Frame& frame) { out << line_prefix << std::dec << std::setw(2) << frame.depth() << "@" << std::hex << std::setw(14) << frame.frame_pointer() << ": " << (frame.symbol().is_cxx() ? frame.symbol().demangled() : frame.symbol().raw()) << std::endl; return true; }); +#endif } } -- GitLab