Use socket activation with TIDL 67/308967/1
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 3 Apr 2024 06:48:47 +0000 (15:48 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 3 Apr 2024 06:48:47 +0000 (15:48 +0900)
TIDL supports socket activation. The process name should have "d::" prefix.

Requires:
 - https://review.tizen.org/gerrit/#/c/platform/core/appfw/pkgmgr-server/+/308961/

Change-Id: I6db0153364592e6e796a2d1a32982efd0bcb7e57
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
client/src/connector.cc
client/src/connector.hh

index 381fe45..0a2865a 100644 (file)
@@ -27,8 +27,7 @@
 
 namespace {
 
-constexpr const char SERVER_PROC_NAME[] = "org.tizen.appfw.pkgmgr";
-constexpr const char PACKAGE_MANAGER_SOCKET_PATH[] = "/run/package-manager";
+constexpr const char SERVER_PROC_NAME[] = "d::org.tizen.appfw.pkgmgr";
 
 static int _is_system_user(void)
 {
@@ -124,9 +123,6 @@ bool Connector::ConnectForAdmin() {
   if (conn_admin_listener_.GetState() == ConnectionState::Connected)
     return true;
 
-  if (!activator_.Connect())
-    return false;
-
   try {
     admin_proxy_->Connect(true);
   } catch (const pkg_proxy::Exception& e) {
@@ -145,9 +141,6 @@ bool Connector::ConnectForInfo() {
   if (conn_info_listener_.GetState() == ConnectionState::Connected)
     return true;
 
-  if (!activator_.Connect())
-    return false;
-
   try {
     info_proxy_->Connect(true);
   } catch (const pkg_proxy::Exception& e) {
@@ -166,9 +159,6 @@ bool Connector::ConnectForCache() {
   if (conn_cache_listener_.GetState() == ConnectionState::Connected)
     return true;
 
-  if (!activator_.Connect())
-    return false;
-
   try {
     cache_proxy_->Connect(true);
   } catch (const pkg_proxy::Exception& e) {
@@ -187,9 +177,6 @@ bool Connector::ConnectForDelayedResult() {
   if (conn_delayed_result_listener_.GetState() == ConnectionState::Connected)
     return true;
 
-  if (!activator_.Connect())
-    return false;
-
   try {
     delayed_result_proxy_->Connect(true);
   } catch (const pkg_proxy::Exception& e) {
@@ -234,128 +221,5 @@ pkgmgr_client_t* Connector::GetRawPc() {
   return raw_pc_;
 }
 
-bool Connector::Activator::Connect() {
-  if (!Create())
-    return false;
-
-  SetTimeout(60 * 1000);
-
-  int retry_cnt = 3;
-  do {
-    int ret = TryConnect();
-    if (ret == 0) {
-      break;
-    } else if (ret < -1) {
-      _E("Maybe peer not launched or peer dead. path: %s, fd: %d",
-          PACKAGE_MANAGER_SOCKET_PATH, fd_);
-      // If requester is root, don't wait
-      if (getuid() == 0)
-        return false;
-
-      usleep(100 * 1000);
-      --retry_cnt;
-    } else if (ret < 0) {
-      _E("Failed to connect to socket: %s, fd: %d",
-          PACKAGE_MANAGER_SOCKET_PATH, fd_);
-      return false;
-    }
-  } while (retry_cnt > 0);
-
-  if (retry_cnt == 0) {
-    _E("Failed to connect with server");
-    return false;
-  }
-
-  return ReceiveReady();
-}
-
-bool Connector::Activator::Create() {
-  if (fd_ != -1)
-    return true;
-
-  fd_ = ::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
-  if (fd_ < 0) {
-    _E("socket() is failed. errno: %d", errno);
-    return false;
-  }
-
-  addr_.sun_family = AF_UNIX;
-  snprintf(addr_.sun_path, sizeof(addr_.sun_path), "%s",
-      PACKAGE_MANAGER_SOCKET_PATH);
-  return true;
-}
-
-void Connector::Activator::SetTimeout(int timeout_msec) {
-  if (timeout_msec < 0) {
-    _E("Invalid timeout parameter");
-    return;
-  }
-
-  timeval timeout = {
-    .tv_sec = static_cast<time_t>(timeout_msec / 1000),
-    .tv_usec = static_cast<time_t>(timeout_msec % 1000) * 1000
-  };
-  if (setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0)
-    _E("setsockopt() is failed, fd: %d, errno : %d", fd_, errno);
-}
-
-int Connector::Activator::TryConnect() {
-  int ret = connect(fd_, reinterpret_cast<sockaddr*>(&addr_), sizeof(addr_));
-  _E("ret = %d", ret);
-  if (ret < 0) {
-    if (errno != EAGAIN && errno != EINPROGRESS)
-      return -2;
-  }
-
-  return 0;
-}
-
-
-bool Connector::Activator::ReceiveReady() {
-  bool is_blocking;
-  if (fcntl(fd_, F_GETFL, 0) & O_NONBLOCK)
-    is_blocking = false;
-  else
-    is_blocking = true;
-
-  int retry_count = 20;
-  size_t len = 1;
-  char buffer[2] = {0,};
-  while (len) {
-    ssize_t bytes = recv(fd_, buffer, len, 0);
-    if (bytes == 0) {
-      _W("EOF. fd(%d)", fd_);
-      return -ECOMM;
-    }
-
-    if (bytes < 0) {
-      if (errno == EINTR || errno == EAGAIN) {
-        if (is_blocking && errno == EAGAIN) {
-          _E("Timed out. fd(%d)", fd_);
-          return -EAGAIN;
-        }
-
-        if (retry_count > 0) {
-          usleep(100 * 1000);
-          retry_count--;
-          continue;
-        }
-      }
-
-      _E("recv() is failed. fd(%d), errno(%d)", fd_, errno);
-      return -ECOMM;
-    }
-
-    len -= bytes;
-  }
-
-  _E("Received: %s\n", buffer);
-
-  close(fd_);
-  fd_ = -1;
-
-  return true;
-}
-
 }  // namespace client
 }  // namespace pkgmgr
index 29236b3..26c1ea7 100644 (file)
@@ -68,21 +68,6 @@ class Connector {
   bool ConnectForDelayedResult();
 
  private:
-  class Activator {
-   public:
-    Activator() : fd_(-1), addr_{} {}
-
-    bool Connect();
-    bool Create();
-    void SetTimeout(int timeout_msec);
-    int TryConnect();
-    bool ReceiveReady();
-
-   private:
-    int fd_;
-    sockaddr_un addr_;
-  };
-
   pkgmgr_client_type pc_type_;
   std::unique_ptr<pkg_proxy::PkgMgr> info_proxy_;
   std::unique_ptr<pkg_proxy::PkgMgrForClearCache> cache_proxy_;
@@ -100,10 +85,9 @@ class Connector {
   std::string tep_path_;
   pkgmgr_client_t *raw_pc_;
   bool tep_move_ = false;
-  Activator activator_;
 };
 
 }  // namespace client
 }  // namespace pkgmgr
 
-#endif  // CLIENT_SRC_CONNECTOR_H_
\ No newline at end of file
+#endif  // CLIENT_SRC_CONNECTOR_H_