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
@ -89,7 +89,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
chan_wrapper clone() const {
|
chan_wrapper clone() const {
|
||||||
return chan_wrapper { name(), mode_ };
|
return chan_wrapper {name(), mode_};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
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 "libipc/shm.h"
|
||||||
|
|
||||||
#include "get_wait_time.h"
|
#include "get_wait_time.h"
|
||||||
|
#include "get_error.h"
|
||||||
|
|
||||||
namespace ipc {
|
namespace ipc {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
@ -40,7 +41,7 @@ public:
|
|||||||
}
|
}
|
||||||
h_ = ::sem_open(name, O_CREAT, 0666, static_cast<unsigned>(count));
|
h_ = ::sem_open(name, O_CREAT, 0666, static_cast<unsigned>(count));
|
||||||
if (h_ == SEM_FAILED) {
|
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 false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -49,14 +50,14 @@ public:
|
|||||||
void close() noexcept {
|
void close() noexcept {
|
||||||
if (!valid()) return;
|
if (!valid()) return;
|
||||||
if (::sem_close(h_) != 0) {
|
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;
|
h_ = SEM_FAILED;
|
||||||
if (shm_.name() != nullptr) {
|
if (shm_.name() != nullptr) {
|
||||||
std::string name = shm_.name();
|
std::string name = shm_.name();
|
||||||
if (shm_.release() <= 1) {
|
if (shm_.release() <= 1) {
|
||||||
if (::sem_unlink(name.c_str()) != 0) {
|
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 (!valid()) return false;
|
||||||
if (tm == invalid_value) {
|
if (tm == invalid_value) {
|
||||||
if (::sem_wait(h_) != 0) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
auto ts = detail::make_timespec(tm);
|
auto ts = detail::make_timespec(tm);
|
||||||
if (::sem_timedwait(h_, &ts) != 0) {
|
if (::sem_timedwait(h_, &ts) != 0) {
|
||||||
if (errno != ETIMEDOUT) {
|
if (errno != ETIMEDOUT) {
|
||||||
ipc::error("fail sem_timedwait[%d]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
|
ipc::error("fail sem_timedwait[%s]: tm = %zd, tv_sec = %ld, tv_nsec = %ld\n",
|
||||||
errno, tm, ts.tv_sec, ts.tv_nsec);
|
curr_error(), tm, ts.tv_sec, ts.tv_nsec);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -86,7 +87,7 @@ public:
|
|||||||
if (!valid()) return false;
|
if (!valid()) return false;
|
||||||
for (std::uint32_t i = 0; i < count; ++i) {
|
for (std::uint32_t i = 0; i < count; ++i) {
|
||||||
if (::sem_post(h_) != 0) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,6 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -18,6 +17,8 @@
|
|||||||
#include "libipc/utility/log.h"
|
#include "libipc/utility/log.h"
|
||||||
#include "libipc/memory/resource.h"
|
#include "libipc/memory/resource.h"
|
||||||
|
|
||||||
|
#include "get_error.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
struct info_t {
|
struct info_t {
|
||||||
@ -70,7 +71,7 @@ id_t acquire(char const * name, std::size_t size, unsigned mode) {
|
|||||||
S_IRGRP | S_IWGRP |
|
S_IRGRP | S_IWGRP |
|
||||||
S_IROTH | S_IWOTH);
|
S_IROTH | S_IWOTH);
|
||||||
if (fd == -1) {
|
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;
|
return nullptr;
|
||||||
}
|
}
|
||||||
auto ii = mem::alloc<id_info_t>();
|
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");
|
ipc::error("fail get_mem: invalid id (fd = -1)\n");
|
||||||
return nullptr;
|
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) {
|
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<std::size_t>(st.st_size);
|
ii->size_ = static_cast<std::size_t>(st.st_size);
|
||||||
if ((ii->size_ <= sizeof(info_t)) || (ii->size_ % sizeof(info_t))) {
|
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_);
|
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 {
|
else {
|
||||||
ii->size_ = calc_size(ii->size_);
|
ii->size_ = calc_size(ii->size_);
|
||||||
if (::ftruncate(fd, static_cast<off_t>(ii->size_)) != 0) {
|
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;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void* mem = ::mmap(nullptr, ii->size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
void* mem = ::mmap(nullptr, ii->size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||||
if (mem == MAP_FAILED) {
|
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;
|
return nullptr;
|
||||||
}
|
}
|
||||||
::close(fd);
|
::close(fd);
|
||||||
|
|||||||
@ -62,12 +62,12 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool notify() noexcept {
|
bool notify() noexcept {
|
||||||
{ IPC_UNUSED_ std::lock_guard<ipc::sync::mutex> guard{lock_}; } // barrier
|
{ IPC_UNUSED_ std::lock_guard<ipc::sync::mutex> guard {lock_}; } // barrier
|
||||||
return cond_.notify(lock_);
|
return cond_.notify(lock_);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool broadcast() noexcept {
|
bool broadcast() noexcept {
|
||||||
{ IPC_UNUSED_ std::lock_guard<ipc::sync::mutex> guard{lock_}; } // barrier
|
{ IPC_UNUSED_ std::lock_guard<ipc::sync::mutex> guard {lock_}; } // barrier
|
||||||
return cond_.broadcast(lock_);
|
return cond_.broadcast(lock_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user