diff --git a/include/libipc/ipc.h b/include/libipc/ipc.h index 64b262c..ceb26ea 100755 --- a/include/libipc/ipc.h +++ b/include/libipc/ipc.h @@ -89,7 +89,7 @@ public: } chan_wrapper clone() const { - return chan_wrapper { name(), mode_ }; + return chan_wrapper {name(), mode_}; } /** diff --git a/src/libipc/platform/posix/get_error.h b/src/libipc/platform/posix/get_error.h new file mode 100644 index 0000000..c552f22 --- /dev/null +++ b/src/libipc/platform/posix/get_error.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +namespace ipc { +namespace detail { + +inline char const *curr_error() noexcept { + return ::strerror(errno); +} + +} // namespace detail +} // namespace ipc diff --git a/src/libipc/platform/posix/semaphore_impl.h b/src/libipc/platform/posix/semaphore_impl.h index d48bcd4..94a5350 100644 --- a/src/libipc/platform/posix/semaphore_impl.h +++ b/src/libipc/platform/posix/semaphore_impl.h @@ -11,6 +11,7 @@ #include "libipc/shm.h" #include "get_wait_time.h" +#include "get_error.h" namespace ipc { namespace detail { @@ -40,7 +41,7 @@ public: } h_ = ::sem_open(name, O_CREAT, 0666, static_cast(count)); if (h_ == SEM_FAILED) { - ipc::error("fail sem_open[%d]: %s\n", errno, name); + ipc::error("fail sem_open[%s]: name = %s\n", curr_error(), name); return false; } return true; @@ -49,14 +50,14 @@ public: void close() noexcept { if (!valid()) return; if (::sem_close(h_) != 0) { - ipc::error("fail sem_close[%d]: %s\n", errno); + ipc::error("fail sem_close[%s]\n", curr_error()); } h_ = SEM_FAILED; if (shm_.name() != nullptr) { std::string name = shm_.name(); if (shm_.release() <= 1) { if (::sem_unlink(name.c_str()) != 0) { - ipc::error("fail sem_unlink[%d]: %s, name: %s\n", errno, name.c_str()); + ipc::error("fail sem_unlink[%s]: name = %s\n", curr_error(), name.c_str()); } } } @@ -66,15 +67,15 @@ public: if (!valid()) return false; if (tm == invalid_value) { if (::sem_wait(h_) != 0) { - ipc::error("fail sem_wait[%d]: %s\n", errno); + ipc::error("fail sem_wait[%s]\n", curr_error()); return false; } } else { auto ts = detail::make_timespec(tm); if (::sem_timedwait(h_, &ts) != 0) { if (errno != ETIMEDOUT) { - ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n", - errno, tm, ts.tv_sec, ts.tv_nsec); + ipc::error("fail sem_timedwait[%s]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n", + curr_error(), tm, ts.tv_sec, ts.tv_nsec); } return false; } @@ -86,7 +87,7 @@ public: if (!valid()) return false; for (std::uint32_t i = 0; i < count; ++i) { if (::sem_post(h_) != 0) { - ipc::error("fail sem_post[%d]: %s\n", errno); + ipc::error("fail sem_post[%s]\n", curr_error()); return false; } } diff --git a/src/libipc/platform/posix/shm_posix.cpp b/src/libipc/platform/posix/shm_posix.cpp index 7f70b07..7ed98c8 100644 --- a/src/libipc/platform/posix/shm_posix.cpp +++ b/src/libipc/platform/posix/shm_posix.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include @@ -18,6 +17,8 @@ #include "libipc/utility/log.h" #include "libipc/memory/resource.h" +#include "get_error.h" + namespace { struct info_t { @@ -70,7 +71,7 @@ id_t acquire(char const * name, std::size_t size, unsigned mode) { S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if (fd == -1) { - ipc::error("fail shm_open[%d]: %s\n", errno, name); + ipc::error("fail shm_open[%s]: name = %s\n", curr_error(), name); return nullptr; } auto ii = mem::alloc(); @@ -119,12 +120,12 @@ void * get_mem(id_t id, std::size_t * size) { ipc::error("fail get_mem: invalid id (fd = -1)\n"); return nullptr; } + struct stat st; + if (::fstat(fd, &st) != 0) { + ipc::error("fail fstat[%s]: name = %s, size = %zd\n", curr_error(), ii->name_.c_str(), ii->size_); + return nullptr; + } if (ii->size_ == 0) { - struct stat st; - if (::fstat(fd, &st) != 0) { - ipc::error("fail fstat[%d]: %s, size = %zd\n", errno, ii->name_.c_str(), ii->size_); - return nullptr; - } ii->size_ = static_cast(st.st_size); if ((ii->size_ <= sizeof(info_t)) || (ii->size_ % sizeof(info_t))) { ipc::error("fail get_mem: %s, invalid size = %zd\n", ii->name_.c_str(), ii->size_); @@ -134,13 +135,13 @@ void * get_mem(id_t id, std::size_t * size) { else { ii->size_ = calc_size(ii->size_); if (::ftruncate(fd, static_cast(ii->size_)) != 0) { - ipc::error("fail ftruncate[%d]: %s, size = %zd\n", errno, ii->name_.c_str(), ii->size_); + ipc::error("fail ftruncate[%s]: name = %s, size = %zd\n", curr_error(), ii->name_.c_str(), ii->size_); return nullptr; } } void* mem = ::mmap(nullptr, ii->size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (mem == MAP_FAILED) { - ipc::error("fail mmap[%d]: %s, size = %zd\n", errno, ii->name_.c_str(), ii->size_); + ipc::error("fail mmap[%s]: name = %s, size = %zd\n", curr_error(), ii->name_.c_str(), ii->size_); return nullptr; } ::close(fd); diff --git a/src/libipc/waiter.h b/src/libipc/waiter.h index ce4e592..130bc98 100644 --- a/src/libipc/waiter.h +++ b/src/libipc/waiter.h @@ -62,12 +62,12 @@ public: } bool notify() noexcept { - { IPC_UNUSED_ std::lock_guard guard{lock_}; } // barrier + { IPC_UNUSED_ std::lock_guard guard {lock_}; } // barrier return cond_.notify(lock_); } bool broadcast() noexcept { - { IPC_UNUSED_ std::lock_guard guard{lock_}; } // barrier + { IPC_UNUSED_ std::lock_guard guard {lock_}; } // barrier return cond_.broadcast(lock_); }