Increase timeout of client socket
[platform/core/appfw/pkgmgr-info.git] / src / common / socket / client_socket.cc
index a7d0019..e60015b 100644 (file)
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include "client_socket.hh"
+
 #include <errno.h>
 #include <fcntl.h>
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <unistd.h>
 
-#include "client_socket.hh"
-#include "dlog.h"
+#include "utils/logging.hh"
 
-namespace pkgmgr_common {
-namespace socket {
+#include "pkgmgrinfo_debug.h"
 
-ClientSocket::ClientSocket(std::string path) : AbstractSocket(std::move(path)) {
-  int retry_cnt = 2;
-  int ret = Create();
-  if (ret < 0)
-    return;
-
-  do {
-    ret = Connect();
-    if (ret == 0) {
-      break;
-    } else if (ret < -1) {
-      LOGE("Maybe peer not launched or peer dead. path(%s), fd(%d)",
-          GetPath().c_str(), GetFd());
-      usleep(100 * 1000);
-      --retry_cnt;
-    } else if (ret < 0) {
-      LOGE("Failed to connect to socket(%s), fd(%d)",
-          GetPath().c_str(), GetFd());
-      return;
-    }
-  } while (retry_cnt > 0);
+namespace {
 
-  if (retry_cnt == 0)
-    return;
+bool IsDBWriteRequest(pkgmgr_common::ReqType req_type) {
+  if (req_type != pkgmgr_common::ReqType::SET_PKG_INFO &&
+      req_type != pkgmgr_common::ReqType::SET_CERT_INFO &&
+      req_type != pkgmgr_common::ReqType::WRITE_QUERY)
+    return false;
 
-  SetTimeout(5);
+  return true;
 }
 
-ClientSocket::~ClientSocket() {
-  Destroy();
 }
 
+namespace pkgmgr_common {
+namespace socket {
+
+ClientSocket::ClientSocket(std::string path)
+    : AbstractSocket(std::move(path)) {}
+
 void ClientSocket::SetTimeout(int timeout_msec) {
   if (timeout_msec == -1)
     timeout_msec = 5000;
 
   if (timeout_msec < 0) {
-    LOGE("Invalid timeout_msec parameter");
+    LOG(ERROR) << "Invalid timeout_msec parameter";
     return;
   }
 
   struct timeval timeout = {
-    .tv_sec = static_cast<time_t>(timeout_msec / 1000),
-    .tv_usec = static_cast<suseconds_t>((timeout_msec % 1000) * 1000)
-  };
-  int ret = setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
-  if (ret < 0)
-    LOGE("setsockopt() is failed. fd(%d), errno(%d)", fd_, errno);
+      .tv_sec = static_cast<time_t>(timeout_msec / 1000),
+      .tv_usec = static_cast<suseconds_t>((timeout_msec % 1000) * 1000)};
+
+  if (setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0)
+    LOG(ERROR) << "setsockopt() is failed. fd: " << fd_
+        << ", errno: " << errno;
 }
 
-int ClientSocket::Connect() {
+bool ClientSocket::Connect(ReqType req_type) {
+  if (Create() < 0)
+    return false;
+
+  SetTimeout(IsDBWriteRequest(req_type) ? 60 * 1000 : 30 * 1000);
+
+  int retry_cnt = 3;
+  do {
+    int ret = TryConnection();
+    if (ret == 0) {
+      break;
+    } else if (ret < -1) {
+      LOG(ERROR) << "Maybe peer not launched or peer dead. path: " << GetPath()
+          << ", fd: " << GetFd();
+
+      // If requester is root, don't wait
+      if (getuid() == 0)
+        return false;
+
+      usleep(100 * 1000);
+      --retry_cnt;
+    } else if (ret < 0) {
+      LOG(ERROR) << "Failed to connect to socket: " << GetPath()
+          << ", fd: " << GetFd();
+      return false;
+    }
+  } while (retry_cnt > 0);
+
+  return (retry_cnt > 0);
+}
+
+int ClientSocket::TryConnection() {
   int flags = fcntl(fd_, F_GETFL, 0);
-  fcntl(fd_, F_SETFL, flags | O_NONBLOCK);
 
-  int ret = connect(fd_, reinterpret_cast<struct sockaddr*>(&addr_),
-      sizeof(addr_));
-  fcntl(fd_, F_SETFL, flags);
+  if (fcntl(fd_, F_SETFL, flags | O_NONBLOCK) != 0) {
+    LOG(ERROR) << "Failed to set flags: " << (flags | O_NONBLOCK) << " on fd: "
+        << fd_ << ", errno: " << errno;
+    return -1;
+  }
+
+  int ret =
+      connect(fd_, reinterpret_cast<struct sockaddr*>(&addr_), sizeof(addr_));
+  if (fcntl(fd_, F_SETFL, flags) != 0) {
+    LOG(ERROR) << "Failed to set flags: " << flags << " on fd: " << fd_
+        << ", errno: " <<  errno;
+    return -1;
+  }
   if (ret < 0) {
     if (errno != EAGAIN && errno != EINPROGRESS)
       return -2;
@@ -96,7 +123,7 @@ int ClientSocket::Connect() {
   FD_ZERO(&readfds);
   FD_SET(fd_, &readfds);
   fd_set writefds = readfds;
-  struct timeval timeout = { 0, 100 * 1000 };
+  struct timeval timeout = {0, 100 * 1000};
   ret = select(fd_ + 1, &readfds, &writefds, NULL, &timeout);
   if (ret == 0) {
     errno = ETIMEDOUT;