From 9597aa4c4db9cd48d49d8a3927b2ea1f7b83ac70 Mon Sep 17 00:00:00 2001 From: Jiwoong Im Date: Thu, 28 May 2015 10:45:43 +0900 Subject: [PATCH 2/6] libeventsystem : initail commit MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This package provides new event-system API. Event Publication - Can broadcast the specific event and it’s data to all the listeners. (sender/receiver) On-Event-Launch (service-application only) - Can launch the dedicated application when the specific event occurs. JIRA ticket : TC-2473 Change-Id: I1990ed0d028f245c2edfdb540a0641534bdb7398 Signed-off-by: Jiwoong Im --- AUTHORS | 2 + CMakeLists.txt | 54 ++ LICENSE | 204 ++++++ eventsystem.manifest.in | 5 + eventsystem.pc.in | 13 + include/eventsystem.h | 177 +++++ include/eventsystem_internal.h | 273 +++++++ packaging/eventsystem.spec | 63 ++ src/eventsystem.c | 1579 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 2370 insertions(+) create mode 100644 AUTHORS create mode 100755 CMakeLists.txt create mode 100644 LICENSE create mode 100644 eventsystem.manifest.in create mode 100644 eventsystem.pc.in create mode 100755 include/eventsystem.h create mode 100644 include/eventsystem_internal.h create mode 100755 packaging/eventsystem.spec create mode 100755 src/eventsystem.c diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..cf4d288 --- /dev/null +++ b/AUTHORS @@ -0,0 +1,2 @@ +Jongmyeong Ko +Jusung Son diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100755 index 0000000..4d0280e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,54 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) + +PROJECT(eventsystem C) + +SET(VERSION 0.0.1) +SET(VERSION_MAJOR 0) + +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) +SET(EXEC_PREFIX "\${prefix}") +SET(LIBDIR "\${prefix}/lib") +SET(INCLUDEDIR "\${prefix}/include") + +set(CMAKE_SKIP_BUILD_RPATH true) + +### Local include directories +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src) + +### Required packages +INCLUDE(FindPkgConfig) + +pkg_check_modules(libpkgs REQUIRED dlog bundle openssl glib-2.0) + +#FIND_LIBRARY(LIB_DL dl) + +FOREACH(flag ${libpkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") +ENDFOREACH(flag) + +## Additional flag +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") +SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g -Wall") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") + +## Linker flags +SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed") + +## build eventsystem library +add_library(eventsystem SHARED + src/eventsystem.c + ) + +#TARGET_LINK_LIBRARIES(eventsystem "-ldl") +TARGET_LINK_LIBRARIES(eventsystem ${libpkgs_LDFLAGS}) +SET_TARGET_PROPERTIES(eventsystem PROPERTIES SOVERSION ${VERSION_MAJOR}) +SET_TARGET_PROPERTIES(eventsystem PROPERTIES VERSION ${VERSION}) + +# pkgconfig file +CONFIGURE_FILE(${CMAKE_SOURCE_DIR}/eventsystem.pc.in ${CMAKE_BINARY_DIR}/eventsystem.pc @ONLY) +configure_file(eventsystem.manifest.in eventsystem.manifest @ONLY) + +INSTALL(TARGETS eventsystem DESTINATION ${LIB_INSTALL_DIR} COMPONENT RuntimeLibraries) +INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/eventsystem.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/eventsystem.h DESTINATION include) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/eventsystem_internal.h DESTINATION include) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9c13a9b --- /dev/null +++ b/LICENSE @@ -0,0 +1,204 @@ +Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/eventsystem.manifest.in b/eventsystem.manifest.in new file mode 100644 index 0000000..97e8c31 --- /dev/null +++ b/eventsystem.manifest.in @@ -0,0 +1,5 @@ + + + + + diff --git a/eventsystem.pc.in b/eventsystem.pc.in new file mode 100644 index 0000000..85881d4 --- /dev/null +++ b/eventsystem.pc.in @@ -0,0 +1,13 @@ +# Package Information for pkg-config + +prefix=/usr +exec_prefix=${prefix} +libdir=@LIB_INSTALL_DIR@ +includedir=${prefix}/include + +Name: libeventsystem +Description: event system library +Version: @VERSION@ +Requires: bundle openssl glib-2.0 gio-2.0 +Libs: -L${libdir} -leventsystem +Cflags: -I${includedir} diff --git a/include/eventsystem.h b/include/eventsystem.h new file mode 100755 index 0000000..ae4b723 --- /dev/null +++ b/include/eventsystem.h @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __EVENT_SYSTEM_H__ +#define __EVENT_SYSTEM_H__ + +/** + * header file for eventsystem + */ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define APPFW_EVENT_SYSTEM_EARLIER_FEATURE + +typedef enum _eventsystem_return_val { + ES_R_ENOTPERMITTED = -4, /* Not permitted */ + ES_R_ENOMEM = -3, /* Memory allocation error */ + ES_R_EINVAL = -2, /* Invalid argument */ + ES_R_ERROR = -1, /* General error */ + ES_R_OK = 0, /* General success */ + ES_R_REMOVE = 1 /* Neet to remove something */ +} eventsystem_return_val; + +typedef enum _eventsystem_event_type { + ES_TYPE_UNKNOWN = 0, /* unknown event */ + ES_TYPE_USER, /* user event */ + ES_TYPE_SYSTEM /* system event */ +} eventsystem_event_type; + +#define FREE_AND_NULL(ptr) do { \ + if (ptr) { \ + free((void *)ptr); \ + ptr = NULL; \ + } \ +} while (0) + + +/** + * APIs for Application Framework. + */ + +/** + * interface : Callback for app core + */ +typedef void (*eventsystem_cb)(const char *event_name, bundle_raw *event_data, + int len, void *user_data); + +/** + * function : Send the user-event + */ +API int eventsystem_send_user_event(const char *event_name, bundle *data, bool is_trusted); + +/** + * function : Register the event of the application + */ +API int eventsystem_register_application_event(const char *event_name, unsigned int *reg_id, + int *event_type, eventsystem_cb callback, void *user_data); + +/** + * function : Unregister the event of the application + */ +API int eventsystem_unregister_application_event(unsigned int reg_id); + +/** + *function : Finalizer for releasing all resources + */ +API int eventsystem_application_finalize(void); + + +/** + * APIs for Internal-Use (daemon or privileged app for sending system-event) + */ + +/** + * interface : Callback for internal use + * + * example : +#include +#include +void battery_level_event_handler(const char *event_name, bundle *data, void *user_data) +{ + const char *batt_level_status = NULL; + _I("battery event(%s) received", event_name); + + batt_level_status = bundle_get_val(data, EVT_KEY_BATTERY_LEVEL_STATUS); + _I("batt_level_status(%s)", batt_level_status); +} + * + */ + +typedef void (*eventsystem_handler)(const char *event_name, bundle *data, void *user_data); + +/** + * function : Register the event + * + * example : +#include +static void init_func(void *user_data) +{ + int ret = 0; + ret = eventsystem_register_event(SYS_EVENT_BATTERY_LEVEL_STATUS, + &batt_level_reg_id, + (eventsystem_handler)battery_level_event_handler, + user_data); + if (ret != ES_R_OK) { + printf("error"); + } +} + * + */ +API int eventsystem_register_event(const char *event_name, unsigned int *reg_id, + eventsystem_handler callback, void *user_data); + +/** + * function : Unregister the event + * + * example : +static void exit_func(void *data) +{ + int ret = 0; + ret = eventsystem_unregister_event(batt_level_reg_id); + if (ret != ES_R_OK) { + printf("error"); + } +} + * + */ +API int eventsystem_unregister_event(unsigned int reg_id); + +/** + * function : Send the system-event + * + * example : +static void send_func(void *user_data) +{ + bundle *b = NULL; + b = bundle_create(); + bundle_add_str(b, EVT_KEY_BATTERY_LEVEL_STATUS, EVT_VAL_BATTERY_LEVEL_HIGH); + eventsystem_send_system_event(SYS_EVENT_BATTERY_LEVEL_STATUS, b); + bundle_free(b); +} + * + */ +API int eventsystem_send_system_event(const char *event_name, bundle *data); + +/** + * function : Request sending the event + * note : This API is only for privileged application which can not use eventsystem_send_system_event() API because of dbus policy. This API need a privilege. + * example : similar to event_send_system_event(). + * + */ +API int eventsystem_request_sending_system_event(const char *event_name, bundle *data); + + +#ifdef __cplusplus +} +#endif + +#endif /* __EVENT_SYSTEM_H__ */ diff --git a/include/eventsystem_internal.h b/include/eventsystem_internal.h new file mode 100644 index 0000000..358882b --- /dev/null +++ b/include/eventsystem_internal.h @@ -0,0 +1,273 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __EVENTSYSTEM_INTERNAL_H__ +#define ___EVENTSYSTEM_INTERNAL_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#define ESD_BUS_NAME "tizen.system.event.app2esd" +#define ESD_OBJECT_PATH "/tizen/system/event/app2esd" +#define ESD_INTERFACE_NAME "tizen.system.event.app2esd" + +#define USER_EVENT_NAME_PREFIX "event." + +/** + * system-event definitions + */ + +/** esd : for checking esd available */ +#define SYS_EVENT_ESD_STATUS "tizen.system.event.esd_status" +/* key */ +#define EVT_KEY_ESD_STATUS "esd_status" +/* value */ +#define EVT_VAL_ESD_STARTED "started" +#define EVT_VAL_ESD_STOPPED "stopped" + +/** battery : charger status */ +#define SYS_EVENT_BATTERY_CHARGER_STATUS "tizen.system.event.battery_charger_status" +/* key */ +#define EVT_KEY_BATTERY_CHARGER_STATUS "battery_charger_status" +/* value */ +#define EVT_VAL_BATTERY_CHARGER_DISCONNECTED "disconnected" +#define EVT_VAL_BATTERY_CHARGER_CONNECTED "connected" +#define EVT_VAL_BATTERY_CHARGER_CHARGING "charging" + +/** battery : level status */ +#define SYS_EVENT_BATTERY_LEVEL_STATUS "tizen.system.event.battery_level_status" +/* key */ +#define EVT_KEY_BATTERY_LEVEL_STATUS "battery_level_status" +/* value */ +#define EVT_VAL_BATTERY_LEVEL_EMPTY "empty" +#define EVT_VAL_BATTERY_LEVEL_CRITICAL "critical" +#define EVT_VAL_BATTERY_LEVEL_LOW "low" +#define EVT_VAL_BATTERY_LEVEL_HIGH "high" +#define EVT_VAL_BATTERY_LEVEL_FULL "full" + +/** usb : status of usb connection */ +#define SYS_EVENT_USB_STATUS "tizen.system.event.usb_status" +/* key */ +#define EVT_KEY_USB_STATUS "usb_status" +/* value */ +#define EVT_VAL_USB_DISCONNECTED "disconnected" +#define EVT_VAL_USB_CONNECTED "connected" +#define EVT_VAL_USB_AVAILABLE "available" + +/** ear-jack : status of ear-jack connection */ +#define SYS_EVENT_EARJACK_STATUS "tizen.system.event.earjack_status" +/* key */ +#define EVT_KEY_EARJACK_STATUS "earjack_status" +/* value */ +#define EVT_VAL_EARJACK_DISCONNECTED "disconnected" +#define EVT_VAL_EARJACK_CONNECTED "connected" + +/** display : state of display */ +#define SYS_EVENT_DISPLAY_STATE "tizen.system.event.display_state" +/* key */ +#define EVT_KEY_DISPLAY_STATE "display_state" +/* value */ +#define EVT_VAL_DISPLAY_NORMAL "normal" +#define EVT_VAL_DISPLAY_DIM "dim" +#define EVT_VAL_DISPLAY_OFF "off" + +/** system : boot completion */ +#define SYS_EVENT_BOOT_COMPLETED "tizen.system.event.boot_completed" +/* key */ +#define EVT_KEY_BOOT_COMPLETED "boot_completed" +/* value */ +#define EVT_VAL_BOOT_COMPLETED_TRUE "true" + +/** system : shutdown */ +#define SYS_EVENT_SYSTEM_SHUTDOWN "tizen.system.event.system_shutdown" +/* key */ +#define EVT_KEY_SYSTEM_SHUTDOWN "system_shutdown" +/* value */ +#define EVT_VAL_SYSTEM_SHUTDOWN_TRUE "true" /* go to shutdown */ + +/** resource : low memory */ +#define SYS_EVENT_LOW_MEMORY "tizen.system.event.low_memory" +/* key */ +#define EVT_KEY_LOW_MEMORY "low_memory" +/* value */ +#define EVT_VAL_MEMORY_NORMAL "normal" +#define EVT_VAL_MEMORY_SOFT_WARNING "soft_warning" +#define EVT_VAL_MEMORY_HARD_WARNING "hard_warning" + +/** wifi : state of wifi */ +#define SYS_EVENT_WIFI_STATE "tizen.system.event.wifi_state" +/* key */ +#define EVT_KEY_WIFI_STATE "wifi_state" +/* value */ +#define EVT_VAL_WIFI_OFF "off" +#define EVT_VAL_WIFI_ON "on" +#define EVT_VAL_WIFI_CONNECTED "connected" + +/** bluetooth : state of bluetooth */ +#define SYS_EVENT_BT_STATE "tizen.system.event.bt_state" +/* key */ +#define EVT_KEY_BT_STATE "bt_state" +/* value */ +#define EVT_VAL_BT_OFF "off" +#define EVT_VAL_BT_ON "on" +/* key */ +#define EVT_KEY_BT_LE_STATE "bt_le_state" +/* value */ +#define EVT_VAL_BT_LE_OFF "off" +#define EVT_VAL_BT_LE_ON "on" +/* key */ +#define EVT_KEY_BT_TRANSFERING_STATE "bt_transfering_state" +/* value */ +#define EVT_VAL_BT_NON_TRANSFERING "non_transfering" +#define EVT_VAL_BT_TRANSFERING "transfering" + +/** location : enable state of location */ +#define SYS_EVENT_LOCATION_ENABLE_STATE "tizen.system.event.location_enable_state" +/* key */ +#define EVT_KEY_LOCATION_ENABLE_STATE "location_enable_state" +/* value */ +#define EVT_VAL_LOCATION_DISABLED "disabled" +#define EVT_VAL_LOCATION_ENABLED "enabled" + +/** location : enable state of gps */ +#define SYS_EVENT_GPS_ENABLE_STATE "tizen.system.event.gps_enable_state" +/* key */ +#define EVT_KEY_GPS_ENABLE_STATE "gps_enable_state" +/* value */ +#define EVT_VAL_GPS_DISABLED "disabled" +#define EVT_VAL_GPS_ENABLED "enabled" + +/** location : enable state of nps */ +#define SYS_EVENT_NPS_ENABLE_STATE "tizen.system.event.nps_enable_state" +/* key */ +#define EVT_KEY_NPS_ENABLE_STATE "nps_enable_state" +/* value */ +#define EVT_VAL_NPS_DISABLED "disabled" +#define EVT_VAL_NPS_ENABLED "enabled" + +/** message : incoming msg */ +#define SYS_EVENT_INCOMMING_MSG "tizen.system.event.incoming_msg" +/* key */ +#define EVT_KEY_MSG_TYPE "msg_type" +/* value */ +#define EVT_VAL_SMS "sms" +#define EVT_VAL_PUSH "push" +#define EVT_VAL_CB "cb" +/* key */ +#define EVT_KEY_MSG_ID "msg_id" +/* value description + *"{unsigned int value}" : new message id + */ + +/** setting : time changed */ +#define SYS_EVENT_TIME_CHANGED "tizen.system.event.time_changed" +/* key */ +#define EVT_KEY_TIME_CHANGED "time_changed" +/* value */ +/* do not set "false", just set "true" for broadcasting time_changed */ +#define EVT_VAL_TIME_CHANGED_TRUE "true" + +/** setting : timezone setting */ +#define SYS_EVENT_TIME_ZONE "tizen.system.event.time_zone" +/* key */ +#define EVT_KEY_TIME_ZONE "time_zone" +/* vlaue description + * "Asia/Seoul" : tzpaht value + */ + +/** setting : hour format */ +#define SYS_EVENT_HOUR_FORMAT "tizen.system.event.hour_format" +/* key */ +#define EVT_KEY_HOUR_FORMAT "hour_format" +/* value */ +#define EVT_VAL_HOURFORMAT_12 "12" +#define EVT_VAL_HOURFORMAT_24 "24" + +/** setting : language setting */ +#define SYS_EVENT_LANGUAGE_SET "tizen.system.event.language_set" +/* key */ +#define EVT_KEY_LANGUAGE_SET "language_set" +/* value description + * "ko_KR.UTF8" : in case of Korean language + * "en_US.UTF8" : in case of USA language + * ... + */ + +/** setting : region format */ +#define SYS_EVENT_REGION_FORMAT "tizen.system.event.region_format" +/* key */ +#define EVT_KEY_REGION_FORMAT "region_format" +/* value description + * "ko_KR.UTF8" : in case of Korean region format + * "en_US.UTF8" : in case of USA region format + * "en_UK.UTF8" : in case of United Kingdom + * ... + */ + +/** setting : silent mode */ +#define SYS_EVENT_SILENT_MODE "tizen.system.event.silent_mode" +/* key */ +#define EVT_KEY_SILENT_MODE "silent_mode" +/* value */ +#define EVT_VAL_SILENTMODE_ON "on" +#define EVT_VAL_SILENTMODE_OFF "off" + +/** setting : state of vibration */ +#define SYS_EVENT_VIBRATION_STATE "tizen.system.event.vibration_state" +/* key */ +#define EVT_KEY_VIBRATION_STATE "vibration_state" +/* value */ +#define EVT_VAL_VIBRATION_ON "on" +#define EVT_VAL_VIBRATION_OFF "off" + +/** setting : state of screen's auto-rotation */ +#define SYS_EVENT_SCREEN_AUTOROTATE_STATE "tizen.system.event.screen_autorotate_state" +/* key */ +#define EVT_KEY_SCREEN_AUTOROTATE_STATE "screen_autorotate_state" +/* value */ +#define EVT_VAL_SCREEN_AUTOROTATE_ON "on" +#define EVT_VAL_SCREEN_AUTOROTATE_OFF "off" + +/** setting : state of mobile data */ +#define SYS_EVENT_MOBILE_DATA_STATE "tizen.system.event.mobile_data_state" +/* key */ +#define EVT_KEY_MOBILE_DATA_STATE "mobile_data_state" +/* value */ +#define EVT_VAL_MOBILE_DATA_OFF "off" +#define EVT_VAL_MOBILE_DATA_ON "on" + +/** setting : state of data roaming */ +#define SYS_EVENT_DATA_ROAMING_STATE "tizen.system.event.data_roaming_state" +/* key */ +#define EVT_KEY_DATA_ROAMING_STATE "data_roaming_state" +/* value */ +#define EVT_VAL_DATA_ROAMING_OFF "off" +#define EVT_VAL_DATA_ROAMING_ON "on" + +/** setting : font setting */ +#define SYS_EVENT_FONT_SET "tizen.system.event.font_set" +/* key */ +#define EVT_KEY_FONT_SET "font_set" +/* value description + * font name of string type by font-config. + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __EVENTSYSTEM_INTERNAL_H__ */ diff --git a/packaging/eventsystem.spec b/packaging/eventsystem.spec new file mode 100755 index 0000000..0f09994 --- /dev/null +++ b/packaging/eventsystem.spec @@ -0,0 +1,63 @@ +Name: eventsystem +Summary: Event system library +Version: 0.0.1 +Release: 1 +Group: System/Libraries +License: Apache-2.0 +Source0: %{name}-%{version}.tar.gz +BuildRequires: cmake +BuildRequires: pkgconfig(ecore) +BuildRequires: pkgconfig(bundle) +BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(gio-2.0) +BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(openssl) + +Requires(post): /sbin/ldconfig +Requires(postun): /sbin/ldconfig + +%description +Event System Library + +%package devel +Summary: Event system library (devel) +Group: Development/Libraries +Requires: %{name} = %{version}-%{release} + +%description devel +Event system library (devel) + +%prep +%setup -q + +%build +export CFLAGS="$CFLAGS -DTIZEN_DEBUG_ENABLE" +export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE" +export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE" + + +%cmake . +make %{?jobs:-j%jobs} + +%install +rm -rf %{buildroot} +%make_install + +mkdir -p %{buildroot}/usr/share/license +cp LICENSE %{buildroot}/usr/share/license/%{name} + +%post -p /sbin/ldconfig + +%postun -p /sbin/ldconfig + +%files +%manifest eventsystem.manifest +%defattr(-,root,root,-) +%{_libdir}/libeventsystem.so.* +/usr/share/license/%{name} + +%files devel +%defattr(-,root,root,-) +%{_includedir}/*.h +%{_libdir}/libeventsystem.so +%{_libdir}/pkgconfig/eventsystem.pc diff --git a/src/eventsystem.c b/src/eventsystem.c new file mode 100755 index 0000000..8e0491d --- /dev/null +++ b/src/eventsystem.c @@ -0,0 +1,1579 @@ +/** + * event system low-level API + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#undef LOG_TAG +#define LOG_TAG "eventsystem" + +#define SYS_EVENT_NAME_PREFIX "tizen.system.event" +#define EVENT_SYSTEM_PREFIX "eventsystem.id_" +#define EVENT_SYSTEM_PREFIX_LEN 15 +#define EVENT_SYSTEM_MEMBER "eventsystem" +#define VALID_COUNT_OF_EVENTNAME_TOKEN 3 +#define VALID_LAST_COUNT_FOR_EVENTNAME (VALID_COUNT_OF_EVENTNAME_TOKEN + 1) +#define MAX_COUNT_FOR_EVENTNAME_CHECK (VALID_LAST_COUNT_FOR_EVENTNAME + 1) + +#define _E(fmt, arg...) LOGE(fmt, ##arg) +#define _D(fmt, arg...) LOGD(fmt, ##arg) +#define _W(fmt, arg...) LOGW(fmt, ##arg) +#define _I(fmt, arg...) LOGI(fmt, ##arg) + +#define retvm_if(expr, val, fmt, arg...) do { \ + if (expr) { \ + _E(fmt, ##arg); \ + _E("(%s) -> %s() return", #expr, __func__); \ + return val; \ + } \ +} while (0) + +#define retv_if(expr, val) do { \ + if (expr) { \ + _E("(%s) -> %s() return", #expr, __func__); \ + return val; \ + } \ +} while (0) + +pthread_mutex_t send_sync_lock = PTHREAD_MUTEX_INITIALIZER; + +static GList *system_event_list; +static int _initialized; +static GHashTable *filter_tbl; +static GHashTable *check_tbl; + +typedef struct eventmap { + char *event_name; + char *interface_name; + char *member_name; + guint reg_id; + GBusType bus_type; + int event_type; + union { + eventsystem_cb es_cb; + eventsystem_handler ep_cb; + }; +} eventmap_s; + +typedef struct eventinfo { + char *event_name; + char *interface_name; + char *object_path; + char *member_name; + bool is_user_event; + gboolean is_trusted; + bundle *event_data; +} eventinfo_s; + +typedef struct sysevent_info { + guint owner_id; + guint owner_id_session; +} sysevent_info_s; +static sysevent_info_s s_info; + +static int __eventsystem_request_event_launch(const char *event_name, bundle *data); +static bool __eventsystem_check_sender_validation_userevent(GDBusConnection *connection, + char *sender_name); +static int __eventsystem_check_user_send_validation(const char *event_name); +static int __eventsystem_check_user_certificate(int sender_pid); + +static int __event_compare_name_cb(gconstpointer a, gconstpointer b) +{ + eventmap_s *key1 = (eventmap_s *)a; + eventmap_s *key2 = (eventmap_s *)b; + return strcmp(key1->interface_name, key2->interface_name) | + strcmp(key1->member_name, key2->member_name); +} + +static int __event_compare_reg_id_cb(gconstpointer a, gconstpointer b) +{ + eventmap_s *key1 = (eventmap_s *)a; + eventmap_s *key2 = (eventmap_s *)b; + return !(key1->reg_id == key2->reg_id); +} + +static void __initialize(void) +{ + g_type_init(); + _initialized = 1; +} + +static char *__get_object_path(char *interface_name) +{ + int i; + char *object_path = (char *)calloc(strlen(interface_name), sizeof(char)+2); + + if (object_path == NULL) { + _E("failed to allocate memory"); + return NULL; + } + + object_path[0] = '/'; + + for (i = 0 ; interface_name[i] ; i++) { + + if (interface_name[i] == '.') { + object_path[i+1] = '/'; + } else { + object_path[i+1] = interface_name[i]; + } + } + + return object_path; +} + +static char *__get_encoded_interface_name(char *interface_name) +{ + unsigned char c[MD5_DIGEST_LENGTH] = {0, }; + char *md5_evtid = NULL; + char *temp; + int index = 0; + MD5_CTX mdContext; + + MD5_Init(&mdContext); + MD5_Update(&mdContext, interface_name, strlen(interface_name)); + MD5_Final(c, &mdContext); + + md5_evtid = (char *)calloc(EVENT_SYSTEM_PREFIX_LEN + (MD5_DIGEST_LENGTH * 2) + 1, + sizeof(char)); + if (md5_evtid == NULL) { + _D("Malloc failed!!"); + return NULL; + } + + sprintf(md5_evtid, "%s", EVENT_SYSTEM_PREFIX); + + temp = md5_evtid; + + temp += EVENT_SYSTEM_PREFIX_LEN; + + for (index = 0; index < MD5_DIGEST_LENGTH; index++) { + sprintf(temp, "%02x", c[index]); + temp += 2; + } + + return md5_evtid; +} + +static char *__get_member_name_from_eventname(char *event_name) +{ + char *ptr = NULL; + char *ptr_last = NULL; + char *temp_name = NULL; + char *member_name = NULL; + int count = 0; + + temp_name = strdup(event_name); + if (temp_name == NULL) { + _E("out of memory"); + return NULL; + } + + ptr = strtok(temp_name, "."); + if (ptr == NULL) { + _E("invalid event_name(%s), count(%d)", event_name, count); + return NULL; + } + count++; + + while (count < MAX_COUNT_FOR_EVENTNAME_CHECK) { + ptr = strtok(NULL, "."); + if (ptr == NULL) + break; + /* _D("(%d)ptr(%s)(%d)", count, ptr, strlen(ptr)); */ + ptr_last = ptr; + count++; + } + FREE_AND_NULL(temp_name); + + if (count != VALID_LAST_COUNT_FOR_EVENTNAME) { + _E("invalid event_name(%s), count(%d)", event_name, count); + return NULL; + } + + if (ptr_last) { + /* _D("new member_name(%s)(%d)", ptr_last, strlen(ptr_last)); */ + member_name = strdup(ptr_last); + if (!member_name) { + _E("out_of_memory"); + return NULL; + } + } else { + _E("ptr_last is NULL"); + return NULL; + } + + _D("member_name(%s)", member_name); + + return member_name; +} + +static int __check_validation_user_defined_name(const char *event_name) +{ + char *event_id = NULL; + char *key = NULL; + int ret = 1; + + if (check_tbl == NULL) { + check_tbl = g_hash_table_new(g_str_hash, g_str_equal); + } + + event_id = (char *)g_hash_table_lookup(check_tbl, event_name); + + if (event_id == NULL) { + if (__eventsystem_check_user_send_validation(event_name) < 0) { + _E("invalid user-event name"); + ret = 0; + } else { + key = strdup(event_name); + if (key == NULL) { + _E("out_of_memory"); + ret = 0; + } else { + g_hash_table_insert(check_tbl, key, key); + } + } + } + + return ret; +} + +static int __check_eventname_validation_user(char *event_name) +{ + int ret = 1; + int len = strlen(USER_EVENT_NAME_PREFIX); + + if (strncmp(event_name, USER_EVENT_NAME_PREFIX, len) != 0) { + ret = 0; + } + + return ret; +} + +static int __check_eventname_validation_system(char *event_name) +{ + int ret = 1; + int len = strlen(SYS_EVENT_NAME_PREFIX); + + if (strncmp(event_name, SYS_EVENT_NAME_PREFIX, len) != 0) { + ret = 0; + } + + return ret; +} + +static int __get_proc_status_by_pid(const char *what, int pid) +{ + int fd = 0; + int ret = 0; + int i = 0; + char ch = 0; + char _path[128] = {0, }; + char _buf[1024] = {0, }; + char *pval; + char *ptr; + + snprintf(_path, 128, "/proc/%d/status", pid); + + fd = open(_path, O_RDONLY); + if (fd < 0) { + _E("open file(%s) error(%d), pid(%d)", _path, fd, pid); + return fd; + } + + ret = read(fd, _buf, 1024); + if (ret < 0) { + _E("read file(%s) error(%d), pid(%d)", _path, ret, pid); + goto error; + } + + pval = strstr(_buf, what); + if (pval == NULL) { + _E("pval is NULL"); + ret = -1; + goto error; + } + + for (i = 0; i < strlen(pval); i++) { + ch = *pval; + if (ch == ' ' || ch == '\t') { + ret = (int)strtol(pval, &ptr, 10); + if (ret == 0) { + if ((int)strlen(ptr) >= (int)strlen(pval)) { + _E("wrong 0 value"); + ret = -2; + } + } + break; + } else if (ch == '\n') + break; + pval++; + } + +error: + close(fd); + + return ret; +} + +static int __get_gdbus_shared_connection(GDBusConnection **connection, + GBusType bus_type, const char *interface_name) +{ + GError *error = NULL; + guint owner_id = 0; + + if (!_initialized) { + __initialize(); + } + + *connection = g_bus_get_sync(bus_type, NULL, &error); + if (*connection == NULL) { + if (error != NULL) { + _E("Failed to get dbus [%s], bus_type [%d]", + error->message, bus_type); + g_error_free(error); + } + return ES_R_ERROR; + } + + if (interface_name && + ((bus_type == G_BUS_TYPE_SYSTEM && !s_info.owner_id) || + (bus_type == G_BUS_TYPE_SESSION && !s_info.owner_id_session))) { + char own_name[128] = {0, }; + snprintf(own_name, 128, "%s_%d_%d", interface_name, + bus_type, getpid()); + _D("own_name is [%s]", own_name); + owner_id = g_bus_own_name_on_connection(*connection, own_name, + G_BUS_NAME_OWNER_FLAGS_NONE, + NULL, NULL, NULL, NULL); + if (!owner_id) { + _E("g_bus_own_name_on_connection, error"); + return ES_R_ERROR; + } + if (bus_type == G_BUS_TYPE_SESSION) { + s_info.owner_id_session = owner_id; + } else { + s_info.owner_id = owner_id; + } + } + + return ES_R_OK; +} + +static int __get_sender_pid(GDBusConnection *conn, const char *sender_name) +{ + GDBusMessage *msg = NULL; + GDBusMessage *reply = NULL; + GError *err = NULL; + GVariant *body; + int pid = 0; + + msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus", + "org.freedesktop.DBus", "GetConnectionUnixProcessID"); + if (!msg) { + _D("Can't allocate new method call"); + goto out; + } + + g_dbus_message_set_body(msg, g_variant_new ("(s)", sender_name)); + reply = g_dbus_connection_send_message_with_reply_sync(conn, msg, + G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err); + + if (!reply) { + if (err != NULL) { + _E("Failed to get pid [%s]", err->message); + g_error_free(err); + } + goto out; + } + + body = g_dbus_message_get_body(reply); + g_variant_get(body, "(u)", &pid); + +out: + if (msg) + g_object_unref(msg); + if (reply) + g_object_unref(reply); + + return pid; +} + +static void __eventsystem_event_handler(GDBusConnection *connection, + const gchar *sender_name, const gchar *object_path, + const gchar *interface_name, const gchar *signal_name, + GVariant *parameters, gpointer user_data) +{ + int len; + eventmap_s em; + GList *cb_list = NULL; + bundle *buf = NULL; + bundle_raw *raw = NULL; + gboolean is_trusted = FALSE; + + em.interface_name = (char *)interface_name; + em.member_name = (char *)signal_name; + + _D("sender_name(%s), interface_name(%s), signal_name(%s)", + sender_name, interface_name, signal_name); + + cb_list = g_list_find_custom(system_event_list, &em, + (GCompareFunc)__event_compare_name_cb); + if (cb_list == NULL) { + return; + } + + g_variant_get(parameters, "(bus)", &is_trusted, &len, &raw); + + buf = bundle_decode((bundle_raw *)raw, len); + + em.event_name = ((eventmap_s *)cb_list->data)->event_name; + em.ep_cb = ((eventmap_s *)cb_list->data)->ep_cb; + if (em.ep_cb) { + em.ep_cb(em.event_name, buf, user_data); + } + + bundle_free_encoded_rawdata(&raw); + bundle_free(buf); +} + +static void __eventsystem_application_event_handler(GDBusConnection *connection, + const gchar *sender_name, const gchar *object_path, + const gchar *interface_name, const gchar *signal_name, + GVariant *parameters, gpointer user_data) +{ + GList *cb_list = NULL; + eventmap_s em; + bundle_raw *raw = NULL; + int len; + + em.interface_name = (char *)interface_name; + em.member_name = (char *)signal_name; + + _D("sender_name(%s), interface_name(%s), signal_name(%s)", + sender_name, interface_name, signal_name); + + cb_list = g_list_find_custom(system_event_list, &em, + (GCompareFunc)__event_compare_name_cb); + + if (cb_list == NULL) { + return; + } + + g_variant_get(parameters, "(bus)", NULL, &len, &raw); + + em.event_name = ((eventmap_s *)cb_list->data)->event_name; + em.es_cb = ((eventmap_s *)cb_list->data)->es_cb; + if (em.es_cb) { + em.es_cb(em.event_name, raw, len, user_data); + } + + bundle_free_encoded_rawdata(&raw); +} + +static bool __eventsystem_check_sender_validation_userevent(GDBusConnection *connection, + char *sender_name) +{ + char *sender_id = NULL; + char *key = NULL; + int sender_pid = 0; + + if (filter_tbl == NULL) { + filter_tbl = g_hash_table_new(g_str_hash, g_str_equal); + } + + sender_id = (char *)g_hash_table_lookup(filter_tbl, sender_name); + + if (sender_id == NULL) { + sender_pid = __get_sender_pid(connection, sender_name); + + if (sender_pid > 0) { + if (__eventsystem_check_user_certificate(sender_pid) < 0) { + _E("not match"); + return false; + } + + key = strdup(sender_name); + if (key == NULL) { + _E("out_of_memory"); + return false; + } + g_hash_table_insert(filter_tbl, key, key); + } else { + _E("failed to get sender_pid"); + return false; + } + } else { + _D("sender_id(%s)", sender_id); + } + + return true; +} + +/** + * application-use filter for user-event + */ +static void __eventsystem_filter_userevent_for_application(GDBusConnection *connection, + const gchar *sender_name, const gchar *object_path, + const gchar *interface_name, const gchar *signal_name, + GVariant *parameters, gpointer user_data) +{ + gboolean is_trusted = FALSE; + + _D("sender_name(%s), interface_name(%s)", sender_name, interface_name); + + g_variant_get(parameters, "(bus)", &is_trusted, NULL, NULL); + + /* check signature */ + if (is_trusted && + __eventsystem_check_sender_validation_userevent(connection, + (char *)sender_name) == false) { + return; + } + + __eventsystem_application_event_handler(connection, sender_name, + object_path, interface_name, signal_name, parameters, user_data); +} + +/** + * application-use filter for system-event + */ +static void __eventsystem_filter_sysevent_for_application(GDBusConnection *connection, + const gchar *sender_name, const gchar *object_path, + const gchar *interface_name, const gchar *signal_name, + GVariant *parameters, gpointer user_data) +{ + _D("sender_name(%s), interface_name(%s)", sender_name, interface_name); + + __eventsystem_application_event_handler(connection, sender_name, + object_path, interface_name, signal_name, parameters, user_data); +} + +/** + * internal-use filter for user-event + */ +static void __eventsystem_filter_userevent_for_internal(GDBusConnection *connection, + const gchar *sender_name, const gchar *object_path, + const gchar *interface_name, const gchar *signal_name, + GVariant *parameters, gpointer user_data) +{ + _D("sender_name(%s), interface_name(%s), signal_name(%s)", + sender_name, interface_name, signal_name); + + __eventsystem_event_handler(connection, sender_name, + object_path, interface_name, signal_name, parameters, user_data); +} + +/** + * internal-use filter for system-event + */ +static void __eventsystem_filter_sysevent_for_internal(GDBusConnection *connection, + const gchar *sender_name, const gchar *object_path, + const gchar *interface_name, const gchar *signal_name, + GVariant *parameters, gpointer user_data) +{ + _D("sender_name(%s), interface_name(%s), signal_name(%s)", + sender_name, interface_name, signal_name); + + __eventsystem_event_handler(connection, sender_name, + object_path, interface_name, signal_name, parameters, user_data); +} + +static int __eventsystem_register_event_internal(const char *event_name, + eventmap_s **em_s, void *user_data) +{ + eventmap_s *em = NULL; + char *interface_name = NULL; + char *object_path = NULL; + char *member_name = NULL; + char *sender_name = NULL; + GDBusSignalCallback filter; + GBusType bus_type; + guint subscription_id = 0; + int ret = 0; + int evt_type = ES_TYPE_UNKNOWN; + GDBusConnection *conn = NULL; + + if (__check_eventname_validation_system((char *)event_name)) { + evt_type = ES_TYPE_SYSTEM; + } else if (__check_eventname_validation_user((char *)event_name)) { + evt_type = ES_TYPE_USER; + } else { + evt_type = ES_TYPE_UNKNOWN; + _E("unknown type event(%s)", event_name); + return ES_R_EINVAL; + } + + if (evt_type == ES_TYPE_SYSTEM) { + interface_name = strdup(SYS_EVENT_NAME_PREFIX); + if (interface_name == NULL) { + _E("out of memory"); + ret = ES_R_ENOMEM; + goto out_1; + } + + member_name = __get_member_name_from_eventname((char *)event_name); + if (member_name == NULL) { + _E("invalid member_name"); + ret = ES_R_ERROR; + goto out_2; + } + + if (!g_dbus_is_member_name(member_name)) { + _E("invalid member name"); + ret = ES_R_EINVAL; + goto out_3; + } + filter = __eventsystem_filter_sysevent_for_internal; + } else { + interface_name = __get_encoded_interface_name((char *)event_name); + if (!interface_name) { + _E("interface_name is NULL"); + ret = ES_R_ERROR; + goto out_1; + } + if (!g_dbus_is_interface_name(interface_name)) { + _E("invalid interface_name(%s)", interface_name); + ret = ES_R_EINVAL; + goto out_2; + } + member_name = strdup(EVENT_SYSTEM_MEMBER); + if (!member_name) { + _E("out_of_memory"); + ret = ES_R_ERROR; + goto out_2; + } + filter = __eventsystem_filter_userevent_for_internal; + } + + object_path = __get_object_path(interface_name); + if (!object_path) { + _E("object_path is NULL"); + ret = ES_R_ERROR; + goto out_3; + } + sender_name = NULL; + + bus_type = G_BUS_TYPE_SYSTEM; + + if (__get_gdbus_shared_connection(&conn, bus_type, interface_name) < 0) { + _E("getting gdbus-connetion error"); + goto out_4; + } + + subscription_id = g_dbus_connection_signal_subscribe(conn, + sender_name, /* sender */ + interface_name, + member_name, /* member */ + object_path, /* object_path */ + NULL, /* arg0 */ + G_DBUS_SIGNAL_FLAGS_NONE, + filter, + user_data, + NULL); /* user_data_free_func */ + + _D("event_name(%s), interface_name(%s)", event_name, interface_name); + _D("member_name(%s), subscription_id(%d), bus_type(%d)", + member_name, subscription_id, bus_type); + + if (subscription_id != 0) { + em = calloc(1, sizeof(eventmap_s)); + if (!em) { + _E("memory alloc failed"); + ret = ES_R_ENOMEM; + } else { + em->interface_name = strdup(interface_name); + em->member_name = strdup(member_name); + em->event_name = strdup(event_name); + em->bus_type = bus_type; + em->reg_id = subscription_id; + em->event_type = evt_type; + + if (!em->interface_name || !em->member_name || !em->event_name) { + _E("out_of_memory"); + FREE_AND_NULL(em->interface_name); + FREE_AND_NULL(em->member_name); + FREE_AND_NULL(em->event_name); + FREE_AND_NULL(em); + ret = ES_R_ENOMEM; + goto out_4; + } + + *em_s = em; + ret = ES_R_OK; + } + } else { + _D("dbus subscribe: error(%d), event(%s)", subscription_id, event_name); + ret = ES_R_ERROR; + } + +out_4: + FREE_AND_NULL(object_path); +out_3: + FREE_AND_NULL(member_name); +out_2: + FREE_AND_NULL(interface_name); +out_1: + if (conn) { + g_object_unref(conn); + } + + return ret; +} + +/** + * function : register the event + */ +int eventsystem_register_event(const char *event_name, unsigned int *reg_id, + eventsystem_handler callback, void *user_data) +{ + eventmap_s *em = NULL; + int ret = ES_R_ERROR; + + retvm_if(!g_dbus_is_interface_name(event_name), ES_R_EINVAL, + "Invalid argument : event_name(%s)", event_name); + retvm_if(!reg_id, ES_R_EINVAL, "Invalid argument : reg_id"); + retvm_if(!callback, ES_R_EINVAL, "Invalid argument : callback"); + + if (!_initialized) { + __initialize(); + } + + ret = __eventsystem_register_event_internal(event_name, &em, user_data); + + if (ret == ES_R_OK && em) { + em->ep_cb = callback; + system_event_list = g_list_append(system_event_list, em); + *reg_id = em->reg_id; + ret = ES_R_OK; + } else { + _E("error, ret(%d), em(%s)", ret, em); + } + + return ret; +} + +/** + * function : unregister the event + */ +int eventsystem_unregister_event(unsigned int reg_id) +{ + eventmap_s em; + eventmap_s *em_data = NULL; + GBusType bus_type; + GList *cb_list = NULL; + GDBusConnection *conn = NULL; + + retvm_if(reg_id == 0, ES_R_EINVAL, "Invalid argument : reg_id"); + + if (!_initialized) { + __initialize(); + } + + em.reg_id = reg_id; + cb_list = g_list_find_custom(system_event_list, &em, + (GCompareFunc)__event_compare_reg_id_cb); + if (cb_list) { + em_data = (eventmap_s *)cb_list->data; + + bus_type = em_data->bus_type; + + _D("unsubscribe: reg_id(%d), bus_type(%d)", reg_id, bus_type); + + if (__get_gdbus_shared_connection(&conn, bus_type, em_data->interface_name) < 0) { + _E("getting gdbus-connetion error"); + return ES_R_ERROR; + } + g_dbus_connection_signal_unsubscribe(conn, reg_id); + + system_event_list = g_list_remove(system_event_list, cb_list->data); + + FREE_AND_NULL(em_data->interface_name); + FREE_AND_NULL(em_data->member_name); + FREE_AND_NULL(em_data->event_name); + FREE_AND_NULL(em_data); + g_object_unref(conn); + } + + return ES_R_OK; +} + +static int eventsystem_send_event(GDBusConnection *conn, eventinfo_s *evti, bundle *data) +{ + GError *error = NULL; + GVariant *param = NULL; + gboolean ret; + + bundle_raw *raw = NULL; + bundle *buf = data; + int len; + + bundle_encode(buf, &raw, &len); + + if (!evti->is_user_event) + evti->is_trusted = FALSE; + + param = g_variant_new("(bus)", evti->is_trusted, len, raw); + ret = g_dbus_connection_emit_signal(conn, + NULL, + evti->object_path, + evti->interface_name, + evti->member_name, + param, + &error); + + _D("interface_name(%s)", evti->interface_name); + _D("object_path(%s)", evti->object_path); + _D("member_name(%s)", evti->member_name); + + bundle_free_encoded_rawdata(&raw); + + if (ret == FALSE) { + _E("Unable to connect to dbus: %s", error->message); + g_error_free(error); + return ES_R_ERROR; + } + + return ES_R_OK; +} + +/** + * function : send the user-event + */ +int eventsystem_send_user_event(const char *event_name, bundle *data, bool is_trusted) +{ + int ret = 0; + + /* check validation */ + retvm_if(!event_name, ES_R_EINVAL, "Invalid argument : event_name is NULL"); + retvm_if(!data, ES_R_EINVAL, "Invalid argument : data is NULL"); + retvm_if(!__check_eventname_validation_user((char *)event_name), ES_R_EINVAL, + "Invalid argument : event_name(%s)", event_name); + + if (!__check_validation_user_defined_name(event_name)) { + _E("Invalid event name(%s)", event_name); + return ES_R_EINVAL; + } + + eventinfo_s *evti = NULL; + evti = calloc(1, sizeof(eventinfo_s)); + if (!evti) { + _E("memory alloc failed"); + return ES_R_ENOMEM; + } + evti->event_name = strdup(event_name); + if (!evti->event_name) { + _E("memory alloc failed"); + ret = ES_R_ENOMEM; + goto out_1; + } + + evti->interface_name = __get_encoded_interface_name(evti->event_name); + if (!evti->interface_name) { + _E("interface_name is NULL"); + ret = ES_R_ERROR; + goto out_2; + } + evti->member_name = strdup(EVENT_SYSTEM_MEMBER); + if (!evti->member_name) { + _E("memory alloc failed"); + ret = ES_R_ENOMEM; + goto out_3; + } + + evti->object_path = __get_object_path(evti->interface_name); + if (!evti->object_path) { + _E("object_path is NULL"); + ret = ES_R_ERROR; + goto out_4; + } + + evti->is_user_event = true; + evti->is_trusted = (gboolean)is_trusted; + + GDBusConnection *conn = NULL; + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SESSION, + evti->interface_name) == ES_R_OK) { + ret = eventsystem_send_event(conn, evti, data); + + if (ret == ES_R_OK) { + __eventsystem_request_event_launch(evti->event_name, data); + } + } else { + _E("getting gdbus-connetion error"); + ret = ES_R_ERROR; + } + + if (conn) { + g_object_unref(conn); + } + FREE_AND_NULL(evti->object_path); +out_4: + FREE_AND_NULL(evti->member_name); +out_3: + FREE_AND_NULL(evti->interface_name); +out_2: + FREE_AND_NULL(evti->event_name); +out_1: + FREE_AND_NULL(evti); + + return ret; +} + +/** + * function : send the system-event + */ +int eventsystem_send_system_event(const char *event_name, bundle *data) +{ + int ret = 0; + + pthread_mutex_lock(&send_sync_lock); + + /* check validation */ + retvm_if(!event_name, ES_R_EINVAL, "Invalid argument : event_name is NULL"); + retvm_if(!data, ES_R_EINVAL, "Invalid argument : data is NULL"); + retvm_if(!__check_eventname_validation_system((char *)event_name), ES_R_EINVAL, + "Invalid argument : event_name(%s)", event_name); + retvm_if(!g_dbus_is_interface_name(event_name), ES_R_EINVAL, + "Invalid argument : event_name(%s)", event_name); + + /* only permitted process could send the system-evnet */ + int pid = getpid(); + int sender_ppid = __get_proc_status_by_pid("PPid:", pid); + int sender_uid = __get_proc_status_by_pid("Uid:", pid); + + _D("event_name(%s), pid(%d), sender_ppid(%d), sender_uid(%d)", + event_name, pid, sender_ppid, sender_uid); + + eventinfo_s *evti = NULL; + evti = calloc(1, sizeof(eventinfo_s)); + if (!evti) { + _E("memory alloc failed"); + pthread_mutex_unlock(&send_sync_lock); + return ES_R_ENOMEM; + } + evti->event_name = strdup(event_name); + if (!evti->event_name) { + _E("out_of_memory"); + ret = ES_R_ENOMEM; + goto out_1; + } + evti->interface_name = strdup(SYS_EVENT_NAME_PREFIX); + if (!evti->interface_name) { + _E("out of memory"); + ret = ES_R_ENOMEM; + goto out_2; + } + evti->member_name = __get_member_name_from_eventname(evti->event_name); + if (!evti->member_name) { + _E("member_name is NULL"); + ret = ES_R_ERROR; + goto out_3; + } + if (!g_dbus_is_member_name(evti->member_name)) { + _E("Invalid member_name(%s)", evti->member_name); + ret = ES_R_EINVAL; + goto out_4; + } + evti->object_path = __get_object_path(evti->interface_name); + if (!evti->object_path) { + _E("object_path is NULL"); + ret = ES_R_ERROR; + goto out_4; + } + evti->is_user_event = false; + evti->is_trusted = FALSE; + + GDBusConnection *conn = NULL; + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, evti->interface_name) == ES_R_OK) { + ret = eventsystem_send_event(conn, evti, data); + } else { + _E("getting gdbus-connection error"); + ret = ES_R_ERROR; + } + + if (conn) { + g_object_unref(conn); + } + FREE_AND_NULL(evti->object_path); +out_4: + FREE_AND_NULL(evti->member_name); +out_3: + FREE_AND_NULL(evti->interface_name); +out_2: + FREE_AND_NULL(evti->event_name); +out_1: + FREE_AND_NULL(evti); + + pthread_mutex_unlock(&send_sync_lock); + + return ret; +} + +/** + * function : request sending the event + */ +int eventsystem_request_sending_system_event(const char *event_name, bundle *data) +{ + int ret = 0; + GDBusConnection *conn = NULL; + GError *error = NULL; + GDBusProxy *proxy = NULL; + GVariant *param = NULL; + GVariant *value = NULL; + gint result = 0; + bundle_raw *raw = NULL; + int len = 0; + + _D("event_name(%s)", event_name); + + if (!_initialized) { + __initialize(); + } + + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, NULL) < 0) { + _E("getting gdbus-connetion error"); + ret = ES_R_ERROR; + goto out_1; + } + + proxy = g_dbus_proxy_new_sync(conn, + G_DBUS_PROXY_FLAGS_NONE, NULL, + ESD_BUS_NAME, ESD_OBJECT_PATH, ESD_INTERFACE_NAME, + NULL, &error); + if (proxy == NULL) { + _E("failed to create new proxy, error(%s)", error->message); + g_error_free(error); + ret = ES_R_ERROR; + goto out_1; + } + + bundle_encode(data, &raw, &len); + + param = g_variant_new("(ssi)", event_name, raw, len); + value = g_dbus_proxy_call_sync(proxy, "RequestSendingEvent", param, + G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (error != NULL) { + _E("proxy call sync error(%s)", error->message); + g_error_free(error); + ret = ES_R_ERROR; + goto out_2; + } + + g_variant_get(value, "(i)", &result); + + _D("result(%d)", result); + + ret = ES_R_OK; + +out_2: + g_variant_unref(value); +out_1: + if (conn) { + g_object_unref(conn); + } + + return ret; +} + +static int __eventsystem_request_event_launch(const char *event_name, bundle *data) +{ + int ret = 0; + GDBusConnection *conn = NULL; + GError *error = NULL; + GDBusProxy *proxy = NULL; + GVariant *param = NULL; + GVariant *value = NULL; + gint result = 0; + bundle_raw *raw = NULL; + int len = 0; + + _D("event_name(%s)", event_name); + + if (!_initialized) { + __initialize(); + } + + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, NULL) < 0) { + _E("getting gdbus-connetion error"); + ret = ES_R_ERROR; + goto out_1; + } + + proxy = g_dbus_proxy_new_sync(conn, + G_DBUS_PROXY_FLAGS_NONE, NULL, + ESD_BUS_NAME, ESD_OBJECT_PATH, ESD_INTERFACE_NAME, + NULL, &error); + if (proxy == NULL) { + _E("failed to create new proxy, error(%s)", error->message); + g_error_free(error); + ret = ES_R_ERROR; + goto out_1; + } + + bundle_encode(data, &raw, &len); + + param = g_variant_new("(ssi)", event_name, raw, len); + value = g_dbus_proxy_call_sync(proxy, "RequestEventLaunch", param, + G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (error != NULL) { + _E("proxy call sync error(%s)", error->message); + g_error_free(error); + ret = ES_R_ERROR; + goto out_2; + } + + g_variant_get(value, "(i)", &result); + + _D("result(%d)", result); + + ret = ES_R_OK; + +out_2: + g_variant_unref(value); +out_1: + if (conn) { + g_object_unref(conn); + } + + return ret; +} + +static int __eventsystem_check_user_certificate(int sender_pid) +{ + int ret = 0; + GDBusConnection *conn = NULL; + GError *error = NULL; + GDBusProxy *proxy = NULL; + GVariant *param = NULL; + GVariant *value = NULL; + gint result = ES_R_ERROR;; + + if (!_initialized) { + __initialize(); + } + + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, NULL) < 0) { + _E("getting gdbus-connetion error"); + ret = ES_R_ERROR; + goto out_1; + } + + proxy = g_dbus_proxy_new_sync(conn, + G_DBUS_PROXY_FLAGS_NONE, NULL, + ESD_BUS_NAME, ESD_OBJECT_PATH, ESD_INTERFACE_NAME, + NULL, &error); + if (proxy == NULL) { + _E("failed to create new proxy, error(%s)", error->message); + g_error_free(error); + ret = ES_R_ERROR; + goto out_1; + } + + param = g_variant_new("(i)", sender_pid); + value = g_dbus_proxy_call_sync(proxy, "CheckUserCertValidation", param, + G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (error != NULL) { + _E("proxy call sync error(%s)", error->message); + g_error_free(error); + ret = ES_R_ERROR; + goto out_2; + } + + g_variant_get(value, "(i)", &result); + + _D("result(%d)", result); + + if (result == 1) { + ret = ES_R_OK; + } +out_2: + g_variant_unref(value); +out_1: + if (conn) { + g_object_unref(conn); + } + + return ret; +} + +static int __eventsystem_check_user_send_validation(const char *event_name) +{ + int ret = 0; + GDBusConnection *conn = NULL; + GError *error = NULL; + GDBusProxy *proxy = NULL; + GVariant *param = NULL; + GVariant *value = NULL; + gint result = ES_R_ERROR;; + + if (!_initialized) { + __initialize(); + } + + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, NULL) < 0) { + _E("getting gdbus-connetion error"); + ret = ES_R_ERROR; + goto out_1; + } + + proxy = g_dbus_proxy_new_sync(conn, + G_DBUS_PROXY_FLAGS_NONE, NULL, + ESD_BUS_NAME, ESD_OBJECT_PATH, ESD_INTERFACE_NAME, + NULL, &error); + if (proxy == NULL) { + _E("failed to create new proxy, error(%s)", error->message); + g_error_free(error); + ret = ES_R_ERROR; + goto out_1; + } + + param = g_variant_new("(s)", event_name); + value = g_dbus_proxy_call_sync(proxy, "CheckUserSendValidation", param, + G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (error != NULL) { + _E("proxy call sync error(%s)", error->message); + g_error_free(error); + ret = ES_R_ERROR; + goto out_2; + } + + g_variant_get(value, "(i)", &result); + + _D("result(%d)", result); + + if (result == 1) { + ret = ES_R_OK; + } +out_2: + g_variant_unref(value); +out_1: + if (conn) { + g_object_unref(conn); + } + + return ret; +} +#ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE +static int __eventsystem_request_earlier_data(const char *event_name, + eventsystem_cb callback, void *user_data) +{ + int ret = 0; + GDBusConnection *conn = NULL; + GError *error = NULL; + GDBusProxy *proxy = NULL; + GVariant *param = NULL; + gint result = 0; + bundle_raw *raw = NULL; + int len = 0; + + if (!_initialized) { + __initialize(); + } + + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, NULL) < 0) { + _E("getting gdbus-connetion error"); + ret = ES_R_ERROR; + goto out_1; + } + + proxy = g_dbus_proxy_new_sync(conn, + G_DBUS_PROXY_FLAGS_NONE, NULL, + ESD_BUS_NAME, ESD_OBJECT_PATH, ESD_INTERFACE_NAME, + NULL, &error); + + if (proxy == NULL) { + _E("failed to create new proxy, error(%s)", error->message); + g_error_free(error); + ret = ES_R_ERROR; + goto out_1; + } + + param = g_variant_new("(s)", event_name); + GVariant *value = g_dbus_proxy_call_sync(proxy, "GetEarlierData", param, + G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (error != NULL) { + _E("proxy call sync error(%s)", error->message); + g_error_free(error); + ret = ES_R_ERROR; + goto out_2; + } + + g_variant_get(value, "(iis)", &result, &len, &raw); + g_variant_unref(value); + + _D("result(%d), len(%d)", result, len); + + if (!result && raw && len > 0) { + callback(event_name, raw, len, user_data); + bundle_free_encoded_rawdata(&raw); + } + + ret = ES_R_OK; + +out_2: + g_variant_unref(value); +out_1: + if (conn) { + g_object_unref(conn); + } + + return ret; +} +#endif + +int eventsystem_register_application_event(const char *event_name, unsigned int *reg_id, + int *event_type, eventsystem_cb callback, void *user_data) +{ + eventmap_s *em; + char *interface_name = NULL; + char *object_path = NULL; + char *member_name = NULL; + char *sender_name = NULL; + GDBusSignalCallback filter; + GBusType bus_type; + guint subscription_id = 0; + int ret = 0; + GDBusConnection *conn = NULL; + + if (!_initialized) { + __initialize(); + } + + if (__check_eventname_validation_system((char *)event_name)) { + *event_type = ES_TYPE_SYSTEM; + } else if (__check_eventname_validation_user((char *)event_name)) { + *event_type = ES_TYPE_USER; + } else { + *event_type = ES_TYPE_UNKNOWN; + _E("unknown type event(%s)", event_name); + return ES_R_EINVAL; + } + + if (*event_type == ES_TYPE_SYSTEM) { + interface_name = strdup(SYS_EVENT_NAME_PREFIX); + if (interface_name == NULL) { + _E("out of memory"); + return ES_R_ENOMEM; + } + if (!g_dbus_is_interface_name(interface_name)) { + _E("invalid interface_name(%s)", interface_name); + FREE_AND_NULL(interface_name); + return ES_R_EINVAL; + } + member_name = __get_member_name_from_eventname((char *)event_name); + if (member_name == NULL) { + _E("member_name is NULL(%s)", event_name); + FREE_AND_NULL(interface_name); + return ES_R_ERROR; + } + if (!g_dbus_is_member_name(member_name)) { + _E("Invalid member_name(%s)", member_name); + FREE_AND_NULL(interface_name); + FREE_AND_NULL(member_name); + return ES_R_ERROR; + } + filter = __eventsystem_filter_sysevent_for_application; + bus_type = G_BUS_TYPE_SYSTEM; + } else { + interface_name = __get_encoded_interface_name((char *)event_name); + if (!interface_name) { + _E("interface_name is NULL"); + return ES_R_ERROR; + } + if (!g_dbus_is_interface_name(interface_name)) { + _E("invalid interface_name(%s)", interface_name); + FREE_AND_NULL(interface_name); + return ES_R_EINVAL; + } + member_name = strdup(EVENT_SYSTEM_MEMBER); + if (!member_name) { + _E("out_of_memory"); + FREE_AND_NULL(interface_name); + return ES_R_ERROR; + } + filter = __eventsystem_filter_userevent_for_application; + bus_type = G_BUS_TYPE_SESSION; + } + + object_path = __get_object_path(interface_name); + if (!object_path) { + _E("failed get object_path"); + FREE_AND_NULL(interface_name); + FREE_AND_NULL(member_name); + return ES_R_ERROR; + } + sender_name = NULL; + + _D("interface_name(%s), object_path(%s)", interface_name, object_path); + _D(" member_name(%s), sender_name(%s), type(%d), bus_type(%d)", + member_name, sender_name, *event_type, bus_type); + + if (__get_gdbus_shared_connection(&conn, bus_type, interface_name) < 0) { + _E("getting gdbus-connetion error"); + FREE_AND_NULL(interface_name); + FREE_AND_NULL(object_path); + FREE_AND_NULL(member_name); + return ES_R_ERROR; + } + +#ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE + __eventsystem_request_earlier_data(event_name, callback, user_data); +#endif + + subscription_id = g_dbus_connection_signal_subscribe(conn, + sender_name, /* sender */ + interface_name, + member_name, /* member */ + object_path, /* object_path */ + NULL, /* arg0 */ + G_DBUS_SIGNAL_FLAGS_NONE, + filter, + user_data, + NULL); /* user_data_free_func */ + + _D("event_name(%s), subscription_id(%d)", event_name, subscription_id); + + if (subscription_id != 0) { + em = calloc(1, sizeof(eventmap_s)); + if (!em) { + _E("memory alloc failed"); + ret = ES_R_ENOMEM; + } else { + em->interface_name = strdup(interface_name); + em->member_name = strdup(member_name); + em->event_name = strdup(event_name); + em->es_cb = callback; + em->bus_type = bus_type; + em->reg_id = subscription_id; + em->event_type = *event_type; + + if (!em->interface_name || !em->member_name || + !em->event_name) { + _E("out_of_memory"); + FREE_AND_NULL(em->interface_name); + FREE_AND_NULL(em->member_name); + FREE_AND_NULL(em->event_name); + FREE_AND_NULL(em); + ret = ES_R_ENOMEM; + } else { + system_event_list = + g_list_append(system_event_list, em); + *reg_id = subscription_id; + ret = ES_R_OK; + } + } + } else { + _D("dbus subscribe: error(%d)", subscription_id); + ret = ES_R_ERROR; + } + + FREE_AND_NULL(interface_name); + FREE_AND_NULL(object_path); + FREE_AND_NULL(member_name); + if (conn) { + g_object_unref(conn); + } + + return ret; +} + +int eventsystem_unregister_application_event(unsigned int reg_id) +{ + eventmap_s em; + eventmap_s *em_data = NULL; + GBusType bus_type; + GList *cb_list = NULL; + GDBusConnection *conn = NULL; + + retvm_if(reg_id == 0, ES_R_EINVAL, "Invalid argument : reg_id"); + + if (!_initialized) { + __initialize(); + } + + em.reg_id = reg_id; + cb_list = g_list_find_custom(system_event_list, &em, + (GCompareFunc)__event_compare_reg_id_cb); + if (cb_list) { + em_data = (eventmap_s *)cb_list->data; + + bus_type = em_data->bus_type; + + _D("unsubscribe: reg_id(%d), bus_type(%d)", reg_id, bus_type); + + if (__get_gdbus_shared_connection(&conn, bus_type, em_data->interface_name) < 0) { + _E("getting gdbus-connetion error"); + return ES_R_ERROR; + } + g_dbus_connection_signal_unsubscribe(conn, reg_id); + + system_event_list = g_list_remove(system_event_list, cb_list->data); + + FREE_AND_NULL(em_data->interface_name); + FREE_AND_NULL(em_data->member_name); + FREE_AND_NULL(em_data->event_name); + FREE_AND_NULL(em_data); + g_object_unref(conn); + } else { + _E("not found matched item"); + return ES_R_ERROR; + } + + return ES_R_OK; +} + +int eventsystem_application_finalize(void) +{ + gpointer key, value; + + _D("release all resouces"); + + if (system_event_list) { + g_list_free(system_event_list); + } + + if (filter_tbl) { + GHashTableIter iter; + + g_hash_table_iter_init(&iter, filter_tbl); + + while (g_hash_table_iter_next(&iter, &key, &value)) { + char *val_item = (char *)value; + if (val_item) { + free(val_item); + } else { + _E("filter_tbl, val_item is NULL"); + } + g_hash_table_iter_remove(&iter); + } + g_hash_table_unref(filter_tbl); + } + + if (check_tbl) { + GHashTableIter iter; + + g_hash_table_iter_init(&iter, check_tbl); + + while (g_hash_table_iter_next(&iter, &key, &value)) { + char *val_item = (char *)value; + if (val_item) { + free(val_item); + } else { + _E("check_tbl, val_item is NULL"); + } + g_hash_table_iter_remove(&iter); + } + g_hash_table_unref(check_tbl); + } + + return 0; +} -- 2.7.4 From 3ccb7eba69afc75df135f428efdc6b0ec2dd92ce Mon Sep 17 00:00:00 2001 From: Jiwoong Im Date: Fri, 10 Jul 2015 19:14:32 +0900 Subject: [PATCH 3/6] Sync with tizen 2.4 libeventsystem source. Change-Id: I5eb98aadf46f5a1f3c0fb4f0b3d59c85e6a3b897 Signed-off-by: Jiwoong Im --- CMakeLists.txt | 2 +- packaging/eventsystem.spec | 1 + src/eventsystem.c | 536 +++++++++++++++++++++++++++------------------ 3 files changed, 330 insertions(+), 209 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d0280e..08cad00 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,7 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src) ### Required packages INCLUDE(FindPkgConfig) -pkg_check_modules(libpkgs REQUIRED dlog bundle openssl glib-2.0) +pkg_check_modules(libpkgs REQUIRED dlog bundle openssl glib-2.0 capi-base-common) #FIND_LIBRARY(LIB_DL dl) diff --git a/packaging/eventsystem.spec b/packaging/eventsystem.spec index 0f09994..9b88f3d 100755 --- a/packaging/eventsystem.spec +++ b/packaging/eventsystem.spec @@ -12,6 +12,7 @@ BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(gio-2.0) BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(openssl) +BuildRequires: pkgconfig(capi-base-common) Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig diff --git a/src/eventsystem.c b/src/eventsystem.c index 8e0491d..2b02d34 100755 --- a/src/eventsystem.c +++ b/src/eventsystem.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -43,11 +44,18 @@ } \ } while (0) +#define tryvm_if(expr, val, fmt, arg...) do { \ + if (expr) { \ + _E("(%s) "fmt, #expr, ##arg); \ + val; \ + goto catch; \ + } \ +} while (0) + pthread_mutex_t send_sync_lock = PTHREAD_MUTEX_INITIALIZER; static GList *system_event_list; static int _initialized; -static GHashTable *filter_tbl; static GHashTable *check_tbl; typedef struct eventmap { @@ -65,6 +73,7 @@ typedef struct eventmap { typedef struct eventinfo { char *event_name; + char *destination_name; char *interface_name; char *object_path; char *member_name; @@ -76,14 +85,15 @@ typedef struct eventinfo { typedef struct sysevent_info { guint owner_id; guint owner_id_session; + char *own_name_system_bus; + char *own_name_session_bus; } sysevent_info_s; static sysevent_info_s s_info; -static int __eventsystem_request_event_launch(const char *event_name, bundle *data); -static bool __eventsystem_check_sender_validation_userevent(GDBusConnection *connection, - char *sender_name); +static int __eventsystem_request_event_launch(const char *method_name, + const char *event_name, bundle *data); static int __eventsystem_check_user_send_validation(const char *event_name); -static int __eventsystem_check_user_certificate(int sender_pid); +static int __eventsystem_requet_destination_list(const char *event_name, GList **dest_list); static int __event_compare_name_cb(gconstpointer a, gconstpointer b) { @@ -102,7 +112,10 @@ static int __event_compare_reg_id_cb(gconstpointer a, gconstpointer b) static void __initialize(void) { +#if (GLIB_MAJOR_VERSION <= 2 && GLIB_MINOR_VERSION < 36) g_type_init(); +#endif + _initialized = 1; } @@ -180,6 +193,7 @@ static char *__get_member_name_from_eventname(char *event_name) ptr = strtok(temp_name, "."); if (ptr == NULL) { _E("invalid event_name(%s), count(%d)", event_name, count); + FREE_AND_NULL(temp_name); return NULL; } count++; @@ -270,62 +284,8 @@ static int __check_eventname_validation_system(char *event_name) return ret; } -static int __get_proc_status_by_pid(const char *what, int pid) -{ - int fd = 0; - int ret = 0; - int i = 0; - char ch = 0; - char _path[128] = {0, }; - char _buf[1024] = {0, }; - char *pval; - char *ptr; - - snprintf(_path, 128, "/proc/%d/status", pid); - - fd = open(_path, O_RDONLY); - if (fd < 0) { - _E("open file(%s) error(%d), pid(%d)", _path, fd, pid); - return fd; - } - - ret = read(fd, _buf, 1024); - if (ret < 0) { - _E("read file(%s) error(%d), pid(%d)", _path, ret, pid); - goto error; - } - - pval = strstr(_buf, what); - if (pval == NULL) { - _E("pval is NULL"); - ret = -1; - goto error; - } - - for (i = 0; i < strlen(pval); i++) { - ch = *pval; - if (ch == ' ' || ch == '\t') { - ret = (int)strtol(pval, &ptr, 10); - if (ret == 0) { - if ((int)strlen(ptr) >= (int)strlen(pval)) { - _E("wrong 0 value"); - ret = -2; - } - } - break; - } else if (ch == '\n') - break; - pval++; - } - -error: - close(fd); - - return ret; -} - -static int __get_gdbus_shared_connection(GDBusConnection **connection, - GBusType bus_type, const char *interface_name) +static int __get_gdbus_shared_connection(GDBusConnection **connection, GBusType bus_type, + eventsystem_event_type event_type) { GError *error = NULL; guint owner_id = 0; @@ -344,13 +304,22 @@ static int __get_gdbus_shared_connection(GDBusConnection **connection, return ES_R_ERROR; } - if (interface_name && - ((bus_type == G_BUS_TYPE_SYSTEM && !s_info.owner_id) || + if (((bus_type == G_BUS_TYPE_SYSTEM && !s_info.owner_id) || (bus_type == G_BUS_TYPE_SESSION && !s_info.owner_id_session))) { + gchar *guid = NULL; char own_name[128] = {0, }; - snprintf(own_name, 128, "%s_%d_%d", interface_name, - bus_type, getpid()); - _D("own_name is [%s]", own_name); + + guid = g_dbus_generate_guid(); + if (guid == NULL) { + _E("failed to get guid"); + return ES_R_ERROR; + } + snprintf(own_name, 128, "%s_%s_%s_%d", "event.busname", + (bus_type == G_BUS_TYPE_SESSION ? "session" : "system"), + guid, getpid()); + g_free(guid); + + _D("bus_name is [%s]", own_name); owner_id = g_bus_own_name_on_connection(*connection, own_name, G_BUS_NAME_OWNER_FLAGS_NONE, NULL, NULL, NULL, NULL); @@ -358,55 +327,54 @@ static int __get_gdbus_shared_connection(GDBusConnection **connection, _E("g_bus_own_name_on_connection, error"); return ES_R_ERROR; } + + if (bus_type == G_BUS_TYPE_SESSION && event_type == ES_TYPE_USER) { + /* set same name on system-bus */ + GDBusConnection *conn_system = NULL; + + error = NULL; + conn_system = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); + if (conn_system == NULL) { + _E("failed to get system connection"); + if (error != NULL) { + _E("error(%s)", error->message); + g_error_free(error); + } + return ES_R_ERROR; + } else { + owner_id = g_bus_own_name_on_connection(conn_system, + own_name, G_BUS_NAME_OWNER_FLAGS_NONE, + NULL, NULL, NULL, NULL); + if (!owner_id) { + _E("g_bus_own_name_on_connection(for system), error"); + g_object_unref(conn_system); + return ES_R_ERROR; + } + + g_object_unref(conn_system); + } + } + if (bus_type == G_BUS_TYPE_SESSION) { s_info.owner_id_session = owner_id; + s_info.own_name_session_bus = strdup(own_name); + if (s_info.own_name_session_bus == NULL) { + _E("out of memory"); + return ES_R_ERROR; + } } else { s_info.owner_id = owner_id; + s_info.own_name_system_bus = strdup(own_name); + if (s_info.own_name_system_bus == NULL) { + _E("out of memory"); + return ES_R_ERROR; + } } } return ES_R_OK; } -static int __get_sender_pid(GDBusConnection *conn, const char *sender_name) -{ - GDBusMessage *msg = NULL; - GDBusMessage *reply = NULL; - GError *err = NULL; - GVariant *body; - int pid = 0; - - msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus", - "org.freedesktop.DBus", "GetConnectionUnixProcessID"); - if (!msg) { - _D("Can't allocate new method call"); - goto out; - } - - g_dbus_message_set_body(msg, g_variant_new ("(s)", sender_name)); - reply = g_dbus_connection_send_message_with_reply_sync(conn, msg, - G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err); - - if (!reply) { - if (err != NULL) { - _E("Failed to get pid [%s]", err->message); - g_error_free(err); - } - goto out; - } - - body = g_dbus_message_get_body(reply); - g_variant_get(body, "(u)", &pid); - -out: - if (msg) - g_object_unref(msg); - if (reply) - g_object_unref(reply); - - return pid; -} - static void __eventsystem_event_handler(GDBusConnection *connection, const gchar *sender_name, const gchar *object_path, const gchar *interface_name, const gchar *signal_name, @@ -479,45 +447,6 @@ static void __eventsystem_application_event_handler(GDBusConnection *connection, bundle_free_encoded_rawdata(&raw); } -static bool __eventsystem_check_sender_validation_userevent(GDBusConnection *connection, - char *sender_name) -{ - char *sender_id = NULL; - char *key = NULL; - int sender_pid = 0; - - if (filter_tbl == NULL) { - filter_tbl = g_hash_table_new(g_str_hash, g_str_equal); - } - - sender_id = (char *)g_hash_table_lookup(filter_tbl, sender_name); - - if (sender_id == NULL) { - sender_pid = __get_sender_pid(connection, sender_name); - - if (sender_pid > 0) { - if (__eventsystem_check_user_certificate(sender_pid) < 0) { - _E("not match"); - return false; - } - - key = strdup(sender_name); - if (key == NULL) { - _E("out_of_memory"); - return false; - } - g_hash_table_insert(filter_tbl, key, key); - } else { - _E("failed to get sender_pid"); - return false; - } - } else { - _D("sender_id(%s)", sender_id); - } - - return true; -} - /** * application-use filter for user-event */ @@ -532,13 +461,6 @@ static void __eventsystem_filter_userevent_for_application(GDBusConnection *conn g_variant_get(parameters, "(bus)", &is_trusted, NULL, NULL); - /* check signature */ - if (is_trusted && - __eventsystem_check_sender_validation_userevent(connection, - (char *)sender_name) == false) { - return; - } - __eventsystem_application_event_handler(connection, sender_name, object_path, interface_name, signal_name, parameters, user_data); } @@ -664,7 +586,7 @@ static int __eventsystem_register_event_internal(const char *event_name, bus_type = G_BUS_TYPE_SYSTEM; - if (__get_gdbus_shared_connection(&conn, bus_type, interface_name) < 0) { + if (__get_gdbus_shared_connection(&conn, bus_type, evt_type) < 0) { _E("getting gdbus-connetion error"); goto out_4; } @@ -771,6 +693,7 @@ int eventsystem_unregister_event(unsigned int reg_id) GBusType bus_type; GList *cb_list = NULL; GDBusConnection *conn = NULL; + eventsystem_event_type evt_type; retvm_if(reg_id == 0, ES_R_EINVAL, "Invalid argument : reg_id"); @@ -785,10 +708,11 @@ int eventsystem_unregister_event(unsigned int reg_id) em_data = (eventmap_s *)cb_list->data; bus_type = em_data->bus_type; + evt_type = em_data->event_type; _D("unsubscribe: reg_id(%d), bus_type(%d)", reg_id, bus_type); - if (__get_gdbus_shared_connection(&conn, bus_type, em_data->interface_name) < 0) { + if (__get_gdbus_shared_connection(&conn, bus_type, evt_type) < 0) { _E("getting gdbus-connetion error"); return ES_R_ERROR; } @@ -823,7 +747,7 @@ static int eventsystem_send_event(GDBusConnection *conn, eventinfo_s *evti, bund param = g_variant_new("(bus)", evti->is_trusted, len, raw); ret = g_dbus_connection_emit_signal(conn, - NULL, + evti->destination_name, evti->object_path, evti->interface_name, evti->member_name, @@ -845,6 +769,51 @@ static int eventsystem_send_event(GDBusConnection *conn, eventinfo_s *evti, bund return ES_R_OK; } +static void __eventsystem_free_trusted_list(gpointer data) +{ + char *name = (char *)data; + + FREE_AND_NULL(name); +} + +static int __eventsystem_send_trusted_event(GDBusConnection *conn, eventinfo_s *evti, + bundle *data, GList *dest_list) +{ + GList *tmp_list = NULL; + int ret = 0; + int real_sent = 0; + + if (dest_list) { + tmp_list = g_list_first(dest_list); + + while (tmp_list != NULL) { + char *dest_name = (char *)tmp_list->data; + if (dest_name && dest_name[0] != '\0') { + _D("dest_name(%s)", dest_name); + evti->destination_name = dest_name; + ret = eventsystem_send_event(conn, evti, data); + if (ret != ES_R_OK) { + _E("send error"); + ret = ES_R_ERROR; + break; + } + real_sent = 1; + } + tmp_list = g_list_next(tmp_list); + } + g_list_free_full(dest_list, __eventsystem_free_trusted_list); + + if (real_sent) { + __eventsystem_request_event_launch("RequestTrustedEventLaunch", + evti->event_name, data); + } + } else { + _E("dest_list is null"); + } + + return ES_R_OK; +} + /** * function : send the user-event */ @@ -896,16 +865,28 @@ int eventsystem_send_user_event(const char *event_name, bundle *data, bool is_tr goto out_4; } + evti->destination_name = NULL; evti->is_user_event = true; evti->is_trusted = (gboolean)is_trusted; GDBusConnection *conn = NULL; - if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SESSION, - evti->interface_name) == ES_R_OK) { - ret = eventsystem_send_event(conn, evti, data); - - if (ret == ES_R_OK) { - __eventsystem_request_event_launch(evti->event_name, data); + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SESSION, ES_TYPE_USER) == ES_R_OK) { + if (evti->is_trusted) { + GList *dest_list = NULL; + ret = __eventsystem_requet_destination_list(evti->event_name, &dest_list); + if (ret < 0) { + _E("failed to get dest list"); + ret = ES_R_ERROR; + } else { + ret = __eventsystem_send_trusted_event(conn, evti, + data, dest_list); + } + } else { + ret = eventsystem_send_event(conn, evti, data); + if (ret == ES_R_OK) { + __eventsystem_request_event_launch("RequestEventLaunch", + evti->event_name, data); + } } } else { _E("getting gdbus-connetion error"); @@ -938,20 +919,14 @@ int eventsystem_send_system_event(const char *event_name, bundle *data) pthread_mutex_lock(&send_sync_lock); /* check validation */ - retvm_if(!event_name, ES_R_EINVAL, "Invalid argument : event_name is NULL"); - retvm_if(!data, ES_R_EINVAL, "Invalid argument : data is NULL"); - retvm_if(!__check_eventname_validation_system((char *)event_name), ES_R_EINVAL, + tryvm_if(!event_name, ret = ES_R_EINVAL, "Invalid argument : event_name is NULL"); + tryvm_if(!data, ret = ES_R_EINVAL, "Invalid argument : data is NULL"); + tryvm_if(!__check_eventname_validation_system((char *)event_name), ret = ES_R_EINVAL, "Invalid argument : event_name(%s)", event_name); - retvm_if(!g_dbus_is_interface_name(event_name), ES_R_EINVAL, + tryvm_if(!g_dbus_is_interface_name(event_name), ret = ES_R_EINVAL, "Invalid argument : event_name(%s)", event_name); - /* only permitted process could send the system-evnet */ - int pid = getpid(); - int sender_ppid = __get_proc_status_by_pid("PPid:", pid); - int sender_uid = __get_proc_status_by_pid("Uid:", pid); - - _D("event_name(%s), pid(%d), sender_ppid(%d), sender_uid(%d)", - event_name, pid, sender_ppid, sender_uid); + _D("event_name(%s)", event_name); eventinfo_s *evti = NULL; evti = calloc(1, sizeof(eventinfo_s)); @@ -989,11 +964,12 @@ int eventsystem_send_system_event(const char *event_name, bundle *data) ret = ES_R_ERROR; goto out_4; } + evti->destination_name = NULL; evti->is_user_event = false; evti->is_trusted = FALSE; GDBusConnection *conn = NULL; - if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, evti->interface_name) == ES_R_OK) { + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) == ES_R_OK) { ret = eventsystem_send_event(conn, evti, data); } else { _E("getting gdbus-connection error"); @@ -1013,6 +989,7 @@ out_2: out_1: FREE_AND_NULL(evti); +catch: pthread_mutex_unlock(&send_sync_lock); return ret; @@ -1039,7 +1016,7 @@ int eventsystem_request_sending_system_event(const char *event_name, bundle *dat __initialize(); } - if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, NULL) < 0) { + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) < 0) { _E("getting gdbus-connetion error"); ret = ES_R_ERROR; goto out_1; @@ -1084,7 +1061,8 @@ out_1: return ret; } -static int __eventsystem_request_event_launch(const char *event_name, bundle *data) +static int __eventsystem_request_event_launch(const char *method_name, + const char *event_name, bundle *data) { int ret = 0; GDBusConnection *conn = NULL; @@ -1102,7 +1080,7 @@ static int __eventsystem_request_event_launch(const char *event_name, bundle *da __initialize(); } - if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, NULL) < 0) { + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) < 0) { _E("getting gdbus-connetion error"); ret = ES_R_ERROR; goto out_1; @@ -1122,7 +1100,7 @@ static int __eventsystem_request_event_launch(const char *event_name, bundle *da bundle_encode(data, &raw, &len); param = g_variant_new("(ssi)", event_name, raw, len); - value = g_dbus_proxy_call_sync(proxy, "RequestEventLaunch", param, + value = g_dbus_proxy_call_sync(proxy, method_name, param, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); if (error != NULL) { _E("proxy call sync error(%s)", error->message); @@ -1147,21 +1125,21 @@ out_1: return ret; } -static int __eventsystem_check_user_certificate(int sender_pid) +static int __eventsystem_check_user_send_validation(const char *event_name) { - int ret = 0; + int ret = ES_R_EINVAL; GDBusConnection *conn = NULL; GError *error = NULL; GDBusProxy *proxy = NULL; GVariant *param = NULL; GVariant *value = NULL; - gint result = ES_R_ERROR;; + gint result = 0; if (!_initialized) { __initialize(); } - if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, NULL) < 0) { + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) < 0) { _E("getting gdbus-connetion error"); ret = ES_R_ERROR; goto out_1; @@ -1178,8 +1156,8 @@ static int __eventsystem_check_user_certificate(int sender_pid) goto out_1; } - param = g_variant_new("(i)", sender_pid); - value = g_dbus_proxy_call_sync(proxy, "CheckUserCertValidation", param, + param = g_variant_new("(s)", event_name); + value = g_dbus_proxy_call_sync(proxy, "CheckUserSendValidation", param, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); if (error != NULL) { _E("proxy call sync error(%s)", error->message); @@ -1205,21 +1183,21 @@ out_1: return ret; } -static int __eventsystem_check_user_send_validation(const char *event_name) +static int __eventsystem_check_privilege_validation(const char *event_name) { - int ret = 0; + int ret = ES_R_ENOTPERMITTED; GDBusConnection *conn = NULL; GError *error = NULL; GDBusProxy *proxy = NULL; GVariant *param = NULL; GVariant *value = NULL; - gint result = ES_R_ERROR;; + gint result = 0; if (!_initialized) { __initialize(); } - if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, NULL) < 0) { + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) < 0) { _E("getting gdbus-connetion error"); ret = ES_R_ERROR; goto out_1; @@ -1237,7 +1215,66 @@ static int __eventsystem_check_user_send_validation(const char *event_name) } param = g_variant_new("(s)", event_name); - value = g_dbus_proxy_call_sync(proxy, "CheckUserSendValidation", param, + value = g_dbus_proxy_call_sync(proxy, "CheckPrivilegeValidation", param, + G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (error != NULL) { + _E("proxy call sync error(%s)", error->message); + g_error_free(error); + ret = ES_R_ERROR; + goto out_2; + } + + g_variant_get(value, "(i)", &result); + + _D("result(%d)", result); + + if (result == 1) { + ret = ES_R_OK; + } +out_2: + g_variant_unref(value); +out_1: + if (conn) { + g_object_unref(conn); + } + + return ret; +} + +static int __eventsystem_setup_trusted_peer(const char *event_name, const char *dest_bus_name) +{ + int ret = ES_R_ERROR; + GDBusConnection *conn = NULL; + GError *error = NULL; + GDBusProxy *proxy = NULL; + GVariant *param = NULL; + GVariant *value = NULL; + gint result = 0; + + if (!_initialized) { + __initialize(); + } + + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) < 0) { + _E("getting gdbus-connetion error"); + ret = ES_R_ERROR; + goto out_1; + } + + proxy = g_dbus_proxy_new_sync(conn, + G_DBUS_PROXY_FLAGS_NONE, NULL, + ESD_BUS_NAME, ESD_OBJECT_PATH, ESD_INTERFACE_NAME, + NULL, &error); + if (proxy == NULL) { + _E("failed to create new proxy, error(%s)", error->message); + g_error_free(error); + ret = ES_R_ERROR; + goto out_1; + } + + param = g_variant_new("(ss)", event_name, dest_bus_name); + + value = g_dbus_proxy_call_sync(proxy, "SetupTrustedPeer", param, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); if (error != NULL) { _E("proxy call sync error(%s)", error->message); @@ -1262,6 +1299,82 @@ out_1: return ret; } + +static int __eventsystem_requet_destination_list(const char *event_name, GList **dest_list) +{ + int ret = ES_R_ERROR; + GDBusConnection *conn = NULL; + GError *error = NULL; + GDBusProxy *proxy = NULL; + GVariant *param = NULL; + GVariant *value = NULL; + GVariantIter *iter; + gchar *str; + char *dest_name = NULL; + gint result = 0; + + if (!_initialized) { + __initialize(); + } + + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) < 0) { + _E("getting gdbus-connetion error"); + ret = ES_R_ERROR; + goto out_1; + } + + proxy = g_dbus_proxy_new_sync(conn, + G_DBUS_PROXY_FLAGS_NONE, NULL, + ESD_BUS_NAME, ESD_OBJECT_PATH, ESD_INTERFACE_NAME, + NULL, &error); + if (proxy == NULL) { + _E("failed to create new proxy, error(%s)", error->message); + g_error_free(error); + ret = ES_R_ERROR; + goto out_1; + } + + param = g_variant_new("(s)", event_name); + + value = g_dbus_proxy_call_sync(proxy, "GetTrustedPeerList", param, + G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); + if (error != NULL) { + _E("proxy call sync error(%s)", error->message); + g_error_free(error); + ret = ES_R_ERROR; + goto out_2; + } + + g_variant_get(value, "(ias)", &result, &iter); + + _D("result(%d)", result); + + if (result == 1) { + ret = ES_R_OK; + while (g_variant_iter_loop(iter, "s", &str)) { + dest_name = strdup((char *)str); + if (dest_name) { + _D("dest name(%s)", str); + *dest_list = g_list_append(*dest_list, dest_name); + } else { + _E("out of memory"); + ret = ES_R_ENOMEM; + break; + } + } + } + g_variant_iter_free(iter); + +out_2: + g_variant_unref(value); +out_1: + if (conn) { + g_object_unref(conn); + } + + return ret; +} + #ifdef APPFW_EVENT_SYSTEM_EARLIER_FEATURE static int __eventsystem_request_earlier_data(const char *event_name, eventsystem_cb callback, void *user_data) @@ -1279,7 +1392,7 @@ static int __eventsystem_request_earlier_data(const char *event_name, __initialize(); } - if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, NULL) < 0) { + if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) < 0) { _E("getting gdbus-connetion error"); ret = ES_R_ERROR; goto out_1; @@ -1349,6 +1462,10 @@ int eventsystem_register_application_event(const char *event_name, unsigned int } if (__check_eventname_validation_system((char *)event_name)) { + if (__eventsystem_check_privilege_validation(event_name) != ES_R_OK) { + _E("invalid privilege(%s)", event_name); + return ES_R_ENOTPERMITTED; + } *event_type = ES_TYPE_SYSTEM; } else if (__check_eventname_validation_user((char *)event_name)) { *event_type = ES_TYPE_USER; @@ -1417,7 +1534,7 @@ int eventsystem_register_application_event(const char *event_name, unsigned int _D(" member_name(%s), sender_name(%s), type(%d), bus_type(%d)", member_name, sender_name, *event_type, bus_type); - if (__get_gdbus_shared_connection(&conn, bus_type, interface_name) < 0) { + if (__get_gdbus_shared_connection(&conn, bus_type, *event_type) < 0) { _E("getting gdbus-connetion error"); FREE_AND_NULL(interface_name); FREE_AND_NULL(object_path); @@ -1468,11 +1585,26 @@ int eventsystem_register_application_event(const char *event_name, unsigned int system_event_list = g_list_append(system_event_list, em); *reg_id = subscription_id; + ret = ES_R_OK; + + if (em->bus_type == G_BUS_TYPE_SESSION && + em->event_type == ES_TYPE_USER) { + if (s_info.own_name_session_bus == NULL) { + _E("session bus is not ready"); + ret = ES_R_ERROR; + } else { + if (__eventsystem_setup_trusted_peer(event_name, + s_info.own_name_session_bus) < 0) { + _E("failed to setup trusted peer"); + ret = ES_R_ERROR; + } + } + } } } } else { - _D("dbus subscribe: error(%d)", subscription_id); + _E("dbus subscribe: error(%d)", subscription_id); ret = ES_R_ERROR; } @@ -1493,6 +1625,7 @@ int eventsystem_unregister_application_event(unsigned int reg_id) GBusType bus_type; GList *cb_list = NULL; GDBusConnection *conn = NULL; + eventsystem_event_type evt_type; retvm_if(reg_id == 0, ES_R_EINVAL, "Invalid argument : reg_id"); @@ -1507,10 +1640,11 @@ int eventsystem_unregister_application_event(unsigned int reg_id) em_data = (eventmap_s *)cb_list->data; bus_type = em_data->bus_type; + evt_type = em_data->event_type; _D("unsubscribe: reg_id(%d), bus_type(%d)", reg_id, bus_type); - if (__get_gdbus_shared_connection(&conn, bus_type, em_data->interface_name) < 0) { + if (__get_gdbus_shared_connection(&conn, bus_type, evt_type) < 0) { _E("getting gdbus-connetion error"); return ES_R_ERROR; } @@ -1541,23 +1675,6 @@ int eventsystem_application_finalize(void) g_list_free(system_event_list); } - if (filter_tbl) { - GHashTableIter iter; - - g_hash_table_iter_init(&iter, filter_tbl); - - while (g_hash_table_iter_next(&iter, &key, &value)) { - char *val_item = (char *)value; - if (val_item) { - free(val_item); - } else { - _E("filter_tbl, val_item is NULL"); - } - g_hash_table_iter_remove(&iter); - } - g_hash_table_unref(filter_tbl); - } - if (check_tbl) { GHashTableIter iter; @@ -1575,5 +1692,8 @@ int eventsystem_application_finalize(void) g_hash_table_unref(check_tbl); } + FREE_AND_NULL(s_info.own_name_system_bus); + FREE_AND_NULL(s_info.own_name_session_bus); + return 0; } -- 2.7.4 From ffc64b62183c35275d51eecc9cebfbde1f053e9e Mon Sep 17 00:00:00 2001 From: jongmyeongko Date: Tue, 10 Nov 2015 17:56:42 +0900 Subject: [PATCH 4/6] merge tizen 2.4 Change-Id: I6b09e6aabaa3f09f12e0ed309fdb5b8e4bf6f0c7 Signed-off-by: jongmyeongko --- CMakeLists.txt | 0 include/eventsystem.h | 0 include/eventsystem_internal.h | 1 + packaging/eventsystem.spec | 3 +- src/eventsystem.c | 261 ++++++++++++++++++++++++++++------------- 5 files changed, 182 insertions(+), 83 deletions(-) mode change 100755 => 100644 CMakeLists.txt mode change 100755 => 100644 include/eventsystem.h mode change 100755 => 100644 packaging/eventsystem.spec mode change 100755 => 100644 src/eventsystem.c diff --git a/CMakeLists.txt b/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/include/eventsystem.h b/include/eventsystem.h old mode 100755 new mode 100644 diff --git a/include/eventsystem_internal.h b/include/eventsystem_internal.h index 358882b..b70a24a 100644 --- a/include/eventsystem_internal.h +++ b/include/eventsystem_internal.h @@ -47,6 +47,7 @@ extern "C" { #define EVT_VAL_BATTERY_CHARGER_DISCONNECTED "disconnected" #define EVT_VAL_BATTERY_CHARGER_CONNECTED "connected" #define EVT_VAL_BATTERY_CHARGER_CHARGING "charging" +#define EVT_VAL_BATTERY_CHARGER_DISCHARGING "discharging" /** battery : level status */ #define SYS_EVENT_BATTERY_LEVEL_STATUS "tizen.system.event.battery_level_status" diff --git a/packaging/eventsystem.spec b/packaging/eventsystem.spec old mode 100755 new mode 100644 index 9b88f3d..f23d124 --- a/packaging/eventsystem.spec +++ b/packaging/eventsystem.spec @@ -2,7 +2,7 @@ Name: eventsystem Summary: Event system library Version: 0.0.1 Release: 1 -Group: System/Libraries +Group: Application Framework/Libraries License: Apache-2.0 Source0: %{name}-%{version}.tar.gz BuildRequires: cmake @@ -11,7 +11,6 @@ BuildRequires: pkgconfig(bundle) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(gio-2.0) BuildRequires: pkgconfig(glib-2.0) -BuildRequires: pkgconfig(openssl) BuildRequires: pkgconfig(capi-base-common) Requires(post): /sbin/ldconfig diff --git a/src/eventsystem.c b/src/eventsystem.c old mode 100755 new mode 100644 index 2b02d34..4ed3904 --- a/src/eventsystem.c +++ b/src/eventsystem.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #undef LOG_TAG @@ -57,6 +56,7 @@ pthread_mutex_t send_sync_lock = PTHREAD_MUTEX_INITIALIZER; static GList *system_event_list; static int _initialized; static GHashTable *check_tbl; +static GHashTable *filter_tbl; typedef struct eventmap { char *event_name; @@ -78,7 +78,6 @@ typedef struct eventinfo { char *object_path; char *member_name; bool is_user_event; - gboolean is_trusted; bundle *event_data; } eventinfo_s; @@ -90,10 +89,10 @@ typedef struct sysevent_info { } sysevent_info_s; static sysevent_info_s s_info; -static int __eventsystem_request_event_launch(const char *method_name, - const char *event_name, bundle *data); static int __eventsystem_check_user_send_validation(const char *event_name); static int __eventsystem_requet_destination_list(const char *event_name, GList **dest_list); +static int __eventsystem_check_sender_validation(int sender_pid, + const char *event_name, char **sender); static int __event_compare_name_cb(gconstpointer a, gconstpointer b) { @@ -119,6 +118,45 @@ static void __initialize(void) _initialized = 1; } +static int __eventsystem_get_sender_pid(GDBusConnection *conn, const char *sender_name) +{ + GDBusMessage *msg = NULL; + GDBusMessage *reply = NULL; + GError *err = NULL; + GVariant *body; + int pid = 0; + + msg = g_dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus", + "org.freedesktop.DBus", "GetConnectionUnixProcessID"); + if (!msg) { + _E("Can't allocate new method call"); + goto out; + } + + g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name)); + reply = g_dbus_connection_send_message_with_reply_sync(conn, msg, + G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, NULL, NULL, &err); + + if (!reply) { + if (err != NULL) { + _E("Failed to get pid [%s]", err->message); + g_error_free(err); + } + goto out; + } + + body = g_dbus_message_get_body(reply); + g_variant_get(body, "(u)", &pid); + +out: + if (msg) + g_object_unref(msg); + if (reply) + g_object_unref(reply); + + return pid; +} + static char *__get_object_path(char *interface_name) { int i; @@ -145,35 +183,27 @@ static char *__get_object_path(char *interface_name) static char *__get_encoded_interface_name(char *interface_name) { - unsigned char c[MD5_DIGEST_LENGTH] = {0, }; - char *md5_evtid = NULL; - char *temp; - int index = 0; - MD5_CTX mdContext; - - MD5_Init(&mdContext); - MD5_Update(&mdContext, interface_name, strlen(interface_name)); - MD5_Final(c, &mdContext); - - md5_evtid = (char *)calloc(EVENT_SYSTEM_PREFIX_LEN + (MD5_DIGEST_LENGTH * 2) + 1, - sizeof(char)); - if (md5_evtid == NULL) { - _D("Malloc failed!!"); - return NULL; - } - - sprintf(md5_evtid, "%s", EVENT_SYSTEM_PREFIX); + char *evtid = NULL; + int evtid_len = 0; + gchar *encoded_value; - temp = md5_evtid; + encoded_value = g_compute_checksum_for_string(G_CHECKSUM_MD5, + interface_name, strlen(interface_name)); - temp += EVENT_SYSTEM_PREFIX_LEN; + evtid_len = EVENT_SYSTEM_PREFIX_LEN + strlen(encoded_value) + 1; - for (index = 0; index < MD5_DIGEST_LENGTH; index++) { - sprintf(temp, "%02x", c[index]); - temp += 2; + evtid = (char *)calloc(evtid_len, sizeof(char)); + if (evtid == NULL) { + _D("memory alloc failed"); + g_free(encoded_value); + return NULL; } - return md5_evtid; + snprintf(evtid, evtid_len, "%s%s", EVENT_SYSTEM_PREFIX, (char *)encoded_value); + + g_free(encoded_value); + + return evtid; } static char *__get_member_name_from_eventname(char *event_name) @@ -182,6 +212,7 @@ static char *__get_member_name_from_eventname(char *event_name) char *ptr_last = NULL; char *temp_name = NULL; char *member_name = NULL; + char *save_ptr = NULL; int count = 0; temp_name = strdup(event_name); @@ -190,7 +221,7 @@ static char *__get_member_name_from_eventname(char *event_name) return NULL; } - ptr = strtok(temp_name, "."); + ptr = strtok_r(temp_name, ".", &save_ptr); if (ptr == NULL) { _E("invalid event_name(%s), count(%d)", event_name, count); FREE_AND_NULL(temp_name); @@ -199,7 +230,7 @@ static char *__get_member_name_from_eventname(char *event_name) count++; while (count < MAX_COUNT_FOR_EVENTNAME_CHECK) { - ptr = strtok(NULL, "."); + ptr = strtok_r(NULL, ".", &save_ptr); if (ptr == NULL) break; /* _D("(%d)ptr(%s)(%d)", count, ptr, strlen(ptr)); */ @@ -230,6 +261,42 @@ static char *__get_member_name_from_eventname(char *event_name) return member_name; } +static int __check_validation_usrevent_sender(int sender_pid, + const char *interface_name, const char *event_name) +{ + char *sender_id = NULL; + char *key = NULL; + char *val = NULL; + + if (__eventsystem_check_sender_validation(sender_pid, + event_name, &sender_id) < 0) { + _E("invalid user-event sender"); + return ES_R_EINVAL; + } + + if (sender_id == NULL) { + _E("sender_id is null"); + return ES_R_EINVAL; + } + + key = strdup(interface_name); + if (key == NULL) { + _E("out of memory"); + return ES_R_ENOMEM; + } + + val = strdup(sender_id); + if (val == NULL) { + _E("out of memory"); + free(key); + return ES_R_ENOMEM; + } + + g_hash_table_insert(filter_tbl, key, val); + + return ES_R_OK; +} + static int __check_validation_user_defined_name(const char *event_name) { char *event_id = NULL; @@ -260,6 +327,18 @@ static int __check_validation_user_defined_name(const char *event_name) return ret; } +static int __check_interface_validation_user(char *interface_name) +{ + int ret = 1; + int len = strlen(EVENT_SYSTEM_PREFIX); + + if (strncmp(interface_name, EVENT_SYSTEM_PREFIX, len) != 0) { + ret = 0; + } + + return ret; +} + static int __check_eventname_validation_user(char *event_name) { int ret = 1; @@ -385,7 +464,6 @@ static void __eventsystem_event_handler(GDBusConnection *connection, GList *cb_list = NULL; bundle *buf = NULL; bundle_raw *raw = NULL; - gboolean is_trusted = FALSE; em.interface_name = (char *)interface_name; em.member_name = (char *)signal_name; @@ -399,7 +477,7 @@ static void __eventsystem_event_handler(GDBusConnection *connection, return; } - g_variant_get(parameters, "(bus)", &is_trusted, &len, &raw); + g_variant_get(parameters, "(us)", &len, &raw); buf = bundle_decode((bundle_raw *)raw, len); @@ -413,8 +491,7 @@ static void __eventsystem_event_handler(GDBusConnection *connection, bundle_free(buf); } -static void __eventsystem_application_event_handler(GDBusConnection *connection, - const gchar *sender_name, const gchar *object_path, +static void __eventsystem_application_event_handler(int sender_pid, const gchar *interface_name, const gchar *signal_name, GVariant *parameters, gpointer user_data) { @@ -426,19 +503,25 @@ static void __eventsystem_application_event_handler(GDBusConnection *connection, em.interface_name = (char *)interface_name; em.member_name = (char *)signal_name; - _D("sender_name(%s), interface_name(%s), signal_name(%s)", - sender_name, interface_name, signal_name); - cb_list = g_list_find_custom(system_event_list, &em, (GCompareFunc)__event_compare_name_cb); if (cb_list == NULL) { + _E("not interested event"); return; } - g_variant_get(parameters, "(bus)", NULL, &len, &raw); - em.event_name = ((eventmap_s *)cb_list->data)->event_name; + + if (sender_pid > 0 && __check_interface_validation_user((char *)interface_name)) { + if (__check_validation_usrevent_sender(sender_pid, + (const char *)interface_name, em.event_name) < 0) { + _E("invalid sender"); + return; + } + } + + g_variant_get(parameters, "(us)", &len, &raw); em.es_cb = ((eventmap_s *)cb_list->data)->es_cb; if (em.es_cb) { em.es_cb(em.event_name, raw, len, user_data); @@ -455,14 +538,28 @@ static void __eventsystem_filter_userevent_for_application(GDBusConnection *conn const gchar *interface_name, const gchar *signal_name, GVariant *parameters, gpointer user_data) { - gboolean is_trusted = FALSE; + char *sender_id = NULL; + int sender_pid = -1; _D("sender_name(%s), interface_name(%s)", sender_name, interface_name); - g_variant_get(parameters, "(bus)", &is_trusted, NULL, NULL); + if (filter_tbl == NULL) { + filter_tbl = g_hash_table_new(g_str_hash, g_str_equal); + } - __eventsystem_application_event_handler(connection, sender_name, - object_path, interface_name, signal_name, parameters, user_data); + sender_id = (char *)g_hash_table_lookup(filter_tbl, interface_name); + + if (sender_id == NULL) { + sender_pid = __eventsystem_get_sender_pid(connection, sender_name); + if (sender_pid <= 0) { + _E("failed to get pid of sender(%s)", sender_name); + return; + } + _D("sender_pid(%d)", sender_pid); + } + + __eventsystem_application_event_handler(sender_pid, interface_name, + signal_name, parameters, user_data); } /** @@ -475,8 +572,8 @@ static void __eventsystem_filter_sysevent_for_application(GDBusConnection *conne { _D("sender_name(%s), interface_name(%s)", sender_name, interface_name); - __eventsystem_application_event_handler(connection, sender_name, - object_path, interface_name, signal_name, parameters, user_data); + __eventsystem_application_event_handler(-1, interface_name, + signal_name, parameters, user_data); } /** @@ -730,7 +827,7 @@ int eventsystem_unregister_event(unsigned int reg_id) return ES_R_OK; } -static int eventsystem_send_event(GDBusConnection *conn, eventinfo_s *evti, bundle *data) +static int __eventsystem_send_event(GDBusConnection *conn, eventinfo_s *evti, bundle *data) { GError *error = NULL; GVariant *param = NULL; @@ -742,10 +839,7 @@ static int eventsystem_send_event(GDBusConnection *conn, eventinfo_s *evti, bund bundle_encode(buf, &raw, &len); - if (!evti->is_user_event) - evti->is_trusted = FALSE; - - param = g_variant_new("(bus)", evti->is_trusted, len, raw); + param = g_variant_new("(us)", len, raw); ret = g_dbus_connection_emit_signal(conn, evti->destination_name, evti->object_path, @@ -781,7 +875,6 @@ static int __eventsystem_send_trusted_event(GDBusConnection *conn, eventinfo_s * { GList *tmp_list = NULL; int ret = 0; - int real_sent = 0; if (dest_list) { tmp_list = g_list_first(dest_list); @@ -791,22 +884,16 @@ static int __eventsystem_send_trusted_event(GDBusConnection *conn, eventinfo_s * if (dest_name && dest_name[0] != '\0') { _D("dest_name(%s)", dest_name); evti->destination_name = dest_name; - ret = eventsystem_send_event(conn, evti, data); + ret = __eventsystem_send_event(conn, evti, data); if (ret != ES_R_OK) { _E("send error"); ret = ES_R_ERROR; break; } - real_sent = 1; } tmp_list = g_list_next(tmp_list); } g_list_free_full(dest_list, __eventsystem_free_trusted_list); - - if (real_sent) { - __eventsystem_request_event_launch("RequestTrustedEventLaunch", - evti->event_name, data); - } } else { _E("dest_list is null"); } @@ -867,11 +954,10 @@ int eventsystem_send_user_event(const char *event_name, bundle *data, bool is_tr evti->destination_name = NULL; evti->is_user_event = true; - evti->is_trusted = (gboolean)is_trusted; GDBusConnection *conn = NULL; if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SESSION, ES_TYPE_USER) == ES_R_OK) { - if (evti->is_trusted) { + if (is_trusted) { GList *dest_list = NULL; ret = __eventsystem_requet_destination_list(evti->event_name, &dest_list); if (ret < 0) { @@ -882,11 +968,7 @@ int eventsystem_send_user_event(const char *event_name, bundle *data, bool is_tr data, dest_list); } } else { - ret = eventsystem_send_event(conn, evti, data); - if (ret == ES_R_OK) { - __eventsystem_request_event_launch("RequestEventLaunch", - evti->event_name, data); - } + ret = __eventsystem_send_event(conn, evti, data); } } else { _E("getting gdbus-connetion error"); @@ -966,11 +1048,10 @@ int eventsystem_send_system_event(const char *event_name, bundle *data) } evti->destination_name = NULL; evti->is_user_event = false; - evti->is_trusted = FALSE; GDBusConnection *conn = NULL; if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) == ES_R_OK) { - ret = eventsystem_send_event(conn, evti, data); + ret = __eventsystem_send_event(conn, evti, data); } else { _E("getting gdbus-connection error"); ret = ES_R_ERROR; @@ -1061,20 +1142,16 @@ out_1: return ret; } -static int __eventsystem_request_event_launch(const char *method_name, - const char *event_name, bundle *data) +static int __eventsystem_check_sender_validation(int sender_pid, const char *event_name, + char **sender_id) { - int ret = 0; + int ret = ES_R_EINVAL; GDBusConnection *conn = NULL; GError *error = NULL; GDBusProxy *proxy = NULL; GVariant *param = NULL; GVariant *value = NULL; gint result = 0; - bundle_raw *raw = NULL; - int len = 0; - - _D("event_name(%s)", event_name); if (!_initialized) { __initialize(); @@ -1097,10 +1174,8 @@ static int __eventsystem_request_event_launch(const char *method_name, goto out_1; } - bundle_encode(data, &raw, &len); - - param = g_variant_new("(ssi)", event_name, raw, len); - value = g_dbus_proxy_call_sync(proxy, method_name, param, + param = g_variant_new("(is)", sender_pid, event_name); + value = g_dbus_proxy_call_sync(proxy, "CheckSenderValidation", param, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); if (error != NULL) { _E("proxy call sync error(%s)", error->message); @@ -1109,12 +1184,13 @@ static int __eventsystem_request_event_launch(const char *method_name, goto out_2; } - g_variant_get(value, "(i)", &result); + g_variant_get(value, "(is)", &result, sender_id); _D("result(%d)", result); - ret = ES_R_OK; - + if (result == 1) { + ret = ES_R_OK; + } out_2: g_variant_unref(value); out_1: @@ -1692,6 +1768,29 @@ int eventsystem_application_finalize(void) g_hash_table_unref(check_tbl); } + if (filter_tbl) { + GHashTableIter iter; + + g_hash_table_iter_init(&iter, filter_tbl); + + while (g_hash_table_iter_next(&iter, &key, &value)) { + char *key_item = (char *)key; + if (key_item) { + free(key_item); + } else { + _E("filter_tbl, key_item is NULL"); + } + char *val_item = (char *)value; + if (val_item) { + free(val_item); + } else { + _E("filter_tbl, val_item is NULL"); + } + g_hash_table_iter_remove(&iter); + } + g_hash_table_unref(filter_tbl); + } + FREE_AND_NULL(s_info.own_name_system_bus); FREE_AND_NULL(s_info.own_name_session_bus); -- 2.7.4 From 13b10c39fff21cbd3633545b0ce3571457d2242b Mon Sep 17 00:00:00 2001 From: jongmyeongko Date: Fri, 27 Nov 2015 00:54:05 +0900 Subject: [PATCH 5/6] change owning name for dbus policy Change-Id: Ie78815debd2bc62b6393747320274adcd3b3b519 Signed-off-by: jongmyeongko --- src/eventsystem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/eventsystem.c b/src/eventsystem.c index 4ed3904..1753e7e 100644 --- a/src/eventsystem.c +++ b/src/eventsystem.c @@ -393,9 +393,9 @@ static int __get_gdbus_shared_connection(GDBusConnection **connection, GBusType _E("failed to get guid"); return ES_R_ERROR; } - snprintf(own_name, 128, "%s_%s_%s_%d", "event.busname", + snprintf(own_name, 128, "%s.%s.id%s_%d_%d", "event.busname", (bus_type == G_BUS_TYPE_SESSION ? "session" : "system"), - guid, getpid()); + guid, getuid(), getpid()); g_free(guid); _D("bus_name is [%s]", own_name); -- 2.7.4 From ee586d724ba72533f730ac40e82582df0363b512 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Tue, 15 Dec 2015 19:46:31 +0900 Subject: [PATCH 6/6] Apply coding rule Change-Id: I7df3e5aa9ffe648f2eab3cb1ed4b24a4ead23b1a Signed-off-by: Hwankyu Jhun --- src/eventsystem.c | 195 +++++++++++++++++++++--------------------------------- 1 file changed, 76 insertions(+), 119 deletions(-) diff --git a/src/eventsystem.c b/src/eventsystem.c index 1753e7e..1cef92c 100644 --- a/src/eventsystem.c +++ b/src/eventsystem.c @@ -160,7 +160,7 @@ out: static char *__get_object_path(char *interface_name) { int i; - char *object_path = (char *)calloc(strlen(interface_name), sizeof(char)+2); + char *object_path = (char *)calloc(strlen(interface_name), sizeof(char) + 2); if (object_path == NULL) { _E("failed to allocate memory"); @@ -169,13 +169,11 @@ static char *__get_object_path(char *interface_name) object_path[0] = '/'; - for (i = 0 ; interface_name[i] ; i++) { - - if (interface_name[i] == '.') { - object_path[i+1] = '/'; - } else { - object_path[i+1] = interface_name[i]; - } + for (i = 0; interface_name[i]; i++) { + if (interface_name[i] == '.') + object_path[i + 1] = '/'; + else + object_path[i + 1] = interface_name[i]; } return object_path; @@ -303,12 +301,10 @@ static int __check_validation_user_defined_name(const char *event_name) char *key = NULL; int ret = 1; - if (check_tbl == NULL) { + if (check_tbl == NULL) check_tbl = g_hash_table_new(g_str_hash, g_str_equal); - } event_id = (char *)g_hash_table_lookup(check_tbl, event_name); - if (event_id == NULL) { if (__eventsystem_check_user_send_validation(event_name) < 0) { _E("invalid user-event name"); @@ -329,38 +325,32 @@ static int __check_validation_user_defined_name(const char *event_name) static int __check_interface_validation_user(char *interface_name) { - int ret = 1; int len = strlen(EVENT_SYSTEM_PREFIX); - if (strncmp(interface_name, EVENT_SYSTEM_PREFIX, len) != 0) { - ret = 0; - } + if (strncmp(interface_name, EVENT_SYSTEM_PREFIX, len) != 0) + return 0; - return ret; + return 1; } static int __check_eventname_validation_user(char *event_name) { - int ret = 1; int len = strlen(USER_EVENT_NAME_PREFIX); - if (strncmp(event_name, USER_EVENT_NAME_PREFIX, len) != 0) { - ret = 0; - } + if (strncmp(event_name, USER_EVENT_NAME_PREFIX, len) != 0) + return 0; - return ret; + return 1; } static int __check_eventname_validation_system(char *event_name) { - int ret = 1; int len = strlen(SYS_EVENT_NAME_PREFIX); - if (strncmp(event_name, SYS_EVENT_NAME_PREFIX, len) != 0) { - ret = 0; - } + if (strncmp(event_name, SYS_EVENT_NAME_PREFIX, len) != 0) + return 0; - return ret; + return 1; } static int __get_gdbus_shared_connection(GDBusConnection **connection, GBusType bus_type, @@ -368,10 +358,12 @@ static int __get_gdbus_shared_connection(GDBusConnection **connection, GBusType { GError *error = NULL; guint owner_id = 0; + gchar *guid = NULL; + char own_name[128] = {0, }; + GDBusConnection *conn_system = NULL; - if (!_initialized) { + if (!_initialized) __initialize(); - } *connection = g_bus_get_sync(bus_type, NULL, &error); if (*connection == NULL) { @@ -385,9 +377,6 @@ static int __get_gdbus_shared_connection(GDBusConnection **connection, GBusType if (((bus_type == G_BUS_TYPE_SYSTEM && !s_info.owner_id) || (bus_type == G_BUS_TYPE_SESSION && !s_info.owner_id_session))) { - gchar *guid = NULL; - char own_name[128] = {0, }; - guid = g_dbus_generate_guid(); if (guid == NULL) { _E("failed to get guid"); @@ -409,8 +398,6 @@ static int __get_gdbus_shared_connection(GDBusConnection **connection, GBusType if (bus_type == G_BUS_TYPE_SESSION && event_type == ES_TYPE_USER) { /* set same name on system-bus */ - GDBusConnection *conn_system = NULL; - error = NULL; conn_system = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); if (conn_system == NULL) { @@ -473,9 +460,8 @@ static void __eventsystem_event_handler(GDBusConnection *connection, cb_list = g_list_find_custom(system_event_list, &em, (GCompareFunc)__event_compare_name_cb); - if (cb_list == NULL) { + if (cb_list == NULL) return; - } g_variant_get(parameters, "(us)", &len, &raw); @@ -483,9 +469,8 @@ static void __eventsystem_event_handler(GDBusConnection *connection, em.event_name = ((eventmap_s *)cb_list->data)->event_name; em.ep_cb = ((eventmap_s *)cb_list->data)->ep_cb; - if (em.ep_cb) { + if (em.ep_cb) em.ep_cb(em.event_name, buf, user_data); - } bundle_free_encoded_rawdata(&raw); bundle_free(buf); @@ -523,9 +508,8 @@ static void __eventsystem_application_event_handler(int sender_pid, g_variant_get(parameters, "(us)", &len, &raw); em.es_cb = ((eventmap_s *)cb_list->data)->es_cb; - if (em.es_cb) { + if (em.es_cb) em.es_cb(em.event_name, raw, len, user_data); - } bundle_free_encoded_rawdata(&raw); } @@ -543,12 +527,10 @@ static void __eventsystem_filter_userevent_for_application(GDBusConnection *conn _D("sender_name(%s), interface_name(%s)", sender_name, interface_name); - if (filter_tbl == NULL) { + if (filter_tbl == NULL) filter_tbl = g_hash_table_new(g_str_hash, g_str_equal); - } sender_id = (char *)g_hash_table_lookup(filter_tbl, interface_name); - if (sender_id == NULL) { sender_pid = __eventsystem_get_sender_pid(connection, sender_name); if (sender_pid <= 0) { @@ -741,9 +723,8 @@ out_3: out_2: FREE_AND_NULL(interface_name); out_1: - if (conn) { + if (conn) g_object_unref(conn); - } return ret; } @@ -762,12 +743,10 @@ int eventsystem_register_event(const char *event_name, unsigned int *reg_id, retvm_if(!reg_id, ES_R_EINVAL, "Invalid argument : reg_id"); retvm_if(!callback, ES_R_EINVAL, "Invalid argument : callback"); - if (!_initialized) { + if (!_initialized) __initialize(); - } ret = __eventsystem_register_event_internal(event_name, &em, user_data); - if (ret == ES_R_OK && em) { em->ep_cb = callback; system_event_list = g_list_append(system_event_list, em); @@ -794,9 +773,8 @@ int eventsystem_unregister_event(unsigned int reg_id) retvm_if(reg_id == 0, ES_R_EINVAL, "Invalid argument : reg_id"); - if (!_initialized) { + if (!_initialized) __initialize(); - } em.reg_id = reg_id; cb_list = g_list_find_custom(system_event_list, &em, @@ -875,12 +853,13 @@ static int __eventsystem_send_trusted_event(GDBusConnection *conn, eventinfo_s * { GList *tmp_list = NULL; int ret = 0; + char *dest_name; if (dest_list) { tmp_list = g_list_first(dest_list); while (tmp_list != NULL) { - char *dest_name = (char *)tmp_list->data; + dest_name = (char *)tmp_list->data; if (dest_name && dest_name[0] != '\0') { _D("dest_name(%s)", dest_name); evti->destination_name = dest_name; @@ -907,6 +886,7 @@ static int __eventsystem_send_trusted_event(GDBusConnection *conn, eventinfo_s * int eventsystem_send_user_event(const char *event_name, bundle *data, bool is_trusted) { int ret = 0; + eventinfo_s *evti = NULL; /* check validation */ retvm_if(!event_name, ES_R_EINVAL, "Invalid argument : event_name is NULL"); @@ -919,7 +899,6 @@ int eventsystem_send_user_event(const char *event_name, bundle *data, bool is_tr return ES_R_EINVAL; } - eventinfo_s *evti = NULL; evti = calloc(1, sizeof(eventinfo_s)); if (!evti) { _E("memory alloc failed"); @@ -975,9 +954,9 @@ int eventsystem_send_user_event(const char *event_name, bundle *data, bool is_tr ret = ES_R_ERROR; } - if (conn) { + if (conn) g_object_unref(conn); - } + FREE_AND_NULL(evti->object_path); out_4: FREE_AND_NULL(evti->member_name); @@ -997,6 +976,8 @@ out_1: int eventsystem_send_system_event(const char *event_name, bundle *data) { int ret = 0; + eventinfo_s *evti = NULL; + GDBusConnection *conn = NULL; pthread_mutex_lock(&send_sync_lock); @@ -1010,7 +991,6 @@ int eventsystem_send_system_event(const char *event_name, bundle *data) _D("event_name(%s)", event_name); - eventinfo_s *evti = NULL; evti = calloc(1, sizeof(eventinfo_s)); if (!evti) { _E("memory alloc failed"); @@ -1049,7 +1029,6 @@ int eventsystem_send_system_event(const char *event_name, bundle *data) evti->destination_name = NULL; evti->is_user_event = false; - GDBusConnection *conn = NULL; if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) == ES_R_OK) { ret = __eventsystem_send_event(conn, evti, data); } else { @@ -1057,9 +1036,9 @@ int eventsystem_send_system_event(const char *event_name, bundle *data) ret = ES_R_ERROR; } - if (conn) { + if (conn) g_object_unref(conn); - } + FREE_AND_NULL(evti->object_path); out_4: FREE_AND_NULL(evti->member_name); @@ -1093,9 +1072,8 @@ int eventsystem_request_sending_system_event(const char *event_name, bundle *dat _D("event_name(%s)", event_name); - if (!_initialized) { + if (!_initialized) __initialize(); - } if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) < 0) { _E("getting gdbus-connetion error"); @@ -1135,9 +1113,8 @@ int eventsystem_request_sending_system_event(const char *event_name, bundle *dat out_2: g_variant_unref(value); out_1: - if (conn) { + if (conn) g_object_unref(conn); - } return ret; } @@ -1153,9 +1130,8 @@ static int __eventsystem_check_sender_validation(int sender_pid, const char *eve GVariant *value = NULL; gint result = 0; - if (!_initialized) { + if (!_initialized) __initialize(); - } if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) < 0) { _E("getting gdbus-connetion error"); @@ -1188,15 +1164,13 @@ static int __eventsystem_check_sender_validation(int sender_pid, const char *eve _D("result(%d)", result); - if (result == 1) { + if (result == 1) ret = ES_R_OK; - } out_2: g_variant_unref(value); out_1: - if (conn) { + if (conn) g_object_unref(conn); - } return ret; } @@ -1211,9 +1185,8 @@ static int __eventsystem_check_user_send_validation(const char *event_name) GVariant *value = NULL; gint result = 0; - if (!_initialized) { + if (!_initialized) __initialize(); - } if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) < 0) { _E("getting gdbus-connetion error"); @@ -1246,15 +1219,13 @@ static int __eventsystem_check_user_send_validation(const char *event_name) _D("result(%d)", result); - if (result == 1) { + if (result == 1) ret = ES_R_OK; - } out_2: g_variant_unref(value); out_1: - if (conn) { + if (conn) g_object_unref(conn); - } return ret; } @@ -1269,9 +1240,8 @@ static int __eventsystem_check_privilege_validation(const char *event_name) GVariant *value = NULL; gint result = 0; - if (!_initialized) { + if (!_initialized) __initialize(); - } if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) < 0) { _E("getting gdbus-connetion error"); @@ -1304,15 +1274,13 @@ static int __eventsystem_check_privilege_validation(const char *event_name) _D("result(%d)", result); - if (result == 1) { + if (result == 1) ret = ES_R_OK; - } out_2: g_variant_unref(value); out_1: - if (conn) { + if (conn) g_object_unref(conn); - } return ret; } @@ -1327,9 +1295,8 @@ static int __eventsystem_setup_trusted_peer(const char *event_name, const char * GVariant *value = NULL; gint result = 0; - if (!_initialized) { + if (!_initialized) __initialize(); - } if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) < 0) { _E("getting gdbus-connetion error"); @@ -1363,15 +1330,13 @@ static int __eventsystem_setup_trusted_peer(const char *event_name, const char * _D("result(%d)", result); - if (result == 1) { + if (result == 1) ret = ES_R_OK; - } out_2: g_variant_unref(value); out_1: - if (conn) { + if (conn) g_object_unref(conn); - } return ret; } @@ -1389,9 +1354,8 @@ static int __eventsystem_requet_destination_list(const char *event_name, GList * char *dest_name = NULL; gint result = 0; - if (!_initialized) { + if (!_initialized) __initialize(); - } if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) < 0) { _E("getting gdbus-connetion error"); @@ -1444,9 +1408,8 @@ static int __eventsystem_requet_destination_list(const char *event_name, GList * out_2: g_variant_unref(value); out_1: - if (conn) { + if (conn) g_object_unref(conn); - } return ret; } @@ -1460,13 +1423,13 @@ static int __eventsystem_request_earlier_data(const char *event_name, GError *error = NULL; GDBusProxy *proxy = NULL; GVariant *param = NULL; + GVariant *value = NULL; gint result = 0; bundle_raw *raw = NULL; int len = 0; - if (!_initialized) { + if (!_initialized) __initialize(); - } if (__get_gdbus_shared_connection(&conn, G_BUS_TYPE_SYSTEM, ES_TYPE_SYSTEM) < 0) { _E("getting gdbus-connetion error"); @@ -1487,7 +1450,7 @@ static int __eventsystem_request_earlier_data(const char *event_name, } param = g_variant_new("(s)", event_name); - GVariant *value = g_dbus_proxy_call_sync(proxy, "GetEarlierData", param, + value = g_dbus_proxy_call_sync(proxy, "GetEarlierData", param, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error); if (error != NULL) { _E("proxy call sync error(%s)", error->message); @@ -1511,9 +1474,8 @@ static int __eventsystem_request_earlier_data(const char *event_name, out_2: g_variant_unref(value); out_1: - if (conn) { + if (conn) g_object_unref(conn); - } return ret; } @@ -1533,9 +1495,8 @@ int eventsystem_register_application_event(const char *event_name, unsigned int int ret = 0; GDBusConnection *conn = NULL; - if (!_initialized) { + if (!_initialized) __initialize(); - } if (__check_eventname_validation_system((char *)event_name)) { if (__eventsystem_check_privilege_validation(event_name) != ES_R_OK) { @@ -1687,9 +1648,8 @@ int eventsystem_register_application_event(const char *event_name, unsigned int FREE_AND_NULL(interface_name); FREE_AND_NULL(object_path); FREE_AND_NULL(member_name); - if (conn) { + if (conn) g_object_unref(conn); - } return ret; } @@ -1705,9 +1665,8 @@ int eventsystem_unregister_application_event(unsigned int reg_id) retvm_if(reg_id == 0, ES_R_EINVAL, "Invalid argument : reg_id"); - if (!_initialized) { + if (!_initialized) __initialize(); - } em.reg_id = reg_id; cb_list = g_list_find_custom(system_event_list, &em, @@ -1743,49 +1702,47 @@ int eventsystem_unregister_application_event(unsigned int reg_id) int eventsystem_application_finalize(void) { - gpointer key, value; + gpointer key; + gpointer value; + GHashTableIter iter; + char *key_item; + char *val_item; _D("release all resouces"); - if (system_event_list) { + if (system_event_list) g_list_free(system_event_list); - } if (check_tbl) { - GHashTableIter iter; - g_hash_table_iter_init(&iter, check_tbl); while (g_hash_table_iter_next(&iter, &key, &value)) { - char *val_item = (char *)value; - if (val_item) { + val_item = (char *)value; + if (val_item) free(val_item); - } else { + else _E("check_tbl, val_item is NULL"); - } g_hash_table_iter_remove(&iter); } g_hash_table_unref(check_tbl); } if (filter_tbl) { - GHashTableIter iter; - g_hash_table_iter_init(&iter, filter_tbl); while (g_hash_table_iter_next(&iter, &key, &value)) { - char *key_item = (char *)key; - if (key_item) { + key_item = (char *)key; + if (key_item) free(key_item); - } else { + else _E("filter_tbl, key_item is NULL"); - } - char *val_item = (char *)value; - if (val_item) { + + val_item = (char *)value; + if (val_item) free(val_item); - } else { + else _E("filter_tbl, val_item is NULL"); - } + g_hash_table_iter_remove(&iter); } g_hash_table_unref(filter_tbl); -- 2.7.4