Update the dlog of percent and dlog level related with msg channel
[platform/core/multimedia/mmsvc-core.git] / server / src / muse_server_ipc.c
1 /*
2  * muse-server
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: YoungHun Kim <yh8004.kim@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include "muse_core_internal.h"
23 #include "muse_server_private.h"
24
25 #define MSG_THREAD_NAME                                         "msg"
26 #define DATA_THREAD_NAME                                        "data"
27
28 #define DATA_WORKER_QDATA_MAX_SIZE                      (3840 * 2160 * 4) /* UHD BGRA8888 */
29 #define UNLIMITED_INSTANCE                                      -1
30
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);
44
45 static void _ms_ipc_data_ch_init(muse_module_h m)
46 {
47         muse_return_if_fail(m);
48
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);
52 }
53
54 static void _ms_ipc_data_ch_deinit(muse_module_h m)
55 {
56         muse_return_if_fail(m);
57
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);
63
64         g_mutex_clear(&m->ch[MUSE_CHANNEL_DATA].data_mutex);
65         g_cond_clear(&m->ch[MUSE_CHANNEL_DATA].data_cond);
66 }
67
68 static void _ms_ipc_module_data_ch_cleanup(muse_module_h m)
69 {
70         muse_return_if_fail(m);
71         muse_return_if_fail(m->ch[MUSE_CHANNEL_DATA].thread);
72
73         g_thread_join(m->ch[MUSE_CHANNEL_DATA].thread);
74         m->ch[MUSE_CHANNEL_DATA].thread = NULL;
75
76         _ms_ipc_data_ch_deinit(m);
77
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);
80
81 }
82
83 static void _ms_ipc_module_msg_ch_cleanup(muse_module_h m)
84 {
85         muse_return_if_fail(m);
86         muse_return_if_fail(m->ch[MUSE_CHANNEL_MSG].thread);
87
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);
90
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;
94 }
95
96 static void _ms_ipc_module_cleanup(muse_module_h m)
97 {
98         muse_return_if_fail(m);
99
100         _ms_ipc_module_data_ch_cleanup(m);
101
102         _ms_ipc_module_msg_ch_cleanup(m);
103
104         ms_connection_unregister(m);
105
106         g_mutex_clear(&m->dispatch_lock);
107
108         memset(m, 0, sizeof(muse_module_t));
109
110         LOGI("[module %p] EXIT", m);
111         g_free(m);
112 }
113
114 static gboolean _ms_ipc_module_instance_creation_is_allowed(int module_idx)
115 {
116         int max_instance, created_module_instance_count;
117
118         muse_return_val_if_fail(ms_check_module_idx(module_idx), FALSE);
119
120         max_instance = ms_config_get_max_instance(module_idx);
121         created_module_instance_count = muse_server_get_module_instance_count(module_idx);
122
123         if (max_instance == UNLIMITED_INSTANCE || created_module_instance_count < max_instance) {
124                 return TRUE;
125         } else {
126                 LOGW("The number (%d) of created module instance is over the value of max instance (%d)",
127                                 created_module_instance_count, max_instance);
128                 return FALSE;
129         }
130 }
131
132 static gboolean _ms_ipc_get_module_idx(muse_module_h m, void *jobj)
133 {
134         muse_return_val_if_fail(m, FALSE);
135         muse_return_val_if_fail(jobj, FALSE);
136
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");
139                 return FALSE;
140         }
141
142         return TRUE;
143 }
144
145 static gboolean _ms_ipc_dispatch_create(muse_module_h m, void *jobj)
146 {
147         int pid, dispatch_ret = MM_ERROR_NONE;
148
149         muse_return_val_if_fail(m, FALSE);
150         muse_return_val_if_fail(jobj, FALSE);
151
152         ms_module_dispatch_lock(m);
153
154         if (!_ms_ipc_get_module_idx(m, jobj)) {
155                 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_DEBUG_INFO_DUMP);
156                 goto out;
157         }
158
159         m->ch[MUSE_CHANNEL_MSG].dll_handle = ms_module_open(m->idx);
160
161         if (!_ms_ipc_module_instance_creation_is_allowed(m->idx)) {
162                 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_RESOURCE_NOT_AVAILABLE);
163                 goto out;
164         }
165
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);
168
169         ms_connection_register(m);
170
171         if (muse_server_is_ready())
172                 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_CREATE_SERVER_ACK);
173         else
174                 LOGW("Do not send server acknowledgement because muse server is actually not ready");
175
176         _ms_ipc_data_ch_init(m);
177
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);
180
181         if (dispatch_ret != MM_ERROR_NONE) {
182                 LOGE("create dispatch failed 0x%x, clean up module", dispatch_ret);
183                 goto out;
184         }
185
186         m->is_created = TRUE;
187
188         ms_module_dispatch_unlock(m);
189
190         return TRUE;
191
192 out:
193         ms_module_dispatch_unlock(m);
194
195         return FALSE;
196 }
197
198 static gboolean _ms_ipc_dispatch_destroy(muse_module_h m)
199 {
200         int dispatch_ret = MM_ERROR_NONE;
201
202         muse_return_val_if_fail(m, FALSE);
203
204         ms_module_dispatch_lock(m);
205         dispatch_ret = ms_module_dispatch(m);
206         if (dispatch_ret != MM_ERROR_NONE)
207                 LOGE("destroy dispatch failed 0x%x", dispatch_ret);
208         ms_module_dispatch_unlock(m);
209
210         return FALSE;
211 }
212
213 static gboolean _ms_ipc_dispatch_no_instance(muse_module_h m, void *jobj)
214 {
215         muse_return_val_if_fail(m, FALSE);
216         muse_return_val_if_fail(jobj, FALSE);
217
218         ms_module_dispatch_lock(m);
219
220         if (!_ms_ipc_get_module_idx(m, jobj)) {
221                 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_DEBUG_INFO_DUMP);
222                 goto out;
223         }
224
225         m->ch[MUSE_CHANNEL_MSG].dll_handle = ms_module_open(m->idx);
226
227         if (!_ms_ipc_module_instance_creation_is_allowed(m->idx)) {
228                 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_RESOURCE_NOT_AVAILABLE);
229                 goto out;
230         }
231
232         ms_connection_register(m);
233
234         _ms_ipc_data_ch_init(m);
235
236         ms_module_dispatch(m);
237
238         SECURE_LOGW("[module %p] [loaded value %d]", m, ms_module_get_loaded_dllsym(m->idx));
239
240 out:
241         ms_module_dispatch_unlock(m);
242
243         return TRUE;
244 }
245
246 static gpointer _ms_ipc_dispatch_worker(gpointer data)
247 {
248         int len, fd, i;
249         int parse_len = 0;
250         muse_module_h m = NULL;
251         gboolean attempt_to_dispatch = TRUE;
252         void *jobj = NULL;
253         char err_msg[MUSE_MSG_LEN_MAX] = {'\0',};
254
255         muse_return_val_if_fail(data, NULL);
256
257         m = (muse_module_h)data;
258
259         fd = m->ch[MUSE_CHANNEL_MSG].sock_fd;
260         m->ch[MUSE_CHANNEL_MSG].thread = g_thread_self();
261
262         LOGI("Enter %d module %p thread %p", fd, m, m->ch[MUSE_CHANNEL_MSG].thread);
263
264         while (attempt_to_dispatch) {
265                 memset(m->recv_msg, 0x00, sizeof(m->recv_msg));
266
267                 for (i = 0; i < MUSE_NUM_FD; i++)
268                         m->ch[MUSE_CHANNEL_MSG].tbm_fd[i] = -1;
269
270                 len = muse_core_msg_recv_fd(fd, m->recv_msg, MUSE_MSG_MAX_LENGTH, m->ch[MUSE_CHANNEL_MSG].tbm_fd);
271                 if (len <= 0) {
272                         strerror_r(errno, err_msg, MUSE_MSG_LEN_MAX);
273                         LOGE("[%s] [%d] recv : %s (%d)", ms_config_get_host_name(m->idx), fd, err_msg, errno);
274                         ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_SHUTDOWN);
275                         attempt_to_dispatch = FALSE;
276                 }
277
278                 m->msg_offset = 0;
279
280                 while (attempt_to_dispatch && m->msg_offset < len && ms_is_server_ready()) {
281                         jobj = muse_core_msg_object_new(m->recv_msg + m->msg_offset, &parse_len, NULL);
282                         if (!jobj) {
283                                 LOGE("jobj is null");
284                                 attempt_to_dispatch = FALSE;
285                                 break;
286                         }
287
288                         if (muse_core_msg_object_get_value(MSG_KEY_API, jobj, MUSE_TYPE_INT, &m->api)) {
289                                 switch (m->api) {
290                                 case API_CREATE:
291                                         SECURE_LOGI("CREATE module %p %d", m, fd);
292                                         attempt_to_dispatch = _ms_ipc_dispatch_create(m, jobj);
293                                         break;
294                                 case API_DESTROY:
295                                         SECURE_LOGI("DESTROY module %p %d", m, fd);
296                                         attempt_to_dispatch = _ms_ipc_dispatch_destroy(m);
297                                         break;
298                                 default:
299                                         if (m->is_created) /* handle based */
300                                                 ms_module_dispatch(m);
301                                         else
302                                                 attempt_to_dispatch = _ms_ipc_dispatch_no_instance(m, jobj);
303                                         break;
304                                 }
305                         }
306                         muse_core_msg_object_free(jobj);
307                         jobj = NULL;
308                         m->msg_offset += parse_len;
309                         parse_len = len - parse_len;
310                 }
311         }
312
313         _ms_ipc_module_cleanup(m);
314
315         LOGD("worker exit");
316
317 #ifdef MUSE_GCOV_TEST
318         muse_core_gcov_flush();
319 #endif
320
321         return NULL;
322 }
323
324 static gboolean _ms_ipc_data_processing(int fd, muse_recv_data_head_t *header, muse_channel_info_t *ch)
325 {
326         char *raw_data = NULL;
327         muse_server_h ms = ms_get_instance();
328
329         muse_return_val_if_fail(ms, FALSE);
330
331         if (!(fd > 0 && header && ch)) {
332                 LOGE("invalid param %d %p %p", fd, header, ch);
333                 goto _PROCESSING_FAILED;
334         }
335
336         /* check marker */
337         if (header->marker != MUSE_DATA_HEAD) {
338                 LOGE("invalid marker 0x%x", header->marker);
339                 goto _PROCESSING_FAILED;
340         }
341
342         /* check data size */
343         if (header->size > DATA_WORKER_QDATA_MAX_SIZE) {
344                 LOGE("invalid data size %d", header->size);
345                 goto _PROCESSING_FAILED;
346         }
347
348         /* allocation data */
349         raw_data = (char *)g_try_new0(char, header->size + sizeof(muse_recv_data_head_t));
350         if (!raw_data) {
351                 LOGE("failed to alloc data %d + %zu", header->size, sizeof(muse_recv_data_head_t));
352                 goto _PROCESSING_FAILED;
353         }
354
355         /* copy header */
356         memcpy(raw_data, header, sizeof(muse_recv_data_head_t));
357
358         /* receive data */
359         if (!muse_core_msg_recv_len(fd, raw_data + sizeof(muse_recv_data_head_t), header->size)) {
360                 LOGE("receive data failed - length %d", header->size);
361                 goto _PROCESSING_FAILED;
362         }
363
364         /* push data */
365         g_mutex_lock(&ch->data_mutex);
366         g_queue_push_tail(ch->data_queue, (gpointer)raw_data);
367         g_cond_signal(&ch->data_cond);
368         g_mutex_unlock(&ch->data_mutex);
369
370         return TRUE;
371
372 _PROCESSING_FAILED:
373
374         MUSE_G_FREE(raw_data);
375
376         ms_log_process_info(ms->pid);
377
378         return FALSE;
379 }
380
381 static gpointer _ms_ipc_data_worker(gpointer data)
382 {
383         char recv_buf[MUSE_MSG_LEN_MAX] = {'\0',};
384         int fd;
385         muse_module_h m = NULL;
386         muse_channel_info_t *ch = NULL;
387
388         muse_return_val_if_fail(data, NULL);
389
390         m = (muse_module_h)data;
391         SECURE_LOGW("module : %p", m);
392
393         fd = m->ch[MUSE_CHANNEL_DATA].sock_fd;
394         ch = &m->ch[MUSE_CHANNEL_DATA];
395
396         /* get data */
397         while (1) {
398                 if (!muse_core_msg_recv_len(fd, recv_buf, sizeof(muse_recv_data_head_t)))
399                         break;
400
401                 if (!_ms_ipc_data_processing(fd, (muse_recv_data_head_t *)recv_buf, ch)) {
402                         LOGE("ipc data processing failed");
403                         break;
404                 }
405         }
406
407         LOGW("Leave");
408
409         return NULL;
410 }
411
412 gboolean ms_ipc_create_msg_dispatch_worker(muse_module_h m)
413 {
414         GThread *thread = NULL;
415         GError *error = NULL;
416         muse_server_h ms = ms_get_instance();
417
418         LOGD("Enter");
419
420         muse_return_val_if_fail(ms, FALSE);
421         muse_return_val_if_fail(ms_is_server_ready(), FALSE);
422         muse_return_val_if_fail(muse_server_module_is_valid(m), FALSE);
423
424         SECURE_LOGD("[PID %d module %p] module's msg channel fd : %d", m->pid, m, m->ch[MUSE_CHANNEL_MSG].sock_fd);
425
426         thread = g_thread_try_new(MSG_THREAD_NAME, _ms_ipc_dispatch_worker, (gpointer)m, &error);
427         if (!thread) {
428                 LOGE("[module %p] thread creation failed : %s", m, error->message);
429                 g_error_free(error);
430                 ms_log_process_info(ms->pid);
431                 return FALSE;
432         }
433
434         LOGD("Leave module %p, thread %p", m, thread);
435
436         return TRUE;
437 }
438
439 gboolean ms_ipc_create_data_dispatch_worker(muse_module_h m)
440 {
441         GError *error = NULL;
442         muse_server_h ms = ms_get_instance();
443
444         LOGD("Enter");
445
446         muse_return_val_if_fail(ms, FALSE);
447         muse_return_val_if_fail(ms_is_server_ready(), FALSE);
448         muse_return_val_if_fail(muse_server_module_is_valid(m), TRUE);
449
450         m->ch[MUSE_CHANNEL_DATA].thread = g_thread_try_new(DATA_THREAD_NAME, _ms_ipc_data_worker, (gpointer)m, &error);
451         if (!m->ch[MUSE_CHANNEL_DATA].thread) {
452                 LOGE("thread creation failed : %s", error->message);
453                 g_error_free(error);
454                 ms_log_process_info(ms->pid);
455                 ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_RESOURCE_NOT_AVAILABLE);
456                 muse_core_connection_close(m->ch[MUSE_CHANNEL_MSG].sock_fd);
457         }
458
459         muse_return_val_if_fail(m->ch[MUSE_CHANNEL_DATA].thread, FALSE);
460
461         LOGD("Leave");
462         return TRUE;
463 }