Merge "Fix static analysis" into tizen
[platform/core/appfw/librua.git] / src / rua_util.c
index 0c0f23e..ee83521 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <linux/limits.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 #include <grp.h>
 #include <pwd.h>
 
-#include <tzplatform_config.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 DBPATH_LEN_MAX 4096
-#define ERR_BUF_MAX 1024
+#define BASE_UID 5000
 
 char *_rua_util_get_db_path(uid_t uid, char *db_name)
 {
-       char db_path[DBPATH_LEN_MAX];
+       char db_path[PATH_MAX];
        const char *db_path_prefix;
 
-       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);
+       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;
-       char *db_path = _rua_util_get_db_path(uid, db_name);
+       char *db_path;
+
+       db_path = _rua_util_get_db_path(uid, db_name);
        if (db_path == NULL) {
                LOGE("out of memory _rua_util_get_db_path fail");
                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)
 {
        uid_t uid = getuid();
+
        if (uid > BASE_UID && uid != target_uid) {
                LOGE("Invalid UID : %d, target UID : %d", uid, target_uid);
                return -1;