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