From 1a680bb1d2592a4110ca5d026c06dd11222d4e7c Mon Sep 17 00:00:00 2001 From: Zofia Abramowska Date: Fri, 10 Apr 2020 12:47:47 +0200 Subject: [PATCH] Add listing running apps based on namespace Change-Id: I8240646edef06fc267cc4a2177764494ec081fdb --- packaging/security-manager.spec | 1 + test/CMakeLists.txt | 15 ++++++++++++ test/list-running-apps-ns.cpp | 51 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 test/list-running-apps-ns.cpp diff --git a/packaging/security-manager.spec b/packaging/security-manager.spec index 11100a1..f21a196 100644 --- a/packaging/security-manager.spec +++ b/packaging/security-manager.spec @@ -333,6 +333,7 @@ chsmack -a System %{db_test_dir}/.security-manager-test-rules*.txt %attr(755,root,root) %{_bindir}/security-manager-unit-tests %attr(755,root,root) %{_bindir}/security-manager-performance-tests %attr(755,root,root) %{_bindir}/security-manager-test-rules-loader +%attr(755,root,root) %{_bindir}/list-running-apps-ns %attr(0600,root,root) %{db_test_dir}/.security-manager-test.db %attr(0600,root,root) %{db_test_dir}/.security-manager-test.db-journal %attr(0600,root,root) %{db_test_dir}/.security-manager-test-v0.db diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index e73f0e0..3ccaacf 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -179,3 +179,18 @@ TARGET_LINK_LIBRARIES(${TARGET_SM_PERFORMANCE_TESTS} ) INSTALL(TARGETS ${TARGET_SM_TESTS} ${TARGET_SM_PERFORMANCE_TESTS} DESTINATION ${BIN_INSTALL_DIR}) + +########################### list running apps based on namespace ################################ + +SET(LIST_SOURCES + ${SM_TEST_SRC}/list-running-apps-ns.cpp) + +SET(LIST_TARGET "list-running-apps-ns") + +ADD_EXECUTABLE(${LIST_TARGET} ${LIST_SOURCES}) + +TARGET_LINK_LIBRARIES(${LIST_TARGET} + boost_regex + boost_filesystem) + +INSTALL(TARGETS ${LIST_TARGET} DESTINATION ${BIN_INSTALL_DIR}) diff --git a/test/list-running-apps-ns.cpp b/test/list-running-apps-ns.cpp new file mode 100644 index 0000000..c44d17f --- /dev/null +++ b/test/list-running-apps-ns.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include + +std::string ROOT_DIR = "/run/user"; + +std::string default_regexp = + "/run/user/(\\d*)/apps/(User::Pkg::([^/]*))|(User::Pkg::User::App::([^/]*))"; + +int main(int argc, char **argv) { + std::string regexp = default_regexp; + if (argc > 1) { + regexp = argv[1]; + } + + boost::system::error_code ec; + boost::filesystem::path root(ROOT_DIR); + boost::regex appFilter(regexp); + + if (boost::filesystem::is_directory(root)) { + for (boost::filesystem::recursive_directory_iterator it(root, ec), eit; + it != eit; + it.increment(ec)) + { + if (ec) { + it.pop(); + continue; + } + + boost::smatch matches; + std::string path = it->path().string(); + if (boost::regex_match(path, matches, appFilter)) { + if (matches.size() < 4) { + std::cerr << "Something went wrong with regexp!" << std::endl; + continue; + } + std::string uidStr = matches[1]; + std::string appLabel = matches[2]; + std::string appId = matches[3]; + + std::cout << uidStr << " : " << appLabel << " : " << appId << std::endl; + } + } + } + else { + std::cerr << ROOT_DIR << " is not a directory" << std::endl; + } + return 0; +} -- 2.7.4