#include "muse_client.h"
#include "muse_core_internal.h"
-GMutex g_mutex;
-GHashTable *g_table;
-int g_table_id;
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG "MUSED_CLIENT"
+
+/* signal handler */
+struct sigaction muse_client_int_old_action;
+struct sigaction muse_client_abrt_old_action;
+struct sigaction muse_client_segv_old_action;
+struct sigaction muse_client_term_old_action;
+struct sigaction muse_client_sys_old_action;
+static void _muse_client_sigaction(int signo, siginfo_t *siginfo, void *context);
+static void _muse_client_constructor() __attribute__((constructor));
+
+GMutex g_muse_client_mutex;
+GHashTable *g_muse_client_table;
+int g_muse_client_table_id;
const char *UDS_files[MUSE_CHANNEL_MAX] = {MUSE_SOCK_FILE0, MUSE_SOCK_FILE1};
static int _muse_client_new(muse_channel_e channel);
static void _muse_client_table_new(void);
static gpointer _muse_client_get_fd_ptr(int sock_fd);
+static gboolean _muse_client_table_remove_func(gpointer key, gpointer value, gpointer user_data);
+
+
+static gboolean _muse_client_table_remove_func(gpointer key, gpointer value, gpointer user_data)
+{
+ int sock_fd = GPOINTER_TO_INT(key);
+
+ LOGW("close muse connection fd %d", sock_fd);
+
+ muse_core_connection_close(sock_fd);
+
+ return TRUE;
+}
+
+static void _muse_client_sigaction(int signo, siginfo_t *siginfo, void *context)
+{
+ struct sigaction *old_action = NULL;
+ pid_t my_pid = getpid();
+
+ LOGE("Enter - signo [%d], pid [%d]", signo, my_pid);
+
+ /* close all opened fds */
+ g_mutex_lock(&g_muse_client_mutex);
+
+ g_hash_table_foreach_remove(g_muse_client_table,
+ _muse_client_table_remove_func,
+ NULL);
+
+ g_mutex_unlock(&g_muse_client_mutex);
+
+ /* call old signal handler */
+ switch (signo) {
+ case SIGINT:
+ old_action = &muse_client_int_old_action;
+ break;
+ case SIGABRT:
+ old_action = &muse_client_abrt_old_action;
+ break;
+ case SIGSEGV:
+ old_action = &muse_client_segv_old_action;
+ break;
+ case SIGTERM:
+ old_action = &muse_client_term_old_action;
+ break;
+ case SIGSYS:
+ old_action = &muse_client_sys_old_action;
+ break;
+ default:
+ LOGE("unhandled signal %d", signo);
+ return;
+ }
+
+ if (old_action->sa_sigaction)
+ old_action->sa_sigaction(signo, siginfo, context);
+ else
+ sigaction(signo, old_action, NULL);
+
+ LOGE("Leave");
+
+ return;
+}
+
+
+static void _muse_client_constructor()
+{
+ struct sigaction muse_client_action;
+ muse_client_action.sa_sigaction = _muse_client_sigaction;
+ muse_client_action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
+
+ LOGI("Enter");
+
+ sigemptyset(&muse_client_action.sa_mask);
+
+ sigaction(SIGINT, &muse_client_action, &muse_client_int_old_action);
+ sigaction(SIGABRT, &muse_client_action, &muse_client_abrt_old_action);
+ sigaction(SIGSEGV, &muse_client_action, &muse_client_segv_old_action);
+ sigaction(SIGTERM, &muse_client_action, &muse_client_term_old_action);
+ sigaction(SIGSYS, &muse_client_action, &muse_client_sys_old_action);
+
+ LOGI("Leave");
+
+ return;
+}
+
static int _muse_client_new(muse_channel_e channel)
{
return ret;
}
- g_mutex_lock(&g_mutex);
+ g_mutex_lock(&g_muse_client_mutex);
- g_hash_table_insert(g_table, GINT_TO_POINTER(sockfd), GINT_TO_POINTER(g_table_id++));
+ g_hash_table_insert(g_muse_client_table, GINT_TO_POINTER(sockfd), GINT_TO_POINTER(g_muse_client_table_id++));
- g_mutex_unlock(&g_mutex);
+ g_mutex_unlock(&g_muse_client_mutex);
LOGI("Leave");
return sockfd;
static void _muse_client_table_new(void)
{
- g_mutex_lock(&g_mutex);
+ g_mutex_lock(&g_muse_client_mutex);
- if (!g_table) {
- g_table_id = 1;
- g_table = g_hash_table_new(g_direct_hash, g_direct_equal);
- LOGD("client table : %p", g_table);
+ if (!g_muse_client_table) {
+ g_muse_client_table_id = 1;
+ g_muse_client_table = g_hash_table_new(g_direct_hash, g_direct_equal);
+ LOGD("client table : %p", g_muse_client_table);
}
- g_mutex_unlock(&g_mutex);
+ g_mutex_unlock(&g_muse_client_mutex);
}
static gpointer _muse_client_get_fd_ptr(int sock_fd)
{
- g_return_val_if_fail(g_table, NULL);
+ g_return_val_if_fail(g_muse_client_table, NULL);
- return g_hash_table_lookup(g_table, GINT_TO_POINTER(sock_fd));
+ return g_hash_table_lookup(g_muse_client_table, GINT_TO_POINTER(sock_fd));
}
int muse_client_new(void)
return _muse_client_new(MUSE_CHANNEL_DATA);
}
+int muse_client_close(int sock_fd)
+{
+ int ret = MM_ERROR_NONE;
+
+ ret = muse_core_connection_close(sock_fd);
+ if (ret != MM_ERROR_NONE) {
+ LOGE("close connection failed 0x%x", ret);
+ return ret;
+ }
+
+ /* remove fd from table */
+ g_mutex_lock(&g_muse_client_mutex);
+
+ if (g_hash_table_remove(g_muse_client_table, GINT_TO_POINTER(sock_fd)))
+ LOGD("success : remove fd %d from table", sock_fd);
+ else
+ LOGW("fail : remove fd %d from table[%p]", sock_fd, g_muse_client_table);
+
+ g_mutex_unlock(&g_muse_client_mutex);
+
+ return MM_ERROR_NONE;
+}
+
int muse_client_ipc_push_data(int sock_fd, const char *data, int size, uint64_t data_id)
{
char err_msg[MUSE_MAX_MSG_LEN] = {'\0',};