Remove strerror usage 54/38854/5
authorMateusz Malicki <m.malicki2@samsung.com>
Wed, 29 Apr 2015 10:49:46 +0000 (12:49 +0200)
committerMateusz Malicki <m.malicki2@samsung.com>
Wed, 6 May 2015 14:27:12 +0000 (16:27 +0200)
[Bug]           strerror isn't thread-safe
[Cause]         Thread usage
[Solution]      Replaces all strerror with strerror_r
[Verification]  Build, run test

Change-Id: I9fc5e84da011d80af7fb7a7877fe868e8e300ea9

common/ipc/internals/socket.cpp
common/utils/environment.cpp
common/utils/eventfd.cpp
common/utils/fd-utils.cpp
common/utils/fs.cpp
common/utils/img.cpp
common/utils/signal.cpp
common/utils/vt.cpp
libs/config/fdstore.cpp
server/server.cpp

index 3cdbf7c..26e1c27 100644 (file)
@@ -49,8 +49,9 @@ void setFdOptions(int fd)
 {
     // Prevent from inheriting fd by zones
     if (-1 == ::fcntl(fd, F_SETFD, FD_CLOEXEC)) {
-        LOGE("Error in fcntl: " + std::string(strerror(errno)));
-        throw IPCException("Error in fcntl: " + std::string(strerror(errno)));
+        const std::string msg = getSystemErrorMessage();
+        LOGE("Error in fcntl: " + msg);
+        throw IPCException("Error in fcntl: " + msg);
     }
 }
 
@@ -90,8 +91,9 @@ std::shared_ptr<Socket> Socket::accept()
 {
     int sockfd = ::accept(mFD, nullptr, nullptr);
     if (sockfd == -1) {
-        LOGE("Error in accept: " << std::string(strerror(errno)));
-        throw IPCException("Error in accept: " + std::string(strerror(errno)));
+        const std::string msg = getSystemErrorMessage();
+        LOGE("Error in accept: " << msg);
+        throw IPCException("Error in accept: " + msg);
     }
     setFdOptions(sockfd);
     return std::make_shared<Socket>(sockfd);
@@ -139,8 +141,9 @@ int Socket::createZoneSocket(const std::string& path)
 
     int sockfd = ::socket(AF_UNIX, SOCK_STREAM, 0);
     if (sockfd == -1) {
-        LOGE("Error in socket: " + std::string(strerror(errno)));
-        throw IPCException("Error in socket: " + std::string(strerror(errno)));
+        const std::string msg = getSystemErrorMessage();
+        LOGE("Error in socket: " + msg);
+        throw IPCException("Error in socket: " + msg);
     }
     setFdOptions(sockfd);
 
@@ -154,7 +157,7 @@ int Socket::createZoneSocket(const std::string& path)
     if (-1 == ::bind(sockfd,
                      reinterpret_cast<struct sockaddr*>(&serverAddress),
                      sizeof(struct sockaddr_un))) {
-        std::string message = strerror(errno);
+        std::string message = getSystemErrorMessage();
         utils::close(sockfd);
         LOGE("Error in bind: " << message);
         throw IPCException("Error in bind: " + message);
@@ -162,7 +165,7 @@ int Socket::createZoneSocket(const std::string& path)
 
     if (-1 == ::listen(sockfd,
                        MAX_QUEUE_LENGTH)) {
-        std::string message = strerror(errno);
+        std::string message = getSystemErrorMessage();
         utils::close(sockfd);
         LOGE("Error in listen: " << message);
         throw IPCException("Error in listen: " + message);
@@ -190,8 +193,9 @@ Socket Socket::connectSocket(const std::string& path)
 
     int fd = socket(AF_UNIX, SOCK_STREAM, 0);
     if (fd == -1) {
-        LOGE("Error in socket: " + std::string(strerror(errno)));
-        throw IPCException("Error in socket: " + std::string(strerror(errno)));
+        const std::string msg = getSystemErrorMessage();
+        LOGE("Error in socket: " + msg);
+        throw IPCException("Error in socket: " + msg);
     }
     setFdOptions(fd);
 
@@ -202,17 +206,19 @@ Socket Socket::connectSocket(const std::string& path)
     if (-1 == connect(fd,
                       reinterpret_cast<struct sockaddr*>(&serverAddress),
                       sizeof(struct sockaddr_un))) {
+        const std::string msg = getSystemErrorMessage();
         utils::close(fd);
-        LOGE("Error in connect: " + std::string(strerror(errno)));
-        throw IPCException("Error in connect: " + std::string(strerror(errno)));
+        LOGE("Error in connect: " + msg);
+        throw IPCException("Error in connect: " + msg);
     }
 
     // Nonblock socket
     int flags = fcntl(fd, F_GETFL, 0);
     if (-1 == fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
+        const std::string msg = getSystemErrorMessage();
         utils::close(fd);
-        LOGE("Error in fcntl: " + std::string(strerror(errno)));
-        throw IPCException("Error in fcntl: " + std::string(strerror(errno)));
+        LOGE("Error in fcntl: " + msg);
+        throw IPCException("Error in fcntl: " + msg);
     }
 
     return Socket(fd);
index 511fcda..209f9ba 100644 (file)
@@ -147,7 +147,7 @@ bool setSuppGroups(const std::vector<std::string>& groups)
     }
 
     if (::setgroups(gids.size(), gids.data()) != 0) {
-        LOGE("setgroups() failed: " << strerror(errno));
+        LOGE("setgroups() failed: " << getSystemErrorMessage());
         return false;
     }
 
index 2fd8d62..d69d572 100644 (file)
@@ -41,8 +41,9 @@ EventFD::EventFD()
 {
     mFD = ::eventfd(0, EFD_SEMAPHORE | EFD_CLOEXEC);
     if (mFD == -1) {
-        LOGE("Error in eventfd: " << getSystemErrorMessage());
-        throw UtilsException("Error in eventfd: " + getSystemErrorMessage());
+        const std::string msg = getSystemErrorMessage();
+        LOGE("Error in eventfd: " << msg);
+        throw UtilsException("Error in eventfd: " + msg);
     }
 }
 
index 59bad36..17448b5 100644 (file)
@@ -67,8 +67,9 @@ void waitForEvent(int fd,
             if (errno == EINTR) {
                 continue;
             }
-            LOGE("Error in poll: " + getSystemErrorMessage());
-            throw UtilsException("Error in poll: " + getSystemErrorMessage());
+            const std::string msg = getSystemErrorMessage();
+            LOGE("Error in poll: " + msg);
+            throw UtilsException("Error in poll: " + msg);
         }
 
         if (ret == 0) {
@@ -128,8 +129,9 @@ void write(int fd, const void* bufferPtr, const size_t size, int timeoutMS)
             // Neglected errors
             LOGD("Retrying write");
         } else {
-            LOGE("Error during writing: " + getSystemErrorMessage());
-            throw UtilsException("Error during writing: " + getSystemErrorMessage());
+            const std::string msg = getSystemErrorMessage();
+            LOGE("Error during writing: " + msg);
+            throw UtilsException("Error during writing: " + msg);
         }
 
         waitForEvent(fd, POLLOUT, deadline);
@@ -160,8 +162,9 @@ void read(int fd, void* bufferPtr, const size_t size, int timeoutMS)
             // Neglected errors
             LOGD("Retrying read");
         } else {
-            LOGE("Error during reading: " + getSystemErrorMessage());
-            throw UtilsException("Error during reading: " + getSystemErrorMessage());
+            const std::string msg = getSystemErrorMessage();
+            LOGE("Error during reading: " + msg);
+            throw UtilsException("Error during reading: " + msg);
         }
 
         waitForEvent(fd, POLLIN, deadline);
@@ -172,8 +175,9 @@ unsigned int getMaxFDNumber()
 {
     struct rlimit rlim;
     if (-1 ==  getrlimit(RLIMIT_NOFILE, &rlim)) {
-        LOGE("Error during getrlimit: " + getSystemErrorMessage());
-        throw UtilsException("Error during getrlimit: " + getSystemErrorMessage());
+        const std::string msg = getSystemErrorMessage();
+        LOGE("Error during getrlimit: " + msg);
+        throw UtilsException("Error during getrlimit: " + msg);
     }
     return rlim.rlim_cur;
 }
@@ -184,8 +188,9 @@ void setMaxFDNumber(unsigned int limit)
     rlim.rlim_cur = limit;
     rlim.rlim_max = limit;
     if (-1 ==  setrlimit(RLIMIT_NOFILE, &rlim)) {
-        LOGE("Error during setrlimit: " + getSystemErrorMessage());
-        throw UtilsException("Error during setrlimit: " + getSystemErrorMessage());
+        const std::string msg = getSystemErrorMessage();
+        LOGE("Error during setrlimit: " + msg);
+        throw UtilsException("Error during setrlimit: " + msg);
     }
 }
 
index e3a59b3..e4dcd7f 100644 (file)
@@ -122,7 +122,7 @@ bool removeFile(const std::string& path)
     LOGD(path << ": exists, removing.");
     if (::remove(path.c_str())) {
         if (errno != ENOENT) {
-            LOGE(path << ": failed to delete: " << ::strerror(errno));
+            LOGE(path << ": failed to delete: " << getSystemErrorMessage());
             return false;
         }
     }
@@ -145,7 +145,7 @@ const unsigned long RUN_MOUNT_POINT_FLAGS = MS_NOSUID | MS_NODEV | MS_STRICTATIM
 bool mountTmpfs(const std::string& path, unsigned long flags, const std::string& options)
 {
     if (::mount("tmpfs", path.c_str(), "tmpfs", flags, options.c_str()) != 0) {
-        LOGD("Mount failed for '" << path << "', options=" << options << ": " << strerror(errno));
+        LOGD("Mount failed for '" << path << "', options=" << options << ": " << getSystemErrorMessage());
         return false;
     }
     return true;
@@ -192,7 +192,7 @@ bool mount(const std::string& source,
 bool umount(const std::string& path)
 {
     if (::umount(path.c_str()) != 0) {
-        LOGD("Umount failed for '" << path << "': " << strerror(errno));
+        LOGD("Umount failed for '" << path << "': " << getSystemErrorMessage());
         return false;
     }
     return true;
@@ -213,12 +213,12 @@ bool hasSameMountPoint(const std::string& path1, const std::string& path2, bool&
     struct stat s1, s2;
 
     if (::stat(path1.c_str(), &s1)) {
-        LOGD("Failed to get stat of " << path1 << ": " << strerror(errno));
+        LOGD("Failed to get stat of " << path1 << ": " << getSystemErrorMessage());
         return false;
     }
 
     if (::stat(path2.c_str(), &s2)) {
-        LOGD("Failed to get stat of " << path2 << ": " << strerror(errno));
+        LOGD("Failed to get stat of " << path2 << ": " << getSystemErrorMessage());
         return false;
     }
 
@@ -305,11 +305,11 @@ bool copyDirContentsRec(const boost::filesystem::path& src, const boost::filesys
             ::stat(current.string().c_str(), &info);
             if (fs::is_symlink(destination)) {
                 if (::lchown(destination.string().c_str(), info.st_uid, info.st_gid) < 0) {
-                    LOGW("Failed to change owner of symlink " << destination.string() << ": " << strerror(errno));
+                    LOGW("Failed to change owner of symlink " << destination.string() << ": " << getSystemErrorMessage());
                 }
             } else {
                 if (::chown(destination.string().c_str(), info.st_uid, info.st_gid) < 0) {
-                    LOGW("Failed to change owner of file " << destination.string() << ": " << strerror(errno));
+                    LOGW("Failed to change owner of file " << destination.string() << ": " << getSystemErrorMessage());
                 }
             }
         }
@@ -366,11 +366,12 @@ bool createDir(const std::string& path, uid_t uid, uid_t gid, boost::filesystem:
 
     // set owner
     if (::chown(path.c_str(), uid, gid) != 0) {
+        int err = errno;
         // remove the directory only if it hadn't existed before
         if (runDirCreated) {
             fs::remove(dirPath);
         }
-        LOGE("chown() failed for path '" << path << "': " << strerror(errno));
+        LOGE("chown() failed for path '" << path << "': " << getSystemErrorMessage(err));
         return false;
     }
 
index a33b5fd..785291c 100644 (file)
@@ -27,6 +27,7 @@
 #include "utils/img.hpp"
 #include "utils/fs.hpp"
 #include "utils/paths.hpp"
+#include "base-exception.hpp"
 
 #include <sys/mount.h>
 #include <fcntl.h>
@@ -52,7 +53,7 @@ bool isLoopDevFree(const std::string& loopdev, bool& ret)
     // open loop device FD
     int loopFD = ::open(loopdev.c_str(), O_RDWR);
     if (loopFD < 0) {
-        LOGD("Failed to open loop device descriptor: " << ::strerror(errno));
+        LOGD("Failed to open loop device descriptor: " << getSystemErrorMessage());
         return false;
     }
 
@@ -77,21 +78,21 @@ bool mountLoop(const std::string& img,
     // get image file  FD
     int fileFD = ::open(img.c_str(), O_RDWR);
     if (fileFD < 0) {
-        LOGD("Failed to open image file descriptor: " << ::strerror(errno));
+        LOGD("Failed to open image file descriptor: " << getSystemErrorMessage());
         return false;
     }
 
     // get loop device FD
     int loopFD = ::open(loopdev.c_str(), O_RDWR);
     if (loopFD < 0) {
-        LOGD("Failed to open loop device descriptor: " << ::strerror(errno));
+        LOGD("Failed to open loop device descriptor: " << getSystemErrorMessage());
         ::close(fileFD);
         return false;
     }
 
     // set loop device
     if (::ioctl(loopFD, LOOP_SET_FD, fileFD)) {
-        LOGD("Failed to assign loop device to image: " << ::strerror(errno));
+        LOGD("Failed to assign loop device to image: " << getSystemErrorMessage());
         ::close(fileFD);
         ::close(loopFD);
         return false;
@@ -99,7 +100,7 @@ bool mountLoop(const std::string& img,
 
     // mount loop device to path
     if (::mount(loopdev.c_str(), path.c_str(), type.c_str(), flags, options.c_str()) != 0) {
-        LOGD("Mount failed for '" << path << "', options=" << options << ": " << strerror(errno));
+        LOGD("Mount failed for '" << path << "', options=" << options << ": " << getSystemErrorMessage());
         ::ioctl(loopFD, LOOP_CLR_FD, 0);
         ::close(fileFD);
         ::close(loopFD);
@@ -150,7 +151,7 @@ bool mountImage(const std::string& image, const std::string& path, const std::st
 bool umountImage(const std::string& path, const std::string& loopdev)
 {
     if (::umount(path.c_str()) != 0) {
-        LOGD("Umount failed for '" << path << "': " << strerror(errno));
+        LOGD("Umount failed for '" << path << "': " << getSystemErrorMessage());
         return false;
     }
 
index fdb571c..11ea8f2 100644 (file)
@@ -38,13 +38,15 @@ void signalBlock(const int signalToBlock)
 {
     ::sigset_t set;
     if (-1 == ::sigemptyset(&set)) {
-        LOGE("Error in sigemptyset: " << std::string(strerror(errno)));
-        throw UtilsException("Error in sigemptyset: " + std::string(strerror(errno)));
+        const std::string msg = getSystemErrorMessage();
+        LOGE("Error in sigemptyset: " << msg);
+        throw UtilsException("Error in sigemptyset: " + msg);
     }
 
     if (-1 ==::sigaddset(&set, signalToBlock)) {
-        LOGE("Error in sigaddset: " << std::string(strerror(errno)));
-        throw UtilsException("Error in sigaddset: " + std::string(strerror(errno)));
+        const std::string msg = getSystemErrorMessage();
+        LOGE("Error in sigaddset: " << msg);
+        throw UtilsException("Error in sigaddset: " + msg);
     }
 
     int ret = ::pthread_sigmask(SIG_BLOCK, &set, nullptr /*&oldSet*/);
index df8a923..44461e4 100644 (file)
@@ -25,6 +25,7 @@
 #include "config.hpp"
 #include "utils/vt.hpp"
 #include "logger/logger.hpp"
+#include "base-exception.hpp"
 
 #include <fcntl.h>
 #include <sys/ioctl.h>
@@ -46,14 +47,14 @@ bool activateVT(const int& vt)
 {
     int consoleFD = ::open(TTY_DEV.c_str(), O_WRONLY);
     if (consoleFD < 0) {
-        LOGE("console open failed: " << errno << " (" << strerror(errno) << ")");
+        LOGE("console open failed: " << errno << " (" << getSystemErrorMessage() << ")");
         return false;
     }
 
     struct vt_stat vtstat;
     vtstat.v_active = 0;
     if (::ioctl(consoleFD, VT_GETSTATE, &vtstat)) {
-        LOGE("Failed to get vt state: " << errno << " (" << strerror(errno) << ")");
+        LOGE("Failed to get vt state: " << errno << " (" << getSystemErrorMessage() << ")");
         ::close(consoleFD);
         return false;
     }
@@ -66,14 +67,14 @@ bool activateVT(const int& vt)
 
     // activate vt
     if (::ioctl(consoleFD, VT_ACTIVATE, vt)) {
-        LOGE("Failed to activate vt" << vt << ": " << errno << " (" << strerror(errno) << ")");
+        LOGE("Failed to activate vt" << vt << ": " << errno << " (" << getSystemErrorMessage() << ")");
         ::close(consoleFD);
         return false;
     }
 
     // wait until activation is finished
     if (::ioctl(consoleFD, VT_WAITACTIVE, vt)) {
-        LOGE("Failed to wait for vt" << vt << " activation: " << errno << " (" << strerror(errno) << ")");
+        LOGE("Failed to wait for vt" << vt << " activation: " << errno << " (" << getSystemErrorMessage() << ")");
         ::close(consoleFD);
         return false;
     }
index 2b761eb..c0b7d08 100644 (file)
@@ -37,6 +37,15 @@ namespace config {
 
 namespace {
 
+const int ERROR_MESSAGE_BUFFER_CAPACITY = 256;
+
+std::string getSystemErrorMessage()
+{
+    char buf[ERROR_MESSAGE_BUFFER_CAPACITY];
+    return strerror_r(errno, buf, sizeof(buf));
+}
+
+
 void waitForEvent(int fd,
                   short event,
                   const std::chrono::high_resolution_clock::time_point deadline)
@@ -59,7 +68,7 @@ void waitForEvent(int fd,
             if (errno == EINTR) {
                 continue;
             }
-            throw ConfigException("Error in poll: " + std::string(strerror(errno)));
+            throw ConfigException("Error in poll: " + getSystemErrorMessage());
         }
 
         if (ret == 0) {
@@ -110,7 +119,7 @@ void FDStore::write(const void* bufferPtr, const size_t size, const unsigned int
         } else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
             // Neglected errors
         } else {
-            throw ConfigException("Error during writing: " + std::string(strerror(errno)));
+            throw ConfigException("Error during writing: " + getSystemErrorMessage());
         }
 
         waitForEvent(mFD, POLLOUT, deadline);
@@ -139,7 +148,7 @@ void FDStore::read(void* bufferPtr, const size_t size, const unsigned int timeou
         } else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
             // Neglected errors
         } else {
-            throw ConfigException("Error during reading: " + std::string(strerror(errno)));
+            throw ConfigException("Error during reading: " + getSystemErrorMessage());
         }
 
         waitForEvent(mFD, POLLIN, deadline);
index f64ea03..ab8cba3 100644 (file)
@@ -130,8 +130,7 @@ void Server::reloadIfRequired(char* argv[])
 {
     if (gUpdateTriggered) {
         execve(argv[0], argv, environ);
-
-        LOGE("Failed to reload " << argv[0] << ": " << strerror(errno));
+        LOGE("Failed to reload " << argv[0] << ": " << getSystemErrorMessage());
     }
 }