From 0608c5f92f6bfd5afbba0d064eea332154b11105 Mon Sep 17 00:00:00 2001 From: Jusung Son Date: Thu, 6 Aug 2020 11:25:08 +0900 Subject: [PATCH 01/16] Change service file name according to dbus naming rule Change-Id: I34c24a24dfbb00c591463ed64f206762194bce31 Signed-off-by: Jusung Son --- CMakeLists.txt | 2 +- org.tizen.esd.service.in => tizen.system.event.app2esd.service.in | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename org.tizen.esd.service.in => tizen.system.event.app2esd.service.in (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0febb69..11e42d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,7 @@ SET_TARGET_PROPERTIES(esd PROPERTIES LINK_FLAGS "-pie -Wl,-z,relro") # pkgconfig file configure_file(esd.manifest.in esd.manifest @ONLY) -configure_file(org.tizen.esd.service.in tizen.system.event.app2esd.service @ONLY) +configure_file(tizen.system.event.app2esd.service.in tizen.system.event.app2esd.service @ONLY) INSTALL(TARGETS esd DESTINATION bin) INSTALL(FILES ${CMAKE_SOURCE_DIR}/eventsystem.conf DESTINATION /etc/dbus-1/system.d) diff --git a/org.tizen.esd.service.in b/tizen.system.event.app2esd.service.in similarity index 100% rename from org.tizen.esd.service.in rename to tizen.system.event.app2esd.service.in -- 2.7.4 From 2a8cd22282e8e574a876c2726c5a9e2283157843 Mon Sep 17 00:00:00 2001 From: Jusung Son Date: Thu, 6 Aug 2020 13:05:06 +0900 Subject: [PATCH 02/16] Release version 0.1.20 Changes: - [fix] Rename dbus auto-activation service file - Change service file name according to dbus naming rule Change-Id: I4e6805914169895865de7e1e1acd78b498d52f19 Signed-off-by: Jusung Son --- packaging/esd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/esd.spec b/packaging/esd.spec index aa264a4..84b4dfa 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 0.1.19 +Version: 0.1.20 Release: 1 Group: Application Framework/Service License: Apache-2.0 -- 2.7.4 From d7b6dc0c5dd17338a5239c647bbbf7ea2417d8a3 Mon Sep 17 00:00:00 2001 From: Jusung Son Date: Mon, 21 Dec 2020 14:48:14 +0900 Subject: [PATCH 03/16] Fix wrong memory release order Change-Id: Id8841bc9b552a7856adab4671622289e8870ee5d Signed-off-by: Jusung Son --- src/esd_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/esd_main.c b/src/esd_main.c index 9484e89..f34b0a4 100644 --- a/src/esd_main.c +++ b/src/esd_main.c @@ -260,8 +260,8 @@ static bool __esd_check_platform_cert(const char *pkgid, uid_t uid) _E("certsvc_certificate_get_visibility() is failed."); pkgmgrinfo_pkginfo_destroy_certinfo(certinfo); - certsvc_instance_free(instance); certsvc_certificate_free(certificate); + certsvc_instance_free(instance); _D("visibility is %d", visibility); if (visibility & CERTSVC_VISIBILITY_PLATFORM) { -- 2.7.4 From 927d468709ca9f254c544dac3e6867a564fb44c6 Mon Sep 17 00:00:00 2001 From: Jusung Son Date: Mon, 21 Dec 2020 16:07:30 +0900 Subject: [PATCH 04/16] Release version 0.1.21 Changes: - Fix wrong memory release order Change-Id: I7337dd43f676d02d46f9d4c9cb4d1c2cbb794cad Signed-off-by: Jusung Son --- packaging/esd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/esd.spec b/packaging/esd.spec index 84b4dfa..8918116 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 0.1.20 +Version: 0.1.21 Release: 1 Group: Application Framework/Service License: Apache-2.0 -- 2.7.4 From 6e2e3444660bc3f33f18728feddd2479bbee7fb0 Mon Sep 17 00:00:00 2001 From: INSUN PYO Date: Wed, 3 Feb 2021 12:21:56 +0900 Subject: [PATCH 05/16] spec: remove unused pkgconfig(libsystemd-daemon) build dependency Change-Id: I8a6ec5f2c1f37cbe80ab42ce39792527a26ccfda --- CMakeLists.txt | 2 +- packaging/esd.spec | 2 +- src/esd_main.c | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 11e42d7..d62feab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,7 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src) ### Required packages INCLUDE(FindPkgConfig) -pkg_check_modules(pkgs REQUIRED dlog bundle pkgmgr-info glib-2.0 gio-2.0 appsvc aul vconf libtzplatform-config libsystemd-daemon cert-svc-vcore cynara-client cynara-creds-gdbus cynara-session security-manager) +pkg_check_modules(pkgs REQUIRED dlog bundle pkgmgr-info glib-2.0 gio-2.0 appsvc aul vconf libtzplatform-config cert-svc-vcore cynara-client cynara-creds-gdbus cynara-session security-manager) FOREACH(flag ${pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") diff --git a/packaging/esd.spec b/packaging/esd.spec index 8918116..62e7c50 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -18,7 +18,7 @@ BuildRequires: pkgconfig(pkgmgr) BuildRequires: pkgconfig(eventsystem) BuildRequires: pkgconfig(vconf) BuildRequires: pkgconfig(libtzplatform-config) -BuildRequires: pkgconfig(libsystemd-daemon) +BuildRequires: pkgconfig(systemd) BuildRequires: pkgconfig(cert-svc-vcore) BuildRequires: pkgconfig(cynara-client) BuildRequires: pkgconfig(cynara-creds-gdbus) diff --git a/src/esd_main.c b/src/esd_main.c index f34b0a4..42409f4 100644 --- a/src/esd_main.c +++ b/src/esd_main.c @@ -15,7 +15,6 @@ #include #include #include -#include #include #include #include -- 2.7.4 From b0d3e4dc862d94fecfee218331f5af58e5a9da36 Mon Sep 17 00:00:00 2001 From: jusung Date: Wed, 10 Feb 2021 09:06:56 +0900 Subject: [PATCH 06/16] Release version 0.1.22 Changes: - spec: remove unused pkgconfig(libsystemd-daemon) build dependency Signed-off-by: jusung Change-Id: I717d3886598c99d632361978c76c0e82d2a9cd47 --- packaging/esd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/esd.spec b/packaging/esd.spec index 62e7c50..55d4b2f 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 0.1.21 +Version: 0.1.22 Release: 1 Group: Application Framework/Service License: Apache-2.0 -- 2.7.4 From e9568556c624294baf61d01f8f954801cdf4decc Mon Sep 17 00:00:00 2001 From: Sangyoon Jang Date: Mon, 12 Apr 2021 17:52:42 +0900 Subject: [PATCH 07/16] Add esd-cion module Change-Id: I79a1a49a03073a2f9aa3cdc201d54e5fd1adb627 Signed-off-by: Sangyoon Jang --- include/eventsystem_daemon.h | 3 +- src/esd_cion.c | 316 +++++++++++++++++++++++++++++++++++++++++++ src/esd_main.c | 7 + 3 files changed, 325 insertions(+), 1 deletion(-) create mode 100644 src/esd_cion.c diff --git a/include/eventsystem_daemon.h b/include/eventsystem_daemon.h index a5aaa8f..775426b 100644 --- a/include/eventsystem_daemon.h +++ b/include/eventsystem_daemon.h @@ -49,10 +49,11 @@ extern "C" { #define SYSTEMD_DBUS_SIGNAL_USER_STARTUP_FINISHED "UserSessionStartupFinished" int __esd_register_vconf_callbacks(void); +int __esd_cion_init(void); +void __esd_cion_finalize(void); #ifdef __cplusplus } #endif #endif /* __EVENTSYSTEM_DAEMONE_H__ */ - diff --git a/src/esd_cion.c b/src/esd_cion.c new file mode 100644 index 0000000..48447e0 --- /dev/null +++ b/src/esd_cion.c @@ -0,0 +1,316 @@ +#include +#include +#include +#include +#include +#include + +#include "eventsystem_daemon.h" + +#define CION_METADATA_KEY "http://tizen.org/metadata/cion" + +static uid_t cur_uid; +static pkgmgr_client *pkgmgr; +static GList *service_list; + +struct cion_service { + char *pkgid; + char *appid; + char *service_name; + char *uuid; + int port; +}; + +static void __free_cion_service(gpointer data) +{ + struct cion_service *service = (struct cion_service *)data; + + free(service->pkgid); + free(service->appid); + free(service->service_name); + free(service->uuid); + free(service); +} + +static int __esd_cion_set_cur_uid(void) +{ + /* TODO(jeremy.jang): get current user from gumd or systemd */ + cur_uid = 5001; + return 0; +} + +static int __esd_cion_foreach_metadata_callback(const char *key, + const char *val, void *user_data) +{ + struct cion_service *service; + GList **service_list = (GList **)user_data; + + if (strncmp(key, CION_METADATA_KEY, strlen(CION_METADATA_KEY)) != 0) + return 0; + + if (val == NULL || strlen(val) == 0) { + _E("Service name is mandatory"); + return 0; + } + + service = calloc(1, sizeof(struct cion_service)); + if (service == NULL) { + _E("Out of memory"); + return -1; + } + + service->service_name = strdup(val); + if (service->service_name == NULL) { + _E("Out of memory"); + return -1; + } + + *service_list = g_list_append(*service_list, (gpointer)service); + + return 0; +} + +static void __esd_cion_remove_cion_service_by_appid(const char *appid) +{ + GList *item; + GList *next; + struct cion_service *service; + + item = service_list; + while (item != NULL) { + next = item->next; + service = (struct cion_service *)item->data; + if (strcmp(service->appid, appid) == 0) { + _D("Remove a cion service [%s:%s:%s:%d]", + service->appid, service->service_name, + service->uuid, service->port); + __free_cion_service(service); + service_list = g_list_delete_link(service_list, item); + } + item = next; + } +} + +static void __esd_cion_remove_cion_service_by_pkgid(const char *pkgid) +{ + GList *item; + GList *next; + struct cion_service *service; + + item = service_list; + while (item != NULL) { + next = item->next; + service = (struct cion_service *)item->data; + if (strcmp(service->pkgid, pkgid) == 0) { + _D("Remove a cion service [%s:%s:%s:%d]", + service->appid, service->service_name, + service->uuid, service->port); + __free_cion_service(service); + service_list = g_list_delete_link(service_list, item); + } + item = next; + } +} + +static int __esd_cion_add_cion_service(struct cion_service *service, + const char *pkgid, const char *appid) +{ + /* service name already set by __esd_cion_foreach_metadata_callback() */ + service->pkgid = strdup(pkgid); + if (service->pkgid == NULL) { + _E("Out of memory"); + return -1; + } + + service->appid = strdup(appid); + if (service->appid == NULL) { + _E("Out of memory"); + return -1; + } + + /* how to get uuid? */ + + service_list = g_list_append(service_list, service); + _D("Add a cion service [%s:%s:%s:%d]", service->appid, + service->service_name, service->uuid, + service->port); + + return 0; +} + +static int __esd_cion_foreach_app_callback(const pkgmgrinfo_appinfo_h appinfo, + void *user_data) +{ + int ret; + char *pkgid; + char *appid; + struct cion_service *service; + GList *item; + GList *list = NULL; + + ret = pkgmgrinfo_appinfo_foreach_metadata(appinfo, + __esd_cion_foreach_metadata_callback, &list); + if (ret != PMINFO_R_OK) { + _E("Failed to get metadata: %d", ret); + return -1; + } + + ret = pkgmgrinfo_appinfo_get_pkgid(appinfo, &pkgid); + if (ret != PMINFO_R_OK) { + _E("Failed to get pkgid: %d", ret); + return -1; + } + + ret = pkgmgrinfo_appinfo_get_appid(appinfo, &appid); + if (ret != PMINFO_R_OK) { + _E("Failed to get appid: %d", ret); + return -1; + } + + /* remove first, the updated app may no longer provide cion service */ + __esd_cion_remove_cion_service_by_appid(appid); + for (item = list; item; item = item->next) { + service = (struct cion_service *)item->data; + if (__esd_cion_add_cion_service(service, pkgid, appid)) { + _E("Failed to add a cion service"); + __free_cion_service(service); + } + /* remove reference, the global 'service_list' takes reference + * of 'service'. + */ + item->data = NULL; + } + + g_list_free(list); + + return 0; +} + +static int __esd_cion_pkgmgr_event_callback(uid_t target_uid, int req_id, + const char *pkg_type, const char *pkgid, const char *key, + const char *val, const void *pmsg, void *data) +{ + int ret; + pkgmgrinfo_pkginfo_h pkginfo; + + if (strncmp(key, "end", strlen("end")) || + strncmp(val, "ok", strlen("ok"))) + return 0; + + ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, target_uid, &pkginfo); + if (ret == PMINFO_R_OK) { + /* install or update */ + ret = pkgmgrinfo_appinfo_get_usr_list(pkginfo, PMINFO_ALL_APP, + __esd_cion_foreach_app_callback, NULL, + target_uid); + if (ret != PMINFO_R_OK) { + _E("Failed to get appinfo of pkgid %s: %d", pkgid, ret); + pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo); + return 0; + } + pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo); + } else if (ret == PMINFO_R_ENOENT) { + /* uninstall */ + __esd_cion_remove_cion_service_by_pkgid(pkgid); + } else { + _E("Failed to get pkginfo of %s: %d", pkgid, ret); + } + + return 0; +} + +static int __esd_cion_set_pkgmgr_callback(void) +{ + int ret; + + pkgmgr = pkgmgr_client_new(PC_LISTENING); + if (pkgmgr == NULL) { + _E("Failed to create pkgmgr client"); + return -1; + } + + ret = pkgmgr_client_set_status_type(pkgmgr, + PKGMGR_CLIENT_STATUS_INSTALL | + PKGMGR_CLIENT_STATUS_UPGRADE | + PKGMGR_CLIENT_STATUS_UNINSTALL); + if (ret != PKGMGR_R_OK) { + _E("Failed to set pkgmgr event status type: %d", ret); + pkgmgr_client_free(pkgmgr); + pkgmgr = NULL; + return -1; + } + + ret = pkgmgr_client_listen_status(pkgmgr, + __esd_cion_pkgmgr_event_callback, NULL); + if (ret < 0) { + _E("Failed to set event callback: %d", ret); + pkgmgr_client_free(pkgmgr); + pkgmgr = NULL; + return -1; + } + + return 0; +} + +static int __esd_cion_load_services(uid_t uid) +{ + int ret; + pkgmgrinfo_appinfo_metadata_filter_h filter; + + ret = pkgmgrinfo_appinfo_metadata_filter_create(&filter); + if (ret != PMINFO_R_OK) { + _E("Failed to create metadata filter: %d", ret); + return -1; + } + + ret = pkgmgrinfo_appinfo_metadata_filter_add(filter, + CION_METADATA_KEY, ""); + if (ret != PMINFO_R_OK) { + _E("Failed to add keyval to metadata filter: %d", ret); + pkgmgrinfo_appinfo_metadata_filter_destroy(filter); + return -1; + } + + ret = pkgmgrinfo_appinfo_usr_metadata_filter_foreach(filter, + __esd_cion_foreach_app_callback, NULL, uid); + if (ret != PMINFO_R_OK) { + _E("Failed to metadata filter foreach: %d", ret); + pkgmgrinfo_appinfo_metadata_filter_destroy(filter); + return -1; + } + + pkgmgrinfo_appinfo_metadata_filter_destroy(filter); + + return 0; +} + +int __esd_cion_init(void) +{ + if (__esd_cion_set_cur_uid()) { + _E("Failed to set current uid"); + return -1; + } + + /* how to handle when user switched? */ + if (__esd_cion_load_services(cur_uid)) { + _E("Failed to load cion services"); + return -1; + } + + if (__esd_cion_set_pkgmgr_callback()) { + _E("Failed to set pkgmgr event callback"); + return -1; + } + + return 0; +} + +void __esd_cion_finalize(void) +{ + if (pkgmgr) { + pkgmgr_client_remove_listen_status(pkgmgr); + pkgmgr_client_free(pkgmgr); + } + + g_list_free_full(service_list, __free_cion_service); +} diff --git a/src/esd_main.c b/src/esd_main.c index 42409f4..61f64ea 100644 --- a/src/esd_main.c +++ b/src/esd_main.c @@ -2404,6 +2404,12 @@ int main(int argc, char *argv[]) return ES_R_ERROR; } + if (__esd_cion_init() != 0) { + _E("ESD Cion Initialization failed!"); + g_main_loop_unref(mainloop); + return ES_R_ERROR; + } + if (__esd_before_loop() < 0) { _E("ESD failed!"); __esd_finalize(); @@ -2415,6 +2421,7 @@ int main(int argc, char *argv[]) _E("shutdown"); + __esd_cion_finalize(); __esd_finalize(); g_main_loop_unref(mainloop); -- 2.7.4 From 0cbe031476619fc0be9bb739c14f464786a3cd7d Mon Sep 17 00:00:00 2001 From: "SukHyung, Kang" Date: Fri, 28 May 2021 12:11:45 +0900 Subject: [PATCH 08/16] Add db for cion uuid Change-Id: I9a139d2b7f95eda3aa08c03ee162d47dcfdbf428 Signed-off-by: SukHyung, Kang --- CMakeLists.txt | 19 +++- include/eventsystem_daemon.h | 3 + packaging/esd.spec | 2 + src/esd_cion_db.c | 206 +++++++++++++++++++++++++++++++++++++++++++ src/esd_main.c | 90 ++++++++++++++++--- 5 files changed, 306 insertions(+), 14 deletions(-) create mode 100644 src/esd_cion_db.c diff --git a/CMakeLists.txt b/CMakeLists.txt index d62feab..66e0a4e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,24 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src) ### Required packages INCLUDE(FindPkgConfig) -pkg_check_modules(pkgs REQUIRED dlog bundle pkgmgr-info glib-2.0 gio-2.0 appsvc aul vconf libtzplatform-config cert-svc-vcore cynara-client cynara-creds-gdbus cynara-session security-manager) +pkg_check_modules(pkgs REQUIRED + dlog + bundle + pkgmgr-info + glib-2.0 + gio-2.0 + appsvc + aul + vconf + libtzplatform-config + cert-svc-vcore + cynara-client + cynara-creds-gdbus + cynara-session + security-manager + sqlite3 + uuid +) FOREACH(flag ${pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") diff --git a/include/eventsystem_daemon.h b/include/eventsystem_daemon.h index 775426b..5f600ce 100644 --- a/include/eventsystem_daemon.h +++ b/include/eventsystem_daemon.h @@ -52,6 +52,9 @@ int __esd_register_vconf_callbacks(void); int __esd_cion_init(void); void __esd_cion_finalize(void); +int esd_cion_db_init(void); +int esd_cion_get_uuid_with_generate(const char* appid, char** uuid); + #ifdef __cplusplus } #endif diff --git a/packaging/esd.spec b/packaging/esd.spec index 55d4b2f..6d7c3c5 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -24,6 +24,8 @@ BuildRequires: pkgconfig(cynara-client) BuildRequires: pkgconfig(cynara-creds-gdbus) BuildRequires: pkgconfig(cynara-session) BuildRequires: pkgconfig(security-manager) +BuildRequires: pkgconfig(uuid) +BuildRequires: pkgconfig(sqlite3) Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig diff --git a/src/esd_cion_db.c b/src/esd_cion_db.c new file mode 100644 index 0000000..2c34c62 --- /dev/null +++ b/src/esd_cion_db.c @@ -0,0 +1,206 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "eventsystem_daemon.h" + +#define DBPATH tzplatform_mkpath(TZ_SYS_DB, ".cion.db") + +#define CREATE_CION_TABLE " \ +PRAGMA user_version = 50; \ +PRAGMA journal_mode = PERSIST; \ +PRAGMA foreign_keys = ON; \ +BEGIN EXCLUSIVE TRANSACTION; \ +CREATE TABLE IF NOT EXISTS cion_info ( \ + appid TEXT NOT NULL, \ + uuid TEXT NOT NULL, \ + PRIMARY KEY (appid) \ +); \ +COMMIT TRANSACTION; " + +static int __check_table_exist(sqlite3 *db) { + int ret; + const char *val; + sqlite3_stmt *stmt = NULL; + const char query[] = + "SELECT name FROM sqlite_master WHERE type='table'" + " ORDER BY name ASC"; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _E("prepare error: %s", sqlite3_errmsg(db)); + ret = -1; + goto out; + } + + ret = sqlite3_step(stmt); + if (ret != SQLITE_ROW) { + _E("fail to get row"); + ret = -1; + goto out; + } + + val = (const char*)sqlite3_column_text(stmt, 0); + if (val == NULL) { + _E("name is NULL"); + ret = -1; + goto out; + } + + if (strcmp("cion", val) != 0) { + ret = -1; + goto out; + } + + ret = 0; + +out: + sqlite3_finalize(stmt); + + return ret; +} + +static int __create_table(sqlite3 *db) { + int ret; + char *errmsg = NULL; + + ret = sqlite3_exec(db, CREATE_CION_TABLE, + NULL, NULL, &errmsg); + if (ret != SQLITE_OK) { + _E("create table fail : %s", errmsg); + sqlite3_free(errmsg); + return -1; + } + + return 0; +} + +int esd_cion_db_init(void) { + sqlite3 *db = NULL; + + if (sqlite3_open_v2(DBPATH, &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) { + sqlite3_close_v2(db); + unlink(DBPATH); + + if (sqlite3_open_v2(DBPATH, &db, + SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, + NULL) != SQLITE_OK) { + _E("Fail to create db"); + unlink(DBPATH); + return -1; + } + } + + if (__check_table_exist(db) < 0) { + if (__create_table(db) < 0) { + sqlite3_close_v2(db); + _E("Fail to create table"); + return -1; + } + } + + sqlite3_close_v2(db); + + return 0; +} + +sqlite3 *esd_cion_db_open() { + sqlite3 *db; + + if (access(DBPATH, R_OK | W_OK) != 0) + return NULL; + + if (sqlite3_open_v2(DBPATH, &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) + return NULL; + + return db; +} + +int esd_cion_db_close(sqlite3 **db) { + if (db == NULL || *db == NULL) + return 0; + + if (sqlite3_close(*db) != SQLITE_OK) { + _E("Failed to close db"); + return 0; + } + + *db = NULL; + + return 0; +} + +static const char *__esd_cion_generate_uuid() { + uuid_t uu; + char _uuid[37]; + char *uuid; + + uuid_generate_random(uu); + uuid_unparse(uu, _uuid); + + uuid = strdup(_uuid); + + return uuid; +} + +int esd_cion_get_uuid_with_generate(const char *appid, char **uuid) { + int ret; + sqlite3 *db; + char *query = NULL; + sqlite3_stmt *stmt = NULL; + const char *_uuid; + + db = esd_cion_db_open(); + if (!db) { + _E("db open fail"); + return -1; + } + + query = sqlite3_mprintf("SELECT uuid FROM cion_info " + "WHERE appid = %Q", appid); + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + ret = -1; + goto out; + } + + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + ret = 0; + *uuid = strdup((char*)sqlite3_column_text(stmt, 0)); + } else { + sqlite3_free(query); + sqlite3_finalize(stmt); + stmt = NULL; + + _uuid = __esd_cion_generate_uuid(); + query = sqlite3_mprintf("INSERT INTO cion_info (appid, uuid) " + "VALUES (%Q, %Q) ", appid, _uuid); + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + ret = -1; + goto out; + } + + ret = sqlite3_step(stmt); + if (ret == SQLITE_OK || ret == SQLITE_DONE) { + ret = 0; + *uuid = strdup(_uuid); + } else { + ret = -1; + } + } + +out: + sqlite3_free(query); + sqlite3_finalize(stmt); + esd_cion_db_close(&db); + + return ret; +} diff --git a/src/esd_main.c b/src/esd_main.c index 61f64ea..9d4480e 100644 --- a/src/esd_main.c +++ b/src/esd_main.c @@ -1246,10 +1246,14 @@ static const gchar introspection_xml[] = " " " " " " +" " +" " +" " +" " " " ""; -static int __esd_get_appid_by_pid(int pid, uid_t uid, char *app_id, int buf_size) +static int __esd_get_appid_by_pid_for_uid(int pid, uid_t uid, char *app_id, int buf_size) { int retval = ES_R_OK; int ret = 0; @@ -1272,6 +1276,33 @@ static int __esd_get_appid_by_pid(int pid, uid_t uid, char *app_id, int buf_size return retval; } +static int __esd_get_appid_by_pid(int pid, char *app_id, int buf_size) +{ + int ret; + int fd; + char buf[128] = { 0, }; + + ret = aul_app_get_appid_bypid(pid, app_id, buf_size); + if (ret != AUL_R_OK) { + snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid); + + fd = open(buf, O_RDONLY); + if (fd < 0) + return ES_R_ERROR; + + ret = read(fd, app_id, sizeof(app_id) - 1); + close(fd); + + if (ret <= 0) + return ES_R_ERROR; + + app_id[ret] = '\0'; + ret = ES_R_OK; + } + + return ret; +} + static int check_user_event_sender_valid(const char *event_name, const char *app_id) { char *valid_name = NULL; @@ -1338,7 +1369,7 @@ static void check_sender_valid_method_call(GDBusConnection *connection, const gc _D("event_sender_pid(%d), event_name(%s)", event_sender_pid, event_name); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(event_sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { + if (__esd_get_appid_by_pid_for_uid(event_sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { if (check_user_event_sender_valid(event_name, app_id) < 0) { @@ -1369,7 +1400,7 @@ static void check_send_event_valid_method_call(GDBusConnection *connection, cons sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { if (check_user_event_sender_valid(event_name, app_id) < 0) { @@ -1408,7 +1439,7 @@ static void get_trusted_peer_method_call(GDBusConnection *connection, const gcha sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { builder = g_variant_builder_new(G_VARIANT_TYPE("as")); @@ -1456,7 +1487,7 @@ static void setup_trusted_peer_method_call(GDBusConnection *connection, const gc if (destination_name && destination_name[0] != '\0') { sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { ret = __esd_trusted_busname_add_item(sender_uid, app_id, destination_name, @@ -1500,7 +1531,7 @@ static void check_privilege_valid_method_call(GDBusConnection *connection, const if (privilege_name) { sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { ret = cynara_creds_gdbus_get_client(connection, sender, CLIENT_METHOD_DEFAULT, &client); @@ -1605,7 +1636,7 @@ static void keep_last_data_method_call(GDBusConnection *connection, sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { _E("failed to get appid by pid"); result = ES_R_ERROR; @@ -1670,7 +1701,7 @@ static void check_last_data_method_call(GDBusConnection *connection, sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { result = ES_R_ERROR; _E("failed to get appid by pid"); @@ -1757,7 +1788,7 @@ static void launch_on_event_from_userevent(GDBusConnection *connection, sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { _E("failed to get appid by pid"); result = ES_R_ERROR; @@ -1782,6 +1813,37 @@ out: g_dbus_method_invocation_return_value(invocation, param); } +static void get_uuid_method_call(GDBusConnection *connection, + const gchar *sender, GVariant *parameters, + GDBusMethodInvocation *invocation) +{ + GVariant *param = NULL; + int result = ES_R_OK; + char *uuid; + char app_id[128] = { 0, }; + int sender_pid; + + sender_pid = __get_sender_pid(connection, sender); + if (__esd_get_appid_by_pid(sender_pid, app_id, sizeof(app_id)) < 0) { + _E("failed to get appid by pid"); + result = ES_R_ERROR; + goto out; + } + + if (esd_cion_get_uuid_with_generate(app_id, &uuid) == 0) { + param = g_variant_new("(is)", result, uuid); + free(uuid); + } else { + result = ES_R_ERROR; + } + +out: + if (param == NULL) + param = g_variant_new("(is)", result, ""); + + g_dbus_method_invocation_return_value(invocation, param); +} + static void handle_method_call(GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, @@ -1806,8 +1868,10 @@ static void handle_method_call(GDBusConnection *connection, keep_last_data_method_call(connection, sender, parameters, invocation); } else if (g_strcmp0(method_name, "CheckLastData") == 0) { check_last_data_method_call(connection, sender, parameters, invocation); - } else if (g_strcmp0(method_name, "LaunchOnEventFromUserEvent") == 0) { + } else if (g_strcmp0(method_name, "LaunchOnEventFromUserEvent") == 0) { launch_on_event_from_userevent(connection, sender, parameters, invocation); + } else if (g_strcmp0(method_name, "GetUuid") == 0) { + get_uuid_method_call(connection, sender, parameters, invocation); } } @@ -1875,8 +1939,6 @@ static void __esd_on_name_acquired(GDBusConnection *connection, { bundle *b; - _I("name acquired(%s)", name); - __esd_check_trusted_events(connection, "ListNames"); __esd_check_trusted_events(connection, "ListActivatableNames"); @@ -1897,7 +1959,6 @@ static void __esd_on_name_acquired(GDBusConnection *connection, static void __esd_on_name_lost(GDBusConnection *connection, const gchar *name, gpointer user_data) { - _E("name lost(%s)", name); } static int __esd_before_loop(void) @@ -2417,6 +2478,9 @@ int main(int argc, char *argv[]) return ES_R_ERROR; } + if (esd_cion_db_init() < 0) + _E("db init failed!"); + g_main_loop_run(mainloop); _E("shutdown"); -- 2.7.4 From 5251d11ed350eb6245867f2882f7ad6a38f16565 Mon Sep 17 00:00:00 2001 From: "SukHyung, Kang" Date: Thu, 8 Jul 2021 17:27:20 +0900 Subject: [PATCH 09/16] Add display name for cion db Change-Id: I5fb7078dc6268b20acdf4778aa13c238fac4d6a7 Signed-off-by: SukHyung, Kang --- CMakeLists.txt | 3 +- include/eventsystem_daemon.h | 10 + src/{ => esd_cion}/esd_cion.c | 0 src/esd_cion/esd_cion_db.c | 456 ++++++++++++++++++++++++++++++++++++++++++ src/esd_cion_db.c | 206 ------------------- src/esd_main.c | 158 ++++++++++++++- 6 files changed, 624 insertions(+), 209 deletions(-) rename src/{ => esd_cion}/esd_cion.c (100%) create mode 100644 src/esd_cion/esd_cion_db.c delete mode 100644 src/esd_cion_db.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 66e0a4e..cbdc5cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) PROJECT(esd C) AUX_SOURCE_DIRECTORY(src/ SRCS) +AUX_SOURCE_DIRECTORY(src/esd_cion/ CION_SRCS) SET(VERSION 0.0.1) SET(VERSION_MAJOR 0) @@ -53,7 +54,7 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed") ##build eventsystem daemon -add_executable(esd ${SRCS}) +add_executable(esd ${SRCS} ${CION_SRCS}) TARGET_LINK_LIBRARIES(esd eventsystem pkgmgr-client ${pkgs_LDFLAGS}) SET_TARGET_PROPERTIES(esd PROPERTIES COMPILE_FLAGS ${CFLAGS} "-fPIE") SET_TARGET_PROPERTIES(esd PROPERTIES LINK_FLAGS "-pie -Wl,-z,relro") diff --git a/include/eventsystem_daemon.h b/include/eventsystem_daemon.h index 5f600ce..879a62e 100644 --- a/include/eventsystem_daemon.h +++ b/include/eventsystem_daemon.h @@ -54,6 +54,16 @@ void __esd_cion_finalize(void); int esd_cion_db_init(void); int esd_cion_get_uuid_with_generate(const char* appid, char** uuid); +int esd_cion_set_display_name(const char *appid, const char *service_name, + char *display_name); +int esd_cion_get_display_name(const char *appid, const char *service_name, + char **display_name); +int esd_cion_set_enabled(const char *appid, const char *service_name, + bool enabled); +int esd_cion_get_enabled(const char *appid, const char *service_name, + int *enabled); +int esd_cion_get_enabled_service_list(const char *service_name, + const char *display_name, GList **list); #ifdef __cplusplus } diff --git a/src/esd_cion.c b/src/esd_cion/esd_cion.c similarity index 100% rename from src/esd_cion.c rename to src/esd_cion/esd_cion.c diff --git a/src/esd_cion/esd_cion_db.c b/src/esd_cion/esd_cion_db.c new file mode 100644 index 0000000..2cf7fd9 --- /dev/null +++ b/src/esd_cion/esd_cion_db.c @@ -0,0 +1,456 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "eventsystem_daemon.h" + +#define DBPATH tzplatform_mkpath(TZ_SYS_DB, ".cion.db") + +#define CREATE_CION_TABLE " \ +PRAGMA user_version = 50; \ +PRAGMA journal_mode = PERSIST; \ +PRAGMA foreign_keys = ON; \ +BEGIN EXCLUSIVE TRANSACTION; \ +CREATE TABLE IF NOT EXISTS cion_uuid ( \ + appid TEXT NOT NULL, \ + uuid TEXT NOT NULL, \ + PRIMARY KEY(appid) \ +); \ +CREATE TABLE IF NOT EXISTS cion_display_name ( \ + service_name TEXT NOT NULL, \ + appid TEXT NOT NULL, \ + display_name TEXT NULL, \ + enabled INTEGER DEFAULT 0, \ + PRIMARY KEY(service_name, appid) , \ + FOREIGN KEY(appid) REFERENCES cion_uuid (appid) ON DELETE CASCADE \ +); \ +COMMIT TRANSACTION; " + +static int __check_table_exist(sqlite3 *db) { + int ret; + const char *val; + sqlite3_stmt *stmt = NULL; + const char query[] = + "SELECT name FROM sqlite_master WHERE type='table'" + " ORDER BY name ASC"; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _E("prepare error: %s", sqlite3_errmsg(db)); + ret = -1; + goto out; + } + + ret = sqlite3_step(stmt); + if (ret != SQLITE_ROW) { + _E("fail to get row"); + ret = -1; + goto out; + } + + val = (const char*)sqlite3_column_text(stmt, 0); + if (val == NULL) { + _E("name is NULL"); + ret = -1; + goto out; + } + + if (strcmp("cion", val) != 0) { + ret = -1; + goto out; + } + + ret = 0; + +out: + sqlite3_finalize(stmt); + + return ret; +} + +static int __create_table(sqlite3 *db) { + int ret; + char *errmsg = NULL; + + ret = sqlite3_exec(db, CREATE_CION_TABLE, + NULL, NULL, &errmsg); + if (ret != SQLITE_OK) { + _E("create table fail : %s", errmsg); + sqlite3_free(errmsg); + return -1; + } + + return 0; +} + +int esd_cion_db_init(void) { + sqlite3 *db = NULL; + + if (sqlite3_open_v2(DBPATH, &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) { + sqlite3_close_v2(db); + unlink(DBPATH); + + if (sqlite3_open_v2(DBPATH, &db, + SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, + NULL) != SQLITE_OK) { + _E("Fail to create db"); + unlink(DBPATH); + return -1; + } + } + + if (__check_table_exist(db) < 0) { + if (__create_table(db) < 0) { + sqlite3_close_v2(db); + _E("Fail to create table"); + return -1; + } + } + + sqlite3_close_v2(db); + + return 0; +} + +sqlite3 *esd_cion_db_open() { + sqlite3 *db; + + if (access(DBPATH, R_OK | W_OK) != 0) + return NULL; + + if (sqlite3_open_v2(DBPATH, &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) + return NULL; + + return db; +} + +int esd_cion_db_close(sqlite3 **db) { + if (db == NULL || *db == NULL) + return 0; + + if (sqlite3_close(*db) != SQLITE_OK) { + _E("Failed to close db"); + return 0; + } + + *db = NULL; + + return 0; +} + +static const char *__esd_cion_generate_uuid() { + uuid_t uu; + char _uuid[37]; + char *uuid; + + uuid_generate_random(uu); + uuid_unparse(uu, _uuid); + + uuid = strdup(_uuid); + + return uuid; +} + +static int __esd_cion_get_uuid(sqlite3 *db, const char *appid, char **uuid) { + int ret = -1; + char *query; + sqlite3_stmt *stmt; + + query = sqlite3_mprintf("SELECT uuid FROM cion_uuid " + "WHERE appid = %Q", appid); + if (query == NULL) + goto out; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) + goto out; + + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + ret = 0; + *uuid = strdup((char*)sqlite3_column_text(stmt, 0)); + } + +out: + sqlite3_free(query); + sqlite3_finalize(stmt); + + return ret; +} + +static int __esd_cion_set_uuid(sqlite3 *db, const char *appid, const char *uuid) { + int ret = -1; + char *query; + sqlite3_stmt *stmt; + + query = sqlite3_mprintf("INSERT INTO cion_uuid (appid, uuid) " + "VALUES (%Q, %Q) ", appid, uuid); + if (query == NULL) + goto out; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) + goto out; + + ret = sqlite3_step(stmt); + if (ret == SQLITE_OK || ret == SQLITE_DONE) + ret = 0; + +out: + sqlite3_free(query); + sqlite3_finalize(stmt); + + return ret; +} + +int esd_cion_get_uuid_with_generate(const char *appid, char **uuid) { + int ret = 0; + sqlite3 *db; + const char *_uuid; + + db = esd_cion_db_open(); + if (!db) { + _E("db open fail"); + return -1; + } + + if (__esd_cion_get_uuid(db, appid, uuid) != 0) { + _uuid = __esd_cion_generate_uuid(); + ret = __esd_cion_set_uuid(db, appid, _uuid); + if (ret == 0) + *uuid = (char*)_uuid; + else + free((char*)_uuid); + + _E("get uuid generate"); + } + + esd_cion_db_close(&db); + + return ret; +} + +int esd_cion_set_display_name(const char *appid, const char *service_name, + char *display_name) { + int ret = -1; + sqlite3 *db; + char *query = NULL; + sqlite3_stmt *stmt = NULL; + char *_uuid; + + db = esd_cion_db_open(); + if (!db) { + _E("db open fail"); + return -1; + } + + if (__esd_cion_get_uuid(db, appid, &_uuid) == 0) { + free(_uuid); + } else { + _uuid = (char*)__esd_cion_generate_uuid(); + ret = __esd_cion_set_uuid(db, appid, _uuid); + if (ret != 0) { + free(_uuid); + goto out; + } + _E("set uuid generate"); + } + + query = sqlite3_mprintf("INSERT INTO cion_display_name " + "(service_name, appid, display_name) " + "VALUES (%Q, %Q, %Q) ON CONFLICT(service_name, appid)" + "DO UPDATE SET display_name = %Q " + "WHERE service_name = %Q AND appid = %Q ", service_name, appid, display_name, + display_name, service_name, appid); + if (query == NULL) + goto out; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) + goto out; + + ret = sqlite3_step(stmt); + _E("set name step: %d", ret); + + if (ret == SQLITE_OK || ret == SQLITE_DONE) + ret = 0; + +out: + sqlite3_free(query); + sqlite3_finalize(stmt); + esd_cion_db_close(&db); + + return ret; +} + +int esd_cion_get_display_name(const char *appid, const char *service_name, + char **display_name) { + int ret = -1; + sqlite3 *db; + char *query = NULL; + sqlite3_stmt *stmt = NULL; + char *_name = NULL; + + db = esd_cion_db_open(); + if (!db) { + _E("db open fail"); + return -1; + } + + query = sqlite3_mprintf("SELECT display_name FROM cion_display_name " + "WHERE appid = %Q AND service_name = %Q", appid, service_name); + if (query == NULL) + goto out; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) + goto out; + + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + ret = 0; + + _name = (char*)sqlite3_column_text(stmt, 0); + + if (_name != NULL) + *display_name = strdup(_name); + else + _E("get display name null"); + + } else if (ret == SQLITE_DONE){ + _E("get display name not exist"); + ret = 0; + } + +out: + sqlite3_free(query); + sqlite3_finalize(stmt); + esd_cion_db_close(&db); + + return ret; +} + +int esd_cion_set_enabled(const char *appid, const char *service_name, + bool enabled) { + int ret = -1; + sqlite3 *db; + char *query = NULL; + sqlite3_stmt *stmt = NULL; + + db = esd_cion_db_open(); + if (!db) { + _E("db open fail"); + return -1; + } + + query = sqlite3_mprintf("UPDATE cion_display_name SET enabled = %i " + "WHERE appid = %Q AND service_name = %Q", enabled? 1 : 0 , + appid, service_name); + if (query == NULL) + goto out; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) + goto out; + + ret = sqlite3_step(stmt); + if (ret == SQLITE_OK || ret == SQLITE_DONE) + ret = 0; + +out: + sqlite3_free(query); + sqlite3_finalize(stmt); + esd_cion_db_close(&db); + + return ret; +} + +int esd_cion_get_enabled(const char *appid, const char *service_name, + int *enabled) { + int ret = -1; + sqlite3 *db; + char *query = NULL; + sqlite3_stmt *stmt = NULL; + + db = esd_cion_db_open(); + if (!db) { + _E("db open fail"); + return -1; + } + + query = sqlite3_mprintf("SELECT enabled FROM cion_display_name " + "WHERE appid = %Q AND service_name = %Q", appid, service_name); + if (query == NULL) + goto out; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) + goto out; + + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + ret = 0; + + *enabled = sqlite3_column_int(stmt, 0); + } else if (ret == SQLITE_DONE){ + _E("get enabled not exist"); + ret = 0; + } + +out: + sqlite3_free(query); + sqlite3_finalize(stmt); + esd_cion_db_close(&db); + + return ret; +} + +int esd_cion_get_enabled_service_list(const char *service_name, + const char *display_name, GList **list) +{ + int ret = -1; + sqlite3 *db; + char *query = NULL; + sqlite3_stmt *stmt = NULL; + char *appid; + + db = esd_cion_db_open(); + if (!db) { + _E("db open fail"); + return -1; + } + + if (display_name) + query = sqlite3_mprintf("SELECT appid FROM cion_display_name " + "WHERE service_name = %Q AND display_name = %Q AND enabled = 1", + service_name, display_name); + else + query = sqlite3_mprintf("SELECT appid FROM cion_display_name " + "WHERE service_name = %Q AND enabled = 1", service_name); + if (query == NULL) + goto out; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) + goto out; + + while(sqlite3_step(stmt) == SQLITE_ROW) { + appid = strdup((char*)sqlite3_column_text(stmt, 0)); + _E("get appid list : %s", appid); + + *list = g_list_append(*list, appid); + } + +out: + sqlite3_free(query); + sqlite3_finalize(stmt); + esd_cion_db_close(&db); + + return ret; +} diff --git a/src/esd_cion_db.c b/src/esd_cion_db.c deleted file mode 100644 index 2c34c62..0000000 --- a/src/esd_cion_db.c +++ /dev/null @@ -1,206 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "eventsystem_daemon.h" - -#define DBPATH tzplatform_mkpath(TZ_SYS_DB, ".cion.db") - -#define CREATE_CION_TABLE " \ -PRAGMA user_version = 50; \ -PRAGMA journal_mode = PERSIST; \ -PRAGMA foreign_keys = ON; \ -BEGIN EXCLUSIVE TRANSACTION; \ -CREATE TABLE IF NOT EXISTS cion_info ( \ - appid TEXT NOT NULL, \ - uuid TEXT NOT NULL, \ - PRIMARY KEY (appid) \ -); \ -COMMIT TRANSACTION; " - -static int __check_table_exist(sqlite3 *db) { - int ret; - const char *val; - sqlite3_stmt *stmt = NULL; - const char query[] = - "SELECT name FROM sqlite_master WHERE type='table'" - " ORDER BY name ASC"; - - ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); - if (ret != SQLITE_OK) { - _E("prepare error: %s", sqlite3_errmsg(db)); - ret = -1; - goto out; - } - - ret = sqlite3_step(stmt); - if (ret != SQLITE_ROW) { - _E("fail to get row"); - ret = -1; - goto out; - } - - val = (const char*)sqlite3_column_text(stmt, 0); - if (val == NULL) { - _E("name is NULL"); - ret = -1; - goto out; - } - - if (strcmp("cion", val) != 0) { - ret = -1; - goto out; - } - - ret = 0; - -out: - sqlite3_finalize(stmt); - - return ret; -} - -static int __create_table(sqlite3 *db) { - int ret; - char *errmsg = NULL; - - ret = sqlite3_exec(db, CREATE_CION_TABLE, - NULL, NULL, &errmsg); - if (ret != SQLITE_OK) { - _E("create table fail : %s", errmsg); - sqlite3_free(errmsg); - return -1; - } - - return 0; -} - -int esd_cion_db_init(void) { - sqlite3 *db = NULL; - - if (sqlite3_open_v2(DBPATH, &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) { - sqlite3_close_v2(db); - unlink(DBPATH); - - if (sqlite3_open_v2(DBPATH, &db, - SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, - NULL) != SQLITE_OK) { - _E("Fail to create db"); - unlink(DBPATH); - return -1; - } - } - - if (__check_table_exist(db) < 0) { - if (__create_table(db) < 0) { - sqlite3_close_v2(db); - _E("Fail to create table"); - return -1; - } - } - - sqlite3_close_v2(db); - - return 0; -} - -sqlite3 *esd_cion_db_open() { - sqlite3 *db; - - if (access(DBPATH, R_OK | W_OK) != 0) - return NULL; - - if (sqlite3_open_v2(DBPATH, &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) - return NULL; - - return db; -} - -int esd_cion_db_close(sqlite3 **db) { - if (db == NULL || *db == NULL) - return 0; - - if (sqlite3_close(*db) != SQLITE_OK) { - _E("Failed to close db"); - return 0; - } - - *db = NULL; - - return 0; -} - -static const char *__esd_cion_generate_uuid() { - uuid_t uu; - char _uuid[37]; - char *uuid; - - uuid_generate_random(uu); - uuid_unparse(uu, _uuid); - - uuid = strdup(_uuid); - - return uuid; -} - -int esd_cion_get_uuid_with_generate(const char *appid, char **uuid) { - int ret; - sqlite3 *db; - char *query = NULL; - sqlite3_stmt *stmt = NULL; - const char *_uuid; - - db = esd_cion_db_open(); - if (!db) { - _E("db open fail"); - return -1; - } - - query = sqlite3_mprintf("SELECT uuid FROM cion_info " - "WHERE appid = %Q", appid); - - ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); - if (ret != SQLITE_OK) { - ret = -1; - goto out; - } - - ret = sqlite3_step(stmt); - if (ret == SQLITE_ROW) { - ret = 0; - *uuid = strdup((char*)sqlite3_column_text(stmt, 0)); - } else { - sqlite3_free(query); - sqlite3_finalize(stmt); - stmt = NULL; - - _uuid = __esd_cion_generate_uuid(); - query = sqlite3_mprintf("INSERT INTO cion_info (appid, uuid) " - "VALUES (%Q, %Q) ", appid, _uuid); - - ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); - if (ret != SQLITE_OK) { - ret = -1; - goto out; - } - - ret = sqlite3_step(stmt); - if (ret == SQLITE_OK || ret == SQLITE_DONE) { - ret = 0; - *uuid = strdup(_uuid); - } else { - ret = -1; - } - } - -out: - sqlite3_free(query); - sqlite3_finalize(stmt); - esd_cion_db_close(&db); - - return ret; -} diff --git a/src/esd_main.c b/src/esd_main.c index 9d4480e..dabb5ce 100644 --- a/src/esd_main.c +++ b/src/esd_main.c @@ -1246,10 +1246,30 @@ static const gchar introspection_xml[] = " " " " " " -" " +" " " " " " " " +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " +" " " " ""; @@ -1844,6 +1864,132 @@ out: g_dbus_method_invocation_return_value(invocation, param); } +static void set_display_name_method_call(GDBusConnection *connection, + const gchar *sender, GVariant *parameters, + GDBusMethodInvocation *invocation) +{ + GVariant *param = NULL; + int result = ES_R_OK; + char app_id[128] = { 0, }; + int sender_pid; + char *display_name = NULL; + char *service_name = NULL; + + sender_pid = __get_sender_pid(connection, sender); + if (__esd_get_appid_by_pid(sender_pid, app_id, sizeof(app_id)) < 0) { + _E("failed to get appid by pid"); + result = ES_R_ERROR; + goto out; + } + + g_variant_get(parameters, "(&s&s)", &service_name, &display_name); + + if (esd_cion_set_display_name(app_id, service_name, display_name) != 0) + result = ES_R_ERROR; + +out: + param = g_variant_new("(i)", result); + g_dbus_method_invocation_return_value(invocation, param); +} + +static void get_display_name_method_call(GDBusConnection *connection, + const gchar *sender, GVariant *parameters, + GDBusMethodInvocation *invocation) +{ + GVariant *param = NULL; + int result = ES_R_OK; + char app_id[128] = { 0, }; + int sender_pid; + char *service_name = NULL; + char *display_name = NULL; + + sender_pid = __get_sender_pid(connection, sender); + if (__esd_get_appid_by_pid(sender_pid, app_id, sizeof(app_id)) < 0) { + _E("failed to get appid by pid"); + result = ES_R_ERROR; + goto out; + } + + g_variant_get(parameters, "(&s)", &service_name); + + if (esd_cion_get_display_name(app_id, service_name, &display_name) == 0) { + if (display_name != NULL) { + param = g_variant_new("(is)", result, display_name); + free(display_name); + } else { + goto out; + } + } else { + result = ES_R_ERROR; + } + +out: + if (param == NULL) + param = g_variant_new("(is)", result, ""); + + g_dbus_method_invocation_return_value(invocation, param); +} + +static void set_enabled_method_call(GDBusConnection *connection, + const gchar *sender, GVariant *parameters, + GDBusMethodInvocation *invocation) +{ + GVariant *param = NULL; + int result = ES_R_OK; + char app_id[128] = { 0, }; + int sender_pid; + gboolean enabled; + char *service_name = NULL; + + sender_pid = __get_sender_pid(connection, sender); + if (__esd_get_appid_by_pid(sender_pid, app_id, sizeof(app_id)) < 0) { + _E("failed to get appid by pid"); + result = ES_R_ERROR; + goto out; + } + + g_variant_get(parameters, "(&sb)", &service_name, &enabled); + + if (esd_cion_set_enabled(app_id, service_name, (bool)enabled) != 0) + result = ES_R_ERROR; + +out: + param = g_variant_new("(i)", result); + g_dbus_method_invocation_return_value(invocation, param); +} + +static void get_enabled_method_call(GDBusConnection *connection, + const gchar *sender, GVariant *parameters, + GDBusMethodInvocation *invocation) +{ + GVariant *param = NULL; + int result = ES_R_OK; + char app_id[128] = { 0, }; + int sender_pid; + char *service_name = NULL; + int enabled; + + sender_pid = __get_sender_pid(connection, sender); + if (__esd_get_appid_by_pid(sender_pid, app_id, sizeof(app_id)) < 0) { + _E("failed to get appid by pid"); + result = ES_R_ERROR; + goto out; + } + + g_variant_get(parameters, "(&s)", &service_name); + + if (esd_cion_get_enabled(app_id, service_name, &enabled) == 0) + param = g_variant_new("(ib)", result, (bool)enabled); + else + result = ES_R_ERROR; + +out: + if (param == NULL) + param = g_variant_new("(ib)", result, false); + + g_dbus_method_invocation_return_value(invocation, param); +} + static void handle_method_call(GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, @@ -1870,8 +2016,16 @@ static void handle_method_call(GDBusConnection *connection, check_last_data_method_call(connection, sender, parameters, invocation); } else if (g_strcmp0(method_name, "LaunchOnEventFromUserEvent") == 0) { launch_on_event_from_userevent(connection, sender, parameters, invocation); - } else if (g_strcmp0(method_name, "GetUuid") == 0) { + } else if (g_strcmp0(method_name, "CionGetUuid") == 0) { get_uuid_method_call(connection, sender, parameters, invocation); + } else if (g_strcmp0(method_name, "CionSetDisplayName") == 0) { + set_display_name_method_call(connection, sender, parameters, invocation); + } else if (g_strcmp0(method_name, "CionGetDisplayName") == 0) { + get_display_name_method_call(connection, sender, parameters, invocation); + } else if (g_strcmp0(method_name, "CionSetEnabled") == 0) { + set_enabled_method_call(connection, sender, parameters, invocation); + } else if (g_strcmp0(method_name, "CionGetEnabled") == 0) { + get_enabled_method_call(connection, sender, parameters, invocation); } } -- 2.7.4 From a141d3e9fdf5db7af96ac46a54486d719878e2f7 Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Thu, 19 Aug 2021 14:07:08 +0900 Subject: [PATCH 10/16] Add Cion Server - Discovery ondemand list for cion - Ondemand Launch Application for cion Related patch : https://review.tizen.org/gerrit/c/platform/core/appfw/cion/+/261308 https://review.tizen.org/gerrit/c/platform/core/appfw/cion/+/262199 Change-Id: I71ae650acf99c7cacef2b3e0bfbacb2bfe8b45d6 Signed-off-by: Inkyun Kil --- CMakeLists.txt | 8 +- include/eventsystem_daemon.h | 19 +- packaging/esd.spec | 3 + src/esd_cion/cion_ondemand_server.cc | 366 +++++++++++++++++++++++++++++++++++ src/esd_cion/cion_ondemand_server.h | 62 ++++++ src/esd_cion/cion_peer_info.cc | 35 ++++ src/esd_cion/cion_peer_info.h | 36 ++++ src/esd_cion/esd_cion.c | 316 ------------------------------ src/esd_cion/esd_cion.cc | 62 ++++++ src/esd_cion/esd_cion_db.c | 51 +++-- src/esd_main.c | 3 +- 11 files changed, 623 insertions(+), 338 deletions(-) create mode 100644 src/esd_cion/cion_ondemand_server.cc create mode 100644 src/esd_cion/cion_ondemand_server.h create mode 100644 src/esd_cion/cion_peer_info.cc create mode 100644 src/esd_cion/cion_peer_info.h delete mode 100644 src/esd_cion/esd_cion.c create mode 100644 src/esd_cion/esd_cion.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index cbdc5cb..d207d7c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -PROJECT(esd C) +PROJECT(esd C CXX) AUX_SOURCE_DIRECTORY(src/ SRCS) AUX_SOURCE_DIRECTORY(src/esd_cion/ CION_SRCS) @@ -23,6 +23,7 @@ INCLUDE(FindPkgConfig) pkg_check_modules(pkgs REQUIRED dlog bundle + parcel pkgmgr-info glib-2.0 gio-2.0 @@ -37,6 +38,8 @@ pkg_check_modules(pkgs REQUIRED security-manager sqlite3 uuid + cion + capi-system-info ) FOREACH(flag ${pkgs_CFLAGS}) @@ -48,6 +51,7 @@ ENDFOREACH(flag) ## Additional flag SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -fvisibility=hidden") SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g -Wall -Werror") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EXTRA_CFLAGS} -std=c++14 -Werror") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") ## Linker flags @@ -56,7 +60,7 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed") ##build eventsystem daemon add_executable(esd ${SRCS} ${CION_SRCS}) TARGET_LINK_LIBRARIES(esd eventsystem pkgmgr-client ${pkgs_LDFLAGS}) -SET_TARGET_PROPERTIES(esd PROPERTIES COMPILE_FLAGS ${CFLAGS} "-fPIE") +SET_TARGET_PROPERTIES(esd PROPERTIES COMPILE_FLAGS ${CFLAGS} ${CXXFLAGS} "-fPIE") SET_TARGET_PROPERTIES(esd PROPERTIES LINK_FLAGS "-pie -Wl,-z,relro") # pkgconfig file diff --git a/include/eventsystem_daemon.h b/include/eventsystem_daemon.h index 879a62e..706a89b 100644 --- a/include/eventsystem_daemon.h +++ b/include/eventsystem_daemon.h @@ -25,6 +25,9 @@ extern "C" { #endif +#include +#include + #undef LOG_TAG #define LOG_TAG "ESD" @@ -48,9 +51,18 @@ extern "C" { #define SYSTEMD_DBUS_SIGNAL_STARTUP_FINISHED "StartupFinished" #define SYSTEMD_DBUS_SIGNAL_USER_STARTUP_FINISHED "UserSessionStartupFinished" +typedef struct cion_service_info { + char *service_name; + char *appid; + char *display_name; +} cion_service_info_s; + int __esd_register_vconf_callbacks(void); -int __esd_cion_init(void); -void __esd_cion_finalize(void); + +int _esd_cion_init(void); +void _esd_cion_adds_enabled_app(const char *service_name, const char *app_id, + const char *display_name); +void _esd_cion_removes_enabled_app(const char *service_name, const char *app_id); int esd_cion_db_init(void); int esd_cion_get_uuid_with_generate(const char* appid, char** uuid); @@ -62,8 +74,7 @@ int esd_cion_set_enabled(const char *appid, const char *service_name, bool enabled); int esd_cion_get_enabled(const char *appid, const char *service_name, int *enabled); -int esd_cion_get_enabled_service_list(const char *service_name, - const char *display_name, GList **list); +int esd_cion_get_enabled_service_list(GList **list); #ifdef __cplusplus } diff --git a/packaging/esd.spec b/packaging/esd.spec index 6d7c3c5..4bcc907 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -9,6 +9,7 @@ Source1: esd.service BuildRequires: cmake BuildRequires: pkgconfig(aul) BuildRequires: pkgconfig(bundle) +BuildRequires: pkgconfig(parcel) BuildRequires: pkgconfig(dlog) BuildRequires: pkgconfig(pkgmgr-info) BuildRequires: pkgconfig(appsvc) @@ -26,6 +27,8 @@ BuildRequires: pkgconfig(cynara-session) BuildRequires: pkgconfig(security-manager) BuildRequires: pkgconfig(uuid) BuildRequires: pkgconfig(sqlite3) +BuildRequires: pkgconfig(cion) +BuildRequires: pkgconfig(capi-system-info) Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig diff --git a/src/esd_cion/cion_ondemand_server.cc b/src/esd_cion/cion_ondemand_server.cc new file mode 100644 index 0000000..0d5965e --- /dev/null +++ b/src/esd_cion/cion_ondemand_server.cc @@ -0,0 +1,366 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "eventsystem_daemon.h" +#include "cion_ondemand_server.h" +#include "cion_peer_info.h" + + +namespace { + +std::string GetAppVersion(const char* appid) { + char *pkgid; + char *pkg_version = NULL; + pkgmgrinfo_appinfo_h appinfo = NULL; + + int retval = pkgmgrinfo_appinfo_get_usr_appinfo(appid, getuid(), &appinfo); + if (retval != PMINFO_R_OK) + return {}; + + retval = pkgmgrinfo_appinfo_get_pkgid(appinfo, &pkgid); + if (retval != PMINFO_R_OK) { + pkgmgrinfo_appinfo_destroy_appinfo(appinfo); + return {}; + } + + pkgmgrinfo_pkginfo_h pkginfo = NULL; + retval = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &pkginfo); + if (retval != PMINFO_R_OK) { + pkgmgrinfo_appinfo_destroy_appinfo(appinfo); + return {}; + } + + retval = pkgmgrinfo_pkginfo_get_version(pkginfo, &pkg_version); + if (retval != PMINFO_R_OK) { + pkgmgrinfo_appinfo_destroy_appinfo(appinfo); + pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo); + return {}; + } + + std::string version(pkg_version); + pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo); + pkgmgrinfo_appinfo_destroy_appinfo(appinfo); + + return version; +} + +std::string GetSystemInfoPlatformString(const char* key) { + char* val = nullptr; + int ret = system_info_get_platform_string(key, &val); + if (ret != SYSTEM_INFO_ERROR_NONE || !val) + return {}; + + std::string val_str = val; + free(val); + + return val_str; +} + +std::string GetVconfString(const char* key) { + char* val = vconf_get_str(key); + if (val == nullptr) + return {}; + + std::string val_str = val; + free(val); + + return val_str; +} + +void FreeList(gpointer data) { + cion_service_info_s *info = (cion_service_info_s *)data; + + free(info->appid); + free(info->service_name); + if (info->display_name) + free(info->display_name); + free(info); +} + +std::string device_id = GetSystemInfoPlatformString("http://tizen.org/system/tizenid"); +std::string device_name = GetVconfString(VCONFKEY_SETAPPL_DEVICE_NAME_STR); +std::string device_platform = "Tizen"; +std::string device_platform_version = + GetSystemInfoPlatformString("http://tizen.org/feature/platform.version"); +std::string device_type = GetSystemInfoPlatformString("http://tizen.org/system/device_type"); + +} // namespace + +CionOndemandServer::CionOndemandServer(std::string service_name, + std::string display_name) + : cion::channel::ServerChannel(service_name, display_name) { + LoadOndemandServiceList(); +} + +CionOndemandServer::CionOndemandServer(std::string service_name, + std::string display_name, cion::SecurityInfo security) : + cion::channel::ServerChannel(service_name, display_name, + std::move(security)) { + LoadOndemandServiceList(); +} + +void CionOndemandServer::OnConnectionResult(std::shared_ptr info, + const cion::ConnectionResult& result) { +} + +void CionOndemandServer::OnDisconnected(std::shared_ptr peer) { +} + +std::vector CionOndemandServer::OnDataReceived( + const std::vector& data, std::shared_ptr peer) { + std::string return_data("returned"); + + std::vector v(return_data.begin(), return_data.end()); + return v; +} + +void CionOndemandServer::OnPayloadReceived(std::shared_ptr data, + std::shared_ptr peer, + IPayloadReceiver::PayloadTransferStatus status) { +} + +void CionOndemandServer::OnConnectionRequest( + std::shared_ptr peer) { +} + +void CionOndemandServer::OnOndemandListRequested( + std::shared_ptr data, + std::shared_ptr peer) { + std::vector raw; + + _D("[Request Ondemand List]"); + + if (data->GetType() == cion::IPayload::PayloadType::File) + return; + + std::shared_ptr data_payload = + std::dynamic_pointer_cast(data); + std::vector requested_data = data_payload->GetData(); + tizen_base::Parcel ondemand_parcel(requested_data.data(), + requested_data.size()); + + std::string list_header = ondemand_parcel.ReadString(); + std::string list_service_name = ondemand_parcel.ReadString(); + _D("Ondemand Service_name : %s", list_service_name.c_str()); + if (list_header == std::string("OndemandList")) { + raw = GetOndemandList(list_service_name).GetRaw(); + if (raw.size() == 0) + return; + } + + auto dpl = std::make_unique(); + std::vector listdata_vector(raw.begin(), raw.end()); + dpl->SetData(listdata_vector); + + SendPayloadAsync(dpl.get(), peer, [] ( + std::shared_ptr result) { + _I("result received !!! %s", result->GetPayloadID().c_str()); + }); +} + +int CionOndemandServer::OnOndemandLaunchRequested( + std::shared_ptr data, + std::shared_ptr peer) { + //TODO check privilege app_id + std::string app_id = peer->GetAppID(); + int ret = -20; + + cion::IPayload::PayloadType type = data->GetType(); + if (type == cion::IPayload::PayloadType::File) + return ret; + + std::shared_ptr data_payload = + std::dynamic_pointer_cast(data); + + std::vector getdata = data_payload->GetData(); + std::string launch_requested(getdata.begin(), getdata.end()); + + ret = OndemandLaunchApp(launch_requested); + if (ret != 0) { + _E("Faled to __esd_cion_launch_ondemand : %d", ret); + } + + return ret; +} + +int CionOndemandServer::OndemandLaunchApp(std::string appid) { + uid_t uid = getuid(); + tizen_base::Bundle b; + int ret = -20; + bool found = false; + + for (std::shared_ptr cs : ondemand_peer_list_) { + if (cs->GetAppID() == appid) { + found = true; + break; + } + } + + if (found == false) { + _E("%s is not found", appid.c_str()); + return ret; + } + + _D("cion launch ondemand: app_id(%s)", appid.c_str()); + + if (!aul_app_is_running_for_uid(appid.c_str(), uid)) { + aul_svc_set_operation(b.GetHandle(), AUL_SVC_OPERATION_DEFAULT); + aul_svc_set_appid(b.GetHandle(), appid.c_str()); + + ret = aul_svc_run_service_async_for_uid(b.GetHandle(), 0, NULL, NULL, uid); + if (ret < 0) + _E("Failed to launch app : %s", appid.c_str()); + + } else { + _D("already is running or launch failed"); + } + + return ret; +} + +void CionOndemandServer::LoadOndemandServiceList() { + GList *list = nullptr; + int ret = esd_cion_get_enabled_service_list(&list); + if (ret != 0 || list == nullptr) { + _E("Get list error"); + } else { + for (GList *it = g_list_first(list); it; it = g_list_next(it)) { + cion_service_info *cion_info = (cion_service_info_s *)(it->data); + char *app_id = cion_info->appid; + char *service_name = cion_info->service_name; + char *display_name = cion_info->display_name; + char *uuid; + ret = esd_cion_get_uuid_with_generate(app_id, &uuid); + if (ret != 0) { + _E("Get uuid for %s", app_id); + } + + std::string app_version = GetAppVersion(app_id); + + tizen_base::Parcel parcel; + parcel.WriteString(device_id); + parcel.WriteString(device_name); + parcel.WriteString(device_platform); + parcel.WriteString(device_platform_version); + parcel.WriteString(device_type); + parcel.WriteString(std::string(app_id)); + parcel.WriteString(app_version); + parcel.WriteString(std::string(uuid)); + parcel.WriteString(std::string(display_name)); + + std::shared_ptr pi = + std::make_shared(std::string(service_name), + parcel.GetRaw().data(), parcel.GetRaw().size()); + ondemand_peer_list_.emplace_back(pi); + + free(uuid); + } + } + + g_list_free_full(list, FreeList); +} + +void CionOndemandServer::AddOndemandServiceList(std::string service_name, + std::string appid, std::string display_name) { + for (auto peer : ondemand_peer_list_) { + if (peer->GetServiceName() == service_name && + peer->GetAppID() == appid) { + _W("%s is already exist", appid.c_str()); + return; + } + } + + char *uuid; + int ret = esd_cion_get_uuid_with_generate(appid.c_str(), &uuid); + if (ret != 0) { + _E("Get uuid for %s", appid.c_str()); + return; + } + + std::string app_version = GetAppVersion(appid.c_str()); + + tizen_base::Parcel parcel; + parcel.WriteString(device_id); + parcel.WriteString(device_name); + parcel.WriteString(device_platform); + parcel.WriteString(device_platform_version); + parcel.WriteString(device_type); + parcel.WriteString(appid); + parcel.WriteString(app_version); + parcel.WriteString(std::string(uuid)); + parcel.WriteString(display_name); + + std::shared_ptr pi = + std::make_shared(service_name, parcel.GetRaw().data(), + parcel.GetRaw().size()); + ondemand_peer_list_.emplace_back(pi); + _D("[%s:%s] is added to list", appid.c_str(), service_name.c_str()); +} + +void CionOndemandServer::RemoveOndemandServiceList(std::string service_name, + std::string appid) { + for (auto peer = ondemand_peer_list_.begin(); + peer != ondemand_peer_list_.end(); peer++) { + if (peer->get()->GetServiceName() == service_name && + peer->get()->GetAppID() == appid) { + ondemand_peer_list_.erase(peer); + _D("[%s:%s] is removed from list", appid.c_str(), service_name.c_str()); + break; + } + } +} + +tizen_base::Parcel CionOndemandServer::GetOndemandList( + std::string service_name) { + if (ondemand_peer_list_.size() == 0) { + _W("ondemand peer list is empty"); + return {}; + } + + std::string header("ODL:"); + tizen_base::Parcel parcel; + parcel.WriteString(header); + + for (auto peer : ondemand_peer_list_) { + if (peer->GetServiceName() != service_name || + aul_app_is_running(peer->GetAppID().c_str())) + continue; + + parcel.WriteBool(true); + std::vector peer_raw = peer->Serialize(); + parcel.WriteUInt32(peer_raw.size()); + parcel.Write(peer_raw.data(), peer_raw.size()); + } + + parcel.WriteBool(false); + + return parcel; +} \ No newline at end of file diff --git a/src/esd_cion/cion_ondemand_server.h b/src/esd_cion/cion_ondemand_server.h new file mode 100644 index 0000000..b3852d0 --- /dev/null +++ b/src/esd_cion/cion_ondemand_server.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * + * 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_DAEMON_CION_ONDEMAND_SERVER_H_ +#define EVENTSYSTEM_DAEMON_CION_ONDEMAND_SERVER_H_ + +#include + +#include +#include +#include + +#include "cion_peer_info.h" + +class CionOndemandServer : public cion::channel::ServerChannel { + public: + explicit CionOndemandServer(std::string service_name, + std::string display_name); + explicit CionOndemandServer(std::string service_name, + std::string display_name, cion::SecurityInfo security); + + void AddOndemandServiceList(std::string service_name, std::string appid, + std::string display_name); + void RemoveOndemandServiceList(std::string service_name, std::string appid); + + protected: + void OnConnectionResult(std::shared_ptr info, + const cion::ConnectionResult& result) override; + void OnDisconnected(std::shared_ptr peer) override; + std::vector OnDataReceived(const std::vector& data, + std::shared_ptr peer) override; + void OnPayloadReceived(std::shared_ptr data, + std::shared_ptr peer, + IPayloadReceiver::PayloadTransferStatus status) override; + void OnConnectionRequest(std::shared_ptr peer) override; + void OnOndemandListRequested(std::shared_ptr data, + std::shared_ptr peer) override; + int OnOndemandLaunchRequested(std::shared_ptr data, + std::shared_ptr peer) override; + + int OndemandLaunchApp(std::string appid); + void LoadOndemandServiceList(); + tizen_base::Parcel GetOndemandList(std::string service_name); + + private: + std::list> ondemand_peer_list_; +}; + +#endif /* EVENTSYSTEM_DAEMON_CION_ONDEMAND_SERVER_H_ */ \ No newline at end of file diff --git a/src/esd_cion/cion_peer_info.cc b/src/esd_cion/cion_peer_info.cc new file mode 100644 index 0000000..33705d8 --- /dev/null +++ b/src/esd_cion/cion_peer_info.cc @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#include "cion_peer_info.h" + +CionPeerInfo::CionPeerInfo(std::string service_name) : cion::PeerInfo(), + service_name_(service_name) { +} + +CionPeerInfo::CionPeerInfo(std::string service_name, + const void* buf, uint32_t size) : cion::PeerInfo(buf, size), + service_name_(service_name){ + +} + +void CionPeerInfo::SetServiceName(std::string service_name) { + service_name_ = service_name; +} + +std::string CionPeerInfo::GetServiceName() const { + return service_name_; +} \ No newline at end of file diff --git a/src/esd_cion/cion_peer_info.h b/src/esd_cion/cion_peer_info.h new file mode 100644 index 0000000..acf14b0 --- /dev/null +++ b/src/esd_cion/cion_peer_info.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * + * 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_DAEMON_CION_PEER_INFO_H_ +#define EVENTSYSTEM_DAEMON_CION_PEER_INFO_H_ + +#include + +#include + +class CionPeerInfo : public cion::PeerInfo { + public: + CionPeerInfo(std::string service_name); + CionPeerInfo(std::string service_name, const void* buf, uint32_t size); + + std::string GetServiceName() const; + void SetServiceName(std::string service_name); + + private: + std::string service_name_; +}; + +#endif /* EVENTSYSTEM_DAEMON_CION_PEER_INFO_H_ */ \ No newline at end of file diff --git a/src/esd_cion/esd_cion.c b/src/esd_cion/esd_cion.c deleted file mode 100644 index 48447e0..0000000 --- a/src/esd_cion/esd_cion.c +++ /dev/null @@ -1,316 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "eventsystem_daemon.h" - -#define CION_METADATA_KEY "http://tizen.org/metadata/cion" - -static uid_t cur_uid; -static pkgmgr_client *pkgmgr; -static GList *service_list; - -struct cion_service { - char *pkgid; - char *appid; - char *service_name; - char *uuid; - int port; -}; - -static void __free_cion_service(gpointer data) -{ - struct cion_service *service = (struct cion_service *)data; - - free(service->pkgid); - free(service->appid); - free(service->service_name); - free(service->uuid); - free(service); -} - -static int __esd_cion_set_cur_uid(void) -{ - /* TODO(jeremy.jang): get current user from gumd or systemd */ - cur_uid = 5001; - return 0; -} - -static int __esd_cion_foreach_metadata_callback(const char *key, - const char *val, void *user_data) -{ - struct cion_service *service; - GList **service_list = (GList **)user_data; - - if (strncmp(key, CION_METADATA_KEY, strlen(CION_METADATA_KEY)) != 0) - return 0; - - if (val == NULL || strlen(val) == 0) { - _E("Service name is mandatory"); - return 0; - } - - service = calloc(1, sizeof(struct cion_service)); - if (service == NULL) { - _E("Out of memory"); - return -1; - } - - service->service_name = strdup(val); - if (service->service_name == NULL) { - _E("Out of memory"); - return -1; - } - - *service_list = g_list_append(*service_list, (gpointer)service); - - return 0; -} - -static void __esd_cion_remove_cion_service_by_appid(const char *appid) -{ - GList *item; - GList *next; - struct cion_service *service; - - item = service_list; - while (item != NULL) { - next = item->next; - service = (struct cion_service *)item->data; - if (strcmp(service->appid, appid) == 0) { - _D("Remove a cion service [%s:%s:%s:%d]", - service->appid, service->service_name, - service->uuid, service->port); - __free_cion_service(service); - service_list = g_list_delete_link(service_list, item); - } - item = next; - } -} - -static void __esd_cion_remove_cion_service_by_pkgid(const char *pkgid) -{ - GList *item; - GList *next; - struct cion_service *service; - - item = service_list; - while (item != NULL) { - next = item->next; - service = (struct cion_service *)item->data; - if (strcmp(service->pkgid, pkgid) == 0) { - _D("Remove a cion service [%s:%s:%s:%d]", - service->appid, service->service_name, - service->uuid, service->port); - __free_cion_service(service); - service_list = g_list_delete_link(service_list, item); - } - item = next; - } -} - -static int __esd_cion_add_cion_service(struct cion_service *service, - const char *pkgid, const char *appid) -{ - /* service name already set by __esd_cion_foreach_metadata_callback() */ - service->pkgid = strdup(pkgid); - if (service->pkgid == NULL) { - _E("Out of memory"); - return -1; - } - - service->appid = strdup(appid); - if (service->appid == NULL) { - _E("Out of memory"); - return -1; - } - - /* how to get uuid? */ - - service_list = g_list_append(service_list, service); - _D("Add a cion service [%s:%s:%s:%d]", service->appid, - service->service_name, service->uuid, - service->port); - - return 0; -} - -static int __esd_cion_foreach_app_callback(const pkgmgrinfo_appinfo_h appinfo, - void *user_data) -{ - int ret; - char *pkgid; - char *appid; - struct cion_service *service; - GList *item; - GList *list = NULL; - - ret = pkgmgrinfo_appinfo_foreach_metadata(appinfo, - __esd_cion_foreach_metadata_callback, &list); - if (ret != PMINFO_R_OK) { - _E("Failed to get metadata: %d", ret); - return -1; - } - - ret = pkgmgrinfo_appinfo_get_pkgid(appinfo, &pkgid); - if (ret != PMINFO_R_OK) { - _E("Failed to get pkgid: %d", ret); - return -1; - } - - ret = pkgmgrinfo_appinfo_get_appid(appinfo, &appid); - if (ret != PMINFO_R_OK) { - _E("Failed to get appid: %d", ret); - return -1; - } - - /* remove first, the updated app may no longer provide cion service */ - __esd_cion_remove_cion_service_by_appid(appid); - for (item = list; item; item = item->next) { - service = (struct cion_service *)item->data; - if (__esd_cion_add_cion_service(service, pkgid, appid)) { - _E("Failed to add a cion service"); - __free_cion_service(service); - } - /* remove reference, the global 'service_list' takes reference - * of 'service'. - */ - item->data = NULL; - } - - g_list_free(list); - - return 0; -} - -static int __esd_cion_pkgmgr_event_callback(uid_t target_uid, int req_id, - const char *pkg_type, const char *pkgid, const char *key, - const char *val, const void *pmsg, void *data) -{ - int ret; - pkgmgrinfo_pkginfo_h pkginfo; - - if (strncmp(key, "end", strlen("end")) || - strncmp(val, "ok", strlen("ok"))) - return 0; - - ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, target_uid, &pkginfo); - if (ret == PMINFO_R_OK) { - /* install or update */ - ret = pkgmgrinfo_appinfo_get_usr_list(pkginfo, PMINFO_ALL_APP, - __esd_cion_foreach_app_callback, NULL, - target_uid); - if (ret != PMINFO_R_OK) { - _E("Failed to get appinfo of pkgid %s: %d", pkgid, ret); - pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo); - return 0; - } - pkgmgrinfo_pkginfo_destroy_pkginfo(pkginfo); - } else if (ret == PMINFO_R_ENOENT) { - /* uninstall */ - __esd_cion_remove_cion_service_by_pkgid(pkgid); - } else { - _E("Failed to get pkginfo of %s: %d", pkgid, ret); - } - - return 0; -} - -static int __esd_cion_set_pkgmgr_callback(void) -{ - int ret; - - pkgmgr = pkgmgr_client_new(PC_LISTENING); - if (pkgmgr == NULL) { - _E("Failed to create pkgmgr client"); - return -1; - } - - ret = pkgmgr_client_set_status_type(pkgmgr, - PKGMGR_CLIENT_STATUS_INSTALL | - PKGMGR_CLIENT_STATUS_UPGRADE | - PKGMGR_CLIENT_STATUS_UNINSTALL); - if (ret != PKGMGR_R_OK) { - _E("Failed to set pkgmgr event status type: %d", ret); - pkgmgr_client_free(pkgmgr); - pkgmgr = NULL; - return -1; - } - - ret = pkgmgr_client_listen_status(pkgmgr, - __esd_cion_pkgmgr_event_callback, NULL); - if (ret < 0) { - _E("Failed to set event callback: %d", ret); - pkgmgr_client_free(pkgmgr); - pkgmgr = NULL; - return -1; - } - - return 0; -} - -static int __esd_cion_load_services(uid_t uid) -{ - int ret; - pkgmgrinfo_appinfo_metadata_filter_h filter; - - ret = pkgmgrinfo_appinfo_metadata_filter_create(&filter); - if (ret != PMINFO_R_OK) { - _E("Failed to create metadata filter: %d", ret); - return -1; - } - - ret = pkgmgrinfo_appinfo_metadata_filter_add(filter, - CION_METADATA_KEY, ""); - if (ret != PMINFO_R_OK) { - _E("Failed to add keyval to metadata filter: %d", ret); - pkgmgrinfo_appinfo_metadata_filter_destroy(filter); - return -1; - } - - ret = pkgmgrinfo_appinfo_usr_metadata_filter_foreach(filter, - __esd_cion_foreach_app_callback, NULL, uid); - if (ret != PMINFO_R_OK) { - _E("Failed to metadata filter foreach: %d", ret); - pkgmgrinfo_appinfo_metadata_filter_destroy(filter); - return -1; - } - - pkgmgrinfo_appinfo_metadata_filter_destroy(filter); - - return 0; -} - -int __esd_cion_init(void) -{ - if (__esd_cion_set_cur_uid()) { - _E("Failed to set current uid"); - return -1; - } - - /* how to handle when user switched? */ - if (__esd_cion_load_services(cur_uid)) { - _E("Failed to load cion services"); - return -1; - } - - if (__esd_cion_set_pkgmgr_callback()) { - _E("Failed to set pkgmgr event callback"); - return -1; - } - - return 0; -} - -void __esd_cion_finalize(void) -{ - if (pkgmgr) { - pkgmgr_client_remove_listen_status(pkgmgr); - pkgmgr_client_free(pkgmgr); - } - - g_list_free_full(service_list, __free_cion_service); -} diff --git a/src/esd_cion/esd_cion.cc b/src/esd_cion/esd_cion.cc new file mode 100644 index 0000000..8ee6485 --- /dev/null +++ b/src/esd_cion/esd_cion.cc @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. + * + * 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. + */ + +#include + +#include "eventsystem_daemon.h" +#include "cion_ondemand_server.h" + +namespace { + std::shared_ptr esd_cion_server; +} // namespace + +int _esd_cion_init() { + + try { + esd_cion_server = + std::make_shared("__CION_INTERNAL_DAEMON__", ""); + } catch (const cion::Exception& e) { + _D("cion_init failed : %s", e.what()); + return -1; + } + + esd_cion_server->Listen(); + + _D("cion_init done"); + + return 0; +} + +void _esd_cion_adds_enabled_app(const char *service_name, const char *app_id, + const char *display_name) { + if (esd_cion_server == nullptr) { + _E("Call init function at first"); + return; + } + + esd_cion_server->AddOndemandServiceList(std::string(service_name), + std::string(app_id), std::string(display_name)); +} + +void _esd_cion_removes_enabled_app(const char *service_name, const char *app_id) { + if (esd_cion_server == nullptr) { + _E("Call init function at first"); + return; + } + + esd_cion_server->RemoveOndemandServiceList(std::string(service_name), + std::string(app_id)); +} \ No newline at end of file diff --git a/src/esd_cion/esd_cion_db.c b/src/esd_cion/esd_cion_db.c index 2cf7fd9..1a99dc0 100644 --- a/src/esd_cion/esd_cion_db.c +++ b/src/esd_cion/esd_cion_db.c @@ -341,7 +341,9 @@ int esd_cion_set_enabled(const char *appid, const char *service_name, int ret = -1; sqlite3 *db; char *query = NULL; + const char *display_name = NULL; sqlite3_stmt *stmt = NULL; + sqlite3_stmt *select_stmt = NULL; db = esd_cion_db_open(); if (!db) { @@ -359,13 +361,34 @@ int esd_cion_set_enabled(const char *appid, const char *service_name, if (ret != SQLITE_OK) goto out; + query = sqlite3_mprintf("SELECT display_name FROM cion_display_name " + "WHERE appid = %Q AND service_name = %Q", appid, service_name); + if (query == NULL) + goto out; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &select_stmt, NULL); + if (ret != SQLITE_OK) + goto out; + ret = sqlite3_step(stmt); - if (ret == SQLITE_OK || ret == SQLITE_DONE) + if (ret != SQLITE_OK && ret != SQLITE_DONE) + goto out; + + ret = sqlite3_step(select_stmt); + if (ret == SQLITE_ROW) { ret = 0; + display_name = (const char*)sqlite3_column_text(select_stmt, 0); + } + + if (enabled) + _esd_cion_adds_enabled_app(service_name, appid, display_name); + else + _esd_cion_removes_enabled_app(service_name, appid); out: sqlite3_free(query); sqlite3_finalize(stmt); + sqlite3_finalize(select_stmt); esd_cion_db_close(&db); return ret; @@ -411,14 +434,12 @@ out: return ret; } -int esd_cion_get_enabled_service_list(const char *service_name, - const char *display_name, GList **list) +int esd_cion_get_enabled_service_list(GList **list) { int ret = -1; sqlite3 *db; char *query = NULL; sqlite3_stmt *stmt = NULL; - char *appid; db = esd_cion_db_open(); if (!db) { @@ -426,13 +447,8 @@ int esd_cion_get_enabled_service_list(const char *service_name, return -1; } - if (display_name) - query = sqlite3_mprintf("SELECT appid FROM cion_display_name " - "WHERE service_name = %Q AND display_name = %Q AND enabled = 1", - service_name, display_name); - else - query = sqlite3_mprintf("SELECT appid FROM cion_display_name " - "WHERE service_name = %Q AND enabled = 1", service_name); + query = sqlite3_mprintf("SELECT service_name, appid, display_name " + "FROM cion_display_name WHERE enabled = 1"); if (query == NULL) goto out; @@ -441,10 +457,17 @@ int esd_cion_get_enabled_service_list(const char *service_name, goto out; while(sqlite3_step(stmt) == SQLITE_ROW) { - appid = strdup((char*)sqlite3_column_text(stmt, 0)); - _E("get appid list : %s", appid); + cion_service_info_s *cion_info = malloc(sizeof(cion_service_info_s)); + cion_info->service_name = strdup((char*)sqlite3_column_text(stmt, 0)); + cion_info->appid = strdup((char*)sqlite3_column_text(stmt, 1)); + + char *display_name = (char*)sqlite3_column_text(stmt, 2); + if (display_name) + cion_info->display_name = strdup(display_name); + _D("get list : [%s:%s:%s]", + cion_info->service_name, cion_info->appid, cion_info->display_name); - *list = g_list_append(*list, appid); + *list = g_list_append(*list, cion_info); } out: diff --git a/src/esd_main.c b/src/esd_main.c index dabb5ce..69d5eb1 100644 --- a/src/esd_main.c +++ b/src/esd_main.c @@ -2619,7 +2619,7 @@ int main(int argc, char *argv[]) return ES_R_ERROR; } - if (__esd_cion_init() != 0) { + if (_esd_cion_init() != 0) { _E("ESD Cion Initialization failed!"); g_main_loop_unref(mainloop); return ES_R_ERROR; @@ -2639,7 +2639,6 @@ int main(int argc, char *argv[]) _E("shutdown"); - __esd_cion_finalize(); __esd_finalize(); g_main_loop_unref(mainloop); -- 2.7.4 From 4d2c55a47ee357a53cdac18b88ac302b5219bbaa Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Mon, 23 Aug 2021 09:55:55 +0900 Subject: [PATCH 11/16] Release version 0.2.0 Changes: - Add esd-cion module - Add db for cion uuid - Add display name for cion db - Add Cion Server Change-Id: I504fdc148efca60abfe871be93795da5e06456ae Signed-off-by: Inkyun Kil --- packaging/esd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/esd.spec b/packaging/esd.spec index 4bcc907..4241d43 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 0.1.22 +Version: 0.2.0 Release: 1 Group: Application Framework/Service License: Apache-2.0 -- 2.7.4 From bf3ccc693938007cc8d4b651b42cc30599d2701d Mon Sep 17 00:00:00 2001 From: SukhyungKang Date: Thu, 26 Aug 2021 15:59:52 +0900 Subject: [PATCH 12/16] Fix static analysis issues - NO_CATCH - UNINIT.LOCAL_VAR.EX - MEMORY_LEAK.EX - BAD_SIZEOF - FORWARD_NULL - USE_AFTER_FREE Change-Id: Icc2afb3ca9ebb431b278dfbd7296d6ead1683a66 Signed-off-by: SukhyungKang Signed-off-by: jusung --- src/esd_cion/cion_ondemand_server.cc | 2 ++ src/esd_cion/esd_cion.cc | 3 +-- src/esd_cion/esd_cion_db.c | 29 ++++++++++++++--------------- src/esd_main.c | 10 +++------- 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/esd_cion/cion_ondemand_server.cc b/src/esd_cion/cion_ondemand_server.cc index 0d5965e..2493ac3 100644 --- a/src/esd_cion/cion_ondemand_server.cc +++ b/src/esd_cion/cion_ondemand_server.cc @@ -322,6 +322,8 @@ void CionOndemandServer::AddOndemandServiceList(std::string service_name, std::make_shared(service_name, parcel.GetRaw().data(), parcel.GetRaw().size()); ondemand_peer_list_.emplace_back(pi); + free(uuid); + _D("[%s:%s] is added to list", appid.c_str(), service_name.c_str()); } diff --git a/src/esd_cion/esd_cion.cc b/src/esd_cion/esd_cion.cc index 8ee6485..a91b95c 100644 --- a/src/esd_cion/esd_cion.cc +++ b/src/esd_cion/esd_cion.cc @@ -28,13 +28,12 @@ int _esd_cion_init() { try { esd_cion_server = std::make_shared("__CION_INTERNAL_DAEMON__", ""); + esd_cion_server->Listen(); } catch (const cion::Exception& e) { _D("cion_init failed : %s", e.what()); return -1; } - esd_cion_server->Listen(); - _D("cion_init done"); return 0; diff --git a/src/esd_cion/esd_cion_db.c b/src/esd_cion/esd_cion_db.c index 1a99dc0..61b7b5b 100644 --- a/src/esd_cion/esd_cion_db.c +++ b/src/esd_cion/esd_cion_db.c @@ -160,7 +160,7 @@ static const char *__esd_cion_generate_uuid() { static int __esd_cion_get_uuid(sqlite3 *db, const char *appid, char **uuid) { int ret = -1; char *query; - sqlite3_stmt *stmt; + sqlite3_stmt *stmt = NULL; query = sqlite3_mprintf("SELECT uuid FROM cion_uuid " "WHERE appid = %Q", appid); @@ -187,7 +187,7 @@ out: static int __esd_cion_set_uuid(sqlite3 *db, const char *appid, const char *uuid) { int ret = -1; char *query; - sqlite3_stmt *stmt; + sqlite3_stmt *stmt = NULL; query = sqlite3_mprintf("INSERT INTO cion_uuid (appid, uuid) " "VALUES (%Q, %Q) ", appid, uuid); @@ -242,7 +242,7 @@ int esd_cion_set_display_name(const char *appid, const char *service_name, sqlite3 *db; char *query = NULL; sqlite3_stmt *stmt = NULL; - char *_uuid; + char *_uuid = NULL; db = esd_cion_db_open(); if (!db) { @@ -250,16 +250,11 @@ int esd_cion_set_display_name(const char *appid, const char *service_name, return -1; } - if (__esd_cion_get_uuid(db, appid, &_uuid) == 0) { - free(_uuid); - } else { + if (__esd_cion_get_uuid(db, appid, &_uuid) != 0) { _uuid = (char*)__esd_cion_generate_uuid(); ret = __esd_cion_set_uuid(db, appid, _uuid); - if (ret != 0) { - free(_uuid); + if (ret != 0) goto out; - } - _E("set uuid generate"); } query = sqlite3_mprintf("INSERT INTO cion_display_name " @@ -282,6 +277,7 @@ int esd_cion_set_display_name(const char *appid, const char *service_name, ret = 0; out: + free(_uuid); sqlite3_free(query); sqlite3_finalize(stmt); esd_cion_db_close(&db); @@ -341,7 +337,7 @@ int esd_cion_set_enabled(const char *appid, const char *service_name, int ret = -1; sqlite3 *db; char *query = NULL; - const char *display_name = NULL; + const char *display_name = ""; sqlite3_stmt *stmt = NULL; sqlite3_stmt *select_stmt = NULL; @@ -361,6 +357,11 @@ int esd_cion_set_enabled(const char *appid, const char *service_name, if (ret != SQLITE_OK) goto out; + ret = sqlite3_step(stmt); + if (ret != SQLITE_OK && ret != SQLITE_DONE) + goto out; + + sqlite3_free(query); query = sqlite3_mprintf("SELECT display_name FROM cion_display_name " "WHERE appid = %Q AND service_name = %Q", appid, service_name); if (query == NULL) @@ -370,10 +371,6 @@ int esd_cion_set_enabled(const char *appid, const char *service_name, if (ret != SQLITE_OK) goto out; - ret = sqlite3_step(stmt); - if (ret != SQLITE_OK && ret != SQLITE_DONE) - goto out; - ret = sqlite3_step(select_stmt); if (ret == SQLITE_ROW) { ret = 0; @@ -464,6 +461,8 @@ int esd_cion_get_enabled_service_list(GList **list) char *display_name = (char*)sqlite3_column_text(stmt, 2); if (display_name) cion_info->display_name = strdup(display_name); + else + cion_info->display_name = strdup(""); _D("get list : [%s:%s:%s]", cion_info->service_name, cion_info->appid, cion_info->display_name); diff --git a/src/esd_main.c b/src/esd_main.c index 69d5eb1..5a931ad 100644 --- a/src/esd_main.c +++ b/src/esd_main.c @@ -1310,7 +1310,7 @@ static int __esd_get_appid_by_pid(int pid, char *app_id, int buf_size) if (fd < 0) return ES_R_ERROR; - ret = read(fd, app_id, sizeof(app_id) - 1); + ret = read(fd, app_id, buf_size - 1); close(fd); if (ret <= 0) @@ -2066,10 +2066,8 @@ static void __esd_on_bus_acquired(GDBusConnection *connection, NULL, NULL); - if (boot_id == 0) { + if (boot_id == 0) _E("g_dbus_connection_signal_subscribe() is failed."); - g_object_unref(connection); - } user_boot_id = g_dbus_connection_signal_subscribe(connection, NULL, @@ -2082,10 +2080,8 @@ static void __esd_on_bus_acquired(GDBusConnection *connection, NULL, NULL); - if (user_boot_id == 0) { + if (user_boot_id == 0) _E("g_dbus_connection_signal_subscribe() is failed."); - g_object_unref(connection); - } } static void __esd_on_name_acquired(GDBusConnection *connection, -- 2.7.4 From 934432c9e778c89e560fd60553c6a3ebf6be619e Mon Sep 17 00:00:00 2001 From: SukhyungKang Date: Thu, 26 Aug 2021 17:27:28 +0900 Subject: [PATCH 13/16] Release version 0.2.1 Changes: - Fix static analysis issues Change-Id: Iccbcae5af0986bbf35cd5fac08efdeabcfc3ec5e Signed-off-by: SukhyungKang --- packaging/esd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/esd.spec b/packaging/esd.spec index 4241d43..65e4ac5 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 0.2.0 +Version: 0.2.1 Release: 1 Group: Application Framework/Service License: Apache-2.0 -- 2.7.4 From 407377e2d3c59f62a3ea6be8721238b73fc8f2de Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Fri, 10 Sep 2021 11:56:30 +0900 Subject: [PATCH 14/16] Remove error code when failing cion init Change-Id: Id10dd5e1805f4cd00b0c07ab4825b525b4253eba Signed-off-by: Inkyun Kil --- src/esd_main.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/esd_main.c b/src/esd_main.c index 5a931ad..dee892c 100644 --- a/src/esd_main.c +++ b/src/esd_main.c @@ -2615,11 +2615,8 @@ int main(int argc, char *argv[]) return ES_R_ERROR; } - if (_esd_cion_init() != 0) { + if (_esd_cion_init() != 0) _E("ESD Cion Initialization failed!"); - g_main_loop_unref(mainloop); - return ES_R_ERROR; - } if (__esd_before_loop() < 0) { _E("ESD failed!"); -- 2.7.4 From e3d4303b1bb4a94cfad63f6eb17d87298253f9ab Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Fri, 10 Sep 2021 15:25:19 +0900 Subject: [PATCH 15/16] Release version 0.2.2 changes: - Remove error code when failing cion init Change-Id: I010c11b0e3f01d330d5e7c338a38c49fe78569b6 Signed-off-by: Inkyun Kil --- packaging/esd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/esd.spec b/packaging/esd.spec index 65e4ac5..ca63ca6 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 0.2.1 +Version: 0.2.2 Release: 1 Group: Application Framework/Service License: Apache-2.0 -- 2.7.4 From b18552e143befba9d758dd570cff0eae96f02922 Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Wed, 13 Oct 2021 18:55:04 +0900 Subject: [PATCH 16/16] Change cion listen status - If ondemand list is empty, there is no need to listen. Change-Id: I0d9869b835d90714151061a9b5d55e2195120afb Signed-off-by: Inkyun Kil --- src/esd_cion/esd_cion.cc | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/esd_cion/esd_cion.cc b/src/esd_cion/esd_cion.cc index a91b95c..e124d59 100644 --- a/src/esd_cion/esd_cion.cc +++ b/src/esd_cion/esd_cion.cc @@ -21,14 +21,35 @@ namespace { std::shared_ptr esd_cion_server; + bool is_listening = false; } // namespace -int _esd_cion_init() { +static void __change_listen_status_cion_server() { + try { + GList *list = nullptr; + int ret = esd_cion_get_enabled_service_list(&list); + if (ret != 0) { + _D("Get list error"); + } else if (list == nullptr && is_listening) { + esd_cion_server->Stop(); + is_listening = false; + _D("esd_cion_server stop listen"); + } else if (list != nullptr && !is_listening) { + esd_cion_server->Listen(); + is_listening = true; + _D("esd_cion_server start listen"); + } + } catch (const cion::Exception& e) { + _E("cion_init failed : %s", e.what()); + } + + return; +} +int _esd_cion_init() { try { esd_cion_server = std::make_shared("__CION_INTERNAL_DAEMON__", ""); - esd_cion_server->Listen(); } catch (const cion::Exception& e) { _D("cion_init failed : %s", e.what()); return -1; @@ -36,6 +57,8 @@ int _esd_cion_init() { _D("cion_init done"); + __change_listen_status_cion_server(); + return 0; } @@ -48,6 +71,9 @@ void _esd_cion_adds_enabled_app(const char *service_name, const char *app_id, esd_cion_server->AddOndemandServiceList(std::string(service_name), std::string(app_id), std::string(display_name)); + + if (!is_listening) + __change_listen_status_cion_server(); } void _esd_cion_removes_enabled_app(const char *service_name, const char *app_id) { @@ -58,4 +84,7 @@ void _esd_cion_removes_enabled_app(const char *service_name, const char *app_id) esd_cion_server->RemoveOndemandServiceList(std::string(service_name), std::string(app_id)); + + if (is_listening) + __change_listen_status_cion_server(); } \ No newline at end of file -- 2.7.4