Apply server-client feature
authorSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 21 Oct 2019 08:57:38 +0000 (17:57 +0900)
committerSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 21 Oct 2019 08:57:38 +0000 (17:57 +0900)
- Add daemon named vistd
- Expose client api "Query::Execute()"

Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
19 files changed:
packaging/vist.spec
src/vist/CMakeLists.txt
src/vist/client/CMakeLists.txt [new file with mode: 0644]
src/vist/client/query.cpp [new file with mode: 0644]
src/vist/client/query.h [new file with mode: 0644]
src/vist/client/tests/client_tests.cpp [new file with mode: 0644]
src/vist/common/CMakeLists.txt [new file with mode: 0644]
src/vist/common/ipc/client.h [new file with mode: 0644]
src/vist/common/ipc/server.h [new file with mode: 0644]
src/vist/common/ipc/tests/ipc_tests.cpp [new file with mode: 0644]
src/vist/ipc/CMakeLists.txt [deleted file]
src/vist/ipc/client.h [deleted file]
src/vist/ipc/server.h [deleted file]
src/vist/ipc/tests/ipc_tests.cpp [deleted file]
src/vist/main/main.cpp [new file with mode: 0644]
src/vist/service/CMakeLists.txt [new file with mode: 0644]
src/vist/service/tests/daemon_tests.cpp [new file with mode: 0644]
src/vist/service/vistd.cpp [new file with mode: 0644]
src/vist/service/vistd.h [new file with mode: 0644]

index 4a4ad7eaafd91212320545a746f71b254c1c4a44..f88f53cd249e595b2c582e314871858adbfc35ce 100644 (file)
@@ -91,6 +91,7 @@ rm -rf %{buildroot}
 %license LICENSE-Apache-2.0
 %license LICENSE-GPL-2.0
 %license LICENSE-MIT
+%{_bindir}/vistd
 %{vist_script_dir}/*.sql
 %dir %attr(-, %{user_name}, %{group_name}) %{vist_db_dir}
 %dir %attr(-, %{user_name}, %{group_name}) %{vist_plugin_dir}
index 203118a5a523374bf4f62eb17394bb15612798c4..db63cb9d05cdb206963bca213d2e07a7ffa388ac 100644 (file)
 #  See the License for the specific language governing permissions and
 #  limitations under the License
 
+SET(TARGET_VIST_DAEMON vistd)
 SET(TARGET_VIST_TEST vist-test)
 
 SET(${TARGET_VIST_LIB}_SRCS "")
 SET(${TARGET_VIST_LIB}_DEPS "")
 SET(${TARGET_VIST_LIB}_TESTS "")
 
-ADD_SUBDIRECTORY(ipc)
+INCLUDE_DIRECTORIES(. common)
+
+ADD_SUBDIRECTORY(client)
+ADD_SUBDIRECTORY(common)
 ADD_SUBDIRECTORY(manager)
 ADD_SUBDIRECTORY(notification)
 ADD_SUBDIRECTORY(property)
+ADD_SUBDIRECTORY(service)
 
 ADD_LIBRARY(${TARGET_VIST_LIB} STATIC ${${TARGET_VIST_LIB}_SRCS})
 TARGET_LINK_LIBRARIES(${TARGET_VIST_LIB} ${${TARGET_VIST_LIB}_DEPS}
                                                                                 ${TARGET_OSQUERY_LIB})
 
+ADD_EXECUTABLE(${TARGET_VIST_DAEMON} main/main.cpp)
+TARGET_LINK_LIBRARIES(${TARGET_VIST_DAEMON} ${TARGET_VIST_LIB})
+TARGET_LINK_WHOLE(${TARGET_VIST_DAEMON} ${TARGET_OSQUERY_LIB})
+INSTALL(TARGETS ${TARGET_VIST_DAEMON}
+               DESTINATION ${CMAKE_INSTALL_BINDIR}
+               PERMISSIONS OWNER_READ
+                                       OWNER_WRITE
+                                       OWNER_EXECUTE
+                                       GROUP_READ
+                                       GROUP_EXECUTE
+                                       WORLD_READ
+                                       WORLD_EXECUTE)
+
 ADD_EXECUTABLE(${TARGET_VIST_TEST} main/tests.cpp
                                                                   ${${TARGET_VIST_LIB}_TESTS})
 TARGET_LINK_LIBRARIES(${TARGET_VIST_TEST} ${TARGET_VIST_LIB}
diff --git a/src/vist/client/CMakeLists.txt b/src/vist/client/CMakeLists.txt
new file mode 100644 (file)
index 0000000..821d857
--- /dev/null
@@ -0,0 +1,18 @@
+#  Copyright (c) 2019 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
+
+ADD_VIST_LIBRARY(vist_client query.cpp)
+
+FILE(GLOB CLIENT_TESTS "tests/*.cpp")
+ADD_VIST_TEST(${CLIENT_TESTS})
diff --git a/src/vist/client/query.cpp b/src/vist/client/query.cpp
new file mode 100644 (file)
index 0000000..29783ad
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ *  Copyright (c) 2019 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
+ */
+
+#include "query.h"
+
+#include "ipc/client.h" 
+
+namespace {
+       const std::string SOCK_ADDR = "/tmp/.vistd";
+} // anonymous namespace
+
+namespace vist {
+
+Rows Query::Execute(const std::string& statement)
+{
+       auto& client = ipc::Client::Instance(SOCK_ADDR);
+
+       return client->methodCall<Rows>("Vistd::query", statement);
+}
+
+} // namespace vist
diff --git a/src/vist/client/query.h b/src/vist/client/query.h
new file mode 100644 (file)
index 0000000..ccce173
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ *  Copyright (c) 2019 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
+ */
+
+#pragma once
+
+#include <map>
+#include <string>
+#include <vector>
+
+namespace vist {
+
+using Row = std::map<std::string, std::string>;
+using Rows = std::vector<Row>;
+
+struct Query final {
+       static Rows Execute(const std::string& statement);
+};
+
+} // namespace vist
diff --git a/src/vist/client/tests/client_tests.cpp b/src/vist/client/tests/client_tests.cpp
new file mode 100644 (file)
index 0000000..d05d5db
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ *  Copyright (c) 2019 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
+ */
+
+#include <gtest/gtest.h>
+
+#include "../query.h" 
+
+#include <chrono>
+#include <thread>
+
+using namespace vist;
+
+class ClientTests : public testing::Test {};
+
+TEST_F(ClientTests, query) {
+       auto rows = Query::Execute("SELECT * FROM policy");
+
+       EXPECT_TRUE(rows.size() > 0);
+}
diff --git a/src/vist/common/CMakeLists.txt b/src/vist/common/CMakeLists.txt
new file mode 100644 (file)
index 0000000..717a17f
--- /dev/null
@@ -0,0 +1,16 @@
+#  Copyright (c) 2019 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(GLOB IPC_TESTS "ipc/tests/*.cpp")
+ADD_VIST_TEST(${IPC_TESTS})
diff --git a/src/vist/common/ipc/client.h b/src/vist/common/ipc/client.h
new file mode 100644 (file)
index 0000000..5e39a57
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ *  Copyright (c) 2019 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
+ */
+
+#pragma once
+
+#include <string>
+#include <memory>
+
+#include <klay/rmi/client.h>
+
+namespace vist {
+namespace ipc {
+
+class Client final {
+public:
+       Client(const Client&) = delete;
+       Client& operator=(const Client&) = delete;
+
+       Client(Client&&) = delete;
+       Client& operator=(Client&&) = delete;
+
+       static std::unique_ptr<klay::rmi::Client>& Instance(const std::string& sock)
+       {
+               static Client client(sock);
+               return client.instance;
+       }
+
+private:
+       explicit Client(const std::string& sock) :
+               instance(std::make_unique<klay::rmi::Client>(sock))
+       {
+               instance->connect();
+       }
+       ~Client() = default;
+
+       std::unique_ptr<klay::rmi::Client> instance;
+};
+
+} // namespace ipc
+} // namespace vist
diff --git a/src/vist/common/ipc/server.h b/src/vist/common/ipc/server.h
new file mode 100644 (file)
index 0000000..667d6b1
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ *  Copyright (c) 2019 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
+ */
+
+#pragma once
+
+#include <string>
+#include <memory>
+
+#include <klay/rmi/service.h>
+
+namespace vist {
+namespace ipc {
+
+class Server final {
+public:
+       Server(const Server&) = delete;
+       Server& operator=(const Server&) = delete;
+
+       Server(Server&&) = delete;
+       Server& operator=(Server&&) = delete;
+
+       static std::unique_ptr<klay::rmi::Service>& Instance(const std::string& sock)
+       {
+               static Server server(sock);
+               return server.instance;
+       }
+
+private:
+       explicit Server(const std::string& sock) :
+               instance(std::make_unique<klay::rmi::Service>(sock)) {}
+       ~Server() = default;
+
+       std::unique_ptr<klay::rmi::Service> instance;
+};
+
+} // namespace ipc
+} // namespace vist
diff --git a/src/vist/common/ipc/tests/ipc_tests.cpp b/src/vist/common/ipc/tests/ipc_tests.cpp
new file mode 100644 (file)
index 0000000..e9f656e
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ *  Copyright (c) 2019 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
+ */
+
+#include <gtest/gtest.h>
+
+#include "ipc/server.h"
+#include "ipc/client.h"
+
+#include <chrono>
+#include <thread>
+
+#include <sys/types.h>
+#include <unistd.h>
+
+using namespace vist;
+
+namespace {
+       std::string g_socket = "/tmp/vist-test";
+} // anonymous namespace
+
+class TestServer {
+public:
+       void init()
+       {
+               auto& server = ipc::Server::Instance(g_socket);
+               server->expose(this, "", (std::string)(TestServer::testMethod)());
+               server->start();
+       }
+
+       std::string testMethod()
+       {
+               return "test";
+       }
+};
+
+class IpcTests : public testing::Test {
+public:
+       void SetUp() override
+       {
+               ::unlink(g_socket.c_str());
+
+               this->pid = fork();
+               if (pid < 0) {
+                       return;
+               } else if (pid == 0) {
+                       TestServer server;
+                       server.init();
+                       
+               }
+
+               std::this_thread::sleep_for(std::chrono::seconds(1));
+       }
+
+       void TearDown() override
+       {
+               if (::kill(pid, SIGTERM) == -1)
+                       return;
+
+               std::this_thread::sleep_for(std::chrono::seconds(1));
+       }
+
+private:
+       pid_t pid = -1;
+};
+
+TEST_F(IpcTests, method_call) {
+       auto& client = ipc::Client::Instance(g_socket);
+
+       std::string ret = client->methodCall<std::string>("TestServer::testMethod");
+       EXPECT_EQ(ret, "test");
+}
diff --git a/src/vist/ipc/CMakeLists.txt b/src/vist/ipc/CMakeLists.txt
deleted file mode 100644 (file)
index 68418d1..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-#  Copyright (c) 2019 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(GLOB SERVER_TESTS "tests/*.cpp")
-ADD_VIST_TEST(${SERVER_TESTS})
diff --git a/src/vist/ipc/client.h b/src/vist/ipc/client.h
deleted file mode 100644 (file)
index ddf7a5c..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- *  Copyright (c) 2019 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
- */
-
-#pragma once
-
-#include <string>
-#include <memory>
-
-#include <klay/rmi/client.h>
-
-namespace ipc {
-
-class Client final {
-public:
-       Client(const Client&) = delete;
-       Client& operator=(const Client&) = delete;
-
-       Client(Client&&) = delete;
-       Client& operator=(Client&&) = delete;
-
-       static std::unique_ptr<klay::rmi::Client>& Instance(const std::string& sock)
-       {
-               static Client client(sock);
-               return client.instance;
-       }
-
-private:
-       explicit Client(const std::string& sock) :
-               instance(std::make_unique<klay::rmi::Client>(sock))
-       {
-               instance->connect();
-       }
-       ~Client() = default;
-
-       std::unique_ptr<klay::rmi::Client> instance;
-};
-
-} // namespace ipc
diff --git a/src/vist/ipc/server.h b/src/vist/ipc/server.h
deleted file mode 100644 (file)
index 5bbcc49..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- *  Copyright (c) 2019 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
- */
-
-#pragma once
-
-#include <string>
-#include <memory>
-
-#include <klay/rmi/service.h>
-
-namespace ipc {
-
-class Server final {
-public:
-       Server(const Server&) = delete;
-       Server& operator=(const Server&) = delete;
-
-       Server(Server&&) = delete;
-       Server& operator=(Server&&) = delete;
-
-       static std::unique_ptr<klay::rmi::Service>& Instance(const std::string& sock)
-       {
-               static Server server(sock);
-               return server.instance;
-       }
-
-private:
-       explicit Server(const std::string& sock) :
-               instance(std::make_unique<klay::rmi::Service>(sock)) {}
-       ~Server() = default;
-
-       std::unique_ptr<klay::rmi::Service> instance;
-};
-
-} // namespace ipc
diff --git a/src/vist/ipc/tests/ipc_tests.cpp b/src/vist/ipc/tests/ipc_tests.cpp
deleted file mode 100644 (file)
index 40b7451..0000000
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- *  Copyright (c) 2019 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
- */
-
-#include <gtest/gtest.h>
-
-#include "../server.h"
-#include "../client.h"
-
-#include <chrono>
-#include <thread>
-
-#include <sys/types.h>
-#include <unistd.h>
-
-using namespace ipc;
-
-namespace {
-       std::string g_socket = "/tmp/vist-test";
-
-       void init()
-       {
-       }
-
-} // anonymous namespace
-
-class TestServer {
-public:
-       void init()
-       {
-               auto& server = ipc::Server::Instance(g_socket);
-               server->expose(this, "", (std::string)(TestServer::testMethod)());
-               server->start();
-       }
-
-       std::string testMethod()
-       {
-               return "test";
-       }
-};
-
-class IpcTests : public testing::Test {
-public:
-       void SetUp() override
-       {
-               ::unlink(g_socket.c_str());
-
-               this->pid = fork();
-               if (pid < 0) {
-                       return;
-               } else if (pid == 0) {
-                       TestServer server;
-                       server.init();
-                       
-               }
-
-               std::this_thread::sleep_for(std::chrono::seconds(1));
-       }
-
-       void TearDown() override
-       {
-               if (::kill(pid, SIGTERM) == -1)
-                       return;
-
-               std::this_thread::sleep_for(std::chrono::seconds(1));
-       }
-
-private:
-       pid_t pid = -1;
-};
-
-TEST_F(IpcTests, method_call) {
-       auto& client = ipc::Client::Instance(g_socket);
-
-       std::string ret = client->methodCall<std::string>("TestServer::testMethod");
-       EXPECT_EQ(ret, "test");
-}
diff --git a/src/vist/main/main.cpp b/src/vist/main/main.cpp
new file mode 100644 (file)
index 0000000..edee3f4
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ *  Copyright (c) 2019 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
+ */
+
+#include "service/vistd.h"
+
+#include <cstdlib>
+#include <stdexcept>
+
+int main() try
+{
+       vist::Vistd vistd;
+       vistd.start();
+
+       return EXIT_SUCCESS;
+} catch(const std::exception&)
+{
+       return EXIT_FAILURE;
+}
diff --git a/src/vist/service/CMakeLists.txt b/src/vist/service/CMakeLists.txt
new file mode 100644 (file)
index 0000000..d02ecd1
--- /dev/null
@@ -0,0 +1,18 @@
+#  Copyright (c) 2019 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
+
+ADD_VIST_LIBRARY(vist_daemon vistd.cpp)
+
+FILE(GLOB DAEMON_TESTS "tests/*.cpp")
+ADD_VIST_TEST(${DAEMON_TESTS})
diff --git a/src/vist/service/tests/daemon_tests.cpp b/src/vist/service/tests/daemon_tests.cpp
new file mode 100644 (file)
index 0000000..0de3101
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ *  Copyright (c) 2019 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
+ */
+
+#include <gtest/gtest.h>
+
+#include "../vistd.h" 
+#include "ipc/client.h"
+
+#include <chrono>
+#include <thread>
+
+using namespace vist;
+
+class DaemonTests : public testing::Test {};
+
+TEST_F(DaemonTests, query) {
+       Vistd vistd;
+       auto rows = vistd.query("SELECT * FROM policy");
+
+       EXPECT_TRUE(rows.size() > 0);
+}
diff --git a/src/vist/service/vistd.cpp b/src/vist/service/vistd.cpp
new file mode 100644 (file)
index 0000000..3b4ec1e
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ *  Copyright (c) 2019 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
+ */
+
+#include "vistd.h"
+#include "ipc/server.h"
+
+#include <stdexcept>
+
+#include <osquery/registry.h>
+#include <osquery/sql.h>
+#include <osquery/status.h>
+
+#define QUERY_RET_TYPE std::vector<std::map<std::string, std::string>>
+
+namespace {
+       const std::string SOCK_ADDR = "/tmp/.vistd";
+} // anonymous namespace
+
+namespace vist {
+
+Vistd::Vistd()
+{
+       osquery::registryAndPluginInit();
+}
+
+void Vistd::start()
+{
+       auto& server = ipc::Server::Instance(SOCK_ADDR);
+
+       server->expose(this, "", (QUERY_RET_TYPE)(Vistd::query)(std::string));
+       server->start();
+}
+
+Rows Vistd::query(const std::string& statement)
+{
+       osquery::SQL sql(statement, true);
+       if (!sql.ok())
+               throw std::runtime_error("Faild to execute query: " + sql.getMessageString());
+
+       return std::move(sql.rows());
+}
+
+} // namespace vist
diff --git a/src/vist/service/vistd.h b/src/vist/service/vistd.h
new file mode 100644 (file)
index 0000000..897e9f3
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ *  Copyright (c) 2019 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
+ */
+
+#pragma once
+
+#include <map>
+#include <string>
+#include <vector>
+
+namespace vist {
+
+using Row = std::map<std::string, std::string>;
+using Rows = std::vector<Row>;
+
+class Vistd final {
+public:
+       explicit Vistd();
+
+       Rows query(const std::string& statement);
+       void start();
+};
+
+} // namespace vist