Add signal handler for client process 51/145251/6
authorJeongmo Yang <jm80.yang@samsung.com>
Tue, 22 Aug 2017 01:40:10 +0000 (10:40 +0900)
committerJeongmo Yang <jm80.yang@samsung.com>
Wed, 23 Aug 2017 03:13:28 +0000 (12:13 +0900)
[Version] 0.3.3
[Profile] Common
[Issue Type] Update

Change-Id: I87549723d14be78fd45fae7e2bfb4775c280af6c
Signed-off-by: Jeongmo Yang <jm80.yang@samsung.com>
client/CMakeLists.txt
client/include/muse_client.h
client/src/muse_client.c
core/include/muse_core.h
core/src/muse_core.c
packaging/mused.spec

index 5a05a1b..3bae720 100644 (file)
@@ -32,6 +32,7 @@ ADD_DEFINITIONS("-DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"")
 ADD_DEFINITIONS("-DTIZEN_DEBUG")
 
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
+SET(CMAKE_SHARED_LINKER_FLAGS "-Wl,-init,_muse_client_constructor")
 
 aux_source_directory(src CLI_SOURCES)
 ADD_LIBRARY(${fw_name} SHARED ${CLI_SOURCES})
index 0471e1c..b078ffd 100644 (file)
@@ -30,6 +30,7 @@ extern "C" {
 
 int muse_client_new(void);
 int muse_client_new_data_ch(void);
+int muse_client_close(int sock_fd);
 int muse_client_ipc_push_data(int sock_fd, const char *data, int size, uint64_t data_id);
 int muse_client_get_fd_id_value(int sock_fd);
 bool muse_client_check_fd_id_value(int sock_fd, int sock_id);
index fba9b0c..59c3ab2 100644 (file)
 #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)
 {
@@ -73,11 +171,11 @@ 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;
@@ -85,22 +183,22 @@ static int _muse_client_new(muse_channel_e channel)
 
 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)
@@ -114,6 +212,29 @@ int muse_client_new_data_ch(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',};
index 8d4b6de..1bc892e 100644 (file)
@@ -85,7 +85,7 @@ typedef enum {
 
 extern const char *UDS_files[MUSE_CHANNEL_MAX];
 
-void muse_core_connection_close(int sock_fd);
+int muse_core_connection_close(int sock_fd);
 int muse_core_set_nonblocking(int fd, bool value);
 int muse_core_set_socket_timeout(int sock_fd, int timeout_sec);
 bool muse_core_fd_is_valid(int fd);
index a8017cc..4345bfe 100644 (file)
@@ -180,13 +180,13 @@ static json_object *_muse_core_msg_json_find_obj(json_object * jobj, const char
        return NULL;
 }
 
-void muse_core_connection_close(int sock_fd)
+int muse_core_connection_close(int sock_fd)
 {
        char err_msg[MUSE_MAX_MSG_LEN] = {'\0',};
 
        if (!muse_core_fd_is_valid(sock_fd)) {
                LOGE("[%d] invalid socket", sock_fd);
-               return;
+               return MM_ERROR_INVALID_ARGUMENT;
        }
 
        if (shutdown(sock_fd, SHUT_RDWR) == MUSE_ERR) {
@@ -200,6 +200,8 @@ void muse_core_connection_close(int sock_fd)
        }
 
        LOGI("[%d] connection close", sock_fd);
+
+       return MM_ERROR_NONE;
 }
 
 int muse_core_set_nonblocking(int fd, bool value)
index d866aef..1e4537b 100644 (file)
@@ -1,6 +1,6 @@
 Name:       mused
 Summary:    A multimedia daemon
-Version:    0.3.2
+Version:    0.3.3
 Release:    0
 Group:      System/Libraries
 License:    Apache-2.0