From 2ade2811c606908d4e10e7e2aaaf4773755c27d7 Mon Sep 17 00:00:00 2001 From: Katz Yamada Date: Fri, 23 Mar 2018 10:32:51 -0700 Subject: [PATCH] fix: LocIpc client app unable to exit LocIpc client apps such as garden app is unable to delete LocIpc object since its socket listening thread cannot be closed while it is waiting for data and cannot be closed. Fixed to close it by sending an abort message. CRs-Fixed: 2213212 Change-Id: I95f26862e9faf7bd75a2f447421ba4ab7220576e --- utils/LocIpc.cpp | 38 ++++++++++++++++++++++++-------------- utils/LocIpc.h | 3 +-- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/utils/LocIpc.cpp b/utils/LocIpc.cpp index 26a20765..59b659e4 100644 --- a/utils/LocIpc.cpp +++ b/utils/LocIpc.cpp @@ -28,19 +28,8 @@ */ #include -#include -#include #include -#include -#include -#include -#include -#include -#include #include -#include -#include -#include "gps_extended_c.h" #include "LocIpc.h" namespace loc_util { @@ -52,8 +41,10 @@ namespace loc_util { #define LOC_MSG_BUF_LEN 8192 #define LOC_MSG_HEAD "$MSGLEN$" +#define LOC_MSG_ABORT "LocIpcMsg::ABORT" class LocIpcRunnable : public LocRunnable { +friend LocIpc; public: LocIpcRunnable(LocIpc& locIpc, const std::string& ipcName) : mLocIpc(locIpc), mIpcName(ipcName) {} @@ -70,10 +61,10 @@ private: }; bool LocIpc::startListeningNonBlocking(const std::string& name) { - mRunnable.reset(new LocIpcRunnable(*this, name)); + mRunnable = new LocIpcRunnable(*this, name); std::string threadName("LocIpc-"); threadName.append(name); - return mThread.start(threadName.c_str(), mRunnable.get()); + return mThread.start(threadName.c_str(), mRunnable); } bool LocIpc::startListeningBlocking(const std::string& name) { @@ -107,6 +98,7 @@ bool LocIpc::startListeningBlocking(const std::string& name) { ssize_t nBytes = 0; std::string msg = ""; + std::string abort = LOC_MSG_ABORT; while (1) { msg.resize(LOC_MSG_BUF_LEN); nBytes = ::recvfrom(mIpcFd, (void*)(msg.data()), msg.size(), 0, NULL, NULL); @@ -116,6 +108,11 @@ bool LocIpc::startListeningBlocking(const std::string& name) { continue; } + if (strncmp(msg.data(), abort.c_str(), abort.length()) == 0) { + LOC_LOGi("recvd abort msg.data %s", msg.data()); + break; + } + if (strncmp(msg.data(), LOC_MSG_HEAD, sizeof(LOC_MSG_HEAD) - 1)) { // short message msg.resize(nBytes); @@ -142,7 +139,6 @@ bool LocIpc::startListeningBlocking(const std::string& name) { if (mStopRequested) { mStopRequested = false; return true; - } else { LOC_LOGe("cannot read socket. reason:%s", strerror(errno)); (void)::close(mIpcFd); @@ -152,14 +148,28 @@ bool LocIpc::startListeningBlocking(const std::string& name) { } void LocIpc::stopListening() { + + const char *socketName = nullptr; mStopRequested = true; + if (mRunnable) { + std::string abort = LOC_MSG_ABORT; + socketName = (reinterpret_cast(mRunnable))->mIpcName.c_str(); + send(socketName, abort); + mRunnable = nullptr; + } + if (mIpcFd >= 0) { if (::close(mIpcFd)) { LOC_LOGe("cannot close socket:%s", strerror(errno)); } mIpcFd = -1; } + + //delete from the file system at the end + if (socketName) { + unlink(socketName); + } } bool LocIpc::send(const char name[], const std::string& data) { diff --git a/utils/LocIpc.h b/utils/LocIpc.h index 0c37a7ad..a1a994d1 100644 --- a/utils/LocIpc.h +++ b/utils/LocIpc.h @@ -32,7 +32,6 @@ #include #include -#include #include #include #include @@ -96,7 +95,7 @@ private: int mIpcFd; bool mStopRequested; LocThread mThread; - std::unique_ptr mRunnable; + LocRunnable *mRunnable; }; class LocIpcSender {