#include <glib.h>
-GMainLoop *ms_db_get_mainloop(void);
+void ms_db_finalize(void);
gboolean ms_db_get_thread_status(void);
gpointer ms_db_thread(gpointer data);
#ifndef _MEDIA_SERVER_DCM_H_
#define _MEDIA_SERVER_DCM_H_
+void ms_dcm_finalize(void);
int ms_dcm_get_server_pid(void);
void ms_dcm_reset_server_status(void);
gpointer ms_dcm_agent_start_thread(gpointer data);
-
#endif /*_MEDIA_SERVER_DCM_H_*/
#define MAX_THUMB_REQUEST 100
-GMainLoop * ms_get_thumb_thread_mainloop(void);
+void ms_thumb_finalize(void);
int ms_thumb_get_server_pid(void);
void ms_thumb_reset_server_status(void);
gpointer ms_thumb_agent_start_thread(gpointer data);
#include "media-server-db.h"
static GMainLoop *g_db_mainloop = NULL;
+static guint g_db_source_id;
static bool db_thread_ready = false;
-GMainLoop *ms_db_get_mainloop(void)
+void ms_db_finalize(void)
{
- return g_db_mainloop;
+ if (g_main_loop_is_running(g_db_mainloop)) {
+ g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), g_db_source_id));
+ g_main_loop_quit(g_db_mainloop);
+ }
}
+
gboolean ms_db_get_thread_status(void)
{
return db_thread_ready;
context = g_main_context_new();
/*Init main loop*/
g_db_mainloop = g_main_loop_new(context, FALSE);
+ g_main_context_push_thread_default(context);
/* Create new channel to watch UDP socket */
channel = g_io_channel_unix_new(sockfd);
source = g_io_create_watch(channel, G_IO_IN);
+ g_io_channel_unref(channel);
/* Set callback to be called when socket is readable */
g_source_set_callback(source, (GSourceFunc)ms_read_db_tcp_socket, db_handle, NULL);
- g_source_attach(source, context);
-
- g_main_context_push_thread_default(context);
+ g_db_source_id = g_source_attach(source, context);
+ g_source_unref(source);
MS_DBG_INFO("Media Server DB thread is running");
db_thread_ready = true;
MS_DBG_INFO("Media Server DB thread is shutting down");
db_thread_ready = false;
-
- g_io_channel_shutdown(channel, FALSE, NULL);
- g_io_channel_unref(channel);
-
- /*close socket*/
close(sockfd);
-
g_main_loop_unref(g_db_mainloop);
return NULL;
static GMainLoop *g_dcm_agent_loop = NULL;
static gboolean g_folk_dcm_server = FALSE;
static gboolean g_shutdowning_dcm_server = FALSE;
-static int g_dcm_comm_sock = 0;
+static int g_dcm_comm_sock = -1;
static int g_dcm_timer_id = 0;
static int g_dcm_service_pid = 0;
+static guint g_dcm_source_id;
static GQueue *g_dcm_request_queue = NULL;
static gboolean g_dcm_queue_work = FALSE;
extern char MEDIA_IPC_PATH[][70];
+void ms_dcm_finalize(void)
+{
+ if (g_dcm_comm_sock != -1) {
+ close(g_dcm_comm_sock);
+ g_dcm_comm_sock = -1;
+ }
+
+ if (g_main_loop_is_running(g_dcm_agent_loop)) {
+ g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), g_dcm_source_id));
+ g_main_loop_quit(g_dcm_agent_loop);
+ }
+}
int ms_dcm_get_server_pid(void)
{
ERROR:
close(sock);
- return FALSE;
+ return G_SOURCE_REMOVE;
}
static gboolean __ms_dcm_agent_timer(gpointer data)
return FALSE;
}
-static void __ms_dcm_create_timer(int id)
+static void __ms_dcm_create_timer(void)
{
- if (id > 0)
- g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), id));
+ if (g_dcm_timer_id > 0)
+ g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), g_dcm_timer_id));
GSource *timer_src = g_timeout_source_new_seconds(MS_TIMEOUT_SEC_20);
g_source_set_callback(timer_src, __ms_dcm_agent_timer, NULL, NULL);
g_dcm_timer_id = g_source_attach(timer_src, g_main_context_get_thread_default());
-
+ g_source_unref(timer_src);
}
static gboolean __ms_dcm_request_to_server(gpointer data)
return G_SOURCE_REMOVE;
}
- __ms_dcm_create_timer(g_dcm_timer_id);
+ __ms_dcm_create_timer();
}
} else {
/* Timer is re-created*/
- __ms_dcm_create_timer(g_dcm_timer_id);
+ __ms_dcm_create_timer();
}
dcmRequest *req = NULL;
context = g_main_context_new();
- if (context == NULL)
- MS_DBG_ERR("g_main_context_new failed");
- else
- MS_DBG("g_main_context_new success");
-
g_dcm_agent_loop = g_main_loop_new(context, FALSE);
g_main_context_push_thread_default(context);
/* Create new channel to watch udp socket */
channel = g_io_channel_unix_new(sockfd);
source = g_io_create_watch(channel, G_IO_IN);
+ g_io_channel_unref(channel);
/* Set callback to be called when socket is readable */
g_source_set_callback(source, (GSourceFunc)__ms_dcm_agent_read_socket, NULL, NULL);
- g_source_attach(source, context);
+ g_dcm_source_id = g_source_attach(source, context);
+ g_source_unref(source);
MS_DBG_INFO("DCM Agent thread is running");
g_main_loop_run(g_dcm_agent_loop);
MS_DBG_INFO("DCM Agent thread is shutting down");
/*close an IO channel & remove resources */
- g_io_channel_shutdown(channel, FALSE, NULL);
- g_io_channel_unref(channel);
- close(g_dcm_comm_sock);
-
- g_main_loop_unref(g_dcm_agent_loop);
-
close(sockfd);
+ g_main_loop_unref(g_dcm_agent_loop);
return NULL;
}
#define SMARTHUB_RESET_VCONF "db/smarthub/reset/firstscreen"
bool smarthub_reset_start;
bool smarthub_reset;
+static guint main_source_id;
static void __ms_check_mediadb(void);
static void __ms_update_storage_status(void);
static void __ms_add_signal_handler(void);
-static void __ms_add_event_receiver(GIOChannel *channel);
-static void __ms_add_request_receiver(GMainLoop *mainloop, GIOChannel **channel);
+static void __ms_add_event_receiver(void);
static bool __ms_is_dcm_supported(void);
static bool __ms_is_external_supported(void);
#ifdef _USE_DEVICED_DBUS
static void __power_off_cb(void *data)
{
- int fd = -1;
MS_DBG_ERR("POWER OFF");
-
- GIOChannel *channel = (GIOChannel *)data;
-
power_off = true;
/*Quit Thumbnail Thread*/
- GMainLoop *thumb_mainloop = ms_get_thumb_thread_mainloop();
- if (thumb_mainloop && g_main_loop_is_running(thumb_mainloop))
- g_main_loop_quit(thumb_mainloop);
+ ms_thumb_finalize();
/*Quit DB Thread*/
- GMainLoop *db_mainloop = ms_db_get_mainloop();
- if (db_mainloop && g_main_loop_is_running(db_mainloop))
- g_main_loop_quit(db_mainloop);
-
- /*close an IO channel*/
- fd = g_io_channel_unix_get_fd(channel);
- g_io_channel_shutdown(channel, FALSE, NULL);
- g_io_channel_unref(channel);
-
- if (fd > 0) {
- if (close(fd) < 0)
- MS_DBG_STRERROR("CLOSE ERROR");
- }
+ ms_db_finalize();
+
+ /*Quit DCM Thread*/
+ ms_dcm_finalize();
+
+ g_source_remove(main_source_id);
ms_sys_unset_device_block_event_cb();
ms_sys_unset_poweroff_cb();
bool is_dcm_supported = __ms_is_dcm_supported();
power_off = false;
int ret = 0;
+ int sockfd = -1;
ret = ms_load_functions();
if (ret != MS_MEDIA_ERR_NONE) {
return -1;
}
- __ms_add_request_receiver(mainloop, &channel);
+ /*prepare socket*/
+ g_mkdir_with_parents(tzplatform_mkpath(TZ_SYS_RUN, "media-server"), S_IRWXU | S_IRWXG | S_IRWXO);
+
+ if (ms_ipc_create_server_socket(MS_SCANNER_PORT, &sockfd) != MS_MEDIA_ERR_NONE) {
+ MS_DBG_ERR("Failed to create socket");
+ return -1;
+ } else {
+ if (ms_cynara_enable_credentials_passing(sockfd) != MS_MEDIA_ERR_NONE)
+ MS_DBG_ERR("Failed to setup credential passing");
+
+ channel = g_io_channel_unix_new(sockfd);
+ g_io_channel_set_close_on_unref(channel, TRUE);
+ main_source_id = g_io_add_watch(channel, G_IO_IN, ms_read_socket, NULL);
+ g_io_channel_unref(channel);
+ }
/* recevie event from other modules */
- __ms_add_event_receiver(channel);
+ __ms_add_event_receiver();
__ms_add_signal_handler();
return 0;
}
-static void __ms_add_request_receiver(GMainLoop *mainloop, GIOChannel **channel)
-{
-
- int sockfd = -1;
- GSource *source = NULL;
- GMainContext *context = NULL;
-
- /*prepare socket*/
- /* create dir socket */
- g_mkdir_with_parents(tzplatform_mkpath(TZ_SYS_RUN, "media-server"), S_IRWXU | S_IRWXG | S_IRWXO);
-
- /* Create and bind new UDP socket */
- if (ms_ipc_create_server_socket(MS_SCANNER_PORT, &sockfd) != MS_MEDIA_ERR_NONE) {
- MS_DBG_ERR("Failed to create socket");
- return;
- } else {
- if (ms_cynara_enable_credentials_passing(sockfd) != MS_MEDIA_ERR_NONE)
- MS_DBG_ERR("Failed to setup credential passing");
-
- context = g_main_loop_get_context(mainloop);
-
- /* Create new channel to watch udp socket */
- *channel = g_io_channel_unix_new(sockfd);
- source = g_io_create_watch(*channel, G_IO_IN);
-
- /* Set callback to be called when socket is readable */
- g_source_set_callback(source, (GSourceFunc)ms_read_socket, *channel, NULL);
- g_source_attach(source, context);
- g_source_unref(source);
- }
-}
-
-static void __ms_add_event_receiver(GIOChannel *channel)
+static void __ms_add_event_receiver(void)
{
int err = 0;
#ifdef _USE_META_UPDATE
#endif
/*set power off callback function*/
#ifdef _USE_DEVICED_DBUS
- ms_sys_set_poweroff_cb(__power_off_cb, channel);
+ ms_sys_set_poweroff_cb(__power_off_cb, NULL);
#endif
ms_sys_set_device_block_event_cb(ms_device_block_changed_cb, NULL);
}
static GMainLoop *g_thumb_agent_loop = NULL;
static gboolean g_thumb_server_forked = FALSE;
static gboolean g_shutdowning_thumb_server = FALSE;
-static int g_communicate_sock = 0;
+static int g_communicate_sock = -1;
static int g_timer_id = 0;
static int g_server_pid = 0;
static GQueue *g_request_queue = NULL;
static gboolean g_queue_work = FALSE;
+static guint g_thumb_agent_source_id;
typedef struct {
int client_sock;
extern char MEDIA_IPC_PATH[][70];
-GMainLoop* ms_get_thumb_thread_mainloop(void)
+void ms_thumb_finalize(void)
{
- return g_thumb_agent_loop;
+ if (g_communicate_sock != -1) {
+ close(g_communicate_sock);
+ g_communicate_sock = -1;
+ }
+
+ if (g_main_loop_is_running(g_thumb_agent_loop)) {
+ g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), g_thumb_agent_source_id));
+ g_main_loop_quit(g_thumb_agent_loop);
+ }
}
int ms_thumb_get_server_pid(void)
MS_DBG_ERR("g_server_pid is %d. Maybe there's problem in thumbnail-server", g_server_pid);
}
- return FALSE;
+ return G_SOURCE_REMOVE;
}
-static void __ms_thumb_create_timer(int id)
+static void __ms_thumb_create_timer(void)
{
- if (id > 0)
- g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), id));
+ if (g_timer_id > 0)
+ g_source_destroy(g_main_context_find_source_by_id(g_main_context_get_thread_default(), g_timer_id));
GSource *timer_src = g_timeout_source_new_seconds(MS_TIMEOUT_SEC_20);
g_source_set_callback(timer_src, __ms_thumb_agent_timer, NULL, NULL);
g_timer_id = g_source_attach(timer_src, g_main_context_get_thread_default());
+ g_source_unref(timer_src);
}
static gboolean __ms_thumb_request_to_server(gpointer data)
return G_SOURCE_REMOVE;
}
}
- __ms_thumb_create_timer(g_timer_id);
+ __ms_thumb_create_timer();
req = (thumbRequest *)g_queue_pop_head(g_request_queue);
MS_DBG_RETVM_IF(!req, TRUE, "Failed to get a request job from queue");
}
context = g_main_context_new();
- if (!context) {
- MS_DBG_ERR("g_main_context_new failed");
- close(sockfd);
- return NULL;
- }
g_thumb_agent_loop = g_main_loop_new(context, FALSE);
g_main_context_push_thread_default(context);
/* Create new channel to watch udp socket */
channel = g_io_channel_unix_new(sockfd);
source = g_io_create_watch(channel, G_IO_IN);
+ g_io_channel_unref(channel);
/* Set callback to be called when socket is readable */
g_source_set_callback(source, (GSourceFunc)__ms_thumb_agent_read_socket, NULL, NULL);
- g_source_attach(source, context);
+ g_thumb_agent_source_id = g_source_attach(source, context);
+ g_source_unref(source);
MS_DBG_INFO("Thumbnail Agent thread is running");
g_main_loop_run(g_thumb_agent_loop);
MS_DBG_INFO("Thumbnail Agent thread is shutting down");
/*close an IO channel*/
- g_io_channel_shutdown(channel, FALSE, NULL);
- g_io_channel_unref(channel);
- close(g_communicate_sock);
-
- g_main_loop_unref(g_thumb_agent_loop);
-
close(sockfd);
+ g_main_loop_unref(g_thumb_agent_loop);
return NULL;
}