daemon/socket: Get client pid from fd
authorSangchul Lee <sc11.lee@samsung.com>
Thu, 27 Apr 2023 02:29:31 +0000 (11:29 +0900)
committer이상철/Tizen Platform Lab(SR)/삼성전자 <sc11.lee@samsung.com>
Fri, 28 Apr 2023 09:06:30 +0000 (18:06 +0900)
Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
src/daemon/espp_service_socket.c

index 924c844b75c06c630c7b8f48659d23eeded710ae..335e02da7a635090cff474367f4aab8edd1872b3 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "espp_service_priv.h"
 #include <gio/gio.h>
+#define __USE_GNU /* define for ucred in socket.h */
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <pthread.h>
@@ -37,6 +38,7 @@ do { \
 typedef struct {
        espp_service_s *svc;
        int fd;
+       unsigned int pid;
 } work_thread_userdata_s;
 
 static int __event_ret_msg_handling(int fd, espp_service_s *svc)
@@ -152,6 +154,23 @@ exit:
        pthread_exit(NULL);
 }
 
+static unsigned int get_pid_from_fd(int fd)
+{
+       struct ucred cred;
+       socklen_t len = (socklen_t)sizeof(struct ucred);
+       char str_error[MAX_ERROR_LEN] = {'\0',};
+
+       if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) < 0) {
+               strerror_r(errno, str_error, sizeof(str_error));
+               LOG_ERROR("failed to getsockopt(), err: %s", str_error);
+               return 0;
+       }
+
+       LOG_INFO("fd[%d] pid[%u]", fd, cred.pid);
+
+       return cred.pid;
+}
+
 static void *__listen_thread_func(void *data)
 {
        espp_service_s *svc = (espp_service_s *)data;
@@ -201,6 +220,7 @@ static void *__listen_thread_func(void *data)
                userdata = g_new0(work_thread_userdata_s, 1);
                userdata->svc = svc;
                userdata->fd = client_fd;
+               userdata->pid = get_pid_from_fd(client_fd);
 
                if (pthread_create(&work_thread_id, &attr, (void *)__work_thread_func, (void *)userdata)) {
                        LOG_ERROR("failed to pthread_create(), client_fd[%d]", client_fd);