Command line interface to SCS 01/28701/5
authorMateusz Malicki <m.malicki2@samsung.com>
Thu, 9 Oct 2014 17:10:11 +0000 (19:10 +0200)
committerMateusz Malicki <m.malicki2@samsung.com>
Thu, 16 Oct 2014 14:10:41 +0000 (16:10 +0200)
[Feature]      Command line interface to SCS
[Cause]        Need to manage SCS from shell
[Solution]     Binary that use libsecurity-containers
[Verification] Build, install, execute security-containers-cli (switch container),
               check SCS logs.

Change-Id: Ia6cc1cc00295e19befd2e0987900b69e2d4e7bd3

CMakeLists.txt
cli/CMakeLists.txt [new file with mode: 0644]
cli/command-line-interface.cpp [new file with mode: 0644]
cli/command-line-interface.hpp [new file with mode: 0644]
cli/main.cpp [new file with mode: 0644]
doc/doxygen.cfg
packaging/security-containers.spec

index c8e74c6..f824f8f 100644 (file)
@@ -99,6 +99,7 @@ SET(CONTAINER_SUPPORT_FOLDER ${PROJECT_SOURCE_DIR}/container-support)
 SET(CONTAINER_DAEMON_FOLDER ${PROJECT_SOURCE_DIR}/container-daemon)
 SET(TESTS_FOLDER ${PROJECT_SOURCE_DIR}/tests)
 SET(UNIT_TESTS_FOLDER ${TESTS_FOLDER}/unit_tests)
+SET(CLI_FOLDER ${PROJECT_SOURCE_DIR}/cli)
 
 IF(NOT DEFINED SYSCONF_INSTALL_DIR)
     SET(SYSCONF_INSTALL_DIR "/etc")
@@ -132,4 +133,5 @@ ADD_SUBDIRECTORY(${SERVER_FOLDER})
 ADD_SUBDIRECTORY(${CONTAINER_SUPPORT_FOLDER})
 ADD_SUBDIRECTORY(${CONTAINER_DAEMON_FOLDER})
 ADD_SUBDIRECTORY(${TESTS_FOLDER})
+ADD_SUBDIRECTORY(${CLI_FOLDER})
 
diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt
new file mode 100644 (file)
index 0000000..076dd94
--- /dev/null
@@ -0,0 +1,37 @@
+# Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+#
+#    Licensed under the Apache License, Version 2.0 (the "License");
+#    you may not use this file except in compliance with the License.
+#    You may obtain a copy of the License at
+#
+#        http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS,
+#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#    See the License for the specific language governing permissions and
+#    limitations under the License.
+#
+#
+# @file   CMakeLists.txt
+# @author Mateusz Malicki (m.malicki2@samsung.com)
+#
+
+MESSAGE(STATUS "Generating makefile for the command line interface...")
+FILE(GLOB cli_SRCS *.cpp *.hpp)
+
+## Setup target ################################################################
+SET(CLI_CODENAME "${PROJECT_NAME}-cli")
+ADD_EXECUTABLE(${CLI_CODENAME} ${cli_SRCS})
+
+
+## Link libraries ##############################################################
+PKG_CHECK_MODULES(LIB_DEPS REQUIRED security-containers)
+
+INCLUDE_DIRECTORIES(${CLIENT_FOLDER})
+INCLUDE_DIRECTORIES(${COMMON_FOLDER})
+TARGET_LINK_LIBRARIES(${CLI_CODENAME} ${LIB_DEPS_LIBRARIES} ${PROJECT_NAME})
+
+
+## Install #####################################################################
+INSTALL(TARGETS ${CLI_CODENAME} DESTINATION bin)
diff --git a/cli/command-line-interface.cpp b/cli/command-line-interface.cpp
new file mode 100644 (file)
index 0000000..02c867f
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Mateusz Malicki <m.malicki2@samsung.com>
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+/**
+ * @file
+ * @author  Mateusz Malicki (m.malicki2@samsung.com)
+ * @brief   Definition of CommandLineInterface class
+ */
+
+#include "config.hpp"
+#include "command-line-interface.hpp"
+#include <security-containers-client.h>
+
+#include <map>
+#include <stdexcept>
+#include <functional>
+#include <ostream>
+
+using namespace std;
+
+namespace security_containers {
+namespace cli {
+
+namespace {
+
+/**
+ * Invoke specific function on ScClient
+ *
+ * @param fun Function to be called. It must not throw any exception.
+ */
+void one_shot(const function<ScStatus(ScClient)>& fun)
+{
+    string msg;
+    ScStatus status;
+    ScClient client;
+
+    status = sc_start_glib_loop();
+    if (SCCLIENT_SUCCESS != status) {
+        throw runtime_error("Can't start glib loop");
+    }
+
+    client = sc_client_create();
+    if (NULL == client) {
+        msg = "Can't create client";
+        goto finish;
+    }
+
+    status = sc_connect(client);
+    if (SCCLIENT_SUCCESS != status) {
+        msg = sc_get_status_message(client);
+        goto finish;
+    }
+
+    status = fun(client);
+    if (SCCLIENT_SUCCESS != status) {
+        msg = sc_get_status_message(client);
+        goto finish;
+    }
+
+finish:
+    sc_client_free(client);
+    sc_stop_glib_loop();
+    if (! msg.empty()) {
+        throw runtime_error(msg);
+    }
+}
+
+} // namespace
+
+void CommandLineInterface::printUsage(std::ostream& out) const
+{
+    out << mUsage << "\n\n"
+        << "\tDescription\n"
+        << "\t\t" << mUsageInfo << "\n\n"
+        << "\tOptions\n";
+    for (const auto& args : mArgsSpec) {
+        out << "\t\t" << args.first << " -- " << args.second << "\n";
+    }
+    out << "\n";
+}
+
+void CommandLineInterface::execute(int pos, int argc, const char** argv)
+{
+    mExecutorCallback(pos, argc, argv);
+}
+
+
+void set_active_container(int pos, int argc, const char** argv)
+{
+    using namespace std::placeholders;
+
+    if (argc <= pos + 1) {
+        throw runtime_error("Not enough parameters");
+    }
+
+    one_shot(bind(sc_set_active_container, _1, argv[pos + 1]));
+}
+
+} // namespace cli
+} // namespace security_containers
diff --git a/cli/command-line-interface.hpp b/cli/command-line-interface.hpp
new file mode 100644 (file)
index 0000000..c193068
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Mateusz Malicki <m.malicki2@samsung.com>
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+/**
+ * @file
+ * @author  Mateusz Malicki (m.malicki2@samsung.com)
+ * @brief   Declaration of CommandLineInterface class
+ */
+#ifndef CLI_COMMAND_LINE_INTERFACE_HPP
+#define CLI_COMMAND_LINE_INTERFACE_HPP
+
+#include <map>
+#include <functional>
+#include <ostream>
+#include <string>
+
+namespace security_containers {
+namespace cli {
+
+/**
+ * Class that implements command pattern.
+ */
+class CommandLineInterface {
+
+public:
+    /**
+     * @see CommandLineInterface::execute
+     */
+    typedef std::function<void(int, int, const char**)> ExecutorCallback;
+
+    /**
+     * @see CommandLineInterface::CommandLineInterface
+     */
+    typedef std::map<std::string, std::string> ArgsSpec;
+
+    /**
+     * Dummy constructor (for stl usage)
+     */
+    CommandLineInterface() {}
+
+    /**
+     *  Construct command
+     *
+     *  @param executorCallback Callback function that will do the job
+     *  @param usage Description of use
+     *  @param usageInfo Description of the command
+     *  @param argsSpec Description of arguments
+     */
+    CommandLineInterface(const ExecutorCallback& executorCallback,
+                    const std::string& usage,
+                    const std::string& usageInfo,
+                    const ArgsSpec& argsSpec)
+        : mExecutorCallback(executorCallback),
+          mUsage(usage),
+          mUsageInfo(usageInfo),
+          mArgsSpec(argsSpec) {}
+
+    /**
+     * Print usage to stream
+     *
+     * @param out Output stream
+     */
+    void printUsage(std::ostream& out) const;
+
+    /**
+     * Do the work
+     *
+     * It calls the callback passed in constructor
+     *
+     * @param pos Points to element in argv where command was recognized (i.e. command name)
+     * @param argc Number of elements in argv
+     * @param argv Command line arguments
+     */
+    void execute(int pos, int argc, const char** argv);
+
+
+private:
+    const ExecutorCallback mExecutorCallback;
+    const std::string mUsage;
+    const std::string mUsageInfo;
+    const ArgsSpec mArgsSpec;
+};
+
+/**
+ * Parses command line arguments and call sc_set_active_container
+ *
+ * @see sc_set_active_container
+ */
+void set_active_container(int pos, int argc, const char** argv);
+
+} // namespace cli
+} // namespace security_containers
+
+#endif /* CLI_COMMAND_LINE_INTERFACE_HPP */
diff --git a/cli/main.cpp b/cli/main.cpp
new file mode 100644 (file)
index 0000000..e74f620
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Mateusz Malicki <m.malicki2@samsung.com>
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+/**
+ * @file
+ * @author  Mateusz Malicki (m.malicki2@samsung.com)
+ * @brief   Declaration of CommandLineInterface class
+ */
+
+#include "command-line-interface.hpp"
+
+#include <cstdlib>
+#include <map>
+#include <stdexcept>
+#include <string>
+#include <iostream>
+
+using namespace security_containers::cli;
+
+std::map<std::string, CommandLineInterface> commands = {
+    {"set_active_container", {
+        set_active_container,
+        "set_active_container container_id",
+        "Set active (foreground) container",
+        {{"container_id", "id container name"}}}
+    }
+};
+
+void printUsage(std::ostream& out, const std::string& name)
+{
+    out << "Usage: " << name << " [command [args]]\n\n"
+        << "command can be one of the following:\n";
+
+    for (const auto& command : commands) {
+        command.second.printUsage(out);
+    }
+}
+
+int main(const int argc, const char** argv)
+{
+    if (argc < 2) {
+        printUsage(std::cout, argv[0]);
+        return EXIT_FAILURE;
+    }
+    if (commands.count(argv[1]) == 0) {
+        printUsage(std::cout, argv[0]);
+        return EXIT_FAILURE;
+    }
+
+    CommandLineInterface& command = commands[argv[1]];
+    try {
+         command.execute(1, argc, argv);
+    } catch (const std::runtime_error& ex) {
+        std::cerr << ex.what() << std::endl;
+        return EXIT_FAILURE;
+    }
+    return EXIT_SUCCESS;
+}
+
index 7e304e5..e997623 100644 (file)
@@ -647,7 +647,7 @@ WARN_LOGFILE           =
 # directories like "/usr/src/myproject". Separate the files or directories
 # with spaces.
 
-INPUT                  = ../common ../client ../server
+INPUT                  = ../common ../client ../server ../cli
 
 # This tag can be used to specify the character encoding of the source files
 # that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is
index 3905904..137f02b 100644 (file)
@@ -186,6 +186,20 @@ Daemon running inside every container.
 /etc/dbus-1/system.d/org.tizen.containers.domain.daemon.conf
 
 
+## Command Line Interface ######################################################
+%package cli
+Summary:          Security Containers Command Line Interface
+Group:            Security/Other
+Requires:         security-containers-client = %{version}-%{release}
+
+%description cli
+Command Line Interface for security-containers.
+
+%files cli
+%defattr(644,root,root,755)
+%attr(755,root,root) %{_bindir}/security-containers-cli
+
+
 ## Test Package ################################################################
 %package tests
 Summary:          Security Containers Tests