Adding extra logs
[platform/core/pim/contacts-service.git] / common / ctsvc_socket.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include <errno.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <unistd.h>
23
24 #include "contacts.h"
25 #include "ctsvc_internal.h"
26 #include "ctsvc_socket.h"
27 #include "ctsvc_mutex.h"
28 #include "ctsvc_inotify.h"
29 #ifdef _CONTACTS_IPC_CLIENT
30 #include "ctsvc_client_utils.h"
31 #endif
32 static int __ctsvc_conn_refcnt = 0;
33 static int __ctsvc_sockfd = -1;
34
35 int ctsvc_socket_init(void)
36 {
37         int ret;
38         struct sockaddr_un caddr;
39         uid_t uid = getuid();
40
41         WARN("ctsvc_socket_init: socket ref count : %d", __ctsvc_conn_refcnt);
42         if (0 < __ctsvc_conn_refcnt) {
43                 __ctsvc_conn_refcnt++;
44                 return  CONTACTS_ERROR_NONE;
45         }
46
47         char sock_file[CTSVC_PATH_MAX_LEN] = {0};
48
49 #ifdef _CONTACTS_IPC_CLIENT
50         if (ctsvc_client_is_in_system_session()) {
51                 /* LCOV_EXCL_START */
52                 if (CONTACTS_ERROR_NONE != ctsvc_client_get_active_uid(&uid))
53                         return CONTACTS_ERROR_SYSTEM;
54                 /* LCOV_EXCL_STOP */
55         }
56 #endif
57
58         snprintf(sock_file, sizeof(sock_file), CTSVC_SOCK_PATH"/%s", uid, CTSVC_SOCKET_FILE);
59
60         bzero(&caddr, sizeof(caddr));
61         caddr.sun_family = AF_UNIX;
62         snprintf(caddr.sun_path, sizeof(caddr.sun_path), "%s", sock_file);
63
64         __ctsvc_sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
65         RETVM_IF(-1 == __ctsvc_sockfd, CONTACTS_ERROR_IPC,
66                         "socket() Fail(errno = %d)", errno);
67
68         ret = connect(__ctsvc_sockfd, (struct sockaddr *)&caddr, sizeof(caddr));
69         if (-1 == ret) {
70                 /* LCOV_EXCL_START */
71                 ERR("connect() Fail(errno = %d)", errno);
72                 close(__ctsvc_sockfd);
73                 __ctsvc_sockfd = -1;
74                 return CONTACTS_ERROR_IPC;
75                 /* LCOV_EXCL_STOP */
76         }
77
78         __ctsvc_conn_refcnt++;
79         return CONTACTS_ERROR_NONE;
80 }
81
82 void ctsvc_socket_final(void)
83 {
84         if (1 < __ctsvc_conn_refcnt) {
85                 DBG("socket ref count : %d", __ctsvc_conn_refcnt);
86                 __ctsvc_conn_refcnt--;
87                 return;
88         } else if (__ctsvc_conn_refcnt < 1) {
89                 DBG("Please call connection API. socket ref count : %d", __ctsvc_conn_refcnt);
90                 return;
91         }
92         __ctsvc_conn_refcnt--;
93
94         close(__ctsvc_sockfd);
95         __ctsvc_sockfd = -1;
96 }
97
98 static inline int __ctsvc_safe_write(int fd, const char *buf, int buf_size)
99 {
100         int ret, writed = 0;
101         while (buf_size) {
102                 ret = write(fd, buf+writed, buf_size);
103                 if (-1 == ret) {
104                         if (EINTR == errno)
105                                 continue;
106                         else
107                                 return ret;
108                 }
109                 writed += ret;
110                 buf_size -= ret;
111         }
112         return writed;
113 }
114
115 static inline int __ctsvc_safe_read(int fd, char *buf, int buf_size)
116 {
117         int ret, read_size = 0;
118         while (buf_size) {
119                 ret = read(fd, buf+read_size, buf_size);
120                 if (-1 == ret) {
121                         if (EINTR == errno)
122                                 continue;
123                         else
124                                 return ret;
125                 }
126                 read_size += ret;
127                 buf_size -= ret;
128         }
129         return read_size;
130 }
131
132 static int __ctsvc_socket_handle_return(int fd, ctsvc_socket_msg_s *msg)
133 {
134         CTS_FN_CALL;
135         int ret;
136
137         ret = __ctsvc_safe_read(fd, (char *)msg, sizeof(ctsvc_socket_msg_s));
138         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_read() Fail(errno = %d)", errno);
139
140         WARN_IF(CTSVC_SOCKET_MSG_TYPE_REQUEST_RETURN_VALUE != msg->type,
141                         "Unknown Type(%d), ret=%d, attach_num= %d,"
142                         "attach1 = %d, attach2 = %d, attach3 = %d, attach4 = %d",
143                         msg->type, msg->val, msg->attach_num,
144                         msg->attach_sizes[0], msg->attach_sizes[1], msg->attach_sizes[2],
145                         msg->attach_sizes[3]);
146
147         RETVM_IF(CTSVC_SOCKET_MSG_REQUEST_MAX_ATTACH < msg->attach_num, CONTACTS_ERROR_IPC,
148                         "Invalid msg(attach_num = %d)", msg->attach_num);
149
150         return CONTACTS_ERROR_NONE;
151 }
152
153 static int __ctsvc_cancel_sim_import(int fd)
154 {
155         int ret;
156         ctsvc_socket_msg_s msg = {0};
157
158         RETVM_IF(-1 == fd, CONTACTS_ERROR_IPC, "socket is not connected");
159
160         msg.type = CTSVC_SOCKET_MSG_TYPE_CANCEL_IMPORT_SIM;
161
162         ret = __ctsvc_safe_write(fd, (char *)&msg, sizeof(msg));
163         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
164
165         return CONTACTS_ERROR_NONE;
166 }
167
168 static int __ctsvc_client_socket_response_handler(int fd, ctsvc_socket_msg_s *msg, contacts_sim_import_progress_cb callback, void *user_data)
169 {
170         CTS_FN_CALL;
171         int ret;
172
173         do {
174                 ret = __ctsvc_safe_read(fd, (char *)msg, sizeof(ctsvc_socket_msg_s));
175                 RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_read() Fail(errno = %d)", errno);
176                 RETVM_IF(CTSVC_SOCKET_MSG_REQUEST_MAX_ATTACH < msg->attach_num, CONTACTS_ERROR_IPC,
177                                         "Invalid msg(attach_num = %d)", msg->attach_num);
178
179                 switch (msg->type) {
180                 case CTSVC_SOCKET_MSG_TYPE_REQUEST_RETURN_VALUE:
181                         break;
182
183                 case CTSVC_SOCKET_MSG_TYPE_REQUEST_INVOKE_CB:
184                         DBG("total[%d], index[%d]", msg->val, msg->data);
185                         if (callback) {
186                                 if (false == callback(msg->val, msg->data, user_data))
187                                         __ctsvc_cancel_sim_import(fd);
188                         } else {
189                                 WARN("No callback");
190                         }
191                         break;
192
193                 /* LCOV_EXCL_START */
194                 default:
195                         WARN("Unknown Type(%d), ret=%d, attach_num= %d,"
196                                         "attach1 = %d, attach2 = %d, attach3 = %d, attach4 = %d",
197                                         msg->type, msg->val, msg->attach_num,
198                                         msg->attach_sizes[0], msg->attach_sizes[1], msg->attach_sizes[2],
199                                         msg->attach_sizes[3]);
200                         break;
201                 /* LCOV_EXCL_STOP */
202                 }
203         } while (CTSVC_SOCKET_MSG_TYPE_REQUEST_RETURN_VALUE != msg->type);
204
205         return CONTACTS_ERROR_NONE;
206 }
207
208 static void __ctsvc_remove_invalid_msg(int fd, int size)
209 {
210         int ret;
211         char dummy[CTSVC_SOCKET_MSG_SIZE] = {0};
212
213         while (size) {
214                 if (sizeof(dummy) < size) {
215                         ret = read(fd, dummy, sizeof(dummy));
216                         if (-1 == ret) {
217                                 if (EINTR == errno)
218                                         continue;
219                                 else
220                                         return;
221                         }
222                         size -= ret;
223                 } else {
224                         ret = read(fd, dummy, size);
225                         if (-1 == ret) {
226                                 if (EINTR == errno)
227                                         continue;
228                                 else
229                                         return;
230                         }
231                         size -= ret;
232                 }
233         }
234 }
235
236 int ctsvc_request_sim_import(int sim_slot_no, contacts_sim_import_progress_cb callback,
237                 void *user_data)
238 {
239         int i, ret;
240         ctsvc_socket_msg_s msg = {0};
241         char src[64] = {0};
242
243         RETVM_IF(-1 == __ctsvc_sockfd, CONTACTS_ERROR_IPC, "socket is not connected");
244
245         msg.type = CTSVC_SOCKET_MSG_TYPE_REQUEST_IMPORT_SIM;
246
247         snprintf(src, sizeof(src), "%d", sim_slot_no);
248         msg.attach_num = 1;
249         msg.attach_sizes[0] = strlen(src);
250
251         ret = __ctsvc_safe_write(__ctsvc_sockfd, (char *)&msg, sizeof(msg));
252         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
253
254         ret = __ctsvc_safe_write(__ctsvc_sockfd, src, msg.attach_sizes[0]);
255         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
256
257         ret = __ctsvc_client_socket_response_handler(__ctsvc_sockfd, &msg, callback, user_data);
258         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "__ctsvc_socket_handle_return() Fail(%d)", ret);
259         DBG("attach_num = %d", msg.attach_num);
260
261         for (i = 0; i < msg.attach_num; i++)
262                 __ctsvc_remove_invalid_msg(__ctsvc_sockfd, msg.attach_sizes[i]);
263
264         return msg.val;
265 }
266
267 int ctsvc_request_sim_get_initialization_status(int sim_slot_no, bool *completed)
268 {
269         int ret = 0;
270         ctsvc_socket_msg_s msg = {0};
271         char dest[CTSVC_SOCKET_MSG_SIZE] = {0};
272         char src[64] = {0};
273
274         RETVM_IF(-1 == __ctsvc_sockfd, CONTACTS_ERROR_IPC, "socket is not connected");
275
276         msg.type = CTSVC_SOCKET_MSG_TYPE_REQUEST_SIM_INIT_COMPLETE;
277
278         snprintf(src, sizeof(src), "%d", sim_slot_no);
279         msg.attach_num = 1;
280         msg.attach_sizes[0] = strlen(src);
281
282         ret = __ctsvc_safe_write(__ctsvc_sockfd, (char *)&msg, sizeof(msg));
283         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
284
285         ret = __ctsvc_safe_write(__ctsvc_sockfd, src, msg.attach_sizes[0]);
286         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
287
288         ret = __ctsvc_socket_handle_return(__ctsvc_sockfd, &msg);
289         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "__ctsvc_socket_handle_return() Fail(%d)", ret);
290         DBG("attach_num = %d", msg.attach_num);
291
292         ret = __ctsvc_safe_read(__ctsvc_sockfd, dest, msg.attach_sizes[0]);
293         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_read() Fail(errno = %d)", errno);
294
295         if (atoi(dest) == 0)
296                 *completed = false;
297         else
298                 *completed = true;
299
300         INFO("sim init complete : %d", *completed);
301
302         return msg.val;
303 }