Enable to send and receive multiple fds simultaneously 16/91716/12
authorYoungHun Kim <yh8004.kim@samsung.com>
Tue, 11 Oct 2016 04:24:11 +0000 (13:24 +0900)
committerYoungHun Kim <yh8004.kim@samsung.com>
Wed, 19 Oct 2016 08:46:30 +0000 (17:46 +0900)
Change-Id: Ieadabab3a6a38c9bfec79ac40c735949c05bad07

include/muse_core.h
include/muse_core_ipc.h
packaging/mused.spec
src/muse_core_ipc.c
src/muse_core_msg_json.c

index 84adcc0..c90fa27 100644 (file)
@@ -30,6 +30,7 @@ extern "C" {
 
 #define MUSE_URI_MAX_LENGTH 4096
 #define MUSE_MSG_MAX_LENGTH 4096
+#define MUSE_NUM_FD 4
 
 typedef struct muse_module *muse_module_h;
 
index 542fc59..3d05ef4 100644 (file)
@@ -50,9 +50,9 @@ typedef enum {
 
 gboolean muse_core_ipc_job_function(struct muse_core_workqueue_job * job);
 int muse_core_ipc_send_msg(int sock_fd, const char *msg);
-int muse_core_ipc_send_fd_msg(int sock_fd, int fd, const char *buf);
+int muse_core_ipc_send_fd_msg(int sock_fd, int *fds, const char *buf);
 int muse_core_ipc_recv_msg(int sock_fd, char *msg);
-int muse_core_ipc_recv_fd_msg(int sock_fd, const char *buf, int *out_fd);
+int muse_core_ipc_recv_fd_msg(int sock_fd, char *buf, int *out_fd);
 void muse_core_ipc_set_timeout(int sock_fd, unsigned long timeout_sec);
 gboolean muse_core_ipc_data_job_function(muse_core_workqueue_job_t * job);
 int muse_core_ipc_push_data(int sock_fd, const char *data, int size, uint64_t data_id);
index e4c1ebd..84014f5 100644 (file)
@@ -1,6 +1,6 @@
 Name:       mused
 Summary:    A Multimedia Daemon in Tizen Native API
-Version:    0.1.21
+Version:    0.1.22
 Release:    0
 Group:      System/Libraries
 License:    Apache-2.0
index 161b0fc..3f2e381 100644 (file)
@@ -155,7 +155,7 @@ static gpointer _muse_core_ipc_dispatch_worker(gpointer data)
                                                muse_core_module_get_instance()->dispatch(cmd, module);
 
                                                if (module->is_create_api_called == false) {
-                                                       LOGE("<<< ### _muse_core_ipc_client_cleanup [module %p] [get_dllsymbol_loaded_value %d]", module, value);
+                                                       LOGE("_muse_core_ipc_client_cleanup [module %p] [get_dllsymbol_loaded_value %d]", module, value);
 
                                                        if (!value) {
                                                                LOGW("release module dll handle");
@@ -292,14 +292,10 @@ static int _muse_core_ipc_msg_complete_confirm(char *msg)
 {
        char *ptr = NULL;
        size_t ptr_len = 0;
-       size_t msg_len = 0;
        int idx = 0;
 
        g_return_val_if_fail(msg != NULL, MM_ERROR_NONE);
 
-       msg_len = strlen(msg);
-       msg[msg_len + 1] = '\0';
-
        if (g_muse_core_ipc == NULL)
                return MM_ERROR_NONE;
 
@@ -312,7 +308,6 @@ static int _muse_core_ipc_msg_complete_confirm(char *msg)
                idx = ptr - msg;
                memcpy(g_muse_core_ipc->muse_msg->cache, ptr + 1, ptr_len);
                g_muse_core_ipc->muse_msg->cache_len = ptr_len;
-               msg[idx + 1] = '\0';
        }
 
        return ptr_len;
@@ -451,17 +446,18 @@ gboolean muse_core_ipc_data_job_function(muse_core_workqueue_job_t *job)
 int muse_core_ipc_send_msg(int sock_fd, const char *msg)
 {
        g_return_val_if_fail(msg != NULL, MM_ERROR_INVALID_ARGUMENT);
-       return muse_core_ipc_send_fd_msg(sock_fd, SOCK_ERR, msg);
+       return muse_core_ipc_send_fd_msg(sock_fd, NULL, msg);
 }
 
-int muse_core_ipc_send_fd_msg(int sock_fd, int fd, const char *buf)
+int muse_core_ipc_send_fd_msg(int sock_fd, int *fds, const char *buf)
 {
        int ret = MM_ERROR_NONE;
        struct cmsghdr *cptr;
        struct msghdr msg;
        struct iovec iov;
-       char data[CMSG_SPACE(sizeof(int))];
+       char data[CMSG_SPACE(sizeof(int)) * MUSE_NUM_FD];
        char err_msg[MAX_ERROR_MSG_LEN] = {'\0',};
+       int *fdptr;
 
        g_return_val_if_fail(buf != NULL, MM_ERROR_INVALID_ARGUMENT);
 
@@ -470,7 +466,7 @@ int muse_core_ipc_send_fd_msg(int sock_fd, int fd, const char *buf)
 #endif
 
        memset(&iov, 0, sizeof(iov));
-       iov.iov_base = (void*)buf;
+       iov.iov_base = (void *)buf;
        iov.iov_len = strlen(buf);
 
        memset(&msg, 0, sizeof(msg));
@@ -479,7 +475,7 @@ int muse_core_ipc_send_fd_msg(int sock_fd, int fd, const char *buf)
        msg.msg_iov = &iov;
        msg.msg_iovlen = 1;
 
-       if (fd == SOCK_ERR) { /* when muse_core_ipc_send_msg is called */
+       if (fds == NULL) { /* when muse_core_ipc_send_msg is called */
                msg.msg_control = NULL;
                msg.msg_controllen = 0;
        } else {
@@ -489,12 +485,14 @@ int muse_core_ipc_send_fd_msg(int sock_fd, int fd, const char *buf)
 
                cptr = CMSG_FIRSTHDR(&msg);
                if (cptr) {
-                       cptr->cmsg_len = CMSG_LEN(sizeof(int));
+                       cptr->cmsg_len = CMSG_LEN(sizeof(int) * MUSE_NUM_FD);
                        cptr->cmsg_level = SOL_SOCKET;
                        cptr->cmsg_type = SCM_RIGHTS;
-                       *((int *) CMSG_DATA(cptr)) = fd;
-               } else {
-                       LOGE("CMSG_FIRSTHDR NULL");
+                       fdptr = (int *)CMSG_DATA(cptr);
+                       memcpy(fdptr, fds, sizeof(int) * MUSE_NUM_FD);
+
+                       /* the value of msg_controllen increases after memcpy so reassignes the orginal value */
+                       msg.msg_controllen = cptr->cmsg_len;
                }
        }
 
@@ -512,15 +510,16 @@ int muse_core_ipc_recv_msg(int sock_fd, char *msg)
        return muse_core_ipc_recv_fd_msg(sock_fd, msg, NULL);
 }
 
-int muse_core_ipc_recv_fd_msg(int sock_fd, const char *buf, int *out_fd)
+int muse_core_ipc_recv_fd_msg(int sock_fd, char *buf, int *out_fd)
 {
        int ret = MM_ERROR_NONE;
        struct cmsghdr *cptr;
        struct msghdr msg;
        struct iovec iov;
        int remained_msg_len = 0;
-       char data[CMSG_SPACE(sizeof(int))];
+       char data[CMSG_SPACE(sizeof(int) * MUSE_NUM_FD)];
        char err_msg[MAX_ERROR_MSG_LEN] = {'\0',};
+       int idx;
 
        g_return_val_if_fail(buf != NULL, MM_ERROR_INVALID_ARGUMENT);
 
@@ -541,20 +540,25 @@ int muse_core_ipc_recv_fd_msg(int sock_fd, const char *buf, int *out_fd)
        if ((ret = recvmsg(sock_fd, &msg, 0)) == SOCK_ERR) {
                strerror_r(errno, err_msg, MAX_ERROR_MSG_LEN);
                LOGE("fail to receive msg (%s)", err_msg);
+               return ret;
        }
 
-       /* When muse_core_ipc_send_fd_msg is called, cptr is null because send_fd is -1 (EBADF ERROR) */
-       cptr = CMSG_FIRSTHDR(&msg);
-
        if (out_fd) {
-               if (cptr) {
-                       *out_fd = *(int*)CMSG_DATA(cptr);
-                       LOGD("out fd : %d", *out_fd);
-               } else {
-                       *out_fd = SOCK_ERR;
+               /* When muse_core_ipc_send_fd_msg is called, cptr is null because send_fd is -1 (EBADF ERROR) */
+               cptr = CMSG_FIRSTHDR(&msg);
+
+               for (idx = 0; idx < MUSE_NUM_FD; idx++) {
+                       if (cptr == NULL) {
+                               out_fd[idx] = SOCK_ERR; /* you can check that idx is the last pointer */
+                               break;
+                       }
+                       out_fd[idx] = *(int *)CMSG_DATA(cptr);
+                       cptr = CMSG_NXTHDR(&msg, cptr);
                }
        }
 
+       buf[ret] = '\0';
+
        return ret;
 }
 
index 5929613..e4270ba 100644 (file)
@@ -265,24 +265,19 @@ gboolean muse_core_msg_json_object_get_value(const char *key, void* jobj, void *
                break;
        case json_type_double:
                *(double *)data = json_object_get_double(val);
-               LOGD("json_type_double (%s)          value: %p", key, (double *)data);
                break;
        case json_type_int:
                if (m_type == MUSE_TYPE_ANY || m_type == MUSE_TYPE_INT) {
                        *(int32_t *)data = json_object_get_int(val);
-                       LOGD("json_type_int (%s)          value: %d", key, *(int32_t *)data);
                } else if (m_type == MUSE_TYPE_INT64) {
                        *(int64_t *)data = json_object_get_int64(val);
-                       LOGD("json_type_int (%s)          value: %" G_GINT64_FORMAT "", key, *(int64_t *)data);
                } else if (m_type == MUSE_TYPE_POINTER) {
                        if (sizeof(intptr_t) == 8)
                                *(intptr_t *)data = json_object_get_int64(val);
                        else
                                *(intptr_t *)data = json_object_get_int(val);
-                       LOGD("json_type_int (%s)          value: %p", key, *(intptr_t *)data);
                } else if (m_type == MUSE_TYPE_DOUBLE) {
                        *(double *)data = json_object_get_double(val);
-                       LOGD("json_type_double (%s)          value: %.20lf", key, *(double *)data);
                }
                break;
        case json_type_object: