#include <aul.h>
#include <gio/gio.h>
#include <gio/gunixfdlist.h>
+#include <pthread.h>
+#include <glib-unix.h>
+#include <poll.h>
#include "message-port.h"
#include "message-port-log.h"
static GHashTable *__sender_appid_hash;
static GHashTable *__trusted_app_list_hash;
static GHashTable *__callback_info_hash;
+static GHashTable *__registered_callback_info_hash;
static const int MAX_MESSAGE_SIZE = 16 * 1024;
enum __certificate_info_type {
} message_port_local_port_info_s;
typedef struct message_port_remote_port_info {
- char *sender_id;
char *remote_app_id;
int certificate_info;
GList *port_list;
} message_port_remote_app_info_s;
typedef struct port_list_info {
+ message_port_remote_app_info_s *remote_app_info;
char *port_name;
char *encoded_bus_name;
bool is_trusted;
int send_sock_fd;
- int watcher_id;
bool exist;
GIOChannel *gio_read;
int g_src_id;
+ GList *delayed_message_list;
+ unsigned int delayed_message_size;
+ int delay_src_id;
} port_list_info_s;
+typedef struct registered_callback_info {
+ char *remote_app_id;
+ char *remote_port;
+ bool is_trusted;
+ int watcher_id;
+ void *user_data;
+ messageport_registration_event_cb registered_cb;
+ messageport_registration_event_cb unregistered_cb;
+} registered_callback_info_s;
+
+enum transmission_sequence {
+ SEQUENCE_START = 0,
+ SEQUENCE_PORT_LEN,
+ SEQUENCE_PORT_NAME,
+ SEQUENCE_BIDIRECTION,
+ SEQUENCE_TRUSTED,
+ SEQUENCE_DTAT_LEN,
+ SEQUENCE_DATA,
+ SEQUENCE_END
+};
+
+typedef struct delay_message {
+ unsigned int size;
+ unsigned int sent_bytes;
+ int sequence;
+ int local_port_len;
+ char *local_port_name;
+ bool is_bidirection;
+ bool local_trusted;
+ int data_len;
+ bundle_raw *data;
+} delay_message_info_s;
+
+
+extern pthread_mutex_t mutex;
+static void __free_list_delay_message_info(gpointer data);
+
+
static void __callback_info_free(gpointer data)
{
message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
g_list_free(find_list);
}
+static void __registered_callback_info_free(gpointer data)
+{
+ registered_callback_info_s *callback_info = (registered_callback_info_s *)data;
+ if (callback_info == NULL)
+ return;
+
+ if (callback_info->remote_app_id)
+ free(callback_info->remote_app_id);
+
+ if (callback_info->remote_port)
+ free(callback_info->remote_port);
+
+ free(callback_info);
+}
+
+
static void __hash_destroy_callback_info(gpointer data)
{
if (bus_name)
free(bus_name);
- _LOGI("encoded_bus_name : %s ", md5_interface);
+ _LOGD("encoded_bus_name : %s ", md5_interface);
return md5_interface;
}
static bool __is_preloaded(const char *local_appid, const char *remote_appid)
{
- _LOGI("IsPreloaded");
+ _LOGD("IsPreloaded");
bool preload_local = false;
bool preload_remote = false;
static int __check_certificate(const char *local_appid, const char *remote_appid)
{
- _LOGI("CheckCertificate");
+ _LOGD("CheckCertificate");
pkgmgrinfo_cert_compare_result_type_e res;
int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(local_appid, remote_appid, getuid(), &res);
return MESSAGEPORT_ERROR_NONE;
}
-static void on_name_appeared(GDBusConnection *connection,
- const gchar *name,
- const gchar *name_owner,
- gpointer user_data)
-{
- _LOGI("name appeared : %s %s", __app_id, name);
-}
-
-static void on_name_vanished(GDBusConnection *connection,
- const gchar *name,
- gpointer user_data)
-{
- _LOGI("name vanished : %s", name);
- port_list_info_s *pli = (port_list_info_s *)user_data;
- 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);
- pli->send_sock_fd = 0;
- }
-}
-
static int __get_local_port_info(int id, message_port_local_port_info_s **info)
{
message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
if (port_info == NULL)
return;
- _LOGI("__clear_disconnect_socket : fd [%d]", port_info->send_sock_fd);
if (port_info->gio_read != NULL) {
g_io_channel_shutdown(port_info->gio_read, TRUE, &error);
g_source_remove(port_info->g_src_id);
port_info->g_src_id = 0;
}
+
+ if (port_info->delay_src_id != 0) {
+ g_source_remove(port_info->delay_src_id);
+ port_info->delay_src_id = 0;
+ }
+
+ if (port_info->delayed_message_list != NULL) {
+ g_list_free_full(port_info->delayed_message_list, __free_list_delay_message_info);
+ /* can be reused */
+ port_info->delayed_message_list = NULL;
+ }
+
+ port_info->delayed_message_size = 0;
port_info->send_sock_fd = 0;
}
-static gboolean __socket_disconnect_handler(GIOChannel *gio,
- GIOCondition cond,
- gpointer data)
+/* LCOV_EXCL_START */
+void __free_port_info(gpointer data)
{
- /* It's sender socket's gio channel so, only EOF can be received */
port_list_info_s *port_info = (port_list_info_s *)data;
- _LOGI("__socket_disconnect_handler %d", cond);
- __clear_disconnect_socket(port_info);
- return FALSE;
-}
+ message_port_remote_app_info_s *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;
+ remote_app_info = port_info->remote_app_info;
+
+ _LOGI("__free_port_info : remote_app_id : %s port_name : %s",
+ remote_app_info->remote_app_id,
+ port_info->port_name);
+
+ remote_app_info->port_list = g_list_remove(remote_app_info->port_list,
+ port_info);
+
+ __clear_disconnect_socket(port_info);
+
+ if (port_info->encoded_bus_name)
+ free(port_info->encoded_bus_name);
+ if (port_info->port_name)
+ free(port_info->port_name);
+
+ free(port_info);
+
+ if (g_list_length(remote_app_info->port_list) == 0) {
+ g_hash_table_remove(__remote_app_info,
+ remote_app_info->remote_app_id);
}
}
+/* LCOV_EXCL_STOP */
+
+static gboolean __socket_disconnect_handler(GIOChannel *gio,
+ GIOCondition cond,
+ gpointer data)
+{
+ _LOGI("__socket_disconnect_handler %d", cond);
+ __free_port_info(data);
+
+ return FALSE;
+}
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)
*mri = remote_app_info;
port_info.port_name = strdup(remote_port);
+ if (port_info.port_name == NULL) {
+ ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
+ goto out;
+ }
port_info.is_trusted = is_trusted;
cb_list = g_list_find_custom(remote_app_info->port_list, &port_info,
(GCompareFunc)__remote_port_compare_cb);
goto out;
}
remote_app_info->port_list = g_list_append(remote_app_info->port_list, tmp);
+ tmp->remote_app_info = remote_app_info;
*pli = tmp;
} else {
*pli = (port_list_info_s *)cb_list->data;
static int __write_socket(int fd,
const char *buffer,
unsigned int nbytes,
- unsigned int *bytes_write)
+ unsigned int *bytes_write,
+ int *sequence)
{
+#define SEND_TIMEOUT 500 /* milliseconds */
+
unsigned int left = nbytes;
ssize_t nb;
int retry_cnt = 0;
+ struct pollfd fds[1];
+ int ret;
+ *sequence += 1;
*bytes_write = 0;
+
+ fds[0].fd = fd;
+ fds[0].events = POLLOUT;
+ fds[0].revents = 0;
+
+ ret = poll(fds, 1, SEND_TIMEOUT);
+ if (ret == 0) {
+ LOGE("__write_socket: : fd %d poll timeout", fd);
+ return MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
+ }
+
while (left && (retry_cnt < MAX_RETRY_CNT)) {
nb = write(fd, buffer, left);
if (nb == -1) {
continue;
}
LOGE("__write_socket: ...error fd %d: errno %d\n", fd, errno);
+
+ if (errno == EWOULDBLOCK || errno == EAGAIN)
+ return MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
+
return MESSAGEPORT_ERROR_IO_ERROR;
}
return MESSAGEPORT_ERROR_NONE;
}
-static int __write_string_to_socket(int fd, const char *buffer, int string_len)
+static int __write_string_to_socket(int fd,
+ const char *buffer,
+ int string_len,
+ unsigned int *bytes_write,
+ int *sequence)
{
- unsigned int nb;
- if (__write_socket(fd, (char *)&string_len, sizeof(string_len), &nb) != MESSAGEPORT_ERROR_NONE) {
+ int ret;
+
+ ret = __write_socket(fd, (char *)&string_len, sizeof(string_len),
+ bytes_write, sequence);
+ if (ret != MESSAGEPORT_ERROR_NONE) {
_LOGE("write string_len fail");
- return MESSAGEPORT_ERROR_IO_ERROR;
+ return ret;
}
if (string_len > 0) {
- if (__write_socket(fd, buffer, string_len, &nb) != MESSAGEPORT_ERROR_NONE) {
+ ret = __write_socket(fd, buffer, string_len, bytes_write, sequence);
+ if (ret != MESSAGEPORT_ERROR_NONE) {
_LOGE("wirte buffer fail");
- return MESSAGEPORT_ERROR_IO_ERROR;
+ return ret;
}
+ } else {
+ *sequence += 1;
}
+
return MESSAGEPORT_ERROR_NONE;
}
if (__read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGEPORT_ERROR_NONE) {
LOGE("read socket fail: data");
+ if (pkt->data)
+ free(pkt->data);
free(pkt->remote_port_name);
free(pkt);
return NULL;
bundle_raw *raw = NULL;
message_port_local_port_info_s *mi;
int local_reg_id = 0;
- message_port_callback_info_s *callback_info;
+ message_port_callback_info_s *callback_info = NULL;
message_port_callback_info_s *head_callback_info;
GList *callback_info_list = NULL;
int fd_len;
int *returned_fds = NULL;
int fd;
+ bool ret = false;
g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
&remote_appid, &remote_port, &remote_trusted, &len, &raw);
callback_info->local_id = mi->local_id;
callback_info->remote_app_id = strdup(local_appid);
+ if (callback_info->remote_app_id == NULL) {
+ _LOGE("out of memory");
+ goto out;
+ }
callback_info->callback = mi->callback;
msg = g_dbus_method_invocation_get_message(invocation);
returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
if (returned_fds == NULL) {
_LOGE("fail to get fds");
- __callback_info_free(callback_info);
- return false;
+ goto out;
}
fd = returned_fds[0];
callback_info->gio_read = g_io_channel_unix_new(fd);
if (!callback_info->gio_read) {
_LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
- __callback_info_free(callback_info);
- return false;
+ goto out;
}
callback_info->g_src_id = g_io_add_watch(callback_info->gio_read, G_IO_IN | G_IO_HUP,
__socket_request_handler, (gpointer)callback_info);
if (callback_info->g_src_id == 0) {
_LOGE("fail to add watch on socket");
- __callback_info_free(callback_info);
- return false;
+ goto out;
}
callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(mi->local_id));
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 false;
+ goto out;
}
head_callback_info->local_id = 0;
head_callback_info->remote_app_id = NULL;
goto out;
}
- LOGI("call calback %s", local_appid);
+ LOGD("call calback %s", local_appid);
if (bi_dir)
mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, NULL);
else
mi->callback(mi->local_id, local_appid, NULL, false, data, NULL);
bundle_free(data);
+
+ ret = true;
out:
+ if (ret == false)
+ __callback_info_free(callback_info);
+
if (returned_fds)
free(returned_fds);
- return true;
+ return ret;
}
static int __check_remote_port(const char *remote_app_id, const char *remote_port, bool is_trusted, bool *exist)
{
- _LOGI("Check a remote port : [%s:%s]", remote_app_id, remote_port);
+ _LOGD("Check a remote port : [%s:%s]", remote_app_id, remote_port);
GVariant *result = NULL;
GError *err = NULL;
message_port_local_port_info_s *mi = NULL;
gboolean name_exist = false;
- _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
+ _LOGD("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
ret_val = __get_remote_port_info(remote_app_id, remote_port, is_trusted, &remote_app_info, &port_info);
if (ret_val != MESSAGEPORT_ERROR_NONE)
/* self check */
if (strcmp(remote_app_id, __app_id) == 0) {
- _LOGI("__is_local_port_registed ");
+ _LOGD("__is_local_port_registed ");
if (!__is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
*exist = false;
else
*exist = true;
- _LOGI("__is_local_port_registed : %d ", *exist);
+ _LOGD("__is_local_port_registed : %d ", *exist);
return MESSAGEPORT_ERROR_NONE;
}
g_variant_get(result, "(b)", &name_exist);
if (!name_exist) {
- LOGE("Name not exist %s", bus_name);
+ _LOGI("Name not exist %s", bus_name);
*exist = false;
ret_val = MESSAGEPORT_ERROR_NONE;
} else {
port_info->exist = true;
*exist = true;
ret_val = MESSAGEPORT_ERROR_NONE;
- __watch_remote_port_info(port_info);
}
}
out:
if (result)
g_variant_unref(result);
+ if (ret_val != MESSAGEPORT_ERROR_NONE || !name_exist)
+ __free_port_info((gpointer)port_info);
+
return ret_val;
}
char *local_appid = NULL;
int pid = __get_sender_pid(conn, sender);
int *watcher_id = (int *)calloc(1, sizeof(int));
+ char *_sender;
+ retvm_if(!watcher_id, false, "Malloc failed");
ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
if (ret != AUL_R_OK) {
}
if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
- _LOGI("insert sender !!!!! %s", sender);
- g_hash_table_insert(__sender_appid_hash, (gpointer)strdup(sender), GINT_TO_POINTER(pid));
+ _LOGD("insert sender !!!!! %s", sender);
+ _sender = strdup(sender);
+ if (_sender == NULL) {
+ _LOGE("out of memory");
+ free(watcher_id);
+ return false;
+ }
+ g_hash_table_insert(__sender_appid_hash, (gpointer)_sender, GINT_TO_POINTER(pid));
*watcher_id = g_bus_watch_name_on_connection(
__gdbus_conn,
sender,
goto out;
}
- _LOGI("Acquiring the own name : %d", owner_id);
+ _LOGD("Acquiring the own name : %d", owner_id);
snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
&interface_vtable, NULL, NULL, NULL);
- _LOGI("registration_id %d", registration_id);
+ _LOGD("registration_id %d", registration_id);
if (registration_id == 0) {
_LOGE("Failed to g_dbus_connection_register_object");
}
/* LCOV_EXCL_START */
-void __list_free_port_list(gpointer data)
-{
- port_list_info_s *n = (port_list_info_s *)data;
-
- FREE_AND_NULL(n->encoded_bus_name);
- 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;
{
message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
if (mri) {
- FREE_AND_NULL(mri->sender_id);
FREE_AND_NULL(mri->remote_app_id);
if (mri->port_list)
- g_list_free_full(mri->port_list, __list_free_port_list);
+ g_list_free_full(mri->port_list, __free_port_info);
free(mri);
}
int ret = 0;
char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
- _LOGI("initialize");
ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
retvm_if(ret != AUL_R_OK, false, "Failed to get the application ID: %d", ret);
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");
+ retvm_if(!__callback_info_hash, false, "fail to create __callback_info_hash");
}
if (!__dbus_init())
static int __register_message_port(const char *local_port, bool is_trusted, messageport_message_cb callback)
{
- _SECURE_LOGI("Register a message port : [%s:%s]", __app_id, local_port);
+ _SECURE_LOGI("local_port : [%s:%s]", local_port, is_trusted ? "trusted" : "non-trusted");
int local_id = 0;
return local_id;
}
-int __message_port_send_async(int sockfd, bundle *kb, const char *local_port,
+static void __free_delay_message_info(delay_message_info_s *message)
+{
+ if (message != NULL) {
+ FREE_AND_NULL(message->local_port_name);
+ FREE_AND_NULL(message->data);
+ FREE_AND_NULL(message);
+ }
+}
+
+static void __free_list_delay_message_info(gpointer data)
+{
+ delay_message_info_s *message = (delay_message_info_s *)data;
+
+ if (message != NULL)
+ __free_delay_message_info(message);
+}
+
+static int __send_delayed_message(int sockfd, delay_message_info_s *message)
+{
+ unsigned int nb = 0;
+ int sequence = message->sequence - 1;
+ int ret = MESSAGEPORT_ERROR_NONE;
+ bool is_startline = true;
+ int offset = 0;
+
+ _LOGI("send_delayed_message : sockfd (%d) sequence(%d) sent byte(%d)",
+ sockfd, message->sequence, message->sent_bytes);
+
+ switch (message->sequence) {
+ case SEQUENCE_START:
+ sequence++;
+ is_startline = false;
+
+ case SEQUENCE_PORT_LEN:
+ if (is_startline)
+ offset = message->sent_bytes;
+
+ ret = __write_socket(sockfd, ((char *)&message->local_port_len) + offset,
+ sizeof(message->local_port_len) - offset, &nb, &sequence);
+ if (ret != MESSAGEPORT_ERROR_NONE) {
+ _LOGE("write local_port_len fail");
+ goto out;
+ }
+ offset = 0;
+ is_startline = false;
+
+ case SEQUENCE_PORT_NAME:
+ if (is_startline)
+ offset = message->sent_bytes;
+
+ if (message->local_port_len > 0)
+ ret = __write_socket(sockfd, message->local_port_name + offset,
+ message->local_port_len - offset , &nb, &sequence);
+ else
+ sequence++;
+
+ if (ret != MESSAGEPORT_ERROR_NONE) {
+ _LOGE("write local_port fail");
+ goto out;
+ }
+ offset = 0;
+ is_startline = false;
+
+ case SEQUENCE_BIDIRECTION:
+ if (is_startline)
+ offset = message->sent_bytes;
+
+ ret = __write_socket(sockfd, ((char *)&message->is_bidirection) + offset,
+ sizeof(message->is_bidirection) - offset, &nb, &sequence);
+ if (ret != MESSAGEPORT_ERROR_NONE) {
+ _LOGE("write is_bidirection fail");
+ goto out;
+ }
+ offset = 0;
+ is_startline = false;
+
+ case SEQUENCE_TRUSTED:
+ if (is_startline)
+ offset = message->sent_bytes;
+
+ ret = __write_socket(sockfd, ((char *)&message->local_trusted) + offset,
+ sizeof(message->local_trusted) - offset, &nb, &sequence);
+ if (ret != MESSAGEPORT_ERROR_NONE) {
+ _LOGE("write local_trusted fail");
+ goto out;
+ }
+ offset = 0;
+ is_startline = false;
+
+ case SEQUENCE_DTAT_LEN:
+ if (is_startline)
+ offset = message->sent_bytes;
+
+ ret = __write_socket(sockfd, ((char *)&message->data_len) + offset,
+ sizeof(message->data_len) - offset, &nb, &sequence);
+ if (ret != MESSAGEPORT_ERROR_NONE) {
+ _LOGE("write data_len fail");
+ goto out;
+ }
+ offset = 0;
+ is_startline = false;
+
+ case SEQUENCE_DATA:
+ if (is_startline)
+ offset = message->sent_bytes;
+
+ ret = __write_socket(sockfd, (char *)message->data + offset,
+ message->data_len -offset, &nb, &sequence);
+
+ if (ret != MESSAGEPORT_ERROR_NONE) {
+ _LOGE("write data fail");
+ goto out;
+ }
+ offset = 0;
+ is_startline = false;
+
+ default:
+ ret = MESSAGEPORT_ERROR_NONE;
+
+ }
+
+out:
+ if (ret == MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE) {
+ if (is_startline)
+ message->sent_bytes += nb;
+ else
+ message->sent_bytes = nb;
+
+ message->sequence = sequence;
+ _LOGE("send_delayed_message fail : sockfd (%d) sequence(%d) sent byte(%d)",
+ sockfd, message->sequence, message->sent_bytes);
+ }
+
+ return ret;
+
+}
+
+static gboolean __process_delayed_message(gint fd, GIOCondition cond, gpointer data)
+{
+ port_list_info_s *port_info = (port_list_info_s *)data;
+ delay_message_info_s *message;
+ int ret;
+
+ if (port_info == NULL)
+ return G_SOURCE_REMOVE;
+
+ pthread_mutex_lock(&mutex);
+
+ if (port_info->delayed_message_list == NULL) {
+ port_info->delayed_message_size = 0;
+ port_info->delay_src_id = 0;
+ pthread_mutex_unlock(&mutex);
+ return G_SOURCE_REMOVE;
+ } else {
+ message = g_list_nth_data(port_info->delayed_message_list, 0);
+ ret = __send_delayed_message(port_info->send_sock_fd, message);
+
+ if (ret == MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE) {
+ pthread_mutex_unlock(&mutex);
+ return G_SOURCE_CONTINUE;
+ } else if (ret == MESSAGEPORT_ERROR_IO_ERROR) {
+ __free_port_info((gpointer)port_info);
+ pthread_mutex_unlock(&mutex);
+ return G_SOURCE_REMOVE;
+ }
+
+ port_info->delayed_message_size -= message->size;
+
+ port_info->delayed_message_list = g_list_remove(port_info->delayed_message_list, message);
+ __free_delay_message_info(message);
+ }
+
+ pthread_mutex_unlock(&mutex);
+
+ return G_SOURCE_CONTINUE;
+}
+
+static int __insert_delayed_message(port_list_info_s *port_info,
+ int sequence,
+ bundle_raw *kb_data,
+ int data_len,
+ unsigned int sent_bytes,
+ const char *local_port,
+ bool local_trusted,
+ bool is_bidirection)
+{
+#define QUEUE_SIZE_MAX (1024 * 1024) /* 1MB per remote port (MAX) */
+
+ unsigned int tmp_size;
+ unsigned int message_size;
+ int ret = MESSAGEPORT_ERROR_NONE;
+
+ if (port_info->delayed_message_size >= QUEUE_SIZE_MAX) {
+ _LOGE("cache fail : delayed_message_size (%d), count(%d)",
+ port_info->delayed_message_size, g_list_length(port_info->delayed_message_list));
+ return MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
+ }
+
+ delay_message_info_s *message = (delay_message_info_s *)calloc(1, sizeof(delay_message_info_s));
+ retvm_if(!message, MESSAGEPORT_ERROR_OUT_OF_MEMORY, "Malloc failed");
+
+ message_size = sizeof(delay_message_info_s);
+
+ message->sequence = sequence;
+ tmp_size = strlen(local_port) + 1;
+ message_size += tmp_size;
+ message->local_port_len = tmp_size;
+ message->local_port_name = strdup(local_port);
+ if (message->local_port_name == NULL) {
+ _LOGE("local_port_name strdup fail");
+ ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
+ goto out;
+ }
+ message->is_bidirection = is_bidirection;
+ message->local_trusted = local_trusted;
+ message_size += data_len;
+ message->data_len = data_len;
+ message->data = (bundle_raw *)strdup((const char *)kb_data);
+ if (message->data == NULL) {
+ _LOGE("data strdup fail");
+ ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
+ goto out;
+ }
+
+
+ message->sent_bytes = sent_bytes;
+ message->size = message_size;
+ port_info->delayed_message_size += message_size;
+
+ port_info->delayed_message_list = g_list_append(port_info->delayed_message_list, message);
+
+ if (port_info->delay_src_id == 0) {
+ port_info->delay_src_id = g_unix_fd_add_full(G_PRIORITY_DEFAULT,
+ port_info->send_sock_fd, G_IO_OUT, __process_delayed_message,
+ port_info, NULL);
+ }
+
+ _LOGE("inserted : pm(%s) fd(%d) ms(%d) ds(%d) dlc(%d) sqn(%d) sb (%d)",
+ port_info->port_name, port_info->send_sock_fd, message_size,
+ port_info->delayed_message_size,
+ g_list_length(port_info->delayed_message_list), sequence, sent_bytes);
+
+
+
+out:
+ if (ret != MESSAGEPORT_ERROR_NONE)
+ __free_delay_message_info(message);
+
+ return ret;
+}
+
+int __message_port_send_async(port_list_info_s *port_info, bundle *kb, const char *local_port,
bool local_trusted, bool is_bidirection)
{
int ret = 0;
int data_len;
int local_port_len = 0;
- unsigned int nb;
+ unsigned int nb = 0;
bundle_raw *kb_data = NULL;
+ int sequence = SEQUENCE_START;
bundle_encode(kb, &kb_data, &data_len);
if (kb_data == NULL) {
_LOGE("bundle encode fail");
- ret = MESSAGEPORT_ERROR_IO_ERROR;
+ ret = MESSAGEPORT_ERROR_INVALID_PARAMETER;
goto out;
}
goto out;
}
+ if (g_list_length(port_info->delayed_message_list) > 0) {
+ ret = MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
+ _LOGE("There are messages in the delayed_message_list (count %d)",
+ g_list_length(port_info->delayed_message_list));
+ goto out;
+ }
+
if (local_port != NULL)
local_port_len = strlen(local_port) + 1;
- if (__write_string_to_socket(sockfd, local_port, local_port_len) != MESSAGEPORT_ERROR_NONE) {
+ ret = __write_string_to_socket(port_info->send_sock_fd, local_port,
+ local_port_len, &nb, &sequence);
+ if (ret != MESSAGEPORT_ERROR_NONE) {
_LOGE("write local_port fail");
- ret = MESSAGEPORT_ERROR_IO_ERROR;
goto out;
}
- if (__write_socket(sockfd, (char *)&is_bidirection, sizeof(is_bidirection), &nb) != MESSAGEPORT_ERROR_NONE) {
+ ret = __write_socket(port_info->send_sock_fd, (char *)&is_bidirection,
+ sizeof(is_bidirection), &nb, &sequence);
+ if (ret != MESSAGEPORT_ERROR_NONE) {
_LOGE("write is_bidirection fail");
- ret = MESSAGEPORT_ERROR_IO_ERROR;
goto out;
}
- if (__write_socket(sockfd, (char *)&local_trusted, sizeof(local_trusted), &nb) != MESSAGEPORT_ERROR_NONE) {
+ ret = __write_socket(port_info->send_sock_fd, (char *)&local_trusted,
+ sizeof(local_trusted), &nb, &sequence);
+ if (ret != MESSAGEPORT_ERROR_NONE) {
_LOGE("write local_trusted fail");
- ret = MESSAGEPORT_ERROR_IO_ERROR;
goto out;
}
- if (__write_string_to_socket(sockfd, (void *)kb_data, data_len) != MESSAGEPORT_ERROR_NONE) {
+ ret = __write_string_to_socket(port_info->send_sock_fd, (void *)kb_data,
+ data_len, &nb, &sequence);
+ if (ret != MESSAGEPORT_ERROR_NONE) {
_LOGE("write kb_data fail");
- ret = MESSAGEPORT_ERROR_IO_ERROR;
goto out;
}
+
out:
+ if (ret == MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE) {
+ ret = __insert_delayed_message(port_info, sequence, kb_data, data_len, nb,
+ local_port, local_trusted, is_bidirection);
+ if (ret != MESSAGEPORT_ERROR_NONE)
+ ret = MESSAGEPORT_ERROR_IO_ERROR;
+ }
+
if (kb_data)
free(kb_data);
if (port_info->exist == false) {
bool exist = false;
- _LOGI("port exist check !!");
+ _LOGD("port exist check !!");
ret = __check_remote_port(remote_appid, remote_port, trusted_message, &exist);
- if (ret != MESSAGEPORT_ERROR_NONE) {
- goto out;
- } else if (!exist) {
- ret = MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
- goto out;
- }
+ if (ret != MESSAGEPORT_ERROR_NONE)
+ return ret;
+ else if (!exist)
+ return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
}
if (port_info->send_sock_fd > 0) {
- ret = __message_port_send_async(port_info->send_sock_fd, message,
+ ret = __message_port_send_async(port_info, message,
(local_port) ? local_port : "", local_trusted, bi_dir);
} else {
goto out;
}
- port_info->g_src_id = g_io_add_watch(port_info->gio_read, G_IO_IN | G_IO_HUP,
- __socket_disconnect_handler, (gpointer)port_info);
+ port_info->g_src_id = g_io_add_watch(
+ port_info->gio_read,
+ G_IO_IN | G_IO_HUP,
+ __socket_disconnect_handler,
+ (gpointer)port_info);
if (port_info->g_src_id == 0) {
_LOGE("fail to add watch on socket");
ret = MESSAGEPORT_ERROR_IO_ERROR;
ret = MESSAGEPORT_ERROR_IO_ERROR;
goto out;
}
- __watch_remote_port_info(port_info);
-
}
out:
g_object_unref(fd_list);
if (ret != MESSAGEPORT_ERROR_NONE) {
- __clear_disconnect_socket(port_info);
+ __free_port_info((gpointer)port_info);
if (sock_pair[SOCK_PAIR_SENDER])
close(sock_pair[SOCK_PAIR_SENDER]);
if (sock_pair[SOCK_PAIR_RECEIVER])
if (ret != MESSAGEPORT_ERROR_NONE)
return ret;
- _LOGI("bidirectional_message %s", local_info->port_name);
+ _LOGD("bidirectional_message %s", local_info->port_name);
return __message_port_send_message(remote_app_id, remote_port,
local_info->port_name, trusted_message, local_info->is_trusted, true, message);
}
+static void __name_registered(GDBusConnection *connection,
+ const gchar *name,
+ const gchar *name_owner,
+ gpointer user_data)
+{
+
+ registered_callback_info_s *info = (registered_callback_info_s *)user_data;
+ if (info == NULL) {
+ LOGE("NULL registered_callback_info");
+ return;
+ }
+
+ _LOGI("watcher_id : %d, appeared name : %s , name_owner : %s\n", info->watcher_id, name, name_owner);
+ if (info->registered_cb)
+ info->registered_cb(info->remote_app_id, info->remote_port, info->is_trusted, info->user_data);
+}
+
+static void __name_unregistered(GDBusConnection *connection,
+ const gchar *name,
+ gpointer user_data)
+{
+
+ registered_callback_info_s *info = (registered_callback_info_s *)user_data;
+ if (info == NULL) {
+ LOGE("NULL registered_callback_info");
+ return;
+ }
+
+ _LOGI("watcher_id : %d, vanished name : %s\n", info->watcher_id, name);
+ if (info->unregistered_cb)
+ info->unregistered_cb(info->remote_app_id, info->remote_port, info->is_trusted, info->user_data);
+}
+
+int __messageport_watch_remote_port(int *watcher_id, const char *remote_app_id, const char *remote_port, bool trusted_remote_port, messageport_registration_event_cb registered_cb, messageport_registration_event_cb unregistered_cb, void *user_data)
+{
+ int ret_val = MESSAGEPORT_ERROR_NONE;
+ message_port_remote_app_info_s *remote_app_info = NULL;
+ port_list_info_s *port_info = NULL;
+
+ _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
+
+ ret_val = __get_remote_port_info(remote_app_id, remote_port, trusted_remote_port, &remote_app_info, &port_info);
+ if (ret_val != MESSAGEPORT_ERROR_NONE)
+ return ret_val;
+
+ if (__registered_callback_info_hash == NULL)
+ __registered_callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __registered_callback_info_free);
+
+ registered_callback_info_s *registered_cb_info = (registered_callback_info_s *)calloc(1, sizeof(registered_callback_info_s));
+ retvm_if(!registered_cb_info, MESSAGEPORT_ERROR_OUT_OF_MEMORY, "Malloc failed");
+
+ registered_cb_info->registered_cb = registered_cb;
+ registered_cb_info->unregistered_cb = unregistered_cb;
+ registered_cb_info->user_data = user_data;
+ registered_cb_info->remote_app_id = strdup(remote_app_info->remote_app_id);
+ if (registered_cb_info->remote_app_id == NULL) {
+ free(registered_cb_info);
+ return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
+ }
+ registered_cb_info->remote_port = strdup(port_info->port_name);
+ if (registered_cb_info->remote_port == NULL) {
+ free(registered_cb_info->remote_app_id);
+ free(registered_cb_info);
+ return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
+ }
+
+ registered_cb_info->watcher_id = g_bus_watch_name_on_connection(
+ __gdbus_conn,
+ port_info->encoded_bus_name,
+ G_BUS_NAME_WATCHER_FLAGS_NONE,
+ __name_registered,
+ __name_unregistered,
+ registered_cb_info,
+ NULL);
+ if (registered_cb_info->watcher_id == 0) {
+ free(registered_cb_info->remote_app_id);
+ free(registered_cb_info->remote_port);
+ free(registered_cb_info);
+ return MESSAGEPORT_ERROR_IO_ERROR;
+ }
+
+ g_hash_table_insert(__registered_callback_info_hash,
+ GINT_TO_POINTER(registered_cb_info->watcher_id), registered_cb_info);
+
+ *watcher_id = registered_cb_info->watcher_id;
+ return MESSAGEPORT_ERROR_NONE;
+}
+
int messageport_unregister_local_port(int local_port_id, bool trusted_port)
{
return __message_send_bidirectional_message(id, remote_app_id, remote_port, true, message);
}
+int messageport_add_registered_cb(const char *remote_app_id, const char *remote_port, bool is_trusted, messageport_registration_event_cb registered_cb, void *user_data, int *watcher_id)
+{
+ if (!_initialized) {
+ if (!__initialize())
+ return MESSAGEPORT_ERROR_IO_ERROR;
+ }
+ return __messageport_watch_remote_port(watcher_id, remote_app_id, remote_port, is_trusted, registered_cb, NULL, user_data);
+}
+
+int messageport_add_unregistered_cb(const char *remote_app_id, const char *remote_port, bool is_trusted, messageport_registration_event_cb unregistered_cb, void *user_data, int *watcher_id)
+{
+ if (!_initialized) {
+ if (!__initialize())
+ return MESSAGEPORT_ERROR_IO_ERROR;
+ }
+ return __messageport_watch_remote_port(watcher_id, remote_app_id, remote_port, is_trusted, NULL, unregistered_cb, user_data);
+}
+
+
+int messageport_remove_registration_event_cb(int watcher_id)
+{
+ registered_callback_info_s *registered_cb_info = NULL;
+ gboolean remove_result = FALSE;
+
+ if (watcher_id < 1)
+ return MESSAGEPORT_ERROR_INVALID_PARAMETER;
+
+ registered_cb_info = g_hash_table_lookup(__registered_callback_info_hash, GINT_TO_POINTER(watcher_id));
+ if (registered_cb_info == NULL)
+ return MESSAGEPORT_ERROR_INVALID_PARAMETER;
+
+ remove_result = g_hash_table_remove(__registered_callback_info_hash, GINT_TO_POINTER(watcher_id));
+ if (!remove_result)
+ return MESSAGEPORT_ERROR_IO_ERROR;
+
+ g_bus_unwatch_name(watcher_id);
+
+ return MESSAGEPORT_ERROR_NONE;
+}