From: SukHyung, Kang Date: Fri, 28 May 2021 03:11:45 +0000 (+0900) Subject: Add db for cion uuid X-Git-Tag: accepted/tizen/unified/20210823.124242~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F32%2F258932%2F15;p=platform%2Fcore%2Fappfw%2Fevent-system.git Add db for cion uuid Change-Id: I9a139d2b7f95eda3aa08c03ee162d47dcfdbf428 Signed-off-by: SukHyung, Kang --- diff --git a/CMakeLists.txt b/CMakeLists.txt index d62feab..66e0a4e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,24 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src) ### Required packages INCLUDE(FindPkgConfig) -pkg_check_modules(pkgs REQUIRED dlog bundle pkgmgr-info glib-2.0 gio-2.0 appsvc aul vconf libtzplatform-config cert-svc-vcore cynara-client cynara-creds-gdbus cynara-session security-manager) +pkg_check_modules(pkgs REQUIRED + dlog + bundle + pkgmgr-info + glib-2.0 + gio-2.0 + appsvc + aul + vconf + libtzplatform-config + cert-svc-vcore + cynara-client + cynara-creds-gdbus + cynara-session + security-manager + sqlite3 + uuid +) FOREACH(flag ${pkgs_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") diff --git a/include/eventsystem_daemon.h b/include/eventsystem_daemon.h index 775426b..5f600ce 100644 --- a/include/eventsystem_daemon.h +++ b/include/eventsystem_daemon.h @@ -52,6 +52,9 @@ int __esd_register_vconf_callbacks(void); int __esd_cion_init(void); void __esd_cion_finalize(void); +int esd_cion_db_init(void); +int esd_cion_get_uuid_with_generate(const char* appid, char** uuid); + #ifdef __cplusplus } #endif diff --git a/packaging/esd.spec b/packaging/esd.spec index 55d4b2f..6d7c3c5 100644 --- a/packaging/esd.spec +++ b/packaging/esd.spec @@ -24,6 +24,8 @@ BuildRequires: pkgconfig(cynara-client) BuildRequires: pkgconfig(cynara-creds-gdbus) BuildRequires: pkgconfig(cynara-session) BuildRequires: pkgconfig(security-manager) +BuildRequires: pkgconfig(uuid) +BuildRequires: pkgconfig(sqlite3) Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig diff --git a/src/esd_cion_db.c b/src/esd_cion_db.c new file mode 100644 index 0000000..2c34c62 --- /dev/null +++ b/src/esd_cion_db.c @@ -0,0 +1,206 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "eventsystem_daemon.h" + +#define DBPATH tzplatform_mkpath(TZ_SYS_DB, ".cion.db") + +#define CREATE_CION_TABLE " \ +PRAGMA user_version = 50; \ +PRAGMA journal_mode = PERSIST; \ +PRAGMA foreign_keys = ON; \ +BEGIN EXCLUSIVE TRANSACTION; \ +CREATE TABLE IF NOT EXISTS cion_info ( \ + appid TEXT NOT NULL, \ + uuid TEXT NOT NULL, \ + PRIMARY KEY (appid) \ +); \ +COMMIT TRANSACTION; " + +static int __check_table_exist(sqlite3 *db) { + int ret; + const char *val; + sqlite3_stmt *stmt = NULL; + const char query[] = + "SELECT name FROM sqlite_master WHERE type='table'" + " ORDER BY name ASC"; + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _E("prepare error: %s", sqlite3_errmsg(db)); + ret = -1; + goto out; + } + + ret = sqlite3_step(stmt); + if (ret != SQLITE_ROW) { + _E("fail to get row"); + ret = -1; + goto out; + } + + val = (const char*)sqlite3_column_text(stmt, 0); + if (val == NULL) { + _E("name is NULL"); + ret = -1; + goto out; + } + + if (strcmp("cion", val) != 0) { + ret = -1; + goto out; + } + + ret = 0; + +out: + sqlite3_finalize(stmt); + + return ret; +} + +static int __create_table(sqlite3 *db) { + int ret; + char *errmsg = NULL; + + ret = sqlite3_exec(db, CREATE_CION_TABLE, + NULL, NULL, &errmsg); + if (ret != SQLITE_OK) { + _E("create table fail : %s", errmsg); + sqlite3_free(errmsg); + return -1; + } + + return 0; +} + +int esd_cion_db_init(void) { + sqlite3 *db = NULL; + + if (sqlite3_open_v2(DBPATH, &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) { + sqlite3_close_v2(db); + unlink(DBPATH); + + if (sqlite3_open_v2(DBPATH, &db, + SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, + NULL) != SQLITE_OK) { + _E("Fail to create db"); + unlink(DBPATH); + return -1; + } + } + + if (__check_table_exist(db) < 0) { + if (__create_table(db) < 0) { + sqlite3_close_v2(db); + _E("Fail to create table"); + return -1; + } + } + + sqlite3_close_v2(db); + + return 0; +} + +sqlite3 *esd_cion_db_open() { + sqlite3 *db; + + if (access(DBPATH, R_OK | W_OK) != 0) + return NULL; + + if (sqlite3_open_v2(DBPATH, &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) + return NULL; + + return db; +} + +int esd_cion_db_close(sqlite3 **db) { + if (db == NULL || *db == NULL) + return 0; + + if (sqlite3_close(*db) != SQLITE_OK) { + _E("Failed to close db"); + return 0; + } + + *db = NULL; + + return 0; +} + +static const char *__esd_cion_generate_uuid() { + uuid_t uu; + char _uuid[37]; + char *uuid; + + uuid_generate_random(uu); + uuid_unparse(uu, _uuid); + + uuid = strdup(_uuid); + + return uuid; +} + +int esd_cion_get_uuid_with_generate(const char *appid, char **uuid) { + int ret; + sqlite3 *db; + char *query = NULL; + sqlite3_stmt *stmt = NULL; + const char *_uuid; + + db = esd_cion_db_open(); + if (!db) { + _E("db open fail"); + return -1; + } + + query = sqlite3_mprintf("SELECT uuid FROM cion_info " + "WHERE appid = %Q", appid); + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + ret = -1; + goto out; + } + + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + ret = 0; + *uuid = strdup((char*)sqlite3_column_text(stmt, 0)); + } else { + sqlite3_free(query); + sqlite3_finalize(stmt); + stmt = NULL; + + _uuid = __esd_cion_generate_uuid(); + query = sqlite3_mprintf("INSERT INTO cion_info (appid, uuid) " + "VALUES (%Q, %Q) ", appid, _uuid); + + ret = sqlite3_prepare_v2(db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + ret = -1; + goto out; + } + + ret = sqlite3_step(stmt); + if (ret == SQLITE_OK || ret == SQLITE_DONE) { + ret = 0; + *uuid = strdup(_uuid); + } else { + ret = -1; + } + } + +out: + sqlite3_free(query); + sqlite3_finalize(stmt); + esd_cion_db_close(&db); + + return ret; +} diff --git a/src/esd_main.c b/src/esd_main.c index 61f64ea..9d4480e 100644 --- a/src/esd_main.c +++ b/src/esd_main.c @@ -1246,10 +1246,14 @@ static const gchar introspection_xml[] = " " " " " " +" " +" " +" " +" " " " ""; -static int __esd_get_appid_by_pid(int pid, uid_t uid, char *app_id, int buf_size) +static int __esd_get_appid_by_pid_for_uid(int pid, uid_t uid, char *app_id, int buf_size) { int retval = ES_R_OK; int ret = 0; @@ -1272,6 +1276,33 @@ static int __esd_get_appid_by_pid(int pid, uid_t uid, char *app_id, int buf_size return retval; } +static int __esd_get_appid_by_pid(int pid, char *app_id, int buf_size) +{ + int ret; + int fd; + char buf[128] = { 0, }; + + ret = aul_app_get_appid_bypid(pid, app_id, buf_size); + if (ret != AUL_R_OK) { + snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid); + + fd = open(buf, O_RDONLY); + if (fd < 0) + return ES_R_ERROR; + + ret = read(fd, app_id, sizeof(app_id) - 1); + close(fd); + + if (ret <= 0) + return ES_R_ERROR; + + app_id[ret] = '\0'; + ret = ES_R_OK; + } + + return ret; +} + static int check_user_event_sender_valid(const char *event_name, const char *app_id) { char *valid_name = NULL; @@ -1338,7 +1369,7 @@ static void check_sender_valid_method_call(GDBusConnection *connection, const gc _D("event_sender_pid(%d), event_name(%s)", event_sender_pid, event_name); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(event_sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { + if (__esd_get_appid_by_pid_for_uid(event_sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { if (check_user_event_sender_valid(event_name, app_id) < 0) { @@ -1369,7 +1400,7 @@ static void check_send_event_valid_method_call(GDBusConnection *connection, cons sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { if (check_user_event_sender_valid(event_name, app_id) < 0) { @@ -1408,7 +1439,7 @@ static void get_trusted_peer_method_call(GDBusConnection *connection, const gcha sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { builder = g_variant_builder_new(G_VARIANT_TYPE("as")); @@ -1456,7 +1487,7 @@ static void setup_trusted_peer_method_call(GDBusConnection *connection, const gc if (destination_name && destination_name[0] != '\0') { sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { ret = __esd_trusted_busname_add_item(sender_uid, app_id, destination_name, @@ -1500,7 +1531,7 @@ static void check_privilege_valid_method_call(GDBusConnection *connection, const if (privilege_name) { sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { result = ES_R_ERROR; } else { ret = cynara_creds_gdbus_get_client(connection, sender, CLIENT_METHOD_DEFAULT, &client); @@ -1605,7 +1636,7 @@ static void keep_last_data_method_call(GDBusConnection *connection, sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { _E("failed to get appid by pid"); result = ES_R_ERROR; @@ -1670,7 +1701,7 @@ static void check_last_data_method_call(GDBusConnection *connection, sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { result = ES_R_ERROR; _E("failed to get appid by pid"); @@ -1757,7 +1788,7 @@ static void launch_on_event_from_userevent(GDBusConnection *connection, sender_pid = __get_sender_pid(connection, sender); sender_uid = (uid_t)__get_sender_uid(connection, sender); - if (__esd_get_appid_by_pid(sender_pid, sender_uid, app_id, + if (__esd_get_appid_by_pid_for_uid(sender_pid, sender_uid, app_id, sizeof(app_id)) < 0) { _E("failed to get appid by pid"); result = ES_R_ERROR; @@ -1782,6 +1813,37 @@ out: g_dbus_method_invocation_return_value(invocation, param); } +static void get_uuid_method_call(GDBusConnection *connection, + const gchar *sender, GVariant *parameters, + GDBusMethodInvocation *invocation) +{ + GVariant *param = NULL; + int result = ES_R_OK; + char *uuid; + char app_id[128] = { 0, }; + int sender_pid; + + sender_pid = __get_sender_pid(connection, sender); + if (__esd_get_appid_by_pid(sender_pid, app_id, sizeof(app_id)) < 0) { + _E("failed to get appid by pid"); + result = ES_R_ERROR; + goto out; + } + + if (esd_cion_get_uuid_with_generate(app_id, &uuid) == 0) { + param = g_variant_new("(is)", result, uuid); + free(uuid); + } else { + result = ES_R_ERROR; + } + +out: + if (param == NULL) + param = g_variant_new("(is)", result, ""); + + g_dbus_method_invocation_return_value(invocation, param); +} + static void handle_method_call(GDBusConnection *connection, const gchar *sender, const gchar *object_path, const gchar *interface_name, const gchar *method_name, @@ -1806,8 +1868,10 @@ static void handle_method_call(GDBusConnection *connection, keep_last_data_method_call(connection, sender, parameters, invocation); } else if (g_strcmp0(method_name, "CheckLastData") == 0) { check_last_data_method_call(connection, sender, parameters, invocation); - } else if (g_strcmp0(method_name, "LaunchOnEventFromUserEvent") == 0) { + } else if (g_strcmp0(method_name, "LaunchOnEventFromUserEvent") == 0) { launch_on_event_from_userevent(connection, sender, parameters, invocation); + } else if (g_strcmp0(method_name, "GetUuid") == 0) { + get_uuid_method_call(connection, sender, parameters, invocation); } } @@ -1875,8 +1939,6 @@ static void __esd_on_name_acquired(GDBusConnection *connection, { bundle *b; - _I("name acquired(%s)", name); - __esd_check_trusted_events(connection, "ListNames"); __esd_check_trusted_events(connection, "ListActivatableNames"); @@ -1897,7 +1959,6 @@ static void __esd_on_name_acquired(GDBusConnection *connection, static void __esd_on_name_lost(GDBusConnection *connection, const gchar *name, gpointer user_data) { - _E("name lost(%s)", name); } static int __esd_before_loop(void) @@ -2417,6 +2478,9 @@ int main(int argc, char *argv[]) return ES_R_ERROR; } + if (esd_cion_db_init() < 0) + _E("db init failed!"); + g_main_loop_run(mainloop); _E("shutdown");