Fix memory leak
[platform/core/appfw/message-port.git] / src / message-port.c
1 /*
2  * Message Port
3  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /**
19  * @file        message-port.cpp
20  * @brief       This is the implementation file for the MessagePort.
21  */
22 #define _GNU_SOURCE
23
24 #include <sys/socket.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <glib.h>
28 #include <gio/gio.h>
29 #include <aul/aul.h>
30 #include <openssl/md5.h>
31 #include <bundle.h>
32 #include <bundle_internal.h>
33 #include <pkgmgr-info.h>
34 #include <aul.h>
35 #include <gio/gio.h>
36 #include <gio/gunixfdlist.h>
37
38 #include "message-port.h"
39 #include "message-port-log.h"
40
41 #define MAX_PACKAGE_STR_SIZE 512
42 #define MESSAGEPORT_BUS_NAME_PREFIX "org.tizen.messageport._"
43 #define MESSAGEPORT_OBJECT_PATH "/org/tizen/messageport"
44 #define MESSAGEPORT_INTERFACE_PREFIX "org.tizen.messageport._"
45
46 #define DBUS_SERVICE_DBUS "org.freedesktop.DBus"
47 #define DBUS_PATH_DBUS "/org/freedesktop/DBus"
48 #define DBUS_INTERFACE_DBUS "org.freedesktop.DBus"
49
50 #define DBUS_RELEASE_NAME_REPLY_RELEASED        1 /* *< Service was released from the given name */
51 #define DBUS_RELEASE_NAME_REPLY_NON_EXISTENT    2 /* *< The given name does not exist on the bus */
52 #define DBUS_RELEASE_NAME_REPLY_NOT_OWNER       3 /* *< Service is not an owner of the given name */
53 #define HEADER_LEN 8
54 #define MAX_RETRY_CNT 10
55 #define SOCK_PAIR_SENDER 0
56 #define SOCK_PAIR_RECEIVER 1
57
58
59 #define retvm_if(expr, val, fmt, arg...) do { \
60         if (expr) { \
61                 _LOGE(fmt, ##arg); \
62                 _LOGE("(%s) -> %s() return", #expr, __func__); \
63                 return val; \
64         } \
65 } while (0)
66
67 #define retv_if(expr, val) do { \
68         if (expr) { \
69                 _LOGE("(%s) -> %s() return", #expr, __func__); \
70                 return val; \
71         } \
72 } while (0)
73
74 #define FREE_AND_NULL(ptr) do { \
75         if (ptr) { \
76                 free((void *)ptr); \
77                 ptr = NULL; \
78         } \
79 } while (0)
80
81 static bool _initialized = false;
82 static GDBusConnection *__gdbus_conn;
83 static char *__app_id;
84 static GHashTable *__local_port_info;
85 static GHashTable *__remote_app_info;
86 static GHashTable *__sender_appid_hash;
87 static GHashTable *__trusted_app_list_hash;
88 static GHashTable *__callback_info_hash;
89 static const int MAX_MESSAGE_SIZE = 16 * 1024;
90
91 enum __certificate_info_type {
92         UNKNOWN = 0,
93         CERTIFICATE_MATCH,
94         CERTIFICATE_NOT_MATCH,
95 };
96
97 typedef struct message_port_pkt {
98         int remote_port_name_len;
99         char *remote_port_name;
100         bool is_bidirection;
101         bool is_trusted;
102         int data_len;
103         unsigned char *data;
104 } message_port_pkt_s;
105
106 typedef struct message_port_callback_info {
107         messageport_message_cb callback;
108         int local_id;
109         char *remote_app_id;
110         GIOChannel *gio_read;
111         int g_src_id;
112 } message_port_callback_info_s;
113
114 typedef struct message_port_local_port_info {
115         messageport_message_cb callback;
116         bool is_trusted;
117         char *port_name;
118         int local_id;
119 } message_port_local_port_info_s;
120
121 typedef struct message_port_remote_port_info {
122         char *sender_id;
123         char *remote_app_id;
124         int certificate_info;
125         GList *port_list;
126 } message_port_remote_app_info_s;
127
128 typedef struct port_list_info {
129         char *port_name;
130         char *encoded_bus_name;
131         bool is_trusted;
132         int send_sock_fd;
133         int watcher_id;
134         bool exist;
135         GIOChannel *gio_read;
136         int g_src_id;
137 } port_list_info_s;
138
139 static void __callback_info_free(gpointer data)
140 {
141         message_port_callback_info_s *callback_info = (message_port_callback_info_s *)data;
142         GError *error = NULL;
143         if (callback_info == NULL)
144                 return;
145
146         if (callback_info->remote_app_id)
147                 FREE_AND_NULL(callback_info->remote_app_id);
148
149         if (callback_info->gio_read != NULL) {
150                 g_io_channel_shutdown(callback_info->gio_read, TRUE, &error);
151                 if (error) {
152                         _LOGE("g_io_channel_shutdown error : %s", error->message);
153                         g_error_free(error);
154                 }
155                 g_io_channel_unref(callback_info->gio_read);
156                 callback_info->gio_read = NULL;
157         }
158
159         if (callback_info->g_src_id != 0) {
160                 g_source_remove(callback_info->g_src_id);
161                 callback_info->g_src_id = 0;
162         }
163
164         FREE_AND_NULL(callback_info);
165 }
166
167 static void __callback_info_free_by_info(message_port_callback_info_s *callback_info)
168 {
169         GList *callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(callback_info->local_id));
170         GList *find_list;
171
172         if (callback_info_list == NULL)
173                 return;
174
175         find_list = g_list_find(callback_info_list, callback_info);
176         if (find_list == NULL)
177                 return;
178
179         callback_info_list = g_list_remove_link(callback_info_list, find_list);
180         __callback_info_free(callback_info);
181         g_list_free(find_list);
182 }
183
184 static void __hash_destroy_callback_info(gpointer data)
185 {
186
187         GList *callback_list = (GList *)data;
188         if (callback_list != NULL)
189                 g_list_free_full(callback_list, __callback_info_free);
190 }
191
192 static char *__get_encoded_name(const char *remote_app_id, const char *port_name, bool is_trusted)
193 {
194
195         int prefix_len = strlen(MESSAGEPORT_BUS_NAME_PREFIX);
196         int postfix_len = 1;
197         char *postfix = is_trusted ? "1" : "0";
198
199         unsigned char c[MD5_DIGEST_LENGTH] = {0};
200         char *md5_interface = NULL;
201         char *temp;
202         int index = 0;
203         MD5_CTX mdContext;
204         int encoded_bus_name_len = prefix_len + postfix_len + (MD5_DIGEST_LENGTH * 2) + 2;
205         int bus_name_len = strlen(remote_app_id) + strlen(port_name) + 2;
206         char *bus_name = (char *)calloc(bus_name_len, sizeof(char));
207         if (bus_name == NULL) {
208                 _LOGE("bus_name calloc failed");
209                 return 0;
210         }
211
212         snprintf(bus_name, bus_name_len, "%s_%s", remote_app_id, port_name);
213
214         MD5_Init(&mdContext);
215         MD5_Update(&mdContext, bus_name, bus_name_len);
216         MD5_Final(c, &mdContext);
217
218         md5_interface = (char *)calloc(encoded_bus_name_len , sizeof(char));
219         if (md5_interface == NULL) {
220                 if (bus_name)
221                         free(bus_name);
222
223                 _LOGE("md5_interface calloc failed!!");
224                 return 0;
225         }
226
227         snprintf(md5_interface, encoded_bus_name_len, "%s", MESSAGEPORT_BUS_NAME_PREFIX);
228         temp = md5_interface;
229         temp += prefix_len;
230
231         for (index = 0; index < MD5_DIGEST_LENGTH; index++) {
232                 snprintf(temp, 3, "%02x", c[index]);
233                 temp += 2;
234         }
235
236         if (postfix && postfix_len > 0)
237                 snprintf(temp, encoded_bus_name_len - (temp - md5_interface), "%s", postfix);
238         if (bus_name)
239                 free(bus_name);
240
241         _LOGI("encoded_bus_name : %s ", md5_interface);
242
243         return md5_interface;
244 }
245
246 static int __remote_port_compare_cb(gconstpointer a, gconstpointer b)
247 {
248         port_list_info_s *key1 = (port_list_info_s *)a;
249         port_list_info_s *key2 = (port_list_info_s *)b;
250
251         if (key1->is_trusted == key2->is_trusted)
252                 return strcmp(key1->port_name, key2->port_name);
253
254         return 1;
255 }
256
257
258 static bool __is_preloaded(const char *local_appid, const char *remote_appid)
259 {
260         _LOGI("IsPreloaded");
261
262         bool preload_local = false;
263         bool preload_remote = false;
264
265         pkgmgrinfo_appinfo_h handle = NULL;
266         int ret = pkgmgrinfo_appinfo_get_usr_appinfo(local_appid, getuid(), &handle);
267         if (ret != PMINFO_R_OK) {
268                 _LOGE("Failed to get the appinfo. %d", ret);
269                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
270                 return false;
271         }
272         ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_local);
273         if (ret != PMINFO_R_OK) {
274                 _LOGE("Failed to check the preloaded application. %d", ret);
275                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
276                 return false;
277         }
278         pkgmgrinfo_appinfo_destroy_appinfo(handle);
279
280         ret = pkgmgrinfo_appinfo_get_usr_appinfo(remote_appid, getuid(), &handle);
281         if (ret != PMINFO_R_OK) {
282                 _LOGE("Failed to get the appinfo. %d", ret);
283                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
284                 return false;
285         }
286         ret = pkgmgrinfo_appinfo_is_preload(handle, &preload_remote);
287         if (ret != PMINFO_R_OK) {
288                 _LOGE("Failed to check the preloaded application. %d", ret);
289                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
290                 return false;
291         }
292
293         if (preload_local && preload_remote) {
294                 pkgmgrinfo_appinfo_destroy_appinfo(handle);
295                 return true;
296         }
297         pkgmgrinfo_appinfo_destroy_appinfo(handle);
298         return false;
299 }
300
301 static int __check_certificate(const char *local_appid, const char *remote_appid)
302 {
303         _LOGI("CheckCertificate");
304
305         pkgmgrinfo_cert_compare_result_type_e res;
306         int ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(local_appid, remote_appid, getuid(), &res);
307         if (ret < 0) {
308                 _LOGE(":CheckCertificate() Failed");
309                 return MESSAGEPORT_ERROR_IO_ERROR;
310         }
311         if (res != PMINFO_CERT_COMPARE_MATCH) {
312                 _LOGE("CheckCertificate() Failed : MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH");
313                 return MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
314         }
315
316         return MESSAGEPORT_ERROR_NONE;
317 }
318
319 static void on_name_appeared(GDBusConnection *connection,
320                 const gchar     *name,
321                 const gchar     *name_owner,
322                 gpointer         user_data)
323 {
324         _LOGI("name appeared : %s %s", __app_id, name);
325 }
326
327 static void on_name_vanished(GDBusConnection *connection,
328                 const gchar     *name,
329                 gpointer         user_data)
330 {
331         _LOGI("name vanished : %s", name);
332         port_list_info_s *pli = (port_list_info_s *)user_data;
333         if (pli == NULL) {
334                 LOGE("NULL port info");
335                 return;
336         }
337
338         _LOGI("watcher_id :%d", pli->watcher_id);
339         if (pli->watcher_id > 0)
340                 g_bus_unwatch_name(pli->watcher_id);
341         else
342                 _LOGE("Invalid watcher_id %d", pli->watcher_id);
343         pli->exist = false;
344         pli->watcher_id = 0;
345
346 }
347
348 static int __get_local_port_info(int id, message_port_local_port_info_s **info)
349 {
350         message_port_local_port_info_s *mi = (message_port_local_port_info_s *)g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(id));
351
352         if (mi == NULL)
353                 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
354         *info = mi;
355
356         return MESSAGEPORT_ERROR_NONE;
357 }
358
359 static port_list_info_s *__set_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
360 {
361         int ret_val = MESSAGEPORT_ERROR_NONE;
362         port_list_info_s *port_info = (port_list_info_s *)calloc(1, sizeof(port_list_info_s));
363
364         if (!port_info) {
365                 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
366                 goto out;
367         }
368         port_info->port_name = strdup(remote_port);
369         if (!port_info->port_name) {
370                 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
371                 goto out;
372         }
373         port_info->is_trusted = is_trusted;
374         port_info->encoded_bus_name = __get_encoded_name(remote_app_id, remote_port, is_trusted);
375         if (port_info->encoded_bus_name == NULL) {
376                 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
377                 goto out;
378         }
379         port_info->send_sock_fd = 0;
380 out:
381         if (ret_val != MESSAGEPORT_ERROR_NONE) {
382                 if (port_info) {
383                         FREE_AND_NULL(port_info->port_name);
384                         FREE_AND_NULL(port_info->encoded_bus_name);
385                         free(port_info);
386                 }
387                 return NULL;
388         }
389         return port_info;
390 }
391
392 static message_port_remote_app_info_s *__set_remote_app_info(const char *remote_app_id, const char *remote_port, bool is_trusted)
393 {
394         message_port_remote_app_info_s *remote_app_info = NULL;
395         int ret_val = MESSAGEPORT_ERROR_NONE;
396
397         remote_app_info = (message_port_remote_app_info_s *)calloc(1, sizeof(message_port_remote_app_info_s));
398         if (!remote_app_info) {
399                 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
400                 goto out;
401         }
402
403         remote_app_info->remote_app_id = strdup(remote_app_id);
404         if (remote_app_info->remote_app_id == NULL) {
405                 ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;;
406                 goto out;
407         }
408
409 out:
410         if (ret_val != MESSAGEPORT_ERROR_NONE) {
411                 if (remote_app_info) {
412                         FREE_AND_NULL(remote_app_info->remote_app_id);
413                         FREE_AND_NULL(remote_app_info);
414                 }
415                 return NULL;
416         }
417         return remote_app_info;
418 }
419
420 static void __clear_disconnect_socket(port_list_info_s *port_info)
421 {
422         GError *error = NULL;
423
424         if (port_info == NULL)
425                 return;
426         _LOGI("__clear_disconnect_socket : fd [%d]", port_info->send_sock_fd);
427
428         if (port_info->gio_read != NULL) {
429                 g_io_channel_shutdown(port_info->gio_read, TRUE, &error);
430                 if (error) {
431                         _LOGE("g_io_channel_shutdown error : %s", error->message);
432                         g_error_free(error);
433                 }
434                 g_io_channel_unref(port_info->gio_read);
435                 port_info->gio_read = NULL;
436         }
437
438         if (port_info->g_src_id != 0) {
439                 g_source_remove(port_info->g_src_id);
440                 port_info->g_src_id = 0;
441         }
442         port_info->send_sock_fd = 0;
443 }
444
445 static gboolean __socket_disconnect_handler(GIOChannel *gio,
446                 GIOCondition cond,
447                 gpointer data)
448 {
449         /* It's sender socket's gio channel so, only EOF can be received */
450         port_list_info_s *port_info = (port_list_info_s *)data;
451         _LOGI("__socket_disconnect_handler %d", cond);
452         __clear_disconnect_socket(port_info);
453         return FALSE;
454 }
455
456 static void __watch_remote_port_info(port_list_info_s *port_info)
457 {
458         if (port_info == NULL)
459                 return;
460
461         if (port_info->watcher_id < 1) {
462                 port_info->watcher_id = g_bus_watch_name_on_connection(
463                                 __gdbus_conn,
464                                 port_info->encoded_bus_name,
465                                 G_BUS_NAME_WATCHER_FLAGS_NONE,
466                                 on_name_appeared,
467                                 on_name_vanished,
468                                 port_info,
469                                 NULL);
470         } else {
471                 LOGI("Already watched port info");
472                 return;
473         }
474 }
475
476 static int __get_remote_port_info(const char *remote_app_id, const char *remote_port, bool is_trusted,
477                 message_port_remote_app_info_s **mri, port_list_info_s **pli)
478 {
479         message_port_remote_app_info_s *remote_app_info = NULL;
480         port_list_info_s port_info;
481         GList *cb_list = NULL;
482         int ret_val = MESSAGEPORT_ERROR_NONE;
483
484         remote_app_info = (message_port_remote_app_info_s *)g_hash_table_lookup(__remote_app_info, remote_app_id);
485
486         if (remote_app_info == NULL) {
487                 remote_app_info = __set_remote_app_info(remote_app_id, remote_port, is_trusted);
488
489                 if (remote_app_info == NULL) {
490                         ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
491                         goto out;
492                 }
493                 g_hash_table_insert(__remote_app_info, remote_app_info->remote_app_id, remote_app_info);
494         }
495         *mri = remote_app_info;
496
497         port_info.port_name = strdup(remote_port);
498         port_info.is_trusted = is_trusted;
499         cb_list = g_list_find_custom(remote_app_info->port_list, &port_info,
500                                         (GCompareFunc)__remote_port_compare_cb);
501         if (port_info.port_name)
502                 free(port_info.port_name);
503         if (cb_list == NULL) {
504                 port_list_info_s *tmp = __set_remote_port_info(remote_app_id, remote_port, is_trusted);
505                 if (tmp == NULL) {
506                         ret_val = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
507                         goto out;
508                 }
509                 remote_app_info->port_list = g_list_append(remote_app_info->port_list, tmp);
510                 *pli = tmp;
511         } else {
512                 *pli = (port_list_info_s *)cb_list->data;
513         }
514 out:
515
516         return ret_val;
517 }
518
519 static bool __is_local_port_registed(const char *local_port, bool trusted, int *local_id, message_port_local_port_info_s **lpi)
520 {
521         GHashTableIter iter;
522         gpointer key, value;
523
524         g_hash_table_iter_init(&iter, __local_port_info);
525         while (g_hash_table_iter_next(&iter, &key, &value)) {
526                 message_port_local_port_info_s *mi = (message_port_local_port_info_s *)value;
527
528                 if ((mi->is_trusted == trusted) && strcmp(mi->port_name, local_port) == 0) {
529                         *local_id = mi->local_id;
530                         if (lpi != NULL)
531                                 *lpi = mi;
532                         return true;
533                 }
534         }
535         return false;
536 }
537
538 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
539 {
540         GDBusMessage *msg = NULL;
541         GDBusMessage *reply = NULL;
542         GError *err = NULL;
543         GVariant *body;
544         int pid = 0;
545
546         msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
547                         "org.freedesktop.DBus", "GetConnectionUnixProcessID");
548         if (!msg) {
549                 _LOGE("Can't allocate new method call");
550                 goto out;
551         }
552
553         g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
554         reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
555                                                         G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
556
557         if (!reply) {
558                 if (err != NULL) {
559                         _LOGE("Failed to get pid [%s]", err->message);
560                         g_error_free(err);
561                 }
562                 goto out;
563         }
564
565         body = g_dbus_message_get_body(reply);
566         g_variant_get(body, "(u)", &pid);
567
568 out:
569         if (msg)
570                 g_object_unref(msg);
571         if (reply)
572                 g_object_unref(reply);
573
574         return pid;
575 }
576
577 static int __write_socket(int fd,
578                 const char *buffer,
579                 unsigned int nbytes,
580                 unsigned int *bytes_write)
581 {
582         unsigned int left = nbytes;
583         ssize_t nb;
584         int retry_cnt = 0;
585
586         *bytes_write = 0;
587         while (left && (retry_cnt < MAX_RETRY_CNT)) {
588                 nb = write(fd, buffer, left);
589                 if (nb == -1) {
590                         if (errno == EINTR) {
591                                 LOGE("__write_socket: EINTR error continue ...");
592                                 retry_cnt++;
593                                 continue;
594                         }
595                         LOGE("__write_socket: ...error fd %d: errno %d\n", fd, errno);
596                         return MESSAGEPORT_ERROR_IO_ERROR;
597                 }
598
599                 left -= nb;
600                 buffer += nb;
601                 *bytes_write += nb;
602                 retry_cnt = 0;
603         }
604         return MESSAGEPORT_ERROR_NONE;
605 }
606
607 static int __write_string_to_socket(int fd, const char *buffer, int string_len)
608 {
609         unsigned int nb;
610         if (__write_socket(fd, (char *)&string_len, sizeof(string_len), &nb) != MESSAGEPORT_ERROR_NONE) {
611                 _LOGE("write string_len fail");
612                 return MESSAGEPORT_ERROR_IO_ERROR;
613         }
614
615         if (string_len > 0) {
616                 if (__write_socket(fd, buffer, string_len, &nb) != MESSAGEPORT_ERROR_NONE) {
617                         _LOGE("wirte buffer fail");
618                         return MESSAGEPORT_ERROR_IO_ERROR;
619                 }
620         }
621         return MESSAGEPORT_ERROR_NONE;
622 }
623
624 static int __read_socket(int fd,
625                 char *buffer,
626                 unsigned int nbytes,
627                 unsigned int *bytes_read)
628 {
629         unsigned int left = nbytes;
630         ssize_t nb;
631         int retry_cnt = 0;
632         const struct timespec TRY_SLEEP_TIME = { 0, 500 * 1000 * 1000 };
633
634         *bytes_read = 0;
635         while (left && (retry_cnt < MAX_RETRY_CNT)) {
636                 nb = read(fd, buffer, left);
637                 if (nb == 0) {
638                         LOGE("__read_socket: ...read EOF, socket closed %d: nb %d\n", fd, nb);
639                         return MESSAGEPORT_ERROR_IO_ERROR;
640                 } else if (nb == -1) {
641                         /*  wrt(nodejs) could change socket to none-blocking socket :-( */
642                         if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) {
643                                 LOGE("__read_socket: %d errno, sleep and retry ...", errno);
644                                 retry_cnt++;
645                                 nanosleep(&TRY_SLEEP_TIME, 0);
646                                 continue;
647                         }
648                         LOGE("__read_socket: ...error fd %d: errno %d\n", fd, errno);
649                         return MESSAGEPORT_ERROR_IO_ERROR;
650                 }
651
652                 left -= nb;
653                 buffer += nb;
654                 *bytes_read += nb;
655                 retry_cnt = 0;
656         }
657         return MESSAGEPORT_ERROR_NONE;
658 }
659
660 static int __read_string_from_socket(int fd, char **buffer, int *string_len)
661 {
662         unsigned int nb;
663         if (__read_socket(fd, (char *)string_len, sizeof(*string_len), &nb) != MESSAGEPORT_ERROR_NONE) {
664                 LOGE("read socket fail");
665                 return MESSAGEPORT_ERROR_IO_ERROR;
666         }
667         if (*string_len > 0 && *string_len < MAX_MESSAGE_SIZE) {
668                 *buffer = (char *)calloc(*string_len, sizeof(char));
669                 if (*buffer == NULL) {
670                         LOGE("Out of memory.");
671                         return MESSAGEPORT_ERROR_IO_ERROR;
672                 }
673                 if (__read_socket(fd, *buffer, *string_len, &nb) != MESSAGEPORT_ERROR_NONE) {
674                         LOGE("read socket fail");
675                         return MESSAGEPORT_ERROR_IO_ERROR;
676                 }
677         } else {
678                 LOGE("Invalid string len %d", *string_len);
679                 return MESSAGEPORT_ERROR_IO_ERROR;
680         }
681         return MESSAGEPORT_ERROR_NONE;
682 }
683
684 message_port_pkt_s *__message_port_recv_raw(int fd)
685 {
686         message_port_pkt_s *pkt = NULL;
687         unsigned int nb;
688
689         pkt = (message_port_pkt_s *)calloc(sizeof(message_port_pkt_s), 1);
690         if (pkt == NULL) {
691                 close(fd);
692                 return NULL;
693         }
694
695         if (__read_string_from_socket(fd, (char **)&pkt->remote_port_name, &pkt->remote_port_name_len) != MESSAGEPORT_ERROR_NONE) {
696                 LOGE("read socket fail: port_name");
697                 free(pkt->remote_port_name);
698                 free(pkt);
699                 return NULL;
700         }
701
702         if (__read_socket(fd, (char *)&pkt->is_bidirection, sizeof(pkt->is_bidirection), &nb) != MESSAGEPORT_ERROR_NONE) {
703                 LOGE("read socket fail: is_bidirection");
704                 free(pkt->remote_port_name);
705                 free(pkt);
706                 return NULL;
707         }
708
709         if (__read_socket(fd, (char *)&pkt->is_trusted, sizeof(pkt->is_trusted), &nb) != MESSAGEPORT_ERROR_NONE) {
710                 LOGE("read socket fail: is_trusted");
711                 free(pkt->remote_port_name);
712                 free(pkt);
713                 return NULL;
714         }
715
716         if (__read_string_from_socket(fd, (char **)&pkt->data, &pkt->data_len) != MESSAGEPORT_ERROR_NONE) {
717                 LOGE("read socket fail: data");
718                 if (pkt->data)
719                         free(pkt->data);
720                 free(pkt->remote_port_name);
721                 free(pkt);
722                 return NULL;
723         }
724
725         return pkt;
726 }
727
728 static gboolean __socket_request_handler(GIOChannel *gio,
729                 GIOCondition cond,
730                 gpointer data)
731 {
732         int fd = 0;
733         message_port_callback_info_s *mi;
734         message_port_pkt_s *pkt;
735         bundle *kb = NULL;
736         GError *error = NULL;
737
738         mi = (message_port_callback_info_s *)data;
739         if (mi == NULL) {
740
741                 g_io_channel_shutdown(gio, TRUE, &error);
742                 if (error) {
743                         _LOGE("g_io_channel_shutdown error : %s", error->message);
744                         g_error_free(error);
745                 }
746                 g_io_channel_unref(gio);
747                 return FALSE;
748         }
749
750         if (cond == G_IO_HUP) {
751
752                 _LOGI("socket G_IO_HUP");
753                 __callback_info_free_by_info(mi);
754                 return FALSE;
755
756         } else {
757
758                 if ((fd = g_io_channel_unix_get_fd(gio)) < 0) {
759                         _LOGE("fail to get fd from io channel");
760                         __callback_info_free_by_info(mi);
761                         return FALSE;
762                 }
763
764                 if ((pkt = __message_port_recv_raw(fd)) == NULL) {
765                         _LOGE("recv error on SOCKET");
766                         __callback_info_free_by_info(mi);
767                         return FALSE;
768                 }
769
770                 kb = bundle_decode(pkt->data, pkt->data_len);
771                 if (pkt->is_bidirection)
772                         mi->callback(mi->local_id, mi->remote_app_id, pkt->remote_port_name, pkt->is_trusted, kb, NULL);
773                 else
774                         mi->callback(mi->local_id, mi->remote_app_id, NULL, pkt->is_trusted, kb, NULL);
775
776                 bundle_free(kb);
777                 if (pkt) {
778                         if (pkt->remote_port_name)
779                                 free(pkt->remote_port_name);
780                         if (pkt->data)
781                                 free(pkt->data);
782                         free(pkt);
783                 }
784         }
785
786         return TRUE;
787 }
788
789 static bool send_message(GVariant *parameters, GDBusMethodInvocation *invocation)
790 {
791         char *local_port = NULL;
792         char *local_appid = NULL;
793         char *remote_appid = NULL;
794         char *remote_port = NULL;
795         gboolean local_trusted = false;
796         gboolean remote_trusted = false;
797         gboolean bi_dir = false;
798         int len = 0;
799
800         bundle *data = NULL;
801         bundle_raw *raw = NULL;
802         message_port_local_port_info_s *mi;
803         int local_reg_id = 0;
804         message_port_callback_info_s *callback_info;
805         message_port_callback_info_s *head_callback_info;
806         GList *callback_info_list = NULL;
807
808         char buf[1024];
809         GDBusMessage *msg;
810         GUnixFDList *fd_list;
811         int fd_len;
812         int *returned_fds = NULL;
813         int fd;
814
815         g_variant_get(parameters, "(&s&sbb&s&sbu&s)", &local_appid, &local_port, &local_trusted, &bi_dir,
816                         &remote_appid, &remote_port, &remote_trusted, &len, &raw);
817
818         if (!remote_port) {
819                 _LOGE("Invalid argument : remote_port is NULL");
820                 goto out;
821         }
822         if (!remote_appid) {
823                 _LOGE("Invalid argument : remote_appid is NULL");
824                 goto out;
825         }
826         if (!__is_local_port_registed(remote_port, remote_trusted, &local_reg_id, &mi)) {
827                 _LOGE("Invalid argument : remote_port:(%s) trusted(%d)", remote_port, remote_trusted);
828                 goto out;
829         }
830         if (!local_appid) {
831                 _LOGE("Invalid argument : local_appid");
832                 goto out;
833         }
834         if (!local_port) {
835                 _LOGE("Invalid argument : local_port");
836                 goto out;
837         }
838         if (strcmp(remote_appid, __app_id) != 0) {
839                 _LOGE("Invalid argument : remote_appid (%s)", remote_appid);
840                 goto out;
841         }
842         if (strcmp(remote_port, mi->port_name) != 0) {
843                 _LOGE("Invalid argument : remote_port (%s)", remote_port);
844                 goto out;
845         }
846         if (!len) {
847                 _LOGE("Invalid argument : data_len");
848                 goto out;
849         }
850         if (remote_trusted) {
851                 if (g_hash_table_lookup(__trusted_app_list_hash, (gpointer)local_appid) == NULL) {
852                         if (!__is_preloaded(local_appid, remote_appid)) {
853                                 int ret = __check_certificate(local_appid, remote_appid);
854                                 if (ret == MESSAGEPORT_ERROR_NONE)
855                                         g_hash_table_insert(__trusted_app_list_hash, local_appid, "TRUE");
856                                 else {
857                                         _LOGE("The application (%s) is not signed with the same certificate",
858                                                         local_appid);
859                                         goto out;
860                                 }
861                         }
862                 }
863         }
864
865         callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
866         if (callback_info == NULL)
867                 goto out;
868
869         callback_info->local_id = mi->local_id;
870         callback_info->remote_app_id = strdup(local_appid);
871         callback_info->callback = mi->callback;
872
873         msg = g_dbus_method_invocation_get_message(invocation);
874         fd_list = g_dbus_message_get_unix_fd_list(msg);
875
876         /* When application send message to self fd_list is NULL */
877         if (fd_list != NULL) {
878                 returned_fds = g_unix_fd_list_steal_fds(fd_list, &fd_len);
879                 if (returned_fds == NULL) {
880                         _LOGE("fail to get fds");
881                         __callback_info_free(callback_info);
882                         return false;
883                 }
884                 fd = returned_fds[0];
885
886                 LOGI("g_unix_fd_list_get %d fd: [%d]", fd_len, fd);
887                 if (fd > 0) {
888
889                         callback_info->gio_read = g_io_channel_unix_new(fd);
890                         if (!callback_info->gio_read) {
891                                 _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
892                                 __callback_info_free(callback_info);
893                                 return false;
894                         }
895
896                         callback_info->g_src_id = g_io_add_watch(callback_info->gio_read, G_IO_IN | G_IO_HUP,
897                                         __socket_request_handler, (gpointer)callback_info);
898                         if (callback_info->g_src_id == 0) {
899                                 _LOGE("fail to add watch on socket");
900                                 __callback_info_free(callback_info);
901                                 return false;
902                         }
903
904                         callback_info_list = g_hash_table_lookup(__callback_info_hash, GUINT_TO_POINTER(mi->local_id));
905                         if (callback_info_list == NULL) {
906                                 head_callback_info = (message_port_callback_info_s *)calloc(1, sizeof(message_port_callback_info_s));
907                                 if (head_callback_info == NULL) {
908                                         _LOGE("fail to alloc head_callback_info");
909                                         __callback_info_free(callback_info);
910                                         return false;
911                                 }
912                                 head_callback_info->local_id = 0;
913                                 head_callback_info->remote_app_id = NULL;
914                                 head_callback_info->callback = NULL;
915                                 head_callback_info->gio_read = NULL;
916                                 head_callback_info->g_src_id = 0;
917                                 callback_info_list = g_list_append(callback_info_list, head_callback_info);
918                                 callback_info_list = g_list_append(callback_info_list, callback_info);
919                                 g_hash_table_insert(__callback_info_hash, GUINT_TO_POINTER(mi->local_id), callback_info_list);
920                         } else {
921                                 callback_info_list = g_list_append(callback_info_list, callback_info);
922                         }
923                 }
924         }
925
926         data = bundle_decode(raw, len);
927         if (!data) {
928                 _LOGE("Invalid argument : message");
929                 goto out;
930         }
931
932         LOGI("call calback %s", local_appid);
933         if (bi_dir)
934                 mi->callback(mi->local_id, local_appid, local_port, local_trusted, data, NULL);
935         else
936                 mi->callback(mi->local_id, local_appid, NULL, false, data, NULL);
937         bundle_free(data);
938 out:
939         if (returned_fds)
940                 free(returned_fds);
941
942         return true;
943 }
944
945 static int __check_remote_port(const char *remote_app_id, const char *remote_port, bool is_trusted, bool *exist)
946 {
947         _LOGI("Check a remote port : [%s:%s]", remote_app_id, remote_port);
948
949         GVariant *result = NULL;
950         GError *err = NULL;
951         int ret_val = MESSAGEPORT_ERROR_NONE;
952         char *bus_name = NULL;
953         message_port_remote_app_info_s *remote_app_info = NULL;
954         port_list_info_s *port_info = NULL;
955         int local_reg_id = 0;
956         message_port_local_port_info_s *mi = NULL;
957         gboolean name_exist = false;
958
959         _LOGI("remote_app_id, app_id :[%s : %s] ", remote_app_id, __app_id);
960
961         ret_val = __get_remote_port_info(remote_app_id, remote_port, is_trusted, &remote_app_info, &port_info);
962         if (ret_val != MESSAGEPORT_ERROR_NONE)
963                 return ret_val;
964
965         /* self check */
966         if (strcmp(remote_app_id, __app_id) == 0) {
967
968                 _LOGI("__is_local_port_registed ");
969                 if (!__is_local_port_registed(remote_port, is_trusted, &local_reg_id, &mi))
970                         *exist = false;
971                 else
972                         *exist = true;
973
974                 _LOGI("__is_local_port_registed : %d ", *exist);
975                 return MESSAGEPORT_ERROR_NONE;
976         }
977
978         port_info->exist = false;
979         bus_name = port_info->encoded_bus_name;
980
981         result = g_dbus_connection_call_sync(
982                         __gdbus_conn,
983                         DBUS_SERVICE_DBUS,
984                         DBUS_PATH_DBUS,
985                         DBUS_INTERFACE_DBUS,
986                         "NameHasOwner",
987                         g_variant_new("(s)", bus_name),
988                         G_VARIANT_TYPE("(b)"),
989                         G_DBUS_CALL_FLAGS_NONE,
990                         -1,
991                         NULL,
992                         &err);
993
994         if (err || (result == NULL)) {
995                 if (err) {
996                         _LOGE("No reply. error = %s", err->message);
997                         g_error_free(err);
998                 }
999                 ret_val = MESSAGEPORT_ERROR_RESOURCE_UNAVAILABLE;
1000         } else {
1001                 g_variant_get(result, "(b)", &name_exist);
1002
1003                 if (!name_exist) {
1004                         _LOGI("Name not exist %s", bus_name);
1005                         *exist = false;
1006                         ret_val = MESSAGEPORT_ERROR_NONE;
1007                 } else {
1008
1009                         if (is_trusted) {
1010                                 if (remote_app_info->certificate_info != CERTIFICATE_MATCH) {
1011                                         if (!__is_preloaded(__app_id, remote_app_id)) {
1012                                                 if (__check_certificate(__app_id, remote_app_id) != MESSAGEPORT_ERROR_NONE) {
1013                                                         ret_val = MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH;
1014                                                         goto out;
1015                                                 }
1016                                         }
1017                                         remote_app_info->certificate_info = CERTIFICATE_MATCH;
1018                                 }
1019                         }
1020                         port_info->exist = true;
1021                         *exist = true;
1022                         ret_val = MESSAGEPORT_ERROR_NONE;
1023                         __watch_remote_port_info(port_info);
1024                 }
1025         }
1026 out:
1027         if (result)
1028                 g_variant_unref(result);
1029
1030         return ret_val;
1031 }
1032
1033 static void __on_sender_name_appeared(GDBusConnection *connection,
1034                 const gchar     *name,
1035                 const gchar     *name_owner,
1036                 gpointer         user_data)
1037 {
1038         _LOGI("sender name appeared : %s", name);
1039 }
1040
1041 static void __on_sender_name_vanished(GDBusConnection *connection,
1042                 const gchar     *name,
1043                 gpointer         user_data)
1044 {
1045         gboolean remove_result = FALSE;
1046         int *watcher_id = (int *)user_data;
1047         remove_result = g_hash_table_remove(__sender_appid_hash, (gpointer)name);
1048         if (!remove_result)
1049                 _LOGE("Fail to remove sender appid from hash : %s", name);
1050
1051         if (watcher_id) {
1052                 if (*watcher_id > 0)
1053                         g_bus_unwatch_name(*watcher_id);
1054                 else
1055                         LOGE("Invalid watcher_id %d", *watcher_id);
1056                 free(watcher_id);
1057         } else {
1058                 LOGE("watcher_id is NULL");
1059         }
1060 }
1061
1062 static bool __check_sender_validation(GVariant *parameters, const char *sender, GDBusConnection *conn)
1063 {
1064         int ret = 0;
1065         char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
1066         char *local_appid = NULL;
1067         int pid = __get_sender_pid(conn, sender);
1068         int *watcher_id = (int *)calloc(1, sizeof(int));
1069
1070         ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
1071         if (ret != AUL_R_OK) {
1072                 _LOGE("Failed to get the sender ID: (%s) (%d)", sender, pid);
1073                 free(watcher_id);
1074                 return false;
1075         }
1076
1077         g_variant_get_child(parameters, 0, "&s", &local_appid);
1078         if (local_appid == NULL) {
1079                 _LOGE("appid is NULL : (%s) (%d)", sender, pid);
1080                 free(watcher_id);
1081                 return false;
1082         }
1083
1084         if (strncmp(buffer, local_appid, MAX_PACKAGE_STR_SIZE) == 0) {
1085                 _LOGI("insert sender !!!!! %s", sender);
1086                 g_hash_table_insert(__sender_appid_hash, (gpointer)strdup(sender), GINT_TO_POINTER(pid));
1087                 *watcher_id = g_bus_watch_name_on_connection(
1088                                         __gdbus_conn,
1089                                         sender,
1090                                         G_BUS_NAME_WATCHER_FLAGS_NONE,
1091                                         __on_sender_name_appeared,
1092                                         __on_sender_name_vanished,
1093                                         watcher_id,
1094                                         NULL);
1095         } else {
1096                 free(watcher_id);
1097                 return false;
1098         }
1099         return true;
1100 }
1101
1102 static void __dbus_method_call_handler(GDBusConnection *conn,
1103                                 const gchar *sender, const gchar *object_path,
1104                                 const gchar *iface_name, const gchar *method_name,
1105                                 GVariant *parameters, GDBusMethodInvocation *invocation,
1106                                 gpointer user_data)
1107 {
1108         _LOGI("method_name: %s, sender: %s", method_name, sender);
1109         gpointer sender_pid = g_hash_table_lookup(__sender_appid_hash, sender);
1110         if (sender_pid == NULL) {
1111                 if (!__check_sender_validation(parameters, sender, conn))
1112                         goto out;
1113         }
1114         if (g_strcmp0(method_name, "send_message") == 0)
1115                 send_message(parameters, invocation);
1116 out:
1117         g_dbus_method_invocation_return_value(invocation, NULL);
1118 }
1119
1120 static const GDBusInterfaceVTable interface_vtable = {
1121         __dbus_method_call_handler,
1122         NULL,
1123         NULL
1124 };
1125
1126 static int __dbus_init(void)
1127 {
1128         bool ret = false;
1129         GError *error = NULL;
1130
1131         __gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
1132         if (__gdbus_conn == NULL) {
1133                 if (error != NULL) {
1134                         _LOGE("Failed to get dbus [%s]", error->message);
1135                         g_error_free(error);
1136                 }
1137                 goto out;
1138         }
1139
1140         ret = true;
1141
1142 out:
1143         if (!__gdbus_conn)
1144                 g_object_unref(__gdbus_conn);
1145
1146         return ret;
1147
1148 }
1149
1150 int __register_dbus_interface(const char *port_name, bool is_trusted)
1151 {
1152
1153         GDBusNodeInfo *introspection_data = NULL;
1154         int registration_id = 0;
1155
1156         static gchar introspection_prefix[] =
1157                 "<node>"
1158                 "  <interface name='";
1159
1160         static gchar introspection_postfix[] =
1161                 "'>"
1162                 "        <method name='send_message'>"
1163                 "          <arg type='s' name='local_appid' direction='in'/>"
1164                 "          <arg type='s' name='local_port' direction='in'/>"
1165                 "          <arg type='b' name='local_trusted' direction='in'/>"
1166                 "          <arg type='b' name='bi_dir' direction='in'/>"
1167                 "          <arg type='s' name='remote_appid' direction='in'/>"
1168                 "          <arg type='s' name='remote_port' direction='in'/>"
1169                 "          <arg type='b' name='remote_trusted' direction='in'/>"
1170                 "          <arg type='u' name='data_len' direction='in'/>"
1171                 "          <arg type='s' name='data' direction='in'/>"
1172                 "        </method>"
1173                 "  </interface>"
1174                 "</node>";
1175
1176         char *introspection_xml = NULL;
1177         int introspection_xml_len = 0;
1178
1179
1180         int owner_id = 0;
1181         GError *error = NULL;
1182         char *bus_name = NULL;
1183         char *interface_name = NULL;
1184         GVariant *result = NULL;
1185
1186         bus_name = __get_encoded_name(__app_id, port_name, is_trusted);
1187         if (!bus_name) {
1188                 _LOGE("Fail to get bus name");
1189                 goto out;
1190         }
1191         interface_name = bus_name;
1192
1193         introspection_xml_len = strlen(introspection_prefix) + strlen(interface_name) +
1194                 strlen(introspection_postfix) + 1;
1195
1196         introspection_xml = (char *)calloc(introspection_xml_len, sizeof(char));
1197         if (!introspection_xml) {
1198                 _LOGE("out of memory");
1199                 goto out;
1200         }
1201
1202
1203         result = g_dbus_connection_call_sync(
1204                         __gdbus_conn,
1205                         DBUS_SERVICE_DBUS,
1206                         DBUS_PATH_DBUS,
1207                         DBUS_INTERFACE_DBUS,
1208                         "RequestName",
1209                         g_variant_new("(su)", bus_name, G_BUS_NAME_OWNER_FLAGS_NONE),
1210                         G_VARIANT_TYPE("(u)"),
1211                         G_DBUS_CALL_FLAGS_NONE,
1212                         -1,
1213                         NULL,
1214                         &error);
1215         if (error) {
1216                 _LOGE("RequestName fail : %s", error->message);
1217                 g_error_free(error);
1218                 goto out;
1219         }
1220         if (result == NULL) {
1221                 _LOGE("fail to get name NULL");
1222                 goto out;
1223         }
1224         g_variant_get(result, "(u)", &owner_id);
1225         if (owner_id == 0) {
1226                 _LOGE("Acquiring the own name is failed");
1227                 goto out;
1228         }
1229
1230         _LOGI("Acquiring the own name : %d", owner_id);
1231
1232         snprintf(introspection_xml, introspection_xml_len, "%s%s%s", introspection_prefix, interface_name, introspection_postfix);
1233
1234         introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, NULL);
1235         if (!introspection_data) {
1236                 _LOGE("g_dbus_node_info_new_for_xml() is failed.");
1237                 goto out;
1238         }
1239
1240         registration_id = g_dbus_connection_register_object(__gdbus_conn,
1241                                                 MESSAGEPORT_OBJECT_PATH, introspection_data->interfaces[0],
1242                                                 &interface_vtable, NULL, NULL, NULL);
1243
1244         _LOGI("registration_id %d", registration_id);
1245
1246         if (registration_id == 0) {
1247                 _LOGE("Failed to g_dbus_connection_register_object");
1248                 goto out;
1249         }
1250
1251 out:
1252         if (introspection_data)
1253                 g_dbus_node_info_unref(introspection_data);
1254         if (introspection_xml)
1255                 free(introspection_xml);
1256         if (bus_name)
1257                 free(bus_name);
1258         if (result)
1259                 g_variant_unref(result);
1260
1261
1262         return registration_id;
1263 }
1264
1265 /* LCOV_EXCL_START */
1266 void __list_free_port_list(gpointer data)
1267 {
1268         port_list_info_s *n = (port_list_info_s *)data;
1269
1270         FREE_AND_NULL(n->encoded_bus_name);
1271         FREE_AND_NULL(n->port_name);
1272         FREE_AND_NULL(n);
1273 }
1274 /* LCOV_EXCL_STOP */
1275
1276 /* LCOV_EXCL_START */
1277 static void __hash_destory_local_value(gpointer data)
1278 {
1279         message_port_local_port_info_s *mli = (message_port_local_port_info_s *)data;
1280         if (mli) {
1281                 if (mli->port_name)
1282                         free(mli->port_name);
1283                 free(mli);
1284         }
1285 }
1286 /* LCOV_EXCL_STOP */
1287
1288 /* LCOV_EXCL_START */
1289 static void __hash_destory_remote_value(gpointer data)
1290 {
1291         message_port_remote_app_info_s *mri = (message_port_remote_app_info_s *)data;
1292         if (mri) {
1293                 FREE_AND_NULL(mri->sender_id);
1294                 FREE_AND_NULL(mri->remote_app_id);
1295                 if (mri->port_list)
1296                         g_list_free_full(mri->port_list, __list_free_port_list);
1297
1298                 free(mri);
1299         }
1300 }
1301 /* LCOV_EXCL_STOP */
1302
1303 static bool __initialize(void)
1304 {
1305
1306 #if !GLIB_CHECK_VERSION(2, 35, 0)
1307         g_type_init();
1308 #endif
1309
1310         int pid = getpid();
1311         int ret = 0;
1312         char buffer[MAX_PACKAGE_STR_SIZE] = {0, };
1313
1314         _LOGI("initialize");
1315         ret = aul_app_get_appid_bypid(pid, buffer, sizeof(buffer));
1316         retvm_if(ret != AUL_R_OK, false, "Failed to get the application ID: %d", ret);
1317
1318         __app_id = strdup(buffer);
1319         retvm_if(!__app_id, false, "Malloc failed");
1320         _LOGI("init : %s", __app_id);
1321
1322         if (__local_port_info == NULL) {
1323                 __local_port_info = g_hash_table_new_full(g_direct_hash,  g_direct_equal, NULL, __hash_destory_local_value);
1324                 retvm_if(!__local_port_info, false, "fail to create __local_port_info");
1325         }
1326
1327         if (__remote_app_info == NULL) {
1328                 __remote_app_info = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, __hash_destory_remote_value);
1329                 retvm_if(!__remote_app_info, false, "fail to create __remote_app_info");
1330         }
1331
1332         if (__sender_appid_hash == NULL) {
1333                 __sender_appid_hash = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
1334                 retvm_if(!__sender_appid_hash, false, "fail to create __sender_appid_hash");
1335         }
1336
1337         if (__trusted_app_list_hash == NULL) {
1338                 __trusted_app_list_hash = g_hash_table_new(g_str_hash, g_str_equal);
1339                 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
1340         }
1341
1342         if (__callback_info_hash == NULL) {
1343                 __callback_info_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, __hash_destroy_callback_info);
1344                 retvm_if(!__trusted_app_list_hash, false, "fail to create __trusted_app_list_hash");
1345         }
1346
1347         if (!__dbus_init())
1348                 return false;
1349         _initialized = true;
1350
1351         return true;
1352 }
1353
1354
1355 static bool __message_port_register_port(const int local_id, const char *local_port, bool is_trusted, messageport_message_cb callback)
1356 {
1357         message_port_local_port_info_s *mi = (message_port_local_port_info_s *)calloc(1, sizeof(message_port_local_port_info_s));
1358         retvm_if(!mi, false, "Malloc failed");
1359
1360         mi->callback = callback;
1361         mi->is_trusted = is_trusted;
1362         mi->port_name = strdup(local_port);
1363         if (mi->port_name == NULL) {
1364                 _LOGE("Malloc failed (%s)", local_port);
1365                 free(mi);
1366                 return false;
1367         }
1368         mi->local_id = local_id;
1369
1370         g_hash_table_insert(__local_port_info, GINT_TO_POINTER(mi->local_id), mi);
1371         return true;
1372 }
1373
1374 static int __register_message_port(const char *local_port, bool is_trusted, messageport_message_cb callback)
1375 {
1376         _SECURE_LOGI("Register a message port : [%s:%s]", __app_id, local_port);
1377
1378         int local_id = 0;
1379
1380         /* Check the message port is already registed */
1381         if (__is_local_port_registed(local_port, is_trusted, &local_id, NULL))
1382                 return local_id;
1383
1384         local_id = __register_dbus_interface(local_port, is_trusted);
1385         if (local_id < 1) {
1386                 _LOGE("register_dbus_interface fail !!");
1387                 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1388         }
1389
1390         if (!__message_port_register_port(local_id, local_port, is_trusted, callback))
1391                 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1392
1393         return local_id;
1394 }
1395
1396 int __message_port_send_async(int sockfd, bundle *kb, const char *local_port,
1397                 bool local_trusted, bool is_bidirection)
1398 {
1399         int ret = 0;
1400         int data_len;
1401         int local_port_len = 0;
1402         unsigned int nb;
1403         bundle_raw *kb_data = NULL;
1404
1405         bundle_encode(kb, &kb_data, &data_len);
1406         if (kb_data == NULL) {
1407                 _LOGE("bundle encode fail");
1408                 ret = MESSAGEPORT_ERROR_IO_ERROR;
1409                 goto out;
1410         }
1411
1412         if (data_len > MAX_MESSAGE_SIZE) {
1413                 _LOGE("bigger than max size\n");
1414                 ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1415                 goto out;
1416         }
1417
1418         if (local_port != NULL)
1419                 local_port_len = strlen(local_port) + 1;
1420
1421         if (__write_string_to_socket(sockfd, local_port, local_port_len) != MESSAGEPORT_ERROR_NONE) {
1422                 _LOGE("write local_port fail");
1423                 ret = MESSAGEPORT_ERROR_IO_ERROR;
1424                 goto out;
1425         }
1426
1427         if (__write_socket(sockfd, (char *)&is_bidirection, sizeof(is_bidirection), &nb) != MESSAGEPORT_ERROR_NONE) {
1428                 _LOGE("write is_bidirection fail");
1429                 ret = MESSAGEPORT_ERROR_IO_ERROR;
1430                 goto out;
1431         }
1432
1433         if (__write_socket(sockfd, (char *)&local_trusted, sizeof(local_trusted), &nb) != MESSAGEPORT_ERROR_NONE) {
1434                 _LOGE("write local_trusted fail");
1435                 ret = MESSAGEPORT_ERROR_IO_ERROR;
1436                 goto out;
1437         }
1438
1439         if (__write_string_to_socket(sockfd, (void *)kb_data, data_len) != MESSAGEPORT_ERROR_NONE) {
1440                 _LOGE("write kb_data fail");
1441                 ret = MESSAGEPORT_ERROR_IO_ERROR;
1442                 goto out;
1443         }
1444 out:
1445         if (kb_data)
1446                 free(kb_data);
1447
1448         return ret;
1449 }
1450
1451 static int __message_port_send_message(const char *remote_appid, const char *remote_port,
1452                 const char *local_port, bool trusted_message, bool local_trusted, bool bi_dir, bundle *message)
1453 {
1454
1455         int ret = MESSAGEPORT_ERROR_NONE;
1456         GUnixFDList *fd_list = NULL;
1457
1458         int len = 0;
1459         bundle_raw *raw = NULL;
1460         char *bus_name = NULL;
1461         char *interface_name = NULL;
1462
1463         message_port_remote_app_info_s *remote_app_info = NULL;
1464         port_list_info_s *port_info = NULL;
1465         GDBusMessage *msg = NULL;
1466         GError *err = NULL;
1467         GVariant *body = NULL;
1468         int sock_pair[2] = {0,};
1469         char buf[1024];
1470
1471         ret = __get_remote_port_info(remote_appid, remote_port, trusted_message, &remote_app_info, &port_info);
1472         if (ret != MESSAGEPORT_ERROR_NONE)
1473                 return ret;
1474
1475         if (port_info->exist == false) {
1476                 bool exist = false;
1477                 _LOGI("port exist check !!");
1478                 ret =  __check_remote_port(remote_appid, remote_port, trusted_message, &exist);
1479                 if (ret != MESSAGEPORT_ERROR_NONE) {
1480                         goto out;
1481                 } else if (!exist) {
1482                         ret = MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1483                         goto out;
1484                 }
1485         }
1486
1487         if (port_info->send_sock_fd > 0) {
1488                 ret = __message_port_send_async(port_info->send_sock_fd, message,
1489                                 (local_port) ? local_port : "", local_trusted, bi_dir);
1490         } else {
1491
1492                 bus_name = port_info->encoded_bus_name;
1493                 interface_name = bus_name;
1494
1495                 if (bundle_encode(message, &raw, &len) != BUNDLE_ERROR_NONE) {
1496                         ret = MESSAGEPORT_ERROR_INVALID_PARAMETER;
1497                         goto out;
1498                 }
1499
1500                 if (MAX_MESSAGE_SIZE < len) {
1501                         _LOGE("The size of message (%d) has exceeded the maximum limit.", len);
1502                         ret = MESSAGEPORT_ERROR_MAX_EXCEEDED;
1503                         goto out;
1504                 }
1505
1506                 body = g_variant_new("(ssbbssbus)", __app_id, (local_port) ? local_port : "", local_trusted, bi_dir,
1507                                 remote_appid, remote_port, trusted_message, len, raw);
1508                 if (strcmp(remote_appid, __app_id) != 0) { /* self send */
1509
1510                         /*  if message-port fail to get socket pair, communicate using GDBus */
1511                         if (aul_request_message_port_socket_pair(sock_pair) != AUL_R_OK) {
1512                                 _LOGE("error create socket pair");
1513                         } else {
1514
1515                                 _LOGI("sock pair : %d, %d",
1516                                                 sock_pair[SOCK_PAIR_SENDER], sock_pair[SOCK_PAIR_RECEIVER]);
1517                                 fd_list = g_unix_fd_list_new();
1518                                 g_unix_fd_list_append(fd_list, sock_pair[SOCK_PAIR_RECEIVER], &err);
1519                                 if (err != NULL) {
1520                                         _LOGE("g_unix_fd_list_append [%s]", err->message);
1521                                         ret = MESSAGEPORT_ERROR_IO_ERROR;
1522                                         g_error_free(err);
1523                                         goto out;
1524                                 }
1525
1526                                 port_info->send_sock_fd = sock_pair[SOCK_PAIR_SENDER];
1527                                 close(sock_pair[SOCK_PAIR_RECEIVER]);
1528                                 sock_pair[SOCK_PAIR_RECEIVER] = 0;
1529
1530                                 port_info->gio_read = g_io_channel_unix_new(port_info->send_sock_fd);
1531                                 if (!port_info->gio_read) {
1532                                         _LOGE("Error is %s\n", strerror_r(errno, buf, sizeof(buf)));
1533                                         ret = MESSAGEPORT_ERROR_IO_ERROR;
1534                                         goto out;
1535                                 }
1536
1537                                 port_info->g_src_id = g_io_add_watch(port_info->gio_read, G_IO_IN | G_IO_HUP,
1538                                                 __socket_disconnect_handler, (gpointer)port_info);
1539                                 if (port_info->g_src_id == 0) {
1540                                         _LOGE("fail to add watch on socket");
1541                                         ret = MESSAGEPORT_ERROR_IO_ERROR;
1542                                         goto out;
1543                                 }
1544
1545                         }
1546                 }
1547
1548                 msg = g_dbus_message_new_method_call(bus_name, MESSAGEPORT_OBJECT_PATH, interface_name, "send_message");
1549                 if (!msg) {
1550                         _LOGE("Can't allocate new method call");
1551                         ret = MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1552                         goto out;
1553                 }
1554
1555                 g_dbus_message_set_unix_fd_list(msg, fd_list);
1556                 g_dbus_message_set_body(msg, body);
1557                 g_dbus_connection_send_message(__gdbus_conn, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
1558                 if (err != NULL) {
1559                         _LOGE("No reply. error = %s", err->message);
1560                         g_error_free(err);
1561                         ret = MESSAGEPORT_ERROR_IO_ERROR;
1562                         goto out;
1563                 }
1564                 __watch_remote_port_info(port_info);
1565
1566         }
1567
1568 out:
1569         if (msg)
1570                 g_object_unref(msg);
1571         if (raw)
1572                 bundle_free_encoded_rawdata(&raw);
1573         if (fd_list)
1574                 g_object_unref(fd_list);
1575
1576         if (ret != MESSAGEPORT_ERROR_NONE) {
1577                 __clear_disconnect_socket(port_info);
1578                 if (sock_pair[SOCK_PAIR_SENDER])
1579                         close(sock_pair[SOCK_PAIR_SENDER]);
1580                 if (sock_pair[SOCK_PAIR_RECEIVER])
1581                         close(sock_pair[SOCK_PAIR_RECEIVER]);
1582         }
1583
1584         return ret;
1585 }
1586
1587 int __message_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port,  bool trusted_message, bundle *message)
1588 {
1589         message_port_local_port_info_s *local_info;
1590         int ret = __get_local_port_info(id, &local_info);
1591         if (ret != MESSAGEPORT_ERROR_NONE)
1592                 return ret;
1593
1594         _LOGI("bidirectional_message %s", local_info->port_name);
1595         return __message_port_send_message(remote_app_id, remote_port,
1596                         local_info->port_name, trusted_message, local_info->is_trusted, true, message);
1597 }
1598
1599 int messageport_unregister_local_port(int local_port_id, bool trusted_port)
1600 {
1601
1602         GVariant *result;
1603         char *bus_name = NULL;
1604         GError *err = NULL;
1605         int ret = 0;
1606
1607         _LOGI("unregister : %d", local_port_id);
1608
1609         message_port_local_port_info_s *mi =
1610                 (message_port_local_port_info_s *)
1611                 g_hash_table_lookup(__local_port_info, GINT_TO_POINTER(local_port_id));
1612         if (mi == NULL)
1613                 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1614
1615         if (mi->is_trusted != trusted_port)
1616                 return MESSAGEPORT_ERROR_INVALID_PARAMETER;
1617
1618         g_hash_table_remove(__callback_info_hash, GUINT_TO_POINTER(local_port_id));
1619
1620         bus_name = __get_encoded_name(__app_id, mi->port_name, mi->is_trusted);
1621         if (bus_name == NULL)
1622                 return MESSAGEPORT_ERROR_OUT_OF_MEMORY;
1623
1624         g_dbus_connection_unregister_object(__gdbus_conn, local_port_id);
1625
1626         result = g_dbus_connection_call_sync(
1627                         __gdbus_conn,
1628                         DBUS_SERVICE_DBUS,
1629                         DBUS_PATH_DBUS,
1630                         DBUS_INTERFACE_DBUS,
1631                         "ReleaseName",
1632                         g_variant_new("(s)", bus_name),
1633                         G_VARIANT_TYPE("(u)"),
1634                         G_DBUS_CALL_FLAGS_NONE,
1635                         -1,
1636                         NULL,
1637                         &err);
1638
1639         if (bus_name)
1640                 free(bus_name);
1641
1642         if (err) {
1643                 _LOGE("RequestName fail : %s", err->message);
1644                 g_error_free(err);
1645                 return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1646         }
1647         g_variant_get(result, "(u)", &ret);
1648
1649         if (result)
1650                 g_variant_unref(result);
1651
1652         if (ret != DBUS_RELEASE_NAME_REPLY_RELEASED) {
1653
1654                 if (ret == DBUS_RELEASE_NAME_REPLY_NON_EXISTENT) {
1655                         _LOGE("Port Not exist");
1656                         return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
1657                 } else if (ret == DBUS_RELEASE_NAME_REPLY_NOT_OWNER) {
1658                         _LOGE("Try to release not owned name. MESSAGEPORT_ERROR_INVALID_PARAMETER");
1659                         return MESSAGEPORT_ERROR_INVALID_PARAMETER;
1660                 }
1661         }
1662
1663
1664         g_hash_table_remove(__local_port_info, GINT_TO_POINTER(local_port_id));
1665
1666         return MESSAGEPORT_ERROR_NONE;
1667 }
1668
1669 int messageport_register_local_port(const char *local_port, messageport_message_cb callback)
1670 {
1671         if (!_initialized) {
1672                 if (!__initialize())
1673                         return MESSAGEPORT_ERROR_IO_ERROR;
1674         }
1675
1676         return __register_message_port(local_port, false, callback);
1677 }
1678
1679 int messageport_register_trusted_local_port(const char *local_port, messageport_message_cb callback)
1680 {
1681         if (!_initialized) {
1682                 if (!__initialize())
1683                         return MESSAGEPORT_ERROR_IO_ERROR;
1684         }
1685
1686         return __register_message_port(local_port, true, callback);
1687
1688 }
1689
1690 int messageport_check_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
1691 {
1692         if (!_initialized) {
1693                 if (!__initialize())
1694                         return MESSAGEPORT_ERROR_IO_ERROR;
1695         }
1696
1697         int ret = __check_remote_port(remote_app_id, remote_port, false, exist);
1698         if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
1699                 *exist = false;
1700                 ret = MESSAGEPORT_ERROR_NONE;
1701         }
1702
1703         return ret;
1704 }
1705
1706 int messageport_check_trusted_remote_port(const char *remote_app_id, const char *remote_port, bool *exist)
1707 {
1708         if (!_initialized) {
1709                 if (!__initialize())
1710                         return MESSAGEPORT_ERROR_IO_ERROR;
1711         }
1712
1713         int ret = __check_remote_port(remote_app_id, remote_port, true, exist);
1714         if (ret == MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND) {
1715                 *exist = false;
1716                 ret = MESSAGEPORT_ERROR_NONE;
1717         }
1718
1719         return ret;
1720 }
1721
1722 int messageport_send_message(const char *remote_app_id, const char *remote_port, bundle *message)
1723 {
1724         if (!_initialized) {
1725                 if (!__initialize())
1726                         return MESSAGEPORT_ERROR_IO_ERROR;
1727         }
1728
1729         return __message_port_send_message(remote_app_id, remote_port, NULL, false, false, false, message);
1730 }
1731
1732 int messageport_send_trusted_message(const char *remote_app_id, const char *remote_port, bundle *message)
1733 {
1734         if (!_initialized) {
1735                 if (!__initialize())
1736                         return MESSAGEPORT_ERROR_IO_ERROR;
1737         }
1738
1739         return __message_port_send_message(remote_app_id, remote_port, NULL, true, false, false, message);
1740 }
1741
1742 int messageport_send_bidirectional_message(int id, const char *remote_app_id, const char *remote_port,
1743                 bundle *message)
1744 {
1745         if (!_initialized) {
1746                 if (!__initialize())
1747                         return MESSAGEPORT_ERROR_IO_ERROR;
1748         }
1749
1750         return __message_send_bidirectional_message(id, remote_app_id, remote_port, false, message);
1751 }
1752
1753 int messageport_send_bidirectional_trusted_message(int id, const char *remote_app_id, const char *remote_port,
1754                 bundle *message)
1755 {
1756         if (!_initialized) {
1757                 if (!__initialize())
1758                         return MESSAGEPORT_ERROR_IO_ERROR;
1759         }
1760         return __message_send_bidirectional_message(id, remote_app_id, remote_port, true, message);
1761 }
1762