Coverage Improvement of Contacts Service
[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         int wrn = 0;
41
42         WARN("ctsvc_socket_init: socket ref count : %d", __ctsvc_conn_refcnt);
43         if (0 < __ctsvc_conn_refcnt) {
44                 /* LCOV_EXCL_START */
45                 __ctsvc_conn_refcnt++;
46                 return  CONTACTS_ERROR_NONE;
47                 /* LCOV_EXCL_STOP */
48         }
49
50         char sock_file[CTSVC_PATH_MAX_LEN] = {0};
51
52 #ifdef _CONTACTS_IPC_CLIENT
53         if (ctsvc_client_is_in_system_session()) {
54                 /* LCOV_EXCL_START */
55                 if (CONTACTS_ERROR_NONE != ctsvc_client_get_active_uid(&uid))
56                         return CONTACTS_ERROR_SYSTEM;
57                 /* LCOV_EXCL_STOP */
58         }
59 #endif
60
61         wrn = snprintf(sock_file, sizeof(sock_file), CTSVC_SOCK_PATH"/%s", uid, CTSVC_SOCKET_FILE);
62         if (wrn < 0){
63                 WARN("Error writing in sock file (%s)", sock_file); /* LCOV_EXCL_LINE */
64         }
65
66         bzero(&caddr, sizeof(caddr));
67         caddr.sun_family = AF_UNIX;
68         wrn = snprintf(caddr.sun_path, sizeof(caddr.sun_path), "%s", sock_file);
69         if (wrn < 0){
70                 WARN("Error writing in unix socket path (%s)", caddr.sun_path); /* LCOV_EXCL_LINE */
71         }
72         __ctsvc_sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
73         RETVM_IF(-1 == __ctsvc_sockfd, CONTACTS_ERROR_IPC, /* LCOV_EXCL_LINE */
74                         "socket() Fail(errno = %d)", errno); /* LCOV_EXCL_LINE */
75
76         ret = connect(__ctsvc_sockfd, (struct sockaddr *)&caddr, sizeof(caddr));
77         if (-1 == ret) {
78                 /* LCOV_EXCL_START */
79                 ERR("connect() Fail(errno = %d)", errno);
80                 close(__ctsvc_sockfd);
81                 __ctsvc_sockfd = -1;
82                 return CONTACTS_ERROR_IPC;
83                 /* LCOV_EXCL_STOP */
84         }
85
86         __ctsvc_conn_refcnt++;
87         return CONTACTS_ERROR_NONE;
88 }
89
90 void ctsvc_socket_final(void)
91 {
92         if (1 < __ctsvc_conn_refcnt) {
93                 /* LCOV_EXCL_START */
94                 DBG("socket ref count : %d", __ctsvc_conn_refcnt);
95                 __ctsvc_conn_refcnt--;
96                 return;
97                 /* LCOV_EXCL_STOP */
98         } else if (__ctsvc_conn_refcnt < 1) {
99                 DBG("Please call connection API. socket ref count : %d", __ctsvc_conn_refcnt);
100                 return;
101         }
102         __ctsvc_conn_refcnt--;
103
104         close(__ctsvc_sockfd);
105         __ctsvc_sockfd = -1;
106 }
107                 /* LCOV_EXCL_START */
108 static inline int __ctsvc_safe_write(int fd, const char *buf, int buf_size)
109 {
110         int ret, writed = 0;
111         while (buf_size) {
112                 ret = write(fd, buf+writed, buf_size);
113                 if (-1 == ret) {
114                         if (EINTR == errno)
115                                 continue;
116                         else
117                                 return ret;
118                 }
119                 writed += ret;
120                 buf_size -= ret;
121         }
122         return writed;
123 }
124
125 static inline int __ctsvc_safe_read(int fd, char *buf, int buf_size)
126 {
127         int ret, read_size = 0;
128         while (buf_size) {
129                 ret = read(fd, buf+read_size, buf_size);
130                 if (-1 == ret) {
131                         if (EINTR == errno)
132                                 continue;
133                         else
134                                 return ret;
135                 }
136                 read_size += ret;
137                 buf_size -= ret;
138         }
139         return read_size;
140 }
141
142 static int __ctsvc_socket_handle_return(int fd, ctsvc_socket_msg_s *msg)
143 {
144         CTS_FN_CALL;
145         int ret;
146
147         ret = __ctsvc_safe_read(fd, (char *)msg, sizeof(ctsvc_socket_msg_s));
148         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_read() Fail(errno = %d)", errno);
149
150         WARN_IF(CTSVC_SOCKET_MSG_TYPE_REQUEST_RETURN_VALUE != msg->type,
151                         "Unknown Type(%d), ret=%d, attach_num= %d,"
152                         "attach1 = %d, attach2 = %d, attach3 = %d, attach4 = %d",
153                         msg->type, msg->val, msg->attach_num,
154                         msg->attach_sizes[0], msg->attach_sizes[1], msg->attach_sizes[2],
155                         msg->attach_sizes[3]);
156
157         RETVM_IF(CTSVC_SOCKET_MSG_REQUEST_MAX_ATTACH < msg->attach_num, CONTACTS_ERROR_IPC,
158                         "Invalid msg(attach_num = %d)", msg->attach_num);
159
160         return CONTACTS_ERROR_NONE;
161 }
162
163 static int __ctsvc_cancel_sim_import(int fd)
164 {
165         int ret;
166         ctsvc_socket_msg_s msg = {0};
167
168         RETVM_IF(-1 == fd, CONTACTS_ERROR_IPC, "socket is not connected");
169
170         msg.type = CTSVC_SOCKET_MSG_TYPE_CANCEL_IMPORT_SIM;
171
172         ret = __ctsvc_safe_write(fd, (char *)&msg, sizeof(msg));
173         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
174
175         return CONTACTS_ERROR_NONE;
176 }
177
178 static int __ctsvc_client_socket_response_handler(int fd, ctsvc_socket_msg_s *msg, contacts_sim_import_progress_cb callback, void *user_data)
179 {
180         CTS_FN_CALL;
181         int ret;
182
183         do {
184                 ret = __ctsvc_safe_read(fd, (char *)msg, sizeof(ctsvc_socket_msg_s));
185                 RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_read() Fail(errno = %d)", errno);
186                 RETVM_IF(CTSVC_SOCKET_MSG_REQUEST_MAX_ATTACH < msg->attach_num, CONTACTS_ERROR_IPC,
187                                         "Invalid msg(attach_num = %d)", msg->attach_num);
188
189                 switch (msg->type) {
190                 case CTSVC_SOCKET_MSG_TYPE_REQUEST_RETURN_VALUE:
191                         break;
192
193                 case CTSVC_SOCKET_MSG_TYPE_REQUEST_INVOKE_CB:
194                         DBG("total[%d], index[%d]", msg->val, msg->data);
195                         if (callback) {
196                                 if (false == callback(msg->val, msg->data, user_data))
197                                         __ctsvc_cancel_sim_import(fd);
198                         } else {
199                                 WARN("No callback");
200                         }
201                         break;
202
203                 default:
204                         WARN("Unknown Type(%d), ret=%d, attach_num= %d,"
205                                         "attach1 = %d, attach2 = %d, attach3 = %d, attach4 = %d",
206                                         msg->type, msg->val, msg->attach_num,
207                                         msg->attach_sizes[0], msg->attach_sizes[1], msg->attach_sizes[2],
208                                         msg->attach_sizes[3]);
209                         break;
210                 }
211         } while (CTSVC_SOCKET_MSG_TYPE_REQUEST_RETURN_VALUE != msg->type);
212
213         return CONTACTS_ERROR_NONE;
214 }
215
216 static void __ctsvc_remove_invalid_msg(int fd, int size)
217 {
218         int ret;
219         char dummy[CTSVC_SOCKET_MSG_SIZE] = {0};
220
221         while (size) {
222                 if (sizeof(dummy) < size) {
223
224                         ret = read(fd, dummy, sizeof(dummy));
225                         if (-1 == ret) {
226                                 if (EINTR == errno)
227                                         continue;
228                                 else
229                                         return;
230                         }
231                         size -= ret;
232                 } else {
233                         ret = read(fd, dummy, size);
234                         if (-1 == ret) {
235                                 if (EINTR == errno)
236                                         continue;
237                                 else
238                                         return;
239                         }
240                         size -= ret;
241                 }
242         }
243 }
244 int ctsvc_request_sim_import(int sim_slot_no, contacts_sim_import_progress_cb callback,
245                 void *user_data)
246 {
247         int i, ret;
248         ctsvc_socket_msg_s msg = {0};
249         char src[64] = {0};
250
251         RETVM_IF(-1 == __ctsvc_sockfd, CONTACTS_ERROR_IPC, "socket is not connected");
252
253         msg.type = CTSVC_SOCKET_MSG_TYPE_REQUEST_IMPORT_SIM;
254
255         snprintf(src, sizeof(src), "%d", sim_slot_no);
256         msg.attach_num = 1;
257         msg.attach_sizes[0] = strlen(src);
258
259         ret = __ctsvc_safe_write(__ctsvc_sockfd, (char *)&msg, sizeof(msg));
260         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
261
262         ret = __ctsvc_safe_write(__ctsvc_sockfd, src, msg.attach_sizes[0]);
263         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
264
265         ret = __ctsvc_client_socket_response_handler(__ctsvc_sockfd, &msg, callback, user_data);
266         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "__ctsvc_socket_handle_return() Fail(%d)", ret);
267         DBG("attach_num = %d", msg.attach_num);
268
269         for (i = 0; i < msg.attach_num; i++)
270                 __ctsvc_remove_invalid_msg(__ctsvc_sockfd, msg.attach_sizes[i]);
271
272         return msg.val;
273 }
274
275 int ctsvc_request_sim_get_initialization_status(int sim_slot_no, bool *completed)
276 {
277         int ret = 0;
278         ctsvc_socket_msg_s msg = {0};
279         char dest[CTSVC_SOCKET_MSG_SIZE] = {0};
280         char src[64] = {0};
281
282         RETVM_IF(-1 == __ctsvc_sockfd, CONTACTS_ERROR_IPC, "socket is not connected");
283
284         msg.type = CTSVC_SOCKET_MSG_TYPE_REQUEST_SIM_INIT_COMPLETE;
285
286         snprintf(src, sizeof(src), "%d", sim_slot_no);
287         msg.attach_num = 1;
288         msg.attach_sizes[0] = strlen(src);
289
290         ret = __ctsvc_safe_write(__ctsvc_sockfd, (char *)&msg, sizeof(msg));
291         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
292
293         ret = __ctsvc_safe_write(__ctsvc_sockfd, src, msg.attach_sizes[0]);
294         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_write() Fail(errno = %d)", errno);
295
296         ret = __ctsvc_socket_handle_return(__ctsvc_sockfd, &msg);
297         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "__ctsvc_socket_handle_return() Fail(%d)", ret);
298         DBG("attach_num = %d", msg.attach_num);
299
300         ret = __ctsvc_safe_read(__ctsvc_sockfd, dest, msg.attach_sizes[0]);
301         RETVM_IF(-1 == ret, CONTACTS_ERROR_IPC, "__ctsvc_safe_read() Fail(errno = %d)", errno);
302
303         if (atoi(dest) == 0)
304                 *completed = false;
305         else
306                 *completed = true;
307
308         INFO("sim init complete : %d", *completed);
309
310         return msg.val;
311 }
312 /* LCOV_EXCL_STOP */