From 7b749c2d086bfd552a3689d46b8c7b5d388ce508 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 7 Sep 2016 15:02:16 +0900 Subject: [PATCH] Add APIs to set/unset alias appid - Create alias_info table - Add new APIs aul_set_alias_appid() aul_unset_laias_appid() aul_svc_set_alias_appid() aul_svc_unset_alias_appid() Change-Id: I47881716f742d940d793e18929b990de0f32acd1 Signed-off-by: Hwankyu Jhun --- data/appsvc_db.sql | 5 ++ include/aul.h | 35 +++++++++ include/aul_cmd.h | 2 + include/aul_svc.h | 23 ++++++ include/aul_svc_db.h | 7 ++ packaging/aul.spec | 2 +- scripts/101.appsvc_upgrade.sh | 9 +++ src/pkginfo.c | 53 ++++++++++++++ src/service.c | 63 ++++++++++++++++ src/service_db.c | 162 ++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 360 insertions(+), 1 deletion(-) diff --git a/data/appsvc_db.sql b/data/appsvc_db.sql index 290feee..e94bffc 100755 --- a/data/appsvc_db.sql +++ b/data/appsvc_db.sql @@ -8,4 +8,9 @@ CREATE TABLE IF NOT EXISTS appsvc ( PRIMARY KEY (operation,mime_type,uri) ); +CREATE TABLE IF NOT EXISTS alias_info ( + alias_appid TEXT NOT NULL, + appid TEXT NOT NULL, + PRIMARY KEY (alias_appid) +); diff --git a/include/aul.h b/include/aul.h index 46fd48a..27adefe 100644 --- a/include/aul.h +++ b/include/aul.h @@ -236,6 +236,10 @@ typedef enum aul_widget_instance_event { #define AUL_K_APP_DATA_KEY "__AUL_APP_DATA_KEY__" /** AUL internal private key */ #define AUL_K_TARGET_PID "__AUL_TARGET_PID__" +/** AUL internal private key */ +#define AUL_K_ALIAS_APPID "__AUL_ALIAS_APPID__" +/** AUL internal private key */ +#define AUL_K_ORG_APPID "__AUL_ORG_APPID__" /** * @brief This is callback function for aul_launch_init @@ -2905,6 +2909,37 @@ int aul_resume_pid_async(int pid); */ int aul_resume_pid_async_for_uid(int pid, uid_t uid); +/** + * @par Description: + * This API set the alias appid. + * The alias appid is only available for the aul_svc_set_appid() API. + * If the appid is not available, this API returns an error. + * + * @privlevel platform + * @privilege %http://tizen.org/privilege/systemsettings.admin + * @param[in] alias_appid an alias application ID + * @param[in] appid an application ID + * @return 0 if success, negative value(<0) if fail + * + * @remark + * This API is only available to User Session. + */ +int aul_set_alias_appid(const char *alias_appid, const char *appid); + +/** + * @par Description: + * This API unset the alias appid. + * + * @privlevel platform + * @privilege %http://tizen.org/privilege/systemsettings.admin + * @param[in] alias_appid an alias application ID + * @return 0 if success, negative value(<0) if fail + * + * @remark + * This API is only available to User Session. + */ +int aul_unset_alias_appid(const char *alias_appid); + #ifdef __cplusplus } #endif diff --git a/include/aul_cmd.h b/include/aul_cmd.h index 483a48a..63f43fe 100644 --- a/include/aul_cmd.h +++ b/include/aul_cmd.h @@ -109,6 +109,8 @@ enum app_cmd { APP_GET_LAST_CALLER_PID, APP_TERM_BY_PID_SYNC_WITHOUT_RESTART, APP_RESUME_BY_PID_ASYNC, + APP_SET_ALIAS_APPID, + APP_UNSET_ALIAS_APPID, APP_CMD_MAX }; diff --git a/include/aul_svc.h b/include/aul_svc.h index f48114b..8ae1da2 100755 --- a/include/aul_svc.h +++ b/include/aul_svc.h @@ -1132,6 +1132,29 @@ int aul_svc_set_loader_name(bundle *b, const char *loader_name); */ int aul_svc_set_background_launch(bundle *b, int enabled); +/** + * @par Description: + * This API set the alias appid. + * + * @param[in] alias_appid an alias application ID + * @param[in] appid an application ID + * @return 0 if success, negative value(<0) if fail + */ +int aul_svc_set_alias_appid(const char *alias_appid, const char *appid); +int aul_svc_set_alias_appid_for_uid(const char *alias_appid, + const char *appid, uid_t uid); + +/** + * @par Description: + * This API unset the alias appid. + * + * @param[in] alias_appid an alias application ID + * + * @return 0 if success, negative value(<0) if fail + */ +int aul_svc_unset_alias_appid(const char *alias_appid); +int aul_svc_unset_alias_appid_for_uid(const char *alias_appid, uid_t uid); + #ifdef __cplusplus } #endif diff --git a/include/aul_svc_db.h b/include/aul_svc_db.h index d8e9cd8..f380536 100755 --- a/include/aul_svc_db.h +++ b/include/aul_svc_db.h @@ -50,6 +50,13 @@ char *_svc_db_query_builder_add(char *old_query, char *op, char *uri, char *mime char *_svc_db_query_builder_build(char *old_query); int _svc_db_exec_query(const char *query, GSList **pkg_list, uid_t uid); +int _svc_db_add_alias_appid(const char *alias_appid, const char *appid, + uid_t uid); +int _svc_db_delete_alias_appid(const char *alias_appid, uid_t uid); +int _svc_db_get_appid_from_alias_info(const char *alias_appid, + char **appid, uid_t uid); + + #ifdef __cplusplus } #endif diff --git a/packaging/aul.spec b/packaging/aul.spec index 6e977f2..61e512a 100644 --- a/packaging/aul.spec +++ b/packaging/aul.spec @@ -3,7 +3,7 @@ Name: aul Summary: App utility library -Version: 0.0.300 +Version: 0.0.301 Release: 1 Group: Application Framework/Libraries License: Apache-2.0 diff --git a/scripts/101.appsvc_upgrade.sh b/scripts/101.appsvc_upgrade.sh index c4acc02..c500ee9 100755 --- a/scripts/101.appsvc_upgrade.sh +++ b/scripts/101.appsvc_upgrade.sh @@ -23,6 +23,15 @@ chown owner:users $DB_APPSVC_30-journal chsmack -a User::Home $DB_APPSVC_30 chsmack -a User::Home $DB_APPSVC_30-journal +# Create table +sqlite3 $DB_APPSVC_30 << EOF +CREATE TABLE IF NOT EXISTS alias_info ( + alias_appid TEXT NOT NULL, + appid TEXT NOT NULL, + PRIMARY KEY (alias_appid) +); +EOF + # Delete DB rm -rf $DB_APPSVC_24 rm -rf $DB_APPSVC_24-journal diff --git a/src/pkginfo.c b/src/pkginfo.c index 1741a40..32ff069 100644 --- a/src/pkginfo.c +++ b/src/pkginfo.c @@ -461,3 +461,56 @@ API int aul_app_get_last_caller_pid_for_uid(int pid, uid_t uid) return ret; } +API int aul_set_alias_appid(const char *alias_appid, const char *appid) +{ + int ret; + bundle *b; + + if (alias_appid == NULL || appid == NULL) { + _E("Invalid parameters"); + return AUL_R_EINVAL; + } + + b = bundle_create(); + if (b == NULL) { + _E("out of memory"); + return AUL_R_ERROR; + } + bundle_add(b, AUL_K_ALIAS_APPID, alias_appid); + bundle_add(b, AUL_K_APPID, appid); + + ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), + APP_SET_ALIAS_APPID, b, AUL_SOCK_NONE); + bundle_free(b); + if (ret != AUL_R_OK) + return aul_error_convert(ret); + + return AUL_R_OK; +} + +API int aul_unset_alias_appid(const char *alias_appid) +{ + int ret; + bundle *b; + + if (alias_appid == NULL) { + _E("Invalid parameter"); + return AUL_R_EINVAL; + } + + b = bundle_create(); + if (b == NULL) { + _E("out of memory"); + return AUL_R_ERROR; + } + bundle_add(b, AUL_K_ALIAS_APPID, alias_appid); + + ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), + APP_UNSET_ALIAS_APPID, b, AUL_SOCK_NONE); + bundle_free(b); + if (ret != AUL_R_OK) + return aul_error_convert(ret); + + return AUL_R_OK; +} + diff --git a/src/service.c b/src/service.c index bee8030..0d7c139 100755 --- a/src/service.c +++ b/src/service.c @@ -674,6 +674,8 @@ API int aul_svc_set_pkgname(bundle *b, const char *pkg_name) API int aul_svc_set_appid(bundle *b, const char *appid) { char *alias_id = NULL; + char *tmp_appid = NULL; + const char *org_appid; int ret; if (b == NULL || appid == NULL) { @@ -687,6 +689,22 @@ API int aul_svc_set_appid(bundle *b, const char *appid) } else { ret = __set_bundle(b, AUL_SVC_K_PKG_NAME, alias_id); free(alias_id); + alias_id = NULL; + } + + /* Get alias appid from DB */ + _svc_db_get_appid_from_alias_info(appid, &tmp_appid, getuid()); + if (tmp_appid) { + org_appid = bundle_get_val(b, AUL_SVC_K_PKG_NAME); + if (org_appid) + ret = __set_bundle(b, AUL_K_ORG_APPID, org_appid); + else + ret = __set_bundle(b, AUL_K_ORG_APPID, appid); + if (ret < 0) + _E("Failed to set %s:%d", AUL_K_ORG_APPID, ret); + + ret = __set_bundle(b, AUL_SVC_K_PKG_NAME, tmp_appid); + free(tmp_appid); } return ret; @@ -1406,3 +1424,48 @@ API int aul_svc_set_background_launch(bundle *b, int enabled) return AUL_R_OK; } +API int aul_svc_set_alias_appid(const char *alias_appid, const char *appid) +{ + return aul_svc_set_alias_appid_for_uid(alias_appid, appid, getuid()); +} + +API int aul_svc_set_alias_appid_for_uid(const char *alias_appid, + const char *appid, uid_t uid) +{ + int ret; + + ret = _svc_db_check_perm(uid, false); + if (ret < 0) { + _E("Permission error: %d", ret); + return AUL_SVC_RET_EILLACC; + } + + ret = _svc_db_add_alias_appid(alias_appid, appid, uid); + if (ret < 0) + return AUL_SVC_RET_ERROR; + + return AUL_SVC_RET_OK; +} + +API int aul_svc_unset_alias_appid(const char *alias_appid) +{ + return aul_svc_unset_alias_appid_for_uid(alias_appid, getuid()); +} + +API int aul_svc_unset_alias_appid_for_uid(const char *alias_appid, uid_t uid) +{ + int ret; + + ret = _svc_db_check_perm(uid, false); + if (ret < 0) { + _E("Permission error: %d", ret); + return AUL_SVC_RET_EILLACC; + } + + ret = _svc_db_delete_alias_appid(alias_appid, uid); + if (ret < 0) + return AUL_SVC_RET_ERROR; + + return AUL_SVC_RET_OK; +} + diff --git a/src/service_db.c b/src/service_db.c index 6f9eb01..8d84c8c 100755 --- a/src/service_db.c +++ b/src/service_db.c @@ -769,4 +769,166 @@ int _svc_db_exec_query(const char *query, GSList **pkg_list, uid_t uid) return 0; } +int _svc_db_add_alias_appid(const char *alias_appid, const char *appid, + uid_t uid) +{ + int ret; + sqlite3_stmt *stmt = NULL; + const char *query = + "INSERT OR REPLACE INTO alias_info(alias_appid, appid) " \ + "values(?,?);"; + int result = 0; + + if (alias_appid == NULL || appid == NULL) { + _E("Invalid parameters"); + return -1; + } + + if (__init(uid, false) < 0) + return -1; + + ret = sqlite3_prepare_v2(svc_db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _E("sqlite3_prepare_v2() error: %d(%s)", + ret, sqlite3_errmsg(svc_db)); + __fini(); + return ret; + } + + ret = sqlite3_bind_text(stmt, 1, alias_appid, -1, SQLITE_TRANSIENT); + if (ret != SQLITE_OK) { + _E("sqlite3_bind_text() error: %d(%s)", + ret, sqlite3_errmsg(svc_db)); + result = -1; + goto end; + } + + ret = sqlite3_bind_text(stmt, 2, appid, -1, SQLITE_TRANSIENT); + if (ret != SQLITE_OK) { + _E("sqlite3_bind_text() error: %d(%s)", + ret, sqlite3_errmsg(svc_db)); + result = -1; + goto end; + } + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _E("sqlite3_step() error: %d(%s)", + ret, sqlite3_errmsg(svc_db)); + result = -1; + goto end; + } + +end: + sqlite3_finalize(stmt); + __fini(); + + return result; +} + +int _svc_db_delete_alias_appid(const char *alias_appid, uid_t uid) +{ + int ret; + sqlite3_stmt *stmt = NULL; + const char *query = "DELETE FROM alias_info WHERE alias_appid = ?;"; + int result = 0; + + if (alias_appid == NULL) { + _E("Invalid parameter"); + return -1; + } + + if (__init(uid, false) < 0) + return -1; + + ret = sqlite3_prepare_v2(svc_db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _E("sqlite3_prepare_v2() error: %d(%s)", + ret, sqlite3_errmsg(svc_db)); + __fini(); + return -1; + } + + ret = sqlite3_bind_text(stmt, 1, alias_appid, -1, SQLITE_TRANSIENT); + if (ret != SQLITE_OK) { + _E("sqlite3_bind_text() error: %d(%s)", + ret, sqlite3_errmsg(svc_db)); + result = -1; + goto end; + } + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + _E("sqlite3_step() error: %d(%s)", + ret, sqlite3_errmsg(svc_db)); + result = -1; + } + +end: + sqlite3_finalize(stmt); + __fini(); + + return result; +} + +int _svc_db_get_appid_from_alias_info(const char *alias_appid, + char **appid, uid_t uid) +{ + int ret; + sqlite3_stmt *stmt = NULL; + const char *query = + "SELECT appid FROM alias_info WHERE alias_appid = ?;"; + const char *real_appid; + int result = 0; + + if (appid == NULL || alias_appid == NULL) { + _E("Invalid parameter"); + return -1; + } + + if (__init(uid, true) < 0) + return -1; + + ret = sqlite3_prepare_v2(svc_db, query, strlen(query), &stmt, NULL); + if (ret != SQLITE_OK) { + _E("sqlite3_prepare_v2() error: %d(%s)", + ret, sqlite3_errmsg(svc_db)); + __fini(); + return -1; + } + + ret = sqlite3_bind_text(stmt, 1, alias_appid, -1, SQLITE_TRANSIENT); + if (ret != SQLITE_OK) { + _E("sqlite3_bind_text() error: %d(%s)", + ret, sqlite3_errmsg(svc_db)); + result = -1; + goto end; + } + + ret = sqlite3_step(stmt); + if (ret != SQLITE_ROW) { + _E("sqlite3 step() error: %d(%s)", + ret, sqlite3_errmsg(svc_db)); + result = -1; + goto end; + } + + real_appid = (const char *)sqlite3_column_text(stmt, 0); + if (real_appid) { + *appid = strdup(real_appid); + if (*appid == NULL) { + _E("out of memory"); + result = -1; + goto end; + } + } + + SECURE_LOGD("alias_appid: %s, appid: %s", alias_appid, real_appid); + +end: + sqlite3_finalize(stmt); + __fini(); + + return result; +} -- 2.7.4