2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <bundle_internal.h>
23 #include <sys/socket.h>
31 #include <gio/gunixfdlist.h>
32 #include <glib-unix.h>
34 #include "message_port_log.h"
35 #include "message_port.h"
36 #include "message_port_remote.h"
37 #include "message_port_common.h"
39 #define MAX_RETRY_CNT 10
40 #define SOCK_PAIR_SENDER 0
41 #define SOCK_PAIR_RECEIVER 1
43 static bool _initialized = false;
44 static GHashTable *__remote_app_info;
45 static GHashTable *__registered_callback_info_hash;
46 static const int MAX_MESSAGE_SIZE = 16 * 1024;
48 enum __certificate_info_type {
51 CERTIFICATE_NOT_MATCH,
54 typedef struct message_port_remote_port_info {
58 } message_port_remote_app_info_s;
60 typedef struct port_list_info {
61 message_port_remote_app_info_s *remote_app_info;
63 char *encoded_bus_name;
69 GList *delayed_message_list;
70 unsigned int delayed_message_size;
74 typedef struct port_key_info {
80 typedef struct delay_port_info {
81 port_key_info_s *key_info;
82 port_list_info_s *port_info;
85 typedef struct registered_callback_info {
91 message_port_registration_event_cb registered_cb;
92 message_port_registration_event_cb unregistered_cb;
93 } registered_callback_info_s;
95 enum transmission_sequence {
106 typedef struct delay_message {
108 unsigned int sent_bytes;
111 char *local_port_name;
116 } delay_message_info_s;
118 /* LCOV_EXCL_START */
119 static void __free_delay_message_info(delay_message_info_s *message)
121 if (message != NULL) {
122 FREE_AND_NULL(message->local_port_name);
123 FREE_AND_NULL(message->data);
124 FREE_AND_NULL(message);
129 /* LCOV_EXCL_START */
130 static void __free_list_delay_message_info(gpointer data)
132 delay_message_info_s *message = (delay_message_info_s *)data;
135 __free_delay_message_info(message);
139 /* LCOV_EXCL_START */
140 static void __clear_disconnect_socket(port_list_info_s *port_info)
142 GError *error = NULL;
144 if (port_info == NULL)
147 if (port_info->gio_read != NULL) {
148 g_io_channel_shutdown(port_info->gio_read, TRUE, &error);
150 _LOGE("g_io_channel_shutdown error : %s", error->message);
153 g_io_channel_unref(port_info->gio_read);
154 port_info->gio_read = NULL;
157 if (port_info->g_src_id != 0) {
158 g_source_remove(port_info->g_src_id);
159 port_info->g_src_id = 0;
162 if (port_info->delay_src_id != 0) {
163 g_source_remove(port_info->delay_src_id);
164 port_info->delay_src_id = 0;
167 if (port_info->delayed_message_list != NULL) {
168 g_list_free_full(port_info->delayed_message_list, __free_list_delay_message_info);
170 port_info->delayed_message_list = NULL;
173 port_info->delayed_message_size = 0;
174 port_info->send_sock_fd = 0;
178 /* LCOV_EXCL_START */
179 static void __free_port_info(gpointer data)
181 port_list_info_s *port_info = (port_list_info_s *)data;
182 message_port_remote_app_info_s *remote_app_info;
184 if (port_info == NULL)
187 remote_app_info = port_info->remote_app_info;
189 _LOGI("__free_port_info : remote_app_id : %s port_name : %s",
190 remote_app_info->remote_app_id,
191 port_info->port_name);
193 remote_app_info->port_list = g_list_remove(remote_app_info->port_list,
196 __clear_disconnect_socket(port_info);
198 if (port_info->encoded_bus_name)
199 free(port_info->encoded_bus_name);
200 if (port_info->port_name)
201 free(port_info->port_name);
205 if (g_list_length(remote_app_info->port_list) == 0) {
206 g_hash_table_remove(__remote_app_info,
207 remote_app_info->remote_app_id);
212 /* LCOV_EXCL_START */
213 static void __hash_destory_remote_value(gpointer data)
215 message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
217 FREE_AND_NULL(mri->remote_app_id);
219 g_list_free_full(mri->port_list, __free_port_info);
226 static void __registered_callback_info_free(gpointer data)
228 registered_callback_info_s *callback_info = (registered_callback_info_s *)data;
229 if (callback_info == NULL)
232 if (callback_info->remote_app_id)
233 free(callback_info->remote_app_id);
235 if (callback_info->remote_port)
236 free(callback_info->remote_port);
241 static bool __initialize(void)
243 if (!initialized_common) {
244 if (!initialize_common())
248 if (__remote_app_info == NULL) {
249 __remote_app_info = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __hash_destory_remote_value);
250 retvm_if(!__remote_app_info, false, "fail to create __remote_app_info");
253 if (__registered_callback_info_hash == NULL) {
254 __registered_callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __registered_callback_info_free);
255 retvm_if(!__registered_callback_info_hash, false, "fail to create __registered_callback_info_hash");
263 static int __remote_port_compare_cb(gconstpointer a, gconstpointer b)
265 port_list_info_s *key1 = (port_list_info_s *)a;
266 port_list_info_s *key2 = (port_list_info_s *)b;
268 if (key1->is_trusted == key2->is_trusted)
269 return strcmp(key1->port_name, key2->port_name);
274 static int __key_compare_cb(gconstpointer a, gconstpointer b)
276 port_list_info_s *key1 = (port_list_info_s *)a;
277 port_key_info_s *key2 = (port_key_info_s *)b;
279 if (key1->is_trusted == key2->is_trusted)
280 return strcmp(key1->port_name, key2->port_name);
285 static port_list_info_s *__set_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
287 int ret_val = MESSAGE_PORT_ERROR_NONE;
288 port_list_info_s *port_info = (port_list_info_s *)calloc(1, sizeof(port_list_info_s));
291 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
294 port_info->port_name = strdup(remote_port);
295 if (!port_info->port_name) {
296 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
299 port_info->is_trusted = is_trusted;
300 port_info->encoded_bus_name = get_encoded_name(remote_app_id, remote_port, is_trusted);
301 if (port_info->encoded_bus_name == NULL) {
302 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
305 port_info->send_sock_fd = 0;
307 if (ret_val != MESSAGE_PORT_ERROR_NONE) {
309 FREE_AND_NULL(port_info->port_name);
310 FREE_AND_NULL(port_info->encoded_bus_name);
318 static message_port_remote_app_info_s *__set_remote_app_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
320 message_port_remote_app_info_s *remote_app_info = NULL;
321 int ret_val = MESSAGE_PORT_ERROR_NONE;
323 remote_app_info = (message_port_remote_app_info_s *)calloc(1, sizeof(message_port_remote_app_info_s));
324 if (!remote_app_info) {
325 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
329 remote_app_info->remote_app_id = strdup(remote_app_id);
330 if (remote_app_info->remote_app_id == NULL) {
331 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
336 if (ret_val != MESSAGE_PORT_ERROR_NONE) {
337 if (remote_app_info) {
338 FREE_AND_NULL(remote_app_info->remote_app_id);
339 FREE_AND_NULL(remote_app_info);
343 return remote_app_info;
347 static void __free_port_info_by_key(port_key_info_s *key_info)
349 port_list_info_s *found_port_info;
350 message_port_remote_app_info_s *found_remote_port_info;
353 found_remote_port_info =
354 (message_port_remote_app_info_s *)g_hash_table_lookup(
355 __remote_app_info, key_info->remote_app_id);
356 if (found_remote_port_info == NULL)
359 cb_list = g_list_find_custom(found_remote_port_info->port_list, key_info,
360 (GCompareFunc)__key_compare_cb);
364 found_port_info = (port_list_info_s *)cb_list->data;
365 __free_port_info(found_port_info);
370 _LOGE("Not found port_info");
373 static void __free_key_info(port_key_info_s *key_info)
375 FREE_AND_NULL(key_info->port_name);
376 FREE_AND_NULL(key_info->remote_app_id);
377 FREE_AND_NULL(key_info);
380 static gboolean __socket_disconnect_handler(GIOChannel *gio,
384 _LOGI("__socket_disconnect_handler %d", cond);
385 message_port_lock_mutex();
386 __free_port_info_by_key((port_key_info_s *)data);
387 message_port_unlock_mutex();
392 static void __socket_destroy_handler(gpointer data)
394 _LOGI("__socket_destroy_handler");
396 port_key_info_s *key_info = (port_key_info_s *)data;
397 __free_key_info(key_info);
400 /* LCOV_EXCL_START */
401 static void __delay_socket_destroy_handler(gpointer data)
403 _LOGI("__delay_socket_destroy_handler");
404 delay_port_info *delay_info = (delay_port_info *)data;
406 FREE_AND_NULL(delay_info->key_info->port_name);
407 FREE_AND_NULL(delay_info->key_info->remote_app_id);
408 FREE_AND_NULL(delay_info->key_info);
413 static int __create_port_key_info(
414 port_list_info_s *port_info,
415 port_key_info_s **key_info)
417 int ret_val = MESSAGE_PORT_ERROR_NONE;
418 port_key_info_s *_key_info = (port_key_info_s *)
419 calloc(1, sizeof(port_key_info_s));
420 if (_key_info == NULL) {
421 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
422 _LOGE("out of memory");
426 _key_info->port_name = strdup(port_info->port_name);
427 if (_key_info->port_name == NULL) {
428 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
429 _LOGE("out of memory");
433 _key_info->is_trusted = port_info->is_trusted;
435 _key_info->remote_app_id = strdup(port_info->remote_app_info->remote_app_id);
436 if (_key_info->remote_app_id == NULL) {
437 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
438 _LOGE("out of memory");
443 if (ret_val == MESSAGE_PORT_ERROR_NONE) {
444 *key_info = _key_info;
447 FREE_AND_NULL(_key_info->port_name);
448 FREE_AND_NULL(_key_info->remote_app_id);
455 static int __get_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted,
456 message_port_remote_app_info_s **mri, port_list_info_s **pli)
458 message_port_remote_app_info_s *remote_app_info = NULL;
459 port_list_info_s port_info;
460 GList *cb_list = NULL;
461 int ret_val = MESSAGE_PORT_ERROR_NONE;
463 remote_app_info = (message_port_remote_app_info_s *)g_hash_table_lookup(__remote_app_info, remote_app_id);
464 if (remote_app_info == NULL) {
465 remote_app_info = __set_remote_app_info(remote_app_id, remote_port, is_trusted);
467 if (remote_app_info == NULL) {
468 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
471 g_hash_table_insert(__remote_app_info, remote_app_info->remote_app_id, remote_app_info);
473 *mri = remote_app_info;
475 port_info.port_name = strdup(remote_port);
476 if (port_info.port_name == NULL) {
477 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
480 port_info.is_trusted = is_trusted;
481 cb_list = g_list_find_custom(remote_app_info->port_list, &port_info,
482 (GCompareFunc)__remote_port_compare_cb);
483 if (port_info.port_name)
484 free(port_info.port_name);
485 if (cb_list == NULL) {
486 port_list_info_s *tmp = __set_remote_port_info(remote_app_id, remote_port, is_trusted);
488 ret_val = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
491 remote_app_info->port_list = g_list_append(remote_app_info->port_list, tmp);
492 tmp->remote_app_info = remote_app_info;
495 *pli = (port_list_info_s *)cb_list->data;
502 int check_remote_port(const char *remote_app_id, const char *remote_port, bool is_trusted, bool *exist)
504 _LOGD("Check a remote port : [%s:%s]", remote_app_id, remote_port);
506 GVariant *result = NULL;
508 int ret_val = MESSAGE_PORT_ERROR_NONE;
509 char *bus_name = NULL;
510 message_port_remote_app_info_s *remote_app_info = NULL;
511 port_list_info_s *port_info = NULL;
512 int local_reg_id = 0;
513 message_port_local_port_info_s *mi = NULL;
514 gboolean name_exist = false;
518 return MESSAGE_PORT_ERROR_IO_ERROR;
521 _LOGD("remote_app_id, app_id :[%s : %s] ", remote_app_id, app_id);
523 ret_val = __get_remote_port_info(remote_app_id, remote_port, is_trusted, &remote_app_info, &port_info);
524 if (ret_val != MESSAGE_PORT_ERROR_NONE)
528 if (strcmp(remote_app_id, app_id) == 0) {
530 _LOGD("__is_local_port_registed ");
531 if (!is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
536 _LOGD("__is_local_port_registed : %d ", *exist);
537 return MESSAGE_PORT_ERROR_NONE;
540 port_info->exist = false;
541 bus_name = port_info->encoded_bus_name;
543 result = g_dbus_connection_call_sync(
549 g_variant_new("(s)", bus_name),
550 G_VARIANT_TYPE("(b)"),
551 G_DBUS_CALL_FLAGS_NONE,
556 if (err || (result == NULL)) {
558 _LOGE("No reply. error = %s", err->message);
561 ret_val = MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
563 g_variant_get(result, "(b)", &name_exist);
566 _LOGI("Name not exist %s", bus_name);
568 ret_val = MESSAGE_PORT_ERROR_NONE;
572 if (remote_app_info->certificate_info != CERTIFICATE_MATCH) {
573 if (!is_preloaded(app_id, remote_app_id)) {
574 if (check_certificate(app_id, remote_app_id) != MESSAGE_PORT_ERROR_NONE) {
575 ret_val = MESSAGE_PORT_ERROR_CERTIFICATE_NOT_MATCH;
579 remote_app_info->certificate_info = CERTIFICATE_MATCH;
582 port_info->exist = true;
584 ret_val = MESSAGE_PORT_ERROR_NONE;
589 g_variant_unref(result);
591 if (ret_val != MESSAGE_PORT_ERROR_NONE || !name_exist)
592 __free_port_info((gpointer)port_info);
597 /* LCOV_EXCL_START */
598 static int __send_delayed_message(int sockfd, delay_message_info_s *message)
601 int sequence = message->sequence - 1;
602 int ret = MESSAGE_PORT_ERROR_NONE;
603 bool is_startline = true;
606 _LOGI("send_delayed_message : sockfd (%d) sequence(%d) sent byte(%d)",
607 sockfd, message->sequence, message->sent_bytes);
609 switch (message->sequence) {
612 is_startline = false;
614 case SEQUENCE_PORT_LEN:
616 offset = message->sent_bytes;
618 ret = write_socket(sockfd, ((char *)&message->local_port_len) + offset,
619 sizeof(message->local_port_len) - offset, &nb, &sequence);
620 if (ret != MESSAGE_PORT_ERROR_NONE) {
621 _LOGE("write local_port_len fail");
625 is_startline = false;
627 case SEQUENCE_PORT_NAME:
629 offset = message->sent_bytes;
631 if (message->local_port_len > 0)
632 ret = write_socket(sockfd, message->local_port_name + offset,
633 message->local_port_len - offset , &nb, &sequence);
637 if (ret != MESSAGE_PORT_ERROR_NONE) {
638 _LOGE("write local_port fail");
642 is_startline = false;
644 case SEQUENCE_BIDIRECTION:
646 offset = message->sent_bytes;
648 ret = write_socket(sockfd, ((char *)&message->is_bidirection) + offset,
649 sizeof(message->is_bidirection) - offset, &nb, &sequence);
650 if (ret != MESSAGE_PORT_ERROR_NONE) {
651 _LOGE("write is_bidirection fail");
655 is_startline = false;
657 case SEQUENCE_TRUSTED:
659 offset = message->sent_bytes;
661 ret = write_socket(sockfd, ((char *)&message->local_trusted) + offset,
662 sizeof(message->local_trusted) - offset, &nb, &sequence);
663 if (ret != MESSAGE_PORT_ERROR_NONE) {
664 _LOGE("write local_trusted fail");
668 is_startline = false;
670 case SEQUENCE_DTAT_LEN:
672 offset = message->sent_bytes;
674 ret = write_socket(sockfd, ((char *)&message->data_len) + offset,
675 sizeof(message->data_len) - offset, &nb, &sequence);
676 if (ret != MESSAGE_PORT_ERROR_NONE) {
677 _LOGE("write data_len fail");
681 is_startline = false;
685 offset = message->sent_bytes;
687 ret = write_socket(sockfd, (char *)message->data + offset,
688 message->data_len - offset, &nb, &sequence);
690 if (ret != MESSAGE_PORT_ERROR_NONE) {
691 _LOGE("write data fail");
695 is_startline = false;
698 ret = MESSAGE_PORT_ERROR_NONE;
703 if (ret == MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE) {
705 message->sent_bytes += nb;
707 message->sent_bytes = nb;
709 message->sequence = sequence;
710 _LOGE("send_delayed_message fail : sockfd (%d) sequence(%d) sent byte(%d)",
711 sockfd, message->sequence, message->sent_bytes);
719 static bool __validate_delay_port_info(delay_port_info *delay_info)
721 message_port_remote_app_info_s *found_remote_port_info;
724 found_remote_port_info =
725 (message_port_remote_app_info_s *)g_hash_table_lookup(
726 __remote_app_info, delay_info->key_info->remote_app_id);
727 if (found_remote_port_info == NULL)
730 cb_list = g_list_find(found_remote_port_info->port_list, delay_info->port_info);
737 /* LCOV_EXCL_START */
738 static int __pop_delayed_message(port_list_info_s *port_info)
740 delay_message_info_s *message;
743 if (port_info->delayed_message_list == NULL)
744 return MESSAGE_PORT_ERROR_NONE;
746 message = g_list_nth_data(port_info->delayed_message_list, 0);
747 ret = __send_delayed_message(port_info->send_sock_fd, message);
749 if (ret != MESSAGE_PORT_ERROR_NONE)
752 port_info->delayed_message_size -= message->size;
753 port_info->delayed_message_list = g_list_remove(port_info->delayed_message_list, message);
754 _LOGI("pop : delayed_message_size (%d), count(%d)",
755 port_info->delayed_message_size,
756 g_list_length(port_info->delayed_message_list));
758 __free_delay_message_info(message);
760 return MESSAGE_PORT_ERROR_NONE;
764 static gboolean __process_delayed_message(gint fd, GIOCondition cond, gpointer data)
766 delay_port_info *delay_info = (delay_port_info *)data;
767 port_list_info_s *port_info = delay_info->port_info;
771 if (port_info == NULL)
772 return G_SOURCE_REMOVE;
774 message_port_lock_mutex();
776 if (__validate_delay_port_info(delay_info) == false) {
777 message_port_unlock_mutex();
778 return G_SOURCE_REMOVE;
781 if (port_info->delayed_message_list == NULL) {
782 port_info->delayed_message_size = 0;
783 port_info->delay_src_id = 0;
784 message_port_unlock_mutex();
785 return G_SOURCE_REMOVE;
787 ret = __pop_delayed_message(port_info);
788 if (ret == MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE) {
789 message_port_unlock_mutex();
790 return G_SOURCE_CONTINUE;
791 } else if (ret == MESSAGE_PORT_ERROR_IO_ERROR) {
792 port_info->delay_src_id = 0;
793 message_port_unlock_mutex();
794 return G_SOURCE_REMOVE;
798 message_port_unlock_mutex();
800 return G_SOURCE_CONTINUE;
804 /* LCOV_EXCL_START */
805 static int __push_delayed_message(port_list_info_s *port_info,
809 unsigned int sent_bytes,
810 const char *local_port,
814 #define QUEUE_SIZE_MAX (1024 * 1024) /* 1MB per remote port (MAX) */
816 unsigned int tmp_size;
817 unsigned int message_size;
818 int ret = MESSAGE_PORT_ERROR_NONE;
819 delay_port_info *delay_info;
821 if (port_info->delayed_message_size >= QUEUE_SIZE_MAX) {
822 _LOGE("cache fail : delayed_message_size (%d), count(%d)",
823 port_info->delayed_message_size, g_list_length(port_info->delayed_message_list));
824 return MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
827 delay_message_info_s *message = (delay_message_info_s *)calloc(1, sizeof(delay_message_info_s));
828 retvm_if(!message, MESSAGE_PORT_ERROR_OUT_OF_MEMORY, "Malloc failed");
830 message_size = (unsigned int)sizeof(delay_message_info_s);
832 message->sequence = sequence;
833 tmp_size = (unsigned int)strlen(local_port) + 1;
834 message_size += tmp_size;
835 message->local_port_len = tmp_size;
836 message->local_port_name = strdup(local_port);
837 if (message->local_port_name == NULL) {
838 _LOGE("local_port_name strdup fail");
839 ret = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
842 message->is_bidirection = is_bidirection;
843 message->local_trusted = local_trusted;
844 message_size += data_len;
845 message->data_len = data_len;
846 message->data = (bundle_raw *)strdup((const char *)kb_data);
847 if (message->data == NULL) {
848 _LOGE("data strdup fail");
849 ret = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
854 message->sent_bytes = sent_bytes;
855 message->size = message_size;
856 port_info->delayed_message_size += message_size;
858 port_info->delayed_message_list = g_list_append(port_info->delayed_message_list, message);
860 if (port_info->delay_src_id == 0) {
861 delay_info = (delay_port_info *)calloc(1, sizeof(delay_port_info));
862 if (delay_info == NULL) {
863 _LOGE("out of memory");
864 ret = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
868 ret = __create_port_key_info(port_info, &delay_info->key_info);
869 if (ret != MESSAGE_PORT_ERROR_NONE) {
874 delay_info->port_info = port_info;
876 port_info->delay_src_id = g_unix_fd_add_full(G_PRIORITY_DEFAULT,
877 port_info->send_sock_fd, G_IO_OUT, __process_delayed_message,
878 delay_info, __delay_socket_destroy_handler);
881 _LOGE("inserted : pm(%s) fd(%d) ms(%d) ds(%d) dlc(%d) sqn(%d) sb (%d)",
882 port_info->port_name, port_info->send_sock_fd, message_size,
883 port_info->delayed_message_size,
884 g_list_length(port_info->delayed_message_list), sequence, sent_bytes);
887 if (ret != MESSAGE_PORT_ERROR_NONE)
888 __free_delay_message_info(message);
893 static bool __can_write(int fd)
895 struct pollfd fds[1];
897 fds[0].events = POLLOUT;
899 int ret = poll(fds, 1, 100);
900 if (ret == 0 || ret < 0) {
901 _LOGI("poll() is failed. fd(%d), ret(%d) error(%s)",
902 fd, ret, ret == 0 ? "timed out" : "");
910 static int __message_port_send_async(port_list_info_s *port_info, bundle *kb, const char *local_port,
911 bool local_trusted, bool is_bidirection)
916 bundle_raw *kb_data = NULL;
917 int sequence = SEQUENCE_START;
919 bundle_encode(kb, &kb_data, &data_len);
920 if (kb_data == NULL) {
921 _LOGE("bundle encode fail");
922 ret = MESSAGE_PORT_ERROR_INVALID_PARAMETER;
926 if (data_len > MAX_MESSAGE_SIZE) {
927 _LOGE("bigger than max size\n");
928 ret = MESSAGE_PORT_ERROR_MAX_EXCEEDED;
932 if (g_list_length(port_info->delayed_message_list) > 0) {
933 ret = MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE;
934 _LOGE("There are messages in the delayed_message_list (count %d)",
935 g_list_length(port_info->delayed_message_list));
939 ret = write_string_to_socket(port_info->send_sock_fd, local_port,
940 strlen(local_port) + 1, &nb, &sequence);
941 if (ret != MESSAGE_PORT_ERROR_NONE) {
942 _LOGE("write local_port fail");
946 ret = write_socket(port_info->send_sock_fd, (char *)&is_bidirection,
947 sizeof(is_bidirection), &nb, &sequence);
948 if (ret != MESSAGE_PORT_ERROR_NONE) {
949 _LOGE("write is_bidirection fail");
953 ret = write_socket(port_info->send_sock_fd, (char *)&local_trusted,
954 sizeof(local_trusted), &nb, &sequence);
955 if (ret != MESSAGE_PORT_ERROR_NONE) {
956 _LOGE("write local_trusted fail");
960 ret = write_string_to_socket(port_info->send_sock_fd, (void *)kb_data,
961 data_len, &nb, &sequence);
962 if (ret != MESSAGE_PORT_ERROR_NONE) {
963 _LOGE("write kb_data fail");
968 /* LCOV_EXCL_START */
969 if (ret == MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE) {
970 ret = __push_delayed_message(port_info, sequence, kb_data, data_len, nb,
971 local_port, local_trusted, is_bidirection);
972 if (ret != MESSAGE_PORT_ERROR_NONE) {
975 return MESSAGE_PORT_ERROR_IO_ERROR;
978 if (__can_write(port_info->send_sock_fd)) {
979 while (g_list_length(port_info->delayed_message_list) != 0) {
980 ret = __pop_delayed_message(port_info);
981 if (ret != MESSAGE_PORT_ERROR_NONE) {
982 if (ret == MESSAGE_PORT_ERROR_RESOURCE_UNAVAILABLE) {
983 ret = MESSAGE_PORT_ERROR_NONE;
984 } else if (ret == MESSAGE_PORT_ERROR_IO_ERROR) {
985 g_source_remove(port_info->delay_src_id);
986 port_info->delay_src_id = 0;
1000 int send_message(const char *remote_appid, const char *remote_port,
1001 const char *local_port, bool trusted_message, bool local_trusted, bool bi_dir, bundle *message)
1004 int ret = MESSAGE_PORT_ERROR_NONE;
1005 GUnixFDList *fd_list = NULL;
1008 bundle_raw *raw = NULL;
1009 char *bus_name = NULL;
1010 char *interface_name = NULL;
1012 message_port_remote_app_info_s *remote_app_info = NULL;
1013 port_list_info_s *port_info = NULL;
1014 GDBusMessage *msg = NULL;
1016 GVariant *body = NULL;
1017 int sock_pair[2] = {0,};
1019 port_key_info_s *__key_info;
1021 if (!_initialized) {
1022 if (!__initialize())
1023 return MESSAGE_PORT_ERROR_IO_ERROR;
1026 ret = __get_remote_port_info(remote_appid, remote_port, trusted_message, &remote_app_info, &port_info);
1027 if (ret != MESSAGE_PORT_ERROR_NONE)
1030 if (port_info->exist == false) {
1032 _LOGD("port exist check !!");
1033 ret = check_remote_port(remote_appid, remote_port, trusted_message, &exist);
1034 if (ret != MESSAGE_PORT_ERROR_NONE) {
1036 } else if (!exist) {
1037 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1041 if (port_info->send_sock_fd > 0) {
1042 ret = __message_port_send_async(port_info, message,
1043 (local_port) ? local_port : "", local_trusted, bi_dir);
1046 bus_name = port_info->encoded_bus_name;
1047 interface_name = bus_name;
1049 if (bundle_encode(message, &raw, &len) != BUNDLE_ERROR_NONE) {
1050 ret = MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1054 if (MAX_MESSAGE_SIZE < len) {
1055 _LOGE("The size of message (%d) has exceeded the maximum limit.", len);
1056 ret = MESSAGE_PORT_ERROR_MAX_EXCEEDED;
1060 body = g_variant_new("(ssbbssbus)", app_id, (local_port) ? local_port : "", local_trusted, bi_dir,
1061 remote_appid, remote_port, trusted_message, len, raw);
1062 if (strcmp(remote_appid, app_id) != 0) { /* self send */
1064 /* if message-port fail to get socket pair, communicate using GDBus */
1065 if (aul_request_message_port_socket_pair(sock_pair) != AUL_R_OK) {
1066 _LOGE("error create socket pair");
1069 _LOGI("sock pair : %d, %d",
1070 sock_pair[SOCK_PAIR_SENDER], sock_pair[SOCK_PAIR_RECEIVER]);
1071 fd_list = g_unix_fd_list_new();
1072 g_unix_fd_list_append(fd_list, sock_pair[SOCK_PAIR_RECEIVER], &err);
1074 _LOGE("g_unix_fd_list_append [%s]", err->message);
1075 ret = MESSAGE_PORT_ERROR_IO_ERROR;
1080 port_info->send_sock_fd = sock_pair[SOCK_PAIR_SENDER];
1081 close(sock_pair[SOCK_PAIR_RECEIVER]);
1082 sock_pair[SOCK_PAIR_RECEIVER] = 0;
1084 port_info->gio_read = g_io_channel_unix_new(port_info->send_sock_fd);
1085 if (!port_info->gio_read) {
1086 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
1087 ret = MESSAGE_PORT_ERROR_IO_ERROR;
1091 ret = __create_port_key_info(port_info, &__key_info);
1092 if (ret != MESSAGE_PORT_ERROR_NONE) {
1093 _LOGE("out of memory");
1097 port_info->g_src_id = g_io_add_watch_full(
1098 port_info->gio_read,
1101 __socket_disconnect_handler,
1102 (gpointer)__key_info,
1103 __socket_destroy_handler);
1104 if (port_info->g_src_id == 0) {
1105 _LOGE("fail to add watch on socket");
1106 ret = MESSAGE_PORT_ERROR_IO_ERROR;
1107 __free_key_info(__key_info);
1114 msg = g_dbus_message_new_method_call(bus_name, MESSAGEPORT_OBJECT_PATH, interface_name, "send_message");
1116 _LOGE("Can't allocate new method call");
1117 ret = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1121 g_dbus_message_set_unix_fd_list(msg, fd_list);
1122 g_dbus_message_set_body(msg, body);
1123 g_dbus_connection_send_message(gdbus_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
1125 _LOGE("No reply. error = %s", err->message);
1127 ret = MESSAGE_PORT_ERROR_IO_ERROR;
1134 g_object_unref(msg);
1136 bundle_free_encoded_rawdata(&raw);
1138 g_object_unref(fd_list);
1140 if (ret != MESSAGE_PORT_ERROR_NONE
1141 && ret != MESSAGE_PORT_ERROR_MAX_EXCEEDED) {
1143 __create_port_key_info(port_info, &__key_info);
1145 if (__key_info != NULL) {
1146 __free_port_info_by_key(__key_info);
1147 __free_key_info(__key_info);
1150 if (sock_pair[SOCK_PAIR_SENDER])
1151 close(sock_pair[SOCK_PAIR_SENDER]);
1152 if (sock_pair[SOCK_PAIR_RECEIVER])
1153 close(sock_pair[SOCK_PAIR_RECEIVER]);
1159 int send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port, bool trusted_message, bundle *message)
1161 message_port_local_port_info_s *local_info;
1162 int ret = get_local_port_info(id, &local_info);
1163 if (ret != MESSAGE_PORT_ERROR_NONE)
1166 _LOGD("bidirectional_message %s", local_info->port_name);
1167 return send_message(remote_app_id, remote_port,
1168 local_info->port_name, trusted_message, local_info->is_trusted, true, message);
1171 static void __name_registered(GDBusConnection *connection,
1173 const gchar *name_owner,
1177 registered_callback_info_s *info = (registered_callback_info_s *)user_data;
1179 LOGE("NULL registered_callback_info");
1183 _LOGI("watcher_id : %d, appeared name : %s , name_owner : %s\n", info->watcher_id, name, name_owner);
1184 if (info->registered_cb)
1185 info->registered_cb(info->remote_app_id, info->remote_port, info->is_trusted, info->user_data);
1188 static void __name_unregistered(GDBusConnection *connection,
1193 registered_callback_info_s *info = (registered_callback_info_s *)user_data;
1195 LOGE("NULL registered_callback_info");
1199 _LOGI("watcher_id : %d, vanished name : %s\n", info->watcher_id, name);
1200 if (info->unregistered_cb)
1201 info->unregistered_cb(info->remote_app_id, info->remote_port, info->is_trusted, info->user_data);
1204 int watch_remote_port(int *watcher_id, const char *remote_app_id, const char *remote_port, bool trusted_remote_port, message_port_registration_event_cb registered_cb, message_port_registration_event_cb unregistered_cb, void *user_data)
1206 int ret_val = MESSAGE_PORT_ERROR_NONE;
1207 message_port_remote_app_info_s *remote_app_info = NULL;
1208 port_list_info_s *port_info = NULL;
1210 if (!_initialized) {
1211 if (!__initialize())
1212 return MESSAGE_PORT_ERROR_IO_ERROR;
1214 _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, app_id);
1216 ret_val = __get_remote_port_info(remote_app_id, remote_port, trusted_remote_port, &remote_app_info, &port_info);
1217 if (ret_val != MESSAGE_PORT_ERROR_NONE) {
1218 _LOGE("Failed to get remote_port_info %d", ret_val);
1222 registered_callback_info_s *registered_cb_info = (registered_callback_info_s *)calloc(1, sizeof(registered_callback_info_s));
1223 retvm_if(!registered_cb_info, MESSAGE_PORT_ERROR_OUT_OF_MEMORY, "Malloc failed");
1225 registered_cb_info->registered_cb = registered_cb;
1226 registered_cb_info->unregistered_cb = unregistered_cb;
1227 registered_cb_info->user_data = user_data;
1228 registered_cb_info->remote_app_id = strdup(remote_app_info->remote_app_id);
1229 if (registered_cb_info->remote_app_id == NULL) {
1230 free(registered_cb_info);
1231 _LOGE("Failed to alloc memory");
1232 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1234 registered_cb_info->remote_port = strdup(port_info->port_name);
1235 if (registered_cb_info->remote_port == NULL) {
1236 free(registered_cb_info->remote_app_id);
1237 free(registered_cb_info);
1238 _LOGE("Failed to alloc memory");
1239 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1242 registered_cb_info->watcher_id = g_bus_watch_name_on_connection(
1244 port_info->encoded_bus_name,
1245 G_BUS_NAME_WATCHER_FLAGS_NONE,
1247 __name_unregistered,
1250 if (registered_cb_info->watcher_id == 0) {
1251 /* LCOV_EXCL_START */
1252 free(registered_cb_info->remote_app_id);
1253 free(registered_cb_info->remote_port);
1254 free(registered_cb_info);
1255 _LOGE("Failed to watch name");
1256 return MESSAGE_PORT_ERROR_IO_ERROR;
1257 /* LCOV_EXCL_STOP */
1260 g_hash_table_insert(__registered_callback_info_hash,
1261 GINT_TO_POINTER(registered_cb_info->watcher_id), registered_cb_info);
1263 *watcher_id = registered_cb_info->watcher_id;
1264 return MESSAGE_PORT_ERROR_NONE;
1267 int remove_registration_event_cb(int watcher_id)
1269 registered_callback_info_s *registered_cb_info = NULL;
1270 gboolean remove_result = FALSE;
1273 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1275 registered_cb_info = g_hash_table_lookup(__registered_callback_info_hash, GINT_TO_POINTER(watcher_id));
1276 if (registered_cb_info == NULL)
1277 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1279 _LOGI("unwatch_remote_port [%s : %s : %d] ", registered_cb_info->remote_app_id,
1280 registered_cb_info->remote_port, watcher_id);
1282 remove_result = g_hash_table_remove(__registered_callback_info_hash, GINT_TO_POINTER(watcher_id));
1284 return MESSAGE_PORT_ERROR_IO_ERROR;
1286 g_bus_unwatch_name(watcher_id);
1288 return MESSAGE_PORT_ERROR_NONE;