Add strerror_r wrapper implementation 93/198993/2
authorZofia Grzelewska <z.abramowska@samsung.com>
Mon, 28 Jan 2019 14:10:13 +0000 (15:10 +0100)
committerZofia Grzelewska <z.abramowska@samsung.com>
Thu, 31 Jan 2019 15:17:26 +0000 (16:17 +0100)
strerror function cannot be used inside client libararies.
This commit provides a convenient C++ wrapper of strerror_r
and applies it in all files linked with client libraries.

Change-Id: I2defc2fbb76cdca8a63a8b23b581b953d2803679

13 files changed:
src/agent/socket/AgentSocketClient.cpp
src/common/CMakeLists.txt
src/common/error/SafeStrError.cpp [new file with mode: 0644]
src/common/error/SafeStrError.h [new file with mode: 0644]
src/common/exceptions/FileLockAcquiringException.h
src/common/exceptions/UnexpectedErrorException.h
src/common/notify/FdNotifyObject.cpp
src/common/plugin/PluginManager.cpp
src/common/sockets/Socket.cpp
src/helpers/creds-sd-bus/creds-sd-bus-inner.cpp
src/monitor/socket/MonitorSocketClient.cpp
src/storage/Integrity.cpp
test/CMakeLists.txt

index b08692accb0d59f25d990ba163c7549c1dd0a934..9265b7604e11452ce622c9097fc5533971c74bac 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2014-2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2014-2019 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,6 +27,7 @@
 #include <sys/ioctl.h>
 #include <unistd.h>
 
+#include <error/SafeStrError.h>
 #include <log/log.h>
 #include <protocol/Protocol.h>
 #include <request/pointers.h>
@@ -81,7 +82,7 @@ AgentSocketState AgentSocketClient::waitForEvent(void) {
     int ret = TEMP_FAILURE_RETRY(poll(desc, eventCount, -1));
     if (ret == -1) {
         int err = errno;
-        LOGE("Poll returned with error: " << strerror(err));
+        LOGE("Poll returned with error: " << safeStrError(err));
         return AgentSocketState::SS_ERROR;
     }
     if (desc[1].revents & POLLIN) {
index 0da868ed3e59dabe8f0f1248de347b3a5c0bdaa1..089f39081b77f1756af35192ce46335b71320d67 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2014-2018 Samsung Electronics Co., Ltd All Rights Reserved
+# Copyright (c) 2014-2019 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.
@@ -29,6 +29,7 @@ SET(COMMON_SOURCES
     ${COMMON_PATH}/config/PathConfig.cpp
     ${COMMON_PATH}/containers/BinaryQueue.cpp
     ${COMMON_PATH}/error/api.cpp
+    ${COMMON_PATH}/error/SafeStrError.cpp
     ${COMMON_PATH}/lock/FileLock.cpp
     ${COMMON_PATH}/log/AuditLog.cpp
     ${COMMON_PATH}/log/log.cpp
diff --git a/src/common/error/SafeStrError.cpp b/src/common/error/SafeStrError.cpp
new file mode 100644 (file)
index 0000000..611e2c2
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ *  Copyright (c) 2019 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/client-common/error/SafeStrError.cpp
+ * @author      Zofia Grzelewska <z.abramowska@samsung.com>
+ * @version     1.0
+ * @brief       Implementation of thread safe strerror function
+ */
+
+#include <cerrno>
+#include <cstring>
+#include <vector>
+
+#include "SafeStrError.h"
+
+namespace Cynara {
+
+std::string safeStrError(int num) {
+    std::vector<char> buf(128);
+
+    errno = 0;
+    char *errorStr;
+    while ((errorStr = strerror_r(num, buf.data(), buf.size())), errno == ERANGE) {
+        buf.resize(buf.size() * 2);
+    }
+
+    return std::string(errorStr);
+}
+
+} // namespace Cynara
diff --git a/src/common/error/SafeStrError.h b/src/common/error/SafeStrError.h
new file mode 100644 (file)
index 0000000..b9143e4
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ *  Copyright (c) 2019 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/client-common/error/SafeStrError.h
+ * @author      Zofia Grzelewska <z.abramowska@samsung.com>
+ * @version     1.0
+ * @brief       Declaration of thread safe strerror function
+ */
+
+#ifndef SRC_CLIENT_COMMON_ERROR_SAFESTRERROR_H_
+#define SRC_CLIENT_COMMON_ERROR_SAFESTRERROR_H_
+
+#include <string>
+
+namespace Cynara {
+
+std::string safeStrError(int num);
+
+} // namespace Cynara
+
+#endif // SRC_CLIENT_COMMON_ERROR_SAFESTRERROR_H_
index 4640719d9efa61d1a6061be5638c2a9ad7625826..5739f9a3437e5e5856a68050f9b5abb0a5ad4a5d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2019 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.
@@ -26,6 +26,7 @@
 #include <cstring>
 #include <string>
 
+#include <error/SafeStrError.h>
 #include <exceptions/Exception.h>
 
 namespace Cynara {
@@ -48,7 +49,7 @@ public:
     }
 
     const std::string errorString(void) const {
-        return strerror(m_errno);
+        return safeStrError(m_errno);
     }
 
 private:
index 4e0261d8ce70294a1939efe70088f5ac9cbacf1b..630258a54ae8f12547c8dcaab4a89d111b034e52 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2019 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.
@@ -35,10 +35,14 @@ public:
     UnexpectedErrorException() = delete;
     UnexpectedErrorException(int errorCode, const char *errorMsg) {
         m_message = "UnexpectedErrorException with errorCode =[" + std::to_string(errorCode)
-                      + "] and message <" + errorMsg + ">";
+                    + "] and message <" + errorMsg + ">";
     }
-    UnexpectedErrorException(const char *errorMsg) {
-        m_message = "UnexpectedErrorException with message <" + std::string(errorMsg) + ">";
+    UnexpectedErrorException(int errorCode, const std::string &errorMsg) {
+        m_message = "UnexpectedErrorException with errorCode =[" + std::to_string(errorCode)
+                    + "] and message <" + errorMsg + ">";
+    }
+    UnexpectedErrorException(const std::string &errorMsg) {
+        m_message = "UnexpectedErrorException with message <" + errorMsg + ">";
     }
 
     virtual ~UnexpectedErrorException() {};
index 33eadbf05b50cd3edd9f557904c55f9f23417a6f..92007c52c6927d7ba0883aeebbb96e35bcc2b51c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2016-2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2016-2019 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.
@@ -25,6 +25,7 @@
 #include <sys/eventfd.h>
 #include <unistd.h>
 
+#include <error/SafeStrError.h>
 #include <log/log.h>
 
 #include "FdNotifyObject.h"
@@ -38,7 +39,7 @@ bool FdNotifyObject::init(void) {
     m_eventFd = eventfd(0, 0);
     if (m_eventFd == -1) {
         int err = errno;
-        LOGE("Couldn't initialize event fd: " << strerror(err));
+        LOGE("Couldn't initialize event fd: " << safeStrError(err));
         return false;
     }
     return true;
@@ -52,7 +53,7 @@ bool FdNotifyObject::notify(void) {
     int ret = eventfd_write(m_eventFd, 1);
     if (ret == -1) {
         int err = errno;
-        LOGE("Couldn't write to event fd " << strerror(err));
+        LOGE("Couldn't write to event fd " << safeStrError(err));
         return false;
     }
     return true;
@@ -63,7 +64,7 @@ bool FdNotifyObject::snooze(void) {
     int ret = eventfd_read(m_eventFd, &value);
     if (ret == -1) {
         int err = errno;
-        LOGE("Couldn't read from event fd " << strerror(err));
+        LOGE("Couldn't read from event fd " << safeStrError(err));
         return false;
     }
     return true;
index b4291b7b873c5b9751aaecef728e138ceb5dda1a..6e275846226fba1e7bf7c99d8068a40316cdde31 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2019 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.
@@ -23,6 +23,7 @@
 
 #define _BSD_SOURCE_
 
+#include <cerrno>
 #include <cinttypes>
 #include <cstdlib>
 #include <cstring>
@@ -31,6 +32,7 @@
 #include <functional>
 
 #include <attributes/attributes.h>
+#include <error/SafeStrError.h>
 #include <exceptions/UnknownPolicyTypeException.h>
 #include <log/log.h>
 
@@ -95,7 +97,7 @@ void PluginManager::loadPlugins(void) {
 
     if (fileAmount < 0) {
         UNUSED int err = errno;
-        LOGE("Couldn't scan for plugins in <%s> : <%s>", m_dir.c_str(), strerror(err));
+        LOGE("Couldn't scan for plugins in <%s> : <%s>", m_dir.c_str(), safeStrError(err).c_str());
         return;
     }
 
index ce1eb35ce3204d2d16b1b25269a6dcdd3c619c33..d3c8bc01c38d316540933955c3abf2f45d9f35ca 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2000 - 2019 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.
@@ -31,6 +31,7 @@
 #include <sys/un.h>
 #include <unistd.h>
 
+#include <error/SafeStrError.h>
 #include <exceptions/AccessDeniedException.h>
 #include <exceptions/InitException.h>
 #include <exceptions/NoMemoryException.h>
@@ -72,8 +73,8 @@ bool Socket::waitForSocket(int event) {
 
     if (ret == -1) {
         int err = errno;
-        LOGE("'poll' function error [%d] : <%s>", err, strerror(err));
-        throw UnexpectedErrorException(err, strerror(err));
+        LOGE("'poll' function error [%d] : <%s>", err, safeStrError(err).c_str());
+        throw UnexpectedErrorException(err, safeStrError(err));
     } else if (ret == 0) {
         LOGD("Poll timeout");
     }
@@ -87,8 +88,8 @@ int Socket::getSocketError(void) {
     int ret = getsockopt(m_sock, SOL_SOCKET, SO_ERROR, &err, &len);
     if (ret < 0) {
         int err = errno;
-        LOGE("'getsockopt' function error [%d] : <%s>", err, strerror(err));
-        throw UnexpectedErrorException(err, strerror(err));
+        LOGE("'getsockopt' function error [%d] : <%s>", err, safeStrError(err).c_str());
+        throw UnexpectedErrorException(err, safeStrError(err));
     }
     return err;
 }
@@ -112,8 +113,8 @@ void Socket::createSocket(void) {
     m_sock = socket(AF_UNIX, SOCK_STREAM, 0);
     if (m_sock < 0) {
         int err = errno;
-        LOGE("'socket' function error [%d] : <%s>", err, strerror(err));
-        throw UnexpectedErrorException(err, strerror(err));
+        LOGE("'socket' function error [%d] : <%s>", err, safeStrError(err).c_str());
+        throw UnexpectedErrorException(err, safeStrError(err));
     }
 
     if ((flags = fcntl(m_sock, F_GETFL, 0)) < 0 ||
@@ -121,8 +122,8 @@ void Socket::createSocket(void) {
     {
         int err = errno;
         close();
-        LOGE("'fcntl' function error [%d] : <%s>", err, strerror(err));
-        throw UnexpectedErrorException(err, strerror(err));
+        LOGE("'fcntl' function error [%d] : <%s>", err, safeStrError(err).c_str());
+        throw UnexpectedErrorException(err, safeStrError(err));
     }
 }
 
@@ -162,8 +163,8 @@ Socket::ConnectionStatus Socket::connectSocket(void) {
                                              m_socketPath + ">");
             default:
                 close();
-                LOGE("'connect' function error [%d] : <%s>", err, strerror(err));
-                throw UnexpectedErrorException(err, strerror(err));
+                LOGE("'connect' function error [%d] : <%s>", err, safeStrError(err).c_str());
+                throw UnexpectedErrorException(err, safeStrError(err));
         }
     }
     return ConnectionStatus::CONNECTION_SUCCEEDED;
@@ -192,8 +193,8 @@ Socket::SendStatus Socket::sendBuffer(void) {
                     LOGN("Connection closed by server");
                     return SendStatus::CONNECTION_LOST;
                 default:
-                    LOGE("'send' function error [%d] : <%s>", err, strerror(err));
-                    throw UnexpectedErrorException(err, strerror(err));
+                    LOGE("'send' function error [%d] : <%s>", err, safeStrError(err).c_str());
+                    throw UnexpectedErrorException(err, safeStrError(err));
             }
         }
         m_sendBufferPos += static_cast<size_t>(t);
@@ -279,8 +280,8 @@ bool Socket::receiveFromServer(BinaryQueue &queue) {
                     LOGW("read returned -1 with ECONNRESET / Connection closed by server.");
                     return false;
                 default:
-                    LOGE("'read' function error [%d] : <%s>", err, strerror(err));
-                    throw UnexpectedErrorException(err, strerror(err));
+                    LOGE("'read' function error [%d] : <%s>", err, safeStrError(err).c_str());
+                    throw UnexpectedErrorException(err, safeStrError(err));
             }
         }
 
index 24be99e3e2b3a41c5f2f314d5685d8d7dce7a6e8..e15a564b28bd221029ef7738f8ff00ce5fc87655 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2018-2019 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.
  */
 
 #include <cerrno>
-#include <cstring>
 
 #include <systemd/sd-bus.h>
 
 #include <cynara-error.h>
 #include <exceptions/TryCatch.h>
 
+#include <error/SafeStrError.h>
+
 #include <creds-sd-bus-inner.h>
 
 int copyStr(char **destStr, const std::string &str) {
@@ -48,7 +49,7 @@ int getPid(sd_bus *bus, const char *name, pid_t *pid) {
         sd_bus_creds *creds;
         int ret = sd_bus_get_name_creds(bus, name, SD_BUS_CREDS_PID, &creds);
         if (ret < 0) {
-            LOGE("Couldn't get name creds: %s", std::strerror(-ret));
+            LOGE("Couldn't get name creds: " << Cynara::safeStrError(-ret));
             return CYNARA_API_UNKNOWN_ERROR;
         }
 
@@ -56,7 +57,7 @@ int getPid(sd_bus *bus, const char *name, pid_t *pid) {
         sd_bus_creds_unref(creds);
 
         if (ret < 0) {
-            LOGE("Couldn't get pid from creds: %s", std::strerror(-ret));
+            LOGE("Couldn't get pid from creds: " << Cynara::safeStrError(-ret));
             return CYNARA_API_UNKNOWN_ERROR;
         }
         return CYNARA_API_SUCCESS;
@@ -81,14 +82,14 @@ int getClientSmackLabel(sd_bus *bus, const char *name, char **client) {
         /* There's no API specially for Smack label, but SELINUX_CONTEXT maps to a standard Linux Security Label */
         int ret = sd_bus_get_name_creds(bus, name, SD_BUS_CREDS_AUGMENT | SD_BUS_CREDS_SELINUX_CONTEXT, &creds);
         if (ret < 0) {
-            LOGE("Couldn't get name creds: %s", std::strerror(-ret));
+            LOGE("Couldn't get name creds: " << Cynara::safeStrError(-ret));
             return CYNARA_API_UNKNOWN_ERROR;
         }
 
         ret = sd_bus_creds_get_selinux_context(creds, &label);
         if (ret < 0) {
             sd_bus_creds_unref(creds);
-            LOGE("Couldn't get smack label from creds: %s", std::strerror(-ret));
+            LOGE("Couldn't get smack label from creds: " << Cynara::safeStrError(-ret));
             return CYNARA_API_UNKNOWN_ERROR;
         }
 
@@ -104,14 +105,14 @@ int getUserId(sd_bus *bus, const char *name, char **user) {
         uid_t uid;
         int ret = sd_bus_get_name_creds(bus, name, SD_BUS_CREDS_AUGMENT | SD_BUS_CREDS_UID, &creds);
         if (ret < 0) {
-            LOGE("Couldn't get name creds: %s", std::strerror(-ret));
+            LOGE("Couldn't get name creds: " << Cynara::safeStrError(-ret));
             return CYNARA_API_UNKNOWN_ERROR;
         }
 
         ret = sd_bus_creds_get_uid(creds, &uid);
         sd_bus_creds_unref(creds);
         if (ret < 0) {
-            LOGE("Couldn't get uid from creds: %s", std::strerror(-ret));
+            LOGE("Couldn't get uid from creds: " << Cynara::safeStrError(-ret));
             return CYNARA_API_UNKNOWN_ERROR;
         }
         return copyStr(user, std::to_string(uid));
@@ -124,14 +125,14 @@ int getUserGid(sd_bus *bus, const char *name, char **user) {
         gid_t gid;
         int ret = sd_bus_get_name_creds(bus, name, SD_BUS_CREDS_AUGMENT | SD_BUS_CREDS_GID, &creds);
         if (ret < 0) {
-            LOGE("Couldn't get name creds: %s", std::strerror(-ret));
+            LOGE("Couldn't get name creds: " << Cynara::safeStrError(-ret));
             return CYNARA_API_UNKNOWN_ERROR;
         }
 
         ret = sd_bus_creds_get_gid(creds, &gid);
         sd_bus_creds_unref(creds);
         if (ret < 0) {
-            LOGE("Couldn't get gid from creds: %s", std::strerror(-ret));
+            LOGE("Couldn't get gid from creds: " << Cynara::safeStrError(-ret));
             return CYNARA_API_UNKNOWN_ERROR;
         }
         return copyStr(user, std::to_string(gid));
index d8b72549e7979f3e0efee398b98db20a16b8f232..764edeaf97f3561ef3acf7117ab28540694187cc 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2016-2019 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
  *
@@ -30,6 +30,7 @@
 #include <unistd.h>
 
 #include <config/PathConfig.h>
+#include <error/SafeStrError.h>
 #include <log/log.h>
 #include <request/Request.h>
 #include <request/RequestContext.h>
@@ -91,7 +92,7 @@ bool MonitorSocketClient::waitForEvent(MonitorSocketClient::Event &event) {
     int ret = TEMP_FAILURE_RETRY(poll(desc, 2, -1));
     if (ret == -1) {
         int err = errno;
-        LOGE("Poll returned with error: " << strerror(err));
+        LOGE("Poll returned with error: " << safeStrError(err));
         return false;
     }
     if (desc[1].revents & POLLIN) {
index cfc88f5342ec0d31fb2c1b86ebdd4935b1e309fa..996b15b22e3b0470292ec2e5be6a69d8c99fb1d6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2019 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.
@@ -31,6 +31,7 @@
 #include <unistd.h>
 
 #include <config/PathConfig.h>
+#include <error/SafeStrError.h>
 #include <exceptions/CannotCreateFileException.h>
 #include <exceptions/UnexpectedErrorException.h>
 #include <log/log.h>
@@ -52,8 +53,8 @@ bool Integrity::backupGuardExists(void) const {
     } else {
         int err = errno;
         if (err != ENOENT) {
-            LOGE("'stat' function error [%d] : <%s>", err, strerror(err));
-            throw UnexpectedErrorException(err, strerror(err));
+            LOGE("'stat' function error [%d] : <%s>", err, safeStrError(err).c_str());
+            throw UnexpectedErrorException(err, safeStrError(err));
         }
         return false;
     }
@@ -100,8 +101,8 @@ void Integrity::deleteNonIndexedFiles(BucketPresenceTester tester) {
 
     if ((dirPtr = opendir(m_dbPath.c_str())) == nullptr) {
         int err = errno;
-        LOGE("'opendir' function error [%d] : <%s>", err, strerror(err));
-        throw UnexpectedErrorException(err, strerror(err));
+        LOGE("'opendir' function error [%d] : <%s>", err, safeStrError(err).c_str());
+        throw UnexpectedErrorException(err, safeStrError(err));
         return;
     }
 
@@ -110,7 +111,7 @@ void Integrity::deleteNonIndexedFiles(BucketPresenceTester tester) {
                 if (closedir(dir) < 0) {
                     int err = errno;
                     (void) err;
-                    LOGE("'closedir' function error [%d] : <%s>", err, strerror(err));
+                    LOGE("'closedir' function error [%d] : <%s>", err, safeStrError(err).c_str());
                 }
             });
 
@@ -147,8 +148,8 @@ void Integrity::deleteNonIndexedFiles(BucketPresenceTester tester) {
 
     if (errno) {
         int err = errno;
-        LOGE("'readdir' function error [%d] : <%s>", err, strerror(err));
-        throw UnexpectedErrorException(err, strerror(err));
+        LOGE("'readdir' function error [%d] : <%s>", err, safeStrError(err).c_str());
+        throw UnexpectedErrorException(err, safeStrError(err));
         return;
     }
 }
@@ -160,8 +161,8 @@ void Integrity::syncElement(const std::string &filename, int flags, mode_t mode)
         int err = errno;
         if (err != EEXIST) {
             LOGE("File <%s> : 'open' function error [%d] : <%s>", filename.c_str(), err,
-                 strerror(err));
-            throw UnexpectedErrorException(err, strerror(err));
+                 safeStrError(err).c_str());
+            throw UnexpectedErrorException(err, safeStrError(err));
         } else {
             throw CannotCreateFileException(filename);
         }
@@ -171,16 +172,16 @@ void Integrity::syncElement(const std::string &filename, int flags, mode_t mode)
 
     if (ret < 0) {
         int err = errno;
-        LOGE("'fsync' function error [%d] : <%s>", err, strerror(err));
-        throw UnexpectedErrorException(err, strerror(err));
+        LOGE("'fsync' function error [%d] : <%s>", err, safeStrError(err).c_str());
+        throw UnexpectedErrorException(err, safeStrError(err));
     }
 
     ret = close(fileFd);
 
     if (ret < 0) {
         int err = errno;
-        LOGE("'close' function error [%d] : <%s>", err, strerror(err));
-        throw UnexpectedErrorException(err, strerror(err));
+        LOGE("'close' function error [%d] : <%s>", err, safeStrError(err).c_str());
+        throw UnexpectedErrorException(err, safeStrError(err));
     }
 }
 
@@ -230,7 +231,7 @@ void Integrity::createHardLink(const std::string &oldName, const std::string &ne
     if (ret < 0) {
         int err = errno;
         LOGN("Trying to link to non-existent file: <%s>", oldName.c_str());
-        throw UnexpectedErrorException(err, strerror(err));
+        throw UnexpectedErrorException(err, safeStrError(err));
     }
 }
 
@@ -240,8 +241,8 @@ void Integrity::deleteHardLink(const std::string &filename) {
     if (ret < 0) {
         int err = errno;
         if (err != ENOENT) {
-            LOGE("'unlink' function error [%d] : <%s>", err, strerror(err));
-            throw UnexpectedErrorException(err, strerror(err));
+            LOGE("'unlink' function error [%d] : <%s>", err, safeStrError(err).c_str());
+            throw UnexpectedErrorException(err, safeStrError(err));
         } else {
             LOGN("Trying to unlink non-existent file: <%s>", filename.c_str());
         }
index f1936a19ab4fd6856f3930cdb7f148fc1449fafd..c6e27124695e9d3ec225e004c1f1d6b1dc9d299d 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2014-2017 Samsung Electronics Co., Ltd All Rights Reserved
+# Copyright (c) 2014-2019 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,6 +27,7 @@ SET(CYNARA_SOURCES_FOR_TESTS
     ${CYNARA_SRC}/client-common/cache/MonitorCache.cpp
     ${CYNARA_SRC}/common/config/PathConfig.cpp
     ${CYNARA_SRC}/common/containers/BinaryQueue.cpp
+    ${CYNARA_SRC}/common/error/SafeStrError.cpp
     ${CYNARA_SRC}/common/protocol/ProtocolAdmin.cpp
     ${CYNARA_SRC}/common/protocol/ProtocolFrame.cpp
     ${CYNARA_SRC}/common/protocol/ProtocolFrameHeader.cpp