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
/*
- * 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.
#include <sys/ioctl.h>
#include <unistd.h>
+#include <error/SafeStrError.h>
#include <log/log.h>
#include <protocol/Protocol.h>
#include <request/pointers.h>
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) {
-# 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.
${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
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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_
/*
- * 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.
#include <cstring>
#include <string>
+#include <error/SafeStrError.h>
#include <exceptions/Exception.h>
namespace Cynara {
}
const std::string errorString(void) const {
- return strerror(m_errno);
+ return safeStrError(m_errno);
}
private:
/*
- * 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.
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() {};
/*
- * 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.
#include <sys/eventfd.h>
#include <unistd.h>
+#include <error/SafeStrError.h>
#include <log/log.h>
#include "FdNotifyObject.h"
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;
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;
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;
/*
- * 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.
#define _BSD_SOURCE_
+#include <cerrno>
#include <cinttypes>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <attributes/attributes.h>
+#include <error/SafeStrError.h>
#include <exceptions/UnknownPolicyTypeException.h>
#include <log/log.h>
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;
}
/*
- * 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.
#include <sys/un.h>
#include <unistd.h>
+#include <error/SafeStrError.h>
#include <exceptions/AccessDeniedException.h>
#include <exceptions/InitException.h>
#include <exceptions/NoMemoryException.h>
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");
}
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;
}
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 ||
{
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));
}
}
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;
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);
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));
}
}
/*
- * 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) {
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;
}
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;
/* 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;
}
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));
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));
/*
- * 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>
*
#include <unistd.h>
#include <config/PathConfig.h>
+#include <error/SafeStrError.h>
#include <log/log.h>
#include <request/Request.h>
#include <request/RequestContext.h>
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) {
/*
- * 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.
#include <unistd.h>
#include <config/PathConfig.h>
+#include <error/SafeStrError.h>
#include <exceptions/CannotCreateFileException.h>
#include <exceptions/UnexpectedErrorException.h>
#include <log/log.h>
} 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;
}
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;
}
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());
}
});
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;
}
}
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);
}
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));
}
}
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));
}
}
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());
}
-# 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.
${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