From: SukHyung, Kang Date: Thu, 8 Jul 2021 08:27:20 +0000 (+0900) Subject: Add display name for cion db X-Git-Tag: submit/gerrit/20210823.012340~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5251d11ed350eb6245867f2882f7ad6a38f16565;p=platform%2Fcore%2Fappfw%2Fevent-system.git Add display name for cion db Change-Id: I5fb7078dc6268b20acdf4778aa13c238fac4d6a7 Signed-off-by: SukHyung, Kang --- 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.c deleted file mode 100644 index 48447e0..0000000 --- a/src/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.c b/src/esd_cion/esd_cion.c new file mode 100644 index 0000000..48447e0 --- /dev/null +++ b/src/esd_cion/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_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); } }