Add FstreamHelper class to get FD from fstream objects
authorMarek Smolinski <m.smolinski@samsung.com>
Tue, 4 Feb 2014 14:07:32 +0000 (15:07 +0100)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Wed, 12 Feb 2014 18:09:40 +0000 (19:09 +0100)
Rewrite the way of fsync calls, and add fsync in SmackAuditLog

    [Issue#]   N/A
    [Bug]      N/A
    [Cause]    N/A

[Verfication] Build, test fsync(DPL::FstreamHelper::getFd) on local pc.

Change-Id: I835df13a3b6b988afda7bade35fb5020d46efacd

src/server/dpl/core/include/dpl/fstream-helper.h [new file with mode: 0644]
src/server/dpl/log/src/audit-smack-log.cpp
src/server/service/password-file-buffer.cpp
src/server/service/password-file.cpp

diff --git a/src/server/dpl/core/include/dpl/fstream-helper.h b/src/server/dpl/core/include/dpl/fstream-helper.h
new file mode 100644 (file)
index 0000000..9de8237
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ *  Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Contact: Bumjin Im <bj.im@samsung.com>
+ *
+ *  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        fstream-helper.h
+ * @author      Marek Smolinski (m.smolinski@samsung.com)
+ * @version     1.0
+ * @brief       This file is the implementation file of fstream-helper
+ *
+ */
+
+#ifndef __FSTREAM_HELPER__
+#define __FSTREAM_HELPER__
+
+#include <fstream>
+
+namespace DPL {
+
+/*
+ * Bypass lack of public member function to get file
+ * descriptor from fstream objects in std
+ * This feature is needed for flushing data from kernel space buffer to
+ * physical device [fsync(int fd) - syscall] on opened fstream object
+*/
+
+struct FstreamHelper : std::fstream::__filebuf_type {
+    template<typename T>
+    static int getFd(T &strm) {
+        return static_cast<FstreamHelper *>(strm.rdbuf())->_M_file.fd();
+    }
+};
+
+} // namespace DPL
+
+#endif // __FSTREAM_HELPER__
index c442955..dbd328e 100644 (file)
@@ -39,6 +39,7 @@
 
 #include <dpl/log/audit-smack-log.h>
 #include <dpl/log/log.h>
+#include <dpl/fstream-helper.h>
 
 #define UNUSED __attribute__((unused))
 
@@ -132,7 +133,10 @@ void AuditSmackLog::HandleWrite(const char *message,
 
     m_outputStream << std::string("[") <<
         LocateSourceFileName(filename) << std::string(":") << line <<
-        std::string("] ") << function << std::string("(): ") << message << '\n';
+        std::string("] ") << function << std::string("(): ") <<
+        message << std::endl;
+
+    fsync(DPL::FstreamHelper::getFd(m_outputStream)); // flush kernel space buffer
 }
 
 int AuditSmackLog::CreateLogFile()
index d315aa3..4172071 100644 (file)
@@ -28,6 +28,7 @@
 #include <iterator>
 
 #include <dpl/log/log.h>
+#include <dpl/fstream-helper.h>
 
 #include <security-server.h>
 #include <password-exception.h>
@@ -82,16 +83,10 @@ namespace SecurityServer
             LogError("Failed to write data.");
             Throw(PasswordException::FStreamWriteError);
         }
-        file.close();
 
-        int fd;
-        if (0 <= (fd = open(path.c_str(), O_WRONLY | O_APPEND))) {
-            fsync(fd);
-            close(fd);
-        } else {
-            int err = errno;
-            LogError("Failed to fsync on file: " << path << " strerror: " << strerror(err));
-        }
+        file.flush();
+        fsync(DPL::FstreamHelper::getFd(file)); // flush kernel space buffer
+        file.close();
     }
 
     void PasswordFileBuffer::Load(const std::string &path)
index a7c279c..960c523 100644 (file)
@@ -37,6 +37,7 @@
 #include <openssl/sha.h>
 
 #include <dpl/log/log.h>
+#include <dpl/fstream-helper.h>
 
 #include <security-server.h>
 #include <protocols.h>
@@ -359,17 +360,10 @@ namespace SecurityServer
             LogError("Failed to write attempt count.");
             Throw(PasswordException::FStreamWriteError);
         }
-        attemptFile.close();
 
-        int fd;
-        if (0 <= (fd = open(ATTEMPT_FILE.c_str(), O_WRONLY | O_APPEND))) {
-            fchmod(fd, FILE_MODE);
-            fsync(fd); // force synchronization system buffers with file
-            close(fd);
-        } else {
-            int err = errno;
-            LogError("Failed to sync attempt file: " << ATTEMPT_FILE << "strerror: " << strerror(err));
-        }
+        attemptFile.flush();
+        fsync(DPL::FstreamHelper::getFd(attemptFile)); // flush kernel space buffer
+        attemptFile.close();
     }
 
     void PasswordFile::activatePassword()