Release version 1.4.25
[platform/core/appfw/message-port.git] / src / message_port_remote.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #define _GNU_SOURCE
18
19 #include <bundle.h>
20 #include <bundle_internal.h>
21 #include <aul.h>
22
23 #include <sys/socket.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26
27 #include <glib.h>
28 #include <gio/gio.h>
29 #include <gio/gunixfdlist.h>
30 #include <glib-unix.h>
31
32 #include "message_port_log.h"
33 #include "message_port_common.h"
34 #include "message_port_remote.h"
35
36
37 #define MAX_PACKAGE_STR_SIZE 512
38
39 #define DBUS_RELEASE_NAME_REPLY_RELEASED        1 /* *< Service was released from the given name */
40 #define DBUS_RELEASE_NAME_REPLY_NON_EXISTENT    2 /* *< The given name does not exist on the bus */
41 #define DBUS_RELEASE_NAME_REPLY_NOT_OWNER       3 /* *< Service is not an owner of the given name */
42
43 #define MAX_RETRY_CNT 10
44 #define SOCK_PAIR_SENDER 0
45 #define SOCK_PAIR_RECEIVER 1
46
47 static bool _initialized = false;
48 static GHashTable *__local_port_info;
49 static GHashTable *__trusted_app_list_hash;
50 static GList *__callback_info_list;
51 static GHashTable *__sender_appid_hash;
52
53 typedef struct message_port_pkt {
54         int remote_port_name_len;
55         char *remote_port_name;
56         bool is_bidirection;
57         bool is_trusted;
58         int data_len;
59         unsigned char *data;
60 } message_port_pkt_s;
61
62 typedef struct message_port_callback_info {
63         message_port_local_port_info_s *local_info;
64         int local_id;
65         char *remote_app_id;
66         GIOChannel *gio_read;
67         int g_src_id;
68 } message_port_callback_info_s;
69
70 static void __callback_info_free(gpointer data)
71 {
72         message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
73         GError *error = NULL;
74         if (callback_info == NULL)
75                 return;
76
77         if (callback_info->remote_app_id)
78                 FREE_AND_NULL(callback_info->remote_app_id);
79
80         if (callback_info->local_info) {
81                 if (callback_info->local_info->port_name)
82                                 FREE_AND_NULL(callback_info->local_info->port_name);
83
84                 FREE_AND_NULL(callback_info->local_info);
85         }
86
87         if (callback_info->gio_read != NULL) {
88                 g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
89                 if (error) {
90                         _LOGE("g_io_channel_shutdown error : %s", error->message);
91                         g_error_free(error);
92                 }
93                 g_io_channel_unref(callback_info->gio_read);
94                 callback_info->gio_read = NULL;
95         }
96
97         if (callback_info->g_src_id != 0) {
98                 g_source_remove(callback_info->g_src_id);
99                 callback_info->g_src_id = 0;
100         }
101
102         FREE_AND_NULL(callback_info);
103 }
104
105 /* LCOV_EXCL_START */
106 static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
107 {
108         GList *find_list;
109
110         find_list = g_list_find(__callback_info_list, callback_info);
111         if (find_list == NULL)
112                 return;
113
114         __callback_info_list = g_list_remove_link(__callback_info_list, find_list);
115         __callback_info_free(callback_info);
116         g_list_free(find_list);
117 }
118
119 static void __callback_info_free_by_local_id(int local_id)
120 {
121         GList *iter;
122         GList *callback_info;
123
124         for (iter = __callback_info_list; iter != NULL; ) {
125                 if (((message_port_callback_info_s *)iter->data)->local_id == local_id) {
126                         callback_info = iter;
127                         iter = iter->next;
128                         __callback_info_list =
129                                         g_list_remove_link(__callback_info_list, callback_info);
130                         __callback_info_free(callback_info->data);
131                         g_list_free(callback_info);
132                         continue;
133                 }
134                 iter = iter->next;
135         }
136 }
137
138 /* LCOV_EXCL_START */
139 static void __hash_destory_local_value(gpointer data)
140 {
141         message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
142         if (mli) {
143                 if (mli->port_name)
144                         free(mli->port_name);
145                 free(mli);
146         }
147 }
148 /* LCOV_EXCL_STOP */
149
150 static bool __initialize(void)
151 {
152         if (!initialized_common) {
153                 if (!initialize_common())
154                         return false;
155         }
156
157         if (__local_port_info == NULL) {
158                 __local_port_info = g_hash_table_new_full(g_direct_hash,  g_direct_equal, NULL, __hash_destory_local_value);
159                 retvm_if(!__local_port_info, false, "fail to create __local_port_info");
160         }
161
162         if (__sender_appid_hash == NULL) {
163                 __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
164                 retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
165         }
166
167         if (__trusted_app_list_hash == NULL) {
168                 __trusted_app_list_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
169                 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
170         }
171
172         _initialized = true;
173
174         return true;
175 }
176
177 bool is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi)
178 {
179         GHashTableIter iter;
180         gpointer key, value;
181
182         if (__local_port_info == NULL) {
183                 _LOGI("There is no registed local port");
184                 return false;
185         }
186
187         g_hash_table_iter_init(&iter, __local_port_info);
188         while (g_hash_table_iter_next(&iter, &key, &value)) {
189                 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)value;
190
191                 if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
192                         *local_id = mi->local_id;
193                         if (lpi != NULL)
194                                 *lpi = mi;
195                         return true;
196                 }
197         }
198         return false;
199 }
200
201 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
202 {
203         GDBusMessage *msg = NULL;
204         GDBusMessage *reply = NULL;
205         GError *err = NULL;
206         GVariant *body;
207         int pid = 0;
208
209         msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
210                         "org.freedesktop.DBus", "GetConnectionUnixProcessID");
211         if (!msg) {
212                 _LOGE("Can't allocate new method call");
213                 goto out;
214         }
215
216         g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
217         reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
218                                                         G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
219
220         if (!reply) {
221                 if (err != NULL) {
222                         _LOGE("Failed to get pid [%s]", err->message);
223                         g_error_free(err);
224                 } else {
225                         _LOGE("Failed to get pid");
226                 }
227                 goto out;
228         }
229
230         body = g_dbus_message_get_body(reply);
231         g_variant_get(body, "(u)", &pid);
232
233 out:
234         if (msg)
235                 g_object_unref(msg);
236         if (reply)
237                 g_object_unref(reply);
238
239         return pid;
240 }
241
242 static message_port_pkt_s *__message_port_recv_raw(int fd)
243 {
244         message_port_pkt_s *pkt = NULL;
245         unsigned int nb;
246
247         pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
248         if (pkt == NULL) {
249 /* LCOV_EXCL_START */
250                 close(fd);
251                 return NULL;
252 /* LCOV_EXCL_STOP */
253         }
254
255         if (read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGE_PORT_ERROR_NONE) {
256 /* LCOV_EXCL_START */
257                 LOGE("read socket fail: port_name");
258                 free(pkt->remote_port_name);
259                 free(pkt);
260                 return NULL;
261 /* LCOV_EXCL_STOP */
262         }
263
264         if (read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGE_PORT_ERROR_NONE) {
265 /* LCOV_EXCL_START */
266                 LOGE("read socket fail: is_bidirection");
267                 free(pkt->remote_port_name);
268                 free(pkt);
269                 return NULL;
270 /* LCOV_EXCL_STOP */
271         }
272
273         if (read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGE_PORT_ERROR_NONE) {
274 /* LCOV_EXCL_START */
275                 LOGE("read socket fail: is_trusted");
276                 free(pkt->remote_port_name);
277                 free(pkt);
278                 return NULL;
279 /* LCOV_EXCL_STOP */
280         }
281
282         if (read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGE_PORT_ERROR_NONE) {
283 /* LCOV_EXCL_START */
284                 LOGE("read socket fail: data");
285                 if (pkt->data)
286                         free(pkt->data);
287                 free(pkt->remote_port_name);
288                 free(pkt);
289                 return NULL;
290 /* LCOV_EXCL_STOP */
291         }
292
293         return pkt;
294 }
295
296 static bool __validate_callback_info(message_port_callback_info_s *callback_info)
297 {
298         GList *cb_list;
299
300         cb_list = g_list_find(__callback_info_list, callback_info);
301         if (cb_list == NULL) {
302                 _LOGI("local_info : %p is already released from list", callback_info);
303                 return false;
304         }
305
306         return true;
307 }
308
309 static bool __validate_local_info(int local_id)
310 {
311         GList *cb_list;
312
313         cb_list = g_hash_table_lookup(__local_port_info, GUINT_TO_POINTER(local_id));
314         if (cb_list == NULL) {
315                 _LOGI("local_info : %d is already released", local_id);
316                 return false;
317         }
318
319         return true;
320 }
321
322 static gboolean __socket_request_handler(GIOChannel *gio,
323                 GIOCondition cond,
324                 gpointer data)
325 {
326         int fd = 0;
327         message_port_callback_info_s *callback_info;
328         message_port_pkt_s *pkt = NULL;
329         message_port_local_port_info_s *local_port_info;
330         bundle *kb = NULL;
331         bool ret = true;
332         bool existed = true;
333
334         callback_info = (message_port_callback_info_s *)data;
335         if (callback_info == NULL)
336                 return FALSE;
337
338         message_port_lock_mutex();
339         if (__validate_callback_info(callback_info) == false) {
340                 ret = FALSE;
341                 existed = FALSE;
342                 goto out;
343         }
344
345         if (__validate_local_info(callback_info->local_id) == false) {
346                 ret = FALSE;
347                 goto out;
348         }
349
350         local_port_info = callback_info->local_info;
351         if (local_port_info == NULL || local_port_info->callback == NULL) {
352                 _LOGE("Failed to get callback info");
353                 ret = FALSE;
354                 goto out;
355         }
356
357         if (cond == G_IO_HUP) {
358                 _LOGI("socket G_IO_HUP");
359                 ret = FALSE;
360                 goto out;
361         }
362
363         fd = g_io_channel_unix_get_fd(gio);
364         if (fd < 0) {
365                 _LOGE("fail to get fd from io channel");
366                 ret = FALSE;
367                 goto out;
368         }
369
370         pkt = __message_port_recv_raw(fd);
371         if (pkt == NULL) {
372                 _LOGE("recv error on SOCKET");
373                 ret = FALSE;
374                 goto out;
375         }
376
377         kb = bundle_decode(pkt->data, pkt->data_len);
378         if (!kb) {
379                 _LOGE("Invalid argument : message");
380                 ret = FALSE;
381                 goto out;
382         }
383
384         if (pkt->is_bidirection)
385                 local_port_info->callback(callback_info->local_id,
386                                 callback_info->remote_app_id, pkt->remote_port_name,
387                                 pkt->is_trusted, kb, local_port_info->user_data);
388         else
389                 local_port_info->callback(callback_info->local_id,
390                                 callback_info->remote_app_id, NULL,
391                                 pkt->is_trusted, kb, local_port_info->user_data);
392
393         bundle_free(kb);
394
395 out:
396         if (pkt) {
397                 if (pkt->remote_port_name)
398                         free(pkt->remote_port_name);
399                 if (pkt->data)
400                         free(pkt->data);
401                 free(pkt);
402         }
403
404         if (ret == FALSE && existed == TRUE)
405                 __callback_info_free_by_info(callback_info);
406
407         message_port_unlock_mutex();
408
409         return ret;
410 }
411
412 static message_port_callback_info_s *__create_callback_info(message_port_local_port_info_s *mi, char *local_appid)
413 {
414         message_port_local_port_info_s *local_info = NULL;
415         message_port_callback_info_s *callback_info = NULL;
416         bool ret = true;
417
418         callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
419         if (callback_info == NULL) {
420 /* LCOV_EXCL_START */
421                 _LOGE("out of memory");
422                 return NULL;
423 /* LCOV_EXCL_STOP */
424         }
425
426         local_info = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
427         if (local_info == NULL) {
428 /* LCOV_EXCL_START */
429                 ret = false;
430                 _LOGE("out of memory");
431                 goto out;
432 /* LCOV_EXCL_STOP */
433         }
434
435         callback_info->local_id = mi->local_id;
436         callback_info->local_info = local_info;
437         callback_info->remote_app_id = strdup(local_appid);
438         if (callback_info->remote_app_id == NULL) {
439 /* LCOV_EXCL_START */
440                 ret = false;
441                 _LOGE("out of memory");
442                 goto out;
443 /* LCOV_EXCL_STOP */
444         }
445
446         local_info->port_name = strdup(mi->port_name);
447         if (local_info->port_name == NULL) {
448 /* LCOV_EXCL_START */
449                 ret = false;
450                 _LOGE("out of memory");
451                 goto out;
452 /* LCOV_EXCL_STOP */
453         }
454
455         local_info->callback = mi->callback;
456         local_info->is_trusted = mi->is_trusted;
457         local_info->local_id = mi->local_id;
458         local_info->user_data = mi->user_data;
459
460 out:
461         if (ret == false) {
462                 __callback_info_free(callback_info);
463                 return NULL;
464         }
465
466         return callback_info;
467 }
468
469 static void __callback_info_append(message_port_callback_info_s *callback_info)
470 {
471         __callback_info_list = g_list_append(__callback_info_list, callback_info);
472 }
473
474 static void __callback_info_update_user_data(int local_id, message_port_message_cb callback, void *user_data)
475 {
476         GList *iter;
477         message_port_callback_info_s *callback_info;
478
479 /* LCOV_EXCL_START */
480         for (iter = __callback_info_list; iter != NULL; iter = iter->next) {
481                 callback_info = (message_port_callback_info_s *)iter->data;
482                 if (callback_info->local_id == local_id
483                                 && callback_info->local_info != NULL) {
484                         callback_info->local_info->callback = callback;
485                         callback_info->local_info->user_data = user_data;
486                 }
487         }
488 /* LCOV_EXCL_STOP */
489 }
490
491 static bool __receive_message(GVariant *parameters, GDBusMethodInvocation *invocation)
492 {
493         char *local_port = NULL;
494         char *local_appid = NULL;
495         char *remote_appid = NULL;
496         char *remote_port = NULL;
497         gboolean local_trusted = false;
498         gboolean remote_trusted = false;
499         gboolean bi_dir = false;
500         int len = 0;
501
502         bundle *data = NULL;
503         bundle_raw *raw = NULL;
504         message_port_local_port_info_s *mi;
505         int local_reg_id = 0;
506         message_port_callback_info_s *callback_info = NULL;
507
508         char buf[1024];
509         GDBusMessage *msg;
510         GUnixFDList *fd_list;
511         int fd_len;
512         int *returned_fds = NULL;
513         int fd;
514         bool ret = false;
515         char *key_appid;
516
517         message_port_lock_mutex();
518         g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
519                         &remote_appid, &remote_port, &remote_trusted, &len, &raw);
520
521         if (!remote_port) {
522                 _LOGE("Invalid argument : remote_port is NULL");
523                 goto out;
524         }
525         if (!remote_appid) {
526                 _LOGE("Invalid argument : remote_appid is NULL");
527                 goto out;
528         }
529
530         if (!local_appid) {
531                 _LOGE("Invalid argument : local_appid");
532                 goto out;
533         }
534
535         if (!is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
536                 _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
537                 goto out;
538         }
539
540         callback_info = __create_callback_info(mi, local_appid);
541         if (callback_info == NULL) {
542                 goto out;
543         }
544
545         if (!local_port) {
546                 _LOGE("Invalid argument : local_port");
547                 goto out;
548         }
549         if (strcmp(remote_appid, app_id) != 0) {
550                 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
551                 goto out;
552         }
553         if (strcmp(remote_port, callback_info->local_info->port_name) != 0) {
554                 _LOGE("Invalid argument : remote_port (%s)", remote_port);
555                 goto out;
556         }
557         if (!len) {
558                 _LOGE("Invalid argument : data_len");
559                 goto out;
560         }
561         if (remote_trusted) {
562 /* LCOV_EXCL_START */
563                 if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
564                         if (!is_preloaded(local_appid, remote_appid)) {
565                                 int ret = check_certificate(local_appid, remote_appid);
566                                 if (ret == MESSAGE_PORT_ERROR_NONE) {
567                                         key_appid = strdup(local_appid);
568                                         if (key_appid)
569                                                 g_hash_table_insert(__trusted_app_list_hash, key_appid, "TRUE");
570                                 } else {
571                                         _LOGE("The application (%s) is not signed with the same certificate",
572                                                         local_appid);
573                                         goto out;
574                                 }
575                         }
576                 }
577 /* LCOV_EXCL_STOP */
578         }
579
580         data = bundle_decode(raw, len);
581         if (!data) {
582                 _LOGE("Invalid argument : message");
583                 goto out;
584         }
585
586         LOGD("call calback %s", local_appid);
587         if (bi_dir)
588                 callback_info->local_info->callback(callback_info->local_info->local_id,
589                         local_appid, local_port, local_trusted, data, callback_info->local_info->user_data);
590         else
591                 callback_info->local_info->callback(callback_info->local_info->local_id,
592                         local_appid, NULL, false, data, callback_info->local_info->user_data);
593         bundle_free(data);
594
595         ret = true;
596
597         msg = g_dbus_method_invocation_get_message(invocation);
598         fd_list = g_dbus_message_get_unix_fd_list(msg);
599
600         /* When application send message to self fd_list is NULL */
601         if (fd_list != NULL) {
602                 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
603                 if (returned_fds == NULL) {
604                         _LOGE("fail to get fds");
605                         ret = false;
606                         goto out;
607                 }
608                 fd = returned_fds[0];
609
610                 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
611                 if (fd > 0) {
612                         callback_info->gio_read = g_io_channel_unix_new(fd);
613                         if (!callback_info->gio_read) {
614                                 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
615                                 ret = false;
616                                 goto out;
617                         }
618
619                         callback_info->g_src_id = g_io_add_watch_full(
620                                         callback_info->gio_read,
621                                         G_PRIORITY_DEFAULT,
622                                         G_IO_IN | G_IO_HUP,
623                                         __socket_request_handler, callback_info,
624                                         NULL);
625                         if (callback_info->g_src_id == 0) {
626                                 _LOGE("fail to add watch on socket");
627                                 ret = false;
628                                 goto out;
629                         }
630                         __callback_info_append(callback_info);
631                 }
632         }
633
634 out:
635         message_port_unlock_mutex();
636         if (ret == false)
637                 __callback_info_free(callback_info);
638
639         if (returned_fds)
640                 g_free(returned_fds);
641
642         return ret;
643 }
644
645 static void __on_sender_name_appeared(GDBusConnection *connection,
646                 const gchar     *name,
647                 const gchar     *name_owner,
648                 gpointer         user_data)
649 {
650         _LOGI("sender name appeared : %s", name);
651 }
652
653 static void __on_sender_name_vanished(GDBusConnection *connection,
654                 const gchar     *name,
655                 gpointer         user_data)
656 {
657         int *watcher_id = (int *)user_data;
658         char *local_appid;
659
660         local_appid = (char *)g_hash_table_lookup(__sender_appid_hash, name);
661         if (local_appid) {
662                 g_hash_table_remove(__trusted_app_list_hash, (gpointer)local_appid);
663                 g_hash_table_remove(__sender_appid_hash, (gpointer)name);
664         }
665
666         if (watcher_id) {
667                 if (*watcher_id > 0)
668                         g_bus_unwatch_name(*watcher_id);
669                 else
670                         LOGE("Invalid watcher_id %d", *watcher_id);
671                 free(watcher_id);
672         } else {
673                 LOGE("watcher_id is NULL");
674         }
675 }
676
677 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
678 {
679         int ret = 0;
680         char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
681         char *local_appid = NULL;
682         int pid = __get_sender_pid(conn, sender);
683         int *watcher_id = (int *)calloc(1, sizeof(int));
684         char *_sender;
685         char *_appid;
686         retvm_if(!watcher_id, false, "Malloc failed");
687
688         ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
689         if (ret != AUL_R_OK) {
690                 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
691                 free(watcher_id);
692                 return false;
693         }
694
695         g_variant_get_child(parameters, 0, "&s", &local_appid);
696         if (local_appid == NULL) {
697                 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
698                 free(watcher_id);
699                 return false;
700         }
701
702         if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
703                 _LOGD("insert sender !!!!! %s", sender);
704                 _sender = strdup(sender);
705                 if (_sender == NULL) {
706 /* LCOV_EXCL_START */
707                         _LOGE("out of memory");
708                         free(watcher_id);
709                         return false;
710 /* LCOV_EXCL_STOP */
711                 }
712
713                 _appid = strdup(local_appid);
714                 if (_appid == NULL) {
715 /* LCOV_EXCL_START */
716                         _LOGE("out of memory");
717                         free(watcher_id);
718                         free(_sender);
719                         return false;
720 /* LCOV_EXCL_STOP */
721                 }
722
723                 g_hash_table_insert(__sender_appid_hash, (gpointer)_sender, (gpointer)_appid);
724                 *watcher_id = g_bus_watch_name_on_connection(
725                                         gdbus_conn,
726                                         sender,
727                                         G_BUS_NAME_WATCHER_FLAGS_NONE,
728                                         __on_sender_name_appeared,
729                                         __on_sender_name_vanished,
730                                         watcher_id,
731                                         NULL);
732         } else {
733                 free(watcher_id);
734                 return false;
735         }
736         return true;
737 }
738 /* LCOV_EXCL_START */
739 static void __close_socket(GDBusMethodInvocation *invocation)
740 {
741         GDBusMessage *msg;
742         GUnixFDList *fd_list;
743         int fd_len;
744         int *returned_fds = NULL;
745
746         msg = g_dbus_method_invocation_get_message(invocation);
747         fd_list = g_dbus_message_get_unix_fd_list(msg);
748         if (fd_list != NULL) {
749                 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
750                 if (returned_fds != NULL) {
751                         close(returned_fds[0]);
752                         g_free(returned_fds);
753                 }
754         }
755 }
756 /* LCOV_EXCL_STOP */
757 static void __dbus_method_call_handler(GDBusConnection *conn,
758                                 const gchar *sender, const gchar *object_path,
759                                 const gchar *iface_name, const gchar *method_name,
760                                 GVariant *parameters, GDBusMethodInvocation *invocation,
761                                 gpointer user_data)
762 {
763         _LOGI("method_name: %s, sender: %s", method_name, sender);
764         gpointer sender_appid = g_hash_table_lookup(__sender_appid_hash, sender);
765         if (sender_appid == NULL) {
766                 if (!__check_sender_validation(parameters, sender, conn)) {
767                         _LOGE("Failed to validate");
768                         __close_socket(invocation);
769                         goto out;
770                 }
771         }
772
773         if (g_strcmp0(method_name, "send_message") == 0)
774                 __receive_message(parameters, invocation);
775 out:
776         g_dbus_method_invocation_return_value(invocation, NULL);
777 }
778
779 static const GDBusInterfaceVTable interface_vtable = {
780         __dbus_method_call_handler,
781         NULL,
782         NULL
783 };
784
785 static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data)
786 {
787         message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
788         retvm_if(!mi, false, "Malloc failed");
789
790         mi->callback = callback;
791         mi->is_trusted = is_trusted;
792         mi->port_name = strdup(local_port);
793         if (mi->port_name == NULL) {
794 /* LCOV_EXCL_START */
795                 _LOGE("Malloc failed (%s)", local_port);
796                 free(mi);
797                 return false;
798 /* LCOV_EXCL_STOP */
799         }
800         mi->local_id = local_id;
801         mi->user_data = user_data;
802
803         g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
804         return true;
805 }
806
807 static int __register_dbus_interface(const char *port_name,
808                 bool is_trusted, message_port_message_cb callback, void *user_data)
809 {
810         GDBusNodeInfo *introspection_data = NULL;
811         int registration_id = 0;
812         int ret = MESSAGE_PORT_ERROR_NONE;
813
814         static gchar introspection_prefix[] =
815                 "<node>"
816                 "  <interface name='";
817
818         static gchar introspection_postfix[] =
819                 "'>"
820                 "        <method name='send_message'>"
821                 "          <arg type='s' name='local_appid' direction='in'/>"
822                 "          <arg type='s' name='local_port' direction='in'/>"
823                 "          <arg type='b' name='local_trusted' direction='in'/>"
824                 "          <arg type='b' name='bi_dir' direction='in'/>"
825                 "          <arg type='s' name='remote_appid' direction='in'/>"
826                 "          <arg type='s' name='remote_port' direction='in'/>"
827                 "          <arg type='b' name='remote_trusted' direction='in'/>"
828                 "          <arg type='u' name='data_len' direction='in'/>"
829                 "          <arg type='s' name='data' direction='in'/>"
830                 "        </method>"
831                 "  </interface>"
832                 "</node>";
833
834         char *introspection_xml = NULL;
835         int introspection_xml_len = 0;
836
837         int owner_id = 0;
838         GError *error = NULL;
839         char *bus_name = NULL;
840         char *interface_name = NULL;
841         GVariant *result = NULL;
842
843         bus_name = get_encoded_name(app_id, port_name, is_trusted);
844         if (!bus_name) {
845                 _LOGE("Fail to get bus name");
846                 goto out;
847         }
848         interface_name = bus_name;
849
850         introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
851                 strlen(introspection_postfix) + 1;
852
853         introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
854         if (!introspection_xml) {
855 /* LCOV_EXCL_START */
856                 _LOGE("out of memory");
857                 goto out;
858 /* LCOV_EXCL_STOP */
859         }
860
861         snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
862
863         introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
864         if (!introspection_data) {
865                 ret = MESSAGE_PORT_ERROR_IO_ERROR;
866                 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
867                 goto out;
868         }
869
870         registration_id = g_dbus_connection_register_object(gdbus_conn,
871                                                 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
872                                                 &interface_vtable, NULL, NULL, NULL);
873
874         _LOGD("registration_id %d", registration_id);
875
876         if (registration_id == 0) {
877                 ret = MESSAGE_PORT_ERROR_IO_ERROR;
878                 _LOGE("Failed to g_dbus_connection_register_object");
879                 goto out;
880         }
881
882         if (!__message_port_register_port(registration_id, port_name, is_trusted,
883                                 callback, user_data)) {
884                 ret = MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
885                 goto out;
886         }
887
888         result = g_dbus_connection_call_sync(
889                         gdbus_conn,
890                         DBUS_SERVICE_DBUS,
891                         DBUS_PATH_DBUS,
892                         DBUS_INTERFACE_DBUS,
893                         "RequestName",
894                         g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
895                         G_VARIANT_TYPE("(u)"),
896                         G_DBUS_CALL_FLAGS_NONE,
897                         -1,
898                         NULL,
899                         &error);
900         if (error) {
901                 _LOGE("RequestName fail : %s", error->message);
902                 ret = MESSAGE_PORT_ERROR_IO_ERROR;
903                 g_error_free(error);
904                 goto out;
905         }
906         if (result == NULL) {
907                 ret = MESSAGE_PORT_ERROR_IO_ERROR;
908                 _LOGE("fail to get name NULL");
909                 goto out;
910         }
911         g_variant_get(result, "(u)", &owner_id);
912         if (owner_id == 0) {
913                 ret = MESSAGE_PORT_ERROR_IO_ERROR;
914                 _LOGE("Acquiring the own name is failed");
915                 goto out;
916         }
917
918         _LOGI("Acquiring the own name : %d", owner_id);
919
920 out:
921         if (introspection_data)
922                 g_dbus_node_info_unref(introspection_data);
923         if (introspection_xml)
924                 free(introspection_xml);
925         if (bus_name)
926                 free(bus_name);
927         if (result)
928                 g_variant_unref(result);
929
930         if (ret != MESSAGE_PORT_ERROR_NONE) {
931                 if (registration_id != 0)
932                         g_hash_table_remove(__local_port_info, GINT_TO_POINTER(registration_id));
933                 registration_id = ret;
934         }
935
936         return registration_id;
937 }
938
939 int get_local_port_info(int id, message_port_local_port_info_s **info)
940 {
941         message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
942
943         if (mi == NULL)
944                 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
945         *info = mi;
946
947         return MESSAGE_PORT_ERROR_NONE;
948 }
949
950 int register_message_port(const char *local_port, bool is_trusted, message_port_message_cb callback, void *user_data)
951 {
952         _SECURE_LOGI("local_port : [%s:%s]", local_port, is_trusted ? "trusted" : "non-trusted");
953
954         int local_id = 0;
955         message_port_local_port_info_s *port_info;
956         if (!_initialized) {
957                 if (!__initialize())
958                         return MESSAGE_PORT_ERROR_IO_ERROR;
959         }
960
961         /* Check the message port is already registed */
962         if (is_local_port_registed(local_port, is_trusted, &local_id, &port_info)) {
963                 port_info->callback = callback;
964                 port_info->user_data = user_data;
965                 __callback_info_update_user_data(local_id, callback, user_data);
966                 return local_id;
967         }
968
969         local_id = __register_dbus_interface(local_port, is_trusted, callback, user_data);
970         if (local_id < 1) {
971                 _LOGE("register_dbus_interface fail !!");
972         }
973         return local_id;
974 }
975
976 int unregister_local_port(int local_port_id, bool trusted_port)
977 {
978
979         GVariant *result;
980         char *bus_name = NULL;
981         GError *err = NULL;
982         int ret = 0;
983         message_port_local_port_info_s *mi;
984
985         _LOGI("unregister : %d", local_port_id);
986
987         if (!_initialized) {
988                 if (!__initialize())
989                         return MESSAGE_PORT_ERROR_IO_ERROR;
990         }
991
992         mi = (message_port_local_port_info_s *)
993                 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
994         if (mi == NULL)
995                 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
996
997         if (mi->is_trusted != trusted_port)
998                 return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
999
1000         __callback_info_free_by_local_id(local_port_id);
1001
1002         bus_name = get_encoded_name(app_id, mi->port_name, mi->is_trusted);
1003         if (bus_name == NULL)
1004                 return MESSAGE_PORT_ERROR_OUT_OF_MEMORY;
1005
1006         g_dbus_connection_unregister_object(gdbus_conn, local_port_id);
1007
1008         result = g_dbus_connection_call_sync(
1009                         gdbus_conn,
1010                         DBUS_SERVICE_DBUS,
1011                         DBUS_PATH_DBUS,
1012                         DBUS_INTERFACE_DBUS,
1013                         "ReleaseName",
1014                         g_variant_new("(s)", bus_name),
1015                         G_VARIANT_TYPE("(u)"),
1016                         G_DBUS_CALL_FLAGS_NONE,
1017                         -1,
1018                         NULL,
1019                         &err);
1020
1021         if (bus_name)
1022                 free(bus_name);
1023
1024         if (err) {
1025                 _LOGE("RequestName fail : %s", err->message);
1026                 g_error_free(err);
1027                 return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1028         }
1029         g_variant_get(result, "(u)", &ret);
1030
1031         if (result)
1032                 g_variant_unref(result);
1033 /* LCOV_EXCL_START */
1034         if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
1035                 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
1036                         _LOGE("Port Not exist");
1037                         return MESSAGE_PORT_ERROR_PORT_NOT_FOUND;
1038                 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
1039                         _LOGE("Try to release not owned name. MESSAGE_PORT_ERROR_INVALID_PARAMETER");
1040                         return MESSAGE_PORT_ERROR_INVALID_PARAMETER;
1041                 }
1042         }
1043 /* LCOV_EXCL_STOP */
1044         g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
1045
1046         return MESSAGE_PORT_ERROR_NONE;
1047 }