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)", app_id, eep->event_name);
734 if (aul_app_is_running_for_uid(app_id, uid)) {
735 snprintf(event_uri, sizeof(event_uri), "event://%s", eep->event_name);
736 b = bundle_dup(eep->event_data);
737 appsvc_set_operation(b, APPSVC_OPERATION_LAUNCH_ON_EVENT);
738 appsvc_set_uri(b, event_uri);
739 appsvc_set_appid(b, app_id);
741 pid = appsvc_usr_run_service(b, req_id++, NULL, eep->user_data, uid);
742 _D("uid(%d), pid(%d)", uid, pid);
746 _D("already is running");
750 static void __esd_check_event_launch_with_eventid(gpointer data, gpointer user_data)
752 event_launch_item *el_item = (event_launch_item *)data;
753 esd_event_param *eep = (esd_event_param *)user_data;
755 if (strcmp(eep->event_name, (char *)el_item->event_name) == 0) {
756 g_list_foreach(el_item->app_list_evtlaunch,
757 __esd_event_launch_with_appid, user_data);
761 static void __esd_launch_event_handler(char *event_name, bundle *data, void *user_data)
764 const char *msg_type;
766 esd_event_param *eep;
767 event_launch_item *el_item;
769 _D("event_name(%s)", event_name);
771 el_item = (event_launch_item *)g_hash_table_lookup(event_launch_table, event_name);
775 if (el_item->app_list_evtlaunch != NULL) {
776 if (strcmp(SYS_EVENT_BATTERY_CHARGER_STATUS, event_name) == 0) {
777 val = bundle_get_val(data, EVT_KEY_BATTERY_CHARGER_STATUS);
778 _D("charger val(%s)", val);
779 if (strcmp(EVT_VAL_BATTERY_CHARGER_CONNECTED, val) != 0)
781 } else if (strcmp(SYS_EVENT_USB_STATUS, event_name) == 0) {
782 val = bundle_get_val(data, EVT_KEY_USB_STATUS);
783 _D("usb val(%s)", val);
784 if (strcmp(EVT_VAL_USB_CONNECTED, val) != 0)
786 } else if (strcmp(SYS_EVENT_EARJACK_STATUS, event_name) == 0) {
787 val = bundle_get_val(data, EVT_KEY_EARJACK_STATUS);
788 _D("earjack val(%s)", val);
789 if (strcmp(EVT_VAL_EARJACK_CONNECTED, val) != 0)
791 } else if (strcmp(SYS_EVENT_INCOMMING_MSG, event_name) == 0) {
792 msg_type = bundle_get_val(data, EVT_KEY_MSG_TYPE);
793 _D("msg_type(%s)", msg_type);
794 if (msg_type == NULL)
797 msg_id = bundle_get_val(data, EVT_KEY_MSG_ID);
798 _D("msg_id(%s)", msg_id);
803 eep = calloc(1, sizeof(esd_event_param));
805 _E("memory alloc failed");
808 eep->event_name = event_name;
809 eep->event_data = data;
810 eep->user_data = (void *)user_data;
811 __esd_check_event_launch_with_eventid(el_item, eep);
816 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
817 static void __esd_print_earlier_event(gpointer data, gpointer user_data)
819 earlier_item *item = (earlier_item *)data;
820 char *event_name = (char *)item->event_name;
823 _D("event_name = (%s)", event_name);
825 if (strcmp(event_name, SYS_EVENT_BOOT_COMPLETED) == 0) {
826 if (item->earlier_data) {
827 val = bundle_get_val(item->earlier_data, EVT_KEY_BOOT_COMPLETED);
828 _D("boot_completed(%s)", val);
830 } else if (strcmp(event_name, SYS_EVENT_SYSTEM_SHUTDOWN) == 0) {
831 if (item->earlier_data) {
832 val = bundle_get_val(item->earlier_data, EVT_KEY_SYSTEM_SHUTDOWN);
833 _D("shutdown(%s)", val);
835 } else if (strcmp(event_name, SYS_EVENT_LOW_MEMORY) == 0) {
836 if (item->earlier_data) {
837 val = bundle_get_val(item->earlier_data, EVT_KEY_LOW_MEMORY);
838 _D("low_memory(%s)", val);
840 } else if (strcmp(event_name, SYS_EVENT_BATTERY_CHARGER_STATUS) == 0) {
841 if (item->earlier_data) {
842 val = bundle_get_val(item->earlier_data, EVT_KEY_BATTERY_CHARGER_STATUS);
843 _D("charger_status(%s)", val);
848 static void __esd_earlier_table_print_items(void)
854 g_hash_table_iter_init(&iter, earlier_event_table);
856 while (g_hash_table_iter_next(&iter, &key, &value))
857 __esd_print_earlier_event(value, NULL);
860 static void __esd_earlier_event_handler(char *event_name, bundle *data, void *user_data)
864 _D("event_name(%s)", event_name);
866 item = (earlier_item *)g_hash_table_lookup(earlier_event_table, event_name);
868 /* update earlier value */
869 if (item->earlier_data != NULL)
870 bundle_free(item->earlier_data);
872 item->earlier_data = bundle_dup(data);
874 if (!g_is_bootcompleted) {
875 if (strcmp(event_name, SYS_EVENT_BOOT_COMPLETED) == 0) {
876 handle = creat(ESD_BOOT_COMPLETED, 0640);
879 g_is_bootcompleted = true;
886 static void __esd_event_handler(char *event_name, bundle *data, void *user_data)
888 _D("event_name(%s)", event_name);
890 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
891 if (__esd_check_earlier_support(event_name))
892 __esd_earlier_event_handler(event_name, data, user_data);
895 if (__esd_check_event_launch_support(event_name))
896 __esd_launch_event_handler(event_name, data, user_data);
899 static void __esd_trusted_busname_remove_item(char *bus_name)
906 g_hash_table_iter_init(&iter, trusted_busname_table);
908 while (g_hash_table_iter_next(&iter, &key, &value)) {
909 item = (trusted_item *)value;
911 if (strcmp(bus_name, item->bus_name) == 0) {
912 _D("remove trusted busname item(%s, %s)", item->app_id, item->bus_name);
913 FREE_AND_NULL(item->app_id);
914 FREE_AND_NULL(item->bus_name);
916 g_hash_table_iter_remove(&iter);
918 __esd_trusted_busname_print_items();
924 static void __esd_filter_name_owner_changed(GDBusConnection *connection,
925 const gchar *sender_name, const gchar *object_path,
926 const gchar *interface_name, const gchar *signal_name,
927 GVariant *parameters, gpointer user_data)
930 char *old_owner = NULL;
931 char *new_owner = NULL;
935 g_variant_get(parameters, "(sss)", &name, &old_owner, &new_owner);
937 if (strstr(name, "event.busname.session")) {
938 old_len = strlen(old_owner);
939 new_len = strlen(new_owner);
941 _D("changed name(%s), old_onwer(%s)(%d) -> new_onwer(%s)(%d)",
942 name, old_owner, old_len, new_owner, new_len);
944 if (old_len > 0 && new_len == 0)
945 __esd_trusted_busname_remove_item(name);
946 else if (old_len == 0 && new_len > 0)
947 _D("new name owned");
949 _E("not-expected name change");
953 static int __esd_dbus_name_monitor(GDBusConnection *connection)
955 guint name_owner_changed_id = 0;
957 name_owner_changed_id = g_dbus_connection_signal_subscribe(connection,
958 "org.freedesktop.DBus", "org.freedesktop.DBus",
959 "NameOwnerChanged", "/org/freedesktop/DBus", NULL, G_DBUS_SIGNAL_FLAGS_NONE,
960 __esd_filter_name_owner_changed, NULL, NULL);
962 _I("name_owner_changed_id(%d)", name_owner_changed_id);
967 static int __esd_get_user_items(void)
974 ret = sd_get_uids(&uids);
976 _E("failed to get uids (%d)", ret);
980 if (ret == 0 || uids == NULL) {
981 _I("there is no uid for now");
983 /* reset user's item */
984 __esd_launch_table_remove_private_usr_items();
985 for (i = 0; i < ret; i++) {
987 _I("found uid(%d)", cur_uid);
989 ret = pkgmgrinfo_appinfo_get_usr_installed_list(__esd_add_appinfo_handler, cur_uid, &cur_uid);
991 _E("failed to get user(%d)-app list (%d)", cur_uid, ret);
995 __esd_launch_table_print_items();
1000 static Eina_Bool __esd_fd_handler_func(void *data, Ecore_Fd_Handler *fd_handler)
1002 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ)) {
1004 __esd_get_user_items();
1007 return ECORE_CALLBACK_CANCEL;
1010 static int __esd_start_sd_monitor(void)
1015 ret = __esd_get_user_items();
1019 ret = sd_login_monitor_new("uid", &g_sd_monitor);
1021 _E("sd_login_monitor_new error (%d)", ret);
1025 fd = sd_login_monitor_get_fd(g_sd_monitor);
1027 _E("sd_login_monitor_get_fd error");
1028 sd_login_monitor_unref(g_sd_monitor);
1032 g_fd_handler = ecore_main_fd_handler_add(fd,
1033 (Ecore_Fd_Handler_Flags)(ECORE_FD_READ | ECORE_FD_ERROR),
1034 __esd_fd_handler_func, NULL, NULL, NULL);
1035 if (g_fd_handler == NULL) {
1036 _E("fd_handler is NULL");
1037 sd_login_monitor_unref(g_sd_monitor);
1041 _I("setup sd-monitor done");
1046 static int __esd_stop_sd_monitor(void)
1048 _I("stop sd_monitor");
1050 ecore_main_fd_handler_del(g_fd_handler);
1051 g_fd_handler = NULL;
1054 sd_login_monitor_unref(g_sd_monitor);
1060 static GDBusNodeInfo *introspection_data;
1061 static const gchar introspection_xml[] =
1063 " <interface name='tizen.system.event.app2esd'>"
1064 " <method name='CheckSenderValidation'>"
1065 " <arg type='i' name='senderpid' direction='in'/>"
1066 " <arg type='s' name='eventname' direction='in'/>"
1067 " <arg type='i' name='ret' direction='out'/>"
1068 " <arg type='s' name='senderid' direction='out'/>"
1070 " <method name='GetTrustedPeerList'>"
1071 " <arg type='s' name='eventname' direction='in'/>"
1072 " <arg type='i' name='ret' direction='out'/>"
1073 " <arg type='as' name='dest_list' direction='out'/>"
1075 " <method name='SetupTrustedPeer'>"
1076 " <arg type='s' name='eventname' direction='in'/>"
1077 " <arg type='s' name='destination' direction='in'/>"
1078 " <arg type='i' name='ret' direction='out'/>"
1080 " <method name='CheckPrivilegeValidation'>"
1081 " <arg type='s' name='eventname' direction='in'/>"
1082 " <arg type='i' name='ret' direction='out'/>"
1084 " <method name='CheckUserSendValidation'>"
1085 " <arg type='s' name='eventname' direction='in'/>"
1086 " <arg type='i' name='ret' direction='out'/>"
1088 " <method name='RequestTrustedEventLaunch'>"
1089 " <arg type='s' name='eventname' direction='in'/>"
1090 " <arg type='s' name='eventdata' direction='in'/>"
1091 " <arg type='i' name='datalen' direction='in'/>"
1092 " <arg type='i' name='ret' direction='out'/>"
1094 " <method name='RequestEventLaunch'>"
1095 " <arg type='s' name='eventname' direction='in'/>"
1096 " <arg type='s' name='eventdata' direction='in'/>"
1097 " <arg type='i' name='datalen' direction='in'/>"
1098 " <arg type='i' name='ret' direction='out'/>"
1100 " <method name='RequestSendingEvent'>"
1101 " <arg type='s' name='eventname' direction='in'/>"
1102 " <arg type='s' name='eventdata' direction='in'/>"
1103 " <arg type='i' name='datalen' direction='in'/>"
1104 " <arg type='i' name='ret' direction='out'/>"
1106 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
1107 " <method name='GetEarlierData'>"
1108 " <arg type='s' name='appid' direction='in'/>"
1109 " <arg type='i' name='ret' direction='out'/>"
1110 " <arg type='i' name='len' direction='out'/>"
1111 " <arg type='s' name='earlier_data' direction='out'/>"
1117 static int __esd_get_appid_by_pid(int pid, uid_t uid, char *app_id, int buf_size)
1119 int retval = ES_R_OK;
1123 _E("invalid pid(%d)", pid);
1124 retval = ES_R_ERROR;
1125 } else if (uid <= 0) {
1126 _E("invalid uid(%d)", uid);
1127 retval = ES_R_ERROR;
1129 ret = aul_app_get_appid_bypid_for_uid(pid, app_id, buf_size, (uid_t)uid);
1130 if (ret != AUL_R_OK) {
1131 _E("failed to get appid by pid");
1132 retval = ES_R_ERROR;
1134 _D("pid(%d)-uid(%d)-appid(%s)", pid, uid, app_id);
1140 static int check_user_event_sender_valid(const char *event_name, const char *app_id)
1142 char *valid_name = NULL;
1143 char *temp_name = NULL;
1145 int retval = ES_R_OK;
1147 int valid_name_len = 0;
1149 temp_name = strdup(event_name);
1150 if (temp_name == NULL) {
1151 _E("out of memory");
1155 tmp = strrchr(temp_name, '.');
1156 if (tmp == NULL || strlen(tmp) == 0) {
1157 _E("invalid event name");
1158 FREE_AND_NULL(temp_name);
1162 if (len <= 1 || len > 128) {
1163 _E("invalid length(%d) of user-defined name");
1164 FREE_AND_NULL(temp_name);
1169 _D("app_id(%s), len(%d)", app_id, strlen(app_id));
1171 valid_name_len = strlen(USER_EVENT_NAME_PREFIX) + strlen(app_id) + 1;
1172 valid_name = calloc(1, valid_name_len);
1173 if (valid_name == NULL) {
1174 _E("memory alloc failed");
1175 FREE_AND_NULL(temp_name);
1178 snprintf(valid_name, valid_name_len, "%s%s", USER_EVENT_NAME_PREFIX, app_id);
1179 _D("valid_name(%s)", valid_name);
1181 if (strcmp(temp_name, valid_name) != 0) {
1182 _E("appid misamatch");
1183 retval = ES_R_EINVAL;
1186 FREE_AND_NULL(temp_name);
1187 FREE_AND_NULL(valid_name);
1192 static void check_sender_valid_method_call(GDBusConnection *connection, const gchar *sender,
1193 GVariant *parameters, GDBusMethodInvocation *invocation)
1195 GVariant *param = NULL;
1197 char *event_name = NULL;
1198 char app_id[128] = {0, };
1199 int event_sender_pid = 0;
1200 uid_t sender_uid = 0;
1202 g_variant_get(parameters, "(is)", &event_sender_pid, &event_name);
1203 _D("event_sender_pid(%d), event_name(%s)", event_sender_pid, event_name);
1205 sender_uid = (uid_t)__get_sender_uid(connection, sender);
1206 if (__esd_get_appid_by_pid(event_sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
1207 result = ES_R_ERROR;
1209 if (check_user_event_sender_valid(event_name, app_id) < 0) {
1210 _E("invalid sender");
1211 result = ES_R_EINVAL;
1217 param = g_variant_new("(is)", result, app_id);
1218 _D("event_name(%s), result(%d)", event_name, result);
1219 g_dbus_method_invocation_return_value(invocation, param);
1222 static void check_send_event_valid_method_call(GDBusConnection *connection, const gchar *sender,
1223 GVariant *parameters, GDBusMethodInvocation *invocation)
1225 GVariant *param = NULL;
1227 char *event_name = NULL;
1228 char app_id[128] = {0, };
1230 uid_t sender_uid = 0;
1232 g_variant_get(parameters, "(s)", &event_name);
1233 _D("event_name(%s)", event_name);
1235 sender_pid = __get_sender_pid(connection, sender);
1236 sender_uid = (uid_t)__get_sender_uid(connection, sender);
1237 if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
1238 result = ES_R_ERROR;
1240 if (check_user_event_sender_valid(event_name, app_id) < 0) {
1241 _E("invalid sender");
1242 result = ES_R_EINVAL;
1248 param = g_variant_new("(i)", result);
1249 _D("event_name(%s), result(%d)", event_name, result);
1250 g_dbus_method_invocation_return_value(invocation, param);
1253 static void get_trusted_peer_method_call(GDBusConnection *connection, const gchar *sender,
1254 GVariant *parameters, GDBusMethodInvocation *invocation)
1256 GVariant *param = NULL;
1258 GVariantBuilder *builder = NULL;
1259 GHashTableIter iter;
1260 gpointer key, value;
1261 char *event_name = NULL;
1262 char app_id[128] = {0, };
1264 uid_t sender_uid = 0;
1267 char *_appid = NULL;
1268 char *_busname = NULL;
1271 g_variant_get(parameters, "(s)", &event_name);
1272 _D("event_name(%s)", event_name);
1274 sender_pid = __get_sender_pid(connection, sender);
1275 sender_uid = (uid_t)__get_sender_uid(connection, sender);
1276 if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
1277 result = ES_R_ERROR;
1279 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1281 g_hash_table_iter_init(&iter, trusted_busname_table);
1282 while (g_hash_table_iter_next(&iter, &key, &value)) {
1283 item = (trusted_item *)value;
1285 _appid = item->app_id;
1286 _busname = item->bus_name;
1288 if (uid != GLOBAL_USER && uid != sender_uid)
1291 ret = __esd_check_certificate_match(uid, _appid, sender_uid, app_id);
1293 g_variant_builder_add(builder, "s", _busname);
1299 param = g_variant_new("(ias)", result, builder);
1300 _D("result(%d)", result);
1301 g_dbus_method_invocation_return_value(invocation, param);
1303 g_variant_builder_unref(builder);
1306 static void setup_trusted_peer_method_call(GDBusConnection *connection, const gchar *sender,
1307 GVariant *parameters, GDBusMethodInvocation *invocation)
1309 GVariant *param = NULL;
1311 char *event_name = NULL;
1312 char *destination_name = NULL;
1313 char app_id[128] = {0, };
1315 uid_t sender_uid = 0;
1318 g_variant_get(parameters, "(ss)", &event_name, &destination_name);
1319 _D("event_name(%s), destination_name(%s)", event_name, destination_name);
1321 if (destination_name && destination_name[0] != '\0') {
1322 sender_pid = __get_sender_pid(connection, sender);
1323 sender_uid = (uid_t)__get_sender_uid(connection, sender);
1324 if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
1325 result = ES_R_ERROR;
1327 ret = __esd_trusted_busname_add_item(sender_uid, app_id, destination_name,
1330 _E("failed to add trusted busname item");
1331 result = ES_R_ERROR;
1337 _E("invalid destination name");
1338 result = ES_R_ERROR;
1341 param = g_variant_new("(i)", result);
1342 _D("event_name(%s), result(%d)", event_name, result);
1343 g_dbus_method_invocation_return_value(invocation, param);
1346 static void check_privilege_valid_method_call(GDBusConnection *connection, const gchar *sender,
1347 GVariant *parameters, GDBusMethodInvocation *invocation)
1349 GVariant *param = NULL;
1351 char *event_name = NULL;
1352 char *privilege_name = NULL;
1353 char app_id[128] = {0, };
1355 uid_t sender_uid = 0;
1356 char *client = NULL;
1357 char *session = NULL;
1361 g_variant_get(parameters, "(s)", &event_name);
1362 __esd_check_privilege_name(event_name, &privilege_name);
1363 _D("event_name(%s), privilege_name(%s)", event_name, privilege_name);
1365 if (privilege_name) {
1366 sender_pid = __get_sender_pid(connection, sender);
1367 sender_uid = (uid_t)__get_sender_uid(connection, sender);
1368 if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) {
1369 result = ES_R_ERROR;
1371 ret = cynara_creds_gdbus_get_client(connection, sender, CLIENT_METHOD_DEFAULT, &client);
1372 if (ret != CYNARA_API_SUCCESS) {
1373 _E("failed to get client");
1374 result = ES_R_EINVAL;
1378 ret = cynara_creds_gdbus_get_user(connection, sender, USER_METHOD_DEFAULT, &user);
1379 if (ret != CYNARA_API_SUCCESS) {
1380 _E("failed to get user");
1381 result = ES_R_EINVAL;
1385 session = cynara_session_from_pid(sender_pid);
1386 if (session == NULL) {
1387 _E("failed to get session");
1388 result = ES_R_EINVAL;
1392 _D("app_id(%s), client(%s), session(%s), user(%s)", app_id, client, session, user);
1393 if (__esd_check_valid_privilege_by_cynara(app_id, client, session, user, privilege_name))
1396 result = ES_R_EINVAL;
1406 param = g_variant_new("(i)", result);
1407 _D("event_name(%s), result(%d)", event_name, result);
1408 g_dbus_method_invocation_return_value(invocation, param);
1411 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
1412 static void get_earlier_data_method_call(GVariant *parameters, GDBusMethodInvocation *invocation)
1414 GVariant *param = NULL;
1416 char *event_name = NULL;
1418 bundle_raw *raw = NULL;
1422 g_variant_get(parameters, "(s)", &event_name);
1424 if (event_name && strlen(event_name) > 0) {
1425 _D("event_name(%s)", event_name);
1428 _E("invalid event_name(%s)", event_name);
1429 result = ES_R_ERROR;
1432 item = (earlier_item *)g_hash_table_lookup(earlier_event_table, event_name);
1434 if (item->earlier_data) {
1435 b = bundle_dup(item->earlier_data);
1436 bundle_add_str(b, "is_earlier_data", "true");
1437 bundle_encode(b, &raw, &len);
1442 param = g_variant_new("(iis)", result, len, raw);
1444 _D("result(%d), len(%d)", result, len);
1445 g_dbus_method_invocation_return_value(invocation, param);
1447 bundle_free_encoded_rawdata(&raw);
1451 static void handle_method_call(GDBusConnection *connection,
1452 const gchar *sender, const gchar *object_path,
1453 const gchar *interface_name, const gchar *method_name,
1454 GVariant *parameters, GDBusMethodInvocation *invocation,
1457 if (g_strcmp0(method_name, "CheckSenderValidation") == 0) {
1458 check_sender_valid_method_call(connection, sender, parameters, invocation);
1459 } else if (g_strcmp0(method_name, "GetTrustedPeerList") == 0) {
1460 get_trusted_peer_method_call(connection, sender, parameters, invocation);
1461 } else if (g_strcmp0(method_name, "SetupTrustedPeer") == 0) {
1462 setup_trusted_peer_method_call(connection, sender, parameters, invocation);
1463 } else if (g_strcmp0(method_name, "CheckPrivilegeValidation") == 0) {
1464 check_privilege_valid_method_call(connection, sender, parameters, invocation);
1465 } else if (g_strcmp0(method_name, "CheckUserSendValidation") == 0) {
1466 check_send_event_valid_method_call(connection, sender, parameters, invocation);
1467 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
1468 } else if (g_strcmp0(method_name, "GetEarlierData") == 0) {
1469 get_earlier_data_method_call(parameters, invocation);
1474 static const GDBusInterfaceVTable interface_vtable = {
1480 static void __esd_on_bus_acquired(GDBusConnection *connection,
1481 const gchar *name, gpointer user_data)
1483 _I("bus acquired(%s)", name);
1486 GError *error = NULL;
1488 reg_id = g_dbus_connection_register_object(connection,
1490 introspection_data->interfaces[0],
1492 NULL, NULL, &error);
1494 _E("g_dbus_connection_register_object error(%s)", error->message);
1495 g_error_free(error);
1499 static void __esd_on_name_acquired(GDBusConnection *connection,
1500 const gchar *name, gpointer user_data)
1504 _I("name acquired(%s)", name);
1506 __esd_check_trusted_events(connection, "ListNames");
1507 __esd_check_trusted_events(connection, "ListActivatableNames");
1509 b = bundle_create();
1510 bundle_add_str(b, EVT_KEY_ESD_STATUS, EVT_VAL_ESD_STARTED);
1511 eventsystem_send_system_event(SYS_EVENT_ESD_STATUS, b);
1514 __esd_register_vconf_callbacks();
1516 __esd_trusted_busname_print_items();
1518 __esd_start_sd_monitor();
1520 __esd_dbus_name_monitor(connection);
1523 static void __esd_on_name_lost(GDBusConnection *connection,
1524 const gchar *name, gpointer user_data)
1526 _E("name lost(%s)", name);
1529 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);
1660 _I("get event launch list");
1662 /* get global user info */
1664 ret = pkgmgrinfo_appinfo_get_usr_installed_list(__esd_add_appinfo_handler, uid, &uid);
1666 _E("failed to get global-app list (%d)", ret);
1670 __esd_launch_table_print_items();
1672 trusted_busname_table = g_hash_table_new(g_str_hash, g_str_equal);
1674 /* gdbus setup for method call */
1675 introspection_data = g_dbus_node_info_new_for_xml(introspection_xml, &error);
1676 if (!introspection_data) {
1677 _E("g_dbus_node_info_new_for_xml error(%s)", error->message);
1678 g_error_free(error);
1682 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1684 G_BUS_NAME_OWNER_FLAGS_NONE,
1685 __esd_on_bus_acquired,
1686 __esd_on_name_acquired,
1690 _E("g_bus_own_name error");
1691 g_dbus_node_info_unref(introspection_data);
1695 _I("esd before_loop done");
1700 static void __esd_pkgmgr_event_free(esd_pkgmgr_event *pkg_event)
1702 pkg_event->type = UNKNOWN;
1703 if (pkg_event->pkgid) {
1704 free(pkg_event->pkgid);
1705 pkg_event->pkgid = NULL;
1709 static int __esd_appcontrol_cb(const char *operation,
1710 const char *uri, const char *mime, void *data)
1712 esd_appctrl_cb_data *cb_data = (esd_appctrl_cb_data *)data;
1715 char *event_name = NULL;
1716 const char *prefix = "event://";
1719 if (cb_data == NULL) {
1723 appid = cb_data->appid;
1724 pkgid = cb_data->pkgid;
1727 _D("uid(%d), appid(%s), pkgid(%s), operation(%s), uri(%s), mime(%s)",
1728 uid, appid, pkgid, operation, uri, mime);
1730 if (!strcmp(operation, APPSVC_OPERATION_LAUNCH_ON_EVENT)) {
1731 if (!strncmp(uri, prefix, strlen(prefix))) {
1732 event_name = strdup(&uri[8]);
1734 _D("appid(%s), event_name(%s)", appid, event_name);
1735 if (!__esd_check_event_launch_support(event_name)) {
1736 _E("failed to add item (not support event)");
1737 } else if (!__esd_check_app_privileged_event(uid, appid, pkgid, event_name)) {
1738 _E("failed to add item (no privilege)");
1740 if (__esd_add_launch_item(uid, event_name, appid, pkgid))
1741 _E("failed to add item");
1743 FREE_AND_NULL(event_name);
1745 _E("out of memory");
1748 _E("Invalid uri(%s) for event_name", uri);
1755 static int __esd_add_appinfo_handler(const pkgmgrinfo_appinfo_h handle, void *data)
1759 pkgmgrinfo_app_component component_type;
1761 uid_t *p_uid = NULL;
1768 p_uid = (uid_t *)data;
1770 ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
1772 _E("failed to get appid");
1776 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgid);
1778 _E("failed to get pkgid");
1782 ret = pkgmgrinfo_appinfo_get_component(handle, &component_type);
1783 if (ret != PMINFO_R_OK) {
1784 _E("fail to get component type");
1788 _D("uid(%d), appid(%s), component_type(%d)", *p_uid, appid, component_type);
1789 if (component_type == PMINFO_SVC_APP) {
1790 esd_appctrl_cb_data *cb_data = calloc(1, sizeof(esd_appctrl_cb_data));
1791 if (cb_data == NULL) {
1792 _E("memory alloc failed");
1795 cb_data->appid = strdup(appid);
1796 if (cb_data->appid == NULL) {
1797 _E("out_of_memory");
1798 FREE_AND_NULL(cb_data);
1801 cb_data->pkgid = strdup(pkgid);
1802 if (cb_data->pkgid == NULL) {
1803 _E("out_of_memory");
1804 FREE_AND_NULL(cb_data->appid);
1805 FREE_AND_NULL(cb_data);
1808 cb_data->uid = *p_uid;
1809 ret = pkgmgrinfo_appinfo_foreach_appcontrol(handle,
1810 (pkgmgrinfo_app_control_list_cb)__esd_appcontrol_cb, cb_data);
1812 FREE_AND_NULL(cb_data->pkgid);
1813 FREE_AND_NULL(cb_data->appid);
1814 FREE_AND_NULL(cb_data);
1817 _E("failed to get appcontrol info");
1820 __esd_launch_table_print_items();
1826 static int __esd_pkgmgr_event_callback(uid_t target_uid, int req_id, const char *pkg_type,
1827 const char *pkgid, const char *key, const char *val,
1828 const void *pmsg, void *data)
1830 esd_pkgmgr_event *pkg_event = (esd_pkgmgr_event *)data;
1831 pkgmgrinfo_pkginfo_h handle = NULL;
1834 _D("target_uid(%d), req_id(%d), pkg_type(%s), pkgid(%s), key(%s), val(%s)",
1835 target_uid, req_id, pkg_type, pkgid, key, val);
1837 if (strncmp(key, "start", strlen(key)) == 0) {
1838 if (strcmp(val, "install") == 0) {
1839 _D("install start");
1840 pkg_event->type = INSTALL;
1841 } else if (strcmp(val, "uninstall") == 0) {
1842 _D("unistall start");
1843 pkg_event->type = UNINSTALL;
1844 } else if (strcmp(val, "update") == 0) {
1846 pkg_event->type = UPDATE;
1848 _D("val(%s) start", val);
1849 __esd_pkgmgr_event_free(pkg_event);
1851 } else if (strcmp(key, "end") == 0 && strcmp(val, "ok") == 0) {
1852 if (pkg_event->type == INSTALL || pkg_event->type == UPDATE) {
1853 _D("install end (ok)");
1854 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, target_uid, &handle);
1856 _E("failed to get pkginfo");
1857 __esd_pkgmgr_event_free(pkg_event);
1860 ret = pkgmgrinfo_appinfo_get_usr_list(handle,
1861 PMINFO_ALL_APP, __esd_add_appinfo_handler, &target_uid, target_uid);
1863 _E("failed to get appinfo");
1864 __esd_pkgmgr_event_free(pkg_event);
1867 ret = pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1869 _E("failed to destroy pkginfo");
1870 __esd_pkgmgr_event_free(pkg_event);
1873 } else if (pkg_event->type == UNINSTALL) {
1874 _D("uninstall end (ok)");
1875 __esd_launch_table_remove_items(target_uid, pkgid);
1876 __esd_launch_table_print_items();
1878 __esd_pkgmgr_event_free(pkg_event);
1879 } else if (strcmp(key, "end") == 0 && strcmp(val, "fail") == 0) {
1880 _E("pkg_event(%d) falied", pkg_event->type);
1881 __esd_pkgmgr_event_free(pkg_event);
1883 if (strcmp(key, "install_percent") != 0)
1884 __esd_pkgmgr_event_free(pkg_event);
1890 static int __esd_init()
1894 pkgmgr_client *client;
1895 esd_pkgmgr_event *pkg_event;
1897 #if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36)
1902 __esd_init_cynara();
1904 client = pkgmgr_client_new(PC_LISTENING);
1905 if (client == NULL) {
1906 _E("set pkgmgr client failed");
1910 pkg_event = calloc(1, sizeof(esd_pkgmgr_event));
1911 if (pkg_event == NULL) {
1912 _E("memory alloc failed");
1913 ret = pkgmgr_client_free(client);
1914 if (ret != PKGMGR_R_OK)
1915 _E("pkgmgr_client_free failed(%d)", ret);
1920 req_id = pkgmgr_client_listen_status(client, __esd_pkgmgr_event_callback, pkg_event);
1922 _E("pkgmgr client listen failed");
1923 ret = pkgmgr_client_free(client);
1924 if (ret != PKGMGR_R_OK)
1925 _E("pkgmgr_client_free failed(%d)", ret);
1930 s_info.client = client;
1932 _I("esd init done");
1937 static void __esd_remove_esd_list_item(gpointer data, gpointer user_data)
1939 esd_list_item_s *item = (esd_list_item_s *)data;
1945 static void __esd_finalize(void)
1949 GHashTableIter iter;
1951 event_launch_item *el_item;
1953 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
1954 earlier_item *er_item;
1959 __esd_stop_sd_monitor();
1961 if (trusted_busname_table) {
1962 g_hash_table_iter_init(&iter, trusted_busname_table);
1963 while (g_hash_table_iter_next(&iter, &key, &value)) {
1964 item = (trusted_item *)value;
1967 free(item->bus_name);
1972 g_hash_table_iter_remove(&iter);
1974 g_hash_table_unref(trusted_busname_table);
1977 #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE
1978 if (earlier_event_table) {
1979 g_hash_table_iter_init(&iter, earlier_event_table);
1980 while (g_hash_table_iter_next(&iter, &key, &value)) {
1981 er_item = (earlier_item *)value;
1983 eventsystem_unregister_event(er_item->reg_id);
1984 free(er_item->event_name);
1985 bundle_free(er_item->earlier_data);
1988 _E("ealier item is NULL");
1990 g_hash_table_iter_remove(&iter);
1992 g_hash_table_unref(earlier_event_table);
1996 if (event_launch_table) {
1997 g_hash_table_iter_init(&iter, event_launch_table);
1998 while (g_hash_table_iter_next(&iter, &key, &value)) {
1999 el_item = (event_launch_item *)value;
2001 eventsystem_unregister_event(el_item->reg_id);
2002 free(el_item->event_name);
2003 g_list_foreach(el_item->app_list_evtlaunch,
2004 __esd_remove_esd_list_item, NULL);
2005 g_list_free(el_item->app_list_evtlaunch);
2010 g_hash_table_iter_remove(&iter);
2012 g_hash_table_unref(event_launch_table);
2015 if (introspection_data)
2016 g_dbus_node_info_unref(introspection_data);
2018 if (s_info.client) {
2019 ret = pkgmgr_client_free(s_info.client);
2020 if (ret != PKGMGR_R_OK)
2021 _E("pkgmgr_client_free failed(%d)", ret);
2024 __esd_finish_cynara();
2026 _D("esd finalize end");
2029 int main(int argc, char *argv[])
2031 _I("event system daemon : main()");
2033 if (__esd_init() != 0) {
2034 _E("ESD Initialization failed!");
2038 if (__esd_before_loop() < 0) {
2044 ecore_main_loop_begin();