Add compile-time switching to RMI socket
authorSangwan Kwon <sangwan.kwon@samsung.com>
Mon, 6 Jan 2020 09:56:16 +0000 (18:56 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Mon, 13 Jan 2020 03:51:29 +0000 (12:51 +0900)
We should apply on-demand service based systemd.
But docker does not support systemd.
So, we need to switch socket at compile-time.

- Docker: boost based socket
- Tizen: systemd based socket

Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/rmi/gateway.cpp
src/vist/rmi/impl/ondemand/server.hpp
src/vist/rmi/impl/ondemand/systemd-socket.hpp [new file with mode: 0644]
src/vist/rmi/remote.cpp

index 72768c7..1a2c2e6 100644 (file)
 #include <vist/exception.hpp>
 #include <vist/rmi/message.hpp>
 #include <vist/rmi/impl/server.hpp>
+
+#ifdef TIZEN
+#include <vist/rmi/impl/ondemand/server.hpp>
+#else
 #include <vist/rmi/impl/general/server.hpp>
+#endif
 
 #include <memory>
 #include <string>
@@ -48,9 +53,13 @@ public:
                        reply.enclose(result);
 
                        return reply;
-               };
+       };
 
+#ifdef TIZEN
+               this->server = std::make_unique<ondemand::Server>(path, dispatcher);
+#else
                this->server = std::make_unique<general::Server>(path, dispatcher);
+#endif
        }
 
        inline void start()
index 5745fbd..5a898e6 100644 (file)
 #include <vist/rmi/impl/ondemand/mainloop.hpp>
 #include <vist/rmi/impl/ondemand/socket.hpp>
 
+#ifdef TIZEN
+#include <vist/rmi/impl/ondemand/systemd-socket.hpp>
+#endif
+
 #include <vist/exception.hpp>
 #include <vist/logger.hpp>
 
@@ -39,7 +43,11 @@ class Server : public interface::Server {
 public:
        Server(const std::string& path, const interface::Task& task) :
                interface::Server(path, task),
+#ifdef TIZEN
+               socket(SystemdSocket::Create(path))
+#else
                socket(path)
+#endif
        {
                this->accept(task);
        }
diff --git a/src/vist/rmi/impl/ondemand/systemd-socket.hpp b/src/vist/rmi/impl/ondemand/systemd-socket.hpp
new file mode 100644 (file)
index 0000000..d952dd0
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ *  Copyright (c) 2020 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 <vist/logger.hpp>
+#include <vist/exception.hpp>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <systemd/sd-daemon.h>
+
+namespace vist {
+namespace rmi {
+namespace impl {
+namespace ondemand {
+
+class SystemdSocket {
+public:
+       static int Create(const std::string& path)
+       {
+               static int fds = -1;
+
+               if (fds == -1)
+                       fds = ::sd_listen_fds(0);
+
+               if (fds < 0)
+                       THROW(ErrCode::RuntimeError) << "Failed to get listened systemd fds.";
+
+               for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + fds; ++fd) {
+                       if (::sd_is_socket_unix(fd, SOCK_STREAM, 1, path.c_str(), 0) > 0) {
+                               INFO(VIST) << "Systemd socket of service is found with fd: " << fd;
+                               return fd;
+                       }
+               }
+
+               THROW(ErrCode::RuntimeError) << "Failed to find listened systemd fds.";
+       }
+};
+
+} // namespace ondemand
+} // namespace impl
+} // namespace rmi
+} // namespace vist
index 8d3b722..9f8923e 100644 (file)
 #include "remote.hpp"
 
 #include <vist/rmi/impl/client.hpp>
+
+#ifdef TIZEN
+#include <vist/rmi/impl/ondemand/client.hpp>
+#else
 #include <vist/rmi/impl/general/client.hpp>
+#endif
 
 #include <memory>
 #include <mutex>
@@ -30,8 +35,13 @@ using namespace vist::rmi::impl;
 
 class Remote::Impl {
 public:
-       explicit Impl(const std::string& path) : client(std::make_unique<general::Client>(path))
+       explicit Impl(const std::string& path)
        {
+#ifdef TIZEN
+               this->client = std::make_unique<ondemand::Client>(path);
+#else
+               this->client = std::make_unique<general::Client>(path);
+#endif
        }
 
        Message request(Message& message)