mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
修正和改进posix errno打印
This commit is contained in:
parent
8e070076fc
commit
a2e5cc7804
14
src/libipc/platform/posix/get_error.h
Normal file
14
src/libipc/platform/posix/get_error.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace ipc {
|
||||
namespace detail {
|
||||
|
||||
inline char const *curr_error() noexcept {
|
||||
return ::strerror(errno);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace ipc
|
||||
@ -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<unsigned>(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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,7 +4,6 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <string>
|
||||
@ -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<id_info_t>();
|
||||
@ -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;
|
||||
}
|
||||
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_);
|
||||
ipc::error("fail fstat[%s]: name = %s, size = %zd\n", curr_error(), ii->name_.c_str(), ii->size_);
|
||||
return nullptr;
|
||||
}
|
||||
if (ii->size_ == 0) {
|
||||
ii->size_ = static_cast<std::size_t>(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<off_t>(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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user