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 {
127 int certificate_info;
129 } message_port_remote_app_info_s;
131 typedef struct port_list_info {
132 message_port_remote_app_info_s *remote_app_info;
134 char *encoded_bus_name;
138 GIOChannel *gio_read;
140 GList *delayed_message_list;
141 unsigned int delayed_message_size;
145 typedef struct registered_callback_info {
151 messageport_registration_event_cb registered_cb;
152 messageport_registration_event_cb unregistered_cb;
153 } registered_callback_info_s;
155 enum transmission_sequence {
159 SEQUENCE_BIDIRECTION,
166 typedef struct delay_message {
168 unsigned int sent_bytes;
171 char *local_port_name;
176 } delay_message_info_s;
179 extern pthread_mutex_t mutex;
180 static void __free_list_delay_message_info(gpointer data);
183 static void __callback_info_free(gpointer data)
185 message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
186 GError *error = NULL;
187 if (callback_info == NULL)
190 if (callback_info->remote_app_id)
191 FREE_AND_NULL(callback_info->remote_app_id);
193 if (callback_info->gio_read != NULL) {
194 g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
196 _LOGE("g_io_channel_shutdown error : %s", error->message);
199 g_io_channel_unref(callback_info->gio_read);
200 callback_info->gio_read = NULL;
203 if (callback_info->g_src_id != 0) {
204 g_source_remove(callback_info->g_src_id);
205 callback_info->g_src_id = 0;
208 FREE_AND_NULL(callback_info);
211 static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
213 GList *callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
216 if (callback_info_list == NULL)
219 find_list = g_list_find(callback_info_list, callback_info);
220 if (find_list == NULL)
223 callback_info_list = g_list_remove_link(callback_info_list, find_list);
224 __callback_info_free(callback_info);
225 g_list_free(find_list);
228 static void __registered_callback_info_free(gpointer data)
230 registered_callback_info_s *callback_info = (registered_callback_info_s *)data;
231 if (callback_info == NULL)
234 if (callback_info->remote_app_id)
235 free(callback_info->remote_app_id);
237 if (callback_info->remote_port)
238 free(callback_info->remote_port);
244 static void __hash_destroy_callback_info(gpointer data)
247 GList *callback_list = (GList *)data;
248 if (callback_list != NULL)
249 g_list_free_full(callback_list, __callback_info_free);
252 static char *__get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted)
255 int prefix_len = strlen(MESSAGEPORT_BUS_NAME_PREFIX);
257 char *postfix = is_trusted ? "1" : "0";
259 unsigned char c[MD5_DIGEST_LENGTH] = {0};
260 char *md5_interface = NULL;
264 int encoded_bus_name_len = prefix_len + postfix_len + (MD5_DIGEST_LENGTH * 2) + 2;
265 int bus_name_len = strlen(remote_app_id) + strlen(port_name) + 2;
266 char *bus_name = (char *)calloc(bus_name_len, sizeof(char));
267 if (bus_name == NULL) {
268 _LOGE("bus_name calloc failed");
272 snprintf(bus_name, bus_name_len, "%s_%s", remote_app_id, port_name);
274 MD5_Init(&mdContext);
275 MD5_Update(&mdContext, bus_name, bus_name_len);
276 MD5_Final(c, &mdContext);
278 md5_interface = (char *)calloc(encoded_bus_name_len , sizeof(char));
279 if (md5_interface == NULL) {
283 _LOGE("md5_interface calloc failed!!");
287 snprintf(md5_interface, encoded_bus_name_len, "%s", MESSAGEPORT_BUS_NAME_PREFIX);
288 temp = md5_interface;
291 for (index = 0; index < MD5_DIGEST_LENGTH; index++) {
292 snprintf(temp, 3, "%02x", c[index]);
296 if (postfix && postfix_len > 0)
297 snprintf(temp, encoded_bus_name_len - (temp - md5_interface), "%s", postfix);
301 _LOGD("encoded_bus_name : %s ", md5_interface);
303 return md5_interface;
306 static int __remote_port_compare_cb(gconstpointer a, gconstpointer b)
308 port_list_info_s *key1 = (port_list_info_s *)a;
309 port_list_info_s *key2 = (port_list_info_s *)b;
311 if (key1->is_trusted == key2->is_trusted)
312 return strcmp(key1->port_name, key2->port_name);
318 static bool __is_preloaded(const char *local_appid, const char *remote_appid)
320 _LOGD("IsPreloaded");
322 bool preload_local = false;
323 bool preload_remote = false;
325 pkgmgrinfo_appinfo_h handle = NULL;
326 int ret = pkgmgrinfo_appinfo_get_usr_appinfo(local_appid, getuid(), &handle);
327 if (ret != PMINFO_R_OK) {
328 _LOGE("Failed to get the appinfo. %d", ret);
329 pkgmgrinfo_appinfo_destroy_appinfo(handle);
332 ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_local);
333 if (ret != PMINFO_R_OK) {
334 _LOGE("Failed to check the preloaded application. %d", ret);
335 pkgmgrinfo_appinfo_destroy_appinfo(handle);
338 pkgmgrinfo_appinfo_destroy_appinfo(handle);
340 ret = pkgmgrinfo_appinfo_get_usr_appinfo(remote_appid, getuid(), &handle);
341 if (ret != PMINFO_R_OK) {
342 _LOGE("Failed to get the appinfo. %d", ret);
343 pkgmgrinfo_appinfo_destroy_appinfo(handle);
346 ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_remote);
347 if (ret != PMINFO_R_OK) {
348 _LOGE("Failed to check the preloaded application. %d", ret);
349 pkgmgrinfo_appinfo_destroy_appinfo(handle);
353 if (preload_local && preload_remote) {
354 pkgmgrinfo_appinfo_destroy_appinfo(handle);
357 pkgmgrinfo_appinfo_destroy_appinfo(handle);
361 static int __check_certificate(const char *local_appid, const char *remote_appid)
363 _LOGD("CheckCertificate");
365 pkgmgrinfo_cert_compare_result_type_e res;
366 int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(local_appid, remote_appid, getuid(), &res);
368 _LOGE(":CheckCertificate() Failed");
369 return MESSAGEPORT_ERROR_IO_ERROR;
371 if (res != PMINFO_CERT_COMPARE_MATCH) {
372 _LOGE("CheckCertificate() Failed : MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH");
373 return MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
376 return MESSAGEPORT_ERROR_NONE;
379 static int __get_local_port_info(int id, message_port_local_port_info_s **info)
381 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
384 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
387 return MESSAGEPORT_ERROR_NONE;
390 static port_list_info_s *__set_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
392 int ret_val = MESSAGEPORT_ERROR_NONE;
393 port_list_info_s *port_info = (port_list_info_s *)calloc(1, sizeof(port_list_info_s));
396 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
399 port_info->port_name = strdup(remote_port);
400 if (!port_info->port_name) {
401 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
404 port_info->is_trusted = is_trusted;
405 port_info->encoded_bus_name = __get_encoded_name(remote_app_id, remote_port, is_trusted);
406 if (port_info->encoded_bus_name == NULL) {
407 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
410 port_info->send_sock_fd = 0;
412 if (ret_val != MESSAGEPORT_ERROR_NONE) {
414 FREE_AND_NULL(port_info->port_name);
415 FREE_AND_NULL(port_info->encoded_bus_name);
423 static message_port_remote_app_info_s *__set_remote_app_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
425 message_port_remote_app_info_s *remote_app_info = NULL;
426 int ret_val = MESSAGEPORT_ERROR_NONE;
428 remote_app_info = (message_port_remote_app_info_s *)calloc(1, sizeof(message_port_remote_app_info_s));
429 if (!remote_app_info) {
430 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
434 remote_app_info->remote_app_id = strdup(remote_app_id);
435 if (remote_app_info->remote_app_id == NULL) {
436 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;;
441 if (ret_val != MESSAGEPORT_ERROR_NONE) {
442 if (remote_app_info) {
443 FREE_AND_NULL(remote_app_info->remote_app_id);
444 FREE_AND_NULL(remote_app_info);
448 return remote_app_info;
451 static void __clear_disconnect_socket(port_list_info_s *port_info)
453 GError *error = NULL;
455 if (port_info == NULL)
458 if (port_info->gio_read != NULL) {
459 g_io_channel_shutdown(port_info->gio_read, TRUE, &error);
461 _LOGE("g_io_channel_shutdown error : %s", error->message);
464 g_io_channel_unref(port_info->gio_read);
465 port_info->gio_read = NULL;
468 if (port_info->g_src_id != 0) {
469 g_source_remove(port_info->g_src_id);
470 port_info->g_src_id = 0;
473 if (port_info->delay_src_id != 0) {
474 g_source_remove(port_info->delay_src_id);
475 port_info->delay_src_id = 0;
478 if (port_info->delayed_message_list != NULL) {
479 g_list_free_full(port_info->delayed_message_list, __free_list_delay_message_info);
481 port_info->delayed_message_list = NULL;
484 port_info->delayed_message_size = 0;
485 port_info->send_sock_fd = 0;
488 /* LCOV_EXCL_START */
489 void __free_port_info(gpointer data)
491 port_list_info_s *port_info = (port_list_info_s *)data;
492 message_port_remote_app_info_s *remote_app_info;
494 if (port_info == NULL)
497 remote_app_info = port_info->remote_app_info;
499 _LOGI("__free_port_info : remote_app_id : %s port_name : %s",
500 remote_app_info->remote_app_id,
501 port_info->port_name);
503 remote_app_info->port_list = g_list_remove(remote_app_info->port_list,
506 __clear_disconnect_socket(port_info);
508 if (port_info->encoded_bus_name)
509 free(port_info->encoded_bus_name);
510 if (port_info->port_name)
511 free(port_info->port_name);
515 if (g_list_length(remote_app_info->port_list) == 0) {
516 g_hash_table_remove(__remote_app_info,
517 remote_app_info->remote_app_id);
522 static gboolean __socket_disconnect_handler(GIOChannel *gio,
526 _LOGI("__socket_disconnect_handler %d", cond);
527 __free_port_info(data);
532 static int __get_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted,
533 message_port_remote_app_info_s **mri, port_list_info_s **pli)
535 message_port_remote_app_info_s *remote_app_info = NULL;
536 port_list_info_s port_info;
537 GList *cb_list = NULL;
538 int ret_val = MESSAGEPORT_ERROR_NONE;
540 remote_app_info = (message_port_remote_app_info_s *)g_hash_table_lookup(__remote_app_info, remote_app_id);
542 if (remote_app_info == NULL) {
543 remote_app_info = __set_remote_app_info(remote_app_id, remote_port, is_trusted);
545 if (remote_app_info == NULL) {
546 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
549 g_hash_table_insert(__remote_app_info, remote_app_info->remote_app_id, remote_app_info);
551 *mri = remote_app_info;
553 port_info.port_name = strdup(remote_port);
554 port_info.is_trusted = is_trusted;
555 cb_list = g_list_find_custom(remote_app_info->port_list, &port_info,
556 (GCompareFunc)__remote_port_compare_cb);
557 if (port_info.port_name)
558 free(port_info.port_name);
559 if (cb_list == NULL) {
560 port_list_info_s *tmp = __set_remote_port_info(remote_app_id, remote_port, is_trusted);
562 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
565 remote_app_info->port_list = g_list_append(remote_app_info->port_list, tmp);
566 tmp->remote_app_info = remote_app_info;
569 *pli = (port_list_info_s *)cb_list->data;
576 static bool __is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi)
581 g_hash_table_iter_init(&iter, __local_port_info);
582 while (g_hash_table_iter_next(&iter, &key, &value)) {
583 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)value;
585 if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
586 *local_id = mi->local_id;
595 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
597 GDBusMessage *msg = NULL;
598 GDBusMessage *reply = NULL;
603 msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
604 "org.freedesktop.DBus", "GetConnectionUnixProcessID");
606 _LOGE("Can't allocate new method call");
610 g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
611 reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
612 G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
616 _LOGE("Failed to get pid [%s]", err->message);
622 body = g_dbus_message_get_body(reply);
623 g_variant_get(body, "(u)", &pid);
629 g_object_unref(reply);
634 static int __write_socket(int fd,
637 unsigned int *bytes_write,
640 #define SEND_TIMEOUT 500 /* milliseconds */
642 unsigned int left = nbytes;
645 struct pollfd fds[1];
652 fds[0].events = POLLOUT;
655 ret = poll(fds, 1, SEND_TIMEOUT);
657 LOGE("__write_socket: : fd %d poll timeout", fd);
658 return MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
661 while (left && (retry_cnt < MAX_RETRY_CNT)) {
662 nb = write(fd, buffer, left);
664 if (errno == EINTR) {
665 LOGE("__write_socket: EINTR error continue ...");
669 LOGE("__write_socket: ...error fd %d: errno %d\n", fd, errno);
671 if (errno == EWOULDBLOCK || errno == EAGAIN)
672 return MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
674 return MESSAGEPORT_ERROR_IO_ERROR;
682 return MESSAGEPORT_ERROR_NONE;
685 static int __write_string_to_socket(int fd,
688 unsigned int *bytes_write,
693 ret = __write_socket(fd, (char *)&string_len, sizeof(string_len),
694 bytes_write, sequence);
695 if (ret != MESSAGEPORT_ERROR_NONE) {
696 _LOGE("write string_len fail");
700 if (string_len > 0) {
701 ret = __write_socket(fd, buffer, string_len, bytes_write, sequence);
702 if (ret != MESSAGEPORT_ERROR_NONE) {
703 _LOGE("wirte buffer fail");
710 return MESSAGEPORT_ERROR_NONE;
713 static int __read_socket(int fd,
716 unsigned int *bytes_read)
718 unsigned int left = nbytes;
721 const struct timespec TRY_SLEEP_TIME = { 0, 500 * 1000 * 1000 };
724 while (left && (retry_cnt < MAX_RETRY_CNT)) {
725 nb = read(fd, buffer, left);
727 LOGE("__read_socket: ...read EOF, socket closed %d: nb %d\n", fd, nb);
728 return MESSAGEPORT_ERROR_IO_ERROR;
729 } else if (nb == -1) {
730 /* wrt(nodejs) could change socket to none-blocking socket :-( */
731 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
732 LOGE("__read_socket: %d errno, sleep and retry ...", errno);
734 nanosleep(&TRY_SLEEP_TIME, 0);
737 LOGE("__read_socket: ...error fd %d: errno %d\n", fd, errno);
738 return MESSAGEPORT_ERROR_IO_ERROR;
746 return MESSAGEPORT_ERROR_NONE;
749 static int __read_string_from_socket(int fd, char **buffer, int *string_len)
752 if (__read_socket(fd, (char *)string_len, sizeof(*string_len), &nb) != MESSAGEPORT_ERROR_NONE) {
753 LOGE("read socket fail");
754 return MESSAGEPORT_ERROR_IO_ERROR;
756 if (*string_len > 0 && *string_len < MAX_MESSAGE_SIZE) {
757 *buffer = (char *)calloc(*string_len, sizeof(char));
758 if (*buffer == NULL) {
759 LOGE("Out of memory.");
760 return MESSAGEPORT_ERROR_IO_ERROR;
762 if (__read_socket(fd, *buffer, *string_len, &nb) != MESSAGEPORT_ERROR_NONE) {
763 LOGE("read socket fail");
764 return MESSAGEPORT_ERROR_IO_ERROR;
767 LOGE("Invalid string len %d", *string_len);
768 return MESSAGEPORT_ERROR_IO_ERROR;
770 return MESSAGEPORT_ERROR_NONE;
773 message_port_pkt_s *__message_port_recv_raw(int fd)
775 message_port_pkt_s *pkt = NULL;
778 pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
784 if (__read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGEPORT_ERROR_NONE) {
785 LOGE("read socket fail: port_name");
786 free(pkt->remote_port_name);
791 if (__read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGEPORT_ERROR_NONE) {
792 LOGE("read socket fail: is_bidirection");
793 free(pkt->remote_port_name);
798 if (__read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGEPORT_ERROR_NONE) {
799 LOGE("read socket fail: is_trusted");
800 free(pkt->remote_port_name);
805 if (__read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGEPORT_ERROR_NONE) {
806 LOGE("read socket fail: data");
809 free(pkt->remote_port_name);
817 static gboolean __socket_request_handler(GIOChannel *gio,
822 message_port_callback_info_s *mi;
823 message_port_pkt_s *pkt;
825 GError *error = NULL;
827 mi = (message_port_callback_info_s *)data;
830 g_io_channel_shutdown(gio, TRUE, &error);
832 _LOGE("g_io_channel_shutdown error : %s", error->message);
835 g_io_channel_unref(gio);
839 if (cond == G_IO_HUP) {
841 _LOGI("socket G_IO_HUP");
842 __callback_info_free_by_info(mi);
847 if ((fd = g_io_channel_unix_get_fd(gio)) < 0) {
848 _LOGE("fail to get fd from io channel");
849 __callback_info_free_by_info(mi);
853 if ((pkt = __message_port_recv_raw(fd)) == NULL) {
854 _LOGE("recv error on SOCKET");
855 __callback_info_free_by_info(mi);
859 kb = bundle_decode(pkt->data, pkt->data_len);
860 if (pkt->is_bidirection)
861 mi->callback(mi->local_id, mi->remote_app_id, pkt->remote_port_name, pkt->is_trusted, kb, NULL);
863 mi->callback(mi->local_id, mi->remote_app_id, NULL, pkt->is_trusted, kb, NULL);
867 if (pkt->remote_port_name)
868 free(pkt->remote_port_name);
878 static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation)
880 char *local_port = NULL;
881 char *local_appid = NULL;
882 char *remote_appid = NULL;
883 char *remote_port = NULL;
884 gboolean local_trusted = false;
885 gboolean remote_trusted = false;
886 gboolean bi_dir = false;
890 bundle_raw *raw = NULL;
891 message_port_local_port_info_s *mi;
892 int local_reg_id = 0;
893 message_port_callback_info_s *callback_info;
894 message_port_callback_info_s *head_callback_info;
895 GList *callback_info_list = NULL;
899 GUnixFDList *fd_list;
901 int *returned_fds = NULL;
904 g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
905 &remote_appid, &remote_port, &remote_trusted, &len, &raw);
908 _LOGE("Invalid argument : remote_port is NULL");
912 _LOGE("Invalid argument : remote_appid is NULL");
915 if (!__is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
916 _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
920 _LOGE("Invalid argument : local_appid");
924 _LOGE("Invalid argument : local_port");
927 if (strcmp(remote_appid, __app_id) != 0) {
928 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
931 if (strcmp(remote_port, mi->port_name) != 0) {
932 _LOGE("Invalid argument : remote_port (%s)", remote_port);
936 _LOGE("Invalid argument : data_len");
939 if (remote_trusted) {
940 if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
941 if (!__is_preloaded(local_appid, remote_appid)) {
942 int ret = __check_certificate(local_appid, remote_appid);
943 if (ret == MESSAGEPORT_ERROR_NONE)
944 g_hash_table_insert(__trusted_app_list_hash, local_appid, "TRUE");
946 _LOGE("The application (%s) is not signed with the same certificate",
954 callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
955 if (callback_info == NULL)
958 callback_info->local_id = mi->local_id;
959 callback_info->remote_app_id = strdup(local_appid);
960 callback_info->callback = mi->callback;
962 msg = g_dbus_method_invocation_get_message(invocation);
963 fd_list = g_dbus_message_get_unix_fd_list(msg);
965 /* When application send message to self fd_list is NULL */
966 if (fd_list != NULL) {
967 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
968 if (returned_fds == NULL) {
969 _LOGE("fail to get fds");
970 __callback_info_free(callback_info);
973 fd = returned_fds[0];
975 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
978 callback_info->gio_read = g_io_channel_unix_new(fd);
979 if (!callback_info->gio_read) {
980 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
981 __callback_info_free(callback_info);
985 callback_info->g_src_id = g_io_add_watch(callback_info->gio_read, G_IO_IN | G_IO_HUP,
986 __socket_request_handler, (gpointer)callback_info);
987 if (callback_info->g_src_id == 0) {
988 _LOGE("fail to add watch on socket");
989 __callback_info_free(callback_info);
993 callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(mi->local_id));
994 if (callback_info_list == NULL) {
995 head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
996 if (head_callback_info == NULL) {
997 _LOGE("fail to alloc head_callback_info");
998 __callback_info_free(callback_info);
1001 head_callback_info->local_id = 0;
1002 head_callback_info->remote_app_id = NULL;
1003 head_callback_info->callback = NULL;
1004 head_callback_info->gio_read = NULL;
1005 head_callback_info->g_src_id = 0;
1006 callback_info_list = g_list_append(callback_info_list, head_callback_info);
1007 callback_info_list = g_list_append(callback_info_list, callback_info);
1008 g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(mi->local_id), callback_info_list);
1010 callback_info_list = g_list_append(callback_info_list, callback_info);
1015 data = bundle_decode(raw, len);
1017 _LOGE("Invalid argument : message");
1021 LOGD("call calback %s", local_appid);
1023 mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, NULL);
1025 mi->callback(mi->local_id, local_appid, NULL, false, data, NULL);
1034 static int __check_remote_port(const char *remote_app_id, const char *remote_port, bool is_trusted, bool *exist)
1036 _LOGD("Check a remote port : [%s:%s]", remote_app_id, remote_port);
1038 GVariant *result = NULL;
1040 int ret_val = MESSAGEPORT_ERROR_NONE;
1041 char *bus_name = NULL;
1042 message_port_remote_app_info_s *remote_app_info = NULL;
1043 port_list_info_s *port_info = NULL;
1044 int local_reg_id = 0;
1045 message_port_local_port_info_s *mi = NULL;
1046 gboolean name_exist = false;
1048 _LOGD("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
1050 ret_val = __get_remote_port_info(remote_app_id, remote_port, is_trusted, &remote_app_info, &port_info);
1051 if (ret_val != MESSAGEPORT_ERROR_NONE)
1055 if (strcmp(remote_app_id, __app_id) == 0) {
1057 _LOGD("__is_local_port_registed ");
1058 if (!__is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
1063 _LOGD("__is_local_port_registed : %d ", *exist);
1064 return MESSAGEPORT_ERROR_NONE;
1067 port_info->exist = false;
1068 bus_name = port_info->encoded_bus_name;
1070 result = g_dbus_connection_call_sync(
1074 DBUS_INTERFACE_DBUS,
1076 g_variant_new("(s)", bus_name),
1077 G_VARIANT_TYPE("(b)"),
1078 G_DBUS_CALL_FLAGS_NONE,
1083 if (err || (result == NULL)) {
1085 _LOGE("No reply. error = %s", err->message);
1088 ret_val = MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
1090 g_variant_get(result, "(b)", &name_exist);
1093 _LOGI("Name not exist %s", bus_name);
1095 ret_val = MESSAGEPORT_ERROR_NONE;
1099 if (remote_app_info->certificate_info != CERTIFICATE_MATCH) {
1100 if (!__is_preloaded(__app_id, remote_app_id)) {
1101 if (__check_certificate(__app_id, remote_app_id) != MESSAGEPORT_ERROR_NONE) {
1102 ret_val = MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
1106 remote_app_info->certificate_info = CERTIFICATE_MATCH;
1109 port_info->exist = true;
1111 ret_val = MESSAGEPORT_ERROR_NONE;
1116 g_variant_unref(result);
1118 if (ret_val != MESSAGEPORT_ERROR_NONE || !name_exist)
1119 __free_port_info((gpointer)port_info);
1124 static void __on_sender_name_appeared(GDBusConnection *connection,
1126 const gchar *name_owner,
1129 _LOGI("sender name appeared : %s", name);
1132 static void __on_sender_name_vanished(GDBusConnection *connection,
1136 gboolean remove_result = FALSE;
1137 int *watcher_id = (int *)user_data;
1138 remove_result = g_hash_table_remove(__sender_appid_hash, (gpointer)name);
1140 _LOGE("Fail to remove sender appid from hash : %s", name);
1143 if (*watcher_id > 0)
1144 g_bus_unwatch_name(*watcher_id);
1146 LOGE("Invalid watcher_id %d", *watcher_id);
1149 LOGE("watcher_id is NULL");
1153 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
1156 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
1157 char *local_appid = NULL;
1158 int pid = __get_sender_pid(conn, sender);
1159 int *watcher_id = (int *)calloc(1, sizeof(int));
1160 retvm_if(!watcher_id, false, "Malloc failed");
1162 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
1163 if (ret != AUL_R_OK) {
1164 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
1169 g_variant_get_child(parameters, 0, "&s", &local_appid);
1170 if (local_appid == NULL) {
1171 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
1176 if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
1177 _LOGD("insert sender !!!!! %s", sender);
1178 g_hash_table_insert(__sender_appid_hash, (gpointer)strdup(sender), GINT_TO_POINTER(pid));
1179 *watcher_id = g_bus_watch_name_on_connection(
1182 G_BUS_NAME_WATCHER_FLAGS_NONE,
1183 __on_sender_name_appeared,
1184 __on_sender_name_vanished,
1194 static void __dbus_method_call_handler(GDBusConnection *conn,
1195 const gchar *sender, const gchar *object_path,
1196 const gchar *iface_name, const gchar *method_name,
1197 GVariant *parameters, GDBusMethodInvocation *invocation,
1200 _LOGI("method_name: %s, sender: %s", method_name, sender);
1201 gpointer sender_pid = g_hash_table_lookup(__sender_appid_hash, sender);
1202 if (sender_pid == NULL) {
1203 if (!__check_sender_validation(parameters, sender, conn))
1206 if (g_strcmp0(method_name, "send_message") == 0)
1207 send_message(parameters, invocation);
1209 g_dbus_method_invocation_return_value(invocation, NULL);
1212 static const GDBusInterfaceVTable interface_vtable = {
1213 __dbus_method_call_handler,
1218 static int __dbus_init(void)
1221 GError *error = NULL;
1223 __gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
1224 if (__gdbus_conn == NULL) {
1225 if (error != NULL) {
1226 _LOGE("Failed to get dbus [%s]", error->message);
1227 g_error_free(error);
1236 g_object_unref(__gdbus_conn);
1242 int __register_dbus_interface(const char *port_name, bool is_trusted)
1245 GDBusNodeInfo *introspection_data = NULL;
1246 int registration_id = 0;
1248 static gchar introspection_prefix[] =
1250 " <interface name='";
1252 static gchar introspection_postfix[] =
1254 " <method name='send_message'>"
1255 " <arg type='s' name='local_appid' direction='in'/>"
1256 " <arg type='s' name='local_port' direction='in'/>"
1257 " <arg type='b' name='local_trusted' direction='in'/>"
1258 " <arg type='b' name='bi_dir' direction='in'/>"
1259 " <arg type='s' name='remote_appid' direction='in'/>"
1260 " <arg type='s' name='remote_port' direction='in'/>"
1261 " <arg type='b' name='remote_trusted' direction='in'/>"
1262 " <arg type='u' name='data_len' direction='in'/>"
1263 " <arg type='s' name='data' direction='in'/>"
1268 char *introspection_xml = NULL;
1269 int introspection_xml_len = 0;
1273 GError *error = NULL;
1274 char *bus_name = NULL;
1275 char *interface_name = NULL;
1276 GVariant *result = NULL;
1278 bus_name = __get_encoded_name(__app_id, port_name, is_trusted);
1280 _LOGE("Fail to get bus name");
1283 interface_name = bus_name;
1285 introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
1286 strlen(introspection_postfix) + 1;
1288 introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
1289 if (!introspection_xml) {
1290 _LOGE("out of memory");
1295 result = g_dbus_connection_call_sync(
1299 DBUS_INTERFACE_DBUS,
1301 g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
1302 G_VARIANT_TYPE("(u)"),
1303 G_DBUS_CALL_FLAGS_NONE,
1308 _LOGE("RequestName fail : %s", error->message);
1309 g_error_free(error);
1312 if (result == NULL) {
1313 _LOGE("fail to get name NULL");
1316 g_variant_get(result, "(u)", &owner_id);
1317 if (owner_id == 0) {
1318 _LOGE("Acquiring the own name is failed");
1322 _LOGD("Acquiring the own name : %d", owner_id);
1324 snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
1326 introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
1327 if (!introspection_data) {
1328 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
1332 registration_id = g_dbus_connection_register_object(__gdbus_conn,
1333 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
1334 &interface_vtable, NULL, NULL, NULL);
1336 _LOGD("registration_id %d", registration_id);
1338 if (registration_id == 0) {
1339 _LOGE("Failed to g_dbus_connection_register_object");
1344 if (introspection_data)
1345 g_dbus_node_info_unref(introspection_data);
1346 if (introspection_xml)
1347 free(introspection_xml);
1351 g_variant_unref(result);
1354 return registration_id;
1357 /* LCOV_EXCL_START */
1358 static void __hash_destory_local_value(gpointer data)
1360 message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
1363 free(mli->port_name);
1367 /* LCOV_EXCL_STOP */
1369 /* LCOV_EXCL_START */
1370 static void __hash_destory_remote_value(gpointer data)
1372 message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
1374 FREE_AND_NULL(mri->remote_app_id);
1376 g_list_free_full(mri->port_list, __free_port_info);
1381 /* LCOV_EXCL_STOP */
1383 static bool __initialize(void)
1386 #if !GLIB_CHECK_VERSION(2, 35, 0)
1392 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
1394 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
1395 retvm_if(ret != AUL_R_OK, false, "Failed to get the application ID: %d", ret);
1397 __app_id = strdup(buffer);
1398 retvm_if(!__app_id, false, "Malloc failed");
1399 _LOGI("init : %s", __app_id);
1401 if (__local_port_info == NULL) {
1402 __local_port_info = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destory_local_value);
1403 retvm_if(!__local_port_info, false, "fail to create __local_port_info");
1406 if (__remote_app_info == NULL) {
1407 __remote_app_info = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __hash_destory_remote_value);
1408 retvm_if(!__remote_app_info, false, "fail to create __remote_app_info");
1411 if (__sender_appid_hash == NULL) {
1412 __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
1413 retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
1416 if (__trusted_app_list_hash == NULL) {
1417 __trusted_app_list_hash = g_hash_table_new(g_str_hash, g_str_equal);
1418 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
1421 if (__callback_info_hash == NULL) {
1422 __callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destroy_callback_info);
1423 retvm_if(!__callback_info_hash, false, "fail to create __callback_info_hash");
1428 _initialized = true;
1434 static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, messageport_message_cb callback)
1436 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
1437 retvm_if(!mi, false, "Malloc failed");
1439 mi->callback = callback;
1440 mi->is_trusted = is_trusted;
1441 mi->port_name = strdup(local_port);
1442 if (mi->port_name == NULL) {
1443 _LOGE("Malloc failed (%s)", local_port);
1447 mi->local_id = local_id;
1449 g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
1453 static int __register_message_port(const char *local_port, bool is_trusted, messageport_message_cb callback)
1455 _SECURE_LOGI("local_port : [%s:%s]", local_port, is_trusted ? "trusted" : "non-trusted");
1459 /* Check the message port is already registed */
1460 if (__is_local_port_registed(local_port, is_trusted, &local_id, NULL))
1463 local_id = __register_dbus_interface(local_port, is_trusted);
1465 _LOGE("register_dbus_interface fail !!");
1466 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1469 if (!__message_port_register_port(local_id, local_port, is_trusted, callback))
1470 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1475 static void __free_delay_message_info(delay_message_info_s *message)
1477 if (message != NULL) {
1478 FREE_AND_NULL(message->local_port_name);
1479 FREE_AND_NULL(message->data);
1480 FREE_AND_NULL(message);
1484 static void __free_list_delay_message_info(gpointer data)
1486 delay_message_info_s *message = (delay_message_info_s *)data;
1488 if (message != NULL)
1489 __free_delay_message_info(message);
1492 static int __send_delayed_message(int sockfd, delay_message_info_s *message)
1494 unsigned int nb = 0;
1495 int sequence = message->sequence - 1;
1496 int ret = MESSAGEPORT_ERROR_NONE;
1497 bool is_startline = true;
1500 _LOGI("send_delayed_message : sockfd (%d) sequence(%d) sent byte(%d)",
1501 sockfd, message->sequence, message->sent_bytes);
1503 switch (message->sequence) {
1504 case SEQUENCE_START:
1506 is_startline = false;
1508 case SEQUENCE_PORT_LEN:
1510 offset = message->sent_bytes;
1512 ret = __write_socket(sockfd, ((char *)&message->local_port_len) + offset,
1513 sizeof(message->local_port_len) - offset, &nb, &sequence);
1514 if (ret != MESSAGEPORT_ERROR_NONE) {
1515 _LOGE("write local_port_len fail");
1519 is_startline = false;
1521 case SEQUENCE_PORT_NAME:
1523 offset = message->sent_bytes;
1525 if (message->local_port_len > 0)
1526 ret = __write_socket(sockfd, message->local_port_name + offset,
1527 message->local_port_len - offset , &nb, &sequence);
1531 if (ret != MESSAGEPORT_ERROR_NONE) {
1532 _LOGE("write local_port fail");
1536 is_startline = false;
1538 case SEQUENCE_BIDIRECTION:
1540 offset = message->sent_bytes;
1542 ret = __write_socket(sockfd, ((char *)&message->is_bidirection) + offset,
1543 sizeof(message->is_bidirection) - offset, &nb, &sequence);
1544 if (ret != MESSAGEPORT_ERROR_NONE) {
1545 _LOGE("write is_bidirection fail");
1549 is_startline = false;
1551 case SEQUENCE_TRUSTED:
1553 offset = message->sent_bytes;
1555 ret = __write_socket(sockfd, ((char *)&message->local_trusted) + offset,
1556 sizeof(message->local_trusted) - offset, &nb, &sequence);
1557 if (ret != MESSAGEPORT_ERROR_NONE) {
1558 _LOGE("write local_trusted fail");
1562 is_startline = false;
1564 case SEQUENCE_DTAT_LEN:
1566 offset = message->sent_bytes;
1568 ret = __write_socket(sockfd, ((char *)&message->data_len) + offset,
1569 sizeof(message->data_len) - offset, &nb, &sequence);
1570 if (ret != MESSAGEPORT_ERROR_NONE) {
1571 _LOGE("write data_len fail");
1575 is_startline = false;
1579 offset = message->sent_bytes;
1581 ret = __write_socket(sockfd, (char *)message->data + offset,
1582 message->data_len -offset, &nb, &sequence);
1584 if (ret != MESSAGEPORT_ERROR_NONE) {
1585 _LOGE("write data fail");
1589 is_startline = false;
1592 ret = MESSAGEPORT_ERROR_NONE;
1597 if (ret == MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE) {
1599 message->sent_bytes += nb;
1601 message->sent_bytes = nb;
1603 message->sequence = sequence;
1604 _LOGE("send_delayed_message fail : sockfd (%d) sequence(%d) sent byte(%d)",
1605 sockfd, message->sequence, message->sent_bytes);
1612 static gboolean __process_delayed_message(gint fd, GIOCondition cond, gpointer data)
1614 port_list_info_s *port_info = (port_list_info_s *)data;
1615 delay_message_info_s *message;
1618 if (port_info == NULL)
1619 return G_SOURCE_REMOVE;
1621 pthread_mutex_lock(&mutex);
1623 if (port_info->delayed_message_list == NULL) {
1624 port_info->delayed_message_size = 0;
1625 port_info->delay_src_id = 0;
1626 pthread_mutex_unlock(&mutex);
1627 return G_SOURCE_REMOVE;
1629 message = g_list_nth_data(port_info->delayed_message_list, 0);
1630 ret = __send_delayed_message(port_info->send_sock_fd, message);
1632 if (ret == MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE) {
1633 pthread_mutex_unlock(&mutex);
1634 return G_SOURCE_CONTINUE;
1635 } else if (ret == MESSAGEPORT_ERROR_IO_ERROR) {
1636 __free_port_info((gpointer)port_info);
1637 pthread_mutex_unlock(&mutex);
1638 return G_SOURCE_REMOVE;
1641 port_info->delayed_message_size -= message->size;
1643 port_info->delayed_message_list = g_list_remove(port_info->delayed_message_list, message);
1644 __free_delay_message_info(message);
1647 pthread_mutex_unlock(&mutex);
1649 return G_SOURCE_CONTINUE;
1652 static int __insert_delayed_message(port_list_info_s *port_info,
1654 bundle_raw *kb_data,
1656 unsigned int sent_bytes,
1657 const char *local_port,
1659 bool is_bidirection)
1661 #define QUEUE_SIZE_MAX (1024 * 1024) /* 1MB per remote port (MAX) */
1663 unsigned int tmp_size;
1664 unsigned int message_size;
1665 int ret = MESSAGEPORT_ERROR_NONE;
1667 if (port_info->delayed_message_size >= QUEUE_SIZE_MAX) {
1668 _LOGE("cache fail : delayed_message_size (%d), count(%d)",
1669 port_info->delayed_message_size, g_list_length(port_info->delayed_message_list));
1670 return MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
1673 delay_message_info_s *message = (delay_message_info_s *)calloc(1, sizeof(delay_message_info_s));
1674 retvm_if(!message, MESSAGEPORT_ERROR_OUT_OF_MEMORY, "Malloc failed");
1676 message_size = sizeof(delay_message_info_s);
1678 message->sequence = sequence;
1679 tmp_size = strlen(local_port) + 1;
1680 message_size += tmp_size;
1681 message->local_port_len = tmp_size;
1682 message->local_port_name = strdup(local_port);
1683 if (message->local_port_name == NULL) {
1684 _LOGE("local_port_name strdup fail");
1685 ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1688 message->is_bidirection = is_bidirection;
1689 message->local_trusted = local_trusted;
1690 message_size += data_len;
1691 message->data_len = data_len;
1692 message->data = (bundle_raw *)strdup((const char *)kb_data);
1693 if (message->data == NULL) {
1694 _LOGE("data strdup fail");
1695 ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1700 message->sent_bytes = sent_bytes;
1701 message->size = message_size;
1702 port_info->delayed_message_size += message_size;
1704 port_info->delayed_message_list = g_list_append(port_info->delayed_message_list, message);
1706 if (port_info->delay_src_id == 0) {
1707 port_info->delay_src_id = g_unix_fd_add_full(G_PRIORITY_DEFAULT,
1708 port_info->send_sock_fd, G_IO_OUT, __process_delayed_message,
1712 _LOGE("inserted : pm(%s) fd(%d) ms(%d) ds(%d) dlc(%d) sqn(%d) sb (%d)",
1713 port_info->port_name, port_info->send_sock_fd, message_size,
1714 port_info->delayed_message_size,
1715 g_list_length(port_info->delayed_message_list), sequence, sent_bytes);
1720 if (ret != MESSAGEPORT_ERROR_NONE)
1721 __free_delay_message_info(message);
1726 int __message_port_send_async(port_list_info_s *port_info, bundle *kb, const char *local_port,
1727 bool local_trusted, bool is_bidirection)
1731 int local_port_len = 0;
1732 unsigned int nb = 0;
1733 bundle_raw *kb_data = NULL;
1734 int sequence = SEQUENCE_START;
1736 bundle_encode(kb, &kb_data, &data_len);
1737 if (kb_data == NULL) {
1738 _LOGE("bundle encode fail");
1739 ret = MESSAGEPORT_ERROR_INVALID_PARAMETER;
1743 if (data_len > MAX_MESSAGE_SIZE) {
1744 _LOGE("bigger than max size\n");
1745 ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1749 if (g_list_length(port_info->delayed_message_list) > 0) {
1750 ret = MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
1751 _LOGE("There are messages in the delayed_message_list (count %d)",
1752 g_list_length(port_info->delayed_message_list));
1756 if (local_port != NULL)
1757 local_port_len = strlen(local_port) + 1;
1759 ret = __write_string_to_socket(port_info->send_sock_fd, local_port,
1760 local_port_len, &nb, &sequence);
1761 if (ret != MESSAGEPORT_ERROR_NONE) {
1762 _LOGE("write local_port fail");
1766 ret = __write_socket(port_info->send_sock_fd, (char *)&is_bidirection,
1767 sizeof(is_bidirection), &nb, &sequence);
1768 if (ret != MESSAGEPORT_ERROR_NONE) {
1769 _LOGE("write is_bidirection fail");
1773 ret = __write_socket(port_info->send_sock_fd, (char *)&local_trusted,
1774 sizeof(local_trusted), &nb, &sequence);
1775 if (ret != MESSAGEPORT_ERROR_NONE) {
1776 _LOGE("write local_trusted fail");
1780 ret = __write_string_to_socket(port_info->send_sock_fd, (void *)kb_data,
1781 data_len, &nb, &sequence);
1782 if (ret != MESSAGEPORT_ERROR_NONE) {
1783 _LOGE("write kb_data fail");
1788 if (ret == MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE) {
1789 ret = __insert_delayed_message(port_info, sequence, kb_data, data_len, nb,
1790 local_port, local_trusted, is_bidirection);
1791 if (ret != MESSAGEPORT_ERROR_NONE)
1792 ret = MESSAGEPORT_ERROR_IO_ERROR;
1801 static int __message_port_send_message(const char *remote_appid, const char *remote_port,
1802 const char *local_port, bool trusted_message, bool local_trusted, bool bi_dir, bundle *message)
1805 int ret = MESSAGEPORT_ERROR_NONE;
1806 GUnixFDList *fd_list = NULL;
1809 bundle_raw *raw = NULL;
1810 char *bus_name = NULL;
1811 char *interface_name = NULL;
1813 message_port_remote_app_info_s *remote_app_info = NULL;
1814 port_list_info_s *port_info = NULL;
1815 GDBusMessage *msg = NULL;
1817 GVariant *body = NULL;
1818 int sock_pair[2] = {0,};
1821 ret = __get_remote_port_info(remote_appid, remote_port, trusted_message, &remote_app_info, &port_info);
1822 if (ret != MESSAGEPORT_ERROR_NONE)
1825 if (port_info->exist == false) {
1827 _LOGD("port exist check !!");
1828 ret = __check_remote_port(remote_appid, remote_port, trusted_message, &exist);
1829 if (ret != MESSAGEPORT_ERROR_NONE)
1832 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1835 if (port_info->send_sock_fd > 0) {
1836 ret = __message_port_send_async(port_info, message,
1837 (local_port) ? local_port : "", local_trusted, bi_dir);
1840 bus_name = port_info->encoded_bus_name;
1841 interface_name = bus_name;
1843 if (bundle_encode(message, &raw, &len) != BUNDLE_ERROR_NONE) {
1844 ret = MESSAGEPORT_ERROR_INVALID_PARAMETER;
1848 if (MAX_MESSAGE_SIZE < len) {
1849 _LOGE("The size of message (%d) has exceeded the maximum limit.", len);
1850 ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1854 body = g_variant_new("(ssbbssbus)", __app_id, (local_port) ? local_port : "", local_trusted, bi_dir,
1855 remote_appid, remote_port, trusted_message, len, raw);
1856 if (strcmp(remote_appid, __app_id) != 0) { /* self send */
1858 /* if message-port fail to get socket pair, communicate using GDBus */
1859 if (aul_request_message_port_socket_pair(sock_pair) != AUL_R_OK) {
1860 _LOGE("error create socket pair");
1863 _LOGI("sock pair : %d, %d",
1864 sock_pair[SOCK_PAIR_SENDER], sock_pair[SOCK_PAIR_RECEIVER]);
1865 fd_list = g_unix_fd_list_new();
1866 g_unix_fd_list_append(fd_list, sock_pair[SOCK_PAIR_RECEIVER], &err);
1868 _LOGE("g_unix_fd_list_append [%s]", err->message);
1869 ret = MESSAGEPORT_ERROR_IO_ERROR;
1874 port_info->send_sock_fd = sock_pair[SOCK_PAIR_SENDER];
1875 close(sock_pair[SOCK_PAIR_RECEIVER]);
1876 sock_pair[SOCK_PAIR_RECEIVER] = 0;
1878 port_info->gio_read = g_io_channel_unix_new(port_info->send_sock_fd);
1879 if (!port_info->gio_read) {
1880 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
1881 ret = MESSAGEPORT_ERROR_IO_ERROR;
1885 port_info->g_src_id = g_io_add_watch(
1886 port_info->gio_read,
1888 __socket_disconnect_handler,
1889 (gpointer)port_info);
1890 if (port_info->g_src_id == 0) {
1891 _LOGE("fail to add watch on socket");
1892 ret = MESSAGEPORT_ERROR_IO_ERROR;
1899 msg = g_dbus_message_new_method_call(bus_name, MESSAGEPORT_OBJECT_PATH, interface_name, "send_message");
1901 _LOGE("Can't allocate new method call");
1902 ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1906 g_dbus_message_set_unix_fd_list(msg, fd_list);
1907 g_dbus_message_set_body(msg, body);
1908 g_dbus_connection_send_message(__gdbus_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
1910 _LOGE("No reply. error = %s", err->message);
1912 ret = MESSAGEPORT_ERROR_IO_ERROR;
1919 g_object_unref(msg);
1921 bundle_free_encoded_rawdata(&raw);
1923 g_object_unref(fd_list);
1925 if (ret != MESSAGEPORT_ERROR_NONE) {
1926 __free_port_info((gpointer)port_info);
1927 if (sock_pair[SOCK_PAIR_SENDER])
1928 close(sock_pair[SOCK_PAIR_SENDER]);
1929 if (sock_pair[SOCK_PAIR_RECEIVER])
1930 close(sock_pair[SOCK_PAIR_RECEIVER]);
1936 int __message_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port, bool trusted_message, bundle *message)
1938 message_port_local_port_info_s *local_info;
1939 int ret = __get_local_port_info(id, &local_info);
1940 if (ret != MESSAGEPORT_ERROR_NONE)
1943 _LOGD("bidirectional_message %s", local_info->port_name);
1944 return __message_port_send_message(remote_app_id, remote_port,
1945 local_info->port_name, trusted_message, local_info->is_trusted, true, message);
1948 static void __name_registered(GDBusConnection *connection,
1950 const gchar *name_owner,
1954 registered_callback_info_s *info = (registered_callback_info_s *)user_data;
1956 LOGE("NULL registered_callback_info");
1960 _LOGI("watcher_id : %d, appeared name : %s , name_owner : %s\n", info->watcher_id, name, name_owner);
1961 if (info->registered_cb)
1962 info->registered_cb(info->remote_app_id, info->remote_port, info->is_trusted, info->user_data);
1965 static void __name_unregistered(GDBusConnection *connection,
1970 registered_callback_info_s *info = (registered_callback_info_s *)user_data;
1972 LOGE("NULL registered_callback_info");
1976 _LOGI("watcher_id : %d, vanished name : %s\n", info->watcher_id, name);
1977 if (info->unregistered_cb)
1978 info->unregistered_cb(info->remote_app_id, info->remote_port, info->is_trusted, info->user_data);
1981 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)
1983 int ret_val = MESSAGEPORT_ERROR_NONE;
1984 message_port_remote_app_info_s *remote_app_info = NULL;
1985 port_list_info_s *port_info = NULL;
1987 _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
1989 ret_val = __get_remote_port_info(remote_app_id, remote_port, trusted_remote_port, &remote_app_info, &port_info);
1990 if (ret_val != MESSAGEPORT_ERROR_NONE)
1993 if (__registered_callback_info_hash == NULL)
1994 __registered_callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __registered_callback_info_free);
1996 registered_callback_info_s *registered_cb_info = (registered_callback_info_s *)calloc(1, sizeof(registered_callback_info_s));
1997 retvm_if(!registered_cb_info, MESSAGEPORT_ERROR_OUT_OF_MEMORY, "Malloc failed");
1999 registered_cb_info->registered_cb = registered_cb;
2000 registered_cb_info->unregistered_cb = unregistered_cb;
2001 registered_cb_info->user_data = user_data;
2002 registered_cb_info->remote_app_id = strdup(remote_app_info->remote_app_id);
2003 if (registered_cb_info->remote_app_id == NULL) {
2004 free(registered_cb_info);
2005 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
2007 registered_cb_info->remote_port = strdup(port_info->port_name);
2008 if (registered_cb_info->remote_port == NULL) {
2009 free(registered_cb_info->remote_app_id);
2010 free(registered_cb_info);
2011 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
2014 registered_cb_info->watcher_id = g_bus_watch_name_on_connection(
2016 port_info->encoded_bus_name,
2017 G_BUS_NAME_WATCHER_FLAGS_NONE,
2019 __name_unregistered,
2022 if (registered_cb_info->watcher_id == 0) {
2023 free(registered_cb_info->remote_app_id);
2024 free(registered_cb_info->remote_port);
2025 free(registered_cb_info);
2026 return MESSAGEPORT_ERROR_IO_ERROR;
2029 g_hash_table_insert(__registered_callback_info_hash,
2030 GINT_TO_POINTER(registered_cb_info->watcher_id), registered_cb_info);
2032 *watcher_id = registered_cb_info->watcher_id;
2033 return MESSAGEPORT_ERROR_NONE;
2036 int messageport_unregister_local_port(int local_port_id, bool trusted_port)
2040 char *bus_name = NULL;
2044 _LOGI("unregister : %d", local_port_id);
2046 message_port_local_port_info_s *mi =
2047 (message_port_local_port_info_s *)
2048 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
2050 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
2052 if (mi->is_trusted != trusted_port)
2053 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
2055 g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
2057 bus_name = __get_encoded_name(__app_id, mi->port_name, mi->is_trusted);
2058 if (bus_name == NULL)
2059 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
2061 g_dbus_connection_unregister_object(__gdbus_conn, local_port_id);
2063 result = g_dbus_connection_call_sync(
2067 DBUS_INTERFACE_DBUS,
2069 g_variant_new("(s)", bus_name),
2070 G_VARIANT_TYPE("(u)"),
2071 G_DBUS_CALL_FLAGS_NONE,
2080 _LOGE("RequestName fail : %s", err->message);
2082 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
2084 g_variant_get(result, "(u)", &ret);
2087 g_variant_unref(result);
2089 if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
2091 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
2092 _LOGE("Port Not exist");
2093 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
2094 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
2095 _LOGE("Try to release not owned name. MESSAGEPORT_ERROR_INVALID_PARAMETER");
2096 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
2101 g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
2103 return MESSAGEPORT_ERROR_NONE;
2106 int messageport_register_local_port(const char *local_port, messageport_message_cb callback)
2108 if (!_initialized) {
2109 if (!__initialize())
2110 return MESSAGEPORT_ERROR_IO_ERROR;
2113 return __register_message_port(local_port, false, callback);
2116 int messageport_register_trusted_local_port(const char *local_port, messageport_message_cb callback)
2118 if (!_initialized) {
2119 if (!__initialize())
2120 return MESSAGEPORT_ERROR_IO_ERROR;
2123 return __register_message_port(local_port, true, callback);
2127 int messageport_check_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
2129 if (!_initialized) {
2130 if (!__initialize())
2131 return MESSAGEPORT_ERROR_IO_ERROR;
2134 int ret = __check_remote_port(remote_app_id, remote_port, false, exist);
2135 if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
2137 ret = MESSAGEPORT_ERROR_NONE;
2143 int messageport_check_trusted_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
2145 if (!_initialized) {
2146 if (!__initialize())
2147 return MESSAGEPORT_ERROR_IO_ERROR;
2150 int ret = __check_remote_port(remote_app_id, remote_port, true, exist);
2151 if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
2153 ret = MESSAGEPORT_ERROR_NONE;
2159 int messageport_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
2161 if (!_initialized) {
2162 if (!__initialize())
2163 return MESSAGEPORT_ERROR_IO_ERROR;
2166 return __message_port_send_message(remote_app_id, remote_port, NULL, false, false, false, message);
2169 int messageport_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
2171 if (!_initialized) {
2172 if (!__initialize())
2173 return MESSAGEPORT_ERROR_IO_ERROR;
2176 return __message_port_send_message(remote_app_id, remote_port, NULL, true, false, false, message);
2179 int messageport_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port,
2182 if (!_initialized) {
2183 if (!__initialize())
2184 return MESSAGEPORT_ERROR_IO_ERROR;
2187 return __message_send_bidirectional_message(id, remote_app_id, remote_port, false, message);
2190 int messageport_send_bidirectional_trusted_message(int id, const char *remote_app_id, const char *remote_port,
2193 if (!_initialized) {
2194 if (!__initialize())
2195 return MESSAGEPORT_ERROR_IO_ERROR;
2197 return __message_send_bidirectional_message(id, remote_app_id, remote_port, true, message);
2200 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)
2202 if (!_initialized) {
2203 if (!__initialize())
2204 return MESSAGEPORT_ERROR_IO_ERROR;
2206 return __messageport_watch_remote_port(watcher_id, remote_app_id, remote_port, is_trusted, registered_cb, NULL, user_data);
2209 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)
2211 if (!_initialized) {
2212 if (!__initialize())
2213 return MESSAGEPORT_ERROR_IO_ERROR;
2215 return __messageport_watch_remote_port(watcher_id, remote_app_id, remote_port, is_trusted, NULL, unregistered_cb, user_data);
2219 int messageport_remove_registration_event_cb(int watcher_id)
2221 registered_callback_info_s *registered_cb_info = NULL;
2222 gboolean remove_result = FALSE;
2225 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
2227 registered_cb_info = g_hash_table_lookup(__registered_callback_info_hash, GINT_TO_POINTER(watcher_id));
2228 if (registered_cb_info == NULL)
2229 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
2231 remove_result = g_hash_table_remove(__registered_callback_info_hash, GINT_TO_POINTER(watcher_id));
2233 return MESSAGEPORT_ERROR_IO_ERROR;
2235 g_bus_unwatch_name(watcher_id);
2237 return MESSAGEPORT_ERROR_NONE;