Make compilation with systemd configurable 69/38369/10
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Fri, 17 Apr 2015 19:19:06 +0000 (21:19 +0200)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Fri, 24 Apr 2015 13:22:11 +0000 (15:22 +0200)
All places where cynara is dependent on systemd are compiled
conditionaly if BUILD_WITH_SYSTEMD is defined.
Cmake checks if systemd packages are available and sets this flag.

Change-Id: I0c0a3fb1601a556131b1ae60ef29131fd483e955

CMakeLists.txt
packaging/cynara.spec
src/CMakeLists.txt
src/common/log/AuditLog.cpp
src/common/log/log.h
src/service/main/main.cpp
src/service/sockets/SocketManager.cpp
src/service/sockets/SocketManager.h

index 3f91d6d..93438f6 100644 (file)
@@ -28,6 +28,18 @@ set(CYNARA_VERSION 0.8.0)
 INCLUDE(FindPkgConfig)
 INCLUDE(CheckCXXCompilerFlag)
 
+########################## search for packages ################################
+
+PKG_CHECK_MODULES(SYSTEMD_DEP
+    QUIET
+    libsystemd-daemon
+    libsystemd-journal
+    )
+
+IF (SYSTEMD_DEP_FOUND)
+    SET(BUILD_WITH_SYSTEMD ON)
+ENDIF (SYSTEMD_DEP_FOUND)
+
 #############################  install dirs  ##################################
 
 SET(LIB_INSTALL_DIR
@@ -83,6 +95,10 @@ IF (CMAKE_BUILD_TYPE MATCHES "DEBUG")
     ADD_DEFINITIONS("-DBUILD_TYPE_DEBUG")
 ENDIF (CMAKE_BUILD_TYPE MATCHES "DEBUG")
 
+IF (BUILD_WITH_SYSTEMD)
+    ADD_DEFINITIONS("-DBUILD_WITH_SYSTEMD")
+ENDIF (BUILD_WITH_SYSTEMD)
+
 SET(TARGET_CYNARA "cynara")
 SET(TARGET_LIB_CYNARA "cynara-client")
 SET(TARGET_LIB_CYNARA_ASYNC "cynara-client-async")
@@ -102,9 +118,13 @@ SET(TARGET_CHSGEN "cynara-db-chsgen")
 
 ADD_SUBDIRECTORY(src)
 ADD_SUBDIRECTORY(pkgconfig)
-ADD_SUBDIRECTORY(systemd)
+
+IF (BUILD_WITH_SYSTEMD)
+    ADD_SUBDIRECTORY(systemd)
+ENDIF (BUILD_WITH_SYSTEMD)
+
 ADD_SUBDIRECTORY(migration)
 
 IF (BUILD_TESTS)
-ADD_SUBDIRECTORY(test)
-ENDIF()
+    ADD_SUBDIRECTORY(test)
+ENDIF (BUILD_TESTS)
index 0c724a3..63efd67 100644 (file)
@@ -213,7 +213,6 @@ export LDFLAGS+="-Wl,--rpath=%{_libdir}"
 
 %cmake . \
         -DBUILD_TESTS=ON \
-        -DBUILD_WITH_SYSTEMD=ON \
         -DCMAKE_BUILD_TYPE=%{?build_type} \
         -DCMAKE_VERBOSE_MAKEFILE=ON \
         -DDB_FILES_SMACK_LABEL="System"
index 99b1c40..2e58844 100644 (file)
 # @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
 #
 
+
+SET(COMMON_DEPS)
+
+IF (BUILD_WITH_SYSTEMD)
 SET(COMMON_DEPS
+    ${COMMON_DEPS}
     libsystemd-daemon
     libsystemd-journal
     )
+ENDIF (BUILD_WITH_SYSTEMD)
 
 IF (CMAKE_BUILD_TYPE MATCHES "DEBUG")
 SET(COMMON_DEPS
index 8ddf8f8..70696d5 100644 (file)
 /**
  * @file        src/common/log/AuditLog.cpp
  * @author      Adam Malinowski <a.malinowsk2@partner.samsung.com>
+ * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  * @version     1.0
  * @brief       This file implements privilege check logging utility.
  */
 
+#ifdef BUILD_WITH_SYSTEMD
 #include <systemd/sd-journal.h>
+#else
+#include <syslog.h>
+#endif
 
 #include "AuditLog.h"
 
@@ -76,11 +81,18 @@ void AuditLog::log(const PolicyKey &policyKey, const PolicyResult &policyResult)
     if (m_logLevel == AL_ALL || (m_logLevel == AL_DENY && policyType == PPT::DENY) ||
         (m_logLevel == AL_ALLOW && policyType == PPT::ALLOW) ||
         (m_logLevel == AL_OTHER && policyType != PPT::ALLOW && policyType != PPT::DENY)) {
+#ifdef BUILD_WITH_SYSTEMD
             sd_journal_send("MESSAGE=%s;%s;%s => %s", policyKey.client().toString().c_str(),
                             policyKey.user().toString().c_str(),
                             policyKey.privilege().toString().c_str(),
                             policyResultToString(policyResult), "PRIORITY=%i", LOG_INFO,
                             "CYNARA_LOG_TYPE=AUDIT", NULL);
+#else // BUILD_WITH_SYSTEMD
+            syslog(LOG_INFO, "CYNARA AUDIT MESSAGE=%s;%s;%s => %s", policyKey.client().toString().c_str(),
+                   policyKey.user().toString().c_str(),
+                   policyKey.privilege().toString().c_str(),
+                   policyResultToString(policyResult));
+#endif // BUILD_WITH_SYSTEMD
     }
 }
 
index e559300..fd89163 100644 (file)
 
 #ifndef CYNARA_NO_LOGS
 #include <sstream>
+#ifdef BUILD_WITH_SYSTEMD
 #include <systemd/sd-journal.h>
-#endif
+#else // BUILD_WITH_SYSTEMD
+#include <syslog.h>
+#endif // BUILD_WITH_SYSTEMD
+#endif // CYNARA_NO_LOGS
 
 #include <attributes/attributes.h>
 
@@ -39,12 +43,20 @@ extern int __log_level;
 namespace {
     template <typename ...Args>
     void UNUSED __LOG_FUN(int level, const std::stringstream &format, Args&&... args) {
+#ifdef BUILD_WITH_SYSTEMD
         sd_journal_print(level, format.str().c_str(), std::forward<Args>(args)...);
+#else // BUILD_WITH_SYSTEMD
+        syslog(level, format.str().c_str(), std::forward<Args>(args)...);
+#endif // BUILD_WITH_SYSTEMD
     }
 
     template <>
     void UNUSED __LOG_FUN(int level, const std::stringstream &format) {
+#ifdef BUILD_WITH_SYSTEMD
         sd_journal_print(level, "%s", format.str().c_str());
+#else // BUILD_WITH_SYSTEMD
+        syslog(level, "%s", format.str().c_str());
+#endif // BUILD_WITH_SYSTEMD
     }
 } // namespace anonymous
 
index 969510d..47d8414 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  *
@@ -27,8 +27,9 @@
 #include <iostream>
 #include <stdlib.h>
 
-#include <systemd/sd-journal.h>
+#ifdef BUILD_WITH_SYSTEMD
 #include <systemd/sd-daemon.h>
+#endif
 
 #include <common.h>
 #include <log/log.h>
@@ -50,12 +51,14 @@ int main(int argc, char **argv) {
         cynara.init();
         LOGI("Cynara service is started");
 
+#ifdef BUILD_WITH_SYSTEMD
         int ret = sd_notify(0, "READY=1");
         if (ret == 0) {
             LOGW("Cynara was not configured to notify its status");
         } else if (ret < 0) {
             LOGE("sd_notify failed [%d]", ret);
         }
+#endif
 
         LOGD("Starting the real job");
         cynara.run();
index f1200a5..0da326d 100644 (file)
@@ -33,7 +33,9 @@
 #include <sys/un.h>
 #include <unistd.h>
 
+#ifdef BUILD_WITH_SYSTEMD
 #include <systemd/sd-daemon.h>
+#endif
 
 #include <log/log.h>
 #include <common.h>
@@ -254,8 +256,11 @@ bool SocketManager::handleRead(int fd, const RawBuffer &readbuffer) {
 
 void SocketManager::createDomainSocket(ProtocolPtr protocol, const std::string &path, mode_t mask,
                                        bool client) {
-    int fd = getSocketFromSystemD(path);
+    int fd;
+#ifdef BUILD_WITH_SYSTEMD
+    fd = getSocketFromSystemD(path);
     if (fd == -1)
+#endif
         fd = createDomainSocketHelp(path, mask);
 
     auto &desc = createDescriptor(fd, client);
@@ -321,6 +326,7 @@ int SocketManager::createDomainSocketHelp(const std::string &path, mode_t mask)
     return fd;
 }
 
+#ifdef BUILD_WITH_SYSTEMD
 int SocketManager::getSocketFromSystemD(const std::string &path) {
     int n = sd_listen_fds(0);
     LOGI("sd_listen_fds returns: [%d]", n);
@@ -339,6 +345,7 @@ int SocketManager::getSocketFromSystemD(const std::string &path) {
     LOGI("No useable sockets were passed by systemd.");
     return -1;
 }
+#endif // BUILD_WITH_SYSTEMD
 
 void SocketManager::createSignalSocket(ProtocolPtr protocol) {
     sigset_t mask;
index 99d218b..47fc74a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2015 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.
@@ -80,7 +80,9 @@ private:
     void createDomainSocket(ProtocolPtr protocol, const std::string &path, mode_t mask,
                             bool client);
     static int createDomainSocketHelp(const std::string &path, mode_t mask);
+#ifdef BUILD_WITH_SYSTEMD
     static int getSocketFromSystemD(const std::string &path);
+#endif
     void createSignalSocket(ProtocolPtr protocol);
 
     Descriptor &createDescriptor(int fd, bool client);