summaryrefslogtreecommitdiff
path: root/server/alarm-manager-db.c
blob: a6fa50e039d43f691a4af4be52b43358f21cdba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
 * Copyright (c) 2000 - 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.
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>

#include <sqlite3.h>
#include <glib.h>
#include <tzplatform_config.h>
#if !GLIB_CHECK_VERSION(2, 31, 0)
#include <glib/gmacros.h>
#endif

#include "alarm-manager-db.h"

#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,\
						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;

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);
		LOGD("alarmmgr_db: busy handler called. count: %d", count + 1);
		return 1;
	} else {
		LOGD("alarmmgr_db: busy handler will return SQLITE_BUSY error");
		return 0;
	}
}

bool _save_alarms(__alarm_info_t *__alarm_info)
{
	char *error_message = NULL;
	alarm_info_t *alarm_info =
	    (alarm_info_t *) &(__alarm_info->alarm_info);
	alarm_date_t *start = &alarm_info->start;
	alarm_mode_t *mode = &alarm_info->mode;

	char *query = sqlite3_mprintf("insert into alarmmgr( alarm_id, start,\
			end, uid, global, is_disabled, caller_pkgid, callee_pkgid, app_unique_name,\
			app_service_name, app_service_name_mod, bundle, noti_len, noti, year,\
			month, day, hour, min, sec, msec, day_of_week, repeat,\
			alarm_type, reserved_info, dst_service_name, dst_service_name_mod)\
			values (%d,%lld,%lld,%d,%d,0,%Q,%Q,%Q,%Q,%Q,%Q,%d,%Q,%d,%d,%d,%d,%d,%d,%d,%d,%d,\
			%d,%lld,%Q,%Q)",\
			__alarm_info->alarm_id,
			(gint64)__alarm_info->start,
			(gint64)__alarm_info->end,
			__alarm_info->uid,
			__alarm_info->global,
			CHECK_NULL_STRING(__alarm_info->caller_pkgid),
			CHECK_NULL_STRING(__alarm_info->callee_pkgid),
			CHECK_NULL_STRING(__alarm_info->app_unique_name),
			CHECK_NULL_STRING(__alarm_info->app_service_name),
			CHECK_NULL_STRING(__alarm_info->app_service_name_mod),
			CHECK_NULL_STRING(__alarm_info->bundle),
			__alarm_info->noti ? strlen(__alarm_info->noti) : 0,
			CHECK_NULL_STRING(__alarm_info->noti),
			start->year,
			start->month,
			start->day,
			start->hour,
			start->min,
			start->sec,
			alarm_info->msec,
			mode->u_interval.day_of_week,
			mode->repeat,
			alarm_info->alarm_type,
			(gint64)alarm_info->reserved_info,
			CHECK_NULL_STRING(__alarm_info->dst_service_name),
			CHECK_NULL_STRING(__alarm_info->dst_service_name_mod));

	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(error_message);
		sqlite3_free(query);
		return false;
	}

	sqlite3_free(query);
	return true;
}

bool _update_alarms(__alarm_info_t *__alarm_info)
{
	char *error_message = NULL;
	alarm_info_t *alarm_info =
	    (alarm_info_t *) &(__alarm_info->alarm_info);
	alarm_date_t *start = &alarm_info->start;
	alarm_mode_t *mode = &alarm_info->mode;

	char *query = sqlite3_mprintf("update alarmmgr set start=%lld, end=%lld,\
			uid=%d, global=%d, is_disabled=0, caller_pkgid=%Q, callee_pkgid=%Q, app_unique_name=%Q, app_service_name=%Q, app_service_name_mod=%Q,\
			bundle=%Q, noti_len=%d, noti=%Q, year=%d, month=%d, day=%d, hour=%d, min=%d, sec=%d, msec=%d,\
			day_of_week=%d, repeat=%d, alarm_type=%d,\
			reserved_info=%lld, dst_service_name=%Q, dst_service_name_mod=%Q\
			where alarm_id=%d",\
			(gint64)__alarm_info->start,
			(gint64)__alarm_info->end,
			__alarm_info->uid,
			__alarm_info->global,
			CHECK_NULL_STRING(__alarm_info->caller_pkgid),
			CHECK_NULL_STRING(__alarm_info->callee_pkgid),
			CHECK_NULL_STRING(__alarm_info->app_unique_name),
			CHECK_NULL_STRING(__alarm_info->app_service_name),
			CHECK_NULL_STRING(__alarm_info->app_service_name_mod),
			CHECK_NULL_STRING(__alarm_info->bundle),
			__alarm_info->noti ? strlen(__alarm_info->noti) : 0,
			CHECK_NULL_STRING(__alarm_info->noti),
			start->year,
			start->month,
			start->day,
			start->hour,
			start->min,
			start->sec,
			alarm_info->msec,
			mode->u_interval.day_of_week,
			mode->repeat,
			alarm_info->alarm_type,
			(gint64)alarm_info->reserved_info,
			CHECK_NULL_STRING(__alarm_info->dst_service_name),
			CHECK_NULL_STRING(__alarm_info->dst_service_name_mod),
			__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(error_message);
		sqlite3_free(query);
		return false;
	}

	sqlite3_free(query);
	return true;
}

bool _delete_alarms(alarm_id_t alarm_id)
{
	char *error_message = NULL;
	char *query = sqlite3_mprintf("delete from alarmmgr where alarm_id=%d", 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(error_message);
		sqlite3_free(query);
		return false;
	}

	sqlite3_free(query);
	return true;
}

void _load_alarms_from_db()
{
	int i = 0;
	int col_idx;
	const char *query = "select * from alarmmgr";
	const char *null_str = "null";
	sqlite3_stmt *stmt = NULL;
	const char *tail = NULL;
	alarm_info_t *alarm_info = NULL;
	__alarm_info_t *__alarm_info = NULL;
	alarm_date_t *start = NULL;
	alarm_mode_t *mode = NULL;
	int is_disabled;
	char caller_pkgid[MAX_PKG_ID_LEN] = {0,};
	char callee_pkgid[MAX_PKG_ID_LEN] = {0,};
	char app_unique_name[MAX_APP_ID_LEN] = {0,};
	char app_service_name[MAX_SERVICE_NAME_LEN] = {0,};
	char app_service_name_mod[MAX_SERVICE_NAME_LEN] = {0,};
	char dst_service_name[MAX_SERVICE_NAME_LEN] = {0,};
	char dst_service_name_mod[MAX_SERVICE_NAME_LEN] = {0,};
	char bundle[MAX_BUNDLE_NAME_LEN] = {0,};
	int noti_len;
	char *noti;
	gint64 start_64, end_64, reserved_info_64;

	if (SQLITE_OK != sqlite3_prepare(alarmmgr_db, query, strlen(query), &stmt, &tail)) {
		LOGE("sqlite3_prepare() is failed.");
		return;
	}

	for (i = 0; SQLITE_ROW == sqlite3_step(stmt); i++) {
		col_idx = 0;
		__alarm_info = (__alarm_info_t *)calloc(1, sizeof(__alarm_info_t));

		if (G_UNLIKELY(__alarm_info == NULL)) {
			LOGE("Memory allocation failed.");
			goto done;
		}
		alarm_info = (alarm_info_t *) &(__alarm_info->alarm_info);
		start = &alarm_info->start;
		mode = &alarm_info->mode;

		__alarm_info->alarm_id = sqlite3_column_int(stmt, col_idx++);
		start_64 = sqlite3_column_int64(stmt, col_idx++);
		__alarm_info->start = (time_t)start_64;
		end_64 = sqlite3_column_int64(stmt, col_idx++);
		__alarm_info->end = (time_t)end_64;
		__alarm_info->uid = sqlite3_column_int(stmt, col_idx++);
		__alarm_info->global = sqlite3_column_int(stmt, col_idx++);
		is_disabled = sqlite3_column_int(stmt, col_idx++);

		strncpy(caller_pkgid, (const char *)sqlite3_column_text(stmt, col_idx++),
			MAX_PKG_ID_LEN - 1);
		strncpy(callee_pkgid, (const char *)sqlite3_column_text(stmt, col_idx++),
			MAX_PKG_ID_LEN - 1);
		strncpy(app_unique_name, (const char *)sqlite3_column_text(stmt, col_idx++),
			MAX_APP_ID_LEN - 1);
		strncpy(app_service_name, (const char *)sqlite3_column_text(stmt, col_idx++),
			MAX_SERVICE_NAME_LEN - 1);
		strncpy(app_service_name_mod, (const char *)sqlite3_column_text(stmt, col_idx++),
			MAX_SERVICE_NAME_LEN - 1);
		strncpy(bundle, (const char *)sqlite3_column_text(stmt, col_idx++),
			MAX_BUNDLE_NAME_LEN - 1);
		noti_len = sqlite3_column_int(stmt, col_idx++);
		noti_len = noti_len ? noti_len : strlen(null_str);
		noti = (char *)calloc(1, noti_len + 1);
		strncpy(noti, (const char *)sqlite3_column_text(stmt, col_idx++),
			noti_len);
		start->year = sqlite3_column_int(stmt, col_idx++);
		start->month = sqlite3_column_int(stmt, col_idx++);
		start->day = sqlite3_column_int(stmt, col_idx++);
		start->hour = sqlite3_column_int(stmt, col_idx++);
		start->min = sqlite3_column_int(stmt, col_idx++);
		start->sec = sqlite3_column_int(stmt, col_idx++);
		alarm_info->msec = sqlite3_column_int(stmt, col_idx++);
		mode->u_interval.day_of_week = sqlite3_column_int(stmt, col_idx++);
		mode->repeat = (alarm_repeat_mode_t)sqlite3_column_int(stmt, col_idx++);
		alarm_info->alarm_type = sqlite3_column_int(stmt, col_idx++);
		reserved_info_64 = sqlite3_column_int64(stmt, col_idx++);
		alarm_info->reserved_info = (time_t)reserved_info_64;
		strncpy(dst_service_name, (const char *)sqlite3_column_text(stmt, col_idx++),
			MAX_SERVICE_NAME_LEN - 1);
		strncpy(dst_service_name_mod, (const char *)sqlite3_column_text(stmt, col_idx++),
			MAX_SERVICE_NAME_LEN - 1);

		__alarm_info->caller_pkgid = STRDUP_WITH_NULLCMP(caller_pkgid);
		__alarm_info->callee_pkgid = STRDUP_WITH_NULLCMP(callee_pkgid);
		__alarm_info->app_unique_name = STRDUP_WITH_NULLCMP(app_unique_name);
		__alarm_info->app_service_name = STRDUP_WITH_NULLCMP(app_service_name);
		__alarm_info->app_service_name_mod =
			STRDUP_WITH_NULLCMP(app_service_name_mod);
		__alarm_info->dst_service_name = STRDUP_WITH_NULLCMP(dst_service_name);
		__alarm_info->dst_service_name_mod =
			STRDUP_WITH_NULLCMP(dst_service_name_mod);
		__alarm_info->bundle = STRDUP_WITH_NULLCMP(bundle);
		__alarm_info->noti = STRDUP_WITH_NULLCMP(noti);
		free(noti);

		if (is_disabled) {
			_alarm_set_next_duetime(__alarm_info);
			g_disabled_alarm_list = g_slist_append(g_disabled_alarm_list, __alarm_info);
			LOGW("Save alarm_id[%d] caller[%s] callee[%s]", __alarm_info->alarm_id, caller_pkgid, callee_pkgid);
		} else {
			_alarm_set_next_duetime(__alarm_info);
			alarm_context.alarms = g_slist_append(alarm_context.alarms, __alarm_info);
		}
	}
done:
	_alarm_schedule();
	if (sqlite3_finalize(stmt) != SQLITE_OK)
		LOGE("sqlite3_finalize() is failed.");

	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,\
							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) {
		LOGE("Out of memory");
		return ERR_ALARM_SYSTEM_FAIL;
	}

	if (sqlite3_open(db_path_tmp, &alarmmgr_tool_db) != SQLITE_OK) {
		LOGE("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) {
		LOGE("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) {
		LOGE("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) {
		LOGE("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, 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,%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,
				(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;

	LOGW("Update (%d) is_disabled to (%d)", alarm_id, disabled);
	char *query = sqlite3_mprintf("update alarmmgr set is_disabled=%d where alarm_id=%d", disabled, alarm_id);

	if (SQLITE_OK != sqlite3_exec(alarmmgr_db, query, NULL, NULL, &error_message)) {
		SECURE_LOGE("Failed to update the DB table. error message = %s", error_message);
		sqlite3_free(error_message);
	}

	sqlite3_free(query);
	return;
}

int __check_callback(void *pid, int argc, char **argv, char **notUsed2)
{
	if (strcmp(argv[0], "ok") != 0) {
		LOGE("check integrity result : %s" , argv[0]);
		is_db_corrupted = true;
		return -1;
	} else {
		LOGD("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) {
		LOGE("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) {
		LOGE("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) {
		LOGE("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) {
		LOGE("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) {
		LOGE("[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) {
		LOGE("[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) {
		LOGE("[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;
}