Adding extra logs
[platform/core/pim/contacts-service.git] / common / ctsvc_socket.c
index ea369aa..189285d 100644 (file)
@@ -1,8 +1,7 @@
 /*
  * Contacts Service
  *
- * Copyright (c) 2010 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
- *
+ * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -27,7 +26,9 @@
 #include "ctsvc_socket.h"
 #include "ctsvc_mutex.h"
 #include "ctsvc_inotify.h"
-
+#ifdef _CONTACTS_IPC_CLIENT
+#include "ctsvc_client_utils.h"
+#endif
 static int __ctsvc_conn_refcnt = 0;
 static int __ctsvc_sockfd = -1;
 
@@ -35,14 +36,26 @@ int ctsvc_socket_init(void)
 {
        int ret;
        struct sockaddr_un caddr;
+       uid_t uid = getuid();
 
+       WARN("ctsvc_socket_init: socket ref count : %d", __ctsvc_conn_refcnt);
        if (0 < __ctsvc_conn_refcnt) {
                __ctsvc_conn_refcnt++;
                return  CONTACTS_ERROR_NONE;
        }
 
        char sock_file[CTSVC_PATH_MAX_LEN] = {0};
-       snprintf(sock_file, sizeof(sock_file), CTSVC_SOCK_PATH"/%s", getuid(), CTSVC_SOCKET_FILE);
+
+#ifdef _CONTACTS_IPC_CLIENT
+       if (ctsvc_client_is_in_system_session()) {
+               /* LCOV_EXCL_START */
+               if (CONTACTS_ERROR_NONE != ctsvc_client_get_active_uid(&uid))
+                       return CONTACTS_ERROR_SYSTEM;
+               /* LCOV_EXCL_STOP */
+       }
+#endif
+
+       snprintf(sock_file, sizeof(sock_file), CTSVC_SOCK_PATH"/%s", uid, CTSVC_SOCKET_FILE);
 
        bzero(&caddr, sizeof(caddr));
        caddr.sun_family = AF_UNIX;
@@ -50,14 +63,16 @@ int ctsvc_socket_init(void)
 
        __ctsvc_sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
        RETVM_IF(-1 == __ctsvc_sockfd, CONTACTS_ERROR_IPC,
-                       "socket() Failed(errno = %d)", errno);
+                       "socket() Fail(errno = %d)", errno);
 
        ret = connect(__ctsvc_sockfd, (struct sockaddr *)&caddr, sizeof(caddr));
        if (-1 == ret) {
-               CTS_ERR("connect() Failed(errno = %d)", errno);
+               /* LCOV_EXCL_START */
+               ERR("connect() Fail(errno = %d)", errno);
                close(__ctsvc_sockfd);
                __ctsvc_sockfd = -1;
                return CONTACTS_ERROR_IPC;
+               /* LCOV_EXCL_STOP */
        }
 
        __ctsvc_conn_refcnt++;
@@ -67,12 +82,11 @@ int ctsvc_socket_init(void)
 void ctsvc_socket_final(void)
 {
        if (1 < __ctsvc_conn_refcnt) {
-               CTS_DBG("socket ref count : %d", __ctsvc_conn_refcnt);
+               DBG("socket ref count : %d", __ctsvc_conn_refcnt);
                __ctsvc_conn_refcnt--;
                return;
-       }
-       else if (__ctsvc_conn_refcnt < 1) {
-               CTS_DBG("Please call connection API. socket ref count : %d", __ctsvc_conn_refcnt);
+       } else if (__ctsvc_conn_refcnt < 1) {
+               DBG("Please call connection API. socket ref count : %d", __ctsvc_conn_refcnt);
                return;
        }
        __ctsvc_conn_refcnt--;
@@ -83,7 +97,7 @@ void ctsvc_socket_final(void)
 
 static inline int __ctsvc_safe_write(int fd, const char *buf, int buf_size)
 {
-       int ret, writed=0;
+       int ret, writed = 0;
        while (buf_size) {
                ret = write(fd, buf+writed, buf_size);
                if (-1 == ret) {
@@ -100,7 +114,7 @@ static inline int __ctsvc_safe_write(int fd, const char *buf, int buf_size)
 
 static inline int __ctsvc_safe_read(int fd, char *buf, int buf_size)
 {
-       int ret, read_size=0;
+       int ret, read_size = 0;
        while (buf_size) {
                ret = read(fd, buf+read_size, buf_size);
                if (-1 == ret) {
@@ -115,20 +129,19 @@ static inline int __ctsvc_safe_read(int fd, char *buf, int buf_size)
        return read_size;
 }
 
-#ifdef ENABLE_SIM_FEATURE
 static int __ctsvc_socket_handle_return(int fd, ctsvc_socket_msg_s *msg)
 {
        CTS_FN_CALL;
        int ret;
 
        ret = __ctsvc_safe_read(fd, (char *)msg, sizeof(ctsvc_socket_msg_s));
-       RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_read() Failed(errno = %d)", errno);
+       RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_read() Fail(errno = %d)", errno);
 
        WARN_IF(CTSVC_SOCKET_MSG_TYPE_REQUEST_RETURN_VALUE != msg->type,
                        "Unknown Type(%d), ret=%d, attach_num= %d,"
                        "attach1 = %d, attach2 = %d, attach3 = %d, attach4 = %d",
                        msg->type, msg->val, msg->attach_num,
-                       msg->attach_sizes[0],msg->attach_sizes[1],msg->attach_sizes[2],
+                       msg->attach_sizes[0], msg->attach_sizes[1], msg->attach_sizes[2],
                        msg->attach_sizes[3]);
 
        RETVM_IF(CTSVC_SOCKET_MSG_REQUEST_MAX_ATTACH < msg->attach_num, CONTACTS_ERROR_IPC,
@@ -137,6 +150,61 @@ static int __ctsvc_socket_handle_return(int fd, ctsvc_socket_msg_s *msg)
        return CONTACTS_ERROR_NONE;
 }
 
+static int __ctsvc_cancel_sim_import(int fd)
+{
+       int ret;
+       ctsvc_socket_msg_s msg = {0};
+
+       RETVM_IF(-1 == fd, CONTACTS_ERROR_IPC, "socket is not connected");
+
+       msg.type = CTSVC_SOCKET_MSG_TYPE_CANCEL_IMPORT_SIM;
+
+       ret = __ctsvc_safe_write(fd, (char *)&msg, sizeof(msg));
+       RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
+
+       return CONTACTS_ERROR_NONE;
+}
+
+static int __ctsvc_client_socket_response_handler(int fd, ctsvc_socket_msg_s *msg, contacts_sim_import_progress_cb callback, void *user_data)
+{
+       CTS_FN_CALL;
+       int ret;
+
+       do {
+               ret = __ctsvc_safe_read(fd, (char *)msg, sizeof(ctsvc_socket_msg_s));
+               RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_read() Fail(errno = %d)", errno);
+               RETVM_IF(CTSVC_SOCKET_MSG_REQUEST_MAX_ATTACH < msg->attach_num, CONTACTS_ERROR_IPC,
+                                       "Invalid msg(attach_num = %d)", msg->attach_num);
+
+               switch (msg->type) {
+               case CTSVC_SOCKET_MSG_TYPE_REQUEST_RETURN_VALUE:
+                       break;
+
+               case CTSVC_SOCKET_MSG_TYPE_REQUEST_INVOKE_CB:
+                       DBG("total[%d], index[%d]", msg->val, msg->data);
+                       if (callback) {
+                               if (false == callback(msg->val, msg->data, user_data))
+                                       __ctsvc_cancel_sim_import(fd);
+                       } else {
+                               WARN("No callback");
+                       }
+                       break;
+
+               /* LCOV_EXCL_START */
+               default:
+                       WARN("Unknown Type(%d), ret=%d, attach_num= %d,"
+                                       "attach1 = %d, attach2 = %d, attach3 = %d, attach4 = %d",
+                                       msg->type, msg->val, msg->attach_num,
+                                       msg->attach_sizes[0], msg->attach_sizes[1], msg->attach_sizes[2],
+                                       msg->attach_sizes[3]);
+                       break;
+               /* LCOV_EXCL_STOP */
+               }
+       } while (CTSVC_SOCKET_MSG_TYPE_REQUEST_RETURN_VALUE != msg->type);
+
+       return CONTACTS_ERROR_NONE;
+}
+
 static void __ctsvc_remove_invalid_msg(int fd, int size)
 {
        int ret;
@@ -152,8 +220,7 @@ static void __ctsvc_remove_invalid_msg(int fd, int size)
                                        return;
                        }
                        size -= ret;
-               }
-               else {
+               } else {
                        ret = read(fd, dummy, size);
                        if (-1 == ret) {
                                if (EINTR == errno)
@@ -166,7 +233,8 @@ static void __ctsvc_remove_invalid_msg(int fd, int size)
        }
 }
 
-int ctsvc_request_sim_import(int sim_slot_no)
+int ctsvc_request_sim_import(int sim_slot_no, contacts_sim_import_progress_cb callback,
+               void *user_data)
 {
        int i, ret;
        ctsvc_socket_msg_s msg = {0};
@@ -181,16 +249,16 @@ int ctsvc_request_sim_import(int sim_slot_no)
        msg.attach_sizes[0] = strlen(src);
 
        ret = __ctsvc_safe_write(__ctsvc_sockfd, (char *)&msg, sizeof(msg));
-       RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Failed(errno = %d)", errno);
+       RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
 
        ret = __ctsvc_safe_write(__ctsvc_sockfd, src, msg.attach_sizes[0]);
-       RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Failed(errno = %d)", errno);
+       RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
 
-       ret = __ctsvc_socket_handle_return(__ctsvc_sockfd, &msg);
-       RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "__ctsvc_socket_handle_return() Failed(%d)", ret);
-       CTS_DBG("attach_num = %d", msg.attach_num);
+       ret = __ctsvc_client_socket_response_handler(__ctsvc_sockfd, &msg, callback, user_data);
+       RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "__ctsvc_socket_handle_return() Fail(%d)", ret);
+       DBG("attach_num = %d", msg.attach_num);
 
-       for (i=0;i<msg.attach_num;i++)
+       for (i = 0; i < msg.attach_num; i++)
                __ctsvc_remove_invalid_msg(__ctsvc_sockfd, msg.attach_sizes[i]);
 
        return msg.val;
@@ -212,26 +280,24 @@ int ctsvc_request_sim_get_initialization_status(int sim_slot_no, bool *completed
        msg.attach_sizes[0] = strlen(src);
 
        ret = __ctsvc_safe_write(__ctsvc_sockfd, (char *)&msg, sizeof(msg));
-       RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Failed(errno = %d)", errno);
+       RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
 
        ret = __ctsvc_safe_write(__ctsvc_sockfd, src, msg.attach_sizes[0]);
-       RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Failed(errno = %d)", errno);
+       RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
 
        ret = __ctsvc_socket_handle_return(__ctsvc_sockfd, &msg);
-       RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "__ctsvc_socket_handle_return() Failed(%d)", ret);
-       CTS_DBG("attach_num = %d", msg.attach_num);
+       RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "__ctsvc_socket_handle_return() Fail(%d)", ret);
+       DBG("attach_num = %d", msg.attach_num);
 
        ret = __ctsvc_safe_read(__ctsvc_sockfd, dest, msg.attach_sizes[0]);
-       RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_read() Failed(errno = %d)", errno);
+       RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_read() Fail(errno = %d)", errno);
 
-       if(atoi(dest) ==0)
+       if (atoi(dest) == 0)
                *completed = false;
        else
                *completed = true;
 
-       CTS_INFO("sim init complete : %d", *completed);
+       INFO("sim init complete : %d", *completed);
 
        return msg.val;
 }
-
-#endif // ENABLE_SIM_FEATURE