Fix widget recovery init bug 09/164209/2
authorhyunho <hhstark.kang@samsung.com>
Mon, 18 Dec 2017 05:36:50 +0000 (14:36 +0900)
committerHyunho Kang <hhstark.kang@samsung.com>
Mon, 18 Dec 2017 09:47:44 +0000 (09:47 +0000)
chown is unnecessary for widget db initialize logic and
default user is not created yet when widget db recovery logic
is started. So chown logic for default user could cause failure
of widget db initialize logic. We need to remove chown logic
from widget db initialize logic.

Change-Id: I42c17d48255c8a7833069b8b625189531707f83f
Signed-off-by: hyunho <hhstark.kang@samsung.com>
src/widget_service.c

index a9b963d1b44ab1f45862556c228c7eab6cf573f9..aad837e656d8279e0315f7f043154db56750c7be 100644 (file)
@@ -240,32 +240,66 @@ static int _check_integrity_cb(void *pid, int argc, char **argv,
        return -1;
 }
 
-static int _recover_db(sqlite3 *db, const char *path, uid_t uid)
+static void __change_smack(const char *path)
+{
+       char journal_path[PATH_MAX];
+
+       snprintf(journal_path, sizeof(journal_path), "%s-journal", path);
+       SET_SMACK_LABEL(path);
+       SET_SMACK_LABEL(journal_path);
+}
+
+static int __change_own(uid_t uid, const char *path)
 {
-       int ret;
-       char *errmsg = NULL;
        struct passwd pwd;
        struct passwd *result;
-       char buf[MAX_BUF_SIZE];
+       int ret;
        uid_t new_uid;
+       char buf[MAX_BUF_SIZE];
        char journal_path[PATH_MAX];
 
-       _I("DB recovery process start");
-       if (db)
-               sqlite3_close(db);
-       unlink(path);
-
        ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
        if (result == NULL) {
                if (ret == 0)
-                       _E("no such user: %d", uid);
+                       printf("no such user: %d", uid);
                else
-                       _E("getpwuid_r failed: %d", errno);
+                       printf("getpwuid_r failed: %d", errno);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       if (pwd.pw_uid == tzplatform_getuid(TZ_SYS_GLOBALAPP_USER))
+               new_uid = 0;
+       else
+               new_uid = pwd.pw_uid;
+
+       ret = chown(path, new_uid, pwd.pw_gid);
+       if (ret == -1) {
+               printf("chown fail %s", path);
+               return WIDGET_ERROR_FAULT;
+       }
+
+       snprintf(journal_path, sizeof(journal_path), "%s-journal", path);
+       ret = chown(journal_path, new_uid, pwd.pw_gid);
+       if (ret == -1) {
+               printf("chown fail %s", path);
                return WIDGET_ERROR_FAULT;
        }
 
+       return WIDGET_ERROR_NONE;
+}
+
+static int _recover_db(sqlite3 *db, const char *path, uid_t uid, bool is_init)
+{
+       int ret;
+       char *errmsg = NULL;
+
+       _I("DB recovery process start");
+       if (db)
+               sqlite3_close(db);
+       unlink(path);
+
        ret = sqlite3_open_v2(path, &db,
-                       SQLITE_OPEN_CREATE |SQLITE_OPEN_READWRITE,
+                       SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE,
                        NULL);
        if (ret != SQLITE_OK) {
                _E("Failed to open db[%d]", ret);
@@ -281,24 +315,11 @@ static int _recover_db(sqlite3 *db, const char *path, uid_t uid)
                return WIDGET_ERROR_FAULT;
        }
 
-       snprintf(journal_path, sizeof(journal_path), "%s-journal", path);
-       SET_SMACK_LABEL(path);
-       SET_SMACK_LABEL(journal_path);
-       if (pwd.pw_uid == tzplatform_getuid(TZ_SYS_GLOBALAPP_USER))
-               new_uid = 0;
-       else
-               new_uid = pwd.pw_uid;
-
-       ret = chown(path, new_uid, pwd.pw_gid);
-       if (ret == -1) {
-               _E("chown fail %s", path);
-               return WIDGET_ERROR_FAULT;
-       }
-
-       ret = chown(journal_path, new_uid, pwd.pw_gid);
-       if (ret == -1) {
-               _E("chown fail %s", path);
-               return WIDGET_ERROR_FAULT;
+       __change_smack(path);
+       if (!is_init) {
+               ret = __change_own(uid, path);
+               if (ret != WIDGET_ERROR_NONE)
+                       return ret;
        }
        _I("DB recovery process done");
 
@@ -369,11 +390,13 @@ static int _check_db_integrity(uid_t uid, bool is_init)
        if (path == NULL)
                return WIDGET_ERROR_FAULT;
 
+       _I("_check_db_integrity %d %d", uid, is_init);
+
        /* check table exist */
        ret = sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY, NULL);
        if (ret != SQLITE_OK) {
                /* LCOV_EXCL_START */
-               ret = _recover_db(db, path, uid);
+               ret = _recover_db(db, path, uid, is_init);
                return ret;
                /* LCOV_EXCL_STOP */
        }
@@ -381,14 +404,14 @@ static int _check_db_integrity(uid_t uid, bool is_init)
        /* check integrity */
        ret = _integrity_check(db);
        if (ret != WIDGET_ERROR_NONE) {
-               ret = _recover_db(db, path, uid);
+               ret = _recover_db(db, path, uid, is_init);
                return ret;
        }
 
        /* check table exist */
        ret = _check_table_exist(db);
        if (ret != WIDGET_ERROR_NONE) {
-               ret = _recover_db(db, path, uid);
+               ret = _recover_db(db, path, uid, is_init);
                return ret;
        }
 
@@ -402,8 +425,7 @@ EAPI int widget_service_check_db_integrity(bool is_init)
 {
        int ret;
 
-       ret = _check_db_integrity(tzplatform_getuid(TZ_SYS_GLOBALAPP_USER),
-                       is_init);
+       ret = _check_db_integrity(ROOT_USER, is_init);
        _I("check global app db result : %d", ret);
 
        ret = _check_db_integrity(tzplatform_getuid(TZ_SYS_DEFAULT_USER),