fix launch_on_event failure.
[platform/core/appfw/event-system.git] / src / esd_main.c
1 #include <stdio.h>
2 #include <glib.h>
3 #include <aul.h>
4 #include <unistd.h>
5 #include <ctype.h>
6 #include <dlog.h>
7 #include <Ecore.h>
8 #include <gio/gio.h>
9 #include <package-manager.h>
10 #include <pkgmgr-info.h>
11 #include <appsvc/appsvc.h>
12 #include <eventsystem.h>
13 #include <bundle_internal.h>
14 #include <fcntl.h>
15 #include <vconf.h>
16 #include <tzplatform_config.h>
17 #include <systemd/sd-login.h>
18 #include <cynara-client.h>
19 #include <cynara-creds-gdbus.h>
20 #include <cynara-session.h>
21 #include <security-manager.h>
22 #include "eventsystem_daemon.h"
23
24 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
25 #define ROOT_USER 0
26
27 static GHashTable *event_launch_table; /* table of events for launch_on_event*/
28
29 static const char *event_launch_support_list[] = {
30         SYS_EVENT_BATTERY_CHARGER_STATUS,
31         SYS_EVENT_USB_STATUS,
32         SYS_EVENT_EARJACK_STATUS,
33         SYS_EVENT_INCOMMING_MSG
34 };
35
36 struct privilege_info {
37         const char *event_name;
38         const char *privilege_name;
39 };
40
41 static const struct privilege_info privilege_check_list[] = {
42         {SYS_EVENT_DISPLAY_STATE, "http://tizen.org/privilege/display"},
43         {SYS_EVENT_WIFI_STATE, "http://tizen.org/privilege/network.get"},
44         {SYS_EVENT_INCOMMING_MSG, "http://tizen.org/privilege/message.read"}
45 };
46
47 static int privilege_check_size = sizeof(privilege_check_list)/sizeof(struct privilege_info);
48
49 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
50 static const char *earlier_event_list[] = {
51         SYS_EVENT_ESD_STATUS,
52         SYS_EVENT_LOW_MEMORY,
53         SYS_EVENT_BOOT_COMPLETED,
54         SYS_EVENT_SYSTEM_SHUTDOWN,
55         SYS_EVENT_BATTERY_CHARGER_STATUS
56 };
57
58 static GHashTable *earlier_event_table; /* table of events for earlier_data */
59
60 typedef struct __earlier_table_item {
61         char *event_name;
62         guint reg_id;
63         bundle *earlier_data; /* event-data from earlier occurrence */
64 } earlier_item;
65
66 static bool g_is_bootcompleted = false;
67 #endif
68
69 static GHashTable *trusted_busname_table; /* table of dbus bus-names for trusted user-event */
70
71 typedef struct __trusted_busname_item {
72         char *app_id;
73         char *bus_name;
74         int pid;
75         uid_t uid;
76 } trusted_item;
77
78 typedef struct __eventlaunch_item_param {
79         char *app_id;
80 } eventlaunch_item_param_s;
81
82 typedef struct esd_list_item {
83         char *pkg_id;
84         char *app_id;
85         uid_t uid;
86 } esd_list_item_s;
87
88 typedef struct  __event_launch_table_item {
89         char *event_name;
90         char *package_name; /* just for passing pointer to app-list removal func */
91         GList *app_list_evtlaunch; /* app-list for on-event-launch */
92         guint reg_id;
93         uid_t uid;
94 } event_launch_item;
95
96 enum __pkg_event_type {
97         UNKNOWN = 0,
98         INSTALL,
99         UNINSTALL,
100         UPDATE,
101 };
102
103 typedef struct __pkgmgr_event {
104         int type;
105         char *pkgid;
106 } esd_pkgmgr_event;
107
108 typedef struct __esd_event_param {
109         char *event_name;
110         bundle *event_data;
111         void *user_data;
112 } esd_event_param;
113
114 typedef struct esd_info {
115         pkgmgr_client *client;
116 } esd_info_s;
117 static esd_info_s s_info;
118
119 static Ecore_Fd_Handler *g_fd_handler;
120 sd_login_monitor *g_sd_monitor;
121
122 typedef struct __esd_appctrl_cb_data {
123         char *appid;
124         char *pkgid;
125         uid_t uid;
126 } esd_appctrl_cb_data;
127
128 static void __esd_event_handler(char *event_name, bundle *data, void *user_data);
129 static int __esd_add_appinfo_handler(const pkgmgrinfo_appinfo_h handle, void *data);
130
131 static cynara *r_cynara = NULL;
132
133 static int __esd_init_cynara(void)
134 {
135         int ret;
136
137         ret  = cynara_initialize(&r_cynara, NULL);
138         if (ret != CYNARA_API_SUCCESS) {
139                 _E("cynara initialize failed.");
140                 return ret;
141         }
142
143         return 0;
144 }
145
146 static void __esd_finish_cynara(void)
147 {
148         if (r_cynara)
149                 cynara_finish(r_cynara);
150         r_cynara = NULL;
151 }
152
153 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
154 static int __esd_check_earlier_support(const char *event_name)
155 {
156         int i = 0;
157         int size = sizeof(earlier_event_list)/sizeof(*earlier_event_list);
158
159         for (i = 0; i < size; i++) {
160                 if (strcmp(earlier_event_list[i], event_name) == 0)
161                         return true;
162         }
163
164         return false;
165 }
166 #endif
167
168 static int __esd_check_event_launch_support(const char *event_name)
169 {
170         int i = 0;
171         int size = sizeof(event_launch_support_list)/sizeof(*event_launch_support_list);
172
173         for (i = 0; i < size; i++) {
174                 if (strcmp(event_launch_support_list[i], event_name) == 0)
175                         return true;
176         }
177
178         return false;
179 }
180
181 static int __get_sender_unixinfo(GDBusConnection *conn, const char *sender_name, const char *type)
182 {
183         GDBusMessage *msg = NULL;
184         GDBusMessage *reply = NULL;
185         GError *err = NULL;
186         GVariant *body;
187         int ret = -1;
188         unsigned int value;
189
190         msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
191                 "org.freedesktop.DBus", type);
192         if (!msg) {
193                 _E("Can't allocate new method call");
194                 goto out;
195         }
196
197         g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
198         reply = g_dbus_connection_send_message_with_reply_sync(conn, msg,
199                 G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err);
200
201         if (!reply) {
202                 if (err != NULL) {
203                         _E("Failed to get info [%s]", err->message);
204                         g_error_free(err);
205                 }
206                 goto out;
207         }
208
209         body = g_dbus_message_get_body(reply);
210         g_variant_get(body, "(u)", &value);
211         ret = (int)value;
212
213 out:
214         if (msg)
215                 g_object_unref(msg);
216         if (reply)
217                 g_object_unref(reply);
218
219         return ret;
220 }
221
222 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
223 {
224         int pid = 0;
225
226         pid = __get_sender_unixinfo(conn, sender_name, "GetConnectionUnixProcessID");
227         if (pid < 0) {
228                 _E("failed to get pid");
229                 pid = 0;
230         }
231
232         _D("sender_name(%s), pid(%d)", sender_name, pid);
233
234         return pid;
235 }
236
237 static int __get_sender_uid(GDBusConnection *conn, const char *sender_name)
238 {
239         int uid = -1;
240
241         uid = __get_sender_unixinfo(conn, sender_name, "GetConnectionUnixUser");
242         if (uid < 0) {
243                 _E("failed to get uid");
244         }
245
246         _D("sender_name(%s), uid(%d)", sender_name, uid);
247
248         return uid;
249 }
250
251 static int __esd_check_certificate_match(uid_t uid, const char *app_id, uid_t from_uid, const char *from_appid)
252 {
253         pkgmgrinfo_cert_compare_result_type_e res;
254         int ret = 0;
255
256         _D("uid(%d), app_id(%s), from_uid(%d), from_appid(%s)", uid, app_id, from_uid, from_appid);
257
258         ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(app_id, from_appid, from_uid, &res);
259         if (ret < 0) {
260                 _E("failed to check certificate");
261                 return ES_R_ERROR;
262         }
263
264         if (res != PMINFO_CERT_COMPARE_MATCH) {
265                 _D("certificat not match (%s)", app_id);
266                 return ES_R_EINVAL;
267         }
268
269         return ES_R_OK;
270 }
271
272 static bool __esd_check_application_validation(uid_t uid, const char *appid)
273 {
274         int ret = 0;
275         pkgmgrinfo_appinfo_h handle;
276
277         ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &handle);
278         if (ret != PMINFO_R_OK)
279                 return false;
280
281         pkgmgrinfo_appinfo_destroy_appinfo(handle);
282
283         if (!aul_app_is_running_for_uid(appid, uid))
284                 return false;
285
286         return true;
287 }
288
289 static void __esd_trusted_busname_print_items(void)
290 {
291         GHashTableIter iter;
292         gpointer key;
293         gpointer value;
294
295         g_hash_table_iter_init(&iter, trusted_busname_table);
296
297         while (g_hash_table_iter_next(&iter, &key, &value)) {
298                 trusted_item *item = (trusted_item *)value;
299                 if (item)
300                         _D("uid(%d), appid(%s), pid(%d), busname(%s)", item->uid, item->app_id, item->pid, item->bus_name);
301         }
302 }
303
304 static int __esd_trusted_busname_add_item(uid_t uid, const char *appid, const char *busname, int pid)
305 {
306         char *app_id = NULL;
307         char *bus_name = NULL;
308         trusted_item *item = NULL;
309         trusted_item *new_item;
310
311         app_id = strdup(appid);
312         if (app_id == NULL) {
313                 _E("out of memory");
314                 return ES_R_ENOMEM;
315         }
316
317         bus_name = strdup(busname);
318         if (bus_name == NULL) {
319                 _E("out of memory");
320                 FREE_AND_NULL(app_id);
321                 return ES_R_ENOMEM;
322         }
323
324         item = (trusted_item *)g_hash_table_lookup(trusted_busname_table, app_id);
325
326         if (item && item->bus_name && strcmp(item->bus_name, bus_name) == 0 &&
327                 (item->uid == uid)) {
328                 _D("already exist (%s, %s)", app_id, bus_name);
329                 FREE_AND_NULL(app_id);
330                 FREE_AND_NULL(bus_name);
331         } else {
332                 new_item = calloc(1, sizeof(trusted_item));
333                 if (new_item == NULL) {
334                         _E("memory alloc failed");
335                         FREE_AND_NULL(app_id);
336                         FREE_AND_NULL(bus_name);
337                         return ES_R_ENOMEM;
338                 }
339                 new_item->uid = uid;
340                 new_item->app_id = app_id;
341                 new_item->bus_name = bus_name;
342                 new_item->pid = pid;
343                 g_hash_table_insert(trusted_busname_table, new_item->app_id, new_item);
344                 _D("added busname(%s)", new_item->bus_name);
345         }
346
347         return ES_R_OK;
348 }
349
350 static int __esd_check_trusted_events(GDBusConnection *conn, const char *list_name)
351 {
352         GVariant *result;
353         GError *error = NULL;
354         GVariantIter *iter;
355         gchar *str;
356         char tmp_appid[128] = {0, };
357         int pid = 0;
358         int uid = 0;
359         int ret = 0;
360
361         result = g_dbus_connection_call_sync(conn,
362                 "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus",
363                 list_name, NULL, G_VARIANT_TYPE("(as)"), G_DBUS_CALL_FLAGS_NONE,
364                 -1, NULL, &error);
365
366         if (result == NULL) {
367                 _E("get (%s) error(%s)", list_name, error->message);
368                 g_error_free(error);
369                 return ES_R_ERROR;
370         }
371
372         g_variant_get(result, "(as)", &iter);
373         while (g_variant_iter_loop(iter, "s", &str)) {
374                 if (!strstr((const char *)str, "event.busname.session"))
375                         continue;
376
377                 _D("list(%s), name(%s)", list_name, str);
378                 pid = __get_sender_pid(conn, (const char *)str);
379                 if (pid <= 0) {
380                         _E("failed to get pid(%d)", pid);
381                         continue;
382                 }
383
384                 uid = __get_sender_uid(conn, (const char *)str);
385                 if (uid < 0) {
386                         _E("failed to get uid(%d)", uid);
387                         continue;
388                 }
389                 _D("uid(%d)", uid);
390
391                 memset(tmp_appid, 0, sizeof(tmp_appid));
392                 ret = aul_app_get_appid_bypid_for_uid(pid, tmp_appid, sizeof(tmp_appid), (uid_t)uid);
393                 if (ret != AUL_R_OK) {
394                         _E("failed to get appid by pid(%d)", pid);
395                         continue;
396                 }
397
398                 _D("appid(%s)", tmp_appid);
399                 if (__esd_check_application_validation((uid_t)uid, tmp_appid)) {
400                         _D("add to table");
401                         ret = __esd_trusted_busname_add_item((uid_t)uid, tmp_appid, (const char *)str, pid);
402                         if (ret < 0)
403                                 _E("failed to add item");
404                 }
405         }
406         g_variant_iter_free(iter);
407         g_variant_unref(result);
408
409         return ES_R_OK;
410 }
411
412 static int __esd_check_privilege_name(const char *event_name, char **privilege_name)
413 {
414         int i = 0;
415
416         *privilege_name = NULL;
417
418         for (i = 0; i < privilege_check_size; i++) {
419                 if (strcmp(event_name, privilege_check_list[i].event_name) == 0) {
420                         *privilege_name = (char *)privilege_check_list[i].privilege_name;
421                         _D("[%d] privilege_name(%s)", i, *privilege_name);
422                         break;
423                 }
424         }
425
426         return ES_R_OK;
427 }
428
429 static bool __esd_check_valid_privilege_by_cynara(const char *appid, const char *client,
430         const char *session, const char *user, const char *privilege_name)
431 {
432         int ret = 0;
433         bool has_privilege = false;
434
435         _D("check privilege, (%s, %s, %s, %s, %s)", appid, client, session, user, privilege_name);
436
437         ret = cynara_check(r_cynara, client, session, user, privilege_name);
438         if (ret == CYNARA_API_ACCESS_ALLOWED) {
439                 _D("valid privilege");
440                 has_privilege = true;
441         } else if (ret == CYNARA_API_ACCESS_DENIED) {
442                 _E("invalid privilege");
443         } else {
444                 _E("failed to check privilege, error(%d)", ret);
445         }
446
447         return has_privilege;
448 }
449
450 static int __esd_check_app_privileged_event(uid_t uid, const char *appid, const char *pkgid, const char *event_name)
451 {
452         char *privilege_name = NULL;
453         int ret = 0;
454         int result = 0;
455
456         _D("event_name(%s), uid(%d), appid(%s), pkgid(%s)", event_name, uid, appid, pkgid);
457
458         __esd_check_privilege_name(event_name, &privilege_name);
459
460         if (privilege_name) {
461                 ret = security_manager_app_has_privilege(appid, privilege_name, uid, &result);
462                 if (ret != SECURITY_MANAGER_SUCCESS)
463                         _E("failed to check privilege(%d)", ret);
464                 _D("result(%d)", result);
465         } else {
466                 result = 1;
467         }
468
469         return result;
470 }
471
472 static void __esd_print_appid_with_eventid(gpointer data, gpointer user_data)
473 {
474         esd_list_item_s *item = (esd_list_item_s *)data;
475         char *event_name = (char *)user_data;
476
477         _D("event_name(%s)-uid(%d)-app_id(%s)-pkg_id(%s)", event_name, item->uid, item->app_id, item->pkg_id);
478 }
479
480 static void __esd_print_interested_event(gpointer data, gpointer user_data)
481 {
482         event_launch_item *el_item = (event_launch_item *)data;
483         char *event_name = (char *)el_item->event_name;
484         _D("event_name = (%s)", event_name);
485         g_list_foreach(el_item->app_list_evtlaunch, __esd_print_appid_with_eventid, event_name);
486 }
487
488 static void __esd_launch_table_print_items(void)
489 {
490         GHashTableIter iter;
491         gpointer key;
492         gpointer value;
493
494         g_hash_table_iter_init(&iter, event_launch_table);
495
496         while (g_hash_table_iter_next(&iter, &key, &value))
497                 __esd_print_interested_event(value, NULL);
498 }
499
500 static int __esd_find_compare_by_list_item(gconstpointer data, gconstpointer user_data)
501 {
502         esd_list_item_s *item_1 = (esd_list_item_s *)user_data;
503         esd_list_item_s *item_2 = (esd_list_item_s *)data;
504
505         return (item_1->uid != item_2->uid) |
506                 strcmp(item_1->app_id, item_2->app_id) |
507                 strcmp(item_1->pkg_id, item_2->pkg_id);
508 }
509
510 static int __esd_add_list_item(uid_t uid, event_launch_item *el_item,
511                 const char *app_id, const char *pkg_id)
512 {
513         esd_list_item_s *item_of_list = NULL;
514
515         item_of_list = calloc(1, sizeof(esd_list_item_s));
516         if (item_of_list == NULL) {
517                 _E("out_of_memory");
518                 return ES_R_ENOMEM;
519         }
520         item_of_list->uid = uid;
521         item_of_list->app_id = (char *)app_id;
522         item_of_list->pkg_id = (char *)pkg_id;
523         el_item->app_list_evtlaunch =
524                 g_list_append(el_item->app_list_evtlaunch, item_of_list);
525
526         return ES_R_OK;
527 }
528
529 static int __esd_add_launch_item(uid_t uid, const char *event_name, const char *appid, const char *pkgid)
530 {
531         GList *app_list = NULL;
532         guint subscription_id = 0;
533         char *app_id = NULL;
534         char *pkg_id = NULL;
535         esd_list_item_s *item_of_list = NULL;
536         event_launch_item *eli;
537         event_launch_item *el_item =
538                 (event_launch_item *)g_hash_table_lookup(event_launch_table, event_name);
539
540         if (el_item) {
541                 item_of_list = calloc(1, sizeof(esd_list_item_s));
542                 if (item_of_list == NULL) {
543                         _E("memory alloc failed");
544                         return ES_R_ENOMEM;
545                 }
546                 item_of_list->uid = uid;
547                 item_of_list->app_id = (char *)appid;
548                 item_of_list->pkg_id = (char *)pkgid;
549
550                 app_list = g_list_find_custom(el_item->app_list_evtlaunch,
551                         item_of_list, (GCompareFunc)__esd_find_compare_by_list_item);
552                 free(item_of_list);
553                 if (app_list == NULL) {
554                         _D("add new item (list item only)");
555                         app_id = strdup((char *)appid);
556                         if (!app_id) {
557                                 _E("out_of_memory");
558                                 return ES_R_ENOMEM;
559                         }
560                         pkg_id = strdup((char *)pkgid);
561                         if (!pkg_id) {
562                                 _E("out_of_memory");
563                                 FREE_AND_NULL(app_id);
564                                 return ES_R_ENOMEM;
565                         }
566                         if (__esd_add_list_item(uid, el_item, app_id, pkg_id) < 0) {
567                                 _E("failed to add list item");
568                                 FREE_AND_NULL(app_id);
569                                 FREE_AND_NULL(pkg_id);
570                                 return ES_R_ERROR;
571                         }
572                 }
573         } else {
574                 _D("add new item (all)");
575                 eli = calloc(1, sizeof(event_launch_item));
576                 if (!eli) {
577                         _E("memory alloc failed");
578                         return ES_R_ENOMEM;
579                 }
580
581                 eli->event_name = strdup(event_name);
582                 if (!eli->event_name) {
583                         _E("out_of_memory");
584                         FREE_AND_NULL(eli);
585                         return ES_R_ENOMEM;
586                 }
587
588                 app_id = strdup((char *)appid);
589                 if (!app_id) {
590                         _E("out_of_memory");
591                         FREE_AND_NULL(eli->event_name);
592                         FREE_AND_NULL(eli);
593                         return ES_R_ENOMEM;
594                 }
595
596                 pkg_id = strdup((char *)pkgid);
597                 if (!pkg_id) {
598                         _E("out_of_memory");
599                         FREE_AND_NULL(app_id);
600                         FREE_AND_NULL(eli->event_name);
601                         FREE_AND_NULL(eli);
602                         return ES_R_ENOMEM;
603                 }
604
605                 if (__esd_add_list_item(uid, eli, app_id, pkg_id) < 0) {
606                         _E("failed to add list item");
607                         FREE_AND_NULL(app_id);
608                         FREE_AND_NULL(pkg_id);
609                         FREE_AND_NULL(eli->event_name);
610                         FREE_AND_NULL(eli);
611                         return ES_R_ERROR;
612                 }
613
614                 g_hash_table_insert(event_launch_table, eli->event_name, eli);
615
616                 eventsystem_register_event(eli->event_name, &subscription_id,
617                         (eventsystem_handler)__esd_event_handler, NULL);
618                 if (subscription_id == 0) {
619                         _E("signal subscription error, event_name(%s), app_id(%s)",
620                                 eli->event_name, app_id);
621                         return ES_R_ERROR;
622                 } else {
623                         eli->reg_id = subscription_id;
624                 }
625         }
626
627         return ES_R_OK;
628 }
629
630 static void __esd_remove_all_private_usr_app_list(gpointer data, gpointer user_data)
631 {
632         esd_list_item_s *item = (esd_list_item_s *)data;
633         event_launch_item *eli = (event_launch_item *)user_data;
634
635         if (item->uid != GLOBAL_USER && !strcmp(eli->package_name, item->pkg_id)) {
636                 _D("uid(%d), app_id(%s), pkg_id(%s)", item->uid, item->app_id, eli->package_name);
637                 eli->app_list_evtlaunch = g_list_remove_all(eli->app_list_evtlaunch, data);
638         }
639 }
640
641 static int __esd_launch_table_remove_private_usr_items(void)
642 {
643         GHashTableIter iter;
644         gpointer key;
645         gpointer value;
646         event_launch_item *eli = NULL;
647         GList *first_list = NULL;
648
649         g_hash_table_iter_init(&iter, event_launch_table);
650
651         while (g_hash_table_iter_next(&iter, &key, &value)) {
652                 eli = (event_launch_item *)value;
653                 g_list_foreach(eli->app_list_evtlaunch, __esd_remove_all_private_usr_app_list, eli);
654
655                 first_list = g_list_first(eli->app_list_evtlaunch);
656                 if (first_list == NULL) {
657                         if (eli->reg_id)
658                                 eventsystem_unregister_event(eli->reg_id);
659
660                         g_hash_table_iter_remove(&iter);
661                 }
662         }
663
664         return ES_R_OK;
665 }
666
667 static void __esd_remove_app_list(gpointer data, gpointer user_data)
668 {
669         bool skip = false;
670         esd_list_item_s *item = (esd_list_item_s *)data;
671         event_launch_item *eli = (event_launch_item *)user_data;
672
673         if (eli->uid != GLOBAL_USER && eli->uid != item->uid)
674                 skip = true;
675
676         if (!skip && !strcmp(eli->package_name, item->pkg_id)) {
677                 _D("pkg_id(%s), app_id(%s)", eli->package_name, item->app_id);
678                 eli->app_list_evtlaunch =
679                         g_list_remove_all(eli->app_list_evtlaunch, data);
680         }
681 }
682
683 static int __esd_remove_launch_item(uid_t uid, gpointer data, const char *pkg_id)
684 {
685         event_launch_item *eli = (event_launch_item *)data;
686         GList *first_list = NULL;
687
688         eli->uid = uid;
689         eli->package_name = (char *)pkg_id;
690         g_list_foreach(eli->app_list_evtlaunch, __esd_remove_app_list, eli);
691
692         first_list = g_list_first(eli->app_list_evtlaunch);
693         if (first_list == NULL) {
694                 if (eli->reg_id)
695                         eventsystem_unregister_event(eli->reg_id);
696
697                 return ES_R_REMOVE;
698         }
699
700         return ES_R_OK;
701 }
702
703 static int __esd_launch_table_remove_items(uid_t uid, const char *pkg_id)
704 {
705         GHashTableIter iter;
706         gpointer key;
707         gpointer value;
708
709         g_hash_table_iter_init(&iter, event_launch_table);
710
711         while (g_hash_table_iter_next(&iter, &key, &value)) {
712                 if (__esd_remove_launch_item(uid, value, pkg_id) == ES_R_REMOVE) {
713                         _D("remove item itself");
714                         g_hash_table_iter_remove(&iter);
715                 }
716         }
717
718         return ES_R_OK;
719 }
720
721 static void __esd_event_launch_with_appid(gpointer data, gpointer user_data)
722 {
723         esd_list_item_s *item = (esd_list_item_s *)data;
724         uid_t uid = item->uid;
725         char *app_id = item->app_id;
726         esd_event_param *eep = (esd_event_param *)user_data;
727         static unsigned int req_id;
728         int pid;
729         char event_uri[1024];
730         bundle *b;
731
732         _D("launch_on_event: app_id(%s), event_name(%s), uid(%d)",
733                         app_id, eep->event_name, uid);
734
735         if (!aul_app_is_running_for_uid(app_id, uid)) {
736                 snprintf(event_uri, sizeof(event_uri), "event://%s", eep->event_name);
737                 b = bundle_dup(eep->event_data);
738                 appsvc_set_operation(b, APPSVC_OPERATION_LAUNCH_ON_EVENT);
739                 appsvc_set_uri(b, event_uri);
740                 appsvc_set_appid(b, app_id);
741
742                 pid = appsvc_usr_run_service(b, req_id++, NULL, eep->user_data, uid);
743                 _D("uid(%d), pid(%d)", uid, pid);
744
745                 bundle_free(b);
746         } else {
747                 _D("already is running or launch failed");
748         }
749 }
750
751 static void __esd_check_event_launch_with_eventid(gpointer data, gpointer user_data)
752 {
753         event_launch_item *el_item = (event_launch_item *)data;
754         esd_event_param *eep = (esd_event_param *)user_data;
755
756         if (strcmp(eep->event_name, (char *)el_item->event_name) == 0) {
757                 g_list_foreach(el_item->app_list_evtlaunch,
758                         __esd_event_launch_with_appid, user_data);
759         }
760 }
761
762 static void __esd_launch_event_handler(char *event_name, bundle *data, void *user_data)
763 {
764         const char *val;
765         const char *msg_type;
766         const char *msg_id;
767         esd_event_param *eep;
768         event_launch_item *el_item;
769
770         _D("event_name(%s)", event_name);
771
772         el_item = (event_launch_item *)g_hash_table_lookup(event_launch_table, event_name);
773         if (el_item == NULL)
774                 return;
775
776         if (el_item->app_list_evtlaunch != NULL) {
777                 if (strcmp(SYS_EVENT_BATTERY_CHARGER_STATUS, event_name) == 0) {
778                         val = bundle_get_val(data, EVT_KEY_BATTERY_CHARGER_STATUS);
779                         _D("charger val(%s)", val);
780                         if (strcmp(EVT_VAL_BATTERY_CHARGER_CONNECTED, val) != 0)
781                                 return;
782                 } else if (strcmp(SYS_EVENT_USB_STATUS, event_name) == 0) {
783                         val = bundle_get_val(data, EVT_KEY_USB_STATUS);
784                         _D("usb val(%s)", val);
785                         if (strcmp(EVT_VAL_USB_CONNECTED, val) != 0)
786                                 return;
787                 } else if (strcmp(SYS_EVENT_EARJACK_STATUS, event_name) == 0) {
788                         val = bundle_get_val(data, EVT_KEY_EARJACK_STATUS);
789                         _D("earjack val(%s)", val);
790                         if (strcmp(EVT_VAL_EARJACK_CONNECTED, val) != 0)
791                                 return;
792                 } else if (strcmp(SYS_EVENT_INCOMMING_MSG, event_name) == 0) {
793                         msg_type = bundle_get_val(data, EVT_KEY_MSG_TYPE);
794                         _D("msg_type(%s)", msg_type);
795                         if (msg_type == NULL)
796                                 return;
797
798                         msg_id = bundle_get_val(data, EVT_KEY_MSG_ID);
799                         _D("msg_id(%s)", msg_id);
800                         if (msg_id == NULL)
801                                 return;
802                 }
803
804                 eep = calloc(1, sizeof(esd_event_param));
805                 if (!eep) {
806                         _E("memory alloc failed");
807                         return;
808                 }
809                 eep->event_name = event_name;
810                 eep->event_data = data;
811                 eep->user_data = (void *)user_data;
812                 __esd_check_event_launch_with_eventid(el_item, eep);
813                 free(eep);
814         }
815 }
816
817 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
818 static void __esd_print_earlier_event(gpointer data, gpointer user_data)
819 {
820         earlier_item *item = (earlier_item *)data;
821         char *event_name = (char *)item->event_name;
822         const char *val;
823
824         _D("event_name = (%s)", event_name);
825
826         if (strcmp(event_name, SYS_EVENT_BOOT_COMPLETED) == 0) {
827                 if (item->earlier_data) {
828                         val = bundle_get_val(item->earlier_data, EVT_KEY_BOOT_COMPLETED);
829                         _D("boot_completed(%s)", val);
830                 }
831         } else if (strcmp(event_name, SYS_EVENT_SYSTEM_SHUTDOWN) == 0) {
832                 if (item->earlier_data) {
833                         val = bundle_get_val(item->earlier_data, EVT_KEY_SYSTEM_SHUTDOWN);
834                         _D("shutdown(%s)", val);
835                 }
836         } else if (strcmp(event_name, SYS_EVENT_LOW_MEMORY) == 0) {
837                 if (item->earlier_data) {
838                         val = bundle_get_val(item->earlier_data, EVT_KEY_LOW_MEMORY);
839                         _D("low_memory(%s)", val);
840                 }
841         } else if (strcmp(event_name, SYS_EVENT_BATTERY_CHARGER_STATUS) == 0) {
842                 if (item->earlier_data) {
843                         val = bundle_get_val(item->earlier_data, EVT_KEY_BATTERY_CHARGER_STATUS);
844                         _D("charger_status(%s)", val);
845                 }
846         }
847 }
848
849 static void __esd_earlier_table_print_items(void)
850 {
851         GHashTableIter iter;
852         gpointer key;
853         gpointer value;
854
855         g_hash_table_iter_init(&iter, earlier_event_table);
856
857         while (g_hash_table_iter_next(&iter, &key, &value))
858                 __esd_print_earlier_event(value, NULL);
859 }
860
861 static void __esd_earlier_event_handler(char *event_name, bundle *data, void *user_data)
862 {
863         int handle;
864         earlier_item *item;
865         _D("event_name(%s)", event_name);
866
867         item = (earlier_item *)g_hash_table_lookup(earlier_event_table, event_name);
868         if (item) {
869                 /* update earlier value */
870                 if (item->earlier_data != NULL)
871                         bundle_free(item->earlier_data);
872
873                 item->earlier_data = bundle_dup(data);
874
875                 if (!g_is_bootcompleted) {
876                         if (strcmp(event_name, SYS_EVENT_BOOT_COMPLETED) == 0) {
877                                 handle = creat(ESD_BOOT_COMPLETED, 0640);
878                                 if (handle != -1)
879                                         close(handle);
880                                 g_is_bootcompleted = true;
881                         }
882                 }
883         }
884 }
885 #endif
886
887 static void __esd_event_handler(char *event_name, bundle *data, void *user_data)
888 {
889         _D("event_name(%s)", event_name);
890
891 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
892         if (__esd_check_earlier_support(event_name))
893                 __esd_earlier_event_handler(event_name, data, user_data);
894 #endif
895
896         if (__esd_check_event_launch_support(event_name))
897                 __esd_launch_event_handler(event_name, data, user_data);
898 }
899
900 static void __esd_trusted_busname_remove_item(char *bus_name)
901 {
902         GHashTableIter iter;
903         gpointer key;
904         gpointer value;
905         trusted_item *item;
906
907         g_hash_table_iter_init(&iter, trusted_busname_table);
908
909         while (g_hash_table_iter_next(&iter, &key, &value)) {
910                 item = (trusted_item *)value;
911                 if (item) {
912                         if (strcmp(bus_name, item->bus_name) == 0) {
913                                 _D("remove trusted busname item(%s, %s)", item->app_id, item->bus_name);
914                                 FREE_AND_NULL(item->app_id);
915                                 FREE_AND_NULL(item->bus_name);
916                                 FREE_AND_NULL(item);
917                                 g_hash_table_iter_remove(&iter);
918
919                                 __esd_trusted_busname_print_items();
920                         }
921                 }
922         }
923 }
924
925 static void __esd_filter_name_owner_changed(GDBusConnection *connection,
926                 const gchar *sender_name, const gchar *object_path,
927                 const gchar *interface_name, const gchar *signal_name,
928                 GVariant *parameters, gpointer user_data)
929 {
930         char *name = NULL;
931         char *old_owner = NULL;
932         char *new_owner = NULL;
933         int old_len = 0;
934         int new_len = 0;
935
936         g_variant_get(parameters, "(sss)", &name, &old_owner, &new_owner);
937
938         if (strstr(name, "event.busname.session")) {
939                 old_len = strlen(old_owner);
940                 new_len = strlen(new_owner);
941
942                 _D("changed name(%s), old_onwer(%s)(%d) -> new_onwer(%s)(%d)",
943                         name, old_owner, old_len, new_owner, new_len);
944
945                 if (old_len > 0 && new_len == 0)
946                         __esd_trusted_busname_remove_item(name);
947                 else if (old_len == 0 && new_len > 0)
948                         _D("new name owned");
949                 else
950                         _E("not-expected name change");
951         }
952 }
953
954 static int __esd_dbus_name_monitor(GDBusConnection *connection)
955 {
956         guint name_owner_changed_id = 0;
957
958         name_owner_changed_id = g_dbus_connection_signal_subscribe(connection,
959                 "org.freedesktop.DBus", "org.freedesktop.DBus",
960                 "NameOwnerChanged", "/org/freedesktop/DBus", NULL, G_DBUS_SIGNAL_FLAGS_NONE,
961                 __esd_filter_name_owner_changed, NULL, NULL);
962
963         _I("name_owner_changed_id(%d)", name_owner_changed_id);
964
965         return ES_R_OK;
966 }
967
968 static int __esd_get_user_items(void)
969 {
970         int ret = 0;
971         int i = 0;
972         uid_t *uids = NULL;
973         uid_t cur_uid = 0;
974
975         ret = sd_get_uids(&uids);
976         if (ret < 0) {
977                 _E("failed to get uids (%d)", ret);
978                 return ES_R_ERROR;
979         }
980
981         if (ret == 0 || uids == NULL) {
982                 _I("there is no uid for now");
983         } else {
984                 /* reset user's item */
985                 __esd_launch_table_remove_private_usr_items();
986                 for (i = 0; i < ret; i++) {
987                         cur_uid = uids[i];
988                         _I("found uid(%d)", cur_uid);
989
990                         ret = pkgmgrinfo_appinfo_get_usr_installed_list(__esd_add_appinfo_handler, cur_uid, &cur_uid);
991                         if (ret < 0)
992                                 _E("failed to get user(%d)-app list (%d)", cur_uid, ret);
993                 }
994         }
995
996         __esd_launch_table_print_items();
997
998         return ES_R_OK;
999 }
1000
1001 static Eina_Bool __esd_fd_handler_func(void *data, Ecore_Fd_Handler *fd_handler)
1002 {
1003         if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ)) {
1004                 _I("fd read");
1005                 __esd_get_user_items();
1006         }
1007
1008         return ECORE_CALLBACK_CANCEL;
1009 }
1010
1011 static int __esd_start_sd_monitor(void)
1012 {
1013         int ret = 0;
1014         int fd = 0;
1015
1016         ret = __esd_get_user_items();
1017         if (ret < 0)
1018                 return ES_R_ERROR;
1019
1020         ret = sd_login_monitor_new("uid", &g_sd_monitor);
1021         if (ret < 0) {
1022                 _E("sd_login_monitor_new error (%d)", ret);
1023                 return ES_R_ERROR;
1024         }
1025
1026         fd = sd_login_monitor_get_fd(g_sd_monitor);
1027         if (fd < 0) {
1028                 _E("sd_login_monitor_get_fd error");
1029                 sd_login_monitor_unref(g_sd_monitor);
1030                 return ES_R_ERROR;
1031         }
1032
1033         g_fd_handler = ecore_main_fd_handler_add(fd,
1034                 (Ecore_Fd_Handler_Flags)(ECORE_FD_READ | ECORE_FD_ERROR),
1035                 __esd_fd_handler_func, NULL, NULL, NULL);
1036         if (g_fd_handler == NULL) {
1037                 _E("fd_handler is NULL");
1038                 sd_login_monitor_unref(g_sd_monitor);
1039                 return ES_R_ERROR;
1040         }
1041
1042         _I("setup sd-monitor done");
1043
1044         return ES_R_OK;
1045 }
1046
1047 static int __esd_stop_sd_monitor(void)
1048 {
1049         _I("stop sd_monitor");
1050         if (g_fd_handler) {
1051                 ecore_main_fd_handler_del(g_fd_handler);
1052                 g_fd_handler = NULL;
1053         }
1054
1055         sd_login_monitor_unref(g_sd_monitor);
1056         g_sd_monitor = 0;
1057
1058         return ES_R_OK;
1059 }
1060
1061 static GDBusNodeInfo *introspection_data;
1062 static const gchar introspection_xml[] =
1063 "<node>"
1064 "       <interface name='tizen.system.event.app2esd'>"
1065 "               <method name='CheckSenderValidation'>"
1066 "                       <arg type='i' name='senderpid' direction='in'/>"
1067 "                       <arg type='s' name='eventname' direction='in'/>"
1068 "                       <arg type='i' name='ret' direction='out'/>"
1069 "                       <arg type='s' name='senderid' direction='out'/>"
1070 "               </method>"
1071 "               <method name='GetTrustedPeerList'>"
1072 "                       <arg type='s' name='eventname' direction='in'/>"
1073 "                       <arg type='i' name='ret' direction='out'/>"
1074 "                       <arg type='as' name='dest_list' direction='out'/>"
1075 "               </method>"
1076 "               <method name='SetupTrustedPeer'>"
1077 "                       <arg type='s' name='eventname' direction='in'/>"
1078 "                       <arg type='s' name='destination' direction='in'/>"
1079 "                       <arg type='i' name='ret' direction='out'/>"
1080 "               </method>"
1081 "               <method name='CheckPrivilegeValidation'>"
1082 "                       <arg type='s' name='eventname' direction='in'/>"
1083 "                       <arg type='i' name='ret' direction='out'/>"
1084 "               </method>"
1085 "               <method name='CheckUserSendValidation'>"
1086 "                       <arg type='s' name='eventname' direction='in'/>"
1087 "                       <arg type='i' name='ret' direction='out'/>"
1088 "               </method>"
1089 "               <method name='RequestTrustedEventLaunch'>"
1090 "                       <arg type='s' name='eventname' direction='in'/>"
1091 "                       <arg type='s' name='eventdata' direction='in'/>"
1092 "                       <arg type='i' name='datalen' direction='in'/>"
1093 "                       <arg type='i' name='ret' direction='out'/>"
1094 "               </method>"
1095 "               <method name='RequestEventLaunch'>"
1096 "                       <arg type='s' name='eventname' direction='in'/>"
1097 "                       <arg type='s' name='eventdata' direction='in'/>"
1098 "                       <arg type='i' name='datalen' direction='in'/>"
1099 "                       <arg type='i' name='ret' direction='out'/>"
1100 "               </method>"
1101 "               <method name='RequestSendingEvent'>"
1102 "                       <arg type='s' name='eventname' direction='in'/>"
1103 "                       <arg type='s' name='eventdata' direction='in'/>"
1104 "                       <arg type='i' name='datalen' direction='in'/>"
1105 "                       <arg type='i' name='ret' direction='out'/>"
1106 "               </method>"
1107 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
1108 "               <method name='GetEarlierData'>"
1109 "                       <arg type='s' name='appid' direction='in'/>"
1110 "                       <arg type='i' name='ret' direction='out'/>"
1111 "                       <arg type='i' name='len' direction='out'/>"
1112 "                       <arg type='s' name='earlier_data' direction='out'/>"
1113 "               </method>"
1114 #endif
1115 "       </interface>"
1116 "</node>";
1117
1118 static int __esd_get_appid_by_pid(int pid, uid_t uid, char *app_id, int buf_size)
1119 {
1120         int retval = ES_R_OK;
1121         int ret = 0;
1122
1123         if (pid <= 0) {
1124                 _E("invalid pid(%d)", pid);
1125                 retval = ES_R_ERROR;
1126         } else if (uid <= 0) {
1127                 _E("invalid uid(%d)", uid);
1128                 retval = ES_R_ERROR;
1129         } else {
1130                 ret = aul_app_get_appid_bypid_for_uid(pid, app_id, buf_size, (uid_t)uid);
1131                 if (ret != AUL_R_OK) {
1132                         _E("failed to get appid by pid");
1133                         retval = ES_R_ERROR;
1134                 }
1135                 _D("pid(%d)-uid(%d)-appid(%s)", pid, uid, app_id);
1136         }
1137
1138         return retval;
1139 }
1140
1141 static int check_user_event_sender_valid(const char *event_name, const char *app_id)
1142 {
1143         char *valid_name = NULL;
1144         char *temp_name = NULL;
1145         char *tmp = NULL;
1146         int retval = ES_R_OK;
1147         int len = 0;
1148         int valid_name_len = 0;
1149
1150         temp_name = strdup(event_name);
1151         if (temp_name == NULL) {
1152                 _E("out of memory");
1153                 return ES_R_ENOMEM;
1154         }
1155
1156         tmp = strrchr(temp_name, '.');
1157         if (tmp == NULL || strlen(tmp) == 0) {
1158                 _E("invalid event name");
1159                 FREE_AND_NULL(temp_name);
1160                 return ES_R_EINVAL;
1161         }
1162         len = strlen(tmp);
1163         if (len <= 1 || len > 128) {
1164                 _E("invalid length(%d) of user-defined name");
1165                 FREE_AND_NULL(temp_name);
1166                 return ES_R_EINVAL;
1167         }
1168         *tmp = '\0';
1169
1170         _D("app_id(%s), len(%d)", app_id, strlen(app_id));
1171
1172         valid_name_len = strlen(USER_EVENT_NAME_PREFIX) + strlen(app_id) + 1;
1173         valid_name = calloc(1, valid_name_len);
1174         if (valid_name == NULL) {
1175                 _E("memory alloc failed");
1176                 FREE_AND_NULL(temp_name);
1177                 return ES_R_ENOMEM;
1178         }
1179         snprintf(valid_name, valid_name_len, "%s%s", USER_EVENT_NAME_PREFIX, app_id);
1180         _D("valid_name(%s)", valid_name);
1181
1182         if (strcmp(temp_name, valid_name) != 0) {
1183                 _E("appid misamatch");
1184                 retval = ES_R_EINVAL;
1185         }
1186
1187         FREE_AND_NULL(temp_name);
1188         FREE_AND_NULL(valid_name);
1189
1190         return retval;
1191 }
1192
1193 static void check_sender_valid_method_call(GDBusConnection *connection, const gchar *sender,
1194         GVariant *parameters, GDBusMethodInvocation *invocation)
1195 {
1196         GVariant *param = NULL;
1197         int result = 0;
1198         char *event_name = NULL;
1199         char app_id[128] = {0, };
1200         int event_sender_pid = 0;
1201         uid_t sender_uid = 0;
1202
1203         g_variant_get(parameters, "(is)", &event_sender_pid, &event_name);
1204         _D("event_sender_pid(%d), event_name(%s)", event_sender_pid, event_name);
1205
1206         sender_uid = (uid_t)__get_sender_uid(connection, sender);
1207         if (__esd_get_appid_by_pid(event_sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
1208                 result = ES_R_ERROR;
1209         } else {
1210                 if (check_user_event_sender_valid(event_name, app_id) < 0) {
1211                         _E("invalid sender");
1212                         result = ES_R_EINVAL;
1213                 } else {
1214                         result = 1;
1215                 }
1216         }
1217
1218         param = g_variant_new("(is)", result, app_id);
1219         _D("event_name(%s), result(%d)", event_name, result);
1220         g_dbus_method_invocation_return_value(invocation, param);
1221 }
1222
1223 static void check_send_event_valid_method_call(GDBusConnection *connection, const gchar *sender,
1224         GVariant *parameters, GDBusMethodInvocation *invocation)
1225 {
1226         GVariant *param = NULL;
1227         int result = 0;
1228         char *event_name = NULL;
1229         char app_id[128] = {0, };
1230         int sender_pid = 0;
1231         uid_t sender_uid = 0;
1232
1233         g_variant_get(parameters, "(s)", &event_name);
1234         _D("event_name(%s)", event_name);
1235
1236         sender_pid = __get_sender_pid(connection, sender);
1237         sender_uid = (uid_t)__get_sender_uid(connection, sender);
1238         if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
1239                 result = ES_R_ERROR;
1240         } else {
1241                 if (check_user_event_sender_valid(event_name, app_id) < 0) {
1242                         _E("invalid sender");
1243                         result = ES_R_EINVAL;
1244                 } else {
1245                         result = 1;
1246                 }
1247         }
1248
1249         param = g_variant_new("(i)", result);
1250         _D("event_name(%s), result(%d)", event_name, result);
1251         g_dbus_method_invocation_return_value(invocation, param);
1252 }
1253
1254 static void get_trusted_peer_method_call(GDBusConnection *connection, const gchar *sender,
1255         GVariant *parameters, GDBusMethodInvocation *invocation)
1256 {
1257         GVariant *param = NULL;
1258         int result = 0;
1259         GVariantBuilder *builder = NULL;
1260         GHashTableIter iter;
1261         gpointer key, value;
1262         char *event_name = NULL;
1263         char app_id[128] = {0, };
1264         int sender_pid = 0;
1265         uid_t sender_uid = 0;
1266         int ret = 0;
1267         uid_t uid = 0;
1268         char *_appid = NULL;
1269         char *_busname = NULL;
1270         trusted_item *item;
1271
1272         g_variant_get(parameters, "(s)", &event_name);
1273         _D("event_name(%s)", event_name);
1274
1275         sender_pid = __get_sender_pid(connection, sender);
1276         sender_uid = (uid_t)__get_sender_uid(connection, sender);
1277         if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
1278                 result = ES_R_ERROR;
1279         } else {
1280                 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1281
1282                 g_hash_table_iter_init(&iter, trusted_busname_table);
1283                 while (g_hash_table_iter_next(&iter, &key, &value)) {
1284                         item = (trusted_item *)value;
1285                         uid = item->uid;
1286                         _appid = item->app_id;
1287                         _busname = item->bus_name;
1288
1289                         if (uid != GLOBAL_USER && uid != sender_uid)
1290                                 continue;
1291
1292                         ret = __esd_check_certificate_match(uid, _appid, sender_uid, app_id);
1293                         if (ret == ES_R_OK)
1294                                 g_variant_builder_add(builder, "s", _busname);
1295                 }
1296
1297                 result = 1;
1298         }
1299
1300         param = g_variant_new("(ias)", result, builder);
1301         _D("result(%d)", result);
1302         g_dbus_method_invocation_return_value(invocation, param);
1303         if (builder)
1304                 g_variant_builder_unref(builder);
1305 }
1306
1307 static void setup_trusted_peer_method_call(GDBusConnection *connection, const gchar *sender,
1308         GVariant *parameters, GDBusMethodInvocation *invocation)
1309 {
1310         GVariant *param = NULL;
1311         int result = 0;
1312         char *event_name = NULL;
1313         char *destination_name = NULL;
1314         char app_id[128] = {0, };
1315         int sender_pid = 0;
1316         uid_t sender_uid = 0;
1317         int ret = 0;
1318
1319         g_variant_get(parameters, "(ss)", &event_name, &destination_name);
1320         _D("event_name(%s), destination_name(%s)", event_name, destination_name);
1321
1322         if (destination_name && destination_name[0] != '\0') {
1323                 sender_pid = __get_sender_pid(connection, sender);
1324                 sender_uid = (uid_t)__get_sender_uid(connection, sender);
1325                 if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
1326                         result = ES_R_ERROR;
1327                 } else {
1328                         ret = __esd_trusted_busname_add_item(sender_uid, app_id, destination_name,
1329                                 sender_pid);
1330                         if (ret < 0) {
1331                                 _E("failed to add trusted busname item");
1332                                 result = ES_R_ERROR;
1333                         } else {
1334                                 result = 1;
1335                         }
1336                 }
1337         } else {
1338                 _E("invalid destination name");
1339                 result = ES_R_ERROR;
1340         }
1341
1342         param = g_variant_new("(i)", result);
1343         _D("event_name(%s), result(%d)", event_name, result);
1344         g_dbus_method_invocation_return_value(invocation, param);
1345 }
1346
1347 static void check_privilege_valid_method_call(GDBusConnection *connection, const gchar *sender,
1348         GVariant *parameters, GDBusMethodInvocation *invocation)
1349 {
1350         GVariant *param = NULL;
1351         int result = 0;
1352         char *event_name = NULL;
1353         char *privilege_name = NULL;
1354         char app_id[128] = {0, };
1355         int sender_pid = 0;
1356         uid_t sender_uid = 0;
1357         char *client = NULL;
1358         char *session = NULL;
1359         char *user = NULL;
1360         int ret = 0;
1361
1362         g_variant_get(parameters, "(s)", &event_name);
1363         __esd_check_privilege_name(event_name, &privilege_name);
1364         _D("event_name(%s), privilege_name(%s)", event_name, privilege_name);
1365
1366         if (privilege_name) {
1367                 sender_pid = __get_sender_pid(connection, sender);
1368                 sender_uid = (uid_t)__get_sender_uid(connection, sender);
1369                 if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
1370                         result = ES_R_ERROR;
1371                 } else {
1372                         ret = cynara_creds_gdbus_get_client(connection, sender, CLIENT_METHOD_DEFAULT, &client);
1373                         if (ret != CYNARA_API_SUCCESS) {
1374                                 _E("failed to get client");
1375                                 result = ES_R_EINVAL;
1376                                 goto out;
1377                         }
1378
1379                         ret = cynara_creds_gdbus_get_user(connection, sender, USER_METHOD_DEFAULT, &user);
1380                         if (ret != CYNARA_API_SUCCESS) {
1381                                 _E("failed to get user");
1382                                 result = ES_R_EINVAL;
1383                                 goto out;
1384                         }
1385
1386                         session = cynara_session_from_pid(sender_pid);
1387                         if (session == NULL) {
1388                                 _E("failed to get session");
1389                                 result = ES_R_EINVAL;
1390                                 goto out;
1391                         }
1392
1393                         _D("app_id(%s), client(%s), session(%s), user(%s)", app_id, client, session, user);
1394                         if (__esd_check_valid_privilege_by_cynara(app_id, client, session, user, privilege_name))
1395                                 result = 1;
1396                         else
1397                                 result = ES_R_EINVAL;
1398                 }
1399         } else {
1400                 result = 1;
1401         }
1402
1403 out:
1404         g_free(client);
1405         g_free(user);
1406         g_free(session);
1407         param = g_variant_new("(i)", result);
1408         _D("event_name(%s), result(%d)", event_name, result);
1409         g_dbus_method_invocation_return_value(invocation, param);
1410 }
1411
1412 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
1413 static void get_earlier_data_method_call(GVariant *parameters, GDBusMethodInvocation *invocation)
1414 {
1415         GVariant *param = NULL;
1416         int result = 0;
1417         char *event_name = NULL;
1418         bundle *b = NULL;
1419         bundle_raw *raw = NULL;
1420         int len = 0;
1421         earlier_item *item;
1422
1423         g_variant_get(parameters, "(s)", &event_name);
1424
1425         if (event_name && strlen(event_name) > 0) {
1426                 _D("event_name(%s)", event_name);
1427                 result = ES_R_OK;
1428         } else {
1429                 _E("invalid event_name(%s)", event_name);
1430                 result = ES_R_ERROR;
1431         }
1432
1433         item = (earlier_item *)g_hash_table_lookup(earlier_event_table, event_name);
1434         if (item != NULL) {
1435                 if (item->earlier_data) {
1436                         b = bundle_dup(item->earlier_data);
1437                         bundle_add_str(b, "is_earlier_data", "true");
1438                         bundle_encode(b, &raw, &len);
1439                         bundle_free(b);
1440                 }
1441         }
1442
1443         param = g_variant_new("(iis)", result, len, raw);
1444
1445         _D("result(%d), len(%d)", result, len);
1446         g_dbus_method_invocation_return_value(invocation, param);
1447
1448         bundle_free_encoded_rawdata(&raw);
1449 }
1450 #endif
1451
1452 static void handle_method_call(GDBusConnection *connection,
1453         const gchar *sender, const gchar *object_path,
1454         const gchar *interface_name, const gchar *method_name,
1455         GVariant *parameters, GDBusMethodInvocation *invocation,
1456         gpointer user_data)
1457 {
1458         if (g_strcmp0(method_name, "CheckSenderValidation") == 0) {
1459                 check_sender_valid_method_call(connection, sender, parameters, invocation);
1460         } else if (g_strcmp0(method_name, "GetTrustedPeerList") == 0) {
1461                 get_trusted_peer_method_call(connection, sender, parameters, invocation);
1462         } else if (g_strcmp0(method_name, "SetupTrustedPeer") == 0) {
1463                 setup_trusted_peer_method_call(connection, sender, parameters, invocation);
1464         } else if (g_strcmp0(method_name, "CheckPrivilegeValidation") == 0) {
1465                 check_privilege_valid_method_call(connection, sender, parameters, invocation);
1466         } else if (g_strcmp0(method_name, "CheckUserSendValidation") == 0) {
1467                 check_send_event_valid_method_call(connection, sender, parameters, invocation);
1468 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
1469         } else if (g_strcmp0(method_name, "GetEarlierData") == 0) {
1470                 get_earlier_data_method_call(parameters, invocation);
1471 #endif
1472         }
1473 }
1474
1475 static const GDBusInterfaceVTable interface_vtable = {
1476         handle_method_call,
1477         NULL,
1478         NULL
1479 };
1480
1481 static void __esd_on_bus_acquired(GDBusConnection *connection,
1482                 const gchar *name, gpointer user_data)
1483 {
1484         _I("bus acquired(%s)", name);
1485
1486         guint reg_id = 0;
1487         GError *error = NULL;
1488
1489         reg_id = g_dbus_connection_register_object(connection,
1490                 ESD_OBJECT_PATH,
1491                 introspection_data->interfaces[0],
1492                 &interface_vtable,
1493                 NULL, NULL, &error);
1494         if (reg_id == 0) {
1495                 _E("g_dbus_connection_register_object error(%s)", error->message);
1496                 g_error_free(error);
1497         }
1498 }
1499
1500 static void __esd_on_name_acquired(GDBusConnection *connection,
1501                 const gchar *name, gpointer user_data)
1502 {
1503         bundle *b;
1504
1505         _I("name acquired(%s)", name);
1506
1507         __esd_check_trusted_events(connection, "ListNames");
1508         __esd_check_trusted_events(connection, "ListActivatableNames");
1509
1510         b = bundle_create();
1511         bundle_add_str(b, EVT_KEY_ESD_STATUS, EVT_VAL_ESD_STARTED);
1512         eventsystem_send_system_event(SYS_EVENT_ESD_STATUS, b);
1513         bundle_free(b);
1514
1515         __esd_register_vconf_callbacks();
1516
1517         __esd_trusted_busname_print_items();
1518
1519         __esd_start_sd_monitor();
1520
1521         __esd_dbus_name_monitor(connection);
1522 }
1523
1524 static void __esd_on_name_lost(GDBusConnection *connection,
1525                 const gchar *name, gpointer user_data)
1526 {
1527         _E("name lost(%s)", name);
1528 }
1529
1530 static int __esd_before_loop(void)
1531 {
1532         int ret = 0;
1533         GError *error = NULL;
1534         guint owner_id = 0;
1535
1536 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
1537         guint subscription_id = 0;
1538         int i;
1539         int size;
1540         char *event_name;
1541         int fd;
1542         int val;
1543         int status;
1544         int charger_status;
1545         int charge_now;
1546         earlier_item *item;
1547
1548         earlier_event_table = g_hash_table_new(g_str_hash, g_str_equal);
1549
1550         _I("register events for earlier_data");
1551         size = sizeof(earlier_event_list)/sizeof(*earlier_event_list);
1552         for (i = 0; i < size; i++) {
1553                 event_name = (char *)earlier_event_list[i];
1554                 _I("event_name(%s)", event_name);
1555
1556                 item = calloc(1, sizeof(earlier_item));
1557                 if (item == NULL) {
1558                         _E("memery alloc failed");
1559                         return ES_R_ENOMEM;
1560                 }
1561                 item->event_name = strdup(event_name);
1562                 if (item->event_name == NULL) {
1563                         _E("out of memory");
1564                         free(item);
1565                         return ES_R_ENOMEM;
1566                 }
1567
1568                 /* set initial data */
1569                 if (strcmp(event_name, SYS_EVENT_BOOT_COMPLETED) == 0) {
1570                         fd = open(ESD_BOOT_COMPLETED, O_RDONLY);
1571                         if (fd < 0) {
1572                                 _D("open file error(%d)", fd);
1573                         } else {
1574                                 item->earlier_data = bundle_create();
1575                                 bundle_add_str(item->earlier_data, EVT_KEY_BOOT_COMPLETED,
1576                                         EVT_VAL_BOOT_COMPLETED_TRUE);
1577                                 close(fd);
1578                         }
1579                 } else if (strcmp(event_name, SYS_EVENT_SYSTEM_SHUTDOWN) == 0) {
1580                         ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &val);
1581                         if (ret != VCONF_OK) {
1582                                 _E("failed to get power_off status (%d)", ret);
1583                         } else {
1584                                 if (val == VCONFKEY_SYSMAN_POWER_OFF_DIRECT ||
1585                                         val == VCONFKEY_SYSMAN_POWER_OFF_RESTART) {
1586                                         /* power-off requested */
1587                                         item->earlier_data = bundle_create();
1588                                         bundle_add_str(item->earlier_data, EVT_KEY_SYSTEM_SHUTDOWN,
1589                                                 EVT_VAL_SYSTEM_SHUTDOWN_TRUE);
1590                                 }
1591                         }
1592                 } else if (strcmp(event_name, SYS_EVENT_LOW_MEMORY) == 0) {
1593                         ret = vconf_get_int(VCONFKEY_SYSMAN_LOW_MEMORY, &status);
1594                         if (ret != VCONF_OK) {
1595                                 _E("failed to get low_memory status (%d)", ret);
1596                         } else {
1597                                 item->earlier_data = bundle_create();
1598                                 if (status == VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING)
1599                                         bundle_add_str(item->earlier_data, EVT_KEY_LOW_MEMORY,
1600                                                 EVT_VAL_MEMORY_SOFT_WARNING);
1601                                 else if (status == VCONFKEY_SYSMAN_LOW_MEMORY_HARD_WARNING)
1602                                         bundle_add_str(item->earlier_data, EVT_KEY_LOW_MEMORY,
1603                                                 EVT_VAL_MEMORY_HARD_WARNING);
1604                                 else
1605                                         bundle_add_str(item->earlier_data, EVT_KEY_LOW_MEMORY,
1606                                                 EVT_VAL_MEMORY_NORMAL);
1607                         }
1608                 } else if (strcmp(event_name, SYS_EVENT_BATTERY_CHARGER_STATUS) == 0) {
1609                         ret = vconf_get_int(VCONFKEY_SYSMAN_CHARGER_STATUS, &charger_status);
1610                         if (ret != VCONF_OK) {
1611                                 _E("failed to get charger_status (%d)", ret);
1612                         } else {
1613                                 ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, &charge_now);
1614                                 if (ret != VCONF_OK)
1615                                         _E("failed to get charge_now (%d)", ret);
1616                         }
1617
1618                         if (ret == VCONF_OK) {
1619                                 item->earlier_data = bundle_create();
1620                                 if (charger_status == VCONFKEY_SYSMAN_CHARGER_CONNECTED) {
1621                                         if (charge_now == 0) {
1622                                                 bundle_add_str(item->earlier_data,
1623                                                         EVT_KEY_BATTERY_CHARGER_STATUS,
1624                                                         EVT_VAL_BATTERY_CHARGER_DISCHARGING);
1625                                         } else {
1626                                                 bundle_add_str(item->earlier_data,
1627                                                         EVT_KEY_BATTERY_CHARGER_STATUS,
1628                                                         EVT_VAL_BATTERY_CHARGER_CHARGING);
1629                                         }
1630                                 } else {
1631                                         bundle_add_str(item->earlier_data,
1632                                                 EVT_KEY_BATTERY_CHARGER_STATUS,
1633                                                 EVT_VAL_BATTERY_CHARGER_DISCONNECTED);
1634                                 }
1635                         }
1636                 }
1637
1638                 eventsystem_register_event(event_name, &subscription_id,
1639                         (eventsystem_handler)__esd_event_handler, NULL);
1640                 if (subscription_id == 0) {
1641                         _E("signal subscription error, event_name(%s)", event_name);
1642                         if (item->earlier_data)
1643                                 bundle_free(item->earlier_data);
1644                         free(item->event_name);
1645                         free(item);
1646
1647                         return ES_R_ERROR;
1648                 } else {
1649                         item->reg_id = subscription_id;
1650                 }
1651
1652                 g_hash_table_insert(earlier_event_table, event_name, item);
1653         }
1654
1655         __esd_earlier_table_print_items();
1656 #endif
1657
1658         event_launch_table = g_hash_table_new(g_str_hash, g_str_equal);
1659         trusted_busname_table = g_hash_table_new(g_str_hash, g_str_equal);
1660
1661         /* gdbus setup for method call */
1662         introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, &error);
1663         if (!introspection_data) {
1664                 _E("g_dbus_node_info_new_for_xml error(%s)", error->message);
1665                 g_error_free(error);
1666                 return ES_R_ERROR;
1667         }
1668
1669         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1670                 ESD_BUS_NAME,
1671                 G_BUS_NAME_OWNER_FLAGS_NONE,
1672                 __esd_on_bus_acquired,
1673                 __esd_on_name_acquired,
1674                 __esd_on_name_lost,
1675                 NULL, NULL);
1676         if (!owner_id) {
1677                 _E("g_bus_own_name error");
1678                 g_dbus_node_info_unref(introspection_data);
1679                 return ES_R_ERROR;
1680         }
1681
1682         _I("esd before_loop done");
1683
1684         return ES_R_OK;
1685 }
1686
1687 static void __esd_pkgmgr_event_free(esd_pkgmgr_event *pkg_event)
1688 {
1689         pkg_event->type = UNKNOWN;
1690         if (pkg_event->pkgid) {
1691                 free(pkg_event->pkgid);
1692                 pkg_event->pkgid = NULL;
1693         }
1694 }
1695
1696 static int __esd_appcontrol_cb(const char *operation,
1697                 const char *uri, const char *mime, void *data)
1698 {
1699         esd_appctrl_cb_data *cb_data = (esd_appctrl_cb_data *)data;
1700         char *appid = NULL;
1701         char *pkgid = NULL;
1702         char *event_name = NULL;
1703         const char *prefix = "event://";
1704         uid_t uid = 0;
1705
1706         if (cb_data == NULL) {
1707                 _E("invalid data");
1708                 return 0;
1709         }
1710         appid = cb_data->appid;
1711         pkgid = cb_data->pkgid;
1712         uid = cb_data->uid;
1713
1714         _D("uid(%d), appid(%s), pkgid(%s), operation(%s), uri(%s), mime(%s)",
1715                 uid, appid, pkgid, operation, uri, mime);
1716
1717         if (!strcmp(operation, APPSVC_OPERATION_LAUNCH_ON_EVENT)) {
1718                 if (!strncmp(uri, prefix, strlen(prefix))) {
1719                         event_name = strdup(&uri[8]);
1720                         if (event_name) {
1721                                 _D("appid(%s), event_name(%s)", appid, event_name);
1722                                 if (!__esd_check_event_launch_support(event_name)) {
1723                                         _E("failed to add item (not support event)");
1724                                 } else if (!__esd_check_app_privileged_event(uid, appid, pkgid, event_name)) {
1725                                         _E("failed to add item (no privilege)");
1726                                 } else {
1727                                         if (__esd_add_launch_item(uid, event_name, appid, pkgid))
1728                                                 _E("failed to add item");
1729                                 }
1730                                 FREE_AND_NULL(event_name);
1731                         } else {
1732                                 _E("out of memory");
1733                         }
1734                 } else {
1735                         _E("Invalid uri(%s) for event_name", uri);
1736                 }
1737         }
1738
1739         return 0;
1740 }
1741
1742 static int __esd_add_appinfo_handler(const pkgmgrinfo_appinfo_h handle, void *data)
1743 {
1744         char *appid = NULL;
1745         char *pkgid = NULL;
1746         pkgmgrinfo_app_component component_type;
1747         int ret = 0;
1748         uid_t *p_uid = NULL;
1749
1750         if (data == NULL) {
1751                 _E("invalid data");
1752                 return ES_R_ERROR;
1753         }
1754
1755         p_uid = (uid_t *)data;
1756
1757         ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
1758         if (ret < 0) {
1759                 _E("failed to get appid");
1760                 return ES_R_ERROR;
1761         }
1762
1763         ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
1764         if (ret < 0) {
1765                 _E("failed to get pkgid");
1766                 return ES_R_ERROR;
1767         }
1768
1769         ret = pkgmgrinfo_appinfo_get_component(handle, &component_type);
1770         if (ret != PMINFO_R_OK) {
1771                 _E("fail to get component type");
1772                 return ES_R_ERROR;
1773         }
1774
1775         _D("uid(%d), appid(%s), component_type(%d)", *p_uid, appid, component_type);
1776         if (component_type == PMINFO_SVC_APP) {
1777                 esd_appctrl_cb_data *cb_data = calloc(1, sizeof(esd_appctrl_cb_data));
1778                 if (cb_data == NULL) {
1779                         _E("memory alloc failed");
1780                         return ES_R_ENOMEM;
1781                 }
1782                 cb_data->appid = strdup(appid);
1783                 if (cb_data->appid == NULL) {
1784                         _E("out_of_memory");
1785                         FREE_AND_NULL(cb_data);
1786                         return ES_R_ENOMEM;
1787                 }
1788                 cb_data->pkgid = strdup(pkgid);
1789                 if (cb_data->pkgid == NULL) {
1790                         _E("out_of_memory");
1791                         FREE_AND_NULL(cb_data->appid);
1792                         FREE_AND_NULL(cb_data);
1793                         return ES_R_ENOMEM;
1794                 }
1795                 cb_data->uid = *p_uid;
1796                 ret = pkgmgrinfo_appinfo_foreach_appcontrol(handle,
1797                         (pkgmgrinfo_app_control_list_cb)__esd_appcontrol_cb, cb_data);
1798
1799                 FREE_AND_NULL(cb_data->pkgid);
1800                 FREE_AND_NULL(cb_data->appid);
1801                 FREE_AND_NULL(cb_data);
1802
1803                 if (ret < 0) {
1804                         _E("failed to get appcontrol info");
1805                         return ES_R_ERROR;
1806                 }
1807                 __esd_launch_table_print_items();
1808         }
1809
1810         return ES_R_OK;
1811 }
1812
1813 static int __esd_pkgmgr_event_callback(uid_t target_uid, int req_id, const char *pkg_type,
1814                 const char *pkgid, const char *key, const char *val,
1815                 const void *pmsg, void *data)
1816 {
1817         esd_pkgmgr_event *pkg_event = (esd_pkgmgr_event *)data;
1818         pkgmgrinfo_pkginfo_h handle = NULL;
1819         int ret = 0;
1820
1821         _D("target_uid(%d), req_id(%d), pkg_type(%s), pkgid(%s), key(%s), val(%s)",
1822                 target_uid, req_id, pkg_type, pkgid, key, val);
1823
1824         if (strncmp(key, "start", strlen(key)) == 0) {
1825                 if (strcmp(val, "install") == 0) {
1826                         _D("install start");
1827                         pkg_event->type = INSTALL;
1828                 } else if (strcmp(val, "uninstall") == 0) {
1829                         _D("unistall start");
1830                         pkg_event->type = UNINSTALL;
1831                 } else if (strcmp(val, "update") == 0) {
1832                         _D("update start");
1833                         pkg_event->type = UPDATE;
1834                 } else {
1835                         _D("val(%s) start", val);
1836                         __esd_pkgmgr_event_free(pkg_event);
1837                 }
1838         } else if (strcmp(key, "end") == 0 && strcmp(val, "ok") == 0) {
1839                 if (pkg_event->type == INSTALL || pkg_event->type == UPDATE) {
1840                         _D("install end (ok)");
1841                         ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, target_uid, &handle);
1842                         if (ret < 0) {
1843                                 _E("failed to get pkginfo");
1844                                 __esd_pkgmgr_event_free(pkg_event);
1845                                 return 0;
1846                         }
1847                         ret = pkgmgrinfo_appinfo_get_usr_list(handle,
1848                                 PMINFO_ALL_APP, __esd_add_appinfo_handler, &target_uid, target_uid);
1849                         if (ret < 0) {
1850                                 _E("failed to get appinfo");
1851                                 __esd_pkgmgr_event_free(pkg_event);
1852                                 return 0;
1853                         }
1854                         ret = pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1855                         if (ret < 0) {
1856                                 _E("failed to destroy pkginfo");
1857                                 __esd_pkgmgr_event_free(pkg_event);
1858                                 return 0;
1859                         }
1860                 } else if (pkg_event->type == UNINSTALL) {
1861                         _D("uninstall end (ok)");
1862                         __esd_launch_table_remove_items(target_uid, pkgid);
1863                         __esd_launch_table_print_items();
1864                 }
1865                 __esd_pkgmgr_event_free(pkg_event);
1866         } else if (strcmp(key, "end") == 0 && strcmp(val, "fail") == 0) {
1867                 _E("pkg_event(%d) falied", pkg_event->type);
1868                 __esd_pkgmgr_event_free(pkg_event);
1869         } else {
1870                 if (strcmp(key, "install_percent") != 0)
1871                         __esd_pkgmgr_event_free(pkg_event);
1872         }
1873
1874         return 0;
1875 }
1876
1877 static int __esd_init()
1878 {
1879         int req_id = 0;
1880         int ret = 0;
1881         pkgmgr_client *client;
1882         esd_pkgmgr_event *pkg_event;
1883
1884 #if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36)
1885         g_type_init();
1886 #endif
1887         ecore_init();
1888
1889         __esd_init_cynara();
1890
1891         client = pkgmgr_client_new(PC_LISTENING);
1892         if (client == NULL) {
1893                 _E("set pkgmgr client failed");
1894                 return ES_R_ERROR;
1895         }
1896
1897         pkg_event = calloc(1, sizeof(esd_pkgmgr_event));
1898         if (pkg_event == NULL) {
1899                 _E("memory alloc failed");
1900                 ret = pkgmgr_client_free(client);
1901                 if (ret != PKGMGR_R_OK)
1902                         _E("pkgmgr_client_free failed(%d)", ret);
1903
1904                 return ES_R_ENOMEM;
1905         }
1906
1907         req_id = pkgmgr_client_listen_status(client, __esd_pkgmgr_event_callback, pkg_event);
1908         if (req_id < 0) {
1909                 _E("pkgmgr client listen failed");
1910                 ret = pkgmgr_client_free(client);
1911                 if (ret != PKGMGR_R_OK)
1912                         _E("pkgmgr_client_free failed(%d)", ret);
1913
1914                 return ES_R_ERROR;
1915         }
1916
1917         s_info.client = client;
1918
1919         _I("esd init done");
1920
1921         return 0;
1922 }
1923
1924 static void __esd_remove_esd_list_item(gpointer data, gpointer user_data)
1925 {
1926         esd_list_item_s *item = (esd_list_item_s *)data;
1927
1928         free(item->app_id);
1929         free(item->pkg_id);
1930 }
1931
1932 static void __esd_finalize(void)
1933 {
1934         gpointer key;
1935         gpointer value;
1936         GHashTableIter iter;
1937         trusted_item *item;
1938         event_launch_item *el_item;
1939         int ret = 0;
1940 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
1941         earlier_item *er_item;
1942 #endif
1943
1944         _D("esd finalize");
1945
1946         __esd_stop_sd_monitor();
1947
1948         if (trusted_busname_table) {
1949                 g_hash_table_iter_init(&iter, trusted_busname_table);
1950                 while (g_hash_table_iter_next(&iter, &key, &value)) {
1951                         item = (trusted_item *)value;
1952                         if (item) {
1953                                 free(item->app_id);
1954                                 free(item->bus_name);
1955                                 free(item);
1956                         } else {
1957                                 _E("item is null");
1958                         }
1959                         g_hash_table_iter_remove(&iter);
1960                 }
1961                 g_hash_table_unref(trusted_busname_table);
1962         }
1963
1964 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
1965         if (earlier_event_table) {
1966                 g_hash_table_iter_init(&iter, earlier_event_table);
1967                 while (g_hash_table_iter_next(&iter, &key, &value)) {
1968                         er_item = (earlier_item *)value;
1969                         if (er_item) {
1970                                 eventsystem_unregister_event(er_item->reg_id);
1971                                 free(er_item->event_name);
1972                                 bundle_free(er_item->earlier_data);
1973                                 free(er_item);
1974                         } else {
1975                                 _E("ealier item is NULL");
1976                         }
1977                         g_hash_table_iter_remove(&iter);
1978                 }
1979                 g_hash_table_unref(earlier_event_table);
1980         }
1981 #endif
1982
1983         if (event_launch_table) {
1984                 g_hash_table_iter_init(&iter, event_launch_table);
1985                 while (g_hash_table_iter_next(&iter, &key, &value)) {
1986                         el_item = (event_launch_item *)value;
1987                         if (el_item) {
1988                                 eventsystem_unregister_event(el_item->reg_id);
1989                                 free(el_item->event_name);
1990                                 g_list_foreach(el_item->app_list_evtlaunch,
1991                                         __esd_remove_esd_list_item, NULL);
1992                                 g_list_free(el_item->app_list_evtlaunch);
1993                                 free(el_item);
1994                         } else {
1995                                 _E("item is NULL");
1996                         }
1997                         g_hash_table_iter_remove(&iter);
1998                 }
1999                 g_hash_table_unref(event_launch_table);
2000         }
2001
2002         if (introspection_data)
2003                 g_dbus_node_info_unref(introspection_data);
2004
2005         if (s_info.client) {
2006                 ret = pkgmgr_client_free(s_info.client);
2007                 if (ret != PKGMGR_R_OK)
2008                         _E("pkgmgr_client_free failed(%d)", ret);
2009         }
2010
2011         __esd_finish_cynara();
2012
2013         _D("esd finalize end");
2014 }
2015
2016 int main(int argc, char *argv[])
2017 {
2018         _I("event system daemon : main()");
2019
2020         if (__esd_init() != 0) {
2021                 _E("ESD Initialization failed!");
2022                 return ES_R_ERROR;
2023         }
2024
2025         if (__esd_before_loop() < 0) {
2026                 _E("ESD failed!");
2027                 __esd_finalize();
2028                 return ES_R_ERROR;
2029         }
2030
2031         ecore_main_loop_begin();
2032
2033         _E("shutdown");
2034
2035         __esd_finalize();
2036
2037         ecore_shutdown();
2038
2039         return 0;
2040 }