lxcpp: Stopping container's init process 04/49304/3
authorJan Olszak <j.olszak@samsung.com>
Mon, 12 Oct 2015 10:43:10 +0000 (12:43 +0200)
committerJan Olszak <j.olszak@samsung.com>
Mon, 12 Oct 2015 12:01:06 +0000 (14:01 +0200)
[Feature]       Terminating container's init process
[Cause]         N/A
[Solution]      N/A
[Verification]  Build, install and run tests

Change-Id: I77af77016d06555b6eea3e133e25a44574580c67

common/utils/signal.cpp
common/utils/signal.hpp
libs/lxcpp/commands/stop.cpp [new file with mode: 0644]
libs/lxcpp/commands/stop.hpp [new file with mode: 0644]
libs/lxcpp/container-impl.cpp
libs/lxcpp/utils.cpp
tests/unit_tests/lxcpp/ut-container.cpp

index bad3b0b..51c45b1 100644 (file)
@@ -133,6 +133,17 @@ void signalIgnore(const std::initializer_list<int>& signals)
     }
 }
 
+void sendSignal(const pid_t pid, const int sigNum)
+{
+    if (-1 == ::kill(pid, sigNum)) {
+        const std::string msg = "Error during killing pid: " + std::to_string(pid) +
+                                " sigNum: " + std::to_string(sigNum) +
+                                ": "  + getSystemErrorMessage();
+        LOGE(msg);
+        throw UtilsException(msg);
+    }
+}
+
 } // namespace utils
 
 
index 3ecf166..f4a42e3 100644 (file)
@@ -36,6 +36,7 @@ void signalBlockAllExcept(const std::initializer_list<int>& signals);
 void signalBlock(const int sigNum);
 void signalUnblock(const int sigNum);
 void signalIgnore(const std::initializer_list<int>& signals);
+void sendSignal(const pid_t pid, const int sigNum);
 
 } // namespace utils
 
diff --git a/libs/lxcpp/commands/stop.cpp b/libs/lxcpp/commands/stop.cpp
new file mode 100644 (file)
index 0000000..111ef9f
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ *  Copyright (C) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License version 2.1 as published by the Free Software Foundation.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/**
+ * @file
+ * @author  Jan Olszak (j.olszak@samsung.com)
+ * @brief   Implementation of stopping a container
+ */
+
+#include "lxcpp/commands/stop.hpp"
+#include "lxcpp/exception.hpp"
+#include "lxcpp/process.hpp"
+
+#include "logger/logger.hpp"
+#include "utils/signal.hpp"
+
+namespace lxcpp {
+
+Stop::Stop(ContainerConfig &config)
+    : mConfig(config)
+{
+}
+
+Stop::~Stop()
+{
+}
+
+void Stop::execute()
+{
+    LOGD("Stopping container: " << mConfig.mName);
+
+    // TODO: Use initctl/systemd-initctl if available in container
+
+    utils::sendSignal(mConfig.mInitPid, SIGTERM);
+}
+
+} // namespace lxcpp
diff --git a/libs/lxcpp/commands/stop.hpp b/libs/lxcpp/commands/stop.hpp
new file mode 100644 (file)
index 0000000..291a0cd
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ *  Copyright (C) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License version 2.1 as published by the Free Software Foundation.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/**
+ * @file
+ * @author  Jan Olszak (j.olszak@samsung.com)
+ * @brief   Implementation of stopping a container
+ */
+
+#ifndef LXCPP_COMMANDS_STOP_HPP
+#define LXCPP_COMMANDS_STOP_HPP
+
+#include "lxcpp/commands/command.hpp"
+#include "lxcpp/container-config.hpp"
+
+#include <sys/types.h>
+
+
+namespace lxcpp {
+
+
+class Stop final: Command {
+public:
+    /**
+     * Stops the container
+     *
+     * @param config container's config
+     */
+    Stop(ContainerConfig &config);
+    ~Stop();
+
+    void execute();
+
+private:
+    ContainerConfig &mConfig;
+};
+
+
+} // namespace lxcpp
+
+
+#endif // LXCPP_COMMANDS_STOP_HPP
index 9ff4b52..d3258aa 100644 (file)
@@ -29,6 +29,7 @@
 #include "lxcpp/capability.hpp"
 #include "lxcpp/commands/attach.hpp"
 #include "lxcpp/commands/start.hpp"
+#include "lxcpp/commands/stop.hpp"
 #include "lxcpp/commands/prep-host-terminal.hpp"
 
 #include "logger/logger.hpp"
@@ -187,7 +188,8 @@ void ContainerImpl::stop()
     // TODO: things to do when shuttting down the container:
     // - close PTY master FDs from the config so we won't keep PTYs open
 
-    throw NotImplementedException();
+    Stop stop(mConfig);
+    stop.execute();
 }
 
 void ContainerImpl::freeze()
index 0c731b5..d0418af 100644 (file)
@@ -54,8 +54,8 @@ void setProcTitle(const std::string &title)
 
     // Skip the first 47 fields, entries 48-49 are ARG_START and ARG_END.
     std::advance(it, 47);
-    unsigned long argStart = std::stol(*it++);
-    unsigned long argEnd = std::stol(*it++);
+    unsigned long argStart = std::stoul(*it++);
+    unsigned long argEnd = std::stoul(*it);
 
     f.close();
 
index ff72b94..1c1a76c 100644 (file)
@@ -109,14 +109,15 @@ BOOST_AUTO_TEST_CASE(SetLogger)
                       BadArgument);
 }
 
-// BOOST_AUTO_TEST_CASE(StartStop)
-// {
-//     auto c = std::unique_ptr<Container>(createContainer("StartStop", "/"));
-//     BOOST_CHECK_NO_THROW(c->setInit(COMMAND));
-//     BOOST_CHECK_NO_THROW(c->setLogger(logger::LogType::LOG_PERSISTENT_FILE,
-//                                       logger::LogLevel::DEBUG,
-//                                       LOGGER_FILE));
-//     BOOST_CHECK_NO_THROW(c->start());
-// }
+BOOST_AUTO_TEST_CASE(StartStop)
+{
+    auto c = std::unique_ptr<Container>(createContainer("StartStop", "/"));
+    BOOST_CHECK_NO_THROW(c->setInit(COMMAND));
+    BOOST_CHECK_NO_THROW(c->setLogger(logger::LogType::LOG_PERSISTENT_FILE,
+                                      logger::LogLevel::DEBUG,
+                                      LOGGER_FILE));
+    BOOST_CHECK_NO_THROW(c->start());
+    BOOST_CHECK_NO_THROW(c->stop());
+}
 
 BOOST_AUTO_TEST_SUITE_END()