Implement RPC commands on the Android side

This commit is contained in:
Simon Fels 2016-06-28 19:46:35 +02:00
commit d7c73bf766
7 changed files with 94 additions and 5 deletions

View file

@ -9,8 +9,12 @@ LOCAL_SRC_FILES := \
external/process-cpp-minimal/src/core/posix/signal.cpp \ 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/signalable.cpp \
external/process-cpp-minimal/src/core/posix/standard_stream.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 := \ LOCAL_CFLAGS := \
-DANDROID \
-fexceptions -fexceptions
LOCAL_C_INCLUDES += \ LOCAL_C_INCLUDES += \
$(LOCAL_PATH)/external/process-cpp-minimal/include $(LOCAL_PATH)/external/process-cpp-minimal/include
@ -26,11 +30,12 @@ LOCAL_SRC_FILES := \
android/service/host_connector.cpp \ android/service/host_connector.cpp \
android/service/local_socket_connection.cpp \ android/service/local_socket_connection.cpp \
android/service/message_processor.cpp \ android/service/message_processor.cpp \
android/service/server.cpp \
src/anbox/common/fd.cpp \ src/anbox/common/fd.cpp \
src/anbox/bridge/message_processor.cpp \ src/anbox/bridge/message_processor.cpp \
src/anbox/bridge/pending_call_cache.cpp \ src/anbox/bridge/pending_call_cache.cpp \
src/anbox/bridge/rpc_channel.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 proto_header_dir := $(call local-generated-sources-dir)/proto/$(LOCAL_PATH)/src/anbox/protobuf
LOCAL_C_INCLUDES += \ LOCAL_C_INCLUDES += \
$(proto_header_dir) \ $(proto_header_dir) \

View file

@ -19,3 +19,8 @@ target_link_libraries(anboxd
pthread pthread
process-cpp process-cpp
anbox-protobuf) 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)

View file

@ -19,6 +19,16 @@
#include "anbox_bridge.pb.h" #include "anbox_bridge.pb.h"
#include <core/posix/exec.h>
#include <core/posix/child_process.h>
namespace {
std::map<std::string,std::string> common_env = {
{"ANDROID_DATA", "/data"},
{"ANDROID_ROOT", "/system"},
};
}
namespace anbox { namespace anbox {
namespace android { namespace android {
Server::Server() { Server::Server() {
@ -30,20 +40,60 @@ Server::~Server() {
void Server::install_application(anbox::protobuf::bridge::InstallApplication const *request, void Server::install_application(anbox::protobuf::bridge::InstallApplication const *request,
anbox::protobuf::bridge::Void *response, anbox::protobuf::bridge::Void *response,
google::protobuf::Closure *done) { google::protobuf::Closure *done) {
(void) request;
(void) response; (void) response;
std::vector<std::string> 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(); done->Run();
} }
void Server::launch_application(anbox::protobuf::bridge::LaunchApplication const *request, void Server::launch_application(anbox::protobuf::bridge::LaunchApplication const *request,
anbox::protobuf::bridge::Void *response, anbox::protobuf::bridge::Void *response,
google::protobuf::Closure *done) { google::protobuf::Closure *done) {
(void) request;
(void) response; (void) response;
std::string intent = request->package_name();
intent += "/";
intent += request->activity();
std::vector<std::string> 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(); 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<std::string> 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 anbox
} // namespace network } // namespace network

View file

@ -29,6 +29,7 @@ namespace protobuf {
namespace bridge { namespace bridge {
class InstallApplication; class InstallApplication;
class LaunchApplication; class LaunchApplication;
class SetDnsServers;
class Void; class Void;
} // namespace bridge } // namespace bridge
} // namespace protobuf } // namespace protobuf
@ -45,6 +46,10 @@ public:
void launch_application(anbox::protobuf::bridge::LaunchApplication const *request, void launch_application(anbox::protobuf::bridge::LaunchApplication const *request,
anbox::protobuf::bridge::Void *response, anbox::protobuf::bridge::Void *response,
google::protobuf::Closure *done); 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 android
} // namespace anbox } // namespace anbox

View file

@ -114,6 +114,7 @@ public:
*/ */
wait::Result wait_for(const wait::Flags& flags); wait::Result wait_for(const wait::Flags& flags);
#ifndef ANDROID
/** /**
* @brief Access this process's stderr. * @brief Access this process's stderr.
*/ */
@ -128,6 +129,7 @@ public:
* @brief Access this process's stdout. * @brief Access this process's stdout.
*/ */
std::istream& cout(); std::istream& cout();
#endif
private: private:
friend ChildProcess fork(const std::function<posix::exit::Status()>&, const StandardStream&); friend ChildProcess fork(const std::function<posix::exit::Status()>&, const StandardStream&);

View file

@ -18,8 +18,10 @@
#include <core/posix/child_process.h> #include <core/posix/child_process.h>
#ifndef ANDROID
#include <boost/iostreams/device/file_descriptor.hpp> #include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp> #include <boost/iostreams/stream.hpp>
#endif
#include <atomic> #include <atomic>
#include <fstream> #include <fstream>
@ -33,7 +35,13 @@
#include <sys/eventfd.h> #include <sys/eventfd.h>
#include <sys/signalfd.h> #include <sys/signalfd.h>
#include <assert.h>
#include <sys/wait.h>
#ifndef ANDROID
namespace io = boost::iostreams; namespace io = boost::iostreams;
#endif
namespace namespace
{ {
@ -279,12 +287,14 @@ struct ChildProcess::Private
const ChildProcess::Pipe& stdin, const ChildProcess::Pipe& stdin,
const ChildProcess::Pipe& stdout) const ChildProcess::Pipe& stdout)
: pipes{stderr, stdin, stdout}, : pipes{stderr, stdin, stdout},
#ifndef ANDROID
serr(pipes.stderr.read_fd(), io::never_close_handle), serr(pipes.stderr.read_fd(), io::never_close_handle),
sin(pipes.stdin.write_fd(), io::never_close_handle), sin(pipes.stdin.write_fd(), io::never_close_handle),
sout(pipes.stdout.read_fd(), io::never_close_handle), sout(pipes.stdout.read_fd(), io::never_close_handle),
cerr(&serr), cerr(&serr),
cin(&sin), cin(&sin),
cout(&sout), cout(&sout),
#endif
original_parent_pid(::getpid()), original_parent_pid(::getpid()),
original_child_pid(pid) original_child_pid(pid)
{ {
@ -308,12 +318,15 @@ struct ChildProcess::Private
ChildProcess::Pipe stdout; ChildProcess::Pipe stdout;
ChildProcess::Pipe stderr; ChildProcess::Pipe stderr;
} pipes; } pipes;
#ifndef ANDROID
io::stream_buffer<io::file_descriptor_source> serr; io::stream_buffer<io::file_descriptor_source> serr;
io::stream_buffer<io::file_descriptor_sink> sin; io::stream_buffer<io::file_descriptor_sink> sin;
io::stream_buffer<io::file_descriptor_source> sout; io::stream_buffer<io::file_descriptor_source> sout;
std::istream cerr; std::istream cerr;
std::ostream cin; std::ostream cin;
std::istream cout; std::istream cout;
#endif
// We need to store the original parent pid as we might have been forked // 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 // 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.status = wait::Result::Status::stopped;
result.detail.if_stopped.signal = static_cast<Signal>(WSTOPSIG(status)); result.detail.if_stopped.signal = static_cast<Signal>(WSTOPSIG(status));
} else if (WIFCONTINUED(status)) }
#ifndef ANDROID
else if (WIFCONTINUED(status))
{ {
result.status = wait::Result::Status::continued; result.status = wait::Result::Status::continued;
} }
#endif
return result; return result;
} }
#ifndef ANDROID
std::istream& ChildProcess::cerr() std::istream& ChildProcess::cerr()
{ {
return d->cerr; return d->cerr;
@ -393,5 +410,6 @@ std::istream& ChildProcess::cout()
{ {
return d->cout; return d->cout;
} }
#endif
} }
} }

View file

@ -19,7 +19,9 @@
#include <core/posix/exit.h> #include <core/posix/exit.h>
#include <core/posix/fork.h> #include <core/posix/fork.h>
#ifndef ANDROID
#include "backtrace.h" #include "backtrace.h"
#endif
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
@ -38,12 +40,14 @@ void redirect_stream_to_fd(int fd, int stream)
void print_backtrace(std::ostream& out, const std::string& line_prefix) 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) 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() << ": " 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; << (frame.symbol().is_cxx() ? frame.symbol().demangled() : frame.symbol().raw()) << std::endl;
return true; return true;
}); });
#endif
} }
} }