error.
Change-Id: I85833d74b5f0652c0f7c48882d1e08c3170c0449
Signed-off-by: saerome.kim <saerome.kim@samsung.com>
src/ua-manager-request-handler.c
src/ua-manager-event-sender.c
src/ua-manager-core.c
+ src/ua-manager-db.c
src/ua-manager-device-db.c
src/ua-manager-user-db.c
src/ua-manager-service-db.c
extern "C" {
#endif
+#define SQLITE_BUSY_TIMEOUT 500000
+
+#ifndef DATABASE_FULL_PATH
+#define DATABASE_FULL_PATH "/opt/usr/dbspace/.ua-manager-data.db"
+#endif
+
+/* Helper Functions */
+int _ua_db_initialize_once(void);
+
/* USER QUERIES */
typedef struct db_user_info {
--- /dev/null
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License")
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+
+#include <sqlite3.h>
+#include <dlfcn.h>
+
+#include "ua-manager-common.h"
+#include "ua-manager-database.h"
+
+#define UAM_DB_USERDATA_TABLE "userdata" /**< User data DB table name */
+#define UAM_DB_DEVICES_TABLE "devices" /**< Device DB table name */
+#define UAM_DB_SERVICES_TABLE "services" /**< Service DB table name */
+#define UAM_DB_DEVICE_SERVICES_TABLE "device_services" /**< Device services DB table name */
+
+#define CREATE_USERDATA_TABLE "CREATE TABLE IF NOT EXISTS userdata ( " \
+ "name TEXT, " \
+ "user_id INTEGER, " \
+ "account TEXT PRIMARY KEY " \
+ "); "
+
+#define CREATE_DEVICES_TABLE "CREATE TABLE IF NOT EXISTS devices ( " \
+ "device_number INTEGER, " \
+ "device_id TEXT, " \
+ "user_id INTEGER, " \
+ "tech_type INTEGER, " \
+ "address TEXT, " \
+ "ip_address TEXT, " \
+ "timestamp LONG, " \
+ "presence_state INTEGER, " \
+ "os_type INTEGER, " \
+ "discriminant INTETER, " \
+ "FOREIGN KEY(user_id) REFERENCES userdata(user_id), " \
+ "PRIMARY KEY(device_id, tech_type, address) " \
+ "); "
+
+#define CREATE_SERVICES_TABLE "CREATE TABLE IF NOT EXISTS services ( " \
+ "service_number INTEGER, " \
+ "service_name TEXT PRIMARY KEY, " \
+ "cycle INTEGER " \
+ "); "
+
+#define CREATE_DEVICE_SERVICES_TABLE "CREATE TABLE IF NOT EXISTS device_services ( " \
+ "device_number INTEGER, " \
+ "service_number INTEGER, " \
+ "FOREIGN KEY(device_number) REFERENCES devices(device_number), " \
+ "FOREIGN KEY(service_number) REFERENCES services(service_number), " \
+ "PRIMARY KEY(device_number, service_number) " \
+ "); "
+
+sqlite3 *database;
+
+static int __ua_db_busy(void *user, int attempts)
+{
+ FUNC_ENTRY;
+ UAM_ERR("DB locked by another process, attempts number %d",
+ attempts);
+
+ usleep(SQLITE_BUSY_TIMEOUT); /* wait for a half second*/
+ FUNC_EXIT;
+ return 1;
+}
+
+static int __uam_db_check_integrity_cb(void *err, int count, char **data, char **columns)
+{
+ FUNC_ENTRY;
+ int i;
+
+ for (i = 0; i < count; i++) {
+ UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
+ if (!g_strcmp0(columns[i], "integrity_check") && !g_strcmp0(data[i], "ok")) return 0;
+ }
+ return 1;
+}
+
+static int __uam_db_check_table_cb(void *err, int count, char **data, char **columns)
+{
+ FUNC_ENTRY;
+ int i;
+
+ for (i = 0; i < count; i++) {
+ UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
+ if (!g_strcmp0(columns[i], "count") && !g_strcmp0(data[i], "1")) return 0;
+ }
+ return 1;
+}
+
+static int __check_integrity(void)
+{
+ FUNC_ENTRY;
+ int res = 0;
+ char *sql = NULL;
+
+ res = sqlite3_open(DATABASE_FULL_PATH, &database);
+ if (res != SQLITE_OK) {
+ UAM_ERR("Error in opening database %s: %s", DATABASE_FULL_PATH, sqlite3_errmsg(database));
+ FUNC_EXIT;
+ return UAM_ERROR_DB_FAILED;
+ }
+ UAM_DBG("Successfully opened database");
+
+ size_t db_size = _uam_getFilesize(DATABASE_FULL_PATH);
+ if (db_size == 0) {
+ UAM_ERR("Database size is 0");
+ FUNC_EXIT;
+ return UAM_ERROR_DB_FAILED;
+ }
+ UAM_DBG("Database size: %d", db_size);
+
+ res = sqlite3_exec(database, "PRAGMA integrity_check", __uam_db_check_integrity_cb, 0, 0);
+ if (res != SQLITE_OK) {
+ UAM_ERR("Can't verify database integrity %s",
+ sqlite3_errmsg(database));
+ sqlite3_close(database);
+ database = NULL;
+ FUNC_EXIT;
+ return UAM_ERROR_DB_FAILED;
+ }
+ UAM_DBG("Successfully verified database integrity");
+
+ sql = sqlite3_mprintf(
+ "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='%s'",
+ UAM_DB_USERDATA_TABLE);
+
+ res = sqlite3_exec(database, sql, __uam_db_check_table_cb, 0, 0);
+ sqlite3_free(sql);
+
+ if (res != SQLITE_OK) {
+ UAM_ERR("Can't verify table creation %s",
+ sqlite3_errmsg(database));
+ sqlite3_close(database);
+ database = NULL;
+ FUNC_EXIT;
+ return UAM_ERROR_DB_FAILED;
+ }
+ UAM_DBG("Successfully verified %s table creation", UAM_DB_USERDATA_TABLE);
+
+ sql = sqlite3_mprintf(
+ "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='%s'",
+ UAM_DB_DEVICES_TABLE);
+
+ res = sqlite3_exec(database, sql, __uam_db_check_table_cb, 0, 0);
+ sqlite3_free(sql);
+
+ if (res != SQLITE_OK) {
+ UAM_ERR("Can't verify table creation %s",
+ sqlite3_errmsg(database));
+ sqlite3_close(database);
+ database = NULL;
+ FUNC_EXIT;
+ return UAM_ERROR_DB_FAILED;
+ }
+ UAM_DBG("Successfully verified %s table creation", UAM_DB_DEVICES_TABLE);
+
+ sql = sqlite3_mprintf(
+ "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='%s'",
+ UAM_DB_SERVICES_TABLE);
+
+ res = sqlite3_exec(database, sql, __uam_db_check_table_cb, 0, 0);
+ sqlite3_free(sql);
+
+ if (res != SQLITE_OK) {
+ UAM_ERR("Can't verify table creation %s",
+ sqlite3_errmsg(database));
+ sqlite3_close(database);
+ database = NULL;
+ FUNC_EXIT;
+ return UAM_ERROR_DB_FAILED;
+ }
+ UAM_DBG("Successfully verified %s table creation", UAM_DB_SERVICES_TABLE);
+
+ sql = sqlite3_mprintf(
+ "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='%s'",
+ UAM_DB_DEVICE_SERVICES_TABLE);
+
+ res = sqlite3_exec(database, sql, __uam_db_check_table_cb, 0, 0);
+ sqlite3_free(sql);
+
+ if (res != SQLITE_OK) {
+ UAM_ERR("Can't verify table creation %s",
+ sqlite3_errmsg(database));
+ sqlite3_close(database);
+ database = NULL;
+ FUNC_EXIT;
+ return UAM_ERROR_DB_FAILED;
+ }
+ UAM_DBG("Successfully verified %s table creation", UAM_DB_DEVICE_SERVICES_TABLE);
+
+ res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
+ if (res != SQLITE_OK) {
+ UAM_ERR("Can't set locking mode %s, skip set busy handler.",
+ sqlite3_errmsg(database));
+ sqlite3_close(database);
+ database = NULL;
+ FUNC_EXIT;
+ return UAM_ERROR_DB_FAILED;
+ }
+
+ FUNC_EXIT;
+ return UAM_ERROR_NONE;
+}
+
+static bool __is_table_existing(const char *table)
+{
+ int ret;
+ char *sql;
+ int count;
+ bool result;
+
+ sql = sqlite3_mprintf(
+ "SELECT count(*) FROM sqlite_master WHERE type='table' AND name ='%s';", table);
+ if (NULL == sql) {
+ UAM_ERR("sqlite3_mprintf failed");
+ return false;
+ }
+ sqlite3_stmt *stmt = NULL;
+
+ ret = sqlite3_prepare_v2(database, sql, strlen(sql), &stmt, NULL);
+ if (SQLITE_OK != ret) {
+ UAM_ERR("sqlite3_prepare_v2 failed, [%d:%s]", ret, sqlite3_errmsg(database));
+ return false;
+ }
+
+ ret = sqlite3_step(stmt);
+ if (SQLITE_ROW != ret) {
+ UAM_ERR("sqlite3_step failed, [%d:%s]", ret, sqlite3_errmsg(database));
+ return false;
+ }
+
+ count = sqlite3_column_int(stmt, 0);
+ if (count > 0)
+ result = true;
+ else
+ result = false;
+
+ sqlite3_finalize(stmt);
+ sqlite3_free(sql);
+
+ return result;
+}
+
+static int __ua_create_table(const char *table_name)
+{
+ int sql_ret;
+ char *sql = NULL;
+ char *error = NULL;
+ int ret = UAM_ERROR_NONE;
+
+ if (database == NULL) {
+ UAM_ERR("database is NULL");
+ return UAM_ERROR_DB_FAILED;
+ }
+
+ sql = sqlite3_mprintf(table_name);
+ if (NULL == sql) {
+ UAM_ERR("sql is NULL");
+ ret = UAM_ERROR_DB_FAILED;
+ }
+
+ sql_ret = sqlite3_exec(database, sql, NULL, NULL, &error);
+ if (sql_ret != SQLITE_OK) {
+ UAM_ERR("sqlite3_exec failed, %d, %s", sql_ret, error);
+ ret = UAM_ERROR_DB_FAILED;
+ sqlite3_free(error);
+ }
+ sqlite3_free(sql);
+
+ return ret;
+}
+
+static void __ua_prepare_table(void)
+{
+ if (!__is_table_existing(UAM_DB_USERDATA_TABLE))
+ __ua_create_table(CREATE_USERDATA_TABLE);
+
+ if (!__is_table_existing(UAM_DB_DEVICES_TABLE))
+ __ua_create_table(CREATE_DEVICES_TABLE);
+
+ if (!__is_table_existing(UAM_DB_SERVICES_TABLE))
+ __ua_create_table(CREATE_SERVICES_TABLE);
+
+ if (!__is_table_existing(UAM_DB_DEVICE_SERVICES_TABLE))
+ __ua_create_table(CREATE_DEVICE_SERVICES_TABLE);
+}
+
+int _ua_db_initialize_once(void)
+{
+ FUNC_ENTRY;
+ int sql_ret = SQLITE_OK;
+ char *error = NULL;
+
+ sql_ret = sqlite3_open_v2(DATABASE_FULL_PATH, &(database),
+ SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
+ if (SQLITE_OK != sql_ret) {
+ UAM_ERR("sqlite3_open_v2 failed, (%d)", sql_ret);
+ return UAM_ERROR_DB_FAILED;
+ }
+
+ if (UAM_ERROR_NONE != __check_integrity()) {
+ UAM_ERR("Failed to check integrity");
+ unlink(DATABASE_FULL_PATH);
+ database = NULL;
+ sql_ret = sqlite3_open_v2(DATABASE_FULL_PATH, &database,
+ SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
+ if (SQLITE_OK != sql_ret) {
+ UAM_ERR("sqlite3_open_v2 failed, (%d)", sql_ret);
+ database = NULL;
+ return UAM_ERROR_DB_FAILED;
+ }
+ }
+
+ __ua_prepare_table();
+
+ /* Enable persist journal mode */
+ sql_ret = sqlite3_exec(database, "PRAGMA journal_mode = PERSIST", NULL, NULL, &error);
+ if (SQLITE_OK != sql_ret) {
+ UAM_ERR("Fail to change journal mode: (%d) %s", sql_ret, error);
+ sqlite3_free(error);
+ sqlite3_close(database);
+ database = NULL;
+ return UAM_ERROR_DB_FAILED;
+ }
+
+ /* Set how many times we'll repeat our attempts for sqlite_step */
+ if (SQLITE_OK != sqlite3_busy_handler(database, __ua_db_busy, NULL))
+ UAM_ERR("Couldn't set busy handler!");
+
+ return UAM_ERROR_NONE;
+}
+
/* INSERT statements */
static sqlite3_stmt *insert_device_info;
-static sqlite3 *database;
+extern sqlite3 *database;
static int max_device_number;
-static int __ua_device_db_busy(void *user, int attempts)
-{
- FUNC_ENTRY;
- UAM_ERR("DB locked by another process, attempts number %d",
- attempts);
-
- usleep(SQLITE_BUSY_TIMEOUT); /* wait for a half second*/
- FUNC_EXIT;
- return 1;
-}
-
-static int __uam_db_check_integrity_cb(void *err, int count, char **data, char **columns)
-{
- FUNC_ENTRY;
- int i;
-
- for (i = 0; i < count; i++) {
- UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
- if (!g_strcmp0(columns[i], "integrity_check") && !g_strcmp0(data[i], "ok")) return 0;
- }
- return 1;
-}
-
-static int __uam_db_check_table_cb(void *err, int count, char **data, char **columns)
-{
- FUNC_ENTRY;
- int i;
-
- for (i = 0; i < count; i++) {
- UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
- if (!g_strcmp0(columns[i], "count") && !g_strcmp0(data[i], "1")) return 0;
- }
- return 1;
-}
-
-static int __ua_device_db_initialize_once(void)
-{
- FUNC_ENTRY;
- int res = 0;
-
- if (database != NULL) {
- FUNC_EXIT;
- return UAM_ERROR_NONE;
- }
-
- res = sqlite3_open(DATABASE_FULL_PATH, &database);
- if (res != SQLITE_OK) {
- // TODO : Can add logic of retry if not able to open
- UAM_ERR("Error in opening database %s: %s", DATABASE_FULL_PATH, sqlite3_errmsg(database));
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Successfully opened database");
-
- size_t db_size = _uam_getFilesize(DATABASE_FULL_PATH);
- if (db_size == 0) {
- UAM_ERR("Database size is 0");
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Database size: %d", db_size);
-
- res = sqlite3_exec(database, "PRAGMA integrity_check", __uam_db_check_integrity_cb, 0, 0);
- if (res != SQLITE_OK) {
- UAM_ERR("Can't verify database integrity %s",
- sqlite3_errmsg(database));
- sqlite3_close(database);
- database = NULL;
- FUNC_EXIT;
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Successfully verified database integrity");
-
- res = sqlite3_exec(database,
- "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='devices'",
- __uam_db_check_table_cb, 0, 0);
- if (res != SQLITE_OK) {
- UAM_ERR("Can't verify table creation %s",
- sqlite3_errmsg(database));
- sqlite3_close(database);
- database = NULL;
- FUNC_EXIT;
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Successfully verified table creation");
-
- res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
- if (res != SQLITE_OK) {
- UAM_ERR("Can't set locking mode %s, skip set busy handler.",
- sqlite3_errmsg(database));
- sqlite3_close(database);
- database = NULL;
- FUNC_EXIT;
- return UAM_ERROR_DB_FAILED;
- }
-
- /* Set how many times we'll repeat our attempts for sqlite_step */
- if (sqlite3_busy_handler(database, __ua_device_db_busy, NULL)
- != SQLITE_OK)
- UAM_ERR("Couldn't set busy handler!");
-
- FUNC_EXIT;
- return UAM_ERROR_NONE;
-}
-
static int __ua_device_prepare_delete(sqlite3 *db)
{
FUNC_ENTRY;
static sqlite3 *__ua_device_db_get_database(void)
{
if (database == NULL)
- __ua_device_db_initialize_once();
+ _ua_db_initialize_once();
return database;
}
int _ua_device_db_initialize(void)
{
FUNC_ENTRY;
- database = NULL;
-
- __ua_device_db_initialize_once();
EXEC(UAM_ERROR_NONE, __ua_device_table_devicesinfo_prepare(database));
if (_uam_db_get_max_device_number() != UAM_ERROR_NONE)
/* INSERT statements */
static sqlite3_stmt *insert_device_service;
-
-static sqlite3 *database;
-
-static int __ua_device_service_db_busy(void *user, int attempts)
-{
- FUNC_ENTRY;
- UAM_ERR("DB locked by another process, attempts number %d",
- attempts);
-
- usleep(SQLITE_BUSY_TIMEOUT); /* wait for a half second*/
- FUNC_EXIT;
- return 1;
-}
-
-static int __uam_db_check_integrity_cb(void *err, int count, char **data, char **columns)
-{
- FUNC_ENTRY;
- int i;
-
- for (i = 0; i < count; i++) {
- UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
- if (!g_strcmp0(columns[i], "integrity_check") && !g_strcmp0(data[i], "ok")) return 0;
- }
- return 1;
-}
-
-static int __uam_db_check_table_cb(void *err, int count, char **data, char **columns)
-{
- FUNC_ENTRY;
- int i;
-
- for (i = 0; i < count; i++) {
- UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
- if (!g_strcmp0(columns[i], "count") && !g_strcmp0(data[i], "1")) return 0;
- }
- return 1;
-}
-
-static int __ua_device_service_db_initialize_once(void)
-{
- FUNC_ENTRY;
- int res = 0;
-
- if (database != NULL) {
- FUNC_EXIT;
- return UAM_ERROR_NONE;
- }
-
- res = sqlite3_open(DATABASE_FULL_PATH, &database);
- if (res != SQLITE_OK) {
- // TODO : Can add logic of retry if not able to open
- UAM_ERR("Error in opening database %s: %s", DATABASE_FULL_PATH, sqlite3_errmsg(database));
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Successfully opened database");
-
- size_t db_size = _uam_getFilesize(DATABASE_FULL_PATH);
- if (db_size == 0) {
- UAM_ERR("Database size is 0");
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Database size: %d", db_size);
-
- res = sqlite3_exec(database, "PRAGMA integrity_check", __uam_db_check_integrity_cb, 0, 0);
- if (res != SQLITE_OK) {
- UAM_ERR("Can't verify database integrity %s",
- sqlite3_errmsg(database));
- sqlite3_close(database);
- database = NULL;
- FUNC_EXIT;
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Successfully verified database integrity");
-
- res = sqlite3_exec(database,
- "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='device_services'",
- __uam_db_check_table_cb, 0, 0);
- if (res != SQLITE_OK) {
- UAM_ERR("Can't verify table creation %s",
- sqlite3_errmsg(database));
- sqlite3_close(database);
- database = NULL;
- FUNC_EXIT;
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Successfully verified table creation");
-
- res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
- if (res != SQLITE_OK) {
- UAM_ERR("Can't set locking mode %s, skip set busy handler.",
- sqlite3_errmsg(database));
- sqlite3_close(database);
- database = NULL;
- FUNC_EXIT;
- return UAM_ERROR_DB_FAILED;
- }
-
- /* Set how many times we'll repeat our attempts for sqlite_step */
- if (sqlite3_busy_handler(database, __ua_device_service_db_busy, NULL)
- != SQLITE_OK)
- UAM_ERR("Couldn't set busy handler!");
-
- FUNC_EXIT;
- return UAM_ERROR_NONE;
-}
+extern sqlite3 *database;
static int __ua_device_service_prepare_delete(sqlite3 *db)
{
int _ua_device_service_db_initialize(void)
{
FUNC_ENTRY;
- database = NULL;
-
- __ua_device_service_db_initialize_once();
EXEC(UAM_ERROR_NONE, __ua_device_service_table_deviceservicesinfo_prepare(database));
static sqlite3 *__ua_device_service_db_get_database(void)
{
if (database == NULL)
- __ua_device_service_db_initialize_once();
+ _ua_db_initialize_once();
return database;
}
/* INSERT statements */
static sqlite3_stmt *insert_service_info;
-static sqlite3 *database;
+extern sqlite3 *database;
static int max_service_number;
-static int __ua_service_db_busy(void *user, int attempts)
-{
- FUNC_ENTRY;
- UAM_ERR("DB locked by another process, attempts number %d",
- attempts);
-
- usleep(SQLITE_BUSY_TIMEOUT); /* wait for a half second*/
- FUNC_EXIT;
- return 1;
-}
-
-static int __uam_db_check_integrity_cb(void *err, int count, char **data, char **columns)
-{
- FUNC_ENTRY;
- int i;
-
- for (i = 0; i < count; i++) {
- UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
- if (!g_strcmp0(columns[i], "integrity_check") && !g_strcmp0(data[i], "ok")) return 0;
- }
- return 1;
-}
-
-static int __uam_db_check_table_cb(void *err, int count, char **data, char **columns)
-{
- FUNC_ENTRY;
- int i;
-
- for (i = 0; i < count; i++) {
- UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
- if (!g_strcmp0(columns[i], "count") && !g_strcmp0(data[i], "1")) return 0;
- }
- return 1;
-}
-
-static int __ua_service_db_initialize_once(void)
-{
- FUNC_ENTRY;
- int res = 0;
-
- if (database != NULL) {
- FUNC_EXIT;
- return UAM_ERROR_NONE;
- }
-
- res = sqlite3_open(DATABASE_FULL_PATH, &database);
- if (res != SQLITE_OK) {
- // TODO : Can add logic of retry if not able to open
- UAM_ERR("Error in opening database %s: %s", DATABASE_FULL_PATH, sqlite3_errmsg(database));
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Successfully opened database");
-
- size_t db_size = _uam_getFilesize(DATABASE_FULL_PATH);
- if (db_size == 0) {
- UAM_ERR("Database size is 0");
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Database size: %d", db_size);
-
- res = sqlite3_exec(database, "PRAGMA integrity_check", __uam_db_check_integrity_cb, 0, 0);
- if (res != SQLITE_OK) {
- UAM_ERR("Can't verify database integrity %s",
- sqlite3_errmsg(database));
- sqlite3_close(database);
- database = NULL;
- FUNC_EXIT;
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Successfully verified database integrity");
-
- res = sqlite3_exec(database,
- "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='services'",
- __uam_db_check_table_cb, 0, 0);
- if (res != SQLITE_OK) {
- UAM_ERR("Can't verify table creation %s",
- sqlite3_errmsg(database));
- sqlite3_close(database);
- database = NULL;
- FUNC_EXIT;
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Successfully verified table creation");
-
- res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
- if (res != SQLITE_OK) {
- UAM_ERR("Can't set locking mode %s, skip set busy handler.",
- sqlite3_errmsg(database));
- sqlite3_close(database);
- database = NULL;
- FUNC_EXIT;
- return UAM_ERROR_DB_FAILED;
- }
-
- /* Set how many times we'll repeat our attempts for sqlite_step */
- if (sqlite3_busy_handler(database, __ua_service_db_busy, NULL)
- != SQLITE_OK)
- UAM_ERR("Couldn't set busy handler!");
-
- FUNC_EXIT;
- return UAM_ERROR_NONE;
-}
-
static int __ua_service_prepare_delete(sqlite3 *db)
{
FUNC_ENTRY;
static sqlite3 *__ua_service_db_get_database(void)
{
if (database == NULL)
- __ua_service_db_initialize_once();
+ _ua_db_initialize_once();
return database;
}
int _ua_service_db_initialize(void)
{
FUNC_ENTRY;
- database = NULL;
-
- __ua_service_db_initialize_once();
EXEC(UAM_ERROR_NONE, __ua_service_table_servicesinfo_prepare(database));
if (_uam_db_get_max_service_number() != UAM_ERROR_NONE)
*
*/
-#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdbool.h>
+
+#include <sqlite3.h>
#include <dlfcn.h>
#include "ua-manager-common.h"
/* INSERT statements */
static sqlite3_stmt *insert_user_info;
-static sqlite3 *database;
+sqlite3 *database;
static int max_user_id;
-static int __ua_db_busy(void *user, int attempts)
-{
- FUNC_ENTRY;
- UAM_ERR("DB locked by another process, attempts number %d",
- attempts);
-
- usleep(SQLITE_BUSY_TIMEOUT); /* wait for a half second*/
- FUNC_EXIT;
- return 1;
-}
-
-static int __uam_db_check_integrity_cb(void *err, int count, char **data, char **columns)
-{
- FUNC_ENTRY;
- int i;
-
- for (i = 0; i < count; i++) {
- UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
- if (!g_strcmp0(columns[i], "integrity_check") && !g_strcmp0(data[i], "ok")) return 0;
- }
- return 1;
-}
-
-static int __uam_db_check_table_cb(void *err, int count, char **data, char **columns)
-{
- FUNC_ENTRY;
- int i;
-
- for (i = 0; i < count; i++) {
- UAM_DBG("%s = %s\n", columns[i], data[i] ? data[i] : "NULL");
- if (!g_strcmp0(columns[i], "count") && !g_strcmp0(data[i], "1")) return 0;
- }
- return 1;
-}
-
-static int __ua_db_initialize_once(void)
-{
- FUNC_ENTRY;
- int res = 0;
-
- if (database != NULL) {
- FUNC_EXIT;
- return UAM_ERROR_NONE;
- }
-
- res = sqlite3_open(DATABASE_FULL_PATH, &database);
- if (res != SQLITE_OK) {
- // TODO : Can add logic of retry if not able to open
- UAM_ERR("Error in opening database %s: %s", DATABASE_FULL_PATH, sqlite3_errmsg(database));
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Successfully opened database");
-
- size_t db_size = _uam_getFilesize(DATABASE_FULL_PATH);
- if (db_size == 0) {
- UAM_ERR("Database size is 0");
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Database size: %d", db_size);
-
- res = sqlite3_exec(database, "PRAGMA integrity_check", __uam_db_check_integrity_cb, 0, 0);
- if (res != SQLITE_OK) {
- UAM_ERR("Can't verify database integrity %s",
- sqlite3_errmsg(database));
- sqlite3_close(database);
- database = NULL;
- FUNC_EXIT;
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Successfully verified database integrity");
-
- res = sqlite3_exec(database,
- "SELECT count(*) as count FROM sqlite_master WHERE type='table' AND name='userdata'",
- __uam_db_check_table_cb, 0, 0);
- if (res != SQLITE_OK) {
- UAM_ERR("Can't verify table creation %s",
- sqlite3_errmsg(database));
- sqlite3_close(database);
- database = NULL;
- FUNC_EXIT;
- return UAM_ERROR_DB_FAILED;
- }
- UAM_DBG("Successfully verified table creation");
-
- res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0);
- if (res != SQLITE_OK) {
- UAM_ERR("Can't set locking mode %s, skip set busy handler.",
- sqlite3_errmsg(database));
- sqlite3_close(database);
- database = NULL;
- FUNC_EXIT;
- return UAM_ERROR_DB_FAILED;
- }
-
- /* Set how many times we'll repeat our attempts for sqlite_step */
- if (sqlite3_busy_handler(database, __ua_db_busy, NULL)
- != SQLITE_OK)
- UAM_ERR("Couldn't set busy handler!");
-
- FUNC_EXIT;
- return UAM_ERROR_NONE;
-}
-
static int __ua_prepare_delete(sqlite3 *db)
{
FUNC_ENTRY;
static sqlite3 *__ua_db_get_database(void)
{
if (database == NULL)
- __ua_db_initialize_once();
+ _ua_db_initialize_once();
return database;
}
FUNC_ENTRY;
database = NULL;
- __ua_db_initialize_once();
+ if (UAM_ERROR_NONE != _ua_db_initialize_once())
+ goto handle_error;
EXEC(UAM_ERROR_NONE, __ua_table_usersinfo_prepare(database));
if (_ua_device_db_initialize() != UAM_ERROR_NONE)
goto handle_error;
+
if (_ua_service_db_initialize() != UAM_ERROR_NONE)
goto handle_error;
+
if (_ua_device_service_db_initialize() != UAM_ERROR_NONE)
goto handle_error;