From 5251d11ed350eb6245867f2882f7ad6a38f16565 Mon Sep 17 00:00:00 2001 From: "SukHyung, Kang" Date: Thu, 8 Jul 2021 17:27:20 +0900 Subject: [PATCH 01/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 02/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 03/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 04/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 05/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 06/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 07/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 08/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 From c6398d3f2d76303c89417dd2ebbfd28931895bf8 Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Thu, 4 Nov 2021 09:18:08 +0900 Subject: [PATCH 09/16] Release version 0.2.3 changes: - Change cion listen status Change-Id: Ie2a9444b17fd176b294ad5d983c62e62122df11f 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 ca63ca6..317a3f2 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 0.2.2 +Version: 0.2.3 Release: 1 Group: Application Framework/Service License: Apache-2.0 -- 2.7.4 From 1c82ddc6db3d7136f1b446eacc9542f0e16cc70f Mon Sep 17 00:00:00 2001 From: jusung Date: Thu, 11 Nov 2021 09:12:27 +0900 Subject: [PATCH 10/16] Rewire extern "C" blocks This is needed in order not to #include "glib.h" in the extern "C" blocks, which breaks in the newest GLib version. Change-Id: Ie94aedff5bbbdb5f85b0abbb167d5e7d5498ea0c Signed-off-by: jusung --- include/eventsystem_daemon.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/eventsystem_daemon.h b/include/eventsystem_daemon.h index 706a89b..f6f853d 100644 --- a/include/eventsystem_daemon.h +++ b/include/eventsystem_daemon.h @@ -21,13 +21,13 @@ * header file for eventsystem_daemon */ +#include +#include + #ifdef __cplusplus extern "C" { #endif -#include -#include - #undef LOG_TAG #define LOG_TAG "ESD" -- 2.7.4 From 919f482b4e45d594700cfe951bda0c7bd11e09dc Mon Sep 17 00:00:00 2001 From: jusung Date: Thu, 11 Nov 2021 09:15:34 +0900 Subject: [PATCH 11/16] Release version 0.2.4 Changes: - Rewire extern "C" blocks Change-Id: I3984f8f2a1d41454035596d2fa6eafde95589d07 Signed-off-by: jusung --- packaging/esd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/esd.spec b/packaging/esd.spec index 317a3f2..35a6100 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 0.2.3 +Version: 0.2.4 Release: 1 Group: Application Framework/Service License: Apache-2.0 -- 2.7.4 From 31feec915d92a3b142144eb1dd05e69ed04dcf01 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 26 Sep 2022 02:35:54 +0000 Subject: [PATCH 12/16] Use modified tizen_base::Parcel To improve the performance of the parcel creation, the implementation of the Parcel is changed. It uses malloc() instead of std::vector. Requires: - https://review.tizen.org/gerrit/#/c/platform/core/base/bundle/+/281779/ Change-Id: Iba427db6a0e5624fdb6cc5a0b0290cc4960d3fa0 Signed-off-by: Hwankyu Jhun --- src/esd_cion/cion_ondemand_server.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/esd_cion/cion_ondemand_server.cc b/src/esd_cion/cion_ondemand_server.cc index 2493ac3..13f9923 100644 --- a/src/esd_cion/cion_ondemand_server.cc +++ b/src/esd_cion/cion_ondemand_server.cc @@ -171,7 +171,7 @@ void CionOndemandServer::OnOndemandListRequested( 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(); + raw = GetOndemandList(list_service_name).ToRaw(); if (raw.size() == 0) return; } @@ -278,7 +278,7 @@ void CionOndemandServer::LoadOndemandServiceList() { std::shared_ptr pi = std::make_shared(std::string(service_name), - parcel.GetRaw().data(), parcel.GetRaw().size()); + parcel.GetData(), parcel.GetDataSize()); ondemand_peer_list_.emplace_back(pi); free(uuid); @@ -319,8 +319,8 @@ void CionOndemandServer::AddOndemandServiceList(std::string service_name, parcel.WriteString(display_name); std::shared_ptr pi = - std::make_shared(service_name, parcel.GetRaw().data(), - parcel.GetRaw().size()); + std::make_shared(service_name, parcel.GetData(), + parcel.GetDataSize()); ondemand_peer_list_.emplace_back(pi); free(uuid); @@ -365,4 +365,4 @@ tizen_base::Parcel CionOndemandServer::GetOndemandList( parcel.WriteBool(false); return parcel; -} \ No newline at end of file +} -- 2.7.4 From 142004a960943efa2dd32159543d187ba3f1c860 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Tue, 27 Sep 2022 06:03:42 +0000 Subject: [PATCH 13/16] Fix static anlaysis issues The following issues are fixed: - NULL_RETURNS - AUTO_CAUSES_COPY Change-Id: Id7244305e0762f6b532dff6ea792a68740e49872 Signed-off-by: Hwankyu Jhun --- src/esd_cion/cion_ondemand_server.cc | 4 +- src/esd_cion/cion_ondemand_server.h | 2 +- src/esd_cion/esd_cion_db.c | 86 +++++++++++++++++++++++++++++++----- 3 files changed, 78 insertions(+), 14 deletions(-) diff --git a/src/esd_cion/cion_ondemand_server.cc b/src/esd_cion/cion_ondemand_server.cc index 13f9923..a7c267b 100644 --- a/src/esd_cion/cion_ondemand_server.cc +++ b/src/esd_cion/cion_ondemand_server.cc @@ -290,7 +290,7 @@ void CionOndemandServer::LoadOndemandServiceList() { void CionOndemandServer::AddOndemandServiceList(std::string service_name, std::string appid, std::string display_name) { - for (auto peer : ondemand_peer_list_) { + for (auto const& peer : ondemand_peer_list_) { if (peer->GetServiceName() == service_name && peer->GetAppID() == appid) { _W("%s is already exist", appid.c_str()); @@ -330,7 +330,7 @@ void CionOndemandServer::AddOndemandServiceList(std::string service_name, void CionOndemandServer::RemoveOndemandServiceList(std::string service_name, std::string appid) { for (auto peer = ondemand_peer_list_.begin(); - peer != ondemand_peer_list_.end(); peer++) { + peer != ondemand_peer_list_.end(); peer++) { if (peer->get()->GetServiceName() == service_name && peer->get()->GetAppID() == appid) { ondemand_peer_list_.erase(peer); diff --git a/src/esd_cion/cion_ondemand_server.h b/src/esd_cion/cion_ondemand_server.h index b3852d0..3f7ed16 100644 --- a/src/esd_cion/cion_ondemand_server.h +++ b/src/esd_cion/cion_ondemand_server.h @@ -59,4 +59,4 @@ class CionOndemandServer : public cion::channel::ServerChannel { std::list> ondemand_peer_list_; }; -#endif /* EVENTSYSTEM_DAEMON_CION_ONDEMAND_SERVER_H_ */ \ No newline at end of file +#endif /* EVENTSYSTEM_DAEMON_CION_ONDEMAND_SERVER_H_ */ diff --git a/src/esd_cion/esd_cion_db.c b/src/esd_cion/esd_cion_db.c index 61b7b5b..f6689f4 100644 --- a/src/esd_cion/esd_cion_db.c +++ b/src/esd_cion/esd_cion_db.c @@ -431,12 +431,79 @@ out: return ret; } +static void __cion_service_info_destroy(cion_service_info_s *info) +{ + if (info == NULL) + return; + + free(info->service_name); + free(info->appid); + free(info->display_name); + free(info); +} + +static cion_service_info_s *__cion_service_info_create(sqlite3_stmt *stmt) +{ + cion_service_info_s *info; + const char* value; + int index = 0; + + info = calloc(1, sizeof(cion_service_info_s)); + if (info == NULL) { + _E("malloc() is failed"); + return NULL; + } + + value = (const char *)sqlite3_column_text(stmt, index++); + if (value == NULL) { + _E("sqlite3_column_text() is failed. index: %d", index - 1); + __cion_service_info_destroy(info); + return NULL; + } + + info->service_name = strdup(value); + if (info->service_name == NULL) { + _E("strdup() is failed"); + __cion_service_info_destroy(info); + return NULL; + } + + value = (const char *)sqlite3_column_text(stmt, index++); + if (value == NULL) { + _E("sqlite3_column_text() is failed. index: %d", index - 1); + __cion_service_info_destroy(info); + return NULL; + } + + info->appid = strdup(value); + if (info->appid == NULL) { + _E("strdup() is failed"); + __cion_service_info_destroy(info); + return NULL; + } + + value = (const char *)sqlite3_column_text(stmt, index++); + if (value != NULL) + info->display_name = strdup(value); + else + info->display_name = strdup(""); + + if (info->display_name == NULL) { + _E("strdup() is failed"); + __cion_service_info_destroy(info); + return NULL; + } + + return info; +} + int esd_cion_get_enabled_service_list(GList **list) { int ret = -1; sqlite3 *db; char *query = NULL; sqlite3_stmt *stmt = NULL; + cion_service_info_s *cion_info; db = esd_cion_db_open(); if (!db) { @@ -453,19 +520,16 @@ int esd_cion_get_enabled_service_list(GList **list) if (ret != SQLITE_OK) goto out; - while(sqlite3_step(stmt) == SQLITE_ROW) { - 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)); + while (sqlite3_step(stmt) == SQLITE_ROW) { + cion_info = __cion_service_info_create(stmt); + if (cion_info == NULL) { + ret = -1; + goto out; + } - 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); - + cion_info->service_name, cion_info->appid, + cion_info->display_name); *list = g_list_append(*list, cion_info); } -- 2.7.4 From 7b2f4e851464d61db17c464e5614b9953defbd9f Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Tue, 27 Sep 2022 06:56:40 +0000 Subject: [PATCH 14/16] Release version 0.2.5 Changes: - Use modified tizen_base::Parcel - Fix static anlaysis issues Change-Id: Idab9d19241a0888eff6329862380e17219fc3d51 Signed-off-by: Hwankyu Jhun --- packaging/esd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/esd.spec b/packaging/esd.spec index 35a6100..e419837 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 0.2.4 +Version: 0.2.5 Release: 1 Group: Application Framework/Service License: Apache-2.0 -- 2.7.4 From 5241fb18697622153a5f3bed7aaff8344e2b3168 Mon Sep 17 00:00:00 2001 From: Inkyun Kil Date: Thu, 26 Oct 2023 15:48:54 +0900 Subject: [PATCH 15/16] Set memory limit Change-Id: I54ad1e8d74dfede294781c2c48e1bab15ff694cc Signed-off-by: Inkyun Kil (cherry picked from commit df189a1fdaab167bcc8662d552137c54623d01a4) --- packaging/esd.service | 1 + 1 file changed, 1 insertion(+) diff --git a/packaging/esd.service b/packaging/esd.service index e59df2e..277f18c 100644 --- a/packaging/esd.service +++ b/packaging/esd.service @@ -12,6 +12,7 @@ BusName=tizen.system.event.app2esd ExecStart=/usr/bin/esd Restart=on-failure RestartSec=0 +MemoryLimit=100M [Install] WantedBy=multi-user.target \ No newline at end of file -- 2.7.4 From 920157c81debde6f6295eaf4ecc7505023959f9b Mon Sep 17 00:00:00 2001 From: jusung son Date: Fri, 27 Oct 2023 08:31:12 +0900 Subject: [PATCH 16/16] Release version 0.2.6 Changes: - Set memory limit Signed-off-by: jusung son Change-Id: I70815ffa24349b0e39aa8e915c76edcc8e2de444 --- packaging/esd.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/esd.spec b/packaging/esd.spec index e419837..3d938f9 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -1,6 +1,6 @@ Name: esd Summary: Event system daemon -Version: 0.2.5 +Version: 0.2.6 Release: 1 Group: Application Framework/Service License: Apache-2.0 -- 2.7.4