4 * Copyright (c) 2012 - 2016 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.
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/eventfd.h>
29 #include "pims-internal.h"
30 #include "pims-ipc-data-internal.h"
31 #include "pims-ipc-data.h"
32 #include "pims-socket.h"
33 #include "pims-ipc-utils.h"
34 #include "pims-ipc-worker.h"
36 #define PIMS_IPC_WORKER_THREAD_WAIT_TIME 100 /* milliseconds */
39 pims_ipc_svc_client_disconnected_cb callback;
41 } pims_ipc_svc_client_disconnected_cb_t;
43 /* idle_worker_pool SHOULD handle on main thread */
44 static GList *idle_worker_pool;
45 static GHashTable *worker_cb_table; /* call_id, cb_data */
46 static __thread pims_ipc_svc_client_disconnected_cb_t _client_disconnected_cb = {NULL, NULL};
48 static int unique_sequence_number;
49 static GHashTable *worker_client_info_map; /* key : worker_id, data : pims_ipc_client_info_s* */
50 static int client_register_info(pims_ipc_worker_data_s *worker_data, int client_pid);
51 static pthread_mutex_t _worker_client_mutex = PTHREAD_MUTEX_INITIALIZER; /* for worker_client_info_map */
53 int worker_wait_idle_worker_ready(pims_ipc_worker_data_s *worker_data)
55 struct timespec timeout = {0};
57 clock_gettime(CLOCK_REALTIME, &timeout);
58 timeout.tv_nsec += PIMS_IPC_WORKER_THREAD_WAIT_TIME * 1000000;
59 timeout.tv_sec += timeout.tv_nsec / 1000000000L;
60 timeout.tv_nsec = timeout.tv_nsec % 1000000000L;
62 pthread_mutex_lock(&worker_data->ready_mutex);
64 if (!worker_data->fd) {
65 WARN("worker fd is null, wait until worker thread create done.");
66 if (pthread_cond_timedwait(&worker_data->ready, &worker_data->ready_mutex, &timeout)) {
67 ERR("Get idle worker timeout Fail!");
68 pthread_mutex_unlock(&worker_data->ready_mutex);
73 pthread_mutex_unlock(&worker_data->ready_mutex);
77 pims_ipc_worker_data_s* worker_get_idle_worker(pims_ipc_svc_s *ipc_svc,
78 const char *client_id)
80 pims_ipc_worker_data_s *worker_data;
82 RETV_IF(NULL == client_id, NULL);
83 RETVM_IF(NULL == idle_worker_pool, NULL, "There is no idle worker");
85 worker_data = g_hash_table_lookup(ipc_svc->client_worker_map, client_id);
89 worker_data = idle_worker_pool->data;
91 idle_worker_pool = g_list_delete_link(idle_worker_pool, idle_worker_pool);
94 g_hash_table_insert(ipc_svc->client_worker_map, g_strdup(client_id), worker_data);
99 pims_ipc_worker_data_s* worker_find(pims_ipc_svc_s *ipc_svc, const char *client_id)
101 pims_ipc_worker_data_s *worker_data;
103 RETV_IF(NULL == client_id, NULL);
105 if (FALSE == g_hash_table_lookup_extended(ipc_svc->client_worker_map, client_id,
106 NULL, (gpointer*)&worker_data)) {
107 ERR("g_hash_table_lookup_extended(%s) Fail", client_id);
114 void worker_stop_client_worker(pims_ipc_svc_s *ipc_svc, const char *client_id)
116 pims_ipc_worker_data_s *worker_data;
118 worker_data = worker_find(ipc_svc, client_id);
120 /* remove client_fd */
121 g_hash_table_remove(ipc_svc->client_worker_map, client_id);
123 /* stop worker thread */
125 worker_data->stop_thread = TRUE;
126 worker_data->client_fd = -1;
127 write_command(worker_data->fd, 1);
128 DBG("write command to worker terminate(worker_fd:%d)", worker_data->fd);
133 void worker_free_raw_data(void *data)
135 pims_ipc_raw_data_s *raw_data = data;
137 if (NULL == raw_data)
140 free(raw_data->client_id);
141 free(raw_data->call_id);
142 free(raw_data->data);
146 void worker_free_data(gpointer data)
148 pims_ipc_worker_data_s *worker_data = data;
150 pthread_mutex_lock(&worker_data->queue_mutex);
151 if (worker_data->list)
152 g_list_free_full(worker_data->list, worker_free_raw_data);
153 pthread_mutex_unlock(&worker_data->queue_mutex);
155 pthread_cond_destroy(&worker_data->ready);
156 pthread_mutex_destroy(&worker_data->ready_mutex);
162 int worker_push_raw_data(pims_ipc_worker_data_s *worker_data, int client_fd,
163 pims_ipc_raw_data_s *data)
165 pthread_mutex_lock(&worker_data->queue_mutex);
166 worker_data->list = g_list_append(worker_data->list, data);
167 worker_data->client_fd = client_fd;
168 pthread_mutex_unlock(&worker_data->queue_mutex);
173 static gboolean worker_pop_raw_data(pims_ipc_worker_data_s *worker,
174 pims_ipc_raw_data_s **data)
179 pthread_mutex_lock(&worker->queue_mutex);
181 pthread_mutex_unlock(&worker->queue_mutex);
186 *data = g_list_first(worker->list)->data;
187 worker->list = g_list_delete_link(worker->list, g_list_first(worker->list));
188 pthread_mutex_unlock(&worker->queue_mutex);
193 int worker_set_callback(char *call_id, pims_ipc_svc_cb_s *cb_data)
195 return g_hash_table_insert(worker_cb_table, call_id, cb_data);
198 static void __run_callback(int client_pid, char *call_id, pims_ipc_data_h dhandle_in,
199 pims_ipc_data_h *dhandle_out)
201 pims_ipc_svc_cb_s *cb_data = NULL;
203 VERBOSE("Call id [%s]", call_id);
205 cb_data = g_hash_table_lookup(worker_cb_table, call_id);
206 if (cb_data == NULL) {
207 VERBOSE("No Data for %s", call_id);
211 /* TODO: client_pid is not valide pims_ipc_h */
212 cb_data->callback((pims_ipc_h)client_pid, dhandle_in, dhandle_out, cb_data->user_data);
215 static void __make_raw_data(const char *call_id, int seq_no, pims_ipc_data_h data,
216 pims_ipc_raw_data_s **out)
218 pims_ipc_data_s *data_in = data;
219 pims_ipc_raw_data_s *raw_data = NULL;
222 RET_IF(NULL == call_id);
224 raw_data = calloc(1, sizeof(pims_ipc_raw_data_s));
225 if (NULL == raw_data) {
226 ERR("calloc() Fail(%d)", errno);
230 raw_data->call_id = g_strdup(call_id);
231 raw_data->call_id_len = strlen(raw_data->call_id);
232 raw_data->seq_no = seq_no;
234 if (data_in && 0 < data_in->buf_size) {
235 raw_data->has_data = TRUE;
236 raw_data->data = calloc(1, data_in->buf_size+1);
237 if (NULL == raw_data->data) {
238 ERR("calloc() Fail");
239 free(raw_data->call_id);
243 memcpy(raw_data->data, data_in->buf, data_in->buf_size);
244 raw_data->data_len = data_in->buf_size;
246 raw_data->has_data = FALSE;
247 raw_data->data_len = 0;
248 raw_data->data = NULL;
254 static int __send_raw_data(int fd, const char *client_id, pims_ipc_raw_data_s *data)
257 unsigned int len, total_len, client_id_len;
259 RETV_IF(NULL == data, -1);
260 RETV_IF(NULL == client_id, -1);
262 client_id_len = strlen(client_id);
264 len = sizeof(total_len) + sizeof(client_id_len) + client_id_len + sizeof(data->seq_no)
265 + data->call_id_len + sizeof(data->call_id) + sizeof(data->has_data);
268 if (data->has_data) {
269 len += sizeof(data->data_len);
270 total_len = len + data->data_len;
273 INFO("client_id: %s, call_id : %s, seq no :%d, len:%d, total len :%d", client_id,
274 data->call_id, data->seq_no, len, total_len);
278 memset(buf, 0x0, len+1);
280 memcpy(buf, &total_len, sizeof(total_len));
281 length += sizeof(total_len);
283 memcpy(buf+length, &client_id_len, sizeof(client_id_len));
284 length += sizeof(client_id_len);
285 memcpy(buf+length, client_id, client_id_len);
286 length += client_id_len;
288 memcpy(buf+length, &(data->seq_no), sizeof(data->seq_no));
289 length += sizeof(data->seq_no);
291 memcpy(buf+length, &(data->call_id_len), sizeof(data->call_id_len));
292 length += sizeof(data->call_id_len);
293 memcpy(buf+length, data->call_id, data->call_id_len);
294 length += data->call_id_len;
296 memcpy(buf+length, &(data->has_data), sizeof(data->has_data));
297 length += sizeof(data->has_data);
299 if (data->has_data) {
300 memcpy(buf+length, &(data->data_len), sizeof(data->data_len));
301 length += sizeof(data->data_len);
302 ret = socket_send(fd, buf, length);
306 ret += socket_send_data(fd, data->data, data->data_len);
308 ret = socket_send(fd, buf, length);
314 static int _get_pid_from_fd(int fd)
317 socklen_t uc_len = sizeof(uc);
319 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &uc, &uc_len) < 0)
320 ERR("getsockopt() Failed(%d)", errno);
322 DBG("Client PID(%d)", uc.pid);
326 static int __worker_loop_handle_raw_data(pims_ipc_worker_data_s *worker_data)
328 int disconnected = FALSE;
329 pims_ipc_data_h data_in = NULL;
330 pims_ipc_data_h data_out = NULL;
331 pims_ipc_raw_data_s *result = NULL;
332 pims_ipc_raw_data_s *raw_data = NULL;
334 if (FALSE == worker_pop_raw_data(worker_data, &raw_data))
337 int client_pid = _get_pid_from_fd(worker_data->client_fd);
339 if (UTILS_STR_EQUAL == strcmp(PIMS_IPC_CALL_ID_CREATE, raw_data->call_id)) {
340 client_register_info(worker_data, client_pid);
342 } else if (UTILS_STR_EQUAL == strcmp(PIMS_IPC_CALL_ID_DESTROY, raw_data->call_id)) {
345 data_in = pims_ipc_data_steal_unmarshal(raw_data->data, raw_data->data_len);
347 __run_callback(client_pid, raw_data->call_id, data_in, &data_out);
348 pims_ipc_data_destroy(data_in);
352 __make_raw_data(raw_data->call_id, raw_data->seq_no, data_out, &result);
353 pims_ipc_data_destroy(data_out);
355 __make_raw_data(raw_data->call_id, raw_data->seq_no, NULL, &result);
357 if (worker_data->client_fd != -1)
358 __send_raw_data(worker_data->client_fd, raw_data->client_id, result);
359 worker_free_raw_data(raw_data);
360 worker_free_raw_data(result);
365 static void* __worker_loop(void *data)
370 int disconnected = FALSE;
371 pims_ipc_worker_data_s *worker_data = data;
373 RETV_IF(NULL == data, NULL);
375 worker_fd = eventfd(0, 0);
379 INFO("worker Created ********** worker_fd = %d ***********", worker_fd);
381 pid = pthread_self();
382 worker_data->client_fd = -1;
383 worker_data->stop_thread = FALSE;
384 pthread_mutex_lock(&worker_data->ready_mutex);
385 worker_data->fd = worker_fd;
386 pthread_cond_signal(&worker_data->ready);
387 pthread_mutex_unlock(&worker_data->ready_mutex);
389 struct pollfd pollfds[1];
390 pollfds[0].fd = worker_fd;
391 pollfds[0].events = POLLIN;
392 pollfds[0].revents = 0;
394 while (!worker_data->stop_thread) {
395 ret = poll(pollfds, 1, 3000); /* waiting command from router */
398 ERR("poll() Fail(%d)", errno);
401 if (worker_data->stop_thread)
407 if (pollfds[0].revents & POLLIN) {
409 read_command(pollfds[0].fd, &dummy);
411 disconnected = __worker_loop_handle_raw_data(worker_data);
416 ERR("client fd closed, worker_fd : %d", worker_fd);
417 INFO("task thread terminated --------------------------- (worker_fd : %d)", worker_fd);
419 int flag = fcntl(worker_data->client_fd, F_GETFL, 0);
420 if (0 == (FD_CLOEXEC & flag)) {
421 int client_pid = _get_pid_from_fd(worker_data->client_fd);
422 pthread_mutex_lock(&_worker_client_mutex);
423 g_hash_table_remove(worker_client_info_map, GINT_TO_POINTER(client_pid));
424 DBG("client pid(%u) is removed", client_pid);
425 pthread_mutex_unlock(&_worker_client_mutex);
427 DBG("fd(%d) is already closed", worker_data->client_fd);
430 worker_free_data(worker_data);
433 if (_client_disconnected_cb.callback)
434 _client_disconnected_cb.callback((pims_ipc_h)pid, _client_disconnected_cb.user_data);
439 void worker_start_idle_worker(pims_ipc_svc_s *ipc_data)
442 pims_ipc_worker_data_s *worker_data;
444 for (i = g_list_length(idle_worker_pool); i < ipc_data->workers_max_count; i++) {
445 worker_data = calloc(1, sizeof(pims_ipc_worker_data_s));
446 if (NULL == worker_data) {
447 ERR("calloc() Fail(%d)", errno);
450 pthread_mutex_init(&worker_data->queue_mutex, 0);
451 pthread_mutex_init(&worker_data->ready_mutex, NULL);
452 pthread_cond_init(&worker_data->ready, NULL);
454 utils_launch_thread(__worker_loop, worker_data);
455 idle_worker_pool = g_list_append(idle_worker_pool, worker_data);
459 void worker_stop_idle_worker()
462 pims_ipc_worker_data_s *worker_data;
464 cursor = idle_worker_pool;
466 worker_data = cursor->data;
467 worker_data->stop_thread = TRUE;
468 write_command(worker_data->fd, 1);
469 cursor = cursor->next;
475 worker_cb_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
476 WARN_IF(NULL == worker_cb_table, "worker cb table is NULL");
481 g_list_free(idle_worker_pool);
482 idle_worker_pool = NULL;
484 g_hash_table_destroy(worker_cb_table);
485 worker_cb_table = NULL;
488 API void pims_ipc_svc_set_client_disconnected_cb(
489 pims_ipc_svc_client_disconnected_cb callback, void *user_data)
491 if (_client_disconnected_cb.callback) {
492 ERR("already registered");
495 _client_disconnected_cb.callback = callback;
496 _client_disconnected_cb.user_data = user_data;
499 void client_destroy_info(gpointer p)
501 pims_ipc_client_info_s *client_info = p;
503 if (NULL == client_info)
505 free(client_info->smack);
506 free(client_info->uid);
507 free(client_info->client_session);
511 void client_init(void)
513 pthread_mutex_init(&_worker_client_mutex, 0);
515 pthread_mutex_lock(&_worker_client_mutex);
516 unique_sequence_number = 0;
517 if (worker_client_info_map) {
518 ERR("worker_client_info_map already exists");
519 pthread_mutex_unlock(&_worker_client_mutex);
523 worker_client_info_map = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
524 client_destroy_info);
525 pthread_mutex_unlock(&_worker_client_mutex);
528 void client_deinit(void)
530 DBG("----------destroied");
531 pthread_mutex_lock(&_worker_client_mutex);
532 g_hash_table_destroy(worker_client_info_map);
533 worker_client_info_map = NULL;
534 pthread_mutex_unlock(&_worker_client_mutex);
535 pthread_mutex_destroy(&_worker_client_mutex);
538 static int _create_client_info(int fd, pims_ipc_client_info_s **p_client_info)
542 char errmsg[1024] = {0};
544 pims_ipc_client_info_s *client_info = calloc(1, sizeof(pims_ipc_client_info_s));
545 if (NULL == client_info) {
546 ERR("calloc() return NULL");
550 ret = cynara_creds_socket_get_client(fd, CLIENT_METHOD_SMACK, &(client_info->smack));
551 if (CYNARA_API_SUCCESS != ret) {
552 cynara_strerror(ret, errmsg, sizeof(errmsg));
553 ERR("cynara_creds_socket_get_client() Fail(%d,%s)", ret, errmsg);
554 client_destroy_info(client_info);
558 ret = cynara_creds_socket_get_user(fd, USER_METHOD_UID, &(client_info->uid));
559 if (CYNARA_API_SUCCESS != ret) {
560 cynara_strerror(ret, errmsg, sizeof(errmsg));
561 ERR("cynara_creds_socket_get_user() Fail(%d,%s)", ret, errmsg);
562 client_destroy_info(client_info);
566 ret = cynara_creds_socket_get_pid(fd, &pid);
567 if (CYNARA_API_SUCCESS != ret) {
568 cynara_strerror(ret, errmsg, sizeof(errmsg));
569 ERR("cynara_creds_socket_get_pid() Fail(%d,%s)", ret, errmsg);
570 client_destroy_info(client_info);
574 client_info->client_session = cynara_session_from_pid(pid);
575 if (NULL == client_info->client_session) {
576 ERR("cynara_session_from_pid() return NULL");
577 client_destroy_info(client_info);
580 *p_client_info = client_info;
585 static int client_register_info(pims_ipc_worker_data_s *worker_data, int client_pid)
587 pims_ipc_client_info_s *client_info = NULL;
590 ret = _create_client_info(worker_data->client_fd, &client_info);
592 ERR("_create_client_info() Fail(%d)", ret);
595 pthread_mutex_lock(&_worker_client_mutex);
596 g_hash_table_insert(worker_client_info_map, GINT_TO_POINTER(client_pid), client_info);
597 DBG("-------inserted:pid(%d), info(%p)", client_pid, client_info);
598 pthread_mutex_unlock(&_worker_client_mutex);
603 int client_get_unique_sequence_number(void)
605 return unique_sequence_number++;
608 pims_ipc_client_info_s* client_clone_info(pims_ipc_client_info_s *client_info)
610 if (NULL == client_info) {
611 ERR("client_info is NULL");
615 pims_ipc_client_info_s *clone = calloc(1, sizeof(pims_ipc_client_info_s));
617 ERR("calloc() Fail");
621 if (client_info->smack) {
622 clone->smack = strdup(client_info->smack);
623 if (NULL == clone->smack) {
624 ERR("strdup() Fail");
625 client_destroy_info(clone);
630 if (client_info->uid) {
631 clone->uid = strdup(client_info->uid);
632 if (NULL == clone->uid) {
633 ERR("strdup() Fail");
634 client_destroy_info(clone);
639 if (client_info->client_session) {
640 clone->client_session = strdup(client_info->client_session);
641 if (NULL == clone->client_session) {
642 ERR("strdup() Fail");
643 client_destroy_info(clone);
651 pims_ipc_client_info_s* client_get_info(int client_pid)
653 pims_ipc_client_info_s *client_info = NULL;
655 pthread_mutex_lock(&_worker_client_mutex);
656 client_info = g_hash_table_lookup(worker_client_info_map, GINT_TO_POINTER(client_pid));
657 pthread_mutex_unlock(&_worker_client_mutex);
658 DBG("Get client_info(%p) from pid(%d)", client_info, client_pid);