#include <pwd.h>
#include <sys/stat.h>
-#include <db-util.h>
+#include <sqlite3.h>
+#include <tzplatform_config.h>
+#include <dlog.h>
#include "rua_util.h"
+#include "rua_private.h"
+
+#define BASE_UID 5000
char *_rua_util_get_db_path(uid_t uid, char *db_name)
{
char db_path[PATH_MAX];
+ const char *db_path_prefix;
- snprintf(db_path, sizeof(db_path), "/run/aul/dbspace/%d/%s",
+ if (strcmp(db_name, RUA_DB_NAME) == 0) {
+ snprintf(db_path, sizeof(db_path), "/run/aul/dbspace/%d/%s",
uid, db_name);
+ } else {
+ tzplatform_set_user(uid);
+ db_path_prefix = tzplatform_getenv(TZ_USER_DB);
+ tzplatform_reset_user();
+ snprintf(db_path, sizeof(db_path), "%s/%s", db_path_prefix, db_name);
+ }
LOGD("db path %s", db_path);
-
return strdup(db_path);
}
+#define BUSY_WAITING_USEC (1000000 / 10 / 2) /* 0.05 sec */
+#define BUSY_WAITING_MAX 20 /* wait for max 1 sec */
+static int __db_busy_handler(void *data, int count)
+{
+ /* sqlite3_prepare_V2 will return SQLITE_BUSY */
+ if (count >= BUSY_WAITING_MAX)
+ return 0;
+
+ usleep(BUSY_WAITING_USEC);
+
+ return 1;
+}
+
int _rua_util_open_db(sqlite3 **db, int flags, uid_t uid, char *db_name)
{
int r;
return -1;
}
- 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));
- free(db_path);
+ r = sqlite3_open_v2(db_path, db, flags, NULL);
+ free(db_path);
+ if (r != SQLITE_OK) {
+ LOGE("open db failed: %d", r);
return -1;
}
- free(db_path);
- return r;
+ r = sqlite3_busy_handler(*db, __db_busy_handler, NULL);
+ if (r != SQLITE_OK) {
+ LOGE("register busy handler failed: %s", sqlite3_errmsg(*db));
+ return -1;
+ }
+
+ return 0;
}
int _rua_util_check_uid(uid_t target_uid)