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