9 #include <package-manager.h>
10 #include <pkgmgr-info.h>
11 #include <appsvc/appsvc.h>
12 #include <eventsystem.h>
13 #include <bundle_internal.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"
24 #define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
27 static GHashTable *event_launch_table; /* table of events for launch_on_event*/
29 static const char *event_launch_support_list[] = {
30 SYS_EVENT_BATTERY_CHARGER_STATUS,
32 SYS_EVENT_EARJACK_STATUS,
33 SYS_EVENT_INCOMMING_MSG
36 struct privilege_info {
37 const char *event_name;
38 const char *privilege_name;
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"}
47 static int privilege_check_size = sizeof(privilege_check_list)/sizeof(struct privilege_info);
49 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
50 static const char *earlier_event_list[] = {
53 SYS_EVENT_BOOT_COMPLETED,
54 SYS_EVENT_SYSTEM_SHUTDOWN,
55 SYS_EVENT_BATTERY_CHARGER_STATUS
58 static GHashTable *earlier_event_table; /* table of events for earlier_data */
60 typedef struct __earlier_table_item {
63 bundle *earlier_data; /* event-data from earlier occurrence */
66 static bool g_is_bootcompleted = false;
69 static GHashTable *trusted_busname_table; /* table of dbus bus-names for trusted user-event */
71 typedef struct __trusted_busname_item {
78 typedef struct __eventlaunch_item_param {
80 } eventlaunch_item_param_s;
82 typedef struct esd_list_item {
88 typedef struct __event_launch_table_item {
90 char *package_name; /* just for passing pointer to app-list removal func */
91 GList *app_list_evtlaunch; /* app-list for on-event-launch */
96 enum __pkg_event_type {
103 typedef struct __pkgmgr_event {
108 typedef struct __esd_event_param {
114 typedef struct esd_info {
115 pkgmgr_client *client;
117 static esd_info_s s_info;
119 static Ecore_Fd_Handler *g_fd_handler;
120 sd_login_monitor *g_sd_monitor;
122 typedef struct __esd_appctrl_cb_data {
126 } esd_appctrl_cb_data;
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);
131 static cynara *r_cynara = NULL;
133 static int __esd_init_cynara(void)
137 ret = cynara_initialize(&r_cynara, NULL);
138 if (ret != CYNARA_API_SUCCESS) {
139 _E("cynara initialize failed.");
146 static void __esd_finish_cynara(void)
149 cynara_finish(r_cynara);
153 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
154 static int __esd_check_earlier_support(const char *event_name)
157 int size = sizeof(earlier_event_list)/sizeof(*earlier_event_list);
159 for (i = 0; i < size; i++) {
160 if (strcmp(earlier_event_list[i], event_name) == 0)
168 static int __esd_check_event_launch_support(const char *event_name)
171 int size = sizeof(event_launch_support_list)/sizeof(*event_launch_support_list);
173 for (i = 0; i < size; i++) {
174 if (strcmp(event_launch_support_list[i], event_name) == 0)
181 static int __get_sender_unixinfo(GDBusConnection *conn, const char *sender_name, const char *type)
183 GDBusMessage *msg = NULL;
184 GDBusMessage *reply = NULL;
190 msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus",
191 "org.freedesktop.DBus", type);
193 _E("Can't allocate new method call");
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);
203 _E("Failed to get info [%s]", err->message);
209 body = g_dbus_message_get_body(reply);
210 g_variant_get(body, "(u)", &value);
217 g_object_unref(reply);
222 static int __get_sender_pid(GDBusConnection *conn, const char *sender_name)
226 pid = __get_sender_unixinfo(conn, sender_name, "GetConnectionUnixProcessID");
228 _E("failed to get pid");
232 _D("sender_name(%s), pid(%d)", sender_name, pid);
237 static int __get_sender_uid(GDBusConnection *conn, const char *sender_name)
241 uid = __get_sender_unixinfo(conn, sender_name, "GetConnectionUnixUser");
243 _E("failed to get uid");
246 _D("sender_name(%s), uid(%d)", sender_name, uid);
251 static int __esd_check_certificate_match(uid_t uid, const char *app_id, uid_t from_uid, const char *from_appid)
253 pkgmgrinfo_cert_compare_result_type_e res;
256 _D("uid(%d), app_id(%s), from_uid(%d), from_appid(%s)", uid, app_id, from_uid, from_appid);
258 ret = pkgmgrinfo_pkginfo_compare_usr_app_cert_info(app_id, from_appid, from_uid, &res);
260 _E("failed to check certificate");
264 if (res != PMINFO_CERT_COMPARE_MATCH) {
265 _D("certificat not match (%s)", app_id);
272 static bool __esd_check_application_validation(uid_t uid, const char *appid)
275 pkgmgrinfo_appinfo_h handle;
277 ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &handle);
278 if (ret != PMINFO_R_OK)
281 pkgmgrinfo_appinfo_destroy_appinfo(handle);
283 if (!aul_app_is_running_for_uid(appid, uid))
289 static void __esd_trusted_busname_print_items(void)
295 g_hash_table_iter_init(&iter, trusted_busname_table);
297 while (g_hash_table_iter_next(&iter, &key, &value)) {
298 trusted_item *item = (trusted_item *)value;
300 _D("uid(%d), appid(%s), pid(%d), busname(%s)", item->uid, item->app_id, item->pid, item->bus_name);
304 static int __esd_trusted_busname_add_item(uid_t uid, const char *appid, const char *busname, int pid)
307 char *bus_name = NULL;
308 trusted_item *item = NULL;
309 trusted_item *new_item;
311 app_id = strdup(appid);
312 if (app_id == NULL) {
317 bus_name = strdup(busname);
318 if (bus_name == NULL) {
320 FREE_AND_NULL(app_id);
324 item = (trusted_item *)g_hash_table_lookup(trusted_busname_table, app_id);
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);
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);
340 new_item->app_id = app_id;
341 new_item->bus_name = bus_name;
343 g_hash_table_insert(trusted_busname_table, new_item->app_id, new_item);
344 _D("added busname(%s)", new_item->bus_name);
350 static int __esd_check_trusted_events(GDBusConnection *conn, const char *list_name)
353 GError *error = NULL;
356 char tmp_appid[128] = {0, };
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,
366 if (result == NULL) {
367 _E("get (%s) error(%s)", list_name, error->message);
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"))
377 _D("list(%s), name(%s)", list_name, str);
378 pid = __get_sender_pid(conn, (const char *)str);
380 _E("failed to get pid(%d)", pid);
384 uid = __get_sender_uid(conn, (const char *)str);
386 _E("failed to get uid(%d)", uid);
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);
398 _D("appid(%s)", tmp_appid);
399 if (__esd_check_application_validation((uid_t)uid, tmp_appid)) {
401 ret = __esd_trusted_busname_add_item((uid_t)uid, tmp_appid, (const char *)str, pid);
403 _E("failed to add item");
406 g_variant_iter_free(iter);
407 g_variant_unref(result);
412 static int __esd_check_privilege_name(const char *event_name, char **privilege_name)
416 *privilege_name = NULL;
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);
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)
433 bool has_privilege = false;
435 _D("check privilege, (%s, %s, %s, %s, %s)", appid, client, session, user, privilege_name);
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");
444 _E("failed to check privilege, error(%d)", ret);
447 return has_privilege;
450 static int __esd_check_app_privileged_event(uid_t uid, const char *appid, const char *pkgid, const char *event_name)
452 char *privilege_name = NULL;
456 _D("event_name(%s), uid(%d), appid(%s), pkgid(%s)", event_name, uid, appid, pkgid);
458 __esd_check_privilege_name(event_name, &privilege_name);
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);
472 static void __esd_print_appid_with_eventid(gpointer data, gpointer user_data)
474 esd_list_item_s *item = (esd_list_item_s *)data;
475 char *event_name = (char *)user_data;
477 _D("event_name(%s)-uid(%d)-app_id(%s)-pkg_id(%s)", event_name, item->uid, item->app_id, item->pkg_id);
480 static void __esd_print_interested_event(gpointer data, gpointer user_data)
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);
488 static void __esd_launch_table_print_items(void)
494 g_hash_table_iter_init(&iter, event_launch_table);
496 while (g_hash_table_iter_next(&iter, &key, &value))
497 __esd_print_interested_event(value, NULL);
500 static int __esd_find_compare_by_list_item(gconstpointer data, gconstpointer user_data)
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;
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);
510 static int __esd_add_list_item(uid_t uid, event_launch_item *el_item,
511 const char *app_id, const char *pkg_id)
513 esd_list_item_s *item_of_list = NULL;
515 item_of_list = calloc(1, sizeof(esd_list_item_s));
516 if (item_of_list == NULL) {
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);
529 static int __esd_add_launch_item(uid_t uid, const char *event_name, const char *appid, const char *pkgid)
531 GList *app_list = NULL;
532 guint subscription_id = 0;
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);
541 item_of_list = calloc(1, sizeof(esd_list_item_s));
542 if (item_of_list == NULL) {
543 _E("memory alloc failed");
546 item_of_list->uid = uid;
547 item_of_list->app_id = (char *)appid;
548 item_of_list->pkg_id = (char *)pkgid;
550 app_list = g_list_find_custom(el_item->app_list_evtlaunch,
551 item_of_list, (GCompareFunc)__esd_find_compare_by_list_item);
553 if (app_list == NULL) {
554 _D("add new item (list item only)");
555 app_id = strdup((char *)appid);
560 pkg_id = strdup((char *)pkgid);
563 FREE_AND_NULL(app_id);
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);
574 _D("add new item (all)");
575 eli = calloc(1, sizeof(event_launch_item));
577 _E("memory alloc failed");
581 eli->event_name = strdup(event_name);
582 if (!eli->event_name) {
588 app_id = strdup((char *)appid);
591 FREE_AND_NULL(eli->event_name);
596 pkg_id = strdup((char *)pkgid);
599 FREE_AND_NULL(app_id);
600 FREE_AND_NULL(eli->event_name);
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);
614 g_hash_table_insert(event_launch_table, eli->event_name, eli);
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);
623 eli->reg_id = subscription_id;
630 static void __esd_remove_all_private_usr_app_list(gpointer data, gpointer user_data)
632 esd_list_item_s *item = (esd_list_item_s *)data;
633 event_launch_item *eli = (event_launch_item *)user_data;
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);
641 static int __esd_launch_table_remove_private_usr_items(void)
646 event_launch_item *eli = NULL;
647 GList *first_list = NULL;
649 g_hash_table_iter_init(&iter, event_launch_table);
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);
655 first_list = g_list_first(eli->app_list_evtlaunch);
656 if (first_list == NULL) {
658 eventsystem_unregister_event(eli->reg_id);
660 g_hash_table_iter_remove(&iter);
667 static void __esd_remove_app_list(gpointer data, gpointer user_data)
670 esd_list_item_s *item = (esd_list_item_s *)data;
671 event_launch_item *eli = (event_launch_item *)user_data;
673 if (eli->uid != GLOBAL_USER && eli->uid != item->uid)
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);
683 static int __esd_remove_launch_item(uid_t uid, gpointer data, const char *pkg_id)
685 event_launch_item *eli = (event_launch_item *)data;
686 GList *first_list = NULL;
689 eli->package_name = (char *)pkg_id;
690 g_list_foreach(eli->app_list_evtlaunch, __esd_remove_app_list, eli);
692 first_list = g_list_first(eli->app_list_evtlaunch);
693 if (first_list == NULL) {
695 eventsystem_unregister_event(eli->reg_id);
703 static int __esd_launch_table_remove_items(uid_t uid, const char *pkg_id)
709 g_hash_table_iter_init(&iter, event_launch_table);
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);
721 static void __esd_event_launch_with_appid(gpointer data, gpointer user_data)
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;
729 char event_uri[1024];
732 _D("launch_on_event: app_id(%s), event_name(%s), uid(%d)",
733 app_id, eep->event_name, uid);
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);
742 pid = appsvc_usr_run_service(b, req_id++, NULL, eep->user_data, uid);
743 _D("uid(%d), pid(%d)", uid, pid);
747 _D("already is running or launch failed");
751 static void __esd_check_event_launch_with_eventid(gpointer data, gpointer user_data)
753 event_launch_item *el_item = (event_launch_item *)data;
754 esd_event_param *eep = (esd_event_param *)user_data;
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);
762 static void __esd_launch_event_handler(char *event_name, bundle *data, void *user_data)
765 const char *msg_type;
767 esd_event_param *eep;
768 event_launch_item *el_item;
770 _D("event_name(%s)", event_name);
772 el_item = (event_launch_item *)g_hash_table_lookup(event_launch_table, event_name);
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)
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)
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)
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)
798 msg_id = bundle_get_val(data, EVT_KEY_MSG_ID);
799 _D("msg_id(%s)", msg_id);
804 eep = calloc(1, sizeof(esd_event_param));
806 _E("memory alloc failed");
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);
817 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
818 static void __esd_print_earlier_event(gpointer data, gpointer user_data)
820 earlier_item *item = (earlier_item *)data;
821 char *event_name = (char *)item->event_name;
824 _D("event_name = (%s)", event_name);
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);
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);
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);
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);
849 static void __esd_earlier_table_print_items(void)
855 g_hash_table_iter_init(&iter, earlier_event_table);
857 while (g_hash_table_iter_next(&iter, &key, &value))
858 __esd_print_earlier_event(value, NULL);
861 static void __esd_earlier_event_handler(char *event_name, bundle *data, void *user_data)
865 _D("event_name(%s)", event_name);
867 item = (earlier_item *)g_hash_table_lookup(earlier_event_table, event_name);
869 /* update earlier value */
870 if (item->earlier_data != NULL)
871 bundle_free(item->earlier_data);
873 item->earlier_data = bundle_dup(data);
875 if (!g_is_bootcompleted) {
876 if (strcmp(event_name, SYS_EVENT_BOOT_COMPLETED) == 0) {
877 handle = creat(ESD_BOOT_COMPLETED, 0640);
880 g_is_bootcompleted = true;
887 static void __esd_event_handler(char *event_name, bundle *data, void *user_data)
889 _D("event_name(%s)", event_name);
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);
896 if (__esd_check_event_launch_support(event_name))
897 __esd_launch_event_handler(event_name, data, user_data);
900 static void __esd_trusted_busname_remove_item(char *bus_name)
907 g_hash_table_iter_init(&iter, trusted_busname_table);
909 while (g_hash_table_iter_next(&iter, &key, &value)) {
910 item = (trusted_item *)value;
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);
917 g_hash_table_iter_remove(&iter);
919 __esd_trusted_busname_print_items();
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)
931 char *old_owner = NULL;
932 char *new_owner = NULL;
936 g_variant_get(parameters, "(sss)", &name, &old_owner, &new_owner);
938 if (strstr(name, "event.busname.session")) {
939 old_len = strlen(old_owner);
940 new_len = strlen(new_owner);
942 _D("changed name(%s), old_onwer(%s)(%d) -> new_onwer(%s)(%d)",
943 name, old_owner, old_len, new_owner, new_len);
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");
950 _E("not-expected name change");
954 static int __esd_dbus_name_monitor(GDBusConnection *connection)
956 guint name_owner_changed_id = 0;
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);
963 _I("name_owner_changed_id(%d)", name_owner_changed_id);
968 static int __esd_get_user_items(void)
975 ret = sd_get_uids(&uids);
977 _E("failed to get uids (%d)", ret);
981 if (ret == 0 || uids == NULL) {
982 _I("there is no uid for now");
984 /* reset user's item */
985 __esd_launch_table_remove_private_usr_items();
986 for (i = 0; i < ret; i++) {
988 _I("found uid(%d)", cur_uid);
990 ret = pkgmgrinfo_appinfo_get_usr_installed_list(__esd_add_appinfo_handler, cur_uid, &cur_uid);
992 _E("failed to get user(%d)-app list (%d)", cur_uid, ret);
996 __esd_launch_table_print_items();
1001 static Eina_Bool __esd_fd_handler_func(void *data, Ecore_Fd_Handler *fd_handler)
1003 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ)) {
1005 __esd_get_user_items();
1008 return ECORE_CALLBACK_CANCEL;
1011 static int __esd_start_sd_monitor(void)
1016 ret = __esd_get_user_items();
1020 ret = sd_login_monitor_new("uid", &g_sd_monitor);
1022 _E("sd_login_monitor_new error (%d)", ret);
1026 fd = sd_login_monitor_get_fd(g_sd_monitor);
1028 _E("sd_login_monitor_get_fd error");
1029 sd_login_monitor_unref(g_sd_monitor);
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);
1042 _I("setup sd-monitor done");
1047 static int __esd_stop_sd_monitor(void)
1049 _I("stop sd_monitor");
1051 ecore_main_fd_handler_del(g_fd_handler);
1052 g_fd_handler = NULL;
1055 sd_login_monitor_unref(g_sd_monitor);
1061 static GDBusNodeInfo *introspection_data;
1062 static const gchar introspection_xml[] =
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'/>"
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'/>"
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'/>"
1081 " <method name='CheckPrivilegeValidation'>"
1082 " <arg type='s' name='eventname' direction='in'/>"
1083 " <arg type='i' name='ret' direction='out'/>"
1085 " <method name='CheckUserSendValidation'>"
1086 " <arg type='s' name='eventname' direction='in'/>"
1087 " <arg type='i' name='ret' direction='out'/>"
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'/>"
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'/>"
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'/>"
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'/>"
1118 static int __esd_get_appid_by_pid(int pid, uid_t uid, char *app_id, int buf_size)
1120 int retval = ES_R_OK;
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;
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;
1135 _D("pid(%d)-uid(%d)-appid(%s)", pid, uid, app_id);
1141 static int check_user_event_sender_valid(const char *event_name, const char *app_id)
1143 char *valid_name = NULL;
1144 char *temp_name = NULL;
1146 int retval = ES_R_OK;
1148 int valid_name_len = 0;
1150 temp_name = strdup(event_name);
1151 if (temp_name == NULL) {
1152 _E("out of memory");
1156 tmp = strrchr(temp_name, '.');
1157 if (tmp == NULL || strlen(tmp) == 0) {
1158 _E("invalid event name");
1159 FREE_AND_NULL(temp_name);
1163 if (len <= 1 || len > 128) {
1164 _E("invalid length(%d) of user-defined name");
1165 FREE_AND_NULL(temp_name);
1170 _D("app_id(%s), len(%d)", app_id, strlen(app_id));
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);
1179 snprintf(valid_name, valid_name_len, "%s%s", USER_EVENT_NAME_PREFIX, app_id);
1180 _D("valid_name(%s)", valid_name);
1182 if (strcmp(temp_name, valid_name) != 0) {
1183 _E("appid misamatch");
1184 retval = ES_R_EINVAL;
1187 FREE_AND_NULL(temp_name);
1188 FREE_AND_NULL(valid_name);
1193 static void check_sender_valid_method_call(GDBusConnection *connection, const gchar *sender,
1194 GVariant *parameters, GDBusMethodInvocation *invocation)
1196 GVariant *param = NULL;
1198 char *event_name = NULL;
1199 char app_id[128] = {0, };
1200 int event_sender_pid = 0;
1201 uid_t sender_uid = 0;
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);
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;
1210 if (check_user_event_sender_valid(event_name, app_id) < 0) {
1211 _E("invalid sender");
1212 result = ES_R_EINVAL;
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);
1223 static void check_send_event_valid_method_call(GDBusConnection *connection, const gchar *sender,
1224 GVariant *parameters, GDBusMethodInvocation *invocation)
1226 GVariant *param = NULL;
1228 char *event_name = NULL;
1229 char app_id[128] = {0, };
1231 uid_t sender_uid = 0;
1233 g_variant_get(parameters, "(s)", &event_name);
1234 _D("event_name(%s)", event_name);
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;
1241 if (check_user_event_sender_valid(event_name, app_id) < 0) {
1242 _E("invalid sender");
1243 result = ES_R_EINVAL;
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);
1254 static void get_trusted_peer_method_call(GDBusConnection *connection, const gchar *sender,
1255 GVariant *parameters, GDBusMethodInvocation *invocation)
1257 GVariant *param = NULL;
1259 GVariantBuilder *builder = NULL;
1260 GHashTableIter iter;
1261 gpointer key, value;
1262 char *event_name = NULL;
1263 char app_id[128] = {0, };
1265 uid_t sender_uid = 0;
1268 char *_appid = NULL;
1269 char *_busname = NULL;
1272 g_variant_get(parameters, "(s)", &event_name);
1273 _D("event_name(%s)", event_name);
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;
1280 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
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;
1286 _appid = item->app_id;
1287 _busname = item->bus_name;
1289 if (uid != GLOBAL_USER && uid != sender_uid)
1292 ret = __esd_check_certificate_match(uid, _appid, sender_uid, app_id);
1294 g_variant_builder_add(builder, "s", _busname);
1300 param = g_variant_new("(ias)", result, builder);
1301 _D("result(%d)", result);
1302 g_dbus_method_invocation_return_value(invocation, param);
1304 g_variant_builder_unref(builder);
1307 static void setup_trusted_peer_method_call(GDBusConnection *connection, const gchar *sender,
1308 GVariant *parameters, GDBusMethodInvocation *invocation)
1310 GVariant *param = NULL;
1312 char *event_name = NULL;
1313 char *destination_name = NULL;
1314 char app_id[128] = {0, };
1316 uid_t sender_uid = 0;
1319 g_variant_get(parameters, "(ss)", &event_name, &destination_name);
1320 _D("event_name(%s), destination_name(%s)", event_name, destination_name);
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;
1328 ret = __esd_trusted_busname_add_item(sender_uid, app_id, destination_name,
1331 _E("failed to add trusted busname item");
1332 result = ES_R_ERROR;
1338 _E("invalid destination name");
1339 result = ES_R_ERROR;
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);
1347 static void check_privilege_valid_method_call(GDBusConnection *connection, const gchar *sender,
1348 GVariant *parameters, GDBusMethodInvocation *invocation)
1350 GVariant *param = NULL;
1352 char *event_name = NULL;
1353 char *privilege_name = NULL;
1354 char app_id[128] = {0, };
1356 uid_t sender_uid = 0;
1357 char *client = NULL;
1358 char *session = NULL;
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);
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;
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;
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;
1386 session = cynara_session_from_pid(sender_pid);
1387 if (session == NULL) {
1388 _E("failed to get session");
1389 result = ES_R_EINVAL;
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))
1397 result = ES_R_EINVAL;
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);
1412 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
1413 static void get_earlier_data_method_call(GVariant *parameters, GDBusMethodInvocation *invocation)
1415 GVariant *param = NULL;
1417 char *event_name = NULL;
1419 bundle_raw *raw = NULL;
1423 g_variant_get(parameters, "(s)", &event_name);
1425 if (event_name && strlen(event_name) > 0) {
1426 _D("event_name(%s)", event_name);
1429 _E("invalid event_name(%s)", event_name);
1430 result = ES_R_ERROR;
1433 item = (earlier_item *)g_hash_table_lookup(earlier_event_table, event_name);
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);
1443 param = g_variant_new("(iis)", result, len, raw);
1445 _D("result(%d), len(%d)", result, len);
1446 g_dbus_method_invocation_return_value(invocation, param);
1448 bundle_free_encoded_rawdata(&raw);
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,
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);
1475 static const GDBusInterfaceVTable interface_vtable = {
1481 static void __esd_on_bus_acquired(GDBusConnection *connection,
1482 const gchar *name, gpointer user_data)
1484 _I("bus acquired(%s)", name);
1487 GError *error = NULL;
1489 reg_id = g_dbus_connection_register_object(connection,
1491 introspection_data->interfaces[0],
1493 NULL, NULL, &error);
1495 _E("g_dbus_connection_register_object error(%s)", error->message);
1496 g_error_free(error);
1500 static void __esd_on_name_acquired(GDBusConnection *connection,
1501 const gchar *name, gpointer user_data)
1505 _I("name acquired(%s)", name);
1507 __esd_check_trusted_events(connection, "ListNames");
1508 __esd_check_trusted_events(connection, "ListActivatableNames");
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);
1515 __esd_register_vconf_callbacks();
1517 __esd_trusted_busname_print_items();
1519 __esd_start_sd_monitor();
1521 __esd_dbus_name_monitor(connection);
1524 static void __esd_on_name_lost(GDBusConnection *connection,
1525 const gchar *name, gpointer user_data)
1527 _E("name lost(%s)", name);
1530 static int __esd_before_loop(void)
1533 GError *error = NULL;
1536 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
1537 guint subscription_id = 0;
1548 earlier_event_table = g_hash_table_new(g_str_hash, g_str_equal);
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);
1556 item = calloc(1, sizeof(earlier_item));
1558 _E("memery alloc failed");
1561 item->event_name = strdup(event_name);
1562 if (item->event_name == NULL) {
1563 _E("out of memory");
1568 /* set initial data */
1569 if (strcmp(event_name, SYS_EVENT_BOOT_COMPLETED) == 0) {
1570 fd = open(ESD_BOOT_COMPLETED, O_RDONLY);
1572 _D("open file error(%d)", fd);
1574 item->earlier_data = bundle_create();
1575 bundle_add_str(item->earlier_data, EVT_KEY_BOOT_COMPLETED,
1576 EVT_VAL_BOOT_COMPLETED_TRUE);
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);
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);
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);
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);
1605 bundle_add_str(item->earlier_data, EVT_KEY_LOW_MEMORY,
1606 EVT_VAL_MEMORY_NORMAL);
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);
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);
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);
1626 bundle_add_str(item->earlier_data,
1627 EVT_KEY_BATTERY_CHARGER_STATUS,
1628 EVT_VAL_BATTERY_CHARGER_CHARGING);
1631 bundle_add_str(item->earlier_data,
1632 EVT_KEY_BATTERY_CHARGER_STATUS,
1633 EVT_VAL_BATTERY_CHARGER_DISCONNECTED);
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);
1649 item->reg_id = subscription_id;
1652 g_hash_table_insert(earlier_event_table, event_name, item);
1655 __esd_earlier_table_print_items();
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);
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);
1669 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1671 G_BUS_NAME_OWNER_FLAGS_NONE,
1672 __esd_on_bus_acquired,
1673 __esd_on_name_acquired,
1677 _E("g_bus_own_name error");
1678 g_dbus_node_info_unref(introspection_data);
1682 _I("esd before_loop done");
1687 static void __esd_pkgmgr_event_free(esd_pkgmgr_event *pkg_event)
1689 pkg_event->type = UNKNOWN;
1690 if (pkg_event->pkgid) {
1691 free(pkg_event->pkgid);
1692 pkg_event->pkgid = NULL;
1696 static int __esd_appcontrol_cb(const char *operation,
1697 const char *uri, const char *mime, void *data)
1699 esd_appctrl_cb_data *cb_data = (esd_appctrl_cb_data *)data;
1702 char *event_name = NULL;
1703 const char *prefix = "event://";
1706 if (cb_data == NULL) {
1710 appid = cb_data->appid;
1711 pkgid = cb_data->pkgid;
1714 _D("uid(%d), appid(%s), pkgid(%s), operation(%s), uri(%s), mime(%s)",
1715 uid, appid, pkgid, operation, uri, mime);
1717 if (!strcmp(operation, APPSVC_OPERATION_LAUNCH_ON_EVENT)) {
1718 if (!strncmp(uri, prefix, strlen(prefix))) {
1719 event_name = strdup(&uri[8]);
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)");
1727 if (__esd_add_launch_item(uid, event_name, appid, pkgid))
1728 _E("failed to add item");
1730 FREE_AND_NULL(event_name);
1732 _E("out of memory");
1735 _E("Invalid uri(%s) for event_name", uri);
1742 static int __esd_add_appinfo_handler(const pkgmgrinfo_appinfo_h handle, void *data)
1746 pkgmgrinfo_app_component component_type;
1748 uid_t *p_uid = NULL;
1755 p_uid = (uid_t *)data;
1757 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
1759 _E("failed to get appid");
1763 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
1765 _E("failed to get pkgid");
1769 ret = pkgmgrinfo_appinfo_get_component(handle, &component_type);
1770 if (ret != PMINFO_R_OK) {
1771 _E("fail to get component type");
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");
1782 cb_data->appid = strdup(appid);
1783 if (cb_data->appid == NULL) {
1784 _E("out_of_memory");
1785 FREE_AND_NULL(cb_data);
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);
1795 cb_data->uid = *p_uid;
1796 ret = pkgmgrinfo_appinfo_foreach_appcontrol(handle,
1797 (pkgmgrinfo_app_control_list_cb)__esd_appcontrol_cb, cb_data);
1799 FREE_AND_NULL(cb_data->pkgid);
1800 FREE_AND_NULL(cb_data->appid);
1801 FREE_AND_NULL(cb_data);
1804 _E("failed to get appcontrol info");
1807 __esd_launch_table_print_items();
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)
1817 esd_pkgmgr_event *pkg_event = (esd_pkgmgr_event *)data;
1818 pkgmgrinfo_pkginfo_h handle = NULL;
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);
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) {
1833 pkg_event->type = UPDATE;
1835 _D("val(%s) start", val);
1836 __esd_pkgmgr_event_free(pkg_event);
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);
1843 _E("failed to get pkginfo");
1844 __esd_pkgmgr_event_free(pkg_event);
1847 ret = pkgmgrinfo_appinfo_get_usr_list(handle,
1848 PMINFO_ALL_APP, __esd_add_appinfo_handler, &target_uid, target_uid);
1850 _E("failed to get appinfo");
1851 __esd_pkgmgr_event_free(pkg_event);
1854 ret = pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1856 _E("failed to destroy pkginfo");
1857 __esd_pkgmgr_event_free(pkg_event);
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();
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);
1870 if (strcmp(key, "install_percent") != 0)
1871 __esd_pkgmgr_event_free(pkg_event);
1877 static int __esd_init()
1881 pkgmgr_client *client;
1882 esd_pkgmgr_event *pkg_event;
1884 #if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36)
1889 __esd_init_cynara();
1891 client = pkgmgr_client_new(PC_LISTENING);
1892 if (client == NULL) {
1893 _E("set pkgmgr client failed");
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);
1907 req_id = pkgmgr_client_listen_status(client, __esd_pkgmgr_event_callback, pkg_event);
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);
1917 s_info.client = client;
1919 _I("esd init done");
1924 static void __esd_remove_esd_list_item(gpointer data, gpointer user_data)
1926 esd_list_item_s *item = (esd_list_item_s *)data;
1932 static void __esd_finalize(void)
1936 GHashTableIter iter;
1938 event_launch_item *el_item;
1940 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
1941 earlier_item *er_item;
1946 __esd_stop_sd_monitor();
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;
1954 free(item->bus_name);
1959 g_hash_table_iter_remove(&iter);
1961 g_hash_table_unref(trusted_busname_table);
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;
1970 eventsystem_unregister_event(er_item->reg_id);
1971 free(er_item->event_name);
1972 bundle_free(er_item->earlier_data);
1975 _E("ealier item is NULL");
1977 g_hash_table_iter_remove(&iter);
1979 g_hash_table_unref(earlier_event_table);
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;
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);
1997 g_hash_table_iter_remove(&iter);
1999 g_hash_table_unref(event_launch_table);
2002 if (introspection_data)
2003 g_dbus_node_info_unref(introspection_data);
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);
2011 __esd_finish_cynara();
2013 _D("esd finalize end");
2016 int main(int argc, char *argv[])
2018 _I("event system daemon : main()");
2020 if (__esd_init() != 0) {
2021 _E("ESD Initialization failed!");
2025 if (__esd_before_loop() < 0) {
2031 ecore_main_loop_begin();