From 1c2cd625924b29fd07c81063d4bf834e151e3f75 Mon Sep 17 00:00:00 2001 From: "Sabera Djelti (sdi2)" Date: Thu, 30 Oct 2014 15:59:08 +0100 Subject: [PATCH] add ail_createdb & ail_syncdb The creation and the synchronisation of the global/user application database made by ail_initdb(_user) is now split up in two separate tools ail_createdb(_user) and ail_syncdb(_user) ail_initdb is an obsolete tool Change-Id: I433d70b139b686f0264c41074efd13ca61f1235d Signed-off-by: Sabera Djelti (sdi2) --- packaging/ail.spec | 8 +- tool/CMakeLists.txt | 42 ++++++++ tool/src/createdb.c | 200 ++++++++++++++++++++++++++++++++++ tool/src/createdb_user.c | 95 +++++++++++++++++ tool/src/initdb.c | 1 + tool/src/initdb_user.c | 1 + tool/src/syncdb.c | 273 +++++++++++++++++++++++++++++++++++++++++++++++ tool/src/syncdb_user.c | 201 ++++++++++++++++++++++++++++++++++ 8 files changed, 820 insertions(+), 1 deletion(-) create mode 100644 tool/src/createdb.c create mode 100644 tool/src/createdb_user.c create mode 100644 tool/src/syncdb.c create mode 100644 tool/src/syncdb_user.c diff --git a/packaging/ail.spec b/packaging/ail.spec index 2a5ce8a..36983a5 100755 --- a/packaging/ail.spec +++ b/packaging/ail.spec @@ -92,7 +92,8 @@ chown %TZ_SYS_GLOBALAPP_USER:root %{TZ_SYS_DB} chown %TZ_SYS_GLOBALAPP_USER:root %{TZ_SYS_DB} chown %TZ_SYS_GLOBALAPP_USER:root -R %{TZ_SYS_RW_ICONS} -ail_initdb 2>/dev/null +ail_createdb 2>/dev/null +ail_syncdb 2>/dev/null chsmack -a '*' %{TZ_SYS_DB}/.app_info.db* %postun @@ -104,6 +105,11 @@ fi %files %manifest %{name}.manifest %license LICENSE +%attr(06775,root,root) %{_bindir}/ail_createdb +%attr(0775,root,root) %{_bindir}/ail_createdb_user +%attr(06775,root,root) %{_bindir}/ail_syncdb +%attr(0775,root,root) %{_bindir}/ail_syncdb_user +#obsolete tools %attr(06775,root,root) %{_bindir}/ail_initdb %attr(0775,root,root) %{_bindir}/ail_initdb_user %{_bindir}/ail_fota diff --git a/tool/CMakeLists.txt b/tool/CMakeLists.txt index b174b89..e853a6d 100755 --- a/tool/CMakeLists.txt +++ b/tool/CMakeLists.txt @@ -1,5 +1,18 @@ #AIL init DB build script +SET(CREATEDB ail_createdb) +SET(CREATESRCS src/createdb.c ../src/ail_db.c) + +SET(CREATEDB_USER ail_createdb_user) +SET(CREATEUSERSRCS src/createdb_user.c ../src/ail_db.c) + +SET(SYNCDB ail_syncdb) +SET(SYNCSRCS src/syncdb.c ../src/ail_db.c) + +SET(SYNCDB_USER ail_syncdb_user) +SET(SYNCUSERSRCS src/syncdb_user.c ../src/ail_db.c) + +#obsolete tools SET(INITDB ail_initdb) SET(SRCS src/initdb.c ../src/ail_db.c) @@ -27,6 +40,35 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src) STRING(REPLACE ";" " " EXTRA_CFLAGS "${INITDB_PKGS_CFLAGS}") +ADD_EXECUTABLE(${CREATEDB} ${CREATESRCS}) +TARGET_LINK_LIBRARIES(${CREATEDB} ${LIBNAME} ${INITDB_PKGS_LIBRARIES}) +SET_TARGET_PROPERTIES(${CREATEDB} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS}") +SET_TARGET_PROPERTIES(${CREATEDB} PROPERTIES SKIP_BUILD_RPATH true) + +INSTALL(TARGETS ${CREATEDB} DESTINATION ${BINDIR}) + +ADD_EXECUTABLE(${CREATEDB_USER} ${CREATEUSERSRCS}) +TARGET_LINK_LIBRARIES(${CREATEDB_USER} ${LIBNAME} ${INITDB_PKGS_LIBRARIES}) +SET_TARGET_PROPERTIES(${CREATEDB_USER} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS}") +SET_TARGET_PROPERTIES(${CREATEDB_USER} PROPERTIES SKIP_BUILD_RPATH true) + +INSTALL(TARGETS ${CREATEDB_USER} DESTINATION ${BINDIR}) + +ADD_EXECUTABLE(${SYNCDB} ${SYNCSRCS}) +TARGET_LINK_LIBRARIES(${SYNCDB} ${LIBNAME} ${INITDB_PKGS_LIBRARIES}) +SET_TARGET_PROPERTIES(${SYNCDB} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS}") +SET_TARGET_PROPERTIES(${SYNCDB} PROPERTIES SKIP_BUILD_RPATH true) + +INSTALL(TARGETS ${SYNCDB} DESTINATION ${BINDIR}) + + +ADD_EXECUTABLE(${SYNCDB_USER} ${SYNCUSERSRCS}) +TARGET_LINK_LIBRARIES(${SYNCDB_USER} ${LIBNAME} ${INITDB_PKGS_LIBRARIES}) +SET_TARGET_PROPERTIES(${SYNCDB_USER} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS}") +SET_TARGET_PROPERTIES(${SYNCDB_USER} PROPERTIES SKIP_BUILD_RPATH true) + +INSTALL(TARGETS ${SYNCDB_USER} DESTINATION ${BINDIR}) + ADD_EXECUTABLE(${INITDB} ${SRCS}) TARGET_LINK_LIBRARIES(${INITDB} ${LIBNAME} ${INITDB_PKGS_LIBRARIES}) SET_TARGET_PROPERTIES(${INITDB} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS}") diff --git a/tool/src/createdb.c b/tool/src/createdb.c new file mode 100644 index 0000000..f203c74 --- /dev/null +++ b/tool/src/createdb.c @@ -0,0 +1,200 @@ +/* + * ail + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * Copyright (C) 2013-2014 Intel Corporation. + * + * Contact: Sabera Djelti , + * Jayoun Lee , Sewook Park , Jaeho Lee + * + * 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 "ail.h" +#include "ail_private.h" +#include "ail_db.h" + + +#ifdef _E +#undef _E +#endif +#define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg); + +#ifdef _D +#undef _D +#endif +#define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg); + +#define SET_DEFAULT_LABEL(x) \ + if(smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) _E("failed chsmack -a \"*\" %s", x) \ + else _D("chsmack -a \"*\" %s", x) + +static int createb_count_app(void) +{ + ail_filter_h filter; + ail_error_e ret; + int total = 0; + + ret = ail_filter_new(&filter); + if (ret != AIL_ERROR_OK) { + return -1; + } + + ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false); + if (ret != AIL_ERROR_OK) { + ail_filter_destroy(filter); + return -1; + } + ret = ail_filter_count_appinfo(filter, &total); + if (ret != AIL_ERROR_OK) { + ail_filter_destroy(filter); + return -1; + } + + ail_filter_destroy(filter); + + return total; +} + + +static int createdb_change_perm(const char *db_file) +{ + char buf[BUFSZE]; + char journal_file[BUFSZE]; + char *files[3]; + int ret, i; + + files[0] = (char *)db_file; + files[1] = journal_file; + files[2] = NULL; + + retv_if(!db_file, AIL_ERROR_FAIL); + + snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal"); + + for (i = 0; files[i]; i++) { + ret = chown(files[i], GLOBAL_USER, OWNER_ROOT); + if (ret == -1) { + strerror_r(errno, buf, sizeof(buf)); + _E("FAIL : chown %s %d.%d, because %s", db_file, OWNER_ROOT, OWNER_ROOT, buf); + return AIL_ERROR_FAIL; + } + + ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + if (ret == -1) { + strerror_r(errno, buf, sizeof(buf)); + _E("FAIL : chmod %s 0664, because %s", db_file, buf); + return AIL_ERROR_FAIL; + } + } + + return AIL_ERROR_OK; +} + + +static int __is_authorized() +{ + /* ail_init db should be called by as root privilege. */ + + uid_t uid = getuid(); + uid_t euid = geteuid(); + //euid need to be root to allow smack label changes during initialization + if ((uid_t) OWNER_ROOT == uid) + return 1; + else + return 0; +} + +int xsystem(const char *argv[]) +{ + int status = 0; + pid_t pid; + pid = fork(); + switch (pid) { + case -1: + perror("fork failed"); + return -1; + case 0: + /* child */ + execvp(argv[0], (char *const *)argv); + _exit(-1); + default: + /* parent */ + break; + } + if (waitpid(pid, &status, 0) == -1) { + perror("waitpid failed"); + return -1; + } + if (WIFSIGNALED(status)) { + perror("signal"); + return -1; + } + if (!WIFEXITED(status)) { + /* shouldn't happen */ + perror("should not happen"); + return -1; + } + return WEXITSTATUS(status); +} + +int main(int argc, char *argv[]) +{ + int ret; + + if (!__is_authorized()) { + fprintf(stderr, "You are not an authorized user!\n"); + _D("You are not root user!\n"); + } + else { + if(remove(APP_INFO_DB_FILE)) + _E(" %s is not removed",APP_INFO_DB_FILE); + if(remove(APP_INFO_DB_FILE_JOURNAL)) + _E(" %s is not removed",APP_INFO_DB_FILE_JOURNAL); + return -1; + } + ret = setenv("AIL_INITDB", "1", 1); + _D("AIL_INITDB : %d", ret); + setresuid(GLOBAL_USER, GLOBAL_USER, OWNER_ROOT); + + if (db_open(DB_OPEN_RW, GLOBAL_USER) != AIL_ERROR_OK) { + _E("Fail to create system databases"); + return AIL_ERROR_DB_FAILED; + } + + setuid(OWNER_ROOT); + ret = createdb_change_perm(APP_INFO_DB_FILE); + if (ret == AIL_ERROR_FAIL) { + _E("cannot chown."); + } + SET_DEFAULT_LABEL(APP_INFO_DB_FILE); + SET_DEFAULT_LABEL(APP_INFO_DB_FILE_JOURNAL); + + return AIL_ERROR_OK; +} + + + +// END diff --git a/tool/src/createdb_user.c b/tool/src/createdb_user.c new file mode 100644 index 0000000..66c936a --- /dev/null +++ b/tool/src/createdb_user.c @@ -0,0 +1,95 @@ +/* + * ail + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * Copyright (C) 2013-2014 Intel Corporation. + * + * Contact: Sabera Djelti , + * Jayoun Lee , Sewook Park , Jaeho Lee + * + * 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 "ail.h" +#include "ail_db.h" +#include "ail_private.h" + + +#ifdef _E +#undef _E +#endif +#define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg); + +#ifdef _D +#undef _D +#endif +#define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg); + +#define SET_DEFAULT_LABEL(x) \ + if(smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) _E("failed chsmack -a \"*\" %s", x) \ + else _D("chsmack -a \"*\" %s", x) + + +static int __is_authorized() +{ + /* ail_init db should be called by an user. */ + + uid_t uid = getuid(); + if ((uid_t) OWNER_ROOT != uid) + return 1; + else + return 0; +} + + +int main(int argc, char *argv[]) +{ + int ret; + + if (!__is_authorized()) { + fprintf(stderr, "You are not an authorized user!\n"); + _D("You are root user! Please switch to a regular user\n"); + return -1; + } + else { + if(remove(ail_get_app_DB(getuid()))) + _E(" %s is not removed", ail_get_app_DB(getuid())); + if(remove(ail_get_app_DB_journal(getuid()))) + _E(" %s is not removed", ail_get_app_DB_journal(getuid())); + } + + ret = setenv("AIL_INITDB", "1", 1); + _D("AIL_INITDB : %d", ret); + if (db_open(DB_OPEN_RW, getuid()) != AIL_ERROR_OK) { + _E("Fail to create system databases"); + return AIL_ERROR_DB_FAILED; + } + + return AIL_ERROR_OK; +} + + + +// END diff --git a/tool/src/initdb.c b/tool/src/initdb.c index 447e8d7..75d5788 100755 --- a/tool/src/initdb.c +++ b/tool/src/initdb.c @@ -244,6 +244,7 @@ int main(int argc, char *argv[]) if (!__is_authorized()) { fprintf(stderr, "You are not an authorized user!\n"); _D("You are not root user!\n"); + return -1; } else { if(remove(APP_INFO_DB_FILE)) diff --git a/tool/src/initdb_user.c b/tool/src/initdb_user.c index 6e076e9..b00baf7 100755 --- a/tool/src/initdb_user.c +++ b/tool/src/initdb_user.c @@ -173,6 +173,7 @@ int main(int argc, char *argv[]) if (!__is_authorized()) { fprintf(stderr, "You are not an authorized user!\n"); _D("You are root user! Please switch to a regular user\n"); + return -1; } else { if(remove(ail_get_app_DB(getuid()))) diff --git a/tool/src/syncdb.c b/tool/src/syncdb.c new file mode 100644 index 0000000..247602b --- /dev/null +++ b/tool/src/syncdb.c @@ -0,0 +1,273 @@ +/* + * ail + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * Copyright (C) 2013-2014 Intel Corporation. + * + * Contact: Sabera Djelti , + * Jayoun Lee , Sewook Park , Jaeho Lee + * + * 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 "ail.h" +#include "ail_private.h" +#include "ail_db.h" + + +#ifdef _E +#undef _E +#endif +#define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg); + +#ifdef _D +#undef _D +#endif +#define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg); + +#define SET_DEFAULT_LABEL(x) \ + if(smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) _E("failed chsmack -a \"*\" %s", x) \ + else _D("chsmack -a \"*\" %s", x) + +static int syncdb_count_app(void) +{ + ail_filter_h filter; + ail_error_e ret; + int total = 0; + + ret = ail_filter_new(&filter); + if (ret != AIL_ERROR_OK) { + return -1; + } + + ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false); + if (ret != AIL_ERROR_OK) { + ail_filter_destroy(filter); + return -1; + } + ret = ail_filter_count_appinfo(filter, &total); + if (ret != AIL_ERROR_OK) { + ail_filter_destroy(filter); + return -1; + } + + ail_filter_destroy(filter); + + return total; +} + + + +char* _desktop_to_package(const char* desktop) +{ + char *package, *tmp; + + retv_if(!desktop, NULL); + + package = strdup(desktop); + retv_if(!package, NULL); + + tmp = strrchr(package, '.'); + if(tmp == NULL) { + _E("[%s] is not a desktop file", package); + free(package); + return NULL; + } + + if (strcmp(tmp, ".desktop")) { + _E("%s is not a desktop file", desktop); + free(package); + return NULL; + } + + *tmp = '\0'; + + return package; +} + + + +int syncdb_load_directory(const char *directory) +{ + DIR *dir; + struct dirent entry, *result; + int len, ret; + char buf[BUFSZE]; + int total_cnt = 0; + int ok_cnt = 0; + + // desktop file + dir = opendir(directory); + if (!dir) { + if (strerror_r(errno, buf, sizeof(buf)) == 0) + _E("Failed to access the [%s] because %s\n", directory, buf); + return AIL_ERROR_FAIL; + } + + len = strlen(directory) + 1; + _D("Loading desktop files from %s", directory); + + for (ret = readdir_r(dir, &entry, &result); + ret == 0 && result != NULL; + ret = readdir_r(dir, &entry, &result)) { + char *package; + + if (entry.d_name[0] == '.') continue; + total_cnt++; + package = _desktop_to_package(entry.d_name); + if (!package) { + _E("Failed to convert file to package[%s]", entry.d_name); + continue; + } + + if (ail_desktop_add(package) != AIL_ERROR_OK) { + _E("Failed to add a package[%s]", package); + } else { + ok_cnt++; + } + free(package); + } + + _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt); + closedir(dir); + + return AIL_ERROR_OK; +} + + + +static int syncdb_change_perm(const char *db_file) +{ + char buf[BUFSZE]; + char journal_file[BUFSZE]; + char *files[3]; + int ret, i; + + files[0] = (char *)db_file; + files[1] = journal_file; + files[2] = NULL; + + retv_if(!db_file, AIL_ERROR_FAIL); + + snprintf(journal_file, sizeof(journal_file), "%s%s", db_file, "-journal"); + + for (i = 0; files[i]; i++) { + ret = chown(files[i], GLOBAL_USER, OWNER_ROOT); + if (ret == -1) { + strerror_r(errno, buf, sizeof(buf)); + _E("FAIL : chown %s %d.%d, because %s", db_file, OWNER_ROOT, OWNER_ROOT, buf); + return AIL_ERROR_FAIL; + } + + ret = chmod(files[i], S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + if (ret == -1) { + strerror_r(errno, buf, sizeof(buf)); + _E("FAIL : chmod %s 0664, because %s", db_file, buf); + return AIL_ERROR_FAIL; + } + } + + return AIL_ERROR_OK; +} + + +static int __is_authorized() +{ + /* ail_init db should be called by as root privilege. */ + + uid_t uid = getuid(); + uid_t euid = geteuid(); + //euid need to be root to allow smack label changes during initialization + if ((uid_t) OWNER_ROOT == uid) + return 1; + else + return 0; +} + +int xsystem(const char *argv[]) +{ + int status = 0; + pid_t pid; + pid = fork(); + switch (pid) { + case -1: + perror("fork failed"); + return -1; + case 0: + /* child */ + execvp(argv[0], (char *const *)argv); + _exit(-1); + default: + /* parent */ + break; + } + if (waitpid(pid, &status, 0) == -1) { + perror("waitpid failed"); + return -1; + } + if (WIFSIGNALED(status)) { + perror("signal"); + return -1; + } + if (!WIFEXITED(status)) { + /* shouldn't happen */ + perror("should not happen"); + return -1; + } + return WEXITSTATUS(status); +} + +int main(int argc, char *argv[]) +{ + int ret; + + if (!__is_authorized()) { + fprintf(stderr, "You are not an authorized user!\n"); + _D("You are not root user!\n"); + return -1; + } + if (access(APP_INFO_DB_FILE, F_OK)) { + fprintf(stderr, "Application database %s is missing, please use ail_createdb to create one before\n", APP_INFO_DB_FILE); + return AIL_ERROR_FAIL; + } + ret = setenv("AIL_INITDB", "1", 1); + _D("AIL_INITDB : %d", ret); + setresuid(GLOBAL_USER, GLOBAL_USER, OWNER_ROOT); + + if (db_open(DB_OPEN_RW, GLOBAL_USER) != AIL_ERROR_OK) { + _E("Fail to create system databases"); + return AIL_ERROR_DB_FAILED; + } + ret = syncdb_load_directory(USR_DESKTOP_DIRECTORY); + if (ret == AIL_ERROR_FAIL) { + _E("cannot load usr desktop directory."); + } + + return AIL_ERROR_OK; +} + + + +// END diff --git a/tool/src/syncdb_user.c b/tool/src/syncdb_user.c new file mode 100644 index 0000000..8a346a2 --- /dev/null +++ b/tool/src/syncdb_user.c @@ -0,0 +1,201 @@ +/* + * ail + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * Copyright (C) 2013-2014 Intel Corporation. + * + * Contact: Sabera Djelti , + * Jayoun Lee , Sewook Park , Jaeho Lee + * + * 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 "ail.h" +#include "ail_db.h" +#include "ail_private.h" + + +#ifdef _E +#undef _E +#endif +#define _E(fmt, arg...) fprintf(stderr, "[AIL_INITDB][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg); + +#ifdef _D +#undef _D +#endif +#define _D(fmt, arg...) fprintf(stderr, "[AIL_INITDB][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg); + +#define SET_DEFAULT_LABEL(x) \ + if(smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) _E("failed chsmack -a \"*\" %s", x) \ + else _D("chsmack -a \"*\" %s", x) + +static int syncdb_user_count_app(void) +{ + ail_filter_h filter; + ail_error_e ret; + int total = 0; + + ret = ail_filter_new(&filter); + if (ret != AIL_ERROR_OK) { + return -1; + } + + ret = ail_filter_add_bool(filter, AIL_PROP_NODISPLAY_BOOL, false); + if (ret != AIL_ERROR_OK) { + ail_filter_destroy(filter); + return -1; + } + ret = ail_filter_count_usr_appinfo(filter, &total, getuid()); + if (ret != AIL_ERROR_OK) { + ail_filter_destroy(filter); + return -1; + } + + ail_filter_destroy(filter); + + return total; +} + + + +char* _desktop_to_package(const char* desktop) +{ + char *package, *tmp; + + retv_if(!desktop, NULL); + + package = strdup(desktop); + retv_if(!package, NULL); + + tmp = strrchr(package, '.'); + if(tmp == NULL) { + _E("[%s] is not a desktop file", package); + free(package); + return NULL; + } + + if (strcmp(tmp, ".desktop")) { + _E("%s is not a desktop file", desktop); + free(package); + return NULL; + } + + *tmp = '\0'; + + return package; +} + + + +int syncdb_user_load_directory(const char *directory) +{ + DIR *dir; + struct dirent entry, *result; + int len, ret; + char buf[BUFSZE]; + int total_cnt = 0; + int ok_cnt = 0; + + // desktop file + dir = opendir(directory); + if (!dir) { + if (strerror_r(errno, buf, sizeof(buf)) == 0) + _E("Failed to access the [%s] because %s\n", directory, buf); + return AIL_ERROR_FAIL; + } + + len = strlen(directory) + 1; + _D("Loading desktop files from %s", directory); + + for (ret = readdir_r(dir, &entry, &result); + ret == 0 && result != NULL; + ret = readdir_r(dir, &entry, &result)) { + char *package; + + if (entry.d_name[0] == '.') continue; + total_cnt++; + package = _desktop_to_package(entry.d_name); + if (!package) { + _E("Failed to convert file to package[%s]", entry.d_name); + continue; + } + + if (ail_usr_desktop_add(package, getuid()) != AIL_ERROR_OK) { + _E("Failed to add a package[%s]", package); + } else { + ok_cnt++; + } + free(package); + } + + _D("Application-Desktop process : Success [%d], fail[%d], total[%d] \n", ok_cnt, total_cnt-ok_cnt, total_cnt); + closedir(dir); + + return AIL_ERROR_OK; +} + +static int __is_authorized() +{ + /* ail_init db should be called by an user. */ + + uid_t uid = getuid(); + if ((uid_t) OWNER_ROOT != uid) + return 1; + else + return 0; +} + + +int main(int argc, char *argv[]) +{ + int ret; + + if (!__is_authorized()) { + fprintf(stderr, "You are not an authorized user!\n"); + _D("You are root user! Please switch to a regular user\n"); + return -1; + } + if (access(ail_get_app_DB(getuid()), F_OK)) { + fprintf(stderr, "Application database is missing, please use ail_createdb to create one before\n"); + return AIL_ERROR_FAIL; + } + ret = setenv("AIL_INITDB", "1", 1); + _D("AIL_INITDB : %d", ret); + ret = syncdb_user_count_app(); + if (ret > 0) { + _D("Some Apps in the App Info DB."); + } + + ret = syncdb_user_load_directory(ail_get_desktop_path(getuid())); + if (ret == AIL_ERROR_FAIL) { + _E("cannot load usr desktop directory."); + } + + return AIL_ERROR_OK; +} + + + +// END -- 2.7.4