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