Add getting peer info method for inhouse packages 13/315013/5
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 24 Jul 2024 05:21:11 +0000 (14:21 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Mon, 29 Jul 2024 00:26:22 +0000 (09:26 +0900)
This patch adds a new method for inhouse developers.
 - <PREFIX>_<NAME>_context_get_peer_info()
 - ServiceBase::GetPid()
 - ServiceBase::GetUid()

Change-Id: I1ad077c2feece1004056fbd3549525b3aa6fe118
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
21 files changed:
idlc/gen/c_stub_body_gen.cc
idlc/gen/c_stub_body_gen.h
idlc/gen/c_stub_body_gen_cb.h
idlc/gen/c_stub_header_gen.cc
idlc/gen/c_stub_header_gen.h
idlc/gen/c_stub_header_gen_cb.h
idlc/gen/cpp_stub_body_gen.cc
idlc/gen/cpp_stub_body_gen_cb.h
idlc/gen/cpp_stub_header_gen.cc
idlc/gen/cpp_stub_header_gen_cb.h
idlc/gen/version2/c_stub_body_generator.cc
idlc/gen/version2/c_stub_body_generator.hh
idlc/gen/version2/c_stub_body_generator_cb.hh
idlc/gen/version2/c_stub_header_generator.cc
idlc/gen/version2/c_stub_header_generator.hh
idlc/gen/version2/c_stub_header_generator_cb.hh
idlc/gen/version2/cpp_stub_body_generator.cc
idlc/gen/version2/cpp_stub_body_generator_cb.hh
idlc/gen/version2/cpp_stub_header_generator.cc
idlc/gen/version2/cpp_stub_header_generator.hh
idlc/gen/version2/cpp_stub_header_generator_cb.hh

index 087ab08d708eb822442319913d49c879bd682a5a..de4d949d38e07acb6a45d07419dc2736744e79cf 100644 (file)
@@ -272,6 +272,7 @@ void CStubBodyGen::GenInterface(std::ofstream& stream, const Interface& iface) {
 
   GenInterfaceMethodTable(stream, iface);
   GenInterfaceContextBase(stream, iface);
+  if (options_->UseExtension()) GenInterfaceContextExtensionBase(stream, iface);
 
   if (has_delegate)
     GenInterfaceCallbackPortCheck(stream, iface);
@@ -746,4 +747,13 @@ void CStubBodyGen::GenInterfaceExtensionBase(std::ofstream& stream,
   stream << SmartIndent(code);
 }
 
+void CStubBodyGen::GenInterfaceContextExtensionBase(std::ofstream& stream,
+                                                    const Interface& iface) {
+  ReplaceAll(CB_INTERFACE_CONTEXT_EXTENSION_BASE)
+      .Change("<PREFIX>", GetHandlePrefix())
+      .Change("<NAME>", iface.GetID())
+      .Transform([&](std::string str) { return SmartIndent(str); })
+      .Out(stream);
+}
+
 }  // namespace tidl
index f65e8c93971692d88e796b6e93f5ae4ec198700b..989e7f00c192ffba34c385123172ba064498c1c1 100644 (file)
@@ -67,6 +67,8 @@ class CStubBodyGen : public CBodyGeneratorBase {
       const Interface& iface);
   void GenInterfaceBase(std::ofstream& stream, const Interface& iface);
   void GenInterfaceExtensionBase(std::ofstream& stream, const Interface& iface);
+  void GenInterfaceContextExtensionBase(std::ofstream& stream,
+                                        const Interface& iface);
 
   std::string GenMethodEnums(const Interface& iface);
   std::string GenDelegateEnums(const Interface& iface);
index fe54947c3bb4905485db34dd7fc058e2fba416da..1a324684a5936d13ae7c7683308ef8658330e2a8 100644 (file)
@@ -425,6 +425,18 @@ static void __<PREFIX>_<NAME>_remove_context(<PREFIX>_<NAME>_context_h context)
 }
 )__c_cb";
 
+/**
+ * <PREFIX> The prefix of the interface.
+ * <NAME> The name of the interface.
+ */
+constexpr const char CB_INTERFACE_CONTEXT_EXTENSION_BASE[] =
+R"__c_cb(
+int <PREFIX>_<NAME>_context_get_peer_info(<PREFIX>_<NAME>_context_h context, pid_t *pid, uid_t *uid)
+{
+  return rpc_port_get_peer_info(context->port, pid, uid);
+}
+)__c_cb";
+
 /**
  * <PREFIX> The prefix of the interface.
  * <NAME> The name of the interface.
index 45cf26caf5c7cf9a8b5173256298c9588e3858fd..5df798b83b49dc00dd8ef34e59da79b4d4665749 100644 (file)
@@ -149,6 +149,7 @@ void CStubHeaderGen::GenInterfaces(std::ofstream& stream) {
 void CStubHeaderGen::GenInterface(std::ofstream& stream,
     const Interface& iface) {
   GenInterfaceContextBase(stream, iface);
+  if (options_->UseExtension()) GenInterfaceContextExtensionBase(stream, iface);
   for (const auto& d : iface.GetDeclarations()) {
     if (d->GetMethodType() != Declaration::MethodType::DELEGATE)
       continue;
@@ -241,4 +242,13 @@ void CStubHeaderGen::GenInterfaceExtensionBase(std::ofstream& stream,
   stream << SmartIndent(code);
 }
 
+void CStubHeaderGen::GenInterfaceContextExtensionBase(std::ofstream& stream,
+                                                      const Interface& iface) {
+  ReplaceAll(CB_INTERFACE_CONTEXT_EXTENSION_BASE)
+      .Change("<PREFIX>", GetHandlePrefix())
+      .Change("<NAME>", iface.GetID())
+      .Transform([&](std::string str) { return SmartIndent(str); })
+      .Out(stream);
+}
+
 }  // namespace tidl
index cfb77f1f777a6a1914628bb8a669115b1bed901a..7a204373403ae7a9ee2c6a5293fb993f989e838c 100644 (file)
@@ -53,6 +53,8 @@ class CStubHeaderGen : public CHeaderGeneratorBase {
       const Declaration& decl);
   void GenInterfaceBase(std::ofstream& stream, const Interface& iface);
   void GenInterfaceExtensionBase(std::ofstream& stream, const Interface& iface);
+  void GenInterfaceContextExtensionBase(std::ofstream& stream,
+                                        const Interface& iface);
 
   std::string GenDelegateParams(const Interface& iface,
       const Declaration& decl);
index 99b9508ed60bcbb8b9c0aa83e38b5eb4ac1f4c17..af9a978dd689a3c54f808b9f06ba86f481aff1f6 100644 (file)
@@ -101,6 +101,23 @@ int <PREFIX>_<NAME>_context_get_instance(<PREFIX>_<NAME>_context_h context, char
 int <PREFIX>_<NAME>_context_disconnect(<PREFIX>_<NAME>_context_h context);
 )__c_cb";
 
+constexpr const char CB_INTERFACE_CONTEXT_EXTENSION_BASE[] =
+R"__c_cb(
+/**
+ * @brief Gets the peer info from the context handle.
+ *
+ * @param[in] context The context handle
+ * @param[out] pid The process ID
+ * @param[out] uid The user ID
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #RPC_PORT_ERROR_NONE Successful
+ * @retval #RPC_PORT_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #RPC_PORT_ERROR_IO_ERROR I/O error
+ */
+int <PREFIX>_<NAME>_context_get_peer_info(<PREFIX>_<NAME>_context_h context, pid_t *pid, uid_t *uid);
+)__c_cb";
+
 /**
  * <PREFIX> The prefix of the interface.
  * <NAME> The name of the interface.
index 0b171001b29e1035d7ddc02b51b6189e55e82c8a..4cb0a515fe674a33cda545853c1d5f3d36eac4a4 100644 (file)
@@ -95,17 +95,15 @@ void CppStubBodyGen::GenInterface(std::ofstream& stream,
 
 void CppStubBodyGen::GenServiceBase(std::ofstream& stream,
                                     const Interface& iface) {
-  GenTemplate(CB_CTOR_SERVICE_BASE, stream,
-    [&]()->std::string {
-      return iface.GetID();
-    });
+  ReplaceAll(CB_CTOR_SERVICE_BASE).Replace("$$", iface.GetID()).Out(stream);
   stream << NLine(1);
-  GenTemplate(CB_SERVICE_DEFAULT_METHOD, stream,
-    [&]()->std::string {
-      return iface.GetID();
-    }, [&]()->std::string {
-      return iface.GetID();
-    });
+
+  std::string extension_impl =
+      options_->UseExtension() ? "rpc_port_get_peerinfo(port, &pid_, &uid_);"
+                               : "// Set Port";
+  auto code =
+      ReplaceAll(CB_SERVICE_DEFAULT_METHOD).Replace("$$", iface.GetID());
+  ReplaceAll(code).Change("<EXTENSION_IMPL>", extension_impl).Out(stream);
   stream << NLine(1);
 }
 
index a544ff0a2c58275167414bead4dd9382f59c0f0e..2e7b82204f2a1bdf2e62a7ef941db5a91397ed3d 100644 (file)
@@ -256,6 +256,7 @@ R"__cpp_cb($$::ServiceBase::ServiceBase(std::string sender, std::string instance
 
 const char CB_SERVICE_DEFAULT_METHOD[] =
 R"__cpp_cb(void $$::ServiceBase::SetPort(rpc_port_h port) {
+  <EXTENSION_IMPL>
   port_ = port;
 }
 
index 72a19dca2bd71702243ba6125272b3abd11ddf97..d9974809976b9be116730e7ddd86777fd7af134c 100644 (file)
@@ -115,6 +115,8 @@ void CppStubHeaderGen::GenServiceBase(std::ofstream& stream,
     stream << ") = 0;" << NLine(1);
   }
 
+  if (options_->UseExtension()) stream << CB_SERVICE_EXTENSION_BASE;
+
   stream << CB_SERVICE_BASE_BACK << NLine(2);
 }
 
index a6182466e33844dc08ef14c452e3cd7fc48c99a2..5a7c4d7b5636fb7f70fa07bbcde420a305296b1e 100644 (file)
@@ -317,4 +317,21 @@ R"__cpp_cb(
   bool HasPendingRequest() const;
 )__cpp_cb";
 
+const char CB_SERVICE_EXTENSION_BASE[] =
+R"__cpp_cb(
+    /// <summary>
+    /// Gets the process ID of the client.
+    /// </summary>
+    pid_t GetPid() const { return pid_; }
+
+    /// <summary>
+    /// Gets the user ID of the client.
+    /// </summary>
+    uid_t GetUid() const { return uid_; }
+
+   private:
+    pid_t pid_ = -1;
+    uid_t uid_ = 0;
+)__cpp_cb";
+
 #endif  // IDLC_CPP_GEN_CPP_STUB_HEADER_GEN_CB_H_
index f968c4c94f62631d2c786703eb6dbcbb5357572f..75b493aa3b1ce4c562f020c14ec1c4dcf2c83ea4 100644 (file)
@@ -334,6 +334,7 @@ void CStubBodyGenerator::GenInterface(std::ofstream& stream,
   GenInterfaceMethodPrivilegeCheckerTable(stream, iface);
   GenInterfaceMethodTable(stream, iface);
   GenInterfaceContextBase(stream, iface);
+  GenInterfaceContextExtensionBase(stream, iface);
 
   if (has_delegate)
     GenInterfaceCallbackPortCheck(stream, iface);
@@ -814,5 +815,17 @@ void CStubBodyGenerator::GenInterfaceExtensionBase(std::ofstream& stream,
   }
 }
 
+// @see #CB_INTERFACE_CONTEXT_EXTENSION_BASE
+void CStubBodyGenerator::GenInterfaceContextExtensionBase(
+    std::ofstream& stream, const Interface& iface) {
+  if (options_->UseExtension()) {
+    ReplaceAll(CB_INTERFACE_CONTEXT_EXTENSION_BASE)
+        .Change("<PREFIX>", GetHandlePrefix())
+        .Change("<NAME>", iface.GetID())
+        .Transform([&](std::string code) { return SmartIndent(code); })
+        .Out(stream);
+  }
+}
+
 }  // namespace version2
 }  // namespace tidl
index e340ba3d3bc5c5dc3bf850a08ca20072c82d1779..afd87c640a7dc979ad0d200a31bb033eddcb1950 100644 (file)
@@ -74,6 +74,8 @@ class CStubBodyGenerator : public CBodyGeneratorBase {
                                      const Interface& iface);
   void GenInterfaceBase(std::ofstream& stream, const Interface& iface);
   void GenInterfaceExtensionBase(std::ofstream& stream, const Interface& iface);
+  void GenInterfaceContextExtensionBase(std::ofstream& stream,
+                                        const Interface& iface);
 
   std::string GenMethodEnums(const Interface& iface);
   std::string GenDelegateEnums(const Interface& iface);
index 9791525674e1fd84353c15b28b576404bd989c17..6e98740ff567cbc49d444a8fd6488672bb8ad7b8 100644 (file)
@@ -2377,6 +2377,18 @@ EXPORT_API bool <PREFIX>_<NAME>_has_pending_request(void)
 }
 )__c_cb";
 
+/**
+ * <PREFIX> The prefix of the interface.
+ * <NAME> The name of the interface.
+ */
+constexpr const char CB_INTERFACE_CONTEXT_EXTENSION_BASE[] =
+R"__c_cb(
+EXPORT_API int <PREFIX>_<NAME>_context_get_peer_info(<PREFIX>_<NAME>_context_h context, pid_t *pid, uid_t *uid)
+{
+  return rpc_port_get_peer_info(context->port, pid, uid);
+}
+)__c_cb";
+
 }  // namespace version2
 }  // namespace tidl
 
index 62c9889a4c179d3bf7b36f3bf8f607eb2483a335..0bd88b3cabd8a7d8d9328bcd580631b4acfc7b8d 100644 (file)
@@ -239,5 +239,17 @@ void CStubHeaderGenerator::GenInterfaceExtensionBase(std::ofstream& stream,
   }
 }
 
+// @see #CB_INTERFACE_CONTEXT_EXTENSION_BASE
+void CStubHeaderGenerator::GenInterfaceContextExtensionBase(
+    std::ofstream& stream, const Interface& iface) {
+  if (options_->UseExtension()) {
+    ReplaceAll(CB_INTERFACE_CONTEXT_EXTENSION_BASE)
+        .Change("<PREFIX>", GetHandlePrefix())
+        .Change("<NAME>", iface.GetID())
+        .Transform([&](std::string code) { return SmartIndent(code); })
+        .Out(stream);
+  }
+}
+
 }  // namespace version2
 }  // namespace tidl
index 209a0e56909cddaaade17d9a99f99dabd5dec42c..24153a9b66f8f38ed765dc9b1f1501f147996cc5 100644 (file)
@@ -55,6 +55,8 @@ class CStubHeaderGenerator : public CHeaderGeneratorBase {
                                 const Declaration& decl);
   void GenInterfaceBase(std::ofstream& stream, const Interface& iface);
   void GenInterfaceExtensionBase(std::ofstream& stream, const Interface& iface);
+  void GenInterfaceContextExtensionBase(std::ofstream& stream,
+                                        const Interface& iface);
 
   std::string GenDelegateParams(const Interface& iface,
                                 const Declaration& decl);
index 003fbac9aff2f4e85c08361aa455063e04b6bbd2..22f1e854f610ab12dd3e955a865fde4090f72fc4 100644 (file)
@@ -374,9 +374,36 @@ R"__c_cb(
  */
 constexpr const char CB_INTERFACE_EXTENSION_BASE[] =
 R"__c_cb(
+/**
+ * @brief Checks whether the pending request exists or not.
+ *
+ * @return @c true if the pending request exists,
+ *         otherwise @c false
+ */
 bool <PREFIX>_<NAME>_has_pending_request(void);
 )__c_cb";
 
+/**
+ * <PREFIX> The prefix of the interface.
+ * <NAME> The name of the interface.
+ */
+constexpr const char CB_INTERFACE_CONTEXT_EXTENSION_BASE[] =
+R"__c_cb(
+/**
+ * @brief Gets the peer information from the context handle.
+ *
+ * @param[in] context The context handle
+ * @param[out] pid The process ID of the client
+ * @param[out] uid The user ID of the client
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ * @retval #RPC_PORT_ERROR_NONE Successful
+ * @retval #RPC_PORT_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #RPC_PORT_ERROR_IO_ERROR I/O error
+ */
+int <PREFIX>_<NAME>_context_get_peer_info(<PREFIX>_<NAME>_context_h context, pid_t *pid, uid_t *uid);
+)__c_cb";
+
 }  // namespace version2
 }  // namespace tidl
 
index c5ace9aee1c60efaf7b44639d539da6c0142e90f..3c087f84c3fa95dbb1e365bfc5024a84e528e6b8 100644 (file)
@@ -152,29 +152,36 @@ std::string CppStubBodyGenerator::GenInterface(const Interface& iface) {
           .Change("<CLS_NAME>", iface.GetID())
           .Change("<CALLBACKS>", GenInterfaceCallbacks(iface))
           .Change("<IMPL_SERVICE_BASE_THREAD_MEMBER_INIT>",
-              GenInterfaceImplServiceBaseThreadMemberInit())
+                  GenInterfaceImplServiceBaseThreadMemberInit())
           .Change("<IMPL_SERVICE_BASE_DISPATCH_FUNC_INIT>",
-              GenInterfaceImplServiceBaseDispatchFuncInit(iface))
+                  GenInterfaceImplServiceBaseDispatchFuncInit(iface))
           .Change("<IMPL_SERVICE_BASE_DISPATCH>",
-              GenInterfaceImplServiceBaseDispatch())
+                  GenInterfaceImplServiceBaseDispatch())
           .Change("<IMPL_SERVICE_BASE_DISPATCH_FUNCS>",
-              GenInterfaceImplServiceBaseDispatchFuncs(iface))
+                  GenInterfaceImplServiceBaseDispatchFuncs(iface))
           .Change("<IMPL_SERVICE_BASE_SET_PRIVILEGE_MAP>",
-              GenInterfaceImplServiceBaseSetPrivilegeMap(iface))
+                  GenInterfaceImplServiceBaseSetPrivilegeMap(iface))
           .Repeat("IMPL_ADD_PRIVILEGE", iface.GetAttributes(),
-              [&](ReplaceAll* ra, const std::unique_ptr<tidl::Attribute>& attr) {
-                if (attr->GetKey() != "privilege")
-                  return false;
-
-                ra->Replace("PRIVILEGE", attr->GetValue());
-                return true;
-              })
-          .Remove("IMPL_SET_TRUSTED", std::find_if(
-                iface.GetAttributes().begin(),
-                iface.GetAttributes().end(),
-                [](const auto& attr) {
-                  return attr->GetKey() == "trusted" && attr->GetValue() == "true";
-                }) == iface.GetAttributes().end()));
+                  [&](ReplaceAll* ra,
+                      const std::unique_ptr<tidl::Attribute>& attr) {
+                    if (attr->GetKey() != "privilege") return false;
+
+                    ra->Replace("PRIVILEGE", attr->GetValue());
+                    return true;
+                  })
+          .Remove("IMPL_SET_TRUSTED",
+                  std::find_if(iface.GetAttributes().begin(),
+                               iface.GetAttributes().end(),
+                               [](const auto& attr) {
+                                 return attr->GetKey() == "trusted" &&
+                                        attr->GetValue() == "true";
+                               }) == iface.GetAttributes().end())
+          .Change("<IMPL_SERVICE_BASE_EXTENSION>", [&]() {
+            if (options_->UseExtension())
+              return "rpc_port_get_peer_info(port_, &pid_, &uid_);";
+
+            return "";
+          }));
 }
 
 std::string CppStubBodyGenerator::GenInterfaceCallbacks(
index 2c7a51184444085237f6b73b1f8717a170ac25dd..88eea6ee23253779ca7e613b364364ac39f990ff 100644 (file)
@@ -154,6 +154,7 @@ void <IFACE_NAME>::PendingJob::OnRun() {
  * <IMPL_SERVICE_BASE_DISPATCH> The implementation of the dispatch method of the service base.
  * <IMPL_SERVICE_BASE_DISPATCH_FUNCS> The implementation of the dispatch functions of the service base.
  * <IMPL_SERVICE_BASE_SET_PRIVILEGE_MAP> The implementation of setting privilege map of the service base.
+ * <IMPL_SERVICE_BASE_EXTENSION> The implementation of the extension codes of the service base.
  */
 constexpr const char CB_INTERFACE_BASE[] =
 R"__cpp_cb(
@@ -169,6 +170,7 @@ R"__cpp_cb(
 }
 
 void <CLS_NAME>::ServiceBase::SetPort(rpc_port_h port) {
+  <IMPL_SERVICE_BASE_EXTENSION>
   port_ = port;
 }
 
index 20d7773b78b9de3fd5015e6fb69877388516d7d8..1c0620b462a092c2725d8f9d2aa3f5138be0c923 100644 (file)
@@ -89,7 +89,9 @@ std::string CppStubHeaderGenerator::GenInterface(const Interface& iface) {
                   GenInterfaceServiceBaseImplThreadMember())
           .Change("<EXTENSION_BASE>", GenInterfaceExtensionBase())
           .Change("<METHOD_IDS>", GenMethodIds(iface))
-          .Change("<DELEGATE_IDS>", GenDelegateIds(iface)));
+          .Change("<DELEGATE_IDS>", GenDelegateIds(iface))
+          .Change("<SERVICE_BASE_EXTENSION>",
+                  GenInterfaceServiceBaseExtension()));
 }
 
 std::string CppStubHeaderGenerator::GenInterfaceCallbacks(
@@ -157,5 +159,12 @@ std::string CppStubHeaderGenerator::GenInterfaceExtensionBase() {
   return "";
 }
 
+std::string CppStubHeaderGenerator::GenInterfaceServiceBaseExtension() {
+  if (options_->UseExtension())
+    return std::string(CB_INTERFACE_SERVICE_BASE_EXTENSION);
+
+  return "";
+}
+
 }  // namespace version2
 }  // namespace tidl
index 7c4277d91e4e8e17630195b7afd4e44169000b51..d12c3c7dd7f59f33581693eb0de319485123a73b 100644 (file)
@@ -46,6 +46,7 @@ class CppStubHeaderGenerator : public CppGeneratorBase {
   std::string GenInterfaceServiceBaseDispatchFuncs(const Interface& iface);
   std::string GenInterfaceServiceBaseImplThreadMember();
   std::string GenInterfaceExtensionBase();
+  std::string GenInterfaceServiceBaseExtension();
 
  private:
   std::shared_ptr<Options> options_;
index 4eea2d66b7072f8da2668ac536b65371646a3e54..5031d562e0506ffa7aa8867bbb00c477f3f850a4 100644 (file)
@@ -241,6 +241,7 @@ class PendingJob : public Job, Job::IEvent {
  * <SERVICE_BASE_IMPL_THREAD_MEMBER> The member variable of the thread of service base.
  * <METHOD_IDS> The enumeration of methods of the interface.
  * <DELEGATE_IDS> The enumeration of delegates(callbacks) of the interface.
+ * <SERVICE_BASE_EXTENSION> The extension methods of the service base of the interface.
  */
 constexpr const char CB_INTERFACE_BASE[] =
 R"__cpp_cb(
@@ -308,6 +309,7 @@ class <CLS_NAME> : public LocalExecution::IEvent {
     void Dispatch(rpc_port_h port, rpc_port_h callback_port, rpc_port_parcel_h parcel);
 
     <SERVICE_BASE_METHODS>
+    <SERVICE_BASE_EXTENSION>
 
    protected:
     ServiceBase(std::string sender, std::string instance);
@@ -395,6 +397,23 @@ R"__cpp_cb(
 std::unique_ptr<ActiveObject> active_object_;
 )__cpp_cb";
 
+constexpr const char CB_INTERFACE_SERVICE_BASE_EXTENSION[] =
+R"__cpp_cb(
+ /// <summary>
+ /// Gets the process ID of the client.
+ /// </summary>
+ pid_t GetPid() const { return pid_; }
+
+ /// <summary>
+ /// Gets the user ID of the client.
+ /// </summary>
+ uid_t GetUid() const { return uid_; }
+
+private:
+ pid_t pid_ = -1;
+ uid_t uid_ = 0;
+)__cpp_cb";
+
 constexpr const char CB_LEM_BASE[] =
 R"__cpp_cb(
 class LocalExecution {