4 * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: YoungHun Kim <yh8004.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #include "muse_core_internal.h"
23 #include "muse_server_private.h"
25 #define MSG_THREAD_NAME "msg"
26 #define DATA_THREAD_NAME "data"
28 #define DATA_WORKER_QDATA_MAX_SIZE (3840 * 2160 * 4) /* UHD BGRA8888 */
29 #define UNLIMITED_INSTANCE -1
31 static void _ms_ipc_data_ch_init(muse_module_h m);
32 static void _ms_ipc_data_ch_deinit(muse_module_h m);
33 static void _ms_ipc_module_data_ch_cleanup(muse_module_h m);
34 static void _ms_ipc_module_msg_ch_cleanup(muse_module_h m);
35 static void _ms_ipc_module_cleanup(muse_module_h m);
36 static gboolean _ms_ipc_module_instance_creation_is_allowed(int module_idx);
37 static gboolean _ms_ipc_get_module_idx(muse_module_h m, void *jobj);
38 static gboolean _ms_ipc_dispatch_create(muse_module_h m, void *jobj);
39 static gboolean _ms_ipc_dispatch_destroy(muse_module_h m);
40 static gboolean _ms_ipc_dispatch_no_instance(muse_module_h m, void *jobj);
41 static gpointer _ms_ipc_dispatch_worker(gpointer data);
42 static gboolean _ms_ipc_data_processing(int fd, muse_recv_data_head_t *header, muse_channel_info_t *ch);
43 static gpointer _ms_ipc_data_worker(gpointer data);
45 static void _ms_ipc_data_ch_init(muse_module_h m)
47 muse_return_if_fail(m);
49 m->ch[MUSE_CHANNEL_DATA].data_queue = g_queue_new();
50 g_mutex_init(&m->ch[MUSE_CHANNEL_DATA].data_mutex);
51 g_cond_init(&m->ch[MUSE_CHANNEL_DATA].data_cond);
54 static void _ms_ipc_data_ch_deinit(muse_module_h m)
56 muse_return_if_fail(m);
58 g_mutex_lock(&m->ch[MUSE_CHANNEL_DATA].data_mutex);
59 g_queue_free(m->ch[MUSE_CHANNEL_DATA].data_queue);
60 m->ch[MUSE_CHANNEL_DATA].data_queue = NULL;
61 g_cond_broadcast(&m->ch[MUSE_CHANNEL_DATA].data_cond);
62 g_mutex_unlock(&m->ch[MUSE_CHANNEL_DATA].data_mutex);
64 g_mutex_clear(&m->ch[MUSE_CHANNEL_DATA].data_mutex);
65 g_cond_clear(&m->ch[MUSE_CHANNEL_DATA].data_cond);
68 static void _ms_ipc_module_data_ch_cleanup(muse_module_h m)
70 muse_return_if_fail(m);
71 muse_return_if_fail(m->ch[MUSE_CHANNEL_DATA].thread);
73 g_thread_join(m->ch[MUSE_CHANNEL_DATA].thread);
74 m->ch[MUSE_CHANNEL_DATA].thread = NULL;
76 _ms_ipc_data_ch_deinit(m);
78 LOGD("[close] [%d] MUSE_CHANNEL_DATA", m->ch[MUSE_CHANNEL_DATA].sock_fd);
79 muse_core_connection_close(m->ch[MUSE_CHANNEL_DATA].sock_fd);
83 static void _ms_ipc_module_msg_ch_cleanup(muse_module_h m)
85 muse_return_if_fail(m);
86 muse_return_if_fail(m->ch[MUSE_CHANNEL_MSG].thread);
88 SECURE_LOGD("[close] [%d] MUSE_CHANNEL_MSG %p", m->ch[MUSE_CHANNEL_MSG].sock_fd, m);
89 muse_core_connection_close(m->ch[MUSE_CHANNEL_MSG].sock_fd);
91 SECURE_LOGD("msg thread (%p) exit", m->ch[MUSE_CHANNEL_MSG].thread);
92 g_thread_unref(m->ch[MUSE_CHANNEL_MSG].thread);
93 m->ch[MUSE_CHANNEL_MSG].thread = NULL;
96 static void _ms_ipc_module_cleanup(muse_module_h m)
98 muse_return_if_fail(m);
100 _ms_ipc_module_data_ch_cleanup(m);
102 _ms_ipc_module_msg_ch_cleanup(m);
104 ms_connection_unregister(m);
106 g_mutex_clear(&m->dispatch_lock);
108 memset(m, 0, sizeof(muse_module_t));
110 LOGI("[module %p] EXIT pid %d handle %zd created %d", m, m->pid, m->handle, m->is_created);
114 static gboolean _ms_ipc_module_instance_creation_is_allowed(int module_idx)
116 int max_instance, created_module_instance_count;
118 muse_return_val_if_fail(ms_check_module_idx(module_idx), FALSE);
120 max_instance = ms_config_get_max_instance(module_idx);
121 created_module_instance_count = muse_server_get_module_instance_count(module_idx);
123 if (max_instance == UNLIMITED_INSTANCE || created_module_instance_count < max_instance) {
126 LOGW("The number (%d) of created module instance is over the value of max instance (%d)",
127 created_module_instance_count, max_instance);
132 static gboolean _ms_ipc_get_module_idx(muse_module_h m, void *jobj)
134 muse_return_val_if_fail(m, FALSE);
135 muse_return_val_if_fail(jobj, FALSE);
137 if (!muse_core_msg_object_get_value(MSG_KEY_MODULE_INDEX, jobj, MUSE_TYPE_INT, &m->idx)) {
138 LOGE("Failed to get the value of module index");
145 static gboolean _ms_ipc_dispatch_create(muse_module_h m, void *jobj)
147 int pid, dispatch_ret = MM_ERROR_NONE;
149 muse_return_val_if_fail(m, FALSE);
150 muse_return_val_if_fail(jobj, FALSE);
152 ms_module_dispatch_lock(m);
154 if (!_ms_ipc_get_module_idx(m, jobj)) {
155 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_DEBUG_INFO_DUMP);
159 m->ch[MUSE_CHANNEL_MSG].dll_handle = ms_module_open(m->idx);
161 if (!_ms_ipc_module_instance_creation_is_allowed(m->idx)) {
162 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_RESOURCE_NOT_AVAILABLE);
166 if (muse_core_msg_object_get_value(MSG_KEY_PID, jobj, MUSE_TYPE_INT, &pid) && m->pid != pid)
167 LOGW("connected pid [%d] msg [%d] is different", m->pid, pid);
169 ms_connection_register(m);
171 if (muse_server_is_ready())
172 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_CREATE_SERVER_ACK);
174 LOGW("Do not send server acknowledgement because muse server is actually not ready");
176 _ms_ipc_data_ch_init(m);
178 LOGD("module fd: %d dll_handle: %p", m->ch[MUSE_CHANNEL_MSG].sock_fd, m->ch[MUSE_CHANNEL_MSG].dll_handle);
179 dispatch_ret = ms_module_dispatch(m);
181 if (dispatch_ret != MM_ERROR_NONE) {
182 LOGE("create dispatch failed 0x%x, clean up module", dispatch_ret);
186 m->is_created = TRUE;
188 ms_module_dispatch_unlock(m);
195 ms_module_dispatch_unlock(m);
200 static gboolean _ms_ipc_dispatch_destroy(muse_module_h m)
202 int dispatch_ret = MM_ERROR_NONE;
204 muse_return_val_if_fail(m, FALSE);
206 ms_module_dispatch_lock(m);
207 dispatch_ret = ms_module_dispatch(m);
208 if (dispatch_ret != MM_ERROR_NONE) {
209 LOGE("destroy dispatch failed 0x%x", dispatch_ret);
210 ms_module_dispatch_unlock(m);
214 ms_module_dispatch_unlock(m);
219 static gboolean _ms_ipc_dispatch_no_instance(muse_module_h m, void *jobj)
221 muse_return_val_if_fail(m, FALSE);
222 muse_return_val_if_fail(jobj, FALSE);
224 ms_module_dispatch_lock(m);
226 if (!_ms_ipc_get_module_idx(m, jobj)) {
227 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_DEBUG_INFO_DUMP);
231 m->ch[MUSE_CHANNEL_MSG].dll_handle = ms_module_open(m->idx);
233 if (!_ms_ipc_module_instance_creation_is_allowed(m->idx)) {
234 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_RESOURCE_NOT_AVAILABLE);
238 ms_connection_register(m);
240 _ms_ipc_data_ch_init(m);
242 ms_module_dispatch(m);
244 SECURE_LOGW("[module %p] [loaded value %d]", m, ms_module_get_loaded_dllsym(m->idx));
247 ms_module_dispatch_unlock(m);
252 static gpointer _ms_ipc_dispatch_worker(gpointer data)
256 muse_module_h m = NULL;
257 gboolean attempt_to_dispatch = TRUE;
259 char err_msg[MUSE_MSG_LEN_MAX] = {'\0',};
261 muse_return_val_if_fail(data, NULL);
263 m = (muse_module_h)data;
265 fd = m->ch[MUSE_CHANNEL_MSG].sock_fd;
266 m->ch[MUSE_CHANNEL_MSG].thread = g_thread_self();
268 LOGI("Enter %d module %p thread %p", fd, m, m->ch[MUSE_CHANNEL_MSG].thread);
270 while (attempt_to_dispatch) {
271 memset(m->recv_msg, 0x00, sizeof(m->recv_msg));
273 for (i = 0; i < MUSE_NUM_FD; i++)
274 m->ch[MUSE_CHANNEL_MSG].tbm_fd[i] = -1;
276 len = muse_core_msg_recv_fd(fd, m->recv_msg, MUSE_MSG_MAX_LENGTH, m->ch[MUSE_CHANNEL_MSG].tbm_fd);
278 strerror_r(errno, err_msg, MUSE_MSG_LEN_MAX);
279 LOGE("[%s] [%d] recv : %s (%d)", ms_config_get_host_name(m->idx), fd, err_msg, errno);
280 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_SHUTDOWN);
281 attempt_to_dispatch = FALSE;
286 while (attempt_to_dispatch && m->msg_offset < len && ms_is_server_ready()) {
287 jobj = muse_core_msg_object_new(m->recv_msg + m->msg_offset, &parse_len, NULL);
289 LOGE("jobj is null");
290 attempt_to_dispatch = FALSE;
294 if (muse_core_msg_object_get_value(MSG_KEY_API, jobj, MUSE_TYPE_INT, &m->api)) {
297 SECURE_LOGI("CREATE module %p %d", m, fd);
298 attempt_to_dispatch = _ms_ipc_dispatch_create(m, jobj);
301 SECURE_LOGI("DESTROY module %p %d", m, fd);
302 attempt_to_dispatch = _ms_ipc_dispatch_destroy(m);
305 if (m->is_created) /* handle based */
306 ms_module_dispatch(m);
308 attempt_to_dispatch = _ms_ipc_dispatch_no_instance(m, jobj);
312 muse_core_msg_object_free(jobj);
314 m->msg_offset += parse_len;
315 parse_len = len - parse_len;
319 _ms_ipc_module_cleanup(m);
323 #ifdef MUSE_GCOV_TEST
324 muse_core_gcov_flush();
330 static gboolean _ms_ipc_data_processing(int fd, muse_recv_data_head_t *header, muse_channel_info_t *ch)
332 char *raw_data = NULL;
333 muse_server_h ms = ms_get_instance();
335 muse_return_val_if_fail(ms, FALSE);
337 if (!(fd > 0 && header && ch)) {
338 LOGE("invalid param %d %p %p", fd, header, ch);
339 goto _PROCESSING_FAILED;
343 if (header->marker != MUSE_DATA_HEAD) {
344 LOGE("invalid marker 0x%x", header->marker);
345 goto _PROCESSING_FAILED;
348 /* check data size */
349 if (header->size > DATA_WORKER_QDATA_MAX_SIZE) {
350 LOGE("invalid data size %d", header->size);
351 goto _PROCESSING_FAILED;
354 /* allocation data */
355 raw_data = (char *)g_try_new0(char, header->size + sizeof(muse_recv_data_head_t));
357 LOGE("failed to alloc data %d + %zu", header->size, sizeof(muse_recv_data_head_t));
358 goto _PROCESSING_FAILED;
362 memcpy(raw_data, header, sizeof(muse_recv_data_head_t));
365 if (!muse_core_msg_recv_len(fd, raw_data + sizeof(muse_recv_data_head_t), header->size)) {
366 LOGE("receive data failed - length %d", header->size);
367 goto _PROCESSING_FAILED;
371 g_mutex_lock(&ch->data_mutex);
372 g_queue_push_tail(ch->data_queue, (gpointer)raw_data);
373 g_cond_signal(&ch->data_cond);
374 g_mutex_unlock(&ch->data_mutex);
380 MUSE_G_FREE(raw_data);
382 ms_log_process_info(ms->pid);
387 static gpointer _ms_ipc_data_worker(gpointer data)
389 char recv_buf[MUSE_MSG_LEN_MAX] = {'\0',};
391 muse_module_h m = NULL;
392 muse_channel_info_t *ch = NULL;
394 muse_return_val_if_fail(data, NULL);
396 m = (muse_module_h)data;
397 SECURE_LOGW("module : %p pid %d handle %zd created %d", m, m->pid, m->handle, m->is_created);
399 muse_return_val_if_fail(m->pid > 0 && m->handle && m->is_created, NULL);
401 fd = m->ch[MUSE_CHANNEL_DATA].sock_fd;
402 ch = &m->ch[MUSE_CHANNEL_DATA];
406 if (!muse_core_msg_recv_len(fd, recv_buf, sizeof(muse_recv_data_head_t)))
409 if (!_ms_ipc_data_processing(fd, (muse_recv_data_head_t *)recv_buf, ch)) {
410 LOGE("ipc data processing failed");
420 gboolean ms_ipc_create_msg_dispatch_worker(muse_module_h m)
422 GThread *thread = NULL;
423 GError *error = NULL;
424 muse_server_h ms = ms_get_instance();
426 ms_connection_t *connection = NULL;
430 muse_return_val_if_fail(ms, FALSE);
431 muse_return_val_if_fail(ms_is_server_ready(), FALSE);
432 muse_return_val_if_fail(muse_server_module_is_valid(m), FALSE);
434 connection = ms->connection;
435 muse_return_val_if_fail(connection, FALSE);
437 ms_connection_lock(connection);
439 SECURE_LOGD("[PID %d module %p] module's msg channel fd : %d", m->pid, m, m->ch[MUSE_CHANNEL_MSG].sock_fd);
441 thread = g_thread_try_new(MSG_THREAD_NAME, _ms_ipc_dispatch_worker, (gpointer)m, &error);
443 LOGE("[module %p] thread creation failed : %s", m, error->message);
445 ms_log_process_info(ms->pid);
446 ms_connection_unlock(connection);
450 LOGD("Leave module %p thread %p", m, thread);
452 ms_connection_unlock(connection);
457 gboolean ms_ipc_create_data_dispatch_worker(muse_module_h m)
459 GError *error = NULL;
460 muse_server_h ms = ms_get_instance();
461 ms_connection_t *connection = NULL;
465 muse_return_val_if_fail(ms, FALSE);
466 muse_return_val_if_fail(ms_is_server_ready(), FALSE);
467 muse_return_val_if_fail(muse_server_module_is_valid(m), TRUE);
469 connection = ms->connection;
470 muse_return_val_if_fail(connection, FALSE);
472 ms_connection_lock(connection);
474 m->ch[MUSE_CHANNEL_DATA].thread = g_thread_try_new(DATA_THREAD_NAME, _ms_ipc_data_worker, (gpointer)m, &error);
475 if (!m->ch[MUSE_CHANNEL_DATA].thread) {
476 LOGE("thread creation failed : %s", error->message);
478 ms_log_process_info(ms->pid);
479 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_RESOURCE_NOT_AVAILABLE);
480 muse_core_connection_close(m->ch[MUSE_CHANNEL_MSG].sock_fd);
481 ms_connection_unlock(connection);
487 ms_connection_unlock(connection);