X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=tool%2Fsrc%2Finitdb.c;h=1df643e7716f975df14b1957f5e018d5d5890fce;hb=refs%2Ftags%2Fsubmit%2Ftizen_common%2F20151229.144031;hp=75d5788b8022b02e26b79de9305f54349be51c8f;hpb=9241f15a201da5ea3e02b95f15325f7aa83f64bf;p=platform%2Fcore%2Fappfw%2Fail.git diff --git a/tool/src/initdb.c b/tool/src/initdb.c index 75d5788..1df643e 100755 --- a/tool/src/initdb.c +++ b/tool/src/initdb.c @@ -19,14 +19,15 @@ * */ - - +#define _GNU_SOURCE #include #include #include #include #include #include +#include +#include #include #include @@ -34,51 +35,25 @@ #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); +#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 _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 initdb_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) + do { \ + if (smack_setlabel((x), "*", SMACK_LABEL_ACCESS)) \ + _E("failed chsmack -a \"*\" %s", x); \ + else \ + _D("chsmack -a \"*\" %s", x); \ + } while (0) + +char *_desktop_to_package(const char* desktop) { char *package, *tmp; @@ -88,7 +63,7 @@ char* _desktop_to_package(const char* desktop) retv_if(!package, NULL); tmp = strrchr(package, '.'); - if(tmp == NULL) { + if (tmp == NULL) { _E("[%s] is not a desktop file", package); free(package); return NULL; @@ -105,18 +80,16 @@ char* _desktop_to_package(const char* desktop) return package; } - - int initdb_load_directory(const char *directory) { DIR *dir; struct dirent entry, *result; - int len, ret; + int ret; char buf[BUFSZE]; int total_cnt = 0; int ok_cnt = 0; - // desktop file + /* desktop file */ dir = opendir(directory); if (!dir) { if (strerror_r(errno, buf, sizeof(buf)) == 0) @@ -124,7 +97,6 @@ int initdb_load_directory(const char *directory) return AIL_ERROR_FAIL; } - len = strlen(directory) + 1; _D("Loading desktop files from %s", directory); for (ret = readdir_r(dir, &entry, &result); @@ -140,11 +112,11 @@ int initdb_load_directory(const char *directory) continue; } - if (ail_desktop_add(package) != AIL_ERROR_OK) { + if (ail_desktop_add(package) != AIL_ERROR_OK) _E("Failed to add a package[%s]", package); - } else { + else ok_cnt++; - } + free(package); } @@ -154,11 +126,8 @@ int initdb_load_directory(const char *directory) return AIL_ERROR_OK; } - - static int initdb_change_perm(const char *db_file) { - char buf[BUFSZE]; char journal_file[BUFSZE]; char *files[3]; int ret, i; @@ -174,15 +143,13 @@ static int initdb_change_perm(const char *db_file) 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); + _E("FAIL : chown %s %d.%d, because %d", db_file, OWNER_ROOT, OWNER_ROOT, errno); 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); + _E("FAIL : chmod %s 0664, because %d", db_file, errno); return AIL_ERROR_FAIL; } } @@ -190,14 +157,12 @@ static int initdb_change_perm(const char *db_file) 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 + /* euid need to be root to allow smack label changes during initialization */ + /* uid_t euid = geteuid(); */ if ((uid_t) OWNER_ROOT == uid) return 1; else @@ -245,37 +210,35 @@ int main(int argc, char *argv[]) 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)) - _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); + } 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); } ret = setenv("AIL_INITDB", "1", 1); _D("AIL_INITDB : %d", ret); - setresuid(GLOBAL_USER, GLOBAL_USER, OWNER_ROOT); + + if (setresuid(GLOBAL_USER, GLOBAL_USER, OWNER_ROOT) != 0) + _E("setresuid() is failed."); if (db_open(DB_OPEN_RW, GLOBAL_USER) != AIL_ERROR_OK) { _E("Fail to create system databases"); return AIL_ERROR_DB_FAILED; } ret = initdb_load_directory(USR_DESKTOP_DIRECTORY); - if (ret == AIL_ERROR_FAIL) { + if (ret == AIL_ERROR_FAIL) _E("cannot load usr desktop directory."); - } - setuid(OWNER_ROOT); + if (setuid(OWNER_ROOT) != 0) + _E("setuid() is failed."); + ret = initdb_change_perm(APP_INFO_DB_FILE); - if (ret == AIL_ERROR_FAIL) { + 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