summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorInkyun Kil <inkyun.kil@samsung.com>2019-05-28 18:21:16 +0900
committerInkyun Kil <inkyun.kil@samsung.com>2019-06-13 15:47:30 +0900
commit0cb41c9756760da9b86dc9f2dd049c1c9cda1af1 (patch)
tree7608bb6d2b832b14068645551b0f005540bdb2a1
parent65e9c06ccad0e49bb7e1dfd93026ec6db8acb321 (diff)
downloadalarm-manager-0cb41c9756760da9b86dc9f2dd049c1c9cda1af1.tar.gz
alarm-manager-0cb41c9756760da9b86dc9f2dd049c1c9cda1af1.tar.bz2
alarm-manager-0cb41c9756760da9b86dc9f2dd049c1c9cda1af1.zip
Arrange db codes
Change-Id: I16f965adfcc13aeb1a59d083e4124fadee1f43b1 Signed-off-by: Inkyun Kil <inkyun.kil@samsung.com>
-rwxr-xr-xinclude/alarm-internal.h10
-rw-r--r--lib/CMakeLists.txt1
-rwxr-xr-xserver/CMakeLists.txt1
-rw-r--r--server/alarm-manager-db.c (renamed from server/alarm-manager-registry.c)307
-rwxr-xr-xserver/alarm-manager-db.h46
-rwxr-xr-xserver/alarm-manager.c306
-rwxr-xr-xunittest/alarmmanager_unittest.cpp2
7 files changed, 359 insertions, 314 deletions
diff --git a/include/alarm-internal.h b/include/alarm-internal.h
index 17f5f92..3d0368a 100755
--- a/include/alarm-internal.h
+++ b/include/alarm-internal.h
@@ -25,7 +25,6 @@
extern "C" {
#endif
-#include "alarm.h"
#include <glib.h>
#include <dlog.h>
#include <bundle.h>
@@ -33,6 +32,8 @@ extern "C" {
#include <notification.h>
#include <gio/gio.h>
+#include "alarm.h"
+
#define INIT_ALARM_LIST_SIZE 64
#define INIT_SCHEDULED_ALARM_LIST_SIZE 32
#define MAX_BUNDLE_NAME_LEN 2048
@@ -90,7 +91,6 @@ bool _send_alarm_get_next_duetime(alarm_context_t context, alarm_id_t alarm_id,
bool _send_alarm_get_all_info(alarm_context_t context, char ** db_path, int *error_code);
bool _send_alarm_reset(alarm_context_t context, int *error_code);
bool _remove_from_scheduled_alarm_list(uid_t uid, alarm_id_t alarm_id);
-void _load_alarms_from_registry();
bundle *_send_alarm_get_appsvc_info(alarm_context_t context, alarm_id_t alarm_id, int *error_code);
notification_h _send_alarm_get_noti_info(alarm_context_t context, alarm_id_t alarm_id, int *error_code);
bool _send_alarm_set_rtc_time(alarm_context_t context, alarm_date_t *time, int *error_code);
@@ -171,10 +171,6 @@ bool _alarm_schedule();
bool _clear_scheduled_alarm_list();
bool _add_to_scheduled_alarm_list(__alarm_info_t *__alarm_info);
-bool _save_alarms(__alarm_info_t *__alarm_info);
-bool _delete_alarms(alarm_id_t alarm_id);
-bool _update_alarms(__alarm_info_t *__alarm_info);
-
bool _alarm_set_timer(__alarm_server_context_t *alarm_context, int timer, time_t due_time);
bool _alarm_disable_timer(__alarm_server_context_t alarm_context);
bool _init_scheduled_alarm_list();
@@ -182,8 +178,6 @@ bool _init_scheduled_alarm_list();
void _alarm_initialize();
time_t _get_periodic_alarm_standard_time(void);
-void _update_db_for_disabled_alarm(alarm_id_t alarm_id, bool disabled_by_ups);
-
#define ALARM_MGR_LOG_PRINT(FMT, ARG...) LOGD(FMT, ##ARG)
#define ALARM_MGR_EXCEPTION_PRINT(FMT, ARG...) LOGW(FMT, ##ARG)
#define ALARM_MGR_ASSERT_PRINT(FMT, ARG...) LOGE(FMT, ##ARG)
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index f7f0bc3..081c3bc 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -1,5 +1,6 @@
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/common)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/server)
AUX_SOURCE_DIRECTORY(./ LIB_SRCS)
SET(LIB_SRCS ${LIB_SRCS} ${CMAKE_SOURCE_DIR}/common/alarm-mgr-stub.c)
diff --git a/server/CMakeLists.txt b/server/CMakeLists.txt
index cc80cef..eb2c121 100755
--- a/server/CMakeLists.txt
+++ b/server/CMakeLists.txt
@@ -1,5 +1,6 @@
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/common)
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/server)
AUX_SOURCE_DIRECTORY(./ SRCS)
SET(SRCS ${SRCS} ${CMAKE_SOURCE_DIR}/common/alarm-mgr-stub.c)
diff --git a/server/alarm-manager-registry.c b/server/alarm-manager-db.c
index 0e80c80..5999469 100644
--- a/server/alarm-manager-registry.c
+++ b/server/alarm-manager-db.c
@@ -22,24 +22,68 @@
#include <sqlite3.h>
#include <glib.h>
+#include <tzplatform_config.h>
#if !GLIB_CHECK_VERSION(2, 31, 0)
#include <glib/gmacros.h>
#endif
-#include "alarm.h"
-#include "alarm-internal.h"
+#include "alarm-manager-db.h"
#define MAX_GCONF_PATH_LEN 256
-extern GSList *g_disabled_alarm_list;
+#define ALARMMGR_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".alarmmgr.db")
+#define QUERY_CREATE_TABLE_ALARMMGR "create table if not exists alarmmgr \
+ (alarm_id integer primary key,\
+ start integer,\
+ end integer,\
+ uid integer,\
+ pid integer,\
+ global integer,\
+ is_disabled integer,\
+ caller_pkgid text,\
+ callee_pkgid text,\
+ app_unique_name text,\
+ app_service_name text,\
+ app_service_name_mod text,\
+ bundle text, \
+ noti_len integer,\
+ noti text, \
+ year integer,\
+ month integer,\
+ day integer,\
+ hour integer,\
+ min integer,\
+ sec integer,\
+ msec integer,\
+ day_of_week integer,\
+ repeat integer,\
+ alarm_type integer,\
+ reserved_info integer,\
+ dst_service_name text, \
+ dst_service_name_mod text \
+ )"
+extern GSList *g_disabled_alarm_list;
extern __alarm_server_context_t alarm_context;
-extern sqlite3 *alarmmgr_db;
-bool _save_alarms(__alarm_info_t *__alarm_info);
-bool _update_alarms(__alarm_info_t *__alarm_info);
-bool _delete_alarms(alarm_id_t alarm_id);
-void _load_alarms_from_registry(void);
+sqlite3 *alarmmgr_db;
+static bool is_db_corrupted = false;
+
+static int __db_busyhandler(void *pData, int count)
+{
+ if (5 - count > 0) {
+ struct timespec time = {
+ .tv_sec = 0,
+ .tv_nsec = (count + 1) * 100 * 1000 * 1000
+ };
+ nanosleep(&time, NULL);
+ ALARM_MGR_LOG_PRINT("alarmmgr_db: busy handler called. count: %d", count + 1);
+ return 1;
+ } else {
+ ALARM_MGR_LOG_PRINT("alarmmgr_db: busy handler will return SQLITE_BUSY error");
+ return 0;
+ }
+}
bool _save_alarms(__alarm_info_t *__alarm_info)
{
@@ -164,7 +208,7 @@ bool _delete_alarms(alarm_id_t alarm_id)
return true;
}
-void _load_alarms_from_registry()
+void _load_alarms_from_db()
{
int i = 0;
int col_idx;
@@ -276,6 +320,160 @@ done:
return;
}
+int _get_db_path_for_all_info(uid_t uid, char** db_path)
+{
+ sqlite3 *alarmmgr_tool_db;
+ char db_path_tmp[50] = {0,};
+ const char *query_for_creating_table = "create table alarmmgr_tool \
+ (alarm_id integer primary key,\
+ duetime_epoch integer,\
+ duetime text,\
+ start_epoch integer,\
+ end_epoch integer,\
+ pid integer,\
+ global integer,\
+ caller_pkgid text,\
+ callee_pkgid text,\
+ app_unique_name text,\
+ app_service_name text,\
+ dst_service_name text,\
+ day_of_week integer,\
+ repeat integer,\
+ alarm_type integer)";
+ const char *query_for_deleting_table = "drop table alarmmgr_tool";
+ time_t current_time = 0;
+ struct tm current_tm;
+ GSList *gs_iter = NULL;
+ __alarm_info_t *entry = NULL;
+ char *error_message = NULL;
+ int index = 0;
+
+ /* Open a DB */
+ time(&current_time);
+ localtime_r(&current_time, &current_tm);
+ snprintf(db_path_tmp, sizeof(db_path_tmp), "/tmp/alarmmgr_%d%d%d_%02d%02d%02d.db",
+ current_tm.tm_year + 1900, current_tm.tm_mon + 1, current_tm.tm_mday, current_tm.tm_hour, current_tm.tm_min, current_tm.tm_sec);
+ *db_path = strdup(db_path_tmp);
+ if (*db_path == NULL) {
+ ALARM_MGR_EXCEPTION_PRINT("Out of memory");
+ return ERR_ALARM_SYSTEM_FAIL;
+ }
+
+ if (sqlite3_open(db_path_tmp, &alarmmgr_tool_db) != SQLITE_OK) {
+ ALARM_MGR_EXCEPTION_PRINT("Failed to open [%s]", db_path_tmp);
+ return ERR_ALARM_SYSTEM_FAIL;
+ }
+
+ /* Register busy handler */
+ if (sqlite3_busy_handler(alarmmgr_tool_db, __db_busyhandler, NULL) != SQLITE_OK) {
+ ALARM_MGR_EXCEPTION_PRINT("Failed to register the busy handler");
+ sqlite3_close(alarmmgr_tool_db);
+ return ERR_ALARM_SYSTEM_FAIL;
+ }
+
+ /* Drop a table */
+ if (sqlite3_exec(alarmmgr_tool_db, query_for_deleting_table, NULL, NULL, &error_message) != SQLITE_OK) {
+ ALARM_MGR_EXCEPTION_PRINT("Deleting the table is failed. error message = %s", error_message);
+ sqlite3_free(error_message);
+ }
+
+ /* Create a table if it does not exist */
+ if (sqlite3_exec(alarmmgr_tool_db, query_for_creating_table, NULL, NULL, &error_message) != SQLITE_OK) {
+ ALARM_MGR_EXCEPTION_PRINT("Creating the table is failed. error message = %s", error_message);
+ sqlite3_free(error_message);
+ sqlite3_close(alarmmgr_tool_db);
+ return ERR_ALARM_SYSTEM_FAIL;
+ }
+
+ /* Get information of all alarms and save those into the DB. */
+ for (gs_iter = alarm_context.alarms; gs_iter != NULL; gs_iter = g_slist_next(gs_iter)) {
+ entry = (__alarm_info_t*)gs_iter->data;
+ if (uid >= REGULAR_UID_MIN && entry->uid != uid)
+ continue;
+ ++index;
+ SECURE_LOGD("#%d alarm id[%d] app_name[%s] duetime[%ld]",
+ index, entry->alarm_id, entry->app_unique_name, entry->start);
+
+ alarm_info_t *alarm_info = (alarm_info_t *) &(entry->alarm_info);
+ alarm_mode_t *mode = &alarm_info->mode;
+
+ char *query = sqlite3_mprintf("insert into alarmmgr_tool( alarm_id, duetime_epoch, duetime, start_epoch,\
+ end_epoch, pid, global, caller_pkgid, callee_pkgid, app_unique_name, app_service_name, dst_service_name, day_of_week, repeat, alarm_type)\
+ values (%d,%d,%Q,%d,%d,%d,%d,%Q,%Q,%Q,%Q,%Q,%d,%d,%d)",
+ entry->alarm_id,
+ (int)entry->due_time,
+ ctime(&(entry->due_time)),
+ (int)entry->start,
+ (int)entry->end,
+ (int)entry->pid,
+ (bool)entry->global,
+ CHECK_NULL_STRING(entry->caller_pkgid),
+ CHECK_NULL_STRING(entry->callee_pkgid),
+ CHECK_NULL_STRING(entry->app_unique_name),
+ CHECK_NULL_STRING(entry->app_service_name),
+ CHECK_NULL_STRING(entry->dst_service_name),
+ mode->u_interval.day_of_week,
+ mode->repeat,
+ entry->alarm_info.alarm_type);
+
+ if (sqlite3_exec(alarmmgr_tool_db, query, NULL, NULL, &error_message) != SQLITE_OK) {
+ SECURE_LOGE("sqlite3_exec() is failed. error message = %s", error_message);
+ sqlite3_free(error_message);
+ }
+
+ sqlite3_free(query);
+ }
+
+ sqlite3_close(alarmmgr_tool_db);
+ return ALARMMGR_RESULT_SUCCESS;
+}
+
+gboolean _update_relative_alarms(gpointer user_data)
+{
+ GSList *iter = NULL;
+ __alarm_info_t *entry = NULL;
+ char *error_message = NULL;
+
+ if (sqlite3_exec(alarmmgr_db, "BEGIN EXCLUSIVE", NULL, NULL, &error_message) != SQLITE_OK) {
+ SECURE_LOGE("sqlite3_exec() is failed. error message = %s", error_message);
+ sqlite3_free(error_message);
+ return false;
+ }
+
+ for (iter = alarm_context.alarms; iter != NULL; iter = g_slist_next(iter)) {
+ entry = (__alarm_info_t *)iter->data;
+ alarm_info_t *alarm_info = &(entry->alarm_info);
+ if (alarm_info->alarm_type & ALARM_TYPE_RELATIVE)
+ _update_alarms(entry);
+ }
+
+ if (sqlite3_exec(alarmmgr_db, "COMMIT", NULL, NULL, &error_message) != SQLITE_OK) {
+ SECURE_LOGE("sqlite3_exec() is failed. error message = %s", error_message);
+ sqlite3_free(error_message);
+ return false;
+ }
+
+ return false;
+}
+
+bool _alarm_set_global_to_db(__alarm_info_t *alarm_info, bool global)
+{
+
+ char *error_message = NULL;
+ char *query = sqlite3_mprintf("update alarmmgr set global=%d where alarm_id=%d",
+ alarm_info->global, alarm_info->alarm_id);
+
+ if (SQLITE_OK != sqlite3_exec(alarmmgr_db, query, NULL, NULL, &error_message)) {
+ SECURE_LOGE("sqlite3_exec() is failed. query = %s, error message = %s", query, error_message);
+ sqlite3_free(query);
+ sqlite3_free(error_message);
+ return false;
+ }
+
+ sqlite3_free(query);
+ return true;
+}
+
void _update_db_for_disabled_alarm(alarm_id_t alarm_id, bool disabled)
{
char *error_message = NULL;
@@ -291,3 +489,94 @@ void _update_db_for_disabled_alarm(alarm_id_t alarm_id, bool disabled)
sqlite3_free(query);
return;
}
+
+int __check_callback(void *pid, int argc, char **argv, char **notUsed2)
+{
+ if (strcmp(argv[0], "ok") != 0) {
+ ALARM_MGR_EXCEPTION_PRINT("check integrity result : %s" , argv[0]);
+ is_db_corrupted = true;
+ return -1;
+ } else {
+ ALARM_MGR_LOG_PRINT("check integrity result : %s" , argv[0]);
+ }
+
+ return 0;
+}
+
+bool _initialize_db()
+{
+ char *error_message = NULL;
+ int ret;
+
+ /* Create or Open the DB file */
+ ret = sqlite3_open(ALARMMGR_DB_FILE, &alarmmgr_db);
+ if (ret != SQLITE_OK) {
+ ALARM_MGR_EXCEPTION_PRINT("Failed to open [%s]. error: %s", ALARMMGR_DB_FILE, sqlite3_errmsg(alarmmgr_db));
+ if (ret == SQLITE_CORRUPT)
+ goto recover;
+ else
+ return false;
+ }
+ /* Register busy handler */
+ ret = sqlite3_busy_handler(alarmmgr_db, __db_busyhandler, NULL);
+ if (ret != SQLITE_OK) {
+ ALARM_MGR_EXCEPTION_PRINT("Failed to register the busy handler");
+ if (ret == SQLITE_CORRUPT) {
+ goto recover;
+ } else {
+ sqlite3_close(alarmmgr_db);
+ return false;
+ }
+ }
+ /* Create alarmmgr table */
+ ret = sqlite3_exec(alarmmgr_db, QUERY_CREATE_TABLE_ALARMMGR, NULL, NULL, &error_message);
+ if (ret != SQLITE_OK) {
+ ALARM_MGR_EXCEPTION_PRINT("Don't execute query = %s, error message = %s", QUERY_CREATE_TABLE_ALARMMGR, error_message);
+ if (ret == SQLITE_CORRUPT || ret == SQLITE_NOTADB) {
+ goto recover;
+ } else {
+ sqlite3_close(alarmmgr_db);
+ sqlite3_free(error_message);
+ return false;
+ }
+ }
+
+ /* Check integrity of DB */
+ ret = sqlite3_exec(alarmmgr_db, "PRAGMA integrity_check", __check_callback, NULL, 0);
+ if (ret != SQLITE_OK || is_db_corrupted) {
+ ALARM_MGR_EXCEPTION_PRINT("Loss alarm db's integrity");
+ goto recover;
+ }
+
+ return true;
+
+recover:
+ if (alarmmgr_db)
+ sqlite3_close(alarmmgr_db);
+ if (error_message)
+ sqlite3_free(error_message);
+ unlink(ALARMMGR_DB_FILE);
+
+ ret = sqlite3_open(ALARMMGR_DB_FILE, &alarmmgr_db);
+ if (ret != SQLITE_OK) {
+ ALARM_MGR_EXCEPTION_PRINT("[recover] Failed to open [%s]. error: %s", ALARMMGR_DB_FILE, sqlite3_errmsg(alarmmgr_db));
+ return false;
+ }
+
+ ret = sqlite3_busy_handler(alarmmgr_db, __db_busyhandler, NULL);
+ if (ret != SQLITE_OK) {
+ ALARM_MGR_EXCEPTION_PRINT("[recover] Failed to register the busy handler");
+ sqlite3_close(alarmmgr_db);
+ return false;
+ }
+
+ ret = sqlite3_exec(alarmmgr_db, QUERY_CREATE_TABLE_ALARMMGR, NULL, NULL, &error_message);
+ if (ret != SQLITE_OK) {
+ ALARM_MGR_EXCEPTION_PRINT("[recover] Don't execute query = %s, error message = %s", QUERY_CREATE_TABLE_ALARMMGR, error_message);
+ sqlite3_close(alarmmgr_db);
+ sqlite3_free(error_message);
+ return false;
+ }
+
+ return true;
+}
diff --git a/server/alarm-manager-db.h b/server/alarm-manager-db.h
new file mode 100755
index 0000000..44fd4d3
--- /dev/null
+++ b/server/alarm-manager-db.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2019 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.
+ */
+#pragma once
+
+#define MAX_SNOOZE_CNT 5
+#define REPEAT_MODE_ONCE 0x80
+
+#define SIG_TIMER 0x32
+#define ALARM_INFO_MAX 100
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <glib.h>
+
+#include "alarm.h"
+#include "alarm-internal.h"
+
+bool _alarm_set_global_to_db(__alarm_info_t *alarm_info, bool global);
+bool _delete_alarms(alarm_id_t alarm_id);
+int _get_db_path_for_all_info(uid_t uid, char** db_path);
+bool _initialize_db();
+void _load_alarms_from_db();
+bool _save_alarms(__alarm_info_t *__alarm_info);
+bool _update_alarms(__alarm_info_t *__alarm_info);
+gboolean _update_relative_alarms(gpointer user_data);
+void _update_db_for_disabled_alarm(alarm_id_t alarm_id, bool disabled_by_ups);
+
+#ifdef __cplusplus
+}
+#endif
+
diff --git a/server/alarm-manager.c b/server/alarm-manager.c
index 81d2c47..5f76f52 100755
--- a/server/alarm-manager.c
+++ b/server/alarm-manager.c
@@ -46,7 +46,6 @@
#include <notification_ipc.h>
#include <notification_internal.h>
#include <system_info.h>
-#include <sqlite3.h>
#include <cert-svc/ccert.h>
#include <cert-svc/cinstance.h>
#include <cynara-session.h>
@@ -61,6 +60,7 @@
#include "gio/gio.h"
#include "alarm.h"
#include "alarm-internal.h"
+#include "alarm-manager-db.h"
#include "alarm-mgr-stub.h"
@@ -118,12 +118,10 @@ static int log_fd = 0;
#define ALARM_MGR_DBUS_NAME "org.tizen.alarm.manager"
static AlarmManager *interface;
-sqlite3 *alarmmgr_db;
bool is_time_changed = false; /* for calculating next duetime */
#define BILLION 1000000000 /* for calculating nano seconds */
static time_t periodic_alarm_standard_time = 0;
-static bool is_db_corrupted = false;
static int __is_ui_app(const char *appid, uid_t uid);
static long __get_proper_interval(long interval, int alarm_type);
@@ -157,13 +155,11 @@ static void __initialize_scheduled_alarm_list();
static bool __initialize_noti();
static bool __initialize_dbus();
-static bool __initialize_db();
void _release_alarm_info_t(__alarm_info_t *entry);
void on_bus_name_owner_changed(GDBusConnection *connection, const gchar *sender_name, const gchar *object_path,
const gchar *interface_name, const gchar *signal_name, GVariant *parameters, gpointer user_data);
bool __get_caller_unique_name(int pid, char *unique_name, int size, bool *is_app, uid_t uid);
static bool __permit_by_config(pkgmgrinfo_appinfo_h handle, uid_t uid);
-static int __db_busyhandler(void *pData, int count);
static notification_h __get_notification(guchar *data, int datalen);
#ifdef _APPFW_FEATURE_ALARM_MANAGER_MODULE_LOG
@@ -705,34 +701,6 @@ static bool __alarm_set_start_and_end_time(alarm_info_t *alarm_info,
return true;
}
-gboolean __update_relative_alarms(gpointer user_data)
-{
- GSList *iter = NULL;
- __alarm_info_t *entry = NULL;
- char *error_message = NULL;
-
- if (sqlite3_exec(alarmmgr_db, "BEGIN EXCLUSIVE", NULL, NULL, &error_message) != SQLITE_OK) {
- SECURE_LOGE("sqlite3_exec() is failed. error message = %s", error_message);
- sqlite3_free(error_message);
- return false;
- }
-
- for (iter = alarm_context.alarms; iter != NULL; iter = g_slist_next(iter)) {
- entry = (__alarm_info_t *)iter->data;
- alarm_info_t *alarm_info = &(entry->alarm_info);
- if (alarm_info->alarm_type & ALARM_TYPE_RELATIVE)
- _update_alarms(entry);
- }
-
- if (sqlite3_exec(alarmmgr_db, "COMMIT", NULL, NULL, &error_message) != SQLITE_OK) {
- SECURE_LOGE("sqlite3_exec() is failed. error message = %s", error_message);
- sqlite3_free(error_message);
- return false;
- }
-
- return false;
-}
-
static bool __alarm_update_due_time_of_all_items_in_list(double diff_time)
{
time_t current_time;
@@ -814,7 +782,7 @@ static bool __alarm_update_due_time_of_all_items_in_list(double diff_time)
is_time_changed = false;
alarm_context.c_due_time = min_time;
- g_idle_add(__update_relative_alarms, NULL);
+ g_idle_add(_update_relative_alarms, NULL);
return true;
}
@@ -1329,24 +1297,6 @@ static bool __alarm_update(uid_t uid, int pid, alarm_id_t alarm_id,
return true;
}
-static bool __alarm_set_global_to_db(__alarm_info_t *alarm_info, bool global)
-{
-
- char *error_message = NULL;
- char *query = sqlite3_mprintf("update alarmmgr set global=%d where alarm_id=%d",
- alarm_info->global, alarm_info->alarm_id);
-
- if (SQLITE_OK != sqlite3_exec(alarmmgr_db, query, NULL, NULL, &error_message)) {
- SECURE_LOGE("sqlite3_exec() is failed. query = %s, error message = %s", query, error_message);
- sqlite3_free(query);
- sqlite3_free(error_message);
- return false;
- }
-
- sqlite3_free(query);
- return true;
-}
-
static bool __alarm_delete(uid_t uid, alarm_id_t alarm_id, int *error_code)
{
@@ -3876,123 +3826,25 @@ gboolean alarm_manager_alarm_get_next_duetime(AlarmManager *pObject, GDBusMethod
gboolean alarm_manager_alarm_get_all_info(AlarmManager *pObject, GDBusMethodInvocation *invoc, gpointer user_data)
{
- sqlite3 *alarmmgr_tool_db;
- char *db_path = NULL;
- char db_path_tmp[50] = {0,};
- time_t current_time = 0;
- struct tm current_tm;
- const char *query_for_creating_table = "create table alarmmgr_tool \
- (alarm_id integer primary key,\
- duetime_epoch integer,\
- duetime text,\
- start_epoch integer,\
- end_epoch integer,\
- pid integer,\
- global integer,\
- caller_pkgid text,\
- callee_pkgid text,\
- app_unique_name text,\
- app_service_name text,\
- dst_service_name text,\
- day_of_week integer,\
- repeat integer,\
- alarm_type integer)";
- const char *query_for_deleting_table = "drop table alarmmgr_tool";
int return_code = ALARMMGR_RESULT_SUCCESS;
- GSList *gs_iter = NULL;
- __alarm_info_t *entry = NULL;
- char *error_message = NULL;
uid_t uid;
- int index = 0;
const char *name = g_dbus_method_invocation_get_sender(invoc);
+ char *db_path = NULL;
uid = __get_caller_uid(name);
if (uid < 0) {
return_code = ERR_ALARM_SYSTEM_FAIL;
- g_dbus_method_invocation_return_value(invoc, g_variant_new("(si)", db_path, return_code));
+ g_dbus_method_invocation_return_value(invoc, g_variant_new("(si)", NULL, return_code));
return true;
}
- /* Open a DB */
- time(&current_time);
- localtime_r(&current_time, &current_tm);
- snprintf(db_path_tmp, sizeof(db_path_tmp), "/tmp/alarmmgr_%d%d%d_%02d%02d%02d.db",
- current_tm.tm_year + 1900, current_tm.tm_mon + 1, current_tm.tm_mday, current_tm.tm_hour, current_tm.tm_min, current_tm.tm_sec);
- db_path = strdup(db_path_tmp);
-
- if (sqlite3_open(db_path, &alarmmgr_tool_db) != SQLITE_OK) {
- ALARM_MGR_EXCEPTION_PRINT("Failed to open [%s]", db_path);
- return_code = ERR_ALARM_SYSTEM_FAIL;
- goto done;
- }
- /* Register busy handler */
- if (sqlite3_busy_handler(alarmmgr_tool_db, __db_busyhandler, NULL) != SQLITE_OK) {
- ALARM_MGR_EXCEPTION_PRINT("Failed to register the busy handler");
- return_code = ERR_ALARM_SYSTEM_FAIL;
- goto done;
- }
-
- /* Drop a table */
- if (sqlite3_exec(alarmmgr_tool_db, query_for_deleting_table, NULL, NULL, &error_message) != SQLITE_OK) {
- ALARM_MGR_EXCEPTION_PRINT("Deleting the table is failed. error message = %s", error_message);
- sqlite3_free(error_message);
- }
-
- /* Create a table if it does not exist */
- if (sqlite3_exec(alarmmgr_tool_db, query_for_creating_table, NULL, NULL, &error_message) != SQLITE_OK) {
- ALARM_MGR_EXCEPTION_PRINT("Creating the table is failed. error message = %s", error_message);
- sqlite3_free(error_message);
- sqlite3_close(alarmmgr_tool_db);
- return_code = ERR_ALARM_SYSTEM_FAIL;
- goto done;
- }
-
- /* Get information of all alarms and save those into the DB. */
- for (gs_iter = alarm_context.alarms; gs_iter != NULL; gs_iter = g_slist_next(gs_iter)) {
- entry = (__alarm_info_t*)gs_iter->data;
- if (uid >= REGULAR_UID_MIN && entry->uid != uid)
- continue;
- ++index;
- SECURE_LOGD("#%d alarm id[%d] app_name[%s] duetime[%ld]",
- index, entry->alarm_id, entry->app_unique_name, entry->start);
-
- alarm_info_t *alarm_info = (alarm_info_t *) &(entry->alarm_info);
- alarm_mode_t *mode = &alarm_info->mode;
-
- char *query = sqlite3_mprintf("insert into alarmmgr_tool( alarm_id, duetime_epoch, duetime, start_epoch,\
- end_epoch, pid, global, caller_pkgid, callee_pkgid, app_unique_name, app_service_name, dst_service_name, day_of_week, repeat, alarm_type)\
- values (%d,%d,%Q,%d,%d,%d,%d,%Q,%Q,%Q,%Q,%Q,%d,%d,%d)",
- entry->alarm_id,
- (int)entry->due_time,
- ctime(&(entry->due_time)),
- (int)entry->start,
- (int)entry->end,
- (int)entry->pid,
- (bool)entry->global,
- CHECK_NULL_STRING(entry->caller_pkgid),
- CHECK_NULL_STRING(entry->callee_pkgid),
- CHECK_NULL_STRING(entry->app_unique_name),
- CHECK_NULL_STRING(entry->app_service_name),
- CHECK_NULL_STRING(entry->dst_service_name),
- mode->u_interval.day_of_week,
- mode->repeat,
- entry->alarm_info.alarm_type);
-
- if (sqlite3_exec(alarmmgr_tool_db, query, NULL, NULL, &error_message) != SQLITE_OK) {
- SECURE_LOGE("sqlite3_exec() is failed. error message = %s", error_message);
- sqlite3_free(error_message);
- }
+ return_code = _get_db_path_for_all_info(uid, &db_path);
- sqlite3_free(query);
- }
- return_code = ALARMMGR_RESULT_SUCCESS;
-
-done:
g_dbus_method_invocation_return_value(invoc, g_variant_new("(si)", db_path, return_code));
- if (alarmmgr_tool_db)
- sqlite3_close(alarmmgr_tool_db);
+
if (db_path)
free(db_path);
+
return true;
}
@@ -4048,7 +3900,7 @@ gboolean alarm_manager_alarm_set_global(AlarmManager *pObject, GDBusMethodInvoca
retval = pkgmgrinfo_pkginfo_is_global(handle, &is_global);
if (retval == PMINFO_R_OK && is_global) {
entry->global = global;
- if (!__alarm_set_global_to_db(entry, global))
+ if (!_alarm_set_global_to_db(entry, global))
return_code = ERR_ALARM_SYSTEM_FAIL;
} else if (retval == PMINFO_R_OK && !is_global) {
return_code = ERR_ALARM_NOT_PERMITTED_APP;
@@ -4189,7 +4041,7 @@ static void __initialize_alarm_list()
alarm_context.alarms = NULL;
alarm_context.c_due_time = -1;
- _load_alarms_from_registry();
+ _load_alarms_from_db();
__rtc_set(); /*Set RTC1 Alarm with alarm due time for alarm-manager initialization*/
}
@@ -4336,144 +4188,6 @@ static bool __initialize_dbus()
return true;
}
-#define ALARMMGR_DB_FILE tzplatform_mkpath(TZ_SYS_DB, ".alarmmgr.db")
-#define QUERY_CREATE_TABLE_ALARMMGR "create table if not exists alarmmgr \
- (alarm_id integer primary key,\
- start integer,\
- end integer,\
- uid integer,\
- pid integer,\
- global integer,\
- is_disabled integer,\
- caller_pkgid text,\
- callee_pkgid text,\
- app_unique_name text,\
- app_service_name text,\
- app_service_name_mod text,\
- bundle text, \
- noti_len integer,\
- noti text, \
- year integer,\
- month integer,\
- day integer,\
- hour integer,\
- min integer,\
- sec integer,\
- msec integer,\
- day_of_week integer,\
- repeat integer,\
- alarm_type integer,\
- reserved_info integer,\
- dst_service_name text, \
- dst_service_name_mod text \
- )"
-
-static int __db_busyhandler(void *pData, int count)
-{
- if (5 - count > 0) {
- struct timespec time = {
- .tv_sec = 0,
- .tv_nsec = (count + 1) * 100 * 1000 * 1000
- };
- nanosleep(&time, NULL);
- ALARM_MGR_LOG_PRINT("alarmmgr_db: busy handler called. count: %d", count + 1);
- return 1;
- } else {
- ALARM_MGR_LOG_PRINT("alarmmgr_db: busy handler will return SQLITE_BUSY error");
- return 0;
- }
-}
-
-int check_callback(void *pid, int argc, char **argv, char **notUsed2)
-{
- if (strcmp(argv[0], "ok") != 0) {
- ALARM_MGR_EXCEPTION_PRINT("check integrity result : %s" , argv[0]);
- is_db_corrupted = true;
- return -1;
- } else {
- ALARM_MGR_LOG_PRINT("check integrity result : %s" , argv[0]);
- }
-
- return 0;
-}
-
-static bool __initialize_db()
-{
- char *error_message = NULL;
- int ret;
-
- /* Create or Open the DB file */
- ret = sqlite3_open(ALARMMGR_DB_FILE, &alarmmgr_db);
- if (ret != SQLITE_OK) {
- ALARM_MGR_EXCEPTION_PRINT("Failed to open [%s]. error: %s", ALARMMGR_DB_FILE, sqlite3_errmsg(alarmmgr_db));
- if (ret == SQLITE_CORRUPT)
- goto recover;
- else
- return false;
- }
- /* Register busy handler */
- ret = sqlite3_busy_handler(alarmmgr_db, __db_busyhandler, NULL);
- if (ret != SQLITE_OK) {
- ALARM_MGR_EXCEPTION_PRINT("Failed to register the busy handler");
- if (ret == SQLITE_CORRUPT) {
- goto recover;
- } else {
- sqlite3_close(alarmmgr_db);
- return false;
- }
- }
- /* Create alarmmgr table */
- ret = sqlite3_exec(alarmmgr_db, QUERY_CREATE_TABLE_ALARMMGR, NULL, NULL, &error_message);
- if (ret != SQLITE_OK) {
- ALARM_MGR_EXCEPTION_PRINT("Don't execute query = %s, error message = %s", QUERY_CREATE_TABLE_ALARMMGR, error_message);
- if (ret == SQLITE_CORRUPT || ret == SQLITE_NOTADB) {
- goto recover;
- } else {
- sqlite3_close(alarmmgr_db);
- sqlite3_free(error_message);
- return false;
- }
- }
-
- /* Check integrity of DB */
- ret = sqlite3_exec(alarmmgr_db, "PRAGMA integrity_check", check_callback, NULL, 0);
- if (ret != SQLITE_OK || is_db_corrupted) {
- ALARM_MGR_EXCEPTION_PRINT("Loss alarm db's integrity");
- goto recover;
- }
-
- return true;
-
-recover:
- if (alarmmgr_db)
- sqlite3_close(alarmmgr_db);
- if (error_message)
- sqlite3_free(error_message);
- unlink(ALARMMGR_DB_FILE);
-
- ret = sqlite3_open(ALARMMGR_DB_FILE, &alarmmgr_db);
- if (ret != SQLITE_OK) {
- ALARM_MGR_EXCEPTION_PRINT("[recover] Failed to open [%s]. error: %s", ALARMMGR_DB_FILE, sqlite3_errmsg(alarmmgr_db));
- return false;
- }
-
- ret = sqlite3_busy_handler(alarmmgr_db, __db_busyhandler, NULL);
- if (ret != SQLITE_OK) {
- ALARM_MGR_EXCEPTION_PRINT("[recover] Failed to register the busy handler");
- sqlite3_close(alarmmgr_db);
- return false;
- }
-
- ret = sqlite3_exec(alarmmgr_db, QUERY_CREATE_TABLE_ALARMMGR, NULL, NULL, &error_message);
- if (ret != SQLITE_OK) {
- ALARM_MGR_EXCEPTION_PRINT("[recover] Don't execute query = %s, error message = %s", QUERY_CREATE_TABLE_ALARMMGR, error_message);
- sqlite3_close(alarmmgr_db);
- sqlite3_free(error_message);
- return false;
- }
-
- return true;
-}
void _alarm_initialize()
{
@@ -4500,7 +4214,7 @@ void _alarm_initialize()
#endif
__initialize_scheduled_alarm_list();
- __initialize_db();
+ _initialize_db();
__initialize_alarm_list();
__initialize_noti();
diff --git a/unittest/alarmmanager_unittest.cpp b/unittest/alarmmanager_unittest.cpp
index 8361a82..342d377 100755
--- a/unittest/alarmmanager_unittest.cpp
+++ b/unittest/alarmmanager_unittest.cpp
@@ -24,7 +24,7 @@
#include "alarm-manager.c"
#include "alarm-manager-schedule.c"
#include "alarm-manager-timer.c"
-#include "alarm-manager-registry.c"
+#include "alarm-manager-db.c"
#include "mock/alarm_dbus.h"
#include "mock/cynara_fake.h"