Fix a bug in rpc-port-parcel 85/318185/2
authorpjh9216 <jh9216.park@samsung.com>
Tue, 14 Jan 2025 04:48:04 +0000 (13:48 +0900)
committerpjh9216 <jh9216.park@samsung.com>
Tue, 14 Jan 2025 07:01:22 +0000 (16:01 +0900)
- Consider making sub-parcel using fd-send feature
- Add internal API 'rpc_port_parcel_clone' for local excution mode

Change-Id: I2876a1c1ef4dec9e6223c32eec1a08ba48096ba7
Signed-off-by: pjh9216 <jh9216.park@samsung.com>
include/rpc-port-parcel-internal.h
src/rpc-port/parcel-internal.cc
src/rpc-port/parcel-internal.hh
src/rpc-port/rpc-port-parcel.cc

index 45441e69feb247c533a96560842d0e510335aebc..e0668e5ef275742f5e1e9339c744f33abfc2fa03 100644 (file)
@@ -49,6 +49,7 @@ int rpc_port_parcel_get_reader(rpc_port_parcel_h h, unsigned int* reader_pos);
 int rpc_port_parcel_set_reader(rpc_port_parcel_h h, unsigned int reader_pos);
 int rpc_port_parcel_read_fd(rpc_port_parcel_h h, int* fd);
 int rpc_port_parcel_write_fd(rpc_port_parcel_h h, int fd);
+int rpc_port_parcel_clone(rpc_port_parcel_h* h, rpc_port_parcel_h origin);
 
 #ifdef __cplusplus
 }
index 7f8d7b1ecb1b9a3359b168bb467ece79d964634b..af21b323a2d09e73ee9b7db702f31d84d8d5a14f 100644 (file)
@@ -31,14 +31,32 @@ namespace internal {
 
 constexpr const int MAX_NR_OF_DESCRIPTORS = 32;
 
+Parcel::Parcel(const Parcel& p) {
+  if (p.header_) header_.reset(new ParcelHeader(*p.header_));
+  parcelable_fds_ = p.parcelable_fds_;
+  handle_.reset(new tizen_base::Parcel(*p.handle_));
+}
+
+Parcel& Parcel::operator=(const Parcel& p) {
+  if (this != &p) {
+    if (p.header_) header_.reset(new ParcelHeader(*p.header_));
+    parcelable_fds_ = p.parcelable_fds_;
+    handle_.reset(new tizen_base::Parcel(*p.handle_));
+  }
+
+  return *this;
+}
+
 Parcel::Parcel(bool without_header)
     : header_(without_header ? nullptr : new ParcelHeader()) {
   handle_.reset(new tizen_base::Parcel());
 }
 
-Parcel::Parcel(bool without_header, tizen_base::Parcel* parcel)
+Parcel::Parcel(bool without_header, tizen_base::Parcel* parcel,
+               const Parcel& origin)
     : header_(without_header ? nullptr : new ParcelHeader()) {
   handle_.reset(parcel);
+  parcelable_fds_ = origin.parcelable_fds_;
 }
 
 Parcel::~Parcel() {}
@@ -88,6 +106,12 @@ void Parcel::SetRawParcel(tizen_base::Parcel* raw_parcel) {
 
 tizen_base::Parcel* Parcel::GetRawParcel() const { return raw_parcel_.get(); }
 
+void Parcel::Dup() {
+  for (auto& i : parcelable_fds_) {
+    i = dup(i);
+  }
+}
+
 int Parcel::GetParcelableFd(uint32_t idx) const {
   if (idx >= parcelable_fds_.size()) {
     return -1;
index b6a381cc205570670a682480372381ce329132a4..7e98477fda82beed3667a8629c82a23bbd89fc40 100644 (file)
 
 #include <parcel.h>
 
+#include <memory>
 #include <parcel.hh>
 #include <parcelable.hh>
 
-#include <memory>
-
 #include "parcel-header-internal.hh"
 
 namespace rpc_port {
@@ -31,9 +30,12 @@ namespace internal {
 
 class Parcel : public tizen_base::Parcelable {
  public:
-   Parcel(bool without_header = false);
-   Parcel(bool without_header, tizen_base::Parcel* parcel);
-   ~Parcel();
+  Parcel(bool without_header = false);
+  Parcel(bool without_header, tizen_base::Parcel* parcel, const Parcel& origin);
+  Parcel(const Parcel& p);
+  ~Parcel();
+
+  Parcel& operator=(const Parcel& p);
 
   void WriteToParcel(tizen_base::Parcel* parcel) const override;
   void ReadFromParcel(tizen_base::Parcel* parcel) override;
@@ -47,6 +49,7 @@ class Parcel : public tizen_base::Parcelable {
   void PushBackParcelableFd(int fd);
   void WriteParcelableFds(int socket_fd);
   void ReadParcelableFds(int socket_fd, int cnt);
+  void Dup();
 
  private:
   bool DoWriteParcelableFds(int socket_fd, int start_idx, int cnt);
@@ -54,7 +57,7 @@ class Parcel : public tizen_base::Parcelable {
 
   std::unique_ptr<ParcelHeader> header_;
   std::unique_ptr<tizen_base::Parcel> handle_;
-  std::unique_ptr<tizen_base::Parcel> raw_parcel_ { nullptr };
+  std::unique_ptr<tizen_base::Parcel> raw_parcel_{nullptr};
   std::vector<int> parcelable_fds_;
 };
 
index 5bdc7e0977ab263d07fab40ddf9d4a3e9dab8bde..eb5772c4c3baecd0afe6fddcfa516fa89b212901 100644 (file)
@@ -564,6 +564,20 @@ RPC_API int rpc_port_parcel_get_raw(rpc_port_parcel_h h, void** raw,
   return RPC_PORT_ERROR_NONE;
 }
 
+RPC_API int rpc_port_parcel_clone(rpc_port_parcel_h* h,
+                                  rpc_port_parcel_h origin) {
+  if (h == nullptr || origin == nullptr)
+    return RPC_PORT_ERROR_INVALID_PARAMETER;
+
+  auto* parcel = static_cast<internal::Parcel*>(origin);
+  auto* new_parcel = new internal::Parcel(*parcel);
+
+  new_parcel->Dup();
+  *h = static_cast<rpc_port_parcel_h>(new_parcel);
+
+  return RPC_PORT_ERROR_NONE;
+}
+
 RPC_API int rpc_port_parcel_create_from_raw(rpc_port_parcel_h* h,
                                             const void* raw,
                                             unsigned int size) {
@@ -636,7 +650,7 @@ RPC_API int rpc_port_parcel_create_from_parcel(rpc_port_parcel_h* h,
 
   try {
     auto* new_parcel = new internal::Parcel(
-        true, new tizen_base::Parcel(*ih, start_pos, size));
+        true, new tizen_base::Parcel(*ih, start_pos, size), *parcel);
     *h = static_cast<rpc_port_parcel_h>(new_parcel);
   } catch (...) {
     return RPC_PORT_ERROR_OUT_OF_MEMORY;