From 9f5816c9a34f40b6acfc8924ee914d5165c1e35f Mon Sep 17 00:00:00 2001 From: Jan Olszak Date: Thu, 8 Oct 2015 14:19:22 +0200 Subject: [PATCH] lxcpp: Switch on serialization from libConfig [Feature] Use std::pair and enum serialization [Cause] N/A [Solution] N/A [Verification] Build, install, run tests Change-Id: I6edb4543bf85eacfb2efe13065970326b30aba17 --- libs/lxcpp/attach/attach-config.hpp | 14 ++++++-------- libs/lxcpp/attach/attach-helper.cpp | 4 ++-- libs/lxcpp/commands/attach.cpp | 2 +- libs/lxcpp/commands/attach.hpp | 2 +- libs/lxcpp/container-impl.cpp | 2 +- libs/lxcpp/container-impl.hpp | 2 +- libs/lxcpp/container.hpp | 2 +- libs/lxcpp/guard/guard.cpp | 11 +---------- libs/lxcpp/process.cpp | 16 ++++++++++++++++ libs/lxcpp/process.hpp | 2 ++ 10 files changed, 32 insertions(+), 25 deletions(-) diff --git a/libs/lxcpp/attach/attach-config.hpp b/libs/lxcpp/attach/attach-config.hpp index 9788abe..94e2626 100644 --- a/libs/lxcpp/attach/attach-config.hpp +++ b/libs/lxcpp/attach/attach-config.hpp @@ -39,7 +39,7 @@ namespace lxcpp { struct AttachConfig { /// Arguments passed by user, argv[0] is the binary's path in container - std::vector argv; + std::vector argv; /// PID of the container's init process pid_t initPid; @@ -73,7 +73,7 @@ struct AttachConfig { AttachConfig() = default; - AttachConfig(const std::vector& argv, + AttachConfig(const std::vector& argv, const pid_t initPid, const std::vector& namespaces, const uid_t uid, @@ -99,19 +99,17 @@ struct AttachConfig { CONFIG_REGISTER ( //TODO: Uncomment and fix cstring serialization - // argv, + argv, initPid, - //TODO: Uncomment and fix Namespace serialization (or remove Namespace) - // namespaces, + namespaces, uid, gid, ttyFD, supplementaryGids, capsToKeep, workDirInContainer, - envToKeep - //TODO: Uncomment and fix std::pair serialization - // envToSet + envToKeep, + envToSet ) }; diff --git a/libs/lxcpp/attach/attach-helper.cpp b/libs/lxcpp/attach/attach-helper.cpp index 321502f..5aaaecb 100644 --- a/libs/lxcpp/attach/attach-helper.cpp +++ b/libs/lxcpp/attach/attach-helper.cpp @@ -70,8 +70,8 @@ int child(void* data) ::_exit(EXIT_FAILURE); } - // Run user's binary - ::execve(config.argv[0], const_cast(config.argv.data()), nullptr); + lxcpp::execve(config.argv); + return EXIT_FAILURE; } diff --git a/libs/lxcpp/commands/attach.cpp b/libs/lxcpp/commands/attach.cpp index 2bfa592..a059986 100644 --- a/libs/lxcpp/commands/attach.cpp +++ b/libs/lxcpp/commands/attach.cpp @@ -34,7 +34,7 @@ namespace lxcpp { Attach::Attach(const lxcpp::ContainerImpl& container, - const std::vector& argv, + const std::vector& argv, const uid_t uid, const gid_t gid, const std::string& ttyPath, diff --git a/libs/lxcpp/commands/attach.hpp b/libs/lxcpp/commands/attach.hpp index ce39b83..123a384 100644 --- a/libs/lxcpp/commands/attach.hpp +++ b/libs/lxcpp/commands/attach.hpp @@ -56,7 +56,7 @@ public: * @param envToSet new environment variables that will be set */ Attach(const lxcpp::ContainerImpl& container, - const std::vector& argv, + const std::vector& argv, const uid_t uid, const gid_t gid, const std::string& ttyPath, diff --git a/libs/lxcpp/container-impl.cpp b/libs/lxcpp/container-impl.cpp index c7035e4..9ff4b52 100644 --- a/libs/lxcpp/container-impl.cpp +++ b/libs/lxcpp/container-impl.cpp @@ -205,7 +205,7 @@ void ContainerImpl::reboot() throw NotImplementedException(); } -void ContainerImpl::attach(const std::vector& argv, +void ContainerImpl::attach(const std::vector& argv, const std::string& cwdInContainer) { Attach attach(*this, diff --git a/libs/lxcpp/container-impl.hpp b/libs/lxcpp/container-impl.hpp index 7393974..041303b 100644 --- a/libs/lxcpp/container-impl.hpp +++ b/libs/lxcpp/container-impl.hpp @@ -65,7 +65,7 @@ public: void reboot(); // Other - void attach(const std::vector& argv, + void attach(const std::vector& argv, const std::string& cwdInContainer); // Network interfaces setup/config diff --git a/libs/lxcpp/container.hpp b/libs/lxcpp/container.hpp index 51dda8c..7f98aed 100644 --- a/libs/lxcpp/container.hpp +++ b/libs/lxcpp/container.hpp @@ -71,7 +71,7 @@ public: virtual void reboot() = 0; // Other - virtual void attach(const std::vector& argv, + virtual void attach(const std::vector& argv, const std::string& cwdInContainer) = 0; // Network interfaces setup/config diff --git a/libs/lxcpp/guard/guard.cpp b/libs/lxcpp/guard/guard.cpp index 952cdf5..369d423 100644 --- a/libs/lxcpp/guard/guard.cpp +++ b/libs/lxcpp/guard/guard.cpp @@ -38,16 +38,7 @@ namespace lxcpp { void startContainer(const ContainerConfig &cfg) { - std::vector argv; - argv.reserve(cfg.mInit.size() + 1); - for (auto const & it : cfg.mInit) { - argv.push_back(it.c_str()); - } - argv.push_back(nullptr); - - LOGD("Executing container's init: " << argv[0]); - ::execve(argv[0], const_cast(argv.data()), NULL); - ::_exit(EXIT_FAILURE); + lxcpp::execve(cfg.mInit); } int startGuard(int channelFD) diff --git a/libs/lxcpp/process.cpp b/libs/lxcpp/process.cpp index 0353d82..204dec2 100644 --- a/libs/lxcpp/process.cpp +++ b/libs/lxcpp/process.cpp @@ -156,4 +156,20 @@ void unshare(const Namespace ns) throw ProcessSetupException(msg); } } + +void execve(const std::vector& argv) +{ + // Prepare the arguments + std::vector tmpArgv; + tmpArgv.reserve(argv.size() + 1); + + for (auto const &str : argv) { + tmpArgv.push_back(str.c_str()); + } + tmpArgv.push_back(nullptr); + + // Run user's binary + ::execve(tmpArgv[0], const_cast(tmpArgv.data()), nullptr); +} + } // namespace lxcpp diff --git a/libs/lxcpp/process.hpp b/libs/lxcpp/process.hpp index 869e3d6..75c1e4f 100644 --- a/libs/lxcpp/process.hpp +++ b/libs/lxcpp/process.hpp @@ -49,6 +49,8 @@ int waitpid(const pid_t pid); void unshare(const Namespace ns); +void execve(const std::vector& argv); + } // namespace lxcpp #endif // LXCPP_PROCESS_HPP \ No newline at end of file -- 2.7.4