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