{
// 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);
}
}
{
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);
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);
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);
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);
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);
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);
}
if (::setgroups(gids.size(), gids.data()) != 0) {
- LOGE("setgroups() failed: " << strerror(errno));
+ LOGE("setgroups() failed: " << getSystemErrorMessage());
return false;
}
{
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);
}
}
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) {
// 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);
// 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);
{
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;
}
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);
}
}
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;
}
}
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;
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;
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;
}
::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());
}
}
}
// 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;
}
#include "utils/img.hpp"
#include "utils/fs.hpp"
#include "utils/paths.hpp"
+#include "base-exception.hpp"
#include <sys/mount.h>
#include <fcntl.h>
// 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;
}
// 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;
// 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);
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;
}
{
::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*/);
#include "config.hpp"
#include "utils/vt.hpp"
#include "logger/logger.hpp"
+#include "base-exception.hpp"
#include <fcntl.h>
#include <sys/ioctl.h>
{
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;
}
// 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;
}
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)
if (errno == EINTR) {
continue;
}
- throw ConfigException("Error in poll: " + std::string(strerror(errno)));
+ throw ConfigException("Error in poll: " + getSystemErrorMessage());
}
if (ret == 0) {
} 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);
} 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);
{
if (gUpdateTriggered) {
execve(argv[0], argv, environ);
-
- LOGE("Failed to reload " << argv[0] << ": " << strerror(errno));
+ LOGE("Failed to reload " << argv[0] << ": " << getSystemErrorMessage());
}
}