} while (0)
static bool _initialized = false;
-static GDBusConnection *__gdbus_conn = NULL;
+static GDBusConnection *__gdbus_conn;
static char *__app_id;
-static GHashTable *__local_port_info = NULL;
-static GHashTable *__remote_app_info = NULL;
-static GHashTable *__sender_appid_hash = NULL;
-static GHashTable *__trusted_app_list_hash = NULL;
+static GHashTable *__local_port_info;
+static GHashTable *__remote_app_info;
+static GHashTable *__sender_appid_hash;
+static GHashTable *__trusted_app_list_hash;
+static GHashTable *__callback_info_hash;
static const int MAX_MESSAGE_SIZE = 16 * 1024;
enum __certificate_info_type {
bool exist;
} port_list_info_s;
-static void __callback_info_free(message_port_callback_info_s *callback_info)
+static void __callback_info_free(gpointer data)
{
+ message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
GError *error = NULL;
if (callback_info == NULL)
return;
if (callback_info->remote_app_id)
- free(callback_info->remote_app_id);
+ FREE_AND_NULL(callback_info->remote_app_id);
if (callback_info->gio_read != NULL) {
g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
callback_info->g_src_id = 0;
}
- free(callback_info);
+ FREE_AND_NULL(callback_info);
+}
+
+static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
+{
+ GList *callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
+ GList *find_list;
+
+ if (callback_info_list == NULL)
+ return;
+
+ find_list = g_list_find(callback_info_list, callback_info);
+ if (find_list == NULL)
+ return;
+
+ callback_info_list = g_list_remove_link(callback_info_list, find_list);
+ __callback_info_free(callback_info);
+ g_list_free(find_list);
+}
+
+static void __hash_destroy_callback_info(gpointer data)
+{
+
+ GList *callback_list = (GList *)data;
+ if (callback_list != NULL)
+ g_list_free_full(callback_list, __callback_info_free);
}
static char *__get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted)
{
_LOGI("name vanished : %s", name);
port_list_info_s *pli = (port_list_info_s *)user_data;
- g_bus_unwatch_name(pli->watcher_id);
+ if (pli == NULL) {
+ LOGE("NULL port info");
+ return;
+ }
+
+ _LOGI("watcher_id :%d", pli->watcher_id);
+ if (pli->watcher_id > 0)
+ g_bus_unwatch_name(pli->watcher_id);
+ else
+ _LOGE("Invalid watcher_id %d", pli->watcher_id);
pli->exist = false;
pli->watcher_id = 0;
+
_LOGI("name vanished socket : %d", pli->send_sock_fd);
if (pli->send_sock_fd > 0) {
close(pli->send_sock_fd);
return remote_app_info;
}
+static void __watch_remote_port_info(port_list_info_s *port_info)
+{
+ if (port_info == NULL)
+ return;
+
+ if (port_info->watcher_id < 1) {
+ port_info->watcher_id = g_bus_watch_name_on_connection(
+ __gdbus_conn,
+ port_info->encoded_bus_name,
+ G_BUS_NAME_WATCHER_FLAGS_NONE,
+ on_name_appeared,
+ on_name_vanished,
+ port_info,
+ NULL);
+ } else {
+ LOGI("Already watched port info");
+ return;
+ }
+}
+
static int __get_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted,
message_port_remote_app_info_s **mri, port_list_info_s **pli)
{
} else {
*pli = (port_list_info_s *)cb_list->data;
}
- if ((*pli)->watcher_id < 1) {
- LOGI("watch remote port : %s", (*pli)->encoded_bus_name);
- (*pli)->watcher_id = g_bus_watch_name_on_connection(
- __gdbus_conn,
- (*pli)->encoded_bus_name,
- G_BUS_NAME_WATCHER_FLAGS_NONE,
- on_name_appeared,
- on_name_vanished,
- *pli,
- NULL);
- }
out:
return ret_val;
if (cond == G_IO_HUP) {
_LOGI("socket G_IO_HUP");
- __callback_info_free(mi);
+ __callback_info_free_by_info(mi);
return FALSE;
} else {
if ((fd = g_io_channel_unix_get_fd(gio)) < 0) {
_LOGE("fail to get fd from io channel");
- __callback_info_free(mi);
+ __callback_info_free_by_info(mi);
return FALSE;
}
if ((pkt = __message_port_recv_raw(fd)) == NULL) {
_LOGE("recv error on SOCKET");
- __callback_info_free(mi);
+ __callback_info_free_by_info(mi);
return FALSE;
}
message_port_local_port_info_s *mi;
int local_reg_id = 0;
message_port_callback_info_s *callback_info;
+ message_port_callback_info_s *head_callback_info;
+ GList *callback_info_list = NULL;
char buf[1024];
GDBusMessage *msg;
return -1;
}
+ callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(mi->local_id));
+ if (callback_info_list == NULL) {
+ head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
+ if (head_callback_info == NULL) {
+ _LOGE("fail to alloc head_callback_info");
+ __callback_info_free(callback_info);
+ return -1;
+ }
+ head_callback_info->local_id = 0;
+ head_callback_info->remote_app_id = NULL;
+ head_callback_info->callback = NULL;
+ head_callback_info->gio_read = NULL;
+ head_callback_info->g_src_id = 0;
+ callback_info_list = g_list_append(callback_info_list, head_callback_info);
+ callback_info_list = g_list_append(callback_info_list, callback_info);
+ g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(mi->local_id), callback_info_list);
+ } else {
+ callback_info_list = g_list_append(callback_info_list, callback_info);
+ }
}
}
port_info->exist = true;
*exist = true;
ret_val = MESSAGEPORT_ERROR_NONE;
+ __watch_remote_port_info(port_info);
}
}
out:
if (!remove_result)
_LOGE("Fail to remove sender appid from hash : %s", name);
- g_bus_unwatch_name(*watcher_id);
- free(watcher_id);
+ if (watcher_id) {
+ if (*watcher_id > 0)
+ g_bus_unwatch_name(*watcher_id);
+ else
+ LOGE("Invalid watcher_id %d", *watcher_id);
+ free(watcher_id);
+ } else {
+ LOGE("watcher_id is NULL");
+ }
}
static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
return registration_id;
}
-
+/* LCOV_EXCL_START */
void __list_free_port_list(gpointer data)
{
port_list_info_s *n = (port_list_info_s *)data;
FREE_AND_NULL(n->port_name);
FREE_AND_NULL(n);
}
+/* LCOV_EXCL_STOP */
+/* LCOV_EXCL_START */
static void __hash_destory_local_value(gpointer data)
{
message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
free(mli);
}
}
+/* LCOV_EXCL_STOP */
+/* LCOV_EXCL_START */
static void __hash_destory_remote_value(gpointer data)
{
message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
free(mri);
}
}
+/* LCOV_EXCL_STOP */
static bool __initialize(void)
{
retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
}
+ if (__callback_info_hash == NULL) {
+ __callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destroy_callback_info);
+ retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
+ }
+
if (!__dbus_init())
return false;
_initialized = true;
goto out;
}
}
+ __watch_remote_port_info(port_info);
if (port_info->send_sock_fd > 0) {
ret = __message_port_send_async(port_info->send_sock_fd, message,
g_dbus_message_set_unix_fd_list(msg, fd_list);
g_dbus_message_set_body(msg, body);
- g_dbus_message_set_flags(msg, G_DBUS_MESSAGE_FLAGS_NO_REPLY_EXPECTED);
g_dbus_connection_send_message(__gdbus_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
if (err != NULL) {
_LOGE("No reply. error = %s", err->message);
if (mi->is_trusted != trusted_port)
return MESSAGEPORT_ERROR_INVALID_PARAMETER;
+ g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
+
bus_name = __get_encoded_name(__app_id, mi->port_name, mi->is_trusted);
if (bus_name == NULL)
return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
return __message_send_bidirectional_message(id, remote_app_id, remote_port, true, message);
}
-int messageport_get_local_port_name(int id, char **name)
-{
- message_port_local_port_info_s *local_info;
- int ret = __get_local_port_info(id, &local_info);
-
- if (ret != MESSAGEPORT_ERROR_NONE)
- return ret;
-
- *name = strdup(local_info->port_name);
-
- if (*name == NULL)
- return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
-
- return MESSAGEPORT_ERROR_NONE;
-}
-
-int messageport_check_trusted_local_port(int id, bool *trusted)
-{
- message_port_local_port_info_s *local_info;
- int ret = __get_local_port_info(id, &local_info);
-
- if (ret != MESSAGEPORT_ERROR_NONE)
- return ret;
-
- *trusted = local_info->is_trusted;
-
- return MESSAGEPORT_ERROR_NONE;;
-}
-