Add systemd socket
authorSangwan Kwon <sangwan.kwon@samsung.com>
Tue, 31 Dec 2019 04:39:36 +0000 (13:39 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Fri, 3 Jan 2020 07:58:41 +0000 (16:58 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
packaging/vist.spec
src/vist/CMakeLists.txt
src/vist/transport/systemd-socket.hpp [new file with mode: 0644]

index 247bc98..4f18b9f 100644 (file)
@@ -23,6 +23,7 @@ BuildRequires: pkgconfig(openssl)
 BuildRequires: pkgconfig(dlog)
 BuildRequires: pkgconfig(sqlite3)
 BuildRequires: pkgconfig(libtzplatform-config)
+BuildRequires: pkgconfig(libsystemd-daemon)
 Requires: glog
 Requires: gflag
 Requires: boost-regex boost-system boost-thread boost-filesystem
index 65b8c3c..bf217bd 100644 (file)
@@ -21,7 +21,10 @@ SET(${TARGET_VIST_LIB}_TESTS "")
 SET(${TARGET_VIST_COMMON_LIB}_SRCS "")
 
 IF(DEFINED GBS_BUILD)
-       SET(DEPENDENCY sqlite3 dlog gflags)
+       SET(DEPENDENCY sqlite3
+                                  dlog
+                                  gflags
+                                  libsystemd-daemon)
        PKG_CHECK_MODULES(VIST_COMMON_DEPS REQUIRED ${DEPENDENCY})
        INCLUDE_DIRECTORIES(${VIST_COMMON_DEPS_INCLUDE_DIRS})
        ADD_DEFINITIONS(-DTIZEN="TIZEN")
diff --git a/src/vist/transport/systemd-socket.hpp b/src/vist/transport/systemd-socket.hpp
new file mode 100644 (file)
index 0000000..4d768ed
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ *  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 <vist/logger.hpp>
+#include <vist/exception.hpp>
+
+#include <systemd/sd-daemon.h>
+
+namespace vist {
+namespace transport {
+
+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 transport
+} // namespace vist