Increase timeout of client socket
[platform/core/appfw/pkgmgr-info.git] / src / common / socket / client_socket.cc
index 5c1f218..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 "utils/logging.hh"
 
 #include "pkgmgrinfo_debug.h"
 
+namespace {
+
+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;
+
+  return true;
+}
+
+}
+
 namespace pkgmgr_common {
 namespace socket {
 
@@ -45,32 +59,30 @@ void ClientSocket::SetTimeout(int timeout_msec) {
       .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)
+  if (setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0)
     LOG(ERROR) << "setsockopt() is failed. fd: " << fd_
         << ", errno: " << errno;
 }
 
-bool ClientSocket::Connect() {
+bool ClientSocket::Connect(ReqType req_type) {
   if (Create() < 0)
     return false;
 
-  SetTimeout(5000);
+  SetTimeout(IsDBWriteRequest(req_type) ? 60 * 1000 : 30 * 1000);
 
-  int ret = -1;
   int retry_cnt = 3;
   do {
-    ret = TryConnection();
+    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 (getuid() == 0) {
-        // If requester is root, don't wait
+
+      // If requester is root, don't wait
+      if (getuid() == 0)
         return false;
-      }
+
       usleep(100 * 1000);
       --retry_cnt;
     } else if (ret < 0) {