#include <sys/types.h>
#include <unistd.h>
+#include <dlog.h>
#include <tzplatform_config.h>
#include "pkgmgr-info.h"
#include "pkgmgr_parser_db.h"
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+
+#define LOG_TAG "PKG_DB_CREATOR"
+
#ifndef OWNER_ROOT
#define OWNER_ROOT 0
#endif
parser_db = getUserPkgParserDBPathUID(uid);
if (!parser_db) {
- printf("Failed to get parser db path\n");
+ LOGE("Failed to get parser db path");
return -1;
}
if (_remove_file(parser_db) != 0) {
- printf("Failed to remove parser db[%s]\n", parser_db);
+ LOGE("Failed to remove parser db[%s]", parser_db);
free(parser_db);
return -1;
}
snprintf(journal_path, sizeof(journal_path), "%s-journal", parser_db);
free(parser_db);
if (_remove_file(journal_path) != 0) {
- printf("Failed to remove journal[%s]\n", journal_path);
+ LOGE("Failed to remove journal[%s]", journal_path);
return -1;
}
cert_db = getUserPkgCertDBPath();
if (!cert_db) {
- printf("Failed to get cet db path\n");
+ LOGE("Failed to get cet db path");
return -1;
}
if (_remove_file(cert_db) != 0) {
- printf("Failed to remove parser db[%s]\n", cert_db);
+ LOGE("Failed to remove parser db[%s]", cert_db);
free(cert_db);
return -1;
}
snprintf(journal_path, sizeof(journal_path), "%s-journal", cert_db);
free(cert_db);
if (_remove_file(journal_path) != 0) {
- printf("Failed to remove journal[%s]\n", journal_path);
+ LOGE("Failed to remove journal[%s]", journal_path);
return -1;
}
int ret = 0;
if ((int)getuid() > OWNER_ROOT) {
- printf("This cmd is not allowed for regular user\n");
+ LOGE("This cmd is not allowed for regular user");
return -1;
}
if (argc == 1) {
- printf("argument should be provided\n");
+ LOGE("argument should be provided");
return -1;
}
uid = atoi(argv[1]);
ret = _remove_db((uid_t)uid);
if (ret != 0)
- printf("failed to remove database for uid[%d]\n", uid);
+ LOGE("failed to remove database for uid[%d]", uid);
ret = pkgmgr_parser_initialize_parser_db((uid_t)uid);
if (ret != 0) {
- printf("failed to create parser db for uid [%d], err[%d]\n", uid, ret);
+ LOGE("failed to create parser db for uid [%d], err[%d]", uid, ret);
return -1;
}
if (uid == 0) {
ret = pkgmgr_parser_initialize_cert_db();
if (ret != 0) {
- printf("failed to create cert db for uid [%d], err[%d]\n", uid, ret);
+ LOGE("failed to create cert db for uid [%d], err[%d]", uid, ret);
return -1;
}
}
#include <pwd.h>
#include <unistd.h>
+#include <dlog.h>
#include <sqlite3.h>
#include <tzplatform_config.h>
#include <pkgmgr_parser_db.h>
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+
+#define LOG_TAG "PKG_DB_RECOVERY"
+
#ifndef APPFW_USER
#define APPFW_USER "app_fw"
#endif
db_path = tzplatform_getenv(TZ_SYS_DB);
if (db_path == NULL) {
- printf("Failed to get TZ_SYS_DB path");
+ LOGE("Failed to get TZ_SYS_DB path");
return NULL;
}
return strdup(path);
}
+#define BUSY_WAITING_USEC (1000000 / 10 / 2) /* 0.05 sec */
+#define BUSY_WAITING_MAX 100 /* wait for max 5 sec */
+static int __db_busy_handler(void *data, int count)
+{
+ if (count < BUSY_WAITING_MAX) {
+ usleep(BUSY_WAITING_USEC);
+ return 1;
+ } else {
+ /* sqlite3_prepare_v2 will return SQLITE_BUSY */
+ return 0;
+ }
+}
+
static bool __integrity_check(const char *db_path)
{
int ret = -1;
ret = sqlite3_open_v2(db_path, &db,
SQLITE_OPEN_READWRITE, NULL);
if (ret != SQLITE_OK) {
- printf("Failed to open db\n");
+ LOGE("Failed to open db");
return false;
}
+ ret = sqlite3_busy_handler(db, __db_busy_handler, NULL);
+ if (ret != SQLITE_OK) {
+ LOGE("failed to register busy handler: %s",
+ sqlite3_errmsg(db));
+ sqlite3_close_v2(db);
+ return ret;
+ }
+
ret = sqlite3_prepare_v2(db, integrity_check_query,
strlen(integrity_check_query), &stmt, NULL);
if (ret != SQLITE_OK) {
- printf("Failed to check integrity_check : %s\n", sqlite3_errmsg(db));
+ LOGE("Failed to check integrity_check : %s", sqlite3_errmsg(db));
goto err;
}
ret = sqlite3_step(stmt);
if (ret != SQLITE_ROW) {
- printf("Failed to check integrity db :%s\n", sqlite3_errmsg(db));
+ LOGE("Failed to check integrity db :%s", sqlite3_errmsg(db));
goto err;
}
check_result = (const char *)sqlite3_column_text(stmt, 0);
if (!check_result) {
- printf("Failed to check integrity db :%s\n", sqlite3_errmsg(db));
+ LOGE("Failed to check integrity db :%s", sqlite3_errmsg(db));
goto err;
}
if (strcasecmp(check_result, "ok") != 0) {
- printf("db corrupted :%s\n", db_path);
+ LOGE("db corrupted :%s", db_path);
goto err;
}
ret = getpwnam_r(APPFW_USER, &pwd, buf, sizeof(buf), &result);
if (result == NULL) {
if (ret == 0)
- printf("no such user: %d\n", uid);
+ LOGE("no such user: %d", uid);
else
- printf("getpwnam_r failed: %d", errno);
+ LOGE("getpwnam_r failed: %d", errno);
return false;
}
uid = pwd.pw_uid;
ret = getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);
if (result == NULL) {
if (ret == 0)
- printf("no such user: %d\n", uid);
+ LOGE("no such user: %d", uid);
else
- printf("getpwuid_r failed: %d\n", errno);
+ LOGE("getpwuid_r failed: %d", errno);
return false;
}
for (i = 0; i < 2; i++) {
fd = open(files[i], O_RDONLY);
if (fd == -1) {
- printf("open %s failed: %d\n", files[i], errno);
+ LOGE("open %s failed: %d", files[i], errno);
return false;
}
ret = fstat(fd, &sb);
if (ret == -1) {
- printf("stat %s failed: %d\n", files[i], errno);
+ LOGE("stat %s failed: %d", files[i], errno);
close(fd);
return false;
}
if (S_ISLNK(sb.st_mode)) {
- printf("%s is symlink!\n", files[i]);
+ LOGE("%s is symlink!", files[i]);
close(fd);
return false;
}
ret = fchown(fd, uid, pwd.pw_gid);
if (ret == -1) {
- printf("fchown %s failed: %d\n", files[i], errno);
+ LOGE("fchown %s failed: %d", files[i], errno);
close(fd);
return false;
}
mode |= S_IWOTH;
ret = fchmod(fd, mode);
if (ret == -1) {
- printf("fchmod %s failed: %d\n", files[i], errno);
+ LOGE("fchmod %s failed: %d", files[i], errno);
close(fd);
return false;;
}
files[1] = journal_file;
if (!__change_owner(files, uid)) {
- printf("Failed to change ownership\n");
+ LOGE("Failed to change ownership");
return;
}
"%s-journal", PKGMGR_CERT_DB_FILE);
files[1] = journal_file;
if (!__change_owner(files, uid)) {
- printf("Failed to change ownership\n");
+ LOGE("Failed to change ownership");
return;
}
} else {
files[1] = journal_file;
if (!__change_owner(files, uid)) {
- printf("Failed to change ownership\n");
+ LOGE("Failed to change ownership");
return;
}
}
if (!tmp_info)
continue;
if (!__integrity_check(tmp_info->db_path)) {
- printf("User db for %d has corrupted\n", (int)tmp_info->uid);
+ LOGE("User db for %d has corrupted", (int)tmp_info->uid);
__initdb(tmp_info->uid);
}
}
db_path = tzplatform_getenv(TZ_SYS_DB);
if (db_path == NULL) {
- printf("Failed to get TZ_SYS_DB path");
+ LOGE("Failed to get TZ_SYS_DB path");
return;
}
ret = stat(abs_dirname, &stats);
if (ret != 0) {
- printf("failed to stat: %d (%s)", errno, abs_dirname);
+ LOGE("failed to stat: %d (%s)", errno, abs_dirname);
continue;
}
static void _check_db()
{
if (!__integrity_check(PKGMGR_CERT_DB_FILE)) {
- printf("Cert db corrupted. restore entire pkgmgr db\n");
+ LOGE("Cert db corrupted. restore entire pkgmgr db");
__initdb_all();
return;
} else if (!__integrity_check(PKGMGR_PARSER_DB_FILE)) {
- printf("Global parser db corrupted. restore entire pkgmgr db\n");
+ LOGE("Global parser db corrupted. restore entire pkgmgr db");
__initdb_all();
return;
}
int main(int argc, char *argv[])
{
if ((int)getuid() > REGULAR_USER) {
- printf("This cmd is not allowed for regular user\n");
+ LOGE("This cmd is not allowed for regular user");
return -1;
}