From be9e3ad3470922965241f606090664618f43ae9b Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Mon, 16 May 2022 17:26:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=90=8C=E4=B8=80=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E7=9A=84=E4=BA=92=E6=96=A5=E9=94=81=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hgdriver/hgdev/hg_ipc.cpp | 70 +++++++++++++++++++++++++++++++++- hgdriver/hgdev/hg_ipc.h | 4 ++ hgdriver/hgdev/usb_manager.cpp | 10 ++--- 3 files changed, 77 insertions(+), 7 deletions(-) diff --git a/hgdriver/hgdev/hg_ipc.cpp b/hgdriver/hgdev/hg_ipc.cpp index 86a2fb7..246cf1a 100644 --- a/hgdriver/hgdev/hg_ipc.cpp +++ b/hgdriver/hgdev/hg_ipc.cpp @@ -156,20 +156,50 @@ void shared_memory::init(void) int obj = shmget(key_, bytes_, IPC_EXCL | IPC_CREAT | 0600); if (obj < 0) { + unsigned int* v = (unsigned int*)&key_; if (errno == EEXIST) { first_ = false; obj = shmget(key_, bytes_, 0600); - if(read().empty()) + if(obj == -1) + obj = shmget(key_, bytes_, 0); + HG_VLOG_MINI_3(HG_LOG_LEVEL_DEBUG_INFO, "open existing: shmget(0x%x%08x) = %d\n", v[1], v[0], obj); + obj_ = (void*)obj; + + std::string prev(read()), proc(""); + HG_VLOG_MINI_1(HG_LOG_LEVEL_DEBUG_INFO, "shared memory content: %s\n", prev.c_str()); + if(prev.length()) + { + proc = prev; + size_t pos = proc.find("pid: "); + if (pos != std::string::npos) + proc.erase(0, pos + 5); + pos = proc.find(")"); + if (pos != std::string::npos) + proc.erase(pos); + proc = shared_memory::get_proc_name_by_pid(atoi(proc.c_str())); + if (proc.length()) + { + pos = prev.find("("); + if (pos == std::string::npos) + pos = prev.length(); + if (strcasecmp(proc.c_str(), prev.substr(0, pos).c_str())) + proc = ""; + } + } + if (proc.empty()) { first_ = true; clear(); obj = shmget(key_, bytes_, IPC_EXCL | IPC_CREAT | 0600); - HG_VLOG_MINI_1(HG_LOG_LEVEL_DEBUG_INFO, "shared memory id(%d) already exists but is not accessible, close and reopen it\n", obj); + HG_VLOG_MINI_1(HG_LOG_LEVEL_DEBUG_INFO, "%s is not existing and reopen it\n", prev.c_str()); } } else + { + HG_VLOG_MINI_3(HG_LOG_LEVEL_DEBUG_INFO, "shmget(0x%x%08x) = %d\n", v[1], v[0], errno); return; + } } obj_ = (void*)obj; HG_VLOG_MINI_2(HG_LOG_LEVEL_DEBUG_INFO, "shared memory id = %d[%s], \n", obj, first_ ? "created" : "opened"); @@ -232,6 +262,42 @@ void shared_memory::release_buf(void* buf) #endif } +#ifndef WIN32 +std::string shared_memory::get_proc_name_by_pid(pid_t pid) +{ + char path[512] = { 0 }; + unsigned int* v = (unsigned int*)&pid; + std::string ret(""); + + if (sizeof(pid) > 4 && v[1]) + sprintf(path, "/proc/%lld/status", pid); + else + sprintf(path, "/proc/%u/status", pid); + + FILE* src = fopen(path, "rb"); + if (src) + { + char val[512] = { 0 }; + + memset(path, 0, sizeof(path)); + fgets(path, sizeof(path) - 1, src); + fclose(src); + sscanf(path, "%*s %s", val); + ret = val; + } + if (sizeof(pid) > 4 && v[1]) + { + HG_VLOG_MINI_2(HG_LOG_LEVEL_DEBUG_INFO, "PID(%lld) name is: %s\n", pid, ret.c_str()); + } + else + { + HG_VLOG_MINI_2(HG_LOG_LEVEL_DEBUG_INFO, "PID(%u) name is: %s\n", pid, ret.c_str()); + } + + return ret; +} +#endif + bool shared_memory::is_ok(void) { return obj_ != nullptr; diff --git a/hgdriver/hgdev/hg_ipc.h b/hgdriver/hgdev/hg_ipc.h index 5e136d6..4cae825 100644 --- a/hgdriver/hgdev/hg_ipc.h +++ b/hgdriver/hgdev/hg_ipc.h @@ -110,6 +110,10 @@ class shared_memory : public refer char* get_buf(void); void release_buf(void* buf); +#ifndef WIN32 + static std::string get_proc_name_by_pid(pid_t pid); +#endif + public: shared_memory(unsigned long long key, size_t size = 1024); diff --git a/hgdriver/hgdev/usb_manager.cpp b/hgdriver/hgdev/usb_manager.cpp index 456f376..02e8ab5 100644 --- a/hgdriver/hgdev/usb_manager.cpp +++ b/hgdriver/hgdev/usb_manager.cpp @@ -497,7 +497,7 @@ void usb_manager::enum_endpoints(libusb_device* device, USBTRANSENDP* endp) else s = &ep->out; // = (conf->interface[j].altsetting[k].endpoint[l].bEndpointAddress & 3) | LIBUSB_ENDPOINT_OUT; - // NOTE: 这里应该尽量将输入输出端口统一到同一个接口上来,目前未做,只取第一个 + // NOTE: 这里应该尽量将输入输出端口统一到同一个接口上来,目前未做,只取第一? if (s->port == usb_manager::uninit_uint8) { s->port = conf->interface[j].altsetting[k].endpoint[l].bEndpointAddress & (LIBUSB_ENDPOINT_IN | LIBUSB_ENDPOINT_OUT | 3); @@ -626,14 +626,14 @@ bool usb_io::make_singleton(void) return true; } - char msg[128] = { 0 }; std::string str(singleton_->read()); singleton_->release(); singleton_ = nullptr; last_err_ = HG_ERR_OPENED_BY_OTHER_PROCESS; - sprintf(msg, "\350\256\276\345\244\207\345\267\262\347\273\217\350\242\253\350\277\233\347\250\213%s\345\215\240\347\224\250", str.c_str()); - init_err_msg_ = msg; + str.insert(0, "\350\256\276\345\244\207\345\267\262\347\273\217\350\242\253\350\277\233\347\250\213 '"); + str += "' \345\215\240\347\224\250"; + init_err_msg_ = str; return false; } @@ -791,7 +791,7 @@ bool usb_io::on_io_error(hg_err err, usb_manager::USBSIMPLEX* endp) if (err == HG_ERR_TIMEOUT) { - //因为在发送img参数出现timeout,暂时禁用 + //因为在发送img参数出现timeout,暂时禁? // //HG_LOG(HG_LOG_LEVEL_DEBUG_INFO, "Operation timeout\n"); // libusb_clear_halt(handle_, endp->port);