Add file logger to server 96/188996/19
authorseolheui, kim <s414.kim@samsung.com>
Wed, 12 Sep 2018 09:31:04 +0000 (18:31 +0900)
committerseolheui, kim <s414.kim@samsung.com>
Thu, 27 Dec 2018 10:29:33 +0000 (19:29 +0900)
- To get the log of failed encryption or decryption after reboot

Change-Id: I67c2fcf054d24feab23772ef9d507f1eb6294ded
Signed-off-by: seolheui, kim <s414.kim@samsung.com>
server/CMakeLists.txt
server/file-sink.cpp [new file with mode: 0644]
server/file-sink.h [new file with mode: 0644]
server/server.cpp

index 533d606..acc97bb 100644 (file)
@@ -18,6 +18,7 @@ SET(SERVER_SRCS       main.cpp
                                misc.cpp
                                ext4-tool.cpp
                                file-footer.cpp
+                               file-sink.cpp
                                secure-erase.cpp
                                progress-bar.cpp
                                kernel-keyring.cpp
diff --git a/server/file-sink.cpp b/server/file-sink.cpp
new file mode 100644 (file)
index 0000000..ebd2da1
--- /dev/null
@@ -0,0 +1,130 @@
+/*
+ *  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
+ */
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#include <iostream>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <chrono>
+#include <errno.h>
+
+#include "file-sink.h"
+
+namespace ode {
+
+#define MAX_LOG_LEN 16
+
+namespace {
+std::string getFileName(const std::string &path)
+{
+       std::string name;
+       auto pos = path.rfind('/');
+       pos = (pos == std::string::npos) ? 0 : pos + 1;
+       name = path.substr(pos, path.size());
+       return name;
+}
+} //namespace
+
+FileLogSink::FileLogSink(const std::string& name)
+       : path("/opt/var/log/"), fd(-1)
+{
+       path.append(getFileName(name));
+       fd = ::open(path.c_str(), O_CREAT | O_RDWR | O_APPEND | O_SYNC, 0664);
+}
+
+FileLogSink::~FileLogSink()
+{
+       ::close(fd);
+}
+
+void FileLogSink::resize()
+{
+       std::lock_guard<std::recursive_mutex> lock(mtx);
+       struct stat st;
+       int ret = 0;
+       int blkcnt = MAX_LOG_LEN;
+       size_t blksize = 0;
+
+       if (::lstat(path.c_str(), &st) < 0) {
+               std::cerr << "Invalid log file" << std::endl;
+               return;
+       }
+
+       blksize = st.st_blksize;
+       if (blksize > 0)
+               blkcnt = (st.st_size / blksize) + 1;
+
+       if (blkcnt <= MAX_LOG_LEN)
+               return;
+
+       ret = ::fallocate(fd, FALLOC_FL_COLLAPSE_RANGE, 0, blksize*(blkcnt-MAX_LOG_LEN));
+       if (ret < 0)
+               std::cerr << "Failed to collapse the log file : " << errno << std::endl;
+}
+
+void FileLogSink::write(const std::string &message)
+{
+       std::lock_guard<std::recursive_mutex> lock(mtx);
+       resize();
+
+       auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
+       std::tm tm = *std::localtime(&now);
+       std::string log("[" + std::to_string(tm.tm_hour)
+                       + ":"
+                       + std::to_string(tm.tm_min)
+                       + ":"
+                       + std::to_string(tm.tm_sec) + "] ");
+
+       log.append(message);
+       ::write(fd, log.c_str(), log.size());
+       ::sync();
+}
+
+void FileLogSink::sink(const std::string &message)
+{
+       auto level = message.substr(0, message.find('<'));
+       auto subMsg = message.substr(message.find(':') + 1);
+       std::string log;
+
+       switch (audit::StringToLogLevel(level)) {
+       case audit::LogLevel::Error:
+               log.append("[" + this->tag + " ERROR] : " + subMsg);
+               break;
+       case audit::LogLevel::Warning:
+               log.append("[" + this->tag + " WARN] : " + subMsg);
+               break;
+       case audit::LogLevel::Debug:
+               log.append("[" + this->tag + " DEBUG] : " + subMsg);
+               break;
+       case audit::LogLevel::Info:
+               log.append("[" + this->tag + " INFO] : " + subMsg);
+               break;
+       case audit::LogLevel::Trace:
+               log.append("[" + this->tag + " TRACE] : " + subMsg);
+               break;
+       default:
+               log.append("[" + this->tag + " SILENT] : " + subMsg);
+               break;
+       }
+
+       write(log);
+}
+
+} //namespace ode
diff --git a/server/file-sink.h b/server/file-sink.h
new file mode 100644 (file)
index 0000000..fc4fd8b
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ *  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
+ */
+
+#ifndef __ODE_FILE_LOGSINK_H__
+#define __ODE_FILE_LOGSINK_H__
+
+#include <string>
+#include <memory>
+#include <mutex>
+
+#include <klay/filesystem.h>
+#include <klay/audit/logger.h>
+
+namespace ode {
+
+class FileLogSink : public audit::LogSink {
+public:
+       explicit FileLogSink(const std::string& tag);
+       ~FileLogSink();
+       void sink(const std::string& message) override;
+
+private:
+       void resize();
+       void write(const std::string &message);
+
+private:
+       std::string path;
+       int fd;
+       std::string tag;
+       std::recursive_mutex mtx;
+};
+
+} //namespace ode
+
+#endif //__ODE_FILE_LOGSINK_H__
index 3a374e4..33537cf 100644 (file)
@@ -16,9 +16,8 @@
 #include <cynara-client.h>
 #include <sys/smack.h>
 
-#include <klay/audit/dlog-sink.h>
-
 #include "logger.h"
+#include "file-sink.h"
 #include "secure-erase.h"
 #include "internal-encryption.h"
 #include "external-encryption.h"
@@ -36,13 +35,13 @@ namespace ode {
 namespace {
 
 const std::string ODE_MANAGER_ADDRESS = "/tmp/.ode.sock";
-std::unique_ptr<audit::DlogLogSink> _sink = nullptr;
+std::unique_ptr<FileLogSink> _sink = nullptr;
 
 } // namespace
 
 ServerContext::ServerContext() : rmi::Service(ODE_MANAGER_ADDRESS)
 {
-       _sink.reset(new audit::DlogLogSink("ODE"));
+       _sink.reset(new FileLogSink("ode.log"));
        SINK = _sink.get();
 
        INFO(SINK, "ODE server starting.");