Partially remove libdlog dependency 26/239926/1
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Wed, 29 Jul 2020 08:54:59 +0000 (10:54 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Wed, 29 Jul 2020 17:20:25 +0000 (19:20 +0200)
Switch to journal logging by default in all modules except cynara service.
This is to avoid libdlog dependency in client libraries.

Only cynara service may depend on libdlog and use it by providing a logging
callback to libcynara-commons.

Minor fixes and refactoring.

Change-Id: I15cf8dae915bd5863db79d27cad539cd7103ea34

14 files changed:
packaging/cynara.spec
packaging/libcynara-commons.spec
packaging/libcynara-dbus.spec
src/CMakeLists.txt
src/common/CMakeLists.txt
src/common/log/AuditLog.cpp
src/common/log/DlogLog.cpp [deleted file]
src/common/log/DlogLog.h [deleted file]
src/common/log/log.cpp
src/common/log/log.h
src/service/CMakeLists.txt
src/service/main/DlogLog.cpp [new file with mode: 0644]
src/service/main/DlogLog.h [new file with mode: 0644]
src/service/main/main.cpp

index 272dd7bab2a6a104e0e051c92cc218aeba791a45..28de2b3c9aa10bb09394db1816df71972c58d66d 100644 (file)
@@ -26,6 +26,10 @@ BuildRequires: pkgconfig(cynara-commons)
 %endif
 %endif
 
+%if %{log_type} == "DLOG"
+BuildRequires: pkgconfig(dlog)
+%endif
+
 %if !%{defined build_type}
 %define build_type RELEASE
 %endif
index fffa4c264e4f795398a8fa09c8487c0a46228f92..4b5e404d9b75991e44e46f164c7b87dc057c1b5d 100644 (file)
@@ -28,20 +28,15 @@ BuildRequires: zip
 BuildRequires: pkgconfig(libsmack)
 Obsoletes:     libcynara-storage
 
-# alternatives: DLOG, JOURNAL, NONE, (otherwise SYSLOG is used)
+# alternatives: JOURNAL, NONE, (otherwise SYSLOG is used)
 %if !%{defined log_type}
 %if 0%{tizen_version_major} > 4
-%define log_type "DLOG"
+%define log_type "JOURNAL"
 %else
 %define log_type "SYSLOG"
 %endif
 %endif
 
-
-%if %{log_type} == "DLOG"
-BuildRequires: pkgconfig(dlog)
-%endif
-
 %if %{log_type} == "JOURNAL"
 BuildRequires: pkgconfig(libsystemd)
 %endif
index 050d486dea96da3d3b7f1007c8b6165d9d46c590..469030c0ef9bd98ee48970bafce925db4fc55b26 100644 (file)
@@ -22,10 +22,10 @@ BuildRequires: pkgconfig(libsmack)
 BuildRequires: pkgconfig(libsystemd)
 BuildRequires: pkgconfig(dbus-1)
 
-# alternatives: DLOG, JOURNAL, NONE, (otherwise SYSLOG is used)
+# alternatives: JOURNAL, NONE, (otherwise SYSLOG is used)
 %if !%{defined log_type}
 %if 0%{tizen_version_major} > 4
-%define log_type "DLOG"
+%define log_type "JOURNAL"
 %else
 %define log_type "SYSLOG"
 %endif
index bcb0511654b75ca30292554ecd302ec656df672f..4951d76263831566cec3a11b3d8e662debc2c877 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2014-2018 Samsung Electronics Co., Ltd All Rights Reserved
+# Copyright (c) 2014-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.
@@ -27,12 +27,12 @@ SET(COMMON_DEPS
     )
 ENDIF (BUILD_WITH_SYSTEMD_DAEMON OR LOG_TYPE MATCHES "JOURNAL")
 
-IF (BUILD_COMMONS AND LOG_TYPE MATCHES "DLOG")
+IF (BUILD_SERVICE AND LOG_TYPE MATCHES "DLOG")
 SET(COMMON_DEPS
     ${COMMON_DEPS}
     dlog
     )
-ENDIF (BUILD_COMMONS AND LOG_TYPE MATCHES "DLOG")
+ENDIF (BUILD_SERVICE AND LOG_TYPE MATCHES "DLOG")
 
 IF (CMAKE_BUILD_TYPE MATCHES "DEBUG")
 SET(COMMON_DEPS
index 51371b1b8af53e3bfbd2824288375c6b0515f027..1908570addda50d0e092805a27ae65502b6593fe 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2014-2019 Samsung Electronics Co., Ltd All Rights Reserved
+# Copyright (c) 2014-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.
@@ -91,12 +91,6 @@ SET(COMMON_SOURCES ${COMMON_SOURCES}
     )
 ENDIF (CMAKE_BUILD_TYPE MATCHES "DEBUG")
 
-IF (LOG_TYPE MATCHES "DLOG")
-SET(COMMON_SOURCES ${COMMON_SOURCES}
-    ${COMMON_PATH}/log/DlogLog.cpp
-    )
-ENDIF (LOG_TYPE MATCHES "DLOG")
-
 ADD_DEFINITIONS("-fvisibility=default")
 
 ADD_LIBRARY(${TARGET_CYNARA_COMMON} SHARED ${COMMON_SOURCES})
index 5d10cf7f9cd3d30214d21d574082831d6d5536d9..b2c599972a8e6cc79a12021186297b367e8941a5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015-2018 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-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.
  * @brief       This file implements privilege check logging utility.
  */
 
-#ifdef BUILD_WITH_SYSTEMD_JOURNAL
-#include <systemd/sd-journal.h>
-#elif defined(BUILD_WITH_DLOG)
-#include "DlogLog.h"
-#else
-#include <syslog.h>
-#endif
-
-
 #include "AuditLog.h"
 
+#include "log.h"
+
 namespace Cynara {
 
 AuditLog::AuditLog() : m_logLevel(AL_DENY) {
@@ -83,24 +76,35 @@ 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)) {
+        (m_logLevel == AL_OTHER && policyType != PPT::ALLOW && policyType != PPT::DENY))
+    {
+        if (__log_callback) {
+            (*__log_callback)(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));
+        } else {
 #ifdef BUILD_WITH_SYSTEMD_JOURNAL
-            sd_journal_send("MESSAGE=%s;%s;%s => %s", policyKey.client().toString().c_str(),
+            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);
-#elif defined(BUILD_WITH_DLOG)
-            cynara_dlog_print(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));
+                            policyResultToString(policyResult),
+                            "PRIORITY=%i",
+                            LOG_INFO,
+                            "CYNARA_LOG_TYPE=AUDIT",
+                            NULL);
 #else
-            syslog(LOG_INFO, "CYNARA AUDIT MESSAGE=%s;%s;%s => %s", policyKey.client().toString().c_str(),
+            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
+        }
     }
 }
 
diff --git a/src/common/log/DlogLog.cpp b/src/common/log/DlogLog.cpp
deleted file mode 100644 (file)
index 55471f7..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Copyright (c) 2018 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        src/common/log/DlogLog.cpp
- * @author      Tomasz Swierczek <t.swierczek@samsung.com>
- * @version     1.0
- * @brief       This file implements loging functions on top of dlog
- */
-
-#include "DlogLog.h"
-
-#include <dlog.h>
-#include <stdlib.h>
-#include <syslog.h>
-
-#define CYNARA_DLOG_TAG "CYNARA"
-
-void cynara_dlog_print(int priority, char const *fmt, ...)
-{
-    log_priority dlog_prio;
-
-    switch (priority) {
-        case LOG_EMERG:
-            dlog_prio = DLOG_FATAL;
-            break;
-        case LOG_ALERT:
-            dlog_prio = DLOG_ERROR;
-            break;
-        case LOG_CRIT:
-            dlog_prio = DLOG_ERROR;
-            break;
-        case LOG_ERR:
-            dlog_prio = DLOG_ERROR;
-            break;
-        case LOG_WARNING:
-            dlog_prio = DLOG_WARN;
-            break;
-        case LOG_NOTICE:
-            dlog_prio = DLOG_INFO;
-            break;
-        case LOG_INFO:
-            dlog_prio = DLOG_INFO;
-            break;
-        case LOG_DEBUG:
-            dlog_prio = DLOG_DEBUG;
-            break;
-        default:
-            dlog_prio = DLOG_DEFAULT;
-    }
-
-    va_list ap;
-    va_start(ap, fmt);
-    (void) vprint_system_log(dlog_prio, CYNARA_DLOG_TAG, fmt, ap);
-    va_end(ap);
-}
diff --git a/src/common/log/DlogLog.h b/src/common/log/DlogLog.h
deleted file mode 100644 (file)
index f3e2fd4..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright (c) 2018 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        src/common/log/DlogLog.h
- * @author      Tomasz Swierczek <t.swierczek@samsung.com>
- * @version     1.0
- * @brief       This file declares loging functions on top of dlog
- */
-
-#ifndef SRC_COMMON_LOG_DLOGLOG_H
-#define SRC_COMMON_LOG_DLOGLOG_H
-
-#include <syslog.h>
-
-void cynara_dlog_print(int priority, char const *fmt, ...);
-
-#endif /* SRC_COMMON_LOG_DLOGLOG_H */
index fe6c56971a2e665a382bd658bb69421107ff624a..5903e021bbbe82a362005ff2a95aff7aebe63806 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2020 Samsung Electronics Co., Ltd All Rights Reserved
  *
  * Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  *
@@ -34,6 +34,8 @@ int __log_level = LOG_DEBUG;
 int __log_level = LOG_ERR;
 #endif
 
+LoggingCallback __log_callback = nullptr;
+
 static int strlog2intlog(const char *strlog) {
     if(!strncmp("LOG_EMERG", strlog, strlen("LOG_EMERG")))
         return LOG_EMERG;
@@ -55,11 +57,12 @@ static int strlog2intlog(const char *strlog) {
     return LOG_ERR;
 }
 
-void init_log(void) {
+void init_log(LoggingCallback callback) {
     char *env_val = getenv("CYNARA_LOG_LEVEL");
     if (env_val) {
         __log_level = strlog2intlog(env_val);
     }
+    __log_callback = callback;
 }
 
 #else
index 70871c76f170f42caadb3de52b651c92770d9b93..9cac69e515a44fc5c5d3002d110bfbc2943f82f3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014-2018 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2020 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  *
 #include <sstream>
 #ifdef BUILD_WITH_SYSTEMD_JOURNAL
 #include <systemd/sd-journal.h>
-#elif defined(BUILD_WITH_DLOG)
-#include "DlogLog.h"
-#else
-#include <syslog.h>
-#endif
+#endif // BUILD_WITH_SYSTEMD_JOURNAL
 #endif // CYNARA_NO_LOGS
 
+#include <syslog.h> // for LOG_* and syslog()
 #include <attributes/attributes.h>
 
 extern int __log_level;
 
+typedef void (*LoggingCallback) (int priority, char const *fmt, ...);
+extern LoggingCallback __log_callback;
+
 #ifndef CYNARA_NO_LOGS
 namespace {
     template <typename ...Args>
     void UNUSED __DIRECT_LOG_FUN(int level, Args&&... args) {
+        if (__log_callback) {
+            (*__log_callback)(level, std::forward<Args>(args)...);
+        } else {
 #ifdef BUILD_WITH_SYSTEMD_JOURNAL
-        sd_journal_print(level, std::forward<Args>(args)...);
-#elif defined(BUILD_WITH_DLOG)
-        cynara_dlog_print(level, std::forward<Args>(args)...);
+            sd_journal_print(level, std::forward<Args>(args)...);
 #else
-        syslog(level, std::forward<Args>(args)...);
+            syslog(level, std::forward<Args>(args)...);
 #endif
+        }
     }
 
     template <typename ...Args>
@@ -115,6 +117,6 @@ namespace {
 #define LOGI_NOTHROW(...)  __LOG_NOTHROW(LOG_INFO, __VA_ARGS__)    /* informational */
 #define LOGD_NOTHROW(...)  __LOG_NOTHROW(LOG_DEBUG, __VA_ARGS__)   /* debug-level messages */
 
-void init_log(void);
+void init_log(LoggingCallback callback = nullptr);
 
 #endif /* CYNARA_COMMON_LOG_H */
index 9d90e6a5177cac08b20c7b80f327aa929d14448e..6295d1a478392a07d200a22e016e5988edeab10b 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2014-2016 Samsung Electronics Co., Ltd All Rights Reserved
+# Copyright (c) 2014-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.
@@ -45,6 +45,12 @@ SET(CYNARA_SOURCES
     ${CYNARA_SERVICE_PATH}/sockets/SocketManager.cpp
     )
 
+IF (LOG_TYPE MATCHES "DLOG")
+    SET(CYNARA_SOURCES ${CYNARA_SOURCES}
+        ${CYNARA_SERVICE_PATH}/main/DlogLog.cpp
+    )
+ENDIF (LOG_TYPE MATCHES "DLOG")
+
 INCLUDE_DIRECTORIES(
     ${CYNARA_SERVICE_PATH}
     ${CYNARA_PATH}
diff --git a/src/service/main/DlogLog.cpp b/src/service/main/DlogLog.cpp
new file mode 100644 (file)
index 0000000..d0ab235
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2018-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.
+ */
+/**
+ * @file        src/service/main/DlogLog.cpp
+ * @author      Tomasz Swierczek <t.swierczek@samsung.com>
+ * @version     1.0
+ * @brief       This file implements loging functions on top of dlog
+ */
+
+#include "DlogLog.h"
+
+#include <dlog.h>
+#include <stdlib.h>
+#include <syslog.h>
+
+#define CYNARA_DLOG_TAG "CYNARA"
+
+void cynara_dlog_print(int priority, char const *fmt, ...)
+{
+    log_priority dlog_prio;
+
+    switch (priority) {
+        case LOG_EMERG:
+            dlog_prio = DLOG_FATAL;
+            break;
+        case LOG_ALERT:
+            dlog_prio = DLOG_ERROR;
+            break;
+        case LOG_CRIT:
+            dlog_prio = DLOG_ERROR;
+            break;
+        case LOG_ERR:
+            dlog_prio = DLOG_ERROR;
+            break;
+        case LOG_WARNING:
+            dlog_prio = DLOG_WARN;
+            break;
+        case LOG_NOTICE:
+            dlog_prio = DLOG_INFO;
+            break;
+        case LOG_INFO:
+            dlog_prio = DLOG_INFO;
+            break;
+        case LOG_DEBUG:
+            dlog_prio = DLOG_DEBUG;
+            break;
+        default:
+            dlog_prio = DLOG_DEFAULT;
+    }
+
+    va_list ap;
+    va_start(ap, fmt);
+    (void) vprint_system_log(dlog_prio, CYNARA_DLOG_TAG, fmt, ap);
+    va_end(ap);
+}
diff --git a/src/service/main/DlogLog.h b/src/service/main/DlogLog.h
new file mode 100644 (file)
index 0000000..68f4bd9
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2018-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.
+ */
+/**
+ * @file        src/service/main/DlogLog.h
+ * @author      Tomasz Swierczek <t.swierczek@samsung.com>
+ * @version     1.0
+ * @brief       This file declares loging functions on top of dlog
+ */
+
+#ifndef SRC_SERVICE_MAIN_DLOGLOG_H
+#define SRC_SERVICE_MAIN_DLOGLOG_H
+
+void cynara_dlog_print(int priority, char const *fmt, ...);
+
+#endif /* SRC_SERVICE_MAIN_DLOGLOG_H */
index a6070f155575ec22b6344c51e7689e3178b88579..30f405b41a78b4746028e85f9329e5c4fbedcc8c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2014-2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2014-2020 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  *
 #include <systemd/sd-daemon.h>
 #endif
 
+#if defined(BUILD_WITH_DLOG)
+#include "DlogLog.h"
+#endif
+
+
 #include <common.h>
 #include <log/log.h>
 
@@ -104,7 +109,11 @@ int main(int argc, char **argv) {
             if (setgid(options.m_gid) == -1)
                 return EXIT_FAILURE;
 
+#if defined(BUILD_WITH_DLOG)
+        init_log(cynara_dlog_print);
+#else
         init_log();
+#endif
 
         Cynara::Cynara cynara;
         LOGI("Cynara service is starting ...");