process as warning if cynara session is NULL
[platform/core/pim/pims-ipc.git] / src / pims-ipc-worker.c
index d6a06be..657382f 100644 (file)
@@ -47,6 +47,8 @@ static __thread pims_ipc_svc_client_disconnected_cb_t _client_disconnected_cb =
 
 static int unique_sequence_number;
 static GHashTable *worker_client_info_map; /* key : worker_id, data : pims_ipc_client_info_s* */
+static int client_register_info(pims_ipc_worker_data_s *worker_data, int client_pid);
+static pthread_mutex_t _worker_client_mutex = PTHREAD_MUTEX_INITIALIZER; /* for worker_client_info_map */
 
 int worker_wait_idle_worker_ready(pims_ipc_worker_data_s *worker_data)
 {
@@ -181,7 +183,13 @@ static gboolean worker_pop_raw_data(pims_ipc_worker_data_s *worker,
                return FALSE;
        }
 
-       *data = g_list_first(worker->list)->data;
+       GList *cursor = g_list_first(worker->list);
+       if (NULL == cursor) {
+               pthread_mutex_unlock(&worker->queue_mutex);
+               *data = NULL;
+               return FALSE;
+       }
+       *data = cursor->data;
        worker->list = g_list_delete_link(worker->list, g_list_first(worker->list));
        pthread_mutex_unlock(&worker->queue_mutex);
 
@@ -193,7 +201,6 @@ int worker_set_callback(char *call_id, pims_ipc_svc_cb_s *cb_data)
        return g_hash_table_insert(worker_cb_table, call_id, cb_data);
 }
 
-
 static void __run_callback(int client_pid, char *call_id, pims_ipc_data_h dhandle_in,
                pims_ipc_data_h *dhandle_out)
 {
@@ -336,7 +343,7 @@ static int __worker_loop_handle_raw_data(pims_ipc_worker_data_s *worker_data)
        int client_pid = _get_pid_from_fd(worker_data->client_fd);
 
        if (UTILS_STR_EQUAL == strcmp(PIMS_IPC_CALL_ID_CREATE, raw_data->call_id)) {
-               client_register_info(worker_data->client_fd, client_pid);
+               client_register_info(worker_data, client_pid);
 
        } else if (UTILS_STR_EQUAL == strcmp(PIMS_IPC_CALL_ID_DESTROY, raw_data->call_id)) {
                disconnected = TRUE;
@@ -418,10 +425,10 @@ static void* __worker_loop(void *data)
        int flag = fcntl(worker_data->client_fd, F_GETFL, 0);
        if (0 == (FD_CLOEXEC & flag)) {
                int client_pid = _get_pid_from_fd(worker_data->client_fd);
-               /*      pthread_mutex_lock(&worker_data->client_mutex); */
+               pthread_mutex_lock(&_worker_client_mutex);
                g_hash_table_remove(worker_client_info_map, GINT_TO_POINTER(client_pid));
                DBG("client pid(%u) is removed", client_pid);
-               /*      pthread_mutex_unlock(&worker_data->client_mutex); */
+               pthread_mutex_unlock(&_worker_client_mutex);
        } else {
                DBG("fd(%d) is already closed", worker_data->client_fd);
        }
@@ -449,7 +456,6 @@ void worker_start_idle_worker(pims_ipc_svc_s *ipc_data)
                pthread_mutex_init(&worker_data->queue_mutex, 0);
                pthread_mutex_init(&worker_data->ready_mutex, NULL);
                pthread_cond_init(&worker_data->ready, NULL);
-               /* pthread_mutex_init(&worker_data->client_mutex, 0); */
 
                utils_launch_thread(__worker_loop, worker_data);
                idle_worker_pool = g_list_append(idle_worker_pool, worker_data);
@@ -510,15 +516,29 @@ void client_destroy_info(gpointer p)
 
 void client_init(void)
 {
+       pthread_mutex_init(&_worker_client_mutex, 0);
+
+       pthread_mutex_lock(&_worker_client_mutex);
        unique_sequence_number = 0;
+       if (worker_client_info_map) {
+               ERR("worker_client_info_map already exists");
+               pthread_mutex_unlock(&_worker_client_mutex);
+               return;
+       }
+
        worker_client_info_map = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
                        client_destroy_info);
+       pthread_mutex_unlock(&_worker_client_mutex);
 }
 
 void client_deinit(void)
 {
        DBG("----------destroied");
+       pthread_mutex_lock(&_worker_client_mutex);
        g_hash_table_destroy(worker_client_info_map);
+       worker_client_info_map = NULL;
+       pthread_mutex_unlock(&_worker_client_mutex);
+       pthread_mutex_destroy(&_worker_client_mutex);
 }
 
 static int _create_client_info(int fd, pims_ipc_client_info_s **p_client_info)
@@ -559,27 +579,28 @@ static int _create_client_info(int fd, pims_ipc_client_info_s **p_client_info)
 
        client_info->client_session = cynara_session_from_pid(pid);
        if (NULL == client_info->client_session) {
-               ERR("cynara_session_from_pid() return NULL");
-               client_destroy_info(client_info);
-               return -1;
+               WARN("cynara_session_from_pid() return NULL");
+               client_info->client_session = strdup("");
        }
        *p_client_info = client_info;
 
        return 0;
 }
 
-int client_register_info(int client_fd, int client_pid)
+static int client_register_info(pims_ipc_worker_data_s *worker_data, int client_pid)
 {
        pims_ipc_client_info_s *client_info = NULL;
        int ret = 0;
 
-       ret = _create_client_info(client_fd, &client_info);
+       ret = _create_client_info(worker_data->client_fd, &client_info);
        if (ret < 0) {
                ERR("_create_client_info() Fail(%d)", ret);
                return -1;
        }
+       pthread_mutex_lock(&_worker_client_mutex);
        g_hash_table_insert(worker_client_info_map, GINT_TO_POINTER(client_pid), client_info);
        DBG("-------inserted:pid(%d), info(%p)", client_pid, client_info);
+       pthread_mutex_unlock(&_worker_client_mutex);
 
        return 0;
 }
@@ -635,9 +656,13 @@ pims_ipc_client_info_s* client_clone_info(pims_ipc_client_info_s *client_info)
 pims_ipc_client_info_s* client_get_info(int client_pid)
 {
        pims_ipc_client_info_s *client_info = NULL;
+       pims_ipc_client_info_s *client_info_clone = NULL;
 
+       pthread_mutex_lock(&_worker_client_mutex);
        client_info = g_hash_table_lookup(worker_client_info_map, GINT_TO_POINTER(client_pid));
-       DBG("---------------get client_pid(%d)", client_pid);
-       return client_info;
+       client_info_clone = client_clone_info(client_info);
+       pthread_mutex_unlock(&_worker_client_mutex);
+       DBG("Get client_info(%p) from pid(%d)", client_info, client_pid);
+       return client_info_clone;
 }