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.
23 #include <sys/socket.h>
29 #include <openssl/md5.h>
31 #include <bundle_internal.h>
32 #include <pkgmgr-info.h>
35 #include <gio/gunixfdlist.h>
37 #include "message-port.h"
38 #include "message-port-log.h"
40 #define MAX_PACKAGE_STR_SIZE 512
41 #define MESSAGEPORT_BUS_NAME_PREFIX "org.tizen.messageport._"
42 #define MESSAGEPORT_OBJECT_PATH "/org/tizen/messageport"
43 #define MESSAGEPORT_INTERFACE_PREFIX "org.tizen.messageport._"
45 #define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
46 #define DBUS_PATH_DBUS "/org/freedesktop/DBus"
47 #define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
49 #define DBUS_RELEASE_NAME_REPLY_RELEASED 1 /* *< Service was released from the given name */
50 #define DBUS_RELEASE_NAME_REPLY_NON_EXISTENT 2 /* *< The given name does not exist on the bus */
51 #define DBUS_RELEASE_NAME_REPLY_NOT_OWNER 3 /* *< Service is not an owner of the given name */
53 #define MAX_RETRY_CNT 2
54 #define SOCK_PAIR_SENDER 0
55 #define SOCK_PAIR_RECEIVER 1
58 #define retvm_if(expr, val, fmt, arg...) do { \
61 _LOGE("(%s) -> %s() return", #expr, __func__); \
66 #define retv_if(expr, val) do { \
68 _LOGE("(%s) -> %s() return", #expr, __func__); \
73 #define FREE_AND_NULL(ptr) do { \
80 static bool _initialized = false;
81 static GDBusConnection *__gdbus_conn = NULL;
82 static char *__app_id;
83 static GHashTable *__local_port_info = NULL;
84 static GHashTable *__remote_app_info = NULL;
85 static GHashTable *__sender_appid_hash = NULL;
86 static GHashTable *__trusted_app_list_hash = NULL;
87 static const int MAX_MESSAGE_SIZE = 16 * 1024;
89 enum __certificate_info_type {
92 CERTIFICATE_NOT_MATCH,
95 typedef struct message_port_pkt {
96 int remote_port_name_len;
97 char *remote_port_name;
102 } message_port_pkt_s;
104 typedef struct message_port_callback_info {
105 messageport_message_cb callback;
108 GIOChannel *gio_read;
110 } message_port_callback_info_s;
112 typedef struct message_port_local_port_info {
113 messageport_message_cb callback;
117 } message_port_local_port_info_s;
119 typedef struct message_port_remote_port_info {
122 int certificate_info;
124 } message_port_remote_app_info_s;
126 typedef struct port_list_info {
128 char *encoded_bus_name;
135 static void __callback_info_free(message_port_callback_info_s *callback_info)
137 GError *error = NULL;
138 if (callback_info == NULL)
141 if (callback_info->remote_app_id)
142 free(callback_info->remote_app_id);
144 if (callback_info->gio_read != NULL) {
145 g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
147 _LOGE("g_io_channel_shutdown error : %s", error->message);
150 g_io_channel_unref(callback_info->gio_read);
151 callback_info->gio_read = NULL;
154 if (callback_info->g_src_id != 0) {
155 g_source_remove(callback_info->g_src_id);
156 callback_info->g_src_id = 0;
162 static char *__get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted)
165 int prefix_len = strlen(MESSAGEPORT_BUS_NAME_PREFIX);
167 char *postfix = is_trusted ? "1" : "0";
169 unsigned char c[MD5_DIGEST_LENGTH] = {0};
170 char *md5_interface = NULL;
174 int encoded_bus_name_len = prefix_len + postfix_len + (MD5_DIGEST_LENGTH * 2) + 2;
175 int bus_name_len = strlen(remote_app_id) + strlen(port_name) + 2;
176 char *bus_name = (char *)calloc(bus_name_len, sizeof(char));
177 if (bus_name == NULL) {
178 _LOGE("bus_name calloc failed");
182 snprintf(bus_name, bus_name_len, "%s_%s", remote_app_id, port_name);
184 MD5_Init(&mdContext);
185 MD5_Update(&mdContext, bus_name, bus_name_len);
186 MD5_Final(c, &mdContext);
188 md5_interface = (char *)calloc(encoded_bus_name_len , sizeof(char));
189 if (md5_interface == NULL) {
193 _LOGE("md5_interface calloc failed!!");
197 snprintf(md5_interface, encoded_bus_name_len, "%s", MESSAGEPORT_BUS_NAME_PREFIX);
198 temp = md5_interface;
201 for (index = 0; index < MD5_DIGEST_LENGTH; index++) {
202 snprintf(temp, 3, "%02x", c[index]);
206 if (postfix && postfix_len > 0)
207 snprintf(temp, encoded_bus_name_len - (temp - md5_interface), "%s", postfix);
211 _LOGI("encoded_bus_name : %s ", md5_interface);
213 return md5_interface;
216 static int __remote_port_compare_cb(gconstpointer a, gconstpointer b)
218 port_list_info_s *key1 = (port_list_info_s *)a;
219 port_list_info_s *key2 = (port_list_info_s *)b;
221 if (key1->is_trusted == key2->is_trusted)
222 return strcmp(key1->port_name, key2->port_name);
228 static bool __is_preloaded(const char *local_appid, const char *remote_appid)
230 _LOGI("IsPreloaded");
232 bool preload_local = false;
233 bool preload_remote = false;
235 pkgmgrinfo_appinfo_h handle = NULL;
236 int ret = pkgmgrinfo_appinfo_get_usr_appinfo(local_appid, getuid(), &handle);
237 if (ret != PMINFO_R_OK) {
238 _LOGE("Failed to get the appinfo. %d", ret);
239 pkgmgrinfo_appinfo_destroy_appinfo(handle);
242 ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_local);
243 if (ret != PMINFO_R_OK) {
244 _LOGE("Failed to check the preloaded application. %d", ret);
245 pkgmgrinfo_appinfo_destroy_appinfo(handle);
248 ret = pkgmgrinfo_appinfo_get_usr_appinfo(remote_appid, getuid(), &handle);
249 if (ret != PMINFO_R_OK) {
250 _LOGE("Failed to get the appinfo. %d", ret);
251 pkgmgrinfo_appinfo_destroy_appinfo(handle);
254 ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_remote);
255 if (ret != PMINFO_R_OK) {
256 _LOGE("Failed to check the preloaded application. %d", ret);
257 pkgmgrinfo_appinfo_destroy_appinfo(handle);
261 if (preload_local && preload_remote) {
262 pkgmgrinfo_appinfo_destroy_appinfo(handle);
265 pkgmgrinfo_appinfo_destroy_appinfo(handle);
269 static int __check_certificate(const char *local_appid, const char *remote_appid)
271 _LOGI("CheckCertificate");
273 pkgmgrinfo_cert_compare_result_type_e res;
274 int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(local_appid, remote_appid, getuid(), &res);
276 _LOGE(":CheckCertificate() Failed");
277 return MESSAGEPORT_ERROR_IO_ERROR;
279 if (res != PMINFO_CERT_COMPARE_MATCH) {
280 _LOGE("CheckCertificate() Failed : MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH");
281 return MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
284 return MESSAGEPORT_ERROR_NONE;
287 static void on_name_appeared(GDBusConnection *connection,
289 const gchar *name_owner,
292 _LOGI("name appeared : %s %s", __app_id, name);
295 static void on_name_vanished(GDBusConnection *connection,
299 _LOGI("name vanished : %s", name);
300 port_list_info_s *pli = (port_list_info_s *)user_data;
301 g_bus_unwatch_name(pli->watcher_id);
304 _LOGI("name vanished socket : %d", pli->send_sock_fd);
305 if (pli->send_sock_fd > 0) {
306 close(pli->send_sock_fd);
307 pli->send_sock_fd = 0;
311 static int __get_local_port_info(int id, message_port_local_port_info_s **info)
313 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
316 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
319 return MESSAGEPORT_ERROR_NONE;
322 static port_list_info_s *__set_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
324 int ret_val = MESSAGEPORT_ERROR_NONE;
325 port_list_info_s *port_info = (port_list_info_s *)calloc(1, sizeof(port_list_info_s));
328 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
331 port_info->port_name = strdup(remote_port);
332 if (!port_info->port_name) {
333 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
336 port_info->is_trusted = is_trusted;
337 port_info->encoded_bus_name = __get_encoded_name(remote_app_id, remote_port, is_trusted);
338 if (port_info->encoded_bus_name == NULL) {
339 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
342 port_info->send_sock_fd = 0;
344 if (ret_val != MESSAGEPORT_ERROR_NONE) {
346 FREE_AND_NULL(port_info->port_name);
347 FREE_AND_NULL(port_info->encoded_bus_name);
355 static message_port_remote_app_info_s *__set_remote_app_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
357 message_port_remote_app_info_s *remote_app_info = NULL;
358 int ret_val = MESSAGEPORT_ERROR_NONE;
360 remote_app_info = (message_port_remote_app_info_s *)calloc(1, sizeof(message_port_remote_app_info_s));
361 if (!remote_app_info) {
362 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
366 remote_app_info->remote_app_id = strdup(remote_app_id);
367 if (remote_app_info->remote_app_id == NULL) {
368 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;;
373 if (ret_val != MESSAGEPORT_ERROR_NONE) {
374 if (remote_app_info) {
375 FREE_AND_NULL(remote_app_info->remote_app_id);
376 FREE_AND_NULL(remote_app_info);
380 return remote_app_info;
383 static int __get_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted,
384 message_port_remote_app_info_s **mri, port_list_info_s **pli)
386 message_port_remote_app_info_s *remote_app_info = NULL;
387 port_list_info_s port_info;
388 GList *cb_list = NULL;
389 int ret_val = MESSAGEPORT_ERROR_NONE;
391 remote_app_info = (message_port_remote_app_info_s *)g_hash_table_lookup(__remote_app_info, remote_app_id);
393 if (remote_app_info == NULL) {
394 remote_app_info = __set_remote_app_info(remote_app_id, remote_port, is_trusted);
396 if (remote_app_info == NULL) {
397 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
400 g_hash_table_insert(__remote_app_info, remote_app_info->remote_app_id, remote_app_info);
402 *mri = remote_app_info;
404 port_info.port_name = strdup(remote_port);
405 port_info.is_trusted = is_trusted;
406 cb_list = g_list_find_custom(remote_app_info->port_list, &port_info,
407 (GCompareFunc)__remote_port_compare_cb);
408 if (port_info.port_name)
409 free(port_info.port_name);
410 if (cb_list == NULL) {
411 port_list_info_s *tmp = __set_remote_port_info(remote_app_id, remote_port, is_trusted);
413 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
416 remote_app_info->port_list = g_list_append(remote_app_info->port_list, tmp);
419 *pli = (port_list_info_s *)cb_list->data;
421 if ((*pli)->watcher_id < 1) {
422 LOGI("watch remote port : %s", (*pli)->encoded_bus_name);
423 (*pli)->watcher_id = g_bus_watch_name_on_connection(
425 (*pli)->encoded_bus_name,
426 G_BUS_NAME_WATCHER_FLAGS_NONE,
437 static bool __is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi)
442 g_hash_table_iter_init(&iter, __local_port_info);
443 while (g_hash_table_iter_next(&iter, &key, &value)) {
444 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)value;
446 if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
447 *local_id = mi->local_id;
456 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
458 GDBusMessage *msg = NULL;
459 GDBusMessage *reply = NULL;
464 msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
465 "org.freedesktop.DBus", "GetConnectionUnixProcessID");
467 _LOGE("Can't allocate new method call");
471 g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
472 reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
473 G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
477 _LOGE("Failed to get pid [%s]", err->message);
483 body = g_dbus_message_get_body(reply);
484 g_variant_get(body, "(u)", &pid);
490 g_object_unref(reply);
495 static int __write_socket(int fd,
498 unsigned int *bytes_write)
500 unsigned int left = nbytes;
505 while (left && (retry_cnt < MAX_RETRY_CNT)) {
506 nb = write(fd, buffer, left);
508 if (errno == EINTR) {
509 LOGE("__write_socket: EINTR error continue ...");
513 LOGE("__write_socket: ...error fd %d: errno %d\n", fd, errno);
514 return MESSAGEPORT_ERROR_IO_ERROR;
522 return MESSAGEPORT_ERROR_NONE;
525 static int __write_string_to_socket(int fd, const char *buffer, int string_len)
528 if (__write_socket(fd, (char *)&string_len, sizeof(string_len), &nb) != MESSAGEPORT_ERROR_NONE) {
529 _LOGE("write string_len fail");
530 return MESSAGEPORT_ERROR_IO_ERROR;
533 if (string_len > 0) {
534 if (__write_socket(fd, buffer, string_len, &nb) != MESSAGEPORT_ERROR_NONE) {
535 _LOGE("wirte buffer fail");
536 return MESSAGEPORT_ERROR_IO_ERROR;
539 return MESSAGEPORT_ERROR_NONE;
542 static int __read_socket(int fd,
545 unsigned int *bytes_read)
547 unsigned int left = nbytes;
552 while (left && (retry_cnt < MAX_RETRY_CNT)) {
553 nb = read(fd, buffer, left);
555 LOGE("__read_socket: ...read EOF, socket closed %d: nb %d\n", fd, nb);
556 return MESSAGEPORT_ERROR_IO_ERROR;
557 } else if (nb == -1) {
558 if (errno == EINTR) {
559 LOGE("__read_socket: EINTR error continue ...");
563 LOGE("__read_socket: ...error fd %d: errno %d\n", fd, errno);
564 return MESSAGEPORT_ERROR_IO_ERROR;
572 return MESSAGEPORT_ERROR_NONE;
575 static int __read_string_from_socket(int fd, char **buffer, int *string_len)
578 if (__read_socket(fd, (char *)string_len, sizeof(*string_len), &nb) != MESSAGEPORT_ERROR_NONE) {
579 LOGE("read socket fail");
580 return MESSAGEPORT_ERROR_IO_ERROR;
582 if (*string_len > 0 && *string_len < MAX_MESSAGE_SIZE) {
583 *buffer = (char *)calloc(*string_len, sizeof(char));
584 if (*buffer == NULL) {
585 LOGE("Out of memory.");
586 return MESSAGEPORT_ERROR_IO_ERROR;
588 if (__read_socket(fd, *buffer, *string_len, &nb) != MESSAGEPORT_ERROR_NONE) {
589 LOGE("read socket fail");
590 return MESSAGEPORT_ERROR_IO_ERROR;
593 LOGE("Invalid string len %d", &string_len);
594 return MESSAGEPORT_ERROR_IO_ERROR;
596 return MESSAGEPORT_ERROR_NONE;
599 message_port_pkt_s *__message_port_recv_raw(int fd)
601 message_port_pkt_s *pkt = NULL;
604 pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
610 if (__read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGEPORT_ERROR_NONE) {
611 LOGE("read socket fail: port_name");
612 free(pkt->remote_port_name);
617 if (__read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGEPORT_ERROR_NONE) {
618 LOGE("read socket fail: is_bidirection");
619 free(pkt->remote_port_name);
624 if (__read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGEPORT_ERROR_NONE) {
625 LOGE("read socket fail: is_trusted");
626 free(pkt->remote_port_name);
631 if (__read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGEPORT_ERROR_NONE) {
632 LOGE("read socket fail: data");
633 free(pkt->remote_port_name);
641 static gboolean __socket_request_handler(GIOChannel *gio,
646 message_port_callback_info_s *mi;
647 message_port_pkt_s *pkt;
649 GError *error = NULL;
651 mi = (message_port_callback_info_s *)data;
654 g_io_channel_shutdown(gio, TRUE, &error);
656 _LOGE("g_io_channel_shutdown error : %s", error->message);
659 g_io_channel_unref(gio);
663 if (cond == G_IO_HUP) {
665 _LOGI("socket G_IO_HUP");
666 __callback_info_free(mi);
671 if ((fd = g_io_channel_unix_get_fd(gio)) < 0) {
672 _LOGE("fail to get fd from io channel");
673 __callback_info_free(mi);
677 if ((pkt = __message_port_recv_raw(fd)) == NULL) {
678 _LOGE("recv error on SOCKET");
679 __callback_info_free(mi);
683 kb = bundle_decode(pkt->data, pkt->data_len);
684 if (pkt->is_bidirection)
685 mi->callback(mi->local_id, mi->remote_app_id, pkt->remote_port_name, pkt->is_trusted, kb, NULL);
687 mi->callback(mi->local_id, mi->remote_app_id, NULL, pkt->is_trusted, kb, NULL);
691 if (pkt->remote_port_name)
692 free(pkt->remote_port_name);
702 static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation)
704 char *local_port = NULL;
705 char *local_appid = NULL;
706 char *remote_appid = NULL;
707 char *remote_port = NULL;
708 gboolean local_trusted = false;
709 gboolean remote_trusted = false;
710 gboolean bi_dir = false;
714 bundle_raw *raw = NULL;
715 message_port_local_port_info_s *mi;
716 int local_reg_id = 0;
717 message_port_callback_info_s *callback_info;
721 GUnixFDList *fd_list;
723 int *returned_fds = NULL;
726 g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
727 &remote_appid, &remote_port, &remote_trusted, &len, &raw);
730 _LOGE("Invalid argument : remote_port is NULL");
734 _LOGE("Invalid argument : remote_appid is NULL");
737 if (!__is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
738 _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
742 _LOGE("Invalid argument : local_appid");
746 _LOGE("Invalid argument : local_port");
749 if (strcmp(remote_appid, __app_id) != 0) {
750 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
753 if (strcmp(remote_port, mi->port_name) != 0) {
754 _LOGE("Invalid argument : remote_port (%s)", remote_port);
758 _LOGE("Invalid argument : data_len");
761 if (remote_trusted) {
762 if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
763 if (!__is_preloaded(local_appid, remote_appid)) {
764 int ret = __check_certificate(local_appid, remote_appid);
765 if (ret == MESSAGEPORT_ERROR_NONE)
766 g_hash_table_insert(__trusted_app_list_hash, local_appid, "TRUE");
768 _LOGE("The application (%s) is not signed with the same certificate",
776 callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
777 if (callback_info == NULL)
780 callback_info->local_id = mi->local_id;
781 callback_info->remote_app_id = strdup(local_appid);
782 callback_info->callback = mi->callback;
784 msg = g_dbus_method_invocation_get_message(invocation);
785 fd_list = g_dbus_message_get_unix_fd_list(msg);
787 /* When application send message to self fd_list is NULL */
788 if (fd_list != NULL) {
789 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
790 if (returned_fds == NULL) {
791 _LOGE("fail to get fds");
792 __callback_info_free(callback_info);
795 fd = returned_fds[0];
797 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
800 callback_info->gio_read = g_io_channel_unix_new(fd);
801 if (!callback_info->gio_read) {
802 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
803 __callback_info_free(callback_info);
807 callback_info->g_src_id = g_io_add_watch(callback_info->gio_read, G_IO_IN | G_IO_HUP,
808 __socket_request_handler, (gpointer)callback_info);
809 if (callback_info->g_src_id == 0) {
810 _LOGE("fail to add watch on socket");
811 __callback_info_free(callback_info);
818 data = bundle_decode(raw, len);
820 _LOGE("Invalid argument : message");
824 LOGI("call calback %s", local_appid);
826 mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, NULL);
828 mi->callback(mi->local_id, local_appid, NULL, false, data, NULL);
837 static int __check_remote_port(const char *remote_app_id, const char *remote_port, bool is_trusted, bool *exist)
839 _LOGI("Check a remote port : [%s:%s]", remote_app_id, remote_port);
841 GVariant *result = NULL;
843 int ret_val = MESSAGEPORT_ERROR_NONE;
844 char *bus_name = NULL;
845 message_port_remote_app_info_s *remote_app_info = NULL;
846 port_list_info_s *port_info = NULL;
847 int local_reg_id = 0;
848 message_port_local_port_info_s *mi = NULL;
849 gboolean name_exist = false;
851 _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
853 ret_val = __get_remote_port_info(remote_app_id, remote_port, is_trusted, &remote_app_info, &port_info);
854 if (ret_val != MESSAGEPORT_ERROR_NONE)
858 if (strcmp(remote_app_id, __app_id) == 0) {
860 _LOGI("__is_local_port_registed ");
861 if (!__is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
866 _LOGI("__is_local_port_registed : %d ", *exist);
867 return MESSAGEPORT_ERROR_NONE;
870 port_info->exist = false;
871 bus_name = port_info->encoded_bus_name;
873 result = g_dbus_connection_call_sync(
879 g_variant_new("(s)", bus_name),
880 G_VARIANT_TYPE("(b)"),
881 G_DBUS_CALL_FLAGS_NONE,
886 if (err || (result == NULL)) {
888 _LOGE("No reply. error = %s", err->message);
891 ret_val = MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
893 g_variant_get(result, "(b)", &name_exist);
896 LOGE("Name not exist %s", bus_name);
898 ret_val = MESSAGEPORT_ERROR_NONE;
902 if (remote_app_info->certificate_info != CERTIFICATE_MATCH) {
903 if (!__is_preloaded(__app_id, remote_app_id)) {
904 if (__check_certificate(__app_id, remote_app_id) != MESSAGEPORT_ERROR_NONE) {
905 ret_val = MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
909 remote_app_info->certificate_info = CERTIFICATE_MATCH;
912 port_info->exist = true;
914 ret_val = MESSAGEPORT_ERROR_NONE;
919 g_variant_unref(result);
924 static void __on_sender_name_appeared(GDBusConnection *connection,
926 const gchar *name_owner,
929 _LOGI("sender name appeared : %s", name);
932 static void __on_sender_name_vanished(GDBusConnection *connection,
936 gboolean remove_result = FALSE;
937 int *watcher_id = (int *)user_data;
938 remove_result = g_hash_table_remove(__sender_appid_hash, (gpointer)name);
940 _LOGE("Fail to remove sender appid from hash : %s", name);
942 g_bus_unwatch_name(*watcher_id);
946 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
949 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
950 char *local_appid = NULL;
951 int pid = __get_sender_pid(conn, sender);
952 int *watcher_id = (int *)calloc(1, sizeof(int));
954 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
955 if (ret != AUL_R_OK) {
956 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
961 g_variant_get_child(parameters, 0, "&s", &local_appid);
962 if (local_appid == NULL) {
963 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
968 if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
969 _LOGI("insert sender !!!!! %s", sender);
970 g_hash_table_insert(__sender_appid_hash, (gpointer)strdup(sender), GINT_TO_POINTER(pid));
971 *watcher_id = g_bus_watch_name_on_connection(
974 G_BUS_NAME_WATCHER_FLAGS_NONE,
975 __on_sender_name_appeared,
976 __on_sender_name_vanished,
986 static void __dbus_method_call_handler(GDBusConnection *conn,
987 const gchar *sender, const gchar *object_path,
988 const gchar *iface_name, const gchar *method_name,
989 GVariant *parameters, GDBusMethodInvocation *invocation,
992 _LOGI("method_name: %s, sender: %s", method_name, sender);
993 gpointer sender_pid = g_hash_table_lookup(__sender_appid_hash, sender);
994 if (sender_pid == NULL) {
995 if (!__check_sender_validation(parameters, sender, conn))
998 if (g_strcmp0(method_name, "send_message") == 0)
999 send_message(parameters, invocation);
1001 g_dbus_method_invocation_return_value(invocation, NULL);
1004 static const GDBusInterfaceVTable interface_vtable = {
1005 __dbus_method_call_handler,
1010 static int __dbus_init(void)
1013 GError *error = NULL;
1015 __gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
1016 if (__gdbus_conn == NULL) {
1017 if (error != NULL) {
1018 _LOGE("Failed to get dbus [%s]", error->message);
1019 g_error_free(error);
1028 g_object_unref(__gdbus_conn);
1034 int __register_dbus_interface(const char *port_name, bool is_trusted)
1037 GDBusNodeInfo *introspection_data = NULL;
1038 int registration_id = 0;
1040 static gchar introspection_prefix[] =
1042 " <interface name='";
1044 static gchar introspection_postfix[] =
1046 " <method name='send_message'>"
1047 " <arg type='s' name='local_appid' direction='in'/>"
1048 " <arg type='s' name='local_port' direction='in'/>"
1049 " <arg type='b' name='local_trusted' direction='in'/>"
1050 " <arg type='b' name='bi_dir' direction='in'/>"
1051 " <arg type='s' name='remote_appid' direction='in'/>"
1052 " <arg type='s' name='remote_port' direction='in'/>"
1053 " <arg type='b' name='remote_trusted' direction='in'/>"
1054 " <arg type='u' name='data_len' direction='in'/>"
1055 " <arg type='s' name='data' direction='in'/>"
1060 char *introspection_xml = NULL;
1061 int introspection_xml_len = 0;
1065 GError *error = NULL;
1066 char *bus_name = NULL;
1067 char *interface_name = NULL;
1068 GVariant *result = NULL;
1070 bus_name = __get_encoded_name(__app_id, port_name, is_trusted);
1072 _LOGE("Fail to get bus name");
1075 interface_name = bus_name;
1077 introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
1078 strlen(introspection_postfix) + 1;
1080 introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
1081 if (!introspection_xml) {
1082 _LOGE("out of memory");
1087 result = g_dbus_connection_call_sync(
1091 DBUS_INTERFACE_DBUS,
1093 g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
1094 G_VARIANT_TYPE("(u)"),
1095 G_DBUS_CALL_FLAGS_NONE,
1100 _LOGE("RequestName fail : %s", error->message);
1103 if (result == NULL) {
1104 _LOGE("fail to get name NULL");
1107 g_variant_get(result, "(u)", &owner_id);
1108 if (owner_id == 0) {
1109 _LOGE("Acquiring the own name is failed");
1113 _LOGI("Acquiring the own name : %d", owner_id);
1115 snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
1117 introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
1118 if (!introspection_data) {
1119 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
1123 registration_id = g_dbus_connection_register_object(__gdbus_conn,
1124 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
1125 &interface_vtable, NULL, NULL, NULL);
1127 _LOGI("registration_id %d", registration_id);
1129 if (registration_id == 0) {
1130 _LOGE("Failed to g_dbus_connection_register_object");
1135 if (introspection_data)
1136 g_dbus_node_info_unref(introspection_data);
1137 if (introspection_xml)
1138 free(introspection_xml);
1142 g_variant_unref(result);
1145 return registration_id;
1148 /* LCOV_EXCL_START */
1149 void __list_free_port_list(gpointer data)
1151 port_list_info_s *n = (port_list_info_s *)data;
1153 FREE_AND_NULL(n->encoded_bus_name);
1154 FREE_AND_NULL(n->port_name);
1157 /* LCOV_EXCL_STOP */
1159 /* LCOV_EXCL_START */
1160 static void __hash_destory_local_value(gpointer data)
1162 message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
1165 free(mli->port_name);
1169 /* LCOV_EXCL_STOP */
1171 /* LCOV_EXCL_START */
1172 static void __hash_destory_remote_value(gpointer data)
1174 message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
1176 FREE_AND_NULL(mri->sender_id);
1177 FREE_AND_NULL(mri->remote_app_id);
1179 g_list_free_full(mri->port_list, __list_free_port_list);
1184 /* LCOV_EXCL_STOP */
1186 static bool __initialize(void)
1189 #if !GLIB_CHECK_VERSION(2, 35, 0)
1195 char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
1197 _LOGI("initialize");
1198 ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
1199 retvm_if(ret != AUL_R_OK, false, "Failed to get the application ID: %d", ret);
1201 __app_id = strdup(buffer);
1202 retvm_if(!__app_id, false, "Malloc failed");
1203 _LOGI("init : %s", __app_id);
1205 if (__local_port_info == NULL) {
1206 __local_port_info = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destory_local_value);
1207 retvm_if(!__local_port_info, false, "fail to create __local_port_info");
1210 if (__remote_app_info == NULL) {
1211 __remote_app_info = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __hash_destory_remote_value);
1212 retvm_if(!__remote_app_info, false, "fail to create __remote_app_info");
1215 if (__sender_appid_hash == NULL) {
1216 __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
1217 retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
1220 if (__trusted_app_list_hash == NULL) {
1221 __trusted_app_list_hash = g_hash_table_new(g_str_hash, g_str_equal);
1222 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
1227 _initialized = true;
1233 static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, messageport_message_cb callback)
1235 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
1236 retvm_if(!mi, false, "Malloc failed");
1238 mi->callback = callback;
1239 mi->is_trusted = is_trusted;
1240 mi->port_name = strdup(local_port);
1241 if (mi->port_name == NULL) {
1242 _LOGE("Malloc failed (%s)", local_port);
1246 mi->local_id = local_id;
1248 g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
1252 static int __register_message_port(const char *local_port, bool is_trusted, messageport_message_cb callback)
1254 _SECURE_LOGI("Register a message port : [%s:%s]", __app_id, local_port);
1258 /* Check the message port is already registed */
1259 if (__is_local_port_registed(local_port, is_trusted, &local_id, NULL))
1262 local_id = __register_dbus_interface(local_port, is_trusted);
1264 _LOGE("register_dbus_interface fail !!");
1265 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1268 if (!__message_port_register_port(local_id, local_port, is_trusted, callback))
1269 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1274 int __message_port_send_async(int sockfd, bundle *kb, const char *local_port,
1275 bool local_trusted, bool is_bidirection)
1279 int local_port_len = 0;
1281 bundle_raw *kb_data = NULL;
1283 if (local_port != NULL)
1284 local_port_len = strlen(local_port) + 1;
1286 if (__write_string_to_socket(sockfd, local_port, local_port_len) != MESSAGEPORT_ERROR_NONE) {
1287 _LOGE("write local_port fail");
1288 return MESSAGEPORT_ERROR_IO_ERROR;
1291 if (__write_socket(sockfd, (char *)&is_bidirection, sizeof(is_bidirection), &nb) != MESSAGEPORT_ERROR_NONE) {
1292 _LOGE("write is_bidirection fail");
1293 return MESSAGEPORT_ERROR_IO_ERROR;
1296 if (__write_socket(sockfd, (char *)&local_trusted, sizeof(local_trusted), &nb) != MESSAGEPORT_ERROR_NONE) {
1297 _LOGE("write local_trusted fail");
1298 return MESSAGEPORT_ERROR_IO_ERROR;
1301 bundle_encode(kb, &kb_data, &data_len);
1302 if (kb_data == NULL) {
1303 _LOGE("bundle encode fail");
1304 ret = MESSAGEPORT_ERROR_IO_ERROR;
1308 if (data_len > MAX_MESSAGE_SIZE) {
1309 _LOGE("bigger than max size\n");
1310 ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1314 if (__write_string_to_socket(sockfd, (void *)kb_data, data_len) != MESSAGEPORT_ERROR_NONE) {
1315 _LOGE("write kb_data fail");
1316 ret = MESSAGEPORT_ERROR_IO_ERROR;
1325 static int __message_port_send_message(const char *remote_appid, const char *remote_port,
1326 const char *local_port, bool trusted_message, bool local_trusted, bool bi_dir, bundle *message)
1329 int ret = MESSAGEPORT_ERROR_NONE;
1330 GUnixFDList *fd_list = NULL;
1333 bundle_raw *raw = NULL;
1334 char *bus_name = NULL;
1335 char *interface_name = NULL;
1337 message_port_remote_app_info_s *remote_app_info = NULL;
1338 port_list_info_s *port_info = NULL;
1339 GDBusMessage *msg = NULL;
1341 GVariant *body = NULL;
1342 int sock_pair[2] = {0,};
1344 ret = __get_remote_port_info(remote_appid, remote_port, trusted_message, &remote_app_info, &port_info);
1345 if (ret != MESSAGEPORT_ERROR_NONE)
1348 if (port_info->exist == false) {
1350 _LOGI("port exist check !!");
1351 ret = __check_remote_port(remote_appid, remote_port, trusted_message, &exist);
1352 if (ret != MESSAGEPORT_ERROR_NONE) {
1354 } else if (!exist) {
1355 ret = MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1360 if (port_info->send_sock_fd > 0) {
1361 ret = __message_port_send_async(port_info->send_sock_fd, message,
1362 (local_port) ? local_port : "", local_trusted, bi_dir);
1365 bus_name = port_info->encoded_bus_name;
1366 interface_name = bus_name;
1368 if (bundle_encode(message, &raw, &len) != BUNDLE_ERROR_NONE) {
1369 ret = MESSAGEPORT_ERROR_INVALID_PARAMETER;
1373 if (MAX_MESSAGE_SIZE < len) {
1374 _LOGE("The size of message (%d) has exceeded the maximum limit.", len);
1375 ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1379 body = g_variant_new("(ssbbssbus)", __app_id, (local_port) ? local_port : "", local_trusted, bi_dir,
1380 remote_appid, remote_port, trusted_message, len, raw);
1381 if (strcmp(remote_appid, __app_id) != 0) { /* self send */
1383 /* if message-port fail to get socket pair, communicate using GDBus */
1384 if (aul_request_message_port_socket_pair(sock_pair) != AUL_R_OK) {
1385 _LOGE("error create socket pair");
1388 _LOGI("sock pair : %d, %d",
1389 sock_pair[SOCK_PAIR_SENDER], sock_pair[SOCK_PAIR_RECEIVER]);
1390 fd_list = g_unix_fd_list_new();
1391 g_unix_fd_list_append(fd_list, sock_pair[SOCK_PAIR_RECEIVER], &err);
1393 _LOGE("g_unix_fd_list_append [%s]", err->message);
1394 ret = MESSAGEPORT_ERROR_IO_ERROR;
1398 port_info->send_sock_fd = sock_pair[SOCK_PAIR_SENDER];
1399 close(sock_pair[SOCK_PAIR_RECEIVER]);
1403 msg = g_dbus_message_new_method_call(bus_name, MESSAGEPORT_OBJECT_PATH, interface_name, "send_message");
1405 _LOGE("Can't allocate new method call");
1406 ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1410 g_dbus_message_set_unix_fd_list(msg, fd_list);
1411 g_dbus_message_set_body(msg, body);
1412 g_dbus_connection_send_message(__gdbus_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
1414 _LOGE("No reply. error = %s", err->message);
1416 ret = MESSAGEPORT_ERROR_IO_ERROR;
1425 g_object_unref(msg);
1427 bundle_free_encoded_rawdata(&raw);
1429 g_object_unref(fd_list);
1435 int __message_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port, bool trusted_message, bundle *message)
1437 message_port_local_port_info_s *local_info;
1438 int ret = __get_local_port_info(id, &local_info);
1439 if (ret != MESSAGEPORT_ERROR_NONE)
1442 _LOGI("bidirectional_message %s", local_info->port_name);
1443 return __message_port_send_message(remote_app_id, remote_port,
1444 local_info->port_name, trusted_message, local_info->is_trusted, true, message);
1447 int messageport_unregister_local_port(int local_port_id, bool trusted_port)
1451 char *bus_name = NULL;
1455 _LOGI("unregister : %d", local_port_id);
1457 message_port_local_port_info_s *mi =
1458 (message_port_local_port_info_s *)
1459 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
1461 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1463 if (mi->is_trusted != trusted_port)
1464 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
1466 bus_name = __get_encoded_name(__app_id, mi->port_name, mi->is_trusted);
1467 if (bus_name == NULL)
1468 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1470 g_dbus_connection_unregister_object(__gdbus_conn, local_port_id);
1472 result = g_dbus_connection_call_sync(
1476 DBUS_INTERFACE_DBUS,
1478 g_variant_new("(s)", bus_name),
1479 G_VARIANT_TYPE("(u)"),
1480 G_DBUS_CALL_FLAGS_NONE,
1489 _LOGE("RequestName fail : %s", err->message);
1491 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1493 g_variant_get(result, "(u)", &ret);
1496 g_variant_unref(result);
1498 if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
1500 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
1501 _LOGE("Port Not exist");
1502 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1503 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
1504 _LOGE("Try to release not owned name. MESSAGEPORT_ERROR_INVALID_PARAMETER");
1505 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
1510 g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
1512 return MESSAGEPORT_ERROR_NONE;
1515 int messageport_register_local_port(const char *local_port, messageport_message_cb callback)
1517 if (!_initialized) {
1518 if (!__initialize())
1519 return MESSAGEPORT_ERROR_IO_ERROR;
1522 return __register_message_port(local_port, false, callback);
1525 int messageport_register_trusted_local_port(const char *local_port, messageport_message_cb callback)
1527 if (!_initialized) {
1528 if (!__initialize())
1529 return MESSAGEPORT_ERROR_IO_ERROR;
1532 return __register_message_port(local_port, true, callback);
1536 int messageport_check_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
1538 if (!_initialized) {
1539 if (!__initialize())
1540 return MESSAGEPORT_ERROR_IO_ERROR;
1543 int ret = __check_remote_port(remote_app_id, remote_port, false, exist);
1544 if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
1546 ret = MESSAGEPORT_ERROR_NONE;
1552 int messageport_check_trusted_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
1554 if (!_initialized) {
1555 if (!__initialize())
1556 return MESSAGEPORT_ERROR_IO_ERROR;
1559 int ret = __check_remote_port(remote_app_id, remote_port, true, exist);
1560 if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
1562 ret = MESSAGEPORT_ERROR_NONE;
1568 int messageport_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
1570 if (!_initialized) {
1571 if (!__initialize())
1572 return MESSAGEPORT_ERROR_IO_ERROR;
1575 return __message_port_send_message(remote_app_id, remote_port, NULL, false, false, false, message);
1578 int messageport_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
1580 if (!_initialized) {
1581 if (!__initialize())
1582 return MESSAGEPORT_ERROR_IO_ERROR;
1585 return __message_port_send_message(remote_app_id, remote_port, NULL, true, false, false, message);
1588 int messageport_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port,
1591 if (!_initialized) {
1592 if (!__initialize())
1593 return MESSAGEPORT_ERROR_IO_ERROR;
1596 return __message_send_bidirectional_message(id, remote_app_id, remote_port, false, message);
1599 int messageport_send_bidirectional_trusted_message(int id, const char *remote_app_id, const char *remote_port,
1602 if (!_initialized) {
1603 if (!__initialize())
1604 return MESSAGEPORT_ERROR_IO_ERROR;
1606 return __message_send_bidirectional_message(id, remote_app_id, remote_port, true, message);