3 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * @file message-port.cpp
20 * @brief This is the implementation file for the MessagePort.
24 #include <sys/socket.h>
30 #include <openssl/md5.h>
32 #include <bundle_internal.h>
33 #include <pkgmgr-info.h>
36 #include <gio/gunixfdlist.h>
38 #include <glib-unix.h>
41 #include "message-port.h"
42 #include "message-port-log.h"
44 #define MAX_PACKAGE_STR_SIZE 512
45 #define MESSAGEPORT_BUS_NAME_PREFIX "org.tizen.messageport._"
46 #define MESSAGEPORT_OBJECT_PATH "/org/tizen/messageport"
47 #define MESSAGEPORT_INTERFACE_PREFIX "org.tizen.messageport._"
49 #define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
50 #define DBUS_PATH_DBUS "/org/freedesktop/DBus"
51 #define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
53 #define DBUS_RELEASE_NAME_REPLY_RELEASED 1 /* *< Service was released from the given name */
54 #define DBUS_RELEASE_NAME_REPLY_NON_EXISTENT 2 /* *< The given name does not exist on the bus */
55 #define DBUS_RELEASE_NAME_REPLY_NOT_OWNER 3 /* *< Service is not an owner of the given name */
57 #define MAX_RETRY_CNT 10
58 #define SOCK_PAIR_SENDER 0
59 #define SOCK_PAIR_RECEIVER 1
62 #define retvm_if(expr, val, fmt, arg...) do { \
65 _LOGE("(%s) -> %s() return", #expr, __func__); \
70 #define retv_if(expr, val) do { \
72 _LOGE("(%s) -> %s() return", #expr, __func__); \
77 #define FREE_AND_NULL(ptr) do { \
84 static bool _initialized = false;
85 static GDBusConnection *__gdbus_conn;
86 static char *__app_id;
87 static GHashTable *__local_port_info;
88 static GHashTable *__remote_app_info;
89 static GHashTable *__sender_appid_hash;
90 static GHashTable *__trusted_app_list_hash;
91 static GHashTable *__callback_info_hash;
92 static GHashTable *__registered_callback_info_hash;
93 static const int MAX_MESSAGE_SIZE = 16 * 1024;
95 enum __certificate_info_type {
98 CERTIFICATE_NOT_MATCH,
101 typedef struct message_port_pkt {
102 int remote_port_name_len;
103 char *remote_port_name;
108 } message_port_pkt_s;
110 typedef struct message_port_callback_info {
111 messageport_message_cb callback;
114 GIOChannel *gio_read;
116 } message_port_callback_info_s;
118 typedef struct message_port_local_port_info {
119 messageport_message_cb callback;
123 } message_port_local_port_info_s;
125 typedef struct message_port_remote_port_info {
128 int certificate_info;
130 } message_port_remote_app_info_s;
132 typedef struct port_list_info {
134 char *encoded_bus_name;
139 GIOChannel *gio_read;
141 GList *delayed_message_list;
142 unsigned int delayed_message_size;
146 typedef struct registered_callback_info {
152 messageport_registration_event_cb registered_cb;
153 messageport_registration_event_cb unregistered_cb;
154 } registered_callback_info_s;
156 enum transmission_sequence {
160 SEQUENCE_BIDIRECTION,
167 typedef struct delay_message {
169 unsigned int sent_bytes;
172 char *local_port_name;
177 } delay_message_info_s;
180 extern pthread_mutex_t mutex;
181 static void __free_list_delay_message_info(gpointer data);
184 static void __callback_info_free(gpointer data)
186 message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
187 GError *error = NULL;
188 if (callback_info == NULL)
191 if (callback_info->remote_app_id)
192 FREE_AND_NULL(callback_info->remote_app_id);
194 if (callback_info->gio_read != NULL) {
195 g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
197 _LOGE("g_io_channel_shutdown error : %s", error->message);
200 g_io_channel_unref(callback_info->gio_read);
201 callback_info->gio_read = NULL;
204 if (callback_info->g_src_id != 0) {
205 g_source_remove(callback_info->g_src_id);
206 callback_info->g_src_id = 0;
209 FREE_AND_NULL(callback_info);
212 static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
214 GList *callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
217 if (callback_info_list == NULL)
220 find_list = g_list_find(callback_info_list, callback_info);
221 if (find_list == NULL)
224 callback_info_list = g_list_remove_link(callback_info_list, find_list);
225 __callback_info_free(callback_info);
226 g_list_free(find_list);
229 static void __registered_callback_info_free(gpointer data)
231 registered_callback_info_s *callback_info = (registered_callback_info_s *)data;
232 if (callback_info == NULL)
235 if (callback_info->remote_app_id)
236 free(callback_info->remote_app_id);
238 if (callback_info->remote_port)
239 free(callback_info->remote_port);
245 static void __hash_destroy_callback_info(gpointer data)
248 GList *callback_list = (GList *)data;
249 if (callback_list != NULL)
250 g_list_free_full(callback_list, __callback_info_free);
253 static char *__get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted)
256 int prefix_len = strlen(MESSAGEPORT_BUS_NAME_PREFIX);
258 char *postfix = is_trusted ? "1" : "0";
260 unsigned char c[MD5_DIGEST_LENGTH] = {0};
261 char *md5_interface = NULL;
265 int encoded_bus_name_len = prefix_len + postfix_len + (MD5_DIGEST_LENGTH * 2) + 2;
266 int bus_name_len = strlen(remote_app_id) + strlen(port_name) + 2;
267 char *bus_name = (char *)calloc(bus_name_len, sizeof(char));
268 if (bus_name == NULL) {
269 _LOGE("bus_name calloc failed");
273 snprintf(bus_name, bus_name_len, "%s_%s", remote_app_id, port_name);
275 MD5_Init(&mdContext);
276 MD5_Update(&mdContext, bus_name, bus_name_len);
277 MD5_Final(c, &mdContext);
279 md5_interface = (char *)calloc(encoded_bus_name_len , sizeof(char));
280 if (md5_interface == NULL) {
284 _LOGE("md5_interface calloc failed!!");
288 snprintf(md5_interface, encoded_bus_name_len, "%s", MESSAGEPORT_BUS_NAME_PREFIX);
289 temp = md5_interface;
292 for (index = 0; index < MD5_DIGEST_LENGTH; index++) {
293 snprintf(temp, 3, "%02x", c[index]);
297 if (postfix && postfix_len > 0)
298 snprintf(temp, encoded_bus_name_len - (temp - md5_interface), "%s", postfix);
302 _LOGD("encoded_bus_name : %s ", md5_interface);
304 return md5_interface;
307 static int __remote_port_compare_cb(gconstpointer a, gconstpointer b)
309 port_list_info_s *key1 = (port_list_info_s *)a;
310 port_list_info_s *key2 = (port_list_info_s *)b;
312 if (key1->is_trusted == key2->is_trusted)
313 return strcmp(key1->port_name, key2->port_name);
319 static bool __is_preloaded(const char *local_appid, const char *remote_appid)
321 _LOGD("IsPreloaded");
323 bool preload_local = false;
324 bool preload_remote = false;
326 pkgmgrinfo_appinfo_h handle = NULL;
327 int ret = pkgmgrinfo_appinfo_get_usr_appinfo(local_appid, getuid(), &handle);
328 if (ret != PMINFO_R_OK) {
329 _LOGE("Failed to get the appinfo. %d", ret);
330 pkgmgrinfo_appinfo_destroy_appinfo(handle);
333 ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_local);
334 if (ret != PMINFO_R_OK) {
335 _LOGE("Failed to check the preloaded application. %d", ret);
336 pkgmgrinfo_appinfo_destroy_appinfo(handle);
339 pkgmgrinfo_appinfo_destroy_appinfo(handle);
341 ret = pkgmgrinfo_appinfo_get_usr_appinfo(remote_appid, getuid(), &handle);
342 if (ret != PMINFO_R_OK) {
343 _LOGE("Failed to get the appinfo. %d", ret);
344 pkgmgrinfo_appinfo_destroy_appinfo(handle);
347 ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_remote);
348 if (ret != PMINFO_R_OK) {
349 _LOGE("Failed to check the preloaded application. %d", ret);
350 pkgmgrinfo_appinfo_destroy_appinfo(handle);
354 if (preload_local && preload_remote) {
355 pkgmgrinfo_appinfo_destroy_appinfo(handle);
358 pkgmgrinfo_appinfo_destroy_appinfo(handle);
362 static int __check_certificate(const char *local_appid, const char *remote_appid)
364 _LOGD("CheckCertificate");
366 pkgmgrinfo_cert_compare_result_type_e res;
367 int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(local_appid, remote_appid, getuid(), &res);
369 _LOGE(":CheckCertificate() Failed");
370 return MESSAGEPORT_ERROR_IO_ERROR;
372 if (res != PMINFO_CERT_COMPARE_MATCH) {
373 _LOGE("CheckCertificate() Failed : MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH");
374 return MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
377 return MESSAGEPORT_ERROR_NONE;
380 static void on_name_appeared(GDBusConnection *connection,
382 const gchar *name_owner,
385 _LOGD("name appeared : %s %s", __app_id, name);
388 static void on_name_vanished(GDBusConnection *connection,
392 _LOGI("name vanished : %s", name);
393 port_list_info_s *pli = (port_list_info_s *)user_data;
395 LOGE("NULL port info");
399 _LOGD("watcher_id :%d", pli->watcher_id);
400 if (pli->watcher_id > 0)
401 g_bus_unwatch_name(pli->watcher_id);
403 _LOGE("Invalid watcher_id %d", pli->watcher_id);
409 static int __get_local_port_info(int id, message_port_local_port_info_s **info)
411 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
414 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
417 return MESSAGEPORT_ERROR_NONE;
420 static port_list_info_s *__set_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
422 int ret_val = MESSAGEPORT_ERROR_NONE;
423 port_list_info_s *port_info = (port_list_info_s *)calloc(1, sizeof(port_list_info_s));
426 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
429 port_info->port_name = strdup(remote_port);
430 if (!port_info->port_name) {
431 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
434 port_info->is_trusted = is_trusted;
435 port_info->encoded_bus_name = __get_encoded_name(remote_app_id, remote_port, is_trusted);
436 if (port_info->encoded_bus_name == NULL) {
437 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
440 port_info->send_sock_fd = 0;
442 if (ret_val != MESSAGEPORT_ERROR_NONE) {
444 FREE_AND_NULL(port_info->port_name);
445 FREE_AND_NULL(port_info->encoded_bus_name);
453 static message_port_remote_app_info_s *__set_remote_app_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
455 message_port_remote_app_info_s *remote_app_info = NULL;
456 int ret_val = MESSAGEPORT_ERROR_NONE;
458 remote_app_info = (message_port_remote_app_info_s *)calloc(1, sizeof(message_port_remote_app_info_s));
459 if (!remote_app_info) {
460 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
464 remote_app_info->remote_app_id = strdup(remote_app_id);
465 if (remote_app_info->remote_app_id == NULL) {
466 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;;
471 if (ret_val != MESSAGEPORT_ERROR_NONE) {
472 if (remote_app_info) {
473 FREE_AND_NULL(remote_app_info->remote_app_id);
474 FREE_AND_NULL(remote_app_info);
478 return remote_app_info;
481 static void __clear_disconnect_socket(port_list_info_s *port_info)
483 GError *error = NULL;
485 if (port_info == NULL)
487 _LOGI("__clear_disconnect_socket : fd [%d]", port_info->send_sock_fd);
489 if (port_info->gio_read != NULL) {
490 g_io_channel_shutdown(port_info->gio_read, TRUE, &error);
492 _LOGE("g_io_channel_shutdown error : %s", error->message);
495 g_io_channel_unref(port_info->gio_read);
496 port_info->gio_read = NULL;
499 if (port_info->g_src_id != 0) {
500 g_source_remove(port_info->g_src_id);
501 port_info->g_src_id = 0;
504 if (port_info->delay_src_id != 0) {
505 g_source_remove(port_info->delay_src_id);
506 port_info->delay_src_id = 0;
509 if (port_info->delayed_message_list != NULL) {
510 g_list_free_full(port_info->delayed_message_list, __free_list_delay_message_info);
512 port_info->delayed_message_list = NULL;
515 port_info->delayed_message_size = 0;
516 port_info->send_sock_fd = 0;
519 static gboolean __socket_disconnect_handler(GIOChannel *gio,
523 /* It's sender socket's gio channel so, only EOF can be received */
524 port_list_info_s *port_info = (port_list_info_s *)data;
525 _LOGI("__socket_disconnect_handler %d", cond);
526 __clear_disconnect_socket(port_info);
530 static void __watch_remote_port_info(port_list_info_s *port_info)
532 if (port_info == NULL)
535 if (port_info->watcher_id < 1) {
536 port_info->watcher_id = g_bus_watch_name_on_connection(
538 port_info->encoded_bus_name,
539 G_BUS_NAME_WATCHER_FLAGS_NONE,
545 LOGD("Already watched port info");
550 static int __get_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted,
551 message_port_remote_app_info_s **mri, port_list_info_s **pli)
553 message_port_remote_app_info_s *remote_app_info = NULL;
554 port_list_info_s port_info;
555 GList *cb_list = NULL;
556 int ret_val = MESSAGEPORT_ERROR_NONE;
558 remote_app_info = (message_port_remote_app_info_s *)g_hash_table_lookup(__remote_app_info, remote_app_id);
560 if (remote_app_info == NULL) {
561 remote_app_info = __set_remote_app_info(remote_app_id, remote_port, is_trusted);
563 if (remote_app_info == NULL) {
564 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
567 g_hash_table_insert(__remote_app_info, remote_app_info->remote_app_id, remote_app_info);
569 *mri = remote_app_info;
571 port_info.port_name = strdup(remote_port);
572 port_info.is_trusted = is_trusted;
573 cb_list = g_list_find_custom(remote_app_info->port_list, &port_info,
574 (GCompareFunc)__remote_port_compare_cb);
575 if (port_info.port_name)
576 free(port_info.port_name);
577 if (cb_list == NULL) {
578 port_list_info_s *tmp = __set_remote_port_info(remote_app_id, remote_port, is_trusted);
580 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
583 remote_app_info->port_list = g_list_append(remote_app_info->port_list, tmp);
586 *pli = (port_list_info_s *)cb_list->data;
593 static bool __is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi)
598 g_hash_table_iter_init(&iter, __local_port_info);
599 while (g_hash_table_iter_next(&iter, &key, &value)) {
600 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)value;
602 if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
603 *local_id = mi->local_id;
612 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
614 GDBusMessage *msg = NULL;
615 GDBusMessage *reply = NULL;
620 msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
621 "org.freedesktop.DBus", "GetConnectionUnixProcessID");
623 _LOGE("Can't allocate new method call");
627 g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
628 reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
629 G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
633 _LOGE("Failed to get pid [%s]", err->message);
639 body = g_dbus_message_get_body(reply);
640 g_variant_get(body, "(u)", &pid);
646 g_object_unref(reply);
651 static int __write_socket(int fd,
654 unsigned int *bytes_write,
657 #define SEND_TIMEOUT 500 /* milliseconds */
659 unsigned int left = nbytes;
662 struct pollfd fds[1];
669 fds[0].events = POLLOUT;
672 ret = poll(fds, 1, SEND_TIMEOUT);
674 LOGE("__write_socket: : fd %d poll timeout", fd);
675 return MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
678 while (left && (retry_cnt < MAX_RETRY_CNT)) {
679 nb = write(fd, buffer, left);
681 if (errno == EINTR) {
682 LOGE("__write_socket: EINTR error continue ...");
686 LOGE("__write_socket: ...error fd %d: errno %d\n", fd, errno);
688 if (errno == EWOULDBLOCK || errno == EAGAIN)
689 return MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
691 return MESSAGEPORT_ERROR_IO_ERROR;
699 return MESSAGEPORT_ERROR_NONE;
702 static int __write_string_to_socket(int fd,
705 unsigned int *bytes_write,
710 ret = __write_socket(fd, (char *)&string_len, sizeof(string_len),
711 bytes_write, sequence);
712 if (ret != MESSAGEPORT_ERROR_NONE) {
713 _LOGE("write string_len fail");
717 if (string_len > 0) {
718 ret = __write_socket(fd, buffer, string_len, bytes_write, sequence);
719 if (ret != MESSAGEPORT_ERROR_NONE) {
720 _LOGE("wirte buffer fail");
727 return MESSAGEPORT_ERROR_NONE;
730 static int __read_socket(int fd,
733 unsigned int *bytes_read)
735 unsigned int left = nbytes;
738 const struct timespec TRY_SLEEP_TIME = { 0, 500 * 1000 * 1000 };
741 while (left && (retry_cnt < MAX_RETRY_CNT)) {
742 nb = read(fd, buffer, left);
744 LOGE("__read_socket: ...read EOF, socket closed %d: nb %d\n", fd, nb);
745 return MESSAGEPORT_ERROR_IO_ERROR;
746 } else if (nb == -1) {
747 /* wrt(nodejs) could change socket to none-blocking socket :-( */
748 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
749 LOGE("__read_socket: %d errno, sleep and retry ...", errno);
751 nanosleep(&TRY_SLEEP_TIME, 0);
754 LOGE("__read_socket: ...error fd %d: errno %d\n", fd, errno);
755 return MESSAGEPORT_ERROR_IO_ERROR;
763 return MESSAGEPORT_ERROR_NONE;
766 static int __read_string_from_socket(int fd, char **buffer, int *string_len)
769 if (__read_socket(fd, (char *)string_len, sizeof(*string_len), &nb) != MESSAGEPORT_ERROR_NONE) {
770 LOGE("read socket fail");
771 return MESSAGEPORT_ERROR_IO_ERROR;
773 if (*string_len > 0 && *string_len < MAX_MESSAGE_SIZE) {
774 *buffer = (char *)calloc(*string_len, sizeof(char));
775 if (*buffer == NULL) {
776 LOGE("Out of memory.");
777 return MESSAGEPORT_ERROR_IO_ERROR;
779 if (__read_socket(fd, *buffer, *string_len, &nb) != MESSAGEPORT_ERROR_NONE) {
780 LOGE("read socket fail");
781 return MESSAGEPORT_ERROR_IO_ERROR;
784 LOGE("Invalid string len %d", *string_len);
785 return MESSAGEPORT_ERROR_IO_ERROR;
787 return MESSAGEPORT_ERROR_NONE;
790 message_port_pkt_s *__message_port_recv_raw(int fd)
792 message_port_pkt_s *pkt = NULL;
795 pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
801 if (__read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGEPORT_ERROR_NONE) {
802 LOGE("read socket fail: port_name");
803 free(pkt->remote_port_name);
808 if (__read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGEPORT_ERROR_NONE) {
809 LOGE("read socket fail: is_bidirection");
810 free(pkt->remote_port_name);
815 if (__read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGEPORT_ERROR_NONE) {
816 LOGE("read socket fail: is_trusted");
817 free(pkt->remote_port_name);
822 if (__read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGEPORT_ERROR_NONE) {
823 LOGE("read socket fail: data");
826 free(pkt->remote_port_name);
834 static gboolean __socket_request_handler(GIOChannel *gio,
839 message_port_callback_info_s *mi;
840 message_port_pkt_s *pkt;
842 GError *error = NULL;
844 mi = (message_port_callback_info_s *)data;
847 g_io_channel_shutdown(gio, TRUE, &error);
849 _LOGE("g_io_channel_shutdown error : %s", error->message);
852 g_io_channel_unref(gio);
856 if (cond == G_IO_HUP) {
858 _LOGI("socket G_IO_HUP");
859 __callback_info_free_by_info(mi);
864 if ((fd = g_io_channel_unix_get_fd(gio)) < 0) {
865 _LOGE("fail to get fd from io channel");
866 __callback_info_free_by_info(mi);
870 if ((pkt = __message_port_recv_raw(fd)) == NULL) {
871 _LOGE("recv error on SOCKET");
872 __callback_info_free_by_info(mi);
876 kb = bundle_decode(pkt->data, pkt->data_len);
877 if (pkt->is_bidirection)
878 mi->callback(mi->local_id, mi->remote_app_id, pkt->remote_port_name, pkt->is_trusted, kb, NULL);
880 mi->callback(mi->local_id, mi->remote_app_id, NULL, pkt->is_trusted, kb, NULL);
884 if (pkt->remote_port_name)
885 free(pkt->remote_port_name);
895 static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation)
897 char *local_port = NULL;
898 char *local_appid = NULL;
899 char *remote_appid = NULL;
900 char *remote_port = NULL;
901 gboolean local_trusted = false;
902 gboolean remote_trusted = false;
903 gboolean bi_dir = false;
907 bundle_raw *raw = NULL;
908 message_port_local_port_info_s *mi;
909 int local_reg_id = 0;
910 message_port_callback_info_s *callback_info;
911 message_port_callback_info_s *head_callback_info;
912 GList *callback_info_list = NULL;
916 GUnixFDList *fd_list;
918 int *returned_fds = NULL;
921 g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
922 &remote_appid, &remote_port, &remote_trusted, &len, &raw);
925 _LOGE("Invalid argument : remote_port is NULL");
929 _LOGE("Invalid argument : remote_appid is NULL");
932 if (!__is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
933 _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
937 _LOGE("Invalid argument : local_appid");
941 _LOGE("Invalid argument : local_port");
944 if (strcmp(remote_appid, __app_id) != 0) {
945 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
948 if (strcmp(remote_port, mi->port_name) != 0) {
949 _LOGE("Invalid argument : remote_port (%s)", remote_port);
953 _LOGE("Invalid argument : data_len");
956 if (remote_trusted) {
957 if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
958 if (!__is_preloaded(local_appid, remote_appid)) {
959 int ret = __check_certificate(local_appid, remote_appid);
960 if (ret == MESSAGEPORT_ERROR_NONE)
961 g_hash_table_insert(__trusted_app_list_hash, local_appid, "TRUE");
963 _LOGE("The application (%s) is not signed with the same certificate",
971 callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
972 if (callback_info == NULL)
975 callback_info->local_id = mi->local_id;
976 callback_info->remote_app_id = strdup(local_appid);
977 callback_info->callback = mi->callback;
979 msg = g_dbus_method_invocation_get_message(invocation);
980 fd_list = g_dbus_message_get_unix_fd_list(msg);
982 /* When application send message to self fd_list is NULL */
983 if (fd_list != NULL) {
984 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
985 if (returned_fds == NULL) {
986 _LOGE("fail to get fds");
987 __callback_info_free(callback_info);
990 fd = returned_fds[0];
992 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
995 callback_info->gio_read = g_io_channel_unix_new(fd);
996 if (!callback_info->gio_read) {
997 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
998 __callback_info_free(callback_info);
1002 callback_info->g_src_id = g_io_add_watch(callback_info->gio_read, G_IO_IN | G_IO_HUP,
1003 __socket_request_handler, (gpointer)callback_info);
1004 if (callback_info->g_src_id == 0) {
1005 _LOGE("fail to add watch on socket");
1006 __callback_info_free(callback_info);
1010 callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(mi->local_id));
1011 if (callback_info_list == NULL) {
1012 head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
1013 if (head_callback_info == NULL) {
1014 _LOGE("fail to alloc head_callback_info");
1015 __callback_info_free(callback_info);
1018 head_callback_info->local_id = 0;
1019 head_callback_info->remote_app_id = NULL;
1020 head_callback_info->callback = NULL;
1021 head_callback_info->gio_read = NULL;
1022 head_callback_info->g_src_id = 0;
1023 callback_info_list = g_list_append(callback_info_list, head_callback_info);
1024 callback_info_list = g_list_append(callback_info_list, callback_info);
1025 g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(mi->local_id), callback_info_list);
1027 callback_info_list = g_list_append(callback_info_list, callback_info);
1032 data = bundle_decode(raw, len);
1034 _LOGE("Invalid argument : message");
1038 LOGD("call calback %s", local_appid);
1040 mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, NULL);
1042 mi->callback(mi->local_id, local_appid, NULL, false, data, NULL);
1051 static int __check_remote_port(const char *remote_app_id, const char *remote_port, bool is_trusted, bool *exist)
1053 _LOGD("Check a remote port : [%s:%s]", remote_app_id, remote_port);
1055 GVariant *result = NULL;
1057 int ret_val = MESSAGEPORT_ERROR_NONE;
1058 char *bus_name = NULL;
1059 message_port_remote_app_info_s *remote_app_info = NULL;
1060 port_list_info_s *port_info = NULL;
1061 int local_reg_id = 0;
1062 message_port_local_port_info_s *mi = NULL;
1063 gboolean name_exist = false;
1065 _LOGD("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
1067 ret_val = __get_remote_port_info(remote_app_id, remote_port, is_trusted, &remote_app_info, &port_info);
1068 if (ret_val != MESSAGEPORT_ERROR_NONE)
1072 if (strcmp(remote_app_id, __app_id) == 0) {
1074 _LOGD("__is_local_port_registed ");
1075 if (!__is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
1080 _LOGD("__is_local_port_registed : %d ", *exist);
1081 return MESSAGEPORT_ERROR_NONE;
1084 port_info->exist = false;
1085 bus_name = port_info->encoded_bus_name;
1087 result = g_dbus_connection_call_sync(
1091 DBUS_INTERFACE_DBUS,
1093 g_variant_new("(s)", bus_name),
1094 G_VARIANT_TYPE("(b)"),
1095 G_DBUS_CALL_FLAGS_NONE,
1100 if (err || (result == NULL)) {
1102 _LOGE("No reply. error = %s", err->message);
1105 ret_val = MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
1107 g_variant_get(result, "(b)", &name_exist);
1110 _LOGI("Name not exist %s", bus_name);
1112 ret_val = MESSAGEPORT_ERROR_NONE;
1116 if (remote_app_info->certificate_info != CERTIFICATE_MATCH) {
1117 if (!__is_preloaded(__app_id, remote_app_id)) {
1118 if (__check_certificate(__app_id, remote_app_id) != MESSAGEPORT_ERROR_NONE) {
1119 ret_val = MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
1123 remote_app_info->certificate_info = CERTIFICATE_MATCH;
1126 port_info->exist = true;
1128 ret_val = MESSAGEPORT_ERROR_NONE;
1129 __watch_remote_port_info(port_info);
1134 g_variant_unref(result);
1139 static void __on_sender_name_appeared(GDBusConnection *connection,
1141 const gchar *name_owner,
1144 _LOGI("sender name appeared : %s", name);
1147 static void __on_sender_name_vanished(GDBusConnection *connection,
1151 gboolean remove_result = FALSE;
1152 int *watcher_id = (int *)user_data;
1153 remove_result = g_hash_table_remove(__sender_appid_hash, (gpointer)name);
1155 _LOGE("Fail to remove sender appid from hash : %s", name);
1158 if (*watcher_id > 0)
1159 g_bus_unwatch_name(*watcher_id);
1161 LOGE("Invalid watcher_id %d", *watcher_id);
1164 LOGE("watcher_id is NULL");
1168 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
1171 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
1172 char *local_appid = NULL;
1173 int pid = __get_sender_pid(conn, sender);
1174 int *watcher_id = (int *)calloc(1, sizeof(int));
1176 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
1177 if (ret != AUL_R_OK) {
1178 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
1183 g_variant_get_child(parameters, 0, "&s", &local_appid);
1184 if (local_appid == NULL) {
1185 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
1190 if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
1191 _LOGD("insert sender !!!!! %s", sender);
1192 g_hash_table_insert(__sender_appid_hash, (gpointer)strdup(sender), GINT_TO_POINTER(pid));
1193 *watcher_id = g_bus_watch_name_on_connection(
1196 G_BUS_NAME_WATCHER_FLAGS_NONE,
1197 __on_sender_name_appeared,
1198 __on_sender_name_vanished,
1208 static void __dbus_method_call_handler(GDBusConnection *conn,
1209 const gchar *sender, const gchar *object_path,
1210 const gchar *iface_name, const gchar *method_name,
1211 GVariant *parameters, GDBusMethodInvocation *invocation,
1214 _LOGI("method_name: %s, sender: %s", method_name, sender);
1215 gpointer sender_pid = g_hash_table_lookup(__sender_appid_hash, sender);
1216 if (sender_pid == NULL) {
1217 if (!__check_sender_validation(parameters, sender, conn))
1220 if (g_strcmp0(method_name, "send_message") == 0)
1221 send_message(parameters, invocation);
1223 g_dbus_method_invocation_return_value(invocation, NULL);
1226 static const GDBusInterfaceVTable interface_vtable = {
1227 __dbus_method_call_handler,
1232 static int __dbus_init(void)
1235 GError *error = NULL;
1237 __gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
1238 if (__gdbus_conn == NULL) {
1239 if (error != NULL) {
1240 _LOGE("Failed to get dbus [%s]", error->message);
1241 g_error_free(error);
1250 g_object_unref(__gdbus_conn);
1256 int __register_dbus_interface(const char *port_name, bool is_trusted)
1259 GDBusNodeInfo *introspection_data = NULL;
1260 int registration_id = 0;
1262 static gchar introspection_prefix[] =
1264 " <interface name='";
1266 static gchar introspection_postfix[] =
1268 " <method name='send_message'>"
1269 " <arg type='s' name='local_appid' direction='in'/>"
1270 " <arg type='s' name='local_port' direction='in'/>"
1271 " <arg type='b' name='local_trusted' direction='in'/>"
1272 " <arg type='b' name='bi_dir' direction='in'/>"
1273 " <arg type='s' name='remote_appid' direction='in'/>"
1274 " <arg type='s' name='remote_port' direction='in'/>"
1275 " <arg type='b' name='remote_trusted' direction='in'/>"
1276 " <arg type='u' name='data_len' direction='in'/>"
1277 " <arg type='s' name='data' direction='in'/>"
1282 char *introspection_xml = NULL;
1283 int introspection_xml_len = 0;
1287 GError *error = NULL;
1288 char *bus_name = NULL;
1289 char *interface_name = NULL;
1290 GVariant *result = NULL;
1292 bus_name = __get_encoded_name(__app_id, port_name, is_trusted);
1294 _LOGE("Fail to get bus name");
1297 interface_name = bus_name;
1299 introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
1300 strlen(introspection_postfix) + 1;
1302 introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
1303 if (!introspection_xml) {
1304 _LOGE("out of memory");
1309 result = g_dbus_connection_call_sync(
1313 DBUS_INTERFACE_DBUS,
1315 g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
1316 G_VARIANT_TYPE("(u)"),
1317 G_DBUS_CALL_FLAGS_NONE,
1322 _LOGE("RequestName fail : %s", error->message);
1323 g_error_free(error);
1326 if (result == NULL) {
1327 _LOGE("fail to get name NULL");
1330 g_variant_get(result, "(u)", &owner_id);
1331 if (owner_id == 0) {
1332 _LOGE("Acquiring the own name is failed");
1336 _LOGD("Acquiring the own name : %d", owner_id);
1338 snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
1340 introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
1341 if (!introspection_data) {
1342 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
1346 registration_id = g_dbus_connection_register_object(__gdbus_conn,
1347 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
1348 &interface_vtable, NULL, NULL, NULL);
1350 _LOGD("registration_id %d", registration_id);
1352 if (registration_id == 0) {
1353 _LOGE("Failed to g_dbus_connection_register_object");
1358 if (introspection_data)
1359 g_dbus_node_info_unref(introspection_data);
1360 if (introspection_xml)
1361 free(introspection_xml);
1365 g_variant_unref(result);
1368 return registration_id;
1371 /* LCOV_EXCL_START */
1372 void __list_free_port_list(gpointer data)
1374 port_list_info_s *n = (port_list_info_s *)data;
1376 FREE_AND_NULL(n->encoded_bus_name);
1377 FREE_AND_NULL(n->port_name);
1380 /* LCOV_EXCL_STOP */
1382 /* LCOV_EXCL_START */
1383 static void __hash_destory_local_value(gpointer data)
1385 message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
1388 free(mli->port_name);
1392 /* LCOV_EXCL_STOP */
1394 /* LCOV_EXCL_START */
1395 static void __hash_destory_remote_value(gpointer data)
1397 message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
1399 FREE_AND_NULL(mri->sender_id);
1400 FREE_AND_NULL(mri->remote_app_id);
1402 g_list_free_full(mri->port_list, __list_free_port_list);
1407 /* LCOV_EXCL_STOP */
1409 static bool __initialize(void)
1412 #if !GLIB_CHECK_VERSION(2, 35, 0)
1418 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
1420 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
1421 retvm_if(ret != AUL_R_OK, false, "Failed to get the application ID: %d", ret);
1423 __app_id = strdup(buffer);
1424 retvm_if(!__app_id, false, "Malloc failed");
1425 _LOGI("init : %s", __app_id);
1427 if (__local_port_info == NULL) {
1428 __local_port_info = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destory_local_value);
1429 retvm_if(!__local_port_info, false, "fail to create __local_port_info");
1432 if (__remote_app_info == NULL) {
1433 __remote_app_info = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __hash_destory_remote_value);
1434 retvm_if(!__remote_app_info, false, "fail to create __remote_app_info");
1437 if (__sender_appid_hash == NULL) {
1438 __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
1439 retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
1442 if (__trusted_app_list_hash == NULL) {
1443 __trusted_app_list_hash = g_hash_table_new(g_str_hash, g_str_equal);
1444 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
1447 if (__callback_info_hash == NULL) {
1448 __callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destroy_callback_info);
1449 retvm_if(!__callback_info_hash, false, "fail to create __callback_info_hash");
1454 _initialized = true;
1460 static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, messageport_message_cb callback)
1462 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
1463 retvm_if(!mi, false, "Malloc failed");
1465 mi->callback = callback;
1466 mi->is_trusted = is_trusted;
1467 mi->port_name = strdup(local_port);
1468 if (mi->port_name == NULL) {
1469 _LOGE("Malloc failed (%s)", local_port);
1473 mi->local_id = local_id;
1475 g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
1479 static int __register_message_port(const char *local_port, bool is_trusted, messageport_message_cb callback)
1481 _SECURE_LOGI("local_port : [%s:%s]", local_port, is_trusted ? "trusted" : "non-trusted");
1485 /* Check the message port is already registed */
1486 if (__is_local_port_registed(local_port, is_trusted, &local_id, NULL))
1489 local_id = __register_dbus_interface(local_port, is_trusted);
1491 _LOGE("register_dbus_interface fail !!");
1492 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1495 if (!__message_port_register_port(local_id, local_port, is_trusted, callback))
1496 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1501 static void __free_delay_message_info(delay_message_info_s *message)
1503 if (message != NULL) {
1504 FREE_AND_NULL(message->local_port_name);
1505 FREE_AND_NULL(message->data);
1506 FREE_AND_NULL(message);
1510 static void __free_list_delay_message_info(gpointer data)
1512 delay_message_info_s *message = (delay_message_info_s *)data;
1514 if (message != NULL)
1515 __free_delay_message_info(message);
1518 static int __send_delayed_message(int sockfd, delay_message_info_s *message)
1520 unsigned int nb = 0;
1521 int sequence = message->sequence - 1;
1522 int ret = MESSAGEPORT_ERROR_NONE;
1523 bool is_startline = true;
1526 _LOGI("send_delayed_message : sockfd (%d) sequence(%d) sent byte(%d)",
1527 sockfd, message->sequence, message->sent_bytes);
1529 switch (message->sequence) {
1530 case SEQUENCE_START:
1532 is_startline = false;
1534 case SEQUENCE_PORT_LEN:
1536 offset = message->sent_bytes;
1538 ret = __write_socket(sockfd, ((char *)&message->local_port_len) + offset,
1539 sizeof(message->local_port_len) - offset, &nb, &sequence);
1540 if (ret != MESSAGEPORT_ERROR_NONE) {
1541 _LOGE("write local_port_len fail");
1545 is_startline = false;
1547 case SEQUENCE_PORT_NAME:
1549 offset = message->sent_bytes;
1551 if (message->local_port_len > 0)
1552 ret = __write_socket(sockfd, message->local_port_name + offset,
1553 message->local_port_len - offset , &nb, &sequence);
1557 if (ret != MESSAGEPORT_ERROR_NONE) {
1558 _LOGE("write local_port fail");
1562 is_startline = false;
1564 case SEQUENCE_BIDIRECTION:
1566 offset = message->sent_bytes;
1568 ret = __write_socket(sockfd, ((char *)&message->is_bidirection) + offset,
1569 sizeof(message->is_bidirection) - offset, &nb, &sequence);
1570 if (ret != MESSAGEPORT_ERROR_NONE) {
1571 _LOGE("write is_bidirection fail");
1575 is_startline = false;
1577 case SEQUENCE_TRUSTED:
1579 offset = message->sent_bytes;
1581 ret = __write_socket(sockfd, ((char *)&message->local_trusted) + offset,
1582 sizeof(message->local_trusted) - offset, &nb, &sequence);
1583 if (ret != MESSAGEPORT_ERROR_NONE) {
1584 _LOGE("write local_trusted fail");
1588 is_startline = false;
1590 case SEQUENCE_DTAT_LEN:
1592 offset = message->sent_bytes;
1594 ret = __write_socket(sockfd, ((char *)&message->data_len) + offset,
1595 sizeof(message->data_len) - offset, &nb, &sequence);
1596 if (ret != MESSAGEPORT_ERROR_NONE) {
1597 _LOGE("write data_len fail");
1601 is_startline = false;
1605 offset = message->sent_bytes;
1607 ret = __write_socket(sockfd, (char *)message->data + offset,
1608 message->data_len -offset, &nb, &sequence);
1610 if (ret != MESSAGEPORT_ERROR_NONE) {
1611 _LOGE("write data fail");
1615 is_startline = false;
1618 ret = MESSAGEPORT_ERROR_NONE;
1623 if (ret == MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE) {
1625 message->sent_bytes += nb;
1627 message->sent_bytes = nb;
1629 message->sequence = sequence;
1630 _LOGE("send_delayed_message fail : sockfd (%d) sequence(%d) sent byte(%d)",
1631 sockfd, message->sequence, message->sent_bytes);
1638 static gboolean __process_delayed_message(gint fd, GIOCondition cond, gpointer data)
1640 port_list_info_s *port_info = (port_list_info_s *)data;
1641 delay_message_info_s *message;
1644 if (port_info == NULL)
1645 return G_SOURCE_REMOVE;
1647 pthread_mutex_lock(&mutex);
1649 if (port_info->delayed_message_list == NULL) {
1650 port_info->delayed_message_size = 0;
1651 port_info->delay_src_id = 0;
1652 pthread_mutex_unlock(&mutex);
1653 return G_SOURCE_REMOVE;
1655 message = g_list_nth_data(port_info->delayed_message_list, 0);
1656 ret = __send_delayed_message(port_info->send_sock_fd, message);
1658 if (ret == MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE) {
1659 pthread_mutex_unlock(&mutex);
1660 return G_SOURCE_CONTINUE;
1661 } else if (ret == MESSAGEPORT_ERROR_IO_ERROR) {
1662 __clear_disconnect_socket(port_info);
1663 pthread_mutex_unlock(&mutex);
1664 return G_SOURCE_REMOVE;
1667 port_info->delayed_message_size -= message->size;
1669 port_info->delayed_message_list = g_list_remove(port_info->delayed_message_list, message);
1670 __free_delay_message_info(message);
1673 pthread_mutex_unlock(&mutex);
1675 return G_SOURCE_CONTINUE;
1678 static int __insert_delayed_message(port_list_info_s *port_info,
1680 bundle_raw *kb_data,
1682 unsigned int sent_bytes,
1683 const char *local_port,
1685 bool is_bidirection)
1687 #define QUEUE_SIZE_MAX (1024 * 1024) /* 1MB per remote port (MAX) */
1689 unsigned int tmp_size;
1690 unsigned int message_size;
1691 int ret = MESSAGEPORT_ERROR_NONE;
1693 if (port_info->delayed_message_size >= QUEUE_SIZE_MAX) {
1694 _LOGE("cache fail : delayed_message_size (%d), count(%d)",
1695 port_info->delayed_message_size, g_list_length(port_info->delayed_message_list));
1696 return MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
1699 delay_message_info_s *message = (delay_message_info_s *)calloc(1, sizeof(delay_message_info_s));
1700 retvm_if(!message, MESSAGEPORT_ERROR_OUT_OF_MEMORY, "Malloc failed");
1702 message_size = sizeof(delay_message_info_s);
1704 message->sequence = sequence;
1705 tmp_size = strlen(local_port) + 1;
1706 message_size += tmp_size;
1707 message->local_port_len = tmp_size;
1708 message->local_port_name = strdup(local_port);
1709 if (message->local_port_name == NULL) {
1710 _LOGE("local_port_name strdup fail");
1711 ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1714 message->is_bidirection = is_bidirection;
1715 message->local_trusted = local_trusted;
1716 message_size += data_len;
1717 message->data_len = data_len;
1718 message->data = (bundle_raw *)strdup((const char *)kb_data);
1719 if (message->data == NULL) {
1720 _LOGE("data strdup fail");
1721 ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1726 message->sent_bytes = sent_bytes;
1727 message->size = message_size;
1728 port_info->delayed_message_size += message_size;
1730 port_info->delayed_message_list = g_list_append(port_info->delayed_message_list, message);
1732 if (port_info->delay_src_id == 0) {
1733 port_info->delay_src_id = g_unix_fd_add_full(G_PRIORITY_DEFAULT,
1734 port_info->send_sock_fd, G_IO_OUT, __process_delayed_message,
1738 _LOGE("inserted : pm(%s) fd(%d) ms(%d) ds(%d) dlc(%d) sqn(%d) sb (%d)",
1739 port_info->port_name, port_info->send_sock_fd, message_size,
1740 port_info->delayed_message_size,
1741 g_list_length(port_info->delayed_message_list), sequence, sent_bytes);
1746 if (ret != MESSAGEPORT_ERROR_NONE)
1747 __free_delay_message_info(message);
1752 int __message_port_send_async(port_list_info_s *port_info, bundle *kb, const char *local_port,
1753 bool local_trusted, bool is_bidirection)
1757 int local_port_len = 0;
1758 unsigned int nb = 0;
1759 bundle_raw *kb_data = NULL;
1760 int sequence = SEQUENCE_START;
1762 bundle_encode(kb, &kb_data, &data_len);
1763 if (kb_data == NULL) {
1764 _LOGE("bundle encode fail");
1765 ret = MESSAGEPORT_ERROR_INVALID_PARAMETER;
1769 if (data_len > MAX_MESSAGE_SIZE) {
1770 _LOGE("bigger than max size\n");
1771 ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1775 if (g_list_length(port_info->delayed_message_list) > 0) {
1776 ret = MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
1777 _LOGE("There are messages in the delayed_message_list (count %d)",
1778 g_list_length(port_info->delayed_message_list));
1782 if (local_port != NULL)
1783 local_port_len = strlen(local_port) + 1;
1785 ret = __write_string_to_socket(port_info->send_sock_fd, local_port,
1786 local_port_len, &nb, &sequence);
1787 if (ret != MESSAGEPORT_ERROR_NONE) {
1788 _LOGE("write local_port fail");
1792 ret = __write_socket(port_info->send_sock_fd, (char *)&is_bidirection,
1793 sizeof(is_bidirection), &nb, &sequence);
1794 if (ret != MESSAGEPORT_ERROR_NONE) {
1795 _LOGE("write is_bidirection fail");
1799 ret = __write_socket(port_info->send_sock_fd, (char *)&local_trusted,
1800 sizeof(local_trusted), &nb, &sequence);
1801 if (ret != MESSAGEPORT_ERROR_NONE) {
1802 _LOGE("write local_trusted fail");
1806 ret = __write_string_to_socket(port_info->send_sock_fd, (void *)kb_data,
1807 data_len, &nb, &sequence);
1808 if (ret != MESSAGEPORT_ERROR_NONE) {
1809 _LOGE("write kb_data fail");
1814 if (ret == MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE) {
1815 ret = __insert_delayed_message(port_info, sequence, kb_data, data_len, nb,
1816 local_port, local_trusted, is_bidirection);
1817 if (ret != MESSAGEPORT_ERROR_NONE)
1818 ret = MESSAGEPORT_ERROR_IO_ERROR;
1827 static int __message_port_send_message(const char *remote_appid, const char *remote_port,
1828 const char *local_port, bool trusted_message, bool local_trusted, bool bi_dir, bundle *message)
1831 int ret = MESSAGEPORT_ERROR_NONE;
1832 GUnixFDList *fd_list = NULL;
1835 bundle_raw *raw = NULL;
1836 char *bus_name = NULL;
1837 char *interface_name = NULL;
1839 message_port_remote_app_info_s *remote_app_info = NULL;
1840 port_list_info_s *port_info = NULL;
1841 GDBusMessage *msg = NULL;
1843 GVariant *body = NULL;
1844 int sock_pair[2] = {0,};
1847 ret = __get_remote_port_info(remote_appid, remote_port, trusted_message, &remote_app_info, &port_info);
1848 if (ret != MESSAGEPORT_ERROR_NONE)
1851 if (port_info->exist == false) {
1853 _LOGD("port exist check !!");
1854 ret = __check_remote_port(remote_appid, remote_port, trusted_message, &exist);
1855 if (ret != MESSAGEPORT_ERROR_NONE) {
1857 } else if (!exist) {
1858 ret = MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1863 if (port_info->send_sock_fd > 0) {
1864 ret = __message_port_send_async(port_info, message,
1865 (local_port) ? local_port : "", local_trusted, bi_dir);
1868 bus_name = port_info->encoded_bus_name;
1869 interface_name = bus_name;
1871 if (bundle_encode(message, &raw, &len) != BUNDLE_ERROR_NONE) {
1872 ret = MESSAGEPORT_ERROR_INVALID_PARAMETER;
1876 if (MAX_MESSAGE_SIZE < len) {
1877 _LOGE("The size of message (%d) has exceeded the maximum limit.", len);
1878 ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1882 body = g_variant_new("(ssbbssbus)", __app_id, (local_port) ? local_port : "", local_trusted, bi_dir,
1883 remote_appid, remote_port, trusted_message, len, raw);
1884 if (strcmp(remote_appid, __app_id) != 0) { /* self send */
1886 /* if message-port fail to get socket pair, communicate using GDBus */
1887 if (aul_request_message_port_socket_pair(sock_pair) != AUL_R_OK) {
1888 _LOGE("error create socket pair");
1891 _LOGI("sock pair : %d, %d",
1892 sock_pair[SOCK_PAIR_SENDER], sock_pair[SOCK_PAIR_RECEIVER]);
1893 fd_list = g_unix_fd_list_new();
1894 g_unix_fd_list_append(fd_list, sock_pair[SOCK_PAIR_RECEIVER], &err);
1896 _LOGE("g_unix_fd_list_append [%s]", err->message);
1897 ret = MESSAGEPORT_ERROR_IO_ERROR;
1902 port_info->send_sock_fd = sock_pair[SOCK_PAIR_SENDER];
1903 close(sock_pair[SOCK_PAIR_RECEIVER]);
1904 sock_pair[SOCK_PAIR_RECEIVER] = 0;
1906 port_info->gio_read = g_io_channel_unix_new(port_info->send_sock_fd);
1907 if (!port_info->gio_read) {
1908 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
1909 ret = MESSAGEPORT_ERROR_IO_ERROR;
1913 port_info->g_src_id = g_io_add_watch(port_info->gio_read, G_IO_IN | G_IO_HUP,
1914 __socket_disconnect_handler, (gpointer)port_info);
1915 if (port_info->g_src_id == 0) {
1916 _LOGE("fail to add watch on socket");
1917 ret = MESSAGEPORT_ERROR_IO_ERROR;
1924 msg = g_dbus_message_new_method_call(bus_name, MESSAGEPORT_OBJECT_PATH, interface_name, "send_message");
1926 _LOGE("Can't allocate new method call");
1927 ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1931 g_dbus_message_set_unix_fd_list(msg, fd_list);
1932 g_dbus_message_set_body(msg, body);
1933 g_dbus_connection_send_message(__gdbus_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
1935 _LOGE("No reply. error = %s", err->message);
1937 ret = MESSAGEPORT_ERROR_IO_ERROR;
1940 __watch_remote_port_info(port_info);
1946 g_object_unref(msg);
1948 bundle_free_encoded_rawdata(&raw);
1950 g_object_unref(fd_list);
1952 if (ret != MESSAGEPORT_ERROR_NONE) {
1953 __clear_disconnect_socket(port_info);
1954 if (sock_pair[SOCK_PAIR_SENDER])
1955 close(sock_pair[SOCK_PAIR_SENDER]);
1956 if (sock_pair[SOCK_PAIR_RECEIVER])
1957 close(sock_pair[SOCK_PAIR_RECEIVER]);
1963 int __message_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port, bool trusted_message, bundle *message)
1965 message_port_local_port_info_s *local_info;
1966 int ret = __get_local_port_info(id, &local_info);
1967 if (ret != MESSAGEPORT_ERROR_NONE)
1970 _LOGD("bidirectional_message %s", local_info->port_name);
1971 return __message_port_send_message(remote_app_id, remote_port,
1972 local_info->port_name, trusted_message, local_info->is_trusted, true, message);
1975 static void __name_registered(GDBusConnection *connection,
1977 const gchar *name_owner,
1981 registered_callback_info_s *info = (registered_callback_info_s *)user_data;
1983 LOGE("NULL registered_callback_info");
1987 _LOGI("watcher_id : %d, appeared name : %s , name_owner : %s\n", info->watcher_id, name, name_owner);
1988 if (info->registered_cb)
1989 info->registered_cb(info->remote_app_id, info->remote_port, info->is_trusted, info->user_data);
1992 static void __name_unregistered(GDBusConnection *connection,
1997 registered_callback_info_s *info = (registered_callback_info_s *)user_data;
1999 LOGE("NULL registered_callback_info");
2003 _LOGI("watcher_id : %d, vanished name : %s\n", info->watcher_id, name);
2004 if (info->unregistered_cb)
2005 info->unregistered_cb(info->remote_app_id, info->remote_port, info->is_trusted, info->user_data);
2008 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)
2010 int ret_val = MESSAGEPORT_ERROR_NONE;
2011 message_port_remote_app_info_s *remote_app_info = NULL;
2012 port_list_info_s *port_info = NULL;
2014 _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
2016 ret_val = __get_remote_port_info(remote_app_id, remote_port, trusted_remote_port, &remote_app_info, &port_info);
2017 if (ret_val != MESSAGEPORT_ERROR_NONE)
2020 if (__registered_callback_info_hash == NULL)
2021 __registered_callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __registered_callback_info_free);
2023 registered_callback_info_s *registered_cb_info = (registered_callback_info_s *)calloc(1, sizeof(registered_callback_info_s));
2024 registered_cb_info->registered_cb = registered_cb;
2025 registered_cb_info->unregistered_cb = unregistered_cb;
2026 registered_cb_info->user_data = user_data;
2027 registered_cb_info->remote_app_id = strdup(remote_app_info->remote_app_id);
2028 if (registered_cb_info->remote_app_id == NULL) {
2029 free(registered_cb_info);
2030 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
2032 registered_cb_info->remote_port = strdup(port_info->port_name);
2033 if (registered_cb_info->remote_port == NULL) {
2034 free(registered_cb_info->remote_app_id);
2035 free(registered_cb_info);
2036 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
2039 registered_cb_info->watcher_id = g_bus_watch_name_on_connection(
2041 port_info->encoded_bus_name,
2042 G_BUS_NAME_WATCHER_FLAGS_NONE,
2044 __name_unregistered,
2047 if (registered_cb_info->watcher_id == 0) {
2048 free(registered_cb_info->remote_app_id);
2049 free(registered_cb_info->remote_port);
2050 free(registered_cb_info);
2051 return MESSAGEPORT_ERROR_IO_ERROR;
2054 g_hash_table_insert(__registered_callback_info_hash,
2055 GINT_TO_POINTER(registered_cb_info->watcher_id), registered_cb_info);
2057 *watcher_id = registered_cb_info->watcher_id;
2058 return MESSAGEPORT_ERROR_NONE;
2061 int messageport_unregister_local_port(int local_port_id, bool trusted_port)
2065 char *bus_name = NULL;
2069 _LOGI("unregister : %d", local_port_id);
2071 message_port_local_port_info_s *mi =
2072 (message_port_local_port_info_s *)
2073 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
2075 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
2077 if (mi->is_trusted != trusted_port)
2078 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
2080 g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
2082 bus_name = __get_encoded_name(__app_id, mi->port_name, mi->is_trusted);
2083 if (bus_name == NULL)
2084 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
2086 g_dbus_connection_unregister_object(__gdbus_conn, local_port_id);
2088 result = g_dbus_connection_call_sync(
2092 DBUS_INTERFACE_DBUS,
2094 g_variant_new("(s)", bus_name),
2095 G_VARIANT_TYPE("(u)"),
2096 G_DBUS_CALL_FLAGS_NONE,
2105 _LOGE("RequestName fail : %s", err->message);
2107 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
2109 g_variant_get(result, "(u)", &ret);
2112 g_variant_unref(result);
2114 if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
2116 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
2117 _LOGE("Port Not exist");
2118 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
2119 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
2120 _LOGE("Try to release not owned name. MESSAGEPORT_ERROR_INVALID_PARAMETER");
2121 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
2126 g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
2128 return MESSAGEPORT_ERROR_NONE;
2131 int messageport_register_local_port(const char *local_port, messageport_message_cb callback)
2133 if (!_initialized) {
2134 if (!__initialize())
2135 return MESSAGEPORT_ERROR_IO_ERROR;
2138 return __register_message_port(local_port, false, callback);
2141 int messageport_register_trusted_local_port(const char *local_port, messageport_message_cb callback)
2143 if (!_initialized) {
2144 if (!__initialize())
2145 return MESSAGEPORT_ERROR_IO_ERROR;
2148 return __register_message_port(local_port, true, callback);
2152 int messageport_check_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
2154 if (!_initialized) {
2155 if (!__initialize())
2156 return MESSAGEPORT_ERROR_IO_ERROR;
2159 int ret = __check_remote_port(remote_app_id, remote_port, false, exist);
2160 if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
2162 ret = MESSAGEPORT_ERROR_NONE;
2168 int messageport_check_trusted_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
2170 if (!_initialized) {
2171 if (!__initialize())
2172 return MESSAGEPORT_ERROR_IO_ERROR;
2175 int ret = __check_remote_port(remote_app_id, remote_port, true, exist);
2176 if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
2178 ret = MESSAGEPORT_ERROR_NONE;
2184 int messageport_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
2186 if (!_initialized) {
2187 if (!__initialize())
2188 return MESSAGEPORT_ERROR_IO_ERROR;
2191 return __message_port_send_message(remote_app_id, remote_port, NULL, false, false, false, message);
2194 int messageport_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
2196 if (!_initialized) {
2197 if (!__initialize())
2198 return MESSAGEPORT_ERROR_IO_ERROR;
2201 return __message_port_send_message(remote_app_id, remote_port, NULL, true, false, false, message);
2204 int messageport_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port,
2207 if (!_initialized) {
2208 if (!__initialize())
2209 return MESSAGEPORT_ERROR_IO_ERROR;
2212 return __message_send_bidirectional_message(id, remote_app_id, remote_port, false, message);
2215 int messageport_send_bidirectional_trusted_message(int id, const char *remote_app_id, const char *remote_port,
2218 if (!_initialized) {
2219 if (!__initialize())
2220 return MESSAGEPORT_ERROR_IO_ERROR;
2222 return __message_send_bidirectional_message(id, remote_app_id, remote_port, true, message);
2225 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)
2227 if (!_initialized) {
2228 if (!__initialize())
2229 return MESSAGEPORT_ERROR_IO_ERROR;
2231 return __messageport_watch_remote_port(watcher_id, remote_app_id, remote_port, is_trusted, registered_cb, NULL, user_data);
2234 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)
2236 if (!_initialized) {
2237 if (!__initialize())
2238 return MESSAGEPORT_ERROR_IO_ERROR;
2240 return __messageport_watch_remote_port(watcher_id, remote_app_id, remote_port, is_trusted, NULL, unregistered_cb, user_data);
2244 int messageport_remove_registration_event_cb(int watcher_id)
2246 registered_callback_info_s *registered_cb_info = NULL;
2247 gboolean remove_result = FALSE;
2250 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
2252 registered_cb_info = g_hash_table_lookup(__registered_callback_info_hash, GINT_TO_POINTER(watcher_id));
2253 if (registered_cb_info == NULL)
2254 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
2256 remove_result = g_hash_table_remove(__registered_callback_info_hash, GINT_TO_POINTER(watcher_id));
2258 return MESSAGEPORT_ERROR_IO_ERROR;
2260 g_bus_unwatch_name(watcher_id);
2262 return MESSAGEPORT_ERROR_NONE;