internal: use strerror_r instead of strerror 60/201560/1
authorAdrian Szyndela <adrian.s@samsung.com>
Fri, 15 Mar 2019 14:21:58 +0000 (15:21 +0100)
committerAdrian Szyndela <adrian.s@samsung.com>
Fri, 15 Mar 2019 14:21:58 +0000 (15:21 +0100)
strerror() is thread-unsafe. This replaces usage of strerror()
with usage of strerror_r().

Change-Id: I80d1888c1ec24584c57c68a7b2531084f616342f

src/internal/storage_backend_serialized.cpp
src/internal/tslog.cpp
src/internal/tslog.hpp

index 87e0283..1f4ad78 100644 (file)
@@ -31,7 +31,6 @@
 #include <boost/tokenizer.hpp>
 
 #include <cerrno>
-#include <cstring>
 
 using namespace FB;
 
@@ -68,7 +67,7 @@ void StorageBackendSerialized::StorageBackendSerializedImpl::releaseMMap() {
        assert(0 != length);
 
        if (munmap(mem, length) != 0)
-               tslog::log("munmap(): ", strerror(errno), "\n");
+               tslog::log("munmap(): ", tslog::print_errno(errno), "\n");
 
        mem = static_cast<decltype(mem)>(MAP_FAILED);
        length = 0;
@@ -78,7 +77,7 @@ void StorageBackendSerialized::StorageBackendSerializedImpl::releaseFD() {
        assert(-1 != fd);
 
        if (close(fd) != 0)
-               tslog::log("close(): ", strerror(errno), "\n");
+               tslog::log("close(): ", tslog::print_errno(errno), "\n");
 
        fd = -1;
 }
@@ -100,7 +99,7 @@ bool StorageBackendSerialized::StorageBackendSerializedImpl::init(const char *fi
        assert(nullptr == file);
 
        auto err = [filename] (const char *what) {
-               tslog::log("Can't ", what, " ", filename, ": ", strerror(errno), "\n");
+               tslog::log("Can't ", what, " ", filename, ": ", tslog::print_errno(errno), "\n");
                return false;
        };
 
index 8b86764..99147a5 100644 (file)
@@ -21,6 +21,8 @@
 #include "tslog.hpp"
 #include "libdbuspolicy1-private.h"
 
+#include <cstring>
+
 namespace tslog {
 int8_t g_verbosity;
 }
@@ -48,3 +50,10 @@ void tslog::logWarning(const std::string &warning)
        log(warning, "\n");
        LOGW("%s", warning.c_str());
 }
+
+namespace tslog {
+       std::ostream &operator<<(std::ostream &stream, const print_errno &p) {
+               char buf[256];
+               return stream << strerror_r(p.err, buf, sizeof(buf));
+       }
+}
index 59f2990..c216d8b 100644 (file)
@@ -27,8 +27,6 @@
 #include <stdlib.h>
 #include <string>
 
-typedef std::ostream& (*t_ManFun)(std::ostream&);
-
 namespace tslog
 {
        /** Checks value of environmental variable with given name */
@@ -56,6 +54,13 @@ namespace tslog
 
        template <typename ...Args>
        void log_verbose(const Args &...args) { if (verbose()) log_to_stream(std::cout, args...); }
+
+       struct print_errno {
+               int err;
+               print_errno(int e) : err(e) {}
+       };
+
+       std::ostream &operator<<(std::ostream &stream, const print_errno &p);
 }
 
 #endif