4 * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <pims-ipc-data.h>
29 #include "ctsvc_client_ipc.h"
30 #include "ctsvc_client_utils.h"
31 #include "ctsvc_client_service_helper.h"
33 #include "ctsvc_internal.h"
34 #include "ctsvc_list.h"
35 #include "ctsvc_record.h"
36 #include "ctsvc_inotify.h"
38 #include "ctsvc_ipc_define.h"
39 #include "ctsvc_ipc_marshal.h"
40 #include "ctsvc_view.h"
41 #include "ctsvc_mutex.h"
42 #include "ctsvc_handle.h"
49 static pthread_mutex_t _ctsvc_mutex_disconnected = PTHREAD_MUTEX_INITIALIZER;
50 static GHashTable *_ctsvc_ipc_table = NULL;
51 static bool _ctsvc_ipc_disconnected = false;
53 static pims_ipc_h _ctsvc_get_ipc_handle()
55 struct ctsvc_ipc_s *ipc_data = NULL;
56 char ipc_key[CTSVC_STR_SHORT_LEN] = {0};
57 RETVM_IF(NULL == _ctsvc_ipc_table, NULL, "contacts not connected");
59 snprintf(ipc_key, sizeof(ipc_key), "%u", ctsvc_client_get_tid());
60 ipc_data = g_hash_table_lookup(_ctsvc_ipc_table, ipc_key);
62 if (NULL == ipc_data) {
63 snprintf(ipc_key, sizeof(ipc_key), "%u", ctsvc_client_get_pid());
64 ipc_data = g_hash_table_lookup(_ctsvc_ipc_table, ipc_key);
67 RETVM_IF(NULL == ipc_data, NULL, "g_hash_table_lookup(%s) Fail", ipc_key);
72 bool ctsvc_ipc_is_busy()
76 pims_ipc_h ipc = _ctsvc_get_ipc_handle();
78 ERR("_ctsvc_get_ipc_handle() return NULL");
82 ret = pims_ipc_is_call_in_progress(ipc);
84 ERR("global ipc channel is busy.");
89 static int _ctsvc_ipc_create(pims_ipc_h *p_ipc)
91 char sock_file[CTSVC_PATH_MAX_LEN] = {0};
93 snprintf(sock_file, sizeof(sock_file), CTSVC_SOCK_PATH"/.%s", getuid(),
95 pims_ipc_h ipc = pims_ipc_create(sock_file);
97 if (errno == EACCES) {
98 ERR("pims_ipc_create() Fail(%d)", CONTACTS_ERROR_PERMISSION_DENIED);
99 return CONTACTS_ERROR_PERMISSION_DENIED;
101 ERR("pims_ipc_create() Fail(%d)", CONTACTS_ERROR_IPC_NOT_AVALIABLE);
102 return CONTACTS_ERROR_IPC_NOT_AVALIABLE;
107 return CONTACTS_ERROR_NONE;
110 static void _ctsvc_ipc_data_free(gpointer p)
112 struct ctsvc_ipc_s *ipc_data = p;
113 if (NULL == ipc_data)
117 ctsvc_ipc_unset_disconnected_cb(ipc_data->ipc);
118 pims_ipc_destroy(ipc_data->ipc);
121 g_list_free(ipc_data->list_handle);
126 static int _ctsvc_ipc_connect(contacts_h contact, pims_ipc_h ipc)
129 pims_ipc_data_h outdata = NULL;
130 pims_ipc_data_h indata = NULL;
132 /* Access control : put cookie to indata */
133 indata = pims_ipc_data_create(0);
134 if (indata == NULL) {
135 ERR("pims_ipc_data_create() return NULL");
136 return CONTACTS_ERROR_OUT_OF_MEMORY;
139 ret = ctsvc_ipc_marshal_handle(contact, indata);
140 if (CONTACTS_ERROR_NONE != ret) {
141 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
142 pims_ipc_data_destroy(indata);
147 ret = pims_ipc_call(ipc, CTSVC_IPC_MODULE, CTSVC_IPC_SERVER_CONNECT, indata,
150 ERR("pims_ipc_call() Fail(%d)", ret);
151 pims_ipc_data_destroy(indata);
152 return CONTACTS_ERROR_IPC;
154 pims_ipc_data_destroy(indata);
157 ctsvc_ipc_unmarshal_int(outdata, &ret);
158 pims_ipc_data_destroy(outdata);
159 if (CONTACTS_ERROR_NONE != ret)
160 ERR("ctsvc_ipc_unmarshal_int() return(%d)", ret);
165 static void _ctsvc_ipc_disconnected_cb(void *user_data)
167 ctsvc_ipc_set_disconnected(true);
170 int ctsvc_ipc_connect(contacts_h contact, unsigned int handle_id)
172 int ret = CONTACTS_ERROR_NONE;
173 struct ctsvc_ipc_s *ipc_data = NULL;
174 char ipc_key[CTSVC_STR_SHORT_LEN] = {0};
176 snprintf(ipc_key, sizeof(ipc_key), "%u", handle_id);
178 if (NULL == _ctsvc_ipc_table) {
179 _ctsvc_ipc_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
180 _ctsvc_ipc_data_free);
182 ipc_data = g_hash_table_lookup(_ctsvc_ipc_table, ipc_key);
185 if (NULL == ipc_data) {
186 ipc_data = calloc(1, sizeof(struct ctsvc_ipc_s));
187 if (NULL == ipc_data) {
188 ERR("calloc() Fail");
189 return CONTACTS_ERROR_OUT_OF_MEMORY;
191 ret = _ctsvc_ipc_create(&(ipc_data->ipc));
192 if (CONTACTS_ERROR_NONE != ret) {
193 _ctsvc_ipc_data_free(ipc_data);
196 g_hash_table_insert(_ctsvc_ipc_table, strdup(ipc_key), ipc_data);
197 ctsvc_ipc_set_disconnected_cb(ipc_data->ipc, _ctsvc_ipc_disconnected_cb, NULL);
199 _ctsvc_ipc_connect(contact, ipc_data->ipc);
200 ipc_data->list_handle = g_list_append(ipc_data->list_handle, contact);
202 return CONTACTS_ERROR_NONE;
206 int ctsvc_ipc_disconnect(contacts_h contact, unsigned int handle_id,
207 int connection_count)
209 int ret = CONTACTS_ERROR_NONE;
210 struct ctsvc_ipc_s *ipc_data = NULL;
211 pims_ipc_data_h outdata = NULL;
212 pims_ipc_data_h indata = NULL;
213 char ipc_key[CTSVC_STR_SHORT_LEN] = {0};
215 RETVM_IF(NULL == _ctsvc_ipc_table, CONTACTS_ERROR_IPC, "contacts not connected");
216 snprintf(ipc_key, sizeof(ipc_key), "%u", handle_id);
218 ipc_data = g_hash_table_lookup(_ctsvc_ipc_table, ipc_key);
219 RETVM_IF(ipc_data == NULL, CONTACTS_ERROR_IPC, "contacts not connected");
221 indata = pims_ipc_data_create(0);
222 if (indata == NULL) {
223 ERR("pims_ipc_data_create() Fail");
224 return CONTACTS_ERROR_OUT_OF_MEMORY;
227 ret = ctsvc_ipc_marshal_handle(contact, indata);
228 if (CONTACTS_ERROR_NONE != ret) {
229 ERR("ctsvc_ipc_marshal_handle() Fail(%d)", ret);
230 pims_ipc_data_destroy(indata);
234 ret = pims_ipc_call(ipc_data->ipc, CTSVC_IPC_MODULE, CTSVC_IPC_SERVER_DISCONNECT,
237 pims_ipc_data_destroy(indata);
238 ERR("pims_ipc_call() Fail(%d)", ret);
239 return CONTACTS_ERROR_IPC;
241 pims_ipc_data_destroy(indata);
244 ctsvc_ipc_unmarshal_int(outdata, &ret);
245 pims_ipc_data_destroy(outdata);
247 if (ret != CONTACTS_ERROR_NONE) {
248 ERR("ctsvc_ipc_unmarshal_int() Fail(%d)", ret);
252 if (1 == connection_count)
253 g_hash_table_remove(_ctsvc_ipc_table, ipc_key);
255 ERR("pims_ipc_call out data is NULL");
256 return CONTACTS_ERROR_IPC;
262 static void __ctsvc_ipc_lock()
264 if (0 == ctsvc_client_get_thread_connection_count())
265 ctsvc_mutex_lock(CTS_MUTEX_PIMS_IPC_CALL);
268 static void __ctsvc_ipc_unlock(void)
270 if (0 == ctsvc_client_get_thread_connection_count())
271 ctsvc_mutex_unlock(CTS_MUTEX_PIMS_IPC_CALL);
274 int ctsvc_ipc_call(char *module, char *function, pims_ipc_h data_in,
275 pims_ipc_data_h *data_out)
277 pims_ipc_h ipc_handle;
279 if (true == ctsvc_ipc_get_disconnected()) {
280 ctsvc_ipc_set_disconnected(false);
281 ctsvc_ipc_recovery();
282 ctsvc_ipc_recover_for_change_subscription();
285 ipc_handle = _ctsvc_get_ipc_handle();
288 int ret = pims_ipc_call(ipc_handle, module, function, data_in, data_out);
289 __ctsvc_ipc_unlock();
294 void ctsvc_client_ipc_set_change_version(contacts_h contact, int version)
296 ctsvc_base_s *base = (ctsvc_base_s*)contact;
298 RET_IF(NULL == contact);
300 base->version = version;
303 int ctsvc_client_ipc_get_change_version(contacts_h contact)
305 ctsvc_base_s *base = (ctsvc_base_s*)contact;
307 RETV_IF(NULL == contact, -1);
309 return base->version;
312 int ctsvc_ipc_client_check_permission(int permission, bool *result)
314 pims_ipc_data_h indata = NULL;
315 pims_ipc_data_h outdata = NULL;
321 indata = pims_ipc_data_create(0);
322 if (indata == NULL) {
323 ERR("pims_ipc_data_create() Fail");
324 return CONTACTS_ERROR_OUT_OF_MEMORY;
327 ret = ctsvc_ipc_marshal_int(permission, indata);
328 if (ret != CONTACTS_ERROR_NONE) {
329 ERR("ctsvc_ipc_marshal_int() Fail(%d)", ret);
330 pims_ipc_data_destroy(indata);
334 ret = ctsvc_ipc_call(CTSVC_IPC_MODULE, CTSVC_IPC_SERVER_CHECK_PERMISSION, indata,
337 ERR("ctsvc_ipc_call() Fail(%d)", ret);
338 pims_ipc_data_destroy(indata);
339 return CONTACTS_ERROR_IPC;
342 pims_ipc_data_destroy(indata);
345 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_int(outdata, &ret)) {
346 ERR("ctsvc_ipc_unmarshal_int() Fail");
347 pims_ipc_data_destroy(outdata);
348 return CONTACTS_ERROR_IPC;
351 if (CONTACTS_ERROR_NONE == ret && result) {
352 if (CONTACTS_ERROR_NONE != ctsvc_ipc_unmarshal_bool(outdata, result)) {
353 ERR("ctsvc_ipc_unmarshal_bool() Fail");
354 pims_ipc_data_destroy(outdata);
355 return CONTACTS_ERROR_IPC;
358 pims_ipc_data_destroy(outdata);
364 int ctsvc_ipc_set_disconnected_cb(pims_ipc_h ipc, void (*cb)(void *), void *user_data)
366 return pims_ipc_add_server_disconnected_cb(ipc, cb, user_data);
369 int ctsvc_ipc_unset_disconnected_cb(pims_ipc_h ipc)
371 return pims_ipc_remove_server_disconnected_cb(ipc);
374 void ctsvc_ipc_set_disconnected(bool is_disconnected)
376 pthread_mutex_lock(&_ctsvc_mutex_disconnected);
377 _ctsvc_ipc_disconnected = is_disconnected;
378 pthread_mutex_unlock(&_ctsvc_mutex_disconnected);
381 int ctsvc_ipc_get_disconnected()
383 pthread_mutex_lock(&_ctsvc_mutex_disconnected);
384 DBG("_ctsvc_ipc_disconnected=%d", _ctsvc_ipc_disconnected);
385 pthread_mutex_unlock(&_ctsvc_mutex_disconnected);
386 return _ctsvc_ipc_disconnected;
389 static void _ctsvc_ipc_recovery_foreach_cb(gpointer key, gpointer value,
393 struct ctsvc_ipc_s *ipc_data = value;
395 ctsvc_ipc_unset_disconnected_cb(ipc_data->ipc);
397 int ret = _ctsvc_ipc_create(&(ipc_data->ipc));
398 RETM_IF(CONTACTS_ERROR_NONE != ret, "_ctsvc_ipc_create() Fail(%d)", ret);
400 ctsvc_ipc_set_disconnected_cb(ipc_data->ipc, _ctsvc_ipc_disconnected_cb, NULL);
402 for (c = ipc_data->list_handle; c; c = c->next) {
403 contacts_h contact = c->data;
404 ret = _ctsvc_ipc_connect(contact, ipc_data->ipc);
405 WARN_IF(CONTACTS_ERROR_NONE != ret, "_ctsvc_ipc_connect() Fail(%d)", ret);
409 void ctsvc_ipc_recovery()
412 g_hash_table_foreach(_ctsvc_ipc_table, _ctsvc_ipc_recovery_foreach_cb, NULL);