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 "message-port.h"
39 #include "message-port-log.h"
41 #define MAX_PACKAGE_STR_SIZE 512
42 #define MESSAGEPORT_BUS_NAME_PREFIX "org.tizen.messageport._"
43 #define MESSAGEPORT_OBJECT_PATH "/org/tizen/messageport"
44 #define MESSAGEPORT_INTERFACE_PREFIX "org.tizen.messageport._"
46 #define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
47 #define DBUS_PATH_DBUS "/org/freedesktop/DBus"
48 #define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
50 #define DBUS_RELEASE_NAME_REPLY_RELEASED 1 /* *< Service was released from the given name */
51 #define DBUS_RELEASE_NAME_REPLY_NON_EXISTENT 2 /* *< The given name does not exist on the bus */
52 #define DBUS_RELEASE_NAME_REPLY_NOT_OWNER 3 /* *< Service is not an owner of the given name */
54 #define MAX_RETRY_CNT 10
55 #define SOCK_PAIR_SENDER 0
56 #define SOCK_PAIR_RECEIVER 1
59 #define retvm_if(expr, val, fmt, arg...) do { \
62 _LOGE("(%s) -> %s() return", #expr, __func__); \
67 #define retv_if(expr, val) do { \
69 _LOGE("(%s) -> %s() return", #expr, __func__); \
74 #define FREE_AND_NULL(ptr) do { \
81 static bool _initialized = false;
82 static GDBusConnection *__gdbus_conn;
83 static char *__app_id;
84 static GHashTable *__local_port_info;
85 static GHashTable *__remote_app_info;
86 static GHashTable *__sender_appid_hash;
87 static GHashTable *__trusted_app_list_hash;
88 static GHashTable *__callback_info_hash;
89 static const int MAX_MESSAGE_SIZE = 16 * 1024;
91 enum __certificate_info_type {
94 CERTIFICATE_NOT_MATCH,
97 typedef struct message_port_pkt {
98 int remote_port_name_len;
99 char *remote_port_name;
104 } message_port_pkt_s;
106 typedef struct message_port_callback_info {
107 messageport_message_cb callback;
110 GIOChannel *gio_read;
112 } message_port_callback_info_s;
114 typedef struct message_port_local_port_info {
115 messageport_message_cb callback;
119 } message_port_local_port_info_s;
121 typedef struct message_port_remote_port_info {
124 int certificate_info;
126 } message_port_remote_app_info_s;
128 typedef struct port_list_info {
130 char *encoded_bus_name;
135 GIOChannel *gio_read;
139 static void __callback_info_free(gpointer data)
141 message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
142 GError *error = NULL;
143 if (callback_info == NULL)
146 if (callback_info->remote_app_id)
147 FREE_AND_NULL(callback_info->remote_app_id);
149 if (callback_info->gio_read != NULL) {
150 g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
152 _LOGE("g_io_channel_shutdown error : %s", error->message);
155 g_io_channel_unref(callback_info->gio_read);
156 callback_info->gio_read = NULL;
159 if (callback_info->g_src_id != 0) {
160 g_source_remove(callback_info->g_src_id);
161 callback_info->g_src_id = 0;
164 FREE_AND_NULL(callback_info);
167 static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
169 GList *callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
172 if (callback_info_list == NULL)
175 find_list = g_list_find(callback_info_list, callback_info);
176 if (find_list == NULL)
179 callback_info_list = g_list_remove_link(callback_info_list, find_list);
180 __callback_info_free(callback_info);
181 g_list_free(find_list);
184 static void __hash_destroy_callback_info(gpointer data)
187 GList *callback_list = (GList *)data;
188 if (callback_list != NULL)
189 g_list_free_full(callback_list, __callback_info_free);
192 static char *__get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted)
195 int prefix_len = strlen(MESSAGEPORT_BUS_NAME_PREFIX);
197 char *postfix = is_trusted ? "1" : "0";
199 unsigned char c[MD5_DIGEST_LENGTH] = {0};
200 char *md5_interface = NULL;
204 int encoded_bus_name_len = prefix_len + postfix_len + (MD5_DIGEST_LENGTH * 2) + 2;
205 int bus_name_len = strlen(remote_app_id) + strlen(port_name) + 2;
206 char *bus_name = (char *)calloc(bus_name_len, sizeof(char));
207 if (bus_name == NULL) {
208 _LOGE("bus_name calloc failed");
212 snprintf(bus_name, bus_name_len, "%s_%s", remote_app_id, port_name);
214 MD5_Init(&mdContext);
215 MD5_Update(&mdContext, bus_name, bus_name_len);
216 MD5_Final(c, &mdContext);
218 md5_interface = (char *)calloc(encoded_bus_name_len , sizeof(char));
219 if (md5_interface == NULL) {
223 _LOGE("md5_interface calloc failed!!");
227 snprintf(md5_interface, encoded_bus_name_len, "%s", MESSAGEPORT_BUS_NAME_PREFIX);
228 temp = md5_interface;
231 for (index = 0; index < MD5_DIGEST_LENGTH; index++) {
232 snprintf(temp, 3, "%02x", c[index]);
236 if (postfix && postfix_len > 0)
237 snprintf(temp, encoded_bus_name_len - (temp - md5_interface), "%s", postfix);
241 _LOGI("encoded_bus_name : %s ", md5_interface);
243 return md5_interface;
246 static int __remote_port_compare_cb(gconstpointer a, gconstpointer b)
248 port_list_info_s *key1 = (port_list_info_s *)a;
249 port_list_info_s *key2 = (port_list_info_s *)b;
251 if (key1->is_trusted == key2->is_trusted)
252 return strcmp(key1->port_name, key2->port_name);
258 static bool __is_preloaded(const char *local_appid, const char *remote_appid)
260 _LOGI("IsPreloaded");
262 bool preload_local = false;
263 bool preload_remote = false;
265 pkgmgrinfo_appinfo_h handle = NULL;
266 int ret = pkgmgrinfo_appinfo_get_usr_appinfo(local_appid, getuid(), &handle);
267 if (ret != PMINFO_R_OK) {
268 _LOGE("Failed to get the appinfo. %d", ret);
269 pkgmgrinfo_appinfo_destroy_appinfo(handle);
272 ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_local);
273 if (ret != PMINFO_R_OK) {
274 _LOGE("Failed to check the preloaded application. %d", ret);
275 pkgmgrinfo_appinfo_destroy_appinfo(handle);
278 pkgmgrinfo_appinfo_destroy_appinfo(handle);
280 ret = pkgmgrinfo_appinfo_get_usr_appinfo(remote_appid, getuid(), &handle);
281 if (ret != PMINFO_R_OK) {
282 _LOGE("Failed to get the appinfo. %d", ret);
283 pkgmgrinfo_appinfo_destroy_appinfo(handle);
286 ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_remote);
287 if (ret != PMINFO_R_OK) {
288 _LOGE("Failed to check the preloaded application. %d", ret);
289 pkgmgrinfo_appinfo_destroy_appinfo(handle);
293 if (preload_local && preload_remote) {
294 pkgmgrinfo_appinfo_destroy_appinfo(handle);
297 pkgmgrinfo_appinfo_destroy_appinfo(handle);
301 static int __check_certificate(const char *local_appid, const char *remote_appid)
303 _LOGI("CheckCertificate");
305 pkgmgrinfo_cert_compare_result_type_e res;
306 int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(local_appid, remote_appid, getuid(), &res);
308 _LOGE(":CheckCertificate() Failed");
309 return MESSAGEPORT_ERROR_IO_ERROR;
311 if (res != PMINFO_CERT_COMPARE_MATCH) {
312 _LOGE("CheckCertificate() Failed : MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH");
313 return MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
316 return MESSAGEPORT_ERROR_NONE;
319 static void on_name_appeared(GDBusConnection *connection,
321 const gchar *name_owner,
324 _LOGI("name appeared : %s %s", __app_id, name);
327 static void on_name_vanished(GDBusConnection *connection,
331 _LOGI("name vanished : %s", name);
332 port_list_info_s *pli = (port_list_info_s *)user_data;
334 LOGE("NULL port info");
338 _LOGI("watcher_id :%d", pli->watcher_id);
339 if (pli->watcher_id > 0)
340 g_bus_unwatch_name(pli->watcher_id);
342 _LOGE("Invalid watcher_id %d", pli->watcher_id);
348 static int __get_local_port_info(int id, message_port_local_port_info_s **info)
350 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
353 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
356 return MESSAGEPORT_ERROR_NONE;
359 static port_list_info_s *__set_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
361 int ret_val = MESSAGEPORT_ERROR_NONE;
362 port_list_info_s *port_info = (port_list_info_s *)calloc(1, sizeof(port_list_info_s));
365 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
368 port_info->port_name = strdup(remote_port);
369 if (!port_info->port_name) {
370 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
373 port_info->is_trusted = is_trusted;
374 port_info->encoded_bus_name = __get_encoded_name(remote_app_id, remote_port, is_trusted);
375 if (port_info->encoded_bus_name == NULL) {
376 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
379 port_info->send_sock_fd = 0;
381 if (ret_val != MESSAGEPORT_ERROR_NONE) {
383 FREE_AND_NULL(port_info->port_name);
384 FREE_AND_NULL(port_info->encoded_bus_name);
392 static message_port_remote_app_info_s *__set_remote_app_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
394 message_port_remote_app_info_s *remote_app_info = NULL;
395 int ret_val = MESSAGEPORT_ERROR_NONE;
397 remote_app_info = (message_port_remote_app_info_s *)calloc(1, sizeof(message_port_remote_app_info_s));
398 if (!remote_app_info) {
399 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
403 remote_app_info->remote_app_id = strdup(remote_app_id);
404 if (remote_app_info->remote_app_id == NULL) {
405 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;;
410 if (ret_val != MESSAGEPORT_ERROR_NONE) {
411 if (remote_app_info) {
412 FREE_AND_NULL(remote_app_info->remote_app_id);
413 FREE_AND_NULL(remote_app_info);
417 return remote_app_info;
420 static void __clear_disconnect_socket(port_list_info_s *port_info)
422 GError *error = NULL;
424 if (port_info == NULL)
426 _LOGI("__clear_disconnect_socket : fd [%d]", port_info->send_sock_fd);
428 if (port_info->gio_read != NULL) {
429 g_io_channel_shutdown(port_info->gio_read, TRUE, &error);
431 _LOGE("g_io_channel_shutdown error : %s", error->message);
434 g_io_channel_unref(port_info->gio_read);
435 port_info->gio_read = NULL;
438 if (port_info->g_src_id != 0) {
439 g_source_remove(port_info->g_src_id);
440 port_info->g_src_id = 0;
442 port_info->send_sock_fd = 0;
445 static gboolean __socket_disconnect_handler(GIOChannel *gio,
449 /* It's sender socket's gio channel so, only EOF can be received */
450 port_list_info_s *port_info = (port_list_info_s *)data;
451 _LOGI("__socket_disconnect_handler %d", cond);
452 __clear_disconnect_socket(port_info);
456 static void __watch_remote_port_info(port_list_info_s *port_info)
458 if (port_info == NULL)
461 if (port_info->watcher_id < 1) {
462 port_info->watcher_id = g_bus_watch_name_on_connection(
464 port_info->encoded_bus_name,
465 G_BUS_NAME_WATCHER_FLAGS_NONE,
471 LOGI("Already watched port info");
476 static int __get_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted,
477 message_port_remote_app_info_s **mri, port_list_info_s **pli)
479 message_port_remote_app_info_s *remote_app_info = NULL;
480 port_list_info_s port_info;
481 GList *cb_list = NULL;
482 int ret_val = MESSAGEPORT_ERROR_NONE;
484 remote_app_info = (message_port_remote_app_info_s *)g_hash_table_lookup(__remote_app_info, remote_app_id);
486 if (remote_app_info == NULL) {
487 remote_app_info = __set_remote_app_info(remote_app_id, remote_port, is_trusted);
489 if (remote_app_info == NULL) {
490 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
493 g_hash_table_insert(__remote_app_info, remote_app_info->remote_app_id, remote_app_info);
495 *mri = remote_app_info;
497 port_info.port_name = strdup(remote_port);
498 port_info.is_trusted = is_trusted;
499 cb_list = g_list_find_custom(remote_app_info->port_list, &port_info,
500 (GCompareFunc)__remote_port_compare_cb);
501 if (port_info.port_name)
502 free(port_info.port_name);
503 if (cb_list == NULL) {
504 port_list_info_s *tmp = __set_remote_port_info(remote_app_id, remote_port, is_trusted);
506 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
509 remote_app_info->port_list = g_list_append(remote_app_info->port_list, tmp);
512 *pli = (port_list_info_s *)cb_list->data;
519 static bool __is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi)
524 g_hash_table_iter_init(&iter, __local_port_info);
525 while (g_hash_table_iter_next(&iter, &key, &value)) {
526 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)value;
528 if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
529 *local_id = mi->local_id;
538 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
540 GDBusMessage *msg = NULL;
541 GDBusMessage *reply = NULL;
546 msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
547 "org.freedesktop.DBus", "GetConnectionUnixProcessID");
549 _LOGE("Can't allocate new method call");
553 g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
554 reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
555 G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
559 _LOGE("Failed to get pid [%s]", err->message);
565 body = g_dbus_message_get_body(reply);
566 g_variant_get(body, "(u)", &pid);
572 g_object_unref(reply);
577 static int __write_socket(int fd,
580 unsigned int *bytes_write)
582 unsigned int left = nbytes;
587 while (left && (retry_cnt < MAX_RETRY_CNT)) {
588 nb = write(fd, buffer, left);
590 if (errno == EINTR) {
591 LOGE("__write_socket: EINTR error continue ...");
595 LOGE("__write_socket: ...error fd %d: errno %d\n", fd, errno);
596 return MESSAGEPORT_ERROR_IO_ERROR;
604 return MESSAGEPORT_ERROR_NONE;
607 static int __write_string_to_socket(int fd, const char *buffer, int string_len)
610 if (__write_socket(fd, (char *)&string_len, sizeof(string_len), &nb) != MESSAGEPORT_ERROR_NONE) {
611 _LOGE("write string_len fail");
612 return MESSAGEPORT_ERROR_IO_ERROR;
615 if (string_len > 0) {
616 if (__write_socket(fd, buffer, string_len, &nb) != MESSAGEPORT_ERROR_NONE) {
617 _LOGE("wirte buffer fail");
618 return MESSAGEPORT_ERROR_IO_ERROR;
621 return MESSAGEPORT_ERROR_NONE;
624 static int __read_socket(int fd,
627 unsigned int *bytes_read)
629 unsigned int left = nbytes;
632 const struct timespec TRY_SLEEP_TIME = { 0, 500 * 1000 * 1000 };
635 while (left && (retry_cnt < MAX_RETRY_CNT)) {
636 nb = read(fd, buffer, left);
638 LOGE("__read_socket: ...read EOF, socket closed %d: nb %d\n", fd, nb);
639 return MESSAGEPORT_ERROR_IO_ERROR;
640 } else if (nb == -1) {
641 /* wrt(nodejs) could change socket to none-blocking socket :-( */
642 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
643 LOGE("__read_socket: %d errno, sleep and retry ...", errno);
645 nanosleep(&TRY_SLEEP_TIME, 0);
648 LOGE("__read_socket: ...error fd %d: errno %d\n", fd, errno);
649 return MESSAGEPORT_ERROR_IO_ERROR;
657 return MESSAGEPORT_ERROR_NONE;
660 static int __read_string_from_socket(int fd, char **buffer, int *string_len)
663 if (__read_socket(fd, (char *)string_len, sizeof(*string_len), &nb) != MESSAGEPORT_ERROR_NONE) {
664 LOGE("read socket fail");
665 return MESSAGEPORT_ERROR_IO_ERROR;
667 if (*string_len > 0 && *string_len < MAX_MESSAGE_SIZE) {
668 *buffer = (char *)calloc(*string_len, sizeof(char));
669 if (*buffer == NULL) {
670 LOGE("Out of memory.");
671 return MESSAGEPORT_ERROR_IO_ERROR;
673 if (__read_socket(fd, *buffer, *string_len, &nb) != MESSAGEPORT_ERROR_NONE) {
674 LOGE("read socket fail");
675 return MESSAGEPORT_ERROR_IO_ERROR;
678 LOGE("Invalid string len %d", *string_len);
679 return MESSAGEPORT_ERROR_IO_ERROR;
681 return MESSAGEPORT_ERROR_NONE;
684 message_port_pkt_s *__message_port_recv_raw(int fd)
686 message_port_pkt_s *pkt = NULL;
689 pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
695 if (__read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGEPORT_ERROR_NONE) {
696 LOGE("read socket fail: port_name");
697 free(pkt->remote_port_name);
702 if (__read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGEPORT_ERROR_NONE) {
703 LOGE("read socket fail: is_bidirection");
704 free(pkt->remote_port_name);
709 if (__read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGEPORT_ERROR_NONE) {
710 LOGE("read socket fail: is_trusted");
711 free(pkt->remote_port_name);
716 if (__read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGEPORT_ERROR_NONE) {
717 LOGE("read socket fail: data");
720 free(pkt->remote_port_name);
728 static gboolean __socket_request_handler(GIOChannel *gio,
733 message_port_callback_info_s *mi;
734 message_port_pkt_s *pkt;
736 GError *error = NULL;
738 mi = (message_port_callback_info_s *)data;
741 g_io_channel_shutdown(gio, TRUE, &error);
743 _LOGE("g_io_channel_shutdown error : %s", error->message);
746 g_io_channel_unref(gio);
750 if (cond == G_IO_HUP) {
752 _LOGI("socket G_IO_HUP");
753 __callback_info_free_by_info(mi);
758 if ((fd = g_io_channel_unix_get_fd(gio)) < 0) {
759 _LOGE("fail to get fd from io channel");
760 __callback_info_free_by_info(mi);
764 if ((pkt = __message_port_recv_raw(fd)) == NULL) {
765 _LOGE("recv error on SOCKET");
766 __callback_info_free_by_info(mi);
770 kb = bundle_decode(pkt->data, pkt->data_len);
771 if (pkt->is_bidirection)
772 mi->callback(mi->local_id, mi->remote_app_id, pkt->remote_port_name, pkt->is_trusted, kb, NULL);
774 mi->callback(mi->local_id, mi->remote_app_id, NULL, pkt->is_trusted, kb, NULL);
778 if (pkt->remote_port_name)
779 free(pkt->remote_port_name);
789 static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation)
791 char *local_port = NULL;
792 char *local_appid = NULL;
793 char *remote_appid = NULL;
794 char *remote_port = NULL;
795 gboolean local_trusted = false;
796 gboolean remote_trusted = false;
797 gboolean bi_dir = false;
801 bundle_raw *raw = NULL;
802 message_port_local_port_info_s *mi;
803 int local_reg_id = 0;
804 message_port_callback_info_s *callback_info;
805 message_port_callback_info_s *head_callback_info;
806 GList *callback_info_list = NULL;
810 GUnixFDList *fd_list;
812 int *returned_fds = NULL;
815 g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
816 &remote_appid, &remote_port, &remote_trusted, &len, &raw);
819 _LOGE("Invalid argument : remote_port is NULL");
823 _LOGE("Invalid argument : remote_appid is NULL");
826 if (!__is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
827 _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
831 _LOGE("Invalid argument : local_appid");
835 _LOGE("Invalid argument : local_port");
838 if (strcmp(remote_appid, __app_id) != 0) {
839 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
842 if (strcmp(remote_port, mi->port_name) != 0) {
843 _LOGE("Invalid argument : remote_port (%s)", remote_port);
847 _LOGE("Invalid argument : data_len");
850 if (remote_trusted) {
851 if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
852 if (!__is_preloaded(local_appid, remote_appid)) {
853 int ret = __check_certificate(local_appid, remote_appid);
854 if (ret == MESSAGEPORT_ERROR_NONE)
855 g_hash_table_insert(__trusted_app_list_hash, local_appid, "TRUE");
857 _LOGE("The application (%s) is not signed with the same certificate",
865 callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
866 if (callback_info == NULL)
869 callback_info->local_id = mi->local_id;
870 callback_info->remote_app_id = strdup(local_appid);
871 callback_info->callback = mi->callback;
873 msg = g_dbus_method_invocation_get_message(invocation);
874 fd_list = g_dbus_message_get_unix_fd_list(msg);
876 /* When application send message to self fd_list is NULL */
877 if (fd_list != NULL) {
878 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
879 if (returned_fds == NULL) {
880 _LOGE("fail to get fds");
881 __callback_info_free(callback_info);
884 fd = returned_fds[0];
886 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
889 callback_info->gio_read = g_io_channel_unix_new(fd);
890 if (!callback_info->gio_read) {
891 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
892 __callback_info_free(callback_info);
896 callback_info->g_src_id = g_io_add_watch(callback_info->gio_read, G_IO_IN | G_IO_HUP,
897 __socket_request_handler, (gpointer)callback_info);
898 if (callback_info->g_src_id == 0) {
899 _LOGE("fail to add watch on socket");
900 __callback_info_free(callback_info);
904 callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(mi->local_id));
905 if (callback_info_list == NULL) {
906 head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
907 if (head_callback_info == NULL) {
908 _LOGE("fail to alloc head_callback_info");
909 __callback_info_free(callback_info);
912 head_callback_info->local_id = 0;
913 head_callback_info->remote_app_id = NULL;
914 head_callback_info->callback = NULL;
915 head_callback_info->gio_read = NULL;
916 head_callback_info->g_src_id = 0;
917 callback_info_list = g_list_append(callback_info_list, head_callback_info);
918 callback_info_list = g_list_append(callback_info_list, callback_info);
919 g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(mi->local_id), callback_info_list);
921 callback_info_list = g_list_append(callback_info_list, callback_info);
926 data = bundle_decode(raw, len);
928 _LOGE("Invalid argument : message");
932 LOGI("call calback %s", local_appid);
934 mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, NULL);
936 mi->callback(mi->local_id, local_appid, NULL, false, data, NULL);
945 static int __check_remote_port(const char *remote_app_id, const char *remote_port, bool is_trusted, bool *exist)
947 _LOGI("Check a remote port : [%s:%s]", remote_app_id, remote_port);
949 GVariant *result = NULL;
951 int ret_val = MESSAGEPORT_ERROR_NONE;
952 char *bus_name = NULL;
953 message_port_remote_app_info_s *remote_app_info = NULL;
954 port_list_info_s *port_info = NULL;
955 int local_reg_id = 0;
956 message_port_local_port_info_s *mi = NULL;
957 gboolean name_exist = false;
959 _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
961 ret_val = __get_remote_port_info(remote_app_id, remote_port, is_trusted, &remote_app_info, &port_info);
962 if (ret_val != MESSAGEPORT_ERROR_NONE)
966 if (strcmp(remote_app_id, __app_id) == 0) {
968 _LOGI("__is_local_port_registed ");
969 if (!__is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
974 _LOGI("__is_local_port_registed : %d ", *exist);
975 return MESSAGEPORT_ERROR_NONE;
978 port_info->exist = false;
979 bus_name = port_info->encoded_bus_name;
981 result = g_dbus_connection_call_sync(
987 g_variant_new("(s)", bus_name),
988 G_VARIANT_TYPE("(b)"),
989 G_DBUS_CALL_FLAGS_NONE,
994 if (err || (result == NULL)) {
996 _LOGE("No reply. error = %s", err->message);
999 ret_val = MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
1001 g_variant_get(result, "(b)", &name_exist);
1004 _LOGI("Name not exist %s", bus_name);
1006 ret_val = MESSAGEPORT_ERROR_NONE;
1010 if (remote_app_info->certificate_info != CERTIFICATE_MATCH) {
1011 if (!__is_preloaded(__app_id, remote_app_id)) {
1012 if (__check_certificate(__app_id, remote_app_id) != MESSAGEPORT_ERROR_NONE) {
1013 ret_val = MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
1017 remote_app_info->certificate_info = CERTIFICATE_MATCH;
1020 port_info->exist = true;
1022 ret_val = MESSAGEPORT_ERROR_NONE;
1023 __watch_remote_port_info(port_info);
1028 g_variant_unref(result);
1033 static void __on_sender_name_appeared(GDBusConnection *connection,
1035 const gchar *name_owner,
1038 _LOGI("sender name appeared : %s", name);
1041 static void __on_sender_name_vanished(GDBusConnection *connection,
1045 gboolean remove_result = FALSE;
1046 int *watcher_id = (int *)user_data;
1047 remove_result = g_hash_table_remove(__sender_appid_hash, (gpointer)name);
1049 _LOGE("Fail to remove sender appid from hash : %s", name);
1052 if (*watcher_id > 0)
1053 g_bus_unwatch_name(*watcher_id);
1055 LOGE("Invalid watcher_id %d", *watcher_id);
1058 LOGE("watcher_id is NULL");
1062 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
1065 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
1066 char *local_appid = NULL;
1067 int pid = __get_sender_pid(conn, sender);
1068 int *watcher_id = (int *)calloc(1, sizeof(int));
1069 retvm_if(!watcher_id, false, "Malloc failed");
1071 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
1072 if (ret != AUL_R_OK) {
1073 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
1078 g_variant_get_child(parameters, 0, "&s", &local_appid);
1079 if (local_appid == NULL) {
1080 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
1085 if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
1086 _LOGI("insert sender !!!!! %s", sender);
1087 g_hash_table_insert(__sender_appid_hash, (gpointer)strdup(sender), GINT_TO_POINTER(pid));
1088 *watcher_id = g_bus_watch_name_on_connection(
1091 G_BUS_NAME_WATCHER_FLAGS_NONE,
1092 __on_sender_name_appeared,
1093 __on_sender_name_vanished,
1103 static void __dbus_method_call_handler(GDBusConnection *conn,
1104 const gchar *sender, const gchar *object_path,
1105 const gchar *iface_name, const gchar *method_name,
1106 GVariant *parameters, GDBusMethodInvocation *invocation,
1109 _LOGI("method_name: %s, sender: %s", method_name, sender);
1110 gpointer sender_pid = g_hash_table_lookup(__sender_appid_hash, sender);
1111 if (sender_pid == NULL) {
1112 if (!__check_sender_validation(parameters, sender, conn))
1115 if (g_strcmp0(method_name, "send_message") == 0)
1116 send_message(parameters, invocation);
1118 g_dbus_method_invocation_return_value(invocation, NULL);
1121 static const GDBusInterfaceVTable interface_vtable = {
1122 __dbus_method_call_handler,
1127 static int __dbus_init(void)
1130 GError *error = NULL;
1132 __gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
1133 if (__gdbus_conn == NULL) {
1134 if (error != NULL) {
1135 _LOGE("Failed to get dbus [%s]", error->message);
1136 g_error_free(error);
1145 g_object_unref(__gdbus_conn);
1151 int __register_dbus_interface(const char *port_name, bool is_trusted)
1154 GDBusNodeInfo *introspection_data = NULL;
1155 int registration_id = 0;
1157 static gchar introspection_prefix[] =
1159 " <interface name='";
1161 static gchar introspection_postfix[] =
1163 " <method name='send_message'>"
1164 " <arg type='s' name='local_appid' direction='in'/>"
1165 " <arg type='s' name='local_port' direction='in'/>"
1166 " <arg type='b' name='local_trusted' direction='in'/>"
1167 " <arg type='b' name='bi_dir' direction='in'/>"
1168 " <arg type='s' name='remote_appid' direction='in'/>"
1169 " <arg type='s' name='remote_port' direction='in'/>"
1170 " <arg type='b' name='remote_trusted' direction='in'/>"
1171 " <arg type='u' name='data_len' direction='in'/>"
1172 " <arg type='s' name='data' direction='in'/>"
1177 char *introspection_xml = NULL;
1178 int introspection_xml_len = 0;
1182 GError *error = NULL;
1183 char *bus_name = NULL;
1184 char *interface_name = NULL;
1185 GVariant *result = NULL;
1187 bus_name = __get_encoded_name(__app_id, port_name, is_trusted);
1189 _LOGE("Fail to get bus name");
1192 interface_name = bus_name;
1194 introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
1195 strlen(introspection_postfix) + 1;
1197 introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
1198 if (!introspection_xml) {
1199 _LOGE("out of memory");
1204 result = g_dbus_connection_call_sync(
1208 DBUS_INTERFACE_DBUS,
1210 g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
1211 G_VARIANT_TYPE("(u)"),
1212 G_DBUS_CALL_FLAGS_NONE,
1217 _LOGE("RequestName fail : %s", error->message);
1218 g_error_free(error);
1221 if (result == NULL) {
1222 _LOGE("fail to get name NULL");
1225 g_variant_get(result, "(u)", &owner_id);
1226 if (owner_id == 0) {
1227 _LOGE("Acquiring the own name is failed");
1231 _LOGI("Acquiring the own name : %d", owner_id);
1233 snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
1235 introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
1236 if (!introspection_data) {
1237 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
1241 registration_id = g_dbus_connection_register_object(__gdbus_conn,
1242 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
1243 &interface_vtable, NULL, NULL, NULL);
1245 _LOGI("registration_id %d", registration_id);
1247 if (registration_id == 0) {
1248 _LOGE("Failed to g_dbus_connection_register_object");
1253 if (introspection_data)
1254 g_dbus_node_info_unref(introspection_data);
1255 if (introspection_xml)
1256 free(introspection_xml);
1260 g_variant_unref(result);
1263 return registration_id;
1266 /* LCOV_EXCL_START */
1267 void __list_free_port_list(gpointer data)
1269 port_list_info_s *n = (port_list_info_s *)data;
1271 FREE_AND_NULL(n->encoded_bus_name);
1272 FREE_AND_NULL(n->port_name);
1275 /* LCOV_EXCL_STOP */
1277 /* LCOV_EXCL_START */
1278 static void __hash_destory_local_value(gpointer data)
1280 message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
1283 free(mli->port_name);
1287 /* LCOV_EXCL_STOP */
1289 /* LCOV_EXCL_START */
1290 static void __hash_destory_remote_value(gpointer data)
1292 message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
1294 FREE_AND_NULL(mri->sender_id);
1295 FREE_AND_NULL(mri->remote_app_id);
1297 g_list_free_full(mri->port_list, __list_free_port_list);
1302 /* LCOV_EXCL_STOP */
1304 static bool __initialize(void)
1307 #if !GLIB_CHECK_VERSION(2, 35, 0)
1313 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
1315 _LOGI("initialize");
1316 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
1317 retvm_if(ret != AUL_R_OK, false, "Failed to get the application ID: %d", ret);
1319 __app_id = strdup(buffer);
1320 retvm_if(!__app_id, false, "Malloc failed");
1321 _LOGI("init : %s", __app_id);
1323 if (__local_port_info == NULL) {
1324 __local_port_info = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destory_local_value);
1325 retvm_if(!__local_port_info, false, "fail to create __local_port_info");
1328 if (__remote_app_info == NULL) {
1329 __remote_app_info = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __hash_destory_remote_value);
1330 retvm_if(!__remote_app_info, false, "fail to create __remote_app_info");
1333 if (__sender_appid_hash == NULL) {
1334 __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
1335 retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
1338 if (__trusted_app_list_hash == NULL) {
1339 __trusted_app_list_hash = g_hash_table_new(g_str_hash, g_str_equal);
1340 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
1343 if (__callback_info_hash == NULL) {
1344 __callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destroy_callback_info);
1345 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
1350 _initialized = true;
1356 static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, messageport_message_cb callback)
1358 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
1359 retvm_if(!mi, false, "Malloc failed");
1361 mi->callback = callback;
1362 mi->is_trusted = is_trusted;
1363 mi->port_name = strdup(local_port);
1364 if (mi->port_name == NULL) {
1365 _LOGE("Malloc failed (%s)", local_port);
1369 mi->local_id = local_id;
1371 g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
1375 static int __register_message_port(const char *local_port, bool is_trusted, messageport_message_cb callback)
1377 _SECURE_LOGI("Register a message port : [%s:%s]", __app_id, local_port);
1381 /* Check the message port is already registed */
1382 if (__is_local_port_registed(local_port, is_trusted, &local_id, NULL))
1385 local_id = __register_dbus_interface(local_port, is_trusted);
1387 _LOGE("register_dbus_interface fail !!");
1388 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1391 if (!__message_port_register_port(local_id, local_port, is_trusted, callback))
1392 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1397 int __message_port_send_async(int sockfd, bundle *kb, const char *local_port,
1398 bool local_trusted, bool is_bidirection)
1402 int local_port_len = 0;
1404 bundle_raw *kb_data = NULL;
1406 bundle_encode(kb, &kb_data, &data_len);
1407 if (kb_data == NULL) {
1408 _LOGE("bundle encode fail");
1409 ret = MESSAGEPORT_ERROR_IO_ERROR;
1413 if (data_len > MAX_MESSAGE_SIZE) {
1414 _LOGE("bigger than max size\n");
1415 ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1419 if (local_port != NULL)
1420 local_port_len = strlen(local_port) + 1;
1422 if (__write_string_to_socket(sockfd, local_port, local_port_len) != MESSAGEPORT_ERROR_NONE) {
1423 _LOGE("write local_port fail");
1424 ret = MESSAGEPORT_ERROR_IO_ERROR;
1428 if (__write_socket(sockfd, (char *)&is_bidirection, sizeof(is_bidirection), &nb) != MESSAGEPORT_ERROR_NONE) {
1429 _LOGE("write is_bidirection fail");
1430 ret = MESSAGEPORT_ERROR_IO_ERROR;
1434 if (__write_socket(sockfd, (char *)&local_trusted, sizeof(local_trusted), &nb) != MESSAGEPORT_ERROR_NONE) {
1435 _LOGE("write local_trusted fail");
1436 ret = MESSAGEPORT_ERROR_IO_ERROR;
1440 if (__write_string_to_socket(sockfd, (void *)kb_data, data_len) != MESSAGEPORT_ERROR_NONE) {
1441 _LOGE("write kb_data fail");
1442 ret = MESSAGEPORT_ERROR_IO_ERROR;
1452 static int __message_port_send_message(const char *remote_appid, const char *remote_port,
1453 const char *local_port, bool trusted_message, bool local_trusted, bool bi_dir, bundle *message)
1456 int ret = MESSAGEPORT_ERROR_NONE;
1457 GUnixFDList *fd_list = NULL;
1460 bundle_raw *raw = NULL;
1461 char *bus_name = NULL;
1462 char *interface_name = NULL;
1464 message_port_remote_app_info_s *remote_app_info = NULL;
1465 port_list_info_s *port_info = NULL;
1466 GDBusMessage *msg = NULL;
1468 GVariant *body = NULL;
1469 int sock_pair[2] = {0,};
1472 ret = __get_remote_port_info(remote_appid, remote_port, trusted_message, &remote_app_info, &port_info);
1473 if (ret != MESSAGEPORT_ERROR_NONE)
1476 if (port_info->exist == false) {
1478 _LOGI("port exist check !!");
1479 ret = __check_remote_port(remote_appid, remote_port, trusted_message, &exist);
1480 if (ret != MESSAGEPORT_ERROR_NONE) {
1482 } else if (!exist) {
1483 ret = MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1488 if (port_info->send_sock_fd > 0) {
1489 ret = __message_port_send_async(port_info->send_sock_fd, message,
1490 (local_port) ? local_port : "", local_trusted, bi_dir);
1493 bus_name = port_info->encoded_bus_name;
1494 interface_name = bus_name;
1496 if (bundle_encode(message, &raw, &len) != BUNDLE_ERROR_NONE) {
1497 ret = MESSAGEPORT_ERROR_INVALID_PARAMETER;
1501 if (MAX_MESSAGE_SIZE < len) {
1502 _LOGE("The size of message (%d) has exceeded the maximum limit.", len);
1503 ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1507 body = g_variant_new("(ssbbssbus)", __app_id, (local_port) ? local_port : "", local_trusted, bi_dir,
1508 remote_appid, remote_port, trusted_message, len, raw);
1509 if (strcmp(remote_appid, __app_id) != 0) { /* self send */
1511 /* if message-port fail to get socket pair, communicate using GDBus */
1512 if (aul_request_message_port_socket_pair(sock_pair) != AUL_R_OK) {
1513 _LOGE("error create socket pair");
1516 _LOGI("sock pair : %d, %d",
1517 sock_pair[SOCK_PAIR_SENDER], sock_pair[SOCK_PAIR_RECEIVER]);
1518 fd_list = g_unix_fd_list_new();
1519 g_unix_fd_list_append(fd_list, sock_pair[SOCK_PAIR_RECEIVER], &err);
1521 _LOGE("g_unix_fd_list_append [%s]", err->message);
1522 ret = MESSAGEPORT_ERROR_IO_ERROR;
1527 port_info->send_sock_fd = sock_pair[SOCK_PAIR_SENDER];
1528 close(sock_pair[SOCK_PAIR_RECEIVER]);
1529 sock_pair[SOCK_PAIR_RECEIVER] = 0;
1531 port_info->gio_read = g_io_channel_unix_new(port_info->send_sock_fd);
1532 if (!port_info->gio_read) {
1533 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
1534 ret = MESSAGEPORT_ERROR_IO_ERROR;
1538 port_info->g_src_id = g_io_add_watch(port_info->gio_read, G_IO_IN | G_IO_HUP,
1539 __socket_disconnect_handler, (gpointer)port_info);
1540 if (port_info->g_src_id == 0) {
1541 _LOGE("fail to add watch on socket");
1542 ret = MESSAGEPORT_ERROR_IO_ERROR;
1549 msg = g_dbus_message_new_method_call(bus_name, MESSAGEPORT_OBJECT_PATH, interface_name, "send_message");
1551 _LOGE("Can't allocate new method call");
1552 ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1556 g_dbus_message_set_unix_fd_list(msg, fd_list);
1557 g_dbus_message_set_body(msg, body);
1558 g_dbus_connection_send_message(__gdbus_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
1560 _LOGE("No reply. error = %s", err->message);
1562 ret = MESSAGEPORT_ERROR_IO_ERROR;
1565 __watch_remote_port_info(port_info);
1571 g_object_unref(msg);
1573 bundle_free_encoded_rawdata(&raw);
1575 g_object_unref(fd_list);
1577 if (ret != MESSAGEPORT_ERROR_NONE) {
1578 __clear_disconnect_socket(port_info);
1579 if (sock_pair[SOCK_PAIR_SENDER])
1580 close(sock_pair[SOCK_PAIR_SENDER]);
1581 if (sock_pair[SOCK_PAIR_RECEIVER])
1582 close(sock_pair[SOCK_PAIR_RECEIVER]);
1588 int __message_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port, bool trusted_message, bundle *message)
1590 message_port_local_port_info_s *local_info;
1591 int ret = __get_local_port_info(id, &local_info);
1592 if (ret != MESSAGEPORT_ERROR_NONE)
1595 _LOGI("bidirectional_message %s", local_info->port_name);
1596 return __message_port_send_message(remote_app_id, remote_port,
1597 local_info->port_name, trusted_message, local_info->is_trusted, true, message);
1600 int messageport_unregister_local_port(int local_port_id, bool trusted_port)
1604 char *bus_name = NULL;
1608 _LOGI("unregister : %d", local_port_id);
1610 message_port_local_port_info_s *mi =
1611 (message_port_local_port_info_s *)
1612 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
1614 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1616 if (mi->is_trusted != trusted_port)
1617 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
1619 g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
1621 bus_name = __get_encoded_name(__app_id, mi->port_name, mi->is_trusted);
1622 if (bus_name == NULL)
1623 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1625 g_dbus_connection_unregister_object(__gdbus_conn, local_port_id);
1627 result = g_dbus_connection_call_sync(
1631 DBUS_INTERFACE_DBUS,
1633 g_variant_new("(s)", bus_name),
1634 G_VARIANT_TYPE("(u)"),
1635 G_DBUS_CALL_FLAGS_NONE,
1644 _LOGE("RequestName fail : %s", err->message);
1646 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1648 g_variant_get(result, "(u)", &ret);
1651 g_variant_unref(result);
1653 if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
1655 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
1656 _LOGE("Port Not exist");
1657 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1658 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
1659 _LOGE("Try to release not owned name. MESSAGEPORT_ERROR_INVALID_PARAMETER");
1660 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
1665 g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
1667 return MESSAGEPORT_ERROR_NONE;
1670 int messageport_register_local_port(const char *local_port, messageport_message_cb callback)
1672 if (!_initialized) {
1673 if (!__initialize())
1674 return MESSAGEPORT_ERROR_IO_ERROR;
1677 return __register_message_port(local_port, false, callback);
1680 int messageport_register_trusted_local_port(const char *local_port, messageport_message_cb callback)
1682 if (!_initialized) {
1683 if (!__initialize())
1684 return MESSAGEPORT_ERROR_IO_ERROR;
1687 return __register_message_port(local_port, true, callback);
1691 int messageport_check_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
1693 if (!_initialized) {
1694 if (!__initialize())
1695 return MESSAGEPORT_ERROR_IO_ERROR;
1698 int ret = __check_remote_port(remote_app_id, remote_port, false, exist);
1699 if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
1701 ret = MESSAGEPORT_ERROR_NONE;
1707 int messageport_check_trusted_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
1709 if (!_initialized) {
1710 if (!__initialize())
1711 return MESSAGEPORT_ERROR_IO_ERROR;
1714 int ret = __check_remote_port(remote_app_id, remote_port, true, exist);
1715 if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
1717 ret = MESSAGEPORT_ERROR_NONE;
1723 int messageport_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
1725 if (!_initialized) {
1726 if (!__initialize())
1727 return MESSAGEPORT_ERROR_IO_ERROR;
1730 return __message_port_send_message(remote_app_id, remote_port, NULL, false, false, false, message);
1733 int messageport_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
1735 if (!_initialized) {
1736 if (!__initialize())
1737 return MESSAGEPORT_ERROR_IO_ERROR;
1740 return __message_port_send_message(remote_app_id, remote_port, NULL, true, false, false, message);
1743 int messageport_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port,
1746 if (!_initialized) {
1747 if (!__initialize())
1748 return MESSAGEPORT_ERROR_IO_ERROR;
1751 return __message_send_bidirectional_message(id, remote_app_id, remote_port, false, message);
1754 int messageport_send_bidirectional_trusted_message(int id, const char *remote_app_id, const char *remote_port,
1757 if (!_initialized) {
1758 if (!__initialize())
1759 return MESSAGEPORT_ERROR_IO_ERROR;
1761 return __message_send_bidirectional_message(id, remote_app_id, remote_port, true, message);