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