diff options
author | Hyunho Kang <hhstark.kang@samsung.com> | 2016-06-30 11:00:08 +0900 |
---|---|---|
committer | Hyunho Kang <hhstark.kang@samsung.com> | 2016-06-30 11:00:29 +0900 |
commit | f1e05f4e231382abdbc5a3bc2d2db355a85f17a6 (patch) | |
tree | 9a0642ca6f5ccd08f0a8aceecbeadd2f44e0bf22 /src | |
parent | 8825605008c3befb31c725a2b730f8e43ab5a061 (diff) | |
download | librua-f1e05f4e231382abdbc5a3bc2d2db355a85f17a6.tar.gz librua-f1e05f4e231382abdbc5a3bc2d2db355a85f17a6.tar.bz2 librua-f1e05f4e231382abdbc5a3bc2d2db355a85f17a6.zip |
Add multi-user featuresubmit/tizen_common/20160701.180000submit/tizen/20160630.070457accepted/tizen/wearable/20160701.032745accepted/tizen/tv/20160701.032723accepted/tizen/mobile/20160701.032824accepted/tizen/ivi/20160701.032805accepted/tizen/common/20160703.130334
Add APIs:
- rua_stat_update_for_uid
- rua_add_history_for_uid
- rua_delete_history_with_pkgname_for_uid
- rua_delete_history_with_apppath_for_uid
- rua_clear_history_for_uid
- rua_history_load_db_for_uid
- rua_is_latest_app_for_uid
- rua_db_delete_history
- rua_db_add_history
- rua_stat_get_stat_tags_for_uid
- rua_stat_db_update
Change-Id: I50b203b41a7864ae0609eaaf9125bb383c8a5b6c
Signed-off-by: Hyunho Kang <hhstark.kang@samsung.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/rua.c | 138 | ||||
-rw-r--r-- | src/rua_internal.c | 71 | ||||
-rwxr-xr-x | src/rua_stat.c | 46 | ||||
-rw-r--r-- | src/rua_stat_internal.c | 57 | ||||
-rw-r--r-- | src/rua_util.c | 79 |
5 files changed, 270 insertions, 121 deletions
@@ -22,21 +22,64 @@ /* For multi-user support */ #include <tzplatform_config.h> +#include "rua_util.h" #include "rua_internal.h" #include "rua.h" #include "db-schema.h" -int rua_delete_history_with_pkgname(char *pkg_name) +int rua_add_history_for_uid(char *pkg_name, char *app_path, char *arg, uid_t uid) { int r; - bundle *b = bundle_create(); + char time_str[32] = {0,}; + bundle *b = NULL; + + if (pkg_name == NULL || app_path == NULL) { + LOGE("invalid param"); + return -1; + } + + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + b = bundle_create(); if (b == NULL) { LOGE("bundle_create fail out of memory."); return -1; } + snprintf(time_str, sizeof(time_str), "%d", (int)time(NULL)); + bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name); + bundle_add_str(b, AUL_K_RUA_APPPATH, app_path); + bundle_add_str(b, AUL_K_RUA_ARG, arg); + bundle_add_str(b, AUL_K_RUA_TIME, time_str); + r = aul_add_rua_history_for_uid(b, uid); + LOGI("rua_add_history_for_uid result : %d ", r); + bundle_free(b); + return r; +} + +int rua_delete_history_with_pkgname(char *pkg_name) +{ + return rua_delete_history_with_pkgname_for_uid(pkg_name, getuid()); +} + +int rua_delete_history_with_pkgname_for_uid(char *pkg_name, uid_t uid) +{ + int r; + bundle *b; + + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + b = bundle_create(); + if (b == NULL) { + LOGE("bundle_create fail out of memory."); + return -1; + } bundle_add_str(b, AUL_K_RUA_PKGNAME, pkg_name); - r = aul_delete_rua_history(b); + r = aul_delete_rua_history_for_uid(b, uid); LOGI("rua_delete_history_with_pkgname result : %d ", r); bundle_free(b); return r; @@ -44,45 +87,61 @@ int rua_delete_history_with_pkgname(char *pkg_name) int rua_delete_history_with_apppath(char *app_path) { + return rua_delete_history_with_apppath_for_uid(app_path, getuid()); +} + +int rua_delete_history_with_apppath_for_uid(char *app_path, uid_t uid) +{ int r; - bundle *b = bundle_create(); + bundle *b; + + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + b = bundle_create(); if (b == NULL) { LOGE("bundle_create fail out of memory."); return -1; } - bundle_add_str(b, AUL_K_RUA_APPPATH, app_path); - r = aul_delete_rua_history(b); + r = aul_delete_rua_history_for_uid(b, uid); LOGI("rua_delete_history_with_apppath result : %d ", r); bundle_free(b); - return r; } int rua_clear_history(void) { + return rua_clear_history_for_uid(getuid()); +} + +int rua_clear_history_for_uid(uid_t uid) +{ int r; - r = aul_delete_rua_history(NULL); + + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + r = aul_delete_rua_history_for_uid(NULL, uid); LOGI("rua_clear_history result : %d ", r); return r; } int rua_history_load_db(char ***table, int *nrows, int *ncols) { + return rua_history_load_db_for_uid(table, nrows, ncols, getuid()); +} + +int rua_history_load_db_for_uid(char ***table, int *nrows, int *ncols, uid_t uid) +{ int r; char query[QUERY_MAXLEN]; char *db_err = NULL; char **db_result = NULL; sqlite3 *db = NULL; - char defname[FILENAME_MAX]; - const char *rua_db_path = tzplatform_getenv(TZ_USER_DB); - if (rua_db_path == NULL) { - LOGE("fail to get rua_db_path"); - return -1; - } - snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME); - if (table == NULL) return -1; if (nrows == NULL) @@ -90,14 +149,16 @@ int rua_history_load_db(char ***table, int *nrows, int *ncols) if (ncols == NULL) return -1; - r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL); - if (r) { - db_util_close(db); + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME); + if (r != SQLITE_OK) return -1; - } snprintf(query, QUERY_MAXLEN, - "select * from %s order by launch_time desc;", RUA_HISTORY); + "select pkg_name, app_path, arg, launch_time from %s order by launch_time desc;", RUA_HISTORY); r = sqlite3_get_table(db, query, &db_result, nrows, ncols, &db_err); @@ -136,14 +197,11 @@ int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols, db_result = table + ((row + 1) * ncols); - tmp = db_result[RUA_COL_ID]; - if (tmp) - rec->id = atoi(tmp); - tmp = db_result[RUA_COL_PKGNAME]; if (tmp) rec->pkg_name = tmp; + LOGI("get rec pkg_name %s", rec->pkg_name); tmp = db_result[RUA_COL_APPPATH]; if (tmp) rec->app_path = tmp; @@ -161,29 +219,29 @@ int rua_history_get_rec(struct rua_rec *rec, char **table, int nrows, int ncols, int rua_is_latest_app(const char *pkg_name) { + return rua_is_latest_app_for_uid(pkg_name, getuid()); +} + +int rua_is_latest_app_for_uid(const char *pkg_name, uid_t uid) +{ int r = -1; sqlite3_stmt *stmt; const unsigned char *ct; - sqlite3 *db; - - char defname[FILENAME_MAX]; - const char *rua_db_path = tzplatform_getenv(TZ_USER_DB); - if (rua_db_path == NULL) { - LOGE("fail to get rua_db_path"); - return -1; - } - snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME); + sqlite3 *db = NULL; + char *query = "select pkg_name from rua_history order by launch_time desc limit 1;"; if (!pkg_name) return -1; - r = db_util_open_with_options(defname, &db, SQLITE_OPEN_READONLY, NULL); - if (r) { - db_util_close(db); + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + r = _rua_util_open_db(&db, SQLITE_OPEN_READONLY, uid, RUA_DB_NAME); + if (r != SQLITE_OK) return -1; - } - r = sqlite3_prepare(db, Q_LATEST, sizeof(Q_LATEST), &stmt, NULL); + r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL); if (r != SQLITE_OK) { db_util_close(db); return -1; @@ -220,4 +278,4 @@ int rua_init(void) int rua_fini(void) { return 0; -}
\ No newline at end of file +} diff --git a/src/rua_internal.c b/src/rua_internal.c index 4cc5f05..6438983 100644 --- a/src/rua_internal.c +++ b/src/rua_internal.c @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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 <tzplatform_config.h> #include <db-util.h> @@ -5,6 +20,7 @@ #include "rua_internal.h" #include "db-schema.h" +#include "rua_util.h" static int __exec(sqlite3 *db, char *query) { @@ -40,19 +56,9 @@ static sqlite3 *__db_init() int r; sqlite3 *db = NULL; - char defname[FILENAME_MAX]; - const char *rua_db_path = tzplatform_getenv(TZ_USER_DB); - if (rua_db_path == NULL) { - LOGE("fail to get rua_db_path"); + r = _rua_util_open_db(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, getuid(), RUA_DB_NAME); + if (r != SQLITE_OK) return NULL; - } - snprintf(defname, sizeof(defname), "%s/%s", rua_db_path, RUA_DB_NAME); - - r = db_util_open_with_options(defname, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL); - if (r) { - db_util_close(db); - return NULL; - } r = __create_table(db); if (r) { @@ -68,7 +74,6 @@ int rua_db_delete_history(bundle *b) int r; sqlite3 *db = NULL; char query[QUERY_MAXLEN]; - char *pkg_name = NULL; char *app_path = NULL; char *errmsg = NULL; @@ -105,15 +110,12 @@ int rua_db_delete_history(bundle *b) db_util_close(db); return result; - } int rua_db_add_history(struct rua_rec *rec) { int r; - int cnt = 0; char query[QUERY_MAXLEN]; - sqlite3_stmt *stmt; sqlite3 *db = NULL; db = __db_init(); @@ -129,37 +131,12 @@ int rua_db_add_history(struct rua_rec *rec) } snprintf(query, QUERY_MAXLEN, - "select count(*) from %s where pkg_name = '%s';", RUA_HISTORY, - rec->pkg_name); - - r = sqlite3_prepare(db, query, sizeof(query), &stmt, NULL); - if (r != SQLITE_OK) { - LOGE("Error sqlite3_prepare fail"); - db_util_close(db); - return -1; - } - - r = sqlite3_step(stmt); - if (r == SQLITE_ROW) - cnt = sqlite3_column_int(stmt, 0); - - sqlite3_finalize(stmt); - - if (cnt == 0) - /* insert */ - snprintf(query, QUERY_MAXLEN, - "insert into %s ( pkg_name, app_path, arg, launch_time ) " - " values ( \"%s\", \"%s\", \"%s\", %d ) ", - RUA_HISTORY, - rec->pkg_name ? rec->pkg_name : "", - rec->app_path ? rec->app_path : "", - rec->arg ? rec->arg : "", (int)time(NULL)); - else - /* update */ - snprintf(query, QUERY_MAXLEN, - "update %s set arg='%s', launch_time='%d' where pkg_name = '%s';", - RUA_HISTORY, - rec->arg ? rec->arg : "", (int)time(NULL), rec->pkg_name); + "insert or replace into %s ( pkg_name, app_path, arg, launch_time) " + " values ( \"%s\", \"%s\", \"%s\", %d) ", + RUA_HISTORY, + rec->pkg_name ? rec->pkg_name : "", + rec->app_path ? rec->app_path : "", + rec->arg ? rec->arg : "", (int)rec->launch_time); r = __exec(db, query); if (r == -1) { diff --git a/src/rua_stat.c b/src/rua_stat.c index 78f7da4..33fdc1e 100755 --- a/src/rua_stat.c +++ b/src/rua_stat.c @@ -23,23 +23,64 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> +#include <sys/types.h> #include <db-util.h> +#include <aul.h> #include "rua_stat_internal.h" #include "rua_stat.h" +int rua_stat_update_for_uid(char *caller, char *tag, uid_t uid) +{ + int r; + bundle *b = NULL; + + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + if (caller == NULL || tag == NULL) { + LOGE("invalid param"); + return -1; + } + + b = bundle_create(); + if (b == NULL) { + LOGE("bundle_create fail out of memory."); + return -1; + } + bundle_add_str(b, AUL_SVC_K_RUA_STAT_CALLER, caller); + bundle_add_str(b, AUL_SVC_K_RUA_STAT_TAG, tag); + r = aul_update_rua_stat_for_uid(b, uid); + LOGI("rua_add_history_for_uid result : %d ", r); + bundle_free(b); + return r; +} + int rua_stat_get_stat_tags(char *caller, int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data), void *data) { + return rua_stat_get_stat_tags_for_uid(caller, rua_stat_tag_iter_fn, data, getuid()); +} + +int rua_stat_get_stat_tags_for_uid(char *caller, + int (*rua_stat_tag_iter_fn)(const char *rua_stat_tag, void *data), + void *data, uid_t uid) +{ int r; sqlite3_stmt *stmt; char query[QUERY_MAXLEN]; const unsigned char *ct; sqlite3 *db = NULL; - r = _rua_stat_init(db, SQLITE_OPEN_READONLY); + r = _rua_util_check_uid(uid); + if (r == -1) + return r; + + r = _rua_stat_init(&db, RUA_STAT_DB_NAME, SQLITE_OPEN_READONLY, uid); if (r == -1) { LOGE("__rua_stat_init fail"); return -1; @@ -72,7 +113,8 @@ out: if (stmt) sqlite3_finalize(stmt); - _rua_stat_fini(db); + if (db) + db_util_close(db); return r; } diff --git a/src/rua_stat_internal.c b/src/rua_stat_internal.c index 5fd59e6..ee83905 100644 --- a/src/rua_stat_internal.c +++ b/src/rua_stat_internal.c @@ -1,3 +1,20 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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 <unistd.h> +#include <sys/types.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -8,6 +25,7 @@ #include "db-schema.h" #include "rua_stat_internal.h" +#include "rua_util.h" int __rua_stat_insert(sqlite3 *db, char *caller, char *rua_stat_tag) { @@ -171,43 +189,20 @@ static int __create_table(sqlite3 *db) return 0; } -int _rua_stat_init(sqlite3 **db, int flags) +int _rua_stat_init(sqlite3 **db, char *db_name, int flags, uid_t uid) { - char defname[FILENAME_MAX]; - const char *rua_stat_db_path = tzplatform_getenv(TZ_USER_DB); int r; - - snprintf(defname, sizeof(defname), "%s/%s", rua_stat_db_path, RUA_STAT_DB_NAME); - r = db_util_open_with_options(defname, db, flags, NULL); - if (r) { - LOGE("db util open error(%d/%d/%d/%s)", r, - sqlite3_errcode(*db), - sqlite3_extended_errcode(*db), - sqlite3_errmsg(*db)); - return NULL; - - } - + r = _rua_util_open_db(db, flags, uid, db_name); r = __create_table(*db); if (r) { db_util_close(*db); - return NULL; + return -1; } if (*db == NULL) { - LOGW("__rua_stat_init error"); + LOGE("__rua_stat_init error"); return -1; } - - return 0; -} - -int _rua_stat_fini(sqlite3 *db) -{ - if (db) { - db_util_close(db); - db = NULL; - } return 0; } @@ -219,7 +214,7 @@ int rua_stat_db_update(char *caller, char *rua_stat_tag) LOGD("rua_stat_update start"); - r = _rua_stat_init(&db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE); + r = _rua_stat_init(&db, RUA_STAT_DB_NAME, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, getuid()); if (r == -1) { LOGE("__rua_stat_init fail"); return -1; @@ -240,7 +235,6 @@ int rua_stat_db_update(char *caller, char *rua_stat_tag) return -1; } - r = __rua_stat_lose_score_update(db, caller, rua_stat_tag); if (r != SQLITE_DONE) { LOGE("__rua_stat_lose_score_insert fail."); @@ -257,10 +251,9 @@ int rua_stat_db_update(char *caller, char *rua_stat_tag) return -1; } } - - _rua_stat_fini(db); + if (db) + db_util_close(db); LOGD("rua_stat_update done"); - return r; } diff --git a/src/rua_util.c b/src/rua_util.c new file mode 100644 index 0000000..e52ab3d --- /dev/null +++ b/src/rua_util.c @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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 <unistd.h> +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <grp.h> +#include <pwd.h> + +#include <tzplatform_config.h> +#include <sys/stat.h> +#include <db-util.h> + +#include "rua_util.h" + +#define DBPATH_LEN_MAX 4096 + +char *_rua_util_get_db_path(uid_t uid, char *db_name) +{ + struct group *grpinfo = NULL; + char db_path[DBPATH_LEN_MAX] = {0, }; + struct passwd *userinfo = getpwuid(uid); + if (userinfo == NULL) { + LOGE("getpwuid(%d) returns NULL !", uid); + return NULL; + } + grpinfo = getgrnam("users"); + if (grpinfo == NULL) { + LOGE("getgrnam(users) returns NULL !"); + return NULL; + } + + if (grpinfo->gr_gid != userinfo->pw_gid) { + LOGE("UID [%d] does not belong to 'users' group!", uid); + return NULL; + } + snprintf(db_path, sizeof(db_path), "%s/.applications/dbspace/%s", userinfo->pw_dir, db_name); + LOGD("db path %s", db_path); + return db_path; +} + +int _rua_util_open_db(sqlite3 **db, int flags, uid_t uid, char *db_name) +{ + int r; + char *db_path = _rua_util_get_db_path(uid, db_name); + r = db_util_open_with_options(db_path, db, flags, NULL); + if (r) { + LOGE("db util open error(%d/%d/%d/%s)", r, + sqlite3_errcode(*db), + sqlite3_extended_errcode(*db), + sqlite3_errmsg(*db)); + return -1; + } + return r; +} + +int _rua_util_check_uid(uid_t target_uid) +{ + uid_t uid = getuid(); + if (uid > BASE_UID && uid != target_uid) { + LOGE("Invalid UID : %d, target UID : %d", uid, target_uid); + return -1; + } + return 0; +} |