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