From d465eeddff055db158c5e69f48b8d7865508aab7 Mon Sep 17 00:00:00 2001 From: "seolheui, kim" Date: Wed, 12 Sep 2018 18:31:04 +0900 Subject: [PATCH] Add file logger to server - To get the log of failed encryption or decryption after reboot Change-Id: I67c2fcf054d24feab23772ef9d507f1eb6294ded Signed-off-by: seolheui, kim --- server/CMakeLists.txt | 1 + server/file-sink.cpp | 130 ++++++++++++++++++++++++++++++++++++++++++ server/file-sink.h | 48 ++++++++++++++++ server/server.cpp | 7 +-- 4 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 server/file-sink.cpp create mode 100644 server/file-sink.h diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt index 533d606..acc97bb 100644 --- a/server/CMakeLists.txt +++ b/server/CMakeLists.txt @@ -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 index 0000000..ebd2da1 --- /dev/null +++ b/server/file-sink.cpp @@ -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 +#include +#include +#include +#include +#include +#include + +#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 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 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 index 0000000..fc4fd8b --- /dev/null +++ b/server/file-sink.h @@ -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 +#include +#include + +#include +#include + +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__ diff --git a/server/server.cpp b/server/server.cpp index 3a374e4..33537cf 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -16,9 +16,8 @@ #include #include -#include - #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 _sink = nullptr; +std::unique_ptr _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."); -- 2.34.1