提交 e1aff472 编写于 作者: S Simon Fels 提交者: GitHub

Merge pull request #35 from morphis/feature/refactor-config

Refactor configuration to be more dynamic
...@@ -49,7 +49,7 @@ namespace anbox { ...@@ -49,7 +49,7 @@ namespace anbox {
namespace audio { namespace audio {
Server::Server(const std::shared_ptr<Runtime>& rt, const std::shared_ptr<platform::Policy> &platform_policy) : Server::Server(const std::shared_ptr<Runtime>& rt, const std::shared_ptr<platform::Policy> &platform_policy) :
platform_policy_(platform_policy), platform_policy_(platform_policy),
socket_file_(utils::string_format("%s/anbox_audio", config::socket_path())), socket_file_(utils::string_format("%s/anbox_audio", SystemConfiguration::instance().socket_dir())),
connector_(std::make_shared<network::PublishedSocketConnector>( connector_(std::make_shared<network::PublishedSocketConnector>(
socket_file_, rt, socket_file_, rt,
std::make_shared<network::DelegateConnectionCreator<boost::asio::local::stream_protocol>>(std::bind(&Server::create_connection_for, this, _1)))), std::make_shared<network::DelegateConnectionCreator<boost::asio::local::stream_protocol>>(std::bind(&Server::create_connection_for, this, _1)))),
......
...@@ -94,7 +94,8 @@ anbox::cmds::Run::Run(const BusFactory &bus_factory) ...@@ -94,7 +94,8 @@ anbox::cmds::Run::Run(const BusFactory &bus_factory)
}); });
utils::ensure_paths({ utils::ensure_paths({
config::socket_path(), config::host_input_device_path(), SystemConfiguration::instance().socket_dir(),
SystemConfiguration::instance().input_device_dir(),
}); });
auto rt = Runtime::create(); auto rt = Runtime::create();
...@@ -128,15 +129,17 @@ anbox::cmds::Run::Run(const BusFactory &bus_factory) ...@@ -128,15 +129,17 @@ anbox::cmds::Run::Run(const BusFactory &bus_factory)
auto audio_server = std::make_shared<audio::Server>(rt, policy); auto audio_server = std::make_shared<audio::Server>(rt, policy);
const auto socket_path = SystemConfiguration::instance().socket_dir();
// The qemu pipe is used as a very fast communication channel between guest // The qemu pipe is used as a very fast communication channel between guest
// and host for things like the GLES emulation/translation, the RIL or ADB. // and host for things like the GLES emulation/translation, the RIL or ADB.
auto qemu_pipe_connector = auto qemu_pipe_connector =
std::make_shared<network::PublishedSocketConnector>( std::make_shared<network::PublishedSocketConnector>(
utils::string_format("%s/qemu_pipe", config::socket_path()), rt, utils::string_format("%s/qemu_pipe", socket_path), rt,
std::make_shared<qemu::PipeConnectionCreator>(gl_server->renderer(), rt)); std::make_shared<qemu::PipeConnectionCreator>(gl_server->renderer(), rt));
auto bridge_connector = std::make_shared<network::PublishedSocketConnector>( auto bridge_connector = std::make_shared<network::PublishedSocketConnector>(
utils::string_format("%s/anbox_bridge", config::socket_path()), rt, utils::string_format("%s/anbox_bridge", socket_path), rt,
std::make_shared<rpc::ConnectionCreator>( std::make_shared<rpc::ConnectionCreator>(
rt, [&](const std::shared_ptr<network::MessageSender> &sender) { rt, [&](const std::shared_ptr<network::MessageSender> &sender) {
auto pending_calls = std::make_shared<rpc::PendingCallCache>(); auto pending_calls = std::make_shared<rpc::PendingCallCache>();
...@@ -161,20 +164,18 @@ anbox::cmds::Run::Run(const BusFactory &bus_factory) ...@@ -161,20 +164,18 @@ anbox::cmds::Run::Run(const BusFactory &bus_factory)
{qemu_pipe_connector->socket_file(), "/dev/qemu_pipe"}, {qemu_pipe_connector->socket_file(), "/dev/qemu_pipe"},
{bridge_connector->socket_file(), "/dev/anbox_bridge"}, {bridge_connector->socket_file(), "/dev/anbox_bridge"},
{audio_server->socket_file(), "/dev/anbox_audio"}, {audio_server->socket_file(), "/dev/anbox_audio"},
{config::host_input_device_path(), "/dev/input"}, {SystemConfiguration::instance().input_device_dir(), "/dev/input"},
{"/dev/binder", "/dev/binder"}, {"/dev/binder", "/dev/binder"},
{"/dev/ashmem", "/dev/ashmem"}, {"/dev/ashmem", "/dev/ashmem"},
{"/dev/fuse", "/dev/fuse"}, {"/dev/fuse", "/dev/fuse"},
}; };
dispatcher->dispatch( dispatcher->dispatch([&]() { container.start_container(container_configuration); });
[&]() { container.start_container(container_configuration); });
auto bus = bus_factory_(); auto bus = bus_factory_();
bus->install_executor(core::dbus::asio::make_executor(bus, rt->service())); bus->install_executor(core::dbus::asio::make_executor(bus, rt->service()));
auto skeleton = auto skeleton = anbox::dbus::skeleton::Service::create_for_bus(bus, android_api_stub);
anbox::dbus::skeleton::Service::create_for_bus(bus, android_api_stub);
rt->start(); rt->start();
trap->run(); trap->run();
......
...@@ -15,93 +15,58 @@ ...@@ -15,93 +15,58 @@
* *
*/ */
#include <cstring>
#include "anbox/config.h" #include "anbox/config.h"
#include "anbox/utils.h" #include "anbox/utils.h"
#include <boost/filesystem.hpp>
namespace fs = boost::filesystem;
namespace anbox {
namespace config {
std::string in_snap_dir(const std::string &path) {
return utils::prefix_dir_from_env(path, "SNAP");
}
std::string in_snap_data_dir(const std::string &path) { #include <cstring>
return utils::prefix_dir_from_env(path, "SNAP_COMMON");
}
std::string in_snap_user_data_dir(const std::string &path) { namespace fs = boost::filesystem;
return utils::prefix_dir_from_env(path, "SNAP_USER_COMMON");
}
std::string home_dir() { namespace {
static std::string runtime_dir() {
static std::string path; static std::string path;
if (path.empty()) { if (path.empty()) {
path = utils::get_env_value("HOME", ""); path = anbox::utils::get_env_value("XDG_RUNTIME_DIR", "");
if (path.empty()) if (path.empty())
BOOST_THROW_EXCEPTION(std::runtime_error("No home directory specified")); BOOST_THROW_EXCEPTION(std::runtime_error("No runtime directory specified"));
} }
return path; return path;
} }
std::string runtime_dir() {
static std::string path;
if (path.empty()) {
path = utils::get_env_value("XDG_RUNTIME_DIR", "");
if (path.empty())
BOOST_THROW_EXCEPTION(
std::runtime_error("No runtime directory specified"));
}
return path;
} }
std::string state_dir() { void anbox::SystemConfiguration::set_data_path(const std::string &path) {
static std::string path = "/var/lib"; data_path = path;
return path;
} }
std::string log_path() { std::string anbox::SystemConfiguration::rootfs_dir() const {
static std::string path = return (data_path / "rootfs").string();
in_snap_data_dir(utils::string_format("%s/anbox/", state_dir()));
return path;
} }
std::string socket_path() { std::string anbox::SystemConfiguration::log_dir() const {
static std::string path = return (data_path / "logs").string();
utils::string_format("%s/anbox/sockets", runtime_dir());
return path;
} }
std::string data_path() { std::string anbox::SystemConfiguration::container_config_dir() const {
static std::string path = utils::string_format("%s/.anbox/data", home_dir()); return (data_path / "containers").string();
return path;
} }
std::string rootfs_path() { std::string anbox::SystemConfiguration::container_socket_path() const {
static std::string path = return "/run/anbox-container.socket";
in_snap_data_dir(utils::string_format("%s/anbox/rootfs", state_dir()));
return path;
} }
std::string container_config_path() { std::string anbox::SystemConfiguration::socket_dir() const {
static std::string path = in_snap_data_dir( static std::string dir = anbox::utils::string_format("%s/anbox/sockets", runtime_dir());
utils::string_format("%s/anbox/containers", state_dir())); return dir;
return path;
} }
std::string container_socket_path() { std::string anbox::SystemConfiguration::input_device_dir() const {
std::string path = "/run/anbox-container.socket"; static std::string dir = anbox::utils::string_format("%s/anbox/input", runtime_dir());
return path; return dir;
} }
std::string host_input_device_path() { anbox::SystemConfiguration& anbox::SystemConfiguration::instance() {
static std::string path = static SystemConfiguration config;
utils::string_format("%s/anbox/input-devices", runtime_dir()); return config;
return path;
} }
} // namespace config
} // namespace anbox
...@@ -19,20 +19,32 @@ ...@@ -19,20 +19,32 @@
#define ANBOX_CONFIG_H_ #define ANBOX_CONFIG_H_
#include <string> #include <string>
#include <memory>
#include <boost/filesystem.hpp>
namespace anbox { namespace anbox {
namespace config { class SystemConfiguration {
std::string in_snap_dir(const std::string &path); public:
std::string in_snap_data_dir(const std::string &path); static SystemConfiguration& instance();
std::string in_snap_user_data_dir(const std::string &path);
std::string data_path(); virtual ~SystemConfiguration() = default;
std::string rootfs_path();
std::string log_path(); void set_data_path(const std::string &path);
std::string socket_path();
std::string container_config_path(); std::string rootfs_dir() const;
std::string container_socket_path(); std::string log_dir() const;
std::string host_input_device_path(); std::string socket_dir() const;
} // namespace config std::string container_config_dir() const;
std::string container_socket_path() const;
std::string input_device_dir() const;
protected:
SystemConfiguration() = default;
boost::filesystem::path data_path = "/var/lib/anbox";
};
} // namespace anbox } // namespace anbox
#endif #endif
...@@ -31,7 +31,7 @@ namespace anbox { ...@@ -31,7 +31,7 @@ namespace anbox {
namespace container { namespace container {
Client::Client(const std::shared_ptr<Runtime> &rt) Client::Client(const std::shared_ptr<Runtime> &rt)
: messenger_(std::make_shared<network::LocalSocketMessenger>( : messenger_(std::make_shared<network::LocalSocketMessenger>(
config::container_socket_path(), rt)), SystemConfiguration::instance().container_socket_path(), rt)),
pending_calls_(std::make_shared<rpc::PendingCallCache>()), pending_calls_(std::make_shared<rpc::PendingCallCache>()),
rpc_channel_(std::make_shared<rpc::Channel>(pending_calls_, messenger_)), rpc_channel_(std::make_shared<rpc::Channel>(pending_calls_, messenger_)),
management_api_(std::make_shared<ManagementApiStub>(rpc_channel_)), management_api_(std::make_shared<ManagementApiStub>(rpc_channel_)),
......
...@@ -38,7 +38,8 @@ namespace container { ...@@ -38,7 +38,8 @@ namespace container {
LxcContainer::LxcContainer(const network::Credentials &creds) LxcContainer::LxcContainer(const network::Credentials &creds)
: state_(State::inactive), container_(nullptr), creds_(creds) { : state_(State::inactive), container_(nullptr), creds_(creds) {
utils::ensure_paths({ utils::ensure_paths({
config::container_config_path(), config::log_path(), SystemConfiguration::instance().container_config_dir(),
SystemConfiguration::instance().log_dir(),
}); });
} }
...@@ -78,28 +79,23 @@ void LxcContainer::setup_id_maps() { ...@@ -78,28 +79,23 @@ void LxcContainer::setup_id_maps() {
void LxcContainer::start(const Configuration &configuration) { void LxcContainer::start(const Configuration &configuration) {
if (getuid() != 0) if (getuid() != 0)
BOOST_THROW_EXCEPTION( BOOST_THROW_EXCEPTION(std::runtime_error("You have to start the container as root"));
std::runtime_error("You have to start the container as root"));
if (container_ && container_->is_running(container_)) { if (container_ && container_->is_running(container_)) {
BOOST_THROW_EXCEPTION( WARNING("Container already started, stopping it now");
std::runtime_error("Container already started, stopping it now"));
container_->stop(container_); container_->stop(container_);
} }
if (!container_) { if (!container_) {
DEBUG("Containers are stored in %s", config::container_config_path()); const auto container_config_dir = SystemConfiguration::instance().container_config_dir();
DEBUG("Containers are stored in %s", container_config_dir);
// Remove container config to be be able to rewrite it // Remove container config to be be able to rewrite it
::unlink(utils::string_format("%s/default/config", ::unlink(utils::string_format("%s/default/config", container_config_dir).c_str());
config::container_config_path())
.c_str());
container_ = container_ = lxc_container_new("default", container_config_dir.c_str());
lxc_container_new("default", config::container_config_path().c_str());
if (!container_) if (!container_)
BOOST_THROW_EXCEPTION( BOOST_THROW_EXCEPTION(std::runtime_error("Failed to create LXC container instance"));
std::runtime_error("Failed to create LXC container instance"));
// If container is still running (for example after a crash) we stop it here // If container is still running (for example after a crash) we stop it here
// to ensure // to ensure
...@@ -128,13 +124,13 @@ void LxcContainer::start(const Configuration &configuration) { ...@@ -128,13 +124,13 @@ void LxcContainer::start(const Configuration &configuration) {
set_config_item("lxc.init_cmd", "/anbox-init.sh"); set_config_item("lxc.init_cmd", "/anbox-init.sh");
set_config_item("lxc.rootfs.backend", "dir"); set_config_item("lxc.rootfs.backend", "dir");
DEBUG("Using rootfs path %s", config::rootfs_path()); const auto rootfs_path = SystemConfiguration::instance().rootfs_dir();
set_config_item("lxc.rootfs", config::rootfs_path()); DEBUG("Using rootfs path %s", rootfs_path);
set_config_item("lxc.rootfs", rootfs_path);
set_config_item("lxc.loglevel", "0"); set_config_item("lxc.loglevel", "0");
set_config_item( const auto log_path = SystemConfiguration::instance().log_dir();
"lxc.logfile", set_config_item("lxc.logfile", utils::string_format("%s/container.log", log_path).c_str());
utils::string_format("%s/container.log", config::log_path()).c_str());
if (fs::exists("/sys/class/net/anboxbr0")) { if (fs::exists("/sys/class/net/anboxbr0")) {
set_config_item("lxc.network.type", "veth"); set_config_item("lxc.network.type", "veth");
...@@ -178,7 +174,7 @@ void LxcContainer::start(const Configuration &configuration) { ...@@ -178,7 +174,7 @@ void LxcContainer::start(const Configuration &configuration) {
// when running in confined environments like snap's. // when running in confined environments like snap's.
if (!utils::string_starts_with(target_path, "/")) if (!utils::string_starts_with(target_path, "/"))
target_path = std::string("/") + target_path; target_path = std::string("/") + target_path;
target_path = config::rootfs_path() + target_path; target_path = rootfs_path + target_path;
set_config_item( set_config_item(
"lxc.mount.entry", "lxc.mount.entry",
...@@ -213,8 +209,7 @@ void LxcContainer::stop() { ...@@ -213,8 +209,7 @@ void LxcContainer::stop() {
void LxcContainer::set_config_item(const std::string &key, void LxcContainer::set_config_item(const std::string &key,
const std::string &value) { const std::string &value) {
if (!container_->set_config_item(container_, key.c_str(), value.c_str())) if (!container_->set_config_item(container_, key.c_str(), value.c_str()))
BOOST_THROW_EXCEPTION( BOOST_THROW_EXCEPTION(std::runtime_error("Failed to configure LXC container"));
std::runtime_error("Failed to configure LXC container"));
} }
Container::State LxcContainer::state() { return state_; } Container::State LxcContainer::state() { return state_; }
......
...@@ -38,12 +38,11 @@ std::shared_ptr<Service> Service::create(const std::shared_ptr<Runtime> &rt) { ...@@ -38,12 +38,11 @@ std::shared_ptr<Service> Service::create(const std::shared_ptr<Runtime> &rt) {
[sp](std::shared_ptr<boost::asio::local::stream_protocol::socket> const [sp](std::shared_ptr<boost::asio::local::stream_protocol::socket> const
&socket) { sp->new_client(socket); }); &socket) { sp->new_client(socket); });
sp->connector_ = std::make_shared<network::PublishedSocketConnector>( const auto container_socket_path = SystemConfiguration::instance().container_socket_path();
config::container_socket_path(), rt, delegate_connector); sp->connector_ = std::make_shared<network::PublishedSocketConnector>(container_socket_path, rt, delegate_connector);
// Make sure others can connect to our socket // Make sure others can connect to our socket
::chmod(config::container_socket_path().c_str(), ::chmod(container_socket_path.c_str(), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
DEBUG("Everything setup. Waiting for incoming connections."); DEBUG("Everything setup. Waiting for incoming connections.");
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
namespace anbox { namespace anbox {
namespace input { namespace input {
Manager::Manager(const std::shared_ptr<Runtime> &runtime) : runtime_(runtime) { Manager::Manager(const std::shared_ptr<Runtime> &runtime) : runtime_(runtime) {
utils::ensure_paths({config::host_input_device_path()}); utils::ensure_paths({SystemConfiguration::instance().input_device_dir()});
} }
Manager::~Manager() {} Manager::~Manager() {}
...@@ -45,8 +45,7 @@ std::uint32_t Manager::next_id() { ...@@ -45,8 +45,7 @@ std::uint32_t Manager::next_id() {
} }
std::string Manager::build_device_path(const std::uint32_t &id) { std::string Manager::build_device_path(const std::uint32_t &id) {
return (boost::format("%1%/event%2%") % config::host_input_device_path() % id) return (boost::format("%1%/event%2%") % SystemConfiguration::instance().input_device_dir() % id).str();
.str();
} }
} // namespace input } // namespace input
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册