Add a new internal API for gettting peer info 47/296047/2
authorjusung son <jusung07.son@samsung.com>
Wed, 19 Jul 2023 00:39:32 +0000 (09:39 +0900)
committerjusung son <jusung07.son@samsung.com>
Wed, 19 Jul 2023 00:47:44 +0000 (09:47 +0900)
 - rpc_port_get_peer_info()

Change-Id: I8516e79389fe89d391ea1b30999ba253e811d22e
Signed-off-by: jusung son <jusung07.son@samsung.com>
include/rpc-port-internal.h
src/rpc-port-internal.cc

index 999f73f..636306a 100644 (file)
@@ -64,6 +64,17 @@ int rpc_port_register_proc_info(const char *proc_name, bundle *extra);
  */
 int rpc_port_deregister_proc_info(void);
 
+/**
+ * @brief Gets the pid and uid of the connected rpc port.
+ * @since_tizen 8.0
+ * @param[in] h The rpc port handle
+ * @param[out] pid Process ID
+ * @param[out] uid User ID
+ * @return @c 0 on success,
+ *         otherwise a negative error value
+ */
+int rpc_port_get_peer_info(rpc_port_h h, pid_t *pid, uid_t *uid);
+
 #ifdef __cplusplus
 }
 #endif
index 36682dd..1a8c0b1 100644 (file)
 
 #include "include/rpc-port-internal.h"
 #include "include/rpc-port.h"
+#include "peer-cred-internal.hh"
+#include "port-internal.hh"
 
 #undef RPC_API
 #define RPC_API extern "C" __attribute__((visibility("default")))
 
 namespace {
+using namespace rpc_port::internal;
 
 constexpr uid_t kRegularUidMin = 5000;
 std::atomic<uid_t> __target_uid { getuid() };
@@ -63,3 +66,22 @@ RPC_API int rpc_port_deregister_proc_info(void) {
 
   return RPC_PORT_ERROR_NONE;
 }
+
+RPC_API int  rpc_port_get_peer_info(rpc_port_h h, pid_t* pid, uid_t* uid) {
+  if (h == nullptr)
+    return RPC_PORT_ERROR_INVALID_PARAMETER;
+
+  auto port = static_cast<Port*>(h);
+  std::shared_ptr<PeerCred> cred(PeerCred::Get(port->GetFd()));
+  if (cred.get() == nullptr)
+    return RPC_PORT_ERROR_IO_ERROR;
+
+  if (pid)
+    *pid = cred->GetPid();
+
+  if (uid)
+    *uid = cred->GetUid();
+
+  return RPC_PORT_ERROR_NONE;
+}
+