Check for symbolic link vulnerability
[platform/core/multimedia/libmm-sound.git] / mm_sound_focus_private.c
1 /*
2  * libmm-sound
3  *
4  * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Sangchul Lee <sc11.lee@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 <stdio.h>
23 #include <stdbool.h>
24 #include <string.h>
25 #include <pthread.h>
26 #include <glib.h>
27 #include <poll.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32
33 #include <mm_debug.h>
34 #include <mm_error.h>
35 #include "include/mm_sound_focus_private.h"
36
37 focus_sound_info_t g_focus_sound_handle[FOCUS_HANDLE_MAX];
38
39 static void unlink_if_symbolic_link(const char *path)
40 {
41         int ret = 0;
42         char *resolved_path = NULL;
43
44         if (path == NULL)
45                 return;
46
47         /* return if it does not exist */
48         if ((ret = access(path, F_OK)))
49                 return;
50
51         if ((resolved_path = realpath(path, NULL))) {
52                 /* assume that the path paramether is an absolute path */
53                 if (strcmp(path, resolved_path)) {
54                         debug_warning("unexpected symbolic link!, unlink the symbolic link(%s) to the resolved path(%s)", path, resolved_path);
55                         unlink(path);
56                 }
57                 free(resolved_path);
58         } else {
59                 char str_error[256];
60                 strerror_r(errno, str_error, sizeof(str_error));
61                 debug_warning("failed to realpath() for path:%s, err:%s", path, str_error);
62         }
63 }
64
65 static gpointer _focus_thread_func(gpointer data)
66 {
67         unsigned int thread_id = (unsigned int)pthread_self();
68         GMainLoop *focus_loop = (GMainLoop*)data;
69
70         debug_warning(">>> thread id(%u), mainloop[%p]", thread_id, focus_loop);
71         if (focus_loop)
72                 g_main_loop_run(focus_loop);
73         debug_warning("<<< quit : thread id(%u), mainloop[%p]", thread_id, focus_loop);
74
75         return NULL;
76 }
77
78 static gboolean _focus_fd_prepare(GSource *source, gint *timeout)
79 {
80 #ifdef __DEBUG__
81         debug_warning("[ PREPARE : %p, (%p, %d)", source, timeout, timeout ? *timeout : -1);
82 #endif
83         return FALSE;
84 }
85
86 static gboolean _focus_fd_check(GSource * source)
87 {
88         FocusSource* fsource = (FocusSource *)source;
89
90         if (!fsource) {
91                 debug_error("GSource is null");
92                 return FALSE;
93         }
94 #ifdef __DEBUG__
95         debug_warning("CHECK : %p, 0x%x ]", source, fsource->pollfd.revents);
96 #endif
97         if (fsource->poll_fd.revents & (POLLIN | POLLPRI))
98                 return TRUE;
99         else
100                 return FALSE;
101 }
102
103 static gboolean _focus_fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data)
104 {
105         debug_warning("*** DISPATCH : %p, (%p, %p)", source, callback, user_data);
106         return callback(user_data);
107 }
108
109 static void _focus_fd_finalize(GSource *source)
110 {
111         debug_warning("### FINALIZE : %p", source);
112 }
113
114 static gboolean _focus_callback_handler(gpointer user_data)
115 {
116         focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
117         GPollFD *poll_fd;
118         int count;
119         int tid = 0;
120         focus_cb_data_lib cb_data;
121
122         debug_log(">>> thread id(%u)", (unsigned int)pthread_self());
123
124         if (!focus_handle) {
125                 debug_error("focus_handle is null");
126                 return G_SOURCE_CONTINUE;
127         }
128         poll_fd = &focus_handle->fsrc->poll_fd;
129         debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
130
131         memset(&cb_data, 0, sizeof(focus_cb_data_lib));
132
133         if (poll_fd->revents & (POLLIN | POLLPRI)) {
134                 int changed_state = -1;
135
136                 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
137                 if (count < 0) {
138                         char str_error[256];
139                         strerror_r(errno, str_error, sizeof(str_error));
140                         debug_error("GpollFD read fail, errno=%d(%s)", errno, str_error);
141                         return G_SOURCE_CONTINUE;
142                 }
143                 changed_state = cb_data.state;
144
145                 g_mutex_lock(&focus_handle->focus_lock);
146
147                 tid = focus_handle->focus_pid;
148
149                 if (changed_state != -1) {
150                         debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
151                                         tid, cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type);
152                         if (focus_handle->focus_callback == NULL) {
153                                         debug_error("focus callback is null..");
154                                         g_mutex_unlock(&focus_handle->focus_lock);
155                                         return G_SOURCE_CONTINUE;
156                         }
157                         debug_msg("[CALLBACK(%p) START]", focus_handle->focus_callback);
158                         (focus_handle->focus_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
159                                                                                 cb_data.option, cb_data.ext_info, focus_handle->user_data);
160                         debug_msg("[CALLBACK END]");
161                 }
162                 {
163                         int rett = 0;
164                         int tmpfd = -1;
165                         unsigned int buf = 0;
166                         char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%dr", focus_handle->focus_pid, cb_data.handle);
167
168                         unlink_if_symbolic_link(filename2);
169                         tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
170                         if (tmpfd < 0) {
171                                 char str_error[256];
172                                 strerror_r(errno, str_error, sizeof(str_error));
173                                 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
174                                                         tid, tmpfd, filename2, errno, str_error);
175                                 g_free(filename2);
176                                 g_mutex_unlock(&focus_handle->focus_lock);
177                                 return G_SOURCE_CONTINUE;
178                         }
179                         /* buf contains data as below,
180                          * |<--12bits--><--4bits (reacquisition)--><--16bits (handle)-->| */
181                         buf = (unsigned int)((0x0000ffff & cb_data.handle) | (focus_handle->auto_reacquire << 16));
182                         rett = write(tmpfd, &buf, sizeof(buf));
183                         close(tmpfd);
184                         g_free(filename2);
185                         debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
186                 }
187         }
188
189         g_mutex_unlock(&focus_handle->focus_lock);
190
191         debug_fleave();
192
193         return G_SOURCE_CONTINUE;
194 }
195
196 static gboolean _focus_watch_callback_handler(gpointer user_data)
197 {
198         focus_sound_info_t *focus_handle = (focus_sound_info_t *)user_data;
199         GPollFD *poll_fd;
200         int count;
201         int tid = 0;
202         focus_cb_data_lib cb_data;
203
204         debug_log(">>> thread id(%u)", (unsigned int)pthread_self());
205
206         if (!focus_handle) {
207                 debug_error("focus_handle is null");
208                 return G_SOURCE_CONTINUE;
209         }
210         poll_fd = &focus_handle->fsrc->poll_fd;
211         debug_log("focus_handle(%p), poll_fd(%p)", focus_handle, poll_fd);
212
213         memset(&cb_data, 0, sizeof(focus_cb_data_lib));
214
215         if (poll_fd->revents & (POLLIN | POLLPRI)) {
216                 count = read(poll_fd->fd, &cb_data, sizeof(cb_data));
217                 if (count < 0) {
218                         char str_error[256];
219                         strerror_r(errno, str_error, sizeof(str_error));
220                         debug_error("GpollFD read fail, errno=%d(%s)", errno, str_error);
221                         return G_SOURCE_CONTINUE;
222                 }
223
224                 if (!focus_handle->is_used) {
225                         debug_warning("unsetting watch calllback has been already requested");
226                         goto SKIP_CB_AND_RET;
227                 }
228
229                 g_mutex_lock(&focus_handle->focus_lock);
230
231                 tid = focus_handle->focus_pid;
232
233                 debug_msg("Got and start CB : TID(%d), handle(%d), type(%d), state(%d,(DEACTIVATED(0)/ACTIVATED(1)), trigger(%s)",
234                                 tid, cb_data.handle,  cb_data.type, cb_data.state, cb_data.stream_type);
235
236                 if (focus_handle->watch_callback == NULL) {
237                         debug_warning("watch callback is null..");
238                         goto SKIP_CB_AND_RET;
239                 }
240                 if (focus_handle->unset_watch_callback_requested == true) {
241                         debug_warning("unset_watch_callback_requested..");
242                         goto SKIP_CB_AND_RET;
243                 }
244
245                 debug_msg("[CALLBACK(%p) START]", focus_handle->watch_callback);
246                 (focus_handle->watch_callback)(cb_data.handle, cb_data.type, cb_data.state, cb_data.stream_type,
247                                                                         cb_data.ext_info, focus_handle->user_data);
248                 debug_msg("[CALLBACK END]");
249
250 SKIP_CB_AND_RET:
251                 {
252                         int rett = 0;
253                         int tmpfd = -1;
254                         int buf = -1;
255                         char *filename2 = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr", focus_handle->focus_pid, cb_data.handle);
256
257                         unlink_if_symbolic_link(filename2);
258                         tmpfd = open(filename2, O_WRONLY | O_NONBLOCK);
259                         if (tmpfd < 0) {
260                                 char str_error[256];
261                                 strerror_r(errno, str_error, sizeof(str_error));
262                                 debug_warning("[RETCB][Failed(May Server Close First)]tid(%d) fd(%d) %s errno=%d(%s)",
263                                                         tid, tmpfd, filename2, errno, str_error);
264                                 g_free(filename2);
265                                 g_mutex_unlock(&focus_handle->focus_lock);
266                                 return G_SOURCE_CONTINUE;
267                         }
268                         buf = cb_data.handle;
269                         rett = write(tmpfd, &buf, sizeof(buf));
270                         close(tmpfd);
271                         g_free(filename2);
272                         debug_msg("[RETCB] tid(%d) finishing CB (write=%d)", tid, rett);
273                 }
274         }
275
276         if (focus_handle->is_used) {
277                 debug_msg("unlock focus_lock = %p", &focus_handle->focus_lock);
278                 g_mutex_unlock(&focus_handle->focus_lock);
279         }
280
281         debug_fleave();
282
283         return G_SOURCE_CONTINUE;
284 }
285
286 #define INTERVAL_MS 20
287 static int _focus_loop_is_running_timed_wait(GMainLoop *focus_loop, int timeout_ms)
288 {
289         int reduced_time_ms = timeout_ms;
290         if (!focus_loop || timeout_ms < 0) {
291                 debug_error("invalid argument, focus_loop(%p), timeout_ms(%d)", focus_loop, timeout_ms);
292                 return MM_ERROR_INVALID_ARGUMENT;
293         }
294
295         do {
296                 if (g_main_loop_is_running(focus_loop))
297                         return MM_ERROR_NONE;
298
299                 usleep(INTERVAL_MS * 1000);
300                 if (reduced_time_ms < timeout_ms)
301                         debug_warning("reduced_time_ms(%d)", reduced_time_ms);
302         } while ((reduced_time_ms -= INTERVAL_MS) >= 0);
303
304         debug_error("focus_loop is not running for timeout_ms(%d), focus_loop(%p) ", timeout_ms, focus_loop);
305
306         return MM_ERROR_SOUND_INTERNAL;
307 }
308
309 static void _focus_open_callback(int index, bool is_for_watching)
310 {
311         mode_t pre_mask;
312         char *filename;
313
314         debug_fenter();
315
316         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
317                 debug_error("Invalid focus handle index [%d]", index);
318                 return;
319         }
320
321         if (is_for_watching) {
322                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
323                                                                 g_focus_sound_handle[index].focus_pid,
324                                                                 g_focus_sound_handle[index].handle);
325         } else {
326                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
327                                                                 g_focus_sound_handle[index].focus_pid,
328                                                                 g_focus_sound_handle[index].handle);
329         }
330         unlink_if_symbolic_link(filename);
331         pre_mask = umask(0);
332         if (mknod(filename, S_IFIFO|0666, 0))
333                 debug_error("mknod() failure, errno(%d)", errno);
334         umask(pre_mask);
335         g_focus_sound_handle[index].focus_fd = open(filename, O_RDWR|O_NONBLOCK);
336         if (g_focus_sound_handle[index].focus_fd == -1) {
337                 debug_error("Open fail : index(%d), file open error(%d)", index, errno);
338         } else {
339                 debug_log("Open success : index(%d), filename(%s), fd(%d)",
340                                 index, filename, g_focus_sound_handle[index].focus_fd);
341         }
342         g_free(filename);
343         filename = NULL;
344
345         if (is_for_watching) {
346                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
347                                                                         g_focus_sound_handle[index].focus_pid,
348                                                                         g_focus_sound_handle[index].handle);
349         } else {
350                 filename = g_strdup_printf("/tmp/FOCUS.%d.%dr",
351                                                                         g_focus_sound_handle[index].focus_pid,
352                                                                         g_focus_sound_handle[index].handle);
353         }
354         pre_mask = umask(0);
355         if (mknod(filename, S_IFIFO | 0666, 0))
356                 debug_error("mknod() failure, errno(%d)", errno);
357         umask(pre_mask);
358         g_free(filename);
359         filename = NULL;
360
361         debug_fleave();
362 }
363
364 void _focus_close_callback(int index, bool is_for_watching)
365 {
366         char *filename = NULL;
367
368         debug_fenter();
369
370         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
371                 debug_error("Invalid focus handle index [%d]", index);
372                 return;
373         }
374
375         if (g_focus_sound_handle[index].focus_fd < 0) {
376                 debug_error("Close fail : index(%d)", index);
377         } else {
378                 close(g_focus_sound_handle[index].focus_fd);
379                 debug_log("Close Success : index(%d)", index);
380         }
381
382         if (is_for_watching) {
383                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wch",
384                                                                 g_focus_sound_handle[index].focus_pid,
385                                                                 g_focus_sound_handle[index].handle);
386         } else {
387                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d",
388                                                                 g_focus_sound_handle[index].focus_pid,
389                                                                 g_focus_sound_handle[index].handle);
390         }
391         if (remove(filename)) {
392                 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
393                                         filename, errno);
394         }
395         g_free(filename);
396         filename = NULL;
397
398         if (is_for_watching) {
399                 filename = g_strdup_printf("/tmp/FOCUS.%d.%d.wchr",
400                                                                         g_focus_sound_handle[index].focus_pid,
401                                                                         g_focus_sound_handle[index].handle);
402         } else {
403                 filename = g_strdup_printf("/tmp/FOCUS.%d.%dr",
404                                                                         g_focus_sound_handle[index].focus_pid,
405                                                                         g_focus_sound_handle[index].handle);
406         }
407         if (remove(filename)) {
408                 debug_warning("remove(%s) failure (focus_server probably removed it in advance), errno(%d)",
409                                         filename, errno);
410         }
411         g_free(filename);
412         filename = NULL;
413
414         debug_fleave();
415 }
416
417 static GSourceFuncs event_funcs = {
418         .prepare = _focus_fd_prepare,
419         .check = _focus_fd_check,
420         .dispatch = _focus_fd_dispatch,
421         .finalize = _focus_fd_finalize,
422 };
423
424 static bool _focus_add_sound_callback(int index, focus_callback_handler_t focus_cb_handler)
425 {
426         FocusSource *fsrc = NULL;
427         GSource *src = NULL;
428         guint fsrc_id = 0;
429
430         debug_fenter();
431
432         g_mutex_init(&g_focus_sound_handle[index].focus_lock);
433
434         src = g_source_new(&event_funcs, sizeof(FocusSource));
435         if (!src) {
436                 debug_error("failed to g_source_new for focus source");
437                 goto ERROR;
438         }
439
440         fsrc = (FocusSource*) src;
441
442         fsrc->poll_fd.fd = g_focus_sound_handle[index].focus_fd;
443         fsrc->poll_fd.events = (gushort)(POLLIN | POLLPRI);
444         g_source_add_poll(src, &fsrc->poll_fd);
445
446         g_source_set_callback(src, focus_cb_handler, (gpointer)&g_focus_sound_handle[index], NULL);
447
448         debug_warning("fsrc(%p), src_funcs(%p), pollfd(%p), fd(%d)",
449                                 fsrc, &event_funcs, &fsrc->poll_fd, fsrc->poll_fd.fd);
450
451         fsrc_id = g_source_attach(src, g_main_loop_get_context(g_focus_sound_handle[index].focus_loop));
452         if (!fsrc_id) {
453                 debug_error("failed to attach the source to context");
454                 goto ERROR;
455         }
456         g_source_unref(src);
457
458         g_focus_sound_handle[index].fsrc = fsrc;
459
460         debug_fleave();
461         return true;
462
463 ERROR:
464         if (src)
465                 g_source_unref(src);
466
467         return false;
468 }
469
470 static bool _focus_remove_sound_callback(int index)
471 {
472         focus_sound_info_t *h = NULL;
473
474         debug_fenter();
475
476         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
477                 debug_error("Invalid focus handle index [%d]", index);
478                 return false;
479         }
480
481         h = &g_focus_sound_handle[index];
482         if (h->fsrc) {
483                 g_source_destroy((GSource *)h->fsrc);
484                 h->fsrc = NULL;
485         }
486
487         h->focus_callback = NULL;
488         h->watch_callback = NULL;
489
490         g_mutex_clear(&h->focus_lock);
491
492         debug_fleave();
493
494         return true;
495 }
496
497 static void _focus_add_callback(int index, bool is_for_watching)
498 {
499         debug_fenter();
500
501         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
502                 debug_error("Invalid focus handle index [%d]", index);
503                 return;
504         }
505
506         if (!is_for_watching) {
507                 if (!_focus_add_sound_callback(index, _focus_callback_handler))
508                         debug_error("failed to _focus_add_sound_callback(%p)", _focus_callback_handler);
509         } else {
510                 if (!_focus_add_sound_callback(index, _focus_watch_callback_handler))
511                         debug_error("failed to _focus_add_sound_callback(%p)", _focus_watch_callback_handler);
512         }
513         debug_fleave();
514 }
515
516 static void _focus_remove_callback(int index)
517 {
518         debug_fenter();
519         if (!_focus_remove_sound_callback(index))
520                 debug_error("failed to __focus_remove_sound_callback()");
521         debug_fleave();
522 }
523
524 int focus_find_empty_index(int *index)
525 {
526         int i = 0;
527
528         if (!index)
529                 return -1;
530
531         for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
532                 if (g_focus_sound_handle[i].is_used == false) {
533                         g_focus_sound_handle[i].is_used = true;
534                         debug_log("use index[%d]", i);
535                         break;
536                 }
537         }
538         if (i == FOCUS_HANDLE_MAX) {
539                 debug_error("could not find empty slot, it is full");
540                 return -1;
541         }
542
543         *index = i;
544
545         return 0;
546 }
547
548 int focus_find_index_by_handle(int handle)
549 {
550         int i = 0;
551         for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
552                 if (g_focus_sound_handle[i].focus_callback && handle == g_focus_sound_handle[i].handle) {
553                         /* debug_msg("found index(%d) for handle(%d)", i, handle);*/
554                         return (handle == FOCUS_HANDLE_INIT_VAL) ? -1 : i;
555                 }
556         }
557         return -1;
558 }
559
560 int focus_watch_find_index_by_handle(int handle)
561 {
562         int i = 0;
563         for (i = 0; i < FOCUS_HANDLE_MAX; i++) {
564                 if (g_focus_sound_handle[i].watch_callback && handle == g_focus_sound_handle[i].handle) {
565                         /* debug_msg("found index(%d) for watch handle(%d)", i, handle);*/
566                         return (handle == FOCUS_HANDLE_INIT_VAL) ? -1 : i;
567                 }
568         }
569         return -1;
570 }
571
572 #define LOOP_RUNNING_WAIT_TIME_MS 2500
573 int focus_init_context(int index)
574 {
575         int ret = MM_ERROR_NONE;
576         GMainContext *focus_context;
577
578         debug_fenter();
579
580         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
581                 debug_error("index(%d) is not valid", index);
582                 return MM_ERROR_INVALID_ARGUMENT;
583         }
584
585         focus_context = g_main_context_new();
586         g_focus_sound_handle[index].focus_loop = g_main_loop_new(focus_context, FALSE);
587         g_main_context_unref(focus_context); /* FYI, this context resource will be released when calling g_main_loop_unref() */
588         if (g_focus_sound_handle[index].focus_loop == NULL) {
589                 debug_error("could not create mainloop..");
590                 goto ERROR;
591         }
592
593         g_focus_sound_handle[index].focus_cb_thread = g_thread_new("focus-cb-thread",
594                                                                         _focus_thread_func,
595                                                                         g_focus_sound_handle[index].focus_loop);
596         if (g_focus_sound_handle[index].focus_cb_thread == NULL) {
597                 debug_error("could not create thread..");
598                 goto ERROR;
599         }
600
601         debug_warning("focus cb thread[%p] with mainloop[%p] is created for index(%d)",
602                                 g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
603
604         if ((ret = _focus_loop_is_running_timed_wait(g_focus_sound_handle[index].focus_loop, LOOP_RUNNING_WAIT_TIME_MS))) {
605                 debug_error("failed to _focus_loop_is_running_timed_wait(), ret[0x%x]", ret);
606                 goto ERROR;
607         }
608
609         debug_fleave();
610
611         return MM_ERROR_NONE;
612
613 ERROR:
614         if (g_focus_sound_handle[index].focus_loop) {
615                 if (g_focus_sound_handle[index].focus_cb_thread) {
616                         g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
617                         g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
618                         debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
619                                         g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
620                         g_focus_sound_handle[index].focus_cb_thread = NULL;
621                 }
622                 g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
623                 g_focus_sound_handle[index].focus_loop = NULL;
624         }
625         return MM_ERROR_SOUND_INTERNAL;
626 }
627
628 void focus_deinit_context(int index)
629 {
630         debug_fenter();
631
632         if (index < 0 || index >= FOCUS_HANDLE_MAX) {
633                 debug_error("index(%d) is not valid", index);
634                 return;
635         }
636
637         if (!g_focus_sound_handle[index].focus_loop || !g_focus_sound_handle[index].focus_cb_thread) {
638                 debug_error("focus_loop[%p] or focus_cb_thread[%p] is null",
639                                 g_focus_sound_handle[index].focus_loop, g_focus_sound_handle[index].focus_cb_thread);
640                 return;
641         }
642
643         g_main_loop_quit(g_focus_sound_handle[index].focus_loop);
644         g_thread_join(g_focus_sound_handle[index].focus_cb_thread);
645         debug_warning("after thread join, thread[%p], mainloop[%p] for index(%d)",
646                         g_focus_sound_handle[index].focus_cb_thread, g_focus_sound_handle[index].focus_loop, index);
647         g_main_loop_unref(g_focus_sound_handle[index].focus_loop);
648         g_focus_sound_handle[index].focus_loop = NULL;
649         g_focus_sound_handle[index].focus_cb_thread = NULL;
650
651         debug_fleave();
652 }
653
654 void focus_init_callback(int index, bool is_for_watching)
655 {
656         debug_fenter();
657         _focus_open_callback(index, is_for_watching);
658         _focus_add_callback(index, is_for_watching);
659         debug_fleave();
660 }
661
662 void focus_deinit_callback(int index, bool is_for_watching)
663 {
664         debug_fenter();
665         _focus_remove_callback(index);
666         _focus_close_callback(index, is_for_watching);
667         debug_fleave();
668 }