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