on demand launch server for APIs 04/47504/1
authorGukhwan Cho <gh78.cho@samsung.com>
Wed, 24 Jun 2015 08:55:11 +0000 (17:55 +0900)
committerGukhwan Cho <gh78.cho@samsung.com>
Fri, 4 Sep 2015 04:25:08 +0000 (13:25 +0900)
Change-Id: I843d4bda5eb39fb5effdfce4aba14da3143971f0
Signed-off-by: Gukhwan Cho <gh78.cho@samsung.com>
12 files changed:
client/ctsvc_client_ipc.c
client/ctsvc_client_ipc.h
client/ctsvc_client_service_helper.c
common/ctsvc_mutex.c
common/ctsvc_mutex.h
packaging/contacts-service.service
server/ctsvc_ipc_server.c
server/ctsvc_server.c
server/ctsvc_server.h [new file with mode: 0644]
server/ctsvc_server_bg.c
server/ctsvc_server_utils.c
server/ctsvc_server_utils.h

index e42bed2..4642e81 100644 (file)
@@ -255,12 +255,18 @@ static void __ctsvc_ipc_unlock(void)
 
 int ctsvc_ipc_call(char *module, char *function, pims_ipc_h data_in, pims_ipc_data_h *data_out)
 {
-       pims_ipc_h ipc_handle = _ctsvc_get_ipc_handle();
+       pims_ipc_h ipc_handle;
 
-       __ctsvc_ipc_lock();
+       if (true == ctsvc_ipc_get_disconnected()) {
+               ctsvc_ipc_recovery();
+               ctsvc_ipc_recover_for_change_subscription();
+               ctsvc_ipc_set_disconnected(false);
+       }
 
-       int ret = pims_ipc_call(ipc_handle, module, function, data_in, data_out);
+       ipc_handle = _ctsvc_get_ipc_handle();
 
+       __ctsvc_ipc_lock();
+       int ret = pims_ipc_call(ipc_handle, module, function, data_in, data_out);
        __ctsvc_ipc_unlock();
 
        return ret;
@@ -351,6 +357,12 @@ void ctsvc_ipc_set_disconnected(bool is_disconnected)
        _ctsvc_ipc_disconnected = is_disconnected;
 }
 
+int ctsvc_ipc_get_disconnected()
+{
+       CTS_DBG("_ctsvc_ipc_disconnected=%d", _ctsvc_ipc_disconnected);
+       return _ctsvc_ipc_disconnected;
+}
+
 static void _ctsvc_ipc_recovery_foreach_cb(gpointer key, gpointer value, gpointer user_data)
 {
        GList *c;
@@ -368,11 +380,7 @@ static void _ctsvc_ipc_recovery_foreach_cb(gpointer key, gpointer value, gpointe
 
 void ctsvc_ipc_recovery()
 {
-       CTS_DBG("ctsvc_ipc_recovery (_ctsvc_ipc_disconnected=%d)", _ctsvc_ipc_disconnected);
-
-       if (false == _ctsvc_ipc_disconnected)
-               return;
-
+       CTS_FN_CALL;
        g_hash_table_foreach(_ctsvc_ipc_table, _ctsvc_ipc_recovery_foreach_cb, NULL);
 }
 
index 2550151..af0919a 100644 (file)
@@ -48,6 +48,7 @@ int ctsvc_ipc_client_check_permission(int permission, bool *result);
 int ctsvc_ipc_set_disconnected_cb(void (*cb)(void *), void *user_data);
 int ctsvc_ipc_unset_disconnected_cb();
 void ctsvc_ipc_set_disconnected(bool is_disconnected);
+int ctsvc_ipc_get_disconnected();
 void ctsvc_ipc_recovery();
 
 #endif /* __CTSVC_CLIENT_IPC_H__ */
index 529846b..96b742d 100644 (file)
@@ -83,9 +83,12 @@ static void _ctsvc_ipc_disconnected_cb(void *user_data)
 
 static void _ctsvc_ipc_initialized_cb(void *user_data)
 {
-       ctsvc_ipc_recovery();
-       ctsvc_ipc_recover_for_change_subscription();
-       ctsvc_ipc_set_disconnected(false);
+       CTS_FN_CALL;
+       if (true == ctsvc_ipc_get_disconnected()) {
+               ctsvc_ipc_recovery();
+               ctsvc_ipc_recover_for_change_subscription();
+               ctsvc_ipc_set_disconnected(false);
+       }
 }
 
 int ctsvc_client_connect(contacts_h contact)
index 14ab422..124640e 100644 (file)
@@ -41,6 +41,7 @@ static pthread_mutex_t ipc_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t ipc_pubsub_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t access_control_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t handle_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t timeout_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t socket_client_info_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t cynara_mutex = PTHREAD_MUTEX_INITIALIZER;
 
@@ -70,6 +71,9 @@ static inline pthread_mutex_t* __ctsvc_mutex_get_mutex(int type)
        case CTS_MUTEX_HANDLE:
                ret_val = &handle_mutex;
                break;
+       case CTS_MUTEX_TIMEOUT:
+               ret_val = &timeout_mutex;
+               break;
        case CTS_MUTEX_SOCKET_CLIENT_INFO:
                ret_val = &socket_client_info_mutex;
                break;
index ea951d5..7364f62 100644 (file)
@@ -29,6 +29,7 @@ enum {
        CTS_MUTEX_PIMS_IPC_PUBSUB,
        CTS_MUTEX_ACCESS_CONTROL,
        CTS_MUTEX_HANDLE,
+       CTS_MUTEX_TIMEOUT,
        CTS_MUTEX_CYNARA,
        CTS_MUTEX_SOCKET_CLIENT_INFO,
 };
index a42deaa..a106fe3 100644 (file)
@@ -3,7 +3,8 @@ Description=Start the Contacts service helper service
 
 [Service]
 Type=simple
-ExecStart=/usr/bin/contacts-service-ipcd
+Environment="CMD=timeout" "TIME=0"
+ExecStart=/usr/bin/contacts-service-ipcd $CMD $TIME
 
 [Install]
 WantedBy=default.target
index 790a9d3..98c8b43 100644 (file)
@@ -120,6 +120,7 @@ ERROR_RETURN:
 DATA_FREE:
        ctsvc_handle_destroy(contact);
        ctsvc_server_trim_memory();
+       ctsvc_server_timeout();
 }
 
 void ctsvc_ipc_server_check_permission(pims_ipc_h ipc, pims_ipc_data_h indata,
@@ -165,6 +166,7 @@ ERROR_RETURN:
                        return;
                }
        }
+       ctsvc_server_timeout();
 }
 
 void ctsvc_ipc_server_db_insert_record(pims_ipc_h ipc, pims_ipc_data_h indata, pims_ipc_data_h *outdata, void *userdata)
@@ -257,6 +259,7 @@ DATA_FREE:
        ctsvc_handle_destroy(contact);
        contacts_record_destroy(record,true);
        ctsvc_server_trim_memory();
+       ctsvc_server_timeout();
        return;
 }
 
@@ -331,6 +334,7 @@ DATA_FREE:
        ctsvc_handle_destroy(contact);
        contacts_record_destroy(record,true);
        CONTACTS_FREE(view_uri);
+       ctsvc_server_timeout();
        return;
 }
 
@@ -413,6 +417,7 @@ DATA_FREE:
        ctsvc_handle_destroy(contact);
        contacts_record_destroy(record,true);
        ctsvc_server_trim_memory();
+       ctsvc_server_timeout();
        return;
 }
 
@@ -502,6 +507,7 @@ DATA_FREE:
 
        ctsvc_handle_destroy(contact);
        CONTACTS_FREE(view_uri);
+       ctsvc_server_timeout();
        return;
 }
 
@@ -597,7 +603,7 @@ ERROR_RETURN:
 DATA_FREE:
        ctsvc_handle_destroy(contact);
        contacts_record_destroy(record, true);
-
+       ctsvc_server_timeout();
        return;
 }
 
@@ -699,6 +705,7 @@ DATA_FREE:
        ctsvc_handle_destroy(contact);
        contacts_list_destroy(list,true);
        CONTACTS_FREE(view_uri);
+       ctsvc_server_timeout();
        return;
 }
 
@@ -797,6 +804,7 @@ DATA_FREE:
        ctsvc_handle_destroy(contact);
        contacts_list_destroy(list,true);
        contacts_query_destroy(query);
+       ctsvc_server_timeout();
        return;
 }
 
@@ -883,6 +891,7 @@ ERROR_RETURN:
 DATA_FREE:
        ctsvc_handle_destroy(contact);
        CONTACTS_FREE(view_uri);
+       ctsvc_server_timeout();
        return;
 }
 
@@ -968,6 +977,7 @@ ERROR_RETURN:
 DATA_FREE:
        ctsvc_handle_destroy(contact);
        contacts_query_destroy(query);
+       ctsvc_server_timeout();
        return;
 }
 
@@ -1089,6 +1099,7 @@ DATA_FREE:
        ctsvc_handle_destroy(contact);
        contacts_list_destroy(list,true);
        CONTACTS_FREE(ids);
+       ctsvc_server_timeout();
        return;
 }
 
@@ -1186,6 +1197,7 @@ ERROR_RETURN:
 DATA_FREE:
        ctsvc_handle_destroy(contact);
        contacts_list_destroy(list,true);
+       ctsvc_server_timeout();
        return;
 }
 
@@ -1291,6 +1303,7 @@ DATA_FREE:
        ctsvc_handle_destroy(contact);
        CONTACTS_FREE(uri);
        CONTACTS_FREE(ids);
+       ctsvc_server_timeout();
        return;
 }
 
@@ -1411,6 +1424,7 @@ DATA_FREE:
        ctsvc_handle_destroy(contact);
        contacts_list_destroy(list,true);
        CONTACTS_FREE(ids);
+       ctsvc_server_timeout();
        return;
 }
 
@@ -1520,6 +1534,7 @@ DATA_FREE:
        ctsvc_handle_destroy(contact);
        contacts_list_destroy(record_list,true);
        CONTACTS_FREE(view_uri);
+       ctsvc_server_timeout();
        return;
 }
 
@@ -1576,6 +1591,7 @@ ERROR_RETURN:
 
 DATA_FREE:
        ctsvc_handle_destroy(contact);
+       ctsvc_server_timeout();
        return;
 }
 
@@ -1680,6 +1696,7 @@ DATA_FREE:
        contacts_list_destroy(list,true);
        CONTACTS_FREE(view_uri);
        CONTACTS_FREE(keyword);
+       ctsvc_server_timeout();
        return;
 }
 
@@ -1791,6 +1808,7 @@ DATA_FREE:
        contacts_list_destroy(list,true);
        free(view_uri);
        free(keyword);
+       ctsvc_server_timeout();
        return;
 }
 
@@ -1897,6 +1915,7 @@ DATA_FREE:
        contacts_list_destroy(list,true);
        contacts_query_destroy(query);
        CONTACTS_FREE(keyword);
+       ctsvc_server_timeout();
        return;
 }
 
@@ -1944,6 +1963,7 @@ ERROR_RETURN:
        }
 DATA_FREE:
        ctsvc_handle_destroy(contact);
+       ctsvc_server_timeout();
        return;
 }
 
index 976e45b..5ed13be 100644 (file)
 #include "ctsvc_ipc_server2.h"
 #include "ctsvc_notify.h"
 
+#define CTSVC_TIMEOUT_FOR_DEFAULT 90
+
+static int ctsvc_list_count = 0;
+static int ctsvc_timeout_sec = CTSVC_TIMEOUT_FOR_DEFAULT;
+static GMainLoop *main_loop = NULL;
+
 static int __server_main(void)
 {
        int ret;
@@ -124,7 +130,6 @@ static int __server_main(void)
                ret = ctsvc_server_init_configuration();
                CTS_DBG("%d", ret);
 
-               GMainLoop* main_loop;
                main_loop = g_main_loop_new(NULL, FALSE);
 
                pims_ipc_svc_run_main_loop(main_loop);
@@ -151,6 +156,18 @@ static int __server_main(void)
        return -1;
 }
 
+void ctsvc_server_quit(void)
+{
+       g_main_loop_quit(main_loop);
+       main_loop = NULL;
+}
+
+int ctsvc_server_get_timeout_sec(void)
+{
+       CTS_DBG("ctsvc_timeout_sec:%d", ctsvc_timeout_sec);
+       return ctsvc_timeout_sec;
+}
+
 #define CTSVC_SECURITY_FILE_GROUP 6005
 void ctsvc_create_file_set_permission(const char* file, mode_t mode)
 {
@@ -176,7 +193,6 @@ void ctsvc_create_rep_set_permission(const char* directory, mode_t mode)
        }
 }
 
-
 int main(int argc, char *argv[])
 {
        CTS_FN_CALL;
@@ -189,9 +205,10 @@ int main(int argc, char *argv[])
                WARN_IF(ret <0, "setgroups Fail(%d)", ret);
        }
 
+       if (2 <= argc && STRING_EQUAL == strcmp(argv[1], "timeout"))
+               ctsvc_timeout_sec = atoi(argv[2]);
+
        ctsvc_server_check_schema();
-       if (2 <= argc && STRING_EQUAL ==strcmp(argv[1], "schema"))
-               return CONTACTS_ERROR_NONE;
 
        ctsvc_create_rep_set_permission(DATA_REPERTORY, 0755);
        ctsvc_create_rep_set_permission(CTSVC_NOTI_REPERTORY, 0775);
diff --git a/server/ctsvc_server.h b/server/ctsvc_server.h
new file mode 100644 (file)
index 0000000..4e5c3bf
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Contacts Service
+ *
+ * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef __CTSVC_SERVER_H__
+#define __CTSVC_SERVER_H__
+
+void ctsvc_server_quit(void);
+int ctsvc_server_get_timeout_sec(void);
+
+#endif /* __CTSVC_SERVER_H__ */
+
+
index c4521f4..1b693e9 100644 (file)
@@ -411,6 +411,7 @@ static gpointer __ctsvc_server_bg_delete(gpointer user_data)
                while (1) {
                        if (__ctsvc_cpu_is_busy()) { /* sleep 1 sec in function */
                                CTS_ERR("Now CPU is busy.. waiting");
+                               ctsvc_server_timeout();
                                sleep(CTSVC_SERVER_BG_DELETE_STEP_TIME*59); /* sleep 60 sec(1 min) totally */
                                continue;
                        }
index f49957e..4b6e4c0 100644 (file)
 #include "ctsvc_server_sqlite.h"
 #include "ctsvc_localize.h"
 #include "ctsvc_normalize.h"
+#include "ctsvc_mutex.h"
+#include "ctsvc_server.h"
 
 #define CTSVC_FEATURE_TELEPHONY "http://tizen.org/feature/network.telephony"
 
 static int system_language = -1;
 static bool _ctsvc_have_telephony_feature = false;
+static guint _ctsvc_timeout = 0;
 
 int ctsvc_server_load_feature_list(void)
 {
@@ -161,3 +164,24 @@ void ctsvc_server_trim_memory(void)
        malloc_trim(0);
        sqlite3_release_memory(-1);
 }
+
+static gboolean _timeout_cb(gpointer user_data)
+{
+       CTS_FN_CALL;
+       ctsvc_server_quit();
+       return TRUE;
+}
+
+void ctsvc_server_timeout(void)
+{
+       CTS_FN_CALL;
+       int timeout = ctsvc_server_get_timeout_sec();
+       if (timeout < 1)
+               return;
+
+       ctsvc_mutex_lock(CTS_MUTEX_TIMEOUT);
+       if (_ctsvc_timeout > 0)
+               g_source_remove(_ctsvc_timeout);
+       _ctsvc_timeout = g_timeout_add_seconds(timeout, _timeout_cb, NULL);
+       ctsvc_mutex_unlock(CTS_MUTEX_TIMEOUT);
+}
index 4d3ba91..c971e33 100644 (file)
@@ -28,6 +28,7 @@ void ctsvc_server_final_configuration(void);
 int ctsvc_server_set_default_sort(int lang);
 
 void ctsvc_server_trim_memory(void);
+void ctsvc_server_timeout(void);
 
 #endif /* __CTSVC_SERVER_UTILS_H__ */